Import 2.3.10pre3
[davej-history.git] / net / core / dev.c
blobb9bd18343cc7636660dac24ae323d49a18c83012
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 static struct packet_type *ptype_base[16]; /* 16 way hashed list */
133 static struct packet_type *ptype_all = NULL; /* Taps */
134 static rwlock_t ptype_lock = RW_LOCK_UNLOCKED;
137 * Device list lock. Setting it provides that interface
138 * will not disappear unexpectedly while kernel sleeps.
141 atomic_t dev_lockct = ATOMIC_INIT(0);
144 * Our notifier list
147 static struct notifier_block *netdev_chain=NULL;
150 * Device drivers call our routines to queue packets here. We empty the
151 * queue in the bottom half handler.
154 static struct sk_buff_head backlog;
156 #ifdef CONFIG_NET_FASTROUTE
157 int netdev_fastroute;
158 int netdev_fastroute_obstacles;
159 struct net_fastroute_stats dev_fastroute_stat;
160 #endif
162 static void dev_clear_backlog(struct device *dev);
165 /******************************************************************************************
167 Protocol management and registration routines
169 *******************************************************************************************/
172 * For efficiency
175 int netdev_nit=0;
178 * Add a protocol ID to the list. Now that the input handler is
179 * smarter we can dispense with all the messy stuff that used to be
180 * here.
182 * BEWARE!!! Protocol handlers, mangling input packets,
183 * MUST BE last in hash buckets and checking protocol handlers
184 * MUST start from promiscous ptype_all chain in net_bh.
185 * It is true now, do not change it.
186 * Explantion follows: if protocol handler, mangling packet, will
187 * be the first on list, it is not able to sense, that packet
188 * is cloned and should be copied-on-write, so that it will
189 * change it and subsequent readers will get broken packet.
190 * --ANK (980803)
193 void dev_add_pack(struct packet_type *pt)
195 int hash;
196 #ifdef CONFIG_NET_FASTROUTE
197 /* Hack to detect packet socket */
198 if (pt->data) {
199 netdev_fastroute_obstacles++;
200 dev_clear_fastroute(pt->dev);
202 #endif
203 write_lock_bh(&ptype_lock);
204 if(pt->type==htons(ETH_P_ALL))
206 netdev_nit++;
207 pt->next=ptype_all;
208 ptype_all=pt;
210 else
212 hash=ntohs(pt->type)&15;
213 pt->next = ptype_base[hash];
214 ptype_base[hash] = pt;
216 write_unlock_bh(&ptype_lock);
221 * Remove a protocol ID from the list.
224 void dev_remove_pack(struct packet_type *pt)
226 struct packet_type **pt1;
227 if(pt->type==htons(ETH_P_ALL))
229 netdev_nit--;
230 pt1=&ptype_all;
232 else
233 pt1=&ptype_base[ntohs(pt->type)&15];
234 write_lock_bh(&ptype_lock);
235 for(; (*pt1)!=NULL; pt1=&((*pt1)->next))
237 if(pt==(*pt1))
239 *pt1=pt->next;
240 #ifdef CONFIG_NET_FASTROUTE
241 if (pt->data)
242 netdev_fastroute_obstacles--;
243 #endif
244 write_unlock_bh(&ptype_lock);
245 return;
248 write_unlock_bh(&ptype_lock);
249 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
252 /*****************************************************************************************
254 Device Interface Subroutines
256 ******************************************************************************************/
259 * Find an interface by name.
262 struct device *dev_get(const char *name)
264 struct device *dev;
266 read_lock(&dev_base_lock);
267 for (dev = dev_base; dev != NULL; dev = dev->next) {
268 if (strcmp(dev->name, name) == 0)
269 goto out;
271 out:
272 read_unlock(&dev_base_lock);
273 return dev;
276 struct device * dev_get_by_index(int ifindex)
278 struct device *dev;
280 read_lock(&dev_base_lock);
281 for (dev = dev_base; dev != NULL; dev = dev->next) {
282 if (dev->ifindex == ifindex)
283 goto out;
285 out:
286 read_unlock(&dev_base_lock);
287 return dev;
290 struct device *dev_getbyhwaddr(unsigned short type, char *ha)
292 struct device *dev;
294 read_lock(&dev_base_lock);
295 for (dev = dev_base; dev != NULL; dev = dev->next) {
296 if (dev->type == type &&
297 memcmp(dev->dev_addr, ha, dev->addr_len) == 0)
298 goto out;
300 out:
301 read_unlock(&dev_base_lock);
302 return dev;
306 * Passed a format string - eg "lt%d" it will try and find a suitable
307 * id. Not efficient for many devices, not called a lot..
310 int dev_alloc_name(struct device *dev, const char *name)
312 int i;
314 * If you need over 100 please also fix the algorithm...
316 for(i=0;i<100;i++)
318 sprintf(dev->name,name,i);
319 if(dev_get(dev->name)==NULL)
320 return i;
322 return -ENFILE; /* Over 100 of the things .. bail out! */
325 struct device *dev_alloc(const char *name, int *err)
327 struct device *dev=kmalloc(sizeof(struct device)+16, GFP_KERNEL);
328 if(dev==NULL)
330 *err=-ENOBUFS;
331 return NULL;
333 dev->name=(char *)(dev+1); /* Name string space */
334 *err=dev_alloc_name(dev,name);
335 if(*err<0)
337 kfree(dev);
338 return NULL;
340 return dev;
343 void netdev_state_change(struct device *dev)
345 if (dev->flags&IFF_UP)
346 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
351 * Find and possibly load an interface.
354 #ifdef CONFIG_KMOD
356 void dev_load(const char *name)
358 if(!dev_get(name) && capable(CAP_SYS_MODULE))
359 request_module(name);
362 #else
364 extern inline void dev_load(const char *unused){;}
366 #endif
368 static int default_rebuild_header(struct sk_buff *skb)
370 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n", skb->dev ? skb->dev->name : "NULL!!!");
371 kfree_skb(skb);
372 return 1;
376 * Prepare an interface for use.
379 int dev_open(struct device *dev)
381 int ret = 0;
384 * Is it already up?
387 if (dev->flags&IFF_UP)
388 return 0;
391 * Call device private open method
394 if (dev->open)
395 ret = dev->open(dev);
398 * If it went open OK then:
401 if (ret == 0)
404 * nil rebuild_header routine,
405 * that should be never called and used as just bug trap.
408 if (dev->rebuild_header == NULL)
409 dev->rebuild_header = default_rebuild_header;
412 * Set the flags.
414 dev->flags |= (IFF_UP | IFF_RUNNING);
417 * Initialize multicasting status
419 dev_mc_upload(dev);
422 * Wakeup transmit queue engine
424 dev_activate(dev);
427 * ... and announce new interface.
429 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
432 return(ret);
435 #ifdef CONFIG_NET_FASTROUTE
437 static __inline__ void dev_do_clear_fastroute(struct device *dev)
439 if (dev->accept_fastpath) {
440 int i;
442 for (i=0; i<=NETDEV_FASTROUTE_HMASK; i++)
443 dst_release_irqwait(xchg(dev->fastpath+i, NULL));
447 void dev_clear_fastroute(struct device *dev)
449 if (dev) {
450 dev_do_clear_fastroute(dev);
451 } else {
452 read_lock(&dev_base_lock);
453 for (dev = dev_base; dev; dev = dev->next)
454 dev_do_clear_fastroute(dev);
455 read_unlock(&dev_base_lock);
458 #endif
461 * Completely shutdown an interface.
464 int dev_close(struct device *dev)
466 if (!(dev->flags&IFF_UP))
467 return 0;
469 dev_deactivate(dev);
471 dev_lock_wait();
474 * Call the device specific close. This cannot fail.
475 * Only if device is UP
478 if (dev->stop)
479 dev->stop(dev);
481 if (dev->start)
482 printk("dev_close: bug %s still running\n", dev->name);
485 * Device is now down.
487 dev_clear_backlog(dev);
489 dev->flags&=~(IFF_UP|IFF_RUNNING);
490 #ifdef CONFIG_NET_FASTROUTE
491 dev_clear_fastroute(dev);
492 #endif
495 * Tell people we are going down
497 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
499 return(0);
504 * Device change register/unregister. These are not inline or static
505 * as we export them to the world.
508 int register_netdevice_notifier(struct notifier_block *nb)
510 return notifier_chain_register(&netdev_chain, nb);
513 int unregister_netdevice_notifier(struct notifier_block *nb)
515 return notifier_chain_unregister(&netdev_chain,nb);
519 * Support routine. Sends outgoing frames to any network
520 * taps currently in use.
523 void dev_queue_xmit_nit(struct sk_buff *skb, struct device *dev)
525 struct packet_type *ptype;
526 get_fast_time(&skb->stamp);
528 read_lock(&ptype_lock);
529 for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next)
531 /* Never send packets back to the socket
532 * they originated from - MvS (miquels@drinkel.ow.org)
534 if ((ptype->dev == dev || !ptype->dev) &&
535 ((struct sock *)ptype->data != skb->sk))
537 struct sk_buff *skb2;
538 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
539 break;
541 /* Code, following below is wrong.
543 The only reason, why it does work is that
544 ONLY packet sockets receive outgoing
545 packets. If such a packet will be (occasionally)
546 received by normal packet handler, which expects
547 that mac header is pulled...
550 /* More sensible variant. skb->nh should be correctly
551 set by sender, so that the second statement is
552 just protection against buggy protocols.
554 skb2->mac.raw = skb2->data;
556 if (skb2->nh.raw < skb2->data || skb2->nh.raw >= skb2->tail) {
557 if (net_ratelimit())
558 printk(KERN_DEBUG "protocol %04x is buggy, dev %s\n", skb2->protocol, dev->name);
559 skb2->nh.raw = skb2->data;
560 if (dev->hard_header)
561 skb2->nh.raw += dev->hard_header_len;
564 skb2->h.raw = skb2->nh.raw;
565 skb2->pkt_type = PACKET_OUTGOING;
566 ptype->func(skb2, skb->dev, ptype);
569 read_unlock(&ptype_lock);
573 * Fast path for loopback frames.
576 void dev_loopback_xmit(struct sk_buff *skb)
578 struct sk_buff *newskb=skb_clone(skb, GFP_ATOMIC);
579 if (newskb==NULL)
580 return;
582 newskb->mac.raw = newskb->data;
583 skb_pull(newskb, newskb->nh.raw - newskb->data);
584 newskb->pkt_type = PACKET_LOOPBACK;
585 newskb->ip_summed = CHECKSUM_UNNECESSARY;
586 if (newskb->dst==NULL)
587 printk(KERN_DEBUG "BUG: packet without dst looped back 1\n");
588 netif_rx(newskb);
591 int dev_queue_xmit(struct sk_buff *skb)
593 struct device *dev = skb->dev;
594 struct Qdisc *q;
596 /* Grab device queue */
597 spin_lock_bh(&dev->queue_lock);
598 q = dev->qdisc;
599 if (q->enqueue) {
600 q->enqueue(skb, q);
602 /* If the device is not busy, kick it.
603 * Otherwise or if queue is not empty after kick,
604 * add it to run list.
606 if (dev->tbusy || qdisc_restart(dev))
607 qdisc_run(dev->qdisc);
609 spin_unlock_bh(&dev->queue_lock);
610 return 0;
612 spin_unlock_bh(&dev->queue_lock);
614 /* The device has no queue. Common case for software devices:
615 loopback, all the sorts of tunnels...
617 Really, it is unlikely that xmit_lock protection is necessary here.
618 (f.e. loopback and IP tunnels are clean ignoring statistics counters.)
619 However, it is possible, that they rely on protection
620 made by us here.
622 Check this and shot the lock. It is not prone from deadlocks.
623 Either shot noqueue qdisc, it is even simpler 8)
625 if (dev->flags&IFF_UP) {
626 if (netdev_nit)
627 dev_queue_xmit_nit(skb,dev);
629 local_bh_disable();
630 if (dev->xmit_lock_owner != smp_processor_id()) {
631 spin_lock(&dev->xmit_lock);
632 dev->xmit_lock_owner = smp_processor_id();
633 if (dev->hard_start_xmit(skb, dev) == 0) {
634 dev->xmit_lock_owner = -1;
635 spin_unlock_bh(&dev->xmit_lock);
636 return 0;
638 dev->xmit_lock_owner = -1;
639 spin_unlock_bh(&dev->xmit_lock);
640 if (net_ratelimit())
641 printk(KERN_DEBUG "Virtual device %s asks to queue packet!\n", dev->name);
642 } else {
643 /* Recursion is detected! It is possible, unfortunately */
644 local_bh_enable();
645 if (net_ratelimit())
646 printk(KERN_DEBUG "Dead loop on virtual device %s, fix it urgently!\n", dev->name);
650 kfree_skb(skb);
651 return 0;
655 /*=======================================================================
656 Receiver rotutines
657 =======================================================================*/
659 int netdev_dropping = 0;
660 int netdev_max_backlog = 300;
661 atomic_t netdev_rx_dropped;
663 #ifdef CONFIG_NET_HW_FLOWCONTROL
664 int netdev_throttle_events;
665 static unsigned long netdev_fc_mask = 1;
666 unsigned long netdev_fc_xoff = 0;
668 static struct
670 void (*stimul)(struct device *);
671 struct device *dev;
672 } netdev_fc_slots[32];
674 int netdev_register_fc(struct device *dev, void (*stimul)(struct device *dev))
676 int bit = 0;
677 unsigned long flags;
679 save_flags(flags);
680 cli();
681 if (netdev_fc_mask != ~0UL) {
682 bit = ffz(netdev_fc_mask);
683 netdev_fc_slots[bit].stimul = stimul;
684 netdev_fc_slots[bit].dev = dev;
685 set_bit(bit, &netdev_fc_mask);
686 clear_bit(bit, &netdev_fc_xoff);
688 restore_flags(flags);
689 return bit;
692 void netdev_unregister_fc(int bit)
694 unsigned long flags;
696 save_flags(flags);
697 cli();
698 if (bit > 0) {
699 netdev_fc_slots[bit].stimul = NULL;
700 netdev_fc_slots[bit].dev = NULL;
701 clear_bit(bit, &netdev_fc_mask);
702 clear_bit(bit, &netdev_fc_xoff);
704 restore_flags(flags);
707 static void netdev_wakeup(void)
709 unsigned long xoff;
711 cli();
712 xoff = netdev_fc_xoff;
713 netdev_fc_xoff = 0;
714 netdev_dropping = 0;
715 netdev_throttle_events++;
716 while (xoff) {
717 int i = ffz(~xoff);
718 xoff &= ~(1<<i);
719 netdev_fc_slots[i].stimul(netdev_fc_slots[i].dev);
721 sti();
723 #endif
725 static void dev_clear_backlog(struct device *dev)
727 struct sk_buff *prev, *curr;
731 * Let now clear backlog queue. -AS
733 * We are competing here both with netif_rx() and net_bh().
734 * We don't want either of those to mess with skb ptrs
735 * while we work on them, thus cli()/sti().
737 * It looks better to use net_bh trick, at least
738 * to be sure, that we keep interrupt latency really low. --ANK (980727)
741 if (backlog.qlen) {
742 start_bh_atomic();
743 curr = backlog.next;
744 while ( curr != (struct sk_buff *)(&backlog) ) {
745 unsigned long flags;
746 curr=curr->next;
747 if ( curr->prev->dev == dev ) {
748 prev = curr->prev;
749 spin_lock_irqsave(&backlog.lock, flags);
750 __skb_unlink(prev, &backlog);
751 spin_unlock_irqrestore(&backlog.lock, flags);
752 kfree_skb(prev);
755 end_bh_atomic();
756 #ifdef CONFIG_NET_HW_FLOWCONTROL
757 if (netdev_dropping)
758 netdev_wakeup();
759 #else
760 netdev_dropping = 0;
761 #endif
766 * Receive a packet from a device driver and queue it for the upper
767 * (protocol) levels. It always succeeds.
770 void netif_rx(struct sk_buff *skb)
772 #ifndef CONFIG_CPU_IS_SLOW
773 if(skb->stamp.tv_sec==0)
774 get_fast_time(&skb->stamp);
775 #else
776 skb->stamp = xtime;
777 #endif
779 /* The code is rearranged so that the path is the most
780 short when CPU is congested, but is still operating.
783 if (backlog.qlen <= netdev_max_backlog) {
784 if (backlog.qlen) {
785 if (netdev_dropping == 0) {
786 skb_queue_tail(&backlog,skb);
787 mark_bh(NET_BH);
788 return;
790 atomic_inc(&netdev_rx_dropped);
791 kfree_skb(skb);
792 return;
794 #ifdef CONFIG_NET_HW_FLOWCONTROL
795 if (netdev_dropping)
796 netdev_wakeup();
797 #else
798 netdev_dropping = 0;
799 #endif
800 skb_queue_tail(&backlog,skb);
801 mark_bh(NET_BH);
802 return;
804 netdev_dropping = 1;
805 atomic_inc(&netdev_rx_dropped);
806 kfree_skb(skb);
809 #ifdef CONFIG_BRIDGE
810 static inline void handle_bridge(struct sk_buff *skb, unsigned short type)
812 if (br_stats.flags & BR_UP && br_protocol_ok(ntohs(type)))
815 * We pass the bridge a complete frame. This means
816 * recovering the MAC header first.
819 int offset;
821 skb=skb_clone(skb, GFP_ATOMIC);
822 if(skb==NULL)
823 return;
825 offset=skb->data-skb->mac.raw;
826 skb_push(skb,offset); /* Put header back on for bridge */
828 if(br_receive_frame(skb))
829 return;
830 kfree_skb(skb);
832 return;
834 #endif
838 * When we are called the queue is ready to grab, the interrupts are
839 * on and hardware can interrupt and queue to the receive queue as we
840 * run with no problems.
841 * This is run as a bottom half after an interrupt handler that does
842 * mark_bh(NET_BH);
845 void net_bh(void)
847 struct packet_type *ptype;
848 struct packet_type *pt_prev;
849 unsigned short type;
850 unsigned long start_time = jiffies;
852 NET_PROFILE_ENTER(net_bh);
854 * Can we send anything now? We want to clear the
855 * decks for any more sends that get done as we
856 * process the input. This also minimises the
857 * latency on a transmit interrupt bh.
860 if (qdisc_pending())
861 qdisc_run_queues();
864 * Any data left to process. This may occur because a
865 * mark_bh() is done after we empty the queue including
866 * that from the device which does a mark_bh() just after
870 * While the queue is not empty..
872 * Note that the queue never shrinks due to
873 * an interrupt, so we can do this test without
874 * disabling interrupts.
877 while (!skb_queue_empty(&backlog))
879 struct sk_buff * skb;
881 /* Give chance to other bottom halves to run */
882 if (jiffies - start_time > 1)
883 goto net_bh_break;
886 * We have a packet. Therefore the queue has shrunk
888 skb = skb_dequeue(&backlog);
890 #ifdef CONFIG_NET_FASTROUTE
891 if (skb->pkt_type == PACKET_FASTROUTE) {
892 dev_queue_xmit(skb);
893 continue;
895 #endif
898 * Bump the pointer to the next structure.
900 * On entry to the protocol layer. skb->data and
901 * skb->nh.raw point to the MAC and encapsulated data
904 /* XXX until we figure out every place to modify.. */
905 skb->h.raw = skb->nh.raw = skb->data;
907 if (skb->mac.raw < skb->head || skb->mac.raw > skb->data) {
908 printk(KERN_CRIT "%s: wrong mac.raw ptr, proto=%04x\n", skb->dev->name, skb->protocol);
909 kfree_skb(skb);
910 continue;
914 * Fetch the packet protocol ID.
917 type = skb->protocol;
919 #ifdef CONFIG_BRIDGE
921 * If we are bridging then pass the frame up to the
922 * bridging code (if this protocol is to be bridged).
923 * If it is bridged then move on
925 handle_bridge(skb, type);
926 #endif
929 * We got a packet ID. Now loop over the "known protocols"
930 * list. There are two lists. The ptype_all list of taps (normally empty)
931 * and the main protocol list which is hashed perfectly for normal protocols.
934 pt_prev = NULL;
935 read_lock(&ptype_lock);
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 read_unlock(&ptype_lock);
990 } /* End of queue loop */
993 * We have emptied the queue
997 * One last output flush.
1000 if (qdisc_pending())
1001 qdisc_run_queues();
1003 #ifdef CONFIG_NET_HW_FLOWCONTROL
1004 if (netdev_dropping)
1005 netdev_wakeup();
1006 #else
1007 netdev_dropping = 0;
1008 #endif
1009 NET_PROFILE_LEAVE(net_bh);
1010 return;
1012 net_bh_break:
1013 mark_bh(NET_BH);
1014 NET_PROFILE_LEAVE(net_bh);
1015 return;
1018 /* Protocol dependent address dumping routines */
1020 static gifconf_func_t * gifconf_list [NPROTO];
1022 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1024 if (family>=NPROTO)
1025 return -EINVAL;
1026 gifconf_list[family] = gifconf;
1027 return 0;
1032 * Map an interface index to its name (SIOCGIFNAME)
1036 * We need this ioctl for efficient implementation of the
1037 * if_indextoname() function required by the IPv6 API. Without
1038 * it, we would have to search all the interfaces to find a
1039 * match. --pb
1042 static int dev_ifname(struct ifreq *arg)
1044 struct device *dev;
1045 struct ifreq ifr;
1046 int err;
1049 * Fetch the caller's info block.
1052 err = copy_from_user(&ifr, arg, sizeof(struct ifreq));
1053 if (err)
1054 return -EFAULT;
1056 dev = dev_get_by_index(ifr.ifr_ifindex);
1057 if (!dev)
1058 return -ENODEV;
1060 strcpy(ifr.ifr_name, dev->name);
1062 err = copy_to_user(arg, &ifr, sizeof(struct ifreq));
1063 return (err)?-EFAULT:0;
1067 * Perform a SIOCGIFCONF call. This structure will change
1068 * size eventually, and there is nothing I can do about it.
1069 * Thus we will need a 'compatibility mode'.
1072 static int dev_ifconf(char *arg)
1074 struct ifconf ifc;
1075 struct device *dev;
1076 char *pos;
1077 int len;
1078 int total;
1079 int i;
1082 * Fetch the caller's info block.
1085 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
1086 return -EFAULT;
1088 len = ifc.ifc_len;
1089 if (ifc.ifc_buf) {
1090 pos = (char *) kmalloc(len, GFP_KERNEL);
1091 if(pos == NULL)
1092 return -ENOBUFS;
1093 } else
1094 pos = NULL;
1097 * Loop over the interfaces, and write an info block for each.
1100 total = 0;
1101 read_lock(&dev_base_lock);
1102 for (dev = dev_base; dev != NULL; dev = dev->next) {
1103 for (i=0; i<NPROTO; i++) {
1104 if (gifconf_list[i]) {
1105 int done;
1106 if (pos==NULL) {
1107 done = gifconf_list[i](dev, NULL, 0);
1108 } else {
1109 done = gifconf_list[i](dev, pos+total, len-total);
1111 total += done;
1115 read_unlock(&dev_base_lock);
1117 if(pos != NULL) {
1118 int err = copy_to_user(ifc.ifc_buf, pos, total);
1120 kfree(pos);
1121 if(err)
1122 return -EFAULT;
1126 * All done. Write the updated control block back to the caller.
1128 ifc.ifc_len = total;
1130 if (copy_to_user(arg, &ifc, sizeof(struct ifconf)))
1131 return -EFAULT;
1134 * Both BSD and Solaris return 0 here, so we do too.
1136 return 0;
1140 * This is invoked by the /proc filesystem handler to display a device
1141 * in detail.
1144 #ifdef CONFIG_PROC_FS
1145 static int sprintf_stats(char *buffer, struct device *dev)
1147 struct net_device_stats *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
1148 int size;
1150 if (stats)
1151 size = sprintf(buffer, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu %8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
1152 dev->name,
1153 stats->rx_bytes,
1154 stats->rx_packets, stats->rx_errors,
1155 stats->rx_dropped + stats->rx_missed_errors,
1156 stats->rx_fifo_errors,
1157 stats->rx_length_errors + stats->rx_over_errors
1158 + stats->rx_crc_errors + stats->rx_frame_errors,
1159 stats->rx_compressed, stats->multicast,
1160 stats->tx_bytes,
1161 stats->tx_packets, stats->tx_errors, stats->tx_dropped,
1162 stats->tx_fifo_errors, stats->collisions,
1163 stats->tx_carrier_errors + stats->tx_aborted_errors
1164 + stats->tx_window_errors + stats->tx_heartbeat_errors,
1165 stats->tx_compressed);
1166 else
1167 size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
1169 return size;
1173 * Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
1174 * to create /proc/net/dev
1177 int dev_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1179 int len=0;
1180 off_t begin=0;
1181 off_t pos=0;
1182 int size;
1184 struct device *dev;
1187 size = sprintf(buffer,
1188 "Inter-| Receive | Transmit\n"
1189 " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n");
1191 pos+=size;
1192 len+=size;
1195 read_lock(&dev_base_lock);
1196 for (dev = dev_base; dev != NULL; dev = dev->next) {
1197 size = sprintf_stats(buffer+len, dev);
1198 len+=size;
1199 pos=begin+len;
1201 if(pos<offset) {
1202 len=0;
1203 begin=pos;
1205 if(pos>offset+length)
1206 break;
1208 read_unlock(&dev_base_lock);
1210 *start=buffer+(offset-begin); /* Start of wanted data */
1211 len-=(offset-begin); /* Start slop */
1212 if(len>length)
1213 len=length; /* Ending slop */
1214 return len;
1217 static int dev_proc_stats(char *buffer, char **start, off_t offset,
1218 int length, int *eof, void *data)
1220 int len;
1222 len = sprintf(buffer, "%08x %08x %08x %08x %08x\n",
1223 atomic_read(&netdev_rx_dropped),
1224 #ifdef CONFIG_NET_HW_FLOWCONTROL
1225 netdev_throttle_events,
1226 #else
1228 #endif
1229 #ifdef CONFIG_NET_FASTROUTE
1230 dev_fastroute_stat.hits,
1231 dev_fastroute_stat.succeed,
1232 dev_fastroute_stat.deferred
1233 #else
1234 0, 0, 0
1235 #endif
1238 len -= offset;
1240 if (len > length)
1241 len = length;
1242 if(len < 0)
1243 len = 0;
1245 *start = buffer + offset;
1246 *eof = 1;
1248 return len;
1251 #endif /* CONFIG_PROC_FS */
1254 #ifdef CONFIG_NET_RADIO
1255 #ifdef CONFIG_PROC_FS
1258 * Print one entry of /proc/net/wireless
1259 * This is a clone of /proc/net/dev (just above)
1261 static int sprintf_wireless_stats(char *buffer, struct device *dev)
1263 /* Get stats from the driver */
1264 struct iw_statistics *stats = (dev->get_wireless_stats ?
1265 dev->get_wireless_stats(dev) :
1266 (struct iw_statistics *) NULL);
1267 int size;
1269 if(stats != (struct iw_statistics *) NULL)
1270 size = sprintf(buffer,
1271 "%6s: %02x %3d%c %3d%c %3d%c %5d %5d %5d\n",
1272 dev->name,
1273 stats->status,
1274 stats->qual.qual,
1275 stats->qual.updated & 1 ? '.' : ' ',
1276 stats->qual.level,
1277 stats->qual.updated & 2 ? '.' : ' ',
1278 stats->qual.noise,
1279 stats->qual.updated & 3 ? '.' : ' ',
1280 stats->discard.nwid,
1281 stats->discard.code,
1282 stats->discard.misc);
1283 else
1284 size = 0;
1286 return size;
1290 * Print info for /proc/net/wireless (print all entries)
1291 * This is a clone of /proc/net/dev (just above)
1293 int dev_get_wireless_info(char * buffer, char **start, off_t offset,
1294 int length, int dummy)
1296 int len = 0;
1297 off_t begin = 0;
1298 off_t pos = 0;
1299 int size;
1301 struct device * dev;
1303 size = sprintf(buffer,
1304 "Inter-|sta| Quality | Discarded packets\n"
1305 " face |tus|link level noise| nwid crypt misc\n");
1307 pos+=size;
1308 len+=size;
1310 read_lock(&dev_base_lock);
1311 for(dev = dev_base; dev != NULL; dev = dev->next) {
1312 size = sprintf_wireless_stats(buffer+len, dev);
1313 len+=size;
1314 pos=begin+len;
1316 if(pos < offset) {
1317 len=0;
1318 begin=pos;
1320 if(pos > offset + length)
1321 break;
1323 read_unlock(&dev_base_lock);
1325 *start = buffer + (offset - begin); /* Start of wanted data */
1326 len -= (offset - begin); /* Start slop */
1327 if(len > length)
1328 len = length; /* Ending slop */
1330 return len;
1332 #endif /* CONFIG_PROC_FS */
1333 #endif /* CONFIG_NET_RADIO */
1335 void dev_set_promiscuity(struct device *dev, int inc)
1337 unsigned short old_flags = dev->flags;
1339 dev->flags |= IFF_PROMISC;
1340 if ((dev->promiscuity += inc) == 0)
1341 dev->flags &= ~IFF_PROMISC;
1342 if (dev->flags^old_flags) {
1343 #ifdef CONFIG_NET_FASTROUTE
1344 if (dev->flags&IFF_PROMISC) {
1345 netdev_fastroute_obstacles++;
1346 dev_clear_fastroute(dev);
1347 } else
1348 netdev_fastroute_obstacles--;
1349 #endif
1350 dev_mc_upload(dev);
1351 printk(KERN_INFO "device %s %s promiscuous mode\n",
1352 dev->name, (dev->flags&IFF_PROMISC) ? "entered" : "left");
1356 void dev_set_allmulti(struct device *dev, int inc)
1358 unsigned short old_flags = dev->flags;
1360 dev->flags |= IFF_ALLMULTI;
1361 if ((dev->allmulti += inc) == 0)
1362 dev->flags &= ~IFF_ALLMULTI;
1363 if (dev->flags^old_flags)
1364 dev_mc_upload(dev);
1367 int dev_change_flags(struct device *dev, unsigned flags)
1369 int ret;
1370 int old_flags = dev->flags;
1373 * Set the flags on our device.
1376 dev->flags = (flags & (IFF_DEBUG|IFF_NOTRAILERS|IFF_RUNNING|IFF_NOARP|
1377 IFF_SLAVE|IFF_MASTER|IFF_DYNAMIC|
1378 IFF_MULTICAST|IFF_PORTSEL|IFF_AUTOMEDIA)) |
1379 (dev->flags & (IFF_UP|IFF_VOLATILE|IFF_PROMISC|IFF_ALLMULTI));
1382 * Load in the correct multicast list now the flags have changed.
1385 dev_mc_upload(dev);
1388 * Have we downed the interface. We handle IFF_UP ourselves
1389 * according to user attempts to set it, rather than blindly
1390 * setting it.
1393 ret = 0;
1394 if ((old_flags^flags)&IFF_UP) /* Bit is different ? */
1396 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
1398 if (ret == 0)
1399 dev_mc_upload(dev);
1402 if (dev->flags&IFF_UP &&
1403 ((old_flags^dev->flags)&~(IFF_UP|IFF_RUNNING|IFF_PROMISC|IFF_ALLMULTI|IFF_VOLATILE)))
1404 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
1406 if ((flags^dev->gflags)&IFF_PROMISC) {
1407 int inc = (flags&IFF_PROMISC) ? +1 : -1;
1408 dev->gflags ^= IFF_PROMISC;
1409 dev_set_promiscuity(dev, inc);
1412 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
1413 is important. Some (broken) drivers set IFF_PROMISC, when
1414 IFF_ALLMULTI is requested not asking us and not reporting.
1416 if ((flags^dev->gflags)&IFF_ALLMULTI) {
1417 int inc = (flags&IFF_ALLMULTI) ? +1 : -1;
1418 dev->gflags ^= IFF_ALLMULTI;
1419 dev_set_allmulti(dev, inc);
1422 return ret;
1426 * Perform the SIOCxIFxxx calls.
1429 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
1431 struct device *dev;
1432 int err;
1434 if ((dev = dev_get(ifr->ifr_name)) == NULL)
1435 return -ENODEV;
1437 switch(cmd)
1439 case SIOCGIFFLAGS: /* Get interface flags */
1440 ifr->ifr_flags = (dev->flags&~(IFF_PROMISC|IFF_ALLMULTI))
1441 |(dev->gflags&(IFF_PROMISC|IFF_ALLMULTI));
1442 return 0;
1444 case SIOCSIFFLAGS: /* Set interface flags */
1445 return dev_change_flags(dev, ifr->ifr_flags);
1447 case SIOCGIFMETRIC: /* Get the metric on the interface (currently unused) */
1448 ifr->ifr_metric = 0;
1449 return 0;
1451 case SIOCSIFMETRIC: /* Set the metric on the interface (currently unused) */
1452 return -EOPNOTSUPP;
1454 case SIOCGIFMTU: /* Get the MTU of a device */
1455 ifr->ifr_mtu = dev->mtu;
1456 return 0;
1458 case SIOCSIFMTU: /* Set the MTU of a device */
1459 if (ifr->ifr_mtu == dev->mtu)
1460 return 0;
1463 * MTU must be positive.
1466 if (ifr->ifr_mtu<0)
1467 return -EINVAL;
1469 if (dev->change_mtu)
1470 err = dev->change_mtu(dev, ifr->ifr_mtu);
1471 else {
1472 dev->mtu = ifr->ifr_mtu;
1473 err = 0;
1475 if (!err && dev->flags&IFF_UP)
1476 notifier_call_chain(&netdev_chain, NETDEV_CHANGEMTU, dev);
1477 return err;
1479 case SIOCGIFHWADDR:
1480 memcpy(ifr->ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1481 ifr->ifr_hwaddr.sa_family=dev->type;
1482 return 0;
1484 case SIOCSIFHWADDR:
1485 if(dev->set_mac_address==NULL)
1486 return -EOPNOTSUPP;
1487 if(ifr->ifr_hwaddr.sa_family!=dev->type)
1488 return -EINVAL;
1489 err=dev->set_mac_address(dev,&ifr->ifr_hwaddr);
1490 if (!err)
1491 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
1492 return err;
1494 case SIOCSIFHWBROADCAST:
1495 if(ifr->ifr_hwaddr.sa_family!=dev->type)
1496 return -EINVAL;
1497 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data, MAX_ADDR_LEN);
1498 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
1499 return 0;
1501 case SIOCGIFMAP:
1502 ifr->ifr_map.mem_start=dev->mem_start;
1503 ifr->ifr_map.mem_end=dev->mem_end;
1504 ifr->ifr_map.base_addr=dev->base_addr;
1505 ifr->ifr_map.irq=dev->irq;
1506 ifr->ifr_map.dma=dev->dma;
1507 ifr->ifr_map.port=dev->if_port;
1508 return 0;
1510 case SIOCSIFMAP:
1511 if (dev->set_config)
1512 return dev->set_config(dev,&ifr->ifr_map);
1513 return -EOPNOTSUPP;
1515 case SIOCADDMULTI:
1516 if(dev->set_multicast_list==NULL ||
1517 ifr->ifr_hwaddr.sa_family!=AF_UNSPEC)
1518 return -EINVAL;
1519 dev_mc_add(dev,ifr->ifr_hwaddr.sa_data, dev->addr_len, 1);
1520 return 0;
1522 case SIOCDELMULTI:
1523 if(dev->set_multicast_list==NULL ||
1524 ifr->ifr_hwaddr.sa_family!=AF_UNSPEC)
1525 return -EINVAL;
1526 dev_mc_delete(dev,ifr->ifr_hwaddr.sa_data,dev->addr_len, 1);
1527 return 0;
1529 case SIOCGIFINDEX:
1530 ifr->ifr_ifindex = dev->ifindex;
1531 return 0;
1533 case SIOCGIFTXQLEN:
1534 ifr->ifr_qlen = dev->tx_queue_len;
1535 return 0;
1537 case SIOCSIFTXQLEN:
1538 if(ifr->ifr_qlen<0)
1539 return -EINVAL;
1540 dev->tx_queue_len = ifr->ifr_qlen;
1541 return 0;
1543 case SIOCSIFNAME:
1544 if (dev->flags&IFF_UP)
1545 return -EBUSY;
1546 if (dev_get(ifr->ifr_newname))
1547 return -EEXIST;
1548 memcpy(dev->name, ifr->ifr_newname, IFNAMSIZ);
1549 dev->name[IFNAMSIZ-1] = 0;
1550 notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
1551 return 0;
1554 * Unknown or private ioctl
1557 default:
1558 if(cmd >= SIOCDEVPRIVATE &&
1559 cmd <= SIOCDEVPRIVATE + 15) {
1560 if (dev->do_ioctl)
1561 return dev->do_ioctl(dev, ifr, cmd);
1562 return -EOPNOTSUPP;
1565 #ifdef CONFIG_NET_RADIO
1566 if(cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1567 if (dev->do_ioctl)
1568 return dev->do_ioctl(dev, ifr, cmd);
1569 return -EOPNOTSUPP;
1571 #endif /* CONFIG_NET_RADIO */
1574 return -EINVAL;
1579 * This function handles all "interface"-type I/O control requests. The actual
1580 * 'doing' part of this is dev_ifsioc above.
1583 int dev_ioctl(unsigned int cmd, void *arg)
1585 struct ifreq ifr;
1586 int ret;
1587 char *colon;
1589 /* One special case: SIOCGIFCONF takes ifconf argument
1590 and requires shared lock, because it sleeps writing
1591 to user space.
1594 if (cmd == SIOCGIFCONF) {
1595 rtnl_shlock();
1596 ret = dev_ifconf((char *) arg);
1597 rtnl_shunlock();
1598 return ret;
1600 if (cmd == SIOCGIFNAME) {
1601 return dev_ifname((struct ifreq *)arg);
1604 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
1605 return -EFAULT;
1607 ifr.ifr_name[IFNAMSIZ-1] = 0;
1609 colon = strchr(ifr.ifr_name, ':');
1610 if (colon)
1611 *colon = 0;
1614 * See which interface the caller is talking about.
1617 switch(cmd)
1620 * These ioctl calls:
1621 * - can be done by all.
1622 * - atomic and do not require locking.
1623 * - return a value
1626 case SIOCGIFFLAGS:
1627 case SIOCGIFMETRIC:
1628 case SIOCGIFMTU:
1629 case SIOCGIFHWADDR:
1630 case SIOCGIFSLAVE:
1631 case SIOCGIFMAP:
1632 case SIOCGIFINDEX:
1633 case SIOCGIFTXQLEN:
1634 dev_load(ifr.ifr_name);
1635 ret = dev_ifsioc(&ifr, cmd);
1636 if (!ret) {
1637 if (colon)
1638 *colon = ':';
1639 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1640 return -EFAULT;
1642 return ret;
1645 * These ioctl calls:
1646 * - require superuser power.
1647 * - require strict serialization.
1648 * - do not return a value
1651 case SIOCSIFFLAGS:
1652 case SIOCSIFMETRIC:
1653 case SIOCSIFMTU:
1654 case SIOCSIFMAP:
1655 case SIOCSIFHWADDR:
1656 case SIOCSIFSLAVE:
1657 case SIOCADDMULTI:
1658 case SIOCDELMULTI:
1659 case SIOCSIFHWBROADCAST:
1660 case SIOCSIFTXQLEN:
1661 case SIOCSIFNAME:
1662 if (!capable(CAP_NET_ADMIN))
1663 return -EPERM;
1664 dev_load(ifr.ifr_name);
1665 rtnl_lock();
1666 ret = dev_ifsioc(&ifr, cmd);
1667 rtnl_unlock();
1668 return ret;
1670 case SIOCGIFMEM:
1671 /* Get the per device memory space. We can add this but currently
1672 do not support it */
1673 case SIOCSIFMEM:
1674 /* Set the per device memory buffer space. Not applicable in our case */
1675 case SIOCSIFLINK:
1676 return -EINVAL;
1679 * Unknown or private ioctl.
1682 default:
1683 if (cmd >= SIOCDEVPRIVATE &&
1684 cmd <= SIOCDEVPRIVATE + 15) {
1685 dev_load(ifr.ifr_name);
1686 rtnl_lock();
1687 ret = dev_ifsioc(&ifr, cmd);
1688 rtnl_unlock();
1689 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1690 return -EFAULT;
1691 return ret;
1693 #ifdef CONFIG_NET_RADIO
1694 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1695 dev_load(ifr.ifr_name);
1696 if (IW_IS_SET(cmd)) {
1697 if (!suser())
1698 return -EPERM;
1700 rtnl_lock();
1701 ret = dev_ifsioc(&ifr, cmd);
1702 rtnl_unlock();
1703 if (!ret && IW_IS_GET(cmd) &&
1704 copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1705 return -EFAULT;
1706 return ret;
1708 #endif /* CONFIG_NET_RADIO */
1709 return -EINVAL;
1713 int dev_new_index(void)
1715 static int ifindex;
1716 for (;;) {
1717 if (++ifindex <= 0)
1718 ifindex=1;
1719 if (dev_get_by_index(ifindex) == NULL)
1720 return ifindex;
1724 static int dev_boot_phase = 1;
1727 int register_netdevice(struct device *dev)
1729 struct device *d, **dp;
1731 spin_lock_init(&dev->queue_lock);
1732 spin_lock_init(&dev->xmit_lock);
1733 dev->xmit_lock_owner = -1;
1735 if (dev_boot_phase) {
1736 /* This is NOT bug, but I am not sure, that all the
1737 devices, initialized before netdev module is started
1738 are sane.
1740 Now they are chained to device boot list
1741 and probed later. If a module is initialized
1742 before netdev, but assumes that dev->init
1743 is really called by register_netdev(), it will fail.
1745 So that this message should be printed for a while.
1747 printk(KERN_INFO "early initialization of device %s is deferred\n", dev->name);
1749 /* Check for existence, and append to tail of chain */
1750 for (dp=&dev_base; (d=*dp) != NULL; dp=&d->next) {
1751 if (d == dev || strcmp(d->name, dev->name) == 0) {
1752 return -EEXIST;
1755 dev->next = NULL;
1756 write_lock_bh(&dev_base_lock);
1757 *dp = dev;
1758 write_unlock_bh(&dev_base_lock);
1759 return 0;
1762 dev->iflink = -1;
1764 /* Init, if this function is available */
1765 if (dev->init && dev->init(dev) != 0)
1766 return -EIO;
1768 dev->ifindex = dev_new_index();
1769 if (dev->iflink == -1)
1770 dev->iflink = dev->ifindex;
1772 /* Check for existence, and append to tail of chain */
1773 for (dp=&dev_base; (d=*dp) != NULL; dp=&d->next) {
1774 if (d == dev || strcmp(d->name, dev->name) == 0) {
1775 return -EEXIST;
1778 dev->next = NULL;
1779 dev_init_scheduler(dev);
1780 write_lock_bh(&dev_base_lock);
1781 *dp = dev;
1782 write_unlock_bh(&dev_base_lock);
1784 /* Notify protocols, that a new device appeared. */
1785 notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
1787 return 0;
1790 int unregister_netdevice(struct device *dev)
1792 struct device *d, **dp;
1794 /* If device is running, close it first. */
1795 if (dev->flags & IFF_UP)
1796 dev_close(dev);
1798 /* And unlink it from device chain. */
1799 for (dp = &dev_base; (d=*dp) != NULL; dp=&d->next) {
1800 if (d == dev) {
1801 write_lock_bh(&dev_base_lock);
1802 *dp = d->next;
1803 write_unlock_bh(&dev_base_lock);
1805 /* Sorry. It is known "feature". The race is clear.
1806 Keep it after device reference counting will
1807 be complete.
1809 synchronize_bh();
1810 break;
1813 if (d == NULL)
1814 return -ENODEV;
1816 /* It is "synchronize_bh" to those of guys, who overslept
1817 in skb_alloc/page fault etc. that device is off-line.
1818 Again, it can be removed only if devices are refcounted.
1820 dev_lock_wait();
1822 if (dev_boot_phase == 0) {
1823 #ifdef CONFIG_NET_FASTROUTE
1824 dev_clear_fastroute(dev);
1825 #endif
1827 /* Shutdown queueing discipline. */
1828 dev_shutdown(dev);
1830 /* Notify protocols, that we are about to destroy
1831 this device. They should clean all the things.
1833 notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
1836 * Flush the multicast chain
1838 dev_mc_discard(dev);
1841 if (dev->destructor)
1842 dev->destructor(dev);
1843 return 0;
1848 * Initialize the DEV module. At boot time this walks the device list and
1849 * unhooks any devices that fail to initialise (normally hardware not
1850 * present) and leaves us with a valid list of present and active devices.
1853 extern int lance_init(void);
1854 extern int bpq_init(void);
1855 extern int scc_init(void);
1856 extern void sdla_setup(void);
1857 extern void dlci_setup(void);
1858 extern int dmascc_init(void);
1859 extern int sm_init(void);
1861 extern int baycom_ser_fdx_init(void);
1862 extern int baycom_ser_hdx_init(void);
1863 extern int baycom_par_init(void);
1865 extern int lapbeth_init(void);
1866 extern void arcnet_init(void);
1867 extern void ip_auto_config(void);
1868 #ifdef CONFIG_8xx
1869 extern int cpm_enet_init(void);
1870 #endif /* CONFIG_8xx */
1872 #ifdef CONFIG_PROC_FS
1873 static struct proc_dir_entry proc_net_dev = {
1874 PROC_NET_DEV, 3, "dev",
1875 S_IFREG | S_IRUGO, 1, 0, 0,
1876 0, &proc_net_inode_operations,
1877 dev_get_info
1879 #endif
1881 #ifdef CONFIG_NET_RADIO
1882 #ifdef CONFIG_PROC_FS
1883 static struct proc_dir_entry proc_net_wireless = {
1884 PROC_NET_WIRELESS, 8, "wireless",
1885 S_IFREG | S_IRUGO, 1, 0, 0,
1886 0, &proc_net_inode_operations,
1887 dev_get_wireless_info
1889 #endif /* CONFIG_PROC_FS */
1890 #endif /* CONFIG_NET_RADIO */
1892 __initfunc(int net_dev_init(void))
1894 struct device *dev, **dp;
1896 #ifdef CONFIG_NET_SCHED
1897 pktsched_init();
1898 #endif
1901 * Initialise the packet receive queue.
1904 skb_queue_head_init(&backlog);
1907 * The bridge has to be up before the devices
1910 #ifdef CONFIG_BRIDGE
1911 br_init();
1912 #endif
1915 * This is Very Ugly(tm).
1917 * Some devices want to be initialized early..
1920 #if defined(CONFIG_SCC)
1921 scc_init();
1922 #endif
1923 #if defined(CONFIG_DMASCC)
1924 dmascc_init();
1925 #endif
1926 #if defined(CONFIG_BPQETHER)
1927 bpq_init();
1928 #endif
1929 #if defined(CONFIG_DLCI)
1930 dlci_setup();
1931 #endif
1932 #if defined(CONFIG_SDLA)
1933 sdla_setup();
1934 #endif
1935 #if defined(CONFIG_BAYCOM_PAR)
1936 baycom_par_init();
1937 #endif
1938 #if defined(CONFIG_BAYCOM_SER_FDX)
1939 baycom_ser_fdx_init();
1940 #endif
1941 #if defined(CONFIG_BAYCOM_SER_HDX)
1942 baycom_ser_hdx_init();
1943 #endif
1944 #if defined(CONFIG_SOUNDMODEM)
1945 sm_init();
1946 #endif
1947 #if defined(CONFIG_LAPBETHER)
1948 lapbeth_init();
1949 #endif
1950 #if defined(CONFIG_PLIP)
1951 plip_init();
1952 #endif
1953 #if defined(CONFIG_ARCNET)
1954 arcnet_init();
1955 #endif
1956 #if defined(CONFIG_8xx)
1957 cpm_enet_init();
1958 #endif
1960 * SLHC if present needs attaching so other people see it
1961 * even if not opened.
1964 #ifdef CONFIG_INET
1965 #if (defined(CONFIG_SLIP) && defined(CONFIG_SLIP_COMPRESSED)) \
1966 || defined(CONFIG_PPP) \
1967 || (defined(CONFIG_ISDN) && defined(CONFIG_ISDN_PPP))
1968 slhc_install();
1969 #endif
1970 #endif
1972 #ifdef CONFIG_NET_PROFILE
1973 net_profile_init();
1974 NET_PROFILE_REGISTER(dev_queue_xmit);
1975 NET_PROFILE_REGISTER(net_bh);
1976 #if 0
1977 NET_PROFILE_REGISTER(net_bh_skb);
1978 #endif
1979 #endif
1981 * Add the devices.
1982 * If the call to dev->init fails, the dev is removed
1983 * from the chain disconnecting the device until the
1984 * next reboot.
1986 * NB At boot phase networking is dead. No locking is required.
1987 * But we still preserve dev_base_lock for sanity.
1990 dp = &dev_base;
1991 while ((dev = *dp) != NULL) {
1992 spin_lock_init(&dev->queue_lock);
1993 spin_lock_init(&dev->xmit_lock);
1994 dev->xmit_lock_owner = -1;
1995 dev->iflink = -1;
1996 if (dev->init && dev->init(dev)) {
1998 * It failed to come up. Unhook it.
2000 write_lock_bh(&dev_base_lock);
2001 *dp = dev->next;
2002 write_unlock_bh(&dev_base_lock);
2003 } else {
2004 dp = &dev->next;
2005 dev->ifindex = dev_new_index();
2006 if (dev->iflink == -1)
2007 dev->iflink = dev->ifindex;
2008 dev_init_scheduler(dev);
2012 #ifdef CONFIG_PROC_FS
2013 proc_net_register(&proc_net_dev);
2015 struct proc_dir_entry *ent = create_proc_entry("net/dev_stat", 0, 0);
2016 ent->read_proc = dev_proc_stats;
2018 #endif
2020 #ifdef CONFIG_NET_RADIO
2021 #ifdef CONFIG_PROC_FS
2022 proc_net_register(&proc_net_wireless);
2023 #endif /* CONFIG_PROC_FS */
2024 #endif /* CONFIG_NET_RADIO */
2026 init_bh(NET_BH, net_bh);
2028 dev_boot_phase = 0;
2030 dst_init();
2031 dev_mcast_init();
2033 #ifdef CONFIG_IP_PNP
2034 ip_auto_config();
2035 #endif
2037 return 0;