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