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