async_tx: add the async_tx api
[wrt350n-kernel.git] / net / core / dev.c
blob835202fb34c47d9b07b23e2afea8ae7848f320e7
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 <net/dst.h>
102 #include <net/pkt_sched.h>
103 #include <net/checksum.h>
104 #include <linux/highmem.h>
105 #include <linux/init.h>
106 #include <linux/kmod.h>
107 #include <linux/module.h>
108 #include <linux/kallsyms.h>
109 #include <linux/netpoll.h>
110 #include <linux/rcupdate.h>
111 #include <linux/delay.h>
112 #include <net/wext.h>
113 #include <net/iw_handler.h>
114 #include <asm/current.h>
115 #include <linux/audit.h>
116 #include <linux/dmaengine.h>
117 #include <linux/err.h>
118 #include <linux/ctype.h>
119 #include <linux/if_arp.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] __read_mostly; /* 16 way hashed list */
151 static struct list_head ptype_all __read_mostly; /* Taps */
153 #ifdef CONFIG_NET_DMA
154 struct net_dma {
155 struct dma_client client;
156 spinlock_t lock;
157 cpumask_t channel_mask;
158 struct dma_chan *channels[NR_CPUS];
161 static enum dma_state_client
162 netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
163 enum dma_state state);
165 static struct net_dma net_dma = {
166 .client = {
167 .event_callback = netdev_dma_event,
170 #endif
173 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
174 * semaphore.
176 * Pure readers hold dev_base_lock for reading.
178 * Writers must hold the rtnl semaphore while they loop through the
179 * dev_base_head list, and hold dev_base_lock for writing when they do the
180 * actual updates. This allows pure readers to access the list even
181 * while a writer is preparing to update it.
183 * To put it another way, dev_base_lock is held for writing only to
184 * protect against pure readers; the rtnl semaphore provides the
185 * protection against other writers.
187 * See, for example usages, register_netdevice() and
188 * unregister_netdevice(), which must be called with the rtnl
189 * semaphore held.
191 LIST_HEAD(dev_base_head);
192 DEFINE_RWLOCK(dev_base_lock);
194 EXPORT_SYMBOL(dev_base_head);
195 EXPORT_SYMBOL(dev_base_lock);
197 #define NETDEV_HASHBITS 8
198 static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
199 static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
201 static inline struct hlist_head *dev_name_hash(const char *name)
203 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
204 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
207 static inline struct hlist_head *dev_index_hash(int ifindex)
209 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
213 * Our notifier list
216 static RAW_NOTIFIER_HEAD(netdev_chain);
219 * Device drivers call our routines to queue packets here. We empty the
220 * queue in the local softnet handler.
222 DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL };
224 #ifdef CONFIG_SYSFS
225 extern int netdev_sysfs_init(void);
226 extern int netdev_register_sysfs(struct net_device *);
227 extern void netdev_unregister_sysfs(struct net_device *);
228 #else
229 #define netdev_sysfs_init() (0)
230 #define netdev_register_sysfs(dev) (0)
231 #define netdev_unregister_sysfs(dev) do { } while(0)
232 #endif
234 #ifdef CONFIG_DEBUG_LOCK_ALLOC
236 * register_netdevice() inits dev->_xmit_lock and sets lockdep class
237 * according to dev->type
239 static const unsigned short netdev_lock_type[] =
240 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
241 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
242 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
243 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
244 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
245 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
246 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
247 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
248 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
249 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
250 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
251 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
252 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
253 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
254 ARPHRD_NONE};
256 static const char *netdev_lock_name[] =
257 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
258 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
259 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
260 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
261 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
262 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
263 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
264 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
265 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
266 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
267 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
268 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
269 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
270 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
271 "_xmit_NONE"};
273 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
275 static inline unsigned short netdev_lock_pos(unsigned short dev_type)
277 int i;
279 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
280 if (netdev_lock_type[i] == dev_type)
281 return i;
282 /* the last key is used by default */
283 return ARRAY_SIZE(netdev_lock_type) - 1;
286 static inline void netdev_set_lockdep_class(spinlock_t *lock,
287 unsigned short dev_type)
289 int i;
291 i = netdev_lock_pos(dev_type);
292 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
293 netdev_lock_name[i]);
295 #else
296 static inline void netdev_set_lockdep_class(spinlock_t *lock,
297 unsigned short dev_type)
300 #endif
302 /*******************************************************************************
304 Protocol management and registration routines
306 *******************************************************************************/
309 * Add a protocol ID to the list. Now that the input handler is
310 * smarter we can dispense with all the messy stuff that used to be
311 * here.
313 * BEWARE!!! Protocol handlers, mangling input packets,
314 * MUST BE last in hash buckets and checking protocol handlers
315 * MUST start from promiscuous ptype_all chain in net_bh.
316 * It is true now, do not change it.
317 * Explanation follows: if protocol handler, mangling packet, will
318 * be the first on list, it is not able to sense, that packet
319 * is cloned and should be copied-on-write, so that it will
320 * change it and subsequent readers will get broken packet.
321 * --ANK (980803)
325 * dev_add_pack - add packet handler
326 * @pt: packet type declaration
328 * Add a protocol handler to the networking stack. The passed &packet_type
329 * is linked into kernel lists and may not be freed until it has been
330 * removed from the kernel lists.
332 * This call does not sleep therefore it can not
333 * guarantee all CPU's that are in middle of receiving packets
334 * will see the new packet type (until the next received packet).
337 void dev_add_pack(struct packet_type *pt)
339 int hash;
341 spin_lock_bh(&ptype_lock);
342 if (pt->type == htons(ETH_P_ALL))
343 list_add_rcu(&pt->list, &ptype_all);
344 else {
345 hash = ntohs(pt->type) & 15;
346 list_add_rcu(&pt->list, &ptype_base[hash]);
348 spin_unlock_bh(&ptype_lock);
352 * __dev_remove_pack - remove packet handler
353 * @pt: packet type declaration
355 * Remove a protocol handler that was previously added to the kernel
356 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
357 * from the kernel lists and can be freed or reused once this function
358 * returns.
360 * The packet type might still be in use by receivers
361 * and must not be freed until after all the CPU's have gone
362 * through a quiescent state.
364 void __dev_remove_pack(struct packet_type *pt)
366 struct list_head *head;
367 struct packet_type *pt1;
369 spin_lock_bh(&ptype_lock);
371 if (pt->type == htons(ETH_P_ALL))
372 head = &ptype_all;
373 else
374 head = &ptype_base[ntohs(pt->type) & 15];
376 list_for_each_entry(pt1, head, list) {
377 if (pt == pt1) {
378 list_del_rcu(&pt->list);
379 goto out;
383 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
384 out:
385 spin_unlock_bh(&ptype_lock);
388 * dev_remove_pack - remove packet handler
389 * @pt: packet type declaration
391 * Remove a protocol handler that was previously added to the kernel
392 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
393 * from the kernel lists and can be freed or reused once this function
394 * returns.
396 * This call sleeps to guarantee that no CPU is looking at the packet
397 * type after return.
399 void dev_remove_pack(struct packet_type *pt)
401 __dev_remove_pack(pt);
403 synchronize_net();
406 /******************************************************************************
408 Device Boot-time Settings Routines
410 *******************************************************************************/
412 /* Boot time configuration table */
413 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
416 * netdev_boot_setup_add - add new setup entry
417 * @name: name of the device
418 * @map: configured settings for the device
420 * Adds new setup entry to the dev_boot_setup list. The function
421 * returns 0 on error and 1 on success. This is a generic routine to
422 * all netdevices.
424 static int netdev_boot_setup_add(char *name, struct ifmap *map)
426 struct netdev_boot_setup *s;
427 int i;
429 s = dev_boot_setup;
430 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
431 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
432 memset(s[i].name, 0, sizeof(s[i].name));
433 strcpy(s[i].name, name);
434 memcpy(&s[i].map, map, sizeof(s[i].map));
435 break;
439 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
443 * netdev_boot_setup_check - check boot time settings
444 * @dev: the netdevice
446 * Check boot time settings for the device.
447 * The found settings are set for the device to be used
448 * later in the device probing.
449 * Returns 0 if no settings found, 1 if they are.
451 int netdev_boot_setup_check(struct net_device *dev)
453 struct netdev_boot_setup *s = dev_boot_setup;
454 int i;
456 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
457 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
458 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
459 dev->irq = s[i].map.irq;
460 dev->base_addr = s[i].map.base_addr;
461 dev->mem_start = s[i].map.mem_start;
462 dev->mem_end = s[i].map.mem_end;
463 return 1;
466 return 0;
471 * netdev_boot_base - get address from boot time settings
472 * @prefix: prefix for network device
473 * @unit: id for network device
475 * Check boot time settings for the base address of device.
476 * The found settings are set for the device to be used
477 * later in the device probing.
478 * Returns 0 if no settings found.
480 unsigned long netdev_boot_base(const char *prefix, int unit)
482 const struct netdev_boot_setup *s = dev_boot_setup;
483 char name[IFNAMSIZ];
484 int i;
486 sprintf(name, "%s%d", prefix, unit);
489 * If device already registered then return base of 1
490 * to indicate not to probe for this interface
492 if (__dev_get_by_name(name))
493 return 1;
495 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
496 if (!strcmp(name, s[i].name))
497 return s[i].map.base_addr;
498 return 0;
502 * Saves at boot time configured settings for any netdevice.
504 int __init netdev_boot_setup(char *str)
506 int ints[5];
507 struct ifmap map;
509 str = get_options(str, ARRAY_SIZE(ints), ints);
510 if (!str || !*str)
511 return 0;
513 /* Save settings */
514 memset(&map, 0, sizeof(map));
515 if (ints[0] > 0)
516 map.irq = ints[1];
517 if (ints[0] > 1)
518 map.base_addr = ints[2];
519 if (ints[0] > 2)
520 map.mem_start = ints[3];
521 if (ints[0] > 3)
522 map.mem_end = ints[4];
524 /* Add new entry to the list */
525 return netdev_boot_setup_add(str, &map);
528 __setup("netdev=", netdev_boot_setup);
530 /*******************************************************************************
532 Device Interface Subroutines
534 *******************************************************************************/
537 * __dev_get_by_name - find a device by its name
538 * @name: name to find
540 * Find an interface by name. Must be called under RTNL semaphore
541 * or @dev_base_lock. If the name is found a pointer to the device
542 * is returned. If the name is not found then %NULL is returned. The
543 * reference counters are not incremented so the caller must be
544 * careful with locks.
547 struct net_device *__dev_get_by_name(const char *name)
549 struct hlist_node *p;
551 hlist_for_each(p, dev_name_hash(name)) {
552 struct net_device *dev
553 = hlist_entry(p, struct net_device, name_hlist);
554 if (!strncmp(dev->name, name, IFNAMSIZ))
555 return dev;
557 return NULL;
561 * dev_get_by_name - find a device by its name
562 * @name: name to find
564 * Find an interface by name. This can be called from any
565 * context and does its own locking. The returned handle has
566 * the usage count incremented and the caller must use dev_put() to
567 * release it when it is no longer needed. %NULL is returned if no
568 * matching device is found.
571 struct net_device *dev_get_by_name(const char *name)
573 struct net_device *dev;
575 read_lock(&dev_base_lock);
576 dev = __dev_get_by_name(name);
577 if (dev)
578 dev_hold(dev);
579 read_unlock(&dev_base_lock);
580 return dev;
584 * __dev_get_by_index - find a device by its ifindex
585 * @ifindex: index of device
587 * Search for an interface by index. Returns %NULL if the device
588 * is not found or a pointer to the device. The device has not
589 * had its reference counter increased so the caller must be careful
590 * about locking. The caller must hold either the RTNL semaphore
591 * or @dev_base_lock.
594 struct net_device *__dev_get_by_index(int ifindex)
596 struct hlist_node *p;
598 hlist_for_each(p, dev_index_hash(ifindex)) {
599 struct net_device *dev
600 = hlist_entry(p, struct net_device, index_hlist);
601 if (dev->ifindex == ifindex)
602 return dev;
604 return NULL;
609 * dev_get_by_index - find a device by its ifindex
610 * @ifindex: index of device
612 * Search for an interface by index. Returns NULL if the device
613 * is not found or a pointer to the device. The device returned has
614 * had a reference added and the pointer is safe until the user calls
615 * dev_put to indicate they have finished with it.
618 struct net_device *dev_get_by_index(int ifindex)
620 struct net_device *dev;
622 read_lock(&dev_base_lock);
623 dev = __dev_get_by_index(ifindex);
624 if (dev)
625 dev_hold(dev);
626 read_unlock(&dev_base_lock);
627 return dev;
631 * dev_getbyhwaddr - find a device by its hardware address
632 * @type: media type of device
633 * @ha: hardware address
635 * Search for an interface by MAC address. Returns NULL if the device
636 * is not found or a pointer to the device. The caller must hold the
637 * rtnl semaphore. The returned device has not had its ref count increased
638 * and the caller must therefore be careful about locking
640 * BUGS:
641 * If the API was consistent this would be __dev_get_by_hwaddr
644 struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
646 struct net_device *dev;
648 ASSERT_RTNL();
650 for_each_netdev(dev)
651 if (dev->type == type &&
652 !memcmp(dev->dev_addr, ha, dev->addr_len))
653 return dev;
655 return NULL;
658 EXPORT_SYMBOL(dev_getbyhwaddr);
660 struct net_device *__dev_getfirstbyhwtype(unsigned short type)
662 struct net_device *dev;
664 ASSERT_RTNL();
665 for_each_netdev(dev)
666 if (dev->type == type)
667 return dev;
669 return NULL;
672 EXPORT_SYMBOL(__dev_getfirstbyhwtype);
674 struct net_device *dev_getfirstbyhwtype(unsigned short type)
676 struct net_device *dev;
678 rtnl_lock();
679 dev = __dev_getfirstbyhwtype(type);
680 if (dev)
681 dev_hold(dev);
682 rtnl_unlock();
683 return dev;
686 EXPORT_SYMBOL(dev_getfirstbyhwtype);
689 * dev_get_by_flags - find any device with given flags
690 * @if_flags: IFF_* values
691 * @mask: bitmask of bits in if_flags to check
693 * Search for any interface with the given flags. Returns NULL if a device
694 * is not found or a pointer to the device. The device returned has
695 * had a reference added and the pointer is safe until the user calls
696 * dev_put to indicate they have finished with it.
699 struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
701 struct net_device *dev, *ret;
703 ret = NULL;
704 read_lock(&dev_base_lock);
705 for_each_netdev(dev) {
706 if (((dev->flags ^ if_flags) & mask) == 0) {
707 dev_hold(dev);
708 ret = dev;
709 break;
712 read_unlock(&dev_base_lock);
713 return ret;
717 * dev_valid_name - check if name is okay for network device
718 * @name: name string
720 * Network device names need to be valid file names to
721 * to allow sysfs to work. We also disallow any kind of
722 * whitespace.
724 int dev_valid_name(const char *name)
726 if (*name == '\0')
727 return 0;
728 if (strlen(name) >= IFNAMSIZ)
729 return 0;
730 if (!strcmp(name, ".") || !strcmp(name, ".."))
731 return 0;
733 while (*name) {
734 if (*name == '/' || isspace(*name))
735 return 0;
736 name++;
738 return 1;
742 * dev_alloc_name - allocate a name for a device
743 * @dev: device
744 * @name: name format string
746 * Passed a format string - eg "lt%d" it will try and find a suitable
747 * id. It scans list of devices to build up a free map, then chooses
748 * the first empty slot. The caller must hold the dev_base or rtnl lock
749 * while allocating the name and adding the device in order to avoid
750 * duplicates.
751 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
752 * Returns the number of the unit assigned or a negative errno code.
755 int dev_alloc_name(struct net_device *dev, const char *name)
757 int i = 0;
758 char buf[IFNAMSIZ];
759 const char *p;
760 const int max_netdevices = 8*PAGE_SIZE;
761 long *inuse;
762 struct net_device *d;
764 p = strnchr(name, IFNAMSIZ-1, '%');
765 if (p) {
767 * Verify the string as this thing may have come from
768 * the user. There must be either one "%d" and no other "%"
769 * characters.
771 if (p[1] != 'd' || strchr(p + 2, '%'))
772 return -EINVAL;
774 /* Use one page as a bit array of possible slots */
775 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
776 if (!inuse)
777 return -ENOMEM;
779 for_each_netdev(d) {
780 if (!sscanf(d->name, name, &i))
781 continue;
782 if (i < 0 || i >= max_netdevices)
783 continue;
785 /* avoid cases where sscanf is not exact inverse of printf */
786 snprintf(buf, sizeof(buf), name, i);
787 if (!strncmp(buf, d->name, IFNAMSIZ))
788 set_bit(i, inuse);
791 i = find_first_zero_bit(inuse, max_netdevices);
792 free_page((unsigned long) inuse);
795 snprintf(buf, sizeof(buf), name, i);
796 if (!__dev_get_by_name(buf)) {
797 strlcpy(dev->name, buf, IFNAMSIZ);
798 return i;
801 /* It is possible to run out of possible slots
802 * when the name is long and there isn't enough space left
803 * for the digits, or if all bits are used.
805 return -ENFILE;
810 * dev_change_name - change name of a device
811 * @dev: device
812 * @newname: name (or format string) must be at least IFNAMSIZ
814 * Change name of a device, can pass format strings "eth%d".
815 * for wildcarding.
817 int dev_change_name(struct net_device *dev, char *newname)
819 int err = 0;
821 ASSERT_RTNL();
823 if (dev->flags & IFF_UP)
824 return -EBUSY;
826 if (!dev_valid_name(newname))
827 return -EINVAL;
829 if (strchr(newname, '%')) {
830 err = dev_alloc_name(dev, newname);
831 if (err < 0)
832 return err;
833 strcpy(newname, dev->name);
835 else if (__dev_get_by_name(newname))
836 return -EEXIST;
837 else
838 strlcpy(dev->name, newname, IFNAMSIZ);
840 device_rename(&dev->dev, dev->name);
841 hlist_del(&dev->name_hlist);
842 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
843 raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
845 return err;
849 * netdev_features_change - device changes features
850 * @dev: device to cause notification
852 * Called to indicate a device has changed features.
854 void netdev_features_change(struct net_device *dev)
856 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
858 EXPORT_SYMBOL(netdev_features_change);
861 * netdev_state_change - device changes state
862 * @dev: device to cause notification
864 * Called to indicate a device has changed state. This function calls
865 * the notifier chains for netdev_chain and sends a NEWLINK message
866 * to the routing socket.
868 void netdev_state_change(struct net_device *dev)
870 if (dev->flags & IFF_UP) {
871 raw_notifier_call_chain(&netdev_chain,
872 NETDEV_CHANGE, dev);
873 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
878 * dev_load - load a network module
879 * @name: name of interface
881 * If a network interface is not present and the process has suitable
882 * privileges this function loads the module. If module loading is not
883 * available in this kernel then it becomes a nop.
886 void dev_load(const char *name)
888 struct net_device *dev;
890 read_lock(&dev_base_lock);
891 dev = __dev_get_by_name(name);
892 read_unlock(&dev_base_lock);
894 if (!dev && capable(CAP_SYS_MODULE))
895 request_module("%s", name);
898 static int default_rebuild_header(struct sk_buff *skb)
900 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
901 skb->dev ? skb->dev->name : "NULL!!!");
902 kfree_skb(skb);
903 return 1;
907 * dev_open - prepare an interface for use.
908 * @dev: device to open
910 * Takes a device from down to up state. The device's private open
911 * function is invoked and then the multicast lists are loaded. Finally
912 * the device is moved into the up state and a %NETDEV_UP message is
913 * sent to the netdev notifier chain.
915 * Calling this function on an active interface is a nop. On a failure
916 * a negative errno code is returned.
918 int dev_open(struct net_device *dev)
920 int ret = 0;
923 * Is it already up?
926 if (dev->flags & IFF_UP)
927 return 0;
930 * Is it even present?
932 if (!netif_device_present(dev))
933 return -ENODEV;
936 * Call device private open method
938 set_bit(__LINK_STATE_START, &dev->state);
939 if (dev->open) {
940 ret = dev->open(dev);
941 if (ret)
942 clear_bit(__LINK_STATE_START, &dev->state);
946 * If it went open OK then:
949 if (!ret) {
951 * Set the flags.
953 dev->flags |= IFF_UP;
956 * Initialize multicasting status
958 dev_mc_upload(dev);
961 * Wakeup transmit queue engine
963 dev_activate(dev);
966 * ... and announce new interface.
968 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
970 return ret;
974 * dev_close - shutdown an interface.
975 * @dev: device to shutdown
977 * This function moves an active device into down state. A
978 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
979 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
980 * chain.
982 int dev_close(struct net_device *dev)
984 if (!(dev->flags & IFF_UP))
985 return 0;
988 * Tell people we are going down, so that they can
989 * prepare to death, when device is still operating.
991 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
993 dev_deactivate(dev);
995 clear_bit(__LINK_STATE_START, &dev->state);
997 /* Synchronize to scheduled poll. We cannot touch poll list,
998 * it can be even on different cpu. So just clear netif_running(),
999 * and wait when poll really will happen. Actually, the best place
1000 * for this is inside dev->stop() after device stopped its irq
1001 * engine, but this requires more changes in devices. */
1003 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1004 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
1005 /* No hurry. */
1006 msleep(1);
1010 * Call the device specific close. This cannot fail.
1011 * Only if device is UP
1013 * We allow it to be called even after a DETACH hot-plug
1014 * event.
1016 if (dev->stop)
1017 dev->stop(dev);
1020 * Device is now down.
1023 dev->flags &= ~IFF_UP;
1026 * Tell people we are down
1028 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
1030 return 0;
1035 * Device change register/unregister. These are not inline or static
1036 * as we export them to the world.
1040 * register_netdevice_notifier - register a network notifier block
1041 * @nb: notifier
1043 * Register a notifier to be called when network device events occur.
1044 * The notifier passed is linked into the kernel structures and must
1045 * not be reused until it has been unregistered. A negative errno code
1046 * is returned on a failure.
1048 * When registered all registration and up events are replayed
1049 * to the new notifier to allow device to have a race free
1050 * view of the network device list.
1053 int register_netdevice_notifier(struct notifier_block *nb)
1055 struct net_device *dev;
1056 int err;
1058 rtnl_lock();
1059 err = raw_notifier_chain_register(&netdev_chain, nb);
1060 if (!err) {
1061 for_each_netdev(dev) {
1062 nb->notifier_call(nb, NETDEV_REGISTER, dev);
1064 if (dev->flags & IFF_UP)
1065 nb->notifier_call(nb, NETDEV_UP, dev);
1068 rtnl_unlock();
1069 return err;
1073 * unregister_netdevice_notifier - unregister a network notifier block
1074 * @nb: notifier
1076 * Unregister a notifier previously registered by
1077 * register_netdevice_notifier(). The notifier is unlinked into the
1078 * kernel structures and may then be reused. A negative errno code
1079 * is returned on a failure.
1082 int unregister_netdevice_notifier(struct notifier_block *nb)
1084 int err;
1086 rtnl_lock();
1087 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1088 rtnl_unlock();
1089 return err;
1093 * call_netdevice_notifiers - call all network notifier blocks
1094 * @val: value passed unmodified to notifier function
1095 * @v: pointer passed unmodified to notifier function
1097 * Call all network notifier blocks. Parameters and return value
1098 * are as for raw_notifier_call_chain().
1101 int call_netdevice_notifiers(unsigned long val, void *v)
1103 return raw_notifier_call_chain(&netdev_chain, val, v);
1106 /* When > 0 there are consumers of rx skb time stamps */
1107 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1109 void net_enable_timestamp(void)
1111 atomic_inc(&netstamp_needed);
1114 void net_disable_timestamp(void)
1116 atomic_dec(&netstamp_needed);
1119 static inline void net_timestamp(struct sk_buff *skb)
1121 if (atomic_read(&netstamp_needed))
1122 __net_timestamp(skb);
1123 else
1124 skb->tstamp.tv64 = 0;
1128 * Support routine. Sends outgoing frames to any network
1129 * taps currently in use.
1132 static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1134 struct packet_type *ptype;
1136 net_timestamp(skb);
1138 rcu_read_lock();
1139 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1140 /* Never send packets back to the socket
1141 * they originated from - MvS (miquels@drinkel.ow.org)
1143 if ((ptype->dev == dev || !ptype->dev) &&
1144 (ptype->af_packet_priv == NULL ||
1145 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1146 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1147 if (!skb2)
1148 break;
1150 /* skb->nh should be correctly
1151 set by sender, so that the second statement is
1152 just protection against buggy protocols.
1154 skb_reset_mac_header(skb2);
1156 if (skb_network_header(skb2) < skb2->data ||
1157 skb2->network_header > skb2->tail) {
1158 if (net_ratelimit())
1159 printk(KERN_CRIT "protocol %04x is "
1160 "buggy, dev %s\n",
1161 skb2->protocol, dev->name);
1162 skb_reset_network_header(skb2);
1165 skb2->transport_header = skb2->network_header;
1166 skb2->pkt_type = PACKET_OUTGOING;
1167 ptype->func(skb2, skb->dev, ptype, skb->dev);
1170 rcu_read_unlock();
1174 void __netif_schedule(struct net_device *dev)
1176 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1177 unsigned long flags;
1178 struct softnet_data *sd;
1180 local_irq_save(flags);
1181 sd = &__get_cpu_var(softnet_data);
1182 dev->next_sched = sd->output_queue;
1183 sd->output_queue = dev;
1184 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1185 local_irq_restore(flags);
1188 EXPORT_SYMBOL(__netif_schedule);
1190 void __netif_rx_schedule(struct net_device *dev)
1192 unsigned long flags;
1194 local_irq_save(flags);
1195 dev_hold(dev);
1196 list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
1197 if (dev->quota < 0)
1198 dev->quota += dev->weight;
1199 else
1200 dev->quota = dev->weight;
1201 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1202 local_irq_restore(flags);
1204 EXPORT_SYMBOL(__netif_rx_schedule);
1206 void dev_kfree_skb_any(struct sk_buff *skb)
1208 if (in_irq() || irqs_disabled())
1209 dev_kfree_skb_irq(skb);
1210 else
1211 dev_kfree_skb(skb);
1213 EXPORT_SYMBOL(dev_kfree_skb_any);
1216 /* Hot-plugging. */
1217 void netif_device_detach(struct net_device *dev)
1219 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1220 netif_running(dev)) {
1221 netif_stop_queue(dev);
1224 EXPORT_SYMBOL(netif_device_detach);
1226 void netif_device_attach(struct net_device *dev)
1228 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1229 netif_running(dev)) {
1230 netif_wake_queue(dev);
1231 __netdev_watchdog_up(dev);
1234 EXPORT_SYMBOL(netif_device_attach);
1238 * Invalidate hardware checksum when packet is to be mangled, and
1239 * complete checksum manually on outgoing path.
1241 int skb_checksum_help(struct sk_buff *skb)
1243 __wsum csum;
1244 int ret = 0, offset;
1246 if (skb->ip_summed == CHECKSUM_COMPLETE)
1247 goto out_set_summed;
1249 if (unlikely(skb_shinfo(skb)->gso_size)) {
1250 /* Let GSO fix up the checksum. */
1251 goto out_set_summed;
1254 if (skb_cloned(skb)) {
1255 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1256 if (ret)
1257 goto out;
1260 offset = skb->csum_start - skb_headroom(skb);
1261 BUG_ON(offset > (int)skb->len);
1262 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1264 offset = skb_headlen(skb) - offset;
1265 BUG_ON(offset <= 0);
1266 BUG_ON(skb->csum_offset + 2 > offset);
1268 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) =
1269 csum_fold(csum);
1270 out_set_summed:
1271 skb->ip_summed = CHECKSUM_NONE;
1272 out:
1273 return ret;
1277 * skb_gso_segment - Perform segmentation on skb.
1278 * @skb: buffer to segment
1279 * @features: features for the output path (see dev->features)
1281 * This function segments the given skb and returns a list of segments.
1283 * It may return NULL if the skb requires no segmentation. This is
1284 * only possible when GSO is used for verifying header integrity.
1286 struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
1288 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1289 struct packet_type *ptype;
1290 __be16 type = skb->protocol;
1291 int err;
1293 BUG_ON(skb_shinfo(skb)->frag_list);
1295 skb_reset_mac_header(skb);
1296 skb->mac_len = skb->network_header - skb->mac_header;
1297 __skb_pull(skb, skb->mac_len);
1299 if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
1300 if (skb_header_cloned(skb) &&
1301 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1302 return ERR_PTR(err);
1305 rcu_read_lock();
1306 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1307 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
1308 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1309 err = ptype->gso_send_check(skb);
1310 segs = ERR_PTR(err);
1311 if (err || skb_gso_ok(skb, features))
1312 break;
1313 __skb_push(skb, (skb->data -
1314 skb_network_header(skb)));
1316 segs = ptype->gso_segment(skb, features);
1317 break;
1320 rcu_read_unlock();
1322 __skb_push(skb, skb->data - skb_mac_header(skb));
1324 return segs;
1327 EXPORT_SYMBOL(skb_gso_segment);
1329 /* Take action when hardware reception checksum errors are detected. */
1330 #ifdef CONFIG_BUG
1331 void netdev_rx_csum_fault(struct net_device *dev)
1333 if (net_ratelimit()) {
1334 printk(KERN_ERR "%s: hw csum failure.\n",
1335 dev ? dev->name : "<unknown>");
1336 dump_stack();
1339 EXPORT_SYMBOL(netdev_rx_csum_fault);
1340 #endif
1342 /* Actually, we should eliminate this check as soon as we know, that:
1343 * 1. IOMMU is present and allows to map all the memory.
1344 * 2. No high memory really exists on this machine.
1347 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1349 #ifdef CONFIG_HIGHMEM
1350 int i;
1352 if (dev->features & NETIF_F_HIGHDMA)
1353 return 0;
1355 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1356 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1357 return 1;
1359 #endif
1360 return 0;
1363 struct dev_gso_cb {
1364 void (*destructor)(struct sk_buff *skb);
1367 #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1369 static void dev_gso_skb_destructor(struct sk_buff *skb)
1371 struct dev_gso_cb *cb;
1373 do {
1374 struct sk_buff *nskb = skb->next;
1376 skb->next = nskb->next;
1377 nskb->next = NULL;
1378 kfree_skb(nskb);
1379 } while (skb->next);
1381 cb = DEV_GSO_CB(skb);
1382 if (cb->destructor)
1383 cb->destructor(skb);
1387 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1388 * @skb: buffer to segment
1390 * This function segments the given skb and stores the list of segments
1391 * in skb->next.
1393 static int dev_gso_segment(struct sk_buff *skb)
1395 struct net_device *dev = skb->dev;
1396 struct sk_buff *segs;
1397 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1398 NETIF_F_SG : 0);
1400 segs = skb_gso_segment(skb, features);
1402 /* Verifying header integrity only. */
1403 if (!segs)
1404 return 0;
1406 if (unlikely(IS_ERR(segs)))
1407 return PTR_ERR(segs);
1409 skb->next = segs;
1410 DEV_GSO_CB(skb)->destructor = skb->destructor;
1411 skb->destructor = dev_gso_skb_destructor;
1413 return 0;
1416 int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1418 if (likely(!skb->next)) {
1419 if (!list_empty(&ptype_all))
1420 dev_queue_xmit_nit(skb, dev);
1422 if (netif_needs_gso(dev, skb)) {
1423 if (unlikely(dev_gso_segment(skb)))
1424 goto out_kfree_skb;
1425 if (skb->next)
1426 goto gso;
1429 return dev->hard_start_xmit(skb, dev);
1432 gso:
1433 do {
1434 struct sk_buff *nskb = skb->next;
1435 int rc;
1437 skb->next = nskb->next;
1438 nskb->next = NULL;
1439 rc = dev->hard_start_xmit(nskb, dev);
1440 if (unlikely(rc)) {
1441 nskb->next = skb->next;
1442 skb->next = nskb;
1443 return rc;
1445 if (unlikely(netif_queue_stopped(dev) && skb->next))
1446 return NETDEV_TX_BUSY;
1447 } while (skb->next);
1449 skb->destructor = DEV_GSO_CB(skb)->destructor;
1451 out_kfree_skb:
1452 kfree_skb(skb);
1453 return 0;
1456 #define HARD_TX_LOCK(dev, cpu) { \
1457 if ((dev->features & NETIF_F_LLTX) == 0) { \
1458 netif_tx_lock(dev); \
1462 #define HARD_TX_UNLOCK(dev) { \
1463 if ((dev->features & NETIF_F_LLTX) == 0) { \
1464 netif_tx_unlock(dev); \
1469 * dev_queue_xmit - transmit a buffer
1470 * @skb: buffer to transmit
1472 * Queue a buffer for transmission to a network device. The caller must
1473 * have set the device and priority and built the buffer before calling
1474 * this function. The function can be called from an interrupt.
1476 * A negative errno code is returned on a failure. A success does not
1477 * guarantee the frame will be transmitted as it may be dropped due
1478 * to congestion or traffic shaping.
1480 * -----------------------------------------------------------------------------------
1481 * I notice this method can also return errors from the queue disciplines,
1482 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1483 * be positive.
1485 * Regardless of the return value, the skb is consumed, so it is currently
1486 * difficult to retry a send to this method. (You can bump the ref count
1487 * before sending to hold a reference for retry if you are careful.)
1489 * When calling this method, interrupts MUST be enabled. This is because
1490 * the BH enable code must have IRQs enabled so that it will not deadlock.
1491 * --BLG
1494 int dev_queue_xmit(struct sk_buff *skb)
1496 struct net_device *dev = skb->dev;
1497 struct Qdisc *q;
1498 int rc = -ENOMEM;
1500 /* GSO will handle the following emulations directly. */
1501 if (netif_needs_gso(dev, skb))
1502 goto gso;
1504 if (skb_shinfo(skb)->frag_list &&
1505 !(dev->features & NETIF_F_FRAGLIST) &&
1506 __skb_linearize(skb))
1507 goto out_kfree_skb;
1509 /* Fragmented skb is linearized if device does not support SG,
1510 * or if at least one of fragments is in highmem and device
1511 * does not support DMA from it.
1513 if (skb_shinfo(skb)->nr_frags &&
1514 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1515 __skb_linearize(skb))
1516 goto out_kfree_skb;
1518 /* If packet is not checksummed and device does not support
1519 * checksumming for this protocol, complete checksumming here.
1521 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1522 skb_set_transport_header(skb, skb->csum_start -
1523 skb_headroom(skb));
1525 if (!(dev->features & NETIF_F_GEN_CSUM) &&
1526 (!(dev->features & NETIF_F_IP_CSUM) ||
1527 skb->protocol != htons(ETH_P_IP)))
1528 if (skb_checksum_help(skb))
1529 goto out_kfree_skb;
1532 gso:
1533 spin_lock_prefetch(&dev->queue_lock);
1535 /* Disable soft irqs for various locks below. Also
1536 * stops preemption for RCU.
1538 rcu_read_lock_bh();
1540 /* Updates of qdisc are serialized by queue_lock.
1541 * The struct Qdisc which is pointed to by qdisc is now a
1542 * rcu structure - it may be accessed without acquiring
1543 * a lock (but the structure may be stale.) The freeing of the
1544 * qdisc will be deferred until it's known that there are no
1545 * more references to it.
1547 * If the qdisc has an enqueue function, we still need to
1548 * hold the queue_lock before calling it, since queue_lock
1549 * also serializes access to the device queue.
1552 q = rcu_dereference(dev->qdisc);
1553 #ifdef CONFIG_NET_CLS_ACT
1554 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1555 #endif
1556 if (q->enqueue) {
1557 /* Grab device queue */
1558 spin_lock(&dev->queue_lock);
1559 q = dev->qdisc;
1560 if (q->enqueue) {
1561 rc = q->enqueue(skb, q);
1562 qdisc_run(dev);
1563 spin_unlock(&dev->queue_lock);
1565 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1566 goto out;
1568 spin_unlock(&dev->queue_lock);
1571 /* The device has no queue. Common case for software devices:
1572 loopback, all the sorts of tunnels...
1574 Really, it is unlikely that netif_tx_lock protection is necessary
1575 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1576 counters.)
1577 However, it is possible, that they rely on protection
1578 made by us here.
1580 Check this and shot the lock. It is not prone from deadlocks.
1581 Either shot noqueue qdisc, it is even simpler 8)
1583 if (dev->flags & IFF_UP) {
1584 int cpu = smp_processor_id(); /* ok because BHs are off */
1586 if (dev->xmit_lock_owner != cpu) {
1588 HARD_TX_LOCK(dev, cpu);
1590 if (!netif_queue_stopped(dev)) {
1591 rc = 0;
1592 if (!dev_hard_start_xmit(skb, dev)) {
1593 HARD_TX_UNLOCK(dev);
1594 goto out;
1597 HARD_TX_UNLOCK(dev);
1598 if (net_ratelimit())
1599 printk(KERN_CRIT "Virtual device %s asks to "
1600 "queue packet!\n", dev->name);
1601 } else {
1602 /* Recursion is detected! It is possible,
1603 * unfortunately */
1604 if (net_ratelimit())
1605 printk(KERN_CRIT "Dead loop on virtual device "
1606 "%s, fix it urgently!\n", dev->name);
1610 rc = -ENETDOWN;
1611 rcu_read_unlock_bh();
1613 out_kfree_skb:
1614 kfree_skb(skb);
1615 return rc;
1616 out:
1617 rcu_read_unlock_bh();
1618 return rc;
1622 /*=======================================================================
1623 Receiver routines
1624 =======================================================================*/
1626 int netdev_max_backlog __read_mostly = 1000;
1627 int netdev_budget __read_mostly = 300;
1628 int weight_p __read_mostly = 64; /* old backlog weight */
1630 DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1634 * netif_rx - post buffer to the network code
1635 * @skb: buffer to post
1637 * This function receives a packet from a device driver and queues it for
1638 * the upper (protocol) levels to process. It always succeeds. The buffer
1639 * may be dropped during processing for congestion control or by the
1640 * protocol layers.
1642 * return values:
1643 * NET_RX_SUCCESS (no congestion)
1644 * NET_RX_CN_LOW (low congestion)
1645 * NET_RX_CN_MOD (moderate congestion)
1646 * NET_RX_CN_HIGH (high congestion)
1647 * NET_RX_DROP (packet was dropped)
1651 int netif_rx(struct sk_buff *skb)
1653 struct softnet_data *queue;
1654 unsigned long flags;
1656 /* if netpoll wants it, pretend we never saw it */
1657 if (netpoll_rx(skb))
1658 return NET_RX_DROP;
1660 if (!skb->tstamp.tv64)
1661 net_timestamp(skb);
1664 * The code is rearranged so that the path is the most
1665 * short when CPU is congested, but is still operating.
1667 local_irq_save(flags);
1668 queue = &__get_cpu_var(softnet_data);
1670 __get_cpu_var(netdev_rx_stat).total++;
1671 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1672 if (queue->input_pkt_queue.qlen) {
1673 enqueue:
1674 dev_hold(skb->dev);
1675 __skb_queue_tail(&queue->input_pkt_queue, skb);
1676 local_irq_restore(flags);
1677 return NET_RX_SUCCESS;
1680 netif_rx_schedule(&queue->backlog_dev);
1681 goto enqueue;
1684 __get_cpu_var(netdev_rx_stat).dropped++;
1685 local_irq_restore(flags);
1687 kfree_skb(skb);
1688 return NET_RX_DROP;
1691 int netif_rx_ni(struct sk_buff *skb)
1693 int err;
1695 preempt_disable();
1696 err = netif_rx(skb);
1697 if (local_softirq_pending())
1698 do_softirq();
1699 preempt_enable();
1701 return err;
1704 EXPORT_SYMBOL(netif_rx_ni);
1706 static inline struct net_device *skb_bond(struct sk_buff *skb)
1708 struct net_device *dev = skb->dev;
1710 if (dev->master) {
1711 if (skb_bond_should_drop(skb)) {
1712 kfree_skb(skb);
1713 return NULL;
1715 skb->dev = dev->master;
1718 return dev;
1721 static void net_tx_action(struct softirq_action *h)
1723 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1725 if (sd->completion_queue) {
1726 struct sk_buff *clist;
1728 local_irq_disable();
1729 clist = sd->completion_queue;
1730 sd->completion_queue = NULL;
1731 local_irq_enable();
1733 while (clist) {
1734 struct sk_buff *skb = clist;
1735 clist = clist->next;
1737 BUG_TRAP(!atomic_read(&skb->users));
1738 __kfree_skb(skb);
1742 if (sd->output_queue) {
1743 struct net_device *head;
1745 local_irq_disable();
1746 head = sd->output_queue;
1747 sd->output_queue = NULL;
1748 local_irq_enable();
1750 while (head) {
1751 struct net_device *dev = head;
1752 head = head->next_sched;
1754 smp_mb__before_clear_bit();
1755 clear_bit(__LINK_STATE_SCHED, &dev->state);
1757 if (spin_trylock(&dev->queue_lock)) {
1758 qdisc_run(dev);
1759 spin_unlock(&dev->queue_lock);
1760 } else {
1761 netif_schedule(dev);
1767 static inline int deliver_skb(struct sk_buff *skb,
1768 struct packet_type *pt_prev,
1769 struct net_device *orig_dev)
1771 atomic_inc(&skb->users);
1772 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1775 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1776 /* These hooks defined here for ATM */
1777 struct net_bridge;
1778 struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1779 unsigned char *addr);
1780 void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
1783 * If bridge module is loaded call bridging hook.
1784 * returns NULL if packet was consumed.
1786 struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
1787 struct sk_buff *skb) __read_mostly;
1788 static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
1789 struct packet_type **pt_prev, int *ret,
1790 struct net_device *orig_dev)
1792 struct net_bridge_port *port;
1794 if (skb->pkt_type == PACKET_LOOPBACK ||
1795 (port = rcu_dereference(skb->dev->br_port)) == NULL)
1796 return skb;
1798 if (*pt_prev) {
1799 *ret = deliver_skb(skb, *pt_prev, orig_dev);
1800 *pt_prev = NULL;
1803 return br_handle_frame_hook(port, skb);
1805 #else
1806 #define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
1807 #endif
1809 #ifdef CONFIG_NET_CLS_ACT
1810 /* TODO: Maybe we should just force sch_ingress to be compiled in
1811 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1812 * a compare and 2 stores extra right now if we dont have it on
1813 * but have CONFIG_NET_CLS_ACT
1814 * NOTE: This doesnt stop any functionality; if you dont have
1815 * the ingress scheduler, you just cant add policies on ingress.
1818 static int ing_filter(struct sk_buff *skb)
1820 struct Qdisc *q;
1821 struct net_device *dev = skb->dev;
1822 int result = TC_ACT_OK;
1824 if (dev->qdisc_ingress) {
1825 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1826 if (MAX_RED_LOOP < ttl++) {
1827 printk(KERN_WARNING "Redir loop detected Dropping packet (%d->%d)\n",
1828 skb->iif, skb->dev->ifindex);
1829 return TC_ACT_SHOT;
1832 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1834 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1836 spin_lock(&dev->ingress_lock);
1837 if ((q = dev->qdisc_ingress) != NULL)
1838 result = q->enqueue(skb, q);
1839 spin_unlock(&dev->ingress_lock);
1843 return result;
1845 #endif
1847 int netif_receive_skb(struct sk_buff *skb)
1849 struct packet_type *ptype, *pt_prev;
1850 struct net_device *orig_dev;
1851 int ret = NET_RX_DROP;
1852 __be16 type;
1854 /* if we've gotten here through NAPI, check netpoll */
1855 if (skb->dev->poll && netpoll_rx(skb))
1856 return NET_RX_DROP;
1858 if (!skb->tstamp.tv64)
1859 net_timestamp(skb);
1861 if (!skb->iif)
1862 skb->iif = skb->dev->ifindex;
1864 orig_dev = skb_bond(skb);
1866 if (!orig_dev)
1867 return NET_RX_DROP;
1869 __get_cpu_var(netdev_rx_stat).total++;
1871 skb_reset_network_header(skb);
1872 skb_reset_transport_header(skb);
1873 skb->mac_len = skb->network_header - skb->mac_header;
1875 pt_prev = NULL;
1877 rcu_read_lock();
1879 #ifdef CONFIG_NET_CLS_ACT
1880 if (skb->tc_verd & TC_NCLS) {
1881 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1882 goto ncls;
1884 #endif
1886 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1887 if (!ptype->dev || ptype->dev == skb->dev) {
1888 if (pt_prev)
1889 ret = deliver_skb(skb, pt_prev, orig_dev);
1890 pt_prev = ptype;
1894 #ifdef CONFIG_NET_CLS_ACT
1895 if (pt_prev) {
1896 ret = deliver_skb(skb, pt_prev, orig_dev);
1897 pt_prev = NULL; /* noone else should process this after*/
1898 } else {
1899 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1902 ret = ing_filter(skb);
1904 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1905 kfree_skb(skb);
1906 goto out;
1909 skb->tc_verd = 0;
1910 ncls:
1911 #endif
1913 skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
1914 if (!skb)
1915 goto out;
1917 type = skb->protocol;
1918 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1919 if (ptype->type == type &&
1920 (!ptype->dev || ptype->dev == skb->dev)) {
1921 if (pt_prev)
1922 ret = deliver_skb(skb, pt_prev, orig_dev);
1923 pt_prev = ptype;
1927 if (pt_prev) {
1928 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1929 } else {
1930 kfree_skb(skb);
1931 /* Jamal, now you will not able to escape explaining
1932 * me how you were going to use this. :-)
1934 ret = NET_RX_DROP;
1937 out:
1938 rcu_read_unlock();
1939 return ret;
1942 static int process_backlog(struct net_device *backlog_dev, int *budget)
1944 int work = 0;
1945 int quota = min(backlog_dev->quota, *budget);
1946 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1947 unsigned long start_time = jiffies;
1949 backlog_dev->weight = weight_p;
1950 for (;;) {
1951 struct sk_buff *skb;
1952 struct net_device *dev;
1954 local_irq_disable();
1955 skb = __skb_dequeue(&queue->input_pkt_queue);
1956 if (!skb)
1957 goto job_done;
1958 local_irq_enable();
1960 dev = skb->dev;
1962 netif_receive_skb(skb);
1964 dev_put(dev);
1966 work++;
1968 if (work >= quota || jiffies - start_time > 1)
1969 break;
1973 backlog_dev->quota -= work;
1974 *budget -= work;
1975 return -1;
1977 job_done:
1978 backlog_dev->quota -= work;
1979 *budget -= work;
1981 list_del(&backlog_dev->poll_list);
1982 smp_mb__before_clear_bit();
1983 netif_poll_enable(backlog_dev);
1985 local_irq_enable();
1986 return 0;
1989 static void net_rx_action(struct softirq_action *h)
1991 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1992 unsigned long start_time = jiffies;
1993 int budget = netdev_budget;
1994 void *have;
1996 local_irq_disable();
1998 while (!list_empty(&queue->poll_list)) {
1999 struct net_device *dev;
2001 if (budget <= 0 || jiffies - start_time > 1)
2002 goto softnet_break;
2004 local_irq_enable();
2006 dev = list_entry(queue->poll_list.next,
2007 struct net_device, poll_list);
2008 have = netpoll_poll_lock(dev);
2010 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
2011 netpoll_poll_unlock(have);
2012 local_irq_disable();
2013 list_move_tail(&dev->poll_list, &queue->poll_list);
2014 if (dev->quota < 0)
2015 dev->quota += dev->weight;
2016 else
2017 dev->quota = dev->weight;
2018 } else {
2019 netpoll_poll_unlock(have);
2020 dev_put(dev);
2021 local_irq_disable();
2024 out:
2025 local_irq_enable();
2026 #ifdef CONFIG_NET_DMA
2028 * There may not be any more sk_buffs coming right now, so push
2029 * any pending DMA copies to hardware
2031 if (!cpus_empty(net_dma.channel_mask)) {
2032 int chan_idx;
2033 for_each_cpu_mask(chan_idx, net_dma.channel_mask) {
2034 struct dma_chan *chan = net_dma.channels[chan_idx];
2035 if (chan)
2036 dma_async_memcpy_issue_pending(chan);
2039 #endif
2040 return;
2042 softnet_break:
2043 __get_cpu_var(netdev_rx_stat).time_squeeze++;
2044 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2045 goto out;
2048 static gifconf_func_t * gifconf_list [NPROTO];
2051 * register_gifconf - register a SIOCGIF handler
2052 * @family: Address family
2053 * @gifconf: Function handler
2055 * Register protocol dependent address dumping routines. The handler
2056 * that is passed must not be freed or reused until it has been replaced
2057 * by another handler.
2059 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2061 if (family >= NPROTO)
2062 return -EINVAL;
2063 gifconf_list[family] = gifconf;
2064 return 0;
2069 * Map an interface index to its name (SIOCGIFNAME)
2073 * We need this ioctl for efficient implementation of the
2074 * if_indextoname() function required by the IPv6 API. Without
2075 * it, we would have to search all the interfaces to find a
2076 * match. --pb
2079 static int dev_ifname(struct ifreq __user *arg)
2081 struct net_device *dev;
2082 struct ifreq ifr;
2085 * Fetch the caller's info block.
2088 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2089 return -EFAULT;
2091 read_lock(&dev_base_lock);
2092 dev = __dev_get_by_index(ifr.ifr_ifindex);
2093 if (!dev) {
2094 read_unlock(&dev_base_lock);
2095 return -ENODEV;
2098 strcpy(ifr.ifr_name, dev->name);
2099 read_unlock(&dev_base_lock);
2101 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2102 return -EFAULT;
2103 return 0;
2107 * Perform a SIOCGIFCONF call. This structure will change
2108 * size eventually, and there is nothing I can do about it.
2109 * Thus we will need a 'compatibility mode'.
2112 static int dev_ifconf(char __user *arg)
2114 struct ifconf ifc;
2115 struct net_device *dev;
2116 char __user *pos;
2117 int len;
2118 int total;
2119 int i;
2122 * Fetch the caller's info block.
2125 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2126 return -EFAULT;
2128 pos = ifc.ifc_buf;
2129 len = ifc.ifc_len;
2132 * Loop over the interfaces, and write an info block for each.
2135 total = 0;
2136 for_each_netdev(dev) {
2137 for (i = 0; i < NPROTO; i++) {
2138 if (gifconf_list[i]) {
2139 int done;
2140 if (!pos)
2141 done = gifconf_list[i](dev, NULL, 0);
2142 else
2143 done = gifconf_list[i](dev, pos + total,
2144 len - total);
2145 if (done < 0)
2146 return -EFAULT;
2147 total += done;
2153 * All done. Write the updated control block back to the caller.
2155 ifc.ifc_len = total;
2158 * Both BSD and Solaris return 0 here, so we do too.
2160 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2163 #ifdef CONFIG_PROC_FS
2165 * This is invoked by the /proc filesystem handler to display a device
2166 * in detail.
2168 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2170 loff_t off;
2171 struct net_device *dev;
2173 read_lock(&dev_base_lock);
2174 if (!*pos)
2175 return SEQ_START_TOKEN;
2177 off = 1;
2178 for_each_netdev(dev)
2179 if (off++ == *pos)
2180 return dev;
2182 return NULL;
2185 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2187 ++*pos;
2188 return v == SEQ_START_TOKEN ?
2189 first_net_device() : next_net_device((struct net_device *)v);
2192 void dev_seq_stop(struct seq_file *seq, void *v)
2194 read_unlock(&dev_base_lock);
2197 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2199 struct net_device_stats *stats = dev->get_stats(dev);
2201 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2202 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2203 dev->name, stats->rx_bytes, stats->rx_packets,
2204 stats->rx_errors,
2205 stats->rx_dropped + stats->rx_missed_errors,
2206 stats->rx_fifo_errors,
2207 stats->rx_length_errors + stats->rx_over_errors +
2208 stats->rx_crc_errors + stats->rx_frame_errors,
2209 stats->rx_compressed, stats->multicast,
2210 stats->tx_bytes, stats->tx_packets,
2211 stats->tx_errors, stats->tx_dropped,
2212 stats->tx_fifo_errors, stats->collisions,
2213 stats->tx_carrier_errors +
2214 stats->tx_aborted_errors +
2215 stats->tx_window_errors +
2216 stats->tx_heartbeat_errors,
2217 stats->tx_compressed);
2221 * Called from the PROCfs module. This now uses the new arbitrary sized
2222 * /proc/net interface to create /proc/net/dev
2224 static int dev_seq_show(struct seq_file *seq, void *v)
2226 if (v == SEQ_START_TOKEN)
2227 seq_puts(seq, "Inter-| Receive "
2228 " | Transmit\n"
2229 " face |bytes packets errs drop fifo frame "
2230 "compressed multicast|bytes packets errs "
2231 "drop fifo colls carrier compressed\n");
2232 else
2233 dev_seq_printf_stats(seq, v);
2234 return 0;
2237 static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2239 struct netif_rx_stats *rc = NULL;
2241 while (*pos < NR_CPUS)
2242 if (cpu_online(*pos)) {
2243 rc = &per_cpu(netdev_rx_stat, *pos);
2244 break;
2245 } else
2246 ++*pos;
2247 return rc;
2250 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2252 return softnet_get_online(pos);
2255 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2257 ++*pos;
2258 return softnet_get_online(pos);
2261 static void softnet_seq_stop(struct seq_file *seq, void *v)
2265 static int softnet_seq_show(struct seq_file *seq, void *v)
2267 struct netif_rx_stats *s = v;
2269 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2270 s->total, s->dropped, s->time_squeeze, 0,
2271 0, 0, 0, 0, /* was fastroute */
2272 s->cpu_collision );
2273 return 0;
2276 static const struct seq_operations dev_seq_ops = {
2277 .start = dev_seq_start,
2278 .next = dev_seq_next,
2279 .stop = dev_seq_stop,
2280 .show = dev_seq_show,
2283 static int dev_seq_open(struct inode *inode, struct file *file)
2285 return seq_open(file, &dev_seq_ops);
2288 static const struct file_operations dev_seq_fops = {
2289 .owner = THIS_MODULE,
2290 .open = dev_seq_open,
2291 .read = seq_read,
2292 .llseek = seq_lseek,
2293 .release = seq_release,
2296 static const struct seq_operations softnet_seq_ops = {
2297 .start = softnet_seq_start,
2298 .next = softnet_seq_next,
2299 .stop = softnet_seq_stop,
2300 .show = softnet_seq_show,
2303 static int softnet_seq_open(struct inode *inode, struct file *file)
2305 return seq_open(file, &softnet_seq_ops);
2308 static const struct file_operations softnet_seq_fops = {
2309 .owner = THIS_MODULE,
2310 .open = softnet_seq_open,
2311 .read = seq_read,
2312 .llseek = seq_lseek,
2313 .release = seq_release,
2316 static void *ptype_get_idx(loff_t pos)
2318 struct packet_type *pt = NULL;
2319 loff_t i = 0;
2320 int t;
2322 list_for_each_entry_rcu(pt, &ptype_all, list) {
2323 if (i == pos)
2324 return pt;
2325 ++i;
2328 for (t = 0; t < 16; t++) {
2329 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
2330 if (i == pos)
2331 return pt;
2332 ++i;
2335 return NULL;
2338 static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
2340 rcu_read_lock();
2341 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
2344 static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2346 struct packet_type *pt;
2347 struct list_head *nxt;
2348 int hash;
2350 ++*pos;
2351 if (v == SEQ_START_TOKEN)
2352 return ptype_get_idx(0);
2354 pt = v;
2355 nxt = pt->list.next;
2356 if (pt->type == htons(ETH_P_ALL)) {
2357 if (nxt != &ptype_all)
2358 goto found;
2359 hash = 0;
2360 nxt = ptype_base[0].next;
2361 } else
2362 hash = ntohs(pt->type) & 15;
2364 while (nxt == &ptype_base[hash]) {
2365 if (++hash >= 16)
2366 return NULL;
2367 nxt = ptype_base[hash].next;
2369 found:
2370 return list_entry(nxt, struct packet_type, list);
2373 static void ptype_seq_stop(struct seq_file *seq, void *v)
2375 rcu_read_unlock();
2378 static void ptype_seq_decode(struct seq_file *seq, void *sym)
2380 #ifdef CONFIG_KALLSYMS
2381 unsigned long offset = 0, symsize;
2382 const char *symname;
2383 char *modname;
2384 char namebuf[128];
2386 symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
2387 &modname, namebuf);
2389 if (symname) {
2390 char *delim = ":";
2392 if (!modname)
2393 modname = delim = "";
2394 seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
2395 symname, offset);
2396 return;
2398 #endif
2400 seq_printf(seq, "[%p]", sym);
2403 static int ptype_seq_show(struct seq_file *seq, void *v)
2405 struct packet_type *pt = v;
2407 if (v == SEQ_START_TOKEN)
2408 seq_puts(seq, "Type Device Function\n");
2409 else {
2410 if (pt->type == htons(ETH_P_ALL))
2411 seq_puts(seq, "ALL ");
2412 else
2413 seq_printf(seq, "%04x", ntohs(pt->type));
2415 seq_printf(seq, " %-8s ",
2416 pt->dev ? pt->dev->name : "");
2417 ptype_seq_decode(seq, pt->func);
2418 seq_putc(seq, '\n');
2421 return 0;
2424 static const struct seq_operations ptype_seq_ops = {
2425 .start = ptype_seq_start,
2426 .next = ptype_seq_next,
2427 .stop = ptype_seq_stop,
2428 .show = ptype_seq_show,
2431 static int ptype_seq_open(struct inode *inode, struct file *file)
2433 return seq_open(file, &ptype_seq_ops);
2436 static const struct file_operations ptype_seq_fops = {
2437 .owner = THIS_MODULE,
2438 .open = ptype_seq_open,
2439 .read = seq_read,
2440 .llseek = seq_lseek,
2441 .release = seq_release,
2445 static int __init dev_proc_init(void)
2447 int rc = -ENOMEM;
2449 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2450 goto out;
2451 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2452 goto out_dev;
2453 if (!proc_net_fops_create("ptype", S_IRUGO, &ptype_seq_fops))
2454 goto out_dev2;
2456 if (wext_proc_init())
2457 goto out_softnet;
2458 rc = 0;
2459 out:
2460 return rc;
2461 out_softnet:
2462 proc_net_remove("ptype");
2463 out_dev2:
2464 proc_net_remove("softnet_stat");
2465 out_dev:
2466 proc_net_remove("dev");
2467 goto out;
2469 #else
2470 #define dev_proc_init() 0
2471 #endif /* CONFIG_PROC_FS */
2475 * netdev_set_master - set up master/slave pair
2476 * @slave: slave device
2477 * @master: new master device
2479 * Changes the master device of the slave. Pass %NULL to break the
2480 * bonding. The caller must hold the RTNL semaphore. On a failure
2481 * a negative errno code is returned. On success the reference counts
2482 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2483 * function returns zero.
2485 int netdev_set_master(struct net_device *slave, struct net_device *master)
2487 struct net_device *old = slave->master;
2489 ASSERT_RTNL();
2491 if (master) {
2492 if (old)
2493 return -EBUSY;
2494 dev_hold(master);
2497 slave->master = master;
2499 synchronize_net();
2501 if (old)
2502 dev_put(old);
2504 if (master)
2505 slave->flags |= IFF_SLAVE;
2506 else
2507 slave->flags &= ~IFF_SLAVE;
2509 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2510 return 0;
2514 * dev_set_promiscuity - update promiscuity count on a device
2515 * @dev: device
2516 * @inc: modifier
2518 * Add or remove promiscuity from a device. While the count in the device
2519 * remains above zero the interface remains promiscuous. Once it hits zero
2520 * the device reverts back to normal filtering operation. A negative inc
2521 * value is used to drop promiscuity on the device.
2523 void dev_set_promiscuity(struct net_device *dev, int inc)
2525 unsigned short old_flags = dev->flags;
2527 if ((dev->promiscuity += inc) == 0)
2528 dev->flags &= ~IFF_PROMISC;
2529 else
2530 dev->flags |= IFF_PROMISC;
2531 if (dev->flags != old_flags) {
2532 dev_mc_upload(dev);
2533 printk(KERN_INFO "device %s %s promiscuous mode\n",
2534 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2535 "left");
2536 audit_log(current->audit_context, GFP_ATOMIC,
2537 AUDIT_ANOM_PROMISCUOUS,
2538 "dev=%s prom=%d old_prom=%d auid=%u",
2539 dev->name, (dev->flags & IFF_PROMISC),
2540 (old_flags & IFF_PROMISC),
2541 audit_get_loginuid(current->audit_context));
2546 * dev_set_allmulti - update allmulti count on a device
2547 * @dev: device
2548 * @inc: modifier
2550 * Add or remove reception of all multicast frames to a device. While the
2551 * count in the device remains above zero the interface remains listening
2552 * to all interfaces. Once it hits zero the device reverts back to normal
2553 * filtering operation. A negative @inc value is used to drop the counter
2554 * when releasing a resource needing all multicasts.
2557 void dev_set_allmulti(struct net_device *dev, int inc)
2559 unsigned short old_flags = dev->flags;
2561 dev->flags |= IFF_ALLMULTI;
2562 if ((dev->allmulti += inc) == 0)
2563 dev->flags &= ~IFF_ALLMULTI;
2564 if (dev->flags ^ old_flags)
2565 dev_mc_upload(dev);
2568 unsigned dev_get_flags(const struct net_device *dev)
2570 unsigned flags;
2572 flags = (dev->flags & ~(IFF_PROMISC |
2573 IFF_ALLMULTI |
2574 IFF_RUNNING |
2575 IFF_LOWER_UP |
2576 IFF_DORMANT)) |
2577 (dev->gflags & (IFF_PROMISC |
2578 IFF_ALLMULTI));
2580 if (netif_running(dev)) {
2581 if (netif_oper_up(dev))
2582 flags |= IFF_RUNNING;
2583 if (netif_carrier_ok(dev))
2584 flags |= IFF_LOWER_UP;
2585 if (netif_dormant(dev))
2586 flags |= IFF_DORMANT;
2589 return flags;
2592 int dev_change_flags(struct net_device *dev, unsigned flags)
2594 int ret, changes;
2595 int old_flags = dev->flags;
2598 * Set the flags on our device.
2601 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2602 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2603 IFF_AUTOMEDIA)) |
2604 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2605 IFF_ALLMULTI));
2608 * Load in the correct multicast list now the flags have changed.
2611 dev_mc_upload(dev);
2614 * Have we downed the interface. We handle IFF_UP ourselves
2615 * according to user attempts to set it, rather than blindly
2616 * setting it.
2619 ret = 0;
2620 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2621 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2623 if (!ret)
2624 dev_mc_upload(dev);
2627 if (dev->flags & IFF_UP &&
2628 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2629 IFF_VOLATILE)))
2630 raw_notifier_call_chain(&netdev_chain,
2631 NETDEV_CHANGE, dev);
2633 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2634 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2635 dev->gflags ^= IFF_PROMISC;
2636 dev_set_promiscuity(dev, inc);
2639 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2640 is important. Some (broken) drivers set IFF_PROMISC, when
2641 IFF_ALLMULTI is requested not asking us and not reporting.
2643 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2644 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2645 dev->gflags ^= IFF_ALLMULTI;
2646 dev_set_allmulti(dev, inc);
2649 /* Exclude state transition flags, already notified */
2650 changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
2651 if (changes)
2652 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
2654 return ret;
2657 int dev_set_mtu(struct net_device *dev, int new_mtu)
2659 int err;
2661 if (new_mtu == dev->mtu)
2662 return 0;
2664 /* MTU must be positive. */
2665 if (new_mtu < 0)
2666 return -EINVAL;
2668 if (!netif_device_present(dev))
2669 return -ENODEV;
2671 err = 0;
2672 if (dev->change_mtu)
2673 err = dev->change_mtu(dev, new_mtu);
2674 else
2675 dev->mtu = new_mtu;
2676 if (!err && dev->flags & IFF_UP)
2677 raw_notifier_call_chain(&netdev_chain,
2678 NETDEV_CHANGEMTU, dev);
2679 return err;
2682 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2684 int err;
2686 if (!dev->set_mac_address)
2687 return -EOPNOTSUPP;
2688 if (sa->sa_family != dev->type)
2689 return -EINVAL;
2690 if (!netif_device_present(dev))
2691 return -ENODEV;
2692 err = dev->set_mac_address(dev, sa);
2693 if (!err)
2694 raw_notifier_call_chain(&netdev_chain,
2695 NETDEV_CHANGEADDR, dev);
2696 return err;
2700 * Perform the SIOCxIFxxx calls.
2702 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2704 int err;
2705 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2707 if (!dev)
2708 return -ENODEV;
2710 switch (cmd) {
2711 case SIOCGIFFLAGS: /* Get interface flags */
2712 ifr->ifr_flags = dev_get_flags(dev);
2713 return 0;
2715 case SIOCSIFFLAGS: /* Set interface flags */
2716 return dev_change_flags(dev, ifr->ifr_flags);
2718 case SIOCGIFMETRIC: /* Get the metric on the interface
2719 (currently unused) */
2720 ifr->ifr_metric = 0;
2721 return 0;
2723 case SIOCSIFMETRIC: /* Set the metric on the interface
2724 (currently unused) */
2725 return -EOPNOTSUPP;
2727 case SIOCGIFMTU: /* Get the MTU of a device */
2728 ifr->ifr_mtu = dev->mtu;
2729 return 0;
2731 case SIOCSIFMTU: /* Set the MTU of a device */
2732 return dev_set_mtu(dev, ifr->ifr_mtu);
2734 case SIOCGIFHWADDR:
2735 if (!dev->addr_len)
2736 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2737 else
2738 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2739 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2740 ifr->ifr_hwaddr.sa_family = dev->type;
2741 return 0;
2743 case SIOCSIFHWADDR:
2744 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2746 case SIOCSIFHWBROADCAST:
2747 if (ifr->ifr_hwaddr.sa_family != dev->type)
2748 return -EINVAL;
2749 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2750 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2751 raw_notifier_call_chain(&netdev_chain,
2752 NETDEV_CHANGEADDR, dev);
2753 return 0;
2755 case SIOCGIFMAP:
2756 ifr->ifr_map.mem_start = dev->mem_start;
2757 ifr->ifr_map.mem_end = dev->mem_end;
2758 ifr->ifr_map.base_addr = dev->base_addr;
2759 ifr->ifr_map.irq = dev->irq;
2760 ifr->ifr_map.dma = dev->dma;
2761 ifr->ifr_map.port = dev->if_port;
2762 return 0;
2764 case SIOCSIFMAP:
2765 if (dev->set_config) {
2766 if (!netif_device_present(dev))
2767 return -ENODEV;
2768 return dev->set_config(dev, &ifr->ifr_map);
2770 return -EOPNOTSUPP;
2772 case SIOCADDMULTI:
2773 if (!dev->set_multicast_list ||
2774 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2775 return -EINVAL;
2776 if (!netif_device_present(dev))
2777 return -ENODEV;
2778 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2779 dev->addr_len, 1);
2781 case SIOCDELMULTI:
2782 if (!dev->set_multicast_list ||
2783 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2784 return -EINVAL;
2785 if (!netif_device_present(dev))
2786 return -ENODEV;
2787 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2788 dev->addr_len, 1);
2790 case SIOCGIFINDEX:
2791 ifr->ifr_ifindex = dev->ifindex;
2792 return 0;
2794 case SIOCGIFTXQLEN:
2795 ifr->ifr_qlen = dev->tx_queue_len;
2796 return 0;
2798 case SIOCSIFTXQLEN:
2799 if (ifr->ifr_qlen < 0)
2800 return -EINVAL;
2801 dev->tx_queue_len = ifr->ifr_qlen;
2802 return 0;
2804 case SIOCSIFNAME:
2805 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2806 return dev_change_name(dev, ifr->ifr_newname);
2809 * Unknown or private ioctl
2812 default:
2813 if ((cmd >= SIOCDEVPRIVATE &&
2814 cmd <= SIOCDEVPRIVATE + 15) ||
2815 cmd == SIOCBONDENSLAVE ||
2816 cmd == SIOCBONDRELEASE ||
2817 cmd == SIOCBONDSETHWADDR ||
2818 cmd == SIOCBONDSLAVEINFOQUERY ||
2819 cmd == SIOCBONDINFOQUERY ||
2820 cmd == SIOCBONDCHANGEACTIVE ||
2821 cmd == SIOCGMIIPHY ||
2822 cmd == SIOCGMIIREG ||
2823 cmd == SIOCSMIIREG ||
2824 cmd == SIOCBRADDIF ||
2825 cmd == SIOCBRDELIF ||
2826 cmd == SIOCWANDEV) {
2827 err = -EOPNOTSUPP;
2828 if (dev->do_ioctl) {
2829 if (netif_device_present(dev))
2830 err = dev->do_ioctl(dev, ifr,
2831 cmd);
2832 else
2833 err = -ENODEV;
2835 } else
2836 err = -EINVAL;
2839 return err;
2843 * This function handles all "interface"-type I/O control requests. The actual
2844 * 'doing' part of this is dev_ifsioc above.
2848 * dev_ioctl - network device ioctl
2849 * @cmd: command to issue
2850 * @arg: pointer to a struct ifreq in user space
2852 * Issue ioctl functions to devices. This is normally called by the
2853 * user space syscall interfaces but can sometimes be useful for
2854 * other purposes. The return value is the return from the syscall if
2855 * positive or a negative errno code on error.
2858 int dev_ioctl(unsigned int cmd, void __user *arg)
2860 struct ifreq ifr;
2861 int ret;
2862 char *colon;
2864 /* One special case: SIOCGIFCONF takes ifconf argument
2865 and requires shared lock, because it sleeps writing
2866 to user space.
2869 if (cmd == SIOCGIFCONF) {
2870 rtnl_lock();
2871 ret = dev_ifconf((char __user *) arg);
2872 rtnl_unlock();
2873 return ret;
2875 if (cmd == SIOCGIFNAME)
2876 return dev_ifname((struct ifreq __user *)arg);
2878 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2879 return -EFAULT;
2881 ifr.ifr_name[IFNAMSIZ-1] = 0;
2883 colon = strchr(ifr.ifr_name, ':');
2884 if (colon)
2885 *colon = 0;
2888 * See which interface the caller is talking about.
2891 switch (cmd) {
2893 * These ioctl calls:
2894 * - can be done by all.
2895 * - atomic and do not require locking.
2896 * - return a value
2898 case SIOCGIFFLAGS:
2899 case SIOCGIFMETRIC:
2900 case SIOCGIFMTU:
2901 case SIOCGIFHWADDR:
2902 case SIOCGIFSLAVE:
2903 case SIOCGIFMAP:
2904 case SIOCGIFINDEX:
2905 case SIOCGIFTXQLEN:
2906 dev_load(ifr.ifr_name);
2907 read_lock(&dev_base_lock);
2908 ret = dev_ifsioc(&ifr, cmd);
2909 read_unlock(&dev_base_lock);
2910 if (!ret) {
2911 if (colon)
2912 *colon = ':';
2913 if (copy_to_user(arg, &ifr,
2914 sizeof(struct ifreq)))
2915 ret = -EFAULT;
2917 return ret;
2919 case SIOCETHTOOL:
2920 dev_load(ifr.ifr_name);
2921 rtnl_lock();
2922 ret = dev_ethtool(&ifr);
2923 rtnl_unlock();
2924 if (!ret) {
2925 if (colon)
2926 *colon = ':';
2927 if (copy_to_user(arg, &ifr,
2928 sizeof(struct ifreq)))
2929 ret = -EFAULT;
2931 return ret;
2934 * These ioctl calls:
2935 * - require superuser power.
2936 * - require strict serialization.
2937 * - return a value
2939 case SIOCGMIIPHY:
2940 case SIOCGMIIREG:
2941 case SIOCSIFNAME:
2942 if (!capable(CAP_NET_ADMIN))
2943 return -EPERM;
2944 dev_load(ifr.ifr_name);
2945 rtnl_lock();
2946 ret = dev_ifsioc(&ifr, cmd);
2947 rtnl_unlock();
2948 if (!ret) {
2949 if (colon)
2950 *colon = ':';
2951 if (copy_to_user(arg, &ifr,
2952 sizeof(struct ifreq)))
2953 ret = -EFAULT;
2955 return ret;
2958 * These ioctl calls:
2959 * - require superuser power.
2960 * - require strict serialization.
2961 * - do not return a value
2963 case SIOCSIFFLAGS:
2964 case SIOCSIFMETRIC:
2965 case SIOCSIFMTU:
2966 case SIOCSIFMAP:
2967 case SIOCSIFHWADDR:
2968 case SIOCSIFSLAVE:
2969 case SIOCADDMULTI:
2970 case SIOCDELMULTI:
2971 case SIOCSIFHWBROADCAST:
2972 case SIOCSIFTXQLEN:
2973 case SIOCSMIIREG:
2974 case SIOCBONDENSLAVE:
2975 case SIOCBONDRELEASE:
2976 case SIOCBONDSETHWADDR:
2977 case SIOCBONDCHANGEACTIVE:
2978 case SIOCBRADDIF:
2979 case SIOCBRDELIF:
2980 if (!capable(CAP_NET_ADMIN))
2981 return -EPERM;
2982 /* fall through */
2983 case SIOCBONDSLAVEINFOQUERY:
2984 case SIOCBONDINFOQUERY:
2985 dev_load(ifr.ifr_name);
2986 rtnl_lock();
2987 ret = dev_ifsioc(&ifr, cmd);
2988 rtnl_unlock();
2989 return ret;
2991 case SIOCGIFMEM:
2992 /* Get the per device memory space. We can add this but
2993 * currently do not support it */
2994 case SIOCSIFMEM:
2995 /* Set the per device memory buffer space.
2996 * Not applicable in our case */
2997 case SIOCSIFLINK:
2998 return -EINVAL;
3001 * Unknown or private ioctl.
3003 default:
3004 if (cmd == SIOCWANDEV ||
3005 (cmd >= SIOCDEVPRIVATE &&
3006 cmd <= SIOCDEVPRIVATE + 15)) {
3007 dev_load(ifr.ifr_name);
3008 rtnl_lock();
3009 ret = dev_ifsioc(&ifr, cmd);
3010 rtnl_unlock();
3011 if (!ret && copy_to_user(arg, &ifr,
3012 sizeof(struct ifreq)))
3013 ret = -EFAULT;
3014 return ret;
3016 /* Take care of Wireless Extensions */
3017 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
3018 return wext_handle_ioctl(&ifr, cmd, arg);
3019 return -EINVAL;
3025 * dev_new_index - allocate an ifindex
3027 * Returns a suitable unique value for a new device interface
3028 * number. The caller must hold the rtnl semaphore or the
3029 * dev_base_lock to be sure it remains unique.
3031 static int dev_new_index(void)
3033 static int ifindex;
3034 for (;;) {
3035 if (++ifindex <= 0)
3036 ifindex = 1;
3037 if (!__dev_get_by_index(ifindex))
3038 return ifindex;
3042 static int dev_boot_phase = 1;
3044 /* Delayed registration/unregisteration */
3045 static DEFINE_SPINLOCK(net_todo_list_lock);
3046 static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
3048 static void net_set_todo(struct net_device *dev)
3050 spin_lock(&net_todo_list_lock);
3051 list_add_tail(&dev->todo_list, &net_todo_list);
3052 spin_unlock(&net_todo_list_lock);
3056 * register_netdevice - register a network device
3057 * @dev: device to register
3059 * Take a completed network device structure and add it to the kernel
3060 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3061 * chain. 0 is returned on success. A negative errno code is returned
3062 * on a failure to set up the device, or if the name is a duplicate.
3064 * Callers must hold the rtnl semaphore. You may want
3065 * register_netdev() instead of this.
3067 * BUGS:
3068 * The locking appears insufficient to guarantee two parallel registers
3069 * will not get the same name.
3072 int register_netdevice(struct net_device *dev)
3074 struct hlist_head *head;
3075 struct hlist_node *p;
3076 int ret;
3078 BUG_ON(dev_boot_phase);
3079 ASSERT_RTNL();
3081 might_sleep();
3083 /* When net_device's are persistent, this will be fatal. */
3084 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
3086 spin_lock_init(&dev->queue_lock);
3087 spin_lock_init(&dev->_xmit_lock);
3088 netdev_set_lockdep_class(&dev->_xmit_lock, dev->type);
3089 dev->xmit_lock_owner = -1;
3090 spin_lock_init(&dev->ingress_lock);
3092 dev->iflink = -1;
3094 /* Init, if this function is available */
3095 if (dev->init) {
3096 ret = dev->init(dev);
3097 if (ret) {
3098 if (ret > 0)
3099 ret = -EIO;
3100 goto out;
3104 if (!dev_valid_name(dev->name)) {
3105 ret = -EINVAL;
3106 goto out;
3109 dev->ifindex = dev_new_index();
3110 if (dev->iflink == -1)
3111 dev->iflink = dev->ifindex;
3113 /* Check for existence of name */
3114 head = dev_name_hash(dev->name);
3115 hlist_for_each(p, head) {
3116 struct net_device *d
3117 = hlist_entry(p, struct net_device, name_hlist);
3118 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3119 ret = -EEXIST;
3120 goto out;
3124 /* Fix illegal SG+CSUM combinations. */
3125 if ((dev->features & NETIF_F_SG) &&
3126 !(dev->features & NETIF_F_ALL_CSUM)) {
3127 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
3128 dev->name);
3129 dev->features &= ~NETIF_F_SG;
3132 /* TSO requires that SG is present as well. */
3133 if ((dev->features & NETIF_F_TSO) &&
3134 !(dev->features & NETIF_F_SG)) {
3135 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
3136 dev->name);
3137 dev->features &= ~NETIF_F_TSO;
3139 if (dev->features & NETIF_F_UFO) {
3140 if (!(dev->features & NETIF_F_HW_CSUM)) {
3141 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3142 "NETIF_F_HW_CSUM feature.\n",
3143 dev->name);
3144 dev->features &= ~NETIF_F_UFO;
3146 if (!(dev->features & NETIF_F_SG)) {
3147 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3148 "NETIF_F_SG feature.\n",
3149 dev->name);
3150 dev->features &= ~NETIF_F_UFO;
3155 * nil rebuild_header routine,
3156 * that should be never called and used as just bug trap.
3159 if (!dev->rebuild_header)
3160 dev->rebuild_header = default_rebuild_header;
3162 ret = netdev_register_sysfs(dev);
3163 if (ret)
3164 goto out;
3165 dev->reg_state = NETREG_REGISTERED;
3168 * Default initial state at registry is that the
3169 * device is present.
3172 set_bit(__LINK_STATE_PRESENT, &dev->state);
3174 dev_init_scheduler(dev);
3175 write_lock_bh(&dev_base_lock);
3176 list_add_tail(&dev->dev_list, &dev_base_head);
3177 hlist_add_head(&dev->name_hlist, head);
3178 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
3179 dev_hold(dev);
3180 write_unlock_bh(&dev_base_lock);
3182 /* Notify protocols, that a new device appeared. */
3183 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
3185 ret = 0;
3187 out:
3188 return ret;
3192 * register_netdev - register a network device
3193 * @dev: device to register
3195 * Take a completed network device structure and add it to the kernel
3196 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3197 * chain. 0 is returned on success. A negative errno code is returned
3198 * on a failure to set up the device, or if the name is a duplicate.
3200 * This is a wrapper around register_netdevice that takes the rtnl semaphore
3201 * and expands the device name if you passed a format string to
3202 * alloc_netdev.
3204 int register_netdev(struct net_device *dev)
3206 int err;
3208 rtnl_lock();
3211 * If the name is a format string the caller wants us to do a
3212 * name allocation.
3214 if (strchr(dev->name, '%')) {
3215 err = dev_alloc_name(dev, dev->name);
3216 if (err < 0)
3217 goto out;
3220 err = register_netdevice(dev);
3221 out:
3222 rtnl_unlock();
3223 return err;
3225 EXPORT_SYMBOL(register_netdev);
3228 * netdev_wait_allrefs - wait until all references are gone.
3230 * This is called when unregistering network devices.
3232 * Any protocol or device that holds a reference should register
3233 * for netdevice notification, and cleanup and put back the
3234 * reference if they receive an UNREGISTER event.
3235 * We can get stuck here if buggy protocols don't correctly
3236 * call dev_put.
3238 static void netdev_wait_allrefs(struct net_device *dev)
3240 unsigned long rebroadcast_time, warning_time;
3242 rebroadcast_time = warning_time = jiffies;
3243 while (atomic_read(&dev->refcnt) != 0) {
3244 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
3245 rtnl_lock();
3247 /* Rebroadcast unregister notification */
3248 raw_notifier_call_chain(&netdev_chain,
3249 NETDEV_UNREGISTER, dev);
3251 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3252 &dev->state)) {
3253 /* We must not have linkwatch events
3254 * pending on unregister. If this
3255 * happens, we simply run the queue
3256 * unscheduled, resulting in a noop
3257 * for this device.
3259 linkwatch_run_queue();
3262 __rtnl_unlock();
3264 rebroadcast_time = jiffies;
3267 msleep(250);
3269 if (time_after(jiffies, warning_time + 10 * HZ)) {
3270 printk(KERN_EMERG "unregister_netdevice: "
3271 "waiting for %s to become free. Usage "
3272 "count = %d\n",
3273 dev->name, atomic_read(&dev->refcnt));
3274 warning_time = jiffies;
3279 /* The sequence is:
3281 * rtnl_lock();
3282 * ...
3283 * register_netdevice(x1);
3284 * register_netdevice(x2);
3285 * ...
3286 * unregister_netdevice(y1);
3287 * unregister_netdevice(y2);
3288 * ...
3289 * rtnl_unlock();
3290 * free_netdev(y1);
3291 * free_netdev(y2);
3293 * We are invoked by rtnl_unlock() after it drops the semaphore.
3294 * This allows us to deal with problems:
3295 * 1) We can delete sysfs objects which invoke hotplug
3296 * without deadlocking with linkwatch via keventd.
3297 * 2) Since we run with the RTNL semaphore not held, we can sleep
3298 * safely in order to wait for the netdev refcnt to drop to zero.
3300 static DEFINE_MUTEX(net_todo_run_mutex);
3301 void netdev_run_todo(void)
3303 struct list_head list;
3305 /* Need to guard against multiple cpu's getting out of order. */
3306 mutex_lock(&net_todo_run_mutex);
3308 /* Not safe to do outside the semaphore. We must not return
3309 * until all unregister events invoked by the local processor
3310 * have been completed (either by this todo run, or one on
3311 * another cpu).
3313 if (list_empty(&net_todo_list))
3314 goto out;
3316 /* Snapshot list, allow later requests */
3317 spin_lock(&net_todo_list_lock);
3318 list_replace_init(&net_todo_list, &list);
3319 spin_unlock(&net_todo_list_lock);
3321 while (!list_empty(&list)) {
3322 struct net_device *dev
3323 = list_entry(list.next, struct net_device, todo_list);
3324 list_del(&dev->todo_list);
3326 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
3327 printk(KERN_ERR "network todo '%s' but state %d\n",
3328 dev->name, dev->reg_state);
3329 dump_stack();
3330 continue;
3333 dev->reg_state = NETREG_UNREGISTERED;
3335 netdev_wait_allrefs(dev);
3337 /* paranoia */
3338 BUG_ON(atomic_read(&dev->refcnt));
3339 BUG_TRAP(!dev->ip_ptr);
3340 BUG_TRAP(!dev->ip6_ptr);
3341 BUG_TRAP(!dev->dn_ptr);
3343 if (dev->destructor)
3344 dev->destructor(dev);
3346 /* Free network device */
3347 kobject_put(&dev->dev.kobj);
3350 out:
3351 mutex_unlock(&net_todo_run_mutex);
3354 static struct net_device_stats *internal_stats(struct net_device *dev)
3356 return &dev->stats;
3360 * alloc_netdev - allocate network device
3361 * @sizeof_priv: size of private data to allocate space for
3362 * @name: device name format string
3363 * @setup: callback to initialize device
3365 * Allocates a struct net_device with private data area for driver use
3366 * and performs basic initialization.
3368 struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3369 void (*setup)(struct net_device *))
3371 void *p;
3372 struct net_device *dev;
3373 int alloc_size;
3375 BUG_ON(strlen(name) >= sizeof(dev->name));
3377 /* ensure 32-byte alignment of both the device and private area */
3378 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3379 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3381 p = kzalloc(alloc_size, GFP_KERNEL);
3382 if (!p) {
3383 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
3384 return NULL;
3387 dev = (struct net_device *)
3388 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3389 dev->padded = (char *)dev - (char *)p;
3391 if (sizeof_priv)
3392 dev->priv = netdev_priv(dev);
3394 dev->get_stats = internal_stats;
3395 setup(dev);
3396 strcpy(dev->name, name);
3397 return dev;
3399 EXPORT_SYMBOL(alloc_netdev);
3402 * free_netdev - free network device
3403 * @dev: device
3405 * This function does the last stage of destroying an allocated device
3406 * interface. The reference to the device object is released.
3407 * If this is the last reference then it will be freed.
3409 void free_netdev(struct net_device *dev)
3411 #ifdef CONFIG_SYSFS
3412 /* Compatibility with error handling in drivers */
3413 if (dev->reg_state == NETREG_UNINITIALIZED) {
3414 kfree((char *)dev - dev->padded);
3415 return;
3418 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3419 dev->reg_state = NETREG_RELEASED;
3421 /* will free via device release */
3422 put_device(&dev->dev);
3423 #else
3424 kfree((char *)dev - dev->padded);
3425 #endif
3428 /* Synchronize with packet receive processing. */
3429 void synchronize_net(void)
3431 might_sleep();
3432 synchronize_rcu();
3436 * unregister_netdevice - remove device from the kernel
3437 * @dev: device
3439 * This function shuts down a device interface and removes it
3440 * from the kernel tables. On success 0 is returned, on a failure
3441 * a negative errno code is returned.
3443 * Callers must hold the rtnl semaphore. You may want
3444 * unregister_netdev() instead of this.
3447 void unregister_netdevice(struct net_device *dev)
3449 BUG_ON(dev_boot_phase);
3450 ASSERT_RTNL();
3452 /* Some devices call without registering for initialization unwind. */
3453 if (dev->reg_state == NETREG_UNINITIALIZED) {
3454 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3455 "was registered\n", dev->name, dev);
3457 WARN_ON(1);
3458 return;
3461 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3463 /* If device is running, close it first. */
3464 if (dev->flags & IFF_UP)
3465 dev_close(dev);
3467 /* And unlink it from device chain. */
3468 write_lock_bh(&dev_base_lock);
3469 list_del(&dev->dev_list);
3470 hlist_del(&dev->name_hlist);
3471 hlist_del(&dev->index_hlist);
3472 write_unlock_bh(&dev_base_lock);
3474 dev->reg_state = NETREG_UNREGISTERING;
3476 synchronize_net();
3478 /* Shutdown queueing discipline. */
3479 dev_shutdown(dev);
3482 /* Notify protocols, that we are about to destroy
3483 this device. They should clean all the things.
3485 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3488 * Flush the multicast chain
3490 dev_mc_discard(dev);
3492 if (dev->uninit)
3493 dev->uninit(dev);
3495 /* Notifier chain MUST detach us from master device. */
3496 BUG_TRAP(!dev->master);
3498 /* Remove entries from sysfs */
3499 netdev_unregister_sysfs(dev);
3501 /* Finish processing unregister after unlock */
3502 net_set_todo(dev);
3504 synchronize_net();
3506 dev_put(dev);
3510 * unregister_netdev - remove device from the kernel
3511 * @dev: device
3513 * This function shuts down a device interface and removes it
3514 * from the kernel tables. On success 0 is returned, on a failure
3515 * a negative errno code is returned.
3517 * This is just a wrapper for unregister_netdevice that takes
3518 * the rtnl semaphore. In general you want to use this and not
3519 * unregister_netdevice.
3521 void unregister_netdev(struct net_device *dev)
3523 rtnl_lock();
3524 unregister_netdevice(dev);
3525 rtnl_unlock();
3528 EXPORT_SYMBOL(unregister_netdev);
3530 static int dev_cpu_callback(struct notifier_block *nfb,
3531 unsigned long action,
3532 void *ocpu)
3534 struct sk_buff **list_skb;
3535 struct net_device **list_net;
3536 struct sk_buff *skb;
3537 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3538 struct softnet_data *sd, *oldsd;
3540 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
3541 return NOTIFY_OK;
3543 local_irq_disable();
3544 cpu = smp_processor_id();
3545 sd = &per_cpu(softnet_data, cpu);
3546 oldsd = &per_cpu(softnet_data, oldcpu);
3548 /* Find end of our completion_queue. */
3549 list_skb = &sd->completion_queue;
3550 while (*list_skb)
3551 list_skb = &(*list_skb)->next;
3552 /* Append completion queue from offline CPU. */
3553 *list_skb = oldsd->completion_queue;
3554 oldsd->completion_queue = NULL;
3556 /* Find end of our output_queue. */
3557 list_net = &sd->output_queue;
3558 while (*list_net)
3559 list_net = &(*list_net)->next_sched;
3560 /* Append output queue from offline CPU. */
3561 *list_net = oldsd->output_queue;
3562 oldsd->output_queue = NULL;
3564 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3565 local_irq_enable();
3567 /* Process offline CPU's input_pkt_queue */
3568 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3569 netif_rx(skb);
3571 return NOTIFY_OK;
3574 #ifdef CONFIG_NET_DMA
3576 * net_dma_rebalance -
3577 * This is called when the number of channels allocated to the net_dma_client
3578 * changes. The net_dma_client tries to have one DMA channel per CPU.
3581 static void net_dma_rebalance(struct net_dma *net_dma)
3583 unsigned int cpu, i, n, chan_idx;
3584 struct dma_chan *chan;
3586 if (cpus_empty(net_dma->channel_mask)) {
3587 for_each_online_cpu(cpu)
3588 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
3589 return;
3592 i = 0;
3593 cpu = first_cpu(cpu_online_map);
3595 for_each_cpu_mask(chan_idx, net_dma->channel_mask) {
3596 chan = net_dma->channels[chan_idx];
3598 n = ((num_online_cpus() / cpus_weight(net_dma->channel_mask))
3599 + (i < (num_online_cpus() %
3600 cpus_weight(net_dma->channel_mask)) ? 1 : 0));
3602 while(n) {
3603 per_cpu(softnet_data, cpu).net_dma = chan;
3604 cpu = next_cpu(cpu, cpu_online_map);
3605 n--;
3607 i++;
3612 * netdev_dma_event - event callback for the net_dma_client
3613 * @client: should always be net_dma_client
3614 * @chan: DMA channel for the event
3615 * @event: event type
3617 static enum dma_state_client
3618 netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3619 enum dma_state state)
3621 int i, found = 0, pos = -1;
3622 struct net_dma *net_dma =
3623 container_of(client, struct net_dma, client);
3624 enum dma_state_client ack = DMA_DUP; /* default: take no action */
3626 spin_lock(&net_dma->lock);
3627 switch (state) {
3628 case DMA_RESOURCE_AVAILABLE:
3629 for (i = 0; i < NR_CPUS; i++)
3630 if (net_dma->channels[i] == chan) {
3631 found = 1;
3632 break;
3633 } else if (net_dma->channels[i] == NULL && pos < 0)
3634 pos = i;
3636 if (!found && pos >= 0) {
3637 ack = DMA_ACK;
3638 net_dma->channels[pos] = chan;
3639 cpu_set(pos, net_dma->channel_mask);
3640 net_dma_rebalance(net_dma);
3642 break;
3643 case DMA_RESOURCE_REMOVED:
3644 for (i = 0; i < NR_CPUS; i++)
3645 if (net_dma->channels[i] == chan) {
3646 found = 1;
3647 pos = i;
3648 break;
3651 if (found) {
3652 ack = DMA_ACK;
3653 cpu_clear(pos, net_dma->channel_mask);
3654 net_dma->channels[i] = NULL;
3655 net_dma_rebalance(net_dma);
3657 break;
3658 default:
3659 break;
3661 spin_unlock(&net_dma->lock);
3663 return ack;
3667 * netdev_dma_regiser - register the networking subsystem as a DMA client
3669 static int __init netdev_dma_register(void)
3671 spin_lock_init(&net_dma.lock);
3672 dma_cap_set(DMA_MEMCPY, net_dma.client.cap_mask);
3673 dma_async_client_register(&net_dma.client);
3674 dma_async_client_chan_request(&net_dma.client);
3675 return 0;
3678 #else
3679 static int __init netdev_dma_register(void) { return -ENODEV; }
3680 #endif /* CONFIG_NET_DMA */
3683 * Initialize the DEV module. At boot time this walks the device list and
3684 * unhooks any devices that fail to initialise (normally hardware not
3685 * present) and leaves us with a valid list of present and active devices.
3690 * This is called single threaded during boot, so no need
3691 * to take the rtnl semaphore.
3693 static int __init net_dev_init(void)
3695 int i, rc = -ENOMEM;
3697 BUG_ON(!dev_boot_phase);
3699 if (dev_proc_init())
3700 goto out;
3702 if (netdev_sysfs_init())
3703 goto out;
3705 INIT_LIST_HEAD(&ptype_all);
3706 for (i = 0; i < 16; i++)
3707 INIT_LIST_HEAD(&ptype_base[i]);
3709 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3710 INIT_HLIST_HEAD(&dev_name_head[i]);
3712 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3713 INIT_HLIST_HEAD(&dev_index_head[i]);
3716 * Initialise the packet receive queues.
3719 for_each_possible_cpu(i) {
3720 struct softnet_data *queue;
3722 queue = &per_cpu(softnet_data, i);
3723 skb_queue_head_init(&queue->input_pkt_queue);
3724 queue->completion_queue = NULL;
3725 INIT_LIST_HEAD(&queue->poll_list);
3726 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3727 queue->backlog_dev.weight = weight_p;
3728 queue->backlog_dev.poll = process_backlog;
3729 atomic_set(&queue->backlog_dev.refcnt, 1);
3732 netdev_dma_register();
3734 dev_boot_phase = 0;
3736 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3737 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3739 hotcpu_notifier(dev_cpu_callback, 0);
3740 dst_init();
3741 dev_mcast_init();
3742 rc = 0;
3743 out:
3744 return rc;
3747 subsys_initcall(net_dev_init);
3749 EXPORT_SYMBOL(__dev_get_by_index);
3750 EXPORT_SYMBOL(__dev_get_by_name);
3751 EXPORT_SYMBOL(__dev_remove_pack);
3752 EXPORT_SYMBOL(dev_valid_name);
3753 EXPORT_SYMBOL(dev_add_pack);
3754 EXPORT_SYMBOL(dev_alloc_name);
3755 EXPORT_SYMBOL(dev_close);
3756 EXPORT_SYMBOL(dev_get_by_flags);
3757 EXPORT_SYMBOL(dev_get_by_index);
3758 EXPORT_SYMBOL(dev_get_by_name);
3759 EXPORT_SYMBOL(dev_open);
3760 EXPORT_SYMBOL(dev_queue_xmit);
3761 EXPORT_SYMBOL(dev_remove_pack);
3762 EXPORT_SYMBOL(dev_set_allmulti);
3763 EXPORT_SYMBOL(dev_set_promiscuity);
3764 EXPORT_SYMBOL(dev_change_flags);
3765 EXPORT_SYMBOL(dev_set_mtu);
3766 EXPORT_SYMBOL(dev_set_mac_address);
3767 EXPORT_SYMBOL(free_netdev);
3768 EXPORT_SYMBOL(netdev_boot_setup_check);
3769 EXPORT_SYMBOL(netdev_set_master);
3770 EXPORT_SYMBOL(netdev_state_change);
3771 EXPORT_SYMBOL(netif_receive_skb);
3772 EXPORT_SYMBOL(netif_rx);
3773 EXPORT_SYMBOL(register_gifconf);
3774 EXPORT_SYMBOL(register_netdevice);
3775 EXPORT_SYMBOL(register_netdevice_notifier);
3776 EXPORT_SYMBOL(skb_checksum_help);
3777 EXPORT_SYMBOL(synchronize_net);
3778 EXPORT_SYMBOL(unregister_netdevice);
3779 EXPORT_SYMBOL(unregister_netdevice_notifier);
3780 EXPORT_SYMBOL(net_enable_timestamp);
3781 EXPORT_SYMBOL(net_disable_timestamp);
3782 EXPORT_SYMBOL(dev_get_flags);
3784 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3785 EXPORT_SYMBOL(br_handle_frame_hook);
3786 EXPORT_SYMBOL(br_fdb_get_hook);
3787 EXPORT_SYMBOL(br_fdb_put_hook);
3788 #endif
3790 #ifdef CONFIG_KMOD
3791 EXPORT_SYMBOL(dev_load);
3792 #endif
3794 EXPORT_PER_CPU_SYMBOL(softnet_data);