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 WRITE_ONCE(bond->send_peer_notif, bond->send_peer_notif - 1);
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 WRITE_ONCE(bond->slave_cnt, bond->slave_cnt + 1);
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 && oldcurrent == slave) {
2521 /* Note that we hold RTNL over this sequence, so there
2522 * is no concern that another slave add/remove event
2523 * will interfere.
2524 */
2525 bond_select_active_slave(bond);
2526 }
2527
2528 bond_set_carrier(bond);
2529 if (!bond_has_slaves(bond))
2530 eth_hw_addr_random(bond_dev);
2531
2532 unblock_netpoll_tx();
2533 synchronize_rcu();
2534 WRITE_ONCE(bond->slave_cnt, bond->slave_cnt - 1);
2535
2536 if (!bond_has_slaves(bond)) {
2537 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
2538 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
2539 }
2540
2541 netdev_compute_master_upper_features(bond->dev, true);
2542 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2543 (old_features & NETIF_F_VLAN_CHALLENGED))
2544 slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n");
2545
2546 vlan_vids_del_by_dev(slave_dev, bond_dev);
2547
2548 /* If the mode uses primary, then this case was handled above by
2549 * bond_change_active_slave(..., NULL)
2550 */
2551 if (!bond_uses_primary(bond)) {
2552 /* unset promiscuity level from slave
2553 * NOTE: The NETDEV_CHANGEADDR call above may change the value
2554 * of the IFF_PROMISC flag in the bond_dev, but we need the
2555 * value of that flag before that change, as that was the value
2556 * when this slave was attached, so we cache at the start of the
2557 * function and use it here. Same goes for ALLMULTI below
2558 */
2559 if (old_flags & IFF_PROMISC)
2560 dev_set_promiscuity(slave_dev, -1);
2561
2562 /* unset allmulti level from slave */
2563 if (old_flags & IFF_ALLMULTI)
2564 dev_set_allmulti(slave_dev, -1);
2565
2566 if (old_flags & IFF_UP)
2567 bond_hw_addr_flush(bond_dev, slave_dev);
2568 }
2569
2570 slave_disable_netpoll(slave);
2571
2572 /* close slave before restoring its mac address */
2573 dev_close(slave_dev);
2574
2575 slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
2576
2577 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
2578 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2579 /* restore original ("permanent") mac address */
2580 bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
2581 slave->dev->addr_len);
2582 ss.ss_family = slave_dev->type;
2583 dev_set_mac_address(slave_dev, &ss, NULL);
2584 }
2585
2586 if (unregister) {
2587 netdev_lock_ops(slave_dev);
2588 __netif_set_mtu(slave_dev, slave->original_mtu);
2589 netdev_unlock_ops(slave_dev);
2590 } else {
2591 dev_set_mtu(slave_dev, slave->original_mtu);
2592 }
2593
2594 if (!netif_is_bond_master(slave_dev))
2595 slave_dev->priv_flags &= ~IFF_BONDING;
2596
2597 bond_xdp_set_features(bond_dev);
2598 kobject_put(&slave->kobj);
2599
2600 return 0;
2601 }
2602
2603 /* A wrapper used because of ndo_del_link */
bond_release(struct net_device * bond_dev,struct net_device * slave_dev)2604 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
2605 {
2606 return __bond_release_one(bond_dev, slave_dev, false, false);
2607 }
2608
2609 /* First release a slave and then destroy the bond if no more slaves are left.
2610 * Must be under rtnl_lock when this function is called.
2611 */
bond_release_and_destroy(struct net_device * bond_dev,struct net_device * slave_dev)2612 static int bond_release_and_destroy(struct net_device *bond_dev,
2613 struct net_device *slave_dev)
2614 {
2615 struct bonding *bond = netdev_priv(bond_dev);
2616 int ret;
2617
2618 ret = __bond_release_one(bond_dev, slave_dev, false, true);
2619 if (ret == 0 && !bond_has_slaves(bond) &&
2620 bond_dev->reg_state != NETREG_UNREGISTERING) {
2621 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2622 netdev_info(bond_dev, "Destroying bond\n");
2623 bond_remove_proc_entry(bond);
2624 unregister_netdevice(bond_dev);
2625 }
2626 return ret;
2627 }
2628
bond_info_query(struct net_device * bond_dev,struct ifbond * info)2629 static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2630 {
2631 struct bonding *bond = netdev_priv(bond_dev);
2632
2633 bond_fill_ifbond(bond, info);
2634 }
2635
bond_slave_info_query(struct net_device * bond_dev,struct ifslave * info)2636 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2637 {
2638 struct bonding *bond = netdev_priv(bond_dev);
2639 struct list_head *iter;
2640 int i = 0, res = -ENODEV;
2641 struct slave *slave;
2642
2643 bond_for_each_slave(bond, slave, iter) {
2644 if (i++ == (int)info->slave_id) {
2645 res = 0;
2646 bond_fill_ifslave(slave, info);
2647 break;
2648 }
2649 }
2650
2651 return res;
2652 }
2653
2654 /*-------------------------------- Monitoring -------------------------------*/
2655
2656 /* called with rcu_read_lock() */
bond_miimon_inspect(struct bonding * bond)2657 static int bond_miimon_inspect(struct bonding *bond)
2658 {
2659 bool ignore_updelay = false;
2660 int link_state, commit = 0;
2661 struct list_head *iter;
2662 struct slave *slave;
2663
2664 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
2665 ignore_updelay = !rcu_dereference(bond->curr_active_slave);
2666 } else {
2667 struct bond_up_slave *usable_slaves;
2668
2669 usable_slaves = rcu_dereference(bond->usable_slaves);
2670
2671 if (usable_slaves && usable_slaves->count == 0)
2672 ignore_updelay = true;
2673 }
2674
2675 bond_for_each_slave_rcu(bond, slave, iter) {
2676 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2677
2678 link_state = netif_running(slave->dev) &&
2679 netif_carrier_ok(slave->dev);
2680
2681 switch (slave->link) {
2682 case BOND_LINK_UP:
2683 if (link_state)
2684 continue;
2685
2686 bond_propose_link_state(slave, BOND_LINK_FAIL);
2687 commit++;
2688 slave->delay = bond->params.downdelay;
2689 if (slave->delay && net_ratelimit()) {
2690 slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
2691 (BOND_MODE(bond) ==
2692 BOND_MODE_ACTIVEBACKUP) ?
2693 (bond_is_active_slave(slave) ?
2694 "active " : "backup ") : "",
2695 bond->params.downdelay * bond->params.miimon);
2696 }
2697 fallthrough;
2698 case BOND_LINK_FAIL:
2699 if (link_state) {
2700 /* recovered before downdelay expired */
2701 bond_propose_link_state(slave, BOND_LINK_UP);
2702 slave->last_link_up = jiffies;
2703 if (net_ratelimit())
2704 slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
2705 (bond->params.downdelay - slave->delay) *
2706 bond->params.miimon);
2707 commit++;
2708 continue;
2709 }
2710
2711 if (slave->delay <= 0) {
2712 bond_propose_link_state(slave, BOND_LINK_DOWN);
2713 commit++;
2714 continue;
2715 }
2716
2717 slave->delay--;
2718 break;
2719
2720 case BOND_LINK_DOWN:
2721 if (!link_state)
2722 continue;
2723
2724 bond_propose_link_state(slave, BOND_LINK_BACK);
2725 commit++;
2726 slave->delay = bond->params.updelay;
2727
2728 if (slave->delay && net_ratelimit()) {
2729 slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
2730 ignore_updelay ? 0 :
2731 bond->params.updelay *
2732 bond->params.miimon);
2733 }
2734 fallthrough;
2735 case BOND_LINK_BACK:
2736 if (!link_state) {
2737 bond_propose_link_state(slave, BOND_LINK_DOWN);
2738 if (net_ratelimit())
2739 slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
2740 (bond->params.updelay - slave->delay) *
2741 bond->params.miimon);
2742 commit++;
2743 continue;
2744 }
2745
2746 if (ignore_updelay)
2747 slave->delay = 0;
2748
2749 if (slave->delay <= 0) {
2750 bond_propose_link_state(slave, BOND_LINK_UP);
2751 commit++;
2752 ignore_updelay = false;
2753 continue;
2754 }
2755
2756 slave->delay--;
2757 break;
2758 }
2759 }
2760
2761 return commit;
2762 }
2763
bond_miimon_link_change(struct bonding * bond,struct slave * slave,char link)2764 static void bond_miimon_link_change(struct bonding *bond,
2765 struct slave *slave,
2766 char link)
2767 {
2768 switch (BOND_MODE(bond)) {
2769 case BOND_MODE_8023AD:
2770 bond_3ad_handle_link_change(slave, link);
2771 break;
2772 case BOND_MODE_TLB:
2773 case BOND_MODE_ALB:
2774 bond_alb_handle_link_change(bond, slave, link);
2775 break;
2776 case BOND_MODE_XOR:
2777 bond_update_slave_arr(bond, NULL);
2778 break;
2779 }
2780 }
2781
bond_miimon_commit(struct bonding * bond)2782 static void bond_miimon_commit(struct bonding *bond)
2783 {
2784 struct slave *slave, *primary, *active;
2785 bool do_failover = false;
2786 struct list_head *iter;
2787 int err;
2788
2789 ASSERT_RTNL();
2790
2791 bond_for_each_slave(bond, slave, iter) {
2792 switch (slave->link_new_state) {
2793 case BOND_LINK_NOCHANGE:
2794 /* For 802.3ad mode, check current slave speed and
2795 * duplex again in case its port was disabled after
2796 * invalid speed/duplex reporting but recovered before
2797 * link monitoring could make a decision on the actual
2798 * link status
2799 */
2800 if (BOND_MODE(bond) == BOND_MODE_8023AD &&
2801 slave->link == BOND_LINK_UP)
2802 bond_3ad_adapter_speed_duplex_changed(slave);
2803 continue;
2804
2805 case BOND_LINK_UP:
2806 netdev_lock_ops(slave->dev);
2807 err = bond_update_speed_duplex(slave);
2808 netdev_unlock_ops(slave->dev);
2809 if (err && bond_needs_speed_duplex(bond)) {
2810 slave->link = BOND_LINK_DOWN;
2811 if (net_ratelimit())
2812 slave_warn(bond->dev, slave->dev,
2813 "failed to get link speed/duplex\n");
2814 continue;
2815 }
2816 bond_set_slave_link_state(slave, BOND_LINK_UP,
2817 BOND_SLAVE_NOTIFY_NOW);
2818 slave->last_link_up = jiffies;
2819
2820 primary = rtnl_dereference(bond->primary_slave);
2821 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
2822 /* prevent it from being the active one */
2823 bond_set_backup_slave(slave);
2824 } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2825 /* make it immediately active */
2826 bond_set_active_slave(slave);
2827 }
2828
2829 slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
2830 slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
2831 slave->duplex ? "full" : "half");
2832
2833 bond_miimon_link_change(bond, slave, BOND_LINK_UP);
2834
2835 active = rtnl_dereference(bond->curr_active_slave);
2836 if (!active || slave == primary || slave->prio > active->prio)
2837 do_failover = true;
2838
2839 continue;
2840
2841 case BOND_LINK_DOWN:
2842 if (slave->link_failure_count < UINT_MAX)
2843 slave->link_failure_count++;
2844
2845 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
2846 BOND_SLAVE_NOTIFY_NOW);
2847
2848 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
2849 BOND_MODE(bond) == BOND_MODE_8023AD)
2850 bond_set_slave_inactive_flags(slave,
2851 BOND_SLAVE_NOTIFY_NOW);
2852
2853 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
2854
2855 bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
2856
2857 if (slave == rcu_access_pointer(bond->curr_active_slave))
2858 do_failover = true;
2859
2860 continue;
2861
2862 case BOND_LINK_FAIL:
2863 case BOND_LINK_BACK:
2864 slave_dbg(bond->dev, slave->dev, "link_new_state %d on slave\n",
2865 slave->link_new_state);
2866 continue;
2867
2868 default:
2869 slave_err(bond->dev, slave->dev, "invalid link_new_state %d on slave\n",
2870 slave->link_new_state);
2871 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2872
2873 continue;
2874 }
2875 }
2876
2877 if (do_failover) {
2878 block_netpoll_tx();
2879 bond_select_active_slave(bond);
2880 unblock_netpoll_tx();
2881 }
2882
2883 bond_set_carrier(bond);
2884 }
2885
2886 /* bond_mii_monitor
2887 *
2888 * Really a wrapper that splits the mii monitor into two phases: an
2889 * inspection, then (if inspection indicates something needs to be done)
2890 * an acquisition of appropriate locks followed by a commit phase to
2891 * implement whatever link state changes are indicated.
2892 */
bond_mii_monitor(struct work_struct * work)2893 static void bond_mii_monitor(struct work_struct *work)
2894 {
2895 struct bonding *bond = container_of(work, struct bonding,
2896 mii_work.work);
2897 struct list_head *iter;
2898 struct slave *slave;
2899 unsigned long delay;
2900 bool commit;
2901
2902 delay = msecs_to_jiffies(bond->params.miimon);
2903
2904 if (!bond_has_slaves(bond))
2905 goto re_arm;
2906
2907 rcu_read_lock();
2908
2909 commit = !!bond_miimon_inspect(bond);
2910
2911 rcu_read_unlock();
2912
2913 if (commit || READ_ONCE(bond->send_peer_notif)) {
2914 /* Race avoidance with bond_close cancel of workqueue */
2915 if (!rtnl_trylock()) {
2916 delay = 1;
2917 goto re_arm;
2918 }
2919
2920 if (commit) {
2921 bond_for_each_slave(bond, slave, iter) {
2922 bond_commit_link_state(slave,
2923 BOND_SLAVE_NOTIFY_LATER);
2924 }
2925 bond_miimon_commit(bond);
2926 }
2927
2928 if (bond->send_peer_notif)
2929 bond_peer_notify_may_events(bond, true);
2930
2931 rtnl_unlock(); /* might sleep, hold no other locks */
2932 }
2933
2934 re_arm:
2935 if (bond->params.miimon)
2936 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2937 }
2938
bond_upper_dev_walk(struct net_device * upper,struct netdev_nested_priv * priv)2939 static int bond_upper_dev_walk(struct net_device *upper,
2940 struct netdev_nested_priv *priv)
2941 {
2942 __be32 ip = *(__be32 *)priv->data;
2943
2944 return ip == bond_confirm_addr(upper, 0, ip);
2945 }
2946
bond_has_this_ip(struct bonding * bond,__be32 ip)2947 static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
2948 {
2949 struct netdev_nested_priv priv = {
2950 .data = (void *)&ip,
2951 };
2952 bool ret = false;
2953
2954 if (ip == bond_confirm_addr(bond->dev, 0, ip))
2955 return true;
2956
2957 rcu_read_lock();
2958 if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv))
2959 ret = true;
2960 rcu_read_unlock();
2961
2962 return ret;
2963 }
2964
2965 #define BOND_VLAN_PROTO_NONE cpu_to_be16(0xffff)
2966
bond_handle_vlan(struct slave * slave,struct bond_vlan_tag * tags,struct sk_buff * skb)2967 static bool bond_handle_vlan(struct slave *slave, struct bond_vlan_tag *tags,
2968 struct sk_buff *skb)
2969 {
2970 struct net_device *bond_dev = slave->bond->dev;
2971 struct net_device *slave_dev = slave->dev;
2972 struct bond_vlan_tag *outer_tag = tags;
2973
2974 if (!tags || tags->vlan_proto == BOND_VLAN_PROTO_NONE)
2975 return true;
2976
2977 tags++;
2978
2979 /* Go through all the tags backwards and add them to the packet */
2980 while (tags->vlan_proto != BOND_VLAN_PROTO_NONE) {
2981 if (!tags->vlan_id) {
2982 tags++;
2983 continue;
2984 }
2985
2986 slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
2987 ntohs(outer_tag->vlan_proto), tags->vlan_id);
2988 skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
2989 tags->vlan_id);
2990 if (!skb) {
2991 net_err_ratelimited("failed to insert inner VLAN tag\n");
2992 return false;
2993 }
2994
2995 tags++;
2996 }
2997 /* Set the outer tag */
2998 if (outer_tag->vlan_id) {
2999 slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
3000 ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
3001 __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
3002 outer_tag->vlan_id);
3003 }
3004
3005 return true;
3006 }
3007
3008 /* We go to the (large) trouble of VLAN tagging ARP frames because
3009 * switches in VLAN mode (especially if ports are configured as
3010 * "native" to a VLAN) might not pass non-tagged frames.
3011 */
bond_arp_send(struct slave * slave,int arp_op,__be32 dest_ip,__be32 src_ip,struct bond_vlan_tag * tags)3012 static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
3013 __be32 src_ip, struct bond_vlan_tag *tags)
3014 {
3015 struct net_device *bond_dev = slave->bond->dev;
3016 struct net_device *slave_dev = slave->dev;
3017 struct sk_buff *skb;
3018
3019 slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
3020 arp_op, &dest_ip, &src_ip);
3021
3022 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
3023 NULL, slave_dev->dev_addr, NULL);
3024
3025 if (!skb) {
3026 net_err_ratelimited("ARP packet allocation failed\n");
3027 return;
3028 }
3029
3030 if (bond_handle_vlan(slave, tags, skb)) {
3031 slave_update_last_tx(slave);
3032 arp_xmit(skb);
3033 }
3034
3035 return;
3036 }
3037
3038 /* Validate the device path between the @start_dev and the @end_dev.
3039 * The path is valid if the @end_dev is reachable through device
3040 * stacking.
3041 * When the path is validated, collect any vlan information in the
3042 * path.
3043 */
bond_verify_device_path(struct net_device * start_dev,struct net_device * end_dev,int level)3044 struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
3045 struct net_device *end_dev,
3046 int level)
3047 {
3048 struct bond_vlan_tag *tags;
3049 struct net_device *upper;
3050 struct list_head *iter;
3051
3052 if (start_dev == end_dev) {
3053 tags = kzalloc_objs(*tags, level + 1, GFP_ATOMIC);
3054 if (!tags)
3055 return ERR_PTR(-ENOMEM);
3056 tags[level].vlan_proto = BOND_VLAN_PROTO_NONE;
3057 return tags;
3058 }
3059
3060 netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
3061 tags = bond_verify_device_path(upper, end_dev, level + 1);
3062 if (IS_ERR_OR_NULL(tags)) {
3063 if (IS_ERR(tags))
3064 return tags;
3065 continue;
3066 }
3067 if (is_vlan_dev(upper)) {
3068 tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
3069 tags[level].vlan_id = vlan_dev_vlan_id(upper);
3070 }
3071
3072 return tags;
3073 }
3074
3075 return NULL;
3076 }
3077
bond_arp_send_all(struct bonding * bond,struct slave * slave)3078 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
3079 {
3080 struct rtable *rt;
3081 struct bond_vlan_tag *tags;
3082 __be32 *targets = bond->params.arp_targets, addr;
3083 int i;
3084
3085 for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
3086 slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
3087 __func__, &targets[i]);
3088 tags = NULL;
3089
3090 /* Find out through which dev should the packet go */
3091 rt = ip_route_output(dev_net(bond->dev), targets[i], 0, 0, 0,
3092 RT_SCOPE_LINK);
3093 if (IS_ERR(rt)) {
3094 /* there's no route to target - try to send arp
3095 * probe to generate any traffic (arp_validate=0)
3096 */
3097 if (bond->params.arp_validate)
3098 pr_warn_once("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
3099 bond->dev->name,
3100 &targets[i]);
3101 bond_arp_send(slave, ARPOP_REQUEST, targets[i],
3102 0, tags);
3103 continue;
3104 }
3105
3106 /* bond device itself */
3107 if (rt->dst.dev == bond->dev)
3108 goto found;
3109
3110 rcu_read_lock();
3111 tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
3112 rcu_read_unlock();
3113
3114 if (!IS_ERR_OR_NULL(tags))
3115 goto found;
3116
3117 /* Not our device - skip */
3118 slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n",
3119 &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
3120
3121 ip_rt_put(rt);
3122 continue;
3123
3124 found:
3125 addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
3126 ip_rt_put(rt);
3127 bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
3128 kfree(tags);
3129 }
3130 }
3131
bond_validate_arp(struct bonding * bond,struct slave * slave,__be32 sip,__be32 tip)3132 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
3133 {
3134 int i;
3135
3136 if (!sip || !bond_has_this_ip(bond, tip)) {
3137 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
3138 __func__, &sip, &tip);
3139 return;
3140 }
3141
3142 i = bond_get_targets_ip(bond->params.arp_targets, sip);
3143 if (i == -1) {
3144 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
3145 __func__, &sip);
3146 return;
3147 }
3148 WRITE_ONCE(slave->last_rx, jiffies);
3149 WRITE_ONCE(slave->target_last_arp_rx[i], jiffies);
3150 }
3151
bond_arp_rcv(const struct sk_buff * skb,struct bonding * bond,struct slave * slave)3152 static int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
3153 struct slave *slave)
3154 {
3155 struct arphdr *arp = (struct arphdr *)skb->data;
3156 struct slave *curr_active_slave, *curr_arp_slave;
3157 unsigned char *arp_ptr;
3158 __be32 sip, tip;
3159 unsigned int alen;
3160
3161 alen = arp_hdr_len(bond->dev);
3162
3163 if (alen > skb_headlen(skb)) {
3164 arp = kmalloc(alen, GFP_ATOMIC);
3165 if (!arp)
3166 goto out_unlock;
3167 if (skb_copy_bits(skb, 0, arp, alen) < 0)
3168 goto out_unlock;
3169 }
3170
3171 if (arp->ar_hln != bond->dev->addr_len ||
3172 skb->pkt_type == PACKET_OTHERHOST ||
3173 skb->pkt_type == PACKET_LOOPBACK ||
3174 arp->ar_hrd != htons(ARPHRD_ETHER) ||
3175 arp->ar_pro != htons(ETH_P_IP) ||
3176 arp->ar_pln != 4)
3177 goto out_unlock;
3178
3179 arp_ptr = (unsigned char *)(arp + 1);
3180 arp_ptr += bond->dev->addr_len;
3181 memcpy(&sip, arp_ptr, 4);
3182 arp_ptr += 4 + bond->dev->addr_len;
3183 memcpy(&tip, arp_ptr, 4);
3184
3185 slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
3186 __func__, slave->dev->name, bond_slave_state(slave),
3187 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
3188 &sip, &tip);
3189
3190 curr_active_slave = rcu_dereference(bond->curr_active_slave);
3191 curr_arp_slave = rcu_dereference(bond->current_arp_slave);
3192
3193 /* We 'trust' the received ARP enough to validate it if:
3194 *
3195 * (a) the slave receiving the ARP is active (which includes the
3196 * current ARP slave, if any), or
3197 *
3198 * (b) the receiving slave isn't active, but there is a currently
3199 * active slave and it received valid arp reply(s) after it became
3200 * the currently active slave, or
3201 *
3202 * (c) there is an ARP slave that sent an ARP during the prior ARP
3203 * interval, and we receive an ARP reply on any slave. We accept
3204 * these because switch FDB update delays may deliver the ARP
3205 * reply to a slave other than the sender of the ARP request.
3206 *
3207 * Note: for (b), backup slaves are receiving the broadcast ARP
3208 * request, not a reply. This request passes from the sending
3209 * slave through the L2 switch(es) to the receiving slave. Since
3210 * this is checking the request, sip/tip are swapped for
3211 * validation.
3212 *
3213 * This is done to avoid endless looping when we can't reach the
3214 * arp_ip_target and fool ourselves with our own arp requests.
3215 */
3216 if (bond_is_active_slave(slave))
3217 bond_validate_arp(bond, slave, sip, tip);
3218 else if (curr_active_slave &&
3219 time_after(slave_last_rx(bond, curr_active_slave),
3220 curr_active_slave->last_link_up))
3221 bond_validate_arp(bond, slave, tip, sip);
3222 else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
3223 bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
3224 bond_validate_arp(bond, slave, sip, tip);
3225
3226 out_unlock:
3227 if (arp != (struct arphdr *)skb->data)
3228 kfree(arp);
3229 return RX_HANDLER_ANOTHER;
3230 }
3231
3232 #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)3233 static void bond_ns_send(struct slave *slave, const struct in6_addr *daddr,
3234 const struct in6_addr *saddr, struct bond_vlan_tag *tags)
3235 {
3236 struct net_device *bond_dev = slave->bond->dev;
3237 struct net_device *slave_dev = slave->dev;
3238 struct in6_addr mcaddr;
3239 struct sk_buff *skb;
3240
3241 slave_dbg(bond_dev, slave_dev, "NS on slave: dst %pI6c src %pI6c\n",
3242 daddr, saddr);
3243
3244 skb = ndisc_ns_create(slave_dev, daddr, saddr, 0);
3245 if (!skb) {
3246 net_err_ratelimited("NS packet allocation failed\n");
3247 return;
3248 }
3249
3250 addrconf_addr_solict_mult(daddr, &mcaddr);
3251 if (bond_handle_vlan(slave, tags, skb)) {
3252 slave_update_last_tx(slave);
3253 ndisc_send_skb(skb, &mcaddr, saddr);
3254 }
3255 }
3256
bond_ns_send_all(struct bonding * bond,struct slave * slave)3257 static void bond_ns_send_all(struct bonding *bond, struct slave *slave)
3258 {
3259 struct in6_addr *targets = bond->params.ns_targets;
3260 struct bond_vlan_tag *tags;
3261 struct dst_entry *dst;
3262 struct in6_addr saddr;
3263 struct flowi6 fl6;
3264 int i;
3265
3266 for (i = 0; i < BOND_MAX_NS_TARGETS && !ipv6_addr_any(&targets[i]); i++) {
3267 slave_dbg(bond->dev, slave->dev, "%s: target %pI6c\n",
3268 __func__, &targets[i]);
3269 tags = NULL;
3270
3271 /* Find out through which dev should the packet go */
3272 memset(&fl6, 0, sizeof(struct flowi6));
3273 fl6.daddr = targets[i];
3274
3275 dst = ip6_route_output(dev_net(bond->dev), NULL, &fl6);
3276 if (dst->error) {
3277 dst_release(dst);
3278 /* there's no route to target - try to send arp
3279 * probe to generate any traffic (arp_validate=0)
3280 */
3281 if (bond->params.arp_validate)
3282 pr_warn_once("%s: no route to ns_ip6_target %pI6c and arp_validate is set\n",
3283 bond->dev->name,
3284 &targets[i]);
3285 bond_ns_send(slave, &targets[i], &in6addr_any, tags);
3286 continue;
3287 }
3288
3289 /* bond device itself */
3290 if (dst->dev == bond->dev)
3291 goto found;
3292
3293 rcu_read_lock();
3294 tags = bond_verify_device_path(bond->dev, dst->dev, 0);
3295 rcu_read_unlock();
3296
3297 if (!IS_ERR_OR_NULL(tags))
3298 goto found;
3299
3300 /* Not our device - skip */
3301 slave_dbg(bond->dev, slave->dev, "no path to ns_ip6_target %pI6c via dst->dev %s\n",
3302 &targets[i], dst->dev ? dst->dev->name : "NULL");
3303
3304 dst_release(dst);
3305 continue;
3306
3307 found:
3308 if (!ipv6_dev_get_saddr(dev_net(dst->dev), dst->dev, &targets[i], 0, &saddr))
3309 bond_ns_send(slave, &targets[i], &saddr, tags);
3310 else
3311 bond_ns_send(slave, &targets[i], &in6addr_any, tags);
3312
3313 dst_release(dst);
3314 kfree(tags);
3315 }
3316 }
3317
bond_confirm_addr6(struct net_device * dev,struct netdev_nested_priv * priv)3318 static int bond_confirm_addr6(struct net_device *dev,
3319 struct netdev_nested_priv *priv)
3320 {
3321 struct in6_addr *addr = (struct in6_addr *)priv->data;
3322
3323 return ipv6_chk_addr(dev_net(dev), addr, dev, 0);
3324 }
3325
bond_has_this_ip6(struct bonding * bond,struct in6_addr * addr)3326 static bool bond_has_this_ip6(struct bonding *bond, struct in6_addr *addr)
3327 {
3328 struct netdev_nested_priv priv = {
3329 .data = addr,
3330 };
3331 int ret = false;
3332
3333 if (bond_confirm_addr6(bond->dev, &priv))
3334 return true;
3335
3336 rcu_read_lock();
3337 if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_confirm_addr6, &priv))
3338 ret = true;
3339 rcu_read_unlock();
3340
3341 return ret;
3342 }
3343
bond_validate_na(struct bonding * bond,struct slave * slave,struct in6_addr * saddr,struct in6_addr * daddr)3344 static void bond_validate_na(struct bonding *bond, struct slave *slave,
3345 struct in6_addr *saddr, struct in6_addr *daddr)
3346 {
3347 int i;
3348
3349 /* Ignore NAs that:
3350 * 1. Source address is unspecified address.
3351 * 2. Dest address is neither all-nodes multicast address nor
3352 * exist on bond interface.
3353 */
3354 if (ipv6_addr_any(saddr) ||
3355 (!ipv6_addr_equal(daddr, &in6addr_linklocal_allnodes) &&
3356 !bond_has_this_ip6(bond, daddr))) {
3357 slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c tip %pI6c not found\n",
3358 __func__, saddr, daddr);
3359 return;
3360 }
3361
3362 i = bond_get_targets_ip6(bond->params.ns_targets, saddr);
3363 if (i == -1) {
3364 slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c not found in targets\n",
3365 __func__, saddr);
3366 return;
3367 }
3368 WRITE_ONCE(slave->last_rx, jiffies);
3369 WRITE_ONCE(slave->target_last_arp_rx[i], jiffies);
3370 }
3371
bond_na_rcv(const struct sk_buff * skb,struct bonding * bond,struct slave * slave)3372 static int bond_na_rcv(const struct sk_buff *skb, struct bonding *bond,
3373 struct slave *slave)
3374 {
3375 struct slave *curr_active_slave, *curr_arp_slave;
3376 struct in6_addr *saddr, *daddr;
3377 struct {
3378 struct ipv6hdr ip6;
3379 struct icmp6hdr icmp6;
3380 } *combined, _combined;
3381
3382 if (skb->pkt_type == PACKET_OTHERHOST ||
3383 skb->pkt_type == PACKET_LOOPBACK)
3384 goto out;
3385
3386 combined = skb_header_pointer(skb, 0, sizeof(_combined), &_combined);
3387 if (!combined || combined->ip6.nexthdr != NEXTHDR_ICMP ||
3388 (combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
3389 combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
3390 goto out;
3391
3392 saddr = &combined->ip6.saddr;
3393 daddr = &combined->ip6.daddr;
3394
3395 slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI6c tip %pI6c\n",
3396 __func__, slave->dev->name, bond_slave_state(slave),
3397 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
3398 saddr, daddr);
3399
3400 curr_active_slave = rcu_dereference(bond->curr_active_slave);
3401 curr_arp_slave = rcu_dereference(bond->current_arp_slave);
3402
3403 /* We 'trust' the received ARP enough to validate it if:
3404 * see bond_arp_rcv().
3405 */
3406 if (bond_is_active_slave(slave))
3407 bond_validate_na(bond, slave, saddr, daddr);
3408 else if (curr_active_slave &&
3409 time_after(slave_last_rx(bond, curr_active_slave),
3410 curr_active_slave->last_link_up))
3411 bond_validate_na(bond, slave, daddr, saddr);
3412 else if (curr_arp_slave &&
3413 bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
3414 bond_validate_na(bond, slave, saddr, daddr);
3415
3416 out:
3417 return RX_HANDLER_ANOTHER;
3418 }
3419 #endif
3420
bond_rcv_validate(const struct sk_buff * skb,struct bonding * bond,struct slave * slave)3421 int bond_rcv_validate(const struct sk_buff *skb, struct bonding *bond,
3422 struct slave *slave)
3423 {
3424 #if IS_ENABLED(CONFIG_IPV6)
3425 bool is_ipv6 = skb->protocol == __cpu_to_be16(ETH_P_IPV6);
3426 #endif
3427 bool is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
3428
3429 slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
3430 __func__, skb->dev->name);
3431
3432 /* Use arp validate logic for both ARP and NS */
3433 if (!slave_do_arp_validate(bond, slave)) {
3434 if ((slave_do_arp_validate_only(bond) && is_arp) ||
3435 #if IS_ENABLED(CONFIG_IPV6)
3436 (slave_do_arp_validate_only(bond) && is_ipv6) ||
3437 #endif
3438 !slave_do_arp_validate_only(bond))
3439 WRITE_ONCE(slave->last_rx, jiffies);
3440 return RX_HANDLER_ANOTHER;
3441 } else if (is_arp) {
3442 return bond_arp_rcv(skb, bond, slave);
3443 #if IS_ENABLED(CONFIG_IPV6)
3444 } else if (is_ipv6 && likely(ipv6_mod_enabled())) {
3445 return bond_na_rcv(skb, bond, slave);
3446 #endif
3447 } else {
3448 return RX_HANDLER_ANOTHER;
3449 }
3450 }
3451
bond_send_validate(struct bonding * bond,struct slave * slave)3452 static void bond_send_validate(struct bonding *bond, struct slave *slave)
3453 {
3454 bond_arp_send_all(bond, slave);
3455 #if IS_ENABLED(CONFIG_IPV6)
3456 if (likely(ipv6_mod_enabled()))
3457 bond_ns_send_all(bond, slave);
3458 #endif
3459 }
3460
3461 /* function to verify if we're in the arp_interval timeslice, returns true if
3462 * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
3463 * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
3464 */
bond_time_in_interval(struct bonding * bond,unsigned long last_act,int mod)3465 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
3466 int mod)
3467 {
3468 int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3469
3470 return time_in_range(jiffies,
3471 last_act - delta_in_ticks,
3472 last_act + mod * delta_in_ticks + delta_in_ticks/2);
3473 }
3474
3475 /* This function is called regularly to monitor each slave's link
3476 * ensuring that traffic is being sent and received when arp monitoring
3477 * is used in load-balancing mode. if the adapter has been dormant, then an
3478 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
3479 * arp monitoring in active backup mode.
3480 */
bond_loadbalance_arp_mon(struct bonding * bond)3481 static void bond_loadbalance_arp_mon(struct bonding *bond)
3482 {
3483 struct slave *slave, *oldcurrent;
3484 struct list_head *iter;
3485 int do_failover = 0, slave_state_changed = 0;
3486
3487 if (!bond_has_slaves(bond))
3488 goto re_arm;
3489
3490 rcu_read_lock();
3491
3492 oldcurrent = rcu_dereference(bond->curr_active_slave);
3493 /* see if any of the previous devices are up now (i.e. they have
3494 * xmt and rcv traffic). the curr_active_slave does not come into
3495 * the picture unless it is null. also, slave->last_link_up is not
3496 * needed here because we send an arp on each slave and give a slave
3497 * as long as it needs to get the tx/rx within the delta.
3498 * TODO: what about up/down delay in arp mode? it wasn't here before
3499 * so it can wait
3500 */
3501 bond_for_each_slave_rcu(bond, slave, iter) {
3502 unsigned long last_tx = slave_last_tx(slave);
3503
3504 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
3505
3506 if (slave->link != BOND_LINK_UP) {
3507 if (bond_time_in_interval(bond, last_tx, 1) &&
3508 bond_time_in_interval(bond, READ_ONCE(slave->last_rx), 1)) {
3509
3510 bond_propose_link_state(slave, BOND_LINK_UP);
3511 slave_state_changed = 1;
3512
3513 /* primary_slave has no meaning in round-robin
3514 * mode. the window of a slave being up and
3515 * curr_active_slave being null after enslaving
3516 * is closed.
3517 */
3518 if (!oldcurrent) {
3519 slave_info(bond->dev, slave->dev, "link status definitely up\n");
3520 do_failover = 1;
3521 } else {
3522 slave_info(bond->dev, slave->dev, "interface is now up\n");
3523 }
3524 }
3525 } else {
3526 /* slave->link == BOND_LINK_UP */
3527
3528 /* not all switches will respond to an arp request
3529 * when the source ip is 0, so don't take the link down
3530 * if we don't know our ip yet
3531 */
3532 if (!bond_time_in_interval(bond, last_tx,
3533 bond->params.missed_max) ||
3534 !bond_time_in_interval(bond, READ_ONCE(slave->last_rx),
3535 bond->params.missed_max)) {
3536
3537 bond_propose_link_state(slave, BOND_LINK_DOWN);
3538 slave_state_changed = 1;
3539
3540 if (slave->link_failure_count < UINT_MAX)
3541 slave->link_failure_count++;
3542
3543 slave_info(bond->dev, slave->dev, "interface is now down\n");
3544
3545 if (slave == oldcurrent)
3546 do_failover = 1;
3547 }
3548 }
3549
3550 /* note: if switch is in round-robin mode, all links
3551 * must tx arp to ensure all links rx an arp - otherwise
3552 * links may oscillate or not come up at all; if switch is
3553 * in something like xor mode, there is nothing we can
3554 * do - all replies will be rx'ed on same link causing slaves
3555 * to be unstable during low/no traffic periods
3556 */
3557 if (bond_slave_is_up(slave))
3558 bond_send_validate(bond, slave);
3559 }
3560
3561 rcu_read_unlock();
3562
3563 if (do_failover || slave_state_changed) {
3564 if (!rtnl_trylock())
3565 goto re_arm;
3566
3567 bond_for_each_slave(bond, slave, iter) {
3568 if (slave->link_new_state != BOND_LINK_NOCHANGE)
3569 slave->link = slave->link_new_state;
3570 }
3571
3572 if (slave_state_changed) {
3573 bond_slave_state_change(bond);
3574 if (BOND_MODE(bond) == BOND_MODE_XOR)
3575 bond_update_slave_arr(bond, NULL);
3576 }
3577 if (do_failover) {
3578 block_netpoll_tx();
3579 bond_select_active_slave(bond);
3580 unblock_netpoll_tx();
3581 }
3582 rtnl_unlock();
3583 }
3584
3585 re_arm:
3586 if (bond->params.arp_interval)
3587 queue_delayed_work(bond->wq, &bond->arp_work,
3588 msecs_to_jiffies(bond->params.arp_interval));
3589 }
3590
3591 /* Called to inspect slaves for active-backup mode ARP monitor link state
3592 * changes. Sets proposed link state in slaves to specify what action
3593 * should take place for the slave. Returns 0 if no changes are found, >0
3594 * if changes to link states must be committed.
3595 *
3596 * Called with rcu_read_lock held.
3597 */
bond_ab_arp_inspect(struct bonding * bond)3598 static int bond_ab_arp_inspect(struct bonding *bond)
3599 {
3600 unsigned long last_tx, last_rx;
3601 struct list_head *iter;
3602 struct slave *slave;
3603 int commit = 0;
3604
3605 bond_for_each_slave_rcu(bond, slave, iter) {
3606 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
3607 last_rx = slave_last_rx(bond, slave);
3608
3609 if (slave->link != BOND_LINK_UP) {
3610 if (bond_time_in_interval(bond, last_rx, 1)) {
3611 bond_propose_link_state(slave, BOND_LINK_UP);
3612 commit++;
3613 } else if (slave->link == BOND_LINK_BACK) {
3614 bond_propose_link_state(slave, BOND_LINK_FAIL);
3615 commit++;
3616 }
3617 continue;
3618 }
3619
3620 /* Give slaves 2*delta after being enslaved or made
3621 * active. This avoids bouncing, as the last receive
3622 * times need a full ARP monitor cycle to be updated.
3623 */
3624 if (bond_time_in_interval(bond, slave->last_link_up, 2))
3625 continue;
3626
3627 /* Backup slave is down if:
3628 * - No current_arp_slave AND
3629 * - more than (missed_max+1)*delta since last receive AND
3630 * - the bond has an IP address
3631 *
3632 * Note: a non-null current_arp_slave indicates
3633 * the curr_active_slave went down and we are
3634 * searching for a new one; under this condition
3635 * we only take the curr_active_slave down - this
3636 * gives each slave a chance to tx/rx traffic
3637 * before being taken out
3638 */
3639 if (!bond_is_active_slave(slave) &&
3640 !rcu_access_pointer(bond->current_arp_slave) &&
3641 !bond_time_in_interval(bond, last_rx, bond->params.missed_max + 1)) {
3642 bond_propose_link_state(slave, BOND_LINK_DOWN);
3643 commit++;
3644 }
3645
3646 /* Active slave is down if:
3647 * - more than missed_max*delta since transmitting OR
3648 * - (more than missed_max*delta since receive AND
3649 * the bond has an IP address)
3650 */
3651 last_tx = slave_last_tx(slave);
3652 if (bond_is_active_slave(slave) &&
3653 (!bond_time_in_interval(bond, last_tx, bond->params.missed_max) ||
3654 !bond_time_in_interval(bond, last_rx, bond->params.missed_max))) {
3655 bond_propose_link_state(slave, BOND_LINK_DOWN);
3656 commit++;
3657 }
3658 }
3659
3660 return commit;
3661 }
3662
3663 /* Called to commit link state changes noted by inspection step of
3664 * active-backup mode ARP monitor.
3665 *
3666 * Called with RTNL hold.
3667 */
bond_ab_arp_commit(struct bonding * bond)3668 static void bond_ab_arp_commit(struct bonding *bond)
3669 {
3670 bool do_failover = false;
3671 struct list_head *iter;
3672 unsigned long last_tx;
3673 struct slave *slave;
3674
3675 bond_for_each_slave(bond, slave, iter) {
3676 switch (slave->link_new_state) {
3677 case BOND_LINK_NOCHANGE:
3678 continue;
3679
3680 case BOND_LINK_UP:
3681 last_tx = slave_last_tx(slave);
3682 if (rtnl_dereference(bond->curr_active_slave) != slave ||
3683 (!rtnl_dereference(bond->curr_active_slave) &&
3684 bond_time_in_interval(bond, last_tx, 1))) {
3685 struct slave *current_arp_slave;
3686
3687 current_arp_slave = rtnl_dereference(bond->current_arp_slave);
3688 bond_set_slave_link_state(slave, BOND_LINK_UP,
3689 BOND_SLAVE_NOTIFY_NOW);
3690 if (current_arp_slave) {
3691 bond_set_slave_inactive_flags(
3692 current_arp_slave,
3693 BOND_SLAVE_NOTIFY_NOW);
3694 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3695 }
3696
3697 slave_info(bond->dev, slave->dev, "link status definitely up\n");
3698
3699 if (!rtnl_dereference(bond->curr_active_slave) ||
3700 slave == rtnl_dereference(bond->primary_slave) ||
3701 slave->prio > rtnl_dereference(bond->curr_active_slave)->prio)
3702 do_failover = true;
3703
3704 }
3705
3706 continue;
3707
3708 case BOND_LINK_DOWN:
3709 if (slave->link_failure_count < UINT_MAX)
3710 slave->link_failure_count++;
3711
3712 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3713 BOND_SLAVE_NOTIFY_NOW);
3714 bond_set_slave_inactive_flags(slave,
3715 BOND_SLAVE_NOTIFY_NOW);
3716
3717 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
3718
3719 if (slave == rtnl_dereference(bond->curr_active_slave)) {
3720 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3721 do_failover = true;
3722 }
3723
3724 continue;
3725
3726 case BOND_LINK_FAIL:
3727 bond_set_slave_link_state(slave, BOND_LINK_FAIL,
3728 BOND_SLAVE_NOTIFY_NOW);
3729 bond_set_slave_inactive_flags(slave,
3730 BOND_SLAVE_NOTIFY_NOW);
3731
3732 /* A slave has just been enslaved and has become
3733 * the current active slave.
3734 */
3735 if (rtnl_dereference(bond->curr_active_slave))
3736 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3737 continue;
3738
3739 default:
3740 slave_err(bond->dev, slave->dev,
3741 "impossible: link_new_state %d on slave\n",
3742 slave->link_new_state);
3743 continue;
3744 }
3745 }
3746
3747 if (do_failover) {
3748 block_netpoll_tx();
3749 bond_select_active_slave(bond);
3750 unblock_netpoll_tx();
3751 }
3752
3753 bond_set_carrier(bond);
3754 }
3755
3756 /* Send ARP probes for active-backup mode ARP monitor.
3757 *
3758 * Called with rcu_read_lock held.
3759 */
bond_ab_arp_probe(struct bonding * bond)3760 static bool bond_ab_arp_probe(struct bonding *bond)
3761 {
3762 struct slave *slave, *before = NULL, *new_slave = NULL,
3763 *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
3764 *curr_active_slave = rcu_dereference(bond->curr_active_slave);
3765 struct list_head *iter;
3766 bool found = false;
3767 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
3768
3769 if (curr_arp_slave && curr_active_slave)
3770 netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
3771 curr_arp_slave->dev->name,
3772 curr_active_slave->dev->name);
3773
3774 if (curr_active_slave) {
3775 bond_send_validate(bond, curr_active_slave);
3776 return should_notify_rtnl;
3777 }
3778
3779 /* if we don't have a curr_active_slave, search for the next available
3780 * backup slave from the current_arp_slave and make it the candidate
3781 * for becoming the curr_active_slave
3782 */
3783
3784 if (!curr_arp_slave) {
3785 curr_arp_slave = bond_first_slave_rcu(bond);
3786 if (!curr_arp_slave)
3787 return should_notify_rtnl;
3788 }
3789
3790 bond_for_each_slave_rcu(bond, slave, iter) {
3791 if (!found && !before && bond_slave_is_up(slave))
3792 before = slave;
3793
3794 if (found && !new_slave && bond_slave_is_up(slave))
3795 new_slave = slave;
3796 /* if the link state is up at this point, we
3797 * mark it down - this can happen if we have
3798 * simultaneous link failures and
3799 * reselect_active_interface doesn't make this
3800 * one the current slave so it is still marked
3801 * up when it is actually down
3802 */
3803 if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
3804 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3805 BOND_SLAVE_NOTIFY_LATER);
3806 if (slave->link_failure_count < UINT_MAX)
3807 slave->link_failure_count++;
3808
3809 bond_set_slave_inactive_flags(slave,
3810 BOND_SLAVE_NOTIFY_LATER);
3811
3812 slave_info(bond->dev, slave->dev, "backup interface is now down\n");
3813 }
3814 if (slave == curr_arp_slave)
3815 found = true;
3816 }
3817
3818 if (!new_slave && before)
3819 new_slave = before;
3820
3821 if (!new_slave)
3822 goto check_state;
3823
3824 bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
3825 BOND_SLAVE_NOTIFY_LATER);
3826 bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
3827 bond_send_validate(bond, new_slave);
3828 new_slave->last_link_up = jiffies;
3829 rcu_assign_pointer(bond->current_arp_slave, new_slave);
3830
3831 check_state:
3832 bond_for_each_slave_rcu(bond, slave, iter) {
3833 if (slave->should_notify || slave->should_notify_link) {
3834 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
3835 break;
3836 }
3837 }
3838 return should_notify_rtnl;
3839 }
3840
bond_activebackup_arp_mon(struct bonding * bond)3841 static void bond_activebackup_arp_mon(struct bonding *bond)
3842 {
3843 bool should_notify_rtnl;
3844 int delta_in_ticks;
3845
3846 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3847
3848 if (!bond_has_slaves(bond))
3849 goto re_arm;
3850
3851 rcu_read_lock();
3852
3853 if (bond_ab_arp_inspect(bond)) {
3854 rcu_read_unlock();
3855
3856 /* Race avoidance with bond_close flush of workqueue */
3857 if (!rtnl_trylock()) {
3858 delta_in_ticks = 1;
3859 goto re_arm;
3860 }
3861
3862 bond_ab_arp_commit(bond);
3863
3864 rtnl_unlock();
3865 rcu_read_lock();
3866 }
3867
3868 should_notify_rtnl = bond_ab_arp_probe(bond);
3869 rcu_read_unlock();
3870
3871 if (READ_ONCE(bond->send_peer_notif) || should_notify_rtnl) {
3872 if (!rtnl_trylock()) {
3873 delta_in_ticks = 1;
3874 goto re_arm;
3875 }
3876
3877 if (bond->send_peer_notif)
3878 bond_peer_notify_may_events(bond, true);
3879
3880 if (should_notify_rtnl) {
3881 bond_slave_state_notify(bond);
3882 bond_slave_link_notify(bond);
3883 }
3884
3885 rtnl_unlock();
3886 }
3887
3888 re_arm:
3889 if (bond->params.arp_interval)
3890 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3891 }
3892
bond_arp_monitor(struct work_struct * work)3893 static void bond_arp_monitor(struct work_struct *work)
3894 {
3895 struct bonding *bond = container_of(work, struct bonding,
3896 arp_work.work);
3897
3898 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
3899 bond_activebackup_arp_mon(bond);
3900 else
3901 bond_loadbalance_arp_mon(bond);
3902 }
3903
3904 /*-------------------------- netdev event handling --------------------------*/
3905
3906 /* Change device name */
bond_event_changename(struct bonding * bond)3907 static int bond_event_changename(struct bonding *bond)
3908 {
3909 bond_remove_proc_entry(bond);
3910 bond_create_proc_entry(bond);
3911
3912 bond_debug_reregister(bond);
3913
3914 return NOTIFY_DONE;
3915 }
3916
bond_master_netdev_event(unsigned long event,struct net_device * bond_dev)3917 static int bond_master_netdev_event(unsigned long event,
3918 struct net_device *bond_dev)
3919 {
3920 struct bonding *event_bond = netdev_priv(bond_dev);
3921
3922 netdev_dbg(bond_dev, "%s called\n", __func__);
3923
3924 switch (event) {
3925 case NETDEV_CHANGENAME:
3926 return bond_event_changename(event_bond);
3927 case NETDEV_UNREGISTER:
3928 bond_remove_proc_entry(event_bond);
3929 #ifdef CONFIG_XFRM_OFFLOAD
3930 xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
3931 #endif /* CONFIG_XFRM_OFFLOAD */
3932 break;
3933 case NETDEV_REGISTER:
3934 bond_create_proc_entry(event_bond);
3935 break;
3936 default:
3937 break;
3938 }
3939
3940 return NOTIFY_DONE;
3941 }
3942
bond_slave_netdev_event(unsigned long event,struct net_device * slave_dev)3943 static int bond_slave_netdev_event(unsigned long event,
3944 struct net_device *slave_dev)
3945 {
3946 struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
3947 struct bonding *bond;
3948 struct net_device *bond_dev;
3949
3950 /* A netdev event can be generated while enslaving a device
3951 * before netdev_rx_handler_register is called in which case
3952 * slave will be NULL
3953 */
3954 if (!slave) {
3955 netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
3956 return NOTIFY_DONE;
3957 }
3958
3959 bond_dev = slave->bond->dev;
3960 bond = slave->bond;
3961 primary = rtnl_dereference(bond->primary_slave);
3962
3963 slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
3964
3965 switch (event) {
3966 case NETDEV_UNREGISTER:
3967 if (bond_dev->type != ARPHRD_ETHER)
3968 bond_release_and_destroy(bond_dev, slave_dev);
3969 else
3970 __bond_release_one(bond_dev, slave_dev, false, true);
3971 break;
3972 case NETDEV_UP:
3973 case NETDEV_CHANGE:
3974 /* For 802.3ad mode only:
3975 * Getting invalid Speed/Duplex values here will put slave
3976 * in weird state. Mark it as link-fail if the link was
3977 * previously up or link-down if it hasn't yet come up, and
3978 * let link-monitoring (miimon) set it right when correct
3979 * speeds/duplex are available.
3980 */
3981 if (bond_update_speed_duplex(slave) &&
3982 BOND_MODE(bond) == BOND_MODE_8023AD) {
3983 if (slave->last_link_up)
3984 slave->link = BOND_LINK_FAIL;
3985 else
3986 slave->link = BOND_LINK_DOWN;
3987 }
3988
3989 if (BOND_MODE(bond) == BOND_MODE_8023AD)
3990 bond_3ad_adapter_speed_duplex_changed(slave);
3991 fallthrough;
3992 case NETDEV_DOWN:
3993 /* Refresh slave-array if applicable!
3994 * If the setup does not use miimon or arpmon (mode-specific!),
3995 * then these events will not cause the slave-array to be
3996 * refreshed. This will cause xmit to use a slave that is not
3997 * usable. Avoid such situation by refeshing the array at these
3998 * events. If these (miimon/arpmon) parameters are configured
3999 * then array gets refreshed twice and that should be fine!
4000 */
4001 if (bond_mode_can_use_xmit_hash(bond))
4002 bond_update_slave_arr(bond, NULL);
4003 break;
4004 case NETDEV_CHANGEMTU:
4005 /* TODO: Should slaves be allowed to
4006 * independently alter their MTU? For
4007 * an active-backup bond, slaves need
4008 * not be the same type of device, so
4009 * MTUs may vary. For other modes,
4010 * slaves arguably should have the
4011 * same MTUs. To do this, we'd need to
4012 * take over the slave's change_mtu
4013 * function for the duration of their
4014 * servitude.
4015 */
4016 break;
4017 case NETDEV_CHANGENAME:
4018 /* we don't care if we don't have primary set */
4019 if (!bond_uses_primary(bond) ||
4020 !bond->params.primary[0])
4021 break;
4022
4023 if (slave == primary) {
4024 /* slave's name changed - he's no longer primary */
4025 RCU_INIT_POINTER(bond->primary_slave, NULL);
4026 } else if (!strcmp(slave_dev->name, bond->params.primary)) {
4027 /* we have a new primary slave */
4028 rcu_assign_pointer(bond->primary_slave, slave);
4029 } else { /* we didn't change primary - exit */
4030 break;
4031 }
4032
4033 netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
4034 primary ? slave_dev->name : "none");
4035
4036 block_netpoll_tx();
4037 bond_select_active_slave(bond);
4038 unblock_netpoll_tx();
4039 break;
4040 case NETDEV_FEAT_CHANGE:
4041 if (!bond->notifier_ctx) {
4042 bond->notifier_ctx = true;
4043 netdev_compute_master_upper_features(bond->dev, true);
4044 bond->notifier_ctx = false;
4045 }
4046 break;
4047 case NETDEV_RESEND_IGMP:
4048 /* Propagate to master device */
4049 call_netdevice_notifiers(event, slave->bond->dev);
4050 break;
4051 case NETDEV_XDP_FEAT_CHANGE:
4052 bond_xdp_set_features(bond_dev);
4053 break;
4054 default:
4055 break;
4056 }
4057
4058 return NOTIFY_DONE;
4059 }
4060
4061 /* bond_netdev_event: handle netdev notifier chain events.
4062 *
4063 * This function receives events for the netdev chain. The caller (an
4064 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
4065 * locks for us to safely manipulate the slave devices (RTNL lock,
4066 * dev_probe_lock).
4067 */
bond_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)4068 static int bond_netdev_event(struct notifier_block *this,
4069 unsigned long event, void *ptr)
4070 {
4071 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
4072
4073 netdev_dbg(event_dev, "%s received %s\n",
4074 __func__, netdev_cmd_to_name(event));
4075
4076 if (!(event_dev->priv_flags & IFF_BONDING))
4077 return NOTIFY_DONE;
4078
4079 if (event_dev->flags & IFF_MASTER) {
4080 int ret;
4081
4082 ret = bond_master_netdev_event(event, event_dev);
4083 if (ret != NOTIFY_DONE)
4084 return ret;
4085 }
4086
4087 if (event_dev->flags & IFF_SLAVE)
4088 return bond_slave_netdev_event(event, event_dev);
4089
4090 return NOTIFY_DONE;
4091 }
4092
4093 static struct notifier_block bond_netdev_notifier = {
4094 .notifier_call = bond_netdev_event,
4095 };
4096
4097 /*---------------------------- Hashing Policies -----------------------------*/
4098
4099 /* Helper to access data in a packet, with or without a backing skb.
4100 * If skb is given the data is linearized if necessary via pskb_may_pull.
4101 */
bond_pull_data(struct sk_buff * skb,const void * data,int hlen,int n)4102 static inline const void *bond_pull_data(struct sk_buff *skb,
4103 const void *data, int hlen, int n)
4104 {
4105 if (likely(n <= hlen))
4106 return data;
4107 else if (skb && likely(pskb_may_pull(skb, n)))
4108 return skb->data;
4109
4110 return NULL;
4111 }
4112
4113 /* L2 hash helper */
bond_eth_hash(struct sk_buff * skb,const void * data,int mhoff,int hlen)4114 static inline u32 bond_eth_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
4115 {
4116 struct ethhdr *ep;
4117
4118 data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
4119 if (!data)
4120 return 0;
4121
4122 ep = (struct ethhdr *)(data + mhoff);
4123 return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto);
4124 }
4125
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)4126 static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk, const void *data,
4127 int hlen, __be16 l2_proto, int *nhoff, int *ip_proto, bool l34)
4128 {
4129 const struct ipv6hdr *iph6;
4130 const struct iphdr *iph;
4131
4132 if (l2_proto == htons(ETH_P_IP)) {
4133 data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph));
4134 if (!data)
4135 return false;
4136
4137 iph = (const struct iphdr *)(data + *nhoff);
4138 iph_to_flow_copy_v4addrs(fk, iph);
4139 *nhoff += iph->ihl << 2;
4140 if (!ip_is_fragment(iph))
4141 *ip_proto = iph->protocol;
4142 } else if (l2_proto == htons(ETH_P_IPV6)) {
4143 data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph6));
4144 if (!data)
4145 return false;
4146
4147 iph6 = (const struct ipv6hdr *)(data + *nhoff);
4148 iph_to_flow_copy_v6addrs(fk, iph6);
4149 *nhoff += sizeof(*iph6);
4150 *ip_proto = iph6->nexthdr;
4151 } else {
4152 return false;
4153 }
4154
4155 if (l34 && *ip_proto >= 0)
4156 fk->ports.ports = skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen);
4157
4158 return true;
4159 }
4160
bond_vlan_srcmac_hash(struct sk_buff * skb,const void * data,int mhoff,int hlen)4161 static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
4162 {
4163 u32 srcmac_vendor = 0, srcmac_dev = 0;
4164 struct ethhdr *mac_hdr;
4165 u16 vlan = 0;
4166 int i;
4167
4168 data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
4169 if (!data)
4170 return 0;
4171 mac_hdr = (struct ethhdr *)(data + mhoff);
4172
4173 for (i = 0; i < 3; i++)
4174 srcmac_vendor = (srcmac_vendor << 8) | mac_hdr->h_source[i];
4175
4176 for (i = 3; i < ETH_ALEN; i++)
4177 srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i];
4178
4179 if (skb && skb_vlan_tag_present(skb))
4180 vlan = skb_vlan_tag_get(skb);
4181
4182 return vlan ^ srcmac_vendor ^ srcmac_dev;
4183 }
4184
4185 /* 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)4186 static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, const void *data,
4187 __be16 l2_proto, int nhoff, int hlen, struct flow_keys *fk)
4188 {
4189 bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
4190 int ip_proto = -1;
4191
4192 switch (bond->params.xmit_policy) {
4193 case BOND_XMIT_POLICY_ENCAP23:
4194 case BOND_XMIT_POLICY_ENCAP34:
4195 memset(fk, 0, sizeof(*fk));
4196 return __skb_flow_dissect(dev_net(bond->dev), skb,
4197 &flow_keys_bonding, fk, data,
4198 l2_proto, nhoff, hlen, 0);
4199 default:
4200 break;
4201 }
4202
4203 fk->ports.ports = 0;
4204 memset(&fk->icmp, 0, sizeof(fk->icmp));
4205 if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34))
4206 return false;
4207
4208 /* ICMP error packets contains at least 8 bytes of the header
4209 * of the packet which generated the error. Use this information
4210 * to correlate ICMP error packets within the same flow which
4211 * generated the error.
4212 */
4213 if (ip_proto == IPPROTO_ICMP || ip_proto == IPPROTO_ICMPV6) {
4214 skb_flow_get_icmp_tci(skb, &fk->icmp, data, nhoff, hlen);
4215 if (ip_proto == IPPROTO_ICMP) {
4216 if (!icmp_is_err(fk->icmp.type))
4217 return true;
4218
4219 nhoff += sizeof(struct icmphdr);
4220 } else if (ip_proto == IPPROTO_ICMPV6) {
4221 if (!icmpv6_is_err(fk->icmp.type))
4222 return true;
4223
4224 nhoff += sizeof(struct icmp6hdr);
4225 }
4226 return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34);
4227 }
4228
4229 return true;
4230 }
4231
bond_ip_hash(u32 hash,struct flow_keys * flow,int xmit_policy)4232 static u32 bond_ip_hash(u32 hash, struct flow_keys *flow, int xmit_policy)
4233 {
4234 hash ^= (__force u32)flow_get_u32_dst(flow) ^
4235 (__force u32)flow_get_u32_src(flow);
4236 hash ^= (hash >> 16);
4237 hash ^= (hash >> 8);
4238
4239 /* discard lowest hash bit to deal with the common even ports pattern */
4240 if (xmit_policy == BOND_XMIT_POLICY_LAYER34 ||
4241 xmit_policy == BOND_XMIT_POLICY_ENCAP34)
4242 return hash >> 1;
4243
4244 return hash;
4245 }
4246
4247 /* Generate hash based on xmit policy. If @skb is given it is used to linearize
4248 * the data as required, but this function can be used without it if the data is
4249 * known to be linear (e.g. with xdp_buff).
4250 */
__bond_xmit_hash(struct bonding * bond,struct sk_buff * skb,const void * data,__be16 l2_proto,int mhoff,int nhoff,int hlen)4251 static u32 __bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, const void *data,
4252 __be16 l2_proto, int mhoff, int nhoff, int hlen)
4253 {
4254 struct flow_keys flow;
4255 u32 hash;
4256
4257 if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC)
4258 return bond_vlan_srcmac_hash(skb, data, mhoff, hlen);
4259
4260 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
4261 !bond_flow_dissect(bond, skb, data, l2_proto, nhoff, hlen, &flow))
4262 return bond_eth_hash(skb, data, mhoff, hlen);
4263
4264 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
4265 bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
4266 hash = bond_eth_hash(skb, data, mhoff, hlen);
4267 } else {
4268 if (flow.icmp.id)
4269 memcpy(&hash, &flow.icmp, sizeof(hash));
4270 else
4271 memcpy(&hash, &flow.ports.ports, sizeof(hash));
4272 }
4273
4274 return bond_ip_hash(hash, &flow, bond->params.xmit_policy);
4275 }
4276
4277 /**
4278 * bond_xmit_hash - generate a hash value based on the xmit policy
4279 * @bond: bonding device
4280 * @skb: buffer to use for headers
4281 *
4282 * This function will extract the necessary headers from the skb buffer and use
4283 * them to generate a hash based on the xmit_policy set in the bonding device
4284 */
bond_xmit_hash(struct bonding * bond,struct sk_buff * skb)4285 u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
4286 {
4287 if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
4288 skb->l4_hash)
4289 return skb->hash;
4290
4291 return __bond_xmit_hash(bond, skb, skb->data, skb->protocol,
4292 0, skb_network_offset(skb),
4293 skb_headlen(skb));
4294 }
4295
4296 /**
4297 * bond_xmit_hash_xdp - generate a hash value based on the xmit policy
4298 * @bond: bonding device
4299 * @xdp: buffer to use for headers
4300 *
4301 * The XDP variant of bond_xmit_hash.
4302 */
bond_xmit_hash_xdp(struct bonding * bond,struct xdp_buff * xdp)4303 static u32 bond_xmit_hash_xdp(struct bonding *bond, struct xdp_buff *xdp)
4304 {
4305 struct ethhdr *eth;
4306
4307 if (xdp->data + sizeof(struct ethhdr) > xdp->data_end)
4308 return 0;
4309
4310 eth = (struct ethhdr *)xdp->data;
4311
4312 return __bond_xmit_hash(bond, NULL, xdp->data, eth->h_proto, 0,
4313 sizeof(struct ethhdr), xdp->data_end - xdp->data);
4314 }
4315
4316 /*-------------------------- Device entry points ----------------------------*/
4317
bond_work_init_all(struct bonding * bond)4318 void bond_work_init_all(struct bonding *bond)
4319 {
4320 /* ndo_stop, bond_close() will try to flush the work under
4321 * the rtnl lock. The workqueue must not block on rtnl lock
4322 * to avoid deadlock.
4323 */
4324 INIT_DELAYED_WORK(&bond->mcast_work,
4325 bond_resend_igmp_join_requests_delayed);
4326 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
4327 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
4328 INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
4329 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
4330 INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
4331 INIT_DELAYED_WORK(&bond->peer_notify_work, bond_peer_notify_handler);
4332 }
4333
bond_work_cancel_all(struct bonding * bond)4334 void bond_work_cancel_all(struct bonding *bond)
4335 {
4336 cancel_delayed_work_sync(&bond->mii_work);
4337 cancel_delayed_work_sync(&bond->arp_work);
4338 cancel_delayed_work_sync(&bond->alb_work);
4339 cancel_delayed_work_sync(&bond->ad_work);
4340 cancel_delayed_work_sync(&bond->mcast_work);
4341 cancel_delayed_work_sync(&bond->slave_arr_work);
4342 cancel_delayed_work_sync(&bond->peer_notify_work);
4343 }
4344
bond_open(struct net_device * bond_dev)4345 static int bond_open(struct net_device *bond_dev)
4346 {
4347 struct bonding *bond = netdev_priv(bond_dev);
4348 struct list_head *iter;
4349 struct slave *slave;
4350
4351 if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN && !bond->rr_tx_counter) {
4352 bond->rr_tx_counter = alloc_percpu(u32);
4353 if (!bond->rr_tx_counter)
4354 return -ENOMEM;
4355 }
4356
4357 /* reset slave->backup and slave->inactive */
4358 if (bond_has_slaves(bond)) {
4359 bond_for_each_slave(bond, slave, iter) {
4360 if (bond_uses_primary(bond) &&
4361 slave != rcu_access_pointer(bond->curr_active_slave)) {
4362 bond_set_slave_inactive_flags(slave,
4363 BOND_SLAVE_NOTIFY_NOW);
4364 } else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
4365 bond_set_slave_active_flags(slave,
4366 BOND_SLAVE_NOTIFY_NOW);
4367 }
4368 }
4369 }
4370
4371 if (bond_is_lb(bond)) {
4372 /* bond_alb_initialize must be called before the timer
4373 * is started.
4374 */
4375 if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
4376 return -ENOMEM;
4377 if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
4378 queue_delayed_work(bond->wq, &bond->alb_work, 0);
4379 }
4380
4381 if (bond->params.miimon) /* link check interval, in milliseconds. */
4382 queue_delayed_work(bond->wq, &bond->mii_work, 0);
4383
4384 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
4385 queue_delayed_work(bond->wq, &bond->arp_work, 0);
4386 WRITE_ONCE(bond->recv_probe, bond_rcv_validate);
4387 }
4388
4389 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4390 queue_delayed_work(bond->wq, &bond->ad_work, 0);
4391 /* register to receive LACPDUs */
4392 WRITE_ONCE(bond->recv_probe, bond_3ad_lacpdu_recv);
4393 bond_3ad_initiate_agg_selection(bond, 1);
4394
4395 bond_for_each_slave(bond, slave, iter)
4396 dev_mc_add(slave->dev, lacpdu_mcast_addr);
4397
4398 if (bond->params.broadcast_neighbor)
4399 static_branch_inc(&bond_bcast_neigh_enabled);
4400 }
4401
4402 if (bond_mode_can_use_xmit_hash(bond))
4403 bond_update_slave_arr(bond, NULL);
4404
4405 return 0;
4406 }
4407
bond_close(struct net_device * bond_dev)4408 static int bond_close(struct net_device *bond_dev)
4409 {
4410 struct bonding *bond = netdev_priv(bond_dev);
4411 struct slave *slave;
4412
4413 bond_work_cancel_all(bond);
4414 WRITE_ONCE(bond->send_peer_notif, 0);
4415 WRITE_ONCE(bond->recv_probe, NULL);
4416
4417 /* Wait for any in-flight RX handlers */
4418 synchronize_net();
4419
4420 if (bond_is_lb(bond))
4421 bond_alb_deinitialize(bond);
4422
4423 if (BOND_MODE(bond) == BOND_MODE_8023AD &&
4424 bond->params.broadcast_neighbor)
4425 static_branch_dec(&bond_bcast_neigh_enabled);
4426
4427 if (bond_uses_primary(bond)) {
4428 rcu_read_lock();
4429 slave = rcu_dereference(bond->curr_active_slave);
4430 if (slave)
4431 bond_hw_addr_flush(bond_dev, slave->dev);
4432 rcu_read_unlock();
4433 } else {
4434 struct list_head *iter;
4435
4436 bond_for_each_slave(bond, slave, iter)
4437 bond_hw_addr_flush(bond_dev, slave->dev);
4438 }
4439
4440 return 0;
4441 }
4442
4443 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
4444 * that some drivers can provide 32bit values only.
4445 */
bond_fold_stats(struct rtnl_link_stats64 * _res,const struct rtnl_link_stats64 * _new,const struct rtnl_link_stats64 * _old)4446 static void bond_fold_stats(struct rtnl_link_stats64 *_res,
4447 const struct rtnl_link_stats64 *_new,
4448 const struct rtnl_link_stats64 *_old)
4449 {
4450 const u64 *new = (const u64 *)_new;
4451 const u64 *old = (const u64 *)_old;
4452 u64 *res = (u64 *)_res;
4453 int i;
4454
4455 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
4456 u64 nv = new[i];
4457 u64 ov = old[i];
4458 s64 delta = nv - ov;
4459
4460 /* detects if this particular field is 32bit only */
4461 if (((nv | ov) >> 32) == 0)
4462 delta = (s64)(s32)((u32)nv - (u32)ov);
4463
4464 /* filter anomalies, some drivers reset their stats
4465 * at down/up events.
4466 */
4467 if (delta > 0)
4468 res[i] += delta;
4469 }
4470 }
4471
4472 #ifdef CONFIG_LOCKDEP
bond_get_lowest_level_rcu(struct net_device * dev)4473 static int bond_get_lowest_level_rcu(struct net_device *dev)
4474 {
4475 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
4476 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
4477 int cur = 0, max = 0;
4478
4479 now = dev;
4480 iter = &dev->adj_list.lower;
4481
4482 while (1) {
4483 next = NULL;
4484 while (1) {
4485 ldev = netdev_next_lower_dev_rcu(now, &iter);
4486 if (!ldev)
4487 break;
4488
4489 next = ldev;
4490 niter = &ldev->adj_list.lower;
4491 dev_stack[cur] = now;
4492 iter_stack[cur++] = iter;
4493 if (max <= cur)
4494 max = cur;
4495 break;
4496 }
4497
4498 if (!next) {
4499 if (!cur)
4500 return max;
4501 next = dev_stack[--cur];
4502 niter = iter_stack[cur];
4503 }
4504
4505 now = next;
4506 iter = niter;
4507 }
4508
4509 return max;
4510 }
4511 #endif
4512
bond_get_stats(struct net_device * bond_dev,struct rtnl_link_stats64 * stats)4513 static void bond_get_stats(struct net_device *bond_dev,
4514 struct rtnl_link_stats64 *stats)
4515 {
4516 struct bonding *bond = netdev_priv(bond_dev);
4517 struct rtnl_link_stats64 temp;
4518 struct list_head *iter;
4519 struct slave *slave;
4520 int nest_level = 0;
4521
4522
4523 rcu_read_lock();
4524 #ifdef CONFIG_LOCKDEP
4525 nest_level = bond_get_lowest_level_rcu(bond_dev);
4526 #endif
4527
4528 spin_lock_nested(&bond->stats_lock, nest_level);
4529 memcpy(stats, &bond->bond_stats, sizeof(*stats));
4530
4531 bond_for_each_slave_rcu(bond, slave, iter) {
4532 const struct rtnl_link_stats64 *new =
4533 dev_get_stats(slave->dev, &temp);
4534
4535 bond_fold_stats(stats, new, &slave->slave_stats);
4536
4537 /* save off the slave stats for the next run */
4538 memcpy(&slave->slave_stats, new, sizeof(*new));
4539 }
4540
4541 memcpy(&bond->bond_stats, stats, sizeof(*stats));
4542 spin_unlock(&bond->stats_lock);
4543 rcu_read_unlock();
4544 }
4545
bond_eth_ioctl(struct net_device * bond_dev,struct ifreq * ifr,int cmd)4546 static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4547 {
4548 struct bonding *bond = netdev_priv(bond_dev);
4549 struct mii_ioctl_data *mii = NULL;
4550
4551 netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd);
4552
4553 switch (cmd) {
4554 case SIOCGMIIPHY:
4555 mii = if_mii(ifr);
4556 if (!mii)
4557 return -EINVAL;
4558
4559 mii->phy_id = 0;
4560 fallthrough;
4561 case SIOCGMIIREG:
4562 /* We do this again just in case we were called by SIOCGMIIREG
4563 * instead of SIOCGMIIPHY.
4564 */
4565 mii = if_mii(ifr);
4566 if (!mii)
4567 return -EINVAL;
4568
4569 if (mii->reg_num == 1) {
4570 mii->val_out = 0;
4571 if (netif_carrier_ok(bond->dev))
4572 mii->val_out = BMSR_LSTATUS;
4573 }
4574
4575 break;
4576 default:
4577 return -EOPNOTSUPP;
4578 }
4579
4580 return 0;
4581 }
4582
bond_do_ioctl(struct net_device * bond_dev,struct ifreq * ifr,int cmd)4583 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4584 {
4585 struct bonding *bond = netdev_priv(bond_dev);
4586 struct net_device *slave_dev = NULL;
4587 struct ifbond k_binfo;
4588 struct ifbond __user *u_binfo = NULL;
4589 struct ifslave k_sinfo;
4590 struct ifslave __user *u_sinfo = NULL;
4591 struct bond_opt_value newval;
4592 struct net *net;
4593 int res = 0;
4594
4595 netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
4596
4597 switch (cmd) {
4598 case SIOCBONDINFOQUERY:
4599 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4600
4601 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
4602 return -EFAULT;
4603
4604 bond_info_query(bond_dev, &k_binfo);
4605 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4606 return -EFAULT;
4607
4608 return 0;
4609 case SIOCBONDSLAVEINFOQUERY:
4610 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4611
4612 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
4613 return -EFAULT;
4614
4615 res = bond_slave_info_query(bond_dev, &k_sinfo);
4616 if (res == 0 &&
4617 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4618 return -EFAULT;
4619
4620 return res;
4621 default:
4622 break;
4623 }
4624
4625 net = dev_net(bond_dev);
4626
4627 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4628 return -EPERM;
4629
4630 slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
4631
4632 if (!slave_dev)
4633 return -ENODEV;
4634
4635 slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
4636
4637 switch (cmd) {
4638 case SIOCBONDENSLAVE:
4639 res = bond_enslave(bond_dev, slave_dev, NULL);
4640 break;
4641 case SIOCBONDRELEASE:
4642 res = bond_release(bond_dev, slave_dev);
4643 break;
4644 case SIOCBONDSETHWADDR:
4645 res = bond_set_dev_addr(bond_dev, slave_dev);
4646 break;
4647 case SIOCBONDCHANGEACTIVE:
4648 bond_opt_initstr(&newval, slave_dev->name);
4649 res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
4650 &newval);
4651 break;
4652 default:
4653 res = -EOPNOTSUPP;
4654 }
4655
4656 return res;
4657 }
4658
bond_siocdevprivate(struct net_device * bond_dev,struct ifreq * ifr,void __user * data,int cmd)4659 static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr,
4660 void __user *data, int cmd)
4661 {
4662 struct ifreq ifrdata = { .ifr_data = data };
4663
4664 switch (cmd) {
4665 case BOND_INFO_QUERY_OLD:
4666 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY);
4667 case BOND_SLAVE_INFO_QUERY_OLD:
4668 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY);
4669 case BOND_ENSLAVE_OLD:
4670 return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE);
4671 case BOND_RELEASE_OLD:
4672 return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE);
4673 case BOND_SETHWADDR_OLD:
4674 return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR);
4675 case BOND_CHANGE_ACTIVE_OLD:
4676 return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE);
4677 }
4678
4679 return -EOPNOTSUPP;
4680 }
4681
bond_change_rx_flags(struct net_device * bond_dev,int change)4682 static void bond_change_rx_flags(struct net_device *bond_dev, int change)
4683 {
4684 struct bonding *bond = netdev_priv(bond_dev);
4685
4686 if (change & IFF_PROMISC)
4687 bond_set_promiscuity(bond,
4688 bond_dev->flags & IFF_PROMISC ? 1 : -1);
4689
4690 if (change & IFF_ALLMULTI)
4691 bond_set_allmulti(bond,
4692 bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
4693 }
4694
bond_set_rx_mode(struct net_device * bond_dev)4695 static void bond_set_rx_mode(struct net_device *bond_dev)
4696 {
4697 struct bonding *bond = netdev_priv(bond_dev);
4698 struct list_head *iter;
4699 struct slave *slave;
4700
4701 rcu_read_lock();
4702 if (bond_uses_primary(bond)) {
4703 slave = rcu_dereference(bond->curr_active_slave);
4704 if (slave) {
4705 dev_uc_sync(slave->dev, bond_dev);
4706 dev_mc_sync(slave->dev, bond_dev);
4707 }
4708 } else {
4709 bond_for_each_slave_rcu(bond, slave, iter) {
4710 dev_uc_sync_multiple(slave->dev, bond_dev);
4711 dev_mc_sync_multiple(slave->dev, bond_dev);
4712 }
4713 }
4714 rcu_read_unlock();
4715 }
4716
bond_neigh_init(struct neighbour * n)4717 static int bond_neigh_init(struct neighbour *n)
4718 {
4719 struct bonding *bond = netdev_priv(n->dev);
4720 const struct net_device_ops *slave_ops;
4721 struct neigh_parms parms;
4722 struct slave *slave;
4723 int ret = 0;
4724
4725 rcu_read_lock();
4726 slave = bond_first_slave_rcu(bond);
4727 if (!slave)
4728 goto out;
4729 slave_ops = slave->dev->netdev_ops;
4730 if (!slave_ops->ndo_neigh_setup)
4731 goto out;
4732
4733 /* TODO: find another way [1] to implement this.
4734 * Passing a zeroed structure is fragile,
4735 * but at least we do not pass garbage.
4736 *
4737 * [1] One way would be that ndo_neigh_setup() never touch
4738 * struct neigh_parms, but propagate the new neigh_setup()
4739 * back to ___neigh_create() / neigh_parms_alloc()
4740 */
4741 memset(&parms, 0, sizeof(parms));
4742 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
4743
4744 if (ret)
4745 goto out;
4746
4747 if (parms.neigh_setup)
4748 ret = parms.neigh_setup(n);
4749 out:
4750 rcu_read_unlock();
4751 return ret;
4752 }
4753
4754 /* The bonding ndo_neigh_setup is called at init time beofre any
4755 * slave exists. So we must declare proxy setup function which will
4756 * be used at run time to resolve the actual slave neigh param setup.
4757 *
4758 * It's also called by master devices (such as vlans) to setup their
4759 * underlying devices. In that case - do nothing, we're already set up from
4760 * our init.
4761 */
bond_neigh_setup(struct net_device * dev,struct neigh_parms * parms)4762 static int bond_neigh_setup(struct net_device *dev,
4763 struct neigh_parms *parms)
4764 {
4765 /* modify only our neigh_parms */
4766 if (parms->dev == dev)
4767 parms->neigh_setup = bond_neigh_init;
4768
4769 return 0;
4770 }
4771
4772 /* 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)4773 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4774 {
4775 struct bonding *bond = netdev_priv(bond_dev);
4776 struct slave *slave, *rollback_slave;
4777 struct list_head *iter;
4778 int res = 0;
4779
4780 netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
4781
4782 bond_for_each_slave(bond, slave, iter) {
4783 slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
4784 slave, slave->dev->netdev_ops->ndo_change_mtu);
4785
4786 res = dev_set_mtu(slave->dev, new_mtu);
4787
4788 if (res) {
4789 /* If we failed to set the slave's mtu to the new value
4790 * we must abort the operation even in ACTIVE_BACKUP
4791 * mode, because if we allow the backup slaves to have
4792 * different mtu values than the active slave we'll
4793 * need to change their mtu when doing a failover. That
4794 * means changing their mtu from timer context, which
4795 * is probably not a good idea.
4796 */
4797 slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
4798 res, new_mtu);
4799 goto unwind;
4800 }
4801 }
4802
4803 WRITE_ONCE(bond_dev->mtu, new_mtu);
4804
4805 return 0;
4806
4807 unwind:
4808 /* unwind from head to the slave that failed */
4809 bond_for_each_slave(bond, rollback_slave, iter) {
4810 int tmp_res;
4811
4812 if (rollback_slave == slave)
4813 break;
4814
4815 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
4816 if (tmp_res)
4817 slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
4818 tmp_res);
4819 }
4820
4821 return res;
4822 }
4823
4824 /* Change HW address
4825 *
4826 * Note that many devices must be down to change the HW address, and
4827 * downing the master releases all slaves. We can make bonds full of
4828 * bonding devices to test this, however.
4829 */
bond_set_mac_address(struct net_device * bond_dev,void * addr)4830 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4831 {
4832 struct bonding *bond = netdev_priv(bond_dev);
4833 struct slave *slave, *rollback_slave;
4834 struct sockaddr_storage *ss = addr, tmp_ss;
4835 struct list_head *iter;
4836 int res = 0;
4837
4838 if (BOND_MODE(bond) == BOND_MODE_ALB)
4839 return bond_alb_set_mac_address(bond_dev, addr);
4840
4841
4842 netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
4843
4844 /* If fail_over_mac is enabled, do nothing and return success.
4845 * Returning an error causes ifenslave to fail.
4846 */
4847 if (bond->params.fail_over_mac &&
4848 BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
4849 return 0;
4850
4851 if (!is_valid_ether_addr(ss->__data))
4852 return -EADDRNOTAVAIL;
4853
4854 bond_for_each_slave(bond, slave, iter) {
4855 slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
4856 __func__, slave);
4857 res = dev_set_mac_address(slave->dev, addr, NULL);
4858 if (res) {
4859 slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
4860 __func__, res);
4861 goto unwind;
4862 }
4863 }
4864
4865 /* success */
4866 dev_addr_set(bond_dev, ss->__data);
4867 return 0;
4868
4869 unwind:
4870 memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
4871 tmp_ss.ss_family = bond_dev->type;
4872
4873 /* unwind from head to the slave that failed */
4874 bond_for_each_slave(bond, rollback_slave, iter) {
4875 int tmp_res;
4876
4877 if (rollback_slave == slave)
4878 break;
4879
4880 tmp_res = dev_set_mac_address(rollback_slave->dev, &tmp_ss, NULL);
4881 if (tmp_res) {
4882 slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
4883 __func__, tmp_res);
4884 }
4885 }
4886
4887 return res;
4888 }
4889
4890 /**
4891 * bond_get_slave_by_id - get xmit slave with slave_id
4892 * @bond: bonding device that is transmitting
4893 * @slave_id: slave id up to slave_cnt-1 through which to transmit
4894 *
4895 * This function tries to get slave with slave_id but in case
4896 * it fails, it tries to find the first available slave for transmission.
4897 */
bond_get_slave_by_id(struct bonding * bond,int slave_id)4898 static struct slave *bond_get_slave_by_id(struct bonding *bond,
4899 int slave_id)
4900 {
4901 struct list_head *iter;
4902 struct slave *slave;
4903 int i = slave_id;
4904
4905 /* Here we start from the slave with slave_id */
4906 bond_for_each_slave_rcu(bond, slave, iter) {
4907 if (--i < 0) {
4908 if (bond_slave_can_tx(slave))
4909 return slave;
4910 }
4911 }
4912
4913 /* Here we start from the first slave up to slave_id */
4914 i = slave_id;
4915 bond_for_each_slave_rcu(bond, slave, iter) {
4916 if (--i < 0)
4917 break;
4918 if (bond_slave_can_tx(slave))
4919 return slave;
4920 }
4921 /* no slave that can tx has been found */
4922 return NULL;
4923 }
4924
4925 /**
4926 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
4927 * @bond: bonding device to use
4928 *
4929 * Based on the value of the bonding device's packets_per_slave parameter
4930 * this function generates a slave id, which is usually used as the next
4931 * slave to transmit through.
4932 */
bond_rr_gen_slave_id(struct bonding * bond)4933 static u32 bond_rr_gen_slave_id(struct bonding *bond)
4934 {
4935 u32 slave_id;
4936 struct reciprocal_value reciprocal_packets_per_slave;
4937 int packets_per_slave = bond->params.packets_per_slave;
4938
4939 switch (packets_per_slave) {
4940 case 0:
4941 slave_id = get_random_u32();
4942 break;
4943 case 1:
4944 slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
4945 break;
4946 default:
4947 reciprocal_packets_per_slave =
4948 bond->params.reciprocal_packets_per_slave;
4949 slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
4950 slave_id = reciprocal_divide(slave_id,
4951 reciprocal_packets_per_slave);
4952 break;
4953 }
4954
4955 return slave_id;
4956 }
4957
bond_xmit_roundrobin_slave_get(struct bonding * bond,struct sk_buff * skb)4958 static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
4959 struct sk_buff *skb)
4960 {
4961 struct slave *slave;
4962 int slave_cnt;
4963 u32 slave_id;
4964
4965 /* Start with the curr_active_slave that joined the bond as the
4966 * default for sending IGMP traffic. For failover purposes one
4967 * needs to maintain some consistency for the interface that will
4968 * send the join/membership reports. The curr_active_slave found
4969 * will send all of this type of traffic.
4970 */
4971 if (skb->protocol == htons(ETH_P_IP)) {
4972 int noff = skb_network_offset(skb);
4973 struct iphdr *iph;
4974
4975 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
4976 goto non_igmp;
4977
4978 iph = ip_hdr(skb);
4979 if (iph->protocol == IPPROTO_IGMP) {
4980 slave = rcu_dereference(bond->curr_active_slave);
4981 if (slave)
4982 return slave;
4983 return bond_get_slave_by_id(bond, 0);
4984 }
4985 }
4986
4987 non_igmp:
4988 slave_cnt = READ_ONCE(bond->slave_cnt);
4989 if (likely(slave_cnt)) {
4990 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
4991 return bond_get_slave_by_id(bond, slave_id);
4992 }
4993 return NULL;
4994 }
4995
bond_xdp_xmit_roundrobin_slave_get(struct bonding * bond,struct xdp_buff * xdp)4996 static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond,
4997 struct xdp_buff *xdp)
4998 {
4999 struct slave *slave;
5000 int slave_cnt;
5001 u32 slave_id;
5002 const struct ethhdr *eth;
5003 void *data = xdp->data;
5004
5005 if (data + sizeof(struct ethhdr) > xdp->data_end)
5006 goto non_igmp;
5007
5008 eth = (struct ethhdr *)data;
5009 data += sizeof(struct ethhdr);
5010
5011 /* See comment on IGMP in bond_xmit_roundrobin_slave_get() */
5012 if (eth->h_proto == htons(ETH_P_IP)) {
5013 const struct iphdr *iph;
5014
5015 if (data + sizeof(struct iphdr) > xdp->data_end)
5016 goto non_igmp;
5017
5018 iph = (struct iphdr *)data;
5019
5020 if (iph->protocol == IPPROTO_IGMP) {
5021 slave = rcu_dereference(bond->curr_active_slave);
5022 if (slave)
5023 return slave;
5024 return bond_get_slave_by_id(bond, 0);
5025 }
5026 }
5027
5028 non_igmp:
5029 slave_cnt = READ_ONCE(bond->slave_cnt);
5030 if (likely(slave_cnt)) {
5031 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
5032 return bond_get_slave_by_id(bond, slave_id);
5033 }
5034 return NULL;
5035 }
5036
bond_xmit_roundrobin(struct sk_buff * skb,struct net_device * bond_dev)5037 static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
5038 struct net_device *bond_dev)
5039 {
5040 struct bonding *bond = netdev_priv(bond_dev);
5041 struct slave *slave;
5042
5043 slave = bond_xmit_roundrobin_slave_get(bond, skb);
5044 if (likely(slave))
5045 return bond_dev_queue_xmit(bond, skb, slave->dev);
5046
5047 return bond_tx_drop(bond_dev, skb);
5048 }
5049
bond_xmit_activebackup_slave_get(struct bonding * bond)5050 static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond)
5051 {
5052 return rcu_dereference(bond->curr_active_slave);
5053 }
5054
5055 /* In active-backup mode, we know that bond->curr_active_slave is always valid if
5056 * the bond has a usable interface.
5057 */
bond_xmit_activebackup(struct sk_buff * skb,struct net_device * bond_dev)5058 static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
5059 struct net_device *bond_dev)
5060 {
5061 struct bonding *bond = netdev_priv(bond_dev);
5062 struct slave *slave;
5063
5064 slave = bond_xmit_activebackup_slave_get(bond);
5065 if (slave)
5066 return bond_dev_queue_xmit(bond, skb, slave->dev);
5067
5068 return bond_tx_drop(bond_dev, skb);
5069 }
5070
5071 /* Use this to update slave_array when (a) it's not appropriate to update
5072 * slave_array right away (note that update_slave_array() may sleep)
5073 * and / or (b) RTNL is not held.
5074 */
bond_slave_arr_work_rearm(struct bonding * bond,unsigned long delay)5075 void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
5076 {
5077 queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
5078 }
5079
5080 /* Slave array work handler. Holds only RTNL */
bond_slave_arr_handler(struct work_struct * work)5081 static void bond_slave_arr_handler(struct work_struct *work)
5082 {
5083 struct bonding *bond = container_of(work, struct bonding,
5084 slave_arr_work.work);
5085 int ret;
5086
5087 if (!rtnl_trylock())
5088 goto err;
5089
5090 ret = bond_update_slave_arr(bond, NULL);
5091 rtnl_unlock();
5092 if (ret) {
5093 pr_warn_ratelimited("Failed to update slave array from WT\n");
5094 goto err;
5095 }
5096 return;
5097
5098 err:
5099 bond_slave_arr_work_rearm(bond, 1);
5100 }
5101
bond_skip_slave(struct bond_up_slave * slaves,struct slave * skipslave)5102 static void bond_skip_slave(struct bond_up_slave *slaves,
5103 struct slave *skipslave)
5104 {
5105 int idx;
5106
5107 /* Rare situation where caller has asked to skip a specific
5108 * slave but allocation failed (most likely!). BTW this is
5109 * only possible when the call is initiated from
5110 * __bond_release_one(). In this situation; overwrite the
5111 * skipslave entry in the array with the last entry from the
5112 * array to avoid a situation where the xmit path may choose
5113 * this to-be-skipped slave to send a packet out.
5114 */
5115 for (idx = 0; slaves && idx < slaves->count; idx++) {
5116 if (skipslave == slaves->arr[idx]) {
5117 slaves->arr[idx] =
5118 slaves->arr[slaves->count - 1];
5119 WRITE_ONCE(slaves->count, slaves->count - 1);
5120 break;
5121 }
5122 }
5123 }
5124
bond_set_slave_arr(struct bonding * bond,struct bond_up_slave * usable_slaves,struct bond_up_slave * all_slaves)5125 static void bond_set_slave_arr(struct bonding *bond,
5126 struct bond_up_slave *usable_slaves,
5127 struct bond_up_slave *all_slaves)
5128 {
5129 struct bond_up_slave *usable, *all;
5130
5131 all = rtnl_dereference(bond->all_slaves);
5132 rcu_assign_pointer(bond->all_slaves, all_slaves);
5133 kfree_rcu(all, rcu);
5134
5135 if (BOND_MODE(bond) == BOND_MODE_BROADCAST) {
5136 kfree_rcu(usable_slaves, rcu);
5137 return;
5138 }
5139
5140 usable = rtnl_dereference(bond->usable_slaves);
5141 rcu_assign_pointer(bond->usable_slaves, usable_slaves);
5142 kfree_rcu(usable, rcu);
5143 }
5144
bond_reset_slave_arr(struct bonding * bond)5145 static void bond_reset_slave_arr(struct bonding *bond)
5146 {
5147 bond_set_slave_arr(bond, NULL, NULL);
5148 }
5149
5150 /* Build the usable slaves array in control path for modes that use xmit-hash
5151 * to determine the slave interface -
5152 * (a) BOND_MODE_8023AD
5153 * (b) BOND_MODE_XOR
5154 * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
5155 *
5156 * The caller is expected to hold RTNL only and NO other lock!
5157 */
bond_update_slave_arr(struct bonding * bond,struct slave * skipslave)5158 int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
5159 {
5160 struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
5161 struct slave *slave;
5162 struct list_head *iter;
5163 int agg_id = 0;
5164 int ret = 0;
5165
5166 might_sleep();
5167
5168 usable_slaves = kzalloc_flex(*usable_slaves, arr, bond->slave_cnt);
5169 all_slaves = kzalloc_flex(*all_slaves, arr, bond->slave_cnt);
5170 if (!usable_slaves || !all_slaves) {
5171 ret = -ENOMEM;
5172 goto out;
5173 }
5174 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
5175 struct ad_info ad_info;
5176
5177 spin_lock_bh(&bond->mode_lock);
5178 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
5179 spin_unlock_bh(&bond->mode_lock);
5180 pr_debug("bond_3ad_get_active_agg_info failed\n");
5181 /* No active aggragator means it's not safe to use
5182 * the previous array.
5183 */
5184 bond_reset_slave_arr(bond);
5185 goto out;
5186 }
5187 spin_unlock_bh(&bond->mode_lock);
5188 agg_id = ad_info.aggregator_id;
5189 }
5190 rcu_read_lock();
5191 bond_for_each_slave(bond, slave, iter) {
5192 if (skipslave == slave)
5193 continue;
5194
5195 all_slaves->arr[all_slaves->count++] = slave;
5196 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
5197 const struct aggregator *agg;
5198
5199 agg = rcu_dereference(SLAVE_AD_INFO(slave)->port.aggregator);
5200 if (!agg || agg->aggregator_identifier != agg_id)
5201 continue;
5202 }
5203 if (!bond_slave_can_tx(slave))
5204 continue;
5205
5206 slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
5207 usable_slaves->count);
5208
5209 usable_slaves->arr[usable_slaves->count++] = slave;
5210 }
5211 rcu_read_unlock();
5212
5213 bond_set_slave_arr(bond, usable_slaves, all_slaves);
5214 return ret;
5215 out:
5216 if (ret != 0 && skipslave) {
5217 bond_skip_slave(rtnl_dereference(bond->all_slaves),
5218 skipslave);
5219 bond_skip_slave(rtnl_dereference(bond->usable_slaves),
5220 skipslave);
5221 }
5222 kfree_rcu(all_slaves, rcu);
5223 kfree_rcu(usable_slaves, rcu);
5224
5225 return ret;
5226 }
5227
bond_xmit_3ad_xor_slave_get(struct bonding * bond,struct sk_buff * skb,struct bond_up_slave * slaves)5228 static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
5229 struct sk_buff *skb,
5230 struct bond_up_slave *slaves)
5231 {
5232 struct slave *slave;
5233 unsigned int count;
5234 u32 hash;
5235
5236 hash = bond_xmit_hash(bond, skb);
5237 count = slaves ? READ_ONCE(slaves->count) : 0;
5238 if (unlikely(!count))
5239 return NULL;
5240
5241 slave = slaves->arr[hash % count];
5242 return slave;
5243 }
5244
bond_xdp_xmit_3ad_xor_slave_get(struct bonding * bond,struct xdp_buff * xdp)5245 static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond,
5246 struct xdp_buff *xdp)
5247 {
5248 struct bond_up_slave *slaves;
5249 unsigned int count;
5250 u32 hash;
5251
5252 hash = bond_xmit_hash_xdp(bond, xdp);
5253 slaves = rcu_dereference(bond->usable_slaves);
5254 count = slaves ? READ_ONCE(slaves->count) : 0;
5255 if (unlikely(!count))
5256 return NULL;
5257
5258 return slaves->arr[hash % count];
5259 }
5260
bond_should_broadcast_neighbor(struct sk_buff * skb,struct net_device * dev)5261 static bool bond_should_broadcast_neighbor(struct sk_buff *skb,
5262 struct net_device *dev)
5263 {
5264 struct bonding *bond = netdev_priv(dev);
5265 struct {
5266 struct ipv6hdr ip6;
5267 struct icmp6hdr icmp6;
5268 } *combined, _combined;
5269
5270 if (!static_branch_unlikely(&bond_bcast_neigh_enabled))
5271 return false;
5272
5273 if (!bond->params.broadcast_neighbor)
5274 return false;
5275
5276 if (skb->protocol == htons(ETH_P_ARP))
5277 return true;
5278
5279 if (skb->protocol == htons(ETH_P_IPV6)) {
5280 combined = skb_header_pointer(skb, skb_mac_header_len(skb),
5281 sizeof(_combined),
5282 &_combined);
5283 if (combined && combined->ip6.nexthdr == NEXTHDR_ICMP &&
5284 (combined->icmp6.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
5285 combined->icmp6.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT))
5286 return true;
5287 }
5288
5289 return false;
5290 }
5291
5292 /* Use this Xmit function for 3AD as well as XOR modes. The current
5293 * usable slave array is formed in the control path. The xmit function
5294 * just calculates hash and sends the packet out.
5295 */
bond_3ad_xor_xmit(struct sk_buff * skb,struct net_device * dev)5296 static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
5297 struct net_device *dev)
5298 {
5299 struct bonding *bond = netdev_priv(dev);
5300 struct bond_up_slave *slaves;
5301 struct slave *slave;
5302
5303 slaves = rcu_dereference(bond->usable_slaves);
5304 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
5305 if (likely(slave))
5306 return bond_dev_queue_xmit(bond, skb, slave->dev);
5307
5308 return bond_tx_drop(dev, skb);
5309 }
5310
5311 /* in broadcast mode, we send everything to all or usable slave interfaces.
5312 * under rcu_read_lock when this function is called.
5313 */
bond_xmit_broadcast(struct sk_buff * skb,struct net_device * bond_dev,bool all_slaves)5314 static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
5315 struct net_device *bond_dev,
5316 bool all_slaves)
5317 {
5318 struct bonding *bond = netdev_priv(bond_dev);
5319 struct bond_up_slave *slaves;
5320 bool xmit_suc = false;
5321 bool skb_used = false;
5322 int slaves_count, i;
5323
5324 if (all_slaves)
5325 slaves = rcu_dereference(bond->all_slaves);
5326 else
5327 slaves = rcu_dereference(bond->usable_slaves);
5328
5329 slaves_count = slaves ? READ_ONCE(slaves->count) : 0;
5330 for (i = 0; i < slaves_count; i++) {
5331 struct slave *slave = slaves->arr[i];
5332 struct sk_buff *skb2;
5333
5334 if (!(bond_slave_is_up(slave) && slave->link == BOND_LINK_UP))
5335 continue;
5336
5337 if (i + 1 == slaves_count) {
5338 skb2 = skb;
5339 skb_used = true;
5340 } else {
5341 skb2 = skb_clone(skb, GFP_ATOMIC);
5342 if (!skb2) {
5343 net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
5344 bond_dev->name, __func__);
5345 continue;
5346 }
5347 }
5348
5349 if (bond_dev_queue_xmit(bond, skb2, slave->dev) == NETDEV_TX_OK)
5350 xmit_suc = true;
5351 }
5352
5353 if (!skb_used)
5354 dev_kfree_skb_any(skb);
5355
5356 if (xmit_suc)
5357 return NETDEV_TX_OK;
5358
5359 dev_core_stats_tx_dropped_inc(bond_dev);
5360 return NET_XMIT_DROP;
5361 }
5362
5363 /*------------------------- Device initialization ---------------------------*/
5364
5365 /* Lookup the slave that corresponds to a qid */
bond_slave_override(struct bonding * bond,struct sk_buff * skb)5366 static inline int bond_slave_override(struct bonding *bond,
5367 struct sk_buff *skb)
5368 {
5369 struct slave *slave = NULL;
5370 struct list_head *iter;
5371
5372 if (!skb_rx_queue_recorded(skb))
5373 return 1;
5374
5375 /* Find out if any slaves have the same mapping as this skb. */
5376 bond_for_each_slave_rcu(bond, slave, iter) {
5377 if (READ_ONCE(slave->queue_id) == skb_get_queue_mapping(skb)) {
5378 if (bond_slave_is_up(slave) &&
5379 slave->link == BOND_LINK_UP) {
5380 bond_dev_queue_xmit(bond, skb, slave->dev);
5381 return 0;
5382 }
5383 /* If the slave isn't UP, use default transmit policy. */
5384 break;
5385 }
5386 }
5387
5388 return 1;
5389 }
5390
5391
bond_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)5392 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
5393 struct net_device *sb_dev)
5394 {
5395 /* This helper function exists to help dev_pick_tx get the correct
5396 * destination queue. Using a helper function skips a call to
5397 * skb_tx_hash and will put the skbs in the queue we expect on their
5398 * way down to the bonding driver.
5399 */
5400 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
5401
5402 /* Save the original txq to restore before passing to the driver */
5403 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
5404
5405 if (unlikely(txq >= dev->real_num_tx_queues)) {
5406 do {
5407 txq -= dev->real_num_tx_queues;
5408 } while (txq >= dev->real_num_tx_queues);
5409 }
5410 return txq;
5411 }
5412
bond_xmit_get_slave(struct net_device * master_dev,struct sk_buff * skb,bool all_slaves)5413 static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
5414 struct sk_buff *skb,
5415 bool all_slaves)
5416 {
5417 struct bonding *bond = netdev_priv(master_dev);
5418 struct bond_up_slave *slaves;
5419 struct slave *slave = NULL;
5420
5421 switch (BOND_MODE(bond)) {
5422 case BOND_MODE_ROUNDROBIN:
5423 slave = bond_xmit_roundrobin_slave_get(bond, skb);
5424 break;
5425 case BOND_MODE_ACTIVEBACKUP:
5426 slave = bond_xmit_activebackup_slave_get(bond);
5427 break;
5428 case BOND_MODE_8023AD:
5429 case BOND_MODE_XOR:
5430 if (all_slaves)
5431 slaves = rcu_dereference(bond->all_slaves);
5432 else
5433 slaves = rcu_dereference(bond->usable_slaves);
5434 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
5435 break;
5436 case BOND_MODE_BROADCAST:
5437 break;
5438 case BOND_MODE_ALB:
5439 slave = bond_xmit_alb_slave_get(bond, skb);
5440 break;
5441 case BOND_MODE_TLB:
5442 slave = bond_xmit_tlb_slave_get(bond, skb);
5443 break;
5444 default:
5445 /* Should never happen, mode already checked */
5446 WARN_ONCE(true, "Unknown bonding mode");
5447 break;
5448 }
5449
5450 if (slave)
5451 return slave->dev;
5452 return NULL;
5453 }
5454
bond_sk_to_flow(struct sock * sk,struct flow_keys * flow)5455 static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow)
5456 {
5457 switch (sk->sk_family) {
5458 #if IS_ENABLED(CONFIG_IPV6)
5459 case AF_INET6:
5460 if (ipv6_only_sock(sk) ||
5461 ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) {
5462 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
5463 flow->addrs.v6addrs.src = inet6_sk(sk)->saddr;
5464 flow->addrs.v6addrs.dst = sk->sk_v6_daddr;
5465 break;
5466 }
5467 fallthrough;
5468 #endif
5469 default: /* AF_INET */
5470 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
5471 flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr;
5472 flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr;
5473 break;
5474 }
5475
5476 flow->ports.src = inet_sk(sk)->inet_sport;
5477 flow->ports.dst = inet_sk(sk)->inet_dport;
5478 }
5479
5480 /**
5481 * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields
5482 * @sk: socket to use for headers
5483 *
5484 * This function will extract the necessary field from the socket and use
5485 * them to generate a hash based on the LAYER34 xmit_policy.
5486 * Assumes that sk is a TCP or UDP socket.
5487 */
bond_sk_hash_l34(struct sock * sk)5488 static u32 bond_sk_hash_l34(struct sock *sk)
5489 {
5490 struct flow_keys flow;
5491 u32 hash;
5492
5493 bond_sk_to_flow(sk, &flow);
5494
5495 /* L4 */
5496 memcpy(&hash, &flow.ports.ports, sizeof(hash));
5497 /* L3 */
5498 return bond_ip_hash(hash, &flow, BOND_XMIT_POLICY_LAYER34);
5499 }
5500
__bond_sk_get_lower_dev(struct bonding * bond,struct sock * sk)5501 static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond,
5502 struct sock *sk)
5503 {
5504 struct bond_up_slave *slaves;
5505 struct slave *slave;
5506 unsigned int count;
5507 u32 hash;
5508
5509 slaves = rcu_dereference(bond->usable_slaves);
5510 count = slaves ? READ_ONCE(slaves->count) : 0;
5511 if (unlikely(!count))
5512 return NULL;
5513
5514 hash = bond_sk_hash_l34(sk);
5515 slave = slaves->arr[hash % count];
5516
5517 return slave->dev;
5518 }
5519
bond_sk_get_lower_dev(struct net_device * dev,struct sock * sk)5520 static struct net_device *bond_sk_get_lower_dev(struct net_device *dev,
5521 struct sock *sk)
5522 {
5523 struct bonding *bond = netdev_priv(dev);
5524 struct net_device *lower = NULL;
5525
5526 rcu_read_lock();
5527 if (bond_sk_check(bond))
5528 lower = __bond_sk_get_lower_dev(bond, sk);
5529 rcu_read_unlock();
5530
5531 return lower;
5532 }
5533
5534 #if IS_ENABLED(CONFIG_TLS_DEVICE)
bond_tls_device_xmit(struct bonding * bond,struct sk_buff * skb,struct net_device * dev)5535 static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb,
5536 struct net_device *dev)
5537 {
5538 struct net_device *tls_netdev = rcu_dereference(tls_get_ctx(skb->sk)->netdev);
5539
5540 /* tls_netdev might become NULL, even if tls_is_skb_tx_device_offloaded
5541 * was true, if tls_device_down is running in parallel, but it's OK,
5542 * because bond_get_slave_by_dev has a NULL check.
5543 */
5544 if (likely(bond_get_slave_by_dev(bond, tls_netdev)))
5545 return bond_dev_queue_xmit(bond, skb, tls_netdev);
5546 return bond_tx_drop(dev, skb);
5547 }
5548 #endif
5549
__bond_start_xmit(struct sk_buff * skb,struct net_device * dev)5550 static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
5551 {
5552 struct bonding *bond = netdev_priv(dev);
5553
5554 if (bond_should_override_tx_queue(bond) &&
5555 !bond_slave_override(bond, skb))
5556 return NETDEV_TX_OK;
5557
5558 #if IS_ENABLED(CONFIG_TLS_DEVICE)
5559 if (tls_is_skb_tx_device_offloaded(skb))
5560 return bond_tls_device_xmit(bond, skb, dev);
5561 #endif
5562
5563 switch (BOND_MODE(bond)) {
5564 case BOND_MODE_ROUNDROBIN:
5565 return bond_xmit_roundrobin(skb, dev);
5566 case BOND_MODE_ACTIVEBACKUP:
5567 return bond_xmit_activebackup(skb, dev);
5568 case BOND_MODE_8023AD:
5569 if (bond_should_broadcast_neighbor(skb, dev))
5570 return bond_xmit_broadcast(skb, dev, false);
5571 fallthrough;
5572 case BOND_MODE_XOR:
5573 return bond_3ad_xor_xmit(skb, dev);
5574 case BOND_MODE_BROADCAST:
5575 return bond_xmit_broadcast(skb, dev, true);
5576 case BOND_MODE_ALB:
5577 return bond_alb_xmit(skb, dev);
5578 case BOND_MODE_TLB:
5579 return bond_tlb_xmit(skb, dev);
5580 default:
5581 /* Should never happen, mode already checked */
5582 netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
5583 WARN_ON_ONCE(1);
5584 return bond_tx_drop(dev, skb);
5585 }
5586 }
5587
bond_start_xmit(struct sk_buff * skb,struct net_device * dev)5588 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
5589 {
5590 struct bonding *bond = netdev_priv(dev);
5591 netdev_tx_t ret = NETDEV_TX_OK;
5592
5593 /* If we risk deadlock from transmitting this in the
5594 * netpoll path, tell netpoll to queue the frame for later tx
5595 */
5596 if (unlikely(is_netpoll_tx_blocked(dev)))
5597 return NETDEV_TX_BUSY;
5598
5599 rcu_read_lock();
5600 if (bond_has_slaves(bond))
5601 ret = __bond_start_xmit(skb, dev);
5602 else
5603 ret = bond_tx_drop(dev, skb);
5604 rcu_read_unlock();
5605
5606 return ret;
5607 }
5608
5609 static struct net_device *
bond_xdp_get_xmit_slave(struct net_device * bond_dev,struct xdp_buff * xdp)5610 bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp)
5611 {
5612 struct bonding *bond = netdev_priv(bond_dev);
5613 struct slave *slave;
5614
5615 /* Caller needs to hold rcu_read_lock() */
5616
5617 switch (BOND_MODE(bond)) {
5618 case BOND_MODE_ROUNDROBIN:
5619 slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp);
5620 break;
5621
5622 case BOND_MODE_ACTIVEBACKUP:
5623 slave = bond_xmit_activebackup_slave_get(bond);
5624 break;
5625
5626 case BOND_MODE_8023AD:
5627 case BOND_MODE_XOR:
5628 slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp);
5629 break;
5630
5631 default:
5632 if (net_ratelimit())
5633 netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n",
5634 BOND_MODE(bond));
5635 return NULL;
5636 }
5637
5638 if (slave)
5639 return slave->dev;
5640
5641 return NULL;
5642 }
5643
bond_xdp_xmit(struct net_device * bond_dev,int n,struct xdp_frame ** frames,u32 flags)5644 static int bond_xdp_xmit(struct net_device *bond_dev,
5645 int n, struct xdp_frame **frames, u32 flags)
5646 {
5647 int nxmit, err = -ENXIO;
5648
5649 rcu_read_lock();
5650
5651 for (nxmit = 0; nxmit < n; nxmit++) {
5652 struct xdp_frame *frame = frames[nxmit];
5653 struct xdp_frame *frames1[] = {frame};
5654 struct net_device *slave_dev;
5655 struct xdp_buff xdp;
5656
5657 xdp_convert_frame_to_buff(frame, &xdp);
5658
5659 slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp);
5660 if (!slave_dev) {
5661 err = -ENXIO;
5662 break;
5663 }
5664
5665 err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags);
5666 if (err < 1)
5667 break;
5668 }
5669
5670 rcu_read_unlock();
5671
5672 /* If error happened on the first frame then we can pass the error up, otherwise
5673 * report the number of frames that were xmitted.
5674 */
5675 if (err < 0)
5676 return (nxmit == 0 ? err : nxmit);
5677
5678 return nxmit;
5679 }
5680
bond_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)5681 static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog,
5682 struct netlink_ext_ack *extack)
5683 {
5684 struct bonding *bond = netdev_priv(dev);
5685 struct list_head *iter;
5686 struct slave *slave, *rollback_slave;
5687 struct bpf_prog *old_prog;
5688 struct netdev_bpf xdp = {
5689 .command = XDP_SETUP_PROG,
5690 .flags = 0,
5691 .prog = prog,
5692 .extack = extack,
5693 };
5694 int err;
5695
5696 ASSERT_RTNL();
5697
5698 if (!bond_xdp_check(bond, BOND_MODE(bond))) {
5699 BOND_NL_ERR(dev, extack,
5700 "No native XDP support for the current bonding mode");
5701 return -EOPNOTSUPP;
5702 }
5703
5704 old_prog = bond->xdp_prog;
5705 bond->xdp_prog = prog;
5706
5707 bond_for_each_slave(bond, slave, iter) {
5708 struct net_device *slave_dev = slave->dev;
5709
5710 if (!slave_dev->netdev_ops->ndo_bpf ||
5711 !slave_dev->netdev_ops->ndo_xdp_xmit) {
5712 SLAVE_NL_ERR(dev, slave_dev, extack,
5713 "Slave device does not support XDP");
5714 err = -EOPNOTSUPP;
5715 goto err;
5716 }
5717
5718 if (dev_xdp_prog_count(slave_dev) > 0) {
5719 SLAVE_NL_ERR(dev, slave_dev, extack,
5720 "Slave has XDP program loaded, please unload before enslaving");
5721 err = -EOPNOTSUPP;
5722 goto err;
5723 }
5724
5725 err = dev_xdp_propagate(slave_dev, &xdp);
5726 if (err < 0) {
5727 /* ndo_bpf() sets extack error message */
5728 slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err);
5729 goto err;
5730 }
5731 if (prog)
5732 bpf_prog_inc(prog);
5733 }
5734
5735 if (prog) {
5736 static_branch_inc(&bpf_master_redirect_enabled_key);
5737 } else if (old_prog) {
5738 bpf_prog_put(old_prog);
5739 static_branch_dec(&bpf_master_redirect_enabled_key);
5740 }
5741
5742 return 0;
5743
5744 err:
5745 /* unwind the program changes */
5746 bond->xdp_prog = old_prog;
5747 xdp.prog = old_prog;
5748 xdp.extack = NULL; /* do not overwrite original error */
5749
5750 bond_for_each_slave(bond, rollback_slave, iter) {
5751 struct net_device *slave_dev = rollback_slave->dev;
5752 int err_unwind;
5753
5754 if (slave == rollback_slave)
5755 break;
5756
5757 err_unwind = dev_xdp_propagate(slave_dev, &xdp);
5758 if (err_unwind < 0)
5759 slave_err(dev, slave_dev,
5760 "Error %d when unwinding XDP program change\n", err_unwind);
5761 else if (xdp.prog)
5762 bpf_prog_inc(xdp.prog);
5763 }
5764 return err;
5765 }
5766
bond_xdp(struct net_device * dev,struct netdev_bpf * xdp)5767 static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5768 {
5769 switch (xdp->command) {
5770 case XDP_SETUP_PROG:
5771 return bond_xdp_set(dev, xdp->prog, xdp->extack);
5772 default:
5773 return -EINVAL;
5774 }
5775 }
5776
bond_mode_bcast_speed(struct slave * slave,u32 speed)5777 static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
5778 {
5779 if (speed == 0 || speed == SPEED_UNKNOWN)
5780 speed = slave->speed;
5781 else
5782 speed = min(speed, slave->speed);
5783
5784 return speed;
5785 }
5786
5787 /* Set the BOND_PHC_INDEX flag to notify user space */
bond_set_phc_index_flag(struct kernel_hwtstamp_config * kernel_cfg)5788 static int bond_set_phc_index_flag(struct kernel_hwtstamp_config *kernel_cfg)
5789 {
5790 struct ifreq *ifr = kernel_cfg->ifr;
5791 struct hwtstamp_config cfg;
5792
5793 if (kernel_cfg->copied_to_user) {
5794 /* Lower device has a legacy implementation */
5795 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
5796 return -EFAULT;
5797
5798 cfg.flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
5799 if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
5800 return -EFAULT;
5801 } else {
5802 kernel_cfg->flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
5803 }
5804
5805 return 0;
5806 }
5807
bond_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * cfg)5808 static int bond_hwtstamp_get(struct net_device *dev,
5809 struct kernel_hwtstamp_config *cfg)
5810 {
5811 struct bonding *bond = netdev_priv(dev);
5812 struct net_device *real_dev;
5813 int err;
5814
5815 real_dev = bond_option_active_slave_get_rcu(bond);
5816 if (!real_dev)
5817 return -EOPNOTSUPP;
5818
5819 err = generic_hwtstamp_get_lower(real_dev, cfg);
5820 if (err)
5821 return err;
5822
5823 return bond_set_phc_index_flag(cfg);
5824 }
5825
bond_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)5826 static int bond_hwtstamp_set(struct net_device *dev,
5827 struct kernel_hwtstamp_config *cfg,
5828 struct netlink_ext_ack *extack)
5829 {
5830 struct bonding *bond = netdev_priv(dev);
5831 struct net_device *real_dev;
5832 int err;
5833
5834 if (!(cfg->flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX))
5835 return -EOPNOTSUPP;
5836
5837 real_dev = bond_option_active_slave_get_rcu(bond);
5838 if (!real_dev)
5839 return -EOPNOTSUPP;
5840
5841 err = generic_hwtstamp_set_lower(real_dev, cfg, extack);
5842 if (err)
5843 return err;
5844
5845 return bond_set_phc_index_flag(cfg);
5846 }
5847
bond_ethtool_get_link_ksettings(struct net_device * bond_dev,struct ethtool_link_ksettings * cmd)5848 static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
5849 struct ethtool_link_ksettings *cmd)
5850 {
5851 struct bonding *bond = netdev_priv(bond_dev);
5852 struct list_head *iter;
5853 struct slave *slave;
5854 u32 speed = 0;
5855
5856 cmd->base.duplex = DUPLEX_UNKNOWN;
5857 cmd->base.port = PORT_OTHER;
5858
5859 /* Since bond_slave_can_tx returns false for all inactive or down slaves, we
5860 * do not need to check mode. Though link speed might not represent
5861 * the true receive or transmit bandwidth (not all modes are symmetric)
5862 * this is an accurate maximum.
5863 */
5864 bond_for_each_slave(bond, slave, iter) {
5865 if (bond_slave_can_tx(slave)) {
5866 netdev_lock_ops(slave->dev);
5867 bond_update_speed_duplex(slave);
5868 netdev_unlock_ops(slave->dev);
5869 if (slave->speed != SPEED_UNKNOWN) {
5870 if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
5871 speed = bond_mode_bcast_speed(slave,
5872 speed);
5873 else
5874 speed += slave->speed;
5875 }
5876 if (cmd->base.duplex == DUPLEX_UNKNOWN &&
5877 slave->duplex != DUPLEX_UNKNOWN)
5878 cmd->base.duplex = slave->duplex;
5879 }
5880 }
5881 cmd->base.speed = speed ? : SPEED_UNKNOWN;
5882
5883 return 0;
5884 }
5885
bond_ethtool_get_drvinfo(struct net_device * bond_dev,struct ethtool_drvinfo * drvinfo)5886 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
5887 struct ethtool_drvinfo *drvinfo)
5888 {
5889 strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
5890 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
5891 BOND_ABI_VERSION);
5892 }
5893
bond_ethtool_get_ts_info(struct net_device * bond_dev,struct kernel_ethtool_ts_info * info)5894 static int bond_ethtool_get_ts_info(struct net_device *bond_dev,
5895 struct kernel_ethtool_ts_info *info)
5896 {
5897 struct bonding *bond = netdev_priv(bond_dev);
5898 struct kernel_ethtool_ts_info ts_info;
5899 struct net_device *real_dev;
5900 bool sw_tx_support = false;
5901 struct list_head *iter;
5902 struct slave *slave;
5903 int ret = 0;
5904
5905 rcu_read_lock();
5906 real_dev = bond_option_active_slave_get_rcu(bond);
5907 dev_hold(real_dev);
5908 rcu_read_unlock();
5909
5910 if (real_dev) {
5911 ret = ethtool_get_ts_info_by_layer(real_dev, info);
5912 } else {
5913 /* Check if all slaves support software tx timestamping */
5914 rcu_read_lock();
5915 bond_for_each_slave_rcu(bond, slave, iter) {
5916 ret = ethtool_get_ts_info_by_layer(slave->dev, &ts_info);
5917 if (!ret && (ts_info.so_timestamping & SOF_TIMESTAMPING_TX_SOFTWARE)) {
5918 sw_tx_support = true;
5919 continue;
5920 }
5921
5922 sw_tx_support = false;
5923 break;
5924 }
5925 rcu_read_unlock();
5926 }
5927
5928 if (sw_tx_support)
5929 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
5930
5931 dev_put(real_dev);
5932 return ret;
5933 }
5934
5935 static const struct ethtool_ops bond_ethtool_ops = {
5936 .get_drvinfo = bond_ethtool_get_drvinfo,
5937 .get_link = ethtool_op_get_link,
5938 .get_link_ksettings = bond_ethtool_get_link_ksettings,
5939 .get_ts_info = bond_ethtool_get_ts_info,
5940 };
5941
5942 static const struct net_device_ops bond_netdev_ops = {
5943 .ndo_init = bond_init,
5944 .ndo_uninit = bond_uninit,
5945 .ndo_open = bond_open,
5946 .ndo_stop = bond_close,
5947 .ndo_start_xmit = bond_start_xmit,
5948 .ndo_select_queue = bond_select_queue,
5949 .ndo_get_stats64 = bond_get_stats,
5950 .ndo_eth_ioctl = bond_eth_ioctl,
5951 .ndo_siocbond = bond_do_ioctl,
5952 .ndo_siocdevprivate = bond_siocdevprivate,
5953 .ndo_change_rx_flags = bond_change_rx_flags,
5954 .ndo_set_rx_mode = bond_set_rx_mode,
5955 .ndo_change_mtu = bond_change_mtu,
5956 .ndo_set_mac_address = bond_set_mac_address,
5957 .ndo_neigh_setup = bond_neigh_setup,
5958 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
5959 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
5960 #ifdef CONFIG_NET_POLL_CONTROLLER
5961 .ndo_netpoll_setup = bond_netpoll_setup,
5962 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
5963 .ndo_poll_controller = bond_poll_controller,
5964 #endif
5965 .ndo_add_slave = bond_enslave,
5966 .ndo_del_slave = bond_release,
5967 .ndo_fix_features = bond_fix_features,
5968 .ndo_features_check = passthru_features_check,
5969 .ndo_get_xmit_slave = bond_xmit_get_slave,
5970 .ndo_sk_get_lower_dev = bond_sk_get_lower_dev,
5971 .ndo_bpf = bond_xdp,
5972 .ndo_xdp_xmit = bond_xdp_xmit,
5973 .ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave,
5974 .ndo_hwtstamp_get = bond_hwtstamp_get,
5975 .ndo_hwtstamp_set = bond_hwtstamp_set,
5976 };
5977
5978 static const struct device_type bond_type = {
5979 .name = "bond",
5980 };
5981
bond_destructor(struct net_device * bond_dev)5982 static void bond_destructor(struct net_device *bond_dev)
5983 {
5984 struct bonding *bond = netdev_priv(bond_dev);
5985
5986 if (bond->wq)
5987 destroy_workqueue(bond->wq);
5988
5989 free_percpu(bond->rr_tx_counter);
5990 }
5991
bond_setup(struct net_device * bond_dev)5992 void bond_setup(struct net_device *bond_dev)
5993 {
5994 struct bonding *bond = netdev_priv(bond_dev);
5995
5996 spin_lock_init(&bond->mode_lock);
5997 bond->params = bonding_defaults;
5998
5999 /* Initialize pointers */
6000 bond->dev = bond_dev;
6001
6002 /* Initialize the device entry points */
6003 ether_setup(bond_dev);
6004 bond_dev->max_mtu = ETH_MAX_MTU;
6005 bond_dev->netdev_ops = &bond_netdev_ops;
6006 bond_dev->ethtool_ops = &bond_ethtool_ops;
6007
6008 bond_dev->needs_free_netdev = true;
6009 bond_dev->priv_destructor = bond_destructor;
6010
6011 SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
6012
6013 /* Initialize the device options */
6014 bond_dev->flags |= IFF_MASTER;
6015 bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
6016 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
6017
6018 #ifdef CONFIG_XFRM_OFFLOAD
6019 /* set up xfrm device ops (only supported in active-backup right now) */
6020 bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
6021 INIT_LIST_HEAD(&bond->ipsec_list);
6022 mutex_init(&bond->ipsec_lock);
6023 #endif /* CONFIG_XFRM_OFFLOAD */
6024
6025 /* don't acquire bond device's netif_tx_lock when transmitting */
6026 bond_dev->lltx = true;
6027
6028 /* Don't allow bond devices to change network namespaces. */
6029 bond_dev->netns_immutable = true;
6030
6031 /* By default, we declare the bond to be fully
6032 * VLAN hardware accelerated capable. Special
6033 * care is taken in the various xmit functions
6034 * when there are slaves that are not hw accel
6035 * capable
6036 */
6037
6038 bond_dev->hw_features = MASTER_UPPER_DEV_VLAN_FEATURES |
6039 NETIF_F_HW_VLAN_CTAG_RX |
6040 NETIF_F_HW_VLAN_CTAG_FILTER |
6041 NETIF_F_HW_VLAN_STAG_RX |
6042 NETIF_F_HW_VLAN_STAG_FILTER;
6043
6044 bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
6045 bond_dev->features |= bond_dev->hw_features;
6046 bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
6047 bond_dev->features |= NETIF_F_GSO_PARTIAL;
6048 #ifdef CONFIG_XFRM_OFFLOAD
6049 bond_dev->hw_features |= BOND_XFRM_FEATURES;
6050 /* Only enable XFRM features if this is an active-backup config */
6051 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
6052 bond_dev->features |= BOND_XFRM_FEATURES;
6053 #endif /* CONFIG_XFRM_OFFLOAD */
6054 }
6055
6056 /* Destroy a bonding device.
6057 * Must be under rtnl_lock when this function is called.
6058 */
bond_uninit(struct net_device * bond_dev)6059 static void bond_uninit(struct net_device *bond_dev)
6060 {
6061 struct bonding *bond = netdev_priv(bond_dev);
6062 struct list_head *iter;
6063 struct slave *slave;
6064
6065 bond_netpoll_cleanup(bond_dev);
6066
6067 /* Release the bonded slaves */
6068 bond_for_each_slave(bond, slave, iter)
6069 __bond_release_one(bond_dev, slave->dev, true, true);
6070 netdev_info(bond_dev, "Released all slaves\n");
6071
6072 #ifdef CONFIG_XFRM_OFFLOAD
6073 mutex_destroy(&bond->ipsec_lock);
6074 #endif /* CONFIG_XFRM_OFFLOAD */
6075
6076 bond_set_slave_arr(bond, NULL, NULL);
6077
6078 list_del_rcu(&bond->bond_list);
6079
6080 bond_debug_unregister(bond);
6081 }
6082
6083 /*------------------------- Module initialization ---------------------------*/
6084
bond_check_params(struct bond_params * params)6085 static int __init bond_check_params(struct bond_params *params)
6086 {
6087 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
6088 struct bond_opt_value newval;
6089 const struct bond_opt_value *valptr;
6090 int arp_all_targets_value = 0;
6091 u16 ad_actor_sys_prio = 0;
6092 u16 ad_user_port_key = 0;
6093 __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
6094 int arp_ip_count;
6095 int bond_mode = BOND_MODE_ROUNDROBIN;
6096 int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
6097 int lacp_fast = 0;
6098 int tlb_dynamic_lb;
6099
6100 /* Convert string parameters. */
6101 if (mode) {
6102 bond_opt_initstr(&newval, mode);
6103 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
6104 if (!valptr) {
6105 pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
6106 return -EINVAL;
6107 }
6108 bond_mode = valptr->value;
6109 }
6110
6111 if (xmit_hash_policy) {
6112 if (bond_mode == BOND_MODE_ROUNDROBIN ||
6113 bond_mode == BOND_MODE_ACTIVEBACKUP ||
6114 bond_mode == BOND_MODE_BROADCAST) {
6115 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
6116 bond_mode_name(bond_mode));
6117 } else {
6118 bond_opt_initstr(&newval, xmit_hash_policy);
6119 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
6120 &newval);
6121 if (!valptr) {
6122 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
6123 xmit_hash_policy);
6124 return -EINVAL;
6125 }
6126 xmit_hashtype = valptr->value;
6127 }
6128 }
6129
6130 if (lacp_rate) {
6131 if (bond_mode != BOND_MODE_8023AD) {
6132 pr_info("lacp_rate param is irrelevant in mode %s\n",
6133 bond_mode_name(bond_mode));
6134 } else {
6135 bond_opt_initstr(&newval, lacp_rate);
6136 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
6137 &newval);
6138 if (!valptr) {
6139 pr_err("Error: Invalid lacp rate \"%s\"\n",
6140 lacp_rate);
6141 return -EINVAL;
6142 }
6143 lacp_fast = valptr->value;
6144 }
6145 }
6146
6147 if (ad_select) {
6148 bond_opt_initstr(&newval, ad_select);
6149 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
6150 &newval);
6151 if (!valptr) {
6152 pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
6153 return -EINVAL;
6154 }
6155 params->ad_select = valptr->value;
6156 if (bond_mode != BOND_MODE_8023AD)
6157 pr_warn("ad_select param only affects 802.3ad mode\n");
6158 } else {
6159 params->ad_select = BOND_AD_STABLE;
6160 }
6161
6162 if (max_bonds < 0) {
6163 pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
6164 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
6165 max_bonds = BOND_DEFAULT_MAX_BONDS;
6166 }
6167
6168 if (miimon < 0) {
6169 pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6170 miimon, INT_MAX);
6171 miimon = 0;
6172 }
6173
6174 if (updelay < 0) {
6175 pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6176 updelay, INT_MAX);
6177 updelay = 0;
6178 }
6179
6180 if (downdelay < 0) {
6181 pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6182 downdelay, INT_MAX);
6183 downdelay = 0;
6184 }
6185
6186 if (use_carrier != 1) {
6187 pr_err("Error: invalid use_carrier parameter (%d)\n",
6188 use_carrier);
6189 return -EINVAL;
6190 }
6191
6192 if (num_peer_notif < 0 || num_peer_notif > 255) {
6193 pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
6194 num_peer_notif);
6195 num_peer_notif = 1;
6196 }
6197
6198 /* reset values for 802.3ad/TLB/ALB */
6199 if (!bond_mode_uses_arp(bond_mode)) {
6200 if (!miimon) {
6201 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");
6202 pr_warn("Forcing miimon to 100msec\n");
6203 miimon = BOND_DEFAULT_MIIMON;
6204 }
6205 }
6206
6207 if (tx_queues < 1 || tx_queues > 255) {
6208 pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
6209 tx_queues, BOND_DEFAULT_TX_QUEUES);
6210 tx_queues = BOND_DEFAULT_TX_QUEUES;
6211 }
6212
6213 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
6214 pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
6215 all_slaves_active);
6216 all_slaves_active = 0;
6217 }
6218
6219 if (resend_igmp < 0 || resend_igmp > 255) {
6220 pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
6221 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
6222 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
6223 }
6224
6225 bond_opt_initval(&newval, packets_per_slave);
6226 if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
6227 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
6228 packets_per_slave, USHRT_MAX);
6229 packets_per_slave = 1;
6230 }
6231
6232 if (bond_mode == BOND_MODE_ALB) {
6233 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",
6234 updelay);
6235 }
6236
6237 if (!miimon) {
6238 if (updelay || downdelay) {
6239 /* just warn the user the up/down delay will have
6240 * no effect since miimon is zero...
6241 */
6242 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",
6243 updelay, downdelay);
6244 }
6245 } else {
6246 /* don't allow arp monitoring */
6247 if (arp_interval) {
6248 pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
6249 miimon, arp_interval);
6250 arp_interval = 0;
6251 }
6252
6253 if ((updelay % miimon) != 0) {
6254 pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
6255 updelay, miimon, (updelay / miimon) * miimon);
6256 }
6257
6258 updelay /= miimon;
6259
6260 if ((downdelay % miimon) != 0) {
6261 pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
6262 downdelay, miimon,
6263 (downdelay / miimon) * miimon);
6264 }
6265
6266 downdelay /= miimon;
6267 }
6268
6269 if (arp_interval < 0) {
6270 pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6271 arp_interval, INT_MAX);
6272 arp_interval = 0;
6273 }
6274
6275 for (arp_ip_count = 0, i = 0;
6276 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
6277 __be32 ip;
6278
6279 /* not a complete check, but good enough to catch mistakes */
6280 if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
6281 !bond_is_ip_target_ok(ip)) {
6282 pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
6283 arp_ip_target[i]);
6284 arp_interval = 0;
6285 } else {
6286 if (bond_get_targets_ip(arp_target, ip) == -1)
6287 arp_target[arp_ip_count++] = ip;
6288 else
6289 pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
6290 &ip);
6291 }
6292 }
6293
6294 if (arp_interval && !arp_ip_count) {
6295 /* don't allow arping if no arp_ip_target given... */
6296 pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
6297 arp_interval);
6298 arp_interval = 0;
6299 }
6300
6301 if (arp_validate) {
6302 if (!arp_interval) {
6303 pr_err("arp_validate requires arp_interval\n");
6304 return -EINVAL;
6305 }
6306
6307 bond_opt_initstr(&newval, arp_validate);
6308 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
6309 &newval);
6310 if (!valptr) {
6311 pr_err("Error: invalid arp_validate \"%s\"\n",
6312 arp_validate);
6313 return -EINVAL;
6314 }
6315 arp_validate_value = valptr->value;
6316 } else {
6317 arp_validate_value = 0;
6318 }
6319
6320 if (arp_all_targets) {
6321 bond_opt_initstr(&newval, arp_all_targets);
6322 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
6323 &newval);
6324 if (!valptr) {
6325 pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
6326 arp_all_targets);
6327 arp_all_targets_value = 0;
6328 } else {
6329 arp_all_targets_value = valptr->value;
6330 }
6331 }
6332
6333 if (miimon) {
6334 pr_info("MII link monitoring set to %d ms\n", miimon);
6335 } else if (arp_interval) {
6336 valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
6337 arp_validate_value);
6338 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
6339 arp_interval, valptr->string, arp_ip_count);
6340
6341 for (i = 0; i < arp_ip_count; i++)
6342 pr_cont(" %s", arp_ip_target[i]);
6343
6344 pr_cont("\n");
6345
6346 } else if (max_bonds) {
6347 /* miimon and arp_interval not set, we need one so things
6348 * work as expected, see bonding.txt for details
6349 */
6350 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");
6351 }
6352
6353 if (primary && !bond_mode_uses_primary(bond_mode)) {
6354 /* currently, using a primary only makes sense
6355 * in active backup, TLB or ALB modes
6356 */
6357 pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
6358 primary, bond_mode_name(bond_mode));
6359 primary = NULL;
6360 }
6361
6362 if (primary && primary_reselect) {
6363 bond_opt_initstr(&newval, primary_reselect);
6364 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
6365 &newval);
6366 if (!valptr) {
6367 pr_err("Error: Invalid primary_reselect \"%s\"\n",
6368 primary_reselect);
6369 return -EINVAL;
6370 }
6371 primary_reselect_value = valptr->value;
6372 } else {
6373 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
6374 }
6375
6376 if (fail_over_mac) {
6377 bond_opt_initstr(&newval, fail_over_mac);
6378 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
6379 &newval);
6380 if (!valptr) {
6381 pr_err("Error: invalid fail_over_mac \"%s\"\n",
6382 fail_over_mac);
6383 return -EINVAL;
6384 }
6385 fail_over_mac_value = valptr->value;
6386 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
6387 pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
6388 } else {
6389 fail_over_mac_value = BOND_FOM_NONE;
6390 }
6391
6392 bond_opt_initstr(&newval, "default");
6393 valptr = bond_opt_parse(
6394 bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
6395 &newval);
6396 if (!valptr) {
6397 pr_err("Error: No ad_actor_sys_prio default value");
6398 return -EINVAL;
6399 }
6400 ad_actor_sys_prio = valptr->value;
6401
6402 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
6403 &newval);
6404 if (!valptr) {
6405 pr_err("Error: No ad_user_port_key default value");
6406 return -EINVAL;
6407 }
6408 ad_user_port_key = valptr->value;
6409
6410 bond_opt_initstr(&newval, "default");
6411 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
6412 if (!valptr) {
6413 pr_err("Error: No tlb_dynamic_lb default value");
6414 return -EINVAL;
6415 }
6416 tlb_dynamic_lb = valptr->value;
6417
6418 if (lp_interval == 0) {
6419 pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
6420 INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
6421 lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
6422 }
6423
6424 /* fill params struct with the proper values */
6425 params->mode = bond_mode;
6426 params->xmit_policy = xmit_hashtype;
6427 params->miimon = miimon;
6428 params->num_peer_notif = num_peer_notif;
6429 params->arp_interval = arp_interval;
6430 params->arp_validate = arp_validate_value;
6431 params->arp_all_targets = arp_all_targets_value;
6432 params->missed_max = 2;
6433 params->updelay = updelay;
6434 params->downdelay = downdelay;
6435 params->peer_notif_delay = 0;
6436 params->lacp_active = 1;
6437 params->lacp_fast = lacp_fast;
6438 params->primary[0] = 0;
6439 params->primary_reselect = primary_reselect_value;
6440 params->fail_over_mac = fail_over_mac_value;
6441 params->tx_queues = tx_queues;
6442 params->all_slaves_active = all_slaves_active;
6443 params->resend_igmp = resend_igmp;
6444 params->min_links = min_links;
6445 params->lp_interval = lp_interval;
6446 params->packets_per_slave = packets_per_slave;
6447 params->tlb_dynamic_lb = tlb_dynamic_lb;
6448 params->ad_actor_sys_prio = ad_actor_sys_prio;
6449 eth_zero_addr(params->ad_actor_system);
6450 params->ad_user_port_key = ad_user_port_key;
6451 params->coupled_control = 1;
6452 params->broadcast_neighbor = 0;
6453 params->lacp_strict = 0;
6454 if (packets_per_slave > 0) {
6455 params->reciprocal_packets_per_slave =
6456 reciprocal_value(packets_per_slave);
6457 } else {
6458 /* reciprocal_packets_per_slave is unused if
6459 * packets_per_slave is 0 or 1, just initialize it
6460 */
6461 params->reciprocal_packets_per_slave =
6462 (struct reciprocal_value) { 0 };
6463 }
6464
6465 if (primary)
6466 strscpy_pad(params->primary, primary, sizeof(params->primary));
6467
6468 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
6469 #if IS_ENABLED(CONFIG_IPV6)
6470 memset(params->ns_targets, 0, sizeof(struct in6_addr) * BOND_MAX_NS_TARGETS);
6471 #endif
6472
6473 return 0;
6474 }
6475
6476 /* Called from registration process */
bond_init(struct net_device * bond_dev)6477 static int bond_init(struct net_device *bond_dev)
6478 {
6479 struct bonding *bond = netdev_priv(bond_dev);
6480 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
6481
6482 netdev_dbg(bond_dev, "Begin bond_init\n");
6483
6484 bond->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
6485 bond_dev->name);
6486 if (!bond->wq)
6487 return -ENOMEM;
6488
6489 bond->notifier_ctx = false;
6490
6491 spin_lock_init(&bond->stats_lock);
6492 netdev_lockdep_set_classes(bond_dev);
6493
6494 list_add_tail_rcu(&bond->bond_list, &bn->dev_list);
6495
6496 bond_prepare_sysfs_group(bond);
6497
6498 bond_debug_register(bond);
6499
6500 /* Ensure valid dev_addr */
6501 if (is_zero_ether_addr(bond_dev->dev_addr) &&
6502 bond_dev->addr_assign_type == NET_ADDR_PERM)
6503 eth_hw_addr_random(bond_dev);
6504
6505 return 0;
6506 }
6507
bond_get_num_tx_queues(void)6508 unsigned int bond_get_num_tx_queues(void)
6509 {
6510 return tx_queues;
6511 }
6512
6513 /* Create a new bond based on the specified name and bonding parameters.
6514 * If name is NULL, obtain a suitable "bond%d" name for us.
6515 * Caller must NOT hold rtnl_lock; we need to release it here before we
6516 * set up our sysfs entries.
6517 */
bond_create(struct net * net,const char * name)6518 int bond_create(struct net *net, const char *name)
6519 {
6520 struct net_device *bond_dev;
6521 struct bonding *bond;
6522 int res = -ENOMEM;
6523
6524 rtnl_lock();
6525
6526 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
6527 name ? name : "bond%d", NET_NAME_UNKNOWN,
6528 bond_setup, tx_queues);
6529 if (!bond_dev)
6530 goto out;
6531
6532 bond = netdev_priv(bond_dev);
6533 dev_net_set(bond_dev, net);
6534 bond_dev->rtnl_link_ops = &bond_link_ops;
6535
6536 res = register_netdevice(bond_dev);
6537 if (res < 0) {
6538 free_netdev(bond_dev);
6539 goto out;
6540 }
6541
6542 netif_carrier_off(bond_dev);
6543
6544 bond_work_init_all(bond);
6545
6546 out:
6547 rtnl_unlock();
6548 return res;
6549 }
6550
bond_net_init(struct net * net)6551 static int __net_init bond_net_init(struct net *net)
6552 {
6553 struct bond_net *bn = net_generic(net, bond_net_id);
6554
6555 bn->net = net;
6556 INIT_LIST_HEAD(&bn->dev_list);
6557
6558 bond_create_proc_dir(bn);
6559 bond_create_sysfs(bn);
6560
6561 return 0;
6562 }
6563
6564 /* According to commit 69b0216ac255 ("bonding: fix bonding_masters
6565 * race condition in bond unloading") we need to remove sysfs files
6566 * before we remove our devices (done later in bond_net_exit_rtnl())
6567 */
bond_net_pre_exit(struct net * net)6568 static void __net_exit bond_net_pre_exit(struct net *net)
6569 {
6570 struct bond_net *bn = net_generic(net, bond_net_id);
6571
6572 bond_destroy_sysfs(bn);
6573 }
6574
bond_net_exit_rtnl(struct net * net,struct list_head * dev_kill_list)6575 static void __net_exit bond_net_exit_rtnl(struct net *net,
6576 struct list_head *dev_kill_list)
6577 {
6578 struct bond_net *bn = net_generic(net, bond_net_id);
6579 struct bonding *bond, *tmp_bond;
6580
6581 /* Kill off any bonds created after unregistering bond rtnl ops */
6582 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
6583 unregister_netdevice_queue(bond->dev, dev_kill_list);
6584 }
6585
6586 /* According to commit 23fa5c2caae0 ("bonding: destroy proc directory
6587 * only after all bonds are gone") bond_destroy_proc_dir() is called
6588 * after bond_net_exit_rtnl() has completed.
6589 */
bond_net_exit_batch(struct list_head * net_list)6590 static void __net_exit bond_net_exit_batch(struct list_head *net_list)
6591 {
6592 struct bond_net *bn;
6593 struct net *net;
6594
6595 list_for_each_entry(net, net_list, exit_list) {
6596 bn = net_generic(net, bond_net_id);
6597 bond_destroy_proc_dir(bn);
6598 }
6599 }
6600
6601 static struct pernet_operations bond_net_ops = {
6602 .init = bond_net_init,
6603 .pre_exit = bond_net_pre_exit,
6604 .exit_rtnl = bond_net_exit_rtnl,
6605 .exit_batch = bond_net_exit_batch,
6606 .id = &bond_net_id,
6607 .size = sizeof(struct bond_net),
6608 };
6609
bonding_init(void)6610 static int __init bonding_init(void)
6611 {
6612 int i;
6613 int res;
6614
6615 res = bond_check_params(&bonding_defaults);
6616 if (res)
6617 goto out;
6618
6619 bond_create_debugfs();
6620
6621 res = register_pernet_subsys(&bond_net_ops);
6622 if (res)
6623 goto err_net_ops;
6624
6625 res = bond_netlink_init();
6626 if (res)
6627 goto err_link;
6628
6629 for (i = 0; i < max_bonds; i++) {
6630 res = bond_create(&init_net, NULL);
6631 if (res)
6632 goto err;
6633 }
6634
6635 skb_flow_dissector_init(&flow_keys_bonding,
6636 flow_keys_bonding_keys,
6637 ARRAY_SIZE(flow_keys_bonding_keys));
6638
6639 res = register_netdevice_notifier(&bond_netdev_notifier);
6640 if (res)
6641 goto err;
6642 out:
6643 return res;
6644 err:
6645 bond_netlink_fini();
6646 err_link:
6647 unregister_pernet_subsys(&bond_net_ops);
6648 err_net_ops:
6649 bond_destroy_debugfs();
6650 goto out;
6651
6652 }
6653
bonding_exit(void)6654 static void __exit bonding_exit(void)
6655 {
6656 unregister_netdevice_notifier(&bond_netdev_notifier);
6657
6658 bond_netlink_fini();
6659 unregister_pernet_subsys(&bond_net_ops);
6660
6661 bond_destroy_debugfs();
6662
6663 #ifdef CONFIG_NET_POLL_CONTROLLER
6664 /* Make sure we don't have an imbalance on our netpoll blocking */
6665 WARN_ON(static_branch_unlikely(&netpoll_block_tx));
6666 #endif
6667 }
6668
6669 module_init(bonding_init);
6670 module_exit(bonding_exit);
6671 MODULE_LICENSE("GPL");
6672 MODULE_DESCRIPTION("Ethernet Channel Bonding Driver");
6673 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
6674 MODULE_IMPORT_NS("NETDEV_INTERNAL");
6675