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