xref: /linux/drivers/net/bonding/bond_options.c (revision 74d4432421a3e2669fbccc08c0f4fc2980bf0e39)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/net/bond/bond_options.c - bonding options
4  * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
5  * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
6  */
7 
8 #include <linux/errno.h>
9 #include <linux/if.h>
10 #include <linux/netdevice.h>
11 #include <linux/spinlock.h>
12 #include <linux/rcupdate.h>
13 #include <linux/ctype.h>
14 #include <linux/inet.h>
15 #include <linux/sched/signal.h>
16 
17 #include <net/bonding.h>
18 #include <net/ndisc.h>
19 
20 static int bond_option_active_slave_set(struct bonding *bond,
21 					const struct bond_opt_value *newval);
22 static int bond_option_miimon_set(struct bonding *bond,
23 				  const struct bond_opt_value *newval);
24 static int bond_option_updelay_set(struct bonding *bond,
25 				   const struct bond_opt_value *newval);
26 static int bond_option_downdelay_set(struct bonding *bond,
27 				     const struct bond_opt_value *newval);
28 static int bond_option_peer_notif_delay_set(struct bonding *bond,
29 					    const struct bond_opt_value *newval);
30 static int bond_option_use_carrier_set(struct bonding *bond,
31 				       const struct bond_opt_value *newval);
32 static int bond_option_arp_interval_set(struct bonding *bond,
33 					const struct bond_opt_value *newval);
34 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
35 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
36 static int bond_option_arp_ip_targets_set(struct bonding *bond,
37 					  const struct bond_opt_value *newval);
38 static int bond_option_ns_ip6_targets_set(struct bonding *bond,
39 					  const struct bond_opt_value *newval);
40 static int bond_option_arp_validate_set(struct bonding *bond,
41 					const struct bond_opt_value *newval);
42 static int bond_option_arp_all_targets_set(struct bonding *bond,
43 					   const struct bond_opt_value *newval);
44 static int bond_option_prio_set(struct bonding *bond,
45 				const struct bond_opt_value *newval);
46 static int bond_option_primary_set(struct bonding *bond,
47 				   const struct bond_opt_value *newval);
48 static int bond_option_primary_reselect_set(struct bonding *bond,
49 					    const struct bond_opt_value *newval);
50 static int bond_option_fail_over_mac_set(struct bonding *bond,
51 					 const struct bond_opt_value *newval);
52 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
53 					    const struct bond_opt_value *newval);
54 static int bond_option_resend_igmp_set(struct bonding *bond,
55 				       const struct bond_opt_value *newval);
56 static int bond_option_num_peer_notif_set(struct bonding *bond,
57 					  const struct bond_opt_value *newval);
58 static int bond_option_all_slaves_active_set(struct bonding *bond,
59 					     const struct bond_opt_value *newval);
60 static int bond_option_min_links_set(struct bonding *bond,
61 				     const struct bond_opt_value *newval);
62 static int bond_option_lp_interval_set(struct bonding *bond,
63 				       const struct bond_opt_value *newval);
64 static int bond_option_pps_set(struct bonding *bond,
65 			       const struct bond_opt_value *newval);
66 static int bond_option_lacp_active_set(struct bonding *bond,
67 				       const struct bond_opt_value *newval);
68 static int bond_option_lacp_rate_set(struct bonding *bond,
69 				     const struct bond_opt_value *newval);
70 static int bond_option_ad_select_set(struct bonding *bond,
71 				     const struct bond_opt_value *newval);
72 static int bond_option_queue_id_set(struct bonding *bond,
73 				    const struct bond_opt_value *newval);
74 static int bond_option_mode_set(struct bonding *bond,
75 				const struct bond_opt_value *newval);
76 static int bond_option_slaves_set(struct bonding *bond,
77 				  const struct bond_opt_value *newval);
78 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
79 				  const struct bond_opt_value *newval);
80 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
81 					     const struct bond_opt_value *newval);
82 static int bond_option_actor_port_prio_set(struct bonding *bond,
83 					   const struct bond_opt_value *newval);
84 static int bond_option_ad_actor_system_set(struct bonding *bond,
85 					   const struct bond_opt_value *newval);
86 static int bond_option_ad_user_port_key_set(struct bonding *bond,
87 					    const struct bond_opt_value *newval);
88 static int bond_option_missed_max_set(struct bonding *bond,
89 				      const struct bond_opt_value *newval);
90 static int bond_option_coupled_control_set(struct bonding *bond,
91 					   const struct bond_opt_value *newval);
92 static int bond_option_broadcast_neigh_set(struct bonding *bond,
93 					   const struct bond_opt_value *newval);
94 
95 static const struct bond_opt_value bond_mode_tbl[] = {
96 	{ "balance-rr",    BOND_MODE_ROUNDROBIN,   BOND_VALFLAG_DEFAULT},
97 	{ "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
98 	{ "balance-xor",   BOND_MODE_XOR,          0},
99 	{ "broadcast",     BOND_MODE_BROADCAST,    0},
100 	{ "802.3ad",       BOND_MODE_8023AD,       0},
101 	{ "balance-tlb",   BOND_MODE_TLB,          0},
102 	{ "balance-alb",   BOND_MODE_ALB,          0},
103 	{ NULL,            -1,                     0},
104 };
105 
106 static const struct bond_opt_value bond_pps_tbl[] = {
107 	{ "default", 1,         BOND_VALFLAG_DEFAULT},
108 	{ "maxval",  USHRT_MAX, BOND_VALFLAG_MAX},
109 	{ NULL,      -1,        0},
110 };
111 
112 static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
113 	{ "layer2",      BOND_XMIT_POLICY_LAYER2,      BOND_VALFLAG_DEFAULT},
114 	{ "layer3+4",    BOND_XMIT_POLICY_LAYER34,     0},
115 	{ "layer2+3",    BOND_XMIT_POLICY_LAYER23,     0},
116 	{ "encap2+3",    BOND_XMIT_POLICY_ENCAP23,     0},
117 	{ "encap3+4",    BOND_XMIT_POLICY_ENCAP34,     0},
118 	{ "vlan+srcmac", BOND_XMIT_POLICY_VLAN_SRCMAC, 0},
119 	{ NULL,          -1,                           0},
120 };
121 
122 static const struct bond_opt_value bond_arp_validate_tbl[] = {
123 	{ "none",		BOND_ARP_VALIDATE_NONE,		BOND_VALFLAG_DEFAULT},
124 	{ "active",		BOND_ARP_VALIDATE_ACTIVE,	0},
125 	{ "backup",		BOND_ARP_VALIDATE_BACKUP,	0},
126 	{ "all",		BOND_ARP_VALIDATE_ALL,		0},
127 	{ "filter",		BOND_ARP_FILTER,		0},
128 	{ "filter_active",	BOND_ARP_FILTER_ACTIVE,		0},
129 	{ "filter_backup",	BOND_ARP_FILTER_BACKUP,		0},
130 	{ NULL,			-1,				0},
131 };
132 
133 static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
134 	{ "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
135 	{ "all", BOND_ARP_TARGETS_ALL, 0},
136 	{ NULL,  -1,                   0},
137 };
138 
139 static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
140 	{ "none",   BOND_FOM_NONE,   BOND_VALFLAG_DEFAULT},
141 	{ "active", BOND_FOM_ACTIVE, 0},
142 	{ "follow", BOND_FOM_FOLLOW, 0},
143 	{ NULL,     -1,              0},
144 };
145 
146 static const struct bond_opt_value bond_intmax_tbl[] = {
147 	{ "off",     0,       BOND_VALFLAG_DEFAULT},
148 	{ "maxval",  INT_MAX, BOND_VALFLAG_MAX},
149 	{ NULL,      -1,      0}
150 };
151 
152 static const struct bond_opt_value bond_lacp_active[] = {
153 	{ "off", 0,  0},
154 	{ "on",  1,  BOND_VALFLAG_DEFAULT},
155 	{ NULL,  -1, 0}
156 };
157 
158 static const struct bond_opt_value bond_lacp_rate_tbl[] = {
159 	{ "slow", AD_LACP_SLOW, 0},
160 	{ "fast", AD_LACP_FAST, 0},
161 	{ NULL,   -1,           0},
162 };
163 
164 static const struct bond_opt_value bond_ad_select_tbl[] = {
165 	{ "stable",          BOND_AD_STABLE,    BOND_VALFLAG_DEFAULT},
166 	{ "bandwidth",       BOND_AD_BANDWIDTH, 0},
167 	{ "count",           BOND_AD_COUNT,     0},
168 	{ "actor_port_prio", BOND_AD_PRIO,      0},
169 	{ NULL,              -1,                0},
170 };
171 
172 static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
173 	{ "off",     0,   0},
174 	{ "maxval",  255, BOND_VALFLAG_MAX},
175 	{ "default", 1,   BOND_VALFLAG_DEFAULT},
176 	{ NULL,      -1,  0}
177 };
178 
179 static const struct bond_opt_value bond_peer_notif_delay_tbl[] = {
180 	{ "off",     0,   0},
181 	{ "maxval",  300000, BOND_VALFLAG_MAX},
182 	{ NULL,      -1,  0}
183 };
184 
185 static const struct bond_opt_value bond_primary_reselect_tbl[] = {
186 	{ "always",  BOND_PRI_RESELECT_ALWAYS,  BOND_VALFLAG_DEFAULT},
187 	{ "better",  BOND_PRI_RESELECT_BETTER,  0},
188 	{ "failure", BOND_PRI_RESELECT_FAILURE, 0},
189 	{ NULL,      -1},
190 };
191 
192 static const struct bond_opt_value bond_use_carrier_tbl[] = {
193 	{ "on",  1,  BOND_VALFLAG_DEFAULT},
194 	{ NULL,  -1, 0}
195 };
196 
197 static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
198 	{ "off", 0,  BOND_VALFLAG_DEFAULT},
199 	{ "on",  1,  0},
200 	{ NULL,  -1, 0}
201 };
202 
203 static const struct bond_opt_value bond_resend_igmp_tbl[] = {
204 	{ "off",     0,   0},
205 	{ "maxval",  255, BOND_VALFLAG_MAX},
206 	{ "default", 1,   BOND_VALFLAG_DEFAULT},
207 	{ NULL,      -1,  0}
208 };
209 
210 static const struct bond_opt_value bond_lp_interval_tbl[] = {
211 	{ "minval",  1,       BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
212 	{ "maxval",  INT_MAX, BOND_VALFLAG_MAX},
213 	{ NULL,      -1,      0},
214 };
215 
216 static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
217 	{ "off", 0,  0},
218 	{ "on",  1,  BOND_VALFLAG_DEFAULT},
219 	{ NULL,  -1, 0}
220 };
221 
222 static const struct bond_opt_value bond_ad_actor_sys_prio_tbl[] = {
223 	{ "minval",  1,     BOND_VALFLAG_MIN},
224 	{ "maxval",  65535, BOND_VALFLAG_MAX | BOND_VALFLAG_DEFAULT},
225 	{ NULL,      -1,    0},
226 };
227 
228 static const struct bond_opt_value bond_ad_user_port_key_tbl[] = {
229 	{ "minval",  0,     BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
230 	{ "maxval",  1023,  BOND_VALFLAG_MAX},
231 	{ NULL,      -1,    0},
232 };
233 
234 static const struct bond_opt_value bond_missed_max_tbl[] = {
235 	{ "minval",	1,	BOND_VALFLAG_MIN},
236 	{ "maxval",	255,	BOND_VALFLAG_MAX},
237 	{ "default",	2,	BOND_VALFLAG_DEFAULT},
238 	{ NULL,		-1,	0},
239 };
240 
241 static const struct bond_opt_value bond_coupled_control_tbl[] = {
242 	{ "on",  1,  BOND_VALFLAG_DEFAULT},
243 	{ "off", 0,  0},
244 	{ NULL,  -1, 0},
245 };
246 
247 static const struct bond_opt_value bond_broadcast_neigh_tbl[] = {
248 	{ "off", 0, BOND_VALFLAG_DEFAULT},
249 	{ "on",	 1, 0},
250 	{ NULL,  -1, 0}
251 };
252 
253 static const struct bond_option bond_opts[BOND_OPT_LAST] = {
254 	[BOND_OPT_MODE] = {
255 		.id = BOND_OPT_MODE,
256 		.name = "mode",
257 		.desc = "bond device mode",
258 		.flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
259 		.values = bond_mode_tbl,
260 		.set = bond_option_mode_set
261 	},
262 	[BOND_OPT_PACKETS_PER_SLAVE] = {
263 		.id = BOND_OPT_PACKETS_PER_SLAVE,
264 		.name = "packets_per_slave",
265 		.desc = "Packets to send per slave in RR mode",
266 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
267 		.values = bond_pps_tbl,
268 		.set = bond_option_pps_set
269 	},
270 	[BOND_OPT_XMIT_HASH] = {
271 		.id = BOND_OPT_XMIT_HASH,
272 		.name = "xmit_hash_policy",
273 		.desc = "balance-xor, 802.3ad, and tlb hashing method",
274 		.values = bond_xmit_hashtype_tbl,
275 		.set = bond_option_xmit_hash_policy_set
276 	},
277 	[BOND_OPT_ARP_VALIDATE] = {
278 		.id = BOND_OPT_ARP_VALIDATE,
279 		.name = "arp_validate",
280 		.desc = "validate src/dst of ARP probes",
281 		.unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
282 			       BIT(BOND_MODE_ALB),
283 		.values = bond_arp_validate_tbl,
284 		.set = bond_option_arp_validate_set
285 	},
286 	[BOND_OPT_ARP_ALL_TARGETS] = {
287 		.id = BOND_OPT_ARP_ALL_TARGETS,
288 		.name = "arp_all_targets",
289 		.desc = "fail on any/all arp targets timeout",
290 		.values = bond_arp_all_targets_tbl,
291 		.set = bond_option_arp_all_targets_set
292 	},
293 	[BOND_OPT_FAIL_OVER_MAC] = {
294 		.id = BOND_OPT_FAIL_OVER_MAC,
295 		.name = "fail_over_mac",
296 		.desc = "For active-backup, do not set all slaves to the same MAC",
297 		.flags = BOND_OPTFLAG_NOSLAVES,
298 		.values = bond_fail_over_mac_tbl,
299 		.set = bond_option_fail_over_mac_set
300 	},
301 	[BOND_OPT_ARP_INTERVAL] = {
302 		.id = BOND_OPT_ARP_INTERVAL,
303 		.name = "arp_interval",
304 		.desc = "arp interval in milliseconds",
305 		.unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
306 			       BIT(BOND_MODE_ALB),
307 		.values = bond_intmax_tbl,
308 		.set = bond_option_arp_interval_set
309 	},
310 	[BOND_OPT_MISSED_MAX] = {
311 		.id = BOND_OPT_MISSED_MAX,
312 		.name = "arp_missed_max",
313 		.desc = "Maximum number of missed ARP interval",
314 		.unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
315 			       BIT(BOND_MODE_ALB),
316 		.values = bond_missed_max_tbl,
317 		.set = bond_option_missed_max_set
318 	},
319 	[BOND_OPT_ARP_TARGETS] = {
320 		.id = BOND_OPT_ARP_TARGETS,
321 		.name = "arp_ip_target",
322 		.desc = "arp targets in n.n.n.n form",
323 		.flags = BOND_OPTFLAG_RAWVAL,
324 		.set = bond_option_arp_ip_targets_set
325 	},
326 	[BOND_OPT_NS_TARGETS] = {
327 		.id = BOND_OPT_NS_TARGETS,
328 		.name = "ns_ip6_target",
329 		.desc = "NS targets in ffff:ffff::ffff:ffff form",
330 		.flags = BOND_OPTFLAG_RAWVAL,
331 		.set = bond_option_ns_ip6_targets_set
332 	},
333 	[BOND_OPT_DOWNDELAY] = {
334 		.id = BOND_OPT_DOWNDELAY,
335 		.name = "downdelay",
336 		.desc = "Delay before considering link down, in milliseconds",
337 		.values = bond_intmax_tbl,
338 		.set = bond_option_downdelay_set
339 	},
340 	[BOND_OPT_UPDELAY] = {
341 		.id = BOND_OPT_UPDELAY,
342 		.name = "updelay",
343 		.desc = "Delay before considering link up, in milliseconds",
344 		.values = bond_intmax_tbl,
345 		.set = bond_option_updelay_set
346 	},
347 	[BOND_OPT_LACP_ACTIVE] = {
348 		.id = BOND_OPT_LACP_ACTIVE,
349 		.name = "lacp_active",
350 		.desc = "Send LACPDU frames with configured lacp rate or acts as speak when spoken to",
351 		.flags = BOND_OPTFLAG_IFDOWN,
352 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
353 		.values = bond_lacp_active,
354 		.set = bond_option_lacp_active_set
355 	},
356 	[BOND_OPT_LACP_RATE] = {
357 		.id = BOND_OPT_LACP_RATE,
358 		.name = "lacp_rate",
359 		.desc = "LACPDU tx rate to request from 802.3ad partner",
360 		.flags = BOND_OPTFLAG_IFDOWN,
361 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
362 		.values = bond_lacp_rate_tbl,
363 		.set = bond_option_lacp_rate_set
364 	},
365 	[BOND_OPT_MINLINKS] = {
366 		.id = BOND_OPT_MINLINKS,
367 		.name = "min_links",
368 		.desc = "Minimum number of available links before turning on carrier",
369 		.values = bond_intmax_tbl,
370 		.set = bond_option_min_links_set
371 	},
372 	[BOND_OPT_AD_SELECT] = {
373 		.id = BOND_OPT_AD_SELECT,
374 		.name = "ad_select",
375 		.desc = "802.3ad aggregation selection logic",
376 		.flags = BOND_OPTFLAG_IFDOWN,
377 		.values = bond_ad_select_tbl,
378 		.set = bond_option_ad_select_set
379 	},
380 	[BOND_OPT_NUM_PEER_NOTIF] = {
381 		.id = BOND_OPT_NUM_PEER_NOTIF,
382 		.name = "num_unsol_na",
383 		.desc = "Number of peer notifications to send on failover event",
384 		.values = bond_num_peer_notif_tbl,
385 		.set = bond_option_num_peer_notif_set
386 	},
387 	[BOND_OPT_MIIMON] = {
388 		.id = BOND_OPT_MIIMON,
389 		.name = "miimon",
390 		.desc = "Link check interval in milliseconds",
391 		.values = bond_intmax_tbl,
392 		.set = bond_option_miimon_set
393 	},
394 	[BOND_OPT_PRIO] = {
395 		.id = BOND_OPT_PRIO,
396 		.name = "prio",
397 		.desc = "Link priority for failover re-selection",
398 		.flags = BOND_OPTFLAG_RAWVAL,
399 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
400 						BIT(BOND_MODE_TLB) |
401 						BIT(BOND_MODE_ALB)),
402 		.set = bond_option_prio_set
403 	},
404 	[BOND_OPT_PRIMARY] = {
405 		.id = BOND_OPT_PRIMARY,
406 		.name = "primary",
407 		.desc = "Primary network device to use",
408 		.flags = BOND_OPTFLAG_RAWVAL,
409 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
410 						BIT(BOND_MODE_TLB) |
411 						BIT(BOND_MODE_ALB)),
412 		.set = bond_option_primary_set
413 	},
414 	[BOND_OPT_PRIMARY_RESELECT] = {
415 		.id = BOND_OPT_PRIMARY_RESELECT,
416 		.name = "primary_reselect",
417 		.desc = "Reselect primary slave once it comes up",
418 		.values = bond_primary_reselect_tbl,
419 		.set = bond_option_primary_reselect_set
420 	},
421 	[BOND_OPT_USE_CARRIER] = {
422 		.id = BOND_OPT_USE_CARRIER,
423 		.name = "use_carrier",
424 		.desc = "option obsolete, use_carrier cannot be disabled",
425 		.values = bond_use_carrier_tbl,
426 		.set = bond_option_use_carrier_set
427 	},
428 	[BOND_OPT_ACTIVE_SLAVE] = {
429 		.id = BOND_OPT_ACTIVE_SLAVE,
430 		.name = "active_slave",
431 		.desc = "Currently active slave",
432 		.flags = BOND_OPTFLAG_RAWVAL,
433 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
434 						BIT(BOND_MODE_TLB) |
435 						BIT(BOND_MODE_ALB)),
436 		.set = bond_option_active_slave_set
437 	},
438 	[BOND_OPT_QUEUE_ID] = {
439 		.id = BOND_OPT_QUEUE_ID,
440 		.name = "queue_id",
441 		.desc = "Set queue id of a slave",
442 		.flags = BOND_OPTFLAG_RAWVAL,
443 		.set = bond_option_queue_id_set
444 	},
445 	[BOND_OPT_ALL_SLAVES_ACTIVE] = {
446 		.id = BOND_OPT_ALL_SLAVES_ACTIVE,
447 		.name = "all_slaves_active",
448 		.desc = "Keep all frames received on an interface by setting active flag for all slaves",
449 		.values = bond_all_slaves_active_tbl,
450 		.set = bond_option_all_slaves_active_set
451 	},
452 	[BOND_OPT_RESEND_IGMP] = {
453 		.id = BOND_OPT_RESEND_IGMP,
454 		.name = "resend_igmp",
455 		.desc = "Number of IGMP membership reports to send on link failure",
456 		.values = bond_resend_igmp_tbl,
457 		.set = bond_option_resend_igmp_set
458 	},
459 	[BOND_OPT_LP_INTERVAL] = {
460 		.id = BOND_OPT_LP_INTERVAL,
461 		.name = "lp_interval",
462 		.desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
463 		.values = bond_lp_interval_tbl,
464 		.set = bond_option_lp_interval_set
465 	},
466 	[BOND_OPT_SLAVES] = {
467 		.id = BOND_OPT_SLAVES,
468 		.name = "slaves",
469 		.desc = "Slave membership management",
470 		.flags = BOND_OPTFLAG_RAWVAL,
471 		.set = bond_option_slaves_set
472 	},
473 	[BOND_OPT_TLB_DYNAMIC_LB] = {
474 		.id = BOND_OPT_TLB_DYNAMIC_LB,
475 		.name = "tlb_dynamic_lb",
476 		.desc = "Enable dynamic flow shuffling",
477 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB) | BIT(BOND_MODE_ALB)),
478 		.values = bond_tlb_dynamic_lb_tbl,
479 		.flags = BOND_OPTFLAG_IFDOWN,
480 		.set = bond_option_tlb_dynamic_lb_set,
481 	},
482 	[BOND_OPT_AD_ACTOR_SYS_PRIO] = {
483 		.id = BOND_OPT_AD_ACTOR_SYS_PRIO,
484 		.name = "ad_actor_sys_prio",
485 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
486 		.values = bond_ad_actor_sys_prio_tbl,
487 		.set = bond_option_ad_actor_sys_prio_set,
488 	},
489 	[BOND_OPT_ACTOR_PORT_PRIO] = {
490 		.id = BOND_OPT_ACTOR_PORT_PRIO,
491 		.name = "actor_port_prio",
492 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
493 		.flags = BOND_OPTFLAG_RAWVAL,
494 		.set = bond_option_actor_port_prio_set,
495 	},
496 	[BOND_OPT_AD_ACTOR_SYSTEM] = {
497 		.id = BOND_OPT_AD_ACTOR_SYSTEM,
498 		.name = "ad_actor_system",
499 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
500 		.flags = BOND_OPTFLAG_RAWVAL,
501 		.set = bond_option_ad_actor_system_set,
502 	},
503 	[BOND_OPT_AD_USER_PORT_KEY] = {
504 		.id = BOND_OPT_AD_USER_PORT_KEY,
505 		.name = "ad_user_port_key",
506 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
507 		.flags = BOND_OPTFLAG_IFDOWN,
508 		.values = bond_ad_user_port_key_tbl,
509 		.set = bond_option_ad_user_port_key_set,
510 	},
511 	[BOND_OPT_NUM_PEER_NOTIF_ALIAS] = {
512 		.id = BOND_OPT_NUM_PEER_NOTIF_ALIAS,
513 		.name = "num_grat_arp",
514 		.desc = "Number of peer notifications to send on failover event",
515 		.values = bond_num_peer_notif_tbl,
516 		.set = bond_option_num_peer_notif_set
517 	},
518 	[BOND_OPT_PEER_NOTIF_DELAY] = {
519 		.id = BOND_OPT_PEER_NOTIF_DELAY,
520 		.name = "peer_notif_delay",
521 		.desc = "Delay between each peer notification on failover event, in milliseconds",
522 		.values = bond_peer_notif_delay_tbl,
523 		.set = bond_option_peer_notif_delay_set
524 	},
525 	[BOND_OPT_COUPLED_CONTROL] = {
526 		.id = BOND_OPT_COUPLED_CONTROL,
527 		.name = "coupled_control",
528 		.desc = "Opt into using coupled control MUX for LACP states",
529 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
530 		.flags = BOND_OPTFLAG_IFDOWN,
531 		.values = bond_coupled_control_tbl,
532 		.set = bond_option_coupled_control_set,
533 	},
534 	[BOND_OPT_BROADCAST_NEIGH] = {
535 		.id = BOND_OPT_BROADCAST_NEIGH,
536 		.name = "broadcast_neighbor",
537 		.desc = "Broadcast neighbor packets to all active slaves",
538 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
539 		.values = bond_broadcast_neigh_tbl,
540 		.set = bond_option_broadcast_neigh_set,
541 	}
542 };
543 
544 /* Searches for an option by name */
545 const struct bond_option *bond_opt_get_by_name(const char *name)
546 {
547 	const struct bond_option *opt;
548 	int option;
549 
550 	for (option = 0; option < BOND_OPT_LAST; option++) {
551 		opt = bond_opt_get(option);
552 		if (opt && !strcmp(opt->name, name))
553 			return opt;
554 	}
555 
556 	return NULL;
557 }
558 
559 /* Searches for a value in opt's values[] table */
560 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
561 {
562 	const struct bond_option *opt;
563 	int i;
564 
565 	opt = bond_opt_get(option);
566 	if (WARN_ON(!opt))
567 		return NULL;
568 	for (i = 0; opt->values && opt->values[i].string; i++)
569 		if (opt->values[i].value == val)
570 			return &opt->values[i];
571 
572 	return NULL;
573 }
574 
575 /* Searches for a value in opt's values[] table which matches the flagmask */
576 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
577 						       u32 flagmask)
578 {
579 	int i;
580 
581 	for (i = 0; opt->values && opt->values[i].string; i++)
582 		if (opt->values[i].flags & flagmask)
583 			return &opt->values[i];
584 
585 	return NULL;
586 }
587 
588 /* If maxval is missing then there's no range to check. In case minval is
589  * missing then it's considered to be 0.
590  */
591 static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
592 {
593 	const struct bond_opt_value *minval, *maxval;
594 
595 	minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
596 	maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
597 	if (!maxval || (minval && val < minval->value) || val > maxval->value)
598 		return false;
599 
600 	return true;
601 }
602 
603 /**
604  * bond_opt_parse - parse option value
605  * @opt: the option to parse against
606  * @val: value to parse
607  *
608  * This function tries to extract the value from @val and check if it's
609  * a possible match for the option and returns NULL if a match isn't found,
610  * or the struct_opt_value that matched. It also strips the new line from
611  * @val->string if it's present.
612  */
613 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
614 					    struct bond_opt_value *val)
615 {
616 	char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
617 	const struct bond_opt_value *tbl;
618 	const struct bond_opt_value *ret = NULL;
619 	bool checkval;
620 	int i, rv;
621 
622 	/* No parsing if the option wants a raw val */
623 	if (opt->flags & BOND_OPTFLAG_RAWVAL)
624 		return val;
625 
626 	tbl = opt->values;
627 	if (!tbl)
628 		goto out;
629 
630 	/* ULLONG_MAX is used to bypass string processing */
631 	checkval = val->value != ULLONG_MAX;
632 	if (!checkval) {
633 		if (!val->string)
634 			goto out;
635 		p = strchr(val->string, '\n');
636 		if (p)
637 			*p = '\0';
638 		for (p = val->string; *p; p++)
639 			if (!(isdigit(*p) || isspace(*p)))
640 				break;
641 		/* The following code extracts the string to match or the value
642 		 * and sets checkval appropriately
643 		 */
644 		if (*p) {
645 			rv = sscanf(val->string, "%32s", valstr);
646 		} else {
647 			rv = sscanf(val->string, "%llu", &val->value);
648 			checkval = true;
649 		}
650 		if (!rv)
651 			goto out;
652 	}
653 
654 	for (i = 0; tbl[i].string; i++) {
655 		/* Check for exact match */
656 		if (checkval) {
657 			if (val->value == tbl[i].value)
658 				ret = &tbl[i];
659 		} else {
660 			if (!strcmp(valstr, "default") &&
661 			    (tbl[i].flags & BOND_VALFLAG_DEFAULT))
662 				ret = &tbl[i];
663 
664 			if (!strcmp(valstr, tbl[i].string))
665 				ret = &tbl[i];
666 		}
667 		/* Found an exact match */
668 		if (ret)
669 			goto out;
670 	}
671 	/* Possible range match */
672 	if (checkval && bond_opt_check_range(opt, val->value))
673 		ret = val;
674 out:
675 	return ret;
676 }
677 
678 /* Check opt's dependencies against bond mode and currently set options */
679 static int bond_opt_check_deps(struct bonding *bond,
680 			       const struct bond_option *opt)
681 {
682 	struct bond_params *params = &bond->params;
683 
684 	if (test_bit(params->mode, &opt->unsuppmodes))
685 		return -EACCES;
686 	if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
687 		return -ENOTEMPTY;
688 	if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
689 		return -EBUSY;
690 
691 	return 0;
692 }
693 
694 static void bond_opt_dep_print(struct bonding *bond,
695 			       const struct bond_option *opt,
696 			       struct nlattr *bad_attr,
697 			       struct netlink_ext_ack *extack)
698 {
699 	const struct bond_opt_value *modeval;
700 	struct bond_params *params;
701 
702 	params = &bond->params;
703 	modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
704 	if (test_bit(params->mode, &opt->unsuppmodes)) {
705 		netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
706 			   opt->name, modeval->string, modeval->value);
707 		NL_SET_ERR_MSG_ATTR(extack, bad_attr,
708 				    "option not supported in mode");
709 	}
710 }
711 
712 static void bond_opt_error_interpret(struct bonding *bond,
713 				     const struct bond_option *opt,
714 				     int error, const struct bond_opt_value *val,
715 				     struct nlattr *bad_attr,
716 				     struct netlink_ext_ack *extack)
717 {
718 	const struct bond_opt_value *minval, *maxval;
719 	char *p;
720 
721 	switch (error) {
722 	case -EINVAL:
723 		NL_SET_ERR_MSG_ATTR(extack, bad_attr, "invalid option value");
724 		if (val) {
725 			if (val->string) {
726 				/* sometimes RAWVAL opts may have new lines */
727 				p = strchr(val->string, '\n');
728 				if (p)
729 					*p = '\0';
730 				netdev_err(bond->dev, "option %s: invalid value (%s)\n",
731 					   opt->name, val->string);
732 			} else {
733 				netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
734 					   opt->name, val->value);
735 			}
736 		}
737 		minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
738 		maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
739 		if (!maxval)
740 			break;
741 		netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
742 			   opt->name, minval ? minval->value : 0, maxval->value);
743 		break;
744 	case -EACCES:
745 		bond_opt_dep_print(bond, opt, bad_attr, extack);
746 		break;
747 	case -ENOTEMPTY:
748 		NL_SET_ERR_MSG_ATTR(extack, bad_attr,
749 				    "unable to set option because the bond device has slaves");
750 		netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
751 			   opt->name);
752 		break;
753 	case -EBUSY:
754 		NL_SET_ERR_MSG_ATTR(extack, bad_attr,
755 				    "unable to set option because the bond is up");
756 		netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
757 			   opt->name);
758 		break;
759 	case -ENODEV:
760 		if (val && val->string) {
761 			p = strchr(val->string, '\n');
762 			if (p)
763 				*p = '\0';
764 			netdev_err(bond->dev, "option %s: interface %s does not exist!\n",
765 				   opt->name, val->string);
766 			NL_SET_ERR_MSG_ATTR(extack, bad_attr,
767 					    "interface does not exist");
768 		}
769 		break;
770 	default:
771 		break;
772 	}
773 }
774 
775 /**
776  * __bond_opt_set - set a bonding option
777  * @bond: target bond device
778  * @option: option to set
779  * @val: value to set it to
780  * @bad_attr: netlink attribue that caused the error
781  * @extack: extended netlink error structure, used when an error message
782  *          needs to be returned to the caller via netlink
783  *
784  * This function is used to change the bond's option value, it can be
785  * used for both enabling/changing an option and for disabling it. RTNL lock
786  * must be obtained before calling this function.
787  */
788 int __bond_opt_set(struct bonding *bond,
789 		   unsigned int option, struct bond_opt_value *val,
790 		   struct nlattr *bad_attr, struct netlink_ext_ack *extack)
791 {
792 	const struct bond_opt_value *retval = NULL;
793 	const struct bond_option *opt;
794 	int ret = -ENOENT;
795 
796 	ASSERT_RTNL();
797 
798 	opt = bond_opt_get(option);
799 	if (WARN_ON(!val) || WARN_ON(!opt))
800 		goto out;
801 	ret = bond_opt_check_deps(bond, opt);
802 	if (ret)
803 		goto out;
804 	retval = bond_opt_parse(opt, val);
805 	if (!retval) {
806 		ret = -EINVAL;
807 		goto out;
808 	}
809 	ret = opt->set(bond, retval);
810 out:
811 	if (ret)
812 		bond_opt_error_interpret(bond, opt, ret, val, bad_attr, extack);
813 
814 	return ret;
815 }
816 /**
817  * __bond_opt_set_notify - set a bonding option
818  * @bond: target bond device
819  * @option: option to set
820  * @val: value to set it to
821  *
822  * This function is used to change the bond's option value and trigger
823  * a notification to user sapce. It can be used for both enabling/changing
824  * an option and for disabling it. RTNL lock must be obtained before calling
825  * this function.
826  */
827 int __bond_opt_set_notify(struct bonding *bond,
828 			  unsigned int option, struct bond_opt_value *val)
829 {
830 	int ret;
831 
832 	ASSERT_RTNL();
833 
834 	ret = __bond_opt_set(bond, option, val, NULL, NULL);
835 
836 	if (!ret && (bond->dev->reg_state == NETREG_REGISTERED))
837 		call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
838 
839 	return ret;
840 }
841 
842 /**
843  * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
844  * @bond: target bond device
845  * @option: option to set
846  * @buf: value to set it to
847  *
848  * This function tries to acquire RTNL without blocking and if successful
849  * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
850  */
851 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
852 {
853 	struct bond_opt_value optval;
854 	int ret;
855 
856 	if (!rtnl_trylock())
857 		return restart_syscall();
858 	bond_opt_initstr(&optval, buf);
859 	ret = __bond_opt_set_notify(bond, option, &optval);
860 	rtnl_unlock();
861 
862 	return ret;
863 }
864 
865 /**
866  * bond_opt_get - get a pointer to an option
867  * @option: option for which to return a pointer
868  *
869  * This function checks if option is valid and if so returns a pointer
870  * to its entry in the bond_opts[] option array.
871  */
872 const struct bond_option *bond_opt_get(unsigned int option)
873 {
874 	if (!BOND_OPT_VALID(option))
875 		return NULL;
876 
877 	return &bond_opts[option];
878 }
879 
880 static bool bond_set_xfrm_features(struct bonding *bond)
881 {
882 	if (!IS_ENABLED(CONFIG_XFRM_OFFLOAD))
883 		return false;
884 
885 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
886 		bond->dev->wanted_features |= BOND_XFRM_FEATURES;
887 	else
888 		bond->dev->wanted_features &= ~BOND_XFRM_FEATURES;
889 
890 	return true;
891 }
892 
893 static int bond_option_mode_set(struct bonding *bond,
894 				const struct bond_opt_value *newval)
895 {
896 	if (bond->xdp_prog && !bond_xdp_check(bond, newval->value))
897 		return -EOPNOTSUPP;
898 
899 	if (!bond_mode_uses_arp(newval->value)) {
900 		if (bond->params.arp_interval) {
901 			netdev_dbg(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
902 				   newval->string);
903 			/* disable arp monitoring */
904 			bond->params.arp_interval = 0;
905 		}
906 
907 		if (!bond->params.miimon) {
908 			/* set miimon to default value */
909 			bond->params.miimon = BOND_DEFAULT_MIIMON;
910 			netdev_dbg(bond->dev, "Setting MII monitoring interval to %d\n",
911 				   bond->params.miimon);
912 		}
913 	}
914 
915 	if (newval->value == BOND_MODE_ALB)
916 		bond->params.tlb_dynamic_lb = 1;
917 
918 	/* don't cache arp_validate between modes */
919 	bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
920 	bond->params.mode = newval->value;
921 
922 	/* When changing mode, the bond device is down, we may reduce
923 	 * the bond_bcast_neigh_enabled in bond_close() if broadcast_neighbor
924 	 * enabled in 8023ad mode. Therefore, only clear broadcast_neighbor
925 	 * to 0.
926 	 */
927 	bond->params.broadcast_neighbor = 0;
928 
929 	if (bond->dev->reg_state == NETREG_REGISTERED) {
930 		bool update = false;
931 
932 		update |= bond_set_xfrm_features(bond);
933 
934 		if (update)
935 			netdev_update_features(bond->dev);
936 	}
937 
938 	bond_xdp_set_features(bond->dev);
939 
940 	return 0;
941 }
942 
943 static int bond_option_active_slave_set(struct bonding *bond,
944 					const struct bond_opt_value *newval)
945 {
946 	char ifname[IFNAMSIZ] = { 0, };
947 	struct net_device *slave_dev;
948 	int ret = 0;
949 
950 	sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
951 	if (!strlen(ifname) || newval->string[0] == '\n') {
952 		slave_dev = NULL;
953 	} else {
954 		slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
955 		if (!slave_dev)
956 			return -ENODEV;
957 	}
958 
959 	if (slave_dev) {
960 		if (!netif_is_bond_slave(slave_dev)) {
961 			slave_err(bond->dev, slave_dev, "Device is not bonding slave\n");
962 			return -EINVAL;
963 		}
964 
965 		if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
966 			slave_err(bond->dev, slave_dev, "Device is not our slave\n");
967 			return -EINVAL;
968 		}
969 	}
970 
971 	block_netpoll_tx();
972 	/* check to see if we are clearing active */
973 	if (!slave_dev) {
974 		netdev_dbg(bond->dev, "Clearing current active slave\n");
975 		bond_change_active_slave(bond, NULL);
976 		bond_select_active_slave(bond);
977 	} else {
978 		struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
979 		struct slave *new_active = bond_slave_get_rtnl(slave_dev);
980 
981 		BUG_ON(!new_active);
982 
983 		if (new_active == old_active) {
984 			/* do nothing */
985 			slave_dbg(bond->dev, new_active->dev, "is already the current active slave\n");
986 		} else {
987 			if (old_active && (new_active->link == BOND_LINK_UP) &&
988 			    bond_slave_is_up(new_active)) {
989 				slave_dbg(bond->dev, new_active->dev, "Setting as active slave\n");
990 				bond_change_active_slave(bond, new_active);
991 			} else {
992 				slave_err(bond->dev, new_active->dev, "Could not set as active slave; either %s is down or the link is down\n",
993 					  new_active->dev->name);
994 				ret = -EINVAL;
995 			}
996 		}
997 	}
998 	unblock_netpoll_tx();
999 
1000 	return ret;
1001 }
1002 
1003 /* There are two tricky bits here.  First, if MII monitoring is activated, then
1004  * we must disable ARP monitoring.  Second, if the timer isn't running, we must
1005  * start it.
1006  */
1007 static int bond_option_miimon_set(struct bonding *bond,
1008 				  const struct bond_opt_value *newval)
1009 {
1010 	netdev_dbg(bond->dev, "Setting MII monitoring interval to %llu\n",
1011 		   newval->value);
1012 	bond->params.miimon = newval->value;
1013 	if (bond->params.updelay)
1014 		netdev_dbg(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
1015 			   bond->params.updelay * bond->params.miimon);
1016 	if (bond->params.downdelay)
1017 		netdev_dbg(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
1018 			   bond->params.downdelay * bond->params.miimon);
1019 	if (bond->params.peer_notif_delay)
1020 		netdev_dbg(bond->dev, "Note: Updating peer_notif_delay (to %d) since it is a multiple of the miimon value\n",
1021 			   bond->params.peer_notif_delay * bond->params.miimon);
1022 	if (newval->value && bond->params.arp_interval) {
1023 		netdev_dbg(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
1024 		bond->params.arp_interval = 0;
1025 		if (bond->params.arp_validate)
1026 			bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1027 	}
1028 	if (bond->dev->flags & IFF_UP) {
1029 		/* If the interface is up, we may need to fire off
1030 		 * the MII timer. If the interface is down, the
1031 		 * timer will get fired off when the open function
1032 		 * is called.
1033 		 */
1034 		if (!newval->value) {
1035 			cancel_delayed_work_sync(&bond->mii_work);
1036 		} else {
1037 			cancel_delayed_work_sync(&bond->arp_work);
1038 			queue_delayed_work(bond->wq, &bond->mii_work, 0);
1039 		}
1040 	}
1041 
1042 	return 0;
1043 }
1044 
1045 /* Set up, down and peer notification delays. These must be multiples
1046  * of the MII monitoring value, and are stored internally as the
1047  * multiplier. Thus, we must translate to MS for the real world.
1048  */
1049 static int _bond_option_delay_set(struct bonding *bond,
1050 				  const struct bond_opt_value *newval,
1051 				  const char *name,
1052 				  int *target)
1053 {
1054 	int value = newval->value;
1055 
1056 	if (!bond->params.miimon) {
1057 		netdev_err(bond->dev, "Unable to set %s as MII monitoring is disabled\n",
1058 			   name);
1059 		return -EPERM;
1060 	}
1061 	if ((value % bond->params.miimon) != 0) {
1062 		netdev_warn(bond->dev,
1063 			    "%s (%d) is not a multiple of miimon (%d), value rounded to %d ms\n",
1064 			    name,
1065 			    value, bond->params.miimon,
1066 			    (value / bond->params.miimon) *
1067 			    bond->params.miimon);
1068 	}
1069 	*target = value / bond->params.miimon;
1070 	netdev_dbg(bond->dev, "Setting %s to %d\n",
1071 		   name,
1072 		   *target * bond->params.miimon);
1073 
1074 	return 0;
1075 }
1076 
1077 static int bond_option_updelay_set(struct bonding *bond,
1078 				   const struct bond_opt_value *newval)
1079 {
1080 	return _bond_option_delay_set(bond, newval, "up delay",
1081 				      &bond->params.updelay);
1082 }
1083 
1084 static int bond_option_downdelay_set(struct bonding *bond,
1085 				     const struct bond_opt_value *newval)
1086 {
1087 	return _bond_option_delay_set(bond, newval, "down delay",
1088 				      &bond->params.downdelay);
1089 }
1090 
1091 static int bond_option_peer_notif_delay_set(struct bonding *bond,
1092 					    const struct bond_opt_value *newval)
1093 {
1094 	int ret = _bond_option_delay_set(bond, newval,
1095 					 "peer notification delay",
1096 					 &bond->params.peer_notif_delay);
1097 	return ret;
1098 }
1099 
1100 static int bond_option_use_carrier_set(struct bonding *bond,
1101 				       const struct bond_opt_value *newval)
1102 {
1103 	return 0;
1104 }
1105 
1106 /* There are two tricky bits here.  First, if ARP monitoring is activated, then
1107  * we must disable MII monitoring.  Second, if the ARP timer isn't running,
1108  * we must start it.
1109  */
1110 static int bond_option_arp_interval_set(struct bonding *bond,
1111 					const struct bond_opt_value *newval)
1112 {
1113 	netdev_dbg(bond->dev, "Setting ARP monitoring interval to %llu\n",
1114 		   newval->value);
1115 	bond->params.arp_interval = newval->value;
1116 	if (newval->value) {
1117 		if (bond->params.miimon) {
1118 			netdev_dbg(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
1119 			bond->params.miimon = 0;
1120 		}
1121 		if (!bond->params.arp_targets[0])
1122 			netdev_dbg(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
1123 	}
1124 	if (bond->dev->flags & IFF_UP) {
1125 		/* If the interface is up, we may need to fire off
1126 		 * the ARP timer.  If the interface is down, the
1127 		 * timer will get fired off when the open function
1128 		 * is called.
1129 		 */
1130 		if (!newval->value) {
1131 			if (bond->params.arp_validate)
1132 				bond->recv_probe = NULL;
1133 			cancel_delayed_work_sync(&bond->arp_work);
1134 		} else {
1135 			/* arp_validate can be set only in active-backup mode */
1136 			bond->recv_probe = bond_rcv_validate;
1137 			cancel_delayed_work_sync(&bond->mii_work);
1138 			queue_delayed_work(bond->wq, &bond->arp_work, 0);
1139 		}
1140 	}
1141 
1142 	return 0;
1143 }
1144 
1145 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
1146 					    __be32 target,
1147 					    unsigned long last_rx)
1148 {
1149 	__be32 *targets = bond->params.arp_targets;
1150 	struct list_head *iter;
1151 	struct slave *slave;
1152 
1153 	if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
1154 		bond_for_each_slave(bond, slave, iter)
1155 			slave->target_last_arp_rx[slot] = last_rx;
1156 		targets[slot] = target;
1157 	}
1158 }
1159 
1160 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
1161 {
1162 	__be32 *targets = bond->params.arp_targets;
1163 	int ind;
1164 
1165 	if (!bond_is_ip_target_ok(target)) {
1166 		netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
1167 			   &target);
1168 		return -EINVAL;
1169 	}
1170 
1171 	if (bond_get_targets_ip(targets, target) != -1) { /* dup */
1172 		netdev_err(bond->dev, "ARP target %pI4 is already present\n",
1173 			   &target);
1174 		return -EINVAL;
1175 	}
1176 
1177 	ind = bond_get_targets_ip(targets, 0); /* first free slot */
1178 	if (ind == -1) {
1179 		netdev_err(bond->dev, "ARP target table is full!\n");
1180 		return -EINVAL;
1181 	}
1182 
1183 	netdev_dbg(bond->dev, "Adding ARP target %pI4\n", &target);
1184 
1185 	_bond_options_arp_ip_target_set(bond, ind, target, jiffies);
1186 
1187 	return 0;
1188 }
1189 
1190 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
1191 {
1192 	return _bond_option_arp_ip_target_add(bond, target);
1193 }
1194 
1195 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
1196 {
1197 	__be32 *targets = bond->params.arp_targets;
1198 	struct list_head *iter;
1199 	struct slave *slave;
1200 	unsigned long *targets_rx;
1201 	int ind, i;
1202 
1203 	if (!bond_is_ip_target_ok(target)) {
1204 		netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
1205 			   &target);
1206 		return -EINVAL;
1207 	}
1208 
1209 	ind = bond_get_targets_ip(targets, target);
1210 	if (ind == -1) {
1211 		netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
1212 			   &target);
1213 		return -EINVAL;
1214 	}
1215 
1216 	if (ind == 0 && !targets[1] && bond->params.arp_interval)
1217 		netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
1218 
1219 	netdev_dbg(bond->dev, "Removing ARP target %pI4\n", &target);
1220 
1221 	bond_for_each_slave(bond, slave, iter) {
1222 		targets_rx = slave->target_last_arp_rx;
1223 		for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1224 			targets_rx[i] = targets_rx[i+1];
1225 		targets_rx[i] = 0;
1226 	}
1227 	for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1228 		targets[i] = targets[i+1];
1229 	targets[i] = 0;
1230 
1231 	return 0;
1232 }
1233 
1234 void bond_option_arp_ip_targets_clear(struct bonding *bond)
1235 {
1236 	int i;
1237 
1238 	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1239 		_bond_options_arp_ip_target_set(bond, i, 0, 0);
1240 }
1241 
1242 static int bond_option_arp_ip_targets_set(struct bonding *bond,
1243 					  const struct bond_opt_value *newval)
1244 {
1245 	int ret = -EPERM;
1246 	__be32 target;
1247 
1248 	if (newval->string) {
1249 		if (strlen(newval->string) < 1 ||
1250 		    !in4_pton(newval->string + 1, -1, (u8 *)&target, -1, NULL)) {
1251 			netdev_err(bond->dev, "invalid ARP target specified\n");
1252 			return ret;
1253 		}
1254 		if (newval->string[0] == '+')
1255 			ret = bond_option_arp_ip_target_add(bond, target);
1256 		else if (newval->string[0] == '-')
1257 			ret = bond_option_arp_ip_target_rem(bond, target);
1258 		else
1259 			netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
1260 	} else {
1261 		target = newval->value;
1262 		ret = bond_option_arp_ip_target_add(bond, target);
1263 	}
1264 
1265 	return ret;
1266 }
1267 
1268 #if IS_ENABLED(CONFIG_IPV6)
1269 static bool slave_can_set_ns_maddr(const struct bonding *bond, struct slave *slave)
1270 {
1271 	return BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
1272 	       !bond_is_active_slave(slave) &&
1273 	       slave->dev->flags & IFF_MULTICAST;
1274 }
1275 
1276 /**
1277  * slave_set_ns_maddrs - add/del all NS mac addresses for slave
1278  * @bond: bond device
1279  * @slave: slave device
1280  * @add: add or remove all the NS mac addresses
1281  *
1282  * This function tries to add or delete all the NS mac addresses on the slave
1283  *
1284  * Note, the IPv6 NS target address is the unicast address in Neighbor
1285  * Solicitation (NS) message. The dest address of NS message should be
1286  * solicited-node multicast address of the target. The dest mac of NS message
1287  * is converted from the solicited-node multicast address.
1288  *
1289  * This function is called when
1290  *   * arp_validate changes
1291  *   * enslaving, releasing new slaves
1292  */
1293 static void slave_set_ns_maddrs(struct bonding *bond, struct slave *slave, bool add)
1294 {
1295 	struct in6_addr *targets = bond->params.ns_targets;
1296 	char slot_maddr[MAX_ADDR_LEN];
1297 	struct in6_addr mcaddr;
1298 	int i;
1299 
1300 	if (!slave_can_set_ns_maddr(bond, slave))
1301 		return;
1302 
1303 	for (i = 0; i < BOND_MAX_NS_TARGETS; i++) {
1304 		if (ipv6_addr_any(&targets[i]))
1305 			break;
1306 
1307 		addrconf_addr_solict_mult(&targets[i], &mcaddr);
1308 		if (!ndisc_mc_map(&mcaddr, slot_maddr, slave->dev, 0)) {
1309 			if (add)
1310 				dev_mc_add(slave->dev, slot_maddr);
1311 			else
1312 				dev_mc_del(slave->dev, slot_maddr);
1313 		}
1314 	}
1315 }
1316 
1317 void bond_slave_ns_maddrs_add(struct bonding *bond, struct slave *slave)
1318 {
1319 	if (!bond->params.arp_validate)
1320 		return;
1321 	slave_set_ns_maddrs(bond, slave, true);
1322 }
1323 
1324 void bond_slave_ns_maddrs_del(struct bonding *bond, struct slave *slave)
1325 {
1326 	if (!bond->params.arp_validate)
1327 		return;
1328 	slave_set_ns_maddrs(bond, slave, false);
1329 }
1330 
1331 /**
1332  * slave_set_ns_maddr - set new NS mac address for slave
1333  * @bond: bond device
1334  * @slave: slave device
1335  * @target: the new IPv6 target
1336  * @slot: the old IPv6 target in the slot
1337  *
1338  * This function tries to replace the old mac address to new one on the slave.
1339  *
1340  * Note, the target/slot IPv6 address is the unicast address in Neighbor
1341  * Solicitation (NS) message. The dest address of NS message should be
1342  * solicited-node multicast address of the target. The dest mac of NS message
1343  * is converted from the solicited-node multicast address.
1344  *
1345  * This function is called when
1346  *   * An IPv6 NS target is added or removed.
1347  */
1348 static void slave_set_ns_maddr(struct bonding *bond, struct slave *slave,
1349 			       struct in6_addr *target, struct in6_addr *slot)
1350 {
1351 	char mac_addr[MAX_ADDR_LEN];
1352 	struct in6_addr mcast_addr;
1353 
1354 	if (!bond->params.arp_validate || !slave_can_set_ns_maddr(bond, slave))
1355 		return;
1356 
1357 	/* remove the previous mac addr from slave */
1358 	addrconf_addr_solict_mult(slot, &mcast_addr);
1359 	if (!ipv6_addr_any(slot) &&
1360 	    !ndisc_mc_map(&mcast_addr, mac_addr, slave->dev, 0))
1361 		dev_mc_del(slave->dev, mac_addr);
1362 
1363 	/* add new mac addr on slave if target is set */
1364 	addrconf_addr_solict_mult(target, &mcast_addr);
1365 	if (!ipv6_addr_any(target) &&
1366 	    !ndisc_mc_map(&mcast_addr, mac_addr, slave->dev, 0))
1367 		dev_mc_add(slave->dev, mac_addr);
1368 }
1369 
1370 static void _bond_options_ns_ip6_target_set(struct bonding *bond, int slot,
1371 					    struct in6_addr *target,
1372 					    unsigned long last_rx)
1373 {
1374 	struct in6_addr *targets = bond->params.ns_targets;
1375 	struct list_head *iter;
1376 	struct slave *slave;
1377 
1378 	if (slot >= 0 && slot < BOND_MAX_NS_TARGETS) {
1379 		bond_for_each_slave(bond, slave, iter) {
1380 			slave->target_last_arp_rx[slot] = last_rx;
1381 			slave_set_ns_maddr(bond, slave, target, &targets[slot]);
1382 		}
1383 		targets[slot] = *target;
1384 	}
1385 }
1386 
1387 void bond_option_ns_ip6_targets_clear(struct bonding *bond)
1388 {
1389 	struct in6_addr addr_any = in6addr_any;
1390 	int i;
1391 
1392 	for (i = 0; i < BOND_MAX_NS_TARGETS; i++)
1393 		_bond_options_ns_ip6_target_set(bond, i, &addr_any, 0);
1394 }
1395 
1396 static int bond_option_ns_ip6_targets_set(struct bonding *bond,
1397 					  const struct bond_opt_value *newval)
1398 {
1399 	struct in6_addr *target = (struct in6_addr *)newval->extra;
1400 	struct in6_addr *targets = bond->params.ns_targets;
1401 	struct in6_addr addr_any = in6addr_any;
1402 	int index;
1403 
1404 	if (!bond_is_ip6_target_ok(target)) {
1405 		netdev_err(bond->dev, "invalid NS target %pI6c specified for addition\n",
1406 			   target);
1407 		return -EINVAL;
1408 	}
1409 
1410 	if (bond_get_targets_ip6(targets, target) != -1) { /* dup */
1411 		netdev_err(bond->dev, "NS target %pI6c is already present\n",
1412 			   target);
1413 		return -EINVAL;
1414 	}
1415 
1416 	index = bond_get_targets_ip6(targets, &addr_any); /* first free slot */
1417 	if (index == -1) {
1418 		netdev_err(bond->dev, "NS target table is full!\n");
1419 		return -EINVAL;
1420 	}
1421 
1422 	netdev_dbg(bond->dev, "Adding NS target %pI6c\n", target);
1423 
1424 	_bond_options_ns_ip6_target_set(bond, index, target, jiffies);
1425 
1426 	return 0;
1427 }
1428 #else
1429 static int bond_option_ns_ip6_targets_set(struct bonding *bond,
1430 					  const struct bond_opt_value *newval)
1431 {
1432 	return -EPERM;
1433 }
1434 
1435 static void slave_set_ns_maddrs(struct bonding *bond, struct slave *slave, bool add) {}
1436 
1437 void bond_slave_ns_maddrs_add(struct bonding *bond, struct slave *slave) {}
1438 
1439 void bond_slave_ns_maddrs_del(struct bonding *bond, struct slave *slave) {}
1440 #endif
1441 
1442 static int bond_option_arp_validate_set(struct bonding *bond,
1443 					const struct bond_opt_value *newval)
1444 {
1445 	bool changed = !!bond->params.arp_validate != !!newval->value;
1446 	struct list_head *iter;
1447 	struct slave *slave;
1448 
1449 	netdev_dbg(bond->dev, "Setting arp_validate to %s (%llu)\n",
1450 		   newval->string, newval->value);
1451 	bond->params.arp_validate = newval->value;
1452 
1453 	if (changed) {
1454 		bond_for_each_slave(bond, slave, iter)
1455 			slave_set_ns_maddrs(bond, slave, !!bond->params.arp_validate);
1456 	}
1457 
1458 	return 0;
1459 }
1460 
1461 static int bond_option_arp_all_targets_set(struct bonding *bond,
1462 					   const struct bond_opt_value *newval)
1463 {
1464 	netdev_dbg(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
1465 		   newval->string, newval->value);
1466 	bond->params.arp_all_targets = newval->value;
1467 
1468 	return 0;
1469 }
1470 
1471 static int bond_option_missed_max_set(struct bonding *bond,
1472 				      const struct bond_opt_value *newval)
1473 {
1474 	netdev_dbg(bond->dev, "Setting missed max to %s (%llu)\n",
1475 		   newval->string, newval->value);
1476 	bond->params.missed_max = newval->value;
1477 
1478 	return 0;
1479 }
1480 
1481 static int bond_option_prio_set(struct bonding *bond,
1482 				const struct bond_opt_value *newval)
1483 {
1484 	struct slave *slave;
1485 
1486 	slave = bond_slave_get_rtnl(newval->slave_dev);
1487 	if (!slave) {
1488 		netdev_dbg(newval->slave_dev, "%s called on NULL slave\n", __func__);
1489 		return -ENODEV;
1490 	}
1491 	slave->prio = newval->value;
1492 
1493 	if (rtnl_dereference(bond->primary_slave))
1494 		slave_warn(bond->dev, slave->dev,
1495 			   "prio updated, but will not affect failover re-selection as primary slave have been set\n");
1496 	else
1497 		bond_select_active_slave(bond);
1498 
1499 	return 0;
1500 }
1501 
1502 static int bond_option_primary_set(struct bonding *bond,
1503 				   const struct bond_opt_value *newval)
1504 {
1505 	char *p, *primary = newval->string;
1506 	struct list_head *iter;
1507 	struct slave *slave;
1508 
1509 	block_netpoll_tx();
1510 
1511 	p = strchr(primary, '\n');
1512 	if (p)
1513 		*p = '\0';
1514 	/* check to see if we are clearing primary */
1515 	if (!strlen(primary)) {
1516 		netdev_dbg(bond->dev, "Setting primary slave to None\n");
1517 		RCU_INIT_POINTER(bond->primary_slave, NULL);
1518 		memset(bond->params.primary, 0, sizeof(bond->params.primary));
1519 		bond_select_active_slave(bond);
1520 		goto out;
1521 	}
1522 
1523 	bond_for_each_slave(bond, slave, iter) {
1524 		if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1525 			slave_dbg(bond->dev, slave->dev, "Setting as primary slave\n");
1526 			rcu_assign_pointer(bond->primary_slave, slave);
1527 			strcpy(bond->params.primary, slave->dev->name);
1528 			bond->force_primary = true;
1529 			bond_select_active_slave(bond);
1530 			goto out;
1531 		}
1532 	}
1533 
1534 	if (rtnl_dereference(bond->primary_slave)) {
1535 		netdev_dbg(bond->dev, "Setting primary slave to None\n");
1536 		RCU_INIT_POINTER(bond->primary_slave, NULL);
1537 		bond_select_active_slave(bond);
1538 	}
1539 	strscpy_pad(bond->params.primary, primary, IFNAMSIZ);
1540 
1541 	netdev_dbg(bond->dev, "Recording %s as primary, but it has not been enslaved yet\n",
1542 		   primary);
1543 
1544 out:
1545 	unblock_netpoll_tx();
1546 
1547 	return 0;
1548 }
1549 
1550 static int bond_option_primary_reselect_set(struct bonding *bond,
1551 					    const struct bond_opt_value *newval)
1552 {
1553 	netdev_dbg(bond->dev, "Setting primary_reselect to %s (%llu)\n",
1554 		   newval->string, newval->value);
1555 	bond->params.primary_reselect = newval->value;
1556 
1557 	block_netpoll_tx();
1558 	bond_select_active_slave(bond);
1559 	unblock_netpoll_tx();
1560 
1561 	return 0;
1562 }
1563 
1564 static int bond_option_fail_over_mac_set(struct bonding *bond,
1565 					 const struct bond_opt_value *newval)
1566 {
1567 	netdev_dbg(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
1568 		   newval->string, newval->value);
1569 	bond->params.fail_over_mac = newval->value;
1570 
1571 	return 0;
1572 }
1573 
1574 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1575 					    const struct bond_opt_value *newval)
1576 {
1577 	netdev_dbg(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
1578 		   newval->string, newval->value);
1579 	bond->params.xmit_policy = newval->value;
1580 
1581 	return 0;
1582 }
1583 
1584 static int bond_option_resend_igmp_set(struct bonding *bond,
1585 				       const struct bond_opt_value *newval)
1586 {
1587 	netdev_dbg(bond->dev, "Setting resend_igmp to %llu\n",
1588 		   newval->value);
1589 	bond->params.resend_igmp = newval->value;
1590 
1591 	return 0;
1592 }
1593 
1594 static int bond_option_num_peer_notif_set(struct bonding *bond,
1595 				   const struct bond_opt_value *newval)
1596 {
1597 	bond->params.num_peer_notif = newval->value;
1598 
1599 	return 0;
1600 }
1601 
1602 static int bond_option_all_slaves_active_set(struct bonding *bond,
1603 					     const struct bond_opt_value *newval)
1604 {
1605 	struct list_head *iter;
1606 	struct slave *slave;
1607 
1608 	if (newval->value == bond->params.all_slaves_active)
1609 		return 0;
1610 	bond->params.all_slaves_active = newval->value;
1611 	bond_for_each_slave(bond, slave, iter) {
1612 		if (!bond_is_active_slave(slave)) {
1613 			if (newval->value)
1614 				slave->inactive = 0;
1615 			else
1616 				slave->inactive = 1;
1617 		}
1618 	}
1619 
1620 	return 0;
1621 }
1622 
1623 static int bond_option_min_links_set(struct bonding *bond,
1624 				     const struct bond_opt_value *newval)
1625 {
1626 	netdev_dbg(bond->dev, "Setting min links value to %llu\n",
1627 		   newval->value);
1628 	bond->params.min_links = newval->value;
1629 	bond_set_carrier(bond);
1630 
1631 	return 0;
1632 }
1633 
1634 static int bond_option_lp_interval_set(struct bonding *bond,
1635 				       const struct bond_opt_value *newval)
1636 {
1637 	bond->params.lp_interval = newval->value;
1638 
1639 	return 0;
1640 }
1641 
1642 static int bond_option_pps_set(struct bonding *bond,
1643 			       const struct bond_opt_value *newval)
1644 {
1645 	netdev_dbg(bond->dev, "Setting packets per slave to %llu\n",
1646 		   newval->value);
1647 	bond->params.packets_per_slave = newval->value;
1648 	if (newval->value > 0) {
1649 		bond->params.reciprocal_packets_per_slave =
1650 			reciprocal_value(newval->value);
1651 	} else {
1652 		/* reciprocal_packets_per_slave is unused if
1653 		 * packets_per_slave is 0 or 1, just initialize it
1654 		 */
1655 		bond->params.reciprocal_packets_per_slave =
1656 			(struct reciprocal_value) { 0 };
1657 	}
1658 
1659 	return 0;
1660 }
1661 
1662 static int bond_option_lacp_active_set(struct bonding *bond,
1663 				       const struct bond_opt_value *newval)
1664 {
1665 	netdev_dbg(bond->dev, "Setting LACP active to %s (%llu)\n",
1666 		   newval->string, newval->value);
1667 	bond->params.lacp_active = newval->value;
1668 	bond_3ad_update_lacp_active(bond);
1669 
1670 	return 0;
1671 }
1672 
1673 static int bond_option_lacp_rate_set(struct bonding *bond,
1674 				     const struct bond_opt_value *newval)
1675 {
1676 	netdev_dbg(bond->dev, "Setting LACP rate to %s (%llu)\n",
1677 		   newval->string, newval->value);
1678 	bond->params.lacp_fast = newval->value;
1679 	bond_3ad_update_lacp_rate(bond);
1680 
1681 	return 0;
1682 }
1683 
1684 static int bond_option_ad_select_set(struct bonding *bond,
1685 				     const struct bond_opt_value *newval)
1686 {
1687 	netdev_dbg(bond->dev, "Setting ad_select to %s (%llu)\n",
1688 		   newval->string, newval->value);
1689 	bond->params.ad_select = newval->value;
1690 
1691 	return 0;
1692 }
1693 
1694 static int bond_option_queue_id_set(struct bonding *bond,
1695 				    const struct bond_opt_value *newval)
1696 {
1697 	struct slave *slave, *update_slave;
1698 	struct net_device *sdev;
1699 	struct list_head *iter;
1700 	char *delim;
1701 	int ret = 0;
1702 	u16 qid;
1703 
1704 	/* delim will point to queue id if successful */
1705 	delim = strchr(newval->string, ':');
1706 	if (!delim)
1707 		goto err_no_cmd;
1708 
1709 	/* Terminate string that points to device name and bump it
1710 	 * up one, so we can read the queue id there.
1711 	 */
1712 	*delim = '\0';
1713 	if (sscanf(++delim, "%hd\n", &qid) != 1)
1714 		goto err_no_cmd;
1715 
1716 	/* Check buffer length, valid ifname and queue id */
1717 	if (!dev_valid_name(newval->string) ||
1718 	    qid > bond->dev->real_num_tx_queues)
1719 		goto err_no_cmd;
1720 
1721 	/* Get the pointer to that interface if it exists */
1722 	sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1723 	if (!sdev)
1724 		goto err_no_cmd;
1725 
1726 	/* Search for thes slave and check for duplicate qids */
1727 	update_slave = NULL;
1728 	bond_for_each_slave(bond, slave, iter) {
1729 		if (sdev == slave->dev)
1730 			/* We don't need to check the matching
1731 			 * slave for dups, since we're overwriting it
1732 			 */
1733 			update_slave = slave;
1734 		else if (qid && qid == slave->queue_id) {
1735 			goto err_no_cmd;
1736 		}
1737 	}
1738 
1739 	if (!update_slave)
1740 		goto err_no_cmd;
1741 
1742 	/* Actually set the qids for the slave */
1743 	WRITE_ONCE(update_slave->queue_id, qid);
1744 
1745 out:
1746 	return ret;
1747 
1748 err_no_cmd:
1749 	netdev_dbg(bond->dev, "invalid input for queue_id set\n");
1750 	ret = -EPERM;
1751 	goto out;
1752 
1753 }
1754 
1755 static int bond_option_slaves_set(struct bonding *bond,
1756 				  const struct bond_opt_value *newval)
1757 {
1758 	char command[IFNAMSIZ + 1] = { 0, };
1759 	struct net_device *dev;
1760 	char *ifname;
1761 	int ret;
1762 
1763 	sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1764 	ifname = command + 1;
1765 	if ((strlen(command) <= 1) ||
1766 	    (command[0] != '+' && command[0] != '-') ||
1767 	    !dev_valid_name(ifname))
1768 		goto err_no_cmd;
1769 
1770 	dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1771 	if (!dev) {
1772 		netdev_dbg(bond->dev, "interface %s does not exist!\n",
1773 			   ifname);
1774 		ret = -ENODEV;
1775 		goto out;
1776 	}
1777 
1778 	switch (command[0]) {
1779 	case '+':
1780 		slave_dbg(bond->dev, dev, "Enslaving interface\n");
1781 		ret = bond_enslave(bond->dev, dev, NULL);
1782 		break;
1783 
1784 	case '-':
1785 		slave_dbg(bond->dev, dev, "Releasing interface\n");
1786 		ret = bond_release(bond->dev, dev);
1787 		break;
1788 
1789 	default:
1790 		/* should not run here. */
1791 		goto err_no_cmd;
1792 	}
1793 
1794 out:
1795 	return ret;
1796 
1797 err_no_cmd:
1798 	netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
1799 	ret = -EPERM;
1800 	goto out;
1801 }
1802 
1803 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
1804 					  const struct bond_opt_value *newval)
1805 {
1806 	netdev_dbg(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
1807 		   newval->string, newval->value);
1808 	bond->params.tlb_dynamic_lb = newval->value;
1809 
1810 	return 0;
1811 }
1812 
1813 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
1814 					     const struct bond_opt_value *newval)
1815 {
1816 	netdev_dbg(bond->dev, "Setting ad_actor_sys_prio to %llu\n",
1817 		   newval->value);
1818 
1819 	bond->params.ad_actor_sys_prio = newval->value;
1820 	bond_3ad_update_ad_actor_settings(bond);
1821 
1822 	return 0;
1823 }
1824 
1825 static int bond_option_actor_port_prio_set(struct bonding *bond,
1826 					   const struct bond_opt_value *newval)
1827 {
1828 	struct slave *slave;
1829 
1830 	slave = bond_slave_get_rtnl(newval->slave_dev);
1831 	if (!slave) {
1832 		netdev_dbg(bond->dev, "%s called on NULL slave\n", __func__);
1833 		return -ENODEV;
1834 	}
1835 
1836 	netdev_dbg(newval->slave_dev, "Setting actor_port_prio to %llu\n",
1837 		   newval->value);
1838 
1839 	SLAVE_AD_INFO(slave)->port_priority = newval->value;
1840 	bond_3ad_update_ad_actor_settings(bond);
1841 
1842 	return 0;
1843 }
1844 
1845 static int bond_option_ad_actor_system_set(struct bonding *bond,
1846 					   const struct bond_opt_value *newval)
1847 {
1848 	u8 macaddr[ETH_ALEN];
1849 	u8 *mac;
1850 
1851 	if (newval->string) {
1852 		if (!mac_pton(newval->string, macaddr))
1853 			goto err;
1854 		mac = macaddr;
1855 	} else {
1856 		mac = (u8 *)&newval->value;
1857 	}
1858 
1859 	if (is_multicast_ether_addr(mac))
1860 		goto err;
1861 
1862 	netdev_dbg(bond->dev, "Setting ad_actor_system to %pM\n", mac);
1863 	ether_addr_copy(bond->params.ad_actor_system, mac);
1864 	bond_3ad_update_ad_actor_settings(bond);
1865 
1866 	return 0;
1867 
1868 err:
1869 	netdev_err(bond->dev, "Invalid ad_actor_system MAC address.\n");
1870 	return -EINVAL;
1871 }
1872 
1873 static int bond_option_ad_user_port_key_set(struct bonding *bond,
1874 					    const struct bond_opt_value *newval)
1875 {
1876 	netdev_dbg(bond->dev, "Setting ad_user_port_key to %llu\n",
1877 		   newval->value);
1878 
1879 	bond->params.ad_user_port_key = newval->value;
1880 	return 0;
1881 }
1882 
1883 static int bond_option_coupled_control_set(struct bonding *bond,
1884 					   const struct bond_opt_value *newval)
1885 {
1886 	netdev_info(bond->dev, "Setting coupled_control to %s (%llu)\n",
1887 		    newval->string, newval->value);
1888 
1889 	bond->params.coupled_control = newval->value;
1890 	return 0;
1891 }
1892 
1893 static int bond_option_broadcast_neigh_set(struct bonding *bond,
1894 					   const struct bond_opt_value *newval)
1895 {
1896 	if (bond->params.broadcast_neighbor == newval->value)
1897 		return 0;
1898 
1899 	bond->params.broadcast_neighbor = newval->value;
1900 	if (bond->dev->flags & IFF_UP) {
1901 		if (bond->params.broadcast_neighbor)
1902 			static_branch_inc(&bond_bcast_neigh_enabled);
1903 		else
1904 			static_branch_dec(&bond_bcast_neigh_enabled);
1905 	}
1906 
1907 	netdev_dbg(bond->dev, "Setting broadcast_neighbor to %s (%llu)\n",
1908 		   newval->string, newval->value);
1909 	return 0;
1910 }
1911