File Index
Files and directories
Sign in
FILE

bond_main.c

File preview with syntax highlighting.

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