[NET] gso: Fix up GSO packets with broken checksums
[deliverable/linux.git] / net / core / dev.c
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 */
74
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/bitops.h>
78 #include <linux/capability.h>
79 #include <linux/cpu.h>
80 #include <linux/types.h>
81 #include <linux/kernel.h>
82 #include <linux/sched.h>
83 #include <linux/mutex.h>
84 #include <linux/string.h>
85 #include <linux/mm.h>
86 #include <linux/socket.h>
87 #include <linux/sockios.h>
88 #include <linux/errno.h>
89 #include <linux/interrupt.h>
90 #include <linux/if_ether.h>
91 #include <linux/netdevice.h>
92 #include <linux/etherdevice.h>
93 #include <linux/notifier.h>
94 #include <linux/skbuff.h>
95 #include <net/sock.h>
96 #include <linux/rtnetlink.h>
97 #include <linux/proc_fs.h>
98 #include <linux/seq_file.h>
99 #include <linux/stat.h>
100 #include <linux/if_bridge.h>
101 #include <linux/divert.h>
102 #include <net/dst.h>
103 #include <net/pkt_sched.h>
104 #include <net/checksum.h>
105 #include <linux/highmem.h>
106 #include <linux/init.h>
107 #include <linux/kmod.h>
108 #include <linux/module.h>
109 #include <linux/kallsyms.h>
110 #include <linux/netpoll.h>
111 #include <linux/rcupdate.h>
112 #include <linux/delay.h>
113 #include <linux/wireless.h>
114 #include <net/iw_handler.h>
115 #include <asm/current.h>
116 #include <linux/audit.h>
117 #include <linux/dmaengine.h>
118 #include <linux/err.h>
119
120 /*
121 * The list of packet types we will receive (as opposed to discard)
122 * and the routines to invoke.
123 *
124 * Why 16. Because with 16 the only overlap we get on a hash of the
125 * low nibble of the protocol value is RARP/SNAP/X.25.
126 *
127 * NOTE: That is no longer true with the addition of VLAN tags. Not
128 * sure which should go first, but I bet it won't make much
129 * difference if we are running VLANs. The good news is that
130 * this protocol won't be in the list unless compiled in, so
131 * the average user (w/out VLANs) will not be adversely affected.
132 * --BLG
133 *
134 * 0800 IP
135 * 8100 802.1Q VLAN
136 * 0001 802.3
137 * 0002 AX.25
138 * 0004 802.2
139 * 8035 RARP
140 * 0005 SNAP
141 * 0805 X.25
142 * 0806 ARP
143 * 8137 IPX
144 * 0009 Localtalk
145 * 86DD IPv6
146 */
147
148 static DEFINE_SPINLOCK(ptype_lock);
149 static struct list_head ptype_base[16]; /* 16 way hashed list */
150 static struct list_head ptype_all; /* Taps */
151
152 #ifdef CONFIG_NET_DMA
153 static struct dma_client *net_dma_client;
154 static unsigned int net_dma_count;
155 static spinlock_t net_dma_event_lock;
156 #endif
157
158 /*
159 * The @dev_base list is protected by @dev_base_lock and the rtnl
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);
180
181 EXPORT_SYMBOL(dev_base);
182 EXPORT_SYMBOL(dev_base_lock);
183
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];
187
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 }
193
194 static inline struct hlist_head *dev_index_hash(int ifindex)
195 {
196 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
197 }
198
199 /*
200 * Our notifier list
201 */
202
203 static RAW_NOTIFIER_HEAD(netdev_chain);
204
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 };
210
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
220
221
222 /*******************************************************************************
223
224 Protocol management and registration routines
225
226 *******************************************************************************/
227
228 /*
229 * For efficiency
230 */
231
232 static int netdev_nit;
233
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 */
249
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 */
262
263 void dev_add_pack(struct packet_type *pt)
264 {
265 int hash;
266
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 }
277
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;
295
296 spin_lock_bh(&ptype_lock);
297
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];
303
304 list_for_each_entry(pt1, head, list) {
305 if (pt == pt1) {
306 list_del_rcu(&pt->list);
307 goto out;
308 }
309 }
310
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);
330
331 synchronize_net();
332 }
333
334 /******************************************************************************
335
336 Device Boot-time Settings Routines
337
338 *******************************************************************************/
339
340 /* Boot time configuration table */
341 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
342
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;
356
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 }
366
367 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
368 }
369
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;
383
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 }
396
397
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;
413
414 sprintf(name, "%s%d", prefix, unit);
415
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;
422
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 }
428
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;
436
437 str = get_options(str, ARRAY_SIZE(ints), ints);
438 if (!str || !*str)
439 return 0;
440
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];
451
452 /* Add new entry to the list */
453 return netdev_boot_setup_add(str, &map);
454 }
455
456 __setup("netdev=", netdev_boot_setup);
457
458 /*******************************************************************************
459
460 Device Interface Subroutines
461
462 *******************************************************************************/
463
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 */
474
475 struct net_device *__dev_get_by_name(const char *name)
476 {
477 struct hlist_node *p;
478
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 }
487
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 */
498
499 struct net_device *dev_get_by_name(const char *name)
500 {
501 struct net_device *dev;
502
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 }
510
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 */
521
522 struct net_device *__dev_get_by_index(int ifindex)
523 {
524 struct hlist_node *p;
525
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 }
534
535
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 */
545
546 struct net_device *dev_get_by_index(int ifindex)
547 {
548 struct net_device *dev;
549
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 }
557
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 */
571
572 struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
573 {
574 struct net_device *dev;
575
576 ASSERT_RTNL();
577
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 }
584
585 EXPORT_SYMBOL(dev_getbyhwaddr);
586
587 struct net_device *dev_getfirstbyhwtype(unsigned short type)
588 {
589 struct net_device *dev;
590
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 }
601
602 EXPORT_SYMBOL(dev_getfirstbyhwtype);
603
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 */
614
615 struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
616 {
617 struct net_device *dev;
618
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 }
629
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 }
644
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. It scans list of devices to build up a free map, then chooses
652 * the first empty slot. The caller must hold the dev_base or rtnl lock
653 * while allocating the name and adding the device in order to avoid
654 * duplicates.
655 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
656 * Returns the number of the unit assigned or a negative errno code.
657 */
658
659 int dev_alloc_name(struct net_device *dev, const char *name)
660 {
661 int i = 0;
662 char buf[IFNAMSIZ];
663 const char *p;
664 const int max_netdevices = 8*PAGE_SIZE;
665 long *inuse;
666 struct net_device *d;
667
668 p = strnchr(name, IFNAMSIZ-1, '%');
669 if (p) {
670 /*
671 * Verify the string as this thing may have come from
672 * the user. There must be either one "%d" and no other "%"
673 * characters.
674 */
675 if (p[1] != 'd' || strchr(p + 2, '%'))
676 return -EINVAL;
677
678 /* Use one page as a bit array of possible slots */
679 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
680 if (!inuse)
681 return -ENOMEM;
682
683 for (d = dev_base; d; d = d->next) {
684 if (!sscanf(d->name, name, &i))
685 continue;
686 if (i < 0 || i >= max_netdevices)
687 continue;
688
689 /* avoid cases where sscanf is not exact inverse of printf */
690 snprintf(buf, sizeof(buf), name, i);
691 if (!strncmp(buf, d->name, IFNAMSIZ))
692 set_bit(i, inuse);
693 }
694
695 i = find_first_zero_bit(inuse, max_netdevices);
696 free_page((unsigned long) inuse);
697 }
698
699 snprintf(buf, sizeof(buf), name, i);
700 if (!__dev_get_by_name(buf)) {
701 strlcpy(dev->name, buf, IFNAMSIZ);
702 return i;
703 }
704
705 /* It is possible to run out of possible slots
706 * when the name is long and there isn't enough space left
707 * for the digits, or if all bits are used.
708 */
709 return -ENFILE;
710 }
711
712
713 /**
714 * dev_change_name - change name of a device
715 * @dev: device
716 * @newname: name (or format string) must be at least IFNAMSIZ
717 *
718 * Change name of a device, can pass format strings "eth%d".
719 * for wildcarding.
720 */
721 int dev_change_name(struct net_device *dev, char *newname)
722 {
723 int err = 0;
724
725 ASSERT_RTNL();
726
727 if (dev->flags & IFF_UP)
728 return -EBUSY;
729
730 if (!dev_valid_name(newname))
731 return -EINVAL;
732
733 if (strchr(newname, '%')) {
734 err = dev_alloc_name(dev, newname);
735 if (err < 0)
736 return err;
737 strcpy(newname, dev->name);
738 }
739 else if (__dev_get_by_name(newname))
740 return -EEXIST;
741 else
742 strlcpy(dev->name, newname, IFNAMSIZ);
743
744 err = class_device_rename(&dev->class_dev, dev->name);
745 if (!err) {
746 hlist_del(&dev->name_hlist);
747 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
748 raw_notifier_call_chain(&netdev_chain,
749 NETDEV_CHANGENAME, dev);
750 }
751
752 return err;
753 }
754
755 /**
756 * netdev_features_change - device changes features
757 * @dev: device to cause notification
758 *
759 * Called to indicate a device has changed features.
760 */
761 void netdev_features_change(struct net_device *dev)
762 {
763 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
764 }
765 EXPORT_SYMBOL(netdev_features_change);
766
767 /**
768 * netdev_state_change - device changes state
769 * @dev: device to cause notification
770 *
771 * Called to indicate a device has changed state. This function calls
772 * the notifier chains for netdev_chain and sends a NEWLINK message
773 * to the routing socket.
774 */
775 void netdev_state_change(struct net_device *dev)
776 {
777 if (dev->flags & IFF_UP) {
778 raw_notifier_call_chain(&netdev_chain,
779 NETDEV_CHANGE, dev);
780 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
781 }
782 }
783
784 /**
785 * dev_load - load a network module
786 * @name: name of interface
787 *
788 * If a network interface is not present and the process has suitable
789 * privileges this function loads the module. If module loading is not
790 * available in this kernel then it becomes a nop.
791 */
792
793 void dev_load(const char *name)
794 {
795 struct net_device *dev;
796
797 read_lock(&dev_base_lock);
798 dev = __dev_get_by_name(name);
799 read_unlock(&dev_base_lock);
800
801 if (!dev && capable(CAP_SYS_MODULE))
802 request_module("%s", name);
803 }
804
805 static int default_rebuild_header(struct sk_buff *skb)
806 {
807 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
808 skb->dev ? skb->dev->name : "NULL!!!");
809 kfree_skb(skb);
810 return 1;
811 }
812
813
814 /**
815 * dev_open - prepare an interface for use.
816 * @dev: device to open
817 *
818 * Takes a device from down to up state. The device's private open
819 * function is invoked and then the multicast lists are loaded. Finally
820 * the device is moved into the up state and a %NETDEV_UP message is
821 * sent to the netdev notifier chain.
822 *
823 * Calling this function on an active interface is a nop. On a failure
824 * a negative errno code is returned.
825 */
826 int dev_open(struct net_device *dev)
827 {
828 int ret = 0;
829
830 /*
831 * Is it already up?
832 */
833
834 if (dev->flags & IFF_UP)
835 return 0;
836
837 /*
838 * Is it even present?
839 */
840 if (!netif_device_present(dev))
841 return -ENODEV;
842
843 /*
844 * Call device private open method
845 */
846 set_bit(__LINK_STATE_START, &dev->state);
847 if (dev->open) {
848 ret = dev->open(dev);
849 if (ret)
850 clear_bit(__LINK_STATE_START, &dev->state);
851 }
852
853 /*
854 * If it went open OK then:
855 */
856
857 if (!ret) {
858 /*
859 * Set the flags.
860 */
861 dev->flags |= IFF_UP;
862
863 /*
864 * Initialize multicasting status
865 */
866 dev_mc_upload(dev);
867
868 /*
869 * Wakeup transmit queue engine
870 */
871 dev_activate(dev);
872
873 /*
874 * ... and announce new interface.
875 */
876 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
877 }
878 return ret;
879 }
880
881 /**
882 * dev_close - shutdown an interface.
883 * @dev: device to shutdown
884 *
885 * This function moves an active device into down state. A
886 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
887 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
888 * chain.
889 */
890 int dev_close(struct net_device *dev)
891 {
892 if (!(dev->flags & IFF_UP))
893 return 0;
894
895 /*
896 * Tell people we are going down, so that they can
897 * prepare to death, when device is still operating.
898 */
899 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
900
901 dev_deactivate(dev);
902
903 clear_bit(__LINK_STATE_START, &dev->state);
904
905 /* Synchronize to scheduled poll. We cannot touch poll list,
906 * it can be even on different cpu. So just clear netif_running(),
907 * and wait when poll really will happen. Actually, the best place
908 * for this is inside dev->stop() after device stopped its irq
909 * engine, but this requires more changes in devices. */
910
911 smp_mb__after_clear_bit(); /* Commit netif_running(). */
912 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
913 /* No hurry. */
914 msleep(1);
915 }
916
917 /*
918 * Call the device specific close. This cannot fail.
919 * Only if device is UP
920 *
921 * We allow it to be called even after a DETACH hot-plug
922 * event.
923 */
924 if (dev->stop)
925 dev->stop(dev);
926
927 /*
928 * Device is now down.
929 */
930
931 dev->flags &= ~IFF_UP;
932
933 /*
934 * Tell people we are down
935 */
936 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
937
938 return 0;
939 }
940
941
942 /*
943 * Device change register/unregister. These are not inline or static
944 * as we export them to the world.
945 */
946
947 /**
948 * register_netdevice_notifier - register a network notifier block
949 * @nb: notifier
950 *
951 * Register a notifier to be called when network device events occur.
952 * The notifier passed is linked into the kernel structures and must
953 * not be reused until it has been unregistered. A negative errno code
954 * is returned on a failure.
955 *
956 * When registered all registration and up events are replayed
957 * to the new notifier to allow device to have a race free
958 * view of the network device list.
959 */
960
961 int register_netdevice_notifier(struct notifier_block *nb)
962 {
963 struct net_device *dev;
964 int err;
965
966 rtnl_lock();
967 err = raw_notifier_chain_register(&netdev_chain, nb);
968 if (!err) {
969 for (dev = dev_base; dev; dev = dev->next) {
970 nb->notifier_call(nb, NETDEV_REGISTER, dev);
971
972 if (dev->flags & IFF_UP)
973 nb->notifier_call(nb, NETDEV_UP, dev);
974 }
975 }
976 rtnl_unlock();
977 return err;
978 }
979
980 /**
981 * unregister_netdevice_notifier - unregister a network notifier block
982 * @nb: notifier
983 *
984 * Unregister a notifier previously registered by
985 * register_netdevice_notifier(). The notifier is unlinked into the
986 * kernel structures and may then be reused. A negative errno code
987 * is returned on a failure.
988 */
989
990 int unregister_netdevice_notifier(struct notifier_block *nb)
991 {
992 int err;
993
994 rtnl_lock();
995 err = raw_notifier_chain_unregister(&netdev_chain, nb);
996 rtnl_unlock();
997 return err;
998 }
999
1000 /**
1001 * call_netdevice_notifiers - call all network notifier blocks
1002 * @val: value passed unmodified to notifier function
1003 * @v: pointer passed unmodified to notifier function
1004 *
1005 * Call all network notifier blocks. Parameters and return value
1006 * are as for raw_notifier_call_chain().
1007 */
1008
1009 int call_netdevice_notifiers(unsigned long val, void *v)
1010 {
1011 return raw_notifier_call_chain(&netdev_chain, val, v);
1012 }
1013
1014 /* When > 0 there are consumers of rx skb time stamps */
1015 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1016
1017 void net_enable_timestamp(void)
1018 {
1019 atomic_inc(&netstamp_needed);
1020 }
1021
1022 void net_disable_timestamp(void)
1023 {
1024 atomic_dec(&netstamp_needed);
1025 }
1026
1027 void __net_timestamp(struct sk_buff *skb)
1028 {
1029 struct timeval tv;
1030
1031 do_gettimeofday(&tv);
1032 skb_set_timestamp(skb, &tv);
1033 }
1034 EXPORT_SYMBOL(__net_timestamp);
1035
1036 static inline void net_timestamp(struct sk_buff *skb)
1037 {
1038 if (atomic_read(&netstamp_needed))
1039 __net_timestamp(skb);
1040 else {
1041 skb->tstamp.off_sec = 0;
1042 skb->tstamp.off_usec = 0;
1043 }
1044 }
1045
1046 /*
1047 * Support routine. Sends outgoing frames to any network
1048 * taps currently in use.
1049 */
1050
1051 static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1052 {
1053 struct packet_type *ptype;
1054
1055 net_timestamp(skb);
1056
1057 rcu_read_lock();
1058 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1059 /* Never send packets back to the socket
1060 * they originated from - MvS (miquels@drinkel.ow.org)
1061 */
1062 if ((ptype->dev == dev || !ptype->dev) &&
1063 (ptype->af_packet_priv == NULL ||
1064 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1065 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1066 if (!skb2)
1067 break;
1068
1069 /* skb->nh should be correctly
1070 set by sender, so that the second statement is
1071 just protection against buggy protocols.
1072 */
1073 skb2->mac.raw = skb2->data;
1074
1075 if (skb2->nh.raw < skb2->data ||
1076 skb2->nh.raw > skb2->tail) {
1077 if (net_ratelimit())
1078 printk(KERN_CRIT "protocol %04x is "
1079 "buggy, dev %s\n",
1080 skb2->protocol, dev->name);
1081 skb2->nh.raw = skb2->data;
1082 }
1083
1084 skb2->h.raw = skb2->nh.raw;
1085 skb2->pkt_type = PACKET_OUTGOING;
1086 ptype->func(skb2, skb->dev, ptype, skb->dev);
1087 }
1088 }
1089 rcu_read_unlock();
1090 }
1091
1092
1093 void __netif_schedule(struct net_device *dev)
1094 {
1095 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1096 unsigned long flags;
1097 struct softnet_data *sd;
1098
1099 local_irq_save(flags);
1100 sd = &__get_cpu_var(softnet_data);
1101 dev->next_sched = sd->output_queue;
1102 sd->output_queue = dev;
1103 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1104 local_irq_restore(flags);
1105 }
1106 }
1107 EXPORT_SYMBOL(__netif_schedule);
1108
1109 void __netif_rx_schedule(struct net_device *dev)
1110 {
1111 unsigned long flags;
1112
1113 local_irq_save(flags);
1114 dev_hold(dev);
1115 list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
1116 if (dev->quota < 0)
1117 dev->quota += dev->weight;
1118 else
1119 dev->quota = dev->weight;
1120 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1121 local_irq_restore(flags);
1122 }
1123 EXPORT_SYMBOL(__netif_rx_schedule);
1124
1125 void dev_kfree_skb_any(struct sk_buff *skb)
1126 {
1127 if (in_irq() || irqs_disabled())
1128 dev_kfree_skb_irq(skb);
1129 else
1130 dev_kfree_skb(skb);
1131 }
1132 EXPORT_SYMBOL(dev_kfree_skb_any);
1133
1134
1135 /* Hot-plugging. */
1136 void netif_device_detach(struct net_device *dev)
1137 {
1138 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1139 netif_running(dev)) {
1140 netif_stop_queue(dev);
1141 }
1142 }
1143 EXPORT_SYMBOL(netif_device_detach);
1144
1145 void netif_device_attach(struct net_device *dev)
1146 {
1147 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1148 netif_running(dev)) {
1149 netif_wake_queue(dev);
1150 __netdev_watchdog_up(dev);
1151 }
1152 }
1153 EXPORT_SYMBOL(netif_device_attach);
1154
1155
1156 /*
1157 * Invalidate hardware checksum when packet is to be mangled, and
1158 * complete checksum manually on outgoing path.
1159 */
1160 int skb_checksum_help(struct sk_buff *skb, int inward)
1161 {
1162 unsigned int csum;
1163 int ret = 0, offset = skb->h.raw - skb->data;
1164
1165 if (inward)
1166 goto out_set_summed;
1167
1168 if (unlikely(skb_shinfo(skb)->gso_size)) {
1169 static int warned;
1170
1171 WARN_ON(!warned);
1172 warned = 1;
1173
1174 /* Let GSO fix up the checksum. */
1175 goto out_set_summed;
1176 }
1177
1178 if (skb_cloned(skb)) {
1179 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1180 if (ret)
1181 goto out;
1182 }
1183
1184 BUG_ON(offset > (int)skb->len);
1185 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1186
1187 offset = skb->tail - skb->h.raw;
1188 BUG_ON(offset <= 0);
1189 BUG_ON(skb->csum + 2 > offset);
1190
1191 *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
1192
1193 out_set_summed:
1194 skb->ip_summed = CHECKSUM_NONE;
1195 out:
1196 return ret;
1197 }
1198
1199 /**
1200 * skb_gso_segment - Perform segmentation on skb.
1201 * @skb: buffer to segment
1202 * @features: features for the output path (see dev->features)
1203 *
1204 * This function segments the given skb and returns a list of segments.
1205 *
1206 * It may return NULL if the skb requires no segmentation. This is
1207 * only possible when GSO is used for verifying header integrity.
1208 */
1209 struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
1210 {
1211 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1212 struct packet_type *ptype;
1213 int type = skb->protocol;
1214 int err;
1215
1216 BUG_ON(skb_shinfo(skb)->frag_list);
1217
1218 skb->mac.raw = skb->data;
1219 skb->mac_len = skb->nh.raw - skb->data;
1220 __skb_pull(skb, skb->mac_len);
1221
1222 if (unlikely(skb->ip_summed != CHECKSUM_HW)) {
1223 static int warned;
1224
1225 WARN_ON(!warned);
1226 warned = 1;
1227
1228 if (skb_header_cloned(skb) &&
1229 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1230 return ERR_PTR(err);
1231 }
1232
1233 rcu_read_lock();
1234 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1235 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
1236 if (unlikely(skb->ip_summed != CHECKSUM_HW)) {
1237 err = ptype->gso_send_check(skb);
1238 segs = ERR_PTR(err);
1239 if (err || skb_gso_ok(skb, features))
1240 break;
1241 __skb_push(skb, skb->data - skb->nh.raw);
1242 }
1243 segs = ptype->gso_segment(skb, features);
1244 break;
1245 }
1246 }
1247 rcu_read_unlock();
1248
1249 __skb_push(skb, skb->data - skb->mac.raw);
1250
1251 return segs;
1252 }
1253
1254 EXPORT_SYMBOL(skb_gso_segment);
1255
1256 /* Take action when hardware reception checksum errors are detected. */
1257 #ifdef CONFIG_BUG
1258 void netdev_rx_csum_fault(struct net_device *dev)
1259 {
1260 if (net_ratelimit()) {
1261 printk(KERN_ERR "%s: hw csum failure.\n",
1262 dev ? dev->name : "<unknown>");
1263 dump_stack();
1264 }
1265 }
1266 EXPORT_SYMBOL(netdev_rx_csum_fault);
1267 #endif
1268
1269 /* Actually, we should eliminate this check as soon as we know, that:
1270 * 1. IOMMU is present and allows to map all the memory.
1271 * 2. No high memory really exists on this machine.
1272 */
1273
1274 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1275 {
1276 #ifdef CONFIG_HIGHMEM
1277 int i;
1278
1279 if (dev->features & NETIF_F_HIGHDMA)
1280 return 0;
1281
1282 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1283 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1284 return 1;
1285
1286 #endif
1287 return 0;
1288 }
1289
1290 struct dev_gso_cb {
1291 void (*destructor)(struct sk_buff *skb);
1292 };
1293
1294 #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1295
1296 static void dev_gso_skb_destructor(struct sk_buff *skb)
1297 {
1298 struct dev_gso_cb *cb;
1299
1300 do {
1301 struct sk_buff *nskb = skb->next;
1302
1303 skb->next = nskb->next;
1304 nskb->next = NULL;
1305 kfree_skb(nskb);
1306 } while (skb->next);
1307
1308 cb = DEV_GSO_CB(skb);
1309 if (cb->destructor)
1310 cb->destructor(skb);
1311 }
1312
1313 /**
1314 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1315 * @skb: buffer to segment
1316 *
1317 * This function segments the given skb and stores the list of segments
1318 * in skb->next.
1319 */
1320 static int dev_gso_segment(struct sk_buff *skb)
1321 {
1322 struct net_device *dev = skb->dev;
1323 struct sk_buff *segs;
1324 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1325 NETIF_F_SG : 0);
1326
1327 segs = skb_gso_segment(skb, features);
1328
1329 /* Verifying header integrity only. */
1330 if (!segs)
1331 return 0;
1332
1333 if (unlikely(IS_ERR(segs)))
1334 return PTR_ERR(segs);
1335
1336 skb->next = segs;
1337 DEV_GSO_CB(skb)->destructor = skb->destructor;
1338 skb->destructor = dev_gso_skb_destructor;
1339
1340 return 0;
1341 }
1342
1343 int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1344 {
1345 if (likely(!skb->next)) {
1346 if (netdev_nit)
1347 dev_queue_xmit_nit(skb, dev);
1348
1349 if (netif_needs_gso(dev, skb)) {
1350 if (unlikely(dev_gso_segment(skb)))
1351 goto out_kfree_skb;
1352 if (skb->next)
1353 goto gso;
1354 }
1355
1356 return dev->hard_start_xmit(skb, dev);
1357 }
1358
1359 gso:
1360 do {
1361 struct sk_buff *nskb = skb->next;
1362 int rc;
1363
1364 skb->next = nskb->next;
1365 nskb->next = NULL;
1366 rc = dev->hard_start_xmit(nskb, dev);
1367 if (unlikely(rc)) {
1368 nskb->next = skb->next;
1369 skb->next = nskb;
1370 return rc;
1371 }
1372 if (unlikely(netif_queue_stopped(dev) && skb->next))
1373 return NETDEV_TX_BUSY;
1374 } while (skb->next);
1375
1376 skb->destructor = DEV_GSO_CB(skb)->destructor;
1377
1378 out_kfree_skb:
1379 kfree_skb(skb);
1380 return 0;
1381 }
1382
1383 #define HARD_TX_LOCK(dev, cpu) { \
1384 if ((dev->features & NETIF_F_LLTX) == 0) { \
1385 netif_tx_lock(dev); \
1386 } \
1387 }
1388
1389 #define HARD_TX_UNLOCK(dev) { \
1390 if ((dev->features & NETIF_F_LLTX) == 0) { \
1391 netif_tx_unlock(dev); \
1392 } \
1393 }
1394
1395 /**
1396 * dev_queue_xmit - transmit a buffer
1397 * @skb: buffer to transmit
1398 *
1399 * Queue a buffer for transmission to a network device. The caller must
1400 * have set the device and priority and built the buffer before calling
1401 * this function. The function can be called from an interrupt.
1402 *
1403 * A negative errno code is returned on a failure. A success does not
1404 * guarantee the frame will be transmitted as it may be dropped due
1405 * to congestion or traffic shaping.
1406 *
1407 * -----------------------------------------------------------------------------------
1408 * I notice this method can also return errors from the queue disciplines,
1409 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1410 * be positive.
1411 *
1412 * Regardless of the return value, the skb is consumed, so it is currently
1413 * difficult to retry a send to this method. (You can bump the ref count
1414 * before sending to hold a reference for retry if you are careful.)
1415 *
1416 * When calling this method, interrupts MUST be enabled. This is because
1417 * the BH enable code must have IRQs enabled so that it will not deadlock.
1418 * --BLG
1419 */
1420
1421 int dev_queue_xmit(struct sk_buff *skb)
1422 {
1423 struct net_device *dev = skb->dev;
1424 struct Qdisc *q;
1425 int rc = -ENOMEM;
1426
1427 /* GSO will handle the following emulations directly. */
1428 if (netif_needs_gso(dev, skb))
1429 goto gso;
1430
1431 if (skb_shinfo(skb)->frag_list &&
1432 !(dev->features & NETIF_F_FRAGLIST) &&
1433 __skb_linearize(skb))
1434 goto out_kfree_skb;
1435
1436 /* Fragmented skb is linearized if device does not support SG,
1437 * or if at least one of fragments is in highmem and device
1438 * does not support DMA from it.
1439 */
1440 if (skb_shinfo(skb)->nr_frags &&
1441 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1442 __skb_linearize(skb))
1443 goto out_kfree_skb;
1444
1445 /* If packet is not checksummed and device does not support
1446 * checksumming for this protocol, complete checksumming here.
1447 */
1448 if (skb->ip_summed == CHECKSUM_HW &&
1449 (!(dev->features & NETIF_F_GEN_CSUM) &&
1450 (!(dev->features & NETIF_F_IP_CSUM) ||
1451 skb->protocol != htons(ETH_P_IP))))
1452 if (skb_checksum_help(skb, 0))
1453 goto out_kfree_skb;
1454
1455 gso:
1456 spin_lock_prefetch(&dev->queue_lock);
1457
1458 /* Disable soft irqs for various locks below. Also
1459 * stops preemption for RCU.
1460 */
1461 rcu_read_lock_bh();
1462
1463 /* Updates of qdisc are serialized by queue_lock.
1464 * The struct Qdisc which is pointed to by qdisc is now a
1465 * rcu structure - it may be accessed without acquiring
1466 * a lock (but the structure may be stale.) The freeing of the
1467 * qdisc will be deferred until it's known that there are no
1468 * more references to it.
1469 *
1470 * If the qdisc has an enqueue function, we still need to
1471 * hold the queue_lock before calling it, since queue_lock
1472 * also serializes access to the device queue.
1473 */
1474
1475 q = rcu_dereference(dev->qdisc);
1476 #ifdef CONFIG_NET_CLS_ACT
1477 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1478 #endif
1479 if (q->enqueue) {
1480 /* Grab device queue */
1481 spin_lock(&dev->queue_lock);
1482
1483 rc = q->enqueue(skb, q);
1484
1485 qdisc_run(dev);
1486
1487 spin_unlock(&dev->queue_lock);
1488 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1489 goto out;
1490 }
1491
1492 /* The device has no queue. Common case for software devices:
1493 loopback, all the sorts of tunnels...
1494
1495 Really, it is unlikely that netif_tx_lock protection is necessary
1496 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1497 counters.)
1498 However, it is possible, that they rely on protection
1499 made by us here.
1500
1501 Check this and shot the lock. It is not prone from deadlocks.
1502 Either shot noqueue qdisc, it is even simpler 8)
1503 */
1504 if (dev->flags & IFF_UP) {
1505 int cpu = smp_processor_id(); /* ok because BHs are off */
1506
1507 if (dev->xmit_lock_owner != cpu) {
1508
1509 HARD_TX_LOCK(dev, cpu);
1510
1511 if (!netif_queue_stopped(dev)) {
1512 rc = 0;
1513 if (!dev_hard_start_xmit(skb, dev)) {
1514 HARD_TX_UNLOCK(dev);
1515 goto out;
1516 }
1517 }
1518 HARD_TX_UNLOCK(dev);
1519 if (net_ratelimit())
1520 printk(KERN_CRIT "Virtual device %s asks to "
1521 "queue packet!\n", dev->name);
1522 } else {
1523 /* Recursion is detected! It is possible,
1524 * unfortunately */
1525 if (net_ratelimit())
1526 printk(KERN_CRIT "Dead loop on virtual device "
1527 "%s, fix it urgently!\n", dev->name);
1528 }
1529 }
1530
1531 rc = -ENETDOWN;
1532 rcu_read_unlock_bh();
1533
1534 out_kfree_skb:
1535 kfree_skb(skb);
1536 return rc;
1537 out:
1538 rcu_read_unlock_bh();
1539 return rc;
1540 }
1541
1542
1543 /*=======================================================================
1544 Receiver routines
1545 =======================================================================*/
1546
1547 int netdev_max_backlog = 1000;
1548 int netdev_budget = 300;
1549 int weight_p = 64; /* old backlog weight */
1550
1551 DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1552
1553
1554 /**
1555 * netif_rx - post buffer to the network code
1556 * @skb: buffer to post
1557 *
1558 * This function receives a packet from a device driver and queues it for
1559 * the upper (protocol) levels to process. It always succeeds. The buffer
1560 * may be dropped during processing for congestion control or by the
1561 * protocol layers.
1562 *
1563 * return values:
1564 * NET_RX_SUCCESS (no congestion)
1565 * NET_RX_CN_LOW (low congestion)
1566 * NET_RX_CN_MOD (moderate congestion)
1567 * NET_RX_CN_HIGH (high congestion)
1568 * NET_RX_DROP (packet was dropped)
1569 *
1570 */
1571
1572 int netif_rx(struct sk_buff *skb)
1573 {
1574 struct softnet_data *queue;
1575 unsigned long flags;
1576
1577 /* if netpoll wants it, pretend we never saw it */
1578 if (netpoll_rx(skb))
1579 return NET_RX_DROP;
1580
1581 if (!skb->tstamp.off_sec)
1582 net_timestamp(skb);
1583
1584 /*
1585 * The code is rearranged so that the path is the most
1586 * short when CPU is congested, but is still operating.
1587 */
1588 local_irq_save(flags);
1589 queue = &__get_cpu_var(softnet_data);
1590
1591 __get_cpu_var(netdev_rx_stat).total++;
1592 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1593 if (queue->input_pkt_queue.qlen) {
1594 enqueue:
1595 dev_hold(skb->dev);
1596 __skb_queue_tail(&queue->input_pkt_queue, skb);
1597 local_irq_restore(flags);
1598 return NET_RX_SUCCESS;
1599 }
1600
1601 netif_rx_schedule(&queue->backlog_dev);
1602 goto enqueue;
1603 }
1604
1605 __get_cpu_var(netdev_rx_stat).dropped++;
1606 local_irq_restore(flags);
1607
1608 kfree_skb(skb);
1609 return NET_RX_DROP;
1610 }
1611
1612 int netif_rx_ni(struct sk_buff *skb)
1613 {
1614 int err;
1615
1616 preempt_disable();
1617 err = netif_rx(skb);
1618 if (local_softirq_pending())
1619 do_softirq();
1620 preempt_enable();
1621
1622 return err;
1623 }
1624
1625 EXPORT_SYMBOL(netif_rx_ni);
1626
1627 static inline struct net_device *skb_bond(struct sk_buff *skb)
1628 {
1629 struct net_device *dev = skb->dev;
1630
1631 if (dev->master) {
1632 /*
1633 * On bonding slaves other than the currently active
1634 * slave, suppress duplicates except for 802.3ad
1635 * ETH_P_SLOW and alb non-mcast/bcast.
1636 */
1637 if (dev->priv_flags & IFF_SLAVE_INACTIVE) {
1638 if (dev->master->priv_flags & IFF_MASTER_ALB) {
1639 if (skb->pkt_type != PACKET_BROADCAST &&
1640 skb->pkt_type != PACKET_MULTICAST)
1641 goto keep;
1642 }
1643
1644 if (dev->master->priv_flags & IFF_MASTER_8023AD &&
1645 skb->protocol == __constant_htons(ETH_P_SLOW))
1646 goto keep;
1647
1648 kfree_skb(skb);
1649 return NULL;
1650 }
1651 keep:
1652 skb->dev = dev->master;
1653 }
1654
1655 return dev;
1656 }
1657
1658 static void net_tx_action(struct softirq_action *h)
1659 {
1660 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1661
1662 if (sd->completion_queue) {
1663 struct sk_buff *clist;
1664
1665 local_irq_disable();
1666 clist = sd->completion_queue;
1667 sd->completion_queue = NULL;
1668 local_irq_enable();
1669
1670 while (clist) {
1671 struct sk_buff *skb = clist;
1672 clist = clist->next;
1673
1674 BUG_TRAP(!atomic_read(&skb->users));
1675 __kfree_skb(skb);
1676 }
1677 }
1678
1679 if (sd->output_queue) {
1680 struct net_device *head;
1681
1682 local_irq_disable();
1683 head = sd->output_queue;
1684 sd->output_queue = NULL;
1685 local_irq_enable();
1686
1687 while (head) {
1688 struct net_device *dev = head;
1689 head = head->next_sched;
1690
1691 smp_mb__before_clear_bit();
1692 clear_bit(__LINK_STATE_SCHED, &dev->state);
1693
1694 if (spin_trylock(&dev->queue_lock)) {
1695 qdisc_run(dev);
1696 spin_unlock(&dev->queue_lock);
1697 } else {
1698 netif_schedule(dev);
1699 }
1700 }
1701 }
1702 }
1703
1704 static __inline__ int deliver_skb(struct sk_buff *skb,
1705 struct packet_type *pt_prev,
1706 struct net_device *orig_dev)
1707 {
1708 atomic_inc(&skb->users);
1709 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1710 }
1711
1712 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1713 int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1714 struct net_bridge;
1715 struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1716 unsigned char *addr);
1717 void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
1718
1719 static __inline__ int handle_bridge(struct sk_buff **pskb,
1720 struct packet_type **pt_prev, int *ret,
1721 struct net_device *orig_dev)
1722 {
1723 struct net_bridge_port *port;
1724
1725 if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1726 (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1727 return 0;
1728
1729 if (*pt_prev) {
1730 *ret = deliver_skb(*pskb, *pt_prev, orig_dev);
1731 *pt_prev = NULL;
1732 }
1733
1734 return br_handle_frame_hook(port, pskb);
1735 }
1736 #else
1737 #define handle_bridge(skb, pt_prev, ret, orig_dev) (0)
1738 #endif
1739
1740 #ifdef CONFIG_NET_CLS_ACT
1741 /* TODO: Maybe we should just force sch_ingress to be compiled in
1742 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1743 * a compare and 2 stores extra right now if we dont have it on
1744 * but have CONFIG_NET_CLS_ACT
1745 * NOTE: This doesnt stop any functionality; if you dont have
1746 * the ingress scheduler, you just cant add policies on ingress.
1747 *
1748 */
1749 static int ing_filter(struct sk_buff *skb)
1750 {
1751 struct Qdisc *q;
1752 struct net_device *dev = skb->dev;
1753 int result = TC_ACT_OK;
1754
1755 if (dev->qdisc_ingress) {
1756 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1757 if (MAX_RED_LOOP < ttl++) {
1758 printk(KERN_WARNING "Redir loop detected Dropping packet (%s->%s)\n",
1759 skb->input_dev->name, skb->dev->name);
1760 return TC_ACT_SHOT;
1761 }
1762
1763 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1764
1765 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1766
1767 spin_lock(&dev->ingress_lock);
1768 if ((q = dev->qdisc_ingress) != NULL)
1769 result = q->enqueue(skb, q);
1770 spin_unlock(&dev->ingress_lock);
1771
1772 }
1773
1774 return result;
1775 }
1776 #endif
1777
1778 int netif_receive_skb(struct sk_buff *skb)
1779 {
1780 struct packet_type *ptype, *pt_prev;
1781 struct net_device *orig_dev;
1782 int ret = NET_RX_DROP;
1783 unsigned short type;
1784
1785 /* if we've gotten here through NAPI, check netpoll */
1786 if (skb->dev->poll && netpoll_rx(skb))
1787 return NET_RX_DROP;
1788
1789 if (!skb->tstamp.off_sec)
1790 net_timestamp(skb);
1791
1792 if (!skb->input_dev)
1793 skb->input_dev = skb->dev;
1794
1795 orig_dev = skb_bond(skb);
1796
1797 if (!orig_dev)
1798 return NET_RX_DROP;
1799
1800 __get_cpu_var(netdev_rx_stat).total++;
1801
1802 skb->h.raw = skb->nh.raw = skb->data;
1803 skb->mac_len = skb->nh.raw - skb->mac.raw;
1804
1805 pt_prev = NULL;
1806
1807 rcu_read_lock();
1808
1809 #ifdef CONFIG_NET_CLS_ACT
1810 if (skb->tc_verd & TC_NCLS) {
1811 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1812 goto ncls;
1813 }
1814 #endif
1815
1816 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1817 if (!ptype->dev || ptype->dev == skb->dev) {
1818 if (pt_prev)
1819 ret = deliver_skb(skb, pt_prev, orig_dev);
1820 pt_prev = ptype;
1821 }
1822 }
1823
1824 #ifdef CONFIG_NET_CLS_ACT
1825 if (pt_prev) {
1826 ret = deliver_skb(skb, pt_prev, orig_dev);
1827 pt_prev = NULL; /* noone else should process this after*/
1828 } else {
1829 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1830 }
1831
1832 ret = ing_filter(skb);
1833
1834 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1835 kfree_skb(skb);
1836 goto out;
1837 }
1838
1839 skb->tc_verd = 0;
1840 ncls:
1841 #endif
1842
1843 handle_diverter(skb);
1844
1845 if (handle_bridge(&skb, &pt_prev, &ret, orig_dev))
1846 goto out;
1847
1848 type = skb->protocol;
1849 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1850 if (ptype->type == type &&
1851 (!ptype->dev || ptype->dev == skb->dev)) {
1852 if (pt_prev)
1853 ret = deliver_skb(skb, pt_prev, orig_dev);
1854 pt_prev = ptype;
1855 }
1856 }
1857
1858 if (pt_prev) {
1859 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1860 } else {
1861 kfree_skb(skb);
1862 /* Jamal, now you will not able to escape explaining
1863 * me how you were going to use this. :-)
1864 */
1865 ret = NET_RX_DROP;
1866 }
1867
1868 out:
1869 rcu_read_unlock();
1870 return ret;
1871 }
1872
1873 static int process_backlog(struct net_device *backlog_dev, int *budget)
1874 {
1875 int work = 0;
1876 int quota = min(backlog_dev->quota, *budget);
1877 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1878 unsigned long start_time = jiffies;
1879
1880 backlog_dev->weight = weight_p;
1881 for (;;) {
1882 struct sk_buff *skb;
1883 struct net_device *dev;
1884
1885 local_irq_disable();
1886 skb = __skb_dequeue(&queue->input_pkt_queue);
1887 if (!skb)
1888 goto job_done;
1889 local_irq_enable();
1890
1891 dev = skb->dev;
1892
1893 netif_receive_skb(skb);
1894
1895 dev_put(dev);
1896
1897 work++;
1898
1899 if (work >= quota || jiffies - start_time > 1)
1900 break;
1901
1902 }
1903
1904 backlog_dev->quota -= work;
1905 *budget -= work;
1906 return -1;
1907
1908 job_done:
1909 backlog_dev->quota -= work;
1910 *budget -= work;
1911
1912 list_del(&backlog_dev->poll_list);
1913 smp_mb__before_clear_bit();
1914 netif_poll_enable(backlog_dev);
1915
1916 local_irq_enable();
1917 return 0;
1918 }
1919
1920 static void net_rx_action(struct softirq_action *h)
1921 {
1922 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1923 unsigned long start_time = jiffies;
1924 int budget = netdev_budget;
1925 void *have;
1926
1927 local_irq_disable();
1928
1929 while (!list_empty(&queue->poll_list)) {
1930 struct net_device *dev;
1931
1932 if (budget <= 0 || jiffies - start_time > 1)
1933 goto softnet_break;
1934
1935 local_irq_enable();
1936
1937 dev = list_entry(queue->poll_list.next,
1938 struct net_device, poll_list);
1939 have = netpoll_poll_lock(dev);
1940
1941 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
1942 netpoll_poll_unlock(have);
1943 local_irq_disable();
1944 list_move_tail(&dev->poll_list, &queue->poll_list);
1945 if (dev->quota < 0)
1946 dev->quota += dev->weight;
1947 else
1948 dev->quota = dev->weight;
1949 } else {
1950 netpoll_poll_unlock(have);
1951 dev_put(dev);
1952 local_irq_disable();
1953 }
1954 }
1955 out:
1956 #ifdef CONFIG_NET_DMA
1957 /*
1958 * There may not be any more sk_buffs coming right now, so push
1959 * any pending DMA copies to hardware
1960 */
1961 if (net_dma_client) {
1962 struct dma_chan *chan;
1963 rcu_read_lock();
1964 list_for_each_entry_rcu(chan, &net_dma_client->channels, client_node)
1965 dma_async_memcpy_issue_pending(chan);
1966 rcu_read_unlock();
1967 }
1968 #endif
1969 local_irq_enable();
1970 return;
1971
1972 softnet_break:
1973 __get_cpu_var(netdev_rx_stat).time_squeeze++;
1974 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1975 goto out;
1976 }
1977
1978 static gifconf_func_t * gifconf_list [NPROTO];
1979
1980 /**
1981 * register_gifconf - register a SIOCGIF handler
1982 * @family: Address family
1983 * @gifconf: Function handler
1984 *
1985 * Register protocol dependent address dumping routines. The handler
1986 * that is passed must not be freed or reused until it has been replaced
1987 * by another handler.
1988 */
1989 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1990 {
1991 if (family >= NPROTO)
1992 return -EINVAL;
1993 gifconf_list[family] = gifconf;
1994 return 0;
1995 }
1996
1997
1998 /*
1999 * Map an interface index to its name (SIOCGIFNAME)
2000 */
2001
2002 /*
2003 * We need this ioctl for efficient implementation of the
2004 * if_indextoname() function required by the IPv6 API. Without
2005 * it, we would have to search all the interfaces to find a
2006 * match. --pb
2007 */
2008
2009 static int dev_ifname(struct ifreq __user *arg)
2010 {
2011 struct net_device *dev;
2012 struct ifreq ifr;
2013
2014 /*
2015 * Fetch the caller's info block.
2016 */
2017
2018 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2019 return -EFAULT;
2020
2021 read_lock(&dev_base_lock);
2022 dev = __dev_get_by_index(ifr.ifr_ifindex);
2023 if (!dev) {
2024 read_unlock(&dev_base_lock);
2025 return -ENODEV;
2026 }
2027
2028 strcpy(ifr.ifr_name, dev->name);
2029 read_unlock(&dev_base_lock);
2030
2031 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2032 return -EFAULT;
2033 return 0;
2034 }
2035
2036 /*
2037 * Perform a SIOCGIFCONF call. This structure will change
2038 * size eventually, and there is nothing I can do about it.
2039 * Thus we will need a 'compatibility mode'.
2040 */
2041
2042 static int dev_ifconf(char __user *arg)
2043 {
2044 struct ifconf ifc;
2045 struct net_device *dev;
2046 char __user *pos;
2047 int len;
2048 int total;
2049 int i;
2050
2051 /*
2052 * Fetch the caller's info block.
2053 */
2054
2055 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2056 return -EFAULT;
2057
2058 pos = ifc.ifc_buf;
2059 len = ifc.ifc_len;
2060
2061 /*
2062 * Loop over the interfaces, and write an info block for each.
2063 */
2064
2065 total = 0;
2066 for (dev = dev_base; dev; dev = dev->next) {
2067 for (i = 0; i < NPROTO; i++) {
2068 if (gifconf_list[i]) {
2069 int done;
2070 if (!pos)
2071 done = gifconf_list[i](dev, NULL, 0);
2072 else
2073 done = gifconf_list[i](dev, pos + total,
2074 len - total);
2075 if (done < 0)
2076 return -EFAULT;
2077 total += done;
2078 }
2079 }
2080 }
2081
2082 /*
2083 * All done. Write the updated control block back to the caller.
2084 */
2085 ifc.ifc_len = total;
2086
2087 /*
2088 * Both BSD and Solaris return 0 here, so we do too.
2089 */
2090 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2091 }
2092
2093 #ifdef CONFIG_PROC_FS
2094 /*
2095 * This is invoked by the /proc filesystem handler to display a device
2096 * in detail.
2097 */
2098 static __inline__ struct net_device *dev_get_idx(loff_t pos)
2099 {
2100 struct net_device *dev;
2101 loff_t i;
2102
2103 for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
2104
2105 return i == pos ? dev : NULL;
2106 }
2107
2108 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2109 {
2110 read_lock(&dev_base_lock);
2111 return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
2112 }
2113
2114 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2115 {
2116 ++*pos;
2117 return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
2118 }
2119
2120 void dev_seq_stop(struct seq_file *seq, void *v)
2121 {
2122 read_unlock(&dev_base_lock);
2123 }
2124
2125 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2126 {
2127 if (dev->get_stats) {
2128 struct net_device_stats *stats = dev->get_stats(dev);
2129
2130 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2131 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2132 dev->name, stats->rx_bytes, stats->rx_packets,
2133 stats->rx_errors,
2134 stats->rx_dropped + stats->rx_missed_errors,
2135 stats->rx_fifo_errors,
2136 stats->rx_length_errors + stats->rx_over_errors +
2137 stats->rx_crc_errors + stats->rx_frame_errors,
2138 stats->rx_compressed, stats->multicast,
2139 stats->tx_bytes, stats->tx_packets,
2140 stats->tx_errors, stats->tx_dropped,
2141 stats->tx_fifo_errors, stats->collisions,
2142 stats->tx_carrier_errors +
2143 stats->tx_aborted_errors +
2144 stats->tx_window_errors +
2145 stats->tx_heartbeat_errors,
2146 stats->tx_compressed);
2147 } else
2148 seq_printf(seq, "%6s: No statistics available.\n", dev->name);
2149 }
2150
2151 /*
2152 * Called from the PROCfs module. This now uses the new arbitrary sized
2153 * /proc/net interface to create /proc/net/dev
2154 */
2155 static int dev_seq_show(struct seq_file *seq, void *v)
2156 {
2157 if (v == SEQ_START_TOKEN)
2158 seq_puts(seq, "Inter-| Receive "
2159 " | Transmit\n"
2160 " face |bytes packets errs drop fifo frame "
2161 "compressed multicast|bytes packets errs "
2162 "drop fifo colls carrier compressed\n");
2163 else
2164 dev_seq_printf_stats(seq, v);
2165 return 0;
2166 }
2167
2168 static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2169 {
2170 struct netif_rx_stats *rc = NULL;
2171
2172 while (*pos < NR_CPUS)
2173 if (cpu_online(*pos)) {
2174 rc = &per_cpu(netdev_rx_stat, *pos);
2175 break;
2176 } else
2177 ++*pos;
2178 return rc;
2179 }
2180
2181 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2182 {
2183 return softnet_get_online(pos);
2184 }
2185
2186 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2187 {
2188 ++*pos;
2189 return softnet_get_online(pos);
2190 }
2191
2192 static void softnet_seq_stop(struct seq_file *seq, void *v)
2193 {
2194 }
2195
2196 static int softnet_seq_show(struct seq_file *seq, void *v)
2197 {
2198 struct netif_rx_stats *s = v;
2199
2200 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2201 s->total, s->dropped, s->time_squeeze, 0,
2202 0, 0, 0, 0, /* was fastroute */
2203 s->cpu_collision );
2204 return 0;
2205 }
2206
2207 static struct seq_operations dev_seq_ops = {
2208 .start = dev_seq_start,
2209 .next = dev_seq_next,
2210 .stop = dev_seq_stop,
2211 .show = dev_seq_show,
2212 };
2213
2214 static int dev_seq_open(struct inode *inode, struct file *file)
2215 {
2216 return seq_open(file, &dev_seq_ops);
2217 }
2218
2219 static struct file_operations dev_seq_fops = {
2220 .owner = THIS_MODULE,
2221 .open = dev_seq_open,
2222 .read = seq_read,
2223 .llseek = seq_lseek,
2224 .release = seq_release,
2225 };
2226
2227 static struct seq_operations softnet_seq_ops = {
2228 .start = softnet_seq_start,
2229 .next = softnet_seq_next,
2230 .stop = softnet_seq_stop,
2231 .show = softnet_seq_show,
2232 };
2233
2234 static int softnet_seq_open(struct inode *inode, struct file *file)
2235 {
2236 return seq_open(file, &softnet_seq_ops);
2237 }
2238
2239 static struct file_operations softnet_seq_fops = {
2240 .owner = THIS_MODULE,
2241 .open = softnet_seq_open,
2242 .read = seq_read,
2243 .llseek = seq_lseek,
2244 .release = seq_release,
2245 };
2246
2247 #ifdef CONFIG_WIRELESS_EXT
2248 extern int wireless_proc_init(void);
2249 #else
2250 #define wireless_proc_init() 0
2251 #endif
2252
2253 static int __init dev_proc_init(void)
2254 {
2255 int rc = -ENOMEM;
2256
2257 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2258 goto out;
2259 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2260 goto out_dev;
2261 if (wireless_proc_init())
2262 goto out_softnet;
2263 rc = 0;
2264 out:
2265 return rc;
2266 out_softnet:
2267 proc_net_remove("softnet_stat");
2268 out_dev:
2269 proc_net_remove("dev");
2270 goto out;
2271 }
2272 #else
2273 #define dev_proc_init() 0
2274 #endif /* CONFIG_PROC_FS */
2275
2276
2277 /**
2278 * netdev_set_master - set up master/slave pair
2279 * @slave: slave device
2280 * @master: new master device
2281 *
2282 * Changes the master device of the slave. Pass %NULL to break the
2283 * bonding. The caller must hold the RTNL semaphore. On a failure
2284 * a negative errno code is returned. On success the reference counts
2285 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2286 * function returns zero.
2287 */
2288 int netdev_set_master(struct net_device *slave, struct net_device *master)
2289 {
2290 struct net_device *old = slave->master;
2291
2292 ASSERT_RTNL();
2293
2294 if (master) {
2295 if (old)
2296 return -EBUSY;
2297 dev_hold(master);
2298 }
2299
2300 slave->master = master;
2301
2302 synchronize_net();
2303
2304 if (old)
2305 dev_put(old);
2306
2307 if (master)
2308 slave->flags |= IFF_SLAVE;
2309 else
2310 slave->flags &= ~IFF_SLAVE;
2311
2312 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2313 return 0;
2314 }
2315
2316 /**
2317 * dev_set_promiscuity - update promiscuity count on a device
2318 * @dev: device
2319 * @inc: modifier
2320 *
2321 * Add or remove promiscuity from a device. While the count in the device
2322 * remains above zero the interface remains promiscuous. Once it hits zero
2323 * the device reverts back to normal filtering operation. A negative inc
2324 * value is used to drop promiscuity on the device.
2325 */
2326 void dev_set_promiscuity(struct net_device *dev, int inc)
2327 {
2328 unsigned short old_flags = dev->flags;
2329
2330 if ((dev->promiscuity += inc) == 0)
2331 dev->flags &= ~IFF_PROMISC;
2332 else
2333 dev->flags |= IFF_PROMISC;
2334 if (dev->flags != old_flags) {
2335 dev_mc_upload(dev);
2336 printk(KERN_INFO "device %s %s promiscuous mode\n",
2337 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2338 "left");
2339 audit_log(current->audit_context, GFP_ATOMIC,
2340 AUDIT_ANOM_PROMISCUOUS,
2341 "dev=%s prom=%d old_prom=%d auid=%u",
2342 dev->name, (dev->flags & IFF_PROMISC),
2343 (old_flags & IFF_PROMISC),
2344 audit_get_loginuid(current->audit_context));
2345 }
2346 }
2347
2348 /**
2349 * dev_set_allmulti - update allmulti count on a device
2350 * @dev: device
2351 * @inc: modifier
2352 *
2353 * Add or remove reception of all multicast frames to a device. While the
2354 * count in the device remains above zero the interface remains listening
2355 * to all interfaces. Once it hits zero the device reverts back to normal
2356 * filtering operation. A negative @inc value is used to drop the counter
2357 * when releasing a resource needing all multicasts.
2358 */
2359
2360 void dev_set_allmulti(struct net_device *dev, int inc)
2361 {
2362 unsigned short old_flags = dev->flags;
2363
2364 dev->flags |= IFF_ALLMULTI;
2365 if ((dev->allmulti += inc) == 0)
2366 dev->flags &= ~IFF_ALLMULTI;
2367 if (dev->flags ^ old_flags)
2368 dev_mc_upload(dev);
2369 }
2370
2371 unsigned dev_get_flags(const struct net_device *dev)
2372 {
2373 unsigned flags;
2374
2375 flags = (dev->flags & ~(IFF_PROMISC |
2376 IFF_ALLMULTI |
2377 IFF_RUNNING |
2378 IFF_LOWER_UP |
2379 IFF_DORMANT)) |
2380 (dev->gflags & (IFF_PROMISC |
2381 IFF_ALLMULTI));
2382
2383 if (netif_running(dev)) {
2384 if (netif_oper_up(dev))
2385 flags |= IFF_RUNNING;
2386 if (netif_carrier_ok(dev))
2387 flags |= IFF_LOWER_UP;
2388 if (netif_dormant(dev))
2389 flags |= IFF_DORMANT;
2390 }
2391
2392 return flags;
2393 }
2394
2395 int dev_change_flags(struct net_device *dev, unsigned flags)
2396 {
2397 int ret;
2398 int old_flags = dev->flags;
2399
2400 /*
2401 * Set the flags on our device.
2402 */
2403
2404 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2405 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2406 IFF_AUTOMEDIA)) |
2407 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2408 IFF_ALLMULTI));
2409
2410 /*
2411 * Load in the correct multicast list now the flags have changed.
2412 */
2413
2414 dev_mc_upload(dev);
2415
2416 /*
2417 * Have we downed the interface. We handle IFF_UP ourselves
2418 * according to user attempts to set it, rather than blindly
2419 * setting it.
2420 */
2421
2422 ret = 0;
2423 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2424 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2425
2426 if (!ret)
2427 dev_mc_upload(dev);
2428 }
2429
2430 if (dev->flags & IFF_UP &&
2431 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2432 IFF_VOLATILE)))
2433 raw_notifier_call_chain(&netdev_chain,
2434 NETDEV_CHANGE, dev);
2435
2436 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2437 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2438 dev->gflags ^= IFF_PROMISC;
2439 dev_set_promiscuity(dev, inc);
2440 }
2441
2442 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2443 is important. Some (broken) drivers set IFF_PROMISC, when
2444 IFF_ALLMULTI is requested not asking us and not reporting.
2445 */
2446 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2447 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2448 dev->gflags ^= IFF_ALLMULTI;
2449 dev_set_allmulti(dev, inc);
2450 }
2451
2452 if (old_flags ^ dev->flags)
2453 rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2454
2455 return ret;
2456 }
2457
2458 int dev_set_mtu(struct net_device *dev, int new_mtu)
2459 {
2460 int err;
2461
2462 if (new_mtu == dev->mtu)
2463 return 0;
2464
2465 /* MTU must be positive. */
2466 if (new_mtu < 0)
2467 return -EINVAL;
2468
2469 if (!netif_device_present(dev))
2470 return -ENODEV;
2471
2472 err = 0;
2473 if (dev->change_mtu)
2474 err = dev->change_mtu(dev, new_mtu);
2475 else
2476 dev->mtu = new_mtu;
2477 if (!err && dev->flags & IFF_UP)
2478 raw_notifier_call_chain(&netdev_chain,
2479 NETDEV_CHANGEMTU, dev);
2480 return err;
2481 }
2482
2483 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2484 {
2485 int err;
2486
2487 if (!dev->set_mac_address)
2488 return -EOPNOTSUPP;
2489 if (sa->sa_family != dev->type)
2490 return -EINVAL;
2491 if (!netif_device_present(dev))
2492 return -ENODEV;
2493 err = dev->set_mac_address(dev, sa);
2494 if (!err)
2495 raw_notifier_call_chain(&netdev_chain,
2496 NETDEV_CHANGEADDR, dev);
2497 return err;
2498 }
2499
2500 /*
2501 * Perform the SIOCxIFxxx calls.
2502 */
2503 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2504 {
2505 int err;
2506 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2507
2508 if (!dev)
2509 return -ENODEV;
2510
2511 switch (cmd) {
2512 case SIOCGIFFLAGS: /* Get interface flags */
2513 ifr->ifr_flags = dev_get_flags(dev);
2514 return 0;
2515
2516 case SIOCSIFFLAGS: /* Set interface flags */
2517 return dev_change_flags(dev, ifr->ifr_flags);
2518
2519 case SIOCGIFMETRIC: /* Get the metric on the interface
2520 (currently unused) */
2521 ifr->ifr_metric = 0;
2522 return 0;
2523
2524 case SIOCSIFMETRIC: /* Set the metric on the interface
2525 (currently unused) */
2526 return -EOPNOTSUPP;
2527
2528 case SIOCGIFMTU: /* Get the MTU of a device */
2529 ifr->ifr_mtu = dev->mtu;
2530 return 0;
2531
2532 case SIOCSIFMTU: /* Set the MTU of a device */
2533 return dev_set_mtu(dev, ifr->ifr_mtu);
2534
2535 case SIOCGIFHWADDR:
2536 if (!dev->addr_len)
2537 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2538 else
2539 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2540 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2541 ifr->ifr_hwaddr.sa_family = dev->type;
2542 return 0;
2543
2544 case SIOCSIFHWADDR:
2545 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2546
2547 case SIOCSIFHWBROADCAST:
2548 if (ifr->ifr_hwaddr.sa_family != dev->type)
2549 return -EINVAL;
2550 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2551 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2552 raw_notifier_call_chain(&netdev_chain,
2553 NETDEV_CHANGEADDR, dev);
2554 return 0;
2555
2556 case SIOCGIFMAP:
2557 ifr->ifr_map.mem_start = dev->mem_start;
2558 ifr->ifr_map.mem_end = dev->mem_end;
2559 ifr->ifr_map.base_addr = dev->base_addr;
2560 ifr->ifr_map.irq = dev->irq;
2561 ifr->ifr_map.dma = dev->dma;
2562 ifr->ifr_map.port = dev->if_port;
2563 return 0;
2564
2565 case SIOCSIFMAP:
2566 if (dev->set_config) {
2567 if (!netif_device_present(dev))
2568 return -ENODEV;
2569 return dev->set_config(dev, &ifr->ifr_map);
2570 }
2571 return -EOPNOTSUPP;
2572
2573 case SIOCADDMULTI:
2574 if (!dev->set_multicast_list ||
2575 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2576 return -EINVAL;
2577 if (!netif_device_present(dev))
2578 return -ENODEV;
2579 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2580 dev->addr_len, 1);
2581
2582 case SIOCDELMULTI:
2583 if (!dev->set_multicast_list ||
2584 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2585 return -EINVAL;
2586 if (!netif_device_present(dev))
2587 return -ENODEV;
2588 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2589 dev->addr_len, 1);
2590
2591 case SIOCGIFINDEX:
2592 ifr->ifr_ifindex = dev->ifindex;
2593 return 0;
2594
2595 case SIOCGIFTXQLEN:
2596 ifr->ifr_qlen = dev->tx_queue_len;
2597 return 0;
2598
2599 case SIOCSIFTXQLEN:
2600 if (ifr->ifr_qlen < 0)
2601 return -EINVAL;
2602 dev->tx_queue_len = ifr->ifr_qlen;
2603 return 0;
2604
2605 case SIOCSIFNAME:
2606 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2607 return dev_change_name(dev, ifr->ifr_newname);
2608
2609 /*
2610 * Unknown or private ioctl
2611 */
2612
2613 default:
2614 if ((cmd >= SIOCDEVPRIVATE &&
2615 cmd <= SIOCDEVPRIVATE + 15) ||
2616 cmd == SIOCBONDENSLAVE ||
2617 cmd == SIOCBONDRELEASE ||
2618 cmd == SIOCBONDSETHWADDR ||
2619 cmd == SIOCBONDSLAVEINFOQUERY ||
2620 cmd == SIOCBONDINFOQUERY ||
2621 cmd == SIOCBONDCHANGEACTIVE ||
2622 cmd == SIOCGMIIPHY ||
2623 cmd == SIOCGMIIREG ||
2624 cmd == SIOCSMIIREG ||
2625 cmd == SIOCBRADDIF ||
2626 cmd == SIOCBRDELIF ||
2627 cmd == SIOCWANDEV) {
2628 err = -EOPNOTSUPP;
2629 if (dev->do_ioctl) {
2630 if (netif_device_present(dev))
2631 err = dev->do_ioctl(dev, ifr,
2632 cmd);
2633 else
2634 err = -ENODEV;
2635 }
2636 } else
2637 err = -EINVAL;
2638
2639 }
2640 return err;
2641 }
2642
2643 /*
2644 * This function handles all "interface"-type I/O control requests. The actual
2645 * 'doing' part of this is dev_ifsioc above.
2646 */
2647
2648 /**
2649 * dev_ioctl - network device ioctl
2650 * @cmd: command to issue
2651 * @arg: pointer to a struct ifreq in user space
2652 *
2653 * Issue ioctl functions to devices. This is normally called by the
2654 * user space syscall interfaces but can sometimes be useful for
2655 * other purposes. The return value is the return from the syscall if
2656 * positive or a negative errno code on error.
2657 */
2658
2659 int dev_ioctl(unsigned int cmd, void __user *arg)
2660 {
2661 struct ifreq ifr;
2662 int ret;
2663 char *colon;
2664
2665 /* One special case: SIOCGIFCONF takes ifconf argument
2666 and requires shared lock, because it sleeps writing
2667 to user space.
2668 */
2669
2670 if (cmd == SIOCGIFCONF) {
2671 rtnl_lock();
2672 ret = dev_ifconf((char __user *) arg);
2673 rtnl_unlock();
2674 return ret;
2675 }
2676 if (cmd == SIOCGIFNAME)
2677 return dev_ifname((struct ifreq __user *)arg);
2678
2679 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2680 return -EFAULT;
2681
2682 ifr.ifr_name[IFNAMSIZ-1] = 0;
2683
2684 colon = strchr(ifr.ifr_name, ':');
2685 if (colon)
2686 *colon = 0;
2687
2688 /*
2689 * See which interface the caller is talking about.
2690 */
2691
2692 switch (cmd) {
2693 /*
2694 * These ioctl calls:
2695 * - can be done by all.
2696 * - atomic and do not require locking.
2697 * - return a value
2698 */
2699 case SIOCGIFFLAGS:
2700 case SIOCGIFMETRIC:
2701 case SIOCGIFMTU:
2702 case SIOCGIFHWADDR:
2703 case SIOCGIFSLAVE:
2704 case SIOCGIFMAP:
2705 case SIOCGIFINDEX:
2706 case SIOCGIFTXQLEN:
2707 dev_load(ifr.ifr_name);
2708 read_lock(&dev_base_lock);
2709 ret = dev_ifsioc(&ifr, cmd);
2710 read_unlock(&dev_base_lock);
2711 if (!ret) {
2712 if (colon)
2713 *colon = ':';
2714 if (copy_to_user(arg, &ifr,
2715 sizeof(struct ifreq)))
2716 ret = -EFAULT;
2717 }
2718 return ret;
2719
2720 case SIOCETHTOOL:
2721 dev_load(ifr.ifr_name);
2722 rtnl_lock();
2723 ret = dev_ethtool(&ifr);
2724 rtnl_unlock();
2725 if (!ret) {
2726 if (colon)
2727 *colon = ':';
2728 if (copy_to_user(arg, &ifr,
2729 sizeof(struct ifreq)))
2730 ret = -EFAULT;
2731 }
2732 return ret;
2733
2734 /*
2735 * These ioctl calls:
2736 * - require superuser power.
2737 * - require strict serialization.
2738 * - return a value
2739 */
2740 case SIOCGMIIPHY:
2741 case SIOCGMIIREG:
2742 case SIOCSIFNAME:
2743 if (!capable(CAP_NET_ADMIN))
2744 return -EPERM;
2745 dev_load(ifr.ifr_name);
2746 rtnl_lock();
2747 ret = dev_ifsioc(&ifr, cmd);
2748 rtnl_unlock();
2749 if (!ret) {
2750 if (colon)
2751 *colon = ':';
2752 if (copy_to_user(arg, &ifr,
2753 sizeof(struct ifreq)))
2754 ret = -EFAULT;
2755 }
2756 return ret;
2757
2758 /*
2759 * These ioctl calls:
2760 * - require superuser power.
2761 * - require strict serialization.
2762 * - do not return a value
2763 */
2764 case SIOCSIFFLAGS:
2765 case SIOCSIFMETRIC:
2766 case SIOCSIFMTU:
2767 case SIOCSIFMAP:
2768 case SIOCSIFHWADDR:
2769 case SIOCSIFSLAVE:
2770 case SIOCADDMULTI:
2771 case SIOCDELMULTI:
2772 case SIOCSIFHWBROADCAST:
2773 case SIOCSIFTXQLEN:
2774 case SIOCSMIIREG:
2775 case SIOCBONDENSLAVE:
2776 case SIOCBONDRELEASE:
2777 case SIOCBONDSETHWADDR:
2778 case SIOCBONDCHANGEACTIVE:
2779 case SIOCBRADDIF:
2780 case SIOCBRDELIF:
2781 if (!capable(CAP_NET_ADMIN))
2782 return -EPERM;
2783 /* fall through */
2784 case SIOCBONDSLAVEINFOQUERY:
2785 case SIOCBONDINFOQUERY:
2786 dev_load(ifr.ifr_name);
2787 rtnl_lock();
2788 ret = dev_ifsioc(&ifr, cmd);
2789 rtnl_unlock();
2790 return ret;
2791
2792 case SIOCGIFMEM:
2793 /* Get the per device memory space. We can add this but
2794 * currently do not support it */
2795 case SIOCSIFMEM:
2796 /* Set the per device memory buffer space.
2797 * Not applicable in our case */
2798 case SIOCSIFLINK:
2799 return -EINVAL;
2800
2801 /*
2802 * Unknown or private ioctl.
2803 */
2804 default:
2805 if (cmd == SIOCWANDEV ||
2806 (cmd >= SIOCDEVPRIVATE &&
2807 cmd <= SIOCDEVPRIVATE + 15)) {
2808 dev_load(ifr.ifr_name);
2809 rtnl_lock();
2810 ret = dev_ifsioc(&ifr, cmd);
2811 rtnl_unlock();
2812 if (!ret && copy_to_user(arg, &ifr,
2813 sizeof(struct ifreq)))
2814 ret = -EFAULT;
2815 return ret;
2816 }
2817 #ifdef CONFIG_WIRELESS_EXT
2818 /* Take care of Wireless Extensions */
2819 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2820 /* If command is `set a parameter', or
2821 * `get the encoding parameters', check if
2822 * the user has the right to do it */
2823 if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE
2824 || cmd == SIOCGIWENCODEEXT) {
2825 if (!capable(CAP_NET_ADMIN))
2826 return -EPERM;
2827 }
2828 dev_load(ifr.ifr_name);
2829 rtnl_lock();
2830 /* Follow me in net/core/wireless.c */
2831 ret = wireless_process_ioctl(&ifr, cmd);
2832 rtnl_unlock();
2833 if (IW_IS_GET(cmd) &&
2834 copy_to_user(arg, &ifr,
2835 sizeof(struct ifreq)))
2836 ret = -EFAULT;
2837 return ret;
2838 }
2839 #endif /* CONFIG_WIRELESS_EXT */
2840 return -EINVAL;
2841 }
2842 }
2843
2844
2845 /**
2846 * dev_new_index - allocate an ifindex
2847 *
2848 * Returns a suitable unique value for a new device interface
2849 * number. The caller must hold the rtnl semaphore or the
2850 * dev_base_lock to be sure it remains unique.
2851 */
2852 static int dev_new_index(void)
2853 {
2854 static int ifindex;
2855 for (;;) {
2856 if (++ifindex <= 0)
2857 ifindex = 1;
2858 if (!__dev_get_by_index(ifindex))
2859 return ifindex;
2860 }
2861 }
2862
2863 static int dev_boot_phase = 1;
2864
2865 /* Delayed registration/unregisteration */
2866 static DEFINE_SPINLOCK(net_todo_list_lock);
2867 static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
2868
2869 static inline void net_set_todo(struct net_device *dev)
2870 {
2871 spin_lock(&net_todo_list_lock);
2872 list_add_tail(&dev->todo_list, &net_todo_list);
2873 spin_unlock(&net_todo_list_lock);
2874 }
2875
2876 /**
2877 * register_netdevice - register a network device
2878 * @dev: device to register
2879 *
2880 * Take a completed network device structure and add it to the kernel
2881 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2882 * chain. 0 is returned on success. A negative errno code is returned
2883 * on a failure to set up the device, or if the name is a duplicate.
2884 *
2885 * Callers must hold the rtnl semaphore. You may want
2886 * register_netdev() instead of this.
2887 *
2888 * BUGS:
2889 * The locking appears insufficient to guarantee two parallel registers
2890 * will not get the same name.
2891 */
2892
2893 int register_netdevice(struct net_device *dev)
2894 {
2895 struct hlist_head *head;
2896 struct hlist_node *p;
2897 int ret;
2898
2899 BUG_ON(dev_boot_phase);
2900 ASSERT_RTNL();
2901
2902 might_sleep();
2903
2904 /* When net_device's are persistent, this will be fatal. */
2905 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
2906
2907 spin_lock_init(&dev->queue_lock);
2908 spin_lock_init(&dev->_xmit_lock);
2909 dev->xmit_lock_owner = -1;
2910 #ifdef CONFIG_NET_CLS_ACT
2911 spin_lock_init(&dev->ingress_lock);
2912 #endif
2913
2914 ret = alloc_divert_blk(dev);
2915 if (ret)
2916 goto out;
2917
2918 dev->iflink = -1;
2919
2920 /* Init, if this function is available */
2921 if (dev->init) {
2922 ret = dev->init(dev);
2923 if (ret) {
2924 if (ret > 0)
2925 ret = -EIO;
2926 goto out_err;
2927 }
2928 }
2929
2930 if (!dev_valid_name(dev->name)) {
2931 ret = -EINVAL;
2932 goto out_err;
2933 }
2934
2935 dev->ifindex = dev_new_index();
2936 if (dev->iflink == -1)
2937 dev->iflink = dev->ifindex;
2938
2939 /* Check for existence of name */
2940 head = dev_name_hash(dev->name);
2941 hlist_for_each(p, head) {
2942 struct net_device *d
2943 = hlist_entry(p, struct net_device, name_hlist);
2944 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
2945 ret = -EEXIST;
2946 goto out_err;
2947 }
2948 }
2949
2950 /* Fix illegal SG+CSUM combinations. */
2951 if ((dev->features & NETIF_F_SG) &&
2952 !(dev->features & NETIF_F_ALL_CSUM)) {
2953 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
2954 dev->name);
2955 dev->features &= ~NETIF_F_SG;
2956 }
2957
2958 /* TSO requires that SG is present as well. */
2959 if ((dev->features & NETIF_F_TSO) &&
2960 !(dev->features & NETIF_F_SG)) {
2961 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
2962 dev->name);
2963 dev->features &= ~NETIF_F_TSO;
2964 }
2965 if (dev->features & NETIF_F_UFO) {
2966 if (!(dev->features & NETIF_F_HW_CSUM)) {
2967 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2968 "NETIF_F_HW_CSUM feature.\n",
2969 dev->name);
2970 dev->features &= ~NETIF_F_UFO;
2971 }
2972 if (!(dev->features & NETIF_F_SG)) {
2973 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2974 "NETIF_F_SG feature.\n",
2975 dev->name);
2976 dev->features &= ~NETIF_F_UFO;
2977 }
2978 }
2979
2980 /*
2981 * nil rebuild_header routine,
2982 * that should be never called and used as just bug trap.
2983 */
2984
2985 if (!dev->rebuild_header)
2986 dev->rebuild_header = default_rebuild_header;
2987
2988 ret = netdev_register_sysfs(dev);
2989 if (ret)
2990 goto out_err;
2991 dev->reg_state = NETREG_REGISTERED;
2992
2993 /*
2994 * Default initial state at registry is that the
2995 * device is present.
2996 */
2997
2998 set_bit(__LINK_STATE_PRESENT, &dev->state);
2999
3000 dev->next = NULL;
3001 dev_init_scheduler(dev);
3002 write_lock_bh(&dev_base_lock);
3003 *dev_tail = dev;
3004 dev_tail = &dev->next;
3005 hlist_add_head(&dev->name_hlist, head);
3006 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
3007 dev_hold(dev);
3008 write_unlock_bh(&dev_base_lock);
3009
3010 /* Notify protocols, that a new device appeared. */
3011 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
3012
3013 ret = 0;
3014
3015 out:
3016 return ret;
3017 out_err:
3018 free_divert_blk(dev);
3019 goto out;
3020 }
3021
3022 /**
3023 * register_netdev - register a network device
3024 * @dev: device to register
3025 *
3026 * Take a completed network device structure and add it to the kernel
3027 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3028 * chain. 0 is returned on success. A negative errno code is returned
3029 * on a failure to set up the device, or if the name is a duplicate.
3030 *
3031 * This is a wrapper around register_netdev that takes the rtnl semaphore
3032 * and expands the device name if you passed a format string to
3033 * alloc_netdev.
3034 */
3035 int register_netdev(struct net_device *dev)
3036 {
3037 int err;
3038
3039 rtnl_lock();
3040
3041 /*
3042 * If the name is a format string the caller wants us to do a
3043 * name allocation.
3044 */
3045 if (strchr(dev->name, '%')) {
3046 err = dev_alloc_name(dev, dev->name);
3047 if (err < 0)
3048 goto out;
3049 }
3050
3051 /*
3052 * Back compatibility hook. Kill this one in 2.5
3053 */
3054 if (dev->name[0] == 0 || dev->name[0] == ' ') {
3055 err = dev_alloc_name(dev, "eth%d");
3056 if (err < 0)
3057 goto out;
3058 }
3059
3060 err = register_netdevice(dev);
3061 out:
3062 rtnl_unlock();
3063 return err;
3064 }
3065 EXPORT_SYMBOL(register_netdev);
3066
3067 /*
3068 * netdev_wait_allrefs - wait until all references are gone.
3069 *
3070 * This is called when unregistering network devices.
3071 *
3072 * Any protocol or device that holds a reference should register
3073 * for netdevice notification, and cleanup and put back the
3074 * reference if they receive an UNREGISTER event.
3075 * We can get stuck here if buggy protocols don't correctly
3076 * call dev_put.
3077 */
3078 static void netdev_wait_allrefs(struct net_device *dev)
3079 {
3080 unsigned long rebroadcast_time, warning_time;
3081
3082 rebroadcast_time = warning_time = jiffies;
3083 while (atomic_read(&dev->refcnt) != 0) {
3084 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
3085 rtnl_lock();
3086
3087 /* Rebroadcast unregister notification */
3088 raw_notifier_call_chain(&netdev_chain,
3089 NETDEV_UNREGISTER, dev);
3090
3091 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3092 &dev->state)) {
3093 /* We must not have linkwatch events
3094 * pending on unregister. If this
3095 * happens, we simply run the queue
3096 * unscheduled, resulting in a noop
3097 * for this device.
3098 */
3099 linkwatch_run_queue();
3100 }
3101
3102 __rtnl_unlock();
3103
3104 rebroadcast_time = jiffies;
3105 }
3106
3107 msleep(250);
3108
3109 if (time_after(jiffies, warning_time + 10 * HZ)) {
3110 printk(KERN_EMERG "unregister_netdevice: "
3111 "waiting for %s to become free. Usage "
3112 "count = %d\n",
3113 dev->name, atomic_read(&dev->refcnt));
3114 warning_time = jiffies;
3115 }
3116 }
3117 }
3118
3119 /* The sequence is:
3120 *
3121 * rtnl_lock();
3122 * ...
3123 * register_netdevice(x1);
3124 * register_netdevice(x2);
3125 * ...
3126 * unregister_netdevice(y1);
3127 * unregister_netdevice(y2);
3128 * ...
3129 * rtnl_unlock();
3130 * free_netdev(y1);
3131 * free_netdev(y2);
3132 *
3133 * We are invoked by rtnl_unlock() after it drops the semaphore.
3134 * This allows us to deal with problems:
3135 * 1) We can delete sysfs objects which invoke hotplug
3136 * without deadlocking with linkwatch via keventd.
3137 * 2) Since we run with the RTNL semaphore not held, we can sleep
3138 * safely in order to wait for the netdev refcnt to drop to zero.
3139 */
3140 static DEFINE_MUTEX(net_todo_run_mutex);
3141 void netdev_run_todo(void)
3142 {
3143 struct list_head list;
3144
3145 /* Need to guard against multiple cpu's getting out of order. */
3146 mutex_lock(&net_todo_run_mutex);
3147
3148 /* Not safe to do outside the semaphore. We must not return
3149 * until all unregister events invoked by the local processor
3150 * have been completed (either by this todo run, or one on
3151 * another cpu).
3152 */
3153 if (list_empty(&net_todo_list))
3154 goto out;
3155
3156 /* Snapshot list, allow later requests */
3157 spin_lock(&net_todo_list_lock);
3158 list_replace_init(&net_todo_list, &list);
3159 spin_unlock(&net_todo_list_lock);
3160
3161 while (!list_empty(&list)) {
3162 struct net_device *dev
3163 = list_entry(list.next, struct net_device, todo_list);
3164 list_del(&dev->todo_list);
3165
3166 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
3167 printk(KERN_ERR "network todo '%s' but state %d\n",
3168 dev->name, dev->reg_state);
3169 dump_stack();
3170 continue;
3171 }
3172
3173 netdev_unregister_sysfs(dev);
3174 dev->reg_state = NETREG_UNREGISTERED;
3175
3176 netdev_wait_allrefs(dev);
3177
3178 /* paranoia */
3179 BUG_ON(atomic_read(&dev->refcnt));
3180 BUG_TRAP(!dev->ip_ptr);
3181 BUG_TRAP(!dev->ip6_ptr);
3182 BUG_TRAP(!dev->dn_ptr);
3183
3184 /* It must be the very last action,
3185 * after this 'dev' may point to freed up memory.
3186 */
3187 if (dev->destructor)
3188 dev->destructor(dev);
3189 }
3190
3191 out:
3192 mutex_unlock(&net_todo_run_mutex);
3193 }
3194
3195 /**
3196 * alloc_netdev - allocate network device
3197 * @sizeof_priv: size of private data to allocate space for
3198 * @name: device name format string
3199 * @setup: callback to initialize device
3200 *
3201 * Allocates a struct net_device with private data area for driver use
3202 * and performs basic initialization.
3203 */
3204 struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3205 void (*setup)(struct net_device *))
3206 {
3207 void *p;
3208 struct net_device *dev;
3209 int alloc_size;
3210
3211 /* ensure 32-byte alignment of both the device and private area */
3212 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3213 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3214
3215 p = kzalloc(alloc_size, GFP_KERNEL);
3216 if (!p) {
3217 printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
3218 return NULL;
3219 }
3220
3221 dev = (struct net_device *)
3222 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3223 dev->padded = (char *)dev - (char *)p;
3224
3225 if (sizeof_priv)
3226 dev->priv = netdev_priv(dev);
3227
3228 setup(dev);
3229 strcpy(dev->name, name);
3230 return dev;
3231 }
3232 EXPORT_SYMBOL(alloc_netdev);
3233
3234 /**
3235 * free_netdev - free network device
3236 * @dev: device
3237 *
3238 * This function does the last stage of destroying an allocated device
3239 * interface. The reference to the device object is released.
3240 * If this is the last reference then it will be freed.
3241 */
3242 void free_netdev(struct net_device *dev)
3243 {
3244 #ifdef CONFIG_SYSFS
3245 /* Compatibility with error handling in drivers */
3246 if (dev->reg_state == NETREG_UNINITIALIZED) {
3247 kfree((char *)dev - dev->padded);
3248 return;
3249 }
3250
3251 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3252 dev->reg_state = NETREG_RELEASED;
3253
3254 /* will free via class release */
3255 class_device_put(&dev->class_dev);
3256 #else
3257 kfree((char *)dev - dev->padded);
3258 #endif
3259 }
3260
3261 /* Synchronize with packet receive processing. */
3262 void synchronize_net(void)
3263 {
3264 might_sleep();
3265 synchronize_rcu();
3266 }
3267
3268 /**
3269 * unregister_netdevice - remove device from the kernel
3270 * @dev: device
3271 *
3272 * This function shuts down a device interface and removes it
3273 * from the kernel tables. On success 0 is returned, on a failure
3274 * a negative errno code is returned.
3275 *
3276 * Callers must hold the rtnl semaphore. You may want
3277 * unregister_netdev() instead of this.
3278 */
3279
3280 int unregister_netdevice(struct net_device *dev)
3281 {
3282 struct net_device *d, **dp;
3283
3284 BUG_ON(dev_boot_phase);
3285 ASSERT_RTNL();
3286
3287 /* Some devices call without registering for initialization unwind. */
3288 if (dev->reg_state == NETREG_UNINITIALIZED) {
3289 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3290 "was registered\n", dev->name, dev);
3291 return -ENODEV;
3292 }
3293
3294 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3295
3296 /* If device is running, close it first. */
3297 if (dev->flags & IFF_UP)
3298 dev_close(dev);
3299
3300 /* And unlink it from device chain. */
3301 for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3302 if (d == dev) {
3303 write_lock_bh(&dev_base_lock);
3304 hlist_del(&dev->name_hlist);
3305 hlist_del(&dev->index_hlist);
3306 if (dev_tail == &dev->next)
3307 dev_tail = dp;
3308 *dp = d->next;
3309 write_unlock_bh(&dev_base_lock);
3310 break;
3311 }
3312 }
3313 if (!d) {
3314 printk(KERN_ERR "unregister net_device: '%s' not found\n",
3315 dev->name);
3316 return -ENODEV;
3317 }
3318
3319 dev->reg_state = NETREG_UNREGISTERING;
3320
3321 synchronize_net();
3322
3323 /* Shutdown queueing discipline. */
3324 dev_shutdown(dev);
3325
3326
3327 /* Notify protocols, that we are about to destroy
3328 this device. They should clean all the things.
3329 */
3330 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3331
3332 /*
3333 * Flush the multicast chain
3334 */
3335 dev_mc_discard(dev);
3336
3337 if (dev->uninit)
3338 dev->uninit(dev);
3339
3340 /* Notifier chain MUST detach us from master device. */
3341 BUG_TRAP(!dev->master);
3342
3343 free_divert_blk(dev);
3344
3345 /* Finish processing unregister after unlock */
3346 net_set_todo(dev);
3347
3348 synchronize_net();
3349
3350 dev_put(dev);
3351 return 0;
3352 }
3353
3354 /**
3355 * unregister_netdev - remove device from the kernel
3356 * @dev: device
3357 *
3358 * This function shuts down a device interface and removes it
3359 * from the kernel tables. On success 0 is returned, on a failure
3360 * a negative errno code is returned.
3361 *
3362 * This is just a wrapper for unregister_netdevice that takes
3363 * the rtnl semaphore. In general you want to use this and not
3364 * unregister_netdevice.
3365 */
3366 void unregister_netdev(struct net_device *dev)
3367 {
3368 rtnl_lock();
3369 unregister_netdevice(dev);
3370 rtnl_unlock();
3371 }
3372
3373 EXPORT_SYMBOL(unregister_netdev);
3374
3375 #ifdef CONFIG_HOTPLUG_CPU
3376 static int dev_cpu_callback(struct notifier_block *nfb,
3377 unsigned long action,
3378 void *ocpu)
3379 {
3380 struct sk_buff **list_skb;
3381 struct net_device **list_net;
3382 struct sk_buff *skb;
3383 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3384 struct softnet_data *sd, *oldsd;
3385
3386 if (action != CPU_DEAD)
3387 return NOTIFY_OK;
3388
3389 local_irq_disable();
3390 cpu = smp_processor_id();
3391 sd = &per_cpu(softnet_data, cpu);
3392 oldsd = &per_cpu(softnet_data, oldcpu);
3393
3394 /* Find end of our completion_queue. */
3395 list_skb = &sd->completion_queue;
3396 while (*list_skb)
3397 list_skb = &(*list_skb)->next;
3398 /* Append completion queue from offline CPU. */
3399 *list_skb = oldsd->completion_queue;
3400 oldsd->completion_queue = NULL;
3401
3402 /* Find end of our output_queue. */
3403 list_net = &sd->output_queue;
3404 while (*list_net)
3405 list_net = &(*list_net)->next_sched;
3406 /* Append output queue from offline CPU. */
3407 *list_net = oldsd->output_queue;
3408 oldsd->output_queue = NULL;
3409
3410 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3411 local_irq_enable();
3412
3413 /* Process offline CPU's input_pkt_queue */
3414 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3415 netif_rx(skb);
3416
3417 return NOTIFY_OK;
3418 }
3419 #endif /* CONFIG_HOTPLUG_CPU */
3420
3421 #ifdef CONFIG_NET_DMA
3422 /**
3423 * net_dma_rebalance -
3424 * This is called when the number of channels allocated to the net_dma_client
3425 * changes. The net_dma_client tries to have one DMA channel per CPU.
3426 */
3427 static void net_dma_rebalance(void)
3428 {
3429 unsigned int cpu, i, n;
3430 struct dma_chan *chan;
3431
3432 lock_cpu_hotplug();
3433
3434 if (net_dma_count == 0) {
3435 for_each_online_cpu(cpu)
3436 rcu_assign_pointer(per_cpu(softnet_data.net_dma, cpu), NULL);
3437 unlock_cpu_hotplug();
3438 return;
3439 }
3440
3441 i = 0;
3442 cpu = first_cpu(cpu_online_map);
3443
3444 rcu_read_lock();
3445 list_for_each_entry(chan, &net_dma_client->channels, client_node) {
3446 n = ((num_online_cpus() / net_dma_count)
3447 + (i < (num_online_cpus() % net_dma_count) ? 1 : 0));
3448
3449 while(n) {
3450 per_cpu(softnet_data.net_dma, cpu) = chan;
3451 cpu = next_cpu(cpu, cpu_online_map);
3452 n--;
3453 }
3454 i++;
3455 }
3456 rcu_read_unlock();
3457
3458 unlock_cpu_hotplug();
3459 }
3460
3461 /**
3462 * netdev_dma_event - event callback for the net_dma_client
3463 * @client: should always be net_dma_client
3464 * @chan: DMA channel for the event
3465 * @event: event type
3466 */
3467 static void netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3468 enum dma_event event)
3469 {
3470 spin_lock(&net_dma_event_lock);
3471 switch (event) {
3472 case DMA_RESOURCE_ADDED:
3473 net_dma_count++;
3474 net_dma_rebalance();
3475 break;
3476 case DMA_RESOURCE_REMOVED:
3477 net_dma_count--;
3478 net_dma_rebalance();
3479 break;
3480 default:
3481 break;
3482 }
3483 spin_unlock(&net_dma_event_lock);
3484 }
3485
3486 /**
3487 * netdev_dma_regiser - register the networking subsystem as a DMA client
3488 */
3489 static int __init netdev_dma_register(void)
3490 {
3491 spin_lock_init(&net_dma_event_lock);
3492 net_dma_client = dma_async_client_register(netdev_dma_event);
3493 if (net_dma_client == NULL)
3494 return -ENOMEM;
3495
3496 dma_async_client_chan_request(net_dma_client, num_online_cpus());
3497 return 0;
3498 }
3499
3500 #else
3501 static int __init netdev_dma_register(void) { return -ENODEV; }
3502 #endif /* CONFIG_NET_DMA */
3503
3504 /*
3505 * Initialize the DEV module. At boot time this walks the device list and
3506 * unhooks any devices that fail to initialise (normally hardware not
3507 * present) and leaves us with a valid list of present and active devices.
3508 *
3509 */
3510
3511 /*
3512 * This is called single threaded during boot, so no need
3513 * to take the rtnl semaphore.
3514 */
3515 static int __init net_dev_init(void)
3516 {
3517 int i, rc = -ENOMEM;
3518
3519 BUG_ON(!dev_boot_phase);
3520
3521 net_random_init();
3522
3523 if (dev_proc_init())
3524 goto out;
3525
3526 if (netdev_sysfs_init())
3527 goto out;
3528
3529 INIT_LIST_HEAD(&ptype_all);
3530 for (i = 0; i < 16; i++)
3531 INIT_LIST_HEAD(&ptype_base[i]);
3532
3533 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3534 INIT_HLIST_HEAD(&dev_name_head[i]);
3535
3536 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3537 INIT_HLIST_HEAD(&dev_index_head[i]);
3538
3539 /*
3540 * Initialise the packet receive queues.
3541 */
3542
3543 for_each_possible_cpu(i) {
3544 struct softnet_data *queue;
3545
3546 queue = &per_cpu(softnet_data, i);
3547 skb_queue_head_init(&queue->input_pkt_queue);
3548 queue->completion_queue = NULL;
3549 INIT_LIST_HEAD(&queue->poll_list);
3550 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3551 queue->backlog_dev.weight = weight_p;
3552 queue->backlog_dev.poll = process_backlog;
3553 atomic_set(&queue->backlog_dev.refcnt, 1);
3554 }
3555
3556 netdev_dma_register();
3557
3558 dev_boot_phase = 0;
3559
3560 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3561 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3562
3563 hotcpu_notifier(dev_cpu_callback, 0);
3564 dst_init();
3565 dev_mcast_init();
3566 rc = 0;
3567 out:
3568 return rc;
3569 }
3570
3571 subsys_initcall(net_dev_init);
3572
3573 EXPORT_SYMBOL(__dev_get_by_index);
3574 EXPORT_SYMBOL(__dev_get_by_name);
3575 EXPORT_SYMBOL(__dev_remove_pack);
3576 EXPORT_SYMBOL(dev_valid_name);
3577 EXPORT_SYMBOL(dev_add_pack);
3578 EXPORT_SYMBOL(dev_alloc_name);
3579 EXPORT_SYMBOL(dev_close);
3580 EXPORT_SYMBOL(dev_get_by_flags);
3581 EXPORT_SYMBOL(dev_get_by_index);
3582 EXPORT_SYMBOL(dev_get_by_name);
3583 EXPORT_SYMBOL(dev_open);
3584 EXPORT_SYMBOL(dev_queue_xmit);
3585 EXPORT_SYMBOL(dev_remove_pack);
3586 EXPORT_SYMBOL(dev_set_allmulti);
3587 EXPORT_SYMBOL(dev_set_promiscuity);
3588 EXPORT_SYMBOL(dev_change_flags);
3589 EXPORT_SYMBOL(dev_set_mtu);
3590 EXPORT_SYMBOL(dev_set_mac_address);
3591 EXPORT_SYMBOL(free_netdev);
3592 EXPORT_SYMBOL(netdev_boot_setup_check);
3593 EXPORT_SYMBOL(netdev_set_master);
3594 EXPORT_SYMBOL(netdev_state_change);
3595 EXPORT_SYMBOL(netif_receive_skb);
3596 EXPORT_SYMBOL(netif_rx);
3597 EXPORT_SYMBOL(register_gifconf);
3598 EXPORT_SYMBOL(register_netdevice);
3599 EXPORT_SYMBOL(register_netdevice_notifier);
3600 EXPORT_SYMBOL(skb_checksum_help);
3601 EXPORT_SYMBOL(synchronize_net);
3602 EXPORT_SYMBOL(unregister_netdevice);
3603 EXPORT_SYMBOL(unregister_netdevice_notifier);
3604 EXPORT_SYMBOL(net_enable_timestamp);
3605 EXPORT_SYMBOL(net_disable_timestamp);
3606 EXPORT_SYMBOL(dev_get_flags);
3607
3608 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3609 EXPORT_SYMBOL(br_handle_frame_hook);
3610 EXPORT_SYMBOL(br_fdb_get_hook);
3611 EXPORT_SYMBOL(br_fdb_put_hook);
3612 #endif
3613
3614 #ifdef CONFIG_KMOD
3615 EXPORT_SYMBOL(dev_load);
3616 #endif
3617
3618 EXPORT_PER_CPU_SYMBOL(softnet_data);
This page took 0.124702 seconds and 5 git commands to generate.