xref: /linux/drivers/net/bonding/bond_main.c (revision e3966940559d52aa1800a008dcfeec218dd31f88)
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * originally based on the dummy device.
4  *
5  * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
6  * Based on dummy.c, and eql.c devices.
7  *
8  * bonding.c: an Ethernet Bonding driver
9  *
10  * This is useful to talk to a Cisco EtherChannel compatible equipment:
11  *	Cisco 5500
12  *	Sun Trunking (Solaris)
13  *	Alteon AceDirector Trunks
14  *	Linux Bonding
15  *	and probably many L2 switches ...
16  *
17  * How it works:
18  *    ifconfig bond0 ipaddress netmask up
19  *      will setup a network device, with an ip address.  No mac address
20  *	will be assigned at this time.  The hw mac address will come from
21  *	the first slave bonded to the channel.  All slaves will then use
22  *	this hw mac address.
23  *
24  *    ifconfig bond0 down
25  *         will release all slaves, marking them as down.
26  *
27  *    ifenslave bond0 eth0
28  *	will attach eth0 to bond0 as a slave.  eth0 hw mac address will either
29  *	a: be used as initial mac address
30  *	b: if a hw mac address already is there, eth0's hw mac address
31  *	   will then be set from bond0.
32  *
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/fcntl.h>
39 #include <linux/filter.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.h>
43 #include <linux/in.h>
44 #include <net/ip.h>
45 #include <linux/ip.h>
46 #include <linux/icmp.h>
47 #include <linux/icmpv6.h>
48 #include <linux/tcp.h>
49 #include <linux/udp.h>
50 #include <linux/slab.h>
51 #include <linux/string.h>
52 #include <linux/init.h>
53 #include <linux/timer.h>
54 #include <linux/socket.h>
55 #include <linux/ctype.h>
56 #include <linux/inet.h>
57 #include <linux/bitops.h>
58 #include <linux/io.h>
59 #include <asm/dma.h>
60 #include <linux/uaccess.h>
61 #include <linux/errno.h>
62 #include <linux/netdevice.h>
63 #include <linux/inetdevice.h>
64 #include <linux/igmp.h>
65 #include <linux/etherdevice.h>
66 #include <linux/skbuff.h>
67 #include <net/sock.h>
68 #include <linux/rtnetlink.h>
69 #include <linux/smp.h>
70 #include <linux/if_ether.h>
71 #include <net/arp.h>
72 #include <linux/mii.h>
73 #include <linux/ethtool.h>
74 #include <linux/if_vlan.h>
75 #include <linux/if_bonding.h>
76 #include <linux/phy.h>
77 #include <linux/jiffies.h>
78 #include <linux/preempt.h>
79 #include <net/route.h>
80 #include <net/net_namespace.h>
81 #include <net/netns/generic.h>
82 #include <net/pkt_sched.h>
83 #include <linux/rculist.h>
84 #include <net/flow_dissector.h>
85 #include <net/xfrm.h>
86 #include <net/bonding.h>
87 #include <net/bond_3ad.h>
88 #include <net/bond_alb.h>
89 #if IS_ENABLED(CONFIG_TLS_DEVICE)
90 #include <net/tls.h>
91 #endif
92 #include <net/ip6_route.h>
93 #include <net/netdev_lock.h>
94 #include <net/xdp.h>
95 
96 #include "bonding_priv.h"
97 
98 /*---------------------------- Module parameters ----------------------------*/
99 
100 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
101 
102 static int max_bonds	= BOND_DEFAULT_MAX_BONDS;
103 static int tx_queues	= BOND_DEFAULT_TX_QUEUES;
104 static int num_peer_notif = 1;
105 static int miimon;
106 static int updelay;
107 static int downdelay;
108 static int use_carrier	= 1;
109 static char *mode;
110 static char *primary;
111 static char *primary_reselect;
112 static char *lacp_rate;
113 static int min_links;
114 static char *ad_select;
115 static char *xmit_hash_policy;
116 static int arp_interval;
117 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
118 static char *arp_validate;
119 static char *arp_all_targets;
120 static char *fail_over_mac;
121 static int all_slaves_active;
122 static struct bond_params bonding_defaults;
123 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
124 static int packets_per_slave = 1;
125 static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
126 
127 module_param(max_bonds, int, 0);
128 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
129 module_param(tx_queues, int, 0);
130 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
131 module_param_named(num_grat_arp, num_peer_notif, int, 0644);
132 MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
133 			       "failover event (alias of num_unsol_na)");
134 module_param_named(num_unsol_na, num_peer_notif, int, 0644);
135 MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
136 			       "failover event (alias of num_grat_arp)");
137 module_param(miimon, int, 0);
138 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
139 module_param(updelay, int, 0);
140 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
141 module_param(downdelay, int, 0);
142 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
143 			    "in milliseconds");
144 module_param(use_carrier, int, 0);
145 MODULE_PARM_DESC(use_carrier, "option obsolete, use_carrier cannot be disabled");
146 module_param(mode, charp, 0);
147 MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
148 		       "1 for active-backup, 2 for balance-xor, "
149 		       "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
150 		       "6 for balance-alb");
151 module_param(primary, charp, 0);
152 MODULE_PARM_DESC(primary, "Primary network device to use");
153 module_param(primary_reselect, charp, 0);
154 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
155 				   "once it comes up; "
156 				   "0 for always (default), "
157 				   "1 for only if speed of primary is "
158 				   "better, "
159 				   "2 for only on active slave "
160 				   "failure");
161 module_param(lacp_rate, charp, 0);
162 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
163 			    "0 for slow, 1 for fast");
164 module_param(ad_select, charp, 0);
165 MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; "
166 			    "0 for stable (default), 1 for bandwidth, "
167 			    "2 for count");
168 module_param(min_links, int, 0);
169 MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
170 
171 module_param(xmit_hash_policy, charp, 0);
172 MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
173 				   "0 for layer 2 (default), 1 for layer 3+4, "
174 				   "2 for layer 2+3, 3 for encap layer 2+3, "
175 				   "4 for encap layer 3+4, 5 for vlan+srcmac");
176 module_param(arp_interval, int, 0);
177 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
178 module_param_array(arp_ip_target, charp, NULL, 0);
179 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
180 module_param(arp_validate, charp, 0);
181 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
182 			       "0 for none (default), 1 for active, "
183 			       "2 for backup, 3 for all");
184 module_param(arp_all_targets, charp, 0);
185 MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all");
186 module_param(fail_over_mac, charp, 0);
187 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
188 				"the same MAC; 0 for none (default), "
189 				"1 for active, 2 for follow");
190 module_param(all_slaves_active, int, 0);
191 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface "
192 				     "by setting active flag for all slaves; "
193 				     "0 for never (default), 1 for always.");
194 module_param(resend_igmp, int, 0);
195 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
196 			      "link failure");
197 module_param(packets_per_slave, int, 0);
198 MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
199 				    "mode; 0 for a random slave, 1 packet per "
200 				    "slave (default), >1 packets per slave.");
201 module_param(lp_interval, uint, 0);
202 MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where "
203 			      "the bonding driver sends learning packets to "
204 			      "each slaves peer switch. The default is 1.");
205 
206 /*----------------------------- Global variables ----------------------------*/
207 
208 #ifdef CONFIG_NET_POLL_CONTROLLER
209 atomic_t netpoll_block_tx = ATOMIC_INIT(0);
210 #endif
211 
212 unsigned int bond_net_id __read_mostly;
213 
214 DEFINE_STATIC_KEY_FALSE(bond_bcast_neigh_enabled);
215 
216 static const struct flow_dissector_key flow_keys_bonding_keys[] = {
217 	{
218 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
219 		.offset = offsetof(struct flow_keys, control),
220 	},
221 	{
222 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
223 		.offset = offsetof(struct flow_keys, basic),
224 	},
225 	{
226 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
227 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
228 	},
229 	{
230 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
231 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
232 	},
233 	{
234 		.key_id = FLOW_DISSECTOR_KEY_TIPC,
235 		.offset = offsetof(struct flow_keys, addrs.tipckey),
236 	},
237 	{
238 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
239 		.offset = offsetof(struct flow_keys, ports),
240 	},
241 	{
242 		.key_id = FLOW_DISSECTOR_KEY_ICMP,
243 		.offset = offsetof(struct flow_keys, icmp),
244 	},
245 	{
246 		.key_id = FLOW_DISSECTOR_KEY_VLAN,
247 		.offset = offsetof(struct flow_keys, vlan),
248 	},
249 	{
250 		.key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
251 		.offset = offsetof(struct flow_keys, tags),
252 	},
253 	{
254 		.key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
255 		.offset = offsetof(struct flow_keys, keyid),
256 	},
257 };
258 
259 static struct flow_dissector flow_keys_bonding __read_mostly;
260 
261 /*-------------------------- Forward declarations ---------------------------*/
262 
263 static int bond_init(struct net_device *bond_dev);
264 static void bond_uninit(struct net_device *bond_dev);
265 static void bond_get_stats(struct net_device *bond_dev,
266 			   struct rtnl_link_stats64 *stats);
267 static void bond_slave_arr_handler(struct work_struct *work);
268 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
269 				  int mod);
270 static void bond_netdev_notify_work(struct work_struct *work);
271 
272 /*---------------------------- General routines -----------------------------*/
273 
274 const char *bond_mode_name(int mode)
275 {
276 	static const char *names[] = {
277 		[BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
278 		[BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
279 		[BOND_MODE_XOR] = "load balancing (xor)",
280 		[BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
281 		[BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
282 		[BOND_MODE_TLB] = "transmit load balancing",
283 		[BOND_MODE_ALB] = "adaptive load balancing",
284 	};
285 
286 	if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
287 		return "unknown";
288 
289 	return names[mode];
290 }
291 
292 /**
293  * bond_dev_queue_xmit - Prepare skb for xmit.
294  *
295  * @bond: bond device that got this skb for tx.
296  * @skb: hw accel VLAN tagged skb to transmit
297  * @slave_dev: slave that is supposed to xmit this skbuff
298  */
299 netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
300 			struct net_device *slave_dev)
301 {
302 	skb->dev = slave_dev;
303 
304 	BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
305 		     sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
306 	skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
307 
308 	if (unlikely(netpoll_tx_running(bond->dev)))
309 		return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
310 
311 	return dev_queue_xmit(skb);
312 }
313 
314 static bool bond_sk_check(struct bonding *bond)
315 {
316 	switch (BOND_MODE(bond)) {
317 	case BOND_MODE_8023AD:
318 	case BOND_MODE_XOR:
319 		if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
320 			return true;
321 		fallthrough;
322 	default:
323 		return false;
324 	}
325 }
326 
327 bool bond_xdp_check(struct bonding *bond, int mode)
328 {
329 	switch (mode) {
330 	case BOND_MODE_ROUNDROBIN:
331 	case BOND_MODE_ACTIVEBACKUP:
332 		return true;
333 	case BOND_MODE_8023AD:
334 	case BOND_MODE_XOR:
335 		/* vlan+srcmac is not supported with XDP as in most cases the 802.1q
336 		 * payload is not in the packet due to hardware offload.
337 		 */
338 		if (bond->params.xmit_policy != BOND_XMIT_POLICY_VLAN_SRCMAC)
339 			return true;
340 		fallthrough;
341 	default:
342 		return false;
343 	}
344 }
345 
346 /*---------------------------------- VLAN -----------------------------------*/
347 
348 /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
349  * We don't protect the slave list iteration with a lock because:
350  * a. This operation is performed in IOCTL context,
351  * b. The operation is protected by the RTNL semaphore in the 8021q code,
352  * c. Holding a lock with BH disabled while directly calling a base driver
353  *    entry point is generally a BAD idea.
354  *
355  * The design of synchronization/protection for this operation in the 8021q
356  * module is good for one or more VLAN devices over a single physical device
357  * and cannot be extended for a teaming solution like bonding, so there is a
358  * potential race condition here where a net device from the vlan group might
359  * be referenced (either by a base driver or the 8021q code) while it is being
360  * removed from the system. However, it turns out we're not making matters
361  * worse, and if it works for regular VLAN usage it will work here too.
362 */
363 
364 /**
365  * bond_vlan_rx_add_vid - Propagates adding an id to slaves
366  * @bond_dev: bonding net device that got called
367  * @proto: network protocol ID
368  * @vid: vlan id being added
369  */
370 static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
371 				__be16 proto, u16 vid)
372 {
373 	struct bonding *bond = netdev_priv(bond_dev);
374 	struct slave *slave, *rollback_slave;
375 	struct list_head *iter;
376 	int res;
377 
378 	bond_for_each_slave(bond, slave, iter) {
379 		res = vlan_vid_add(slave->dev, proto, vid);
380 		if (res)
381 			goto unwind;
382 	}
383 
384 	return 0;
385 
386 unwind:
387 	/* unwind to the slave that failed */
388 	bond_for_each_slave(bond, rollback_slave, iter) {
389 		if (rollback_slave == slave)
390 			break;
391 
392 		vlan_vid_del(rollback_slave->dev, proto, vid);
393 	}
394 
395 	return res;
396 }
397 
398 /**
399  * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
400  * @bond_dev: bonding net device that got called
401  * @proto: network protocol ID
402  * @vid: vlan id being removed
403  */
404 static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
405 				 __be16 proto, u16 vid)
406 {
407 	struct bonding *bond = netdev_priv(bond_dev);
408 	struct list_head *iter;
409 	struct slave *slave;
410 
411 	bond_for_each_slave(bond, slave, iter)
412 		vlan_vid_del(slave->dev, proto, vid);
413 
414 	if (bond_is_lb(bond))
415 		bond_alb_clear_vlan(bond, vid);
416 
417 	return 0;
418 }
419 
420 /*---------------------------------- XFRM -----------------------------------*/
421 
422 #ifdef CONFIG_XFRM_OFFLOAD
423 /**
424  * bond_ipsec_dev - Get active device for IPsec offload
425  * @xs: pointer to transformer state struct
426  *
427  * Context: caller must hold rcu_read_lock.
428  *
429  * Return: the device for ipsec offload, or NULL if not exist.
430  **/
431 static struct net_device *bond_ipsec_dev(struct xfrm_state *xs)
432 {
433 	struct net_device *bond_dev = xs->xso.dev;
434 	struct bonding *bond;
435 	struct slave *slave;
436 
437 	bond = netdev_priv(bond_dev);
438 	if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
439 		return NULL;
440 
441 	slave = rcu_dereference(bond->curr_active_slave);
442 	if (!slave)
443 		return NULL;
444 
445 	if (!xs->xso.real_dev)
446 		return NULL;
447 
448 	if (xs->xso.real_dev != slave->dev)
449 		pr_warn_ratelimited("%s: (slave %s): not same with IPsec offload real dev %s\n",
450 				    bond_dev->name, slave->dev->name, xs->xso.real_dev->name);
451 
452 	return slave->dev;
453 }
454 
455 /**
456  * bond_ipsec_add_sa - program device with a security association
457  * @bond_dev: pointer to the bond net device
458  * @xs: pointer to transformer state struct
459  * @extack: extack point to fill failure reason
460  **/
461 static int bond_ipsec_add_sa(struct net_device *bond_dev,
462 			     struct xfrm_state *xs,
463 			     struct netlink_ext_ack *extack)
464 {
465 	struct net_device *real_dev;
466 	netdevice_tracker tracker;
467 	struct bond_ipsec *ipsec;
468 	struct bonding *bond;
469 	struct slave *slave;
470 	int err;
471 
472 	if (!bond_dev)
473 		return -EINVAL;
474 
475 	rcu_read_lock();
476 	bond = netdev_priv(bond_dev);
477 	slave = rcu_dereference(bond->curr_active_slave);
478 	real_dev = slave ? slave->dev : NULL;
479 	netdev_hold(real_dev, &tracker, GFP_ATOMIC);
480 	rcu_read_unlock();
481 	if (!real_dev) {
482 		err = -ENODEV;
483 		goto out;
484 	}
485 
486 	if (!real_dev->xfrmdev_ops ||
487 	    !real_dev->xfrmdev_ops->xdo_dev_state_add ||
488 	    netif_is_bond_master(real_dev)) {
489 		NL_SET_ERR_MSG_MOD(extack, "Slave does not support ipsec offload");
490 		err = -EINVAL;
491 		goto out;
492 	}
493 
494 	ipsec = kmalloc(sizeof(*ipsec), GFP_KERNEL);
495 	if (!ipsec) {
496 		err = -ENOMEM;
497 		goto out;
498 	}
499 
500 	err = real_dev->xfrmdev_ops->xdo_dev_state_add(real_dev, xs, extack);
501 	if (!err) {
502 		xs->xso.real_dev = real_dev;
503 		ipsec->xs = xs;
504 		INIT_LIST_HEAD(&ipsec->list);
505 		mutex_lock(&bond->ipsec_lock);
506 		list_add(&ipsec->list, &bond->ipsec_list);
507 		mutex_unlock(&bond->ipsec_lock);
508 	} else {
509 		kfree(ipsec);
510 	}
511 out:
512 	netdev_put(real_dev, &tracker);
513 	return err;
514 }
515 
516 static void bond_ipsec_add_sa_all(struct bonding *bond)
517 {
518 	struct net_device *bond_dev = bond->dev;
519 	struct net_device *real_dev;
520 	struct bond_ipsec *ipsec;
521 	struct slave *slave;
522 
523 	slave = rtnl_dereference(bond->curr_active_slave);
524 	real_dev = slave ? slave->dev : NULL;
525 	if (!real_dev)
526 		return;
527 
528 	mutex_lock(&bond->ipsec_lock);
529 	if (!real_dev->xfrmdev_ops ||
530 	    !real_dev->xfrmdev_ops->xdo_dev_state_add ||
531 	    netif_is_bond_master(real_dev)) {
532 		if (!list_empty(&bond->ipsec_list))
533 			slave_warn(bond_dev, real_dev,
534 				   "%s: no slave xdo_dev_state_add\n",
535 				   __func__);
536 		goto out;
537 	}
538 
539 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
540 		/* If new state is added before ipsec_lock acquired */
541 		if (ipsec->xs->xso.real_dev == real_dev)
542 			continue;
543 
544 		if (real_dev->xfrmdev_ops->xdo_dev_state_add(real_dev,
545 							     ipsec->xs, NULL)) {
546 			slave_warn(bond_dev, real_dev, "%s: failed to add SA\n", __func__);
547 			continue;
548 		}
549 
550 		spin_lock_bh(&ipsec->xs->lock);
551 		/* xs might have been killed by the user during the migration
552 		 * to the new dev, but bond_ipsec_del_sa() should have done
553 		 * nothing, as xso.real_dev is NULL.
554 		 * Delete it from the device we just added it to. The pending
555 		 * bond_ipsec_free_sa() call will do the rest of the cleanup.
556 		 */
557 		if (ipsec->xs->km.state == XFRM_STATE_DEAD &&
558 		    real_dev->xfrmdev_ops->xdo_dev_state_delete)
559 			real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,
560 								    ipsec->xs);
561 		ipsec->xs->xso.real_dev = real_dev;
562 		spin_unlock_bh(&ipsec->xs->lock);
563 	}
564 out:
565 	mutex_unlock(&bond->ipsec_lock);
566 }
567 
568 /**
569  * bond_ipsec_del_sa - clear out this specific SA
570  * @bond_dev: pointer to the bond net device
571  * @xs: pointer to transformer state struct
572  **/
573 static void bond_ipsec_del_sa(struct net_device *bond_dev,
574 			      struct xfrm_state *xs)
575 {
576 	struct net_device *real_dev;
577 
578 	if (!bond_dev || !xs->xso.real_dev)
579 		return;
580 
581 	real_dev = xs->xso.real_dev;
582 
583 	if (!real_dev->xfrmdev_ops ||
584 	    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
585 	    netif_is_bond_master(real_dev)) {
586 		slave_warn(bond_dev, real_dev, "%s: no slave xdo_dev_state_delete\n", __func__);
587 		return;
588 	}
589 
590 	real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev, xs);
591 }
592 
593 static void bond_ipsec_del_sa_all(struct bonding *bond)
594 {
595 	struct net_device *bond_dev = bond->dev;
596 	struct net_device *real_dev;
597 	struct bond_ipsec *ipsec;
598 	struct slave *slave;
599 
600 	slave = rtnl_dereference(bond->curr_active_slave);
601 	real_dev = slave ? slave->dev : NULL;
602 	if (!real_dev)
603 		return;
604 
605 	mutex_lock(&bond->ipsec_lock);
606 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
607 		if (!ipsec->xs->xso.real_dev)
608 			continue;
609 
610 		if (!real_dev->xfrmdev_ops ||
611 		    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
612 		    netif_is_bond_master(real_dev)) {
613 			slave_warn(bond_dev, real_dev,
614 				   "%s: no slave xdo_dev_state_delete\n",
615 				   __func__);
616 			continue;
617 		}
618 
619 		spin_lock_bh(&ipsec->xs->lock);
620 		ipsec->xs->xso.real_dev = NULL;
621 		/* Don't double delete states killed by the user. */
622 		if (ipsec->xs->km.state != XFRM_STATE_DEAD)
623 			real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,
624 								    ipsec->xs);
625 		spin_unlock_bh(&ipsec->xs->lock);
626 
627 		if (real_dev->xfrmdev_ops->xdo_dev_state_free)
628 			real_dev->xfrmdev_ops->xdo_dev_state_free(real_dev,
629 								  ipsec->xs);
630 	}
631 	mutex_unlock(&bond->ipsec_lock);
632 }
633 
634 static void bond_ipsec_free_sa(struct net_device *bond_dev,
635 			       struct xfrm_state *xs)
636 {
637 	struct net_device *real_dev;
638 	struct bond_ipsec *ipsec;
639 	struct bonding *bond;
640 
641 	if (!bond_dev)
642 		return;
643 
644 	bond = netdev_priv(bond_dev);
645 
646 	mutex_lock(&bond->ipsec_lock);
647 	if (!xs->xso.real_dev)
648 		goto out;
649 
650 	real_dev = xs->xso.real_dev;
651 
652 	xs->xso.real_dev = NULL;
653 	if (real_dev->xfrmdev_ops &&
654 	    real_dev->xfrmdev_ops->xdo_dev_state_free)
655 		real_dev->xfrmdev_ops->xdo_dev_state_free(real_dev, xs);
656 out:
657 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
658 		if (ipsec->xs == xs) {
659 			list_del(&ipsec->list);
660 			kfree(ipsec);
661 			break;
662 		}
663 	}
664 	mutex_unlock(&bond->ipsec_lock);
665 }
666 
667 /**
668  * bond_ipsec_offload_ok - can this packet use the xfrm hw offload
669  * @skb: current data packet
670  * @xs: pointer to transformer state struct
671  **/
672 static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
673 {
674 	struct net_device *real_dev;
675 
676 	rcu_read_lock();
677 	real_dev = bond_ipsec_dev(xs);
678 	if (!real_dev || netif_is_bond_master(real_dev)) {
679 		rcu_read_unlock();
680 		return false;
681 	}
682 
683 	rcu_read_unlock();
684 	return true;
685 }
686 
687 /**
688  * bond_advance_esn_state - ESN support for IPSec HW offload
689  * @xs: pointer to transformer state struct
690  **/
691 static void bond_advance_esn_state(struct xfrm_state *xs)
692 {
693 	struct net_device *real_dev;
694 
695 	rcu_read_lock();
696 	real_dev = bond_ipsec_dev(xs);
697 	if (!real_dev)
698 		goto out;
699 
700 	if (!real_dev->xfrmdev_ops ||
701 	    !real_dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
702 		pr_warn_ratelimited("%s: %s doesn't support xdo_dev_state_advance_esn\n", __func__, real_dev->name);
703 		goto out;
704 	}
705 
706 	real_dev->xfrmdev_ops->xdo_dev_state_advance_esn(xs);
707 out:
708 	rcu_read_unlock();
709 }
710 
711 /**
712  * bond_xfrm_update_stats - Update xfrm state
713  * @xs: pointer to transformer state struct
714  **/
715 static void bond_xfrm_update_stats(struct xfrm_state *xs)
716 {
717 	struct net_device *real_dev;
718 
719 	rcu_read_lock();
720 	real_dev = bond_ipsec_dev(xs);
721 	if (!real_dev)
722 		goto out;
723 
724 	if (!real_dev->xfrmdev_ops ||
725 	    !real_dev->xfrmdev_ops->xdo_dev_state_update_stats) {
726 		pr_warn_ratelimited("%s: %s doesn't support xdo_dev_state_update_stats\n", __func__, real_dev->name);
727 		goto out;
728 	}
729 
730 	real_dev->xfrmdev_ops->xdo_dev_state_update_stats(xs);
731 out:
732 	rcu_read_unlock();
733 }
734 
735 static const struct xfrmdev_ops bond_xfrmdev_ops = {
736 	.xdo_dev_state_add = bond_ipsec_add_sa,
737 	.xdo_dev_state_delete = bond_ipsec_del_sa,
738 	.xdo_dev_state_free = bond_ipsec_free_sa,
739 	.xdo_dev_offload_ok = bond_ipsec_offload_ok,
740 	.xdo_dev_state_advance_esn = bond_advance_esn_state,
741 	.xdo_dev_state_update_stats = bond_xfrm_update_stats,
742 };
743 #endif /* CONFIG_XFRM_OFFLOAD */
744 
745 /*------------------------------- Link status -------------------------------*/
746 
747 /* Set the carrier state for the master according to the state of its
748  * slaves.  If any slaves are up, the master is up.  In 802.3ad mode,
749  * do special 802.3ad magic.
750  *
751  * Returns zero if carrier state does not change, nonzero if it does.
752  */
753 int bond_set_carrier(struct bonding *bond)
754 {
755 	struct list_head *iter;
756 	struct slave *slave;
757 
758 	if (!bond_has_slaves(bond))
759 		goto down;
760 
761 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
762 		return bond_3ad_set_carrier(bond);
763 
764 	bond_for_each_slave(bond, slave, iter) {
765 		if (slave->link == BOND_LINK_UP) {
766 			if (!netif_carrier_ok(bond->dev)) {
767 				netif_carrier_on(bond->dev);
768 				return 1;
769 			}
770 			return 0;
771 		}
772 	}
773 
774 down:
775 	if (netif_carrier_ok(bond->dev)) {
776 		netif_carrier_off(bond->dev);
777 		return 1;
778 	}
779 	return 0;
780 }
781 
782 /* Get link speed and duplex from the slave's base driver
783  * using ethtool. If for some reason the call fails or the
784  * values are invalid, set speed and duplex to -1,
785  * and return. Return 1 if speed or duplex settings are
786  * UNKNOWN; 0 otherwise.
787  */
788 static int bond_update_speed_duplex(struct slave *slave)
789 {
790 	struct net_device *slave_dev = slave->dev;
791 	struct ethtool_link_ksettings ecmd;
792 	int res;
793 
794 	slave->speed = SPEED_UNKNOWN;
795 	slave->duplex = DUPLEX_UNKNOWN;
796 
797 	res = __ethtool_get_link_ksettings(slave_dev, &ecmd);
798 	if (res < 0)
799 		return 1;
800 	if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1))
801 		return 1;
802 	switch (ecmd.base.duplex) {
803 	case DUPLEX_FULL:
804 	case DUPLEX_HALF:
805 		break;
806 	default:
807 		return 1;
808 	}
809 
810 	slave->speed = ecmd.base.speed;
811 	slave->duplex = ecmd.base.duplex;
812 
813 	return 0;
814 }
815 
816 const char *bond_slave_link_status(s8 link)
817 {
818 	switch (link) {
819 	case BOND_LINK_UP:
820 		return "up";
821 	case BOND_LINK_FAIL:
822 		return "going down";
823 	case BOND_LINK_DOWN:
824 		return "down";
825 	case BOND_LINK_BACK:
826 		return "going back";
827 	default:
828 		return "unknown";
829 	}
830 }
831 
832 /*----------------------------- Multicast list ------------------------------*/
833 
834 /* Push the promiscuity flag down to appropriate slaves */
835 static int bond_set_promiscuity(struct bonding *bond, int inc)
836 {
837 	struct list_head *iter;
838 	int err = 0;
839 
840 	if (bond_uses_primary(bond)) {
841 		struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
842 
843 		if (curr_active)
844 			err = dev_set_promiscuity(curr_active->dev, inc);
845 	} else {
846 		struct slave *slave;
847 
848 		bond_for_each_slave(bond, slave, iter) {
849 			err = dev_set_promiscuity(slave->dev, inc);
850 			if (err)
851 				return err;
852 		}
853 	}
854 	return err;
855 }
856 
857 /* Push the allmulti flag down to all slaves */
858 static int bond_set_allmulti(struct bonding *bond, int inc)
859 {
860 	struct list_head *iter;
861 	int err = 0;
862 
863 	if (bond_uses_primary(bond)) {
864 		struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
865 
866 		if (curr_active)
867 			err = dev_set_allmulti(curr_active->dev, inc);
868 	} else {
869 		struct slave *slave;
870 
871 		bond_for_each_slave(bond, slave, iter) {
872 			err = dev_set_allmulti(slave->dev, inc);
873 			if (err)
874 				return err;
875 		}
876 	}
877 	return err;
878 }
879 
880 /* Retrieve the list of registered multicast addresses for the bonding
881  * device and retransmit an IGMP JOIN request to the current active
882  * slave.
883  */
884 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
885 {
886 	struct bonding *bond = container_of(work, struct bonding,
887 					    mcast_work.work);
888 
889 	if (!rtnl_trylock()) {
890 		queue_delayed_work(bond->wq, &bond->mcast_work, 1);
891 		return;
892 	}
893 	call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
894 
895 	if (bond->igmp_retrans > 1) {
896 		bond->igmp_retrans--;
897 		queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
898 	}
899 	rtnl_unlock();
900 }
901 
902 /* Flush bond's hardware addresses from slave */
903 static void bond_hw_addr_flush(struct net_device *bond_dev,
904 			       struct net_device *slave_dev)
905 {
906 	struct bonding *bond = netdev_priv(bond_dev);
907 
908 	dev_uc_unsync(slave_dev, bond_dev);
909 	dev_mc_unsync(slave_dev, bond_dev);
910 
911 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
912 		dev_mc_del(slave_dev, lacpdu_mcast_addr);
913 }
914 
915 /*--------------------------- Active slave change ---------------------------*/
916 
917 /* Update the hardware address list and promisc/allmulti for the new and
918  * old active slaves (if any).  Modes that are not using primary keep all
919  * slaves up date at all times; only the modes that use primary need to call
920  * this function to swap these settings during a failover.
921  */
922 static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
923 			      struct slave *old_active)
924 {
925 	if (old_active) {
926 		if (bond->dev->flags & IFF_PROMISC)
927 			dev_set_promiscuity(old_active->dev, -1);
928 
929 		if (bond->dev->flags & IFF_ALLMULTI)
930 			dev_set_allmulti(old_active->dev, -1);
931 
932 		if (bond->dev->flags & IFF_UP)
933 			bond_hw_addr_flush(bond->dev, old_active->dev);
934 
935 		bond_slave_ns_maddrs_add(bond, old_active);
936 	}
937 
938 	if (new_active) {
939 		/* FIXME: Signal errors upstream. */
940 		if (bond->dev->flags & IFF_PROMISC)
941 			dev_set_promiscuity(new_active->dev, 1);
942 
943 		if (bond->dev->flags & IFF_ALLMULTI)
944 			dev_set_allmulti(new_active->dev, 1);
945 
946 		if (bond->dev->flags & IFF_UP) {
947 			netif_addr_lock_bh(bond->dev);
948 			dev_uc_sync(new_active->dev, bond->dev);
949 			dev_mc_sync(new_active->dev, bond->dev);
950 			netif_addr_unlock_bh(bond->dev);
951 		}
952 
953 		bond_slave_ns_maddrs_del(bond, new_active);
954 	}
955 }
956 
957 /**
958  * bond_set_dev_addr - clone slave's address to bond
959  * @bond_dev: bond net device
960  * @slave_dev: slave net device
961  *
962  * Should be called with RTNL held.
963  */
964 static int bond_set_dev_addr(struct net_device *bond_dev,
965 			     struct net_device *slave_dev)
966 {
967 	int err;
968 
969 	slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
970 		  bond_dev, slave_dev, slave_dev->addr_len);
971 	err = netif_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL);
972 	if (err)
973 		return err;
974 
975 	__dev_addr_set(bond_dev, slave_dev->dev_addr, slave_dev->addr_len);
976 	bond_dev->addr_assign_type = NET_ADDR_STOLEN;
977 	call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
978 	return 0;
979 }
980 
981 static struct slave *bond_get_old_active(struct bonding *bond,
982 					 struct slave *new_active)
983 {
984 	struct slave *slave;
985 	struct list_head *iter;
986 
987 	bond_for_each_slave(bond, slave, iter) {
988 		if (slave == new_active)
989 			continue;
990 
991 		if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
992 			return slave;
993 	}
994 
995 	return NULL;
996 }
997 
998 /* bond_do_fail_over_mac
999  *
1000  * Perform special MAC address swapping for fail_over_mac settings
1001  *
1002  * Called with RTNL
1003  */
1004 static void bond_do_fail_over_mac(struct bonding *bond,
1005 				  struct slave *new_active,
1006 				  struct slave *old_active)
1007 {
1008 	u8 tmp_mac[MAX_ADDR_LEN];
1009 	struct sockaddr_storage ss;
1010 	int rv;
1011 
1012 	switch (bond->params.fail_over_mac) {
1013 	case BOND_FOM_ACTIVE:
1014 		if (new_active) {
1015 			rv = bond_set_dev_addr(bond->dev, new_active->dev);
1016 			if (rv)
1017 				slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n",
1018 					  -rv);
1019 		}
1020 		break;
1021 	case BOND_FOM_FOLLOW:
1022 		/* if new_active && old_active, swap them
1023 		 * if just old_active, do nothing (going to no active slave)
1024 		 * if just new_active, set new_active to bond's MAC
1025 		 */
1026 		if (!new_active)
1027 			return;
1028 
1029 		if (!old_active)
1030 			old_active = bond_get_old_active(bond, new_active);
1031 
1032 		if (old_active) {
1033 			bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr,
1034 					  new_active->dev->addr_len);
1035 			bond_hw_addr_copy(ss.__data,
1036 					  old_active->dev->dev_addr,
1037 					  old_active->dev->addr_len);
1038 			ss.ss_family = new_active->dev->type;
1039 		} else {
1040 			bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
1041 					  bond->dev->addr_len);
1042 			ss.ss_family = bond->dev->type;
1043 		}
1044 
1045 		rv = dev_set_mac_address(new_active->dev, &ss, NULL);
1046 		if (rv) {
1047 			slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n",
1048 				  -rv);
1049 			goto out;
1050 		}
1051 
1052 		if (!old_active)
1053 			goto out;
1054 
1055 		bond_hw_addr_copy(ss.__data, tmp_mac,
1056 				  new_active->dev->addr_len);
1057 		ss.ss_family = old_active->dev->type;
1058 
1059 		rv = dev_set_mac_address(old_active->dev, &ss, NULL);
1060 		if (rv)
1061 			slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n",
1062 				  -rv);
1063 out:
1064 		break;
1065 	default:
1066 		netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n",
1067 			   bond->params.fail_over_mac);
1068 		break;
1069 	}
1070 
1071 }
1072 
1073 /**
1074  * bond_choose_primary_or_current - select the primary or high priority slave
1075  * @bond: our bonding struct
1076  *
1077  * - Check if there is a primary link. If the primary link was set and is up,
1078  *   go on and do link reselection.
1079  *
1080  * - If primary link is not set or down, find the highest priority link.
1081  *   If the highest priority link is not current slave, set it as primary
1082  *   link and do link reselection.
1083  */
1084 static struct slave *bond_choose_primary_or_current(struct bonding *bond)
1085 {
1086 	struct slave *prim = rtnl_dereference(bond->primary_slave);
1087 	struct slave *curr = rtnl_dereference(bond->curr_active_slave);
1088 	struct slave *slave, *hprio = NULL;
1089 	struct list_head *iter;
1090 
1091 	if (!prim || prim->link != BOND_LINK_UP) {
1092 		bond_for_each_slave(bond, slave, iter) {
1093 			if (slave->link == BOND_LINK_UP) {
1094 				hprio = hprio ?: slave;
1095 				if (slave->prio > hprio->prio)
1096 					hprio = slave;
1097 			}
1098 		}
1099 
1100 		if (hprio && hprio != curr) {
1101 			prim = hprio;
1102 			goto link_reselect;
1103 		}
1104 
1105 		if (!curr || curr->link != BOND_LINK_UP)
1106 			return NULL;
1107 		return curr;
1108 	}
1109 
1110 	if (bond->force_primary) {
1111 		bond->force_primary = false;
1112 		return prim;
1113 	}
1114 
1115 link_reselect:
1116 	if (!curr || curr->link != BOND_LINK_UP)
1117 		return prim;
1118 
1119 	/* At this point, prim and curr are both up */
1120 	switch (bond->params.primary_reselect) {
1121 	case BOND_PRI_RESELECT_ALWAYS:
1122 		return prim;
1123 	case BOND_PRI_RESELECT_BETTER:
1124 		if (prim->speed < curr->speed)
1125 			return curr;
1126 		if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
1127 			return curr;
1128 		return prim;
1129 	case BOND_PRI_RESELECT_FAILURE:
1130 		return curr;
1131 	default:
1132 		netdev_err(bond->dev, "impossible primary_reselect %d\n",
1133 			   bond->params.primary_reselect);
1134 		return curr;
1135 	}
1136 }
1137 
1138 /**
1139  * bond_find_best_slave - select the best available slave to be the active one
1140  * @bond: our bonding struct
1141  */
1142 static struct slave *bond_find_best_slave(struct bonding *bond)
1143 {
1144 	struct slave *slave, *bestslave = NULL;
1145 	struct list_head *iter;
1146 	int mintime = bond->params.updelay;
1147 
1148 	slave = bond_choose_primary_or_current(bond);
1149 	if (slave)
1150 		return slave;
1151 
1152 	bond_for_each_slave(bond, slave, iter) {
1153 		if (slave->link == BOND_LINK_UP)
1154 			return slave;
1155 		if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
1156 		    slave->delay < mintime) {
1157 			mintime = slave->delay;
1158 			bestslave = slave;
1159 		}
1160 	}
1161 
1162 	return bestslave;
1163 }
1164 
1165 /* must be called in RCU critical section or with RTNL held */
1166 static bool bond_should_notify_peers(struct bonding *bond)
1167 {
1168 	struct bond_up_slave *usable;
1169 	struct slave *slave = NULL;
1170 
1171 	if (!bond->send_peer_notif ||
1172 	    bond->send_peer_notif %
1173 	    max(1, bond->params.peer_notif_delay) != 0 ||
1174 	    !netif_carrier_ok(bond->dev))
1175 		return false;
1176 
1177 	/* The send_peer_notif is set by active-backup or 8023ad
1178 	 * mode, and cleared in bond_close() when changing mode.
1179 	 * It is safe to only check bond mode here.
1180 	 */
1181 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1182 		usable = rcu_dereference_rtnl(bond->usable_slaves);
1183 		if (!usable || !READ_ONCE(usable->count))
1184 			return false;
1185 	} else {
1186 		slave = rcu_dereference_rtnl(bond->curr_active_slave);
1187 		if (!slave || test_bit(__LINK_STATE_LINKWATCH_PENDING,
1188 				       &slave->dev->state))
1189 			return false;
1190 	}
1191 
1192 	netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n",
1193 		   slave ? slave->dev->name : "all");
1194 
1195 	return true;
1196 }
1197 
1198 /**
1199  * bond_change_active_slave - change the active slave into the specified one
1200  * @bond: our bonding struct
1201  * @new_active: the new slave to make the active one
1202  *
1203  * Set the new slave to the bond's settings and unset them on the old
1204  * curr_active_slave.
1205  * Setting include flags, mc-list, promiscuity, allmulti, etc.
1206  *
1207  * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1208  * because it is apparently the best available slave we have, even though its
1209  * updelay hasn't timed out yet.
1210  *
1211  * Caller must hold RTNL.
1212  */
1213 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1214 {
1215 	struct slave *old_active;
1216 
1217 	ASSERT_RTNL();
1218 
1219 	old_active = rtnl_dereference(bond->curr_active_slave);
1220 
1221 	if (old_active == new_active)
1222 		return;
1223 
1224 #ifdef CONFIG_XFRM_OFFLOAD
1225 	bond_ipsec_del_sa_all(bond);
1226 #endif /* CONFIG_XFRM_OFFLOAD */
1227 
1228 	if (new_active) {
1229 		new_active->last_link_up = jiffies;
1230 
1231 		if (new_active->link == BOND_LINK_BACK) {
1232 			if (bond_uses_primary(bond)) {
1233 				slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n",
1234 					   (bond->params.updelay - new_active->delay) * bond->params.miimon);
1235 			}
1236 
1237 			new_active->delay = 0;
1238 			bond_set_slave_link_state(new_active, BOND_LINK_UP,
1239 						  BOND_SLAVE_NOTIFY_NOW);
1240 
1241 			if (BOND_MODE(bond) == BOND_MODE_8023AD)
1242 				bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1243 
1244 			if (bond_is_lb(bond))
1245 				bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1246 		} else {
1247 			if (bond_uses_primary(bond))
1248 				slave_info(bond->dev, new_active->dev, "making interface the new active one\n");
1249 		}
1250 	}
1251 
1252 	if (bond_uses_primary(bond))
1253 		bond_hw_addr_swap(bond, new_active, old_active);
1254 
1255 	if (bond_is_lb(bond)) {
1256 		bond_alb_handle_active_change(bond, new_active);
1257 		if (old_active)
1258 			bond_set_slave_inactive_flags(old_active,
1259 						      BOND_SLAVE_NOTIFY_NOW);
1260 		if (new_active)
1261 			bond_set_slave_active_flags(new_active,
1262 						    BOND_SLAVE_NOTIFY_NOW);
1263 	} else {
1264 		rcu_assign_pointer(bond->curr_active_slave, new_active);
1265 	}
1266 
1267 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
1268 		if (old_active)
1269 			bond_set_slave_inactive_flags(old_active,
1270 						      BOND_SLAVE_NOTIFY_NOW);
1271 
1272 		if (new_active) {
1273 			bool should_notify_peers = false;
1274 
1275 			bond_set_slave_active_flags(new_active,
1276 						    BOND_SLAVE_NOTIFY_NOW);
1277 
1278 			if (bond->params.fail_over_mac)
1279 				bond_do_fail_over_mac(bond, new_active,
1280 						      old_active);
1281 
1282 			if (netif_running(bond->dev)) {
1283 				bond->send_peer_notif =
1284 					bond->params.num_peer_notif *
1285 					max(1, bond->params.peer_notif_delay);
1286 				should_notify_peers =
1287 					bond_should_notify_peers(bond);
1288 			}
1289 
1290 			call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
1291 			if (should_notify_peers) {
1292 				bond->send_peer_notif--;
1293 				call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
1294 							 bond->dev);
1295 			}
1296 		}
1297 	}
1298 
1299 #ifdef CONFIG_XFRM_OFFLOAD
1300 	bond_ipsec_add_sa_all(bond);
1301 #endif /* CONFIG_XFRM_OFFLOAD */
1302 
1303 	/* resend IGMP joins since active slave has changed or
1304 	 * all were sent on curr_active_slave.
1305 	 * resend only if bond is brought up with the affected
1306 	 * bonding modes and the retransmission is enabled
1307 	 */
1308 	if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
1309 	    ((bond_uses_primary(bond) && new_active) ||
1310 	     BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) {
1311 		bond->igmp_retrans = bond->params.resend_igmp;
1312 		queue_delayed_work(bond->wq, &bond->mcast_work, 1);
1313 	}
1314 }
1315 
1316 /**
1317  * bond_select_active_slave - select a new active slave, if needed
1318  * @bond: our bonding struct
1319  *
1320  * This functions should be called when one of the following occurs:
1321  * - The old curr_active_slave has been released or lost its link.
1322  * - The primary_slave has got its link back.
1323  * - A slave has got its link back and there's no old curr_active_slave.
1324  *
1325  * Caller must hold RTNL.
1326  */
1327 void bond_select_active_slave(struct bonding *bond)
1328 {
1329 	struct slave *best_slave;
1330 	int rv;
1331 
1332 	ASSERT_RTNL();
1333 
1334 	best_slave = bond_find_best_slave(bond);
1335 	if (best_slave != rtnl_dereference(bond->curr_active_slave)) {
1336 		bond_change_active_slave(bond, best_slave);
1337 		rv = bond_set_carrier(bond);
1338 		if (!rv)
1339 			return;
1340 
1341 		if (netif_carrier_ok(bond->dev))
1342 			netdev_info(bond->dev, "active interface up!\n");
1343 		else
1344 			netdev_info(bond->dev, "now running without any active interface!\n");
1345 	}
1346 }
1347 
1348 #ifdef CONFIG_NET_POLL_CONTROLLER
1349 static inline int slave_enable_netpoll(struct slave *slave)
1350 {
1351 	struct netpoll *np;
1352 	int err = 0;
1353 
1354 	np = kzalloc(sizeof(*np), GFP_KERNEL);
1355 	err = -ENOMEM;
1356 	if (!np)
1357 		goto out;
1358 
1359 	err = __netpoll_setup(np, slave->dev);
1360 	if (err) {
1361 		kfree(np);
1362 		goto out;
1363 	}
1364 	slave->np = np;
1365 out:
1366 	return err;
1367 }
1368 static inline void slave_disable_netpoll(struct slave *slave)
1369 {
1370 	struct netpoll *np = slave->np;
1371 
1372 	if (!np)
1373 		return;
1374 
1375 	slave->np = NULL;
1376 
1377 	__netpoll_free(np);
1378 }
1379 
1380 static void bond_poll_controller(struct net_device *bond_dev)
1381 {
1382 	struct bonding *bond = netdev_priv(bond_dev);
1383 	struct slave *slave = NULL;
1384 	struct list_head *iter;
1385 	struct ad_info ad_info;
1386 
1387 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
1388 		if (bond_3ad_get_active_agg_info(bond, &ad_info))
1389 			return;
1390 
1391 	bond_for_each_slave_rcu(bond, slave, iter) {
1392 		if (!bond_slave_is_up(slave))
1393 			continue;
1394 
1395 		if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1396 			struct aggregator *agg =
1397 			    SLAVE_AD_INFO(slave)->port.aggregator;
1398 
1399 			if (agg &&
1400 			    agg->aggregator_identifier != ad_info.aggregator_id)
1401 				continue;
1402 		}
1403 
1404 		netpoll_poll_dev(slave->dev);
1405 	}
1406 }
1407 
1408 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1409 {
1410 	struct bonding *bond = netdev_priv(bond_dev);
1411 	struct list_head *iter;
1412 	struct slave *slave;
1413 
1414 	bond_for_each_slave(bond, slave, iter)
1415 		if (bond_slave_is_up(slave))
1416 			slave_disable_netpoll(slave);
1417 }
1418 
1419 static int bond_netpoll_setup(struct net_device *dev)
1420 {
1421 	struct bonding *bond = netdev_priv(dev);
1422 	struct list_head *iter;
1423 	struct slave *slave;
1424 	int err = 0;
1425 
1426 	bond_for_each_slave(bond, slave, iter) {
1427 		err = slave_enable_netpoll(slave);
1428 		if (err) {
1429 			bond_netpoll_cleanup(dev);
1430 			break;
1431 		}
1432 	}
1433 	return err;
1434 }
1435 #else
1436 static inline int slave_enable_netpoll(struct slave *slave)
1437 {
1438 	return 0;
1439 }
1440 static inline void slave_disable_netpoll(struct slave *slave)
1441 {
1442 }
1443 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1444 {
1445 }
1446 #endif
1447 
1448 /*---------------------------------- IOCTL ----------------------------------*/
1449 
1450 static netdev_features_t bond_fix_features(struct net_device *dev,
1451 					   netdev_features_t features)
1452 {
1453 	struct bonding *bond = netdev_priv(dev);
1454 	struct list_head *iter;
1455 	netdev_features_t mask;
1456 	struct slave *slave;
1457 
1458 	mask = features;
1459 	features = netdev_base_features(features);
1460 
1461 	bond_for_each_slave(bond, slave, iter) {
1462 		features = netdev_increment_features(features,
1463 						     slave->dev->features,
1464 						     mask);
1465 	}
1466 	features = netdev_add_tso_features(features, mask);
1467 
1468 	return features;
1469 }
1470 
1471 #define BOND_VLAN_FEATURES	(NETIF_F_HW_CSUM | NETIF_F_SG | \
1472 				 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | \
1473 				 NETIF_F_GSO_ENCAP_ALL | \
1474 				 NETIF_F_HIGHDMA | NETIF_F_LRO)
1475 
1476 #define BOND_ENC_FEATURES	(NETIF_F_HW_CSUM | NETIF_F_SG | \
1477 				 NETIF_F_RXCSUM | NETIF_F_GSO_SOFTWARE | \
1478 				 NETIF_F_GSO_PARTIAL)
1479 
1480 #define BOND_MPLS_FEATURES	(NETIF_F_HW_CSUM | NETIF_F_SG | \
1481 				 NETIF_F_GSO_SOFTWARE)
1482 
1483 #define BOND_GSO_PARTIAL_FEATURES (NETIF_F_GSO_ESP)
1484 
1485 
1486 static void bond_compute_features(struct bonding *bond)
1487 {
1488 	netdev_features_t gso_partial_features = BOND_GSO_PARTIAL_FEATURES;
1489 	unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
1490 					IFF_XMIT_DST_RELEASE_PERM;
1491 	netdev_features_t vlan_features = BOND_VLAN_FEATURES;
1492 	netdev_features_t enc_features  = BOND_ENC_FEATURES;
1493 #ifdef CONFIG_XFRM_OFFLOAD
1494 	netdev_features_t xfrm_features  = BOND_XFRM_FEATURES;
1495 #endif /* CONFIG_XFRM_OFFLOAD */
1496 	netdev_features_t mpls_features  = BOND_MPLS_FEATURES;
1497 	struct net_device *bond_dev = bond->dev;
1498 	struct list_head *iter;
1499 	struct slave *slave;
1500 	unsigned short max_hard_header_len = ETH_HLEN;
1501 	unsigned int tso_max_size = TSO_MAX_SIZE;
1502 	u16 tso_max_segs = TSO_MAX_SEGS;
1503 
1504 	if (!bond_has_slaves(bond))
1505 		goto done;
1506 
1507 	vlan_features = netdev_base_features(vlan_features);
1508 	mpls_features = netdev_base_features(mpls_features);
1509 
1510 	bond_for_each_slave(bond, slave, iter) {
1511 		vlan_features = netdev_increment_features(vlan_features,
1512 			slave->dev->vlan_features, BOND_VLAN_FEATURES);
1513 
1514 		enc_features = netdev_increment_features(enc_features,
1515 							 slave->dev->hw_enc_features,
1516 							 BOND_ENC_FEATURES);
1517 
1518 #ifdef CONFIG_XFRM_OFFLOAD
1519 		xfrm_features = netdev_increment_features(xfrm_features,
1520 							  slave->dev->hw_enc_features,
1521 							  BOND_XFRM_FEATURES);
1522 #endif /* CONFIG_XFRM_OFFLOAD */
1523 
1524 		gso_partial_features = netdev_increment_features(gso_partial_features,
1525 								 slave->dev->gso_partial_features,
1526 								 BOND_GSO_PARTIAL_FEATURES);
1527 
1528 		mpls_features = netdev_increment_features(mpls_features,
1529 							  slave->dev->mpls_features,
1530 							  BOND_MPLS_FEATURES);
1531 
1532 		dst_release_flag &= slave->dev->priv_flags;
1533 		if (slave->dev->hard_header_len > max_hard_header_len)
1534 			max_hard_header_len = slave->dev->hard_header_len;
1535 
1536 		tso_max_size = min(tso_max_size, slave->dev->tso_max_size);
1537 		tso_max_segs = min(tso_max_segs, slave->dev->tso_max_segs);
1538 	}
1539 	bond_dev->hard_header_len = max_hard_header_len;
1540 
1541 done:
1542 	bond_dev->gso_partial_features = gso_partial_features;
1543 	bond_dev->vlan_features = vlan_features;
1544 	bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
1545 				    NETIF_F_HW_VLAN_CTAG_TX |
1546 				    NETIF_F_HW_VLAN_STAG_TX;
1547 #ifdef CONFIG_XFRM_OFFLOAD
1548 	bond_dev->hw_enc_features |= xfrm_features;
1549 #endif /* CONFIG_XFRM_OFFLOAD */
1550 	bond_dev->mpls_features = mpls_features;
1551 	netif_set_tso_max_segs(bond_dev, tso_max_segs);
1552 	netif_set_tso_max_size(bond_dev, tso_max_size);
1553 
1554 	bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1555 	if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) &&
1556 	    dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
1557 		bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE;
1558 
1559 	netdev_change_features(bond_dev);
1560 }
1561 
1562 static void bond_setup_by_slave(struct net_device *bond_dev,
1563 				struct net_device *slave_dev)
1564 {
1565 	bool was_up = !!(bond_dev->flags & IFF_UP);
1566 
1567 	dev_close(bond_dev);
1568 
1569 	bond_dev->header_ops	    = slave_dev->header_ops;
1570 
1571 	bond_dev->type		    = slave_dev->type;
1572 	bond_dev->hard_header_len   = slave_dev->hard_header_len;
1573 	bond_dev->needed_headroom   = slave_dev->needed_headroom;
1574 	bond_dev->addr_len	    = slave_dev->addr_len;
1575 
1576 	memcpy(bond_dev->broadcast, slave_dev->broadcast,
1577 		slave_dev->addr_len);
1578 
1579 	if (slave_dev->flags & IFF_POINTOPOINT) {
1580 		bond_dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
1581 		bond_dev->flags |= (IFF_POINTOPOINT | IFF_NOARP);
1582 	}
1583 	if (was_up)
1584 		dev_open(bond_dev, NULL);
1585 }
1586 
1587 /* On bonding slaves other than the currently active slave, suppress
1588  * duplicates except for alb non-mcast/bcast.
1589  */
1590 static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1591 					    struct slave *slave,
1592 					    struct bonding *bond)
1593 {
1594 	if (bond_is_slave_inactive(slave)) {
1595 		if (BOND_MODE(bond) == BOND_MODE_ALB &&
1596 		    skb->pkt_type != PACKET_BROADCAST &&
1597 		    skb->pkt_type != PACKET_MULTICAST)
1598 			return false;
1599 		return true;
1600 	}
1601 	return false;
1602 }
1603 
1604 static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
1605 {
1606 	struct sk_buff *skb = *pskb;
1607 	struct slave *slave;
1608 	struct bonding *bond;
1609 	int (*recv_probe)(const struct sk_buff *, struct bonding *,
1610 			  struct slave *);
1611 	int ret = RX_HANDLER_ANOTHER;
1612 
1613 	skb = skb_share_check(skb, GFP_ATOMIC);
1614 	if (unlikely(!skb))
1615 		return RX_HANDLER_CONSUMED;
1616 
1617 	*pskb = skb;
1618 
1619 	slave = bond_slave_get_rcu(skb->dev);
1620 	bond = slave->bond;
1621 
1622 	recv_probe = READ_ONCE(bond->recv_probe);
1623 	if (recv_probe) {
1624 		ret = recv_probe(skb, bond, slave);
1625 		if (ret == RX_HANDLER_CONSUMED) {
1626 			consume_skb(skb);
1627 			return ret;
1628 		}
1629 	}
1630 
1631 	/*
1632 	 * For packets determined by bond_should_deliver_exact_match() call to
1633 	 * be suppressed we want to make an exception for link-local packets.
1634 	 * This is necessary for e.g. LLDP daemons to be able to monitor
1635 	 * inactive slave links without being forced to bind to them
1636 	 * explicitly.
1637 	 *
1638 	 * At the same time, packets that are passed to the bonding master
1639 	 * (including link-local ones) can have their originating interface
1640 	 * determined via PACKET_ORIGDEV socket option.
1641 	 */
1642 	if (bond_should_deliver_exact_match(skb, slave, bond)) {
1643 		if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
1644 			return RX_HANDLER_PASS;
1645 		return RX_HANDLER_EXACT;
1646 	}
1647 
1648 	skb->dev = bond->dev;
1649 
1650 	if (BOND_MODE(bond) == BOND_MODE_ALB &&
1651 	    netif_is_bridge_port(bond->dev) &&
1652 	    skb->pkt_type == PACKET_HOST) {
1653 
1654 		if (unlikely(skb_cow_head(skb,
1655 					  skb->data - skb_mac_header(skb)))) {
1656 			kfree_skb(skb);
1657 			return RX_HANDLER_CONSUMED;
1658 		}
1659 		bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr,
1660 				  bond->dev->addr_len);
1661 	}
1662 
1663 	return ret;
1664 }
1665 
1666 static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond)
1667 {
1668 	switch (BOND_MODE(bond)) {
1669 	case BOND_MODE_ROUNDROBIN:
1670 		return NETDEV_LAG_TX_TYPE_ROUNDROBIN;
1671 	case BOND_MODE_ACTIVEBACKUP:
1672 		return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
1673 	case BOND_MODE_BROADCAST:
1674 		return NETDEV_LAG_TX_TYPE_BROADCAST;
1675 	case BOND_MODE_XOR:
1676 	case BOND_MODE_8023AD:
1677 		return NETDEV_LAG_TX_TYPE_HASH;
1678 	default:
1679 		return NETDEV_LAG_TX_TYPE_UNKNOWN;
1680 	}
1681 }
1682 
1683 static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
1684 					       enum netdev_lag_tx_type type)
1685 {
1686 	if (type != NETDEV_LAG_TX_TYPE_HASH)
1687 		return NETDEV_LAG_HASH_NONE;
1688 
1689 	switch (bond->params.xmit_policy) {
1690 	case BOND_XMIT_POLICY_LAYER2:
1691 		return NETDEV_LAG_HASH_L2;
1692 	case BOND_XMIT_POLICY_LAYER34:
1693 		return NETDEV_LAG_HASH_L34;
1694 	case BOND_XMIT_POLICY_LAYER23:
1695 		return NETDEV_LAG_HASH_L23;
1696 	case BOND_XMIT_POLICY_ENCAP23:
1697 		return NETDEV_LAG_HASH_E23;
1698 	case BOND_XMIT_POLICY_ENCAP34:
1699 		return NETDEV_LAG_HASH_E34;
1700 	case BOND_XMIT_POLICY_VLAN_SRCMAC:
1701 		return NETDEV_LAG_HASH_VLAN_SRCMAC;
1702 	default:
1703 		return NETDEV_LAG_HASH_UNKNOWN;
1704 	}
1705 }
1706 
1707 static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave,
1708 				      struct netlink_ext_ack *extack)
1709 {
1710 	struct netdev_lag_upper_info lag_upper_info;
1711 	enum netdev_lag_tx_type type;
1712 	int err;
1713 
1714 	type = bond_lag_tx_type(bond);
1715 	lag_upper_info.tx_type = type;
1716 	lag_upper_info.hash_type = bond_lag_hash_type(bond, type);
1717 
1718 	err = netdev_master_upper_dev_link(slave->dev, bond->dev, slave,
1719 					   &lag_upper_info, extack);
1720 	if (err)
1721 		return err;
1722 
1723 	slave->dev->flags |= IFF_SLAVE;
1724 	return 0;
1725 }
1726 
1727 static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave)
1728 {
1729 	netdev_upper_dev_unlink(slave->dev, bond->dev);
1730 	slave->dev->flags &= ~IFF_SLAVE;
1731 }
1732 
1733 static void slave_kobj_release(struct kobject *kobj)
1734 {
1735 	struct slave *slave = to_slave(kobj);
1736 	struct bonding *bond = bond_get_bond_by_slave(slave);
1737 
1738 	cancel_delayed_work_sync(&slave->notify_work);
1739 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
1740 		kfree(SLAVE_AD_INFO(slave));
1741 
1742 	kfree(slave);
1743 }
1744 
1745 static struct kobj_type slave_ktype = {
1746 	.release = slave_kobj_release,
1747 #ifdef CONFIG_SYSFS
1748 	.sysfs_ops = &slave_sysfs_ops,
1749 #endif
1750 };
1751 
1752 static int bond_kobj_init(struct slave *slave)
1753 {
1754 	int err;
1755 
1756 	err = kobject_init_and_add(&slave->kobj, &slave_ktype,
1757 				   &(slave->dev->dev.kobj), "bonding_slave");
1758 	if (err)
1759 		kobject_put(&slave->kobj);
1760 
1761 	return err;
1762 }
1763 
1764 static struct slave *bond_alloc_slave(struct bonding *bond,
1765 				      struct net_device *slave_dev)
1766 {
1767 	struct slave *slave = NULL;
1768 
1769 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1770 	if (!slave)
1771 		return NULL;
1772 
1773 	slave->bond = bond;
1774 	slave->dev = slave_dev;
1775 	INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work);
1776 
1777 	if (bond_kobj_init(slave))
1778 		return NULL;
1779 
1780 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1781 		SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info),
1782 					       GFP_KERNEL);
1783 		if (!SLAVE_AD_INFO(slave)) {
1784 			kobject_put(&slave->kobj);
1785 			return NULL;
1786 		}
1787 	}
1788 
1789 	return slave;
1790 }
1791 
1792 static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info)
1793 {
1794 	info->bond_mode = BOND_MODE(bond);
1795 	info->miimon = bond->params.miimon;
1796 	info->num_slaves = bond->slave_cnt;
1797 }
1798 
1799 static void bond_fill_ifslave(struct slave *slave, struct ifslave *info)
1800 {
1801 	strcpy(info->slave_name, slave->dev->name);
1802 	info->link = slave->link;
1803 	info->state = bond_slave_state(slave);
1804 	info->link_failure_count = slave->link_failure_count;
1805 }
1806 
1807 static void bond_netdev_notify_work(struct work_struct *_work)
1808 {
1809 	struct slave *slave = container_of(_work, struct slave,
1810 					   notify_work.work);
1811 
1812 	if (rtnl_trylock()) {
1813 		struct netdev_bonding_info binfo;
1814 
1815 		bond_fill_ifslave(slave, &binfo.slave);
1816 		bond_fill_ifbond(slave->bond, &binfo.master);
1817 		netdev_bonding_info_change(slave->dev, &binfo);
1818 		rtnl_unlock();
1819 	} else {
1820 		queue_delayed_work(slave->bond->wq, &slave->notify_work, 1);
1821 	}
1822 }
1823 
1824 void bond_queue_slave_event(struct slave *slave)
1825 {
1826 	queue_delayed_work(slave->bond->wq, &slave->notify_work, 0);
1827 }
1828 
1829 void bond_lower_state_changed(struct slave *slave)
1830 {
1831 	struct netdev_lag_lower_state_info info;
1832 
1833 	info.link_up = slave->link == BOND_LINK_UP ||
1834 		       slave->link == BOND_LINK_FAIL;
1835 	info.tx_enabled = bond_is_active_slave(slave);
1836 	netdev_lower_state_changed(slave->dev, &info);
1837 }
1838 
1839 #define BOND_NL_ERR(bond_dev, extack, errmsg) do {		\
1840 	if (extack)						\
1841 		NL_SET_ERR_MSG(extack, errmsg);			\
1842 	else							\
1843 		netdev_err(bond_dev, "Error: %s\n", errmsg);	\
1844 } while (0)
1845 
1846 #define SLAVE_NL_ERR(bond_dev, slave_dev, extack, errmsg) do {		\
1847 	if (extack)							\
1848 		NL_SET_ERR_MSG(extack, errmsg);				\
1849 	else								\
1850 		slave_err(bond_dev, slave_dev, "Error: %s\n", errmsg);	\
1851 } while (0)
1852 
1853 /* The bonding driver uses ether_setup() to convert a master bond device
1854  * to ARPHRD_ETHER, that resets the target netdevice's flags so we always
1855  * have to restore the IFF_MASTER flag, and only restore IFF_SLAVE and IFF_UP
1856  * if they were set
1857  */
1858 static void bond_ether_setup(struct net_device *bond_dev)
1859 {
1860 	unsigned int flags = bond_dev->flags & (IFF_SLAVE | IFF_UP);
1861 
1862 	ether_setup(bond_dev);
1863 	bond_dev->flags |= IFF_MASTER | flags;
1864 	bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1865 }
1866 
1867 void bond_xdp_set_features(struct net_device *bond_dev)
1868 {
1869 	struct bonding *bond = netdev_priv(bond_dev);
1870 	xdp_features_t val = NETDEV_XDP_ACT_MASK;
1871 	struct list_head *iter;
1872 	struct slave *slave;
1873 
1874 	ASSERT_RTNL();
1875 
1876 	if (!bond_xdp_check(bond, BOND_MODE(bond)) || !bond_has_slaves(bond)) {
1877 		xdp_clear_features_flag(bond_dev);
1878 		return;
1879 	}
1880 
1881 	bond_for_each_slave(bond, slave, iter)
1882 		val &= slave->dev->xdp_features;
1883 
1884 	val &= ~NETDEV_XDP_ACT_XSK_ZEROCOPY;
1885 
1886 	xdp_set_features_flag(bond_dev, val);
1887 }
1888 
1889 /* enslave device <slave> to bond device <master> */
1890 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
1891 		 struct netlink_ext_ack *extack)
1892 {
1893 	struct bonding *bond = netdev_priv(bond_dev);
1894 	const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1895 	struct slave *new_slave = NULL, *prev_slave;
1896 	struct sockaddr_storage ss;
1897 	int res = 0, i;
1898 
1899 	if (slave_dev->flags & IFF_MASTER &&
1900 	    !netif_is_bond_master(slave_dev)) {
1901 		BOND_NL_ERR(bond_dev, extack,
1902 			    "Device type (master device) cannot be enslaved");
1903 		return -EPERM;
1904 	}
1905 
1906 	/* already in-use? */
1907 	if (netdev_is_rx_handler_busy(slave_dev)) {
1908 		SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1909 			     "Device is in use and cannot be enslaved");
1910 		return -EBUSY;
1911 	}
1912 
1913 	if (bond_dev == slave_dev) {
1914 		BOND_NL_ERR(bond_dev, extack, "Cannot enslave bond to itself.");
1915 		return -EPERM;
1916 	}
1917 
1918 	/* vlan challenged mutual exclusion */
1919 	/* no need to lock since we're protected by rtnl_lock */
1920 	if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1921 		slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n");
1922 		if (vlan_uses_dev(bond_dev)) {
1923 			SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1924 				     "Can not enslave VLAN challenged device to VLAN enabled bond");
1925 			return -EPERM;
1926 		} else {
1927 			slave_warn(bond_dev, slave_dev, "enslaved VLAN challenged slave. Adding VLANs will be blocked as long as it is part of bond.\n");
1928 		}
1929 	} else {
1930 		slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n");
1931 	}
1932 
1933 	if (slave_dev->features & NETIF_F_HW_ESP)
1934 		slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n");
1935 
1936 	/* Old ifenslave binaries are no longer supported.  These can
1937 	 * be identified with moderate accuracy by the state of the slave:
1938 	 * the current ifenslave will set the interface down prior to
1939 	 * enslaving it; the old ifenslave will not.
1940 	 */
1941 	if (slave_dev->flags & IFF_UP) {
1942 		SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1943 			     "Device can not be enslaved while up");
1944 		return -EPERM;
1945 	}
1946 
1947 	/* set bonding device ether type by slave - bonding netdevices are
1948 	 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1949 	 * there is a need to override some of the type dependent attribs/funcs.
1950 	 *
1951 	 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1952 	 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1953 	 */
1954 	if (!bond_has_slaves(bond)) {
1955 		if (bond_dev->type != slave_dev->type) {
1956 			slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n",
1957 				  bond_dev->type, slave_dev->type);
1958 
1959 			res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
1960 						       bond_dev);
1961 			res = notifier_to_errno(res);
1962 			if (res) {
1963 				slave_err(bond_dev, slave_dev, "refused to change device type\n");
1964 				return -EBUSY;
1965 			}
1966 
1967 			/* Flush unicast and multicast addresses */
1968 			dev_uc_flush(bond_dev);
1969 			dev_mc_flush(bond_dev);
1970 
1971 			if (slave_dev->type != ARPHRD_ETHER)
1972 				bond_setup_by_slave(bond_dev, slave_dev);
1973 			else
1974 				bond_ether_setup(bond_dev);
1975 
1976 			call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
1977 						 bond_dev);
1978 		}
1979 	} else if (bond_dev->type != slave_dev->type) {
1980 		SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1981 			     "Device type is different from other slaves");
1982 		return -EINVAL;
1983 	}
1984 
1985 	if (slave_dev->type == ARPHRD_INFINIBAND &&
1986 	    BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
1987 		SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1988 			     "Only active-backup mode is supported for infiniband slaves");
1989 		res = -EOPNOTSUPP;
1990 		goto err_undo_flags;
1991 	}
1992 
1993 	if (!slave_ops->ndo_set_mac_address ||
1994 	    slave_dev->type == ARPHRD_INFINIBAND) {
1995 		slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n");
1996 		if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
1997 		    bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1998 			if (!bond_has_slaves(bond)) {
1999 				bond->params.fail_over_mac = BOND_FOM_ACTIVE;
2000 				slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n");
2001 			} else {
2002 				SLAVE_NL_ERR(bond_dev, slave_dev, extack,
2003 					     "Slave device does not support setting the MAC address, but fail_over_mac is not set to active");
2004 				res = -EOPNOTSUPP;
2005 				goto err_undo_flags;
2006 			}
2007 		}
2008 	}
2009 
2010 	call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
2011 
2012 	/* If this is the first slave, then we need to set the master's hardware
2013 	 * address to be the same as the slave's.
2014 	 */
2015 	if (!bond_has_slaves(bond) &&
2016 	    bond->dev->addr_assign_type == NET_ADDR_RANDOM) {
2017 		res = bond_set_dev_addr(bond->dev, slave_dev);
2018 		if (res)
2019 			goto err_undo_flags;
2020 	}
2021 
2022 	new_slave = bond_alloc_slave(bond, slave_dev);
2023 	if (!new_slave) {
2024 		res = -ENOMEM;
2025 		goto err_undo_flags;
2026 	}
2027 
2028 	/* Set the new_slave's queue_id to be zero.  Queue ID mapping
2029 	 * is set via sysfs or module option if desired.
2030 	 */
2031 	new_slave->queue_id = 0;
2032 
2033 	/* Save slave's original mtu and then set it to match the bond */
2034 	new_slave->original_mtu = slave_dev->mtu;
2035 	res = dev_set_mtu(slave_dev, bond->dev->mtu);
2036 	if (res) {
2037 		slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res);
2038 		goto err_free;
2039 	}
2040 
2041 	/* Save slave's original ("permanent") mac address for modes
2042 	 * that need it, and for restoring it upon release, and then
2043 	 * set it to the master's address
2044 	 */
2045 	bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr,
2046 			  slave_dev->addr_len);
2047 
2048 	if (!bond->params.fail_over_mac ||
2049 	    BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2050 		/* Set slave to master's mac address.  The application already
2051 		 * set the master's mac address to that of the first slave
2052 		 */
2053 		memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
2054 	} else if (bond->params.fail_over_mac == BOND_FOM_FOLLOW &&
2055 		   BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
2056 		   bond_has_slaves(bond) &&
2057 		   memcmp(slave_dev->dev_addr, bond_dev->dev_addr, bond_dev->addr_len) == 0) {
2058 		/* Set slave to random address to avoid duplicate mac
2059 		 * address in later fail over.
2060 		 */
2061 		eth_random_addr(ss.__data);
2062 	} else {
2063 		goto skip_mac_set;
2064 	}
2065 
2066 	ss.ss_family = slave_dev->type;
2067 	res = dev_set_mac_address(slave_dev, &ss, extack);
2068 	if (res) {
2069 		slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
2070 		goto err_restore_mtu;
2071 	}
2072 
2073 skip_mac_set:
2074 
2075 	/* set no_addrconf flag before open to prevent IPv6 addrconf */
2076 	slave_dev->priv_flags |= IFF_NO_ADDRCONF;
2077 
2078 	/* open the slave since the application closed it */
2079 	res = dev_open(slave_dev, extack);
2080 	if (res) {
2081 		slave_err(bond_dev, slave_dev, "Opening slave failed\n");
2082 		goto err_restore_mac;
2083 	}
2084 
2085 	slave_dev->priv_flags |= IFF_BONDING;
2086 	/* initialize slave stats */
2087 	dev_get_stats(new_slave->dev, &new_slave->slave_stats);
2088 
2089 	if (bond_is_lb(bond)) {
2090 		/* bond_alb_init_slave() must be called before all other stages since
2091 		 * it might fail and we do not want to have to undo everything
2092 		 */
2093 		res = bond_alb_init_slave(bond, new_slave);
2094 		if (res)
2095 			goto err_close;
2096 	}
2097 
2098 	res = vlan_vids_add_by_dev(slave_dev, bond_dev);
2099 	if (res) {
2100 		slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n");
2101 		goto err_close;
2102 	}
2103 
2104 	prev_slave = bond_last_slave(bond);
2105 
2106 	new_slave->delay = 0;
2107 	new_slave->link_failure_count = 0;
2108 
2109 	if (bond_update_speed_duplex(new_slave) &&
2110 	    bond_needs_speed_duplex(bond))
2111 		new_slave->link = BOND_LINK_DOWN;
2112 
2113 	new_slave->last_rx = jiffies -
2114 		(msecs_to_jiffies(bond->params.arp_interval) + 1);
2115 	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
2116 		new_slave->target_last_arp_rx[i] = new_slave->last_rx;
2117 
2118 	new_slave->last_tx = new_slave->last_rx;
2119 
2120 	/* check for initial state */
2121 	new_slave->link = BOND_LINK_NOCHANGE;
2122 	if (bond->params.miimon) {
2123 		if (netif_carrier_ok(slave_dev)) {
2124 			if (bond->params.updelay) {
2125 				bond_set_slave_link_state(new_slave,
2126 							  BOND_LINK_BACK,
2127 							  BOND_SLAVE_NOTIFY_NOW);
2128 				new_slave->delay = bond->params.updelay;
2129 			} else {
2130 				bond_set_slave_link_state(new_slave,
2131 							  BOND_LINK_UP,
2132 							  BOND_SLAVE_NOTIFY_NOW);
2133 			}
2134 		} else {
2135 			bond_set_slave_link_state(new_slave, BOND_LINK_DOWN,
2136 						  BOND_SLAVE_NOTIFY_NOW);
2137 		}
2138 	} else if (bond->params.arp_interval) {
2139 		bond_set_slave_link_state(new_slave,
2140 					  (netif_carrier_ok(slave_dev) ?
2141 					  BOND_LINK_UP : BOND_LINK_DOWN),
2142 					  BOND_SLAVE_NOTIFY_NOW);
2143 	} else {
2144 		bond_set_slave_link_state(new_slave, BOND_LINK_UP,
2145 					  BOND_SLAVE_NOTIFY_NOW);
2146 	}
2147 
2148 	if (new_slave->link != BOND_LINK_DOWN)
2149 		new_slave->last_link_up = jiffies;
2150 	slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n",
2151 		  new_slave->link == BOND_LINK_DOWN ? "DOWN" :
2152 		  (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
2153 
2154 	if (bond_uses_primary(bond) && bond->params.primary[0]) {
2155 		/* if there is a primary slave, remember it */
2156 		if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
2157 			rcu_assign_pointer(bond->primary_slave, new_slave);
2158 			bond->force_primary = true;
2159 		}
2160 	}
2161 
2162 	switch (BOND_MODE(bond)) {
2163 	case BOND_MODE_ACTIVEBACKUP:
2164 		bond_set_slave_inactive_flags(new_slave,
2165 					      BOND_SLAVE_NOTIFY_NOW);
2166 		break;
2167 	case BOND_MODE_8023AD:
2168 		/* in 802.3ad mode, the internal mechanism
2169 		 * will activate the slaves in the selected
2170 		 * aggregator
2171 		 */
2172 		bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
2173 		/* if this is the first slave */
2174 		if (!prev_slave) {
2175 			SLAVE_AD_INFO(new_slave)->id = 1;
2176 			/* Initialize AD with the number of times that the AD timer is called in 1 second
2177 			 * can be called only after the mac address of the bond is set
2178 			 */
2179 			bond_3ad_initialize(bond);
2180 		} else {
2181 			SLAVE_AD_INFO(new_slave)->id =
2182 				SLAVE_AD_INFO(prev_slave)->id + 1;
2183 		}
2184 
2185 		bond_3ad_bind_slave(new_slave);
2186 		break;
2187 	case BOND_MODE_TLB:
2188 	case BOND_MODE_ALB:
2189 		bond_set_active_slave(new_slave);
2190 		bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
2191 		break;
2192 	default:
2193 		slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n");
2194 
2195 		/* always active in trunk mode */
2196 		bond_set_active_slave(new_slave);
2197 
2198 		/* In trunking mode there is little meaning to curr_active_slave
2199 		 * anyway (it holds no special properties of the bond device),
2200 		 * so we can change it without calling change_active_interface()
2201 		 */
2202 		if (!rcu_access_pointer(bond->curr_active_slave) &&
2203 		    new_slave->link == BOND_LINK_UP)
2204 			rcu_assign_pointer(bond->curr_active_slave, new_slave);
2205 
2206 		break;
2207 	} /* switch(bond_mode) */
2208 
2209 #ifdef CONFIG_NET_POLL_CONTROLLER
2210 	if (bond->dev->npinfo) {
2211 		if (slave_enable_netpoll(new_slave)) {
2212 			slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n");
2213 			res = -EBUSY;
2214 			goto err_detach;
2215 		}
2216 	}
2217 #endif
2218 
2219 	if (!(bond_dev->features & NETIF_F_LRO))
2220 		dev_disable_lro(slave_dev);
2221 
2222 	res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
2223 					 new_slave);
2224 	if (res) {
2225 		slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res);
2226 		goto err_detach;
2227 	}
2228 
2229 	res = bond_master_upper_dev_link(bond, new_slave, extack);
2230 	if (res) {
2231 		slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
2232 		goto err_unregister;
2233 	}
2234 
2235 	bond_lower_state_changed(new_slave);
2236 
2237 	res = bond_sysfs_slave_add(new_slave);
2238 	if (res) {
2239 		slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res);
2240 		goto err_upper_unlink;
2241 	}
2242 
2243 	/* If the mode uses primary, then the following is handled by
2244 	 * bond_change_active_slave().
2245 	 */
2246 	if (!bond_uses_primary(bond)) {
2247 		/* set promiscuity level to new slave */
2248 		if (bond_dev->flags & IFF_PROMISC) {
2249 			res = dev_set_promiscuity(slave_dev, 1);
2250 			if (res)
2251 				goto err_sysfs_del;
2252 		}
2253 
2254 		/* set allmulti level to new slave */
2255 		if (bond_dev->flags & IFF_ALLMULTI) {
2256 			res = dev_set_allmulti(slave_dev, 1);
2257 			if (res) {
2258 				if (bond_dev->flags & IFF_PROMISC)
2259 					dev_set_promiscuity(slave_dev, -1);
2260 				goto err_sysfs_del;
2261 			}
2262 		}
2263 
2264 		if (bond_dev->flags & IFF_UP) {
2265 			netif_addr_lock_bh(bond_dev);
2266 			dev_mc_sync_multiple(slave_dev, bond_dev);
2267 			dev_uc_sync_multiple(slave_dev, bond_dev);
2268 			netif_addr_unlock_bh(bond_dev);
2269 
2270 			if (BOND_MODE(bond) == BOND_MODE_8023AD)
2271 				dev_mc_add(slave_dev, lacpdu_mcast_addr);
2272 		}
2273 	}
2274 
2275 	bond->slave_cnt++;
2276 	bond_compute_features(bond);
2277 	bond_set_carrier(bond);
2278 
2279 	/* Needs to be called before bond_select_active_slave(), which will
2280 	 * remove the maddrs if the slave is selected as active slave.
2281 	 */
2282 	bond_slave_ns_maddrs_add(bond, new_slave);
2283 
2284 	if (bond_uses_primary(bond)) {
2285 		block_netpoll_tx();
2286 		bond_select_active_slave(bond);
2287 		unblock_netpoll_tx();
2288 	}
2289 
2290 	/* broadcast mode uses the all_slaves to loop through slaves. */
2291 	if (bond_mode_can_use_xmit_hash(bond) ||
2292 	    BOND_MODE(bond) == BOND_MODE_BROADCAST)
2293 		bond_update_slave_arr(bond, NULL);
2294 
2295 	if (!slave_dev->netdev_ops->ndo_bpf ||
2296 	    !slave_dev->netdev_ops->ndo_xdp_xmit) {
2297 		if (bond->xdp_prog) {
2298 			SLAVE_NL_ERR(bond_dev, slave_dev, extack,
2299 				     "Slave does not support XDP");
2300 			res = -EOPNOTSUPP;
2301 			goto err_sysfs_del;
2302 		}
2303 	} else if (bond->xdp_prog) {
2304 		struct netdev_bpf xdp = {
2305 			.command = XDP_SETUP_PROG,
2306 			.flags   = 0,
2307 			.prog    = bond->xdp_prog,
2308 			.extack  = extack,
2309 		};
2310 
2311 		if (dev_xdp_prog_count(slave_dev) > 0) {
2312 			SLAVE_NL_ERR(bond_dev, slave_dev, extack,
2313 				     "Slave has XDP program loaded, please unload before enslaving");
2314 			res = -EOPNOTSUPP;
2315 			goto err_sysfs_del;
2316 		}
2317 
2318 		res = dev_xdp_propagate(slave_dev, &xdp);
2319 		if (res < 0) {
2320 			/* ndo_bpf() sets extack error message */
2321 			slave_dbg(bond_dev, slave_dev, "Error %d calling ndo_bpf\n", res);
2322 			goto err_sysfs_del;
2323 		}
2324 		if (bond->xdp_prog)
2325 			bpf_prog_inc(bond->xdp_prog);
2326 	}
2327 
2328 	bond_xdp_set_features(bond_dev);
2329 
2330 	slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
2331 		   bond_is_active_slave(new_slave) ? "an active" : "a backup",
2332 		   new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");
2333 
2334 	/* enslave is successful */
2335 	bond_queue_slave_event(new_slave);
2336 	return 0;
2337 
2338 /* Undo stages on error */
2339 err_sysfs_del:
2340 	bond_sysfs_slave_del(new_slave);
2341 
2342 err_upper_unlink:
2343 	bond_upper_dev_unlink(bond, new_slave);
2344 
2345 err_unregister:
2346 	netdev_rx_handler_unregister(slave_dev);
2347 
2348 err_detach:
2349 	vlan_vids_del_by_dev(slave_dev, bond_dev);
2350 	if (rcu_access_pointer(bond->primary_slave) == new_slave)
2351 		RCU_INIT_POINTER(bond->primary_slave, NULL);
2352 	if (rcu_access_pointer(bond->curr_active_slave) == new_slave) {
2353 		block_netpoll_tx();
2354 		bond_change_active_slave(bond, NULL);
2355 		bond_select_active_slave(bond);
2356 		unblock_netpoll_tx();
2357 	}
2358 	/* either primary_slave or curr_active_slave might've changed */
2359 	synchronize_rcu();
2360 	slave_disable_netpoll(new_slave);
2361 
2362 err_close:
2363 	if (!netif_is_bond_master(slave_dev))
2364 		slave_dev->priv_flags &= ~IFF_BONDING;
2365 	dev_close(slave_dev);
2366 
2367 err_restore_mac:
2368 	slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
2369 	if (!bond->params.fail_over_mac ||
2370 	    BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2371 		/* XXX TODO - fom follow mode needs to change master's
2372 		 * MAC if this slave's MAC is in use by the bond, or at
2373 		 * least print a warning.
2374 		 */
2375 		bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr,
2376 				  new_slave->dev->addr_len);
2377 		ss.ss_family = slave_dev->type;
2378 		dev_set_mac_address(slave_dev, &ss, NULL);
2379 	}
2380 
2381 err_restore_mtu:
2382 	dev_set_mtu(slave_dev, new_slave->original_mtu);
2383 
2384 err_free:
2385 	kobject_put(&new_slave->kobj);
2386 
2387 err_undo_flags:
2388 	/* Enslave of first slave has failed and we need to fix master's mac */
2389 	if (!bond_has_slaves(bond)) {
2390 		if (ether_addr_equal_64bits(bond_dev->dev_addr,
2391 					    slave_dev->dev_addr))
2392 			eth_hw_addr_random(bond_dev);
2393 		if (bond_dev->type != ARPHRD_ETHER) {
2394 			dev_close(bond_dev);
2395 			bond_ether_setup(bond_dev);
2396 		}
2397 	}
2398 
2399 	return res;
2400 }
2401 
2402 /* Try to release the slave device <slave> from the bond device <master>
2403  * It is legal to access curr_active_slave without a lock because all the function
2404  * is RTNL-locked. If "all" is true it means that the function is being called
2405  * while destroying a bond interface and all slaves are being released.
2406  *
2407  * The rules for slave state should be:
2408  *   for Active/Backup:
2409  *     Active stays on all backups go down
2410  *   for Bonded connections:
2411  *     The first up interface should be left on and all others downed.
2412  */
2413 static int __bond_release_one(struct net_device *bond_dev,
2414 			      struct net_device *slave_dev,
2415 			      bool all, bool unregister)
2416 {
2417 	struct bonding *bond = netdev_priv(bond_dev);
2418 	struct slave *slave, *oldcurrent;
2419 	struct sockaddr_storage ss;
2420 	int old_flags = bond_dev->flags;
2421 	netdev_features_t old_features = bond_dev->features;
2422 
2423 	/* slave is not a slave or master is not master of this slave */
2424 	if (!(slave_dev->flags & IFF_SLAVE) ||
2425 	    !netdev_has_upper_dev(slave_dev, bond_dev)) {
2426 		slave_dbg(bond_dev, slave_dev, "cannot release slave\n");
2427 		return -EINVAL;
2428 	}
2429 
2430 	block_netpoll_tx();
2431 
2432 	slave = bond_get_slave_by_dev(bond, slave_dev);
2433 	if (!slave) {
2434 		/* not a slave of this bond */
2435 		slave_info(bond_dev, slave_dev, "interface not enslaved\n");
2436 		unblock_netpoll_tx();
2437 		return -EINVAL;
2438 	}
2439 
2440 	bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW);
2441 
2442 	bond_sysfs_slave_del(slave);
2443 
2444 	/* recompute stats just before removing the slave */
2445 	bond_get_stats(bond->dev, &bond->bond_stats);
2446 
2447 	if (bond->xdp_prog) {
2448 		struct netdev_bpf xdp = {
2449 			.command = XDP_SETUP_PROG,
2450 			.flags   = 0,
2451 			.prog	 = NULL,
2452 			.extack  = NULL,
2453 		};
2454 		if (dev_xdp_propagate(slave_dev, &xdp))
2455 			slave_warn(bond_dev, slave_dev, "failed to unload XDP program\n");
2456 	}
2457 
2458 	/* unregister rx_handler early so bond_handle_frame wouldn't be called
2459 	 * for this slave anymore.
2460 	 */
2461 	netdev_rx_handler_unregister(slave_dev);
2462 
2463 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
2464 		bond_3ad_unbind_slave(slave);
2465 
2466 	bond_upper_dev_unlink(bond, slave);
2467 
2468 	if (bond_mode_can_use_xmit_hash(bond) ||
2469 	    BOND_MODE(bond) == BOND_MODE_BROADCAST)
2470 		bond_update_slave_arr(bond, slave);
2471 
2472 	slave_info(bond_dev, slave_dev, "Releasing %s interface\n",
2473 		    bond_is_active_slave(slave) ? "active" : "backup");
2474 
2475 	oldcurrent = rcu_access_pointer(bond->curr_active_slave);
2476 
2477 	RCU_INIT_POINTER(bond->current_arp_slave, NULL);
2478 
2479 	if (!all && (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
2480 		     BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) {
2481 		if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) &&
2482 		    bond_has_slaves(bond))
2483 			slave_warn(bond_dev, slave_dev, "the permanent HWaddr of slave - %pM - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts\n",
2484 				   slave->perm_hwaddr);
2485 	}
2486 
2487 	if (rtnl_dereference(bond->primary_slave) == slave)
2488 		RCU_INIT_POINTER(bond->primary_slave, NULL);
2489 
2490 	if (oldcurrent == slave)
2491 		bond_change_active_slave(bond, NULL);
2492 
2493 	/* Must be called after bond_change_active_slave () as the slave
2494 	 * might change from an active slave to a backup slave. Then it is
2495 	 * necessary to clear the maddrs on the backup slave.
2496 	 */
2497 	bond_slave_ns_maddrs_del(bond, slave);
2498 
2499 	if (bond_is_lb(bond)) {
2500 		/* Must be called only after the slave has been
2501 		 * detached from the list and the curr_active_slave
2502 		 * has been cleared (if our_slave == old_current),
2503 		 * but before a new active slave is selected.
2504 		 */
2505 		bond_alb_deinit_slave(bond, slave);
2506 	}
2507 
2508 	if (all) {
2509 		RCU_INIT_POINTER(bond->curr_active_slave, NULL);
2510 	} else if (oldcurrent == slave) {
2511 		/* Note that we hold RTNL over this sequence, so there
2512 		 * is no concern that another slave add/remove event
2513 		 * will interfere.
2514 		 */
2515 		bond_select_active_slave(bond);
2516 	}
2517 
2518 	bond_set_carrier(bond);
2519 	if (!bond_has_slaves(bond))
2520 		eth_hw_addr_random(bond_dev);
2521 
2522 	unblock_netpoll_tx();
2523 	synchronize_rcu();
2524 	bond->slave_cnt--;
2525 
2526 	if (!bond_has_slaves(bond)) {
2527 		call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
2528 		call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
2529 	}
2530 
2531 	bond_compute_features(bond);
2532 	if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2533 	    (old_features & NETIF_F_VLAN_CHALLENGED))
2534 		slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n");
2535 
2536 	vlan_vids_del_by_dev(slave_dev, bond_dev);
2537 
2538 	/* If the mode uses primary, then this case was handled above by
2539 	 * bond_change_active_slave(..., NULL)
2540 	 */
2541 	if (!bond_uses_primary(bond)) {
2542 		/* unset promiscuity level from slave
2543 		 * NOTE: The NETDEV_CHANGEADDR call above may change the value
2544 		 * of the IFF_PROMISC flag in the bond_dev, but we need the
2545 		 * value of that flag before that change, as that was the value
2546 		 * when this slave was attached, so we cache at the start of the
2547 		 * function and use it here. Same goes for ALLMULTI below
2548 		 */
2549 		if (old_flags & IFF_PROMISC)
2550 			dev_set_promiscuity(slave_dev, -1);
2551 
2552 		/* unset allmulti level from slave */
2553 		if (old_flags & IFF_ALLMULTI)
2554 			dev_set_allmulti(slave_dev, -1);
2555 
2556 		if (old_flags & IFF_UP)
2557 			bond_hw_addr_flush(bond_dev, slave_dev);
2558 	}
2559 
2560 	slave_disable_netpoll(slave);
2561 
2562 	/* close slave before restoring its mac address */
2563 	dev_close(slave_dev);
2564 
2565 	slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
2566 
2567 	if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
2568 	    BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2569 		/* restore original ("permanent") mac address */
2570 		bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
2571 				  slave->dev->addr_len);
2572 		ss.ss_family = slave_dev->type;
2573 		dev_set_mac_address(slave_dev, &ss, NULL);
2574 	}
2575 
2576 	if (unregister) {
2577 		netdev_lock_ops(slave_dev);
2578 		__netif_set_mtu(slave_dev, slave->original_mtu);
2579 		netdev_unlock_ops(slave_dev);
2580 	} else {
2581 		dev_set_mtu(slave_dev, slave->original_mtu);
2582 	}
2583 
2584 	if (!netif_is_bond_master(slave_dev))
2585 		slave_dev->priv_flags &= ~IFF_BONDING;
2586 
2587 	bond_xdp_set_features(bond_dev);
2588 	kobject_put(&slave->kobj);
2589 
2590 	return 0;
2591 }
2592 
2593 /* A wrapper used because of ndo_del_link */
2594 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
2595 {
2596 	return __bond_release_one(bond_dev, slave_dev, false, false);
2597 }
2598 
2599 /* First release a slave and then destroy the bond if no more slaves are left.
2600  * Must be under rtnl_lock when this function is called.
2601  */
2602 static int bond_release_and_destroy(struct net_device *bond_dev,
2603 				    struct net_device *slave_dev)
2604 {
2605 	struct bonding *bond = netdev_priv(bond_dev);
2606 	int ret;
2607 
2608 	ret = __bond_release_one(bond_dev, slave_dev, false, true);
2609 	if (ret == 0 && !bond_has_slaves(bond) &&
2610 	    bond_dev->reg_state != NETREG_UNREGISTERING) {
2611 		bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2612 		netdev_info(bond_dev, "Destroying bond\n");
2613 		bond_remove_proc_entry(bond);
2614 		unregister_netdevice(bond_dev);
2615 	}
2616 	return ret;
2617 }
2618 
2619 static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2620 {
2621 	struct bonding *bond = netdev_priv(bond_dev);
2622 
2623 	bond_fill_ifbond(bond, info);
2624 }
2625 
2626 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2627 {
2628 	struct bonding *bond = netdev_priv(bond_dev);
2629 	struct list_head *iter;
2630 	int i = 0, res = -ENODEV;
2631 	struct slave *slave;
2632 
2633 	bond_for_each_slave(bond, slave, iter) {
2634 		if (i++ == (int)info->slave_id) {
2635 			res = 0;
2636 			bond_fill_ifslave(slave, info);
2637 			break;
2638 		}
2639 	}
2640 
2641 	return res;
2642 }
2643 
2644 /*-------------------------------- Monitoring -------------------------------*/
2645 
2646 /* called with rcu_read_lock() */
2647 static int bond_miimon_inspect(struct bonding *bond)
2648 {
2649 	bool ignore_updelay = false;
2650 	int link_state, commit = 0;
2651 	struct list_head *iter;
2652 	struct slave *slave;
2653 
2654 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
2655 		ignore_updelay = !rcu_dereference(bond->curr_active_slave);
2656 	} else {
2657 		struct bond_up_slave *usable_slaves;
2658 
2659 		usable_slaves = rcu_dereference(bond->usable_slaves);
2660 
2661 		if (usable_slaves && usable_slaves->count == 0)
2662 			ignore_updelay = true;
2663 	}
2664 
2665 	bond_for_each_slave_rcu(bond, slave, iter) {
2666 		bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2667 
2668 		link_state = netif_carrier_ok(slave->dev);
2669 
2670 		switch (slave->link) {
2671 		case BOND_LINK_UP:
2672 			if (link_state)
2673 				continue;
2674 
2675 			bond_propose_link_state(slave, BOND_LINK_FAIL);
2676 			commit++;
2677 			slave->delay = bond->params.downdelay;
2678 			if (slave->delay && net_ratelimit()) {
2679 				slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
2680 					   (BOND_MODE(bond) ==
2681 					    BOND_MODE_ACTIVEBACKUP) ?
2682 					    (bond_is_active_slave(slave) ?
2683 					     "active " : "backup ") : "",
2684 					   bond->params.downdelay * bond->params.miimon);
2685 			}
2686 			fallthrough;
2687 		case BOND_LINK_FAIL:
2688 			if (link_state) {
2689 				/* recovered before downdelay expired */
2690 				bond_propose_link_state(slave, BOND_LINK_UP);
2691 				slave->last_link_up = jiffies;
2692 				if (net_ratelimit())
2693 					slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
2694 						   (bond->params.downdelay - slave->delay) *
2695 						   bond->params.miimon);
2696 				commit++;
2697 				continue;
2698 			}
2699 
2700 			if (slave->delay <= 0) {
2701 				bond_propose_link_state(slave, BOND_LINK_DOWN);
2702 				commit++;
2703 				continue;
2704 			}
2705 
2706 			slave->delay--;
2707 			break;
2708 
2709 		case BOND_LINK_DOWN:
2710 			if (!link_state)
2711 				continue;
2712 
2713 			bond_propose_link_state(slave, BOND_LINK_BACK);
2714 			commit++;
2715 			slave->delay = bond->params.updelay;
2716 
2717 			if (slave->delay && net_ratelimit()) {
2718 				slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
2719 					   ignore_updelay ? 0 :
2720 					   bond->params.updelay *
2721 					   bond->params.miimon);
2722 			}
2723 			fallthrough;
2724 		case BOND_LINK_BACK:
2725 			if (!link_state) {
2726 				bond_propose_link_state(slave, BOND_LINK_DOWN);
2727 				if (net_ratelimit())
2728 					slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
2729 						   (bond->params.updelay - slave->delay) *
2730 						   bond->params.miimon);
2731 				commit++;
2732 				continue;
2733 			}
2734 
2735 			if (ignore_updelay)
2736 				slave->delay = 0;
2737 
2738 			if (slave->delay <= 0) {
2739 				bond_propose_link_state(slave, BOND_LINK_UP);
2740 				commit++;
2741 				ignore_updelay = false;
2742 				continue;
2743 			}
2744 
2745 			slave->delay--;
2746 			break;
2747 		}
2748 	}
2749 
2750 	return commit;
2751 }
2752 
2753 static void bond_miimon_link_change(struct bonding *bond,
2754 				    struct slave *slave,
2755 				    char link)
2756 {
2757 	switch (BOND_MODE(bond)) {
2758 	case BOND_MODE_8023AD:
2759 		bond_3ad_handle_link_change(slave, link);
2760 		break;
2761 	case BOND_MODE_TLB:
2762 	case BOND_MODE_ALB:
2763 		bond_alb_handle_link_change(bond, slave, link);
2764 		break;
2765 	case BOND_MODE_XOR:
2766 		bond_update_slave_arr(bond, NULL);
2767 		break;
2768 	}
2769 }
2770 
2771 static void bond_miimon_commit(struct bonding *bond)
2772 {
2773 	struct slave *slave, *primary, *active;
2774 	bool do_failover = false;
2775 	struct list_head *iter;
2776 
2777 	ASSERT_RTNL();
2778 
2779 	bond_for_each_slave(bond, slave, iter) {
2780 		switch (slave->link_new_state) {
2781 		case BOND_LINK_NOCHANGE:
2782 			/* For 802.3ad mode, check current slave speed and
2783 			 * duplex again in case its port was disabled after
2784 			 * invalid speed/duplex reporting but recovered before
2785 			 * link monitoring could make a decision on the actual
2786 			 * link status
2787 			 */
2788 			if (BOND_MODE(bond) == BOND_MODE_8023AD &&
2789 			    slave->link == BOND_LINK_UP)
2790 				bond_3ad_adapter_speed_duplex_changed(slave);
2791 			continue;
2792 
2793 		case BOND_LINK_UP:
2794 			if (bond_update_speed_duplex(slave) &&
2795 			    bond_needs_speed_duplex(bond)) {
2796 				slave->link = BOND_LINK_DOWN;
2797 				if (net_ratelimit())
2798 					slave_warn(bond->dev, slave->dev,
2799 						   "failed to get link speed/duplex\n");
2800 				continue;
2801 			}
2802 			bond_set_slave_link_state(slave, BOND_LINK_UP,
2803 						  BOND_SLAVE_NOTIFY_NOW);
2804 			slave->last_link_up = jiffies;
2805 
2806 			primary = rtnl_dereference(bond->primary_slave);
2807 			if (BOND_MODE(bond) == BOND_MODE_8023AD) {
2808 				/* prevent it from being the active one */
2809 				bond_set_backup_slave(slave);
2810 			} else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2811 				/* make it immediately active */
2812 				bond_set_active_slave(slave);
2813 			}
2814 
2815 			slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
2816 				   slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
2817 				   slave->duplex ? "full" : "half");
2818 
2819 			bond_miimon_link_change(bond, slave, BOND_LINK_UP);
2820 
2821 			active = rtnl_dereference(bond->curr_active_slave);
2822 			if (!active || slave == primary || slave->prio > active->prio)
2823 				do_failover = true;
2824 
2825 			continue;
2826 
2827 		case BOND_LINK_DOWN:
2828 			if (slave->link_failure_count < UINT_MAX)
2829 				slave->link_failure_count++;
2830 
2831 			bond_set_slave_link_state(slave, BOND_LINK_DOWN,
2832 						  BOND_SLAVE_NOTIFY_NOW);
2833 
2834 			if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
2835 			    BOND_MODE(bond) == BOND_MODE_8023AD)
2836 				bond_set_slave_inactive_flags(slave,
2837 							      BOND_SLAVE_NOTIFY_NOW);
2838 
2839 			slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
2840 
2841 			bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
2842 
2843 			if (slave == rcu_access_pointer(bond->curr_active_slave))
2844 				do_failover = true;
2845 
2846 			continue;
2847 
2848 		default:
2849 			slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n",
2850 				  slave->link_new_state);
2851 			bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2852 
2853 			continue;
2854 		}
2855 	}
2856 
2857 	if (do_failover) {
2858 		block_netpoll_tx();
2859 		bond_select_active_slave(bond);
2860 		unblock_netpoll_tx();
2861 	}
2862 
2863 	bond_set_carrier(bond);
2864 }
2865 
2866 /* bond_mii_monitor
2867  *
2868  * Really a wrapper that splits the mii monitor into two phases: an
2869  * inspection, then (if inspection indicates something needs to be done)
2870  * an acquisition of appropriate locks followed by a commit phase to
2871  * implement whatever link state changes are indicated.
2872  */
2873 static void bond_mii_monitor(struct work_struct *work)
2874 {
2875 	struct bonding *bond = container_of(work, struct bonding,
2876 					    mii_work.work);
2877 	bool should_notify_peers;
2878 	bool commit;
2879 	unsigned long delay;
2880 	struct slave *slave;
2881 	struct list_head *iter;
2882 
2883 	delay = msecs_to_jiffies(bond->params.miimon);
2884 
2885 	if (!bond_has_slaves(bond))
2886 		goto re_arm;
2887 
2888 	rcu_read_lock();
2889 
2890 	should_notify_peers = bond_should_notify_peers(bond);
2891 	commit = !!bond_miimon_inspect(bond);
2892 
2893 	rcu_read_unlock();
2894 
2895 	if (commit || bond->send_peer_notif) {
2896 		/* Race avoidance with bond_close cancel of workqueue */
2897 		if (!rtnl_trylock()) {
2898 			delay = 1;
2899 			goto re_arm;
2900 		}
2901 
2902 		if (commit) {
2903 			bond_for_each_slave(bond, slave, iter) {
2904 				bond_commit_link_state(slave,
2905 						       BOND_SLAVE_NOTIFY_LATER);
2906 			}
2907 			bond_miimon_commit(bond);
2908 		}
2909 
2910 		if (bond->send_peer_notif) {
2911 			bond->send_peer_notif--;
2912 			if (should_notify_peers)
2913 				call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
2914 							 bond->dev);
2915 		}
2916 
2917 		rtnl_unlock();	/* might sleep, hold no other locks */
2918 	}
2919 
2920 re_arm:
2921 	if (bond->params.miimon)
2922 		queue_delayed_work(bond->wq, &bond->mii_work, delay);
2923 }
2924 
2925 static int bond_upper_dev_walk(struct net_device *upper,
2926 			       struct netdev_nested_priv *priv)
2927 {
2928 	__be32 ip = *(__be32 *)priv->data;
2929 
2930 	return ip == bond_confirm_addr(upper, 0, ip);
2931 }
2932 
2933 static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
2934 {
2935 	struct netdev_nested_priv priv = {
2936 		.data = (void *)&ip,
2937 	};
2938 	bool ret = false;
2939 
2940 	if (ip == bond_confirm_addr(bond->dev, 0, ip))
2941 		return true;
2942 
2943 	rcu_read_lock();
2944 	if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv))
2945 		ret = true;
2946 	rcu_read_unlock();
2947 
2948 	return ret;
2949 }
2950 
2951 #define BOND_VLAN_PROTO_NONE cpu_to_be16(0xffff)
2952 
2953 static bool bond_handle_vlan(struct slave *slave, struct bond_vlan_tag *tags,
2954 			     struct sk_buff *skb)
2955 {
2956 	struct net_device *bond_dev = slave->bond->dev;
2957 	struct net_device *slave_dev = slave->dev;
2958 	struct bond_vlan_tag *outer_tag = tags;
2959 
2960 	if (!tags || tags->vlan_proto == BOND_VLAN_PROTO_NONE)
2961 		return true;
2962 
2963 	tags++;
2964 
2965 	/* Go through all the tags backwards and add them to the packet */
2966 	while (tags->vlan_proto != BOND_VLAN_PROTO_NONE) {
2967 		if (!tags->vlan_id) {
2968 			tags++;
2969 			continue;
2970 		}
2971 
2972 		slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
2973 			  ntohs(outer_tag->vlan_proto), tags->vlan_id);
2974 		skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
2975 						tags->vlan_id);
2976 		if (!skb) {
2977 			net_err_ratelimited("failed to insert inner VLAN tag\n");
2978 			return false;
2979 		}
2980 
2981 		tags++;
2982 	}
2983 	/* Set the outer tag */
2984 	if (outer_tag->vlan_id) {
2985 		slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
2986 			  ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
2987 		__vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
2988 				       outer_tag->vlan_id);
2989 	}
2990 
2991 	return true;
2992 }
2993 
2994 /* We go to the (large) trouble of VLAN tagging ARP frames because
2995  * switches in VLAN mode (especially if ports are configured as
2996  * "native" to a VLAN) might not pass non-tagged frames.
2997  */
2998 static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
2999 			  __be32 src_ip, struct bond_vlan_tag *tags)
3000 {
3001 	struct net_device *bond_dev = slave->bond->dev;
3002 	struct net_device *slave_dev = slave->dev;
3003 	struct sk_buff *skb;
3004 
3005 	slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
3006 		  arp_op, &dest_ip, &src_ip);
3007 
3008 	skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
3009 			 NULL, slave_dev->dev_addr, NULL);
3010 
3011 	if (!skb) {
3012 		net_err_ratelimited("ARP packet allocation failed\n");
3013 		return;
3014 	}
3015 
3016 	if (bond_handle_vlan(slave, tags, skb)) {
3017 		slave_update_last_tx(slave);
3018 		arp_xmit(skb);
3019 	}
3020 
3021 	return;
3022 }
3023 
3024 /* Validate the device path between the @start_dev and the @end_dev.
3025  * The path is valid if the @end_dev is reachable through device
3026  * stacking.
3027  * When the path is validated, collect any vlan information in the
3028  * path.
3029  */
3030 struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
3031 					      struct net_device *end_dev,
3032 					      int level)
3033 {
3034 	struct bond_vlan_tag *tags;
3035 	struct net_device *upper;
3036 	struct list_head  *iter;
3037 
3038 	if (start_dev == end_dev) {
3039 		tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC);
3040 		if (!tags)
3041 			return ERR_PTR(-ENOMEM);
3042 		tags[level].vlan_proto = BOND_VLAN_PROTO_NONE;
3043 		return tags;
3044 	}
3045 
3046 	netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
3047 		tags = bond_verify_device_path(upper, end_dev, level + 1);
3048 		if (IS_ERR_OR_NULL(tags)) {
3049 			if (IS_ERR(tags))
3050 				return tags;
3051 			continue;
3052 		}
3053 		if (is_vlan_dev(upper)) {
3054 			tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
3055 			tags[level].vlan_id = vlan_dev_vlan_id(upper);
3056 		}
3057 
3058 		return tags;
3059 	}
3060 
3061 	return NULL;
3062 }
3063 
3064 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
3065 {
3066 	struct rtable *rt;
3067 	struct bond_vlan_tag *tags;
3068 	__be32 *targets = bond->params.arp_targets, addr;
3069 	int i;
3070 
3071 	for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
3072 		slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
3073 			  __func__, &targets[i]);
3074 		tags = NULL;
3075 
3076 		/* Find out through which dev should the packet go */
3077 		rt = ip_route_output(dev_net(bond->dev), targets[i], 0, 0, 0,
3078 				     RT_SCOPE_LINK);
3079 		if (IS_ERR(rt)) {
3080 			/* there's no route to target - try to send arp
3081 			 * probe to generate any traffic (arp_validate=0)
3082 			 */
3083 			if (bond->params.arp_validate)
3084 				pr_warn_once("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
3085 					     bond->dev->name,
3086 					     &targets[i]);
3087 			bond_arp_send(slave, ARPOP_REQUEST, targets[i],
3088 				      0, tags);
3089 			continue;
3090 		}
3091 
3092 		/* bond device itself */
3093 		if (rt->dst.dev == bond->dev)
3094 			goto found;
3095 
3096 		rcu_read_lock();
3097 		tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
3098 		rcu_read_unlock();
3099 
3100 		if (!IS_ERR_OR_NULL(tags))
3101 			goto found;
3102 
3103 		/* Not our device - skip */
3104 		slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n",
3105 			   &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
3106 
3107 		ip_rt_put(rt);
3108 		continue;
3109 
3110 found:
3111 		addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
3112 		ip_rt_put(rt);
3113 		bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
3114 		kfree(tags);
3115 	}
3116 }
3117 
3118 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
3119 {
3120 	int i;
3121 
3122 	if (!sip || !bond_has_this_ip(bond, tip)) {
3123 		slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
3124 			   __func__, &sip, &tip);
3125 		return;
3126 	}
3127 
3128 	i = bond_get_targets_ip(bond->params.arp_targets, sip);
3129 	if (i == -1) {
3130 		slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
3131 			   __func__, &sip);
3132 		return;
3133 	}
3134 	slave->last_rx = jiffies;
3135 	slave->target_last_arp_rx[i] = jiffies;
3136 }
3137 
3138 static int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
3139 			struct slave *slave)
3140 {
3141 	struct arphdr *arp = (struct arphdr *)skb->data;
3142 	struct slave *curr_active_slave, *curr_arp_slave;
3143 	unsigned char *arp_ptr;
3144 	__be32 sip, tip;
3145 	unsigned int alen;
3146 
3147 	alen = arp_hdr_len(bond->dev);
3148 
3149 	if (alen > skb_headlen(skb)) {
3150 		arp = kmalloc(alen, GFP_ATOMIC);
3151 		if (!arp)
3152 			goto out_unlock;
3153 		if (skb_copy_bits(skb, 0, arp, alen) < 0)
3154 			goto out_unlock;
3155 	}
3156 
3157 	if (arp->ar_hln != bond->dev->addr_len ||
3158 	    skb->pkt_type == PACKET_OTHERHOST ||
3159 	    skb->pkt_type == PACKET_LOOPBACK ||
3160 	    arp->ar_hrd != htons(ARPHRD_ETHER) ||
3161 	    arp->ar_pro != htons(ETH_P_IP) ||
3162 	    arp->ar_pln != 4)
3163 		goto out_unlock;
3164 
3165 	arp_ptr = (unsigned char *)(arp + 1);
3166 	arp_ptr += bond->dev->addr_len;
3167 	memcpy(&sip, arp_ptr, 4);
3168 	arp_ptr += 4 + bond->dev->addr_len;
3169 	memcpy(&tip, arp_ptr, 4);
3170 
3171 	slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
3172 		  __func__, slave->dev->name, bond_slave_state(slave),
3173 		  bond->params.arp_validate, slave_do_arp_validate(bond, slave),
3174 		  &sip, &tip);
3175 
3176 	curr_active_slave = rcu_dereference(bond->curr_active_slave);
3177 	curr_arp_slave = rcu_dereference(bond->current_arp_slave);
3178 
3179 	/* We 'trust' the received ARP enough to validate it if:
3180 	 *
3181 	 * (a) the slave receiving the ARP is active (which includes the
3182 	 * current ARP slave, if any), or
3183 	 *
3184 	 * (b) the receiving slave isn't active, but there is a currently
3185 	 * active slave and it received valid arp reply(s) after it became
3186 	 * the currently active slave, or
3187 	 *
3188 	 * (c) there is an ARP slave that sent an ARP during the prior ARP
3189 	 * interval, and we receive an ARP reply on any slave.  We accept
3190 	 * these because switch FDB update delays may deliver the ARP
3191 	 * reply to a slave other than the sender of the ARP request.
3192 	 *
3193 	 * Note: for (b), backup slaves are receiving the broadcast ARP
3194 	 * request, not a reply.  This request passes from the sending
3195 	 * slave through the L2 switch(es) to the receiving slave.  Since
3196 	 * this is checking the request, sip/tip are swapped for
3197 	 * validation.
3198 	 *
3199 	 * This is done to avoid endless looping when we can't reach the
3200 	 * arp_ip_target and fool ourselves with our own arp requests.
3201 	 */
3202 	if (bond_is_active_slave(slave))
3203 		bond_validate_arp(bond, slave, sip, tip);
3204 	else if (curr_active_slave &&
3205 		 time_after(slave_last_rx(bond, curr_active_slave),
3206 			    curr_active_slave->last_link_up))
3207 		bond_validate_arp(bond, slave, tip, sip);
3208 	else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
3209 		 bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
3210 		bond_validate_arp(bond, slave, sip, tip);
3211 
3212 out_unlock:
3213 	if (arp != (struct arphdr *)skb->data)
3214 		kfree(arp);
3215 	return RX_HANDLER_ANOTHER;
3216 }
3217 
3218 #if IS_ENABLED(CONFIG_IPV6)
3219 static void bond_ns_send(struct slave *slave, const struct in6_addr *daddr,
3220 			 const struct in6_addr *saddr, struct bond_vlan_tag *tags)
3221 {
3222 	struct net_device *bond_dev = slave->bond->dev;
3223 	struct net_device *slave_dev = slave->dev;
3224 	struct in6_addr mcaddr;
3225 	struct sk_buff *skb;
3226 
3227 	slave_dbg(bond_dev, slave_dev, "NS on slave: dst %pI6c src %pI6c\n",
3228 		  daddr, saddr);
3229 
3230 	skb = ndisc_ns_create(slave_dev, daddr, saddr, 0);
3231 	if (!skb) {
3232 		net_err_ratelimited("NS packet allocation failed\n");
3233 		return;
3234 	}
3235 
3236 	addrconf_addr_solict_mult(daddr, &mcaddr);
3237 	if (bond_handle_vlan(slave, tags, skb)) {
3238 		slave_update_last_tx(slave);
3239 		ndisc_send_skb(skb, &mcaddr, saddr);
3240 	}
3241 }
3242 
3243 static void bond_ns_send_all(struct bonding *bond, struct slave *slave)
3244 {
3245 	struct in6_addr *targets = bond->params.ns_targets;
3246 	struct bond_vlan_tag *tags;
3247 	struct dst_entry *dst;
3248 	struct in6_addr saddr;
3249 	struct flowi6 fl6;
3250 	int i;
3251 
3252 	for (i = 0; i < BOND_MAX_NS_TARGETS && !ipv6_addr_any(&targets[i]); i++) {
3253 		slave_dbg(bond->dev, slave->dev, "%s: target %pI6c\n",
3254 			  __func__, &targets[i]);
3255 		tags = NULL;
3256 
3257 		/* Find out through which dev should the packet go */
3258 		memset(&fl6, 0, sizeof(struct flowi6));
3259 		fl6.daddr = targets[i];
3260 
3261 		dst = ip6_route_output(dev_net(bond->dev), NULL, &fl6);
3262 		if (dst->error) {
3263 			dst_release(dst);
3264 			/* there's no route to target - try to send arp
3265 			 * probe to generate any traffic (arp_validate=0)
3266 			 */
3267 			if (bond->params.arp_validate)
3268 				pr_warn_once("%s: no route to ns_ip6_target %pI6c and arp_validate is set\n",
3269 					     bond->dev->name,
3270 					     &targets[i]);
3271 			bond_ns_send(slave, &targets[i], &in6addr_any, tags);
3272 			continue;
3273 		}
3274 
3275 		/* bond device itself */
3276 		if (dst->dev == bond->dev)
3277 			goto found;
3278 
3279 		rcu_read_lock();
3280 		tags = bond_verify_device_path(bond->dev, dst->dev, 0);
3281 		rcu_read_unlock();
3282 
3283 		if (!IS_ERR_OR_NULL(tags))
3284 			goto found;
3285 
3286 		/* Not our device - skip */
3287 		slave_dbg(bond->dev, slave->dev, "no path to ns_ip6_target %pI6c via dst->dev %s\n",
3288 			  &targets[i], dst->dev ? dst->dev->name : "NULL");
3289 
3290 		dst_release(dst);
3291 		continue;
3292 
3293 found:
3294 		if (!ipv6_dev_get_saddr(dev_net(dst->dev), dst->dev, &targets[i], 0, &saddr))
3295 			bond_ns_send(slave, &targets[i], &saddr, tags);
3296 		else
3297 			bond_ns_send(slave, &targets[i], &in6addr_any, tags);
3298 
3299 		dst_release(dst);
3300 		kfree(tags);
3301 	}
3302 }
3303 
3304 static int bond_confirm_addr6(struct net_device *dev,
3305 			      struct netdev_nested_priv *priv)
3306 {
3307 	struct in6_addr *addr = (struct in6_addr *)priv->data;
3308 
3309 	return ipv6_chk_addr(dev_net(dev), addr, dev, 0);
3310 }
3311 
3312 static bool bond_has_this_ip6(struct bonding *bond, struct in6_addr *addr)
3313 {
3314 	struct netdev_nested_priv priv = {
3315 		.data = addr,
3316 	};
3317 	int ret = false;
3318 
3319 	if (bond_confirm_addr6(bond->dev, &priv))
3320 		return true;
3321 
3322 	rcu_read_lock();
3323 	if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_confirm_addr6, &priv))
3324 		ret = true;
3325 	rcu_read_unlock();
3326 
3327 	return ret;
3328 }
3329 
3330 static void bond_validate_na(struct bonding *bond, struct slave *slave,
3331 			     struct in6_addr *saddr, struct in6_addr *daddr)
3332 {
3333 	int i;
3334 
3335 	/* Ignore NAs that:
3336 	 * 1. Source address is unspecified address.
3337 	 * 2. Dest address is neither all-nodes multicast address nor
3338 	 *    exist on bond interface.
3339 	 */
3340 	if (ipv6_addr_any(saddr) ||
3341 	    (!ipv6_addr_equal(daddr, &in6addr_linklocal_allnodes) &&
3342 	     !bond_has_this_ip6(bond, daddr))) {
3343 		slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c tip %pI6c not found\n",
3344 			  __func__, saddr, daddr);
3345 		return;
3346 	}
3347 
3348 	i = bond_get_targets_ip6(bond->params.ns_targets, saddr);
3349 	if (i == -1) {
3350 		slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c not found in targets\n",
3351 			  __func__, saddr);
3352 		return;
3353 	}
3354 	slave->last_rx = jiffies;
3355 	slave->target_last_arp_rx[i] = jiffies;
3356 }
3357 
3358 static int bond_na_rcv(const struct sk_buff *skb, struct bonding *bond,
3359 		       struct slave *slave)
3360 {
3361 	struct slave *curr_active_slave, *curr_arp_slave;
3362 	struct in6_addr *saddr, *daddr;
3363 	struct {
3364 		struct ipv6hdr ip6;
3365 		struct icmp6hdr icmp6;
3366 	} *combined, _combined;
3367 
3368 	if (skb->pkt_type == PACKET_OTHERHOST ||
3369 	    skb->pkt_type == PACKET_LOOPBACK)
3370 		goto out;
3371 
3372 	combined = skb_header_pointer(skb, 0, sizeof(_combined), &_combined);
3373 	if (!combined || combined->ip6.nexthdr != NEXTHDR_ICMP ||
3374 	    (combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
3375 	     combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
3376 		goto out;
3377 
3378 	saddr = &combined->ip6.saddr;
3379 	daddr = &combined->ip6.daddr;
3380 
3381 	slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI6c tip %pI6c\n",
3382 		  __func__, slave->dev->name, bond_slave_state(slave),
3383 		  bond->params.arp_validate, slave_do_arp_validate(bond, slave),
3384 		  saddr, daddr);
3385 
3386 	curr_active_slave = rcu_dereference(bond->curr_active_slave);
3387 	curr_arp_slave = rcu_dereference(bond->current_arp_slave);
3388 
3389 	/* We 'trust' the received ARP enough to validate it if:
3390 	 * see bond_arp_rcv().
3391 	 */
3392 	if (bond_is_active_slave(slave))
3393 		bond_validate_na(bond, slave, saddr, daddr);
3394 	else if (curr_active_slave &&
3395 		 time_after(slave_last_rx(bond, curr_active_slave),
3396 			    curr_active_slave->last_link_up))
3397 		bond_validate_na(bond, slave, daddr, saddr);
3398 	else if (curr_arp_slave &&
3399 		 bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
3400 		bond_validate_na(bond, slave, saddr, daddr);
3401 
3402 out:
3403 	return RX_HANDLER_ANOTHER;
3404 }
3405 #endif
3406 
3407 int bond_rcv_validate(const struct sk_buff *skb, struct bonding *bond,
3408 		      struct slave *slave)
3409 {
3410 #if IS_ENABLED(CONFIG_IPV6)
3411 	bool is_ipv6 = skb->protocol == __cpu_to_be16(ETH_P_IPV6);
3412 #endif
3413 	bool is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
3414 
3415 	slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
3416 		  __func__, skb->dev->name);
3417 
3418 	/* Use arp validate logic for both ARP and NS */
3419 	if (!slave_do_arp_validate(bond, slave)) {
3420 		if ((slave_do_arp_validate_only(bond) && is_arp) ||
3421 #if IS_ENABLED(CONFIG_IPV6)
3422 		    (slave_do_arp_validate_only(bond) && is_ipv6) ||
3423 #endif
3424 		    !slave_do_arp_validate_only(bond))
3425 			slave->last_rx = jiffies;
3426 		return RX_HANDLER_ANOTHER;
3427 	} else if (is_arp) {
3428 		return bond_arp_rcv(skb, bond, slave);
3429 #if IS_ENABLED(CONFIG_IPV6)
3430 	} else if (is_ipv6) {
3431 		return bond_na_rcv(skb, bond, slave);
3432 #endif
3433 	} else {
3434 		return RX_HANDLER_ANOTHER;
3435 	}
3436 }
3437 
3438 static void bond_send_validate(struct bonding *bond, struct slave *slave)
3439 {
3440 	bond_arp_send_all(bond, slave);
3441 #if IS_ENABLED(CONFIG_IPV6)
3442 	bond_ns_send_all(bond, slave);
3443 #endif
3444 }
3445 
3446 /* function to verify if we're in the arp_interval timeslice, returns true if
3447  * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
3448  * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
3449  */
3450 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
3451 				  int mod)
3452 {
3453 	int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3454 
3455 	return time_in_range(jiffies,
3456 			     last_act - delta_in_ticks,
3457 			     last_act + mod * delta_in_ticks + delta_in_ticks/2);
3458 }
3459 
3460 /* This function is called regularly to monitor each slave's link
3461  * ensuring that traffic is being sent and received when arp monitoring
3462  * is used in load-balancing mode. if the adapter has been dormant, then an
3463  * arp is transmitted to generate traffic. see activebackup_arp_monitor for
3464  * arp monitoring in active backup mode.
3465  */
3466 static void bond_loadbalance_arp_mon(struct bonding *bond)
3467 {
3468 	struct slave *slave, *oldcurrent;
3469 	struct list_head *iter;
3470 	int do_failover = 0, slave_state_changed = 0;
3471 
3472 	if (!bond_has_slaves(bond))
3473 		goto re_arm;
3474 
3475 	rcu_read_lock();
3476 
3477 	oldcurrent = rcu_dereference(bond->curr_active_slave);
3478 	/* see if any of the previous devices are up now (i.e. they have
3479 	 * xmt and rcv traffic). the curr_active_slave does not come into
3480 	 * the picture unless it is null. also, slave->last_link_up is not
3481 	 * needed here because we send an arp on each slave and give a slave
3482 	 * as long as it needs to get the tx/rx within the delta.
3483 	 * TODO: what about up/down delay in arp mode? it wasn't here before
3484 	 *       so it can wait
3485 	 */
3486 	bond_for_each_slave_rcu(bond, slave, iter) {
3487 		unsigned long last_tx = slave_last_tx(slave);
3488 
3489 		bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
3490 
3491 		if (slave->link != BOND_LINK_UP) {
3492 			if (bond_time_in_interval(bond, last_tx, 1) &&
3493 			    bond_time_in_interval(bond, slave->last_rx, 1)) {
3494 
3495 				bond_propose_link_state(slave, BOND_LINK_UP);
3496 				slave_state_changed = 1;
3497 
3498 				/* primary_slave has no meaning in round-robin
3499 				 * mode. the window of a slave being up and
3500 				 * curr_active_slave being null after enslaving
3501 				 * is closed.
3502 				 */
3503 				if (!oldcurrent) {
3504 					slave_info(bond->dev, slave->dev, "link status definitely up\n");
3505 					do_failover = 1;
3506 				} else {
3507 					slave_info(bond->dev, slave->dev, "interface is now up\n");
3508 				}
3509 			}
3510 		} else {
3511 			/* slave->link == BOND_LINK_UP */
3512 
3513 			/* not all switches will respond to an arp request
3514 			 * when the source ip is 0, so don't take the link down
3515 			 * if we don't know our ip yet
3516 			 */
3517 			if (!bond_time_in_interval(bond, last_tx, bond->params.missed_max) ||
3518 			    !bond_time_in_interval(bond, slave->last_rx, bond->params.missed_max)) {
3519 
3520 				bond_propose_link_state(slave, BOND_LINK_DOWN);
3521 				slave_state_changed = 1;
3522 
3523 				if (slave->link_failure_count < UINT_MAX)
3524 					slave->link_failure_count++;
3525 
3526 				slave_info(bond->dev, slave->dev, "interface is now down\n");
3527 
3528 				if (slave == oldcurrent)
3529 					do_failover = 1;
3530 			}
3531 		}
3532 
3533 		/* note: if switch is in round-robin mode, all links
3534 		 * must tx arp to ensure all links rx an arp - otherwise
3535 		 * links may oscillate or not come up at all; if switch is
3536 		 * in something like xor mode, there is nothing we can
3537 		 * do - all replies will be rx'ed on same link causing slaves
3538 		 * to be unstable during low/no traffic periods
3539 		 */
3540 		if (bond_slave_is_up(slave))
3541 			bond_send_validate(bond, slave);
3542 	}
3543 
3544 	rcu_read_unlock();
3545 
3546 	if (do_failover || slave_state_changed) {
3547 		if (!rtnl_trylock())
3548 			goto re_arm;
3549 
3550 		bond_for_each_slave(bond, slave, iter) {
3551 			if (slave->link_new_state != BOND_LINK_NOCHANGE)
3552 				slave->link = slave->link_new_state;
3553 		}
3554 
3555 		if (slave_state_changed) {
3556 			bond_slave_state_change(bond);
3557 			if (BOND_MODE(bond) == BOND_MODE_XOR)
3558 				bond_update_slave_arr(bond, NULL);
3559 		}
3560 		if (do_failover) {
3561 			block_netpoll_tx();
3562 			bond_select_active_slave(bond);
3563 			unblock_netpoll_tx();
3564 		}
3565 		rtnl_unlock();
3566 	}
3567 
3568 re_arm:
3569 	if (bond->params.arp_interval)
3570 		queue_delayed_work(bond->wq, &bond->arp_work,
3571 				   msecs_to_jiffies(bond->params.arp_interval));
3572 }
3573 
3574 /* Called to inspect slaves for active-backup mode ARP monitor link state
3575  * changes.  Sets proposed link state in slaves to specify what action
3576  * should take place for the slave.  Returns 0 if no changes are found, >0
3577  * if changes to link states must be committed.
3578  *
3579  * Called with rcu_read_lock held.
3580  */
3581 static int bond_ab_arp_inspect(struct bonding *bond)
3582 {
3583 	unsigned long last_tx, last_rx;
3584 	struct list_head *iter;
3585 	struct slave *slave;
3586 	int commit = 0;
3587 
3588 	bond_for_each_slave_rcu(bond, slave, iter) {
3589 		bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
3590 		last_rx = slave_last_rx(bond, slave);
3591 
3592 		if (slave->link != BOND_LINK_UP) {
3593 			if (bond_time_in_interval(bond, last_rx, 1)) {
3594 				bond_propose_link_state(slave, BOND_LINK_UP);
3595 				commit++;
3596 			} else if (slave->link == BOND_LINK_BACK) {
3597 				bond_propose_link_state(slave, BOND_LINK_FAIL);
3598 				commit++;
3599 			}
3600 			continue;
3601 		}
3602 
3603 		/* Give slaves 2*delta after being enslaved or made
3604 		 * active.  This avoids bouncing, as the last receive
3605 		 * times need a full ARP monitor cycle to be updated.
3606 		 */
3607 		if (bond_time_in_interval(bond, slave->last_link_up, 2))
3608 			continue;
3609 
3610 		/* Backup slave is down if:
3611 		 * - No current_arp_slave AND
3612 		 * - more than (missed_max+1)*delta since last receive AND
3613 		 * - the bond has an IP address
3614 		 *
3615 		 * Note: a non-null current_arp_slave indicates
3616 		 * the curr_active_slave went down and we are
3617 		 * searching for a new one; under this condition
3618 		 * we only take the curr_active_slave down - this
3619 		 * gives each slave a chance to tx/rx traffic
3620 		 * before being taken out
3621 		 */
3622 		if (!bond_is_active_slave(slave) &&
3623 		    !rcu_access_pointer(bond->current_arp_slave) &&
3624 		    !bond_time_in_interval(bond, last_rx, bond->params.missed_max + 1)) {
3625 			bond_propose_link_state(slave, BOND_LINK_DOWN);
3626 			commit++;
3627 		}
3628 
3629 		/* Active slave is down if:
3630 		 * - more than missed_max*delta since transmitting OR
3631 		 * - (more than missed_max*delta since receive AND
3632 		 *    the bond has an IP address)
3633 		 */
3634 		last_tx = slave_last_tx(slave);
3635 		if (bond_is_active_slave(slave) &&
3636 		    (!bond_time_in_interval(bond, last_tx, bond->params.missed_max) ||
3637 		     !bond_time_in_interval(bond, last_rx, bond->params.missed_max))) {
3638 			bond_propose_link_state(slave, BOND_LINK_DOWN);
3639 			commit++;
3640 		}
3641 	}
3642 
3643 	return commit;
3644 }
3645 
3646 /* Called to commit link state changes noted by inspection step of
3647  * active-backup mode ARP monitor.
3648  *
3649  * Called with RTNL hold.
3650  */
3651 static void bond_ab_arp_commit(struct bonding *bond)
3652 {
3653 	bool do_failover = false;
3654 	struct list_head *iter;
3655 	unsigned long last_tx;
3656 	struct slave *slave;
3657 
3658 	bond_for_each_slave(bond, slave, iter) {
3659 		switch (slave->link_new_state) {
3660 		case BOND_LINK_NOCHANGE:
3661 			continue;
3662 
3663 		case BOND_LINK_UP:
3664 			last_tx = slave_last_tx(slave);
3665 			if (rtnl_dereference(bond->curr_active_slave) != slave ||
3666 			    (!rtnl_dereference(bond->curr_active_slave) &&
3667 			     bond_time_in_interval(bond, last_tx, 1))) {
3668 				struct slave *current_arp_slave;
3669 
3670 				current_arp_slave = rtnl_dereference(bond->current_arp_slave);
3671 				bond_set_slave_link_state(slave, BOND_LINK_UP,
3672 							  BOND_SLAVE_NOTIFY_NOW);
3673 				if (current_arp_slave) {
3674 					bond_set_slave_inactive_flags(
3675 						current_arp_slave,
3676 						BOND_SLAVE_NOTIFY_NOW);
3677 					RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3678 				}
3679 
3680 				slave_info(bond->dev, slave->dev, "link status definitely up\n");
3681 
3682 				if (!rtnl_dereference(bond->curr_active_slave) ||
3683 				    slave == rtnl_dereference(bond->primary_slave) ||
3684 				    slave->prio > rtnl_dereference(bond->curr_active_slave)->prio)
3685 					do_failover = true;
3686 
3687 			}
3688 
3689 			continue;
3690 
3691 		case BOND_LINK_DOWN:
3692 			if (slave->link_failure_count < UINT_MAX)
3693 				slave->link_failure_count++;
3694 
3695 			bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3696 						  BOND_SLAVE_NOTIFY_NOW);
3697 			bond_set_slave_inactive_flags(slave,
3698 						      BOND_SLAVE_NOTIFY_NOW);
3699 
3700 			slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
3701 
3702 			if (slave == rtnl_dereference(bond->curr_active_slave)) {
3703 				RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3704 				do_failover = true;
3705 			}
3706 
3707 			continue;
3708 
3709 		case BOND_LINK_FAIL:
3710 			bond_set_slave_link_state(slave, BOND_LINK_FAIL,
3711 						  BOND_SLAVE_NOTIFY_NOW);
3712 			bond_set_slave_inactive_flags(slave,
3713 						      BOND_SLAVE_NOTIFY_NOW);
3714 
3715 			/* A slave has just been enslaved and has become
3716 			 * the current active slave.
3717 			 */
3718 			if (rtnl_dereference(bond->curr_active_slave))
3719 				RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3720 			continue;
3721 
3722 		default:
3723 			slave_err(bond->dev, slave->dev,
3724 				  "impossible: link_new_state %d on slave\n",
3725 				  slave->link_new_state);
3726 			continue;
3727 		}
3728 	}
3729 
3730 	if (do_failover) {
3731 		block_netpoll_tx();
3732 		bond_select_active_slave(bond);
3733 		unblock_netpoll_tx();
3734 	}
3735 
3736 	bond_set_carrier(bond);
3737 }
3738 
3739 /* Send ARP probes for active-backup mode ARP monitor.
3740  *
3741  * Called with rcu_read_lock held.
3742  */
3743 static bool bond_ab_arp_probe(struct bonding *bond)
3744 {
3745 	struct slave *slave, *before = NULL, *new_slave = NULL,
3746 		     *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
3747 		     *curr_active_slave = rcu_dereference(bond->curr_active_slave);
3748 	struct list_head *iter;
3749 	bool found = false;
3750 	bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
3751 
3752 	if (curr_arp_slave && curr_active_slave)
3753 		netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
3754 			    curr_arp_slave->dev->name,
3755 			    curr_active_slave->dev->name);
3756 
3757 	if (curr_active_slave) {
3758 		bond_send_validate(bond, curr_active_slave);
3759 		return should_notify_rtnl;
3760 	}
3761 
3762 	/* if we don't have a curr_active_slave, search for the next available
3763 	 * backup slave from the current_arp_slave and make it the candidate
3764 	 * for becoming the curr_active_slave
3765 	 */
3766 
3767 	if (!curr_arp_slave) {
3768 		curr_arp_slave = bond_first_slave_rcu(bond);
3769 		if (!curr_arp_slave)
3770 			return should_notify_rtnl;
3771 	}
3772 
3773 	bond_for_each_slave_rcu(bond, slave, iter) {
3774 		if (!found && !before && bond_slave_is_up(slave))
3775 			before = slave;
3776 
3777 		if (found && !new_slave && bond_slave_is_up(slave))
3778 			new_slave = slave;
3779 		/* if the link state is up at this point, we
3780 		 * mark it down - this can happen if we have
3781 		 * simultaneous link failures and
3782 		 * reselect_active_interface doesn't make this
3783 		 * one the current slave so it is still marked
3784 		 * up when it is actually down
3785 		 */
3786 		if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
3787 			bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3788 						  BOND_SLAVE_NOTIFY_LATER);
3789 			if (slave->link_failure_count < UINT_MAX)
3790 				slave->link_failure_count++;
3791 
3792 			bond_set_slave_inactive_flags(slave,
3793 						      BOND_SLAVE_NOTIFY_LATER);
3794 
3795 			slave_info(bond->dev, slave->dev, "backup interface is now down\n");
3796 		}
3797 		if (slave == curr_arp_slave)
3798 			found = true;
3799 	}
3800 
3801 	if (!new_slave && before)
3802 		new_slave = before;
3803 
3804 	if (!new_slave)
3805 		goto check_state;
3806 
3807 	bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
3808 				  BOND_SLAVE_NOTIFY_LATER);
3809 	bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
3810 	bond_send_validate(bond, new_slave);
3811 	new_slave->last_link_up = jiffies;
3812 	rcu_assign_pointer(bond->current_arp_slave, new_slave);
3813 
3814 check_state:
3815 	bond_for_each_slave_rcu(bond, slave, iter) {
3816 		if (slave->should_notify || slave->should_notify_link) {
3817 			should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
3818 			break;
3819 		}
3820 	}
3821 	return should_notify_rtnl;
3822 }
3823 
3824 static void bond_activebackup_arp_mon(struct bonding *bond)
3825 {
3826 	bool should_notify_peers = false;
3827 	bool should_notify_rtnl = false;
3828 	int delta_in_ticks;
3829 
3830 	delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3831 
3832 	if (!bond_has_slaves(bond))
3833 		goto re_arm;
3834 
3835 	rcu_read_lock();
3836 
3837 	should_notify_peers = bond_should_notify_peers(bond);
3838 
3839 	if (bond_ab_arp_inspect(bond)) {
3840 		rcu_read_unlock();
3841 
3842 		/* Race avoidance with bond_close flush of workqueue */
3843 		if (!rtnl_trylock()) {
3844 			delta_in_ticks = 1;
3845 			should_notify_peers = false;
3846 			goto re_arm;
3847 		}
3848 
3849 		bond_ab_arp_commit(bond);
3850 
3851 		rtnl_unlock();
3852 		rcu_read_lock();
3853 	}
3854 
3855 	should_notify_rtnl = bond_ab_arp_probe(bond);
3856 	rcu_read_unlock();
3857 
3858 re_arm:
3859 	if (bond->params.arp_interval)
3860 		queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3861 
3862 	if (should_notify_peers || should_notify_rtnl) {
3863 		if (!rtnl_trylock())
3864 			return;
3865 
3866 		if (should_notify_peers) {
3867 			bond->send_peer_notif--;
3868 			call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
3869 						 bond->dev);
3870 		}
3871 		if (should_notify_rtnl) {
3872 			bond_slave_state_notify(bond);
3873 			bond_slave_link_notify(bond);
3874 		}
3875 
3876 		rtnl_unlock();
3877 	}
3878 }
3879 
3880 static void bond_arp_monitor(struct work_struct *work)
3881 {
3882 	struct bonding *bond = container_of(work, struct bonding,
3883 					    arp_work.work);
3884 
3885 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
3886 		bond_activebackup_arp_mon(bond);
3887 	else
3888 		bond_loadbalance_arp_mon(bond);
3889 }
3890 
3891 /*-------------------------- netdev event handling --------------------------*/
3892 
3893 /* Change device name */
3894 static int bond_event_changename(struct bonding *bond)
3895 {
3896 	bond_remove_proc_entry(bond);
3897 	bond_create_proc_entry(bond);
3898 
3899 	bond_debug_reregister(bond);
3900 
3901 	return NOTIFY_DONE;
3902 }
3903 
3904 static int bond_master_netdev_event(unsigned long event,
3905 				    struct net_device *bond_dev)
3906 {
3907 	struct bonding *event_bond = netdev_priv(bond_dev);
3908 
3909 	netdev_dbg(bond_dev, "%s called\n", __func__);
3910 
3911 	switch (event) {
3912 	case NETDEV_CHANGENAME:
3913 		return bond_event_changename(event_bond);
3914 	case NETDEV_UNREGISTER:
3915 		bond_remove_proc_entry(event_bond);
3916 #ifdef CONFIG_XFRM_OFFLOAD
3917 		xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
3918 #endif /* CONFIG_XFRM_OFFLOAD */
3919 		break;
3920 	case NETDEV_REGISTER:
3921 		bond_create_proc_entry(event_bond);
3922 		break;
3923 	default:
3924 		break;
3925 	}
3926 
3927 	return NOTIFY_DONE;
3928 }
3929 
3930 static int bond_slave_netdev_event(unsigned long event,
3931 				   struct net_device *slave_dev)
3932 {
3933 	struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
3934 	struct bonding *bond;
3935 	struct net_device *bond_dev;
3936 
3937 	/* A netdev event can be generated while enslaving a device
3938 	 * before netdev_rx_handler_register is called in which case
3939 	 * slave will be NULL
3940 	 */
3941 	if (!slave) {
3942 		netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
3943 		return NOTIFY_DONE;
3944 	}
3945 
3946 	bond_dev = slave->bond->dev;
3947 	bond = slave->bond;
3948 	primary = rtnl_dereference(bond->primary_slave);
3949 
3950 	slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
3951 
3952 	switch (event) {
3953 	case NETDEV_UNREGISTER:
3954 		if (bond_dev->type != ARPHRD_ETHER)
3955 			bond_release_and_destroy(bond_dev, slave_dev);
3956 		else
3957 			__bond_release_one(bond_dev, slave_dev, false, true);
3958 		break;
3959 	case NETDEV_UP:
3960 	case NETDEV_CHANGE:
3961 		/* For 802.3ad mode only:
3962 		 * Getting invalid Speed/Duplex values here will put slave
3963 		 * in weird state. Mark it as link-fail if the link was
3964 		 * previously up or link-down if it hasn't yet come up, and
3965 		 * let link-monitoring (miimon) set it right when correct
3966 		 * speeds/duplex are available.
3967 		 */
3968 		if (bond_update_speed_duplex(slave) &&
3969 		    BOND_MODE(bond) == BOND_MODE_8023AD) {
3970 			if (slave->last_link_up)
3971 				slave->link = BOND_LINK_FAIL;
3972 			else
3973 				slave->link = BOND_LINK_DOWN;
3974 		}
3975 
3976 		if (BOND_MODE(bond) == BOND_MODE_8023AD)
3977 			bond_3ad_adapter_speed_duplex_changed(slave);
3978 		fallthrough;
3979 	case NETDEV_DOWN:
3980 		/* Refresh slave-array if applicable!
3981 		 * If the setup does not use miimon or arpmon (mode-specific!),
3982 		 * then these events will not cause the slave-array to be
3983 		 * refreshed. This will cause xmit to use a slave that is not
3984 		 * usable. Avoid such situation by refeshing the array at these
3985 		 * events. If these (miimon/arpmon) parameters are configured
3986 		 * then array gets refreshed twice and that should be fine!
3987 		 */
3988 		if (bond_mode_can_use_xmit_hash(bond))
3989 			bond_update_slave_arr(bond, NULL);
3990 		break;
3991 	case NETDEV_CHANGEMTU:
3992 		/* TODO: Should slaves be allowed to
3993 		 * independently alter their MTU?  For
3994 		 * an active-backup bond, slaves need
3995 		 * not be the same type of device, so
3996 		 * MTUs may vary.  For other modes,
3997 		 * slaves arguably should have the
3998 		 * same MTUs. To do this, we'd need to
3999 		 * take over the slave's change_mtu
4000 		 * function for the duration of their
4001 		 * servitude.
4002 		 */
4003 		break;
4004 	case NETDEV_CHANGENAME:
4005 		/* we don't care if we don't have primary set */
4006 		if (!bond_uses_primary(bond) ||
4007 		    !bond->params.primary[0])
4008 			break;
4009 
4010 		if (slave == primary) {
4011 			/* slave's name changed - he's no longer primary */
4012 			RCU_INIT_POINTER(bond->primary_slave, NULL);
4013 		} else if (!strcmp(slave_dev->name, bond->params.primary)) {
4014 			/* we have a new primary slave */
4015 			rcu_assign_pointer(bond->primary_slave, slave);
4016 		} else { /* we didn't change primary - exit */
4017 			break;
4018 		}
4019 
4020 		netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
4021 			    primary ? slave_dev->name : "none");
4022 
4023 		block_netpoll_tx();
4024 		bond_select_active_slave(bond);
4025 		unblock_netpoll_tx();
4026 		break;
4027 	case NETDEV_FEAT_CHANGE:
4028 		if (!bond->notifier_ctx) {
4029 			bond->notifier_ctx = true;
4030 			bond_compute_features(bond);
4031 			bond->notifier_ctx = false;
4032 		}
4033 		break;
4034 	case NETDEV_RESEND_IGMP:
4035 		/* Propagate to master device */
4036 		call_netdevice_notifiers(event, slave->bond->dev);
4037 		break;
4038 	case NETDEV_XDP_FEAT_CHANGE:
4039 		bond_xdp_set_features(bond_dev);
4040 		break;
4041 	default:
4042 		break;
4043 	}
4044 
4045 	return NOTIFY_DONE;
4046 }
4047 
4048 /* bond_netdev_event: handle netdev notifier chain events.
4049  *
4050  * This function receives events for the netdev chain.  The caller (an
4051  * ioctl handler calling blocking_notifier_call_chain) holds the necessary
4052  * locks for us to safely manipulate the slave devices (RTNL lock,
4053  * dev_probe_lock).
4054  */
4055 static int bond_netdev_event(struct notifier_block *this,
4056 			     unsigned long event, void *ptr)
4057 {
4058 	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
4059 
4060 	netdev_dbg(event_dev, "%s received %s\n",
4061 		   __func__, netdev_cmd_to_name(event));
4062 
4063 	if (!(event_dev->priv_flags & IFF_BONDING))
4064 		return NOTIFY_DONE;
4065 
4066 	if (event_dev->flags & IFF_MASTER) {
4067 		int ret;
4068 
4069 		ret = bond_master_netdev_event(event, event_dev);
4070 		if (ret != NOTIFY_DONE)
4071 			return ret;
4072 	}
4073 
4074 	if (event_dev->flags & IFF_SLAVE)
4075 		return bond_slave_netdev_event(event, event_dev);
4076 
4077 	return NOTIFY_DONE;
4078 }
4079 
4080 static struct notifier_block bond_netdev_notifier = {
4081 	.notifier_call = bond_netdev_event,
4082 };
4083 
4084 /*---------------------------- Hashing Policies -----------------------------*/
4085 
4086 /* Helper to access data in a packet, with or without a backing skb.
4087  * If skb is given the data is linearized if necessary via pskb_may_pull.
4088  */
4089 static inline const void *bond_pull_data(struct sk_buff *skb,
4090 					 const void *data, int hlen, int n)
4091 {
4092 	if (likely(n <= hlen))
4093 		return data;
4094 	else if (skb && likely(pskb_may_pull(skb, n)))
4095 		return skb->data;
4096 
4097 	return NULL;
4098 }
4099 
4100 /* L2 hash helper */
4101 static inline u32 bond_eth_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
4102 {
4103 	struct ethhdr *ep;
4104 
4105 	data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
4106 	if (!data)
4107 		return 0;
4108 
4109 	ep = (struct ethhdr *)(data + mhoff);
4110 	return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto);
4111 }
4112 
4113 static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk, const void *data,
4114 			 int hlen, __be16 l2_proto, int *nhoff, int *ip_proto, bool l34)
4115 {
4116 	const struct ipv6hdr *iph6;
4117 	const struct iphdr *iph;
4118 
4119 	if (l2_proto == htons(ETH_P_IP)) {
4120 		data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph));
4121 		if (!data)
4122 			return false;
4123 
4124 		iph = (const struct iphdr *)(data + *nhoff);
4125 		iph_to_flow_copy_v4addrs(fk, iph);
4126 		*nhoff += iph->ihl << 2;
4127 		if (!ip_is_fragment(iph))
4128 			*ip_proto = iph->protocol;
4129 	} else if (l2_proto == htons(ETH_P_IPV6)) {
4130 		data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph6));
4131 		if (!data)
4132 			return false;
4133 
4134 		iph6 = (const struct ipv6hdr *)(data + *nhoff);
4135 		iph_to_flow_copy_v6addrs(fk, iph6);
4136 		*nhoff += sizeof(*iph6);
4137 		*ip_proto = iph6->nexthdr;
4138 	} else {
4139 		return false;
4140 	}
4141 
4142 	if (l34 && *ip_proto >= 0)
4143 		fk->ports.ports = skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen);
4144 
4145 	return true;
4146 }
4147 
4148 static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
4149 {
4150 	u32 srcmac_vendor = 0, srcmac_dev = 0;
4151 	struct ethhdr *mac_hdr;
4152 	u16 vlan = 0;
4153 	int i;
4154 
4155 	data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
4156 	if (!data)
4157 		return 0;
4158 	mac_hdr = (struct ethhdr *)(data + mhoff);
4159 
4160 	for (i = 0; i < 3; i++)
4161 		srcmac_vendor = (srcmac_vendor << 8) | mac_hdr->h_source[i];
4162 
4163 	for (i = 3; i < ETH_ALEN; i++)
4164 		srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i];
4165 
4166 	if (skb && skb_vlan_tag_present(skb))
4167 		vlan = skb_vlan_tag_get(skb);
4168 
4169 	return vlan ^ srcmac_vendor ^ srcmac_dev;
4170 }
4171 
4172 /* Extract the appropriate headers based on bond's xmit policy */
4173 static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, const void *data,
4174 			      __be16 l2_proto, int nhoff, int hlen, struct flow_keys *fk)
4175 {
4176 	bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
4177 	int ip_proto = -1;
4178 
4179 	switch (bond->params.xmit_policy) {
4180 	case BOND_XMIT_POLICY_ENCAP23:
4181 	case BOND_XMIT_POLICY_ENCAP34:
4182 		memset(fk, 0, sizeof(*fk));
4183 		return __skb_flow_dissect(NULL, skb, &flow_keys_bonding,
4184 					  fk, data, l2_proto, nhoff, hlen, 0);
4185 	default:
4186 		break;
4187 	}
4188 
4189 	fk->ports.ports = 0;
4190 	memset(&fk->icmp, 0, sizeof(fk->icmp));
4191 	if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34))
4192 		return false;
4193 
4194 	/* ICMP error packets contains at least 8 bytes of the header
4195 	 * of the packet which generated the error. Use this information
4196 	 * to correlate ICMP error packets within the same flow which
4197 	 * generated the error.
4198 	 */
4199 	if (ip_proto == IPPROTO_ICMP || ip_proto == IPPROTO_ICMPV6) {
4200 		skb_flow_get_icmp_tci(skb, &fk->icmp, data, nhoff, hlen);
4201 		if (ip_proto == IPPROTO_ICMP) {
4202 			if (!icmp_is_err(fk->icmp.type))
4203 				return true;
4204 
4205 			nhoff += sizeof(struct icmphdr);
4206 		} else if (ip_proto == IPPROTO_ICMPV6) {
4207 			if (!icmpv6_is_err(fk->icmp.type))
4208 				return true;
4209 
4210 			nhoff += sizeof(struct icmp6hdr);
4211 		}
4212 		return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34);
4213 	}
4214 
4215 	return true;
4216 }
4217 
4218 static u32 bond_ip_hash(u32 hash, struct flow_keys *flow, int xmit_policy)
4219 {
4220 	hash ^= (__force u32)flow_get_u32_dst(flow) ^
4221 		(__force u32)flow_get_u32_src(flow);
4222 	hash ^= (hash >> 16);
4223 	hash ^= (hash >> 8);
4224 
4225 	/* discard lowest hash bit to deal with the common even ports pattern */
4226 	if (xmit_policy == BOND_XMIT_POLICY_LAYER34 ||
4227 		xmit_policy == BOND_XMIT_POLICY_ENCAP34)
4228 		return hash >> 1;
4229 
4230 	return hash;
4231 }
4232 
4233 /* Generate hash based on xmit policy. If @skb is given it is used to linearize
4234  * the data as required, but this function can be used without it if the data is
4235  * known to be linear (e.g. with xdp_buff).
4236  */
4237 static u32 __bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, const void *data,
4238 			    __be16 l2_proto, int mhoff, int nhoff, int hlen)
4239 {
4240 	struct flow_keys flow;
4241 	u32 hash;
4242 
4243 	if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC)
4244 		return bond_vlan_srcmac_hash(skb, data, mhoff, hlen);
4245 
4246 	if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
4247 	    !bond_flow_dissect(bond, skb, data, l2_proto, nhoff, hlen, &flow))
4248 		return bond_eth_hash(skb, data, mhoff, hlen);
4249 
4250 	if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
4251 	    bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
4252 		hash = bond_eth_hash(skb, data, mhoff, hlen);
4253 	} else {
4254 		if (flow.icmp.id)
4255 			memcpy(&hash, &flow.icmp, sizeof(hash));
4256 		else
4257 			memcpy(&hash, &flow.ports.ports, sizeof(hash));
4258 	}
4259 
4260 	return bond_ip_hash(hash, &flow, bond->params.xmit_policy);
4261 }
4262 
4263 /**
4264  * bond_xmit_hash - generate a hash value based on the xmit policy
4265  * @bond: bonding device
4266  * @skb: buffer to use for headers
4267  *
4268  * This function will extract the necessary headers from the skb buffer and use
4269  * them to generate a hash based on the xmit_policy set in the bonding device
4270  */
4271 u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
4272 {
4273 	if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
4274 	    skb->l4_hash)
4275 		return skb->hash;
4276 
4277 	return __bond_xmit_hash(bond, skb, skb->data, skb->protocol,
4278 				0, skb_network_offset(skb),
4279 				skb_headlen(skb));
4280 }
4281 
4282 /**
4283  * bond_xmit_hash_xdp - generate a hash value based on the xmit policy
4284  * @bond: bonding device
4285  * @xdp: buffer to use for headers
4286  *
4287  * The XDP variant of bond_xmit_hash.
4288  */
4289 static u32 bond_xmit_hash_xdp(struct bonding *bond, struct xdp_buff *xdp)
4290 {
4291 	struct ethhdr *eth;
4292 
4293 	if (xdp->data + sizeof(struct ethhdr) > xdp->data_end)
4294 		return 0;
4295 
4296 	eth = (struct ethhdr *)xdp->data;
4297 
4298 	return __bond_xmit_hash(bond, NULL, xdp->data, eth->h_proto, 0,
4299 				sizeof(struct ethhdr), xdp->data_end - xdp->data);
4300 }
4301 
4302 /*-------------------------- Device entry points ----------------------------*/
4303 
4304 void bond_work_init_all(struct bonding *bond)
4305 {
4306 	INIT_DELAYED_WORK(&bond->mcast_work,
4307 			  bond_resend_igmp_join_requests_delayed);
4308 	INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
4309 	INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
4310 	INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
4311 	INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
4312 	INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
4313 }
4314 
4315 void bond_work_cancel_all(struct bonding *bond)
4316 {
4317 	cancel_delayed_work_sync(&bond->mii_work);
4318 	cancel_delayed_work_sync(&bond->arp_work);
4319 	cancel_delayed_work_sync(&bond->alb_work);
4320 	cancel_delayed_work_sync(&bond->ad_work);
4321 	cancel_delayed_work_sync(&bond->mcast_work);
4322 	cancel_delayed_work_sync(&bond->slave_arr_work);
4323 }
4324 
4325 static int bond_open(struct net_device *bond_dev)
4326 {
4327 	struct bonding *bond = netdev_priv(bond_dev);
4328 	struct list_head *iter;
4329 	struct slave *slave;
4330 
4331 	if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN && !bond->rr_tx_counter) {
4332 		bond->rr_tx_counter = alloc_percpu(u32);
4333 		if (!bond->rr_tx_counter)
4334 			return -ENOMEM;
4335 	}
4336 
4337 	/* reset slave->backup and slave->inactive */
4338 	if (bond_has_slaves(bond)) {
4339 		bond_for_each_slave(bond, slave, iter) {
4340 			if (bond_uses_primary(bond) &&
4341 			    slave != rcu_access_pointer(bond->curr_active_slave)) {
4342 				bond_set_slave_inactive_flags(slave,
4343 							      BOND_SLAVE_NOTIFY_NOW);
4344 			} else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
4345 				bond_set_slave_active_flags(slave,
4346 							    BOND_SLAVE_NOTIFY_NOW);
4347 			}
4348 		}
4349 	}
4350 
4351 	if (bond_is_lb(bond)) {
4352 		/* bond_alb_initialize must be called before the timer
4353 		 * is started.
4354 		 */
4355 		if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
4356 			return -ENOMEM;
4357 		if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
4358 			queue_delayed_work(bond->wq, &bond->alb_work, 0);
4359 	}
4360 
4361 	if (bond->params.miimon)  /* link check interval, in milliseconds. */
4362 		queue_delayed_work(bond->wq, &bond->mii_work, 0);
4363 
4364 	if (bond->params.arp_interval) {  /* arp interval, in milliseconds. */
4365 		queue_delayed_work(bond->wq, &bond->arp_work, 0);
4366 		bond->recv_probe = bond_rcv_validate;
4367 	}
4368 
4369 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4370 		queue_delayed_work(bond->wq, &bond->ad_work, 0);
4371 		/* register to receive LACPDUs */
4372 		bond->recv_probe = bond_3ad_lacpdu_recv;
4373 		bond_3ad_initiate_agg_selection(bond, 1);
4374 
4375 		bond_for_each_slave(bond, slave, iter)
4376 			dev_mc_add(slave->dev, lacpdu_mcast_addr);
4377 
4378 		if (bond->params.broadcast_neighbor)
4379 			static_branch_inc(&bond_bcast_neigh_enabled);
4380 	}
4381 
4382 	if (bond_mode_can_use_xmit_hash(bond))
4383 		bond_update_slave_arr(bond, NULL);
4384 
4385 	return 0;
4386 }
4387 
4388 static int bond_close(struct net_device *bond_dev)
4389 {
4390 	struct bonding *bond = netdev_priv(bond_dev);
4391 	struct slave *slave;
4392 
4393 	bond_work_cancel_all(bond);
4394 	bond->send_peer_notif = 0;
4395 	if (bond_is_lb(bond))
4396 		bond_alb_deinitialize(bond);
4397 	bond->recv_probe = NULL;
4398 
4399 	if (BOND_MODE(bond) == BOND_MODE_8023AD &&
4400 	    bond->params.broadcast_neighbor)
4401 		static_branch_dec(&bond_bcast_neigh_enabled);
4402 
4403 	if (bond_uses_primary(bond)) {
4404 		rcu_read_lock();
4405 		slave = rcu_dereference(bond->curr_active_slave);
4406 		if (slave)
4407 			bond_hw_addr_flush(bond_dev, slave->dev);
4408 		rcu_read_unlock();
4409 	} else {
4410 		struct list_head *iter;
4411 
4412 		bond_for_each_slave(bond, slave, iter)
4413 			bond_hw_addr_flush(bond_dev, slave->dev);
4414 	}
4415 
4416 	return 0;
4417 }
4418 
4419 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
4420  * that some drivers can provide 32bit values only.
4421  */
4422 static void bond_fold_stats(struct rtnl_link_stats64 *_res,
4423 			    const struct rtnl_link_stats64 *_new,
4424 			    const struct rtnl_link_stats64 *_old)
4425 {
4426 	const u64 *new = (const u64 *)_new;
4427 	const u64 *old = (const u64 *)_old;
4428 	u64 *res = (u64 *)_res;
4429 	int i;
4430 
4431 	for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
4432 		u64 nv = new[i];
4433 		u64 ov = old[i];
4434 		s64 delta = nv - ov;
4435 
4436 		/* detects if this particular field is 32bit only */
4437 		if (((nv | ov) >> 32) == 0)
4438 			delta = (s64)(s32)((u32)nv - (u32)ov);
4439 
4440 		/* filter anomalies, some drivers reset their stats
4441 		 * at down/up events.
4442 		 */
4443 		if (delta > 0)
4444 			res[i] += delta;
4445 	}
4446 }
4447 
4448 #ifdef CONFIG_LOCKDEP
4449 static int bond_get_lowest_level_rcu(struct net_device *dev)
4450 {
4451 	struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
4452 	struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
4453 	int cur = 0, max = 0;
4454 
4455 	now = dev;
4456 	iter = &dev->adj_list.lower;
4457 
4458 	while (1) {
4459 		next = NULL;
4460 		while (1) {
4461 			ldev = netdev_next_lower_dev_rcu(now, &iter);
4462 			if (!ldev)
4463 				break;
4464 
4465 			next = ldev;
4466 			niter = &ldev->adj_list.lower;
4467 			dev_stack[cur] = now;
4468 			iter_stack[cur++] = iter;
4469 			if (max <= cur)
4470 				max = cur;
4471 			break;
4472 		}
4473 
4474 		if (!next) {
4475 			if (!cur)
4476 				return max;
4477 			next = dev_stack[--cur];
4478 			niter = iter_stack[cur];
4479 		}
4480 
4481 		now = next;
4482 		iter = niter;
4483 	}
4484 
4485 	return max;
4486 }
4487 #endif
4488 
4489 static void bond_get_stats(struct net_device *bond_dev,
4490 			   struct rtnl_link_stats64 *stats)
4491 {
4492 	struct bonding *bond = netdev_priv(bond_dev);
4493 	struct rtnl_link_stats64 temp;
4494 	struct list_head *iter;
4495 	struct slave *slave;
4496 	int nest_level = 0;
4497 
4498 
4499 	rcu_read_lock();
4500 #ifdef CONFIG_LOCKDEP
4501 	nest_level = bond_get_lowest_level_rcu(bond_dev);
4502 #endif
4503 
4504 	spin_lock_nested(&bond->stats_lock, nest_level);
4505 	memcpy(stats, &bond->bond_stats, sizeof(*stats));
4506 
4507 	bond_for_each_slave_rcu(bond, slave, iter) {
4508 		const struct rtnl_link_stats64 *new =
4509 			dev_get_stats(slave->dev, &temp);
4510 
4511 		bond_fold_stats(stats, new, &slave->slave_stats);
4512 
4513 		/* save off the slave stats for the next run */
4514 		memcpy(&slave->slave_stats, new, sizeof(*new));
4515 	}
4516 
4517 	memcpy(&bond->bond_stats, stats, sizeof(*stats));
4518 	spin_unlock(&bond->stats_lock);
4519 	rcu_read_unlock();
4520 }
4521 
4522 static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4523 {
4524 	struct bonding *bond = netdev_priv(bond_dev);
4525 	struct mii_ioctl_data *mii = NULL;
4526 
4527 	netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd);
4528 
4529 	switch (cmd) {
4530 	case SIOCGMIIPHY:
4531 		mii = if_mii(ifr);
4532 		if (!mii)
4533 			return -EINVAL;
4534 
4535 		mii->phy_id = 0;
4536 		fallthrough;
4537 	case SIOCGMIIREG:
4538 		/* We do this again just in case we were called by SIOCGMIIREG
4539 		 * instead of SIOCGMIIPHY.
4540 		 */
4541 		mii = if_mii(ifr);
4542 		if (!mii)
4543 			return -EINVAL;
4544 
4545 		if (mii->reg_num == 1) {
4546 			mii->val_out = 0;
4547 			if (netif_carrier_ok(bond->dev))
4548 				mii->val_out = BMSR_LSTATUS;
4549 		}
4550 
4551 		break;
4552 	default:
4553 		return -EOPNOTSUPP;
4554 	}
4555 
4556 	return 0;
4557 }
4558 
4559 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4560 {
4561 	struct bonding *bond = netdev_priv(bond_dev);
4562 	struct net_device *slave_dev = NULL;
4563 	struct ifbond k_binfo;
4564 	struct ifbond __user *u_binfo = NULL;
4565 	struct ifslave k_sinfo;
4566 	struct ifslave __user *u_sinfo = NULL;
4567 	struct bond_opt_value newval;
4568 	struct net *net;
4569 	int res = 0;
4570 
4571 	netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
4572 
4573 	switch (cmd) {
4574 	case SIOCBONDINFOQUERY:
4575 		u_binfo = (struct ifbond __user *)ifr->ifr_data;
4576 
4577 		if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
4578 			return -EFAULT;
4579 
4580 		bond_info_query(bond_dev, &k_binfo);
4581 		if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4582 			return -EFAULT;
4583 
4584 		return 0;
4585 	case SIOCBONDSLAVEINFOQUERY:
4586 		u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4587 
4588 		if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
4589 			return -EFAULT;
4590 
4591 		res = bond_slave_info_query(bond_dev, &k_sinfo);
4592 		if (res == 0 &&
4593 		    copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4594 			return -EFAULT;
4595 
4596 		return res;
4597 	default:
4598 		break;
4599 	}
4600 
4601 	net = dev_net(bond_dev);
4602 
4603 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4604 		return -EPERM;
4605 
4606 	slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
4607 
4608 	slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
4609 
4610 	if (!slave_dev)
4611 		return -ENODEV;
4612 
4613 	switch (cmd) {
4614 	case SIOCBONDENSLAVE:
4615 		res = bond_enslave(bond_dev, slave_dev, NULL);
4616 		break;
4617 	case SIOCBONDRELEASE:
4618 		res = bond_release(bond_dev, slave_dev);
4619 		break;
4620 	case SIOCBONDSETHWADDR:
4621 		res = bond_set_dev_addr(bond_dev, slave_dev);
4622 		break;
4623 	case SIOCBONDCHANGEACTIVE:
4624 		bond_opt_initstr(&newval, slave_dev->name);
4625 		res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
4626 					    &newval);
4627 		break;
4628 	default:
4629 		res = -EOPNOTSUPP;
4630 	}
4631 
4632 	return res;
4633 }
4634 
4635 static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr,
4636 			       void __user *data, int cmd)
4637 {
4638 	struct ifreq ifrdata = { .ifr_data = data };
4639 
4640 	switch (cmd) {
4641 	case BOND_INFO_QUERY_OLD:
4642 		return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY);
4643 	case BOND_SLAVE_INFO_QUERY_OLD:
4644 		return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY);
4645 	case BOND_ENSLAVE_OLD:
4646 		return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE);
4647 	case BOND_RELEASE_OLD:
4648 		return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE);
4649 	case BOND_SETHWADDR_OLD:
4650 		return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR);
4651 	case BOND_CHANGE_ACTIVE_OLD:
4652 		return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE);
4653 	}
4654 
4655 	return -EOPNOTSUPP;
4656 }
4657 
4658 static void bond_change_rx_flags(struct net_device *bond_dev, int change)
4659 {
4660 	struct bonding *bond = netdev_priv(bond_dev);
4661 
4662 	if (change & IFF_PROMISC)
4663 		bond_set_promiscuity(bond,
4664 				     bond_dev->flags & IFF_PROMISC ? 1 : -1);
4665 
4666 	if (change & IFF_ALLMULTI)
4667 		bond_set_allmulti(bond,
4668 				  bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
4669 }
4670 
4671 static void bond_set_rx_mode(struct net_device *bond_dev)
4672 {
4673 	struct bonding *bond = netdev_priv(bond_dev);
4674 	struct list_head *iter;
4675 	struct slave *slave;
4676 
4677 	rcu_read_lock();
4678 	if (bond_uses_primary(bond)) {
4679 		slave = rcu_dereference(bond->curr_active_slave);
4680 		if (slave) {
4681 			dev_uc_sync(slave->dev, bond_dev);
4682 			dev_mc_sync(slave->dev, bond_dev);
4683 		}
4684 	} else {
4685 		bond_for_each_slave_rcu(bond, slave, iter) {
4686 			dev_uc_sync_multiple(slave->dev, bond_dev);
4687 			dev_mc_sync_multiple(slave->dev, bond_dev);
4688 		}
4689 	}
4690 	rcu_read_unlock();
4691 }
4692 
4693 static int bond_neigh_init(struct neighbour *n)
4694 {
4695 	struct bonding *bond = netdev_priv(n->dev);
4696 	const struct net_device_ops *slave_ops;
4697 	struct neigh_parms parms;
4698 	struct slave *slave;
4699 	int ret = 0;
4700 
4701 	rcu_read_lock();
4702 	slave = bond_first_slave_rcu(bond);
4703 	if (!slave)
4704 		goto out;
4705 	slave_ops = slave->dev->netdev_ops;
4706 	if (!slave_ops->ndo_neigh_setup)
4707 		goto out;
4708 
4709 	/* TODO: find another way [1] to implement this.
4710 	 * Passing a zeroed structure is fragile,
4711 	 * but at least we do not pass garbage.
4712 	 *
4713 	 * [1] One way would be that ndo_neigh_setup() never touch
4714 	 *     struct neigh_parms, but propagate the new neigh_setup()
4715 	 *     back to ___neigh_create() / neigh_parms_alloc()
4716 	 */
4717 	memset(&parms, 0, sizeof(parms));
4718 	ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
4719 
4720 	if (ret)
4721 		goto out;
4722 
4723 	if (parms.neigh_setup)
4724 		ret = parms.neigh_setup(n);
4725 out:
4726 	rcu_read_unlock();
4727 	return ret;
4728 }
4729 
4730 /* The bonding ndo_neigh_setup is called at init time beofre any
4731  * slave exists. So we must declare proxy setup function which will
4732  * be used at run time to resolve the actual slave neigh param setup.
4733  *
4734  * It's also called by master devices (such as vlans) to setup their
4735  * underlying devices. In that case - do nothing, we're already set up from
4736  * our init.
4737  */
4738 static int bond_neigh_setup(struct net_device *dev,
4739 			    struct neigh_parms *parms)
4740 {
4741 	/* modify only our neigh_parms */
4742 	if (parms->dev == dev)
4743 		parms->neigh_setup = bond_neigh_init;
4744 
4745 	return 0;
4746 }
4747 
4748 /* Change the MTU of all of a master's slaves to match the master */
4749 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4750 {
4751 	struct bonding *bond = netdev_priv(bond_dev);
4752 	struct slave *slave, *rollback_slave;
4753 	struct list_head *iter;
4754 	int res = 0;
4755 
4756 	netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
4757 
4758 	bond_for_each_slave(bond, slave, iter) {
4759 		slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
4760 			   slave, slave->dev->netdev_ops->ndo_change_mtu);
4761 
4762 		res = dev_set_mtu(slave->dev, new_mtu);
4763 
4764 		if (res) {
4765 			/* If we failed to set the slave's mtu to the new value
4766 			 * we must abort the operation even in ACTIVE_BACKUP
4767 			 * mode, because if we allow the backup slaves to have
4768 			 * different mtu values than the active slave we'll
4769 			 * need to change their mtu when doing a failover. That
4770 			 * means changing their mtu from timer context, which
4771 			 * is probably not a good idea.
4772 			 */
4773 			slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
4774 				  res, new_mtu);
4775 			goto unwind;
4776 		}
4777 	}
4778 
4779 	WRITE_ONCE(bond_dev->mtu, new_mtu);
4780 
4781 	return 0;
4782 
4783 unwind:
4784 	/* unwind from head to the slave that failed */
4785 	bond_for_each_slave(bond, rollback_slave, iter) {
4786 		int tmp_res;
4787 
4788 		if (rollback_slave == slave)
4789 			break;
4790 
4791 		tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
4792 		if (tmp_res)
4793 			slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
4794 				  tmp_res);
4795 	}
4796 
4797 	return res;
4798 }
4799 
4800 /* Change HW address
4801  *
4802  * Note that many devices must be down to change the HW address, and
4803  * downing the master releases all slaves.  We can make bonds full of
4804  * bonding devices to test this, however.
4805  */
4806 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4807 {
4808 	struct bonding *bond = netdev_priv(bond_dev);
4809 	struct slave *slave, *rollback_slave;
4810 	struct sockaddr_storage *ss = addr, tmp_ss;
4811 	struct list_head *iter;
4812 	int res = 0;
4813 
4814 	if (BOND_MODE(bond) == BOND_MODE_ALB)
4815 		return bond_alb_set_mac_address(bond_dev, addr);
4816 
4817 
4818 	netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
4819 
4820 	/* If fail_over_mac is enabled, do nothing and return success.
4821 	 * Returning an error causes ifenslave to fail.
4822 	 */
4823 	if (bond->params.fail_over_mac &&
4824 	    BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
4825 		return 0;
4826 
4827 	if (!is_valid_ether_addr(ss->__data))
4828 		return -EADDRNOTAVAIL;
4829 
4830 	bond_for_each_slave(bond, slave, iter) {
4831 		slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
4832 			  __func__, slave);
4833 		res = dev_set_mac_address(slave->dev, addr, NULL);
4834 		if (res) {
4835 			/* TODO: consider downing the slave
4836 			 * and retry ?
4837 			 * User should expect communications
4838 			 * breakage anyway until ARP finish
4839 			 * updating, so...
4840 			 */
4841 			slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
4842 				  __func__, res);
4843 			goto unwind;
4844 		}
4845 	}
4846 
4847 	/* success */
4848 	dev_addr_set(bond_dev, ss->__data);
4849 	return 0;
4850 
4851 unwind:
4852 	memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
4853 	tmp_ss.ss_family = bond_dev->type;
4854 
4855 	/* unwind from head to the slave that failed */
4856 	bond_for_each_slave(bond, rollback_slave, iter) {
4857 		int tmp_res;
4858 
4859 		if (rollback_slave == slave)
4860 			break;
4861 
4862 		tmp_res = dev_set_mac_address(rollback_slave->dev, &tmp_ss, NULL);
4863 		if (tmp_res) {
4864 			slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
4865 				   __func__, tmp_res);
4866 		}
4867 	}
4868 
4869 	return res;
4870 }
4871 
4872 /**
4873  * bond_get_slave_by_id - get xmit slave with slave_id
4874  * @bond: bonding device that is transmitting
4875  * @slave_id: slave id up to slave_cnt-1 through which to transmit
4876  *
4877  * This function tries to get slave with slave_id but in case
4878  * it fails, it tries to find the first available slave for transmission.
4879  */
4880 static struct slave *bond_get_slave_by_id(struct bonding *bond,
4881 					  int slave_id)
4882 {
4883 	struct list_head *iter;
4884 	struct slave *slave;
4885 	int i = slave_id;
4886 
4887 	/* Here we start from the slave with slave_id */
4888 	bond_for_each_slave_rcu(bond, slave, iter) {
4889 		if (--i < 0) {
4890 			if (bond_slave_can_tx(slave))
4891 				return slave;
4892 		}
4893 	}
4894 
4895 	/* Here we start from the first slave up to slave_id */
4896 	i = slave_id;
4897 	bond_for_each_slave_rcu(bond, slave, iter) {
4898 		if (--i < 0)
4899 			break;
4900 		if (bond_slave_can_tx(slave))
4901 			return slave;
4902 	}
4903 	/* no slave that can tx has been found */
4904 	return NULL;
4905 }
4906 
4907 /**
4908  * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
4909  * @bond: bonding device to use
4910  *
4911  * Based on the value of the bonding device's packets_per_slave parameter
4912  * this function generates a slave id, which is usually used as the next
4913  * slave to transmit through.
4914  */
4915 static u32 bond_rr_gen_slave_id(struct bonding *bond)
4916 {
4917 	u32 slave_id;
4918 	struct reciprocal_value reciprocal_packets_per_slave;
4919 	int packets_per_slave = bond->params.packets_per_slave;
4920 
4921 	switch (packets_per_slave) {
4922 	case 0:
4923 		slave_id = get_random_u32();
4924 		break;
4925 	case 1:
4926 		slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
4927 		break;
4928 	default:
4929 		reciprocal_packets_per_slave =
4930 			bond->params.reciprocal_packets_per_slave;
4931 		slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
4932 		slave_id = reciprocal_divide(slave_id,
4933 					     reciprocal_packets_per_slave);
4934 		break;
4935 	}
4936 
4937 	return slave_id;
4938 }
4939 
4940 static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
4941 						    struct sk_buff *skb)
4942 {
4943 	struct slave *slave;
4944 	int slave_cnt;
4945 	u32 slave_id;
4946 
4947 	/* Start with the curr_active_slave that joined the bond as the
4948 	 * default for sending IGMP traffic.  For failover purposes one
4949 	 * needs to maintain some consistency for the interface that will
4950 	 * send the join/membership reports.  The curr_active_slave found
4951 	 * will send all of this type of traffic.
4952 	 */
4953 	if (skb->protocol == htons(ETH_P_IP)) {
4954 		int noff = skb_network_offset(skb);
4955 		struct iphdr *iph;
4956 
4957 		if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
4958 			goto non_igmp;
4959 
4960 		iph = ip_hdr(skb);
4961 		if (iph->protocol == IPPROTO_IGMP) {
4962 			slave = rcu_dereference(bond->curr_active_slave);
4963 			if (slave)
4964 				return slave;
4965 			return bond_get_slave_by_id(bond, 0);
4966 		}
4967 	}
4968 
4969 non_igmp:
4970 	slave_cnt = READ_ONCE(bond->slave_cnt);
4971 	if (likely(slave_cnt)) {
4972 		slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
4973 		return bond_get_slave_by_id(bond, slave_id);
4974 	}
4975 	return NULL;
4976 }
4977 
4978 static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond,
4979 							struct xdp_buff *xdp)
4980 {
4981 	struct slave *slave;
4982 	int slave_cnt;
4983 	u32 slave_id;
4984 	const struct ethhdr *eth;
4985 	void *data = xdp->data;
4986 
4987 	if (data + sizeof(struct ethhdr) > xdp->data_end)
4988 		goto non_igmp;
4989 
4990 	eth = (struct ethhdr *)data;
4991 	data += sizeof(struct ethhdr);
4992 
4993 	/* See comment on IGMP in bond_xmit_roundrobin_slave_get() */
4994 	if (eth->h_proto == htons(ETH_P_IP)) {
4995 		const struct iphdr *iph;
4996 
4997 		if (data + sizeof(struct iphdr) > xdp->data_end)
4998 			goto non_igmp;
4999 
5000 		iph = (struct iphdr *)data;
5001 
5002 		if (iph->protocol == IPPROTO_IGMP) {
5003 			slave = rcu_dereference(bond->curr_active_slave);
5004 			if (slave)
5005 				return slave;
5006 			return bond_get_slave_by_id(bond, 0);
5007 		}
5008 	}
5009 
5010 non_igmp:
5011 	slave_cnt = READ_ONCE(bond->slave_cnt);
5012 	if (likely(slave_cnt)) {
5013 		slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
5014 		return bond_get_slave_by_id(bond, slave_id);
5015 	}
5016 	return NULL;
5017 }
5018 
5019 static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
5020 					struct net_device *bond_dev)
5021 {
5022 	struct bonding *bond = netdev_priv(bond_dev);
5023 	struct slave *slave;
5024 
5025 	slave = bond_xmit_roundrobin_slave_get(bond, skb);
5026 	if (likely(slave))
5027 		return bond_dev_queue_xmit(bond, skb, slave->dev);
5028 
5029 	return bond_tx_drop(bond_dev, skb);
5030 }
5031 
5032 static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond)
5033 {
5034 	return rcu_dereference(bond->curr_active_slave);
5035 }
5036 
5037 /* In active-backup mode, we know that bond->curr_active_slave is always valid if
5038  * the bond has a usable interface.
5039  */
5040 static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
5041 					  struct net_device *bond_dev)
5042 {
5043 	struct bonding *bond = netdev_priv(bond_dev);
5044 	struct slave *slave;
5045 
5046 	slave = bond_xmit_activebackup_slave_get(bond);
5047 	if (slave)
5048 		return bond_dev_queue_xmit(bond, skb, slave->dev);
5049 
5050 	return bond_tx_drop(bond_dev, skb);
5051 }
5052 
5053 /* Use this to update slave_array when (a) it's not appropriate to update
5054  * slave_array right away (note that update_slave_array() may sleep)
5055  * and / or (b) RTNL is not held.
5056  */
5057 void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
5058 {
5059 	queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
5060 }
5061 
5062 /* Slave array work handler. Holds only RTNL */
5063 static void bond_slave_arr_handler(struct work_struct *work)
5064 {
5065 	struct bonding *bond = container_of(work, struct bonding,
5066 					    slave_arr_work.work);
5067 	int ret;
5068 
5069 	if (!rtnl_trylock())
5070 		goto err;
5071 
5072 	ret = bond_update_slave_arr(bond, NULL);
5073 	rtnl_unlock();
5074 	if (ret) {
5075 		pr_warn_ratelimited("Failed to update slave array from WT\n");
5076 		goto err;
5077 	}
5078 	return;
5079 
5080 err:
5081 	bond_slave_arr_work_rearm(bond, 1);
5082 }
5083 
5084 static void bond_skip_slave(struct bond_up_slave *slaves,
5085 			    struct slave *skipslave)
5086 {
5087 	int idx;
5088 
5089 	/* Rare situation where caller has asked to skip a specific
5090 	 * slave but allocation failed (most likely!). BTW this is
5091 	 * only possible when the call is initiated from
5092 	 * __bond_release_one(). In this situation; overwrite the
5093 	 * skipslave entry in the array with the last entry from the
5094 	 * array to avoid a situation where the xmit path may choose
5095 	 * this to-be-skipped slave to send a packet out.
5096 	 */
5097 	for (idx = 0; slaves && idx < slaves->count; idx++) {
5098 		if (skipslave == slaves->arr[idx]) {
5099 			slaves->arr[idx] =
5100 				slaves->arr[slaves->count - 1];
5101 			slaves->count--;
5102 			break;
5103 		}
5104 	}
5105 }
5106 
5107 static void bond_set_slave_arr(struct bonding *bond,
5108 			       struct bond_up_slave *usable_slaves,
5109 			       struct bond_up_slave *all_slaves)
5110 {
5111 	struct bond_up_slave *usable, *all;
5112 
5113 	usable = rtnl_dereference(bond->usable_slaves);
5114 	rcu_assign_pointer(bond->usable_slaves, usable_slaves);
5115 	kfree_rcu(usable, rcu);
5116 
5117 	all = rtnl_dereference(bond->all_slaves);
5118 	rcu_assign_pointer(bond->all_slaves, all_slaves);
5119 	kfree_rcu(all, rcu);
5120 }
5121 
5122 static void bond_reset_slave_arr(struct bonding *bond)
5123 {
5124 	bond_set_slave_arr(bond, NULL, NULL);
5125 }
5126 
5127 /* Build the usable slaves array in control path for modes that use xmit-hash
5128  * to determine the slave interface -
5129  * (a) BOND_MODE_8023AD
5130  * (b) BOND_MODE_XOR
5131  * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
5132  *
5133  * The caller is expected to hold RTNL only and NO other lock!
5134  */
5135 int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
5136 {
5137 	struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
5138 	struct slave *slave;
5139 	struct list_head *iter;
5140 	int agg_id = 0;
5141 	int ret = 0;
5142 
5143 	might_sleep();
5144 
5145 	usable_slaves = kzalloc(struct_size(usable_slaves, arr,
5146 					    bond->slave_cnt), GFP_KERNEL);
5147 	all_slaves = kzalloc(struct_size(all_slaves, arr,
5148 					 bond->slave_cnt), GFP_KERNEL);
5149 	if (!usable_slaves || !all_slaves) {
5150 		ret = -ENOMEM;
5151 		goto out;
5152 	}
5153 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
5154 		struct ad_info ad_info;
5155 
5156 		spin_lock_bh(&bond->mode_lock);
5157 		if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
5158 			spin_unlock_bh(&bond->mode_lock);
5159 			pr_debug("bond_3ad_get_active_agg_info failed\n");
5160 			/* No active aggragator means it's not safe to use
5161 			 * the previous array.
5162 			 */
5163 			bond_reset_slave_arr(bond);
5164 			goto out;
5165 		}
5166 		spin_unlock_bh(&bond->mode_lock);
5167 		agg_id = ad_info.aggregator_id;
5168 	}
5169 	bond_for_each_slave(bond, slave, iter) {
5170 		if (skipslave == slave)
5171 			continue;
5172 
5173 		all_slaves->arr[all_slaves->count++] = slave;
5174 		if (BOND_MODE(bond) == BOND_MODE_8023AD) {
5175 			struct aggregator *agg;
5176 
5177 			agg = SLAVE_AD_INFO(slave)->port.aggregator;
5178 			if (!agg || agg->aggregator_identifier != agg_id)
5179 				continue;
5180 		}
5181 		if (!bond_slave_can_tx(slave))
5182 			continue;
5183 
5184 		slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
5185 			  usable_slaves->count);
5186 
5187 		usable_slaves->arr[usable_slaves->count++] = slave;
5188 	}
5189 
5190 	bond_set_slave_arr(bond, usable_slaves, all_slaves);
5191 	return ret;
5192 out:
5193 	if (ret != 0 && skipslave) {
5194 		bond_skip_slave(rtnl_dereference(bond->all_slaves),
5195 				skipslave);
5196 		bond_skip_slave(rtnl_dereference(bond->usable_slaves),
5197 				skipslave);
5198 	}
5199 	kfree_rcu(all_slaves, rcu);
5200 	kfree_rcu(usable_slaves, rcu);
5201 
5202 	return ret;
5203 }
5204 
5205 static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
5206 						 struct sk_buff *skb,
5207 						 struct bond_up_slave *slaves)
5208 {
5209 	struct slave *slave;
5210 	unsigned int count;
5211 	u32 hash;
5212 
5213 	hash = bond_xmit_hash(bond, skb);
5214 	count = slaves ? READ_ONCE(slaves->count) : 0;
5215 	if (unlikely(!count))
5216 		return NULL;
5217 
5218 	slave = slaves->arr[hash % count];
5219 	return slave;
5220 }
5221 
5222 static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond,
5223 						     struct xdp_buff *xdp)
5224 {
5225 	struct bond_up_slave *slaves;
5226 	unsigned int count;
5227 	u32 hash;
5228 
5229 	hash = bond_xmit_hash_xdp(bond, xdp);
5230 	slaves = rcu_dereference(bond->usable_slaves);
5231 	count = slaves ? READ_ONCE(slaves->count) : 0;
5232 	if (unlikely(!count))
5233 		return NULL;
5234 
5235 	return slaves->arr[hash % count];
5236 }
5237 
5238 static bool bond_should_broadcast_neighbor(struct sk_buff *skb,
5239 					   struct net_device *dev)
5240 {
5241 	struct bonding *bond = netdev_priv(dev);
5242 	struct {
5243 		struct ipv6hdr ip6;
5244 		struct icmp6hdr icmp6;
5245 	} *combined, _combined;
5246 
5247 	if (!static_branch_unlikely(&bond_bcast_neigh_enabled))
5248 		return false;
5249 
5250 	if (!bond->params.broadcast_neighbor)
5251 		return false;
5252 
5253 	if (skb->protocol == htons(ETH_P_ARP))
5254 		return true;
5255 
5256 	if (skb->protocol == htons(ETH_P_IPV6)) {
5257 		combined = skb_header_pointer(skb, skb_mac_header_len(skb),
5258 					      sizeof(_combined),
5259 					      &_combined);
5260 		if (combined && combined->ip6.nexthdr == NEXTHDR_ICMP &&
5261 		    (combined->icmp6.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
5262 		     combined->icmp6.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT))
5263 			return true;
5264 	}
5265 
5266 	return false;
5267 }
5268 
5269 /* Use this Xmit function for 3AD as well as XOR modes. The current
5270  * usable slave array is formed in the control path. The xmit function
5271  * just calculates hash and sends the packet out.
5272  */
5273 static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
5274 				     struct net_device *dev)
5275 {
5276 	struct bonding *bond = netdev_priv(dev);
5277 	struct bond_up_slave *slaves;
5278 	struct slave *slave;
5279 
5280 	slaves = rcu_dereference(bond->usable_slaves);
5281 	slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
5282 	if (likely(slave))
5283 		return bond_dev_queue_xmit(bond, skb, slave->dev);
5284 
5285 	return bond_tx_drop(dev, skb);
5286 }
5287 
5288 /* in broadcast mode, we send everything to all or usable slave interfaces.
5289  * under rcu_read_lock when this function is called.
5290  */
5291 static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
5292 				       struct net_device *bond_dev,
5293 				       bool all_slaves)
5294 {
5295 	struct bonding *bond = netdev_priv(bond_dev);
5296 	struct bond_up_slave *slaves;
5297 	bool xmit_suc = false;
5298 	bool skb_used = false;
5299 	int slaves_count, i;
5300 
5301 	if (all_slaves)
5302 		slaves = rcu_dereference(bond->all_slaves);
5303 	else
5304 		slaves = rcu_dereference(bond->usable_slaves);
5305 
5306 	slaves_count = slaves ? READ_ONCE(slaves->count) : 0;
5307 	for (i = 0; i < slaves_count; i++) {
5308 		struct slave *slave = slaves->arr[i];
5309 		struct sk_buff *skb2;
5310 
5311 		if (!(bond_slave_is_up(slave) && slave->link == BOND_LINK_UP))
5312 			continue;
5313 
5314 		if (bond_is_last_slave(bond, slave)) {
5315 			skb2 = skb;
5316 			skb_used = true;
5317 		} else {
5318 			skb2 = skb_clone(skb, GFP_ATOMIC);
5319 			if (!skb2) {
5320 				net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
5321 						    bond_dev->name, __func__);
5322 				continue;
5323 			}
5324 		}
5325 
5326 		if (bond_dev_queue_xmit(bond, skb2, slave->dev) == NETDEV_TX_OK)
5327 			xmit_suc = true;
5328 	}
5329 
5330 	if (!skb_used)
5331 		dev_kfree_skb_any(skb);
5332 
5333 	if (xmit_suc)
5334 		return NETDEV_TX_OK;
5335 
5336 	dev_core_stats_tx_dropped_inc(bond_dev);
5337 	return NET_XMIT_DROP;
5338 }
5339 
5340 /*------------------------- Device initialization ---------------------------*/
5341 
5342 /* Lookup the slave that corresponds to a qid */
5343 static inline int bond_slave_override(struct bonding *bond,
5344 				      struct sk_buff *skb)
5345 {
5346 	struct slave *slave = NULL;
5347 	struct list_head *iter;
5348 
5349 	if (!skb_rx_queue_recorded(skb))
5350 		return 1;
5351 
5352 	/* Find out if any slaves have the same mapping as this skb. */
5353 	bond_for_each_slave_rcu(bond, slave, iter) {
5354 		if (READ_ONCE(slave->queue_id) == skb_get_queue_mapping(skb)) {
5355 			if (bond_slave_is_up(slave) &&
5356 			    slave->link == BOND_LINK_UP) {
5357 				bond_dev_queue_xmit(bond, skb, slave->dev);
5358 				return 0;
5359 			}
5360 			/* If the slave isn't UP, use default transmit policy. */
5361 			break;
5362 		}
5363 	}
5364 
5365 	return 1;
5366 }
5367 
5368 
5369 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
5370 			     struct net_device *sb_dev)
5371 {
5372 	/* This helper function exists to help dev_pick_tx get the correct
5373 	 * destination queue.  Using a helper function skips a call to
5374 	 * skb_tx_hash and will put the skbs in the queue we expect on their
5375 	 * way down to the bonding driver.
5376 	 */
5377 	u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
5378 
5379 	/* Save the original txq to restore before passing to the driver */
5380 	qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
5381 
5382 	if (unlikely(txq >= dev->real_num_tx_queues)) {
5383 		do {
5384 			txq -= dev->real_num_tx_queues;
5385 		} while (txq >= dev->real_num_tx_queues);
5386 	}
5387 	return txq;
5388 }
5389 
5390 static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
5391 					      struct sk_buff *skb,
5392 					      bool all_slaves)
5393 {
5394 	struct bonding *bond = netdev_priv(master_dev);
5395 	struct bond_up_slave *slaves;
5396 	struct slave *slave = NULL;
5397 
5398 	switch (BOND_MODE(bond)) {
5399 	case BOND_MODE_ROUNDROBIN:
5400 		slave = bond_xmit_roundrobin_slave_get(bond, skb);
5401 		break;
5402 	case BOND_MODE_ACTIVEBACKUP:
5403 		slave = bond_xmit_activebackup_slave_get(bond);
5404 		break;
5405 	case BOND_MODE_8023AD:
5406 	case BOND_MODE_XOR:
5407 		if (all_slaves)
5408 			slaves = rcu_dereference(bond->all_slaves);
5409 		else
5410 			slaves = rcu_dereference(bond->usable_slaves);
5411 		slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
5412 		break;
5413 	case BOND_MODE_BROADCAST:
5414 		break;
5415 	case BOND_MODE_ALB:
5416 		slave = bond_xmit_alb_slave_get(bond, skb);
5417 		break;
5418 	case BOND_MODE_TLB:
5419 		slave = bond_xmit_tlb_slave_get(bond, skb);
5420 		break;
5421 	default:
5422 		/* Should never happen, mode already checked */
5423 		WARN_ONCE(true, "Unknown bonding mode");
5424 		break;
5425 	}
5426 
5427 	if (slave)
5428 		return slave->dev;
5429 	return NULL;
5430 }
5431 
5432 static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow)
5433 {
5434 	switch (sk->sk_family) {
5435 #if IS_ENABLED(CONFIG_IPV6)
5436 	case AF_INET6:
5437 		if (ipv6_only_sock(sk) ||
5438 		    ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) {
5439 			flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
5440 			flow->addrs.v6addrs.src = inet6_sk(sk)->saddr;
5441 			flow->addrs.v6addrs.dst = sk->sk_v6_daddr;
5442 			break;
5443 		}
5444 		fallthrough;
5445 #endif
5446 	default: /* AF_INET */
5447 		flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
5448 		flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr;
5449 		flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr;
5450 		break;
5451 	}
5452 
5453 	flow->ports.src = inet_sk(sk)->inet_sport;
5454 	flow->ports.dst = inet_sk(sk)->inet_dport;
5455 }
5456 
5457 /**
5458  * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields
5459  * @sk: socket to use for headers
5460  *
5461  * This function will extract the necessary field from the socket and use
5462  * them to generate a hash based on the LAYER34 xmit_policy.
5463  * Assumes that sk is a TCP or UDP socket.
5464  */
5465 static u32 bond_sk_hash_l34(struct sock *sk)
5466 {
5467 	struct flow_keys flow;
5468 	u32 hash;
5469 
5470 	bond_sk_to_flow(sk, &flow);
5471 
5472 	/* L4 */
5473 	memcpy(&hash, &flow.ports.ports, sizeof(hash));
5474 	/* L3 */
5475 	return bond_ip_hash(hash, &flow, BOND_XMIT_POLICY_LAYER34);
5476 }
5477 
5478 static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond,
5479 						  struct sock *sk)
5480 {
5481 	struct bond_up_slave *slaves;
5482 	struct slave *slave;
5483 	unsigned int count;
5484 	u32 hash;
5485 
5486 	slaves = rcu_dereference(bond->usable_slaves);
5487 	count = slaves ? READ_ONCE(slaves->count) : 0;
5488 	if (unlikely(!count))
5489 		return NULL;
5490 
5491 	hash = bond_sk_hash_l34(sk);
5492 	slave = slaves->arr[hash % count];
5493 
5494 	return slave->dev;
5495 }
5496 
5497 static struct net_device *bond_sk_get_lower_dev(struct net_device *dev,
5498 						struct sock *sk)
5499 {
5500 	struct bonding *bond = netdev_priv(dev);
5501 	struct net_device *lower = NULL;
5502 
5503 	rcu_read_lock();
5504 	if (bond_sk_check(bond))
5505 		lower = __bond_sk_get_lower_dev(bond, sk);
5506 	rcu_read_unlock();
5507 
5508 	return lower;
5509 }
5510 
5511 #if IS_ENABLED(CONFIG_TLS_DEVICE)
5512 static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb,
5513 					struct net_device *dev)
5514 {
5515 	struct net_device *tls_netdev = rcu_dereference(tls_get_ctx(skb->sk)->netdev);
5516 
5517 	/* tls_netdev might become NULL, even if tls_is_skb_tx_device_offloaded
5518 	 * was true, if tls_device_down is running in parallel, but it's OK,
5519 	 * because bond_get_slave_by_dev has a NULL check.
5520 	 */
5521 	if (likely(bond_get_slave_by_dev(bond, tls_netdev)))
5522 		return bond_dev_queue_xmit(bond, skb, tls_netdev);
5523 	return bond_tx_drop(dev, skb);
5524 }
5525 #endif
5526 
5527 static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
5528 {
5529 	struct bonding *bond = netdev_priv(dev);
5530 
5531 	if (bond_should_override_tx_queue(bond) &&
5532 	    !bond_slave_override(bond, skb))
5533 		return NETDEV_TX_OK;
5534 
5535 #if IS_ENABLED(CONFIG_TLS_DEVICE)
5536 	if (tls_is_skb_tx_device_offloaded(skb))
5537 		return bond_tls_device_xmit(bond, skb, dev);
5538 #endif
5539 
5540 	switch (BOND_MODE(bond)) {
5541 	case BOND_MODE_ROUNDROBIN:
5542 		return bond_xmit_roundrobin(skb, dev);
5543 	case BOND_MODE_ACTIVEBACKUP:
5544 		return bond_xmit_activebackup(skb, dev);
5545 	case BOND_MODE_8023AD:
5546 		if (bond_should_broadcast_neighbor(skb, dev))
5547 			return bond_xmit_broadcast(skb, dev, false);
5548 		fallthrough;
5549 	case BOND_MODE_XOR:
5550 		return bond_3ad_xor_xmit(skb, dev);
5551 	case BOND_MODE_BROADCAST:
5552 		return bond_xmit_broadcast(skb, dev, true);
5553 	case BOND_MODE_ALB:
5554 		return bond_alb_xmit(skb, dev);
5555 	case BOND_MODE_TLB:
5556 		return bond_tlb_xmit(skb, dev);
5557 	default:
5558 		/* Should never happen, mode already checked */
5559 		netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
5560 		WARN_ON_ONCE(1);
5561 		return bond_tx_drop(dev, skb);
5562 	}
5563 }
5564 
5565 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
5566 {
5567 	struct bonding *bond = netdev_priv(dev);
5568 	netdev_tx_t ret = NETDEV_TX_OK;
5569 
5570 	/* If we risk deadlock from transmitting this in the
5571 	 * netpoll path, tell netpoll to queue the frame for later tx
5572 	 */
5573 	if (unlikely(is_netpoll_tx_blocked(dev)))
5574 		return NETDEV_TX_BUSY;
5575 
5576 	rcu_read_lock();
5577 	if (bond_has_slaves(bond))
5578 		ret = __bond_start_xmit(skb, dev);
5579 	else
5580 		ret = bond_tx_drop(dev, skb);
5581 	rcu_read_unlock();
5582 
5583 	return ret;
5584 }
5585 
5586 static struct net_device *
5587 bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp)
5588 {
5589 	struct bonding *bond = netdev_priv(bond_dev);
5590 	struct slave *slave;
5591 
5592 	/* Caller needs to hold rcu_read_lock() */
5593 
5594 	switch (BOND_MODE(bond)) {
5595 	case BOND_MODE_ROUNDROBIN:
5596 		slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp);
5597 		break;
5598 
5599 	case BOND_MODE_ACTIVEBACKUP:
5600 		slave = bond_xmit_activebackup_slave_get(bond);
5601 		break;
5602 
5603 	case BOND_MODE_8023AD:
5604 	case BOND_MODE_XOR:
5605 		slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp);
5606 		break;
5607 
5608 	default:
5609 		if (net_ratelimit())
5610 			netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n",
5611 				   BOND_MODE(bond));
5612 		return NULL;
5613 	}
5614 
5615 	if (slave)
5616 		return slave->dev;
5617 
5618 	return NULL;
5619 }
5620 
5621 static int bond_xdp_xmit(struct net_device *bond_dev,
5622 			 int n, struct xdp_frame **frames, u32 flags)
5623 {
5624 	int nxmit, err = -ENXIO;
5625 
5626 	rcu_read_lock();
5627 
5628 	for (nxmit = 0; nxmit < n; nxmit++) {
5629 		struct xdp_frame *frame = frames[nxmit];
5630 		struct xdp_frame *frames1[] = {frame};
5631 		struct net_device *slave_dev;
5632 		struct xdp_buff xdp;
5633 
5634 		xdp_convert_frame_to_buff(frame, &xdp);
5635 
5636 		slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp);
5637 		if (!slave_dev) {
5638 			err = -ENXIO;
5639 			break;
5640 		}
5641 
5642 		err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags);
5643 		if (err < 1)
5644 			break;
5645 	}
5646 
5647 	rcu_read_unlock();
5648 
5649 	/* If error happened on the first frame then we can pass the error up, otherwise
5650 	 * report the number of frames that were xmitted.
5651 	 */
5652 	if (err < 0)
5653 		return (nxmit == 0 ? err : nxmit);
5654 
5655 	return nxmit;
5656 }
5657 
5658 static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog,
5659 			struct netlink_ext_ack *extack)
5660 {
5661 	struct bonding *bond = netdev_priv(dev);
5662 	struct list_head *iter;
5663 	struct slave *slave, *rollback_slave;
5664 	struct bpf_prog *old_prog;
5665 	struct netdev_bpf xdp = {
5666 		.command = XDP_SETUP_PROG,
5667 		.flags   = 0,
5668 		.prog    = prog,
5669 		.extack  = extack,
5670 	};
5671 	int err;
5672 
5673 	ASSERT_RTNL();
5674 
5675 	if (!bond_xdp_check(bond, BOND_MODE(bond))) {
5676 		BOND_NL_ERR(dev, extack,
5677 			    "No native XDP support for the current bonding mode");
5678 		return -EOPNOTSUPP;
5679 	}
5680 
5681 	old_prog = bond->xdp_prog;
5682 	bond->xdp_prog = prog;
5683 
5684 	bond_for_each_slave(bond, slave, iter) {
5685 		struct net_device *slave_dev = slave->dev;
5686 
5687 		if (!slave_dev->netdev_ops->ndo_bpf ||
5688 		    !slave_dev->netdev_ops->ndo_xdp_xmit) {
5689 			SLAVE_NL_ERR(dev, slave_dev, extack,
5690 				     "Slave device does not support XDP");
5691 			err = -EOPNOTSUPP;
5692 			goto err;
5693 		}
5694 
5695 		if (dev_xdp_prog_count(slave_dev) > 0) {
5696 			SLAVE_NL_ERR(dev, slave_dev, extack,
5697 				     "Slave has XDP program loaded, please unload before enslaving");
5698 			err = -EOPNOTSUPP;
5699 			goto err;
5700 		}
5701 
5702 		err = dev_xdp_propagate(slave_dev, &xdp);
5703 		if (err < 0) {
5704 			/* ndo_bpf() sets extack error message */
5705 			slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err);
5706 			goto err;
5707 		}
5708 		if (prog)
5709 			bpf_prog_inc(prog);
5710 	}
5711 
5712 	if (prog) {
5713 		static_branch_inc(&bpf_master_redirect_enabled_key);
5714 	} else if (old_prog) {
5715 		bpf_prog_put(old_prog);
5716 		static_branch_dec(&bpf_master_redirect_enabled_key);
5717 	}
5718 
5719 	return 0;
5720 
5721 err:
5722 	/* unwind the program changes */
5723 	bond->xdp_prog = old_prog;
5724 	xdp.prog = old_prog;
5725 	xdp.extack = NULL; /* do not overwrite original error */
5726 
5727 	bond_for_each_slave(bond, rollback_slave, iter) {
5728 		struct net_device *slave_dev = rollback_slave->dev;
5729 		int err_unwind;
5730 
5731 		if (slave == rollback_slave)
5732 			break;
5733 
5734 		err_unwind = dev_xdp_propagate(slave_dev, &xdp);
5735 		if (err_unwind < 0)
5736 			slave_err(dev, slave_dev,
5737 				  "Error %d when unwinding XDP program change\n", err_unwind);
5738 		else if (xdp.prog)
5739 			bpf_prog_inc(xdp.prog);
5740 	}
5741 	return err;
5742 }
5743 
5744 static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5745 {
5746 	switch (xdp->command) {
5747 	case XDP_SETUP_PROG:
5748 		return bond_xdp_set(dev, xdp->prog, xdp->extack);
5749 	default:
5750 		return -EINVAL;
5751 	}
5752 }
5753 
5754 static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
5755 {
5756 	if (speed == 0 || speed == SPEED_UNKNOWN)
5757 		speed = slave->speed;
5758 	else
5759 		speed = min(speed, slave->speed);
5760 
5761 	return speed;
5762 }
5763 
5764 /* Set the BOND_PHC_INDEX flag to notify user space */
5765 static int bond_set_phc_index_flag(struct kernel_hwtstamp_config *kernel_cfg)
5766 {
5767 	struct ifreq *ifr = kernel_cfg->ifr;
5768 	struct hwtstamp_config cfg;
5769 
5770 	if (kernel_cfg->copied_to_user) {
5771 		/* Lower device has a legacy implementation */
5772 		if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
5773 			return -EFAULT;
5774 
5775 		cfg.flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
5776 		if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
5777 			return -EFAULT;
5778 	} else {
5779 		kernel_cfg->flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
5780 	}
5781 
5782 	return 0;
5783 }
5784 
5785 static int bond_hwtstamp_get(struct net_device *dev,
5786 			     struct kernel_hwtstamp_config *cfg)
5787 {
5788 	struct bonding *bond = netdev_priv(dev);
5789 	struct net_device *real_dev;
5790 	int err;
5791 
5792 	real_dev = bond_option_active_slave_get_rcu(bond);
5793 	if (!real_dev)
5794 		return -EOPNOTSUPP;
5795 
5796 	err = generic_hwtstamp_get_lower(real_dev, cfg);
5797 	if (err)
5798 		return err;
5799 
5800 	return bond_set_phc_index_flag(cfg);
5801 }
5802 
5803 static int bond_hwtstamp_set(struct net_device *dev,
5804 			     struct kernel_hwtstamp_config *cfg,
5805 			     struct netlink_ext_ack *extack)
5806 {
5807 	struct bonding *bond = netdev_priv(dev);
5808 	struct net_device *real_dev;
5809 	int err;
5810 
5811 	if (!(cfg->flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX))
5812 		return -EOPNOTSUPP;
5813 
5814 	real_dev = bond_option_active_slave_get_rcu(bond);
5815 	if (!real_dev)
5816 		return -EOPNOTSUPP;
5817 
5818 	err = generic_hwtstamp_set_lower(real_dev, cfg, extack);
5819 	if (err)
5820 		return err;
5821 
5822 	return bond_set_phc_index_flag(cfg);
5823 }
5824 
5825 static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
5826 					   struct ethtool_link_ksettings *cmd)
5827 {
5828 	struct bonding *bond = netdev_priv(bond_dev);
5829 	struct list_head *iter;
5830 	struct slave *slave;
5831 	u32 speed = 0;
5832 
5833 	cmd->base.duplex = DUPLEX_UNKNOWN;
5834 	cmd->base.port = PORT_OTHER;
5835 
5836 	/* Since bond_slave_can_tx returns false for all inactive or down slaves, we
5837 	 * do not need to check mode.  Though link speed might not represent
5838 	 * the true receive or transmit bandwidth (not all modes are symmetric)
5839 	 * this is an accurate maximum.
5840 	 */
5841 	bond_for_each_slave(bond, slave, iter) {
5842 		if (bond_slave_can_tx(slave)) {
5843 			bond_update_speed_duplex(slave);
5844 			if (slave->speed != SPEED_UNKNOWN) {
5845 				if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
5846 					speed = bond_mode_bcast_speed(slave,
5847 								      speed);
5848 				else
5849 					speed += slave->speed;
5850 			}
5851 			if (cmd->base.duplex == DUPLEX_UNKNOWN &&
5852 			    slave->duplex != DUPLEX_UNKNOWN)
5853 				cmd->base.duplex = slave->duplex;
5854 		}
5855 	}
5856 	cmd->base.speed = speed ? : SPEED_UNKNOWN;
5857 
5858 	return 0;
5859 }
5860 
5861 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
5862 				     struct ethtool_drvinfo *drvinfo)
5863 {
5864 	strscpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
5865 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
5866 		 BOND_ABI_VERSION);
5867 }
5868 
5869 static int bond_ethtool_get_ts_info(struct net_device *bond_dev,
5870 				    struct kernel_ethtool_ts_info *info)
5871 {
5872 	struct bonding *bond = netdev_priv(bond_dev);
5873 	struct kernel_ethtool_ts_info ts_info;
5874 	struct net_device *real_dev;
5875 	bool sw_tx_support = false;
5876 	struct list_head *iter;
5877 	struct slave *slave;
5878 	int ret = 0;
5879 
5880 	rcu_read_lock();
5881 	real_dev = bond_option_active_slave_get_rcu(bond);
5882 	dev_hold(real_dev);
5883 	rcu_read_unlock();
5884 
5885 	if (real_dev) {
5886 		ret = ethtool_get_ts_info_by_layer(real_dev, info);
5887 	} else {
5888 		/* Check if all slaves support software tx timestamping */
5889 		rcu_read_lock();
5890 		bond_for_each_slave_rcu(bond, slave, iter) {
5891 			ret = ethtool_get_ts_info_by_layer(slave->dev, &ts_info);
5892 			if (!ret && (ts_info.so_timestamping & SOF_TIMESTAMPING_TX_SOFTWARE)) {
5893 				sw_tx_support = true;
5894 				continue;
5895 			}
5896 
5897 			sw_tx_support = false;
5898 			break;
5899 		}
5900 		rcu_read_unlock();
5901 	}
5902 
5903 	if (sw_tx_support)
5904 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
5905 
5906 	dev_put(real_dev);
5907 	return ret;
5908 }
5909 
5910 static const struct ethtool_ops bond_ethtool_ops = {
5911 	.get_drvinfo		= bond_ethtool_get_drvinfo,
5912 	.get_link		= ethtool_op_get_link,
5913 	.get_link_ksettings	= bond_ethtool_get_link_ksettings,
5914 	.get_ts_info		= bond_ethtool_get_ts_info,
5915 };
5916 
5917 static const struct net_device_ops bond_netdev_ops = {
5918 	.ndo_init		= bond_init,
5919 	.ndo_uninit		= bond_uninit,
5920 	.ndo_open		= bond_open,
5921 	.ndo_stop		= bond_close,
5922 	.ndo_start_xmit		= bond_start_xmit,
5923 	.ndo_select_queue	= bond_select_queue,
5924 	.ndo_get_stats64	= bond_get_stats,
5925 	.ndo_eth_ioctl		= bond_eth_ioctl,
5926 	.ndo_siocbond		= bond_do_ioctl,
5927 	.ndo_siocdevprivate	= bond_siocdevprivate,
5928 	.ndo_change_rx_flags	= bond_change_rx_flags,
5929 	.ndo_set_rx_mode	= bond_set_rx_mode,
5930 	.ndo_change_mtu		= bond_change_mtu,
5931 	.ndo_set_mac_address	= bond_set_mac_address,
5932 	.ndo_neigh_setup	= bond_neigh_setup,
5933 	.ndo_vlan_rx_add_vid	= bond_vlan_rx_add_vid,
5934 	.ndo_vlan_rx_kill_vid	= bond_vlan_rx_kill_vid,
5935 #ifdef CONFIG_NET_POLL_CONTROLLER
5936 	.ndo_netpoll_setup	= bond_netpoll_setup,
5937 	.ndo_netpoll_cleanup	= bond_netpoll_cleanup,
5938 	.ndo_poll_controller	= bond_poll_controller,
5939 #endif
5940 	.ndo_add_slave		= bond_enslave,
5941 	.ndo_del_slave		= bond_release,
5942 	.ndo_fix_features	= bond_fix_features,
5943 	.ndo_features_check	= passthru_features_check,
5944 	.ndo_get_xmit_slave	= bond_xmit_get_slave,
5945 	.ndo_sk_get_lower_dev	= bond_sk_get_lower_dev,
5946 	.ndo_bpf		= bond_xdp,
5947 	.ndo_xdp_xmit           = bond_xdp_xmit,
5948 	.ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave,
5949 	.ndo_hwtstamp_get	= bond_hwtstamp_get,
5950 	.ndo_hwtstamp_set	= bond_hwtstamp_set,
5951 };
5952 
5953 static const struct device_type bond_type = {
5954 	.name = "bond",
5955 };
5956 
5957 static void bond_destructor(struct net_device *bond_dev)
5958 {
5959 	struct bonding *bond = netdev_priv(bond_dev);
5960 
5961 	if (bond->wq)
5962 		destroy_workqueue(bond->wq);
5963 
5964 	free_percpu(bond->rr_tx_counter);
5965 }
5966 
5967 void bond_setup(struct net_device *bond_dev)
5968 {
5969 	struct bonding *bond = netdev_priv(bond_dev);
5970 
5971 	spin_lock_init(&bond->mode_lock);
5972 	bond->params = bonding_defaults;
5973 
5974 	/* Initialize pointers */
5975 	bond->dev = bond_dev;
5976 
5977 	/* Initialize the device entry points */
5978 	ether_setup(bond_dev);
5979 	bond_dev->max_mtu = ETH_MAX_MTU;
5980 	bond_dev->netdev_ops = &bond_netdev_ops;
5981 	bond_dev->ethtool_ops = &bond_ethtool_ops;
5982 
5983 	bond_dev->needs_free_netdev = true;
5984 	bond_dev->priv_destructor = bond_destructor;
5985 
5986 	SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
5987 
5988 	/* Initialize the device options */
5989 	bond_dev->flags |= IFF_MASTER;
5990 	bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
5991 	bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
5992 
5993 #ifdef CONFIG_XFRM_OFFLOAD
5994 	/* set up xfrm device ops (only supported in active-backup right now) */
5995 	bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
5996 	INIT_LIST_HEAD(&bond->ipsec_list);
5997 	mutex_init(&bond->ipsec_lock);
5998 #endif /* CONFIG_XFRM_OFFLOAD */
5999 
6000 	/* don't acquire bond device's netif_tx_lock when transmitting */
6001 	bond_dev->lltx = true;
6002 
6003 	/* Don't allow bond devices to change network namespaces. */
6004 	bond_dev->netns_immutable = true;
6005 
6006 	/* By default, we declare the bond to be fully
6007 	 * VLAN hardware accelerated capable. Special
6008 	 * care is taken in the various xmit functions
6009 	 * when there are slaves that are not hw accel
6010 	 * capable
6011 	 */
6012 
6013 	bond_dev->hw_features = BOND_VLAN_FEATURES |
6014 				NETIF_F_HW_VLAN_CTAG_RX |
6015 				NETIF_F_HW_VLAN_CTAG_FILTER |
6016 				NETIF_F_HW_VLAN_STAG_RX |
6017 				NETIF_F_HW_VLAN_STAG_FILTER;
6018 
6019 	bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
6020 	bond_dev->features |= bond_dev->hw_features;
6021 	bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
6022 	bond_dev->features |= NETIF_F_GSO_PARTIAL;
6023 #ifdef CONFIG_XFRM_OFFLOAD
6024 	bond_dev->hw_features |= BOND_XFRM_FEATURES;
6025 	/* Only enable XFRM features if this is an active-backup config */
6026 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
6027 		bond_dev->features |= BOND_XFRM_FEATURES;
6028 #endif /* CONFIG_XFRM_OFFLOAD */
6029 }
6030 
6031 /* Destroy a bonding device.
6032  * Must be under rtnl_lock when this function is called.
6033  */
6034 static void bond_uninit(struct net_device *bond_dev)
6035 {
6036 	struct bonding *bond = netdev_priv(bond_dev);
6037 	struct list_head *iter;
6038 	struct slave *slave;
6039 
6040 	bond_netpoll_cleanup(bond_dev);
6041 
6042 	/* Release the bonded slaves */
6043 	bond_for_each_slave(bond, slave, iter)
6044 		__bond_release_one(bond_dev, slave->dev, true, true);
6045 	netdev_info(bond_dev, "Released all slaves\n");
6046 
6047 #ifdef CONFIG_XFRM_OFFLOAD
6048 	mutex_destroy(&bond->ipsec_lock);
6049 #endif /* CONFIG_XFRM_OFFLOAD */
6050 
6051 	bond_set_slave_arr(bond, NULL, NULL);
6052 
6053 	list_del_rcu(&bond->bond_list);
6054 
6055 	bond_debug_unregister(bond);
6056 }
6057 
6058 /*------------------------- Module initialization ---------------------------*/
6059 
6060 static int __init bond_check_params(struct bond_params *params)
6061 {
6062 	int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
6063 	struct bond_opt_value newval;
6064 	const struct bond_opt_value *valptr;
6065 	int arp_all_targets_value = 0;
6066 	u16 ad_actor_sys_prio = 0;
6067 	u16 ad_user_port_key = 0;
6068 	__be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
6069 	int arp_ip_count;
6070 	int bond_mode	= BOND_MODE_ROUNDROBIN;
6071 	int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
6072 	int lacp_fast = 0;
6073 	int tlb_dynamic_lb;
6074 
6075 	/* Convert string parameters. */
6076 	if (mode) {
6077 		bond_opt_initstr(&newval, mode);
6078 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
6079 		if (!valptr) {
6080 			pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
6081 			return -EINVAL;
6082 		}
6083 		bond_mode = valptr->value;
6084 	}
6085 
6086 	if (xmit_hash_policy) {
6087 		if (bond_mode == BOND_MODE_ROUNDROBIN ||
6088 		    bond_mode == BOND_MODE_ACTIVEBACKUP ||
6089 		    bond_mode == BOND_MODE_BROADCAST) {
6090 			pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
6091 				bond_mode_name(bond_mode));
6092 		} else {
6093 			bond_opt_initstr(&newval, xmit_hash_policy);
6094 			valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
6095 						&newval);
6096 			if (!valptr) {
6097 				pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
6098 				       xmit_hash_policy);
6099 				return -EINVAL;
6100 			}
6101 			xmit_hashtype = valptr->value;
6102 		}
6103 	}
6104 
6105 	if (lacp_rate) {
6106 		if (bond_mode != BOND_MODE_8023AD) {
6107 			pr_info("lacp_rate param is irrelevant in mode %s\n",
6108 				bond_mode_name(bond_mode));
6109 		} else {
6110 			bond_opt_initstr(&newval, lacp_rate);
6111 			valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
6112 						&newval);
6113 			if (!valptr) {
6114 				pr_err("Error: Invalid lacp rate \"%s\"\n",
6115 				       lacp_rate);
6116 				return -EINVAL;
6117 			}
6118 			lacp_fast = valptr->value;
6119 		}
6120 	}
6121 
6122 	if (ad_select) {
6123 		bond_opt_initstr(&newval, ad_select);
6124 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
6125 					&newval);
6126 		if (!valptr) {
6127 			pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
6128 			return -EINVAL;
6129 		}
6130 		params->ad_select = valptr->value;
6131 		if (bond_mode != BOND_MODE_8023AD)
6132 			pr_warn("ad_select param only affects 802.3ad mode\n");
6133 	} else {
6134 		params->ad_select = BOND_AD_STABLE;
6135 	}
6136 
6137 	if (max_bonds < 0) {
6138 		pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
6139 			max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
6140 		max_bonds = BOND_DEFAULT_MAX_BONDS;
6141 	}
6142 
6143 	if (miimon < 0) {
6144 		pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6145 			miimon, INT_MAX);
6146 		miimon = 0;
6147 	}
6148 
6149 	if (updelay < 0) {
6150 		pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6151 			updelay, INT_MAX);
6152 		updelay = 0;
6153 	}
6154 
6155 	if (downdelay < 0) {
6156 		pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6157 			downdelay, INT_MAX);
6158 		downdelay = 0;
6159 	}
6160 
6161 	if (use_carrier != 1) {
6162 		pr_err("Error: invalid use_carrier parameter (%d)\n",
6163 		       use_carrier);
6164 		return -EINVAL;
6165 	}
6166 
6167 	if (num_peer_notif < 0 || num_peer_notif > 255) {
6168 		pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
6169 			num_peer_notif);
6170 		num_peer_notif = 1;
6171 	}
6172 
6173 	/* reset values for 802.3ad/TLB/ALB */
6174 	if (!bond_mode_uses_arp(bond_mode)) {
6175 		if (!miimon) {
6176 			pr_warn("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
6177 			pr_warn("Forcing miimon to 100msec\n");
6178 			miimon = BOND_DEFAULT_MIIMON;
6179 		}
6180 	}
6181 
6182 	if (tx_queues < 1 || tx_queues > 255) {
6183 		pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
6184 			tx_queues, BOND_DEFAULT_TX_QUEUES);
6185 		tx_queues = BOND_DEFAULT_TX_QUEUES;
6186 	}
6187 
6188 	if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
6189 		pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
6190 			all_slaves_active);
6191 		all_slaves_active = 0;
6192 	}
6193 
6194 	if (resend_igmp < 0 || resend_igmp > 255) {
6195 		pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
6196 			resend_igmp, BOND_DEFAULT_RESEND_IGMP);
6197 		resend_igmp = BOND_DEFAULT_RESEND_IGMP;
6198 	}
6199 
6200 	bond_opt_initval(&newval, packets_per_slave);
6201 	if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
6202 		pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
6203 			packets_per_slave, USHRT_MAX);
6204 		packets_per_slave = 1;
6205 	}
6206 
6207 	if (bond_mode == BOND_MODE_ALB) {
6208 		pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n",
6209 			  updelay);
6210 	}
6211 
6212 	if (!miimon) {
6213 		if (updelay || downdelay) {
6214 			/* just warn the user the up/down delay will have
6215 			 * no effect since miimon is zero...
6216 			 */
6217 			pr_warn("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n",
6218 				updelay, downdelay);
6219 		}
6220 	} else {
6221 		/* don't allow arp monitoring */
6222 		if (arp_interval) {
6223 			pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
6224 				miimon, arp_interval);
6225 			arp_interval = 0;
6226 		}
6227 
6228 		if ((updelay % miimon) != 0) {
6229 			pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
6230 				updelay, miimon, (updelay / miimon) * miimon);
6231 		}
6232 
6233 		updelay /= miimon;
6234 
6235 		if ((downdelay % miimon) != 0) {
6236 			pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
6237 				downdelay, miimon,
6238 				(downdelay / miimon) * miimon);
6239 		}
6240 
6241 		downdelay /= miimon;
6242 	}
6243 
6244 	if (arp_interval < 0) {
6245 		pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6246 			arp_interval, INT_MAX);
6247 		arp_interval = 0;
6248 	}
6249 
6250 	for (arp_ip_count = 0, i = 0;
6251 	     (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
6252 		__be32 ip;
6253 
6254 		/* not a complete check, but good enough to catch mistakes */
6255 		if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
6256 		    !bond_is_ip_target_ok(ip)) {
6257 			pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
6258 				arp_ip_target[i]);
6259 			arp_interval = 0;
6260 		} else {
6261 			if (bond_get_targets_ip(arp_target, ip) == -1)
6262 				arp_target[arp_ip_count++] = ip;
6263 			else
6264 				pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
6265 					&ip);
6266 		}
6267 	}
6268 
6269 	if (arp_interval && !arp_ip_count) {
6270 		/* don't allow arping if no arp_ip_target given... */
6271 		pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
6272 			arp_interval);
6273 		arp_interval = 0;
6274 	}
6275 
6276 	if (arp_validate) {
6277 		if (!arp_interval) {
6278 			pr_err("arp_validate requires arp_interval\n");
6279 			return -EINVAL;
6280 		}
6281 
6282 		bond_opt_initstr(&newval, arp_validate);
6283 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
6284 					&newval);
6285 		if (!valptr) {
6286 			pr_err("Error: invalid arp_validate \"%s\"\n",
6287 			       arp_validate);
6288 			return -EINVAL;
6289 		}
6290 		arp_validate_value = valptr->value;
6291 	} else {
6292 		arp_validate_value = 0;
6293 	}
6294 
6295 	if (arp_all_targets) {
6296 		bond_opt_initstr(&newval, arp_all_targets);
6297 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
6298 					&newval);
6299 		if (!valptr) {
6300 			pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
6301 			       arp_all_targets);
6302 			arp_all_targets_value = 0;
6303 		} else {
6304 			arp_all_targets_value = valptr->value;
6305 		}
6306 	}
6307 
6308 	if (miimon) {
6309 		pr_info("MII link monitoring set to %d ms\n", miimon);
6310 	} else if (arp_interval) {
6311 		valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
6312 					  arp_validate_value);
6313 		pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
6314 			arp_interval, valptr->string, arp_ip_count);
6315 
6316 		for (i = 0; i < arp_ip_count; i++)
6317 			pr_cont(" %s", arp_ip_target[i]);
6318 
6319 		pr_cont("\n");
6320 
6321 	} else if (max_bonds) {
6322 		/* miimon and arp_interval not set, we need one so things
6323 		 * work as expected, see bonding.txt for details
6324 		 */
6325 		pr_debug("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details\n");
6326 	}
6327 
6328 	if (primary && !bond_mode_uses_primary(bond_mode)) {
6329 		/* currently, using a primary only makes sense
6330 		 * in active backup, TLB or ALB modes
6331 		 */
6332 		pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
6333 			primary, bond_mode_name(bond_mode));
6334 		primary = NULL;
6335 	}
6336 
6337 	if (primary && primary_reselect) {
6338 		bond_opt_initstr(&newval, primary_reselect);
6339 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
6340 					&newval);
6341 		if (!valptr) {
6342 			pr_err("Error: Invalid primary_reselect \"%s\"\n",
6343 			       primary_reselect);
6344 			return -EINVAL;
6345 		}
6346 		primary_reselect_value = valptr->value;
6347 	} else {
6348 		primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
6349 	}
6350 
6351 	if (fail_over_mac) {
6352 		bond_opt_initstr(&newval, fail_over_mac);
6353 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
6354 					&newval);
6355 		if (!valptr) {
6356 			pr_err("Error: invalid fail_over_mac \"%s\"\n",
6357 			       fail_over_mac);
6358 			return -EINVAL;
6359 		}
6360 		fail_over_mac_value = valptr->value;
6361 		if (bond_mode != BOND_MODE_ACTIVEBACKUP)
6362 			pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
6363 	} else {
6364 		fail_over_mac_value = BOND_FOM_NONE;
6365 	}
6366 
6367 	bond_opt_initstr(&newval, "default");
6368 	valptr = bond_opt_parse(
6369 			bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
6370 				     &newval);
6371 	if (!valptr) {
6372 		pr_err("Error: No ad_actor_sys_prio default value");
6373 		return -EINVAL;
6374 	}
6375 	ad_actor_sys_prio = valptr->value;
6376 
6377 	valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
6378 				&newval);
6379 	if (!valptr) {
6380 		pr_err("Error: No ad_user_port_key default value");
6381 		return -EINVAL;
6382 	}
6383 	ad_user_port_key = valptr->value;
6384 
6385 	bond_opt_initstr(&newval, "default");
6386 	valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
6387 	if (!valptr) {
6388 		pr_err("Error: No tlb_dynamic_lb default value");
6389 		return -EINVAL;
6390 	}
6391 	tlb_dynamic_lb = valptr->value;
6392 
6393 	if (lp_interval == 0) {
6394 		pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
6395 			INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
6396 		lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
6397 	}
6398 
6399 	/* fill params struct with the proper values */
6400 	params->mode = bond_mode;
6401 	params->xmit_policy = xmit_hashtype;
6402 	params->miimon = miimon;
6403 	params->num_peer_notif = num_peer_notif;
6404 	params->arp_interval = arp_interval;
6405 	params->arp_validate = arp_validate_value;
6406 	params->arp_all_targets = arp_all_targets_value;
6407 	params->missed_max = 2;
6408 	params->updelay = updelay;
6409 	params->downdelay = downdelay;
6410 	params->peer_notif_delay = 0;
6411 	params->lacp_active = 1;
6412 	params->lacp_fast = lacp_fast;
6413 	params->primary[0] = 0;
6414 	params->primary_reselect = primary_reselect_value;
6415 	params->fail_over_mac = fail_over_mac_value;
6416 	params->tx_queues = tx_queues;
6417 	params->all_slaves_active = all_slaves_active;
6418 	params->resend_igmp = resend_igmp;
6419 	params->min_links = min_links;
6420 	params->lp_interval = lp_interval;
6421 	params->packets_per_slave = packets_per_slave;
6422 	params->tlb_dynamic_lb = tlb_dynamic_lb;
6423 	params->ad_actor_sys_prio = ad_actor_sys_prio;
6424 	eth_zero_addr(params->ad_actor_system);
6425 	params->ad_user_port_key = ad_user_port_key;
6426 	params->coupled_control = 1;
6427 	params->broadcast_neighbor = 0;
6428 	if (packets_per_slave > 0) {
6429 		params->reciprocal_packets_per_slave =
6430 			reciprocal_value(packets_per_slave);
6431 	} else {
6432 		/* reciprocal_packets_per_slave is unused if
6433 		 * packets_per_slave is 0 or 1, just initialize it
6434 		 */
6435 		params->reciprocal_packets_per_slave =
6436 			(struct reciprocal_value) { 0 };
6437 	}
6438 
6439 	if (primary)
6440 		strscpy_pad(params->primary, primary, sizeof(params->primary));
6441 
6442 	memcpy(params->arp_targets, arp_target, sizeof(arp_target));
6443 #if IS_ENABLED(CONFIG_IPV6)
6444 	memset(params->ns_targets, 0, sizeof(struct in6_addr) * BOND_MAX_NS_TARGETS);
6445 #endif
6446 
6447 	return 0;
6448 }
6449 
6450 /* Called from registration process */
6451 static int bond_init(struct net_device *bond_dev)
6452 {
6453 	struct bonding *bond = netdev_priv(bond_dev);
6454 	struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
6455 
6456 	netdev_dbg(bond_dev, "Begin bond_init\n");
6457 
6458 	bond->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
6459 					   bond_dev->name);
6460 	if (!bond->wq)
6461 		return -ENOMEM;
6462 
6463 	bond->notifier_ctx = false;
6464 
6465 	spin_lock_init(&bond->stats_lock);
6466 	netdev_lockdep_set_classes(bond_dev);
6467 
6468 	list_add_tail_rcu(&bond->bond_list, &bn->dev_list);
6469 
6470 	bond_prepare_sysfs_group(bond);
6471 
6472 	bond_debug_register(bond);
6473 
6474 	/* Ensure valid dev_addr */
6475 	if (is_zero_ether_addr(bond_dev->dev_addr) &&
6476 	    bond_dev->addr_assign_type == NET_ADDR_PERM)
6477 		eth_hw_addr_random(bond_dev);
6478 
6479 	return 0;
6480 }
6481 
6482 unsigned int bond_get_num_tx_queues(void)
6483 {
6484 	return tx_queues;
6485 }
6486 
6487 /* Create a new bond based on the specified name and bonding parameters.
6488  * If name is NULL, obtain a suitable "bond%d" name for us.
6489  * Caller must NOT hold rtnl_lock; we need to release it here before we
6490  * set up our sysfs entries.
6491  */
6492 int bond_create(struct net *net, const char *name)
6493 {
6494 	struct net_device *bond_dev;
6495 	struct bonding *bond;
6496 	int res = -ENOMEM;
6497 
6498 	rtnl_lock();
6499 
6500 	bond_dev = alloc_netdev_mq(sizeof(struct bonding),
6501 				   name ? name : "bond%d", NET_NAME_UNKNOWN,
6502 				   bond_setup, tx_queues);
6503 	if (!bond_dev)
6504 		goto out;
6505 
6506 	bond = netdev_priv(bond_dev);
6507 	dev_net_set(bond_dev, net);
6508 	bond_dev->rtnl_link_ops = &bond_link_ops;
6509 
6510 	res = register_netdevice(bond_dev);
6511 	if (res < 0) {
6512 		free_netdev(bond_dev);
6513 		goto out;
6514 	}
6515 
6516 	netif_carrier_off(bond_dev);
6517 
6518 	bond_work_init_all(bond);
6519 
6520 out:
6521 	rtnl_unlock();
6522 	return res;
6523 }
6524 
6525 static int __net_init bond_net_init(struct net *net)
6526 {
6527 	struct bond_net *bn = net_generic(net, bond_net_id);
6528 
6529 	bn->net = net;
6530 	INIT_LIST_HEAD(&bn->dev_list);
6531 
6532 	bond_create_proc_dir(bn);
6533 	bond_create_sysfs(bn);
6534 
6535 	return 0;
6536 }
6537 
6538 /* According to commit 69b0216ac255 ("bonding: fix bonding_masters
6539  * race condition in bond unloading") we need to remove sysfs files
6540  * before we remove our devices (done later in bond_net_exit_rtnl())
6541  */
6542 static void __net_exit bond_net_pre_exit(struct net *net)
6543 {
6544 	struct bond_net *bn = net_generic(net, bond_net_id);
6545 
6546 	bond_destroy_sysfs(bn);
6547 }
6548 
6549 static void __net_exit bond_net_exit_rtnl(struct net *net,
6550 					  struct list_head *dev_kill_list)
6551 {
6552 	struct bond_net *bn = net_generic(net, bond_net_id);
6553 	struct bonding *bond, *tmp_bond;
6554 
6555 	/* Kill off any bonds created after unregistering bond rtnl ops */
6556 	list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
6557 		unregister_netdevice_queue(bond->dev, dev_kill_list);
6558 }
6559 
6560 /* According to commit 23fa5c2caae0 ("bonding: destroy proc directory
6561  * only after all bonds are gone") bond_destroy_proc_dir() is called
6562  * after bond_net_exit_rtnl() has completed.
6563  */
6564 static void __net_exit bond_net_exit_batch(struct list_head *net_list)
6565 {
6566 	struct bond_net *bn;
6567 	struct net *net;
6568 
6569 	list_for_each_entry(net, net_list, exit_list) {
6570 		bn = net_generic(net, bond_net_id);
6571 		bond_destroy_proc_dir(bn);
6572 	}
6573 }
6574 
6575 static struct pernet_operations bond_net_ops = {
6576 	.init = bond_net_init,
6577 	.pre_exit = bond_net_pre_exit,
6578 	.exit_rtnl = bond_net_exit_rtnl,
6579 	.exit_batch = bond_net_exit_batch,
6580 	.id   = &bond_net_id,
6581 	.size = sizeof(struct bond_net),
6582 };
6583 
6584 static int __init bonding_init(void)
6585 {
6586 	int i;
6587 	int res;
6588 
6589 	res = bond_check_params(&bonding_defaults);
6590 	if (res)
6591 		goto out;
6592 
6593 	bond_create_debugfs();
6594 
6595 	res = register_pernet_subsys(&bond_net_ops);
6596 	if (res)
6597 		goto err_net_ops;
6598 
6599 	res = bond_netlink_init();
6600 	if (res)
6601 		goto err_link;
6602 
6603 	for (i = 0; i < max_bonds; i++) {
6604 		res = bond_create(&init_net, NULL);
6605 		if (res)
6606 			goto err;
6607 	}
6608 
6609 	skb_flow_dissector_init(&flow_keys_bonding,
6610 				flow_keys_bonding_keys,
6611 				ARRAY_SIZE(flow_keys_bonding_keys));
6612 
6613 	register_netdevice_notifier(&bond_netdev_notifier);
6614 out:
6615 	return res;
6616 err:
6617 	bond_netlink_fini();
6618 err_link:
6619 	unregister_pernet_subsys(&bond_net_ops);
6620 err_net_ops:
6621 	bond_destroy_debugfs();
6622 	goto out;
6623 
6624 }
6625 
6626 static void __exit bonding_exit(void)
6627 {
6628 	unregister_netdevice_notifier(&bond_netdev_notifier);
6629 
6630 	bond_netlink_fini();
6631 	unregister_pernet_subsys(&bond_net_ops);
6632 
6633 	bond_destroy_debugfs();
6634 
6635 #ifdef CONFIG_NET_POLL_CONTROLLER
6636 	/* Make sure we don't have an imbalance on our netpoll blocking */
6637 	WARN_ON(atomic_read(&netpoll_block_tx));
6638 #endif
6639 }
6640 
6641 module_init(bonding_init);
6642 module_exit(bonding_exit);
6643 MODULE_LICENSE("GPL");
6644 MODULE_DESCRIPTION(DRV_DESCRIPTION);
6645 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
6646 MODULE_IMPORT_NS("NETDEV_INTERNAL");
6647