root/net/inet/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. dev_queue_xmit
  8. netif_rx
  9. dev_rint
  10. dev_transmit
  11. in_net_bh
  12. net_bh
  13. dev_tint
  14. dev_ifconf
  15. sprintf_stats
  16. dev_get_info
  17. bad_mask
  18. dev_ifsioc
  19. dev_ioctl
  20. dev_init

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

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