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

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