[NET]: Disallow whitespace in network device names.
[linux-2.6/verdex.git] / net / core / dev.c
blobd4a1ec3bded5f6afa92c0cbb860101b509f0f338
1 /*
2 * NET3 Protocol independent device support routines.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Derived from the non IP parts of dev.c 1.0.19
10 * Authors: Ross Biro
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
14 * Additional Authors:
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <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>
22 * Changes:
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
34 * drivers
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
44 * call a packet.
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
50 * changes.
51 * Rudi Cilibrasi : Pass the right thing to
52 * set_mac_address()
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
58 * 1 device.
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
66 * the backlog queue.
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/capability.h>
79 #include <linux/cpu.h>
80 #include <linux/types.h>
81 #include <linux/kernel.h>
82 #include <linux/sched.h>
83 #include <linux/mutex.h>
84 #include <linux/string.h>
85 #include <linux/mm.h>
86 #include <linux/socket.h>
87 #include <linux/sockios.h>
88 #include <linux/errno.h>
89 #include <linux/interrupt.h>
90 #include <linux/if_ether.h>
91 #include <linux/netdevice.h>
92 #include <linux/etherdevice.h>
93 #include <linux/notifier.h>
94 #include <linux/skbuff.h>
95 #include <net/sock.h>
96 #include <linux/rtnetlink.h>
97 #include <linux/proc_fs.h>
98 #include <linux/seq_file.h>
99 #include <linux/stat.h>
100 #include <linux/if_bridge.h>
101 #include <linux/divert.h>
102 #include <net/dst.h>
103 #include <net/pkt_sched.h>
104 #include <net/checksum.h>
105 #include <linux/highmem.h>
106 #include <linux/init.h>
107 #include <linux/kmod.h>
108 #include <linux/module.h>
109 #include <linux/kallsyms.h>
110 #include <linux/netpoll.h>
111 #include <linux/rcupdate.h>
112 #include <linux/delay.h>
113 #include <linux/wireless.h>
114 #include <net/iw_handler.h>
115 #include <asm/current.h>
116 #include <linux/audit.h>
117 #include <linux/dmaengine.h>
118 #include <linux/err.h>
119 #include <linux/ctype.h>
122 * The list of packet types we will receive (as opposed to discard)
123 * and the routines to invoke.
125 * Why 16. Because with 16 the only overlap we get on a hash of the
126 * low nibble of the protocol value is RARP/SNAP/X.25.
128 * NOTE: That is no longer true with the addition of VLAN tags. Not
129 * sure which should go first, but I bet it won't make much
130 * difference if we are running VLANs. The good news is that
131 * this protocol won't be in the list unless compiled in, so
132 * the average user (w/out VLANs) will not be adversely affected.
133 * --BLG
135 * 0800 IP
136 * 8100 802.1Q VLAN
137 * 0001 802.3
138 * 0002 AX.25
139 * 0004 802.2
140 * 8035 RARP
141 * 0005 SNAP
142 * 0805 X.25
143 * 0806 ARP
144 * 8137 IPX
145 * 0009 Localtalk
146 * 86DD IPv6
149 static DEFINE_SPINLOCK(ptype_lock);
150 static struct list_head ptype_base[16]; /* 16 way hashed list */
151 static struct list_head ptype_all; /* Taps */
153 #ifdef CONFIG_NET_DMA
154 static struct dma_client *net_dma_client;
155 static unsigned int net_dma_count;
156 static spinlock_t net_dma_event_lock;
157 #endif
160 * The @dev_base list is protected by @dev_base_lock and the rtnl
161 * semaphore.
163 * Pure readers hold dev_base_lock for reading.
165 * Writers must hold the rtnl semaphore while they loop through the
166 * dev_base list, and hold dev_base_lock for writing when they do the
167 * actual updates. This allows pure readers to access the list even
168 * while a writer is preparing to update it.
170 * To put it another way, dev_base_lock is held for writing only to
171 * protect against pure readers; the rtnl semaphore provides the
172 * protection against other writers.
174 * See, for example usages, register_netdevice() and
175 * unregister_netdevice(), which must be called with the rtnl
176 * semaphore held.
178 struct net_device *dev_base;
179 static struct net_device **dev_tail = &dev_base;
180 DEFINE_RWLOCK(dev_base_lock);
182 EXPORT_SYMBOL(dev_base);
183 EXPORT_SYMBOL(dev_base_lock);
185 #define NETDEV_HASHBITS 8
186 static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
187 static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
189 static inline struct hlist_head *dev_name_hash(const char *name)
191 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
192 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
195 static inline struct hlist_head *dev_index_hash(int ifindex)
197 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
201 * Our notifier list
204 static RAW_NOTIFIER_HEAD(netdev_chain);
207 * Device drivers call our routines to queue packets here. We empty the
208 * queue in the local softnet handler.
210 DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL };
212 #ifdef CONFIG_SYSFS
213 extern int netdev_sysfs_init(void);
214 extern int netdev_register_sysfs(struct net_device *);
215 extern void netdev_unregister_sysfs(struct net_device *);
216 #else
217 #define netdev_sysfs_init() (0)
218 #define netdev_register_sysfs(dev) (0)
219 #define netdev_unregister_sysfs(dev) do { } while(0)
220 #endif
223 /*******************************************************************************
225 Protocol management and registration routines
227 *******************************************************************************/
230 * For efficiency
233 static int netdev_nit;
236 * Add a protocol ID to the list. Now that the input handler is
237 * smarter we can dispense with all the messy stuff that used to be
238 * here.
240 * BEWARE!!! Protocol handlers, mangling input packets,
241 * MUST BE last in hash buckets and checking protocol handlers
242 * MUST start from promiscuous ptype_all chain in net_bh.
243 * It is true now, do not change it.
244 * Explanation follows: if protocol handler, mangling packet, will
245 * be the first on list, it is not able to sense, that packet
246 * is cloned and should be copied-on-write, so that it will
247 * change it and subsequent readers will get broken packet.
248 * --ANK (980803)
252 * dev_add_pack - add packet handler
253 * @pt: packet type declaration
255 * Add a protocol handler to the networking stack. The passed &packet_type
256 * is linked into kernel lists and may not be freed until it has been
257 * removed from the kernel lists.
259 * This call does not sleep therefore it can not
260 * guarantee all CPU's that are in middle of receiving packets
261 * will see the new packet type (until the next received packet).
264 void dev_add_pack(struct packet_type *pt)
266 int hash;
268 spin_lock_bh(&ptype_lock);
269 if (pt->type == htons(ETH_P_ALL)) {
270 netdev_nit++;
271 list_add_rcu(&pt->list, &ptype_all);
272 } else {
273 hash = ntohs(pt->type) & 15;
274 list_add_rcu(&pt->list, &ptype_base[hash]);
276 spin_unlock_bh(&ptype_lock);
280 * __dev_remove_pack - remove packet handler
281 * @pt: packet type declaration
283 * Remove a protocol handler that was previously added to the kernel
284 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
285 * from the kernel lists and can be freed or reused once this function
286 * returns.
288 * The packet type might still be in use by receivers
289 * and must not be freed until after all the CPU's have gone
290 * through a quiescent state.
292 void __dev_remove_pack(struct packet_type *pt)
294 struct list_head *head;
295 struct packet_type *pt1;
297 spin_lock_bh(&ptype_lock);
299 if (pt->type == htons(ETH_P_ALL)) {
300 netdev_nit--;
301 head = &ptype_all;
302 } else
303 head = &ptype_base[ntohs(pt->type) & 15];
305 list_for_each_entry(pt1, head, list) {
306 if (pt == pt1) {
307 list_del_rcu(&pt->list);
308 goto out;
312 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
313 out:
314 spin_unlock_bh(&ptype_lock);
317 * dev_remove_pack - remove packet handler
318 * @pt: packet type declaration
320 * Remove a protocol handler that was previously added to the kernel
321 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
322 * from the kernel lists and can be freed or reused once this function
323 * returns.
325 * This call sleeps to guarantee that no CPU is looking at the packet
326 * type after return.
328 void dev_remove_pack(struct packet_type *pt)
330 __dev_remove_pack(pt);
332 synchronize_net();
335 /******************************************************************************
337 Device Boot-time Settings Routines
339 *******************************************************************************/
341 /* Boot time configuration table */
342 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
345 * netdev_boot_setup_add - add new setup entry
346 * @name: name of the device
347 * @map: configured settings for the device
349 * Adds new setup entry to the dev_boot_setup list. The function
350 * returns 0 on error and 1 on success. This is a generic routine to
351 * all netdevices.
353 static int netdev_boot_setup_add(char *name, struct ifmap *map)
355 struct netdev_boot_setup *s;
356 int i;
358 s = dev_boot_setup;
359 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
360 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
361 memset(s[i].name, 0, sizeof(s[i].name));
362 strcpy(s[i].name, name);
363 memcpy(&s[i].map, map, sizeof(s[i].map));
364 break;
368 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
372 * netdev_boot_setup_check - check boot time settings
373 * @dev: the netdevice
375 * Check boot time settings for the device.
376 * The found settings are set for the device to be used
377 * later in the device probing.
378 * Returns 0 if no settings found, 1 if they are.
380 int netdev_boot_setup_check(struct net_device *dev)
382 struct netdev_boot_setup *s = dev_boot_setup;
383 int i;
385 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
386 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
387 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
388 dev->irq = s[i].map.irq;
389 dev->base_addr = s[i].map.base_addr;
390 dev->mem_start = s[i].map.mem_start;
391 dev->mem_end = s[i].map.mem_end;
392 return 1;
395 return 0;
400 * netdev_boot_base - get address from boot time settings
401 * @prefix: prefix for network device
402 * @unit: id for network device
404 * Check boot time settings for the base address of device.
405 * The found settings are set for the device to be used
406 * later in the device probing.
407 * Returns 0 if no settings found.
409 unsigned long netdev_boot_base(const char *prefix, int unit)
411 const struct netdev_boot_setup *s = dev_boot_setup;
412 char name[IFNAMSIZ];
413 int i;
415 sprintf(name, "%s%d", prefix, unit);
418 * If device already registered then return base of 1
419 * to indicate not to probe for this interface
421 if (__dev_get_by_name(name))
422 return 1;
424 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
425 if (!strcmp(name, s[i].name))
426 return s[i].map.base_addr;
427 return 0;
431 * Saves at boot time configured settings for any netdevice.
433 int __init netdev_boot_setup(char *str)
435 int ints[5];
436 struct ifmap map;
438 str = get_options(str, ARRAY_SIZE(ints), ints);
439 if (!str || !*str)
440 return 0;
442 /* Save settings */
443 memset(&map, 0, sizeof(map));
444 if (ints[0] > 0)
445 map.irq = ints[1];
446 if (ints[0] > 1)
447 map.base_addr = ints[2];
448 if (ints[0] > 2)
449 map.mem_start = ints[3];
450 if (ints[0] > 3)
451 map.mem_end = ints[4];
453 /* Add new entry to the list */
454 return netdev_boot_setup_add(str, &map);
457 __setup("netdev=", netdev_boot_setup);
459 /*******************************************************************************
461 Device Interface Subroutines
463 *******************************************************************************/
466 * __dev_get_by_name - find a device by its name
467 * @name: name to find
469 * Find an interface by name. Must be called under RTNL semaphore
470 * or @dev_base_lock. If the name is found a pointer to the device
471 * is returned. If the name is not found then %NULL is returned. The
472 * reference counters are not incremented so the caller must be
473 * careful with locks.
476 struct net_device *__dev_get_by_name(const char *name)
478 struct hlist_node *p;
480 hlist_for_each(p, dev_name_hash(name)) {
481 struct net_device *dev
482 = hlist_entry(p, struct net_device, name_hlist);
483 if (!strncmp(dev->name, name, IFNAMSIZ))
484 return dev;
486 return NULL;
490 * dev_get_by_name - find a device by its name
491 * @name: name to find
493 * Find an interface by name. This can be called from any
494 * context and does its own locking. The returned handle has
495 * the usage count incremented and the caller must use dev_put() to
496 * release it when it is no longer needed. %NULL is returned if no
497 * matching device is found.
500 struct net_device *dev_get_by_name(const char *name)
502 struct net_device *dev;
504 read_lock(&dev_base_lock);
505 dev = __dev_get_by_name(name);
506 if (dev)
507 dev_hold(dev);
508 read_unlock(&dev_base_lock);
509 return dev;
513 * __dev_get_by_index - find a device by its ifindex
514 * @ifindex: index of device
516 * Search for an interface by index. Returns %NULL if the device
517 * is not found or a pointer to the device. The device has not
518 * had its reference counter increased so the caller must be careful
519 * about locking. The caller must hold either the RTNL semaphore
520 * or @dev_base_lock.
523 struct net_device *__dev_get_by_index(int ifindex)
525 struct hlist_node *p;
527 hlist_for_each(p, dev_index_hash(ifindex)) {
528 struct net_device *dev
529 = hlist_entry(p, struct net_device, index_hlist);
530 if (dev->ifindex == ifindex)
531 return dev;
533 return NULL;
538 * dev_get_by_index - find a device by its ifindex
539 * @ifindex: index of device
541 * Search for an interface by index. Returns NULL if the device
542 * is not found or a pointer to the device. The device returned has
543 * had a reference added and the pointer is safe until the user calls
544 * dev_put to indicate they have finished with it.
547 struct net_device *dev_get_by_index(int ifindex)
549 struct net_device *dev;
551 read_lock(&dev_base_lock);
552 dev = __dev_get_by_index(ifindex);
553 if (dev)
554 dev_hold(dev);
555 read_unlock(&dev_base_lock);
556 return dev;
560 * dev_getbyhwaddr - find a device by its hardware address
561 * @type: media type of device
562 * @ha: hardware address
564 * Search for an interface by MAC address. Returns NULL if the device
565 * is not found or a pointer to the device. The caller must hold the
566 * rtnl semaphore. The returned device has not had its ref count increased
567 * and the caller must therefore be careful about locking
569 * BUGS:
570 * If the API was consistent this would be __dev_get_by_hwaddr
573 struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
575 struct net_device *dev;
577 ASSERT_RTNL();
579 for (dev = dev_base; dev; dev = dev->next)
580 if (dev->type == type &&
581 !memcmp(dev->dev_addr, ha, dev->addr_len))
582 break;
583 return dev;
586 EXPORT_SYMBOL(dev_getbyhwaddr);
588 struct net_device *dev_getfirstbyhwtype(unsigned short type)
590 struct net_device *dev;
592 rtnl_lock();
593 for (dev = dev_base; dev; dev = dev->next) {
594 if (dev->type == type) {
595 dev_hold(dev);
596 break;
599 rtnl_unlock();
600 return dev;
603 EXPORT_SYMBOL(dev_getfirstbyhwtype);
606 * dev_get_by_flags - find any device with given flags
607 * @if_flags: IFF_* values
608 * @mask: bitmask of bits in if_flags to check
610 * Search for any interface with the given flags. Returns NULL if a device
611 * is not found or a pointer to the device. The device returned has
612 * had a reference added and the pointer is safe until the user calls
613 * dev_put to indicate they have finished with it.
616 struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
618 struct net_device *dev;
620 read_lock(&dev_base_lock);
621 for (dev = dev_base; dev != NULL; dev = dev->next) {
622 if (((dev->flags ^ if_flags) & mask) == 0) {
623 dev_hold(dev);
624 break;
627 read_unlock(&dev_base_lock);
628 return dev;
632 * dev_valid_name - check if name is okay for network device
633 * @name: name string
635 * Network device names need to be valid file names to
636 * to allow sysfs to work. We also disallow any kind of
637 * whitespace.
639 int dev_valid_name(const char *name)
641 if (*name == '\0')
642 return 0;
643 if (!strcmp(name, ".") || !strcmp(name, ".."))
644 return 0;
646 while (*name) {
647 if (*name == '/' || isspace(*name))
648 return 0;
649 name++;
651 return 1;
655 * dev_alloc_name - allocate a name for a device
656 * @dev: device
657 * @name: name format string
659 * Passed a format string - eg "lt%d" it will try and find a suitable
660 * id. It scans list of devices to build up a free map, then chooses
661 * the first empty slot. The caller must hold the dev_base or rtnl lock
662 * while allocating the name and adding the device in order to avoid
663 * duplicates.
664 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
665 * Returns the number of the unit assigned or a negative errno code.
668 int dev_alloc_name(struct net_device *dev, const char *name)
670 int i = 0;
671 char buf[IFNAMSIZ];
672 const char *p;
673 const int max_netdevices = 8*PAGE_SIZE;
674 long *inuse;
675 struct net_device *d;
677 p = strnchr(name, IFNAMSIZ-1, '%');
678 if (p) {
680 * Verify the string as this thing may have come from
681 * the user. There must be either one "%d" and no other "%"
682 * characters.
684 if (p[1] != 'd' || strchr(p + 2, '%'))
685 return -EINVAL;
687 /* Use one page as a bit array of possible slots */
688 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
689 if (!inuse)
690 return -ENOMEM;
692 for (d = dev_base; d; d = d->next) {
693 if (!sscanf(d->name, name, &i))
694 continue;
695 if (i < 0 || i >= max_netdevices)
696 continue;
698 /* avoid cases where sscanf is not exact inverse of printf */
699 snprintf(buf, sizeof(buf), name, i);
700 if (!strncmp(buf, d->name, IFNAMSIZ))
701 set_bit(i, inuse);
704 i = find_first_zero_bit(inuse, max_netdevices);
705 free_page((unsigned long) inuse);
708 snprintf(buf, sizeof(buf), name, i);
709 if (!__dev_get_by_name(buf)) {
710 strlcpy(dev->name, buf, IFNAMSIZ);
711 return i;
714 /* It is possible to run out of possible slots
715 * when the name is long and there isn't enough space left
716 * for the digits, or if all bits are used.
718 return -ENFILE;
723 * dev_change_name - change name of a device
724 * @dev: device
725 * @newname: name (or format string) must be at least IFNAMSIZ
727 * Change name of a device, can pass format strings "eth%d".
728 * for wildcarding.
730 int dev_change_name(struct net_device *dev, char *newname)
732 int err = 0;
734 ASSERT_RTNL();
736 if (dev->flags & IFF_UP)
737 return -EBUSY;
739 if (!dev_valid_name(newname))
740 return -EINVAL;
742 if (strchr(newname, '%')) {
743 err = dev_alloc_name(dev, newname);
744 if (err < 0)
745 return err;
746 strcpy(newname, dev->name);
748 else if (__dev_get_by_name(newname))
749 return -EEXIST;
750 else
751 strlcpy(dev->name, newname, IFNAMSIZ);
753 err = class_device_rename(&dev->class_dev, dev->name);
754 if (!err) {
755 hlist_del(&dev->name_hlist);
756 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
757 raw_notifier_call_chain(&netdev_chain,
758 NETDEV_CHANGENAME, dev);
761 return err;
765 * netdev_features_change - device changes features
766 * @dev: device to cause notification
768 * Called to indicate a device has changed features.
770 void netdev_features_change(struct net_device *dev)
772 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
774 EXPORT_SYMBOL(netdev_features_change);
777 * netdev_state_change - device changes state
778 * @dev: device to cause notification
780 * Called to indicate a device has changed state. This function calls
781 * the notifier chains for netdev_chain and sends a NEWLINK message
782 * to the routing socket.
784 void netdev_state_change(struct net_device *dev)
786 if (dev->flags & IFF_UP) {
787 raw_notifier_call_chain(&netdev_chain,
788 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!!!");
818 kfree_skb(skb);
819 return 1;
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)
837 int ret = 0;
840 * Is it already up?
843 if (dev->flags & IFF_UP)
844 return 0;
847 * Is it even present?
849 if (!netif_device_present(dev))
850 return -ENODEV;
853 * Call device private open method
855 set_bit(__LINK_STATE_START, &dev->state);
856 if (dev->open) {
857 ret = dev->open(dev);
858 if (ret)
859 clear_bit(__LINK_STATE_START, &dev->state);
863 * If it went open OK then:
866 if (!ret) {
868 * Set the flags.
870 dev->flags |= IFF_UP;
873 * Initialize multicasting status
875 dev_mc_upload(dev);
878 * Wakeup transmit queue engine
880 dev_activate(dev);
883 * ... and announce new interface.
885 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
887 return ret;
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
897 * chain.
899 int dev_close(struct net_device *dev)
901 if (!(dev->flags & IFF_UP))
902 return 0;
905 * Tell people we are going down, so that they can
906 * prepare to death, when device is still operating.
908 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
910 dev_deactivate(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)) {
922 /* No hurry. */
923 msleep(1);
927 * Call the device specific close. This cannot fail.
928 * Only if device is UP
930 * We allow it to be called even after a DETACH hot-plug
931 * event.
933 if (dev->stop)
934 dev->stop(dev);
937 * Device is now down.
940 dev->flags &= ~IFF_UP;
943 * Tell people we are down
945 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
947 return 0;
952 * Device change register/unregister. These are not inline or static
953 * as we export them to the world.
957 * register_netdevice_notifier - register a network notifier block
958 * @nb: notifier
960 * Register a notifier to be called when network device events occur.
961 * The notifier passed is linked into the kernel structures and must
962 * not be reused until it has been unregistered. A negative errno code
963 * is returned on a failure.
965 * When registered all registration and up events are replayed
966 * to the new notifier to allow device to have a race free
967 * view of the network device list.
970 int register_netdevice_notifier(struct notifier_block *nb)
972 struct net_device *dev;
973 int err;
975 rtnl_lock();
976 err = raw_notifier_chain_register(&netdev_chain, nb);
977 if (!err) {
978 for (dev = dev_base; dev; dev = dev->next) {
979 nb->notifier_call(nb, NETDEV_REGISTER, dev);
981 if (dev->flags & IFF_UP)
982 nb->notifier_call(nb, NETDEV_UP, dev);
985 rtnl_unlock();
986 return err;
990 * unregister_netdevice_notifier - unregister a network notifier block
991 * @nb: notifier
993 * Unregister a notifier previously registered by
994 * register_netdevice_notifier(). The notifier is unlinked into the
995 * kernel structures and may then be reused. A negative errno code
996 * is returned on a failure.
999 int unregister_netdevice_notifier(struct notifier_block *nb)
1001 int err;
1003 rtnl_lock();
1004 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1005 rtnl_unlock();
1006 return err;
1010 * call_netdevice_notifiers - call all network notifier blocks
1011 * @val: value passed unmodified to notifier function
1012 * @v: pointer passed unmodified to notifier function
1014 * Call all network notifier blocks. Parameters and return value
1015 * are as for raw_notifier_call_chain().
1018 int call_netdevice_notifiers(unsigned long val, void *v)
1020 return raw_notifier_call_chain(&netdev_chain, val, v);
1023 /* When > 0 there are consumers of rx skb time stamps */
1024 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1026 void net_enable_timestamp(void)
1028 atomic_inc(&netstamp_needed);
1031 void net_disable_timestamp(void)
1033 atomic_dec(&netstamp_needed);
1036 void __net_timestamp(struct sk_buff *skb)
1038 struct timeval tv;
1040 do_gettimeofday(&tv);
1041 skb_set_timestamp(skb, &tv);
1043 EXPORT_SYMBOL(__net_timestamp);
1045 static inline void net_timestamp(struct sk_buff *skb)
1047 if (atomic_read(&netstamp_needed))
1048 __net_timestamp(skb);
1049 else {
1050 skb->tstamp.off_sec = 0;
1051 skb->tstamp.off_usec = 0;
1056 * Support routine. Sends outgoing frames to any network
1057 * taps currently in use.
1060 static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1062 struct packet_type *ptype;
1064 net_timestamp(skb);
1066 rcu_read_lock();
1067 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1068 /* Never send packets back to the socket
1069 * they originated from - MvS (miquels@drinkel.ow.org)
1071 if ((ptype->dev == dev || !ptype->dev) &&
1072 (ptype->af_packet_priv == NULL ||
1073 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1074 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1075 if (!skb2)
1076 break;
1078 /* skb->nh should be correctly
1079 set by sender, so that the second statement is
1080 just protection against buggy protocols.
1082 skb2->mac.raw = skb2->data;
1084 if (skb2->nh.raw < skb2->data ||
1085 skb2->nh.raw > skb2->tail) {
1086 if (net_ratelimit())
1087 printk(KERN_CRIT "protocol %04x is "
1088 "buggy, dev %s\n",
1089 skb2->protocol, dev->name);
1090 skb2->nh.raw = skb2->data;
1093 skb2->h.raw = skb2->nh.raw;
1094 skb2->pkt_type = PACKET_OUTGOING;
1095 ptype->func(skb2, skb->dev, ptype, skb->dev);
1098 rcu_read_unlock();
1102 void __netif_schedule(struct net_device *dev)
1104 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1105 unsigned long flags;
1106 struct softnet_data *sd;
1108 local_irq_save(flags);
1109 sd = &__get_cpu_var(softnet_data);
1110 dev->next_sched = sd->output_queue;
1111 sd->output_queue = dev;
1112 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1113 local_irq_restore(flags);
1116 EXPORT_SYMBOL(__netif_schedule);
1118 void __netif_rx_schedule(struct net_device *dev)
1120 unsigned long flags;
1122 local_irq_save(flags);
1123 dev_hold(dev);
1124 list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
1125 if (dev->quota < 0)
1126 dev->quota += dev->weight;
1127 else
1128 dev->quota = dev->weight;
1129 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1130 local_irq_restore(flags);
1132 EXPORT_SYMBOL(__netif_rx_schedule);
1134 void dev_kfree_skb_any(struct sk_buff *skb)
1136 if (in_irq() || irqs_disabled())
1137 dev_kfree_skb_irq(skb);
1138 else
1139 dev_kfree_skb(skb);
1141 EXPORT_SYMBOL(dev_kfree_skb_any);
1144 /* Hot-plugging. */
1145 void netif_device_detach(struct net_device *dev)
1147 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1148 netif_running(dev)) {
1149 netif_stop_queue(dev);
1152 EXPORT_SYMBOL(netif_device_detach);
1154 void netif_device_attach(struct net_device *dev)
1156 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1157 netif_running(dev)) {
1158 netif_wake_queue(dev);
1159 __netdev_watchdog_up(dev);
1162 EXPORT_SYMBOL(netif_device_attach);
1166 * Invalidate hardware checksum when packet is to be mangled, and
1167 * complete checksum manually on outgoing path.
1169 int skb_checksum_help(struct sk_buff *skb, int inward)
1171 unsigned int csum;
1172 int ret = 0, offset = skb->h.raw - skb->data;
1174 if (inward)
1175 goto out_set_summed;
1177 if (unlikely(skb_shinfo(skb)->gso_size)) {
1178 /* Let GSO fix up the checksum. */
1179 goto out_set_summed;
1182 if (skb_cloned(skb)) {
1183 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1184 if (ret)
1185 goto out;
1188 BUG_ON(offset > (int)skb->len);
1189 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1191 offset = skb->tail - skb->h.raw;
1192 BUG_ON(offset <= 0);
1193 BUG_ON(skb->csum + 2 > offset);
1195 *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
1197 out_set_summed:
1198 skb->ip_summed = CHECKSUM_NONE;
1199 out:
1200 return ret;
1204 * skb_gso_segment - Perform segmentation on skb.
1205 * @skb: buffer to segment
1206 * @features: features for the output path (see dev->features)
1208 * This function segments the given skb and returns a list of segments.
1210 * It may return NULL if the skb requires no segmentation. This is
1211 * only possible when GSO is used for verifying header integrity.
1213 struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
1215 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1216 struct packet_type *ptype;
1217 int type = skb->protocol;
1218 int err;
1220 BUG_ON(skb_shinfo(skb)->frag_list);
1222 skb->mac.raw = skb->data;
1223 skb->mac_len = skb->nh.raw - skb->data;
1224 __skb_pull(skb, skb->mac_len);
1226 if (unlikely(skb->ip_summed != CHECKSUM_HW)) {
1227 if (skb_header_cloned(skb) &&
1228 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1229 return ERR_PTR(err);
1232 rcu_read_lock();
1233 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1234 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
1235 if (unlikely(skb->ip_summed != CHECKSUM_HW)) {
1236 err = ptype->gso_send_check(skb);
1237 segs = ERR_PTR(err);
1238 if (err || skb_gso_ok(skb, features))
1239 break;
1240 __skb_push(skb, skb->data - skb->nh.raw);
1242 segs = ptype->gso_segment(skb, features);
1243 break;
1246 rcu_read_unlock();
1248 __skb_push(skb, skb->data - skb->mac.raw);
1250 return segs;
1253 EXPORT_SYMBOL(skb_gso_segment);
1255 /* Take action when hardware reception checksum errors are detected. */
1256 #ifdef CONFIG_BUG
1257 void netdev_rx_csum_fault(struct net_device *dev)
1259 if (net_ratelimit()) {
1260 printk(KERN_ERR "%s: hw csum failure.\n",
1261 dev ? dev->name : "<unknown>");
1262 dump_stack();
1265 EXPORT_SYMBOL(netdev_rx_csum_fault);
1266 #endif
1268 /* Actually, we should eliminate this check as soon as we know, that:
1269 * 1. IOMMU is present and allows to map all the memory.
1270 * 2. No high memory really exists on this machine.
1273 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1275 #ifdef CONFIG_HIGHMEM
1276 int i;
1278 if (dev->features & NETIF_F_HIGHDMA)
1279 return 0;
1281 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1282 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1283 return 1;
1285 #endif
1286 return 0;
1289 struct dev_gso_cb {
1290 void (*destructor)(struct sk_buff *skb);
1293 #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1295 static void dev_gso_skb_destructor(struct sk_buff *skb)
1297 struct dev_gso_cb *cb;
1299 do {
1300 struct sk_buff *nskb = skb->next;
1302 skb->next = nskb->next;
1303 nskb->next = NULL;
1304 kfree_skb(nskb);
1305 } while (skb->next);
1307 cb = DEV_GSO_CB(skb);
1308 if (cb->destructor)
1309 cb->destructor(skb);
1313 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1314 * @skb: buffer to segment
1316 * This function segments the given skb and stores the list of segments
1317 * in skb->next.
1319 static int dev_gso_segment(struct sk_buff *skb)
1321 struct net_device *dev = skb->dev;
1322 struct sk_buff *segs;
1323 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1324 NETIF_F_SG : 0);
1326 segs = skb_gso_segment(skb, features);
1328 /* Verifying header integrity only. */
1329 if (!segs)
1330 return 0;
1332 if (unlikely(IS_ERR(segs)))
1333 return PTR_ERR(segs);
1335 skb->next = segs;
1336 DEV_GSO_CB(skb)->destructor = skb->destructor;
1337 skb->destructor = dev_gso_skb_destructor;
1339 return 0;
1342 int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1344 if (likely(!skb->next)) {
1345 if (netdev_nit)
1346 dev_queue_xmit_nit(skb, dev);
1348 if (netif_needs_gso(dev, skb)) {
1349 if (unlikely(dev_gso_segment(skb)))
1350 goto out_kfree_skb;
1351 if (skb->next)
1352 goto gso;
1355 return dev->hard_start_xmit(skb, dev);
1358 gso:
1359 do {
1360 struct sk_buff *nskb = skb->next;
1361 int rc;
1363 skb->next = nskb->next;
1364 nskb->next = NULL;
1365 rc = dev->hard_start_xmit(nskb, dev);
1366 if (unlikely(rc)) {
1367 nskb->next = skb->next;
1368 skb->next = nskb;
1369 return rc;
1371 if (unlikely(netif_queue_stopped(dev) && skb->next))
1372 return NETDEV_TX_BUSY;
1373 } while (skb->next);
1375 skb->destructor = DEV_GSO_CB(skb)->destructor;
1377 out_kfree_skb:
1378 kfree_skb(skb);
1379 return 0;
1382 #define HARD_TX_LOCK(dev, cpu) { \
1383 if ((dev->features & NETIF_F_LLTX) == 0) { \
1384 netif_tx_lock(dev); \
1388 #define HARD_TX_UNLOCK(dev) { \
1389 if ((dev->features & NETIF_F_LLTX) == 0) { \
1390 netif_tx_unlock(dev); \
1395 * dev_queue_xmit - transmit a buffer
1396 * @skb: buffer to transmit
1398 * Queue a buffer for transmission to a network device. The caller must
1399 * have set the device and priority and built the buffer before calling
1400 * this function. The function can be called from an interrupt.
1402 * A negative errno code is returned on a failure. A success does not
1403 * guarantee the frame will be transmitted as it may be dropped due
1404 * to congestion or traffic shaping.
1406 * -----------------------------------------------------------------------------------
1407 * I notice this method can also return errors from the queue disciplines,
1408 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1409 * be positive.
1411 * Regardless of the return value, the skb is consumed, so it is currently
1412 * difficult to retry a send to this method. (You can bump the ref count
1413 * before sending to hold a reference for retry if you are careful.)
1415 * When calling this method, interrupts MUST be enabled. This is because
1416 * the BH enable code must have IRQs enabled so that it will not deadlock.
1417 * --BLG
1420 int dev_queue_xmit(struct sk_buff *skb)
1422 struct net_device *dev = skb->dev;
1423 struct Qdisc *q;
1424 int rc = -ENOMEM;
1426 /* GSO will handle the following emulations directly. */
1427 if (netif_needs_gso(dev, skb))
1428 goto gso;
1430 if (skb_shinfo(skb)->frag_list &&
1431 !(dev->features & NETIF_F_FRAGLIST) &&
1432 __skb_linearize(skb))
1433 goto out_kfree_skb;
1435 /* Fragmented skb is linearized if device does not support SG,
1436 * or if at least one of fragments is in highmem and device
1437 * does not support DMA from it.
1439 if (skb_shinfo(skb)->nr_frags &&
1440 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1441 __skb_linearize(skb))
1442 goto out_kfree_skb;
1444 /* If packet is not checksummed and device does not support
1445 * checksumming for this protocol, complete checksumming here.
1447 if (skb->ip_summed == CHECKSUM_HW &&
1448 (!(dev->features & NETIF_F_GEN_CSUM) &&
1449 (!(dev->features & NETIF_F_IP_CSUM) ||
1450 skb->protocol != htons(ETH_P_IP))))
1451 if (skb_checksum_help(skb, 0))
1452 goto out_kfree_skb;
1454 gso:
1455 spin_lock_prefetch(&dev->queue_lock);
1457 /* Disable soft irqs for various locks below. Also
1458 * stops preemption for RCU.
1460 rcu_read_lock_bh();
1462 /* Updates of qdisc are serialized by queue_lock.
1463 * The struct Qdisc which is pointed to by qdisc is now a
1464 * rcu structure - it may be accessed without acquiring
1465 * a lock (but the structure may be stale.) The freeing of the
1466 * qdisc will be deferred until it's known that there are no
1467 * more references to it.
1469 * If the qdisc has an enqueue function, we still need to
1470 * hold the queue_lock before calling it, since queue_lock
1471 * also serializes access to the device queue.
1474 q = rcu_dereference(dev->qdisc);
1475 #ifdef CONFIG_NET_CLS_ACT
1476 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1477 #endif
1478 if (q->enqueue) {
1479 /* Grab device queue */
1480 spin_lock(&dev->queue_lock);
1482 rc = q->enqueue(skb, q);
1484 qdisc_run(dev);
1486 spin_unlock(&dev->queue_lock);
1487 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1488 goto out;
1491 /* The device has no queue. Common case for software devices:
1492 loopback, all the sorts of tunnels...
1494 Really, it is unlikely that netif_tx_lock protection is necessary
1495 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1496 counters.)
1497 However, it is possible, that they rely on protection
1498 made by us here.
1500 Check this and shot the lock. It is not prone from deadlocks.
1501 Either shot noqueue qdisc, it is even simpler 8)
1503 if (dev->flags & IFF_UP) {
1504 int cpu = smp_processor_id(); /* ok because BHs are off */
1506 if (dev->xmit_lock_owner != cpu) {
1508 HARD_TX_LOCK(dev, cpu);
1510 if (!netif_queue_stopped(dev)) {
1511 rc = 0;
1512 if (!dev_hard_start_xmit(skb, dev)) {
1513 HARD_TX_UNLOCK(dev);
1514 goto out;
1517 HARD_TX_UNLOCK(dev);
1518 if (net_ratelimit())
1519 printk(KERN_CRIT "Virtual device %s asks to "
1520 "queue packet!\n", dev->name);
1521 } else {
1522 /* Recursion is detected! It is possible,
1523 * unfortunately */
1524 if (net_ratelimit())
1525 printk(KERN_CRIT "Dead loop on virtual device "
1526 "%s, fix it urgently!\n", dev->name);
1530 rc = -ENETDOWN;
1531 rcu_read_unlock_bh();
1533 out_kfree_skb:
1534 kfree_skb(skb);
1535 return rc;
1536 out:
1537 rcu_read_unlock_bh();
1538 return rc;
1542 /*=======================================================================
1543 Receiver routines
1544 =======================================================================*/
1546 int netdev_max_backlog = 1000;
1547 int netdev_budget = 300;
1548 int weight_p = 64; /* old backlog weight */
1550 DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1554 * netif_rx - post buffer to the network code
1555 * @skb: buffer to post
1557 * This function receives a packet from a device driver and queues it for
1558 * the upper (protocol) levels to process. It always succeeds. The buffer
1559 * may be dropped during processing for congestion control or by the
1560 * protocol layers.
1562 * return values:
1563 * NET_RX_SUCCESS (no congestion)
1564 * NET_RX_CN_LOW (low congestion)
1565 * NET_RX_CN_MOD (moderate congestion)
1566 * NET_RX_CN_HIGH (high congestion)
1567 * NET_RX_DROP (packet was dropped)
1571 int netif_rx(struct sk_buff *skb)
1573 struct softnet_data *queue;
1574 unsigned long flags;
1576 /* if netpoll wants it, pretend we never saw it */
1577 if (netpoll_rx(skb))
1578 return NET_RX_DROP;
1580 if (!skb->tstamp.off_sec)
1581 net_timestamp(skb);
1584 * The code is rearranged so that the path is the most
1585 * short when CPU is congested, but is still operating.
1587 local_irq_save(flags);
1588 queue = &__get_cpu_var(softnet_data);
1590 __get_cpu_var(netdev_rx_stat).total++;
1591 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1592 if (queue->input_pkt_queue.qlen) {
1593 enqueue:
1594 dev_hold(skb->dev);
1595 __skb_queue_tail(&queue->input_pkt_queue, skb);
1596 local_irq_restore(flags);
1597 return NET_RX_SUCCESS;
1600 netif_rx_schedule(&queue->backlog_dev);
1601 goto enqueue;
1604 __get_cpu_var(netdev_rx_stat).dropped++;
1605 local_irq_restore(flags);
1607 kfree_skb(skb);
1608 return NET_RX_DROP;
1611 int netif_rx_ni(struct sk_buff *skb)
1613 int err;
1615 preempt_disable();
1616 err = netif_rx(skb);
1617 if (local_softirq_pending())
1618 do_softirq();
1619 preempt_enable();
1621 return err;
1624 EXPORT_SYMBOL(netif_rx_ni);
1626 static inline struct net_device *skb_bond(struct sk_buff *skb)
1628 struct net_device *dev = skb->dev;
1630 if (dev->master) {
1631 if (skb_bond_should_drop(skb)) {
1632 kfree_skb(skb);
1633 return NULL;
1635 skb->dev = dev->master;
1638 return dev;
1641 static void net_tx_action(struct softirq_action *h)
1643 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1645 if (sd->completion_queue) {
1646 struct sk_buff *clist;
1648 local_irq_disable();
1649 clist = sd->completion_queue;
1650 sd->completion_queue = NULL;
1651 local_irq_enable();
1653 while (clist) {
1654 struct sk_buff *skb = clist;
1655 clist = clist->next;
1657 BUG_TRAP(!atomic_read(&skb->users));
1658 __kfree_skb(skb);
1662 if (sd->output_queue) {
1663 struct net_device *head;
1665 local_irq_disable();
1666 head = sd->output_queue;
1667 sd->output_queue = NULL;
1668 local_irq_enable();
1670 while (head) {
1671 struct net_device *dev = head;
1672 head = head->next_sched;
1674 smp_mb__before_clear_bit();
1675 clear_bit(__LINK_STATE_SCHED, &dev->state);
1677 if (spin_trylock(&dev->queue_lock)) {
1678 qdisc_run(dev);
1679 spin_unlock(&dev->queue_lock);
1680 } else {
1681 netif_schedule(dev);
1687 static __inline__ int deliver_skb(struct sk_buff *skb,
1688 struct packet_type *pt_prev,
1689 struct net_device *orig_dev)
1691 atomic_inc(&skb->users);
1692 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1695 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1696 int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1697 struct net_bridge;
1698 struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1699 unsigned char *addr);
1700 void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
1702 static __inline__ int handle_bridge(struct sk_buff **pskb,
1703 struct packet_type **pt_prev, int *ret,
1704 struct net_device *orig_dev)
1706 struct net_bridge_port *port;
1708 if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1709 (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1710 return 0;
1712 if (*pt_prev) {
1713 *ret = deliver_skb(*pskb, *pt_prev, orig_dev);
1714 *pt_prev = NULL;
1717 return br_handle_frame_hook(port, pskb);
1719 #else
1720 #define handle_bridge(skb, pt_prev, ret, orig_dev) (0)
1721 #endif
1723 #ifdef CONFIG_NET_CLS_ACT
1724 /* TODO: Maybe we should just force sch_ingress to be compiled in
1725 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1726 * a compare and 2 stores extra right now if we dont have it on
1727 * but have CONFIG_NET_CLS_ACT
1728 * NOTE: This doesnt stop any functionality; if you dont have
1729 * the ingress scheduler, you just cant add policies on ingress.
1732 static int ing_filter(struct sk_buff *skb)
1734 struct Qdisc *q;
1735 struct net_device *dev = skb->dev;
1736 int result = TC_ACT_OK;
1738 if (dev->qdisc_ingress) {
1739 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1740 if (MAX_RED_LOOP < ttl++) {
1741 printk(KERN_WARNING "Redir loop detected Dropping packet (%s->%s)\n",
1742 skb->input_dev->name, skb->dev->name);
1743 return TC_ACT_SHOT;
1746 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1748 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1750 spin_lock(&dev->ingress_lock);
1751 if ((q = dev->qdisc_ingress) != NULL)
1752 result = q->enqueue(skb, q);
1753 spin_unlock(&dev->ingress_lock);
1757 return result;
1759 #endif
1761 int netif_receive_skb(struct sk_buff *skb)
1763 struct packet_type *ptype, *pt_prev;
1764 struct net_device *orig_dev;
1765 int ret = NET_RX_DROP;
1766 unsigned short type;
1768 /* if we've gotten here through NAPI, check netpoll */
1769 if (skb->dev->poll && netpoll_rx(skb))
1770 return NET_RX_DROP;
1772 if (!skb->tstamp.off_sec)
1773 net_timestamp(skb);
1775 if (!skb->input_dev)
1776 skb->input_dev = skb->dev;
1778 orig_dev = skb_bond(skb);
1780 if (!orig_dev)
1781 return NET_RX_DROP;
1783 __get_cpu_var(netdev_rx_stat).total++;
1785 skb->h.raw = skb->nh.raw = skb->data;
1786 skb->mac_len = skb->nh.raw - skb->mac.raw;
1788 pt_prev = NULL;
1790 rcu_read_lock();
1792 #ifdef CONFIG_NET_CLS_ACT
1793 if (skb->tc_verd & TC_NCLS) {
1794 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1795 goto ncls;
1797 #endif
1799 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1800 if (!ptype->dev || ptype->dev == skb->dev) {
1801 if (pt_prev)
1802 ret = deliver_skb(skb, pt_prev, orig_dev);
1803 pt_prev = ptype;
1807 #ifdef CONFIG_NET_CLS_ACT
1808 if (pt_prev) {
1809 ret = deliver_skb(skb, pt_prev, orig_dev);
1810 pt_prev = NULL; /* noone else should process this after*/
1811 } else {
1812 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1815 ret = ing_filter(skb);
1817 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1818 kfree_skb(skb);
1819 goto out;
1822 skb->tc_verd = 0;
1823 ncls:
1824 #endif
1826 handle_diverter(skb);
1828 if (handle_bridge(&skb, &pt_prev, &ret, orig_dev))
1829 goto out;
1831 type = skb->protocol;
1832 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1833 if (ptype->type == type &&
1834 (!ptype->dev || ptype->dev == skb->dev)) {
1835 if (pt_prev)
1836 ret = deliver_skb(skb, pt_prev, orig_dev);
1837 pt_prev = ptype;
1841 if (pt_prev) {
1842 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1843 } else {
1844 kfree_skb(skb);
1845 /* Jamal, now you will not able to escape explaining
1846 * me how you were going to use this. :-)
1848 ret = NET_RX_DROP;
1851 out:
1852 rcu_read_unlock();
1853 return ret;
1856 static int process_backlog(struct net_device *backlog_dev, int *budget)
1858 int work = 0;
1859 int quota = min(backlog_dev->quota, *budget);
1860 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1861 unsigned long start_time = jiffies;
1863 backlog_dev->weight = weight_p;
1864 for (;;) {
1865 struct sk_buff *skb;
1866 struct net_device *dev;
1868 local_irq_disable();
1869 skb = __skb_dequeue(&queue->input_pkt_queue);
1870 if (!skb)
1871 goto job_done;
1872 local_irq_enable();
1874 dev = skb->dev;
1876 netif_receive_skb(skb);
1878 dev_put(dev);
1880 work++;
1882 if (work >= quota || jiffies - start_time > 1)
1883 break;
1887 backlog_dev->quota -= work;
1888 *budget -= work;
1889 return -1;
1891 job_done:
1892 backlog_dev->quota -= work;
1893 *budget -= work;
1895 list_del(&backlog_dev->poll_list);
1896 smp_mb__before_clear_bit();
1897 netif_poll_enable(backlog_dev);
1899 local_irq_enable();
1900 return 0;
1903 static void net_rx_action(struct softirq_action *h)
1905 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1906 unsigned long start_time = jiffies;
1907 int budget = netdev_budget;
1908 void *have;
1910 local_irq_disable();
1912 while (!list_empty(&queue->poll_list)) {
1913 struct net_device *dev;
1915 if (budget <= 0 || jiffies - start_time > 1)
1916 goto softnet_break;
1918 local_irq_enable();
1920 dev = list_entry(queue->poll_list.next,
1921 struct net_device, poll_list);
1922 have = netpoll_poll_lock(dev);
1924 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
1925 netpoll_poll_unlock(have);
1926 local_irq_disable();
1927 list_move_tail(&dev->poll_list, &queue->poll_list);
1928 if (dev->quota < 0)
1929 dev->quota += dev->weight;
1930 else
1931 dev->quota = dev->weight;
1932 } else {
1933 netpoll_poll_unlock(have);
1934 dev_put(dev);
1935 local_irq_disable();
1938 out:
1939 #ifdef CONFIG_NET_DMA
1941 * There may not be any more sk_buffs coming right now, so push
1942 * any pending DMA copies to hardware
1944 if (net_dma_client) {
1945 struct dma_chan *chan;
1946 rcu_read_lock();
1947 list_for_each_entry_rcu(chan, &net_dma_client->channels, client_node)
1948 dma_async_memcpy_issue_pending(chan);
1949 rcu_read_unlock();
1951 #endif
1952 local_irq_enable();
1953 return;
1955 softnet_break:
1956 __get_cpu_var(netdev_rx_stat).time_squeeze++;
1957 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1958 goto out;
1961 static gifconf_func_t * gifconf_list [NPROTO];
1964 * register_gifconf - register a SIOCGIF handler
1965 * @family: Address family
1966 * @gifconf: Function handler
1968 * Register protocol dependent address dumping routines. The handler
1969 * that is passed must not be freed or reused until it has been replaced
1970 * by another handler.
1972 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1974 if (family >= NPROTO)
1975 return -EINVAL;
1976 gifconf_list[family] = gifconf;
1977 return 0;
1982 * Map an interface index to its name (SIOCGIFNAME)
1986 * We need this ioctl for efficient implementation of the
1987 * if_indextoname() function required by the IPv6 API. Without
1988 * it, we would have to search all the interfaces to find a
1989 * match. --pb
1992 static int dev_ifname(struct ifreq __user *arg)
1994 struct net_device *dev;
1995 struct ifreq ifr;
1998 * Fetch the caller's info block.
2001 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2002 return -EFAULT;
2004 read_lock(&dev_base_lock);
2005 dev = __dev_get_by_index(ifr.ifr_ifindex);
2006 if (!dev) {
2007 read_unlock(&dev_base_lock);
2008 return -ENODEV;
2011 strcpy(ifr.ifr_name, dev->name);
2012 read_unlock(&dev_base_lock);
2014 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2015 return -EFAULT;
2016 return 0;
2020 * Perform a SIOCGIFCONF call. This structure will change
2021 * size eventually, and there is nothing I can do about it.
2022 * Thus we will need a 'compatibility mode'.
2025 static int dev_ifconf(char __user *arg)
2027 struct ifconf ifc;
2028 struct net_device *dev;
2029 char __user *pos;
2030 int len;
2031 int total;
2032 int i;
2035 * Fetch the caller's info block.
2038 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2039 return -EFAULT;
2041 pos = ifc.ifc_buf;
2042 len = ifc.ifc_len;
2045 * Loop over the interfaces, and write an info block for each.
2048 total = 0;
2049 for (dev = dev_base; dev; dev = dev->next) {
2050 for (i = 0; i < NPROTO; i++) {
2051 if (gifconf_list[i]) {
2052 int done;
2053 if (!pos)
2054 done = gifconf_list[i](dev, NULL, 0);
2055 else
2056 done = gifconf_list[i](dev, pos + total,
2057 len - total);
2058 if (done < 0)
2059 return -EFAULT;
2060 total += done;
2066 * All done. Write the updated control block back to the caller.
2068 ifc.ifc_len = total;
2071 * Both BSD and Solaris return 0 here, so we do too.
2073 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2076 #ifdef CONFIG_PROC_FS
2078 * This is invoked by the /proc filesystem handler to display a device
2079 * in detail.
2081 static __inline__ struct net_device *dev_get_idx(loff_t pos)
2083 struct net_device *dev;
2084 loff_t i;
2086 for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
2088 return i == pos ? dev : NULL;
2091 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2093 read_lock(&dev_base_lock);
2094 return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
2097 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2099 ++*pos;
2100 return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
2103 void dev_seq_stop(struct seq_file *seq, void *v)
2105 read_unlock(&dev_base_lock);
2108 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2110 if (dev->get_stats) {
2111 struct net_device_stats *stats = dev->get_stats(dev);
2113 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2114 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2115 dev->name, stats->rx_bytes, stats->rx_packets,
2116 stats->rx_errors,
2117 stats->rx_dropped + stats->rx_missed_errors,
2118 stats->rx_fifo_errors,
2119 stats->rx_length_errors + stats->rx_over_errors +
2120 stats->rx_crc_errors + stats->rx_frame_errors,
2121 stats->rx_compressed, stats->multicast,
2122 stats->tx_bytes, stats->tx_packets,
2123 stats->tx_errors, stats->tx_dropped,
2124 stats->tx_fifo_errors, stats->collisions,
2125 stats->tx_carrier_errors +
2126 stats->tx_aborted_errors +
2127 stats->tx_window_errors +
2128 stats->tx_heartbeat_errors,
2129 stats->tx_compressed);
2130 } else
2131 seq_printf(seq, "%6s: No statistics available.\n", dev->name);
2135 * Called from the PROCfs module. This now uses the new arbitrary sized
2136 * /proc/net interface to create /proc/net/dev
2138 static int dev_seq_show(struct seq_file *seq, void *v)
2140 if (v == SEQ_START_TOKEN)
2141 seq_puts(seq, "Inter-| Receive "
2142 " | Transmit\n"
2143 " face |bytes packets errs drop fifo frame "
2144 "compressed multicast|bytes packets errs "
2145 "drop fifo colls carrier compressed\n");
2146 else
2147 dev_seq_printf_stats(seq, v);
2148 return 0;
2151 static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2153 struct netif_rx_stats *rc = NULL;
2155 while (*pos < NR_CPUS)
2156 if (cpu_online(*pos)) {
2157 rc = &per_cpu(netdev_rx_stat, *pos);
2158 break;
2159 } else
2160 ++*pos;
2161 return rc;
2164 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2166 return softnet_get_online(pos);
2169 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2171 ++*pos;
2172 return softnet_get_online(pos);
2175 static void softnet_seq_stop(struct seq_file *seq, void *v)
2179 static int softnet_seq_show(struct seq_file *seq, void *v)
2181 struct netif_rx_stats *s = v;
2183 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2184 s->total, s->dropped, s->time_squeeze, 0,
2185 0, 0, 0, 0, /* was fastroute */
2186 s->cpu_collision );
2187 return 0;
2190 static struct seq_operations dev_seq_ops = {
2191 .start = dev_seq_start,
2192 .next = dev_seq_next,
2193 .stop = dev_seq_stop,
2194 .show = dev_seq_show,
2197 static int dev_seq_open(struct inode *inode, struct file *file)
2199 return seq_open(file, &dev_seq_ops);
2202 static struct file_operations dev_seq_fops = {
2203 .owner = THIS_MODULE,
2204 .open = dev_seq_open,
2205 .read = seq_read,
2206 .llseek = seq_lseek,
2207 .release = seq_release,
2210 static struct seq_operations softnet_seq_ops = {
2211 .start = softnet_seq_start,
2212 .next = softnet_seq_next,
2213 .stop = softnet_seq_stop,
2214 .show = softnet_seq_show,
2217 static int softnet_seq_open(struct inode *inode, struct file *file)
2219 return seq_open(file, &softnet_seq_ops);
2222 static struct file_operations softnet_seq_fops = {
2223 .owner = THIS_MODULE,
2224 .open = softnet_seq_open,
2225 .read = seq_read,
2226 .llseek = seq_lseek,
2227 .release = seq_release,
2230 #ifdef CONFIG_WIRELESS_EXT
2231 extern int wireless_proc_init(void);
2232 #else
2233 #define wireless_proc_init() 0
2234 #endif
2236 static int __init dev_proc_init(void)
2238 int rc = -ENOMEM;
2240 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2241 goto out;
2242 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2243 goto out_dev;
2244 if (wireless_proc_init())
2245 goto out_softnet;
2246 rc = 0;
2247 out:
2248 return rc;
2249 out_softnet:
2250 proc_net_remove("softnet_stat");
2251 out_dev:
2252 proc_net_remove("dev");
2253 goto out;
2255 #else
2256 #define dev_proc_init() 0
2257 #endif /* CONFIG_PROC_FS */
2261 * netdev_set_master - set up master/slave pair
2262 * @slave: slave device
2263 * @master: new master device
2265 * Changes the master device of the slave. Pass %NULL to break the
2266 * bonding. The caller must hold the RTNL semaphore. On a failure
2267 * a negative errno code is returned. On success the reference counts
2268 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2269 * function returns zero.
2271 int netdev_set_master(struct net_device *slave, struct net_device *master)
2273 struct net_device *old = slave->master;
2275 ASSERT_RTNL();
2277 if (master) {
2278 if (old)
2279 return -EBUSY;
2280 dev_hold(master);
2283 slave->master = master;
2285 synchronize_net();
2287 if (old)
2288 dev_put(old);
2290 if (master)
2291 slave->flags |= IFF_SLAVE;
2292 else
2293 slave->flags &= ~IFF_SLAVE;
2295 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2296 return 0;
2300 * dev_set_promiscuity - update promiscuity count on a device
2301 * @dev: device
2302 * @inc: modifier
2304 * Add or remove promiscuity from a device. While the count in the device
2305 * remains above zero the interface remains promiscuous. Once it hits zero
2306 * the device reverts back to normal filtering operation. A negative inc
2307 * value is used to drop promiscuity on the device.
2309 void dev_set_promiscuity(struct net_device *dev, int inc)
2311 unsigned short old_flags = dev->flags;
2313 if ((dev->promiscuity += inc) == 0)
2314 dev->flags &= ~IFF_PROMISC;
2315 else
2316 dev->flags |= IFF_PROMISC;
2317 if (dev->flags != old_flags) {
2318 dev_mc_upload(dev);
2319 printk(KERN_INFO "device %s %s promiscuous mode\n",
2320 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2321 "left");
2322 audit_log(current->audit_context, GFP_ATOMIC,
2323 AUDIT_ANOM_PROMISCUOUS,
2324 "dev=%s prom=%d old_prom=%d auid=%u",
2325 dev->name, (dev->flags & IFF_PROMISC),
2326 (old_flags & IFF_PROMISC),
2327 audit_get_loginuid(current->audit_context));
2332 * dev_set_allmulti - update allmulti count on a device
2333 * @dev: device
2334 * @inc: modifier
2336 * Add or remove reception of all multicast frames to a device. While the
2337 * count in the device remains above zero the interface remains listening
2338 * to all interfaces. Once it hits zero the device reverts back to normal
2339 * filtering operation. A negative @inc value is used to drop the counter
2340 * when releasing a resource needing all multicasts.
2343 void dev_set_allmulti(struct net_device *dev, int inc)
2345 unsigned short old_flags = dev->flags;
2347 dev->flags |= IFF_ALLMULTI;
2348 if ((dev->allmulti += inc) == 0)
2349 dev->flags &= ~IFF_ALLMULTI;
2350 if (dev->flags ^ old_flags)
2351 dev_mc_upload(dev);
2354 unsigned dev_get_flags(const struct net_device *dev)
2356 unsigned flags;
2358 flags = (dev->flags & ~(IFF_PROMISC |
2359 IFF_ALLMULTI |
2360 IFF_RUNNING |
2361 IFF_LOWER_UP |
2362 IFF_DORMANT)) |
2363 (dev->gflags & (IFF_PROMISC |
2364 IFF_ALLMULTI));
2366 if (netif_running(dev)) {
2367 if (netif_oper_up(dev))
2368 flags |= IFF_RUNNING;
2369 if (netif_carrier_ok(dev))
2370 flags |= IFF_LOWER_UP;
2371 if (netif_dormant(dev))
2372 flags |= IFF_DORMANT;
2375 return flags;
2378 int dev_change_flags(struct net_device *dev, unsigned flags)
2380 int ret;
2381 int old_flags = dev->flags;
2384 * Set the flags on our device.
2387 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2388 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2389 IFF_AUTOMEDIA)) |
2390 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2391 IFF_ALLMULTI));
2394 * Load in the correct multicast list now the flags have changed.
2397 dev_mc_upload(dev);
2400 * Have we downed the interface. We handle IFF_UP ourselves
2401 * according to user attempts to set it, rather than blindly
2402 * setting it.
2405 ret = 0;
2406 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2407 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2409 if (!ret)
2410 dev_mc_upload(dev);
2413 if (dev->flags & IFF_UP &&
2414 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2415 IFF_VOLATILE)))
2416 raw_notifier_call_chain(&netdev_chain,
2417 NETDEV_CHANGE, dev);
2419 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2420 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2421 dev->gflags ^= IFF_PROMISC;
2422 dev_set_promiscuity(dev, inc);
2425 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2426 is important. Some (broken) drivers set IFF_PROMISC, when
2427 IFF_ALLMULTI is requested not asking us and not reporting.
2429 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2430 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2431 dev->gflags ^= IFF_ALLMULTI;
2432 dev_set_allmulti(dev, inc);
2435 if (old_flags ^ dev->flags)
2436 rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2438 return ret;
2441 int dev_set_mtu(struct net_device *dev, int new_mtu)
2443 int err;
2445 if (new_mtu == dev->mtu)
2446 return 0;
2448 /* MTU must be positive. */
2449 if (new_mtu < 0)
2450 return -EINVAL;
2452 if (!netif_device_present(dev))
2453 return -ENODEV;
2455 err = 0;
2456 if (dev->change_mtu)
2457 err = dev->change_mtu(dev, new_mtu);
2458 else
2459 dev->mtu = new_mtu;
2460 if (!err && dev->flags & IFF_UP)
2461 raw_notifier_call_chain(&netdev_chain,
2462 NETDEV_CHANGEMTU, dev);
2463 return err;
2466 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2468 int err;
2470 if (!dev->set_mac_address)
2471 return -EOPNOTSUPP;
2472 if (sa->sa_family != dev->type)
2473 return -EINVAL;
2474 if (!netif_device_present(dev))
2475 return -ENODEV;
2476 err = dev->set_mac_address(dev, sa);
2477 if (!err)
2478 raw_notifier_call_chain(&netdev_chain,
2479 NETDEV_CHANGEADDR, dev);
2480 return err;
2484 * Perform the SIOCxIFxxx calls.
2486 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2488 int err;
2489 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2491 if (!dev)
2492 return -ENODEV;
2494 switch (cmd) {
2495 case SIOCGIFFLAGS: /* Get interface flags */
2496 ifr->ifr_flags = dev_get_flags(dev);
2497 return 0;
2499 case SIOCSIFFLAGS: /* Set interface flags */
2500 return dev_change_flags(dev, ifr->ifr_flags);
2502 case SIOCGIFMETRIC: /* Get the metric on the interface
2503 (currently unused) */
2504 ifr->ifr_metric = 0;
2505 return 0;
2507 case SIOCSIFMETRIC: /* Set the metric on the interface
2508 (currently unused) */
2509 return -EOPNOTSUPP;
2511 case SIOCGIFMTU: /* Get the MTU of a device */
2512 ifr->ifr_mtu = dev->mtu;
2513 return 0;
2515 case SIOCSIFMTU: /* Set the MTU of a device */
2516 return dev_set_mtu(dev, ifr->ifr_mtu);
2518 case SIOCGIFHWADDR:
2519 if (!dev->addr_len)
2520 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2521 else
2522 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2523 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2524 ifr->ifr_hwaddr.sa_family = dev->type;
2525 return 0;
2527 case SIOCSIFHWADDR:
2528 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2530 case SIOCSIFHWBROADCAST:
2531 if (ifr->ifr_hwaddr.sa_family != dev->type)
2532 return -EINVAL;
2533 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2534 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2535 raw_notifier_call_chain(&netdev_chain,
2536 NETDEV_CHANGEADDR, dev);
2537 return 0;
2539 case SIOCGIFMAP:
2540 ifr->ifr_map.mem_start = dev->mem_start;
2541 ifr->ifr_map.mem_end = dev->mem_end;
2542 ifr->ifr_map.base_addr = dev->base_addr;
2543 ifr->ifr_map.irq = dev->irq;
2544 ifr->ifr_map.dma = dev->dma;
2545 ifr->ifr_map.port = dev->if_port;
2546 return 0;
2548 case SIOCSIFMAP:
2549 if (dev->set_config) {
2550 if (!netif_device_present(dev))
2551 return -ENODEV;
2552 return dev->set_config(dev, &ifr->ifr_map);
2554 return -EOPNOTSUPP;
2556 case SIOCADDMULTI:
2557 if (!dev->set_multicast_list ||
2558 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2559 return -EINVAL;
2560 if (!netif_device_present(dev))
2561 return -ENODEV;
2562 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2563 dev->addr_len, 1);
2565 case SIOCDELMULTI:
2566 if (!dev->set_multicast_list ||
2567 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2568 return -EINVAL;
2569 if (!netif_device_present(dev))
2570 return -ENODEV;
2571 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2572 dev->addr_len, 1);
2574 case SIOCGIFINDEX:
2575 ifr->ifr_ifindex = dev->ifindex;
2576 return 0;
2578 case SIOCGIFTXQLEN:
2579 ifr->ifr_qlen = dev->tx_queue_len;
2580 return 0;
2582 case SIOCSIFTXQLEN:
2583 if (ifr->ifr_qlen < 0)
2584 return -EINVAL;
2585 dev->tx_queue_len = ifr->ifr_qlen;
2586 return 0;
2588 case SIOCSIFNAME:
2589 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2590 return dev_change_name(dev, ifr->ifr_newname);
2593 * Unknown or private ioctl
2596 default:
2597 if ((cmd >= SIOCDEVPRIVATE &&
2598 cmd <= SIOCDEVPRIVATE + 15) ||
2599 cmd == SIOCBONDENSLAVE ||
2600 cmd == SIOCBONDRELEASE ||
2601 cmd == SIOCBONDSETHWADDR ||
2602 cmd == SIOCBONDSLAVEINFOQUERY ||
2603 cmd == SIOCBONDINFOQUERY ||
2604 cmd == SIOCBONDCHANGEACTIVE ||
2605 cmd == SIOCGMIIPHY ||
2606 cmd == SIOCGMIIREG ||
2607 cmd == SIOCSMIIREG ||
2608 cmd == SIOCBRADDIF ||
2609 cmd == SIOCBRDELIF ||
2610 cmd == SIOCWANDEV) {
2611 err = -EOPNOTSUPP;
2612 if (dev->do_ioctl) {
2613 if (netif_device_present(dev))
2614 err = dev->do_ioctl(dev, ifr,
2615 cmd);
2616 else
2617 err = -ENODEV;
2619 } else
2620 err = -EINVAL;
2623 return err;
2627 * This function handles all "interface"-type I/O control requests. The actual
2628 * 'doing' part of this is dev_ifsioc above.
2632 * dev_ioctl - network device ioctl
2633 * @cmd: command to issue
2634 * @arg: pointer to a struct ifreq in user space
2636 * Issue ioctl functions to devices. This is normally called by the
2637 * user space syscall interfaces but can sometimes be useful for
2638 * other purposes. The return value is the return from the syscall if
2639 * positive or a negative errno code on error.
2642 int dev_ioctl(unsigned int cmd, void __user *arg)
2644 struct ifreq ifr;
2645 int ret;
2646 char *colon;
2648 /* One special case: SIOCGIFCONF takes ifconf argument
2649 and requires shared lock, because it sleeps writing
2650 to user space.
2653 if (cmd == SIOCGIFCONF) {
2654 rtnl_lock();
2655 ret = dev_ifconf((char __user *) arg);
2656 rtnl_unlock();
2657 return ret;
2659 if (cmd == SIOCGIFNAME)
2660 return dev_ifname((struct ifreq __user *)arg);
2662 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2663 return -EFAULT;
2665 ifr.ifr_name[IFNAMSIZ-1] = 0;
2667 colon = strchr(ifr.ifr_name, ':');
2668 if (colon)
2669 *colon = 0;
2672 * See which interface the caller is talking about.
2675 switch (cmd) {
2677 * These ioctl calls:
2678 * - can be done by all.
2679 * - atomic and do not require locking.
2680 * - return a value
2682 case SIOCGIFFLAGS:
2683 case SIOCGIFMETRIC:
2684 case SIOCGIFMTU:
2685 case SIOCGIFHWADDR:
2686 case SIOCGIFSLAVE:
2687 case SIOCGIFMAP:
2688 case SIOCGIFINDEX:
2689 case SIOCGIFTXQLEN:
2690 dev_load(ifr.ifr_name);
2691 read_lock(&dev_base_lock);
2692 ret = dev_ifsioc(&ifr, cmd);
2693 read_unlock(&dev_base_lock);
2694 if (!ret) {
2695 if (colon)
2696 *colon = ':';
2697 if (copy_to_user(arg, &ifr,
2698 sizeof(struct ifreq)))
2699 ret = -EFAULT;
2701 return ret;
2703 case SIOCETHTOOL:
2704 dev_load(ifr.ifr_name);
2705 rtnl_lock();
2706 ret = dev_ethtool(&ifr);
2707 rtnl_unlock();
2708 if (!ret) {
2709 if (colon)
2710 *colon = ':';
2711 if (copy_to_user(arg, &ifr,
2712 sizeof(struct ifreq)))
2713 ret = -EFAULT;
2715 return ret;
2718 * These ioctl calls:
2719 * - require superuser power.
2720 * - require strict serialization.
2721 * - return a value
2723 case SIOCGMIIPHY:
2724 case SIOCGMIIREG:
2725 case SIOCSIFNAME:
2726 if (!capable(CAP_NET_ADMIN))
2727 return -EPERM;
2728 dev_load(ifr.ifr_name);
2729 rtnl_lock();
2730 ret = dev_ifsioc(&ifr, cmd);
2731 rtnl_unlock();
2732 if (!ret) {
2733 if (colon)
2734 *colon = ':';
2735 if (copy_to_user(arg, &ifr,
2736 sizeof(struct ifreq)))
2737 ret = -EFAULT;
2739 return ret;
2742 * These ioctl calls:
2743 * - require superuser power.
2744 * - require strict serialization.
2745 * - do not return a value
2747 case SIOCSIFFLAGS:
2748 case SIOCSIFMETRIC:
2749 case SIOCSIFMTU:
2750 case SIOCSIFMAP:
2751 case SIOCSIFHWADDR:
2752 case SIOCSIFSLAVE:
2753 case SIOCADDMULTI:
2754 case SIOCDELMULTI:
2755 case SIOCSIFHWBROADCAST:
2756 case SIOCSIFTXQLEN:
2757 case SIOCSMIIREG:
2758 case SIOCBONDENSLAVE:
2759 case SIOCBONDRELEASE:
2760 case SIOCBONDSETHWADDR:
2761 case SIOCBONDCHANGEACTIVE:
2762 case SIOCBRADDIF:
2763 case SIOCBRDELIF:
2764 if (!capable(CAP_NET_ADMIN))
2765 return -EPERM;
2766 /* fall through */
2767 case SIOCBONDSLAVEINFOQUERY:
2768 case SIOCBONDINFOQUERY:
2769 dev_load(ifr.ifr_name);
2770 rtnl_lock();
2771 ret = dev_ifsioc(&ifr, cmd);
2772 rtnl_unlock();
2773 return ret;
2775 case SIOCGIFMEM:
2776 /* Get the per device memory space. We can add this but
2777 * currently do not support it */
2778 case SIOCSIFMEM:
2779 /* Set the per device memory buffer space.
2780 * Not applicable in our case */
2781 case SIOCSIFLINK:
2782 return -EINVAL;
2785 * Unknown or private ioctl.
2787 default:
2788 if (cmd == SIOCWANDEV ||
2789 (cmd >= SIOCDEVPRIVATE &&
2790 cmd <= SIOCDEVPRIVATE + 15)) {
2791 dev_load(ifr.ifr_name);
2792 rtnl_lock();
2793 ret = dev_ifsioc(&ifr, cmd);
2794 rtnl_unlock();
2795 if (!ret && copy_to_user(arg, &ifr,
2796 sizeof(struct ifreq)))
2797 ret = -EFAULT;
2798 return ret;
2800 #ifdef CONFIG_WIRELESS_EXT
2801 /* Take care of Wireless Extensions */
2802 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2803 /* If command is `set a parameter', or
2804 * `get the encoding parameters', check if
2805 * the user has the right to do it */
2806 if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE
2807 || cmd == SIOCGIWENCODEEXT) {
2808 if (!capable(CAP_NET_ADMIN))
2809 return -EPERM;
2811 dev_load(ifr.ifr_name);
2812 rtnl_lock();
2813 /* Follow me in net/core/wireless.c */
2814 ret = wireless_process_ioctl(&ifr, cmd);
2815 rtnl_unlock();
2816 if (IW_IS_GET(cmd) &&
2817 copy_to_user(arg, &ifr,
2818 sizeof(struct ifreq)))
2819 ret = -EFAULT;
2820 return ret;
2822 #endif /* CONFIG_WIRELESS_EXT */
2823 return -EINVAL;
2829 * dev_new_index - allocate an ifindex
2831 * Returns a suitable unique value for a new device interface
2832 * number. The caller must hold the rtnl semaphore or the
2833 * dev_base_lock to be sure it remains unique.
2835 static int dev_new_index(void)
2837 static int ifindex;
2838 for (;;) {
2839 if (++ifindex <= 0)
2840 ifindex = 1;
2841 if (!__dev_get_by_index(ifindex))
2842 return ifindex;
2846 static int dev_boot_phase = 1;
2848 /* Delayed registration/unregisteration */
2849 static DEFINE_SPINLOCK(net_todo_list_lock);
2850 static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
2852 static inline void net_set_todo(struct net_device *dev)
2854 spin_lock(&net_todo_list_lock);
2855 list_add_tail(&dev->todo_list, &net_todo_list);
2856 spin_unlock(&net_todo_list_lock);
2860 * register_netdevice - 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 * Callers must hold the rtnl semaphore. You may want
2869 * register_netdev() instead of this.
2871 * BUGS:
2872 * The locking appears insufficient to guarantee two parallel registers
2873 * will not get the same name.
2876 int register_netdevice(struct net_device *dev)
2878 struct hlist_head *head;
2879 struct hlist_node *p;
2880 int ret;
2882 BUG_ON(dev_boot_phase);
2883 ASSERT_RTNL();
2885 might_sleep();
2887 /* When net_device's are persistent, this will be fatal. */
2888 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
2890 spin_lock_init(&dev->queue_lock);
2891 spin_lock_init(&dev->_xmit_lock);
2892 dev->xmit_lock_owner = -1;
2893 #ifdef CONFIG_NET_CLS_ACT
2894 spin_lock_init(&dev->ingress_lock);
2895 #endif
2897 ret = alloc_divert_blk(dev);
2898 if (ret)
2899 goto out;
2901 dev->iflink = -1;
2903 /* Init, if this function is available */
2904 if (dev->init) {
2905 ret = dev->init(dev);
2906 if (ret) {
2907 if (ret > 0)
2908 ret = -EIO;
2909 goto out_err;
2913 if (!dev_valid_name(dev->name)) {
2914 ret = -EINVAL;
2915 goto out_err;
2918 dev->ifindex = dev_new_index();
2919 if (dev->iflink == -1)
2920 dev->iflink = dev->ifindex;
2922 /* Check for existence of name */
2923 head = dev_name_hash(dev->name);
2924 hlist_for_each(p, head) {
2925 struct net_device *d
2926 = hlist_entry(p, struct net_device, name_hlist);
2927 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
2928 ret = -EEXIST;
2929 goto out_err;
2933 /* Fix illegal SG+CSUM combinations. */
2934 if ((dev->features & NETIF_F_SG) &&
2935 !(dev->features & NETIF_F_ALL_CSUM)) {
2936 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
2937 dev->name);
2938 dev->features &= ~NETIF_F_SG;
2941 /* TSO requires that SG is present as well. */
2942 if ((dev->features & NETIF_F_TSO) &&
2943 !(dev->features & NETIF_F_SG)) {
2944 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
2945 dev->name);
2946 dev->features &= ~NETIF_F_TSO;
2948 if (dev->features & NETIF_F_UFO) {
2949 if (!(dev->features & NETIF_F_HW_CSUM)) {
2950 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2951 "NETIF_F_HW_CSUM feature.\n",
2952 dev->name);
2953 dev->features &= ~NETIF_F_UFO;
2955 if (!(dev->features & NETIF_F_SG)) {
2956 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2957 "NETIF_F_SG feature.\n",
2958 dev->name);
2959 dev->features &= ~NETIF_F_UFO;
2964 * nil rebuild_header routine,
2965 * that should be never called and used as just bug trap.
2968 if (!dev->rebuild_header)
2969 dev->rebuild_header = default_rebuild_header;
2971 ret = netdev_register_sysfs(dev);
2972 if (ret)
2973 goto out_err;
2974 dev->reg_state = NETREG_REGISTERED;
2977 * Default initial state at registry is that the
2978 * device is present.
2981 set_bit(__LINK_STATE_PRESENT, &dev->state);
2983 dev->next = NULL;
2984 dev_init_scheduler(dev);
2985 write_lock_bh(&dev_base_lock);
2986 *dev_tail = dev;
2987 dev_tail = &dev->next;
2988 hlist_add_head(&dev->name_hlist, head);
2989 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
2990 dev_hold(dev);
2991 write_unlock_bh(&dev_base_lock);
2993 /* Notify protocols, that a new device appeared. */
2994 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
2996 ret = 0;
2998 out:
2999 return ret;
3000 out_err:
3001 free_divert_blk(dev);
3002 goto out;
3006 * register_netdev - register a network device
3007 * @dev: device to register
3009 * Take a completed network device structure and add it to the kernel
3010 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3011 * chain. 0 is returned on success. A negative errno code is returned
3012 * on a failure to set up the device, or if the name is a duplicate.
3014 * This is a wrapper around register_netdev that takes the rtnl semaphore
3015 * and expands the device name if you passed a format string to
3016 * alloc_netdev.
3018 int register_netdev(struct net_device *dev)
3020 int err;
3022 rtnl_lock();
3025 * If the name is a format string the caller wants us to do a
3026 * name allocation.
3028 if (strchr(dev->name, '%')) {
3029 err = dev_alloc_name(dev, dev->name);
3030 if (err < 0)
3031 goto out;
3035 * Back compatibility hook. Kill this one in 2.5
3037 if (dev->name[0] == 0 || dev->name[0] == ' ') {
3038 err = dev_alloc_name(dev, "eth%d");
3039 if (err < 0)
3040 goto out;
3043 err = register_netdevice(dev);
3044 out:
3045 rtnl_unlock();
3046 return err;
3048 EXPORT_SYMBOL(register_netdev);
3051 * netdev_wait_allrefs - wait until all references are gone.
3053 * This is called when unregistering network devices.
3055 * Any protocol or device that holds a reference should register
3056 * for netdevice notification, and cleanup and put back the
3057 * reference if they receive an UNREGISTER event.
3058 * We can get stuck here if buggy protocols don't correctly
3059 * call dev_put.
3061 static void netdev_wait_allrefs(struct net_device *dev)
3063 unsigned long rebroadcast_time, warning_time;
3065 rebroadcast_time = warning_time = jiffies;
3066 while (atomic_read(&dev->refcnt) != 0) {
3067 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
3068 rtnl_lock();
3070 /* Rebroadcast unregister notification */
3071 raw_notifier_call_chain(&netdev_chain,
3072 NETDEV_UNREGISTER, dev);
3074 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3075 &dev->state)) {
3076 /* We must not have linkwatch events
3077 * pending on unregister. If this
3078 * happens, we simply run the queue
3079 * unscheduled, resulting in a noop
3080 * for this device.
3082 linkwatch_run_queue();
3085 __rtnl_unlock();
3087 rebroadcast_time = jiffies;
3090 msleep(250);
3092 if (time_after(jiffies, warning_time + 10 * HZ)) {
3093 printk(KERN_EMERG "unregister_netdevice: "
3094 "waiting for %s to become free. Usage "
3095 "count = %d\n",
3096 dev->name, atomic_read(&dev->refcnt));
3097 warning_time = jiffies;
3102 /* The sequence is:
3104 * rtnl_lock();
3105 * ...
3106 * register_netdevice(x1);
3107 * register_netdevice(x2);
3108 * ...
3109 * unregister_netdevice(y1);
3110 * unregister_netdevice(y2);
3111 * ...
3112 * rtnl_unlock();
3113 * free_netdev(y1);
3114 * free_netdev(y2);
3116 * We are invoked by rtnl_unlock() after it drops the semaphore.
3117 * This allows us to deal with problems:
3118 * 1) We can delete sysfs objects which invoke hotplug
3119 * without deadlocking with linkwatch via keventd.
3120 * 2) Since we run with the RTNL semaphore not held, we can sleep
3121 * safely in order to wait for the netdev refcnt to drop to zero.
3123 static DEFINE_MUTEX(net_todo_run_mutex);
3124 void netdev_run_todo(void)
3126 struct list_head list;
3128 /* Need to guard against multiple cpu's getting out of order. */
3129 mutex_lock(&net_todo_run_mutex);
3131 /* Not safe to do outside the semaphore. We must not return
3132 * until all unregister events invoked by the local processor
3133 * have been completed (either by this todo run, or one on
3134 * another cpu).
3136 if (list_empty(&net_todo_list))
3137 goto out;
3139 /* Snapshot list, allow later requests */
3140 spin_lock(&net_todo_list_lock);
3141 list_replace_init(&net_todo_list, &list);
3142 spin_unlock(&net_todo_list_lock);
3144 while (!list_empty(&list)) {
3145 struct net_device *dev
3146 = list_entry(list.next, struct net_device, todo_list);
3147 list_del(&dev->todo_list);
3149 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
3150 printk(KERN_ERR "network todo '%s' but state %d\n",
3151 dev->name, dev->reg_state);
3152 dump_stack();
3153 continue;
3156 netdev_unregister_sysfs(dev);
3157 dev->reg_state = NETREG_UNREGISTERED;
3159 netdev_wait_allrefs(dev);
3161 /* paranoia */
3162 BUG_ON(atomic_read(&dev->refcnt));
3163 BUG_TRAP(!dev->ip_ptr);
3164 BUG_TRAP(!dev->ip6_ptr);
3165 BUG_TRAP(!dev->dn_ptr);
3167 /* It must be the very last action,
3168 * after this 'dev' may point to freed up memory.
3170 if (dev->destructor)
3171 dev->destructor(dev);
3174 out:
3175 mutex_unlock(&net_todo_run_mutex);
3179 * alloc_netdev - allocate network device
3180 * @sizeof_priv: size of private data to allocate space for
3181 * @name: device name format string
3182 * @setup: callback to initialize device
3184 * Allocates a struct net_device with private data area for driver use
3185 * and performs basic initialization.
3187 struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3188 void (*setup)(struct net_device *))
3190 void *p;
3191 struct net_device *dev;
3192 int alloc_size;
3194 /* ensure 32-byte alignment of both the device and private area */
3195 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3196 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3198 p = kzalloc(alloc_size, GFP_KERNEL);
3199 if (!p) {
3200 printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
3201 return NULL;
3204 dev = (struct net_device *)
3205 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3206 dev->padded = (char *)dev - (char *)p;
3208 if (sizeof_priv)
3209 dev->priv = netdev_priv(dev);
3211 setup(dev);
3212 strcpy(dev->name, name);
3213 return dev;
3215 EXPORT_SYMBOL(alloc_netdev);
3218 * free_netdev - free network device
3219 * @dev: device
3221 * This function does the last stage of destroying an allocated device
3222 * interface. The reference to the device object is released.
3223 * If this is the last reference then it will be freed.
3225 void free_netdev(struct net_device *dev)
3227 #ifdef CONFIG_SYSFS
3228 /* Compatibility with error handling in drivers */
3229 if (dev->reg_state == NETREG_UNINITIALIZED) {
3230 kfree((char *)dev - dev->padded);
3231 return;
3234 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3235 dev->reg_state = NETREG_RELEASED;
3237 /* will free via class release */
3238 class_device_put(&dev->class_dev);
3239 #else
3240 kfree((char *)dev - dev->padded);
3241 #endif
3244 /* Synchronize with packet receive processing. */
3245 void synchronize_net(void)
3247 might_sleep();
3248 synchronize_rcu();
3252 * unregister_netdevice - remove device from the kernel
3253 * @dev: device
3255 * This function shuts down a device interface and removes it
3256 * from the kernel tables. On success 0 is returned, on a failure
3257 * a negative errno code is returned.
3259 * Callers must hold the rtnl semaphore. You may want
3260 * unregister_netdev() instead of this.
3263 int unregister_netdevice(struct net_device *dev)
3265 struct net_device *d, **dp;
3267 BUG_ON(dev_boot_phase);
3268 ASSERT_RTNL();
3270 /* Some devices call without registering for initialization unwind. */
3271 if (dev->reg_state == NETREG_UNINITIALIZED) {
3272 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3273 "was registered\n", dev->name, dev);
3274 return -ENODEV;
3277 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3279 /* If device is running, close it first. */
3280 if (dev->flags & IFF_UP)
3281 dev_close(dev);
3283 /* And unlink it from device chain. */
3284 for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3285 if (d == dev) {
3286 write_lock_bh(&dev_base_lock);
3287 hlist_del(&dev->name_hlist);
3288 hlist_del(&dev->index_hlist);
3289 if (dev_tail == &dev->next)
3290 dev_tail = dp;
3291 *dp = d->next;
3292 write_unlock_bh(&dev_base_lock);
3293 break;
3296 if (!d) {
3297 printk(KERN_ERR "unregister net_device: '%s' not found\n",
3298 dev->name);
3299 return -ENODEV;
3302 dev->reg_state = NETREG_UNREGISTERING;
3304 synchronize_net();
3306 /* Shutdown queueing discipline. */
3307 dev_shutdown(dev);
3310 /* Notify protocols, that we are about to destroy
3311 this device. They should clean all the things.
3313 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3316 * Flush the multicast chain
3318 dev_mc_discard(dev);
3320 if (dev->uninit)
3321 dev->uninit(dev);
3323 /* Notifier chain MUST detach us from master device. */
3324 BUG_TRAP(!dev->master);
3326 free_divert_blk(dev);
3328 /* Finish processing unregister after unlock */
3329 net_set_todo(dev);
3331 synchronize_net();
3333 dev_put(dev);
3334 return 0;
3338 * unregister_netdev - remove device from the kernel
3339 * @dev: device
3341 * This function shuts down a device interface and removes it
3342 * from the kernel tables. On success 0 is returned, on a failure
3343 * a negative errno code is returned.
3345 * This is just a wrapper for unregister_netdevice that takes
3346 * the rtnl semaphore. In general you want to use this and not
3347 * unregister_netdevice.
3349 void unregister_netdev(struct net_device *dev)
3351 rtnl_lock();
3352 unregister_netdevice(dev);
3353 rtnl_unlock();
3356 EXPORT_SYMBOL(unregister_netdev);
3358 #ifdef CONFIG_HOTPLUG_CPU
3359 static int dev_cpu_callback(struct notifier_block *nfb,
3360 unsigned long action,
3361 void *ocpu)
3363 struct sk_buff **list_skb;
3364 struct net_device **list_net;
3365 struct sk_buff *skb;
3366 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3367 struct softnet_data *sd, *oldsd;
3369 if (action != CPU_DEAD)
3370 return NOTIFY_OK;
3372 local_irq_disable();
3373 cpu = smp_processor_id();
3374 sd = &per_cpu(softnet_data, cpu);
3375 oldsd = &per_cpu(softnet_data, oldcpu);
3377 /* Find end of our completion_queue. */
3378 list_skb = &sd->completion_queue;
3379 while (*list_skb)
3380 list_skb = &(*list_skb)->next;
3381 /* Append completion queue from offline CPU. */
3382 *list_skb = oldsd->completion_queue;
3383 oldsd->completion_queue = NULL;
3385 /* Find end of our output_queue. */
3386 list_net = &sd->output_queue;
3387 while (*list_net)
3388 list_net = &(*list_net)->next_sched;
3389 /* Append output queue from offline CPU. */
3390 *list_net = oldsd->output_queue;
3391 oldsd->output_queue = NULL;
3393 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3394 local_irq_enable();
3396 /* Process offline CPU's input_pkt_queue */
3397 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3398 netif_rx(skb);
3400 return NOTIFY_OK;
3402 #endif /* CONFIG_HOTPLUG_CPU */
3404 #ifdef CONFIG_NET_DMA
3406 * net_dma_rebalance -
3407 * This is called when the number of channels allocated to the net_dma_client
3408 * changes. The net_dma_client tries to have one DMA channel per CPU.
3410 static void net_dma_rebalance(void)
3412 unsigned int cpu, i, n;
3413 struct dma_chan *chan;
3415 if (net_dma_count == 0) {
3416 for_each_online_cpu(cpu)
3417 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
3418 return;
3421 i = 0;
3422 cpu = first_cpu(cpu_online_map);
3424 rcu_read_lock();
3425 list_for_each_entry(chan, &net_dma_client->channels, client_node) {
3426 n = ((num_online_cpus() / net_dma_count)
3427 + (i < (num_online_cpus() % net_dma_count) ? 1 : 0));
3429 while(n) {
3430 per_cpu(softnet_data, cpu).net_dma = chan;
3431 cpu = next_cpu(cpu, cpu_online_map);
3432 n--;
3434 i++;
3436 rcu_read_unlock();
3440 * netdev_dma_event - event callback for the net_dma_client
3441 * @client: should always be net_dma_client
3442 * @chan: DMA channel for the event
3443 * @event: event type
3445 static void netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3446 enum dma_event event)
3448 spin_lock(&net_dma_event_lock);
3449 switch (event) {
3450 case DMA_RESOURCE_ADDED:
3451 net_dma_count++;
3452 net_dma_rebalance();
3453 break;
3454 case DMA_RESOURCE_REMOVED:
3455 net_dma_count--;
3456 net_dma_rebalance();
3457 break;
3458 default:
3459 break;
3461 spin_unlock(&net_dma_event_lock);
3465 * netdev_dma_regiser - register the networking subsystem as a DMA client
3467 static int __init netdev_dma_register(void)
3469 spin_lock_init(&net_dma_event_lock);
3470 net_dma_client = dma_async_client_register(netdev_dma_event);
3471 if (net_dma_client == NULL)
3472 return -ENOMEM;
3474 dma_async_client_chan_request(net_dma_client, num_online_cpus());
3475 return 0;
3478 #else
3479 static int __init netdev_dma_register(void) { return -ENODEV; }
3480 #endif /* CONFIG_NET_DMA */
3483 * Initialize the DEV module. At boot time this walks the device list and
3484 * unhooks any devices that fail to initialise (normally hardware not
3485 * present) and leaves us with a valid list of present and active devices.
3490 * This is called single threaded during boot, so no need
3491 * to take the rtnl semaphore.
3493 static int __init net_dev_init(void)
3495 int i, rc = -ENOMEM;
3497 BUG_ON(!dev_boot_phase);
3499 net_random_init();
3501 if (dev_proc_init())
3502 goto out;
3504 if (netdev_sysfs_init())
3505 goto out;
3507 INIT_LIST_HEAD(&ptype_all);
3508 for (i = 0; i < 16; i++)
3509 INIT_LIST_HEAD(&ptype_base[i]);
3511 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3512 INIT_HLIST_HEAD(&dev_name_head[i]);
3514 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3515 INIT_HLIST_HEAD(&dev_index_head[i]);
3518 * Initialise the packet receive queues.
3521 for_each_possible_cpu(i) {
3522 struct softnet_data *queue;
3524 queue = &per_cpu(softnet_data, i);
3525 skb_queue_head_init(&queue->input_pkt_queue);
3526 queue->completion_queue = NULL;
3527 INIT_LIST_HEAD(&queue->poll_list);
3528 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3529 queue->backlog_dev.weight = weight_p;
3530 queue->backlog_dev.poll = process_backlog;
3531 atomic_set(&queue->backlog_dev.refcnt, 1);
3534 netdev_dma_register();
3536 dev_boot_phase = 0;
3538 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3539 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3541 hotcpu_notifier(dev_cpu_callback, 0);
3542 dst_init();
3543 dev_mcast_init();
3544 rc = 0;
3545 out:
3546 return rc;
3549 subsys_initcall(net_dev_init);
3551 EXPORT_SYMBOL(__dev_get_by_index);
3552 EXPORT_SYMBOL(__dev_get_by_name);
3553 EXPORT_SYMBOL(__dev_remove_pack);
3554 EXPORT_SYMBOL(dev_valid_name);
3555 EXPORT_SYMBOL(dev_add_pack);
3556 EXPORT_SYMBOL(dev_alloc_name);
3557 EXPORT_SYMBOL(dev_close);
3558 EXPORT_SYMBOL(dev_get_by_flags);
3559 EXPORT_SYMBOL(dev_get_by_index);
3560 EXPORT_SYMBOL(dev_get_by_name);
3561 EXPORT_SYMBOL(dev_open);
3562 EXPORT_SYMBOL(dev_queue_xmit);
3563 EXPORT_SYMBOL(dev_remove_pack);
3564 EXPORT_SYMBOL(dev_set_allmulti);
3565 EXPORT_SYMBOL(dev_set_promiscuity);
3566 EXPORT_SYMBOL(dev_change_flags);
3567 EXPORT_SYMBOL(dev_set_mtu);
3568 EXPORT_SYMBOL(dev_set_mac_address);
3569 EXPORT_SYMBOL(free_netdev);
3570 EXPORT_SYMBOL(netdev_boot_setup_check);
3571 EXPORT_SYMBOL(netdev_set_master);
3572 EXPORT_SYMBOL(netdev_state_change);
3573 EXPORT_SYMBOL(netif_receive_skb);
3574 EXPORT_SYMBOL(netif_rx);
3575 EXPORT_SYMBOL(register_gifconf);
3576 EXPORT_SYMBOL(register_netdevice);
3577 EXPORT_SYMBOL(register_netdevice_notifier);
3578 EXPORT_SYMBOL(skb_checksum_help);
3579 EXPORT_SYMBOL(synchronize_net);
3580 EXPORT_SYMBOL(unregister_netdevice);
3581 EXPORT_SYMBOL(unregister_netdevice_notifier);
3582 EXPORT_SYMBOL(net_enable_timestamp);
3583 EXPORT_SYMBOL(net_disable_timestamp);
3584 EXPORT_SYMBOL(dev_get_flags);
3586 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3587 EXPORT_SYMBOL(br_handle_frame_hook);
3588 EXPORT_SYMBOL(br_fdb_get_hook);
3589 EXPORT_SYMBOL(br_fdb_put_hook);
3590 #endif
3592 #ifdef CONFIG_KMOD
3593 EXPORT_SYMBOL(dev_load);
3594 #endif
3596 EXPORT_PER_CPU_SYMBOL(softnet_data);