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