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