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