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