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