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