1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding 4 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us> 5 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/errno.h> 10 #include <linux/netdevice.h> 11 #include <linux/etherdevice.h> 12 #include <linux/if_link.h> 13 #include <linux/if_ether.h> 14 #include <net/netlink.h> 15 #include <net/rtnetlink.h> 16 #include <net/bonding.h> 17 #include <net/ipv6.h> 18 19 static size_t bond_get_slave_size(const struct net_device *bond_dev, 20 const struct net_device *slave_dev) 21 { 22 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_STATE */ 23 nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_MII_STATUS */ 24 nla_total_size(sizeof(u32)) + /* IFLA_BOND_SLAVE_LINK_FAILURE_COUNT */ 25 nla_total_size(MAX_ADDR_LEN) + /* IFLA_BOND_SLAVE_PERM_HWADDR */ 26 nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_QUEUE_ID */ 27 nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */ 28 nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */ 29 nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */ 30 nla_total_size(sizeof(s32)) + /* IFLA_BOND_SLAVE_PRIO */ 31 nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_ACTOR_PORT_PRIO */ 32 0; 33 } 34 35 static int bond_fill_slave_info(struct sk_buff *skb, 36 const struct net_device *bond_dev, 37 const struct net_device *slave_dev) 38 { 39 struct slave *slave = bond_slave_get_rtnl(slave_dev); 40 41 if (nla_put_u8(skb, IFLA_BOND_SLAVE_STATE, bond_slave_state(slave))) 42 goto nla_put_failure; 43 44 if (nla_put_u8(skb, IFLA_BOND_SLAVE_MII_STATUS, slave->link)) 45 goto nla_put_failure; 46 47 if (nla_put_u32(skb, IFLA_BOND_SLAVE_LINK_FAILURE_COUNT, 48 slave->link_failure_count)) 49 goto nla_put_failure; 50 51 if (nla_put(skb, IFLA_BOND_SLAVE_PERM_HWADDR, 52 slave_dev->addr_len, slave->perm_hwaddr)) 53 goto nla_put_failure; 54 55 if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, 56 READ_ONCE(slave->queue_id))) 57 goto nla_put_failure; 58 59 if (nla_put_s32(skb, IFLA_BOND_SLAVE_PRIO, slave->prio)) 60 goto nla_put_failure; 61 62 if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) { 63 const struct aggregator *agg; 64 const struct port *ad_port; 65 66 ad_port = &SLAVE_AD_INFO(slave)->port; 67 agg = SLAVE_AD_INFO(slave)->port.aggregator; 68 if (agg) { 69 if (nla_put_u16(skb, IFLA_BOND_SLAVE_AD_AGGREGATOR_ID, 70 agg->aggregator_identifier)) 71 goto nla_put_failure; 72 if (nla_put_u8(skb, 73 IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE, 74 ad_port->actor_oper_port_state)) 75 goto nla_put_failure; 76 if (nla_put_u16(skb, 77 IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE, 78 ad_port->partner_oper.port_state)) 79 goto nla_put_failure; 80 } 81 82 if (nla_put_u16(skb, IFLA_BOND_SLAVE_ACTOR_PORT_PRIO, 83 SLAVE_AD_INFO(slave)->port_priority)) 84 goto nla_put_failure; 85 } 86 87 return 0; 88 89 nla_put_failure: 90 return -EMSGSIZE; 91 } 92 93 /* Limit the max delay range to 300s */ 94 static const struct netlink_range_validation delay_range = { 95 .max = 300000, 96 }; 97 98 static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = { 99 [IFLA_BOND_MODE] = { .type = NLA_U8 }, 100 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 }, 101 [IFLA_BOND_MIIMON] = { .type = NLA_U32 }, 102 [IFLA_BOND_UPDELAY] = { .type = NLA_U32 }, 103 [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 }, 104 [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 }, 105 [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 }, 106 [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED }, 107 [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 }, 108 [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 }, 109 [IFLA_BOND_PRIMARY] = { .type = NLA_U32 }, 110 [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 }, 111 [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 }, 112 [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 }, 113 [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 }, 114 [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 }, 115 [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 }, 116 [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 }, 117 [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 }, 118 [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NLA_U32 }, 119 [IFLA_BOND_AD_LACP_ACTIVE] = { .type = NLA_U8 }, 120 [IFLA_BOND_AD_LACP_RATE] = { .type = NLA_U8 }, 121 [IFLA_BOND_AD_SELECT] = { .type = NLA_U8 }, 122 [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED }, 123 [IFLA_BOND_AD_ACTOR_SYS_PRIO] = { .type = NLA_U16 }, 124 [IFLA_BOND_AD_USER_PORT_KEY] = { .type = NLA_U16 }, 125 [IFLA_BOND_AD_ACTOR_SYSTEM] = { .type = NLA_BINARY, 126 .len = ETH_ALEN }, 127 [IFLA_BOND_TLB_DYNAMIC_LB] = { .type = NLA_U8 }, 128 [IFLA_BOND_PEER_NOTIF_DELAY] = NLA_POLICY_FULL_RANGE(NLA_U32, &delay_range), 129 [IFLA_BOND_MISSED_MAX] = { .type = NLA_U8 }, 130 [IFLA_BOND_NS_IP6_TARGET] = { .type = NLA_NESTED }, 131 [IFLA_BOND_COUPLED_CONTROL] = { .type = NLA_U8 }, 132 [IFLA_BOND_BROADCAST_NEIGH] = { .type = NLA_U8 }, 133 }; 134 135 static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = { 136 [IFLA_BOND_SLAVE_QUEUE_ID] = { .type = NLA_U16 }, 137 [IFLA_BOND_SLAVE_PRIO] = { .type = NLA_S32 }, 138 [IFLA_BOND_SLAVE_ACTOR_PORT_PRIO] = { .type = NLA_U16 }, 139 }; 140 141 static int bond_validate(struct nlattr *tb[], struct nlattr *data[], 142 struct netlink_ext_ack *extack) 143 { 144 if (tb[IFLA_ADDRESS]) { 145 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 146 return -EINVAL; 147 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 148 return -EADDRNOTAVAIL; 149 } 150 return 0; 151 } 152 153 static int bond_slave_changelink(struct net_device *bond_dev, 154 struct net_device *slave_dev, 155 struct nlattr *tb[], struct nlattr *data[], 156 struct netlink_ext_ack *extack) 157 { 158 struct bonding *bond = netdev_priv(bond_dev); 159 struct bond_opt_value newval; 160 int err; 161 162 if (!data) 163 return 0; 164 165 if (data[IFLA_BOND_SLAVE_QUEUE_ID]) { 166 u16 queue_id = nla_get_u16(data[IFLA_BOND_SLAVE_QUEUE_ID]); 167 char queue_id_str[IFNAMSIZ + 7]; 168 169 /* queue_id option setting expects slave_name:queue_id */ 170 snprintf(queue_id_str, sizeof(queue_id_str), "%s:%u\n", 171 slave_dev->name, queue_id); 172 bond_opt_initstr(&newval, queue_id_str); 173 err = __bond_opt_set(bond, BOND_OPT_QUEUE_ID, &newval, 174 data[IFLA_BOND_SLAVE_QUEUE_ID], extack); 175 if (err) 176 return err; 177 } 178 179 if (data[IFLA_BOND_SLAVE_PRIO]) { 180 int prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]); 181 182 bond_opt_slave_initval(&newval, &slave_dev, prio); 183 err = __bond_opt_set(bond, BOND_OPT_PRIO, &newval, 184 data[IFLA_BOND_SLAVE_PRIO], extack); 185 if (err) 186 return err; 187 } 188 189 if (data[IFLA_BOND_SLAVE_ACTOR_PORT_PRIO]) { 190 u16 ad_prio = nla_get_u16(data[IFLA_BOND_SLAVE_ACTOR_PORT_PRIO]); 191 192 bond_opt_slave_initval(&newval, &slave_dev, ad_prio); 193 err = __bond_opt_set(bond, BOND_OPT_ACTOR_PORT_PRIO, &newval, 194 data[IFLA_BOND_SLAVE_ACTOR_PORT_PRIO], extack); 195 if (err) 196 return err; 197 } 198 199 return 0; 200 } 201 202 static int bond_changelink(struct net_device *bond_dev, struct nlattr *tb[], 203 struct nlattr *data[], 204 struct netlink_ext_ack *extack) 205 { 206 struct bonding *bond = netdev_priv(bond_dev); 207 struct bond_opt_value newval; 208 int miimon = 0; 209 int err; 210 211 if (!data) 212 return 0; 213 214 if (data[IFLA_BOND_MODE]) { 215 int mode = nla_get_u8(data[IFLA_BOND_MODE]); 216 217 bond_opt_initval(&newval, mode); 218 err = __bond_opt_set(bond, BOND_OPT_MODE, &newval, 219 data[IFLA_BOND_MODE], extack); 220 if (err) 221 return err; 222 } 223 if (data[IFLA_BOND_ACTIVE_SLAVE]) { 224 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]); 225 struct net_device *slave_dev; 226 char *active_slave = ""; 227 228 if (ifindex != 0) { 229 slave_dev = __dev_get_by_index(dev_net(bond_dev), 230 ifindex); 231 if (!slave_dev) 232 return -ENODEV; 233 active_slave = slave_dev->name; 234 } 235 bond_opt_initstr(&newval, active_slave); 236 err = __bond_opt_set(bond, BOND_OPT_ACTIVE_SLAVE, &newval, 237 data[IFLA_BOND_ACTIVE_SLAVE], extack); 238 if (err) 239 return err; 240 } 241 if (data[IFLA_BOND_MIIMON]) { 242 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]); 243 244 bond_opt_initval(&newval, miimon); 245 err = __bond_opt_set(bond, BOND_OPT_MIIMON, &newval, 246 data[IFLA_BOND_MIIMON], extack); 247 if (err) 248 return err; 249 } 250 if (data[IFLA_BOND_UPDELAY]) { 251 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]); 252 253 bond_opt_initval(&newval, updelay); 254 err = __bond_opt_set(bond, BOND_OPT_UPDELAY, &newval, 255 data[IFLA_BOND_UPDELAY], extack); 256 if (err) 257 return err; 258 } 259 if (data[IFLA_BOND_DOWNDELAY]) { 260 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]); 261 262 bond_opt_initval(&newval, downdelay); 263 err = __bond_opt_set(bond, BOND_OPT_DOWNDELAY, &newval, 264 data[IFLA_BOND_DOWNDELAY], extack); 265 if (err) 266 return err; 267 } 268 if (data[IFLA_BOND_PEER_NOTIF_DELAY]) { 269 int delay = nla_get_u32(data[IFLA_BOND_PEER_NOTIF_DELAY]); 270 271 bond_opt_initval(&newval, delay); 272 err = __bond_opt_set(bond, BOND_OPT_PEER_NOTIF_DELAY, &newval, 273 data[IFLA_BOND_PEER_NOTIF_DELAY], extack); 274 if (err) 275 return err; 276 } 277 if (data[IFLA_BOND_USE_CARRIER]) { 278 if (nla_get_u8(data[IFLA_BOND_USE_CARRIER]) != 1) { 279 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_USE_CARRIER], 280 "option obsolete, use_carrier cannot be disabled"); 281 return -EINVAL; 282 } 283 } 284 if (data[IFLA_BOND_ARP_INTERVAL]) { 285 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]); 286 287 if (arp_interval && miimon) { 288 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_ARP_INTERVAL], 289 "ARP monitoring cannot be used with MII monitoring"); 290 return -EINVAL; 291 } 292 293 bond_opt_initval(&newval, arp_interval); 294 err = __bond_opt_set(bond, BOND_OPT_ARP_INTERVAL, &newval, 295 data[IFLA_BOND_ARP_INTERVAL], extack); 296 if (err) 297 return err; 298 } 299 if (data[IFLA_BOND_ARP_IP_TARGET]) { 300 struct nlattr *attr; 301 int i = 0, rem; 302 303 bond_option_arp_ip_targets_clear(bond); 304 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) { 305 __be32 target; 306 307 if (nla_len(attr) < sizeof(target)) 308 return -EINVAL; 309 310 target = nla_get_be32(attr); 311 312 bond_opt_initval(&newval, (__force u64)target); 313 err = __bond_opt_set(bond, BOND_OPT_ARP_TARGETS, 314 &newval, 315 data[IFLA_BOND_ARP_IP_TARGET], 316 extack); 317 if (err) 318 break; 319 i++; 320 } 321 if (i == 0 && bond->params.arp_interval) 322 netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n"); 323 if (err) 324 return err; 325 } 326 #if IS_ENABLED(CONFIG_IPV6) 327 if (data[IFLA_BOND_NS_IP6_TARGET]) { 328 struct nlattr *attr; 329 int i = 0, rem; 330 331 bond_option_ns_ip6_targets_clear(bond); 332 nla_for_each_nested(attr, data[IFLA_BOND_NS_IP6_TARGET], rem) { 333 struct in6_addr addr6; 334 335 if (nla_len(attr) < sizeof(addr6)) { 336 NL_SET_ERR_MSG(extack, "Invalid IPv6 address"); 337 return -EINVAL; 338 } 339 340 addr6 = nla_get_in6_addr(attr); 341 342 bond_opt_initextra(&newval, &addr6, sizeof(addr6)); 343 err = __bond_opt_set(bond, BOND_OPT_NS_TARGETS, 344 &newval, 345 data[IFLA_BOND_NS_IP6_TARGET], 346 extack); 347 if (err) 348 break; 349 i++; 350 } 351 if (i == 0 && bond->params.arp_interval) 352 netdev_warn(bond->dev, "Removing last ns target with arp_interval on\n"); 353 if (err) 354 return err; 355 } 356 #endif 357 if (data[IFLA_BOND_ARP_VALIDATE]) { 358 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]); 359 360 if (arp_validate && miimon) { 361 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_ARP_INTERVAL], 362 "ARP validating cannot be used with MII monitoring"); 363 return -EINVAL; 364 } 365 366 bond_opt_initval(&newval, arp_validate); 367 err = __bond_opt_set(bond, BOND_OPT_ARP_VALIDATE, &newval, 368 data[IFLA_BOND_ARP_VALIDATE], extack); 369 if (err) 370 return err; 371 } 372 if (data[IFLA_BOND_ARP_ALL_TARGETS]) { 373 int arp_all_targets = 374 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]); 375 376 bond_opt_initval(&newval, arp_all_targets); 377 err = __bond_opt_set(bond, BOND_OPT_ARP_ALL_TARGETS, &newval, 378 data[IFLA_BOND_ARP_ALL_TARGETS], extack); 379 if (err) 380 return err; 381 } 382 if (data[IFLA_BOND_PRIMARY]) { 383 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]); 384 struct net_device *dev; 385 char *primary = ""; 386 387 dev = __dev_get_by_index(dev_net(bond_dev), ifindex); 388 if (dev) 389 primary = dev->name; 390 391 bond_opt_initstr(&newval, primary); 392 err = __bond_opt_set(bond, BOND_OPT_PRIMARY, &newval, 393 data[IFLA_BOND_PRIMARY], extack); 394 if (err) 395 return err; 396 } 397 if (data[IFLA_BOND_PRIMARY_RESELECT]) { 398 int primary_reselect = 399 nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]); 400 401 bond_opt_initval(&newval, primary_reselect); 402 err = __bond_opt_set(bond, BOND_OPT_PRIMARY_RESELECT, &newval, 403 data[IFLA_BOND_PRIMARY_RESELECT], extack); 404 if (err) 405 return err; 406 } 407 if (data[IFLA_BOND_FAIL_OVER_MAC]) { 408 int fail_over_mac = 409 nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]); 410 411 bond_opt_initval(&newval, fail_over_mac); 412 err = __bond_opt_set(bond, BOND_OPT_FAIL_OVER_MAC, &newval, 413 data[IFLA_BOND_FAIL_OVER_MAC], extack); 414 if (err) 415 return err; 416 } 417 if (data[IFLA_BOND_XMIT_HASH_POLICY]) { 418 int xmit_hash_policy = 419 nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]); 420 421 bond_opt_initval(&newval, xmit_hash_policy); 422 err = __bond_opt_set(bond, BOND_OPT_XMIT_HASH, &newval, 423 data[IFLA_BOND_XMIT_HASH_POLICY], extack); 424 if (err) 425 return err; 426 } 427 if (data[IFLA_BOND_RESEND_IGMP]) { 428 int resend_igmp = 429 nla_get_u32(data[IFLA_BOND_RESEND_IGMP]); 430 431 bond_opt_initval(&newval, resend_igmp); 432 err = __bond_opt_set(bond, BOND_OPT_RESEND_IGMP, &newval, 433 data[IFLA_BOND_RESEND_IGMP], extack); 434 if (err) 435 return err; 436 } 437 if (data[IFLA_BOND_NUM_PEER_NOTIF]) { 438 int num_peer_notif = 439 nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]); 440 441 bond_opt_initval(&newval, num_peer_notif); 442 err = __bond_opt_set(bond, BOND_OPT_NUM_PEER_NOTIF, &newval, 443 data[IFLA_BOND_NUM_PEER_NOTIF], extack); 444 if (err) 445 return err; 446 } 447 if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) { 448 int all_slaves_active = 449 nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]); 450 451 bond_opt_initval(&newval, all_slaves_active); 452 err = __bond_opt_set(bond, BOND_OPT_ALL_SLAVES_ACTIVE, &newval, 453 data[IFLA_BOND_ALL_SLAVES_ACTIVE], extack); 454 if (err) 455 return err; 456 } 457 if (data[IFLA_BOND_MIN_LINKS]) { 458 int min_links = 459 nla_get_u32(data[IFLA_BOND_MIN_LINKS]); 460 461 bond_opt_initval(&newval, min_links); 462 err = __bond_opt_set(bond, BOND_OPT_MINLINKS, &newval, 463 data[IFLA_BOND_MIN_LINKS], extack); 464 if (err) 465 return err; 466 } 467 if (data[IFLA_BOND_LP_INTERVAL]) { 468 int lp_interval = 469 nla_get_u32(data[IFLA_BOND_LP_INTERVAL]); 470 471 bond_opt_initval(&newval, lp_interval); 472 err = __bond_opt_set(bond, BOND_OPT_LP_INTERVAL, &newval, 473 data[IFLA_BOND_LP_INTERVAL], extack); 474 if (err) 475 return err; 476 } 477 if (data[IFLA_BOND_PACKETS_PER_SLAVE]) { 478 int packets_per_slave = 479 nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]); 480 481 bond_opt_initval(&newval, packets_per_slave); 482 err = __bond_opt_set(bond, BOND_OPT_PACKETS_PER_SLAVE, &newval, 483 data[IFLA_BOND_PACKETS_PER_SLAVE], extack); 484 if (err) 485 return err; 486 } 487 488 if (data[IFLA_BOND_AD_LACP_ACTIVE]) { 489 int lacp_active = nla_get_u8(data[IFLA_BOND_AD_LACP_ACTIVE]); 490 491 bond_opt_initval(&newval, lacp_active); 492 err = __bond_opt_set(bond, BOND_OPT_LACP_ACTIVE, &newval, 493 data[IFLA_BOND_AD_LACP_ACTIVE], extack); 494 if (err) 495 return err; 496 } 497 498 if (data[IFLA_BOND_AD_LACP_RATE]) { 499 int lacp_rate = 500 nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]); 501 502 bond_opt_initval(&newval, lacp_rate); 503 err = __bond_opt_set(bond, BOND_OPT_LACP_RATE, &newval, 504 data[IFLA_BOND_AD_LACP_RATE], extack); 505 if (err) 506 return err; 507 } 508 if (data[IFLA_BOND_AD_SELECT]) { 509 int ad_select = 510 nla_get_u8(data[IFLA_BOND_AD_SELECT]); 511 512 bond_opt_initval(&newval, ad_select); 513 err = __bond_opt_set(bond, BOND_OPT_AD_SELECT, &newval, 514 data[IFLA_BOND_AD_SELECT], extack); 515 if (err) 516 return err; 517 } 518 if (data[IFLA_BOND_AD_ACTOR_SYS_PRIO]) { 519 int actor_sys_prio = 520 nla_get_u16(data[IFLA_BOND_AD_ACTOR_SYS_PRIO]); 521 522 bond_opt_initval(&newval, actor_sys_prio); 523 err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYS_PRIO, &newval, 524 data[IFLA_BOND_AD_ACTOR_SYS_PRIO], extack); 525 if (err) 526 return err; 527 } 528 if (data[IFLA_BOND_AD_USER_PORT_KEY]) { 529 int port_key = 530 nla_get_u16(data[IFLA_BOND_AD_USER_PORT_KEY]); 531 532 bond_opt_initval(&newval, port_key); 533 err = __bond_opt_set(bond, BOND_OPT_AD_USER_PORT_KEY, &newval, 534 data[IFLA_BOND_AD_USER_PORT_KEY], extack); 535 if (err) 536 return err; 537 } 538 if (data[IFLA_BOND_AD_ACTOR_SYSTEM]) { 539 if (nla_len(data[IFLA_BOND_AD_ACTOR_SYSTEM]) != ETH_ALEN) 540 return -EINVAL; 541 542 bond_opt_initval(&newval, 543 nla_get_u64(data[IFLA_BOND_AD_ACTOR_SYSTEM])); 544 err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYSTEM, &newval, 545 data[IFLA_BOND_AD_ACTOR_SYSTEM], extack); 546 if (err) 547 return err; 548 } 549 if (data[IFLA_BOND_TLB_DYNAMIC_LB]) { 550 int dynamic_lb = nla_get_u8(data[IFLA_BOND_TLB_DYNAMIC_LB]); 551 552 bond_opt_initval(&newval, dynamic_lb); 553 err = __bond_opt_set(bond, BOND_OPT_TLB_DYNAMIC_LB, &newval, 554 data[IFLA_BOND_TLB_DYNAMIC_LB], extack); 555 if (err) 556 return err; 557 } 558 559 if (data[IFLA_BOND_MISSED_MAX]) { 560 int missed_max = nla_get_u8(data[IFLA_BOND_MISSED_MAX]); 561 562 bond_opt_initval(&newval, missed_max); 563 err = __bond_opt_set(bond, BOND_OPT_MISSED_MAX, &newval, 564 data[IFLA_BOND_MISSED_MAX], extack); 565 if (err) 566 return err; 567 } 568 569 if (data[IFLA_BOND_COUPLED_CONTROL]) { 570 int coupled_control = nla_get_u8(data[IFLA_BOND_COUPLED_CONTROL]); 571 572 bond_opt_initval(&newval, coupled_control); 573 err = __bond_opt_set(bond, BOND_OPT_COUPLED_CONTROL, &newval, 574 data[IFLA_BOND_COUPLED_CONTROL], extack); 575 if (err) 576 return err; 577 } 578 579 if (data[IFLA_BOND_BROADCAST_NEIGH]) { 580 int broadcast_neigh = nla_get_u8(data[IFLA_BOND_BROADCAST_NEIGH]); 581 582 bond_opt_initval(&newval, broadcast_neigh); 583 err = __bond_opt_set(bond, BOND_OPT_BROADCAST_NEIGH, &newval, 584 data[IFLA_BOND_BROADCAST_NEIGH], extack); 585 if (err) 586 return err; 587 } 588 589 return 0; 590 } 591 592 static int bond_newlink(struct net_device *bond_dev, 593 struct rtnl_newlink_params *params, 594 struct netlink_ext_ack *extack) 595 { 596 struct nlattr **data = params->data; 597 struct nlattr **tb = params->tb; 598 int err; 599 600 err = bond_changelink(bond_dev, tb, data, extack); 601 if (err < 0) 602 return err; 603 604 err = register_netdevice(bond_dev); 605 if (!err) { 606 struct bonding *bond = netdev_priv(bond_dev); 607 608 netif_carrier_off(bond_dev); 609 bond_work_init_all(bond); 610 } 611 612 return err; 613 } 614 615 static size_t bond_get_size(const struct net_device *bond_dev) 616 { 617 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */ 618 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */ 619 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */ 620 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */ 621 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */ 622 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */ 623 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */ 624 /* IFLA_BOND_ARP_IP_TARGET */ 625 nla_total_size(sizeof(struct nlattr)) + 626 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS + 627 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */ 628 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */ 629 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */ 630 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */ 631 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */ 632 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */ 633 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */ 634 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */ 635 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */ 636 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */ 637 nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */ 638 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */ 639 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_ACTIVE */ 640 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */ 641 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */ 642 nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */ 643 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */ 644 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */ 645 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */ 646 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/ 647 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/ 648 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_ACTOR_SYS_PRIO */ 649 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_USER_PORT_KEY */ 650 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_ACTOR_SYSTEM */ 651 nla_total_size(sizeof(u8)) + /* IFLA_BOND_TLB_DYNAMIC_LB */ 652 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PEER_NOTIF_DELAY */ 653 nla_total_size(sizeof(u8)) + /* IFLA_BOND_MISSED_MAX */ 654 /* IFLA_BOND_NS_IP6_TARGET */ 655 nla_total_size(sizeof(struct nlattr)) + 656 nla_total_size(sizeof(struct in6_addr)) * BOND_MAX_NS_TARGETS + 657 nla_total_size(sizeof(u8)) + /* IFLA_BOND_COUPLED_CONTROL */ 658 nla_total_size(sizeof(u8)) + /* IFLA_BOND_BROADCAST_NEIGH */ 659 0; 660 } 661 662 static int bond_option_active_slave_get_ifindex(struct bonding *bond) 663 { 664 const struct net_device *slave; 665 int ifindex; 666 667 rcu_read_lock(); 668 slave = bond_option_active_slave_get_rcu(bond); 669 ifindex = slave ? slave->ifindex : 0; 670 rcu_read_unlock(); 671 return ifindex; 672 } 673 674 static int bond_fill_info(struct sk_buff *skb, 675 const struct net_device *bond_dev) 676 { 677 struct bonding *bond = netdev_priv(bond_dev); 678 unsigned int packets_per_slave; 679 int ifindex, i, targets_added; 680 struct nlattr *targets; 681 struct slave *primary; 682 683 if (nla_put_u8(skb, IFLA_BOND_MODE, BOND_MODE(bond))) 684 goto nla_put_failure; 685 686 ifindex = bond_option_active_slave_get_ifindex(bond); 687 if (ifindex && nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, ifindex)) 688 goto nla_put_failure; 689 690 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon)) 691 goto nla_put_failure; 692 693 if (nla_put_u32(skb, IFLA_BOND_UPDELAY, 694 bond->params.updelay * bond->params.miimon)) 695 goto nla_put_failure; 696 697 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY, 698 bond->params.downdelay * bond->params.miimon)) 699 goto nla_put_failure; 700 701 if (nla_put_u32(skb, IFLA_BOND_PEER_NOTIF_DELAY, 702 bond->params.peer_notif_delay * bond->params.miimon)) 703 goto nla_put_failure; 704 705 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, 1)) 706 goto nla_put_failure; 707 708 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval)) 709 goto nla_put_failure; 710 711 targets = nla_nest_start_noflag(skb, IFLA_BOND_ARP_IP_TARGET); 712 if (!targets) 713 goto nla_put_failure; 714 715 targets_added = 0; 716 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) { 717 if (bond->params.arp_targets[i]) { 718 if (nla_put_be32(skb, i, bond->params.arp_targets[i])) 719 goto nla_put_failure; 720 targets_added = 1; 721 } 722 } 723 724 if (targets_added) 725 nla_nest_end(skb, targets); 726 else 727 nla_nest_cancel(skb, targets); 728 729 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate)) 730 goto nla_put_failure; 731 732 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS, 733 bond->params.arp_all_targets)) 734 goto nla_put_failure; 735 736 #if IS_ENABLED(CONFIG_IPV6) 737 targets = nla_nest_start(skb, IFLA_BOND_NS_IP6_TARGET); 738 if (!targets) 739 goto nla_put_failure; 740 741 targets_added = 0; 742 for (i = 0; i < BOND_MAX_NS_TARGETS; i++) { 743 if (!ipv6_addr_any(&bond->params.ns_targets[i])) { 744 if (nla_put_in6_addr(skb, i, &bond->params.ns_targets[i])) 745 goto nla_put_failure; 746 targets_added = 1; 747 } 748 } 749 750 if (targets_added) 751 nla_nest_end(skb, targets); 752 else 753 nla_nest_cancel(skb, targets); 754 #endif 755 756 primary = rtnl_dereference(bond->primary_slave); 757 if (primary && 758 nla_put_u32(skb, IFLA_BOND_PRIMARY, primary->dev->ifindex)) 759 goto nla_put_failure; 760 761 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT, 762 bond->params.primary_reselect)) 763 goto nla_put_failure; 764 765 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC, 766 bond->params.fail_over_mac)) 767 goto nla_put_failure; 768 769 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY, 770 bond->params.xmit_policy)) 771 goto nla_put_failure; 772 773 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP, 774 bond->params.resend_igmp)) 775 goto nla_put_failure; 776 777 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF, 778 bond->params.num_peer_notif)) 779 goto nla_put_failure; 780 781 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE, 782 bond->params.all_slaves_active)) 783 goto nla_put_failure; 784 785 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS, 786 bond->params.min_links)) 787 goto nla_put_failure; 788 789 if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL, 790 bond->params.lp_interval)) 791 goto nla_put_failure; 792 793 packets_per_slave = bond->params.packets_per_slave; 794 if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE, 795 packets_per_slave)) 796 goto nla_put_failure; 797 798 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_ACTIVE, 799 bond->params.lacp_active)) 800 goto nla_put_failure; 801 802 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE, 803 bond->params.lacp_fast)) 804 goto nla_put_failure; 805 806 if (nla_put_u8(skb, IFLA_BOND_AD_SELECT, 807 bond->params.ad_select)) 808 goto nla_put_failure; 809 810 if (nla_put_u8(skb, IFLA_BOND_TLB_DYNAMIC_LB, 811 bond->params.tlb_dynamic_lb)) 812 goto nla_put_failure; 813 814 if (nla_put_u8(skb, IFLA_BOND_MISSED_MAX, 815 bond->params.missed_max)) 816 goto nla_put_failure; 817 818 if (nla_put_u8(skb, IFLA_BOND_COUPLED_CONTROL, 819 bond->params.coupled_control)) 820 goto nla_put_failure; 821 822 if (nla_put_u8(skb, IFLA_BOND_BROADCAST_NEIGH, 823 bond->params.broadcast_neighbor)) 824 goto nla_put_failure; 825 826 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 827 struct ad_info info; 828 829 if (capable(CAP_NET_ADMIN)) { 830 if (nla_put_u16(skb, IFLA_BOND_AD_ACTOR_SYS_PRIO, 831 bond->params.ad_actor_sys_prio)) 832 goto nla_put_failure; 833 834 if (nla_put_u16(skb, IFLA_BOND_AD_USER_PORT_KEY, 835 bond->params.ad_user_port_key)) 836 goto nla_put_failure; 837 838 if (nla_put(skb, IFLA_BOND_AD_ACTOR_SYSTEM, 839 ETH_ALEN, &bond->params.ad_actor_system)) 840 goto nla_put_failure; 841 } 842 if (!bond_3ad_get_active_agg_info(bond, &info)) { 843 struct nlattr *nest; 844 845 nest = nla_nest_start_noflag(skb, IFLA_BOND_AD_INFO); 846 if (!nest) 847 goto nla_put_failure; 848 849 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR, 850 info.aggregator_id)) 851 goto nla_put_failure; 852 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS, 853 info.ports)) 854 goto nla_put_failure; 855 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY, 856 info.actor_key)) 857 goto nla_put_failure; 858 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY, 859 info.partner_key)) 860 goto nla_put_failure; 861 if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC, 862 sizeof(info.partner_system), 863 &info.partner_system)) 864 goto nla_put_failure; 865 866 nla_nest_end(skb, nest); 867 } 868 } 869 870 return 0; 871 872 nla_put_failure: 873 return -EMSGSIZE; 874 } 875 876 static size_t bond_get_linkxstats_size(const struct net_device *dev, int attr) 877 { 878 switch (attr) { 879 case IFLA_STATS_LINK_XSTATS: 880 case IFLA_STATS_LINK_XSTATS_SLAVE: 881 break; 882 default: 883 return 0; 884 } 885 886 return bond_3ad_stats_size() + nla_total_size(0); 887 } 888 889 static int bond_fill_linkxstats(struct sk_buff *skb, 890 const struct net_device *dev, 891 int *prividx, int attr) 892 { 893 struct nlattr *nla __maybe_unused; 894 struct slave *slave = NULL; 895 struct nlattr *nest, *nest2; 896 struct bonding *bond; 897 898 switch (attr) { 899 case IFLA_STATS_LINK_XSTATS: 900 bond = netdev_priv(dev); 901 break; 902 case IFLA_STATS_LINK_XSTATS_SLAVE: 903 slave = bond_slave_get_rtnl(dev); 904 if (!slave) 905 return 0; 906 bond = slave->bond; 907 break; 908 default: 909 return -EINVAL; 910 } 911 912 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BOND); 913 if (!nest) 914 return -EMSGSIZE; 915 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 916 struct bond_3ad_stats *stats; 917 918 if (slave) 919 stats = &SLAVE_AD_INFO(slave)->stats; 920 else 921 stats = &BOND_AD_INFO(bond).stats; 922 923 nest2 = nla_nest_start_noflag(skb, BOND_XSTATS_3AD); 924 if (!nest2) { 925 nla_nest_end(skb, nest); 926 return -EMSGSIZE; 927 } 928 929 if (bond_3ad_stats_fill(skb, stats)) { 930 nla_nest_cancel(skb, nest2); 931 nla_nest_end(skb, nest); 932 return -EMSGSIZE; 933 } 934 nla_nest_end(skb, nest2); 935 } 936 nla_nest_end(skb, nest); 937 938 return 0; 939 } 940 941 struct rtnl_link_ops bond_link_ops __read_mostly = { 942 .kind = "bond", 943 .priv_size = sizeof(struct bonding), 944 .setup = bond_setup, 945 .maxtype = IFLA_BOND_MAX, 946 .policy = bond_policy, 947 .validate = bond_validate, 948 .newlink = bond_newlink, 949 .changelink = bond_changelink, 950 .get_size = bond_get_size, 951 .fill_info = bond_fill_info, 952 .get_num_tx_queues = bond_get_num_tx_queues, 953 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number 954 as for TX queues */ 955 .fill_linkxstats = bond_fill_linkxstats, 956 .get_linkxstats_size = bond_get_linkxstats_size, 957 .slave_maxtype = IFLA_BOND_SLAVE_MAX, 958 .slave_policy = bond_slave_policy, 959 .slave_changelink = bond_slave_changelink, 960 .get_slave_size = bond_get_slave_size, 961 .fill_slave_info = bond_fill_slave_info, 962 }; 963 964 int __init bond_netlink_init(void) 965 { 966 return rtnl_link_register(&bond_link_ops); 967 } 968 969 void bond_netlink_fini(void) 970 { 971 rtnl_link_unregister(&bond_link_ops); 972 } 973 974 MODULE_ALIAS_RTNL_LINK("bond"); 975