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