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