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