Import 2.2.0pre6
[davej-history.git] / net / core / dev.c
blob95178439b0e4e0602b5ef2074a1f3e375eaeb462
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>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
21 * Changes:
22 * Alan Cox : device private ioctl copies fields back.
23 * Alan Cox : Transmit queue code does relevant stunts to
24 * keep the queue safe.
25 * Alan Cox : Fixed double lock.
26 * Alan Cox : Fixed promisc NULL pointer trap
27 * ???????? : Support the full private ioctl range
28 * Alan Cox : Moved ioctl permission check into drivers
29 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
30 * Alan Cox : 100 backlog just doesn't cut it when
31 * you start doing multicast video 8)
32 * Alan Cox : Rewrote net_bh and list manager.
33 * Alan Cox : Fix ETH_P_ALL echoback lengths.
34 * Alan Cox : Took out transmit every packet pass
35 * Saved a few bytes in the ioctl handler
36 * Alan Cox : Network driver sets packet type before calling netif_rx. Saves
37 * a function call a packet.
38 * Alan Cox : Hashed net_bh()
39 * Richard Kooijman: Timestamp fixes.
40 * Alan Cox : Wrong field in SIOCGIFDSTADDR
41 * Alan Cox : Device lock protection.
42 * Alan Cox : Fixed nasty side effect of device close changes.
43 * Rudi Cilibrasi : Pass the right thing to set_mac_address()
44 * Dave Miller : 32bit quantity for the device lock to make it work out
45 * on a Sparc.
46 * Bjorn Ekwall : Added KERNELD hack.
47 * Alan Cox : Cleaned up the backlog initialise.
48 * Craig Metz : SIOCGIFCONF fix if space for under
49 * 1 device.
50 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
51 * is no device open function.
52 * Andi Kleen : Fix error reporting for SIOCGIFCONF
53 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
54 * Cyrus Durgin : Cleaned for KMOD
55 * Adam Sulmicki : Bug Fix : Network Device Unload
56 * A network device unload needs to purge
57 * the backlog queue.
58 * Paul Rusty Russel : SIOCSIFNAME
61 #include <asm/uaccess.h>
62 #include <asm/system.h>
63 #include <asm/bitops.h>
64 #include <linux/config.h>
65 #include <linux/types.h>
66 #include <linux/kernel.h>
67 #include <linux/sched.h>
68 #include <linux/string.h>
69 #include <linux/mm.h>
70 #include <linux/socket.h>
71 #include <linux/sockios.h>
72 #include <linux/errno.h>
73 #include <linux/interrupt.h>
74 #include <linux/if_ether.h>
75 #include <linux/netdevice.h>
76 #include <linux/etherdevice.h>
77 #include <linux/notifier.h>
78 #include <linux/skbuff.h>
79 #include <net/sock.h>
80 #include <linux/rtnetlink.h>
81 #include <net/slhc.h>
82 #include <linux/proc_fs.h>
83 #include <linux/stat.h>
84 #include <net/br.h>
85 #include <net/dst.h>
86 #include <net/pkt_sched.h>
87 #include <net/profile.h>
88 #include <linux/init.h>
89 #include <linux/kmod.h>
90 #ifdef CONFIG_NET_RADIO
91 #include <linux/wireless.h>
92 #endif /* CONFIG_NET_RADIO */
93 #ifdef CONFIG_PLIP
94 extern int plip_init(void);
95 #endif
97 NET_PROFILE_DEFINE(dev_queue_xmit)
98 NET_PROFILE_DEFINE(net_bh)
99 NET_PROFILE_DEFINE(net_bh_skb)
102 const char *if_port_text[] = {
103 "unknown",
104 "BNC",
105 "10baseT",
106 "AUI",
107 "100baseT",
108 "100baseTX",
109 "100baseFX"
113 * The list of packet types we will receive (as opposed to discard)
114 * and the routines to invoke.
116 * Why 16. Because with 16 the only overlap we get on a hash of the
117 * low nibble of the protocol value is RARP/SNAP/X.25.
119 * 0800 IP
120 * 0001 802.3
121 * 0002 AX.25
122 * 0004 802.2
123 * 8035 RARP
124 * 0005 SNAP
125 * 0805 X.25
126 * 0806 ARP
127 * 8137 IPX
128 * 0009 Localtalk
129 * 86DD IPv6
132 struct packet_type *ptype_base[16]; /* 16 way hashed list */
133 struct packet_type *ptype_all = NULL; /* Taps */
136 * Device list lock. Setting it provides that interface
137 * will not disappear unexpectedly while kernel sleeps.
140 atomic_t dev_lockct = ATOMIC_INIT(0);
143 * Our notifier list
146 static struct notifier_block *netdev_chain=NULL;
149 * Device drivers call our routines to queue packets here. We empty the
150 * queue in the bottom half handler.
153 static struct sk_buff_head backlog;
155 #ifdef CONFIG_NET_FASTROUTE
156 int netdev_fastroute;
157 int netdev_fastroute_obstacles;
158 struct net_fastroute_stats dev_fastroute_stat;
159 #endif
161 static void dev_clear_backlog(struct device *dev);
164 /******************************************************************************************
166 Protocol management and registration routines
168 *******************************************************************************************/
171 * For efficiency
174 int netdev_nit=0;
177 * Add a protocol ID to the list. Now that the input handler is
178 * smarter we can dispense with all the messy stuff that used to be
179 * here.
181 * BEWARE!!! Protocol handlers, mangling input packets,
182 * MUST BE last in hash buckets and checking protocol handlers
183 * MUST start from promiscous ptype_all chain in net_bh.
184 * It is true now, do not change it.
185 * Explantion follows: if protocol handler, mangling packet, will
186 * be the first on list, it is not able to sense, that packet
187 * is cloned and should be copied-on-write, so that it will
188 * change it and subsequent readers will get broken packet.
189 * --ANK (980803)
192 void dev_add_pack(struct packet_type *pt)
194 int hash;
195 #ifdef CONFIG_NET_FASTROUTE
196 /* Hack to detect packet socket */
197 if (pt->data) {
198 netdev_fastroute_obstacles++;
199 dev_clear_fastroute(pt->dev);
201 #endif
202 if(pt->type==htons(ETH_P_ALL))
204 netdev_nit++;
205 pt->next=ptype_all;
206 ptype_all=pt;
208 else
210 hash=ntohs(pt->type)&15;
211 pt->next = ptype_base[hash];
212 ptype_base[hash] = pt;
218 * Remove a protocol ID from the list.
221 void dev_remove_pack(struct packet_type *pt)
223 struct packet_type **pt1;
224 if(pt->type==htons(ETH_P_ALL))
226 netdev_nit--;
227 pt1=&ptype_all;
229 else
230 pt1=&ptype_base[ntohs(pt->type)&15];
231 for(; (*pt1)!=NULL; pt1=&((*pt1)->next))
233 if(pt==(*pt1))
235 *pt1=pt->next;
236 #ifdef CONFIG_NET_FASTROUTE
237 if (pt->data)
238 netdev_fastroute_obstacles--;
239 #endif
240 return;
243 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
246 /*****************************************************************************************
248 Device Interface Subroutines
250 ******************************************************************************************/
253 * Find an interface by name.
256 struct device *dev_get(const char *name)
258 struct device *dev;
260 for (dev = dev_base; dev != NULL; dev = dev->next)
262 if (strcmp(dev->name, name) == 0)
263 return(dev);
265 return NULL;
268 struct device * dev_get_by_index(int ifindex)
270 struct device *dev;
272 for (dev = dev_base; dev != NULL; dev = dev->next)
274 if (dev->ifindex == ifindex)
275 return(dev);
277 return NULL;
280 struct device *dev_getbyhwaddr(unsigned short type, char *ha)
282 struct device *dev;
284 for (dev = dev_base; dev != NULL; dev = dev->next)
286 if (dev->type == type &&
287 memcmp(dev->dev_addr, ha, dev->addr_len) == 0)
288 return(dev);
290 return(NULL);
294 * Passed a format string - eg "lt%d" it will try and find a suitable
295 * id. Not efficient for many devices, not called a lot..
298 int dev_alloc_name(struct device *dev, const char *name)
300 int i;
302 * If you need over 100 please also fix the algorithm...
304 for(i=0;i<100;i++)
306 sprintf(dev->name,name,i);
307 if(dev_get(dev->name)==NULL)
308 return i;
310 return -ENFILE; /* Over 100 of the things .. bail out! */
313 struct device *dev_alloc(const char *name, int *err)
315 struct device *dev=kmalloc(sizeof(struct device)+16, GFP_KERNEL);
316 if(dev==NULL)
318 *err=-ENOBUFS;
319 return NULL;
321 dev->name=(char *)(dev+1); /* Name string space */
322 *err=dev_alloc_name(dev,name);
323 if(*err<0)
325 kfree(dev);
326 return NULL;
328 return dev;
333 * Find and possibly load an interface.
336 #ifdef CONFIG_KMOD
338 void dev_load(const char *name)
340 if(!dev_get(name) && capable(CAP_SYS_MODULE))
341 request_module(name);
344 #else
346 extern inline void dev_load(const char *unused){;}
348 #endif
350 static int default_rebuild_header(struct sk_buff *skb)
352 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n", skb->dev ? skb->dev->name : "NULL!!!");
353 kfree_skb(skb);
354 return 1;
358 * Prepare an interface for use.
361 int dev_open(struct device *dev)
363 int ret = 0;
366 * Is it already up?
369 if (dev->flags&IFF_UP)
370 return 0;
373 * Call device private open method
376 if (dev->open)
377 ret = dev->open(dev);
380 * If it went open OK then:
383 if (ret == 0)
386 * nil rebuild_header routine,
387 * that should be never called and used as just bug trap.
390 if (dev->rebuild_header == NULL)
391 dev->rebuild_header = default_rebuild_header;
394 * Set the flags.
396 dev->flags |= (IFF_UP | IFF_RUNNING);
399 * Initialize multicasting status
401 dev_mc_upload(dev);
404 * Wakeup transmit queue engine
406 dev_activate(dev);
409 * ... and announce new interface.
411 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
414 return(ret);
417 #ifdef CONFIG_NET_FASTROUTE
419 static __inline__ void dev_do_clear_fastroute(struct device *dev)
421 if (dev->accept_fastpath) {
422 int i;
424 for (i=0; i<=NETDEV_FASTROUTE_HMASK; i++)
425 dst_release(xchg(dev->fastpath+i, NULL));
429 void dev_clear_fastroute(struct device *dev)
431 if (dev) {
432 dev_do_clear_fastroute(dev);
433 } else {
434 for (dev = dev_base; dev; dev = dev->next)
435 dev_do_clear_fastroute(dev);
438 #endif
441 * Completely shutdown an interface.
444 int dev_close(struct device *dev)
446 if (!(dev->flags&IFF_UP))
447 return 0;
449 dev_deactivate(dev);
451 dev_lock_wait();
454 * Call the device specific close. This cannot fail.
455 * Only if device is UP
458 if (dev->stop)
459 dev->stop(dev);
461 if (dev->start)
462 printk("dev_close: bug %s still running\n", dev->name);
465 * Device is now down.
467 dev_clear_backlog(dev);
469 dev->flags&=~(IFF_UP|IFF_RUNNING);
470 #ifdef CONFIG_NET_FASTROUTE
471 dev_clear_fastroute(dev);
472 #endif
475 * Tell people we are going down
477 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
479 return(0);
484 * Device change register/unregister. These are not inline or static
485 * as we export them to the world.
488 int register_netdevice_notifier(struct notifier_block *nb)
490 return notifier_chain_register(&netdev_chain, nb);
493 int unregister_netdevice_notifier(struct notifier_block *nb)
495 return notifier_chain_unregister(&netdev_chain,nb);
499 * Support routine. Sends outgoing frames to any network
500 * taps currently in use.
503 void dev_queue_xmit_nit(struct sk_buff *skb, struct device *dev)
505 struct packet_type *ptype;
506 get_fast_time(&skb->stamp);
508 for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next)
510 /* Never send packets back to the socket
511 * they originated from - MvS (miquels@drinkel.ow.org)
513 if ((ptype->dev == dev || !ptype->dev) &&
514 ((struct sock *)ptype->data != skb->sk))
516 struct sk_buff *skb2;
517 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
518 break;
520 /* Code, following below is wrong.
522 The only reason, why it does work is that
523 ONLY packet sockets receive outgoing
524 packets. If such a packet will be (occasionally)
525 received by normal packet handler, which expects
526 that mac header is pulled...
529 /* More sensible variant. skb->nh should be correctly
530 set by sender, so that the second statement is
531 just protection against buggy protocols.
533 skb2->mac.raw = skb2->data;
535 if (skb2->nh.raw < skb2->data || skb2->nh.raw >= skb2->tail) {
536 if (net_ratelimit())
537 printk(KERN_DEBUG "protocol %04x is buggy, dev %s\n", skb2->protocol, dev->name);
538 skb2->nh.raw = skb2->data;
539 if (dev->hard_header)
540 skb2->nh.raw += dev->hard_header_len;
543 skb2->h.raw = skb2->nh.raw;
544 skb2->pkt_type = PACKET_OUTGOING;
545 ptype->func(skb2, skb->dev, ptype);
551 * Fast path for loopback frames.
554 void dev_loopback_xmit(struct sk_buff *skb)
556 struct sk_buff *newskb=skb_clone(skb, GFP_ATOMIC);
557 if (newskb==NULL)
558 return;
560 newskb->mac.raw = newskb->data;
561 skb_pull(newskb, newskb->nh.raw - newskb->data);
562 newskb->pkt_type = PACKET_LOOPBACK;
563 newskb->ip_summed = CHECKSUM_UNNECESSARY;
564 if (newskb->dst==NULL)
565 printk(KERN_DEBUG "BUG: packet without dst looped back 1\n");
566 netif_rx(newskb);
569 int dev_queue_xmit(struct sk_buff *skb)
571 struct device *dev = skb->dev;
572 struct Qdisc *q;
574 #ifdef CONFIG_NET_PROFILE
575 start_bh_atomic();
576 NET_PROFILE_ENTER(dev_queue_xmit);
577 #endif
579 start_bh_atomic();
580 q = dev->qdisc;
581 if (q->enqueue) {
582 q->enqueue(skb, q);
583 qdisc_wakeup(dev);
584 end_bh_atomic();
586 #ifdef CONFIG_NET_PROFILE
587 NET_PROFILE_LEAVE(dev_queue_xmit);
588 end_bh_atomic();
589 #endif
591 return 0;
594 /* The device has no queue. Common case for software devices:
595 loopback, all the sorts of tunnels...
597 Really, it is unlikely that bh protection is necessary here:
598 virtual devices do not generate EOI events.
599 However, it is possible, that they rely on bh protection
600 made by us here.
602 if (dev->flags&IFF_UP) {
603 if (netdev_nit)
604 dev_queue_xmit_nit(skb,dev);
605 if (dev->hard_start_xmit(skb, dev) == 0) {
606 end_bh_atomic();
608 #ifdef CONFIG_NET_PROFILE
609 NET_PROFILE_LEAVE(dev_queue_xmit);
610 end_bh_atomic();
611 #endif
613 return 0;
615 if (net_ratelimit())
616 printk(KERN_DEBUG "Virtual device %s asks to queue packet!\n", dev->name);
618 end_bh_atomic();
620 kfree_skb(skb);
622 #ifdef CONFIG_NET_PROFILE
623 NET_PROFILE_LEAVE(dev_queue_xmit);
624 end_bh_atomic();
625 #endif
627 return 0;
631 /*=======================================================================
632 Receiver rotutines
633 =======================================================================*/
635 int netdev_dropping = 0;
636 int netdev_max_backlog = 300;
637 atomic_t netdev_rx_dropped;
638 #ifdef CONFIG_CPU_IS_SLOW
639 int net_cpu_congestion;
640 #endif
642 #ifdef CONFIG_NET_HW_FLOWCONTROL
643 int netdev_throttle_events;
644 static unsigned long netdev_fc_mask = 1;
645 unsigned long netdev_fc_xoff = 0;
647 static struct
649 void (*stimul)(struct device *);
650 struct device *dev;
651 } netdev_fc_slots[32];
653 int netdev_register_fc(struct device *dev, void (*stimul)(struct device *dev))
655 int bit = 0;
656 unsigned long flags;
658 save_flags(flags);
659 cli();
660 if (netdev_fc_mask != ~0UL) {
661 bit = ffz(netdev_fc_mask);
662 netdev_fc_slots[bit].stimul = stimul;
663 netdev_fc_slots[bit].dev = dev;
664 set_bit(bit, &netdev_fc_mask);
665 clear_bit(bit, &netdev_fc_xoff);
667 restore_flags(flags);
668 return bit;
671 void netdev_unregister_fc(int bit)
673 unsigned long flags;
675 save_flags(flags);
676 cli();
677 if (bit > 0) {
678 netdev_fc_slots[bit].stimul = NULL;
679 netdev_fc_slots[bit].dev = NULL;
680 clear_bit(bit, &netdev_fc_mask);
681 clear_bit(bit, &netdev_fc_xoff);
683 restore_flags(flags);
686 static void netdev_wakeup(void)
688 unsigned long xoff;
690 cli();
691 xoff = netdev_fc_xoff;
692 netdev_fc_xoff = 0;
693 netdev_dropping = 0;
694 netdev_throttle_events++;
695 while (xoff) {
696 int i = ffz(~xoff);
697 xoff &= ~(1<<i);
698 netdev_fc_slots[i].stimul(netdev_fc_slots[i].dev);
700 sti();
702 #endif
704 static void dev_clear_backlog(struct device *dev)
706 struct sk_buff *prev, *curr;
710 * Let now clear backlog queue. -AS
712 * We are competing here both with netif_rx() and net_bh().
713 * We don't want either of those to mess with skb ptrs
714 * while we work on them, thus cli()/sti().
716 * It looks better to use net_bh trick, at least
717 * to be sure, that we keep interrupt latency really low. --ANK (980727)
720 if (backlog.qlen) {
721 start_bh_atomic();
722 curr = backlog.next;
723 while ( curr != (struct sk_buff *)(&backlog) ) {
724 unsigned long flags;
725 curr=curr->next;
726 if ( curr->prev->dev == dev ) {
727 prev = curr->prev;
728 spin_lock_irqsave(&skb_queue_lock, flags);
729 __skb_unlink(prev, &backlog);
730 spin_unlock_irqrestore(&skb_queue_lock, flags);
731 kfree_skb(prev);
734 end_bh_atomic();
735 #ifdef CONFIG_NET_HW_FLOWCONTROL
736 if (netdev_dropping)
737 netdev_wakeup();
738 #else
739 netdev_dropping = 0;
740 #endif
745 * Receive a packet from a device driver and queue it for the upper
746 * (protocol) levels. It always succeeds.
749 void netif_rx(struct sk_buff *skb)
751 #ifndef CONFIG_CPU_IS_SLOW
752 if(skb->stamp.tv_sec==0)
753 get_fast_time(&skb->stamp);
754 #else
755 skb->stamp = xtime;
756 #endif
758 /* The code is rearranged so that the path is the most
759 short when CPU is congested, but is still operating.
762 if (backlog.qlen <= netdev_max_backlog) {
763 if (backlog.qlen) {
764 if (netdev_dropping == 0) {
765 skb_queue_tail(&backlog,skb);
766 mark_bh(NET_BH);
767 return;
769 atomic_inc(&netdev_rx_dropped);
770 kfree_skb(skb);
771 return;
773 #ifdef CONFIG_NET_HW_FLOWCONTROL
774 if (netdev_dropping)
775 netdev_wakeup();
776 #else
777 netdev_dropping = 0;
778 #endif
779 skb_queue_tail(&backlog,skb);
780 mark_bh(NET_BH);
781 return;
783 netdev_dropping = 1;
784 atomic_inc(&netdev_rx_dropped);
785 kfree_skb(skb);
788 #ifdef CONFIG_BRIDGE
789 static inline void handle_bridge(struct sk_buff *skb, unsigned short type)
791 if (br_stats.flags & BR_UP && br_protocol_ok(ntohs(type)))
794 * We pass the bridge a complete frame. This means
795 * recovering the MAC header first.
798 int offset;
800 skb=skb_clone(skb, GFP_ATOMIC);
801 if(skb==NULL)
802 return;
804 offset=skb->data-skb->mac.raw;
805 skb_push(skb,offset); /* Put header back on for bridge */
807 if(br_receive_frame(skb))
808 return;
809 kfree_skb(skb);
811 return;
813 #endif
817 * When we are called the queue is ready to grab, the interrupts are
818 * on and hardware can interrupt and queue to the receive queue as we
819 * run with no problems.
820 * This is run as a bottom half after an interrupt handler that does
821 * mark_bh(NET_BH);
824 void net_bh(void)
826 struct packet_type *ptype;
827 struct packet_type *pt_prev;
828 unsigned short type;
829 unsigned long start_time = jiffies;
830 #ifdef CONFIG_CPU_IS_SLOW
831 static unsigned long start_busy = 0;
832 static unsigned long ave_busy = 0;
834 if (start_busy == 0)
835 start_busy = start_time;
836 net_cpu_congestion = ave_busy>>8;
837 #endif
839 NET_PROFILE_ENTER(net_bh);
841 * Can we send anything now? We want to clear the
842 * decks for any more sends that get done as we
843 * process the input. This also minimises the
844 * latency on a transmit interrupt bh.
847 if (qdisc_head.forw != &qdisc_head)
848 qdisc_run_queues();
851 * Any data left to process. This may occur because a
852 * mark_bh() is done after we empty the queue including
853 * that from the device which does a mark_bh() just after
857 * While the queue is not empty..
859 * Note that the queue never shrinks due to
860 * an interrupt, so we can do this test without
861 * disabling interrupts.
864 while (!skb_queue_empty(&backlog))
866 struct sk_buff * skb;
868 /* Give chance to other bottom halves to run */
869 if (jiffies - start_time > 1)
870 goto net_bh_break;
873 * We have a packet. Therefore the queue has shrunk
875 skb = skb_dequeue(&backlog);
877 #ifdef CONFIG_CPU_IS_SLOW
878 if (ave_busy > 128*16) {
879 kfree_skb(skb);
880 while ((skb = skb_dequeue(&backlog)) != NULL)
881 kfree_skb(skb);
882 break;
884 #endif
887 #if 0
888 NET_PROFILE_SKB_PASSED(skb, net_bh_skb);
889 #endif
890 #ifdef CONFIG_NET_FASTROUTE
891 if (skb->pkt_type == PACKET_FASTROUTE) {
892 dev_queue_xmit(skb);
893 continue;
895 #endif
898 * Fetch the packet protocol ID.
901 type = skb->protocol;
904 #ifdef CONFIG_BRIDGE
906 * If we are bridging then pass the frame up to the
907 * bridging code (if this protocol is to be bridged).
908 * If it is bridged then move on
910 handle_bridge(skb, type);
911 #endif
914 * Bump the pointer to the next structure.
916 * On entry to the protocol layer. skb->data and
917 * skb->nh.raw point to the MAC and encapsulated data
920 /* XXX until we figure out every place to modify.. */
921 skb->h.raw = skb->nh.raw = skb->data;
923 if (skb->mac.raw < skb->head || skb->mac.raw > skb->data) {
924 printk(KERN_CRIT "%s: wrong mac.raw ptr, proto=%04x\n", skb->dev->name, skb->protocol);
925 kfree_skb(skb);
926 continue;
930 * We got a packet ID. Now loop over the "known protocols"
931 * list. There are two lists. The ptype_all list of taps (normally empty)
932 * and the main protocol list which is hashed perfectly for normal protocols.
935 pt_prev = NULL;
936 for (ptype = ptype_all; ptype!=NULL; ptype=ptype->next)
938 if (!ptype->dev || ptype->dev == skb->dev) {
939 if(pt_prev)
941 struct sk_buff *skb2=skb_clone(skb, GFP_ATOMIC);
942 if(skb2)
943 pt_prev->func(skb2,skb->dev, pt_prev);
945 pt_prev=ptype;
949 for (ptype = ptype_base[ntohs(type)&15]; ptype != NULL; ptype = ptype->next)
951 if (ptype->type == type && (!ptype->dev || ptype->dev==skb->dev))
954 * We already have a match queued. Deliver
955 * to it and then remember the new match
957 if(pt_prev)
959 struct sk_buff *skb2;
961 skb2=skb_clone(skb, GFP_ATOMIC);
964 * Kick the protocol handler. This should be fast
965 * and efficient code.
968 if(skb2)
969 pt_prev->func(skb2, skb->dev, pt_prev);
971 /* Remember the current last to do */
972 pt_prev=ptype;
974 } /* End of protocol list loop */
977 * Is there a last item to send to ?
980 if(pt_prev)
981 pt_prev->func(skb, skb->dev, pt_prev);
983 * Has an unknown packet has been received ?
986 else {
987 kfree_skb(skb);
989 } /* End of queue loop */
992 * We have emptied the queue
996 * One last output flush.
999 if (qdisc_head.forw != &qdisc_head)
1000 qdisc_run_queues();
1002 #ifdef CONFIG_CPU_IS_SLOW
1003 if (1) {
1004 unsigned long start_idle = jiffies;
1005 ave_busy += ((start_idle - start_busy)<<3) - (ave_busy>>4);
1006 start_busy = 0;
1008 #endif
1009 #ifdef CONFIG_NET_HW_FLOWCONTROL
1010 if (netdev_dropping)
1011 netdev_wakeup();
1012 #else
1013 netdev_dropping = 0;
1014 #endif
1015 NET_PROFILE_LEAVE(net_bh);
1016 return;
1018 net_bh_break:
1019 mark_bh(NET_BH);
1020 NET_PROFILE_LEAVE(net_bh);
1021 return;
1024 /* Protocol dependent address dumping routines */
1026 static gifconf_func_t * gifconf_list [NPROTO];
1028 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1030 if (family>=NPROTO)
1031 return -EINVAL;
1032 gifconf_list[family] = gifconf;
1033 return 0;
1038 * Map an interface index to its name (SIOCGIFNAME)
1042 * This call is useful, but I'd remove it too.
1044 * The reason is purely aestetical, it is the only call
1045 * from SIOC* family using struct ifreq in reversed manner.
1046 * Besides that, it is pretty silly to put "drawing" facility
1047 * to kernel, it is useful only to print ifindices
1048 * in readable form, is not it? --ANK
1050 * We need this ioctl for efficient implementation of the
1051 * if_indextoname() function required by the IPv6 API. Without
1052 * it, we would have to search all the interfaces to find a
1053 * match. --pb
1056 static int dev_ifname(struct ifreq *arg)
1058 struct device *dev;
1059 struct ifreq ifr;
1060 int err;
1063 * Fetch the caller's info block.
1066 err = copy_from_user(&ifr, arg, sizeof(struct ifreq));
1067 if (err)
1068 return -EFAULT;
1070 dev = dev_get_by_index(ifr.ifr_ifindex);
1071 if (!dev)
1072 return -ENODEV;
1074 strcpy(ifr.ifr_name, dev->name);
1076 err = copy_to_user(arg, &ifr, sizeof(struct ifreq));
1077 return (err)?-EFAULT:0;
1081 * Perform a SIOCGIFCONF call. This structure will change
1082 * size eventually, and there is nothing I can do about it.
1083 * Thus we will need a 'compatibility mode'.
1086 static int dev_ifconf(char *arg)
1088 struct ifconf ifc;
1089 struct device *dev;
1090 char *pos;
1091 int len;
1092 int total;
1093 int i;
1096 * Fetch the caller's info block.
1099 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
1100 return -EFAULT;
1102 pos = ifc.ifc_buf;
1103 len = ifc.ifc_len;
1106 * Loop over the interfaces, and write an info block for each.
1109 total = 0;
1110 for (dev = dev_base; dev != NULL; dev = dev->next) {
1111 for (i=0; i<NPROTO; i++) {
1112 if (gifconf_list[i]) {
1113 int done;
1114 if (pos==NULL) {
1115 done = gifconf_list[i](dev, NULL, 0);
1116 } else {
1117 done = gifconf_list[i](dev, pos+total, len-total);
1119 if (done<0)
1120 return -EFAULT;
1121 total += done;
1127 * All done. Write the updated control block back to the caller.
1129 ifc.ifc_len = total;
1131 if (copy_to_user(arg, &ifc, sizeof(struct ifconf)))
1132 return -EFAULT;
1135 * Both BSD and Solaris return 0 here, so we do too.
1137 return 0;
1141 * This is invoked by the /proc filesystem handler to display a device
1142 * in detail.
1145 #ifdef CONFIG_PROC_FS
1146 static int sprintf_stats(char *buffer, struct device *dev)
1148 struct net_device_stats *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
1149 int size;
1151 if (stats)
1152 size = sprintf(buffer, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu %8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
1153 dev->name,
1154 stats->rx_bytes,
1155 stats->rx_packets, stats->rx_errors,
1156 stats->rx_dropped + stats->rx_missed_errors,
1157 stats->rx_fifo_errors,
1158 stats->rx_length_errors + stats->rx_over_errors
1159 + stats->rx_crc_errors + stats->rx_frame_errors,
1160 stats->rx_compressed, stats->multicast,
1161 stats->tx_bytes,
1162 stats->tx_packets, stats->tx_errors, stats->tx_dropped,
1163 stats->tx_fifo_errors, stats->collisions,
1164 stats->tx_carrier_errors + stats->tx_aborted_errors
1165 + stats->tx_window_errors + stats->tx_heartbeat_errors,
1166 stats->tx_compressed);
1167 else
1168 size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
1170 return size;
1174 * Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
1175 * to create /proc/net/dev
1178 int dev_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1180 int len=0;
1181 off_t begin=0;
1182 off_t pos=0;
1183 int size;
1185 struct device *dev;
1188 size = sprintf(buffer,
1189 "Inter-| Receive | Transmit\n"
1190 " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n");
1192 pos+=size;
1193 len+=size;
1196 for (dev = dev_base; dev != NULL; dev = dev->next)
1198 size = sprintf_stats(buffer+len, dev);
1199 len+=size;
1200 pos=begin+len;
1202 if(pos<offset)
1204 len=0;
1205 begin=pos;
1207 if(pos>offset+length)
1208 break;
1211 *start=buffer+(offset-begin); /* Start of wanted data */
1212 len-=(offset-begin); /* Start slop */
1213 if(len>length)
1214 len=length; /* Ending slop */
1215 return len;
1218 static int dev_proc_stats(char *buffer, char **start, off_t offset,
1219 int length, int *eof, void *data)
1221 int len;
1223 len = sprintf(buffer, "%08x %08x %08x %08x %08x\n",
1224 atomic_read(&netdev_rx_dropped),
1225 #ifdef CONFIG_NET_HW_FLOWCONTROL
1226 netdev_throttle_events,
1227 #else
1229 #endif
1230 #ifdef CONFIG_NET_FASTROUTE
1231 dev_fastroute_stat.hits,
1232 dev_fastroute_stat.succeed,
1233 dev_fastroute_stat.deferred
1234 #else
1235 0, 0, 0
1236 #endif
1239 len -= offset;
1241 if (len > length)
1242 len = length;
1243 if(len < 0)
1244 len = 0;
1246 *start = buffer + offset;
1247 *eof = 1;
1249 return len;
1252 #endif /* CONFIG_PROC_FS */
1255 #ifdef CONFIG_NET_RADIO
1256 #ifdef CONFIG_PROC_FS
1259 * Print one entry of /proc/net/wireless
1260 * This is a clone of /proc/net/dev (just above)
1262 static int sprintf_wireless_stats(char *buffer, struct device *dev)
1264 /* Get stats from the driver */
1265 struct iw_statistics *stats = (dev->get_wireless_stats ?
1266 dev->get_wireless_stats(dev) :
1267 (struct iw_statistics *) NULL);
1268 int size;
1270 if(stats != (struct iw_statistics *) NULL)
1271 size = sprintf(buffer,
1272 "%6s: %02x %3d%c %3d%c %3d%c %5d %5d %5d\n",
1273 dev->name,
1274 stats->status,
1275 stats->qual.qual,
1276 stats->qual.updated & 1 ? '.' : ' ',
1277 stats->qual.level,
1278 stats->qual.updated & 2 ? '.' : ' ',
1279 stats->qual.noise,
1280 stats->qual.updated & 3 ? '.' : ' ',
1281 stats->discard.nwid,
1282 stats->discard.code,
1283 stats->discard.misc);
1284 else
1285 size = 0;
1287 return size;
1291 * Print info for /proc/net/wireless (print all entries)
1292 * This is a clone of /proc/net/dev (just above)
1294 int dev_get_wireless_info(char * buffer, char **start, off_t offset,
1295 int length, int dummy)
1297 int len = 0;
1298 off_t begin = 0;
1299 off_t pos = 0;
1300 int size;
1302 struct device * dev;
1304 size = sprintf(buffer,
1305 "Inter-|sta| Quality | Discarded packets\n"
1306 " face |tus|link level noise| nwid crypt misc\n");
1308 pos+=size;
1309 len+=size;
1311 for(dev = dev_base; dev != NULL; dev = dev->next)
1313 size = sprintf_wireless_stats(buffer+len, dev);
1314 len+=size;
1315 pos=begin+len;
1317 if(pos < offset)
1319 len=0;
1320 begin=pos;
1322 if(pos > offset + length)
1323 break;
1326 *start = buffer + (offset - begin); /* Start of wanted data */
1327 len -= (offset - begin); /* Start slop */
1328 if(len > length)
1329 len = length; /* Ending slop */
1331 return len;
1333 #endif /* CONFIG_PROC_FS */
1334 #endif /* CONFIG_NET_RADIO */
1336 void dev_set_promiscuity(struct device *dev, int inc)
1338 unsigned short old_flags = dev->flags;
1340 dev->flags |= IFF_PROMISC;
1341 if ((dev->promiscuity += inc) == 0)
1342 dev->flags &= ~IFF_PROMISC;
1343 if (dev->flags^old_flags) {
1344 #ifdef CONFIG_NET_FASTROUTE
1345 if (dev->flags&IFF_PROMISC) {
1346 netdev_fastroute_obstacles++;
1347 dev_clear_fastroute(dev);
1348 } else
1349 netdev_fastroute_obstacles--;
1350 #endif
1351 dev_mc_upload(dev);
1352 printk(KERN_INFO "device %s %s promiscuous mode\n",
1353 dev->name, (dev->flags&IFF_PROMISC) ? "entered" : "left");
1357 void dev_set_allmulti(struct device *dev, int inc)
1359 unsigned short old_flags = dev->flags;
1361 dev->flags |= IFF_ALLMULTI;
1362 if ((dev->allmulti += inc) == 0)
1363 dev->flags &= ~IFF_ALLMULTI;
1364 if (dev->flags^old_flags)
1365 dev_mc_upload(dev);
1368 int dev_change_flags(struct device *dev, unsigned flags)
1370 int ret;
1371 int old_flags = dev->flags;
1374 * Set the flags on our device.
1377 dev->flags = (flags & (IFF_DEBUG|IFF_NOTRAILERS|IFF_RUNNING|IFF_NOARP|
1378 IFF_SLAVE|IFF_MASTER|IFF_DYNAMIC|
1379 IFF_MULTICAST|IFF_PORTSEL|IFF_AUTOMEDIA)) |
1380 (dev->flags & (IFF_UP|IFF_VOLATILE|IFF_PROMISC|IFF_ALLMULTI));
1383 * Load in the correct multicast list now the flags have changed.
1386 dev_mc_upload(dev);
1389 * Have we downed the interface. We handle IFF_UP ourselves
1390 * according to user attempts to set it, rather than blindly
1391 * setting it.
1394 ret = 0;
1395 if ((old_flags^flags)&IFF_UP) /* Bit is different ? */
1397 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
1399 if (ret == 0)
1400 dev_mc_upload(dev);
1403 if (dev->flags&IFF_UP &&
1404 ((old_flags^dev->flags)&~(IFF_UP|IFF_RUNNING|IFF_PROMISC|IFF_ALLMULTI|IFF_VOLATILE)))
1405 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
1407 if ((flags^dev->gflags)&IFF_PROMISC) {
1408 int inc = (flags&IFF_PROMISC) ? +1 : -1;
1409 dev->gflags ^= IFF_PROMISC;
1410 dev_set_promiscuity(dev, inc);
1413 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
1414 is important. Some (broken) drivers set IFF_PROMISC, when
1415 IFF_ALLMULTI is requested not asking us and not reporting.
1417 if ((flags^dev->gflags)&IFF_ALLMULTI) {
1418 int inc = (flags&IFF_ALLMULTI) ? +1 : -1;
1419 dev->gflags ^= IFF_ALLMULTI;
1420 dev_set_allmulti(dev, inc);
1423 return ret;
1427 * Perform the SIOCxIFxxx calls.
1430 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
1432 struct device *dev;
1433 int err;
1435 if ((dev = dev_get(ifr->ifr_name)) == NULL)
1436 return -ENODEV;
1438 switch(cmd)
1440 case SIOCGIFFLAGS: /* Get interface flags */
1441 ifr->ifr_flags = (dev->flags&~(IFF_PROMISC|IFF_ALLMULTI))
1442 |(dev->gflags&(IFF_PROMISC|IFF_ALLMULTI));
1443 return 0;
1445 case SIOCSIFFLAGS: /* Set interface flags */
1446 return dev_change_flags(dev, ifr->ifr_flags);
1448 case SIOCGIFMETRIC: /* Get the metric on the interface (currently unused) */
1449 ifr->ifr_metric = 0;
1450 return 0;
1452 case SIOCSIFMETRIC: /* Set the metric on the interface (currently unused) */
1453 return -EOPNOTSUPP;
1455 case SIOCGIFMTU: /* Get the MTU of a device */
1456 ifr->ifr_mtu = dev->mtu;
1457 return 0;
1459 case SIOCSIFMTU: /* Set the MTU of a device */
1460 if (ifr->ifr_mtu == dev->mtu)
1461 return 0;
1464 * MTU must be positive.
1467 if (ifr->ifr_mtu<0)
1468 return -EINVAL;
1470 if (dev->change_mtu)
1471 err = dev->change_mtu(dev, ifr->ifr_mtu);
1472 else {
1473 dev->mtu = ifr->ifr_mtu;
1474 err = 0;
1476 if (!err && dev->flags&IFF_UP)
1477 notifier_call_chain(&netdev_chain, NETDEV_CHANGEMTU, dev);
1478 return err;
1480 case SIOCGIFHWADDR:
1481 memcpy(ifr->ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1482 ifr->ifr_hwaddr.sa_family=dev->type;
1483 return 0;
1485 case SIOCSIFHWADDR:
1486 if(dev->set_mac_address==NULL)
1487 return -EOPNOTSUPP;
1488 if(ifr->ifr_hwaddr.sa_family!=dev->type)
1489 return -EINVAL;
1490 err=dev->set_mac_address(dev,&ifr->ifr_hwaddr);
1491 if (!err)
1492 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
1493 return err;
1495 case SIOCSIFHWBROADCAST:
1496 if(ifr->ifr_hwaddr.sa_family!=dev->type)
1497 return -EINVAL;
1498 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data, MAX_ADDR_LEN);
1499 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
1500 return 0;
1502 case SIOCGIFMAP:
1503 ifr->ifr_map.mem_start=dev->mem_start;
1504 ifr->ifr_map.mem_end=dev->mem_end;
1505 ifr->ifr_map.base_addr=dev->base_addr;
1506 ifr->ifr_map.irq=dev->irq;
1507 ifr->ifr_map.dma=dev->dma;
1508 ifr->ifr_map.port=dev->if_port;
1509 return 0;
1511 case SIOCSIFMAP:
1512 if (dev->set_config)
1513 return dev->set_config(dev,&ifr->ifr_map);
1514 return -EOPNOTSUPP;
1516 case SIOCADDMULTI:
1517 if(dev->set_multicast_list==NULL ||
1518 ifr->ifr_hwaddr.sa_family!=AF_UNSPEC)
1519 return -EINVAL;
1520 dev_mc_add(dev,ifr->ifr_hwaddr.sa_data, dev->addr_len, 1);
1521 return 0;
1523 case SIOCDELMULTI:
1524 if(dev->set_multicast_list==NULL ||
1525 ifr->ifr_hwaddr.sa_family!=AF_UNSPEC)
1526 return -EINVAL;
1527 dev_mc_delete(dev,ifr->ifr_hwaddr.sa_data,dev->addr_len, 1);
1528 return 0;
1530 case SIOCGIFINDEX:
1531 ifr->ifr_ifindex = dev->ifindex;
1532 return 0;
1534 case SIOCGIFTXQLEN:
1535 ifr->ifr_qlen = dev->tx_queue_len;
1536 return 0;
1538 case SIOCSIFTXQLEN:
1539 /* Why <2? 0 and 1 are valid values. --ANK (980807) */
1540 if(/*ifr->ifr_qlen<2 ||*/ ifr->ifr_qlen>1024)
1541 return -EINVAL;
1542 dev->tx_queue_len = ifr->ifr_qlen;
1543 return 0;
1545 case SIOCSIFNAME:
1546 if (dev->flags&IFF_UP)
1547 return -EBUSY;
1548 if (dev_get(ifr->ifr_newname))
1549 return -EEXIST;
1550 memcpy(dev->name, ifr->ifr_newname, IFNAMSIZ);
1551 dev->name[IFNAMSIZ-1] = 0;
1552 notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
1553 return 0;
1556 * Unknown or private ioctl
1559 default:
1560 if(cmd >= SIOCDEVPRIVATE &&
1561 cmd <= SIOCDEVPRIVATE + 15) {
1562 if (dev->do_ioctl)
1563 return dev->do_ioctl(dev, ifr, cmd);
1564 return -EOPNOTSUPP;
1567 #ifdef CONFIG_NET_RADIO
1568 if(cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1569 if (dev->do_ioctl)
1570 return dev->do_ioctl(dev, ifr, cmd);
1571 return -EOPNOTSUPP;
1573 #endif /* CONFIG_NET_RADIO */
1576 return -EINVAL;
1581 * This function handles all "interface"-type I/O control requests. The actual
1582 * 'doing' part of this is dev_ifsioc above.
1585 int dev_ioctl(unsigned int cmd, void *arg)
1587 struct ifreq ifr;
1588 int ret;
1589 char *colon;
1591 /* One special case: SIOCGIFCONF takes ifconf argument
1592 and requires shared lock, because it sleeps writing
1593 to user space.
1596 if (cmd == SIOCGIFCONF) {
1597 rtnl_shlock();
1598 ret = dev_ifconf((char *) arg);
1599 rtnl_shunlock();
1600 return ret;
1602 if (cmd == SIOCGIFNAME) {
1603 return dev_ifname((struct ifreq *)arg);
1606 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
1607 return -EFAULT;
1609 ifr.ifr_name[IFNAMSIZ-1] = 0;
1611 colon = strchr(ifr.ifr_name, ':');
1612 if (colon)
1613 *colon = 0;
1616 * See which interface the caller is talking about.
1619 switch(cmd)
1622 * These ioctl calls:
1623 * - can be done by all.
1624 * - atomic and do not require locking.
1625 * - return a value
1628 case SIOCGIFFLAGS:
1629 case SIOCGIFMETRIC:
1630 case SIOCGIFMTU:
1631 case SIOCGIFHWADDR:
1632 case SIOCGIFSLAVE:
1633 case SIOCGIFMAP:
1634 case SIOCGIFINDEX:
1635 case SIOCGIFTXQLEN:
1636 dev_load(ifr.ifr_name);
1637 ret = dev_ifsioc(&ifr, cmd);
1638 if (!ret) {
1639 if (colon)
1640 *colon = ':';
1641 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1642 return -EFAULT;
1644 return ret;
1647 * These ioctl calls:
1648 * - require superuser power.
1649 * - require strict serialization.
1650 * - do not return a value
1653 case SIOCSIFFLAGS:
1654 case SIOCSIFMETRIC:
1655 case SIOCSIFMTU:
1656 case SIOCSIFMAP:
1657 case SIOCSIFHWADDR:
1658 case SIOCSIFSLAVE:
1659 case SIOCADDMULTI:
1660 case SIOCDELMULTI:
1661 case SIOCSIFHWBROADCAST:
1662 case SIOCSIFTXQLEN:
1663 case SIOCSIFNAME:
1664 if (!capable(CAP_NET_ADMIN))
1665 return -EPERM;
1666 dev_load(ifr.ifr_name);
1667 rtnl_lock();
1668 ret = dev_ifsioc(&ifr, cmd);
1669 rtnl_unlock();
1670 return ret;
1672 case SIOCGIFMEM:
1673 /* Get the per device memory space. We can add this but currently
1674 do not support it */
1675 case SIOCSIFMEM:
1676 /* Set the per device memory buffer space. Not applicable in our case */
1677 case SIOCSIFLINK:
1678 return -EINVAL;
1681 * Unknown or private ioctl.
1684 default:
1685 if (cmd >= SIOCDEVPRIVATE &&
1686 cmd <= SIOCDEVPRIVATE + 15) {
1687 dev_load(ifr.ifr_name);
1688 rtnl_lock();
1689 ret = dev_ifsioc(&ifr, cmd);
1690 rtnl_unlock();
1691 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1692 return -EFAULT;
1693 return ret;
1695 #ifdef CONFIG_NET_RADIO
1696 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1697 dev_load(ifr.ifr_name);
1698 if (IW_IS_SET(cmd)) {
1699 if (!suser())
1700 return -EPERM;
1701 rtnl_lock();
1703 ret = dev_ifsioc(&ifr, cmd);
1704 if (IW_IS_SET(cmd))
1705 rtnl_unlock();
1706 if (!ret && IW_IS_GET(cmd) &&
1707 copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1708 return -EFAULT;
1709 return ret;
1711 #endif /* CONFIG_NET_RADIO */
1712 return -EINVAL;
1716 int dev_new_index(void)
1718 static int ifindex;
1719 for (;;) {
1720 if (++ifindex <= 0)
1721 ifindex=1;
1722 if (dev_get_by_index(ifindex) == NULL)
1723 return ifindex;
1727 static int dev_boot_phase = 1;
1730 int register_netdevice(struct device *dev)
1732 struct device *d, **dp;
1734 if (dev_boot_phase) {
1735 /* This is NOT bug, but I am not sure, that all the
1736 devices, initialized before netdev module is started
1737 are sane.
1739 Now they are chained to device boot list
1740 and probed later. If a module is initialized
1741 before netdev, but assumes that dev->init
1742 is really called by register_netdev(), it will fail.
1744 So that this message should be printed for a while.
1746 printk(KERN_INFO "early initialization of device %s is deferred\n", dev->name);
1748 /* Check for existence, and append to tail of chain */
1749 for (dp=&dev_base; (d=*dp) != NULL; dp=&d->next) {
1750 if (d == dev || strcmp(d->name, dev->name) == 0)
1751 return -EEXIST;
1753 dev->next = NULL;
1754 *dp = dev;
1755 return 0;
1758 dev->iflink = -1;
1760 /* Init, if this function is available */
1761 if (dev->init && dev->init(dev) != 0)
1762 return -EIO;
1764 /* Check for existence, and append to tail of chain */
1765 for (dp=&dev_base; (d=*dp) != NULL; dp=&d->next) {
1766 if (d == dev || strcmp(d->name, dev->name) == 0)
1767 return -EEXIST;
1769 dev->next = NULL;
1770 dev_init_scheduler(dev);
1771 dev->ifindex = dev_new_index();
1772 if (dev->iflink == -1)
1773 dev->iflink = dev->ifindex;
1774 *dp = dev;
1776 /* Notify protocols, that a new device appeared. */
1777 notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
1779 return 0;
1782 int unregister_netdevice(struct device *dev)
1784 struct device *d, **dp;
1786 if (dev_boot_phase == 0) {
1787 /* If device is running, close it.
1788 It is very bad idea, really we should
1789 complain loudly here, but random hackery
1790 in linux/drivers/net likes it.
1792 if (dev->flags & IFF_UP)
1793 dev_close(dev);
1795 #ifdef CONFIG_NET_FASTROUTE
1796 dev_clear_fastroute(dev);
1797 #endif
1799 /* Shutdown queueing discipline. */
1800 dev_shutdown(dev);
1802 /* Notify protocols, that we are about to destroy
1803 this device. They should clean all the things.
1805 notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
1808 * Flush the multicast chain
1810 dev_mc_discard(dev);
1812 /* To avoid pointers looking to nowhere,
1813 we wait for end of critical section */
1814 dev_lock_wait();
1817 /* And unlink it from device chain. */
1818 for (dp = &dev_base; (d=*dp) != NULL; dp=&d->next) {
1819 if (d == dev) {
1820 *dp = d->next;
1821 d->next = NULL;
1822 if (dev->destructor)
1823 dev->destructor(dev);
1824 return 0;
1827 return -ENODEV;
1832 * Initialize the DEV module. At boot time this walks the device list and
1833 * unhooks any devices that fail to initialise (normally hardware not
1834 * present) and leaves us with a valid list of present and active devices.
1837 extern int lance_init(void);
1838 extern int bpq_init(void);
1839 extern int scc_init(void);
1840 extern void sdla_setup(void);
1841 extern void dlci_setup(void);
1842 extern int dmascc_init(void);
1843 extern int sm_init(void);
1845 extern int baycom_ser_fdx_init(void);
1846 extern int baycom_ser_hdx_init(void);
1847 extern int baycom_par_init(void);
1849 extern int lapbeth_init(void);
1850 extern void arcnet_init(void);
1851 extern void ip_auto_config(void);
1852 #ifdef CONFIG_8xx
1853 extern int cpm_enet_init(void);
1854 #endif /* CONFIG_8xx */
1856 #ifdef CONFIG_PROC_FS
1857 static struct proc_dir_entry proc_net_dev = {
1858 PROC_NET_DEV, 3, "dev",
1859 S_IFREG | S_IRUGO, 1, 0, 0,
1860 0, &proc_net_inode_operations,
1861 dev_get_info
1863 #endif
1865 #ifdef CONFIG_NET_RADIO
1866 #ifdef CONFIG_PROC_FS
1867 static struct proc_dir_entry proc_net_wireless = {
1868 PROC_NET_WIRELESS, 8, "wireless",
1869 S_IFREG | S_IRUGO, 1, 0, 0,
1870 0, &proc_net_inode_operations,
1871 dev_get_wireless_info
1873 #endif /* CONFIG_PROC_FS */
1874 #endif /* CONFIG_NET_RADIO */
1876 __initfunc(int net_dev_init(void))
1878 struct device *dev, **dp;
1880 #ifdef CONFIG_NET_SCHED
1881 pktsched_init();
1882 #endif
1885 * Initialise the packet receive queue.
1888 skb_queue_head_init(&backlog);
1891 * The bridge has to be up before the devices
1894 #ifdef CONFIG_BRIDGE
1895 br_init();
1896 #endif
1899 * This is Very Ugly(tm).
1901 * Some devices want to be initialized early..
1904 #if defined(CONFIG_SCC)
1905 scc_init();
1906 #endif
1907 #if defined(CONFIG_DMASCC)
1908 dmascc_init();
1909 #endif
1910 #if defined(CONFIG_BPQETHER)
1911 bpq_init();
1912 #endif
1913 #if defined(CONFIG_DLCI)
1914 dlci_setup();
1915 #endif
1916 #if defined(CONFIG_SDLA)
1917 sdla_setup();
1918 #endif
1919 #if defined(CONFIG_BAYCOM_PAR)
1920 baycom_par_init();
1921 #endif
1922 #if defined(CONFIG_BAYCOM_SER_FDX)
1923 baycom_ser_fdx_init();
1924 #endif
1925 #if defined(CONFIG_BAYCOM_SER_HDX)
1926 baycom_ser_hdx_init();
1927 #endif
1928 #if defined(CONFIG_SOUNDMODEM)
1929 sm_init();
1930 #endif
1931 #if defined(CONFIG_LAPBETHER)
1932 lapbeth_init();
1933 #endif
1934 #if defined(CONFIG_PLIP)
1935 plip_init();
1936 #endif
1937 #if defined(CONFIG_ARCNET)
1938 arcnet_init();
1939 #endif
1940 #if defined(CONFIG_8xx)
1941 cpm_enet_init();
1942 #endif
1944 * SLHC if present needs attaching so other people see it
1945 * even if not opened.
1948 #ifdef CONFIG_INET
1949 #if (defined(CONFIG_SLIP) && defined(CONFIG_SLIP_COMPRESSED)) \
1950 || defined(CONFIG_PPP) \
1951 || (defined(CONFIG_ISDN) && defined(CONFIG_ISDN_PPP))
1952 slhc_install();
1953 #endif
1954 #endif
1956 #ifdef CONFIG_NET_PROFILE
1957 net_profile_init();
1958 NET_PROFILE_REGISTER(dev_queue_xmit);
1959 NET_PROFILE_REGISTER(net_bh);
1960 #if 0
1961 NET_PROFILE_REGISTER(net_bh_skb);
1962 #endif
1963 #endif
1965 * Add the devices.
1966 * If the call to dev->init fails, the dev is removed
1967 * from the chain disconnecting the device until the
1968 * next reboot.
1971 dp = &dev_base;
1972 while ((dev = *dp) != NULL)
1974 dev->iflink = -1;
1975 if (dev->init && dev->init(dev))
1978 * It failed to come up. Unhook it.
1980 *dp = dev->next;
1982 else
1984 dp = &dev->next;
1985 dev->ifindex = dev_new_index();
1986 if (dev->iflink == -1)
1987 dev->iflink = dev->ifindex;
1988 dev_init_scheduler(dev);
1992 #ifdef CONFIG_PROC_FS
1993 proc_net_register(&proc_net_dev);
1995 struct proc_dir_entry *ent = create_proc_entry("net/dev_stat", 0, 0);
1996 ent->read_proc = dev_proc_stats;
1998 #endif
2000 #ifdef CONFIG_NET_RADIO
2001 #ifdef CONFIG_PROC_FS
2002 proc_net_register(&proc_net_wireless);
2003 #endif /* CONFIG_PROC_FS */
2004 #endif /* CONFIG_NET_RADIO */
2006 init_bh(NET_BH, net_bh);
2008 dev_boot_phase = 0;
2010 dev_mcast_init();
2012 #ifdef CONFIG_IP_PNP
2013 ip_auto_config();
2014 #endif
2016 return 0;