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