1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * drivers/net/bond/bond_options.c - bonding options 4 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us> 5 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com> 6 */ 7 8 #include <linux/errno.h> 9 #include <linux/if.h> 10 #include <linux/netdevice.h> 11 #include <linux/spinlock.h> 12 #include <linux/rcupdate.h> 13 #include <linux/ctype.h> 14 #include <linux/inet.h> 15 #include <linux/sched/signal.h> 16 17 #include <net/bonding.h> 18 #include <net/ndisc.h> 19 20 static int bond_option_active_slave_set(struct bonding *bond, 21 const struct bond_opt_value *newval); 22 static int bond_option_miimon_set(struct bonding *bond, 23 const struct bond_opt_value *newval); 24 static int bond_option_updelay_set(struct bonding *bond, 25 const struct bond_opt_value *newval); 26 static int bond_option_downdelay_set(struct bonding *bond, 27 const struct bond_opt_value *newval); 28 static int bond_option_peer_notif_delay_set(struct bonding *bond, 29 const struct bond_opt_value *newval); 30 static int bond_option_use_carrier_set(struct bonding *bond, 31 const struct bond_opt_value *newval); 32 static int bond_option_arp_interval_set(struct bonding *bond, 33 const struct bond_opt_value *newval); 34 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target); 35 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target); 36 static int bond_option_arp_ip_targets_set(struct bonding *bond, 37 const struct bond_opt_value *newval); 38 static int bond_option_ns_ip6_targets_set(struct bonding *bond, 39 const struct bond_opt_value *newval); 40 static int bond_option_arp_validate_set(struct bonding *bond, 41 const struct bond_opt_value *newval); 42 static int bond_option_arp_all_targets_set(struct bonding *bond, 43 const struct bond_opt_value *newval); 44 static int bond_option_prio_set(struct bonding *bond, 45 const struct bond_opt_value *newval); 46 static int bond_option_primary_set(struct bonding *bond, 47 const struct bond_opt_value *newval); 48 static int bond_option_primary_reselect_set(struct bonding *bond, 49 const struct bond_opt_value *newval); 50 static int bond_option_fail_over_mac_set(struct bonding *bond, 51 const struct bond_opt_value *newval); 52 static int bond_option_xmit_hash_policy_set(struct bonding *bond, 53 const struct bond_opt_value *newval); 54 static int bond_option_resend_igmp_set(struct bonding *bond, 55 const struct bond_opt_value *newval); 56 static int bond_option_num_peer_notif_set(struct bonding *bond, 57 const struct bond_opt_value *newval); 58 static int bond_option_all_slaves_active_set(struct bonding *bond, 59 const struct bond_opt_value *newval); 60 static int bond_option_min_links_set(struct bonding *bond, 61 const struct bond_opt_value *newval); 62 static int bond_option_lp_interval_set(struct bonding *bond, 63 const struct bond_opt_value *newval); 64 static int bond_option_pps_set(struct bonding *bond, 65 const struct bond_opt_value *newval); 66 static int bond_option_lacp_active_set(struct bonding *bond, 67 const struct bond_opt_value *newval); 68 static int bond_option_lacp_rate_set(struct bonding *bond, 69 const struct bond_opt_value *newval); 70 static int bond_option_ad_select_set(struct bonding *bond, 71 const struct bond_opt_value *newval); 72 static int bond_option_queue_id_set(struct bonding *bond, 73 const struct bond_opt_value *newval); 74 static int bond_option_mode_set(struct bonding *bond, 75 const struct bond_opt_value *newval); 76 static int bond_option_slaves_set(struct bonding *bond, 77 const struct bond_opt_value *newval); 78 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond, 79 const struct bond_opt_value *newval); 80 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond, 81 const struct bond_opt_value *newval); 82 static int bond_option_ad_actor_system_set(struct bonding *bond, 83 const struct bond_opt_value *newval); 84 static int bond_option_ad_user_port_key_set(struct bonding *bond, 85 const struct bond_opt_value *newval); 86 static int bond_option_missed_max_set(struct bonding *bond, 87 const struct bond_opt_value *newval); 88 static int bond_option_coupled_control_set(struct bonding *bond, 89 const struct bond_opt_value *newval); 90 91 static const struct bond_opt_value bond_mode_tbl[] = { 92 { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT}, 93 { "active-backup", BOND_MODE_ACTIVEBACKUP, 0}, 94 { "balance-xor", BOND_MODE_XOR, 0}, 95 { "broadcast", BOND_MODE_BROADCAST, 0}, 96 { "802.3ad", BOND_MODE_8023AD, 0}, 97 { "balance-tlb", BOND_MODE_TLB, 0}, 98 { "balance-alb", BOND_MODE_ALB, 0}, 99 { NULL, -1, 0}, 100 }; 101 102 static const struct bond_opt_value bond_pps_tbl[] = { 103 { "default", 1, BOND_VALFLAG_DEFAULT}, 104 { "maxval", USHRT_MAX, BOND_VALFLAG_MAX}, 105 { NULL, -1, 0}, 106 }; 107 108 static const struct bond_opt_value bond_xmit_hashtype_tbl[] = { 109 { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT}, 110 { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0}, 111 { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0}, 112 { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0}, 113 { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0}, 114 { "vlan+srcmac", BOND_XMIT_POLICY_VLAN_SRCMAC, 0}, 115 { NULL, -1, 0}, 116 }; 117 118 static const struct bond_opt_value bond_arp_validate_tbl[] = { 119 { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT}, 120 { "active", BOND_ARP_VALIDATE_ACTIVE, 0}, 121 { "backup", BOND_ARP_VALIDATE_BACKUP, 0}, 122 { "all", BOND_ARP_VALIDATE_ALL, 0}, 123 { "filter", BOND_ARP_FILTER, 0}, 124 { "filter_active", BOND_ARP_FILTER_ACTIVE, 0}, 125 { "filter_backup", BOND_ARP_FILTER_BACKUP, 0}, 126 { NULL, -1, 0}, 127 }; 128 129 static const struct bond_opt_value bond_arp_all_targets_tbl[] = { 130 { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT}, 131 { "all", BOND_ARP_TARGETS_ALL, 0}, 132 { NULL, -1, 0}, 133 }; 134 135 static const struct bond_opt_value bond_fail_over_mac_tbl[] = { 136 { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT}, 137 { "active", BOND_FOM_ACTIVE, 0}, 138 { "follow", BOND_FOM_FOLLOW, 0}, 139 { NULL, -1, 0}, 140 }; 141 142 static const struct bond_opt_value bond_intmax_tbl[] = { 143 { "off", 0, BOND_VALFLAG_DEFAULT}, 144 { "maxval", INT_MAX, BOND_VALFLAG_MAX}, 145 { NULL, -1, 0} 146 }; 147 148 static const struct bond_opt_value bond_lacp_active[] = { 149 { "off", 0, 0}, 150 { "on", 1, BOND_VALFLAG_DEFAULT}, 151 { NULL, -1, 0} 152 }; 153 154 static const struct bond_opt_value bond_lacp_rate_tbl[] = { 155 { "slow", AD_LACP_SLOW, 0}, 156 { "fast", AD_LACP_FAST, 0}, 157 { NULL, -1, 0}, 158 }; 159 160 static const struct bond_opt_value bond_ad_select_tbl[] = { 161 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT}, 162 { "bandwidth", BOND_AD_BANDWIDTH, 0}, 163 { "count", BOND_AD_COUNT, 0}, 164 { NULL, -1, 0}, 165 }; 166 167 static const struct bond_opt_value bond_num_peer_notif_tbl[] = { 168 { "off", 0, 0}, 169 { "maxval", 255, BOND_VALFLAG_MAX}, 170 { "default", 1, BOND_VALFLAG_DEFAULT}, 171 { NULL, -1, 0} 172 }; 173 174 static const struct bond_opt_value bond_peer_notif_delay_tbl[] = { 175 { "off", 0, 0}, 176 { "maxval", 300000, BOND_VALFLAG_MAX}, 177 { NULL, -1, 0} 178 }; 179 180 static const struct bond_opt_value bond_primary_reselect_tbl[] = { 181 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT}, 182 { "better", BOND_PRI_RESELECT_BETTER, 0}, 183 { "failure", BOND_PRI_RESELECT_FAILURE, 0}, 184 { NULL, -1}, 185 }; 186 187 static const struct bond_opt_value bond_use_carrier_tbl[] = { 188 { "off", 0, 0}, 189 { "on", 1, BOND_VALFLAG_DEFAULT}, 190 { NULL, -1, 0} 191 }; 192 193 static const struct bond_opt_value bond_all_slaves_active_tbl[] = { 194 { "off", 0, BOND_VALFLAG_DEFAULT}, 195 { "on", 1, 0}, 196 { NULL, -1, 0} 197 }; 198 199 static const struct bond_opt_value bond_resend_igmp_tbl[] = { 200 { "off", 0, 0}, 201 { "maxval", 255, BOND_VALFLAG_MAX}, 202 { "default", 1, BOND_VALFLAG_DEFAULT}, 203 { NULL, -1, 0} 204 }; 205 206 static const struct bond_opt_value bond_lp_interval_tbl[] = { 207 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT}, 208 { "maxval", INT_MAX, BOND_VALFLAG_MAX}, 209 { NULL, -1, 0}, 210 }; 211 212 static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = { 213 { "off", 0, 0}, 214 { "on", 1, BOND_VALFLAG_DEFAULT}, 215 { NULL, -1, 0} 216 }; 217 218 static const struct bond_opt_value bond_ad_actor_sys_prio_tbl[] = { 219 { "minval", 1, BOND_VALFLAG_MIN}, 220 { "maxval", 65535, BOND_VALFLAG_MAX | BOND_VALFLAG_DEFAULT}, 221 { NULL, -1, 0}, 222 }; 223 224 static const struct bond_opt_value bond_ad_user_port_key_tbl[] = { 225 { "minval", 0, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT}, 226 { "maxval", 1023, BOND_VALFLAG_MAX}, 227 { NULL, -1, 0}, 228 }; 229 230 static const struct bond_opt_value bond_missed_max_tbl[] = { 231 { "minval", 1, BOND_VALFLAG_MIN}, 232 { "maxval", 255, BOND_VALFLAG_MAX}, 233 { "default", 2, BOND_VALFLAG_DEFAULT}, 234 { NULL, -1, 0}, 235 }; 236 237 static const struct bond_opt_value bond_coupled_control_tbl[] = { 238 { "on", 1, BOND_VALFLAG_DEFAULT}, 239 { "off", 0, 0}, 240 { NULL, -1, 0}, 241 }; 242 243 static const struct bond_option bond_opts[BOND_OPT_LAST] = { 244 [BOND_OPT_MODE] = { 245 .id = BOND_OPT_MODE, 246 .name = "mode", 247 .desc = "bond device mode", 248 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN, 249 .values = bond_mode_tbl, 250 .set = bond_option_mode_set 251 }, 252 [BOND_OPT_PACKETS_PER_SLAVE] = { 253 .id = BOND_OPT_PACKETS_PER_SLAVE, 254 .name = "packets_per_slave", 255 .desc = "Packets to send per slave in RR mode", 256 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)), 257 .values = bond_pps_tbl, 258 .set = bond_option_pps_set 259 }, 260 [BOND_OPT_XMIT_HASH] = { 261 .id = BOND_OPT_XMIT_HASH, 262 .name = "xmit_hash_policy", 263 .desc = "balance-xor, 802.3ad, and tlb hashing method", 264 .values = bond_xmit_hashtype_tbl, 265 .set = bond_option_xmit_hash_policy_set 266 }, 267 [BOND_OPT_ARP_VALIDATE] = { 268 .id = BOND_OPT_ARP_VALIDATE, 269 .name = "arp_validate", 270 .desc = "validate src/dst of ARP probes", 271 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) | 272 BIT(BOND_MODE_ALB), 273 .values = bond_arp_validate_tbl, 274 .set = bond_option_arp_validate_set 275 }, 276 [BOND_OPT_ARP_ALL_TARGETS] = { 277 .id = BOND_OPT_ARP_ALL_TARGETS, 278 .name = "arp_all_targets", 279 .desc = "fail on any/all arp targets timeout", 280 .values = bond_arp_all_targets_tbl, 281 .set = bond_option_arp_all_targets_set 282 }, 283 [BOND_OPT_FAIL_OVER_MAC] = { 284 .id = BOND_OPT_FAIL_OVER_MAC, 285 .name = "fail_over_mac", 286 .desc = "For active-backup, do not set all slaves to the same MAC", 287 .flags = BOND_OPTFLAG_NOSLAVES, 288 .values = bond_fail_over_mac_tbl, 289 .set = bond_option_fail_over_mac_set 290 }, 291 [BOND_OPT_ARP_INTERVAL] = { 292 .id = BOND_OPT_ARP_INTERVAL, 293 .name = "arp_interval", 294 .desc = "arp interval in milliseconds", 295 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) | 296 BIT(BOND_MODE_ALB), 297 .values = bond_intmax_tbl, 298 .set = bond_option_arp_interval_set 299 }, 300 [BOND_OPT_MISSED_MAX] = { 301 .id = BOND_OPT_MISSED_MAX, 302 .name = "arp_missed_max", 303 .desc = "Maximum number of missed ARP interval", 304 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) | 305 BIT(BOND_MODE_ALB), 306 .values = bond_missed_max_tbl, 307 .set = bond_option_missed_max_set 308 }, 309 [BOND_OPT_ARP_TARGETS] = { 310 .id = BOND_OPT_ARP_TARGETS, 311 .name = "arp_ip_target", 312 .desc = "arp targets in n.n.n.n form", 313 .flags = BOND_OPTFLAG_RAWVAL, 314 .set = bond_option_arp_ip_targets_set 315 }, 316 [BOND_OPT_NS_TARGETS] = { 317 .id = BOND_OPT_NS_TARGETS, 318 .name = "ns_ip6_target", 319 .desc = "NS targets in ffff:ffff::ffff:ffff form", 320 .flags = BOND_OPTFLAG_RAWVAL, 321 .set = bond_option_ns_ip6_targets_set 322 }, 323 [BOND_OPT_DOWNDELAY] = { 324 .id = BOND_OPT_DOWNDELAY, 325 .name = "downdelay", 326 .desc = "Delay before considering link down, in milliseconds", 327 .values = bond_intmax_tbl, 328 .set = bond_option_downdelay_set 329 }, 330 [BOND_OPT_UPDELAY] = { 331 .id = BOND_OPT_UPDELAY, 332 .name = "updelay", 333 .desc = "Delay before considering link up, in milliseconds", 334 .values = bond_intmax_tbl, 335 .set = bond_option_updelay_set 336 }, 337 [BOND_OPT_LACP_ACTIVE] = { 338 .id = BOND_OPT_LACP_ACTIVE, 339 .name = "lacp_active", 340 .desc = "Send LACPDU frames with configured lacp rate or acts as speak when spoken to", 341 .flags = BOND_OPTFLAG_IFDOWN, 342 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)), 343 .values = bond_lacp_active, 344 .set = bond_option_lacp_active_set 345 }, 346 [BOND_OPT_LACP_RATE] = { 347 .id = BOND_OPT_LACP_RATE, 348 .name = "lacp_rate", 349 .desc = "LACPDU tx rate to request from 802.3ad partner", 350 .flags = BOND_OPTFLAG_IFDOWN, 351 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)), 352 .values = bond_lacp_rate_tbl, 353 .set = bond_option_lacp_rate_set 354 }, 355 [BOND_OPT_MINLINKS] = { 356 .id = BOND_OPT_MINLINKS, 357 .name = "min_links", 358 .desc = "Minimum number of available links before turning on carrier", 359 .values = bond_intmax_tbl, 360 .set = bond_option_min_links_set 361 }, 362 [BOND_OPT_AD_SELECT] = { 363 .id = BOND_OPT_AD_SELECT, 364 .name = "ad_select", 365 .desc = "803.ad aggregation selection logic", 366 .flags = BOND_OPTFLAG_IFDOWN, 367 .values = bond_ad_select_tbl, 368 .set = bond_option_ad_select_set 369 }, 370 [BOND_OPT_NUM_PEER_NOTIF] = { 371 .id = BOND_OPT_NUM_PEER_NOTIF, 372 .name = "num_unsol_na", 373 .desc = "Number of peer notifications to send on failover event", 374 .values = bond_num_peer_notif_tbl, 375 .set = bond_option_num_peer_notif_set 376 }, 377 [BOND_OPT_MIIMON] = { 378 .id = BOND_OPT_MIIMON, 379 .name = "miimon", 380 .desc = "Link check interval in milliseconds", 381 .values = bond_intmax_tbl, 382 .set = bond_option_miimon_set 383 }, 384 [BOND_OPT_PRIO] = { 385 .id = BOND_OPT_PRIO, 386 .name = "prio", 387 .desc = "Link priority for failover re-selection", 388 .flags = BOND_OPTFLAG_RAWVAL, 389 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) | 390 BIT(BOND_MODE_TLB) | 391 BIT(BOND_MODE_ALB)), 392 .set = bond_option_prio_set 393 }, 394 [BOND_OPT_PRIMARY] = { 395 .id = BOND_OPT_PRIMARY, 396 .name = "primary", 397 .desc = "Primary network device to use", 398 .flags = BOND_OPTFLAG_RAWVAL, 399 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) | 400 BIT(BOND_MODE_TLB) | 401 BIT(BOND_MODE_ALB)), 402 .set = bond_option_primary_set 403 }, 404 [BOND_OPT_PRIMARY_RESELECT] = { 405 .id = BOND_OPT_PRIMARY_RESELECT, 406 .name = "primary_reselect", 407 .desc = "Reselect primary slave once it comes up", 408 .values = bond_primary_reselect_tbl, 409 .set = bond_option_primary_reselect_set 410 }, 411 [BOND_OPT_USE_CARRIER] = { 412 .id = BOND_OPT_USE_CARRIER, 413 .name = "use_carrier", 414 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon", 415 .values = bond_use_carrier_tbl, 416 .set = bond_option_use_carrier_set 417 }, 418 [BOND_OPT_ACTIVE_SLAVE] = { 419 .id = BOND_OPT_ACTIVE_SLAVE, 420 .name = "active_slave", 421 .desc = "Currently active slave", 422 .flags = BOND_OPTFLAG_RAWVAL, 423 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) | 424 BIT(BOND_MODE_TLB) | 425 BIT(BOND_MODE_ALB)), 426 .set = bond_option_active_slave_set 427 }, 428 [BOND_OPT_QUEUE_ID] = { 429 .id = BOND_OPT_QUEUE_ID, 430 .name = "queue_id", 431 .desc = "Set queue id of a slave", 432 .flags = BOND_OPTFLAG_RAWVAL, 433 .set = bond_option_queue_id_set 434 }, 435 [BOND_OPT_ALL_SLAVES_ACTIVE] = { 436 .id = BOND_OPT_ALL_SLAVES_ACTIVE, 437 .name = "all_slaves_active", 438 .desc = "Keep all frames received on an interface by setting active flag for all slaves", 439 .values = bond_all_slaves_active_tbl, 440 .set = bond_option_all_slaves_active_set 441 }, 442 [BOND_OPT_RESEND_IGMP] = { 443 .id = BOND_OPT_RESEND_IGMP, 444 .name = "resend_igmp", 445 .desc = "Number of IGMP membership reports to send on link failure", 446 .values = bond_resend_igmp_tbl, 447 .set = bond_option_resend_igmp_set 448 }, 449 [BOND_OPT_LP_INTERVAL] = { 450 .id = BOND_OPT_LP_INTERVAL, 451 .name = "lp_interval", 452 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch", 453 .values = bond_lp_interval_tbl, 454 .set = bond_option_lp_interval_set 455 }, 456 [BOND_OPT_SLAVES] = { 457 .id = BOND_OPT_SLAVES, 458 .name = "slaves", 459 .desc = "Slave membership management", 460 .flags = BOND_OPTFLAG_RAWVAL, 461 .set = bond_option_slaves_set 462 }, 463 [BOND_OPT_TLB_DYNAMIC_LB] = { 464 .id = BOND_OPT_TLB_DYNAMIC_LB, 465 .name = "tlb_dynamic_lb", 466 .desc = "Enable dynamic flow shuffling", 467 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB) | BIT(BOND_MODE_ALB)), 468 .values = bond_tlb_dynamic_lb_tbl, 469 .flags = BOND_OPTFLAG_IFDOWN, 470 .set = bond_option_tlb_dynamic_lb_set, 471 }, 472 [BOND_OPT_AD_ACTOR_SYS_PRIO] = { 473 .id = BOND_OPT_AD_ACTOR_SYS_PRIO, 474 .name = "ad_actor_sys_prio", 475 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)), 476 .values = bond_ad_actor_sys_prio_tbl, 477 .set = bond_option_ad_actor_sys_prio_set, 478 }, 479 [BOND_OPT_AD_ACTOR_SYSTEM] = { 480 .id = BOND_OPT_AD_ACTOR_SYSTEM, 481 .name = "ad_actor_system", 482 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)), 483 .flags = BOND_OPTFLAG_RAWVAL, 484 .set = bond_option_ad_actor_system_set, 485 }, 486 [BOND_OPT_AD_USER_PORT_KEY] = { 487 .id = BOND_OPT_AD_USER_PORT_KEY, 488 .name = "ad_user_port_key", 489 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)), 490 .flags = BOND_OPTFLAG_IFDOWN, 491 .values = bond_ad_user_port_key_tbl, 492 .set = bond_option_ad_user_port_key_set, 493 }, 494 [BOND_OPT_NUM_PEER_NOTIF_ALIAS] = { 495 .id = BOND_OPT_NUM_PEER_NOTIF_ALIAS, 496 .name = "num_grat_arp", 497 .desc = "Number of peer notifications to send on failover event", 498 .values = bond_num_peer_notif_tbl, 499 .set = bond_option_num_peer_notif_set 500 }, 501 [BOND_OPT_PEER_NOTIF_DELAY] = { 502 .id = BOND_OPT_PEER_NOTIF_DELAY, 503 .name = "peer_notif_delay", 504 .desc = "Delay between each peer notification on failover event, in milliseconds", 505 .values = bond_peer_notif_delay_tbl, 506 .set = bond_option_peer_notif_delay_set 507 }, 508 [BOND_OPT_COUPLED_CONTROL] = { 509 .id = BOND_OPT_COUPLED_CONTROL, 510 .name = "coupled_control", 511 .desc = "Opt into using coupled control MUX for LACP states", 512 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)), 513 .flags = BOND_OPTFLAG_IFDOWN, 514 .values = bond_coupled_control_tbl, 515 .set = bond_option_coupled_control_set, 516 } 517 }; 518 519 /* Searches for an option by name */ 520 const struct bond_option *bond_opt_get_by_name(const char *name) 521 { 522 const struct bond_option *opt; 523 int option; 524 525 for (option = 0; option < BOND_OPT_LAST; option++) { 526 opt = bond_opt_get(option); 527 if (opt && !strcmp(opt->name, name)) 528 return opt; 529 } 530 531 return NULL; 532 } 533 534 /* Searches for a value in opt's values[] table */ 535 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val) 536 { 537 const struct bond_option *opt; 538 int i; 539 540 opt = bond_opt_get(option); 541 if (WARN_ON(!opt)) 542 return NULL; 543 for (i = 0; opt->values && opt->values[i].string; i++) 544 if (opt->values[i].value == val) 545 return &opt->values[i]; 546 547 return NULL; 548 } 549 550 /* Searches for a value in opt's values[] table which matches the flagmask */ 551 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt, 552 u32 flagmask) 553 { 554 int i; 555 556 for (i = 0; opt->values && opt->values[i].string; i++) 557 if (opt->values[i].flags & flagmask) 558 return &opt->values[i]; 559 560 return NULL; 561 } 562 563 /* If maxval is missing then there's no range to check. In case minval is 564 * missing then it's considered to be 0. 565 */ 566 static bool bond_opt_check_range(const struct bond_option *opt, u64 val) 567 { 568 const struct bond_opt_value *minval, *maxval; 569 570 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN); 571 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX); 572 if (!maxval || (minval && val < minval->value) || val > maxval->value) 573 return false; 574 575 return true; 576 } 577 578 /** 579 * bond_opt_parse - parse option value 580 * @opt: the option to parse against 581 * @val: value to parse 582 * 583 * This function tries to extract the value from @val and check if it's 584 * a possible match for the option and returns NULL if a match isn't found, 585 * or the struct_opt_value that matched. It also strips the new line from 586 * @val->string if it's present. 587 */ 588 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt, 589 struct bond_opt_value *val) 590 { 591 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, }; 592 const struct bond_opt_value *tbl; 593 const struct bond_opt_value *ret = NULL; 594 bool checkval; 595 int i, rv; 596 597 /* No parsing if the option wants a raw val */ 598 if (opt->flags & BOND_OPTFLAG_RAWVAL) 599 return val; 600 601 tbl = opt->values; 602 if (!tbl) 603 goto out; 604 605 /* ULLONG_MAX is used to bypass string processing */ 606 checkval = val->value != ULLONG_MAX; 607 if (!checkval) { 608 if (!val->string) 609 goto out; 610 p = strchr(val->string, '\n'); 611 if (p) 612 *p = '\0'; 613 for (p = val->string; *p; p++) 614 if (!(isdigit(*p) || isspace(*p))) 615 break; 616 /* The following code extracts the string to match or the value 617 * and sets checkval appropriately 618 */ 619 if (*p) { 620 rv = sscanf(val->string, "%32s", valstr); 621 } else { 622 rv = sscanf(val->string, "%llu", &val->value); 623 checkval = true; 624 } 625 if (!rv) 626 goto out; 627 } 628 629 for (i = 0; tbl[i].string; i++) { 630 /* Check for exact match */ 631 if (checkval) { 632 if (val->value == tbl[i].value) 633 ret = &tbl[i]; 634 } else { 635 if (!strcmp(valstr, "default") && 636 (tbl[i].flags & BOND_VALFLAG_DEFAULT)) 637 ret = &tbl[i]; 638 639 if (!strcmp(valstr, tbl[i].string)) 640 ret = &tbl[i]; 641 } 642 /* Found an exact match */ 643 if (ret) 644 goto out; 645 } 646 /* Possible range match */ 647 if (checkval && bond_opt_check_range(opt, val->value)) 648 ret = val; 649 out: 650 return ret; 651 } 652 653 /* Check opt's dependencies against bond mode and currently set options */ 654 static int bond_opt_check_deps(struct bonding *bond, 655 const struct bond_option *opt) 656 { 657 struct bond_params *params = &bond->params; 658 659 if (test_bit(params->mode, &opt->unsuppmodes)) 660 return -EACCES; 661 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond)) 662 return -ENOTEMPTY; 663 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP)) 664 return -EBUSY; 665 666 return 0; 667 } 668 669 static void bond_opt_dep_print(struct bonding *bond, 670 const struct bond_option *opt, 671 struct nlattr *bad_attr, 672 struct netlink_ext_ack *extack) 673 { 674 const struct bond_opt_value *modeval; 675 struct bond_params *params; 676 677 params = &bond->params; 678 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode); 679 if (test_bit(params->mode, &opt->unsuppmodes)) { 680 netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n", 681 opt->name, modeval->string, modeval->value); 682 NL_SET_ERR_MSG_ATTR(extack, bad_attr, 683 "option not supported in mode"); 684 } 685 } 686 687 static void bond_opt_error_interpret(struct bonding *bond, 688 const struct bond_option *opt, 689 int error, const struct bond_opt_value *val, 690 struct nlattr *bad_attr, 691 struct netlink_ext_ack *extack) 692 { 693 const struct bond_opt_value *minval, *maxval; 694 char *p; 695 696 switch (error) { 697 case -EINVAL: 698 NL_SET_ERR_MSG_ATTR(extack, bad_attr, "invalid option value"); 699 if (val) { 700 if (val->string) { 701 /* sometimes RAWVAL opts may have new lines */ 702 p = strchr(val->string, '\n'); 703 if (p) 704 *p = '\0'; 705 netdev_err(bond->dev, "option %s: invalid value (%s)\n", 706 opt->name, val->string); 707 } else { 708 netdev_err(bond->dev, "option %s: invalid value (%llu)\n", 709 opt->name, val->value); 710 } 711 } 712 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN); 713 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX); 714 if (!maxval) 715 break; 716 netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n", 717 opt->name, minval ? minval->value : 0, maxval->value); 718 break; 719 case -EACCES: 720 bond_opt_dep_print(bond, opt, bad_attr, extack); 721 break; 722 case -ENOTEMPTY: 723 NL_SET_ERR_MSG_ATTR(extack, bad_attr, 724 "unable to set option because the bond device has slaves"); 725 netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n", 726 opt->name); 727 break; 728 case -EBUSY: 729 NL_SET_ERR_MSG_ATTR(extack, bad_attr, 730 "unable to set option because the bond is up"); 731 netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n", 732 opt->name); 733 break; 734 case -ENODEV: 735 if (val && val->string) { 736 p = strchr(val->string, '\n'); 737 if (p) 738 *p = '\0'; 739 netdev_err(bond->dev, "option %s: interface %s does not exist!\n", 740 opt->name, val->string); 741 NL_SET_ERR_MSG_ATTR(extack, bad_attr, 742 "interface does not exist"); 743 } 744 break; 745 default: 746 break; 747 } 748 } 749 750 /** 751 * __bond_opt_set - set a bonding option 752 * @bond: target bond device 753 * @option: option to set 754 * @val: value to set it to 755 * @bad_attr: netlink attribue that caused the error 756 * @extack: extended netlink error structure, used when an error message 757 * needs to be returned to the caller via netlink 758 * 759 * This function is used to change the bond's option value, it can be 760 * used for both enabling/changing an option and for disabling it. RTNL lock 761 * must be obtained before calling this function. 762 */ 763 int __bond_opt_set(struct bonding *bond, 764 unsigned int option, struct bond_opt_value *val, 765 struct nlattr *bad_attr, struct netlink_ext_ack *extack) 766 { 767 const struct bond_opt_value *retval = NULL; 768 const struct bond_option *opt; 769 int ret = -ENOENT; 770 771 ASSERT_RTNL(); 772 773 opt = bond_opt_get(option); 774 if (WARN_ON(!val) || WARN_ON(!opt)) 775 goto out; 776 ret = bond_opt_check_deps(bond, opt); 777 if (ret) 778 goto out; 779 retval = bond_opt_parse(opt, val); 780 if (!retval) { 781 ret = -EINVAL; 782 goto out; 783 } 784 ret = opt->set(bond, retval); 785 out: 786 if (ret) 787 bond_opt_error_interpret(bond, opt, ret, val, bad_attr, extack); 788 789 return ret; 790 } 791 /** 792 * __bond_opt_set_notify - set a bonding option 793 * @bond: target bond device 794 * @option: option to set 795 * @val: value to set it to 796 * 797 * This function is used to change the bond's option value and trigger 798 * a notification to user sapce. It can be used for both enabling/changing 799 * an option and for disabling it. RTNL lock must be obtained before calling 800 * this function. 801 */ 802 int __bond_opt_set_notify(struct bonding *bond, 803 unsigned int option, struct bond_opt_value *val) 804 { 805 int ret; 806 807 ASSERT_RTNL(); 808 809 ret = __bond_opt_set(bond, option, val, NULL, NULL); 810 811 if (!ret && (bond->dev->reg_state == NETREG_REGISTERED)) 812 call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev); 813 814 return ret; 815 } 816 817 /** 818 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set 819 * @bond: target bond device 820 * @option: option to set 821 * @buf: value to set it to 822 * 823 * This function tries to acquire RTNL without blocking and if successful 824 * calls __bond_opt_set. It is mainly used for sysfs option manipulation. 825 */ 826 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf) 827 { 828 struct bond_opt_value optval; 829 int ret; 830 831 if (!rtnl_trylock()) 832 return restart_syscall(); 833 bond_opt_initstr(&optval, buf); 834 ret = __bond_opt_set_notify(bond, option, &optval); 835 rtnl_unlock(); 836 837 return ret; 838 } 839 840 /** 841 * bond_opt_get - get a pointer to an option 842 * @option: option for which to return a pointer 843 * 844 * This function checks if option is valid and if so returns a pointer 845 * to its entry in the bond_opts[] option array. 846 */ 847 const struct bond_option *bond_opt_get(unsigned int option) 848 { 849 if (!BOND_OPT_VALID(option)) 850 return NULL; 851 852 return &bond_opts[option]; 853 } 854 855 static bool bond_set_xfrm_features(struct bonding *bond) 856 { 857 if (!IS_ENABLED(CONFIG_XFRM_OFFLOAD)) 858 return false; 859 860 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) 861 bond->dev->wanted_features |= BOND_XFRM_FEATURES; 862 else 863 bond->dev->wanted_features &= ~BOND_XFRM_FEATURES; 864 865 return true; 866 } 867 868 static int bond_option_mode_set(struct bonding *bond, 869 const struct bond_opt_value *newval) 870 { 871 if (bond->xdp_prog && !bond_xdp_check(bond, newval->value)) 872 return -EOPNOTSUPP; 873 874 if (!bond_mode_uses_arp(newval->value)) { 875 if (bond->params.arp_interval) { 876 netdev_dbg(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n", 877 newval->string); 878 /* disable arp monitoring */ 879 bond->params.arp_interval = 0; 880 } 881 882 if (!bond->params.miimon) { 883 /* set miimon to default value */ 884 bond->params.miimon = BOND_DEFAULT_MIIMON; 885 netdev_dbg(bond->dev, "Setting MII monitoring interval to %d\n", 886 bond->params.miimon); 887 } 888 } 889 890 if (newval->value == BOND_MODE_ALB) 891 bond->params.tlb_dynamic_lb = 1; 892 893 /* don't cache arp_validate between modes */ 894 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE; 895 bond->params.mode = newval->value; 896 897 if (bond->dev->reg_state == NETREG_REGISTERED) { 898 bool update = false; 899 900 update |= bond_set_xfrm_features(bond); 901 902 if (update) 903 netdev_update_features(bond->dev); 904 } 905 906 bond_xdp_set_features(bond->dev); 907 908 return 0; 909 } 910 911 static int bond_option_active_slave_set(struct bonding *bond, 912 const struct bond_opt_value *newval) 913 { 914 char ifname[IFNAMSIZ] = { 0, }; 915 struct net_device *slave_dev; 916 int ret = 0; 917 918 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */ 919 if (!strlen(ifname) || newval->string[0] == '\n') { 920 slave_dev = NULL; 921 } else { 922 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname); 923 if (!slave_dev) 924 return -ENODEV; 925 } 926 927 if (slave_dev) { 928 if (!netif_is_bond_slave(slave_dev)) { 929 slave_err(bond->dev, slave_dev, "Device is not bonding slave\n"); 930 return -EINVAL; 931 } 932 933 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) { 934 slave_err(bond->dev, slave_dev, "Device is not our slave\n"); 935 return -EINVAL; 936 } 937 } 938 939 block_netpoll_tx(); 940 /* check to see if we are clearing active */ 941 if (!slave_dev) { 942 netdev_dbg(bond->dev, "Clearing current active slave\n"); 943 bond_change_active_slave(bond, NULL); 944 bond_select_active_slave(bond); 945 } else { 946 struct slave *old_active = rtnl_dereference(bond->curr_active_slave); 947 struct slave *new_active = bond_slave_get_rtnl(slave_dev); 948 949 BUG_ON(!new_active); 950 951 if (new_active == old_active) { 952 /* do nothing */ 953 slave_dbg(bond->dev, new_active->dev, "is already the current active slave\n"); 954 } else { 955 if (old_active && (new_active->link == BOND_LINK_UP) && 956 bond_slave_is_up(new_active)) { 957 slave_dbg(bond->dev, new_active->dev, "Setting as active slave\n"); 958 bond_change_active_slave(bond, new_active); 959 } else { 960 slave_err(bond->dev, new_active->dev, "Could not set as active slave; either %s is down or the link is down\n", 961 new_active->dev->name); 962 ret = -EINVAL; 963 } 964 } 965 } 966 unblock_netpoll_tx(); 967 968 return ret; 969 } 970 971 /* There are two tricky bits here. First, if MII monitoring is activated, then 972 * we must disable ARP monitoring. Second, if the timer isn't running, we must 973 * start it. 974 */ 975 static int bond_option_miimon_set(struct bonding *bond, 976 const struct bond_opt_value *newval) 977 { 978 netdev_dbg(bond->dev, "Setting MII monitoring interval to %llu\n", 979 newval->value); 980 bond->params.miimon = newval->value; 981 if (bond->params.updelay) 982 netdev_dbg(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n", 983 bond->params.updelay * bond->params.miimon); 984 if (bond->params.downdelay) 985 netdev_dbg(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n", 986 bond->params.downdelay * bond->params.miimon); 987 if (bond->params.peer_notif_delay) 988 netdev_dbg(bond->dev, "Note: Updating peer_notif_delay (to %d) since it is a multiple of the miimon value\n", 989 bond->params.peer_notif_delay * bond->params.miimon); 990 if (newval->value && bond->params.arp_interval) { 991 netdev_dbg(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n"); 992 bond->params.arp_interval = 0; 993 if (bond->params.arp_validate) 994 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE; 995 } 996 if (bond->dev->flags & IFF_UP) { 997 /* If the interface is up, we may need to fire off 998 * the MII timer. If the interface is down, the 999 * timer will get fired off when the open function 1000 * is called. 1001 */ 1002 if (!newval->value) { 1003 cancel_delayed_work_sync(&bond->mii_work); 1004 } else { 1005 cancel_delayed_work_sync(&bond->arp_work); 1006 queue_delayed_work(bond->wq, &bond->mii_work, 0); 1007 } 1008 } 1009 1010 return 0; 1011 } 1012 1013 /* Set up, down and peer notification delays. These must be multiples 1014 * of the MII monitoring value, and are stored internally as the 1015 * multiplier. Thus, we must translate to MS for the real world. 1016 */ 1017 static int _bond_option_delay_set(struct bonding *bond, 1018 const struct bond_opt_value *newval, 1019 const char *name, 1020 int *target) 1021 { 1022 int value = newval->value; 1023 1024 if (!bond->params.miimon) { 1025 netdev_err(bond->dev, "Unable to set %s as MII monitoring is disabled\n", 1026 name); 1027 return -EPERM; 1028 } 1029 if ((value % bond->params.miimon) != 0) { 1030 netdev_warn(bond->dev, 1031 "%s (%d) is not a multiple of miimon (%d), value rounded to %d ms\n", 1032 name, 1033 value, bond->params.miimon, 1034 (value / bond->params.miimon) * 1035 bond->params.miimon); 1036 } 1037 *target = value / bond->params.miimon; 1038 netdev_dbg(bond->dev, "Setting %s to %d\n", 1039 name, 1040 *target * bond->params.miimon); 1041 1042 return 0; 1043 } 1044 1045 static int bond_option_updelay_set(struct bonding *bond, 1046 const struct bond_opt_value *newval) 1047 { 1048 return _bond_option_delay_set(bond, newval, "up delay", 1049 &bond->params.updelay); 1050 } 1051 1052 static int bond_option_downdelay_set(struct bonding *bond, 1053 const struct bond_opt_value *newval) 1054 { 1055 return _bond_option_delay_set(bond, newval, "down delay", 1056 &bond->params.downdelay); 1057 } 1058 1059 static int bond_option_peer_notif_delay_set(struct bonding *bond, 1060 const struct bond_opt_value *newval) 1061 { 1062 int ret = _bond_option_delay_set(bond, newval, 1063 "peer notification delay", 1064 &bond->params.peer_notif_delay); 1065 return ret; 1066 } 1067 1068 static int bond_option_use_carrier_set(struct bonding *bond, 1069 const struct bond_opt_value *newval) 1070 { 1071 netdev_dbg(bond->dev, "Setting use_carrier to %llu\n", 1072 newval->value); 1073 bond->params.use_carrier = newval->value; 1074 1075 return 0; 1076 } 1077 1078 /* There are two tricky bits here. First, if ARP monitoring is activated, then 1079 * we must disable MII monitoring. Second, if the ARP timer isn't running, 1080 * we must start it. 1081 */ 1082 static int bond_option_arp_interval_set(struct bonding *bond, 1083 const struct bond_opt_value *newval) 1084 { 1085 netdev_dbg(bond->dev, "Setting ARP monitoring interval to %llu\n", 1086 newval->value); 1087 bond->params.arp_interval = newval->value; 1088 if (newval->value) { 1089 if (bond->params.miimon) { 1090 netdev_dbg(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n"); 1091 bond->params.miimon = 0; 1092 } 1093 if (!bond->params.arp_targets[0]) 1094 netdev_dbg(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n"); 1095 } 1096 if (bond->dev->flags & IFF_UP) { 1097 /* If the interface is up, we may need to fire off 1098 * the ARP timer. If the interface is down, the 1099 * timer will get fired off when the open function 1100 * is called. 1101 */ 1102 if (!newval->value) { 1103 if (bond->params.arp_validate) 1104 bond->recv_probe = NULL; 1105 cancel_delayed_work_sync(&bond->arp_work); 1106 } else { 1107 /* arp_validate can be set only in active-backup mode */ 1108 bond->recv_probe = bond_rcv_validate; 1109 cancel_delayed_work_sync(&bond->mii_work); 1110 queue_delayed_work(bond->wq, &bond->arp_work, 0); 1111 } 1112 } 1113 1114 return 0; 1115 } 1116 1117 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot, 1118 __be32 target, 1119 unsigned long last_rx) 1120 { 1121 __be32 *targets = bond->params.arp_targets; 1122 struct list_head *iter; 1123 struct slave *slave; 1124 1125 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) { 1126 bond_for_each_slave(bond, slave, iter) 1127 slave->target_last_arp_rx[slot] = last_rx; 1128 targets[slot] = target; 1129 } 1130 } 1131 1132 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target) 1133 { 1134 __be32 *targets = bond->params.arp_targets; 1135 int ind; 1136 1137 if (!bond_is_ip_target_ok(target)) { 1138 netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n", 1139 &target); 1140 return -EINVAL; 1141 } 1142 1143 if (bond_get_targets_ip(targets, target) != -1) { /* dup */ 1144 netdev_err(bond->dev, "ARP target %pI4 is already present\n", 1145 &target); 1146 return -EINVAL; 1147 } 1148 1149 ind = bond_get_targets_ip(targets, 0); /* first free slot */ 1150 if (ind == -1) { 1151 netdev_err(bond->dev, "ARP target table is full!\n"); 1152 return -EINVAL; 1153 } 1154 1155 netdev_dbg(bond->dev, "Adding ARP target %pI4\n", &target); 1156 1157 _bond_options_arp_ip_target_set(bond, ind, target, jiffies); 1158 1159 return 0; 1160 } 1161 1162 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target) 1163 { 1164 return _bond_option_arp_ip_target_add(bond, target); 1165 } 1166 1167 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target) 1168 { 1169 __be32 *targets = bond->params.arp_targets; 1170 struct list_head *iter; 1171 struct slave *slave; 1172 unsigned long *targets_rx; 1173 int ind, i; 1174 1175 if (!bond_is_ip_target_ok(target)) { 1176 netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n", 1177 &target); 1178 return -EINVAL; 1179 } 1180 1181 ind = bond_get_targets_ip(targets, target); 1182 if (ind == -1) { 1183 netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n", 1184 &target); 1185 return -EINVAL; 1186 } 1187 1188 if (ind == 0 && !targets[1] && bond->params.arp_interval) 1189 netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n"); 1190 1191 netdev_dbg(bond->dev, "Removing ARP target %pI4\n", &target); 1192 1193 bond_for_each_slave(bond, slave, iter) { 1194 targets_rx = slave->target_last_arp_rx; 1195 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++) 1196 targets_rx[i] = targets_rx[i+1]; 1197 targets_rx[i] = 0; 1198 } 1199 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++) 1200 targets[i] = targets[i+1]; 1201 targets[i] = 0; 1202 1203 return 0; 1204 } 1205 1206 void bond_option_arp_ip_targets_clear(struct bonding *bond) 1207 { 1208 int i; 1209 1210 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) 1211 _bond_options_arp_ip_target_set(bond, i, 0, 0); 1212 } 1213 1214 static int bond_option_arp_ip_targets_set(struct bonding *bond, 1215 const struct bond_opt_value *newval) 1216 { 1217 int ret = -EPERM; 1218 __be32 target; 1219 1220 if (newval->string) { 1221 if (strlen(newval->string) < 1 || 1222 !in4_pton(newval->string + 1, -1, (u8 *)&target, -1, NULL)) { 1223 netdev_err(bond->dev, "invalid ARP target specified\n"); 1224 return ret; 1225 } 1226 if (newval->string[0] == '+') 1227 ret = bond_option_arp_ip_target_add(bond, target); 1228 else if (newval->string[0] == '-') 1229 ret = bond_option_arp_ip_target_rem(bond, target); 1230 else 1231 netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n"); 1232 } else { 1233 target = newval->value; 1234 ret = bond_option_arp_ip_target_add(bond, target); 1235 } 1236 1237 return ret; 1238 } 1239 1240 #if IS_ENABLED(CONFIG_IPV6) 1241 static bool slave_can_set_ns_maddr(const struct bonding *bond, struct slave *slave) 1242 { 1243 return BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP && 1244 !bond_is_active_slave(slave) && 1245 slave->dev->flags & IFF_MULTICAST; 1246 } 1247 1248 /** 1249 * slave_set_ns_maddrs - add/del all NS mac addresses for slave 1250 * @bond: bond device 1251 * @slave: slave device 1252 * @add: add or remove all the NS mac addresses 1253 * 1254 * This function tries to add or delete all the NS mac addresses on the slave 1255 * 1256 * Note, the IPv6 NS target address is the unicast address in Neighbor 1257 * Solicitation (NS) message. The dest address of NS message should be 1258 * solicited-node multicast address of the target. The dest mac of NS message 1259 * is converted from the solicited-node multicast address. 1260 * 1261 * This function is called when 1262 * * arp_validate changes 1263 * * enslaving, releasing new slaves 1264 */ 1265 static void slave_set_ns_maddrs(struct bonding *bond, struct slave *slave, bool add) 1266 { 1267 struct in6_addr *targets = bond->params.ns_targets; 1268 char slot_maddr[MAX_ADDR_LEN]; 1269 struct in6_addr mcaddr; 1270 int i; 1271 1272 if (!slave_can_set_ns_maddr(bond, slave)) 1273 return; 1274 1275 for (i = 0; i < BOND_MAX_NS_TARGETS; i++) { 1276 if (ipv6_addr_any(&targets[i])) 1277 break; 1278 1279 addrconf_addr_solict_mult(&targets[i], &mcaddr); 1280 if (!ndisc_mc_map(&mcaddr, slot_maddr, slave->dev, 0)) { 1281 if (add) 1282 dev_mc_add(slave->dev, slot_maddr); 1283 else 1284 dev_mc_del(slave->dev, slot_maddr); 1285 } 1286 } 1287 } 1288 1289 void bond_slave_ns_maddrs_add(struct bonding *bond, struct slave *slave) 1290 { 1291 if (!bond->params.arp_validate) 1292 return; 1293 slave_set_ns_maddrs(bond, slave, true); 1294 } 1295 1296 void bond_slave_ns_maddrs_del(struct bonding *bond, struct slave *slave) 1297 { 1298 if (!bond->params.arp_validate) 1299 return; 1300 slave_set_ns_maddrs(bond, slave, false); 1301 } 1302 1303 /** 1304 * slave_set_ns_maddr - set new NS mac address for slave 1305 * @bond: bond device 1306 * @slave: slave device 1307 * @target: the new IPv6 target 1308 * @slot: the old IPv6 target in the slot 1309 * 1310 * This function tries to replace the old mac address to new one on the slave. 1311 * 1312 * Note, the target/slot IPv6 address is the unicast address in Neighbor 1313 * Solicitation (NS) message. The dest address of NS message should be 1314 * solicited-node multicast address of the target. The dest mac of NS message 1315 * is converted from the solicited-node multicast address. 1316 * 1317 * This function is called when 1318 * * An IPv6 NS target is added or removed. 1319 */ 1320 static void slave_set_ns_maddr(struct bonding *bond, struct slave *slave, 1321 struct in6_addr *target, struct in6_addr *slot) 1322 { 1323 char mac_addr[MAX_ADDR_LEN]; 1324 struct in6_addr mcast_addr; 1325 1326 if (!bond->params.arp_validate || !slave_can_set_ns_maddr(bond, slave)) 1327 return; 1328 1329 /* remove the previous mac addr from slave */ 1330 addrconf_addr_solict_mult(slot, &mcast_addr); 1331 if (!ipv6_addr_any(slot) && 1332 !ndisc_mc_map(&mcast_addr, mac_addr, slave->dev, 0)) 1333 dev_mc_del(slave->dev, mac_addr); 1334 1335 /* add new mac addr on slave if target is set */ 1336 addrconf_addr_solict_mult(target, &mcast_addr); 1337 if (!ipv6_addr_any(target) && 1338 !ndisc_mc_map(&mcast_addr, mac_addr, slave->dev, 0)) 1339 dev_mc_add(slave->dev, mac_addr); 1340 } 1341 1342 static void _bond_options_ns_ip6_target_set(struct bonding *bond, int slot, 1343 struct in6_addr *target, 1344 unsigned long last_rx) 1345 { 1346 struct in6_addr *targets = bond->params.ns_targets; 1347 struct list_head *iter; 1348 struct slave *slave; 1349 1350 if (slot >= 0 && slot < BOND_MAX_NS_TARGETS) { 1351 bond_for_each_slave(bond, slave, iter) { 1352 slave->target_last_arp_rx[slot] = last_rx; 1353 slave_set_ns_maddr(bond, slave, target, &targets[slot]); 1354 } 1355 targets[slot] = *target; 1356 } 1357 } 1358 1359 void bond_option_ns_ip6_targets_clear(struct bonding *bond) 1360 { 1361 struct in6_addr addr_any = in6addr_any; 1362 int i; 1363 1364 for (i = 0; i < BOND_MAX_NS_TARGETS; i++) 1365 _bond_options_ns_ip6_target_set(bond, i, &addr_any, 0); 1366 } 1367 1368 static int bond_option_ns_ip6_targets_set(struct bonding *bond, 1369 const struct bond_opt_value *newval) 1370 { 1371 struct in6_addr *target = (struct in6_addr *)newval->extra; 1372 struct in6_addr *targets = bond->params.ns_targets; 1373 struct in6_addr addr_any = in6addr_any; 1374 int index; 1375 1376 if (!bond_is_ip6_target_ok(target)) { 1377 netdev_err(bond->dev, "invalid NS target %pI6c specified for addition\n", 1378 target); 1379 return -EINVAL; 1380 } 1381 1382 if (bond_get_targets_ip6(targets, target) != -1) { /* dup */ 1383 netdev_err(bond->dev, "NS target %pI6c is already present\n", 1384 target); 1385 return -EINVAL; 1386 } 1387 1388 index = bond_get_targets_ip6(targets, &addr_any); /* first free slot */ 1389 if (index == -1) { 1390 netdev_err(bond->dev, "NS target table is full!\n"); 1391 return -EINVAL; 1392 } 1393 1394 netdev_dbg(bond->dev, "Adding NS target %pI6c\n", target); 1395 1396 _bond_options_ns_ip6_target_set(bond, index, target, jiffies); 1397 1398 return 0; 1399 } 1400 #else 1401 static int bond_option_ns_ip6_targets_set(struct bonding *bond, 1402 const struct bond_opt_value *newval) 1403 { 1404 return -EPERM; 1405 } 1406 1407 static void slave_set_ns_maddrs(struct bonding *bond, struct slave *slave, bool add) {} 1408 1409 void bond_slave_ns_maddrs_add(struct bonding *bond, struct slave *slave) {} 1410 1411 void bond_slave_ns_maddrs_del(struct bonding *bond, struct slave *slave) {} 1412 #endif 1413 1414 static int bond_option_arp_validate_set(struct bonding *bond, 1415 const struct bond_opt_value *newval) 1416 { 1417 bool changed = !!bond->params.arp_validate != !!newval->value; 1418 struct list_head *iter; 1419 struct slave *slave; 1420 1421 netdev_dbg(bond->dev, "Setting arp_validate to %s (%llu)\n", 1422 newval->string, newval->value); 1423 bond->params.arp_validate = newval->value; 1424 1425 if (changed) { 1426 bond_for_each_slave(bond, slave, iter) 1427 slave_set_ns_maddrs(bond, slave, !!bond->params.arp_validate); 1428 } 1429 1430 return 0; 1431 } 1432 1433 static int bond_option_arp_all_targets_set(struct bonding *bond, 1434 const struct bond_opt_value *newval) 1435 { 1436 netdev_dbg(bond->dev, "Setting arp_all_targets to %s (%llu)\n", 1437 newval->string, newval->value); 1438 bond->params.arp_all_targets = newval->value; 1439 1440 return 0; 1441 } 1442 1443 static int bond_option_missed_max_set(struct bonding *bond, 1444 const struct bond_opt_value *newval) 1445 { 1446 netdev_dbg(bond->dev, "Setting missed max to %s (%llu)\n", 1447 newval->string, newval->value); 1448 bond->params.missed_max = newval->value; 1449 1450 return 0; 1451 } 1452 1453 static int bond_option_prio_set(struct bonding *bond, 1454 const struct bond_opt_value *newval) 1455 { 1456 struct slave *slave; 1457 1458 slave = bond_slave_get_rtnl(newval->slave_dev); 1459 if (!slave) { 1460 netdev_dbg(newval->slave_dev, "%s called on NULL slave\n", __func__); 1461 return -ENODEV; 1462 } 1463 slave->prio = newval->value; 1464 1465 if (rtnl_dereference(bond->primary_slave)) 1466 slave_warn(bond->dev, slave->dev, 1467 "prio updated, but will not affect failover re-selection as primary slave have been set\n"); 1468 else 1469 bond_select_active_slave(bond); 1470 1471 return 0; 1472 } 1473 1474 static int bond_option_primary_set(struct bonding *bond, 1475 const struct bond_opt_value *newval) 1476 { 1477 char *p, *primary = newval->string; 1478 struct list_head *iter; 1479 struct slave *slave; 1480 1481 block_netpoll_tx(); 1482 1483 p = strchr(primary, '\n'); 1484 if (p) 1485 *p = '\0'; 1486 /* check to see if we are clearing primary */ 1487 if (!strlen(primary)) { 1488 netdev_dbg(bond->dev, "Setting primary slave to None\n"); 1489 RCU_INIT_POINTER(bond->primary_slave, NULL); 1490 memset(bond->params.primary, 0, sizeof(bond->params.primary)); 1491 bond_select_active_slave(bond); 1492 goto out; 1493 } 1494 1495 bond_for_each_slave(bond, slave, iter) { 1496 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) { 1497 slave_dbg(bond->dev, slave->dev, "Setting as primary slave\n"); 1498 rcu_assign_pointer(bond->primary_slave, slave); 1499 strcpy(bond->params.primary, slave->dev->name); 1500 bond->force_primary = true; 1501 bond_select_active_slave(bond); 1502 goto out; 1503 } 1504 } 1505 1506 if (rtnl_dereference(bond->primary_slave)) { 1507 netdev_dbg(bond->dev, "Setting primary slave to None\n"); 1508 RCU_INIT_POINTER(bond->primary_slave, NULL); 1509 bond_select_active_slave(bond); 1510 } 1511 strscpy_pad(bond->params.primary, primary, IFNAMSIZ); 1512 1513 netdev_dbg(bond->dev, "Recording %s as primary, but it has not been enslaved yet\n", 1514 primary); 1515 1516 out: 1517 unblock_netpoll_tx(); 1518 1519 return 0; 1520 } 1521 1522 static int bond_option_primary_reselect_set(struct bonding *bond, 1523 const struct bond_opt_value *newval) 1524 { 1525 netdev_dbg(bond->dev, "Setting primary_reselect to %s (%llu)\n", 1526 newval->string, newval->value); 1527 bond->params.primary_reselect = newval->value; 1528 1529 block_netpoll_tx(); 1530 bond_select_active_slave(bond); 1531 unblock_netpoll_tx(); 1532 1533 return 0; 1534 } 1535 1536 static int bond_option_fail_over_mac_set(struct bonding *bond, 1537 const struct bond_opt_value *newval) 1538 { 1539 netdev_dbg(bond->dev, "Setting fail_over_mac to %s (%llu)\n", 1540 newval->string, newval->value); 1541 bond->params.fail_over_mac = newval->value; 1542 1543 return 0; 1544 } 1545 1546 static int bond_option_xmit_hash_policy_set(struct bonding *bond, 1547 const struct bond_opt_value *newval) 1548 { 1549 netdev_dbg(bond->dev, "Setting xmit hash policy to %s (%llu)\n", 1550 newval->string, newval->value); 1551 bond->params.xmit_policy = newval->value; 1552 1553 return 0; 1554 } 1555 1556 static int bond_option_resend_igmp_set(struct bonding *bond, 1557 const struct bond_opt_value *newval) 1558 { 1559 netdev_dbg(bond->dev, "Setting resend_igmp to %llu\n", 1560 newval->value); 1561 bond->params.resend_igmp = newval->value; 1562 1563 return 0; 1564 } 1565 1566 static int bond_option_num_peer_notif_set(struct bonding *bond, 1567 const struct bond_opt_value *newval) 1568 { 1569 bond->params.num_peer_notif = newval->value; 1570 1571 return 0; 1572 } 1573 1574 static int bond_option_all_slaves_active_set(struct bonding *bond, 1575 const struct bond_opt_value *newval) 1576 { 1577 struct list_head *iter; 1578 struct slave *slave; 1579 1580 if (newval->value == bond->params.all_slaves_active) 1581 return 0; 1582 bond->params.all_slaves_active = newval->value; 1583 bond_for_each_slave(bond, slave, iter) { 1584 if (!bond_is_active_slave(slave)) { 1585 if (newval->value) 1586 slave->inactive = 0; 1587 else 1588 slave->inactive = 1; 1589 } 1590 } 1591 1592 return 0; 1593 } 1594 1595 static int bond_option_min_links_set(struct bonding *bond, 1596 const struct bond_opt_value *newval) 1597 { 1598 netdev_dbg(bond->dev, "Setting min links value to %llu\n", 1599 newval->value); 1600 bond->params.min_links = newval->value; 1601 bond_set_carrier(bond); 1602 1603 return 0; 1604 } 1605 1606 static int bond_option_lp_interval_set(struct bonding *bond, 1607 const struct bond_opt_value *newval) 1608 { 1609 bond->params.lp_interval = newval->value; 1610 1611 return 0; 1612 } 1613 1614 static int bond_option_pps_set(struct bonding *bond, 1615 const struct bond_opt_value *newval) 1616 { 1617 netdev_dbg(bond->dev, "Setting packets per slave to %llu\n", 1618 newval->value); 1619 bond->params.packets_per_slave = newval->value; 1620 if (newval->value > 0) { 1621 bond->params.reciprocal_packets_per_slave = 1622 reciprocal_value(newval->value); 1623 } else { 1624 /* reciprocal_packets_per_slave is unused if 1625 * packets_per_slave is 0 or 1, just initialize it 1626 */ 1627 bond->params.reciprocal_packets_per_slave = 1628 (struct reciprocal_value) { 0 }; 1629 } 1630 1631 return 0; 1632 } 1633 1634 static int bond_option_lacp_active_set(struct bonding *bond, 1635 const struct bond_opt_value *newval) 1636 { 1637 netdev_dbg(bond->dev, "Setting LACP active to %s (%llu)\n", 1638 newval->string, newval->value); 1639 bond->params.lacp_active = newval->value; 1640 1641 return 0; 1642 } 1643 1644 static int bond_option_lacp_rate_set(struct bonding *bond, 1645 const struct bond_opt_value *newval) 1646 { 1647 netdev_dbg(bond->dev, "Setting LACP rate to %s (%llu)\n", 1648 newval->string, newval->value); 1649 bond->params.lacp_fast = newval->value; 1650 bond_3ad_update_lacp_rate(bond); 1651 1652 return 0; 1653 } 1654 1655 static int bond_option_ad_select_set(struct bonding *bond, 1656 const struct bond_opt_value *newval) 1657 { 1658 netdev_dbg(bond->dev, "Setting ad_select to %s (%llu)\n", 1659 newval->string, newval->value); 1660 bond->params.ad_select = newval->value; 1661 1662 return 0; 1663 } 1664 1665 static int bond_option_queue_id_set(struct bonding *bond, 1666 const struct bond_opt_value *newval) 1667 { 1668 struct slave *slave, *update_slave; 1669 struct net_device *sdev; 1670 struct list_head *iter; 1671 char *delim; 1672 int ret = 0; 1673 u16 qid; 1674 1675 /* delim will point to queue id if successful */ 1676 delim = strchr(newval->string, ':'); 1677 if (!delim) 1678 goto err_no_cmd; 1679 1680 /* Terminate string that points to device name and bump it 1681 * up one, so we can read the queue id there. 1682 */ 1683 *delim = '\0'; 1684 if (sscanf(++delim, "%hd\n", &qid) != 1) 1685 goto err_no_cmd; 1686 1687 /* Check buffer length, valid ifname and queue id */ 1688 if (!dev_valid_name(newval->string) || 1689 qid > bond->dev->real_num_tx_queues) 1690 goto err_no_cmd; 1691 1692 /* Get the pointer to that interface if it exists */ 1693 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string); 1694 if (!sdev) 1695 goto err_no_cmd; 1696 1697 /* Search for thes slave and check for duplicate qids */ 1698 update_slave = NULL; 1699 bond_for_each_slave(bond, slave, iter) { 1700 if (sdev == slave->dev) 1701 /* We don't need to check the matching 1702 * slave for dups, since we're overwriting it 1703 */ 1704 update_slave = slave; 1705 else if (qid && qid == slave->queue_id) { 1706 goto err_no_cmd; 1707 } 1708 } 1709 1710 if (!update_slave) 1711 goto err_no_cmd; 1712 1713 /* Actually set the qids for the slave */ 1714 WRITE_ONCE(update_slave->queue_id, qid); 1715 1716 out: 1717 return ret; 1718 1719 err_no_cmd: 1720 netdev_dbg(bond->dev, "invalid input for queue_id set\n"); 1721 ret = -EPERM; 1722 goto out; 1723 1724 } 1725 1726 static int bond_option_slaves_set(struct bonding *bond, 1727 const struct bond_opt_value *newval) 1728 { 1729 char command[IFNAMSIZ + 1] = { 0, }; 1730 struct net_device *dev; 1731 char *ifname; 1732 int ret; 1733 1734 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/ 1735 ifname = command + 1; 1736 if ((strlen(command) <= 1) || 1737 (command[0] != '+' && command[0] != '-') || 1738 !dev_valid_name(ifname)) 1739 goto err_no_cmd; 1740 1741 dev = __dev_get_by_name(dev_net(bond->dev), ifname); 1742 if (!dev) { 1743 netdev_dbg(bond->dev, "interface %s does not exist!\n", 1744 ifname); 1745 ret = -ENODEV; 1746 goto out; 1747 } 1748 1749 switch (command[0]) { 1750 case '+': 1751 slave_dbg(bond->dev, dev, "Enslaving interface\n"); 1752 ret = bond_enslave(bond->dev, dev, NULL); 1753 break; 1754 1755 case '-': 1756 slave_dbg(bond->dev, dev, "Releasing interface\n"); 1757 ret = bond_release(bond->dev, dev); 1758 break; 1759 1760 default: 1761 /* should not run here. */ 1762 goto err_no_cmd; 1763 } 1764 1765 out: 1766 return ret; 1767 1768 err_no_cmd: 1769 netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n"); 1770 ret = -EPERM; 1771 goto out; 1772 } 1773 1774 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond, 1775 const struct bond_opt_value *newval) 1776 { 1777 netdev_dbg(bond->dev, "Setting dynamic-lb to %s (%llu)\n", 1778 newval->string, newval->value); 1779 bond->params.tlb_dynamic_lb = newval->value; 1780 1781 return 0; 1782 } 1783 1784 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond, 1785 const struct bond_opt_value *newval) 1786 { 1787 netdev_dbg(bond->dev, "Setting ad_actor_sys_prio to %llu\n", 1788 newval->value); 1789 1790 bond->params.ad_actor_sys_prio = newval->value; 1791 bond_3ad_update_ad_actor_settings(bond); 1792 1793 return 0; 1794 } 1795 1796 static int bond_option_ad_actor_system_set(struct bonding *bond, 1797 const struct bond_opt_value *newval) 1798 { 1799 u8 macaddr[ETH_ALEN]; 1800 u8 *mac; 1801 1802 if (newval->string) { 1803 if (!mac_pton(newval->string, macaddr)) 1804 goto err; 1805 mac = macaddr; 1806 } else { 1807 mac = (u8 *)&newval->value; 1808 } 1809 1810 if (is_multicast_ether_addr(mac)) 1811 goto err; 1812 1813 netdev_dbg(bond->dev, "Setting ad_actor_system to %pM\n", mac); 1814 ether_addr_copy(bond->params.ad_actor_system, mac); 1815 bond_3ad_update_ad_actor_settings(bond); 1816 1817 return 0; 1818 1819 err: 1820 netdev_err(bond->dev, "Invalid ad_actor_system MAC address.\n"); 1821 return -EINVAL; 1822 } 1823 1824 static int bond_option_ad_user_port_key_set(struct bonding *bond, 1825 const struct bond_opt_value *newval) 1826 { 1827 netdev_dbg(bond->dev, "Setting ad_user_port_key to %llu\n", 1828 newval->value); 1829 1830 bond->params.ad_user_port_key = newval->value; 1831 return 0; 1832 } 1833 1834 static int bond_option_coupled_control_set(struct bonding *bond, 1835 const struct bond_opt_value *newval) 1836 { 1837 netdev_info(bond->dev, "Setting coupled_control to %s (%llu)\n", 1838 newval->string, newval->value); 1839 1840 bond->params.coupled_control = newval->value; 1841 return 0; 1842 } 1843