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