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

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