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

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