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 packet_type *ptype;
 333         int where = 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                 where = 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 
 386         save_flags(flags);
 387         cli();  
 388         if (!where)             /* Always keep order. It helps other hosts
 389                                            far more than it costs us */
 390         {
 391                 /*
 392                  *      Check we are not overruning the device queue length.
 393                  */
 394                  
 395                 if(skb_queue_len(dev->buffs + pri) > dev->tx_queue_len)
 396                 {
 397                         dev_kfree_skb(skb, FREE_WRITE);
 398                         return;
 399                 }
 400                 skb_queue_tail(dev->buffs + pri,skb);
 401                 skb_device_unlock(skb);         /* Buffer is on the device queue and can be freed safely */
 402                 skb = skb_dequeue(dev->buffs + pri);
 403                 skb_device_lock(skb);           /* New buffer needs locking down */
 404         }
 405         restore_flags(flags);
 406 
 407         /* copy outgoing packets to any sniffer packet handlers */
 408         if(!where && dev_nit)
 409         {
 410                 skb->stamp=xtime;
 411                 for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next) 
 412                 {
 413                         /* Never send packets back to the socket
 414                          * they originated from - MvS (miquels@drinkel.ow.org)
 415                          */
 416                         if ((ptype->dev == dev || !ptype->dev) &&
 417                            ((struct sock *)ptype->data != skb->sk))
 418                         {
 419                                 struct sk_buff *skb2;
 420                                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
 421                                         break;
 422                                 skb2->h.raw = skb2->data + dev->hard_header_len;
 423                                 skb2->mac.raw = skb2->data;
 424                                 ptype->func(skb2, skb->dev, ptype);
 425                         }
 426                 }
 427         }
 428         start_bh_atomic();
 429         if (dev->hard_start_xmit(skb, dev) == 0) {
 430                 /*
 431                  *      Packet is now solely the responsibility of the driver
 432                  */
 433                 end_bh_atomic();
 434                 return;
 435         }
 436         end_bh_atomic();
 437 
 438         /*
 439          *      Transmission failed, put skb back into a list. Once on the list it's safe and
 440          *      no longer device locked (it can be freed safely from the device queue)
 441          */
 442         cli();
 443         skb_device_unlock(skb);
 444         skb_queue_head(dev->buffs + pri,skb);
 445         restore_flags(flags);
 446 }
 447 
 448 /*
 449  *      Receive a packet from a device driver and queue it for the upper
 450  *      (protocol) levels.  It always succeeds. This is the recommended 
 451  *      interface to use.
 452  */
 453 
 454 void netif_rx(struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 455 {
 456         static int dropping = 0;
 457 
 458         /*
 459          *      Any received buffers are un-owned and should be discarded
 460          *      when freed. These will be updated later as the frames get
 461          *      owners.
 462          */
 463 
 464         skb->sk = NULL;
 465         skb->free = 1;
 466         if(skb->stamp.tv_sec==0)
 467                 skb->stamp = xtime;
 468 
 469         /*
 470          *      Check that we aren't overdoing things.
 471          */
 472 
 473         if (!backlog_size)
 474                 dropping = 0;
 475         else if (backlog_size > 300)
 476                 dropping = 1;
 477 
 478         if (dropping) 
 479         {
 480                 kfree_skb(skb, FREE_READ);
 481                 return;
 482         }
 483 
 484         /*
 485          *      Add it to the "backlog" queue. 
 486          */
 487 #if CONFIG_SKB_CHECK
 488         IS_SKB(skb);
 489 #endif  
 490         skb_queue_tail(&backlog,skb);
 491         backlog_size++;
 492   
 493         /*
 494          *      If any packet arrived, mark it for processing after the
 495          *      hardware interrupt returns.
 496          */
 497 
 498 #ifdef CONFIG_NET_RUNONIRQ      /* Dont enable yet, needs some driver mods */
 499         net_bh();
 500 #else
 501         mark_bh(NET_BH);
 502 #endif
 503         return;
 504 }
 505 
 506 /*
 507  *      This routine causes all interfaces to try to send some data. 
 508  */
 509  
 510 void dev_transmit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 511 {
 512         struct device *dev;
 513 
 514         for (dev = dev_base; dev != NULL; dev = dev->next)
 515         {
 516                 if (dev->flags != 0 && !dev->tbusy) {
 517                         /*
 518                          *      Kick the device
 519                          */
 520                         dev_tint(dev);
 521                 }
 522         }
 523 }
 524 
 525 
 526 /**********************************************************************************
 527 
 528                         Receive Queue Processor
 529                         
 530 ***********************************************************************************/
 531 
 532 /*
 533  *      This is a single non-reentrant routine which takes the received packet
 534  *      queue and throws it at the networking layers in the hope that something
 535  *      useful will emerge.
 536  */
 537  
 538 volatile unsigned long in_bh = 0;       /* Non-reentrant remember */
 539 
 540 int in_net_bh() /* Used by timer.c */
     /* [previous][next][first][last][top][bottom][index][help] */
 541 {
 542         return(in_bh==0?0:1);
 543 }
 544 
 545 /*
 546  *      When we are called the queue is ready to grab, the interrupts are
 547  *      on and hardware can interrupt and queue to the receive queue a we
 548  *      run with no problems.
 549  *      This is run as a bottom half after an interrupt handler that does
 550  *      mark_bh(NET_BH);
 551  */
 552  
 553 void net_bh(void *tmp)
     /* [previous][next][first][last][top][bottom][index][help] */
 554 {
 555         struct sk_buff *skb;
 556         struct packet_type *ptype;
 557         struct packet_type *pt_prev;
 558         unsigned short type;
 559 
 560         /*
 561          *      Atomically check and mark our BUSY state. 
 562          */
 563 
 564         if (set_bit(1, (void*)&in_bh))
 565                 return;
 566 
 567         /*
 568          *      Can we send anything now? We want to clear the
 569          *      decks for any more sends that get done as we
 570          *      process the input. This also minimises the
 571          *      latency on a transmit interrupt bh.
 572          */
 573 
 574         dev_transmit();
 575   
 576         /*
 577          *      Any data left to process. This may occur because a
 578          *      mark_bh() is done after we empty the queue including
 579          *      that from the device which does a mark_bh() just after
 580          */
 581 
 582         cli();
 583         
 584         /*
 585          *      While the queue is not empty
 586          */
 587          
 588         while((skb=skb_dequeue(&backlog))!=NULL)
 589         {
 590                 /*
 591                  *      We have a packet. Therefore the queue has shrunk
 592                  */
 593                 backlog_size--;
 594 
 595                 sti();
 596                 
 597                /*
 598                 *       Bump the pointer to the next structure.
 599                 *
 600                 *       On entry to the protocol layer. skb->data and
 601                 *       skb->h.raw point to the MAC and encapsulated data
 602                 */
 603 
 604                 skb->h.raw = skb->data;
 605 
 606                /*
 607                 *       Fetch the packet protocol ID. 
 608                 */
 609                 
 610                 type = skb->protocol;
 611 
 612                 /*
 613                  *      We got a packet ID.  Now loop over the "known protocols"
 614                  *      list. There are two lists. The ptype_all list of taps (normally empty)
 615                  *      and the main protocol list which is hashed perfectly for normal protocols.
 616                  */
 617                 pt_prev = NULL;
 618                 for (ptype = ptype_all; ptype!=NULL; ptype=ptype->next)
 619                 {
 620                         if(pt_prev)
 621                         {
 622                                 struct sk_buff *skb2=skb_clone(skb, GFP_ATOMIC);
 623                                 if(skb2)
 624                                         pt_prev->func(skb2,skb->dev, pt_prev);
 625                         }
 626                         pt_prev=ptype;
 627                 }
 628                 
 629                 for (ptype = ptype_base[ntohs(type)&15]; ptype != NULL; ptype = ptype->next) 
 630                 {
 631                         if (ptype->type == type && (!ptype->dev || ptype->dev==skb->dev))
 632                         {
 633                                 /*
 634                                  *      We already have a match queued. Deliver
 635                                  *      to it and then remember the new match
 636                                  */
 637                                 if(pt_prev)
 638                                 {
 639                                         struct sk_buff *skb2;
 640 
 641                                         skb2=skb_clone(skb, GFP_ATOMIC);
 642 
 643                                         /*
 644                                          *      Kick the protocol handler. This should be fast
 645                                          *      and efficient code.
 646                                          */
 647 
 648                                         if(skb2)
 649                                                 pt_prev->func(skb2, skb->dev, pt_prev);
 650                                 }
 651                                 /* Remember the current last to do */
 652                                 pt_prev=ptype;
 653                         }
 654                 } /* End of protocol list loop */
 655                 
 656                 /*
 657                  *      Is there a last item to send to ?
 658                  */
 659 
 660                 if(pt_prev)
 661                         pt_prev->func(skb, skb->dev, pt_prev);
 662                 /*
 663                  *      Has an unknown packet has been received ?
 664                  */
 665          
 666                 else
 667                         kfree_skb(skb, FREE_WRITE);
 668 
 669                 /*
 670                  *      Again, see if we can transmit anything now. 
 671                  *      [Ought to take this out judging by tests it slows
 672                  *       us down not speeds us up]
 673                  */
 674 #ifdef CONFIG_XMIT_EVERY
 675                 dev_transmit();
 676 #endif          
 677                 cli();
 678         }       /* End of queue loop */
 679         
 680         /*
 681          *      We have emptied the queue
 682          */
 683          
 684         in_bh = 0;
 685         sti();
 686         
 687         /*
 688          *      One last output flush.
 689          */
 690          
 691         dev_transmit();
 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         struct sk_buff *skb;
 704         unsigned long flags;
 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         save_flags(flags);      
 714         /*
 715          *      Work the queues in priority order
 716          */
 717          
 718         for(i = 0;i < DEV_NUMBUFFS; i++) 
 719         {
 720                 /*
 721                  *      Pull packets from the queue
 722                  */
 723                  
 724 
 725                 cli();
 726                 while((skb=skb_dequeue(&dev->buffs[i]))!=NULL)
 727                 {
 728                         /*
 729                          *      Stop anyone freeing the buffer while we retransmit it
 730                          */
 731                         skb_device_lock(skb);
 732                         restore_flags(flags);
 733                         /*
 734                          *      Feed them to the output stage and if it fails
 735                          *      indicate they re-queue at the front.
 736                          */
 737                         dev_queue_xmit(skb,dev,-i - 1);
 738                         /*
 739                          *      If we can take no more then stop here.
 740                          */
 741                         if (dev->tbusy)
 742                                 return;
 743                         cli();
 744                 }
 745         }
 746         restore_flags(flags);
 747 }
 748 
 749 
 750 /*
 751  *      Perform a SIOCGIFCONF call. This structure will change
 752  *      size shortly, and there is nothing I can do about it.
 753  *      Thus we will need a 'compatibility mode'.
 754  */
 755 
 756 static int dev_ifconf(char *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 757 {
 758         struct ifconf ifc;
 759         struct ifreq ifr;
 760         struct device *dev;
 761         char *pos;
 762         int len;
 763         int err;
 764 
 765         /*
 766          *      Fetch the caller's info block. 
 767          */
 768          
 769         err=verify_area(VERIFY_WRITE, arg, sizeof(struct ifconf));
 770         if(err)
 771                 return err;
 772         memcpy_fromfs(&ifc, arg, sizeof(struct ifconf));
 773         len = ifc.ifc_len;
 774         pos = ifc.ifc_buf;
 775 
 776         /*
 777          *      We now walk the device list filling each active device
 778          *      into the array.
 779          */
 780          
 781         err=verify_area(VERIFY_WRITE,pos,len);
 782         if(err)
 783                 return err;
 784         
 785         /*
 786          *      Loop over the interfaces, and write an info block for each. 
 787          */
 788 
 789         for (dev = dev_base; dev != NULL; dev = dev->next) 
 790         {
 791                 if(!(dev->flags & IFF_UP))      /* Downed devices don't count */
 792                         continue;
 793                 memset(&ifr, 0, sizeof(struct ifreq));
 794                 strcpy(ifr.ifr_name, dev->name);
 795                 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = dev->family;
 796                 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
 797 
 798                 /*
 799                  *      Have we run out of space here ?
 800                  */
 801         
 802                 if (len < sizeof(struct ifreq)) 
 803                         break;
 804 
 805                 /*
 806                  *      Write this block to the caller's space. 
 807                  */
 808                  
 809                 memcpy_tofs(pos, &ifr, sizeof(struct ifreq));
 810                 pos += sizeof(struct ifreq);
 811                 len -= sizeof(struct ifreq);            
 812         }
 813 
 814         /*
 815          *      All done.  Write the updated control block back to the caller. 
 816          */
 817          
 818         ifc.ifc_len = (pos - ifc.ifc_buf);
 819         ifc.ifc_req = (struct ifreq *) ifc.ifc_buf;
 820         memcpy_tofs(arg, &ifc, sizeof(struct ifconf));
 821         
 822         /*
 823          *      Report how much was filled in
 824          */
 825          
 826         return(pos - arg);
 827 }
 828 
 829 
 830 /*
 831  *      This is invoked by the /proc filesystem handler to display a device
 832  *      in detail.
 833  */
 834 
 835 #ifdef CONFIG_PROC_FS
 836 static int sprintf_stats(char *buffer, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 837 {
 838         struct enet_statistics *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
 839         int size;
 840         
 841         if (stats)
 842                 size = sprintf(buffer, "%6s:%7d %4d %4d %4d %4d %8d %4d %4d %4d %5d %4d\n",
 843                    dev->name,
 844                    stats->rx_packets, stats->rx_errors,
 845                    stats->rx_dropped + stats->rx_missed_errors,
 846                    stats->rx_fifo_errors,
 847                    stats->rx_length_errors + stats->rx_over_errors
 848                    + stats->rx_crc_errors + stats->rx_frame_errors,
 849                    stats->tx_packets, stats->tx_errors, stats->tx_dropped,
 850                    stats->tx_fifo_errors, stats->collisions,
 851                    stats->tx_carrier_errors + stats->tx_aborted_errors
 852                    + stats->tx_window_errors + stats->tx_heartbeat_errors);
 853         else
 854                 size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
 855 
 856         return size;
 857 }
 858 
 859 /*
 860  *      Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
 861  *      to create /proc/net/dev
 862  */
 863  
 864 int dev_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 865 {
 866         int len=0;
 867         off_t begin=0;
 868         off_t pos=0;
 869         int size;
 870         
 871         struct device *dev;
 872 
 873 
 874         size = sprintf(buffer, "Inter-|   Receive                  |  Transmit\n"
 875                             " face |packets errs drop fifo frame|packets errs drop fifo colls carrier\n");
 876         
 877         pos+=size;
 878         len+=size;
 879         
 880 
 881         for (dev = dev_base; dev != NULL; dev = dev->next) 
 882         {
 883                 size = sprintf_stats(buffer+len, dev);
 884                 len+=size;
 885                 pos=begin+len;
 886                                 
 887                 if(pos<offset)
 888                 {
 889                         len=0;
 890                         begin=pos;
 891                 }
 892                 if(pos>offset+length)
 893                         break;
 894         }
 895         
 896         *start=buffer+(offset-begin);   /* Start of wanted data */
 897         len-=(offset-begin);            /* Start slop */
 898         if(len>length)
 899                 len=length;             /* Ending slop */
 900         return len;
 901 }
 902 #endif  /* CONFIG_PROC_FS */
 903 
 904 
 905 /*
 906  *      This checks bitmasks for the ioctl calls for devices.
 907  */
 908  
 909 static inline int bad_mask(unsigned long mask, unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 910 {
 911         if (addr & (mask = ~mask))
 912                 return 1;
 913         mask = ntohl(mask);
 914         if (mask & (mask+1))
 915                 return 1;
 916         return 0;
 917 }
 918 
 919 /*
 920  *      Perform the SIOCxIFxxx calls. 
 921  *
 922  *      The socket layer has seen an ioctl the address family thinks is
 923  *      for the device. At this point we get invoked to make a decision
 924  */
 925  
 926 static int dev_ifsioc(void *arg, unsigned int getset)
     /* [previous][next][first][last][top][bottom][index][help] */
 927 {
 928         struct ifreq ifr;
 929         struct device *dev;
 930         int ret;
 931 
 932         /*
 933          *      Fetch the caller's info block into kernel space
 934          */
 935 
 936         int err=verify_area(VERIFY_WRITE, arg, sizeof(struct ifreq));
 937         if(err)
 938                 return err;
 939         
 940         memcpy_fromfs(&ifr, arg, sizeof(struct ifreq));
 941 
 942         /*
 943          *      See which interface the caller is talking about. 
 944          */
 945          
 946         /*
 947          *
 948          *      net_alias_dev_get(): dev_get() with added alias naming magic.
 949          *      only allow alias creation/deletion if (getset==SIOCSIFADDR)
 950          *
 951          */
 952          
 953 #ifdef CONFIG_KERNELD
 954         dev_load(ifr.ifr_name);
 955 #endif  
 956 
 957 #ifdef CONFIG_NET_ALIAS
 958         if ((dev = net_alias_dev_get(ifr.ifr_name, getset == SIOCSIFADDR, &err, NULL, NULL)) == NULL)
 959                 return(err);
 960 #else
 961         if ((dev = dev_get(ifr.ifr_name)) == NULL)      
 962                 return(-ENODEV);
 963 #endif
 964         switch(getset) 
 965         {
 966                 case SIOCGIFFLAGS:      /* Get interface flags */
 967                         ifr.ifr_flags = dev->flags;
 968                         goto rarok;
 969 
 970                 case SIOCSIFFLAGS:      /* Set interface flags */
 971                         {
 972                                 int old_flags = dev->flags;
 973                                 
 974                                 /*
 975                                  *      We are not allowed to potentially close/unload
 976                                  *      a device until we get this lock.
 977                                  */
 978                                 
 979                                 dev_lock_wait();
 980                                 
 981                                 /*
 982                                  *      Set the flags on our device.
 983                                  */
 984                                  
 985                                 dev->flags = (ifr.ifr_flags & (
 986                                         IFF_BROADCAST | IFF_DEBUG | IFF_LOOPBACK |
 987                                         IFF_POINTOPOINT | IFF_NOTRAILERS | IFF_RUNNING |
 988                                         IFF_NOARP | IFF_PROMISC | IFF_ALLMULTI | IFF_SLAVE | IFF_MASTER
 989                                         | IFF_MULTICAST)) | (dev->flags & IFF_UP);
 990                                 /*
 991                                  *      Load in the correct multicast list now the flags have changed.
 992                                  */                             
 993 
 994                                 dev_mc_upload(dev);
 995 
 996                                 /*
 997                                  *      Have we downed the interface. We handle IFF_UP ourselves
 998                                  *      according to user attempts to set it, rather than blindly
 999                                  *      setting it.
1000                                  */
1001                                  
1002                                 if ((old_flags^ifr.ifr_flags)&IFF_UP)   /* Bit is different  ? */
1003                                 {
1004                                         if(old_flags&IFF_UP)            /* Gone down */
1005                                                 ret=dev_close(dev);             
1006                                         else                            /* Come up */
1007                                         {
1008                                                 ret=dev_open(dev);
1009                                                 if(ret<0)
1010                                                         dev->flags&=~IFF_UP;    /* Open failed */
1011                                         }       
1012                                 }
1013                                 else
1014                                         ret=0;
1015                                 /*
1016                                  *      Load in the correct multicast list now the flags have changed.
1017                                  */                             
1018 
1019                                 dev_mc_upload(dev);
1020                         }
1021                         break;
1022                 
1023                 case SIOCGIFADDR:       /* Get interface address (and family) */
1024                         if(ifr.ifr_addr.sa_family==AF_UNSPEC)
1025                         {
1026                                 memcpy(ifr.ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1027                                 ifr.ifr_hwaddr.sa_family=dev->type;                     
1028                                 goto rarok;
1029                         }
1030                         else
1031                         {
1032                                 (*(struct sockaddr_in *)
1033                                           &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
1034                                 (*(struct sockaddr_in *)
1035                                           &ifr.ifr_addr).sin_family = dev->family;
1036                                 (*(struct sockaddr_in *)
1037                                           &ifr.ifr_addr).sin_port = 0;
1038                         }
1039                         goto rarok;
1040         
1041                 case SIOCSIFADDR:       /* Set interface address (and family) */
1042                 
1043                         /*
1044                          *      BSDism. SIOCSIFADDR family=AF_UNSPEC sets the
1045                          *      physical address. We can cope with this now.
1046                          */
1047                         
1048                         if(ifr.ifr_addr.sa_family==AF_UNSPEC)
1049                         {
1050                                 if(dev->set_mac_address==NULL)
1051                                         return -EOPNOTSUPP;
1052                                 ret=dev->set_mac_address(dev,&ifr.ifr_addr);
1053                         }
1054                         else
1055                         {
1056 
1057                                 /*
1058                                  *      if dev is an alias, must rehash to update
1059                                  *      address change
1060                                  */
1061 
1062 #ifdef CONFIG_NET_ALIAS
1063                                 if (net_alias_is(dev))
1064                                 net_alias_dev_rehash(dev ,&ifr.ifr_addr);
1065 #endif
1066                                 dev->pa_addr = (*(struct sockaddr_in *)
1067                                          &ifr.ifr_addr).sin_addr.s_addr;
1068                                 dev->family = ifr.ifr_addr.sa_family;
1069                         
1070 #ifdef CONFIG_INET      
1071                                 /* This is naughty. When net-032e comes out It wants moving into the net032
1072                                    code not the kernel. Till then it can sit here (SIGH) */             
1073                                 dev->pa_mask = ip_get_mask(dev->pa_addr);
1074 #endif                  
1075                                 dev->pa_brdaddr = dev->pa_addr | ~dev->pa_mask;
1076                                 ret = 0;
1077                         }
1078                         break;
1079                         
1080                 case SIOCGIFBRDADDR:    /* Get the broadcast address */
1081                         (*(struct sockaddr_in *)
1082                                 &ifr.ifr_broadaddr).sin_addr.s_addr = dev->pa_brdaddr;
1083                         (*(struct sockaddr_in *)
1084                                 &ifr.ifr_broadaddr).sin_family = dev->family;
1085                         (*(struct sockaddr_in *)
1086                                 &ifr.ifr_broadaddr).sin_port = 0;
1087                         goto rarok;
1088 
1089                 case SIOCSIFBRDADDR:    /* Set the broadcast address */
1090                         dev->pa_brdaddr = (*(struct sockaddr_in *)
1091                                 &ifr.ifr_broadaddr).sin_addr.s_addr;
1092                         ret = 0;
1093                         break;
1094                         
1095                 case SIOCGIFDSTADDR:    /* Get the destination address (for point-to-point links) */
1096                         (*(struct sockaddr_in *)
1097                                 &ifr.ifr_dstaddr).sin_addr.s_addr = dev->pa_dstaddr;
1098                         (*(struct sockaddr_in *)
1099                                 &ifr.ifr_dstaddr).sin_family = dev->family;
1100                         (*(struct sockaddr_in *)
1101                                 &ifr.ifr_dstaddr).sin_port = 0;
1102                         goto rarok;
1103         
1104                 case SIOCSIFDSTADDR:    /* Set the destination address (for point-to-point links) */
1105                         dev->pa_dstaddr = (*(struct sockaddr_in *)
1106                                 &ifr.ifr_dstaddr).sin_addr.s_addr;
1107                         ret = 0;
1108                         break;
1109                         
1110                 case SIOCGIFNETMASK:    /* Get the netmask for the interface */
1111                         (*(struct sockaddr_in *)
1112                                 &ifr.ifr_netmask).sin_addr.s_addr = dev->pa_mask;
1113                         (*(struct sockaddr_in *)
1114                                 &ifr.ifr_netmask).sin_family = dev->family;
1115                         (*(struct sockaddr_in *)
1116                                 &ifr.ifr_netmask).sin_port = 0;
1117                         goto rarok;
1118 
1119                 case SIOCSIFNETMASK:    /* Set the netmask for the interface */
1120                         {
1121                                 unsigned long mask = (*(struct sockaddr_in *)
1122                                         &ifr.ifr_netmask).sin_addr.s_addr;
1123                                 ret = -EINVAL;
1124                                 /*
1125                                  *      The mask we set must be legal.
1126                                  */
1127                                 if (bad_mask(mask,0))
1128                                         break;
1129                                 dev->pa_mask = mask;
1130                                 ret = 0;
1131                         }
1132                         break;
1133                         
1134                 case SIOCGIFMETRIC:     /* Get the metric on the interface (currently unused) */
1135                         
1136                         ifr.ifr_metric = dev->metric;
1137                         goto  rarok;
1138                         
1139                 case SIOCSIFMETRIC:     /* Set the metric on the interface (currently unused) */
1140                         dev->metric = ifr.ifr_metric;
1141                         ret=0;
1142                         break;
1143         
1144                 case SIOCGIFMTU:        /* Get the MTU of a device */
1145                         ifr.ifr_mtu = dev->mtu;
1146                         goto rarok;
1147         
1148                 case SIOCSIFMTU:        /* Set the MTU of a device */
1149                 
1150                         /*
1151                          *      MTU must be positive.
1152                          */
1153                          
1154                         if(ifr.ifr_mtu<68)
1155                                 return -EINVAL;
1156                         dev->mtu = ifr.ifr_mtu;
1157                         ret = 0;
1158                         break;
1159         
1160                 case SIOCGIFMEM:        /* Get the per device memory space. We can add this but currently
1161                                            do not support it */
1162                         ret = -EINVAL;
1163                         break;
1164                 
1165                 case SIOCSIFMEM:        /* Set the per device memory buffer space. Not applicable in our case */
1166                         ret = -EINVAL;
1167                         break;
1168 
1169                 case SIOCGIFHWADDR:
1170                         memcpy(ifr.ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1171                         ifr.ifr_hwaddr.sa_family=dev->type;                     
1172                         goto rarok;
1173                                 
1174                 case SIOCSIFHWADDR:
1175                         if(dev->set_mac_address==NULL)
1176                                 return -EOPNOTSUPP;
1177                         if(ifr.ifr_hwaddr.sa_family!=dev->type)
1178                                 return -EINVAL;
1179                         ret=dev->set_mac_address(dev,&ifr.ifr_hwaddr);
1180                         break;
1181                         
1182                 case SIOCGIFMAP:
1183                         ifr.ifr_map.mem_start=dev->mem_start;
1184                         ifr.ifr_map.mem_end=dev->mem_end;
1185                         ifr.ifr_map.base_addr=dev->base_addr;
1186                         ifr.ifr_map.irq=dev->irq;
1187                         ifr.ifr_map.dma=dev->dma;
1188                         ifr.ifr_map.port=dev->if_port;
1189                         goto rarok;
1190                         
1191                 case SIOCSIFMAP:
1192                         if(dev->set_config==NULL)
1193                                 return -EOPNOTSUPP;
1194                         return dev->set_config(dev,&ifr.ifr_map);
1195                         
1196                 case SIOCADDMULTI:
1197                         if(dev->set_multicast_list==NULL)
1198                                 return -EINVAL;
1199                         if(ifr.ifr_hwaddr.sa_family!=AF_UNSPEC)
1200                                 return -EINVAL;
1201                         dev_mc_add(dev,ifr.ifr_hwaddr.sa_data, dev->addr_len, 1);
1202                         return 0;
1203 
1204                 case SIOCDELMULTI:
1205                         if(dev->set_multicast_list==NULL)
1206                                 return -EINVAL;
1207                         if(ifr.ifr_hwaddr.sa_family!=AF_UNSPEC)
1208                                 return -EINVAL;
1209                         dev_mc_delete(dev,ifr.ifr_hwaddr.sa_data,dev->addr_len, 1);
1210                         return 0;
1211                 /*
1212                  *      Unknown or private ioctl
1213                  */
1214 
1215                 default:
1216                         if((getset >= SIOCDEVPRIVATE) &&
1217                            (getset <= (SIOCDEVPRIVATE + 15))) {
1218                                 if(dev->do_ioctl==NULL)
1219                                         return -EOPNOTSUPP;
1220                                 ret=dev->do_ioctl(dev, &ifr, getset);
1221                                 memcpy_tofs(arg,&ifr,sizeof(struct ifreq));
1222                                 break;
1223                         }
1224                         
1225                         ret = -EINVAL;
1226         }
1227         return(ret);
1228 /*
1229  *      The load of calls that return an ifreq and ok (saves memory).
1230  */
1231 rarok:
1232         memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
1233         return 0;
1234 }
1235 
1236 
1237 /*
1238  *      This function handles all "interface"-type I/O control requests. The actual
1239  *      'doing' part of this is dev_ifsioc above.
1240  */
1241 
1242 int dev_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1243 {
1244         switch(cmd) 
1245         {
1246                 case SIOCGIFCONF:
1247                         (void) dev_ifconf((char *) arg);
1248                         return 0;
1249 
1250                 /*
1251                  *      Ioctl calls that can be done by all.
1252                  */
1253                  
1254                 case SIOCGIFFLAGS:
1255                 case SIOCGIFADDR:
1256                 case SIOCGIFDSTADDR:
1257                 case SIOCGIFBRDADDR:
1258                 case SIOCGIFNETMASK:
1259                 case SIOCGIFMETRIC:
1260                 case SIOCGIFMTU:
1261                 case SIOCGIFMEM:
1262                 case SIOCGIFHWADDR:
1263                 case SIOCSIFHWADDR:
1264                 case SIOCGIFSLAVE:
1265                 case SIOCGIFMAP:
1266                         return dev_ifsioc(arg, cmd);
1267 
1268                 /*
1269                  *      Ioctl calls requiring the power of a superuser
1270                  */
1271                  
1272                 case SIOCSIFFLAGS:
1273                 case SIOCSIFADDR:
1274                 case SIOCSIFDSTADDR:
1275                 case SIOCSIFBRDADDR:
1276                 case SIOCSIFNETMASK:
1277                 case SIOCSIFMETRIC:
1278                 case SIOCSIFMTU:
1279                 case SIOCSIFMEM:
1280                 case SIOCSIFMAP:
1281                 case SIOCSIFSLAVE:
1282                 case SIOCADDMULTI:
1283                 case SIOCDELMULTI:
1284                         if (!suser())
1285                                 return -EPERM;
1286                         return dev_ifsioc(arg, cmd);
1287         
1288                 case SIOCSIFLINK:
1289                         return -EINVAL;
1290 
1291                 /*
1292                  *      Unknown or private ioctl.
1293                  */     
1294                  
1295                 default:
1296                         if((cmd >= SIOCDEVPRIVATE) &&
1297                            (cmd <= (SIOCDEVPRIVATE + 15))) {
1298                                 return dev_ifsioc(arg, cmd);
1299                         }
1300                         return -EINVAL;
1301         }
1302 }
1303 
1304 
1305 /*
1306  *      Initialize the DEV module. At boot time this walks the device list and
1307  *      unhooks any devices that fail to initialise (normally hardware not 
1308  *      present) and leaves us with a valid list of present and active devices.
1309  *
1310  */
1311 extern int lance_init(void);
1312 extern int pi_init(void);
1313 extern int dec21040_init(void);
1314 
1315 int net_dev_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1316 {
1317         struct device *dev, **dp;
1318 
1319         /*
1320          *      Initialise the packet receive queue.
1321          */
1322          
1323         skb_queue_head_init(&backlog);
1324         
1325         /*
1326          * This is VeryUgly(tm).
1327          *
1328          * Some devices want to be initialized eary..
1329          */
1330 #if defined(CONFIG_LANCE)
1331         lance_init();
1332 #endif
1333 #if defined(CONFIG_PI)
1334         pi_init();
1335 #endif  
1336 #if defined(CONFIG_PT)
1337         pt_init();
1338 #endif
1339 #if defined(CONFIG_DEC_ELCP)
1340         dec21040_init();
1341 #endif  
1342         /*
1343          *      SLHC if present needs attaching so other people see it
1344          *      even if not opened.
1345          */
1346 #if (defined(CONFIG_SLIP_COMPRESSED) || defined(CONFIG_PPP)) && defined(CONFIG_SLHC_BUILTIN)
1347         slhc_install();
1348 #endif  
1349 
1350         /*
1351          *      Add the devices.
1352          *      If the call to dev->init fails, the dev is removed
1353          *      from the chain disconnecting the device until the
1354          *      next reboot.
1355          */
1356 
1357         dp = &dev_base;
1358         while ((dev = *dp) != NULL)
1359         {
1360                 int i;
1361                 for (i = 0; i < DEV_NUMBUFFS; i++)  {
1362                         skb_queue_head_init(dev->buffs + i);
1363                 }
1364 
1365                 if (dev->init && dev->init(dev)) 
1366                 {
1367                         /*
1368                          *      It failed to come up. Unhook it.
1369                          */
1370                         *dp = dev->next;
1371                 } 
1372                 else
1373                 {
1374                         dp = &dev->next;
1375                 }
1376         }
1377 
1378 #ifdef CONFIG_PROC_FS
1379         proc_net_register(&(struct proc_dir_entry) {
1380                 PROC_NET_DEV, 3, "dev",
1381                 S_IFREG | S_IRUGO, 1, 0, 0,
1382                 0, &proc_net_inode_operations,
1383                 dev_get_info
1384         });
1385 #endif
1386 
1387         /*      
1388          *      Initialise net_alias engine 
1389          *
1390          *              - register net_alias device notifier
1391          *              - register proc entries:        /proc/net/alias_types
1392          *                                                                      /proc/net/aliases
1393          */
1394 
1395 #ifdef CONFIG_NET_ALIAS
1396         net_alias_init();
1397 #endif
1398 
1399         bh_base[NET_BH].routine = net_bh;
1400         enable_bh(NET_BH);
1401         return 0;
1402 }

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