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