root/net/core/dev.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. min
  2. dev_add_pack
  3. dev_remove_pack
  4. dev_get
  5. dev_load
  6. dev_open
  7. dev_close
  8. register_netdevice_notifier
  9. unregister_netdevice_notifier
  10. dev_queue_xmit
  11. netif_rx
  12. dev_transmit
  13. net_bh
  14. dev_tint
  15. dev_ifconf
  16. sprintf_stats
  17. dev_get_info
  18. bad_mask
  19. dev_ifsioc
  20. dev_ioctl
  21. net_dev_init

   1 /*
   2  *      NET3    Protocol independent device support routines.
   3  *
   4  *              This program is free software; you can redistribute it and/or
   5  *              modify it under the terms of the GNU General Public License
   6  *              as published by the Free Software Foundation; either version
   7  *              2 of the License, or (at your option) any later version.
   8  *
   9  *      Derived from the non IP parts of dev.c 1.0.19
  10  *              Authors:        Ross Biro, <bir7@leland.Stanford.Edu>
  11  *                              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *                              Mark Evans, <evansmp@uhura.aston.ac.uk>
  13  *
  14  *      Additional Authors:
  15  *              Florian la Roche <rzsfl@rz.uni-sb.de>
  16  *              Alan Cox <gw4pts@gw4pts.ampr.org>
  17  *              David Hinds <dhinds@allegro.stanford.edu>
  18  *
  19  *      Changes:
  20  *              Alan Cox        :       device private ioctl copies fields back.
  21  *              Alan Cox        :       Transmit queue code does relevant stunts to
  22  *                                      keep the queue safe.
  23  *              Alan Cox        :       Fixed double lock.
  24  *              Alan Cox        :       Fixed promisc NULL pointer trap
  25  *              ????????        :       Support the full private ioctl range
  26  *              Alan Cox        :       Moved ioctl permission check into drivers
  27  *              Tim Kordas      :       SIOCADDMULTI/SIOCDELMULTI
  28  *              Alan Cox        :       100 backlog just doesn't cut it when
  29  *                                      you start doing multicast video 8)
  30  *              Alan Cox        :       Rewrote net_bh and list manager.
  31  *              Alan Cox        :       Fix ETH_P_ALL echoback lengths.
  32  *              Alan Cox        :       Took out transmit every packet pass
  33  *                                      Saved a few bytes in the ioctl handler
  34  *              Alan Cox        :       Network driver sets packet type before calling netif_rx. Saves
  35  *                                      a function call a packet.
  36  *              Alan Cox        :       Hashed net_bh()
  37  *              Richard Kooijman:       Timestamp fixes.
  38  *              Alan Cox        :       Wrong field in SIOCGIFDSTADDR
  39  *              Alan Cox        :       Device lock protection.
  40  *              Alan Cox        :       Fixed nasty side effect of device close changes.
  41  *              Rudi Cilibrasi  :       Pass the right thing to set_mac_address()
  42  *              Dave Miller     :       32bit quantity for the device lock to make it work out
  43  *                                      on a Sparc.
  44  *              Bjorn Ekwall    :       Added KERNELD hack.
  45  *              Alan Cox        :       Cleaned up the backlog initialise.
  46  *              Craig Metz      :       SIOCGIFCONF fix if space for under
  47  *                                      1 device.
  48  *
  49  */
  50 
  51 #include <asm/segment.h>
  52 #include <asm/system.h>
  53 #include <asm/bitops.h>
  54 #include <linux/config.h>
  55 #include <linux/types.h>
  56 #include <linux/kernel.h>
  57 #include <linux/sched.h>
  58 #include <linux/string.h>
  59 #include <linux/mm.h>
  60 #include <linux/socket.h>
  61 #include <linux/sockios.h>
  62 #include <linux/in.h>
  63 #include <linux/errno.h>
  64 #include <linux/interrupt.h>
  65 #include <linux/if_ether.h>
  66 #include <linux/inet.h>
  67 #include <linux/netdevice.h>
  68 #include <linux/etherdevice.h>
  69 #include <linux/notifier.h>
  70 #include <net/ip.h>
  71 #include <net/route.h>
  72 #include <linux/skbuff.h>
  73 #include <net/sock.h>
  74 #include <net/arp.h>
  75 #include <net/slhc.h>
  76 #include <linux/proc_fs.h>
  77 #include <linux/stat.h>
  78 #ifdef CONFIG_NET_ALIAS
  79 #include <linux/net_alias.h>
  80 #endif
  81 #ifdef CONFIG_KERNELD
  82 #include <linux/kerneld.h>
  83 #endif
  84 
  85 /*
  86  *      The list of packet types we will receive (as opposed to discard)
  87  *      and the routines to invoke.
  88  */
  89 
  90 struct packet_type *ptype_base[16];
  91 struct packet_type *ptype_all = NULL;           /* Taps */
  92 
  93 /*
  94  *      Device list lock
  95  */
  96  
  97 int dev_lockct=0;
  98  
  99 /*
 100  *      Our notifier list
 101  */
 102  
 103 struct notifier_block *netdev_chain=NULL;
 104 
 105 /*
 106  *      Device drivers call our routines to queue packets here. We empty the
 107  *      queue in the bottom half handler.
 108  */
 109 
 110 static struct sk_buff_head backlog;
 111 
 112 /* 
 113  *      We don't overdo the queue or we will thrash memory badly.
 114  */
 115  
 116 static int backlog_size = 0;
 117 
 118 /*
 119  *      Return the lesser of the two values. 
 120  */
 121  
 122 static __inline__ unsigned long min(unsigned long a, unsigned long b)
     /* [previous][next][first][last][top][bottom][index][help] */
 123 {
 124         return (a < b)? a : b;
 125 }
 126 
 127 
 128 /******************************************************************************************
 129 
 130                 Protocol management and registration routines
 131 
 132 *******************************************************************************************/
 133 
 134 /*
 135  *      For efficiency
 136  */
 137 
 138 static int dev_nit=0;
 139 
 140 /*
 141  *      Add a protocol ID to the list. Now that the input handler is
 142  *      smarter we can dispense with all the messy stuff that used to be
 143  *      here.
 144  */
 145  
 146 void dev_add_pack(struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148         int hash;
 149         if(pt->type==htons(ETH_P_ALL))
 150         {
 151                 dev_nit++;
 152                 pt->next=ptype_all;
 153                 ptype_all=pt;
 154         }
 155         else
 156         {       
 157                 hash=ntohs(pt->type)&15;
 158                 pt->next = ptype_base[hash];
 159                 ptype_base[hash] = pt;
 160         }
 161 }
 162 
 163 
 164 /*
 165  *      Remove a protocol ID from the list.
 166  */
 167  
 168 void dev_remove_pack(struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170         struct packet_type **pt1;
 171         if(pt->type==htons(ETH_P_ALL))
 172         {
 173                 dev_nit--;
 174                 pt1=&ptype_all;
 175         }
 176         else
 177                 pt1=&ptype_base[ntohs(pt->type)&15];
 178         for(; (*pt1)!=NULL; pt1=&((*pt1)->next))
 179         {
 180                 if(pt==(*pt1))
 181                 {
 182                         *pt1=pt->next;
 183                         return;
 184                 }
 185         }
 186         printk("dev_remove_pack: %p not found.\n", pt);
 187 }
 188 
 189 /*****************************************************************************************
 190 
 191                             Device Interface Subroutines
 192 
 193 ******************************************************************************************/
 194 
 195 /* 
 196  *      Find an interface by name.
 197  */
 198  
 199 struct device *dev_get(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         struct device *dev;
 202 
 203         for (dev = dev_base; dev != NULL; dev = dev->next) 
 204         {
 205                 if (strcmp(dev->name, name) == 0)
 206                         return(dev);
 207         }
 208         return NULL;
 209 }
 210         
 211 /*
 212  *      Find and possibly load an interface.
 213  */
 214  
 215 #ifdef CONFIG_KERNELD
 216 
 217 extern __inline__ void dev_load(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219         if(!dev_get(name)) {
 220 #ifdef CONFIG_NET_ALIAS
 221                 const char *sptr;
 222  
 223                 for (sptr=name ; *sptr ; sptr++) if(*sptr==':') break;
 224                 if (!(*sptr && *(sptr+1)))
 225 #endif
 226                         request_module(name);
 227         }
 228 }
 229 
 230 #endif
 231  
 232 /*
 233  *      Prepare an interface for use. 
 234  */
 235  
 236 int dev_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 237 {
 238         int ret = 0;
 239 
 240         /*
 241          *      Call device private open method
 242          */
 243         if (dev->open) 
 244                 ret = dev->open(dev);
 245 
 246         /*
 247          *      If it went open OK then set the flags
 248          */
 249          
 250         if (ret == 0) 
 251         {
 252                 dev->flags |= (IFF_UP | IFF_RUNNING);
 253                 /*
 254                  *      Initialise multicasting status 
 255                  */
 256                 dev_mc_upload(dev);
 257                 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
 258         }
 259         return(ret);
 260 }
 261 
 262 
 263 /*
 264  *      Completely shutdown an interface.
 265  */
 266  
 267 int dev_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 268 {
 269         int ct=0;
 270 
 271         /*
 272          *      Call the device specific close. This cannot fail.
 273          *      Only if device is UP
 274          */
 275          
 276         if ((dev->flags & IFF_UP) && dev->stop)
 277                 dev->stop(dev);
 278 
 279         /*
 280          *      Device is now down.
 281          */
 282          
 283         dev->flags&=~(IFF_UP|IFF_RUNNING);
 284 
 285         /*
 286          *      Tell people we are going down
 287          */
 288         notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
 289         /*
 290          *      Flush the multicast chain
 291          */
 292         dev_mc_discard(dev);
 293         /*
 294          *      Blank the IP addresses
 295          */
 296         dev->pa_addr = 0;
 297         dev->pa_dstaddr = 0;
 298         dev->pa_brdaddr = 0;
 299         dev->pa_mask = 0;
 300         /*
 301          *      Purge any queued packets when we down the link 
 302          */
 303         while(ct<DEV_NUMBUFFS)
 304         {
 305                 struct sk_buff *skb;
 306                 while((skb=skb_dequeue(&dev->buffs[ct]))!=NULL)
 307                         if(skb->free)
 308                                 kfree_skb(skb,FREE_WRITE);
 309                 ct++;
 310         }
 311         return(0);
 312 }
 313 
 314 
 315 /*
 316  *      Device change register/unregister. These are not inline or static
 317  *      as we export them to the world.
 318  */
 319 
 320 int register_netdevice_notifier(struct notifier_block *nb)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {
 322         return notifier_chain_register(&netdev_chain, nb);
 323 }
 324 
 325 int unregister_netdevice_notifier(struct notifier_block *nb)
     /* [previous][next][first][last][top][bottom][index][help] */
 326 {
 327         return notifier_chain_unregister(&netdev_chain,nb);
 328 }
 329 
 330 /*
 331  *      Send (or queue for sending) a packet. 
 332  *
 333  *      IMPORTANT: When this is called to resend frames. The caller MUST
 334  *      already have locked the sk_buff. Apart from that we do the
 335  *      rest of the magic.
 336  */
 337 
 338 void dev_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
     /* [previous][next][first][last][top][bottom][index][help] */
 339 {
 340         unsigned long flags;
 341         struct sk_buff_head *list;
 342         int retransmission = 0; /* used to say if the packet should go  */
 343                                 /* at the front or the back of the      */
 344                                 /* queue - front is a retransmit try    */
 345 
 346         if(pri>=0 && !skb_device_locked(skb))
 347                 skb_device_lock(skb);   /* Shove a lock on the frame */
 348 #if CONFIG_SKB_CHECK 
 349         IS_SKB(skb);
 350 #endif    
 351         skb->dev = dev;
 352 
 353         /*
 354          *      Negative priority is used to flag a frame that is being pulled from the
 355          *      queue front as a retransmit attempt. It therefore goes back on the queue
 356          *      start on a failure.
 357          */
 358          
 359         if (pri < 0) 
 360         {
 361                 pri = -pri-1;
 362                 retransmission = 1;
 363         }
 364 
 365 #ifdef CONFIG_NET_DEBUG
 366         if (pri >= DEV_NUMBUFFS) 
 367         {
 368                 printk("bad priority in dev_queue_xmit.\n");
 369                 pri = 1;
 370         }
 371 #endif
 372 
 373         /*
 374          *      If the address has not been resolved. Call the device header rebuilder.
 375          *      This can cover all protocols and technically not just ARP either.
 376          */
 377          
 378         if (!skb->arp && dev->rebuild_header(skb->data, dev, skb->raddr, skb)) {
 379                 return;
 380         }
 381 
 382         /*
 383          *
 384          *      If dev is an alias, switch to its main device.
 385          *      "arp" resolution has been made with alias device, so
 386          *      arp entries refer to alias, not main.
 387          *
 388          */
 389 
 390 #ifdef CONFIG_NET_ALIAS
 391         if (net_alias_is(dev))
 392                 skb->dev = dev = net_alias_main_dev(dev);
 393 #endif
 394         list = dev->buffs + pri;
 395 
 396         save_flags(flags);
 397         /* if this isn't a retransmission, use the first packet instead... */
 398         if (!retransmission) {
 399                 if (skb_queue_len(list)) {
 400                         /* avoid overrunning the device queue.. */
 401                         if (skb_queue_len(list) > dev->tx_queue_len) {
 402                                 dev_kfree_skb(skb, FREE_WRITE);
 403                                 return;
 404                         }
 405                         cli();
 406                         skb_device_unlock(skb);         /* Buffer is on the device queue and can be freed safely */
 407                         __skb_queue_tail(list, skb);
 408                         skb = __skb_dequeue(list);
 409                         skb_device_lock(skb);           /* New buffer needs locking down */
 410                         restore_flags(flags);
 411                 }
 412                 
 413                 /* copy outgoing packets to any sniffer packet handlers */
 414                 if (dev_nit) {
 415                         struct packet_type *ptype;
 416                         skb->stamp=xtime;
 417                         for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next) 
 418                         {
 419                                 /* Never send packets back to the socket
 420                                  * they originated from - MvS (miquels@drinkel.ow.org)
 421                                  */
 422                                 if ((ptype->dev == dev || !ptype->dev) &&
 423                                    ((struct sock *)ptype->data != skb->sk))
 424                                 {
 425                                         struct sk_buff *skb2;
 426                                         if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
 427                                                 break;
 428                                         skb2->h.raw = skb2->data + dev->hard_header_len;
 429                                         skb2->mac.raw = skb2->data;
 430                                         ptype->func(skb2, skb->dev, ptype);
 431                                 }
 432                         }
 433                 }
 434         }
 435         start_bh_atomic();
 436         if (dev->hard_start_xmit(skb, dev) == 0) {
 437                 /*
 438                  *      Packet is now solely the responsibility of the driver
 439                  */
 440                 end_bh_atomic();
 441                 return;
 442         }
 443         end_bh_atomic();
 444 
 445         /*
 446          *      Transmission failed, put skb back into a list. Once on the list it's safe and
 447          *      no longer device locked (it can be freed safely from the device queue)
 448          */
 449         cli();
 450         skb_device_unlock(skb);
 451         __skb_queue_head(list,skb);
 452         restore_flags(flags);
 453 }
 454 
 455 /*
 456  *      Receive a packet from a device driver and queue it for the upper
 457  *      (protocol) levels.  It always succeeds. This is the recommended 
 458  *      interface to use.
 459  */
 460 
 461 void netif_rx(struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 462 {
 463         static int dropping = 0;
 464 
 465         /*
 466          *      Any received buffers are un-owned and should be discarded
 467          *      when freed. These will be updated later as the frames get
 468          *      owners.
 469          */
 470 
 471         skb->sk = NULL;
 472         skb->free = 1;
 473         if(skb->stamp.tv_sec==0)
 474                 skb->stamp = xtime;
 475 
 476         /*
 477          *      Check that we aren't overdoing things.
 478          */
 479 
 480         if (!backlog_size)
 481                 dropping = 0;
 482         else if (backlog_size > 300)
 483                 dropping = 1;
 484 
 485         if (dropping) 
 486         {
 487                 kfree_skb(skb, FREE_READ);
 488                 return;
 489         }
 490 
 491         /*
 492          *      Add it to the "backlog" queue. 
 493          */
 494 #if CONFIG_SKB_CHECK
 495         IS_SKB(skb);
 496 #endif  
 497         skb_queue_tail(&backlog,skb);
 498         backlog_size++;
 499   
 500         /*
 501          *      If any packet arrived, mark it for processing after the
 502          *      hardware interrupt returns.
 503          */
 504 
 505 #ifdef CONFIG_NET_RUNONIRQ      /* Dont enable yet, needs some driver mods */
 506         net_bh();
 507 #else
 508         mark_bh(NET_BH);
 509 #endif
 510         return;
 511 }
 512 
 513 /*
 514  *      This routine causes all interfaces to try to send some data. 
 515  */
 516  
 517 void dev_transmit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 518 {
 519         struct device *dev;
 520 
 521         for (dev = dev_base; dev != NULL; dev = dev->next)
 522         {
 523                 if (dev->flags != 0 && !dev->tbusy) {
 524                         /*
 525                          *      Kick the device
 526                          */
 527                         dev_tint(dev);
 528                 }
 529         }
 530 }
 531 
 532 
 533 /**********************************************************************************
 534 
 535                         Receive Queue Processor
 536                         
 537 ***********************************************************************************/
 538 
 539 /*
 540  *      When we are called the queue is ready to grab, the interrupts are
 541  *      on and hardware can interrupt and queue to the receive queue a we
 542  *      run with no problems.
 543  *      This is run as a bottom half after an interrupt handler that does
 544  *      mark_bh(NET_BH);
 545  */
 546  
 547 void net_bh(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 548 {
 549         struct packet_type *ptype;
 550         struct packet_type *pt_prev;
 551         unsigned short type;
 552 
 553         /*
 554          *      Can we send anything now? We want to clear the
 555          *      decks for any more sends that get done as we
 556          *      process the input. This also minimises the
 557          *      latency on a transmit interrupt bh.
 558          */
 559 
 560         dev_transmit();
 561   
 562         /*
 563          *      Any data left to process. This may occur because a
 564          *      mark_bh() is done after we empty the queue including
 565          *      that from the device which does a mark_bh() just after
 566          */
 567 
 568         /*
 569          *      While the queue is not empty..
 570          *
 571          *      Note that the queue never shrinks due to
 572          *      an interrupt, so we can do this test without
 573          *      disabling interrupts.
 574          */
 575 
 576         while (!skb_queue_empty(&backlog)) {
 577                 struct sk_buff * skb = backlog.next;
 578 
 579                 /*
 580                  *      We have a packet. Therefore the queue has shrunk
 581                  */
 582                 cli();
 583                 __skb_unlink(skb, &backlog);
 584                 backlog_size--;
 585                 sti();
 586                 
 587                /*
 588                 *       Bump the pointer to the next structure.
 589                 *
 590                 *       On entry to the protocol layer. skb->data and
 591                 *       skb->h.raw point to the MAC and encapsulated data
 592                 */
 593 
 594                 skb->h.raw = skb->data;
 595 
 596                /*
 597                 *       Fetch the packet protocol ID. 
 598                 */
 599                 
 600                 type = skb->protocol;
 601 
 602                 /*
 603                  *      We got a packet ID.  Now loop over the "known protocols"
 604                  *      list. There are two lists. The ptype_all list of taps (normally empty)
 605                  *      and the main protocol list which is hashed perfectly for normal protocols.
 606                  */
 607                 pt_prev = NULL;
 608                 for (ptype = ptype_all; ptype!=NULL; ptype=ptype->next)
 609                 {
 610                         if(pt_prev)
 611                         {
 612                                 struct sk_buff *skb2=skb_clone(skb, GFP_ATOMIC);
 613                                 if(skb2)
 614                                         pt_prev->func(skb2,skb->dev, pt_prev);
 615                         }
 616                         pt_prev=ptype;
 617                 }
 618                 
 619                 for (ptype = ptype_base[ntohs(type)&15]; ptype != NULL; ptype = ptype->next) 
 620                 {
 621                         if (ptype->type == type && (!ptype->dev || ptype->dev==skb->dev))
 622                         {
 623                                 /*
 624                                  *      We already have a match queued. Deliver
 625                                  *      to it and then remember the new match
 626                                  */
 627                                 if(pt_prev)
 628                                 {
 629                                         struct sk_buff *skb2;
 630 
 631                                         skb2=skb_clone(skb, GFP_ATOMIC);
 632 
 633                                         /*
 634                                          *      Kick the protocol handler. This should be fast
 635                                          *      and efficient code.
 636                                          */
 637 
 638                                         if(skb2)
 639                                                 pt_prev->func(skb2, skb->dev, pt_prev);
 640                                 }
 641                                 /* Remember the current last to do */
 642                                 pt_prev=ptype;
 643                         }
 644                 } /* End of protocol list loop */
 645                 
 646                 /*
 647                  *      Is there a last item to send to ?
 648                  */
 649 
 650                 if(pt_prev)
 651                         pt_prev->func(skb, skb->dev, pt_prev);
 652                 /*
 653                  *      Has an unknown packet has been received ?
 654                  */
 655          
 656                 else
 657                         kfree_skb(skb, FREE_WRITE);
 658 
 659                 /*
 660                  *      Again, see if we can transmit anything now. 
 661                  *      [Ought to take this out judging by tests it slows
 662                  *       us down not speeds us up]
 663                  */
 664 #ifdef XMIT_EVERY
 665                 dev_transmit();
 666 #endif          
 667         }       /* End of queue loop */
 668         
 669         /*
 670          *      We have emptied the queue
 671          */
 672         
 673         /*
 674          *      One last output flush.
 675          */
 676 
 677 #ifdef XMIT_AFTER        
 678         dev_transmit();
 679 #endif
 680 }
 681 
 682 
 683 /*
 684  *      This routine is called when an device driver (i.e. an
 685  *      interface) is ready to transmit a packet.
 686  */
 687  
 688 void dev_tint(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 689 {
 690         int i;
 691         unsigned long flags;
 692         struct sk_buff_head * head;
 693         
 694         /*
 695          * aliases do not transmit (for now :) )
 696          */
 697 
 698 #ifdef CONFIG_NET_ALIAS
 699         if (net_alias_is(dev)) return;
 700 #endif
 701         head = dev->buffs;
 702         save_flags(flags);
 703         cli();
 704 
 705         /*
 706          *      Work the queues in priority order
 707          */      
 708         for(i = 0;i < DEV_NUMBUFFS; i++,head++)
 709         {
 710 
 711                 while (!skb_queue_empty(head)) {
 712                         struct sk_buff *skb;
 713 
 714                         skb = head->next;
 715                         __skb_unlink(skb, head);
 716                         /*
 717                          *      Stop anyone freeing the buffer while we retransmit it
 718                          */
 719                         skb_device_lock(skb);
 720                         restore_flags(flags);
 721                         /*
 722                          *      Feed them to the output stage and if it fails
 723                          *      indicate they re-queue at the front.
 724                          */
 725                         dev_queue_xmit(skb,dev,-i - 1);
 726                         /*
 727                          *      If we can take no more then stop here.
 728                          */
 729                         if (dev->tbusy)
 730                                 return;
 731                         cli();
 732                 }
 733         }
 734         restore_flags(flags);
 735 }
 736 
 737 
 738 /*
 739  *      Perform a SIOCGIFCONF call. This structure will change
 740  *      size shortly, and there is nothing I can do about it.
 741  *      Thus we will need a 'compatibility mode'.
 742  */
 743 
 744 static int dev_ifconf(char *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 745 {
 746         struct ifconf ifc;
 747         struct ifreq ifr;
 748         struct device *dev;
 749         char *pos;
 750         int len;
 751         int err;
 752 
 753         /*
 754          *      Fetch the caller's info block. 
 755          */
 756          
 757         err=verify_area(VERIFY_WRITE, arg, sizeof(struct ifconf));
 758         if(err)
 759                 return err;
 760         memcpy_fromfs(&ifc, arg, sizeof(struct ifconf));
 761         len = ifc.ifc_len;
 762         pos = ifc.ifc_buf;
 763 
 764         /*
 765          *      We now walk the device list filling each active device
 766          *      into the array.
 767          */
 768          
 769         err=verify_area(VERIFY_WRITE,pos,len);
 770         if(err)
 771                 return err;
 772         
 773         /*
 774          *      Loop over the interfaces, and write an info block for each. 
 775          */
 776 
 777         for (dev = dev_base; dev != NULL; dev = dev->next) 
 778         {
 779                 if(!(dev->flags & IFF_UP))      /* Downed devices don't count */
 780                         continue;
 781                 /*
 782                  *      Have we run out of space here ?
 783                  */
 784         
 785                 if (len < sizeof(struct ifreq)) 
 786                         break;
 787 
 788                 memset(&ifr, 0, sizeof(struct ifreq));
 789                 strcpy(ifr.ifr_name, dev->name);
 790                 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = dev->family;
 791                 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
 792 
 793 
 794                 /*
 795                  *      Write this block to the caller's space. 
 796                  */
 797                  
 798                 memcpy_tofs(pos, &ifr, sizeof(struct ifreq));
 799                 pos += sizeof(struct ifreq);
 800                 len -= sizeof(struct ifreq);            
 801         }
 802 
 803         /*
 804          *      All done.  Write the updated control block back to the caller. 
 805          */
 806          
 807         ifc.ifc_len = (pos - ifc.ifc_buf);
 808         ifc.ifc_req = (struct ifreq *) ifc.ifc_buf;
 809         memcpy_tofs(arg, &ifc, sizeof(struct ifconf));
 810         
 811         /*
 812          *      Report how much was filled in
 813          */
 814          
 815         return(pos - arg);
 816 }
 817 
 818 
 819 /*
 820  *      This is invoked by the /proc filesystem handler to display a device
 821  *      in detail.
 822  */
 823 
 824 #ifdef CONFIG_PROC_FS
 825 static int sprintf_stats(char *buffer, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 826 {
 827         struct enet_statistics *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
 828         int size;
 829         
 830         if (stats)
 831                 size = sprintf(buffer, "%6s:%7d %4d %4d %4d %4d %8d %4d %4d %4d %5d %4d\n",
 832                    dev->name,
 833                    stats->rx_packets, stats->rx_errors,
 834                    stats->rx_dropped + stats->rx_missed_errors,
 835                    stats->rx_fifo_errors,
 836                    stats->rx_length_errors + stats->rx_over_errors
 837                    + stats->rx_crc_errors + stats->rx_frame_errors,
 838                    stats->tx_packets, stats->tx_errors, stats->tx_dropped,
 839                    stats->tx_fifo_errors, stats->collisions,
 840                    stats->tx_carrier_errors + stats->tx_aborted_errors
 841                    + stats->tx_window_errors + stats->tx_heartbeat_errors);
 842         else
 843                 size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
 844 
 845         return size;
 846 }
 847 
 848 /*
 849  *      Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
 850  *      to create /proc/net/dev
 851  */
 852  
 853 int dev_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 854 {
 855         int len=0;
 856         off_t begin=0;
 857         off_t pos=0;
 858         int size;
 859         
 860         struct device *dev;
 861 
 862 
 863         size = sprintf(buffer, "Inter-|   Receive                  |  Transmit\n"
 864                             " face |packets errs drop fifo frame|packets errs drop fifo colls carrier\n");
 865         
 866         pos+=size;
 867         len+=size;
 868         
 869 
 870         for (dev = dev_base; dev != NULL; dev = dev->next) 
 871         {
 872                 size = sprintf_stats(buffer+len, dev);
 873                 len+=size;
 874                 pos=begin+len;
 875                                 
 876                 if(pos<offset)
 877                 {
 878                         len=0;
 879                         begin=pos;
 880                 }
 881                 if(pos>offset+length)
 882                         break;
 883         }
 884         
 885         *start=buffer+(offset-begin);   /* Start of wanted data */
 886         len-=(offset-begin);            /* Start slop */
 887         if(len>length)
 888                 len=length;             /* Ending slop */
 889         return len;
 890 }
 891 #endif  /* CONFIG_PROC_FS */
 892 
 893 
 894 /*
 895  *      This checks bitmasks for the ioctl calls for devices.
 896  */
 897  
 898 static inline int bad_mask(unsigned long mask, unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 899 {
 900         if (addr & (mask = ~mask))
 901                 return 1;
 902         mask = ntohl(mask);
 903         if (mask & (mask+1))
 904                 return 1;
 905         return 0;
 906 }
 907 
 908 /*
 909  *      Perform the SIOCxIFxxx calls. 
 910  *
 911  *      The socket layer has seen an ioctl the address family thinks is
 912  *      for the device. At this point we get invoked to make a decision
 913  */
 914  
 915 static int dev_ifsioc(void *arg, unsigned int getset)
     /* [previous][next][first][last][top][bottom][index][help] */
 916 {
 917         struct ifreq ifr;
 918         struct device *dev;
 919         int ret;
 920 
 921         /*
 922          *      Fetch the caller's info block into kernel space
 923          */
 924 
 925         int err=verify_area(VERIFY_WRITE, arg, sizeof(struct ifreq));
 926         if(err)
 927                 return err;
 928         
 929         memcpy_fromfs(&ifr, arg, sizeof(struct ifreq));
 930 
 931         /*
 932          *      See which interface the caller is talking about. 
 933          */
 934          
 935         /*
 936          *
 937          *      net_alias_dev_get(): dev_get() with added alias naming magic.
 938          *      only allow alias creation/deletion if (getset==SIOCSIFADDR)
 939          *
 940          */
 941          
 942 #ifdef CONFIG_KERNELD
 943         dev_load(ifr.ifr_name);
 944 #endif  
 945 
 946 #ifdef CONFIG_NET_ALIAS
 947         if ((dev = net_alias_dev_get(ifr.ifr_name, getset == SIOCSIFADDR, &err, NULL, NULL)) == NULL)
 948                 return(err);
 949 #else
 950         if ((dev = dev_get(ifr.ifr_name)) == NULL)      
 951                 return(-ENODEV);
 952 #endif
 953         switch(getset) 
 954         {
 955                 case SIOCGIFFLAGS:      /* Get interface flags */
 956                         ifr.ifr_flags = dev->flags;
 957                         goto rarok;
 958 
 959                 case SIOCSIFFLAGS:      /* Set interface flags */
 960                         {
 961                                 int old_flags = dev->flags;
 962                                 
 963                                 /*
 964                                  *      We are not allowed to potentially close/unload
 965                                  *      a device until we get this lock.
 966                                  */
 967                                 
 968                                 dev_lock_wait();
 969                                 
 970                                 /*
 971                                  *      Set the flags on our device.
 972                                  */
 973                                  
 974                                 dev->flags = (ifr.ifr_flags & (
 975                                         IFF_BROADCAST | IFF_DEBUG | IFF_LOOPBACK |
 976                                         IFF_POINTOPOINT | IFF_NOTRAILERS | IFF_RUNNING |
 977                                         IFF_NOARP | IFF_PROMISC | IFF_ALLMULTI | IFF_SLAVE | IFF_MASTER
 978                                         | IFF_MULTICAST)) | (dev->flags & IFF_UP);
 979                                 /*
 980                                  *      Load in the correct multicast list now the flags have changed.
 981                                  */                             
 982 
 983                                 dev_mc_upload(dev);
 984 
 985                                 /*
 986                                  *      Have we downed the interface. We handle IFF_UP ourselves
 987                                  *      according to user attempts to set it, rather than blindly
 988                                  *      setting it.
 989                                  */
 990                                  
 991                                 if ((old_flags^ifr.ifr_flags)&IFF_UP)   /* Bit is different  ? */
 992                                 {
 993                                         if(old_flags&IFF_UP)            /* Gone down */
 994                                                 ret=dev_close(dev);             
 995                                         else                            /* Come up */
 996                                         {
 997                                                 ret=dev_open(dev);
 998                                                 if(ret<0)
 999                                                         dev->flags&=~IFF_UP;    /* Open failed */
1000                                         }       
1001                                 }
1002                                 else
1003                                         ret=0;
1004                                 /*
1005                                  *      Load in the correct multicast list now the flags have changed.
1006                                  */                             
1007 
1008                                 dev_mc_upload(dev);
1009                         }
1010                         break;
1011                 
1012                 case SIOCGIFADDR:       /* Get interface address (and family) */
1013                         if(ifr.ifr_addr.sa_family==AF_UNSPEC)
1014                         {
1015                                 memcpy(ifr.ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1016                                 ifr.ifr_hwaddr.sa_family=dev->type;                     
1017                                 goto rarok;
1018                         }
1019                         else
1020                         {
1021                                 (*(struct sockaddr_in *)
1022                                           &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
1023                                 (*(struct sockaddr_in *)
1024                                           &ifr.ifr_addr).sin_family = dev->family;
1025                                 (*(struct sockaddr_in *)
1026                                           &ifr.ifr_addr).sin_port = 0;
1027                         }
1028                         goto rarok;
1029         
1030                 case SIOCSIFADDR:       /* Set interface address (and family) */
1031                 
1032                         /*
1033                          *      BSDism. SIOCSIFADDR family=AF_UNSPEC sets the
1034                          *      physical address. We can cope with this now.
1035                          */
1036                         
1037                         if(ifr.ifr_addr.sa_family==AF_UNSPEC)
1038                         {
1039                                 if(dev->set_mac_address==NULL)
1040                                         return -EOPNOTSUPP;
1041                                 ret=dev->set_mac_address(dev,&ifr.ifr_addr);
1042                         }
1043                         else
1044                         {
1045 
1046                                 /*
1047                                  *      if dev is an alias, must rehash to update
1048                                  *      address change
1049                                  */
1050 
1051 #ifdef CONFIG_NET_ALIAS
1052                                 if (net_alias_is(dev))
1053                                 net_alias_dev_rehash(dev ,&ifr.ifr_addr);
1054 #endif
1055                                 dev->pa_addr = (*(struct sockaddr_in *)
1056                                          &ifr.ifr_addr).sin_addr.s_addr;
1057                                 dev->family = ifr.ifr_addr.sa_family;
1058                         
1059 #ifdef CONFIG_INET      
1060                                 /* This is naughty. When net-032e comes out It wants moving into the net032
1061                                    code not the kernel. Till then it can sit here (SIGH) */             
1062                                 dev->pa_mask = ip_get_mask(dev->pa_addr);
1063 #endif                  
1064                                 dev->pa_brdaddr = dev->pa_addr | ~dev->pa_mask;
1065                                 ret = 0;
1066                         }
1067                         break;
1068                         
1069                 case SIOCGIFBRDADDR:    /* Get the broadcast address */
1070                         (*(struct sockaddr_in *)
1071                                 &ifr.ifr_broadaddr).sin_addr.s_addr = dev->pa_brdaddr;
1072                         (*(struct sockaddr_in *)
1073                                 &ifr.ifr_broadaddr).sin_family = dev->family;
1074                         (*(struct sockaddr_in *)
1075                                 &ifr.ifr_broadaddr).sin_port = 0;
1076                         goto rarok;
1077 
1078                 case SIOCSIFBRDADDR:    /* Set the broadcast address */
1079                         dev->pa_brdaddr = (*(struct sockaddr_in *)
1080                                 &ifr.ifr_broadaddr).sin_addr.s_addr;
1081                         ret = 0;
1082                         break;
1083                         
1084                 case SIOCGIFDSTADDR:    /* Get the destination address (for point-to-point links) */
1085                         (*(struct sockaddr_in *)
1086                                 &ifr.ifr_dstaddr).sin_addr.s_addr = dev->pa_dstaddr;
1087                         (*(struct sockaddr_in *)
1088                                 &ifr.ifr_dstaddr).sin_family = dev->family;
1089                         (*(struct sockaddr_in *)
1090                                 &ifr.ifr_dstaddr).sin_port = 0;
1091                         goto rarok;
1092         
1093                 case SIOCSIFDSTADDR:    /* Set the destination address (for point-to-point links) */
1094                         dev->pa_dstaddr = (*(struct sockaddr_in *)
1095                                 &ifr.ifr_dstaddr).sin_addr.s_addr;
1096                         ret = 0;
1097                         break;
1098                         
1099                 case SIOCGIFNETMASK:    /* Get the netmask for the interface */
1100                         (*(struct sockaddr_in *)
1101                                 &ifr.ifr_netmask).sin_addr.s_addr = dev->pa_mask;
1102                         (*(struct sockaddr_in *)
1103                                 &ifr.ifr_netmask).sin_family = dev->family;
1104                         (*(struct sockaddr_in *)
1105                                 &ifr.ifr_netmask).sin_port = 0;
1106                         goto rarok;
1107 
1108                 case SIOCSIFNETMASK:    /* Set the netmask for the interface */
1109                         {
1110                                 unsigned long mask = (*(struct sockaddr_in *)
1111                                         &ifr.ifr_netmask).sin_addr.s_addr;
1112                                 ret = -EINVAL;
1113                                 /*
1114                                  *      The mask we set must be legal.
1115                                  */
1116                                 if (bad_mask(mask,0))
1117                                         break;
1118                                 dev->pa_mask = mask;
1119                                 ret = 0;
1120                         }
1121                         break;
1122                         
1123                 case SIOCGIFMETRIC:     /* Get the metric on the interface (currently unused) */
1124                         
1125                         ifr.ifr_metric = dev->metric;
1126                         goto  rarok;
1127                         
1128                 case SIOCSIFMETRIC:     /* Set the metric on the interface (currently unused) */
1129                         dev->metric = ifr.ifr_metric;
1130                         ret=0;
1131                         break;
1132         
1133                 case SIOCGIFMTU:        /* Get the MTU of a device */
1134                         ifr.ifr_mtu = dev->mtu;
1135                         goto rarok;
1136         
1137                 case SIOCSIFMTU:        /* Set the MTU of a device */
1138                 
1139                         /*
1140                          *      MTU must be positive.
1141                          */
1142                          
1143                         if(ifr.ifr_mtu<68)
1144                                 return -EINVAL;
1145                         dev->mtu = ifr.ifr_mtu;
1146                         ret = 0;
1147                         break;
1148         
1149                 case SIOCGIFMEM:        /* Get the per device memory space. We can add this but currently
1150                                            do not support it */
1151                         ret = -EINVAL;
1152                         break;
1153                 
1154                 case SIOCSIFMEM:        /* Set the per device memory buffer space. Not applicable in our case */
1155                         ret = -EINVAL;
1156                         break;
1157 
1158                 case SIOCGIFHWADDR:
1159                         memcpy(ifr.ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1160                         ifr.ifr_hwaddr.sa_family=dev->type;                     
1161                         goto rarok;
1162                                 
1163                 case SIOCSIFHWADDR:
1164                         if(dev->set_mac_address==NULL)
1165                                 return -EOPNOTSUPP;
1166                         if(ifr.ifr_hwaddr.sa_family!=dev->type)
1167                                 return -EINVAL;
1168                         ret=dev->set_mac_address(dev,&ifr.ifr_hwaddr);
1169                         break;
1170                         
1171                 case SIOCGIFMAP:
1172                         ifr.ifr_map.mem_start=dev->mem_start;
1173                         ifr.ifr_map.mem_end=dev->mem_end;
1174                         ifr.ifr_map.base_addr=dev->base_addr;
1175                         ifr.ifr_map.irq=dev->irq;
1176                         ifr.ifr_map.dma=dev->dma;
1177                         ifr.ifr_map.port=dev->if_port;
1178                         goto rarok;
1179                         
1180                 case SIOCSIFMAP:
1181                         if(dev->set_config==NULL)
1182                                 return -EOPNOTSUPP;
1183                         return dev->set_config(dev,&ifr.ifr_map);
1184                         
1185                 case SIOCADDMULTI:
1186                         if(dev->set_multicast_list==NULL)
1187                                 return -EINVAL;
1188                         if(ifr.ifr_hwaddr.sa_family!=AF_UNSPEC)
1189                                 return -EINVAL;
1190                         dev_mc_add(dev,ifr.ifr_hwaddr.sa_data, dev->addr_len, 1);
1191                         return 0;
1192 
1193                 case SIOCDELMULTI:
1194                         if(dev->set_multicast_list==NULL)
1195                                 return -EINVAL;
1196                         if(ifr.ifr_hwaddr.sa_family!=AF_UNSPEC)
1197                                 return -EINVAL;
1198                         dev_mc_delete(dev,ifr.ifr_hwaddr.sa_data,dev->addr_len, 1);
1199                         return 0;
1200                 /*
1201                  *      Unknown or private ioctl
1202                  */
1203 
1204                 default:
1205                         if((getset >= SIOCDEVPRIVATE) &&
1206                            (getset <= (SIOCDEVPRIVATE + 15))) {
1207                                 if(dev->do_ioctl==NULL)
1208                                         return -EOPNOTSUPP;
1209                                 ret=dev->do_ioctl(dev, &ifr, getset);
1210                                 memcpy_tofs(arg,&ifr,sizeof(struct ifreq));
1211                                 break;
1212                         }
1213                         
1214                         ret = -EINVAL;
1215         }
1216         return(ret);
1217 /*
1218  *      The load of calls that return an ifreq and ok (saves memory).
1219  */
1220 rarok:
1221         memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
1222         return 0;
1223 }
1224 
1225 
1226 /*
1227  *      This function handles all "interface"-type I/O control requests. The actual
1228  *      'doing' part of this is dev_ifsioc above.
1229  */
1230 
1231 int dev_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1232 {
1233         switch(cmd) 
1234         {
1235                 case SIOCGIFCONF:
1236                         (void) dev_ifconf((char *) arg);
1237                         return 0;
1238 
1239                 /*
1240                  *      Ioctl calls that can be done by all.
1241                  */
1242                  
1243                 case SIOCGIFFLAGS:
1244                 case SIOCGIFADDR:
1245                 case SIOCGIFDSTADDR:
1246                 case SIOCGIFBRDADDR:
1247                 case SIOCGIFNETMASK:
1248                 case SIOCGIFMETRIC:
1249                 case SIOCGIFMTU:
1250                 case SIOCGIFMEM:
1251                 case SIOCGIFHWADDR:
1252                 case SIOCSIFHWADDR:
1253                 case SIOCGIFSLAVE:
1254                 case SIOCGIFMAP:
1255                         return dev_ifsioc(arg, cmd);
1256 
1257                 /*
1258                  *      Ioctl calls requiring the power of a superuser
1259                  */
1260                  
1261                 case SIOCSIFFLAGS:
1262                 case SIOCSIFADDR:
1263                 case SIOCSIFDSTADDR:
1264                 case SIOCSIFBRDADDR:
1265                 case SIOCSIFNETMASK:
1266                 case SIOCSIFMETRIC:
1267                 case SIOCSIFMTU:
1268                 case SIOCSIFMEM:
1269                 case SIOCSIFMAP:
1270                 case SIOCSIFSLAVE:
1271                 case SIOCADDMULTI:
1272                 case SIOCDELMULTI:
1273                         if (!suser())
1274                                 return -EPERM;
1275                         return dev_ifsioc(arg, cmd);
1276         
1277                 case SIOCSIFLINK:
1278                         return -EINVAL;
1279 
1280                 /*
1281                  *      Unknown or private ioctl.
1282                  */     
1283                  
1284                 default:
1285                         if((cmd >= SIOCDEVPRIVATE) &&
1286                            (cmd <= (SIOCDEVPRIVATE + 15))) {
1287                                 return dev_ifsioc(arg, cmd);
1288                         }
1289                         return -EINVAL;
1290         }
1291 }
1292 
1293 
1294 /*
1295  *      Initialize the DEV module. At boot time this walks the device list and
1296  *      unhooks any devices that fail to initialise (normally hardware not 
1297  *      present) and leaves us with a valid list of present and active devices.
1298  *
1299  */
1300 extern int lance_init(void);
1301 extern int ni65_init(void);
1302 extern int pi_init(void);
1303 extern int dec21040_init(void);
1304 
1305 int net_dev_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1306 {
1307         struct device *dev, **dp;
1308 
1309         /*
1310          *      Initialise the packet receive queue.
1311          */
1312          
1313         skb_queue_head_init(&backlog);
1314         
1315         /*
1316          * This is VeryUgly(tm).
1317          *
1318          * Some devices want to be initialized eary..
1319          */
1320 #if defined(CONFIG_LANCE)
1321         lance_init();
1322 #endif
1323 #if defined(CONFIG_NI65)
1324         ni65_init();
1325 #endif
1326 #if defined(CONFIG_PI)
1327         pi_init();
1328 #endif  
1329 #if defined(CONFIG_PT)
1330         pt_init();
1331 #endif
1332 #if defined(CONFIG_DEC_ELCP)
1333         dec21040_init();
1334 #endif  
1335         /*
1336          *      SLHC if present needs attaching so other people see it
1337          *      even if not opened.
1338          */
1339 #if (defined(CONFIG_SLIP_COMPRESSED) || defined(CONFIG_PPP)) && defined(CONFIG_SLHC_BUILTIN)
1340         slhc_install();
1341 #endif  
1342 
1343         /*
1344          *      Add the devices.
1345          *      If the call to dev->init fails, the dev is removed
1346          *      from the chain disconnecting the device until the
1347          *      next reboot.
1348          */
1349 
1350         dp = &dev_base;
1351         while ((dev = *dp) != NULL)
1352         {
1353                 int i;
1354                 for (i = 0; i < DEV_NUMBUFFS; i++)  {
1355                         skb_queue_head_init(dev->buffs + i);
1356                 }
1357 
1358                 if (dev->init && dev->init(dev)) 
1359                 {
1360                         /*
1361                          *      It failed to come up. Unhook it.
1362                          */
1363                         *dp = dev->next;
1364                 } 
1365                 else
1366                 {
1367                         dp = &dev->next;
1368                 }
1369         }
1370 
1371 #ifdef CONFIG_PROC_FS
1372         proc_net_register(&(struct proc_dir_entry) {
1373                 PROC_NET_DEV, 3, "dev",
1374                 S_IFREG | S_IRUGO, 1, 0, 0,
1375                 0, &proc_net_inode_operations,
1376                 dev_get_info
1377         });
1378 #endif
1379 
1380         /*      
1381          *      Initialise net_alias engine 
1382          *
1383          *              - register net_alias device notifier
1384          *              - register proc entries:        /proc/net/alias_types
1385          *                                                                      /proc/net/aliases
1386          */
1387 
1388 #ifdef CONFIG_NET_ALIAS
1389         net_alias_init();
1390 #endif
1391 
1392         init_bh(NET_BH, net_bh);
1393         return 0;
1394 }

/* [previous][next][first][last][top][bottom][index][help] */