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