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