blob: a0a46e7ed1375b4296261d43722870640e5cd289 [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>
Johannes Berg295f4a12007-04-26 20:43:56 -0700112#include <net/wext.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>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700119#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
122 * The list of packet types we will receive (as opposed to discard)
123 * and the routines to invoke.
124 *
125 * Why 16. Because with 16 the only overlap we get on a hash of the
126 * low nibble of the protocol value is RARP/SNAP/X.25.
127 *
128 * NOTE: That is no longer true with the addition of VLAN tags. Not
129 * sure which should go first, but I bet it won't make much
130 * difference if we are running VLANs. The good news is that
131 * this protocol won't be in the list unless compiled in, so
Stephen Hemminger3041a062006-05-26 13:25:24 -0700132 * the average user (w/out VLANs) will not be adversely affected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * --BLG
134 *
135 * 0800 IP
136 * 8100 802.1Q VLAN
137 * 0001 802.3
138 * 0002 AX.25
139 * 0004 802.2
140 * 8035 RARP
141 * 0005 SNAP
142 * 0805 X.25
143 * 0806 ARP
144 * 8137 IPX
145 * 0009 Localtalk
146 * 86DD IPv6
147 */
148
149static DEFINE_SPINLOCK(ptype_lock);
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -0700150static struct list_head ptype_base[16] __read_mostly; /* 16 way hashed list */
151static struct list_head ptype_all __read_mostly; /* Taps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Chris Leechdb217332006-06-17 21:24:58 -0700153#ifdef CONFIG_NET_DMA
154static struct dma_client *net_dma_client;
155static unsigned int net_dma_count;
156static spinlock_t net_dma_event_lock;
157#endif
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700160 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * semaphore.
162 *
163 * Pure readers hold dev_base_lock for reading.
164 *
165 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700166 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * actual updates. This allows pure readers to access the list even
168 * while a writer is preparing to update it.
169 *
170 * To put it another way, dev_base_lock is held for writing only to
171 * protect against pure readers; the rtnl semaphore provides the
172 * protection against other writers.
173 *
174 * See, for example usages, register_netdevice() and
175 * unregister_netdevice(), which must be called with the rtnl
176 * semaphore held.
177 */
Pavel Emelianov7562f872007-05-03 15:13:45 -0700178LIST_HEAD(dev_base_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179DEFINE_RWLOCK(dev_base_lock);
180
Pavel Emelianov7562f872007-05-03 15:13:45 -0700181EXPORT_SYMBOL(dev_base_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182EXPORT_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
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700221#ifdef CONFIG_DEBUG_LOCK_ALLOC
222/*
223 * register_netdevice() inits dev->_xmit_lock and sets lockdep class
224 * according to dev->type
225 */
226static const unsigned short netdev_lock_type[] =
227 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
228 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
229 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
230 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
231 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
232 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
233 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
234 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
235 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
236 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
237 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
238 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
239 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
240 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
241 ARPHRD_NONE};
242
243static const char *netdev_lock_name[] =
244 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
245 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
246 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
247 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
248 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
249 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
250 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
251 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
252 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
253 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
254 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
255 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
256 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
257 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
258 "_xmit_NONE"};
259
260static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
261
262static inline unsigned short netdev_lock_pos(unsigned short dev_type)
263{
264 int i;
265
266 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
267 if (netdev_lock_type[i] == dev_type)
268 return i;
269 /* the last key is used by default */
270 return ARRAY_SIZE(netdev_lock_type) - 1;
271}
272
273static inline void netdev_set_lockdep_class(spinlock_t *lock,
274 unsigned short dev_type)
275{
276 int i;
277
278 i = netdev_lock_pos(dev_type);
279 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
280 netdev_lock_name[i]);
281}
282#else
283static inline void netdev_set_lockdep_class(spinlock_t *lock,
284 unsigned short dev_type)
285{
286}
287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/*******************************************************************************
290
291 Protocol management and registration routines
292
293*******************************************************************************/
294
295/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * Add a protocol ID to the list. Now that the input handler is
297 * smarter we can dispense with all the messy stuff that used to be
298 * here.
299 *
300 * BEWARE!!! Protocol handlers, mangling input packets,
301 * MUST BE last in hash buckets and checking protocol handlers
302 * MUST start from promiscuous ptype_all chain in net_bh.
303 * It is true now, do not change it.
304 * Explanation follows: if protocol handler, mangling packet, will
305 * be the first on list, it is not able to sense, that packet
306 * is cloned and should be copied-on-write, so that it will
307 * change it and subsequent readers will get broken packet.
308 * --ANK (980803)
309 */
310
311/**
312 * dev_add_pack - add packet handler
313 * @pt: packet type declaration
314 *
315 * Add a protocol handler to the networking stack. The passed &packet_type
316 * is linked into kernel lists and may not be freed until it has been
317 * removed from the kernel lists.
318 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900319 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * guarantee all CPU's that are in middle of receiving packets
321 * will see the new packet type (until the next received packet).
322 */
323
324void dev_add_pack(struct packet_type *pt)
325{
326 int hash;
327
328 spin_lock_bh(&ptype_lock);
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700329 if (pt->type == htons(ETH_P_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 list_add_rcu(&pt->list, &ptype_all);
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700331 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 hash = ntohs(pt->type) & 15;
333 list_add_rcu(&pt->list, &ptype_base[hash]);
334 }
335 spin_unlock_bh(&ptype_lock);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/**
339 * __dev_remove_pack - remove packet handler
340 * @pt: packet type declaration
341 *
342 * Remove a protocol handler that was previously added to the kernel
343 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
344 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900345 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
347 * The packet type might still be in use by receivers
348 * and must not be freed until after all the CPU's have gone
349 * through a quiescent state.
350 */
351void __dev_remove_pack(struct packet_type *pt)
352{
353 struct list_head *head;
354 struct packet_type *pt1;
355
356 spin_lock_bh(&ptype_lock);
357
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700358 if (pt->type == htons(ETH_P_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 head = &ptype_all;
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700360 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 head = &ptype_base[ntohs(pt->type) & 15];
362
363 list_for_each_entry(pt1, head, list) {
364 if (pt == pt1) {
365 list_del_rcu(&pt->list);
366 goto out;
367 }
368 }
369
370 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
371out:
372 spin_unlock_bh(&ptype_lock);
373}
374/**
375 * dev_remove_pack - remove packet handler
376 * @pt: packet type declaration
377 *
378 * Remove a protocol handler that was previously added to the kernel
379 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
380 * from the kernel lists and can be freed or reused once this function
381 * returns.
382 *
383 * This call sleeps to guarantee that no CPU is looking at the packet
384 * type after return.
385 */
386void dev_remove_pack(struct packet_type *pt)
387{
388 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 synchronize_net();
391}
392
393/******************************************************************************
394
395 Device Boot-time Settings Routines
396
397*******************************************************************************/
398
399/* Boot time configuration table */
400static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
401
402/**
403 * netdev_boot_setup_add - add new setup entry
404 * @name: name of the device
405 * @map: configured settings for the device
406 *
407 * Adds new setup entry to the dev_boot_setup list. The function
408 * returns 0 on error and 1 on success. This is a generic routine to
409 * all netdevices.
410 */
411static int netdev_boot_setup_add(char *name, struct ifmap *map)
412{
413 struct netdev_boot_setup *s;
414 int i;
415
416 s = dev_boot_setup;
417 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
418 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
419 memset(s[i].name, 0, sizeof(s[i].name));
420 strcpy(s[i].name, name);
421 memcpy(&s[i].map, map, sizeof(s[i].map));
422 break;
423 }
424 }
425
426 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
427}
428
429/**
430 * netdev_boot_setup_check - check boot time settings
431 * @dev: the netdevice
432 *
433 * Check boot time settings for the device.
434 * The found settings are set for the device to be used
435 * later in the device probing.
436 * Returns 0 if no settings found, 1 if they are.
437 */
438int netdev_boot_setup_check(struct net_device *dev)
439{
440 struct netdev_boot_setup *s = dev_boot_setup;
441 int i;
442
443 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
444 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
445 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
446 dev->irq = s[i].map.irq;
447 dev->base_addr = s[i].map.base_addr;
448 dev->mem_start = s[i].map.mem_start;
449 dev->mem_end = s[i].map.mem_end;
450 return 1;
451 }
452 }
453 return 0;
454}
455
456
457/**
458 * netdev_boot_base - get address from boot time settings
459 * @prefix: prefix for network device
460 * @unit: id for network device
461 *
462 * Check boot time settings for the base address of device.
463 * The found settings are set for the device to be used
464 * later in the device probing.
465 * Returns 0 if no settings found.
466 */
467unsigned long netdev_boot_base(const char *prefix, int unit)
468{
469 const struct netdev_boot_setup *s = dev_boot_setup;
470 char name[IFNAMSIZ];
471 int i;
472
473 sprintf(name, "%s%d", prefix, unit);
474
475 /*
476 * If device already registered then return base of 1
477 * to indicate not to probe for this interface
478 */
479 if (__dev_get_by_name(name))
480 return 1;
481
482 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
483 if (!strcmp(name, s[i].name))
484 return s[i].map.base_addr;
485 return 0;
486}
487
488/*
489 * Saves at boot time configured settings for any netdevice.
490 */
491int __init netdev_boot_setup(char *str)
492{
493 int ints[5];
494 struct ifmap map;
495
496 str = get_options(str, ARRAY_SIZE(ints), ints);
497 if (!str || !*str)
498 return 0;
499
500 /* Save settings */
501 memset(&map, 0, sizeof(map));
502 if (ints[0] > 0)
503 map.irq = ints[1];
504 if (ints[0] > 1)
505 map.base_addr = ints[2];
506 if (ints[0] > 2)
507 map.mem_start = ints[3];
508 if (ints[0] > 3)
509 map.mem_end = ints[4];
510
511 /* Add new entry to the list */
512 return netdev_boot_setup_add(str, &map);
513}
514
515__setup("netdev=", netdev_boot_setup);
516
517/*******************************************************************************
518
519 Device Interface Subroutines
520
521*******************************************************************************/
522
523/**
524 * __dev_get_by_name - find a device by its name
525 * @name: name to find
526 *
527 * Find an interface by name. Must be called under RTNL semaphore
528 * or @dev_base_lock. If the name is found a pointer to the device
529 * is returned. If the name is not found then %NULL is returned. The
530 * reference counters are not incremented so the caller must be
531 * careful with locks.
532 */
533
534struct net_device *__dev_get_by_name(const char *name)
535{
536 struct hlist_node *p;
537
538 hlist_for_each(p, dev_name_hash(name)) {
539 struct net_device *dev
540 = hlist_entry(p, struct net_device, name_hlist);
541 if (!strncmp(dev->name, name, IFNAMSIZ))
542 return dev;
543 }
544 return NULL;
545}
546
547/**
548 * dev_get_by_name - find a device by its name
549 * @name: name to find
550 *
551 * Find an interface by name. This can be called from any
552 * context and does its own locking. The returned handle has
553 * the usage count incremented and the caller must use dev_put() to
554 * release it when it is no longer needed. %NULL is returned if no
555 * matching device is found.
556 */
557
558struct net_device *dev_get_by_name(const char *name)
559{
560 struct net_device *dev;
561
562 read_lock(&dev_base_lock);
563 dev = __dev_get_by_name(name);
564 if (dev)
565 dev_hold(dev);
566 read_unlock(&dev_base_lock);
567 return dev;
568}
569
570/**
571 * __dev_get_by_index - find a device by its ifindex
572 * @ifindex: index of device
573 *
574 * Search for an interface by index. Returns %NULL if the device
575 * is not found or a pointer to the device. The device has not
576 * had its reference counter increased so the caller must be careful
577 * about locking. The caller must hold either the RTNL semaphore
578 * or @dev_base_lock.
579 */
580
581struct net_device *__dev_get_by_index(int ifindex)
582{
583 struct hlist_node *p;
584
585 hlist_for_each(p, dev_index_hash(ifindex)) {
586 struct net_device *dev
587 = hlist_entry(p, struct net_device, index_hlist);
588 if (dev->ifindex == ifindex)
589 return dev;
590 }
591 return NULL;
592}
593
594
595/**
596 * dev_get_by_index - find a device by its ifindex
597 * @ifindex: index of device
598 *
599 * Search for an interface by index. Returns NULL if the device
600 * is not found or a pointer to the device. The device returned has
601 * had a reference added and the pointer is safe until the user calls
602 * dev_put to indicate they have finished with it.
603 */
604
605struct net_device *dev_get_by_index(int ifindex)
606{
607 struct net_device *dev;
608
609 read_lock(&dev_base_lock);
610 dev = __dev_get_by_index(ifindex);
611 if (dev)
612 dev_hold(dev);
613 read_unlock(&dev_base_lock);
614 return dev;
615}
616
617/**
618 * dev_getbyhwaddr - find a device by its hardware address
619 * @type: media type of device
620 * @ha: hardware address
621 *
622 * Search for an interface by MAC address. Returns NULL if the device
623 * is not found or a pointer to the device. The caller must hold the
624 * rtnl semaphore. The returned device has not had its ref count increased
625 * and the caller must therefore be careful about locking
626 *
627 * BUGS:
628 * If the API was consistent this would be __dev_get_by_hwaddr
629 */
630
631struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
632{
633 struct net_device *dev;
634
635 ASSERT_RTNL();
636
Pavel Emelianov7562f872007-05-03 15:13:45 -0700637 for_each_netdev(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (dev->type == type &&
639 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700640 return dev;
641
642 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300645EXPORT_SYMBOL(dev_getbyhwaddr);
646
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700647struct net_device *__dev_getfirstbyhwtype(unsigned short type)
648{
649 struct net_device *dev;
650
651 ASSERT_RTNL();
Pavel Emelianov7562f872007-05-03 15:13:45 -0700652 for_each_netdev(dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700653 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700654 return dev;
655
656 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700657}
658
659EXPORT_SYMBOL(__dev_getfirstbyhwtype);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661struct net_device *dev_getfirstbyhwtype(unsigned short type)
662{
663 struct net_device *dev;
664
665 rtnl_lock();
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700666 dev = __dev_getfirstbyhwtype(type);
667 if (dev)
668 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 rtnl_unlock();
670 return dev;
671}
672
673EXPORT_SYMBOL(dev_getfirstbyhwtype);
674
675/**
676 * dev_get_by_flags - find any device with given flags
677 * @if_flags: IFF_* values
678 * @mask: bitmask of bits in if_flags to check
679 *
680 * Search for any interface with the given flags. Returns NULL if a device
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900681 * is not found or a pointer to the device. The device returned has
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * had a reference added and the pointer is safe until the user calls
683 * dev_put to indicate they have finished with it.
684 */
685
686struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
687{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700688 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Pavel Emelianov7562f872007-05-03 15:13:45 -0700690 ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700692 for_each_netdev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (((dev->flags ^ if_flags) & mask) == 0) {
694 dev_hold(dev);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700695 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697 }
698 }
699 read_unlock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703/**
704 * dev_valid_name - check if name is okay for network device
705 * @name: name string
706 *
707 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -0700708 * to allow sysfs to work. We also disallow any kind of
709 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
Mitch Williamsc2373ee2005-11-09 10:34:45 -0800711int dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
David S. Millerc7fa9d12006-08-15 16:34:13 -0700713 if (*name == '\0')
714 return 0;
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -0700715 if (strlen(name) >= IFNAMSIZ)
716 return 0;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700717 if (!strcmp(name, ".") || !strcmp(name, ".."))
718 return 0;
719
720 while (*name) {
721 if (*name == '/' || isspace(*name))
722 return 0;
723 name++;
724 }
725 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/**
729 * dev_alloc_name - allocate a name for a device
730 * @dev: device
731 * @name: name format string
732 *
733 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -0700734 * id. It scans list of devices to build up a free map, then chooses
735 * the first empty slot. The caller must hold the dev_base or rtnl lock
736 * while allocating the name and adding the device in order to avoid
737 * duplicates.
738 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
739 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
741
742int dev_alloc_name(struct net_device *dev, const char *name)
743{
744 int i = 0;
745 char buf[IFNAMSIZ];
746 const char *p;
747 const int max_netdevices = 8*PAGE_SIZE;
748 long *inuse;
749 struct net_device *d;
750
751 p = strnchr(name, IFNAMSIZ-1, '%');
752 if (p) {
753 /*
754 * Verify the string as this thing may have come from
755 * the user. There must be either one "%d" and no other "%"
756 * characters.
757 */
758 if (p[1] != 'd' || strchr(p + 2, '%'))
759 return -EINVAL;
760
761 /* Use one page as a bit array of possible slots */
762 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
763 if (!inuse)
764 return -ENOMEM;
765
Pavel Emelianov7562f872007-05-03 15:13:45 -0700766 for_each_netdev(d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (!sscanf(d->name, name, &i))
768 continue;
769 if (i < 0 || i >= max_netdevices)
770 continue;
771
772 /* avoid cases where sscanf is not exact inverse of printf */
773 snprintf(buf, sizeof(buf), name, i);
774 if (!strncmp(buf, d->name, IFNAMSIZ))
775 set_bit(i, inuse);
776 }
777
778 i = find_first_zero_bit(inuse, max_netdevices);
779 free_page((unsigned long) inuse);
780 }
781
782 snprintf(buf, sizeof(buf), name, i);
783 if (!__dev_get_by_name(buf)) {
784 strlcpy(dev->name, buf, IFNAMSIZ);
785 return i;
786 }
787
788 /* It is possible to run out of possible slots
789 * when the name is long and there isn't enough space left
790 * for the digits, or if all bits are used.
791 */
792 return -ENFILE;
793}
794
795
796/**
797 * dev_change_name - change name of a device
798 * @dev: device
799 * @newname: name (or format string) must be at least IFNAMSIZ
800 *
801 * Change name of a device, can pass format strings "eth%d".
802 * for wildcarding.
803 */
804int dev_change_name(struct net_device *dev, char *newname)
805{
806 int err = 0;
807
808 ASSERT_RTNL();
809
810 if (dev->flags & IFF_UP)
811 return -EBUSY;
812
813 if (!dev_valid_name(newname))
814 return -EINVAL;
815
816 if (strchr(newname, '%')) {
817 err = dev_alloc_name(dev, newname);
818 if (err < 0)
819 return err;
820 strcpy(newname, dev->name);
821 }
822 else if (__dev_get_by_name(newname))
823 return -EEXIST;
824 else
825 strlcpy(dev->name, newname, IFNAMSIZ);
826
Eric W. Biederman92749822007-04-03 00:07:30 -0600827 device_rename(&dev->dev, dev->name);
828 hlist_del(&dev->name_hlist);
829 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
830 raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return err;
833}
834
835/**
Stephen Hemminger3041a062006-05-26 13:25:24 -0700836 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700837 * @dev: device to cause notification
838 *
839 * Called to indicate a device has changed features.
840 */
841void netdev_features_change(struct net_device *dev)
842{
Alan Sternf07d5b92006-05-09 15:23:03 -0700843 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700844}
845EXPORT_SYMBOL(netdev_features_change);
846
847/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * netdev_state_change - device changes state
849 * @dev: device to cause notification
850 *
851 * Called to indicate a device has changed state. This function calls
852 * the notifier chains for netdev_chain and sends a NEWLINK message
853 * to the routing socket.
854 */
855void netdev_state_change(struct net_device *dev)
856{
857 if (dev->flags & IFF_UP) {
Alan Sternf07d5b92006-05-09 15:23:03 -0700858 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -0800859 NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
861 }
862}
863
864/**
865 * dev_load - load a network module
866 * @name: name of interface
867 *
868 * If a network interface is not present and the process has suitable
869 * privileges this function loads the module. If module loading is not
870 * available in this kernel then it becomes a nop.
871 */
872
873void dev_load(const char *name)
874{
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900875 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 read_lock(&dev_base_lock);
878 dev = __dev_get_by_name(name);
879 read_unlock(&dev_base_lock);
880
881 if (!dev && capable(CAP_SYS_MODULE))
882 request_module("%s", name);
883}
884
885static int default_rebuild_header(struct sk_buff *skb)
886{
887 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
888 skb->dev ? skb->dev->name : "NULL!!!");
889 kfree_skb(skb);
890 return 1;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/**
894 * dev_open - prepare an interface for use.
895 * @dev: device to open
896 *
897 * Takes a device from down to up state. The device's private open
898 * function is invoked and then the multicast lists are loaded. Finally
899 * the device is moved into the up state and a %NETDEV_UP message is
900 * sent to the netdev notifier chain.
901 *
902 * Calling this function on an active interface is a nop. On a failure
903 * a negative errno code is returned.
904 */
905int dev_open(struct net_device *dev)
906{
907 int ret = 0;
908
909 /*
910 * Is it already up?
911 */
912
913 if (dev->flags & IFF_UP)
914 return 0;
915
916 /*
917 * Is it even present?
918 */
919 if (!netif_device_present(dev))
920 return -ENODEV;
921
922 /*
923 * Call device private open method
924 */
925 set_bit(__LINK_STATE_START, &dev->state);
926 if (dev->open) {
927 ret = dev->open(dev);
928 if (ret)
929 clear_bit(__LINK_STATE_START, &dev->state);
930 }
931
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900932 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * If it went open OK then:
934 */
935
936 if (!ret) {
937 /*
938 * Set the flags.
939 */
940 dev->flags |= IFF_UP;
941
942 /*
943 * Initialize multicasting status
944 */
945 dev_mc_upload(dev);
946
947 /*
948 * Wakeup transmit queue engine
949 */
950 dev_activate(dev);
951
952 /*
953 * ... and announce new interface.
954 */
Alan Sternf07d5b92006-05-09 15:23:03 -0700955 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 return ret;
958}
959
960/**
961 * dev_close - shutdown an interface.
962 * @dev: device to shutdown
963 *
964 * This function moves an active device into down state. A
965 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
966 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
967 * chain.
968 */
969int dev_close(struct net_device *dev)
970{
971 if (!(dev->flags & IFF_UP))
972 return 0;
973
974 /*
975 * Tell people we are going down, so that they can
976 * prepare to death, when device is still operating.
977 */
Alan Sternf07d5b92006-05-09 15:23:03 -0700978 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 dev_deactivate(dev);
981
982 clear_bit(__LINK_STATE_START, &dev->state);
983
984 /* Synchronize to scheduled poll. We cannot touch poll list,
985 * it can be even on different cpu. So just clear netif_running(),
986 * and wait when poll really will happen. Actually, the best place
987 * for this is inside dev->stop() after device stopped its irq
988 * engine, but this requires more changes in devices. */
989
990 smp_mb__after_clear_bit(); /* Commit netif_running(). */
991 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
992 /* No hurry. */
David S. Miller6192b542005-07-28 12:12:58 -0700993 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995
996 /*
997 * Call the device specific close. This cannot fail.
998 * Only if device is UP
999 *
1000 * We allow it to be called even after a DETACH hot-plug
1001 * event.
1002 */
1003 if (dev->stop)
1004 dev->stop(dev);
1005
1006 /*
1007 * Device is now down.
1008 */
1009
1010 dev->flags &= ~IFF_UP;
1011
1012 /*
1013 * Tell people we are down
1014 */
Alan Sternf07d5b92006-05-09 15:23:03 -07001015 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 return 0;
1018}
1019
1020
1021/*
1022 * Device change register/unregister. These are not inline or static
1023 * as we export them to the world.
1024 */
1025
1026/**
1027 * register_netdevice_notifier - register a network notifier block
1028 * @nb: notifier
1029 *
1030 * Register a notifier to be called when network device events occur.
1031 * The notifier passed is linked into the kernel structures and must
1032 * not be reused until it has been unregistered. A negative errno code
1033 * is returned on a failure.
1034 *
1035 * When registered all registration and up events are replayed
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001036 * to the new notifier to allow device to have a race free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * view of the network device list.
1038 */
1039
1040int register_netdevice_notifier(struct notifier_block *nb)
1041{
1042 struct net_device *dev;
1043 int err;
1044
1045 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001046 err = raw_notifier_chain_register(&netdev_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (!err) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001048 for_each_netdev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 nb->notifier_call(nb, NETDEV_REGISTER, dev);
1050
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001051 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 nb->notifier_call(nb, NETDEV_UP, dev);
1053 }
1054 }
1055 rtnl_unlock();
1056 return err;
1057}
1058
1059/**
1060 * unregister_netdevice_notifier - unregister a network notifier block
1061 * @nb: notifier
1062 *
1063 * Unregister a notifier previously registered by
1064 * register_netdevice_notifier(). The notifier is unlinked into the
1065 * kernel structures and may then be reused. A negative errno code
1066 * is returned on a failure.
1067 */
1068
1069int unregister_netdevice_notifier(struct notifier_block *nb)
1070{
Herbert Xu9f514952006-03-25 01:24:25 -08001071 int err;
1072
1073 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001074 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xu9f514952006-03-25 01:24:25 -08001075 rtnl_unlock();
1076 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079/**
1080 * call_netdevice_notifiers - call all network notifier blocks
1081 * @val: value passed unmodified to notifier function
1082 * @v: pointer passed unmodified to notifier function
1083 *
1084 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001085 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
1087
1088int call_netdevice_notifiers(unsigned long val, void *v)
1089{
Alan Sternf07d5b92006-05-09 15:23:03 -07001090 return raw_notifier_call_chain(&netdev_chain, val, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093/* When > 0 there are consumers of rx skb time stamps */
1094static atomic_t netstamp_needed = ATOMIC_INIT(0);
1095
1096void net_enable_timestamp(void)
1097{
1098 atomic_inc(&netstamp_needed);
1099}
1100
1101void net_disable_timestamp(void)
1102{
1103 atomic_dec(&netstamp_needed);
1104}
1105
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001106static inline void net_timestamp(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 if (atomic_read(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001109 __net_timestamp(skb);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001110 else
1111 skb->tstamp.tv64 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114/*
1115 * Support routine. Sends outgoing frames to any network
1116 * taps currently in use.
1117 */
1118
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001119static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 struct packet_type *ptype;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001122
1123 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 rcu_read_lock();
1126 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1127 /* Never send packets back to the socket
1128 * they originated from - MvS (miquels@drinkel.ow.org)
1129 */
1130 if ((ptype->dev == dev || !ptype->dev) &&
1131 (ptype->af_packet_priv == NULL ||
1132 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1133 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1134 if (!skb2)
1135 break;
1136
1137 /* skb->nh should be correctly
1138 set by sender, so that the second statement is
1139 just protection against buggy protocols.
1140 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001141 skb_reset_mac_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001143 if (skb_network_header(skb2) < skb2->data ||
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001144 skb2->network_header > skb2->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (net_ratelimit())
1146 printk(KERN_CRIT "protocol %04x is "
1147 "buggy, dev %s\n",
1148 skb2->protocol, dev->name);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001149 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001152 skb2->transport_header = skb2->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 skb2->pkt_type = PACKET_OUTGOING;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001154 ptype->func(skb2, skb->dev, ptype, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 }
1157 rcu_read_unlock();
1158}
1159
Denis Vlasenko56079432006-03-29 15:57:29 -08001160
1161void __netif_schedule(struct net_device *dev)
1162{
1163 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1164 unsigned long flags;
1165 struct softnet_data *sd;
1166
1167 local_irq_save(flags);
1168 sd = &__get_cpu_var(softnet_data);
1169 dev->next_sched = sd->output_queue;
1170 sd->output_queue = dev;
1171 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1172 local_irq_restore(flags);
1173 }
1174}
1175EXPORT_SYMBOL(__netif_schedule);
1176
1177void __netif_rx_schedule(struct net_device *dev)
1178{
1179 unsigned long flags;
1180
1181 local_irq_save(flags);
1182 dev_hold(dev);
1183 list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
1184 if (dev->quota < 0)
1185 dev->quota += dev->weight;
1186 else
1187 dev->quota = dev->weight;
1188 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1189 local_irq_restore(flags);
1190}
1191EXPORT_SYMBOL(__netif_rx_schedule);
1192
1193void dev_kfree_skb_any(struct sk_buff *skb)
1194{
1195 if (in_irq() || irqs_disabled())
1196 dev_kfree_skb_irq(skb);
1197 else
1198 dev_kfree_skb(skb);
1199}
1200EXPORT_SYMBOL(dev_kfree_skb_any);
1201
1202
1203/* Hot-plugging. */
1204void netif_device_detach(struct net_device *dev)
1205{
1206 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1207 netif_running(dev)) {
1208 netif_stop_queue(dev);
1209 }
1210}
1211EXPORT_SYMBOL(netif_device_detach);
1212
1213void netif_device_attach(struct net_device *dev)
1214{
1215 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1216 netif_running(dev)) {
1217 netif_wake_queue(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001218 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08001219 }
1220}
1221EXPORT_SYMBOL(netif_device_attach);
1222
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/*
1225 * Invalidate hardware checksum when packet is to be mangled, and
1226 * complete checksum manually on outgoing path.
1227 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07001228int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Al Virod3bc23e2006-11-14 21:24:49 -08001230 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07001231 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Patrick McHardy84fa7932006-08-29 16:44:56 -07001233 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07001234 goto out_set_summed;
1235
1236 if (unlikely(skb_shinfo(skb)->gso_size)) {
Herbert Xua430a432006-07-08 13:34:56 -07001237 /* Let GSO fix up the checksum. */
1238 goto out_set_summed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
1241 if (skb_cloned(skb)) {
1242 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1243 if (ret)
1244 goto out;
1245 }
1246
Herbert Xu663ead32007-04-09 11:59:07 -07001247 offset = skb->csum_start - skb_headroom(skb);
Kris Katterjohn09a62662006-01-08 22:24:28 -08001248 BUG_ON(offset > (int)skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1250
Herbert Xu663ead32007-04-09 11:59:07 -07001251 offset = skb_headlen(skb) - offset;
Kris Katterjohn09a62662006-01-08 22:24:28 -08001252 BUG_ON(offset <= 0);
Al Viroff1dcad2006-11-20 18:07:29 -08001253 BUG_ON(skb->csum_offset + 2 > offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Herbert Xu663ead32007-04-09 11:59:07 -07001255 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) =
1256 csum_fold(csum);
Herbert Xua430a432006-07-08 13:34:56 -07001257out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001259out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return ret;
1261}
1262
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001263/**
1264 * skb_gso_segment - Perform segmentation on skb.
1265 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001266 * @features: features for the output path (see dev->features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001267 *
1268 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07001269 *
1270 * It may return NULL if the skb requires no segmentation. This is
1271 * only possible when GSO is used for verifying header integrity.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001272 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001273struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001274{
1275 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1276 struct packet_type *ptype;
Al Viro252e3342006-11-14 20:48:11 -08001277 __be16 type = skb->protocol;
Herbert Xua430a432006-07-08 13:34:56 -07001278 int err;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001279
1280 BUG_ON(skb_shinfo(skb)->frag_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001281
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001282 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001283 skb->mac_len = skb->network_header - skb->mac_header;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001284 __skb_pull(skb, skb->mac_len);
1285
Herbert Xuf9d106a2007-04-23 22:36:13 -07001286 if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001287 if (skb_header_cloned(skb) &&
1288 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1289 return ERR_PTR(err);
1290 }
1291
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001292 rcu_read_lock();
1293 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1294 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07001295 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001296 err = ptype->gso_send_check(skb);
1297 segs = ERR_PTR(err);
1298 if (err || skb_gso_ok(skb, features))
1299 break;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001300 __skb_push(skb, (skb->data -
1301 skb_network_header(skb)));
Herbert Xua430a432006-07-08 13:34:56 -07001302 }
Herbert Xu576a30e2006-06-27 13:22:38 -07001303 segs = ptype->gso_segment(skb, features);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001304 break;
1305 }
1306 }
1307 rcu_read_unlock();
1308
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001309 __skb_push(skb, skb->data - skb_mac_header(skb));
Herbert Xu576a30e2006-06-27 13:22:38 -07001310
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001311 return segs;
1312}
1313
1314EXPORT_SYMBOL(skb_gso_segment);
1315
Herbert Xufb286bb2005-11-10 13:01:24 -08001316/* Take action when hardware reception checksum errors are detected. */
1317#ifdef CONFIG_BUG
1318void netdev_rx_csum_fault(struct net_device *dev)
1319{
1320 if (net_ratelimit()) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001321 printk(KERN_ERR "%s: hw csum failure.\n",
Stephen Hemminger246a4212005-12-08 15:21:39 -08001322 dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08001323 dump_stack();
1324 }
1325}
1326EXPORT_SYMBOL(netdev_rx_csum_fault);
1327#endif
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/* Actually, we should eliminate this check as soon as we know, that:
1330 * 1. IOMMU is present and allows to map all the memory.
1331 * 2. No high memory really exists on this machine.
1332 */
1333
1334static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1335{
Herbert Xu3d3a8532006-06-27 13:33:10 -07001336#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 int i;
1338
1339 if (dev->features & NETIF_F_HIGHDMA)
1340 return 0;
1341
1342 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1343 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1344 return 1;
1345
Herbert Xu3d3a8532006-06-27 13:33:10 -07001346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 0;
1348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001350struct dev_gso_cb {
1351 void (*destructor)(struct sk_buff *skb);
1352};
1353
1354#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1355
1356static void dev_gso_skb_destructor(struct sk_buff *skb)
1357{
1358 struct dev_gso_cb *cb;
1359
1360 do {
1361 struct sk_buff *nskb = skb->next;
1362
1363 skb->next = nskb->next;
1364 nskb->next = NULL;
1365 kfree_skb(nskb);
1366 } while (skb->next);
1367
1368 cb = DEV_GSO_CB(skb);
1369 if (cb->destructor)
1370 cb->destructor(skb);
1371}
1372
1373/**
1374 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1375 * @skb: buffer to segment
1376 *
1377 * This function segments the given skb and stores the list of segments
1378 * in skb->next.
1379 */
1380static int dev_gso_segment(struct sk_buff *skb)
1381{
1382 struct net_device *dev = skb->dev;
1383 struct sk_buff *segs;
Herbert Xu576a30e2006-06-27 13:22:38 -07001384 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1385 NETIF_F_SG : 0);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001386
Herbert Xu576a30e2006-06-27 13:22:38 -07001387 segs = skb_gso_segment(skb, features);
1388
1389 /* Verifying header integrity only. */
1390 if (!segs)
1391 return 0;
1392
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001393 if (unlikely(IS_ERR(segs)))
1394 return PTR_ERR(segs);
1395
1396 skb->next = segs;
1397 DEV_GSO_CB(skb)->destructor = skb->destructor;
1398 skb->destructor = dev_gso_skb_destructor;
1399
1400 return 0;
1401}
1402
1403int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1404{
1405 if (likely(!skb->next)) {
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -07001406 if (!list_empty(&ptype_all))
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001407 dev_queue_xmit_nit(skb, dev);
1408
Herbert Xu576a30e2006-06-27 13:22:38 -07001409 if (netif_needs_gso(dev, skb)) {
1410 if (unlikely(dev_gso_segment(skb)))
1411 goto out_kfree_skb;
1412 if (skb->next)
1413 goto gso;
1414 }
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001415
Herbert Xu576a30e2006-06-27 13:22:38 -07001416 return dev->hard_start_xmit(skb, dev);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001417 }
1418
Herbert Xu576a30e2006-06-27 13:22:38 -07001419gso:
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001420 do {
1421 struct sk_buff *nskb = skb->next;
1422 int rc;
1423
1424 skb->next = nskb->next;
1425 nskb->next = NULL;
1426 rc = dev->hard_start_xmit(nskb, dev);
1427 if (unlikely(rc)) {
Michael Chanf54d9e82006-06-25 23:57:04 -07001428 nskb->next = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001429 skb->next = nskb;
1430 return rc;
1431 }
Michael Chanf54d9e82006-06-25 23:57:04 -07001432 if (unlikely(netif_queue_stopped(dev) && skb->next))
1433 return NETDEV_TX_BUSY;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001434 } while (skb->next);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001435
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001436 skb->destructor = DEV_GSO_CB(skb)->destructor;
1437
1438out_kfree_skb:
1439 kfree_skb(skb);
1440 return 0;
1441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443#define HARD_TX_LOCK(dev, cpu) { \
1444 if ((dev->features & NETIF_F_LLTX) == 0) { \
Herbert Xu932ff272006-06-09 12:20:56 -07001445 netif_tx_lock(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 } \
1447}
1448
1449#define HARD_TX_UNLOCK(dev) { \
1450 if ((dev->features & NETIF_F_LLTX) == 0) { \
Herbert Xu932ff272006-06-09 12:20:56 -07001451 netif_tx_unlock(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 } \
1453}
1454
1455/**
1456 * dev_queue_xmit - transmit a buffer
1457 * @skb: buffer to transmit
1458 *
1459 * Queue a buffer for transmission to a network device. The caller must
1460 * have set the device and priority and built the buffer before calling
1461 * this function. The function can be called from an interrupt.
1462 *
1463 * A negative errno code is returned on a failure. A success does not
1464 * guarantee the frame will be transmitted as it may be dropped due
1465 * to congestion or traffic shaping.
Ben Greearaf191362005-04-24 20:12:36 -07001466 *
1467 * -----------------------------------------------------------------------------------
1468 * I notice this method can also return errors from the queue disciplines,
1469 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1470 * be positive.
1471 *
1472 * Regardless of the return value, the skb is consumed, so it is currently
1473 * difficult to retry a send to this method. (You can bump the ref count
1474 * before sending to hold a reference for retry if you are careful.)
1475 *
1476 * When calling this method, interrupts MUST be enabled. This is because
1477 * the BH enable code must have IRQs enabled so that it will not deadlock.
1478 * --BLG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
1480
1481int dev_queue_xmit(struct sk_buff *skb)
1482{
1483 struct net_device *dev = skb->dev;
1484 struct Qdisc *q;
1485 int rc = -ENOMEM;
1486
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001487 /* GSO will handle the following emulations directly. */
1488 if (netif_needs_gso(dev, skb))
1489 goto gso;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (skb_shinfo(skb)->frag_list &&
1492 !(dev->features & NETIF_F_FRAGLIST) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001493 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 goto out_kfree_skb;
1495
1496 /* Fragmented skb is linearized if device does not support SG,
1497 * or if at least one of fragments is in highmem and device
1498 * does not support DMA from it.
1499 */
1500 if (skb_shinfo(skb)->nr_frags &&
1501 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001502 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 goto out_kfree_skb;
1504
1505 /* If packet is not checksummed and device does not support
1506 * checksumming for this protocol, complete checksumming here.
1507 */
Herbert Xu663ead32007-04-09 11:59:07 -07001508 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1509 skb_set_transport_header(skb, skb->csum_start -
1510 skb_headroom(skb));
1511
Stephen Hemmingerd212f872007-06-27 00:47:37 -07001512 if (!(dev->features & NETIF_F_GEN_CSUM)
1513 || ((dev->features & NETIF_F_IP_CSUM)
1514 && skb->protocol == htons(ETH_P_IP))
1515 || ((dev->features & NETIF_F_IPV6_CSUM)
1516 && skb->protocol == htons(ETH_P_IPV6)))
Herbert Xu663ead32007-04-09 11:59:07 -07001517 if (skb_checksum_help(skb))
1518 goto out_kfree_skb;
1519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001521gso:
Eric Dumazet2d7ceec2005-09-27 15:22:58 -07001522 spin_lock_prefetch(&dev->queue_lock);
1523
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001524 /* Disable soft irqs for various locks below. Also
1525 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001527 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001529 /* Updates of qdisc are serialized by queue_lock.
1530 * The struct Qdisc which is pointed to by qdisc is now a
1531 * rcu structure - it may be accessed without acquiring
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 * a lock (but the structure may be stale.) The freeing of the
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001533 * qdisc will be deferred until it's known that there are no
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * more references to it.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001535 *
1536 * If the qdisc has an enqueue function, we still need to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * hold the queue_lock before calling it, since queue_lock
1538 * also serializes access to the device queue.
1539 */
1540
1541 q = rcu_dereference(dev->qdisc);
1542#ifdef CONFIG_NET_CLS_ACT
1543 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1544#endif
1545 if (q->enqueue) {
1546 /* Grab device queue */
1547 spin_lock(&dev->queue_lock);
Patrick McHardy85670cc2006-09-27 16:45:45 -07001548 q = dev->qdisc;
1549 if (q->enqueue) {
1550 rc = q->enqueue(skb, q);
1551 qdisc_run(dev);
1552 spin_unlock(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Patrick McHardy85670cc2006-09-27 16:45:45 -07001554 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1555 goto out;
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 spin_unlock(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
1560 /* The device has no queue. Common case for software devices:
1561 loopback, all the sorts of tunnels...
1562
Herbert Xu932ff272006-06-09 12:20:56 -07001563 Really, it is unlikely that netif_tx_lock protection is necessary
1564 here. (f.e. loopback and IP tunnels are clean ignoring statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 counters.)
1566 However, it is possible, that they rely on protection
1567 made by us here.
1568
1569 Check this and shot the lock. It is not prone from deadlocks.
1570 Either shot noqueue qdisc, it is even simpler 8)
1571 */
1572 if (dev->flags & IFF_UP) {
1573 int cpu = smp_processor_id(); /* ok because BHs are off */
1574
1575 if (dev->xmit_lock_owner != cpu) {
1576
1577 HARD_TX_LOCK(dev, cpu);
1578
1579 if (!netif_queue_stopped(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 rc = 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001581 if (!dev_hard_start_xmit(skb, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 HARD_TX_UNLOCK(dev);
1583 goto out;
1584 }
1585 }
1586 HARD_TX_UNLOCK(dev);
1587 if (net_ratelimit())
1588 printk(KERN_CRIT "Virtual device %s asks to "
1589 "queue packet!\n", dev->name);
1590 } else {
1591 /* Recursion is detected! It is possible,
1592 * unfortunately */
1593 if (net_ratelimit())
1594 printk(KERN_CRIT "Dead loop on virtual device "
1595 "%s, fix it urgently!\n", dev->name);
1596 }
1597 }
1598
1599 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07001600 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602out_kfree_skb:
1603 kfree_skb(skb);
1604 return rc;
1605out:
Herbert Xud4828d82006-06-22 02:28:18 -07001606 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return rc;
1608}
1609
1610
1611/*=======================================================================
1612 Receiver routines
1613 =======================================================================*/
1614
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07001615int netdev_max_backlog __read_mostly = 1000;
1616int netdev_budget __read_mostly = 300;
1617int weight_p __read_mostly = 64; /* old backlog weight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1620
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/**
1623 * netif_rx - post buffer to the network code
1624 * @skb: buffer to post
1625 *
1626 * This function receives a packet from a device driver and queues it for
1627 * the upper (protocol) levels to process. It always succeeds. The buffer
1628 * may be dropped during processing for congestion control or by the
1629 * protocol layers.
1630 *
1631 * return values:
1632 * NET_RX_SUCCESS (no congestion)
1633 * NET_RX_CN_LOW (low congestion)
1634 * NET_RX_CN_MOD (moderate congestion)
1635 * NET_RX_CN_HIGH (high congestion)
1636 * NET_RX_DROP (packet was dropped)
1637 *
1638 */
1639
1640int netif_rx(struct sk_buff *skb)
1641{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct softnet_data *queue;
1643 unsigned long flags;
1644
1645 /* if netpoll wants it, pretend we never saw it */
1646 if (netpoll_rx(skb))
1647 return NET_RX_DROP;
1648
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001649 if (!skb->tstamp.tv64)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001650 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 /*
1653 * The code is rearranged so that the path is the most
1654 * short when CPU is congested, but is still operating.
1655 */
1656 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 queue = &__get_cpu_var(softnet_data);
1658
1659 __get_cpu_var(netdev_rx_stat).total++;
1660 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1661 if (queue->input_pkt_queue.qlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662enqueue:
1663 dev_hold(skb->dev);
1664 __skb_queue_tail(&queue->input_pkt_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 local_irq_restore(flags);
Stephen Hemminger34008d82005-06-23 20:10:00 -07001666 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 netif_rx_schedule(&queue->backlog_dev);
1670 goto enqueue;
1671 }
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 __get_cpu_var(netdev_rx_stat).dropped++;
1674 local_irq_restore(flags);
1675
1676 kfree_skb(skb);
1677 return NET_RX_DROP;
1678}
1679
1680int netif_rx_ni(struct sk_buff *skb)
1681{
1682 int err;
1683
1684 preempt_disable();
1685 err = netif_rx(skb);
1686 if (local_softirq_pending())
1687 do_softirq();
1688 preempt_enable();
1689
1690 return err;
1691}
1692
1693EXPORT_SYMBOL(netif_rx_ni);
1694
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001695static inline struct net_device *skb_bond(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct net_device *dev = skb->dev;
1698
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001699 if (dev->master) {
David S. Miller7ea49ed2006-08-14 17:08:36 -07001700 if (skb_bond_should_drop(skb)) {
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001701 kfree_skb(skb);
1702 return NULL;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 skb->dev = dev->master;
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001705 }
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001706
1707 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710static void net_tx_action(struct softirq_action *h)
1711{
1712 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1713
1714 if (sd->completion_queue) {
1715 struct sk_buff *clist;
1716
1717 local_irq_disable();
1718 clist = sd->completion_queue;
1719 sd->completion_queue = NULL;
1720 local_irq_enable();
1721
1722 while (clist) {
1723 struct sk_buff *skb = clist;
1724 clist = clist->next;
1725
1726 BUG_TRAP(!atomic_read(&skb->users));
1727 __kfree_skb(skb);
1728 }
1729 }
1730
1731 if (sd->output_queue) {
1732 struct net_device *head;
1733
1734 local_irq_disable();
1735 head = sd->output_queue;
1736 sd->output_queue = NULL;
1737 local_irq_enable();
1738
1739 while (head) {
1740 struct net_device *dev = head;
1741 head = head->next_sched;
1742
1743 smp_mb__before_clear_bit();
1744 clear_bit(__LINK_STATE_SCHED, &dev->state);
1745
1746 if (spin_trylock(&dev->queue_lock)) {
1747 qdisc_run(dev);
1748 spin_unlock(&dev->queue_lock);
1749 } else {
1750 netif_schedule(dev);
1751 }
1752 }
1753 }
1754}
1755
Stephen Hemminger6f05f622007-03-08 20:46:03 -08001756static inline int deliver_skb(struct sk_buff *skb,
1757 struct packet_type *pt_prev,
1758 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 atomic_inc(&skb->users);
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001761 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
1764#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
Stephen Hemminger6229e362007-03-21 13:38:47 -07001765/* These hooks defined here for ATM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766struct net_bridge;
1767struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1768 unsigned char *addr);
Stephen Hemminger6229e362007-03-21 13:38:47 -07001769void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Stephen Hemminger6229e362007-03-21 13:38:47 -07001771/*
1772 * If bridge module is loaded call bridging hook.
1773 * returns NULL if packet was consumed.
1774 */
1775struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
1776 struct sk_buff *skb) __read_mostly;
1777static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
1778 struct packet_type **pt_prev, int *ret,
1779 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 struct net_bridge_port *port;
1782
Stephen Hemminger6229e362007-03-21 13:38:47 -07001783 if (skb->pkt_type == PACKET_LOOPBACK ||
1784 (port = rcu_dereference(skb->dev->br_port)) == NULL)
1785 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 if (*pt_prev) {
Stephen Hemminger6229e362007-03-21 13:38:47 -07001788 *ret = deliver_skb(skb, *pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 *pt_prev = NULL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001790 }
1791
Stephen Hemminger6229e362007-03-21 13:38:47 -07001792 return br_handle_frame_hook(port, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794#else
Stephen Hemminger6229e362007-03-21 13:38:47 -07001795#define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796#endif
1797
1798#ifdef CONFIG_NET_CLS_ACT
1799/* TODO: Maybe we should just force sch_ingress to be compiled in
1800 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1801 * a compare and 2 stores extra right now if we dont have it on
1802 * but have CONFIG_NET_CLS_ACT
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001803 * NOTE: This doesnt stop any functionality; if you dont have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * the ingress scheduler, you just cant add policies on ingress.
1805 *
1806 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001807static int ing_filter(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct Qdisc *q;
1810 struct net_device *dev = skb->dev;
1811 int result = TC_ACT_OK;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (dev->qdisc_ingress) {
1814 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1815 if (MAX_RED_LOOP < ttl++) {
Patrick McHardyc01003c2007-03-29 11:46:52 -07001816 printk(KERN_WARNING "Redir loop detected Dropping packet (%d->%d)\n",
1817 skb->iif, skb->dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return TC_ACT_SHOT;
1819 }
1820
1821 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1822
1823 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
David S. Miller86e65da2005-08-09 19:36:29 -07001824
Patrick McHardyfd44de72007-04-16 17:07:08 -07001825 spin_lock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if ((q = dev->qdisc_ingress) != NULL)
1827 result = q->enqueue(skb, q);
Patrick McHardyfd44de72007-04-16 17:07:08 -07001828 spin_unlock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 }
1831
1832 return result;
1833}
1834#endif
1835
1836int netif_receive_skb(struct sk_buff *skb)
1837{
1838 struct packet_type *ptype, *pt_prev;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001839 struct net_device *orig_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 int ret = NET_RX_DROP;
Al Viro252e3342006-11-14 20:48:11 -08001841 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 /* if we've gotten here through NAPI, check netpoll */
1844 if (skb->dev->poll && netpoll_rx(skb))
1845 return NET_RX_DROP;
1846
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001847 if (!skb->tstamp.tv64)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001848 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Patrick McHardyc01003c2007-03-29 11:46:52 -07001850 if (!skb->iif)
1851 skb->iif = skb->dev->ifindex;
David S. Miller86e65da2005-08-09 19:36:29 -07001852
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001853 orig_dev = skb_bond(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001855 if (!orig_dev)
1856 return NET_RX_DROP;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 __get_cpu_var(netdev_rx_stat).total++;
1859
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001860 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001861 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001862 skb->mac_len = skb->network_header - skb->mac_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 pt_prev = NULL;
1865
1866 rcu_read_lock();
1867
1868#ifdef CONFIG_NET_CLS_ACT
1869 if (skb->tc_verd & TC_NCLS) {
1870 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1871 goto ncls;
1872 }
1873#endif
1874
1875 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1876 if (!ptype->dev || ptype->dev == skb->dev) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001877 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001878 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 pt_prev = ptype;
1880 }
1881 }
1882
1883#ifdef CONFIG_NET_CLS_ACT
1884 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001885 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 pt_prev = NULL; /* noone else should process this after*/
1887 } else {
1888 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1889 }
1890
1891 ret = ing_filter(skb);
1892
1893 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1894 kfree_skb(skb);
1895 goto out;
1896 }
1897
1898 skb->tc_verd = 0;
1899ncls:
1900#endif
1901
Stephen Hemminger6229e362007-03-21 13:38:47 -07001902 skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
1903 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto out;
1905
1906 type = skb->protocol;
1907 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1908 if (ptype->type == type &&
1909 (!ptype->dev || ptype->dev == skb->dev)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001910 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001911 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 pt_prev = ptype;
1913 }
1914 }
1915
1916 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001917 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 } else {
1919 kfree_skb(skb);
1920 /* Jamal, now you will not able to escape explaining
1921 * me how you were going to use this. :-)
1922 */
1923 ret = NET_RX_DROP;
1924 }
1925
1926out:
1927 rcu_read_unlock();
1928 return ret;
1929}
1930
1931static int process_backlog(struct net_device *backlog_dev, int *budget)
1932{
1933 int work = 0;
1934 int quota = min(backlog_dev->quota, *budget);
1935 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1936 unsigned long start_time = jiffies;
1937
Stephen Hemmingere3876602005-06-08 14:56:01 -07001938 backlog_dev->weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 for (;;) {
1940 struct sk_buff *skb;
1941 struct net_device *dev;
1942
1943 local_irq_disable();
1944 skb = __skb_dequeue(&queue->input_pkt_queue);
1945 if (!skb)
1946 goto job_done;
1947 local_irq_enable();
1948
1949 dev = skb->dev;
1950
1951 netif_receive_skb(skb);
1952
1953 dev_put(dev);
1954
1955 work++;
1956
1957 if (work >= quota || jiffies - start_time > 1)
1958 break;
1959
1960 }
1961
1962 backlog_dev->quota -= work;
1963 *budget -= work;
1964 return -1;
1965
1966job_done:
1967 backlog_dev->quota -= work;
1968 *budget -= work;
1969
1970 list_del(&backlog_dev->poll_list);
1971 smp_mb__before_clear_bit();
1972 netif_poll_enable(backlog_dev);
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 local_irq_enable();
1975 return 0;
1976}
1977
1978static void net_rx_action(struct softirq_action *h)
1979{
1980 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1981 unsigned long start_time = jiffies;
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07001982 int budget = netdev_budget;
Matt Mackall53fb95d2005-08-11 19:27:43 -07001983 void *have;
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 local_irq_disable();
1986
1987 while (!list_empty(&queue->poll_list)) {
1988 struct net_device *dev;
1989
1990 if (budget <= 0 || jiffies - start_time > 1)
1991 goto softnet_break;
1992
1993 local_irq_enable();
1994
1995 dev = list_entry(queue->poll_list.next,
1996 struct net_device, poll_list);
Matt Mackall53fb95d2005-08-11 19:27:43 -07001997 have = netpoll_poll_lock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
Matt Mackall53fb95d2005-08-11 19:27:43 -07002000 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 local_irq_disable();
Stephen Hemminger8aca8a22006-03-20 22:26:39 -08002002 list_move_tail(&dev->poll_list, &queue->poll_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (dev->quota < 0)
2004 dev->quota += dev->weight;
2005 else
2006 dev->quota = dev->weight;
2007 } else {
Matt Mackall53fb95d2005-08-11 19:27:43 -07002008 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 dev_put(dev);
2010 local_irq_disable();
2011 }
2012 }
2013out:
Shannon Nelson515e06c2007-06-23 23:09:23 -07002014 local_irq_enable();
Chris Leechdb217332006-06-17 21:24:58 -07002015#ifdef CONFIG_NET_DMA
2016 /*
2017 * There may not be any more sk_buffs coming right now, so push
2018 * any pending DMA copies to hardware
2019 */
2020 if (net_dma_client) {
2021 struct dma_chan *chan;
2022 rcu_read_lock();
2023 list_for_each_entry_rcu(chan, &net_dma_client->channels, client_node)
2024 dma_async_memcpy_issue_pending(chan);
2025 rcu_read_unlock();
2026 }
2027#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return;
2029
2030softnet_break:
2031 __get_cpu_var(netdev_rx_stat).time_squeeze++;
2032 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2033 goto out;
2034}
2035
2036static gifconf_func_t * gifconf_list [NPROTO];
2037
2038/**
2039 * register_gifconf - register a SIOCGIF handler
2040 * @family: Address family
2041 * @gifconf: Function handler
2042 *
2043 * Register protocol dependent address dumping routines. The handler
2044 * that is passed must not be freed or reused until it has been replaced
2045 * by another handler.
2046 */
2047int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2048{
2049 if (family >= NPROTO)
2050 return -EINVAL;
2051 gifconf_list[family] = gifconf;
2052 return 0;
2053}
2054
2055
2056/*
2057 * Map an interface index to its name (SIOCGIFNAME)
2058 */
2059
2060/*
2061 * We need this ioctl for efficient implementation of the
2062 * if_indextoname() function required by the IPv6 API. Without
2063 * it, we would have to search all the interfaces to find a
2064 * match. --pb
2065 */
2066
2067static int dev_ifname(struct ifreq __user *arg)
2068{
2069 struct net_device *dev;
2070 struct ifreq ifr;
2071
2072 /*
2073 * Fetch the caller's info block.
2074 */
2075
2076 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2077 return -EFAULT;
2078
2079 read_lock(&dev_base_lock);
2080 dev = __dev_get_by_index(ifr.ifr_ifindex);
2081 if (!dev) {
2082 read_unlock(&dev_base_lock);
2083 return -ENODEV;
2084 }
2085
2086 strcpy(ifr.ifr_name, dev->name);
2087 read_unlock(&dev_base_lock);
2088
2089 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2090 return -EFAULT;
2091 return 0;
2092}
2093
2094/*
2095 * Perform a SIOCGIFCONF call. This structure will change
2096 * size eventually, and there is nothing I can do about it.
2097 * Thus we will need a 'compatibility mode'.
2098 */
2099
2100static int dev_ifconf(char __user *arg)
2101{
2102 struct ifconf ifc;
2103 struct net_device *dev;
2104 char __user *pos;
2105 int len;
2106 int total;
2107 int i;
2108
2109 /*
2110 * Fetch the caller's info block.
2111 */
2112
2113 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2114 return -EFAULT;
2115
2116 pos = ifc.ifc_buf;
2117 len = ifc.ifc_len;
2118
2119 /*
2120 * Loop over the interfaces, and write an info block for each.
2121 */
2122
2123 total = 0;
Pavel Emelianov7562f872007-05-03 15:13:45 -07002124 for_each_netdev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 for (i = 0; i < NPROTO; i++) {
2126 if (gifconf_list[i]) {
2127 int done;
2128 if (!pos)
2129 done = gifconf_list[i](dev, NULL, 0);
2130 else
2131 done = gifconf_list[i](dev, pos + total,
2132 len - total);
2133 if (done < 0)
2134 return -EFAULT;
2135 total += done;
2136 }
2137 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /*
2141 * All done. Write the updated control block back to the caller.
2142 */
2143 ifc.ifc_len = total;
2144
2145 /*
2146 * Both BSD and Solaris return 0 here, so we do too.
2147 */
2148 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2149}
2150
2151#ifdef CONFIG_PROC_FS
2152/*
2153 * This is invoked by the /proc filesystem handler to display a device
2154 * in detail.
2155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2157{
Pavel Emelianov7562f872007-05-03 15:13:45 -07002158 loff_t off;
2159 struct net_device *dev;
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07002162 if (!*pos)
2163 return SEQ_START_TOKEN;
2164
2165 off = 1;
2166 for_each_netdev(dev)
2167 if (off++ == *pos)
2168 return dev;
2169
2170 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
2173void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2174{
2175 ++*pos;
Pavel Emelianov7562f872007-05-03 15:13:45 -07002176 return v == SEQ_START_TOKEN ?
2177 first_net_device() : next_net_device((struct net_device *)v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180void dev_seq_stop(struct seq_file *seq, void *v)
2181{
2182 read_unlock(&dev_base_lock);
2183}
2184
2185static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2186{
Rusty Russellc45d2862007-03-28 14:29:08 -07002187 struct net_device_stats *stats = dev->get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Rusty Russell5a1b5892007-04-28 21:04:03 -07002189 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2190 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2191 dev->name, stats->rx_bytes, stats->rx_packets,
2192 stats->rx_errors,
2193 stats->rx_dropped + stats->rx_missed_errors,
2194 stats->rx_fifo_errors,
2195 stats->rx_length_errors + stats->rx_over_errors +
2196 stats->rx_crc_errors + stats->rx_frame_errors,
2197 stats->rx_compressed, stats->multicast,
2198 stats->tx_bytes, stats->tx_packets,
2199 stats->tx_errors, stats->tx_dropped,
2200 stats->tx_fifo_errors, stats->collisions,
2201 stats->tx_carrier_errors +
2202 stats->tx_aborted_errors +
2203 stats->tx_window_errors +
2204 stats->tx_heartbeat_errors,
2205 stats->tx_compressed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
2208/*
2209 * Called from the PROCfs module. This now uses the new arbitrary sized
2210 * /proc/net interface to create /proc/net/dev
2211 */
2212static int dev_seq_show(struct seq_file *seq, void *v)
2213{
2214 if (v == SEQ_START_TOKEN)
2215 seq_puts(seq, "Inter-| Receive "
2216 " | Transmit\n"
2217 " face |bytes packets errs drop fifo frame "
2218 "compressed multicast|bytes packets errs "
2219 "drop fifo colls carrier compressed\n");
2220 else
2221 dev_seq_printf_stats(seq, v);
2222 return 0;
2223}
2224
2225static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2226{
2227 struct netif_rx_stats *rc = NULL;
2228
2229 while (*pos < NR_CPUS)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002230 if (cpu_online(*pos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 rc = &per_cpu(netdev_rx_stat, *pos);
2232 break;
2233 } else
2234 ++*pos;
2235 return rc;
2236}
2237
2238static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2239{
2240 return softnet_get_online(pos);
2241}
2242
2243static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2244{
2245 ++*pos;
2246 return softnet_get_online(pos);
2247}
2248
2249static void softnet_seq_stop(struct seq_file *seq, void *v)
2250{
2251}
2252
2253static int softnet_seq_show(struct seq_file *seq, void *v)
2254{
2255 struct netif_rx_stats *s = v;
2256
2257 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Stephen Hemminger31aa02c2005-06-23 20:12:48 -07002258 s->total, s->dropped, s->time_squeeze, 0,
Stephen Hemmingerc1ebcdb2005-06-23 20:08:59 -07002259 0, 0, 0, 0, /* was fastroute */
2260 s->cpu_collision );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return 0;
2262}
2263
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002264static const struct seq_operations dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 .start = dev_seq_start,
2266 .next = dev_seq_next,
2267 .stop = dev_seq_stop,
2268 .show = dev_seq_show,
2269};
2270
2271static int dev_seq_open(struct inode *inode, struct file *file)
2272{
2273 return seq_open(file, &dev_seq_ops);
2274}
2275
Arjan van de Ven9a321442007-02-12 00:55:35 -08002276static const struct file_operations dev_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 .owner = THIS_MODULE,
2278 .open = dev_seq_open,
2279 .read = seq_read,
2280 .llseek = seq_lseek,
2281 .release = seq_release,
2282};
2283
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002284static const struct seq_operations softnet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 .start = softnet_seq_start,
2286 .next = softnet_seq_next,
2287 .stop = softnet_seq_stop,
2288 .show = softnet_seq_show,
2289};
2290
2291static int softnet_seq_open(struct inode *inode, struct file *file)
2292{
2293 return seq_open(file, &softnet_seq_ops);
2294}
2295
Arjan van de Ven9a321442007-02-12 00:55:35 -08002296static const struct file_operations softnet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 .owner = THIS_MODULE,
2298 .open = softnet_seq_open,
2299 .read = seq_read,
2300 .llseek = seq_lseek,
2301 .release = seq_release,
2302};
2303
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002304static void *ptype_get_idx(loff_t pos)
2305{
2306 struct packet_type *pt = NULL;
2307 loff_t i = 0;
2308 int t;
2309
2310 list_for_each_entry_rcu(pt, &ptype_all, list) {
2311 if (i == pos)
2312 return pt;
2313 ++i;
2314 }
2315
2316 for (t = 0; t < 16; t++) {
2317 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
2318 if (i == pos)
2319 return pt;
2320 ++i;
2321 }
2322 }
2323 return NULL;
2324}
2325
2326static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
2327{
2328 rcu_read_lock();
2329 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
2330}
2331
2332static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2333{
2334 struct packet_type *pt;
2335 struct list_head *nxt;
2336 int hash;
2337
2338 ++*pos;
2339 if (v == SEQ_START_TOKEN)
2340 return ptype_get_idx(0);
2341
2342 pt = v;
2343 nxt = pt->list.next;
2344 if (pt->type == htons(ETH_P_ALL)) {
2345 if (nxt != &ptype_all)
2346 goto found;
2347 hash = 0;
2348 nxt = ptype_base[0].next;
2349 } else
2350 hash = ntohs(pt->type) & 15;
2351
2352 while (nxt == &ptype_base[hash]) {
2353 if (++hash >= 16)
2354 return NULL;
2355 nxt = ptype_base[hash].next;
2356 }
2357found:
2358 return list_entry(nxt, struct packet_type, list);
2359}
2360
2361static void ptype_seq_stop(struct seq_file *seq, void *v)
2362{
2363 rcu_read_unlock();
2364}
2365
2366static void ptype_seq_decode(struct seq_file *seq, void *sym)
2367{
2368#ifdef CONFIG_KALLSYMS
2369 unsigned long offset = 0, symsize;
2370 const char *symname;
2371 char *modname;
2372 char namebuf[128];
2373
2374 symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
2375 &modname, namebuf);
2376
2377 if (symname) {
2378 char *delim = ":";
2379
2380 if (!modname)
2381 modname = delim = "";
2382 seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
2383 symname, offset);
2384 return;
2385 }
2386#endif
2387
2388 seq_printf(seq, "[%p]", sym);
2389}
2390
2391static int ptype_seq_show(struct seq_file *seq, void *v)
2392{
2393 struct packet_type *pt = v;
2394
2395 if (v == SEQ_START_TOKEN)
2396 seq_puts(seq, "Type Device Function\n");
2397 else {
2398 if (pt->type == htons(ETH_P_ALL))
2399 seq_puts(seq, "ALL ");
2400 else
2401 seq_printf(seq, "%04x", ntohs(pt->type));
2402
2403 seq_printf(seq, " %-8s ",
2404 pt->dev ? pt->dev->name : "");
2405 ptype_seq_decode(seq, pt->func);
2406 seq_putc(seq, '\n');
2407 }
2408
2409 return 0;
2410}
2411
2412static const struct seq_operations ptype_seq_ops = {
2413 .start = ptype_seq_start,
2414 .next = ptype_seq_next,
2415 .stop = ptype_seq_stop,
2416 .show = ptype_seq_show,
2417};
2418
2419static int ptype_seq_open(struct inode *inode, struct file *file)
2420{
2421 return seq_open(file, &ptype_seq_ops);
2422}
2423
2424static const struct file_operations ptype_seq_fops = {
2425 .owner = THIS_MODULE,
2426 .open = ptype_seq_open,
2427 .read = seq_read,
2428 .llseek = seq_lseek,
2429 .release = seq_release,
2430};
2431
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433static int __init dev_proc_init(void)
2434{
2435 int rc = -ENOMEM;
2436
2437 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2438 goto out;
2439 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2440 goto out_dev;
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002441 if (!proc_net_fops_create("ptype", S_IRUGO, &ptype_seq_fops))
2442 goto out_dev2;
2443
Johannes Berg295f4a12007-04-26 20:43:56 -07002444 if (wext_proc_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 goto out_softnet;
2446 rc = 0;
2447out:
2448 return rc;
2449out_softnet:
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002450 proc_net_remove("ptype");
Josef 'Jeff' Sipek2396a222007-05-07 00:33:18 -07002451out_dev2:
2452 proc_net_remove("softnet_stat");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453out_dev:
2454 proc_net_remove("dev");
2455 goto out;
2456}
2457#else
2458#define dev_proc_init() 0
2459#endif /* CONFIG_PROC_FS */
2460
2461
2462/**
2463 * netdev_set_master - set up master/slave pair
2464 * @slave: slave device
2465 * @master: new master device
2466 *
2467 * Changes the master device of the slave. Pass %NULL to break the
2468 * bonding. The caller must hold the RTNL semaphore. On a failure
2469 * a negative errno code is returned. On success the reference counts
2470 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2471 * function returns zero.
2472 */
2473int netdev_set_master(struct net_device *slave, struct net_device *master)
2474{
2475 struct net_device *old = slave->master;
2476
2477 ASSERT_RTNL();
2478
2479 if (master) {
2480 if (old)
2481 return -EBUSY;
2482 dev_hold(master);
2483 }
2484
2485 slave->master = master;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 synchronize_net();
2488
2489 if (old)
2490 dev_put(old);
2491
2492 if (master)
2493 slave->flags |= IFF_SLAVE;
2494 else
2495 slave->flags &= ~IFF_SLAVE;
2496
2497 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2498 return 0;
2499}
2500
2501/**
2502 * dev_set_promiscuity - update promiscuity count on a device
2503 * @dev: device
2504 * @inc: modifier
2505 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07002506 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 * remains above zero the interface remains promiscuous. Once it hits zero
2508 * the device reverts back to normal filtering operation. A negative inc
2509 * value is used to drop promiscuity on the device.
2510 */
2511void dev_set_promiscuity(struct net_device *dev, int inc)
2512{
2513 unsigned short old_flags = dev->flags;
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if ((dev->promiscuity += inc) == 0)
2516 dev->flags &= ~IFF_PROMISC;
David Chau52609c02005-07-05 15:11:06 -07002517 else
2518 dev->flags |= IFF_PROMISC;
2519 if (dev->flags != old_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 dev_mc_upload(dev);
2521 printk(KERN_INFO "device %s %s promiscuous mode\n",
2522 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002523 "left");
Steve Grubb5bdb9882005-12-03 08:39:35 -05002524 audit_log(current->audit_context, GFP_ATOMIC,
2525 AUDIT_ANOM_PROMISCUOUS,
2526 "dev=%s prom=%d old_prom=%d auid=%u",
2527 dev->name, (dev->flags & IFF_PROMISC),
2528 (old_flags & IFF_PROMISC),
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002529 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531}
2532
2533/**
2534 * dev_set_allmulti - update allmulti count on a device
2535 * @dev: device
2536 * @inc: modifier
2537 *
2538 * Add or remove reception of all multicast frames to a device. While the
2539 * count in the device remains above zero the interface remains listening
2540 * to all interfaces. Once it hits zero the device reverts back to normal
2541 * filtering operation. A negative @inc value is used to drop the counter
2542 * when releasing a resource needing all multicasts.
2543 */
2544
2545void dev_set_allmulti(struct net_device *dev, int inc)
2546{
2547 unsigned short old_flags = dev->flags;
2548
2549 dev->flags |= IFF_ALLMULTI;
2550 if ((dev->allmulti += inc) == 0)
2551 dev->flags &= ~IFF_ALLMULTI;
2552 if (dev->flags ^ old_flags)
2553 dev_mc_upload(dev);
2554}
2555
2556unsigned dev_get_flags(const struct net_device *dev)
2557{
2558 unsigned flags;
2559
2560 flags = (dev->flags & ~(IFF_PROMISC |
2561 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08002562 IFF_RUNNING |
2563 IFF_LOWER_UP |
2564 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 (dev->gflags & (IFF_PROMISC |
2566 IFF_ALLMULTI));
2567
Stefan Rompfb00055a2006-03-20 17:09:11 -08002568 if (netif_running(dev)) {
2569 if (netif_oper_up(dev))
2570 flags |= IFF_RUNNING;
2571 if (netif_carrier_ok(dev))
2572 flags |= IFF_LOWER_UP;
2573 if (netif_dormant(dev))
2574 flags |= IFF_DORMANT;
2575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 return flags;
2578}
2579
2580int dev_change_flags(struct net_device *dev, unsigned flags)
2581{
Thomas Graf7c355f52007-06-05 16:03:03 -07002582 int ret, changes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 int old_flags = dev->flags;
2584
2585 /*
2586 * Set the flags on our device.
2587 */
2588
2589 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2590 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2591 IFF_AUTOMEDIA)) |
2592 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2593 IFF_ALLMULTI));
2594
2595 /*
2596 * Load in the correct multicast list now the flags have changed.
2597 */
2598
2599 dev_mc_upload(dev);
2600
2601 /*
2602 * Have we downed the interface. We handle IFF_UP ourselves
2603 * according to user attempts to set it, rather than blindly
2604 * setting it.
2605 */
2606
2607 ret = 0;
2608 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2609 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2610
2611 if (!ret)
2612 dev_mc_upload(dev);
2613 }
2614
2615 if (dev->flags & IFF_UP &&
2616 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2617 IFF_VOLATILE)))
Alan Sternf07d5b92006-05-09 15:23:03 -07002618 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002619 NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2622 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2623 dev->gflags ^= IFF_PROMISC;
2624 dev_set_promiscuity(dev, inc);
2625 }
2626
2627 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2628 is important. Some (broken) drivers set IFF_PROMISC, when
2629 IFF_ALLMULTI is requested not asking us and not reporting.
2630 */
2631 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2632 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2633 dev->gflags ^= IFF_ALLMULTI;
2634 dev_set_allmulti(dev, inc);
2635 }
2636
Thomas Graf7c355f52007-06-05 16:03:03 -07002637 /* Exclude state transition flags, already notified */
2638 changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
2639 if (changes)
2640 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 return ret;
2643}
2644
2645int dev_set_mtu(struct net_device *dev, int new_mtu)
2646{
2647 int err;
2648
2649 if (new_mtu == dev->mtu)
2650 return 0;
2651
2652 /* MTU must be positive. */
2653 if (new_mtu < 0)
2654 return -EINVAL;
2655
2656 if (!netif_device_present(dev))
2657 return -ENODEV;
2658
2659 err = 0;
2660 if (dev->change_mtu)
2661 err = dev->change_mtu(dev, new_mtu);
2662 else
2663 dev->mtu = new_mtu;
2664 if (!err && dev->flags & IFF_UP)
Alan Sternf07d5b92006-05-09 15:23:03 -07002665 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002666 NETDEV_CHANGEMTU, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return err;
2668}
2669
2670int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2671{
2672 int err;
2673
2674 if (!dev->set_mac_address)
2675 return -EOPNOTSUPP;
2676 if (sa->sa_family != dev->type)
2677 return -EINVAL;
2678 if (!netif_device_present(dev))
2679 return -ENODEV;
2680 err = dev->set_mac_address(dev, sa);
2681 if (!err)
Alan Sternf07d5b92006-05-09 15:23:03 -07002682 raw_notifier_call_chain(&netdev_chain,
Alan Sterne041c682006-03-27 01:16:30 -08002683 NETDEV_CHANGEADDR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return err;
2685}
2686
2687/*
2688 * Perform the SIOCxIFxxx calls.
2689 */
2690static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2691{
2692 int err;
2693 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2694
2695 if (!dev)
2696 return -ENODEV;
2697
2698 switch (cmd) {
2699 case SIOCGIFFLAGS: /* Get interface flags */
2700 ifr->ifr_flags = dev_get_flags(dev);
2701 return 0;
2702
2703 case SIOCSIFFLAGS: /* Set interface flags */
2704 return dev_change_flags(dev, ifr->ifr_flags);
2705
2706 case SIOCGIFMETRIC: /* Get the metric on the interface
2707 (currently unused) */
2708 ifr->ifr_metric = 0;
2709 return 0;
2710
2711 case SIOCSIFMETRIC: /* Set the metric on the interface
2712 (currently unused) */
2713 return -EOPNOTSUPP;
2714
2715 case SIOCGIFMTU: /* Get the MTU of a device */
2716 ifr->ifr_mtu = dev->mtu;
2717 return 0;
2718
2719 case SIOCSIFMTU: /* Set the MTU of a device */
2720 return dev_set_mtu(dev, ifr->ifr_mtu);
2721
2722 case SIOCGIFHWADDR:
2723 if (!dev->addr_len)
2724 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2725 else
2726 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2727 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2728 ifr->ifr_hwaddr.sa_family = dev->type;
2729 return 0;
2730
2731 case SIOCSIFHWADDR:
2732 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2733
2734 case SIOCSIFHWBROADCAST:
2735 if (ifr->ifr_hwaddr.sa_family != dev->type)
2736 return -EINVAL;
2737 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2738 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
Alan Sternf07d5b92006-05-09 15:23:03 -07002739 raw_notifier_call_chain(&netdev_chain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 NETDEV_CHANGEADDR, dev);
2741 return 0;
2742
2743 case SIOCGIFMAP:
2744 ifr->ifr_map.mem_start = dev->mem_start;
2745 ifr->ifr_map.mem_end = dev->mem_end;
2746 ifr->ifr_map.base_addr = dev->base_addr;
2747 ifr->ifr_map.irq = dev->irq;
2748 ifr->ifr_map.dma = dev->dma;
2749 ifr->ifr_map.port = dev->if_port;
2750 return 0;
2751
2752 case SIOCSIFMAP:
2753 if (dev->set_config) {
2754 if (!netif_device_present(dev))
2755 return -ENODEV;
2756 return dev->set_config(dev, &ifr->ifr_map);
2757 }
2758 return -EOPNOTSUPP;
2759
2760 case SIOCADDMULTI:
2761 if (!dev->set_multicast_list ||
2762 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2763 return -EINVAL;
2764 if (!netif_device_present(dev))
2765 return -ENODEV;
2766 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2767 dev->addr_len, 1);
2768
2769 case SIOCDELMULTI:
2770 if (!dev->set_multicast_list ||
2771 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2772 return -EINVAL;
2773 if (!netif_device_present(dev))
2774 return -ENODEV;
2775 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2776 dev->addr_len, 1);
2777
2778 case SIOCGIFINDEX:
2779 ifr->ifr_ifindex = dev->ifindex;
2780 return 0;
2781
2782 case SIOCGIFTXQLEN:
2783 ifr->ifr_qlen = dev->tx_queue_len;
2784 return 0;
2785
2786 case SIOCSIFTXQLEN:
2787 if (ifr->ifr_qlen < 0)
2788 return -EINVAL;
2789 dev->tx_queue_len = ifr->ifr_qlen;
2790 return 0;
2791
2792 case SIOCSIFNAME:
2793 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2794 return dev_change_name(dev, ifr->ifr_newname);
2795
2796 /*
2797 * Unknown or private ioctl
2798 */
2799
2800 default:
2801 if ((cmd >= SIOCDEVPRIVATE &&
2802 cmd <= SIOCDEVPRIVATE + 15) ||
2803 cmd == SIOCBONDENSLAVE ||
2804 cmd == SIOCBONDRELEASE ||
2805 cmd == SIOCBONDSETHWADDR ||
2806 cmd == SIOCBONDSLAVEINFOQUERY ||
2807 cmd == SIOCBONDINFOQUERY ||
2808 cmd == SIOCBONDCHANGEACTIVE ||
2809 cmd == SIOCGMIIPHY ||
2810 cmd == SIOCGMIIREG ||
2811 cmd == SIOCSMIIREG ||
2812 cmd == SIOCBRADDIF ||
2813 cmd == SIOCBRDELIF ||
2814 cmd == SIOCWANDEV) {
2815 err = -EOPNOTSUPP;
2816 if (dev->do_ioctl) {
2817 if (netif_device_present(dev))
2818 err = dev->do_ioctl(dev, ifr,
2819 cmd);
2820 else
2821 err = -ENODEV;
2822 }
2823 } else
2824 err = -EINVAL;
2825
2826 }
2827 return err;
2828}
2829
2830/*
2831 * This function handles all "interface"-type I/O control requests. The actual
2832 * 'doing' part of this is dev_ifsioc above.
2833 */
2834
2835/**
2836 * dev_ioctl - network device ioctl
2837 * @cmd: command to issue
2838 * @arg: pointer to a struct ifreq in user space
2839 *
2840 * Issue ioctl functions to devices. This is normally called by the
2841 * user space syscall interfaces but can sometimes be useful for
2842 * other purposes. The return value is the return from the syscall if
2843 * positive or a negative errno code on error.
2844 */
2845
2846int dev_ioctl(unsigned int cmd, void __user *arg)
2847{
2848 struct ifreq ifr;
2849 int ret;
2850 char *colon;
2851
2852 /* One special case: SIOCGIFCONF takes ifconf argument
2853 and requires shared lock, because it sleeps writing
2854 to user space.
2855 */
2856
2857 if (cmd == SIOCGIFCONF) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002858 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 ret = dev_ifconf((char __user *) arg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002860 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return ret;
2862 }
2863 if (cmd == SIOCGIFNAME)
2864 return dev_ifname((struct ifreq __user *)arg);
2865
2866 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2867 return -EFAULT;
2868
2869 ifr.ifr_name[IFNAMSIZ-1] = 0;
2870
2871 colon = strchr(ifr.ifr_name, ':');
2872 if (colon)
2873 *colon = 0;
2874
2875 /*
2876 * See which interface the caller is talking about.
2877 */
2878
2879 switch (cmd) {
2880 /*
2881 * These ioctl calls:
2882 * - can be done by all.
2883 * - atomic and do not require locking.
2884 * - return a value
2885 */
2886 case SIOCGIFFLAGS:
2887 case SIOCGIFMETRIC:
2888 case SIOCGIFMTU:
2889 case SIOCGIFHWADDR:
2890 case SIOCGIFSLAVE:
2891 case SIOCGIFMAP:
2892 case SIOCGIFINDEX:
2893 case SIOCGIFTXQLEN:
2894 dev_load(ifr.ifr_name);
2895 read_lock(&dev_base_lock);
2896 ret = dev_ifsioc(&ifr, cmd);
2897 read_unlock(&dev_base_lock);
2898 if (!ret) {
2899 if (colon)
2900 *colon = ':';
2901 if (copy_to_user(arg, &ifr,
2902 sizeof(struct ifreq)))
2903 ret = -EFAULT;
2904 }
2905 return ret;
2906
2907 case SIOCETHTOOL:
2908 dev_load(ifr.ifr_name);
2909 rtnl_lock();
2910 ret = dev_ethtool(&ifr);
2911 rtnl_unlock();
2912 if (!ret) {
2913 if (colon)
2914 *colon = ':';
2915 if (copy_to_user(arg, &ifr,
2916 sizeof(struct ifreq)))
2917 ret = -EFAULT;
2918 }
2919 return ret;
2920
2921 /*
2922 * These ioctl calls:
2923 * - require superuser power.
2924 * - require strict serialization.
2925 * - return a value
2926 */
2927 case SIOCGMIIPHY:
2928 case SIOCGMIIREG:
2929 case SIOCSIFNAME:
2930 if (!capable(CAP_NET_ADMIN))
2931 return -EPERM;
2932 dev_load(ifr.ifr_name);
2933 rtnl_lock();
2934 ret = dev_ifsioc(&ifr, cmd);
2935 rtnl_unlock();
2936 if (!ret) {
2937 if (colon)
2938 *colon = ':';
2939 if (copy_to_user(arg, &ifr,
2940 sizeof(struct ifreq)))
2941 ret = -EFAULT;
2942 }
2943 return ret;
2944
2945 /*
2946 * These ioctl calls:
2947 * - require superuser power.
2948 * - require strict serialization.
2949 * - do not return a value
2950 */
2951 case SIOCSIFFLAGS:
2952 case SIOCSIFMETRIC:
2953 case SIOCSIFMTU:
2954 case SIOCSIFMAP:
2955 case SIOCSIFHWADDR:
2956 case SIOCSIFSLAVE:
2957 case SIOCADDMULTI:
2958 case SIOCDELMULTI:
2959 case SIOCSIFHWBROADCAST:
2960 case SIOCSIFTXQLEN:
2961 case SIOCSMIIREG:
2962 case SIOCBONDENSLAVE:
2963 case SIOCBONDRELEASE:
2964 case SIOCBONDSETHWADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 case SIOCBONDCHANGEACTIVE:
2966 case SIOCBRADDIF:
2967 case SIOCBRDELIF:
2968 if (!capable(CAP_NET_ADMIN))
2969 return -EPERM;
Thomas Grafcabcac02006-01-24 12:46:33 -08002970 /* fall through */
2971 case SIOCBONDSLAVEINFOQUERY:
2972 case SIOCBONDINFOQUERY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 dev_load(ifr.ifr_name);
2974 rtnl_lock();
2975 ret = dev_ifsioc(&ifr, cmd);
2976 rtnl_unlock();
2977 return ret;
2978
2979 case SIOCGIFMEM:
2980 /* Get the per device memory space. We can add this but
2981 * currently do not support it */
2982 case SIOCSIFMEM:
2983 /* Set the per device memory buffer space.
2984 * Not applicable in our case */
2985 case SIOCSIFLINK:
2986 return -EINVAL;
2987
2988 /*
2989 * Unknown or private ioctl.
2990 */
2991 default:
2992 if (cmd == SIOCWANDEV ||
2993 (cmd >= SIOCDEVPRIVATE &&
2994 cmd <= SIOCDEVPRIVATE + 15)) {
2995 dev_load(ifr.ifr_name);
2996 rtnl_lock();
2997 ret = dev_ifsioc(&ifr, cmd);
2998 rtnl_unlock();
2999 if (!ret && copy_to_user(arg, &ifr,
3000 sizeof(struct ifreq)))
3001 ret = -EFAULT;
3002 return ret;
3003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 /* Take care of Wireless Extensions */
Johannes Berg295f4a12007-04-26 20:43:56 -07003005 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
3006 return wext_handle_ioctl(&ifr, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return -EINVAL;
3008 }
3009}
3010
3011
3012/**
3013 * dev_new_index - allocate an ifindex
3014 *
3015 * Returns a suitable unique value for a new device interface
3016 * number. The caller must hold the rtnl semaphore or the
3017 * dev_base_lock to be sure it remains unique.
3018 */
3019static int dev_new_index(void)
3020{
3021 static int ifindex;
3022 for (;;) {
3023 if (++ifindex <= 0)
3024 ifindex = 1;
3025 if (!__dev_get_by_index(ifindex))
3026 return ifindex;
3027 }
3028}
3029
3030static int dev_boot_phase = 1;
3031
3032/* Delayed registration/unregisteration */
3033static DEFINE_SPINLOCK(net_todo_list_lock);
3034static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
3035
Stephen Hemminger6f05f622007-03-08 20:46:03 -08003036static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 spin_lock(&net_todo_list_lock);
3039 list_add_tail(&dev->todo_list, &net_todo_list);
3040 spin_unlock(&net_todo_list_lock);
3041}
3042
3043/**
3044 * register_netdevice - register a network device
3045 * @dev: device to register
3046 *
3047 * Take a completed network device structure and add it to the kernel
3048 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3049 * chain. 0 is returned on success. A negative errno code is returned
3050 * on a failure to set up the device, or if the name is a duplicate.
3051 *
3052 * Callers must hold the rtnl semaphore. You may want
3053 * register_netdev() instead of this.
3054 *
3055 * BUGS:
3056 * The locking appears insufficient to guarantee two parallel registers
3057 * will not get the same name.
3058 */
3059
3060int register_netdevice(struct net_device *dev)
3061{
3062 struct hlist_head *head;
3063 struct hlist_node *p;
3064 int ret;
3065
3066 BUG_ON(dev_boot_phase);
3067 ASSERT_RTNL();
3068
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003069 might_sleep();
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 /* When net_device's are persistent, this will be fatal. */
3072 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
3073
3074 spin_lock_init(&dev->queue_lock);
Herbert Xu932ff272006-06-09 12:20:56 -07003075 spin_lock_init(&dev->_xmit_lock);
Jarek Poplawski723e98b2007-05-15 22:46:18 -07003076 netdev_set_lockdep_class(&dev->_xmit_lock, dev->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 dev->xmit_lock_owner = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 spin_lock_init(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 dev->iflink = -1;
3081
3082 /* Init, if this function is available */
3083 if (dev->init) {
3084 ret = dev->init(dev);
3085 if (ret) {
3086 if (ret > 0)
3087 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08003088 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 if (!dev_valid_name(dev->name)) {
3093 ret = -EINVAL;
Adrian Bunk90833aa2006-11-13 16:02:22 -08003094 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096
3097 dev->ifindex = dev_new_index();
3098 if (dev->iflink == -1)
3099 dev->iflink = dev->ifindex;
3100
3101 /* Check for existence of name */
3102 head = dev_name_hash(dev->name);
3103 hlist_for_each(p, head) {
3104 struct net_device *d
3105 = hlist_entry(p, struct net_device, name_hlist);
3106 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3107 ret = -EEXIST;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Stephen Hemmingerd212f872007-06-27 00:47:37 -07003112 /* Fix illegal checksum combinations */
3113 if ((dev->features & NETIF_F_HW_CSUM) &&
3114 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3115 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
3116 dev->name);
3117 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
3118 }
3119
3120 if ((dev->features & NETIF_F_NO_CSUM) &&
3121 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3122 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
3123 dev->name);
3124 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
3125 }
3126
3127
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 /* Fix illegal SG+CSUM combinations. */
3129 if ((dev->features & NETIF_F_SG) &&
Herbert Xu8648b302006-06-17 22:06:05 -07003130 !(dev->features & NETIF_F_ALL_CSUM)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07003131 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 dev->name);
3133 dev->features &= ~NETIF_F_SG;
3134 }
3135
3136 /* TSO requires that SG is present as well. */
3137 if ((dev->features & NETIF_F_TSO) &&
3138 !(dev->features & NETIF_F_SG)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07003139 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 dev->name);
3141 dev->features &= ~NETIF_F_TSO;
3142 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003143 if (dev->features & NETIF_F_UFO) {
3144 if (!(dev->features & NETIF_F_HW_CSUM)) {
3145 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3146 "NETIF_F_HW_CSUM feature.\n",
3147 dev->name);
3148 dev->features &= ~NETIF_F_UFO;
3149 }
3150 if (!(dev->features & NETIF_F_SG)) {
3151 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3152 "NETIF_F_SG feature.\n",
3153 dev->name);
3154 dev->features &= ~NETIF_F_UFO;
3155 }
3156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158 /*
3159 * nil rebuild_header routine,
3160 * that should be never called and used as just bug trap.
3161 */
3162
3163 if (!dev->rebuild_header)
3164 dev->rebuild_header = default_rebuild_header;
3165
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003166 ret = netdev_register_sysfs(dev);
3167 if (ret)
Adrian Bunk90833aa2006-11-13 16:02:22 -08003168 goto out;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003169 dev->reg_state = NETREG_REGISTERED;
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 /*
3172 * Default initial state at registry is that the
3173 * device is present.
3174 */
3175
3176 set_bit(__LINK_STATE_PRESENT, &dev->state);
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 dev_init_scheduler(dev);
3179 write_lock_bh(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07003180 list_add_tail(&dev->dev_list, &dev_base_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 hlist_add_head(&dev->name_hlist, head);
3182 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
3183 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 write_unlock_bh(&dev_base_lock);
3185
3186 /* Notify protocols, that a new device appeared. */
Alan Sternf07d5b92006-05-09 15:23:03 -07003187 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 ret = 0;
3190
3191out:
3192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
3195/**
3196 * register_netdev - register a network device
3197 * @dev: device to register
3198 *
3199 * Take a completed network device structure and add it to the kernel
3200 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3201 * chain. 0 is returned on success. A negative errno code is returned
3202 * on a failure to set up the device, or if the name is a duplicate.
3203 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07003204 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 * and expands the device name if you passed a format string to
3206 * alloc_netdev.
3207 */
3208int register_netdev(struct net_device *dev)
3209{
3210 int err;
3211
3212 rtnl_lock();
3213
3214 /*
3215 * If the name is a format string the caller wants us to do a
3216 * name allocation.
3217 */
3218 if (strchr(dev->name, '%')) {
3219 err = dev_alloc_name(dev, dev->name);
3220 if (err < 0)
3221 goto out;
3222 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 err = register_netdevice(dev);
3225out:
3226 rtnl_unlock();
3227 return err;
3228}
3229EXPORT_SYMBOL(register_netdev);
3230
3231/*
3232 * netdev_wait_allrefs - wait until all references are gone.
3233 *
3234 * This is called when unregistering network devices.
3235 *
3236 * Any protocol or device that holds a reference should register
3237 * for netdevice notification, and cleanup and put back the
3238 * reference if they receive an UNREGISTER event.
3239 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003240 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 */
3242static void netdev_wait_allrefs(struct net_device *dev)
3243{
3244 unsigned long rebroadcast_time, warning_time;
3245
3246 rebroadcast_time = warning_time = jiffies;
3247 while (atomic_read(&dev->refcnt) != 0) {
3248 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003249 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 /* Rebroadcast unregister notification */
Alan Sternf07d5b92006-05-09 15:23:03 -07003252 raw_notifier_call_chain(&netdev_chain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 NETDEV_UNREGISTER, dev);
3254
3255 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3256 &dev->state)) {
3257 /* We must not have linkwatch events
3258 * pending on unregister. If this
3259 * happens, we simply run the queue
3260 * unscheduled, resulting in a noop
3261 * for this device.
3262 */
3263 linkwatch_run_queue();
3264 }
3265
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003266 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 rebroadcast_time = jiffies;
3269 }
3270
3271 msleep(250);
3272
3273 if (time_after(jiffies, warning_time + 10 * HZ)) {
3274 printk(KERN_EMERG "unregister_netdevice: "
3275 "waiting for %s to become free. Usage "
3276 "count = %d\n",
3277 dev->name, atomic_read(&dev->refcnt));
3278 warning_time = jiffies;
3279 }
3280 }
3281}
3282
3283/* The sequence is:
3284 *
3285 * rtnl_lock();
3286 * ...
3287 * register_netdevice(x1);
3288 * register_netdevice(x2);
3289 * ...
3290 * unregister_netdevice(y1);
3291 * unregister_netdevice(y2);
3292 * ...
3293 * rtnl_unlock();
3294 * free_netdev(y1);
3295 * free_netdev(y2);
3296 *
3297 * We are invoked by rtnl_unlock() after it drops the semaphore.
3298 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003299 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 * without deadlocking with linkwatch via keventd.
3301 * 2) Since we run with the RTNL semaphore not held, we can sleep
3302 * safely in order to wait for the netdev refcnt to drop to zero.
3303 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003304static DEFINE_MUTEX(net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305void netdev_run_todo(void)
3306{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003307 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 /* Need to guard against multiple cpu's getting out of order. */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003310 mutex_lock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
3312 /* Not safe to do outside the semaphore. We must not return
3313 * until all unregister events invoked by the local processor
3314 * have been completed (either by this todo run, or one on
3315 * another cpu).
3316 */
3317 if (list_empty(&net_todo_list))
3318 goto out;
3319
3320 /* Snapshot list, allow later requests */
3321 spin_lock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003322 list_replace_init(&net_todo_list, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 spin_unlock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 while (!list_empty(&list)) {
3326 struct net_device *dev
3327 = list_entry(list.next, struct net_device, todo_list);
3328 list_del(&dev->todo_list);
3329
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003330 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 printk(KERN_ERR "network todo '%s' but state %d\n",
3332 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003333 dump_stack();
3334 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003336
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003337 dev->reg_state = NETREG_UNREGISTERED;
3338
3339 netdev_wait_allrefs(dev);
3340
3341 /* paranoia */
3342 BUG_ON(atomic_read(&dev->refcnt));
3343 BUG_TRAP(!dev->ip_ptr);
3344 BUG_TRAP(!dev->ip6_ptr);
3345 BUG_TRAP(!dev->dn_ptr);
3346
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003347 if (dev->destructor)
3348 dev->destructor(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07003349
3350 /* Free network device */
3351 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 }
3353
3354out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003355 mutex_unlock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
3357
Rusty Russell5a1b5892007-04-28 21:04:03 -07003358static struct net_device_stats *internal_stats(struct net_device *dev)
Rusty Russellc45d2862007-03-28 14:29:08 -07003359{
Rusty Russell5a1b5892007-04-28 21:04:03 -07003360 return &dev->stats;
Rusty Russellc45d2862007-03-28 14:29:08 -07003361}
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363/**
3364 * alloc_netdev - allocate network device
3365 * @sizeof_priv: size of private data to allocate space for
3366 * @name: device name format string
3367 * @setup: callback to initialize device
3368 *
3369 * Allocates a struct net_device with private data area for driver use
3370 * and performs basic initialization.
3371 */
3372struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3373 void (*setup)(struct net_device *))
3374{
3375 void *p;
3376 struct net_device *dev;
3377 int alloc_size;
3378
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07003379 BUG_ON(strlen(name) >= sizeof(dev->name));
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /* ensure 32-byte alignment of both the device and private area */
3382 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3383 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3384
Paolo 'Blaisorblade' Giarrusso31380de2006-04-06 22:38:28 -07003385 p = kzalloc(alloc_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (!p) {
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07003387 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 return NULL;
3389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
3391 dev = (struct net_device *)
3392 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3393 dev->padded = (char *)dev - (char *)p;
3394
3395 if (sizeof_priv)
3396 dev->priv = netdev_priv(dev);
3397
Rusty Russell5a1b5892007-04-28 21:04:03 -07003398 dev->get_stats = internal_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 setup(dev);
3400 strcpy(dev->name, name);
3401 return dev;
3402}
3403EXPORT_SYMBOL(alloc_netdev);
3404
3405/**
3406 * free_netdev - free network device
3407 * @dev: device
3408 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003409 * This function does the last stage of destroying an allocated device
3410 * interface. The reference to the device object is released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 * If this is the last reference then it will be freed.
3412 */
3413void free_netdev(struct net_device *dev)
3414{
3415#ifdef CONFIG_SYSFS
Stephen Hemminger3041a062006-05-26 13:25:24 -07003416 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 if (dev->reg_state == NETREG_UNINITIALIZED) {
3418 kfree((char *)dev - dev->padded);
3419 return;
3420 }
3421
3422 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3423 dev->reg_state = NETREG_RELEASED;
3424
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07003425 /* will free via device release */
3426 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427#else
3428 kfree((char *)dev - dev->padded);
3429#endif
3430}
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432/* Synchronize with packet receive processing. */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003433void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
3435 might_sleep();
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003436 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437}
3438
3439/**
3440 * unregister_netdevice - remove device from the kernel
3441 * @dev: device
3442 *
3443 * This function shuts down a device interface and removes it
3444 * from the kernel tables. On success 0 is returned, on a failure
3445 * a negative errno code is returned.
3446 *
3447 * Callers must hold the rtnl semaphore. You may want
3448 * unregister_netdev() instead of this.
3449 */
3450
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08003451void unregister_netdevice(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 BUG_ON(dev_boot_phase);
3454 ASSERT_RTNL();
3455
3456 /* Some devices call without registering for initialization unwind. */
3457 if (dev->reg_state == NETREG_UNINITIALIZED) {
3458 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3459 "was registered\n", dev->name, dev);
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08003460
3461 WARN_ON(1);
3462 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 }
3464
3465 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3466
3467 /* If device is running, close it first. */
3468 if (dev->flags & IFF_UP)
3469 dev_close(dev);
3470
3471 /* And unlink it from device chain. */
Pavel Emelianov7562f872007-05-03 15:13:45 -07003472 write_lock_bh(&dev_base_lock);
3473 list_del(&dev->dev_list);
3474 hlist_del(&dev->name_hlist);
3475 hlist_del(&dev->index_hlist);
3476 write_unlock_bh(&dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
3478 dev->reg_state = NETREG_UNREGISTERING;
3479
3480 synchronize_net();
3481
3482 /* Shutdown queueing discipline. */
3483 dev_shutdown(dev);
3484
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 /* Notify protocols, that we are about to destroy
3487 this device. They should clean all the things.
3488 */
Alan Sternf07d5b92006-05-09 15:23:03 -07003489 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 /*
3492 * Flush the multicast chain
3493 */
3494 dev_mc_discard(dev);
3495
3496 if (dev->uninit)
3497 dev->uninit(dev);
3498
3499 /* Notifier chain MUST detach us from master device. */
3500 BUG_TRAP(!dev->master);
3501
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07003502 /* Remove entries from sysfs */
3503 netdev_unregister_sysfs(dev);
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 /* Finish processing unregister after unlock */
3506 net_set_todo(dev);
3507
3508 synchronize_net();
3509
3510 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
3513/**
3514 * unregister_netdev - remove device from the kernel
3515 * @dev: device
3516 *
3517 * This function shuts down a device interface and removes it
3518 * from the kernel tables. On success 0 is returned, on a failure
3519 * a negative errno code is returned.
3520 *
3521 * This is just a wrapper for unregister_netdevice that takes
3522 * the rtnl semaphore. In general you want to use this and not
3523 * unregister_netdevice.
3524 */
3525void unregister_netdev(struct net_device *dev)
3526{
3527 rtnl_lock();
3528 unregister_netdevice(dev);
3529 rtnl_unlock();
3530}
3531
3532EXPORT_SYMBOL(unregister_netdev);
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534static int dev_cpu_callback(struct notifier_block *nfb,
3535 unsigned long action,
3536 void *ocpu)
3537{
3538 struct sk_buff **list_skb;
3539 struct net_device **list_net;
3540 struct sk_buff *skb;
3541 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3542 struct softnet_data *sd, *oldsd;
3543
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003544 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return NOTIFY_OK;
3546
3547 local_irq_disable();
3548 cpu = smp_processor_id();
3549 sd = &per_cpu(softnet_data, cpu);
3550 oldsd = &per_cpu(softnet_data, oldcpu);
3551
3552 /* Find end of our completion_queue. */
3553 list_skb = &sd->completion_queue;
3554 while (*list_skb)
3555 list_skb = &(*list_skb)->next;
3556 /* Append completion queue from offline CPU. */
3557 *list_skb = oldsd->completion_queue;
3558 oldsd->completion_queue = NULL;
3559
3560 /* Find end of our output_queue. */
3561 list_net = &sd->output_queue;
3562 while (*list_net)
3563 list_net = &(*list_net)->next_sched;
3564 /* Append output queue from offline CPU. */
3565 *list_net = oldsd->output_queue;
3566 oldsd->output_queue = NULL;
3567
3568 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3569 local_irq_enable();
3570
3571 /* Process offline CPU's input_pkt_queue */
3572 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3573 netif_rx(skb);
3574
3575 return NOTIFY_OK;
3576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Chris Leechdb217332006-06-17 21:24:58 -07003578#ifdef CONFIG_NET_DMA
3579/**
3580 * net_dma_rebalance -
3581 * This is called when the number of channels allocated to the net_dma_client
3582 * changes. The net_dma_client tries to have one DMA channel per CPU.
3583 */
3584static void net_dma_rebalance(void)
3585{
3586 unsigned int cpu, i, n;
3587 struct dma_chan *chan;
3588
Chris Leechdb217332006-06-17 21:24:58 -07003589 if (net_dma_count == 0) {
3590 for_each_online_cpu(cpu)
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07003591 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
Chris Leechdb217332006-06-17 21:24:58 -07003592 return;
3593 }
3594
3595 i = 0;
3596 cpu = first_cpu(cpu_online_map);
3597
3598 rcu_read_lock();
3599 list_for_each_entry(chan, &net_dma_client->channels, client_node) {
3600 n = ((num_online_cpus() / net_dma_count)
3601 + (i < (num_online_cpus() % net_dma_count) ? 1 : 0));
3602
3603 while(n) {
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07003604 per_cpu(softnet_data, cpu).net_dma = chan;
Chris Leechdb217332006-06-17 21:24:58 -07003605 cpu = next_cpu(cpu, cpu_online_map);
3606 n--;
3607 }
3608 i++;
3609 }
3610 rcu_read_unlock();
Chris Leechdb217332006-06-17 21:24:58 -07003611}
3612
3613/**
3614 * netdev_dma_event - event callback for the net_dma_client
3615 * @client: should always be net_dma_client
Randy Dunlapf4b8ea72006-06-22 16:00:11 -07003616 * @chan: DMA channel for the event
3617 * @event: event type
Chris Leechdb217332006-06-17 21:24:58 -07003618 */
3619static void netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3620 enum dma_event event)
3621{
3622 spin_lock(&net_dma_event_lock);
3623 switch (event) {
3624 case DMA_RESOURCE_ADDED:
3625 net_dma_count++;
3626 net_dma_rebalance();
3627 break;
3628 case DMA_RESOURCE_REMOVED:
3629 net_dma_count--;
3630 net_dma_rebalance();
3631 break;
3632 default:
3633 break;
3634 }
3635 spin_unlock(&net_dma_event_lock);
3636}
3637
3638/**
3639 * netdev_dma_regiser - register the networking subsystem as a DMA client
3640 */
3641static int __init netdev_dma_register(void)
3642{
3643 spin_lock_init(&net_dma_event_lock);
3644 net_dma_client = dma_async_client_register(netdev_dma_event);
3645 if (net_dma_client == NULL)
3646 return -ENOMEM;
3647
3648 dma_async_client_chan_request(net_dma_client, num_online_cpus());
3649 return 0;
3650}
3651
3652#else
3653static int __init netdev_dma_register(void) { return -ENODEV; }
3654#endif /* CONFIG_NET_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656/*
3657 * Initialize the DEV module. At boot time this walks the device list and
3658 * unhooks any devices that fail to initialise (normally hardware not
3659 * present) and leaves us with a valid list of present and active devices.
3660 *
3661 */
3662
3663/*
3664 * This is called single threaded during boot, so no need
3665 * to take the rtnl semaphore.
3666 */
3667static int __init net_dev_init(void)
3668{
3669 int i, rc = -ENOMEM;
3670
3671 BUG_ON(!dev_boot_phase);
3672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 if (dev_proc_init())
3674 goto out;
3675
3676 if (netdev_sysfs_init())
3677 goto out;
3678
3679 INIT_LIST_HEAD(&ptype_all);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003680 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 INIT_LIST_HEAD(&ptype_base[i]);
3682
3683 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3684 INIT_HLIST_HEAD(&dev_name_head[i]);
3685
3686 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3687 INIT_HLIST_HEAD(&dev_index_head[i]);
3688
3689 /*
3690 * Initialise the packet receive queues.
3691 */
3692
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07003693 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 struct softnet_data *queue;
3695
3696 queue = &per_cpu(softnet_data, i);
3697 skb_queue_head_init(&queue->input_pkt_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 queue->completion_queue = NULL;
3699 INIT_LIST_HEAD(&queue->poll_list);
3700 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3701 queue->backlog_dev.weight = weight_p;
3702 queue->backlog_dev.poll = process_backlog;
3703 atomic_set(&queue->backlog_dev.refcnt, 1);
3704 }
3705
Chris Leechdb217332006-06-17 21:24:58 -07003706 netdev_dma_register();
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 dev_boot_phase = 0;
3709
3710 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3711 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3712
3713 hotcpu_notifier(dev_cpu_callback, 0);
3714 dst_init();
3715 dev_mcast_init();
3716 rc = 0;
3717out:
3718 return rc;
3719}
3720
3721subsys_initcall(net_dev_init);
3722
3723EXPORT_SYMBOL(__dev_get_by_index);
3724EXPORT_SYMBOL(__dev_get_by_name);
3725EXPORT_SYMBOL(__dev_remove_pack);
Mitch Williamsc2373ee2005-11-09 10:34:45 -08003726EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727EXPORT_SYMBOL(dev_add_pack);
3728EXPORT_SYMBOL(dev_alloc_name);
3729EXPORT_SYMBOL(dev_close);
3730EXPORT_SYMBOL(dev_get_by_flags);
3731EXPORT_SYMBOL(dev_get_by_index);
3732EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733EXPORT_SYMBOL(dev_open);
3734EXPORT_SYMBOL(dev_queue_xmit);
3735EXPORT_SYMBOL(dev_remove_pack);
3736EXPORT_SYMBOL(dev_set_allmulti);
3737EXPORT_SYMBOL(dev_set_promiscuity);
3738EXPORT_SYMBOL(dev_change_flags);
3739EXPORT_SYMBOL(dev_set_mtu);
3740EXPORT_SYMBOL(dev_set_mac_address);
3741EXPORT_SYMBOL(free_netdev);
3742EXPORT_SYMBOL(netdev_boot_setup_check);
3743EXPORT_SYMBOL(netdev_set_master);
3744EXPORT_SYMBOL(netdev_state_change);
3745EXPORT_SYMBOL(netif_receive_skb);
3746EXPORT_SYMBOL(netif_rx);
3747EXPORT_SYMBOL(register_gifconf);
3748EXPORT_SYMBOL(register_netdevice);
3749EXPORT_SYMBOL(register_netdevice_notifier);
3750EXPORT_SYMBOL(skb_checksum_help);
3751EXPORT_SYMBOL(synchronize_net);
3752EXPORT_SYMBOL(unregister_netdevice);
3753EXPORT_SYMBOL(unregister_netdevice_notifier);
3754EXPORT_SYMBOL(net_enable_timestamp);
3755EXPORT_SYMBOL(net_disable_timestamp);
3756EXPORT_SYMBOL(dev_get_flags);
3757
3758#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3759EXPORT_SYMBOL(br_handle_frame_hook);
3760EXPORT_SYMBOL(br_fdb_get_hook);
3761EXPORT_SYMBOL(br_fdb_put_hook);
3762#endif
3763
3764#ifdef CONFIG_KMOD
3765EXPORT_SYMBOL(dev_load);
3766#endif
3767
3768EXPORT_PER_CPU_SYMBOL(softnet_data);