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 bonding *bond = netdev_priv(bond_dev); 597 struct nlattr **data = params->data; 598 struct nlattr **tb = params->tb; 599 int err; 600 601 err = register_netdevice(bond_dev); 602 if (err) 603 return err; 604 605 netif_carrier_off(bond_dev); 606 bond_work_init_all(bond); 607 608 err = bond_changelink(bond_dev, tb, data, extack); 609 if (err) { 610 bond_work_cancel_all(bond); 611 unregister_netdevice(bond_dev); 612 } 613 614 return err; 615 } 616 617 static size_t bond_get_size(const struct net_device *bond_dev) 618 { 619 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */ 620 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */ 621 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */ 622 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */ 623 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */ 624 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */ 625 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */ 626 /* IFLA_BOND_ARP_IP_TARGET */ 627 nla_total_size(sizeof(struct nlattr)) + 628 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS + 629 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */ 630 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */ 631 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */ 632 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */ 633 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */ 634 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */ 635 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */ 636 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */ 637 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */ 638 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */ 639 nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */ 640 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */ 641 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_ACTIVE */ 642 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */ 643 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */ 644 nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */ 645 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */ 646 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */ 647 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */ 648 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/ 649 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/ 650 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_ACTOR_SYS_PRIO */ 651 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_USER_PORT_KEY */ 652 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_ACTOR_SYSTEM */ 653 nla_total_size(sizeof(u8)) + /* IFLA_BOND_TLB_DYNAMIC_LB */ 654 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PEER_NOTIF_DELAY */ 655 nla_total_size(sizeof(u8)) + /* IFLA_BOND_MISSED_MAX */ 656 /* IFLA_BOND_NS_IP6_TARGET */ 657 nla_total_size(sizeof(struct nlattr)) + 658 nla_total_size(sizeof(struct in6_addr)) * BOND_MAX_NS_TARGETS + 659 nla_total_size(sizeof(u8)) + /* IFLA_BOND_COUPLED_CONTROL */ 660 nla_total_size(sizeof(u8)) + /* IFLA_BOND_BROADCAST_NEIGH */ 661 0; 662 } 663 664 static int bond_option_active_slave_get_ifindex(struct bonding *bond) 665 { 666 const struct net_device *slave; 667 int ifindex; 668 669 rcu_read_lock(); 670 slave = bond_option_active_slave_get_rcu(bond); 671 ifindex = slave ? slave->ifindex : 0; 672 rcu_read_unlock(); 673 return ifindex; 674 } 675 676 static int bond_fill_info(struct sk_buff *skb, 677 const struct net_device *bond_dev) 678 { 679 struct bonding *bond = netdev_priv(bond_dev); 680 unsigned int packets_per_slave; 681 int ifindex, i, targets_added; 682 struct nlattr *targets; 683 struct slave *primary; 684 685 if (nla_put_u8(skb, IFLA_BOND_MODE, BOND_MODE(bond))) 686 goto nla_put_failure; 687 688 ifindex = bond_option_active_slave_get_ifindex(bond); 689 if (ifindex && nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, ifindex)) 690 goto nla_put_failure; 691 692 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon)) 693 goto nla_put_failure; 694 695 if (nla_put_u32(skb, IFLA_BOND_UPDELAY, 696 bond->params.updelay * bond->params.miimon)) 697 goto nla_put_failure; 698 699 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY, 700 bond->params.downdelay * bond->params.miimon)) 701 goto nla_put_failure; 702 703 if (nla_put_u32(skb, IFLA_BOND_PEER_NOTIF_DELAY, 704 bond->params.peer_notif_delay * bond->params.miimon)) 705 goto nla_put_failure; 706 707 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, 1)) 708 goto nla_put_failure; 709 710 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval)) 711 goto nla_put_failure; 712 713 targets = nla_nest_start_noflag(skb, IFLA_BOND_ARP_IP_TARGET); 714 if (!targets) 715 goto nla_put_failure; 716 717 targets_added = 0; 718 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) { 719 if (bond->params.arp_targets[i]) { 720 if (nla_put_be32(skb, i, bond->params.arp_targets[i])) 721 goto nla_put_failure; 722 targets_added = 1; 723 } 724 } 725 726 if (targets_added) 727 nla_nest_end(skb, targets); 728 else 729 nla_nest_cancel(skb, targets); 730 731 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate)) 732 goto nla_put_failure; 733 734 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS, 735 bond->params.arp_all_targets)) 736 goto nla_put_failure; 737 738 #if IS_ENABLED(CONFIG_IPV6) 739 targets = nla_nest_start(skb, IFLA_BOND_NS_IP6_TARGET); 740 if (!targets) 741 goto nla_put_failure; 742 743 targets_added = 0; 744 for (i = 0; i < BOND_MAX_NS_TARGETS; i++) { 745 if (!ipv6_addr_any(&bond->params.ns_targets[i])) { 746 if (nla_put_in6_addr(skb, i, &bond->params.ns_targets[i])) 747 goto nla_put_failure; 748 targets_added = 1; 749 } 750 } 751 752 if (targets_added) 753 nla_nest_end(skb, targets); 754 else 755 nla_nest_cancel(skb, targets); 756 #endif 757 758 primary = rtnl_dereference(bond->primary_slave); 759 if (primary && 760 nla_put_u32(skb, IFLA_BOND_PRIMARY, primary->dev->ifindex)) 761 goto nla_put_failure; 762 763 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT, 764 bond->params.primary_reselect)) 765 goto nla_put_failure; 766 767 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC, 768 bond->params.fail_over_mac)) 769 goto nla_put_failure; 770 771 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY, 772 bond->params.xmit_policy)) 773 goto nla_put_failure; 774 775 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP, 776 bond->params.resend_igmp)) 777 goto nla_put_failure; 778 779 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF, 780 bond->params.num_peer_notif)) 781 goto nla_put_failure; 782 783 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE, 784 bond->params.all_slaves_active)) 785 goto nla_put_failure; 786 787 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS, 788 bond->params.min_links)) 789 goto nla_put_failure; 790 791 if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL, 792 bond->params.lp_interval)) 793 goto nla_put_failure; 794 795 packets_per_slave = bond->params.packets_per_slave; 796 if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE, 797 packets_per_slave)) 798 goto nla_put_failure; 799 800 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_ACTIVE, 801 bond->params.lacp_active)) 802 goto nla_put_failure; 803 804 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE, 805 bond->params.lacp_fast)) 806 goto nla_put_failure; 807 808 if (nla_put_u8(skb, IFLA_BOND_AD_SELECT, 809 bond->params.ad_select)) 810 goto nla_put_failure; 811 812 if (nla_put_u8(skb, IFLA_BOND_TLB_DYNAMIC_LB, 813 bond->params.tlb_dynamic_lb)) 814 goto nla_put_failure; 815 816 if (nla_put_u8(skb, IFLA_BOND_MISSED_MAX, 817 bond->params.missed_max)) 818 goto nla_put_failure; 819 820 if (nla_put_u8(skb, IFLA_BOND_COUPLED_CONTROL, 821 bond->params.coupled_control)) 822 goto nla_put_failure; 823 824 if (nla_put_u8(skb, IFLA_BOND_BROADCAST_NEIGH, 825 bond->params.broadcast_neighbor)) 826 goto nla_put_failure; 827 828 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 829 struct ad_info info; 830 831 if (capable(CAP_NET_ADMIN)) { 832 if (nla_put_u16(skb, IFLA_BOND_AD_ACTOR_SYS_PRIO, 833 bond->params.ad_actor_sys_prio)) 834 goto nla_put_failure; 835 836 if (nla_put_u16(skb, IFLA_BOND_AD_USER_PORT_KEY, 837 bond->params.ad_user_port_key)) 838 goto nla_put_failure; 839 840 if (nla_put(skb, IFLA_BOND_AD_ACTOR_SYSTEM, 841 ETH_ALEN, &bond->params.ad_actor_system)) 842 goto nla_put_failure; 843 } 844 if (!bond_3ad_get_active_agg_info(bond, &info)) { 845 struct nlattr *nest; 846 847 nest = nla_nest_start_noflag(skb, IFLA_BOND_AD_INFO); 848 if (!nest) 849 goto nla_put_failure; 850 851 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR, 852 info.aggregator_id)) 853 goto nla_put_failure; 854 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS, 855 info.ports)) 856 goto nla_put_failure; 857 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY, 858 info.actor_key)) 859 goto nla_put_failure; 860 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY, 861 info.partner_key)) 862 goto nla_put_failure; 863 if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC, 864 sizeof(info.partner_system), 865 &info.partner_system)) 866 goto nla_put_failure; 867 868 nla_nest_end(skb, nest); 869 } 870 } 871 872 return 0; 873 874 nla_put_failure: 875 return -EMSGSIZE; 876 } 877 878 static size_t bond_get_linkxstats_size(const struct net_device *dev, int attr) 879 { 880 switch (attr) { 881 case IFLA_STATS_LINK_XSTATS: 882 case IFLA_STATS_LINK_XSTATS_SLAVE: 883 break; 884 default: 885 return 0; 886 } 887 888 return bond_3ad_stats_size() + nla_total_size(0); 889 } 890 891 static int bond_fill_linkxstats(struct sk_buff *skb, 892 const struct net_device *dev, 893 int *prividx, int attr) 894 { 895 struct nlattr *nla __maybe_unused; 896 struct slave *slave = NULL; 897 struct nlattr *nest, *nest2; 898 struct bonding *bond; 899 900 switch (attr) { 901 case IFLA_STATS_LINK_XSTATS: 902 bond = netdev_priv(dev); 903 break; 904 case IFLA_STATS_LINK_XSTATS_SLAVE: 905 slave = bond_slave_get_rtnl(dev); 906 if (!slave) 907 return 0; 908 bond = slave->bond; 909 break; 910 default: 911 return -EINVAL; 912 } 913 914 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BOND); 915 if (!nest) 916 return -EMSGSIZE; 917 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 918 struct bond_3ad_stats *stats; 919 920 if (slave) 921 stats = &SLAVE_AD_INFO(slave)->stats; 922 else 923 stats = &BOND_AD_INFO(bond).stats; 924 925 nest2 = nla_nest_start_noflag(skb, BOND_XSTATS_3AD); 926 if (!nest2) { 927 nla_nest_end(skb, nest); 928 return -EMSGSIZE; 929 } 930 931 if (bond_3ad_stats_fill(skb, stats)) { 932 nla_nest_cancel(skb, nest2); 933 nla_nest_end(skb, nest); 934 return -EMSGSIZE; 935 } 936 nla_nest_end(skb, nest2); 937 } 938 nla_nest_end(skb, nest); 939 940 return 0; 941 } 942 943 struct rtnl_link_ops bond_link_ops __read_mostly = { 944 .kind = "bond", 945 .priv_size = sizeof(struct bonding), 946 .setup = bond_setup, 947 .maxtype = IFLA_BOND_MAX, 948 .policy = bond_policy, 949 .validate = bond_validate, 950 .newlink = bond_newlink, 951 .changelink = bond_changelink, 952 .get_size = bond_get_size, 953 .fill_info = bond_fill_info, 954 .get_num_tx_queues = bond_get_num_tx_queues, 955 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number 956 as for TX queues */ 957 .fill_linkxstats = bond_fill_linkxstats, 958 .get_linkxstats_size = bond_get_linkxstats_size, 959 .slave_maxtype = IFLA_BOND_SLAVE_MAX, 960 .slave_policy = bond_slave_policy, 961 .slave_changelink = bond_slave_changelink, 962 .get_slave_size = bond_get_slave_size, 963 .fill_slave_info = bond_fill_slave_info, 964 }; 965 966 int __init bond_netlink_init(void) 967 { 968 return rtnl_link_register(&bond_link_ops); 969 } 970 971 void bond_netlink_fini(void) 972 { 973 rtnl_link_unregister(&bond_link_ops); 974 } 975 976 MODULE_ALIAS_RTNL_LINK("bond"); 977