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
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 #include <linux/delay.h>
112 #ifdef CONFIG_NET_RADIO
113 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
114 #include <net/iw_handler.h>
115 #endif /* CONFIG_NET_RADIO */
116 #include <linux/vs_network.h>
117 #include <asm/current.h>
118 #include <linux/vs_network.h>
120 /* This define, if set, will randomly drop a packet when congestion
121 * is more than moderate. It helps fairness in the multi-interface
122 * case when one of them is a hog, but it kills performance for the
123 * single interface case so it is off now by default.
127 /* Setting this will sample the queue lengths and thus congestion
128 * via a timer instead of as each packet is received.
130 #undef OFFLINE_SAMPLE
133 * The list of packet types we will receive (as opposed to discard)
134 * and the routines to invoke.
136 * Why 16. Because with 16 the only overlap we get on a hash of the
137 * low nibble of the protocol value is RARP/SNAP/X.25.
139 * NOTE: That is no longer true with the addition of VLAN tags. Not
140 * sure which should go first, but I bet it won't make much
141 * difference if we are running VLANs. The good news is that
142 * this protocol won't be in the list unless compiled in, so
143 * the average user (w/out VLANs) will not be adversly affected.
160 static DEFINE_SPINLOCK(ptype_lock);
161 static struct list_head ptype_base[16]; /* 16 way hashed list */
162 static struct list_head ptype_all; /* Taps */
164 #ifdef OFFLINE_SAMPLE
165 static void sample_queue(unsigned long dummy);
166 static struct timer_list samp_timer = TIMER_INITIALIZER(sample_queue, 0, 0);
170 * The @dev_base list is protected by @dev_base_lock and the rtln
173 * Pure readers hold dev_base_lock for reading.
175 * Writers must hold the rtnl semaphore while they loop through the
176 * dev_base list, and hold dev_base_lock for writing when they do the
177 * actual updates. This allows pure readers to access the list even
178 * while a writer is preparing to update it.
180 * To put it another way, dev_base_lock is held for writing only to
181 * protect against pure readers; the rtnl semaphore provides the
182 * protection against other writers.
184 * See, for example usages, register_netdevice() and
185 * unregister_netdevice(), which must be called with the rtnl
188 struct net_device *dev_base;
189 static struct net_device **dev_tail = &dev_base;
190 DEFINE_RWLOCK(dev_base_lock);
192 EXPORT_SYMBOL(dev_base);
193 EXPORT_SYMBOL(dev_base_lock);
195 #define NETDEV_HASHBITS 8
196 static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
197 static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
199 static inline struct hlist_head *dev_name_hash(const char *name)
201 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
202 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
205 static inline struct hlist_head *dev_index_hash(int ifindex)
207 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
214 static struct notifier_block *netdev_chain;
217 * Device drivers call our routines to queue packets here. We empty the
218 * queue in the local softnet handler.
220 DEFINE_PER_CPU(struct softnet_data, softnet_data) = { 0, };
223 extern int netdev_sysfs_init(void);
224 extern int netdev_register_sysfs(struct net_device *);
225 extern void netdev_unregister_sysfs(struct net_device *);
227 #define netdev_sysfs_init() (0)
228 #define netdev_register_sysfs(dev) (0)
229 #define netdev_unregister_sysfs(dev) do { } while(0)
233 /*******************************************************************************
235 Protocol management and registration routines
237 *******************************************************************************/
246 * Add a protocol ID to the list. Now that the input handler is
247 * smarter we can dispense with all the messy stuff that used to be
250 * BEWARE!!! Protocol handlers, mangling input packets,
251 * MUST BE last in hash buckets and checking protocol handlers
252 * MUST start from promiscuous ptype_all chain in net_bh.
253 * It is true now, do not change it.
254 * Explanation follows: if protocol handler, mangling packet, will
255 * be the first on list, it is not able to sense, that packet
256 * is cloned and should be copied-on-write, so that it will
257 * change it and subsequent readers will get broken packet.
262 * dev_add_pack - add packet handler
263 * @pt: packet type declaration
265 * Add a protocol handler to the networking stack. The passed &packet_type
266 * is linked into kernel lists and may not be freed until it has been
267 * removed from the kernel lists.
269 * This call does not sleep therefore it can not
270 * guarantee all CPU's that are in middle of receiving packets
271 * will see the new packet type (until the next received packet).
274 void dev_add_pack(struct packet_type *pt)
278 spin_lock_bh(&ptype_lock);
279 if (pt->type == htons(ETH_P_ALL)) {
281 list_add_rcu(&pt->list, &ptype_all);
283 hash = ntohs(pt->type) & 15;
284 list_add_rcu(&pt->list, &ptype_base[hash]);
286 spin_unlock_bh(&ptype_lock);
289 extern void linkwatch_run_queue(void);
294 * __dev_remove_pack - remove packet handler
295 * @pt: packet type declaration
297 * Remove a protocol handler that was previously added to the kernel
298 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
299 * from the kernel lists and can be freed or reused once this function
302 * The packet type might still be in use by receivers
303 * and must not be freed until after all the CPU's have gone
304 * through a quiescent state.
306 void __dev_remove_pack(struct packet_type *pt)
308 struct list_head *head;
309 struct packet_type *pt1;
311 spin_lock_bh(&ptype_lock);
313 if (pt->type == htons(ETH_P_ALL)) {
317 head = &ptype_base[ntohs(pt->type) & 15];
319 list_for_each_entry(pt1, head, list) {
321 list_del_rcu(&pt->list);
326 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
328 spin_unlock_bh(&ptype_lock);
331 * dev_remove_pack - remove packet handler
332 * @pt: packet type declaration
334 * Remove a protocol handler that was previously added to the kernel
335 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
336 * from the kernel lists and can be freed or reused once this function
339 * This call sleeps to guarantee that no CPU is looking at the packet
342 void dev_remove_pack(struct packet_type *pt)
344 __dev_remove_pack(pt);
349 /******************************************************************************
351 Device Boot-time Settings Routines
353 *******************************************************************************/
355 /* Boot time configuration table */
356 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
359 * netdev_boot_setup_add - add new setup entry
360 * @name: name of the device
361 * @map: configured settings for the device
363 * Adds new setup entry to the dev_boot_setup list. The function
364 * returns 0 on error and 1 on success. This is a generic routine to
367 static int netdev_boot_setup_add(char *name, struct ifmap *map)
369 struct netdev_boot_setup *s;
373 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
374 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
375 memset(s[i].name, 0, sizeof(s[i].name));
376 strcpy(s[i].name, name);
377 memcpy(&s[i].map, map, sizeof(s[i].map));
382 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
386 * netdev_boot_setup_check - check boot time settings
387 * @dev: the netdevice
389 * Check boot time settings for the device.
390 * The found settings are set for the device to be used
391 * later in the device probing.
392 * Returns 0 if no settings found, 1 if they are.
394 int netdev_boot_setup_check(struct net_device *dev)
396 struct netdev_boot_setup *s = dev_boot_setup;
399 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
400 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
401 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
402 dev->irq = s[i].map.irq;
403 dev->base_addr = s[i].map.base_addr;
404 dev->mem_start = s[i].map.mem_start;
405 dev->mem_end = s[i].map.mem_end;
414 * netdev_boot_base - get address from boot time settings
415 * @prefix: prefix for network device
416 * @unit: id for network device
418 * Check boot time settings for the base address of device.
419 * The found settings are set for the device to be used
420 * later in the device probing.
421 * Returns 0 if no settings found.
423 unsigned long netdev_boot_base(const char *prefix, int unit)
425 const struct netdev_boot_setup *s = dev_boot_setup;
429 sprintf(name, "%s%d", prefix, unit);
432 * If device already registered then return base of 1
433 * to indicate not to probe for this interface
435 if (__dev_get_by_name(name))
438 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
439 if (!strcmp(name, s[i].name))
440 return s[i].map.base_addr;
445 * Saves at boot time configured settings for any netdevice.
447 int __init netdev_boot_setup(char *str)
452 str = get_options(str, ARRAY_SIZE(ints), ints);
457 memset(&map, 0, sizeof(map));
461 map.base_addr = ints[2];
463 map.mem_start = ints[3];
465 map.mem_end = ints[4];
467 /* Add new entry to the list */
468 return netdev_boot_setup_add(str, &map);
471 __setup("netdev=", netdev_boot_setup);
473 /*******************************************************************************
475 Device Interface Subroutines
477 *******************************************************************************/
480 * __dev_get_by_name - find a device by its name
481 * @name: name to find
483 * Find an interface by name. Must be called under RTNL semaphore
484 * or @dev_base_lock. If the name is found a pointer to the device
485 * is returned. If the name is not found then %NULL is returned. The
486 * reference counters are not incremented so the caller must be
487 * careful with locks.
490 struct net_device *__dev_get_by_name(const char *name)
492 struct hlist_node *p;
494 hlist_for_each(p, dev_name_hash(name)) {
495 struct net_device *dev
496 = hlist_entry(p, struct net_device, name_hlist);
497 if (!strncmp(dev->name, name, IFNAMSIZ))
504 * dev_get_by_name - find a device by its name
505 * @name: name to find
507 * Find an interface by name. This can be called from any
508 * context and does its own locking. The returned handle has
509 * the usage count incremented and the caller must use dev_put() to
510 * release it when it is no longer needed. %NULL is returned if no
511 * matching device is found.
514 struct net_device *dev_get_by_name(const char *name)
516 struct net_device *dev;
518 read_lock(&dev_base_lock);
519 dev = __dev_get_by_name(name);
522 read_unlock(&dev_base_lock);
527 * __dev_get_by_index - find a device by its ifindex
528 * @ifindex: index of device
530 * Search for an interface by index. Returns %NULL if the device
531 * is not found or a pointer to the device. The device has not
532 * had its reference counter increased so the caller must be careful
533 * about locking. The caller must hold either the RTNL semaphore
537 struct net_device *__dev_get_by_index(int ifindex)
539 struct hlist_node *p;
541 hlist_for_each(p, dev_index_hash(ifindex)) {
542 struct net_device *dev
543 = hlist_entry(p, struct net_device, index_hlist);
544 if (dev->ifindex == ifindex)
552 * dev_get_by_index - find a device by its ifindex
553 * @ifindex: index of device
555 * Search for an interface by index. Returns NULL if the device
556 * is not found or a pointer to the device. The device returned has
557 * had a reference added and the pointer is safe until the user calls
558 * dev_put to indicate they have finished with it.
561 struct net_device *dev_get_by_index(int ifindex)
563 struct net_device *dev;
565 read_lock(&dev_base_lock);
566 dev = __dev_get_by_index(ifindex);
569 read_unlock(&dev_base_lock);
574 * dev_getbyhwaddr - find a device by its hardware address
575 * @type: media type of device
576 * @ha: hardware address
578 * Search for an interface by MAC address. Returns NULL if the device
579 * is not found or a pointer to the device. The caller must hold the
580 * rtnl semaphore. The returned device has not had its ref count increased
581 * and the caller must therefore be careful about locking
584 * If the API was consistent this would be __dev_get_by_hwaddr
587 struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
589 struct net_device *dev;
593 for (dev = dev_base; dev; dev = dev->next)
594 if (dev->type == type &&
595 !memcmp(dev->dev_addr, ha, dev->addr_len))
600 struct net_device *dev_getfirstbyhwtype(unsigned short type)
602 struct net_device *dev;
605 for (dev = dev_base; dev; dev = dev->next) {
606 if (dev->type == type) {
615 EXPORT_SYMBOL(dev_getfirstbyhwtype);
618 * dev_get_by_flags - find any device with given flags
619 * @if_flags: IFF_* values
620 * @mask: bitmask of bits in if_flags to check
622 * Search for any interface with the given flags. Returns NULL if a device
623 * is not found or a pointer to the device. The device returned has
624 * had a reference added and the pointer is safe until the user calls
625 * dev_put to indicate they have finished with it.
628 struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
630 struct net_device *dev;
632 read_lock(&dev_base_lock);
633 for (dev = dev_base; dev != NULL; dev = dev->next) {
634 if (((dev->flags ^ if_flags) & mask) == 0) {
639 read_unlock(&dev_base_lock);
644 * dev_valid_name - check if name is okay for network device
647 * Network device names need to be valid file names to
648 * to allow sysfs to work
650 static int dev_valid_name(const char *name)
652 return !(*name == '\0'
653 || !strcmp(name, ".")
654 || !strcmp(name, "..")
655 || strchr(name, '/'));
659 * dev_alloc_name - allocate a name for a device
661 * @name: name format string
663 * Passed a format string - eg "lt%d" it will try and find a suitable
664 * id. Not efficient for many devices, not called a lot. The caller
665 * must hold the dev_base or rtnl lock while allocating the name and
666 * adding the device in order to avoid duplicates. Returns the number
667 * of the unit assigned or a negative errno code.
670 int dev_alloc_name(struct net_device *dev, const char *name)
675 const int max_netdevices = 8*PAGE_SIZE;
677 struct net_device *d;
679 p = strnchr(name, IFNAMSIZ-1, '%');
682 * Verify the string as this thing may have come from
683 * the user. There must be either one "%d" and no other "%"
686 if (p[1] != 'd' || strchr(p + 2, '%'))
689 /* Use one page as a bit array of possible slots */
690 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
694 for (d = dev_base; d; d = d->next) {
695 if (!sscanf(d->name, name, &i))
697 if (i < 0 || i >= max_netdevices)
700 /* avoid cases where sscanf is not exact inverse of printf */
701 snprintf(buf, sizeof(buf), name, i);
702 if (!strncmp(buf, d->name, IFNAMSIZ))
706 i = find_first_zero_bit(inuse, max_netdevices);
707 free_page((unsigned long) inuse);
710 snprintf(buf, sizeof(buf), name, i);
711 if (!__dev_get_by_name(buf)) {
712 strlcpy(dev->name, buf, IFNAMSIZ);
716 /* It is possible to run out of possible slots
717 * when the name is long and there isn't enough space left
718 * for the digits, or if all bits are used.
725 * dev_change_name - change name of a device
727 * @newname: name (or format string) must be at least IFNAMSIZ
729 * Change name of a device, can pass format strings "eth%d".
732 int dev_change_name(struct net_device *dev, char *newname)
738 if (dev->flags & IFF_UP)
741 if (!dev_valid_name(newname))
744 if (strchr(newname, '%')) {
745 err = dev_alloc_name(dev, newname);
748 strcpy(newname, dev->name);
750 else if (__dev_get_by_name(newname))
753 strlcpy(dev->name, newname, IFNAMSIZ);
755 err = class_device_rename(&dev->class_dev, dev->name);
757 hlist_del(&dev->name_hlist);
758 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
759 notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
766 * netdev_features_change - device changes fatures
767 * @dev: device to cause notification
769 * Called to indicate a device has changed features.
771 void netdev_features_change(struct net_device *dev)
773 notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
775 EXPORT_SYMBOL(netdev_features_change);
778 * netdev_state_change - device changes state
779 * @dev: device to cause notification
781 * Called to indicate a device has changed state. This function calls
782 * the notifier chains for netdev_chain and sends a NEWLINK message
783 * to the routing socket.
785 void netdev_state_change(struct net_device *dev)
787 if (dev->flags & IFF_UP) {
788 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
789 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
794 * dev_load - load a network module
795 * @name: name of interface
797 * If a network interface is not present and the process has suitable
798 * privileges this function loads the module. If module loading is not
799 * available in this kernel then it becomes a nop.
802 void dev_load(const char *name)
804 struct net_device *dev;
806 read_lock(&dev_base_lock);
807 dev = __dev_get_by_name(name);
808 read_unlock(&dev_base_lock);
810 if (!dev && capable(CAP_SYS_MODULE))
811 request_module("%s", name);
814 static int default_rebuild_header(struct sk_buff *skb)
816 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
817 skb->dev ? skb->dev->name : "NULL!!!");
824 * dev_open - prepare an interface for use.
825 * @dev: device to open
827 * Takes a device from down to up state. The device's private open
828 * function is invoked and then the multicast lists are loaded. Finally
829 * the device is moved into the up state and a %NETDEV_UP message is
830 * sent to the netdev notifier chain.
832 * Calling this function on an active interface is a nop. On a failure
833 * a negative errno code is returned.
835 int dev_open(struct net_device *dev)
843 if (dev->flags & IFF_UP)
847 * Is it even present?
849 if (!netif_device_present(dev))
853 * Call device private open method
855 set_bit(__LINK_STATE_START, &dev->state);
857 ret = dev->open(dev);
859 clear_bit(__LINK_STATE_START, &dev->state);
863 * If it went open OK then:
870 dev->flags |= IFF_UP;
873 * Initialize multicasting status
878 * Wakeup transmit queue engine
883 * ... and announce new interface.
885 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
891 * dev_close - shutdown an interface.
892 * @dev: device to shutdown
894 * This function moves an active device into down state. A
895 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
896 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
899 int dev_close(struct net_device *dev)
901 if (!(dev->flags & IFF_UP))
905 * Tell people we are going down, so that they can
906 * prepare to death, when device is still operating.
908 notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
912 clear_bit(__LINK_STATE_START, &dev->state);
914 /* Synchronize to scheduled poll. We cannot touch poll list,
915 * it can be even on different cpu. So just clear netif_running(),
916 * and wait when poll really will happen. Actually, the best place
917 * for this is inside dev->stop() after device stopped its irq
918 * engine, but this requires more changes in devices. */
920 smp_mb__after_clear_bit(); /* Commit netif_running(). */
921 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
923 current->state = TASK_INTERRUPTIBLE;
928 * Call the device specific close. This cannot fail.
929 * Only if device is UP
931 * We allow it to be called even after a DETACH hot-plug
938 * Device is now down.
941 dev->flags &= ~IFF_UP;
944 * Tell people we are down
946 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
953 * Device change register/unregister. These are not inline or static
954 * as we export them to the world.
958 * register_netdevice_notifier - register a network notifier block
961 * Register a notifier to be called when network device events occur.
962 * The notifier passed is linked into the kernel structures and must
963 * not be reused until it has been unregistered. A negative errno code
964 * is returned on a failure.
966 * When registered all registration and up events are replayed
967 * to the new notifier to allow device to have a race free
968 * view of the network device list.
971 int register_netdevice_notifier(struct notifier_block *nb)
973 struct net_device *dev;
977 err = notifier_chain_register(&netdev_chain, nb);
979 for (dev = dev_base; dev; dev = dev->next) {
980 nb->notifier_call(nb, NETDEV_REGISTER, dev);
982 if (dev->flags & IFF_UP)
983 nb->notifier_call(nb, NETDEV_UP, dev);
991 * unregister_netdevice_notifier - unregister a network notifier block
994 * Unregister a notifier previously registered by
995 * register_netdevice_notifier(). The notifier is unlinked into the
996 * kernel structures and may then be reused. A negative errno code
997 * is returned on a failure.
1000 int unregister_netdevice_notifier(struct notifier_block *nb)
1002 return notifier_chain_unregister(&netdev_chain, nb);
1006 * call_netdevice_notifiers - call all network notifier blocks
1007 * @val: value passed unmodified to notifier function
1008 * @v: pointer passed unmodified to notifier function
1010 * Call all network notifier blocks. Parameters and return value
1011 * are as for notifier_call_chain().
1014 int call_netdevice_notifiers(unsigned long val, void *v)
1016 return notifier_call_chain(&netdev_chain, val, v);
1019 /* When > 0 there are consumers of rx skb time stamps */
1020 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1022 void net_enable_timestamp(void)
1024 atomic_inc(&netstamp_needed);
1027 void net_disable_timestamp(void)
1029 atomic_dec(&netstamp_needed);
1032 static inline void net_timestamp(struct timeval *stamp)
1034 if (atomic_read(&netstamp_needed))
1035 do_gettimeofday(stamp);
1043 * Support routine. Sends outgoing frames to any network
1044 * taps currently in use.
1047 void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1049 struct packet_type *ptype;
1050 net_timestamp(&skb->stamp);
1053 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1054 /* Never send packets back to the socket
1055 * they originated from - MvS (miquels@drinkel.ow.org)
1057 if ((ptype->dev == dev || !ptype->dev) &&
1058 (ptype->af_packet_priv == NULL ||
1059 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1060 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1064 /* skb->nh should be correctly
1065 set by sender, so that the second statement is
1066 just protection against buggy protocols.
1068 skb2->mac.raw = skb2->data;
1070 if (skb2->nh.raw < skb2->data ||
1071 skb2->nh.raw > skb2->tail) {
1072 if (net_ratelimit())
1073 printk(KERN_CRIT "protocol %04x is "
1075 skb2->protocol, dev->name);
1076 skb2->nh.raw = skb2->data;
1079 skb2->h.raw = skb2->nh.raw;
1080 skb2->pkt_type = PACKET_OUTGOING;
1081 ptype->func(skb2, skb->dev, ptype);
1088 * Invalidate hardware checksum when packet is to be mangled, and
1089 * complete checksum manually on outgoing path.
1091 int skb_checksum_help(struct sk_buff *skb, int inward)
1094 int ret = 0, offset = skb->h.raw - skb->data;
1097 skb->ip_summed = CHECKSUM_NONE;
1101 if (skb_cloned(skb)) {
1102 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1107 if (offset > (int)skb->len)
1109 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1111 offset = skb->tail - skb->h.raw;
1114 if (skb->csum + 2 > offset)
1117 *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
1118 skb->ip_summed = CHECKSUM_NONE;
1123 #ifdef CONFIG_HIGHMEM
1124 /* Actually, we should eliminate this check as soon as we know, that:
1125 * 1. IOMMU is present and allows to map all the memory.
1126 * 2. No high memory really exists on this machine.
1129 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1133 if (dev->features & NETIF_F_HIGHDMA)
1136 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1137 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1143 #define illegal_highdma(dev, skb) (0)
1146 extern void skb_release_data(struct sk_buff *);
1148 /* Keep head the same: replace data */
1149 int __skb_linearize(struct sk_buff *skb, int gfp_mask)
1154 struct skb_shared_info *ninfo;
1155 int headerlen = skb->data - skb->head;
1156 int expand = (skb->tail + skb->data_len) - skb->end;
1158 if (skb_shared(skb))
1164 size = skb->end - skb->head + expand;
1165 size = SKB_DATA_ALIGN(size);
1166 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
1170 /* Copy entire thing */
1171 if (skb_copy_bits(skb, -headerlen, data, headerlen + skb->len))
1175 ninfo = (struct skb_shared_info*)(data + size);
1176 atomic_set(&ninfo->dataref, 1);
1177 ninfo->tso_size = skb_shinfo(skb)->tso_size;
1178 ninfo->tso_segs = skb_shinfo(skb)->tso_segs;
1179 ninfo->nr_frags = 0;
1180 ninfo->frag_list = NULL;
1182 /* Offset between the two in bytes */
1183 offset = data - skb->head;
1185 /* Free old data. */
1186 skb_release_data(skb);
1189 skb->end = data + size;
1191 /* Set up new pointers */
1192 skb->h.raw += offset;
1193 skb->nh.raw += offset;
1194 skb->mac.raw += offset;
1195 skb->tail += offset;
1196 skb->data += offset;
1198 /* We are no longer a clone, even if we were. */
1201 skb->tail += skb->data_len;
1206 #define HARD_TX_LOCK(dev, cpu) { \
1207 if ((dev->features & NETIF_F_LLTX) == 0) { \
1208 spin_lock(&dev->xmit_lock); \
1209 dev->xmit_lock_owner = cpu; \
1213 #define HARD_TX_UNLOCK(dev) { \
1214 if ((dev->features & NETIF_F_LLTX) == 0) { \
1215 dev->xmit_lock_owner = -1; \
1216 spin_unlock(&dev->xmit_lock); \
1221 * dev_queue_xmit - transmit a buffer
1222 * @skb: buffer to transmit
1224 * Queue a buffer for transmission to a network device. The caller must
1225 * have set the device and priority and built the buffer before calling
1226 * this function. The function can be called from an interrupt.
1228 * A negative errno code is returned on a failure. A success does not
1229 * guarantee the frame will be transmitted as it may be dropped due
1230 * to congestion or traffic shaping.
1232 * -----------------------------------------------------------------------------------
1233 * I notice this method can also return errors from the queue disciplines,
1234 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1237 * Regardless of the return value, the skb is consumed, so it is currently
1238 * difficult to retry a send to this method. (You can bump the ref count
1239 * before sending to hold a reference for retry if you are careful.)
1241 * When calling this method, interrupts MUST be enabled. This is because
1242 * the BH enable code must have IRQs enabled so that it will not deadlock.
1246 int dev_queue_xmit(struct sk_buff *skb)
1248 struct net_device *dev = skb->dev;
1252 if (skb_shinfo(skb)->frag_list &&
1253 !(dev->features & NETIF_F_FRAGLIST) &&
1254 __skb_linearize(skb, GFP_ATOMIC))
1257 /* Fragmented skb is linearized if device does not support SG,
1258 * or if at least one of fragments is in highmem and device
1259 * does not support DMA from it.
1261 if (skb_shinfo(skb)->nr_frags &&
1262 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1263 __skb_linearize(skb, GFP_ATOMIC))
1266 /* If packet is not checksummed and device does not support
1267 * checksumming for this protocol, complete checksumming here.
1269 if (skb->ip_summed == CHECKSUM_HW &&
1270 (!(dev->features & (NETIF_F_HW_CSUM | NETIF_F_NO_CSUM)) &&
1271 (!(dev->features & NETIF_F_IP_CSUM) ||
1272 skb->protocol != htons(ETH_P_IP))))
1273 if (skb_checksum_help(skb, 0))
1276 /* Disable soft irqs for various locks below. Also
1277 * stops preemption for RCU.
1281 /* Updates of qdisc are serialized by queue_lock.
1282 * The struct Qdisc which is pointed to by qdisc is now a
1283 * rcu structure - it may be accessed without acquiring
1284 * a lock (but the structure may be stale.) The freeing of the
1285 * qdisc will be deferred until it's known that there are no
1286 * more references to it.
1288 * If the qdisc has an enqueue function, we still need to
1289 * hold the queue_lock before calling it, since queue_lock
1290 * also serializes access to the device queue.
1293 q = rcu_dereference(dev->qdisc);
1294 #ifdef CONFIG_NET_CLS_ACT
1295 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1298 /* Grab device queue */
1299 spin_lock(&dev->queue_lock);
1301 rc = q->enqueue(skb, q);
1305 spin_unlock(&dev->queue_lock);
1306 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1310 /* The device has no queue. Common case for software devices:
1311 loopback, all the sorts of tunnels...
1313 Really, it is unlikely that xmit_lock protection is necessary here.
1314 (f.e. loopback and IP tunnels are clean ignoring statistics
1316 However, it is possible, that they rely on protection
1319 Check this and shot the lock. It is not prone from deadlocks.
1320 Either shot noqueue qdisc, it is even simpler 8)
1322 if (dev->flags & IFF_UP) {
1323 int cpu = smp_processor_id(); /* ok because BHs are off */
1325 if (dev->xmit_lock_owner != cpu) {
1327 HARD_TX_LOCK(dev, cpu);
1329 if (!netif_queue_stopped(dev)) {
1331 dev_queue_xmit_nit(skb, dev);
1334 if (!dev->hard_start_xmit(skb, dev)) {
1335 HARD_TX_UNLOCK(dev);
1339 HARD_TX_UNLOCK(dev);
1340 if (net_ratelimit())
1341 printk(KERN_CRIT "Virtual device %s asks to "
1342 "queue packet!\n", dev->name);
1344 /* Recursion is detected! It is possible,
1346 if (net_ratelimit())
1347 printk(KERN_CRIT "Dead loop on virtual device "
1348 "%s, fix it urgently!\n", dev->name);
1364 /*=======================================================================
1366 =======================================================================*/
1368 int netdev_max_backlog = 300;
1369 int weight_p = 64; /* old backlog weight */
1370 /* These numbers are selected based on intuition and some
1371 * experimentatiom, if you have more scientific way of doing this
1372 * please go ahead and fix things.
1374 int no_cong_thresh = 10;
1379 DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1382 static void get_sample_stats(int cpu)
1388 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
1389 int blog = sd->input_pkt_queue.qlen;
1390 int avg_blog = sd->avg_blog;
1392 avg_blog = (avg_blog >> 1) + (blog >> 1);
1394 if (avg_blog > mod_cong) {
1395 /* Above moderate congestion levels. */
1396 sd->cng_level = NET_RX_CN_HIGH;
1399 rq = rd % netdev_max_backlog;
1400 if (rq < avg_blog) /* unlucky bastard */
1401 sd->cng_level = NET_RX_DROP;
1403 } else if (avg_blog > lo_cong) {
1404 sd->cng_level = NET_RX_CN_MOD;
1407 rq = rd % netdev_max_backlog;
1408 if (rq < avg_blog) /* unlucky bastard */
1409 sd->cng_level = NET_RX_CN_HIGH;
1411 } else if (avg_blog > no_cong)
1412 sd->cng_level = NET_RX_CN_LOW;
1413 else /* no congestion */
1414 sd->cng_level = NET_RX_SUCCESS;
1416 sd->avg_blog = avg_blog;
1419 #ifdef OFFLINE_SAMPLE
1420 static void sample_queue(unsigned long dummy)
1422 /* 10 ms 0r 1ms -- i don't care -- JHS */
1424 int cpu = smp_processor_id();
1426 get_sample_stats(cpu);
1427 next_tick += jiffies;
1428 mod_timer(&samp_timer, next_tick);
1434 * netif_rx - post buffer to the network code
1435 * @skb: buffer to post
1437 * This function receives a packet from a device driver and queues it for
1438 * the upper (protocol) levels to process. It always succeeds. The buffer
1439 * may be dropped during processing for congestion control or by the
1443 * NET_RX_SUCCESS (no congestion)
1444 * NET_RX_CN_LOW (low congestion)
1445 * NET_RX_CN_MOD (moderate congestion)
1446 * NET_RX_CN_HIGH (high congestion)
1447 * NET_RX_DROP (packet was dropped)
1451 int netif_rx(struct sk_buff *skb)
1454 struct softnet_data *queue;
1455 unsigned long flags;
1457 /* if netpoll wants it, pretend we never saw it */
1458 if (netpoll_rx(skb))
1461 if (!skb->stamp.tv_sec)
1462 net_timestamp(&skb->stamp);
1465 * The code is rearranged so that the path is the most
1466 * short when CPU is congested, but is still operating.
1468 local_irq_save(flags);
1469 this_cpu = smp_processor_id();
1470 queue = &__get_cpu_var(softnet_data);
1472 __get_cpu_var(netdev_rx_stat).total++;
1473 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1474 if (queue->input_pkt_queue.qlen) {
1475 if (queue->throttle)
1480 __skb_queue_tail(&queue->input_pkt_queue, skb);
1481 #ifndef OFFLINE_SAMPLE
1482 get_sample_stats(this_cpu);
1484 local_irq_restore(flags);
1485 return queue->cng_level;
1488 if (queue->throttle)
1489 queue->throttle = 0;
1491 netif_rx_schedule(&queue->backlog_dev);
1495 if (!queue->throttle) {
1496 queue->throttle = 1;
1497 __get_cpu_var(netdev_rx_stat).throttled++;
1501 __get_cpu_var(netdev_rx_stat).dropped++;
1502 local_irq_restore(flags);
1508 int netif_rx_ni(struct sk_buff *skb)
1513 err = netif_rx(skb);
1514 if (local_softirq_pending())
1521 EXPORT_SYMBOL(netif_rx_ni);
1523 static __inline__ void skb_bond(struct sk_buff *skb)
1525 struct net_device *dev = skb->dev;
1528 skb->real_dev = skb->dev;
1529 skb->dev = dev->master;
1533 static void net_tx_action(struct softirq_action *h)
1535 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1537 if (sd->completion_queue) {
1538 struct sk_buff *clist;
1540 local_irq_disable();
1541 clist = sd->completion_queue;
1542 sd->completion_queue = NULL;
1546 struct sk_buff *skb = clist;
1547 clist = clist->next;
1549 BUG_TRAP(!atomic_read(&skb->users));
1554 if (sd->output_queue) {
1555 struct net_device *head;
1557 local_irq_disable();
1558 head = sd->output_queue;
1559 sd->output_queue = NULL;
1563 struct net_device *dev = head;
1564 head = head->next_sched;
1566 smp_mb__before_clear_bit();
1567 clear_bit(__LINK_STATE_SCHED, &dev->state);
1569 if (spin_trylock(&dev->queue_lock)) {
1571 spin_unlock(&dev->queue_lock);
1573 netif_schedule(dev);
1579 static __inline__ int deliver_skb(struct sk_buff *skb,
1580 struct packet_type *pt_prev)
1582 atomic_inc(&skb->users);
1583 return pt_prev->func(skb, skb->dev, pt_prev);
1586 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1587 int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1589 struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1590 unsigned char *addr);
1591 void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
1593 static __inline__ int handle_bridge(struct sk_buff **pskb,
1594 struct packet_type **pt_prev, int *ret)
1596 struct net_bridge_port *port;
1598 if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1599 (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1603 *ret = deliver_skb(*pskb, *pt_prev);
1607 return br_handle_frame_hook(port, pskb);
1610 #define handle_bridge(skb, pt_prev, ret) (0)
1613 #ifdef CONFIG_NET_CLS_ACT
1614 /* TODO: Maybe we should just force sch_ingress to be compiled in
1615 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1616 * a compare and 2 stores extra right now if we dont have it on
1617 * but have CONFIG_NET_CLS_ACT
1618 * NOTE: This doesnt stop any functionality; if you dont have
1619 * the ingress scheduler, you just cant add policies on ingress.
1622 static int ing_filter(struct sk_buff *skb)
1625 struct net_device *dev = skb->dev;
1626 int result = TC_ACT_OK;
1628 if (dev->qdisc_ingress) {
1629 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1630 if (MAX_RED_LOOP < ttl++) {
1631 printk("Redir loop detected Dropping packet (%s->%s)\n",
1632 skb->input_dev?skb->input_dev->name:"??",skb->dev->name);
1636 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1638 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1639 if (NULL == skb->input_dev) {
1640 skb->input_dev = skb->dev;
1641 printk("ing_filter: fixed %s out %s\n",skb->input_dev->name,skb->dev->name);
1643 spin_lock(&dev->ingress_lock);
1644 if ((q = dev->qdisc_ingress) != NULL)
1645 result = q->enqueue(skb, q);
1646 spin_unlock(&dev->ingress_lock);
1654 int netif_receive_skb(struct sk_buff *skb)
1656 struct packet_type *ptype, *pt_prev;
1657 int ret = NET_RX_DROP;
1658 unsigned short type;
1660 /* if we've gotten here through NAPI, check netpoll */
1661 if (skb->dev->poll && netpoll_rx(skb))
1664 if (!skb->stamp.tv_sec)
1665 net_timestamp(&skb->stamp);
1669 __get_cpu_var(netdev_rx_stat).total++;
1671 skb->h.raw = skb->nh.raw = skb->data;
1672 skb->mac_len = skb->nh.raw - skb->mac.raw;
1678 #ifdef CONFIG_NET_CLS_ACT
1679 if (skb->tc_verd & TC_NCLS) {
1680 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1685 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1686 if (!ptype->dev || ptype->dev == skb->dev) {
1688 ret = deliver_skb(skb, pt_prev);
1693 #ifdef CONFIG_NET_CLS_ACT
1695 ret = deliver_skb(skb, pt_prev);
1696 pt_prev = NULL; /* noone else should process this after*/
1698 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1701 ret = ing_filter(skb);
1703 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1712 handle_diverter(skb);
1714 if (handle_bridge(&skb, &pt_prev, &ret))
1717 type = skb->protocol;
1718 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1719 if (ptype->type == type &&
1720 (!ptype->dev || ptype->dev == skb->dev)) {
1722 ret = deliver_skb(skb, pt_prev);
1728 ret = pt_prev->func(skb, skb->dev, pt_prev);
1731 /* Jamal, now you will not able to escape explaining
1732 * me how you were going to use this. :-)
1742 static int process_backlog(struct net_device *backlog_dev, int *budget)
1745 int quota = min(backlog_dev->quota, *budget);
1746 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1747 unsigned long start_time = jiffies;
1749 backlog_dev->weight = weight_p;
1751 struct sk_buff *skb;
1752 struct net_device *dev;
1754 local_irq_disable();
1755 skb = __skb_dequeue(&queue->input_pkt_queue);
1762 netif_receive_skb(skb);
1768 if (work >= quota || jiffies - start_time > 1)
1773 backlog_dev->quota -= work;
1778 backlog_dev->quota -= work;
1781 list_del(&backlog_dev->poll_list);
1782 smp_mb__before_clear_bit();
1783 netif_poll_enable(backlog_dev);
1785 if (queue->throttle)
1786 queue->throttle = 0;
1791 static void net_rx_action(struct softirq_action *h)
1793 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1794 unsigned long start_time = jiffies;
1795 int budget = netdev_max_backlog;
1798 local_irq_disable();
1800 while (!list_empty(&queue->poll_list)) {
1801 struct net_device *dev;
1803 if (budget <= 0 || jiffies - start_time > 1)
1808 dev = list_entry(queue->poll_list.next,
1809 struct net_device, poll_list);
1810 netpoll_poll_lock(dev);
1812 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
1813 netpoll_poll_unlock(dev);
1814 local_irq_disable();
1815 list_del(&dev->poll_list);
1816 list_add_tail(&dev->poll_list, &queue->poll_list);
1818 dev->quota += dev->weight;
1820 dev->quota = dev->weight;
1822 netpoll_poll_unlock(dev);
1824 local_irq_disable();
1832 __get_cpu_var(netdev_rx_stat).time_squeeze++;
1833 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1837 static gifconf_func_t * gifconf_list [NPROTO];
1840 * register_gifconf - register a SIOCGIF handler
1841 * @family: Address family
1842 * @gifconf: Function handler
1844 * Register protocol dependent address dumping routines. The handler
1845 * that is passed must not be freed or reused until it has been replaced
1846 * by another handler.
1848 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1850 if (family >= NPROTO)
1852 gifconf_list[family] = gifconf;
1858 * Map an interface index to its name (SIOCGIFNAME)
1862 * We need this ioctl for efficient implementation of the
1863 * if_indextoname() function required by the IPv6 API. Without
1864 * it, we would have to search all the interfaces to find a
1868 static int dev_ifname(struct ifreq __user *arg)
1870 struct net_device *dev;
1874 * Fetch the caller's info block.
1877 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
1880 read_lock(&dev_base_lock);
1881 dev = __dev_get_by_index(ifr.ifr_ifindex);
1883 read_unlock(&dev_base_lock);
1887 strcpy(ifr.ifr_name, dev->name);
1888 read_unlock(&dev_base_lock);
1890 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1896 * Perform a SIOCGIFCONF call. This structure will change
1897 * size eventually, and there is nothing I can do about it.
1898 * Thus we will need a 'compatibility mode'.
1901 static int dev_ifconf(char __user *arg)
1904 struct net_device *dev;
1911 * Fetch the caller's info block.
1914 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
1921 * Loop over the interfaces, and write an info block for each.
1925 for (dev = dev_base; dev; dev = dev->next) {
1926 if (vx_flags(VXF_HIDE_NETIF, 0) &&
1927 !dev_in_nx_info(dev, current->nx_info))
1929 for (i = 0; i < NPROTO; i++) {
1930 if (gifconf_list[i]) {
1933 done = gifconf_list[i](dev, NULL, 0);
1935 done = gifconf_list[i](dev, pos + total,
1945 * All done. Write the updated control block back to the caller.
1947 ifc.ifc_len = total;
1950 * Both BSD and Solaris return 0 here, so we do too.
1952 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
1955 #ifdef CONFIG_PROC_FS
1957 * This is invoked by the /proc filesystem handler to display a device
1960 static __inline__ struct net_device *dev_get_idx(loff_t pos)
1962 struct net_device *dev;
1965 for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
1967 return i == pos ? dev : NULL;
1970 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
1972 read_lock(&dev_base_lock);
1973 return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
1976 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1979 return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
1982 void dev_seq_stop(struct seq_file *seq, void *v)
1984 read_unlock(&dev_base_lock);
1987 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
1989 struct nx_info *nxi = current->nx_info;
1991 if (vx_flags(VXF_HIDE_NETIF, 0) && !dev_in_nx_info(dev, nxi))
1993 if (dev->get_stats) {
1994 struct net_device_stats *stats = dev->get_stats(dev);
1996 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
1997 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
1998 dev->name, stats->rx_bytes, stats->rx_packets,
2000 stats->rx_dropped + stats->rx_missed_errors,
2001 stats->rx_fifo_errors,
2002 stats->rx_length_errors + stats->rx_over_errors +
2003 stats->rx_crc_errors + stats->rx_frame_errors,
2004 stats->rx_compressed, stats->multicast,
2005 stats->tx_bytes, stats->tx_packets,
2006 stats->tx_errors, stats->tx_dropped,
2007 stats->tx_fifo_errors, stats->collisions,
2008 stats->tx_carrier_errors +
2009 stats->tx_aborted_errors +
2010 stats->tx_window_errors +
2011 stats->tx_heartbeat_errors,
2012 stats->tx_compressed);
2014 seq_printf(seq, "%6s: No statistics available.\n", dev->name);
2018 * Called from the PROCfs module. This now uses the new arbitrary sized
2019 * /proc/net interface to create /proc/net/dev
2021 static int dev_seq_show(struct seq_file *seq, void *v)
2023 if (v == SEQ_START_TOKEN)
2024 seq_puts(seq, "Inter-| Receive "
2026 " face |bytes packets errs drop fifo frame "
2027 "compressed multicast|bytes packets errs "
2028 "drop fifo colls carrier compressed\n");
2030 dev_seq_printf_stats(seq, v);
2034 static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2036 struct netif_rx_stats *rc = NULL;
2038 while (*pos < NR_CPUS)
2039 if (cpu_online(*pos)) {
2040 rc = &per_cpu(netdev_rx_stat, *pos);
2047 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2049 return softnet_get_online(pos);
2052 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2055 return softnet_get_online(pos);
2058 static void softnet_seq_stop(struct seq_file *seq, void *v)
2062 static int softnet_seq_show(struct seq_file *seq, void *v)
2064 struct netif_rx_stats *s = v;
2066 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2067 s->total, s->dropped, s->time_squeeze, s->throttled,
2068 s->fastroute_hit, s->fastroute_success, s->fastroute_defer,
2069 s->fastroute_deferred_out,
2071 s->fastroute_latency_reduction
2079 static struct seq_operations dev_seq_ops = {
2080 .start = dev_seq_start,
2081 .next = dev_seq_next,
2082 .stop = dev_seq_stop,
2083 .show = dev_seq_show,
2086 static int dev_seq_open(struct inode *inode, struct file *file)
2088 return seq_open(file, &dev_seq_ops);
2091 static struct file_operations dev_seq_fops = {
2092 .owner = THIS_MODULE,
2093 .open = dev_seq_open,
2095 .llseek = seq_lseek,
2096 .release = seq_release,
2099 static struct seq_operations softnet_seq_ops = {
2100 .start = softnet_seq_start,
2101 .next = softnet_seq_next,
2102 .stop = softnet_seq_stop,
2103 .show = softnet_seq_show,
2106 static int softnet_seq_open(struct inode *inode, struct file *file)
2108 return seq_open(file, &softnet_seq_ops);
2111 static struct file_operations softnet_seq_fops = {
2112 .owner = THIS_MODULE,
2113 .open = softnet_seq_open,
2115 .llseek = seq_lseek,
2116 .release = seq_release,
2120 extern int wireless_proc_init(void);
2122 #define wireless_proc_init() 0
2125 static int __init dev_proc_init(void)
2129 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2131 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2133 if (wireless_proc_init())
2139 proc_net_remove("softnet_stat");
2141 proc_net_remove("dev");
2145 #define dev_proc_init() 0
2146 #endif /* CONFIG_PROC_FS */
2150 * netdev_set_master - set up master/slave pair
2151 * @slave: slave device
2152 * @master: new master device
2154 * Changes the master device of the slave. Pass %NULL to break the
2155 * bonding. The caller must hold the RTNL semaphore. On a failure
2156 * a negative errno code is returned. On success the reference counts
2157 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2158 * function returns zero.
2160 int netdev_set_master(struct net_device *slave, struct net_device *master)
2162 struct net_device *old = slave->master;
2172 slave->master = master;
2180 slave->flags |= IFF_SLAVE;
2182 slave->flags &= ~IFF_SLAVE;
2184 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2189 * dev_set_promiscuity - update promiscuity count on a device
2193 * Add or remove promsicuity from a device. While the count in the device
2194 * remains above zero the interface remains promiscuous. Once it hits zero
2195 * the device reverts back to normal filtering operation. A negative inc
2196 * value is used to drop promiscuity on the device.
2198 void dev_set_promiscuity(struct net_device *dev, int inc)
2200 unsigned short old_flags = dev->flags;
2202 dev->flags |= IFF_PROMISC;
2203 if ((dev->promiscuity += inc) == 0)
2204 dev->flags &= ~IFF_PROMISC;
2205 if (dev->flags ^ old_flags) {
2207 printk(KERN_INFO "device %s %s promiscuous mode\n",
2208 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2214 * dev_set_allmulti - update allmulti count on a device
2218 * Add or remove reception of all multicast frames to a device. While the
2219 * count in the device remains above zero the interface remains listening
2220 * to all interfaces. Once it hits zero the device reverts back to normal
2221 * filtering operation. A negative @inc value is used to drop the counter
2222 * when releasing a resource needing all multicasts.
2225 void dev_set_allmulti(struct net_device *dev, int inc)
2227 unsigned short old_flags = dev->flags;
2229 dev->flags |= IFF_ALLMULTI;
2230 if ((dev->allmulti += inc) == 0)
2231 dev->flags &= ~IFF_ALLMULTI;
2232 if (dev->flags ^ old_flags)
2236 unsigned dev_get_flags(const struct net_device *dev)
2240 flags = (dev->flags & ~(IFF_PROMISC |
2243 (dev->gflags & (IFF_PROMISC |
2246 if (netif_running(dev) && netif_carrier_ok(dev))
2247 flags |= IFF_RUNNING;
2252 int dev_change_flags(struct net_device *dev, unsigned flags)
2255 int old_flags = dev->flags;
2258 * Set the flags on our device.
2261 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2262 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2264 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2268 * Load in the correct multicast list now the flags have changed.
2274 * Have we downed the interface. We handle IFF_UP ourselves
2275 * according to user attempts to set it, rather than blindly
2280 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2281 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2287 if (dev->flags & IFF_UP &&
2288 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2290 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
2292 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2293 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2294 dev->gflags ^= IFF_PROMISC;
2295 dev_set_promiscuity(dev, inc);
2298 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2299 is important. Some (broken) drivers set IFF_PROMISC, when
2300 IFF_ALLMULTI is requested not asking us and not reporting.
2302 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2303 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2304 dev->gflags ^= IFF_ALLMULTI;
2305 dev_set_allmulti(dev, inc);
2308 if (old_flags ^ dev->flags)
2309 rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2314 int dev_set_mtu(struct net_device *dev, int new_mtu)
2318 if (new_mtu == dev->mtu)
2321 /* MTU must be positive. */
2325 if (!netif_device_present(dev))
2329 if (dev->change_mtu)
2330 err = dev->change_mtu(dev, new_mtu);
2333 if (!err && dev->flags & IFF_UP)
2334 notifier_call_chain(&netdev_chain,
2335 NETDEV_CHANGEMTU, dev);
2339 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2343 if (!dev->set_mac_address)
2345 if (sa->sa_family != dev->type)
2347 if (!netif_device_present(dev))
2349 err = dev->set_mac_address(dev, sa);
2351 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
2356 * Perform the SIOCxIFxxx calls.
2358 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2361 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2367 case SIOCGIFFLAGS: /* Get interface flags */
2368 ifr->ifr_flags = dev_get_flags(dev);
2371 case SIOCSIFFLAGS: /* Set interface flags */
2372 return dev_change_flags(dev, ifr->ifr_flags);
2374 case SIOCGIFMETRIC: /* Get the metric on the interface
2375 (currently unused) */
2376 ifr->ifr_metric = 0;
2379 case SIOCSIFMETRIC: /* Set the metric on the interface
2380 (currently unused) */
2383 case SIOCGIFMTU: /* Get the MTU of a device */
2384 ifr->ifr_mtu = dev->mtu;
2387 case SIOCSIFMTU: /* Set the MTU of a device */
2388 return dev_set_mtu(dev, ifr->ifr_mtu);
2392 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2394 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2395 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2396 ifr->ifr_hwaddr.sa_family = dev->type;
2400 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2402 case SIOCSIFHWBROADCAST:
2403 if (ifr->ifr_hwaddr.sa_family != dev->type)
2405 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2406 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2407 notifier_call_chain(&netdev_chain,
2408 NETDEV_CHANGEADDR, dev);
2412 ifr->ifr_map.mem_start = dev->mem_start;
2413 ifr->ifr_map.mem_end = dev->mem_end;
2414 ifr->ifr_map.base_addr = dev->base_addr;
2415 ifr->ifr_map.irq = dev->irq;
2416 ifr->ifr_map.dma = dev->dma;
2417 ifr->ifr_map.port = dev->if_port;
2421 if (dev->set_config) {
2422 if (!netif_device_present(dev))
2424 return dev->set_config(dev, &ifr->ifr_map);
2429 if (!dev->set_multicast_list ||
2430 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2432 if (!netif_device_present(dev))
2434 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2438 if (!dev->set_multicast_list ||
2439 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2441 if (!netif_device_present(dev))
2443 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2447 ifr->ifr_ifindex = dev->ifindex;
2451 ifr->ifr_qlen = dev->tx_queue_len;
2455 if (ifr->ifr_qlen < 0)
2457 dev->tx_queue_len = ifr->ifr_qlen;
2461 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2462 return dev_change_name(dev, ifr->ifr_newname);
2465 * Unknown or private ioctl
2469 if ((cmd >= SIOCDEVPRIVATE &&
2470 cmd <= SIOCDEVPRIVATE + 15) ||
2471 cmd == SIOCBONDENSLAVE ||
2472 cmd == SIOCBONDRELEASE ||
2473 cmd == SIOCBONDSETHWADDR ||
2474 cmd == SIOCBONDSLAVEINFOQUERY ||
2475 cmd == SIOCBONDINFOQUERY ||
2476 cmd == SIOCBONDCHANGEACTIVE ||
2477 cmd == SIOCGMIIPHY ||
2478 cmd == SIOCGMIIREG ||
2479 cmd == SIOCSMIIREG ||
2480 cmd == SIOCBRADDIF ||
2481 cmd == SIOCBRDELIF ||
2482 cmd == SIOCWANDEV) {
2484 if (dev->do_ioctl) {
2485 if (netif_device_present(dev))
2486 err = dev->do_ioctl(dev, ifr,
2499 * This function handles all "interface"-type I/O control requests. The actual
2500 * 'doing' part of this is dev_ifsioc above.
2504 * dev_ioctl - network device ioctl
2505 * @cmd: command to issue
2506 * @arg: pointer to a struct ifreq in user space
2508 * Issue ioctl functions to devices. This is normally called by the
2509 * user space syscall interfaces but can sometimes be useful for
2510 * other purposes. The return value is the return from the syscall if
2511 * positive or a negative errno code on error.
2514 int dev_ioctl(unsigned int cmd, void __user *arg)
2520 /* One special case: SIOCGIFCONF takes ifconf argument
2521 and requires shared lock, because it sleeps writing
2525 if (cmd == SIOCGIFCONF) {
2527 ret = dev_ifconf((char __user *) arg);
2531 if (cmd == SIOCGIFNAME)
2532 return dev_ifname((struct ifreq __user *)arg);
2534 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2537 ifr.ifr_name[IFNAMSIZ-1] = 0;
2539 colon = strchr(ifr.ifr_name, ':');
2544 * See which interface the caller is talking about.
2549 * These ioctl calls:
2550 * - can be done by all.
2551 * - atomic and do not require locking.
2562 dev_load(ifr.ifr_name);
2563 read_lock(&dev_base_lock);
2564 ret = dev_ifsioc(&ifr, cmd);
2565 read_unlock(&dev_base_lock);
2569 if (copy_to_user(arg, &ifr,
2570 sizeof(struct ifreq)))
2576 dev_load(ifr.ifr_name);
2578 ret = dev_ethtool(&ifr);
2583 if (copy_to_user(arg, &ifr,
2584 sizeof(struct ifreq)))
2590 * These ioctl calls:
2591 * - require superuser power.
2592 * - require strict serialization.
2598 if (!capable(CAP_NET_ADMIN))
2600 dev_load(ifr.ifr_name);
2602 ret = dev_ifsioc(&ifr, cmd);
2607 if (copy_to_user(arg, &ifr,
2608 sizeof(struct ifreq)))
2614 * These ioctl calls:
2615 * - require superuser power.
2616 * - require strict serialization.
2617 * - do not return a value
2627 case SIOCSIFHWBROADCAST:
2630 case SIOCBONDENSLAVE:
2631 case SIOCBONDRELEASE:
2632 case SIOCBONDSETHWADDR:
2633 case SIOCBONDSLAVEINFOQUERY:
2634 case SIOCBONDINFOQUERY:
2635 case SIOCBONDCHANGEACTIVE:
2638 if (!capable(CAP_NET_ADMIN))
2640 dev_load(ifr.ifr_name);
2642 ret = dev_ifsioc(&ifr, cmd);
2647 /* Get the per device memory space. We can add this but
2648 * currently do not support it */
2650 /* Set the per device memory buffer space.
2651 * Not applicable in our case */
2656 * Unknown or private ioctl.
2659 if (cmd == SIOCWANDEV ||
2660 (cmd >= SIOCDEVPRIVATE &&
2661 cmd <= SIOCDEVPRIVATE + 15)) {
2662 dev_load(ifr.ifr_name);
2664 ret = dev_ifsioc(&ifr, cmd);
2666 if (!ret && copy_to_user(arg, &ifr,
2667 sizeof(struct ifreq)))
2672 /* Take care of Wireless Extensions */
2673 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2674 /* If command is `set a parameter', or
2675 * `get the encoding parameters', check if
2676 * the user has the right to do it */
2677 if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE) {
2678 if (!capable(CAP_NET_ADMIN))
2681 dev_load(ifr.ifr_name);
2683 /* Follow me in net/core/wireless.c */
2684 ret = wireless_process_ioctl(&ifr, cmd);
2686 if (IW_IS_GET(cmd) &&
2687 copy_to_user(arg, &ifr,
2688 sizeof(struct ifreq)))
2692 #endif /* WIRELESS_EXT */
2699 * dev_new_index - allocate an ifindex
2701 * Returns a suitable unique value for a new device interface
2702 * number. The caller must hold the rtnl semaphore or the
2703 * dev_base_lock to be sure it remains unique.
2705 static int dev_new_index(void)
2711 if (!__dev_get_by_index(ifindex))
2716 static int dev_boot_phase = 1;
2718 /* Delayed registration/unregisteration */
2719 static DEFINE_SPINLOCK(net_todo_list_lock);
2720 static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
2722 static inline void net_set_todo(struct net_device *dev)
2724 spin_lock(&net_todo_list_lock);
2725 list_add_tail(&dev->todo_list, &net_todo_list);
2726 spin_unlock(&net_todo_list_lock);
2730 * register_netdevice - register a network device
2731 * @dev: device to register
2733 * Take a completed network device structure and add it to the kernel
2734 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2735 * chain. 0 is returned on success. A negative errno code is returned
2736 * on a failure to set up the device, or if the name is a duplicate.
2738 * Callers must hold the rtnl semaphore. You may want
2739 * register_netdev() instead of this.
2742 * The locking appears insufficient to guarantee two parallel registers
2743 * will not get the same name.
2746 int register_netdevice(struct net_device *dev)
2748 struct hlist_head *head;
2749 struct hlist_node *p;
2752 BUG_ON(dev_boot_phase);
2755 /* When net_device's are persistent, this will be fatal. */
2756 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
2758 spin_lock_init(&dev->queue_lock);
2759 spin_lock_init(&dev->xmit_lock);
2760 dev->xmit_lock_owner = -1;
2761 #ifdef CONFIG_NET_CLS_ACT
2762 spin_lock_init(&dev->ingress_lock);
2765 ret = alloc_divert_blk(dev);
2771 /* Init, if this function is available */
2773 ret = dev->init(dev);
2781 if (!dev_valid_name(dev->name)) {
2786 dev->ifindex = dev_new_index();
2787 if (dev->iflink == -1)
2788 dev->iflink = dev->ifindex;
2790 /* Check for existence of name */
2791 head = dev_name_hash(dev->name);
2792 hlist_for_each(p, head) {
2793 struct net_device *d
2794 = hlist_entry(p, struct net_device, name_hlist);
2795 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
2801 /* Fix illegal SG+CSUM combinations. */
2802 if ((dev->features & NETIF_F_SG) &&
2803 !(dev->features & (NETIF_F_IP_CSUM |
2805 NETIF_F_HW_CSUM))) {
2806 printk("%s: Dropping NETIF_F_SG since no checksum feature.\n",
2808 dev->features &= ~NETIF_F_SG;
2811 /* TSO requires that SG is present as well. */
2812 if ((dev->features & NETIF_F_TSO) &&
2813 !(dev->features & NETIF_F_SG)) {
2814 printk("%s: Dropping NETIF_F_TSO since no SG feature.\n",
2816 dev->features &= ~NETIF_F_TSO;
2820 * nil rebuild_header routine,
2821 * that should be never called and used as just bug trap.
2824 if (!dev->rebuild_header)
2825 dev->rebuild_header = default_rebuild_header;
2828 * Default initial state at registry is that the
2829 * device is present.
2832 set_bit(__LINK_STATE_PRESENT, &dev->state);
2835 dev_init_scheduler(dev);
2836 write_lock_bh(&dev_base_lock);
2838 dev_tail = &dev->next;
2839 hlist_add_head(&dev->name_hlist, head);
2840 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
2842 dev->reg_state = NETREG_REGISTERING;
2843 write_unlock_bh(&dev_base_lock);
2845 /* Notify protocols, that a new device appeared. */
2846 notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
2848 /* Finish registration after unlock */
2855 free_divert_blk(dev);
2860 * register_netdev - register a network device
2861 * @dev: device to register
2863 * Take a completed network device structure and add it to the kernel
2864 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2865 * chain. 0 is returned on success. A negative errno code is returned
2866 * on a failure to set up the device, or if the name is a duplicate.
2868 * This is a wrapper around register_netdev that takes the rtnl semaphore
2869 * and expands the device name if you passed a format string to
2872 int register_netdev(struct net_device *dev)
2879 * If the name is a format string the caller wants us to do a
2882 if (strchr(dev->name, '%')) {
2883 err = dev_alloc_name(dev, dev->name);
2889 * Back compatibility hook. Kill this one in 2.5
2891 if (dev->name[0] == 0 || dev->name[0] == ' ') {
2892 err = dev_alloc_name(dev, "eth%d");
2897 err = register_netdevice(dev);
2902 EXPORT_SYMBOL(register_netdev);
2905 * netdev_wait_allrefs - wait until all references are gone.
2907 * This is called when unregistering network devices.
2909 * Any protocol or device that holds a reference should register
2910 * for netdevice notification, and cleanup and put back the
2911 * reference if they receive an UNREGISTER event.
2912 * We can get stuck here if buggy protocols don't correctly
2915 static void netdev_wait_allrefs(struct net_device *dev)
2917 unsigned long rebroadcast_time, warning_time;
2919 rebroadcast_time = warning_time = jiffies;
2920 while (atomic_read(&dev->refcnt) != 0) {
2921 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
2924 /* Rebroadcast unregister notification */
2925 notifier_call_chain(&netdev_chain,
2926 NETDEV_UNREGISTER, dev);
2928 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
2930 /* We must not have linkwatch events
2931 * pending on unregister. If this
2932 * happens, we simply run the queue
2933 * unscheduled, resulting in a noop
2936 linkwatch_run_queue();
2941 rebroadcast_time = jiffies;
2946 if (time_after(jiffies, warning_time + 10 * HZ)) {
2947 printk(KERN_EMERG "unregister_netdevice: "
2948 "waiting for %s to become free. Usage "
2950 dev->name, atomic_read(&dev->refcnt));
2951 warning_time = jiffies;
2960 * register_netdevice(x1);
2961 * register_netdevice(x2);
2963 * unregister_netdevice(y1);
2964 * unregister_netdevice(y2);
2970 * We are invoked by rtnl_unlock() after it drops the semaphore.
2971 * This allows us to deal with problems:
2972 * 1) We can create/delete sysfs objects which invoke hotplug
2973 * without deadlocking with linkwatch via keventd.
2974 * 2) Since we run with the RTNL semaphore not held, we can sleep
2975 * safely in order to wait for the netdev refcnt to drop to zero.
2977 static DECLARE_MUTEX(net_todo_run_mutex);
2978 void netdev_run_todo(void)
2980 struct list_head list = LIST_HEAD_INIT(list);
2984 /* Need to guard against multiple cpu's getting out of order. */
2985 down(&net_todo_run_mutex);
2987 /* Not safe to do outside the semaphore. We must not return
2988 * until all unregister events invoked by the local processor
2989 * have been completed (either by this todo run, or one on
2992 if (list_empty(&net_todo_list))
2995 /* Snapshot list, allow later requests */
2996 spin_lock(&net_todo_list_lock);
2997 list_splice_init(&net_todo_list, &list);
2998 spin_unlock(&net_todo_list_lock);
3000 while (!list_empty(&list)) {
3001 struct net_device *dev
3002 = list_entry(list.next, struct net_device, todo_list);
3003 list_del(&dev->todo_list);
3005 switch(dev->reg_state) {
3006 case NETREG_REGISTERING:
3007 err = netdev_register_sysfs(dev);
3009 printk(KERN_ERR "%s: failed sysfs registration (%d)\n",
3011 dev->reg_state = NETREG_REGISTERED;
3014 case NETREG_UNREGISTERING:
3015 netdev_unregister_sysfs(dev);
3016 dev->reg_state = NETREG_UNREGISTERED;
3018 netdev_wait_allrefs(dev);
3021 BUG_ON(atomic_read(&dev->refcnt));
3022 BUG_TRAP(!dev->ip_ptr);
3023 BUG_TRAP(!dev->ip6_ptr);
3024 BUG_TRAP(!dev->dn_ptr);
3027 /* It must be the very last action,
3028 * after this 'dev' may point to freed up memory.
3030 if (dev->destructor)
3031 dev->destructor(dev);
3035 printk(KERN_ERR "network todo '%s' but state %d\n",
3036 dev->name, dev->reg_state);
3042 up(&net_todo_run_mutex);
3046 * alloc_netdev - allocate network device
3047 * @sizeof_priv: size of private data to allocate space for
3048 * @name: device name format string
3049 * @setup: callback to initialize device
3051 * Allocates a struct net_device with private data area for driver use
3052 * and performs basic initialization.
3054 struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3055 void (*setup)(struct net_device *))
3058 struct net_device *dev;
3061 /* ensure 32-byte alignment of both the device and private area */
3062 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3063 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3065 p = kmalloc(alloc_size, GFP_KERNEL);
3067 printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
3070 memset(p, 0, alloc_size);
3072 dev = (struct net_device *)
3073 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3074 dev->padded = (char *)dev - (char *)p;
3077 dev->priv = netdev_priv(dev);
3080 strcpy(dev->name, name);
3083 EXPORT_SYMBOL(alloc_netdev);
3086 * free_netdev - free network device
3089 * This function does the last stage of destroying an allocated device
3090 * interface. The reference to the device object is released.
3091 * If this is the last reference then it will be freed.
3093 void free_netdev(struct net_device *dev)
3096 /* Compatiablity with error handling in drivers */
3097 if (dev->reg_state == NETREG_UNINITIALIZED) {
3098 kfree((char *)dev - dev->padded);
3102 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3103 dev->reg_state = NETREG_RELEASED;
3105 /* will free via class release */
3106 class_device_put(&dev->class_dev);
3108 kfree((char *)dev - dev->padded);
3112 /* Synchronize with packet receive processing. */
3113 void synchronize_net(void)
3120 * unregister_netdevice - remove device from the kernel
3123 * This function shuts down a device interface and removes it
3124 * from the kernel tables. On success 0 is returned, on a failure
3125 * a negative errno code is returned.
3127 * Callers must hold the rtnl semaphore. You may want
3128 * unregister_netdev() instead of this.
3131 int unregister_netdevice(struct net_device *dev)
3133 struct net_device *d, **dp;
3135 BUG_ON(dev_boot_phase);
3138 /* Some devices call without registering for initialization unwind. */
3139 if (dev->reg_state == NETREG_UNINITIALIZED) {
3140 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3141 "was registered\n", dev->name, dev);
3145 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3147 /* If device is running, close it first. */
3148 if (dev->flags & IFF_UP)
3151 /* And unlink it from device chain. */
3152 for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3154 write_lock_bh(&dev_base_lock);
3155 hlist_del(&dev->name_hlist);
3156 hlist_del(&dev->index_hlist);
3157 if (dev_tail == &dev->next)
3160 write_unlock_bh(&dev_base_lock);
3165 printk(KERN_ERR "unregister net_device: '%s' not found\n",
3170 dev->reg_state = NETREG_UNREGISTERING;
3174 /* Shutdown queueing discipline. */
3178 /* Notify protocols, that we are about to destroy
3179 this device. They should clean all the things.
3181 notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3184 * Flush the multicast chain
3186 dev_mc_discard(dev);
3191 /* Notifier chain MUST detach us from master device. */
3192 BUG_TRAP(!dev->master);
3194 free_divert_blk(dev);
3196 /* Finish processing unregister after unlock */
3206 * unregister_netdev - remove device from the kernel
3209 * This function shuts down a device interface and removes it
3210 * from the kernel tables. On success 0 is returned, on a failure
3211 * a negative errno code is returned.
3213 * This is just a wrapper for unregister_netdevice that takes
3214 * the rtnl semaphore. In general you want to use this and not
3215 * unregister_netdevice.
3217 void unregister_netdev(struct net_device *dev)
3220 unregister_netdevice(dev);
3224 EXPORT_SYMBOL(unregister_netdev);
3226 #ifdef CONFIG_HOTPLUG_CPU
3227 static int dev_cpu_callback(struct notifier_block *nfb,
3228 unsigned long action,
3231 struct sk_buff **list_skb;
3232 struct net_device **list_net;
3233 struct sk_buff *skb;
3234 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3235 struct softnet_data *sd, *oldsd;
3237 if (action != CPU_DEAD)
3240 local_irq_disable();
3241 cpu = smp_processor_id();
3242 sd = &per_cpu(softnet_data, cpu);
3243 oldsd = &per_cpu(softnet_data, oldcpu);
3245 /* Find end of our completion_queue. */
3246 list_skb = &sd->completion_queue;
3248 list_skb = &(*list_skb)->next;
3249 /* Append completion queue from offline CPU. */
3250 *list_skb = oldsd->completion_queue;
3251 oldsd->completion_queue = NULL;
3253 /* Find end of our output_queue. */
3254 list_net = &sd->output_queue;
3256 list_net = &(*list_net)->next_sched;
3257 /* Append output queue from offline CPU. */
3258 *list_net = oldsd->output_queue;
3259 oldsd->output_queue = NULL;
3261 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3264 /* Process offline CPU's input_pkt_queue */
3265 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3270 #endif /* CONFIG_HOTPLUG_CPU */
3274 * Initialize the DEV module. At boot time this walks the device list and
3275 * unhooks any devices that fail to initialise (normally hardware not
3276 * present) and leaves us with a valid list of present and active devices.
3281 * This is called single threaded during boot, so no need
3282 * to take the rtnl semaphore.
3284 static int __init net_dev_init(void)
3286 int i, rc = -ENOMEM;
3288 BUG_ON(!dev_boot_phase);
3292 if (dev_proc_init())
3295 if (netdev_sysfs_init())
3298 INIT_LIST_HEAD(&ptype_all);
3299 for (i = 0; i < 16; i++)
3300 INIT_LIST_HEAD(&ptype_base[i]);
3302 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3303 INIT_HLIST_HEAD(&dev_name_head[i]);
3305 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3306 INIT_HLIST_HEAD(&dev_index_head[i]);
3309 * Initialise the packet receive queues.
3312 for (i = 0; i < NR_CPUS; i++) {
3313 struct softnet_data *queue;
3315 queue = &per_cpu(softnet_data, i);
3316 skb_queue_head_init(&queue->input_pkt_queue);
3317 queue->throttle = 0;
3318 queue->cng_level = 0;
3319 queue->avg_blog = 10; /* arbitrary non-zero */
3320 queue->completion_queue = NULL;
3321 INIT_LIST_HEAD(&queue->poll_list);
3322 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3323 queue->backlog_dev.weight = weight_p;
3324 queue->backlog_dev.poll = process_backlog;
3325 atomic_set(&queue->backlog_dev.refcnt, 1);
3328 #ifdef OFFLINE_SAMPLE
3329 samp_timer.expires = jiffies + (10 * HZ);
3330 add_timer(&samp_timer);
3335 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3336 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3338 hotcpu_notifier(dev_cpu_callback, 0);
3346 subsys_initcall(net_dev_init);
3348 EXPORT_SYMBOL(__dev_get_by_index);
3349 EXPORT_SYMBOL(__dev_get_by_name);
3350 EXPORT_SYMBOL(__dev_remove_pack);
3351 EXPORT_SYMBOL(__skb_linearize);
3352 EXPORT_SYMBOL(dev_add_pack);
3353 EXPORT_SYMBOL(dev_alloc_name);
3354 EXPORT_SYMBOL(dev_close);
3355 EXPORT_SYMBOL(dev_get_by_flags);
3356 EXPORT_SYMBOL(dev_get_by_index);
3357 EXPORT_SYMBOL(dev_get_by_name);
3358 EXPORT_SYMBOL(dev_ioctl);
3359 EXPORT_SYMBOL(dev_open);
3360 EXPORT_SYMBOL(dev_queue_xmit);
3361 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
3362 EXPORT_SYMBOL(dev_queue_xmit_nit);
3364 EXPORT_SYMBOL(dev_remove_pack);
3365 EXPORT_SYMBOL(dev_set_allmulti);
3366 EXPORT_SYMBOL(dev_set_promiscuity);
3367 EXPORT_SYMBOL(dev_change_flags);
3368 EXPORT_SYMBOL(dev_change_name);
3369 EXPORT_SYMBOL(dev_set_mtu);
3370 EXPORT_SYMBOL(dev_set_mac_address);
3371 EXPORT_SYMBOL(free_netdev);
3372 EXPORT_SYMBOL(netdev_boot_setup_check);
3373 EXPORT_SYMBOL(netdev_set_master);
3374 EXPORT_SYMBOL(netdev_state_change);
3375 EXPORT_SYMBOL(netif_receive_skb);
3376 EXPORT_SYMBOL(netif_rx);
3377 EXPORT_SYMBOL(register_gifconf);
3378 EXPORT_SYMBOL(register_netdevice);
3379 EXPORT_SYMBOL(register_netdevice_notifier);
3380 EXPORT_SYMBOL(skb_checksum_help);
3381 EXPORT_SYMBOL(synchronize_net);
3382 EXPORT_SYMBOL(unregister_netdevice);
3383 EXPORT_SYMBOL(unregister_netdevice_notifier);
3384 EXPORT_SYMBOL(net_enable_timestamp);
3385 EXPORT_SYMBOL(net_disable_timestamp);
3386 EXPORT_SYMBOL(dev_get_flags);
3388 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3389 EXPORT_SYMBOL(br_handle_frame_hook);
3390 EXPORT_SYMBOL(br_fdb_get_hook);
3391 EXPORT_SYMBOL(br_fdb_put_hook);
3395 EXPORT_SYMBOL(dev_load);
3398 EXPORT_PER_CPU_SYMBOL(softnet_data);