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