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