]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/dev.c
net: Fix build failure with 'make mandocs'.
[net-next-2.6.git] / net / core / dev.c
CommitLineData
1da177e4
LT
1/*
2 * NET3 Protocol independent device support routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the non IP parts of dev.c 1.0.19
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dahinds@users.sourceforge.net>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
21 *
22 * Changes:
23 * D.J. Barrow : Fixed bug where dev->refcnt gets set
24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
27 * Alan Cox : device private ioctl copies fields back.
28 * Alan Cox : Transmit queue code does relevant
29 * stunts to keep the queue safe.
30 * Alan Cox : Fixed double lock.
31 * Alan Cox : Fixed promisc NULL pointer trap
32 * ???????? : Support the full private ioctl range
33 * Alan Cox : Moved ioctl permission check into
34 * drivers
35 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
36 * Alan Cox : 100 backlog just doesn't cut it when
37 * you start doing multicast video 8)
38 * Alan Cox : Rewrote net_bh and list manager.
39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
40 * Alan Cox : Took out transmit every packet pass
41 * Saved a few bytes in the ioctl handler
42 * Alan Cox : Network driver sets packet type before
43 * calling netif_rx. Saves a function
44 * call a packet.
45 * Alan Cox : Hashed net_bh()
46 * Richard Kooijman: Timestamp fixes.
47 * Alan Cox : Wrong field in SIOCGIFDSTADDR
48 * Alan Cox : Device lock protection.
49 * Alan Cox : Fixed nasty side effect of device close
50 * changes.
51 * Rudi Cilibrasi : Pass the right thing to
52 * set_mac_address()
53 * Dave Miller : 32bit quantity for the device lock to
54 * make it work out on a Sparc.
55 * Bjorn Ekwall : Added KERNELD hack.
56 * Alan Cox : Cleaned up the backlog initialise.
57 * Craig Metz : SIOCGIFCONF fix if space for under
58 * 1 device.
59 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
60 * is no device open function.
61 * Andi Kleen : Fix error reporting for SIOCGIFCONF
62 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
63 * Cyrus Durgin : Cleaned for KMOD
64 * Adam Sulmicki : Bug Fix : Network Device Unload
65 * A network device unload needs to purge
66 * the backlog queue.
67 * Paul Rusty Russell : SIOCSIFNAME
68 * Pekka Riikonen : Netdev boot-time settings code
69 * Andrew Morton : Make unregister_netdevice wait
70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
72 * - netif_rx() feedback
73 */
74
75#include <asm/uaccess.h>
76#include <asm/system.h>
77#include <linux/bitops.h>
4fc268d2 78#include <linux/capability.h>
1da177e4
LT
79#include <linux/cpu.h>
80#include <linux/types.h>
81#include <linux/kernel.h>
82#include <linux/sched.h>
4a3e2f71 83#include <linux/mutex.h>
1da177e4
LT
84#include <linux/string.h>
85#include <linux/mm.h>
86#include <linux/socket.h>
87#include <linux/sockios.h>
88#include <linux/errno.h>
89#include <linux/interrupt.h>
90#include <linux/if_ether.h>
91#include <linux/netdevice.h>
92#include <linux/etherdevice.h>
0187bdfb 93#include <linux/ethtool.h>
1da177e4
LT
94#include <linux/notifier.h>
95#include <linux/skbuff.h>
457c4cbc 96#include <net/net_namespace.h>
1da177e4
LT
97#include <net/sock.h>
98#include <linux/rtnetlink.h>
99#include <linux/proc_fs.h>
100#include <linux/seq_file.h>
101#include <linux/stat.h>
102#include <linux/if_bridge.h>
b863ceb7 103#include <linux/if_macvlan.h>
1da177e4
LT
104#include <net/dst.h>
105#include <net/pkt_sched.h>
106#include <net/checksum.h>
107#include <linux/highmem.h>
108#include <linux/init.h>
109#include <linux/kmod.h>
110#include <linux/module.h>
111#include <linux/kallsyms.h>
112#include <linux/netpoll.h>
113#include <linux/rcupdate.h>
114#include <linux/delay.h>
295f4a1f 115#include <net/wext.h>
1da177e4 116#include <net/iw_handler.h>
1da177e4 117#include <asm/current.h>
5bdb9886 118#include <linux/audit.h>
db217334 119#include <linux/dmaengine.h>
f6a78bfc 120#include <linux/err.h>
c7fa9d18 121#include <linux/ctype.h>
723e98b7 122#include <linux/if_arp.h>
6de329e2 123#include <linux/if_vlan.h>
8f0f2223
DM
124#include <linux/ip.h>
125#include <linux/ipv6.h>
126#include <linux/in.h>
b6b2fed1
DM
127#include <linux/jhash.h>
128#include <linux/random.h>
1da177e4 129
342709ef
PE
130#include "net-sysfs.h"
131
1da177e4
LT
132/*
133 * The list of packet types we will receive (as opposed to discard)
134 * and the routines to invoke.
135 *
136 * Why 16. Because with 16 the only overlap we get on a hash of the
137 * low nibble of the protocol value is RARP/SNAP/X.25.
138 *
139 * NOTE: That is no longer true with the addition of VLAN tags. Not
140 * sure which should go first, but I bet it won't make much
141 * difference if we are running VLANs. The good news is that
142 * this protocol won't be in the list unless compiled in, so
3041a069 143 * the average user (w/out VLANs) will not be adversely affected.
1da177e4
LT
144 * --BLG
145 *
146 * 0800 IP
147 * 8100 802.1Q VLAN
148 * 0001 802.3
149 * 0002 AX.25
150 * 0004 802.2
151 * 8035 RARP
152 * 0005 SNAP
153 * 0805 X.25
154 * 0806 ARP
155 * 8137 IPX
156 * 0009 Localtalk
157 * 86DD IPv6
158 */
159
82d8a867
PE
160#define PTYPE_HASH_SIZE (16)
161#define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
162
1da177e4 163static DEFINE_SPINLOCK(ptype_lock);
82d8a867 164static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
6b2bedc3 165static struct list_head ptype_all __read_mostly; /* Taps */
1da177e4 166
db217334 167#ifdef CONFIG_NET_DMA
d379b01e
DW
168struct net_dma {
169 struct dma_client client;
170 spinlock_t lock;
171 cpumask_t channel_mask;
0c0b0aca 172 struct dma_chan **channels;
d379b01e
DW
173};
174
175static enum dma_state_client
176netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
177 enum dma_state state);
178
179static struct net_dma net_dma = {
180 .client = {
181 .event_callback = netdev_dma_event,
182 },
183};
db217334
CL
184#endif
185
1da177e4 186/*
7562f876 187 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
1da177e4
LT
188 * semaphore.
189 *
190 * Pure readers hold dev_base_lock for reading.
191 *
192 * Writers must hold the rtnl semaphore while they loop through the
7562f876 193 * dev_base_head list, and hold dev_base_lock for writing when they do the
1da177e4
LT
194 * actual updates. This allows pure readers to access the list even
195 * while a writer is preparing to update it.
196 *
197 * To put it another way, dev_base_lock is held for writing only to
198 * protect against pure readers; the rtnl semaphore provides the
199 * protection against other writers.
200 *
201 * See, for example usages, register_netdevice() and
202 * unregister_netdevice(), which must be called with the rtnl
203 * semaphore held.
204 */
1da177e4
LT
205DEFINE_RWLOCK(dev_base_lock);
206
1da177e4
LT
207EXPORT_SYMBOL(dev_base_lock);
208
209#define NETDEV_HASHBITS 8
881d966b 210#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
1da177e4 211
881d966b 212static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
1da177e4
LT
213{
214 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
881d966b 215 return &net->dev_name_head[hash & ((1 << NETDEV_HASHBITS) - 1)];
1da177e4
LT
216}
217
881d966b 218static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
1da177e4 219{
881d966b 220 return &net->dev_index_head[ifindex & ((1 << NETDEV_HASHBITS) - 1)];
1da177e4
LT
221}
222
ce286d32
EB
223/* Device list insertion */
224static int list_netdevice(struct net_device *dev)
225{
c346dca1 226 struct net *net = dev_net(dev);
ce286d32
EB
227
228 ASSERT_RTNL();
229
230 write_lock_bh(&dev_base_lock);
231 list_add_tail(&dev->dev_list, &net->dev_base_head);
232 hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name));
233 hlist_add_head(&dev->index_hlist, dev_index_hash(net, dev->ifindex));
234 write_unlock_bh(&dev_base_lock);
235 return 0;
236}
237
238/* Device list removal */
239static void unlist_netdevice(struct net_device *dev)
240{
241 ASSERT_RTNL();
242
243 /* Unlink dev from the device chain */
244 write_lock_bh(&dev_base_lock);
245 list_del(&dev->dev_list);
246 hlist_del(&dev->name_hlist);
247 hlist_del(&dev->index_hlist);
248 write_unlock_bh(&dev_base_lock);
249}
250
1da177e4
LT
251/*
252 * Our notifier list
253 */
254
f07d5b94 255static RAW_NOTIFIER_HEAD(netdev_chain);
1da177e4
LT
256
257/*
258 * Device drivers call our routines to queue packets here. We empty the
259 * queue in the local softnet handler.
260 */
bea3348e
SH
261
262DEFINE_PER_CPU(struct softnet_data, softnet_data);
1da177e4 263
723e98b7
JP
264#ifdef CONFIG_DEBUG_LOCK_ALLOC
265/*
c773e847 266 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
723e98b7
JP
267 * according to dev->type
268 */
269static const unsigned short netdev_lock_type[] =
270 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
271 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
272 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
273 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
274 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
275 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
276 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
277 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
278 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
279 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
280 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
281 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
282 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
283 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
284 ARPHRD_NONE};
285
286static const char *netdev_lock_name[] =
287 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
288 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
289 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
290 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
291 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
292 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
293 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
294 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
295 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
296 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
297 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
298 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
299 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
300 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
301 "_xmit_NONE"};
302
303static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
304
305static inline unsigned short netdev_lock_pos(unsigned short dev_type)
306{
307 int i;
308
309 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
310 if (netdev_lock_type[i] == dev_type)
311 return i;
312 /* the last key is used by default */
313 return ARRAY_SIZE(netdev_lock_type) - 1;
314}
315
316static inline void netdev_set_lockdep_class(spinlock_t *lock,
317 unsigned short dev_type)
318{
319 int i;
320
321 i = netdev_lock_pos(dev_type);
322 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
323 netdev_lock_name[i]);
324}
325#else
326static inline void netdev_set_lockdep_class(spinlock_t *lock,
327 unsigned short dev_type)
328{
329}
330#endif
1da177e4
LT
331
332/*******************************************************************************
333
334 Protocol management and registration routines
335
336*******************************************************************************/
337
1da177e4
LT
338/*
339 * Add a protocol ID to the list. Now that the input handler is
340 * smarter we can dispense with all the messy stuff that used to be
341 * here.
342 *
343 * BEWARE!!! Protocol handlers, mangling input packets,
344 * MUST BE last in hash buckets and checking protocol handlers
345 * MUST start from promiscuous ptype_all chain in net_bh.
346 * It is true now, do not change it.
347 * Explanation follows: if protocol handler, mangling packet, will
348 * be the first on list, it is not able to sense, that packet
349 * is cloned and should be copied-on-write, so that it will
350 * change it and subsequent readers will get broken packet.
351 * --ANK (980803)
352 */
353
354/**
355 * dev_add_pack - add packet handler
356 * @pt: packet type declaration
357 *
358 * Add a protocol handler to the networking stack. The passed &packet_type
359 * is linked into kernel lists and may not be freed until it has been
360 * removed from the kernel lists.
361 *
4ec93edb 362 * This call does not sleep therefore it can not
1da177e4
LT
363 * guarantee all CPU's that are in middle of receiving packets
364 * will see the new packet type (until the next received packet).
365 */
366
367void dev_add_pack(struct packet_type *pt)
368{
369 int hash;
370
371 spin_lock_bh(&ptype_lock);
9be9a6b9 372 if (pt->type == htons(ETH_P_ALL))
1da177e4 373 list_add_rcu(&pt->list, &ptype_all);
9be9a6b9 374 else {
82d8a867 375 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
1da177e4
LT
376 list_add_rcu(&pt->list, &ptype_base[hash]);
377 }
378 spin_unlock_bh(&ptype_lock);
379}
380
1da177e4
LT
381/**
382 * __dev_remove_pack - remove packet handler
383 * @pt: packet type declaration
384 *
385 * Remove a protocol handler that was previously added to the kernel
386 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
387 * from the kernel lists and can be freed or reused once this function
4ec93edb 388 * returns.
1da177e4
LT
389 *
390 * The packet type might still be in use by receivers
391 * and must not be freed until after all the CPU's have gone
392 * through a quiescent state.
393 */
394void __dev_remove_pack(struct packet_type *pt)
395{
396 struct list_head *head;
397 struct packet_type *pt1;
398
399 spin_lock_bh(&ptype_lock);
400
9be9a6b9 401 if (pt->type == htons(ETH_P_ALL))
1da177e4 402 head = &ptype_all;
9be9a6b9 403 else
82d8a867 404 head = &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
1da177e4
LT
405
406 list_for_each_entry(pt1, head, list) {
407 if (pt == pt1) {
408 list_del_rcu(&pt->list);
409 goto out;
410 }
411 }
412
413 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
414out:
415 spin_unlock_bh(&ptype_lock);
416}
417/**
418 * dev_remove_pack - remove packet handler
419 * @pt: packet type declaration
420 *
421 * Remove a protocol handler that was previously added to the kernel
422 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
423 * from the kernel lists and can be freed or reused once this function
424 * returns.
425 *
426 * This call sleeps to guarantee that no CPU is looking at the packet
427 * type after return.
428 */
429void dev_remove_pack(struct packet_type *pt)
430{
431 __dev_remove_pack(pt);
4ec93edb 432
1da177e4
LT
433 synchronize_net();
434}
435
436/******************************************************************************
437
438 Device Boot-time Settings Routines
439
440*******************************************************************************/
441
442/* Boot time configuration table */
443static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
444
445/**
446 * netdev_boot_setup_add - add new setup entry
447 * @name: name of the device
448 * @map: configured settings for the device
449 *
450 * Adds new setup entry to the dev_boot_setup list. The function
451 * returns 0 on error and 1 on success. This is a generic routine to
452 * all netdevices.
453 */
454static int netdev_boot_setup_add(char *name, struct ifmap *map)
455{
456 struct netdev_boot_setup *s;
457 int i;
458
459 s = dev_boot_setup;
460 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
461 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
462 memset(s[i].name, 0, sizeof(s[i].name));
93b3cff9 463 strlcpy(s[i].name, name, IFNAMSIZ);
1da177e4
LT
464 memcpy(&s[i].map, map, sizeof(s[i].map));
465 break;
466 }
467 }
468
469 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
470}
471
472/**
473 * netdev_boot_setup_check - check boot time settings
474 * @dev: the netdevice
475 *
476 * Check boot time settings for the device.
477 * The found settings are set for the device to be used
478 * later in the device probing.
479 * Returns 0 if no settings found, 1 if they are.
480 */
481int netdev_boot_setup_check(struct net_device *dev)
482{
483 struct netdev_boot_setup *s = dev_boot_setup;
484 int i;
485
486 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
487 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
93b3cff9 488 !strcmp(dev->name, s[i].name)) {
1da177e4
LT
489 dev->irq = s[i].map.irq;
490 dev->base_addr = s[i].map.base_addr;
491 dev->mem_start = s[i].map.mem_start;
492 dev->mem_end = s[i].map.mem_end;
493 return 1;
494 }
495 }
496 return 0;
497}
498
499
500/**
501 * netdev_boot_base - get address from boot time settings
502 * @prefix: prefix for network device
503 * @unit: id for network device
504 *
505 * Check boot time settings for the base address of device.
506 * The found settings are set for the device to be used
507 * later in the device probing.
508 * Returns 0 if no settings found.
509 */
510unsigned long netdev_boot_base(const char *prefix, int unit)
511{
512 const struct netdev_boot_setup *s = dev_boot_setup;
513 char name[IFNAMSIZ];
514 int i;
515
516 sprintf(name, "%s%d", prefix, unit);
517
518 /*
519 * If device already registered then return base of 1
520 * to indicate not to probe for this interface
521 */
881d966b 522 if (__dev_get_by_name(&init_net, name))
1da177e4
LT
523 return 1;
524
525 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
526 if (!strcmp(name, s[i].name))
527 return s[i].map.base_addr;
528 return 0;
529}
530
531/*
532 * Saves at boot time configured settings for any netdevice.
533 */
534int __init netdev_boot_setup(char *str)
535{
536 int ints[5];
537 struct ifmap map;
538
539 str = get_options(str, ARRAY_SIZE(ints), ints);
540 if (!str || !*str)
541 return 0;
542
543 /* Save settings */
544 memset(&map, 0, sizeof(map));
545 if (ints[0] > 0)
546 map.irq = ints[1];
547 if (ints[0] > 1)
548 map.base_addr = ints[2];
549 if (ints[0] > 2)
550 map.mem_start = ints[3];
551 if (ints[0] > 3)
552 map.mem_end = ints[4];
553
554 /* Add new entry to the list */
555 return netdev_boot_setup_add(str, &map);
556}
557
558__setup("netdev=", netdev_boot_setup);
559
560/*******************************************************************************
561
562 Device Interface Subroutines
563
564*******************************************************************************/
565
566/**
567 * __dev_get_by_name - find a device by its name
c4ea43c5 568 * @net: the applicable net namespace
1da177e4
LT
569 * @name: name to find
570 *
571 * Find an interface by name. Must be called under RTNL semaphore
572 * or @dev_base_lock. If the name is found a pointer to the device
573 * is returned. If the name is not found then %NULL is returned. The
574 * reference counters are not incremented so the caller must be
575 * careful with locks.
576 */
577
881d966b 578struct net_device *__dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
579{
580 struct hlist_node *p;
581
881d966b 582 hlist_for_each(p, dev_name_hash(net, name)) {
1da177e4
LT
583 struct net_device *dev
584 = hlist_entry(p, struct net_device, name_hlist);
585 if (!strncmp(dev->name, name, IFNAMSIZ))
586 return dev;
587 }
588 return NULL;
589}
590
591/**
592 * dev_get_by_name - find a device by its name
c4ea43c5 593 * @net: the applicable net namespace
1da177e4
LT
594 * @name: name to find
595 *
596 * Find an interface by name. This can be called from any
597 * context and does its own locking. The returned handle has
598 * the usage count incremented and the caller must use dev_put() to
599 * release it when it is no longer needed. %NULL is returned if no
600 * matching device is found.
601 */
602
881d966b 603struct net_device *dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
604{
605 struct net_device *dev;
606
607 read_lock(&dev_base_lock);
881d966b 608 dev = __dev_get_by_name(net, name);
1da177e4
LT
609 if (dev)
610 dev_hold(dev);
611 read_unlock(&dev_base_lock);
612 return dev;
613}
614
615/**
616 * __dev_get_by_index - find a device by its ifindex
c4ea43c5 617 * @net: the applicable net namespace
1da177e4
LT
618 * @ifindex: index of device
619 *
620 * Search for an interface by index. Returns %NULL if the device
621 * is not found or a pointer to the device. The device has not
622 * had its reference counter increased so the caller must be careful
623 * about locking. The caller must hold either the RTNL semaphore
624 * or @dev_base_lock.
625 */
626
881d966b 627struct net_device *__dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
628{
629 struct hlist_node *p;
630
881d966b 631 hlist_for_each(p, dev_index_hash(net, ifindex)) {
1da177e4
LT
632 struct net_device *dev
633 = hlist_entry(p, struct net_device, index_hlist);
634 if (dev->ifindex == ifindex)
635 return dev;
636 }
637 return NULL;
638}
639
640
641/**
642 * dev_get_by_index - find a device by its ifindex
c4ea43c5 643 * @net: the applicable net namespace
1da177e4
LT
644 * @ifindex: index of device
645 *
646 * Search for an interface by index. Returns NULL if the device
647 * is not found or a pointer to the device. The device returned has
648 * had a reference added and the pointer is safe until the user calls
649 * dev_put to indicate they have finished with it.
650 */
651
881d966b 652struct net_device *dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
653{
654 struct net_device *dev;
655
656 read_lock(&dev_base_lock);
881d966b 657 dev = __dev_get_by_index(net, ifindex);
1da177e4
LT
658 if (dev)
659 dev_hold(dev);
660 read_unlock(&dev_base_lock);
661 return dev;
662}
663
664/**
665 * dev_getbyhwaddr - find a device by its hardware address
c4ea43c5 666 * @net: the applicable net namespace
1da177e4
LT
667 * @type: media type of device
668 * @ha: hardware address
669 *
670 * Search for an interface by MAC address. Returns NULL if the device
671 * is not found or a pointer to the device. The caller must hold the
672 * rtnl semaphore. The returned device has not had its ref count increased
673 * and the caller must therefore be careful about locking
674 *
675 * BUGS:
676 * If the API was consistent this would be __dev_get_by_hwaddr
677 */
678
881d966b 679struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha)
1da177e4
LT
680{
681 struct net_device *dev;
682
683 ASSERT_RTNL();
684
81103a52 685 for_each_netdev(net, dev)
1da177e4
LT
686 if (dev->type == type &&
687 !memcmp(dev->dev_addr, ha, dev->addr_len))
7562f876
PE
688 return dev;
689
690 return NULL;
1da177e4
LT
691}
692
cf309e3f
JF
693EXPORT_SYMBOL(dev_getbyhwaddr);
694
881d966b 695struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
1da177e4
LT
696{
697 struct net_device *dev;
698
4e9cac2b 699 ASSERT_RTNL();
881d966b 700 for_each_netdev(net, dev)
4e9cac2b 701 if (dev->type == type)
7562f876
PE
702 return dev;
703
704 return NULL;
4e9cac2b
PM
705}
706
707EXPORT_SYMBOL(__dev_getfirstbyhwtype);
708
881d966b 709struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
4e9cac2b
PM
710{
711 struct net_device *dev;
712
713 rtnl_lock();
881d966b 714 dev = __dev_getfirstbyhwtype(net, type);
4e9cac2b
PM
715 if (dev)
716 dev_hold(dev);
1da177e4
LT
717 rtnl_unlock();
718 return dev;
719}
720
721EXPORT_SYMBOL(dev_getfirstbyhwtype);
722
723/**
724 * dev_get_by_flags - find any device with given flags
c4ea43c5 725 * @net: the applicable net namespace
1da177e4
LT
726 * @if_flags: IFF_* values
727 * @mask: bitmask of bits in if_flags to check
728 *
729 * Search for any interface with the given flags. Returns NULL if a device
4ec93edb 730 * is not found or a pointer to the device. The device returned has
1da177e4
LT
731 * had a reference added and the pointer is safe until the user calls
732 * dev_put to indicate they have finished with it.
733 */
734
881d966b 735struct net_device * dev_get_by_flags(struct net *net, unsigned short if_flags, unsigned short mask)
1da177e4 736{
7562f876 737 struct net_device *dev, *ret;
1da177e4 738
7562f876 739 ret = NULL;
1da177e4 740 read_lock(&dev_base_lock);
881d966b 741 for_each_netdev(net, dev) {
1da177e4
LT
742 if (((dev->flags ^ if_flags) & mask) == 0) {
743 dev_hold(dev);
7562f876 744 ret = dev;
1da177e4
LT
745 break;
746 }
747 }
748 read_unlock(&dev_base_lock);
7562f876 749 return ret;
1da177e4
LT
750}
751
752/**
753 * dev_valid_name - check if name is okay for network device
754 * @name: name string
755 *
756 * Network device names need to be valid file names to
c7fa9d18
DM
757 * to allow sysfs to work. We also disallow any kind of
758 * whitespace.
1da177e4 759 */
c2373ee9 760int dev_valid_name(const char *name)
1da177e4 761{
c7fa9d18
DM
762 if (*name == '\0')
763 return 0;
b6fe17d6
SH
764 if (strlen(name) >= IFNAMSIZ)
765 return 0;
c7fa9d18
DM
766 if (!strcmp(name, ".") || !strcmp(name, ".."))
767 return 0;
768
769 while (*name) {
770 if (*name == '/' || isspace(*name))
771 return 0;
772 name++;
773 }
774 return 1;
1da177e4
LT
775}
776
777/**
b267b179
EB
778 * __dev_alloc_name - allocate a name for a device
779 * @net: network namespace to allocate the device name in
1da177e4 780 * @name: name format string
b267b179 781 * @buf: scratch buffer and result name string
1da177e4
LT
782 *
783 * Passed a format string - eg "lt%d" it will try and find a suitable
3041a069
SH
784 * id. It scans list of devices to build up a free map, then chooses
785 * the first empty slot. The caller must hold the dev_base or rtnl lock
786 * while allocating the name and adding the device in order to avoid
787 * duplicates.
788 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
789 * Returns the number of the unit assigned or a negative errno code.
1da177e4
LT
790 */
791
b267b179 792static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1da177e4
LT
793{
794 int i = 0;
1da177e4
LT
795 const char *p;
796 const int max_netdevices = 8*PAGE_SIZE;
cfcabdcc 797 unsigned long *inuse;
1da177e4
LT
798 struct net_device *d;
799
800 p = strnchr(name, IFNAMSIZ-1, '%');
801 if (p) {
802 /*
803 * Verify the string as this thing may have come from
804 * the user. There must be either one "%d" and no other "%"
805 * characters.
806 */
807 if (p[1] != 'd' || strchr(p + 2, '%'))
808 return -EINVAL;
809
810 /* Use one page as a bit array of possible slots */
cfcabdcc 811 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1da177e4
LT
812 if (!inuse)
813 return -ENOMEM;
814
881d966b 815 for_each_netdev(net, d) {
1da177e4
LT
816 if (!sscanf(d->name, name, &i))
817 continue;
818 if (i < 0 || i >= max_netdevices)
819 continue;
820
821 /* avoid cases where sscanf is not exact inverse of printf */
b267b179 822 snprintf(buf, IFNAMSIZ, name, i);
1da177e4
LT
823 if (!strncmp(buf, d->name, IFNAMSIZ))
824 set_bit(i, inuse);
825 }
826
827 i = find_first_zero_bit(inuse, max_netdevices);
828 free_page((unsigned long) inuse);
829 }
830
b267b179
EB
831 snprintf(buf, IFNAMSIZ, name, i);
832 if (!__dev_get_by_name(net, buf))
1da177e4 833 return i;
1da177e4
LT
834
835 /* It is possible to run out of possible slots
836 * when the name is long and there isn't enough space left
837 * for the digits, or if all bits are used.
838 */
839 return -ENFILE;
840}
841
b267b179
EB
842/**
843 * dev_alloc_name - allocate a name for a device
844 * @dev: device
845 * @name: name format string
846 *
847 * Passed a format string - eg "lt%d" it will try and find a suitable
848 * id. It scans list of devices to build up a free map, then chooses
849 * the first empty slot. The caller must hold the dev_base or rtnl lock
850 * while allocating the name and adding the device in order to avoid
851 * duplicates.
852 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
853 * Returns the number of the unit assigned or a negative errno code.
854 */
855
856int dev_alloc_name(struct net_device *dev, const char *name)
857{
858 char buf[IFNAMSIZ];
859 struct net *net;
860 int ret;
861
c346dca1
YH
862 BUG_ON(!dev_net(dev));
863 net = dev_net(dev);
b267b179
EB
864 ret = __dev_alloc_name(net, name, buf);
865 if (ret >= 0)
866 strlcpy(dev->name, buf, IFNAMSIZ);
867 return ret;
868}
869
1da177e4
LT
870
871/**
872 * dev_change_name - change name of a device
873 * @dev: device
874 * @newname: name (or format string) must be at least IFNAMSIZ
875 *
876 * Change name of a device, can pass format strings "eth%d".
877 * for wildcarding.
878 */
879int dev_change_name(struct net_device *dev, char *newname)
880{
fcc5a03a 881 char oldname[IFNAMSIZ];
1da177e4 882 int err = 0;
fcc5a03a 883 int ret;
881d966b 884 struct net *net;
1da177e4
LT
885
886 ASSERT_RTNL();
c346dca1 887 BUG_ON(!dev_net(dev));
1da177e4 888
c346dca1 889 net = dev_net(dev);
1da177e4
LT
890 if (dev->flags & IFF_UP)
891 return -EBUSY;
892
893 if (!dev_valid_name(newname))
894 return -EINVAL;
895
c8d90dca
SH
896 if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
897 return 0;
898
fcc5a03a
HX
899 memcpy(oldname, dev->name, IFNAMSIZ);
900
1da177e4
LT
901 if (strchr(newname, '%')) {
902 err = dev_alloc_name(dev, newname);
903 if (err < 0)
904 return err;
905 strcpy(newname, dev->name);
906 }
881d966b 907 else if (__dev_get_by_name(net, newname))
1da177e4
LT
908 return -EEXIST;
909 else
910 strlcpy(dev->name, newname, IFNAMSIZ);
911
fcc5a03a 912rollback:
dcc99773
SH
913 err = device_rename(&dev->dev, dev->name);
914 if (err) {
915 memcpy(dev->name, oldname, IFNAMSIZ);
916 return err;
917 }
7f988eab
HX
918
919 write_lock_bh(&dev_base_lock);
92749821 920 hlist_del(&dev->name_hlist);
881d966b 921 hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name));
7f988eab
HX
922 write_unlock_bh(&dev_base_lock);
923
056925ab 924 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
fcc5a03a
HX
925 ret = notifier_to_errno(ret);
926
927 if (ret) {
928 if (err) {
929 printk(KERN_ERR
930 "%s: name change rollback failed: %d.\n",
931 dev->name, ret);
932 } else {
933 err = ret;
934 memcpy(dev->name, oldname, IFNAMSIZ);
935 goto rollback;
936 }
937 }
1da177e4
LT
938
939 return err;
940}
941
d8a33ac4 942/**
3041a069 943 * netdev_features_change - device changes features
d8a33ac4
SH
944 * @dev: device to cause notification
945 *
946 * Called to indicate a device has changed features.
947 */
948void netdev_features_change(struct net_device *dev)
949{
056925ab 950 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
d8a33ac4
SH
951}
952EXPORT_SYMBOL(netdev_features_change);
953
1da177e4
LT
954/**
955 * netdev_state_change - device changes state
956 * @dev: device to cause notification
957 *
958 * Called to indicate a device has changed state. This function calls
959 * the notifier chains for netdev_chain and sends a NEWLINK message
960 * to the routing socket.
961 */
962void netdev_state_change(struct net_device *dev)
963{
964 if (dev->flags & IFF_UP) {
056925ab 965 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1da177e4
LT
966 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
967 }
968}
969
c1da4ac7
OG
970void netdev_bonding_change(struct net_device *dev)
971{
972 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, dev);
973}
974EXPORT_SYMBOL(netdev_bonding_change);
975
1da177e4
LT
976/**
977 * dev_load - load a network module
c4ea43c5 978 * @net: the applicable net namespace
1da177e4
LT
979 * @name: name of interface
980 *
981 * If a network interface is not present and the process has suitable
982 * privileges this function loads the module. If module loading is not
983 * available in this kernel then it becomes a nop.
984 */
985
881d966b 986void dev_load(struct net *net, const char *name)
1da177e4 987{
4ec93edb 988 struct net_device *dev;
1da177e4
LT
989
990 read_lock(&dev_base_lock);
881d966b 991 dev = __dev_get_by_name(net, name);
1da177e4
LT
992 read_unlock(&dev_base_lock);
993
994 if (!dev && capable(CAP_SYS_MODULE))
995 request_module("%s", name);
996}
997
1da177e4
LT
998/**
999 * dev_open - prepare an interface for use.
1000 * @dev: device to open
1001 *
1002 * Takes a device from down to up state. The device's private open
1003 * function is invoked and then the multicast lists are loaded. Finally
1004 * the device is moved into the up state and a %NETDEV_UP message is
1005 * sent to the netdev notifier chain.
1006 *
1007 * Calling this function on an active interface is a nop. On a failure
1008 * a negative errno code is returned.
1009 */
1010int dev_open(struct net_device *dev)
1011{
1012 int ret = 0;
1013
e46b66bc
BH
1014 ASSERT_RTNL();
1015
1da177e4
LT
1016 /*
1017 * Is it already up?
1018 */
1019
1020 if (dev->flags & IFF_UP)
1021 return 0;
1022
1023 /*
1024 * Is it even present?
1025 */
1026 if (!netif_device_present(dev))
1027 return -ENODEV;
1028
1029 /*
1030 * Call device private open method
1031 */
1032 set_bit(__LINK_STATE_START, &dev->state);
bada339b
JG
1033
1034 if (dev->validate_addr)
1035 ret = dev->validate_addr(dev);
1036
1037 if (!ret && dev->open)
1da177e4 1038 ret = dev->open(dev);
1da177e4 1039
4ec93edb 1040 /*
1da177e4
LT
1041 * If it went open OK then:
1042 */
1043
bada339b
JG
1044 if (ret)
1045 clear_bit(__LINK_STATE_START, &dev->state);
1046 else {
1da177e4
LT
1047 /*
1048 * Set the flags.
1049 */
1050 dev->flags |= IFF_UP;
1051
1052 /*
1053 * Initialize multicasting status
1054 */
4417da66 1055 dev_set_rx_mode(dev);
1da177e4
LT
1056
1057 /*
1058 * Wakeup transmit queue engine
1059 */
1060 dev_activate(dev);
1061
1062 /*
1063 * ... and announce new interface.
1064 */
056925ab 1065 call_netdevice_notifiers(NETDEV_UP, dev);
1da177e4 1066 }
bada339b 1067
1da177e4
LT
1068 return ret;
1069}
1070
1071/**
1072 * dev_close - shutdown an interface.
1073 * @dev: device to shutdown
1074 *
1075 * This function moves an active device into down state. A
1076 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1077 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1078 * chain.
1079 */
1080int dev_close(struct net_device *dev)
1081{
e46b66bc
BH
1082 ASSERT_RTNL();
1083
9d5010db
DM
1084 might_sleep();
1085
1da177e4
LT
1086 if (!(dev->flags & IFF_UP))
1087 return 0;
1088
1089 /*
1090 * Tell people we are going down, so that they can
1091 * prepare to death, when device is still operating.
1092 */
056925ab 1093 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1da177e4 1094
1da177e4
LT
1095 clear_bit(__LINK_STATE_START, &dev->state);
1096
1097 /* Synchronize to scheduled poll. We cannot touch poll list,
bea3348e
SH
1098 * it can be even on different cpu. So just clear netif_running().
1099 *
1100 * dev->stop() will invoke napi_disable() on all of it's
1101 * napi_struct instances on this device.
1102 */
1da177e4 1103 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1da177e4 1104
d8b2a4d2
ML
1105 dev_deactivate(dev);
1106
1da177e4
LT
1107 /*
1108 * Call the device specific close. This cannot fail.
1109 * Only if device is UP
1110 *
1111 * We allow it to be called even after a DETACH hot-plug
1112 * event.
1113 */
1114 if (dev->stop)
1115 dev->stop(dev);
1116
1117 /*
1118 * Device is now down.
1119 */
1120
1121 dev->flags &= ~IFF_UP;
1122
1123 /*
1124 * Tell people we are down
1125 */
056925ab 1126 call_netdevice_notifiers(NETDEV_DOWN, dev);
1da177e4
LT
1127
1128 return 0;
1129}
1130
1131
0187bdfb
BH
1132/**
1133 * dev_disable_lro - disable Large Receive Offload on a device
1134 * @dev: device
1135 *
1136 * Disable Large Receive Offload (LRO) on a net device. Must be
1137 * called under RTNL. This is needed if received packets may be
1138 * forwarded to another interface.
1139 */
1140void dev_disable_lro(struct net_device *dev)
1141{
1142 if (dev->ethtool_ops && dev->ethtool_ops->get_flags &&
1143 dev->ethtool_ops->set_flags) {
1144 u32 flags = dev->ethtool_ops->get_flags(dev);
1145 if (flags & ETH_FLAG_LRO) {
1146 flags &= ~ETH_FLAG_LRO;
1147 dev->ethtool_ops->set_flags(dev, flags);
1148 }
1149 }
1150 WARN_ON(dev->features & NETIF_F_LRO);
1151}
1152EXPORT_SYMBOL(dev_disable_lro);
1153
1154
881d966b
EB
1155static int dev_boot_phase = 1;
1156
1da177e4
LT
1157/*
1158 * Device change register/unregister. These are not inline or static
1159 * as we export them to the world.
1160 */
1161
1162/**
1163 * register_netdevice_notifier - register a network notifier block
1164 * @nb: notifier
1165 *
1166 * Register a notifier to be called when network device events occur.
1167 * The notifier passed is linked into the kernel structures and must
1168 * not be reused until it has been unregistered. A negative errno code
1169 * is returned on a failure.
1170 *
1171 * When registered all registration and up events are replayed
4ec93edb 1172 * to the new notifier to allow device to have a race free
1da177e4
LT
1173 * view of the network device list.
1174 */
1175
1176int register_netdevice_notifier(struct notifier_block *nb)
1177{
1178 struct net_device *dev;
fcc5a03a 1179 struct net_device *last;
881d966b 1180 struct net *net;
1da177e4
LT
1181 int err;
1182
1183 rtnl_lock();
f07d5b94 1184 err = raw_notifier_chain_register(&netdev_chain, nb);
fcc5a03a
HX
1185 if (err)
1186 goto unlock;
881d966b
EB
1187 if (dev_boot_phase)
1188 goto unlock;
1189 for_each_net(net) {
1190 for_each_netdev(net, dev) {
1191 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1192 err = notifier_to_errno(err);
1193 if (err)
1194 goto rollback;
1195
1196 if (!(dev->flags & IFF_UP))
1197 continue;
1da177e4 1198
881d966b
EB
1199 nb->notifier_call(nb, NETDEV_UP, dev);
1200 }
1da177e4 1201 }
fcc5a03a
HX
1202
1203unlock:
1da177e4
LT
1204 rtnl_unlock();
1205 return err;
fcc5a03a
HX
1206
1207rollback:
1208 last = dev;
881d966b
EB
1209 for_each_net(net) {
1210 for_each_netdev(net, dev) {
1211 if (dev == last)
1212 break;
fcc5a03a 1213
881d966b
EB
1214 if (dev->flags & IFF_UP) {
1215 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1216 nb->notifier_call(nb, NETDEV_DOWN, dev);
1217 }
1218 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
fcc5a03a 1219 }
fcc5a03a 1220 }
c67625a1
PE
1221
1222 raw_notifier_chain_unregister(&netdev_chain, nb);
fcc5a03a 1223 goto unlock;
1da177e4
LT
1224}
1225
1226/**
1227 * unregister_netdevice_notifier - unregister a network notifier block
1228 * @nb: notifier
1229 *
1230 * Unregister a notifier previously registered by
1231 * register_netdevice_notifier(). The notifier is unlinked into the
1232 * kernel structures and may then be reused. A negative errno code
1233 * is returned on a failure.
1234 */
1235
1236int unregister_netdevice_notifier(struct notifier_block *nb)
1237{
9f514950
HX
1238 int err;
1239
1240 rtnl_lock();
f07d5b94 1241 err = raw_notifier_chain_unregister(&netdev_chain, nb);
9f514950
HX
1242 rtnl_unlock();
1243 return err;
1da177e4
LT
1244}
1245
1246/**
1247 * call_netdevice_notifiers - call all network notifier blocks
1248 * @val: value passed unmodified to notifier function
c4ea43c5 1249 * @dev: net_device pointer passed unmodified to notifier function
1da177e4
LT
1250 *
1251 * Call all network notifier blocks. Parameters and return value
f07d5b94 1252 * are as for raw_notifier_call_chain().
1da177e4
LT
1253 */
1254
ad7379d4 1255int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1da177e4 1256{
ad7379d4 1257 return raw_notifier_call_chain(&netdev_chain, val, dev);
1da177e4
LT
1258}
1259
1260/* When > 0 there are consumers of rx skb time stamps */
1261static atomic_t netstamp_needed = ATOMIC_INIT(0);
1262
1263void net_enable_timestamp(void)
1264{
1265 atomic_inc(&netstamp_needed);
1266}
1267
1268void net_disable_timestamp(void)
1269{
1270 atomic_dec(&netstamp_needed);
1271}
1272
a61bbcf2 1273static inline void net_timestamp(struct sk_buff *skb)
1da177e4
LT
1274{
1275 if (atomic_read(&netstamp_needed))
a61bbcf2 1276 __net_timestamp(skb);
b7aa0bf7
ED
1277 else
1278 skb->tstamp.tv64 = 0;
1da177e4
LT
1279}
1280
1281/*
1282 * Support routine. Sends outgoing frames to any network
1283 * taps currently in use.
1284 */
1285
f6a78bfc 1286static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
1287{
1288 struct packet_type *ptype;
a61bbcf2
PM
1289
1290 net_timestamp(skb);
1da177e4
LT
1291
1292 rcu_read_lock();
1293 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1294 /* Never send packets back to the socket
1295 * they originated from - MvS (miquels@drinkel.ow.org)
1296 */
1297 if ((ptype->dev == dev || !ptype->dev) &&
1298 (ptype->af_packet_priv == NULL ||
1299 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1300 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1301 if (!skb2)
1302 break;
1303
1304 /* skb->nh should be correctly
1305 set by sender, so that the second statement is
1306 just protection against buggy protocols.
1307 */
459a98ed 1308 skb_reset_mac_header(skb2);
1da177e4 1309
d56f90a7 1310 if (skb_network_header(skb2) < skb2->data ||
27a884dc 1311 skb2->network_header > skb2->tail) {
1da177e4
LT
1312 if (net_ratelimit())
1313 printk(KERN_CRIT "protocol %04x is "
1314 "buggy, dev %s\n",
1315 skb2->protocol, dev->name);
c1d2bbe1 1316 skb_reset_network_header(skb2);
1da177e4
LT
1317 }
1318
b0e380b1 1319 skb2->transport_header = skb2->network_header;
1da177e4 1320 skb2->pkt_type = PACKET_OUTGOING;
f2ccd8fa 1321 ptype->func(skb2, skb->dev, ptype, skb->dev);
1da177e4
LT
1322 }
1323 }
1324 rcu_read_unlock();
1325}
1326
56079431 1327
37437bb2 1328void __netif_schedule(struct Qdisc *q)
56079431 1329{
867d79fb
LT
1330 if (WARN_ON_ONCE(q == &noop_qdisc))
1331 return;
86d804e1 1332
37437bb2 1333 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) {
56079431 1334 struct softnet_data *sd;
86d804e1 1335 unsigned long flags;
56079431
DV
1336
1337 local_irq_save(flags);
1338 sd = &__get_cpu_var(softnet_data);
37437bb2
DM
1339 q->next_sched = sd->output_queue;
1340 sd->output_queue = q;
56079431
DV
1341 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1342 local_irq_restore(flags);
1343 }
1344}
1345EXPORT_SYMBOL(__netif_schedule);
1346
bea3348e 1347void dev_kfree_skb_irq(struct sk_buff *skb)
56079431 1348{
bea3348e
SH
1349 if (atomic_dec_and_test(&skb->users)) {
1350 struct softnet_data *sd;
1351 unsigned long flags;
56079431 1352
bea3348e
SH
1353 local_irq_save(flags);
1354 sd = &__get_cpu_var(softnet_data);
1355 skb->next = sd->completion_queue;
1356 sd->completion_queue = skb;
1357 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1358 local_irq_restore(flags);
1359 }
56079431 1360}
bea3348e 1361EXPORT_SYMBOL(dev_kfree_skb_irq);
56079431
DV
1362
1363void dev_kfree_skb_any(struct sk_buff *skb)
1364{
1365 if (in_irq() || irqs_disabled())
1366 dev_kfree_skb_irq(skb);
1367 else
1368 dev_kfree_skb(skb);
1369}
1370EXPORT_SYMBOL(dev_kfree_skb_any);
1371
1372
bea3348e
SH
1373/**
1374 * netif_device_detach - mark device as removed
1375 * @dev: network device
1376 *
1377 * Mark device as removed from system and therefore no longer available.
1378 */
56079431
DV
1379void netif_device_detach(struct net_device *dev)
1380{
1381 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1382 netif_running(dev)) {
1383 netif_stop_queue(dev);
1384 }
1385}
1386EXPORT_SYMBOL(netif_device_detach);
1387
bea3348e
SH
1388/**
1389 * netif_device_attach - mark device as attached
1390 * @dev: network device
1391 *
1392 * Mark device as attached from system and restart if needed.
1393 */
56079431
DV
1394void netif_device_attach(struct net_device *dev)
1395{
1396 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1397 netif_running(dev)) {
1398 netif_wake_queue(dev);
4ec93edb 1399 __netdev_watchdog_up(dev);
56079431
DV
1400 }
1401}
1402EXPORT_SYMBOL(netif_device_attach);
1403
6de329e2
BH
1404static bool can_checksum_protocol(unsigned long features, __be16 protocol)
1405{
1406 return ((features & NETIF_F_GEN_CSUM) ||
1407 ((features & NETIF_F_IP_CSUM) &&
1408 protocol == htons(ETH_P_IP)) ||
1409 ((features & NETIF_F_IPV6_CSUM) &&
1410 protocol == htons(ETH_P_IPV6)));
1411}
1412
1413static bool dev_can_checksum(struct net_device *dev, struct sk_buff *skb)
1414{
1415 if (can_checksum_protocol(dev->features, skb->protocol))
1416 return true;
1417
1418 if (skb->protocol == htons(ETH_P_8021Q)) {
1419 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
1420 if (can_checksum_protocol(dev->features & dev->vlan_features,
1421 veh->h_vlan_encapsulated_proto))
1422 return true;
1423 }
1424
1425 return false;
1426}
56079431 1427
1da177e4
LT
1428/*
1429 * Invalidate hardware checksum when packet is to be mangled, and
1430 * complete checksum manually on outgoing path.
1431 */
84fa7933 1432int skb_checksum_help(struct sk_buff *skb)
1da177e4 1433{
d3bc23e7 1434 __wsum csum;
663ead3b 1435 int ret = 0, offset;
1da177e4 1436
84fa7933 1437 if (skb->ip_summed == CHECKSUM_COMPLETE)
a430a43d
HX
1438 goto out_set_summed;
1439
1440 if (unlikely(skb_shinfo(skb)->gso_size)) {
a430a43d
HX
1441 /* Let GSO fix up the checksum. */
1442 goto out_set_summed;
1da177e4
LT
1443 }
1444
a030847e
HX
1445 offset = skb->csum_start - skb_headroom(skb);
1446 BUG_ON(offset >= skb_headlen(skb));
1447 csum = skb_checksum(skb, offset, skb->len - offset, 0);
1448
1449 offset += skb->csum_offset;
1450 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
1451
1452 if (skb_cloned(skb) &&
1453 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
1da177e4
LT
1454 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1455 if (ret)
1456 goto out;
1457 }
1458
a030847e 1459 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
a430a43d 1460out_set_summed:
1da177e4 1461 skb->ip_summed = CHECKSUM_NONE;
4ec93edb 1462out:
1da177e4
LT
1463 return ret;
1464}
1465
f6a78bfc
HX
1466/**
1467 * skb_gso_segment - Perform segmentation on skb.
1468 * @skb: buffer to segment
576a30eb 1469 * @features: features for the output path (see dev->features)
f6a78bfc
HX
1470 *
1471 * This function segments the given skb and returns a list of segments.
576a30eb
HX
1472 *
1473 * It may return NULL if the skb requires no segmentation. This is
1474 * only possible when GSO is used for verifying header integrity.
f6a78bfc 1475 */
576a30eb 1476struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
f6a78bfc
HX
1477{
1478 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1479 struct packet_type *ptype;
252e3346 1480 __be16 type = skb->protocol;
a430a43d 1481 int err;
f6a78bfc
HX
1482
1483 BUG_ON(skb_shinfo(skb)->frag_list);
f6a78bfc 1484
459a98ed 1485 skb_reset_mac_header(skb);
b0e380b1 1486 skb->mac_len = skb->network_header - skb->mac_header;
f6a78bfc
HX
1487 __skb_pull(skb, skb->mac_len);
1488
f9d106a6 1489 if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
a430a43d
HX
1490 if (skb_header_cloned(skb) &&
1491 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1492 return ERR_PTR(err);
1493 }
1494
f6a78bfc 1495 rcu_read_lock();
82d8a867
PE
1496 list_for_each_entry_rcu(ptype,
1497 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
f6a78bfc 1498 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
84fa7933 1499 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
a430a43d
HX
1500 err = ptype->gso_send_check(skb);
1501 segs = ERR_PTR(err);
1502 if (err || skb_gso_ok(skb, features))
1503 break;
d56f90a7
ACM
1504 __skb_push(skb, (skb->data -
1505 skb_network_header(skb)));
a430a43d 1506 }
576a30eb 1507 segs = ptype->gso_segment(skb, features);
f6a78bfc
HX
1508 break;
1509 }
1510 }
1511 rcu_read_unlock();
1512
98e399f8 1513 __skb_push(skb, skb->data - skb_mac_header(skb));
576a30eb 1514
f6a78bfc
HX
1515 return segs;
1516}
1517
1518EXPORT_SYMBOL(skb_gso_segment);
1519
fb286bb2
HX
1520/* Take action when hardware reception checksum errors are detected. */
1521#ifdef CONFIG_BUG
1522void netdev_rx_csum_fault(struct net_device *dev)
1523{
1524 if (net_ratelimit()) {
4ec93edb 1525 printk(KERN_ERR "%s: hw csum failure.\n",
246a4212 1526 dev ? dev->name : "<unknown>");
fb286bb2
HX
1527 dump_stack();
1528 }
1529}
1530EXPORT_SYMBOL(netdev_rx_csum_fault);
1531#endif
1532
1da177e4
LT
1533/* Actually, we should eliminate this check as soon as we know, that:
1534 * 1. IOMMU is present and allows to map all the memory.
1535 * 2. No high memory really exists on this machine.
1536 */
1537
1538static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1539{
3d3a8533 1540#ifdef CONFIG_HIGHMEM
1da177e4
LT
1541 int i;
1542
1543 if (dev->features & NETIF_F_HIGHDMA)
1544 return 0;
1545
1546 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1547 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1548 return 1;
1549
3d3a8533 1550#endif
1da177e4
LT
1551 return 0;
1552}
1da177e4 1553
f6a78bfc
HX
1554struct dev_gso_cb {
1555 void (*destructor)(struct sk_buff *skb);
1556};
1557
1558#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1559
1560static void dev_gso_skb_destructor(struct sk_buff *skb)
1561{
1562 struct dev_gso_cb *cb;
1563
1564 do {
1565 struct sk_buff *nskb = skb->next;
1566
1567 skb->next = nskb->next;
1568 nskb->next = NULL;
1569 kfree_skb(nskb);
1570 } while (skb->next);
1571
1572 cb = DEV_GSO_CB(skb);
1573 if (cb->destructor)
1574 cb->destructor(skb);
1575}
1576
1577/**
1578 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1579 * @skb: buffer to segment
1580 *
1581 * This function segments the given skb and stores the list of segments
1582 * in skb->next.
1583 */
1584static int dev_gso_segment(struct sk_buff *skb)
1585{
1586 struct net_device *dev = skb->dev;
1587 struct sk_buff *segs;
576a30eb
HX
1588 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1589 NETIF_F_SG : 0);
1590
1591 segs = skb_gso_segment(skb, features);
1592
1593 /* Verifying header integrity only. */
1594 if (!segs)
1595 return 0;
f6a78bfc 1596
801678c5 1597 if (IS_ERR(segs))
f6a78bfc
HX
1598 return PTR_ERR(segs);
1599
1600 skb->next = segs;
1601 DEV_GSO_CB(skb)->destructor = skb->destructor;
1602 skb->destructor = dev_gso_skb_destructor;
1603
1604 return 0;
1605}
1606
fd2ea0a7
DM
1607int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
1608 struct netdev_queue *txq)
f6a78bfc
HX
1609{
1610 if (likely(!skb->next)) {
9be9a6b9 1611 if (!list_empty(&ptype_all))
f6a78bfc
HX
1612 dev_queue_xmit_nit(skb, dev);
1613
576a30eb
HX
1614 if (netif_needs_gso(dev, skb)) {
1615 if (unlikely(dev_gso_segment(skb)))
1616 goto out_kfree_skb;
1617 if (skb->next)
1618 goto gso;
1619 }
f6a78bfc 1620
576a30eb 1621 return dev->hard_start_xmit(skb, dev);
f6a78bfc
HX
1622 }
1623
576a30eb 1624gso:
f6a78bfc
HX
1625 do {
1626 struct sk_buff *nskb = skb->next;
1627 int rc;
1628
1629 skb->next = nskb->next;
1630 nskb->next = NULL;
1631 rc = dev->hard_start_xmit(nskb, dev);
1632 if (unlikely(rc)) {
f54d9e8d 1633 nskb->next = skb->next;
f6a78bfc
HX
1634 skb->next = nskb;
1635 return rc;
1636 }
fd2ea0a7 1637 if (unlikely(netif_tx_queue_stopped(txq) && skb->next))
f54d9e8d 1638 return NETDEV_TX_BUSY;
f6a78bfc 1639 } while (skb->next);
4ec93edb 1640
f6a78bfc
HX
1641 skb->destructor = DEV_GSO_CB(skb)->destructor;
1642
1643out_kfree_skb:
1644 kfree_skb(skb);
1645 return 0;
1646}
1647
b6b2fed1
DM
1648static u32 simple_tx_hashrnd;
1649static int simple_tx_hashrnd_initialized = 0;
1650
8f0f2223
DM
1651static u16 simple_tx_hash(struct net_device *dev, struct sk_buff *skb)
1652{
b6b2fed1
DM
1653 u32 addr1, addr2, ports;
1654 u32 hash, ihl;
8f0f2223 1655 u8 ip_proto;
b6b2fed1
DM
1656
1657 if (unlikely(!simple_tx_hashrnd_initialized)) {
1658 get_random_bytes(&simple_tx_hashrnd, 4);
1659 simple_tx_hashrnd_initialized = 1;
1660 }
8f0f2223
DM
1661
1662 switch (skb->protocol) {
1663 case __constant_htons(ETH_P_IP):
1664 ip_proto = ip_hdr(skb)->protocol;
b6b2fed1
DM
1665 addr1 = ip_hdr(skb)->saddr;
1666 addr2 = ip_hdr(skb)->daddr;
8f0f2223 1667 ihl = ip_hdr(skb)->ihl;
8f0f2223
DM
1668 break;
1669 case __constant_htons(ETH_P_IPV6):
1670 ip_proto = ipv6_hdr(skb)->nexthdr;
b6b2fed1
DM
1671 addr1 = ipv6_hdr(skb)->saddr.s6_addr32[3];
1672 addr2 = ipv6_hdr(skb)->daddr.s6_addr32[3];
8f0f2223 1673 ihl = (40 >> 2);
8f0f2223
DM
1674 break;
1675 default:
1676 return 0;
1677 }
1678
8f0f2223
DM
1679
1680 switch (ip_proto) {
1681 case IPPROTO_TCP:
1682 case IPPROTO_UDP:
1683 case IPPROTO_DCCP:
1684 case IPPROTO_ESP:
1685 case IPPROTO_AH:
1686 case IPPROTO_SCTP:
1687 case IPPROTO_UDPLITE:
b6b2fed1 1688 ports = *((u32 *) (skb_network_header(skb) + (ihl * 4)));
8f0f2223
DM
1689 break;
1690
1691 default:
b6b2fed1 1692 ports = 0;
8f0f2223
DM
1693 break;
1694 }
1695
b6b2fed1
DM
1696 hash = jhash_3words(addr1, addr2, ports, simple_tx_hashrnd);
1697
1698 return (u16) (((u64) hash * dev->real_num_tx_queues) >> 32);
8f0f2223
DM
1699}
1700
e8a0464c
DM
1701static struct netdev_queue *dev_pick_tx(struct net_device *dev,
1702 struct sk_buff *skb)
1703{
fd2ea0a7
DM
1704 u16 queue_index = 0;
1705
eae792b7
DM
1706 if (dev->select_queue)
1707 queue_index = dev->select_queue(dev, skb);
8f0f2223
DM
1708 else if (dev->real_num_tx_queues > 1)
1709 queue_index = simple_tx_hash(dev, skb);
eae792b7 1710
fd2ea0a7
DM
1711 skb_set_queue_mapping(skb, queue_index);
1712 return netdev_get_tx_queue(dev, queue_index);
e8a0464c
DM
1713}
1714
d29f749e
DJ
1715/**
1716 * dev_queue_xmit - transmit a buffer
1717 * @skb: buffer to transmit
1718 *
1719 * Queue a buffer for transmission to a network device. The caller must
1720 * have set the device and priority and built the buffer before calling
1721 * this function. The function can be called from an interrupt.
1722 *
1723 * A negative errno code is returned on a failure. A success does not
1724 * guarantee the frame will be transmitted as it may be dropped due
1725 * to congestion or traffic shaping.
1726 *
1727 * -----------------------------------------------------------------------------------
1728 * I notice this method can also return errors from the queue disciplines,
1729 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1730 * be positive.
1731 *
1732 * Regardless of the return value, the skb is consumed, so it is currently
1733 * difficult to retry a send to this method. (You can bump the ref count
1734 * before sending to hold a reference for retry if you are careful.)
1735 *
1736 * When calling this method, interrupts MUST be enabled. This is because
1737 * the BH enable code must have IRQs enabled so that it will not deadlock.
1738 * --BLG
1739 */
1da177e4
LT
1740int dev_queue_xmit(struct sk_buff *skb)
1741{
1742 struct net_device *dev = skb->dev;
dc2b4847 1743 struct netdev_queue *txq;
1da177e4
LT
1744 struct Qdisc *q;
1745 int rc = -ENOMEM;
1746
f6a78bfc
HX
1747 /* GSO will handle the following emulations directly. */
1748 if (netif_needs_gso(dev, skb))
1749 goto gso;
1750
1da177e4
LT
1751 if (skb_shinfo(skb)->frag_list &&
1752 !(dev->features & NETIF_F_FRAGLIST) &&
364c6bad 1753 __skb_linearize(skb))
1da177e4
LT
1754 goto out_kfree_skb;
1755
1756 /* Fragmented skb is linearized if device does not support SG,
1757 * or if at least one of fragments is in highmem and device
1758 * does not support DMA from it.
1759 */
1760 if (skb_shinfo(skb)->nr_frags &&
1761 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
364c6bad 1762 __skb_linearize(skb))
1da177e4
LT
1763 goto out_kfree_skb;
1764
1765 /* If packet is not checksummed and device does not support
1766 * checksumming for this protocol, complete checksumming here.
1767 */
663ead3b
HX
1768 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1769 skb_set_transport_header(skb, skb->csum_start -
1770 skb_headroom(skb));
6de329e2
BH
1771 if (!dev_can_checksum(dev, skb) && skb_checksum_help(skb))
1772 goto out_kfree_skb;
663ead3b 1773 }
1da177e4 1774
f6a78bfc 1775gso:
4ec93edb
YH
1776 /* Disable soft irqs for various locks below. Also
1777 * stops preemption for RCU.
1da177e4 1778 */
4ec93edb 1779 rcu_read_lock_bh();
1da177e4 1780
eae792b7 1781 txq = dev_pick_tx(dev, skb);
b0e1e646 1782 q = rcu_dereference(txq->qdisc);
37437bb2 1783
1da177e4
LT
1784#ifdef CONFIG_NET_CLS_ACT
1785 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1786#endif
1787 if (q->enqueue) {
37437bb2
DM
1788 spinlock_t *root_lock = qdisc_root_lock(q);
1789
1790 spin_lock(root_lock);
1791
5f86173b 1792 rc = qdisc_enqueue_root(skb, q);
37437bb2
DM
1793 qdisc_run(q);
1794
1795 spin_unlock(root_lock);
1796
1797 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1798 goto out;
1da177e4
LT
1799 }
1800
1801 /* The device has no queue. Common case for software devices:
1802 loopback, all the sorts of tunnels...
1803
932ff279
HX
1804 Really, it is unlikely that netif_tx_lock protection is necessary
1805 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1da177e4
LT
1806 counters.)
1807 However, it is possible, that they rely on protection
1808 made by us here.
1809
1810 Check this and shot the lock. It is not prone from deadlocks.
1811 Either shot noqueue qdisc, it is even simpler 8)
1812 */
1813 if (dev->flags & IFF_UP) {
1814 int cpu = smp_processor_id(); /* ok because BHs are off */
1815
c773e847 1816 if (txq->xmit_lock_owner != cpu) {
1da177e4 1817
c773e847 1818 HARD_TX_LOCK(dev, txq, cpu);
1da177e4 1819
fd2ea0a7 1820 if (!netif_tx_queue_stopped(txq)) {
1da177e4 1821 rc = 0;
fd2ea0a7 1822 if (!dev_hard_start_xmit(skb, dev, txq)) {
c773e847 1823 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
1824 goto out;
1825 }
1826 }
c773e847 1827 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
1828 if (net_ratelimit())
1829 printk(KERN_CRIT "Virtual device %s asks to "
1830 "queue packet!\n", dev->name);
1831 } else {
1832 /* Recursion is detected! It is possible,
1833 * unfortunately */
1834 if (net_ratelimit())
1835 printk(KERN_CRIT "Dead loop on virtual device "
1836 "%s, fix it urgently!\n", dev->name);
1837 }
1838 }
1839
1840 rc = -ENETDOWN;
d4828d85 1841 rcu_read_unlock_bh();
1da177e4
LT
1842
1843out_kfree_skb:
1844 kfree_skb(skb);
1845 return rc;
1846out:
d4828d85 1847 rcu_read_unlock_bh();
1da177e4
LT
1848 return rc;
1849}
1850
1851
1852/*=======================================================================
1853 Receiver routines
1854 =======================================================================*/
1855
6b2bedc3
SH
1856int netdev_max_backlog __read_mostly = 1000;
1857int netdev_budget __read_mostly = 300;
1858int weight_p __read_mostly = 64; /* old backlog weight */
1da177e4
LT
1859
1860DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1861
1862
1da177e4
LT
1863/**
1864 * netif_rx - post buffer to the network code
1865 * @skb: buffer to post
1866 *
1867 * This function receives a packet from a device driver and queues it for
1868 * the upper (protocol) levels to process. It always succeeds. The buffer
1869 * may be dropped during processing for congestion control or by the
1870 * protocol layers.
1871 *
1872 * return values:
1873 * NET_RX_SUCCESS (no congestion)
1da177e4
LT
1874 * NET_RX_DROP (packet was dropped)
1875 *
1876 */
1877
1878int netif_rx(struct sk_buff *skb)
1879{
1da177e4
LT
1880 struct softnet_data *queue;
1881 unsigned long flags;
1882
1883 /* if netpoll wants it, pretend we never saw it */
1884 if (netpoll_rx(skb))
1885 return NET_RX_DROP;
1886
b7aa0bf7 1887 if (!skb->tstamp.tv64)
a61bbcf2 1888 net_timestamp(skb);
1da177e4
LT
1889
1890 /*
1891 * The code is rearranged so that the path is the most
1892 * short when CPU is congested, but is still operating.
1893 */
1894 local_irq_save(flags);
1da177e4
LT
1895 queue = &__get_cpu_var(softnet_data);
1896
1897 __get_cpu_var(netdev_rx_stat).total++;
1898 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1899 if (queue->input_pkt_queue.qlen) {
1da177e4
LT
1900enqueue:
1901 dev_hold(skb->dev);
1902 __skb_queue_tail(&queue->input_pkt_queue, skb);
1da177e4 1903 local_irq_restore(flags);
34008d8c 1904 return NET_RX_SUCCESS;
1da177e4
LT
1905 }
1906
bea3348e 1907 napi_schedule(&queue->backlog);
1da177e4
LT
1908 goto enqueue;
1909 }
1910
1da177e4
LT
1911 __get_cpu_var(netdev_rx_stat).dropped++;
1912 local_irq_restore(flags);
1913
1914 kfree_skb(skb);
1915 return NET_RX_DROP;
1916}
1917
1918int netif_rx_ni(struct sk_buff *skb)
1919{
1920 int err;
1921
1922 preempt_disable();
1923 err = netif_rx(skb);
1924 if (local_softirq_pending())
1925 do_softirq();
1926 preempt_enable();
1927
1928 return err;
1929}
1930
1931EXPORT_SYMBOL(netif_rx_ni);
1932
f2ccd8fa 1933static inline struct net_device *skb_bond(struct sk_buff *skb)
1da177e4
LT
1934{
1935 struct net_device *dev = skb->dev;
1936
8f903c70 1937 if (dev->master) {
7ea49ed7 1938 if (skb_bond_should_drop(skb)) {
8f903c70
JV
1939 kfree_skb(skb);
1940 return NULL;
1941 }
1da177e4 1942 skb->dev = dev->master;
8f903c70 1943 }
f2ccd8fa
DM
1944
1945 return dev;
1da177e4
LT
1946}
1947
bea3348e 1948
1da177e4
LT
1949static void net_tx_action(struct softirq_action *h)
1950{
1951 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1952
1953 if (sd->completion_queue) {
1954 struct sk_buff *clist;
1955
1956 local_irq_disable();
1957 clist = sd->completion_queue;
1958 sd->completion_queue = NULL;
1959 local_irq_enable();
1960
1961 while (clist) {
1962 struct sk_buff *skb = clist;
1963 clist = clist->next;
1964
1965 BUG_TRAP(!atomic_read(&skb->users));
1966 __kfree_skb(skb);
1967 }
1968 }
1969
1970 if (sd->output_queue) {
37437bb2 1971 struct Qdisc *head;
1da177e4
LT
1972
1973 local_irq_disable();
1974 head = sd->output_queue;
1975 sd->output_queue = NULL;
1976 local_irq_enable();
1977
1978 while (head) {
37437bb2
DM
1979 struct Qdisc *q = head;
1980 spinlock_t *root_lock;
1981
1da177e4
LT
1982 head = head->next_sched;
1983
1984 smp_mb__before_clear_bit();
37437bb2 1985 clear_bit(__QDISC_STATE_SCHED, &q->state);
1da177e4 1986
37437bb2
DM
1987 root_lock = qdisc_root_lock(q);
1988 if (spin_trylock(root_lock)) {
1989 qdisc_run(q);
1990 spin_unlock(root_lock);
1da177e4 1991 } else {
37437bb2 1992 __netif_schedule(q);
1da177e4
LT
1993 }
1994 }
1995 }
1996}
1997
6f05f629
SH
1998static inline int deliver_skb(struct sk_buff *skb,
1999 struct packet_type *pt_prev,
2000 struct net_device *orig_dev)
1da177e4
LT
2001{
2002 atomic_inc(&skb->users);
f2ccd8fa 2003 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1da177e4
LT
2004}
2005
2006#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
6229e362 2007/* These hooks defined here for ATM */
1da177e4
LT
2008struct net_bridge;
2009struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
2010 unsigned char *addr);
6229e362 2011void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
1da177e4 2012
6229e362
SH
2013/*
2014 * If bridge module is loaded call bridging hook.
2015 * returns NULL if packet was consumed.
2016 */
2017struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
2018 struct sk_buff *skb) __read_mostly;
2019static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
2020 struct packet_type **pt_prev, int *ret,
2021 struct net_device *orig_dev)
1da177e4
LT
2022{
2023 struct net_bridge_port *port;
2024
6229e362
SH
2025 if (skb->pkt_type == PACKET_LOOPBACK ||
2026 (port = rcu_dereference(skb->dev->br_port)) == NULL)
2027 return skb;
1da177e4
LT
2028
2029 if (*pt_prev) {
6229e362 2030 *ret = deliver_skb(skb, *pt_prev, orig_dev);
1da177e4 2031 *pt_prev = NULL;
4ec93edb
YH
2032 }
2033
6229e362 2034 return br_handle_frame_hook(port, skb);
1da177e4
LT
2035}
2036#else
6229e362 2037#define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
1da177e4
LT
2038#endif
2039
b863ceb7
PM
2040#if defined(CONFIG_MACVLAN) || defined(CONFIG_MACVLAN_MODULE)
2041struct sk_buff *(*macvlan_handle_frame_hook)(struct sk_buff *skb) __read_mostly;
2042EXPORT_SYMBOL_GPL(macvlan_handle_frame_hook);
2043
2044static inline struct sk_buff *handle_macvlan(struct sk_buff *skb,
2045 struct packet_type **pt_prev,
2046 int *ret,
2047 struct net_device *orig_dev)
2048{
2049 if (skb->dev->macvlan_port == NULL)
2050 return skb;
2051
2052 if (*pt_prev) {
2053 *ret = deliver_skb(skb, *pt_prev, orig_dev);
2054 *pt_prev = NULL;
2055 }
2056 return macvlan_handle_frame_hook(skb);
2057}
2058#else
2059#define handle_macvlan(skb, pt_prev, ret, orig_dev) (skb)
2060#endif
2061
1da177e4
LT
2062#ifdef CONFIG_NET_CLS_ACT
2063/* TODO: Maybe we should just force sch_ingress to be compiled in
2064 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
2065 * a compare and 2 stores extra right now if we dont have it on
2066 * but have CONFIG_NET_CLS_ACT
4ec93edb 2067 * NOTE: This doesnt stop any functionality; if you dont have
1da177e4
LT
2068 * the ingress scheduler, you just cant add policies on ingress.
2069 *
2070 */
4ec93edb 2071static int ing_filter(struct sk_buff *skb)
1da177e4 2072{
1da177e4 2073 struct net_device *dev = skb->dev;
f697c3e8 2074 u32 ttl = G_TC_RTTL(skb->tc_verd);
555353cf
DM
2075 struct netdev_queue *rxq;
2076 int result = TC_ACT_OK;
2077 struct Qdisc *q;
4ec93edb 2078
f697c3e8
HX
2079 if (MAX_RED_LOOP < ttl++) {
2080 printk(KERN_WARNING
2081 "Redir loop detected Dropping packet (%d->%d)\n",
2082 skb->iif, dev->ifindex);
2083 return TC_ACT_SHOT;
2084 }
1da177e4 2085
f697c3e8
HX
2086 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
2087 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
1da177e4 2088
555353cf
DM
2089 rxq = &dev->rx_queue;
2090
83874000
DM
2091 q = rxq->qdisc;
2092 if (q) {
2093 spin_lock(qdisc_lock(q));
5f86173b 2094 result = qdisc_enqueue_root(skb, q);
83874000
DM
2095 spin_unlock(qdisc_lock(q));
2096 }
f697c3e8
HX
2097
2098 return result;
2099}
86e65da9 2100
f697c3e8
HX
2101static inline struct sk_buff *handle_ing(struct sk_buff *skb,
2102 struct packet_type **pt_prev,
2103 int *ret, struct net_device *orig_dev)
2104{
816f3258 2105 if (!skb->dev->rx_queue.qdisc)
f697c3e8 2106 goto out;
1da177e4 2107
f697c3e8
HX
2108 if (*pt_prev) {
2109 *ret = deliver_skb(skb, *pt_prev, orig_dev);
2110 *pt_prev = NULL;
2111 } else {
2112 /* Huh? Why does turning on AF_PACKET affect this? */
2113 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1da177e4
LT
2114 }
2115
f697c3e8
HX
2116 switch (ing_filter(skb)) {
2117 case TC_ACT_SHOT:
2118 case TC_ACT_STOLEN:
2119 kfree_skb(skb);
2120 return NULL;
2121 }
2122
2123out:
2124 skb->tc_verd = 0;
2125 return skb;
1da177e4
LT
2126}
2127#endif
2128
bc1d0411
PM
2129/*
2130 * netif_nit_deliver - deliver received packets to network taps
2131 * @skb: buffer
2132 *
2133 * This function is used to deliver incoming packets to network
2134 * taps. It should be used when the normal netif_receive_skb path
2135 * is bypassed, for example because of VLAN acceleration.
2136 */
2137void netif_nit_deliver(struct sk_buff *skb)
2138{
2139 struct packet_type *ptype;
2140
2141 if (list_empty(&ptype_all))
2142 return;
2143
2144 skb_reset_network_header(skb);
2145 skb_reset_transport_header(skb);
2146 skb->mac_len = skb->network_header - skb->mac_header;
2147
2148 rcu_read_lock();
2149 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2150 if (!ptype->dev || ptype->dev == skb->dev)
2151 deliver_skb(skb, ptype, skb->dev);
2152 }
2153 rcu_read_unlock();
2154}
2155
3b582cc1
SH
2156/**
2157 * netif_receive_skb - process receive buffer from network
2158 * @skb: buffer to process
2159 *
2160 * netif_receive_skb() is the main receive data processing function.
2161 * It always succeeds. The buffer may be dropped during processing
2162 * for congestion control or by the protocol layers.
2163 *
2164 * This function may only be called from softirq context and interrupts
2165 * should be enabled.
2166 *
2167 * Return values (usually ignored):
2168 * NET_RX_SUCCESS: no congestion
2169 * NET_RX_DROP: packet was dropped
2170 */
1da177e4
LT
2171int netif_receive_skb(struct sk_buff *skb)
2172{
2173 struct packet_type *ptype, *pt_prev;
f2ccd8fa 2174 struct net_device *orig_dev;
1da177e4 2175 int ret = NET_RX_DROP;
252e3346 2176 __be16 type;
1da177e4
LT
2177
2178 /* if we've gotten here through NAPI, check netpoll */
bea3348e 2179 if (netpoll_receive_skb(skb))
1da177e4
LT
2180 return NET_RX_DROP;
2181
b7aa0bf7 2182 if (!skb->tstamp.tv64)
a61bbcf2 2183 net_timestamp(skb);
1da177e4 2184
c01003c2
PM
2185 if (!skb->iif)
2186 skb->iif = skb->dev->ifindex;
86e65da9 2187
f2ccd8fa 2188 orig_dev = skb_bond(skb);
1da177e4 2189
8f903c70
JV
2190 if (!orig_dev)
2191 return NET_RX_DROP;
2192
1da177e4
LT
2193 __get_cpu_var(netdev_rx_stat).total++;
2194
c1d2bbe1 2195 skb_reset_network_header(skb);
badff6d0 2196 skb_reset_transport_header(skb);
b0e380b1 2197 skb->mac_len = skb->network_header - skb->mac_header;
1da177e4
LT
2198
2199 pt_prev = NULL;
2200
2201 rcu_read_lock();
2202
b9f75f45
EB
2203 /* Don't receive packets in an exiting network namespace */
2204 if (!net_alive(dev_net(skb->dev)))
2205 goto out;
2206
1da177e4
LT
2207#ifdef CONFIG_NET_CLS_ACT
2208 if (skb->tc_verd & TC_NCLS) {
2209 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
2210 goto ncls;
2211 }
2212#endif
2213
2214 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2215 if (!ptype->dev || ptype->dev == skb->dev) {
4ec93edb 2216 if (pt_prev)
f2ccd8fa 2217 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
2218 pt_prev = ptype;
2219 }
2220 }
2221
2222#ifdef CONFIG_NET_CLS_ACT
f697c3e8
HX
2223 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
2224 if (!skb)
1da177e4 2225 goto out;
1da177e4
LT
2226ncls:
2227#endif
2228
6229e362 2229 skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
b863ceb7
PM
2230 if (!skb)
2231 goto out;
2232 skb = handle_macvlan(skb, &pt_prev, &ret, orig_dev);
6229e362 2233 if (!skb)
1da177e4
LT
2234 goto out;
2235
2236 type = skb->protocol;
82d8a867
PE
2237 list_for_each_entry_rcu(ptype,
2238 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
1da177e4
LT
2239 if (ptype->type == type &&
2240 (!ptype->dev || ptype->dev == skb->dev)) {
4ec93edb 2241 if (pt_prev)
f2ccd8fa 2242 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
2243 pt_prev = ptype;
2244 }
2245 }
2246
2247 if (pt_prev) {
f2ccd8fa 2248 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1da177e4
LT
2249 } else {
2250 kfree_skb(skb);
2251 /* Jamal, now you will not able to escape explaining
2252 * me how you were going to use this. :-)
2253 */
2254 ret = NET_RX_DROP;
2255 }
2256
2257out:
2258 rcu_read_unlock();
2259 return ret;
2260}
2261
bea3348e 2262static int process_backlog(struct napi_struct *napi, int quota)
1da177e4
LT
2263{
2264 int work = 0;
1da177e4
LT
2265 struct softnet_data *queue = &__get_cpu_var(softnet_data);
2266 unsigned long start_time = jiffies;
2267
bea3348e
SH
2268 napi->weight = weight_p;
2269 do {
1da177e4
LT
2270 struct sk_buff *skb;
2271 struct net_device *dev;
2272
2273 local_irq_disable();
2274 skb = __skb_dequeue(&queue->input_pkt_queue);
bea3348e
SH
2275 if (!skb) {
2276 __napi_complete(napi);
2277 local_irq_enable();
2278 break;
2279 }
2280
1da177e4
LT
2281 local_irq_enable();
2282
2283 dev = skb->dev;
2284
2285 netif_receive_skb(skb);
2286
2287 dev_put(dev);
bea3348e 2288 } while (++work < quota && jiffies == start_time);
1da177e4 2289
bea3348e
SH
2290 return work;
2291}
1da177e4 2292
bea3348e
SH
2293/**
2294 * __napi_schedule - schedule for receive
c4ea43c5 2295 * @n: entry to schedule
bea3348e
SH
2296 *
2297 * The entry's receive function will be scheduled to run
2298 */
b5606c2d 2299void __napi_schedule(struct napi_struct *n)
bea3348e
SH
2300{
2301 unsigned long flags;
1da177e4 2302
bea3348e
SH
2303 local_irq_save(flags);
2304 list_add_tail(&n->poll_list, &__get_cpu_var(softnet_data).poll_list);
2305 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2306 local_irq_restore(flags);
1da177e4 2307}
bea3348e
SH
2308EXPORT_SYMBOL(__napi_schedule);
2309
1da177e4
LT
2310
2311static void net_rx_action(struct softirq_action *h)
2312{
bea3348e 2313 struct list_head *list = &__get_cpu_var(softnet_data).poll_list;
1da177e4 2314 unsigned long start_time = jiffies;
51b0bded 2315 int budget = netdev_budget;
53fb95d3
MM
2316 void *have;
2317
1da177e4
LT
2318 local_irq_disable();
2319
bea3348e
SH
2320 while (!list_empty(list)) {
2321 struct napi_struct *n;
2322 int work, weight;
1da177e4 2323
bea3348e
SH
2324 /* If softirq window is exhuasted then punt.
2325 *
2326 * Note that this is a slight policy change from the
2327 * previous NAPI code, which would allow up to 2
2328 * jiffies to pass before breaking out. The test
2329 * used to be "jiffies - start_time > 1".
2330 */
2331 if (unlikely(budget <= 0 || jiffies != start_time))
1da177e4
LT
2332 goto softnet_break;
2333
2334 local_irq_enable();
2335
bea3348e
SH
2336 /* Even though interrupts have been re-enabled, this
2337 * access is safe because interrupts can only add new
2338 * entries to the tail of this list, and only ->poll()
2339 * calls can remove this head entry from the list.
2340 */
2341 n = list_entry(list->next, struct napi_struct, poll_list);
1da177e4 2342
bea3348e
SH
2343 have = netpoll_poll_lock(n);
2344
2345 weight = n->weight;
2346
0a7606c1
DM
2347 /* This NAPI_STATE_SCHED test is for avoiding a race
2348 * with netpoll's poll_napi(). Only the entity which
2349 * obtains the lock and sees NAPI_STATE_SCHED set will
2350 * actually make the ->poll() call. Therefore we avoid
2351 * accidently calling ->poll() when NAPI is not scheduled.
2352 */
2353 work = 0;
2354 if (test_bit(NAPI_STATE_SCHED, &n->state))
2355 work = n->poll(n, weight);
bea3348e
SH
2356
2357 WARN_ON_ONCE(work > weight);
2358
2359 budget -= work;
2360
2361 local_irq_disable();
2362
2363 /* Drivers must not modify the NAPI state if they
2364 * consume the entire weight. In such cases this code
2365 * still "owns" the NAPI instance and therefore can
2366 * move the instance around on the list at-will.
2367 */
fed17f30
DM
2368 if (unlikely(work == weight)) {
2369 if (unlikely(napi_disable_pending(n)))
2370 __napi_complete(n);
2371 else
2372 list_move_tail(&n->poll_list, list);
2373 }
bea3348e
SH
2374
2375 netpoll_poll_unlock(have);
1da177e4
LT
2376 }
2377out:
515e06c4 2378 local_irq_enable();
bea3348e 2379
db217334
CL
2380#ifdef CONFIG_NET_DMA
2381 /*
2382 * There may not be any more sk_buffs coming right now, so push
2383 * any pending DMA copies to hardware
2384 */
d379b01e
DW
2385 if (!cpus_empty(net_dma.channel_mask)) {
2386 int chan_idx;
2387 for_each_cpu_mask(chan_idx, net_dma.channel_mask) {
2388 struct dma_chan *chan = net_dma.channels[chan_idx];
2389 if (chan)
2390 dma_async_memcpy_issue_pending(chan);
2391 }
db217334
CL
2392 }
2393#endif
bea3348e 2394
1da177e4
LT
2395 return;
2396
2397softnet_break:
2398 __get_cpu_var(netdev_rx_stat).time_squeeze++;
2399 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2400 goto out;
2401}
2402
2403static gifconf_func_t * gifconf_list [NPROTO];
2404
2405/**
2406 * register_gifconf - register a SIOCGIF handler
2407 * @family: Address family
2408 * @gifconf: Function handler
2409 *
2410 * Register protocol dependent address dumping routines. The handler
2411 * that is passed must not be freed or reused until it has been replaced
2412 * by another handler.
2413 */
2414int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2415{
2416 if (family >= NPROTO)
2417 return -EINVAL;
2418 gifconf_list[family] = gifconf;
2419 return 0;
2420}
2421
2422
2423/*
2424 * Map an interface index to its name (SIOCGIFNAME)
2425 */
2426
2427/*
2428 * We need this ioctl for efficient implementation of the
2429 * if_indextoname() function required by the IPv6 API. Without
2430 * it, we would have to search all the interfaces to find a
2431 * match. --pb
2432 */
2433
881d966b 2434static int dev_ifname(struct net *net, struct ifreq __user *arg)
1da177e4
LT
2435{
2436 struct net_device *dev;
2437 struct ifreq ifr;
2438
2439 /*
2440 * Fetch the caller's info block.
2441 */
2442
2443 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2444 return -EFAULT;
2445
2446 read_lock(&dev_base_lock);
881d966b 2447 dev = __dev_get_by_index(net, ifr.ifr_ifindex);
1da177e4
LT
2448 if (!dev) {
2449 read_unlock(&dev_base_lock);
2450 return -ENODEV;
2451 }
2452
2453 strcpy(ifr.ifr_name, dev->name);
2454 read_unlock(&dev_base_lock);
2455
2456 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2457 return -EFAULT;
2458 return 0;
2459}
2460
2461/*
2462 * Perform a SIOCGIFCONF call. This structure will change
2463 * size eventually, and there is nothing I can do about it.
2464 * Thus we will need a 'compatibility mode'.
2465 */
2466
881d966b 2467static int dev_ifconf(struct net *net, char __user *arg)
1da177e4
LT
2468{
2469 struct ifconf ifc;
2470 struct net_device *dev;
2471 char __user *pos;
2472 int len;
2473 int total;
2474 int i;
2475
2476 /*
2477 * Fetch the caller's info block.
2478 */
2479
2480 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2481 return -EFAULT;
2482
2483 pos = ifc.ifc_buf;
2484 len = ifc.ifc_len;
2485
2486 /*
2487 * Loop over the interfaces, and write an info block for each.
2488 */
2489
2490 total = 0;
881d966b 2491 for_each_netdev(net, dev) {
1da177e4
LT
2492 for (i = 0; i < NPROTO; i++) {
2493 if (gifconf_list[i]) {
2494 int done;
2495 if (!pos)
2496 done = gifconf_list[i](dev, NULL, 0);
2497 else
2498 done = gifconf_list[i](dev, pos + total,
2499 len - total);
2500 if (done < 0)
2501 return -EFAULT;
2502 total += done;
2503 }
2504 }
4ec93edb 2505 }
1da177e4
LT
2506
2507 /*
2508 * All done. Write the updated control block back to the caller.
2509 */
2510 ifc.ifc_len = total;
2511
2512 /*
2513 * Both BSD and Solaris return 0 here, so we do too.
2514 */
2515 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2516}
2517
2518#ifdef CONFIG_PROC_FS
2519/*
2520 * This is invoked by the /proc filesystem handler to display a device
2521 * in detail.
2522 */
7562f876 2523void *dev_seq_start(struct seq_file *seq, loff_t *pos)
9a429c49 2524 __acquires(dev_base_lock)
1da177e4 2525{
e372c414 2526 struct net *net = seq_file_net(seq);
7562f876 2527 loff_t off;
1da177e4 2528 struct net_device *dev;
1da177e4 2529
7562f876
PE
2530 read_lock(&dev_base_lock);
2531 if (!*pos)
2532 return SEQ_START_TOKEN;
1da177e4 2533
7562f876 2534 off = 1;
881d966b 2535 for_each_netdev(net, dev)
7562f876
PE
2536 if (off++ == *pos)
2537 return dev;
1da177e4 2538
7562f876 2539 return NULL;
1da177e4
LT
2540}
2541
2542void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2543{
e372c414 2544 struct net *net = seq_file_net(seq);
1da177e4 2545 ++*pos;
7562f876 2546 return v == SEQ_START_TOKEN ?
881d966b 2547 first_net_device(net) : next_net_device((struct net_device *)v);
1da177e4
LT
2548}
2549
2550void dev_seq_stop(struct seq_file *seq, void *v)
9a429c49 2551 __releases(dev_base_lock)
1da177e4
LT
2552{
2553 read_unlock(&dev_base_lock);
2554}
2555
2556static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2557{
c45d286e 2558 struct net_device_stats *stats = dev->get_stats(dev);
1da177e4 2559
5a1b5898
RR
2560 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2561 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2562 dev->name, stats->rx_bytes, stats->rx_packets,
2563 stats->rx_errors,
2564 stats->rx_dropped + stats->rx_missed_errors,
2565 stats->rx_fifo_errors,
2566 stats->rx_length_errors + stats->rx_over_errors +
2567 stats->rx_crc_errors + stats->rx_frame_errors,
2568 stats->rx_compressed, stats->multicast,
2569 stats->tx_bytes, stats->tx_packets,
2570 stats->tx_errors, stats->tx_dropped,
2571 stats->tx_fifo_errors, stats->collisions,
2572 stats->tx_carrier_errors +
2573 stats->tx_aborted_errors +
2574 stats->tx_window_errors +
2575 stats->tx_heartbeat_errors,
2576 stats->tx_compressed);
1da177e4
LT
2577}
2578
2579/*
2580 * Called from the PROCfs module. This now uses the new arbitrary sized
2581 * /proc/net interface to create /proc/net/dev
2582 */
2583static int dev_seq_show(struct seq_file *seq, void *v)
2584{
2585 if (v == SEQ_START_TOKEN)
2586 seq_puts(seq, "Inter-| Receive "
2587 " | Transmit\n"
2588 " face |bytes packets errs drop fifo frame "
2589 "compressed multicast|bytes packets errs "
2590 "drop fifo colls carrier compressed\n");
2591 else
2592 dev_seq_printf_stats(seq, v);
2593 return 0;
2594}
2595
2596static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2597{
2598 struct netif_rx_stats *rc = NULL;
2599
0c0b0aca 2600 while (*pos < nr_cpu_ids)
4ec93edb 2601 if (cpu_online(*pos)) {
1da177e4
LT
2602 rc = &per_cpu(netdev_rx_stat, *pos);
2603 break;
2604 } else
2605 ++*pos;
2606 return rc;
2607}
2608
2609static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2610{
2611 return softnet_get_online(pos);
2612}
2613
2614static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2615{
2616 ++*pos;
2617 return softnet_get_online(pos);
2618}
2619
2620static void softnet_seq_stop(struct seq_file *seq, void *v)
2621{
2622}
2623
2624static int softnet_seq_show(struct seq_file *seq, void *v)
2625{
2626 struct netif_rx_stats *s = v;
2627
2628 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
31aa02c5 2629 s->total, s->dropped, s->time_squeeze, 0,
c1ebcdb8
SH
2630 0, 0, 0, 0, /* was fastroute */
2631 s->cpu_collision );
1da177e4
LT
2632 return 0;
2633}
2634
f690808e 2635static const struct seq_operations dev_seq_ops = {
1da177e4
LT
2636 .start = dev_seq_start,
2637 .next = dev_seq_next,
2638 .stop = dev_seq_stop,
2639 .show = dev_seq_show,
2640};
2641
2642static int dev_seq_open(struct inode *inode, struct file *file)
2643{
e372c414
DL
2644 return seq_open_net(inode, file, &dev_seq_ops,
2645 sizeof(struct seq_net_private));
1da177e4
LT
2646}
2647
9a32144e 2648static const struct file_operations dev_seq_fops = {
1da177e4
LT
2649 .owner = THIS_MODULE,
2650 .open = dev_seq_open,
2651 .read = seq_read,
2652 .llseek = seq_lseek,
e372c414 2653 .release = seq_release_net,
1da177e4
LT
2654};
2655
f690808e 2656static const struct seq_operations softnet_seq_ops = {
1da177e4
LT
2657 .start = softnet_seq_start,
2658 .next = softnet_seq_next,
2659 .stop = softnet_seq_stop,
2660 .show = softnet_seq_show,
2661};
2662
2663static int softnet_seq_open(struct inode *inode, struct file *file)
2664{
2665 return seq_open(file, &softnet_seq_ops);
2666}
2667
9a32144e 2668static const struct file_operations softnet_seq_fops = {
1da177e4
LT
2669 .owner = THIS_MODULE,
2670 .open = softnet_seq_open,
2671 .read = seq_read,
2672 .llseek = seq_lseek,
2673 .release = seq_release,
2674};
2675
0e1256ff
SH
2676static void *ptype_get_idx(loff_t pos)
2677{
2678 struct packet_type *pt = NULL;
2679 loff_t i = 0;
2680 int t;
2681
2682 list_for_each_entry_rcu(pt, &ptype_all, list) {
2683 if (i == pos)
2684 return pt;
2685 ++i;
2686 }
2687
82d8a867 2688 for (t = 0; t < PTYPE_HASH_SIZE; t++) {
0e1256ff
SH
2689 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
2690 if (i == pos)
2691 return pt;
2692 ++i;
2693 }
2694 }
2695 return NULL;
2696}
2697
2698static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
72348a42 2699 __acquires(RCU)
0e1256ff
SH
2700{
2701 rcu_read_lock();
2702 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
2703}
2704
2705static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2706{
2707 struct packet_type *pt;
2708 struct list_head *nxt;
2709 int hash;
2710
2711 ++*pos;
2712 if (v == SEQ_START_TOKEN)
2713 return ptype_get_idx(0);
2714
2715 pt = v;
2716 nxt = pt->list.next;
2717 if (pt->type == htons(ETH_P_ALL)) {
2718 if (nxt != &ptype_all)
2719 goto found;
2720 hash = 0;
2721 nxt = ptype_base[0].next;
2722 } else
82d8a867 2723 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
0e1256ff
SH
2724
2725 while (nxt == &ptype_base[hash]) {
82d8a867 2726 if (++hash >= PTYPE_HASH_SIZE)
0e1256ff
SH
2727 return NULL;
2728 nxt = ptype_base[hash].next;
2729 }
2730found:
2731 return list_entry(nxt, struct packet_type, list);
2732}
2733
2734static void ptype_seq_stop(struct seq_file *seq, void *v)
72348a42 2735 __releases(RCU)
0e1256ff
SH
2736{
2737 rcu_read_unlock();
2738}
2739
2740static void ptype_seq_decode(struct seq_file *seq, void *sym)
2741{
2742#ifdef CONFIG_KALLSYMS
2743 unsigned long offset = 0, symsize;
2744 const char *symname;
2745 char *modname;
2746 char namebuf[128];
2747
2748 symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
2749 &modname, namebuf);
2750
2751 if (symname) {
2752 char *delim = ":";
2753
2754 if (!modname)
2755 modname = delim = "";
2756 seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
2757 symname, offset);
2758 return;
2759 }
2760#endif
2761
2762 seq_printf(seq, "[%p]", sym);
2763}
2764
2765static int ptype_seq_show(struct seq_file *seq, void *v)
2766{
2767 struct packet_type *pt = v;
2768
2769 if (v == SEQ_START_TOKEN)
2770 seq_puts(seq, "Type Device Function\n");
c346dca1 2771 else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
0e1256ff
SH
2772 if (pt->type == htons(ETH_P_ALL))
2773 seq_puts(seq, "ALL ");
2774 else
2775 seq_printf(seq, "%04x", ntohs(pt->type));
2776
2777 seq_printf(seq, " %-8s ",
2778 pt->dev ? pt->dev->name : "");
2779 ptype_seq_decode(seq, pt->func);
2780 seq_putc(seq, '\n');
2781 }
2782
2783 return 0;
2784}
2785
2786static const struct seq_operations ptype_seq_ops = {
2787 .start = ptype_seq_start,
2788 .next = ptype_seq_next,
2789 .stop = ptype_seq_stop,
2790 .show = ptype_seq_show,
2791};
2792
2793static int ptype_seq_open(struct inode *inode, struct file *file)
2794{
2feb27db
PE
2795 return seq_open_net(inode, file, &ptype_seq_ops,
2796 sizeof(struct seq_net_private));
0e1256ff
SH
2797}
2798
2799static const struct file_operations ptype_seq_fops = {
2800 .owner = THIS_MODULE,
2801 .open = ptype_seq_open,
2802 .read = seq_read,
2803 .llseek = seq_lseek,
2feb27db 2804 .release = seq_release_net,
0e1256ff
SH
2805};
2806
2807
4665079c 2808static int __net_init dev_proc_net_init(struct net *net)
1da177e4
LT
2809{
2810 int rc = -ENOMEM;
2811
881d966b 2812 if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
1da177e4 2813 goto out;
881d966b 2814 if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
1da177e4 2815 goto out_dev;
881d966b 2816 if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
457c4cbc 2817 goto out_softnet;
0e1256ff 2818
881d966b 2819 if (wext_proc_init(net))
457c4cbc 2820 goto out_ptype;
1da177e4
LT
2821 rc = 0;
2822out:
2823 return rc;
457c4cbc 2824out_ptype:
881d966b 2825 proc_net_remove(net, "ptype");
1da177e4 2826out_softnet:
881d966b 2827 proc_net_remove(net, "softnet_stat");
1da177e4 2828out_dev:
881d966b 2829 proc_net_remove(net, "dev");
1da177e4
LT
2830 goto out;
2831}
881d966b 2832
4665079c 2833static void __net_exit dev_proc_net_exit(struct net *net)
881d966b
EB
2834{
2835 wext_proc_exit(net);
2836
2837 proc_net_remove(net, "ptype");
2838 proc_net_remove(net, "softnet_stat");
2839 proc_net_remove(net, "dev");
2840}
2841
022cbae6 2842static struct pernet_operations __net_initdata dev_proc_ops = {
881d966b
EB
2843 .init = dev_proc_net_init,
2844 .exit = dev_proc_net_exit,
2845};
2846
2847static int __init dev_proc_init(void)
2848{
2849 return register_pernet_subsys(&dev_proc_ops);
2850}
1da177e4
LT
2851#else
2852#define dev_proc_init() 0
2853#endif /* CONFIG_PROC_FS */
2854
2855
2856/**
2857 * netdev_set_master - set up master/slave pair
2858 * @slave: slave device
2859 * @master: new master device
2860 *
2861 * Changes the master device of the slave. Pass %NULL to break the
2862 * bonding. The caller must hold the RTNL semaphore. On a failure
2863 * a negative errno code is returned. On success the reference counts
2864 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2865 * function returns zero.
2866 */
2867int netdev_set_master(struct net_device *slave, struct net_device *master)
2868{
2869 struct net_device *old = slave->master;
2870
2871 ASSERT_RTNL();
2872
2873 if (master) {
2874 if (old)
2875 return -EBUSY;
2876 dev_hold(master);
2877 }
2878
2879 slave->master = master;
4ec93edb 2880
1da177e4
LT
2881 synchronize_net();
2882
2883 if (old)
2884 dev_put(old);
2885
2886 if (master)
2887 slave->flags |= IFF_SLAVE;
2888 else
2889 slave->flags &= ~IFF_SLAVE;
2890
2891 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2892 return 0;
2893}
2894
dad9b335 2895static int __dev_set_promiscuity(struct net_device *dev, int inc)
1da177e4
LT
2896{
2897 unsigned short old_flags = dev->flags;
2898
24023451
PM
2899 ASSERT_RTNL();
2900
dad9b335
WC
2901 dev->flags |= IFF_PROMISC;
2902 dev->promiscuity += inc;
2903 if (dev->promiscuity == 0) {
2904 /*
2905 * Avoid overflow.
2906 * If inc causes overflow, untouch promisc and return error.
2907 */
2908 if (inc < 0)
2909 dev->flags &= ~IFF_PROMISC;
2910 else {
2911 dev->promiscuity -= inc;
2912 printk(KERN_WARNING "%s: promiscuity touches roof, "
2913 "set promiscuity failed, promiscuity feature "
2914 "of device might be broken.\n", dev->name);
2915 return -EOVERFLOW;
2916 }
2917 }
52609c0b 2918 if (dev->flags != old_flags) {
1da177e4
LT
2919 printk(KERN_INFO "device %s %s promiscuous mode\n",
2920 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
4ec93edb 2921 "left");
7759db82
KHK
2922 if (audit_enabled)
2923 audit_log(current->audit_context, GFP_ATOMIC,
2924 AUDIT_ANOM_PROMISCUOUS,
2925 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
2926 dev->name, (dev->flags & IFF_PROMISC),
2927 (old_flags & IFF_PROMISC),
2928 audit_get_loginuid(current),
2929 current->uid, current->gid,
2930 audit_get_sessionid(current));
24023451
PM
2931
2932 if (dev->change_rx_flags)
2933 dev->change_rx_flags(dev, IFF_PROMISC);
1da177e4 2934 }
dad9b335 2935 return 0;
1da177e4
LT
2936}
2937
4417da66
PM
2938/**
2939 * dev_set_promiscuity - update promiscuity count on a device
2940 * @dev: device
2941 * @inc: modifier
2942 *
2943 * Add or remove promiscuity from a device. While the count in the device
2944 * remains above zero the interface remains promiscuous. Once it hits zero
2945 * the device reverts back to normal filtering operation. A negative inc
2946 * value is used to drop promiscuity on the device.
dad9b335 2947 * Return 0 if successful or a negative errno code on error.
4417da66 2948 */
dad9b335 2949int dev_set_promiscuity(struct net_device *dev, int inc)
4417da66
PM
2950{
2951 unsigned short old_flags = dev->flags;
dad9b335 2952 int err;
4417da66 2953
dad9b335 2954 err = __dev_set_promiscuity(dev, inc);
4b5a698e 2955 if (err < 0)
dad9b335 2956 return err;
4417da66
PM
2957 if (dev->flags != old_flags)
2958 dev_set_rx_mode(dev);
dad9b335 2959 return err;
4417da66
PM
2960}
2961
1da177e4
LT
2962/**
2963 * dev_set_allmulti - update allmulti count on a device
2964 * @dev: device
2965 * @inc: modifier
2966 *
2967 * Add or remove reception of all multicast frames to a device. While the
2968 * count in the device remains above zero the interface remains listening
2969 * to all interfaces. Once it hits zero the device reverts back to normal
2970 * filtering operation. A negative @inc value is used to drop the counter
2971 * when releasing a resource needing all multicasts.
dad9b335 2972 * Return 0 if successful or a negative errno code on error.
1da177e4
LT
2973 */
2974
dad9b335 2975int dev_set_allmulti(struct net_device *dev, int inc)
1da177e4
LT
2976{
2977 unsigned short old_flags = dev->flags;
2978
24023451
PM
2979 ASSERT_RTNL();
2980
1da177e4 2981 dev->flags |= IFF_ALLMULTI;
dad9b335
WC
2982 dev->allmulti += inc;
2983 if (dev->allmulti == 0) {
2984 /*
2985 * Avoid overflow.
2986 * If inc causes overflow, untouch allmulti and return error.
2987 */
2988 if (inc < 0)
2989 dev->flags &= ~IFF_ALLMULTI;
2990 else {
2991 dev->allmulti -= inc;
2992 printk(KERN_WARNING "%s: allmulti touches roof, "
2993 "set allmulti failed, allmulti feature of "
2994 "device might be broken.\n", dev->name);
2995 return -EOVERFLOW;
2996 }
2997 }
24023451
PM
2998 if (dev->flags ^ old_flags) {
2999 if (dev->change_rx_flags)
3000 dev->change_rx_flags(dev, IFF_ALLMULTI);
4417da66 3001 dev_set_rx_mode(dev);
24023451 3002 }
dad9b335 3003 return 0;
4417da66
PM
3004}
3005
3006/*
3007 * Upload unicast and multicast address lists to device and
3008 * configure RX filtering. When the device doesn't support unicast
53ccaae1 3009 * filtering it is put in promiscuous mode while unicast addresses
4417da66
PM
3010 * are present.
3011 */
3012void __dev_set_rx_mode(struct net_device *dev)
3013{
3014 /* dev_open will call this function so the list will stay sane. */
3015 if (!(dev->flags&IFF_UP))
3016 return;
3017
3018 if (!netif_device_present(dev))
40b77c94 3019 return;
4417da66
PM
3020
3021 if (dev->set_rx_mode)
3022 dev->set_rx_mode(dev);
3023 else {
3024 /* Unicast addresses changes may only happen under the rtnl,
3025 * therefore calling __dev_set_promiscuity here is safe.
3026 */
3027 if (dev->uc_count > 0 && !dev->uc_promisc) {
3028 __dev_set_promiscuity(dev, 1);
3029 dev->uc_promisc = 1;
3030 } else if (dev->uc_count == 0 && dev->uc_promisc) {
3031 __dev_set_promiscuity(dev, -1);
3032 dev->uc_promisc = 0;
3033 }
3034
3035 if (dev->set_multicast_list)
3036 dev->set_multicast_list(dev);
3037 }
3038}
3039
3040void dev_set_rx_mode(struct net_device *dev)
3041{
b9e40857 3042 netif_addr_lock_bh(dev);
4417da66 3043 __dev_set_rx_mode(dev);
b9e40857 3044 netif_addr_unlock_bh(dev);
1da177e4
LT
3045}
3046
61cbc2fc
PM
3047int __dev_addr_delete(struct dev_addr_list **list, int *count,
3048 void *addr, int alen, int glbl)
bf742482
PM
3049{
3050 struct dev_addr_list *da;
3051
3052 for (; (da = *list) != NULL; list = &da->next) {
3053 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
3054 alen == da->da_addrlen) {
3055 if (glbl) {
3056 int old_glbl = da->da_gusers;
3057 da->da_gusers = 0;
3058 if (old_glbl == 0)
3059 break;
3060 }
3061 if (--da->da_users)
3062 return 0;
3063
3064 *list = da->next;
3065 kfree(da);
61cbc2fc 3066 (*count)--;
bf742482
PM
3067 return 0;
3068 }
3069 }
3070 return -ENOENT;
3071}
3072
61cbc2fc
PM
3073int __dev_addr_add(struct dev_addr_list **list, int *count,
3074 void *addr, int alen, int glbl)
bf742482
PM
3075{
3076 struct dev_addr_list *da;
3077
3078 for (da = *list; da != NULL; da = da->next) {
3079 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
3080 da->da_addrlen == alen) {
3081 if (glbl) {
3082 int old_glbl = da->da_gusers;
3083 da->da_gusers = 1;
3084 if (old_glbl)
3085 return 0;
3086 }
3087 da->da_users++;
3088 return 0;
3089 }
3090 }
3091
12aa343a 3092 da = kzalloc(sizeof(*da), GFP_ATOMIC);
bf742482
PM
3093 if (da == NULL)
3094 return -ENOMEM;
3095 memcpy(da->da_addr, addr, alen);
3096 da->da_addrlen = alen;
3097 da->da_users = 1;
3098 da->da_gusers = glbl ? 1 : 0;
3099 da->next = *list;
3100 *list = da;
61cbc2fc 3101 (*count)++;
bf742482
PM
3102 return 0;
3103}
3104
4417da66
PM
3105/**
3106 * dev_unicast_delete - Release secondary unicast address.
3107 * @dev: device
0ed72ec4
RD
3108 * @addr: address to delete
3109 * @alen: length of @addr
4417da66
PM
3110 *
3111 * Release reference to a secondary unicast address and remove it
0ed72ec4 3112 * from the device if the reference count drops to zero.
4417da66
PM
3113 *
3114 * The caller must hold the rtnl_mutex.
3115 */
3116int dev_unicast_delete(struct net_device *dev, void *addr, int alen)
3117{
3118 int err;
3119
3120 ASSERT_RTNL();
3121
b9e40857 3122 netif_addr_lock_bh(dev);
61cbc2fc
PM
3123 err = __dev_addr_delete(&dev->uc_list, &dev->uc_count, addr, alen, 0);
3124 if (!err)
4417da66 3125 __dev_set_rx_mode(dev);
b9e40857 3126 netif_addr_unlock_bh(dev);
4417da66
PM
3127 return err;
3128}
3129EXPORT_SYMBOL(dev_unicast_delete);
3130
3131/**
3132 * dev_unicast_add - add a secondary unicast address
3133 * @dev: device
5dbaec5d 3134 * @addr: address to add
0ed72ec4 3135 * @alen: length of @addr
4417da66
PM
3136 *
3137 * Add a secondary unicast address to the device or increase
3138 * the reference count if it already exists.
3139 *
3140 * The caller must hold the rtnl_mutex.
3141 */
3142int dev_unicast_add(struct net_device *dev, void *addr, int alen)
3143{
3144 int err;
3145
3146 ASSERT_RTNL();
3147
b9e40857 3148 netif_addr_lock_bh(dev);
61cbc2fc
PM
3149 err = __dev_addr_add(&dev->uc_list, &dev->uc_count, addr, alen, 0);
3150 if (!err)
4417da66 3151 __dev_set_rx_mode(dev);
b9e40857 3152 netif_addr_unlock_bh(dev);
4417da66
PM
3153 return err;
3154}
3155EXPORT_SYMBOL(dev_unicast_add);
3156
e83a2ea8
CL
3157int __dev_addr_sync(struct dev_addr_list **to, int *to_count,
3158 struct dev_addr_list **from, int *from_count)
3159{
3160 struct dev_addr_list *da, *next;
3161 int err = 0;
3162
3163 da = *from;
3164 while (da != NULL) {
3165 next = da->next;
3166 if (!da->da_synced) {
3167 err = __dev_addr_add(to, to_count,
3168 da->da_addr, da->da_addrlen, 0);
3169 if (err < 0)
3170 break;
3171 da->da_synced = 1;
3172 da->da_users++;
3173 } else if (da->da_users == 1) {
3174 __dev_addr_delete(to, to_count,
3175 da->da_addr, da->da_addrlen, 0);
3176 __dev_addr_delete(from, from_count,
3177 da->da_addr, da->da_addrlen, 0);
3178 }
3179 da = next;
3180 }
3181 return err;
3182}
3183
3184void __dev_addr_unsync(struct dev_addr_list **to, int *to_count,
3185 struct dev_addr_list **from, int *from_count)
3186{
3187 struct dev_addr_list *da, *next;
3188
3189 da = *from;
3190 while (da != NULL) {
3191 next = da->next;
3192 if (da->da_synced) {
3193 __dev_addr_delete(to, to_count,
3194 da->da_addr, da->da_addrlen, 0);
3195 da->da_synced = 0;
3196 __dev_addr_delete(from, from_count,
3197 da->da_addr, da->da_addrlen, 0);
3198 }
3199 da = next;
3200 }
3201}
3202
3203/**
3204 * dev_unicast_sync - Synchronize device's unicast list to another device
3205 * @to: destination device
3206 * @from: source device
3207 *
3208 * Add newly added addresses to the destination device and release
3209 * addresses that have no users left. The source device must be
3210 * locked by netif_tx_lock_bh.
3211 *
3212 * This function is intended to be called from the dev->set_rx_mode
3213 * function of layered software devices.
3214 */
3215int dev_unicast_sync(struct net_device *to, struct net_device *from)
3216{
3217 int err = 0;
3218
b9e40857 3219 netif_addr_lock_bh(to);
e83a2ea8
CL
3220 err = __dev_addr_sync(&to->uc_list, &to->uc_count,
3221 &from->uc_list, &from->uc_count);
3222 if (!err)
3223 __dev_set_rx_mode(to);
b9e40857 3224 netif_addr_unlock_bh(to);
e83a2ea8
CL
3225 return err;
3226}
3227EXPORT_SYMBOL(dev_unicast_sync);
3228
3229/**
bc2cda1e 3230 * dev_unicast_unsync - Remove synchronized addresses from the destination device
e83a2ea8
CL
3231 * @to: destination device
3232 * @from: source device
3233 *
3234 * Remove all addresses that were added to the destination device by
3235 * dev_unicast_sync(). This function is intended to be called from the
3236 * dev->stop function of layered software devices.
3237 */
3238void dev_unicast_unsync(struct net_device *to, struct net_device *from)
3239{
b9e40857 3240 netif_addr_lock_bh(from);
e308a5d8 3241 netif_addr_lock(to);
e83a2ea8
CL
3242
3243 __dev_addr_unsync(&to->uc_list, &to->uc_count,
3244 &from->uc_list, &from->uc_count);
3245 __dev_set_rx_mode(to);
3246
e308a5d8 3247 netif_addr_unlock(to);
b9e40857 3248 netif_addr_unlock_bh(from);
e83a2ea8
CL
3249}
3250EXPORT_SYMBOL(dev_unicast_unsync);
3251
12972621
DC
3252static void __dev_addr_discard(struct dev_addr_list **list)
3253{
3254 struct dev_addr_list *tmp;
3255
3256 while (*list != NULL) {
3257 tmp = *list;
3258 *list = tmp->next;
3259 if (tmp->da_users > tmp->da_gusers)
3260 printk("__dev_addr_discard: address leakage! "
3261 "da_users=%d\n", tmp->da_users);
3262 kfree(tmp);
3263 }
3264}
3265
26cc2522 3266static void dev_addr_discard(struct net_device *dev)
4417da66 3267{
b9e40857 3268 netif_addr_lock_bh(dev);
26cc2522 3269
4417da66
PM
3270 __dev_addr_discard(&dev->uc_list);
3271 dev->uc_count = 0;
4417da66 3272
456ad75c
DC
3273 __dev_addr_discard(&dev->mc_list);
3274 dev->mc_count = 0;
26cc2522 3275
b9e40857 3276 netif_addr_unlock_bh(dev);
456ad75c
DC
3277}
3278
1da177e4
LT
3279unsigned dev_get_flags(const struct net_device *dev)
3280{
3281 unsigned flags;
3282
3283 flags = (dev->flags & ~(IFF_PROMISC |
3284 IFF_ALLMULTI |
b00055aa
SR
3285 IFF_RUNNING |
3286 IFF_LOWER_UP |
3287 IFF_DORMANT)) |
1da177e4
LT
3288 (dev->gflags & (IFF_PROMISC |
3289 IFF_ALLMULTI));
3290
b00055aa
SR
3291 if (netif_running(dev)) {
3292 if (netif_oper_up(dev))
3293 flags |= IFF_RUNNING;
3294 if (netif_carrier_ok(dev))
3295 flags |= IFF_LOWER_UP;
3296 if (netif_dormant(dev))
3297 flags |= IFF_DORMANT;
3298 }
1da177e4
LT
3299
3300 return flags;
3301}
3302
3303int dev_change_flags(struct net_device *dev, unsigned flags)
3304{
7c355f53 3305 int ret, changes;
1da177e4
LT
3306 int old_flags = dev->flags;
3307
24023451
PM
3308 ASSERT_RTNL();
3309
1da177e4
LT
3310 /*
3311 * Set the flags on our device.
3312 */
3313
3314 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
3315 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
3316 IFF_AUTOMEDIA)) |
3317 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
3318 IFF_ALLMULTI));
3319
3320 /*
3321 * Load in the correct multicast list now the flags have changed.
3322 */
3323
0e91796e 3324 if (dev->change_rx_flags && (old_flags ^ flags) & IFF_MULTICAST)
24023451
PM
3325 dev->change_rx_flags(dev, IFF_MULTICAST);
3326
4417da66 3327 dev_set_rx_mode(dev);
1da177e4
LT
3328
3329 /*
3330 * Have we downed the interface. We handle IFF_UP ourselves
3331 * according to user attempts to set it, rather than blindly
3332 * setting it.
3333 */
3334
3335 ret = 0;
3336 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
3337 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
3338
3339 if (!ret)
4417da66 3340 dev_set_rx_mode(dev);
1da177e4
LT
3341 }
3342
3343 if (dev->flags & IFF_UP &&
3344 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
3345 IFF_VOLATILE)))
056925ab 3346 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1da177e4
LT
3347
3348 if ((flags ^ dev->gflags) & IFF_PROMISC) {
3349 int inc = (flags & IFF_PROMISC) ? +1 : -1;
3350 dev->gflags ^= IFF_PROMISC;
3351 dev_set_promiscuity(dev, inc);
3352 }
3353
3354 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
3355 is important. Some (broken) drivers set IFF_PROMISC, when
3356 IFF_ALLMULTI is requested not asking us and not reporting.
3357 */
3358 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
3359 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
3360 dev->gflags ^= IFF_ALLMULTI;
3361 dev_set_allmulti(dev, inc);
3362 }
3363
7c355f53
TG
3364 /* Exclude state transition flags, already notified */
3365 changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
3366 if (changes)
3367 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
1da177e4
LT
3368
3369 return ret;
3370}
3371
3372int dev_set_mtu(struct net_device *dev, int new_mtu)
3373{
3374 int err;
3375
3376 if (new_mtu == dev->mtu)
3377 return 0;
3378
3379 /* MTU must be positive. */
3380 if (new_mtu < 0)
3381 return -EINVAL;
3382
3383 if (!netif_device_present(dev))
3384 return -ENODEV;
3385
3386 err = 0;
3387 if (dev->change_mtu)
3388 err = dev->change_mtu(dev, new_mtu);
3389 else
3390 dev->mtu = new_mtu;
3391 if (!err && dev->flags & IFF_UP)
056925ab 3392 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
1da177e4
LT
3393 return err;
3394}
3395
3396int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
3397{
3398 int err;
3399
3400 if (!dev->set_mac_address)
3401 return -EOPNOTSUPP;
3402 if (sa->sa_family != dev->type)
3403 return -EINVAL;
3404 if (!netif_device_present(dev))
3405 return -ENODEV;
3406 err = dev->set_mac_address(dev, sa);
3407 if (!err)
056925ab 3408 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1da177e4
LT
3409 return err;
3410}
3411
3412/*
14e3e079 3413 * Perform the SIOCxIFxxx calls, inside read_lock(dev_base_lock)
1da177e4 3414 */
14e3e079 3415static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
1da177e4
LT
3416{
3417 int err;
881d966b 3418 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4
LT
3419
3420 if (!dev)
3421 return -ENODEV;
3422
3423 switch (cmd) {
3424 case SIOCGIFFLAGS: /* Get interface flags */
3425 ifr->ifr_flags = dev_get_flags(dev);
3426 return 0;
3427
1da177e4
LT
3428 case SIOCGIFMETRIC: /* Get the metric on the interface
3429 (currently unused) */
3430 ifr->ifr_metric = 0;
3431 return 0;
3432
1da177e4
LT
3433 case SIOCGIFMTU: /* Get the MTU of a device */
3434 ifr->ifr_mtu = dev->mtu;
3435 return 0;
3436
1da177e4
LT
3437 case SIOCGIFHWADDR:
3438 if (!dev->addr_len)
3439 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
3440 else
3441 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
3442 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
3443 ifr->ifr_hwaddr.sa_family = dev->type;
3444 return 0;
3445
14e3e079
JG
3446 case SIOCGIFSLAVE:
3447 err = -EINVAL;
3448 break;
3449
3450 case SIOCGIFMAP:
3451 ifr->ifr_map.mem_start = dev->mem_start;
3452 ifr->ifr_map.mem_end = dev->mem_end;
3453 ifr->ifr_map.base_addr = dev->base_addr;
3454 ifr->ifr_map.irq = dev->irq;
3455 ifr->ifr_map.dma = dev->dma;
3456 ifr->ifr_map.port = dev->if_port;
3457 return 0;
3458
3459 case SIOCGIFINDEX:
3460 ifr->ifr_ifindex = dev->ifindex;
3461 return 0;
3462
3463 case SIOCGIFTXQLEN:
3464 ifr->ifr_qlen = dev->tx_queue_len;
3465 return 0;
3466
3467 default:
3468 /* dev_ioctl() should ensure this case
3469 * is never reached
3470 */
3471 WARN_ON(1);
3472 err = -EINVAL;
3473 break;
3474
3475 }
3476 return err;
3477}
3478
3479/*
3480 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
3481 */
3482static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
3483{
3484 int err;
3485 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
3486
3487 if (!dev)
3488 return -ENODEV;
3489
3490 switch (cmd) {
3491 case SIOCSIFFLAGS: /* Set interface flags */
3492 return dev_change_flags(dev, ifr->ifr_flags);
3493
3494 case SIOCSIFMETRIC: /* Set the metric on the interface
3495 (currently unused) */
3496 return -EOPNOTSUPP;
3497
3498 case SIOCSIFMTU: /* Set the MTU of a device */
3499 return dev_set_mtu(dev, ifr->ifr_mtu);
3500
1da177e4
LT
3501 case SIOCSIFHWADDR:
3502 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
3503
3504 case SIOCSIFHWBROADCAST:
3505 if (ifr->ifr_hwaddr.sa_family != dev->type)
3506 return -EINVAL;
3507 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
3508 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
056925ab 3509 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1da177e4
LT
3510 return 0;
3511
1da177e4
LT
3512 case SIOCSIFMAP:
3513 if (dev->set_config) {
3514 if (!netif_device_present(dev))
3515 return -ENODEV;
3516 return dev->set_config(dev, &ifr->ifr_map);
3517 }
3518 return -EOPNOTSUPP;
3519
3520 case SIOCADDMULTI:
61ee6bd4 3521 if ((!dev->set_multicast_list && !dev->set_rx_mode) ||
1da177e4
LT
3522 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3523 return -EINVAL;
3524 if (!netif_device_present(dev))
3525 return -ENODEV;
3526 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
3527 dev->addr_len, 1);
3528
3529 case SIOCDELMULTI:
61ee6bd4 3530 if ((!dev->set_multicast_list && !dev->set_rx_mode) ||
1da177e4
LT
3531 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3532 return -EINVAL;
3533 if (!netif_device_present(dev))
3534 return -ENODEV;
3535 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
3536 dev->addr_len, 1);
3537
1da177e4
LT
3538 case SIOCSIFTXQLEN:
3539 if (ifr->ifr_qlen < 0)
3540 return -EINVAL;
3541 dev->tx_queue_len = ifr->ifr_qlen;
3542 return 0;
3543
3544 case SIOCSIFNAME:
3545 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
3546 return dev_change_name(dev, ifr->ifr_newname);
3547
3548 /*
3549 * Unknown or private ioctl
3550 */
3551
3552 default:
3553 if ((cmd >= SIOCDEVPRIVATE &&
3554 cmd <= SIOCDEVPRIVATE + 15) ||
3555 cmd == SIOCBONDENSLAVE ||
3556 cmd == SIOCBONDRELEASE ||
3557 cmd == SIOCBONDSETHWADDR ||
3558 cmd == SIOCBONDSLAVEINFOQUERY ||
3559 cmd == SIOCBONDINFOQUERY ||
3560 cmd == SIOCBONDCHANGEACTIVE ||
3561 cmd == SIOCGMIIPHY ||
3562 cmd == SIOCGMIIREG ||
3563 cmd == SIOCSMIIREG ||
3564 cmd == SIOCBRADDIF ||
3565 cmd == SIOCBRDELIF ||
3566 cmd == SIOCWANDEV) {
3567 err = -EOPNOTSUPP;
3568 if (dev->do_ioctl) {
3569 if (netif_device_present(dev))
3570 err = dev->do_ioctl(dev, ifr,
3571 cmd);
3572 else
3573 err = -ENODEV;
3574 }
3575 } else
3576 err = -EINVAL;
3577
3578 }
3579 return err;
3580}
3581
3582/*
3583 * This function handles all "interface"-type I/O control requests. The actual
3584 * 'doing' part of this is dev_ifsioc above.
3585 */
3586
3587/**
3588 * dev_ioctl - network device ioctl
c4ea43c5 3589 * @net: the applicable net namespace
1da177e4
LT
3590 * @cmd: command to issue
3591 * @arg: pointer to a struct ifreq in user space
3592 *
3593 * Issue ioctl functions to devices. This is normally called by the
3594 * user space syscall interfaces but can sometimes be useful for
3595 * other purposes. The return value is the return from the syscall if
3596 * positive or a negative errno code on error.
3597 */
3598
881d966b 3599int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1da177e4
LT
3600{
3601 struct ifreq ifr;
3602 int ret;
3603 char *colon;
3604
3605 /* One special case: SIOCGIFCONF takes ifconf argument
3606 and requires shared lock, because it sleeps writing
3607 to user space.
3608 */
3609
3610 if (cmd == SIOCGIFCONF) {
6756ae4b 3611 rtnl_lock();
881d966b 3612 ret = dev_ifconf(net, (char __user *) arg);
6756ae4b 3613 rtnl_unlock();
1da177e4
LT
3614 return ret;
3615 }
3616 if (cmd == SIOCGIFNAME)
881d966b 3617 return dev_ifname(net, (struct ifreq __user *)arg);
1da177e4
LT
3618
3619 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3620 return -EFAULT;
3621
3622 ifr.ifr_name[IFNAMSIZ-1] = 0;
3623
3624 colon = strchr(ifr.ifr_name, ':');
3625 if (colon)
3626 *colon = 0;
3627
3628 /*
3629 * See which interface the caller is talking about.
3630 */
3631
3632 switch (cmd) {
3633 /*
3634 * These ioctl calls:
3635 * - can be done by all.
3636 * - atomic and do not require locking.
3637 * - return a value
3638 */
3639 case SIOCGIFFLAGS:
3640 case SIOCGIFMETRIC:
3641 case SIOCGIFMTU:
3642 case SIOCGIFHWADDR:
3643 case SIOCGIFSLAVE:
3644 case SIOCGIFMAP:
3645 case SIOCGIFINDEX:
3646 case SIOCGIFTXQLEN:
881d966b 3647 dev_load(net, ifr.ifr_name);
1da177e4 3648 read_lock(&dev_base_lock);
14e3e079 3649 ret = dev_ifsioc_locked(net, &ifr, cmd);
1da177e4
LT
3650 read_unlock(&dev_base_lock);
3651 if (!ret) {
3652 if (colon)
3653 *colon = ':';
3654 if (copy_to_user(arg, &ifr,
3655 sizeof(struct ifreq)))
3656 ret = -EFAULT;
3657 }
3658 return ret;
3659
3660 case SIOCETHTOOL:
881d966b 3661 dev_load(net, ifr.ifr_name);
1da177e4 3662 rtnl_lock();
881d966b 3663 ret = dev_ethtool(net, &ifr);
1da177e4
LT
3664 rtnl_unlock();
3665 if (!ret) {
3666 if (colon)
3667 *colon = ':';
3668 if (copy_to_user(arg, &ifr,
3669 sizeof(struct ifreq)))
3670 ret = -EFAULT;
3671 }
3672 return ret;
3673
3674 /*
3675 * These ioctl calls:
3676 * - require superuser power.
3677 * - require strict serialization.
3678 * - return a value
3679 */
3680 case SIOCGMIIPHY:
3681 case SIOCGMIIREG:
3682 case SIOCSIFNAME:
3683 if (!capable(CAP_NET_ADMIN))
3684 return -EPERM;
881d966b 3685 dev_load(net, ifr.ifr_name);
1da177e4 3686 rtnl_lock();
881d966b 3687 ret = dev_ifsioc(net, &ifr, cmd);
1da177e4
LT
3688 rtnl_unlock();
3689 if (!ret) {
3690 if (colon)
3691 *colon = ':';
3692 if (copy_to_user(arg, &ifr,
3693 sizeof(struct ifreq)))
3694 ret = -EFAULT;
3695 }
3696 return ret;
3697
3698 /*
3699 * These ioctl calls:
3700 * - require superuser power.
3701 * - require strict serialization.
3702 * - do not return a value
3703 */
3704 case SIOCSIFFLAGS:
3705 case SIOCSIFMETRIC:
3706 case SIOCSIFMTU:
3707 case SIOCSIFMAP:
3708 case SIOCSIFHWADDR:
3709 case SIOCSIFSLAVE:
3710 case SIOCADDMULTI:
3711 case SIOCDELMULTI:
3712 case SIOCSIFHWBROADCAST:
3713 case SIOCSIFTXQLEN:
3714 case SIOCSMIIREG:
3715 case SIOCBONDENSLAVE:
3716 case SIOCBONDRELEASE:
3717 case SIOCBONDSETHWADDR:
1da177e4
LT
3718 case SIOCBONDCHANGEACTIVE:
3719 case SIOCBRADDIF:
3720 case SIOCBRDELIF:
3721 if (!capable(CAP_NET_ADMIN))
3722 return -EPERM;
cabcac0b
TG
3723 /* fall through */
3724 case SIOCBONDSLAVEINFOQUERY:
3725 case SIOCBONDINFOQUERY:
881d966b 3726 dev_load(net, ifr.ifr_name);
1da177e4 3727 rtnl_lock();
881d966b 3728 ret = dev_ifsioc(net, &ifr, cmd);
1da177e4
LT
3729 rtnl_unlock();
3730 return ret;
3731
3732 case SIOCGIFMEM:
3733 /* Get the per device memory space. We can add this but
3734 * currently do not support it */
3735 case SIOCSIFMEM:
3736 /* Set the per device memory buffer space.
3737 * Not applicable in our case */
3738 case SIOCSIFLINK:
3739 return -EINVAL;
3740
3741 /*
3742 * Unknown or private ioctl.
3743 */
3744 default:
3745 if (cmd == SIOCWANDEV ||
3746 (cmd >= SIOCDEVPRIVATE &&
3747 cmd <= SIOCDEVPRIVATE + 15)) {
881d966b 3748 dev_load(net, ifr.ifr_name);
1da177e4 3749 rtnl_lock();
881d966b 3750 ret = dev_ifsioc(net, &ifr, cmd);
1da177e4
LT
3751 rtnl_unlock();
3752 if (!ret && copy_to_user(arg, &ifr,
3753 sizeof(struct ifreq)))
3754 ret = -EFAULT;
3755 return ret;
3756 }
1da177e4 3757 /* Take care of Wireless Extensions */
295f4a1f 3758 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
881d966b 3759 return wext_handle_ioctl(net, &ifr, cmd, arg);
1da177e4
LT
3760 return -EINVAL;
3761 }
3762}
3763
3764
3765/**
3766 * dev_new_index - allocate an ifindex
c4ea43c5 3767 * @net: the applicable net namespace
1da177e4
LT
3768 *
3769 * Returns a suitable unique value for a new device interface
3770 * number. The caller must hold the rtnl semaphore or the
3771 * dev_base_lock to be sure it remains unique.
3772 */
881d966b 3773static int dev_new_index(struct net *net)
1da177e4
LT
3774{
3775 static int ifindex;
3776 for (;;) {
3777 if (++ifindex <= 0)
3778 ifindex = 1;
881d966b 3779 if (!__dev_get_by_index(net, ifindex))
1da177e4
LT
3780 return ifindex;
3781 }
3782}
3783
1da177e4
LT
3784/* Delayed registration/unregisteration */
3785static DEFINE_SPINLOCK(net_todo_list_lock);
3b5b34fd 3786static LIST_HEAD(net_todo_list);
1da177e4 3787
6f05f629 3788static void net_set_todo(struct net_device *dev)
1da177e4
LT
3789{
3790 spin_lock(&net_todo_list_lock);
3791 list_add_tail(&dev->todo_list, &net_todo_list);
3792 spin_unlock(&net_todo_list_lock);
3793}
3794
93ee31f1
DL
3795static void rollback_registered(struct net_device *dev)
3796{
3797 BUG_ON(dev_boot_phase);
3798 ASSERT_RTNL();
3799
3800 /* Some devices call without registering for initialization unwind. */
3801 if (dev->reg_state == NETREG_UNINITIALIZED) {
3802 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3803 "was registered\n", dev->name, dev);
3804
3805 WARN_ON(1);
3806 return;
3807 }
3808
3809 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3810
3811 /* If device is running, close it first. */
3812 dev_close(dev);
3813
3814 /* And unlink it from device chain. */
3815 unlist_netdevice(dev);
3816
3817 dev->reg_state = NETREG_UNREGISTERING;
3818
3819 synchronize_net();
3820
3821 /* Shutdown queueing discipline. */
3822 dev_shutdown(dev);
3823
3824
3825 /* Notify protocols, that we are about to destroy
3826 this device. They should clean all the things.
3827 */
3828 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
3829
3830 /*
3831 * Flush the unicast and multicast chains
3832 */
3833 dev_addr_discard(dev);
3834
3835 if (dev->uninit)
3836 dev->uninit(dev);
3837
3838 /* Notifier chain MUST detach us from master device. */
3839 BUG_TRAP(!dev->master);
3840
3841 /* Remove entries from kobject tree */
3842 netdev_unregister_kobject(dev);
3843
3844 synchronize_net();
3845
3846 dev_put(dev);
3847}
3848
e8a0464c
DM
3849static void __netdev_init_queue_locks_one(struct net_device *dev,
3850 struct netdev_queue *dev_queue,
3851 void *_unused)
c773e847
DM
3852{
3853 spin_lock_init(&dev_queue->_xmit_lock);
3854 netdev_set_lockdep_class(&dev_queue->_xmit_lock, dev->type);
3855 dev_queue->xmit_lock_owner = -1;
3856}
3857
3858static void netdev_init_queue_locks(struct net_device *dev)
3859{
e8a0464c
DM
3860 netdev_for_each_tx_queue(dev, __netdev_init_queue_locks_one, NULL);
3861 __netdev_init_queue_locks_one(dev, &dev->rx_queue, NULL);
c773e847
DM
3862}
3863
1da177e4
LT
3864/**
3865 * register_netdevice - register a network device
3866 * @dev: device to register
3867 *
3868 * Take a completed network device structure and add it to the kernel
3869 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3870 * chain. 0 is returned on success. A negative errno code is returned
3871 * on a failure to set up the device, or if the name is a duplicate.
3872 *
3873 * Callers must hold the rtnl semaphore. You may want
3874 * register_netdev() instead of this.
3875 *
3876 * BUGS:
3877 * The locking appears insufficient to guarantee two parallel registers
3878 * will not get the same name.
3879 */
3880
3881int register_netdevice(struct net_device *dev)
3882{
3883 struct hlist_head *head;
3884 struct hlist_node *p;
3885 int ret;
881d966b 3886 struct net *net;
1da177e4
LT
3887
3888 BUG_ON(dev_boot_phase);
3889 ASSERT_RTNL();
3890
b17a7c17
SH
3891 might_sleep();
3892
1da177e4
LT
3893 /* When net_device's are persistent, this will be fatal. */
3894 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
c346dca1
YH
3895 BUG_ON(!dev_net(dev));
3896 net = dev_net(dev);
1da177e4 3897
f1f28aa3 3898 spin_lock_init(&dev->addr_list_lock);
c773e847 3899 netdev_init_queue_locks(dev);
1da177e4 3900
1da177e4
LT
3901 dev->iflink = -1;
3902
3903 /* Init, if this function is available */
3904 if (dev->init) {
3905 ret = dev->init(dev);
3906 if (ret) {
3907 if (ret > 0)
3908 ret = -EIO;
90833aa4 3909 goto out;
1da177e4
LT
3910 }
3911 }
4ec93edb 3912
1da177e4
LT
3913 if (!dev_valid_name(dev->name)) {
3914 ret = -EINVAL;
7ce1b0ed 3915 goto err_uninit;
1da177e4
LT
3916 }
3917
881d966b 3918 dev->ifindex = dev_new_index(net);
1da177e4
LT
3919 if (dev->iflink == -1)
3920 dev->iflink = dev->ifindex;
3921
3922 /* Check for existence of name */
881d966b 3923 head = dev_name_hash(net, dev->name);
1da177e4
LT
3924 hlist_for_each(p, head) {
3925 struct net_device *d
3926 = hlist_entry(p, struct net_device, name_hlist);
3927 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3928 ret = -EEXIST;
7ce1b0ed 3929 goto err_uninit;
1da177e4 3930 }
4ec93edb 3931 }
1da177e4 3932
d212f87b
SH
3933 /* Fix illegal checksum combinations */
3934 if ((dev->features & NETIF_F_HW_CSUM) &&
3935 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3936 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
3937 dev->name);
3938 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
3939 }
3940
3941 if ((dev->features & NETIF_F_NO_CSUM) &&
3942 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3943 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
3944 dev->name);
3945 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
3946 }
3947
3948
1da177e4
LT
3949 /* Fix illegal SG+CSUM combinations. */
3950 if ((dev->features & NETIF_F_SG) &&
8648b305 3951 !(dev->features & NETIF_F_ALL_CSUM)) {
5a8da02b 3952 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
1da177e4
LT
3953 dev->name);
3954 dev->features &= ~NETIF_F_SG;
3955 }
3956
3957 /* TSO requires that SG is present as well. */
3958 if ((dev->features & NETIF_F_TSO) &&
3959 !(dev->features & NETIF_F_SG)) {
5a8da02b 3960 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
1da177e4
LT
3961 dev->name);
3962 dev->features &= ~NETIF_F_TSO;
3963 }
e89e9cf5
AR
3964 if (dev->features & NETIF_F_UFO) {
3965 if (!(dev->features & NETIF_F_HW_CSUM)) {
3966 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3967 "NETIF_F_HW_CSUM feature.\n",
3968 dev->name);
3969 dev->features &= ~NETIF_F_UFO;
3970 }
3971 if (!(dev->features & NETIF_F_SG)) {
3972 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3973 "NETIF_F_SG feature.\n",
3974 dev->name);
3975 dev->features &= ~NETIF_F_UFO;
3976 }
3977 }
1da177e4 3978
aaf8cdc3 3979 netdev_initialize_kobject(dev);
8b41d188 3980 ret = netdev_register_kobject(dev);
b17a7c17 3981 if (ret)
7ce1b0ed 3982 goto err_uninit;
b17a7c17
SH
3983 dev->reg_state = NETREG_REGISTERED;
3984
1da177e4
LT
3985 /*
3986 * Default initial state at registry is that the
3987 * device is present.
3988 */
3989
3990 set_bit(__LINK_STATE_PRESENT, &dev->state);
3991
1da177e4 3992 dev_init_scheduler(dev);
1da177e4 3993 dev_hold(dev);
ce286d32 3994 list_netdevice(dev);
1da177e4
LT
3995
3996 /* Notify protocols, that a new device appeared. */
056925ab 3997 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
fcc5a03a 3998 ret = notifier_to_errno(ret);
93ee31f1
DL
3999 if (ret) {
4000 rollback_registered(dev);
4001 dev->reg_state = NETREG_UNREGISTERED;
4002 }
1da177e4
LT
4003
4004out:
4005 return ret;
7ce1b0ed
HX
4006
4007err_uninit:
4008 if (dev->uninit)
4009 dev->uninit(dev);
4010 goto out;
1da177e4
LT
4011}
4012
4013/**
4014 * register_netdev - register a network device
4015 * @dev: device to register
4016 *
4017 * Take a completed network device structure and add it to the kernel
4018 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
4019 * chain. 0 is returned on success. A negative errno code is returned
4020 * on a failure to set up the device, or if the name is a duplicate.
4021 *
38b4da38 4022 * This is a wrapper around register_netdevice that takes the rtnl semaphore
1da177e4
LT
4023 * and expands the device name if you passed a format string to
4024 * alloc_netdev.
4025 */
4026int register_netdev(struct net_device *dev)
4027{
4028 int err;
4029
4030 rtnl_lock();
4031
4032 /*
4033 * If the name is a format string the caller wants us to do a
4034 * name allocation.
4035 */
4036 if (strchr(dev->name, '%')) {
4037 err = dev_alloc_name(dev, dev->name);
4038 if (err < 0)
4039 goto out;
4040 }
4ec93edb 4041
1da177e4
LT
4042 err = register_netdevice(dev);
4043out:
4044 rtnl_unlock();
4045 return err;
4046}
4047EXPORT_SYMBOL(register_netdev);
4048
4049/*
4050 * netdev_wait_allrefs - wait until all references are gone.
4051 *
4052 * This is called when unregistering network devices.
4053 *
4054 * Any protocol or device that holds a reference should register
4055 * for netdevice notification, and cleanup and put back the
4056 * reference if they receive an UNREGISTER event.
4057 * We can get stuck here if buggy protocols don't correctly
4ec93edb 4058 * call dev_put.
1da177e4
LT
4059 */
4060static void netdev_wait_allrefs(struct net_device *dev)
4061{
4062 unsigned long rebroadcast_time, warning_time;
4063
4064 rebroadcast_time = warning_time = jiffies;
4065 while (atomic_read(&dev->refcnt) != 0) {
4066 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
6756ae4b 4067 rtnl_lock();
1da177e4
LT
4068
4069 /* Rebroadcast unregister notification */
056925ab 4070 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
1da177e4
LT
4071
4072 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
4073 &dev->state)) {
4074 /* We must not have linkwatch events
4075 * pending on unregister. If this
4076 * happens, we simply run the queue
4077 * unscheduled, resulting in a noop
4078 * for this device.
4079 */
4080 linkwatch_run_queue();
4081 }
4082
6756ae4b 4083 __rtnl_unlock();
1da177e4
LT
4084
4085 rebroadcast_time = jiffies;
4086 }
4087
4088 msleep(250);
4089
4090 if (time_after(jiffies, warning_time + 10 * HZ)) {
4091 printk(KERN_EMERG "unregister_netdevice: "
4092 "waiting for %s to become free. Usage "
4093 "count = %d\n",
4094 dev->name, atomic_read(&dev->refcnt));
4095 warning_time = jiffies;
4096 }
4097 }
4098}
4099
4100/* The sequence is:
4101 *
4102 * rtnl_lock();
4103 * ...
4104 * register_netdevice(x1);
4105 * register_netdevice(x2);
4106 * ...
4107 * unregister_netdevice(y1);
4108 * unregister_netdevice(y2);
4109 * ...
4110 * rtnl_unlock();
4111 * free_netdev(y1);
4112 * free_netdev(y2);
4113 *
4114 * We are invoked by rtnl_unlock() after it drops the semaphore.
4115 * This allows us to deal with problems:
b17a7c17 4116 * 1) We can delete sysfs objects which invoke hotplug
1da177e4
LT
4117 * without deadlocking with linkwatch via keventd.
4118 * 2) Since we run with the RTNL semaphore not held, we can sleep
4119 * safely in order to wait for the netdev refcnt to drop to zero.
4120 */
4a3e2f71 4121static DEFINE_MUTEX(net_todo_run_mutex);
1da177e4
LT
4122void netdev_run_todo(void)
4123{
626ab0e6 4124 struct list_head list;
1da177e4
LT
4125
4126 /* Need to guard against multiple cpu's getting out of order. */
4a3e2f71 4127 mutex_lock(&net_todo_run_mutex);
1da177e4
LT
4128
4129 /* Not safe to do outside the semaphore. We must not return
4130 * until all unregister events invoked by the local processor
4131 * have been completed (either by this todo run, or one on
4132 * another cpu).
4133 */
4134 if (list_empty(&net_todo_list))
4135 goto out;
4136
4137 /* Snapshot list, allow later requests */
4138 spin_lock(&net_todo_list_lock);
626ab0e6 4139 list_replace_init(&net_todo_list, &list);
1da177e4 4140 spin_unlock(&net_todo_list_lock);
626ab0e6 4141
1da177e4
LT
4142 while (!list_empty(&list)) {
4143 struct net_device *dev
4144 = list_entry(list.next, struct net_device, todo_list);
4145 list_del(&dev->todo_list);
4146
b17a7c17
SH
4147 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
4148 printk(KERN_ERR "network todo '%s' but state %d\n",
4149 dev->name, dev->reg_state);
4150 dump_stack();
4151 continue;
4152 }
1da177e4 4153
b17a7c17 4154 dev->reg_state = NETREG_UNREGISTERED;
1da177e4 4155
b17a7c17 4156 netdev_wait_allrefs(dev);
1da177e4 4157
b17a7c17
SH
4158 /* paranoia */
4159 BUG_ON(atomic_read(&dev->refcnt));
4160 BUG_TRAP(!dev->ip_ptr);
4161 BUG_TRAP(!dev->ip6_ptr);
4162 BUG_TRAP(!dev->dn_ptr);
1da177e4 4163
b17a7c17
SH
4164 if (dev->destructor)
4165 dev->destructor(dev);
9093bbb2
SH
4166
4167 /* Free network device */
4168 kobject_put(&dev->dev.kobj);
1da177e4
LT
4169 }
4170
4171out:
4a3e2f71 4172 mutex_unlock(&net_todo_run_mutex);
1da177e4
LT
4173}
4174
5a1b5898 4175static struct net_device_stats *internal_stats(struct net_device *dev)
c45d286e 4176{
5a1b5898 4177 return &dev->stats;
c45d286e
RR
4178}
4179
dc2b4847 4180static void netdev_init_one_queue(struct net_device *dev,
e8a0464c
DM
4181 struct netdev_queue *queue,
4182 void *_unused)
dc2b4847 4183{
dc2b4847
DM
4184 queue->dev = dev;
4185}
4186
bb949fbd
DM
4187static void netdev_init_queues(struct net_device *dev)
4188{
e8a0464c
DM
4189 netdev_init_one_queue(dev, &dev->rx_queue, NULL);
4190 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
bb949fbd
DM
4191}
4192
1da177e4 4193/**
f25f4e44 4194 * alloc_netdev_mq - allocate network device
1da177e4
LT
4195 * @sizeof_priv: size of private data to allocate space for
4196 * @name: device name format string
4197 * @setup: callback to initialize device
f25f4e44 4198 * @queue_count: the number of subqueues to allocate
1da177e4
LT
4199 *
4200 * Allocates a struct net_device with private data area for driver use
f25f4e44
PWJ
4201 * and performs basic initialization. Also allocates subquue structs
4202 * for each queue on the device at the end of the netdevice.
1da177e4 4203 */
f25f4e44
PWJ
4204struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
4205 void (*setup)(struct net_device *), unsigned int queue_count)
1da177e4 4206{
e8a0464c 4207 struct netdev_queue *tx;
1da177e4 4208 struct net_device *dev;
7943986c 4209 size_t alloc_size;
e8a0464c 4210 void *p;
1da177e4 4211
b6fe17d6
SH
4212 BUG_ON(strlen(name) >= sizeof(dev->name));
4213
fd2ea0a7 4214 alloc_size = sizeof(struct net_device);
d1643d24
AD
4215 if (sizeof_priv) {
4216 /* ensure 32-byte alignment of private area */
4217 alloc_size = (alloc_size + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
4218 alloc_size += sizeof_priv;
4219 }
4220 /* ensure 32-byte alignment of whole construct */
4221 alloc_size += NETDEV_ALIGN_CONST;
1da177e4 4222
31380de9 4223 p = kzalloc(alloc_size, GFP_KERNEL);
1da177e4 4224 if (!p) {
b6fe17d6 4225 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
1da177e4
LT
4226 return NULL;
4227 }
1da177e4 4228
7943986c 4229 tx = kcalloc(queue_count, sizeof(struct netdev_queue), GFP_KERNEL);
e8a0464c
DM
4230 if (!tx) {
4231 printk(KERN_ERR "alloc_netdev: Unable to allocate "
4232 "tx qdiscs.\n");
4233 kfree(p);
4234 return NULL;
4235 }
4236
1da177e4
LT
4237 dev = (struct net_device *)
4238 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
4239 dev->padded = (char *)dev - (char *)p;
c346dca1 4240 dev_net_set(dev, &init_net);
1da177e4 4241
e8a0464c
DM
4242 dev->_tx = tx;
4243 dev->num_tx_queues = queue_count;
fd2ea0a7 4244 dev->real_num_tx_queues = queue_count;
e8a0464c 4245
f25f4e44
PWJ
4246 if (sizeof_priv) {
4247 dev->priv = ((char *)dev +
fd2ea0a7 4248 ((sizeof(struct net_device) + NETDEV_ALIGN_CONST)
f25f4e44
PWJ
4249 & ~NETDEV_ALIGN_CONST));
4250 }
4251
82cc1a7a 4252 dev->gso_max_size = GSO_MAX_SIZE;
1da177e4 4253
bb949fbd
DM
4254 netdev_init_queues(dev);
4255
5a1b5898 4256 dev->get_stats = internal_stats;
bea3348e 4257 netpoll_netdev_init(dev);
1da177e4
LT
4258 setup(dev);
4259 strcpy(dev->name, name);
4260 return dev;
4261}
f25f4e44 4262EXPORT_SYMBOL(alloc_netdev_mq);
1da177e4
LT
4263
4264/**
4265 * free_netdev - free network device
4266 * @dev: device
4267 *
4ec93edb
YH
4268 * This function does the last stage of destroying an allocated device
4269 * interface. The reference to the device object is released.
1da177e4
LT
4270 * If this is the last reference then it will be freed.
4271 */
4272void free_netdev(struct net_device *dev)
4273{
f3005d7f
DL
4274 release_net(dev_net(dev));
4275
e8a0464c
DM
4276 kfree(dev->_tx);
4277
3041a069 4278 /* Compatibility with error handling in drivers */
1da177e4
LT
4279 if (dev->reg_state == NETREG_UNINITIALIZED) {
4280 kfree((char *)dev - dev->padded);
4281 return;
4282 }
4283
4284 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
4285 dev->reg_state = NETREG_RELEASED;
4286
43cb76d9
GKH
4287 /* will free via device release */
4288 put_device(&dev->dev);
1da177e4 4289}
4ec93edb 4290
1da177e4 4291/* Synchronize with packet receive processing. */
4ec93edb 4292void synchronize_net(void)
1da177e4
LT
4293{
4294 might_sleep();
fbd568a3 4295 synchronize_rcu();
1da177e4
LT
4296}
4297
4298/**
4299 * unregister_netdevice - remove device from the kernel
4300 * @dev: device
4301 *
4302 * This function shuts down a device interface and removes it
d59b54b1 4303 * from the kernel tables.
1da177e4
LT
4304 *
4305 * Callers must hold the rtnl semaphore. You may want
4306 * unregister_netdev() instead of this.
4307 */
4308
22f8cde5 4309void unregister_netdevice(struct net_device *dev)
1da177e4 4310{
a6620712
HX
4311 ASSERT_RTNL();
4312
93ee31f1 4313 rollback_registered(dev);
1da177e4
LT
4314 /* Finish processing unregister after unlock */
4315 net_set_todo(dev);
1da177e4
LT
4316}
4317
4318/**
4319 * unregister_netdev - remove device from the kernel
4320 * @dev: device
4321 *
4322 * This function shuts down a device interface and removes it
d59b54b1 4323 * from the kernel tables.
1da177e4
LT
4324 *
4325 * This is just a wrapper for unregister_netdevice that takes
4326 * the rtnl semaphore. In general you want to use this and not
4327 * unregister_netdevice.
4328 */
4329void unregister_netdev(struct net_device *dev)
4330{
4331 rtnl_lock();
4332 unregister_netdevice(dev);
4333 rtnl_unlock();
4334}
4335
4336EXPORT_SYMBOL(unregister_netdev);
4337
ce286d32
EB
4338/**
4339 * dev_change_net_namespace - move device to different nethost namespace
4340 * @dev: device
4341 * @net: network namespace
4342 * @pat: If not NULL name pattern to try if the current device name
4343 * is already taken in the destination network namespace.
4344 *
4345 * This function shuts down a device interface and moves it
4346 * to a new network namespace. On success 0 is returned, on
4347 * a failure a netagive errno code is returned.
4348 *
4349 * Callers must hold the rtnl semaphore.
4350 */
4351
4352int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
4353{
4354 char buf[IFNAMSIZ];
4355 const char *destname;
4356 int err;
4357
4358 ASSERT_RTNL();
4359
4360 /* Don't allow namespace local devices to be moved. */
4361 err = -EINVAL;
4362 if (dev->features & NETIF_F_NETNS_LOCAL)
4363 goto out;
4364
4365 /* Ensure the device has been registrered */
4366 err = -EINVAL;
4367 if (dev->reg_state != NETREG_REGISTERED)
4368 goto out;
4369
4370 /* Get out if there is nothing todo */
4371 err = 0;
878628fb 4372 if (net_eq(dev_net(dev), net))
ce286d32
EB
4373 goto out;
4374
4375 /* Pick the destination device name, and ensure
4376 * we can use it in the destination network namespace.
4377 */
4378 err = -EEXIST;
4379 destname = dev->name;
4380 if (__dev_get_by_name(net, destname)) {
4381 /* We get here if we can't use the current device name */
4382 if (!pat)
4383 goto out;
4384 if (!dev_valid_name(pat))
4385 goto out;
4386 if (strchr(pat, '%')) {
4387 if (__dev_alloc_name(net, pat, buf) < 0)
4388 goto out;
4389 destname = buf;
4390 } else
4391 destname = pat;
4392 if (__dev_get_by_name(net, destname))
4393 goto out;
4394 }
4395
4396 /*
4397 * And now a mini version of register_netdevice unregister_netdevice.
4398 */
4399
4400 /* If device is running close it first. */
9b772652 4401 dev_close(dev);
ce286d32
EB
4402
4403 /* And unlink it from device chain */
4404 err = -ENODEV;
4405 unlist_netdevice(dev);
4406
4407 synchronize_net();
4408
4409 /* Shutdown queueing discipline. */
4410 dev_shutdown(dev);
4411
4412 /* Notify protocols, that we are about to destroy
4413 this device. They should clean all the things.
4414 */
4415 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
4416
4417 /*
4418 * Flush the unicast and multicast chains
4419 */
4420 dev_addr_discard(dev);
4421
4422 /* Actually switch the network namespace */
c346dca1 4423 dev_net_set(dev, net);
ce286d32
EB
4424
4425 /* Assign the new device name */
4426 if (destname != dev->name)
4427 strcpy(dev->name, destname);
4428
4429 /* If there is an ifindex conflict assign a new one */
4430 if (__dev_get_by_index(net, dev->ifindex)) {
4431 int iflink = (dev->iflink == dev->ifindex);
4432 dev->ifindex = dev_new_index(net);
4433 if (iflink)
4434 dev->iflink = dev->ifindex;
4435 }
4436
8b41d188 4437 /* Fixup kobjects */
aaf8cdc3
DL
4438 netdev_unregister_kobject(dev);
4439 err = netdev_register_kobject(dev);
8b41d188 4440 WARN_ON(err);
ce286d32
EB
4441
4442 /* Add the device back in the hashes */
4443 list_netdevice(dev);
4444
4445 /* Notify protocols, that a new device appeared. */
4446 call_netdevice_notifiers(NETDEV_REGISTER, dev);
4447
4448 synchronize_net();
4449 err = 0;
4450out:
4451 return err;
4452}
4453
1da177e4
LT
4454static int dev_cpu_callback(struct notifier_block *nfb,
4455 unsigned long action,
4456 void *ocpu)
4457{
4458 struct sk_buff **list_skb;
37437bb2 4459 struct Qdisc **list_net;
1da177e4
LT
4460 struct sk_buff *skb;
4461 unsigned int cpu, oldcpu = (unsigned long)ocpu;
4462 struct softnet_data *sd, *oldsd;
4463
8bb78442 4464 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1da177e4
LT
4465 return NOTIFY_OK;
4466
4467 local_irq_disable();
4468 cpu = smp_processor_id();
4469 sd = &per_cpu(softnet_data, cpu);
4470 oldsd = &per_cpu(softnet_data, oldcpu);
4471
4472 /* Find end of our completion_queue. */
4473 list_skb = &sd->completion_queue;
4474 while (*list_skb)
4475 list_skb = &(*list_skb)->next;
4476 /* Append completion queue from offline CPU. */
4477 *list_skb = oldsd->completion_queue;
4478 oldsd->completion_queue = NULL;
4479
4480 /* Find end of our output_queue. */
4481 list_net = &sd->output_queue;
4482 while (*list_net)
4483 list_net = &(*list_net)->next_sched;
4484 /* Append output queue from offline CPU. */
4485 *list_net = oldsd->output_queue;
4486 oldsd->output_queue = NULL;
4487
4488 raise_softirq_irqoff(NET_TX_SOFTIRQ);
4489 local_irq_enable();
4490
4491 /* Process offline CPU's input_pkt_queue */
4492 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
4493 netif_rx(skb);
4494
4495 return NOTIFY_OK;
4496}
1da177e4 4497
db217334
CL
4498#ifdef CONFIG_NET_DMA
4499/**
0ed72ec4
RD
4500 * net_dma_rebalance - try to maintain one DMA channel per CPU
4501 * @net_dma: DMA client and associated data (lock, channels, channel_mask)
4502 *
4503 * This is called when the number of channels allocated to the net_dma client
4504 * changes. The net_dma client tries to have one DMA channel per CPU.
db217334 4505 */
d379b01e
DW
4506
4507static void net_dma_rebalance(struct net_dma *net_dma)
db217334 4508{
d379b01e 4509 unsigned int cpu, i, n, chan_idx;
db217334
CL
4510 struct dma_chan *chan;
4511
d379b01e 4512 if (cpus_empty(net_dma->channel_mask)) {
db217334 4513 for_each_online_cpu(cpu)
29bbd72d 4514 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
db217334
CL
4515 return;
4516 }
4517
4518 i = 0;
4519 cpu = first_cpu(cpu_online_map);
4520
d379b01e
DW
4521 for_each_cpu_mask(chan_idx, net_dma->channel_mask) {
4522 chan = net_dma->channels[chan_idx];
4523
4524 n = ((num_online_cpus() / cpus_weight(net_dma->channel_mask))
4525 + (i < (num_online_cpus() %
4526 cpus_weight(net_dma->channel_mask)) ? 1 : 0));
db217334
CL
4527
4528 while(n) {
29bbd72d 4529 per_cpu(softnet_data, cpu).net_dma = chan;
db217334
CL
4530 cpu = next_cpu(cpu, cpu_online_map);
4531 n--;
4532 }
4533 i++;
4534 }
db217334
CL
4535}
4536
4537/**
4538 * netdev_dma_event - event callback for the net_dma_client
4539 * @client: should always be net_dma_client
f4b8ea78 4540 * @chan: DMA channel for the event
0ed72ec4 4541 * @state: DMA state to be handled
db217334 4542 */
d379b01e
DW
4543static enum dma_state_client
4544netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
4545 enum dma_state state)
4546{
4547 int i, found = 0, pos = -1;
4548 struct net_dma *net_dma =
4549 container_of(client, struct net_dma, client);
4550 enum dma_state_client ack = DMA_DUP; /* default: take no action */
4551
4552 spin_lock(&net_dma->lock);
4553 switch (state) {
4554 case DMA_RESOURCE_AVAILABLE:
0c0b0aca 4555 for (i = 0; i < nr_cpu_ids; i++)
d379b01e
DW
4556 if (net_dma->channels[i] == chan) {
4557 found = 1;
4558 break;
4559 } else if (net_dma->channels[i] == NULL && pos < 0)
4560 pos = i;
4561
4562 if (!found && pos >= 0) {
4563 ack = DMA_ACK;
4564 net_dma->channels[pos] = chan;
4565 cpu_set(pos, net_dma->channel_mask);
4566 net_dma_rebalance(net_dma);
4567 }
db217334
CL
4568 break;
4569 case DMA_RESOURCE_REMOVED:
0c0b0aca 4570 for (i = 0; i < nr_cpu_ids; i++)
d379b01e
DW
4571 if (net_dma->channels[i] == chan) {
4572 found = 1;
4573 pos = i;
4574 break;
4575 }
4576
4577 if (found) {
4578 ack = DMA_ACK;
4579 cpu_clear(pos, net_dma->channel_mask);
4580 net_dma->channels[i] = NULL;
4581 net_dma_rebalance(net_dma);
4582 }
db217334
CL
4583 break;
4584 default:
4585 break;
4586 }
d379b01e
DW
4587 spin_unlock(&net_dma->lock);
4588
4589 return ack;
db217334
CL
4590}
4591
4592/**
4593 * netdev_dma_regiser - register the networking subsystem as a DMA client
4594 */
4595static int __init netdev_dma_register(void)
4596{
0c0b0aca
MT
4597 net_dma.channels = kzalloc(nr_cpu_ids * sizeof(struct net_dma),
4598 GFP_KERNEL);
4599 if (unlikely(!net_dma.channels)) {
4600 printk(KERN_NOTICE
4601 "netdev_dma: no memory for net_dma.channels\n");
4602 return -ENOMEM;
4603 }
d379b01e
DW
4604 spin_lock_init(&net_dma.lock);
4605 dma_cap_set(DMA_MEMCPY, net_dma.client.cap_mask);
4606 dma_async_client_register(&net_dma.client);
4607 dma_async_client_chan_request(&net_dma.client);
db217334
CL
4608 return 0;
4609}
4610
4611#else
4612static int __init netdev_dma_register(void) { return -ENODEV; }
4613#endif /* CONFIG_NET_DMA */
1da177e4 4614
7f353bf2
HX
4615/**
4616 * netdev_compute_feature - compute conjunction of two feature sets
4617 * @all: first feature set
4618 * @one: second feature set
4619 *
4620 * Computes a new feature set after adding a device with feature set
4621 * @one to the master device with current feature set @all. Returns
4622 * the new feature set.
4623 */
4624int netdev_compute_features(unsigned long all, unsigned long one)
4625{
4626 /* if device needs checksumming, downgrade to hw checksumming */
4627 if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
4628 all ^= NETIF_F_NO_CSUM | NETIF_F_HW_CSUM;
4629
4630 /* if device can't do all checksum, downgrade to ipv4/ipv6 */
4631 if (all & NETIF_F_HW_CSUM && !(one & NETIF_F_HW_CSUM))
4632 all ^= NETIF_F_HW_CSUM
4633 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4634
4635 if (one & NETIF_F_GSO)
4636 one |= NETIF_F_GSO_SOFTWARE;
4637 one |= NETIF_F_GSO;
4638
4639 /* If even one device supports robust GSO, enable it for all. */
4640 if (one & NETIF_F_GSO_ROBUST)
4641 all |= NETIF_F_GSO_ROBUST;
4642
4643 all &= one | NETIF_F_LLTX;
4644
4645 if (!(all & NETIF_F_ALL_CSUM))
4646 all &= ~NETIF_F_SG;
4647 if (!(all & NETIF_F_SG))
4648 all &= ~NETIF_F_GSO_MASK;
4649
4650 return all;
4651}
4652EXPORT_SYMBOL(netdev_compute_features);
4653
30d97d35
PE
4654static struct hlist_head *netdev_create_hash(void)
4655{
4656 int i;
4657 struct hlist_head *hash;
4658
4659 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
4660 if (hash != NULL)
4661 for (i = 0; i < NETDEV_HASHENTRIES; i++)
4662 INIT_HLIST_HEAD(&hash[i]);
4663
4664 return hash;
4665}
4666
881d966b 4667/* Initialize per network namespace state */
4665079c 4668static int __net_init netdev_init(struct net *net)
881d966b 4669{
881d966b 4670 INIT_LIST_HEAD(&net->dev_base_head);
881d966b 4671
30d97d35
PE
4672 net->dev_name_head = netdev_create_hash();
4673 if (net->dev_name_head == NULL)
4674 goto err_name;
881d966b 4675
30d97d35
PE
4676 net->dev_index_head = netdev_create_hash();
4677 if (net->dev_index_head == NULL)
4678 goto err_idx;
881d966b
EB
4679
4680 return 0;
30d97d35
PE
4681
4682err_idx:
4683 kfree(net->dev_name_head);
4684err_name:
4685 return -ENOMEM;
881d966b
EB
4686}
4687
6579e57b
AV
4688char *netdev_drivername(struct net_device *dev, char *buffer, int len)
4689{
4690 struct device_driver *driver;
4691 struct device *parent;
4692
4693 if (len <= 0 || !buffer)
4694 return buffer;
4695 buffer[0] = 0;
4696
4697 parent = dev->dev.parent;
4698
4699 if (!parent)
4700 return buffer;
4701
4702 driver = parent->driver;
4703 if (driver && driver->name)
4704 strlcpy(buffer, driver->name, len);
4705 return buffer;
4706}
4707
4665079c 4708static void __net_exit netdev_exit(struct net *net)
881d966b
EB
4709{
4710 kfree(net->dev_name_head);
4711 kfree(net->dev_index_head);
4712}
4713
022cbae6 4714static struct pernet_operations __net_initdata netdev_net_ops = {
881d966b
EB
4715 .init = netdev_init,
4716 .exit = netdev_exit,
4717};
4718
4665079c 4719static void __net_exit default_device_exit(struct net *net)
ce286d32
EB
4720{
4721 struct net_device *dev, *next;
4722 /*
4723 * Push all migratable of the network devices back to the
4724 * initial network namespace
4725 */
4726 rtnl_lock();
4727 for_each_netdev_safe(net, dev, next) {
4728 int err;
aca51397 4729 char fb_name[IFNAMSIZ];
ce286d32
EB
4730
4731 /* Ignore unmoveable devices (i.e. loopback) */
4732 if (dev->features & NETIF_F_NETNS_LOCAL)
4733 continue;
4734
4735 /* Push remaing network devices to init_net */
aca51397
PE
4736 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
4737 err = dev_change_net_namespace(dev, &init_net, fb_name);
ce286d32 4738 if (err) {
aca51397 4739 printk(KERN_EMERG "%s: failed to move %s to init_net: %d\n",
ce286d32 4740 __func__, dev->name, err);
aca51397 4741 BUG();
ce286d32
EB
4742 }
4743 }
4744 rtnl_unlock();
4745}
4746
022cbae6 4747static struct pernet_operations __net_initdata default_device_ops = {
ce286d32
EB
4748 .exit = default_device_exit,
4749};
4750
1da177e4
LT
4751/*
4752 * Initialize the DEV module. At boot time this walks the device list and
4753 * unhooks any devices that fail to initialise (normally hardware not
4754 * present) and leaves us with a valid list of present and active devices.
4755 *
4756 */
4757
4758/*
4759 * This is called single threaded during boot, so no need
4760 * to take the rtnl semaphore.
4761 */
4762static int __init net_dev_init(void)
4763{
4764 int i, rc = -ENOMEM;
4765
4766 BUG_ON(!dev_boot_phase);
4767
1da177e4
LT
4768 if (dev_proc_init())
4769 goto out;
4770
8b41d188 4771 if (netdev_kobject_init())
1da177e4
LT
4772 goto out;
4773
4774 INIT_LIST_HEAD(&ptype_all);
82d8a867 4775 for (i = 0; i < PTYPE_HASH_SIZE; i++)
1da177e4
LT
4776 INIT_LIST_HEAD(&ptype_base[i]);
4777
881d966b
EB
4778 if (register_pernet_subsys(&netdev_net_ops))
4779 goto out;
1da177e4 4780
ce286d32
EB
4781 if (register_pernet_device(&default_device_ops))
4782 goto out;
4783
1da177e4
LT
4784 /*
4785 * Initialise the packet receive queues.
4786 */
4787
6f912042 4788 for_each_possible_cpu(i) {
1da177e4
LT
4789 struct softnet_data *queue;
4790
4791 queue = &per_cpu(softnet_data, i);
4792 skb_queue_head_init(&queue->input_pkt_queue);
1da177e4
LT
4793 queue->completion_queue = NULL;
4794 INIT_LIST_HEAD(&queue->poll_list);
bea3348e
SH
4795
4796 queue->backlog.poll = process_backlog;
4797 queue->backlog.weight = weight_p;
1da177e4
LT
4798 }
4799
db217334
CL
4800 netdev_dma_register();
4801
1da177e4
LT
4802 dev_boot_phase = 0;
4803
962cf36c
CM
4804 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
4805 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
1da177e4
LT
4806
4807 hotcpu_notifier(dev_cpu_callback, 0);
4808 dst_init();
4809 dev_mcast_init();
4810 rc = 0;
4811out:
4812 return rc;
4813}
4814
4815subsys_initcall(net_dev_init);
4816
4817EXPORT_SYMBOL(__dev_get_by_index);
4818EXPORT_SYMBOL(__dev_get_by_name);
4819EXPORT_SYMBOL(__dev_remove_pack);
c2373ee9 4820EXPORT_SYMBOL(dev_valid_name);
1da177e4
LT
4821EXPORT_SYMBOL(dev_add_pack);
4822EXPORT_SYMBOL(dev_alloc_name);
4823EXPORT_SYMBOL(dev_close);
4824EXPORT_SYMBOL(dev_get_by_flags);
4825EXPORT_SYMBOL(dev_get_by_index);
4826EXPORT_SYMBOL(dev_get_by_name);
1da177e4
LT
4827EXPORT_SYMBOL(dev_open);
4828EXPORT_SYMBOL(dev_queue_xmit);
4829EXPORT_SYMBOL(dev_remove_pack);
4830EXPORT_SYMBOL(dev_set_allmulti);
4831EXPORT_SYMBOL(dev_set_promiscuity);
4832EXPORT_SYMBOL(dev_change_flags);
4833EXPORT_SYMBOL(dev_set_mtu);
4834EXPORT_SYMBOL(dev_set_mac_address);
4835EXPORT_SYMBOL(free_netdev);
4836EXPORT_SYMBOL(netdev_boot_setup_check);
4837EXPORT_SYMBOL(netdev_set_master);
4838EXPORT_SYMBOL(netdev_state_change);
4839EXPORT_SYMBOL(netif_receive_skb);
4840EXPORT_SYMBOL(netif_rx);
4841EXPORT_SYMBOL(register_gifconf);
4842EXPORT_SYMBOL(register_netdevice);
4843EXPORT_SYMBOL(register_netdevice_notifier);
4844EXPORT_SYMBOL(skb_checksum_help);
4845EXPORT_SYMBOL(synchronize_net);
4846EXPORT_SYMBOL(unregister_netdevice);
4847EXPORT_SYMBOL(unregister_netdevice_notifier);
4848EXPORT_SYMBOL(net_enable_timestamp);
4849EXPORT_SYMBOL(net_disable_timestamp);
4850EXPORT_SYMBOL(dev_get_flags);
4851
4852#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4853EXPORT_SYMBOL(br_handle_frame_hook);
4854EXPORT_SYMBOL(br_fdb_get_hook);
4855EXPORT_SYMBOL(br_fdb_put_hook);
4856#endif
4857
4858#ifdef CONFIG_KMOD
4859EXPORT_SYMBOL(dev_load);
4860#endif
4861
4862EXPORT_PER_CPU_SYMBOL(softnet_data);