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