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