]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/core/dev.c
net: Rewrite netif_set_xps_queues to address several issues
[mirror_ubuntu-hirsute-kernel.git] / net / core / dev.c
CommitLineData
1da177e4
LT
1/*
2 * NET3 Protocol independent device support routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the non IP parts of dev.c 1.0.19
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dahinds@users.sourceforge.net>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
21 *
22 * Changes:
23 * D.J. Barrow : Fixed bug where dev->refcnt gets set
24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
27 * Alan Cox : device private ioctl copies fields back.
28 * Alan Cox : Transmit queue code does relevant
29 * stunts to keep the queue safe.
30 * Alan Cox : Fixed double lock.
31 * Alan Cox : Fixed promisc NULL pointer trap
32 * ???????? : Support the full private ioctl range
33 * Alan Cox : Moved ioctl permission check into
34 * drivers
35 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
36 * Alan Cox : 100 backlog just doesn't cut it when
37 * you start doing multicast video 8)
38 * Alan Cox : Rewrote net_bh and list manager.
39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
40 * Alan Cox : Took out transmit every packet pass
41 * Saved a few bytes in the ioctl handler
42 * Alan Cox : Network driver sets packet type before
43 * calling netif_rx. Saves a function
44 * call a packet.
45 * Alan Cox : Hashed net_bh()
46 * Richard Kooijman: Timestamp fixes.
47 * Alan Cox : Wrong field in SIOCGIFDSTADDR
48 * Alan Cox : Device lock protection.
49 * Alan Cox : Fixed nasty side effect of device close
50 * changes.
51 * Rudi Cilibrasi : Pass the right thing to
52 * set_mac_address()
53 * Dave Miller : 32bit quantity for the device lock to
54 * make it work out on a Sparc.
55 * Bjorn Ekwall : Added KERNELD hack.
56 * Alan Cox : Cleaned up the backlog initialise.
57 * Craig Metz : SIOCGIFCONF fix if space for under
58 * 1 device.
59 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
60 * is no device open function.
61 * Andi Kleen : Fix error reporting for SIOCGIFCONF
62 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
63 * Cyrus Durgin : Cleaned for KMOD
64 * Adam Sulmicki : Bug Fix : Network Device Unload
65 * A network device unload needs to purge
66 * the backlog queue.
67 * Paul Rusty Russell : SIOCSIFNAME
68 * Pekka Riikonen : Netdev boot-time settings code
69 * Andrew Morton : Make unregister_netdevice wait
70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
72 * - netif_rx() feedback
73 */
74
75#include <asm/uaccess.h>
1da177e4 76#include <linux/bitops.h>
4fc268d2 77#include <linux/capability.h>
1da177e4
LT
78#include <linux/cpu.h>
79#include <linux/types.h>
80#include <linux/kernel.h>
08e9897d 81#include <linux/hash.h>
5a0e3ad6 82#include <linux/slab.h>
1da177e4 83#include <linux/sched.h>
4a3e2f71 84#include <linux/mutex.h>
1da177e4
LT
85#include <linux/string.h>
86#include <linux/mm.h>
87#include <linux/socket.h>
88#include <linux/sockios.h>
89#include <linux/errno.h>
90#include <linux/interrupt.h>
91#include <linux/if_ether.h>
92#include <linux/netdevice.h>
93#include <linux/etherdevice.h>
0187bdfb 94#include <linux/ethtool.h>
1da177e4
LT
95#include <linux/notifier.h>
96#include <linux/skbuff.h>
457c4cbc 97#include <net/net_namespace.h>
1da177e4
LT
98#include <net/sock.h>
99#include <linux/rtnetlink.h>
100#include <linux/proc_fs.h>
101#include <linux/seq_file.h>
102#include <linux/stat.h>
1da177e4
LT
103#include <net/dst.h>
104#include <net/pkt_sched.h>
105#include <net/checksum.h>
44540960 106#include <net/xfrm.h>
1da177e4
LT
107#include <linux/highmem.h>
108#include <linux/init.h>
109#include <linux/kmod.h>
110#include <linux/module.h>
1da177e4
LT
111#include <linux/netpoll.h>
112#include <linux/rcupdate.h>
113#include <linux/delay.h>
295f4a1f 114#include <net/wext.h>
1da177e4 115#include <net/iw_handler.h>
1da177e4 116#include <asm/current.h>
5bdb9886 117#include <linux/audit.h>
db217334 118#include <linux/dmaengine.h>
f6a78bfc 119#include <linux/err.h>
c7fa9d18 120#include <linux/ctype.h>
723e98b7 121#include <linux/if_arp.h>
6de329e2 122#include <linux/if_vlan.h>
8f0f2223 123#include <linux/ip.h>
ad55dcaf 124#include <net/ip.h>
8f0f2223
DM
125#include <linux/ipv6.h>
126#include <linux/in.h>
b6b2fed1
DM
127#include <linux/jhash.h>
128#include <linux/random.h>
9cbc1cb8 129#include <trace/events/napi.h>
cf66ba58 130#include <trace/events/net.h>
07dc22e7 131#include <trace/events/skb.h>
5acbbd42 132#include <linux/pci.h>
caeda9b9 133#include <linux/inetdevice.h>
c445477d 134#include <linux/cpu_rmap.h>
4dc360c5 135#include <linux/net_tstamp.h>
c5905afb 136#include <linux/static_key.h>
4504b861 137#include <net/flow_keys.h>
1da177e4 138
342709ef
PE
139#include "net-sysfs.h"
140
d565b0a1
HX
141/* Instead of increasing this, you should create a hash table. */
142#define MAX_GRO_SKBS 8
143
5d38a079
HX
144/* This should be increased if a protocol with a bigger head is added. */
145#define GRO_MAX_HEAD (MAX_HEADER + 128)
146
1da177e4
LT
147/*
148 * The list of packet types we will receive (as opposed to discard)
149 * and the routines to invoke.
150 *
151 * Why 16. Because with 16 the only overlap we get on a hash of the
152 * low nibble of the protocol value is RARP/SNAP/X.25.
153 *
154 * NOTE: That is no longer true with the addition of VLAN tags. Not
155 * sure which should go first, but I bet it won't make much
156 * difference if we are running VLANs. The good news is that
157 * this protocol won't be in the list unless compiled in, so
3041a069 158 * the average user (w/out VLANs) will not be adversely affected.
1da177e4
LT
159 * --BLG
160 *
161 * 0800 IP
162 * 8100 802.1Q VLAN
163 * 0001 802.3
164 * 0002 AX.25
165 * 0004 802.2
166 * 8035 RARP
167 * 0005 SNAP
168 * 0805 X.25
169 * 0806 ARP
170 * 8137 IPX
171 * 0009 Localtalk
172 * 86DD IPv6
173 */
174
82d8a867
PE
175#define PTYPE_HASH_SIZE (16)
176#define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
177
1da177e4 178static DEFINE_SPINLOCK(ptype_lock);
62532da9 179static DEFINE_SPINLOCK(offload_lock);
82d8a867 180static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
6b2bedc3 181static struct list_head ptype_all __read_mostly; /* Taps */
62532da9 182static struct list_head offload_base __read_mostly;
1da177e4 183
1da177e4 184/*
7562f876 185 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
1da177e4
LT
186 * semaphore.
187 *
c6d14c84 188 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
1da177e4
LT
189 *
190 * Writers must hold the rtnl semaphore while they loop through the
7562f876 191 * dev_base_head list, and hold dev_base_lock for writing when they do the
1da177e4
LT
192 * actual updates. This allows pure readers to access the list even
193 * while a writer is preparing to update it.
194 *
195 * To put it another way, dev_base_lock is held for writing only to
196 * protect against pure readers; the rtnl semaphore provides the
197 * protection against other writers.
198 *
199 * See, for example usages, register_netdevice() and
200 * unregister_netdevice(), which must be called with the rtnl
201 * semaphore held.
202 */
1da177e4 203DEFINE_RWLOCK(dev_base_lock);
1da177e4
LT
204EXPORT_SYMBOL(dev_base_lock);
205
30e6c9fa 206seqcount_t devnet_rename_seq;
c91f6df2 207
4e985ada
TG
208static inline void dev_base_seq_inc(struct net *net)
209{
210 while (++net->dev_base_seq == 0);
211}
212
881d966b 213static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
1da177e4 214{
95c96174
ED
215 unsigned int hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
216
08e9897d 217 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
1da177e4
LT
218}
219
881d966b 220static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
1da177e4 221{
7c28bd0b 222 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
1da177e4
LT
223}
224
e36fa2f7 225static inline void rps_lock(struct softnet_data *sd)
152102c7
CG
226{
227#ifdef CONFIG_RPS
e36fa2f7 228 spin_lock(&sd->input_pkt_queue.lock);
152102c7
CG
229#endif
230}
231
e36fa2f7 232static inline void rps_unlock(struct softnet_data *sd)
152102c7
CG
233{
234#ifdef CONFIG_RPS
e36fa2f7 235 spin_unlock(&sd->input_pkt_queue.lock);
152102c7
CG
236#endif
237}
238
ce286d32
EB
239/* Device list insertion */
240static int list_netdevice(struct net_device *dev)
241{
c346dca1 242 struct net *net = dev_net(dev);
ce286d32
EB
243
244 ASSERT_RTNL();
245
246 write_lock_bh(&dev_base_lock);
c6d14c84 247 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
72c9528b 248 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
fb699dfd
ED
249 hlist_add_head_rcu(&dev->index_hlist,
250 dev_index_hash(net, dev->ifindex));
ce286d32 251 write_unlock_bh(&dev_base_lock);
4e985ada
TG
252
253 dev_base_seq_inc(net);
254
ce286d32
EB
255 return 0;
256}
257
fb699dfd
ED
258/* Device list removal
259 * caller must respect a RCU grace period before freeing/reusing dev
260 */
ce286d32
EB
261static void unlist_netdevice(struct net_device *dev)
262{
263 ASSERT_RTNL();
264
265 /* Unlink dev from the device chain */
266 write_lock_bh(&dev_base_lock);
c6d14c84 267 list_del_rcu(&dev->dev_list);
72c9528b 268 hlist_del_rcu(&dev->name_hlist);
fb699dfd 269 hlist_del_rcu(&dev->index_hlist);
ce286d32 270 write_unlock_bh(&dev_base_lock);
4e985ada
TG
271
272 dev_base_seq_inc(dev_net(dev));
ce286d32
EB
273}
274
1da177e4
LT
275/*
276 * Our notifier list
277 */
278
f07d5b94 279static RAW_NOTIFIER_HEAD(netdev_chain);
1da177e4
LT
280
281/*
282 * Device drivers call our routines to queue packets here. We empty the
283 * queue in the local softnet handler.
284 */
bea3348e 285
9958da05 286DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
d1b19dff 287EXPORT_PER_CPU_SYMBOL(softnet_data);
1da177e4 288
cf508b12 289#ifdef CONFIG_LOCKDEP
723e98b7 290/*
c773e847 291 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
723e98b7
JP
292 * according to dev->type
293 */
294static const unsigned short netdev_lock_type[] =
295 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
296 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
297 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
298 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
299 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
300 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
301 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
302 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
303 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
304 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
305 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
306 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
211ed865
PG
307 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
308 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
309 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
723e98b7 310
36cbd3dc 311static const char *const netdev_lock_name[] =
723e98b7
JP
312 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
313 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
314 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
315 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
316 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
317 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
318 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
319 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
320 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
321 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
322 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
323 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
211ed865
PG
324 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
325 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
326 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
723e98b7
JP
327
328static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
cf508b12 329static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
723e98b7
JP
330
331static inline unsigned short netdev_lock_pos(unsigned short dev_type)
332{
333 int i;
334
335 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
336 if (netdev_lock_type[i] == dev_type)
337 return i;
338 /* the last key is used by default */
339 return ARRAY_SIZE(netdev_lock_type) - 1;
340}
341
cf508b12
DM
342static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
343 unsigned short dev_type)
723e98b7
JP
344{
345 int i;
346
347 i = netdev_lock_pos(dev_type);
348 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
349 netdev_lock_name[i]);
350}
cf508b12
DM
351
352static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
353{
354 int i;
355
356 i = netdev_lock_pos(dev->type);
357 lockdep_set_class_and_name(&dev->addr_list_lock,
358 &netdev_addr_lock_key[i],
359 netdev_lock_name[i]);
360}
723e98b7 361#else
cf508b12
DM
362static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
363 unsigned short dev_type)
364{
365}
366static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
723e98b7
JP
367{
368}
369#endif
1da177e4
LT
370
371/*******************************************************************************
372
373 Protocol management and registration routines
374
375*******************************************************************************/
376
1da177e4
LT
377/*
378 * Add a protocol ID to the list. Now that the input handler is
379 * smarter we can dispense with all the messy stuff that used to be
380 * here.
381 *
382 * BEWARE!!! Protocol handlers, mangling input packets,
383 * MUST BE last in hash buckets and checking protocol handlers
384 * MUST start from promiscuous ptype_all chain in net_bh.
385 * It is true now, do not change it.
386 * Explanation follows: if protocol handler, mangling packet, will
387 * be the first on list, it is not able to sense, that packet
388 * is cloned and should be copied-on-write, so that it will
389 * change it and subsequent readers will get broken packet.
390 * --ANK (980803)
391 */
392
c07b68e8
ED
393static inline struct list_head *ptype_head(const struct packet_type *pt)
394{
395 if (pt->type == htons(ETH_P_ALL))
396 return &ptype_all;
397 else
398 return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
399}
400
1da177e4
LT
401/**
402 * dev_add_pack - add packet handler
403 * @pt: packet type declaration
404 *
405 * Add a protocol handler to the networking stack. The passed &packet_type
406 * is linked into kernel lists and may not be freed until it has been
407 * removed from the kernel lists.
408 *
4ec93edb 409 * This call does not sleep therefore it can not
1da177e4
LT
410 * guarantee all CPU's that are in middle of receiving packets
411 * will see the new packet type (until the next received packet).
412 */
413
414void dev_add_pack(struct packet_type *pt)
415{
c07b68e8 416 struct list_head *head = ptype_head(pt);
1da177e4 417
c07b68e8
ED
418 spin_lock(&ptype_lock);
419 list_add_rcu(&pt->list, head);
420 spin_unlock(&ptype_lock);
1da177e4 421}
d1b19dff 422EXPORT_SYMBOL(dev_add_pack);
1da177e4 423
1da177e4
LT
424/**
425 * __dev_remove_pack - remove packet handler
426 * @pt: packet type declaration
427 *
428 * Remove a protocol handler that was previously added to the kernel
429 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
430 * from the kernel lists and can be freed or reused once this function
4ec93edb 431 * returns.
1da177e4
LT
432 *
433 * The packet type might still be in use by receivers
434 * and must not be freed until after all the CPU's have gone
435 * through a quiescent state.
436 */
437void __dev_remove_pack(struct packet_type *pt)
438{
c07b68e8 439 struct list_head *head = ptype_head(pt);
1da177e4
LT
440 struct packet_type *pt1;
441
c07b68e8 442 spin_lock(&ptype_lock);
1da177e4
LT
443
444 list_for_each_entry(pt1, head, list) {
445 if (pt == pt1) {
446 list_del_rcu(&pt->list);
447 goto out;
448 }
449 }
450
7b6cd1ce 451 pr_warn("dev_remove_pack: %p not found\n", pt);
1da177e4 452out:
c07b68e8 453 spin_unlock(&ptype_lock);
1da177e4 454}
d1b19dff
ED
455EXPORT_SYMBOL(__dev_remove_pack);
456
1da177e4
LT
457/**
458 * dev_remove_pack - remove packet handler
459 * @pt: packet type declaration
460 *
461 * Remove a protocol handler that was previously added to the kernel
462 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
463 * from the kernel lists and can be freed or reused once this function
464 * returns.
465 *
466 * This call sleeps to guarantee that no CPU is looking at the packet
467 * type after return.
468 */
469void dev_remove_pack(struct packet_type *pt)
470{
471 __dev_remove_pack(pt);
4ec93edb 472
1da177e4
LT
473 synchronize_net();
474}
d1b19dff 475EXPORT_SYMBOL(dev_remove_pack);
1da177e4 476
62532da9
VY
477
478/**
479 * dev_add_offload - register offload handlers
480 * @po: protocol offload declaration
481 *
482 * Add protocol offload handlers to the networking stack. The passed
483 * &proto_offload is linked into kernel lists and may not be freed until
484 * it has been removed from the kernel lists.
485 *
486 * This call does not sleep therefore it can not
487 * guarantee all CPU's that are in middle of receiving packets
488 * will see the new offload handlers (until the next received packet).
489 */
490void dev_add_offload(struct packet_offload *po)
491{
492 struct list_head *head = &offload_base;
493
494 spin_lock(&offload_lock);
495 list_add_rcu(&po->list, head);
496 spin_unlock(&offload_lock);
497}
498EXPORT_SYMBOL(dev_add_offload);
499
500/**
501 * __dev_remove_offload - remove offload handler
502 * @po: packet offload declaration
503 *
504 * Remove a protocol offload handler that was previously added to the
505 * kernel offload handlers by dev_add_offload(). The passed &offload_type
506 * is removed from the kernel lists and can be freed or reused once this
507 * function returns.
508 *
509 * The packet type might still be in use by receivers
510 * and must not be freed until after all the CPU's have gone
511 * through a quiescent state.
512 */
513void __dev_remove_offload(struct packet_offload *po)
514{
515 struct list_head *head = &offload_base;
516 struct packet_offload *po1;
517
c53aa505 518 spin_lock(&offload_lock);
62532da9
VY
519
520 list_for_each_entry(po1, head, list) {
521 if (po == po1) {
522 list_del_rcu(&po->list);
523 goto out;
524 }
525 }
526
527 pr_warn("dev_remove_offload: %p not found\n", po);
528out:
c53aa505 529 spin_unlock(&offload_lock);
62532da9
VY
530}
531EXPORT_SYMBOL(__dev_remove_offload);
532
533/**
534 * dev_remove_offload - remove packet offload handler
535 * @po: packet offload declaration
536 *
537 * Remove a packet offload handler that was previously added to the kernel
538 * offload handlers by dev_add_offload(). The passed &offload_type is
539 * removed from the kernel lists and can be freed or reused once this
540 * function returns.
541 *
542 * This call sleeps to guarantee that no CPU is looking at the packet
543 * type after return.
544 */
545void dev_remove_offload(struct packet_offload *po)
546{
547 __dev_remove_offload(po);
548
549 synchronize_net();
550}
551EXPORT_SYMBOL(dev_remove_offload);
552
1da177e4
LT
553/******************************************************************************
554
555 Device Boot-time Settings Routines
556
557*******************************************************************************/
558
559/* Boot time configuration table */
560static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
561
562/**
563 * netdev_boot_setup_add - add new setup entry
564 * @name: name of the device
565 * @map: configured settings for the device
566 *
567 * Adds new setup entry to the dev_boot_setup list. The function
568 * returns 0 on error and 1 on success. This is a generic routine to
569 * all netdevices.
570 */
571static int netdev_boot_setup_add(char *name, struct ifmap *map)
572{
573 struct netdev_boot_setup *s;
574 int i;
575
576 s = dev_boot_setup;
577 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
578 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
579 memset(s[i].name, 0, sizeof(s[i].name));
93b3cff9 580 strlcpy(s[i].name, name, IFNAMSIZ);
1da177e4
LT
581 memcpy(&s[i].map, map, sizeof(s[i].map));
582 break;
583 }
584 }
585
586 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
587}
588
589/**
590 * netdev_boot_setup_check - check boot time settings
591 * @dev: the netdevice
592 *
593 * Check boot time settings for the device.
594 * The found settings are set for the device to be used
595 * later in the device probing.
596 * Returns 0 if no settings found, 1 if they are.
597 */
598int netdev_boot_setup_check(struct net_device *dev)
599{
600 struct netdev_boot_setup *s = dev_boot_setup;
601 int i;
602
603 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
604 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
93b3cff9 605 !strcmp(dev->name, s[i].name)) {
1da177e4
LT
606 dev->irq = s[i].map.irq;
607 dev->base_addr = s[i].map.base_addr;
608 dev->mem_start = s[i].map.mem_start;
609 dev->mem_end = s[i].map.mem_end;
610 return 1;
611 }
612 }
613 return 0;
614}
d1b19dff 615EXPORT_SYMBOL(netdev_boot_setup_check);
1da177e4
LT
616
617
618/**
619 * netdev_boot_base - get address from boot time settings
620 * @prefix: prefix for network device
621 * @unit: id for network device
622 *
623 * Check boot time settings for the base address of device.
624 * The found settings are set for the device to be used
625 * later in the device probing.
626 * Returns 0 if no settings found.
627 */
628unsigned long netdev_boot_base(const char *prefix, int unit)
629{
630 const struct netdev_boot_setup *s = dev_boot_setup;
631 char name[IFNAMSIZ];
632 int i;
633
634 sprintf(name, "%s%d", prefix, unit);
635
636 /*
637 * If device already registered then return base of 1
638 * to indicate not to probe for this interface
639 */
881d966b 640 if (__dev_get_by_name(&init_net, name))
1da177e4
LT
641 return 1;
642
643 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
644 if (!strcmp(name, s[i].name))
645 return s[i].map.base_addr;
646 return 0;
647}
648
649/*
650 * Saves at boot time configured settings for any netdevice.
651 */
652int __init netdev_boot_setup(char *str)
653{
654 int ints[5];
655 struct ifmap map;
656
657 str = get_options(str, ARRAY_SIZE(ints), ints);
658 if (!str || !*str)
659 return 0;
660
661 /* Save settings */
662 memset(&map, 0, sizeof(map));
663 if (ints[0] > 0)
664 map.irq = ints[1];
665 if (ints[0] > 1)
666 map.base_addr = ints[2];
667 if (ints[0] > 2)
668 map.mem_start = ints[3];
669 if (ints[0] > 3)
670 map.mem_end = ints[4];
671
672 /* Add new entry to the list */
673 return netdev_boot_setup_add(str, &map);
674}
675
676__setup("netdev=", netdev_boot_setup);
677
678/*******************************************************************************
679
680 Device Interface Subroutines
681
682*******************************************************************************/
683
684/**
685 * __dev_get_by_name - find a device by its name
c4ea43c5 686 * @net: the applicable net namespace
1da177e4
LT
687 * @name: name to find
688 *
689 * Find an interface by name. Must be called under RTNL semaphore
690 * or @dev_base_lock. If the name is found a pointer to the device
691 * is returned. If the name is not found then %NULL is returned. The
692 * reference counters are not incremented so the caller must be
693 * careful with locks.
694 */
695
881d966b 696struct net_device *__dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
697{
698 struct hlist_node *p;
0bd8d536
ED
699 struct net_device *dev;
700 struct hlist_head *head = dev_name_hash(net, name);
1da177e4 701
0bd8d536 702 hlist_for_each_entry(dev, p, head, name_hlist)
1da177e4
LT
703 if (!strncmp(dev->name, name, IFNAMSIZ))
704 return dev;
0bd8d536 705
1da177e4
LT
706 return NULL;
707}
d1b19dff 708EXPORT_SYMBOL(__dev_get_by_name);
1da177e4 709
72c9528b
ED
710/**
711 * dev_get_by_name_rcu - find a device by its name
712 * @net: the applicable net namespace
713 * @name: name to find
714 *
715 * Find an interface by name.
716 * If the name is found a pointer to the device is returned.
717 * If the name is not found then %NULL is returned.
718 * The reference counters are not incremented so the caller must be
719 * careful with locks. The caller must hold RCU lock.
720 */
721
722struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
723{
724 struct hlist_node *p;
725 struct net_device *dev;
726 struct hlist_head *head = dev_name_hash(net, name);
727
728 hlist_for_each_entry_rcu(dev, p, head, name_hlist)
729 if (!strncmp(dev->name, name, IFNAMSIZ))
730 return dev;
731
732 return NULL;
733}
734EXPORT_SYMBOL(dev_get_by_name_rcu);
735
1da177e4
LT
736/**
737 * dev_get_by_name - find a device by its name
c4ea43c5 738 * @net: the applicable net namespace
1da177e4
LT
739 * @name: name to find
740 *
741 * Find an interface by name. This can be called from any
742 * context and does its own locking. The returned handle has
743 * the usage count incremented and the caller must use dev_put() to
744 * release it when it is no longer needed. %NULL is returned if no
745 * matching device is found.
746 */
747
881d966b 748struct net_device *dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
749{
750 struct net_device *dev;
751
72c9528b
ED
752 rcu_read_lock();
753 dev = dev_get_by_name_rcu(net, name);
1da177e4
LT
754 if (dev)
755 dev_hold(dev);
72c9528b 756 rcu_read_unlock();
1da177e4
LT
757 return dev;
758}
d1b19dff 759EXPORT_SYMBOL(dev_get_by_name);
1da177e4
LT
760
761/**
762 * __dev_get_by_index - find a device by its ifindex
c4ea43c5 763 * @net: the applicable net namespace
1da177e4
LT
764 * @ifindex: index of device
765 *
766 * Search for an interface by index. Returns %NULL if the device
767 * is not found or a pointer to the device. The device has not
768 * had its reference counter increased so the caller must be careful
769 * about locking. The caller must hold either the RTNL semaphore
770 * or @dev_base_lock.
771 */
772
881d966b 773struct net_device *__dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
774{
775 struct hlist_node *p;
0bd8d536
ED
776 struct net_device *dev;
777 struct hlist_head *head = dev_index_hash(net, ifindex);
1da177e4 778
0bd8d536 779 hlist_for_each_entry(dev, p, head, index_hlist)
1da177e4
LT
780 if (dev->ifindex == ifindex)
781 return dev;
0bd8d536 782
1da177e4
LT
783 return NULL;
784}
d1b19dff 785EXPORT_SYMBOL(__dev_get_by_index);
1da177e4 786
fb699dfd
ED
787/**
788 * dev_get_by_index_rcu - find a device by its ifindex
789 * @net: the applicable net namespace
790 * @ifindex: index of device
791 *
792 * Search for an interface by index. Returns %NULL if the device
793 * is not found or a pointer to the device. The device has not
794 * had its reference counter increased so the caller must be careful
795 * about locking. The caller must hold RCU lock.
796 */
797
798struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
799{
800 struct hlist_node *p;
801 struct net_device *dev;
802 struct hlist_head *head = dev_index_hash(net, ifindex);
803
804 hlist_for_each_entry_rcu(dev, p, head, index_hlist)
805 if (dev->ifindex == ifindex)
806 return dev;
807
808 return NULL;
809}
810EXPORT_SYMBOL(dev_get_by_index_rcu);
811
1da177e4
LT
812
813/**
814 * dev_get_by_index - find a device by its ifindex
c4ea43c5 815 * @net: the applicable net namespace
1da177e4
LT
816 * @ifindex: index of device
817 *
818 * Search for an interface by index. Returns NULL if the device
819 * is not found or a pointer to the device. The device returned has
820 * had a reference added and the pointer is safe until the user calls
821 * dev_put to indicate they have finished with it.
822 */
823
881d966b 824struct net_device *dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
825{
826 struct net_device *dev;
827
fb699dfd
ED
828 rcu_read_lock();
829 dev = dev_get_by_index_rcu(net, ifindex);
1da177e4
LT
830 if (dev)
831 dev_hold(dev);
fb699dfd 832 rcu_read_unlock();
1da177e4
LT
833 return dev;
834}
d1b19dff 835EXPORT_SYMBOL(dev_get_by_index);
1da177e4
LT
836
837/**
941666c2 838 * dev_getbyhwaddr_rcu - find a device by its hardware address
c4ea43c5 839 * @net: the applicable net namespace
1da177e4
LT
840 * @type: media type of device
841 * @ha: hardware address
842 *
843 * Search for an interface by MAC address. Returns NULL if the device
c506653d
ED
844 * is not found or a pointer to the device.
845 * The caller must hold RCU or RTNL.
941666c2 846 * The returned device has not had its ref count increased
1da177e4
LT
847 * and the caller must therefore be careful about locking
848 *
1da177e4
LT
849 */
850
941666c2
ED
851struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
852 const char *ha)
1da177e4
LT
853{
854 struct net_device *dev;
855
941666c2 856 for_each_netdev_rcu(net, dev)
1da177e4
LT
857 if (dev->type == type &&
858 !memcmp(dev->dev_addr, ha, dev->addr_len))
7562f876
PE
859 return dev;
860
861 return NULL;
1da177e4 862}
941666c2 863EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
cf309e3f 864
881d966b 865struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
1da177e4
LT
866{
867 struct net_device *dev;
868
4e9cac2b 869 ASSERT_RTNL();
881d966b 870 for_each_netdev(net, dev)
4e9cac2b 871 if (dev->type == type)
7562f876
PE
872 return dev;
873
874 return NULL;
4e9cac2b 875}
4e9cac2b
PM
876EXPORT_SYMBOL(__dev_getfirstbyhwtype);
877
881d966b 878struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
4e9cac2b 879{
99fe3c39 880 struct net_device *dev, *ret = NULL;
4e9cac2b 881
99fe3c39
ED
882 rcu_read_lock();
883 for_each_netdev_rcu(net, dev)
884 if (dev->type == type) {
885 dev_hold(dev);
886 ret = dev;
887 break;
888 }
889 rcu_read_unlock();
890 return ret;
1da177e4 891}
1da177e4
LT
892EXPORT_SYMBOL(dev_getfirstbyhwtype);
893
894/**
bb69ae04 895 * dev_get_by_flags_rcu - find any device with given flags
c4ea43c5 896 * @net: the applicable net namespace
1da177e4
LT
897 * @if_flags: IFF_* values
898 * @mask: bitmask of bits in if_flags to check
899 *
900 * Search for any interface with the given flags. Returns NULL if a device
bb69ae04
ED
901 * is not found or a pointer to the device. Must be called inside
902 * rcu_read_lock(), and result refcount is unchanged.
1da177e4
LT
903 */
904
bb69ae04 905struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
d1b19dff 906 unsigned short mask)
1da177e4 907{
7562f876 908 struct net_device *dev, *ret;
1da177e4 909
7562f876 910 ret = NULL;
c6d14c84 911 for_each_netdev_rcu(net, dev) {
1da177e4 912 if (((dev->flags ^ if_flags) & mask) == 0) {
7562f876 913 ret = dev;
1da177e4
LT
914 break;
915 }
916 }
7562f876 917 return ret;
1da177e4 918}
bb69ae04 919EXPORT_SYMBOL(dev_get_by_flags_rcu);
1da177e4
LT
920
921/**
922 * dev_valid_name - check if name is okay for network device
923 * @name: name string
924 *
925 * Network device names need to be valid file names to
c7fa9d18
DM
926 * to allow sysfs to work. We also disallow any kind of
927 * whitespace.
1da177e4 928 */
95f050bf 929bool dev_valid_name(const char *name)
1da177e4 930{
c7fa9d18 931 if (*name == '\0')
95f050bf 932 return false;
b6fe17d6 933 if (strlen(name) >= IFNAMSIZ)
95f050bf 934 return false;
c7fa9d18 935 if (!strcmp(name, ".") || !strcmp(name, ".."))
95f050bf 936 return false;
c7fa9d18
DM
937
938 while (*name) {
939 if (*name == '/' || isspace(*name))
95f050bf 940 return false;
c7fa9d18
DM
941 name++;
942 }
95f050bf 943 return true;
1da177e4 944}
d1b19dff 945EXPORT_SYMBOL(dev_valid_name);
1da177e4
LT
946
947/**
b267b179
EB
948 * __dev_alloc_name - allocate a name for a device
949 * @net: network namespace to allocate the device name in
1da177e4 950 * @name: name format string
b267b179 951 * @buf: scratch buffer and result name string
1da177e4
LT
952 *
953 * Passed a format string - eg "lt%d" it will try and find a suitable
3041a069
SH
954 * id. It scans list of devices to build up a free map, then chooses
955 * the first empty slot. The caller must hold the dev_base or rtnl lock
956 * while allocating the name and adding the device in order to avoid
957 * duplicates.
958 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
959 * Returns the number of the unit assigned or a negative errno code.
1da177e4
LT
960 */
961
b267b179 962static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1da177e4
LT
963{
964 int i = 0;
1da177e4
LT
965 const char *p;
966 const int max_netdevices = 8*PAGE_SIZE;
cfcabdcc 967 unsigned long *inuse;
1da177e4
LT
968 struct net_device *d;
969
970 p = strnchr(name, IFNAMSIZ-1, '%');
971 if (p) {
972 /*
973 * Verify the string as this thing may have come from
974 * the user. There must be either one "%d" and no other "%"
975 * characters.
976 */
977 if (p[1] != 'd' || strchr(p + 2, '%'))
978 return -EINVAL;
979
980 /* Use one page as a bit array of possible slots */
cfcabdcc 981 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1da177e4
LT
982 if (!inuse)
983 return -ENOMEM;
984
881d966b 985 for_each_netdev(net, d) {
1da177e4
LT
986 if (!sscanf(d->name, name, &i))
987 continue;
988 if (i < 0 || i >= max_netdevices)
989 continue;
990
991 /* avoid cases where sscanf is not exact inverse of printf */
b267b179 992 snprintf(buf, IFNAMSIZ, name, i);
1da177e4
LT
993 if (!strncmp(buf, d->name, IFNAMSIZ))
994 set_bit(i, inuse);
995 }
996
997 i = find_first_zero_bit(inuse, max_netdevices);
998 free_page((unsigned long) inuse);
999 }
1000
d9031024
OP
1001 if (buf != name)
1002 snprintf(buf, IFNAMSIZ, name, i);
b267b179 1003 if (!__dev_get_by_name(net, buf))
1da177e4 1004 return i;
1da177e4
LT
1005
1006 /* It is possible to run out of possible slots
1007 * when the name is long and there isn't enough space left
1008 * for the digits, or if all bits are used.
1009 */
1010 return -ENFILE;
1011}
1012
b267b179
EB
1013/**
1014 * dev_alloc_name - allocate a name for a device
1015 * @dev: device
1016 * @name: name format string
1017 *
1018 * Passed a format string - eg "lt%d" it will try and find a suitable
1019 * id. It scans list of devices to build up a free map, then chooses
1020 * the first empty slot. The caller must hold the dev_base or rtnl lock
1021 * while allocating the name and adding the device in order to avoid
1022 * duplicates.
1023 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1024 * Returns the number of the unit assigned or a negative errno code.
1025 */
1026
1027int dev_alloc_name(struct net_device *dev, const char *name)
1028{
1029 char buf[IFNAMSIZ];
1030 struct net *net;
1031 int ret;
1032
c346dca1
YH
1033 BUG_ON(!dev_net(dev));
1034 net = dev_net(dev);
b267b179
EB
1035 ret = __dev_alloc_name(net, name, buf);
1036 if (ret >= 0)
1037 strlcpy(dev->name, buf, IFNAMSIZ);
1038 return ret;
1039}
d1b19dff 1040EXPORT_SYMBOL(dev_alloc_name);
b267b179 1041
828de4f6
G
1042static int dev_alloc_name_ns(struct net *net,
1043 struct net_device *dev,
1044 const char *name)
d9031024 1045{
828de4f6
G
1046 char buf[IFNAMSIZ];
1047 int ret;
8ce6cebc 1048
828de4f6
G
1049 ret = __dev_alloc_name(net, name, buf);
1050 if (ret >= 0)
1051 strlcpy(dev->name, buf, IFNAMSIZ);
1052 return ret;
1053}
1054
1055static int dev_get_valid_name(struct net *net,
1056 struct net_device *dev,
1057 const char *name)
1058{
1059 BUG_ON(!net);
8ce6cebc 1060
d9031024
OP
1061 if (!dev_valid_name(name))
1062 return -EINVAL;
1063
1c5cae81 1064 if (strchr(name, '%'))
828de4f6 1065 return dev_alloc_name_ns(net, dev, name);
d9031024
OP
1066 else if (__dev_get_by_name(net, name))
1067 return -EEXIST;
8ce6cebc
DL
1068 else if (dev->name != name)
1069 strlcpy(dev->name, name, IFNAMSIZ);
d9031024
OP
1070
1071 return 0;
1072}
1da177e4
LT
1073
1074/**
1075 * dev_change_name - change name of a device
1076 * @dev: device
1077 * @newname: name (or format string) must be at least IFNAMSIZ
1078 *
1079 * Change name of a device, can pass format strings "eth%d".
1080 * for wildcarding.
1081 */
cf04a4c7 1082int dev_change_name(struct net_device *dev, const char *newname)
1da177e4 1083{
fcc5a03a 1084 char oldname[IFNAMSIZ];
1da177e4 1085 int err = 0;
fcc5a03a 1086 int ret;
881d966b 1087 struct net *net;
1da177e4
LT
1088
1089 ASSERT_RTNL();
c346dca1 1090 BUG_ON(!dev_net(dev));
1da177e4 1091
c346dca1 1092 net = dev_net(dev);
1da177e4
LT
1093 if (dev->flags & IFF_UP)
1094 return -EBUSY;
1095
30e6c9fa 1096 write_seqcount_begin(&devnet_rename_seq);
c91f6df2
BH
1097
1098 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
30e6c9fa 1099 write_seqcount_end(&devnet_rename_seq);
c8d90dca 1100 return 0;
c91f6df2 1101 }
c8d90dca 1102
fcc5a03a
HX
1103 memcpy(oldname, dev->name, IFNAMSIZ);
1104
828de4f6 1105 err = dev_get_valid_name(net, dev, newname);
c91f6df2 1106 if (err < 0) {
30e6c9fa 1107 write_seqcount_end(&devnet_rename_seq);
d9031024 1108 return err;
c91f6df2 1109 }
1da177e4 1110
fcc5a03a 1111rollback:
a1b3f594
EB
1112 ret = device_rename(&dev->dev, dev->name);
1113 if (ret) {
1114 memcpy(dev->name, oldname, IFNAMSIZ);
30e6c9fa 1115 write_seqcount_end(&devnet_rename_seq);
a1b3f594 1116 return ret;
dcc99773 1117 }
7f988eab 1118
30e6c9fa 1119 write_seqcount_end(&devnet_rename_seq);
c91f6df2 1120
7f988eab 1121 write_lock_bh(&dev_base_lock);
372b2312 1122 hlist_del_rcu(&dev->name_hlist);
72c9528b
ED
1123 write_unlock_bh(&dev_base_lock);
1124
1125 synchronize_rcu();
1126
1127 write_lock_bh(&dev_base_lock);
1128 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
7f988eab
HX
1129 write_unlock_bh(&dev_base_lock);
1130
056925ab 1131 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
fcc5a03a
HX
1132 ret = notifier_to_errno(ret);
1133
1134 if (ret) {
91e9c07b
ED
1135 /* err >= 0 after dev_alloc_name() or stores the first errno */
1136 if (err >= 0) {
fcc5a03a 1137 err = ret;
30e6c9fa 1138 write_seqcount_begin(&devnet_rename_seq);
fcc5a03a
HX
1139 memcpy(dev->name, oldname, IFNAMSIZ);
1140 goto rollback;
91e9c07b 1141 } else {
7b6cd1ce 1142 pr_err("%s: name change rollback failed: %d\n",
91e9c07b 1143 dev->name, ret);
fcc5a03a
HX
1144 }
1145 }
1da177e4
LT
1146
1147 return err;
1148}
1149
0b815a1a
SH
1150/**
1151 * dev_set_alias - change ifalias of a device
1152 * @dev: device
1153 * @alias: name up to IFALIASZ
f0db275a 1154 * @len: limit of bytes to copy from info
0b815a1a
SH
1155 *
1156 * Set ifalias for a device,
1157 */
1158int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1159{
7364e445
AK
1160 char *new_ifalias;
1161
0b815a1a
SH
1162 ASSERT_RTNL();
1163
1164 if (len >= IFALIASZ)
1165 return -EINVAL;
1166
96ca4a2c 1167 if (!len) {
388dfc2d
SK
1168 kfree(dev->ifalias);
1169 dev->ifalias = NULL;
96ca4a2c
OH
1170 return 0;
1171 }
1172
7364e445
AK
1173 new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
1174 if (!new_ifalias)
0b815a1a 1175 return -ENOMEM;
7364e445 1176 dev->ifalias = new_ifalias;
0b815a1a
SH
1177
1178 strlcpy(dev->ifalias, alias, len+1);
1179 return len;
1180}
1181
1182
d8a33ac4 1183/**
3041a069 1184 * netdev_features_change - device changes features
d8a33ac4
SH
1185 * @dev: device to cause notification
1186 *
1187 * Called to indicate a device has changed features.
1188 */
1189void netdev_features_change(struct net_device *dev)
1190{
056925ab 1191 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
d8a33ac4
SH
1192}
1193EXPORT_SYMBOL(netdev_features_change);
1194
1da177e4
LT
1195/**
1196 * netdev_state_change - device changes state
1197 * @dev: device to cause notification
1198 *
1199 * Called to indicate a device has changed state. This function calls
1200 * the notifier chains for netdev_chain and sends a NEWLINK message
1201 * to the routing socket.
1202 */
1203void netdev_state_change(struct net_device *dev)
1204{
1205 if (dev->flags & IFF_UP) {
056925ab 1206 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1da177e4
LT
1207 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1208 }
1209}
d1b19dff 1210EXPORT_SYMBOL(netdev_state_change);
1da177e4 1211
ee89bab1
AW
1212/**
1213 * netdev_notify_peers - notify network peers about existence of @dev
1214 * @dev: network device
1215 *
1216 * Generate traffic such that interested network peers are aware of
1217 * @dev, such as by generating a gratuitous ARP. This may be used when
1218 * a device wants to inform the rest of the network about some sort of
1219 * reconfiguration such as a failover event or virtual machine
1220 * migration.
1221 */
1222void netdev_notify_peers(struct net_device *dev)
c1da4ac7 1223{
ee89bab1
AW
1224 rtnl_lock();
1225 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1226 rtnl_unlock();
c1da4ac7 1227}
ee89bab1 1228EXPORT_SYMBOL(netdev_notify_peers);
c1da4ac7 1229
1da177e4
LT
1230/**
1231 * dev_load - load a network module
c4ea43c5 1232 * @net: the applicable net namespace
1da177e4
LT
1233 * @name: name of interface
1234 *
1235 * If a network interface is not present and the process has suitable
1236 * privileges this function loads the module. If module loading is not
1237 * available in this kernel then it becomes a nop.
1238 */
1239
881d966b 1240void dev_load(struct net *net, const char *name)
1da177e4 1241{
4ec93edb 1242 struct net_device *dev;
8909c9ad 1243 int no_module;
1da177e4 1244
72c9528b
ED
1245 rcu_read_lock();
1246 dev = dev_get_by_name_rcu(net, name);
1247 rcu_read_unlock();
1da177e4 1248
8909c9ad
VK
1249 no_module = !dev;
1250 if (no_module && capable(CAP_NET_ADMIN))
1251 no_module = request_module("netdev-%s", name);
1252 if (no_module && capable(CAP_SYS_MODULE)) {
1253 if (!request_module("%s", name))
7cecb523
VL
1254 pr_warn("Loading kernel module for a network device with CAP_SYS_MODULE (deprecated). Use CAP_NET_ADMIN and alias netdev-%s instead.\n",
1255 name);
8909c9ad 1256 }
1da177e4 1257}
d1b19dff 1258EXPORT_SYMBOL(dev_load);
1da177e4 1259
bd380811 1260static int __dev_open(struct net_device *dev)
1da177e4 1261{
d314774c 1262 const struct net_device_ops *ops = dev->netdev_ops;
3b8bcfd5 1263 int ret;
1da177e4 1264
e46b66bc
BH
1265 ASSERT_RTNL();
1266
1da177e4
LT
1267 if (!netif_device_present(dev))
1268 return -ENODEV;
1269
3b8bcfd5
JB
1270 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1271 ret = notifier_to_errno(ret);
1272 if (ret)
1273 return ret;
1274
1da177e4 1275 set_bit(__LINK_STATE_START, &dev->state);
bada339b 1276
d314774c
SH
1277 if (ops->ndo_validate_addr)
1278 ret = ops->ndo_validate_addr(dev);
bada339b 1279
d314774c
SH
1280 if (!ret && ops->ndo_open)
1281 ret = ops->ndo_open(dev);
1da177e4 1282
bada339b
JG
1283 if (ret)
1284 clear_bit(__LINK_STATE_START, &dev->state);
1285 else {
1da177e4 1286 dev->flags |= IFF_UP;
b4bd07c2 1287 net_dmaengine_get();
4417da66 1288 dev_set_rx_mode(dev);
1da177e4 1289 dev_activate(dev);
7bf23575 1290 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 1291 }
bada339b 1292
1da177e4
LT
1293 return ret;
1294}
1295
1296/**
bd380811
PM
1297 * dev_open - prepare an interface for use.
1298 * @dev: device to open
1da177e4 1299 *
bd380811
PM
1300 * Takes a device from down to up state. The device's private open
1301 * function is invoked and then the multicast lists are loaded. Finally
1302 * the device is moved into the up state and a %NETDEV_UP message is
1303 * sent to the netdev notifier chain.
1304 *
1305 * Calling this function on an active interface is a nop. On a failure
1306 * a negative errno code is returned.
1da177e4 1307 */
bd380811
PM
1308int dev_open(struct net_device *dev)
1309{
1310 int ret;
1311
bd380811
PM
1312 if (dev->flags & IFF_UP)
1313 return 0;
1314
bd380811
PM
1315 ret = __dev_open(dev);
1316 if (ret < 0)
1317 return ret;
1318
bd380811
PM
1319 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1320 call_netdevice_notifiers(NETDEV_UP, dev);
1321
1322 return ret;
1323}
1324EXPORT_SYMBOL(dev_open);
1325
44345724 1326static int __dev_close_many(struct list_head *head)
1da177e4 1327{
44345724 1328 struct net_device *dev;
e46b66bc 1329
bd380811 1330 ASSERT_RTNL();
9d5010db
DM
1331 might_sleep();
1332
44345724 1333 list_for_each_entry(dev, head, unreg_list) {
44345724 1334 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1da177e4 1335
44345724 1336 clear_bit(__LINK_STATE_START, &dev->state);
1da177e4 1337
44345724
OP
1338 /* Synchronize to scheduled poll. We cannot touch poll list, it
1339 * can be even on different cpu. So just clear netif_running().
1340 *
1341 * dev->stop() will invoke napi_disable() on all of it's
1342 * napi_struct instances on this device.
1343 */
1344 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1345 }
1da177e4 1346
44345724 1347 dev_deactivate_many(head);
d8b2a4d2 1348
44345724
OP
1349 list_for_each_entry(dev, head, unreg_list) {
1350 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4 1351
44345724
OP
1352 /*
1353 * Call the device specific close. This cannot fail.
1354 * Only if device is UP
1355 *
1356 * We allow it to be called even after a DETACH hot-plug
1357 * event.
1358 */
1359 if (ops->ndo_stop)
1360 ops->ndo_stop(dev);
1361
44345724 1362 dev->flags &= ~IFF_UP;
44345724
OP
1363 net_dmaengine_put();
1364 }
1365
1366 return 0;
1367}
1368
1369static int __dev_close(struct net_device *dev)
1370{
f87e6f47 1371 int retval;
44345724
OP
1372 LIST_HEAD(single);
1373
1374 list_add(&dev->unreg_list, &single);
f87e6f47
LT
1375 retval = __dev_close_many(&single);
1376 list_del(&single);
1377 return retval;
44345724
OP
1378}
1379
3fbd8758 1380static int dev_close_many(struct list_head *head)
44345724
OP
1381{
1382 struct net_device *dev, *tmp;
1383 LIST_HEAD(tmp_list);
1da177e4 1384
44345724
OP
1385 list_for_each_entry_safe(dev, tmp, head, unreg_list)
1386 if (!(dev->flags & IFF_UP))
1387 list_move(&dev->unreg_list, &tmp_list);
1388
1389 __dev_close_many(head);
1da177e4 1390
44345724
OP
1391 list_for_each_entry(dev, head, unreg_list) {
1392 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1393 call_netdevice_notifiers(NETDEV_DOWN, dev);
1394 }
bd380811 1395
44345724
OP
1396 /* rollback_registered_many needs the complete original list */
1397 list_splice(&tmp_list, head);
bd380811
PM
1398 return 0;
1399}
1400
1401/**
1402 * dev_close - shutdown an interface.
1403 * @dev: device to shutdown
1404 *
1405 * This function moves an active device into down state. A
1406 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1407 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1408 * chain.
1409 */
1410int dev_close(struct net_device *dev)
1411{
e14a5993
ED
1412 if (dev->flags & IFF_UP) {
1413 LIST_HEAD(single);
1da177e4 1414
e14a5993
ED
1415 list_add(&dev->unreg_list, &single);
1416 dev_close_many(&single);
1417 list_del(&single);
1418 }
1da177e4
LT
1419 return 0;
1420}
d1b19dff 1421EXPORT_SYMBOL(dev_close);
1da177e4
LT
1422
1423
0187bdfb
BH
1424/**
1425 * dev_disable_lro - disable Large Receive Offload on a device
1426 * @dev: device
1427 *
1428 * Disable Large Receive Offload (LRO) on a net device. Must be
1429 * called under RTNL. This is needed if received packets may be
1430 * forwarded to another interface.
1431 */
1432void dev_disable_lro(struct net_device *dev)
1433{
f11970e3
NH
1434 /*
1435 * If we're trying to disable lro on a vlan device
1436 * use the underlying physical device instead
1437 */
1438 if (is_vlan_dev(dev))
1439 dev = vlan_dev_real_dev(dev);
1440
bc5787c6
MM
1441 dev->wanted_features &= ~NETIF_F_LRO;
1442 netdev_update_features(dev);
27660515 1443
22d5969f
MM
1444 if (unlikely(dev->features & NETIF_F_LRO))
1445 netdev_WARN(dev, "failed to disable LRO!\n");
0187bdfb
BH
1446}
1447EXPORT_SYMBOL(dev_disable_lro);
1448
1449
881d966b
EB
1450static int dev_boot_phase = 1;
1451
1da177e4
LT
1452/**
1453 * register_netdevice_notifier - register a network notifier block
1454 * @nb: notifier
1455 *
1456 * Register a notifier to be called when network device events occur.
1457 * The notifier passed is linked into the kernel structures and must
1458 * not be reused until it has been unregistered. A negative errno code
1459 * is returned on a failure.
1460 *
1461 * When registered all registration and up events are replayed
4ec93edb 1462 * to the new notifier to allow device to have a race free
1da177e4
LT
1463 * view of the network device list.
1464 */
1465
1466int register_netdevice_notifier(struct notifier_block *nb)
1467{
1468 struct net_device *dev;
fcc5a03a 1469 struct net_device *last;
881d966b 1470 struct net *net;
1da177e4
LT
1471 int err;
1472
1473 rtnl_lock();
f07d5b94 1474 err = raw_notifier_chain_register(&netdev_chain, nb);
fcc5a03a
HX
1475 if (err)
1476 goto unlock;
881d966b
EB
1477 if (dev_boot_phase)
1478 goto unlock;
1479 for_each_net(net) {
1480 for_each_netdev(net, dev) {
1481 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1482 err = notifier_to_errno(err);
1483 if (err)
1484 goto rollback;
1485
1486 if (!(dev->flags & IFF_UP))
1487 continue;
1da177e4 1488
881d966b
EB
1489 nb->notifier_call(nb, NETDEV_UP, dev);
1490 }
1da177e4 1491 }
fcc5a03a
HX
1492
1493unlock:
1da177e4
LT
1494 rtnl_unlock();
1495 return err;
fcc5a03a
HX
1496
1497rollback:
1498 last = dev;
881d966b
EB
1499 for_each_net(net) {
1500 for_each_netdev(net, dev) {
1501 if (dev == last)
8f891489 1502 goto outroll;
fcc5a03a 1503
881d966b
EB
1504 if (dev->flags & IFF_UP) {
1505 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1506 nb->notifier_call(nb, NETDEV_DOWN, dev);
1507 }
1508 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
fcc5a03a 1509 }
fcc5a03a 1510 }
c67625a1 1511
8f891489 1512outroll:
c67625a1 1513 raw_notifier_chain_unregister(&netdev_chain, nb);
fcc5a03a 1514 goto unlock;
1da177e4 1515}
d1b19dff 1516EXPORT_SYMBOL(register_netdevice_notifier);
1da177e4
LT
1517
1518/**
1519 * unregister_netdevice_notifier - unregister a network notifier block
1520 * @nb: notifier
1521 *
1522 * Unregister a notifier previously registered by
1523 * register_netdevice_notifier(). The notifier is unlinked into the
1524 * kernel structures and may then be reused. A negative errno code
1525 * is returned on a failure.
7d3d43da
EB
1526 *
1527 * After unregistering unregister and down device events are synthesized
1528 * for all devices on the device list to the removed notifier to remove
1529 * the need for special case cleanup code.
1da177e4
LT
1530 */
1531
1532int unregister_netdevice_notifier(struct notifier_block *nb)
1533{
7d3d43da
EB
1534 struct net_device *dev;
1535 struct net *net;
9f514950
HX
1536 int err;
1537
1538 rtnl_lock();
f07d5b94 1539 err = raw_notifier_chain_unregister(&netdev_chain, nb);
7d3d43da
EB
1540 if (err)
1541 goto unlock;
1542
1543 for_each_net(net) {
1544 for_each_netdev(net, dev) {
1545 if (dev->flags & IFF_UP) {
1546 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1547 nb->notifier_call(nb, NETDEV_DOWN, dev);
1548 }
1549 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
7d3d43da
EB
1550 }
1551 }
1552unlock:
9f514950
HX
1553 rtnl_unlock();
1554 return err;
1da177e4 1555}
d1b19dff 1556EXPORT_SYMBOL(unregister_netdevice_notifier);
1da177e4
LT
1557
1558/**
1559 * call_netdevice_notifiers - call all network notifier blocks
1560 * @val: value passed unmodified to notifier function
c4ea43c5 1561 * @dev: net_device pointer passed unmodified to notifier function
1da177e4
LT
1562 *
1563 * Call all network notifier blocks. Parameters and return value
f07d5b94 1564 * are as for raw_notifier_call_chain().
1da177e4
LT
1565 */
1566
ad7379d4 1567int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1da177e4 1568{
ab930471 1569 ASSERT_RTNL();
ad7379d4 1570 return raw_notifier_call_chain(&netdev_chain, val, dev);
1da177e4 1571}
edf947f1 1572EXPORT_SYMBOL(call_netdevice_notifiers);
1da177e4 1573
c5905afb 1574static struct static_key netstamp_needed __read_mostly;
b90e5794 1575#ifdef HAVE_JUMP_LABEL
c5905afb 1576/* We are not allowed to call static_key_slow_dec() from irq context
b90e5794 1577 * If net_disable_timestamp() is called from irq context, defer the
c5905afb 1578 * static_key_slow_dec() calls.
b90e5794
ED
1579 */
1580static atomic_t netstamp_needed_deferred;
1581#endif
1da177e4
LT
1582
1583void net_enable_timestamp(void)
1584{
b90e5794
ED
1585#ifdef HAVE_JUMP_LABEL
1586 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
1587
1588 if (deferred) {
1589 while (--deferred)
c5905afb 1590 static_key_slow_dec(&netstamp_needed);
b90e5794
ED
1591 return;
1592 }
1593#endif
1594 WARN_ON(in_interrupt());
c5905afb 1595 static_key_slow_inc(&netstamp_needed);
1da177e4 1596}
d1b19dff 1597EXPORT_SYMBOL(net_enable_timestamp);
1da177e4
LT
1598
1599void net_disable_timestamp(void)
1600{
b90e5794
ED
1601#ifdef HAVE_JUMP_LABEL
1602 if (in_interrupt()) {
1603 atomic_inc(&netstamp_needed_deferred);
1604 return;
1605 }
1606#endif
c5905afb 1607 static_key_slow_dec(&netstamp_needed);
1da177e4 1608}
d1b19dff 1609EXPORT_SYMBOL(net_disable_timestamp);
1da177e4 1610
3b098e2d 1611static inline void net_timestamp_set(struct sk_buff *skb)
1da177e4 1612{
588f0330 1613 skb->tstamp.tv64 = 0;
c5905afb 1614 if (static_key_false(&netstamp_needed))
a61bbcf2 1615 __net_timestamp(skb);
1da177e4
LT
1616}
1617
588f0330 1618#define net_timestamp_check(COND, SKB) \
c5905afb 1619 if (static_key_false(&netstamp_needed)) { \
588f0330
ED
1620 if ((COND) && !(SKB)->tstamp.tv64) \
1621 __net_timestamp(SKB); \
1622 } \
3b098e2d 1623
4dc360c5
RC
1624static int net_hwtstamp_validate(struct ifreq *ifr)
1625{
1626 struct hwtstamp_config cfg;
1627 enum hwtstamp_tx_types tx_type;
1628 enum hwtstamp_rx_filters rx_filter;
1629 int tx_type_valid = 0;
1630 int rx_filter_valid = 0;
1631
1632 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1633 return -EFAULT;
1634
1635 if (cfg.flags) /* reserved for future extensions */
1636 return -EINVAL;
1637
1638 tx_type = cfg.tx_type;
1639 rx_filter = cfg.rx_filter;
1640
1641 switch (tx_type) {
1642 case HWTSTAMP_TX_OFF:
1643 case HWTSTAMP_TX_ON:
1644 case HWTSTAMP_TX_ONESTEP_SYNC:
1645 tx_type_valid = 1;
1646 break;
1647 }
1648
1649 switch (rx_filter) {
1650 case HWTSTAMP_FILTER_NONE:
1651 case HWTSTAMP_FILTER_ALL:
1652 case HWTSTAMP_FILTER_SOME:
1653 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1654 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1655 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1656 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1657 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1658 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1659 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1660 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1661 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1662 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1663 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1664 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1665 rx_filter_valid = 1;
1666 break;
1667 }
1668
1669 if (!tx_type_valid || !rx_filter_valid)
1670 return -ERANGE;
1671
1672 return 0;
1673}
1674
79b569f0
DL
1675static inline bool is_skb_forwardable(struct net_device *dev,
1676 struct sk_buff *skb)
1677{
1678 unsigned int len;
1679
1680 if (!(dev->flags & IFF_UP))
1681 return false;
1682
1683 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1684 if (skb->len <= len)
1685 return true;
1686
1687 /* if TSO is enabled, we don't care about the length as the packet
1688 * could be forwarded without being segmented before
1689 */
1690 if (skb_is_gso(skb))
1691 return true;
1692
1693 return false;
1694}
1695
44540960
AB
1696/**
1697 * dev_forward_skb - loopback an skb to another netif
1698 *
1699 * @dev: destination network device
1700 * @skb: buffer to forward
1701 *
1702 * return values:
1703 * NET_RX_SUCCESS (no congestion)
6ec82562 1704 * NET_RX_DROP (packet was dropped, but freed)
44540960
AB
1705 *
1706 * dev_forward_skb can be used for injecting an skb from the
1707 * start_xmit function of one device into the receive queue
1708 * of another device.
1709 *
1710 * The receiving device may be in another namespace, so
1711 * we have to clear all information in the skb that could
1712 * impact namespace isolation.
1713 */
1714int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1715{
48c83012
MT
1716 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
1717 if (skb_copy_ubufs(skb, GFP_ATOMIC)) {
1718 atomic_long_inc(&dev->rx_dropped);
1719 kfree_skb(skb);
1720 return NET_RX_DROP;
1721 }
1722 }
1723
44540960 1724 skb_orphan(skb);
c736eefa 1725 nf_reset(skb);
44540960 1726
79b569f0 1727 if (unlikely(!is_skb_forwardable(dev, skb))) {
caf586e5 1728 atomic_long_inc(&dev->rx_dropped);
6ec82562 1729 kfree_skb(skb);
44540960 1730 return NET_RX_DROP;
6ec82562 1731 }
3b9785c6 1732 skb->skb_iif = 0;
59b9997b
DM
1733 skb->dev = dev;
1734 skb_dst_drop(skb);
44540960
AB
1735 skb->tstamp.tv64 = 0;
1736 skb->pkt_type = PACKET_HOST;
1737 skb->protocol = eth_type_trans(skb, dev);
59b9997b
DM
1738 skb->mark = 0;
1739 secpath_reset(skb);
1740 nf_reset(skb);
44540960
AB
1741 return netif_rx(skb);
1742}
1743EXPORT_SYMBOL_GPL(dev_forward_skb);
1744
71d9dec2
CG
1745static inline int deliver_skb(struct sk_buff *skb,
1746 struct packet_type *pt_prev,
1747 struct net_device *orig_dev)
1748{
1080e512
MT
1749 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
1750 return -ENOMEM;
71d9dec2
CG
1751 atomic_inc(&skb->users);
1752 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1753}
1754
c0de08d0
EL
1755static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1756{
a3d744e9 1757 if (!ptype->af_packet_priv || !skb->sk)
c0de08d0
EL
1758 return false;
1759
1760 if (ptype->id_match)
1761 return ptype->id_match(ptype, skb->sk);
1762 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1763 return true;
1764
1765 return false;
1766}
1767
1da177e4
LT
1768/*
1769 * Support routine. Sends outgoing frames to any network
1770 * taps currently in use.
1771 */
1772
f6a78bfc 1773static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
1774{
1775 struct packet_type *ptype;
71d9dec2
CG
1776 struct sk_buff *skb2 = NULL;
1777 struct packet_type *pt_prev = NULL;
a61bbcf2 1778
1da177e4
LT
1779 rcu_read_lock();
1780 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1781 /* Never send packets back to the socket
1782 * they originated from - MvS (miquels@drinkel.ow.org)
1783 */
1784 if ((ptype->dev == dev || !ptype->dev) &&
c0de08d0 1785 (!skb_loop_sk(ptype, skb))) {
71d9dec2
CG
1786 if (pt_prev) {
1787 deliver_skb(skb2, pt_prev, skb->dev);
1788 pt_prev = ptype;
1789 continue;
1790 }
1791
1792 skb2 = skb_clone(skb, GFP_ATOMIC);
1da177e4
LT
1793 if (!skb2)
1794 break;
1795
70978182
ED
1796 net_timestamp_set(skb2);
1797
1da177e4
LT
1798 /* skb->nh should be correctly
1799 set by sender, so that the second statement is
1800 just protection against buggy protocols.
1801 */
459a98ed 1802 skb_reset_mac_header(skb2);
1da177e4 1803
d56f90a7 1804 if (skb_network_header(skb2) < skb2->data ||
27a884dc 1805 skb2->network_header > skb2->tail) {
e87cc472
JP
1806 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
1807 ntohs(skb2->protocol),
1808 dev->name);
c1d2bbe1 1809 skb_reset_network_header(skb2);
1da177e4
LT
1810 }
1811
b0e380b1 1812 skb2->transport_header = skb2->network_header;
1da177e4 1813 skb2->pkt_type = PACKET_OUTGOING;
71d9dec2 1814 pt_prev = ptype;
1da177e4
LT
1815 }
1816 }
71d9dec2
CG
1817 if (pt_prev)
1818 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
1da177e4
LT
1819 rcu_read_unlock();
1820}
1821
2c53040f
BH
1822/**
1823 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
4f57c087
JF
1824 * @dev: Network device
1825 * @txq: number of queues available
1826 *
1827 * If real_num_tx_queues is changed the tc mappings may no longer be
1828 * valid. To resolve this verify the tc mapping remains valid and if
1829 * not NULL the mapping. With no priorities mapping to this
1830 * offset/count pair it will no longer be used. In the worst case TC0
1831 * is invalid nothing can be done so disable priority mappings. If is
1832 * expected that drivers will fix this mapping if they can before
1833 * calling netif_set_real_num_tx_queues.
1834 */
bb134d22 1835static void netif_setup_tc(struct net_device *dev, unsigned int txq)
4f57c087
JF
1836{
1837 int i;
1838 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1839
1840 /* If TC0 is invalidated disable TC mapping */
1841 if (tc->offset + tc->count > txq) {
7b6cd1ce 1842 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
4f57c087
JF
1843 dev->num_tc = 0;
1844 return;
1845 }
1846
1847 /* Invalidated prio to tc mappings set to TC0 */
1848 for (i = 1; i < TC_BITMASK + 1; i++) {
1849 int q = netdev_get_prio_tc_map(dev, i);
1850
1851 tc = &dev->tc_to_txq[q];
1852 if (tc->offset + tc->count > txq) {
7b6cd1ce
JP
1853 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
1854 i, q);
4f57c087
JF
1855 netdev_set_prio_tc_map(dev, i, 0);
1856 }
1857 }
1858}
1859
537c00de
AD
1860#ifdef CONFIG_XPS
1861static DEFINE_MUTEX(xps_map_mutex);
1862#define xmap_dereference(P) \
1863 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
1864
10cdc3f3
AD
1865static struct xps_map *remove_xps_queue(struct xps_dev_maps *dev_maps,
1866 int cpu, u16 index)
537c00de 1867{
10cdc3f3
AD
1868 struct xps_map *map = NULL;
1869 int pos;
537c00de 1870
10cdc3f3
AD
1871 if (dev_maps)
1872 map = xmap_dereference(dev_maps->cpu_map[cpu]);
537c00de 1873
10cdc3f3
AD
1874 for (pos = 0; map && pos < map->len; pos++) {
1875 if (map->queues[pos] == index) {
537c00de
AD
1876 if (map->len > 1) {
1877 map->queues[pos] = map->queues[--map->len];
1878 } else {
10cdc3f3 1879 RCU_INIT_POINTER(dev_maps->cpu_map[cpu], NULL);
537c00de
AD
1880 kfree_rcu(map, rcu);
1881 map = NULL;
1882 }
10cdc3f3 1883 break;
537c00de 1884 }
537c00de
AD
1885 }
1886
10cdc3f3
AD
1887 return map;
1888}
1889
1890void netif_reset_xps_queue(struct net_device *dev, u16 index)
1891{
1892 struct xps_dev_maps *dev_maps;
1893 int cpu;
1894 bool active = false;
1895
1896 mutex_lock(&xps_map_mutex);
1897 dev_maps = xmap_dereference(dev->xps_maps);
1898
1899 if (!dev_maps)
1900 goto out_no_maps;
1901
1902 for_each_possible_cpu(cpu) {
1903 if (remove_xps_queue(dev_maps, cpu, index))
1904 active = true;
1905 }
1906
1907 if (!active) {
537c00de
AD
1908 RCU_INIT_POINTER(dev->xps_maps, NULL);
1909 kfree_rcu(dev_maps, rcu);
1910 }
1911
10cdc3f3
AD
1912 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
1913 NUMA_NO_NODE);
537c00de
AD
1914out_no_maps:
1915 mutex_unlock(&xps_map_mutex);
1916}
1917
01c5f864
AD
1918static struct xps_map *expand_xps_map(struct xps_map *map,
1919 int cpu, u16 index)
1920{
1921 struct xps_map *new_map;
1922 int alloc_len = XPS_MIN_MAP_ALLOC;
1923 int i, pos;
1924
1925 for (pos = 0; map && pos < map->len; pos++) {
1926 if (map->queues[pos] != index)
1927 continue;
1928 return map;
1929 }
1930
1931 /* Need to add queue to this CPU's existing map */
1932 if (map) {
1933 if (pos < map->alloc_len)
1934 return map;
1935
1936 alloc_len = map->alloc_len * 2;
1937 }
1938
1939 /* Need to allocate new map to store queue on this CPU's map */
1940 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
1941 cpu_to_node(cpu));
1942 if (!new_map)
1943 return NULL;
1944
1945 for (i = 0; i < pos; i++)
1946 new_map->queues[i] = map->queues[i];
1947 new_map->alloc_len = alloc_len;
1948 new_map->len = pos;
1949
1950 return new_map;
1951}
1952
537c00de
AD
1953int netif_set_xps_queue(struct net_device *dev, struct cpumask *mask, u16 index)
1954{
01c5f864 1955 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
537c00de 1956 struct xps_map *map, *new_map;
537c00de 1957 int maps_sz = max_t(unsigned int, XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES);
01c5f864
AD
1958 int cpu, numa_node_id = -2;
1959 bool active = false;
537c00de
AD
1960
1961 mutex_lock(&xps_map_mutex);
1962
1963 dev_maps = xmap_dereference(dev->xps_maps);
1964
01c5f864
AD
1965 /* allocate memory for queue storage */
1966 for_each_online_cpu(cpu) {
1967 if (!cpumask_test_cpu(cpu, mask))
1968 continue;
1969
1970 if (!new_dev_maps)
1971 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
1972 if (!new_dev_maps)
1973 return -ENOMEM;
1974
1975 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
1976 NULL;
1977
1978 map = expand_xps_map(map, cpu, index);
1979 if (!map)
1980 goto error;
1981
1982 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
1983 }
1984
1985 if (!new_dev_maps)
1986 goto out_no_new_maps;
1987
537c00de 1988 for_each_possible_cpu(cpu) {
01c5f864
AD
1989 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
1990 /* add queue to CPU maps */
1991 int pos = 0;
1992
1993 map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
1994 while ((pos < map->len) && (map->queues[pos] != index))
1995 pos++;
1996
1997 if (pos == map->len)
1998 map->queues[map->len++] = index;
537c00de 1999#ifdef CONFIG_NUMA
537c00de
AD
2000 if (numa_node_id == -2)
2001 numa_node_id = cpu_to_node(cpu);
2002 else if (numa_node_id != cpu_to_node(cpu))
2003 numa_node_id = -1;
537c00de 2004#endif
01c5f864
AD
2005 } else if (dev_maps) {
2006 /* fill in the new device map from the old device map */
2007 map = xmap_dereference(dev_maps->cpu_map[cpu]);
2008 RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map);
537c00de 2009 }
01c5f864 2010
537c00de
AD
2011 }
2012
01c5f864
AD
2013 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
2014
537c00de 2015 /* Cleanup old maps */
01c5f864
AD
2016 if (dev_maps) {
2017 for_each_possible_cpu(cpu) {
2018 new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
2019 map = xmap_dereference(dev_maps->cpu_map[cpu]);
2020 if (map && map != new_map)
2021 kfree_rcu(map, rcu);
2022 }
537c00de 2023
01c5f864 2024 kfree_rcu(dev_maps, rcu);
537c00de
AD
2025 }
2026
01c5f864
AD
2027 dev_maps = new_dev_maps;
2028 active = true;
537c00de 2029
01c5f864
AD
2030out_no_new_maps:
2031 /* update Tx queue numa node */
537c00de
AD
2032 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2033 (numa_node_id >= 0) ? numa_node_id :
2034 NUMA_NO_NODE);
2035
01c5f864
AD
2036 if (!dev_maps)
2037 goto out_no_maps;
2038
2039 /* removes queue from unused CPUs */
2040 for_each_possible_cpu(cpu) {
2041 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu))
2042 continue;
2043
2044 if (remove_xps_queue(dev_maps, cpu, index))
2045 active = true;
2046 }
2047
2048 /* free map if not active */
2049 if (!active) {
2050 RCU_INIT_POINTER(dev->xps_maps, NULL);
2051 kfree_rcu(dev_maps, rcu);
2052 }
2053
2054out_no_maps:
537c00de
AD
2055 mutex_unlock(&xps_map_mutex);
2056
2057 return 0;
2058error:
01c5f864
AD
2059 /* remove any maps that we added */
2060 for_each_possible_cpu(cpu) {
2061 new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]);
2062 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) :
2063 NULL;
2064 if (new_map && new_map != map)
2065 kfree(new_map);
2066 }
2067
537c00de
AD
2068 mutex_unlock(&xps_map_mutex);
2069
537c00de
AD
2070 kfree(new_dev_maps);
2071 return -ENOMEM;
2072}
2073EXPORT_SYMBOL(netif_set_xps_queue);
2074
2075#endif
f0796d5c
JF
2076/*
2077 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2078 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
2079 */
e6484930 2080int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
f0796d5c 2081{
1d24eb48
TH
2082 int rc;
2083
e6484930
TH
2084 if (txq < 1 || txq > dev->num_tx_queues)
2085 return -EINVAL;
f0796d5c 2086
5c56580b
BH
2087 if (dev->reg_state == NETREG_REGISTERED ||
2088 dev->reg_state == NETREG_UNREGISTERING) {
e6484930
TH
2089 ASSERT_RTNL();
2090
1d24eb48
TH
2091 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2092 txq);
bf264145
TH
2093 if (rc)
2094 return rc;
2095
4f57c087
JF
2096 if (dev->num_tc)
2097 netif_setup_tc(dev, txq);
2098
e6484930
TH
2099 if (txq < dev->real_num_tx_queues)
2100 qdisc_reset_all_tx_gt(dev, txq);
f0796d5c 2101 }
e6484930
TH
2102
2103 dev->real_num_tx_queues = txq;
2104 return 0;
f0796d5c
JF
2105}
2106EXPORT_SYMBOL(netif_set_real_num_tx_queues);
56079431 2107
62fe0b40
BH
2108#ifdef CONFIG_RPS
2109/**
2110 * netif_set_real_num_rx_queues - set actual number of RX queues used
2111 * @dev: Network device
2112 * @rxq: Actual number of RX queues
2113 *
2114 * This must be called either with the rtnl_lock held or before
2115 * registration of the net device. Returns 0 on success, or a
4e7f7951
BH
2116 * negative error code. If called before registration, it always
2117 * succeeds.
62fe0b40
BH
2118 */
2119int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2120{
2121 int rc;
2122
bd25fa7b
TH
2123 if (rxq < 1 || rxq > dev->num_rx_queues)
2124 return -EINVAL;
2125
62fe0b40
BH
2126 if (dev->reg_state == NETREG_REGISTERED) {
2127 ASSERT_RTNL();
2128
62fe0b40
BH
2129 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2130 rxq);
2131 if (rc)
2132 return rc;
62fe0b40
BH
2133 }
2134
2135 dev->real_num_rx_queues = rxq;
2136 return 0;
2137}
2138EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2139#endif
2140
2c53040f
BH
2141/**
2142 * netif_get_num_default_rss_queues - default number of RSS queues
16917b87
YM
2143 *
2144 * This routine should set an upper limit on the number of RSS queues
2145 * used by default by multiqueue devices.
2146 */
a55b138b 2147int netif_get_num_default_rss_queues(void)
16917b87
YM
2148{
2149 return min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
2150}
2151EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2152
def82a1d 2153static inline void __netif_reschedule(struct Qdisc *q)
56079431 2154{
def82a1d
JP
2155 struct softnet_data *sd;
2156 unsigned long flags;
56079431 2157
def82a1d
JP
2158 local_irq_save(flags);
2159 sd = &__get_cpu_var(softnet_data);
a9cbd588
CG
2160 q->next_sched = NULL;
2161 *sd->output_queue_tailp = q;
2162 sd->output_queue_tailp = &q->next_sched;
def82a1d
JP
2163 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2164 local_irq_restore(flags);
2165}
2166
2167void __netif_schedule(struct Qdisc *q)
2168{
2169 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2170 __netif_reschedule(q);
56079431
DV
2171}
2172EXPORT_SYMBOL(__netif_schedule);
2173
bea3348e 2174void dev_kfree_skb_irq(struct sk_buff *skb)
56079431 2175{
3578b0c8 2176 if (atomic_dec_and_test(&skb->users)) {
bea3348e
SH
2177 struct softnet_data *sd;
2178 unsigned long flags;
56079431 2179
bea3348e
SH
2180 local_irq_save(flags);
2181 sd = &__get_cpu_var(softnet_data);
2182 skb->next = sd->completion_queue;
2183 sd->completion_queue = skb;
2184 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2185 local_irq_restore(flags);
2186 }
56079431 2187}
bea3348e 2188EXPORT_SYMBOL(dev_kfree_skb_irq);
56079431
DV
2189
2190void dev_kfree_skb_any(struct sk_buff *skb)
2191{
2192 if (in_irq() || irqs_disabled())
2193 dev_kfree_skb_irq(skb);
2194 else
2195 dev_kfree_skb(skb);
2196}
2197EXPORT_SYMBOL(dev_kfree_skb_any);
2198
2199
bea3348e
SH
2200/**
2201 * netif_device_detach - mark device as removed
2202 * @dev: network device
2203 *
2204 * Mark device as removed from system and therefore no longer available.
2205 */
56079431
DV
2206void netif_device_detach(struct net_device *dev)
2207{
2208 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2209 netif_running(dev)) {
d543103a 2210 netif_tx_stop_all_queues(dev);
56079431
DV
2211 }
2212}
2213EXPORT_SYMBOL(netif_device_detach);
2214
bea3348e
SH
2215/**
2216 * netif_device_attach - mark device as attached
2217 * @dev: network device
2218 *
2219 * Mark device as attached from system and restart if needed.
2220 */
56079431
DV
2221void netif_device_attach(struct net_device *dev)
2222{
2223 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2224 netif_running(dev)) {
d543103a 2225 netif_tx_wake_all_queues(dev);
4ec93edb 2226 __netdev_watchdog_up(dev);
56079431
DV
2227 }
2228}
2229EXPORT_SYMBOL(netif_device_attach);
2230
36c92474
BH
2231static void skb_warn_bad_offload(const struct sk_buff *skb)
2232{
65e9d2fa 2233 static const netdev_features_t null_features = 0;
36c92474
BH
2234 struct net_device *dev = skb->dev;
2235 const char *driver = "";
2236
2237 if (dev && dev->dev.parent)
2238 driver = dev_driver_string(dev->dev.parent);
2239
2240 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2241 "gso_type=%d ip_summed=%d\n",
65e9d2fa
MM
2242 driver, dev ? &dev->features : &null_features,
2243 skb->sk ? &skb->sk->sk_route_caps : &null_features,
36c92474
BH
2244 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2245 skb_shinfo(skb)->gso_type, skb->ip_summed);
2246}
2247
1da177e4
LT
2248/*
2249 * Invalidate hardware checksum when packet is to be mangled, and
2250 * complete checksum manually on outgoing path.
2251 */
84fa7933 2252int skb_checksum_help(struct sk_buff *skb)
1da177e4 2253{
d3bc23e7 2254 __wsum csum;
663ead3b 2255 int ret = 0, offset;
1da177e4 2256
84fa7933 2257 if (skb->ip_summed == CHECKSUM_COMPLETE)
a430a43d
HX
2258 goto out_set_summed;
2259
2260 if (unlikely(skb_shinfo(skb)->gso_size)) {
36c92474
BH
2261 skb_warn_bad_offload(skb);
2262 return -EINVAL;
1da177e4
LT
2263 }
2264
55508d60 2265 offset = skb_checksum_start_offset(skb);
a030847e
HX
2266 BUG_ON(offset >= skb_headlen(skb));
2267 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2268
2269 offset += skb->csum_offset;
2270 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2271
2272 if (skb_cloned(skb) &&
2273 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
1da177e4
LT
2274 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2275 if (ret)
2276 goto out;
2277 }
2278
a030847e 2279 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
a430a43d 2280out_set_summed:
1da177e4 2281 skb->ip_summed = CHECKSUM_NONE;
4ec93edb 2282out:
1da177e4
LT
2283 return ret;
2284}
d1b19dff 2285EXPORT_SYMBOL(skb_checksum_help);
1da177e4 2286
f6a78bfc
HX
2287/**
2288 * skb_gso_segment - Perform segmentation on skb.
2289 * @skb: buffer to segment
576a30eb 2290 * @features: features for the output path (see dev->features)
f6a78bfc
HX
2291 *
2292 * This function segments the given skb and returns a list of segments.
576a30eb
HX
2293 *
2294 * It may return NULL if the skb requires no segmentation. This is
2295 * only possible when GSO is used for verifying header integrity.
f6a78bfc 2296 */
c8f44aff
MM
2297struct sk_buff *skb_gso_segment(struct sk_buff *skb,
2298 netdev_features_t features)
f6a78bfc
HX
2299{
2300 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
22061d80 2301 struct packet_offload *ptype;
252e3346 2302 __be16 type = skb->protocol;
c8d5bcd1 2303 int vlan_depth = ETH_HLEN;
a430a43d 2304 int err;
f6a78bfc 2305
c8d5bcd1
JG
2306 while (type == htons(ETH_P_8021Q)) {
2307 struct vlan_hdr *vh;
7b9c6090 2308
c8d5bcd1 2309 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
7b9c6090
JG
2310 return ERR_PTR(-EINVAL);
2311
c8d5bcd1
JG
2312 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
2313 type = vh->h_vlan_encapsulated_proto;
2314 vlan_depth += VLAN_HLEN;
7b9c6090
JG
2315 }
2316
459a98ed 2317 skb_reset_mac_header(skb);
b0e380b1 2318 skb->mac_len = skb->network_header - skb->mac_header;
f6a78bfc
HX
2319 __skb_pull(skb, skb->mac_len);
2320
67fd1a73 2321 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
36c92474 2322 skb_warn_bad_offload(skb);
67fd1a73 2323
a430a43d
HX
2324 if (skb_header_cloned(skb) &&
2325 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
2326 return ERR_PTR(err);
2327 }
2328
f6a78bfc 2329 rcu_read_lock();
22061d80 2330 list_for_each_entry_rcu(ptype, &offload_base, list) {
f191a1d1 2331 if (ptype->type == type && ptype->callbacks.gso_segment) {
84fa7933 2332 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
f191a1d1 2333 err = ptype->callbacks.gso_send_check(skb);
a430a43d
HX
2334 segs = ERR_PTR(err);
2335 if (err || skb_gso_ok(skb, features))
2336 break;
d56f90a7
ACM
2337 __skb_push(skb, (skb->data -
2338 skb_network_header(skb)));
a430a43d 2339 }
f191a1d1 2340 segs = ptype->callbacks.gso_segment(skb, features);
f6a78bfc
HX
2341 break;
2342 }
2343 }
2344 rcu_read_unlock();
2345
98e399f8 2346 __skb_push(skb, skb->data - skb_mac_header(skb));
576a30eb 2347
f6a78bfc
HX
2348 return segs;
2349}
f6a78bfc
HX
2350EXPORT_SYMBOL(skb_gso_segment);
2351
fb286bb2
HX
2352/* Take action when hardware reception checksum errors are detected. */
2353#ifdef CONFIG_BUG
2354void netdev_rx_csum_fault(struct net_device *dev)
2355{
2356 if (net_ratelimit()) {
7b6cd1ce 2357 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
fb286bb2
HX
2358 dump_stack();
2359 }
2360}
2361EXPORT_SYMBOL(netdev_rx_csum_fault);
2362#endif
2363
1da177e4
LT
2364/* Actually, we should eliminate this check as soon as we know, that:
2365 * 1. IOMMU is present and allows to map all the memory.
2366 * 2. No high memory really exists on this machine.
2367 */
2368
9092c658 2369static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1da177e4 2370{
3d3a8533 2371#ifdef CONFIG_HIGHMEM
1da177e4 2372 int i;
5acbbd42 2373 if (!(dev->features & NETIF_F_HIGHDMA)) {
ea2ab693
IC
2374 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2375 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2376 if (PageHighMem(skb_frag_page(frag)))
5acbbd42 2377 return 1;
ea2ab693 2378 }
5acbbd42 2379 }
1da177e4 2380
5acbbd42
FT
2381 if (PCI_DMA_BUS_IS_PHYS) {
2382 struct device *pdev = dev->dev.parent;
1da177e4 2383
9092c658
ED
2384 if (!pdev)
2385 return 0;
5acbbd42 2386 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
ea2ab693
IC
2387 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2388 dma_addr_t addr = page_to_phys(skb_frag_page(frag));
5acbbd42
FT
2389 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
2390 return 1;
2391 }
2392 }
3d3a8533 2393#endif
1da177e4
LT
2394 return 0;
2395}
1da177e4 2396
f6a78bfc
HX
2397struct dev_gso_cb {
2398 void (*destructor)(struct sk_buff *skb);
2399};
2400
2401#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
2402
2403static void dev_gso_skb_destructor(struct sk_buff *skb)
2404{
2405 struct dev_gso_cb *cb;
2406
2407 do {
2408 struct sk_buff *nskb = skb->next;
2409
2410 skb->next = nskb->next;
2411 nskb->next = NULL;
2412 kfree_skb(nskb);
2413 } while (skb->next);
2414
2415 cb = DEV_GSO_CB(skb);
2416 if (cb->destructor)
2417 cb->destructor(skb);
2418}
2419
2420/**
2421 * dev_gso_segment - Perform emulated hardware segmentation on skb.
2422 * @skb: buffer to segment
91ecb63c 2423 * @features: device features as applicable to this skb
f6a78bfc
HX
2424 *
2425 * This function segments the given skb and stores the list of segments
2426 * in skb->next.
2427 */
c8f44aff 2428static int dev_gso_segment(struct sk_buff *skb, netdev_features_t features)
f6a78bfc 2429{
f6a78bfc 2430 struct sk_buff *segs;
576a30eb
HX
2431
2432 segs = skb_gso_segment(skb, features);
2433
2434 /* Verifying header integrity only. */
2435 if (!segs)
2436 return 0;
f6a78bfc 2437
801678c5 2438 if (IS_ERR(segs))
f6a78bfc
HX
2439 return PTR_ERR(segs);
2440
2441 skb->next = segs;
2442 DEV_GSO_CB(skb)->destructor = skb->destructor;
2443 skb->destructor = dev_gso_skb_destructor;
2444
2445 return 0;
2446}
2447
c8f44aff 2448static bool can_checksum_protocol(netdev_features_t features, __be16 protocol)
03634668
JG
2449{
2450 return ((features & NETIF_F_GEN_CSUM) ||
2451 ((features & NETIF_F_V4_CSUM) &&
2452 protocol == htons(ETH_P_IP)) ||
2453 ((features & NETIF_F_V6_CSUM) &&
2454 protocol == htons(ETH_P_IPV6)) ||
2455 ((features & NETIF_F_FCOE_CRC) &&
2456 protocol == htons(ETH_P_FCOE)));
2457}
2458
c8f44aff
MM
2459static netdev_features_t harmonize_features(struct sk_buff *skb,
2460 __be16 protocol, netdev_features_t features)
f01a5236 2461{
c0d680e5
EC
2462 if (skb->ip_summed != CHECKSUM_NONE &&
2463 !can_checksum_protocol(features, protocol)) {
f01a5236
JG
2464 features &= ~NETIF_F_ALL_CSUM;
2465 features &= ~NETIF_F_SG;
2466 } else if (illegal_highdma(skb->dev, skb)) {
2467 features &= ~NETIF_F_SG;
2468 }
2469
2470 return features;
2471}
2472
c8f44aff 2473netdev_features_t netif_skb_features(struct sk_buff *skb)
58e998c6
JG
2474{
2475 __be16 protocol = skb->protocol;
c8f44aff 2476 netdev_features_t features = skb->dev->features;
58e998c6 2477
30b678d8
BH
2478 if (skb_shinfo(skb)->gso_segs > skb->dev->gso_max_segs)
2479 features &= ~NETIF_F_GSO_MASK;
2480
58e998c6
JG
2481 if (protocol == htons(ETH_P_8021Q)) {
2482 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
2483 protocol = veh->h_vlan_encapsulated_proto;
f01a5236
JG
2484 } else if (!vlan_tx_tag_present(skb)) {
2485 return harmonize_features(skb, protocol, features);
2486 }
58e998c6 2487
6ee400aa 2488 features &= (skb->dev->vlan_features | NETIF_F_HW_VLAN_TX);
f01a5236
JG
2489
2490 if (protocol != htons(ETH_P_8021Q)) {
2491 return harmonize_features(skb, protocol, features);
2492 } else {
2493 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
6ee400aa 2494 NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_TX;
f01a5236
JG
2495 return harmonize_features(skb, protocol, features);
2496 }
58e998c6 2497}
f01a5236 2498EXPORT_SYMBOL(netif_skb_features);
58e998c6 2499
6afff0ca
JF
2500/*
2501 * Returns true if either:
2502 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
d1a53dfd 2503 * 2. skb is fragmented and the device does not support SG.
6afff0ca
JF
2504 */
2505static inline int skb_needs_linearize(struct sk_buff *skb,
02932ce9 2506 int features)
6afff0ca 2507{
02932ce9
JG
2508 return skb_is_nonlinear(skb) &&
2509 ((skb_has_frag_list(skb) &&
2510 !(features & NETIF_F_FRAGLIST)) ||
e1e78db6 2511 (skb_shinfo(skb)->nr_frags &&
02932ce9 2512 !(features & NETIF_F_SG)));
6afff0ca
JF
2513}
2514
fd2ea0a7
DM
2515int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
2516 struct netdev_queue *txq)
f6a78bfc 2517{
00829823 2518 const struct net_device_ops *ops = dev->netdev_ops;
572a9d7b 2519 int rc = NETDEV_TX_OK;
ec764bf0 2520 unsigned int skb_len;
00829823 2521
f6a78bfc 2522 if (likely(!skb->next)) {
c8f44aff 2523 netdev_features_t features;
fc741216 2524
93f154b5 2525 /*
25985edc 2526 * If device doesn't need skb->dst, release it right now while
93f154b5
ED
2527 * its hot in this cpu cache
2528 */
adf30907
ED
2529 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2530 skb_dst_drop(skb);
2531
fc741216
JG
2532 features = netif_skb_features(skb);
2533
7b9c6090 2534 if (vlan_tx_tag_present(skb) &&
fc741216 2535 !(features & NETIF_F_HW_VLAN_TX)) {
7b9c6090
JG
2536 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
2537 if (unlikely(!skb))
2538 goto out;
2539
2540 skb->vlan_tci = 0;
2541 }
2542
fc70fb64
AD
2543 /* If encapsulation offload request, verify we are testing
2544 * hardware encapsulation features instead of standard
2545 * features for the netdev
2546 */
2547 if (skb->encapsulation)
2548 features &= dev->hw_enc_features;
2549
fc741216 2550 if (netif_needs_gso(skb, features)) {
91ecb63c 2551 if (unlikely(dev_gso_segment(skb, features)))
9ccb8975
DM
2552 goto out_kfree_skb;
2553 if (skb->next)
2554 goto gso;
6afff0ca 2555 } else {
02932ce9 2556 if (skb_needs_linearize(skb, features) &&
6afff0ca
JF
2557 __skb_linearize(skb))
2558 goto out_kfree_skb;
2559
2560 /* If packet is not checksummed and device does not
2561 * support checksumming for this protocol, complete
2562 * checksumming here.
2563 */
2564 if (skb->ip_summed == CHECKSUM_PARTIAL) {
fc70fb64
AD
2565 if (skb->encapsulation)
2566 skb_set_inner_transport_header(skb,
2567 skb_checksum_start_offset(skb));
2568 else
2569 skb_set_transport_header(skb,
2570 skb_checksum_start_offset(skb));
03634668 2571 if (!(features & NETIF_F_ALL_CSUM) &&
6afff0ca
JF
2572 skb_checksum_help(skb))
2573 goto out_kfree_skb;
2574 }
9ccb8975
DM
2575 }
2576
b40863c6
ED
2577 if (!list_empty(&ptype_all))
2578 dev_queue_xmit_nit(skb, dev);
2579
ec764bf0 2580 skb_len = skb->len;
ac45f602 2581 rc = ops->ndo_start_xmit(skb, dev);
ec764bf0 2582 trace_net_dev_xmit(skb, rc, dev, skb_len);
ec634fe3 2583 if (rc == NETDEV_TX_OK)
08baf561 2584 txq_trans_update(txq);
ac45f602 2585 return rc;
f6a78bfc
HX
2586 }
2587
576a30eb 2588gso:
f6a78bfc
HX
2589 do {
2590 struct sk_buff *nskb = skb->next;
f6a78bfc
HX
2591
2592 skb->next = nskb->next;
2593 nskb->next = NULL;
068a2de5
KK
2594
2595 /*
25985edc 2596 * If device doesn't need nskb->dst, release it right now while
068a2de5
KK
2597 * its hot in this cpu cache
2598 */
2599 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
2600 skb_dst_drop(nskb);
2601
b40863c6
ED
2602 if (!list_empty(&ptype_all))
2603 dev_queue_xmit_nit(nskb, dev);
2604
ec764bf0 2605 skb_len = nskb->len;
00829823 2606 rc = ops->ndo_start_xmit(nskb, dev);
ec764bf0 2607 trace_net_dev_xmit(nskb, rc, dev, skb_len);
ec634fe3 2608 if (unlikely(rc != NETDEV_TX_OK)) {
572a9d7b
PM
2609 if (rc & ~NETDEV_TX_MASK)
2610 goto out_kfree_gso_skb;
f54d9e8d 2611 nskb->next = skb->next;
f6a78bfc
HX
2612 skb->next = nskb;
2613 return rc;
2614 }
08baf561 2615 txq_trans_update(txq);
73466498 2616 if (unlikely(netif_xmit_stopped(txq) && skb->next))
f54d9e8d 2617 return NETDEV_TX_BUSY;
f6a78bfc 2618 } while (skb->next);
4ec93edb 2619
572a9d7b
PM
2620out_kfree_gso_skb:
2621 if (likely(skb->next == NULL))
2622 skb->destructor = DEV_GSO_CB(skb)->destructor;
f6a78bfc
HX
2623out_kfree_skb:
2624 kfree_skb(skb);
7b9c6090 2625out:
572a9d7b 2626 return rc;
f6a78bfc
HX
2627}
2628
0a9627f2 2629static u32 hashrnd __read_mostly;
b6b2fed1 2630
a3d22a68
VZ
2631/*
2632 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2633 * to be used as a distribution range.
2634 */
2635u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
2636 unsigned int num_tx_queues)
8f0f2223 2637{
7019298a 2638 u32 hash;
4f57c087
JF
2639 u16 qoffset = 0;
2640 u16 qcount = num_tx_queues;
b6b2fed1 2641
513de11b
DM
2642 if (skb_rx_queue_recorded(skb)) {
2643 hash = skb_get_rx_queue(skb);
a3d22a68
VZ
2644 while (unlikely(hash >= num_tx_queues))
2645 hash -= num_tx_queues;
513de11b
DM
2646 return hash;
2647 }
ec581f6a 2648
4f57c087
JF
2649 if (dev->num_tc) {
2650 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
2651 qoffset = dev->tc_to_txq[tc].offset;
2652 qcount = dev->tc_to_txq[tc].count;
2653 }
2654
ec581f6a 2655 if (skb->sk && skb->sk->sk_hash)
7019298a 2656 hash = skb->sk->sk_hash;
ec581f6a 2657 else
62b1a8ab 2658 hash = (__force u16) skb->protocol;
0a9627f2 2659 hash = jhash_1word(hash, hashrnd);
b6b2fed1 2660
4f57c087 2661 return (u16) (((u64) hash * qcount) >> 32) + qoffset;
8f0f2223 2662}
a3d22a68 2663EXPORT_SYMBOL(__skb_tx_hash);
8f0f2223 2664
ed04642f
ED
2665static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
2666{
2667 if (unlikely(queue_index >= dev->real_num_tx_queues)) {
e87cc472
JP
2668 net_warn_ratelimited("%s selects TX queue %d, but real number of TX queues is %d\n",
2669 dev->name, queue_index,
2670 dev->real_num_tx_queues);
ed04642f
ED
2671 return 0;
2672 }
2673 return queue_index;
2674}
2675
1d24eb48
TH
2676static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
2677{
bf264145 2678#ifdef CONFIG_XPS
1d24eb48
TH
2679 struct xps_dev_maps *dev_maps;
2680 struct xps_map *map;
2681 int queue_index = -1;
2682
2683 rcu_read_lock();
2684 dev_maps = rcu_dereference(dev->xps_maps);
2685 if (dev_maps) {
2686 map = rcu_dereference(
2687 dev_maps->cpu_map[raw_smp_processor_id()]);
2688 if (map) {
2689 if (map->len == 1)
2690 queue_index = map->queues[0];
2691 else {
2692 u32 hash;
2693 if (skb->sk && skb->sk->sk_hash)
2694 hash = skb->sk->sk_hash;
2695 else
2696 hash = (__force u16) skb->protocol ^
2697 skb->rxhash;
2698 hash = jhash_1word(hash, hashrnd);
2699 queue_index = map->queues[
2700 ((u64)hash * map->len) >> 32];
2701 }
2702 if (unlikely(queue_index >= dev->real_num_tx_queues))
2703 queue_index = -1;
2704 }
2705 }
2706 rcu_read_unlock();
2707
2708 return queue_index;
2709#else
2710 return -1;
2711#endif
2712}
2713
416186fb 2714u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
e8a0464c 2715{
416186fb
AD
2716 struct sock *sk = skb->sk;
2717 int queue_index = sk_tx_queue_get(sk);
a4ee3ce3 2718
416186fb
AD
2719 if (queue_index < 0 || skb->ooo_okay ||
2720 queue_index >= dev->real_num_tx_queues) {
2721 int new_index = get_xps_queue(dev, skb);
2722 if (new_index < 0)
2723 new_index = skb_tx_hash(dev, skb);
fd2ea0a7 2724
416186fb
AD
2725 if (queue_index != new_index && sk) {
2726 struct dst_entry *dst =
3853b584 2727 rcu_dereference_check(sk->sk_dst_cache, 1);
8728c544 2728
416186fb
AD
2729 if (dst && skb_dst(skb) == dst)
2730 sk_tx_queue_set(sk, queue_index);
2731
a4ee3ce3 2732 }
416186fb
AD
2733
2734 queue_index = new_index;
2735 }
2736
2737 return queue_index;
2738}
2739
2740struct netdev_queue *netdev_pick_tx(struct net_device *dev,
2741 struct sk_buff *skb)
2742{
2743 int queue_index = 0;
2744
2745 if (dev->real_num_tx_queues != 1) {
2746 const struct net_device_ops *ops = dev->netdev_ops;
2747 if (ops->ndo_select_queue)
2748 queue_index = ops->ndo_select_queue(dev, skb);
2749 else
2750 queue_index = __netdev_pick_tx(dev, skb);
2751 queue_index = dev_cap_txqueue(dev, queue_index);
a4ee3ce3 2752 }
eae792b7 2753
fd2ea0a7
DM
2754 skb_set_queue_mapping(skb, queue_index);
2755 return netdev_get_tx_queue(dev, queue_index);
e8a0464c
DM
2756}
2757
1def9238
ED
2758static void qdisc_pkt_len_init(struct sk_buff *skb)
2759{
2760 const struct skb_shared_info *shinfo = skb_shinfo(skb);
2761
2762 qdisc_skb_cb(skb)->pkt_len = skb->len;
2763
2764 /* To get more precise estimation of bytes sent on wire,
2765 * we add to pkt_len the headers size of all segments
2766 */
2767 if (shinfo->gso_size) {
2768 unsigned int hdr_len = skb_transport_offset(skb);
2769
2770 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
2771 hdr_len += tcp_hdrlen(skb);
2772 else
2773 hdr_len += sizeof(struct udphdr);
2774 qdisc_skb_cb(skb)->pkt_len += (shinfo->gso_segs - 1) * hdr_len;
2775 }
2776}
2777
bbd8a0d3
KK
2778static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
2779 struct net_device *dev,
2780 struct netdev_queue *txq)
2781{
2782 spinlock_t *root_lock = qdisc_lock(q);
a2da570d 2783 bool contended;
bbd8a0d3
KK
2784 int rc;
2785
1def9238 2786 qdisc_pkt_len_init(skb);
a2da570d 2787 qdisc_calculate_pkt_len(skb, q);
79640a4c
ED
2788 /*
2789 * Heuristic to force contended enqueues to serialize on a
2790 * separate lock before trying to get qdisc main lock.
2791 * This permits __QDISC_STATE_RUNNING owner to get the lock more often
2792 * and dequeue packets faster.
2793 */
a2da570d 2794 contended = qdisc_is_running(q);
79640a4c
ED
2795 if (unlikely(contended))
2796 spin_lock(&q->busylock);
2797
bbd8a0d3
KK
2798 spin_lock(root_lock);
2799 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
2800 kfree_skb(skb);
2801 rc = NET_XMIT_DROP;
2802 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
bc135b23 2803 qdisc_run_begin(q)) {
bbd8a0d3
KK
2804 /*
2805 * This is a work-conserving queue; there are no old skbs
2806 * waiting to be sent out; and the qdisc is not running -
2807 * xmit the skb directly.
2808 */
7fee226a
ED
2809 if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
2810 skb_dst_force(skb);
bfe0d029 2811
bfe0d029
ED
2812 qdisc_bstats_update(q, skb);
2813
79640a4c
ED
2814 if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
2815 if (unlikely(contended)) {
2816 spin_unlock(&q->busylock);
2817 contended = false;
2818 }
bbd8a0d3 2819 __qdisc_run(q);
79640a4c 2820 } else
bc135b23 2821 qdisc_run_end(q);
bbd8a0d3
KK
2822
2823 rc = NET_XMIT_SUCCESS;
2824 } else {
7fee226a 2825 skb_dst_force(skb);
a2da570d 2826 rc = q->enqueue(skb, q) & NET_XMIT_MASK;
79640a4c
ED
2827 if (qdisc_run_begin(q)) {
2828 if (unlikely(contended)) {
2829 spin_unlock(&q->busylock);
2830 contended = false;
2831 }
2832 __qdisc_run(q);
2833 }
bbd8a0d3
KK
2834 }
2835 spin_unlock(root_lock);
79640a4c
ED
2836 if (unlikely(contended))
2837 spin_unlock(&q->busylock);
bbd8a0d3
KK
2838 return rc;
2839}
2840
5bc1421e
NH
2841#if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
2842static void skb_update_prio(struct sk_buff *skb)
2843{
6977a79d 2844 struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
5bc1421e 2845
91c68ce2
ED
2846 if (!skb->priority && skb->sk && map) {
2847 unsigned int prioidx = skb->sk->sk_cgrp_prioidx;
2848
2849 if (prioidx < map->priomap_len)
2850 skb->priority = map->priomap[prioidx];
2851 }
5bc1421e
NH
2852}
2853#else
2854#define skb_update_prio(skb)
2855#endif
2856
745e20f1 2857static DEFINE_PER_CPU(int, xmit_recursion);
11a766ce 2858#define RECURSION_LIMIT 10
745e20f1 2859
95603e22
MM
2860/**
2861 * dev_loopback_xmit - loop back @skb
2862 * @skb: buffer to transmit
2863 */
2864int dev_loopback_xmit(struct sk_buff *skb)
2865{
2866 skb_reset_mac_header(skb);
2867 __skb_pull(skb, skb_network_offset(skb));
2868 skb->pkt_type = PACKET_LOOPBACK;
2869 skb->ip_summed = CHECKSUM_UNNECESSARY;
2870 WARN_ON(!skb_dst(skb));
2871 skb_dst_force(skb);
2872 netif_rx_ni(skb);
2873 return 0;
2874}
2875EXPORT_SYMBOL(dev_loopback_xmit);
2876
d29f749e
DJ
2877/**
2878 * dev_queue_xmit - transmit a buffer
2879 * @skb: buffer to transmit
2880 *
2881 * Queue a buffer for transmission to a network device. The caller must
2882 * have set the device and priority and built the buffer before calling
2883 * this function. The function can be called from an interrupt.
2884 *
2885 * A negative errno code is returned on a failure. A success does not
2886 * guarantee the frame will be transmitted as it may be dropped due
2887 * to congestion or traffic shaping.
2888 *
2889 * -----------------------------------------------------------------------------------
2890 * I notice this method can also return errors from the queue disciplines,
2891 * including NET_XMIT_DROP, which is a positive value. So, errors can also
2892 * be positive.
2893 *
2894 * Regardless of the return value, the skb is consumed, so it is currently
2895 * difficult to retry a send to this method. (You can bump the ref count
2896 * before sending to hold a reference for retry if you are careful.)
2897 *
2898 * When calling this method, interrupts MUST be enabled. This is because
2899 * the BH enable code must have IRQs enabled so that it will not deadlock.
2900 * --BLG
2901 */
1da177e4
LT
2902int dev_queue_xmit(struct sk_buff *skb)
2903{
2904 struct net_device *dev = skb->dev;
dc2b4847 2905 struct netdev_queue *txq;
1da177e4
LT
2906 struct Qdisc *q;
2907 int rc = -ENOMEM;
2908
4ec93edb
YH
2909 /* Disable soft irqs for various locks below. Also
2910 * stops preemption for RCU.
1da177e4 2911 */
4ec93edb 2912 rcu_read_lock_bh();
1da177e4 2913
5bc1421e
NH
2914 skb_update_prio(skb);
2915
8c4c49df 2916 txq = netdev_pick_tx(dev, skb);
a898def2 2917 q = rcu_dereference_bh(txq->qdisc);
37437bb2 2918
1da177e4 2919#ifdef CONFIG_NET_CLS_ACT
d1b19dff 2920 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
1da177e4 2921#endif
cf66ba58 2922 trace_net_dev_queue(skb);
1da177e4 2923 if (q->enqueue) {
bbd8a0d3 2924 rc = __dev_xmit_skb(skb, q, dev, txq);
37437bb2 2925 goto out;
1da177e4
LT
2926 }
2927
2928 /* The device has no queue. Common case for software devices:
2929 loopback, all the sorts of tunnels...
2930
932ff279
HX
2931 Really, it is unlikely that netif_tx_lock protection is necessary
2932 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1da177e4
LT
2933 counters.)
2934 However, it is possible, that they rely on protection
2935 made by us here.
2936
2937 Check this and shot the lock. It is not prone from deadlocks.
2938 Either shot noqueue qdisc, it is even simpler 8)
2939 */
2940 if (dev->flags & IFF_UP) {
2941 int cpu = smp_processor_id(); /* ok because BHs are off */
2942
c773e847 2943 if (txq->xmit_lock_owner != cpu) {
1da177e4 2944
745e20f1
ED
2945 if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
2946 goto recursion_alert;
2947
c773e847 2948 HARD_TX_LOCK(dev, txq, cpu);
1da177e4 2949
73466498 2950 if (!netif_xmit_stopped(txq)) {
745e20f1 2951 __this_cpu_inc(xmit_recursion);
572a9d7b 2952 rc = dev_hard_start_xmit(skb, dev, txq);
745e20f1 2953 __this_cpu_dec(xmit_recursion);
572a9d7b 2954 if (dev_xmit_complete(rc)) {
c773e847 2955 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
2956 goto out;
2957 }
2958 }
c773e847 2959 HARD_TX_UNLOCK(dev, txq);
e87cc472
JP
2960 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
2961 dev->name);
1da177e4
LT
2962 } else {
2963 /* Recursion is detected! It is possible,
745e20f1
ED
2964 * unfortunately
2965 */
2966recursion_alert:
e87cc472
JP
2967 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
2968 dev->name);
1da177e4
LT
2969 }
2970 }
2971
2972 rc = -ENETDOWN;
d4828d85 2973 rcu_read_unlock_bh();
1da177e4 2974
1da177e4
LT
2975 kfree_skb(skb);
2976 return rc;
2977out:
d4828d85 2978 rcu_read_unlock_bh();
1da177e4
LT
2979 return rc;
2980}
d1b19dff 2981EXPORT_SYMBOL(dev_queue_xmit);
1da177e4
LT
2982
2983
2984/*=======================================================================
2985 Receiver routines
2986 =======================================================================*/
2987
6b2bedc3 2988int netdev_max_backlog __read_mostly = 1000;
c9e6bc64
ED
2989EXPORT_SYMBOL(netdev_max_backlog);
2990
3b098e2d 2991int netdev_tstamp_prequeue __read_mostly = 1;
6b2bedc3
SH
2992int netdev_budget __read_mostly = 300;
2993int weight_p __read_mostly = 64; /* old backlog weight */
1da177e4 2994
eecfd7c4
ED
2995/* Called with irq disabled */
2996static inline void ____napi_schedule(struct softnet_data *sd,
2997 struct napi_struct *napi)
2998{
2999 list_add_tail(&napi->poll_list, &sd->poll_list);
3000 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3001}
3002
0a9627f2 3003/*
bfb564e7 3004 * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
bdeab991
TH
3005 * and src/dst port numbers. Sets rxhash in skb to non-zero hash value
3006 * on success, zero indicates no valid hash. Also, sets l4_rxhash in skb
3007 * if hash is a canonical 4-tuple hash over transport ports.
0a9627f2 3008 */
bdeab991 3009void __skb_get_rxhash(struct sk_buff *skb)
0a9627f2 3010{
4504b861
ED
3011 struct flow_keys keys;
3012 u32 hash;
c6865cb3 3013
4504b861
ED
3014 if (!skb_flow_dissect(skb, &keys))
3015 return;
e971b722 3016
68622342 3017 if (keys.ports)
4504b861 3018 skb->l4_rxhash = 1;
0a9627f2 3019
b249dcb8 3020 /* get a consistent hash (same value on both flow directions) */
68622342
CG
3021 if (((__force u32)keys.dst < (__force u32)keys.src) ||
3022 (((__force u32)keys.dst == (__force u32)keys.src) &&
3023 ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
4504b861 3024 swap(keys.dst, keys.src);
68622342
CG
3025 swap(keys.port16[0], keys.port16[1]);
3026 }
0a9627f2 3027
4504b861
ED
3028 hash = jhash_3words((__force u32)keys.dst,
3029 (__force u32)keys.src,
3030 (__force u32)keys.ports, hashrnd);
bfb564e7
KK
3031 if (!hash)
3032 hash = 1;
3033
bdeab991 3034 skb->rxhash = hash;
bfb564e7
KK
3035}
3036EXPORT_SYMBOL(__skb_get_rxhash);
3037
3038#ifdef CONFIG_RPS
3039
3040/* One global table that all flow-based protocols share. */
6e3f7faf 3041struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
bfb564e7
KK
3042EXPORT_SYMBOL(rps_sock_flow_table);
3043
c5905afb 3044struct static_key rps_needed __read_mostly;
adc9300e 3045
c445477d
BH
3046static struct rps_dev_flow *
3047set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3048 struct rps_dev_flow *rflow, u16 next_cpu)
3049{
09994d1b 3050 if (next_cpu != RPS_NO_CPU) {
c445477d
BH
3051#ifdef CONFIG_RFS_ACCEL
3052 struct netdev_rx_queue *rxqueue;
3053 struct rps_dev_flow_table *flow_table;
3054 struct rps_dev_flow *old_rflow;
3055 u32 flow_id;
3056 u16 rxq_index;
3057 int rc;
3058
3059 /* Should we steer this flow to a different hardware queue? */
69a19ee6
BH
3060 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
3061 !(dev->features & NETIF_F_NTUPLE))
c445477d
BH
3062 goto out;
3063 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
3064 if (rxq_index == skb_get_rx_queue(skb))
3065 goto out;
3066
3067 rxqueue = dev->_rx + rxq_index;
3068 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3069 if (!flow_table)
3070 goto out;
3071 flow_id = skb->rxhash & flow_table->mask;
3072 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
3073 rxq_index, flow_id);
3074 if (rc < 0)
3075 goto out;
3076 old_rflow = rflow;
3077 rflow = &flow_table->flows[flow_id];
c445477d
BH
3078 rflow->filter = rc;
3079 if (old_rflow->filter == rflow->filter)
3080 old_rflow->filter = RPS_NO_FILTER;
3081 out:
3082#endif
3083 rflow->last_qtail =
09994d1b 3084 per_cpu(softnet_data, next_cpu).input_queue_head;
c445477d
BH
3085 }
3086
09994d1b 3087 rflow->cpu = next_cpu;
c445477d
BH
3088 return rflow;
3089}
3090
bfb564e7
KK
3091/*
3092 * get_rps_cpu is called from netif_receive_skb and returns the target
3093 * CPU from the RPS map of the receiving queue for a given skb.
3094 * rcu_read_lock must be held on entry.
3095 */
3096static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3097 struct rps_dev_flow **rflowp)
3098{
3099 struct netdev_rx_queue *rxqueue;
6e3f7faf 3100 struct rps_map *map;
bfb564e7
KK
3101 struct rps_dev_flow_table *flow_table;
3102 struct rps_sock_flow_table *sock_flow_table;
3103 int cpu = -1;
3104 u16 tcpu;
3105
3106 if (skb_rx_queue_recorded(skb)) {
3107 u16 index = skb_get_rx_queue(skb);
62fe0b40
BH
3108 if (unlikely(index >= dev->real_num_rx_queues)) {
3109 WARN_ONCE(dev->real_num_rx_queues > 1,
3110 "%s received packet on queue %u, but number "
3111 "of RX queues is %u\n",
3112 dev->name, index, dev->real_num_rx_queues);
bfb564e7
KK
3113 goto done;
3114 }
3115 rxqueue = dev->_rx + index;
3116 } else
3117 rxqueue = dev->_rx;
3118
6e3f7faf
ED
3119 map = rcu_dereference(rxqueue->rps_map);
3120 if (map) {
85875236 3121 if (map->len == 1 &&
33d480ce 3122 !rcu_access_pointer(rxqueue->rps_flow_table)) {
6febfca9
CG
3123 tcpu = map->cpus[0];
3124 if (cpu_online(tcpu))
3125 cpu = tcpu;
3126 goto done;
3127 }
33d480ce 3128 } else if (!rcu_access_pointer(rxqueue->rps_flow_table)) {
bfb564e7 3129 goto done;
6febfca9 3130 }
bfb564e7 3131
2d47b459 3132 skb_reset_network_header(skb);
bfb564e7
KK
3133 if (!skb_get_rxhash(skb))
3134 goto done;
3135
fec5e652
TH
3136 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3137 sock_flow_table = rcu_dereference(rps_sock_flow_table);
3138 if (flow_table && sock_flow_table) {
3139 u16 next_cpu;
3140 struct rps_dev_flow *rflow;
3141
3142 rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
3143 tcpu = rflow->cpu;
3144
3145 next_cpu = sock_flow_table->ents[skb->rxhash &
3146 sock_flow_table->mask];
3147
3148 /*
3149 * If the desired CPU (where last recvmsg was done) is
3150 * different from current CPU (one in the rx-queue flow
3151 * table entry), switch if one of the following holds:
3152 * - Current CPU is unset (equal to RPS_NO_CPU).
3153 * - Current CPU is offline.
3154 * - The current CPU's queue tail has advanced beyond the
3155 * last packet that was enqueued using this table entry.
3156 * This guarantees that all previous packets for the flow
3157 * have been dequeued, thus preserving in order delivery.
3158 */
3159 if (unlikely(tcpu != next_cpu) &&
3160 (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
3161 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
baefa31d
TH
3162 rflow->last_qtail)) >= 0)) {
3163 tcpu = next_cpu;
c445477d 3164 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
baefa31d 3165 }
c445477d 3166
fec5e652
TH
3167 if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
3168 *rflowp = rflow;
3169 cpu = tcpu;
3170 goto done;
3171 }
3172 }
3173
0a9627f2 3174 if (map) {
fec5e652 3175 tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
0a9627f2
TH
3176
3177 if (cpu_online(tcpu)) {
3178 cpu = tcpu;
3179 goto done;
3180 }
3181 }
3182
3183done:
0a9627f2
TH
3184 return cpu;
3185}
3186
c445477d
BH
3187#ifdef CONFIG_RFS_ACCEL
3188
3189/**
3190 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
3191 * @dev: Device on which the filter was set
3192 * @rxq_index: RX queue index
3193 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
3194 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
3195 *
3196 * Drivers that implement ndo_rx_flow_steer() should periodically call
3197 * this function for each installed filter and remove the filters for
3198 * which it returns %true.
3199 */
3200bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
3201 u32 flow_id, u16 filter_id)
3202{
3203 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
3204 struct rps_dev_flow_table *flow_table;
3205 struct rps_dev_flow *rflow;
3206 bool expire = true;
3207 int cpu;
3208
3209 rcu_read_lock();
3210 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3211 if (flow_table && flow_id <= flow_table->mask) {
3212 rflow = &flow_table->flows[flow_id];
3213 cpu = ACCESS_ONCE(rflow->cpu);
3214 if (rflow->filter == filter_id && cpu != RPS_NO_CPU &&
3215 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
3216 rflow->last_qtail) <
3217 (int)(10 * flow_table->mask)))
3218 expire = false;
3219 }
3220 rcu_read_unlock();
3221 return expire;
3222}
3223EXPORT_SYMBOL(rps_may_expire_flow);
3224
3225#endif /* CONFIG_RFS_ACCEL */
3226
0a9627f2 3227/* Called from hardirq (IPI) context */
e36fa2f7 3228static void rps_trigger_softirq(void *data)
0a9627f2 3229{
e36fa2f7
ED
3230 struct softnet_data *sd = data;
3231
eecfd7c4 3232 ____napi_schedule(sd, &sd->backlog);
dee42870 3233 sd->received_rps++;
0a9627f2 3234}
e36fa2f7 3235
fec5e652 3236#endif /* CONFIG_RPS */
0a9627f2 3237
e36fa2f7
ED
3238/*
3239 * Check if this softnet_data structure is another cpu one
3240 * If yes, queue it to our IPI list and return 1
3241 * If no, return 0
3242 */
3243static int rps_ipi_queued(struct softnet_data *sd)
3244{
3245#ifdef CONFIG_RPS
3246 struct softnet_data *mysd = &__get_cpu_var(softnet_data);
3247
3248 if (sd != mysd) {
3249 sd->rps_ipi_next = mysd->rps_ipi_list;
3250 mysd->rps_ipi_list = sd;
3251
3252 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3253 return 1;
3254 }
3255#endif /* CONFIG_RPS */
3256 return 0;
3257}
3258
0a9627f2
TH
3259/*
3260 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
3261 * queue (may be a remote CPU queue).
3262 */
fec5e652
TH
3263static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
3264 unsigned int *qtail)
0a9627f2 3265{
e36fa2f7 3266 struct softnet_data *sd;
0a9627f2
TH
3267 unsigned long flags;
3268
e36fa2f7 3269 sd = &per_cpu(softnet_data, cpu);
0a9627f2
TH
3270
3271 local_irq_save(flags);
0a9627f2 3272
e36fa2f7 3273 rps_lock(sd);
6e7676c1
CG
3274 if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
3275 if (skb_queue_len(&sd->input_pkt_queue)) {
0a9627f2 3276enqueue:
e36fa2f7 3277 __skb_queue_tail(&sd->input_pkt_queue, skb);
76cc8b13 3278 input_queue_tail_incr_save(sd, qtail);
e36fa2f7 3279 rps_unlock(sd);
152102c7 3280 local_irq_restore(flags);
0a9627f2
TH
3281 return NET_RX_SUCCESS;
3282 }
3283
ebda37c2
ED
3284 /* Schedule NAPI for backlog device
3285 * We can use non atomic operation since we own the queue lock
3286 */
3287 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
e36fa2f7 3288 if (!rps_ipi_queued(sd))
eecfd7c4 3289 ____napi_schedule(sd, &sd->backlog);
0a9627f2
TH
3290 }
3291 goto enqueue;
3292 }
3293
dee42870 3294 sd->dropped++;
e36fa2f7 3295 rps_unlock(sd);
0a9627f2 3296
0a9627f2
TH
3297 local_irq_restore(flags);
3298
caf586e5 3299 atomic_long_inc(&skb->dev->rx_dropped);
0a9627f2
TH
3300 kfree_skb(skb);
3301 return NET_RX_DROP;
3302}
1da177e4 3303
1da177e4
LT
3304/**
3305 * netif_rx - post buffer to the network code
3306 * @skb: buffer to post
3307 *
3308 * This function receives a packet from a device driver and queues it for
3309 * the upper (protocol) levels to process. It always succeeds. The buffer
3310 * may be dropped during processing for congestion control or by the
3311 * protocol layers.
3312 *
3313 * return values:
3314 * NET_RX_SUCCESS (no congestion)
1da177e4
LT
3315 * NET_RX_DROP (packet was dropped)
3316 *
3317 */
3318
3319int netif_rx(struct sk_buff *skb)
3320{
b0e28f1e 3321 int ret;
1da177e4
LT
3322
3323 /* if netpoll wants it, pretend we never saw it */
3324 if (netpoll_rx(skb))
3325 return NET_RX_DROP;
3326
588f0330 3327 net_timestamp_check(netdev_tstamp_prequeue, skb);
1da177e4 3328
cf66ba58 3329 trace_netif_rx(skb);
df334545 3330#ifdef CONFIG_RPS
c5905afb 3331 if (static_key_false(&rps_needed)) {
fec5e652 3332 struct rps_dev_flow voidflow, *rflow = &voidflow;
b0e28f1e
ED
3333 int cpu;
3334
cece1945 3335 preempt_disable();
b0e28f1e 3336 rcu_read_lock();
fec5e652
TH
3337
3338 cpu = get_rps_cpu(skb->dev, skb, &rflow);
b0e28f1e
ED
3339 if (cpu < 0)
3340 cpu = smp_processor_id();
fec5e652
TH
3341
3342 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3343
b0e28f1e 3344 rcu_read_unlock();
cece1945 3345 preempt_enable();
adc9300e
ED
3346 } else
3347#endif
fec5e652
TH
3348 {
3349 unsigned int qtail;
3350 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
3351 put_cpu();
3352 }
b0e28f1e 3353 return ret;
1da177e4 3354}
d1b19dff 3355EXPORT_SYMBOL(netif_rx);
1da177e4
LT
3356
3357int netif_rx_ni(struct sk_buff *skb)
3358{
3359 int err;
3360
3361 preempt_disable();
3362 err = netif_rx(skb);
3363 if (local_softirq_pending())
3364 do_softirq();
3365 preempt_enable();
3366
3367 return err;
3368}
1da177e4
LT
3369EXPORT_SYMBOL(netif_rx_ni);
3370
1da177e4
LT
3371static void net_tx_action(struct softirq_action *h)
3372{
3373 struct softnet_data *sd = &__get_cpu_var(softnet_data);
3374
3375 if (sd->completion_queue) {
3376 struct sk_buff *clist;
3377
3378 local_irq_disable();
3379 clist = sd->completion_queue;
3380 sd->completion_queue = NULL;
3381 local_irq_enable();
3382
3383 while (clist) {
3384 struct sk_buff *skb = clist;
3385 clist = clist->next;
3386
547b792c 3387 WARN_ON(atomic_read(&skb->users));
07dc22e7 3388 trace_kfree_skb(skb, net_tx_action);
1da177e4
LT
3389 __kfree_skb(skb);
3390 }
3391 }
3392
3393 if (sd->output_queue) {
37437bb2 3394 struct Qdisc *head;
1da177e4
LT
3395
3396 local_irq_disable();
3397 head = sd->output_queue;
3398 sd->output_queue = NULL;
a9cbd588 3399 sd->output_queue_tailp = &sd->output_queue;
1da177e4
LT
3400 local_irq_enable();
3401
3402 while (head) {
37437bb2
DM
3403 struct Qdisc *q = head;
3404 spinlock_t *root_lock;
3405
1da177e4
LT
3406 head = head->next_sched;
3407
5fb66229 3408 root_lock = qdisc_lock(q);
37437bb2 3409 if (spin_trylock(root_lock)) {
def82a1d
JP
3410 smp_mb__before_clear_bit();
3411 clear_bit(__QDISC_STATE_SCHED,
3412 &q->state);
37437bb2
DM
3413 qdisc_run(q);
3414 spin_unlock(root_lock);
1da177e4 3415 } else {
195648bb 3416 if (!test_bit(__QDISC_STATE_DEACTIVATED,
e8a83e10 3417 &q->state)) {
195648bb 3418 __netif_reschedule(q);
e8a83e10
JP
3419 } else {
3420 smp_mb__before_clear_bit();
3421 clear_bit(__QDISC_STATE_SCHED,
3422 &q->state);
3423 }
1da177e4
LT
3424 }
3425 }
3426 }
3427}
3428
ab95bfe0
JP
3429#if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
3430 (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
da678292
MM
3431/* This hook is defined here for ATM LANE */
3432int (*br_fdb_test_addr_hook)(struct net_device *dev,
3433 unsigned char *addr) __read_mostly;
4fb019a0 3434EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
da678292 3435#endif
1da177e4 3436
1da177e4
LT
3437#ifdef CONFIG_NET_CLS_ACT
3438/* TODO: Maybe we should just force sch_ingress to be compiled in
3439 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
3440 * a compare and 2 stores extra right now if we dont have it on
3441 * but have CONFIG_NET_CLS_ACT
25985edc
LDM
3442 * NOTE: This doesn't stop any functionality; if you dont have
3443 * the ingress scheduler, you just can't add policies on ingress.
1da177e4
LT
3444 *
3445 */
24824a09 3446static int ing_filter(struct sk_buff *skb, struct netdev_queue *rxq)
1da177e4 3447{
1da177e4 3448 struct net_device *dev = skb->dev;
f697c3e8 3449 u32 ttl = G_TC_RTTL(skb->tc_verd);
555353cf
DM
3450 int result = TC_ACT_OK;
3451 struct Qdisc *q;
4ec93edb 3452
de384830 3453 if (unlikely(MAX_RED_LOOP < ttl++)) {
e87cc472
JP
3454 net_warn_ratelimited("Redir loop detected Dropping packet (%d->%d)\n",
3455 skb->skb_iif, dev->ifindex);
f697c3e8
HX
3456 return TC_ACT_SHOT;
3457 }
1da177e4 3458
f697c3e8
HX
3459 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
3460 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
1da177e4 3461
83874000 3462 q = rxq->qdisc;
8d50b53d 3463 if (q != &noop_qdisc) {
83874000 3464 spin_lock(qdisc_lock(q));
a9312ae8
DM
3465 if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
3466 result = qdisc_enqueue_root(skb, q);
83874000
DM
3467 spin_unlock(qdisc_lock(q));
3468 }
f697c3e8
HX
3469
3470 return result;
3471}
86e65da9 3472
f697c3e8
HX
3473static inline struct sk_buff *handle_ing(struct sk_buff *skb,
3474 struct packet_type **pt_prev,
3475 int *ret, struct net_device *orig_dev)
3476{
24824a09
ED
3477 struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue);
3478
3479 if (!rxq || rxq->qdisc == &noop_qdisc)
f697c3e8 3480 goto out;
1da177e4 3481
f697c3e8
HX
3482 if (*pt_prev) {
3483 *ret = deliver_skb(skb, *pt_prev, orig_dev);
3484 *pt_prev = NULL;
1da177e4
LT
3485 }
3486
24824a09 3487 switch (ing_filter(skb, rxq)) {
f697c3e8
HX
3488 case TC_ACT_SHOT:
3489 case TC_ACT_STOLEN:
3490 kfree_skb(skb);
3491 return NULL;
3492 }
3493
3494out:
3495 skb->tc_verd = 0;
3496 return skb;
1da177e4
LT
3497}
3498#endif
3499
ab95bfe0
JP
3500/**
3501 * netdev_rx_handler_register - register receive handler
3502 * @dev: device to register a handler for
3503 * @rx_handler: receive handler to register
93e2c32b 3504 * @rx_handler_data: data pointer that is used by rx handler
ab95bfe0
JP
3505 *
3506 * Register a receive hander for a device. This handler will then be
3507 * called from __netif_receive_skb. A negative errno code is returned
3508 * on a failure.
3509 *
3510 * The caller must hold the rtnl_mutex.
8a4eb573
JP
3511 *
3512 * For a general description of rx_handler, see enum rx_handler_result.
ab95bfe0
JP
3513 */
3514int netdev_rx_handler_register(struct net_device *dev,
93e2c32b
JP
3515 rx_handler_func_t *rx_handler,
3516 void *rx_handler_data)
ab95bfe0
JP
3517{
3518 ASSERT_RTNL();
3519
3520 if (dev->rx_handler)
3521 return -EBUSY;
3522
93e2c32b 3523 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
ab95bfe0
JP
3524 rcu_assign_pointer(dev->rx_handler, rx_handler);
3525
3526 return 0;
3527}
3528EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
3529
3530/**
3531 * netdev_rx_handler_unregister - unregister receive handler
3532 * @dev: device to unregister a handler from
3533 *
3534 * Unregister a receive hander from a device.
3535 *
3536 * The caller must hold the rtnl_mutex.
3537 */
3538void netdev_rx_handler_unregister(struct net_device *dev)
3539{
3540
3541 ASSERT_RTNL();
a9b3cd7f
SH
3542 RCU_INIT_POINTER(dev->rx_handler, NULL);
3543 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
ab95bfe0
JP
3544}
3545EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
3546
b4b9e355
MG
3547/*
3548 * Limit the use of PFMEMALLOC reserves to those protocols that implement
3549 * the special handling of PFMEMALLOC skbs.
3550 */
3551static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
3552{
3553 switch (skb->protocol) {
3554 case __constant_htons(ETH_P_ARP):
3555 case __constant_htons(ETH_P_IP):
3556 case __constant_htons(ETH_P_IPV6):
3557 case __constant_htons(ETH_P_8021Q):
3558 return true;
3559 default:
3560 return false;
3561 }
3562}
3563
10f744d2 3564static int __netif_receive_skb(struct sk_buff *skb)
1da177e4
LT
3565{
3566 struct packet_type *ptype, *pt_prev;
ab95bfe0 3567 rx_handler_func_t *rx_handler;
f2ccd8fa 3568 struct net_device *orig_dev;
63d8ea7f 3569 struct net_device *null_or_dev;
8a4eb573 3570 bool deliver_exact = false;
1da177e4 3571 int ret = NET_RX_DROP;
252e3346 3572 __be16 type;
b4b9e355 3573 unsigned long pflags = current->flags;
1da177e4 3574
588f0330 3575 net_timestamp_check(!netdev_tstamp_prequeue, skb);
81bbb3d4 3576
cf66ba58 3577 trace_netif_receive_skb(skb);
9b22ea56 3578
b4b9e355
MG
3579 /*
3580 * PFMEMALLOC skbs are special, they should
3581 * - be delivered to SOCK_MEMALLOC sockets only
3582 * - stay away from userspace
3583 * - have bounded memory usage
3584 *
3585 * Use PF_MEMALLOC as this saves us from propagating the allocation
3586 * context down to all allocation sites.
3587 */
3588 if (sk_memalloc_socks() && skb_pfmemalloc(skb))
3589 current->flags |= PF_MEMALLOC;
3590
1da177e4 3591 /* if we've gotten here through NAPI, check netpoll */
bea3348e 3592 if (netpoll_receive_skb(skb))
b4b9e355 3593 goto out;
1da177e4 3594
cc9bd5ce 3595 orig_dev = skb->dev;
8f903c70 3596
c1d2bbe1 3597 skb_reset_network_header(skb);
fda55eca
ED
3598 if (!skb_transport_header_was_set(skb))
3599 skb_reset_transport_header(skb);
0b5c9db1 3600 skb_reset_mac_len(skb);
1da177e4
LT
3601
3602 pt_prev = NULL;
3603
3604 rcu_read_lock();
3605
63d8ea7f 3606another_round:
b6858177 3607 skb->skb_iif = skb->dev->ifindex;
63d8ea7f
DM
3608
3609 __this_cpu_inc(softnet_data.processed);
3610
bcc6d479
JP
3611 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
3612 skb = vlan_untag(skb);
3613 if (unlikely(!skb))
b4b9e355 3614 goto unlock;
bcc6d479
JP
3615 }
3616
1da177e4
LT
3617#ifdef CONFIG_NET_CLS_ACT
3618 if (skb->tc_verd & TC_NCLS) {
3619 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
3620 goto ncls;
3621 }
3622#endif
3623
b4b9e355
MG
3624 if (sk_memalloc_socks() && skb_pfmemalloc(skb))
3625 goto skip_taps;
3626
1da177e4 3627 list_for_each_entry_rcu(ptype, &ptype_all, list) {
63d8ea7f 3628 if (!ptype->dev || ptype->dev == skb->dev) {
4ec93edb 3629 if (pt_prev)
f2ccd8fa 3630 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
3631 pt_prev = ptype;
3632 }
3633 }
3634
b4b9e355 3635skip_taps:
1da177e4 3636#ifdef CONFIG_NET_CLS_ACT
f697c3e8
HX
3637 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
3638 if (!skb)
b4b9e355 3639 goto unlock;
1da177e4
LT
3640ncls:
3641#endif
3642
b4b9e355
MG
3643 if (sk_memalloc_socks() && skb_pfmemalloc(skb)
3644 && !skb_pfmemalloc_protocol(skb))
3645 goto drop;
3646
2425717b
JF
3647 if (vlan_tx_tag_present(skb)) {
3648 if (pt_prev) {
3649 ret = deliver_skb(skb, pt_prev, orig_dev);
3650 pt_prev = NULL;
3651 }
48cc32d3 3652 if (vlan_do_receive(&skb))
2425717b
JF
3653 goto another_round;
3654 else if (unlikely(!skb))
b4b9e355 3655 goto unlock;
2425717b
JF
3656 }
3657
48cc32d3 3658 rx_handler = rcu_dereference(skb->dev->rx_handler);
ab95bfe0
JP
3659 if (rx_handler) {
3660 if (pt_prev) {
3661 ret = deliver_skb(skb, pt_prev, orig_dev);
3662 pt_prev = NULL;
3663 }
8a4eb573
JP
3664 switch (rx_handler(&skb)) {
3665 case RX_HANDLER_CONSUMED:
b4b9e355 3666 goto unlock;
8a4eb573 3667 case RX_HANDLER_ANOTHER:
63d8ea7f 3668 goto another_round;
8a4eb573
JP
3669 case RX_HANDLER_EXACT:
3670 deliver_exact = true;
3671 case RX_HANDLER_PASS:
3672 break;
3673 default:
3674 BUG();
3675 }
ab95bfe0 3676 }
1da177e4 3677
48cc32d3
FZ
3678 if (vlan_tx_nonzero_tag_present(skb))
3679 skb->pkt_type = PACKET_OTHERHOST;
3680
63d8ea7f 3681 /* deliver only exact match when indicated */
8a4eb573 3682 null_or_dev = deliver_exact ? skb->dev : NULL;
1f3c8804 3683
1da177e4 3684 type = skb->protocol;
82d8a867
PE
3685 list_for_each_entry_rcu(ptype,
3686 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
63d8ea7f 3687 if (ptype->type == type &&
e3f48d37
JP
3688 (ptype->dev == null_or_dev || ptype->dev == skb->dev ||
3689 ptype->dev == orig_dev)) {
4ec93edb 3690 if (pt_prev)
f2ccd8fa 3691 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
3692 pt_prev = ptype;
3693 }
3694 }
3695
3696 if (pt_prev) {
1080e512 3697 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
0e698bf6 3698 goto drop;
1080e512
MT
3699 else
3700 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1da177e4 3701 } else {
b4b9e355 3702drop:
caf586e5 3703 atomic_long_inc(&skb->dev->rx_dropped);
1da177e4
LT
3704 kfree_skb(skb);
3705 /* Jamal, now you will not able to escape explaining
3706 * me how you were going to use this. :-)
3707 */
3708 ret = NET_RX_DROP;
3709 }
3710
b4b9e355 3711unlock:
1da177e4 3712 rcu_read_unlock();
b4b9e355
MG
3713out:
3714 tsk_restore_flags(current, pflags, PF_MEMALLOC);
1da177e4
LT
3715 return ret;
3716}
0a9627f2
TH
3717
3718/**
3719 * netif_receive_skb - process receive buffer from network
3720 * @skb: buffer to process
3721 *
3722 * netif_receive_skb() is the main receive data processing function.
3723 * It always succeeds. The buffer may be dropped during processing
3724 * for congestion control or by the protocol layers.
3725 *
3726 * This function may only be called from softirq context and interrupts
3727 * should be enabled.
3728 *
3729 * Return values (usually ignored):
3730 * NET_RX_SUCCESS: no congestion
3731 * NET_RX_DROP: packet was dropped
3732 */
3733int netif_receive_skb(struct sk_buff *skb)
3734{
588f0330 3735 net_timestamp_check(netdev_tstamp_prequeue, skb);
3b098e2d 3736
c1f19b51
RC
3737 if (skb_defer_rx_timestamp(skb))
3738 return NET_RX_SUCCESS;
3739
df334545 3740#ifdef CONFIG_RPS
c5905afb 3741 if (static_key_false(&rps_needed)) {
3b098e2d
ED
3742 struct rps_dev_flow voidflow, *rflow = &voidflow;
3743 int cpu, ret;
fec5e652 3744
3b098e2d
ED
3745 rcu_read_lock();
3746
3747 cpu = get_rps_cpu(skb->dev, skb, &rflow);
0a9627f2 3748
3b098e2d
ED
3749 if (cpu >= 0) {
3750 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3751 rcu_read_unlock();
adc9300e 3752 return ret;
3b098e2d 3753 }
adc9300e 3754 rcu_read_unlock();
fec5e652 3755 }
1e94d72f 3756#endif
adc9300e 3757 return __netif_receive_skb(skb);
0a9627f2 3758}
d1b19dff 3759EXPORT_SYMBOL(netif_receive_skb);
1da177e4 3760
88751275
ED
3761/* Network device is going away, flush any packets still pending
3762 * Called with irqs disabled.
3763 */
152102c7 3764static void flush_backlog(void *arg)
6e583ce5 3765{
152102c7 3766 struct net_device *dev = arg;
e36fa2f7 3767 struct softnet_data *sd = &__get_cpu_var(softnet_data);
6e583ce5
SH
3768 struct sk_buff *skb, *tmp;
3769
e36fa2f7 3770 rps_lock(sd);
6e7676c1 3771 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
6e583ce5 3772 if (skb->dev == dev) {
e36fa2f7 3773 __skb_unlink(skb, &sd->input_pkt_queue);
6e583ce5 3774 kfree_skb(skb);
76cc8b13 3775 input_queue_head_incr(sd);
6e583ce5 3776 }
6e7676c1 3777 }
e36fa2f7 3778 rps_unlock(sd);
6e7676c1
CG
3779
3780 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
3781 if (skb->dev == dev) {
3782 __skb_unlink(skb, &sd->process_queue);
3783 kfree_skb(skb);
76cc8b13 3784 input_queue_head_incr(sd);
6e7676c1
CG
3785 }
3786 }
6e583ce5
SH
3787}
3788
d565b0a1
HX
3789static int napi_gro_complete(struct sk_buff *skb)
3790{
22061d80 3791 struct packet_offload *ptype;
d565b0a1 3792 __be16 type = skb->protocol;
22061d80 3793 struct list_head *head = &offload_base;
d565b0a1
HX
3794 int err = -ENOENT;
3795
c3c7c254
ED
3796 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
3797
fc59f9a3
HX
3798 if (NAPI_GRO_CB(skb)->count == 1) {
3799 skb_shinfo(skb)->gso_size = 0;
d565b0a1 3800 goto out;
fc59f9a3 3801 }
d565b0a1
HX
3802
3803 rcu_read_lock();
3804 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 3805 if (ptype->type != type || !ptype->callbacks.gro_complete)
d565b0a1
HX
3806 continue;
3807
f191a1d1 3808 err = ptype->callbacks.gro_complete(skb);
d565b0a1
HX
3809 break;
3810 }
3811 rcu_read_unlock();
3812
3813 if (err) {
3814 WARN_ON(&ptype->list == head);
3815 kfree_skb(skb);
3816 return NET_RX_SUCCESS;
3817 }
3818
3819out:
d565b0a1
HX
3820 return netif_receive_skb(skb);
3821}
3822
2e71a6f8
ED
3823/* napi->gro_list contains packets ordered by age.
3824 * youngest packets at the head of it.
3825 * Complete skbs in reverse order to reduce latencies.
3826 */
3827void napi_gro_flush(struct napi_struct *napi, bool flush_old)
d565b0a1 3828{
2e71a6f8 3829 struct sk_buff *skb, *prev = NULL;
d565b0a1 3830
2e71a6f8
ED
3831 /* scan list and build reverse chain */
3832 for (skb = napi->gro_list; skb != NULL; skb = skb->next) {
3833 skb->prev = prev;
3834 prev = skb;
3835 }
3836
3837 for (skb = prev; skb; skb = prev) {
d565b0a1 3838 skb->next = NULL;
2e71a6f8
ED
3839
3840 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
3841 return;
3842
3843 prev = skb->prev;
d565b0a1 3844 napi_gro_complete(skb);
2e71a6f8 3845 napi->gro_count--;
d565b0a1
HX
3846 }
3847
3848 napi->gro_list = NULL;
3849}
86cac58b 3850EXPORT_SYMBOL(napi_gro_flush);
d565b0a1 3851
89c5fa33
ED
3852static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb)
3853{
3854 struct sk_buff *p;
3855 unsigned int maclen = skb->dev->hard_header_len;
3856
3857 for (p = napi->gro_list; p; p = p->next) {
3858 unsigned long diffs;
3859
3860 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
3861 diffs |= p->vlan_tci ^ skb->vlan_tci;
3862 if (maclen == ETH_HLEN)
3863 diffs |= compare_ether_header(skb_mac_header(p),
3864 skb_gro_mac_header(skb));
3865 else if (!diffs)
3866 diffs = memcmp(skb_mac_header(p),
3867 skb_gro_mac_header(skb),
3868 maclen);
3869 NAPI_GRO_CB(p)->same_flow = !diffs;
3870 NAPI_GRO_CB(p)->flush = 0;
3871 }
3872}
3873
bb728820 3874static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1
HX
3875{
3876 struct sk_buff **pp = NULL;
22061d80 3877 struct packet_offload *ptype;
d565b0a1 3878 __be16 type = skb->protocol;
22061d80 3879 struct list_head *head = &offload_base;
0da2afd5 3880 int same_flow;
d565b0a1 3881 int mac_len;
5b252f0c 3882 enum gro_result ret;
d565b0a1 3883
ce9e76c8 3884 if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
d565b0a1
HX
3885 goto normal;
3886
21dc3301 3887 if (skb_is_gso(skb) || skb_has_frag_list(skb))
f17f5c91
HX
3888 goto normal;
3889
89c5fa33
ED
3890 gro_list_prepare(napi, skb);
3891
d565b0a1
HX
3892 rcu_read_lock();
3893 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 3894 if (ptype->type != type || !ptype->callbacks.gro_receive)
d565b0a1
HX
3895 continue;
3896
86911732 3897 skb_set_network_header(skb, skb_gro_offset(skb));
d565b0a1
HX
3898 mac_len = skb->network_header - skb->mac_header;
3899 skb->mac_len = mac_len;
3900 NAPI_GRO_CB(skb)->same_flow = 0;
3901 NAPI_GRO_CB(skb)->flush = 0;
5d38a079 3902 NAPI_GRO_CB(skb)->free = 0;
d565b0a1 3903
f191a1d1 3904 pp = ptype->callbacks.gro_receive(&napi->gro_list, skb);
d565b0a1
HX
3905 break;
3906 }
3907 rcu_read_unlock();
3908
3909 if (&ptype->list == head)
3910 goto normal;
3911
0da2afd5 3912 same_flow = NAPI_GRO_CB(skb)->same_flow;
5d0d9be8 3913 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
0da2afd5 3914
d565b0a1
HX
3915 if (pp) {
3916 struct sk_buff *nskb = *pp;
3917
3918 *pp = nskb->next;
3919 nskb->next = NULL;
3920 napi_gro_complete(nskb);
4ae5544f 3921 napi->gro_count--;
d565b0a1
HX
3922 }
3923
0da2afd5 3924 if (same_flow)
d565b0a1
HX
3925 goto ok;
3926
4ae5544f 3927 if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
d565b0a1 3928 goto normal;
d565b0a1 3929
4ae5544f 3930 napi->gro_count++;
d565b0a1 3931 NAPI_GRO_CB(skb)->count = 1;
2e71a6f8 3932 NAPI_GRO_CB(skb)->age = jiffies;
86911732 3933 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
d565b0a1
HX
3934 skb->next = napi->gro_list;
3935 napi->gro_list = skb;
5d0d9be8 3936 ret = GRO_HELD;
d565b0a1 3937
ad0f9904 3938pull:
cb18978c
HX
3939 if (skb_headlen(skb) < skb_gro_offset(skb)) {
3940 int grow = skb_gro_offset(skb) - skb_headlen(skb);
3941
3942 BUG_ON(skb->end - skb->tail < grow);
3943
3944 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
3945
3946 skb->tail += grow;
3947 skb->data_len -= grow;
3948
3949 skb_shinfo(skb)->frags[0].page_offset += grow;
9e903e08 3950 skb_frag_size_sub(&skb_shinfo(skb)->frags[0], grow);
cb18978c 3951
9e903e08 3952 if (unlikely(!skb_frag_size(&skb_shinfo(skb)->frags[0]))) {
ea2ab693 3953 skb_frag_unref(skb, 0);
cb18978c
HX
3954 memmove(skb_shinfo(skb)->frags,
3955 skb_shinfo(skb)->frags + 1,
e5093aec 3956 --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
cb18978c 3957 }
ad0f9904
HX
3958 }
3959
d565b0a1 3960ok:
5d0d9be8 3961 return ret;
d565b0a1
HX
3962
3963normal:
ad0f9904
HX
3964 ret = GRO_NORMAL;
3965 goto pull;
5d38a079 3966}
96e93eab 3967
5d38a079 3968
bb728820 3969static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
5d38a079 3970{
5d0d9be8
HX
3971 switch (ret) {
3972 case GRO_NORMAL:
c7c4b3b6
BH
3973 if (netif_receive_skb(skb))
3974 ret = GRO_DROP;
3975 break;
5d38a079 3976
5d0d9be8 3977 case GRO_DROP:
5d38a079
HX
3978 kfree_skb(skb);
3979 break;
5b252f0c 3980
daa86548 3981 case GRO_MERGED_FREE:
d7e8883c
ED
3982 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
3983 kmem_cache_free(skbuff_head_cache, skb);
3984 else
3985 __kfree_skb(skb);
daa86548
ED
3986 break;
3987
5b252f0c
BH
3988 case GRO_HELD:
3989 case GRO_MERGED:
3990 break;
5d38a079
HX
3991 }
3992
c7c4b3b6 3993 return ret;
5d0d9be8 3994}
5d0d9be8 3995
ca07e43e 3996static void skb_gro_reset_offset(struct sk_buff *skb)
78a478d0 3997{
ca07e43e
ED
3998 const struct skb_shared_info *pinfo = skb_shinfo(skb);
3999 const skb_frag_t *frag0 = &pinfo->frags[0];
4000
78a478d0
HX
4001 NAPI_GRO_CB(skb)->data_offset = 0;
4002 NAPI_GRO_CB(skb)->frag0 = NULL;
7489594c 4003 NAPI_GRO_CB(skb)->frag0_len = 0;
78a478d0 4004
78d3fd0b 4005 if (skb->mac_header == skb->tail &&
ca07e43e
ED
4006 pinfo->nr_frags &&
4007 !PageHighMem(skb_frag_page(frag0))) {
4008 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
4009 NAPI_GRO_CB(skb)->frag0_len = skb_frag_size(frag0);
7489594c 4010 }
78a478d0 4011}
78a478d0 4012
c7c4b3b6 4013gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
5d0d9be8 4014{
86911732
HX
4015 skb_gro_reset_offset(skb);
4016
89c5fa33 4017 return napi_skb_finish(dev_gro_receive(napi, skb), skb);
d565b0a1
HX
4018}
4019EXPORT_SYMBOL(napi_gro_receive);
4020
d0c2b0d2 4021static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
96e93eab 4022{
96e93eab 4023 __skb_pull(skb, skb_headlen(skb));
2a2a459e
ED
4024 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
4025 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
3701e513 4026 skb->vlan_tci = 0;
66c46d74 4027 skb->dev = napi->dev;
6d152e23 4028 skb->skb_iif = 0;
96e93eab
HX
4029
4030 napi->skb = skb;
4031}
96e93eab 4032
76620aaf 4033struct sk_buff *napi_get_frags(struct napi_struct *napi)
5d38a079 4034{
5d38a079 4035 struct sk_buff *skb = napi->skb;
5d38a079
HX
4036
4037 if (!skb) {
89d71a66
ED
4038 skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
4039 if (skb)
4040 napi->skb = skb;
80595d59 4041 }
96e93eab
HX
4042 return skb;
4043}
76620aaf 4044EXPORT_SYMBOL(napi_get_frags);
96e93eab 4045
bb728820 4046static gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
c7c4b3b6 4047 gro_result_t ret)
96e93eab 4048{
5d0d9be8
HX
4049 switch (ret) {
4050 case GRO_NORMAL:
86911732 4051 case GRO_HELD:
e76b69cc 4052 skb->protocol = eth_type_trans(skb, skb->dev);
86911732 4053
c7c4b3b6
BH
4054 if (ret == GRO_HELD)
4055 skb_gro_pull(skb, -ETH_HLEN);
4056 else if (netif_receive_skb(skb))
4057 ret = GRO_DROP;
86911732 4058 break;
5d38a079 4059
5d0d9be8 4060 case GRO_DROP:
5d0d9be8
HX
4061 case GRO_MERGED_FREE:
4062 napi_reuse_skb(napi, skb);
4063 break;
5b252f0c
BH
4064
4065 case GRO_MERGED:
4066 break;
5d0d9be8 4067 }
5d38a079 4068
c7c4b3b6 4069 return ret;
5d38a079 4070}
5d0d9be8 4071
4adb9c4a 4072static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
76620aaf
HX
4073{
4074 struct sk_buff *skb = napi->skb;
4075 struct ethhdr *eth;
a5b1cf28
HX
4076 unsigned int hlen;
4077 unsigned int off;
76620aaf
HX
4078
4079 napi->skb = NULL;
4080
4081 skb_reset_mac_header(skb);
4082 skb_gro_reset_offset(skb);
4083
a5b1cf28
HX
4084 off = skb_gro_offset(skb);
4085 hlen = off + sizeof(*eth);
4086 eth = skb_gro_header_fast(skb, off);
4087 if (skb_gro_header_hard(skb, hlen)) {
4088 eth = skb_gro_header_slow(skb, hlen, off);
4089 if (unlikely(!eth)) {
4090 napi_reuse_skb(napi, skb);
4091 skb = NULL;
4092 goto out;
4093 }
76620aaf
HX
4094 }
4095
4096 skb_gro_pull(skb, sizeof(*eth));
4097
4098 /*
4099 * This works because the only protocols we care about don't require
4100 * special handling. We'll fix it up properly at the end.
4101 */
4102 skb->protocol = eth->h_proto;
4103
4104out:
4105 return skb;
4106}
76620aaf 4107
c7c4b3b6 4108gro_result_t napi_gro_frags(struct napi_struct *napi)
5d0d9be8 4109{
76620aaf 4110 struct sk_buff *skb = napi_frags_skb(napi);
5d0d9be8
HX
4111
4112 if (!skb)
c7c4b3b6 4113 return GRO_DROP;
5d0d9be8 4114
89c5fa33 4115 return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
5d0d9be8 4116}
5d38a079
HX
4117EXPORT_SYMBOL(napi_gro_frags);
4118
e326bed2
ED
4119/*
4120 * net_rps_action sends any pending IPI's for rps.
4121 * Note: called with local irq disabled, but exits with local irq enabled.
4122 */
4123static void net_rps_action_and_irq_enable(struct softnet_data *sd)
4124{
4125#ifdef CONFIG_RPS
4126 struct softnet_data *remsd = sd->rps_ipi_list;
4127
4128 if (remsd) {
4129 sd->rps_ipi_list = NULL;
4130
4131 local_irq_enable();
4132
4133 /* Send pending IPI's to kick RPS processing on remote cpus. */
4134 while (remsd) {
4135 struct softnet_data *next = remsd->rps_ipi_next;
4136
4137 if (cpu_online(remsd->cpu))
4138 __smp_call_function_single(remsd->cpu,
4139 &remsd->csd, 0);
4140 remsd = next;
4141 }
4142 } else
4143#endif
4144 local_irq_enable();
4145}
4146
bea3348e 4147static int process_backlog(struct napi_struct *napi, int quota)
1da177e4
LT
4148{
4149 int work = 0;
eecfd7c4 4150 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
1da177e4 4151
e326bed2
ED
4152#ifdef CONFIG_RPS
4153 /* Check if we have pending ipi, its better to send them now,
4154 * not waiting net_rx_action() end.
4155 */
4156 if (sd->rps_ipi_list) {
4157 local_irq_disable();
4158 net_rps_action_and_irq_enable(sd);
4159 }
4160#endif
bea3348e 4161 napi->weight = weight_p;
6e7676c1
CG
4162 local_irq_disable();
4163 while (work < quota) {
1da177e4 4164 struct sk_buff *skb;
6e7676c1
CG
4165 unsigned int qlen;
4166
4167 while ((skb = __skb_dequeue(&sd->process_queue))) {
4168 local_irq_enable();
4169 __netif_receive_skb(skb);
6e7676c1 4170 local_irq_disable();
76cc8b13
TH
4171 input_queue_head_incr(sd);
4172 if (++work >= quota) {
4173 local_irq_enable();
4174 return work;
4175 }
6e7676c1 4176 }
1da177e4 4177
e36fa2f7 4178 rps_lock(sd);
6e7676c1 4179 qlen = skb_queue_len(&sd->input_pkt_queue);
76cc8b13 4180 if (qlen)
6e7676c1
CG
4181 skb_queue_splice_tail_init(&sd->input_pkt_queue,
4182 &sd->process_queue);
76cc8b13 4183
6e7676c1 4184 if (qlen < quota - work) {
eecfd7c4
ED
4185 /*
4186 * Inline a custom version of __napi_complete().
4187 * only current cpu owns and manipulates this napi,
4188 * and NAPI_STATE_SCHED is the only possible flag set on backlog.
4189 * we can use a plain write instead of clear_bit(),
4190 * and we dont need an smp_mb() memory barrier.
4191 */
4192 list_del(&napi->poll_list);
4193 napi->state = 0;
4194
6e7676c1 4195 quota = work + qlen;
bea3348e 4196 }
e36fa2f7 4197 rps_unlock(sd);
6e7676c1
CG
4198 }
4199 local_irq_enable();
1da177e4 4200
bea3348e
SH
4201 return work;
4202}
1da177e4 4203
bea3348e
SH
4204/**
4205 * __napi_schedule - schedule for receive
c4ea43c5 4206 * @n: entry to schedule
bea3348e
SH
4207 *
4208 * The entry's receive function will be scheduled to run
4209 */
b5606c2d 4210void __napi_schedule(struct napi_struct *n)
bea3348e
SH
4211{
4212 unsigned long flags;
1da177e4 4213
bea3348e 4214 local_irq_save(flags);
eecfd7c4 4215 ____napi_schedule(&__get_cpu_var(softnet_data), n);
bea3348e 4216 local_irq_restore(flags);
1da177e4 4217}
bea3348e
SH
4218EXPORT_SYMBOL(__napi_schedule);
4219
d565b0a1
HX
4220void __napi_complete(struct napi_struct *n)
4221{
4222 BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
4223 BUG_ON(n->gro_list);
4224
4225 list_del(&n->poll_list);
4226 smp_mb__before_clear_bit();
4227 clear_bit(NAPI_STATE_SCHED, &n->state);
4228}
4229EXPORT_SYMBOL(__napi_complete);
4230
4231void napi_complete(struct napi_struct *n)
4232{
4233 unsigned long flags;
4234
4235 /*
4236 * don't let napi dequeue from the cpu poll list
4237 * just in case its running on a different cpu
4238 */
4239 if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
4240 return;
4241
2e71a6f8 4242 napi_gro_flush(n, false);
d565b0a1
HX
4243 local_irq_save(flags);
4244 __napi_complete(n);
4245 local_irq_restore(flags);
4246}
4247EXPORT_SYMBOL(napi_complete);
4248
4249void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
4250 int (*poll)(struct napi_struct *, int), int weight)
4251{
4252 INIT_LIST_HEAD(&napi->poll_list);
4ae5544f 4253 napi->gro_count = 0;
d565b0a1 4254 napi->gro_list = NULL;
5d38a079 4255 napi->skb = NULL;
d565b0a1
HX
4256 napi->poll = poll;
4257 napi->weight = weight;
4258 list_add(&napi->dev_list, &dev->napi_list);
d565b0a1 4259 napi->dev = dev;
5d38a079 4260#ifdef CONFIG_NETPOLL
d565b0a1
HX
4261 spin_lock_init(&napi->poll_lock);
4262 napi->poll_owner = -1;
4263#endif
4264 set_bit(NAPI_STATE_SCHED, &napi->state);
4265}
4266EXPORT_SYMBOL(netif_napi_add);
4267
4268void netif_napi_del(struct napi_struct *napi)
4269{
4270 struct sk_buff *skb, *next;
4271
d7b06636 4272 list_del_init(&napi->dev_list);
76620aaf 4273 napi_free_frags(napi);
d565b0a1
HX
4274
4275 for (skb = napi->gro_list; skb; skb = next) {
4276 next = skb->next;
4277 skb->next = NULL;
4278 kfree_skb(skb);
4279 }
4280
4281 napi->gro_list = NULL;
4ae5544f 4282 napi->gro_count = 0;
d565b0a1
HX
4283}
4284EXPORT_SYMBOL(netif_napi_del);
4285
1da177e4
LT
4286static void net_rx_action(struct softirq_action *h)
4287{
e326bed2 4288 struct softnet_data *sd = &__get_cpu_var(softnet_data);
24f8b238 4289 unsigned long time_limit = jiffies + 2;
51b0bded 4290 int budget = netdev_budget;
53fb95d3
MM
4291 void *have;
4292
1da177e4
LT
4293 local_irq_disable();
4294
e326bed2 4295 while (!list_empty(&sd->poll_list)) {
bea3348e
SH
4296 struct napi_struct *n;
4297 int work, weight;
1da177e4 4298
bea3348e 4299 /* If softirq window is exhuasted then punt.
24f8b238
SH
4300 * Allow this to run for 2 jiffies since which will allow
4301 * an average latency of 1.5/HZ.
bea3348e 4302 */
24f8b238 4303 if (unlikely(budget <= 0 || time_after(jiffies, time_limit)))
1da177e4
LT
4304 goto softnet_break;
4305
4306 local_irq_enable();
4307
bea3348e
SH
4308 /* Even though interrupts have been re-enabled, this
4309 * access is safe because interrupts can only add new
4310 * entries to the tail of this list, and only ->poll()
4311 * calls can remove this head entry from the list.
4312 */
e326bed2 4313 n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
1da177e4 4314
bea3348e
SH
4315 have = netpoll_poll_lock(n);
4316
4317 weight = n->weight;
4318
0a7606c1
DM
4319 /* This NAPI_STATE_SCHED test is for avoiding a race
4320 * with netpoll's poll_napi(). Only the entity which
4321 * obtains the lock and sees NAPI_STATE_SCHED set will
4322 * actually make the ->poll() call. Therefore we avoid
25985edc 4323 * accidentally calling ->poll() when NAPI is not scheduled.
0a7606c1
DM
4324 */
4325 work = 0;
4ea7e386 4326 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
0a7606c1 4327 work = n->poll(n, weight);
4ea7e386
NH
4328 trace_napi_poll(n);
4329 }
bea3348e
SH
4330
4331 WARN_ON_ONCE(work > weight);
4332
4333 budget -= work;
4334
4335 local_irq_disable();
4336
4337 /* Drivers must not modify the NAPI state if they
4338 * consume the entire weight. In such cases this code
4339 * still "owns" the NAPI instance and therefore can
4340 * move the instance around on the list at-will.
4341 */
fed17f30 4342 if (unlikely(work == weight)) {
ff780cd8
HX
4343 if (unlikely(napi_disable_pending(n))) {
4344 local_irq_enable();
4345 napi_complete(n);
4346 local_irq_disable();
2e71a6f8
ED
4347 } else {
4348 if (n->gro_list) {
4349 /* flush too old packets
4350 * If HZ < 1000, flush all packets.
4351 */
4352 local_irq_enable();
4353 napi_gro_flush(n, HZ >= 1000);
4354 local_irq_disable();
4355 }
e326bed2 4356 list_move_tail(&n->poll_list, &sd->poll_list);
2e71a6f8 4357 }
fed17f30 4358 }
bea3348e
SH
4359
4360 netpoll_poll_unlock(have);
1da177e4
LT
4361 }
4362out:
e326bed2 4363 net_rps_action_and_irq_enable(sd);
0a9627f2 4364
db217334
CL
4365#ifdef CONFIG_NET_DMA
4366 /*
4367 * There may not be any more sk_buffs coming right now, so push
4368 * any pending DMA copies to hardware
4369 */
2ba05622 4370 dma_issue_pending_all();
db217334 4371#endif
bea3348e 4372
1da177e4
LT
4373 return;
4374
4375softnet_break:
dee42870 4376 sd->time_squeeze++;
1da177e4
LT
4377 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4378 goto out;
4379}
4380
d1b19dff 4381static gifconf_func_t *gifconf_list[NPROTO];
1da177e4
LT
4382
4383/**
4384 * register_gifconf - register a SIOCGIF handler
4385 * @family: Address family
4386 * @gifconf: Function handler
4387 *
4388 * Register protocol dependent address dumping routines. The handler
4389 * that is passed must not be freed or reused until it has been replaced
4390 * by another handler.
4391 */
d1b19dff 4392int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
1da177e4
LT
4393{
4394 if (family >= NPROTO)
4395 return -EINVAL;
4396 gifconf_list[family] = gifconf;
4397 return 0;
4398}
d1b19dff 4399EXPORT_SYMBOL(register_gifconf);
1da177e4
LT
4400
4401
4402/*
4403 * Map an interface index to its name (SIOCGIFNAME)
4404 */
4405
4406/*
4407 * We need this ioctl for efficient implementation of the
4408 * if_indextoname() function required by the IPv6 API. Without
4409 * it, we would have to search all the interfaces to find a
4410 * match. --pb
4411 */
4412
881d966b 4413static int dev_ifname(struct net *net, struct ifreq __user *arg)
1da177e4
LT
4414{
4415 struct net_device *dev;
4416 struct ifreq ifr;
c91f6df2 4417 unsigned seq;
1da177e4
LT
4418
4419 /*
4420 * Fetch the caller's info block.
4421 */
4422
4423 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
4424 return -EFAULT;
4425
c91f6df2 4426retry:
30e6c9fa 4427 seq = read_seqcount_begin(&devnet_rename_seq);
fb699dfd
ED
4428 rcu_read_lock();
4429 dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex);
1da177e4 4430 if (!dev) {
fb699dfd 4431 rcu_read_unlock();
1da177e4
LT
4432 return -ENODEV;
4433 }
4434
4435 strcpy(ifr.ifr_name, dev->name);
fb699dfd 4436 rcu_read_unlock();
30e6c9fa 4437 if (read_seqcount_retry(&devnet_rename_seq, seq))
c91f6df2 4438 goto retry;
1da177e4
LT
4439
4440 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
4441 return -EFAULT;
4442 return 0;
4443}
4444
4445/*
4446 * Perform a SIOCGIFCONF call. This structure will change
4447 * size eventually, and there is nothing I can do about it.
4448 * Thus we will need a 'compatibility mode'.
4449 */
4450
881d966b 4451static int dev_ifconf(struct net *net, char __user *arg)
1da177e4
LT
4452{
4453 struct ifconf ifc;
4454 struct net_device *dev;
4455 char __user *pos;
4456 int len;
4457 int total;
4458 int i;
4459
4460 /*
4461 * Fetch the caller's info block.
4462 */
4463
4464 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
4465 return -EFAULT;
4466
4467 pos = ifc.ifc_buf;
4468 len = ifc.ifc_len;
4469
4470 /*
4471 * Loop over the interfaces, and write an info block for each.
4472 */
4473
4474 total = 0;
881d966b 4475 for_each_netdev(net, dev) {
1da177e4
LT
4476 for (i = 0; i < NPROTO; i++) {
4477 if (gifconf_list[i]) {
4478 int done;
4479 if (!pos)
4480 done = gifconf_list[i](dev, NULL, 0);
4481 else
4482 done = gifconf_list[i](dev, pos + total,
4483 len - total);
4484 if (done < 0)
4485 return -EFAULT;
4486 total += done;
4487 }
4488 }
4ec93edb 4489 }
1da177e4
LT
4490
4491 /*
4492 * All done. Write the updated control block back to the caller.
4493 */
4494 ifc.ifc_len = total;
4495
4496 /*
4497 * Both BSD and Solaris return 0 here, so we do too.
4498 */
4499 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
4500}
4501
4502#ifdef CONFIG_PROC_FS
f04565dd 4503
2def16ae 4504#define BUCKET_SPACE (32 - NETDEV_HASHBITS - 1)
f04565dd
MM
4505
4506#define get_bucket(x) ((x) >> BUCKET_SPACE)
4507#define get_offset(x) ((x) & ((1 << BUCKET_SPACE) - 1))
4508#define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
4509
2def16ae 4510static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff_t *pos)
f04565dd 4511{
f04565dd
MM
4512 struct net *net = seq_file_net(seq);
4513 struct net_device *dev;
4514 struct hlist_node *p;
4515 struct hlist_head *h;
2def16ae 4516 unsigned int count = 0, offset = get_offset(*pos);
f04565dd 4517
2def16ae 4518 h = &net->dev_name_head[get_bucket(*pos)];
f04565dd 4519 hlist_for_each_entry_rcu(dev, p, h, name_hlist) {
2def16ae 4520 if (++count == offset)
f04565dd 4521 return dev;
f04565dd
MM
4522 }
4523
4524 return NULL;
4525}
4526
2def16ae 4527static inline struct net_device *dev_from_bucket(struct seq_file *seq, loff_t *pos)
f04565dd 4528{
f04565dd
MM
4529 struct net_device *dev;
4530 unsigned int bucket;
4531
f04565dd 4532 do {
2def16ae 4533 dev = dev_from_same_bucket(seq, pos);
f04565dd
MM
4534 if (dev)
4535 return dev;
4536
2def16ae
ED
4537 bucket = get_bucket(*pos) + 1;
4538 *pos = set_bucket_offset(bucket, 1);
f04565dd
MM
4539 } while (bucket < NETDEV_HASHENTRIES);
4540
4541 return NULL;
4542}
4543
1da177e4
LT
4544/*
4545 * This is invoked by the /proc filesystem handler to display a device
4546 * in detail.
4547 */
7562f876 4548void *dev_seq_start(struct seq_file *seq, loff_t *pos)
c6d14c84 4549 __acquires(RCU)
1da177e4 4550{
c6d14c84 4551 rcu_read_lock();
7562f876
PE
4552 if (!*pos)
4553 return SEQ_START_TOKEN;
1da177e4 4554
2def16ae 4555 if (get_bucket(*pos) >= NETDEV_HASHENTRIES)
f04565dd 4556 return NULL;
1da177e4 4557
2def16ae 4558 return dev_from_bucket(seq, pos);
1da177e4
LT
4559}
4560
4561void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4562{
f04565dd 4563 ++*pos;
2def16ae 4564 return dev_from_bucket(seq, pos);
1da177e4
LT
4565}
4566
4567void dev_seq_stop(struct seq_file *seq, void *v)
c6d14c84 4568 __releases(RCU)
1da177e4 4569{
c6d14c84 4570 rcu_read_unlock();
1da177e4
LT
4571}
4572
4573static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
4574{
28172739
ED
4575 struct rtnl_link_stats64 temp;
4576 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
1da177e4 4577
be1f3c2c
BH
4578 seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
4579 "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
5a1b5898
RR
4580 dev->name, stats->rx_bytes, stats->rx_packets,
4581 stats->rx_errors,
4582 stats->rx_dropped + stats->rx_missed_errors,
4583 stats->rx_fifo_errors,
4584 stats->rx_length_errors + stats->rx_over_errors +
4585 stats->rx_crc_errors + stats->rx_frame_errors,
4586 stats->rx_compressed, stats->multicast,
4587 stats->tx_bytes, stats->tx_packets,
4588 stats->tx_errors, stats->tx_dropped,
4589 stats->tx_fifo_errors, stats->collisions,
4590 stats->tx_carrier_errors +
4591 stats->tx_aborted_errors +
4592 stats->tx_window_errors +
4593 stats->tx_heartbeat_errors,
4594 stats->tx_compressed);
1da177e4
LT
4595}
4596
4597/*
4598 * Called from the PROCfs module. This now uses the new arbitrary sized
4599 * /proc/net interface to create /proc/net/dev
4600 */
4601static int dev_seq_show(struct seq_file *seq, void *v)
4602{
4603 if (v == SEQ_START_TOKEN)
4604 seq_puts(seq, "Inter-| Receive "
4605 " | Transmit\n"
4606 " face |bytes packets errs drop fifo frame "
4607 "compressed multicast|bytes packets errs "
4608 "drop fifo colls carrier compressed\n");
4609 else
4610 dev_seq_printf_stats(seq, v);
4611 return 0;
4612}
4613
dee42870 4614static struct softnet_data *softnet_get_online(loff_t *pos)
1da177e4 4615{
dee42870 4616 struct softnet_data *sd = NULL;
1da177e4 4617
0c0b0aca 4618 while (*pos < nr_cpu_ids)
4ec93edb 4619 if (cpu_online(*pos)) {
dee42870 4620 sd = &per_cpu(softnet_data, *pos);
1da177e4
LT
4621 break;
4622 } else
4623 ++*pos;
dee42870 4624 return sd;
1da177e4
LT
4625}
4626
4627static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
4628{
4629 return softnet_get_online(pos);
4630}
4631
4632static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4633{
4634 ++*pos;
4635 return softnet_get_online(pos);
4636}
4637
4638static void softnet_seq_stop(struct seq_file *seq, void *v)
4639{
4640}
4641
4642static int softnet_seq_show(struct seq_file *seq, void *v)
4643{
dee42870 4644 struct softnet_data *sd = v;
1da177e4 4645
0a9627f2 4646 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
dee42870 4647 sd->processed, sd->dropped, sd->time_squeeze, 0,
c1ebcdb8 4648 0, 0, 0, 0, /* was fastroute */
dee42870 4649 sd->cpu_collision, sd->received_rps);
1da177e4
LT
4650 return 0;
4651}
4652
f690808e 4653static const struct seq_operations dev_seq_ops = {
1da177e4
LT
4654 .start = dev_seq_start,
4655 .next = dev_seq_next,
4656 .stop = dev_seq_stop,
4657 .show = dev_seq_show,
4658};
4659
4660static int dev_seq_open(struct inode *inode, struct file *file)
4661{
e372c414 4662 return seq_open_net(inode, file, &dev_seq_ops,
2def16ae 4663 sizeof(struct seq_net_private));
5cac98dd
AB
4664}
4665
9a32144e 4666static const struct file_operations dev_seq_fops = {
1da177e4
LT
4667 .owner = THIS_MODULE,
4668 .open = dev_seq_open,
4669 .read = seq_read,
4670 .llseek = seq_lseek,
e372c414 4671 .release = seq_release_net,
1da177e4
LT
4672};
4673
f690808e 4674static const struct seq_operations softnet_seq_ops = {
1da177e4
LT
4675 .start = softnet_seq_start,
4676 .next = softnet_seq_next,
4677 .stop = softnet_seq_stop,
4678 .show = softnet_seq_show,
4679};
4680
4681static int softnet_seq_open(struct inode *inode, struct file *file)
4682{
4683 return seq_open(file, &softnet_seq_ops);
4684}
4685
9a32144e 4686static const struct file_operations softnet_seq_fops = {
1da177e4
LT
4687 .owner = THIS_MODULE,
4688 .open = softnet_seq_open,
4689 .read = seq_read,
4690 .llseek = seq_lseek,
4691 .release = seq_release,
4692};
4693
0e1256ff
SH
4694static void *ptype_get_idx(loff_t pos)
4695{
4696 struct packet_type *pt = NULL;
4697 loff_t i = 0;
4698 int t;
4699
4700 list_for_each_entry_rcu(pt, &ptype_all, list) {
4701 if (i == pos)
4702 return pt;
4703 ++i;
4704 }
4705
82d8a867 4706 for (t = 0; t < PTYPE_HASH_SIZE; t++) {
0e1256ff
SH
4707 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
4708 if (i == pos)
4709 return pt;
4710 ++i;
4711 }
4712 }
4713 return NULL;
4714}
4715
4716static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
72348a42 4717 __acquires(RCU)
0e1256ff
SH
4718{
4719 rcu_read_lock();
4720 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
4721}
4722
4723static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4724{
4725 struct packet_type *pt;
4726 struct list_head *nxt;
4727 int hash;
4728
4729 ++*pos;
4730 if (v == SEQ_START_TOKEN)
4731 return ptype_get_idx(0);
4732
4733 pt = v;
4734 nxt = pt->list.next;
4735 if (pt->type == htons(ETH_P_ALL)) {
4736 if (nxt != &ptype_all)
4737 goto found;
4738 hash = 0;
4739 nxt = ptype_base[0].next;
4740 } else
82d8a867 4741 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
0e1256ff
SH
4742
4743 while (nxt == &ptype_base[hash]) {
82d8a867 4744 if (++hash >= PTYPE_HASH_SIZE)
0e1256ff
SH
4745 return NULL;
4746 nxt = ptype_base[hash].next;
4747 }
4748found:
4749 return list_entry(nxt, struct packet_type, list);
4750}
4751
4752static void ptype_seq_stop(struct seq_file *seq, void *v)
72348a42 4753 __releases(RCU)
0e1256ff
SH
4754{
4755 rcu_read_unlock();
4756}
4757
0e1256ff
SH
4758static int ptype_seq_show(struct seq_file *seq, void *v)
4759{
4760 struct packet_type *pt = v;
4761
4762 if (v == SEQ_START_TOKEN)
4763 seq_puts(seq, "Type Device Function\n");
c346dca1 4764 else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
0e1256ff
SH
4765 if (pt->type == htons(ETH_P_ALL))
4766 seq_puts(seq, "ALL ");
4767 else
4768 seq_printf(seq, "%04x", ntohs(pt->type));
4769
908cd2da
AD
4770 seq_printf(seq, " %-8s %pF\n",
4771 pt->dev ? pt->dev->name : "", pt->func);
0e1256ff
SH
4772 }
4773
4774 return 0;
4775}
4776
4777static const struct seq_operations ptype_seq_ops = {
4778 .start = ptype_seq_start,
4779 .next = ptype_seq_next,
4780 .stop = ptype_seq_stop,
4781 .show = ptype_seq_show,
4782};
4783
4784static int ptype_seq_open(struct inode *inode, struct file *file)
4785{
2feb27db
PE
4786 return seq_open_net(inode, file, &ptype_seq_ops,
4787 sizeof(struct seq_net_private));
0e1256ff
SH
4788}
4789
4790static const struct file_operations ptype_seq_fops = {
4791 .owner = THIS_MODULE,
4792 .open = ptype_seq_open,
4793 .read = seq_read,
4794 .llseek = seq_lseek,
2feb27db 4795 .release = seq_release_net,
0e1256ff
SH
4796};
4797
4798
4665079c 4799static int __net_init dev_proc_net_init(struct net *net)
1da177e4
LT
4800{
4801 int rc = -ENOMEM;
4802
881d966b 4803 if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
1da177e4 4804 goto out;
881d966b 4805 if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
1da177e4 4806 goto out_dev;
881d966b 4807 if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
457c4cbc 4808 goto out_softnet;
0e1256ff 4809
881d966b 4810 if (wext_proc_init(net))
457c4cbc 4811 goto out_ptype;
1da177e4
LT
4812 rc = 0;
4813out:
4814 return rc;
457c4cbc 4815out_ptype:
881d966b 4816 proc_net_remove(net, "ptype");
1da177e4 4817out_softnet:
881d966b 4818 proc_net_remove(net, "softnet_stat");
1da177e4 4819out_dev:
881d966b 4820 proc_net_remove(net, "dev");
1da177e4
LT
4821 goto out;
4822}
881d966b 4823
4665079c 4824static void __net_exit dev_proc_net_exit(struct net *net)
881d966b
EB
4825{
4826 wext_proc_exit(net);
4827
4828 proc_net_remove(net, "ptype");
4829 proc_net_remove(net, "softnet_stat");
4830 proc_net_remove(net, "dev");
4831}
4832
022cbae6 4833static struct pernet_operations __net_initdata dev_proc_ops = {
881d966b
EB
4834 .init = dev_proc_net_init,
4835 .exit = dev_proc_net_exit,
4836};
4837
4838static int __init dev_proc_init(void)
4839{
4840 return register_pernet_subsys(&dev_proc_ops);
4841}
1da177e4
LT
4842#else
4843#define dev_proc_init() 0
4844#endif /* CONFIG_PROC_FS */
4845
4846
9ff162a8
JP
4847struct netdev_upper {
4848 struct net_device *dev;
4849 bool master;
4850 struct list_head list;
4851 struct rcu_head rcu;
4852 struct list_head search_list;
4853};
4854
4855static void __append_search_uppers(struct list_head *search_list,
4856 struct net_device *dev)
4857{
4858 struct netdev_upper *upper;
4859
4860 list_for_each_entry(upper, &dev->upper_dev_list, list) {
4861 /* check if this upper is not already in search list */
4862 if (list_empty(&upper->search_list))
4863 list_add_tail(&upper->search_list, search_list);
4864 }
4865}
4866
4867static bool __netdev_search_upper_dev(struct net_device *dev,
4868 struct net_device *upper_dev)
4869{
4870 LIST_HEAD(search_list);
4871 struct netdev_upper *upper;
4872 struct netdev_upper *tmp;
4873 bool ret = false;
4874
4875 __append_search_uppers(&search_list, dev);
4876 list_for_each_entry(upper, &search_list, search_list) {
4877 if (upper->dev == upper_dev) {
4878 ret = true;
4879 break;
4880 }
4881 __append_search_uppers(&search_list, upper->dev);
4882 }
4883 list_for_each_entry_safe(upper, tmp, &search_list, search_list)
4884 INIT_LIST_HEAD(&upper->search_list);
4885 return ret;
4886}
4887
4888static struct netdev_upper *__netdev_find_upper(struct net_device *dev,
4889 struct net_device *upper_dev)
4890{
4891 struct netdev_upper *upper;
4892
4893 list_for_each_entry(upper, &dev->upper_dev_list, list) {
4894 if (upper->dev == upper_dev)
4895 return upper;
4896 }
4897 return NULL;
4898}
4899
4900/**
4901 * netdev_has_upper_dev - Check if device is linked to an upper device
4902 * @dev: device
4903 * @upper_dev: upper device to check
4904 *
4905 * Find out if a device is linked to specified upper device and return true
4906 * in case it is. Note that this checks only immediate upper device,
4907 * not through a complete stack of devices. The caller must hold the RTNL lock.
4908 */
4909bool netdev_has_upper_dev(struct net_device *dev,
4910 struct net_device *upper_dev)
4911{
4912 ASSERT_RTNL();
4913
4914 return __netdev_find_upper(dev, upper_dev);
4915}
4916EXPORT_SYMBOL(netdev_has_upper_dev);
4917
4918/**
4919 * netdev_has_any_upper_dev - Check if device is linked to some device
4920 * @dev: device
4921 *
4922 * Find out if a device is linked to an upper device and return true in case
4923 * it is. The caller must hold the RTNL lock.
4924 */
4925bool netdev_has_any_upper_dev(struct net_device *dev)
4926{
4927 ASSERT_RTNL();
4928
4929 return !list_empty(&dev->upper_dev_list);
4930}
4931EXPORT_SYMBOL(netdev_has_any_upper_dev);
4932
4933/**
4934 * netdev_master_upper_dev_get - Get master upper device
4935 * @dev: device
4936 *
4937 * Find a master upper device and return pointer to it or NULL in case
4938 * it's not there. The caller must hold the RTNL lock.
4939 */
4940struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4941{
4942 struct netdev_upper *upper;
4943
4944 ASSERT_RTNL();
4945
4946 if (list_empty(&dev->upper_dev_list))
4947 return NULL;
4948
4949 upper = list_first_entry(&dev->upper_dev_list,
4950 struct netdev_upper, list);
4951 if (likely(upper->master))
4952 return upper->dev;
4953 return NULL;
4954}
4955EXPORT_SYMBOL(netdev_master_upper_dev_get);
4956
4957/**
4958 * netdev_master_upper_dev_get_rcu - Get master upper device
4959 * @dev: device
4960 *
4961 * Find a master upper device and return pointer to it or NULL in case
4962 * it's not there. The caller must hold the RCU read lock.
4963 */
4964struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
4965{
4966 struct netdev_upper *upper;
4967
4968 upper = list_first_or_null_rcu(&dev->upper_dev_list,
4969 struct netdev_upper, list);
4970 if (upper && likely(upper->master))
4971 return upper->dev;
4972 return NULL;
4973}
4974EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
4975
4976static int __netdev_upper_dev_link(struct net_device *dev,
4977 struct net_device *upper_dev, bool master)
4978{
4979 struct netdev_upper *upper;
4980
4981 ASSERT_RTNL();
4982
4983 if (dev == upper_dev)
4984 return -EBUSY;
4985
4986 /* To prevent loops, check if dev is not upper device to upper_dev. */
4987 if (__netdev_search_upper_dev(upper_dev, dev))
4988 return -EBUSY;
4989
4990 if (__netdev_find_upper(dev, upper_dev))
4991 return -EEXIST;
4992
4993 if (master && netdev_master_upper_dev_get(dev))
4994 return -EBUSY;
4995
4996 upper = kmalloc(sizeof(*upper), GFP_KERNEL);
4997 if (!upper)
4998 return -ENOMEM;
4999
5000 upper->dev = upper_dev;
5001 upper->master = master;
5002 INIT_LIST_HEAD(&upper->search_list);
5003
5004 /* Ensure that master upper link is always the first item in list. */
5005 if (master)
5006 list_add_rcu(&upper->list, &dev->upper_dev_list);
5007 else
5008 list_add_tail_rcu(&upper->list, &dev->upper_dev_list);
5009 dev_hold(upper_dev);
5010
5011 return 0;
5012}
5013
5014/**
5015 * netdev_upper_dev_link - Add a link to the upper device
5016 * @dev: device
5017 * @upper_dev: new upper device
5018 *
5019 * Adds a link to device which is upper to this one. The caller must hold
5020 * the RTNL lock. On a failure a negative errno code is returned.
5021 * On success the reference counts are adjusted and the function
5022 * returns zero.
5023 */
5024int netdev_upper_dev_link(struct net_device *dev,
5025 struct net_device *upper_dev)
5026{
5027 return __netdev_upper_dev_link(dev, upper_dev, false);
5028}
5029EXPORT_SYMBOL(netdev_upper_dev_link);
5030
5031/**
5032 * netdev_master_upper_dev_link - Add a master link to the upper device
5033 * @dev: device
5034 * @upper_dev: new upper device
5035 *
5036 * Adds a link to device which is upper to this one. In this case, only
5037 * one master upper device can be linked, although other non-master devices
5038 * might be linked as well. The caller must hold the RTNL lock.
5039 * On a failure a negative errno code is returned. On success the reference
5040 * counts are adjusted and the function returns zero.
5041 */
5042int netdev_master_upper_dev_link(struct net_device *dev,
5043 struct net_device *upper_dev)
5044{
5045 return __netdev_upper_dev_link(dev, upper_dev, true);
5046}
5047EXPORT_SYMBOL(netdev_master_upper_dev_link);
5048
5049/**
5050 * netdev_upper_dev_unlink - Removes a link to upper device
5051 * @dev: device
5052 * @upper_dev: new upper device
5053 *
5054 * Removes a link to device which is upper to this one. The caller must hold
5055 * the RTNL lock.
5056 */
5057void netdev_upper_dev_unlink(struct net_device *dev,
5058 struct net_device *upper_dev)
5059{
5060 struct netdev_upper *upper;
5061
5062 ASSERT_RTNL();
5063
5064 upper = __netdev_find_upper(dev, upper_dev);
5065 if (!upper)
5066 return;
5067 list_del_rcu(&upper->list);
5068 dev_put(upper_dev);
5069 kfree_rcu(upper, rcu);
5070}
5071EXPORT_SYMBOL(netdev_upper_dev_unlink);
5072
b6c40d68
PM
5073static void dev_change_rx_flags(struct net_device *dev, int flags)
5074{
d314774c
SH
5075 const struct net_device_ops *ops = dev->netdev_ops;
5076
5077 if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
5078 ops->ndo_change_rx_flags(dev, flags);
b6c40d68
PM
5079}
5080
dad9b335 5081static int __dev_set_promiscuity(struct net_device *dev, int inc)
1da177e4 5082{
b536db93 5083 unsigned int old_flags = dev->flags;
d04a48b0
EB
5084 kuid_t uid;
5085 kgid_t gid;
1da177e4 5086
24023451
PM
5087 ASSERT_RTNL();
5088
dad9b335
WC
5089 dev->flags |= IFF_PROMISC;
5090 dev->promiscuity += inc;
5091 if (dev->promiscuity == 0) {
5092 /*
5093 * Avoid overflow.
5094 * If inc causes overflow, untouch promisc and return error.
5095 */
5096 if (inc < 0)
5097 dev->flags &= ~IFF_PROMISC;
5098 else {
5099 dev->promiscuity -= inc;
7b6cd1ce
JP
5100 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
5101 dev->name);
dad9b335
WC
5102 return -EOVERFLOW;
5103 }
5104 }
52609c0b 5105 if (dev->flags != old_flags) {
7b6cd1ce
JP
5106 pr_info("device %s %s promiscuous mode\n",
5107 dev->name,
5108 dev->flags & IFF_PROMISC ? "entered" : "left");
8192b0c4
DH
5109 if (audit_enabled) {
5110 current_uid_gid(&uid, &gid);
7759db82
KHK
5111 audit_log(current->audit_context, GFP_ATOMIC,
5112 AUDIT_ANOM_PROMISCUOUS,
5113 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
5114 dev->name, (dev->flags & IFF_PROMISC),
5115 (old_flags & IFF_PROMISC),
e1760bd5 5116 from_kuid(&init_user_ns, audit_get_loginuid(current)),
d04a48b0
EB
5117 from_kuid(&init_user_ns, uid),
5118 from_kgid(&init_user_ns, gid),
7759db82 5119 audit_get_sessionid(current));
8192b0c4 5120 }
24023451 5121
b6c40d68 5122 dev_change_rx_flags(dev, IFF_PROMISC);
1da177e4 5123 }
dad9b335 5124 return 0;
1da177e4
LT
5125}
5126
4417da66
PM
5127/**
5128 * dev_set_promiscuity - update promiscuity count on a device
5129 * @dev: device
5130 * @inc: modifier
5131 *
5132 * Add or remove promiscuity from a device. While the count in the device
5133 * remains above zero the interface remains promiscuous. Once it hits zero
5134 * the device reverts back to normal filtering operation. A negative inc
5135 * value is used to drop promiscuity on the device.
dad9b335 5136 * Return 0 if successful or a negative errno code on error.
4417da66 5137 */
dad9b335 5138int dev_set_promiscuity(struct net_device *dev, int inc)
4417da66 5139{
b536db93 5140 unsigned int old_flags = dev->flags;
dad9b335 5141 int err;
4417da66 5142
dad9b335 5143 err = __dev_set_promiscuity(dev, inc);
4b5a698e 5144 if (err < 0)
dad9b335 5145 return err;
4417da66
PM
5146 if (dev->flags != old_flags)
5147 dev_set_rx_mode(dev);
dad9b335 5148 return err;
4417da66 5149}
d1b19dff 5150EXPORT_SYMBOL(dev_set_promiscuity);
4417da66 5151
1da177e4
LT
5152/**
5153 * dev_set_allmulti - update allmulti count on a device
5154 * @dev: device
5155 * @inc: modifier
5156 *
5157 * Add or remove reception of all multicast frames to a device. While the
5158 * count in the device remains above zero the interface remains listening
5159 * to all interfaces. Once it hits zero the device reverts back to normal
5160 * filtering operation. A negative @inc value is used to drop the counter
5161 * when releasing a resource needing all multicasts.
dad9b335 5162 * Return 0 if successful or a negative errno code on error.
1da177e4
LT
5163 */
5164
dad9b335 5165int dev_set_allmulti(struct net_device *dev, int inc)
1da177e4 5166{
b536db93 5167 unsigned int old_flags = dev->flags;
1da177e4 5168
24023451
PM
5169 ASSERT_RTNL();
5170
1da177e4 5171 dev->flags |= IFF_ALLMULTI;
dad9b335
WC
5172 dev->allmulti += inc;
5173 if (dev->allmulti == 0) {
5174 /*
5175 * Avoid overflow.
5176 * If inc causes overflow, untouch allmulti and return error.
5177 */
5178 if (inc < 0)
5179 dev->flags &= ~IFF_ALLMULTI;
5180 else {
5181 dev->allmulti -= inc;
7b6cd1ce
JP
5182 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
5183 dev->name);
dad9b335
WC
5184 return -EOVERFLOW;
5185 }
5186 }
24023451 5187 if (dev->flags ^ old_flags) {
b6c40d68 5188 dev_change_rx_flags(dev, IFF_ALLMULTI);
4417da66 5189 dev_set_rx_mode(dev);
24023451 5190 }
dad9b335 5191 return 0;
4417da66 5192}
d1b19dff 5193EXPORT_SYMBOL(dev_set_allmulti);
4417da66
PM
5194
5195/*
5196 * Upload unicast and multicast address lists to device and
5197 * configure RX filtering. When the device doesn't support unicast
53ccaae1 5198 * filtering it is put in promiscuous mode while unicast addresses
4417da66
PM
5199 * are present.
5200 */
5201void __dev_set_rx_mode(struct net_device *dev)
5202{
d314774c
SH
5203 const struct net_device_ops *ops = dev->netdev_ops;
5204
4417da66
PM
5205 /* dev_open will call this function so the list will stay sane. */
5206 if (!(dev->flags&IFF_UP))
5207 return;
5208
5209 if (!netif_device_present(dev))
40b77c94 5210 return;
4417da66 5211
01789349 5212 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
4417da66
PM
5213 /* Unicast addresses changes may only happen under the rtnl,
5214 * therefore calling __dev_set_promiscuity here is safe.
5215 */
32e7bfc4 5216 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
4417da66 5217 __dev_set_promiscuity(dev, 1);
2d348d1f 5218 dev->uc_promisc = true;
32e7bfc4 5219 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
4417da66 5220 __dev_set_promiscuity(dev, -1);
2d348d1f 5221 dev->uc_promisc = false;
4417da66 5222 }
4417da66 5223 }
01789349
JP
5224
5225 if (ops->ndo_set_rx_mode)
5226 ops->ndo_set_rx_mode(dev);
4417da66
PM
5227}
5228
5229void dev_set_rx_mode(struct net_device *dev)
5230{
b9e40857 5231 netif_addr_lock_bh(dev);
4417da66 5232 __dev_set_rx_mode(dev);
b9e40857 5233 netif_addr_unlock_bh(dev);
1da177e4
LT
5234}
5235
f0db275a
SH
5236/**
5237 * dev_get_flags - get flags reported to userspace
5238 * @dev: device
5239 *
5240 * Get the combination of flag bits exported through APIs to userspace.
5241 */
95c96174 5242unsigned int dev_get_flags(const struct net_device *dev)
1da177e4 5243{
95c96174 5244 unsigned int flags;
1da177e4
LT
5245
5246 flags = (dev->flags & ~(IFF_PROMISC |
5247 IFF_ALLMULTI |
b00055aa
SR
5248 IFF_RUNNING |
5249 IFF_LOWER_UP |
5250 IFF_DORMANT)) |
1da177e4
LT
5251 (dev->gflags & (IFF_PROMISC |
5252 IFF_ALLMULTI));
5253
b00055aa
SR
5254 if (netif_running(dev)) {
5255 if (netif_oper_up(dev))
5256 flags |= IFF_RUNNING;
5257 if (netif_carrier_ok(dev))
5258 flags |= IFF_LOWER_UP;
5259 if (netif_dormant(dev))
5260 flags |= IFF_DORMANT;
5261 }
1da177e4
LT
5262
5263 return flags;
5264}
d1b19dff 5265EXPORT_SYMBOL(dev_get_flags);
1da177e4 5266
bd380811 5267int __dev_change_flags(struct net_device *dev, unsigned int flags)
1da177e4 5268{
b536db93 5269 unsigned int old_flags = dev->flags;
bd380811 5270 int ret;
1da177e4 5271
24023451
PM
5272 ASSERT_RTNL();
5273
1da177e4
LT
5274 /*
5275 * Set the flags on our device.
5276 */
5277
5278 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
5279 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
5280 IFF_AUTOMEDIA)) |
5281 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
5282 IFF_ALLMULTI));
5283
5284 /*
5285 * Load in the correct multicast list now the flags have changed.
5286 */
5287
b6c40d68
PM
5288 if ((old_flags ^ flags) & IFF_MULTICAST)
5289 dev_change_rx_flags(dev, IFF_MULTICAST);
24023451 5290
4417da66 5291 dev_set_rx_mode(dev);
1da177e4
LT
5292
5293 /*
5294 * Have we downed the interface. We handle IFF_UP ourselves
5295 * according to user attempts to set it, rather than blindly
5296 * setting it.
5297 */
5298
5299 ret = 0;
5300 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
bd380811 5301 ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
1da177e4
LT
5302
5303 if (!ret)
4417da66 5304 dev_set_rx_mode(dev);
1da177e4
LT
5305 }
5306
1da177e4 5307 if ((flags ^ dev->gflags) & IFF_PROMISC) {
d1b19dff
ED
5308 int inc = (flags & IFF_PROMISC) ? 1 : -1;
5309
1da177e4
LT
5310 dev->gflags ^= IFF_PROMISC;
5311 dev_set_promiscuity(dev, inc);
5312 }
5313
5314 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
5315 is important. Some (broken) drivers set IFF_PROMISC, when
5316 IFF_ALLMULTI is requested not asking us and not reporting.
5317 */
5318 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
d1b19dff
ED
5319 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
5320
1da177e4
LT
5321 dev->gflags ^= IFF_ALLMULTI;
5322 dev_set_allmulti(dev, inc);
5323 }
5324
bd380811
PM
5325 return ret;
5326}
5327
5328void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
5329{
5330 unsigned int changes = dev->flags ^ old_flags;
5331
5332 if (changes & IFF_UP) {
5333 if (dev->flags & IFF_UP)
5334 call_netdevice_notifiers(NETDEV_UP, dev);
5335 else
5336 call_netdevice_notifiers(NETDEV_DOWN, dev);
5337 }
5338
5339 if (dev->flags & IFF_UP &&
5340 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE)))
5341 call_netdevice_notifiers(NETDEV_CHANGE, dev);
5342}
5343
5344/**
5345 * dev_change_flags - change device settings
5346 * @dev: device
5347 * @flags: device state flags
5348 *
5349 * Change settings on device based state flags. The flags are
5350 * in the userspace exported format.
5351 */
b536db93 5352int dev_change_flags(struct net_device *dev, unsigned int flags)
bd380811 5353{
b536db93
ED
5354 int ret;
5355 unsigned int changes, old_flags = dev->flags;
bd380811
PM
5356
5357 ret = __dev_change_flags(dev, flags);
5358 if (ret < 0)
5359 return ret;
5360
5361 changes = old_flags ^ dev->flags;
7c355f53
TG
5362 if (changes)
5363 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
1da177e4 5364
bd380811 5365 __dev_notify_flags(dev, old_flags);
1da177e4
LT
5366 return ret;
5367}
d1b19dff 5368EXPORT_SYMBOL(dev_change_flags);
1da177e4 5369
f0db275a
SH
5370/**
5371 * dev_set_mtu - Change maximum transfer unit
5372 * @dev: device
5373 * @new_mtu: new transfer unit
5374 *
5375 * Change the maximum transfer size of the network device.
5376 */
1da177e4
LT
5377int dev_set_mtu(struct net_device *dev, int new_mtu)
5378{
d314774c 5379 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4
LT
5380 int err;
5381
5382 if (new_mtu == dev->mtu)
5383 return 0;
5384
5385 /* MTU must be positive. */
5386 if (new_mtu < 0)
5387 return -EINVAL;
5388
5389 if (!netif_device_present(dev))
5390 return -ENODEV;
5391
5392 err = 0;
d314774c
SH
5393 if (ops->ndo_change_mtu)
5394 err = ops->ndo_change_mtu(dev, new_mtu);
1da177e4
LT
5395 else
5396 dev->mtu = new_mtu;
d314774c 5397
e3d8fabe 5398 if (!err)
056925ab 5399 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
1da177e4
LT
5400 return err;
5401}
d1b19dff 5402EXPORT_SYMBOL(dev_set_mtu);
1da177e4 5403
cbda10fa
VD
5404/**
5405 * dev_set_group - Change group this device belongs to
5406 * @dev: device
5407 * @new_group: group this device should belong to
5408 */
5409void dev_set_group(struct net_device *dev, int new_group)
5410{
5411 dev->group = new_group;
5412}
5413EXPORT_SYMBOL(dev_set_group);
5414
f0db275a
SH
5415/**
5416 * dev_set_mac_address - Change Media Access Control Address
5417 * @dev: device
5418 * @sa: new address
5419 *
5420 * Change the hardware (MAC) address of the device
5421 */
1da177e4
LT
5422int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
5423{
d314774c 5424 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4
LT
5425 int err;
5426
d314774c 5427 if (!ops->ndo_set_mac_address)
1da177e4
LT
5428 return -EOPNOTSUPP;
5429 if (sa->sa_family != dev->type)
5430 return -EINVAL;
5431 if (!netif_device_present(dev))
5432 return -ENODEV;
d314774c 5433 err = ops->ndo_set_mac_address(dev, sa);
f6521516
JP
5434 if (err)
5435 return err;
fbdeca2d 5436 dev->addr_assign_type = NET_ADDR_SET;
f6521516 5437 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
7bf23575 5438 add_device_randomness(dev->dev_addr, dev->addr_len);
f6521516 5439 return 0;
1da177e4 5440}
d1b19dff 5441EXPORT_SYMBOL(dev_set_mac_address);
1da177e4 5442
4bf84c35
JP
5443/**
5444 * dev_change_carrier - Change device carrier
5445 * @dev: device
5446 * @new_carries: new value
5447 *
5448 * Change device carrier
5449 */
5450int dev_change_carrier(struct net_device *dev, bool new_carrier)
5451{
5452 const struct net_device_ops *ops = dev->netdev_ops;
5453
5454 if (!ops->ndo_change_carrier)
5455 return -EOPNOTSUPP;
5456 if (!netif_device_present(dev))
5457 return -ENODEV;
5458 return ops->ndo_change_carrier(dev, new_carrier);
5459}
5460EXPORT_SYMBOL(dev_change_carrier);
5461
1da177e4 5462/*
3710becf 5463 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
1da177e4 5464 */
14e3e079 5465static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
1da177e4
LT
5466{
5467 int err;
3710becf 5468 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
1da177e4
LT
5469
5470 if (!dev)
5471 return -ENODEV;
5472
5473 switch (cmd) {
d1b19dff
ED
5474 case SIOCGIFFLAGS: /* Get interface flags */
5475 ifr->ifr_flags = (short) dev_get_flags(dev);
5476 return 0;
1da177e4 5477
d1b19dff
ED
5478 case SIOCGIFMETRIC: /* Get the metric on the interface
5479 (currently unused) */
5480 ifr->ifr_metric = 0;
5481 return 0;
1da177e4 5482
d1b19dff
ED
5483 case SIOCGIFMTU: /* Get the MTU of a device */
5484 ifr->ifr_mtu = dev->mtu;
5485 return 0;
1da177e4 5486
d1b19dff
ED
5487 case SIOCGIFHWADDR:
5488 if (!dev->addr_len)
5489 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
5490 else
5491 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
5492 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
5493 ifr->ifr_hwaddr.sa_family = dev->type;
5494 return 0;
1da177e4 5495
d1b19dff
ED
5496 case SIOCGIFSLAVE:
5497 err = -EINVAL;
5498 break;
14e3e079 5499
d1b19dff
ED
5500 case SIOCGIFMAP:
5501 ifr->ifr_map.mem_start = dev->mem_start;
5502 ifr->ifr_map.mem_end = dev->mem_end;
5503 ifr->ifr_map.base_addr = dev->base_addr;
5504 ifr->ifr_map.irq = dev->irq;
5505 ifr->ifr_map.dma = dev->dma;
5506 ifr->ifr_map.port = dev->if_port;
5507 return 0;
14e3e079 5508
d1b19dff
ED
5509 case SIOCGIFINDEX:
5510 ifr->ifr_ifindex = dev->ifindex;
5511 return 0;
14e3e079 5512
d1b19dff
ED
5513 case SIOCGIFTXQLEN:
5514 ifr->ifr_qlen = dev->tx_queue_len;
5515 return 0;
14e3e079 5516
d1b19dff
ED
5517 default:
5518 /* dev_ioctl() should ensure this case
5519 * is never reached
5520 */
5521 WARN_ON(1);
41c31f31 5522 err = -ENOTTY;
d1b19dff 5523 break;
14e3e079
JG
5524
5525 }
5526 return err;
5527}
5528
5529/*
5530 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
5531 */
5532static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
5533{
5534 int err;
5535 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
5f2f6da7 5536 const struct net_device_ops *ops;
14e3e079
JG
5537
5538 if (!dev)
5539 return -ENODEV;
5540
5f2f6da7
JP
5541 ops = dev->netdev_ops;
5542
14e3e079 5543 switch (cmd) {
d1b19dff
ED
5544 case SIOCSIFFLAGS: /* Set interface flags */
5545 return dev_change_flags(dev, ifr->ifr_flags);
14e3e079 5546
d1b19dff
ED
5547 case SIOCSIFMETRIC: /* Set the metric on the interface
5548 (currently unused) */
5549 return -EOPNOTSUPP;
14e3e079 5550
d1b19dff
ED
5551 case SIOCSIFMTU: /* Set the MTU of a device */
5552 return dev_set_mtu(dev, ifr->ifr_mtu);
1da177e4 5553
d1b19dff
ED
5554 case SIOCSIFHWADDR:
5555 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
1da177e4 5556
d1b19dff
ED
5557 case SIOCSIFHWBROADCAST:
5558 if (ifr->ifr_hwaddr.sa_family != dev->type)
5559 return -EINVAL;
5560 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
5561 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
5562 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
5563 return 0;
1da177e4 5564
d1b19dff
ED
5565 case SIOCSIFMAP:
5566 if (ops->ndo_set_config) {
1da177e4
LT
5567 if (!netif_device_present(dev))
5568 return -ENODEV;
d1b19dff
ED
5569 return ops->ndo_set_config(dev, &ifr->ifr_map);
5570 }
5571 return -EOPNOTSUPP;
1da177e4 5572
d1b19dff 5573 case SIOCADDMULTI:
b81693d9 5574 if (!ops->ndo_set_rx_mode ||
d1b19dff
ED
5575 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
5576 return -EINVAL;
5577 if (!netif_device_present(dev))
5578 return -ENODEV;
22bedad3 5579 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
d1b19dff
ED
5580
5581 case SIOCDELMULTI:
b81693d9 5582 if (!ops->ndo_set_rx_mode ||
d1b19dff
ED
5583 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
5584 return -EINVAL;
5585 if (!netif_device_present(dev))
5586 return -ENODEV;
22bedad3 5587 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
1da177e4 5588
d1b19dff
ED
5589 case SIOCSIFTXQLEN:
5590 if (ifr->ifr_qlen < 0)
5591 return -EINVAL;
5592 dev->tx_queue_len = ifr->ifr_qlen;
5593 return 0;
1da177e4 5594
d1b19dff
ED
5595 case SIOCSIFNAME:
5596 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
5597 return dev_change_name(dev, ifr->ifr_newname);
1da177e4 5598
4dc360c5
RC
5599 case SIOCSHWTSTAMP:
5600 err = net_hwtstamp_validate(ifr);
5601 if (err)
5602 return err;
5603 /* fall through */
5604
d1b19dff
ED
5605 /*
5606 * Unknown or private ioctl
5607 */
5608 default:
5609 if ((cmd >= SIOCDEVPRIVATE &&
5610 cmd <= SIOCDEVPRIVATE + 15) ||
5611 cmd == SIOCBONDENSLAVE ||
5612 cmd == SIOCBONDRELEASE ||
5613 cmd == SIOCBONDSETHWADDR ||
5614 cmd == SIOCBONDSLAVEINFOQUERY ||
5615 cmd == SIOCBONDINFOQUERY ||
5616 cmd == SIOCBONDCHANGEACTIVE ||
5617 cmd == SIOCGMIIPHY ||
5618 cmd == SIOCGMIIREG ||
5619 cmd == SIOCSMIIREG ||
5620 cmd == SIOCBRADDIF ||
5621 cmd == SIOCBRDELIF ||
5622 cmd == SIOCSHWTSTAMP ||
5623 cmd == SIOCWANDEV) {
5624 err = -EOPNOTSUPP;
5625 if (ops->ndo_do_ioctl) {
5626 if (netif_device_present(dev))
5627 err = ops->ndo_do_ioctl(dev, ifr, cmd);
5628 else
5629 err = -ENODEV;
5630 }
5631 } else
5632 err = -EINVAL;
1da177e4
LT
5633
5634 }
5635 return err;
5636}
5637
5638/*
5639 * This function handles all "interface"-type I/O control requests. The actual
5640 * 'doing' part of this is dev_ifsioc above.
5641 */
5642
5643/**
5644 * dev_ioctl - network device ioctl
c4ea43c5 5645 * @net: the applicable net namespace
1da177e4
LT
5646 * @cmd: command to issue
5647 * @arg: pointer to a struct ifreq in user space
5648 *
5649 * Issue ioctl functions to devices. This is normally called by the
5650 * user space syscall interfaces but can sometimes be useful for
5651 * other purposes. The return value is the return from the syscall if
5652 * positive or a negative errno code on error.
5653 */
5654
881d966b 5655int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1da177e4
LT
5656{
5657 struct ifreq ifr;
5658 int ret;
5659 char *colon;
5660
5661 /* One special case: SIOCGIFCONF takes ifconf argument
5662 and requires shared lock, because it sleeps writing
5663 to user space.
5664 */
5665
5666 if (cmd == SIOCGIFCONF) {
6756ae4b 5667 rtnl_lock();
881d966b 5668 ret = dev_ifconf(net, (char __user *) arg);
6756ae4b 5669 rtnl_unlock();
1da177e4
LT
5670 return ret;
5671 }
5672 if (cmd == SIOCGIFNAME)
881d966b 5673 return dev_ifname(net, (struct ifreq __user *)arg);
1da177e4
LT
5674
5675 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
5676 return -EFAULT;
5677
5678 ifr.ifr_name[IFNAMSIZ-1] = 0;
5679
5680 colon = strchr(ifr.ifr_name, ':');
5681 if (colon)
5682 *colon = 0;
5683
5684 /*
5685 * See which interface the caller is talking about.
5686 */
5687
5688 switch (cmd) {
d1b19dff
ED
5689 /*
5690 * These ioctl calls:
5691 * - can be done by all.
5692 * - atomic and do not require locking.
5693 * - return a value
5694 */
5695 case SIOCGIFFLAGS:
5696 case SIOCGIFMETRIC:
5697 case SIOCGIFMTU:
5698 case SIOCGIFHWADDR:
5699 case SIOCGIFSLAVE:
5700 case SIOCGIFMAP:
5701 case SIOCGIFINDEX:
5702 case SIOCGIFTXQLEN:
5703 dev_load(net, ifr.ifr_name);
3710becf 5704 rcu_read_lock();
d1b19dff 5705 ret = dev_ifsioc_locked(net, &ifr, cmd);
3710becf 5706 rcu_read_unlock();
d1b19dff
ED
5707 if (!ret) {
5708 if (colon)
5709 *colon = ':';
5710 if (copy_to_user(arg, &ifr,
5711 sizeof(struct ifreq)))
5712 ret = -EFAULT;
5713 }
5714 return ret;
1da177e4 5715
d1b19dff
ED
5716 case SIOCETHTOOL:
5717 dev_load(net, ifr.ifr_name);
5718 rtnl_lock();
5719 ret = dev_ethtool(net, &ifr);
5720 rtnl_unlock();
5721 if (!ret) {
5722 if (colon)
5723 *colon = ':';
5724 if (copy_to_user(arg, &ifr,
5725 sizeof(struct ifreq)))
5726 ret = -EFAULT;
5727 }
5728 return ret;
1da177e4 5729
d1b19dff
ED
5730 /*
5731 * These ioctl calls:
5732 * - require superuser power.
5733 * - require strict serialization.
5734 * - return a value
5735 */
5736 case SIOCGMIIPHY:
5737 case SIOCGMIIREG:
5738 case SIOCSIFNAME:
5e1fccc0 5739 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
d1b19dff
ED
5740 return -EPERM;
5741 dev_load(net, ifr.ifr_name);
5742 rtnl_lock();
5743 ret = dev_ifsioc(net, &ifr, cmd);
5744 rtnl_unlock();
5745 if (!ret) {
5746 if (colon)
5747 *colon = ':';
5748 if (copy_to_user(arg, &ifr,
5749 sizeof(struct ifreq)))
5750 ret = -EFAULT;
5751 }
5752 return ret;
1da177e4 5753
d1b19dff
ED
5754 /*
5755 * These ioctl calls:
5756 * - require superuser power.
5757 * - require strict serialization.
5758 * - do not return a value
5759 */
5e1fccc0
EB
5760 case SIOCSIFMAP:
5761 case SIOCSIFTXQLEN:
5762 if (!capable(CAP_NET_ADMIN))
5763 return -EPERM;
5764 /* fall through */
5765 /*
5766 * These ioctl calls:
5767 * - require local superuser power.
5768 * - require strict serialization.
5769 * - do not return a value
5770 */
d1b19dff
ED
5771 case SIOCSIFFLAGS:
5772 case SIOCSIFMETRIC:
5773 case SIOCSIFMTU:
d1b19dff
ED
5774 case SIOCSIFHWADDR:
5775 case SIOCSIFSLAVE:
5776 case SIOCADDMULTI:
5777 case SIOCDELMULTI:
5778 case SIOCSIFHWBROADCAST:
d1b19dff
ED
5779 case SIOCSMIIREG:
5780 case SIOCBONDENSLAVE:
5781 case SIOCBONDRELEASE:
5782 case SIOCBONDSETHWADDR:
5783 case SIOCBONDCHANGEACTIVE:
5784 case SIOCBRADDIF:
5785 case SIOCBRDELIF:
5786 case SIOCSHWTSTAMP:
5e1fccc0 5787 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
d1b19dff
ED
5788 return -EPERM;
5789 /* fall through */
5790 case SIOCBONDSLAVEINFOQUERY:
5791 case SIOCBONDINFOQUERY:
5792 dev_load(net, ifr.ifr_name);
5793 rtnl_lock();
5794 ret = dev_ifsioc(net, &ifr, cmd);
5795 rtnl_unlock();
5796 return ret;
5797
5798 case SIOCGIFMEM:
5799 /* Get the per device memory space. We can add this but
5800 * currently do not support it */
5801 case SIOCSIFMEM:
5802 /* Set the per device memory buffer space.
5803 * Not applicable in our case */
5804 case SIOCSIFLINK:
41c31f31 5805 return -ENOTTY;
d1b19dff
ED
5806
5807 /*
5808 * Unknown or private ioctl.
5809 */
5810 default:
5811 if (cmd == SIOCWANDEV ||
5812 (cmd >= SIOCDEVPRIVATE &&
5813 cmd <= SIOCDEVPRIVATE + 15)) {
881d966b 5814 dev_load(net, ifr.ifr_name);
1da177e4 5815 rtnl_lock();
881d966b 5816 ret = dev_ifsioc(net, &ifr, cmd);
1da177e4 5817 rtnl_unlock();
d1b19dff
ED
5818 if (!ret && copy_to_user(arg, &ifr,
5819 sizeof(struct ifreq)))
5820 ret = -EFAULT;
1da177e4 5821 return ret;
d1b19dff
ED
5822 }
5823 /* Take care of Wireless Extensions */
5824 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
5825 return wext_handle_ioctl(net, &ifr, cmd, arg);
41c31f31 5826 return -ENOTTY;
1da177e4
LT
5827 }
5828}
5829
5830
5831/**
5832 * dev_new_index - allocate an ifindex
c4ea43c5 5833 * @net: the applicable net namespace
1da177e4
LT
5834 *
5835 * Returns a suitable unique value for a new device interface
5836 * number. The caller must hold the rtnl semaphore or the
5837 * dev_base_lock to be sure it remains unique.
5838 */
881d966b 5839static int dev_new_index(struct net *net)
1da177e4 5840{
aa79e66e 5841 int ifindex = net->ifindex;
1da177e4
LT
5842 for (;;) {
5843 if (++ifindex <= 0)
5844 ifindex = 1;
881d966b 5845 if (!__dev_get_by_index(net, ifindex))
aa79e66e 5846 return net->ifindex = ifindex;
1da177e4
LT
5847 }
5848}
5849
1da177e4 5850/* Delayed registration/unregisteration */
3b5b34fd 5851static LIST_HEAD(net_todo_list);
1da177e4 5852
6f05f629 5853static void net_set_todo(struct net_device *dev)
1da177e4 5854{
1da177e4 5855 list_add_tail(&dev->todo_list, &net_todo_list);
1da177e4
LT
5856}
5857
9b5e383c 5858static void rollback_registered_many(struct list_head *head)
93ee31f1 5859{
e93737b0 5860 struct net_device *dev, *tmp;
9b5e383c 5861
93ee31f1
DL
5862 BUG_ON(dev_boot_phase);
5863 ASSERT_RTNL();
5864
e93737b0 5865 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
9b5e383c 5866 /* Some devices call without registering
e93737b0
KK
5867 * for initialization unwind. Remove those
5868 * devices and proceed with the remaining.
9b5e383c
ED
5869 */
5870 if (dev->reg_state == NETREG_UNINITIALIZED) {
7b6cd1ce
JP
5871 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
5872 dev->name, dev);
93ee31f1 5873
9b5e383c 5874 WARN_ON(1);
e93737b0
KK
5875 list_del(&dev->unreg_list);
5876 continue;
9b5e383c 5877 }
449f4544 5878 dev->dismantle = true;
9b5e383c 5879 BUG_ON(dev->reg_state != NETREG_REGISTERED);
44345724 5880 }
93ee31f1 5881
44345724
OP
5882 /* If device is running, close it first. */
5883 dev_close_many(head);
93ee31f1 5884
44345724 5885 list_for_each_entry(dev, head, unreg_list) {
9b5e383c
ED
5886 /* And unlink it from device chain. */
5887 unlist_netdevice(dev);
93ee31f1 5888
9b5e383c
ED
5889 dev->reg_state = NETREG_UNREGISTERING;
5890 }
93ee31f1
DL
5891
5892 synchronize_net();
5893
9b5e383c
ED
5894 list_for_each_entry(dev, head, unreg_list) {
5895 /* Shutdown queueing discipline. */
5896 dev_shutdown(dev);
93ee31f1
DL
5897
5898
9b5e383c
ED
5899 /* Notify protocols, that we are about to destroy
5900 this device. They should clean all the things.
5901 */
5902 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
93ee31f1 5903
a2835763
PM
5904 if (!dev->rtnl_link_ops ||
5905 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5906 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
5907
9b5e383c
ED
5908 /*
5909 * Flush the unicast and multicast chains
5910 */
a748ee24 5911 dev_uc_flush(dev);
22bedad3 5912 dev_mc_flush(dev);
93ee31f1 5913
9b5e383c
ED
5914 if (dev->netdev_ops->ndo_uninit)
5915 dev->netdev_ops->ndo_uninit(dev);
93ee31f1 5916
9ff162a8
JP
5917 /* Notifier chain MUST detach us all upper devices. */
5918 WARN_ON(netdev_has_any_upper_dev(dev));
93ee31f1 5919
9b5e383c
ED
5920 /* Remove entries from kobject tree */
5921 netdev_unregister_kobject(dev);
5922 }
93ee31f1 5923
850a545b 5924 synchronize_net();
395264d5 5925
a5ee1551 5926 list_for_each_entry(dev, head, unreg_list)
9b5e383c
ED
5927 dev_put(dev);
5928}
5929
5930static void rollback_registered(struct net_device *dev)
5931{
5932 LIST_HEAD(single);
5933
5934 list_add(&dev->unreg_list, &single);
5935 rollback_registered_many(&single);
ceaaec98 5936 list_del(&single);
93ee31f1
DL
5937}
5938
c8f44aff
MM
5939static netdev_features_t netdev_fix_features(struct net_device *dev,
5940 netdev_features_t features)
b63365a2 5941{
57422dc5
MM
5942 /* Fix illegal checksum combinations */
5943 if ((features & NETIF_F_HW_CSUM) &&
5944 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
6f404e44 5945 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
57422dc5
MM
5946 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
5947 }
5948
b63365a2
HX
5949 /* Fix illegal SG+CSUM combinations. */
5950 if ((features & NETIF_F_SG) &&
5951 !(features & NETIF_F_ALL_CSUM)) {
6f404e44
MM
5952 netdev_dbg(dev,
5953 "Dropping NETIF_F_SG since no checksum feature.\n");
b63365a2
HX
5954 features &= ~NETIF_F_SG;
5955 }
5956
5957 /* TSO requires that SG is present as well. */
ea2d3688 5958 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
6f404e44 5959 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
ea2d3688 5960 features &= ~NETIF_F_ALL_TSO;
b63365a2
HX
5961 }
5962
31d8b9e0
BH
5963 /* TSO ECN requires that TSO is present as well. */
5964 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
5965 features &= ~NETIF_F_TSO_ECN;
5966
212b573f
MM
5967 /* Software GSO depends on SG. */
5968 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
6f404e44 5969 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
212b573f
MM
5970 features &= ~NETIF_F_GSO;
5971 }
5972
acd1130e 5973 /* UFO needs SG and checksumming */
b63365a2 5974 if (features & NETIF_F_UFO) {
79032644
MM
5975 /* maybe split UFO into V4 and V6? */
5976 if (!((features & NETIF_F_GEN_CSUM) ||
5977 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
5978 == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
6f404e44 5979 netdev_dbg(dev,
acd1130e 5980 "Dropping NETIF_F_UFO since no checksum offload features.\n");
b63365a2
HX
5981 features &= ~NETIF_F_UFO;
5982 }
5983
5984 if (!(features & NETIF_F_SG)) {
6f404e44 5985 netdev_dbg(dev,
acd1130e 5986 "Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n");
b63365a2
HX
5987 features &= ~NETIF_F_UFO;
5988 }
5989 }
5990
5991 return features;
5992}
b63365a2 5993
6cb6a27c 5994int __netdev_update_features(struct net_device *dev)
5455c699 5995{
c8f44aff 5996 netdev_features_t features;
5455c699
MM
5997 int err = 0;
5998
87267485
MM
5999 ASSERT_RTNL();
6000
5455c699
MM
6001 features = netdev_get_wanted_features(dev);
6002
6003 if (dev->netdev_ops->ndo_fix_features)
6004 features = dev->netdev_ops->ndo_fix_features(dev, features);
6005
6006 /* driver might be less strict about feature dependencies */
6007 features = netdev_fix_features(dev, features);
6008
6009 if (dev->features == features)
6cb6a27c 6010 return 0;
5455c699 6011
c8f44aff
MM
6012 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
6013 &dev->features, &features);
5455c699
MM
6014
6015 if (dev->netdev_ops->ndo_set_features)
6016 err = dev->netdev_ops->ndo_set_features(dev, features);
6017
6cb6a27c 6018 if (unlikely(err < 0)) {
5455c699 6019 netdev_err(dev,
c8f44aff
MM
6020 "set_features() failed (%d); wanted %pNF, left %pNF\n",
6021 err, &features, &dev->features);
6cb6a27c
MM
6022 return -1;
6023 }
6024
6025 if (!err)
6026 dev->features = features;
6027
6028 return 1;
6029}
6030
afe12cc8
MM
6031/**
6032 * netdev_update_features - recalculate device features
6033 * @dev: the device to check
6034 *
6035 * Recalculate dev->features set and send notifications if it
6036 * has changed. Should be called after driver or hardware dependent
6037 * conditions might have changed that influence the features.
6038 */
6cb6a27c
MM
6039void netdev_update_features(struct net_device *dev)
6040{
6041 if (__netdev_update_features(dev))
6042 netdev_features_change(dev);
5455c699
MM
6043}
6044EXPORT_SYMBOL(netdev_update_features);
6045
afe12cc8
MM
6046/**
6047 * netdev_change_features - recalculate device features
6048 * @dev: the device to check
6049 *
6050 * Recalculate dev->features set and send notifications even
6051 * if they have not changed. Should be called instead of
6052 * netdev_update_features() if also dev->vlan_features might
6053 * have changed to allow the changes to be propagated to stacked
6054 * VLAN devices.
6055 */
6056void netdev_change_features(struct net_device *dev)
6057{
6058 __netdev_update_features(dev);
6059 netdev_features_change(dev);
6060}
6061EXPORT_SYMBOL(netdev_change_features);
6062
fc4a7489
PM
6063/**
6064 * netif_stacked_transfer_operstate - transfer operstate
6065 * @rootdev: the root or lower level device to transfer state from
6066 * @dev: the device to transfer operstate to
6067 *
6068 * Transfer operational state from root to device. This is normally
6069 * called when a stacking relationship exists between the root
6070 * device and the device(a leaf device).
6071 */
6072void netif_stacked_transfer_operstate(const struct net_device *rootdev,
6073 struct net_device *dev)
6074{
6075 if (rootdev->operstate == IF_OPER_DORMANT)
6076 netif_dormant_on(dev);
6077 else
6078 netif_dormant_off(dev);
6079
6080 if (netif_carrier_ok(rootdev)) {
6081 if (!netif_carrier_ok(dev))
6082 netif_carrier_on(dev);
6083 } else {
6084 if (netif_carrier_ok(dev))
6085 netif_carrier_off(dev);
6086 }
6087}
6088EXPORT_SYMBOL(netif_stacked_transfer_operstate);
6089
bf264145 6090#ifdef CONFIG_RPS
1b4bf461
ED
6091static int netif_alloc_rx_queues(struct net_device *dev)
6092{
1b4bf461 6093 unsigned int i, count = dev->num_rx_queues;
bd25fa7b 6094 struct netdev_rx_queue *rx;
1b4bf461 6095
bd25fa7b 6096 BUG_ON(count < 1);
1b4bf461 6097
bd25fa7b
TH
6098 rx = kcalloc(count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
6099 if (!rx) {
7b6cd1ce 6100 pr_err("netdev: Unable to allocate %u rx queues\n", count);
bd25fa7b 6101 return -ENOMEM;
1b4bf461 6102 }
bd25fa7b
TH
6103 dev->_rx = rx;
6104
bd25fa7b 6105 for (i = 0; i < count; i++)
fe822240 6106 rx[i].dev = dev;
1b4bf461
ED
6107 return 0;
6108}
bf264145 6109#endif
1b4bf461 6110
aa942104
CG
6111static void netdev_init_one_queue(struct net_device *dev,
6112 struct netdev_queue *queue, void *_unused)
6113{
6114 /* Initialize queue lock */
6115 spin_lock_init(&queue->_xmit_lock);
6116 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
6117 queue->xmit_lock_owner = -1;
b236da69 6118 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
aa942104 6119 queue->dev = dev;
114cf580
TH
6120#ifdef CONFIG_BQL
6121 dql_init(&queue->dql, HZ);
6122#endif
aa942104
CG
6123}
6124
e6484930
TH
6125static int netif_alloc_netdev_queues(struct net_device *dev)
6126{
6127 unsigned int count = dev->num_tx_queues;
6128 struct netdev_queue *tx;
6129
6130 BUG_ON(count < 1);
6131
6132 tx = kcalloc(count, sizeof(struct netdev_queue), GFP_KERNEL);
6133 if (!tx) {
7b6cd1ce 6134 pr_err("netdev: Unable to allocate %u tx queues\n", count);
e6484930
TH
6135 return -ENOMEM;
6136 }
6137 dev->_tx = tx;
1d24eb48 6138
e6484930
TH
6139 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
6140 spin_lock_init(&dev->tx_global_lock);
aa942104
CG
6141
6142 return 0;
e6484930
TH
6143}
6144
1da177e4
LT
6145/**
6146 * register_netdevice - register a network device
6147 * @dev: device to register
6148 *
6149 * Take a completed network device structure and add it to the kernel
6150 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
6151 * chain. 0 is returned on success. A negative errno code is returned
6152 * on a failure to set up the device, or if the name is a duplicate.
6153 *
6154 * Callers must hold the rtnl semaphore. You may want
6155 * register_netdev() instead of this.
6156 *
6157 * BUGS:
6158 * The locking appears insufficient to guarantee two parallel registers
6159 * will not get the same name.
6160 */
6161
6162int register_netdevice(struct net_device *dev)
6163{
1da177e4 6164 int ret;
d314774c 6165 struct net *net = dev_net(dev);
1da177e4
LT
6166
6167 BUG_ON(dev_boot_phase);
6168 ASSERT_RTNL();
6169
b17a7c17
SH
6170 might_sleep();
6171
1da177e4
LT
6172 /* When net_device's are persistent, this will be fatal. */
6173 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
d314774c 6174 BUG_ON(!net);
1da177e4 6175
f1f28aa3 6176 spin_lock_init(&dev->addr_list_lock);
cf508b12 6177 netdev_set_addr_lockdep_class(dev);
1da177e4 6178
1da177e4
LT
6179 dev->iflink = -1;
6180
828de4f6 6181 ret = dev_get_valid_name(net, dev, dev->name);
0696c3a8
PP
6182 if (ret < 0)
6183 goto out;
6184
1da177e4 6185 /* Init, if this function is available */
d314774c
SH
6186 if (dev->netdev_ops->ndo_init) {
6187 ret = dev->netdev_ops->ndo_init(dev);
1da177e4
LT
6188 if (ret) {
6189 if (ret > 0)
6190 ret = -EIO;
90833aa4 6191 goto out;
1da177e4
LT
6192 }
6193 }
4ec93edb 6194
9c7dafbf
PE
6195 ret = -EBUSY;
6196 if (!dev->ifindex)
6197 dev->ifindex = dev_new_index(net);
6198 else if (__dev_get_by_index(net, dev->ifindex))
6199 goto err_uninit;
6200
1da177e4
LT
6201 if (dev->iflink == -1)
6202 dev->iflink = dev->ifindex;
6203
5455c699
MM
6204 /* Transfer changeable features to wanted_features and enable
6205 * software offloads (GSO and GRO).
6206 */
6207 dev->hw_features |= NETIF_F_SOFT_FEATURES;
14d1232f
MM
6208 dev->features |= NETIF_F_SOFT_FEATURES;
6209 dev->wanted_features = dev->features & dev->hw_features;
1da177e4 6210
c6e1a0d1 6211 /* Turn on no cache copy if HW is doing checksum */
34324dc2
MM
6212 if (!(dev->flags & IFF_LOOPBACK)) {
6213 dev->hw_features |= NETIF_F_NOCACHE_COPY;
6214 if (dev->features & NETIF_F_ALL_CSUM) {
6215 dev->wanted_features |= NETIF_F_NOCACHE_COPY;
6216 dev->features |= NETIF_F_NOCACHE_COPY;
6217 }
c6e1a0d1
TH
6218 }
6219
1180e7d6 6220 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
16c3ea78 6221 */
1180e7d6 6222 dev->vlan_features |= NETIF_F_HIGHDMA;
16c3ea78 6223
7ffbe3fd
JB
6224 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
6225 ret = notifier_to_errno(ret);
6226 if (ret)
6227 goto err_uninit;
6228
8b41d188 6229 ret = netdev_register_kobject(dev);
b17a7c17 6230 if (ret)
7ce1b0ed 6231 goto err_uninit;
b17a7c17
SH
6232 dev->reg_state = NETREG_REGISTERED;
6233
6cb6a27c 6234 __netdev_update_features(dev);
8e9b59b2 6235
1da177e4
LT
6236 /*
6237 * Default initial state at registry is that the
6238 * device is present.
6239 */
6240
6241 set_bit(__LINK_STATE_PRESENT, &dev->state);
6242
8f4cccbb
BH
6243 linkwatch_init_dev(dev);
6244
1da177e4 6245 dev_init_scheduler(dev);
1da177e4 6246 dev_hold(dev);
ce286d32 6247 list_netdevice(dev);
7bf23575 6248 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 6249
948b337e
JP
6250 /* If the device has permanent device address, driver should
6251 * set dev_addr and also addr_assign_type should be set to
6252 * NET_ADDR_PERM (default value).
6253 */
6254 if (dev->addr_assign_type == NET_ADDR_PERM)
6255 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6256
1da177e4 6257 /* Notify protocols, that a new device appeared. */
056925ab 6258 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
fcc5a03a 6259 ret = notifier_to_errno(ret);
93ee31f1
DL
6260 if (ret) {
6261 rollback_registered(dev);
6262 dev->reg_state = NETREG_UNREGISTERED;
6263 }
d90a909e
EB
6264 /*
6265 * Prevent userspace races by waiting until the network
6266 * device is fully setup before sending notifications.
6267 */
a2835763
PM
6268 if (!dev->rtnl_link_ops ||
6269 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
6270 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1da177e4
LT
6271
6272out:
6273 return ret;
7ce1b0ed
HX
6274
6275err_uninit:
d314774c
SH
6276 if (dev->netdev_ops->ndo_uninit)
6277 dev->netdev_ops->ndo_uninit(dev);
7ce1b0ed 6278 goto out;
1da177e4 6279}
d1b19dff 6280EXPORT_SYMBOL(register_netdevice);
1da177e4 6281
937f1ba5
BH
6282/**
6283 * init_dummy_netdev - init a dummy network device for NAPI
6284 * @dev: device to init
6285 *
6286 * This takes a network device structure and initialize the minimum
6287 * amount of fields so it can be used to schedule NAPI polls without
6288 * registering a full blown interface. This is to be used by drivers
6289 * that need to tie several hardware interfaces to a single NAPI
6290 * poll scheduler due to HW limitations.
6291 */
6292int init_dummy_netdev(struct net_device *dev)
6293{
6294 /* Clear everything. Note we don't initialize spinlocks
6295 * are they aren't supposed to be taken by any of the
6296 * NAPI code and this dummy netdev is supposed to be
6297 * only ever used for NAPI polls
6298 */
6299 memset(dev, 0, sizeof(struct net_device));
6300
6301 /* make sure we BUG if trying to hit standard
6302 * register/unregister code path
6303 */
6304 dev->reg_state = NETREG_DUMMY;
6305
937f1ba5
BH
6306 /* NAPI wants this */
6307 INIT_LIST_HEAD(&dev->napi_list);
6308
6309 /* a dummy interface is started by default */
6310 set_bit(__LINK_STATE_PRESENT, &dev->state);
6311 set_bit(__LINK_STATE_START, &dev->state);
6312
29b4433d
ED
6313 /* Note : We dont allocate pcpu_refcnt for dummy devices,
6314 * because users of this 'device' dont need to change
6315 * its refcount.
6316 */
6317
937f1ba5
BH
6318 return 0;
6319}
6320EXPORT_SYMBOL_GPL(init_dummy_netdev);
6321
6322
1da177e4
LT
6323/**
6324 * register_netdev - register a network device
6325 * @dev: device to register
6326 *
6327 * Take a completed network device structure and add it to the kernel
6328 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
6329 * chain. 0 is returned on success. A negative errno code is returned
6330 * on a failure to set up the device, or if the name is a duplicate.
6331 *
38b4da38 6332 * This is a wrapper around register_netdevice that takes the rtnl semaphore
1da177e4
LT
6333 * and expands the device name if you passed a format string to
6334 * alloc_netdev.
6335 */
6336int register_netdev(struct net_device *dev)
6337{
6338 int err;
6339
6340 rtnl_lock();
1da177e4 6341 err = register_netdevice(dev);
1da177e4
LT
6342 rtnl_unlock();
6343 return err;
6344}
6345EXPORT_SYMBOL(register_netdev);
6346
29b4433d
ED
6347int netdev_refcnt_read(const struct net_device *dev)
6348{
6349 int i, refcnt = 0;
6350
6351 for_each_possible_cpu(i)
6352 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
6353 return refcnt;
6354}
6355EXPORT_SYMBOL(netdev_refcnt_read);
6356
2c53040f 6357/**
1da177e4 6358 * netdev_wait_allrefs - wait until all references are gone.
3de7a37b 6359 * @dev: target net_device
1da177e4
LT
6360 *
6361 * This is called when unregistering network devices.
6362 *
6363 * Any protocol or device that holds a reference should register
6364 * for netdevice notification, and cleanup and put back the
6365 * reference if they receive an UNREGISTER event.
6366 * We can get stuck here if buggy protocols don't correctly
4ec93edb 6367 * call dev_put.
1da177e4
LT
6368 */
6369static void netdev_wait_allrefs(struct net_device *dev)
6370{
6371 unsigned long rebroadcast_time, warning_time;
29b4433d 6372 int refcnt;
1da177e4 6373
e014debe
ED
6374 linkwatch_forget_dev(dev);
6375
1da177e4 6376 rebroadcast_time = warning_time = jiffies;
29b4433d
ED
6377 refcnt = netdev_refcnt_read(dev);
6378
6379 while (refcnt != 0) {
1da177e4 6380 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
6756ae4b 6381 rtnl_lock();
1da177e4
LT
6382
6383 /* Rebroadcast unregister notification */
056925ab 6384 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
1da177e4 6385
748e2d93 6386 __rtnl_unlock();
0115e8e3 6387 rcu_barrier();
748e2d93
ED
6388 rtnl_lock();
6389
0115e8e3 6390 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
1da177e4
LT
6391 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
6392 &dev->state)) {
6393 /* We must not have linkwatch events
6394 * pending on unregister. If this
6395 * happens, we simply run the queue
6396 * unscheduled, resulting in a noop
6397 * for this device.
6398 */
6399 linkwatch_run_queue();
6400 }
6401
6756ae4b 6402 __rtnl_unlock();
1da177e4
LT
6403
6404 rebroadcast_time = jiffies;
6405 }
6406
6407 msleep(250);
6408
29b4433d
ED
6409 refcnt = netdev_refcnt_read(dev);
6410
1da177e4 6411 if (time_after(jiffies, warning_time + 10 * HZ)) {
7b6cd1ce
JP
6412 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
6413 dev->name, refcnt);
1da177e4
LT
6414 warning_time = jiffies;
6415 }
6416 }
6417}
6418
6419/* The sequence is:
6420 *
6421 * rtnl_lock();
6422 * ...
6423 * register_netdevice(x1);
6424 * register_netdevice(x2);
6425 * ...
6426 * unregister_netdevice(y1);
6427 * unregister_netdevice(y2);
6428 * ...
6429 * rtnl_unlock();
6430 * free_netdev(y1);
6431 * free_netdev(y2);
6432 *
58ec3b4d 6433 * We are invoked by rtnl_unlock().
1da177e4 6434 * This allows us to deal with problems:
b17a7c17 6435 * 1) We can delete sysfs objects which invoke hotplug
1da177e4
LT
6436 * without deadlocking with linkwatch via keventd.
6437 * 2) Since we run with the RTNL semaphore not held, we can sleep
6438 * safely in order to wait for the netdev refcnt to drop to zero.
58ec3b4d
HX
6439 *
6440 * We must not return until all unregister events added during
6441 * the interval the lock was held have been completed.
1da177e4 6442 */
1da177e4
LT
6443void netdev_run_todo(void)
6444{
626ab0e6 6445 struct list_head list;
1da177e4 6446
1da177e4 6447 /* Snapshot list, allow later requests */
626ab0e6 6448 list_replace_init(&net_todo_list, &list);
58ec3b4d
HX
6449
6450 __rtnl_unlock();
626ab0e6 6451
0115e8e3
ED
6452
6453 /* Wait for rcu callbacks to finish before next phase */
850a545b
EB
6454 if (!list_empty(&list))
6455 rcu_barrier();
6456
1da177e4
LT
6457 while (!list_empty(&list)) {
6458 struct net_device *dev
e5e26d75 6459 = list_first_entry(&list, struct net_device, todo_list);
1da177e4
LT
6460 list_del(&dev->todo_list);
6461
748e2d93 6462 rtnl_lock();
0115e8e3 6463 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
748e2d93 6464 __rtnl_unlock();
0115e8e3 6465
b17a7c17 6466 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
7b6cd1ce 6467 pr_err("network todo '%s' but state %d\n",
b17a7c17
SH
6468 dev->name, dev->reg_state);
6469 dump_stack();
6470 continue;
6471 }
1da177e4 6472
b17a7c17 6473 dev->reg_state = NETREG_UNREGISTERED;
1da177e4 6474
152102c7 6475 on_each_cpu(flush_backlog, dev, 1);
6e583ce5 6476
b17a7c17 6477 netdev_wait_allrefs(dev);
1da177e4 6478
b17a7c17 6479 /* paranoia */
29b4433d 6480 BUG_ON(netdev_refcnt_read(dev));
33d480ce
ED
6481 WARN_ON(rcu_access_pointer(dev->ip_ptr));
6482 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
547b792c 6483 WARN_ON(dev->dn_ptr);
1da177e4 6484
b17a7c17
SH
6485 if (dev->destructor)
6486 dev->destructor(dev);
9093bbb2
SH
6487
6488 /* Free network device */
6489 kobject_put(&dev->dev.kobj);
1da177e4 6490 }
1da177e4
LT
6491}
6492
3cfde79c
BH
6493/* Convert net_device_stats to rtnl_link_stats64. They have the same
6494 * fields in the same order, with only the type differing.
6495 */
77a1abf5
ED
6496void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
6497 const struct net_device_stats *netdev_stats)
3cfde79c
BH
6498{
6499#if BITS_PER_LONG == 64
77a1abf5
ED
6500 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
6501 memcpy(stats64, netdev_stats, sizeof(*stats64));
3cfde79c
BH
6502#else
6503 size_t i, n = sizeof(*stats64) / sizeof(u64);
6504 const unsigned long *src = (const unsigned long *)netdev_stats;
6505 u64 *dst = (u64 *)stats64;
6506
6507 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
6508 sizeof(*stats64) / sizeof(u64));
6509 for (i = 0; i < n; i++)
6510 dst[i] = src[i];
6511#endif
6512}
77a1abf5 6513EXPORT_SYMBOL(netdev_stats_to_stats64);
3cfde79c 6514
eeda3fd6
SH
6515/**
6516 * dev_get_stats - get network device statistics
6517 * @dev: device to get statistics from
28172739 6518 * @storage: place to store stats
eeda3fd6 6519 *
d7753516
BH
6520 * Get network statistics from device. Return @storage.
6521 * The device driver may provide its own method by setting
6522 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
6523 * otherwise the internal statistics structure is used.
eeda3fd6 6524 */
d7753516
BH
6525struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
6526 struct rtnl_link_stats64 *storage)
7004bf25 6527{
eeda3fd6
SH
6528 const struct net_device_ops *ops = dev->netdev_ops;
6529
28172739
ED
6530 if (ops->ndo_get_stats64) {
6531 memset(storage, 0, sizeof(*storage));
caf586e5
ED
6532 ops->ndo_get_stats64(dev, storage);
6533 } else if (ops->ndo_get_stats) {
3cfde79c 6534 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
caf586e5
ED
6535 } else {
6536 netdev_stats_to_stats64(storage, &dev->stats);
28172739 6537 }
caf586e5 6538 storage->rx_dropped += atomic_long_read(&dev->rx_dropped);
28172739 6539 return storage;
c45d286e 6540}
eeda3fd6 6541EXPORT_SYMBOL(dev_get_stats);
c45d286e 6542
24824a09 6543struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
dc2b4847 6544{
24824a09 6545 struct netdev_queue *queue = dev_ingress_queue(dev);
dc2b4847 6546
24824a09
ED
6547#ifdef CONFIG_NET_CLS_ACT
6548 if (queue)
6549 return queue;
6550 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
6551 if (!queue)
6552 return NULL;
6553 netdev_init_one_queue(dev, queue, NULL);
24824a09
ED
6554 queue->qdisc = &noop_qdisc;
6555 queue->qdisc_sleeping = &noop_qdisc;
6556 rcu_assign_pointer(dev->ingress_queue, queue);
6557#endif
6558 return queue;
bb949fbd
DM
6559}
6560
2c60db03
ED
6561static const struct ethtool_ops default_ethtool_ops;
6562
1da177e4 6563/**
36909ea4 6564 * alloc_netdev_mqs - allocate network device
1da177e4
LT
6565 * @sizeof_priv: size of private data to allocate space for
6566 * @name: device name format string
6567 * @setup: callback to initialize device
36909ea4
TH
6568 * @txqs: the number of TX subqueues to allocate
6569 * @rxqs: the number of RX subqueues to allocate
1da177e4
LT
6570 *
6571 * Allocates a struct net_device with private data area for driver use
f25f4e44 6572 * and performs basic initialization. Also allocates subquue structs
36909ea4 6573 * for each queue on the device.
1da177e4 6574 */
36909ea4
TH
6575struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6576 void (*setup)(struct net_device *),
6577 unsigned int txqs, unsigned int rxqs)
1da177e4 6578{
1da177e4 6579 struct net_device *dev;
7943986c 6580 size_t alloc_size;
1ce8e7b5 6581 struct net_device *p;
1da177e4 6582
b6fe17d6
SH
6583 BUG_ON(strlen(name) >= sizeof(dev->name));
6584
36909ea4 6585 if (txqs < 1) {
7b6cd1ce 6586 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
55513fb4
TH
6587 return NULL;
6588 }
6589
36909ea4
TH
6590#ifdef CONFIG_RPS
6591 if (rxqs < 1) {
7b6cd1ce 6592 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
36909ea4
TH
6593 return NULL;
6594 }
6595#endif
6596
fd2ea0a7 6597 alloc_size = sizeof(struct net_device);
d1643d24
AD
6598 if (sizeof_priv) {
6599 /* ensure 32-byte alignment of private area */
1ce8e7b5 6600 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
d1643d24
AD
6601 alloc_size += sizeof_priv;
6602 }
6603 /* ensure 32-byte alignment of whole construct */
1ce8e7b5 6604 alloc_size += NETDEV_ALIGN - 1;
1da177e4 6605
31380de9 6606 p = kzalloc(alloc_size, GFP_KERNEL);
1da177e4 6607 if (!p) {
7b6cd1ce 6608 pr_err("alloc_netdev: Unable to allocate device\n");
1da177e4
LT
6609 return NULL;
6610 }
1da177e4 6611
1ce8e7b5 6612 dev = PTR_ALIGN(p, NETDEV_ALIGN);
1da177e4 6613 dev->padded = (char *)dev - (char *)p;
ab9c73cc 6614
29b4433d
ED
6615 dev->pcpu_refcnt = alloc_percpu(int);
6616 if (!dev->pcpu_refcnt)
e6484930 6617 goto free_p;
ab9c73cc 6618
ab9c73cc 6619 if (dev_addr_init(dev))
29b4433d 6620 goto free_pcpu;
ab9c73cc 6621
22bedad3 6622 dev_mc_init(dev);
a748ee24 6623 dev_uc_init(dev);
ccffad25 6624
c346dca1 6625 dev_net_set(dev, &init_net);
1da177e4 6626
8d3bdbd5 6627 dev->gso_max_size = GSO_MAX_SIZE;
30b678d8 6628 dev->gso_max_segs = GSO_MAX_SEGS;
8d3bdbd5 6629
8d3bdbd5
DM
6630 INIT_LIST_HEAD(&dev->napi_list);
6631 INIT_LIST_HEAD(&dev->unreg_list);
6632 INIT_LIST_HEAD(&dev->link_watch_list);
9ff162a8 6633 INIT_LIST_HEAD(&dev->upper_dev_list);
8d3bdbd5
DM
6634 dev->priv_flags = IFF_XMIT_DST_RELEASE;
6635 setup(dev);
6636
36909ea4
TH
6637 dev->num_tx_queues = txqs;
6638 dev->real_num_tx_queues = txqs;
ed9af2e8 6639 if (netif_alloc_netdev_queues(dev))
8d3bdbd5 6640 goto free_all;
e8a0464c 6641
df334545 6642#ifdef CONFIG_RPS
36909ea4
TH
6643 dev->num_rx_queues = rxqs;
6644 dev->real_num_rx_queues = rxqs;
fe822240 6645 if (netif_alloc_rx_queues(dev))
8d3bdbd5 6646 goto free_all;
df334545 6647#endif
0a9627f2 6648
1da177e4 6649 strcpy(dev->name, name);
cbda10fa 6650 dev->group = INIT_NETDEV_GROUP;
2c60db03
ED
6651 if (!dev->ethtool_ops)
6652 dev->ethtool_ops = &default_ethtool_ops;
1da177e4 6653 return dev;
ab9c73cc 6654
8d3bdbd5
DM
6655free_all:
6656 free_netdev(dev);
6657 return NULL;
6658
29b4433d
ED
6659free_pcpu:
6660 free_percpu(dev->pcpu_refcnt);
ed9af2e8 6661 kfree(dev->_tx);
fe822240
TH
6662#ifdef CONFIG_RPS
6663 kfree(dev->_rx);
6664#endif
6665
ab9c73cc
JP
6666free_p:
6667 kfree(p);
6668 return NULL;
1da177e4 6669}
36909ea4 6670EXPORT_SYMBOL(alloc_netdev_mqs);
1da177e4
LT
6671
6672/**
6673 * free_netdev - free network device
6674 * @dev: device
6675 *
4ec93edb
YH
6676 * This function does the last stage of destroying an allocated device
6677 * interface. The reference to the device object is released.
1da177e4
LT
6678 * If this is the last reference then it will be freed.
6679 */
6680void free_netdev(struct net_device *dev)
6681{
d565b0a1
HX
6682 struct napi_struct *p, *n;
6683
f3005d7f
DL
6684 release_net(dev_net(dev));
6685
e8a0464c 6686 kfree(dev->_tx);
fe822240
TH
6687#ifdef CONFIG_RPS
6688 kfree(dev->_rx);
6689#endif
e8a0464c 6690
33d480ce 6691 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
24824a09 6692
f001fde5
JP
6693 /* Flush device addresses */
6694 dev_addr_flush(dev);
6695
d565b0a1
HX
6696 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
6697 netif_napi_del(p);
6698
29b4433d
ED
6699 free_percpu(dev->pcpu_refcnt);
6700 dev->pcpu_refcnt = NULL;
6701
3041a069 6702 /* Compatibility with error handling in drivers */
1da177e4
LT
6703 if (dev->reg_state == NETREG_UNINITIALIZED) {
6704 kfree((char *)dev - dev->padded);
6705 return;
6706 }
6707
6708 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
6709 dev->reg_state = NETREG_RELEASED;
6710
43cb76d9
GKH
6711 /* will free via device release */
6712 put_device(&dev->dev);
1da177e4 6713}
d1b19dff 6714EXPORT_SYMBOL(free_netdev);
4ec93edb 6715
f0db275a
SH
6716/**
6717 * synchronize_net - Synchronize with packet receive processing
6718 *
6719 * Wait for packets currently being received to be done.
6720 * Does not block later packets from starting.
6721 */
4ec93edb 6722void synchronize_net(void)
1da177e4
LT
6723{
6724 might_sleep();
be3fc413
ED
6725 if (rtnl_is_locked())
6726 synchronize_rcu_expedited();
6727 else
6728 synchronize_rcu();
1da177e4 6729}
d1b19dff 6730EXPORT_SYMBOL(synchronize_net);
1da177e4
LT
6731
6732/**
44a0873d 6733 * unregister_netdevice_queue - remove device from the kernel
1da177e4 6734 * @dev: device
44a0873d 6735 * @head: list
6ebfbc06 6736 *
1da177e4 6737 * This function shuts down a device interface and removes it
d59b54b1 6738 * from the kernel tables.
44a0873d 6739 * If head not NULL, device is queued to be unregistered later.
1da177e4
LT
6740 *
6741 * Callers must hold the rtnl semaphore. You may want
6742 * unregister_netdev() instead of this.
6743 */
6744
44a0873d 6745void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
1da177e4 6746{
a6620712
HX
6747 ASSERT_RTNL();
6748
44a0873d 6749 if (head) {
9fdce099 6750 list_move_tail(&dev->unreg_list, head);
44a0873d
ED
6751 } else {
6752 rollback_registered(dev);
6753 /* Finish processing unregister after unlock */
6754 net_set_todo(dev);
6755 }
1da177e4 6756}
44a0873d 6757EXPORT_SYMBOL(unregister_netdevice_queue);
1da177e4 6758
9b5e383c
ED
6759/**
6760 * unregister_netdevice_many - unregister many devices
6761 * @head: list of devices
9b5e383c
ED
6762 */
6763void unregister_netdevice_many(struct list_head *head)
6764{
6765 struct net_device *dev;
6766
6767 if (!list_empty(head)) {
6768 rollback_registered_many(head);
6769 list_for_each_entry(dev, head, unreg_list)
6770 net_set_todo(dev);
6771 }
6772}
63c8099d 6773EXPORT_SYMBOL(unregister_netdevice_many);
9b5e383c 6774
1da177e4
LT
6775/**
6776 * unregister_netdev - remove device from the kernel
6777 * @dev: device
6778 *
6779 * This function shuts down a device interface and removes it
d59b54b1 6780 * from the kernel tables.
1da177e4
LT
6781 *
6782 * This is just a wrapper for unregister_netdevice that takes
6783 * the rtnl semaphore. In general you want to use this and not
6784 * unregister_netdevice.
6785 */
6786void unregister_netdev(struct net_device *dev)
6787{
6788 rtnl_lock();
6789 unregister_netdevice(dev);
6790 rtnl_unlock();
6791}
1da177e4
LT
6792EXPORT_SYMBOL(unregister_netdev);
6793
ce286d32
EB
6794/**
6795 * dev_change_net_namespace - move device to different nethost namespace
6796 * @dev: device
6797 * @net: network namespace
6798 * @pat: If not NULL name pattern to try if the current device name
6799 * is already taken in the destination network namespace.
6800 *
6801 * This function shuts down a device interface and moves it
6802 * to a new network namespace. On success 0 is returned, on
6803 * a failure a netagive errno code is returned.
6804 *
6805 * Callers must hold the rtnl semaphore.
6806 */
6807
6808int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
6809{
ce286d32
EB
6810 int err;
6811
6812 ASSERT_RTNL();
6813
6814 /* Don't allow namespace local devices to be moved. */
6815 err = -EINVAL;
6816 if (dev->features & NETIF_F_NETNS_LOCAL)
6817 goto out;
6818
6819 /* Ensure the device has been registrered */
ce286d32
EB
6820 if (dev->reg_state != NETREG_REGISTERED)
6821 goto out;
6822
6823 /* Get out if there is nothing todo */
6824 err = 0;
878628fb 6825 if (net_eq(dev_net(dev), net))
ce286d32
EB
6826 goto out;
6827
6828 /* Pick the destination device name, and ensure
6829 * we can use it in the destination network namespace.
6830 */
6831 err = -EEXIST;
d9031024 6832 if (__dev_get_by_name(net, dev->name)) {
ce286d32
EB
6833 /* We get here if we can't use the current device name */
6834 if (!pat)
6835 goto out;
828de4f6 6836 if (dev_get_valid_name(net, dev, pat) < 0)
ce286d32
EB
6837 goto out;
6838 }
6839
6840 /*
6841 * And now a mini version of register_netdevice unregister_netdevice.
6842 */
6843
6844 /* If device is running close it first. */
9b772652 6845 dev_close(dev);
ce286d32
EB
6846
6847 /* And unlink it from device chain */
6848 err = -ENODEV;
6849 unlist_netdevice(dev);
6850
6851 synchronize_net();
6852
6853 /* Shutdown queueing discipline. */
6854 dev_shutdown(dev);
6855
6856 /* Notify protocols, that we are about to destroy
6857 this device. They should clean all the things.
3b27e105
DL
6858
6859 Note that dev->reg_state stays at NETREG_REGISTERED.
6860 This is wanted because this way 8021q and macvlan know
6861 the device is just moving and can keep their slaves up.
ce286d32
EB
6862 */
6863 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6549dd43
G
6864 rcu_barrier();
6865 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
d2237d35 6866 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
ce286d32
EB
6867
6868 /*
6869 * Flush the unicast and multicast chains
6870 */
a748ee24 6871 dev_uc_flush(dev);
22bedad3 6872 dev_mc_flush(dev);
ce286d32 6873
4e66ae2e
SH
6874 /* Send a netdev-removed uevent to the old namespace */
6875 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
6876
ce286d32 6877 /* Actually switch the network namespace */
c346dca1 6878 dev_net_set(dev, net);
ce286d32 6879
ce286d32
EB
6880 /* If there is an ifindex conflict assign a new one */
6881 if (__dev_get_by_index(net, dev->ifindex)) {
6882 int iflink = (dev->iflink == dev->ifindex);
6883 dev->ifindex = dev_new_index(net);
6884 if (iflink)
6885 dev->iflink = dev->ifindex;
6886 }
6887
4e66ae2e
SH
6888 /* Send a netdev-add uevent to the new namespace */
6889 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
6890
8b41d188 6891 /* Fixup kobjects */
a1b3f594 6892 err = device_rename(&dev->dev, dev->name);
8b41d188 6893 WARN_ON(err);
ce286d32
EB
6894
6895 /* Add the device back in the hashes */
6896 list_netdevice(dev);
6897
6898 /* Notify protocols, that a new device appeared. */
6899 call_netdevice_notifiers(NETDEV_REGISTER, dev);
6900
d90a909e
EB
6901 /*
6902 * Prevent userspace races by waiting until the network
6903 * device is fully setup before sending notifications.
6904 */
6905 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
6906
ce286d32
EB
6907 synchronize_net();
6908 err = 0;
6909out:
6910 return err;
6911}
463d0183 6912EXPORT_SYMBOL_GPL(dev_change_net_namespace);
ce286d32 6913
1da177e4
LT
6914static int dev_cpu_callback(struct notifier_block *nfb,
6915 unsigned long action,
6916 void *ocpu)
6917{
6918 struct sk_buff **list_skb;
1da177e4
LT
6919 struct sk_buff *skb;
6920 unsigned int cpu, oldcpu = (unsigned long)ocpu;
6921 struct softnet_data *sd, *oldsd;
6922
8bb78442 6923 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1da177e4
LT
6924 return NOTIFY_OK;
6925
6926 local_irq_disable();
6927 cpu = smp_processor_id();
6928 sd = &per_cpu(softnet_data, cpu);
6929 oldsd = &per_cpu(softnet_data, oldcpu);
6930
6931 /* Find end of our completion_queue. */
6932 list_skb = &sd->completion_queue;
6933 while (*list_skb)
6934 list_skb = &(*list_skb)->next;
6935 /* Append completion queue from offline CPU. */
6936 *list_skb = oldsd->completion_queue;
6937 oldsd->completion_queue = NULL;
6938
1da177e4 6939 /* Append output queue from offline CPU. */
a9cbd588
CG
6940 if (oldsd->output_queue) {
6941 *sd->output_queue_tailp = oldsd->output_queue;
6942 sd->output_queue_tailp = oldsd->output_queue_tailp;
6943 oldsd->output_queue = NULL;
6944 oldsd->output_queue_tailp = &oldsd->output_queue;
6945 }
264524d5
HC
6946 /* Append NAPI poll list from offline CPU. */
6947 if (!list_empty(&oldsd->poll_list)) {
6948 list_splice_init(&oldsd->poll_list, &sd->poll_list);
6949 raise_softirq_irqoff(NET_RX_SOFTIRQ);
6950 }
1da177e4
LT
6951
6952 raise_softirq_irqoff(NET_TX_SOFTIRQ);
6953 local_irq_enable();
6954
6955 /* Process offline CPU's input_pkt_queue */
76cc8b13 6956 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
1da177e4 6957 netif_rx(skb);
76cc8b13 6958 input_queue_head_incr(oldsd);
fec5e652 6959 }
76cc8b13 6960 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
6e7676c1 6961 netif_rx(skb);
76cc8b13
TH
6962 input_queue_head_incr(oldsd);
6963 }
1da177e4
LT
6964
6965 return NOTIFY_OK;
6966}
1da177e4
LT
6967
6968
7f353bf2 6969/**
b63365a2
HX
6970 * netdev_increment_features - increment feature set by one
6971 * @all: current feature set
6972 * @one: new feature set
6973 * @mask: mask feature set
7f353bf2
HX
6974 *
6975 * Computes a new feature set after adding a device with feature set
b63365a2
HX
6976 * @one to the master device with current feature set @all. Will not
6977 * enable anything that is off in @mask. Returns the new feature set.
7f353bf2 6978 */
c8f44aff
MM
6979netdev_features_t netdev_increment_features(netdev_features_t all,
6980 netdev_features_t one, netdev_features_t mask)
b63365a2 6981{
1742f183
MM
6982 if (mask & NETIF_F_GEN_CSUM)
6983 mask |= NETIF_F_ALL_CSUM;
6984 mask |= NETIF_F_VLAN_CHALLENGED;
7f353bf2 6985
1742f183
MM
6986 all |= one & (NETIF_F_ONE_FOR_ALL|NETIF_F_ALL_CSUM) & mask;
6987 all &= one | ~NETIF_F_ALL_FOR_ALL;
c6e1a0d1 6988
1742f183
MM
6989 /* If one device supports hw checksumming, set for all. */
6990 if (all & NETIF_F_GEN_CSUM)
6991 all &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_GEN_CSUM);
7f353bf2
HX
6992
6993 return all;
6994}
b63365a2 6995EXPORT_SYMBOL(netdev_increment_features);
7f353bf2 6996
30d97d35
PE
6997static struct hlist_head *netdev_create_hash(void)
6998{
6999 int i;
7000 struct hlist_head *hash;
7001
7002 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
7003 if (hash != NULL)
7004 for (i = 0; i < NETDEV_HASHENTRIES; i++)
7005 INIT_HLIST_HEAD(&hash[i]);
7006
7007 return hash;
7008}
7009
881d966b 7010/* Initialize per network namespace state */
4665079c 7011static int __net_init netdev_init(struct net *net)
881d966b 7012{
734b6541
RM
7013 if (net != &init_net)
7014 INIT_LIST_HEAD(&net->dev_base_head);
881d966b 7015
30d97d35
PE
7016 net->dev_name_head = netdev_create_hash();
7017 if (net->dev_name_head == NULL)
7018 goto err_name;
881d966b 7019
30d97d35
PE
7020 net->dev_index_head = netdev_create_hash();
7021 if (net->dev_index_head == NULL)
7022 goto err_idx;
881d966b
EB
7023
7024 return 0;
30d97d35
PE
7025
7026err_idx:
7027 kfree(net->dev_name_head);
7028err_name:
7029 return -ENOMEM;
881d966b
EB
7030}
7031
f0db275a
SH
7032/**
7033 * netdev_drivername - network driver for the device
7034 * @dev: network device
f0db275a
SH
7035 *
7036 * Determine network driver for device.
7037 */
3019de12 7038const char *netdev_drivername(const struct net_device *dev)
6579e57b 7039{
cf04a4c7
SH
7040 const struct device_driver *driver;
7041 const struct device *parent;
3019de12 7042 const char *empty = "";
6579e57b
AV
7043
7044 parent = dev->dev.parent;
6579e57b 7045 if (!parent)
3019de12 7046 return empty;
6579e57b
AV
7047
7048 driver = parent->driver;
7049 if (driver && driver->name)
3019de12
DM
7050 return driver->name;
7051 return empty;
6579e57b
AV
7052}
7053
b004ff49 7054static int __netdev_printk(const char *level, const struct net_device *dev,
256df2f3
JP
7055 struct va_format *vaf)
7056{
7057 int r;
7058
b004ff49 7059 if (dev && dev->dev.parent) {
666f355f
JP
7060 r = dev_printk_emit(level[1] - '0',
7061 dev->dev.parent,
7062 "%s %s %s: %pV",
7063 dev_driver_string(dev->dev.parent),
7064 dev_name(dev->dev.parent),
7065 netdev_name(dev), vaf);
b004ff49 7066 } else if (dev) {
256df2f3 7067 r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
b004ff49 7068 } else {
256df2f3 7069 r = printk("%s(NULL net_device): %pV", level, vaf);
b004ff49 7070 }
256df2f3
JP
7071
7072 return r;
7073}
7074
7075int netdev_printk(const char *level, const struct net_device *dev,
7076 const char *format, ...)
7077{
7078 struct va_format vaf;
7079 va_list args;
7080 int r;
7081
7082 va_start(args, format);
7083
7084 vaf.fmt = format;
7085 vaf.va = &args;
7086
7087 r = __netdev_printk(level, dev, &vaf);
b004ff49 7088
256df2f3
JP
7089 va_end(args);
7090
7091 return r;
7092}
7093EXPORT_SYMBOL(netdev_printk);
7094
7095#define define_netdev_printk_level(func, level) \
7096int func(const struct net_device *dev, const char *fmt, ...) \
7097{ \
7098 int r; \
7099 struct va_format vaf; \
7100 va_list args; \
7101 \
7102 va_start(args, fmt); \
7103 \
7104 vaf.fmt = fmt; \
7105 vaf.va = &args; \
7106 \
7107 r = __netdev_printk(level, dev, &vaf); \
b004ff49 7108 \
256df2f3
JP
7109 va_end(args); \
7110 \
7111 return r; \
7112} \
7113EXPORT_SYMBOL(func);
7114
7115define_netdev_printk_level(netdev_emerg, KERN_EMERG);
7116define_netdev_printk_level(netdev_alert, KERN_ALERT);
7117define_netdev_printk_level(netdev_crit, KERN_CRIT);
7118define_netdev_printk_level(netdev_err, KERN_ERR);
7119define_netdev_printk_level(netdev_warn, KERN_WARNING);
7120define_netdev_printk_level(netdev_notice, KERN_NOTICE);
7121define_netdev_printk_level(netdev_info, KERN_INFO);
7122
4665079c 7123static void __net_exit netdev_exit(struct net *net)
881d966b
EB
7124{
7125 kfree(net->dev_name_head);
7126 kfree(net->dev_index_head);
7127}
7128
022cbae6 7129static struct pernet_operations __net_initdata netdev_net_ops = {
881d966b
EB
7130 .init = netdev_init,
7131 .exit = netdev_exit,
7132};
7133
4665079c 7134static void __net_exit default_device_exit(struct net *net)
ce286d32 7135{
e008b5fc 7136 struct net_device *dev, *aux;
ce286d32 7137 /*
e008b5fc 7138 * Push all migratable network devices back to the
ce286d32
EB
7139 * initial network namespace
7140 */
7141 rtnl_lock();
e008b5fc 7142 for_each_netdev_safe(net, dev, aux) {
ce286d32 7143 int err;
aca51397 7144 char fb_name[IFNAMSIZ];
ce286d32
EB
7145
7146 /* Ignore unmoveable devices (i.e. loopback) */
7147 if (dev->features & NETIF_F_NETNS_LOCAL)
7148 continue;
7149
e008b5fc
EB
7150 /* Leave virtual devices for the generic cleanup */
7151 if (dev->rtnl_link_ops)
7152 continue;
d0c082ce 7153
25985edc 7154 /* Push remaining network devices to init_net */
aca51397
PE
7155 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
7156 err = dev_change_net_namespace(dev, &init_net, fb_name);
ce286d32 7157 if (err) {
7b6cd1ce
JP
7158 pr_emerg("%s: failed to move %s to init_net: %d\n",
7159 __func__, dev->name, err);
aca51397 7160 BUG();
ce286d32
EB
7161 }
7162 }
7163 rtnl_unlock();
7164}
7165
04dc7f6b
EB
7166static void __net_exit default_device_exit_batch(struct list_head *net_list)
7167{
7168 /* At exit all network devices most be removed from a network
b595076a 7169 * namespace. Do this in the reverse order of registration.
04dc7f6b
EB
7170 * Do this across as many network namespaces as possible to
7171 * improve batching efficiency.
7172 */
7173 struct net_device *dev;
7174 struct net *net;
7175 LIST_HEAD(dev_kill_list);
7176
7177 rtnl_lock();
7178 list_for_each_entry(net, net_list, exit_list) {
7179 for_each_netdev_reverse(net, dev) {
7180 if (dev->rtnl_link_ops)
7181 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
7182 else
7183 unregister_netdevice_queue(dev, &dev_kill_list);
7184 }
7185 }
7186 unregister_netdevice_many(&dev_kill_list);
ceaaec98 7187 list_del(&dev_kill_list);
04dc7f6b
EB
7188 rtnl_unlock();
7189}
7190
022cbae6 7191static struct pernet_operations __net_initdata default_device_ops = {
ce286d32 7192 .exit = default_device_exit,
04dc7f6b 7193 .exit_batch = default_device_exit_batch,
ce286d32
EB
7194};
7195
1da177e4
LT
7196/*
7197 * Initialize the DEV module. At boot time this walks the device list and
7198 * unhooks any devices that fail to initialise (normally hardware not
7199 * present) and leaves us with a valid list of present and active devices.
7200 *
7201 */
7202
7203/*
7204 * This is called single threaded during boot, so no need
7205 * to take the rtnl semaphore.
7206 */
7207static int __init net_dev_init(void)
7208{
7209 int i, rc = -ENOMEM;
7210
7211 BUG_ON(!dev_boot_phase);
7212
1da177e4
LT
7213 if (dev_proc_init())
7214 goto out;
7215
8b41d188 7216 if (netdev_kobject_init())
1da177e4
LT
7217 goto out;
7218
7219 INIT_LIST_HEAD(&ptype_all);
82d8a867 7220 for (i = 0; i < PTYPE_HASH_SIZE; i++)
1da177e4
LT
7221 INIT_LIST_HEAD(&ptype_base[i]);
7222
62532da9
VY
7223 INIT_LIST_HEAD(&offload_base);
7224
881d966b
EB
7225 if (register_pernet_subsys(&netdev_net_ops))
7226 goto out;
1da177e4
LT
7227
7228 /*
7229 * Initialise the packet receive queues.
7230 */
7231
6f912042 7232 for_each_possible_cpu(i) {
e36fa2f7 7233 struct softnet_data *sd = &per_cpu(softnet_data, i);
1da177e4 7234
dee42870 7235 memset(sd, 0, sizeof(*sd));
e36fa2f7 7236 skb_queue_head_init(&sd->input_pkt_queue);
6e7676c1 7237 skb_queue_head_init(&sd->process_queue);
e36fa2f7
ED
7238 sd->completion_queue = NULL;
7239 INIT_LIST_HEAD(&sd->poll_list);
a9cbd588
CG
7240 sd->output_queue = NULL;
7241 sd->output_queue_tailp = &sd->output_queue;
df334545 7242#ifdef CONFIG_RPS
e36fa2f7
ED
7243 sd->csd.func = rps_trigger_softirq;
7244 sd->csd.info = sd;
7245 sd->csd.flags = 0;
7246 sd->cpu = i;
1e94d72f 7247#endif
0a9627f2 7248
e36fa2f7
ED
7249 sd->backlog.poll = process_backlog;
7250 sd->backlog.weight = weight_p;
7251 sd->backlog.gro_list = NULL;
7252 sd->backlog.gro_count = 0;
1da177e4
LT
7253 }
7254
1da177e4
LT
7255 dev_boot_phase = 0;
7256
505d4f73
EB
7257 /* The loopback device is special if any other network devices
7258 * is present in a network namespace the loopback device must
7259 * be present. Since we now dynamically allocate and free the
7260 * loopback device ensure this invariant is maintained by
7261 * keeping the loopback device as the first device on the
7262 * list of network devices. Ensuring the loopback devices
7263 * is the first device that appears and the last network device
7264 * that disappears.
7265 */
7266 if (register_pernet_device(&loopback_net_ops))
7267 goto out;
7268
7269 if (register_pernet_device(&default_device_ops))
7270 goto out;
7271
962cf36c
CM
7272 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
7273 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
1da177e4
LT
7274
7275 hotcpu_notifier(dev_cpu_callback, 0);
7276 dst_init();
7277 dev_mcast_init();
7278 rc = 0;
7279out:
7280 return rc;
7281}
7282
7283subsys_initcall(net_dev_init);
7284
e88721f8
KK
7285static int __init initialize_hashrnd(void)
7286{
0a9627f2 7287 get_random_bytes(&hashrnd, sizeof(hashrnd));
e88721f8
KK
7288 return 0;
7289}
7290
7291late_initcall_sync(initialize_hashrnd);
7292