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. net_dev_init

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

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