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