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