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 structpacket_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 staticstructsk_buff_headbacklog =
65 { 66 (structsk_buff *)&backlog, (structsk_buff *)&backlog 67 #ifdefCONFIG_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 staticintbacklog_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 staticintdev_nit=0;
84
85 /* 86 * Return the lesser of the two values. 87 */ 88
89 static__inline__unsignedlongmin(unsignedlonga, unsignedlongb)
/* */ 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 voiddev_add_pack(structpacket_type *pt)
/* */ 107 { 108 structpacket_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 voiddev_remove_pack(structpacket_type *pt)
/* */ 173 { 174 structpacket_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 structdevice *dev_get(char *name)
/* */ 229 { 230 structdevice *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 intdev_open(structdevice *dev)
/* */ 246 { 247 intret = 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 intdev_close(structdevice *dev)
/* */ 276 { 277 /* 278 * Only close a device if it is up. 279 */ 280
281 if (dev->flags != 0)
282 { 283 intct=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 #ifdefCONFIG_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 structsk_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 voiddev_queue_xmit(structsk_buff *skb, structdevice *dev, intpri)
/* */ 324 { 325 unsignedlongflags;
326 intwhere = 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 #ifdefCONFIG_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 #ifdefCONFIG_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 #ifdefCONFIG_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 #ifdefCONFIG_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 #ifdefCONFIG_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 voidnetif_rx(structsk_buff *skb)
/* */ 429 { 430 staticintdropping = 0;
431 externstructtimevalxtime;
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 elseif (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 intdev_rint(unsignedchar *buff, longlen, intflags, structdevice *dev)
/* */ 491 { 492 staticintdropping = 0;
493 structsk_buff *skb = NULL;
494 unsignedchar *to;
495 intamount, left;
496 intlen2;
497
498 if (dev == NULL || buff == NULL || len <= 0)
499 return(1);
500
501 if (flags & IN_SKBUFF)
502 { 503 skb = (structsk_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, (unsignedlong) dev->rmem_end -
536 (unsignedlong) buff);
537 memcpy(to, buff, amount);
538 len2 -= amount;
539 left -= amount;
540 buff += amount;
541 to += amount;
542 if ((unsignedlong) buff == dev->rmem_end)
543 buff = (unsignedchar *) 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 voiddev_transmit(void)
/* */ 568 { 569 structdevice *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 volatilecharin_bh = 0; /* Non-rentrant remember */ 596
597 intin_net_bh() /* Used by timer.c *//* */ 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 voidnet_bh(void *tmp)
/* */ 611 { 612 structsk_buff *skb;
613 structpacket_type *ptype;
614 unsignedshorttype;
615 unsignedcharflag = 0;
616 intnitcount;
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 structsk_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 = (unsignedchar *)(
711 (unsignedlong) skb2 +
712 (unsignedlong) skb->h.raw -
713 (unsignedlong) 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 voiddev_tint(structdevice *dev)
/* */ 780 { 781 inti;
782 structsk_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 staticintdev_ifconf(char *arg)
/* */ 818 { 819 structifconfifc;
820 structifreqifr;
821 structdevice *dev;
822 char *pos;
823 intlen;
824 interr;
825
826 /* 827 * Fetch the caller's info block. 828 */ 829
830 err=verify_area(VERIFY_WRITE, arg, sizeof(structifconf));
831 if(err)
832 returnerr;
833 memcpy_fromfs(&ifc, arg, sizeof(structifconf));
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 returnerr;
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(structifreq));
855 strcpy(ifr.ifr_name, dev->name);
856 (*(structsockaddr_in *) &ifr.ifr_addr).sin_family = dev->family;
857 (*(structsockaddr_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(structifreq));
864 pos += sizeof(structifreq);
865 len -= sizeof(structifreq);
866
867 /* 868 * Have we run out of space here ? 869 */ 870
871 if (len < sizeof(structifreq))
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 = (structifreq *) ifc.ifc_buf;
881 memcpy_tofs(arg, &ifc, sizeof(structifconf));
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 staticintsprintf_stats(char *buffer, structdevice *dev)
/* */ 897 { 898 structenet_statistics *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
899 intsize;
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 returnsize;
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 intdev_get_info(char *buffer, char **start, off_toffset, intlength)
/* */ 925 { 926 intlen=0;
927 off_tbegin=0;
928 off_tpos=0;
929 intsize;
930
931 structdevice *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 returnlen;
961 } 962
963
964 /* 965 * This checks bitmasks for the ioctl calls for devices. 966 */ 967
968 staticinlineintbad_mask(unsignedlongmask, unsignedlongaddr)
/* */ 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 staticintdev_ifsioc(void *arg, unsignedintgetset)
/* */ 986 { 987 structifreqifr;
988 structdevice *dev;
989 intret;
990
991 /* 992 * Fetch the caller's info block into kernel space 993 */ 994
995 interr=verify_area(VERIFY_WRITE, arg, sizeof(structifreq));
996 if(err)
997 returnerr;
998
999 memcpy_fromfs(&ifr, arg, sizeof(structifreq));
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 caseSIOCGIFFLAGS: /* Get interface flags */1011 ifr.ifr_flags = dev->flags;
1012 memcpy_tofs(arg, &ifr, sizeof(structifreq));
1013 ret = 0;
1014 break;
1015 caseSIOCSIFFLAGS: /* Set interface flags */1016 {1017 intold_flags = dev->flags;
1018 #ifdefCONFIG_SLAVE_BALANCING1019 if(dev->flags&IFF_SLAVE)
1020 return -EBUSY;
1021 #endif1022 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 #ifdefCONFIG_SLAVE_BALANCING1027 if(!(dev->flags&IFF_MASTER) && dev->slave)
1028 {1029 dev->slave->flags&=~IFF_SLAVE;
1030 dev->slave=NULL;
1031 }1032 #endif1033
1034 /*1035 * Has promiscuous mode been turned off1036 */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 on1042 */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 interface1049 */1050
1051 if ((old_flags & IFF_UP) && ((dev->flags & IFF_UP) == 0))
1052 {1053 ret = dev_close(dev);
1054 }1055 else1056 {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 caseSIOCGIFADDR: /* Get interface address (and family) */1073 (*(structsockaddr_in *)
1074 &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
1075 (*(structsockaddr_in *)
1076 &ifr.ifr_addr).sin_family = dev->family;
1077 (*(structsockaddr_in *)
1078 &ifr.ifr_addr).sin_port = 0;
1079 memcpy_tofs(arg, &ifr, sizeof(structifreq));
1080 ret = 0;
1081 break;
1082
1083 caseSIOCSIFADDR: /* Set interface address (and family) */1084 dev->pa_addr = (*(structsockaddr_in *)
1085 &ifr.ifr_addr).sin_addr.s_addr;
1086 dev->family = ifr.ifr_addr.sa_family;
1087
1088 #ifdefCONFIG_INET1089 /* This is naughty. When net-032e comes out It wants moving into the net0321090 code not the kernel. Till then it can sit here (SIGH) */1091 dev->pa_mask = ip_get_mask(dev->pa_addr);
1092 #endif1093 dev->pa_brdaddr = dev->pa_addr | ~dev->pa_mask;
1094 ret = 0;
1095 break;
1096
1097 caseSIOCGIFBRDADDR: /* Get the broadcast address */1098 (*(structsockaddr_in *)
1099 &ifr.ifr_broadaddr).sin_addr.s_addr = dev->pa_brdaddr;
1100 (*(structsockaddr_in *)
1101 &ifr.ifr_broadaddr).sin_family = dev->family;
1102 (*(structsockaddr_in *)
1103 &ifr.ifr_broadaddr).sin_port = 0;
1104 memcpy_tofs(arg, &ifr, sizeof(structifreq));
1105 ret = 0;
1106 break;
1107
1108 caseSIOCSIFBRDADDR: /* Set the broadcast address */1109 dev->pa_brdaddr = (*(structsockaddr_in *)
1110 &ifr.ifr_broadaddr).sin_addr.s_addr;
1111 ret = 0;
1112 break;
1113
1114 caseSIOCGIFDSTADDR: /* Get the destination address (for point-to-point links) */1115 (*(structsockaddr_in *)
1116 &ifr.ifr_dstaddr).sin_addr.s_addr = dev->pa_dstaddr;
1117 (*(structsockaddr_in *)
1118 &ifr.ifr_broadaddr).sin_family = dev->family;
1119 (*(structsockaddr_in *)
1120 &ifr.ifr_broadaddr).sin_port = 0;
1121 memcpy_tofs(arg, &ifr, sizeof(structifreq));
1122 ret = 0;
1123 break;
1124
1125 caseSIOCSIFDSTADDR: /* Set the destination address (for point-to-point links) */1126 dev->pa_dstaddr = (*(structsockaddr_in *)
1127 &ifr.ifr_dstaddr).sin_addr.s_addr;
1128 ret = 0;
1129 break;
1130
1131 caseSIOCGIFNETMASK: /* Get the netmask for the interface */1132 (*(structsockaddr_in *)
1133 &ifr.ifr_netmask).sin_addr.s_addr = dev->pa_mask;
1134 (*(structsockaddr_in *)
1135 &ifr.ifr_netmask).sin_family = dev->family;
1136 (*(structsockaddr_in *)
1137 &ifr.ifr_netmask).sin_port = 0;
1138 memcpy_tofs(arg, &ifr, sizeof(structifreq));
1139 ret = 0;
1140 break;
1141
1142 caseSIOCSIFNETMASK: /* Set the netmask for the interface */1143 {1144 unsignedlongmask = (*(structsockaddr_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 caseSIOCGIFMETRIC: /* Get the metric on the inteface (currently unused) */1158
1159 ifr.ifr_metric = dev->metric;
1160 memcpy_tofs(arg, &ifr, sizeof(structifreq));
1161 ret = 0;
1162 break;
1163
1164 caseSIOCSIFMETRIC: /* Set the metric on the interface (currently unused) */1165 dev->metric = ifr.ifr_metric;
1166 ret = 0;
1167 break;
1168
1169 caseSIOCGIFMTU: /* Get the MTU of a device */1170 ifr.ifr_mtu = dev->mtu;
1171 memcpy_tofs(arg, &ifr, sizeof(structifreq));
1172 ret = 0;
1173 break;
1174
1175 caseSIOCSIFMTU: /* Set the MTU of a device */1176
1177 /*1178 * MTU must be positive and under the page size problem1179 */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 caseSIOCGIFMEM: /* Get the per device memory space. We can add this but currently1188 do not support it */1189 printk("NET: ioctl(SIOCGIFMEM, 0x%08X)\n", (int)arg);
1190 ret = -EINVAL;
1191 break;
1192
1193 caseSIOCSIFMEM: /* 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 caseOLD_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(structifreq));
1201 ret=0;
1202 break;
1203
1204 caseSIOCGIFHWADDR:
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(structifreq));
1208 ret=0;
1209 break;
1210
1211 caseSIOCSIFHWADDR:
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 caseSIOCDEVPRIVATE:
1220 if(dev->do_ioctl==NULL)
1221 return -EOPNOTSUPP;
1222 returndev->do_ioctl(dev, &ifr);
1223
1224 caseSIOCGIFMAP:
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(structifreq));
1232 ret=0;
1233 break;
1234
1235 caseSIOCSIFMAP:
1236 if(dev->set_config==NULL)
1237 return -EOPNOTSUPP;
1238 returndev->set_config(dev,&ifr.ifr_map);
1239
1240 caseSIOCGIFSLAVE:
1241 #ifdefCONFIG_SLAVE_BALANCING1242 if(dev->slave==NULL)
1243 return -ENOENT;
1244 strncpy(ifr.ifr_name,dev->name,sizeof(ifr.ifr_name));
1245 memcpy_tofs(arg,&ifr,sizeof(structifreq));
1246 ret=0;
1247 #else1248 return -ENOENT;
1249 #endif1250 break;
1251 #ifdefCONFIG_SLAVE_BALANCING1252 caseSIOCSIFSLAVE:
1253 {1254
1255 /*1256 * Fun game. Get the device up and the flags right without1257 * letting some scummy user confuse us.1258 */1259 unsignedlongflags;
1260 structdevice *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 #endif1295 /*1296 * Unknown ioctl1297 */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 actual1308 * 'doing' part of this is dev_ifsioc above.1309 */1310
1311 intdev_ioctl(unsignedintcmd, void *arg)
/* */1312 {1313 switch(cmd)
1314 {1315 /*1316 * The old old setup ioctl. Even its name and this entry will soon be1317 * just so much ionization on a backup tape.1318 */1319
1320 caseSIOCGIFCONF:
1321 (void) dev_ifconf((char *) arg);
1322 return 0;
1323
1324 /*1325 * Ioctl calls that can be done by all.1326 */1327
1328 caseSIOCGIFFLAGS:
1329 caseSIOCGIFADDR:
1330 caseSIOCGIFDSTADDR:
1331 caseSIOCGIFBRDADDR:
1332 caseSIOCGIFNETMASK:
1333 caseSIOCGIFMETRIC:
1334 caseSIOCGIFMTU:
1335 caseSIOCGIFMEM:
1336 caseSIOCGIFHWADDR:
1337 caseSIOCSIFHWADDR:
1338 caseOLD_SIOCGIFHWADDR:
1339 caseSIOCGIFSLAVE:
1340 caseSIOCGIFMAP:
1341 returndev_ifsioc(arg, cmd);
1342
1343 /*1344 * Ioctl calls requiring the power of a superuser1345 */1346
1347 caseSIOCSIFFLAGS:
1348 caseSIOCSIFADDR:
1349 caseSIOCSIFDSTADDR:
1350 caseSIOCSIFBRDADDR:
1351 caseSIOCSIFNETMASK:
1352 caseSIOCSIFMETRIC:
1353 caseSIOCSIFMTU:
1354 caseSIOCSIFMEM:
1355 caseSIOCSIFMAP:
1356 caseSIOCSIFSLAVE:
1357 caseSIOCDEVPRIVATE:
1358 if (!suser())
1359 return -EPERM;
1360 returndev_ifsioc(arg, cmd);
1361
1362 caseSIOCSIFLINK:
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 and1377 * 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 pair1381 * of register_inet_device() unregister_inet_device() calls. This will be1382 * needed for ethernet as modules support.1383 */1384
1385 voiddev_init(void)
/* */1386 {1387 structdevice *dev, *dev2;
1388
1389 /*1390 * Add the devices.1391 * If the call to dev->init fails, the dev is removed1392 * from the chain disconnecting the device until the1393 * 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 else1408 dev2->next = dev->next;
1409 }1410 else1411 {1412 dev2 = dev;
1413 }1414 }1415 }