]> bbs.cooldavid.org Git - net-next-2.6.git/blame_incremental - net/core/dev.c
netdev: Add tracepoints to netdev layer
[net-next-2.6.git] / net / core / dev.c
... / ...
CommitLineData
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
10 * Authors: Ross Biro
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>
78#include <linux/capability.h>
79#include <linux/cpu.h>
80#include <linux/types.h>
81#include <linux/kernel.h>
82#include <linux/hash.h>
83#include <linux/slab.h>
84#include <linux/sched.h>
85#include <linux/mutex.h>
86#include <linux/string.h>
87#include <linux/mm.h>
88#include <linux/socket.h>
89#include <linux/sockios.h>
90#include <linux/errno.h>
91#include <linux/interrupt.h>
92#include <linux/if_ether.h>
93#include <linux/netdevice.h>
94#include <linux/etherdevice.h>
95#include <linux/ethtool.h>
96#include <linux/notifier.h>
97#include <linux/skbuff.h>
98#include <net/net_namespace.h>
99#include <net/sock.h>
100#include <linux/rtnetlink.h>
101#include <linux/proc_fs.h>
102#include <linux/seq_file.h>
103#include <linux/stat.h>
104#include <net/dst.h>
105#include <net/pkt_sched.h>
106#include <net/checksum.h>
107#include <net/xfrm.h>
108#include <linux/highmem.h>
109#include <linux/init.h>
110#include <linux/kmod.h>
111#include <linux/module.h>
112#include <linux/netpoll.h>
113#include <linux/rcupdate.h>
114#include <linux/delay.h>
115#include <net/wext.h>
116#include <net/iw_handler.h>
117#include <asm/current.h>
118#include <linux/audit.h>
119#include <linux/dmaengine.h>
120#include <linux/err.h>
121#include <linux/ctype.h>
122#include <linux/if_arp.h>
123#include <linux/if_vlan.h>
124#include <linux/ip.h>
125#include <net/ip.h>
126#include <linux/ipv6.h>
127#include <linux/in.h>
128#include <linux/jhash.h>
129#include <linux/random.h>
130#include <trace/events/napi.h>
131#include <trace/events/net.h>
132#include <linux/pci.h>
133
134#include "net-sysfs.h"
135
136/* Instead of increasing this, you should create a hash table. */
137#define MAX_GRO_SKBS 8
138
139/* This should be increased if a protocol with a bigger head is added. */
140#define GRO_MAX_HEAD (MAX_HEADER + 128)
141
142/*
143 * The list of packet types we will receive (as opposed to discard)
144 * and the routines to invoke.
145 *
146 * Why 16. Because with 16 the only overlap we get on a hash of the
147 * low nibble of the protocol value is RARP/SNAP/X.25.
148 *
149 * NOTE: That is no longer true with the addition of VLAN tags. Not
150 * sure which should go first, but I bet it won't make much
151 * difference if we are running VLANs. The good news is that
152 * this protocol won't be in the list unless compiled in, so
153 * the average user (w/out VLANs) will not be adversely affected.
154 * --BLG
155 *
156 * 0800 IP
157 * 8100 802.1Q VLAN
158 * 0001 802.3
159 * 0002 AX.25
160 * 0004 802.2
161 * 8035 RARP
162 * 0005 SNAP
163 * 0805 X.25
164 * 0806 ARP
165 * 8137 IPX
166 * 0009 Localtalk
167 * 86DD IPv6
168 */
169
170#define PTYPE_HASH_SIZE (16)
171#define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
172
173static DEFINE_SPINLOCK(ptype_lock);
174static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
175static struct list_head ptype_all __read_mostly; /* Taps */
176
177/*
178 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
179 * semaphore.
180 *
181 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
182 *
183 * Writers must hold the rtnl semaphore while they loop through the
184 * dev_base_head list, and hold dev_base_lock for writing when they do the
185 * actual updates. This allows pure readers to access the list even
186 * while a writer is preparing to update it.
187 *
188 * To put it another way, dev_base_lock is held for writing only to
189 * protect against pure readers; the rtnl semaphore provides the
190 * protection against other writers.
191 *
192 * See, for example usages, register_netdevice() and
193 * unregister_netdevice(), which must be called with the rtnl
194 * semaphore held.
195 */
196DEFINE_RWLOCK(dev_base_lock);
197EXPORT_SYMBOL(dev_base_lock);
198
199static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
200{
201 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
202 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
203}
204
205static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
206{
207 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
208}
209
210static inline void rps_lock(struct softnet_data *sd)
211{
212#ifdef CONFIG_RPS
213 spin_lock(&sd->input_pkt_queue.lock);
214#endif
215}
216
217static inline void rps_unlock(struct softnet_data *sd)
218{
219#ifdef CONFIG_RPS
220 spin_unlock(&sd->input_pkt_queue.lock);
221#endif
222}
223
224/* Device list insertion */
225static int list_netdevice(struct net_device *dev)
226{
227 struct net *net = dev_net(dev);
228
229 ASSERT_RTNL();
230
231 write_lock_bh(&dev_base_lock);
232 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
233 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
234 hlist_add_head_rcu(&dev->index_hlist,
235 dev_index_hash(net, dev->ifindex));
236 write_unlock_bh(&dev_base_lock);
237 return 0;
238}
239
240/* Device list removal
241 * caller must respect a RCU grace period before freeing/reusing dev
242 */
243static void unlist_netdevice(struct net_device *dev)
244{
245 ASSERT_RTNL();
246
247 /* Unlink dev from the device chain */
248 write_lock_bh(&dev_base_lock);
249 list_del_rcu(&dev->dev_list);
250 hlist_del_rcu(&dev->name_hlist);
251 hlist_del_rcu(&dev->index_hlist);
252 write_unlock_bh(&dev_base_lock);
253}
254
255/*
256 * Our notifier list
257 */
258
259static RAW_NOTIFIER_HEAD(netdev_chain);
260
261/*
262 * Device drivers call our routines to queue packets here. We empty the
263 * queue in the local softnet handler.
264 */
265
266DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
267EXPORT_PER_CPU_SYMBOL(softnet_data);
268
269#ifdef CONFIG_LOCKDEP
270/*
271 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
272 * according to dev->type
273 */
274static const unsigned short netdev_lock_type[] =
275 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
276 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
277 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
278 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
279 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
280 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
281 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
282 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
283 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
284 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
285 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
286 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
287 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
288 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET,
289 ARPHRD_PHONET_PIPE, ARPHRD_IEEE802154,
290 ARPHRD_VOID, ARPHRD_NONE};
291
292static const char *const netdev_lock_name[] =
293 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
294 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
295 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
296 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
297 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
298 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
299 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
300 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
301 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
302 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
303 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
304 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
305 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
306 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET",
307 "_xmit_PHONET_PIPE", "_xmit_IEEE802154",
308 "_xmit_VOID", "_xmit_NONE"};
309
310static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
311static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
312
313static inline unsigned short netdev_lock_pos(unsigned short dev_type)
314{
315 int i;
316
317 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
318 if (netdev_lock_type[i] == dev_type)
319 return i;
320 /* the last key is used by default */
321 return ARRAY_SIZE(netdev_lock_type) - 1;
322}
323
324static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
325 unsigned short dev_type)
326{
327 int i;
328
329 i = netdev_lock_pos(dev_type);
330 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
331 netdev_lock_name[i]);
332}
333
334static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
335{
336 int i;
337
338 i = netdev_lock_pos(dev->type);
339 lockdep_set_class_and_name(&dev->addr_list_lock,
340 &netdev_addr_lock_key[i],
341 netdev_lock_name[i]);
342}
343#else
344static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
345 unsigned short dev_type)
346{
347}
348static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
349{
350}
351#endif
352
353/*******************************************************************************
354
355 Protocol management and registration routines
356
357*******************************************************************************/
358
359/*
360 * Add a protocol ID to the list. Now that the input handler is
361 * smarter we can dispense with all the messy stuff that used to be
362 * here.
363 *
364 * BEWARE!!! Protocol handlers, mangling input packets,
365 * MUST BE last in hash buckets and checking protocol handlers
366 * MUST start from promiscuous ptype_all chain in net_bh.
367 * It is true now, do not change it.
368 * Explanation follows: if protocol handler, mangling packet, will
369 * be the first on list, it is not able to sense, that packet
370 * is cloned and should be copied-on-write, so that it will
371 * change it and subsequent readers will get broken packet.
372 * --ANK (980803)
373 */
374
375/**
376 * dev_add_pack - add packet handler
377 * @pt: packet type declaration
378 *
379 * Add a protocol handler to the networking stack. The passed &packet_type
380 * is linked into kernel lists and may not be freed until it has been
381 * removed from the kernel lists.
382 *
383 * This call does not sleep therefore it can not
384 * guarantee all CPU's that are in middle of receiving packets
385 * will see the new packet type (until the next received packet).
386 */
387
388void dev_add_pack(struct packet_type *pt)
389{
390 int hash;
391
392 spin_lock_bh(&ptype_lock);
393 if (pt->type == htons(ETH_P_ALL))
394 list_add_rcu(&pt->list, &ptype_all);
395 else {
396 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
397 list_add_rcu(&pt->list, &ptype_base[hash]);
398 }
399 spin_unlock_bh(&ptype_lock);
400}
401EXPORT_SYMBOL(dev_add_pack);
402
403/**
404 * __dev_remove_pack - remove packet handler
405 * @pt: packet type declaration
406 *
407 * Remove a protocol handler that was previously added to the kernel
408 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
409 * from the kernel lists and can be freed or reused once this function
410 * returns.
411 *
412 * The packet type might still be in use by receivers
413 * and must not be freed until after all the CPU's have gone
414 * through a quiescent state.
415 */
416void __dev_remove_pack(struct packet_type *pt)
417{
418 struct list_head *head;
419 struct packet_type *pt1;
420
421 spin_lock_bh(&ptype_lock);
422
423 if (pt->type == htons(ETH_P_ALL))
424 head = &ptype_all;
425 else
426 head = &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
427
428 list_for_each_entry(pt1, head, list) {
429 if (pt == pt1) {
430 list_del_rcu(&pt->list);
431 goto out;
432 }
433 }
434
435 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
436out:
437 spin_unlock_bh(&ptype_lock);
438}
439EXPORT_SYMBOL(__dev_remove_pack);
440
441/**
442 * dev_remove_pack - remove packet handler
443 * @pt: packet type declaration
444 *
445 * Remove a protocol handler that was previously added to the kernel
446 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
447 * from the kernel lists and can be freed or reused once this function
448 * returns.
449 *
450 * This call sleeps to guarantee that no CPU is looking at the packet
451 * type after return.
452 */
453void dev_remove_pack(struct packet_type *pt)
454{
455 __dev_remove_pack(pt);
456
457 synchronize_net();
458}
459EXPORT_SYMBOL(dev_remove_pack);
460
461/******************************************************************************
462
463 Device Boot-time Settings Routines
464
465*******************************************************************************/
466
467/* Boot time configuration table */
468static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
469
470/**
471 * netdev_boot_setup_add - add new setup entry
472 * @name: name of the device
473 * @map: configured settings for the device
474 *
475 * Adds new setup entry to the dev_boot_setup list. The function
476 * returns 0 on error and 1 on success. This is a generic routine to
477 * all netdevices.
478 */
479static int netdev_boot_setup_add(char *name, struct ifmap *map)
480{
481 struct netdev_boot_setup *s;
482 int i;
483
484 s = dev_boot_setup;
485 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
486 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
487 memset(s[i].name, 0, sizeof(s[i].name));
488 strlcpy(s[i].name, name, IFNAMSIZ);
489 memcpy(&s[i].map, map, sizeof(s[i].map));
490 break;
491 }
492 }
493
494 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
495}
496
497/**
498 * netdev_boot_setup_check - check boot time settings
499 * @dev: the netdevice
500 *
501 * Check boot time settings for the device.
502 * The found settings are set for the device to be used
503 * later in the device probing.
504 * Returns 0 if no settings found, 1 if they are.
505 */
506int netdev_boot_setup_check(struct net_device *dev)
507{
508 struct netdev_boot_setup *s = dev_boot_setup;
509 int i;
510
511 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
512 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
513 !strcmp(dev->name, s[i].name)) {
514 dev->irq = s[i].map.irq;
515 dev->base_addr = s[i].map.base_addr;
516 dev->mem_start = s[i].map.mem_start;
517 dev->mem_end = s[i].map.mem_end;
518 return 1;
519 }
520 }
521 return 0;
522}
523EXPORT_SYMBOL(netdev_boot_setup_check);
524
525
526/**
527 * netdev_boot_base - get address from boot time settings
528 * @prefix: prefix for network device
529 * @unit: id for network device
530 *
531 * Check boot time settings for the base address of device.
532 * The found settings are set for the device to be used
533 * later in the device probing.
534 * Returns 0 if no settings found.
535 */
536unsigned long netdev_boot_base(const char *prefix, int unit)
537{
538 const struct netdev_boot_setup *s = dev_boot_setup;
539 char name[IFNAMSIZ];
540 int i;
541
542 sprintf(name, "%s%d", prefix, unit);
543
544 /*
545 * If device already registered then return base of 1
546 * to indicate not to probe for this interface
547 */
548 if (__dev_get_by_name(&init_net, name))
549 return 1;
550
551 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
552 if (!strcmp(name, s[i].name))
553 return s[i].map.base_addr;
554 return 0;
555}
556
557/*
558 * Saves at boot time configured settings for any netdevice.
559 */
560int __init netdev_boot_setup(char *str)
561{
562 int ints[5];
563 struct ifmap map;
564
565 str = get_options(str, ARRAY_SIZE(ints), ints);
566 if (!str || !*str)
567 return 0;
568
569 /* Save settings */
570 memset(&map, 0, sizeof(map));
571 if (ints[0] > 0)
572 map.irq = ints[1];
573 if (ints[0] > 1)
574 map.base_addr = ints[2];
575 if (ints[0] > 2)
576 map.mem_start = ints[3];
577 if (ints[0] > 3)
578 map.mem_end = ints[4];
579
580 /* Add new entry to the list */
581 return netdev_boot_setup_add(str, &map);
582}
583
584__setup("netdev=", netdev_boot_setup);
585
586/*******************************************************************************
587
588 Device Interface Subroutines
589
590*******************************************************************************/
591
592/**
593 * __dev_get_by_name - find a device by its name
594 * @net: the applicable net namespace
595 * @name: name to find
596 *
597 * Find an interface by name. Must be called under RTNL semaphore
598 * or @dev_base_lock. If the name is found a pointer to the device
599 * is returned. If the name is not found then %NULL is returned. The
600 * reference counters are not incremented so the caller must be
601 * careful with locks.
602 */
603
604struct net_device *__dev_get_by_name(struct net *net, const char *name)
605{
606 struct hlist_node *p;
607 struct net_device *dev;
608 struct hlist_head *head = dev_name_hash(net, name);
609
610 hlist_for_each_entry(dev, p, head, name_hlist)
611 if (!strncmp(dev->name, name, IFNAMSIZ))
612 return dev;
613
614 return NULL;
615}
616EXPORT_SYMBOL(__dev_get_by_name);
617
618/**
619 * dev_get_by_name_rcu - find a device by its name
620 * @net: the applicable net namespace
621 * @name: name to find
622 *
623 * Find an interface by name.
624 * If the name is found a pointer to the device is returned.
625 * If the name is not found then %NULL is returned.
626 * The reference counters are not incremented so the caller must be
627 * careful with locks. The caller must hold RCU lock.
628 */
629
630struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
631{
632 struct hlist_node *p;
633 struct net_device *dev;
634 struct hlist_head *head = dev_name_hash(net, name);
635
636 hlist_for_each_entry_rcu(dev, p, head, name_hlist)
637 if (!strncmp(dev->name, name, IFNAMSIZ))
638 return dev;
639
640 return NULL;
641}
642EXPORT_SYMBOL(dev_get_by_name_rcu);
643
644/**
645 * dev_get_by_name - find a device by its name
646 * @net: the applicable net namespace
647 * @name: name to find
648 *
649 * Find an interface by name. This can be called from any
650 * context and does its own locking. The returned handle has
651 * the usage count incremented and the caller must use dev_put() to
652 * release it when it is no longer needed. %NULL is returned if no
653 * matching device is found.
654 */
655
656struct net_device *dev_get_by_name(struct net *net, const char *name)
657{
658 struct net_device *dev;
659
660 rcu_read_lock();
661 dev = dev_get_by_name_rcu(net, name);
662 if (dev)
663 dev_hold(dev);
664 rcu_read_unlock();
665 return dev;
666}
667EXPORT_SYMBOL(dev_get_by_name);
668
669/**
670 * __dev_get_by_index - find a device by its ifindex
671 * @net: the applicable net namespace
672 * @ifindex: index of device
673 *
674 * Search for an interface by index. Returns %NULL if the device
675 * is not found or a pointer to the device. The device has not
676 * had its reference counter increased so the caller must be careful
677 * about locking. The caller must hold either the RTNL semaphore
678 * or @dev_base_lock.
679 */
680
681struct net_device *__dev_get_by_index(struct net *net, int ifindex)
682{
683 struct hlist_node *p;
684 struct net_device *dev;
685 struct hlist_head *head = dev_index_hash(net, ifindex);
686
687 hlist_for_each_entry(dev, p, head, index_hlist)
688 if (dev->ifindex == ifindex)
689 return dev;
690
691 return NULL;
692}
693EXPORT_SYMBOL(__dev_get_by_index);
694
695/**
696 * dev_get_by_index_rcu - find a device by its ifindex
697 * @net: the applicable net namespace
698 * @ifindex: index of device
699 *
700 * Search for an interface by index. Returns %NULL if the device
701 * is not found or a pointer to the device. The device has not
702 * had its reference counter increased so the caller must be careful
703 * about locking. The caller must hold RCU lock.
704 */
705
706struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
707{
708 struct hlist_node *p;
709 struct net_device *dev;
710 struct hlist_head *head = dev_index_hash(net, ifindex);
711
712 hlist_for_each_entry_rcu(dev, p, head, index_hlist)
713 if (dev->ifindex == ifindex)
714 return dev;
715
716 return NULL;
717}
718EXPORT_SYMBOL(dev_get_by_index_rcu);
719
720
721/**
722 * dev_get_by_index - find a device by its ifindex
723 * @net: the applicable net namespace
724 * @ifindex: index of device
725 *
726 * Search for an interface by index. Returns NULL if the device
727 * is not found or a pointer to the device. The device returned has
728 * had a reference added and the pointer is safe until the user calls
729 * dev_put to indicate they have finished with it.
730 */
731
732struct net_device *dev_get_by_index(struct net *net, int ifindex)
733{
734 struct net_device *dev;
735
736 rcu_read_lock();
737 dev = dev_get_by_index_rcu(net, ifindex);
738 if (dev)
739 dev_hold(dev);
740 rcu_read_unlock();
741 return dev;
742}
743EXPORT_SYMBOL(dev_get_by_index);
744
745/**
746 * dev_getbyhwaddr - find a device by its hardware address
747 * @net: the applicable net namespace
748 * @type: media type of device
749 * @ha: hardware address
750 *
751 * Search for an interface by MAC address. Returns NULL if the device
752 * is not found or a pointer to the device. The caller must hold the
753 * rtnl semaphore. The returned device has not had its ref count increased
754 * and the caller must therefore be careful about locking
755 *
756 * BUGS:
757 * If the API was consistent this would be __dev_get_by_hwaddr
758 */
759
760struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha)
761{
762 struct net_device *dev;
763
764 ASSERT_RTNL();
765
766 for_each_netdev(net, dev)
767 if (dev->type == type &&
768 !memcmp(dev->dev_addr, ha, dev->addr_len))
769 return dev;
770
771 return NULL;
772}
773EXPORT_SYMBOL(dev_getbyhwaddr);
774
775struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
776{
777 struct net_device *dev;
778
779 ASSERT_RTNL();
780 for_each_netdev(net, dev)
781 if (dev->type == type)
782 return dev;
783
784 return NULL;
785}
786EXPORT_SYMBOL(__dev_getfirstbyhwtype);
787
788struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
789{
790 struct net_device *dev, *ret = NULL;
791
792 rcu_read_lock();
793 for_each_netdev_rcu(net, dev)
794 if (dev->type == type) {
795 dev_hold(dev);
796 ret = dev;
797 break;
798 }
799 rcu_read_unlock();
800 return ret;
801}
802EXPORT_SYMBOL(dev_getfirstbyhwtype);
803
804/**
805 * dev_get_by_flags_rcu - find any device with given flags
806 * @net: the applicable net namespace
807 * @if_flags: IFF_* values
808 * @mask: bitmask of bits in if_flags to check
809 *
810 * Search for any interface with the given flags. Returns NULL if a device
811 * is not found or a pointer to the device. Must be called inside
812 * rcu_read_lock(), and result refcount is unchanged.
813 */
814
815struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
816 unsigned short mask)
817{
818 struct net_device *dev, *ret;
819
820 ret = NULL;
821 for_each_netdev_rcu(net, dev) {
822 if (((dev->flags ^ if_flags) & mask) == 0) {
823 ret = dev;
824 break;
825 }
826 }
827 return ret;
828}
829EXPORT_SYMBOL(dev_get_by_flags_rcu);
830
831/**
832 * dev_valid_name - check if name is okay for network device
833 * @name: name string
834 *
835 * Network device names need to be valid file names to
836 * to allow sysfs to work. We also disallow any kind of
837 * whitespace.
838 */
839int dev_valid_name(const char *name)
840{
841 if (*name == '\0')
842 return 0;
843 if (strlen(name) >= IFNAMSIZ)
844 return 0;
845 if (!strcmp(name, ".") || !strcmp(name, ".."))
846 return 0;
847
848 while (*name) {
849 if (*name == '/' || isspace(*name))
850 return 0;
851 name++;
852 }
853 return 1;
854}
855EXPORT_SYMBOL(dev_valid_name);
856
857/**
858 * __dev_alloc_name - allocate a name for a device
859 * @net: network namespace to allocate the device name in
860 * @name: name format string
861 * @buf: scratch buffer and result name string
862 *
863 * Passed a format string - eg "lt%d" it will try and find a suitable
864 * id. It scans list of devices to build up a free map, then chooses
865 * the first empty slot. The caller must hold the dev_base or rtnl lock
866 * while allocating the name and adding the device in order to avoid
867 * duplicates.
868 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
869 * Returns the number of the unit assigned or a negative errno code.
870 */
871
872static int __dev_alloc_name(struct net *net, const char *name, char *buf)
873{
874 int i = 0;
875 const char *p;
876 const int max_netdevices = 8*PAGE_SIZE;
877 unsigned long *inuse;
878 struct net_device *d;
879
880 p = strnchr(name, IFNAMSIZ-1, '%');
881 if (p) {
882 /*
883 * Verify the string as this thing may have come from
884 * the user. There must be either one "%d" and no other "%"
885 * characters.
886 */
887 if (p[1] != 'd' || strchr(p + 2, '%'))
888 return -EINVAL;
889
890 /* Use one page as a bit array of possible slots */
891 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
892 if (!inuse)
893 return -ENOMEM;
894
895 for_each_netdev(net, d) {
896 if (!sscanf(d->name, name, &i))
897 continue;
898 if (i < 0 || i >= max_netdevices)
899 continue;
900
901 /* avoid cases where sscanf is not exact inverse of printf */
902 snprintf(buf, IFNAMSIZ, name, i);
903 if (!strncmp(buf, d->name, IFNAMSIZ))
904 set_bit(i, inuse);
905 }
906
907 i = find_first_zero_bit(inuse, max_netdevices);
908 free_page((unsigned long) inuse);
909 }
910
911 if (buf != name)
912 snprintf(buf, IFNAMSIZ, name, i);
913 if (!__dev_get_by_name(net, buf))
914 return i;
915
916 /* It is possible to run out of possible slots
917 * when the name is long and there isn't enough space left
918 * for the digits, or if all bits are used.
919 */
920 return -ENFILE;
921}
922
923/**
924 * dev_alloc_name - allocate a name for a device
925 * @dev: device
926 * @name: name format string
927 *
928 * Passed a format string - eg "lt%d" it will try and find a suitable
929 * id. It scans list of devices to build up a free map, then chooses
930 * the first empty slot. The caller must hold the dev_base or rtnl lock
931 * while allocating the name and adding the device in order to avoid
932 * duplicates.
933 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
934 * Returns the number of the unit assigned or a negative errno code.
935 */
936
937int dev_alloc_name(struct net_device *dev, const char *name)
938{
939 char buf[IFNAMSIZ];
940 struct net *net;
941 int ret;
942
943 BUG_ON(!dev_net(dev));
944 net = dev_net(dev);
945 ret = __dev_alloc_name(net, name, buf);
946 if (ret >= 0)
947 strlcpy(dev->name, buf, IFNAMSIZ);
948 return ret;
949}
950EXPORT_SYMBOL(dev_alloc_name);
951
952static int dev_get_valid_name(struct net_device *dev, const char *name, bool fmt)
953{
954 struct net *net;
955
956 BUG_ON(!dev_net(dev));
957 net = dev_net(dev);
958
959 if (!dev_valid_name(name))
960 return -EINVAL;
961
962 if (fmt && strchr(name, '%'))
963 return dev_alloc_name(dev, name);
964 else if (__dev_get_by_name(net, name))
965 return -EEXIST;
966 else if (dev->name != name)
967 strlcpy(dev->name, name, IFNAMSIZ);
968
969 return 0;
970}
971
972/**
973 * dev_change_name - change name of a device
974 * @dev: device
975 * @newname: name (or format string) must be at least IFNAMSIZ
976 *
977 * Change name of a device, can pass format strings "eth%d".
978 * for wildcarding.
979 */
980int dev_change_name(struct net_device *dev, const char *newname)
981{
982 char oldname[IFNAMSIZ];
983 int err = 0;
984 int ret;
985 struct net *net;
986
987 ASSERT_RTNL();
988 BUG_ON(!dev_net(dev));
989
990 net = dev_net(dev);
991 if (dev->flags & IFF_UP)
992 return -EBUSY;
993
994 if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
995 return 0;
996
997 memcpy(oldname, dev->name, IFNAMSIZ);
998
999 err = dev_get_valid_name(dev, newname, 1);
1000 if (err < 0)
1001 return err;
1002
1003rollback:
1004 ret = device_rename(&dev->dev, dev->name);
1005 if (ret) {
1006 memcpy(dev->name, oldname, IFNAMSIZ);
1007 return ret;
1008 }
1009
1010 write_lock_bh(&dev_base_lock);
1011 hlist_del(&dev->name_hlist);
1012 write_unlock_bh(&dev_base_lock);
1013
1014 synchronize_rcu();
1015
1016 write_lock_bh(&dev_base_lock);
1017 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
1018 write_unlock_bh(&dev_base_lock);
1019
1020 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
1021 ret = notifier_to_errno(ret);
1022
1023 if (ret) {
1024 /* err >= 0 after dev_alloc_name() or stores the first errno */
1025 if (err >= 0) {
1026 err = ret;
1027 memcpy(dev->name, oldname, IFNAMSIZ);
1028 goto rollback;
1029 } else {
1030 printk(KERN_ERR
1031 "%s: name change rollback failed: %d.\n",
1032 dev->name, ret);
1033 }
1034 }
1035
1036 return err;
1037}
1038
1039/**
1040 * dev_set_alias - change ifalias of a device
1041 * @dev: device
1042 * @alias: name up to IFALIASZ
1043 * @len: limit of bytes to copy from info
1044 *
1045 * Set ifalias for a device,
1046 */
1047int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1048{
1049 ASSERT_RTNL();
1050
1051 if (len >= IFALIASZ)
1052 return -EINVAL;
1053
1054 if (!len) {
1055 if (dev->ifalias) {
1056 kfree(dev->ifalias);
1057 dev->ifalias = NULL;
1058 }
1059 return 0;
1060 }
1061
1062 dev->ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
1063 if (!dev->ifalias)
1064 return -ENOMEM;
1065
1066 strlcpy(dev->ifalias, alias, len+1);
1067 return len;
1068}
1069
1070
1071/**
1072 * netdev_features_change - device changes features
1073 * @dev: device to cause notification
1074 *
1075 * Called to indicate a device has changed features.
1076 */
1077void netdev_features_change(struct net_device *dev)
1078{
1079 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
1080}
1081EXPORT_SYMBOL(netdev_features_change);
1082
1083/**
1084 * netdev_state_change - device changes state
1085 * @dev: device to cause notification
1086 *
1087 * Called to indicate a device has changed state. This function calls
1088 * the notifier chains for netdev_chain and sends a NEWLINK message
1089 * to the routing socket.
1090 */
1091void netdev_state_change(struct net_device *dev)
1092{
1093 if (dev->flags & IFF_UP) {
1094 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1095 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1096 }
1097}
1098EXPORT_SYMBOL(netdev_state_change);
1099
1100int netdev_bonding_change(struct net_device *dev, unsigned long event)
1101{
1102 return call_netdevice_notifiers(event, dev);
1103}
1104EXPORT_SYMBOL(netdev_bonding_change);
1105
1106/**
1107 * dev_load - load a network module
1108 * @net: the applicable net namespace
1109 * @name: name of interface
1110 *
1111 * If a network interface is not present and the process has suitable
1112 * privileges this function loads the module. If module loading is not
1113 * available in this kernel then it becomes a nop.
1114 */
1115
1116void dev_load(struct net *net, const char *name)
1117{
1118 struct net_device *dev;
1119
1120 rcu_read_lock();
1121 dev = dev_get_by_name_rcu(net, name);
1122 rcu_read_unlock();
1123
1124 if (!dev && capable(CAP_NET_ADMIN))
1125 request_module("%s", name);
1126}
1127EXPORT_SYMBOL(dev_load);
1128
1129static int __dev_open(struct net_device *dev)
1130{
1131 const struct net_device_ops *ops = dev->netdev_ops;
1132 int ret;
1133
1134 ASSERT_RTNL();
1135
1136 /*
1137 * Is it even present?
1138 */
1139 if (!netif_device_present(dev))
1140 return -ENODEV;
1141
1142 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1143 ret = notifier_to_errno(ret);
1144 if (ret)
1145 return ret;
1146
1147 /*
1148 * Call device private open method
1149 */
1150 set_bit(__LINK_STATE_START, &dev->state);
1151
1152 if (ops->ndo_validate_addr)
1153 ret = ops->ndo_validate_addr(dev);
1154
1155 if (!ret && ops->ndo_open)
1156 ret = ops->ndo_open(dev);
1157
1158 /*
1159 * If it went open OK then:
1160 */
1161
1162 if (ret)
1163 clear_bit(__LINK_STATE_START, &dev->state);
1164 else {
1165 /*
1166 * Set the flags.
1167 */
1168 dev->flags |= IFF_UP;
1169
1170 /*
1171 * Enable NET_DMA
1172 */
1173 net_dmaengine_get();
1174
1175 /*
1176 * Initialize multicasting status
1177 */
1178 dev_set_rx_mode(dev);
1179
1180 /*
1181 * Wakeup transmit queue engine
1182 */
1183 dev_activate(dev);
1184 }
1185
1186 return ret;
1187}
1188
1189/**
1190 * dev_open - prepare an interface for use.
1191 * @dev: device to open
1192 *
1193 * Takes a device from down to up state. The device's private open
1194 * function is invoked and then the multicast lists are loaded. Finally
1195 * the device is moved into the up state and a %NETDEV_UP message is
1196 * sent to the netdev notifier chain.
1197 *
1198 * Calling this function on an active interface is a nop. On a failure
1199 * a negative errno code is returned.
1200 */
1201int dev_open(struct net_device *dev)
1202{
1203 int ret;
1204
1205 /*
1206 * Is it already up?
1207 */
1208 if (dev->flags & IFF_UP)
1209 return 0;
1210
1211 /*
1212 * Open device
1213 */
1214 ret = __dev_open(dev);
1215 if (ret < 0)
1216 return ret;
1217
1218 /*
1219 * ... and announce new interface.
1220 */
1221 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1222 call_netdevice_notifiers(NETDEV_UP, dev);
1223
1224 return ret;
1225}
1226EXPORT_SYMBOL(dev_open);
1227
1228static int __dev_close(struct net_device *dev)
1229{
1230 const struct net_device_ops *ops = dev->netdev_ops;
1231
1232 ASSERT_RTNL();
1233 might_sleep();
1234
1235 /*
1236 * Tell people we are going down, so that they can
1237 * prepare to death, when device is still operating.
1238 */
1239 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1240
1241 clear_bit(__LINK_STATE_START, &dev->state);
1242
1243 /* Synchronize to scheduled poll. We cannot touch poll list,
1244 * it can be even on different cpu. So just clear netif_running().
1245 *
1246 * dev->stop() will invoke napi_disable() on all of it's
1247 * napi_struct instances on this device.
1248 */
1249 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1250
1251 dev_deactivate(dev);
1252
1253 /*
1254 * Call the device specific close. This cannot fail.
1255 * Only if device is UP
1256 *
1257 * We allow it to be called even after a DETACH hot-plug
1258 * event.
1259 */
1260 if (ops->ndo_stop)
1261 ops->ndo_stop(dev);
1262
1263 /*
1264 * Device is now down.
1265 */
1266
1267 dev->flags &= ~IFF_UP;
1268
1269 /*
1270 * Shutdown NET_DMA
1271 */
1272 net_dmaengine_put();
1273
1274 return 0;
1275}
1276
1277/**
1278 * dev_close - shutdown an interface.
1279 * @dev: device to shutdown
1280 *
1281 * This function moves an active device into down state. A
1282 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1283 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1284 * chain.
1285 */
1286int dev_close(struct net_device *dev)
1287{
1288 if (!(dev->flags & IFF_UP))
1289 return 0;
1290
1291 __dev_close(dev);
1292
1293 /*
1294 * Tell people we are down
1295 */
1296 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1297 call_netdevice_notifiers(NETDEV_DOWN, dev);
1298
1299 return 0;
1300}
1301EXPORT_SYMBOL(dev_close);
1302
1303
1304/**
1305 * dev_disable_lro - disable Large Receive Offload on a device
1306 * @dev: device
1307 *
1308 * Disable Large Receive Offload (LRO) on a net device. Must be
1309 * called under RTNL. This is needed if received packets may be
1310 * forwarded to another interface.
1311 */
1312void dev_disable_lro(struct net_device *dev)
1313{
1314 if (dev->ethtool_ops && dev->ethtool_ops->get_flags &&
1315 dev->ethtool_ops->set_flags) {
1316 u32 flags = dev->ethtool_ops->get_flags(dev);
1317 if (flags & ETH_FLAG_LRO) {
1318 flags &= ~ETH_FLAG_LRO;
1319 dev->ethtool_ops->set_flags(dev, flags);
1320 }
1321 }
1322 WARN_ON(dev->features & NETIF_F_LRO);
1323}
1324EXPORT_SYMBOL(dev_disable_lro);
1325
1326
1327static int dev_boot_phase = 1;
1328
1329/*
1330 * Device change register/unregister. These are not inline or static
1331 * as we export them to the world.
1332 */
1333
1334/**
1335 * register_netdevice_notifier - register a network notifier block
1336 * @nb: notifier
1337 *
1338 * Register a notifier to be called when network device events occur.
1339 * The notifier passed is linked into the kernel structures and must
1340 * not be reused until it has been unregistered. A negative errno code
1341 * is returned on a failure.
1342 *
1343 * When registered all registration and up events are replayed
1344 * to the new notifier to allow device to have a race free
1345 * view of the network device list.
1346 */
1347
1348int register_netdevice_notifier(struct notifier_block *nb)
1349{
1350 struct net_device *dev;
1351 struct net_device *last;
1352 struct net *net;
1353 int err;
1354
1355 rtnl_lock();
1356 err = raw_notifier_chain_register(&netdev_chain, nb);
1357 if (err)
1358 goto unlock;
1359 if (dev_boot_phase)
1360 goto unlock;
1361 for_each_net(net) {
1362 for_each_netdev(net, dev) {
1363 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1364 err = notifier_to_errno(err);
1365 if (err)
1366 goto rollback;
1367
1368 if (!(dev->flags & IFF_UP))
1369 continue;
1370
1371 nb->notifier_call(nb, NETDEV_UP, dev);
1372 }
1373 }
1374
1375unlock:
1376 rtnl_unlock();
1377 return err;
1378
1379rollback:
1380 last = dev;
1381 for_each_net(net) {
1382 for_each_netdev(net, dev) {
1383 if (dev == last)
1384 break;
1385
1386 if (dev->flags & IFF_UP) {
1387 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1388 nb->notifier_call(nb, NETDEV_DOWN, dev);
1389 }
1390 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
1391 nb->notifier_call(nb, NETDEV_UNREGISTER_BATCH, dev);
1392 }
1393 }
1394
1395 raw_notifier_chain_unregister(&netdev_chain, nb);
1396 goto unlock;
1397}
1398EXPORT_SYMBOL(register_netdevice_notifier);
1399
1400/**
1401 * unregister_netdevice_notifier - unregister a network notifier block
1402 * @nb: notifier
1403 *
1404 * Unregister a notifier previously registered by
1405 * register_netdevice_notifier(). The notifier is unlinked into the
1406 * kernel structures and may then be reused. A negative errno code
1407 * is returned on a failure.
1408 */
1409
1410int unregister_netdevice_notifier(struct notifier_block *nb)
1411{
1412 int err;
1413
1414 rtnl_lock();
1415 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1416 rtnl_unlock();
1417 return err;
1418}
1419EXPORT_SYMBOL(unregister_netdevice_notifier);
1420
1421/**
1422 * call_netdevice_notifiers - call all network notifier blocks
1423 * @val: value passed unmodified to notifier function
1424 * @dev: net_device pointer passed unmodified to notifier function
1425 *
1426 * Call all network notifier blocks. Parameters and return value
1427 * are as for raw_notifier_call_chain().
1428 */
1429
1430int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1431{
1432 ASSERT_RTNL();
1433 return raw_notifier_call_chain(&netdev_chain, val, dev);
1434}
1435
1436/* When > 0 there are consumers of rx skb time stamps */
1437static atomic_t netstamp_needed = ATOMIC_INIT(0);
1438
1439void net_enable_timestamp(void)
1440{
1441 atomic_inc(&netstamp_needed);
1442}
1443EXPORT_SYMBOL(net_enable_timestamp);
1444
1445void net_disable_timestamp(void)
1446{
1447 atomic_dec(&netstamp_needed);
1448}
1449EXPORT_SYMBOL(net_disable_timestamp);
1450
1451static inline void net_timestamp_set(struct sk_buff *skb)
1452{
1453 if (atomic_read(&netstamp_needed))
1454 __net_timestamp(skb);
1455 else
1456 skb->tstamp.tv64 = 0;
1457}
1458
1459static inline void net_timestamp_check(struct sk_buff *skb)
1460{
1461 if (!skb->tstamp.tv64 && atomic_read(&netstamp_needed))
1462 __net_timestamp(skb);
1463}
1464
1465/**
1466 * dev_forward_skb - loopback an skb to another netif
1467 *
1468 * @dev: destination network device
1469 * @skb: buffer to forward
1470 *
1471 * return values:
1472 * NET_RX_SUCCESS (no congestion)
1473 * NET_RX_DROP (packet was dropped, but freed)
1474 *
1475 * dev_forward_skb can be used for injecting an skb from the
1476 * start_xmit function of one device into the receive queue
1477 * of another device.
1478 *
1479 * The receiving device may be in another namespace, so
1480 * we have to clear all information in the skb that could
1481 * impact namespace isolation.
1482 */
1483int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1484{
1485 skb_orphan(skb);
1486 nf_reset(skb);
1487
1488 if (!(dev->flags & IFF_UP) ||
1489 (skb->len > (dev->mtu + dev->hard_header_len))) {
1490 kfree_skb(skb);
1491 return NET_RX_DROP;
1492 }
1493 skb_set_dev(skb, dev);
1494 skb->tstamp.tv64 = 0;
1495 skb->pkt_type = PACKET_HOST;
1496 skb->protocol = eth_type_trans(skb, dev);
1497 return netif_rx(skb);
1498}
1499EXPORT_SYMBOL_GPL(dev_forward_skb);
1500
1501/*
1502 * Support routine. Sends outgoing frames to any network
1503 * taps currently in use.
1504 */
1505
1506static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1507{
1508 struct packet_type *ptype;
1509
1510#ifdef CONFIG_NET_CLS_ACT
1511 if (!(skb->tstamp.tv64 && (G_TC_FROM(skb->tc_verd) & AT_INGRESS)))
1512 net_timestamp_set(skb);
1513#else
1514 net_timestamp_set(skb);
1515#endif
1516
1517 rcu_read_lock();
1518 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1519 /* Never send packets back to the socket
1520 * they originated from - MvS (miquels@drinkel.ow.org)
1521 */
1522 if ((ptype->dev == dev || !ptype->dev) &&
1523 (ptype->af_packet_priv == NULL ||
1524 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1525 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1526 if (!skb2)
1527 break;
1528
1529 /* skb->nh should be correctly
1530 set by sender, so that the second statement is
1531 just protection against buggy protocols.
1532 */
1533 skb_reset_mac_header(skb2);
1534
1535 if (skb_network_header(skb2) < skb2->data ||
1536 skb2->network_header > skb2->tail) {
1537 if (net_ratelimit())
1538 printk(KERN_CRIT "protocol %04x is "
1539 "buggy, dev %s\n",
1540 ntohs(skb2->protocol),
1541 dev->name);
1542 skb_reset_network_header(skb2);
1543 }
1544
1545 skb2->transport_header = skb2->network_header;
1546 skb2->pkt_type = PACKET_OUTGOING;
1547 ptype->func(skb2, skb->dev, ptype, skb->dev);
1548 }
1549 }
1550 rcu_read_unlock();
1551}
1552
1553/*
1554 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
1555 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
1556 */
1557void netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
1558{
1559 unsigned int real_num = dev->real_num_tx_queues;
1560
1561 if (unlikely(txq > dev->num_tx_queues))
1562 ;
1563 else if (txq > real_num)
1564 dev->real_num_tx_queues = txq;
1565 else if (txq < real_num) {
1566 dev->real_num_tx_queues = txq;
1567 qdisc_reset_all_tx_gt(dev, txq);
1568 }
1569}
1570EXPORT_SYMBOL(netif_set_real_num_tx_queues);
1571
1572static inline void __netif_reschedule(struct Qdisc *q)
1573{
1574 struct softnet_data *sd;
1575 unsigned long flags;
1576
1577 local_irq_save(flags);
1578 sd = &__get_cpu_var(softnet_data);
1579 q->next_sched = NULL;
1580 *sd->output_queue_tailp = q;
1581 sd->output_queue_tailp = &q->next_sched;
1582 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1583 local_irq_restore(flags);
1584}
1585
1586void __netif_schedule(struct Qdisc *q)
1587{
1588 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
1589 __netif_reschedule(q);
1590}
1591EXPORT_SYMBOL(__netif_schedule);
1592
1593void dev_kfree_skb_irq(struct sk_buff *skb)
1594{
1595 if (atomic_dec_and_test(&skb->users)) {
1596 struct softnet_data *sd;
1597 unsigned long flags;
1598
1599 local_irq_save(flags);
1600 sd = &__get_cpu_var(softnet_data);
1601 skb->next = sd->completion_queue;
1602 sd->completion_queue = skb;
1603 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1604 local_irq_restore(flags);
1605 }
1606}
1607EXPORT_SYMBOL(dev_kfree_skb_irq);
1608
1609void dev_kfree_skb_any(struct sk_buff *skb)
1610{
1611 if (in_irq() || irqs_disabled())
1612 dev_kfree_skb_irq(skb);
1613 else
1614 dev_kfree_skb(skb);
1615}
1616EXPORT_SYMBOL(dev_kfree_skb_any);
1617
1618
1619/**
1620 * netif_device_detach - mark device as removed
1621 * @dev: network device
1622 *
1623 * Mark device as removed from system and therefore no longer available.
1624 */
1625void netif_device_detach(struct net_device *dev)
1626{
1627 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1628 netif_running(dev)) {
1629 netif_tx_stop_all_queues(dev);
1630 }
1631}
1632EXPORT_SYMBOL(netif_device_detach);
1633
1634/**
1635 * netif_device_attach - mark device as attached
1636 * @dev: network device
1637 *
1638 * Mark device as attached from system and restart if needed.
1639 */
1640void netif_device_attach(struct net_device *dev)
1641{
1642 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1643 netif_running(dev)) {
1644 netif_tx_wake_all_queues(dev);
1645 __netdev_watchdog_up(dev);
1646 }
1647}
1648EXPORT_SYMBOL(netif_device_attach);
1649
1650static bool can_checksum_protocol(unsigned long features, __be16 protocol)
1651{
1652 return ((features & NETIF_F_GEN_CSUM) ||
1653 ((features & NETIF_F_IP_CSUM) &&
1654 protocol == htons(ETH_P_IP)) ||
1655 ((features & NETIF_F_IPV6_CSUM) &&
1656 protocol == htons(ETH_P_IPV6)) ||
1657 ((features & NETIF_F_FCOE_CRC) &&
1658 protocol == htons(ETH_P_FCOE)));
1659}
1660
1661static bool dev_can_checksum(struct net_device *dev, struct sk_buff *skb)
1662{
1663 if (can_checksum_protocol(dev->features, skb->protocol))
1664 return true;
1665
1666 if (skb->protocol == htons(ETH_P_8021Q)) {
1667 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
1668 if (can_checksum_protocol(dev->features & dev->vlan_features,
1669 veh->h_vlan_encapsulated_proto))
1670 return true;
1671 }
1672
1673 return false;
1674}
1675
1676/**
1677 * skb_dev_set -- assign a new device to a buffer
1678 * @skb: buffer for the new device
1679 * @dev: network device
1680 *
1681 * If an skb is owned by a device already, we have to reset
1682 * all data private to the namespace a device belongs to
1683 * before assigning it a new device.
1684 */
1685#ifdef CONFIG_NET_NS
1686void skb_set_dev(struct sk_buff *skb, struct net_device *dev)
1687{
1688 skb_dst_drop(skb);
1689 if (skb->dev && !net_eq(dev_net(skb->dev), dev_net(dev))) {
1690 secpath_reset(skb);
1691 nf_reset(skb);
1692 skb_init_secmark(skb);
1693 skb->mark = 0;
1694 skb->priority = 0;
1695 skb->nf_trace = 0;
1696 skb->ipvs_property = 0;
1697#ifdef CONFIG_NET_SCHED
1698 skb->tc_index = 0;
1699#endif
1700 }
1701 skb->dev = dev;
1702}
1703EXPORT_SYMBOL(skb_set_dev);
1704#endif /* CONFIG_NET_NS */
1705
1706/*
1707 * Invalidate hardware checksum when packet is to be mangled, and
1708 * complete checksum manually on outgoing path.
1709 */
1710int skb_checksum_help(struct sk_buff *skb)
1711{
1712 __wsum csum;
1713 int ret = 0, offset;
1714
1715 if (skb->ip_summed == CHECKSUM_COMPLETE)
1716 goto out_set_summed;
1717
1718 if (unlikely(skb_shinfo(skb)->gso_size)) {
1719 /* Let GSO fix up the checksum. */
1720 goto out_set_summed;
1721 }
1722
1723 offset = skb->csum_start - skb_headroom(skb);
1724 BUG_ON(offset >= skb_headlen(skb));
1725 csum = skb_checksum(skb, offset, skb->len - offset, 0);
1726
1727 offset += skb->csum_offset;
1728 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
1729
1730 if (skb_cloned(skb) &&
1731 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
1732 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1733 if (ret)
1734 goto out;
1735 }
1736
1737 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1738out_set_summed:
1739 skb->ip_summed = CHECKSUM_NONE;
1740out:
1741 return ret;
1742}
1743EXPORT_SYMBOL(skb_checksum_help);
1744
1745/**
1746 * skb_gso_segment - Perform segmentation on skb.
1747 * @skb: buffer to segment
1748 * @features: features for the output path (see dev->features)
1749 *
1750 * This function segments the given skb and returns a list of segments.
1751 *
1752 * It may return NULL if the skb requires no segmentation. This is
1753 * only possible when GSO is used for verifying header integrity.
1754 */
1755struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
1756{
1757 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1758 struct packet_type *ptype;
1759 __be16 type = skb->protocol;
1760 int err;
1761
1762 skb_reset_mac_header(skb);
1763 skb->mac_len = skb->network_header - skb->mac_header;
1764 __skb_pull(skb, skb->mac_len);
1765
1766 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1767 struct net_device *dev = skb->dev;
1768 struct ethtool_drvinfo info = {};
1769
1770 if (dev && dev->ethtool_ops && dev->ethtool_ops->get_drvinfo)
1771 dev->ethtool_ops->get_drvinfo(dev, &info);
1772
1773 WARN(1, "%s: caps=(0x%lx, 0x%lx) len=%d data_len=%d "
1774 "ip_summed=%d",
1775 info.driver, dev ? dev->features : 0L,
1776 skb->sk ? skb->sk->sk_route_caps : 0L,
1777 skb->len, skb->data_len, skb->ip_summed);
1778
1779 if (skb_header_cloned(skb) &&
1780 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1781 return ERR_PTR(err);
1782 }
1783
1784 rcu_read_lock();
1785 list_for_each_entry_rcu(ptype,
1786 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
1787 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
1788 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1789 err = ptype->gso_send_check(skb);
1790 segs = ERR_PTR(err);
1791 if (err || skb_gso_ok(skb, features))
1792 break;
1793 __skb_push(skb, (skb->data -
1794 skb_network_header(skb)));
1795 }
1796 segs = ptype->gso_segment(skb, features);
1797 break;
1798 }
1799 }
1800 rcu_read_unlock();
1801
1802 __skb_push(skb, skb->data - skb_mac_header(skb));
1803
1804 return segs;
1805}
1806EXPORT_SYMBOL(skb_gso_segment);
1807
1808/* Take action when hardware reception checksum errors are detected. */
1809#ifdef CONFIG_BUG
1810void netdev_rx_csum_fault(struct net_device *dev)
1811{
1812 if (net_ratelimit()) {
1813 printk(KERN_ERR "%s: hw csum failure.\n",
1814 dev ? dev->name : "<unknown>");
1815 dump_stack();
1816 }
1817}
1818EXPORT_SYMBOL(netdev_rx_csum_fault);
1819#endif
1820
1821/* Actually, we should eliminate this check as soon as we know, that:
1822 * 1. IOMMU is present and allows to map all the memory.
1823 * 2. No high memory really exists on this machine.
1824 */
1825
1826static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1827{
1828#ifdef CONFIG_HIGHMEM
1829 int i;
1830 if (!(dev->features & NETIF_F_HIGHDMA)) {
1831 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1832 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1833 return 1;
1834 }
1835
1836 if (PCI_DMA_BUS_IS_PHYS) {
1837 struct device *pdev = dev->dev.parent;
1838
1839 if (!pdev)
1840 return 0;
1841 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1842 dma_addr_t addr = page_to_phys(skb_shinfo(skb)->frags[i].page);
1843 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
1844 return 1;
1845 }
1846 }
1847#endif
1848 return 0;
1849}
1850
1851struct dev_gso_cb {
1852 void (*destructor)(struct sk_buff *skb);
1853};
1854
1855#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1856
1857static void dev_gso_skb_destructor(struct sk_buff *skb)
1858{
1859 struct dev_gso_cb *cb;
1860
1861 do {
1862 struct sk_buff *nskb = skb->next;
1863
1864 skb->next = nskb->next;
1865 nskb->next = NULL;
1866 kfree_skb(nskb);
1867 } while (skb->next);
1868
1869 cb = DEV_GSO_CB(skb);
1870 if (cb->destructor)
1871 cb->destructor(skb);
1872}
1873
1874/**
1875 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1876 * @skb: buffer to segment
1877 *
1878 * This function segments the given skb and stores the list of segments
1879 * in skb->next.
1880 */
1881static int dev_gso_segment(struct sk_buff *skb)
1882{
1883 struct net_device *dev = skb->dev;
1884 struct sk_buff *segs;
1885 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1886 NETIF_F_SG : 0);
1887
1888 segs = skb_gso_segment(skb, features);
1889
1890 /* Verifying header integrity only. */
1891 if (!segs)
1892 return 0;
1893
1894 if (IS_ERR(segs))
1895 return PTR_ERR(segs);
1896
1897 skb->next = segs;
1898 DEV_GSO_CB(skb)->destructor = skb->destructor;
1899 skb->destructor = dev_gso_skb_destructor;
1900
1901 return 0;
1902}
1903
1904/*
1905 * Try to orphan skb early, right before transmission by the device.
1906 * We cannot orphan skb if tx timestamp is requested, since
1907 * drivers need to call skb_tstamp_tx() to send the timestamp.
1908 */
1909static inline void skb_orphan_try(struct sk_buff *skb)
1910{
1911 struct sock *sk = skb->sk;
1912
1913 if (sk && !skb_tx(skb)->flags) {
1914 /* skb_tx_hash() wont be able to get sk.
1915 * We copy sk_hash into skb->rxhash
1916 */
1917 if (!skb->rxhash)
1918 skb->rxhash = sk->sk_hash;
1919 skb_orphan(skb);
1920 }
1921}
1922
1923/*
1924 * Returns true if either:
1925 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
1926 * 2. skb is fragmented and the device does not support SG, or if
1927 * at least one of fragments is in highmem and device does not
1928 * support DMA from it.
1929 */
1930static inline int skb_needs_linearize(struct sk_buff *skb,
1931 struct net_device *dev)
1932{
1933 return skb_is_nonlinear(skb) &&
1934 ((skb_has_frags(skb) && !(dev->features & NETIF_F_FRAGLIST)) ||
1935 (skb_shinfo(skb)->nr_frags && (!(dev->features & NETIF_F_SG) ||
1936 illegal_highdma(dev, skb))));
1937}
1938
1939int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
1940 struct netdev_queue *txq)
1941{
1942 const struct net_device_ops *ops = dev->netdev_ops;
1943 int rc = NETDEV_TX_OK;
1944
1945 if (likely(!skb->next)) {
1946 if (!list_empty(&ptype_all))
1947 dev_queue_xmit_nit(skb, dev);
1948
1949 /*
1950 * If device doesnt need skb->dst, release it right now while
1951 * its hot in this cpu cache
1952 */
1953 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
1954 skb_dst_drop(skb);
1955
1956 skb_orphan_try(skb);
1957
1958 if (netif_needs_gso(dev, skb)) {
1959 if (unlikely(dev_gso_segment(skb)))
1960 goto out_kfree_skb;
1961 if (skb->next)
1962 goto gso;
1963 } else {
1964 if (skb_needs_linearize(skb, dev) &&
1965 __skb_linearize(skb))
1966 goto out_kfree_skb;
1967
1968 /* If packet is not checksummed and device does not
1969 * support checksumming for this protocol, complete
1970 * checksumming here.
1971 */
1972 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1973 skb_set_transport_header(skb, skb->csum_start -
1974 skb_headroom(skb));
1975 if (!dev_can_checksum(dev, skb) &&
1976 skb_checksum_help(skb))
1977 goto out_kfree_skb;
1978 }
1979 }
1980
1981 rc = ops->ndo_start_xmit(skb, dev);
1982 trace_net_dev_xmit(skb, rc);
1983 if (rc == NETDEV_TX_OK)
1984 txq_trans_update(txq);
1985 return rc;
1986 }
1987
1988gso:
1989 do {
1990 struct sk_buff *nskb = skb->next;
1991
1992 skb->next = nskb->next;
1993 nskb->next = NULL;
1994
1995 /*
1996 * If device doesnt need nskb->dst, release it right now while
1997 * its hot in this cpu cache
1998 */
1999 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2000 skb_dst_drop(nskb);
2001
2002 rc = ops->ndo_start_xmit(nskb, dev);
2003 trace_net_dev_xmit(nskb, rc);
2004 if (unlikely(rc != NETDEV_TX_OK)) {
2005 if (rc & ~NETDEV_TX_MASK)
2006 goto out_kfree_gso_skb;
2007 nskb->next = skb->next;
2008 skb->next = nskb;
2009 return rc;
2010 }
2011 txq_trans_update(txq);
2012 if (unlikely(netif_tx_queue_stopped(txq) && skb->next))
2013 return NETDEV_TX_BUSY;
2014 } while (skb->next);
2015
2016out_kfree_gso_skb:
2017 if (likely(skb->next == NULL))
2018 skb->destructor = DEV_GSO_CB(skb)->destructor;
2019out_kfree_skb:
2020 kfree_skb(skb);
2021 return rc;
2022}
2023
2024static u32 hashrnd __read_mostly;
2025
2026u16 skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb)
2027{
2028 u32 hash;
2029
2030 if (skb_rx_queue_recorded(skb)) {
2031 hash = skb_get_rx_queue(skb);
2032 while (unlikely(hash >= dev->real_num_tx_queues))
2033 hash -= dev->real_num_tx_queues;
2034 return hash;
2035 }
2036
2037 if (skb->sk && skb->sk->sk_hash)
2038 hash = skb->sk->sk_hash;
2039 else
2040 hash = (__force u16) skb->protocol ^ skb->rxhash;
2041 hash = jhash_1word(hash, hashrnd);
2042
2043 return (u16) (((u64) hash * dev->real_num_tx_queues) >> 32);
2044}
2045EXPORT_SYMBOL(skb_tx_hash);
2046
2047static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
2048{
2049 if (unlikely(queue_index >= dev->real_num_tx_queues)) {
2050 if (net_ratelimit()) {
2051 pr_warning("%s selects TX queue %d, but "
2052 "real number of TX queues is %d\n",
2053 dev->name, queue_index, dev->real_num_tx_queues);
2054 }
2055 return 0;
2056 }
2057 return queue_index;
2058}
2059
2060static struct netdev_queue *dev_pick_tx(struct net_device *dev,
2061 struct sk_buff *skb)
2062{
2063 int queue_index;
2064 struct sock *sk = skb->sk;
2065
2066 queue_index = sk_tx_queue_get(sk);
2067 if (queue_index < 0) {
2068 const struct net_device_ops *ops = dev->netdev_ops;
2069
2070 if (ops->ndo_select_queue) {
2071 queue_index = ops->ndo_select_queue(dev, skb);
2072 queue_index = dev_cap_txqueue(dev, queue_index);
2073 } else {
2074 queue_index = 0;
2075 if (dev->real_num_tx_queues > 1)
2076 queue_index = skb_tx_hash(dev, skb);
2077
2078 if (sk) {
2079 struct dst_entry *dst = rcu_dereference_check(sk->sk_dst_cache, 1);
2080
2081 if (dst && skb_dst(skb) == dst)
2082 sk_tx_queue_set(sk, queue_index);
2083 }
2084 }
2085 }
2086
2087 skb_set_queue_mapping(skb, queue_index);
2088 return netdev_get_tx_queue(dev, queue_index);
2089}
2090
2091static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
2092 struct net_device *dev,
2093 struct netdev_queue *txq)
2094{
2095 spinlock_t *root_lock = qdisc_lock(q);
2096 bool contended = qdisc_is_running(q);
2097 int rc;
2098
2099 /*
2100 * Heuristic to force contended enqueues to serialize on a
2101 * separate lock before trying to get qdisc main lock.
2102 * This permits __QDISC_STATE_RUNNING owner to get the lock more often
2103 * and dequeue packets faster.
2104 */
2105 if (unlikely(contended))
2106 spin_lock(&q->busylock);
2107
2108 spin_lock(root_lock);
2109 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
2110 kfree_skb(skb);
2111 rc = NET_XMIT_DROP;
2112 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
2113 qdisc_run_begin(q)) {
2114 /*
2115 * This is a work-conserving queue; there are no old skbs
2116 * waiting to be sent out; and the qdisc is not running -
2117 * xmit the skb directly.
2118 */
2119 if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
2120 skb_dst_force(skb);
2121 __qdisc_update_bstats(q, skb->len);
2122 if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
2123 if (unlikely(contended)) {
2124 spin_unlock(&q->busylock);
2125 contended = false;
2126 }
2127 __qdisc_run(q);
2128 } else
2129 qdisc_run_end(q);
2130
2131 rc = NET_XMIT_SUCCESS;
2132 } else {
2133 skb_dst_force(skb);
2134 rc = qdisc_enqueue_root(skb, q);
2135 if (qdisc_run_begin(q)) {
2136 if (unlikely(contended)) {
2137 spin_unlock(&q->busylock);
2138 contended = false;
2139 }
2140 __qdisc_run(q);
2141 }
2142 }
2143 spin_unlock(root_lock);
2144 if (unlikely(contended))
2145 spin_unlock(&q->busylock);
2146 return rc;
2147}
2148
2149/**
2150 * dev_queue_xmit - transmit a buffer
2151 * @skb: buffer to transmit
2152 *
2153 * Queue a buffer for transmission to a network device. The caller must
2154 * have set the device and priority and built the buffer before calling
2155 * this function. The function can be called from an interrupt.
2156 *
2157 * A negative errno code is returned on a failure. A success does not
2158 * guarantee the frame will be transmitted as it may be dropped due
2159 * to congestion or traffic shaping.
2160 *
2161 * -----------------------------------------------------------------------------------
2162 * I notice this method can also return errors from the queue disciplines,
2163 * including NET_XMIT_DROP, which is a positive value. So, errors can also
2164 * be positive.
2165 *
2166 * Regardless of the return value, the skb is consumed, so it is currently
2167 * difficult to retry a send to this method. (You can bump the ref count
2168 * before sending to hold a reference for retry if you are careful.)
2169 *
2170 * When calling this method, interrupts MUST be enabled. This is because
2171 * the BH enable code must have IRQs enabled so that it will not deadlock.
2172 * --BLG
2173 */
2174int dev_queue_xmit(struct sk_buff *skb)
2175{
2176 struct net_device *dev = skb->dev;
2177 struct netdev_queue *txq;
2178 struct Qdisc *q;
2179 int rc = -ENOMEM;
2180
2181 /* Disable soft irqs for various locks below. Also
2182 * stops preemption for RCU.
2183 */
2184 rcu_read_lock_bh();
2185
2186 txq = dev_pick_tx(dev, skb);
2187 q = rcu_dereference_bh(txq->qdisc);
2188
2189#ifdef CONFIG_NET_CLS_ACT
2190 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
2191#endif
2192 trace_net_dev_queue(skb);
2193 if (q->enqueue) {
2194 rc = __dev_xmit_skb(skb, q, dev, txq);
2195 goto out;
2196 }
2197
2198 /* The device has no queue. Common case for software devices:
2199 loopback, all the sorts of tunnels...
2200
2201 Really, it is unlikely that netif_tx_lock protection is necessary
2202 here. (f.e. loopback and IP tunnels are clean ignoring statistics
2203 counters.)
2204 However, it is possible, that they rely on protection
2205 made by us here.
2206
2207 Check this and shot the lock. It is not prone from deadlocks.
2208 Either shot noqueue qdisc, it is even simpler 8)
2209 */
2210 if (dev->flags & IFF_UP) {
2211 int cpu = smp_processor_id(); /* ok because BHs are off */
2212
2213 if (txq->xmit_lock_owner != cpu) {
2214
2215 HARD_TX_LOCK(dev, txq, cpu);
2216
2217 if (!netif_tx_queue_stopped(txq)) {
2218 rc = dev_hard_start_xmit(skb, dev, txq);
2219 if (dev_xmit_complete(rc)) {
2220 HARD_TX_UNLOCK(dev, txq);
2221 goto out;
2222 }
2223 }
2224 HARD_TX_UNLOCK(dev, txq);
2225 if (net_ratelimit())
2226 printk(KERN_CRIT "Virtual device %s asks to "
2227 "queue packet!\n", dev->name);
2228 } else {
2229 /* Recursion is detected! It is possible,
2230 * unfortunately */
2231 if (net_ratelimit())
2232 printk(KERN_CRIT "Dead loop on virtual device "
2233 "%s, fix it urgently!\n", dev->name);
2234 }
2235 }
2236
2237 rc = -ENETDOWN;
2238 rcu_read_unlock_bh();
2239
2240 kfree_skb(skb);
2241 return rc;
2242out:
2243 rcu_read_unlock_bh();
2244 return rc;
2245}
2246EXPORT_SYMBOL(dev_queue_xmit);
2247
2248
2249/*=======================================================================
2250 Receiver routines
2251 =======================================================================*/
2252
2253int netdev_max_backlog __read_mostly = 1000;
2254int netdev_tstamp_prequeue __read_mostly = 1;
2255int netdev_budget __read_mostly = 300;
2256int weight_p __read_mostly = 64; /* old backlog weight */
2257
2258/* Called with irq disabled */
2259static inline void ____napi_schedule(struct softnet_data *sd,
2260 struct napi_struct *napi)
2261{
2262 list_add_tail(&napi->poll_list, &sd->poll_list);
2263 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2264}
2265
2266#ifdef CONFIG_RPS
2267
2268/* One global table that all flow-based protocols share. */
2269struct rps_sock_flow_table *rps_sock_flow_table __read_mostly;
2270EXPORT_SYMBOL(rps_sock_flow_table);
2271
2272/*
2273 * get_rps_cpu is called from netif_receive_skb and returns the target
2274 * CPU from the RPS map of the receiving queue for a given skb.
2275 * rcu_read_lock must be held on entry.
2276 */
2277static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
2278 struct rps_dev_flow **rflowp)
2279{
2280 struct ipv6hdr *ip6;
2281 struct iphdr *ip;
2282 struct netdev_rx_queue *rxqueue;
2283 struct rps_map *map;
2284 struct rps_dev_flow_table *flow_table;
2285 struct rps_sock_flow_table *sock_flow_table;
2286 int cpu = -1;
2287 u8 ip_proto;
2288 u16 tcpu;
2289 u32 addr1, addr2, ihl;
2290 union {
2291 u32 v32;
2292 u16 v16[2];
2293 } ports;
2294
2295 if (skb_rx_queue_recorded(skb)) {
2296 u16 index = skb_get_rx_queue(skb);
2297 if (unlikely(index >= dev->num_rx_queues)) {
2298 WARN_ONCE(dev->num_rx_queues > 1, "%s received packet "
2299 "on queue %u, but number of RX queues is %u\n",
2300 dev->name, index, dev->num_rx_queues);
2301 goto done;
2302 }
2303 rxqueue = dev->_rx + index;
2304 } else
2305 rxqueue = dev->_rx;
2306
2307 if (!rxqueue->rps_map && !rxqueue->rps_flow_table)
2308 goto done;
2309
2310 if (skb->rxhash)
2311 goto got_hash; /* Skip hash computation on packet header */
2312
2313 switch (skb->protocol) {
2314 case __constant_htons(ETH_P_IP):
2315 if (!pskb_may_pull(skb, sizeof(*ip)))
2316 goto done;
2317
2318 ip = (struct iphdr *) skb->data;
2319 ip_proto = ip->protocol;
2320 addr1 = (__force u32) ip->saddr;
2321 addr2 = (__force u32) ip->daddr;
2322 ihl = ip->ihl;
2323 break;
2324 case __constant_htons(ETH_P_IPV6):
2325 if (!pskb_may_pull(skb, sizeof(*ip6)))
2326 goto done;
2327
2328 ip6 = (struct ipv6hdr *) skb->data;
2329 ip_proto = ip6->nexthdr;
2330 addr1 = (__force u32) ip6->saddr.s6_addr32[3];
2331 addr2 = (__force u32) ip6->daddr.s6_addr32[3];
2332 ihl = (40 >> 2);
2333 break;
2334 default:
2335 goto done;
2336 }
2337 switch (ip_proto) {
2338 case IPPROTO_TCP:
2339 case IPPROTO_UDP:
2340 case IPPROTO_DCCP:
2341 case IPPROTO_ESP:
2342 case IPPROTO_AH:
2343 case IPPROTO_SCTP:
2344 case IPPROTO_UDPLITE:
2345 if (pskb_may_pull(skb, (ihl * 4) + 4)) {
2346 ports.v32 = * (__force u32 *) (skb->data + (ihl * 4));
2347 if (ports.v16[1] < ports.v16[0])
2348 swap(ports.v16[0], ports.v16[1]);
2349 break;
2350 }
2351 default:
2352 ports.v32 = 0;
2353 break;
2354 }
2355
2356 /* get a consistent hash (same value on both flow directions) */
2357 if (addr2 < addr1)
2358 swap(addr1, addr2);
2359 skb->rxhash = jhash_3words(addr1, addr2, ports.v32, hashrnd);
2360 if (!skb->rxhash)
2361 skb->rxhash = 1;
2362
2363got_hash:
2364 flow_table = rcu_dereference(rxqueue->rps_flow_table);
2365 sock_flow_table = rcu_dereference(rps_sock_flow_table);
2366 if (flow_table && sock_flow_table) {
2367 u16 next_cpu;
2368 struct rps_dev_flow *rflow;
2369
2370 rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
2371 tcpu = rflow->cpu;
2372
2373 next_cpu = sock_flow_table->ents[skb->rxhash &
2374 sock_flow_table->mask];
2375
2376 /*
2377 * If the desired CPU (where last recvmsg was done) is
2378 * different from current CPU (one in the rx-queue flow
2379 * table entry), switch if one of the following holds:
2380 * - Current CPU is unset (equal to RPS_NO_CPU).
2381 * - Current CPU is offline.
2382 * - The current CPU's queue tail has advanced beyond the
2383 * last packet that was enqueued using this table entry.
2384 * This guarantees that all previous packets for the flow
2385 * have been dequeued, thus preserving in order delivery.
2386 */
2387 if (unlikely(tcpu != next_cpu) &&
2388 (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
2389 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
2390 rflow->last_qtail)) >= 0)) {
2391 tcpu = rflow->cpu = next_cpu;
2392 if (tcpu != RPS_NO_CPU)
2393 rflow->last_qtail = per_cpu(softnet_data,
2394 tcpu).input_queue_head;
2395 }
2396 if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
2397 *rflowp = rflow;
2398 cpu = tcpu;
2399 goto done;
2400 }
2401 }
2402
2403 map = rcu_dereference(rxqueue->rps_map);
2404 if (map) {
2405 tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
2406
2407 if (cpu_online(tcpu)) {
2408 cpu = tcpu;
2409 goto done;
2410 }
2411 }
2412
2413done:
2414 return cpu;
2415}
2416
2417/* Called from hardirq (IPI) context */
2418static void rps_trigger_softirq(void *data)
2419{
2420 struct softnet_data *sd = data;
2421
2422 ____napi_schedule(sd, &sd->backlog);
2423 sd->received_rps++;
2424}
2425
2426#endif /* CONFIG_RPS */
2427
2428/*
2429 * Check if this softnet_data structure is another cpu one
2430 * If yes, queue it to our IPI list and return 1
2431 * If no, return 0
2432 */
2433static int rps_ipi_queued(struct softnet_data *sd)
2434{
2435#ifdef CONFIG_RPS
2436 struct softnet_data *mysd = &__get_cpu_var(softnet_data);
2437
2438 if (sd != mysd) {
2439 sd->rps_ipi_next = mysd->rps_ipi_list;
2440 mysd->rps_ipi_list = sd;
2441
2442 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2443 return 1;
2444 }
2445#endif /* CONFIG_RPS */
2446 return 0;
2447}
2448
2449/*
2450 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
2451 * queue (may be a remote CPU queue).
2452 */
2453static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
2454 unsigned int *qtail)
2455{
2456 struct softnet_data *sd;
2457 unsigned long flags;
2458
2459 sd = &per_cpu(softnet_data, cpu);
2460
2461 local_irq_save(flags);
2462
2463 rps_lock(sd);
2464 if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
2465 if (skb_queue_len(&sd->input_pkt_queue)) {
2466enqueue:
2467 __skb_queue_tail(&sd->input_pkt_queue, skb);
2468 input_queue_tail_incr_save(sd, qtail);
2469 rps_unlock(sd);
2470 local_irq_restore(flags);
2471 return NET_RX_SUCCESS;
2472 }
2473
2474 /* Schedule NAPI for backlog device
2475 * We can use non atomic operation since we own the queue lock
2476 */
2477 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
2478 if (!rps_ipi_queued(sd))
2479 ____napi_schedule(sd, &sd->backlog);
2480 }
2481 goto enqueue;
2482 }
2483
2484 sd->dropped++;
2485 rps_unlock(sd);
2486
2487 local_irq_restore(flags);
2488
2489 kfree_skb(skb);
2490 return NET_RX_DROP;
2491}
2492
2493/**
2494 * netif_rx - post buffer to the network code
2495 * @skb: buffer to post
2496 *
2497 * This function receives a packet from a device driver and queues it for
2498 * the upper (protocol) levels to process. It always succeeds. The buffer
2499 * may be dropped during processing for congestion control or by the
2500 * protocol layers.
2501 *
2502 * return values:
2503 * NET_RX_SUCCESS (no congestion)
2504 * NET_RX_DROP (packet was dropped)
2505 *
2506 */
2507
2508int netif_rx(struct sk_buff *skb)
2509{
2510 int ret;
2511
2512 /* if netpoll wants it, pretend we never saw it */
2513 if (netpoll_rx(skb))
2514 return NET_RX_DROP;
2515
2516 if (netdev_tstamp_prequeue)
2517 net_timestamp_check(skb);
2518
2519 trace_netif_rx(skb);
2520#ifdef CONFIG_RPS
2521 {
2522 struct rps_dev_flow voidflow, *rflow = &voidflow;
2523 int cpu;
2524
2525 preempt_disable();
2526 rcu_read_lock();
2527
2528 cpu = get_rps_cpu(skb->dev, skb, &rflow);
2529 if (cpu < 0)
2530 cpu = smp_processor_id();
2531
2532 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
2533
2534 rcu_read_unlock();
2535 preempt_enable();
2536 }
2537#else
2538 {
2539 unsigned int qtail;
2540 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
2541 put_cpu();
2542 }
2543#endif
2544 return ret;
2545}
2546EXPORT_SYMBOL(netif_rx);
2547
2548int netif_rx_ni(struct sk_buff *skb)
2549{
2550 int err;
2551
2552 preempt_disable();
2553 err = netif_rx(skb);
2554 if (local_softirq_pending())
2555 do_softirq();
2556 preempt_enable();
2557
2558 return err;
2559}
2560EXPORT_SYMBOL(netif_rx_ni);
2561
2562static void net_tx_action(struct softirq_action *h)
2563{
2564 struct softnet_data *sd = &__get_cpu_var(softnet_data);
2565
2566 if (sd->completion_queue) {
2567 struct sk_buff *clist;
2568
2569 local_irq_disable();
2570 clist = sd->completion_queue;
2571 sd->completion_queue = NULL;
2572 local_irq_enable();
2573
2574 while (clist) {
2575 struct sk_buff *skb = clist;
2576 clist = clist->next;
2577
2578 WARN_ON(atomic_read(&skb->users));
2579 __kfree_skb(skb);
2580 }
2581 }
2582
2583 if (sd->output_queue) {
2584 struct Qdisc *head;
2585
2586 local_irq_disable();
2587 head = sd->output_queue;
2588 sd->output_queue = NULL;
2589 sd->output_queue_tailp = &sd->output_queue;
2590 local_irq_enable();
2591
2592 while (head) {
2593 struct Qdisc *q = head;
2594 spinlock_t *root_lock;
2595
2596 head = head->next_sched;
2597
2598 root_lock = qdisc_lock(q);
2599 if (spin_trylock(root_lock)) {
2600 smp_mb__before_clear_bit();
2601 clear_bit(__QDISC_STATE_SCHED,
2602 &q->state);
2603 qdisc_run(q);
2604 spin_unlock(root_lock);
2605 } else {
2606 if (!test_bit(__QDISC_STATE_DEACTIVATED,
2607 &q->state)) {
2608 __netif_reschedule(q);
2609 } else {
2610 smp_mb__before_clear_bit();
2611 clear_bit(__QDISC_STATE_SCHED,
2612 &q->state);
2613 }
2614 }
2615 }
2616 }
2617}
2618
2619static inline int deliver_skb(struct sk_buff *skb,
2620 struct packet_type *pt_prev,
2621 struct net_device *orig_dev)
2622{
2623 atomic_inc(&skb->users);
2624 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2625}
2626
2627#if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
2628 (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
2629/* This hook is defined here for ATM LANE */
2630int (*br_fdb_test_addr_hook)(struct net_device *dev,
2631 unsigned char *addr) __read_mostly;
2632EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
2633#endif
2634
2635#ifdef CONFIG_NET_CLS_ACT
2636/* TODO: Maybe we should just force sch_ingress to be compiled in
2637 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
2638 * a compare and 2 stores extra right now if we dont have it on
2639 * but have CONFIG_NET_CLS_ACT
2640 * NOTE: This doesnt stop any functionality; if you dont have
2641 * the ingress scheduler, you just cant add policies on ingress.
2642 *
2643 */
2644static int ing_filter(struct sk_buff *skb)
2645{
2646 struct net_device *dev = skb->dev;
2647 u32 ttl = G_TC_RTTL(skb->tc_verd);
2648 struct netdev_queue *rxq;
2649 int result = TC_ACT_OK;
2650 struct Qdisc *q;
2651
2652 if (unlikely(MAX_RED_LOOP < ttl++)) {
2653 if (net_ratelimit())
2654 pr_warning( "Redir loop detected Dropping packet (%d->%d)\n",
2655 skb->skb_iif, dev->ifindex);
2656 return TC_ACT_SHOT;
2657 }
2658
2659 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
2660 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
2661
2662 rxq = &dev->rx_queue;
2663
2664 q = rxq->qdisc;
2665 if (q != &noop_qdisc) {
2666 spin_lock(qdisc_lock(q));
2667 if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
2668 result = qdisc_enqueue_root(skb, q);
2669 spin_unlock(qdisc_lock(q));
2670 }
2671
2672 return result;
2673}
2674
2675static inline struct sk_buff *handle_ing(struct sk_buff *skb,
2676 struct packet_type **pt_prev,
2677 int *ret, struct net_device *orig_dev)
2678{
2679 if (skb->dev->rx_queue.qdisc == &noop_qdisc)
2680 goto out;
2681
2682 if (*pt_prev) {
2683 *ret = deliver_skb(skb, *pt_prev, orig_dev);
2684 *pt_prev = NULL;
2685 }
2686
2687 switch (ing_filter(skb)) {
2688 case TC_ACT_SHOT:
2689 case TC_ACT_STOLEN:
2690 kfree_skb(skb);
2691 return NULL;
2692 }
2693
2694out:
2695 skb->tc_verd = 0;
2696 return skb;
2697}
2698#endif
2699
2700/*
2701 * netif_nit_deliver - deliver received packets to network taps
2702 * @skb: buffer
2703 *
2704 * This function is used to deliver incoming packets to network
2705 * taps. It should be used when the normal netif_receive_skb path
2706 * is bypassed, for example because of VLAN acceleration.
2707 */
2708void netif_nit_deliver(struct sk_buff *skb)
2709{
2710 struct packet_type *ptype;
2711
2712 if (list_empty(&ptype_all))
2713 return;
2714
2715 skb_reset_network_header(skb);
2716 skb_reset_transport_header(skb);
2717 skb->mac_len = skb->network_header - skb->mac_header;
2718
2719 rcu_read_lock();
2720 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2721 if (!ptype->dev || ptype->dev == skb->dev)
2722 deliver_skb(skb, ptype, skb->dev);
2723 }
2724 rcu_read_unlock();
2725}
2726
2727/**
2728 * netdev_rx_handler_register - register receive handler
2729 * @dev: device to register a handler for
2730 * @rx_handler: receive handler to register
2731 * @rx_handler_data: data pointer that is used by rx handler
2732 *
2733 * Register a receive hander for a device. This handler will then be
2734 * called from __netif_receive_skb. A negative errno code is returned
2735 * on a failure.
2736 *
2737 * The caller must hold the rtnl_mutex.
2738 */
2739int netdev_rx_handler_register(struct net_device *dev,
2740 rx_handler_func_t *rx_handler,
2741 void *rx_handler_data)
2742{
2743 ASSERT_RTNL();
2744
2745 if (dev->rx_handler)
2746 return -EBUSY;
2747
2748 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
2749 rcu_assign_pointer(dev->rx_handler, rx_handler);
2750
2751 return 0;
2752}
2753EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
2754
2755/**
2756 * netdev_rx_handler_unregister - unregister receive handler
2757 * @dev: device to unregister a handler from
2758 *
2759 * Unregister a receive hander from a device.
2760 *
2761 * The caller must hold the rtnl_mutex.
2762 */
2763void netdev_rx_handler_unregister(struct net_device *dev)
2764{
2765
2766 ASSERT_RTNL();
2767 rcu_assign_pointer(dev->rx_handler, NULL);
2768 rcu_assign_pointer(dev->rx_handler_data, NULL);
2769}
2770EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
2771
2772static inline void skb_bond_set_mac_by_master(struct sk_buff *skb,
2773 struct net_device *master)
2774{
2775 if (skb->pkt_type == PACKET_HOST) {
2776 u16 *dest = (u16 *) eth_hdr(skb)->h_dest;
2777
2778 memcpy(dest, master->dev_addr, ETH_ALEN);
2779 }
2780}
2781
2782/* On bonding slaves other than the currently active slave, suppress
2783 * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
2784 * ARP on active-backup slaves with arp_validate enabled.
2785 */
2786int __skb_bond_should_drop(struct sk_buff *skb, struct net_device *master)
2787{
2788 struct net_device *dev = skb->dev;
2789
2790 if (master->priv_flags & IFF_MASTER_ARPMON)
2791 dev->last_rx = jiffies;
2792
2793 if ((master->priv_flags & IFF_MASTER_ALB) &&
2794 (master->priv_flags & IFF_BRIDGE_PORT)) {
2795 /* Do address unmangle. The local destination address
2796 * will be always the one master has. Provides the right
2797 * functionality in a bridge.
2798 */
2799 skb_bond_set_mac_by_master(skb, master);
2800 }
2801
2802 if (dev->priv_flags & IFF_SLAVE_INACTIVE) {
2803 if ((dev->priv_flags & IFF_SLAVE_NEEDARP) &&
2804 skb->protocol == __cpu_to_be16(ETH_P_ARP))
2805 return 0;
2806
2807 if (master->priv_flags & IFF_MASTER_ALB) {
2808 if (skb->pkt_type != PACKET_BROADCAST &&
2809 skb->pkt_type != PACKET_MULTICAST)
2810 return 0;
2811 }
2812 if (master->priv_flags & IFF_MASTER_8023AD &&
2813 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
2814 return 0;
2815
2816 return 1;
2817 }
2818 return 0;
2819}
2820EXPORT_SYMBOL(__skb_bond_should_drop);
2821
2822static int __netif_receive_skb(struct sk_buff *skb)
2823{
2824 struct packet_type *ptype, *pt_prev;
2825 rx_handler_func_t *rx_handler;
2826 struct net_device *orig_dev;
2827 struct net_device *master;
2828 struct net_device *null_or_orig;
2829 struct net_device *orig_or_bond;
2830 int ret = NET_RX_DROP;
2831 __be16 type;
2832
2833 if (!netdev_tstamp_prequeue)
2834 net_timestamp_check(skb);
2835
2836 trace_netif_receive_skb(skb);
2837 if (vlan_tx_tag_present(skb) && vlan_hwaccel_do_receive(skb))
2838 return NET_RX_SUCCESS;
2839
2840 /* if we've gotten here through NAPI, check netpoll */
2841 if (netpoll_receive_skb(skb))
2842 return NET_RX_DROP;
2843
2844 if (!skb->skb_iif)
2845 skb->skb_iif = skb->dev->ifindex;
2846
2847 /*
2848 * bonding note: skbs received on inactive slaves should only
2849 * be delivered to pkt handlers that are exact matches. Also
2850 * the deliver_no_wcard flag will be set. If packet handlers
2851 * are sensitive to duplicate packets these skbs will need to
2852 * be dropped at the handler. The vlan accel path may have
2853 * already set the deliver_no_wcard flag.
2854 */
2855 null_or_orig = NULL;
2856 orig_dev = skb->dev;
2857 master = ACCESS_ONCE(orig_dev->master);
2858 if (skb->deliver_no_wcard)
2859 null_or_orig = orig_dev;
2860 else if (master) {
2861 if (skb_bond_should_drop(skb, master)) {
2862 skb->deliver_no_wcard = 1;
2863 null_or_orig = orig_dev; /* deliver only exact match */
2864 } else
2865 skb->dev = master;
2866 }
2867
2868 __this_cpu_inc(softnet_data.processed);
2869 skb_reset_network_header(skb);
2870 skb_reset_transport_header(skb);
2871 skb->mac_len = skb->network_header - skb->mac_header;
2872
2873 pt_prev = NULL;
2874
2875 rcu_read_lock();
2876
2877#ifdef CONFIG_NET_CLS_ACT
2878 if (skb->tc_verd & TC_NCLS) {
2879 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
2880 goto ncls;
2881 }
2882#endif
2883
2884 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2885 if (ptype->dev == null_or_orig || ptype->dev == skb->dev ||
2886 ptype->dev == orig_dev) {
2887 if (pt_prev)
2888 ret = deliver_skb(skb, pt_prev, orig_dev);
2889 pt_prev = ptype;
2890 }
2891 }
2892
2893#ifdef CONFIG_NET_CLS_ACT
2894 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
2895 if (!skb)
2896 goto out;
2897ncls:
2898#endif
2899
2900 /* Handle special case of bridge or macvlan */
2901 rx_handler = rcu_dereference(skb->dev->rx_handler);
2902 if (rx_handler) {
2903 if (pt_prev) {
2904 ret = deliver_skb(skb, pt_prev, orig_dev);
2905 pt_prev = NULL;
2906 }
2907 skb = rx_handler(skb);
2908 if (!skb)
2909 goto out;
2910 }
2911
2912 /*
2913 * Make sure frames received on VLAN interfaces stacked on
2914 * bonding interfaces still make their way to any base bonding
2915 * device that may have registered for a specific ptype. The
2916 * handler may have to adjust skb->dev and orig_dev.
2917 */
2918 orig_or_bond = orig_dev;
2919 if ((skb->dev->priv_flags & IFF_802_1Q_VLAN) &&
2920 (vlan_dev_real_dev(skb->dev)->priv_flags & IFF_BONDING)) {
2921 orig_or_bond = vlan_dev_real_dev(skb->dev);
2922 }
2923
2924 type = skb->protocol;
2925 list_for_each_entry_rcu(ptype,
2926 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
2927 if (ptype->type == type && (ptype->dev == null_or_orig ||
2928 ptype->dev == skb->dev || ptype->dev == orig_dev ||
2929 ptype->dev == orig_or_bond)) {
2930 if (pt_prev)
2931 ret = deliver_skb(skb, pt_prev, orig_dev);
2932 pt_prev = ptype;
2933 }
2934 }
2935
2936 if (pt_prev) {
2937 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2938 } else {
2939 kfree_skb(skb);
2940 /* Jamal, now you will not able to escape explaining
2941 * me how you were going to use this. :-)
2942 */
2943 ret = NET_RX_DROP;
2944 }
2945
2946out:
2947 rcu_read_unlock();
2948 return ret;
2949}
2950
2951/**
2952 * netif_receive_skb - process receive buffer from network
2953 * @skb: buffer to process
2954 *
2955 * netif_receive_skb() is the main receive data processing function.
2956 * It always succeeds. The buffer may be dropped during processing
2957 * for congestion control or by the protocol layers.
2958 *
2959 * This function may only be called from softirq context and interrupts
2960 * should be enabled.
2961 *
2962 * Return values (usually ignored):
2963 * NET_RX_SUCCESS: no congestion
2964 * NET_RX_DROP: packet was dropped
2965 */
2966int netif_receive_skb(struct sk_buff *skb)
2967{
2968 if (netdev_tstamp_prequeue)
2969 net_timestamp_check(skb);
2970
2971 if (skb_defer_rx_timestamp(skb))
2972 return NET_RX_SUCCESS;
2973
2974#ifdef CONFIG_RPS
2975 {
2976 struct rps_dev_flow voidflow, *rflow = &voidflow;
2977 int cpu, ret;
2978
2979 rcu_read_lock();
2980
2981 cpu = get_rps_cpu(skb->dev, skb, &rflow);
2982
2983 if (cpu >= 0) {
2984 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
2985 rcu_read_unlock();
2986 } else {
2987 rcu_read_unlock();
2988 ret = __netif_receive_skb(skb);
2989 }
2990
2991 return ret;
2992 }
2993#else
2994 return __netif_receive_skb(skb);
2995#endif
2996}
2997EXPORT_SYMBOL(netif_receive_skb);
2998
2999/* Network device is going away, flush any packets still pending
3000 * Called with irqs disabled.
3001 */
3002static void flush_backlog(void *arg)
3003{
3004 struct net_device *dev = arg;
3005 struct softnet_data *sd = &__get_cpu_var(softnet_data);
3006 struct sk_buff *skb, *tmp;
3007
3008 rps_lock(sd);
3009 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
3010 if (skb->dev == dev) {
3011 __skb_unlink(skb, &sd->input_pkt_queue);
3012 kfree_skb(skb);
3013 input_queue_head_incr(sd);
3014 }
3015 }
3016 rps_unlock(sd);
3017
3018 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
3019 if (skb->dev == dev) {
3020 __skb_unlink(skb, &sd->process_queue);
3021 kfree_skb(skb);
3022 input_queue_head_incr(sd);
3023 }
3024 }
3025}
3026
3027static int napi_gro_complete(struct sk_buff *skb)
3028{
3029 struct packet_type *ptype;
3030 __be16 type = skb->protocol;
3031 struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
3032 int err = -ENOENT;
3033
3034 if (NAPI_GRO_CB(skb)->count == 1) {
3035 skb_shinfo(skb)->gso_size = 0;
3036 goto out;
3037 }
3038
3039 rcu_read_lock();
3040 list_for_each_entry_rcu(ptype, head, list) {
3041 if (ptype->type != type || ptype->dev || !ptype->gro_complete)
3042 continue;
3043
3044 err = ptype->gro_complete(skb);
3045 break;
3046 }
3047 rcu_read_unlock();
3048
3049 if (err) {
3050 WARN_ON(&ptype->list == head);
3051 kfree_skb(skb);
3052 return NET_RX_SUCCESS;
3053 }
3054
3055out:
3056 return netif_receive_skb(skb);
3057}
3058
3059static void napi_gro_flush(struct napi_struct *napi)
3060{
3061 struct sk_buff *skb, *next;
3062
3063 for (skb = napi->gro_list; skb; skb = next) {
3064 next = skb->next;
3065 skb->next = NULL;
3066 napi_gro_complete(skb);
3067 }
3068
3069 napi->gro_count = 0;
3070 napi->gro_list = NULL;
3071}
3072
3073enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
3074{
3075 struct sk_buff **pp = NULL;
3076 struct packet_type *ptype;
3077 __be16 type = skb->protocol;
3078 struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
3079 int same_flow;
3080 int mac_len;
3081 enum gro_result ret;
3082
3083 if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
3084 goto normal;
3085
3086 if (skb_is_gso(skb) || skb_has_frags(skb))
3087 goto normal;
3088
3089 rcu_read_lock();
3090 list_for_each_entry_rcu(ptype, head, list) {
3091 if (ptype->type != type || ptype->dev || !ptype->gro_receive)
3092 continue;
3093
3094 skb_set_network_header(skb, skb_gro_offset(skb));
3095 mac_len = skb->network_header - skb->mac_header;
3096 skb->mac_len = mac_len;
3097 NAPI_GRO_CB(skb)->same_flow = 0;
3098 NAPI_GRO_CB(skb)->flush = 0;
3099 NAPI_GRO_CB(skb)->free = 0;
3100
3101 pp = ptype->gro_receive(&napi->gro_list, skb);
3102 break;
3103 }
3104 rcu_read_unlock();
3105
3106 if (&ptype->list == head)
3107 goto normal;
3108
3109 same_flow = NAPI_GRO_CB(skb)->same_flow;
3110 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
3111
3112 if (pp) {
3113 struct sk_buff *nskb = *pp;
3114
3115 *pp = nskb->next;
3116 nskb->next = NULL;
3117 napi_gro_complete(nskb);
3118 napi->gro_count--;
3119 }
3120
3121 if (same_flow)
3122 goto ok;
3123
3124 if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
3125 goto normal;
3126
3127 napi->gro_count++;
3128 NAPI_GRO_CB(skb)->count = 1;
3129 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
3130 skb->next = napi->gro_list;
3131 napi->gro_list = skb;
3132 ret = GRO_HELD;
3133
3134pull:
3135 if (skb_headlen(skb) < skb_gro_offset(skb)) {
3136 int grow = skb_gro_offset(skb) - skb_headlen(skb);
3137
3138 BUG_ON(skb->end - skb->tail < grow);
3139
3140 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
3141
3142 skb->tail += grow;
3143 skb->data_len -= grow;
3144
3145 skb_shinfo(skb)->frags[0].page_offset += grow;
3146 skb_shinfo(skb)->frags[0].size -= grow;
3147
3148 if (unlikely(!skb_shinfo(skb)->frags[0].size)) {
3149 put_page(skb_shinfo(skb)->frags[0].page);
3150 memmove(skb_shinfo(skb)->frags,
3151 skb_shinfo(skb)->frags + 1,
3152 --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
3153 }
3154 }
3155
3156ok:
3157 return ret;
3158
3159normal:
3160 ret = GRO_NORMAL;
3161 goto pull;
3162}
3163EXPORT_SYMBOL(dev_gro_receive);
3164
3165static gro_result_t
3166__napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
3167{
3168 struct sk_buff *p;
3169
3170 for (p = napi->gro_list; p; p = p->next) {
3171 NAPI_GRO_CB(p)->same_flow =
3172 (p->dev == skb->dev) &&
3173 !compare_ether_header(skb_mac_header(p),
3174 skb_gro_mac_header(skb));
3175 NAPI_GRO_CB(p)->flush = 0;
3176 }
3177
3178 return dev_gro_receive(napi, skb);
3179}
3180
3181gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
3182{
3183 switch (ret) {
3184 case GRO_NORMAL:
3185 if (netif_receive_skb(skb))
3186 ret = GRO_DROP;
3187 break;
3188
3189 case GRO_DROP:
3190 case GRO_MERGED_FREE:
3191 kfree_skb(skb);
3192 break;
3193
3194 case GRO_HELD:
3195 case GRO_MERGED:
3196 break;
3197 }
3198
3199 return ret;
3200}
3201EXPORT_SYMBOL(napi_skb_finish);
3202
3203void skb_gro_reset_offset(struct sk_buff *skb)
3204{
3205 NAPI_GRO_CB(skb)->data_offset = 0;
3206 NAPI_GRO_CB(skb)->frag0 = NULL;
3207 NAPI_GRO_CB(skb)->frag0_len = 0;
3208
3209 if (skb->mac_header == skb->tail &&
3210 !PageHighMem(skb_shinfo(skb)->frags[0].page)) {
3211 NAPI_GRO_CB(skb)->frag0 =
3212 page_address(skb_shinfo(skb)->frags[0].page) +
3213 skb_shinfo(skb)->frags[0].page_offset;
3214 NAPI_GRO_CB(skb)->frag0_len = skb_shinfo(skb)->frags[0].size;
3215 }
3216}
3217EXPORT_SYMBOL(skb_gro_reset_offset);
3218
3219gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
3220{
3221 skb_gro_reset_offset(skb);
3222
3223 return napi_skb_finish(__napi_gro_receive(napi, skb), skb);
3224}
3225EXPORT_SYMBOL(napi_gro_receive);
3226
3227void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
3228{
3229 __skb_pull(skb, skb_headlen(skb));
3230 skb_reserve(skb, NET_IP_ALIGN - skb_headroom(skb));
3231
3232 napi->skb = skb;
3233}
3234EXPORT_SYMBOL(napi_reuse_skb);
3235
3236struct sk_buff *napi_get_frags(struct napi_struct *napi)
3237{
3238 struct sk_buff *skb = napi->skb;
3239
3240 if (!skb) {
3241 skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
3242 if (skb)
3243 napi->skb = skb;
3244 }
3245 return skb;
3246}
3247EXPORT_SYMBOL(napi_get_frags);
3248
3249gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
3250 gro_result_t ret)
3251{
3252 switch (ret) {
3253 case GRO_NORMAL:
3254 case GRO_HELD:
3255 skb->protocol = eth_type_trans(skb, skb->dev);
3256
3257 if (ret == GRO_HELD)
3258 skb_gro_pull(skb, -ETH_HLEN);
3259 else if (netif_receive_skb(skb))
3260 ret = GRO_DROP;
3261 break;
3262
3263 case GRO_DROP:
3264 case GRO_MERGED_FREE:
3265 napi_reuse_skb(napi, skb);
3266 break;
3267
3268 case GRO_MERGED:
3269 break;
3270 }
3271
3272 return ret;
3273}
3274EXPORT_SYMBOL(napi_frags_finish);
3275
3276struct sk_buff *napi_frags_skb(struct napi_struct *napi)
3277{
3278 struct sk_buff *skb = napi->skb;
3279 struct ethhdr *eth;
3280 unsigned int hlen;
3281 unsigned int off;
3282
3283 napi->skb = NULL;
3284
3285 skb_reset_mac_header(skb);
3286 skb_gro_reset_offset(skb);
3287
3288 off = skb_gro_offset(skb);
3289 hlen = off + sizeof(*eth);
3290 eth = skb_gro_header_fast(skb, off);
3291 if (skb_gro_header_hard(skb, hlen)) {
3292 eth = skb_gro_header_slow(skb, hlen, off);
3293 if (unlikely(!eth)) {
3294 napi_reuse_skb(napi, skb);
3295 skb = NULL;
3296 goto out;
3297 }
3298 }
3299
3300 skb_gro_pull(skb, sizeof(*eth));
3301
3302 /*
3303 * This works because the only protocols we care about don't require
3304 * special handling. We'll fix it up properly at the end.
3305 */
3306 skb->protocol = eth->h_proto;
3307
3308out:
3309 return skb;
3310}
3311EXPORT_SYMBOL(napi_frags_skb);
3312
3313gro_result_t napi_gro_frags(struct napi_struct *napi)
3314{
3315 struct sk_buff *skb = napi_frags_skb(napi);
3316
3317 if (!skb)
3318 return GRO_DROP;
3319
3320 return napi_frags_finish(napi, skb, __napi_gro_receive(napi, skb));
3321}
3322EXPORT_SYMBOL(napi_gro_frags);
3323
3324/*
3325 * net_rps_action sends any pending IPI's for rps.
3326 * Note: called with local irq disabled, but exits with local irq enabled.
3327 */
3328static void net_rps_action_and_irq_enable(struct softnet_data *sd)
3329{
3330#ifdef CONFIG_RPS
3331 struct softnet_data *remsd = sd->rps_ipi_list;
3332
3333 if (remsd) {
3334 sd->rps_ipi_list = NULL;
3335
3336 local_irq_enable();
3337
3338 /* Send pending IPI's to kick RPS processing on remote cpus. */
3339 while (remsd) {
3340 struct softnet_data *next = remsd->rps_ipi_next;
3341
3342 if (cpu_online(remsd->cpu))
3343 __smp_call_function_single(remsd->cpu,
3344 &remsd->csd, 0);
3345 remsd = next;
3346 }
3347 } else
3348#endif
3349 local_irq_enable();
3350}
3351
3352static int process_backlog(struct napi_struct *napi, int quota)
3353{
3354 int work = 0;
3355 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
3356
3357#ifdef CONFIG_RPS
3358 /* Check if we have pending ipi, its better to send them now,
3359 * not waiting net_rx_action() end.
3360 */
3361 if (sd->rps_ipi_list) {
3362 local_irq_disable();
3363 net_rps_action_and_irq_enable(sd);
3364 }
3365#endif
3366 napi->weight = weight_p;
3367 local_irq_disable();
3368 while (work < quota) {
3369 struct sk_buff *skb;
3370 unsigned int qlen;
3371
3372 while ((skb = __skb_dequeue(&sd->process_queue))) {
3373 local_irq_enable();
3374 __netif_receive_skb(skb);
3375 local_irq_disable();
3376 input_queue_head_incr(sd);
3377 if (++work >= quota) {
3378 local_irq_enable();
3379 return work;
3380 }
3381 }
3382
3383 rps_lock(sd);
3384 qlen = skb_queue_len(&sd->input_pkt_queue);
3385 if (qlen)
3386 skb_queue_splice_tail_init(&sd->input_pkt_queue,
3387 &sd->process_queue);
3388
3389 if (qlen < quota - work) {
3390 /*
3391 * Inline a custom version of __napi_complete().
3392 * only current cpu owns and manipulates this napi,
3393 * and NAPI_STATE_SCHED is the only possible flag set on backlog.
3394 * we can use a plain write instead of clear_bit(),
3395 * and we dont need an smp_mb() memory barrier.
3396 */
3397 list_del(&napi->poll_list);
3398 napi->state = 0;
3399
3400 quota = work + qlen;
3401 }
3402 rps_unlock(sd);
3403 }
3404 local_irq_enable();
3405
3406 return work;
3407}
3408
3409/**
3410 * __napi_schedule - schedule for receive
3411 * @n: entry to schedule
3412 *
3413 * The entry's receive function will be scheduled to run
3414 */
3415void __napi_schedule(struct napi_struct *n)
3416{
3417 unsigned long flags;
3418
3419 local_irq_save(flags);
3420 ____napi_schedule(&__get_cpu_var(softnet_data), n);
3421 local_irq_restore(flags);
3422}
3423EXPORT_SYMBOL(__napi_schedule);
3424
3425void __napi_complete(struct napi_struct *n)
3426{
3427 BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
3428 BUG_ON(n->gro_list);
3429
3430 list_del(&n->poll_list);
3431 smp_mb__before_clear_bit();
3432 clear_bit(NAPI_STATE_SCHED, &n->state);
3433}
3434EXPORT_SYMBOL(__napi_complete);
3435
3436void napi_complete(struct napi_struct *n)
3437{
3438 unsigned long flags;
3439
3440 /*
3441 * don't let napi dequeue from the cpu poll list
3442 * just in case its running on a different cpu
3443 */
3444 if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
3445 return;
3446
3447 napi_gro_flush(n);
3448 local_irq_save(flags);
3449 __napi_complete(n);
3450 local_irq_restore(flags);
3451}
3452EXPORT_SYMBOL(napi_complete);
3453
3454void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
3455 int (*poll)(struct napi_struct *, int), int weight)
3456{
3457 INIT_LIST_HEAD(&napi->poll_list);
3458 napi->gro_count = 0;
3459 napi->gro_list = NULL;
3460 napi->skb = NULL;
3461 napi->poll = poll;
3462 napi->weight = weight;
3463 list_add(&napi->dev_list, &dev->napi_list);
3464 napi->dev = dev;
3465#ifdef CONFIG_NETPOLL
3466 spin_lock_init(&napi->poll_lock);
3467 napi->poll_owner = -1;
3468#endif
3469 set_bit(NAPI_STATE_SCHED, &napi->state);
3470}
3471EXPORT_SYMBOL(netif_napi_add);
3472
3473void netif_napi_del(struct napi_struct *napi)
3474{
3475 struct sk_buff *skb, *next;
3476
3477 list_del_init(&napi->dev_list);
3478 napi_free_frags(napi);
3479
3480 for (skb = napi->gro_list; skb; skb = next) {
3481 next = skb->next;
3482 skb->next = NULL;
3483 kfree_skb(skb);
3484 }
3485
3486 napi->gro_list = NULL;
3487 napi->gro_count = 0;
3488}
3489EXPORT_SYMBOL(netif_napi_del);
3490
3491static void net_rx_action(struct softirq_action *h)
3492{
3493 struct softnet_data *sd = &__get_cpu_var(softnet_data);
3494 unsigned long time_limit = jiffies + 2;
3495 int budget = netdev_budget;
3496 void *have;
3497
3498 local_irq_disable();
3499
3500 while (!list_empty(&sd->poll_list)) {
3501 struct napi_struct *n;
3502 int work, weight;
3503
3504 /* If softirq window is exhuasted then punt.
3505 * Allow this to run for 2 jiffies since which will allow
3506 * an average latency of 1.5/HZ.
3507 */
3508 if (unlikely(budget <= 0 || time_after(jiffies, time_limit)))
3509 goto softnet_break;
3510
3511 local_irq_enable();
3512
3513 /* Even though interrupts have been re-enabled, this
3514 * access is safe because interrupts can only add new
3515 * entries to the tail of this list, and only ->poll()
3516 * calls can remove this head entry from the list.
3517 */
3518 n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
3519
3520 have = netpoll_poll_lock(n);
3521
3522 weight = n->weight;
3523
3524 /* This NAPI_STATE_SCHED test is for avoiding a race
3525 * with netpoll's poll_napi(). Only the entity which
3526 * obtains the lock and sees NAPI_STATE_SCHED set will
3527 * actually make the ->poll() call. Therefore we avoid
3528 * accidently calling ->poll() when NAPI is not scheduled.
3529 */
3530 work = 0;
3531 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
3532 work = n->poll(n, weight);
3533 trace_napi_poll(n);
3534 }
3535
3536 WARN_ON_ONCE(work > weight);
3537
3538 budget -= work;
3539
3540 local_irq_disable();
3541
3542 /* Drivers must not modify the NAPI state if they
3543 * consume the entire weight. In such cases this code
3544 * still "owns" the NAPI instance and therefore can
3545 * move the instance around on the list at-will.
3546 */
3547 if (unlikely(work == weight)) {
3548 if (unlikely(napi_disable_pending(n))) {
3549 local_irq_enable();
3550 napi_complete(n);
3551 local_irq_disable();
3552 } else
3553 list_move_tail(&n->poll_list, &sd->poll_list);
3554 }
3555
3556 netpoll_poll_unlock(have);
3557 }
3558out:
3559 net_rps_action_and_irq_enable(sd);
3560
3561#ifdef CONFIG_NET_DMA
3562 /*
3563 * There may not be any more sk_buffs coming right now, so push
3564 * any pending DMA copies to hardware
3565 */
3566 dma_issue_pending_all();
3567#endif
3568
3569 return;
3570
3571softnet_break:
3572 sd->time_squeeze++;
3573 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3574 goto out;
3575}
3576
3577static gifconf_func_t *gifconf_list[NPROTO];
3578
3579/**
3580 * register_gifconf - register a SIOCGIF handler
3581 * @family: Address family
3582 * @gifconf: Function handler
3583 *
3584 * Register protocol dependent address dumping routines. The handler
3585 * that is passed must not be freed or reused until it has been replaced
3586 * by another handler.
3587 */
3588int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
3589{
3590 if (family >= NPROTO)
3591 return -EINVAL;
3592 gifconf_list[family] = gifconf;
3593 return 0;
3594}
3595EXPORT_SYMBOL(register_gifconf);
3596
3597
3598/*
3599 * Map an interface index to its name (SIOCGIFNAME)
3600 */
3601
3602/*
3603 * We need this ioctl for efficient implementation of the
3604 * if_indextoname() function required by the IPv6 API. Without
3605 * it, we would have to search all the interfaces to find a
3606 * match. --pb
3607 */
3608
3609static int dev_ifname(struct net *net, struct ifreq __user *arg)
3610{
3611 struct net_device *dev;
3612 struct ifreq ifr;
3613
3614 /*
3615 * Fetch the caller's info block.
3616 */
3617
3618 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3619 return -EFAULT;
3620
3621 rcu_read_lock();
3622 dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex);
3623 if (!dev) {
3624 rcu_read_unlock();
3625 return -ENODEV;
3626 }
3627
3628 strcpy(ifr.ifr_name, dev->name);
3629 rcu_read_unlock();
3630
3631 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
3632 return -EFAULT;
3633 return 0;
3634}
3635
3636/*
3637 * Perform a SIOCGIFCONF call. This structure will change
3638 * size eventually, and there is nothing I can do about it.
3639 * Thus we will need a 'compatibility mode'.
3640 */
3641
3642static int dev_ifconf(struct net *net, char __user *arg)
3643{
3644 struct ifconf ifc;
3645 struct net_device *dev;
3646 char __user *pos;
3647 int len;
3648 int total;
3649 int i;
3650
3651 /*
3652 * Fetch the caller's info block.
3653 */
3654
3655 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
3656 return -EFAULT;
3657
3658 pos = ifc.ifc_buf;
3659 len = ifc.ifc_len;
3660
3661 /*
3662 * Loop over the interfaces, and write an info block for each.
3663 */
3664
3665 total = 0;
3666 for_each_netdev(net, dev) {
3667 for (i = 0; i < NPROTO; i++) {
3668 if (gifconf_list[i]) {
3669 int done;
3670 if (!pos)
3671 done = gifconf_list[i](dev, NULL, 0);
3672 else
3673 done = gifconf_list[i](dev, pos + total,
3674 len - total);
3675 if (done < 0)
3676 return -EFAULT;
3677 total += done;
3678 }
3679 }
3680 }
3681
3682 /*
3683 * All done. Write the updated control block back to the caller.
3684 */
3685 ifc.ifc_len = total;
3686
3687 /*
3688 * Both BSD and Solaris return 0 here, so we do too.
3689 */
3690 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
3691}
3692
3693#ifdef CONFIG_PROC_FS
3694/*
3695 * This is invoked by the /proc filesystem handler to display a device
3696 * in detail.
3697 */
3698void *dev_seq_start(struct seq_file *seq, loff_t *pos)
3699 __acquires(RCU)
3700{
3701 struct net *net = seq_file_net(seq);
3702 loff_t off;
3703 struct net_device *dev;
3704
3705 rcu_read_lock();
3706 if (!*pos)
3707 return SEQ_START_TOKEN;
3708
3709 off = 1;
3710 for_each_netdev_rcu(net, dev)
3711 if (off++ == *pos)
3712 return dev;
3713
3714 return NULL;
3715}
3716
3717void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3718{
3719 struct net_device *dev = (v == SEQ_START_TOKEN) ?
3720 first_net_device(seq_file_net(seq)) :
3721 next_net_device((struct net_device *)v);
3722
3723 ++*pos;
3724 return rcu_dereference(dev);
3725}
3726
3727void dev_seq_stop(struct seq_file *seq, void *v)
3728 __releases(RCU)
3729{
3730 rcu_read_unlock();
3731}
3732
3733static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
3734{
3735 struct rtnl_link_stats64 temp;
3736 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
3737
3738 seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
3739 "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
3740 dev->name, stats->rx_bytes, stats->rx_packets,
3741 stats->rx_errors,
3742 stats->rx_dropped + stats->rx_missed_errors,
3743 stats->rx_fifo_errors,
3744 stats->rx_length_errors + stats->rx_over_errors +
3745 stats->rx_crc_errors + stats->rx_frame_errors,
3746 stats->rx_compressed, stats->multicast,
3747 stats->tx_bytes, stats->tx_packets,
3748 stats->tx_errors, stats->tx_dropped,
3749 stats->tx_fifo_errors, stats->collisions,
3750 stats->tx_carrier_errors +
3751 stats->tx_aborted_errors +
3752 stats->tx_window_errors +
3753 stats->tx_heartbeat_errors,
3754 stats->tx_compressed);
3755}
3756
3757/*
3758 * Called from the PROCfs module. This now uses the new arbitrary sized
3759 * /proc/net interface to create /proc/net/dev
3760 */
3761static int dev_seq_show(struct seq_file *seq, void *v)
3762{
3763 if (v == SEQ_START_TOKEN)
3764 seq_puts(seq, "Inter-| Receive "
3765 " | Transmit\n"
3766 " face |bytes packets errs drop fifo frame "
3767 "compressed multicast|bytes packets errs "
3768 "drop fifo colls carrier compressed\n");
3769 else
3770 dev_seq_printf_stats(seq, v);
3771 return 0;
3772}
3773
3774static struct softnet_data *softnet_get_online(loff_t *pos)
3775{
3776 struct softnet_data *sd = NULL;
3777
3778 while (*pos < nr_cpu_ids)
3779 if (cpu_online(*pos)) {
3780 sd = &per_cpu(softnet_data, *pos);
3781 break;
3782 } else
3783 ++*pos;
3784 return sd;
3785}
3786
3787static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
3788{
3789 return softnet_get_online(pos);
3790}
3791
3792static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3793{
3794 ++*pos;
3795 return softnet_get_online(pos);
3796}
3797
3798static void softnet_seq_stop(struct seq_file *seq, void *v)
3799{
3800}
3801
3802static int softnet_seq_show(struct seq_file *seq, void *v)
3803{
3804 struct softnet_data *sd = v;
3805
3806 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
3807 sd->processed, sd->dropped, sd->time_squeeze, 0,
3808 0, 0, 0, 0, /* was fastroute */
3809 sd->cpu_collision, sd->received_rps);
3810 return 0;
3811}
3812
3813static const struct seq_operations dev_seq_ops = {
3814 .start = dev_seq_start,
3815 .next = dev_seq_next,
3816 .stop = dev_seq_stop,
3817 .show = dev_seq_show,
3818};
3819
3820static int dev_seq_open(struct inode *inode, struct file *file)
3821{
3822 return seq_open_net(inode, file, &dev_seq_ops,
3823 sizeof(struct seq_net_private));
3824}
3825
3826static const struct file_operations dev_seq_fops = {
3827 .owner = THIS_MODULE,
3828 .open = dev_seq_open,
3829 .read = seq_read,
3830 .llseek = seq_lseek,
3831 .release = seq_release_net,
3832};
3833
3834static const struct seq_operations softnet_seq_ops = {
3835 .start = softnet_seq_start,
3836 .next = softnet_seq_next,
3837 .stop = softnet_seq_stop,
3838 .show = softnet_seq_show,
3839};
3840
3841static int softnet_seq_open(struct inode *inode, struct file *file)
3842{
3843 return seq_open(file, &softnet_seq_ops);
3844}
3845
3846static const struct file_operations softnet_seq_fops = {
3847 .owner = THIS_MODULE,
3848 .open = softnet_seq_open,
3849 .read = seq_read,
3850 .llseek = seq_lseek,
3851 .release = seq_release,
3852};
3853
3854static void *ptype_get_idx(loff_t pos)
3855{
3856 struct packet_type *pt = NULL;
3857 loff_t i = 0;
3858 int t;
3859
3860 list_for_each_entry_rcu(pt, &ptype_all, list) {
3861 if (i == pos)
3862 return pt;
3863 ++i;
3864 }
3865
3866 for (t = 0; t < PTYPE_HASH_SIZE; t++) {
3867 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
3868 if (i == pos)
3869 return pt;
3870 ++i;
3871 }
3872 }
3873 return NULL;
3874}
3875
3876static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
3877 __acquires(RCU)
3878{
3879 rcu_read_lock();
3880 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
3881}
3882
3883static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3884{
3885 struct packet_type *pt;
3886 struct list_head *nxt;
3887 int hash;
3888
3889 ++*pos;
3890 if (v == SEQ_START_TOKEN)
3891 return ptype_get_idx(0);
3892
3893 pt = v;
3894 nxt = pt->list.next;
3895 if (pt->type == htons(ETH_P_ALL)) {
3896 if (nxt != &ptype_all)
3897 goto found;
3898 hash = 0;
3899 nxt = ptype_base[0].next;
3900 } else
3901 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
3902
3903 while (nxt == &ptype_base[hash]) {
3904 if (++hash >= PTYPE_HASH_SIZE)
3905 return NULL;
3906 nxt = ptype_base[hash].next;
3907 }
3908found:
3909 return list_entry(nxt, struct packet_type, list);
3910}
3911
3912static void ptype_seq_stop(struct seq_file *seq, void *v)
3913 __releases(RCU)
3914{
3915 rcu_read_unlock();
3916}
3917
3918static int ptype_seq_show(struct seq_file *seq, void *v)
3919{
3920 struct packet_type *pt = v;
3921
3922 if (v == SEQ_START_TOKEN)
3923 seq_puts(seq, "Type Device Function\n");
3924 else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
3925 if (pt->type == htons(ETH_P_ALL))
3926 seq_puts(seq, "ALL ");
3927 else
3928 seq_printf(seq, "%04x", ntohs(pt->type));
3929
3930 seq_printf(seq, " %-8s %pF\n",
3931 pt->dev ? pt->dev->name : "", pt->func);
3932 }
3933
3934 return 0;
3935}
3936
3937static const struct seq_operations ptype_seq_ops = {
3938 .start = ptype_seq_start,
3939 .next = ptype_seq_next,
3940 .stop = ptype_seq_stop,
3941 .show = ptype_seq_show,
3942};
3943
3944static int ptype_seq_open(struct inode *inode, struct file *file)
3945{
3946 return seq_open_net(inode, file, &ptype_seq_ops,
3947 sizeof(struct seq_net_private));
3948}
3949
3950static const struct file_operations ptype_seq_fops = {
3951 .owner = THIS_MODULE,
3952 .open = ptype_seq_open,
3953 .read = seq_read,
3954 .llseek = seq_lseek,
3955 .release = seq_release_net,
3956};
3957
3958
3959static int __net_init dev_proc_net_init(struct net *net)
3960{
3961 int rc = -ENOMEM;
3962
3963 if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
3964 goto out;
3965 if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
3966 goto out_dev;
3967 if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
3968 goto out_softnet;
3969
3970 if (wext_proc_init(net))
3971 goto out_ptype;
3972 rc = 0;
3973out:
3974 return rc;
3975out_ptype:
3976 proc_net_remove(net, "ptype");
3977out_softnet:
3978 proc_net_remove(net, "softnet_stat");
3979out_dev:
3980 proc_net_remove(net, "dev");
3981 goto out;
3982}
3983
3984static void __net_exit dev_proc_net_exit(struct net *net)
3985{
3986 wext_proc_exit(net);
3987
3988 proc_net_remove(net, "ptype");
3989 proc_net_remove(net, "softnet_stat");
3990 proc_net_remove(net, "dev");
3991}
3992
3993static struct pernet_operations __net_initdata dev_proc_ops = {
3994 .init = dev_proc_net_init,
3995 .exit = dev_proc_net_exit,
3996};
3997
3998static int __init dev_proc_init(void)
3999{
4000 return register_pernet_subsys(&dev_proc_ops);
4001}
4002#else
4003#define dev_proc_init() 0
4004#endif /* CONFIG_PROC_FS */
4005
4006
4007/**
4008 * netdev_set_master - set up master/slave pair
4009 * @slave: slave device
4010 * @master: new master device
4011 *
4012 * Changes the master device of the slave. Pass %NULL to break the
4013 * bonding. The caller must hold the RTNL semaphore. On a failure
4014 * a negative errno code is returned. On success the reference counts
4015 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
4016 * function returns zero.
4017 */
4018int netdev_set_master(struct net_device *slave, struct net_device *master)
4019{
4020 struct net_device *old = slave->master;
4021
4022 ASSERT_RTNL();
4023
4024 if (master) {
4025 if (old)
4026 return -EBUSY;
4027 dev_hold(master);
4028 }
4029
4030 slave->master = master;
4031
4032 if (old) {
4033 synchronize_net();
4034 dev_put(old);
4035 }
4036 if (master)
4037 slave->flags |= IFF_SLAVE;
4038 else
4039 slave->flags &= ~IFF_SLAVE;
4040
4041 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
4042 return 0;
4043}
4044EXPORT_SYMBOL(netdev_set_master);
4045
4046static void dev_change_rx_flags(struct net_device *dev, int flags)
4047{
4048 const struct net_device_ops *ops = dev->netdev_ops;
4049
4050 if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
4051 ops->ndo_change_rx_flags(dev, flags);
4052}
4053
4054static int __dev_set_promiscuity(struct net_device *dev, int inc)
4055{
4056 unsigned short old_flags = dev->flags;
4057 uid_t uid;
4058 gid_t gid;
4059
4060 ASSERT_RTNL();
4061
4062 dev->flags |= IFF_PROMISC;
4063 dev->promiscuity += inc;
4064 if (dev->promiscuity == 0) {
4065 /*
4066 * Avoid overflow.
4067 * If inc causes overflow, untouch promisc and return error.
4068 */
4069 if (inc < 0)
4070 dev->flags &= ~IFF_PROMISC;
4071 else {
4072 dev->promiscuity -= inc;
4073 printk(KERN_WARNING "%s: promiscuity touches roof, "
4074 "set promiscuity failed, promiscuity feature "
4075 "of device might be broken.\n", dev->name);
4076 return -EOVERFLOW;
4077 }
4078 }
4079 if (dev->flags != old_flags) {
4080 printk(KERN_INFO "device %s %s promiscuous mode\n",
4081 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
4082 "left");
4083 if (audit_enabled) {
4084 current_uid_gid(&uid, &gid);
4085 audit_log(current->audit_context, GFP_ATOMIC,
4086 AUDIT_ANOM_PROMISCUOUS,
4087 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
4088 dev->name, (dev->flags & IFF_PROMISC),
4089 (old_flags & IFF_PROMISC),
4090 audit_get_loginuid(current),
4091 uid, gid,
4092 audit_get_sessionid(current));
4093 }
4094
4095 dev_change_rx_flags(dev, IFF_PROMISC);
4096 }
4097 return 0;
4098}
4099
4100/**
4101 * dev_set_promiscuity - update promiscuity count on a device
4102 * @dev: device
4103 * @inc: modifier
4104 *
4105 * Add or remove promiscuity from a device. While the count in the device
4106 * remains above zero the interface remains promiscuous. Once it hits zero
4107 * the device reverts back to normal filtering operation. A negative inc
4108 * value is used to drop promiscuity on the device.
4109 * Return 0 if successful or a negative errno code on error.
4110 */
4111int dev_set_promiscuity(struct net_device *dev, int inc)
4112{
4113 unsigned short old_flags = dev->flags;
4114 int err;
4115
4116 err = __dev_set_promiscuity(dev, inc);
4117 if (err < 0)
4118 return err;
4119 if (dev->flags != old_flags)
4120 dev_set_rx_mode(dev);
4121 return err;
4122}
4123EXPORT_SYMBOL(dev_set_promiscuity);
4124
4125/**
4126 * dev_set_allmulti - update allmulti count on a device
4127 * @dev: device
4128 * @inc: modifier
4129 *
4130 * Add or remove reception of all multicast frames to a device. While the
4131 * count in the device remains above zero the interface remains listening
4132 * to all interfaces. Once it hits zero the device reverts back to normal
4133 * filtering operation. A negative @inc value is used to drop the counter
4134 * when releasing a resource needing all multicasts.
4135 * Return 0 if successful or a negative errno code on error.
4136 */
4137
4138int dev_set_allmulti(struct net_device *dev, int inc)
4139{
4140 unsigned short old_flags = dev->flags;
4141
4142 ASSERT_RTNL();
4143
4144 dev->flags |= IFF_ALLMULTI;
4145 dev->allmulti += inc;
4146 if (dev->allmulti == 0) {
4147 /*
4148 * Avoid overflow.
4149 * If inc causes overflow, untouch allmulti and return error.
4150 */
4151 if (inc < 0)
4152 dev->flags &= ~IFF_ALLMULTI;
4153 else {
4154 dev->allmulti -= inc;
4155 printk(KERN_WARNING "%s: allmulti touches roof, "
4156 "set allmulti failed, allmulti feature of "
4157 "device might be broken.\n", dev->name);
4158 return -EOVERFLOW;
4159 }
4160 }
4161 if (dev->flags ^ old_flags) {
4162 dev_change_rx_flags(dev, IFF_ALLMULTI);
4163 dev_set_rx_mode(dev);
4164 }
4165 return 0;
4166}
4167EXPORT_SYMBOL(dev_set_allmulti);
4168
4169/*
4170 * Upload unicast and multicast address lists to device and
4171 * configure RX filtering. When the device doesn't support unicast
4172 * filtering it is put in promiscuous mode while unicast addresses
4173 * are present.
4174 */
4175void __dev_set_rx_mode(struct net_device *dev)
4176{
4177 const struct net_device_ops *ops = dev->netdev_ops;
4178
4179 /* dev_open will call this function so the list will stay sane. */
4180 if (!(dev->flags&IFF_UP))
4181 return;
4182
4183 if (!netif_device_present(dev))
4184 return;
4185
4186 if (ops->ndo_set_rx_mode)
4187 ops->ndo_set_rx_mode(dev);
4188 else {
4189 /* Unicast addresses changes may only happen under the rtnl,
4190 * therefore calling __dev_set_promiscuity here is safe.
4191 */
4192 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
4193 __dev_set_promiscuity(dev, 1);
4194 dev->uc_promisc = 1;
4195 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
4196 __dev_set_promiscuity(dev, -1);
4197 dev->uc_promisc = 0;
4198 }
4199
4200 if (ops->ndo_set_multicast_list)
4201 ops->ndo_set_multicast_list(dev);
4202 }
4203}
4204
4205void dev_set_rx_mode(struct net_device *dev)
4206{
4207 netif_addr_lock_bh(dev);
4208 __dev_set_rx_mode(dev);
4209 netif_addr_unlock_bh(dev);
4210}
4211
4212/**
4213 * dev_get_flags - get flags reported to userspace
4214 * @dev: device
4215 *
4216 * Get the combination of flag bits exported through APIs to userspace.
4217 */
4218unsigned dev_get_flags(const struct net_device *dev)
4219{
4220 unsigned flags;
4221
4222 flags = (dev->flags & ~(IFF_PROMISC |
4223 IFF_ALLMULTI |
4224 IFF_RUNNING |
4225 IFF_LOWER_UP |
4226 IFF_DORMANT)) |
4227 (dev->gflags & (IFF_PROMISC |
4228 IFF_ALLMULTI));
4229
4230 if (netif_running(dev)) {
4231 if (netif_oper_up(dev))
4232 flags |= IFF_RUNNING;
4233 if (netif_carrier_ok(dev))
4234 flags |= IFF_LOWER_UP;
4235 if (netif_dormant(dev))
4236 flags |= IFF_DORMANT;
4237 }
4238
4239 return flags;
4240}
4241EXPORT_SYMBOL(dev_get_flags);
4242
4243int __dev_change_flags(struct net_device *dev, unsigned int flags)
4244{
4245 int old_flags = dev->flags;
4246 int ret;
4247
4248 ASSERT_RTNL();
4249
4250 /*
4251 * Set the flags on our device.
4252 */
4253
4254 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
4255 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
4256 IFF_AUTOMEDIA)) |
4257 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
4258 IFF_ALLMULTI));
4259
4260 /*
4261 * Load in the correct multicast list now the flags have changed.
4262 */
4263
4264 if ((old_flags ^ flags) & IFF_MULTICAST)
4265 dev_change_rx_flags(dev, IFF_MULTICAST);
4266
4267 dev_set_rx_mode(dev);
4268
4269 /*
4270 * Have we downed the interface. We handle IFF_UP ourselves
4271 * according to user attempts to set it, rather than blindly
4272 * setting it.
4273 */
4274
4275 ret = 0;
4276 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
4277 ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
4278
4279 if (!ret)
4280 dev_set_rx_mode(dev);
4281 }
4282
4283 if ((flags ^ dev->gflags) & IFF_PROMISC) {
4284 int inc = (flags & IFF_PROMISC) ? 1 : -1;
4285
4286 dev->gflags ^= IFF_PROMISC;
4287 dev_set_promiscuity(dev, inc);
4288 }
4289
4290 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
4291 is important. Some (broken) drivers set IFF_PROMISC, when
4292 IFF_ALLMULTI is requested not asking us and not reporting.
4293 */
4294 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
4295 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
4296
4297 dev->gflags ^= IFF_ALLMULTI;
4298 dev_set_allmulti(dev, inc);
4299 }
4300
4301 return ret;
4302}
4303
4304void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
4305{
4306 unsigned int changes = dev->flags ^ old_flags;
4307
4308 if (changes & IFF_UP) {
4309 if (dev->flags & IFF_UP)
4310 call_netdevice_notifiers(NETDEV_UP, dev);
4311 else
4312 call_netdevice_notifiers(NETDEV_DOWN, dev);
4313 }
4314
4315 if (dev->flags & IFF_UP &&
4316 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE)))
4317 call_netdevice_notifiers(NETDEV_CHANGE, dev);
4318}
4319
4320/**
4321 * dev_change_flags - change device settings
4322 * @dev: device
4323 * @flags: device state flags
4324 *
4325 * Change settings on device based state flags. The flags are
4326 * in the userspace exported format.
4327 */
4328int dev_change_flags(struct net_device *dev, unsigned flags)
4329{
4330 int ret, changes;
4331 int old_flags = dev->flags;
4332
4333 ret = __dev_change_flags(dev, flags);
4334 if (ret < 0)
4335 return ret;
4336
4337 changes = old_flags ^ dev->flags;
4338 if (changes)
4339 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
4340
4341 __dev_notify_flags(dev, old_flags);
4342 return ret;
4343}
4344EXPORT_SYMBOL(dev_change_flags);
4345
4346/**
4347 * dev_set_mtu - Change maximum transfer unit
4348 * @dev: device
4349 * @new_mtu: new transfer unit
4350 *
4351 * Change the maximum transfer size of the network device.
4352 */
4353int dev_set_mtu(struct net_device *dev, int new_mtu)
4354{
4355 const struct net_device_ops *ops = dev->netdev_ops;
4356 int err;
4357
4358 if (new_mtu == dev->mtu)
4359 return 0;
4360
4361 /* MTU must be positive. */
4362 if (new_mtu < 0)
4363 return -EINVAL;
4364
4365 if (!netif_device_present(dev))
4366 return -ENODEV;
4367
4368 err = 0;
4369 if (ops->ndo_change_mtu)
4370 err = ops->ndo_change_mtu(dev, new_mtu);
4371 else
4372 dev->mtu = new_mtu;
4373
4374 if (!err && dev->flags & IFF_UP)
4375 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
4376 return err;
4377}
4378EXPORT_SYMBOL(dev_set_mtu);
4379
4380/**
4381 * dev_set_mac_address - Change Media Access Control Address
4382 * @dev: device
4383 * @sa: new address
4384 *
4385 * Change the hardware (MAC) address of the device
4386 */
4387int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
4388{
4389 const struct net_device_ops *ops = dev->netdev_ops;
4390 int err;
4391
4392 if (!ops->ndo_set_mac_address)
4393 return -EOPNOTSUPP;
4394 if (sa->sa_family != dev->type)
4395 return -EINVAL;
4396 if (!netif_device_present(dev))
4397 return -ENODEV;
4398 err = ops->ndo_set_mac_address(dev, sa);
4399 if (!err)
4400 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
4401 return err;
4402}
4403EXPORT_SYMBOL(dev_set_mac_address);
4404
4405/*
4406 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
4407 */
4408static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
4409{
4410 int err;
4411 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
4412
4413 if (!dev)
4414 return -ENODEV;
4415
4416 switch (cmd) {
4417 case SIOCGIFFLAGS: /* Get interface flags */
4418 ifr->ifr_flags = (short) dev_get_flags(dev);
4419 return 0;
4420
4421 case SIOCGIFMETRIC: /* Get the metric on the interface
4422 (currently unused) */
4423 ifr->ifr_metric = 0;
4424 return 0;
4425
4426 case SIOCGIFMTU: /* Get the MTU of a device */
4427 ifr->ifr_mtu = dev->mtu;
4428 return 0;
4429
4430 case SIOCGIFHWADDR:
4431 if (!dev->addr_len)
4432 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
4433 else
4434 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
4435 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
4436 ifr->ifr_hwaddr.sa_family = dev->type;
4437 return 0;
4438
4439 case SIOCGIFSLAVE:
4440 err = -EINVAL;
4441 break;
4442
4443 case SIOCGIFMAP:
4444 ifr->ifr_map.mem_start = dev->mem_start;
4445 ifr->ifr_map.mem_end = dev->mem_end;
4446 ifr->ifr_map.base_addr = dev->base_addr;
4447 ifr->ifr_map.irq = dev->irq;
4448 ifr->ifr_map.dma = dev->dma;
4449 ifr->ifr_map.port = dev->if_port;
4450 return 0;
4451
4452 case SIOCGIFINDEX:
4453 ifr->ifr_ifindex = dev->ifindex;
4454 return 0;
4455
4456 case SIOCGIFTXQLEN:
4457 ifr->ifr_qlen = dev->tx_queue_len;
4458 return 0;
4459
4460 default:
4461 /* dev_ioctl() should ensure this case
4462 * is never reached
4463 */
4464 WARN_ON(1);
4465 err = -EINVAL;
4466 break;
4467
4468 }
4469 return err;
4470}
4471
4472/*
4473 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
4474 */
4475static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
4476{
4477 int err;
4478 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
4479 const struct net_device_ops *ops;
4480
4481 if (!dev)
4482 return -ENODEV;
4483
4484 ops = dev->netdev_ops;
4485
4486 switch (cmd) {
4487 case SIOCSIFFLAGS: /* Set interface flags */
4488 return dev_change_flags(dev, ifr->ifr_flags);
4489
4490 case SIOCSIFMETRIC: /* Set the metric on the interface
4491 (currently unused) */
4492 return -EOPNOTSUPP;
4493
4494 case SIOCSIFMTU: /* Set the MTU of a device */
4495 return dev_set_mtu(dev, ifr->ifr_mtu);
4496
4497 case SIOCSIFHWADDR:
4498 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
4499
4500 case SIOCSIFHWBROADCAST:
4501 if (ifr->ifr_hwaddr.sa_family != dev->type)
4502 return -EINVAL;
4503 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
4504 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
4505 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
4506 return 0;
4507
4508 case SIOCSIFMAP:
4509 if (ops->ndo_set_config) {
4510 if (!netif_device_present(dev))
4511 return -ENODEV;
4512 return ops->ndo_set_config(dev, &ifr->ifr_map);
4513 }
4514 return -EOPNOTSUPP;
4515
4516 case SIOCADDMULTI:
4517 if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
4518 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
4519 return -EINVAL;
4520 if (!netif_device_present(dev))
4521 return -ENODEV;
4522 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
4523
4524 case SIOCDELMULTI:
4525 if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
4526 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
4527 return -EINVAL;
4528 if (!netif_device_present(dev))
4529 return -ENODEV;
4530 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
4531
4532 case SIOCSIFTXQLEN:
4533 if (ifr->ifr_qlen < 0)
4534 return -EINVAL;
4535 dev->tx_queue_len = ifr->ifr_qlen;
4536 return 0;
4537
4538 case SIOCSIFNAME:
4539 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
4540 return dev_change_name(dev, ifr->ifr_newname);
4541
4542 /*
4543 * Unknown or private ioctl
4544 */
4545 default:
4546 if ((cmd >= SIOCDEVPRIVATE &&
4547 cmd <= SIOCDEVPRIVATE + 15) ||
4548 cmd == SIOCBONDENSLAVE ||
4549 cmd == SIOCBONDRELEASE ||
4550 cmd == SIOCBONDSETHWADDR ||
4551 cmd == SIOCBONDSLAVEINFOQUERY ||
4552 cmd == SIOCBONDINFOQUERY ||
4553 cmd == SIOCBONDCHANGEACTIVE ||
4554 cmd == SIOCGMIIPHY ||
4555 cmd == SIOCGMIIREG ||
4556 cmd == SIOCSMIIREG ||
4557 cmd == SIOCBRADDIF ||
4558 cmd == SIOCBRDELIF ||
4559 cmd == SIOCSHWTSTAMP ||
4560 cmd == SIOCWANDEV) {
4561 err = -EOPNOTSUPP;
4562 if (ops->ndo_do_ioctl) {
4563 if (netif_device_present(dev))
4564 err = ops->ndo_do_ioctl(dev, ifr, cmd);
4565 else
4566 err = -ENODEV;
4567 }
4568 } else
4569 err = -EINVAL;
4570
4571 }
4572 return err;
4573}
4574
4575/*
4576 * This function handles all "interface"-type I/O control requests. The actual
4577 * 'doing' part of this is dev_ifsioc above.
4578 */
4579
4580/**
4581 * dev_ioctl - network device ioctl
4582 * @net: the applicable net namespace
4583 * @cmd: command to issue
4584 * @arg: pointer to a struct ifreq in user space
4585 *
4586 * Issue ioctl functions to devices. This is normally called by the
4587 * user space syscall interfaces but can sometimes be useful for
4588 * other purposes. The return value is the return from the syscall if
4589 * positive or a negative errno code on error.
4590 */
4591
4592int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
4593{
4594 struct ifreq ifr;
4595 int ret;
4596 char *colon;
4597
4598 /* One special case: SIOCGIFCONF takes ifconf argument
4599 and requires shared lock, because it sleeps writing
4600 to user space.
4601 */
4602
4603 if (cmd == SIOCGIFCONF) {
4604 rtnl_lock();
4605 ret = dev_ifconf(net, (char __user *) arg);
4606 rtnl_unlock();
4607 return ret;
4608 }
4609 if (cmd == SIOCGIFNAME)
4610 return dev_ifname(net, (struct ifreq __user *)arg);
4611
4612 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
4613 return -EFAULT;
4614
4615 ifr.ifr_name[IFNAMSIZ-1] = 0;
4616
4617 colon = strchr(ifr.ifr_name, ':');
4618 if (colon)
4619 *colon = 0;
4620
4621 /*
4622 * See which interface the caller is talking about.
4623 */
4624
4625 switch (cmd) {
4626 /*
4627 * These ioctl calls:
4628 * - can be done by all.
4629 * - atomic and do not require locking.
4630 * - return a value
4631 */
4632 case SIOCGIFFLAGS:
4633 case SIOCGIFMETRIC:
4634 case SIOCGIFMTU:
4635 case SIOCGIFHWADDR:
4636 case SIOCGIFSLAVE:
4637 case SIOCGIFMAP:
4638 case SIOCGIFINDEX:
4639 case SIOCGIFTXQLEN:
4640 dev_load(net, ifr.ifr_name);
4641 rcu_read_lock();
4642 ret = dev_ifsioc_locked(net, &ifr, cmd);
4643 rcu_read_unlock();
4644 if (!ret) {
4645 if (colon)
4646 *colon = ':';
4647 if (copy_to_user(arg, &ifr,
4648 sizeof(struct ifreq)))
4649 ret = -EFAULT;
4650 }
4651 return ret;
4652
4653 case SIOCETHTOOL:
4654 dev_load(net, ifr.ifr_name);
4655 rtnl_lock();
4656 ret = dev_ethtool(net, &ifr);
4657 rtnl_unlock();
4658 if (!ret) {
4659 if (colon)
4660 *colon = ':';
4661 if (copy_to_user(arg, &ifr,
4662 sizeof(struct ifreq)))
4663 ret = -EFAULT;
4664 }
4665 return ret;
4666
4667 /*
4668 * These ioctl calls:
4669 * - require superuser power.
4670 * - require strict serialization.
4671 * - return a value
4672 */
4673 case SIOCGMIIPHY:
4674 case SIOCGMIIREG:
4675 case SIOCSIFNAME:
4676 if (!capable(CAP_NET_ADMIN))
4677 return -EPERM;
4678 dev_load(net, ifr.ifr_name);
4679 rtnl_lock();
4680 ret = dev_ifsioc(net, &ifr, cmd);
4681 rtnl_unlock();
4682 if (!ret) {
4683 if (colon)
4684 *colon = ':';
4685 if (copy_to_user(arg, &ifr,
4686 sizeof(struct ifreq)))
4687 ret = -EFAULT;
4688 }
4689 return ret;
4690
4691 /*
4692 * These ioctl calls:
4693 * - require superuser power.
4694 * - require strict serialization.
4695 * - do not return a value
4696 */
4697 case SIOCSIFFLAGS:
4698 case SIOCSIFMETRIC:
4699 case SIOCSIFMTU:
4700 case SIOCSIFMAP:
4701 case SIOCSIFHWADDR:
4702 case SIOCSIFSLAVE:
4703 case SIOCADDMULTI:
4704 case SIOCDELMULTI:
4705 case SIOCSIFHWBROADCAST:
4706 case SIOCSIFTXQLEN:
4707 case SIOCSMIIREG:
4708 case SIOCBONDENSLAVE:
4709 case SIOCBONDRELEASE:
4710 case SIOCBONDSETHWADDR:
4711 case SIOCBONDCHANGEACTIVE:
4712 case SIOCBRADDIF:
4713 case SIOCBRDELIF:
4714 case SIOCSHWTSTAMP:
4715 if (!capable(CAP_NET_ADMIN))
4716 return -EPERM;
4717 /* fall through */
4718 case SIOCBONDSLAVEINFOQUERY:
4719 case SIOCBONDINFOQUERY:
4720 dev_load(net, ifr.ifr_name);
4721 rtnl_lock();
4722 ret = dev_ifsioc(net, &ifr, cmd);
4723 rtnl_unlock();
4724 return ret;
4725
4726 case SIOCGIFMEM:
4727 /* Get the per device memory space. We can add this but
4728 * currently do not support it */
4729 case SIOCSIFMEM:
4730 /* Set the per device memory buffer space.
4731 * Not applicable in our case */
4732 case SIOCSIFLINK:
4733 return -EINVAL;
4734
4735 /*
4736 * Unknown or private ioctl.
4737 */
4738 default:
4739 if (cmd == SIOCWANDEV ||
4740 (cmd >= SIOCDEVPRIVATE &&
4741 cmd <= SIOCDEVPRIVATE + 15)) {
4742 dev_load(net, ifr.ifr_name);
4743 rtnl_lock();
4744 ret = dev_ifsioc(net, &ifr, cmd);
4745 rtnl_unlock();
4746 if (!ret && copy_to_user(arg, &ifr,
4747 sizeof(struct ifreq)))
4748 ret = -EFAULT;
4749 return ret;
4750 }
4751 /* Take care of Wireless Extensions */
4752 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
4753 return wext_handle_ioctl(net, &ifr, cmd, arg);
4754 return -EINVAL;
4755 }
4756}
4757
4758
4759/**
4760 * dev_new_index - allocate an ifindex
4761 * @net: the applicable net namespace
4762 *
4763 * Returns a suitable unique value for a new device interface
4764 * number. The caller must hold the rtnl semaphore or the
4765 * dev_base_lock to be sure it remains unique.
4766 */
4767static int dev_new_index(struct net *net)
4768{
4769 static int ifindex;
4770 for (;;) {
4771 if (++ifindex <= 0)
4772 ifindex = 1;
4773 if (!__dev_get_by_index(net, ifindex))
4774 return ifindex;
4775 }
4776}
4777
4778/* Delayed registration/unregisteration */
4779static LIST_HEAD(net_todo_list);
4780
4781static void net_set_todo(struct net_device *dev)
4782{
4783 list_add_tail(&dev->todo_list, &net_todo_list);
4784}
4785
4786static void rollback_registered_many(struct list_head *head)
4787{
4788 struct net_device *dev, *tmp;
4789
4790 BUG_ON(dev_boot_phase);
4791 ASSERT_RTNL();
4792
4793 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
4794 /* Some devices call without registering
4795 * for initialization unwind. Remove those
4796 * devices and proceed with the remaining.
4797 */
4798 if (dev->reg_state == NETREG_UNINITIALIZED) {
4799 pr_debug("unregister_netdevice: device %s/%p never "
4800 "was registered\n", dev->name, dev);
4801
4802 WARN_ON(1);
4803 list_del(&dev->unreg_list);
4804 continue;
4805 }
4806
4807 BUG_ON(dev->reg_state != NETREG_REGISTERED);
4808
4809 /* If device is running, close it first. */
4810 dev_close(dev);
4811
4812 /* And unlink it from device chain. */
4813 unlist_netdevice(dev);
4814
4815 dev->reg_state = NETREG_UNREGISTERING;
4816 }
4817
4818 synchronize_net();
4819
4820 list_for_each_entry(dev, head, unreg_list) {
4821 /* Shutdown queueing discipline. */
4822 dev_shutdown(dev);
4823
4824
4825 /* Notify protocols, that we are about to destroy
4826 this device. They should clean all the things.
4827 */
4828 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
4829
4830 if (!dev->rtnl_link_ops ||
4831 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
4832 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
4833
4834 /*
4835 * Flush the unicast and multicast chains
4836 */
4837 dev_uc_flush(dev);
4838 dev_mc_flush(dev);
4839
4840 if (dev->netdev_ops->ndo_uninit)
4841 dev->netdev_ops->ndo_uninit(dev);
4842
4843 /* Notifier chain MUST detach us from master device. */
4844 WARN_ON(dev->master);
4845
4846 /* Remove entries from kobject tree */
4847 netdev_unregister_kobject(dev);
4848 }
4849
4850 /* Process any work delayed until the end of the batch */
4851 dev = list_first_entry(head, struct net_device, unreg_list);
4852 call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
4853
4854 synchronize_net();
4855
4856 list_for_each_entry(dev, head, unreg_list)
4857 dev_put(dev);
4858}
4859
4860static void rollback_registered(struct net_device *dev)
4861{
4862 LIST_HEAD(single);
4863
4864 list_add(&dev->unreg_list, &single);
4865 rollback_registered_many(&single);
4866}
4867
4868static void __netdev_init_queue_locks_one(struct net_device *dev,
4869 struct netdev_queue *dev_queue,
4870 void *_unused)
4871{
4872 spin_lock_init(&dev_queue->_xmit_lock);
4873 netdev_set_xmit_lockdep_class(&dev_queue->_xmit_lock, dev->type);
4874 dev_queue->xmit_lock_owner = -1;
4875}
4876
4877static void netdev_init_queue_locks(struct net_device *dev)
4878{
4879 netdev_for_each_tx_queue(dev, __netdev_init_queue_locks_one, NULL);
4880 __netdev_init_queue_locks_one(dev, &dev->rx_queue, NULL);
4881}
4882
4883unsigned long netdev_fix_features(unsigned long features, const char *name)
4884{
4885 /* Fix illegal SG+CSUM combinations. */
4886 if ((features & NETIF_F_SG) &&
4887 !(features & NETIF_F_ALL_CSUM)) {
4888 if (name)
4889 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no "
4890 "checksum feature.\n", name);
4891 features &= ~NETIF_F_SG;
4892 }
4893
4894 /* TSO requires that SG is present as well. */
4895 if ((features & NETIF_F_TSO) && !(features & NETIF_F_SG)) {
4896 if (name)
4897 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no "
4898 "SG feature.\n", name);
4899 features &= ~NETIF_F_TSO;
4900 }
4901
4902 if (features & NETIF_F_UFO) {
4903 if (!(features & NETIF_F_GEN_CSUM)) {
4904 if (name)
4905 printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
4906 "since no NETIF_F_HW_CSUM feature.\n",
4907 name);
4908 features &= ~NETIF_F_UFO;
4909 }
4910
4911 if (!(features & NETIF_F_SG)) {
4912 if (name)
4913 printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
4914 "since no NETIF_F_SG feature.\n", name);
4915 features &= ~NETIF_F_UFO;
4916 }
4917 }
4918
4919 return features;
4920}
4921EXPORT_SYMBOL(netdev_fix_features);
4922
4923/**
4924 * netif_stacked_transfer_operstate - transfer operstate
4925 * @rootdev: the root or lower level device to transfer state from
4926 * @dev: the device to transfer operstate to
4927 *
4928 * Transfer operational state from root to device. This is normally
4929 * called when a stacking relationship exists between the root
4930 * device and the device(a leaf device).
4931 */
4932void netif_stacked_transfer_operstate(const struct net_device *rootdev,
4933 struct net_device *dev)
4934{
4935 if (rootdev->operstate == IF_OPER_DORMANT)
4936 netif_dormant_on(dev);
4937 else
4938 netif_dormant_off(dev);
4939
4940 if (netif_carrier_ok(rootdev)) {
4941 if (!netif_carrier_ok(dev))
4942 netif_carrier_on(dev);
4943 } else {
4944 if (netif_carrier_ok(dev))
4945 netif_carrier_off(dev);
4946 }
4947}
4948EXPORT_SYMBOL(netif_stacked_transfer_operstate);
4949
4950/**
4951 * register_netdevice - register a network device
4952 * @dev: device to register
4953 *
4954 * Take a completed network device structure and add it to the kernel
4955 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
4956 * chain. 0 is returned on success. A negative errno code is returned
4957 * on a failure to set up the device, or if the name is a duplicate.
4958 *
4959 * Callers must hold the rtnl semaphore. You may want
4960 * register_netdev() instead of this.
4961 *
4962 * BUGS:
4963 * The locking appears insufficient to guarantee two parallel registers
4964 * will not get the same name.
4965 */
4966
4967int register_netdevice(struct net_device *dev)
4968{
4969 int ret;
4970 struct net *net = dev_net(dev);
4971
4972 BUG_ON(dev_boot_phase);
4973 ASSERT_RTNL();
4974
4975 might_sleep();
4976
4977 /* When net_device's are persistent, this will be fatal. */
4978 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
4979 BUG_ON(!net);
4980
4981 spin_lock_init(&dev->addr_list_lock);
4982 netdev_set_addr_lockdep_class(dev);
4983 netdev_init_queue_locks(dev);
4984
4985 dev->iflink = -1;
4986
4987#ifdef CONFIG_RPS
4988 if (!dev->num_rx_queues) {
4989 /*
4990 * Allocate a single RX queue if driver never called
4991 * alloc_netdev_mq
4992 */
4993
4994 dev->_rx = kzalloc(sizeof(struct netdev_rx_queue), GFP_KERNEL);
4995 if (!dev->_rx) {
4996 ret = -ENOMEM;
4997 goto out;
4998 }
4999
5000 dev->_rx->first = dev->_rx;
5001 atomic_set(&dev->_rx->count, 1);
5002 dev->num_rx_queues = 1;
5003 }
5004#endif
5005 /* Init, if this function is available */
5006 if (dev->netdev_ops->ndo_init) {
5007 ret = dev->netdev_ops->ndo_init(dev);
5008 if (ret) {
5009 if (ret > 0)
5010 ret = -EIO;
5011 goto out;
5012 }
5013 }
5014
5015 ret = dev_get_valid_name(dev, dev->name, 0);
5016 if (ret)
5017 goto err_uninit;
5018
5019 dev->ifindex = dev_new_index(net);
5020 if (dev->iflink == -1)
5021 dev->iflink = dev->ifindex;
5022
5023 /* Fix illegal checksum combinations */
5024 if ((dev->features & NETIF_F_HW_CSUM) &&
5025 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
5026 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
5027 dev->name);
5028 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
5029 }
5030
5031 if ((dev->features & NETIF_F_NO_CSUM) &&
5032 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
5033 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
5034 dev->name);
5035 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
5036 }
5037
5038 dev->features = netdev_fix_features(dev->features, dev->name);
5039
5040 /* Enable software GSO if SG is supported. */
5041 if (dev->features & NETIF_F_SG)
5042 dev->features |= NETIF_F_GSO;
5043
5044 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
5045 ret = notifier_to_errno(ret);
5046 if (ret)
5047 goto err_uninit;
5048
5049 ret = netdev_register_kobject(dev);
5050 if (ret)
5051 goto err_uninit;
5052 dev->reg_state = NETREG_REGISTERED;
5053
5054 /*
5055 * Default initial state at registry is that the
5056 * device is present.
5057 */
5058
5059 set_bit(__LINK_STATE_PRESENT, &dev->state);
5060
5061 dev_init_scheduler(dev);
5062 dev_hold(dev);
5063 list_netdevice(dev);
5064
5065 /* Notify protocols, that a new device appeared. */
5066 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
5067 ret = notifier_to_errno(ret);
5068 if (ret) {
5069 rollback_registered(dev);
5070 dev->reg_state = NETREG_UNREGISTERED;
5071 }
5072 /*
5073 * Prevent userspace races by waiting until the network
5074 * device is fully setup before sending notifications.
5075 */
5076 if (!dev->rtnl_link_ops ||
5077 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5078 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
5079
5080out:
5081 return ret;
5082
5083err_uninit:
5084 if (dev->netdev_ops->ndo_uninit)
5085 dev->netdev_ops->ndo_uninit(dev);
5086 goto out;
5087}
5088EXPORT_SYMBOL(register_netdevice);
5089
5090/**
5091 * init_dummy_netdev - init a dummy network device for NAPI
5092 * @dev: device to init
5093 *
5094 * This takes a network device structure and initialize the minimum
5095 * amount of fields so it can be used to schedule NAPI polls without
5096 * registering a full blown interface. This is to be used by drivers
5097 * that need to tie several hardware interfaces to a single NAPI
5098 * poll scheduler due to HW limitations.
5099 */
5100int init_dummy_netdev(struct net_device *dev)
5101{
5102 /* Clear everything. Note we don't initialize spinlocks
5103 * are they aren't supposed to be taken by any of the
5104 * NAPI code and this dummy netdev is supposed to be
5105 * only ever used for NAPI polls
5106 */
5107 memset(dev, 0, sizeof(struct net_device));
5108
5109 /* make sure we BUG if trying to hit standard
5110 * register/unregister code path
5111 */
5112 dev->reg_state = NETREG_DUMMY;
5113
5114 /* initialize the ref count */
5115 atomic_set(&dev->refcnt, 1);
5116
5117 /* NAPI wants this */
5118 INIT_LIST_HEAD(&dev->napi_list);
5119
5120 /* a dummy interface is started by default */
5121 set_bit(__LINK_STATE_PRESENT, &dev->state);
5122 set_bit(__LINK_STATE_START, &dev->state);
5123
5124 return 0;
5125}
5126EXPORT_SYMBOL_GPL(init_dummy_netdev);
5127
5128
5129/**
5130 * register_netdev - register a network device
5131 * @dev: device to register
5132 *
5133 * Take a completed network device structure and add it to the kernel
5134 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
5135 * chain. 0 is returned on success. A negative errno code is returned
5136 * on a failure to set up the device, or if the name is a duplicate.
5137 *
5138 * This is a wrapper around register_netdevice that takes the rtnl semaphore
5139 * and expands the device name if you passed a format string to
5140 * alloc_netdev.
5141 */
5142int register_netdev(struct net_device *dev)
5143{
5144 int err;
5145
5146 rtnl_lock();
5147
5148 /*
5149 * If the name is a format string the caller wants us to do a
5150 * name allocation.
5151 */
5152 if (strchr(dev->name, '%')) {
5153 err = dev_alloc_name(dev, dev->name);
5154 if (err < 0)
5155 goto out;
5156 }
5157
5158 err = register_netdevice(dev);
5159out:
5160 rtnl_unlock();
5161 return err;
5162}
5163EXPORT_SYMBOL(register_netdev);
5164
5165/*
5166 * netdev_wait_allrefs - wait until all references are gone.
5167 *
5168 * This is called when unregistering network devices.
5169 *
5170 * Any protocol or device that holds a reference should register
5171 * for netdevice notification, and cleanup and put back the
5172 * reference if they receive an UNREGISTER event.
5173 * We can get stuck here if buggy protocols don't correctly
5174 * call dev_put.
5175 */
5176static void netdev_wait_allrefs(struct net_device *dev)
5177{
5178 unsigned long rebroadcast_time, warning_time;
5179
5180 linkwatch_forget_dev(dev);
5181
5182 rebroadcast_time = warning_time = jiffies;
5183 while (atomic_read(&dev->refcnt) != 0) {
5184 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
5185 rtnl_lock();
5186
5187 /* Rebroadcast unregister notification */
5188 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
5189 /* don't resend NETDEV_UNREGISTER_BATCH, _BATCH users
5190 * should have already handle it the first time */
5191
5192 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
5193 &dev->state)) {
5194 /* We must not have linkwatch events
5195 * pending on unregister. If this
5196 * happens, we simply run the queue
5197 * unscheduled, resulting in a noop
5198 * for this device.
5199 */
5200 linkwatch_run_queue();
5201 }
5202
5203 __rtnl_unlock();
5204
5205 rebroadcast_time = jiffies;
5206 }
5207
5208 msleep(250);
5209
5210 if (time_after(jiffies, warning_time + 10 * HZ)) {
5211 printk(KERN_EMERG "unregister_netdevice: "
5212 "waiting for %s to become free. Usage "
5213 "count = %d\n",
5214 dev->name, atomic_read(&dev->refcnt));
5215 warning_time = jiffies;
5216 }
5217 }
5218}
5219
5220/* The sequence is:
5221 *
5222 * rtnl_lock();
5223 * ...
5224 * register_netdevice(x1);
5225 * register_netdevice(x2);
5226 * ...
5227 * unregister_netdevice(y1);
5228 * unregister_netdevice(y2);
5229 * ...
5230 * rtnl_unlock();
5231 * free_netdev(y1);
5232 * free_netdev(y2);
5233 *
5234 * We are invoked by rtnl_unlock().
5235 * This allows us to deal with problems:
5236 * 1) We can delete sysfs objects which invoke hotplug
5237 * without deadlocking with linkwatch via keventd.
5238 * 2) Since we run with the RTNL semaphore not held, we can sleep
5239 * safely in order to wait for the netdev refcnt to drop to zero.
5240 *
5241 * We must not return until all unregister events added during
5242 * the interval the lock was held have been completed.
5243 */
5244void netdev_run_todo(void)
5245{
5246 struct list_head list;
5247
5248 /* Snapshot list, allow later requests */
5249 list_replace_init(&net_todo_list, &list);
5250
5251 __rtnl_unlock();
5252
5253 while (!list_empty(&list)) {
5254 struct net_device *dev
5255 = list_first_entry(&list, struct net_device, todo_list);
5256 list_del(&dev->todo_list);
5257
5258 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
5259 printk(KERN_ERR "network todo '%s' but state %d\n",
5260 dev->name, dev->reg_state);
5261 dump_stack();
5262 continue;
5263 }
5264
5265 dev->reg_state = NETREG_UNREGISTERED;
5266
5267 on_each_cpu(flush_backlog, dev, 1);
5268
5269 netdev_wait_allrefs(dev);
5270
5271 /* paranoia */
5272 BUG_ON(atomic_read(&dev->refcnt));
5273 WARN_ON(dev->ip_ptr);
5274 WARN_ON(dev->ip6_ptr);
5275 WARN_ON(dev->dn_ptr);
5276
5277 if (dev->destructor)
5278 dev->destructor(dev);
5279
5280 /* Free network device */
5281 kobject_put(&dev->dev.kobj);
5282 }
5283}
5284
5285/**
5286 * dev_txq_stats_fold - fold tx_queues stats
5287 * @dev: device to get statistics from
5288 * @stats: struct rtnl_link_stats64 to hold results
5289 */
5290void dev_txq_stats_fold(const struct net_device *dev,
5291 struct rtnl_link_stats64 *stats)
5292{
5293 u64 tx_bytes = 0, tx_packets = 0, tx_dropped = 0;
5294 unsigned int i;
5295 struct netdev_queue *txq;
5296
5297 for (i = 0; i < dev->num_tx_queues; i++) {
5298 txq = netdev_get_tx_queue(dev, i);
5299 spin_lock_bh(&txq->_xmit_lock);
5300 tx_bytes += txq->tx_bytes;
5301 tx_packets += txq->tx_packets;
5302 tx_dropped += txq->tx_dropped;
5303 spin_unlock_bh(&txq->_xmit_lock);
5304 }
5305 if (tx_bytes || tx_packets || tx_dropped) {
5306 stats->tx_bytes = tx_bytes;
5307 stats->tx_packets = tx_packets;
5308 stats->tx_dropped = tx_dropped;
5309 }
5310}
5311EXPORT_SYMBOL(dev_txq_stats_fold);
5312
5313/* Convert net_device_stats to rtnl_link_stats64. They have the same
5314 * fields in the same order, with only the type differing.
5315 */
5316static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
5317 const struct net_device_stats *netdev_stats)
5318{
5319#if BITS_PER_LONG == 64
5320 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
5321 memcpy(stats64, netdev_stats, sizeof(*stats64));
5322#else
5323 size_t i, n = sizeof(*stats64) / sizeof(u64);
5324 const unsigned long *src = (const unsigned long *)netdev_stats;
5325 u64 *dst = (u64 *)stats64;
5326
5327 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
5328 sizeof(*stats64) / sizeof(u64));
5329 for (i = 0; i < n; i++)
5330 dst[i] = src[i];
5331#endif
5332}
5333
5334/**
5335 * dev_get_stats - get network device statistics
5336 * @dev: device to get statistics from
5337 * @storage: place to store stats
5338 *
5339 * Get network statistics from device. Return @storage.
5340 * The device driver may provide its own method by setting
5341 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
5342 * otherwise the internal statistics structure is used.
5343 */
5344struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
5345 struct rtnl_link_stats64 *storage)
5346{
5347 const struct net_device_ops *ops = dev->netdev_ops;
5348
5349 if (ops->ndo_get_stats64) {
5350 memset(storage, 0, sizeof(*storage));
5351 return ops->ndo_get_stats64(dev, storage);
5352 }
5353 if (ops->ndo_get_stats) {
5354 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
5355 return storage;
5356 }
5357 netdev_stats_to_stats64(storage, &dev->stats);
5358 dev_txq_stats_fold(dev, storage);
5359 return storage;
5360}
5361EXPORT_SYMBOL(dev_get_stats);
5362
5363static void netdev_init_one_queue(struct net_device *dev,
5364 struct netdev_queue *queue,
5365 void *_unused)
5366{
5367 queue->dev = dev;
5368}
5369
5370static void netdev_init_queues(struct net_device *dev)
5371{
5372 netdev_init_one_queue(dev, &dev->rx_queue, NULL);
5373 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
5374 spin_lock_init(&dev->tx_global_lock);
5375}
5376
5377/**
5378 * alloc_netdev_mq - allocate network device
5379 * @sizeof_priv: size of private data to allocate space for
5380 * @name: device name format string
5381 * @setup: callback to initialize device
5382 * @queue_count: the number of subqueues to allocate
5383 *
5384 * Allocates a struct net_device with private data area for driver use
5385 * and performs basic initialization. Also allocates subquue structs
5386 * for each queue on the device at the end of the netdevice.
5387 */
5388struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
5389 void (*setup)(struct net_device *), unsigned int queue_count)
5390{
5391 struct netdev_queue *tx;
5392 struct net_device *dev;
5393 size_t alloc_size;
5394 struct net_device *p;
5395#ifdef CONFIG_RPS
5396 struct netdev_rx_queue *rx;
5397 int i;
5398#endif
5399
5400 BUG_ON(strlen(name) >= sizeof(dev->name));
5401
5402 alloc_size = sizeof(struct net_device);
5403 if (sizeof_priv) {
5404 /* ensure 32-byte alignment of private area */
5405 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
5406 alloc_size += sizeof_priv;
5407 }
5408 /* ensure 32-byte alignment of whole construct */
5409 alloc_size += NETDEV_ALIGN - 1;
5410
5411 p = kzalloc(alloc_size, GFP_KERNEL);
5412 if (!p) {
5413 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
5414 return NULL;
5415 }
5416
5417 tx = kcalloc(queue_count, sizeof(struct netdev_queue), GFP_KERNEL);
5418 if (!tx) {
5419 printk(KERN_ERR "alloc_netdev: Unable to allocate "
5420 "tx qdiscs.\n");
5421 goto free_p;
5422 }
5423
5424#ifdef CONFIG_RPS
5425 rx = kcalloc(queue_count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
5426 if (!rx) {
5427 printk(KERN_ERR "alloc_netdev: Unable to allocate "
5428 "rx queues.\n");
5429 goto free_tx;
5430 }
5431
5432 atomic_set(&rx->count, queue_count);
5433
5434 /*
5435 * Set a pointer to first element in the array which holds the
5436 * reference count.
5437 */
5438 for (i = 0; i < queue_count; i++)
5439 rx[i].first = rx;
5440#endif
5441
5442 dev = PTR_ALIGN(p, NETDEV_ALIGN);
5443 dev->padded = (char *)dev - (char *)p;
5444
5445 if (dev_addr_init(dev))
5446 goto free_rx;
5447
5448 dev_mc_init(dev);
5449 dev_uc_init(dev);
5450
5451 dev_net_set(dev, &init_net);
5452
5453 dev->_tx = tx;
5454 dev->num_tx_queues = queue_count;
5455 dev->real_num_tx_queues = queue_count;
5456
5457#ifdef CONFIG_RPS
5458 dev->_rx = rx;
5459 dev->num_rx_queues = queue_count;
5460#endif
5461
5462 dev->gso_max_size = GSO_MAX_SIZE;
5463
5464 netdev_init_queues(dev);
5465
5466 INIT_LIST_HEAD(&dev->ethtool_ntuple_list.list);
5467 dev->ethtool_ntuple_list.count = 0;
5468 INIT_LIST_HEAD(&dev->napi_list);
5469 INIT_LIST_HEAD(&dev->unreg_list);
5470 INIT_LIST_HEAD(&dev->link_watch_list);
5471 dev->priv_flags = IFF_XMIT_DST_RELEASE;
5472 setup(dev);
5473 strcpy(dev->name, name);
5474 return dev;
5475
5476free_rx:
5477#ifdef CONFIG_RPS
5478 kfree(rx);
5479free_tx:
5480#endif
5481 kfree(tx);
5482free_p:
5483 kfree(p);
5484 return NULL;
5485}
5486EXPORT_SYMBOL(alloc_netdev_mq);
5487
5488/**
5489 * free_netdev - free network device
5490 * @dev: device
5491 *
5492 * This function does the last stage of destroying an allocated device
5493 * interface. The reference to the device object is released.
5494 * If this is the last reference then it will be freed.
5495 */
5496void free_netdev(struct net_device *dev)
5497{
5498 struct napi_struct *p, *n;
5499
5500 release_net(dev_net(dev));
5501
5502 kfree(dev->_tx);
5503
5504 /* Flush device addresses */
5505 dev_addr_flush(dev);
5506
5507 /* Clear ethtool n-tuple list */
5508 ethtool_ntuple_flush(dev);
5509
5510 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
5511 netif_napi_del(p);
5512
5513 /* Compatibility with error handling in drivers */
5514 if (dev->reg_state == NETREG_UNINITIALIZED) {
5515 kfree((char *)dev - dev->padded);
5516 return;
5517 }
5518
5519 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
5520 dev->reg_state = NETREG_RELEASED;
5521
5522 /* will free via device release */
5523 put_device(&dev->dev);
5524}
5525EXPORT_SYMBOL(free_netdev);
5526
5527/**
5528 * synchronize_net - Synchronize with packet receive processing
5529 *
5530 * Wait for packets currently being received to be done.
5531 * Does not block later packets from starting.
5532 */
5533void synchronize_net(void)
5534{
5535 might_sleep();
5536 synchronize_rcu();
5537}
5538EXPORT_SYMBOL(synchronize_net);
5539
5540/**
5541 * unregister_netdevice_queue - remove device from the kernel
5542 * @dev: device
5543 * @head: list
5544 *
5545 * This function shuts down a device interface and removes it
5546 * from the kernel tables.
5547 * If head not NULL, device is queued to be unregistered later.
5548 *
5549 * Callers must hold the rtnl semaphore. You may want
5550 * unregister_netdev() instead of this.
5551 */
5552
5553void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
5554{
5555 ASSERT_RTNL();
5556
5557 if (head) {
5558 list_move_tail(&dev->unreg_list, head);
5559 } else {
5560 rollback_registered(dev);
5561 /* Finish processing unregister after unlock */
5562 net_set_todo(dev);
5563 }
5564}
5565EXPORT_SYMBOL(unregister_netdevice_queue);
5566
5567/**
5568 * unregister_netdevice_many - unregister many devices
5569 * @head: list of devices
5570 */
5571void unregister_netdevice_many(struct list_head *head)
5572{
5573 struct net_device *dev;
5574
5575 if (!list_empty(head)) {
5576 rollback_registered_many(head);
5577 list_for_each_entry(dev, head, unreg_list)
5578 net_set_todo(dev);
5579 }
5580}
5581EXPORT_SYMBOL(unregister_netdevice_many);
5582
5583/**
5584 * unregister_netdev - remove device from the kernel
5585 * @dev: device
5586 *
5587 * This function shuts down a device interface and removes it
5588 * from the kernel tables.
5589 *
5590 * This is just a wrapper for unregister_netdevice that takes
5591 * the rtnl semaphore. In general you want to use this and not
5592 * unregister_netdevice.
5593 */
5594void unregister_netdev(struct net_device *dev)
5595{
5596 rtnl_lock();
5597 unregister_netdevice(dev);
5598 rtnl_unlock();
5599}
5600EXPORT_SYMBOL(unregister_netdev);
5601
5602/**
5603 * dev_change_net_namespace - move device to different nethost namespace
5604 * @dev: device
5605 * @net: network namespace
5606 * @pat: If not NULL name pattern to try if the current device name
5607 * is already taken in the destination network namespace.
5608 *
5609 * This function shuts down a device interface and moves it
5610 * to a new network namespace. On success 0 is returned, on
5611 * a failure a netagive errno code is returned.
5612 *
5613 * Callers must hold the rtnl semaphore.
5614 */
5615
5616int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
5617{
5618 int err;
5619
5620 ASSERT_RTNL();
5621
5622 /* Don't allow namespace local devices to be moved. */
5623 err = -EINVAL;
5624 if (dev->features & NETIF_F_NETNS_LOCAL)
5625 goto out;
5626
5627 /* Ensure the device has been registrered */
5628 err = -EINVAL;
5629 if (dev->reg_state != NETREG_REGISTERED)
5630 goto out;
5631
5632 /* Get out if there is nothing todo */
5633 err = 0;
5634 if (net_eq(dev_net(dev), net))
5635 goto out;
5636
5637 /* Pick the destination device name, and ensure
5638 * we can use it in the destination network namespace.
5639 */
5640 err = -EEXIST;
5641 if (__dev_get_by_name(net, dev->name)) {
5642 /* We get here if we can't use the current device name */
5643 if (!pat)
5644 goto out;
5645 if (dev_get_valid_name(dev, pat, 1))
5646 goto out;
5647 }
5648
5649 /*
5650 * And now a mini version of register_netdevice unregister_netdevice.
5651 */
5652
5653 /* If device is running close it first. */
5654 dev_close(dev);
5655
5656 /* And unlink it from device chain */
5657 err = -ENODEV;
5658 unlist_netdevice(dev);
5659
5660 synchronize_net();
5661
5662 /* Shutdown queueing discipline. */
5663 dev_shutdown(dev);
5664
5665 /* Notify protocols, that we are about to destroy
5666 this device. They should clean all the things.
5667 */
5668 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
5669 call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
5670
5671 /*
5672 * Flush the unicast and multicast chains
5673 */
5674 dev_uc_flush(dev);
5675 dev_mc_flush(dev);
5676
5677 /* Actually switch the network namespace */
5678 dev_net_set(dev, net);
5679
5680 /* If there is an ifindex conflict assign a new one */
5681 if (__dev_get_by_index(net, dev->ifindex)) {
5682 int iflink = (dev->iflink == dev->ifindex);
5683 dev->ifindex = dev_new_index(net);
5684 if (iflink)
5685 dev->iflink = dev->ifindex;
5686 }
5687
5688 /* Fixup kobjects */
5689 err = device_rename(&dev->dev, dev->name);
5690 WARN_ON(err);
5691
5692 /* Add the device back in the hashes */
5693 list_netdevice(dev);
5694
5695 /* Notify protocols, that a new device appeared. */
5696 call_netdevice_notifiers(NETDEV_REGISTER, dev);
5697
5698 /*
5699 * Prevent userspace races by waiting until the network
5700 * device is fully setup before sending notifications.
5701 */
5702 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
5703
5704 synchronize_net();
5705 err = 0;
5706out:
5707 return err;
5708}
5709EXPORT_SYMBOL_GPL(dev_change_net_namespace);
5710
5711static int dev_cpu_callback(struct notifier_block *nfb,
5712 unsigned long action,
5713 void *ocpu)
5714{
5715 struct sk_buff **list_skb;
5716 struct sk_buff *skb;
5717 unsigned int cpu, oldcpu = (unsigned long)ocpu;
5718 struct softnet_data *sd, *oldsd;
5719
5720 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
5721 return NOTIFY_OK;
5722
5723 local_irq_disable();
5724 cpu = smp_processor_id();
5725 sd = &per_cpu(softnet_data, cpu);
5726 oldsd = &per_cpu(softnet_data, oldcpu);
5727
5728 /* Find end of our completion_queue. */
5729 list_skb = &sd->completion_queue;
5730 while (*list_skb)
5731 list_skb = &(*list_skb)->next;
5732 /* Append completion queue from offline CPU. */
5733 *list_skb = oldsd->completion_queue;
5734 oldsd->completion_queue = NULL;
5735
5736 /* Append output queue from offline CPU. */
5737 if (oldsd->output_queue) {
5738 *sd->output_queue_tailp = oldsd->output_queue;
5739 sd->output_queue_tailp = oldsd->output_queue_tailp;
5740 oldsd->output_queue = NULL;
5741 oldsd->output_queue_tailp = &oldsd->output_queue;
5742 }
5743
5744 raise_softirq_irqoff(NET_TX_SOFTIRQ);
5745 local_irq_enable();
5746
5747 /* Process offline CPU's input_pkt_queue */
5748 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
5749 netif_rx(skb);
5750 input_queue_head_incr(oldsd);
5751 }
5752 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
5753 netif_rx(skb);
5754 input_queue_head_incr(oldsd);
5755 }
5756
5757 return NOTIFY_OK;
5758}
5759
5760
5761/**
5762 * netdev_increment_features - increment feature set by one
5763 * @all: current feature set
5764 * @one: new feature set
5765 * @mask: mask feature set
5766 *
5767 * Computes a new feature set after adding a device with feature set
5768 * @one to the master device with current feature set @all. Will not
5769 * enable anything that is off in @mask. Returns the new feature set.
5770 */
5771unsigned long netdev_increment_features(unsigned long all, unsigned long one,
5772 unsigned long mask)
5773{
5774 /* If device needs checksumming, downgrade to it. */
5775 if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
5776 all ^= NETIF_F_NO_CSUM | (one & NETIF_F_ALL_CSUM);
5777 else if (mask & NETIF_F_ALL_CSUM) {
5778 /* If one device supports v4/v6 checksumming, set for all. */
5779 if (one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM) &&
5780 !(all & NETIF_F_GEN_CSUM)) {
5781 all &= ~NETIF_F_ALL_CSUM;
5782 all |= one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
5783 }
5784
5785 /* If one device supports hw checksumming, set for all. */
5786 if (one & NETIF_F_GEN_CSUM && !(all & NETIF_F_GEN_CSUM)) {
5787 all &= ~NETIF_F_ALL_CSUM;
5788 all |= NETIF_F_HW_CSUM;
5789 }
5790 }
5791
5792 one |= NETIF_F_ALL_CSUM;
5793
5794 one |= all & NETIF_F_ONE_FOR_ALL;
5795 all &= one | NETIF_F_LLTX | NETIF_F_GSO | NETIF_F_UFO;
5796 all |= one & mask & NETIF_F_ONE_FOR_ALL;
5797
5798 return all;
5799}
5800EXPORT_SYMBOL(netdev_increment_features);
5801
5802static struct hlist_head *netdev_create_hash(void)
5803{
5804 int i;
5805 struct hlist_head *hash;
5806
5807 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
5808 if (hash != NULL)
5809 for (i = 0; i < NETDEV_HASHENTRIES; i++)
5810 INIT_HLIST_HEAD(&hash[i]);
5811
5812 return hash;
5813}
5814
5815/* Initialize per network namespace state */
5816static int __net_init netdev_init(struct net *net)
5817{
5818 INIT_LIST_HEAD(&net->dev_base_head);
5819
5820 net->dev_name_head = netdev_create_hash();
5821 if (net->dev_name_head == NULL)
5822 goto err_name;
5823
5824 net->dev_index_head = netdev_create_hash();
5825 if (net->dev_index_head == NULL)
5826 goto err_idx;
5827
5828 return 0;
5829
5830err_idx:
5831 kfree(net->dev_name_head);
5832err_name:
5833 return -ENOMEM;
5834}
5835
5836/**
5837 * netdev_drivername - network driver for the device
5838 * @dev: network device
5839 * @buffer: buffer for resulting name
5840 * @len: size of buffer
5841 *
5842 * Determine network driver for device.
5843 */
5844char *netdev_drivername(const struct net_device *dev, char *buffer, int len)
5845{
5846 const struct device_driver *driver;
5847 const struct device *parent;
5848
5849 if (len <= 0 || !buffer)
5850 return buffer;
5851 buffer[0] = 0;
5852
5853 parent = dev->dev.parent;
5854
5855 if (!parent)
5856 return buffer;
5857
5858 driver = parent->driver;
5859 if (driver && driver->name)
5860 strlcpy(buffer, driver->name, len);
5861 return buffer;
5862}
5863
5864static int __netdev_printk(const char *level, const struct net_device *dev,
5865 struct va_format *vaf)
5866{
5867 int r;
5868
5869 if (dev && dev->dev.parent)
5870 r = dev_printk(level, dev->dev.parent, "%s: %pV",
5871 netdev_name(dev), vaf);
5872 else if (dev)
5873 r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
5874 else
5875 r = printk("%s(NULL net_device): %pV", level, vaf);
5876
5877 return r;
5878}
5879
5880int netdev_printk(const char *level, const struct net_device *dev,
5881 const char *format, ...)
5882{
5883 struct va_format vaf;
5884 va_list args;
5885 int r;
5886
5887 va_start(args, format);
5888
5889 vaf.fmt = format;
5890 vaf.va = &args;
5891
5892 r = __netdev_printk(level, dev, &vaf);
5893 va_end(args);
5894
5895 return r;
5896}
5897EXPORT_SYMBOL(netdev_printk);
5898
5899#define define_netdev_printk_level(func, level) \
5900int func(const struct net_device *dev, const char *fmt, ...) \
5901{ \
5902 int r; \
5903 struct va_format vaf; \
5904 va_list args; \
5905 \
5906 va_start(args, fmt); \
5907 \
5908 vaf.fmt = fmt; \
5909 vaf.va = &args; \
5910 \
5911 r = __netdev_printk(level, dev, &vaf); \
5912 va_end(args); \
5913 \
5914 return r; \
5915} \
5916EXPORT_SYMBOL(func);
5917
5918define_netdev_printk_level(netdev_emerg, KERN_EMERG);
5919define_netdev_printk_level(netdev_alert, KERN_ALERT);
5920define_netdev_printk_level(netdev_crit, KERN_CRIT);
5921define_netdev_printk_level(netdev_err, KERN_ERR);
5922define_netdev_printk_level(netdev_warn, KERN_WARNING);
5923define_netdev_printk_level(netdev_notice, KERN_NOTICE);
5924define_netdev_printk_level(netdev_info, KERN_INFO);
5925
5926static void __net_exit netdev_exit(struct net *net)
5927{
5928 kfree(net->dev_name_head);
5929 kfree(net->dev_index_head);
5930}
5931
5932static struct pernet_operations __net_initdata netdev_net_ops = {
5933 .init = netdev_init,
5934 .exit = netdev_exit,
5935};
5936
5937static void __net_exit default_device_exit(struct net *net)
5938{
5939 struct net_device *dev, *aux;
5940 /*
5941 * Push all migratable network devices back to the
5942 * initial network namespace
5943 */
5944 rtnl_lock();
5945 for_each_netdev_safe(net, dev, aux) {
5946 int err;
5947 char fb_name[IFNAMSIZ];
5948
5949 /* Ignore unmoveable devices (i.e. loopback) */
5950 if (dev->features & NETIF_F_NETNS_LOCAL)
5951 continue;
5952
5953 /* Leave virtual devices for the generic cleanup */
5954 if (dev->rtnl_link_ops)
5955 continue;
5956
5957 /* Push remaing network devices to init_net */
5958 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
5959 err = dev_change_net_namespace(dev, &init_net, fb_name);
5960 if (err) {
5961 printk(KERN_EMERG "%s: failed to move %s to init_net: %d\n",
5962 __func__, dev->name, err);
5963 BUG();
5964 }
5965 }
5966 rtnl_unlock();
5967}
5968
5969static void __net_exit default_device_exit_batch(struct list_head *net_list)
5970{
5971 /* At exit all network devices most be removed from a network
5972 * namespace. Do this in the reverse order of registeration.
5973 * Do this across as many network namespaces as possible to
5974 * improve batching efficiency.
5975 */
5976 struct net_device *dev;
5977 struct net *net;
5978 LIST_HEAD(dev_kill_list);
5979
5980 rtnl_lock();
5981 list_for_each_entry(net, net_list, exit_list) {
5982 for_each_netdev_reverse(net, dev) {
5983 if (dev->rtnl_link_ops)
5984 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
5985 else
5986 unregister_netdevice_queue(dev, &dev_kill_list);
5987 }
5988 }
5989 unregister_netdevice_many(&dev_kill_list);
5990 rtnl_unlock();
5991}
5992
5993static struct pernet_operations __net_initdata default_device_ops = {
5994 .exit = default_device_exit,
5995 .exit_batch = default_device_exit_batch,
5996};
5997
5998/*
5999 * Initialize the DEV module. At boot time this walks the device list and
6000 * unhooks any devices that fail to initialise (normally hardware not
6001 * present) and leaves us with a valid list of present and active devices.
6002 *
6003 */
6004
6005/*
6006 * This is called single threaded during boot, so no need
6007 * to take the rtnl semaphore.
6008 */
6009static int __init net_dev_init(void)
6010{
6011 int i, rc = -ENOMEM;
6012
6013 BUG_ON(!dev_boot_phase);
6014
6015 if (dev_proc_init())
6016 goto out;
6017
6018 if (netdev_kobject_init())
6019 goto out;
6020
6021 INIT_LIST_HEAD(&ptype_all);
6022 for (i = 0; i < PTYPE_HASH_SIZE; i++)
6023 INIT_LIST_HEAD(&ptype_base[i]);
6024
6025 if (register_pernet_subsys(&netdev_net_ops))
6026 goto out;
6027
6028 /*
6029 * Initialise the packet receive queues.
6030 */
6031
6032 for_each_possible_cpu(i) {
6033 struct softnet_data *sd = &per_cpu(softnet_data, i);
6034
6035 memset(sd, 0, sizeof(*sd));
6036 skb_queue_head_init(&sd->input_pkt_queue);
6037 skb_queue_head_init(&sd->process_queue);
6038 sd->completion_queue = NULL;
6039 INIT_LIST_HEAD(&sd->poll_list);
6040 sd->output_queue = NULL;
6041 sd->output_queue_tailp = &sd->output_queue;
6042#ifdef CONFIG_RPS
6043 sd->csd.func = rps_trigger_softirq;
6044 sd->csd.info = sd;
6045 sd->csd.flags = 0;
6046 sd->cpu = i;
6047#endif
6048
6049 sd->backlog.poll = process_backlog;
6050 sd->backlog.weight = weight_p;
6051 sd->backlog.gro_list = NULL;
6052 sd->backlog.gro_count = 0;
6053 }
6054
6055 dev_boot_phase = 0;
6056
6057 /* The loopback device is special if any other network devices
6058 * is present in a network namespace the loopback device must
6059 * be present. Since we now dynamically allocate and free the
6060 * loopback device ensure this invariant is maintained by
6061 * keeping the loopback device as the first device on the
6062 * list of network devices. Ensuring the loopback devices
6063 * is the first device that appears and the last network device
6064 * that disappears.
6065 */
6066 if (register_pernet_device(&loopback_net_ops))
6067 goto out;
6068
6069 if (register_pernet_device(&default_device_ops))
6070 goto out;
6071
6072 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
6073 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
6074
6075 hotcpu_notifier(dev_cpu_callback, 0);
6076 dst_init();
6077 dev_mcast_init();
6078 rc = 0;
6079out:
6080 return rc;
6081}
6082
6083subsys_initcall(net_dev_init);
6084
6085static int __init initialize_hashrnd(void)
6086{
6087 get_random_bytes(&hashrnd, sizeof(hashrnd));
6088 return 0;
6089}
6090
6091late_initcall_sync(initialize_hashrnd);
6092