blob: 14de297d024d1e8771180dde2e07aec624b90e1c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Jesper Juhl02c30a82005-05-05 16:16:16 -070010 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080078#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/cpu.h>
80#include <linux/types.h>
81#include <linux/kernel.h>
82#include <linux/sched.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080083#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#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>
Adrian Bunkd86b5e02006-01-21 00:46:55 +0100113#include <linux/wireless.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500116#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700117#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700118#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700119#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
122 * The list of packet types we will receive (as opposed to discard)
123 * and the routines to invoke.
124 *
125 * Why 16. Because with 16 the only overlap we get on a hash of the
126 * low nibble of the protocol value is RARP/SNAP/X.25.
127 *
128 * NOTE: That is no longer true with the addition of VLAN tags. Not
129 * sure which should go first, but I bet it won't make much
130 * difference if we are running VLANs. The good news is that
131 * this protocol won't be in the list unless compiled in, so
Stephen Hemminger3041a062006-05-26 13:25:24 -0700132 * the average user (w/out VLANs) will not be adversely affected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * --BLG
134 *
135 * 0800 IP
136 * 8100 802.1Q VLAN
137 * 0001 802.3
138 * 0002 AX.25
139 * 0004 802.2
140 * 8035 RARP
141 * 0005 SNAP
142 * 0805 X.25
143 * 0806 ARP
144 * 8137 IPX
145 * 0009 Localtalk
146 * 86DD IPv6
147 */
148
149static DEFINE_SPINLOCK(ptype_lock);
150static struct list_head ptype_base[16]; /* 16 way hashed list */
151static struct list_head ptype_all; /* Taps */
152
Chris Leechdb217332006-06-17 21:24:58 -0700153#ifdef CONFIG_NET_DMA
154static struct dma_client *net_dma_client;
155static unsigned int net_dma_count;
156static spinlock_t net_dma_event_lock;
157#endif
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
Stephen Hemminger3041a062006-05-26 13:25:24 -0700160 * The @dev_base list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * semaphore.
162 *
163 * Pure readers hold dev_base_lock for reading.
164 *
165 * Writers must hold the rtnl semaphore while they loop through the
166 * dev_base list, and hold dev_base_lock for writing when they do the
167 * actual updates. This allows pure readers to access the list even
168 * while a writer is preparing to update it.
169 *
170 * To put it another way, dev_base_lock is held for writing only to
171 * protect against pure readers; the rtnl semaphore provides the
172 * protection against other writers.
173 *
174 * See, for example usages, register_netdevice() and
175 * unregister_netdevice(), which must be called with the rtnl
176 * semaphore held.
177 */
178struct net_device *dev_base;
179static struct net_device **dev_tail = &dev_base;
180DEFINE_RWLOCK(dev_base_lock);
181
182EXPORT_SYMBOL(dev_base);
183EXPORT_SYMBOL(dev_base_lock);
184
185#define NETDEV_HASHBITS 8
186static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
187static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
188
189static inline struct hlist_head *dev_name_hash(const char *name)
190{
191 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
192 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
193}
194
195static inline struct hlist_head *dev_index_hash(int ifindex)
196{
197 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
198}
199
200/*
201 * Our notifier list
202 */
203
Alan Sternf07d5b92006-05-09 15:23:03 -0700204static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/*
207 * Device drivers call our routines to queue packets here. We empty the
208 * queue in the local softnet handler.
209 */
Stephen Hemminger31aa02c2005-06-23 20:12:48 -0700210DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212#ifdef CONFIG_SYSFS
213extern int netdev_sysfs_init(void);
214extern int netdev_register_sysfs(struct net_device *);
215extern void netdev_unregister_sysfs(struct net_device *);
216#else
217#define netdev_sysfs_init() (0)
218#define netdev_register_sysfs(dev) (0)
219#define netdev_unregister_sysfs(dev) do { } while(0)
220#endif
221
222
223/*******************************************************************************
224
225 Protocol management and registration routines
226
227*******************************************************************************/
228
229/*
230 * For efficiency
231 */
232
Adrian Bunk60481262006-06-25 23:58:10 -0700233static int netdev_nit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235/*
236 * Add a protocol ID to the list. Now that the input handler is
237 * smarter we can dispense with all the messy stuff that used to be
238 * here.
239 *
240 * BEWARE!!! Protocol handlers, mangling input packets,
241 * MUST BE last in hash buckets and checking protocol handlers
242 * MUST start from promiscuous ptype_all chain in net_bh.
243 * It is true now, do not change it.
244 * Explanation follows: if protocol handler, mangling packet, will
245 * be the first on list, it is not able to sense, that packet
246 * is cloned and should be copied-on-write, so that it will
247 * change it and subsequent readers will get broken packet.
248 * --ANK (980803)
249 */
250
251/**
252 * dev_add_pack - add packet handler
253 * @pt: packet type declaration
254 *
255 * Add a protocol handler to the networking stack. The passed &packet_type
256 * is linked into kernel lists and may not be freed until it has been
257 * removed from the kernel lists.
258 *
259 * This call does not sleep therefore it can not
260 * guarantee all CPU's that are in middle of receiving packets
261 * will see the new packet type (until the next received packet).
262 */
263
264void dev_add_pack(struct packet_type *pt)
265{
266 int hash;
267
268 spin_lock_bh(&ptype_lock);
269 if (pt->type == htons(ETH_P_ALL)) {
270 netdev_nit++;
271 list_add_rcu(&pt->list, &ptype_all);
272 } else {
273 hash = ntohs(pt->type) & 15;
274 list_add_rcu(&pt->list, &ptype_base[hash]);
275 }
276 spin_unlock_bh(&ptype_lock);
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/**
280 * __dev_remove_pack - remove packet handler
281 * @pt: packet type declaration
282 *
283 * Remove a protocol handler that was previously added to the kernel
284 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
285 * from the kernel lists and can be freed or reused once this function
286 * returns.
287 *
288 * The packet type might still be in use by receivers
289 * and must not be freed until after all the CPU's have gone
290 * through a quiescent state.
291 */
292void __dev_remove_pack(struct packet_type *pt)
293{
294 struct list_head *head;
295 struct packet_type *pt1;
296
297 spin_lock_bh(&ptype_lock);
298
299 if (pt->type == htons(ETH_P_ALL)) {
300 netdev_nit--;
301 head = &ptype_all;
302 } else
303 head = &ptype_base[ntohs(pt->type) & 15];
304
305 list_for_each_entry(pt1, head, list) {
306 if (pt == pt1) {
307 list_del_rcu(&pt->list);
308 goto out;
309 }
310 }
311
312 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
313out:
314 spin_unlock_bh(&ptype_lock);
315}
316/**
317 * dev_remove_pack - remove packet handler
318 * @pt: packet type declaration
319 *
320 * Remove a protocol handler that was previously added to the kernel
321 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
322 * from the kernel lists and can be freed or reused once this function
323 * returns.
324 *
325 * This call sleeps to guarantee that no CPU is looking at the packet
326 * type after return.
327 */
328void dev_remove_pack(struct packet_type *pt)
329{
330 __dev_remove_pack(pt);
331
332 synchronize_net();
333}
334
335/******************************************************************************
336
337 Device Boot-time Settings Routines
338
339*******************************************************************************/
340
341/* Boot time configuration table */
342static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
343
344/**
345 * netdev_boot_setup_add - add new setup entry
346 * @name: name of the device
347 * @map: configured settings for the device
348 *
349 * Adds new setup entry to the dev_boot_setup list. The function
350 * returns 0 on error and 1 on success. This is a generic routine to
351 * all netdevices.
352 */
353static int netdev_boot_setup_add(char *name, struct ifmap *map)
354{
355 struct netdev_boot_setup *s;
356 int i;
357
358 s = dev_boot_setup;
359 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
360 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
361 memset(s[i].name, 0, sizeof(s[i].name));
362 strcpy(s[i].name, name);
363 memcpy(&s[i].map, map, sizeof(s[i].map));
364 break;
365 }
366 }
367
368 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
369}
370
371/**
372 * netdev_boot_setup_check - check boot time settings
373 * @dev: the netdevice
374 *
375 * Check boot time settings for the device.
376 * The found settings are set for the device to be used
377 * later in the device probing.
378 * Returns 0 if no settings found, 1 if they are.
379 */
380int netdev_boot_setup_check(struct net_device *dev)
381{
382 struct netdev_boot_setup *s = dev_boot_setup;
383 int i;
384
385 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
386 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
387 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
388 dev->irq = s[i].map.irq;
389 dev->base_addr = s[i].map.base_addr;
390 dev->mem_start = s[i].map.mem_start;
391 dev->mem_end = s[i].map.mem_end;
392 return 1;
393 }
394 }
395 return 0;
396}
397
398
399/**
400 * netdev_boot_base - get address from boot time settings
401 * @prefix: prefix for network device
402 * @unit: id for network device
403 *
404 * Check boot time settings for the base address of device.
405 * The found settings are set for the device to be used
406 * later in the device probing.
407 * Returns 0 if no settings found.
408 */
409unsigned long netdev_boot_base(const char *prefix, int unit)
410{
411 const struct netdev_boot_setup *s = dev_boot_setup;
412 char name[IFNAMSIZ];
413 int i;
414
415 sprintf(name, "%s%d", prefix, unit);
416
417 /*
418 * If device already registered then return base of 1
419 * to indicate not to probe for this interface
420 */
421 if (__dev_get_by_name(name))
422 return 1;
423
424 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
425 if (!strcmp(name, s[i].name))
426 return s[i].map.base_addr;
427 return 0;
428}
429
430/*
431 * Saves at boot time configured settings for any netdevice.
432 */
433int __init netdev_boot_setup(char *str)
434{
435 int ints[5];
436 struct ifmap map;
437
438 str = get_options(str, ARRAY_SIZE(ints), ints);
439 if (!str || !*str)
440 return 0;
441
442 /* Save settings */
443 memset(&map, 0, sizeof(map));
444 if (ints[0] > 0)
445 map.irq = ints[1];
446 if (ints[0] > 1)
447 map.base_addr = ints[2];
448 if (ints[0] > 2)
449 map.mem_start = ints[3];
450 if (ints[0] > 3)
451 map.mem_end = ints[4];
452
453 /* Add new entry to the list */
454 return netdev_boot_setup_add(str, &map);
455}
456
457__setup("netdev=", netdev_boot_setup);
458
459/*******************************************************************************
460
461 Device Interface Subroutines
462
463*******************************************************************************/
464
465/**
466 * __dev_get_by_name - find a device by its name
467 * @name: name to find
468 *
469 * Find an interface by name. Must be called under RTNL semaphore
470 * or @dev_base_lock. If the name is found a pointer to the device
471 * is returned. If the name is not found then %NULL is returned. The
472 * reference counters are not incremented so the caller must be
473 * careful with locks.
474 */
475
476struct net_device *__dev_get_by_name(const char *name)
477{
478 struct hlist_node *p;
479
480 hlist_for_each(p, dev_name_hash(name)) {
481 struct net_device *dev
482 = hlist_entry(p, struct net_device, name_hlist);
483 if (!strncmp(dev->name, name, IFNAMSIZ))
484 return dev;
485 }
486 return NULL;
487}
488
489/**
490 * dev_get_by_name - find a device by its name
491 * @name: name to find
492 *
493 * Find an interface by name. This can be called from any
494 * context and does its own locking. The returned handle has
495 * the usage count incremented and the caller must use dev_put() to
496 * release it when it is no longer needed. %NULL is returned if no
497 * matching device is found.
498 */
499
500struct net_device *dev_get_by_name(const char *name)
501{
502 struct net_device *dev;
503
504 read_lock(&dev_base_lock);
505 dev = __dev_get_by_name(name);
506 if (dev)
507 dev_hold(dev);
508 read_unlock(&dev_base_lock);
509 return dev;
510}
511
512/**
513 * __dev_get_by_index - find a device by its ifindex
514 * @ifindex: index of device
515 *
516 * Search for an interface by index. Returns %NULL if the device
517 * is not found or a pointer to the device. The device has not
518 * had its reference counter increased so the caller must be careful
519 * about locking. The caller must hold either the RTNL semaphore
520 * or @dev_base_lock.
521 */
522
523struct net_device *__dev_get_by_index(int ifindex)
524{
525 struct hlist_node *p;
526
527 hlist_for_each(p, dev_index_hash(ifindex)) {
528 struct net_device *dev
529 = hlist_entry(p, struct net_device, index_hlist);
530 if (dev->ifindex == ifindex)
531 return dev;
532 }
533 return NULL;
534}
535
536
537/**
538 * dev_get_by_index - find a device by its ifindex
539 * @ifindex: index of device
540 *
541 * Search for an interface by index. Returns NULL if the device
542 * is not found or a pointer to the device. The device returned has
543 * had a reference added and the pointer is safe until the user calls
544 * dev_put to indicate they have finished with it.
545 */
546
547struct net_device *dev_get_by_index(int ifindex)
548{
549 struct net_device *dev;
550
551 read_lock(&dev_base_lock);
552 dev = __dev_get_by_index(ifindex);
553 if (dev)
554 dev_hold(dev);
555 read_unlock(&dev_base_lock);
556 return dev;
557}
558
559/**
560 * dev_getbyhwaddr - find a device by its hardware address
561 * @type: media type of device
562 * @ha: hardware address
563 *
564 * Search for an interface by MAC address. Returns NULL if the device
565 * is not found or a pointer to the device. The caller must hold the
566 * rtnl semaphore. The returned device has not had its ref count increased
567 * and the caller must therefore be careful about locking
568 *
569 * BUGS:
570 * If the API was consistent this would be __dev_get_by_hwaddr
571 */
572
573struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
574{
575 struct net_device *dev;
576
577 ASSERT_RTNL();
578
579 for (dev = dev_base; dev; dev = dev->next)
580 if (dev->type == type &&
581 !memcmp(dev->dev_addr, ha, dev->addr_len))
582 break;
583 return dev;
584}
585
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300586EXPORT_SYMBOL(dev_getbyhwaddr);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588struct net_device *dev_getfirstbyhwtype(unsigned short type)
589{
590 struct net_device *dev;
591
592 rtnl_lock();
593 for (dev = dev_base; dev; dev = dev->next) {
594 if (dev->type == type) {
595 dev_hold(dev);
596 break;
597 }
598 }
599 rtnl_unlock();
600 return dev;
601}
602
603EXPORT_SYMBOL(dev_getfirstbyhwtype);
604
605/**
606 * dev_get_by_flags - find any device with given flags
607 * @if_flags: IFF_* values
608 * @mask: bitmask of bits in if_flags to check
609 *
610 * Search for any interface with the given flags. Returns NULL if a device
611 * is not found or a pointer to the device. The device returned has
612 * had a reference added and the pointer is safe until the user calls
613 * dev_put to indicate they have finished with it.
614 */
615
616struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
617{
618 struct net_device *dev;
619
620 read_lock(&dev_base_lock);
621 for (dev = dev_base; dev != NULL; dev = dev->next) {
622 if (((dev->flags ^ if_flags) & mask) == 0) {
623 dev_hold(dev);
624 break;
625 }
626 }
627 read_unlock(&dev_base_lock);
628 return dev;
629}
630
631/**
632 * dev_valid_name - check if name is okay for network device
633 * @name: name string
634 *
635 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -0700636 * to allow sysfs to work. We also disallow any kind of
637 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Mitch Williamsc2373ee2005-11-09 10:34:45 -0800639int dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
David S. Millerc7fa9d12006-08-15 16:34:13 -0700641 if (*name == '\0')
642 return 0;
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -0700643 if (strlen(name) >= IFNAMSIZ)
644 return 0;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700645 if (!strcmp(name, ".") || !strcmp(name, ".."))
646 return 0;
647
648 while (*name) {
649 if (*name == '/' || isspace(*name))
650 return 0;
651 name++;
652 }
653 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/**
657 * dev_alloc_name - allocate a name for a device
658 * @dev: device
659 * @name: name format string
660 *
661 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -0700662 * id. It scans list of devices to build up a free map, then chooses
663 * the first empty slot. The caller must hold the dev_base or rtnl lock
664 * while allocating the name and adding the device in order to avoid
665 * duplicates.
666 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
667 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
669
670int dev_alloc_name(struct net_device *dev, const char *name)
671{
672 int i = 0;
673 char buf[IFNAMSIZ];
674 const char *p;
675 const int max_netdevices = 8*PAGE_SIZE;
676 long *inuse;
677 struct net_device *d;
678
679 p = strnchr(name, IFNAMSIZ-1, '%');
680 if (p) {
681 /*
682 * Verify the string as this thing may have come from
683 * the user. There must be either one "%d" and no other "%"
684 * characters.
685 */
686 if (p[1] != 'd' || strchr(p + 2, '%'))
687 return -EINVAL;
688
689 /* Use one page as a bit array of possible slots */
690 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
691 if (!inuse)
692 return -ENOMEM;
693
694 for (d = dev_base; d; d = d->next) {
695 if (!sscanf(d->name, name, &i))
696 continue;
697 if (i < 0 || i >= max_netdevices)
698 continue;
699
700 /* avoid cases where sscanf is not exact inverse of printf */
701 snprintf(buf, sizeof(buf), name, i);
702 if (!strncmp(buf, d->name, IFNAMSIZ))
703 set_bit(i, inuse);
704 }
705
706 i = find_first_zero_bit(inuse, max_netdevices);
707 free_page((unsigned long) inuse);
708 }
709
710 snprintf(buf, sizeof(buf), name, i);
711 if (!__dev_get_by_name(buf)) {
712 strlcpy(dev->name, buf, IFNAMSIZ);
713 return i;
714 }
715
716 /* It is possible to run out of possible slots
717 * when the name is long and there isn't enough space left
718 * for the digits, or if all bits are used.
719 */
720 return -ENFILE;
721}
722
723
724/**
725 * dev_change_name - change name of a device
726 * @dev: device
727 * @newname: name (or format string) must be at least IFNAMSIZ
728 *
729 * Change name of a device, can pass format strings "eth%d".
730 * for wildcarding.
731 */
732int dev_change_name(struct net_device *dev, char *newname)
733{
734 int err = 0;
735
736 ASSERT_RTNL();
737
738 if (dev->flags & IFF_UP)
739 return -EBUSY;
740
741 if (!dev_valid_name(newname))
742 return -EINVAL;
743
744 if (strchr(newname, '%')) {
745 err = dev_alloc_name(dev, newname);
746 if (err < 0)
747 return err;
748 strcpy(newname, dev->name);
749 }
750 else if (__dev_get_by_name(newname))
751 return -EEXIST;
752 else
753 strlcpy(dev->name, newname, IFNAMSIZ);
754
755 err = class_device_rename(&dev->class_dev, dev->name);
756 if (!err) {
757 hlist_del(&dev->name_hlist);
758 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
Alan Sternf07d5b92006-05-09 15:23:03 -0700759 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -0800760 NETDEV_CHANGENAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 return err;
764}
765
766/**
Stephen Hemminger3041a062006-05-26 13:25:24 -0700767 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700768 * @dev: device to cause notification
769 *
770 * Called to indicate a device has changed features.
771 */
772void netdev_features_change(struct net_device *dev)
773{
Alan Sternf07d5b92006-05-09 15:23:03 -0700774 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700775}
776EXPORT_SYMBOL(netdev_features_change);
777
778/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * netdev_state_change - device changes state
780 * @dev: device to cause notification
781 *
782 * Called to indicate a device has changed state. This function calls
783 * the notifier chains for netdev_chain and sends a NEWLINK message
784 * to the routing socket.
785 */
786void netdev_state_change(struct net_device *dev)
787{
788 if (dev->flags & IFF_UP) {
Alan Sternf07d5b92006-05-09 15:23:03 -0700789 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -0800790 NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
792 }
793}
794
795/**
796 * dev_load - load a network module
797 * @name: name of interface
798 *
799 * If a network interface is not present and the process has suitable
800 * privileges this function loads the module. If module loading is not
801 * available in this kernel then it becomes a nop.
802 */
803
804void dev_load(const char *name)
805{
806 struct net_device *dev;
807
808 read_lock(&dev_base_lock);
809 dev = __dev_get_by_name(name);
810 read_unlock(&dev_base_lock);
811
812 if (!dev && capable(CAP_SYS_MODULE))
813 request_module("%s", name);
814}
815
816static int default_rebuild_header(struct sk_buff *skb)
817{
818 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
819 skb->dev ? skb->dev->name : "NULL!!!");
820 kfree_skb(skb);
821 return 1;
822}
823
824
825/**
826 * dev_open - prepare an interface for use.
827 * @dev: device to open
828 *
829 * Takes a device from down to up state. The device's private open
830 * function is invoked and then the multicast lists are loaded. Finally
831 * the device is moved into the up state and a %NETDEV_UP message is
832 * sent to the netdev notifier chain.
833 *
834 * Calling this function on an active interface is a nop. On a failure
835 * a negative errno code is returned.
836 */
837int dev_open(struct net_device *dev)
838{
839 int ret = 0;
840
841 /*
842 * Is it already up?
843 */
844
845 if (dev->flags & IFF_UP)
846 return 0;
847
848 /*
849 * Is it even present?
850 */
851 if (!netif_device_present(dev))
852 return -ENODEV;
853
854 /*
855 * Call device private open method
856 */
857 set_bit(__LINK_STATE_START, &dev->state);
858 if (dev->open) {
859 ret = dev->open(dev);
860 if (ret)
861 clear_bit(__LINK_STATE_START, &dev->state);
862 }
863
864 /*
865 * If it went open OK then:
866 */
867
868 if (!ret) {
869 /*
870 * Set the flags.
871 */
872 dev->flags |= IFF_UP;
873
874 /*
875 * Initialize multicasting status
876 */
877 dev_mc_upload(dev);
878
879 /*
880 * Wakeup transmit queue engine
881 */
882 dev_activate(dev);
883
884 /*
885 * ... and announce new interface.
886 */
Alan Sternf07d5b92006-05-09 15:23:03 -0700887 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 return ret;
890}
891
892/**
893 * dev_close - shutdown an interface.
894 * @dev: device to shutdown
895 *
896 * This function moves an active device into down state. A
897 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
898 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
899 * chain.
900 */
901int dev_close(struct net_device *dev)
902{
903 if (!(dev->flags & IFF_UP))
904 return 0;
905
906 /*
907 * Tell people we are going down, so that they can
908 * prepare to death, when device is still operating.
909 */
Alan Sternf07d5b92006-05-09 15:23:03 -0700910 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 dev_deactivate(dev);
913
914 clear_bit(__LINK_STATE_START, &dev->state);
915
916 /* Synchronize to scheduled poll. We cannot touch poll list,
917 * it can be even on different cpu. So just clear netif_running(),
918 * and wait when poll really will happen. Actually, the best place
919 * for this is inside dev->stop() after device stopped its irq
920 * engine, but this requires more changes in devices. */
921
922 smp_mb__after_clear_bit(); /* Commit netif_running(). */
923 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
924 /* No hurry. */
David S. Miller6192b542005-07-28 12:12:58 -0700925 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927
928 /*
929 * Call the device specific close. This cannot fail.
930 * Only if device is UP
931 *
932 * We allow it to be called even after a DETACH hot-plug
933 * event.
934 */
935 if (dev->stop)
936 dev->stop(dev);
937
938 /*
939 * Device is now down.
940 */
941
942 dev->flags &= ~IFF_UP;
943
944 /*
945 * Tell people we are down
946 */
Alan Sternf07d5b92006-05-09 15:23:03 -0700947 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 return 0;
950}
951
952
953/*
954 * Device change register/unregister. These are not inline or static
955 * as we export them to the world.
956 */
957
958/**
959 * register_netdevice_notifier - register a network notifier block
960 * @nb: notifier
961 *
962 * Register a notifier to be called when network device events occur.
963 * The notifier passed is linked into the kernel structures and must
964 * not be reused until it has been unregistered. A negative errno code
965 * is returned on a failure.
966 *
967 * When registered all registration and up events are replayed
968 * to the new notifier to allow device to have a race free
969 * view of the network device list.
970 */
971
972int register_netdevice_notifier(struct notifier_block *nb)
973{
974 struct net_device *dev;
975 int err;
976
977 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -0700978 err = raw_notifier_chain_register(&netdev_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!err) {
980 for (dev = dev_base; dev; dev = dev->next) {
981 nb->notifier_call(nb, NETDEV_REGISTER, dev);
982
983 if (dev->flags & IFF_UP)
984 nb->notifier_call(nb, NETDEV_UP, dev);
985 }
986 }
987 rtnl_unlock();
988 return err;
989}
990
991/**
992 * unregister_netdevice_notifier - unregister a network notifier block
993 * @nb: notifier
994 *
995 * Unregister a notifier previously registered by
996 * register_netdevice_notifier(). The notifier is unlinked into the
997 * kernel structures and may then be reused. A negative errno code
998 * is returned on a failure.
999 */
1000
1001int unregister_netdevice_notifier(struct notifier_block *nb)
1002{
Herbert Xu9f514952006-03-25 01:24:25 -08001003 int err;
1004
1005 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001006 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xu9f514952006-03-25 01:24:25 -08001007 rtnl_unlock();
1008 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011/**
1012 * call_netdevice_notifiers - call all network notifier blocks
1013 * @val: value passed unmodified to notifier function
1014 * @v: pointer passed unmodified to notifier function
1015 *
1016 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001017 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
1019
1020int call_netdevice_notifiers(unsigned long val, void *v)
1021{
Alan Sternf07d5b92006-05-09 15:23:03 -07001022 return raw_notifier_call_chain(&netdev_chain, val, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025/* When > 0 there are consumers of rx skb time stamps */
1026static atomic_t netstamp_needed = ATOMIC_INIT(0);
1027
1028void net_enable_timestamp(void)
1029{
1030 atomic_inc(&netstamp_needed);
1031}
1032
1033void net_disable_timestamp(void)
1034{
1035 atomic_dec(&netstamp_needed);
1036}
1037
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001038void __net_timestamp(struct sk_buff *skb)
1039{
1040 struct timeval tv;
1041
1042 do_gettimeofday(&tv);
1043 skb_set_timestamp(skb, &tv);
1044}
1045EXPORT_SYMBOL(__net_timestamp);
1046
1047static inline void net_timestamp(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 if (atomic_read(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001050 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 else {
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001052 skb->tstamp.off_sec = 0;
1053 skb->tstamp.off_usec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055}
1056
1057/*
1058 * Support routine. Sends outgoing frames to any network
1059 * taps currently in use.
1060 */
1061
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001062static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct packet_type *ptype;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001065
1066 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 rcu_read_lock();
1069 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1070 /* Never send packets back to the socket
1071 * they originated from - MvS (miquels@drinkel.ow.org)
1072 */
1073 if ((ptype->dev == dev || !ptype->dev) &&
1074 (ptype->af_packet_priv == NULL ||
1075 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1076 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1077 if (!skb2)
1078 break;
1079
1080 /* skb->nh should be correctly
1081 set by sender, so that the second statement is
1082 just protection against buggy protocols.
1083 */
1084 skb2->mac.raw = skb2->data;
1085
1086 if (skb2->nh.raw < skb2->data ||
1087 skb2->nh.raw > skb2->tail) {
1088 if (net_ratelimit())
1089 printk(KERN_CRIT "protocol %04x is "
1090 "buggy, dev %s\n",
1091 skb2->protocol, dev->name);
1092 skb2->nh.raw = skb2->data;
1093 }
1094
1095 skb2->h.raw = skb2->nh.raw;
1096 skb2->pkt_type = PACKET_OUTGOING;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001097 ptype->func(skb2, skb->dev, ptype, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099 }
1100 rcu_read_unlock();
1101}
1102
Denis Vlasenko56079432006-03-29 15:57:29 -08001103
1104void __netif_schedule(struct net_device *dev)
1105{
1106 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1107 unsigned long flags;
1108 struct softnet_data *sd;
1109
1110 local_irq_save(flags);
1111 sd = &__get_cpu_var(softnet_data);
1112 dev->next_sched = sd->output_queue;
1113 sd->output_queue = dev;
1114 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1115 local_irq_restore(flags);
1116 }
1117}
1118EXPORT_SYMBOL(__netif_schedule);
1119
1120void __netif_rx_schedule(struct net_device *dev)
1121{
1122 unsigned long flags;
1123
1124 local_irq_save(flags);
1125 dev_hold(dev);
1126 list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
1127 if (dev->quota < 0)
1128 dev->quota += dev->weight;
1129 else
1130 dev->quota = dev->weight;
1131 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1132 local_irq_restore(flags);
1133}
1134EXPORT_SYMBOL(__netif_rx_schedule);
1135
1136void dev_kfree_skb_any(struct sk_buff *skb)
1137{
1138 if (in_irq() || irqs_disabled())
1139 dev_kfree_skb_irq(skb);
1140 else
1141 dev_kfree_skb(skb);
1142}
1143EXPORT_SYMBOL(dev_kfree_skb_any);
1144
1145
1146/* Hot-plugging. */
1147void netif_device_detach(struct net_device *dev)
1148{
1149 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1150 netif_running(dev)) {
1151 netif_stop_queue(dev);
1152 }
1153}
1154EXPORT_SYMBOL(netif_device_detach);
1155
1156void netif_device_attach(struct net_device *dev)
1157{
1158 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1159 netif_running(dev)) {
1160 netif_wake_queue(dev);
1161 __netdev_watchdog_up(dev);
1162 }
1163}
1164EXPORT_SYMBOL(netif_device_attach);
1165
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167/*
1168 * Invalidate hardware checksum when packet is to be mangled, and
1169 * complete checksum manually on outgoing path.
1170 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07001171int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 unsigned int csum;
1174 int ret = 0, offset = skb->h.raw - skb->data;
1175
Patrick McHardy84fa7932006-08-29 16:44:56 -07001176 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07001177 goto out_set_summed;
1178
1179 if (unlikely(skb_shinfo(skb)->gso_size)) {
Herbert Xua430a432006-07-08 13:34:56 -07001180 /* Let GSO fix up the checksum. */
1181 goto out_set_summed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 if (skb_cloned(skb)) {
1185 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1186 if (ret)
1187 goto out;
1188 }
1189
Kris Katterjohn09a62662006-01-08 22:24:28 -08001190 BUG_ON(offset > (int)skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1192
1193 offset = skb->tail - skb->h.raw;
Kris Katterjohn09a62662006-01-08 22:24:28 -08001194 BUG_ON(offset <= 0);
1195 BUG_ON(skb->csum + 2 > offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
Herbert Xua430a432006-07-08 13:34:56 -07001198
1199out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 skb->ip_summed = CHECKSUM_NONE;
1201out:
1202 return ret;
1203}
1204
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001205/**
1206 * skb_gso_segment - Perform segmentation on skb.
1207 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001208 * @features: features for the output path (see dev->features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001209 *
1210 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07001211 *
1212 * It may return NULL if the skb requires no segmentation. This is
1213 * only possible when GSO is used for verifying header integrity.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001214 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001215struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001216{
1217 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1218 struct packet_type *ptype;
1219 int type = skb->protocol;
Herbert Xua430a432006-07-08 13:34:56 -07001220 int err;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001221
1222 BUG_ON(skb_shinfo(skb)->frag_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001223
1224 skb->mac.raw = skb->data;
1225 skb->mac_len = skb->nh.raw - skb->data;
1226 __skb_pull(skb, skb->mac_len);
1227
Patrick McHardy84fa7932006-08-29 16:44:56 -07001228 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001229 if (skb_header_cloned(skb) &&
1230 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1231 return ERR_PTR(err);
1232 }
1233
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001234 rcu_read_lock();
1235 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1236 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07001237 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001238 err = ptype->gso_send_check(skb);
1239 segs = ERR_PTR(err);
1240 if (err || skb_gso_ok(skb, features))
1241 break;
1242 __skb_push(skb, skb->data - skb->nh.raw);
1243 }
Herbert Xu576a30e2006-06-27 13:22:38 -07001244 segs = ptype->gso_segment(skb, features);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001245 break;
1246 }
1247 }
1248 rcu_read_unlock();
1249
Herbert Xu576a30e2006-06-27 13:22:38 -07001250 __skb_push(skb, skb->data - skb->mac.raw);
1251
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001252 return segs;
1253}
1254
1255EXPORT_SYMBOL(skb_gso_segment);
1256
Herbert Xufb286bb2005-11-10 13:01:24 -08001257/* Take action when hardware reception checksum errors are detected. */
1258#ifdef CONFIG_BUG
1259void netdev_rx_csum_fault(struct net_device *dev)
1260{
1261 if (net_ratelimit()) {
Stephen Hemminger246a4212005-12-08 15:21:39 -08001262 printk(KERN_ERR "%s: hw csum failure.\n",
1263 dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08001264 dump_stack();
1265 }
1266}
1267EXPORT_SYMBOL(netdev_rx_csum_fault);
1268#endif
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/* Actually, we should eliminate this check as soon as we know, that:
1271 * 1. IOMMU is present and allows to map all the memory.
1272 * 2. No high memory really exists on this machine.
1273 */
1274
1275static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1276{
Herbert Xu3d3a8532006-06-27 13:33:10 -07001277#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int i;
1279
1280 if (dev->features & NETIF_F_HIGHDMA)
1281 return 0;
1282
1283 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1284 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1285 return 1;
1286
Herbert Xu3d3a8532006-06-27 13:33:10 -07001287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return 0;
1289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001291struct dev_gso_cb {
1292 void (*destructor)(struct sk_buff *skb);
1293};
1294
1295#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1296
1297static void dev_gso_skb_destructor(struct sk_buff *skb)
1298{
1299 struct dev_gso_cb *cb;
1300
1301 do {
1302 struct sk_buff *nskb = skb->next;
1303
1304 skb->next = nskb->next;
1305 nskb->next = NULL;
1306 kfree_skb(nskb);
1307 } while (skb->next);
1308
1309 cb = DEV_GSO_CB(skb);
1310 if (cb->destructor)
1311 cb->destructor(skb);
1312}
1313
1314/**
1315 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1316 * @skb: buffer to segment
1317 *
1318 * This function segments the given skb and stores the list of segments
1319 * in skb->next.
1320 */
1321static int dev_gso_segment(struct sk_buff *skb)
1322{
1323 struct net_device *dev = skb->dev;
1324 struct sk_buff *segs;
Herbert Xu576a30e2006-06-27 13:22:38 -07001325 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1326 NETIF_F_SG : 0);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001327
Herbert Xu576a30e2006-06-27 13:22:38 -07001328 segs = skb_gso_segment(skb, features);
1329
1330 /* Verifying header integrity only. */
1331 if (!segs)
1332 return 0;
1333
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001334 if (unlikely(IS_ERR(segs)))
1335 return PTR_ERR(segs);
1336
1337 skb->next = segs;
1338 DEV_GSO_CB(skb)->destructor = skb->destructor;
1339 skb->destructor = dev_gso_skb_destructor;
1340
1341 return 0;
1342}
1343
1344int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1345{
1346 if (likely(!skb->next)) {
1347 if (netdev_nit)
1348 dev_queue_xmit_nit(skb, dev);
1349
Herbert Xu576a30e2006-06-27 13:22:38 -07001350 if (netif_needs_gso(dev, skb)) {
1351 if (unlikely(dev_gso_segment(skb)))
1352 goto out_kfree_skb;
1353 if (skb->next)
1354 goto gso;
1355 }
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001356
Herbert Xu576a30e2006-06-27 13:22:38 -07001357 return dev->hard_start_xmit(skb, dev);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001358 }
1359
Herbert Xu576a30e2006-06-27 13:22:38 -07001360gso:
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001361 do {
1362 struct sk_buff *nskb = skb->next;
1363 int rc;
1364
1365 skb->next = nskb->next;
1366 nskb->next = NULL;
1367 rc = dev->hard_start_xmit(nskb, dev);
1368 if (unlikely(rc)) {
Michael Chanf54d9e82006-06-25 23:57:04 -07001369 nskb->next = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001370 skb->next = nskb;
1371 return rc;
1372 }
Michael Chanf54d9e82006-06-25 23:57:04 -07001373 if (unlikely(netif_queue_stopped(dev) && skb->next))
1374 return NETDEV_TX_BUSY;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001375 } while (skb->next);
1376
1377 skb->destructor = DEV_GSO_CB(skb)->destructor;
1378
1379out_kfree_skb:
1380 kfree_skb(skb);
1381 return 0;
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#define HARD_TX_LOCK(dev, cpu) { \
1385 if ((dev->features & NETIF_F_LLTX) == 0) { \
Herbert Xu932ff272006-06-09 12:20:56 -07001386 netif_tx_lock(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 } \
1388}
1389
1390#define HARD_TX_UNLOCK(dev) { \
1391 if ((dev->features & NETIF_F_LLTX) == 0) { \
Herbert Xu932ff272006-06-09 12:20:56 -07001392 netif_tx_unlock(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 } \
1394}
1395
1396/**
1397 * dev_queue_xmit - transmit a buffer
1398 * @skb: buffer to transmit
1399 *
1400 * Queue a buffer for transmission to a network device. The caller must
1401 * have set the device and priority and built the buffer before calling
1402 * this function. The function can be called from an interrupt.
1403 *
1404 * A negative errno code is returned on a failure. A success does not
1405 * guarantee the frame will be transmitted as it may be dropped due
1406 * to congestion or traffic shaping.
Ben Greearaf191362005-04-24 20:12:36 -07001407 *
1408 * -----------------------------------------------------------------------------------
1409 * I notice this method can also return errors from the queue disciplines,
1410 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1411 * be positive.
1412 *
1413 * Regardless of the return value, the skb is consumed, so it is currently
1414 * difficult to retry a send to this method. (You can bump the ref count
1415 * before sending to hold a reference for retry if you are careful.)
1416 *
1417 * When calling this method, interrupts MUST be enabled. This is because
1418 * the BH enable code must have IRQs enabled so that it will not deadlock.
1419 * --BLG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
1421
1422int dev_queue_xmit(struct sk_buff *skb)
1423{
1424 struct net_device *dev = skb->dev;
1425 struct Qdisc *q;
1426 int rc = -ENOMEM;
1427
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001428 /* GSO will handle the following emulations directly. */
1429 if (netif_needs_gso(dev, skb))
1430 goto gso;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (skb_shinfo(skb)->frag_list &&
1433 !(dev->features & NETIF_F_FRAGLIST) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001434 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto out_kfree_skb;
1436
1437 /* Fragmented skb is linearized if device does not support SG,
1438 * or if at least one of fragments is in highmem and device
1439 * does not support DMA from it.
1440 */
1441 if (skb_shinfo(skb)->nr_frags &&
1442 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001443 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 goto out_kfree_skb;
1445
1446 /* If packet is not checksummed and device does not support
1447 * checksumming for this protocol, complete checksumming here.
1448 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07001449 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Herbert Xu8648b302006-06-17 22:06:05 -07001450 (!(dev->features & NETIF_F_GEN_CSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 (!(dev->features & NETIF_F_IP_CSUM) ||
1452 skb->protocol != htons(ETH_P_IP))))
Patrick McHardy84fa7932006-08-29 16:44:56 -07001453 if (skb_checksum_help(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 goto out_kfree_skb;
1455
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001456gso:
Eric Dumazet2d7ceec2005-09-27 15:22:58 -07001457 spin_lock_prefetch(&dev->queue_lock);
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /* Disable soft irqs for various locks below. Also
1460 * stops preemption for RCU.
1461 */
Herbert Xud4828d82006-06-22 02:28:18 -07001462 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* Updates of qdisc are serialized by queue_lock.
1465 * The struct Qdisc which is pointed to by qdisc is now a
1466 * rcu structure - it may be accessed without acquiring
1467 * a lock (but the structure may be stale.) The freeing of the
1468 * qdisc will be deferred until it's known that there are no
1469 * more references to it.
1470 *
1471 * If the qdisc has an enqueue function, we still need to
1472 * hold the queue_lock before calling it, since queue_lock
1473 * also serializes access to the device queue.
1474 */
1475
1476 q = rcu_dereference(dev->qdisc);
1477#ifdef CONFIG_NET_CLS_ACT
1478 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1479#endif
1480 if (q->enqueue) {
1481 /* Grab device queue */
1482 spin_lock(&dev->queue_lock);
1483
1484 rc = q->enqueue(skb, q);
1485
1486 qdisc_run(dev);
1487
1488 spin_unlock(&dev->queue_lock);
1489 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1490 goto out;
1491 }
1492
1493 /* The device has no queue. Common case for software devices:
1494 loopback, all the sorts of tunnels...
1495
Herbert Xu932ff272006-06-09 12:20:56 -07001496 Really, it is unlikely that netif_tx_lock protection is necessary
1497 here. (f.e. loopback and IP tunnels are clean ignoring statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 counters.)
1499 However, it is possible, that they rely on protection
1500 made by us here.
1501
1502 Check this and shot the lock. It is not prone from deadlocks.
1503 Either shot noqueue qdisc, it is even simpler 8)
1504 */
1505 if (dev->flags & IFF_UP) {
1506 int cpu = smp_processor_id(); /* ok because BHs are off */
1507
1508 if (dev->xmit_lock_owner != cpu) {
1509
1510 HARD_TX_LOCK(dev, cpu);
1511
1512 if (!netif_queue_stopped(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 rc = 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001514 if (!dev_hard_start_xmit(skb, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 HARD_TX_UNLOCK(dev);
1516 goto out;
1517 }
1518 }
1519 HARD_TX_UNLOCK(dev);
1520 if (net_ratelimit())
1521 printk(KERN_CRIT "Virtual device %s asks to "
1522 "queue packet!\n", dev->name);
1523 } else {
1524 /* Recursion is detected! It is possible,
1525 * unfortunately */
1526 if (net_ratelimit())
1527 printk(KERN_CRIT "Dead loop on virtual device "
1528 "%s, fix it urgently!\n", dev->name);
1529 }
1530 }
1531
1532 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07001533 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535out_kfree_skb:
1536 kfree_skb(skb);
1537 return rc;
1538out:
Herbert Xud4828d82006-06-22 02:28:18 -07001539 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return rc;
1541}
1542
1543
1544/*=======================================================================
1545 Receiver routines
1546 =======================================================================*/
1547
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07001548int netdev_max_backlog = 1000;
1549int netdev_budget = 300;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550int weight_p = 64; /* old backlog weight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1553
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/**
1556 * netif_rx - post buffer to the network code
1557 * @skb: buffer to post
1558 *
1559 * This function receives a packet from a device driver and queues it for
1560 * the upper (protocol) levels to process. It always succeeds. The buffer
1561 * may be dropped during processing for congestion control or by the
1562 * protocol layers.
1563 *
1564 * return values:
1565 * NET_RX_SUCCESS (no congestion)
1566 * NET_RX_CN_LOW (low congestion)
1567 * NET_RX_CN_MOD (moderate congestion)
1568 * NET_RX_CN_HIGH (high congestion)
1569 * NET_RX_DROP (packet was dropped)
1570 *
1571 */
1572
1573int netif_rx(struct sk_buff *skb)
1574{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 struct softnet_data *queue;
1576 unsigned long flags;
1577
1578 /* if netpoll wants it, pretend we never saw it */
1579 if (netpoll_rx(skb))
1580 return NET_RX_DROP;
1581
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001582 if (!skb->tstamp.off_sec)
1583 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 /*
1586 * The code is rearranged so that the path is the most
1587 * short when CPU is congested, but is still operating.
1588 */
1589 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 queue = &__get_cpu_var(softnet_data);
1591
1592 __get_cpu_var(netdev_rx_stat).total++;
1593 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1594 if (queue->input_pkt_queue.qlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595enqueue:
1596 dev_hold(skb->dev);
1597 __skb_queue_tail(&queue->input_pkt_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 local_irq_restore(flags);
Stephen Hemminger34008d82005-06-23 20:10:00 -07001599 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 netif_rx_schedule(&queue->backlog_dev);
1603 goto enqueue;
1604 }
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 __get_cpu_var(netdev_rx_stat).dropped++;
1607 local_irq_restore(flags);
1608
1609 kfree_skb(skb);
1610 return NET_RX_DROP;
1611}
1612
1613int netif_rx_ni(struct sk_buff *skb)
1614{
1615 int err;
1616
1617 preempt_disable();
1618 err = netif_rx(skb);
1619 if (local_softirq_pending())
1620 do_softirq();
1621 preempt_enable();
1622
1623 return err;
1624}
1625
1626EXPORT_SYMBOL(netif_rx_ni);
1627
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001628static inline struct net_device *skb_bond(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 struct net_device *dev = skb->dev;
1631
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001632 if (dev->master) {
David S. Miller7ea49ed2006-08-14 17:08:36 -07001633 if (skb_bond_should_drop(skb)) {
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001634 kfree_skb(skb);
1635 return NULL;
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 skb->dev = dev->master;
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001638 }
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001639
1640 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static void net_tx_action(struct softirq_action *h)
1644{
1645 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1646
1647 if (sd->completion_queue) {
1648 struct sk_buff *clist;
1649
1650 local_irq_disable();
1651 clist = sd->completion_queue;
1652 sd->completion_queue = NULL;
1653 local_irq_enable();
1654
1655 while (clist) {
1656 struct sk_buff *skb = clist;
1657 clist = clist->next;
1658
1659 BUG_TRAP(!atomic_read(&skb->users));
1660 __kfree_skb(skb);
1661 }
1662 }
1663
1664 if (sd->output_queue) {
1665 struct net_device *head;
1666
1667 local_irq_disable();
1668 head = sd->output_queue;
1669 sd->output_queue = NULL;
1670 local_irq_enable();
1671
1672 while (head) {
1673 struct net_device *dev = head;
1674 head = head->next_sched;
1675
1676 smp_mb__before_clear_bit();
1677 clear_bit(__LINK_STATE_SCHED, &dev->state);
1678
1679 if (spin_trylock(&dev->queue_lock)) {
1680 qdisc_run(dev);
1681 spin_unlock(&dev->queue_lock);
1682 } else {
1683 netif_schedule(dev);
1684 }
1685 }
1686 }
1687}
1688
1689static __inline__ int deliver_skb(struct sk_buff *skb,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001690 struct packet_type *pt_prev,
1691 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 atomic_inc(&skb->users);
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001694 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
1697#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1698int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1699struct net_bridge;
1700struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1701 unsigned char *addr);
1702void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
1703
1704static __inline__ int handle_bridge(struct sk_buff **pskb,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001705 struct packet_type **pt_prev, int *ret,
1706 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 struct net_bridge_port *port;
1709
1710 if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1711 (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1712 return 0;
1713
1714 if (*pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001715 *ret = deliver_skb(*pskb, *pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 *pt_prev = NULL;
1717 }
1718
1719 return br_handle_frame_hook(port, pskb);
1720}
1721#else
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001722#define handle_bridge(skb, pt_prev, ret, orig_dev) (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723#endif
1724
1725#ifdef CONFIG_NET_CLS_ACT
1726/* TODO: Maybe we should just force sch_ingress to be compiled in
1727 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1728 * a compare and 2 stores extra right now if we dont have it on
1729 * but have CONFIG_NET_CLS_ACT
1730 * NOTE: This doesnt stop any functionality; if you dont have
1731 * the ingress scheduler, you just cant add policies on ingress.
1732 *
1733 */
1734static int ing_filter(struct sk_buff *skb)
1735{
1736 struct Qdisc *q;
1737 struct net_device *dev = skb->dev;
1738 int result = TC_ACT_OK;
1739
1740 if (dev->qdisc_ingress) {
1741 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1742 if (MAX_RED_LOOP < ttl++) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07001743 printk(KERN_WARNING "Redir loop detected Dropping packet (%s->%s)\n",
David S. Miller86e65da2005-08-09 19:36:29 -07001744 skb->input_dev->name, skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return TC_ACT_SHOT;
1746 }
1747
1748 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1749
1750 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
David S. Miller86e65da2005-08-09 19:36:29 -07001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 spin_lock(&dev->ingress_lock);
1753 if ((q = dev->qdisc_ingress) != NULL)
1754 result = q->enqueue(skb, q);
1755 spin_unlock(&dev->ingress_lock);
1756
1757 }
1758
1759 return result;
1760}
1761#endif
1762
1763int netif_receive_skb(struct sk_buff *skb)
1764{
1765 struct packet_type *ptype, *pt_prev;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001766 struct net_device *orig_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int ret = NET_RX_DROP;
1768 unsigned short type;
1769
1770 /* if we've gotten here through NAPI, check netpoll */
1771 if (skb->dev->poll && netpoll_rx(skb))
1772 return NET_RX_DROP;
1773
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001774 if (!skb->tstamp.off_sec)
1775 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
David S. Miller86e65da2005-08-09 19:36:29 -07001777 if (!skb->input_dev)
1778 skb->input_dev = skb->dev;
1779
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001780 orig_dev = skb_bond(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001782 if (!orig_dev)
1783 return NET_RX_DROP;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 __get_cpu_var(netdev_rx_stat).total++;
1786
1787 skb->h.raw = skb->nh.raw = skb->data;
1788 skb->mac_len = skb->nh.raw - skb->mac.raw;
1789
1790 pt_prev = NULL;
1791
1792 rcu_read_lock();
1793
1794#ifdef CONFIG_NET_CLS_ACT
1795 if (skb->tc_verd & TC_NCLS) {
1796 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1797 goto ncls;
1798 }
1799#endif
1800
1801 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1802 if (!ptype->dev || ptype->dev == skb->dev) {
1803 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001804 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 pt_prev = ptype;
1806 }
1807 }
1808
1809#ifdef CONFIG_NET_CLS_ACT
1810 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001811 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 pt_prev = NULL; /* noone else should process this after*/
1813 } else {
1814 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1815 }
1816
1817 ret = ing_filter(skb);
1818
1819 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1820 kfree_skb(skb);
1821 goto out;
1822 }
1823
1824 skb->tc_verd = 0;
1825ncls:
1826#endif
1827
1828 handle_diverter(skb);
1829
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001830 if (handle_bridge(&skb, &pt_prev, &ret, orig_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 goto out;
1832
1833 type = skb->protocol;
1834 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1835 if (ptype->type == type &&
1836 (!ptype->dev || ptype->dev == skb->dev)) {
1837 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001838 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 pt_prev = ptype;
1840 }
1841 }
1842
1843 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001844 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 } else {
1846 kfree_skb(skb);
1847 /* Jamal, now you will not able to escape explaining
1848 * me how you were going to use this. :-)
1849 */
1850 ret = NET_RX_DROP;
1851 }
1852
1853out:
1854 rcu_read_unlock();
1855 return ret;
1856}
1857
1858static int process_backlog(struct net_device *backlog_dev, int *budget)
1859{
1860 int work = 0;
1861 int quota = min(backlog_dev->quota, *budget);
1862 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1863 unsigned long start_time = jiffies;
1864
Stephen Hemmingere3876602005-06-08 14:56:01 -07001865 backlog_dev->weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 for (;;) {
1867 struct sk_buff *skb;
1868 struct net_device *dev;
1869
1870 local_irq_disable();
1871 skb = __skb_dequeue(&queue->input_pkt_queue);
1872 if (!skb)
1873 goto job_done;
1874 local_irq_enable();
1875
1876 dev = skb->dev;
1877
1878 netif_receive_skb(skb);
1879
1880 dev_put(dev);
1881
1882 work++;
1883
1884 if (work >= quota || jiffies - start_time > 1)
1885 break;
1886
1887 }
1888
1889 backlog_dev->quota -= work;
1890 *budget -= work;
1891 return -1;
1892
1893job_done:
1894 backlog_dev->quota -= work;
1895 *budget -= work;
1896
1897 list_del(&backlog_dev->poll_list);
1898 smp_mb__before_clear_bit();
1899 netif_poll_enable(backlog_dev);
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 local_irq_enable();
1902 return 0;
1903}
1904
1905static void net_rx_action(struct softirq_action *h)
1906{
1907 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1908 unsigned long start_time = jiffies;
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07001909 int budget = netdev_budget;
Matt Mackall53fb95d2005-08-11 19:27:43 -07001910 void *have;
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 local_irq_disable();
1913
1914 while (!list_empty(&queue->poll_list)) {
1915 struct net_device *dev;
1916
1917 if (budget <= 0 || jiffies - start_time > 1)
1918 goto softnet_break;
1919
1920 local_irq_enable();
1921
1922 dev = list_entry(queue->poll_list.next,
1923 struct net_device, poll_list);
Matt Mackall53fb95d2005-08-11 19:27:43 -07001924 have = netpoll_poll_lock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
Matt Mackall53fb95d2005-08-11 19:27:43 -07001927 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 local_irq_disable();
Stephen Hemminger8aca8a22006-03-20 22:26:39 -08001929 list_move_tail(&dev->poll_list, &queue->poll_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (dev->quota < 0)
1931 dev->quota += dev->weight;
1932 else
1933 dev->quota = dev->weight;
1934 } else {
Matt Mackall53fb95d2005-08-11 19:27:43 -07001935 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 dev_put(dev);
1937 local_irq_disable();
1938 }
1939 }
1940out:
Chris Leechdb217332006-06-17 21:24:58 -07001941#ifdef CONFIG_NET_DMA
1942 /*
1943 * There may not be any more sk_buffs coming right now, so push
1944 * any pending DMA copies to hardware
1945 */
1946 if (net_dma_client) {
1947 struct dma_chan *chan;
1948 rcu_read_lock();
1949 list_for_each_entry_rcu(chan, &net_dma_client->channels, client_node)
1950 dma_async_memcpy_issue_pending(chan);
1951 rcu_read_unlock();
1952 }
1953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 local_irq_enable();
1955 return;
1956
1957softnet_break:
1958 __get_cpu_var(netdev_rx_stat).time_squeeze++;
1959 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1960 goto out;
1961}
1962
1963static gifconf_func_t * gifconf_list [NPROTO];
1964
1965/**
1966 * register_gifconf - register a SIOCGIF handler
1967 * @family: Address family
1968 * @gifconf: Function handler
1969 *
1970 * Register protocol dependent address dumping routines. The handler
1971 * that is passed must not be freed or reused until it has been replaced
1972 * by another handler.
1973 */
1974int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1975{
1976 if (family >= NPROTO)
1977 return -EINVAL;
1978 gifconf_list[family] = gifconf;
1979 return 0;
1980}
1981
1982
1983/*
1984 * Map an interface index to its name (SIOCGIFNAME)
1985 */
1986
1987/*
1988 * We need this ioctl for efficient implementation of the
1989 * if_indextoname() function required by the IPv6 API. Without
1990 * it, we would have to search all the interfaces to find a
1991 * match. --pb
1992 */
1993
1994static int dev_ifname(struct ifreq __user *arg)
1995{
1996 struct net_device *dev;
1997 struct ifreq ifr;
1998
1999 /*
2000 * Fetch the caller's info block.
2001 */
2002
2003 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2004 return -EFAULT;
2005
2006 read_lock(&dev_base_lock);
2007 dev = __dev_get_by_index(ifr.ifr_ifindex);
2008 if (!dev) {
2009 read_unlock(&dev_base_lock);
2010 return -ENODEV;
2011 }
2012
2013 strcpy(ifr.ifr_name, dev->name);
2014 read_unlock(&dev_base_lock);
2015
2016 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2017 return -EFAULT;
2018 return 0;
2019}
2020
2021/*
2022 * Perform a SIOCGIFCONF call. This structure will change
2023 * size eventually, and there is nothing I can do about it.
2024 * Thus we will need a 'compatibility mode'.
2025 */
2026
2027static int dev_ifconf(char __user *arg)
2028{
2029 struct ifconf ifc;
2030 struct net_device *dev;
2031 char __user *pos;
2032 int len;
2033 int total;
2034 int i;
2035
2036 /*
2037 * Fetch the caller's info block.
2038 */
2039
2040 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2041 return -EFAULT;
2042
2043 pos = ifc.ifc_buf;
2044 len = ifc.ifc_len;
2045
2046 /*
2047 * Loop over the interfaces, and write an info block for each.
2048 */
2049
2050 total = 0;
2051 for (dev = dev_base; dev; dev = dev->next) {
2052 for (i = 0; i < NPROTO; i++) {
2053 if (gifconf_list[i]) {
2054 int done;
2055 if (!pos)
2056 done = gifconf_list[i](dev, NULL, 0);
2057 else
2058 done = gifconf_list[i](dev, pos + total,
2059 len - total);
2060 if (done < 0)
2061 return -EFAULT;
2062 total += done;
2063 }
2064 }
2065 }
2066
2067 /*
2068 * All done. Write the updated control block back to the caller.
2069 */
2070 ifc.ifc_len = total;
2071
2072 /*
2073 * Both BSD and Solaris return 0 here, so we do too.
2074 */
2075 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2076}
2077
2078#ifdef CONFIG_PROC_FS
2079/*
2080 * This is invoked by the /proc filesystem handler to display a device
2081 * in detail.
2082 */
2083static __inline__ struct net_device *dev_get_idx(loff_t pos)
2084{
2085 struct net_device *dev;
2086 loff_t i;
2087
2088 for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
2089
2090 return i == pos ? dev : NULL;
2091}
2092
2093void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2094{
2095 read_lock(&dev_base_lock);
2096 return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
2097}
2098
2099void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2100{
2101 ++*pos;
2102 return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
2103}
2104
2105void dev_seq_stop(struct seq_file *seq, void *v)
2106{
2107 read_unlock(&dev_base_lock);
2108}
2109
2110static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2111{
2112 if (dev->get_stats) {
2113 struct net_device_stats *stats = dev->get_stats(dev);
2114
2115 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2116 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2117 dev->name, stats->rx_bytes, stats->rx_packets,
2118 stats->rx_errors,
2119 stats->rx_dropped + stats->rx_missed_errors,
2120 stats->rx_fifo_errors,
2121 stats->rx_length_errors + stats->rx_over_errors +
2122 stats->rx_crc_errors + stats->rx_frame_errors,
2123 stats->rx_compressed, stats->multicast,
2124 stats->tx_bytes, stats->tx_packets,
2125 stats->tx_errors, stats->tx_dropped,
2126 stats->tx_fifo_errors, stats->collisions,
2127 stats->tx_carrier_errors +
2128 stats->tx_aborted_errors +
2129 stats->tx_window_errors +
2130 stats->tx_heartbeat_errors,
2131 stats->tx_compressed);
2132 } else
2133 seq_printf(seq, "%6s: No statistics available.\n", dev->name);
2134}
2135
2136/*
2137 * Called from the PROCfs module. This now uses the new arbitrary sized
2138 * /proc/net interface to create /proc/net/dev
2139 */
2140static int dev_seq_show(struct seq_file *seq, void *v)
2141{
2142 if (v == SEQ_START_TOKEN)
2143 seq_puts(seq, "Inter-| Receive "
2144 " | Transmit\n"
2145 " face |bytes packets errs drop fifo frame "
2146 "compressed multicast|bytes packets errs "
2147 "drop fifo colls carrier compressed\n");
2148 else
2149 dev_seq_printf_stats(seq, v);
2150 return 0;
2151}
2152
2153static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2154{
2155 struct netif_rx_stats *rc = NULL;
2156
2157 while (*pos < NR_CPUS)
2158 if (cpu_online(*pos)) {
2159 rc = &per_cpu(netdev_rx_stat, *pos);
2160 break;
2161 } else
2162 ++*pos;
2163 return rc;
2164}
2165
2166static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2167{
2168 return softnet_get_online(pos);
2169}
2170
2171static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2172{
2173 ++*pos;
2174 return softnet_get_online(pos);
2175}
2176
2177static void softnet_seq_stop(struct seq_file *seq, void *v)
2178{
2179}
2180
2181static int softnet_seq_show(struct seq_file *seq, void *v)
2182{
2183 struct netif_rx_stats *s = v;
2184
2185 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Stephen Hemminger31aa02c2005-06-23 20:12:48 -07002186 s->total, s->dropped, s->time_squeeze, 0,
Stephen Hemmingerc1ebcdb2005-06-23 20:08:59 -07002187 0, 0, 0, 0, /* was fastroute */
2188 s->cpu_collision );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return 0;
2190}
2191
2192static struct seq_operations dev_seq_ops = {
2193 .start = dev_seq_start,
2194 .next = dev_seq_next,
2195 .stop = dev_seq_stop,
2196 .show = dev_seq_show,
2197};
2198
2199static int dev_seq_open(struct inode *inode, struct file *file)
2200{
2201 return seq_open(file, &dev_seq_ops);
2202}
2203
2204static struct file_operations dev_seq_fops = {
2205 .owner = THIS_MODULE,
2206 .open = dev_seq_open,
2207 .read = seq_read,
2208 .llseek = seq_lseek,
2209 .release = seq_release,
2210};
2211
2212static struct seq_operations softnet_seq_ops = {
2213 .start = softnet_seq_start,
2214 .next = softnet_seq_next,
2215 .stop = softnet_seq_stop,
2216 .show = softnet_seq_show,
2217};
2218
2219static int softnet_seq_open(struct inode *inode, struct file *file)
2220{
2221 return seq_open(file, &softnet_seq_ops);
2222}
2223
2224static struct file_operations softnet_seq_fops = {
2225 .owner = THIS_MODULE,
2226 .open = softnet_seq_open,
2227 .read = seq_read,
2228 .llseek = seq_lseek,
2229 .release = seq_release,
2230};
2231
Adrian Bunkd86b5e02006-01-21 00:46:55 +01002232#ifdef CONFIG_WIRELESS_EXT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233extern int wireless_proc_init(void);
2234#else
2235#define wireless_proc_init() 0
2236#endif
2237
2238static int __init dev_proc_init(void)
2239{
2240 int rc = -ENOMEM;
2241
2242 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2243 goto out;
2244 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2245 goto out_dev;
2246 if (wireless_proc_init())
2247 goto out_softnet;
2248 rc = 0;
2249out:
2250 return rc;
2251out_softnet:
2252 proc_net_remove("softnet_stat");
2253out_dev:
2254 proc_net_remove("dev");
2255 goto out;
2256}
2257#else
2258#define dev_proc_init() 0
2259#endif /* CONFIG_PROC_FS */
2260
2261
2262/**
2263 * netdev_set_master - set up master/slave pair
2264 * @slave: slave device
2265 * @master: new master device
2266 *
2267 * Changes the master device of the slave. Pass %NULL to break the
2268 * bonding. The caller must hold the RTNL semaphore. On a failure
2269 * a negative errno code is returned. On success the reference counts
2270 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2271 * function returns zero.
2272 */
2273int netdev_set_master(struct net_device *slave, struct net_device *master)
2274{
2275 struct net_device *old = slave->master;
2276
2277 ASSERT_RTNL();
2278
2279 if (master) {
2280 if (old)
2281 return -EBUSY;
2282 dev_hold(master);
2283 }
2284
2285 slave->master = master;
2286
2287 synchronize_net();
2288
2289 if (old)
2290 dev_put(old);
2291
2292 if (master)
2293 slave->flags |= IFF_SLAVE;
2294 else
2295 slave->flags &= ~IFF_SLAVE;
2296
2297 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2298 return 0;
2299}
2300
2301/**
2302 * dev_set_promiscuity - update promiscuity count on a device
2303 * @dev: device
2304 * @inc: modifier
2305 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07002306 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * remains above zero the interface remains promiscuous. Once it hits zero
2308 * the device reverts back to normal filtering operation. A negative inc
2309 * value is used to drop promiscuity on the device.
2310 */
2311void dev_set_promiscuity(struct net_device *dev, int inc)
2312{
2313 unsigned short old_flags = dev->flags;
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if ((dev->promiscuity += inc) == 0)
2316 dev->flags &= ~IFF_PROMISC;
David Chau52609c02005-07-05 15:11:06 -07002317 else
2318 dev->flags |= IFF_PROMISC;
2319 if (dev->flags != old_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 dev_mc_upload(dev);
2321 printk(KERN_INFO "device %s %s promiscuous mode\n",
2322 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2323 "left");
Steve Grubb5bdb9882005-12-03 08:39:35 -05002324 audit_log(current->audit_context, GFP_ATOMIC,
2325 AUDIT_ANOM_PROMISCUOUS,
2326 "dev=%s prom=%d old_prom=%d auid=%u",
2327 dev->name, (dev->flags & IFF_PROMISC),
2328 (old_flags & IFF_PROMISC),
2329 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331}
2332
2333/**
2334 * dev_set_allmulti - update allmulti count on a device
2335 * @dev: device
2336 * @inc: modifier
2337 *
2338 * Add or remove reception of all multicast frames to a device. While the
2339 * count in the device remains above zero the interface remains listening
2340 * to all interfaces. Once it hits zero the device reverts back to normal
2341 * filtering operation. A negative @inc value is used to drop the counter
2342 * when releasing a resource needing all multicasts.
2343 */
2344
2345void dev_set_allmulti(struct net_device *dev, int inc)
2346{
2347 unsigned short old_flags = dev->flags;
2348
2349 dev->flags |= IFF_ALLMULTI;
2350 if ((dev->allmulti += inc) == 0)
2351 dev->flags &= ~IFF_ALLMULTI;
2352 if (dev->flags ^ old_flags)
2353 dev_mc_upload(dev);
2354}
2355
2356unsigned dev_get_flags(const struct net_device *dev)
2357{
2358 unsigned flags;
2359
2360 flags = (dev->flags & ~(IFF_PROMISC |
2361 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08002362 IFF_RUNNING |
2363 IFF_LOWER_UP |
2364 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 (dev->gflags & (IFF_PROMISC |
2366 IFF_ALLMULTI));
2367
Stefan Rompfb00055a2006-03-20 17:09:11 -08002368 if (netif_running(dev)) {
2369 if (netif_oper_up(dev))
2370 flags |= IFF_RUNNING;
2371 if (netif_carrier_ok(dev))
2372 flags |= IFF_LOWER_UP;
2373 if (netif_dormant(dev))
2374 flags |= IFF_DORMANT;
2375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 return flags;
2378}
2379
2380int dev_change_flags(struct net_device *dev, unsigned flags)
2381{
2382 int ret;
2383 int old_flags = dev->flags;
2384
2385 /*
2386 * Set the flags on our device.
2387 */
2388
2389 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2390 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2391 IFF_AUTOMEDIA)) |
2392 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2393 IFF_ALLMULTI));
2394
2395 /*
2396 * Load in the correct multicast list now the flags have changed.
2397 */
2398
2399 dev_mc_upload(dev);
2400
2401 /*
2402 * Have we downed the interface. We handle IFF_UP ourselves
2403 * according to user attempts to set it, rather than blindly
2404 * setting it.
2405 */
2406
2407 ret = 0;
2408 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2409 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2410
2411 if (!ret)
2412 dev_mc_upload(dev);
2413 }
2414
2415 if (dev->flags & IFF_UP &&
2416 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2417 IFF_VOLATILE)))
Alan Sternf07d5b92006-05-09 15:23:03 -07002418 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002419 NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2422 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2423 dev->gflags ^= IFF_PROMISC;
2424 dev_set_promiscuity(dev, inc);
2425 }
2426
2427 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2428 is important. Some (broken) drivers set IFF_PROMISC, when
2429 IFF_ALLMULTI is requested not asking us and not reporting.
2430 */
2431 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2432 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2433 dev->gflags ^= IFF_ALLMULTI;
2434 dev_set_allmulti(dev, inc);
2435 }
2436
2437 if (old_flags ^ dev->flags)
2438 rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2439
2440 return ret;
2441}
2442
2443int dev_set_mtu(struct net_device *dev, int new_mtu)
2444{
2445 int err;
2446
2447 if (new_mtu == dev->mtu)
2448 return 0;
2449
2450 /* MTU must be positive. */
2451 if (new_mtu < 0)
2452 return -EINVAL;
2453
2454 if (!netif_device_present(dev))
2455 return -ENODEV;
2456
2457 err = 0;
2458 if (dev->change_mtu)
2459 err = dev->change_mtu(dev, new_mtu);
2460 else
2461 dev->mtu = new_mtu;
2462 if (!err && dev->flags & IFF_UP)
Alan Sternf07d5b92006-05-09 15:23:03 -07002463 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002464 NETDEV_CHANGEMTU, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return err;
2466}
2467
2468int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2469{
2470 int err;
2471
2472 if (!dev->set_mac_address)
2473 return -EOPNOTSUPP;
2474 if (sa->sa_family != dev->type)
2475 return -EINVAL;
2476 if (!netif_device_present(dev))
2477 return -ENODEV;
2478 err = dev->set_mac_address(dev, sa);
2479 if (!err)
Alan Sternf07d5b92006-05-09 15:23:03 -07002480 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002481 NETDEV_CHANGEADDR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return err;
2483}
2484
2485/*
2486 * Perform the SIOCxIFxxx calls.
2487 */
2488static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2489{
2490 int err;
2491 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2492
2493 if (!dev)
2494 return -ENODEV;
2495
2496 switch (cmd) {
2497 case SIOCGIFFLAGS: /* Get interface flags */
2498 ifr->ifr_flags = dev_get_flags(dev);
2499 return 0;
2500
2501 case SIOCSIFFLAGS: /* Set interface flags */
2502 return dev_change_flags(dev, ifr->ifr_flags);
2503
2504 case SIOCGIFMETRIC: /* Get the metric on the interface
2505 (currently unused) */
2506 ifr->ifr_metric = 0;
2507 return 0;
2508
2509 case SIOCSIFMETRIC: /* Set the metric on the interface
2510 (currently unused) */
2511 return -EOPNOTSUPP;
2512
2513 case SIOCGIFMTU: /* Get the MTU of a device */
2514 ifr->ifr_mtu = dev->mtu;
2515 return 0;
2516
2517 case SIOCSIFMTU: /* Set the MTU of a device */
2518 return dev_set_mtu(dev, ifr->ifr_mtu);
2519
2520 case SIOCGIFHWADDR:
2521 if (!dev->addr_len)
2522 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2523 else
2524 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2525 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2526 ifr->ifr_hwaddr.sa_family = dev->type;
2527 return 0;
2528
2529 case SIOCSIFHWADDR:
2530 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2531
2532 case SIOCSIFHWBROADCAST:
2533 if (ifr->ifr_hwaddr.sa_family != dev->type)
2534 return -EINVAL;
2535 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2536 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
Alan Sternf07d5b92006-05-09 15:23:03 -07002537 raw_notifier_call_chain(&netdev_chain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 NETDEV_CHANGEADDR, dev);
2539 return 0;
2540
2541 case SIOCGIFMAP:
2542 ifr->ifr_map.mem_start = dev->mem_start;
2543 ifr->ifr_map.mem_end = dev->mem_end;
2544 ifr->ifr_map.base_addr = dev->base_addr;
2545 ifr->ifr_map.irq = dev->irq;
2546 ifr->ifr_map.dma = dev->dma;
2547 ifr->ifr_map.port = dev->if_port;
2548 return 0;
2549
2550 case SIOCSIFMAP:
2551 if (dev->set_config) {
2552 if (!netif_device_present(dev))
2553 return -ENODEV;
2554 return dev->set_config(dev, &ifr->ifr_map);
2555 }
2556 return -EOPNOTSUPP;
2557
2558 case SIOCADDMULTI:
2559 if (!dev->set_multicast_list ||
2560 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2561 return -EINVAL;
2562 if (!netif_device_present(dev))
2563 return -ENODEV;
2564 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2565 dev->addr_len, 1);
2566
2567 case SIOCDELMULTI:
2568 if (!dev->set_multicast_list ||
2569 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2570 return -EINVAL;
2571 if (!netif_device_present(dev))
2572 return -ENODEV;
2573 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2574 dev->addr_len, 1);
2575
2576 case SIOCGIFINDEX:
2577 ifr->ifr_ifindex = dev->ifindex;
2578 return 0;
2579
2580 case SIOCGIFTXQLEN:
2581 ifr->ifr_qlen = dev->tx_queue_len;
2582 return 0;
2583
2584 case SIOCSIFTXQLEN:
2585 if (ifr->ifr_qlen < 0)
2586 return -EINVAL;
2587 dev->tx_queue_len = ifr->ifr_qlen;
2588 return 0;
2589
2590 case SIOCSIFNAME:
2591 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2592 return dev_change_name(dev, ifr->ifr_newname);
2593
2594 /*
2595 * Unknown or private ioctl
2596 */
2597
2598 default:
2599 if ((cmd >= SIOCDEVPRIVATE &&
2600 cmd <= SIOCDEVPRIVATE + 15) ||
2601 cmd == SIOCBONDENSLAVE ||
2602 cmd == SIOCBONDRELEASE ||
2603 cmd == SIOCBONDSETHWADDR ||
2604 cmd == SIOCBONDSLAVEINFOQUERY ||
2605 cmd == SIOCBONDINFOQUERY ||
2606 cmd == SIOCBONDCHANGEACTIVE ||
2607 cmd == SIOCGMIIPHY ||
2608 cmd == SIOCGMIIREG ||
2609 cmd == SIOCSMIIREG ||
2610 cmd == SIOCBRADDIF ||
2611 cmd == SIOCBRDELIF ||
2612 cmd == SIOCWANDEV) {
2613 err = -EOPNOTSUPP;
2614 if (dev->do_ioctl) {
2615 if (netif_device_present(dev))
2616 err = dev->do_ioctl(dev, ifr,
2617 cmd);
2618 else
2619 err = -ENODEV;
2620 }
2621 } else
2622 err = -EINVAL;
2623
2624 }
2625 return err;
2626}
2627
2628/*
2629 * This function handles all "interface"-type I/O control requests. The actual
2630 * 'doing' part of this is dev_ifsioc above.
2631 */
2632
2633/**
2634 * dev_ioctl - network device ioctl
2635 * @cmd: command to issue
2636 * @arg: pointer to a struct ifreq in user space
2637 *
2638 * Issue ioctl functions to devices. This is normally called by the
2639 * user space syscall interfaces but can sometimes be useful for
2640 * other purposes. The return value is the return from the syscall if
2641 * positive or a negative errno code on error.
2642 */
2643
2644int dev_ioctl(unsigned int cmd, void __user *arg)
2645{
2646 struct ifreq ifr;
2647 int ret;
2648 char *colon;
2649
2650 /* One special case: SIOCGIFCONF takes ifconf argument
2651 and requires shared lock, because it sleeps writing
2652 to user space.
2653 */
2654
2655 if (cmd == SIOCGIFCONF) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002656 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 ret = dev_ifconf((char __user *) arg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002658 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return ret;
2660 }
2661 if (cmd == SIOCGIFNAME)
2662 return dev_ifname((struct ifreq __user *)arg);
2663
2664 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2665 return -EFAULT;
2666
2667 ifr.ifr_name[IFNAMSIZ-1] = 0;
2668
2669 colon = strchr(ifr.ifr_name, ':');
2670 if (colon)
2671 *colon = 0;
2672
2673 /*
2674 * See which interface the caller is talking about.
2675 */
2676
2677 switch (cmd) {
2678 /*
2679 * These ioctl calls:
2680 * - can be done by all.
2681 * - atomic and do not require locking.
2682 * - return a value
2683 */
2684 case SIOCGIFFLAGS:
2685 case SIOCGIFMETRIC:
2686 case SIOCGIFMTU:
2687 case SIOCGIFHWADDR:
2688 case SIOCGIFSLAVE:
2689 case SIOCGIFMAP:
2690 case SIOCGIFINDEX:
2691 case SIOCGIFTXQLEN:
2692 dev_load(ifr.ifr_name);
2693 read_lock(&dev_base_lock);
2694 ret = dev_ifsioc(&ifr, cmd);
2695 read_unlock(&dev_base_lock);
2696 if (!ret) {
2697 if (colon)
2698 *colon = ':';
2699 if (copy_to_user(arg, &ifr,
2700 sizeof(struct ifreq)))
2701 ret = -EFAULT;
2702 }
2703 return ret;
2704
2705 case SIOCETHTOOL:
2706 dev_load(ifr.ifr_name);
2707 rtnl_lock();
2708 ret = dev_ethtool(&ifr);
2709 rtnl_unlock();
2710 if (!ret) {
2711 if (colon)
2712 *colon = ':';
2713 if (copy_to_user(arg, &ifr,
2714 sizeof(struct ifreq)))
2715 ret = -EFAULT;
2716 }
2717 return ret;
2718
2719 /*
2720 * These ioctl calls:
2721 * - require superuser power.
2722 * - require strict serialization.
2723 * - return a value
2724 */
2725 case SIOCGMIIPHY:
2726 case SIOCGMIIREG:
2727 case SIOCSIFNAME:
2728 if (!capable(CAP_NET_ADMIN))
2729 return -EPERM;
2730 dev_load(ifr.ifr_name);
2731 rtnl_lock();
2732 ret = dev_ifsioc(&ifr, cmd);
2733 rtnl_unlock();
2734 if (!ret) {
2735 if (colon)
2736 *colon = ':';
2737 if (copy_to_user(arg, &ifr,
2738 sizeof(struct ifreq)))
2739 ret = -EFAULT;
2740 }
2741 return ret;
2742
2743 /*
2744 * These ioctl calls:
2745 * - require superuser power.
2746 * - require strict serialization.
2747 * - do not return a value
2748 */
2749 case SIOCSIFFLAGS:
2750 case SIOCSIFMETRIC:
2751 case SIOCSIFMTU:
2752 case SIOCSIFMAP:
2753 case SIOCSIFHWADDR:
2754 case SIOCSIFSLAVE:
2755 case SIOCADDMULTI:
2756 case SIOCDELMULTI:
2757 case SIOCSIFHWBROADCAST:
2758 case SIOCSIFTXQLEN:
2759 case SIOCSMIIREG:
2760 case SIOCBONDENSLAVE:
2761 case SIOCBONDRELEASE:
2762 case SIOCBONDSETHWADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 case SIOCBONDCHANGEACTIVE:
2764 case SIOCBRADDIF:
2765 case SIOCBRDELIF:
2766 if (!capable(CAP_NET_ADMIN))
2767 return -EPERM;
Thomas Grafcabcac02006-01-24 12:46:33 -08002768 /* fall through */
2769 case SIOCBONDSLAVEINFOQUERY:
2770 case SIOCBONDINFOQUERY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 dev_load(ifr.ifr_name);
2772 rtnl_lock();
2773 ret = dev_ifsioc(&ifr, cmd);
2774 rtnl_unlock();
2775 return ret;
2776
2777 case SIOCGIFMEM:
2778 /* Get the per device memory space. We can add this but
2779 * currently do not support it */
2780 case SIOCSIFMEM:
2781 /* Set the per device memory buffer space.
2782 * Not applicable in our case */
2783 case SIOCSIFLINK:
2784 return -EINVAL;
2785
2786 /*
2787 * Unknown or private ioctl.
2788 */
2789 default:
2790 if (cmd == SIOCWANDEV ||
2791 (cmd >= SIOCDEVPRIVATE &&
2792 cmd <= SIOCDEVPRIVATE + 15)) {
2793 dev_load(ifr.ifr_name);
2794 rtnl_lock();
2795 ret = dev_ifsioc(&ifr, cmd);
2796 rtnl_unlock();
2797 if (!ret && copy_to_user(arg, &ifr,
2798 sizeof(struct ifreq)))
2799 ret = -EFAULT;
2800 return ret;
2801 }
Adrian Bunkd86b5e02006-01-21 00:46:55 +01002802#ifdef CONFIG_WIRELESS_EXT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /* Take care of Wireless Extensions */
2804 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2805 /* If command is `set a parameter', or
2806 * `get the encoding parameters', check if
2807 * the user has the right to do it */
Jean Tourrilhesa4170162006-04-04 15:53:43 -07002808 if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE
2809 || cmd == SIOCGIWENCODEEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!capable(CAP_NET_ADMIN))
2811 return -EPERM;
2812 }
2813 dev_load(ifr.ifr_name);
2814 rtnl_lock();
2815 /* Follow me in net/core/wireless.c */
2816 ret = wireless_process_ioctl(&ifr, cmd);
2817 rtnl_unlock();
2818 if (IW_IS_GET(cmd) &&
2819 copy_to_user(arg, &ifr,
2820 sizeof(struct ifreq)))
2821 ret = -EFAULT;
2822 return ret;
2823 }
Adrian Bunkd86b5e02006-01-21 00:46:55 +01002824#endif /* CONFIG_WIRELESS_EXT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return -EINVAL;
2826 }
2827}
2828
2829
2830/**
2831 * dev_new_index - allocate an ifindex
2832 *
2833 * Returns a suitable unique value for a new device interface
2834 * number. The caller must hold the rtnl semaphore or the
2835 * dev_base_lock to be sure it remains unique.
2836 */
2837static int dev_new_index(void)
2838{
2839 static int ifindex;
2840 for (;;) {
2841 if (++ifindex <= 0)
2842 ifindex = 1;
2843 if (!__dev_get_by_index(ifindex))
2844 return ifindex;
2845 }
2846}
2847
2848static int dev_boot_phase = 1;
2849
2850/* Delayed registration/unregisteration */
2851static DEFINE_SPINLOCK(net_todo_list_lock);
2852static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
2853
2854static inline void net_set_todo(struct net_device *dev)
2855{
2856 spin_lock(&net_todo_list_lock);
2857 list_add_tail(&dev->todo_list, &net_todo_list);
2858 spin_unlock(&net_todo_list_lock);
2859}
2860
2861/**
2862 * register_netdevice - register a network device
2863 * @dev: device to register
2864 *
2865 * Take a completed network device structure and add it to the kernel
2866 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2867 * chain. 0 is returned on success. A negative errno code is returned
2868 * on a failure to set up the device, or if the name is a duplicate.
2869 *
2870 * Callers must hold the rtnl semaphore. You may want
2871 * register_netdev() instead of this.
2872 *
2873 * BUGS:
2874 * The locking appears insufficient to guarantee two parallel registers
2875 * will not get the same name.
2876 */
2877
2878int register_netdevice(struct net_device *dev)
2879{
2880 struct hlist_head *head;
2881 struct hlist_node *p;
2882 int ret;
2883
2884 BUG_ON(dev_boot_phase);
2885 ASSERT_RTNL();
2886
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07002887 might_sleep();
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 /* When net_device's are persistent, this will be fatal. */
2890 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
2891
2892 spin_lock_init(&dev->queue_lock);
Herbert Xu932ff272006-06-09 12:20:56 -07002893 spin_lock_init(&dev->_xmit_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 dev->xmit_lock_owner = -1;
2895#ifdef CONFIG_NET_CLS_ACT
2896 spin_lock_init(&dev->ingress_lock);
2897#endif
2898
2899 ret = alloc_divert_blk(dev);
2900 if (ret)
2901 goto out;
2902
2903 dev->iflink = -1;
2904
2905 /* Init, if this function is available */
2906 if (dev->init) {
2907 ret = dev->init(dev);
2908 if (ret) {
2909 if (ret > 0)
2910 ret = -EIO;
2911 goto out_err;
2912 }
2913 }
2914
2915 if (!dev_valid_name(dev->name)) {
2916 ret = -EINVAL;
2917 goto out_err;
2918 }
2919
2920 dev->ifindex = dev_new_index();
2921 if (dev->iflink == -1)
2922 dev->iflink = dev->ifindex;
2923
2924 /* Check for existence of name */
2925 head = dev_name_hash(dev->name);
2926 hlist_for_each(p, head) {
2927 struct net_device *d
2928 = hlist_entry(p, struct net_device, name_hlist);
2929 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
2930 ret = -EEXIST;
2931 goto out_err;
2932 }
2933 }
2934
2935 /* Fix illegal SG+CSUM combinations. */
2936 if ((dev->features & NETIF_F_SG) &&
Herbert Xu8648b302006-06-17 22:06:05 -07002937 !(dev->features & NETIF_F_ALL_CSUM)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07002938 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 dev->name);
2940 dev->features &= ~NETIF_F_SG;
2941 }
2942
2943 /* TSO requires that SG is present as well. */
2944 if ((dev->features & NETIF_F_TSO) &&
2945 !(dev->features & NETIF_F_SG)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07002946 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 dev->name);
2948 dev->features &= ~NETIF_F_TSO;
2949 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002950 if (dev->features & NETIF_F_UFO) {
2951 if (!(dev->features & NETIF_F_HW_CSUM)) {
2952 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2953 "NETIF_F_HW_CSUM feature.\n",
2954 dev->name);
2955 dev->features &= ~NETIF_F_UFO;
2956 }
2957 if (!(dev->features & NETIF_F_SG)) {
2958 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2959 "NETIF_F_SG feature.\n",
2960 dev->name);
2961 dev->features &= ~NETIF_F_UFO;
2962 }
2963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965 /*
2966 * nil rebuild_header routine,
2967 * that should be never called and used as just bug trap.
2968 */
2969
2970 if (!dev->rebuild_header)
2971 dev->rebuild_header = default_rebuild_header;
2972
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07002973 ret = netdev_register_sysfs(dev);
2974 if (ret)
2975 goto out_err;
2976 dev->reg_state = NETREG_REGISTERED;
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 /*
2979 * Default initial state at registry is that the
2980 * device is present.
2981 */
2982
2983 set_bit(__LINK_STATE_PRESENT, &dev->state);
2984
2985 dev->next = NULL;
2986 dev_init_scheduler(dev);
2987 write_lock_bh(&dev_base_lock);
2988 *dev_tail = dev;
2989 dev_tail = &dev->next;
2990 hlist_add_head(&dev->name_hlist, head);
2991 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
2992 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 write_unlock_bh(&dev_base_lock);
2994
2995 /* Notify protocols, that a new device appeared. */
Alan Sternf07d5b92006-05-09 15:23:03 -07002996 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 ret = 0;
2999
3000out:
3001 return ret;
3002out_err:
3003 free_divert_blk(dev);
3004 goto out;
3005}
3006
3007/**
3008 * register_netdev - register a network device
3009 * @dev: device to register
3010 *
3011 * Take a completed network device structure and add it to the kernel
3012 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3013 * chain. 0 is returned on success. A negative errno code is returned
3014 * on a failure to set up the device, or if the name is a duplicate.
3015 *
3016 * This is a wrapper around register_netdev that takes the rtnl semaphore
3017 * and expands the device name if you passed a format string to
3018 * alloc_netdev.
3019 */
3020int register_netdev(struct net_device *dev)
3021{
3022 int err;
3023
3024 rtnl_lock();
3025
3026 /*
3027 * If the name is a format string the caller wants us to do a
3028 * name allocation.
3029 */
3030 if (strchr(dev->name, '%')) {
3031 err = dev_alloc_name(dev, dev->name);
3032 if (err < 0)
3033 goto out;
3034 }
3035
3036 /*
3037 * Back compatibility hook. Kill this one in 2.5
3038 */
3039 if (dev->name[0] == 0 || dev->name[0] == ' ') {
3040 err = dev_alloc_name(dev, "eth%d");
3041 if (err < 0)
3042 goto out;
3043 }
3044
3045 err = register_netdevice(dev);
3046out:
3047 rtnl_unlock();
3048 return err;
3049}
3050EXPORT_SYMBOL(register_netdev);
3051
3052/*
3053 * netdev_wait_allrefs - wait until all references are gone.
3054 *
3055 * This is called when unregistering network devices.
3056 *
3057 * Any protocol or device that holds a reference should register
3058 * for netdevice notification, and cleanup and put back the
3059 * reference if they receive an UNREGISTER event.
3060 * We can get stuck here if buggy protocols don't correctly
3061 * call dev_put.
3062 */
3063static void netdev_wait_allrefs(struct net_device *dev)
3064{
3065 unsigned long rebroadcast_time, warning_time;
3066
3067 rebroadcast_time = warning_time = jiffies;
3068 while (atomic_read(&dev->refcnt) != 0) {
3069 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003070 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 /* Rebroadcast unregister notification */
Alan Sternf07d5b92006-05-09 15:23:03 -07003073 raw_notifier_call_chain(&netdev_chain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 NETDEV_UNREGISTER, dev);
3075
3076 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3077 &dev->state)) {
3078 /* We must not have linkwatch events
3079 * pending on unregister. If this
3080 * happens, we simply run the queue
3081 * unscheduled, resulting in a noop
3082 * for this device.
3083 */
3084 linkwatch_run_queue();
3085 }
3086
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003087 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 rebroadcast_time = jiffies;
3090 }
3091
3092 msleep(250);
3093
3094 if (time_after(jiffies, warning_time + 10 * HZ)) {
3095 printk(KERN_EMERG "unregister_netdevice: "
3096 "waiting for %s to become free. Usage "
3097 "count = %d\n",
3098 dev->name, atomic_read(&dev->refcnt));
3099 warning_time = jiffies;
3100 }
3101 }
3102}
3103
3104/* The sequence is:
3105 *
3106 * rtnl_lock();
3107 * ...
3108 * register_netdevice(x1);
3109 * register_netdevice(x2);
3110 * ...
3111 * unregister_netdevice(y1);
3112 * unregister_netdevice(y2);
3113 * ...
3114 * rtnl_unlock();
3115 * free_netdev(y1);
3116 * free_netdev(y2);
3117 *
3118 * We are invoked by rtnl_unlock() after it drops the semaphore.
3119 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003120 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 * without deadlocking with linkwatch via keventd.
3122 * 2) Since we run with the RTNL semaphore not held, we can sleep
3123 * safely in order to wait for the netdev refcnt to drop to zero.
3124 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003125static DEFINE_MUTEX(net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126void netdev_run_todo(void)
3127{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003128 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 /* Need to guard against multiple cpu's getting out of order. */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003131 mutex_lock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133 /* Not safe to do outside the semaphore. We must not return
3134 * until all unregister events invoked by the local processor
3135 * have been completed (either by this todo run, or one on
3136 * another cpu).
3137 */
3138 if (list_empty(&net_todo_list))
3139 goto out;
3140
3141 /* Snapshot list, allow later requests */
3142 spin_lock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003143 list_replace_init(&net_todo_list, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 spin_unlock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 while (!list_empty(&list)) {
3147 struct net_device *dev
3148 = list_entry(list.next, struct net_device, todo_list);
3149 list_del(&dev->todo_list);
3150
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003151 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 printk(KERN_ERR "network todo '%s' but state %d\n",
3153 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003154 dump_stack();
3155 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003157
3158 netdev_unregister_sysfs(dev);
3159 dev->reg_state = NETREG_UNREGISTERED;
3160
3161 netdev_wait_allrefs(dev);
3162
3163 /* paranoia */
3164 BUG_ON(atomic_read(&dev->refcnt));
3165 BUG_TRAP(!dev->ip_ptr);
3166 BUG_TRAP(!dev->ip6_ptr);
3167 BUG_TRAP(!dev->dn_ptr);
3168
3169 /* It must be the very last action,
3170 * after this 'dev' may point to freed up memory.
3171 */
3172 if (dev->destructor)
3173 dev->destructor(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175
3176out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003177 mutex_unlock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
3180/**
3181 * alloc_netdev - allocate network device
3182 * @sizeof_priv: size of private data to allocate space for
3183 * @name: device name format string
3184 * @setup: callback to initialize device
3185 *
3186 * Allocates a struct net_device with private data area for driver use
3187 * and performs basic initialization.
3188 */
3189struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3190 void (*setup)(struct net_device *))
3191{
3192 void *p;
3193 struct net_device *dev;
3194 int alloc_size;
3195
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07003196 BUG_ON(strlen(name) >= sizeof(dev->name));
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 /* ensure 32-byte alignment of both the device and private area */
3199 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3200 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3201
Paolo 'Blaisorblade' Giarrusso31380de2006-04-06 22:38:28 -07003202 p = kzalloc(alloc_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (!p) {
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07003204 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return NULL;
3206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208 dev = (struct net_device *)
3209 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3210 dev->padded = (char *)dev - (char *)p;
3211
3212 if (sizeof_priv)
3213 dev->priv = netdev_priv(dev);
3214
3215 setup(dev);
3216 strcpy(dev->name, name);
3217 return dev;
3218}
3219EXPORT_SYMBOL(alloc_netdev);
3220
3221/**
3222 * free_netdev - free network device
3223 * @dev: device
3224 *
3225 * This function does the last stage of destroying an allocated device
3226 * interface. The reference to the device object is released.
3227 * If this is the last reference then it will be freed.
3228 */
3229void free_netdev(struct net_device *dev)
3230{
3231#ifdef CONFIG_SYSFS
Stephen Hemminger3041a062006-05-26 13:25:24 -07003232 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (dev->reg_state == NETREG_UNINITIALIZED) {
3234 kfree((char *)dev - dev->padded);
3235 return;
3236 }
3237
3238 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3239 dev->reg_state = NETREG_RELEASED;
3240
3241 /* will free via class release */
3242 class_device_put(&dev->class_dev);
3243#else
3244 kfree((char *)dev - dev->padded);
3245#endif
3246}
3247
3248/* Synchronize with packet receive processing. */
3249void synchronize_net(void)
3250{
3251 might_sleep();
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003252 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254
3255/**
3256 * unregister_netdevice - remove device from the kernel
3257 * @dev: device
3258 *
3259 * This function shuts down a device interface and removes it
3260 * from the kernel tables. On success 0 is returned, on a failure
3261 * a negative errno code is returned.
3262 *
3263 * Callers must hold the rtnl semaphore. You may want
3264 * unregister_netdev() instead of this.
3265 */
3266
3267int unregister_netdevice(struct net_device *dev)
3268{
3269 struct net_device *d, **dp;
3270
3271 BUG_ON(dev_boot_phase);
3272 ASSERT_RTNL();
3273
3274 /* Some devices call without registering for initialization unwind. */
3275 if (dev->reg_state == NETREG_UNINITIALIZED) {
3276 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3277 "was registered\n", dev->name, dev);
3278 return -ENODEV;
3279 }
3280
3281 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3282
3283 /* If device is running, close it first. */
3284 if (dev->flags & IFF_UP)
3285 dev_close(dev);
3286
3287 /* And unlink it from device chain. */
3288 for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3289 if (d == dev) {
3290 write_lock_bh(&dev_base_lock);
3291 hlist_del(&dev->name_hlist);
3292 hlist_del(&dev->index_hlist);
3293 if (dev_tail == &dev->next)
3294 dev_tail = dp;
3295 *dp = d->next;
3296 write_unlock_bh(&dev_base_lock);
3297 break;
3298 }
3299 }
3300 if (!d) {
3301 printk(KERN_ERR "unregister net_device: '%s' not found\n",
3302 dev->name);
3303 return -ENODEV;
3304 }
3305
3306 dev->reg_state = NETREG_UNREGISTERING;
3307
3308 synchronize_net();
3309
3310 /* Shutdown queueing discipline. */
3311 dev_shutdown(dev);
3312
3313
3314 /* Notify protocols, that we are about to destroy
3315 this device. They should clean all the things.
3316 */
Alan Sternf07d5b92006-05-09 15:23:03 -07003317 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319 /*
3320 * Flush the multicast chain
3321 */
3322 dev_mc_discard(dev);
3323
3324 if (dev->uninit)
3325 dev->uninit(dev);
3326
3327 /* Notifier chain MUST detach us from master device. */
3328 BUG_TRAP(!dev->master);
3329
3330 free_divert_blk(dev);
3331
3332 /* Finish processing unregister after unlock */
3333 net_set_todo(dev);
3334
3335 synchronize_net();
3336
3337 dev_put(dev);
3338 return 0;
3339}
3340
3341/**
3342 * unregister_netdev - remove device from the kernel
3343 * @dev: device
3344 *
3345 * This function shuts down a device interface and removes it
3346 * from the kernel tables. On success 0 is returned, on a failure
3347 * a negative errno code is returned.
3348 *
3349 * This is just a wrapper for unregister_netdevice that takes
3350 * the rtnl semaphore. In general you want to use this and not
3351 * unregister_netdevice.
3352 */
3353void unregister_netdev(struct net_device *dev)
3354{
3355 rtnl_lock();
3356 unregister_netdevice(dev);
3357 rtnl_unlock();
3358}
3359
3360EXPORT_SYMBOL(unregister_netdev);
3361
3362#ifdef CONFIG_HOTPLUG_CPU
3363static int dev_cpu_callback(struct notifier_block *nfb,
3364 unsigned long action,
3365 void *ocpu)
3366{
3367 struct sk_buff **list_skb;
3368 struct net_device **list_net;
3369 struct sk_buff *skb;
3370 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3371 struct softnet_data *sd, *oldsd;
3372
3373 if (action != CPU_DEAD)
3374 return NOTIFY_OK;
3375
3376 local_irq_disable();
3377 cpu = smp_processor_id();
3378 sd = &per_cpu(softnet_data, cpu);
3379 oldsd = &per_cpu(softnet_data, oldcpu);
3380
3381 /* Find end of our completion_queue. */
3382 list_skb = &sd->completion_queue;
3383 while (*list_skb)
3384 list_skb = &(*list_skb)->next;
3385 /* Append completion queue from offline CPU. */
3386 *list_skb = oldsd->completion_queue;
3387 oldsd->completion_queue = NULL;
3388
3389 /* Find end of our output_queue. */
3390 list_net = &sd->output_queue;
3391 while (*list_net)
3392 list_net = &(*list_net)->next_sched;
3393 /* Append output queue from offline CPU. */
3394 *list_net = oldsd->output_queue;
3395 oldsd->output_queue = NULL;
3396
3397 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3398 local_irq_enable();
3399
3400 /* Process offline CPU's input_pkt_queue */
3401 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3402 netif_rx(skb);
3403
3404 return NOTIFY_OK;
3405}
3406#endif /* CONFIG_HOTPLUG_CPU */
3407
Chris Leechdb217332006-06-17 21:24:58 -07003408#ifdef CONFIG_NET_DMA
3409/**
3410 * net_dma_rebalance -
3411 * This is called when the number of channels allocated to the net_dma_client
3412 * changes. The net_dma_client tries to have one DMA channel per CPU.
3413 */
3414static void net_dma_rebalance(void)
3415{
3416 unsigned int cpu, i, n;
3417 struct dma_chan *chan;
3418
Chris Leechdb217332006-06-17 21:24:58 -07003419 if (net_dma_count == 0) {
3420 for_each_online_cpu(cpu)
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07003421 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
Chris Leechdb217332006-06-17 21:24:58 -07003422 return;
3423 }
3424
3425 i = 0;
3426 cpu = first_cpu(cpu_online_map);
3427
3428 rcu_read_lock();
3429 list_for_each_entry(chan, &net_dma_client->channels, client_node) {
3430 n = ((num_online_cpus() / net_dma_count)
3431 + (i < (num_online_cpus() % net_dma_count) ? 1 : 0));
3432
3433 while(n) {
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07003434 per_cpu(softnet_data, cpu).net_dma = chan;
Chris Leechdb217332006-06-17 21:24:58 -07003435 cpu = next_cpu(cpu, cpu_online_map);
3436 n--;
3437 }
3438 i++;
3439 }
3440 rcu_read_unlock();
Chris Leechdb217332006-06-17 21:24:58 -07003441}
3442
3443/**
3444 * netdev_dma_event - event callback for the net_dma_client
3445 * @client: should always be net_dma_client
Randy Dunlapf4b8ea72006-06-22 16:00:11 -07003446 * @chan: DMA channel for the event
3447 * @event: event type
Chris Leechdb217332006-06-17 21:24:58 -07003448 */
3449static void netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3450 enum dma_event event)
3451{
3452 spin_lock(&net_dma_event_lock);
3453 switch (event) {
3454 case DMA_RESOURCE_ADDED:
3455 net_dma_count++;
3456 net_dma_rebalance();
3457 break;
3458 case DMA_RESOURCE_REMOVED:
3459 net_dma_count--;
3460 net_dma_rebalance();
3461 break;
3462 default:
3463 break;
3464 }
3465 spin_unlock(&net_dma_event_lock);
3466}
3467
3468/**
3469 * netdev_dma_regiser - register the networking subsystem as a DMA client
3470 */
3471static int __init netdev_dma_register(void)
3472{
3473 spin_lock_init(&net_dma_event_lock);
3474 net_dma_client = dma_async_client_register(netdev_dma_event);
3475 if (net_dma_client == NULL)
3476 return -ENOMEM;
3477
3478 dma_async_client_chan_request(net_dma_client, num_online_cpus());
3479 return 0;
3480}
3481
3482#else
3483static int __init netdev_dma_register(void) { return -ENODEV; }
3484#endif /* CONFIG_NET_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
3486/*
3487 * Initialize the DEV module. At boot time this walks the device list and
3488 * unhooks any devices that fail to initialise (normally hardware not
3489 * present) and leaves us with a valid list of present and active devices.
3490 *
3491 */
3492
3493/*
3494 * This is called single threaded during boot, so no need
3495 * to take the rtnl semaphore.
3496 */
3497static int __init net_dev_init(void)
3498{
3499 int i, rc = -ENOMEM;
3500
3501 BUG_ON(!dev_boot_phase);
3502
3503 net_random_init();
3504
3505 if (dev_proc_init())
3506 goto out;
3507
3508 if (netdev_sysfs_init())
3509 goto out;
3510
3511 INIT_LIST_HEAD(&ptype_all);
3512 for (i = 0; i < 16; i++)
3513 INIT_LIST_HEAD(&ptype_base[i]);
3514
3515 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3516 INIT_HLIST_HEAD(&dev_name_head[i]);
3517
3518 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3519 INIT_HLIST_HEAD(&dev_index_head[i]);
3520
3521 /*
3522 * Initialise the packet receive queues.
3523 */
3524
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07003525 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 struct softnet_data *queue;
3527
3528 queue = &per_cpu(softnet_data, i);
3529 skb_queue_head_init(&queue->input_pkt_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 queue->completion_queue = NULL;
3531 INIT_LIST_HEAD(&queue->poll_list);
3532 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3533 queue->backlog_dev.weight = weight_p;
3534 queue->backlog_dev.poll = process_backlog;
3535 atomic_set(&queue->backlog_dev.refcnt, 1);
3536 }
3537
Chris Leechdb217332006-06-17 21:24:58 -07003538 netdev_dma_register();
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 dev_boot_phase = 0;
3541
3542 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3543 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3544
3545 hotcpu_notifier(dev_cpu_callback, 0);
3546 dst_init();
3547 dev_mcast_init();
3548 rc = 0;
3549out:
3550 return rc;
3551}
3552
3553subsys_initcall(net_dev_init);
3554
3555EXPORT_SYMBOL(__dev_get_by_index);
3556EXPORT_SYMBOL(__dev_get_by_name);
3557EXPORT_SYMBOL(__dev_remove_pack);
Mitch Williamsc2373ee2005-11-09 10:34:45 -08003558EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559EXPORT_SYMBOL(dev_add_pack);
3560EXPORT_SYMBOL(dev_alloc_name);
3561EXPORT_SYMBOL(dev_close);
3562EXPORT_SYMBOL(dev_get_by_flags);
3563EXPORT_SYMBOL(dev_get_by_index);
3564EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565EXPORT_SYMBOL(dev_open);
3566EXPORT_SYMBOL(dev_queue_xmit);
3567EXPORT_SYMBOL(dev_remove_pack);
3568EXPORT_SYMBOL(dev_set_allmulti);
3569EXPORT_SYMBOL(dev_set_promiscuity);
3570EXPORT_SYMBOL(dev_change_flags);
3571EXPORT_SYMBOL(dev_set_mtu);
3572EXPORT_SYMBOL(dev_set_mac_address);
3573EXPORT_SYMBOL(free_netdev);
3574EXPORT_SYMBOL(netdev_boot_setup_check);
3575EXPORT_SYMBOL(netdev_set_master);
3576EXPORT_SYMBOL(netdev_state_change);
3577EXPORT_SYMBOL(netif_receive_skb);
3578EXPORT_SYMBOL(netif_rx);
3579EXPORT_SYMBOL(register_gifconf);
3580EXPORT_SYMBOL(register_netdevice);
3581EXPORT_SYMBOL(register_netdevice_notifier);
3582EXPORT_SYMBOL(skb_checksum_help);
3583EXPORT_SYMBOL(synchronize_net);
3584EXPORT_SYMBOL(unregister_netdevice);
3585EXPORT_SYMBOL(unregister_netdevice_notifier);
3586EXPORT_SYMBOL(net_enable_timestamp);
3587EXPORT_SYMBOL(net_disable_timestamp);
3588EXPORT_SYMBOL(dev_get_flags);
3589
3590#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3591EXPORT_SYMBOL(br_handle_frame_hook);
3592EXPORT_SYMBOL(br_fdb_get_hook);
3593EXPORT_SYMBOL(br_fdb_put_hook);
3594#endif
3595
3596#ifdef CONFIG_KMOD
3597EXPORT_SYMBOL(dev_load);
3598#endif
3599
3600EXPORT_PER_CPU_SYMBOL(softnet_data);