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