direct-io.hg

view linux-2.6-xen-sparse/net/core/dev.c @ 11443:af50fb41612c

[IA64] Expand memory_op for PV-on-HVM on IPF

Signed-off-by: Tsunehisa Doi <Doi.Tsunehisa@jp.fujitsu.com>
Signed-off-by: Tomonari Horikoshi <t.horikoshi@jp.fujitsu.com>
author awilliam@xenbuild.aw
date Fri Sep 01 08:46:02 2006 -0600 (2006-09-01)
parents 9519445d9e9d
children f3c3f9bd798e
line source
1 /*
2 * NET3 Protocol independent device support routines.
3 *
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.
8 *
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>
13 *
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>
21 *
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
73 */
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/bitops.h>
78 #include <linux/capability.h>
79 #include <linux/config.h>
80 #include <linux/cpu.h>
81 #include <linux/types.h>
82 #include <linux/kernel.h>
83 #include <linux/sched.h>
84 #include <linux/string.h>
85 #include <linux/mm.h>
86 #include <linux/socket.h>
87 #include <linux/sockios.h>
88 #include <linux/errno.h>
89 #include <linux/interrupt.h>
90 #include <linux/if_ether.h>
91 #include <linux/netdevice.h>
92 #include <linux/etherdevice.h>
93 #include <linux/notifier.h>
94 #include <linux/skbuff.h>
95 #include <net/sock.h>
96 #include <linux/rtnetlink.h>
97 #include <linux/proc_fs.h>
98 #include <linux/seq_file.h>
99 #include <linux/stat.h>
100 #include <linux/if_bridge.h>
101 #include <linux/divert.h>
102 #include <net/dst.h>
103 #include <net/pkt_sched.h>
104 #include <net/checksum.h>
105 #include <linux/highmem.h>
106 #include <linux/init.h>
107 #include <linux/kmod.h>
108 #include <linux/module.h>
109 #include <linux/kallsyms.h>
110 #include <linux/netpoll.h>
111 #include <linux/rcupdate.h>
112 #include <linux/delay.h>
113 #ifdef CONFIG_NET_RADIO
114 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
115 #include <net/iw_handler.h>
116 #endif /* CONFIG_NET_RADIO */
117 #include <asm/current.h>
118 #include <linux/err.h>
120 #ifdef CONFIG_XEN
121 #include <net/ip.h>
122 #include <linux/tcp.h>
123 #include <linux/udp.h>
124 #endif
126 /*
127 * The list of packet types we will receive (as opposed to discard)
128 * and the routines to invoke.
129 *
130 * Why 16. Because with 16 the only overlap we get on a hash of the
131 * low nibble of the protocol value is RARP/SNAP/X.25.
132 *
133 * NOTE: That is no longer true with the addition of VLAN tags. Not
134 * sure which should go first, but I bet it won't make much
135 * difference if we are running VLANs. The good news is that
136 * this protocol won't be in the list unless compiled in, so
137 * the average user (w/out VLANs) will not be adversly affected.
138 * --BLG
139 *
140 * 0800 IP
141 * 8100 802.1Q VLAN
142 * 0001 802.3
143 * 0002 AX.25
144 * 0004 802.2
145 * 8035 RARP
146 * 0005 SNAP
147 * 0805 X.25
148 * 0806 ARP
149 * 8137 IPX
150 * 0009 Localtalk
151 * 86DD IPv6
152 */
154 static DEFINE_SPINLOCK(ptype_lock);
155 static struct list_head ptype_base[16]; /* 16 way hashed list */
156 static struct list_head ptype_all; /* Taps */
158 /*
159 * The @dev_base list is protected by @dev_base_lock and the rtln
160 * semaphore.
161 *
162 * Pure readers hold dev_base_lock for reading.
163 *
164 * Writers must hold the rtnl semaphore while they loop through the
165 * dev_base list, and hold dev_base_lock for writing when they do the
166 * actual updates. This allows pure readers to access the list even
167 * while a writer is preparing to update it.
168 *
169 * To put it another way, dev_base_lock is held for writing only to
170 * protect against pure readers; the rtnl semaphore provides the
171 * protection against other writers.
172 *
173 * See, for example usages, register_netdevice() and
174 * unregister_netdevice(), which must be called with the rtnl
175 * semaphore held.
176 */
177 struct net_device *dev_base;
178 static struct net_device **dev_tail = &dev_base;
179 DEFINE_RWLOCK(dev_base_lock);
181 EXPORT_SYMBOL(dev_base);
182 EXPORT_SYMBOL(dev_base_lock);
184 #define NETDEV_HASHBITS 8
185 static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
186 static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
188 static inline struct hlist_head *dev_name_hash(const char *name)
189 {
190 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
191 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
192 }
194 static inline struct hlist_head *dev_index_hash(int ifindex)
195 {
196 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
197 }
199 /*
200 * Our notifier list
201 */
203 static struct notifier_block *netdev_chain;
205 /*
206 * Device drivers call our routines to queue packets here. We empty the
207 * queue in the local softnet handler.
208 */
209 DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL };
211 #ifdef CONFIG_SYSFS
212 extern int netdev_sysfs_init(void);
213 extern int netdev_register_sysfs(struct net_device *);
214 extern void netdev_unregister_sysfs(struct net_device *);
215 #else
216 #define netdev_sysfs_init() (0)
217 #define netdev_register_sysfs(dev) (0)
218 #define netdev_unregister_sysfs(dev) do { } while(0)
219 #endif
222 /*******************************************************************************
224 Protocol management and registration routines
226 *******************************************************************************/
228 /*
229 * For efficiency
230 */
232 int netdev_nit;
234 /*
235 * Add a protocol ID to the list. Now that the input handler is
236 * smarter we can dispense with all the messy stuff that used to be
237 * here.
238 *
239 * BEWARE!!! Protocol handlers, mangling input packets,
240 * MUST BE last in hash buckets and checking protocol handlers
241 * MUST start from promiscuous ptype_all chain in net_bh.
242 * It is true now, do not change it.
243 * Explanation follows: if protocol handler, mangling packet, will
244 * be the first on list, it is not able to sense, that packet
245 * is cloned and should be copied-on-write, so that it will
246 * change it and subsequent readers will get broken packet.
247 * --ANK (980803)
248 */
250 /**
251 * dev_add_pack - add packet handler
252 * @pt: packet type declaration
253 *
254 * Add a protocol handler to the networking stack. The passed &packet_type
255 * is linked into kernel lists and may not be freed until it has been
256 * removed from the kernel lists.
257 *
258 * This call does not sleep therefore it can not
259 * guarantee all CPU's that are in middle of receiving packets
260 * will see the new packet type (until the next received packet).
261 */
263 void dev_add_pack(struct packet_type *pt)
264 {
265 int hash;
267 spin_lock_bh(&ptype_lock);
268 if (pt->type == htons(ETH_P_ALL)) {
269 netdev_nit++;
270 list_add_rcu(&pt->list, &ptype_all);
271 } else {
272 hash = ntohs(pt->type) & 15;
273 list_add_rcu(&pt->list, &ptype_base[hash]);
274 }
275 spin_unlock_bh(&ptype_lock);
276 }
278 /**
279 * __dev_remove_pack - remove packet handler
280 * @pt: packet type declaration
281 *
282 * Remove a protocol handler that was previously added to the kernel
283 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
284 * from the kernel lists and can be freed or reused once this function
285 * returns.
286 *
287 * The packet type might still be in use by receivers
288 * and must not be freed until after all the CPU's have gone
289 * through a quiescent state.
290 */
291 void __dev_remove_pack(struct packet_type *pt)
292 {
293 struct list_head *head;
294 struct packet_type *pt1;
296 spin_lock_bh(&ptype_lock);
298 if (pt->type == htons(ETH_P_ALL)) {
299 netdev_nit--;
300 head = &ptype_all;
301 } else
302 head = &ptype_base[ntohs(pt->type) & 15];
304 list_for_each_entry(pt1, head, list) {
305 if (pt == pt1) {
306 list_del_rcu(&pt->list);
307 goto out;
308 }
309 }
311 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
312 out:
313 spin_unlock_bh(&ptype_lock);
314 }
315 /**
316 * dev_remove_pack - remove packet handler
317 * @pt: packet type declaration
318 *
319 * Remove a protocol handler that was previously added to the kernel
320 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
321 * from the kernel lists and can be freed or reused once this function
322 * returns.
323 *
324 * This call sleeps to guarantee that no CPU is looking at the packet
325 * type after return.
326 */
327 void dev_remove_pack(struct packet_type *pt)
328 {
329 __dev_remove_pack(pt);
331 synchronize_net();
332 }
334 /******************************************************************************
336 Device Boot-time Settings Routines
338 *******************************************************************************/
340 /* Boot time configuration table */
341 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
343 /**
344 * netdev_boot_setup_add - add new setup entry
345 * @name: name of the device
346 * @map: configured settings for the device
347 *
348 * Adds new setup entry to the dev_boot_setup list. The function
349 * returns 0 on error and 1 on success. This is a generic routine to
350 * all netdevices.
351 */
352 static int netdev_boot_setup_add(char *name, struct ifmap *map)
353 {
354 struct netdev_boot_setup *s;
355 int i;
357 s = dev_boot_setup;
358 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
359 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
360 memset(s[i].name, 0, sizeof(s[i].name));
361 strcpy(s[i].name, name);
362 memcpy(&s[i].map, map, sizeof(s[i].map));
363 break;
364 }
365 }
367 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
368 }
370 /**
371 * netdev_boot_setup_check - check boot time settings
372 * @dev: the netdevice
373 *
374 * Check boot time settings for the device.
375 * The found settings are set for the device to be used
376 * later in the device probing.
377 * Returns 0 if no settings found, 1 if they are.
378 */
379 int netdev_boot_setup_check(struct net_device *dev)
380 {
381 struct netdev_boot_setup *s = dev_boot_setup;
382 int i;
384 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
385 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
386 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
387 dev->irq = s[i].map.irq;
388 dev->base_addr = s[i].map.base_addr;
389 dev->mem_start = s[i].map.mem_start;
390 dev->mem_end = s[i].map.mem_end;
391 return 1;
392 }
393 }
394 return 0;
395 }
398 /**
399 * netdev_boot_base - get address from boot time settings
400 * @prefix: prefix for network device
401 * @unit: id for network device
402 *
403 * Check boot time settings for the base address of device.
404 * The found settings are set for the device to be used
405 * later in the device probing.
406 * Returns 0 if no settings found.
407 */
408 unsigned long netdev_boot_base(const char *prefix, int unit)
409 {
410 const struct netdev_boot_setup *s = dev_boot_setup;
411 char name[IFNAMSIZ];
412 int i;
414 sprintf(name, "%s%d", prefix, unit);
416 /*
417 * If device already registered then return base of 1
418 * to indicate not to probe for this interface
419 */
420 if (__dev_get_by_name(name))
421 return 1;
423 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
424 if (!strcmp(name, s[i].name))
425 return s[i].map.base_addr;
426 return 0;
427 }
429 /*
430 * Saves at boot time configured settings for any netdevice.
431 */
432 int __init netdev_boot_setup(char *str)
433 {
434 int ints[5];
435 struct ifmap map;
437 str = get_options(str, ARRAY_SIZE(ints), ints);
438 if (!str || !*str)
439 return 0;
441 /* Save settings */
442 memset(&map, 0, sizeof(map));
443 if (ints[0] > 0)
444 map.irq = ints[1];
445 if (ints[0] > 1)
446 map.base_addr = ints[2];
447 if (ints[0] > 2)
448 map.mem_start = ints[3];
449 if (ints[0] > 3)
450 map.mem_end = ints[4];
452 /* Add new entry to the list */
453 return netdev_boot_setup_add(str, &map);
454 }
456 __setup("netdev=", netdev_boot_setup);
458 /*******************************************************************************
460 Device Interface Subroutines
462 *******************************************************************************/
464 /**
465 * __dev_get_by_name - find a device by its name
466 * @name: name to find
467 *
468 * Find an interface by name. Must be called under RTNL semaphore
469 * or @dev_base_lock. If the name is found a pointer to the device
470 * is returned. If the name is not found then %NULL is returned. The
471 * reference counters are not incremented so the caller must be
472 * careful with locks.
473 */
475 struct net_device *__dev_get_by_name(const char *name)
476 {
477 struct hlist_node *p;
479 hlist_for_each(p, dev_name_hash(name)) {
480 struct net_device *dev
481 = hlist_entry(p, struct net_device, name_hlist);
482 if (!strncmp(dev->name, name, IFNAMSIZ))
483 return dev;
484 }
485 return NULL;
486 }
488 /**
489 * dev_get_by_name - find a device by its name
490 * @name: name to find
491 *
492 * Find an interface by name. This can be called from any
493 * context and does its own locking. The returned handle has
494 * the usage count incremented and the caller must use dev_put() to
495 * release it when it is no longer needed. %NULL is returned if no
496 * matching device is found.
497 */
499 struct net_device *dev_get_by_name(const char *name)
500 {
501 struct net_device *dev;
503 read_lock(&dev_base_lock);
504 dev = __dev_get_by_name(name);
505 if (dev)
506 dev_hold(dev);
507 read_unlock(&dev_base_lock);
508 return dev;
509 }
511 /**
512 * __dev_get_by_index - find a device by its ifindex
513 * @ifindex: index of device
514 *
515 * Search for an interface by index. Returns %NULL if the device
516 * is not found or a pointer to the device. The device has not
517 * had its reference counter increased so the caller must be careful
518 * about locking. The caller must hold either the RTNL semaphore
519 * or @dev_base_lock.
520 */
522 struct net_device *__dev_get_by_index(int ifindex)
523 {
524 struct hlist_node *p;
526 hlist_for_each(p, dev_index_hash(ifindex)) {
527 struct net_device *dev
528 = hlist_entry(p, struct net_device, index_hlist);
529 if (dev->ifindex == ifindex)
530 return dev;
531 }
532 return NULL;
533 }
536 /**
537 * dev_get_by_index - find a device by its ifindex
538 * @ifindex: index of device
539 *
540 * Search for an interface by index. Returns NULL if the device
541 * is not found or a pointer to the device. The device returned has
542 * had a reference added and the pointer is safe until the user calls
543 * dev_put to indicate they have finished with it.
544 */
546 struct net_device *dev_get_by_index(int ifindex)
547 {
548 struct net_device *dev;
550 read_lock(&dev_base_lock);
551 dev = __dev_get_by_index(ifindex);
552 if (dev)
553 dev_hold(dev);
554 read_unlock(&dev_base_lock);
555 return dev;
556 }
558 /**
559 * dev_getbyhwaddr - find a device by its hardware address
560 * @type: media type of device
561 * @ha: hardware address
562 *
563 * Search for an interface by MAC address. Returns NULL if the device
564 * is not found or a pointer to the device. The caller must hold the
565 * rtnl semaphore. The returned device has not had its ref count increased
566 * and the caller must therefore be careful about locking
567 *
568 * BUGS:
569 * If the API was consistent this would be __dev_get_by_hwaddr
570 */
572 struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
573 {
574 struct net_device *dev;
576 ASSERT_RTNL();
578 for (dev = dev_base; dev; dev = dev->next)
579 if (dev->type == type &&
580 !memcmp(dev->dev_addr, ha, dev->addr_len))
581 break;
582 return dev;
583 }
585 EXPORT_SYMBOL(dev_getbyhwaddr);
587 struct net_device *dev_getfirstbyhwtype(unsigned short type)
588 {
589 struct net_device *dev;
591 rtnl_lock();
592 for (dev = dev_base; dev; dev = dev->next) {
593 if (dev->type == type) {
594 dev_hold(dev);
595 break;
596 }
597 }
598 rtnl_unlock();
599 return dev;
600 }
602 EXPORT_SYMBOL(dev_getfirstbyhwtype);
604 /**
605 * dev_get_by_flags - find any device with given flags
606 * @if_flags: IFF_* values
607 * @mask: bitmask of bits in if_flags to check
608 *
609 * Search for any interface with the given flags. Returns NULL if a device
610 * is not found or a pointer to the device. The device returned has
611 * had a reference added and the pointer is safe until the user calls
612 * dev_put to indicate they have finished with it.
613 */
615 struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
616 {
617 struct net_device *dev;
619 read_lock(&dev_base_lock);
620 for (dev = dev_base; dev != NULL; dev = dev->next) {
621 if (((dev->flags ^ if_flags) & mask) == 0) {
622 dev_hold(dev);
623 break;
624 }
625 }
626 read_unlock(&dev_base_lock);
627 return dev;
628 }
630 /**
631 * dev_valid_name - check if name is okay for network device
632 * @name: name string
633 *
634 * Network device names need to be valid file names to
635 * to allow sysfs to work
636 */
637 int dev_valid_name(const char *name)
638 {
639 return !(*name == '\0'
640 || !strcmp(name, ".")
641 || !strcmp(name, "..")
642 || strchr(name, '/'));
643 }
645 /**
646 * dev_alloc_name - allocate a name for a device
647 * @dev: device
648 * @name: name format string
649 *
650 * Passed a format string - eg "lt%d" it will try and find a suitable
651 * id. Not efficient for many devices, not called a lot. The caller
652 * must hold the dev_base or rtnl lock while allocating the name and
653 * adding the device in order to avoid duplicates. Returns the number
654 * of the unit assigned or a negative errno code.
655 */
657 int dev_alloc_name(struct net_device *dev, const char *name)
658 {
659 int i = 0;
660 char buf[IFNAMSIZ];
661 const char *p;
662 const int max_netdevices = 8*PAGE_SIZE;
663 long *inuse;
664 struct net_device *d;
666 p = strnchr(name, IFNAMSIZ-1, '%');
667 if (p) {
668 /*
669 * Verify the string as this thing may have come from
670 * the user. There must be either one "%d" and no other "%"
671 * characters.
672 */
673 if (p[1] != 'd' || strchr(p + 2, '%'))
674 return -EINVAL;
676 /* Use one page as a bit array of possible slots */
677 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
678 if (!inuse)
679 return -ENOMEM;
681 for (d = dev_base; d; d = d->next) {
682 if (!sscanf(d->name, name, &i))
683 continue;
684 if (i < 0 || i >= max_netdevices)
685 continue;
687 /* avoid cases where sscanf is not exact inverse of printf */
688 snprintf(buf, sizeof(buf), name, i);
689 if (!strncmp(buf, d->name, IFNAMSIZ))
690 set_bit(i, inuse);
691 }
693 i = find_first_zero_bit(inuse, max_netdevices);
694 free_page((unsigned long) inuse);
695 }
697 snprintf(buf, sizeof(buf), name, i);
698 if (!__dev_get_by_name(buf)) {
699 strlcpy(dev->name, buf, IFNAMSIZ);
700 return i;
701 }
703 /* It is possible to run out of possible slots
704 * when the name is long and there isn't enough space left
705 * for the digits, or if all bits are used.
706 */
707 return -ENFILE;
708 }
711 /**
712 * dev_change_name - change name of a device
713 * @dev: device
714 * @newname: name (or format string) must be at least IFNAMSIZ
715 *
716 * Change name of a device, can pass format strings "eth%d".
717 * for wildcarding.
718 */
719 int dev_change_name(struct net_device *dev, char *newname)
720 {
721 int err = 0;
723 ASSERT_RTNL();
725 if (dev->flags & IFF_UP)
726 return -EBUSY;
728 if (!dev_valid_name(newname))
729 return -EINVAL;
731 if (strchr(newname, '%')) {
732 err = dev_alloc_name(dev, newname);
733 if (err < 0)
734 return err;
735 strcpy(newname, dev->name);
736 }
737 else if (__dev_get_by_name(newname))
738 return -EEXIST;
739 else
740 strlcpy(dev->name, newname, IFNAMSIZ);
742 err = class_device_rename(&dev->class_dev, dev->name);
743 if (!err) {
744 hlist_del(&dev->name_hlist);
745 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
746 notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
747 }
749 return err;
750 }
752 /**
753 * netdev_features_change - device changes fatures
754 * @dev: device to cause notification
755 *
756 * Called to indicate a device has changed features.
757 */
758 void netdev_features_change(struct net_device *dev)
759 {
760 notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
761 }
762 EXPORT_SYMBOL(netdev_features_change);
764 /**
765 * netdev_state_change - device changes state
766 * @dev: device to cause notification
767 *
768 * Called to indicate a device has changed state. This function calls
769 * the notifier chains for netdev_chain and sends a NEWLINK message
770 * to the routing socket.
771 */
772 void netdev_state_change(struct net_device *dev)
773 {
774 if (dev->flags & IFF_UP) {
775 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
776 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
777 }
778 }
780 /**
781 * dev_load - load a network module
782 * @name: name of interface
783 *
784 * If a network interface is not present and the process has suitable
785 * privileges this function loads the module. If module loading is not
786 * available in this kernel then it becomes a nop.
787 */
789 void dev_load(const char *name)
790 {
791 struct net_device *dev;
793 read_lock(&dev_base_lock);
794 dev = __dev_get_by_name(name);
795 read_unlock(&dev_base_lock);
797 if (!dev && capable(CAP_SYS_MODULE))
798 request_module("%s", name);
799 }
801 static int default_rebuild_header(struct sk_buff *skb)
802 {
803 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
804 skb->dev ? skb->dev->name : "NULL!!!");
805 kfree_skb(skb);
806 return 1;
807 }
810 /**
811 * dev_open - prepare an interface for use.
812 * @dev: device to open
813 *
814 * Takes a device from down to up state. The device's private open
815 * function is invoked and then the multicast lists are loaded. Finally
816 * the device is moved into the up state and a %NETDEV_UP message is
817 * sent to the netdev notifier chain.
818 *
819 * Calling this function on an active interface is a nop. On a failure
820 * a negative errno code is returned.
821 */
822 int dev_open(struct net_device *dev)
823 {
824 int ret = 0;
826 /*
827 * Is it already up?
828 */
830 if (dev->flags & IFF_UP)
831 return 0;
833 /*
834 * Is it even present?
835 */
836 if (!netif_device_present(dev))
837 return -ENODEV;
839 /*
840 * Call device private open method
841 */
842 set_bit(__LINK_STATE_START, &dev->state);
843 if (dev->open) {
844 ret = dev->open(dev);
845 if (ret)
846 clear_bit(__LINK_STATE_START, &dev->state);
847 }
849 /*
850 * If it went open OK then:
851 */
853 if (!ret) {
854 /*
855 * Set the flags.
856 */
857 dev->flags |= IFF_UP;
859 /*
860 * Initialize multicasting status
861 */
862 dev_mc_upload(dev);
864 /*
865 * Wakeup transmit queue engine
866 */
867 dev_activate(dev);
869 /*
870 * ... and announce new interface.
871 */
872 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
873 }
874 return ret;
875 }
877 /**
878 * dev_close - shutdown an interface.
879 * @dev: device to shutdown
880 *
881 * This function moves an active device into down state. A
882 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
883 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
884 * chain.
885 */
886 int dev_close(struct net_device *dev)
887 {
888 if (!(dev->flags & IFF_UP))
889 return 0;
891 /*
892 * Tell people we are going down, so that they can
893 * prepare to death, when device is still operating.
894 */
895 notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
897 dev_deactivate(dev);
899 clear_bit(__LINK_STATE_START, &dev->state);
901 /* Synchronize to scheduled poll. We cannot touch poll list,
902 * it can be even on different cpu. So just clear netif_running(),
903 * and wait when poll really will happen. Actually, the best place
904 * for this is inside dev->stop() after device stopped its irq
905 * engine, but this requires more changes in devices. */
907 smp_mb__after_clear_bit(); /* Commit netif_running(). */
908 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
909 /* No hurry. */
910 msleep(1);
911 }
913 /*
914 * Call the device specific close. This cannot fail.
915 * Only if device is UP
916 *
917 * We allow it to be called even after a DETACH hot-plug
918 * event.
919 */
920 if (dev->stop)
921 dev->stop(dev);
923 /*
924 * Device is now down.
925 */
927 dev->flags &= ~IFF_UP;
929 /*
930 * Tell people we are down
931 */
932 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
934 return 0;
935 }
938 /*
939 * Device change register/unregister. These are not inline or static
940 * as we export them to the world.
941 */
943 /**
944 * register_netdevice_notifier - register a network notifier block
945 * @nb: notifier
946 *
947 * Register a notifier to be called when network device events occur.
948 * The notifier passed is linked into the kernel structures and must
949 * not be reused until it has been unregistered. A negative errno code
950 * is returned on a failure.
951 *
952 * When registered all registration and up events are replayed
953 * to the new notifier to allow device to have a race free
954 * view of the network device list.
955 */
957 int register_netdevice_notifier(struct notifier_block *nb)
958 {
959 struct net_device *dev;
960 int err;
962 rtnl_lock();
963 err = notifier_chain_register(&netdev_chain, nb);
964 if (!err) {
965 for (dev = dev_base; dev; dev = dev->next) {
966 nb->notifier_call(nb, NETDEV_REGISTER, dev);
968 if (dev->flags & IFF_UP)
969 nb->notifier_call(nb, NETDEV_UP, dev);
970 }
971 }
972 rtnl_unlock();
973 return err;
974 }
976 /**
977 * unregister_netdevice_notifier - unregister a network notifier block
978 * @nb: notifier
979 *
980 * Unregister a notifier previously registered by
981 * register_netdevice_notifier(). The notifier is unlinked into the
982 * kernel structures and may then be reused. A negative errno code
983 * is returned on a failure.
984 */
986 int unregister_netdevice_notifier(struct notifier_block *nb)
987 {
988 return notifier_chain_unregister(&netdev_chain, nb);
989 }
991 /**
992 * call_netdevice_notifiers - call all network notifier blocks
993 * @val: value passed unmodified to notifier function
994 * @v: pointer passed unmodified to notifier function
995 *
996 * Call all network notifier blocks. Parameters and return value
997 * are as for notifier_call_chain().
998 */
1000 int call_netdevice_notifiers(unsigned long val, void *v)
1002 return notifier_call_chain(&netdev_chain, val, v);
1005 /* When > 0 there are consumers of rx skb time stamps */
1006 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1008 void net_enable_timestamp(void)
1010 atomic_inc(&netstamp_needed);
1013 void net_disable_timestamp(void)
1015 atomic_dec(&netstamp_needed);
1018 void __net_timestamp(struct sk_buff *skb)
1020 struct timeval tv;
1022 do_gettimeofday(&tv);
1023 skb_set_timestamp(skb, &tv);
1025 EXPORT_SYMBOL(__net_timestamp);
1027 static inline void net_timestamp(struct sk_buff *skb)
1029 if (atomic_read(&netstamp_needed))
1030 __net_timestamp(skb);
1031 else {
1032 skb->tstamp.off_sec = 0;
1033 skb->tstamp.off_usec = 0;
1037 /*
1038 * Support routine. Sends outgoing frames to any network
1039 * taps currently in use.
1040 */
1042 static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1044 struct packet_type *ptype;
1046 net_timestamp(skb);
1048 rcu_read_lock();
1049 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1050 /* Never send packets back to the socket
1051 * they originated from - MvS (miquels@drinkel.ow.org)
1052 */
1053 if ((ptype->dev == dev || !ptype->dev) &&
1054 (ptype->af_packet_priv == NULL ||
1055 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1056 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1057 if (!skb2)
1058 break;
1060 /* skb->nh should be correctly
1061 set by sender, so that the second statement is
1062 just protection against buggy protocols.
1063 */
1064 skb2->mac.raw = skb2->data;
1066 if (skb2->nh.raw < skb2->data ||
1067 skb2->nh.raw > skb2->tail) {
1068 if (net_ratelimit())
1069 printk(KERN_CRIT "protocol %04x is "
1070 "buggy, dev %s\n",
1071 skb2->protocol, dev->name);
1072 skb2->nh.raw = skb2->data;
1075 skb2->h.raw = skb2->nh.raw;
1076 skb2->pkt_type = PACKET_OUTGOING;
1077 ptype->func(skb2, skb->dev, ptype, skb->dev);
1080 rcu_read_unlock();
1083 /*
1084 * Invalidate hardware checksum when packet is to be mangled, and
1085 * complete checksum manually on outgoing path.
1086 */
1087 int skb_checksum_help(struct sk_buff *skb, int inward)
1089 unsigned int csum;
1090 int ret = 0, offset = skb->h.raw - skb->data;
1092 if (inward)
1093 goto out_set_summed;
1095 if (unlikely(skb_shinfo(skb)->gso_size)) {
1096 /* Let GSO fix up the checksum. */
1097 goto out_set_summed;
1100 if (skb_cloned(skb)) {
1101 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1102 if (ret)
1103 goto out;
1106 BUG_ON(offset > (int)skb->len);
1107 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1109 offset = skb->tail - skb->h.raw;
1110 BUG_ON(offset <= 0);
1111 BUG_ON(skb->csum + 2 > offset);
1113 *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
1115 out_set_summed:
1116 skb->ip_summed = CHECKSUM_NONE;
1117 out:
1118 return ret;
1121 /**
1122 * skb_gso_segment - Perform segmentation on skb.
1123 * @skb: buffer to segment
1124 * @features: features for the output path (see dev->features)
1126 * This function segments the given skb and returns a list of segments.
1128 * It may return NULL if the skb requires no segmentation. This is
1129 * only possible when GSO is used for verifying header integrity.
1130 */
1131 struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
1133 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1134 struct packet_type *ptype;
1135 int type = skb->protocol;
1136 int err;
1138 BUG_ON(skb_shinfo(skb)->frag_list);
1140 skb->mac.raw = skb->data;
1141 skb->mac_len = skb->nh.raw - skb->data;
1142 __skb_pull(skb, skb->mac_len);
1144 if (unlikely(skb->ip_summed != CHECKSUM_HW)) {
1145 if (skb_header_cloned(skb) &&
1146 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1147 return ERR_PTR(err);
1150 rcu_read_lock();
1151 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1152 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
1153 if (unlikely(skb->ip_summed != CHECKSUM_HW)) {
1154 err = ptype->gso_send_check(skb);
1155 segs = ERR_PTR(err);
1156 if (err || skb_gso_ok(skb, features))
1157 break;
1158 __skb_push(skb, skb->data - skb->nh.raw);
1160 segs = ptype->gso_segment(skb, features);
1161 break;
1164 rcu_read_unlock();
1166 __skb_push(skb, skb->data - skb->mac.raw);
1168 return segs;
1171 EXPORT_SYMBOL(skb_gso_segment);
1173 /* Take action when hardware reception checksum errors are detected. */
1174 #ifdef CONFIG_BUG
1175 void netdev_rx_csum_fault(struct net_device *dev)
1177 if (net_ratelimit()) {
1178 printk(KERN_ERR "%s: hw csum failure.\n",
1179 dev ? dev->name : "<unknown>");
1180 dump_stack();
1183 EXPORT_SYMBOL(netdev_rx_csum_fault);
1184 #endif
1186 #ifdef CONFIG_HIGHMEM
1187 /* Actually, we should eliminate this check as soon as we know, that:
1188 * 1. IOMMU is present and allows to map all the memory.
1189 * 2. No high memory really exists on this machine.
1190 */
1192 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1194 int i;
1196 if (dev->features & NETIF_F_HIGHDMA)
1197 return 0;
1199 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1200 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1201 return 1;
1203 return 0;
1205 #else
1206 #define illegal_highdma(dev, skb) (0)
1207 #endif
1209 struct dev_gso_cb {
1210 void (*destructor)(struct sk_buff *skb);
1211 };
1213 #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1215 static void dev_gso_skb_destructor(struct sk_buff *skb)
1217 struct dev_gso_cb *cb;
1219 do {
1220 struct sk_buff *nskb = skb->next;
1222 skb->next = nskb->next;
1223 nskb->next = NULL;
1224 kfree_skb(nskb);
1225 } while (skb->next);
1227 cb = DEV_GSO_CB(skb);
1228 if (cb->destructor)
1229 cb->destructor(skb);
1232 /**
1233 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1234 * @skb: buffer to segment
1236 * This function segments the given skb and stores the list of segments
1237 * in skb->next.
1238 */
1239 static int dev_gso_segment(struct sk_buff *skb)
1241 struct net_device *dev = skb->dev;
1242 struct sk_buff *segs;
1243 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1244 NETIF_F_SG : 0);
1246 segs = skb_gso_segment(skb, features);
1248 /* Verifying header integrity only. */
1249 if (!segs)
1250 return 0;
1252 if (unlikely(IS_ERR(segs)))
1253 return PTR_ERR(segs);
1255 skb->next = segs;
1256 DEV_GSO_CB(skb)->destructor = skb->destructor;
1257 skb->destructor = dev_gso_skb_destructor;
1259 return 0;
1262 int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1264 if (likely(!skb->next)) {
1265 if (netdev_nit)
1266 dev_queue_xmit_nit(skb, dev);
1268 if (netif_needs_gso(dev, skb)) {
1269 if (unlikely(dev_gso_segment(skb)))
1270 goto out_kfree_skb;
1271 if (skb->next)
1272 goto gso;
1275 return dev->hard_start_xmit(skb, dev);
1278 gso:
1279 do {
1280 struct sk_buff *nskb = skb->next;
1281 int rc;
1283 skb->next = nskb->next;
1284 nskb->next = NULL;
1285 rc = dev->hard_start_xmit(nskb, dev);
1286 if (unlikely(rc)) {
1287 nskb->next = skb->next;
1288 skb->next = nskb;
1289 return rc;
1291 if (unlikely(netif_queue_stopped(dev) && skb->next))
1292 return NETDEV_TX_BUSY;
1293 } while (skb->next);
1295 skb->destructor = DEV_GSO_CB(skb)->destructor;
1297 out_kfree_skb:
1298 kfree_skb(skb);
1299 return 0;
1302 #define HARD_TX_LOCK(dev, cpu) { \
1303 if ((dev->features & NETIF_F_LLTX) == 0) { \
1304 netif_tx_lock(dev); \
1305 } \
1308 #define HARD_TX_UNLOCK(dev) { \
1309 if ((dev->features & NETIF_F_LLTX) == 0) { \
1310 netif_tx_unlock(dev); \
1311 } \
1314 #ifdef CONFIG_XEN
1315 inline int skb_checksum_setup(struct sk_buff *skb)
1317 if (skb->proto_csum_blank) {
1318 if (skb->protocol != htons(ETH_P_IP))
1319 goto out;
1320 skb->h.raw = (unsigned char *)skb->nh.iph + 4*skb->nh.iph->ihl;
1321 if (skb->h.raw >= skb->tail)
1322 goto out;
1323 switch (skb->nh.iph->protocol) {
1324 case IPPROTO_TCP:
1325 skb->csum = offsetof(struct tcphdr, check);
1326 break;
1327 case IPPROTO_UDP:
1328 skb->csum = offsetof(struct udphdr, check);
1329 break;
1330 default:
1331 if (net_ratelimit())
1332 printk(KERN_ERR "Attempting to checksum a non-"
1333 "TCP/UDP packet, dropping a protocol"
1334 " %d packet", skb->nh.iph->protocol);
1335 goto out;
1337 if ((skb->h.raw + skb->csum + 2) > skb->tail)
1338 goto out;
1339 skb->ip_summed = CHECKSUM_HW;
1340 skb->proto_csum_blank = 0;
1342 return 0;
1343 out:
1344 return -EPROTO;
1346 #else
1347 inline int skb_checksum_setup(struct sk_buff *skb) { return 0; }
1348 #endif
1351 /**
1352 * dev_queue_xmit - transmit a buffer
1353 * @skb: buffer to transmit
1355 * Queue a buffer for transmission to a network device. The caller must
1356 * have set the device and priority and built the buffer before calling
1357 * this function. The function can be called from an interrupt.
1359 * A negative errno code is returned on a failure. A success does not
1360 * guarantee the frame will be transmitted as it may be dropped due
1361 * to congestion or traffic shaping.
1363 * -----------------------------------------------------------------------------------
1364 * I notice this method can also return errors from the queue disciplines,
1365 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1366 * be positive.
1368 * Regardless of the return value, the skb is consumed, so it is currently
1369 * difficult to retry a send to this method. (You can bump the ref count
1370 * before sending to hold a reference for retry if you are careful.)
1372 * When calling this method, interrupts MUST be enabled. This is because
1373 * the BH enable code must have IRQs enabled so that it will not deadlock.
1374 * --BLG
1375 */
1377 int dev_queue_xmit(struct sk_buff *skb)
1379 struct net_device *dev = skb->dev;
1380 struct Qdisc *q;
1381 int rc = -ENOMEM;
1383 /* If a checksum-deferred packet is forwarded to a device that needs a
1384 * checksum, correct the pointers and force checksumming.
1385 */
1386 if (skb_checksum_setup(skb))
1387 goto out_kfree_skb;
1389 /* GSO will handle the following emulations directly. */
1390 if (netif_needs_gso(dev, skb))
1391 goto gso;
1393 if (skb_shinfo(skb)->frag_list &&
1394 !(dev->features & NETIF_F_FRAGLIST) &&
1395 __skb_linearize(skb))
1396 goto out_kfree_skb;
1398 /* Fragmented skb is linearized if device does not support SG,
1399 * or if at least one of fragments is in highmem and device
1400 * does not support DMA from it.
1401 */
1402 if (skb_shinfo(skb)->nr_frags &&
1403 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1404 __skb_linearize(skb))
1405 goto out_kfree_skb;
1407 /* If packet is not checksummed and device does not support
1408 * checksumming for this protocol, complete checksumming here.
1409 */
1410 if (skb->ip_summed == CHECKSUM_HW &&
1411 (!(dev->features & NETIF_F_GEN_CSUM) &&
1412 (!(dev->features & NETIF_F_IP_CSUM) ||
1413 skb->protocol != htons(ETH_P_IP))))
1414 if (skb_checksum_help(skb, 0))
1415 goto out_kfree_skb;
1417 gso:
1418 spin_lock_prefetch(&dev->queue_lock);
1420 /* Disable soft irqs for various locks below. Also
1421 * stops preemption for RCU.
1422 */
1423 rcu_read_lock_bh();
1425 /* Updates of qdisc are serialized by queue_lock.
1426 * The struct Qdisc which is pointed to by qdisc is now a
1427 * rcu structure - it may be accessed without acquiring
1428 * a lock (but the structure may be stale.) The freeing of the
1429 * qdisc will be deferred until it's known that there are no
1430 * more references to it.
1432 * If the qdisc has an enqueue function, we still need to
1433 * hold the queue_lock before calling it, since queue_lock
1434 * also serializes access to the device queue.
1435 */
1437 q = rcu_dereference(dev->qdisc);
1438 #ifdef CONFIG_NET_CLS_ACT
1439 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1440 #endif
1441 if (q->enqueue) {
1442 /* Grab device queue */
1443 spin_lock(&dev->queue_lock);
1445 rc = q->enqueue(skb, q);
1447 qdisc_run(dev);
1449 spin_unlock(&dev->queue_lock);
1450 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1451 goto out;
1454 /* The device has no queue. Common case for software devices:
1455 loopback, all the sorts of tunnels...
1457 Really, it is unlikely that netif_tx_lock protection is necessary
1458 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1459 counters.)
1460 However, it is possible, that they rely on protection
1461 made by us here.
1463 Check this and shot the lock. It is not prone from deadlocks.
1464 Either shot noqueue qdisc, it is even simpler 8)
1465 */
1466 if (dev->flags & IFF_UP) {
1467 int cpu = smp_processor_id(); /* ok because BHs are off */
1469 if (dev->xmit_lock_owner != cpu) {
1471 HARD_TX_LOCK(dev, cpu);
1473 if (!netif_queue_stopped(dev)) {
1474 rc = 0;
1475 if (!dev_hard_start_xmit(skb, dev)) {
1476 HARD_TX_UNLOCK(dev);
1477 goto out;
1480 HARD_TX_UNLOCK(dev);
1481 if (net_ratelimit())
1482 printk(KERN_CRIT "Virtual device %s asks to "
1483 "queue packet!\n", dev->name);
1484 } else {
1485 /* Recursion is detected! It is possible,
1486 * unfortunately */
1487 if (net_ratelimit())
1488 printk(KERN_CRIT "Dead loop on virtual device "
1489 "%s, fix it urgently!\n", dev->name);
1493 rc = -ENETDOWN;
1494 rcu_read_unlock_bh();
1496 out_kfree_skb:
1497 kfree_skb(skb);
1498 return rc;
1499 out:
1500 rcu_read_unlock_bh();
1501 return rc;
1505 /*=======================================================================
1506 Receiver routines
1507 =======================================================================*/
1509 int netdev_max_backlog = 1000;
1510 int netdev_budget = 300;
1511 int weight_p = 64; /* old backlog weight */
1513 DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1516 /**
1517 * netif_rx - post buffer to the network code
1518 * @skb: buffer to post
1520 * This function receives a packet from a device driver and queues it for
1521 * the upper (protocol) levels to process. It always succeeds. The buffer
1522 * may be dropped during processing for congestion control or by the
1523 * protocol layers.
1525 * return values:
1526 * NET_RX_SUCCESS (no congestion)
1527 * NET_RX_CN_LOW (low congestion)
1528 * NET_RX_CN_MOD (moderate congestion)
1529 * NET_RX_CN_HIGH (high congestion)
1530 * NET_RX_DROP (packet was dropped)
1532 */
1534 int netif_rx(struct sk_buff *skb)
1536 struct softnet_data *queue;
1537 unsigned long flags;
1539 /* if netpoll wants it, pretend we never saw it */
1540 if (netpoll_rx(skb))
1541 return NET_RX_DROP;
1543 if (!skb->tstamp.off_sec)
1544 net_timestamp(skb);
1546 /*
1547 * The code is rearranged so that the path is the most
1548 * short when CPU is congested, but is still operating.
1549 */
1550 local_irq_save(flags);
1551 queue = &__get_cpu_var(softnet_data);
1553 __get_cpu_var(netdev_rx_stat).total++;
1554 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1555 if (queue->input_pkt_queue.qlen) {
1556 enqueue:
1557 dev_hold(skb->dev);
1558 __skb_queue_tail(&queue->input_pkt_queue, skb);
1559 local_irq_restore(flags);
1560 return NET_RX_SUCCESS;
1563 netif_rx_schedule(&queue->backlog_dev);
1564 goto enqueue;
1567 __get_cpu_var(netdev_rx_stat).dropped++;
1568 local_irq_restore(flags);
1570 kfree_skb(skb);
1571 return NET_RX_DROP;
1574 int netif_rx_ni(struct sk_buff *skb)
1576 int err;
1578 preempt_disable();
1579 err = netif_rx(skb);
1580 if (local_softirq_pending())
1581 do_softirq();
1582 preempt_enable();
1584 return err;
1587 EXPORT_SYMBOL(netif_rx_ni);
1589 static inline struct net_device *skb_bond(struct sk_buff *skb)
1591 struct net_device *dev = skb->dev;
1593 if (dev->master)
1594 skb->dev = dev->master;
1596 return dev;
1599 static void net_tx_action(struct softirq_action *h)
1601 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1603 if (sd->completion_queue) {
1604 struct sk_buff *clist;
1606 local_irq_disable();
1607 clist = sd->completion_queue;
1608 sd->completion_queue = NULL;
1609 local_irq_enable();
1611 while (clist) {
1612 struct sk_buff *skb = clist;
1613 clist = clist->next;
1615 BUG_TRAP(!atomic_read(&skb->users));
1616 __kfree_skb(skb);
1620 if (sd->output_queue) {
1621 struct net_device *head;
1623 local_irq_disable();
1624 head = sd->output_queue;
1625 sd->output_queue = NULL;
1626 local_irq_enable();
1628 while (head) {
1629 struct net_device *dev = head;
1630 head = head->next_sched;
1632 smp_mb__before_clear_bit();
1633 clear_bit(__LINK_STATE_SCHED, &dev->state);
1635 if (spin_trylock(&dev->queue_lock)) {
1636 qdisc_run(dev);
1637 spin_unlock(&dev->queue_lock);
1638 } else {
1639 netif_schedule(dev);
1645 static __inline__ int deliver_skb(struct sk_buff *skb,
1646 struct packet_type *pt_prev,
1647 struct net_device *orig_dev)
1649 atomic_inc(&skb->users);
1650 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1653 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1654 int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1655 struct net_bridge;
1656 struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1657 unsigned char *addr);
1658 void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
1660 static __inline__ int handle_bridge(struct sk_buff **pskb,
1661 struct packet_type **pt_prev, int *ret,
1662 struct net_device *orig_dev)
1664 struct net_bridge_port *port;
1666 if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1667 (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1668 return 0;
1670 if (*pt_prev) {
1671 *ret = deliver_skb(*pskb, *pt_prev, orig_dev);
1672 *pt_prev = NULL;
1675 return br_handle_frame_hook(port, pskb);
1677 #else
1678 #define handle_bridge(skb, pt_prev, ret, orig_dev) (0)
1679 #endif
1681 #ifdef CONFIG_NET_CLS_ACT
1682 /* TODO: Maybe we should just force sch_ingress to be compiled in
1683 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1684 * a compare and 2 stores extra right now if we dont have it on
1685 * but have CONFIG_NET_CLS_ACT
1686 * NOTE: This doesnt stop any functionality; if you dont have
1687 * the ingress scheduler, you just cant add policies on ingress.
1689 */
1690 static int ing_filter(struct sk_buff *skb)
1692 struct Qdisc *q;
1693 struct net_device *dev = skb->dev;
1694 int result = TC_ACT_OK;
1696 if (dev->qdisc_ingress) {
1697 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1698 if (MAX_RED_LOOP < ttl++) {
1699 printk("Redir loop detected Dropping packet (%s->%s)\n",
1700 skb->input_dev->name, skb->dev->name);
1701 return TC_ACT_SHOT;
1704 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1706 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1708 spin_lock(&dev->ingress_lock);
1709 if ((q = dev->qdisc_ingress) != NULL)
1710 result = q->enqueue(skb, q);
1711 spin_unlock(&dev->ingress_lock);
1715 return result;
1717 #endif
1719 int netif_receive_skb(struct sk_buff *skb)
1721 struct packet_type *ptype, *pt_prev;
1722 struct net_device *orig_dev;
1723 int ret = NET_RX_DROP;
1724 unsigned short type;
1726 /* if we've gotten here through NAPI, check netpoll */
1727 if (skb->dev->poll && netpoll_rx(skb))
1728 return NET_RX_DROP;
1730 if (!skb->tstamp.off_sec)
1731 net_timestamp(skb);
1733 if (!skb->input_dev)
1734 skb->input_dev = skb->dev;
1736 orig_dev = skb_bond(skb);
1738 __get_cpu_var(netdev_rx_stat).total++;
1740 skb->h.raw = skb->nh.raw = skb->data;
1741 skb->mac_len = skb->nh.raw - skb->mac.raw;
1743 pt_prev = NULL;
1745 rcu_read_lock();
1747 #ifdef CONFIG_NET_CLS_ACT
1748 if (skb->tc_verd & TC_NCLS) {
1749 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1750 goto ncls;
1752 #endif
1754 #ifdef CONFIG_XEN
1755 switch (skb->ip_summed) {
1756 case CHECKSUM_UNNECESSARY:
1757 skb->proto_data_valid = 1;
1758 break;
1759 case CHECKSUM_HW:
1760 /* XXX Implement me. */
1761 default:
1762 skb->proto_data_valid = 0;
1763 break;
1765 #endif
1767 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1768 if (!ptype->dev || ptype->dev == skb->dev) {
1769 if (pt_prev)
1770 ret = deliver_skb(skb, pt_prev, orig_dev);
1771 pt_prev = ptype;
1775 #ifdef CONFIG_NET_CLS_ACT
1776 if (pt_prev) {
1777 ret = deliver_skb(skb, pt_prev, orig_dev);
1778 pt_prev = NULL; /* noone else should process this after*/
1779 } else {
1780 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1783 ret = ing_filter(skb);
1785 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1786 kfree_skb(skb);
1787 goto out;
1790 skb->tc_verd = 0;
1791 ncls:
1792 #endif
1794 handle_diverter(skb);
1796 if (handle_bridge(&skb, &pt_prev, &ret, orig_dev))
1797 goto out;
1799 type = skb->protocol;
1800 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1801 if (ptype->type == type &&
1802 (!ptype->dev || ptype->dev == skb->dev)) {
1803 if (pt_prev)
1804 ret = deliver_skb(skb, pt_prev, orig_dev);
1805 pt_prev = ptype;
1809 if (pt_prev) {
1810 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1811 } else {
1812 kfree_skb(skb);
1813 /* Jamal, now you will not able to escape explaining
1814 * me how you were going to use this. :-)
1815 */
1816 ret = NET_RX_DROP;
1819 out:
1820 rcu_read_unlock();
1821 return ret;
1824 static int process_backlog(struct net_device *backlog_dev, int *budget)
1826 int work = 0;
1827 int quota = min(backlog_dev->quota, *budget);
1828 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1829 unsigned long start_time = jiffies;
1831 backlog_dev->weight = weight_p;
1832 for (;;) {
1833 struct sk_buff *skb;
1834 struct net_device *dev;
1836 local_irq_disable();
1837 skb = __skb_dequeue(&queue->input_pkt_queue);
1838 if (!skb)
1839 goto job_done;
1840 local_irq_enable();
1842 dev = skb->dev;
1844 netif_receive_skb(skb);
1846 dev_put(dev);
1848 work++;
1850 if (work >= quota || jiffies - start_time > 1)
1851 break;
1855 backlog_dev->quota -= work;
1856 *budget -= work;
1857 return -1;
1859 job_done:
1860 backlog_dev->quota -= work;
1861 *budget -= work;
1863 list_del(&backlog_dev->poll_list);
1864 smp_mb__before_clear_bit();
1865 netif_poll_enable(backlog_dev);
1867 local_irq_enable();
1868 return 0;
1871 static void net_rx_action(struct softirq_action *h)
1873 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1874 unsigned long start_time = jiffies;
1875 int budget = netdev_budget;
1876 void *have;
1878 local_irq_disable();
1880 while (!list_empty(&queue->poll_list)) {
1881 struct net_device *dev;
1883 if (budget <= 0 || jiffies - start_time > 1)
1884 goto softnet_break;
1886 local_irq_enable();
1888 dev = list_entry(queue->poll_list.next,
1889 struct net_device, poll_list);
1890 have = netpoll_poll_lock(dev);
1892 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
1893 netpoll_poll_unlock(have);
1894 local_irq_disable();
1895 list_del(&dev->poll_list);
1896 list_add_tail(&dev->poll_list, &queue->poll_list);
1897 if (dev->quota < 0)
1898 dev->quota += dev->weight;
1899 else
1900 dev->quota = dev->weight;
1901 } else {
1902 netpoll_poll_unlock(have);
1903 dev_put(dev);
1904 local_irq_disable();
1907 out:
1908 local_irq_enable();
1909 return;
1911 softnet_break:
1912 __get_cpu_var(netdev_rx_stat).time_squeeze++;
1913 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1914 goto out;
1917 static gifconf_func_t * gifconf_list [NPROTO];
1919 /**
1920 * register_gifconf - register a SIOCGIF handler
1921 * @family: Address family
1922 * @gifconf: Function handler
1924 * Register protocol dependent address dumping routines. The handler
1925 * that is passed must not be freed or reused until it has been replaced
1926 * by another handler.
1927 */
1928 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1930 if (family >= NPROTO)
1931 return -EINVAL;
1932 gifconf_list[family] = gifconf;
1933 return 0;
1937 /*
1938 * Map an interface index to its name (SIOCGIFNAME)
1939 */
1941 /*
1942 * We need this ioctl for efficient implementation of the
1943 * if_indextoname() function required by the IPv6 API. Without
1944 * it, we would have to search all the interfaces to find a
1945 * match. --pb
1946 */
1948 static int dev_ifname(struct ifreq __user *arg)
1950 struct net_device *dev;
1951 struct ifreq ifr;
1953 /*
1954 * Fetch the caller's info block.
1955 */
1957 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
1958 return -EFAULT;
1960 read_lock(&dev_base_lock);
1961 dev = __dev_get_by_index(ifr.ifr_ifindex);
1962 if (!dev) {
1963 read_unlock(&dev_base_lock);
1964 return -ENODEV;
1967 strcpy(ifr.ifr_name, dev->name);
1968 read_unlock(&dev_base_lock);
1970 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1971 return -EFAULT;
1972 return 0;
1975 /*
1976 * Perform a SIOCGIFCONF call. This structure will change
1977 * size eventually, and there is nothing I can do about it.
1978 * Thus we will need a 'compatibility mode'.
1979 */
1981 static int dev_ifconf(char __user *arg)
1983 struct ifconf ifc;
1984 struct net_device *dev;
1985 char __user *pos;
1986 int len;
1987 int total;
1988 int i;
1990 /*
1991 * Fetch the caller's info block.
1992 */
1994 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
1995 return -EFAULT;
1997 pos = ifc.ifc_buf;
1998 len = ifc.ifc_len;
2000 /*
2001 * Loop over the interfaces, and write an info block for each.
2002 */
2004 total = 0;
2005 for (dev = dev_base; dev; dev = dev->next) {
2006 for (i = 0; i < NPROTO; i++) {
2007 if (gifconf_list[i]) {
2008 int done;
2009 if (!pos)
2010 done = gifconf_list[i](dev, NULL, 0);
2011 else
2012 done = gifconf_list[i](dev, pos + total,
2013 len - total);
2014 if (done < 0)
2015 return -EFAULT;
2016 total += done;
2021 /*
2022 * All done. Write the updated control block back to the caller.
2023 */
2024 ifc.ifc_len = total;
2026 /*
2027 * Both BSD and Solaris return 0 here, so we do too.
2028 */
2029 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2032 #ifdef CONFIG_PROC_FS
2033 /*
2034 * This is invoked by the /proc filesystem handler to display a device
2035 * in detail.
2036 */
2037 static __inline__ struct net_device *dev_get_idx(loff_t pos)
2039 struct net_device *dev;
2040 loff_t i;
2042 for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
2044 return i == pos ? dev : NULL;
2047 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2049 read_lock(&dev_base_lock);
2050 return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
2053 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2055 ++*pos;
2056 return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
2059 void dev_seq_stop(struct seq_file *seq, void *v)
2061 read_unlock(&dev_base_lock);
2064 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2066 if (dev->get_stats) {
2067 struct net_device_stats *stats = dev->get_stats(dev);
2069 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2070 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2071 dev->name, stats->rx_bytes, stats->rx_packets,
2072 stats->rx_errors,
2073 stats->rx_dropped + stats->rx_missed_errors,
2074 stats->rx_fifo_errors,
2075 stats->rx_length_errors + stats->rx_over_errors +
2076 stats->rx_crc_errors + stats->rx_frame_errors,
2077 stats->rx_compressed, stats->multicast,
2078 stats->tx_bytes, stats->tx_packets,
2079 stats->tx_errors, stats->tx_dropped,
2080 stats->tx_fifo_errors, stats->collisions,
2081 stats->tx_carrier_errors +
2082 stats->tx_aborted_errors +
2083 stats->tx_window_errors +
2084 stats->tx_heartbeat_errors,
2085 stats->tx_compressed);
2086 } else
2087 seq_printf(seq, "%6s: No statistics available.\n", dev->name);
2090 /*
2091 * Called from the PROCfs module. This now uses the new arbitrary sized
2092 * /proc/net interface to create /proc/net/dev
2093 */
2094 static int dev_seq_show(struct seq_file *seq, void *v)
2096 if (v == SEQ_START_TOKEN)
2097 seq_puts(seq, "Inter-| Receive "
2098 " | Transmit\n"
2099 " face |bytes packets errs drop fifo frame "
2100 "compressed multicast|bytes packets errs "
2101 "drop fifo colls carrier compressed\n");
2102 else
2103 dev_seq_printf_stats(seq, v);
2104 return 0;
2107 static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2109 struct netif_rx_stats *rc = NULL;
2111 while (*pos < NR_CPUS)
2112 if (cpu_online(*pos)) {
2113 rc = &per_cpu(netdev_rx_stat, *pos);
2114 break;
2115 } else
2116 ++*pos;
2117 return rc;
2120 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2122 return softnet_get_online(pos);
2125 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2127 ++*pos;
2128 return softnet_get_online(pos);
2131 static void softnet_seq_stop(struct seq_file *seq, void *v)
2135 static int softnet_seq_show(struct seq_file *seq, void *v)
2137 struct netif_rx_stats *s = v;
2139 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2140 s->total, s->dropped, s->time_squeeze, 0,
2141 0, 0, 0, 0, /* was fastroute */
2142 s->cpu_collision );
2143 return 0;
2146 static struct seq_operations dev_seq_ops = {
2147 .start = dev_seq_start,
2148 .next = dev_seq_next,
2149 .stop = dev_seq_stop,
2150 .show = dev_seq_show,
2151 };
2153 static int dev_seq_open(struct inode *inode, struct file *file)
2155 return seq_open(file, &dev_seq_ops);
2158 static struct file_operations dev_seq_fops = {
2159 .owner = THIS_MODULE,
2160 .open = dev_seq_open,
2161 .read = seq_read,
2162 .llseek = seq_lseek,
2163 .release = seq_release,
2164 };
2166 static struct seq_operations softnet_seq_ops = {
2167 .start = softnet_seq_start,
2168 .next = softnet_seq_next,
2169 .stop = softnet_seq_stop,
2170 .show = softnet_seq_show,
2171 };
2173 static int softnet_seq_open(struct inode *inode, struct file *file)
2175 return seq_open(file, &softnet_seq_ops);
2178 static struct file_operations softnet_seq_fops = {
2179 .owner = THIS_MODULE,
2180 .open = softnet_seq_open,
2181 .read = seq_read,
2182 .llseek = seq_lseek,
2183 .release = seq_release,
2184 };
2186 #ifdef WIRELESS_EXT
2187 extern int wireless_proc_init(void);
2188 #else
2189 #define wireless_proc_init() 0
2190 #endif
2192 static int __init dev_proc_init(void)
2194 int rc = -ENOMEM;
2196 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2197 goto out;
2198 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2199 goto out_dev;
2200 if (wireless_proc_init())
2201 goto out_softnet;
2202 rc = 0;
2203 out:
2204 return rc;
2205 out_softnet:
2206 proc_net_remove("softnet_stat");
2207 out_dev:
2208 proc_net_remove("dev");
2209 goto out;
2211 #else
2212 #define dev_proc_init() 0
2213 #endif /* CONFIG_PROC_FS */
2216 /**
2217 * netdev_set_master - set up master/slave pair
2218 * @slave: slave device
2219 * @master: new master device
2221 * Changes the master device of the slave. Pass %NULL to break the
2222 * bonding. The caller must hold the RTNL semaphore. On a failure
2223 * a negative errno code is returned. On success the reference counts
2224 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2225 * function returns zero.
2226 */
2227 int netdev_set_master(struct net_device *slave, struct net_device *master)
2229 struct net_device *old = slave->master;
2231 ASSERT_RTNL();
2233 if (master) {
2234 if (old)
2235 return -EBUSY;
2236 dev_hold(master);
2239 slave->master = master;
2241 synchronize_net();
2243 if (old)
2244 dev_put(old);
2246 if (master)
2247 slave->flags |= IFF_SLAVE;
2248 else
2249 slave->flags &= ~IFF_SLAVE;
2251 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2252 return 0;
2255 /**
2256 * dev_set_promiscuity - update promiscuity count on a device
2257 * @dev: device
2258 * @inc: modifier
2260 * Add or remove promsicuity from a device. While the count in the device
2261 * remains above zero the interface remains promiscuous. Once it hits zero
2262 * the device reverts back to normal filtering operation. A negative inc
2263 * value is used to drop promiscuity on the device.
2264 */
2265 void dev_set_promiscuity(struct net_device *dev, int inc)
2267 unsigned short old_flags = dev->flags;
2269 if ((dev->promiscuity += inc) == 0)
2270 dev->flags &= ~IFF_PROMISC;
2271 else
2272 dev->flags |= IFF_PROMISC;
2273 if (dev->flags != old_flags) {
2274 dev_mc_upload(dev);
2275 printk(KERN_INFO "device %s %s promiscuous mode\n",
2276 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2277 "left");
2281 /**
2282 * dev_set_allmulti - update allmulti count on a device
2283 * @dev: device
2284 * @inc: modifier
2286 * Add or remove reception of all multicast frames to a device. While the
2287 * count in the device remains above zero the interface remains listening
2288 * to all interfaces. Once it hits zero the device reverts back to normal
2289 * filtering operation. A negative @inc value is used to drop the counter
2290 * when releasing a resource needing all multicasts.
2291 */
2293 void dev_set_allmulti(struct net_device *dev, int inc)
2295 unsigned short old_flags = dev->flags;
2297 dev->flags |= IFF_ALLMULTI;
2298 if ((dev->allmulti += inc) == 0)
2299 dev->flags &= ~IFF_ALLMULTI;
2300 if (dev->flags ^ old_flags)
2301 dev_mc_upload(dev);
2304 unsigned dev_get_flags(const struct net_device *dev)
2306 unsigned flags;
2308 flags = (dev->flags & ~(IFF_PROMISC |
2309 IFF_ALLMULTI |
2310 IFF_RUNNING)) |
2311 (dev->gflags & (IFF_PROMISC |
2312 IFF_ALLMULTI));
2314 if (netif_running(dev) && netif_carrier_ok(dev))
2315 flags |= IFF_RUNNING;
2317 return flags;
2320 int dev_change_flags(struct net_device *dev, unsigned flags)
2322 int ret;
2323 int old_flags = dev->flags;
2325 /*
2326 * Set the flags on our device.
2327 */
2329 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2330 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2331 IFF_AUTOMEDIA)) |
2332 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2333 IFF_ALLMULTI));
2335 /*
2336 * Load in the correct multicast list now the flags have changed.
2337 */
2339 dev_mc_upload(dev);
2341 /*
2342 * Have we downed the interface. We handle IFF_UP ourselves
2343 * according to user attempts to set it, rather than blindly
2344 * setting it.
2345 */
2347 ret = 0;
2348 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2349 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2351 if (!ret)
2352 dev_mc_upload(dev);
2355 if (dev->flags & IFF_UP &&
2356 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2357 IFF_VOLATILE)))
2358 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
2360 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2361 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2362 dev->gflags ^= IFF_PROMISC;
2363 dev_set_promiscuity(dev, inc);
2366 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2367 is important. Some (broken) drivers set IFF_PROMISC, when
2368 IFF_ALLMULTI is requested not asking us and not reporting.
2369 */
2370 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2371 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2372 dev->gflags ^= IFF_ALLMULTI;
2373 dev_set_allmulti(dev, inc);
2376 if (old_flags ^ dev->flags)
2377 rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2379 return ret;
2382 int dev_set_mtu(struct net_device *dev, int new_mtu)
2384 int err;
2386 if (new_mtu == dev->mtu)
2387 return 0;
2389 /* MTU must be positive. */
2390 if (new_mtu < 0)
2391 return -EINVAL;
2393 if (!netif_device_present(dev))
2394 return -ENODEV;
2396 err = 0;
2397 if (dev->change_mtu)
2398 err = dev->change_mtu(dev, new_mtu);
2399 else
2400 dev->mtu = new_mtu;
2401 if (!err && dev->flags & IFF_UP)
2402 notifier_call_chain(&netdev_chain,
2403 NETDEV_CHANGEMTU, dev);
2404 return err;
2407 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2409 int err;
2411 if (!dev->set_mac_address)
2412 return -EOPNOTSUPP;
2413 if (sa->sa_family != dev->type)
2414 return -EINVAL;
2415 if (!netif_device_present(dev))
2416 return -ENODEV;
2417 err = dev->set_mac_address(dev, sa);
2418 if (!err)
2419 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
2420 return err;
2423 /*
2424 * Perform the SIOCxIFxxx calls.
2425 */
2426 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2428 int err;
2429 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2431 if (!dev)
2432 return -ENODEV;
2434 switch (cmd) {
2435 case SIOCGIFFLAGS: /* Get interface flags */
2436 ifr->ifr_flags = dev_get_flags(dev);
2437 return 0;
2439 case SIOCSIFFLAGS: /* Set interface flags */
2440 return dev_change_flags(dev, ifr->ifr_flags);
2442 case SIOCGIFMETRIC: /* Get the metric on the interface
2443 (currently unused) */
2444 ifr->ifr_metric = 0;
2445 return 0;
2447 case SIOCSIFMETRIC: /* Set the metric on the interface
2448 (currently unused) */
2449 return -EOPNOTSUPP;
2451 case SIOCGIFMTU: /* Get the MTU of a device */
2452 ifr->ifr_mtu = dev->mtu;
2453 return 0;
2455 case SIOCSIFMTU: /* Set the MTU of a device */
2456 return dev_set_mtu(dev, ifr->ifr_mtu);
2458 case SIOCGIFHWADDR:
2459 if (!dev->addr_len)
2460 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2461 else
2462 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2463 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2464 ifr->ifr_hwaddr.sa_family = dev->type;
2465 return 0;
2467 case SIOCSIFHWADDR:
2468 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2470 case SIOCSIFHWBROADCAST:
2471 if (ifr->ifr_hwaddr.sa_family != dev->type)
2472 return -EINVAL;
2473 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2474 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2475 notifier_call_chain(&netdev_chain,
2476 NETDEV_CHANGEADDR, dev);
2477 return 0;
2479 case SIOCGIFMAP:
2480 ifr->ifr_map.mem_start = dev->mem_start;
2481 ifr->ifr_map.mem_end = dev->mem_end;
2482 ifr->ifr_map.base_addr = dev->base_addr;
2483 ifr->ifr_map.irq = dev->irq;
2484 ifr->ifr_map.dma = dev->dma;
2485 ifr->ifr_map.port = dev->if_port;
2486 return 0;
2488 case SIOCSIFMAP:
2489 if (dev->set_config) {
2490 if (!netif_device_present(dev))
2491 return -ENODEV;
2492 return dev->set_config(dev, &ifr->ifr_map);
2494 return -EOPNOTSUPP;
2496 case SIOCADDMULTI:
2497 if (!dev->set_multicast_list ||
2498 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2499 return -EINVAL;
2500 if (!netif_device_present(dev))
2501 return -ENODEV;
2502 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2503 dev->addr_len, 1);
2505 case SIOCDELMULTI:
2506 if (!dev->set_multicast_list ||
2507 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2508 return -EINVAL;
2509 if (!netif_device_present(dev))
2510 return -ENODEV;
2511 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2512 dev->addr_len, 1);
2514 case SIOCGIFINDEX:
2515 ifr->ifr_ifindex = dev->ifindex;
2516 return 0;
2518 case SIOCGIFTXQLEN:
2519 ifr->ifr_qlen = dev->tx_queue_len;
2520 return 0;
2522 case SIOCSIFTXQLEN:
2523 if (ifr->ifr_qlen < 0)
2524 return -EINVAL;
2525 dev->tx_queue_len = ifr->ifr_qlen;
2526 return 0;
2528 case SIOCSIFNAME:
2529 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2530 return dev_change_name(dev, ifr->ifr_newname);
2532 /*
2533 * Unknown or private ioctl
2534 */
2536 default:
2537 if ((cmd >= SIOCDEVPRIVATE &&
2538 cmd <= SIOCDEVPRIVATE + 15) ||
2539 cmd == SIOCBONDENSLAVE ||
2540 cmd == SIOCBONDRELEASE ||
2541 cmd == SIOCBONDSETHWADDR ||
2542 cmd == SIOCBONDSLAVEINFOQUERY ||
2543 cmd == SIOCBONDINFOQUERY ||
2544 cmd == SIOCBONDCHANGEACTIVE ||
2545 cmd == SIOCGMIIPHY ||
2546 cmd == SIOCGMIIREG ||
2547 cmd == SIOCSMIIREG ||
2548 cmd == SIOCBRADDIF ||
2549 cmd == SIOCBRDELIF ||
2550 cmd == SIOCWANDEV) {
2551 err = -EOPNOTSUPP;
2552 if (dev->do_ioctl) {
2553 if (netif_device_present(dev))
2554 err = dev->do_ioctl(dev, ifr,
2555 cmd);
2556 else
2557 err = -ENODEV;
2559 } else
2560 err = -EINVAL;
2563 return err;
2566 /*
2567 * This function handles all "interface"-type I/O control requests. The actual
2568 * 'doing' part of this is dev_ifsioc above.
2569 */
2571 /**
2572 * dev_ioctl - network device ioctl
2573 * @cmd: command to issue
2574 * @arg: pointer to a struct ifreq in user space
2576 * Issue ioctl functions to devices. This is normally called by the
2577 * user space syscall interfaces but can sometimes be useful for
2578 * other purposes. The return value is the return from the syscall if
2579 * positive or a negative errno code on error.
2580 */
2582 int dev_ioctl(unsigned int cmd, void __user *arg)
2584 struct ifreq ifr;
2585 int ret;
2586 char *colon;
2588 /* One special case: SIOCGIFCONF takes ifconf argument
2589 and requires shared lock, because it sleeps writing
2590 to user space.
2591 */
2593 if (cmd == SIOCGIFCONF) {
2594 rtnl_shlock();
2595 ret = dev_ifconf((char __user *) arg);
2596 rtnl_shunlock();
2597 return ret;
2599 if (cmd == SIOCGIFNAME)
2600 return dev_ifname((struct ifreq __user *)arg);
2602 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2603 return -EFAULT;
2605 ifr.ifr_name[IFNAMSIZ-1] = 0;
2607 colon = strchr(ifr.ifr_name, ':');
2608 if (colon)
2609 *colon = 0;
2611 /*
2612 * See which interface the caller is talking about.
2613 */
2615 switch (cmd) {
2616 /*
2617 * These ioctl calls:
2618 * - can be done by all.
2619 * - atomic and do not require locking.
2620 * - return a value
2621 */
2622 case SIOCGIFFLAGS:
2623 case SIOCGIFMETRIC:
2624 case SIOCGIFMTU:
2625 case SIOCGIFHWADDR:
2626 case SIOCGIFSLAVE:
2627 case SIOCGIFMAP:
2628 case SIOCGIFINDEX:
2629 case SIOCGIFTXQLEN:
2630 dev_load(ifr.ifr_name);
2631 read_lock(&dev_base_lock);
2632 ret = dev_ifsioc(&ifr, cmd);
2633 read_unlock(&dev_base_lock);
2634 if (!ret) {
2635 if (colon)
2636 *colon = ':';
2637 if (copy_to_user(arg, &ifr,
2638 sizeof(struct ifreq)))
2639 ret = -EFAULT;
2641 return ret;
2643 case SIOCETHTOOL:
2644 dev_load(ifr.ifr_name);
2645 rtnl_lock();
2646 ret = dev_ethtool(&ifr);
2647 rtnl_unlock();
2648 if (!ret) {
2649 if (colon)
2650 *colon = ':';
2651 if (copy_to_user(arg, &ifr,
2652 sizeof(struct ifreq)))
2653 ret = -EFAULT;
2655 return ret;
2657 /*
2658 * These ioctl calls:
2659 * - require superuser power.
2660 * - require strict serialization.
2661 * - return a value
2662 */
2663 case SIOCGMIIPHY:
2664 case SIOCGMIIREG:
2665 case SIOCSIFNAME:
2666 if (!capable(CAP_NET_ADMIN))
2667 return -EPERM;
2668 dev_load(ifr.ifr_name);
2669 rtnl_lock();
2670 ret = dev_ifsioc(&ifr, cmd);
2671 rtnl_unlock();
2672 if (!ret) {
2673 if (colon)
2674 *colon = ':';
2675 if (copy_to_user(arg, &ifr,
2676 sizeof(struct ifreq)))
2677 ret = -EFAULT;
2679 return ret;
2681 /*
2682 * These ioctl calls:
2683 * - require superuser power.
2684 * - require strict serialization.
2685 * - do not return a value
2686 */
2687 case SIOCSIFFLAGS:
2688 case SIOCSIFMETRIC:
2689 case SIOCSIFMTU:
2690 case SIOCSIFMAP:
2691 case SIOCSIFHWADDR:
2692 case SIOCSIFSLAVE:
2693 case SIOCADDMULTI:
2694 case SIOCDELMULTI:
2695 case SIOCSIFHWBROADCAST:
2696 case SIOCSIFTXQLEN:
2697 case SIOCSMIIREG:
2698 case SIOCBONDENSLAVE:
2699 case SIOCBONDRELEASE:
2700 case SIOCBONDSETHWADDR:
2701 case SIOCBONDCHANGEACTIVE:
2702 case SIOCBRADDIF:
2703 case SIOCBRDELIF:
2704 if (!capable(CAP_NET_ADMIN))
2705 return -EPERM;
2706 /* fall through */
2707 case SIOCBONDSLAVEINFOQUERY:
2708 case SIOCBONDINFOQUERY:
2709 dev_load(ifr.ifr_name);
2710 rtnl_lock();
2711 ret = dev_ifsioc(&ifr, cmd);
2712 rtnl_unlock();
2713 return ret;
2715 case SIOCGIFMEM:
2716 /* Get the per device memory space. We can add this but
2717 * currently do not support it */
2718 case SIOCSIFMEM:
2719 /* Set the per device memory buffer space.
2720 * Not applicable in our case */
2721 case SIOCSIFLINK:
2722 return -EINVAL;
2724 /*
2725 * Unknown or private ioctl.
2726 */
2727 default:
2728 if (cmd == SIOCWANDEV ||
2729 (cmd >= SIOCDEVPRIVATE &&
2730 cmd <= SIOCDEVPRIVATE + 15)) {
2731 dev_load(ifr.ifr_name);
2732 rtnl_lock();
2733 ret = dev_ifsioc(&ifr, cmd);
2734 rtnl_unlock();
2735 if (!ret && copy_to_user(arg, &ifr,
2736 sizeof(struct ifreq)))
2737 ret = -EFAULT;
2738 return ret;
2740 #ifdef WIRELESS_EXT
2741 /* Take care of Wireless Extensions */
2742 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2743 /* If command is `set a parameter', or
2744 * `get the encoding parameters', check if
2745 * the user has the right to do it */
2746 if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE) {
2747 if (!capable(CAP_NET_ADMIN))
2748 return -EPERM;
2750 dev_load(ifr.ifr_name);
2751 rtnl_lock();
2752 /* Follow me in net/core/wireless.c */
2753 ret = wireless_process_ioctl(&ifr, cmd);
2754 rtnl_unlock();
2755 if (IW_IS_GET(cmd) &&
2756 copy_to_user(arg, &ifr,
2757 sizeof(struct ifreq)))
2758 ret = -EFAULT;
2759 return ret;
2761 #endif /* WIRELESS_EXT */
2762 return -EINVAL;
2767 /**
2768 * dev_new_index - allocate an ifindex
2770 * Returns a suitable unique value for a new device interface
2771 * number. The caller must hold the rtnl semaphore or the
2772 * dev_base_lock to be sure it remains unique.
2773 */
2774 static int dev_new_index(void)
2776 static int ifindex;
2777 for (;;) {
2778 if (++ifindex <= 0)
2779 ifindex = 1;
2780 if (!__dev_get_by_index(ifindex))
2781 return ifindex;
2785 static int dev_boot_phase = 1;
2787 /* Delayed registration/unregisteration */
2788 static DEFINE_SPINLOCK(net_todo_list_lock);
2789 static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
2791 static inline void net_set_todo(struct net_device *dev)
2793 spin_lock(&net_todo_list_lock);
2794 list_add_tail(&dev->todo_list, &net_todo_list);
2795 spin_unlock(&net_todo_list_lock);
2798 /**
2799 * register_netdevice - register a network device
2800 * @dev: device to register
2802 * Take a completed network device structure and add it to the kernel
2803 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2804 * chain. 0 is returned on success. A negative errno code is returned
2805 * on a failure to set up the device, or if the name is a duplicate.
2807 * Callers must hold the rtnl semaphore. You may want
2808 * register_netdev() instead of this.
2810 * BUGS:
2811 * The locking appears insufficient to guarantee two parallel registers
2812 * will not get the same name.
2813 */
2815 int register_netdevice(struct net_device *dev)
2817 struct hlist_head *head;
2818 struct hlist_node *p;
2819 int ret;
2821 BUG_ON(dev_boot_phase);
2822 ASSERT_RTNL();
2824 /* When net_device's are persistent, this will be fatal. */
2825 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
2827 spin_lock_init(&dev->queue_lock);
2828 spin_lock_init(&dev->_xmit_lock);
2829 dev->xmit_lock_owner = -1;
2830 #ifdef CONFIG_NET_CLS_ACT
2831 spin_lock_init(&dev->ingress_lock);
2832 #endif
2834 ret = alloc_divert_blk(dev);
2835 if (ret)
2836 goto out;
2838 dev->iflink = -1;
2840 /* Init, if this function is available */
2841 if (dev->init) {
2842 ret = dev->init(dev);
2843 if (ret) {
2844 if (ret > 0)
2845 ret = -EIO;
2846 goto out_err;
2850 if (!dev_valid_name(dev->name)) {
2851 ret = -EINVAL;
2852 goto out_err;
2855 dev->ifindex = dev_new_index();
2856 if (dev->iflink == -1)
2857 dev->iflink = dev->ifindex;
2859 /* Check for existence of name */
2860 head = dev_name_hash(dev->name);
2861 hlist_for_each(p, head) {
2862 struct net_device *d
2863 = hlist_entry(p, struct net_device, name_hlist);
2864 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
2865 ret = -EEXIST;
2866 goto out_err;
2870 /* Fix illegal SG+CSUM combinations. */
2871 if ((dev->features & NETIF_F_SG) &&
2872 !(dev->features & NETIF_F_ALL_CSUM)) {
2873 printk("%s: Dropping NETIF_F_SG since no checksum feature.\n",
2874 dev->name);
2875 dev->features &= ~NETIF_F_SG;
2878 /* TSO requires that SG is present as well. */
2879 if ((dev->features & NETIF_F_TSO) &&
2880 !(dev->features & NETIF_F_SG)) {
2881 printk("%s: Dropping NETIF_F_TSO since no SG feature.\n",
2882 dev->name);
2883 dev->features &= ~NETIF_F_TSO;
2885 if (dev->features & NETIF_F_UFO) {
2886 if (!(dev->features & NETIF_F_HW_CSUM)) {
2887 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2888 "NETIF_F_HW_CSUM feature.\n",
2889 dev->name);
2890 dev->features &= ~NETIF_F_UFO;
2892 if (!(dev->features & NETIF_F_SG)) {
2893 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2894 "NETIF_F_SG feature.\n",
2895 dev->name);
2896 dev->features &= ~NETIF_F_UFO;
2900 /*
2901 * nil rebuild_header routine,
2902 * that should be never called and used as just bug trap.
2903 */
2905 if (!dev->rebuild_header)
2906 dev->rebuild_header = default_rebuild_header;
2908 /*
2909 * Default initial state at registry is that the
2910 * device is present.
2911 */
2913 set_bit(__LINK_STATE_PRESENT, &dev->state);
2915 dev->next = NULL;
2916 dev_init_scheduler(dev);
2917 write_lock_bh(&dev_base_lock);
2918 *dev_tail = dev;
2919 dev_tail = &dev->next;
2920 hlist_add_head(&dev->name_hlist, head);
2921 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
2922 dev_hold(dev);
2923 dev->reg_state = NETREG_REGISTERING;
2924 write_unlock_bh(&dev_base_lock);
2926 /* Notify protocols, that a new device appeared. */
2927 notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
2929 /* Finish registration after unlock */
2930 net_set_todo(dev);
2931 ret = 0;
2933 out:
2934 return ret;
2935 out_err:
2936 free_divert_blk(dev);
2937 goto out;
2940 /**
2941 * register_netdev - register a network device
2942 * @dev: device to register
2944 * Take a completed network device structure and add it to the kernel
2945 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2946 * chain. 0 is returned on success. A negative errno code is returned
2947 * on a failure to set up the device, or if the name is a duplicate.
2949 * This is a wrapper around register_netdev that takes the rtnl semaphore
2950 * and expands the device name if you passed a format string to
2951 * alloc_netdev.
2952 */
2953 int register_netdev(struct net_device *dev)
2955 int err;
2957 rtnl_lock();
2959 /*
2960 * If the name is a format string the caller wants us to do a
2961 * name allocation.
2962 */
2963 if (strchr(dev->name, '%')) {
2964 err = dev_alloc_name(dev, dev->name);
2965 if (err < 0)
2966 goto out;
2969 /*
2970 * Back compatibility hook. Kill this one in 2.5
2971 */
2972 if (dev->name[0] == 0 || dev->name[0] == ' ') {
2973 err = dev_alloc_name(dev, "eth%d");
2974 if (err < 0)
2975 goto out;
2978 err = register_netdevice(dev);
2979 out:
2980 rtnl_unlock();
2981 return err;
2983 EXPORT_SYMBOL(register_netdev);
2985 /*
2986 * netdev_wait_allrefs - wait until all references are gone.
2988 * This is called when unregistering network devices.
2990 * Any protocol or device that holds a reference should register
2991 * for netdevice notification, and cleanup and put back the
2992 * reference if they receive an UNREGISTER event.
2993 * We can get stuck here if buggy protocols don't correctly
2994 * call dev_put.
2995 */
2996 static void netdev_wait_allrefs(struct net_device *dev)
2998 unsigned long rebroadcast_time, warning_time;
3000 rebroadcast_time = warning_time = jiffies;
3001 while (atomic_read(&dev->refcnt) != 0) {
3002 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
3003 rtnl_shlock();
3005 /* Rebroadcast unregister notification */
3006 notifier_call_chain(&netdev_chain,
3007 NETDEV_UNREGISTER, dev);
3009 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3010 &dev->state)) {
3011 /* We must not have linkwatch events
3012 * pending on unregister. If this
3013 * happens, we simply run the queue
3014 * unscheduled, resulting in a noop
3015 * for this device.
3016 */
3017 linkwatch_run_queue();
3020 rtnl_shunlock();
3022 rebroadcast_time = jiffies;
3025 msleep(250);
3027 if (time_after(jiffies, warning_time + 10 * HZ)) {
3028 printk(KERN_EMERG "unregister_netdevice: "
3029 "waiting for %s to become free. Usage "
3030 "count = %d\n",
3031 dev->name, atomic_read(&dev->refcnt));
3032 warning_time = jiffies;
3037 /* The sequence is:
3039 * rtnl_lock();
3040 * ...
3041 * register_netdevice(x1);
3042 * register_netdevice(x2);
3043 * ...
3044 * unregister_netdevice(y1);
3045 * unregister_netdevice(y2);
3046 * ...
3047 * rtnl_unlock();
3048 * free_netdev(y1);
3049 * free_netdev(y2);
3051 * We are invoked by rtnl_unlock() after it drops the semaphore.
3052 * This allows us to deal with problems:
3053 * 1) We can create/delete sysfs objects which invoke hotplug
3054 * without deadlocking with linkwatch via keventd.
3055 * 2) Since we run with the RTNL semaphore not held, we can sleep
3056 * safely in order to wait for the netdev refcnt to drop to zero.
3057 */
3058 static DECLARE_MUTEX(net_todo_run_mutex);
3059 void netdev_run_todo(void)
3061 struct list_head list = LIST_HEAD_INIT(list);
3062 int err;
3065 /* Need to guard against multiple cpu's getting out of order. */
3066 down(&net_todo_run_mutex);
3068 /* Not safe to do outside the semaphore. We must not return
3069 * until all unregister events invoked by the local processor
3070 * have been completed (either by this todo run, or one on
3071 * another cpu).
3072 */
3073 if (list_empty(&net_todo_list))
3074 goto out;
3076 /* Snapshot list, allow later requests */
3077 spin_lock(&net_todo_list_lock);
3078 list_splice_init(&net_todo_list, &list);
3079 spin_unlock(&net_todo_list_lock);
3081 while (!list_empty(&list)) {
3082 struct net_device *dev
3083 = list_entry(list.next, struct net_device, todo_list);
3084 list_del(&dev->todo_list);
3086 switch(dev->reg_state) {
3087 case NETREG_REGISTERING:
3088 dev->reg_state = NETREG_REGISTERED;
3089 err = netdev_register_sysfs(dev);
3090 if (err)
3091 printk(KERN_ERR "%s: failed sysfs registration (%d)\n",
3092 dev->name, err);
3093 break;
3095 case NETREG_UNREGISTERING:
3096 netdev_unregister_sysfs(dev);
3097 dev->reg_state = NETREG_UNREGISTERED;
3099 netdev_wait_allrefs(dev);
3101 /* paranoia */
3102 BUG_ON(atomic_read(&dev->refcnt));
3103 BUG_TRAP(!dev->ip_ptr);
3104 BUG_TRAP(!dev->ip6_ptr);
3105 BUG_TRAP(!dev->dn_ptr);
3108 /* It must be the very last action,
3109 * after this 'dev' may point to freed up memory.
3110 */
3111 if (dev->destructor)
3112 dev->destructor(dev);
3113 break;
3115 default:
3116 printk(KERN_ERR "network todo '%s' but state %d\n",
3117 dev->name, dev->reg_state);
3118 break;
3122 out:
3123 up(&net_todo_run_mutex);
3126 /**
3127 * alloc_netdev - allocate network device
3128 * @sizeof_priv: size of private data to allocate space for
3129 * @name: device name format string
3130 * @setup: callback to initialize device
3132 * Allocates a struct net_device with private data area for driver use
3133 * and performs basic initialization.
3134 */
3135 struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3136 void (*setup)(struct net_device *))
3138 void *p;
3139 struct net_device *dev;
3140 int alloc_size;
3142 /* ensure 32-byte alignment of both the device and private area */
3143 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3144 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3146 p = kmalloc(alloc_size, GFP_KERNEL);
3147 if (!p) {
3148 printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
3149 return NULL;
3151 memset(p, 0, alloc_size);
3153 dev = (struct net_device *)
3154 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3155 dev->padded = (char *)dev - (char *)p;
3157 if (sizeof_priv)
3158 dev->priv = netdev_priv(dev);
3160 setup(dev);
3161 strcpy(dev->name, name);
3162 return dev;
3164 EXPORT_SYMBOL(alloc_netdev);
3166 /**
3167 * free_netdev - free network device
3168 * @dev: device
3170 * This function does the last stage of destroying an allocated device
3171 * interface. The reference to the device object is released.
3172 * If this is the last reference then it will be freed.
3173 */
3174 void free_netdev(struct net_device *dev)
3176 #ifdef CONFIG_SYSFS
3177 /* Compatiablity with error handling in drivers */
3178 if (dev->reg_state == NETREG_UNINITIALIZED) {
3179 kfree((char *)dev - dev->padded);
3180 return;
3183 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3184 dev->reg_state = NETREG_RELEASED;
3186 /* will free via class release */
3187 class_device_put(&dev->class_dev);
3188 #else
3189 kfree((char *)dev - dev->padded);
3190 #endif
3193 /* Synchronize with packet receive processing. */
3194 void synchronize_net(void)
3196 might_sleep();
3197 synchronize_rcu();
3200 /**
3201 * unregister_netdevice - remove device from the kernel
3202 * @dev: device
3204 * This function shuts down a device interface and removes it
3205 * from the kernel tables. On success 0 is returned, on a failure
3206 * a negative errno code is returned.
3208 * Callers must hold the rtnl semaphore. You may want
3209 * unregister_netdev() instead of this.
3210 */
3212 int unregister_netdevice(struct net_device *dev)
3214 struct net_device *d, **dp;
3216 BUG_ON(dev_boot_phase);
3217 ASSERT_RTNL();
3219 /* Some devices call without registering for initialization unwind. */
3220 if (dev->reg_state == NETREG_UNINITIALIZED) {
3221 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3222 "was registered\n", dev->name, dev);
3223 return -ENODEV;
3226 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3228 /* If device is running, close it first. */
3229 if (dev->flags & IFF_UP)
3230 dev_close(dev);
3232 /* And unlink it from device chain. */
3233 for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3234 if (d == dev) {
3235 write_lock_bh(&dev_base_lock);
3236 hlist_del(&dev->name_hlist);
3237 hlist_del(&dev->index_hlist);
3238 if (dev_tail == &dev->next)
3239 dev_tail = dp;
3240 *dp = d->next;
3241 write_unlock_bh(&dev_base_lock);
3242 break;
3245 if (!d) {
3246 printk(KERN_ERR "unregister net_device: '%s' not found\n",
3247 dev->name);
3248 return -ENODEV;
3251 dev->reg_state = NETREG_UNREGISTERING;
3253 synchronize_net();
3255 /* Shutdown queueing discipline. */
3256 dev_shutdown(dev);
3259 /* Notify protocols, that we are about to destroy
3260 this device. They should clean all the things.
3261 */
3262 notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3264 /*
3265 * Flush the multicast chain
3266 */
3267 dev_mc_discard(dev);
3269 if (dev->uninit)
3270 dev->uninit(dev);
3272 /* Notifier chain MUST detach us from master device. */
3273 BUG_TRAP(!dev->master);
3275 free_divert_blk(dev);
3277 /* Finish processing unregister after unlock */
3278 net_set_todo(dev);
3280 synchronize_net();
3282 dev_put(dev);
3283 return 0;
3286 /**
3287 * unregister_netdev - remove device from the kernel
3288 * @dev: device
3290 * This function shuts down a device interface and removes it
3291 * from the kernel tables. On success 0 is returned, on a failure
3292 * a negative errno code is returned.
3294 * This is just a wrapper for unregister_netdevice that takes
3295 * the rtnl semaphore. In general you want to use this and not
3296 * unregister_netdevice.
3297 */
3298 void unregister_netdev(struct net_device *dev)
3300 rtnl_lock();
3301 unregister_netdevice(dev);
3302 rtnl_unlock();
3305 EXPORT_SYMBOL(unregister_netdev);
3307 #ifdef CONFIG_HOTPLUG_CPU
3308 static int dev_cpu_callback(struct notifier_block *nfb,
3309 unsigned long action,
3310 void *ocpu)
3312 struct sk_buff **list_skb;
3313 struct net_device **list_net;
3314 struct sk_buff *skb;
3315 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3316 struct softnet_data *sd, *oldsd;
3318 if (action != CPU_DEAD)
3319 return NOTIFY_OK;
3321 local_irq_disable();
3322 cpu = smp_processor_id();
3323 sd = &per_cpu(softnet_data, cpu);
3324 oldsd = &per_cpu(softnet_data, oldcpu);
3326 /* Find end of our completion_queue. */
3327 list_skb = &sd->completion_queue;
3328 while (*list_skb)
3329 list_skb = &(*list_skb)->next;
3330 /* Append completion queue from offline CPU. */
3331 *list_skb = oldsd->completion_queue;
3332 oldsd->completion_queue = NULL;
3334 /* Find end of our output_queue. */
3335 list_net = &sd->output_queue;
3336 while (*list_net)
3337 list_net = &(*list_net)->next_sched;
3338 /* Append output queue from offline CPU. */
3339 *list_net = oldsd->output_queue;
3340 oldsd->output_queue = NULL;
3342 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3343 local_irq_enable();
3345 /* Process offline CPU's input_pkt_queue */
3346 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3347 netif_rx(skb);
3349 return NOTIFY_OK;
3351 #endif /* CONFIG_HOTPLUG_CPU */
3354 /*
3355 * Initialize the DEV module. At boot time this walks the device list and
3356 * unhooks any devices that fail to initialise (normally hardware not
3357 * present) and leaves us with a valid list of present and active devices.
3359 */
3361 /*
3362 * This is called single threaded during boot, so no need
3363 * to take the rtnl semaphore.
3364 */
3365 static int __init net_dev_init(void)
3367 int i, rc = -ENOMEM;
3369 BUG_ON(!dev_boot_phase);
3371 net_random_init();
3373 if (dev_proc_init())
3374 goto out;
3376 if (netdev_sysfs_init())
3377 goto out;
3379 INIT_LIST_HEAD(&ptype_all);
3380 for (i = 0; i < 16; i++)
3381 INIT_LIST_HEAD(&ptype_base[i]);
3383 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3384 INIT_HLIST_HEAD(&dev_name_head[i]);
3386 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3387 INIT_HLIST_HEAD(&dev_index_head[i]);
3389 /*
3390 * Initialise the packet receive queues.
3391 */
3393 for_each_cpu(i) {
3394 struct softnet_data *queue;
3396 queue = &per_cpu(softnet_data, i);
3397 skb_queue_head_init(&queue->input_pkt_queue);
3398 queue->completion_queue = NULL;
3399 INIT_LIST_HEAD(&queue->poll_list);
3400 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3401 queue->backlog_dev.weight = weight_p;
3402 queue->backlog_dev.poll = process_backlog;
3403 atomic_set(&queue->backlog_dev.refcnt, 1);
3406 dev_boot_phase = 0;
3408 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3409 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3411 hotcpu_notifier(dev_cpu_callback, 0);
3412 dst_init();
3413 dev_mcast_init();
3414 rc = 0;
3415 out:
3416 return rc;
3419 subsys_initcall(net_dev_init);
3421 EXPORT_SYMBOL(__dev_get_by_index);
3422 EXPORT_SYMBOL(__dev_get_by_name);
3423 EXPORT_SYMBOL(__dev_remove_pack);
3424 EXPORT_SYMBOL(dev_valid_name);
3425 EXPORT_SYMBOL(dev_add_pack);
3426 EXPORT_SYMBOL(dev_alloc_name);
3427 EXPORT_SYMBOL(dev_close);
3428 EXPORT_SYMBOL(dev_get_by_flags);
3429 EXPORT_SYMBOL(dev_get_by_index);
3430 EXPORT_SYMBOL(dev_get_by_name);
3431 EXPORT_SYMBOL(dev_open);
3432 EXPORT_SYMBOL(dev_queue_xmit);
3433 EXPORT_SYMBOL(dev_remove_pack);
3434 EXPORT_SYMBOL(dev_set_allmulti);
3435 EXPORT_SYMBOL(dev_set_promiscuity);
3436 EXPORT_SYMBOL(dev_change_flags);
3437 EXPORT_SYMBOL(dev_set_mtu);
3438 EXPORT_SYMBOL(dev_set_mac_address);
3439 EXPORT_SYMBOL(free_netdev);
3440 EXPORT_SYMBOL(netdev_boot_setup_check);
3441 EXPORT_SYMBOL(netdev_set_master);
3442 EXPORT_SYMBOL(netdev_state_change);
3443 EXPORT_SYMBOL(netif_receive_skb);
3444 EXPORT_SYMBOL(netif_rx);
3445 EXPORT_SYMBOL(register_gifconf);
3446 EXPORT_SYMBOL(register_netdevice);
3447 EXPORT_SYMBOL(register_netdevice_notifier);
3448 EXPORT_SYMBOL(skb_checksum_help);
3449 EXPORT_SYMBOL(synchronize_net);
3450 EXPORT_SYMBOL(unregister_netdevice);
3451 EXPORT_SYMBOL(unregister_netdevice_notifier);
3452 EXPORT_SYMBOL(net_enable_timestamp);
3453 EXPORT_SYMBOL(net_disable_timestamp);
3454 EXPORT_SYMBOL(dev_get_flags);
3455 EXPORT_SYMBOL(skb_checksum_setup);
3457 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3458 EXPORT_SYMBOL(br_handle_frame_hook);
3459 EXPORT_SYMBOL(br_fdb_get_hook);
3460 EXPORT_SYMBOL(br_fdb_put_hook);
3461 #endif
3463 #ifdef CONFIG_KMOD
3464 EXPORT_SYMBOL(dev_load);
3465 #endif
3467 EXPORT_PER_CPU_SYMBOL(softnet_data);