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