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
bond_get_slave_size(const struct net_device * bond_dev,const struct net_device * slave_dev)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
bond_fill_slave_info(struct sk_buff * skb,const struct net_device * bond_dev,const struct net_device * slave_dev)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
bond_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)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
bond_slave_changelink(struct net_device * bond_dev,struct net_device * slave_dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)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
bond_changelink(struct net_device * bond_dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)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 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
263
264 bond_opt_initval(&newval, use_carrier);
265 err = __bond_opt_set(bond, BOND_OPT_USE_CARRIER, &newval,
266 data[IFLA_BOND_USE_CARRIER], extack);
267 if (err)
268 return err;
269 }
270 if (data[IFLA_BOND_ARP_INTERVAL]) {
271 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
272
273 if (arp_interval && miimon) {
274 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_ARP_INTERVAL],
275 "ARP monitoring cannot be used with MII monitoring");
276 return -EINVAL;
277 }
278
279 bond_opt_initval(&newval, arp_interval);
280 err = __bond_opt_set(bond, BOND_OPT_ARP_INTERVAL, &newval,
281 data[IFLA_BOND_ARP_INTERVAL], extack);
282 if (err)
283 return err;
284 }
285 if (data[IFLA_BOND_ARP_IP_TARGET]) {
286 struct nlattr *attr;
287 int i = 0, rem;
288
289 bond_option_arp_ip_targets_clear(bond);
290 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
291 __be32 target;
292
293 if (nla_len(attr) < sizeof(target))
294 return -EINVAL;
295
296 target = nla_get_be32(attr);
297
298 bond_opt_initval(&newval, (__force u64)target);
299 err = __bond_opt_set(bond, BOND_OPT_ARP_TARGETS,
300 &newval,
301 data[IFLA_BOND_ARP_IP_TARGET],
302 extack);
303 if (err)
304 break;
305 i++;
306 }
307 if (i == 0 && bond->params.arp_interval)
308 netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
309 if (err)
310 return err;
311 }
312 #if IS_ENABLED(CONFIG_IPV6)
313 if (data[IFLA_BOND_NS_IP6_TARGET]) {
314 struct nlattr *attr;
315 int i = 0, rem;
316
317 bond_option_ns_ip6_targets_clear(bond);
318 nla_for_each_nested(attr, data[IFLA_BOND_NS_IP6_TARGET], rem) {
319 struct in6_addr addr6;
320
321 if (nla_len(attr) < sizeof(addr6)) {
322 NL_SET_ERR_MSG(extack, "Invalid IPv6 address");
323 return -EINVAL;
324 }
325
326 addr6 = nla_get_in6_addr(attr);
327
328 bond_opt_initextra(&newval, &addr6, sizeof(addr6));
329 err = __bond_opt_set(bond, BOND_OPT_NS_TARGETS,
330 &newval,
331 data[IFLA_BOND_NS_IP6_TARGET],
332 extack);
333 if (err)
334 break;
335 i++;
336 }
337 if (i == 0 && bond->params.arp_interval)
338 netdev_warn(bond->dev, "Removing last ns target with arp_interval on\n");
339 if (err)
340 return err;
341 }
342 #endif
343 if (data[IFLA_BOND_ARP_VALIDATE]) {
344 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
345
346 if (arp_validate && miimon) {
347 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_ARP_INTERVAL],
348 "ARP validating cannot be used with MII monitoring");
349 return -EINVAL;
350 }
351
352 bond_opt_initval(&newval, arp_validate);
353 err = __bond_opt_set(bond, BOND_OPT_ARP_VALIDATE, &newval,
354 data[IFLA_BOND_ARP_VALIDATE], extack);
355 if (err)
356 return err;
357 }
358 if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
359 int arp_all_targets =
360 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
361
362 bond_opt_initval(&newval, arp_all_targets);
363 err = __bond_opt_set(bond, BOND_OPT_ARP_ALL_TARGETS, &newval,
364 data[IFLA_BOND_ARP_ALL_TARGETS], extack);
365 if (err)
366 return err;
367 }
368 if (data[IFLA_BOND_PRIMARY]) {
369 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
370 struct net_device *dev;
371 char *primary = "";
372
373 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
374 if (dev)
375 primary = dev->name;
376
377 bond_opt_initstr(&newval, primary);
378 err = __bond_opt_set(bond, BOND_OPT_PRIMARY, &newval,
379 data[IFLA_BOND_PRIMARY], extack);
380 if (err)
381 return err;
382 }
383 if (data[IFLA_BOND_PRIMARY_RESELECT]) {
384 int primary_reselect =
385 nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
386
387 bond_opt_initval(&newval, primary_reselect);
388 err = __bond_opt_set(bond, BOND_OPT_PRIMARY_RESELECT, &newval,
389 data[IFLA_BOND_PRIMARY_RESELECT], extack);
390 if (err)
391 return err;
392 }
393 if (data[IFLA_BOND_FAIL_OVER_MAC]) {
394 int fail_over_mac =
395 nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
396
397 bond_opt_initval(&newval, fail_over_mac);
398 err = __bond_opt_set(bond, BOND_OPT_FAIL_OVER_MAC, &newval,
399 data[IFLA_BOND_FAIL_OVER_MAC], extack);
400 if (err)
401 return err;
402 }
403 if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
404 int xmit_hash_policy =
405 nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
406
407 bond_opt_initval(&newval, xmit_hash_policy);
408 err = __bond_opt_set(bond, BOND_OPT_XMIT_HASH, &newval,
409 data[IFLA_BOND_XMIT_HASH_POLICY], extack);
410 if (err)
411 return err;
412 }
413 if (data[IFLA_BOND_RESEND_IGMP]) {
414 int resend_igmp =
415 nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
416
417 bond_opt_initval(&newval, resend_igmp);
418 err = __bond_opt_set(bond, BOND_OPT_RESEND_IGMP, &newval,
419 data[IFLA_BOND_RESEND_IGMP], extack);
420 if (err)
421 return err;
422 }
423 if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
424 int num_peer_notif =
425 nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
426
427 bond_opt_initval(&newval, num_peer_notif);
428 err = __bond_opt_set(bond, BOND_OPT_NUM_PEER_NOTIF, &newval,
429 data[IFLA_BOND_NUM_PEER_NOTIF], extack);
430 if (err)
431 return err;
432 }
433 if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
434 int all_slaves_active =
435 nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
436
437 bond_opt_initval(&newval, all_slaves_active);
438 err = __bond_opt_set(bond, BOND_OPT_ALL_SLAVES_ACTIVE, &newval,
439 data[IFLA_BOND_ALL_SLAVES_ACTIVE], extack);
440 if (err)
441 return err;
442 }
443 if (data[IFLA_BOND_MIN_LINKS]) {
444 int min_links =
445 nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
446
447 bond_opt_initval(&newval, min_links);
448 err = __bond_opt_set(bond, BOND_OPT_MINLINKS, &newval,
449 data[IFLA_BOND_MIN_LINKS], extack);
450 if (err)
451 return err;
452 }
453 if (data[IFLA_BOND_LP_INTERVAL]) {
454 int lp_interval =
455 nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
456
457 bond_opt_initval(&newval, lp_interval);
458 err = __bond_opt_set(bond, BOND_OPT_LP_INTERVAL, &newval,
459 data[IFLA_BOND_LP_INTERVAL], extack);
460 if (err)
461 return err;
462 }
463 if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
464 int packets_per_slave =
465 nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
466
467 bond_opt_initval(&newval, packets_per_slave);
468 err = __bond_opt_set(bond, BOND_OPT_PACKETS_PER_SLAVE, &newval,
469 data[IFLA_BOND_PACKETS_PER_SLAVE], extack);
470 if (err)
471 return err;
472 }
473
474 if (data[IFLA_BOND_AD_LACP_ACTIVE]) {
475 int lacp_active = nla_get_u8(data[IFLA_BOND_AD_LACP_ACTIVE]);
476
477 bond_opt_initval(&newval, lacp_active);
478 err = __bond_opt_set(bond, BOND_OPT_LACP_ACTIVE, &newval,
479 data[IFLA_BOND_AD_LACP_ACTIVE], extack);
480 if (err)
481 return err;
482 }
483
484 if (data[IFLA_BOND_AD_LACP_RATE]) {
485 int lacp_rate =
486 nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
487
488 bond_opt_initval(&newval, lacp_rate);
489 err = __bond_opt_set(bond, BOND_OPT_LACP_RATE, &newval,
490 data[IFLA_BOND_AD_LACP_RATE], extack);
491 if (err)
492 return err;
493 }
494 if (data[IFLA_BOND_AD_SELECT]) {
495 int ad_select =
496 nla_get_u8(data[IFLA_BOND_AD_SELECT]);
497
498 bond_opt_initval(&newval, ad_select);
499 err = __bond_opt_set(bond, BOND_OPT_AD_SELECT, &newval,
500 data[IFLA_BOND_AD_SELECT], extack);
501 if (err)
502 return err;
503 }
504 if (data[IFLA_BOND_AD_ACTOR_SYS_PRIO]) {
505 int actor_sys_prio =
506 nla_get_u16(data[IFLA_BOND_AD_ACTOR_SYS_PRIO]);
507
508 bond_opt_initval(&newval, actor_sys_prio);
509 err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYS_PRIO, &newval,
510 data[IFLA_BOND_AD_ACTOR_SYS_PRIO], extack);
511 if (err)
512 return err;
513 }
514 if (data[IFLA_BOND_AD_USER_PORT_KEY]) {
515 int port_key =
516 nla_get_u16(data[IFLA_BOND_AD_USER_PORT_KEY]);
517
518 bond_opt_initval(&newval, port_key);
519 err = __bond_opt_set(bond, BOND_OPT_AD_USER_PORT_KEY, &newval,
520 data[IFLA_BOND_AD_USER_PORT_KEY], extack);
521 if (err)
522 return err;
523 }
524 if (data[IFLA_BOND_AD_ACTOR_SYSTEM]) {
525 if (nla_len(data[IFLA_BOND_AD_ACTOR_SYSTEM]) != ETH_ALEN)
526 return -EINVAL;
527
528 bond_opt_initval(&newval,
529 nla_get_u64(data[IFLA_BOND_AD_ACTOR_SYSTEM]));
530 err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYSTEM, &newval,
531 data[IFLA_BOND_AD_ACTOR_SYSTEM], extack);
532 if (err)
533 return err;
534 }
535 if (data[IFLA_BOND_TLB_DYNAMIC_LB]) {
536 int dynamic_lb = nla_get_u8(data[IFLA_BOND_TLB_DYNAMIC_LB]);
537
538 bond_opt_initval(&newval, dynamic_lb);
539 err = __bond_opt_set(bond, BOND_OPT_TLB_DYNAMIC_LB, &newval,
540 data[IFLA_BOND_TLB_DYNAMIC_LB], extack);
541 if (err)
542 return err;
543 }
544
545 if (data[IFLA_BOND_MISSED_MAX]) {
546 int missed_max = nla_get_u8(data[IFLA_BOND_MISSED_MAX]);
547
548 bond_opt_initval(&newval, missed_max);
549 err = __bond_opt_set(bond, BOND_OPT_MISSED_MAX, &newval,
550 data[IFLA_BOND_MISSED_MAX], extack);
551 if (err)
552 return err;
553 }
554
555 if (data[IFLA_BOND_COUPLED_CONTROL]) {
556 int coupled_control = nla_get_u8(data[IFLA_BOND_COUPLED_CONTROL]);
557
558 bond_opt_initval(&newval, coupled_control);
559 err = __bond_opt_set(bond, BOND_OPT_COUPLED_CONTROL, &newval,
560 data[IFLA_BOND_COUPLED_CONTROL], extack);
561 if (err)
562 return err;
563 }
564
565 if (data[IFLA_BOND_BROADCAST_NEIGH]) {
566 int broadcast_neigh = nla_get_u8(data[IFLA_BOND_BROADCAST_NEIGH]);
567
568 bond_opt_initval(&newval, broadcast_neigh);
569 err = __bond_opt_set(bond, BOND_OPT_BROADCAST_NEIGH, &newval,
570 data[IFLA_BOND_BROADCAST_NEIGH], extack);
571 if (err)
572 return err;
573 }
574
575 return 0;
576 }
577
bond_newlink(struct net_device * bond_dev,struct rtnl_newlink_params * params,struct netlink_ext_ack * extack)578 static int bond_newlink(struct net_device *bond_dev,
579 struct rtnl_newlink_params *params,
580 struct netlink_ext_ack *extack)
581 {
582 struct nlattr **data = params->data;
583 struct nlattr **tb = params->tb;
584 int err;
585
586 err = bond_changelink(bond_dev, tb, data, extack);
587 if (err < 0)
588 return err;
589
590 err = register_netdevice(bond_dev);
591 if (!err) {
592 struct bonding *bond = netdev_priv(bond_dev);
593
594 netif_carrier_off(bond_dev);
595 bond_work_init_all(bond);
596 }
597
598 return err;
599 }
600
bond_get_size(const struct net_device * bond_dev)601 static size_t bond_get_size(const struct net_device *bond_dev)
602 {
603 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
604 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
605 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
606 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
607 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
608 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
609 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
610 /* IFLA_BOND_ARP_IP_TARGET */
611 nla_total_size(sizeof(struct nlattr)) +
612 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
613 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
614 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
615 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
616 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
617 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
618 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
619 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
620 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
621 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
622 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
623 nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
624 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
625 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_ACTIVE */
626 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */
627 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */
628 nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
629 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
630 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
631 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
632 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
633 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
634 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_ACTOR_SYS_PRIO */
635 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_USER_PORT_KEY */
636 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_ACTOR_SYSTEM */
637 nla_total_size(sizeof(u8)) + /* IFLA_BOND_TLB_DYNAMIC_LB */
638 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PEER_NOTIF_DELAY */
639 nla_total_size(sizeof(u8)) + /* IFLA_BOND_MISSED_MAX */
640 /* IFLA_BOND_NS_IP6_TARGET */
641 nla_total_size(sizeof(struct nlattr)) +
642 nla_total_size(sizeof(struct in6_addr)) * BOND_MAX_NS_TARGETS +
643 nla_total_size(sizeof(u8)) + /* IFLA_BOND_COUPLED_CONTROL */
644 nla_total_size(sizeof(u8)) + /* IFLA_BOND_BROADCAST_NEIGH */
645 0;
646 }
647
bond_option_active_slave_get_ifindex(struct bonding * bond)648 static int bond_option_active_slave_get_ifindex(struct bonding *bond)
649 {
650 const struct net_device *slave;
651 int ifindex;
652
653 rcu_read_lock();
654 slave = bond_option_active_slave_get_rcu(bond);
655 ifindex = slave ? slave->ifindex : 0;
656 rcu_read_unlock();
657 return ifindex;
658 }
659
bond_fill_info(struct sk_buff * skb,const struct net_device * bond_dev)660 static int bond_fill_info(struct sk_buff *skb,
661 const struct net_device *bond_dev)
662 {
663 struct bonding *bond = netdev_priv(bond_dev);
664 unsigned int packets_per_slave;
665 int ifindex, i, targets_added;
666 struct nlattr *targets;
667 struct slave *primary;
668
669 if (nla_put_u8(skb, IFLA_BOND_MODE, BOND_MODE(bond)))
670 goto nla_put_failure;
671
672 ifindex = bond_option_active_slave_get_ifindex(bond);
673 if (ifindex && nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, ifindex))
674 goto nla_put_failure;
675
676 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
677 goto nla_put_failure;
678
679 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
680 bond->params.updelay * bond->params.miimon))
681 goto nla_put_failure;
682
683 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
684 bond->params.downdelay * bond->params.miimon))
685 goto nla_put_failure;
686
687 if (nla_put_u32(skb, IFLA_BOND_PEER_NOTIF_DELAY,
688 bond->params.peer_notif_delay * bond->params.miimon))
689 goto nla_put_failure;
690
691 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
692 goto nla_put_failure;
693
694 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
695 goto nla_put_failure;
696
697 targets = nla_nest_start_noflag(skb, IFLA_BOND_ARP_IP_TARGET);
698 if (!targets)
699 goto nla_put_failure;
700
701 targets_added = 0;
702 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
703 if (bond->params.arp_targets[i]) {
704 if (nla_put_be32(skb, i, bond->params.arp_targets[i]))
705 goto nla_put_failure;
706 targets_added = 1;
707 }
708 }
709
710 if (targets_added)
711 nla_nest_end(skb, targets);
712 else
713 nla_nest_cancel(skb, targets);
714
715 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
716 goto nla_put_failure;
717
718 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
719 bond->params.arp_all_targets))
720 goto nla_put_failure;
721
722 #if IS_ENABLED(CONFIG_IPV6)
723 targets = nla_nest_start(skb, IFLA_BOND_NS_IP6_TARGET);
724 if (!targets)
725 goto nla_put_failure;
726
727 targets_added = 0;
728 for (i = 0; i < BOND_MAX_NS_TARGETS; i++) {
729 if (!ipv6_addr_any(&bond->params.ns_targets[i])) {
730 if (nla_put_in6_addr(skb, i, &bond->params.ns_targets[i]))
731 goto nla_put_failure;
732 targets_added = 1;
733 }
734 }
735
736 if (targets_added)
737 nla_nest_end(skb, targets);
738 else
739 nla_nest_cancel(skb, targets);
740 #endif
741
742 primary = rtnl_dereference(bond->primary_slave);
743 if (primary &&
744 nla_put_u32(skb, IFLA_BOND_PRIMARY, primary->dev->ifindex))
745 goto nla_put_failure;
746
747 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
748 bond->params.primary_reselect))
749 goto nla_put_failure;
750
751 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
752 bond->params.fail_over_mac))
753 goto nla_put_failure;
754
755 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
756 bond->params.xmit_policy))
757 goto nla_put_failure;
758
759 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
760 bond->params.resend_igmp))
761 goto nla_put_failure;
762
763 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
764 bond->params.num_peer_notif))
765 goto nla_put_failure;
766
767 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
768 bond->params.all_slaves_active))
769 goto nla_put_failure;
770
771 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
772 bond->params.min_links))
773 goto nla_put_failure;
774
775 if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
776 bond->params.lp_interval))
777 goto nla_put_failure;
778
779 packets_per_slave = bond->params.packets_per_slave;
780 if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
781 packets_per_slave))
782 goto nla_put_failure;
783
784 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_ACTIVE,
785 bond->params.lacp_active))
786 goto nla_put_failure;
787
788 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
789 bond->params.lacp_fast))
790 goto nla_put_failure;
791
792 if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
793 bond->params.ad_select))
794 goto nla_put_failure;
795
796 if (nla_put_u8(skb, IFLA_BOND_TLB_DYNAMIC_LB,
797 bond->params.tlb_dynamic_lb))
798 goto nla_put_failure;
799
800 if (nla_put_u8(skb, IFLA_BOND_MISSED_MAX,
801 bond->params.missed_max))
802 goto nla_put_failure;
803
804 if (nla_put_u8(skb, IFLA_BOND_COUPLED_CONTROL,
805 bond->params.coupled_control))
806 goto nla_put_failure;
807
808 if (nla_put_u8(skb, IFLA_BOND_BROADCAST_NEIGH,
809 bond->params.broadcast_neighbor))
810 goto nla_put_failure;
811
812 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
813 struct ad_info info;
814
815 if (capable(CAP_NET_ADMIN)) {
816 if (nla_put_u16(skb, IFLA_BOND_AD_ACTOR_SYS_PRIO,
817 bond->params.ad_actor_sys_prio))
818 goto nla_put_failure;
819
820 if (nla_put_u16(skb, IFLA_BOND_AD_USER_PORT_KEY,
821 bond->params.ad_user_port_key))
822 goto nla_put_failure;
823
824 if (nla_put(skb, IFLA_BOND_AD_ACTOR_SYSTEM,
825 ETH_ALEN, &bond->params.ad_actor_system))
826 goto nla_put_failure;
827 }
828 if (!bond_3ad_get_active_agg_info(bond, &info)) {
829 struct nlattr *nest;
830
831 nest = nla_nest_start_noflag(skb, IFLA_BOND_AD_INFO);
832 if (!nest)
833 goto nla_put_failure;
834
835 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
836 info.aggregator_id))
837 goto nla_put_failure;
838 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
839 info.ports))
840 goto nla_put_failure;
841 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
842 info.actor_key))
843 goto nla_put_failure;
844 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
845 info.partner_key))
846 goto nla_put_failure;
847 if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
848 sizeof(info.partner_system),
849 &info.partner_system))
850 goto nla_put_failure;
851
852 nla_nest_end(skb, nest);
853 }
854 }
855
856 return 0;
857
858 nla_put_failure:
859 return -EMSGSIZE;
860 }
861
bond_get_linkxstats_size(const struct net_device * dev,int attr)862 static size_t bond_get_linkxstats_size(const struct net_device *dev, int attr)
863 {
864 switch (attr) {
865 case IFLA_STATS_LINK_XSTATS:
866 case IFLA_STATS_LINK_XSTATS_SLAVE:
867 break;
868 default:
869 return 0;
870 }
871
872 return bond_3ad_stats_size() + nla_total_size(0);
873 }
874
bond_fill_linkxstats(struct sk_buff * skb,const struct net_device * dev,int * prividx,int attr)875 static int bond_fill_linkxstats(struct sk_buff *skb,
876 const struct net_device *dev,
877 int *prividx, int attr)
878 {
879 struct nlattr *nla __maybe_unused;
880 struct slave *slave = NULL;
881 struct nlattr *nest, *nest2;
882 struct bonding *bond;
883
884 switch (attr) {
885 case IFLA_STATS_LINK_XSTATS:
886 bond = netdev_priv(dev);
887 break;
888 case IFLA_STATS_LINK_XSTATS_SLAVE:
889 slave = bond_slave_get_rtnl(dev);
890 if (!slave)
891 return 0;
892 bond = slave->bond;
893 break;
894 default:
895 return -EINVAL;
896 }
897
898 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BOND);
899 if (!nest)
900 return -EMSGSIZE;
901 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
902 struct bond_3ad_stats *stats;
903
904 if (slave)
905 stats = &SLAVE_AD_INFO(slave)->stats;
906 else
907 stats = &BOND_AD_INFO(bond).stats;
908
909 nest2 = nla_nest_start_noflag(skb, BOND_XSTATS_3AD);
910 if (!nest2) {
911 nla_nest_end(skb, nest);
912 return -EMSGSIZE;
913 }
914
915 if (bond_3ad_stats_fill(skb, stats)) {
916 nla_nest_cancel(skb, nest2);
917 nla_nest_end(skb, nest);
918 return -EMSGSIZE;
919 }
920 nla_nest_end(skb, nest2);
921 }
922 nla_nest_end(skb, nest);
923
924 return 0;
925 }
926
927 struct rtnl_link_ops bond_link_ops __read_mostly = {
928 .kind = "bond",
929 .priv_size = sizeof(struct bonding),
930 .setup = bond_setup,
931 .maxtype = IFLA_BOND_MAX,
932 .policy = bond_policy,
933 .validate = bond_validate,
934 .newlink = bond_newlink,
935 .changelink = bond_changelink,
936 .get_size = bond_get_size,
937 .fill_info = bond_fill_info,
938 .get_num_tx_queues = bond_get_num_tx_queues,
939 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
940 as for TX queues */
941 .fill_linkxstats = bond_fill_linkxstats,
942 .get_linkxstats_size = bond_get_linkxstats_size,
943 .slave_maxtype = IFLA_BOND_SLAVE_MAX,
944 .slave_policy = bond_slave_policy,
945 .slave_changelink = bond_slave_changelink,
946 .get_slave_size = bond_get_slave_size,
947 .fill_slave_info = bond_fill_slave_info,
948 };
949
bond_netlink_init(void)950 int __init bond_netlink_init(void)
951 {
952 return rtnl_link_register(&bond_link_ops);
953 }
954
bond_netlink_fini(void)955 void bond_netlink_fini(void)
956 {
957 rtnl_link_unregister(&bond_link_ops);
958 }
959
960 MODULE_ALIAS_RTNL_LINK("bond");
961