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