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