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