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