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