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