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