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