Import 2.1.36
[davej-history.git] / net / core / dev.c
blobc02d4052e0298ab571c6e089f5ec030ee45ffe77
1 /*
2 * NET3 Protocol independent device support routines.
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.
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>
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>
19 * Changes:
20 * Alan Cox : device private ioctl copies fields back.
21 * Alan Cox : Transmit queue code does relevant stunts to
22 * keep the queue safe.
23 * Alan Cox : Fixed double lock.
24 * Alan Cox : Fixed promisc NULL pointer trap
25 * ???????? : Support the full private ioctl range
26 * Alan Cox : Moved ioctl permission check into drivers
27 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
28 * Alan Cox : 100 backlog just doesn't cut it when
29 * you start doing multicast video 8)
30 * Alan Cox : Rewrote net_bh and list manager.
31 * Alan Cox : Fix ETH_P_ALL echoback lengths.
32 * Alan Cox : Took out transmit every packet pass
33 * Saved a few bytes in the ioctl handler
34 * Alan Cox : Network driver sets packet type before calling netif_rx. Saves
35 * a function call a packet.
36 * Alan Cox : Hashed net_bh()
37 * Richard Kooijman: Timestamp fixes.
38 * Alan Cox : Wrong field in SIOCGIFDSTADDR
39 * Alan Cox : Device lock protection.
40 * Alan Cox : Fixed nasty side effect of device close changes.
41 * Rudi Cilibrasi : Pass the right thing to set_mac_address()
42 * Dave Miller : 32bit quantity for the device lock to make it work out
43 * on a Sparc.
44 * Bjorn Ekwall : Added KERNELD hack.
45 * Alan Cox : Cleaned up the backlog initialise.
46 * Craig Metz : SIOCGIFCONF fix if space for under
47 * 1 device.
48 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
49 * is no device open function.
53 #include <asm/uaccess.h>
54 #include <asm/system.h>
55 #include <asm/bitops.h>
56 #include <linux/config.h>
57 #include <linux/types.h>
58 #include <linux/kernel.h>
59 #include <linux/sched.h>
60 #include <linux/string.h>
61 #include <linux/mm.h>
62 #include <linux/socket.h>
63 #include <linux/sockios.h>
64 #include <linux/in.h>
65 #include <linux/errno.h>
66 #include <linux/interrupt.h>
67 #include <linux/if_ether.h>
68 #include <linux/inet.h>
69 #include <linux/netdevice.h>
70 #include <linux/etherdevice.h>
71 #include <linux/notifier.h>
72 #include <net/ip.h>
73 #include <net/route.h>
74 #include <linux/skbuff.h>
75 #include <net/sock.h>
76 #include <net/arp.h>
77 #include <net/slhc.h>
78 #include <linux/proc_fs.h>
79 #include <linux/stat.h>
80 #include <net/br.h>
81 #include <linux/net_alias.h>
82 #include <linux/init.h>
83 #ifdef CONFIG_KERNELD
84 #include <linux/kerneld.h>
85 #endif
86 #ifdef CONFIG_NET_RADIO
87 #include <linux/wireless.h>
88 #endif /* CONFIG_NET_RADIO */
89 #ifdef CONFIG_PLIP
90 extern int plip_init(void);
91 #endif
94 * The list of devices, that are able to output.
97 static struct device *dev_up_base;
100 * The list of packet types we will receive (as opposed to discard)
101 * and the routines to invoke.
103 * Why 16. Because with 16 the only overlap we get on a hash of the
104 * low nibble of the protocol value is RARP/SNAP/X.25.
106 * 0800 IP
107 * 0001 802.3
108 * 0002 AX.25
109 * 0004 802.2
110 * 8035 RARP
111 * 0005 SNAP
112 * 0805 X.25
113 * 0806 ARP
114 * 8137 IPX
115 * 0009 Localtalk
116 * 86DD IPv6
119 struct packet_type *ptype_base[16]; /* 16 way hashed list */
120 struct packet_type *ptype_all = NULL; /* Taps */
123 * Device list lock
126 atomic_t dev_lockct = ATOMIC_INIT(0);
129 * Our notifier list
132 struct notifier_block *netdev_chain=NULL;
135 * Device drivers call our routines to queue packets here. We empty the
136 * queue in the bottom half handler.
139 static struct sk_buff_head backlog;
142 * We don't overdo the queue or we will thrash memory badly.
145 static int backlog_size = 0;
149 /******************************************************************************************
151 Protocol management and registration routines
153 *******************************************************************************************/
156 * For efficiency
159 static int dev_nit=0;
162 * Add a protocol ID to the list. Now that the input handler is
163 * smarter we can dispense with all the messy stuff that used to be
164 * here.
167 void dev_add_pack(struct packet_type *pt)
169 int hash;
170 if(pt->type==htons(ETH_P_ALL))
172 dev_nit++;
173 pt->next=ptype_all;
174 ptype_all=pt;
176 else
178 hash=ntohs(pt->type)&15;
179 pt->next = ptype_base[hash];
180 ptype_base[hash] = pt;
186 * Remove a protocol ID from the list.
189 void dev_remove_pack(struct packet_type *pt)
191 struct packet_type **pt1;
192 if(pt->type==htons(ETH_P_ALL))
194 dev_nit--;
195 pt1=&ptype_all;
197 else
198 pt1=&ptype_base[ntohs(pt->type)&15];
199 for(; (*pt1)!=NULL; pt1=&((*pt1)->next))
201 if(pt==(*pt1))
203 *pt1=pt->next;
204 return;
207 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
210 /*****************************************************************************************
212 Device Interface Subroutines
214 ******************************************************************************************/
217 * Find an interface by name.
220 struct device *dev_get(const char *name)
222 struct device *dev;
224 for (dev = dev_base; dev != NULL; dev = dev->next)
226 if (strcmp(dev->name, name) == 0)
227 return(dev);
229 return NULL;
232 struct device * dev_get_by_index(int ifindex)
234 struct device *dev;
236 for (dev = dev_base; dev != NULL; dev = dev->next)
238 if (dev->ifindex == ifindex)
239 return(dev);
241 return NULL;
244 struct device *dev_getbyhwaddr(unsigned short type, char *ha)
246 struct device *dev;
248 for (dev = dev_base; dev != NULL; dev = dev->next)
250 if (dev->type == type &&
251 !(dev->flags&(IFF_LOOPBACK|IFF_NOARP)) &&
252 memcmp(dev->dev_addr, ha, dev->addr_len) == 0)
253 return(dev);
255 return(NULL);
259 * Passed a format string - eg "lt%d" it will try and find a suitable
260 * id. Not efficient for many devices, not called a lot..
263 int dev_alloc_name(struct device *dev, const char *name)
265 int i;
267 * If you need over 100 please also fix the algorithm...
269 for(i=0;i<100;i++)
271 sprintf(dev->name,name,i);
272 if(dev_get(dev->name)==NULL)
273 return i;
275 return -ENFILE; /* Over 100 of the things .. bail out! */
278 struct device *dev_alloc(const char *name, int *err)
280 struct device *dev=kmalloc(sizeof(struct device)+16, GFP_KERNEL);
281 if(dev==NULL)
283 *err=-ENOBUFS;
284 return NULL;
286 dev->name=(char *)(dev+1); /* Name string space */
287 *err=dev_alloc_name(dev,name);
288 if(*err<0)
290 kfree(dev);
291 return NULL;
293 return dev;
298 * Find and possibly load an interface.
301 #ifdef CONFIG_KERNELD
303 void dev_load(const char *name)
305 if(!dev_get(name)) {
306 #ifdef CONFIG_NET_ALIAS
307 const char *sptr;
309 for (sptr=name ; *sptr ; sptr++) if(*sptr==':') break;
310 if (!(*sptr && *(sptr+1)))
311 #endif
312 request_module(name);
316 #endif
319 * Prepare an interface for use.
322 int dev_open(struct device *dev)
324 int ret = 0;
327 * Call device private open method
330 if (dev->open)
331 ret = dev->open(dev);
334 * If it went open OK then set the flags
337 if (ret == 0)
339 dev->flags |= (IFF_UP | IFF_RUNNING);
341 * Initialise multicasting status
343 dev_mc_upload(dev);
344 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
347 * Passive non transmitting devices (including
348 * aliases) need not be on this chain.
350 if (!net_alias_is(dev) && dev->tx_queue_len)
352 cli();
353 dev->next_up = dev_up_base;
354 dev_up_base = dev;
355 sti();
358 return(ret);
363 * Completely shutdown an interface.
366 int dev_close(struct device *dev)
368 int ct=0;
369 struct device **devp;
372 * Call the device specific close. This cannot fail.
373 * Only if device is UP
376 if ((dev->flags & IFF_UP) && dev->stop)
377 dev->stop(dev);
380 * Device is now down.
383 dev->flags&=~(IFF_UP|IFF_RUNNING);
386 * Tell people we are going down
389 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
391 * Flush the multicast chain
393 dev_mc_discard(dev);
396 * Purge any queued packets when we down the link
398 while(ct<DEV_NUMBUFFS)
400 struct sk_buff *skb;
401 while((skb=skb_dequeue(&dev->buffs[ct]))!=NULL)
402 kfree_skb(skb,FREE_WRITE);
403 ct++;
407 * The device is no longer up. Drop it from the list.
410 devp = &dev_up_base;
411 while (*devp)
413 if (*devp == dev)
415 *devp = dev->next_up;
416 break;
418 devp = &(*devp)->next_up;
420 return(0);
425 * Device change register/unregister. These are not inline or static
426 * as we export them to the world.
429 int register_netdevice_notifier(struct notifier_block *nb)
431 return notifier_chain_register(&netdev_chain, nb);
434 int unregister_netdevice_notifier(struct notifier_block *nb)
436 return notifier_chain_unregister(&netdev_chain,nb);
440 * Support routine. Sends outgoing frames to any network
441 * taps currently in use.
444 static void queue_xmit_nit(struct sk_buff *skb, struct device *dev)
446 struct packet_type *ptype;
447 get_fast_time(&skb->stamp);
449 for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next)
451 /* Never send packets back to the socket
452 * they originated from - MvS (miquels@drinkel.ow.org)
454 if ((ptype->dev == dev || !ptype->dev) &&
455 ((struct sock *)ptype->data != skb->sk))
457 struct sk_buff *skb2;
458 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
459 break;
460 skb2->mac.raw = skb2->data;
461 skb2->nh.raw = skb2->h.raw = skb2->data + dev->hard_header_len;
462 ptype->func(skb2, skb->dev, ptype);
468 * Send (or queue for sending) a packet.
470 * IMPORTANT: When this is called to resend frames. The caller MUST
471 * already have locked the sk_buff. Apart from that we do the
472 * rest of the magic.
475 static void do_dev_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
477 unsigned long flags;
478 struct sk_buff_head *list;
479 int retransmission = 0; /* used to say if the packet should go */
480 /* at the front or the back of the */
481 /* queue - front is a retransmit try */
484 * Negative priority is used to flag a frame that is being pulled from the
485 * queue front as a retransmit attempt. It therefore goes back on the queue
486 * start on a failure.
489 if (pri < 0)
491 pri = -pri-1;
492 retransmission = 1;
495 #ifdef CONFIG_NET_DEBUG
496 if (pri >= DEV_NUMBUFFS)
498 printk(KERN_WARNING "bad priority in do_dev_queue_xmit.\n");
499 pri = 1;
501 #endif
504 * If we are bridging and this is directly generated output
505 * pass the frame via the bridge.
508 #ifdef CONFIG_BRIDGE
509 if(skb->pkt_bridged!=IS_BRIDGED && br_stats.flags & BR_UP)
511 if(br_tx_frame(skb))
512 return;
514 #endif
516 list = dev->buffs + pri;
518 save_flags(flags);
521 * If this isn't a retransmission, use the first packet instead.
522 * Note: We don't do strict priority ordering here. We will in
523 * fact kick the queue that is our priority. The dev_tint reload
524 * does strict priority queueing. In effect what we are doing here
525 * is to add some random jitter to the queues and to do so by
526 * saving clocks. Doing a perfect priority queue isn't a good idea
527 * as you get some fascinating timing interactions.
530 if (!retransmission)
532 /* avoid overrunning the device queue.. */
533 if (skb_queue_len(list) > dev->tx_queue_len)
535 dev_kfree_skb(skb, FREE_WRITE);
536 return;
539 /* copy outgoing packets to any sniffer packet handlers */
540 if (dev_nit)
541 queue_xmit_nit(skb,dev);
543 if (skb_queue_len(list)) {
544 cli();
545 __skb_queue_tail(list, skb);
546 skb = __skb_dequeue(list);
547 restore_flags(flags);
550 if (dev->hard_start_xmit(skb, dev) == 0) {
552 * Packet is now solely the responsibility of the driver
554 return;
558 * Transmission failed, put skb back into a list. Once on the list it's safe and
559 * no longer device locked (it can be freed safely from the device queue)
561 cli();
562 __skb_queue_head(list,skb);
563 restore_flags(flags);
567 * Entry point for transmitting frames.
570 int dev_queue_xmit(struct sk_buff *skb)
572 struct device *dev = skb->dev;
574 start_bh_atomic();
577 * If the address has not been resolved. Call the device header rebuilder.
578 * This can cover all protocols and technically not just ARP either.
581 if (!skb->arp)
584 * FIXME: we should make the printk for no rebuild
585 * header a default rebuild_header routine and drop
586 * this call. Similarly we should make hard_header
587 * have a default NULL operation not check conditions.
589 if (dev->rebuild_header)
591 if (dev->rebuild_header(skb))
593 end_bh_atomic();
594 return 0;
597 else
598 printk(KERN_DEBUG "%s: !skb->arp & !rebuild_header!\n", dev->name);
603 * If dev is an alias, switch to its main device.
604 * "arp" resolution has been made with alias device, so
605 * arp entries refer to alias, not main.
609 if (net_alias_is(dev))
610 skb->dev = dev = net_alias_main_dev(dev);
612 do_dev_queue_xmit(skb, dev, skb->priority);
613 end_bh_atomic();
614 return 0;
618 * Fast path for loopback frames.
621 void dev_loopback_xmit(struct sk_buff *skb)
623 struct sk_buff *newskb=skb_clone(skb, GFP_ATOMIC);
624 if (newskb==NULL)
625 return;
627 skb_pull(newskb, newskb->nh.raw - newskb->data);
628 newskb->ip_summed = CHECKSUM_UNNECESSARY;
629 if (newskb->dst==NULL)
630 printk(KERN_DEBUG "BUG: packet without dst looped back 1\n");
631 netif_rx(newskb);
636 * Receive a packet from a device driver and queue it for the upper
637 * (protocol) levels. It always succeeds.
640 void netif_rx(struct sk_buff *skb)
642 static int dropping = 0;
645 * Any received buffers are un-owned and should be discarded
646 * when freed. These will be updated later as the frames get
647 * owners.
650 skb->sk = NULL;
651 if(skb->stamp.tv_sec==0)
652 get_fast_time(&skb->stamp);
655 * Check that we aren't overdoing things.
658 if (!backlog_size)
659 dropping = 0;
660 else if (backlog_size > 300)
661 dropping = 1;
663 if (dropping)
665 kfree_skb(skb, FREE_READ);
666 return;
670 * Add it to the "backlog" queue.
673 skb_queue_tail(&backlog,skb);
674 backlog_size++;
677 * If any packet arrived, mark it for processing after the
678 * hardware interrupt returns.
681 mark_bh(NET_BH);
682 return;
686 * This routine causes all interfaces to try to send some data.
689 static void dev_transmit(void)
691 struct device *dev;
693 for (dev = dev_up_base; dev != NULL; dev = dev->next_up)
695 if (dev->flags != 0 && !dev->tbusy)
698 * Kick the device
700 dev_tint(dev);
706 /**********************************************************************************
708 Receive Queue Processor
710 ***********************************************************************************/
713 * When we are called the queue is ready to grab, the interrupts are
714 * on and hardware can interrupt and queue to the receive queue as we
715 * run with no problems.
716 * This is run as a bottom half after an interrupt handler that does
717 * mark_bh(NET_BH);
720 void net_bh(void)
722 struct packet_type *ptype;
723 struct packet_type *pt_prev;
724 unsigned short type;
725 int nit = 301;
728 * Can we send anything now? We want to clear the
729 * decks for any more sends that get done as we
730 * process the input. This also minimises the
731 * latency on a transmit interrupt bh.
734 dev_transmit();
737 * Any data left to process. This may occur because a
738 * mark_bh() is done after we empty the queue including
739 * that from the device which does a mark_bh() just after
743 * While the queue is not empty..
745 * Note that the queue never shrinks due to
746 * an interrupt, so we can do this test without
747 * disabling interrupts.
750 while (!skb_queue_empty(&backlog))
752 struct sk_buff * skb = backlog.next;
755 * We have a packet. Therefore the queue has shrunk
757 cli();
758 __skb_unlink(skb, &backlog);
759 backlog_size--;
760 sti();
763 * We do not want to spin in net_bh infinitely. --ANK
765 if (--nit <= 0)
767 if (nit == 0)
768 printk(KERN_WARNING "net_bh: too many loops, dropping...\n");
769 kfree_skb(skb, FREE_WRITE);
770 continue;
773 #ifdef CONFIG_BRIDGE
776 * If we are bridging then pass the frame up to the
777 * bridging code (if this protocol is to be bridged).
778 * If it is bridged then move on
781 if (br_stats.flags & BR_UP && br_protocol_ok(ntohs(skb->protocol)))
784 * We pass the bridge a complete frame. This means
785 * recovering the MAC header first.
788 int offset=skb->data-skb->mac.raw;
789 cli();
790 skb_push(skb,offset); /* Put header back on for bridge */
791 if(br_receive_frame(skb))
793 sti();
794 continue;
797 * Pull the MAC header off for the copy going to
798 * the upper layers.
800 skb_pull(skb,offset);
801 sti();
803 #endif
806 * Bump the pointer to the next structure.
808 * On entry to the protocol layer. skb->data and
809 * skb->nh.raw point to the MAC and encapsulated data
812 /* XXX until we figure out every place to modify.. */
813 skb->h.raw = skb->nh.raw = skb->data;
816 * Fetch the packet protocol ID.
819 type = skb->protocol;
822 * We got a packet ID. Now loop over the "known protocols"
823 * list. There are two lists. The ptype_all list of taps (normally empty)
824 * and the main protocol list which is hashed perfectly for normal protocols.
827 pt_prev = NULL;
828 for (ptype = ptype_all; ptype!=NULL; ptype=ptype->next)
830 if(pt_prev)
832 struct sk_buff *skb2=skb_clone(skb, GFP_ATOMIC);
833 if(skb2)
834 pt_prev->func(skb2,skb->dev, pt_prev);
836 pt_prev=ptype;
839 for (ptype = ptype_base[ntohs(type)&15]; ptype != NULL; ptype = ptype->next)
841 if (ptype->type == type && (!ptype->dev || ptype->dev==skb->dev))
844 * We already have a match queued. Deliver
845 * to it and then remember the new match
847 if(pt_prev)
849 struct sk_buff *skb2;
851 skb2=skb_clone(skb, GFP_ATOMIC);
854 * Kick the protocol handler. This should be fast
855 * and efficient code.
858 if(skb2)
859 pt_prev->func(skb2, skb->dev, pt_prev);
861 /* Remember the current last to do */
862 pt_prev=ptype;
864 } /* End of protocol list loop */
867 * Is there a last item to send to ?
870 if(pt_prev)
871 pt_prev->func(skb, skb->dev, pt_prev);
873 * Has an unknown packet has been received ?
876 else
877 kfree_skb(skb, FREE_WRITE);
879 * Again, see if we can transmit anything now.
880 * [Ought to take this out judging by tests it slows
881 * us down not speeds us up]
883 #ifdef XMIT_EVERY
884 dev_transmit();
885 #endif
886 } /* End of queue loop */
889 * We have emptied the queue
893 * One last output flush.
896 dev_transmit();
901 * This routine is called when an device driver (i.e. an
902 * interface) is ready to transmit a packet.
905 void dev_tint(struct device *dev)
907 int i;
908 unsigned long flags;
909 struct sk_buff_head * head;
912 * aliases do not transmit (for now :) )
915 if (net_alias_is(dev)) {
916 printk(KERN_DEBUG "net alias %s transmits\n", dev->name);
917 return;
920 head = dev->buffs;
921 save_flags(flags);
922 cli();
925 * Work the queues in priority order
927 for(i = 0;i < DEV_NUMBUFFS; i++,head++)
930 while (!skb_queue_empty(head)) {
931 struct sk_buff *skb;
933 skb = head->next;
934 __skb_unlink(skb, head);
936 * Stop anyone freeing the buffer while we retransmit it
938 restore_flags(flags);
940 * Feed them to the output stage and if it fails
941 * indicate they re-queue at the front.
943 do_dev_queue_xmit(skb,dev,-i - 1);
945 * If we can take no more then stop here.
947 if (dev->tbusy)
948 return;
949 cli();
952 restore_flags(flags);
957 * Perform a SIOCGIFCONF call. This structure will change
958 * size eventually, and there is nothing I can do about it.
959 * Thus we will need a 'compatibility mode'.
962 static int dev_ifconf(char *arg)
964 struct ifconf ifc;
965 struct ifreq ifr;
966 struct device *dev;
967 char *pos;
968 int len;
969 int err;
972 * Fetch the caller's info block.
975 err = copy_from_user(&ifc, arg, sizeof(struct ifconf));
976 if (err)
977 return -EFAULT;
978 len = ifc.ifc_len;
979 pos = ifc.ifc_buf;
982 * We now walk the device list filling each active device
983 * into the array.
987 * Loop over the interfaces, and write an info block for each.
990 for (dev = dev_base; dev != NULL; dev = dev->next)
993 * Have we run out of space here ?
996 if (len < sizeof(struct ifreq))
997 break;
999 memset(&ifr, 0, sizeof(struct ifreq));
1000 strcpy(ifr.ifr_name, dev->name);
1001 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = dev->family;
1002 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
1006 * Write this block to the caller's space.
1009 err = copy_to_user(pos, &ifr, sizeof(struct ifreq));
1010 if (err)
1011 return -EFAULT;
1012 pos += sizeof(struct ifreq);
1013 len -= sizeof(struct ifreq);
1017 * All done. Write the updated control block back to the caller.
1020 ifc.ifc_len = (pos - ifc.ifc_buf);
1021 ifc.ifc_req = (struct ifreq *) ifc.ifc_buf;
1022 err = copy_to_user(arg, &ifc, sizeof(struct ifconf));
1023 if (err)
1024 return -EFAULT;
1027 * Report how much was filled in
1030 return(pos - arg);
1035 * This is invoked by the /proc filesystem handler to display a device
1036 * in detail.
1039 #ifdef CONFIG_PROC_FS
1040 static int sprintf_stats(char *buffer, struct device *dev)
1042 struct net_device_stats *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
1043 int size;
1045 if (stats)
1046 size = sprintf(buffer, "%6s:%8lu %7lu %4lu %4lu %4lu %4lu %8lu %8lu %4lu %4lu %4lu %5lu %4lu\n",
1047 dev->name,
1048 stats->rx_bytes,
1049 stats->rx_packets, stats->rx_errors,
1050 stats->rx_dropped + stats->rx_missed_errors,
1051 stats->rx_fifo_errors,
1052 stats->rx_length_errors + stats->rx_over_errors
1053 + stats->rx_crc_errors + stats->rx_frame_errors,
1054 stats->tx_bytes,
1055 stats->tx_packets, stats->tx_errors, stats->tx_dropped,
1056 stats->tx_fifo_errors, stats->collisions,
1057 stats->tx_carrier_errors + stats->tx_aborted_errors
1058 + stats->tx_window_errors + stats->tx_heartbeat_errors);
1059 else
1060 size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
1062 return size;
1066 * Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
1067 * to create /proc/net/dev
1070 int dev_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1072 int len=0;
1073 off_t begin=0;
1074 off_t pos=0;
1075 int size;
1077 struct device *dev;
1080 size = sprintf(buffer, "Inter-| Receive | Transmit\n"
1081 " face |bytes packets errs drop fifo frame|bytes packets errs drop fifo colls carrier\n");
1083 pos+=size;
1084 len+=size;
1087 for (dev = dev_base; dev != NULL; dev = dev->next)
1089 size = sprintf_stats(buffer+len, dev);
1090 len+=size;
1091 pos=begin+len;
1093 if(pos<offset)
1095 len=0;
1096 begin=pos;
1098 if(pos>offset+length)
1099 break;
1102 *start=buffer+(offset-begin); /* Start of wanted data */
1103 len-=(offset-begin); /* Start slop */
1104 if(len>length)
1105 len=length; /* Ending slop */
1106 return len;
1108 #endif /* CONFIG_PROC_FS */
1111 #ifdef CONFIG_NET_RADIO
1112 #ifdef CONFIG_PROC_FS
1115 * Print one entry of /proc/net/wireless
1116 * This is a clone of /proc/net/dev (just above)
1118 static int sprintf_wireless_stats(char *buffer, struct device *dev)
1120 /* Get stats from the driver */
1121 struct iw_statistics *stats = (dev->get_wireless_stats ?
1122 dev->get_wireless_stats(dev) :
1123 (struct iw_statistics *) NULL);
1124 int size;
1126 if(stats != (struct iw_statistics *) NULL)
1127 size = sprintf(buffer,
1128 "%6s: %02x %3d%c %3d%c %3d%c %5d %5d %5d\n",
1129 dev->name,
1130 stats->status,
1131 stats->qual.qual,
1132 stats->qual.updated & 1 ? '.' : ' ',
1133 stats->qual.level,
1134 stats->qual.updated & 2 ? '.' : ' ',
1135 stats->qual.noise,
1136 stats->qual.updated & 3 ? '.' : ' ',
1137 stats->discard.nwid,
1138 stats->discard.code,
1139 stats->discard.misc);
1140 else
1141 size = 0;
1143 return size;
1147 * Print info for /proc/net/wireless (print all entries)
1148 * This is a clone of /proc/net/dev (just above)
1150 int dev_get_wireless_info(char * buffer, char **start, off_t offset,
1151 int length, int dummy)
1153 int len = 0;
1154 off_t begin = 0;
1155 off_t pos = 0;
1156 int size;
1158 struct device * dev;
1160 size = sprintf(buffer,
1161 "Inter-|sta| Quality | Discarded packets\n"
1162 " face |tus|link level noise| nwid crypt misc\n");
1164 pos+=size;
1165 len+=size;
1167 for(dev = dev_base; dev != NULL; dev = dev->next)
1169 size = sprintf_wireless_stats(buffer+len, dev);
1170 len+=size;
1171 pos=begin+len;
1173 if(pos < offset)
1175 len=0;
1176 begin=pos;
1178 if(pos > offset + length)
1179 break;
1182 *start = buffer + (offset - begin); /* Start of wanted data */
1183 len -= (offset - begin); /* Start slop */
1184 if(len > length)
1185 len = length; /* Ending slop */
1187 return len;
1189 #endif /* CONFIG_PROC_FS */
1190 #endif /* CONFIG_NET_RADIO */
1194 * Perform the SIOCxIFxxx calls.
1196 * The socket layer has seen an ioctl the address family thinks is
1197 * for the device. At this point we get invoked to make a decision
1200 static int dev_ifsioc(void *arg, unsigned int getset)
1202 struct ifreq ifr;
1203 struct device *dev;
1204 int ret, err;
1207 * Fetch the caller's info block into kernel space
1210 err = copy_from_user(&ifr, arg, sizeof(struct ifreq));
1211 if (err)
1212 return -EFAULT;
1215 * See which interface the caller is talking about.
1220 * net_alias_dev_get(): dev_get() with added alias naming magic.
1221 * only allow alias creation/deletion if (getset==SIOCSIFADDR)
1225 #ifdef CONFIG_KERNELD
1226 dev_load(ifr.ifr_name);
1227 #endif
1229 #ifdef CONFIG_NET_ALIAS
1230 if ((dev = net_alias_dev_get(ifr.ifr_name, getset == SIOCSIFADDR, &err, NULL, NULL)) == NULL)
1231 return(err);
1232 #else
1233 if ((dev = dev_get(ifr.ifr_name)) == NULL)
1234 return(-ENODEV);
1235 #endif
1236 switch(getset)
1238 case SIOCGIFFLAGS: /* Get interface flags */
1239 ifr.ifr_flags = dev->flags;
1240 goto rarok;
1242 case SIOCSIFFLAGS: /* Set interface flags */
1244 int old_flags = dev->flags;
1247 * We are not allowed to potentially close/unload
1248 * a device until we get this lock.
1251 dev_lock_wait();
1252 dev_lock_list();
1255 * Set the flags on our device.
1258 dev->flags = (ifr.ifr_flags & (
1259 IFF_BROADCAST | IFF_DEBUG | IFF_LOOPBACK |
1260 IFF_POINTOPOINT | IFF_NOTRAILERS | IFF_RUNNING |
1261 IFF_NOARP | IFF_PROMISC | IFF_ALLMULTI | IFF_SLAVE | IFF_MASTER
1262 | IFF_MULTICAST)) | (dev->flags & IFF_UP);
1264 * Load in the correct multicast list now the flags have changed.
1267 dev_mc_upload(dev);
1270 * Have we downed the interface. We handle IFF_UP ourselves
1271 * according to user attempts to set it, rather than blindly
1272 * setting it.
1275 if ((old_flags^ifr.ifr_flags)&IFF_UP) /* Bit is different ? */
1277 if(old_flags&IFF_UP) /* Gone down */
1278 ret=dev_close(dev);
1279 else /* Come up */
1281 ret=dev_open(dev);
1282 if(ret<0)
1283 dev->flags&=~IFF_UP; /* Open failed */
1286 else
1287 ret=0;
1289 * Load in the correct multicast list now the flags have changed.
1292 dev_mc_upload(dev);
1293 if ((dev->flags&IFF_UP) && ((old_flags^dev->flags)&~(IFF_UP|IFF_RUNNING|IFF_PROMISC)))
1295 printk(KERN_DEBUG "SIFFL %s(%s)\n", dev->name, current->comm);
1296 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
1298 if ((dev->flags^old_flags)&IFF_PROMISC) {
1299 if (dev->flags&IFF_PROMISC)
1300 printk(KERN_INFO "%s enters promiscuous mode.\n", dev->name);
1301 else
1302 printk(KERN_INFO "%s leave promiscuous mode.\n", dev->name);
1304 dev_unlock_list();
1306 break;
1308 case SIOCGIFMETRIC: /* Get the metric on the interface (currently unused) */
1310 ifr.ifr_metric = dev->metric;
1311 goto rarok;
1313 case SIOCSIFMETRIC: /* Set the metric on the interface (currently unused) */
1314 dev->metric = ifr.ifr_metric;
1315 ret=0;
1316 break;
1318 case SIOCGIFMTU: /* Get the MTU of a device */
1319 ifr.ifr_mtu = dev->mtu;
1320 goto rarok;
1322 case SIOCSIFMTU: /* Set the MTU of a device */
1324 if (ifr.ifr_mtu == dev->mtu) {
1325 ret = 0;
1326 break;
1330 * MTU must be positive.
1333 if(ifr.ifr_mtu<68)
1334 return -EINVAL;
1336 if (dev->change_mtu)
1337 ret = dev->change_mtu(dev, ifr.ifr_mtu);
1338 else
1340 dev->mtu = ifr.ifr_mtu;
1341 ret = 0;
1343 if (!ret && dev->flags&IFF_UP) {
1344 printk(KERN_DEBUG "SIFMTU %s(%s)\n", dev->name, current->comm);
1345 notifier_call_chain(&netdev_chain, NETDEV_CHANGEMTU, dev);
1347 break;
1349 case SIOCGIFMEM: /* Get the per device memory space. We can add this but currently
1350 do not support it */
1351 ret = -EINVAL;
1352 break;
1354 case SIOCSIFMEM: /* Set the per device memory buffer space. Not applicable in our case */
1355 ret = -EINVAL;
1356 break;
1358 case SIOCGIFHWADDR:
1359 memcpy(ifr.ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1360 ifr.ifr_hwaddr.sa_family=dev->type;
1361 goto rarok;
1363 case SIOCSIFHWADDR:
1364 if(dev->set_mac_address==NULL)
1365 return -EOPNOTSUPP;
1366 if(ifr.ifr_hwaddr.sa_family!=dev->type)
1367 return -EINVAL;
1368 ret=dev->set_mac_address(dev,&ifr.ifr_hwaddr);
1369 if (!ret)
1370 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
1371 break;
1373 case SIOCGIFMAP:
1374 ifr.ifr_map.mem_start=dev->mem_start;
1375 ifr.ifr_map.mem_end=dev->mem_end;
1376 ifr.ifr_map.base_addr=dev->base_addr;
1377 ifr.ifr_map.irq=dev->irq;
1378 ifr.ifr_map.dma=dev->dma;
1379 ifr.ifr_map.port=dev->if_port;
1380 goto rarok;
1382 case SIOCSIFMAP:
1383 if(dev->set_config==NULL)
1384 return -EOPNOTSUPP;
1385 return dev->set_config(dev,&ifr.ifr_map);
1387 case SIOCADDMULTI:
1388 if(dev->set_multicast_list==NULL)
1389 return -EINVAL;
1390 if(ifr.ifr_hwaddr.sa_family!=AF_UNSPEC)
1391 return -EINVAL;
1392 dev_mc_add(dev,ifr.ifr_hwaddr.sa_data, dev->addr_len, 1);
1393 return 0;
1395 case SIOCDELMULTI:
1396 if(dev->set_multicast_list==NULL)
1397 return -EINVAL;
1398 if(ifr.ifr_hwaddr.sa_family!=AF_UNSPEC)
1399 return -EINVAL;
1400 dev_mc_delete(dev,ifr.ifr_hwaddr.sa_data,dev->addr_len, 1);
1401 return 0;
1403 case SIOGIFINDEX:
1404 ifr.ifr_ifindex = dev->ifindex;
1405 goto rarok;
1409 * Unknown or private ioctl
1412 default:
1413 if((getset >= SIOCDEVPRIVATE) &&
1414 (getset <= (SIOCDEVPRIVATE + 15))) {
1415 if(dev->do_ioctl==NULL)
1416 return -EOPNOTSUPP;
1417 ret = dev->do_ioctl(dev, &ifr, getset);
1418 if (!ret)
1420 err = copy_to_user(arg,&ifr,sizeof(struct ifreq));
1421 if (err)
1422 ret = -EFAULT;
1424 break;
1427 #ifdef CONFIG_NET_RADIO
1428 if((getset >= SIOCIWFIRST) && (getset <= SIOCIWLAST))
1430 if(dev->do_ioctl==NULL)
1431 return -EOPNOTSUPP;
1432 /* Perform the ioctl */
1433 ret=dev->do_ioctl(dev, &ifr, getset);
1434 /* If return args... */
1435 if(IW_IS_GET(getset))
1437 if (copy_to_user(arg, &ifr,
1438 sizeof(struct ifreq)))
1440 ret = -EFAULT;
1443 break;
1445 #endif /* CONFIG_NET_RADIO */
1447 ret = -EINVAL;
1449 return(ret);
1451 * The load of calls that return an ifreq and ok (saves memory).
1453 rarok:
1454 err = copy_to_user(arg, &ifr, sizeof(struct ifreq));
1455 if (err)
1456 err = -EFAULT;
1457 return err;
1462 * This function handles all "interface"-type I/O control requests. The actual
1463 * 'doing' part of this is dev_ifsioc above.
1466 int dev_ioctl(unsigned int cmd, void *arg)
1468 switch(cmd)
1470 case SIOCGIFCONF:
1471 (void) dev_ifconf((char *) arg);
1472 return 0;
1475 * Ioctl calls that can be done by all.
1478 case SIOCGIFFLAGS:
1479 case SIOCGIFMETRIC:
1480 case SIOCGIFMTU:
1481 case SIOCGIFMEM:
1482 case SIOCGIFHWADDR:
1483 case SIOCSIFHWADDR:
1484 case SIOCGIFSLAVE:
1485 case SIOCGIFMAP:
1486 case SIOGIFINDEX:
1487 return dev_ifsioc(arg, cmd);
1490 * Ioctl calls requiring the power of a superuser
1493 case SIOCSIFFLAGS:
1494 case SIOCSIFMETRIC:
1495 case SIOCSIFMTU:
1496 case SIOCSIFMEM:
1497 case SIOCSIFMAP:
1498 case SIOCSIFSLAVE:
1499 case SIOCADDMULTI:
1500 case SIOCDELMULTI:
1501 if (!suser())
1502 return -EPERM;
1503 return dev_ifsioc(arg, cmd);
1505 case SIOCSIFLINK:
1506 return -EINVAL;
1509 * Unknown or private ioctl.
1512 default:
1513 if((cmd >= SIOCDEVPRIVATE) &&
1514 (cmd <= (SIOCDEVPRIVATE + 15))) {
1515 return dev_ifsioc(arg, cmd);
1517 #ifdef CONFIG_NET_RADIO
1518 if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
1520 if((IW_IS_SET(cmd)) && (!suser()))
1521 return -EPERM;
1522 return dev_ifsioc(arg, cmd);
1524 #endif /* CONFIG_NET_RADIO */
1525 return -EINVAL;
1529 int dev_new_index()
1531 static int ifindex;
1532 return ++ifindex;
1536 * Initialize the DEV module. At boot time this walks the device list and
1537 * unhooks any devices that fail to initialise (normally hardware not
1538 * present) and leaves us with a valid list of present and active devices.
1541 extern int lance_init(void);
1542 extern int pi_init(void);
1543 extern int bpq_init(void);
1544 extern int scc_init(void);
1545 extern void sdla_setup(void);
1546 extern void dlci_setup(void);
1547 extern int pt_init(void);
1548 extern int sm_init(void);
1549 extern int baycom_init(void);
1550 extern int lapbeth_init(void);
1552 #ifdef CONFIG_PROC_FS
1553 static struct proc_dir_entry proc_net_dev = {
1554 PROC_NET_DEV, 3, "dev",
1555 S_IFREG | S_IRUGO, 1, 0, 0,
1556 0, &proc_net_inode_operations,
1557 dev_get_info
1559 #endif
1561 #ifdef CONFIG_NET_RADIO
1562 #ifdef CONFIG_PROC_FS
1563 static struct proc_dir_entry proc_net_wireless = {
1564 PROC_NET_WIRELESS, 8, "wireless",
1565 S_IFREG | S_IRUGO, 1, 0, 0,
1566 0, &proc_net_inode_operations,
1567 dev_get_wireless_info
1569 #endif /* CONFIG_PROC_FS */
1570 #endif /* CONFIG_NET_RADIO */
1572 __initfunc(int net_dev_init(void))
1574 struct device *dev, **dp;
1577 * Initialise the packet receive queue.
1580 skb_queue_head_init(&backlog);
1583 * The bridge has to be up before the devices
1586 #ifdef CONFIG_BRIDGE
1587 br_init();
1588 #endif
1591 * This is Very Ugly(tm).
1593 * Some devices want to be initialized early..
1595 #if defined(CONFIG_LANCE)
1596 lance_init();
1597 #endif
1598 #if defined(CONFIG_PI)
1599 pi_init();
1600 #endif
1601 #if defined(CONFIG_SCC)
1602 scc_init();
1603 #endif
1604 #if defined(CONFIG_PT)
1605 pt_init();
1606 #endif
1607 #if defined(CONFIG_BPQETHER)
1608 bpq_init();
1609 #endif
1610 #if defined(CONFIG_DLCI)
1611 dlci_setup();
1612 #endif
1613 #if defined(CONFIG_SDLA)
1614 sdla_setup();
1615 #endif
1616 #if defined(CONFIG_BAYCOM)
1617 baycom_init();
1618 #endif
1619 #if defined(CONFIG_SOUNDMODEM)
1620 sm_init();
1621 #endif
1622 #if defined(CONFIG_LAPBETHER)
1623 lapbeth_init();
1624 #endif
1625 #if defined(CONFIG_PLIP)
1626 plip_init();
1627 #endif
1629 * SLHC if present needs attaching so other people see it
1630 * even if not opened.
1632 #if (defined(CONFIG_SLIP) && defined(CONFIG_SLIP_COMPRESSED)) \
1633 || defined(CONFIG_PPP) \
1634 || (defined(CONFIG_ISDN) && defined(CONFIG_ISDN_PPP))
1635 slhc_install();
1636 #endif
1639 * Add the devices.
1640 * If the call to dev->init fails, the dev is removed
1641 * from the chain disconnecting the device until the
1642 * next reboot.
1645 dp = &dev_base;
1646 while ((dev = *dp) != NULL)
1648 int i;
1649 for (i = 0; i < DEV_NUMBUFFS; i++) {
1650 skb_queue_head_init(dev->buffs + i);
1653 if (dev->init && dev->init(dev))
1656 * It failed to come up. Unhook it.
1658 *dp = dev->next;
1660 else
1662 dp = &dev->next;
1663 dev->ifindex = dev_new_index();
1667 #ifdef CONFIG_PROC_FS
1668 proc_net_register(&proc_net_dev);
1669 #endif
1671 #ifdef CONFIG_NET_RADIO
1672 #ifdef CONFIG_PROC_FS
1673 proc_net_register(&proc_net_wireless);
1674 #endif /* CONFIG_PROC_FS */
1675 #endif /* CONFIG_NET_RADIO */
1678 * Initialise net_alias engine
1680 * - register net_alias device notifier
1681 * - register proc entries: /proc/net/alias_types
1682 * /proc/net/aliases
1685 #ifdef CONFIG_NET_ALIAS
1686 net_alias_init();
1687 #endif
1689 init_bh(NET_BH, net_bh);
1690 return 0;