blob: d4a1ec3bded5f6afa92c0cbb860101b509f0f338 [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;
643 if (!strcmp(name, ".") || !strcmp(name, ".."))
644 return 0;
645
646 while (*name) {
647 if (*name == '/' || isspace(*name))
648 return 0;
649 name++;
650 }
651 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/**
655 * dev_alloc_name - allocate a name for a device
656 * @dev: device
657 * @name: name format string
658 *
659 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -0700660 * id. It scans list of devices to build up a free map, then chooses
661 * the first empty slot. The caller must hold the dev_base or rtnl lock
662 * while allocating the name and adding the device in order to avoid
663 * duplicates.
664 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
665 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
667
668int dev_alloc_name(struct net_device *dev, const char *name)
669{
670 int i = 0;
671 char buf[IFNAMSIZ];
672 const char *p;
673 const int max_netdevices = 8*PAGE_SIZE;
674 long *inuse;
675 struct net_device *d;
676
677 p = strnchr(name, IFNAMSIZ-1, '%');
678 if (p) {
679 /*
680 * Verify the string as this thing may have come from
681 * the user. There must be either one "%d" and no other "%"
682 * characters.
683 */
684 if (p[1] != 'd' || strchr(p + 2, '%'))
685 return -EINVAL;
686
687 /* Use one page as a bit array of possible slots */
688 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
689 if (!inuse)
690 return -ENOMEM;
691
692 for (d = dev_base; d; d = d->next) {
693 if (!sscanf(d->name, name, &i))
694 continue;
695 if (i < 0 || i >= max_netdevices)
696 continue;
697
698 /* avoid cases where sscanf is not exact inverse of printf */
699 snprintf(buf, sizeof(buf), name, i);
700 if (!strncmp(buf, d->name, IFNAMSIZ))
701 set_bit(i, inuse);
702 }
703
704 i = find_first_zero_bit(inuse, max_netdevices);
705 free_page((unsigned long) inuse);
706 }
707
708 snprintf(buf, sizeof(buf), name, i);
709 if (!__dev_get_by_name(buf)) {
710 strlcpy(dev->name, buf, IFNAMSIZ);
711 return i;
712 }
713
714 /* It is possible to run out of possible slots
715 * when the name is long and there isn't enough space left
716 * for the digits, or if all bits are used.
717 */
718 return -ENFILE;
719}
720
721
722/**
723 * dev_change_name - change name of a device
724 * @dev: device
725 * @newname: name (or format string) must be at least IFNAMSIZ
726 *
727 * Change name of a device, can pass format strings "eth%d".
728 * for wildcarding.
729 */
730int dev_change_name(struct net_device *dev, char *newname)
731{
732 int err = 0;
733
734 ASSERT_RTNL();
735
736 if (dev->flags & IFF_UP)
737 return -EBUSY;
738
739 if (!dev_valid_name(newname))
740 return -EINVAL;
741
742 if (strchr(newname, '%')) {
743 err = dev_alloc_name(dev, newname);
744 if (err < 0)
745 return err;
746 strcpy(newname, dev->name);
747 }
748 else if (__dev_get_by_name(newname))
749 return -EEXIST;
750 else
751 strlcpy(dev->name, newname, IFNAMSIZ);
752
753 err = class_device_rename(&dev->class_dev, dev->name);
754 if (!err) {
755 hlist_del(&dev->name_hlist);
756 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
Alan Sternf07d5b92006-05-09 15:23:03 -0700757 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -0800758 NETDEV_CHANGENAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 return err;
762}
763
764/**
Stephen Hemminger3041a062006-05-26 13:25:24 -0700765 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700766 * @dev: device to cause notification
767 *
768 * Called to indicate a device has changed features.
769 */
770void netdev_features_change(struct net_device *dev)
771{
Alan Sternf07d5b92006-05-09 15:23:03 -0700772 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700773}
774EXPORT_SYMBOL(netdev_features_change);
775
776/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * netdev_state_change - device changes state
778 * @dev: device to cause notification
779 *
780 * Called to indicate a device has changed state. This function calls
781 * the notifier chains for netdev_chain and sends a NEWLINK message
782 * to the routing socket.
783 */
784void netdev_state_change(struct net_device *dev)
785{
786 if (dev->flags & IFF_UP) {
Alan Sternf07d5b92006-05-09 15:23:03 -0700787 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -0800788 NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
790 }
791}
792
793/**
794 * dev_load - load a network module
795 * @name: name of interface
796 *
797 * If a network interface is not present and the process has suitable
798 * privileges this function loads the module. If module loading is not
799 * available in this kernel then it becomes a nop.
800 */
801
802void dev_load(const char *name)
803{
804 struct net_device *dev;
805
806 read_lock(&dev_base_lock);
807 dev = __dev_get_by_name(name);
808 read_unlock(&dev_base_lock);
809
810 if (!dev && capable(CAP_SYS_MODULE))
811 request_module("%s", name);
812}
813
814static int default_rebuild_header(struct sk_buff *skb)
815{
816 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
817 skb->dev ? skb->dev->name : "NULL!!!");
818 kfree_skb(skb);
819 return 1;
820}
821
822
823/**
824 * dev_open - prepare an interface for use.
825 * @dev: device to open
826 *
827 * Takes a device from down to up state. The device's private open
828 * function is invoked and then the multicast lists are loaded. Finally
829 * the device is moved into the up state and a %NETDEV_UP message is
830 * sent to the netdev notifier chain.
831 *
832 * Calling this function on an active interface is a nop. On a failure
833 * a negative errno code is returned.
834 */
835int dev_open(struct net_device *dev)
836{
837 int ret = 0;
838
839 /*
840 * Is it already up?
841 */
842
843 if (dev->flags & IFF_UP)
844 return 0;
845
846 /*
847 * Is it even present?
848 */
849 if (!netif_device_present(dev))
850 return -ENODEV;
851
852 /*
853 * Call device private open method
854 */
855 set_bit(__LINK_STATE_START, &dev->state);
856 if (dev->open) {
857 ret = dev->open(dev);
858 if (ret)
859 clear_bit(__LINK_STATE_START, &dev->state);
860 }
861
862 /*
863 * If it went open OK then:
864 */
865
866 if (!ret) {
867 /*
868 * Set the flags.
869 */
870 dev->flags |= IFF_UP;
871
872 /*
873 * Initialize multicasting status
874 */
875 dev_mc_upload(dev);
876
877 /*
878 * Wakeup transmit queue engine
879 */
880 dev_activate(dev);
881
882 /*
883 * ... and announce new interface.
884 */
Alan Sternf07d5b92006-05-09 15:23:03 -0700885 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 return ret;
888}
889
890/**
891 * dev_close - shutdown an interface.
892 * @dev: device to shutdown
893 *
894 * This function moves an active device into down state. A
895 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
896 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
897 * chain.
898 */
899int dev_close(struct net_device *dev)
900{
901 if (!(dev->flags & IFF_UP))
902 return 0;
903
904 /*
905 * Tell people we are going down, so that they can
906 * prepare to death, when device is still operating.
907 */
Alan Sternf07d5b92006-05-09 15:23:03 -0700908 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 dev_deactivate(dev);
911
912 clear_bit(__LINK_STATE_START, &dev->state);
913
914 /* Synchronize to scheduled poll. We cannot touch poll list,
915 * it can be even on different cpu. So just clear netif_running(),
916 * and wait when poll really will happen. Actually, the best place
917 * for this is inside dev->stop() after device stopped its irq
918 * engine, but this requires more changes in devices. */
919
920 smp_mb__after_clear_bit(); /* Commit netif_running(). */
921 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
922 /* No hurry. */
David S. Miller6192b542005-07-28 12:12:58 -0700923 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925
926 /*
927 * Call the device specific close. This cannot fail.
928 * Only if device is UP
929 *
930 * We allow it to be called even after a DETACH hot-plug
931 * event.
932 */
933 if (dev->stop)
934 dev->stop(dev);
935
936 /*
937 * Device is now down.
938 */
939
940 dev->flags &= ~IFF_UP;
941
942 /*
943 * Tell people we are down
944 */
Alan Sternf07d5b92006-05-09 15:23:03 -0700945 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 return 0;
948}
949
950
951/*
952 * Device change register/unregister. These are not inline or static
953 * as we export them to the world.
954 */
955
956/**
957 * register_netdevice_notifier - register a network notifier block
958 * @nb: notifier
959 *
960 * Register a notifier to be called when network device events occur.
961 * The notifier passed is linked into the kernel structures and must
962 * not be reused until it has been unregistered. A negative errno code
963 * is returned on a failure.
964 *
965 * When registered all registration and up events are replayed
966 * to the new notifier to allow device to have a race free
967 * view of the network device list.
968 */
969
970int register_netdevice_notifier(struct notifier_block *nb)
971{
972 struct net_device *dev;
973 int err;
974
975 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -0700976 err = raw_notifier_chain_register(&netdev_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!err) {
978 for (dev = dev_base; dev; dev = dev->next) {
979 nb->notifier_call(nb, NETDEV_REGISTER, dev);
980
981 if (dev->flags & IFF_UP)
982 nb->notifier_call(nb, NETDEV_UP, dev);
983 }
984 }
985 rtnl_unlock();
986 return err;
987}
988
989/**
990 * unregister_netdevice_notifier - unregister a network notifier block
991 * @nb: notifier
992 *
993 * Unregister a notifier previously registered by
994 * register_netdevice_notifier(). The notifier is unlinked into the
995 * kernel structures and may then be reused. A negative errno code
996 * is returned on a failure.
997 */
998
999int unregister_netdevice_notifier(struct notifier_block *nb)
1000{
Herbert Xu9f514952006-03-25 01:24:25 -08001001 int err;
1002
1003 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001004 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xu9f514952006-03-25 01:24:25 -08001005 rtnl_unlock();
1006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009/**
1010 * call_netdevice_notifiers - call all network notifier blocks
1011 * @val: value passed unmodified to notifier function
1012 * @v: pointer passed unmodified to notifier function
1013 *
1014 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001015 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
1017
1018int call_netdevice_notifiers(unsigned long val, void *v)
1019{
Alan Sternf07d5b92006-05-09 15:23:03 -07001020 return raw_notifier_call_chain(&netdev_chain, val, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023/* When > 0 there are consumers of rx skb time stamps */
1024static atomic_t netstamp_needed = ATOMIC_INIT(0);
1025
1026void net_enable_timestamp(void)
1027{
1028 atomic_inc(&netstamp_needed);
1029}
1030
1031void net_disable_timestamp(void)
1032{
1033 atomic_dec(&netstamp_needed);
1034}
1035
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001036void __net_timestamp(struct sk_buff *skb)
1037{
1038 struct timeval tv;
1039
1040 do_gettimeofday(&tv);
1041 skb_set_timestamp(skb, &tv);
1042}
1043EXPORT_SYMBOL(__net_timestamp);
1044
1045static inline void net_timestamp(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 if (atomic_read(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001048 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 else {
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001050 skb->tstamp.off_sec = 0;
1051 skb->tstamp.off_usec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053}
1054
1055/*
1056 * Support routine. Sends outgoing frames to any network
1057 * taps currently in use.
1058 */
1059
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001060static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 struct packet_type *ptype;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001063
1064 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 rcu_read_lock();
1067 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1068 /* Never send packets back to the socket
1069 * they originated from - MvS (miquels@drinkel.ow.org)
1070 */
1071 if ((ptype->dev == dev || !ptype->dev) &&
1072 (ptype->af_packet_priv == NULL ||
1073 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1074 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1075 if (!skb2)
1076 break;
1077
1078 /* skb->nh should be correctly
1079 set by sender, so that the second statement is
1080 just protection against buggy protocols.
1081 */
1082 skb2->mac.raw = skb2->data;
1083
1084 if (skb2->nh.raw < skb2->data ||
1085 skb2->nh.raw > skb2->tail) {
1086 if (net_ratelimit())
1087 printk(KERN_CRIT "protocol %04x is "
1088 "buggy, dev %s\n",
1089 skb2->protocol, dev->name);
1090 skb2->nh.raw = skb2->data;
1091 }
1092
1093 skb2->h.raw = skb2->nh.raw;
1094 skb2->pkt_type = PACKET_OUTGOING;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001095 ptype->func(skb2, skb->dev, ptype, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 }
1098 rcu_read_unlock();
1099}
1100
Denis Vlasenko56079432006-03-29 15:57:29 -08001101
1102void __netif_schedule(struct net_device *dev)
1103{
1104 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1105 unsigned long flags;
1106 struct softnet_data *sd;
1107
1108 local_irq_save(flags);
1109 sd = &__get_cpu_var(softnet_data);
1110 dev->next_sched = sd->output_queue;
1111 sd->output_queue = dev;
1112 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1113 local_irq_restore(flags);
1114 }
1115}
1116EXPORT_SYMBOL(__netif_schedule);
1117
1118void __netif_rx_schedule(struct net_device *dev)
1119{
1120 unsigned long flags;
1121
1122 local_irq_save(flags);
1123 dev_hold(dev);
1124 list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
1125 if (dev->quota < 0)
1126 dev->quota += dev->weight;
1127 else
1128 dev->quota = dev->weight;
1129 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1130 local_irq_restore(flags);
1131}
1132EXPORT_SYMBOL(__netif_rx_schedule);
1133
1134void dev_kfree_skb_any(struct sk_buff *skb)
1135{
1136 if (in_irq() || irqs_disabled())
1137 dev_kfree_skb_irq(skb);
1138 else
1139 dev_kfree_skb(skb);
1140}
1141EXPORT_SYMBOL(dev_kfree_skb_any);
1142
1143
1144/* Hot-plugging. */
1145void netif_device_detach(struct net_device *dev)
1146{
1147 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1148 netif_running(dev)) {
1149 netif_stop_queue(dev);
1150 }
1151}
1152EXPORT_SYMBOL(netif_device_detach);
1153
1154void netif_device_attach(struct net_device *dev)
1155{
1156 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1157 netif_running(dev)) {
1158 netif_wake_queue(dev);
1159 __netdev_watchdog_up(dev);
1160 }
1161}
1162EXPORT_SYMBOL(netif_device_attach);
1163
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/*
1166 * Invalidate hardware checksum when packet is to be mangled, and
1167 * complete checksum manually on outgoing path.
1168 */
1169int skb_checksum_help(struct sk_buff *skb, int inward)
1170{
1171 unsigned int csum;
1172 int ret = 0, offset = skb->h.raw - skb->data;
1173
Herbert Xua430a432006-07-08 13:34:56 -07001174 if (inward)
1175 goto out_set_summed;
1176
1177 if (unlikely(skb_shinfo(skb)->gso_size)) {
Herbert Xua430a432006-07-08 13:34:56 -07001178 /* Let GSO fix up the checksum. */
1179 goto out_set_summed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
1182 if (skb_cloned(skb)) {
1183 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1184 if (ret)
1185 goto out;
1186 }
1187
Kris Katterjohn09a62662006-01-08 22:24:28 -08001188 BUG_ON(offset > (int)skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1190
1191 offset = skb->tail - skb->h.raw;
Kris Katterjohn09a62662006-01-08 22:24:28 -08001192 BUG_ON(offset <= 0);
1193 BUG_ON(skb->csum + 2 > offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
Herbert Xua430a432006-07-08 13:34:56 -07001196
1197out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 skb->ip_summed = CHECKSUM_NONE;
1199out:
1200 return ret;
1201}
1202
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001203/**
1204 * skb_gso_segment - Perform segmentation on skb.
1205 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001206 * @features: features for the output path (see dev->features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001207 *
1208 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07001209 *
1210 * It may return NULL if the skb requires no segmentation. This is
1211 * only possible when GSO is used for verifying header integrity.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001212 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001213struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001214{
1215 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1216 struct packet_type *ptype;
1217 int type = skb->protocol;
Herbert Xua430a432006-07-08 13:34:56 -07001218 int err;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001219
1220 BUG_ON(skb_shinfo(skb)->frag_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001221
1222 skb->mac.raw = skb->data;
1223 skb->mac_len = skb->nh.raw - skb->data;
1224 __skb_pull(skb, skb->mac_len);
1225
Herbert Xua430a432006-07-08 13:34:56 -07001226 if (unlikely(skb->ip_summed != CHECKSUM_HW)) {
Herbert Xua430a432006-07-08 13:34:56 -07001227 if (skb_header_cloned(skb) &&
1228 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1229 return ERR_PTR(err);
1230 }
1231
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001232 rcu_read_lock();
1233 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1234 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
Herbert Xua430a432006-07-08 13:34:56 -07001235 if (unlikely(skb->ip_summed != CHECKSUM_HW)) {
1236 err = ptype->gso_send_check(skb);
1237 segs = ERR_PTR(err);
1238 if (err || skb_gso_ok(skb, features))
1239 break;
1240 __skb_push(skb, skb->data - skb->nh.raw);
1241 }
Herbert Xu576a30e2006-06-27 13:22:38 -07001242 segs = ptype->gso_segment(skb, features);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001243 break;
1244 }
1245 }
1246 rcu_read_unlock();
1247
Herbert Xu576a30e2006-06-27 13:22:38 -07001248 __skb_push(skb, skb->data - skb->mac.raw);
1249
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001250 return segs;
1251}
1252
1253EXPORT_SYMBOL(skb_gso_segment);
1254
Herbert Xufb286bb2005-11-10 13:01:24 -08001255/* Take action when hardware reception checksum errors are detected. */
1256#ifdef CONFIG_BUG
1257void netdev_rx_csum_fault(struct net_device *dev)
1258{
1259 if (net_ratelimit()) {
Stephen Hemminger246a4212005-12-08 15:21:39 -08001260 printk(KERN_ERR "%s: hw csum failure.\n",
1261 dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08001262 dump_stack();
1263 }
1264}
1265EXPORT_SYMBOL(netdev_rx_csum_fault);
1266#endif
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/* Actually, we should eliminate this check as soon as we know, that:
1269 * 1. IOMMU is present and allows to map all the memory.
1270 * 2. No high memory really exists on this machine.
1271 */
1272
1273static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1274{
Herbert Xu3d3a8532006-06-27 13:33:10 -07001275#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int i;
1277
1278 if (dev->features & NETIF_F_HIGHDMA)
1279 return 0;
1280
1281 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1282 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1283 return 1;
1284
Herbert Xu3d3a8532006-06-27 13:33:10 -07001285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001289struct dev_gso_cb {
1290 void (*destructor)(struct sk_buff *skb);
1291};
1292
1293#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1294
1295static void dev_gso_skb_destructor(struct sk_buff *skb)
1296{
1297 struct dev_gso_cb *cb;
1298
1299 do {
1300 struct sk_buff *nskb = skb->next;
1301
1302 skb->next = nskb->next;
1303 nskb->next = NULL;
1304 kfree_skb(nskb);
1305 } while (skb->next);
1306
1307 cb = DEV_GSO_CB(skb);
1308 if (cb->destructor)
1309 cb->destructor(skb);
1310}
1311
1312/**
1313 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1314 * @skb: buffer to segment
1315 *
1316 * This function segments the given skb and stores the list of segments
1317 * in skb->next.
1318 */
1319static int dev_gso_segment(struct sk_buff *skb)
1320{
1321 struct net_device *dev = skb->dev;
1322 struct sk_buff *segs;
Herbert Xu576a30e2006-06-27 13:22:38 -07001323 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1324 NETIF_F_SG : 0);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001325
Herbert Xu576a30e2006-06-27 13:22:38 -07001326 segs = skb_gso_segment(skb, features);
1327
1328 /* Verifying header integrity only. */
1329 if (!segs)
1330 return 0;
1331
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001332 if (unlikely(IS_ERR(segs)))
1333 return PTR_ERR(segs);
1334
1335 skb->next = segs;
1336 DEV_GSO_CB(skb)->destructor = skb->destructor;
1337 skb->destructor = dev_gso_skb_destructor;
1338
1339 return 0;
1340}
1341
1342int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1343{
1344 if (likely(!skb->next)) {
1345 if (netdev_nit)
1346 dev_queue_xmit_nit(skb, dev);
1347
Herbert Xu576a30e2006-06-27 13:22:38 -07001348 if (netif_needs_gso(dev, skb)) {
1349 if (unlikely(dev_gso_segment(skb)))
1350 goto out_kfree_skb;
1351 if (skb->next)
1352 goto gso;
1353 }
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001354
Herbert Xu576a30e2006-06-27 13:22:38 -07001355 return dev->hard_start_xmit(skb, dev);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001356 }
1357
Herbert Xu576a30e2006-06-27 13:22:38 -07001358gso:
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001359 do {
1360 struct sk_buff *nskb = skb->next;
1361 int rc;
1362
1363 skb->next = nskb->next;
1364 nskb->next = NULL;
1365 rc = dev->hard_start_xmit(nskb, dev);
1366 if (unlikely(rc)) {
Michael Chanf54d9e82006-06-25 23:57:04 -07001367 nskb->next = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001368 skb->next = nskb;
1369 return rc;
1370 }
Michael Chanf54d9e82006-06-25 23:57:04 -07001371 if (unlikely(netif_queue_stopped(dev) && skb->next))
1372 return NETDEV_TX_BUSY;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001373 } while (skb->next);
1374
1375 skb->destructor = DEV_GSO_CB(skb)->destructor;
1376
1377out_kfree_skb:
1378 kfree_skb(skb);
1379 return 0;
1380}
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382#define HARD_TX_LOCK(dev, cpu) { \
1383 if ((dev->features & NETIF_F_LLTX) == 0) { \
Herbert Xu932ff272006-06-09 12:20:56 -07001384 netif_tx_lock(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 } \
1386}
1387
1388#define HARD_TX_UNLOCK(dev) { \
1389 if ((dev->features & NETIF_F_LLTX) == 0) { \
Herbert Xu932ff272006-06-09 12:20:56 -07001390 netif_tx_unlock(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 } \
1392}
1393
1394/**
1395 * dev_queue_xmit - transmit a buffer
1396 * @skb: buffer to transmit
1397 *
1398 * Queue a buffer for transmission to a network device. The caller must
1399 * have set the device and priority and built the buffer before calling
1400 * this function. The function can be called from an interrupt.
1401 *
1402 * A negative errno code is returned on a failure. A success does not
1403 * guarantee the frame will be transmitted as it may be dropped due
1404 * to congestion or traffic shaping.
Ben Greearaf191362005-04-24 20:12:36 -07001405 *
1406 * -----------------------------------------------------------------------------------
1407 * I notice this method can also return errors from the queue disciplines,
1408 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1409 * be positive.
1410 *
1411 * Regardless of the return value, the skb is consumed, so it is currently
1412 * difficult to retry a send to this method. (You can bump the ref count
1413 * before sending to hold a reference for retry if you are careful.)
1414 *
1415 * When calling this method, interrupts MUST be enabled. This is because
1416 * the BH enable code must have IRQs enabled so that it will not deadlock.
1417 * --BLG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
1419
1420int dev_queue_xmit(struct sk_buff *skb)
1421{
1422 struct net_device *dev = skb->dev;
1423 struct Qdisc *q;
1424 int rc = -ENOMEM;
1425
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001426 /* GSO will handle the following emulations directly. */
1427 if (netif_needs_gso(dev, skb))
1428 goto gso;
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (skb_shinfo(skb)->frag_list &&
1431 !(dev->features & NETIF_F_FRAGLIST) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001432 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto out_kfree_skb;
1434
1435 /* Fragmented skb is linearized if device does not support SG,
1436 * or if at least one of fragments is in highmem and device
1437 * does not support DMA from it.
1438 */
1439 if (skb_shinfo(skb)->nr_frags &&
1440 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001441 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 goto out_kfree_skb;
1443
1444 /* If packet is not checksummed and device does not support
1445 * checksumming for this protocol, complete checksumming here.
1446 */
1447 if (skb->ip_summed == CHECKSUM_HW &&
Herbert Xu8648b302006-06-17 22:06:05 -07001448 (!(dev->features & NETIF_F_GEN_CSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 (!(dev->features & NETIF_F_IP_CSUM) ||
1450 skb->protocol != htons(ETH_P_IP))))
1451 if (skb_checksum_help(skb, 0))
1452 goto out_kfree_skb;
1453
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001454gso:
Eric Dumazet2d7ceec2005-09-27 15:22:58 -07001455 spin_lock_prefetch(&dev->queue_lock);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* Disable soft irqs for various locks below. Also
1458 * stops preemption for RCU.
1459 */
Herbert Xud4828d82006-06-22 02:28:18 -07001460 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 /* Updates of qdisc are serialized by queue_lock.
1463 * The struct Qdisc which is pointed to by qdisc is now a
1464 * rcu structure - it may be accessed without acquiring
1465 * a lock (but the structure may be stale.) The freeing of the
1466 * qdisc will be deferred until it's known that there are no
1467 * more references to it.
1468 *
1469 * If the qdisc has an enqueue function, we still need to
1470 * hold the queue_lock before calling it, since queue_lock
1471 * also serializes access to the device queue.
1472 */
1473
1474 q = rcu_dereference(dev->qdisc);
1475#ifdef CONFIG_NET_CLS_ACT
1476 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1477#endif
1478 if (q->enqueue) {
1479 /* Grab device queue */
1480 spin_lock(&dev->queue_lock);
1481
1482 rc = q->enqueue(skb, q);
1483
1484 qdisc_run(dev);
1485
1486 spin_unlock(&dev->queue_lock);
1487 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1488 goto out;
1489 }
1490
1491 /* The device has no queue. Common case for software devices:
1492 loopback, all the sorts of tunnels...
1493
Herbert Xu932ff272006-06-09 12:20:56 -07001494 Really, it is unlikely that netif_tx_lock protection is necessary
1495 here. (f.e. loopback and IP tunnels are clean ignoring statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 counters.)
1497 However, it is possible, that they rely on protection
1498 made by us here.
1499
1500 Check this and shot the lock. It is not prone from deadlocks.
1501 Either shot noqueue qdisc, it is even simpler 8)
1502 */
1503 if (dev->flags & IFF_UP) {
1504 int cpu = smp_processor_id(); /* ok because BHs are off */
1505
1506 if (dev->xmit_lock_owner != cpu) {
1507
1508 HARD_TX_LOCK(dev, cpu);
1509
1510 if (!netif_queue_stopped(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 rc = 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001512 if (!dev_hard_start_xmit(skb, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 HARD_TX_UNLOCK(dev);
1514 goto out;
1515 }
1516 }
1517 HARD_TX_UNLOCK(dev);
1518 if (net_ratelimit())
1519 printk(KERN_CRIT "Virtual device %s asks to "
1520 "queue packet!\n", dev->name);
1521 } else {
1522 /* Recursion is detected! It is possible,
1523 * unfortunately */
1524 if (net_ratelimit())
1525 printk(KERN_CRIT "Dead loop on virtual device "
1526 "%s, fix it urgently!\n", dev->name);
1527 }
1528 }
1529
1530 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07001531 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533out_kfree_skb:
1534 kfree_skb(skb);
1535 return rc;
1536out:
Herbert Xud4828d82006-06-22 02:28:18 -07001537 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return rc;
1539}
1540
1541
1542/*=======================================================================
1543 Receiver routines
1544 =======================================================================*/
1545
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07001546int netdev_max_backlog = 1000;
1547int netdev_budget = 300;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548int weight_p = 64; /* old backlog weight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1551
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/**
1554 * netif_rx - post buffer to the network code
1555 * @skb: buffer to post
1556 *
1557 * This function receives a packet from a device driver and queues it for
1558 * the upper (protocol) levels to process. It always succeeds. The buffer
1559 * may be dropped during processing for congestion control or by the
1560 * protocol layers.
1561 *
1562 * return values:
1563 * NET_RX_SUCCESS (no congestion)
1564 * NET_RX_CN_LOW (low congestion)
1565 * NET_RX_CN_MOD (moderate congestion)
1566 * NET_RX_CN_HIGH (high congestion)
1567 * NET_RX_DROP (packet was dropped)
1568 *
1569 */
1570
1571int netif_rx(struct sk_buff *skb)
1572{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 struct softnet_data *queue;
1574 unsigned long flags;
1575
1576 /* if netpoll wants it, pretend we never saw it */
1577 if (netpoll_rx(skb))
1578 return NET_RX_DROP;
1579
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001580 if (!skb->tstamp.off_sec)
1581 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 /*
1584 * The code is rearranged so that the path is the most
1585 * short when CPU is congested, but is still operating.
1586 */
1587 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 queue = &__get_cpu_var(softnet_data);
1589
1590 __get_cpu_var(netdev_rx_stat).total++;
1591 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1592 if (queue->input_pkt_queue.qlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593enqueue:
1594 dev_hold(skb->dev);
1595 __skb_queue_tail(&queue->input_pkt_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 local_irq_restore(flags);
Stephen Hemminger34008d82005-06-23 20:10:00 -07001597 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 netif_rx_schedule(&queue->backlog_dev);
1601 goto enqueue;
1602 }
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 __get_cpu_var(netdev_rx_stat).dropped++;
1605 local_irq_restore(flags);
1606
1607 kfree_skb(skb);
1608 return NET_RX_DROP;
1609}
1610
1611int netif_rx_ni(struct sk_buff *skb)
1612{
1613 int err;
1614
1615 preempt_disable();
1616 err = netif_rx(skb);
1617 if (local_softirq_pending())
1618 do_softirq();
1619 preempt_enable();
1620
1621 return err;
1622}
1623
1624EXPORT_SYMBOL(netif_rx_ni);
1625
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001626static inline struct net_device *skb_bond(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 struct net_device *dev = skb->dev;
1629
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001630 if (dev->master) {
David S. Miller7ea49ed2006-08-14 17:08:36 -07001631 if (skb_bond_should_drop(skb)) {
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001632 kfree_skb(skb);
1633 return NULL;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 skb->dev = dev->master;
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001636 }
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001637
1638 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641static void net_tx_action(struct softirq_action *h)
1642{
1643 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1644
1645 if (sd->completion_queue) {
1646 struct sk_buff *clist;
1647
1648 local_irq_disable();
1649 clist = sd->completion_queue;
1650 sd->completion_queue = NULL;
1651 local_irq_enable();
1652
1653 while (clist) {
1654 struct sk_buff *skb = clist;
1655 clist = clist->next;
1656
1657 BUG_TRAP(!atomic_read(&skb->users));
1658 __kfree_skb(skb);
1659 }
1660 }
1661
1662 if (sd->output_queue) {
1663 struct net_device *head;
1664
1665 local_irq_disable();
1666 head = sd->output_queue;
1667 sd->output_queue = NULL;
1668 local_irq_enable();
1669
1670 while (head) {
1671 struct net_device *dev = head;
1672 head = head->next_sched;
1673
1674 smp_mb__before_clear_bit();
1675 clear_bit(__LINK_STATE_SCHED, &dev->state);
1676
1677 if (spin_trylock(&dev->queue_lock)) {
1678 qdisc_run(dev);
1679 spin_unlock(&dev->queue_lock);
1680 } else {
1681 netif_schedule(dev);
1682 }
1683 }
1684 }
1685}
1686
1687static __inline__ int deliver_skb(struct sk_buff *skb,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001688 struct packet_type *pt_prev,
1689 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 atomic_inc(&skb->users);
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001692 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1696int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1697struct net_bridge;
1698struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1699 unsigned char *addr);
1700void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
1701
1702static __inline__ int handle_bridge(struct sk_buff **pskb,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001703 struct packet_type **pt_prev, int *ret,
1704 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct net_bridge_port *port;
1707
1708 if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1709 (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1710 return 0;
1711
1712 if (*pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001713 *ret = deliver_skb(*pskb, *pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 *pt_prev = NULL;
1715 }
1716
1717 return br_handle_frame_hook(port, pskb);
1718}
1719#else
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001720#define handle_bridge(skb, pt_prev, ret, orig_dev) (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721#endif
1722
1723#ifdef CONFIG_NET_CLS_ACT
1724/* TODO: Maybe we should just force sch_ingress to be compiled in
1725 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1726 * a compare and 2 stores extra right now if we dont have it on
1727 * but have CONFIG_NET_CLS_ACT
1728 * NOTE: This doesnt stop any functionality; if you dont have
1729 * the ingress scheduler, you just cant add policies on ingress.
1730 *
1731 */
1732static int ing_filter(struct sk_buff *skb)
1733{
1734 struct Qdisc *q;
1735 struct net_device *dev = skb->dev;
1736 int result = TC_ACT_OK;
1737
1738 if (dev->qdisc_ingress) {
1739 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1740 if (MAX_RED_LOOP < ttl++) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07001741 printk(KERN_WARNING "Redir loop detected Dropping packet (%s->%s)\n",
David S. Miller86e65da2005-08-09 19:36:29 -07001742 skb->input_dev->name, skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return TC_ACT_SHOT;
1744 }
1745
1746 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1747
1748 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
David S. Miller86e65da2005-08-09 19:36:29 -07001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 spin_lock(&dev->ingress_lock);
1751 if ((q = dev->qdisc_ingress) != NULL)
1752 result = q->enqueue(skb, q);
1753 spin_unlock(&dev->ingress_lock);
1754
1755 }
1756
1757 return result;
1758}
1759#endif
1760
1761int netif_receive_skb(struct sk_buff *skb)
1762{
1763 struct packet_type *ptype, *pt_prev;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001764 struct net_device *orig_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 int ret = NET_RX_DROP;
1766 unsigned short type;
1767
1768 /* if we've gotten here through NAPI, check netpoll */
1769 if (skb->dev->poll && netpoll_rx(skb))
1770 return NET_RX_DROP;
1771
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001772 if (!skb->tstamp.off_sec)
1773 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
David S. Miller86e65da2005-08-09 19:36:29 -07001775 if (!skb->input_dev)
1776 skb->input_dev = skb->dev;
1777
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001778 orig_dev = skb_bond(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001780 if (!orig_dev)
1781 return NET_RX_DROP;
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 __get_cpu_var(netdev_rx_stat).total++;
1784
1785 skb->h.raw = skb->nh.raw = skb->data;
1786 skb->mac_len = skb->nh.raw - skb->mac.raw;
1787
1788 pt_prev = NULL;
1789
1790 rcu_read_lock();
1791
1792#ifdef CONFIG_NET_CLS_ACT
1793 if (skb->tc_verd & TC_NCLS) {
1794 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1795 goto ncls;
1796 }
1797#endif
1798
1799 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1800 if (!ptype->dev || ptype->dev == skb->dev) {
1801 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001802 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 pt_prev = ptype;
1804 }
1805 }
1806
1807#ifdef CONFIG_NET_CLS_ACT
1808 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001809 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 pt_prev = NULL; /* noone else should process this after*/
1811 } else {
1812 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1813 }
1814
1815 ret = ing_filter(skb);
1816
1817 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1818 kfree_skb(skb);
1819 goto out;
1820 }
1821
1822 skb->tc_verd = 0;
1823ncls:
1824#endif
1825
1826 handle_diverter(skb);
1827
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001828 if (handle_bridge(&skb, &pt_prev, &ret, orig_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 goto out;
1830
1831 type = skb->protocol;
1832 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1833 if (ptype->type == type &&
1834 (!ptype->dev || ptype->dev == skb->dev)) {
1835 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001836 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 pt_prev = ptype;
1838 }
1839 }
1840
1841 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001842 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } else {
1844 kfree_skb(skb);
1845 /* Jamal, now you will not able to escape explaining
1846 * me how you were going to use this. :-)
1847 */
1848 ret = NET_RX_DROP;
1849 }
1850
1851out:
1852 rcu_read_unlock();
1853 return ret;
1854}
1855
1856static int process_backlog(struct net_device *backlog_dev, int *budget)
1857{
1858 int work = 0;
1859 int quota = min(backlog_dev->quota, *budget);
1860 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1861 unsigned long start_time = jiffies;
1862
Stephen Hemmingere3876602005-06-08 14:56:01 -07001863 backlog_dev->weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 for (;;) {
1865 struct sk_buff *skb;
1866 struct net_device *dev;
1867
1868 local_irq_disable();
1869 skb = __skb_dequeue(&queue->input_pkt_queue);
1870 if (!skb)
1871 goto job_done;
1872 local_irq_enable();
1873
1874 dev = skb->dev;
1875
1876 netif_receive_skb(skb);
1877
1878 dev_put(dev);
1879
1880 work++;
1881
1882 if (work >= quota || jiffies - start_time > 1)
1883 break;
1884
1885 }
1886
1887 backlog_dev->quota -= work;
1888 *budget -= work;
1889 return -1;
1890
1891job_done:
1892 backlog_dev->quota -= work;
1893 *budget -= work;
1894
1895 list_del(&backlog_dev->poll_list);
1896 smp_mb__before_clear_bit();
1897 netif_poll_enable(backlog_dev);
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 local_irq_enable();
1900 return 0;
1901}
1902
1903static void net_rx_action(struct softirq_action *h)
1904{
1905 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1906 unsigned long start_time = jiffies;
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07001907 int budget = netdev_budget;
Matt Mackall53fb95d2005-08-11 19:27:43 -07001908 void *have;
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 local_irq_disable();
1911
1912 while (!list_empty(&queue->poll_list)) {
1913 struct net_device *dev;
1914
1915 if (budget <= 0 || jiffies - start_time > 1)
1916 goto softnet_break;
1917
1918 local_irq_enable();
1919
1920 dev = list_entry(queue->poll_list.next,
1921 struct net_device, poll_list);
Matt Mackall53fb95d2005-08-11 19:27:43 -07001922 have = netpoll_poll_lock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
Matt Mackall53fb95d2005-08-11 19:27:43 -07001925 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 local_irq_disable();
Stephen Hemminger8aca8a22006-03-20 22:26:39 -08001927 list_move_tail(&dev->poll_list, &queue->poll_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (dev->quota < 0)
1929 dev->quota += dev->weight;
1930 else
1931 dev->quota = dev->weight;
1932 } else {
Matt Mackall53fb95d2005-08-11 19:27:43 -07001933 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 dev_put(dev);
1935 local_irq_disable();
1936 }
1937 }
1938out:
Chris Leechdb217332006-06-17 21:24:58 -07001939#ifdef CONFIG_NET_DMA
1940 /*
1941 * There may not be any more sk_buffs coming right now, so push
1942 * any pending DMA copies to hardware
1943 */
1944 if (net_dma_client) {
1945 struct dma_chan *chan;
1946 rcu_read_lock();
1947 list_for_each_entry_rcu(chan, &net_dma_client->channels, client_node)
1948 dma_async_memcpy_issue_pending(chan);
1949 rcu_read_unlock();
1950 }
1951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 local_irq_enable();
1953 return;
1954
1955softnet_break:
1956 __get_cpu_var(netdev_rx_stat).time_squeeze++;
1957 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1958 goto out;
1959}
1960
1961static gifconf_func_t * gifconf_list [NPROTO];
1962
1963/**
1964 * register_gifconf - register a SIOCGIF handler
1965 * @family: Address family
1966 * @gifconf: Function handler
1967 *
1968 * Register protocol dependent address dumping routines. The handler
1969 * that is passed must not be freed or reused until it has been replaced
1970 * by another handler.
1971 */
1972int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1973{
1974 if (family >= NPROTO)
1975 return -EINVAL;
1976 gifconf_list[family] = gifconf;
1977 return 0;
1978}
1979
1980
1981/*
1982 * Map an interface index to its name (SIOCGIFNAME)
1983 */
1984
1985/*
1986 * We need this ioctl for efficient implementation of the
1987 * if_indextoname() function required by the IPv6 API. Without
1988 * it, we would have to search all the interfaces to find a
1989 * match. --pb
1990 */
1991
1992static int dev_ifname(struct ifreq __user *arg)
1993{
1994 struct net_device *dev;
1995 struct ifreq ifr;
1996
1997 /*
1998 * Fetch the caller's info block.
1999 */
2000
2001 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2002 return -EFAULT;
2003
2004 read_lock(&dev_base_lock);
2005 dev = __dev_get_by_index(ifr.ifr_ifindex);
2006 if (!dev) {
2007 read_unlock(&dev_base_lock);
2008 return -ENODEV;
2009 }
2010
2011 strcpy(ifr.ifr_name, dev->name);
2012 read_unlock(&dev_base_lock);
2013
2014 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2015 return -EFAULT;
2016 return 0;
2017}
2018
2019/*
2020 * Perform a SIOCGIFCONF call. This structure will change
2021 * size eventually, and there is nothing I can do about it.
2022 * Thus we will need a 'compatibility mode'.
2023 */
2024
2025static int dev_ifconf(char __user *arg)
2026{
2027 struct ifconf ifc;
2028 struct net_device *dev;
2029 char __user *pos;
2030 int len;
2031 int total;
2032 int i;
2033
2034 /*
2035 * Fetch the caller's info block.
2036 */
2037
2038 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2039 return -EFAULT;
2040
2041 pos = ifc.ifc_buf;
2042 len = ifc.ifc_len;
2043
2044 /*
2045 * Loop over the interfaces, and write an info block for each.
2046 */
2047
2048 total = 0;
2049 for (dev = dev_base; dev; dev = dev->next) {
2050 for (i = 0; i < NPROTO; i++) {
2051 if (gifconf_list[i]) {
2052 int done;
2053 if (!pos)
2054 done = gifconf_list[i](dev, NULL, 0);
2055 else
2056 done = gifconf_list[i](dev, pos + total,
2057 len - total);
2058 if (done < 0)
2059 return -EFAULT;
2060 total += done;
2061 }
2062 }
2063 }
2064
2065 /*
2066 * All done. Write the updated control block back to the caller.
2067 */
2068 ifc.ifc_len = total;
2069
2070 /*
2071 * Both BSD and Solaris return 0 here, so we do too.
2072 */
2073 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2074}
2075
2076#ifdef CONFIG_PROC_FS
2077/*
2078 * This is invoked by the /proc filesystem handler to display a device
2079 * in detail.
2080 */
2081static __inline__ struct net_device *dev_get_idx(loff_t pos)
2082{
2083 struct net_device *dev;
2084 loff_t i;
2085
2086 for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
2087
2088 return i == pos ? dev : NULL;
2089}
2090
2091void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2092{
2093 read_lock(&dev_base_lock);
2094 return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
2095}
2096
2097void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2098{
2099 ++*pos;
2100 return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
2101}
2102
2103void dev_seq_stop(struct seq_file *seq, void *v)
2104{
2105 read_unlock(&dev_base_lock);
2106}
2107
2108static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2109{
2110 if (dev->get_stats) {
2111 struct net_device_stats *stats = dev->get_stats(dev);
2112
2113 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2114 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2115 dev->name, stats->rx_bytes, stats->rx_packets,
2116 stats->rx_errors,
2117 stats->rx_dropped + stats->rx_missed_errors,
2118 stats->rx_fifo_errors,
2119 stats->rx_length_errors + stats->rx_over_errors +
2120 stats->rx_crc_errors + stats->rx_frame_errors,
2121 stats->rx_compressed, stats->multicast,
2122 stats->tx_bytes, stats->tx_packets,
2123 stats->tx_errors, stats->tx_dropped,
2124 stats->tx_fifo_errors, stats->collisions,
2125 stats->tx_carrier_errors +
2126 stats->tx_aborted_errors +
2127 stats->tx_window_errors +
2128 stats->tx_heartbeat_errors,
2129 stats->tx_compressed);
2130 } else
2131 seq_printf(seq, "%6s: No statistics available.\n", dev->name);
2132}
2133
2134/*
2135 * Called from the PROCfs module. This now uses the new arbitrary sized
2136 * /proc/net interface to create /proc/net/dev
2137 */
2138static int dev_seq_show(struct seq_file *seq, void *v)
2139{
2140 if (v == SEQ_START_TOKEN)
2141 seq_puts(seq, "Inter-| Receive "
2142 " | Transmit\n"
2143 " face |bytes packets errs drop fifo frame "
2144 "compressed multicast|bytes packets errs "
2145 "drop fifo colls carrier compressed\n");
2146 else
2147 dev_seq_printf_stats(seq, v);
2148 return 0;
2149}
2150
2151static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2152{
2153 struct netif_rx_stats *rc = NULL;
2154
2155 while (*pos < NR_CPUS)
2156 if (cpu_online(*pos)) {
2157 rc = &per_cpu(netdev_rx_stat, *pos);
2158 break;
2159 } else
2160 ++*pos;
2161 return rc;
2162}
2163
2164static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2165{
2166 return softnet_get_online(pos);
2167}
2168
2169static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2170{
2171 ++*pos;
2172 return softnet_get_online(pos);
2173}
2174
2175static void softnet_seq_stop(struct seq_file *seq, void *v)
2176{
2177}
2178
2179static int softnet_seq_show(struct seq_file *seq, void *v)
2180{
2181 struct netif_rx_stats *s = v;
2182
2183 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Stephen Hemminger31aa02c2005-06-23 20:12:48 -07002184 s->total, s->dropped, s->time_squeeze, 0,
Stephen Hemmingerc1ebcdb2005-06-23 20:08:59 -07002185 0, 0, 0, 0, /* was fastroute */
2186 s->cpu_collision );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return 0;
2188}
2189
2190static struct seq_operations dev_seq_ops = {
2191 .start = dev_seq_start,
2192 .next = dev_seq_next,
2193 .stop = dev_seq_stop,
2194 .show = dev_seq_show,
2195};
2196
2197static int dev_seq_open(struct inode *inode, struct file *file)
2198{
2199 return seq_open(file, &dev_seq_ops);
2200}
2201
2202static struct file_operations dev_seq_fops = {
2203 .owner = THIS_MODULE,
2204 .open = dev_seq_open,
2205 .read = seq_read,
2206 .llseek = seq_lseek,
2207 .release = seq_release,
2208};
2209
2210static struct seq_operations softnet_seq_ops = {
2211 .start = softnet_seq_start,
2212 .next = softnet_seq_next,
2213 .stop = softnet_seq_stop,
2214 .show = softnet_seq_show,
2215};
2216
2217static int softnet_seq_open(struct inode *inode, struct file *file)
2218{
2219 return seq_open(file, &softnet_seq_ops);
2220}
2221
2222static struct file_operations softnet_seq_fops = {
2223 .owner = THIS_MODULE,
2224 .open = softnet_seq_open,
2225 .read = seq_read,
2226 .llseek = seq_lseek,
2227 .release = seq_release,
2228};
2229
Adrian Bunkd86b5e02006-01-21 00:46:55 +01002230#ifdef CONFIG_WIRELESS_EXT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231extern int wireless_proc_init(void);
2232#else
2233#define wireless_proc_init() 0
2234#endif
2235
2236static int __init dev_proc_init(void)
2237{
2238 int rc = -ENOMEM;
2239
2240 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2241 goto out;
2242 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2243 goto out_dev;
2244 if (wireless_proc_init())
2245 goto out_softnet;
2246 rc = 0;
2247out:
2248 return rc;
2249out_softnet:
2250 proc_net_remove("softnet_stat");
2251out_dev:
2252 proc_net_remove("dev");
2253 goto out;
2254}
2255#else
2256#define dev_proc_init() 0
2257#endif /* CONFIG_PROC_FS */
2258
2259
2260/**
2261 * netdev_set_master - set up master/slave pair
2262 * @slave: slave device
2263 * @master: new master device
2264 *
2265 * Changes the master device of the slave. Pass %NULL to break the
2266 * bonding. The caller must hold the RTNL semaphore. On a failure
2267 * a negative errno code is returned. On success the reference counts
2268 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2269 * function returns zero.
2270 */
2271int netdev_set_master(struct net_device *slave, struct net_device *master)
2272{
2273 struct net_device *old = slave->master;
2274
2275 ASSERT_RTNL();
2276
2277 if (master) {
2278 if (old)
2279 return -EBUSY;
2280 dev_hold(master);
2281 }
2282
2283 slave->master = master;
2284
2285 synchronize_net();
2286
2287 if (old)
2288 dev_put(old);
2289
2290 if (master)
2291 slave->flags |= IFF_SLAVE;
2292 else
2293 slave->flags &= ~IFF_SLAVE;
2294
2295 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2296 return 0;
2297}
2298
2299/**
2300 * dev_set_promiscuity - update promiscuity count on a device
2301 * @dev: device
2302 * @inc: modifier
2303 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07002304 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 * remains above zero the interface remains promiscuous. Once it hits zero
2306 * the device reverts back to normal filtering operation. A negative inc
2307 * value is used to drop promiscuity on the device.
2308 */
2309void dev_set_promiscuity(struct net_device *dev, int inc)
2310{
2311 unsigned short old_flags = dev->flags;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if ((dev->promiscuity += inc) == 0)
2314 dev->flags &= ~IFF_PROMISC;
David Chau52609c02005-07-05 15:11:06 -07002315 else
2316 dev->flags |= IFF_PROMISC;
2317 if (dev->flags != old_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 dev_mc_upload(dev);
2319 printk(KERN_INFO "device %s %s promiscuous mode\n",
2320 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2321 "left");
Steve Grubb5bdb9882005-12-03 08:39:35 -05002322 audit_log(current->audit_context, GFP_ATOMIC,
2323 AUDIT_ANOM_PROMISCUOUS,
2324 "dev=%s prom=%d old_prom=%d auid=%u",
2325 dev->name, (dev->flags & IFF_PROMISC),
2326 (old_flags & IFF_PROMISC),
2327 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329}
2330
2331/**
2332 * dev_set_allmulti - update allmulti count on a device
2333 * @dev: device
2334 * @inc: modifier
2335 *
2336 * Add or remove reception of all multicast frames to a device. While the
2337 * count in the device remains above zero the interface remains listening
2338 * to all interfaces. Once it hits zero the device reverts back to normal
2339 * filtering operation. A negative @inc value is used to drop the counter
2340 * when releasing a resource needing all multicasts.
2341 */
2342
2343void dev_set_allmulti(struct net_device *dev, int inc)
2344{
2345 unsigned short old_flags = dev->flags;
2346
2347 dev->flags |= IFF_ALLMULTI;
2348 if ((dev->allmulti += inc) == 0)
2349 dev->flags &= ~IFF_ALLMULTI;
2350 if (dev->flags ^ old_flags)
2351 dev_mc_upload(dev);
2352}
2353
2354unsigned dev_get_flags(const struct net_device *dev)
2355{
2356 unsigned flags;
2357
2358 flags = (dev->flags & ~(IFF_PROMISC |
2359 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08002360 IFF_RUNNING |
2361 IFF_LOWER_UP |
2362 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 (dev->gflags & (IFF_PROMISC |
2364 IFF_ALLMULTI));
2365
Stefan Rompfb00055a2006-03-20 17:09:11 -08002366 if (netif_running(dev)) {
2367 if (netif_oper_up(dev))
2368 flags |= IFF_RUNNING;
2369 if (netif_carrier_ok(dev))
2370 flags |= IFF_LOWER_UP;
2371 if (netif_dormant(dev))
2372 flags |= IFF_DORMANT;
2373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 return flags;
2376}
2377
2378int dev_change_flags(struct net_device *dev, unsigned flags)
2379{
2380 int ret;
2381 int old_flags = dev->flags;
2382
2383 /*
2384 * Set the flags on our device.
2385 */
2386
2387 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2388 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2389 IFF_AUTOMEDIA)) |
2390 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2391 IFF_ALLMULTI));
2392
2393 /*
2394 * Load in the correct multicast list now the flags have changed.
2395 */
2396
2397 dev_mc_upload(dev);
2398
2399 /*
2400 * Have we downed the interface. We handle IFF_UP ourselves
2401 * according to user attempts to set it, rather than blindly
2402 * setting it.
2403 */
2404
2405 ret = 0;
2406 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2407 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2408
2409 if (!ret)
2410 dev_mc_upload(dev);
2411 }
2412
2413 if (dev->flags & IFF_UP &&
2414 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2415 IFF_VOLATILE)))
Alan Sternf07d5b92006-05-09 15:23:03 -07002416 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002417 NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2420 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2421 dev->gflags ^= IFF_PROMISC;
2422 dev_set_promiscuity(dev, inc);
2423 }
2424
2425 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2426 is important. Some (broken) drivers set IFF_PROMISC, when
2427 IFF_ALLMULTI is requested not asking us and not reporting.
2428 */
2429 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2430 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2431 dev->gflags ^= IFF_ALLMULTI;
2432 dev_set_allmulti(dev, inc);
2433 }
2434
2435 if (old_flags ^ dev->flags)
2436 rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2437
2438 return ret;
2439}
2440
2441int dev_set_mtu(struct net_device *dev, int new_mtu)
2442{
2443 int err;
2444
2445 if (new_mtu == dev->mtu)
2446 return 0;
2447
2448 /* MTU must be positive. */
2449 if (new_mtu < 0)
2450 return -EINVAL;
2451
2452 if (!netif_device_present(dev))
2453 return -ENODEV;
2454
2455 err = 0;
2456 if (dev->change_mtu)
2457 err = dev->change_mtu(dev, new_mtu);
2458 else
2459 dev->mtu = new_mtu;
2460 if (!err && dev->flags & IFF_UP)
Alan Sternf07d5b92006-05-09 15:23:03 -07002461 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002462 NETDEV_CHANGEMTU, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return err;
2464}
2465
2466int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2467{
2468 int err;
2469
2470 if (!dev->set_mac_address)
2471 return -EOPNOTSUPP;
2472 if (sa->sa_family != dev->type)
2473 return -EINVAL;
2474 if (!netif_device_present(dev))
2475 return -ENODEV;
2476 err = dev->set_mac_address(dev, sa);
2477 if (!err)
Alan Sternf07d5b92006-05-09 15:23:03 -07002478 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002479 NETDEV_CHANGEADDR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return err;
2481}
2482
2483/*
2484 * Perform the SIOCxIFxxx calls.
2485 */
2486static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2487{
2488 int err;
2489 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2490
2491 if (!dev)
2492 return -ENODEV;
2493
2494 switch (cmd) {
2495 case SIOCGIFFLAGS: /* Get interface flags */
2496 ifr->ifr_flags = dev_get_flags(dev);
2497 return 0;
2498
2499 case SIOCSIFFLAGS: /* Set interface flags */
2500 return dev_change_flags(dev, ifr->ifr_flags);
2501
2502 case SIOCGIFMETRIC: /* Get the metric on the interface
2503 (currently unused) */
2504 ifr->ifr_metric = 0;
2505 return 0;
2506
2507 case SIOCSIFMETRIC: /* Set the metric on the interface
2508 (currently unused) */
2509 return -EOPNOTSUPP;
2510
2511 case SIOCGIFMTU: /* Get the MTU of a device */
2512 ifr->ifr_mtu = dev->mtu;
2513 return 0;
2514
2515 case SIOCSIFMTU: /* Set the MTU of a device */
2516 return dev_set_mtu(dev, ifr->ifr_mtu);
2517
2518 case SIOCGIFHWADDR:
2519 if (!dev->addr_len)
2520 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2521 else
2522 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2523 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2524 ifr->ifr_hwaddr.sa_family = dev->type;
2525 return 0;
2526
2527 case SIOCSIFHWADDR:
2528 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2529
2530 case SIOCSIFHWBROADCAST:
2531 if (ifr->ifr_hwaddr.sa_family != dev->type)
2532 return -EINVAL;
2533 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2534 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
Alan Sternf07d5b92006-05-09 15:23:03 -07002535 raw_notifier_call_chain(&netdev_chain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 NETDEV_CHANGEADDR, dev);
2537 return 0;
2538
2539 case SIOCGIFMAP:
2540 ifr->ifr_map.mem_start = dev->mem_start;
2541 ifr->ifr_map.mem_end = dev->mem_end;
2542 ifr->ifr_map.base_addr = dev->base_addr;
2543 ifr->ifr_map.irq = dev->irq;
2544 ifr->ifr_map.dma = dev->dma;
2545 ifr->ifr_map.port = dev->if_port;
2546 return 0;
2547
2548 case SIOCSIFMAP:
2549 if (dev->set_config) {
2550 if (!netif_device_present(dev))
2551 return -ENODEV;
2552 return dev->set_config(dev, &ifr->ifr_map);
2553 }
2554 return -EOPNOTSUPP;
2555
2556 case SIOCADDMULTI:
2557 if (!dev->set_multicast_list ||
2558 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2559 return -EINVAL;
2560 if (!netif_device_present(dev))
2561 return -ENODEV;
2562 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2563 dev->addr_len, 1);
2564
2565 case SIOCDELMULTI:
2566 if (!dev->set_multicast_list ||
2567 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2568 return -EINVAL;
2569 if (!netif_device_present(dev))
2570 return -ENODEV;
2571 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2572 dev->addr_len, 1);
2573
2574 case SIOCGIFINDEX:
2575 ifr->ifr_ifindex = dev->ifindex;
2576 return 0;
2577
2578 case SIOCGIFTXQLEN:
2579 ifr->ifr_qlen = dev->tx_queue_len;
2580 return 0;
2581
2582 case SIOCSIFTXQLEN:
2583 if (ifr->ifr_qlen < 0)
2584 return -EINVAL;
2585 dev->tx_queue_len = ifr->ifr_qlen;
2586 return 0;
2587
2588 case SIOCSIFNAME:
2589 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2590 return dev_change_name(dev, ifr->ifr_newname);
2591
2592 /*
2593 * Unknown or private ioctl
2594 */
2595
2596 default:
2597 if ((cmd >= SIOCDEVPRIVATE &&
2598 cmd <= SIOCDEVPRIVATE + 15) ||
2599 cmd == SIOCBONDENSLAVE ||
2600 cmd == SIOCBONDRELEASE ||
2601 cmd == SIOCBONDSETHWADDR ||
2602 cmd == SIOCBONDSLAVEINFOQUERY ||
2603 cmd == SIOCBONDINFOQUERY ||
2604 cmd == SIOCBONDCHANGEACTIVE ||
2605 cmd == SIOCGMIIPHY ||
2606 cmd == SIOCGMIIREG ||
2607 cmd == SIOCSMIIREG ||
2608 cmd == SIOCBRADDIF ||
2609 cmd == SIOCBRDELIF ||
2610 cmd == SIOCWANDEV) {
2611 err = -EOPNOTSUPP;
2612 if (dev->do_ioctl) {
2613 if (netif_device_present(dev))
2614 err = dev->do_ioctl(dev, ifr,
2615 cmd);
2616 else
2617 err = -ENODEV;
2618 }
2619 } else
2620 err = -EINVAL;
2621
2622 }
2623 return err;
2624}
2625
2626/*
2627 * This function handles all "interface"-type I/O control requests. The actual
2628 * 'doing' part of this is dev_ifsioc above.
2629 */
2630
2631/**
2632 * dev_ioctl - network device ioctl
2633 * @cmd: command to issue
2634 * @arg: pointer to a struct ifreq in user space
2635 *
2636 * Issue ioctl functions to devices. This is normally called by the
2637 * user space syscall interfaces but can sometimes be useful for
2638 * other purposes. The return value is the return from the syscall if
2639 * positive or a negative errno code on error.
2640 */
2641
2642int dev_ioctl(unsigned int cmd, void __user *arg)
2643{
2644 struct ifreq ifr;
2645 int ret;
2646 char *colon;
2647
2648 /* One special case: SIOCGIFCONF takes ifconf argument
2649 and requires shared lock, because it sleeps writing
2650 to user space.
2651 */
2652
2653 if (cmd == SIOCGIFCONF) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002654 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 ret = dev_ifconf((char __user *) arg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002656 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 return ret;
2658 }
2659 if (cmd == SIOCGIFNAME)
2660 return dev_ifname((struct ifreq __user *)arg);
2661
2662 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2663 return -EFAULT;
2664
2665 ifr.ifr_name[IFNAMSIZ-1] = 0;
2666
2667 colon = strchr(ifr.ifr_name, ':');
2668 if (colon)
2669 *colon = 0;
2670
2671 /*
2672 * See which interface the caller is talking about.
2673 */
2674
2675 switch (cmd) {
2676 /*
2677 * These ioctl calls:
2678 * - can be done by all.
2679 * - atomic and do not require locking.
2680 * - return a value
2681 */
2682 case SIOCGIFFLAGS:
2683 case SIOCGIFMETRIC:
2684 case SIOCGIFMTU:
2685 case SIOCGIFHWADDR:
2686 case SIOCGIFSLAVE:
2687 case SIOCGIFMAP:
2688 case SIOCGIFINDEX:
2689 case SIOCGIFTXQLEN:
2690 dev_load(ifr.ifr_name);
2691 read_lock(&dev_base_lock);
2692 ret = dev_ifsioc(&ifr, cmd);
2693 read_unlock(&dev_base_lock);
2694 if (!ret) {
2695 if (colon)
2696 *colon = ':';
2697 if (copy_to_user(arg, &ifr,
2698 sizeof(struct ifreq)))
2699 ret = -EFAULT;
2700 }
2701 return ret;
2702
2703 case SIOCETHTOOL:
2704 dev_load(ifr.ifr_name);
2705 rtnl_lock();
2706 ret = dev_ethtool(&ifr);
2707 rtnl_unlock();
2708 if (!ret) {
2709 if (colon)
2710 *colon = ':';
2711 if (copy_to_user(arg, &ifr,
2712 sizeof(struct ifreq)))
2713 ret = -EFAULT;
2714 }
2715 return ret;
2716
2717 /*
2718 * These ioctl calls:
2719 * - require superuser power.
2720 * - require strict serialization.
2721 * - return a value
2722 */
2723 case SIOCGMIIPHY:
2724 case SIOCGMIIREG:
2725 case SIOCSIFNAME:
2726 if (!capable(CAP_NET_ADMIN))
2727 return -EPERM;
2728 dev_load(ifr.ifr_name);
2729 rtnl_lock();
2730 ret = dev_ifsioc(&ifr, cmd);
2731 rtnl_unlock();
2732 if (!ret) {
2733 if (colon)
2734 *colon = ':';
2735 if (copy_to_user(arg, &ifr,
2736 sizeof(struct ifreq)))
2737 ret = -EFAULT;
2738 }
2739 return ret;
2740
2741 /*
2742 * These ioctl calls:
2743 * - require superuser power.
2744 * - require strict serialization.
2745 * - do not return a value
2746 */
2747 case SIOCSIFFLAGS:
2748 case SIOCSIFMETRIC:
2749 case SIOCSIFMTU:
2750 case SIOCSIFMAP:
2751 case SIOCSIFHWADDR:
2752 case SIOCSIFSLAVE:
2753 case SIOCADDMULTI:
2754 case SIOCDELMULTI:
2755 case SIOCSIFHWBROADCAST:
2756 case SIOCSIFTXQLEN:
2757 case SIOCSMIIREG:
2758 case SIOCBONDENSLAVE:
2759 case SIOCBONDRELEASE:
2760 case SIOCBONDSETHWADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 case SIOCBONDCHANGEACTIVE:
2762 case SIOCBRADDIF:
2763 case SIOCBRDELIF:
2764 if (!capable(CAP_NET_ADMIN))
2765 return -EPERM;
Thomas Grafcabcac02006-01-24 12:46:33 -08002766 /* fall through */
2767 case SIOCBONDSLAVEINFOQUERY:
2768 case SIOCBONDINFOQUERY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 dev_load(ifr.ifr_name);
2770 rtnl_lock();
2771 ret = dev_ifsioc(&ifr, cmd);
2772 rtnl_unlock();
2773 return ret;
2774
2775 case SIOCGIFMEM:
2776 /* Get the per device memory space. We can add this but
2777 * currently do not support it */
2778 case SIOCSIFMEM:
2779 /* Set the per device memory buffer space.
2780 * Not applicable in our case */
2781 case SIOCSIFLINK:
2782 return -EINVAL;
2783
2784 /*
2785 * Unknown or private ioctl.
2786 */
2787 default:
2788 if (cmd == SIOCWANDEV ||
2789 (cmd >= SIOCDEVPRIVATE &&
2790 cmd <= SIOCDEVPRIVATE + 15)) {
2791 dev_load(ifr.ifr_name);
2792 rtnl_lock();
2793 ret = dev_ifsioc(&ifr, cmd);
2794 rtnl_unlock();
2795 if (!ret && copy_to_user(arg, &ifr,
2796 sizeof(struct ifreq)))
2797 ret = -EFAULT;
2798 return ret;
2799 }
Adrian Bunkd86b5e02006-01-21 00:46:55 +01002800#ifdef CONFIG_WIRELESS_EXT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /* Take care of Wireless Extensions */
2802 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2803 /* If command is `set a parameter', or
2804 * `get the encoding parameters', check if
2805 * the user has the right to do it */
Jean Tourrilhesa4170162006-04-04 15:53:43 -07002806 if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE
2807 || cmd == SIOCGIWENCODEEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (!capable(CAP_NET_ADMIN))
2809 return -EPERM;
2810 }
2811 dev_load(ifr.ifr_name);
2812 rtnl_lock();
2813 /* Follow me in net/core/wireless.c */
2814 ret = wireless_process_ioctl(&ifr, cmd);
2815 rtnl_unlock();
2816 if (IW_IS_GET(cmd) &&
2817 copy_to_user(arg, &ifr,
2818 sizeof(struct ifreq)))
2819 ret = -EFAULT;
2820 return ret;
2821 }
Adrian Bunkd86b5e02006-01-21 00:46:55 +01002822#endif /* CONFIG_WIRELESS_EXT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return -EINVAL;
2824 }
2825}
2826
2827
2828/**
2829 * dev_new_index - allocate an ifindex
2830 *
2831 * Returns a suitable unique value for a new device interface
2832 * number. The caller must hold the rtnl semaphore or the
2833 * dev_base_lock to be sure it remains unique.
2834 */
2835static int dev_new_index(void)
2836{
2837 static int ifindex;
2838 for (;;) {
2839 if (++ifindex <= 0)
2840 ifindex = 1;
2841 if (!__dev_get_by_index(ifindex))
2842 return ifindex;
2843 }
2844}
2845
2846static int dev_boot_phase = 1;
2847
2848/* Delayed registration/unregisteration */
2849static DEFINE_SPINLOCK(net_todo_list_lock);
2850static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
2851
2852static inline void net_set_todo(struct net_device *dev)
2853{
2854 spin_lock(&net_todo_list_lock);
2855 list_add_tail(&dev->todo_list, &net_todo_list);
2856 spin_unlock(&net_todo_list_lock);
2857}
2858
2859/**
2860 * register_netdevice - register a network device
2861 * @dev: device to register
2862 *
2863 * Take a completed network device structure and add it to the kernel
2864 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2865 * chain. 0 is returned on success. A negative errno code is returned
2866 * on a failure to set up the device, or if the name is a duplicate.
2867 *
2868 * Callers must hold the rtnl semaphore. You may want
2869 * register_netdev() instead of this.
2870 *
2871 * BUGS:
2872 * The locking appears insufficient to guarantee two parallel registers
2873 * will not get the same name.
2874 */
2875
2876int register_netdevice(struct net_device *dev)
2877{
2878 struct hlist_head *head;
2879 struct hlist_node *p;
2880 int ret;
2881
2882 BUG_ON(dev_boot_phase);
2883 ASSERT_RTNL();
2884
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07002885 might_sleep();
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 /* When net_device's are persistent, this will be fatal. */
2888 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
2889
2890 spin_lock_init(&dev->queue_lock);
Herbert Xu932ff272006-06-09 12:20:56 -07002891 spin_lock_init(&dev->_xmit_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 dev->xmit_lock_owner = -1;
2893#ifdef CONFIG_NET_CLS_ACT
2894 spin_lock_init(&dev->ingress_lock);
2895#endif
2896
2897 ret = alloc_divert_blk(dev);
2898 if (ret)
2899 goto out;
2900
2901 dev->iflink = -1;
2902
2903 /* Init, if this function is available */
2904 if (dev->init) {
2905 ret = dev->init(dev);
2906 if (ret) {
2907 if (ret > 0)
2908 ret = -EIO;
2909 goto out_err;
2910 }
2911 }
2912
2913 if (!dev_valid_name(dev->name)) {
2914 ret = -EINVAL;
2915 goto out_err;
2916 }
2917
2918 dev->ifindex = dev_new_index();
2919 if (dev->iflink == -1)
2920 dev->iflink = dev->ifindex;
2921
2922 /* Check for existence of name */
2923 head = dev_name_hash(dev->name);
2924 hlist_for_each(p, head) {
2925 struct net_device *d
2926 = hlist_entry(p, struct net_device, name_hlist);
2927 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
2928 ret = -EEXIST;
2929 goto out_err;
2930 }
2931 }
2932
2933 /* Fix illegal SG+CSUM combinations. */
2934 if ((dev->features & NETIF_F_SG) &&
Herbert Xu8648b302006-06-17 22:06:05 -07002935 !(dev->features & NETIF_F_ALL_CSUM)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07002936 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 dev->name);
2938 dev->features &= ~NETIF_F_SG;
2939 }
2940
2941 /* TSO requires that SG is present as well. */
2942 if ((dev->features & NETIF_F_TSO) &&
2943 !(dev->features & NETIF_F_SG)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07002944 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 dev->name);
2946 dev->features &= ~NETIF_F_TSO;
2947 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002948 if (dev->features & NETIF_F_UFO) {
2949 if (!(dev->features & NETIF_F_HW_CSUM)) {
2950 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2951 "NETIF_F_HW_CSUM feature.\n",
2952 dev->name);
2953 dev->features &= ~NETIF_F_UFO;
2954 }
2955 if (!(dev->features & NETIF_F_SG)) {
2956 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
2957 "NETIF_F_SG feature.\n",
2958 dev->name);
2959 dev->features &= ~NETIF_F_UFO;
2960 }
2961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 /*
2964 * nil rebuild_header routine,
2965 * that should be never called and used as just bug trap.
2966 */
2967
2968 if (!dev->rebuild_header)
2969 dev->rebuild_header = default_rebuild_header;
2970
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07002971 ret = netdev_register_sysfs(dev);
2972 if (ret)
2973 goto out_err;
2974 dev->reg_state = NETREG_REGISTERED;
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /*
2977 * Default initial state at registry is that the
2978 * device is present.
2979 */
2980
2981 set_bit(__LINK_STATE_PRESENT, &dev->state);
2982
2983 dev->next = NULL;
2984 dev_init_scheduler(dev);
2985 write_lock_bh(&dev_base_lock);
2986 *dev_tail = dev;
2987 dev_tail = &dev->next;
2988 hlist_add_head(&dev->name_hlist, head);
2989 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
2990 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 write_unlock_bh(&dev_base_lock);
2992
2993 /* Notify protocols, that a new device appeared. */
Alan Sternf07d5b92006-05-09 15:23:03 -07002994 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 ret = 0;
2997
2998out:
2999 return ret;
3000out_err:
3001 free_divert_blk(dev);
3002 goto out;
3003}
3004
3005/**
3006 * register_netdev - register a network device
3007 * @dev: device to register
3008 *
3009 * Take a completed network device structure and add it to the kernel
3010 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3011 * chain. 0 is returned on success. A negative errno code is returned
3012 * on a failure to set up the device, or if the name is a duplicate.
3013 *
3014 * This is a wrapper around register_netdev that takes the rtnl semaphore
3015 * and expands the device name if you passed a format string to
3016 * alloc_netdev.
3017 */
3018int register_netdev(struct net_device *dev)
3019{
3020 int err;
3021
3022 rtnl_lock();
3023
3024 /*
3025 * If the name is a format string the caller wants us to do a
3026 * name allocation.
3027 */
3028 if (strchr(dev->name, '%')) {
3029 err = dev_alloc_name(dev, dev->name);
3030 if (err < 0)
3031 goto out;
3032 }
3033
3034 /*
3035 * Back compatibility hook. Kill this one in 2.5
3036 */
3037 if (dev->name[0] == 0 || dev->name[0] == ' ') {
3038 err = dev_alloc_name(dev, "eth%d");
3039 if (err < 0)
3040 goto out;
3041 }
3042
3043 err = register_netdevice(dev);
3044out:
3045 rtnl_unlock();
3046 return err;
3047}
3048EXPORT_SYMBOL(register_netdev);
3049
3050/*
3051 * netdev_wait_allrefs - wait until all references are gone.
3052 *
3053 * This is called when unregistering network devices.
3054 *
3055 * Any protocol or device that holds a reference should register
3056 * for netdevice notification, and cleanup and put back the
3057 * reference if they receive an UNREGISTER event.
3058 * We can get stuck here if buggy protocols don't correctly
3059 * call dev_put.
3060 */
3061static void netdev_wait_allrefs(struct net_device *dev)
3062{
3063 unsigned long rebroadcast_time, warning_time;
3064
3065 rebroadcast_time = warning_time = jiffies;
3066 while (atomic_read(&dev->refcnt) != 0) {
3067 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003068 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 /* Rebroadcast unregister notification */
Alan Sternf07d5b92006-05-09 15:23:03 -07003071 raw_notifier_call_chain(&netdev_chain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 NETDEV_UNREGISTER, dev);
3073
3074 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3075 &dev->state)) {
3076 /* We must not have linkwatch events
3077 * pending on unregister. If this
3078 * happens, we simply run the queue
3079 * unscheduled, resulting in a noop
3080 * for this device.
3081 */
3082 linkwatch_run_queue();
3083 }
3084
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003085 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087 rebroadcast_time = jiffies;
3088 }
3089
3090 msleep(250);
3091
3092 if (time_after(jiffies, warning_time + 10 * HZ)) {
3093 printk(KERN_EMERG "unregister_netdevice: "
3094 "waiting for %s to become free. Usage "
3095 "count = %d\n",
3096 dev->name, atomic_read(&dev->refcnt));
3097 warning_time = jiffies;
3098 }
3099 }
3100}
3101
3102/* The sequence is:
3103 *
3104 * rtnl_lock();
3105 * ...
3106 * register_netdevice(x1);
3107 * register_netdevice(x2);
3108 * ...
3109 * unregister_netdevice(y1);
3110 * unregister_netdevice(y2);
3111 * ...
3112 * rtnl_unlock();
3113 * free_netdev(y1);
3114 * free_netdev(y2);
3115 *
3116 * We are invoked by rtnl_unlock() after it drops the semaphore.
3117 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003118 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 * without deadlocking with linkwatch via keventd.
3120 * 2) Since we run with the RTNL semaphore not held, we can sleep
3121 * safely in order to wait for the netdev refcnt to drop to zero.
3122 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003123static DEFINE_MUTEX(net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124void netdev_run_todo(void)
3125{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003126 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 /* Need to guard against multiple cpu's getting out of order. */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003129 mutex_lock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 /* Not safe to do outside the semaphore. We must not return
3132 * until all unregister events invoked by the local processor
3133 * have been completed (either by this todo run, or one on
3134 * another cpu).
3135 */
3136 if (list_empty(&net_todo_list))
3137 goto out;
3138
3139 /* Snapshot list, allow later requests */
3140 spin_lock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003141 list_replace_init(&net_todo_list, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 spin_unlock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 while (!list_empty(&list)) {
3145 struct net_device *dev
3146 = list_entry(list.next, struct net_device, todo_list);
3147 list_del(&dev->todo_list);
3148
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003149 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 printk(KERN_ERR "network todo '%s' but state %d\n",
3151 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003152 dump_stack();
3153 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003155
3156 netdev_unregister_sysfs(dev);
3157 dev->reg_state = NETREG_UNREGISTERED;
3158
3159 netdev_wait_allrefs(dev);
3160
3161 /* paranoia */
3162 BUG_ON(atomic_read(&dev->refcnt));
3163 BUG_TRAP(!dev->ip_ptr);
3164 BUG_TRAP(!dev->ip6_ptr);
3165 BUG_TRAP(!dev->dn_ptr);
3166
3167 /* It must be the very last action,
3168 * after this 'dev' may point to freed up memory.
3169 */
3170 if (dev->destructor)
3171 dev->destructor(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
3173
3174out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003175 mutex_unlock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
3178/**
3179 * alloc_netdev - allocate network device
3180 * @sizeof_priv: size of private data to allocate space for
3181 * @name: device name format string
3182 * @setup: callback to initialize device
3183 *
3184 * Allocates a struct net_device with private data area for driver use
3185 * and performs basic initialization.
3186 */
3187struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3188 void (*setup)(struct net_device *))
3189{
3190 void *p;
3191 struct net_device *dev;
3192 int alloc_size;
3193
3194 /* ensure 32-byte alignment of both the device and private area */
3195 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3196 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3197
Paolo 'Blaisorblade' Giarrusso31380de2006-04-06 22:38:28 -07003198 p = kzalloc(alloc_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (!p) {
3200 printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
3201 return NULL;
3202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204 dev = (struct net_device *)
3205 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3206 dev->padded = (char *)dev - (char *)p;
3207
3208 if (sizeof_priv)
3209 dev->priv = netdev_priv(dev);
3210
3211 setup(dev);
3212 strcpy(dev->name, name);
3213 return dev;
3214}
3215EXPORT_SYMBOL(alloc_netdev);
3216
3217/**
3218 * free_netdev - free network device
3219 * @dev: device
3220 *
3221 * This function does the last stage of destroying an allocated device
3222 * interface. The reference to the device object is released.
3223 * If this is the last reference then it will be freed.
3224 */
3225void free_netdev(struct net_device *dev)
3226{
3227#ifdef CONFIG_SYSFS
Stephen Hemminger3041a062006-05-26 13:25:24 -07003228 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (dev->reg_state == NETREG_UNINITIALIZED) {
3230 kfree((char *)dev - dev->padded);
3231 return;
3232 }
3233
3234 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3235 dev->reg_state = NETREG_RELEASED;
3236
3237 /* will free via class release */
3238 class_device_put(&dev->class_dev);
3239#else
3240 kfree((char *)dev - dev->padded);
3241#endif
3242}
3243
3244/* Synchronize with packet receive processing. */
3245void synchronize_net(void)
3246{
3247 might_sleep();
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003248 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
3251/**
3252 * unregister_netdevice - remove device from the kernel
3253 * @dev: device
3254 *
3255 * This function shuts down a device interface and removes it
3256 * from the kernel tables. On success 0 is returned, on a failure
3257 * a negative errno code is returned.
3258 *
3259 * Callers must hold the rtnl semaphore. You may want
3260 * unregister_netdev() instead of this.
3261 */
3262
3263int unregister_netdevice(struct net_device *dev)
3264{
3265 struct net_device *d, **dp;
3266
3267 BUG_ON(dev_boot_phase);
3268 ASSERT_RTNL();
3269
3270 /* Some devices call without registering for initialization unwind. */
3271 if (dev->reg_state == NETREG_UNINITIALIZED) {
3272 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3273 "was registered\n", dev->name, dev);
3274 return -ENODEV;
3275 }
3276
3277 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3278
3279 /* If device is running, close it first. */
3280 if (dev->flags & IFF_UP)
3281 dev_close(dev);
3282
3283 /* And unlink it from device chain. */
3284 for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3285 if (d == dev) {
3286 write_lock_bh(&dev_base_lock);
3287 hlist_del(&dev->name_hlist);
3288 hlist_del(&dev->index_hlist);
3289 if (dev_tail == &dev->next)
3290 dev_tail = dp;
3291 *dp = d->next;
3292 write_unlock_bh(&dev_base_lock);
3293 break;
3294 }
3295 }
3296 if (!d) {
3297 printk(KERN_ERR "unregister net_device: '%s' not found\n",
3298 dev->name);
3299 return -ENODEV;
3300 }
3301
3302 dev->reg_state = NETREG_UNREGISTERING;
3303
3304 synchronize_net();
3305
3306 /* Shutdown queueing discipline. */
3307 dev_shutdown(dev);
3308
3309
3310 /* Notify protocols, that we are about to destroy
3311 this device. They should clean all the things.
3312 */
Alan Sternf07d5b92006-05-09 15:23:03 -07003313 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 /*
3316 * Flush the multicast chain
3317 */
3318 dev_mc_discard(dev);
3319
3320 if (dev->uninit)
3321 dev->uninit(dev);
3322
3323 /* Notifier chain MUST detach us from master device. */
3324 BUG_TRAP(!dev->master);
3325
3326 free_divert_blk(dev);
3327
3328 /* Finish processing unregister after unlock */
3329 net_set_todo(dev);
3330
3331 synchronize_net();
3332
3333 dev_put(dev);
3334 return 0;
3335}
3336
3337/**
3338 * unregister_netdev - remove device from the kernel
3339 * @dev: device
3340 *
3341 * This function shuts down a device interface and removes it
3342 * from the kernel tables. On success 0 is returned, on a failure
3343 * a negative errno code is returned.
3344 *
3345 * This is just a wrapper for unregister_netdevice that takes
3346 * the rtnl semaphore. In general you want to use this and not
3347 * unregister_netdevice.
3348 */
3349void unregister_netdev(struct net_device *dev)
3350{
3351 rtnl_lock();
3352 unregister_netdevice(dev);
3353 rtnl_unlock();
3354}
3355
3356EXPORT_SYMBOL(unregister_netdev);
3357
3358#ifdef CONFIG_HOTPLUG_CPU
3359static int dev_cpu_callback(struct notifier_block *nfb,
3360 unsigned long action,
3361 void *ocpu)
3362{
3363 struct sk_buff **list_skb;
3364 struct net_device **list_net;
3365 struct sk_buff *skb;
3366 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3367 struct softnet_data *sd, *oldsd;
3368
3369 if (action != CPU_DEAD)
3370 return NOTIFY_OK;
3371
3372 local_irq_disable();
3373 cpu = smp_processor_id();
3374 sd = &per_cpu(softnet_data, cpu);
3375 oldsd = &per_cpu(softnet_data, oldcpu);
3376
3377 /* Find end of our completion_queue. */
3378 list_skb = &sd->completion_queue;
3379 while (*list_skb)
3380 list_skb = &(*list_skb)->next;
3381 /* Append completion queue from offline CPU. */
3382 *list_skb = oldsd->completion_queue;
3383 oldsd->completion_queue = NULL;
3384
3385 /* Find end of our output_queue. */
3386 list_net = &sd->output_queue;
3387 while (*list_net)
3388 list_net = &(*list_net)->next_sched;
3389 /* Append output queue from offline CPU. */
3390 *list_net = oldsd->output_queue;
3391 oldsd->output_queue = NULL;
3392
3393 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3394 local_irq_enable();
3395
3396 /* Process offline CPU's input_pkt_queue */
3397 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3398 netif_rx(skb);
3399
3400 return NOTIFY_OK;
3401}
3402#endif /* CONFIG_HOTPLUG_CPU */
3403
Chris Leechdb217332006-06-17 21:24:58 -07003404#ifdef CONFIG_NET_DMA
3405/**
3406 * net_dma_rebalance -
3407 * This is called when the number of channels allocated to the net_dma_client
3408 * changes. The net_dma_client tries to have one DMA channel per CPU.
3409 */
3410static void net_dma_rebalance(void)
3411{
3412 unsigned int cpu, i, n;
3413 struct dma_chan *chan;
3414
Chris Leechdb217332006-06-17 21:24:58 -07003415 if (net_dma_count == 0) {
3416 for_each_online_cpu(cpu)
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07003417 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
Chris Leechdb217332006-06-17 21:24:58 -07003418 return;
3419 }
3420
3421 i = 0;
3422 cpu = first_cpu(cpu_online_map);
3423
3424 rcu_read_lock();
3425 list_for_each_entry(chan, &net_dma_client->channels, client_node) {
3426 n = ((num_online_cpus() / net_dma_count)
3427 + (i < (num_online_cpus() % net_dma_count) ? 1 : 0));
3428
3429 while(n) {
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07003430 per_cpu(softnet_data, cpu).net_dma = chan;
Chris Leechdb217332006-06-17 21:24:58 -07003431 cpu = next_cpu(cpu, cpu_online_map);
3432 n--;
3433 }
3434 i++;
3435 }
3436 rcu_read_unlock();
Chris Leechdb217332006-06-17 21:24:58 -07003437}
3438
3439/**
3440 * netdev_dma_event - event callback for the net_dma_client
3441 * @client: should always be net_dma_client
Randy Dunlapf4b8ea72006-06-22 16:00:11 -07003442 * @chan: DMA channel for the event
3443 * @event: event type
Chris Leechdb217332006-06-17 21:24:58 -07003444 */
3445static void netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3446 enum dma_event event)
3447{
3448 spin_lock(&net_dma_event_lock);
3449 switch (event) {
3450 case DMA_RESOURCE_ADDED:
3451 net_dma_count++;
3452 net_dma_rebalance();
3453 break;
3454 case DMA_RESOURCE_REMOVED:
3455 net_dma_count--;
3456 net_dma_rebalance();
3457 break;
3458 default:
3459 break;
3460 }
3461 spin_unlock(&net_dma_event_lock);
3462}
3463
3464/**
3465 * netdev_dma_regiser - register the networking subsystem as a DMA client
3466 */
3467static int __init netdev_dma_register(void)
3468{
3469 spin_lock_init(&net_dma_event_lock);
3470 net_dma_client = dma_async_client_register(netdev_dma_event);
3471 if (net_dma_client == NULL)
3472 return -ENOMEM;
3473
3474 dma_async_client_chan_request(net_dma_client, num_online_cpus());
3475 return 0;
3476}
3477
3478#else
3479static int __init netdev_dma_register(void) { return -ENODEV; }
3480#endif /* CONFIG_NET_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
3482/*
3483 * Initialize the DEV module. At boot time this walks the device list and
3484 * unhooks any devices that fail to initialise (normally hardware not
3485 * present) and leaves us with a valid list of present and active devices.
3486 *
3487 */
3488
3489/*
3490 * This is called single threaded during boot, so no need
3491 * to take the rtnl semaphore.
3492 */
3493static int __init net_dev_init(void)
3494{
3495 int i, rc = -ENOMEM;
3496
3497 BUG_ON(!dev_boot_phase);
3498
3499 net_random_init();
3500
3501 if (dev_proc_init())
3502 goto out;
3503
3504 if (netdev_sysfs_init())
3505 goto out;
3506
3507 INIT_LIST_HEAD(&ptype_all);
3508 for (i = 0; i < 16; i++)
3509 INIT_LIST_HEAD(&ptype_base[i]);
3510
3511 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3512 INIT_HLIST_HEAD(&dev_name_head[i]);
3513
3514 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3515 INIT_HLIST_HEAD(&dev_index_head[i]);
3516
3517 /*
3518 * Initialise the packet receive queues.
3519 */
3520
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07003521 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 struct softnet_data *queue;
3523
3524 queue = &per_cpu(softnet_data, i);
3525 skb_queue_head_init(&queue->input_pkt_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 queue->completion_queue = NULL;
3527 INIT_LIST_HEAD(&queue->poll_list);
3528 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3529 queue->backlog_dev.weight = weight_p;
3530 queue->backlog_dev.poll = process_backlog;
3531 atomic_set(&queue->backlog_dev.refcnt, 1);
3532 }
3533
Chris Leechdb217332006-06-17 21:24:58 -07003534 netdev_dma_register();
3535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 dev_boot_phase = 0;
3537
3538 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3539 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3540
3541 hotcpu_notifier(dev_cpu_callback, 0);
3542 dst_init();
3543 dev_mcast_init();
3544 rc = 0;
3545out:
3546 return rc;
3547}
3548
3549subsys_initcall(net_dev_init);
3550
3551EXPORT_SYMBOL(__dev_get_by_index);
3552EXPORT_SYMBOL(__dev_get_by_name);
3553EXPORT_SYMBOL(__dev_remove_pack);
Mitch Williamsc2373ee2005-11-09 10:34:45 -08003554EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555EXPORT_SYMBOL(dev_add_pack);
3556EXPORT_SYMBOL(dev_alloc_name);
3557EXPORT_SYMBOL(dev_close);
3558EXPORT_SYMBOL(dev_get_by_flags);
3559EXPORT_SYMBOL(dev_get_by_index);
3560EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561EXPORT_SYMBOL(dev_open);
3562EXPORT_SYMBOL(dev_queue_xmit);
3563EXPORT_SYMBOL(dev_remove_pack);
3564EXPORT_SYMBOL(dev_set_allmulti);
3565EXPORT_SYMBOL(dev_set_promiscuity);
3566EXPORT_SYMBOL(dev_change_flags);
3567EXPORT_SYMBOL(dev_set_mtu);
3568EXPORT_SYMBOL(dev_set_mac_address);
3569EXPORT_SYMBOL(free_netdev);
3570EXPORT_SYMBOL(netdev_boot_setup_check);
3571EXPORT_SYMBOL(netdev_set_master);
3572EXPORT_SYMBOL(netdev_state_change);
3573EXPORT_SYMBOL(netif_receive_skb);
3574EXPORT_SYMBOL(netif_rx);
3575EXPORT_SYMBOL(register_gifconf);
3576EXPORT_SYMBOL(register_netdevice);
3577EXPORT_SYMBOL(register_netdevice_notifier);
3578EXPORT_SYMBOL(skb_checksum_help);
3579EXPORT_SYMBOL(synchronize_net);
3580EXPORT_SYMBOL(unregister_netdevice);
3581EXPORT_SYMBOL(unregister_netdevice_notifier);
3582EXPORT_SYMBOL(net_enable_timestamp);
3583EXPORT_SYMBOL(net_disable_timestamp);
3584EXPORT_SYMBOL(dev_get_flags);
3585
3586#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3587EXPORT_SYMBOL(br_handle_frame_hook);
3588EXPORT_SYMBOL(br_fdb_get_hook);
3589EXPORT_SYMBOL(br_fdb_put_hook);
3590#endif
3591
3592#ifdef CONFIG_KMOD
3593EXPORT_SYMBOL(dev_load);
3594#endif
3595
3596EXPORT_PER_CPU_SYMBOL(softnet_data);