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

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