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