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>
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dahinds@users.sourceforge.net>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
23 * D.J. Barrow : Fixed bug where dev->refcnt gets set
24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
27 * Alan Cox : device private ioctl copies fields back.
28 * Alan Cox : Transmit queue code does relevant
29 * stunts to keep the queue safe.
30 * Alan Cox : Fixed double lock.
31 * Alan Cox : Fixed promisc NULL pointer trap
32 * ???????? : Support the full private ioctl range
33 * Alan Cox : Moved ioctl permission check into
35 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
36 * Alan Cox : 100 backlog just doesn't cut it when
37 * you start doing multicast video 8)
38 * Alan Cox : Rewrote net_bh and list manager.
39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
40 * Alan Cox : Took out transmit every packet pass
41 * Saved a few bytes in the ioctl handler
42 * Alan Cox : Network driver sets packet type before
43 * calling netif_rx. Saves a function
45 * Alan Cox : Hashed net_bh()
46 * Richard Kooijman: Timestamp fixes.
47 * Alan Cox : Wrong field in SIOCGIFDSTADDR
48 * Alan Cox : Device lock protection.
49 * Alan Cox : Fixed nasty side effect of device close
51 * Rudi Cilibrasi : Pass the right thing to
53 * Dave Miller : 32bit quantity for the device lock to
54 * make it work out on a Sparc.
55 * Bjorn Ekwall : Added KERNELD hack.
56 * Alan Cox : Cleaned up the backlog initialise.
57 * Craig Metz : SIOCGIFCONF fix if space for under
59 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
60 * is no device open function.
61 * Andi Kleen : Fix error reporting for SIOCGIFCONF
62 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
63 * Cyrus Durgin : Cleaned for KMOD
64 * Adam Sulmicki : Bug Fix : Network Device Unload
65 * A network device unload needs to purge
67 * Paul Rusty Russell : SIOCSIFNAME
68 * Pekka Riikonen : Netdev boot-time settings code
69 * Andrew Morton : Make unregister_netdevice wait
70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
72 * - netif_rx() feedback
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/bitops.h>
78 #include <linux/config.h>
79 #include <linux/cpu.h>
80 #include <linux/types.h>
81 #include <linux/kernel.h>
82 #include <linux/sched.h>
83 #include <linux/string.h>
85 #include <linux/socket.h>
86 #include <linux/sockios.h>
87 #include <linux/errno.h>
88 #include <linux/interrupt.h>
89 #include <linux/if_ether.h>
90 #include <linux/netdevice.h>
91 #include <linux/etherdevice.h>
92 #include <linux/notifier.h>
93 #include <linux/skbuff.h>
95 #include <linux/rtnetlink.h>
96 #include <linux/proc_fs.h>
97 #include <linux/seq_file.h>
98 #include <linux/stat.h>
99 #include <linux/if_bridge.h>
100 #include <linux/divert.h>
102 #include <net/pkt_sched.h>
103 #include <net/checksum.h>
104 #include <linux/highmem.h>
105 #include <linux/init.h>
106 #include <linux/kmod.h>
107 #include <linux/module.h>
108 #include <linux/kallsyms.h>
109 #include <linux/netpoll.h>
110 #include <linux/rcupdate.h>
111 #ifdef CONFIG_NET_RADIO
112 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
113 #include <net/iw_handler.h>
114 #endif /* CONFIG_NET_RADIO */
115 #include <asm/current.h>
117 /* This define, if set, will randomly drop a packet when congestion
118 * is more than moderate. It helps fairness in the multi-interface
119 * case when one of them is a hog, but it kills performance for the
120 * single interface case so it is off now by default.
124 /* Setting this will sample the queue lengths and thus congestion
125 * via a timer instead of as each packet is received.
127 #undef OFFLINE_SAMPLE
130 * The list of packet types we will receive (as opposed to discard)
131 * and the routines to invoke.
133 * Why 16. Because with 16 the only overlap we get on a hash of the
134 * low nibble of the protocol value is RARP/SNAP/X.25.
136 * NOTE: That is no longer true with the addition of VLAN tags. Not
137 * sure which should go first, but I bet it won't make much
138 * difference if we are running VLANs. The good news is that
139 * this protocol won't be in the list unless compiled in, so
140 * the average user (w/out VLANs) will not be adversly affected.
157 static spinlock_t ptype_lock = SPIN_LOCK_UNLOCKED;
158 static struct list_head ptype_base[16]; /* 16 way hashed list */
159 static struct list_head ptype_all; /* Taps */
161 #ifdef OFFLINE_SAMPLE
162 static void sample_queue(unsigned long dummy);
163 static struct timer_list samp_timer = TIMER_INITIALIZER(sample_queue, 0, 0);
167 * The @dev_base list is protected by @dev_base_lock and the rtln
170 * Pure readers hold dev_base_lock for reading.
172 * Writers must hold the rtnl semaphore while they loop through the
173 * dev_base list, and hold dev_base_lock for writing when they do the
174 * actual updates. This allows pure readers to access the list even
175 * while a writer is preparing to update it.
177 * To put it another way, dev_base_lock is held for writing only to
178 * protect against pure readers; the rtnl semaphore provides the
179 * protection against other writers.
181 * See, for example usages, register_netdevice() and
182 * unregister_netdevice(), which must be called with the rtnl
185 struct net_device *dev_base;
186 struct net_device **dev_tail = &dev_base;
187 rwlock_t dev_base_lock = RW_LOCK_UNLOCKED;
189 EXPORT_SYMBOL(dev_base);
190 EXPORT_SYMBOL(dev_base_lock);
192 #define NETDEV_HASHBITS 8
193 static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
194 static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
196 static inline struct hlist_head *dev_name_hash(const char *name)
198 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
199 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
202 static inline struct hlist_head *dev_index_hash(int ifindex)
204 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
211 static struct notifier_block *netdev_chain;
214 * Device drivers call our routines to queue packets here. We empty the
215 * queue in the local softnet handler.
217 DEFINE_PER_CPU(struct softnet_data, softnet_data) = { 0, };
220 extern int netdev_sysfs_init(void);
221 extern int netdev_register_sysfs(struct net_device *);
222 extern void netdev_unregister_sysfs(struct net_device *);
224 #define netdev_sysfs_init() (0)
225 #define netdev_register_sysfs(dev) (0)
226 #define netdev_unregister_sysfs(dev) do { } while(0)
230 /*******************************************************************************
232 Protocol management and registration routines
234 *******************************************************************************/
243 * Add a protocol ID to the list. Now that the input handler is
244 * smarter we can dispense with all the messy stuff that used to be
247 * BEWARE!!! Protocol handlers, mangling input packets,
248 * MUST BE last in hash buckets and checking protocol handlers
249 * MUST start from promiscuous ptype_all chain in net_bh.
250 * It is true now, do not change it.
251 * Explanation follows: if protocol handler, mangling packet, will
252 * be the first on list, it is not able to sense, that packet
253 * is cloned and should be copied-on-write, so that it will
254 * change it and subsequent readers will get broken packet.
259 * dev_add_pack - add packet handler
260 * @pt: packet type declaration
262 * Add a protocol handler to the networking stack. The passed &packet_type
263 * is linked into kernel lists and may not be freed until it has been
264 * removed from the kernel lists.
266 * This call does not sleep therefore it can not
267 * guarantee all CPU's that are in middle of receiving packets
268 * will see the new packet type (until the next received packet).
271 void dev_add_pack(struct packet_type *pt)
275 spin_lock_bh(&ptype_lock);
276 if (pt->type == htons(ETH_P_ALL)) {
278 list_add_rcu(&pt->list, &ptype_all);
280 hash = ntohs(pt->type) & 15;
281 list_add_rcu(&pt->list, &ptype_base[hash]);
283 spin_unlock_bh(&ptype_lock);
286 extern void linkwatch_run_queue(void);
291 * __dev_remove_pack - remove packet handler
292 * @pt: packet type declaration
294 * Remove a protocol handler that was previously added to the kernel
295 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
296 * from the kernel lists and can be freed or reused once this function
299 * The packet type might still be in use by receivers
300 * and must not be freed until after all the CPU's have gone
301 * through a quiescent state.
303 void __dev_remove_pack(struct packet_type *pt)
305 struct list_head *head;
306 struct packet_type *pt1;
308 spin_lock_bh(&ptype_lock);
310 if (pt->type == htons(ETH_P_ALL)) {
314 head = &ptype_base[ntohs(pt->type) & 15];
316 list_for_each_entry(pt1, head, list) {
318 list_del_rcu(&pt->list);
323 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
325 spin_unlock_bh(&ptype_lock);
328 * dev_remove_pack - remove packet handler
329 * @pt: packet type declaration
331 * Remove a protocol handler that was previously added to the kernel
332 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
333 * from the kernel lists and can be freed or reused once this function
336 * This call sleeps to guarantee that no CPU is looking at the packet
339 void dev_remove_pack(struct packet_type *pt)
341 __dev_remove_pack(pt);
346 /******************************************************************************
348 Device Boot-time Settings Routines
350 *******************************************************************************/
352 /* Boot time configuration table */
353 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
356 * netdev_boot_setup_add - add new setup entry
357 * @name: name of the device
358 * @map: configured settings for the device
360 * Adds new setup entry to the dev_boot_setup list. The function
361 * returns 0 on error and 1 on success. This is a generic routine to
364 int netdev_boot_setup_add(char *name, struct ifmap *map)
366 struct netdev_boot_setup *s;
370 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
371 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
372 memset(s[i].name, 0, sizeof(s[i].name));
373 strcpy(s[i].name, name);
374 memcpy(&s[i].map, map, sizeof(s[i].map));
379 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
383 * netdev_boot_setup_check - check boot time settings
384 * @dev: the netdevice
386 * Check boot time settings for the device.
387 * The found settings are set for the device to be used
388 * later in the device probing.
389 * Returns 0 if no settings found, 1 if they are.
391 int netdev_boot_setup_check(struct net_device *dev)
393 struct netdev_boot_setup *s = dev_boot_setup;
396 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
397 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
398 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
399 dev->irq = s[i].map.irq;
400 dev->base_addr = s[i].map.base_addr;
401 dev->mem_start = s[i].map.mem_start;
402 dev->mem_end = s[i].map.mem_end;
411 * netdev_boot_base - get address from boot time settings
412 * @prefix: prefix for network device
413 * @unit: id for network device
415 * Check boot time settings for the base address of device.
416 * The found settings are set for the device to be used
417 * later in the device probing.
418 * Returns 0 if no settings found.
420 unsigned long netdev_boot_base(const char *prefix, int unit)
422 const struct netdev_boot_setup *s = dev_boot_setup;
426 sprintf(name, "%s%d", prefix, unit);
429 * If device already registered then return base of 1
430 * to indicate not to probe for this interface
432 if (__dev_get_by_name(name))
435 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
436 if (!strcmp(name, s[i].name))
437 return s[i].map.base_addr;
442 * Saves at boot time configured settings for any netdevice.
444 int __init netdev_boot_setup(char *str)
449 str = get_options(str, ARRAY_SIZE(ints), ints);
454 memset(&map, 0, sizeof(map));
458 map.base_addr = ints[2];
460 map.mem_start = ints[3];
462 map.mem_end = ints[4];
464 /* Add new entry to the list */
465 return netdev_boot_setup_add(str, &map);
468 __setup("netdev=", netdev_boot_setup);
470 /*******************************************************************************
472 Device Interface Subroutines
474 *******************************************************************************/
477 * __dev_get_by_name - find a device by its name
478 * @name: name to find
480 * Find an interface by name. Must be called under RTNL semaphore
481 * or @dev_base_lock. If the name is found a pointer to the device
482 * is returned. If the name is not found then %NULL is returned. The
483 * reference counters are not incremented so the caller must be
484 * careful with locks.
487 struct net_device *__dev_get_by_name(const char *name)
489 struct hlist_node *p;
491 hlist_for_each(p, dev_name_hash(name)) {
492 struct net_device *dev
493 = hlist_entry(p, struct net_device, name_hlist);
494 if (!strncmp(dev->name, name, IFNAMSIZ))
501 * dev_get_by_name - find a device by its name
502 * @name: name to find
504 * Find an interface by name. This can be called from any
505 * context and does its own locking. The returned handle has
506 * the usage count incremented and the caller must use dev_put() to
507 * release it when it is no longer needed. %NULL is returned if no
508 * matching device is found.
511 struct net_device *dev_get_by_name(const char *name)
513 struct net_device *dev;
515 read_lock(&dev_base_lock);
516 dev = __dev_get_by_name(name);
519 read_unlock(&dev_base_lock);
524 * __dev_get_by_index - find a device by its ifindex
525 * @ifindex: index of device
527 * Search for an interface by index. Returns %NULL if the device
528 * is not found or a pointer to the device. The device has not
529 * had its reference counter increased so the caller must be careful
530 * about locking. The caller must hold either the RTNL semaphore
534 struct net_device *__dev_get_by_index(int ifindex)
536 struct hlist_node *p;
538 hlist_for_each(p, dev_index_hash(ifindex)) {
539 struct net_device *dev
540 = hlist_entry(p, struct net_device, index_hlist);
541 if (dev->ifindex == ifindex)
549 * dev_get_by_index - find a device by its ifindex
550 * @ifindex: index of device
552 * Search for an interface by index. Returns NULL if the device
553 * is not found or a pointer to the device. The device returned has
554 * had a reference added and the pointer is safe until the user calls
555 * dev_put to indicate they have finished with it.
558 struct net_device *dev_get_by_index(int ifindex)
560 struct net_device *dev;
562 read_lock(&dev_base_lock);
563 dev = __dev_get_by_index(ifindex);
566 read_unlock(&dev_base_lock);
571 * dev_getbyhwaddr - find a device by its hardware address
572 * @type: media type of device
573 * @ha: hardware address
575 * Search for an interface by MAC address. Returns NULL if the device
576 * is not found or a pointer to the device. The caller must hold the
577 * rtnl semaphore. The returned device has not had its ref count increased
578 * and the caller must therefore be careful about locking
581 * If the API was consistent this would be __dev_get_by_hwaddr
584 struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
586 struct net_device *dev;
590 for (dev = dev_base; dev; dev = dev->next)
591 if (dev->type == type &&
592 !memcmp(dev->dev_addr, ha, dev->addr_len))
597 struct net_device *dev_getfirstbyhwtype(unsigned short type)
599 struct net_device *dev;
602 for (dev = dev_base; dev; dev = dev->next) {
603 if (dev->type == type) {
612 EXPORT_SYMBOL(dev_getfirstbyhwtype);
615 * dev_get_by_flags - find any device with given flags
616 * @if_flags: IFF_* values
617 * @mask: bitmask of bits in if_flags to check
619 * Search for any interface with the given flags. Returns NULL if a device
620 * is not found or a pointer to the device. The device returned has
621 * had a reference added and the pointer is safe until the user calls
622 * dev_put to indicate they have finished with it.
625 struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
627 struct net_device *dev;
629 read_lock(&dev_base_lock);
630 for (dev = dev_base; dev != NULL; dev = dev->next) {
631 if (((dev->flags ^ if_flags) & mask) == 0) {
636 read_unlock(&dev_base_lock);
641 * dev_valid_name - check if name is okay for network device
644 * Network device names need to be valid file names to
645 * to allow sysfs to work
647 int dev_valid_name(const char *name)
649 return !(*name == '\0'
650 || !strcmp(name, ".")
651 || !strcmp(name, "..")
652 || strchr(name, '/'));
656 * dev_alloc_name - allocate a name for a device
658 * @name: name format string
660 * Passed a format string - eg "lt%d" it will try and find a suitable
661 * id. Not efficient for many devices, not called a lot. The caller
662 * must hold the dev_base or rtnl lock while allocating the name and
663 * adding the device in order to avoid duplicates. Returns the number
664 * of the unit assigned or a negative errno code.
667 int dev_alloc_name(struct net_device *dev, const char *name)
672 const int max_netdevices = 8*PAGE_SIZE;
674 struct net_device *d;
676 p = strnchr(name, IFNAMSIZ-1, '%');
679 * Verify the string as this thing may have come from
680 * the user. There must be either one "%d" and no other "%"
683 if (p[1] != 'd' || strchr(p + 2, '%'))
686 /* Use one page as a bit array of possible slots */
687 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
691 for (d = dev_base; d; d = d->next) {
692 if (!sscanf(d->name, name, &i))
694 if (i < 0 || i >= max_netdevices)
697 /* avoid cases where sscanf is not exact inverse of printf */
698 snprintf(buf, sizeof(buf), name, i);
699 if (!strncmp(buf, d->name, IFNAMSIZ))
703 i = find_first_zero_bit(inuse, max_netdevices);
704 free_page((unsigned long) inuse);
707 snprintf(buf, sizeof(buf), name, i);
708 if (!__dev_get_by_name(buf)) {
709 strlcpy(dev->name, buf, IFNAMSIZ);
713 /* It is possible to run out of possible slots
714 * when the name is long and there isn't enough space left
715 * for the digits, or if all bits are used.
722 * dev_change_name - change name of a device
724 * @newname: name (or format string) must be at least IFNAMSIZ
726 * Change name of a device, can pass format strings "eth%d".
729 int dev_change_name(struct net_device *dev, char *newname)
735 if (dev->flags & IFF_UP)
738 if (!dev_valid_name(newname))
741 if (strchr(newname, '%')) {
742 err = dev_alloc_name(dev, newname);
745 strcpy(newname, dev->name);
747 else if (__dev_get_by_name(newname))
750 strlcpy(dev->name, newname, IFNAMSIZ);
752 err = class_device_rename(&dev->class_dev, dev->name);
754 hlist_del(&dev->name_hlist);
755 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
756 notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
763 * netdev_state_change - device changes state
764 * @dev: device to cause notification
766 * Called to indicate a device has changed state. This function calls
767 * the notifier chains for netdev_chain and sends a NEWLINK message
768 * to the routing socket.
770 void netdev_state_change(struct net_device *dev)
772 if (dev->flags & IFF_UP) {
773 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
774 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
779 * dev_load - load a network module
780 * @name: name of interface
782 * If a network interface is not present and the process has suitable
783 * privileges this function loads the module. If module loading is not
784 * available in this kernel then it becomes a nop.
787 void dev_load(const char *name)
789 struct net_device *dev;
791 read_lock(&dev_base_lock);
792 dev = __dev_get_by_name(name);
793 read_unlock(&dev_base_lock);
795 if (!dev && capable(CAP_SYS_MODULE))
796 request_module("%s", name);
799 static int default_rebuild_header(struct sk_buff *skb)
801 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
802 skb->dev ? skb->dev->name : "NULL!!!");
809 * dev_open - prepare an interface for use.
810 * @dev: device to open
812 * Takes a device from down to up state. The device's private open
813 * function is invoked and then the multicast lists are loaded. Finally
814 * the device is moved into the up state and a %NETDEV_UP message is
815 * sent to the netdev notifier chain.
817 * Calling this function on an active interface is a nop. On a failure
818 * a negative errno code is returned.
820 int dev_open(struct net_device *dev)
828 if (dev->flags & IFF_UP)
832 * Is it even present?
834 if (!netif_device_present(dev))
838 * Call device private open method
840 set_bit(__LINK_STATE_START, &dev->state);
842 ret = dev->open(dev);
844 clear_bit(__LINK_STATE_START, &dev->state);
848 * If it went open OK then:
855 dev->flags |= IFF_UP;
858 * Initialize multicasting status
863 * Wakeup transmit queue engine
868 * ... and announce new interface.
870 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
876 * dev_close - shutdown an interface.
877 * @dev: device to shutdown
879 * This function moves an active device into down state. A
880 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
881 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
884 int dev_close(struct net_device *dev)
886 if (!(dev->flags & IFF_UP))
890 * Tell people we are going down, so that they can
891 * prepare to death, when device is still operating.
893 notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
897 clear_bit(__LINK_STATE_START, &dev->state);
899 /* Synchronize to scheduled poll. We cannot touch poll list,
900 * it can be even on different cpu. So just clear netif_running(),
901 * and wait when poll really will happen. Actually, the best place
902 * for this is inside dev->stop() after device stopped its irq
903 * engine, but this requires more changes in devices. */
905 smp_mb__after_clear_bit(); /* Commit netif_running(). */
906 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
908 current->state = TASK_INTERRUPTIBLE;
913 * Call the device specific close. This cannot fail.
914 * Only if device is UP
916 * We allow it to be called even after a DETACH hot-plug
923 * Device is now down.
926 dev->flags &= ~IFF_UP;
929 * Tell people we are down
931 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
938 * Device change register/unregister. These are not inline or static
939 * as we export them to the world.
943 * register_netdevice_notifier - register a network notifier block
946 * Register a notifier to be called when network device events occur.
947 * The notifier passed is linked into the kernel structures and must
948 * not be reused until it has been unregistered. A negative errno code
949 * is returned on a failure.
951 * When registered all registration and up events are replayed
952 * to the new notifier to allow device to have a race free
953 * view of the network device list.
956 int register_netdevice_notifier(struct notifier_block *nb)
958 struct net_device *dev;
962 err = notifier_chain_register(&netdev_chain, nb);
964 for (dev = dev_base; dev; dev = dev->next) {
965 nb->notifier_call(nb, NETDEV_REGISTER, dev);
967 if (dev->flags & IFF_UP)
968 nb->notifier_call(nb, NETDEV_UP, dev);
976 * unregister_netdevice_notifier - unregister a network notifier block
979 * Unregister a notifier previously registered by
980 * register_netdevice_notifier(). The notifier is unlinked into the
981 * kernel structures and may then be reused. A negative errno code
982 * is returned on a failure.
985 int unregister_netdevice_notifier(struct notifier_block *nb)
987 return notifier_chain_unregister(&netdev_chain, nb);
991 * call_netdevice_notifiers - call all network notifier blocks
992 * @val: value passed unmodified to notifier function
993 * @v: pointer passed unmodified to notifier function
995 * Call all network notifier blocks. Parameters and return value
996 * are as for notifier_call_chain().
999 int call_netdevice_notifiers(unsigned long val, void *v)
1001 return notifier_call_chain(&netdev_chain, val, v);
1004 /* When > 0 there are consumers of rx skb time stamps */
1005 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1007 void net_enable_timestamp(void)
1009 atomic_inc(&netstamp_needed);
1012 void net_disable_timestamp(void)
1014 atomic_dec(&netstamp_needed);
1017 static inline void net_timestamp(struct timeval *stamp)
1019 if (atomic_read(&netstamp_needed))
1020 do_gettimeofday(stamp);
1028 * Support routine. Sends outgoing frames to any network
1029 * taps currently in use.
1032 void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1034 struct packet_type *ptype;
1035 net_timestamp(&skb->stamp);
1038 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1039 /* Never send packets back to the socket
1040 * they originated from - MvS (miquels@drinkel.ow.org)
1042 if ((ptype->dev == dev || !ptype->dev) &&
1043 (ptype->af_packet_priv == NULL ||
1044 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1045 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1049 /* skb->nh should be correctly
1050 set by sender, so that the second statement is
1051 just protection against buggy protocols.
1053 skb2->mac.raw = skb2->data;
1055 if (skb2->nh.raw < skb2->data ||
1056 skb2->nh.raw > skb2->tail) {
1057 if (net_ratelimit())
1058 printk(KERN_CRIT "protocol %04x is "
1060 skb2->protocol, dev->name);
1061 skb2->nh.raw = skb2->data;
1064 skb2->h.raw = skb2->nh.raw;
1065 skb2->pkt_type = PACKET_OUTGOING;
1066 ptype->func(skb2, skb->dev, ptype);
1073 * Invalidate hardware checksum when packet is to be mangled, and
1074 * complete checksum manually on outgoing path.
1076 int skb_checksum_help(struct sk_buff *skb, int inward)
1079 int ret = 0, offset = skb->h.raw - skb->data;
1082 skb->ip_summed = CHECKSUM_NONE;
1086 if (skb_cloned(skb)) {
1087 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1092 if (offset > (int)skb->len)
1094 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1096 offset = skb->tail - skb->h.raw;
1099 if (skb->csum + 2 > offset)
1102 *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
1103 skb->ip_summed = CHECKSUM_NONE;
1108 #ifdef CONFIG_HIGHMEM
1109 /* Actually, we should eliminate this check as soon as we know, that:
1110 * 1. IOMMU is present and allows to map all the memory.
1111 * 2. No high memory really exists on this machine.
1114 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1118 if (dev->features & NETIF_F_HIGHDMA)
1121 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1122 if (skb_shinfo(skb)->frags[i].page >= highmem_start_page)
1128 #define illegal_highdma(dev, skb) (0)
1131 extern void skb_release_data(struct sk_buff *);
1133 /* Keep head the same: replace data */
1134 int __skb_linearize(struct sk_buff *skb, int gfp_mask)
1139 struct skb_shared_info *ninfo;
1140 int headerlen = skb->data - skb->head;
1141 int expand = (skb->tail + skb->data_len) - skb->end;
1143 if (skb_shared(skb))
1149 size = skb->end - skb->head + expand;
1150 size = SKB_DATA_ALIGN(size);
1151 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
1155 /* Copy entire thing */
1156 if (skb_copy_bits(skb, -headerlen, data, headerlen + skb->len))
1160 ninfo = (struct skb_shared_info*)(data + size);
1161 atomic_set(&ninfo->dataref, 1);
1162 ninfo->tso_size = skb_shinfo(skb)->tso_size;
1163 ninfo->tso_segs = skb_shinfo(skb)->tso_segs;
1164 ninfo->nr_frags = 0;
1165 ninfo->frag_list = NULL;
1167 /* Offset between the two in bytes */
1168 offset = data - skb->head;
1170 /* Free old data. */
1171 skb_release_data(skb);
1174 skb->end = data + size;
1176 /* Set up new pointers */
1177 skb->h.raw += offset;
1178 skb->nh.raw += offset;
1179 skb->mac.raw += offset;
1180 skb->tail += offset;
1181 skb->data += offset;
1183 /* We are no longer a clone, even if we were. */
1186 skb->tail += skb->data_len;
1191 #define HARD_TX_LOCK(dev, cpu) { \
1192 if ((dev->features & NETIF_F_LLTX) == 0) { \
1193 spin_lock(&dev->xmit_lock); \
1194 dev->xmit_lock_owner = cpu; \
1198 #define HARD_TX_UNLOCK(dev) { \
1199 if ((dev->features & NETIF_F_LLTX) == 0) { \
1200 dev->xmit_lock_owner = -1; \
1201 spin_unlock(&dev->xmit_lock); \
1206 * dev_queue_xmit - transmit a buffer
1207 * @skb: buffer to transmit
1209 * Queue a buffer for transmission to a network device. The caller must
1210 * have set the device and priority and built the buffer before calling
1211 * this function. The function can be called from an interrupt.
1213 * A negative errno code is returned on a failure. A success does not
1214 * guarantee the frame will be transmitted as it may be dropped due
1215 * to congestion or traffic shaping.
1218 int dev_queue_xmit(struct sk_buff *skb)
1220 struct net_device *dev = skb->dev;
1224 if (skb_shinfo(skb)->frag_list &&
1225 !(dev->features & NETIF_F_FRAGLIST) &&
1226 __skb_linearize(skb, GFP_ATOMIC))
1229 /* Fragmented skb is linearized if device does not support SG,
1230 * or if at least one of fragments is in highmem and device
1231 * does not support DMA from it.
1233 if (skb_shinfo(skb)->nr_frags &&
1234 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1235 __skb_linearize(skb, GFP_ATOMIC))
1238 /* If packet is not checksummed and device does not support
1239 * checksumming for this protocol, complete checksumming here.
1241 if (skb->ip_summed == CHECKSUM_HW &&
1242 (!(dev->features & (NETIF_F_HW_CSUM | NETIF_F_NO_CSUM)) &&
1243 (!(dev->features & NETIF_F_IP_CSUM) ||
1244 skb->protocol != htons(ETH_P_IP))))
1245 if (skb_checksum_help(skb, 0))
1248 /* Disable soft irqs for various locks below. Also
1249 * stops preemption for RCU.
1253 /* Updates of qdisc are serialized by queue_lock.
1254 * The struct Qdisc which is pointed to by qdisc is now a
1255 * rcu structure - it may be accessed without acquiring
1256 * a lock (but the structure may be stale.) The freeing of the
1257 * qdisc will be deferred until it's known that there are no
1258 * more references to it.
1260 * If the qdisc has an enqueue function, we still need to
1261 * hold the queue_lock before calling it, since queue_lock
1262 * also serializes access to the device queue.
1265 q = rcu_dereference(dev->qdisc);
1266 #ifdef CONFIG_NET_CLS_ACT
1267 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1270 /* Grab device queue */
1271 spin_lock(&dev->queue_lock);
1273 rc = q->enqueue(skb, q);
1277 spin_unlock(&dev->queue_lock);
1278 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1282 /* The device has no queue. Common case for software devices:
1283 loopback, all the sorts of tunnels...
1285 Really, it is unlikely that xmit_lock protection is necessary here.
1286 (f.e. loopback and IP tunnels are clean ignoring statistics
1288 However, it is possible, that they rely on protection
1291 Check this and shot the lock. It is not prone from deadlocks.
1292 Either shot noqueue qdisc, it is even simpler 8)
1294 if (dev->flags & IFF_UP) {
1295 int cpu = smp_processor_id(); /* ok because BHs are off */
1297 if (dev->xmit_lock_owner != cpu) {
1299 HARD_TX_LOCK(dev, cpu);
1301 if (!netif_queue_stopped(dev)) {
1303 dev_queue_xmit_nit(skb, dev);
1306 if (!dev->hard_start_xmit(skb, dev)) {
1307 HARD_TX_UNLOCK(dev);
1311 HARD_TX_UNLOCK(dev);
1312 if (net_ratelimit())
1313 printk(KERN_CRIT "Virtual device %s asks to "
1314 "queue packet!\n", dev->name);
1316 /* Recursion is detected! It is possible,
1318 if (net_ratelimit())
1319 printk(KERN_CRIT "Dead loop on virtual device "
1320 "%s, fix it urgently!\n", dev->name);
1336 /*=======================================================================
1338 =======================================================================*/
1340 int netdev_max_backlog = 300;
1341 int weight_p = 64; /* old backlog weight */
1342 /* These numbers are selected based on intuition and some
1343 * experimentatiom, if you have more scientific way of doing this
1344 * please go ahead and fix things.
1346 int no_cong_thresh = 10;
1351 DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1354 static void get_sample_stats(int cpu)
1360 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
1361 int blog = sd->input_pkt_queue.qlen;
1362 int avg_blog = sd->avg_blog;
1364 avg_blog = (avg_blog >> 1) + (blog >> 1);
1366 if (avg_blog > mod_cong) {
1367 /* Above moderate congestion levels. */
1368 sd->cng_level = NET_RX_CN_HIGH;
1371 rq = rd % netdev_max_backlog;
1372 if (rq < avg_blog) /* unlucky bastard */
1373 sd->cng_level = NET_RX_DROP;
1375 } else if (avg_blog > lo_cong) {
1376 sd->cng_level = NET_RX_CN_MOD;
1379 rq = rd % netdev_max_backlog;
1380 if (rq < avg_blog) /* unlucky bastard */
1381 sd->cng_level = NET_RX_CN_HIGH;
1383 } else if (avg_blog > no_cong)
1384 sd->cng_level = NET_RX_CN_LOW;
1385 else /* no congestion */
1386 sd->cng_level = NET_RX_SUCCESS;
1388 sd->avg_blog = avg_blog;
1391 #ifdef OFFLINE_SAMPLE
1392 static void sample_queue(unsigned long dummy)
1394 /* 10 ms 0r 1ms -- i don't care -- JHS */
1396 int cpu = smp_processor_id();
1398 get_sample_stats(cpu);
1399 next_tick += jiffies;
1400 mod_timer(&samp_timer, next_tick);
1406 * netif_rx - post buffer to the network code
1407 * @skb: buffer to post
1409 * This function receives a packet from a device driver and queues it for
1410 * the upper (protocol) levels to process. It always succeeds. The buffer
1411 * may be dropped during processing for congestion control or by the
1415 * NET_RX_SUCCESS (no congestion)
1416 * NET_RX_CN_LOW (low congestion)
1417 * NET_RX_CN_MOD (moderate congestion)
1418 * NET_RX_CN_HIGH (high congestion)
1419 * NET_RX_DROP (packet was dropped)
1423 int netif_rx(struct sk_buff *skb)
1426 struct softnet_data *queue;
1427 unsigned long flags;
1429 #ifdef CONFIG_NETPOLL
1430 if (skb->dev->netpoll_rx && netpoll_rx(skb)) {
1436 if (!skb->stamp.tv_sec)
1437 net_timestamp(&skb->stamp);
1440 * The code is rearranged so that the path is the most
1441 * short when CPU is congested, but is still operating.
1443 local_irq_save(flags);
1444 this_cpu = smp_processor_id();
1445 queue = &__get_cpu_var(softnet_data);
1447 __get_cpu_var(netdev_rx_stat).total++;
1448 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1449 if (queue->input_pkt_queue.qlen) {
1450 if (queue->throttle)
1455 __skb_queue_tail(&queue->input_pkt_queue, skb);
1456 #ifndef OFFLINE_SAMPLE
1457 get_sample_stats(this_cpu);
1459 local_irq_restore(flags);
1460 return queue->cng_level;
1463 if (queue->throttle)
1464 queue->throttle = 0;
1466 netif_rx_schedule(&queue->backlog_dev);
1470 if (!queue->throttle) {
1471 queue->throttle = 1;
1472 __get_cpu_var(netdev_rx_stat).throttled++;
1476 __get_cpu_var(netdev_rx_stat).dropped++;
1477 local_irq_restore(flags);
1483 int netif_rx_ni(struct sk_buff *skb)
1488 err = netif_rx(skb);
1489 if (softirq_pending(smp_processor_id()))
1496 EXPORT_SYMBOL(netif_rx_ni);
1498 static __inline__ void skb_bond(struct sk_buff *skb)
1500 struct net_device *dev = skb->dev;
1503 skb->real_dev = skb->dev;
1504 skb->dev = dev->master;
1508 static void net_tx_action(struct softirq_action *h)
1510 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1512 if (sd->completion_queue) {
1513 struct sk_buff *clist;
1515 local_irq_disable();
1516 clist = sd->completion_queue;
1517 sd->completion_queue = NULL;
1521 struct sk_buff *skb = clist;
1522 clist = clist->next;
1524 BUG_TRAP(!atomic_read(&skb->users));
1529 if (sd->output_queue) {
1530 struct net_device *head;
1532 local_irq_disable();
1533 head = sd->output_queue;
1534 sd->output_queue = NULL;
1538 struct net_device *dev = head;
1539 head = head->next_sched;
1541 smp_mb__before_clear_bit();
1542 clear_bit(__LINK_STATE_SCHED, &dev->state);
1544 if (spin_trylock(&dev->queue_lock)) {
1546 spin_unlock(&dev->queue_lock);
1548 netif_schedule(dev);
1554 static __inline__ int deliver_skb(struct sk_buff *skb,
1555 struct packet_type *pt_prev)
1557 atomic_inc(&skb->users);
1558 return pt_prev->func(skb, skb->dev, pt_prev);
1561 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1562 int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1564 static __inline__ int handle_bridge(struct sk_buff **pskb,
1565 struct packet_type **pt_prev, int *ret)
1567 struct net_bridge_port *port;
1569 if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1570 (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1574 *ret = deliver_skb(*pskb, *pt_prev);
1578 return br_handle_frame_hook(port, pskb);
1581 #define handle_bridge(skb, pt_prev, ret) (0)
1584 #ifdef CONFIG_NET_CLS_ACT
1585 /* TODO: Maybe we should just force sch_ingress to be compiled in
1586 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1587 * a compare and 2 stores extra right now if we dont have it on
1588 * but have CONFIG_NET_CLS_ACT
1589 * NOTE: This doesnt stop any functionality; if you dont have
1590 * the ingress scheduler, you just cant add policies on ingress.
1593 int ing_filter(struct sk_buff *skb)
1596 struct net_device *dev = skb->dev;
1597 int result = TC_ACT_OK;
1599 if (dev->qdisc_ingress) {
1600 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1601 if (MAX_RED_LOOP < ttl++) {
1602 printk("Redir loop detected Dropping packet (%s->%s)\n",
1603 skb->input_dev?skb->input_dev->name:"??",skb->dev->name);
1607 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1609 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1610 if (NULL == skb->input_dev) {
1611 skb->input_dev = skb->dev;
1612 printk("ing_filter: fixed %s out %s\n",skb->input_dev->name,skb->dev->name);
1614 spin_lock(&dev->ingress_lock);
1615 if ((q = dev->qdisc_ingress) != NULL)
1616 result = q->enqueue(skb, q);
1617 spin_unlock(&dev->ingress_lock);
1625 int netif_receive_skb(struct sk_buff *skb)
1627 struct packet_type *ptype, *pt_prev;
1628 int ret = NET_RX_DROP;
1629 unsigned short type;
1631 #ifdef CONFIG_NETPOLL
1632 if (skb->dev->netpoll_rx && skb->dev->poll && netpoll_rx(skb)) {
1638 if (!skb->stamp.tv_sec)
1639 net_timestamp(&skb->stamp);
1643 __get_cpu_var(netdev_rx_stat).total++;
1645 skb->h.raw = skb->nh.raw = skb->data;
1646 skb->mac_len = skb->nh.raw - skb->mac.raw;
1652 #ifdef CONFIG_NET_CLS_ACT
1653 if (skb->tc_verd & TC_NCLS) {
1654 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1659 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1660 if (!ptype->dev || ptype->dev == skb->dev) {
1662 ret = deliver_skb(skb, pt_prev);
1667 #ifdef CONFIG_NET_CLS_ACT
1669 ret = deliver_skb(skb, pt_prev);
1670 pt_prev = NULL; /* noone else should process this after*/
1672 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1675 ret = ing_filter(skb);
1677 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1686 handle_diverter(skb);
1688 if (handle_bridge(&skb, &pt_prev, &ret))
1691 type = skb->protocol;
1692 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1693 if (ptype->type == type &&
1694 (!ptype->dev || ptype->dev == skb->dev)) {
1696 ret = deliver_skb(skb, pt_prev);
1702 ret = pt_prev->func(skb, skb->dev, pt_prev);
1705 /* Jamal, now you will not able to escape explaining
1706 * me how you were going to use this. :-)
1716 static int process_backlog(struct net_device *backlog_dev, int *budget)
1719 int quota = min(backlog_dev->quota, *budget);
1720 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1721 unsigned long start_time = jiffies;
1724 struct sk_buff *skb;
1725 struct net_device *dev;
1727 local_irq_disable();
1728 skb = __skb_dequeue(&queue->input_pkt_queue);
1735 netif_receive_skb(skb);
1741 if (work >= quota || jiffies - start_time > 1)
1746 backlog_dev->quota -= work;
1751 backlog_dev->quota -= work;
1754 list_del(&backlog_dev->poll_list);
1755 smp_mb__before_clear_bit();
1756 netif_poll_enable(backlog_dev);
1758 if (queue->throttle)
1759 queue->throttle = 0;
1764 static void net_rx_action(struct softirq_action *h)
1766 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1767 unsigned long start_time = jiffies;
1768 int budget = netdev_max_backlog;
1771 local_irq_disable();
1773 while (!list_empty(&queue->poll_list)) {
1774 struct net_device *dev;
1776 if (budget <= 0 || jiffies - start_time > 1)
1781 dev = list_entry(queue->poll_list.next,
1782 struct net_device, poll_list);
1784 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
1785 local_irq_disable();
1786 list_del(&dev->poll_list);
1787 list_add_tail(&dev->poll_list, &queue->poll_list);
1789 dev->quota += dev->weight;
1791 dev->quota = dev->weight;
1794 local_irq_disable();
1802 __get_cpu_var(netdev_rx_stat).time_squeeze++;
1803 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1807 static gifconf_func_t * gifconf_list [NPROTO];
1810 * register_gifconf - register a SIOCGIF handler
1811 * @family: Address family
1812 * @gifconf: Function handler
1814 * Register protocol dependent address dumping routines. The handler
1815 * that is passed must not be freed or reused until it has been replaced
1816 * by another handler.
1818 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1820 if (family >= NPROTO)
1822 gifconf_list[family] = gifconf;
1828 * Map an interface index to its name (SIOCGIFNAME)
1832 * We need this ioctl for efficient implementation of the
1833 * if_indextoname() function required by the IPv6 API. Without
1834 * it, we would have to search all the interfaces to find a
1838 static int dev_ifname(struct ifreq __user *arg)
1840 struct net_device *dev;
1844 * Fetch the caller's info block.
1847 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
1850 read_lock(&dev_base_lock);
1851 dev = __dev_get_by_index(ifr.ifr_ifindex);
1853 read_unlock(&dev_base_lock);
1857 strcpy(ifr.ifr_name, dev->name);
1858 read_unlock(&dev_base_lock);
1860 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1866 * Perform a SIOCGIFCONF call. This structure will change
1867 * size eventually, and there is nothing I can do about it.
1868 * Thus we will need a 'compatibility mode'.
1871 static int dev_ifconf(char __user *arg)
1874 struct net_device *dev;
1881 * Fetch the caller's info block.
1884 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
1891 * Loop over the interfaces, and write an info block for each.
1895 for (dev = dev_base; dev; dev = dev->next) {
1896 for (i = 0; i < NPROTO; i++) {
1897 if (gifconf_list[i]) {
1900 done = gifconf_list[i](dev, NULL, 0);
1902 done = gifconf_list[i](dev, pos + total,
1912 * All done. Write the updated control block back to the caller.
1914 ifc.ifc_len = total;
1917 * Both BSD and Solaris return 0 here, so we do too.
1919 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
1922 #ifdef CONFIG_PROC_FS
1924 * This is invoked by the /proc filesystem handler to display a device
1927 static __inline__ struct net_device *dev_get_idx(loff_t pos)
1929 struct net_device *dev;
1932 for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
1934 return i == pos ? dev : NULL;
1937 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
1939 read_lock(&dev_base_lock);
1940 return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
1943 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1946 return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
1949 void dev_seq_stop(struct seq_file *seq, void *v)
1951 read_unlock(&dev_base_lock);
1954 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
1956 if (dev->get_stats) {
1957 struct net_device_stats *stats = dev->get_stats(dev);
1959 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
1960 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
1961 dev->name, stats->rx_bytes, stats->rx_packets,
1963 stats->rx_dropped + stats->rx_missed_errors,
1964 stats->rx_fifo_errors,
1965 stats->rx_length_errors + stats->rx_over_errors +
1966 stats->rx_crc_errors + stats->rx_frame_errors,
1967 stats->rx_compressed, stats->multicast,
1968 stats->tx_bytes, stats->tx_packets,
1969 stats->tx_errors, stats->tx_dropped,
1970 stats->tx_fifo_errors, stats->collisions,
1971 stats->tx_carrier_errors +
1972 stats->tx_aborted_errors +
1973 stats->tx_window_errors +
1974 stats->tx_heartbeat_errors,
1975 stats->tx_compressed);
1977 seq_printf(seq, "%6s: No statistics available.\n", dev->name);
1981 * Called from the PROCfs module. This now uses the new arbitrary sized
1982 * /proc/net interface to create /proc/net/dev
1984 static int dev_seq_show(struct seq_file *seq, void *v)
1986 if (v == SEQ_START_TOKEN)
1987 seq_puts(seq, "Inter-| Receive "
1989 " face |bytes packets errs drop fifo frame "
1990 "compressed multicast|bytes packets errs "
1991 "drop fifo colls carrier compressed\n");
1993 dev_seq_printf_stats(seq, v);
1997 static struct netif_rx_stats *softnet_get_online(loff_t *pos)
1999 struct netif_rx_stats *rc = NULL;
2001 while (*pos < NR_CPUS)
2002 if (cpu_online(*pos)) {
2003 rc = &per_cpu(netdev_rx_stat, *pos);
2010 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2012 return softnet_get_online(pos);
2015 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2018 return softnet_get_online(pos);
2021 static void softnet_seq_stop(struct seq_file *seq, void *v)
2025 static int softnet_seq_show(struct seq_file *seq, void *v)
2027 struct netif_rx_stats *s = v;
2029 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2030 s->total, s->dropped, s->time_squeeze, s->throttled,
2031 s->fastroute_hit, s->fastroute_success, s->fastroute_defer,
2032 s->fastroute_deferred_out,
2034 s->fastroute_latency_reduction
2042 static struct seq_operations dev_seq_ops = {
2043 .start = dev_seq_start,
2044 .next = dev_seq_next,
2045 .stop = dev_seq_stop,
2046 .show = dev_seq_show,
2049 static int dev_seq_open(struct inode *inode, struct file *file)
2051 return seq_open(file, &dev_seq_ops);
2054 static struct file_operations dev_seq_fops = {
2055 .owner = THIS_MODULE,
2056 .open = dev_seq_open,
2058 .llseek = seq_lseek,
2059 .release = seq_release,
2062 static struct seq_operations softnet_seq_ops = {
2063 .start = softnet_seq_start,
2064 .next = softnet_seq_next,
2065 .stop = softnet_seq_stop,
2066 .show = softnet_seq_show,
2069 static int softnet_seq_open(struct inode *inode, struct file *file)
2071 return seq_open(file, &softnet_seq_ops);
2074 static struct file_operations softnet_seq_fops = {
2075 .owner = THIS_MODULE,
2076 .open = softnet_seq_open,
2078 .llseek = seq_lseek,
2079 .release = seq_release,
2083 extern int wireless_proc_init(void);
2085 #define wireless_proc_init() 0
2088 static int __init dev_proc_init(void)
2092 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2094 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2096 if (wireless_proc_init())
2102 proc_net_remove("softnet_stat");
2104 proc_net_remove("dev");
2108 #define dev_proc_init() 0
2109 #endif /* CONFIG_PROC_FS */
2113 * netdev_set_master - set up master/slave pair
2114 * @slave: slave device
2115 * @master: new master device
2117 * Changes the master device of the slave. Pass %NULL to break the
2118 * bonding. The caller must hold the RTNL semaphore. On a failure
2119 * a negative errno code is returned. On success the reference counts
2120 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2121 * function returns zero.
2123 int netdev_set_master(struct net_device *slave, struct net_device *master)
2125 struct net_device *old = slave->master;
2135 slave->master = master;
2143 slave->flags |= IFF_SLAVE;
2145 slave->flags &= ~IFF_SLAVE;
2147 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2152 * dev_set_promiscuity - update promiscuity count on a device
2156 * Add or remove promsicuity from a device. While the count in the device
2157 * remains above zero the interface remains promiscuous. Once it hits zero
2158 * the device reverts back to normal filtering operation. A negative inc
2159 * value is used to drop promiscuity on the device.
2161 void dev_set_promiscuity(struct net_device *dev, int inc)
2163 unsigned short old_flags = dev->flags;
2165 dev->flags |= IFF_PROMISC;
2166 if ((dev->promiscuity += inc) == 0)
2167 dev->flags &= ~IFF_PROMISC;
2168 if (dev->flags ^ old_flags) {
2170 printk(KERN_INFO "device %s %s promiscuous mode\n",
2171 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2177 * dev_set_allmulti - update allmulti count on a device
2181 * Add or remove reception of all multicast frames to a device. While the
2182 * count in the device remains above zero the interface remains listening
2183 * to all interfaces. Once it hits zero the device reverts back to normal
2184 * filtering operation. A negative @inc value is used to drop the counter
2185 * when releasing a resource needing all multicasts.
2188 void dev_set_allmulti(struct net_device *dev, int inc)
2190 unsigned short old_flags = dev->flags;
2192 dev->flags |= IFF_ALLMULTI;
2193 if ((dev->allmulti += inc) == 0)
2194 dev->flags &= ~IFF_ALLMULTI;
2195 if (dev->flags ^ old_flags)
2199 unsigned dev_get_flags(const struct net_device *dev)
2203 flags = (dev->flags & ~(IFF_PROMISC |
2206 (dev->gflags & (IFF_PROMISC |
2209 if (netif_running(dev) && netif_carrier_ok(dev))
2210 flags |= IFF_RUNNING;
2215 int dev_change_flags(struct net_device *dev, unsigned flags)
2218 int old_flags = dev->flags;
2221 * Set the flags on our device.
2224 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2225 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2227 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2231 * Load in the correct multicast list now the flags have changed.
2237 * Have we downed the interface. We handle IFF_UP ourselves
2238 * according to user attempts to set it, rather than blindly
2243 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2244 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2250 if (dev->flags & IFF_UP &&
2251 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2253 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
2255 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2256 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2257 dev->gflags ^= IFF_PROMISC;
2258 dev_set_promiscuity(dev, inc);
2261 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2262 is important. Some (broken) drivers set IFF_PROMISC, when
2263 IFF_ALLMULTI is requested not asking us and not reporting.
2265 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2266 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2267 dev->gflags ^= IFF_ALLMULTI;
2268 dev_set_allmulti(dev, inc);
2271 if (old_flags ^ dev->flags)
2272 rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2277 int dev_set_mtu(struct net_device *dev, int new_mtu)
2281 if (new_mtu == dev->mtu)
2284 /* MTU must be positive. */
2288 if (!netif_device_present(dev))
2292 if (dev->change_mtu)
2293 err = dev->change_mtu(dev, new_mtu);
2296 if (!err && dev->flags & IFF_UP)
2297 notifier_call_chain(&netdev_chain,
2298 NETDEV_CHANGEMTU, dev);
2304 * Perform the SIOCxIFxxx calls.
2306 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2309 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2315 case SIOCGIFFLAGS: /* Get interface flags */
2316 ifr->ifr_flags = dev_get_flags(dev);
2319 case SIOCSIFFLAGS: /* Set interface flags */
2320 return dev_change_flags(dev, ifr->ifr_flags);
2322 case SIOCGIFMETRIC: /* Get the metric on the interface
2323 (currently unused) */
2324 ifr->ifr_metric = 0;
2327 case SIOCSIFMETRIC: /* Set the metric on the interface
2328 (currently unused) */
2331 case SIOCGIFMTU: /* Get the MTU of a device */
2332 ifr->ifr_mtu = dev->mtu;
2335 case SIOCSIFMTU: /* Set the MTU of a device */
2336 return dev_set_mtu(dev, ifr->ifr_mtu);
2340 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2342 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2343 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2344 ifr->ifr_hwaddr.sa_family = dev->type;
2348 if (!dev->set_mac_address)
2350 if (ifr->ifr_hwaddr.sa_family != dev->type)
2352 if (!netif_device_present(dev))
2354 err = dev->set_mac_address(dev, &ifr->ifr_hwaddr);
2356 notifier_call_chain(&netdev_chain,
2357 NETDEV_CHANGEADDR, dev);
2360 case SIOCSIFHWBROADCAST:
2361 if (ifr->ifr_hwaddr.sa_family != dev->type)
2363 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2364 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2365 notifier_call_chain(&netdev_chain,
2366 NETDEV_CHANGEADDR, dev);
2370 ifr->ifr_map.mem_start = dev->mem_start;
2371 ifr->ifr_map.mem_end = dev->mem_end;
2372 ifr->ifr_map.base_addr = dev->base_addr;
2373 ifr->ifr_map.irq = dev->irq;
2374 ifr->ifr_map.dma = dev->dma;
2375 ifr->ifr_map.port = dev->if_port;
2379 if (dev->set_config) {
2380 if (!netif_device_present(dev))
2382 return dev->set_config(dev, &ifr->ifr_map);
2387 if (!dev->set_multicast_list ||
2388 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2390 if (!netif_device_present(dev))
2392 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2396 if (!dev->set_multicast_list ||
2397 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2399 if (!netif_device_present(dev))
2401 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2405 ifr->ifr_ifindex = dev->ifindex;
2409 ifr->ifr_qlen = dev->tx_queue_len;
2413 if (ifr->ifr_qlen < 0)
2415 dev->tx_queue_len = ifr->ifr_qlen;
2419 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2420 return dev_change_name(dev, ifr->ifr_newname);
2423 * Unknown or private ioctl
2427 if ((cmd >= SIOCDEVPRIVATE &&
2428 cmd <= SIOCDEVPRIVATE + 15) ||
2429 cmd == SIOCBONDENSLAVE ||
2430 cmd == SIOCBONDRELEASE ||
2431 cmd == SIOCBONDSETHWADDR ||
2432 cmd == SIOCBONDSLAVEINFOQUERY ||
2433 cmd == SIOCBONDINFOQUERY ||
2434 cmd == SIOCBONDCHANGEACTIVE ||
2435 cmd == SIOCGMIIPHY ||
2436 cmd == SIOCGMIIREG ||
2437 cmd == SIOCSMIIREG ||
2438 cmd == SIOCBRADDIF ||
2439 cmd == SIOCBRDELIF ||
2440 cmd == SIOCWANDEV) {
2442 if (dev->do_ioctl) {
2443 if (netif_device_present(dev))
2444 err = dev->do_ioctl(dev, ifr,
2457 * This function handles all "interface"-type I/O control requests. The actual
2458 * 'doing' part of this is dev_ifsioc above.
2462 * dev_ioctl - network device ioctl
2463 * @cmd: command to issue
2464 * @arg: pointer to a struct ifreq in user space
2466 * Issue ioctl functions to devices. This is normally called by the
2467 * user space syscall interfaces but can sometimes be useful for
2468 * other purposes. The return value is the return from the syscall if
2469 * positive or a negative errno code on error.
2472 int dev_ioctl(unsigned int cmd, void __user *arg)
2478 /* One special case: SIOCGIFCONF takes ifconf argument
2479 and requires shared lock, because it sleeps writing
2483 if (cmd == SIOCGIFCONF) {
2485 ret = dev_ifconf((char __user *) arg);
2489 if (cmd == SIOCGIFNAME)
2490 return dev_ifname((struct ifreq __user *)arg);
2492 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2495 ifr.ifr_name[IFNAMSIZ-1] = 0;
2497 colon = strchr(ifr.ifr_name, ':');
2502 * See which interface the caller is talking about.
2507 * These ioctl calls:
2508 * - can be done by all.
2509 * - atomic and do not require locking.
2520 dev_load(ifr.ifr_name);
2521 read_lock(&dev_base_lock);
2522 ret = dev_ifsioc(&ifr, cmd);
2523 read_unlock(&dev_base_lock);
2527 if (copy_to_user(arg, &ifr,
2528 sizeof(struct ifreq)))
2534 dev_load(ifr.ifr_name);
2536 ret = dev_ethtool(&ifr);
2541 if (copy_to_user(arg, &ifr,
2542 sizeof(struct ifreq)))
2548 * These ioctl calls:
2549 * - require superuser power.
2550 * - require strict serialization.
2556 if (!capable(CAP_NET_ADMIN))
2558 dev_load(ifr.ifr_name);
2560 ret = dev_ifsioc(&ifr, cmd);
2565 if (copy_to_user(arg, &ifr,
2566 sizeof(struct ifreq)))
2572 * These ioctl calls:
2573 * - require superuser power.
2574 * - require strict serialization.
2575 * - do not return a value
2585 case SIOCSIFHWBROADCAST:
2588 case SIOCBONDENSLAVE:
2589 case SIOCBONDRELEASE:
2590 case SIOCBONDSETHWADDR:
2591 case SIOCBONDSLAVEINFOQUERY:
2592 case SIOCBONDINFOQUERY:
2593 case SIOCBONDCHANGEACTIVE:
2596 if (!capable(CAP_NET_ADMIN))
2598 dev_load(ifr.ifr_name);
2600 ret = dev_ifsioc(&ifr, cmd);
2605 /* Get the per device memory space. We can add this but
2606 * currently do not support it */
2608 /* Set the per device memory buffer space.
2609 * Not applicable in our case */
2614 * Unknown or private ioctl.
2617 if (cmd == SIOCWANDEV ||
2618 (cmd >= SIOCDEVPRIVATE &&
2619 cmd <= SIOCDEVPRIVATE + 15)) {
2620 dev_load(ifr.ifr_name);
2622 ret = dev_ifsioc(&ifr, cmd);
2624 if (!ret && copy_to_user(arg, &ifr,
2625 sizeof(struct ifreq)))
2630 /* Take care of Wireless Extensions */
2631 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2632 /* If command is `set a parameter', or
2633 * `get the encoding parameters', check if
2634 * the user has the right to do it */
2635 if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE) {
2636 if (!capable(CAP_NET_ADMIN))
2639 dev_load(ifr.ifr_name);
2641 /* Follow me in net/core/wireless.c */
2642 ret = wireless_process_ioctl(&ifr, cmd);
2644 if (IW_IS_GET(cmd) &&
2645 copy_to_user(arg, &ifr,
2646 sizeof(struct ifreq)))
2650 #endif /* WIRELESS_EXT */
2657 * dev_new_index - allocate an ifindex
2659 * Returns a suitable unique value for a new device interface
2660 * number. The caller must hold the rtnl semaphore or the
2661 * dev_base_lock to be sure it remains unique.
2663 static int dev_new_index(void)
2669 if (!__dev_get_by_index(ifindex))
2674 static int dev_boot_phase = 1;
2676 /* Delayed registration/unregisteration */
2677 static spinlock_t net_todo_list_lock = SPIN_LOCK_UNLOCKED;
2678 static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
2680 static inline void net_set_todo(struct net_device *dev)
2682 spin_lock(&net_todo_list_lock);
2683 list_add_tail(&dev->todo_list, &net_todo_list);
2684 spin_unlock(&net_todo_list_lock);
2688 * register_netdevice - register a network device
2689 * @dev: device to register
2691 * Take a completed network device structure and add it to the kernel
2692 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2693 * chain. 0 is returned on success. A negative errno code is returned
2694 * on a failure to set up the device, or if the name is a duplicate.
2696 * Callers must hold the rtnl semaphore. See the comment at the
2697 * end of Space.c for details about the locking. You may want
2698 * register_netdev() instead of this.
2701 * The locking appears insufficient to guarantee two parallel registers
2702 * will not get the same name.
2705 int register_netdevice(struct net_device *dev)
2707 struct hlist_head *head;
2708 struct hlist_node *p;
2711 BUG_ON(dev_boot_phase);
2714 /* When net_device's are persistent, this will be fatal. */
2715 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
2717 spin_lock_init(&dev->queue_lock);
2718 spin_lock_init(&dev->xmit_lock);
2719 dev->xmit_lock_owner = -1;
2720 #ifdef CONFIG_NET_CLS_ACT
2721 spin_lock_init(&dev->ingress_lock);
2724 ret = alloc_divert_blk(dev);
2730 /* Init, if this function is available */
2732 ret = dev->init(dev);
2740 if (!dev_valid_name(dev->name)) {
2745 dev->ifindex = dev_new_index();
2746 if (dev->iflink == -1)
2747 dev->iflink = dev->ifindex;
2749 /* Check for existence of name */
2750 head = dev_name_hash(dev->name);
2751 hlist_for_each(p, head) {
2752 struct net_device *d
2753 = hlist_entry(p, struct net_device, name_hlist);
2754 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
2760 /* Fix illegal SG+CSUM combinations. */
2761 if ((dev->features & NETIF_F_SG) &&
2762 !(dev->features & (NETIF_F_IP_CSUM |
2764 NETIF_F_HW_CSUM))) {
2765 printk("%s: Dropping NETIF_F_SG since no checksum feature.\n",
2767 dev->features &= ~NETIF_F_SG;
2770 /* TSO requires that SG is present as well. */
2771 if ((dev->features & NETIF_F_TSO) &&
2772 !(dev->features & NETIF_F_SG)) {
2773 printk("%s: Dropping NETIF_F_TSO since no SG feature.\n",
2775 dev->features &= ~NETIF_F_TSO;
2779 * nil rebuild_header routine,
2780 * that should be never called and used as just bug trap.
2783 if (!dev->rebuild_header)
2784 dev->rebuild_header = default_rebuild_header;
2787 * Default initial state at registry is that the
2788 * device is present.
2791 set_bit(__LINK_STATE_PRESENT, &dev->state);
2794 dev_init_scheduler(dev);
2795 write_lock_bh(&dev_base_lock);
2797 dev_tail = &dev->next;
2798 hlist_add_head(&dev->name_hlist, head);
2799 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
2801 dev->reg_state = NETREG_REGISTERING;
2802 write_unlock_bh(&dev_base_lock);
2804 /* Notify protocols, that a new device appeared. */
2805 notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
2807 /* Finish registration after unlock */
2814 free_divert_blk(dev);
2819 * netdev_wait_allrefs - wait until all references are gone.
2821 * This is called when unregistering network devices.
2823 * Any protocol or device that holds a reference should register
2824 * for netdevice notification, and cleanup and put back the
2825 * reference if they receive an UNREGISTER event.
2826 * We can get stuck here if buggy protocols don't correctly
2829 static void netdev_wait_allrefs(struct net_device *dev)
2831 unsigned long rebroadcast_time, warning_time;
2833 rebroadcast_time = warning_time = jiffies;
2834 while (atomic_read(&dev->refcnt) != 0) {
2835 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
2838 /* Rebroadcast unregister notification */
2839 notifier_call_chain(&netdev_chain,
2840 NETDEV_UNREGISTER, dev);
2842 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
2844 /* We must not have linkwatch events
2845 * pending on unregister. If this
2846 * happens, we simply run the queue
2847 * unscheduled, resulting in a noop
2850 linkwatch_run_queue();
2855 rebroadcast_time = jiffies;
2858 current->state = TASK_INTERRUPTIBLE;
2859 schedule_timeout(HZ / 4);
2861 if (time_after(jiffies, warning_time + 10 * HZ)) {
2862 printk(KERN_EMERG "unregister_netdevice: "
2863 "waiting for %s to become free. Usage "
2865 dev->name, atomic_read(&dev->refcnt));
2866 warning_time = jiffies;
2875 * register_netdevice(x1);
2876 * register_netdevice(x2);
2878 * unregister_netdevice(y1);
2879 * unregister_netdevice(y2);
2885 * We are invoked by rtnl_unlock() after it drops the semaphore.
2886 * This allows us to deal with problems:
2887 * 1) We can create/delete sysfs objects which invoke hotplug
2888 * without deadlocking with linkwatch via keventd.
2889 * 2) Since we run with the RTNL semaphore not held, we can sleep
2890 * safely in order to wait for the netdev refcnt to drop to zero.
2892 static DECLARE_MUTEX(net_todo_run_mutex);
2893 void netdev_run_todo(void)
2895 struct list_head list = LIST_HEAD_INIT(list);
2899 /* Need to guard against multiple cpu's getting out of order. */
2900 down(&net_todo_run_mutex);
2902 /* Not safe to do outside the semaphore. We must not return
2903 * until all unregister events invoked by the local processor
2904 * have been completed (either by this todo run, or one on
2907 if (list_empty(&net_todo_list))
2910 /* Snapshot list, allow later requests */
2911 spin_lock(&net_todo_list_lock);
2912 list_splice_init(&net_todo_list, &list);
2913 spin_unlock(&net_todo_list_lock);
2915 while (!list_empty(&list)) {
2916 struct net_device *dev
2917 = list_entry(list.next, struct net_device, todo_list);
2918 list_del(&dev->todo_list);
2920 switch(dev->reg_state) {
2921 case NETREG_REGISTERING:
2922 err = netdev_register_sysfs(dev);
2924 printk(KERN_ERR "%s: failed sysfs registration (%d)\n",
2926 dev->reg_state = NETREG_REGISTERED;
2929 case NETREG_UNREGISTERING:
2930 netdev_unregister_sysfs(dev);
2931 dev->reg_state = NETREG_UNREGISTERED;
2933 netdev_wait_allrefs(dev);
2936 BUG_ON(atomic_read(&dev->refcnt));
2937 BUG_TRAP(!dev->ip_ptr);
2938 BUG_TRAP(!dev->ip6_ptr);
2939 BUG_TRAP(!dev->dn_ptr);
2942 /* It must be the very last action,
2943 * after this 'dev' may point to freed up memory.
2945 if (dev->destructor)
2946 dev->destructor(dev);
2950 printk(KERN_ERR "network todo '%s' but state %d\n",
2951 dev->name, dev->reg_state);
2957 up(&net_todo_run_mutex);
2961 * free_netdev - free network device
2964 * This function does the last stage of destroying an allocated device
2965 * interface. The reference to the device object is released.
2966 * If this is the last reference then it will be freed.
2968 void free_netdev(struct net_device *dev)
2971 /* Compatiablity with error handling in drivers */
2972 if (dev->reg_state == NETREG_UNINITIALIZED) {
2973 kfree((char *)dev - dev->padded);
2977 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
2978 dev->reg_state = NETREG_RELEASED;
2980 /* will free via class release */
2981 class_device_put(&dev->class_dev);
2983 kfree((char *)dev - dev->padded);
2987 /* Synchronize with packet receive processing. */
2988 void synchronize_net(void)
2991 synchronize_kernel();
2995 * unregister_netdevice - remove device from the kernel
2998 * This function shuts down a device interface and removes it
2999 * from the kernel tables. On success 0 is returned, on a failure
3000 * a negative errno code is returned.
3002 * Callers must hold the rtnl semaphore. See the comment at the
3003 * end of Space.c for details about the locking. You may want
3004 * unregister_netdev() instead of this.
3007 int unregister_netdevice(struct net_device *dev)
3009 struct net_device *d, **dp;
3011 BUG_ON(dev_boot_phase);
3014 /* Some devices call without registering for initialization unwind. */
3015 if (dev->reg_state == NETREG_UNINITIALIZED) {
3016 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3017 "was registered\n", dev->name, dev);
3021 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3023 /* If device is running, close it first. */
3024 if (dev->flags & IFF_UP)
3027 /* And unlink it from device chain. */
3028 for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3030 write_lock_bh(&dev_base_lock);
3031 hlist_del(&dev->name_hlist);
3032 hlist_del(&dev->index_hlist);
3033 if (dev_tail == &dev->next)
3036 write_unlock_bh(&dev_base_lock);
3041 printk(KERN_ERR "unregister net_device: '%s' not found\n",
3046 dev->reg_state = NETREG_UNREGISTERING;
3050 /* Shutdown queueing discipline. */
3054 /* Notify protocols, that we are about to destroy
3055 this device. They should clean all the things.
3057 notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3060 * Flush the multicast chain
3062 dev_mc_discard(dev);
3067 /* Notifier chain MUST detach us from master device. */
3068 BUG_TRAP(!dev->master);
3070 free_divert_blk(dev);
3072 /* Finish processing unregister after unlock */
3081 #ifdef CONFIG_HOTPLUG_CPU
3082 static int dev_cpu_callback(struct notifier_block *nfb,
3083 unsigned long action,
3086 struct sk_buff **list_skb;
3087 struct net_device **list_net;
3088 struct sk_buff *skb;
3089 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3090 struct softnet_data *sd, *oldsd;
3092 if (action != CPU_DEAD)
3095 local_irq_disable();
3096 cpu = smp_processor_id();
3097 sd = &per_cpu(softnet_data, cpu);
3098 oldsd = &per_cpu(softnet_data, oldcpu);
3100 /* Find end of our completion_queue. */
3101 list_skb = &sd->completion_queue;
3103 list_skb = &(*list_skb)->next;
3104 /* Append completion queue from offline CPU. */
3105 *list_skb = oldsd->completion_queue;
3106 oldsd->completion_queue = NULL;
3108 /* Find end of our output_queue. */
3109 list_net = &sd->output_queue;
3111 list_net = &(*list_net)->next_sched;
3112 /* Append output queue from offline CPU. */
3113 *list_net = oldsd->output_queue;
3114 oldsd->output_queue = NULL;
3116 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3119 /* Process offline CPU's input_pkt_queue */
3120 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3125 #endif /* CONFIG_HOTPLUG_CPU */
3129 * Initialize the DEV module. At boot time this walks the device list and
3130 * unhooks any devices that fail to initialise (normally hardware not
3131 * present) and leaves us with a valid list of present and active devices.
3136 * This is called single threaded during boot, so no need
3137 * to take the rtnl semaphore.
3139 static int __init net_dev_init(void)
3141 int i, rc = -ENOMEM;
3143 BUG_ON(!dev_boot_phase);
3147 if (dev_proc_init())
3150 if (netdev_sysfs_init())
3153 INIT_LIST_HEAD(&ptype_all);
3154 for (i = 0; i < 16; i++)
3155 INIT_LIST_HEAD(&ptype_base[i]);
3157 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3158 INIT_HLIST_HEAD(&dev_name_head[i]);
3160 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3161 INIT_HLIST_HEAD(&dev_index_head[i]);
3164 * Initialise the packet receive queues.
3167 for (i = 0; i < NR_CPUS; i++) {
3168 struct softnet_data *queue;
3170 queue = &per_cpu(softnet_data, i);
3171 skb_queue_head_init(&queue->input_pkt_queue);
3172 queue->throttle = 0;
3173 queue->cng_level = 0;
3174 queue->avg_blog = 10; /* arbitrary non-zero */
3175 queue->completion_queue = NULL;
3176 INIT_LIST_HEAD(&queue->poll_list);
3177 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3178 queue->backlog_dev.weight = weight_p;
3179 queue->backlog_dev.poll = process_backlog;
3180 atomic_set(&queue->backlog_dev.refcnt, 1);
3183 #ifdef OFFLINE_SAMPLE
3184 samp_timer.expires = jiffies + (10 * HZ);
3185 add_timer(&samp_timer);
3190 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3191 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3193 hotcpu_notifier(dev_cpu_callback, 0);
3201 subsys_initcall(net_dev_init);
3203 EXPORT_SYMBOL(__dev_get_by_index);
3204 EXPORT_SYMBOL(__dev_get_by_name);
3205 EXPORT_SYMBOL(__dev_remove_pack);
3206 EXPORT_SYMBOL(__skb_linearize);
3207 EXPORT_SYMBOL(dev_add_pack);
3208 EXPORT_SYMBOL(dev_alloc_name);
3209 EXPORT_SYMBOL(dev_close);
3210 EXPORT_SYMBOL(dev_get_by_flags);
3211 EXPORT_SYMBOL(dev_get_by_index);
3212 EXPORT_SYMBOL(dev_get_by_name);
3213 EXPORT_SYMBOL(dev_ioctl);
3214 EXPORT_SYMBOL(dev_open);
3215 EXPORT_SYMBOL(dev_queue_xmit);
3216 EXPORT_SYMBOL(dev_remove_pack);
3217 EXPORT_SYMBOL(dev_set_allmulti);
3218 EXPORT_SYMBOL(dev_set_promiscuity);
3219 EXPORT_SYMBOL(dev_change_flags);
3220 EXPORT_SYMBOL(dev_set_mtu);
3221 EXPORT_SYMBOL(free_netdev);
3222 EXPORT_SYMBOL(netdev_boot_setup_check);
3223 EXPORT_SYMBOL(netdev_set_master);
3224 EXPORT_SYMBOL(netdev_state_change);
3225 EXPORT_SYMBOL(netif_receive_skb);
3226 EXPORT_SYMBOL(netif_rx);
3227 EXPORT_SYMBOL(register_gifconf);
3228 EXPORT_SYMBOL(register_netdevice);
3229 EXPORT_SYMBOL(register_netdevice_notifier);
3230 EXPORT_SYMBOL(skb_checksum_help);
3231 EXPORT_SYMBOL(synchronize_net);
3232 EXPORT_SYMBOL(unregister_netdevice);
3233 EXPORT_SYMBOL(unregister_netdevice_notifier);
3234 EXPORT_SYMBOL(net_enable_timestamp);
3235 EXPORT_SYMBOL(net_disable_timestamp);
3237 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3238 EXPORT_SYMBOL(br_handle_frame_hook);
3242 EXPORT_SYMBOL(dev_load);
3245 #ifdef CONFIG_NET_CLS_ACT
3246 EXPORT_SYMBOL(ing_filter);
3250 EXPORT_PER_CPU_SYMBOL(softnet_data);