77d43ae2a7bbe1267f8430d5c35637d1984f463c
[muen/linux.git] / net / core / dev.c
1 /*
2  *      NET3    Protocol independent device support routines.
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  *      Derived from the non IP parts of dev.c 1.0.19
10  *              Authors:        Ross Biro
11  *                              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12  *                              Mark Evans, <evansmp@uhura.aston.ac.uk>
13  *
14  *      Additional Authors:
15  *              Florian la Roche <rzsfl@rz.uni-sb.de>
16  *              Alan Cox <gw4pts@gw4pts.ampr.org>
17  *              David Hinds <dahinds@users.sourceforge.net>
18  *              Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19  *              Adam Sulmicki <adam@cfar.umd.edu>
20  *              Pekka Riikonen <priikone@poesidon.pspt.fi>
21  *
22  *      Changes:
23  *              D.J. Barrow     :       Fixed bug where dev->refcnt gets set
24  *                                      to 2 if register_netdev gets called
25  *                                      before net_dev_init & also removed a
26  *                                      few lines of code in the process.
27  *              Alan Cox        :       device private ioctl copies fields back.
28  *              Alan Cox        :       Transmit queue code does relevant
29  *                                      stunts to keep the queue safe.
30  *              Alan Cox        :       Fixed double lock.
31  *              Alan Cox        :       Fixed promisc NULL pointer trap
32  *              ????????        :       Support the full private ioctl range
33  *              Alan Cox        :       Moved ioctl permission check into
34  *                                      drivers
35  *              Tim Kordas      :       SIOCADDMULTI/SIOCDELMULTI
36  *              Alan Cox        :       100 backlog just doesn't cut it when
37  *                                      you start doing multicast video 8)
38  *              Alan Cox        :       Rewrote net_bh and list manager.
39  *              Alan Cox        :       Fix ETH_P_ALL echoback lengths.
40  *              Alan Cox        :       Took out transmit every packet pass
41  *                                      Saved a few bytes in the ioctl handler
42  *              Alan Cox        :       Network driver sets packet type before
43  *                                      calling netif_rx. Saves a function
44  *                                      call a packet.
45  *              Alan Cox        :       Hashed net_bh()
46  *              Richard Kooijman:       Timestamp fixes.
47  *              Alan Cox        :       Wrong field in SIOCGIFDSTADDR
48  *              Alan Cox        :       Device lock protection.
49  *              Alan Cox        :       Fixed nasty side effect of device close
50  *                                      changes.
51  *              Rudi Cilibrasi  :       Pass the right thing to
52  *                                      set_mac_address()
53  *              Dave Miller     :       32bit quantity for the device lock to
54  *                                      make it work out on a Sparc.
55  *              Bjorn Ekwall    :       Added KERNELD hack.
56  *              Alan Cox        :       Cleaned up the backlog initialise.
57  *              Craig Metz      :       SIOCGIFCONF fix if space for under
58  *                                      1 device.
59  *          Thomas Bogendoerfer :       Return ENODEV for dev_open, if there
60  *                                      is no device open function.
61  *              Andi Kleen      :       Fix error reporting for SIOCGIFCONF
62  *          Michael Chastain    :       Fix signed/unsigned for SIOCGIFCONF
63  *              Cyrus Durgin    :       Cleaned for KMOD
64  *              Adam Sulmicki   :       Bug Fix : Network Device Unload
65  *                                      A network device unload needs to purge
66  *                                      the backlog queue.
67  *      Paul Rusty Russell      :       SIOCSIFNAME
68  *              Pekka Riikonen  :       Netdev boot-time settings code
69  *              Andrew Morton   :       Make unregister_netdevice wait
70  *                                      indefinitely on dev->refcnt
71  *              J Hadi Salim    :       - Backlog queue sampling
72  *                                      - netif_rx() feedback
73  */
74
75 #include <linux/uaccess.h>
76 #include <linux/bitops.h>
77 #include <linux/capability.h>
78 #include <linux/cpu.h>
79 #include <linux/types.h>
80 #include <linux/kernel.h>
81 #include <linux/hash.h>
82 #include <linux/slab.h>
83 #include <linux/sched.h>
84 #include <linux/sched/mm.h>
85 #include <linux/mutex.h>
86 #include <linux/string.h>
87 #include <linux/mm.h>
88 #include <linux/socket.h>
89 #include <linux/sockios.h>
90 #include <linux/errno.h>
91 #include <linux/interrupt.h>
92 #include <linux/if_ether.h>
93 #include <linux/netdevice.h>
94 #include <linux/etherdevice.h>
95 #include <linux/ethtool.h>
96 #include <linux/skbuff.h>
97 #include <linux/bpf.h>
98 #include <linux/bpf_trace.h>
99 #include <net/net_namespace.h>
100 #include <net/sock.h>
101 #include <net/busy_poll.h>
102 #include <linux/rtnetlink.h>
103 #include <linux/stat.h>
104 #include <net/dst.h>
105 #include <net/dst_metadata.h>
106 #include <net/pkt_sched.h>
107 #include <net/pkt_cls.h>
108 #include <net/checksum.h>
109 #include <net/xfrm.h>
110 #include <linux/highmem.h>
111 #include <linux/init.h>
112 #include <linux/module.h>
113 #include <linux/netpoll.h>
114 #include <linux/rcupdate.h>
115 #include <linux/delay.h>
116 #include <net/iw_handler.h>
117 #include <asm/current.h>
118 #include <linux/audit.h>
119 #include <linux/dmaengine.h>
120 #include <linux/err.h>
121 #include <linux/ctype.h>
122 #include <linux/if_arp.h>
123 #include <linux/if_vlan.h>
124 #include <linux/ip.h>
125 #include <net/ip.h>
126 #include <net/mpls.h>
127 #include <linux/ipv6.h>
128 #include <linux/in.h>
129 #include <linux/jhash.h>
130 #include <linux/random.h>
131 #include <trace/events/napi.h>
132 #include <trace/events/net.h>
133 #include <trace/events/skb.h>
134 #include <linux/pci.h>
135 #include <linux/inetdevice.h>
136 #include <linux/cpu_rmap.h>
137 #include <linux/static_key.h>
138 #include <linux/hashtable.h>
139 #include <linux/vmalloc.h>
140 #include <linux/if_macvlan.h>
141 #include <linux/errqueue.h>
142 #include <linux/hrtimer.h>
143 #include <linux/netfilter_ingress.h>
144 #include <linux/crash_dump.h>
145 #include <linux/sctp.h>
146 #include <net/udp_tunnel.h>
147 #include <linux/net_namespace.h>
148
149 #include "net-sysfs.h"
150
151 #define MAX_GRO_SKBS 8
152
153 /* This should be increased if a protocol with a bigger head is added. */
154 #define GRO_MAX_HEAD (MAX_HEADER + 128)
155
156 static DEFINE_SPINLOCK(ptype_lock);
157 static DEFINE_SPINLOCK(offload_lock);
158 struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
159 struct list_head ptype_all __read_mostly;       /* Taps */
160 static struct list_head offload_base __read_mostly;
161
162 static int netif_rx_internal(struct sk_buff *skb);
163 static int call_netdevice_notifiers_info(unsigned long val,
164                                          struct netdev_notifier_info *info);
165 static struct napi_struct *napi_by_id(unsigned int napi_id);
166
167 /*
168  * The @dev_base_head list is protected by @dev_base_lock and the rtnl
169  * semaphore.
170  *
171  * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
172  *
173  * Writers must hold the rtnl semaphore while they loop through the
174  * dev_base_head list, and hold dev_base_lock for writing when they do the
175  * actual updates.  This allows pure readers to access the list even
176  * while a writer is preparing to update it.
177  *
178  * To put it another way, dev_base_lock is held for writing only to
179  * protect against pure readers; the rtnl semaphore provides the
180  * protection against other writers.
181  *
182  * See, for example usages, register_netdevice() and
183  * unregister_netdevice(), which must be called with the rtnl
184  * semaphore held.
185  */
186 DEFINE_RWLOCK(dev_base_lock);
187 EXPORT_SYMBOL(dev_base_lock);
188
189 static DEFINE_MUTEX(ifalias_mutex);
190
191 /* protects napi_hash addition/deletion and napi_gen_id */
192 static DEFINE_SPINLOCK(napi_hash_lock);
193
194 static unsigned int napi_gen_id = NR_CPUS;
195 static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
196
197 static seqcount_t devnet_rename_seq;
198
199 static inline void dev_base_seq_inc(struct net *net)
200 {
201         while (++net->dev_base_seq == 0)
202                 ;
203 }
204
205 static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
206 {
207         unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
208
209         return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
210 }
211
212 static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
213 {
214         return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
215 }
216
217 static inline void rps_lock(struct softnet_data *sd)
218 {
219 #ifdef CONFIG_RPS
220         spin_lock(&sd->input_pkt_queue.lock);
221 #endif
222 }
223
224 static inline void rps_unlock(struct softnet_data *sd)
225 {
226 #ifdef CONFIG_RPS
227         spin_unlock(&sd->input_pkt_queue.lock);
228 #endif
229 }
230
231 /* Device list insertion */
232 static void list_netdevice(struct net_device *dev)
233 {
234         struct net *net = dev_net(dev);
235
236         ASSERT_RTNL();
237
238         write_lock_bh(&dev_base_lock);
239         list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
240         hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
241         hlist_add_head_rcu(&dev->index_hlist,
242                            dev_index_hash(net, dev->ifindex));
243         write_unlock_bh(&dev_base_lock);
244
245         dev_base_seq_inc(net);
246 }
247
248 /* Device list removal
249  * caller must respect a RCU grace period before freeing/reusing dev
250  */
251 static void unlist_netdevice(struct net_device *dev)
252 {
253         ASSERT_RTNL();
254
255         /* Unlink dev from the device chain */
256         write_lock_bh(&dev_base_lock);
257         list_del_rcu(&dev->dev_list);
258         hlist_del_rcu(&dev->name_hlist);
259         hlist_del_rcu(&dev->index_hlist);
260         write_unlock_bh(&dev_base_lock);
261
262         dev_base_seq_inc(dev_net(dev));
263 }
264
265 /*
266  *      Our notifier list
267  */
268
269 static RAW_NOTIFIER_HEAD(netdev_chain);
270
271 /*
272  *      Device drivers call our routines to queue packets here. We empty the
273  *      queue in the local softnet handler.
274  */
275
276 DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
277 EXPORT_PER_CPU_SYMBOL(softnet_data);
278
279 #ifdef CONFIG_LOCKDEP
280 /*
281  * register_netdevice() inits txq->_xmit_lock and sets lockdep class
282  * according to dev->type
283  */
284 static const unsigned short netdev_lock_type[] = {
285          ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
286          ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
287          ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
288          ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
289          ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
290          ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
291          ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
292          ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
293          ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
294          ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
295          ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
296          ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
297          ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
298          ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
299          ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
300
301 static const char *const netdev_lock_name[] = {
302         "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
303         "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
304         "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
305         "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
306         "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
307         "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
308         "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
309         "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
310         "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
311         "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
312         "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
313         "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
314         "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
315         "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
316         "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
317
318 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
319 static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
320
321 static inline unsigned short netdev_lock_pos(unsigned short dev_type)
322 {
323         int i;
324
325         for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
326                 if (netdev_lock_type[i] == dev_type)
327                         return i;
328         /* the last key is used by default */
329         return ARRAY_SIZE(netdev_lock_type) - 1;
330 }
331
332 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
333                                                  unsigned short dev_type)
334 {
335         int i;
336
337         i = netdev_lock_pos(dev_type);
338         lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
339                                    netdev_lock_name[i]);
340 }
341
342 static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
343 {
344         int i;
345
346         i = netdev_lock_pos(dev->type);
347         lockdep_set_class_and_name(&dev->addr_list_lock,
348                                    &netdev_addr_lock_key[i],
349                                    netdev_lock_name[i]);
350 }
351 #else
352 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
353                                                  unsigned short dev_type)
354 {
355 }
356 static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
357 {
358 }
359 #endif
360
361 /*******************************************************************************
362  *
363  *              Protocol management and registration routines
364  *
365  *******************************************************************************/
366
367
368 /*
369  *      Add a protocol ID to the list. Now that the input handler is
370  *      smarter we can dispense with all the messy stuff that used to be
371  *      here.
372  *
373  *      BEWARE!!! Protocol handlers, mangling input packets,
374  *      MUST BE last in hash buckets and checking protocol handlers
375  *      MUST start from promiscuous ptype_all chain in net_bh.
376  *      It is true now, do not change it.
377  *      Explanation follows: if protocol handler, mangling packet, will
378  *      be the first on list, it is not able to sense, that packet
379  *      is cloned and should be copied-on-write, so that it will
380  *      change it and subsequent readers will get broken packet.
381  *                                                      --ANK (980803)
382  */
383
384 static inline struct list_head *ptype_head(const struct packet_type *pt)
385 {
386         if (pt->type == htons(ETH_P_ALL))
387                 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
388         else
389                 return pt->dev ? &pt->dev->ptype_specific :
390                                  &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
391 }
392
393 /**
394  *      dev_add_pack - add packet handler
395  *      @pt: packet type declaration
396  *
397  *      Add a protocol handler to the networking stack. The passed &packet_type
398  *      is linked into kernel lists and may not be freed until it has been
399  *      removed from the kernel lists.
400  *
401  *      This call does not sleep therefore it can not
402  *      guarantee all CPU's that are in middle of receiving packets
403  *      will see the new packet type (until the next received packet).
404  */
405
406 void dev_add_pack(struct packet_type *pt)
407 {
408         struct list_head *head = ptype_head(pt);
409
410         spin_lock(&ptype_lock);
411         list_add_rcu(&pt->list, head);
412         spin_unlock(&ptype_lock);
413 }
414 EXPORT_SYMBOL(dev_add_pack);
415
416 /**
417  *      __dev_remove_pack        - remove packet handler
418  *      @pt: packet type declaration
419  *
420  *      Remove a protocol handler that was previously added to the kernel
421  *      protocol handlers by dev_add_pack(). The passed &packet_type is removed
422  *      from the kernel lists and can be freed or reused once this function
423  *      returns.
424  *
425  *      The packet type might still be in use by receivers
426  *      and must not be freed until after all the CPU's have gone
427  *      through a quiescent state.
428  */
429 void __dev_remove_pack(struct packet_type *pt)
430 {
431         struct list_head *head = ptype_head(pt);
432         struct packet_type *pt1;
433
434         spin_lock(&ptype_lock);
435
436         list_for_each_entry(pt1, head, list) {
437                 if (pt == pt1) {
438                         list_del_rcu(&pt->list);
439                         goto out;
440                 }
441         }
442
443         pr_warn("dev_remove_pack: %p not found\n", pt);
444 out:
445         spin_unlock(&ptype_lock);
446 }
447 EXPORT_SYMBOL(__dev_remove_pack);
448
449 /**
450  *      dev_remove_pack  - remove packet handler
451  *      @pt: packet type declaration
452  *
453  *      Remove a protocol handler that was previously added to the kernel
454  *      protocol handlers by dev_add_pack(). The passed &packet_type is removed
455  *      from the kernel lists and can be freed or reused once this function
456  *      returns.
457  *
458  *      This call sleeps to guarantee that no CPU is looking at the packet
459  *      type after return.
460  */
461 void dev_remove_pack(struct packet_type *pt)
462 {
463         __dev_remove_pack(pt);
464
465         synchronize_net();
466 }
467 EXPORT_SYMBOL(dev_remove_pack);
468
469
470 /**
471  *      dev_add_offload - register offload handlers
472  *      @po: protocol offload declaration
473  *
474  *      Add protocol offload handlers to the networking stack. The passed
475  *      &proto_offload is linked into kernel lists and may not be freed until
476  *      it has been removed from the kernel lists.
477  *
478  *      This call does not sleep therefore it can not
479  *      guarantee all CPU's that are in middle of receiving packets
480  *      will see the new offload handlers (until the next received packet).
481  */
482 void dev_add_offload(struct packet_offload *po)
483 {
484         struct packet_offload *elem;
485
486         spin_lock(&offload_lock);
487         list_for_each_entry(elem, &offload_base, list) {
488                 if (po->priority < elem->priority)
489                         break;
490         }
491         list_add_rcu(&po->list, elem->list.prev);
492         spin_unlock(&offload_lock);
493 }
494 EXPORT_SYMBOL(dev_add_offload);
495
496 /**
497  *      __dev_remove_offload     - remove offload handler
498  *      @po: packet offload declaration
499  *
500  *      Remove a protocol offload handler that was previously added to the
501  *      kernel offload handlers by dev_add_offload(). The passed &offload_type
502  *      is removed from the kernel lists and can be freed or reused once this
503  *      function returns.
504  *
505  *      The packet type might still be in use by receivers
506  *      and must not be freed until after all the CPU's have gone
507  *      through a quiescent state.
508  */
509 static void __dev_remove_offload(struct packet_offload *po)
510 {
511         struct list_head *head = &offload_base;
512         struct packet_offload *po1;
513
514         spin_lock(&offload_lock);
515
516         list_for_each_entry(po1, head, list) {
517                 if (po == po1) {
518                         list_del_rcu(&po->list);
519                         goto out;
520                 }
521         }
522
523         pr_warn("dev_remove_offload: %p not found\n", po);
524 out:
525         spin_unlock(&offload_lock);
526 }
527
528 /**
529  *      dev_remove_offload       - remove packet offload handler
530  *      @po: packet offload declaration
531  *
532  *      Remove a packet offload handler that was previously added to the kernel
533  *      offload handlers by dev_add_offload(). The passed &offload_type is
534  *      removed from the kernel lists and can be freed or reused once this
535  *      function returns.
536  *
537  *      This call sleeps to guarantee that no CPU is looking at the packet
538  *      type after return.
539  */
540 void dev_remove_offload(struct packet_offload *po)
541 {
542         __dev_remove_offload(po);
543
544         synchronize_net();
545 }
546 EXPORT_SYMBOL(dev_remove_offload);
547
548 /******************************************************************************
549  *
550  *                    Device Boot-time Settings Routines
551  *
552  ******************************************************************************/
553
554 /* Boot time configuration table */
555 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
556
557 /**
558  *      netdev_boot_setup_add   - add new setup entry
559  *      @name: name of the device
560  *      @map: configured settings for the device
561  *
562  *      Adds new setup entry to the dev_boot_setup list.  The function
563  *      returns 0 on error and 1 on success.  This is a generic routine to
564  *      all netdevices.
565  */
566 static int netdev_boot_setup_add(char *name, struct ifmap *map)
567 {
568         struct netdev_boot_setup *s;
569         int i;
570
571         s = dev_boot_setup;
572         for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
573                 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
574                         memset(s[i].name, 0, sizeof(s[i].name));
575                         strlcpy(s[i].name, name, IFNAMSIZ);
576                         memcpy(&s[i].map, map, sizeof(s[i].map));
577                         break;
578                 }
579         }
580
581         return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
582 }
583
584 /**
585  * netdev_boot_setup_check      - check boot time settings
586  * @dev: the netdevice
587  *
588  * Check boot time settings for the device.
589  * The found settings are set for the device to be used
590  * later in the device probing.
591  * Returns 0 if no settings found, 1 if they are.
592  */
593 int netdev_boot_setup_check(struct net_device *dev)
594 {
595         struct netdev_boot_setup *s = dev_boot_setup;
596         int i;
597
598         for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
599                 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
600                     !strcmp(dev->name, s[i].name)) {
601                         dev->irq = s[i].map.irq;
602                         dev->base_addr = s[i].map.base_addr;
603                         dev->mem_start = s[i].map.mem_start;
604                         dev->mem_end = s[i].map.mem_end;
605                         return 1;
606                 }
607         }
608         return 0;
609 }
610 EXPORT_SYMBOL(netdev_boot_setup_check);
611
612
613 /**
614  * netdev_boot_base     - get address from boot time settings
615  * @prefix: prefix for network device
616  * @unit: id for network device
617  *
618  * Check boot time settings for the base address of device.
619  * The found settings are set for the device to be used
620  * later in the device probing.
621  * Returns 0 if no settings found.
622  */
623 unsigned long netdev_boot_base(const char *prefix, int unit)
624 {
625         const struct netdev_boot_setup *s = dev_boot_setup;
626         char name[IFNAMSIZ];
627         int i;
628
629         sprintf(name, "%s%d", prefix, unit);
630
631         /*
632          * If device already registered then return base of 1
633          * to indicate not to probe for this interface
634          */
635         if (__dev_get_by_name(&init_net, name))
636                 return 1;
637
638         for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
639                 if (!strcmp(name, s[i].name))
640                         return s[i].map.base_addr;
641         return 0;
642 }
643
644 /*
645  * Saves at boot time configured settings for any netdevice.
646  */
647 int __init netdev_boot_setup(char *str)
648 {
649         int ints[5];
650         struct ifmap map;
651
652         str = get_options(str, ARRAY_SIZE(ints), ints);
653         if (!str || !*str)
654                 return 0;
655
656         /* Save settings */
657         memset(&map, 0, sizeof(map));
658         if (ints[0] > 0)
659                 map.irq = ints[1];
660         if (ints[0] > 1)
661                 map.base_addr = ints[2];
662         if (ints[0] > 2)
663                 map.mem_start = ints[3];
664         if (ints[0] > 3)
665                 map.mem_end = ints[4];
666
667         /* Add new entry to the list */
668         return netdev_boot_setup_add(str, &map);
669 }
670
671 __setup("netdev=", netdev_boot_setup);
672
673 /*******************************************************************************
674  *
675  *                          Device Interface Subroutines
676  *
677  *******************************************************************************/
678
679 /**
680  *      dev_get_iflink  - get 'iflink' value of a interface
681  *      @dev: targeted interface
682  *
683  *      Indicates the ifindex the interface is linked to.
684  *      Physical interfaces have the same 'ifindex' and 'iflink' values.
685  */
686
687 int dev_get_iflink(const struct net_device *dev)
688 {
689         if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
690                 return dev->netdev_ops->ndo_get_iflink(dev);
691
692         return dev->ifindex;
693 }
694 EXPORT_SYMBOL(dev_get_iflink);
695
696 /**
697  *      dev_fill_metadata_dst - Retrieve tunnel egress information.
698  *      @dev: targeted interface
699  *      @skb: The packet.
700  *
701  *      For better visibility of tunnel traffic OVS needs to retrieve
702  *      egress tunnel information for a packet. Following API allows
703  *      user to get this info.
704  */
705 int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
706 {
707         struct ip_tunnel_info *info;
708
709         if (!dev->netdev_ops  || !dev->netdev_ops->ndo_fill_metadata_dst)
710                 return -EINVAL;
711
712         info = skb_tunnel_info_unclone(skb);
713         if (!info)
714                 return -ENOMEM;
715         if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
716                 return -EINVAL;
717
718         return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
719 }
720 EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
721
722 /**
723  *      __dev_get_by_name       - find a device by its name
724  *      @net: the applicable net namespace
725  *      @name: name to find
726  *
727  *      Find an interface by name. Must be called under RTNL semaphore
728  *      or @dev_base_lock. If the name is found a pointer to the device
729  *      is returned. If the name is not found then %NULL is returned. The
730  *      reference counters are not incremented so the caller must be
731  *      careful with locks.
732  */
733
734 struct net_device *__dev_get_by_name(struct net *net, const char *name)
735 {
736         struct net_device *dev;
737         struct hlist_head *head = dev_name_hash(net, name);
738
739         hlist_for_each_entry(dev, head, name_hlist)
740                 if (!strncmp(dev->name, name, IFNAMSIZ))
741                         return dev;
742
743         return NULL;
744 }
745 EXPORT_SYMBOL(__dev_get_by_name);
746
747 /**
748  * dev_get_by_name_rcu  - find a device by its name
749  * @net: the applicable net namespace
750  * @name: name to find
751  *
752  * Find an interface by name.
753  * If the name is found a pointer to the device is returned.
754  * If the name is not found then %NULL is returned.
755  * The reference counters are not incremented so the caller must be
756  * careful with locks. The caller must hold RCU lock.
757  */
758
759 struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
760 {
761         struct net_device *dev;
762         struct hlist_head *head = dev_name_hash(net, name);
763
764         hlist_for_each_entry_rcu(dev, head, name_hlist)
765                 if (!strncmp(dev->name, name, IFNAMSIZ))
766                         return dev;
767
768         return NULL;
769 }
770 EXPORT_SYMBOL(dev_get_by_name_rcu);
771
772 /**
773  *      dev_get_by_name         - find a device by its name
774  *      @net: the applicable net namespace
775  *      @name: name to find
776  *
777  *      Find an interface by name. This can be called from any
778  *      context and does its own locking. The returned handle has
779  *      the usage count incremented and the caller must use dev_put() to
780  *      release it when it is no longer needed. %NULL is returned if no
781  *      matching device is found.
782  */
783
784 struct net_device *dev_get_by_name(struct net *net, const char *name)
785 {
786         struct net_device *dev;
787
788         rcu_read_lock();
789         dev = dev_get_by_name_rcu(net, name);
790         if (dev)
791                 dev_hold(dev);
792         rcu_read_unlock();
793         return dev;
794 }
795 EXPORT_SYMBOL(dev_get_by_name);
796
797 /**
798  *      __dev_get_by_index - find a device by its ifindex
799  *      @net: the applicable net namespace
800  *      @ifindex: index of device
801  *
802  *      Search for an interface by index. Returns %NULL if the device
803  *      is not found or a pointer to the device. The device has not
804  *      had its reference counter increased so the caller must be careful
805  *      about locking. The caller must hold either the RTNL semaphore
806  *      or @dev_base_lock.
807  */
808
809 struct net_device *__dev_get_by_index(struct net *net, int ifindex)
810 {
811         struct net_device *dev;
812         struct hlist_head *head = dev_index_hash(net, ifindex);
813
814         hlist_for_each_entry(dev, head, index_hlist)
815                 if (dev->ifindex == ifindex)
816                         return dev;
817
818         return NULL;
819 }
820 EXPORT_SYMBOL(__dev_get_by_index);
821
822 /**
823  *      dev_get_by_index_rcu - find a device by its ifindex
824  *      @net: the applicable net namespace
825  *      @ifindex: index of device
826  *
827  *      Search for an interface by index. Returns %NULL if the device
828  *      is not found or a pointer to the device. The device has not
829  *      had its reference counter increased so the caller must be careful
830  *      about locking. The caller must hold RCU lock.
831  */
832
833 struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
834 {
835         struct net_device *dev;
836         struct hlist_head *head = dev_index_hash(net, ifindex);
837
838         hlist_for_each_entry_rcu(dev, head, index_hlist)
839                 if (dev->ifindex == ifindex)
840                         return dev;
841
842         return NULL;
843 }
844 EXPORT_SYMBOL(dev_get_by_index_rcu);
845
846
847 /**
848  *      dev_get_by_index - find a device by its ifindex
849  *      @net: the applicable net namespace
850  *      @ifindex: index of device
851  *
852  *      Search for an interface by index. Returns NULL if the device
853  *      is not found or a pointer to the device. The device returned has
854  *      had a reference added and the pointer is safe until the user calls
855  *      dev_put to indicate they have finished with it.
856  */
857
858 struct net_device *dev_get_by_index(struct net *net, int ifindex)
859 {
860         struct net_device *dev;
861
862         rcu_read_lock();
863         dev = dev_get_by_index_rcu(net, ifindex);
864         if (dev)
865                 dev_hold(dev);
866         rcu_read_unlock();
867         return dev;
868 }
869 EXPORT_SYMBOL(dev_get_by_index);
870
871 /**
872  *      dev_get_by_napi_id - find a device by napi_id
873  *      @napi_id: ID of the NAPI struct
874  *
875  *      Search for an interface by NAPI ID. Returns %NULL if the device
876  *      is not found or a pointer to the device. The device has not had
877  *      its reference counter increased so the caller must be careful
878  *      about locking. The caller must hold RCU lock.
879  */
880
881 struct net_device *dev_get_by_napi_id(unsigned int napi_id)
882 {
883         struct napi_struct *napi;
884
885         WARN_ON_ONCE(!rcu_read_lock_held());
886
887         if (napi_id < MIN_NAPI_ID)
888                 return NULL;
889
890         napi = napi_by_id(napi_id);
891
892         return napi ? napi->dev : NULL;
893 }
894 EXPORT_SYMBOL(dev_get_by_napi_id);
895
896 /**
897  *      netdev_get_name - get a netdevice name, knowing its ifindex.
898  *      @net: network namespace
899  *      @name: a pointer to the buffer where the name will be stored.
900  *      @ifindex: the ifindex of the interface to get the name from.
901  *
902  *      The use of raw_seqcount_begin() and cond_resched() before
903  *      retrying is required as we want to give the writers a chance
904  *      to complete when CONFIG_PREEMPT is not set.
905  */
906 int netdev_get_name(struct net *net, char *name, int ifindex)
907 {
908         struct net_device *dev;
909         unsigned int seq;
910
911 retry:
912         seq = raw_seqcount_begin(&devnet_rename_seq);
913         rcu_read_lock();
914         dev = dev_get_by_index_rcu(net, ifindex);
915         if (!dev) {
916                 rcu_read_unlock();
917                 return -ENODEV;
918         }
919
920         strcpy(name, dev->name);
921         rcu_read_unlock();
922         if (read_seqcount_retry(&devnet_rename_seq, seq)) {
923                 cond_resched();
924                 goto retry;
925         }
926
927         return 0;
928 }
929
930 /**
931  *      dev_getbyhwaddr_rcu - find a device by its hardware address
932  *      @net: the applicable net namespace
933  *      @type: media type of device
934  *      @ha: hardware address
935  *
936  *      Search for an interface by MAC address. Returns NULL if the device
937  *      is not found or a pointer to the device.
938  *      The caller must hold RCU or RTNL.
939  *      The returned device has not had its ref count increased
940  *      and the caller must therefore be careful about locking
941  *
942  */
943
944 struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
945                                        const char *ha)
946 {
947         struct net_device *dev;
948
949         for_each_netdev_rcu(net, dev)
950                 if (dev->type == type &&
951                     !memcmp(dev->dev_addr, ha, dev->addr_len))
952                         return dev;
953
954         return NULL;
955 }
956 EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
957
958 struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
959 {
960         struct net_device *dev;
961
962         ASSERT_RTNL();
963         for_each_netdev(net, dev)
964                 if (dev->type == type)
965                         return dev;
966
967         return NULL;
968 }
969 EXPORT_SYMBOL(__dev_getfirstbyhwtype);
970
971 struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
972 {
973         struct net_device *dev, *ret = NULL;
974
975         rcu_read_lock();
976         for_each_netdev_rcu(net, dev)
977                 if (dev->type == type) {
978                         dev_hold(dev);
979                         ret = dev;
980                         break;
981                 }
982         rcu_read_unlock();
983         return ret;
984 }
985 EXPORT_SYMBOL(dev_getfirstbyhwtype);
986
987 /**
988  *      __dev_get_by_flags - find any device with given flags
989  *      @net: the applicable net namespace
990  *      @if_flags: IFF_* values
991  *      @mask: bitmask of bits in if_flags to check
992  *
993  *      Search for any interface with the given flags. Returns NULL if a device
994  *      is not found or a pointer to the device. Must be called inside
995  *      rtnl_lock(), and result refcount is unchanged.
996  */
997
998 struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
999                                       unsigned short mask)
1000 {
1001         struct net_device *dev, *ret;
1002
1003         ASSERT_RTNL();
1004
1005         ret = NULL;
1006         for_each_netdev(net, dev) {
1007                 if (((dev->flags ^ if_flags) & mask) == 0) {
1008                         ret = dev;
1009                         break;
1010                 }
1011         }
1012         return ret;
1013 }
1014 EXPORT_SYMBOL(__dev_get_by_flags);
1015
1016 /**
1017  *      dev_valid_name - check if name is okay for network device
1018  *      @name: name string
1019  *
1020  *      Network device names need to be valid file names to
1021  *      to allow sysfs to work.  We also disallow any kind of
1022  *      whitespace.
1023  */
1024 bool dev_valid_name(const char *name)
1025 {
1026         if (*name == '\0')
1027                 return false;
1028         if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
1029                 return false;
1030         if (!strcmp(name, ".") || !strcmp(name, ".."))
1031                 return false;
1032
1033         while (*name) {
1034                 if (*name == '/' || *name == ':' || isspace(*name))
1035                         return false;
1036                 name++;
1037         }
1038         return true;
1039 }
1040 EXPORT_SYMBOL(dev_valid_name);
1041
1042 /**
1043  *      __dev_alloc_name - allocate a name for a device
1044  *      @net: network namespace to allocate the device name in
1045  *      @name: name format string
1046  *      @buf:  scratch buffer and result name string
1047  *
1048  *      Passed a format string - eg "lt%d" it will try and find a suitable
1049  *      id. It scans list of devices to build up a free map, then chooses
1050  *      the first empty slot. The caller must hold the dev_base or rtnl lock
1051  *      while allocating the name and adding the device in order to avoid
1052  *      duplicates.
1053  *      Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1054  *      Returns the number of the unit assigned or a negative errno code.
1055  */
1056
1057 static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1058 {
1059         int i = 0;
1060         const char *p;
1061         const int max_netdevices = 8*PAGE_SIZE;
1062         unsigned long *inuse;
1063         struct net_device *d;
1064
1065         if (!dev_valid_name(name))
1066                 return -EINVAL;
1067
1068         p = strchr(name, '%');
1069         if (p) {
1070                 /*
1071                  * Verify the string as this thing may have come from
1072                  * the user.  There must be either one "%d" and no other "%"
1073                  * characters.
1074                  */
1075                 if (p[1] != 'd' || strchr(p + 2, '%'))
1076                         return -EINVAL;
1077
1078                 /* Use one page as a bit array of possible slots */
1079                 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1080                 if (!inuse)
1081                         return -ENOMEM;
1082
1083                 for_each_netdev(net, d) {
1084                         if (!sscanf(d->name, name, &i))
1085                                 continue;
1086                         if (i < 0 || i >= max_netdevices)
1087                                 continue;
1088
1089                         /*  avoid cases where sscanf is not exact inverse of printf */
1090                         snprintf(buf, IFNAMSIZ, name, i);
1091                         if (!strncmp(buf, d->name, IFNAMSIZ))
1092                                 set_bit(i, inuse);
1093                 }
1094
1095                 i = find_first_zero_bit(inuse, max_netdevices);
1096                 free_page((unsigned long) inuse);
1097         }
1098
1099         snprintf(buf, IFNAMSIZ, name, i);
1100         if (!__dev_get_by_name(net, buf))
1101                 return i;
1102
1103         /* It is possible to run out of possible slots
1104          * when the name is long and there isn't enough space left
1105          * for the digits, or if all bits are used.
1106          */
1107         return -ENFILE;
1108 }
1109
1110 static int dev_alloc_name_ns(struct net *net,
1111                              struct net_device *dev,
1112                              const char *name)
1113 {
1114         char buf[IFNAMSIZ];
1115         int ret;
1116
1117         BUG_ON(!net);
1118         ret = __dev_alloc_name(net, name, buf);
1119         if (ret >= 0)
1120                 strlcpy(dev->name, buf, IFNAMSIZ);
1121         return ret;
1122 }
1123
1124 /**
1125  *      dev_alloc_name - allocate a name for a device
1126  *      @dev: device
1127  *      @name: name format string
1128  *
1129  *      Passed a format string - eg "lt%d" it will try and find a suitable
1130  *      id. It scans list of devices to build up a free map, then chooses
1131  *      the first empty slot. The caller must hold the dev_base or rtnl lock
1132  *      while allocating the name and adding the device in order to avoid
1133  *      duplicates.
1134  *      Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1135  *      Returns the number of the unit assigned or a negative errno code.
1136  */
1137
1138 int dev_alloc_name(struct net_device *dev, const char *name)
1139 {
1140         return dev_alloc_name_ns(dev_net(dev), dev, name);
1141 }
1142 EXPORT_SYMBOL(dev_alloc_name);
1143
1144 int dev_get_valid_name(struct net *net, struct net_device *dev,
1145                        const char *name)
1146 {
1147         BUG_ON(!net);
1148
1149         if (!dev_valid_name(name))
1150                 return -EINVAL;
1151
1152         if (strchr(name, '%'))
1153                 return dev_alloc_name_ns(net, dev, name);
1154         else if (__dev_get_by_name(net, name))
1155                 return -EEXIST;
1156         else if (dev->name != name)
1157                 strlcpy(dev->name, name, IFNAMSIZ);
1158
1159         return 0;
1160 }
1161 EXPORT_SYMBOL(dev_get_valid_name);
1162
1163 /**
1164  *      dev_change_name - change name of a device
1165  *      @dev: device
1166  *      @newname: name (or format string) must be at least IFNAMSIZ
1167  *
1168  *      Change name of a device, can pass format strings "eth%d".
1169  *      for wildcarding.
1170  */
1171 int dev_change_name(struct net_device *dev, const char *newname)
1172 {
1173         unsigned char old_assign_type;
1174         char oldname[IFNAMSIZ];
1175         int err = 0;
1176         int ret;
1177         struct net *net;
1178
1179         ASSERT_RTNL();
1180         BUG_ON(!dev_net(dev));
1181
1182         net = dev_net(dev);
1183         if (dev->flags & IFF_UP)
1184                 return -EBUSY;
1185
1186         write_seqcount_begin(&devnet_rename_seq);
1187
1188         if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
1189                 write_seqcount_end(&devnet_rename_seq);
1190                 return 0;
1191         }
1192
1193         memcpy(oldname, dev->name, IFNAMSIZ);
1194
1195         err = dev_get_valid_name(net, dev, newname);
1196         if (err < 0) {
1197                 write_seqcount_end(&devnet_rename_seq);
1198                 return err;
1199         }
1200
1201         if (oldname[0] && !strchr(oldname, '%'))
1202                 netdev_info(dev, "renamed from %s\n", oldname);
1203
1204         old_assign_type = dev->name_assign_type;
1205         dev->name_assign_type = NET_NAME_RENAMED;
1206
1207 rollback:
1208         ret = device_rename(&dev->dev, dev->name);
1209         if (ret) {
1210                 memcpy(dev->name, oldname, IFNAMSIZ);
1211                 dev->name_assign_type = old_assign_type;
1212                 write_seqcount_end(&devnet_rename_seq);
1213                 return ret;
1214         }
1215
1216         write_seqcount_end(&devnet_rename_seq);
1217
1218         netdev_adjacent_rename_links(dev, oldname);
1219
1220         write_lock_bh(&dev_base_lock);
1221         hlist_del_rcu(&dev->name_hlist);
1222         write_unlock_bh(&dev_base_lock);
1223
1224         synchronize_rcu();
1225
1226         write_lock_bh(&dev_base_lock);
1227         hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
1228         write_unlock_bh(&dev_base_lock);
1229
1230         ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
1231         ret = notifier_to_errno(ret);
1232
1233         if (ret) {
1234                 /* err >= 0 after dev_alloc_name() or stores the first errno */
1235                 if (err >= 0) {
1236                         err = ret;
1237                         write_seqcount_begin(&devnet_rename_seq);
1238                         memcpy(dev->name, oldname, IFNAMSIZ);
1239                         memcpy(oldname, newname, IFNAMSIZ);
1240                         dev->name_assign_type = old_assign_type;
1241                         old_assign_type = NET_NAME_RENAMED;
1242                         goto rollback;
1243                 } else {
1244                         pr_err("%s: name change rollback failed: %d\n",
1245                                dev->name, ret);
1246                 }
1247         }
1248
1249         return err;
1250 }
1251
1252 /**
1253  *      dev_set_alias - change ifalias of a device
1254  *      @dev: device
1255  *      @alias: name up to IFALIASZ
1256  *      @len: limit of bytes to copy from info
1257  *
1258  *      Set ifalias for a device,
1259  */
1260 int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1261 {
1262         struct dev_ifalias *new_alias = NULL;
1263
1264         if (len >= IFALIASZ)
1265                 return -EINVAL;
1266
1267         if (len) {
1268                 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1269                 if (!new_alias)
1270                         return -ENOMEM;
1271
1272                 memcpy(new_alias->ifalias, alias, len);
1273                 new_alias->ifalias[len] = 0;
1274         }
1275
1276         mutex_lock(&ifalias_mutex);
1277         rcu_swap_protected(dev->ifalias, new_alias,
1278                            mutex_is_locked(&ifalias_mutex));
1279         mutex_unlock(&ifalias_mutex);
1280
1281         if (new_alias)
1282                 kfree_rcu(new_alias, rcuhead);
1283
1284         return len;
1285 }
1286 EXPORT_SYMBOL(dev_set_alias);
1287
1288 /**
1289  *      dev_get_alias - get ifalias of a device
1290  *      @dev: device
1291  *      @name: buffer to store name of ifalias
1292  *      @len: size of buffer
1293  *
1294  *      get ifalias for a device.  Caller must make sure dev cannot go
1295  *      away,  e.g. rcu read lock or own a reference count to device.
1296  */
1297 int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1298 {
1299         const struct dev_ifalias *alias;
1300         int ret = 0;
1301
1302         rcu_read_lock();
1303         alias = rcu_dereference(dev->ifalias);
1304         if (alias)
1305                 ret = snprintf(name, len, "%s", alias->ifalias);
1306         rcu_read_unlock();
1307
1308         return ret;
1309 }
1310
1311 /**
1312  *      netdev_features_change - device changes features
1313  *      @dev: device to cause notification
1314  *
1315  *      Called to indicate a device has changed features.
1316  */
1317 void netdev_features_change(struct net_device *dev)
1318 {
1319         call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
1320 }
1321 EXPORT_SYMBOL(netdev_features_change);
1322
1323 /**
1324  *      netdev_state_change - device changes state
1325  *      @dev: device to cause notification
1326  *
1327  *      Called to indicate a device has changed state. This function calls
1328  *      the notifier chains for netdev_chain and sends a NEWLINK message
1329  *      to the routing socket.
1330  */
1331 void netdev_state_change(struct net_device *dev)
1332 {
1333         if (dev->flags & IFF_UP) {
1334                 struct netdev_notifier_change_info change_info = {
1335                         .info.dev = dev,
1336                 };
1337
1338                 call_netdevice_notifiers_info(NETDEV_CHANGE,
1339                                               &change_info.info);
1340                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1341         }
1342 }
1343 EXPORT_SYMBOL(netdev_state_change);
1344
1345 /**
1346  * netdev_notify_peers - notify network peers about existence of @dev
1347  * @dev: network device
1348  *
1349  * Generate traffic such that interested network peers are aware of
1350  * @dev, such as by generating a gratuitous ARP. This may be used when
1351  * a device wants to inform the rest of the network about some sort of
1352  * reconfiguration such as a failover event or virtual machine
1353  * migration.
1354  */
1355 void netdev_notify_peers(struct net_device *dev)
1356 {
1357         rtnl_lock();
1358         call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1359         call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
1360         rtnl_unlock();
1361 }
1362 EXPORT_SYMBOL(netdev_notify_peers);
1363
1364 static int __dev_open(struct net_device *dev)
1365 {
1366         const struct net_device_ops *ops = dev->netdev_ops;
1367         int ret;
1368
1369         ASSERT_RTNL();
1370
1371         if (!netif_device_present(dev))
1372                 return -ENODEV;
1373
1374         /* Block netpoll from trying to do any rx path servicing.
1375          * If we don't do this there is a chance ndo_poll_controller
1376          * or ndo_poll may be running while we open the device
1377          */
1378         netpoll_poll_disable(dev);
1379
1380         ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1381         ret = notifier_to_errno(ret);
1382         if (ret)
1383                 return ret;
1384
1385         set_bit(__LINK_STATE_START, &dev->state);
1386
1387         if (ops->ndo_validate_addr)
1388                 ret = ops->ndo_validate_addr(dev);
1389
1390         if (!ret && ops->ndo_open)
1391                 ret = ops->ndo_open(dev);
1392
1393         netpoll_poll_enable(dev);
1394
1395         if (ret)
1396                 clear_bit(__LINK_STATE_START, &dev->state);
1397         else {
1398                 dev->flags |= IFF_UP;
1399                 dev_set_rx_mode(dev);
1400                 dev_activate(dev);
1401                 add_device_randomness(dev->dev_addr, dev->addr_len);
1402         }
1403
1404         return ret;
1405 }
1406
1407 /**
1408  *      dev_open        - prepare an interface for use.
1409  *      @dev:   device to open
1410  *
1411  *      Takes a device from down to up state. The device's private open
1412  *      function is invoked and then the multicast lists are loaded. Finally
1413  *      the device is moved into the up state and a %NETDEV_UP message is
1414  *      sent to the netdev notifier chain.
1415  *
1416  *      Calling this function on an active interface is a nop. On a failure
1417  *      a negative errno code is returned.
1418  */
1419 int dev_open(struct net_device *dev)
1420 {
1421         int ret;
1422
1423         if (dev->flags & IFF_UP)
1424                 return 0;
1425
1426         ret = __dev_open(dev);
1427         if (ret < 0)
1428                 return ret;
1429
1430         rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1431         call_netdevice_notifiers(NETDEV_UP, dev);
1432
1433         return ret;
1434 }
1435 EXPORT_SYMBOL(dev_open);
1436
1437 static void __dev_close_many(struct list_head *head)
1438 {
1439         struct net_device *dev;
1440
1441         ASSERT_RTNL();
1442         might_sleep();
1443
1444         list_for_each_entry(dev, head, close_list) {
1445                 /* Temporarily disable netpoll until the interface is down */
1446                 netpoll_poll_disable(dev);
1447
1448                 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1449
1450                 clear_bit(__LINK_STATE_START, &dev->state);
1451
1452                 /* Synchronize to scheduled poll. We cannot touch poll list, it
1453                  * can be even on different cpu. So just clear netif_running().
1454                  *
1455                  * dev->stop() will invoke napi_disable() on all of it's
1456                  * napi_struct instances on this device.
1457                  */
1458                 smp_mb__after_atomic(); /* Commit netif_running(). */
1459         }
1460
1461         dev_deactivate_many(head);
1462
1463         list_for_each_entry(dev, head, close_list) {
1464                 const struct net_device_ops *ops = dev->netdev_ops;
1465
1466                 /*
1467                  *      Call the device specific close. This cannot fail.
1468                  *      Only if device is UP
1469                  *
1470                  *      We allow it to be called even after a DETACH hot-plug
1471                  *      event.
1472                  */
1473                 if (ops->ndo_stop)
1474                         ops->ndo_stop(dev);
1475
1476                 dev->flags &= ~IFF_UP;
1477                 netpoll_poll_enable(dev);
1478         }
1479 }
1480
1481 static void __dev_close(struct net_device *dev)
1482 {
1483         LIST_HEAD(single);
1484
1485         list_add(&dev->close_list, &single);
1486         __dev_close_many(&single);
1487         list_del(&single);
1488 }
1489
1490 void dev_close_many(struct list_head *head, bool unlink)
1491 {
1492         struct net_device *dev, *tmp;
1493
1494         /* Remove the devices that don't need to be closed */
1495         list_for_each_entry_safe(dev, tmp, head, close_list)
1496                 if (!(dev->flags & IFF_UP))
1497                         list_del_init(&dev->close_list);
1498
1499         __dev_close_many(head);
1500
1501         list_for_each_entry_safe(dev, tmp, head, close_list) {
1502                 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1503                 call_netdevice_notifiers(NETDEV_DOWN, dev);
1504                 if (unlink)
1505                         list_del_init(&dev->close_list);
1506         }
1507 }
1508 EXPORT_SYMBOL(dev_close_many);
1509
1510 /**
1511  *      dev_close - shutdown an interface.
1512  *      @dev: device to shutdown
1513  *
1514  *      This function moves an active device into down state. A
1515  *      %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1516  *      is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1517  *      chain.
1518  */
1519 void dev_close(struct net_device *dev)
1520 {
1521         if (dev->flags & IFF_UP) {
1522                 LIST_HEAD(single);
1523
1524                 list_add(&dev->close_list, &single);
1525                 dev_close_many(&single, true);
1526                 list_del(&single);
1527         }
1528 }
1529 EXPORT_SYMBOL(dev_close);
1530
1531
1532 /**
1533  *      dev_disable_lro - disable Large Receive Offload on a device
1534  *      @dev: device
1535  *
1536  *      Disable Large Receive Offload (LRO) on a net device.  Must be
1537  *      called under RTNL.  This is needed if received packets may be
1538  *      forwarded to another interface.
1539  */
1540 void dev_disable_lro(struct net_device *dev)
1541 {
1542         struct net_device *lower_dev;
1543         struct list_head *iter;
1544
1545         dev->wanted_features &= ~NETIF_F_LRO;
1546         netdev_update_features(dev);
1547
1548         if (unlikely(dev->features & NETIF_F_LRO))
1549                 netdev_WARN(dev, "failed to disable LRO!\n");
1550
1551         netdev_for_each_lower_dev(dev, lower_dev, iter)
1552                 dev_disable_lro(lower_dev);
1553 }
1554 EXPORT_SYMBOL(dev_disable_lro);
1555
1556 /**
1557  *      dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1558  *      @dev: device
1559  *
1560  *      Disable HW Generic Receive Offload (GRO_HW) on a net device.  Must be
1561  *      called under RTNL.  This is needed if Generic XDP is installed on
1562  *      the device.
1563  */
1564 static void dev_disable_gro_hw(struct net_device *dev)
1565 {
1566         dev->wanted_features &= ~NETIF_F_GRO_HW;
1567         netdev_update_features(dev);
1568
1569         if (unlikely(dev->features & NETIF_F_GRO_HW))
1570                 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1571 }
1572
1573 const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1574 {
1575 #define N(val)                                          \
1576         case NETDEV_##val:                              \
1577                 return "NETDEV_" __stringify(val);
1578         switch (cmd) {
1579         N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1580         N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1581         N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1582         N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1583         N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1584         N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1585         N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
1586         N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1587         N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
1588         }
1589 #undef N
1590         return "UNKNOWN_NETDEV_EVENT";
1591 }
1592 EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1593
1594 static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1595                                    struct net_device *dev)
1596 {
1597         struct netdev_notifier_info info = {
1598                 .dev = dev,
1599         };
1600
1601         return nb->notifier_call(nb, val, &info);
1602 }
1603
1604 static int dev_boot_phase = 1;
1605
1606 /**
1607  * register_netdevice_notifier - register a network notifier block
1608  * @nb: notifier
1609  *
1610  * Register a notifier to be called when network device events occur.
1611  * The notifier passed is linked into the kernel structures and must
1612  * not be reused until it has been unregistered. A negative errno code
1613  * is returned on a failure.
1614  *
1615  * When registered all registration and up events are replayed
1616  * to the new notifier to allow device to have a race free
1617  * view of the network device list.
1618  */
1619
1620 int register_netdevice_notifier(struct notifier_block *nb)
1621 {
1622         struct net_device *dev;
1623         struct net_device *last;
1624         struct net *net;
1625         int err;
1626
1627         /* Close race with setup_net() and cleanup_net() */
1628         down_write(&pernet_ops_rwsem);
1629         rtnl_lock();
1630         err = raw_notifier_chain_register(&netdev_chain, nb);
1631         if (err)
1632                 goto unlock;
1633         if (dev_boot_phase)
1634                 goto unlock;
1635         for_each_net(net) {
1636                 for_each_netdev(net, dev) {
1637                         err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1638                         err = notifier_to_errno(err);
1639                         if (err)
1640                                 goto rollback;
1641
1642                         if (!(dev->flags & IFF_UP))
1643                                 continue;
1644
1645                         call_netdevice_notifier(nb, NETDEV_UP, dev);
1646                 }
1647         }
1648
1649 unlock:
1650         rtnl_unlock();
1651         up_write(&pernet_ops_rwsem);
1652         return err;
1653
1654 rollback:
1655         last = dev;
1656         for_each_net(net) {
1657                 for_each_netdev(net, dev) {
1658                         if (dev == last)
1659                                 goto outroll;
1660
1661                         if (dev->flags & IFF_UP) {
1662                                 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1663                                                         dev);
1664                                 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1665                         }
1666                         call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1667                 }
1668         }
1669
1670 outroll:
1671         raw_notifier_chain_unregister(&netdev_chain, nb);
1672         goto unlock;
1673 }
1674 EXPORT_SYMBOL(register_netdevice_notifier);
1675
1676 /**
1677  * unregister_netdevice_notifier - unregister a network notifier block
1678  * @nb: notifier
1679  *
1680  * Unregister a notifier previously registered by
1681  * register_netdevice_notifier(). The notifier is unlinked into the
1682  * kernel structures and may then be reused. A negative errno code
1683  * is returned on a failure.
1684  *
1685  * After unregistering unregister and down device events are synthesized
1686  * for all devices on the device list to the removed notifier to remove
1687  * the need for special case cleanup code.
1688  */
1689
1690 int unregister_netdevice_notifier(struct notifier_block *nb)
1691 {
1692         struct net_device *dev;
1693         struct net *net;
1694         int err;
1695
1696         /* Close race with setup_net() and cleanup_net() */
1697         down_write(&pernet_ops_rwsem);
1698         rtnl_lock();
1699         err = raw_notifier_chain_unregister(&netdev_chain, nb);
1700         if (err)
1701                 goto unlock;
1702
1703         for_each_net(net) {
1704                 for_each_netdev(net, dev) {
1705                         if (dev->flags & IFF_UP) {
1706                                 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1707                                                         dev);
1708                                 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1709                         }
1710                         call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1711                 }
1712         }
1713 unlock:
1714         rtnl_unlock();
1715         up_write(&pernet_ops_rwsem);
1716         return err;
1717 }
1718 EXPORT_SYMBOL(unregister_netdevice_notifier);
1719
1720 /**
1721  *      call_netdevice_notifiers_info - call all network notifier blocks
1722  *      @val: value passed unmodified to notifier function
1723  *      @info: notifier information data
1724  *
1725  *      Call all network notifier blocks.  Parameters and return value
1726  *      are as for raw_notifier_call_chain().
1727  */
1728
1729 static int call_netdevice_notifiers_info(unsigned long val,
1730                                          struct netdev_notifier_info *info)
1731 {
1732         ASSERT_RTNL();
1733         return raw_notifier_call_chain(&netdev_chain, val, info);
1734 }
1735
1736 /**
1737  *      call_netdevice_notifiers - call all network notifier blocks
1738  *      @val: value passed unmodified to notifier function
1739  *      @dev: net_device pointer passed unmodified to notifier function
1740  *
1741  *      Call all network notifier blocks.  Parameters and return value
1742  *      are as for raw_notifier_call_chain().
1743  */
1744
1745 int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1746 {
1747         struct netdev_notifier_info info = {
1748                 .dev = dev,
1749         };
1750
1751         return call_netdevice_notifiers_info(val, &info);
1752 }
1753 EXPORT_SYMBOL(call_netdevice_notifiers);
1754
1755 /**
1756  *      call_netdevice_notifiers_mtu - call all network notifier blocks
1757  *      @val: value passed unmodified to notifier function
1758  *      @dev: net_device pointer passed unmodified to notifier function
1759  *      @arg: additional u32 argument passed to the notifier function
1760  *
1761  *      Call all network notifier blocks.  Parameters and return value
1762  *      are as for raw_notifier_call_chain().
1763  */
1764 static int call_netdevice_notifiers_mtu(unsigned long val,
1765                                         struct net_device *dev, u32 arg)
1766 {
1767         struct netdev_notifier_info_ext info = {
1768                 .info.dev = dev,
1769                 .ext.mtu = arg,
1770         };
1771
1772         BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1773
1774         return call_netdevice_notifiers_info(val, &info.info);
1775 }
1776
1777 #ifdef CONFIG_NET_INGRESS
1778 static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
1779
1780 void net_inc_ingress_queue(void)
1781 {
1782         static_branch_inc(&ingress_needed_key);
1783 }
1784 EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1785
1786 void net_dec_ingress_queue(void)
1787 {
1788         static_branch_dec(&ingress_needed_key);
1789 }
1790 EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1791 #endif
1792
1793 #ifdef CONFIG_NET_EGRESS
1794 static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
1795
1796 void net_inc_egress_queue(void)
1797 {
1798         static_branch_inc(&egress_needed_key);
1799 }
1800 EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1801
1802 void net_dec_egress_queue(void)
1803 {
1804         static_branch_dec(&egress_needed_key);
1805 }
1806 EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1807 #endif
1808
1809 static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
1810 #ifdef HAVE_JUMP_LABEL
1811 static atomic_t netstamp_needed_deferred;
1812 static atomic_t netstamp_wanted;
1813 static void netstamp_clear(struct work_struct *work)
1814 {
1815         int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
1816         int wanted;
1817
1818         wanted = atomic_add_return(deferred, &netstamp_wanted);
1819         if (wanted > 0)
1820                 static_branch_enable(&netstamp_needed_key);
1821         else
1822                 static_branch_disable(&netstamp_needed_key);
1823 }
1824 static DECLARE_WORK(netstamp_work, netstamp_clear);
1825 #endif
1826
1827 void net_enable_timestamp(void)
1828 {
1829 #ifdef HAVE_JUMP_LABEL
1830         int wanted;
1831
1832         while (1) {
1833                 wanted = atomic_read(&netstamp_wanted);
1834                 if (wanted <= 0)
1835                         break;
1836                 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
1837                         return;
1838         }
1839         atomic_inc(&netstamp_needed_deferred);
1840         schedule_work(&netstamp_work);
1841 #else
1842         static_branch_inc(&netstamp_needed_key);
1843 #endif
1844 }
1845 EXPORT_SYMBOL(net_enable_timestamp);
1846
1847 void net_disable_timestamp(void)
1848 {
1849 #ifdef HAVE_JUMP_LABEL
1850         int wanted;
1851
1852         while (1) {
1853                 wanted = atomic_read(&netstamp_wanted);
1854                 if (wanted <= 1)
1855                         break;
1856                 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
1857                         return;
1858         }
1859         atomic_dec(&netstamp_needed_deferred);
1860         schedule_work(&netstamp_work);
1861 #else
1862         static_branch_dec(&netstamp_needed_key);
1863 #endif
1864 }
1865 EXPORT_SYMBOL(net_disable_timestamp);
1866
1867 static inline void net_timestamp_set(struct sk_buff *skb)
1868 {
1869         skb->tstamp = 0;
1870         if (static_branch_unlikely(&netstamp_needed_key))
1871                 __net_timestamp(skb);
1872 }
1873
1874 #define net_timestamp_check(COND, SKB)                          \
1875         if (static_branch_unlikely(&netstamp_needed_key)) {     \
1876                 if ((COND) && !(SKB)->tstamp)                   \
1877                         __net_timestamp(SKB);                   \
1878         }                                                       \
1879
1880 bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
1881 {
1882         unsigned int len;
1883
1884         if (!(dev->flags & IFF_UP))
1885                 return false;
1886
1887         len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1888         if (skb->len <= len)
1889                 return true;
1890
1891         /* if TSO is enabled, we don't care about the length as the packet
1892          * could be forwarded without being segmented before
1893          */
1894         if (skb_is_gso(skb))
1895                 return true;
1896
1897         return false;
1898 }
1899 EXPORT_SYMBOL_GPL(is_skb_forwardable);
1900
1901 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1902 {
1903         int ret = ____dev_forward_skb(dev, skb);
1904
1905         if (likely(!ret)) {
1906                 skb->protocol = eth_type_trans(skb, dev);
1907                 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1908         }
1909
1910         return ret;
1911 }
1912 EXPORT_SYMBOL_GPL(__dev_forward_skb);
1913
1914 /**
1915  * dev_forward_skb - loopback an skb to another netif
1916  *
1917  * @dev: destination network device
1918  * @skb: buffer to forward
1919  *
1920  * return values:
1921  *      NET_RX_SUCCESS  (no congestion)
1922  *      NET_RX_DROP     (packet was dropped, but freed)
1923  *
1924  * dev_forward_skb can be used for injecting an skb from the
1925  * start_xmit function of one device into the receive queue
1926  * of another device.
1927  *
1928  * The receiving device may be in another namespace, so
1929  * we have to clear all information in the skb that could
1930  * impact namespace isolation.
1931  */
1932 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1933 {
1934         return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
1935 }
1936 EXPORT_SYMBOL_GPL(dev_forward_skb);
1937
1938 static inline int deliver_skb(struct sk_buff *skb,
1939                               struct packet_type *pt_prev,
1940                               struct net_device *orig_dev)
1941 {
1942         if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
1943                 return -ENOMEM;
1944         refcount_inc(&skb->users);
1945         return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1946 }
1947
1948 static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1949                                           struct packet_type **pt,
1950                                           struct net_device *orig_dev,
1951                                           __be16 type,
1952                                           struct list_head *ptype_list)
1953 {
1954         struct packet_type *ptype, *pt_prev = *pt;
1955
1956         list_for_each_entry_rcu(ptype, ptype_list, list) {
1957                 if (ptype->type != type)
1958                         continue;
1959                 if (pt_prev)
1960                         deliver_skb(skb, pt_prev, orig_dev);
1961                 pt_prev = ptype;
1962         }
1963         *pt = pt_prev;
1964 }
1965
1966 static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1967 {
1968         if (!ptype->af_packet_priv || !skb->sk)
1969                 return false;
1970
1971         if (ptype->id_match)
1972                 return ptype->id_match(ptype, skb->sk);
1973         else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1974                 return true;
1975
1976         return false;
1977 }
1978
1979 /**
1980  * dev_nit_active - return true if any network interface taps are in use
1981  *
1982  * @dev: network device to check for the presence of taps
1983  */
1984 bool dev_nit_active(struct net_device *dev)
1985 {
1986         return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
1987 }
1988 EXPORT_SYMBOL_GPL(dev_nit_active);
1989
1990 /*
1991  *      Support routine. Sends outgoing frames to any network
1992  *      taps currently in use.
1993  */
1994
1995 void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1996 {
1997         struct packet_type *ptype;
1998         struct sk_buff *skb2 = NULL;
1999         struct packet_type *pt_prev = NULL;
2000         struct list_head *ptype_list = &ptype_all;
2001
2002         rcu_read_lock();
2003 again:
2004         list_for_each_entry_rcu(ptype, ptype_list, list) {
2005                 if (ptype->ignore_outgoing)
2006                         continue;
2007
2008                 /* Never send packets back to the socket
2009                  * they originated from - MvS (miquels@drinkel.ow.org)
2010                  */
2011                 if (skb_loop_sk(ptype, skb))
2012                         continue;
2013
2014                 if (pt_prev) {
2015                         deliver_skb(skb2, pt_prev, skb->dev);
2016                         pt_prev = ptype;
2017                         continue;
2018                 }
2019
2020                 /* need to clone skb, done only once */
2021                 skb2 = skb_clone(skb, GFP_ATOMIC);
2022                 if (!skb2)
2023                         goto out_unlock;
2024
2025                 net_timestamp_set(skb2);
2026
2027                 /* skb->nh should be correctly
2028                  * set by sender, so that the second statement is
2029                  * just protection against buggy protocols.
2030                  */
2031                 skb_reset_mac_header(skb2);
2032
2033                 if (skb_network_header(skb2) < skb2->data ||
2034                     skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2035                         net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2036                                              ntohs(skb2->protocol),
2037                                              dev->name);
2038                         skb_reset_network_header(skb2);
2039                 }
2040
2041                 skb2->transport_header = skb2->network_header;
2042                 skb2->pkt_type = PACKET_OUTGOING;
2043                 pt_prev = ptype;
2044         }
2045
2046         if (ptype_list == &ptype_all) {
2047                 ptype_list = &dev->ptype_all;
2048                 goto again;
2049         }
2050 out_unlock:
2051         if (pt_prev) {
2052                 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2053                         pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2054                 else
2055                         kfree_skb(skb2);
2056         }
2057         rcu_read_unlock();
2058 }
2059 EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
2060
2061 /**
2062  * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
2063  * @dev: Network device
2064  * @txq: number of queues available
2065  *
2066  * If real_num_tx_queues is changed the tc mappings may no longer be
2067  * valid. To resolve this verify the tc mapping remains valid and if
2068  * not NULL the mapping. With no priorities mapping to this
2069  * offset/count pair it will no longer be used. In the worst case TC0
2070  * is invalid nothing can be done so disable priority mappings. If is
2071  * expected that drivers will fix this mapping if they can before
2072  * calling netif_set_real_num_tx_queues.
2073  */
2074 static void netif_setup_tc(struct net_device *dev, unsigned int txq)
2075 {
2076         int i;
2077         struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2078
2079         /* If TC0 is invalidated disable TC mapping */
2080         if (tc->offset + tc->count > txq) {
2081                 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
2082                 dev->num_tc = 0;
2083                 return;
2084         }
2085
2086         /* Invalidated prio to tc mappings set to TC0 */
2087         for (i = 1; i < TC_BITMASK + 1; i++) {
2088                 int q = netdev_get_prio_tc_map(dev, i);
2089
2090                 tc = &dev->tc_to_txq[q];
2091                 if (tc->offset + tc->count > txq) {
2092                         pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2093                                 i, q);
2094                         netdev_set_prio_tc_map(dev, i, 0);
2095                 }
2096         }
2097 }
2098
2099 int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2100 {
2101         if (dev->num_tc) {
2102                 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2103                 int i;
2104
2105                 /* walk through the TCs and see if it falls into any of them */
2106                 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2107                         if ((txq - tc->offset) < tc->count)
2108                                 return i;
2109                 }
2110
2111                 /* didn't find it, just return -1 to indicate no match */
2112                 return -1;
2113         }
2114
2115         return 0;
2116 }
2117 EXPORT_SYMBOL(netdev_txq_to_tc);
2118
2119 #ifdef CONFIG_XPS
2120 struct static_key xps_needed __read_mostly;
2121 EXPORT_SYMBOL(xps_needed);
2122 struct static_key xps_rxqs_needed __read_mostly;
2123 EXPORT_SYMBOL(xps_rxqs_needed);
2124 static DEFINE_MUTEX(xps_map_mutex);
2125 #define xmap_dereference(P)             \
2126         rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2127
2128 static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2129                              int tci, u16 index)
2130 {
2131         struct xps_map *map = NULL;
2132         int pos;
2133
2134         if (dev_maps)
2135                 map = xmap_dereference(dev_maps->attr_map[tci]);
2136         if (!map)
2137                 return false;
2138
2139         for (pos = map->len; pos--;) {
2140                 if (map->queues[pos] != index)
2141                         continue;
2142
2143                 if (map->len > 1) {
2144                         map->queues[pos] = map->queues[--map->len];
2145                         break;
2146                 }
2147
2148                 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
2149                 kfree_rcu(map, rcu);
2150                 return false;
2151         }
2152
2153         return true;
2154 }
2155
2156 static bool remove_xps_queue_cpu(struct net_device *dev,
2157                                  struct xps_dev_maps *dev_maps,
2158                                  int cpu, u16 offset, u16 count)
2159 {
2160         int num_tc = dev->num_tc ? : 1;
2161         bool active = false;
2162         int tci;
2163
2164         for (tci = cpu * num_tc; num_tc--; tci++) {
2165                 int i, j;
2166
2167                 for (i = count, j = offset; i--; j++) {
2168                         if (!remove_xps_queue(dev_maps, tci, j))
2169                                 break;
2170                 }
2171
2172                 active |= i < 0;
2173         }
2174
2175         return active;
2176 }
2177
2178 static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2179                            struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2180                            u16 offset, u16 count, bool is_rxqs_map)
2181 {
2182         bool active = false;
2183         int i, j;
2184
2185         for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2186              j < nr_ids;)
2187                 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2188                                                count);
2189         if (!active) {
2190                 if (is_rxqs_map) {
2191                         RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2192                 } else {
2193                         RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2194
2195                         for (i = offset + (count - 1); count--; i--)
2196                                 netdev_queue_numa_node_write(
2197                                         netdev_get_tx_queue(dev, i),
2198                                                         NUMA_NO_NODE);
2199                 }
2200                 kfree_rcu(dev_maps, rcu);
2201         }
2202 }
2203
2204 static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2205                                    u16 count)
2206 {
2207         const unsigned long *possible_mask = NULL;
2208         struct xps_dev_maps *dev_maps;
2209         unsigned int nr_ids;
2210
2211         if (!static_key_false(&xps_needed))
2212                 return;
2213
2214         cpus_read_lock();
2215         mutex_lock(&xps_map_mutex);
2216
2217         if (static_key_false(&xps_rxqs_needed)) {
2218                 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2219                 if (dev_maps) {
2220                         nr_ids = dev->num_rx_queues;
2221                         clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2222                                        offset, count, true);
2223                 }
2224         }
2225
2226         dev_maps = xmap_dereference(dev->xps_cpus_map);
2227         if (!dev_maps)
2228                 goto out_no_maps;
2229
2230         if (num_possible_cpus() > 1)
2231                 possible_mask = cpumask_bits(cpu_possible_mask);
2232         nr_ids = nr_cpu_ids;
2233         clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2234                        false);
2235
2236 out_no_maps:
2237         if (static_key_enabled(&xps_rxqs_needed))
2238                 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2239
2240         static_key_slow_dec_cpuslocked(&xps_needed);
2241         mutex_unlock(&xps_map_mutex);
2242         cpus_read_unlock();
2243 }
2244
2245 static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2246 {
2247         netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2248 }
2249
2250 static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2251                                       u16 index, bool is_rxqs_map)
2252 {
2253         struct xps_map *new_map;
2254         int alloc_len = XPS_MIN_MAP_ALLOC;
2255         int i, pos;
2256
2257         for (pos = 0; map && pos < map->len; pos++) {
2258                 if (map->queues[pos] != index)
2259                         continue;
2260                 return map;
2261         }
2262
2263         /* Need to add tx-queue to this CPU's/rx-queue's existing map */
2264         if (map) {
2265                 if (pos < map->alloc_len)
2266                         return map;
2267
2268                 alloc_len = map->alloc_len * 2;
2269         }
2270
2271         /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2272          *  map
2273          */
2274         if (is_rxqs_map)
2275                 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2276         else
2277                 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2278                                        cpu_to_node(attr_index));
2279         if (!new_map)
2280                 return NULL;
2281
2282         for (i = 0; i < pos; i++)
2283                 new_map->queues[i] = map->queues[i];
2284         new_map->alloc_len = alloc_len;
2285         new_map->len = pos;
2286
2287         return new_map;
2288 }
2289
2290 /* Must be called under cpus_read_lock */
2291 int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2292                           u16 index, bool is_rxqs_map)
2293 {
2294         const unsigned long *online_mask = NULL, *possible_mask = NULL;
2295         struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
2296         int i, j, tci, numa_node_id = -2;
2297         int maps_sz, num_tc = 1, tc = 0;
2298         struct xps_map *map, *new_map;
2299         bool active = false;
2300         unsigned int nr_ids;
2301
2302         if (dev->num_tc) {
2303                 /* Do not allow XPS on subordinate device directly */
2304                 num_tc = dev->num_tc;
2305                 if (num_tc < 0)
2306                         return -EINVAL;
2307
2308                 /* If queue belongs to subordinate dev use its map */
2309                 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2310
2311                 tc = netdev_txq_to_tc(dev, index);
2312                 if (tc < 0)
2313                         return -EINVAL;
2314         }
2315
2316         mutex_lock(&xps_map_mutex);
2317         if (is_rxqs_map) {
2318                 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2319                 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2320                 nr_ids = dev->num_rx_queues;
2321         } else {
2322                 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2323                 if (num_possible_cpus() > 1) {
2324                         online_mask = cpumask_bits(cpu_online_mask);
2325                         possible_mask = cpumask_bits(cpu_possible_mask);
2326                 }
2327                 dev_maps = xmap_dereference(dev->xps_cpus_map);
2328                 nr_ids = nr_cpu_ids;
2329         }
2330
2331         if (maps_sz < L1_CACHE_BYTES)
2332                 maps_sz = L1_CACHE_BYTES;
2333
2334         /* allocate memory for queue storage */
2335         for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2336              j < nr_ids;) {
2337                 if (!new_dev_maps)
2338                         new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2339                 if (!new_dev_maps) {
2340                         mutex_unlock(&xps_map_mutex);
2341                         return -ENOMEM;
2342                 }
2343
2344                 tci = j * num_tc + tc;
2345                 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
2346                                  NULL;
2347
2348                 map = expand_xps_map(map, j, index, is_rxqs_map);
2349                 if (!map)
2350                         goto error;
2351
2352                 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2353         }
2354
2355         if (!new_dev_maps)
2356                 goto out_no_new_maps;
2357
2358         static_key_slow_inc_cpuslocked(&xps_needed);
2359         if (is_rxqs_map)
2360                 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2361
2362         for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2363              j < nr_ids;) {
2364                 /* copy maps belonging to foreign traffic classes */
2365                 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
2366                         /* fill in the new device map from the old device map */
2367                         map = xmap_dereference(dev_maps->attr_map[tci]);
2368                         RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2369                 }
2370
2371                 /* We need to explicitly update tci as prevous loop
2372                  * could break out early if dev_maps is NULL.
2373                  */
2374                 tci = j * num_tc + tc;
2375
2376                 if (netif_attr_test_mask(j, mask, nr_ids) &&
2377                     netif_attr_test_online(j, online_mask, nr_ids)) {
2378                         /* add tx-queue to CPU/rx-queue maps */
2379                         int pos = 0;
2380
2381                         map = xmap_dereference(new_dev_maps->attr_map[tci]);
2382                         while ((pos < map->len) && (map->queues[pos] != index))
2383                                 pos++;
2384
2385                         if (pos == map->len)
2386                                 map->queues[map->len++] = index;
2387 #ifdef CONFIG_NUMA
2388                         if (!is_rxqs_map) {
2389                                 if (numa_node_id == -2)
2390                                         numa_node_id = cpu_to_node(j);
2391                                 else if (numa_node_id != cpu_to_node(j))
2392                                         numa_node_id = -1;
2393                         }
2394 #endif
2395                 } else if (dev_maps) {
2396                         /* fill in the new device map from the old device map */
2397                         map = xmap_dereference(dev_maps->attr_map[tci]);
2398                         RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2399                 }
2400
2401                 /* copy maps belonging to foreign traffic classes */
2402                 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2403                         /* fill in the new device map from the old device map */
2404                         map = xmap_dereference(dev_maps->attr_map[tci]);
2405                         RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2406                 }
2407         }
2408
2409         if (is_rxqs_map)
2410                 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2411         else
2412                 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
2413
2414         /* Cleanup old maps */
2415         if (!dev_maps)
2416                 goto out_no_old_maps;
2417
2418         for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2419              j < nr_ids;) {
2420                 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2421                         new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2422                         map = xmap_dereference(dev_maps->attr_map[tci]);
2423                         if (map && map != new_map)
2424                                 kfree_rcu(map, rcu);
2425                 }
2426         }
2427
2428         kfree_rcu(dev_maps, rcu);
2429
2430 out_no_old_maps:
2431         dev_maps = new_dev_maps;
2432         active = true;
2433
2434 out_no_new_maps:
2435         if (!is_rxqs_map) {
2436                 /* update Tx queue numa node */
2437                 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2438                                              (numa_node_id >= 0) ?
2439                                              numa_node_id : NUMA_NO_NODE);
2440         }
2441
2442         if (!dev_maps)
2443                 goto out_no_maps;
2444
2445         /* removes tx-queue from unused CPUs/rx-queues */
2446         for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2447              j < nr_ids;) {
2448                 for (i = tc, tci = j * num_tc; i--; tci++)
2449                         active |= remove_xps_queue(dev_maps, tci, index);
2450                 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2451                     !netif_attr_test_online(j, online_mask, nr_ids))
2452                         active |= remove_xps_queue(dev_maps, tci, index);
2453                 for (i = num_tc - tc, tci++; --i; tci++)
2454                         active |= remove_xps_queue(dev_maps, tci, index);
2455         }
2456
2457         /* free map if not active */
2458         if (!active) {
2459                 if (is_rxqs_map)
2460                         RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2461                 else
2462                         RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2463                 kfree_rcu(dev_maps, rcu);
2464         }
2465
2466 out_no_maps:
2467         mutex_unlock(&xps_map_mutex);
2468
2469         return 0;
2470 error:
2471         /* remove any maps that we added */
2472         for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2473              j < nr_ids;) {
2474                 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2475                         new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2476                         map = dev_maps ?
2477                               xmap_dereference(dev_maps->attr_map[tci]) :
2478                               NULL;
2479                         if (new_map && new_map != map)
2480                                 kfree(new_map);
2481                 }
2482         }
2483
2484         mutex_unlock(&xps_map_mutex);
2485
2486         kfree(new_dev_maps);
2487         return -ENOMEM;
2488 }
2489 EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
2490
2491 int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2492                         u16 index)
2493 {
2494         int ret;
2495
2496         cpus_read_lock();
2497         ret =  __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2498         cpus_read_unlock();
2499
2500         return ret;
2501 }
2502 EXPORT_SYMBOL(netif_set_xps_queue);
2503
2504 #endif
2505 static void netdev_unbind_all_sb_channels(struct net_device *dev)
2506 {
2507         struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2508
2509         /* Unbind any subordinate channels */
2510         while (txq-- != &dev->_tx[0]) {
2511                 if (txq->sb_dev)
2512                         netdev_unbind_sb_channel(dev, txq->sb_dev);
2513         }
2514 }
2515
2516 void netdev_reset_tc(struct net_device *dev)
2517 {
2518 #ifdef CONFIG_XPS
2519         netif_reset_xps_queues_gt(dev, 0);
2520 #endif
2521         netdev_unbind_all_sb_channels(dev);
2522
2523         /* Reset TC configuration of device */
2524         dev->num_tc = 0;
2525         memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2526         memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2527 }
2528 EXPORT_SYMBOL(netdev_reset_tc);
2529
2530 int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2531 {
2532         if (tc >= dev->num_tc)
2533                 return -EINVAL;
2534
2535 #ifdef CONFIG_XPS
2536         netif_reset_xps_queues(dev, offset, count);
2537 #endif
2538         dev->tc_to_txq[tc].count = count;
2539         dev->tc_to_txq[tc].offset = offset;
2540         return 0;
2541 }
2542 EXPORT_SYMBOL(netdev_set_tc_queue);
2543
2544 int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2545 {
2546         if (num_tc > TC_MAX_QUEUE)
2547                 return -EINVAL;
2548
2549 #ifdef CONFIG_XPS
2550         netif_reset_xps_queues_gt(dev, 0);
2551 #endif
2552         netdev_unbind_all_sb_channels(dev);
2553
2554         dev->num_tc = num_tc;
2555         return 0;
2556 }
2557 EXPORT_SYMBOL(netdev_set_num_tc);
2558
2559 void netdev_unbind_sb_channel(struct net_device *dev,
2560                               struct net_device *sb_dev)
2561 {
2562         struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2563
2564 #ifdef CONFIG_XPS
2565         netif_reset_xps_queues_gt(sb_dev, 0);
2566 #endif
2567         memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2568         memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2569
2570         while (txq-- != &dev->_tx[0]) {
2571                 if (txq->sb_dev == sb_dev)
2572                         txq->sb_dev = NULL;
2573         }
2574 }
2575 EXPORT_SYMBOL(netdev_unbind_sb_channel);
2576
2577 int netdev_bind_sb_channel_queue(struct net_device *dev,
2578                                  struct net_device *sb_dev,
2579                                  u8 tc, u16 count, u16 offset)
2580 {
2581         /* Make certain the sb_dev and dev are already configured */
2582         if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2583                 return -EINVAL;
2584
2585         /* We cannot hand out queues we don't have */
2586         if ((offset + count) > dev->real_num_tx_queues)
2587                 return -EINVAL;
2588
2589         /* Record the mapping */
2590         sb_dev->tc_to_txq[tc].count = count;
2591         sb_dev->tc_to_txq[tc].offset = offset;
2592
2593         /* Provide a way for Tx queue to find the tc_to_txq map or
2594          * XPS map for itself.
2595          */
2596         while (count--)
2597                 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2598
2599         return 0;
2600 }
2601 EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2602
2603 int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2604 {
2605         /* Do not use a multiqueue device to represent a subordinate channel */
2606         if (netif_is_multiqueue(dev))
2607                 return -ENODEV;
2608
2609         /* We allow channels 1 - 32767 to be used for subordinate channels.
2610          * Channel 0 is meant to be "native" mode and used only to represent
2611          * the main root device. We allow writing 0 to reset the device back
2612          * to normal mode after being used as a subordinate channel.
2613          */
2614         if (channel > S16_MAX)
2615                 return -EINVAL;
2616
2617         dev->num_tc = -channel;
2618
2619         return 0;
2620 }
2621 EXPORT_SYMBOL(netdev_set_sb_channel);
2622
2623 /*
2624  * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2625  * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
2626  */
2627 int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
2628 {
2629         bool disabling;
2630         int rc;
2631
2632         disabling = txq < dev->real_num_tx_queues;
2633
2634         if (txq < 1 || txq > dev->num_tx_queues)
2635                 return -EINVAL;
2636
2637         if (dev->reg_state == NETREG_REGISTERED ||
2638             dev->reg_state == NETREG_UNREGISTERING) {
2639                 ASSERT_RTNL();
2640
2641                 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2642                                                   txq);
2643                 if (rc)
2644                         return rc;
2645
2646                 if (dev->num_tc)
2647                         netif_setup_tc(dev, txq);
2648
2649                 dev->real_num_tx_queues = txq;
2650
2651                 if (disabling) {
2652                         synchronize_net();
2653                         qdisc_reset_all_tx_gt(dev, txq);
2654 #ifdef CONFIG_XPS
2655                         netif_reset_xps_queues_gt(dev, txq);
2656 #endif
2657                 }
2658         } else {
2659                 dev->real_num_tx_queues = txq;
2660         }
2661
2662         return 0;
2663 }
2664 EXPORT_SYMBOL(netif_set_real_num_tx_queues);
2665
2666 #ifdef CONFIG_SYSFS
2667 /**
2668  *      netif_set_real_num_rx_queues - set actual number of RX queues used
2669  *      @dev: Network device
2670  *      @rxq: Actual number of RX queues
2671  *
2672  *      This must be called either with the rtnl_lock held or before
2673  *      registration of the net device.  Returns 0 on success, or a
2674  *      negative error code.  If called before registration, it always
2675  *      succeeds.
2676  */
2677 int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2678 {
2679         int rc;
2680
2681         if (rxq < 1 || rxq > dev->num_rx_queues)
2682                 return -EINVAL;
2683
2684         if (dev->reg_state == NETREG_REGISTERED) {
2685                 ASSERT_RTNL();
2686
2687                 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2688                                                   rxq);
2689                 if (rc)
2690                         return rc;
2691         }
2692
2693         dev->real_num_rx_queues = rxq;
2694         return 0;
2695 }
2696 EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2697 #endif
2698
2699 /**
2700  * netif_get_num_default_rss_queues - default number of RSS queues
2701  *
2702  * This routine should set an upper limit on the number of RSS queues
2703  * used by default by multiqueue devices.
2704  */
2705 int netif_get_num_default_rss_queues(void)
2706 {
2707         return is_kdump_kernel() ?
2708                 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
2709 }
2710 EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2711
2712 static void __netif_reschedule(struct Qdisc *q)
2713 {
2714         struct softnet_data *sd;
2715         unsigned long flags;
2716
2717         local_irq_save(flags);
2718         sd = this_cpu_ptr(&softnet_data);
2719         q->next_sched = NULL;
2720         *sd->output_queue_tailp = q;
2721         sd->output_queue_tailp = &q->next_sched;
2722         raise_softirq_irqoff(NET_TX_SOFTIRQ);
2723         local_irq_restore(flags);
2724 }
2725
2726 void __netif_schedule(struct Qdisc *q)
2727 {
2728         if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2729                 __netif_reschedule(q);
2730 }
2731 EXPORT_SYMBOL(__netif_schedule);
2732
2733 struct dev_kfree_skb_cb {
2734         enum skb_free_reason reason;
2735 };
2736
2737 static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
2738 {
2739         return (struct dev_kfree_skb_cb *)skb->cb;
2740 }
2741
2742 void netif_schedule_queue(struct netdev_queue *txq)
2743 {
2744         rcu_read_lock();
2745         if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
2746                 struct Qdisc *q = rcu_dereference(txq->qdisc);
2747
2748                 __netif_schedule(q);
2749         }
2750         rcu_read_unlock();
2751 }
2752 EXPORT_SYMBOL(netif_schedule_queue);
2753
2754 void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2755 {
2756         if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2757                 struct Qdisc *q;
2758
2759                 rcu_read_lock();
2760                 q = rcu_dereference(dev_queue->qdisc);
2761                 __netif_schedule(q);
2762                 rcu_read_unlock();
2763         }
2764 }
2765 EXPORT_SYMBOL(netif_tx_wake_queue);
2766
2767 void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2768 {
2769         unsigned long flags;
2770
2771         if (unlikely(!skb))
2772                 return;
2773
2774         if (likely(refcount_read(&skb->users) == 1)) {
2775                 smp_rmb();
2776                 refcount_set(&skb->users, 0);
2777         } else if (likely(!refcount_dec_and_test(&skb->users))) {
2778                 return;
2779         }
2780         get_kfree_skb_cb(skb)->reason = reason;
2781         local_irq_save(flags);
2782         skb->next = __this_cpu_read(softnet_data.completion_queue);
2783         __this_cpu_write(softnet_data.completion_queue, skb);
2784         raise_softirq_irqoff(NET_TX_SOFTIRQ);
2785         local_irq_restore(flags);
2786 }
2787 EXPORT_SYMBOL(__dev_kfree_skb_irq);
2788
2789 void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
2790 {
2791         if (in_irq() || irqs_disabled())
2792                 __dev_kfree_skb_irq(skb, reason);
2793         else
2794                 dev_kfree_skb(skb);
2795 }
2796 EXPORT_SYMBOL(__dev_kfree_skb_any);
2797
2798
2799 /**
2800  * netif_device_detach - mark device as removed
2801  * @dev: network device
2802  *
2803  * Mark device as removed from system and therefore no longer available.
2804  */
2805 void netif_device_detach(struct net_device *dev)
2806 {
2807         if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2808             netif_running(dev)) {
2809                 netif_tx_stop_all_queues(dev);
2810         }
2811 }
2812 EXPORT_SYMBOL(netif_device_detach);
2813
2814 /**
2815  * netif_device_attach - mark device as attached
2816  * @dev: network device
2817  *
2818  * Mark device as attached from system and restart if needed.
2819  */
2820 void netif_device_attach(struct net_device *dev)
2821 {
2822         if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2823             netif_running(dev)) {
2824                 netif_tx_wake_all_queues(dev);
2825                 __netdev_watchdog_up(dev);
2826         }
2827 }
2828 EXPORT_SYMBOL(netif_device_attach);
2829
2830 /*
2831  * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2832  * to be used as a distribution range.
2833  */
2834 static u16 skb_tx_hash(const struct net_device *dev,
2835                        const struct net_device *sb_dev,
2836                        struct sk_buff *skb)
2837 {
2838         u32 hash;
2839         u16 qoffset = 0;
2840         u16 qcount = dev->real_num_tx_queues;
2841
2842         if (dev->num_tc) {
2843                 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
2844
2845                 qoffset = sb_dev->tc_to_txq[tc].offset;
2846                 qcount = sb_dev->tc_to_txq[tc].count;
2847         }
2848
2849         if (skb_rx_queue_recorded(skb)) {
2850                 hash = skb_get_rx_queue(skb);
2851                 while (unlikely(hash >= qcount))
2852                         hash -= qcount;
2853                 return hash + qoffset;
2854         }
2855
2856         return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
2857 }
2858
2859 static void skb_warn_bad_offload(const struct sk_buff *skb)
2860 {
2861         static const netdev_features_t null_features;
2862         struct net_device *dev = skb->dev;
2863         const char *name = "";
2864
2865         if (!net_ratelimit())
2866                 return;
2867
2868         if (dev) {
2869                 if (dev->dev.parent)
2870                         name = dev_driver_string(dev->dev.parent);
2871                 else
2872                         name = netdev_name(dev);
2873         }
2874         WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2875              "gso_type=%d ip_summed=%d\n",
2876              name, dev ? &dev->features : &null_features,
2877              skb->sk ? &skb->sk->sk_route_caps : &null_features,
2878              skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2879              skb_shinfo(skb)->gso_type, skb->ip_summed);
2880 }
2881
2882 /*
2883  * Invalidate hardware checksum when packet is to be mangled, and
2884  * complete checksum manually on outgoing path.
2885  */
2886 int skb_checksum_help(struct sk_buff *skb)
2887 {
2888         __wsum csum;
2889         int ret = 0, offset;
2890
2891         if (skb->ip_summed == CHECKSUM_COMPLETE)
2892                 goto out_set_summed;
2893
2894         if (unlikely(skb_shinfo(skb)->gso_size)) {
2895                 skb_warn_bad_offload(skb);
2896                 return -EINVAL;
2897         }
2898
2899         /* Before computing a checksum, we should make sure no frag could
2900          * be modified by an external entity : checksum could be wrong.
2901          */
2902         if (skb_has_shared_frag(skb)) {
2903                 ret = __skb_linearize(skb);
2904                 if (ret)
2905                         goto out;
2906         }
2907
2908         offset = skb_checksum_start_offset(skb);
2909         BUG_ON(offset >= skb_headlen(skb));
2910         csum = skb_checksum(skb, offset, skb->len - offset, 0);
2911
2912         offset += skb->csum_offset;
2913         BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2914
2915         if (skb_cloned(skb) &&
2916             !skb_clone_writable(skb, offset + sizeof(__sum16))) {
2917                 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2918                 if (ret)
2919                         goto out;
2920         }
2921
2922         *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
2923 out_set_summed:
2924         skb->ip_summed = CHECKSUM_NONE;
2925 out:
2926         return ret;
2927 }
2928 EXPORT_SYMBOL(skb_checksum_help);
2929
2930 int skb_crc32c_csum_help(struct sk_buff *skb)
2931 {
2932         __le32 crc32c_csum;
2933         int ret = 0, offset, start;
2934
2935         if (skb->ip_summed != CHECKSUM_PARTIAL)
2936                 goto out;
2937
2938         if (unlikely(skb_is_gso(skb)))
2939                 goto out;
2940
2941         /* Before computing a checksum, we should make sure no frag could
2942          * be modified by an external entity : checksum could be wrong.
2943          */
2944         if (unlikely(skb_has_shared_frag(skb))) {
2945                 ret = __skb_linearize(skb);
2946                 if (ret)
2947                         goto out;
2948         }
2949         start = skb_checksum_start_offset(skb);
2950         offset = start + offsetof(struct sctphdr, checksum);
2951         if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
2952                 ret = -EINVAL;
2953                 goto out;
2954         }
2955         if (skb_cloned(skb) &&
2956             !skb_clone_writable(skb, offset + sizeof(__le32))) {
2957                 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2958                 if (ret)
2959                         goto out;
2960         }
2961         crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
2962                                                   skb->len - start, ~(__u32)0,
2963                                                   crc32c_csum_stub));
2964         *(__le32 *)(skb->data + offset) = crc32c_csum;
2965         skb->ip_summed = CHECKSUM_NONE;
2966         skb->csum_not_inet = 0;
2967 out:
2968         return ret;
2969 }
2970
2971 __be16 skb_network_protocol(struct sk_buff *skb, int *depth)
2972 {
2973         __be16 type = skb->protocol;
2974
2975         /* Tunnel gso handlers can set protocol to ethernet. */
2976         if (type == htons(ETH_P_TEB)) {
2977                 struct ethhdr *eth;
2978
2979                 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2980                         return 0;
2981
2982                 eth = (struct ethhdr *)skb->data;
2983                 type = eth->h_proto;
2984         }
2985
2986         return __vlan_get_protocol(skb, type, depth);
2987 }
2988
2989 /**
2990  *      skb_mac_gso_segment - mac layer segmentation handler.
2991  *      @skb: buffer to segment
2992  *      @features: features for the output path (see dev->features)
2993  */
2994 struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
2995                                     netdev_features_t features)
2996 {
2997         struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2998         struct packet_offload *ptype;
2999         int vlan_depth = skb->mac_len;
3000         __be16 type = skb_network_protocol(skb, &vlan_depth);
3001
3002         if (unlikely(!type))
3003                 return ERR_PTR(-EINVAL);
3004
3005         __skb_pull(skb, vlan_depth);
3006
3007         rcu_read_lock();
3008         list_for_each_entry_rcu(ptype, &offload_base, list) {
3009                 if (ptype->type == type && ptype->callbacks.gso_segment) {
3010                         segs = ptype->callbacks.gso_segment(skb, features);
3011                         break;
3012                 }
3013         }
3014         rcu_read_unlock();
3015
3016         __skb_push(skb, skb->data - skb_mac_header(skb));
3017
3018         return segs;
3019 }
3020 EXPORT_SYMBOL(skb_mac_gso_segment);
3021
3022
3023 /* openvswitch calls this on rx path, so we need a different check.
3024  */
3025 static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3026 {
3027         if (tx_path)
3028                 return skb->ip_summed != CHECKSUM_PARTIAL &&
3029                        skb->ip_summed != CHECKSUM_UNNECESSARY;
3030
3031         return skb->ip_summed == CHECKSUM_NONE;
3032 }
3033
3034 /**
3035  *      __skb_gso_segment - Perform segmentation on skb.
3036  *      @skb: buffer to segment
3037  *      @features: features for the output path (see dev->features)
3038  *      @tx_path: whether it is called in TX path
3039  *
3040  *      This function segments the given skb and returns a list of segments.
3041  *
3042  *      It may return NULL if the skb requires no segmentation.  This is
3043  *      only possible when GSO is used for verifying header integrity.
3044  *
3045  *      Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
3046  */
3047 struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3048                                   netdev_features_t features, bool tx_path)
3049 {
3050         struct sk_buff *segs;
3051
3052         if (unlikely(skb_needs_check(skb, tx_path))) {
3053                 int err;
3054
3055                 /* We're going to init ->check field in TCP or UDP header */
3056                 err = skb_cow_head(skb, 0);
3057                 if (err < 0)
3058                         return ERR_PTR(err);
3059         }
3060
3061         /* Only report GSO partial support if it will enable us to
3062          * support segmentation on this frame without needing additional
3063          * work.
3064          */
3065         if (features & NETIF_F_GSO_PARTIAL) {
3066                 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3067                 struct net_device *dev = skb->dev;
3068
3069                 partial_features |= dev->features & dev->gso_partial_features;
3070                 if (!skb_gso_ok(skb, features | partial_features))
3071                         features &= ~NETIF_F_GSO_PARTIAL;
3072         }
3073
3074         BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
3075                      sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3076
3077         SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
3078         SKB_GSO_CB(skb)->encap_level = 0;
3079
3080         skb_reset_mac_header(skb);
3081         skb_reset_mac_len(skb);
3082
3083         segs = skb_mac_gso_segment(skb, features);
3084
3085         if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
3086                 skb_warn_bad_offload(skb);
3087
3088         return segs;
3089 }
3090 EXPORT_SYMBOL(__skb_gso_segment);
3091
3092 /* Take action when hardware reception checksum errors are detected. */
3093 #ifdef CONFIG_BUG
3094 void netdev_rx_csum_fault(struct net_device *dev)
3095 {
3096         if (net_ratelimit()) {
3097                 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
3098                 dump_stack();
3099         }
3100 }
3101 EXPORT_SYMBOL(netdev_rx_csum_fault);
3102 #endif
3103
3104 /* XXX: check that highmem exists at all on the given machine. */
3105 static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
3106 {
3107 #ifdef CONFIG_HIGHMEM
3108         int i;
3109
3110         if (!(dev->features & NETIF_F_HIGHDMA)) {
3111                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3112                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3113
3114                         if (PageHighMem(skb_frag_page(frag)))
3115                                 return 1;
3116                 }
3117         }
3118 #endif
3119         return 0;
3120 }
3121
3122 /* If MPLS offload request, verify we are testing hardware MPLS features
3123  * instead of standard features for the netdev.
3124  */
3125 #if IS_ENABLED(CONFIG_NET_MPLS_GSO)
3126 static netdev_features_t net_mpls_features(struct sk_buff *skb,
3127                                            netdev_features_t features,
3128                                            __be16 type)
3129 {
3130         if (eth_p_mpls(type))
3131                 features &= skb->dev->mpls_features;
3132
3133         return features;
3134 }
3135 #else
3136 static netdev_features_t net_mpls_features(struct sk_buff *skb,
3137                                            netdev_features_t features,
3138                                            __be16 type)
3139 {
3140         return features;
3141 }
3142 #endif
3143
3144 static netdev_features_t harmonize_features(struct sk_buff *skb,
3145         netdev_features_t features)
3146 {
3147         int tmp;
3148         __be16 type;
3149
3150         type = skb_network_protocol(skb, &tmp);
3151         features = net_mpls_features(skb, features, type);
3152
3153         if (skb->ip_summed != CHECKSUM_NONE &&
3154             !can_checksum_protocol(features, type)) {
3155                 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3156         }
3157         if (illegal_highdma(skb->dev, skb))
3158                 features &= ~NETIF_F_SG;
3159
3160         return features;
3161 }
3162
3163 netdev_features_t passthru_features_check(struct sk_buff *skb,
3164                                           struct net_device *dev,
3165                                           netdev_features_t features)
3166 {
3167         return features;
3168 }
3169 EXPORT_SYMBOL(passthru_features_check);
3170
3171 static netdev_features_t dflt_features_check(struct sk_buff *skb,
3172                                              struct net_device *dev,
3173                                              netdev_features_t features)
3174 {
3175         return vlan_features_check(skb, features);
3176 }
3177
3178 static netdev_features_t gso_features_check(const struct sk_buff *skb,
3179                                             struct net_device *dev,
3180                                             netdev_features_t features)
3181 {
3182         u16 gso_segs = skb_shinfo(skb)->gso_segs;
3183
3184         if (gso_segs > dev->gso_max_segs)
3185                 return features & ~NETIF_F_GSO_MASK;
3186
3187         /* Support for GSO partial features requires software
3188          * intervention before we can actually process the packets
3189          * so we need to strip support for any partial features now
3190          * and we can pull them back in after we have partially
3191          * segmented the frame.
3192          */
3193         if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3194                 features &= ~dev->gso_partial_features;
3195
3196         /* Make sure to clear the IPv4 ID mangling feature if the
3197          * IPv4 header has the potential to be fragmented.
3198          */
3199         if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3200                 struct iphdr *iph = skb->encapsulation ?
3201                                     inner_ip_hdr(skb) : ip_hdr(skb);
3202
3203                 if (!(iph->frag_off & htons(IP_DF)))
3204                         features &= ~NETIF_F_TSO_MANGLEID;
3205         }
3206
3207         return features;
3208 }
3209
3210 netdev_features_t netif_skb_features(struct sk_buff *skb)
3211 {
3212         struct net_device *dev = skb->dev;
3213         netdev_features_t features = dev->features;
3214
3215         if (skb_is_gso(skb))
3216                 features = gso_features_check(skb, dev, features);
3217
3218         /* If encapsulation offload request, verify we are testing
3219          * hardware encapsulation features instead of standard
3220          * features for the netdev
3221          */
3222         if (skb->encapsulation)
3223                 features &= dev->hw_enc_features;
3224
3225         if (skb_vlan_tagged(skb))
3226                 features = netdev_intersect_features(features,
3227                                                      dev->vlan_features |
3228                                                      NETIF_F_HW_VLAN_CTAG_TX |
3229                                                      NETIF_F_HW_VLAN_STAG_TX);
3230
3231         if (dev->netdev_ops->ndo_features_check)
3232                 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3233                                                                 features);
3234         else
3235                 features &= dflt_features_check(skb, dev, features);
3236
3237         return harmonize_features(skb, features);
3238 }
3239 EXPORT_SYMBOL(netif_skb_features);
3240
3241 static int xmit_one(struct sk_buff *skb, struct net_device *dev,
3242                     struct netdev_queue *txq, bool more)
3243 {
3244         unsigned int len;
3245         int rc;
3246
3247         if (dev_nit_active(dev))
3248                 dev_queue_xmit_nit(skb, dev);
3249
3250         len = skb->len;
3251         trace_net_dev_start_xmit(skb, dev);
3252         rc = netdev_start_xmit(skb, dev, txq, more);
3253         trace_net_dev_xmit(skb, rc, dev, len);
3254
3255         return rc;
3256 }
3257
3258 struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3259                                     struct netdev_queue *txq, int *ret)
3260 {
3261         struct sk_buff *skb = first;
3262         int rc = NETDEV_TX_OK;
3263
3264         while (skb) {
3265                 struct sk_buff *next = skb->next;
3266
3267                 skb_mark_not_on_list(skb);
3268                 rc = xmit_one(skb, dev, txq, next != NULL);
3269                 if (unlikely(!dev_xmit_complete(rc))) {
3270                         skb->next = next;
3271                         goto out;
3272                 }
3273
3274                 skb = next;
3275                 if (netif_xmit_stopped(txq) && skb) {
3276                         rc = NETDEV_TX_BUSY;
3277                         break;
3278                 }
3279         }
3280
3281 out:
3282         *ret = rc;
3283         return skb;
3284 }
3285
3286 static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3287                                           netdev_features_t features)
3288 {
3289         if (skb_vlan_tag_present(skb) &&
3290             !vlan_hw_offload_capable(features, skb->vlan_proto))
3291                 skb = __vlan_hwaccel_push_inside(skb);
3292         return skb;
3293 }
3294
3295 int skb_csum_hwoffload_help(struct sk_buff *skb,
3296                             const netdev_features_t features)
3297 {
3298         if (unlikely(skb->csum_not_inet))
3299                 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3300                         skb_crc32c_csum_help(skb);
3301
3302         return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3303 }
3304 EXPORT_SYMBOL(skb_csum_hwoffload_help);
3305
3306 static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
3307 {
3308         netdev_features_t features;
3309
3310         features = netif_skb_features(skb);
3311         skb = validate_xmit_vlan(skb, features);
3312         if (unlikely(!skb))
3313                 goto out_null;
3314
3315         skb = sk_validate_xmit_skb(skb, dev);
3316         if (unlikely(!skb))
3317                 goto out_null;
3318
3319         if (netif_needs_gso(skb, features)) {
3320                 struct sk_buff *segs;
3321
3322                 segs = skb_gso_segment(skb, features);
3323                 if (IS_ERR(segs)) {
3324                         goto out_kfree_skb;
3325                 } else if (segs) {
3326                         consume_skb(skb);
3327                         skb = segs;
3328                 }
3329         } else {
3330                 if (skb_needs_linearize(skb, features) &&
3331                     __skb_linearize(skb))
3332                         goto out_kfree_skb;
3333
3334                 /* If packet is not checksummed and device does not
3335                  * support checksumming for this protocol, complete
3336                  * checksumming here.
3337                  */
3338                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3339                         if (skb->encapsulation)
3340                                 skb_set_inner_transport_header(skb,
3341                                                                skb_checksum_start_offset(skb));
3342                         else
3343                                 skb_set_transport_header(skb,
3344                                                          skb_checksum_start_offset(skb));
3345                         if (skb_csum_hwoffload_help(skb, features))
3346                                 goto out_kfree_skb;
3347                 }
3348         }
3349
3350         skb = validate_xmit_xfrm(skb, features, again);
3351
3352         return skb;
3353
3354 out_kfree_skb:
3355         kfree_skb(skb);
3356 out_null:
3357         atomic_long_inc(&dev->tx_dropped);
3358         return NULL;
3359 }
3360
3361 struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
3362 {
3363         struct sk_buff *next, *head = NULL, *tail;
3364
3365         for (; skb != NULL; skb = next) {
3366                 next = skb->next;
3367                 skb_mark_not_on_list(skb);
3368
3369                 /* in case skb wont be segmented, point to itself */
3370                 skb->prev = skb;
3371
3372                 skb = validate_xmit_skb(skb, dev, again);
3373                 if (!skb)
3374                         continue;
3375
3376                 if (!head)
3377                         head = skb;
3378                 else
3379                         tail->next = skb;
3380                 /* If skb was segmented, skb->prev points to
3381                  * the last segment. If not, it still contains skb.
3382                  */
3383                 tail = skb->prev;
3384         }
3385         return head;
3386 }
3387 EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
3388
3389 static void qdisc_pkt_len_init(struct sk_buff *skb)
3390 {
3391         const struct skb_shared_info *shinfo = skb_shinfo(skb);
3392
3393         qdisc_skb_cb(skb)->pkt_len = skb->len;
3394
3395         /* To get more precise estimation of bytes sent on wire,
3396          * we add to pkt_len the headers size of all segments
3397          */
3398         if (shinfo->gso_size)  {
3399                 unsigned int hdr_len;
3400                 u16 gso_segs = shinfo->gso_segs;
3401
3402                 /* mac layer + network layer */
3403                 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3404
3405                 /* + transport layer */
3406                 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3407                         const struct tcphdr *th;
3408                         struct tcphdr _tcphdr;
3409
3410                         th = skb_header_pointer(skb, skb_transport_offset(skb),
3411                                                 sizeof(_tcphdr), &_tcphdr);
3412                         if (likely(th))
3413                                 hdr_len += __tcp_hdrlen(th);
3414                 } else {
3415                         struct udphdr _udphdr;
3416
3417                         if (skb_header_pointer(skb, skb_transport_offset(skb),
3418                                                sizeof(_udphdr), &_udphdr))
3419                                 hdr_len += sizeof(struct udphdr);
3420                 }
3421
3422                 if (shinfo->gso_type & SKB_GSO_DODGY)
3423                         gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3424                                                 shinfo->gso_size);
3425
3426                 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
3427         }
3428 }
3429
3430 static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3431                                  struct net_device *dev,
3432                                  struct netdev_queue *txq)
3433 {
3434         spinlock_t *root_lock = qdisc_lock(q);
3435         struct sk_buff *to_free = NULL;
3436         bool contended;
3437         int rc;
3438
3439         qdisc_calculate_pkt_len(skb, q);
3440
3441         if (q->flags & TCQ_F_NOLOCK) {
3442                 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
3443                         __qdisc_drop(skb, &to_free);
3444                         rc = NET_XMIT_DROP;
3445                 } else {
3446                         rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
3447                         qdisc_run(q);
3448                 }
3449
3450                 if (unlikely(to_free))
3451                         kfree_skb_list(to_free);
3452                 return rc;
3453         }
3454
3455         /*
3456          * Heuristic to force contended enqueues to serialize on a
3457          * separate lock before trying to get qdisc main lock.
3458          * This permits qdisc->running owner to get the lock more
3459          * often and dequeue packets faster.
3460          */
3461         contended = qdisc_is_running(q);
3462         if (unlikely(contended))
3463                 spin_lock(&q->busylock);
3464
3465         spin_lock(root_lock);
3466         if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
3467                 __qdisc_drop(skb, &to_free);
3468                 rc = NET_XMIT_DROP;
3469         } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
3470                    qdisc_run_begin(q)) {
3471                 /*
3472                  * This is a work-conserving queue; there are no old skbs
3473                  * waiting to be sent out; and the qdisc is not running -
3474                  * xmit the skb directly.
3475                  */
3476
3477                 qdisc_bstats_update(q, skb);
3478
3479                 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
3480                         if (unlikely(contended)) {
3481                                 spin_unlock(&q->busylock);
3482                                 contended = false;
3483                         }
3484                         __qdisc_run(q);
3485                 }
3486
3487                 qdisc_run_end(q);
3488                 rc = NET_XMIT_SUCCESS;
3489         } else {
3490                 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
3491                 if (qdisc_run_begin(q)) {
3492                         if (unlikely(contended)) {
3493                                 spin_unlock(&q->busylock);
3494                                 contended = false;
3495                         }
3496                         __qdisc_run(q);
3497                         qdisc_run_end(q);
3498                 }
3499         }
3500         spin_unlock(root_lock);
3501         if (unlikely(to_free))
3502                 kfree_skb_list(to_free);
3503         if (unlikely(contended))
3504                 spin_unlock(&q->busylock);
3505         return rc;
3506 }
3507
3508 #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
3509 static void skb_update_prio(struct sk_buff *skb)
3510 {
3511         const struct netprio_map *map;
3512         const struct sock *sk;
3513         unsigned int prioidx;
3514
3515         if (skb->priority)
3516                 return;
3517         map = rcu_dereference_bh(skb->dev->priomap);
3518         if (!map)
3519                 return;
3520         sk = skb_to_full_sk(skb);
3521         if (!sk)
3522                 return;
3523
3524         prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3525
3526         if (prioidx < map->priomap_len)
3527                 skb->priority = map->priomap[prioidx];
3528 }
3529 #else
3530 #define skb_update_prio(skb)
3531 #endif
3532
3533 DEFINE_PER_CPU(int, xmit_recursion);
3534 EXPORT_SYMBOL(xmit_recursion);
3535
3536 /**
3537  *      dev_loopback_xmit - loop back @skb
3538  *      @net: network namespace this loopback is happening in
3539  *      @sk:  sk needed to be a netfilter okfn
3540  *      @skb: buffer to transmit
3541  */
3542 int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
3543 {
3544         skb_reset_mac_header(skb);
3545         __skb_pull(skb, skb_network_offset(skb));
3546         skb->pkt_type = PACKET_LOOPBACK;
3547         skb->ip_summed = CHECKSUM_UNNECESSARY;
3548         WARN_ON(!skb_dst(skb));
3549         skb_dst_force(skb);
3550         netif_rx_ni(skb);
3551         return 0;
3552 }
3553 EXPORT_SYMBOL(dev_loopback_xmit);
3554
3555 #ifdef CONFIG_NET_EGRESS
3556 static struct sk_buff *
3557 sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3558 {
3559         struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
3560         struct tcf_result cl_res;
3561
3562         if (!miniq)
3563                 return skb;
3564
3565         /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
3566         mini_qdisc_bstats_cpu_update(miniq, skb);
3567
3568         switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
3569         case TC_ACT_OK:
3570         case TC_ACT_RECLASSIFY:
3571                 skb->tc_index = TC_H_MIN(cl_res.classid);
3572                 break;
3573         case TC_ACT_SHOT:
3574                 mini_qdisc_qstats_cpu_drop(miniq);
3575                 *ret = NET_XMIT_DROP;
3576                 kfree_skb(skb);
3577                 return NULL;
3578         case TC_ACT_STOLEN:
3579         case TC_ACT_QUEUED:
3580         case TC_ACT_TRAP:
3581                 *ret = NET_XMIT_SUCCESS;
3582                 consume_skb(skb);
3583                 return NULL;
3584         case TC_ACT_REDIRECT:
3585                 /* No need to push/pop skb's mac_header here on egress! */
3586                 skb_do_redirect(skb);
3587                 *ret = NET_XMIT_SUCCESS;
3588                 return NULL;
3589         default:
3590                 break;
3591         }
3592
3593         return skb;
3594 }
3595 #endif /* CONFIG_NET_EGRESS */
3596
3597 #ifdef CONFIG_XPS
3598 static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3599                                struct xps_dev_maps *dev_maps, unsigned int tci)
3600 {
3601         struct xps_map *map;
3602         int queue_index = -1;
3603
3604         if (dev->num_tc) {
3605                 tci *= dev->num_tc;
3606                 tci += netdev_get_prio_tc_map(dev, skb->priority);
3607         }
3608
3609         map = rcu_dereference(dev_maps->attr_map[tci]);
3610         if (map) {
3611                 if (map->len == 1)
3612                         queue_index = map->queues[0];
3613                 else
3614                         queue_index = map->queues[reciprocal_scale(
3615                                                 skb_get_hash(skb), map->len)];
3616                 if (unlikely(queue_index >= dev->real_num_tx_queues))
3617                         queue_index = -1;
3618         }
3619         return queue_index;
3620 }
3621 #endif
3622
3623 static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3624                          struct sk_buff *skb)
3625 {
3626 #ifdef CONFIG_XPS
3627         struct xps_dev_maps *dev_maps;
3628         struct sock *sk = skb->sk;
3629         int queue_index = -1;