1 /*
2  * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, see <http://www.gnu.org/licenses/>.
16  *
17  * The full GNU General Public License is included in this distribution in the
18  * file called LICENSE.
19  *
20  */
21 
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/sched/signal.h>
28 #include <linux/fs.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/netdevice.h>
32 #include <linux/inetdevice.h>
33 #include <linux/in.h>
34 #include <linux/sysfs.h>
35 #include <linux/ctype.h>
36 #include <linux/inet.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/etherdevice.h>
39 #include <net/net_namespace.h>
40 #include <net/netns/generic.h>
41 #include <linux/nsproxy.h>
42 
43 #include <net/bonding.h>
44 
45 #define to_bond(cd)	((struct bonding *)(netdev_priv(to_net_dev(cd))))
46 
47 /* "show" function for the bond_masters attribute.
48  * The class parameter is ignored.
49  */
bonding_show_bonds(struct class * cls,struct class_attribute * attr,char * buf)50 static ssize_t bonding_show_bonds(struct class *cls,
51 				  struct class_attribute *attr,
52 				  char *buf)
53 {
54 	struct bond_net *bn =
55 		container_of(attr, struct bond_net, class_attr_bonding_masters);
56 	int res = 0;
57 	struct bonding *bond;
58 
59 	rtnl_lock();
60 
61 	list_for_each_entry(bond, &bn->dev_list, bond_list) {
62 		if (res > (PAGE_SIZE - IFNAMSIZ)) {
63 			/* not enough space for another interface name */
64 			if ((PAGE_SIZE - res) > 10)
65 				res = PAGE_SIZE - 10;
66 			res += sprintf(buf + res, "++more++ ");
67 			break;
68 		}
69 		res += sprintf(buf + res, "%s ", bond->dev->name);
70 	}
71 	if (res)
72 		buf[res-1] = '\n'; /* eat the leftover space */
73 
74 	rtnl_unlock();
75 	return res;
76 }
77 
bond_get_by_name(struct bond_net * bn,const char * ifname)78 static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
79 {
80 	struct bonding *bond;
81 
82 	list_for_each_entry(bond, &bn->dev_list, bond_list) {
83 		if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
84 			return bond->dev;
85 	}
86 	return NULL;
87 }
88 
89 /* "store" function for the bond_masters attribute.  This is what
90  * creates and deletes entire bonds.
91  *
92  * The class parameter is ignored.
93  */
bonding_store_bonds(struct class * cls,struct class_attribute * attr,const char * buffer,size_t count)94 static ssize_t bonding_store_bonds(struct class *cls,
95 				   struct class_attribute *attr,
96 				   const char *buffer, size_t count)
97 {
98 	struct bond_net *bn =
99 		container_of(attr, struct bond_net, class_attr_bonding_masters);
100 	char command[IFNAMSIZ + 1] = {0, };
101 	char *ifname;
102 	int rv, res = count;
103 
104 	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
105 	ifname = command + 1;
106 	if ((strlen(command) <= 1) ||
107 	    !dev_valid_name(ifname))
108 		goto err_no_cmd;
109 
110 	if (command[0] == '+') {
111 		pr_info("%s is being created...\n", ifname);
112 		rv = bond_create(bn->net, ifname);
113 		if (rv) {
114 			if (rv == -EEXIST)
115 				pr_info("%s already exists\n", ifname);
116 			else
117 				pr_info("%s creation failed\n", ifname);
118 			res = rv;
119 		}
120 	} else if (command[0] == '-') {
121 		struct net_device *bond_dev;
122 
123 		rtnl_lock();
124 		bond_dev = bond_get_by_name(bn, ifname);
125 		if (bond_dev) {
126 			pr_info("%s is being deleted...\n", ifname);
127 			unregister_netdevice(bond_dev);
128 		} else {
129 			pr_err("unable to delete non-existent %s\n", ifname);
130 			res = -ENODEV;
131 		}
132 		rtnl_unlock();
133 	} else
134 		goto err_no_cmd;
135 
136 	/* Always return either count or an error.  If you return 0, you'll
137 	 * get called forever, which is bad.
138 	 */
139 	return res;
140 
141 err_no_cmd:
142 	pr_err("no command found in bonding_masters - use +ifname or -ifname\n");
143 	return -EPERM;
144 }
145 
146 /* class attribute for bond_masters file.  This ends up in /sys/class/net */
147 static const struct class_attribute class_attr_bonding_masters = {
148 	.attr = {
149 		.name = "bonding_masters",
150 		.mode = 0644,
151 	},
152 	.show = bonding_show_bonds,
153 	.store = bonding_store_bonds,
154 };
155 
156 /* Generic "store" method for bonding sysfs option setting */
bonding_sysfs_store_option(struct device * d,struct device_attribute * attr,const char * buffer,size_t count)157 static ssize_t bonding_sysfs_store_option(struct device *d,
158 					  struct device_attribute *attr,
159 					  const char *buffer, size_t count)
160 {
161 	struct bonding *bond = to_bond(d);
162 	const struct bond_option *opt;
163 	char *buffer_clone;
164 	int ret;
165 
166 	opt = bond_opt_get_by_name(attr->attr.name);
167 	if (WARN_ON(!opt))
168 		return -ENOENT;
169 	buffer_clone = kstrndup(buffer, count, GFP_KERNEL);
170 	if (!buffer_clone)
171 		return -ENOMEM;
172 	ret = bond_opt_tryset_rtnl(bond, opt->id, buffer_clone);
173 	if (!ret)
174 		ret = count;
175 	kfree(buffer_clone);
176 
177 	return ret;
178 }
179 
180 /* Show the slaves in the current bond. */
bonding_show_slaves(struct device * d,struct device_attribute * attr,char * buf)181 static ssize_t bonding_show_slaves(struct device *d,
182 				   struct device_attribute *attr, char *buf)
183 {
184 	struct bonding *bond = to_bond(d);
185 	struct list_head *iter;
186 	struct slave *slave;
187 	int res = 0;
188 
189 	if (!rtnl_trylock())
190 		return restart_syscall();
191 
192 	bond_for_each_slave(bond, slave, iter) {
193 		if (res > (PAGE_SIZE - IFNAMSIZ)) {
194 			/* not enough space for another interface name */
195 			if ((PAGE_SIZE - res) > 10)
196 				res = PAGE_SIZE - 10;
197 			res += sprintf(buf + res, "++more++ ");
198 			break;
199 		}
200 		res += sprintf(buf + res, "%s ", slave->dev->name);
201 	}
202 
203 	rtnl_unlock();
204 
205 	if (res)
206 		buf[res-1] = '\n'; /* eat the leftover space */
207 
208 	return res;
209 }
210 static DEVICE_ATTR(slaves, 0644, bonding_show_slaves,
211 		   bonding_sysfs_store_option);
212 
213 /* Show the bonding mode. */
bonding_show_mode(struct device * d,struct device_attribute * attr,char * buf)214 static ssize_t bonding_show_mode(struct device *d,
215 				 struct device_attribute *attr, char *buf)
216 {
217 	struct bonding *bond = to_bond(d);
218 	const struct bond_opt_value *val;
219 
220 	val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
221 
222 	return sprintf(buf, "%s %d\n", val->string, BOND_MODE(bond));
223 }
224 static DEVICE_ATTR(mode, 0644, bonding_show_mode, bonding_sysfs_store_option);
225 
226 /* Show the bonding transmit hash method. */
bonding_show_xmit_hash(struct device * d,struct device_attribute * attr,char * buf)227 static ssize_t bonding_show_xmit_hash(struct device *d,
228 				      struct device_attribute *attr,
229 				      char *buf)
230 {
231 	struct bonding *bond = to_bond(d);
232 	const struct bond_opt_value *val;
233 
234 	val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
235 
236 	return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
237 }
238 static DEVICE_ATTR(xmit_hash_policy, 0644,
239 		   bonding_show_xmit_hash, bonding_sysfs_store_option);
240 
241 /* Show arp_validate. */
bonding_show_arp_validate(struct device * d,struct device_attribute * attr,char * buf)242 static ssize_t bonding_show_arp_validate(struct device *d,
243 					 struct device_attribute *attr,
244 					 char *buf)
245 {
246 	struct bonding *bond = to_bond(d);
247 	const struct bond_opt_value *val;
248 
249 	val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
250 			       bond->params.arp_validate);
251 
252 	return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
253 }
254 static DEVICE_ATTR(arp_validate, 0644, bonding_show_arp_validate,
255 		   bonding_sysfs_store_option);
256 
257 /* Show arp_all_targets. */
bonding_show_arp_all_targets(struct device * d,struct device_attribute * attr,char * buf)258 static ssize_t bonding_show_arp_all_targets(struct device *d,
259 					 struct device_attribute *attr,
260 					 char *buf)
261 {
262 	struct bonding *bond = to_bond(d);
263 	const struct bond_opt_value *val;
264 
265 	val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
266 			       bond->params.arp_all_targets);
267 	return sprintf(buf, "%s %d\n",
268 		       val->string, bond->params.arp_all_targets);
269 }
270 static DEVICE_ATTR(arp_all_targets, 0644,
271 		   bonding_show_arp_all_targets, bonding_sysfs_store_option);
272 
273 /* Show fail_over_mac. */
bonding_show_fail_over_mac(struct device * d,struct device_attribute * attr,char * buf)274 static ssize_t bonding_show_fail_over_mac(struct device *d,
275 					  struct device_attribute *attr,
276 					  char *buf)
277 {
278 	struct bonding *bond = to_bond(d);
279 	const struct bond_opt_value *val;
280 
281 	val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
282 			       bond->params.fail_over_mac);
283 
284 	return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
285 }
286 static DEVICE_ATTR(fail_over_mac, 0644,
287 		   bonding_show_fail_over_mac, bonding_sysfs_store_option);
288 
289 /* Show the arp timer interval. */
bonding_show_arp_interval(struct device * d,struct device_attribute * attr,char * buf)290 static ssize_t bonding_show_arp_interval(struct device *d,
291 					 struct device_attribute *attr,
292 					 char *buf)
293 {
294 	struct bonding *bond = to_bond(d);
295 
296 	return sprintf(buf, "%d\n", bond->params.arp_interval);
297 }
298 static DEVICE_ATTR(arp_interval, 0644,
299 		   bonding_show_arp_interval, bonding_sysfs_store_option);
300 
301 /* Show the arp targets. */
bonding_show_arp_targets(struct device * d,struct device_attribute * attr,char * buf)302 static ssize_t bonding_show_arp_targets(struct device *d,
303 					struct device_attribute *attr,
304 					char *buf)
305 {
306 	struct bonding *bond = to_bond(d);
307 	int i, res = 0;
308 
309 	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
310 		if (bond->params.arp_targets[i])
311 			res += sprintf(buf + res, "%pI4 ",
312 				       &bond->params.arp_targets[i]);
313 	}
314 	if (res)
315 		buf[res-1] = '\n'; /* eat the leftover space */
316 
317 	return res;
318 }
319 static DEVICE_ATTR(arp_ip_target, 0644,
320 		   bonding_show_arp_targets, bonding_sysfs_store_option);
321 
322 /* Show the up and down delays. */
bonding_show_downdelay(struct device * d,struct device_attribute * attr,char * buf)323 static ssize_t bonding_show_downdelay(struct device *d,
324 				      struct device_attribute *attr,
325 				      char *buf)
326 {
327 	struct bonding *bond = to_bond(d);
328 
329 	return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
330 }
331 static DEVICE_ATTR(downdelay, 0644,
332 		   bonding_show_downdelay, bonding_sysfs_store_option);
333 
bonding_show_updelay(struct device * d,struct device_attribute * attr,char * buf)334 static ssize_t bonding_show_updelay(struct device *d,
335 				    struct device_attribute *attr,
336 				    char *buf)
337 {
338 	struct bonding *bond = to_bond(d);
339 
340 	return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
341 
342 }
343 static DEVICE_ATTR(updelay, 0644,
344 		   bonding_show_updelay, bonding_sysfs_store_option);
345 
346 /* Show the LACP interval. */
bonding_show_lacp(struct device * d,struct device_attribute * attr,char * buf)347 static ssize_t bonding_show_lacp(struct device *d,
348 				 struct device_attribute *attr,
349 				 char *buf)
350 {
351 	struct bonding *bond = to_bond(d);
352 	const struct bond_opt_value *val;
353 
354 	val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);
355 
356 	return sprintf(buf, "%s %d\n", val->string, bond->params.lacp_fast);
357 }
358 static DEVICE_ATTR(lacp_rate, 0644,
359 		   bonding_show_lacp, bonding_sysfs_store_option);
360 
bonding_show_min_links(struct device * d,struct device_attribute * attr,char * buf)361 static ssize_t bonding_show_min_links(struct device *d,
362 				      struct device_attribute *attr,
363 				      char *buf)
364 {
365 	struct bonding *bond = to_bond(d);
366 
367 	return sprintf(buf, "%u\n", bond->params.min_links);
368 }
369 static DEVICE_ATTR(min_links, 0644,
370 		   bonding_show_min_links, bonding_sysfs_store_option);
371 
bonding_show_ad_select(struct device * d,struct device_attribute * attr,char * buf)372 static ssize_t bonding_show_ad_select(struct device *d,
373 				      struct device_attribute *attr,
374 				      char *buf)
375 {
376 	struct bonding *bond = to_bond(d);
377 	const struct bond_opt_value *val;
378 
379 	val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
380 
381 	return sprintf(buf, "%s %d\n", val->string, bond->params.ad_select);
382 }
383 static DEVICE_ATTR(ad_select, 0644,
384 		   bonding_show_ad_select, bonding_sysfs_store_option);
385 
386 /* Show the number of peer notifications to send after a failover event. */
bonding_show_num_peer_notif(struct device * d,struct device_attribute * attr,char * buf)387 static ssize_t bonding_show_num_peer_notif(struct device *d,
388 					   struct device_attribute *attr,
389 					   char *buf)
390 {
391 	struct bonding *bond = to_bond(d);
392 	return sprintf(buf, "%d\n", bond->params.num_peer_notif);
393 }
394 static DEVICE_ATTR(num_grat_arp, 0644,
395 		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
396 static DEVICE_ATTR(num_unsol_na, 0644,
397 		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
398 
399 /* Show the MII monitor interval. */
bonding_show_miimon(struct device * d,struct device_attribute * attr,char * buf)400 static ssize_t bonding_show_miimon(struct device *d,
401 				   struct device_attribute *attr,
402 				   char *buf)
403 {
404 	struct bonding *bond = to_bond(d);
405 
406 	return sprintf(buf, "%d\n", bond->params.miimon);
407 }
408 static DEVICE_ATTR(miimon, 0644,
409 		   bonding_show_miimon, bonding_sysfs_store_option);
410 
411 /* Show the primary slave. */
bonding_show_primary(struct device * d,struct device_attribute * attr,char * buf)412 static ssize_t bonding_show_primary(struct device *d,
413 				    struct device_attribute *attr,
414 				    char *buf)
415 {
416 	struct bonding *bond = to_bond(d);
417 	struct slave *primary;
418 	int count = 0;
419 
420 	rcu_read_lock();
421 	primary = rcu_dereference(bond->primary_slave);
422 	if (primary)
423 		count = sprintf(buf, "%s\n", primary->dev->name);
424 	rcu_read_unlock();
425 
426 	return count;
427 }
428 static DEVICE_ATTR(primary, 0644,
429 		   bonding_show_primary, bonding_sysfs_store_option);
430 
431 /* Show the primary_reselect flag. */
bonding_show_primary_reselect(struct device * d,struct device_attribute * attr,char * buf)432 static ssize_t bonding_show_primary_reselect(struct device *d,
433 					     struct device_attribute *attr,
434 					     char *buf)
435 {
436 	struct bonding *bond = to_bond(d);
437 	const struct bond_opt_value *val;
438 
439 	val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
440 			       bond->params.primary_reselect);
441 
442 	return sprintf(buf, "%s %d\n",
443 		       val->string, bond->params.primary_reselect);
444 }
445 static DEVICE_ATTR(primary_reselect, 0644,
446 		   bonding_show_primary_reselect, bonding_sysfs_store_option);
447 
448 /* Show the use_carrier flag. */
bonding_show_carrier(struct device * d,struct device_attribute * attr,char * buf)449 static ssize_t bonding_show_carrier(struct device *d,
450 				    struct device_attribute *attr,
451 				    char *buf)
452 {
453 	struct bonding *bond = to_bond(d);
454 
455 	return sprintf(buf, "%d\n", bond->params.use_carrier);
456 }
457 static DEVICE_ATTR(use_carrier, 0644,
458 		   bonding_show_carrier, bonding_sysfs_store_option);
459 
460 
461 /* Show currently active_slave. */
bonding_show_active_slave(struct device * d,struct device_attribute * attr,char * buf)462 static ssize_t bonding_show_active_slave(struct device *d,
463 					 struct device_attribute *attr,
464 					 char *buf)
465 {
466 	struct bonding *bond = to_bond(d);
467 	struct net_device *slave_dev;
468 	int count = 0;
469 
470 	rcu_read_lock();
471 	slave_dev = bond_option_active_slave_get_rcu(bond);
472 	if (slave_dev)
473 		count = sprintf(buf, "%s\n", slave_dev->name);
474 	rcu_read_unlock();
475 
476 	return count;
477 }
478 static DEVICE_ATTR(active_slave, 0644,
479 		   bonding_show_active_slave, bonding_sysfs_store_option);
480 
481 /* Show link status of the bond interface. */
bonding_show_mii_status(struct device * d,struct device_attribute * attr,char * buf)482 static ssize_t bonding_show_mii_status(struct device *d,
483 				       struct device_attribute *attr,
484 				       char *buf)
485 {
486 	struct bonding *bond = to_bond(d);
487 	bool active = netif_carrier_ok(bond->dev);
488 
489 	return sprintf(buf, "%s\n", active ? "up" : "down");
490 }
491 static DEVICE_ATTR(mii_status, 0444, bonding_show_mii_status, NULL);
492 
493 /* Show current 802.3ad aggregator ID. */
bonding_show_ad_aggregator(struct device * d,struct device_attribute * attr,char * buf)494 static ssize_t bonding_show_ad_aggregator(struct device *d,
495 					  struct device_attribute *attr,
496 					  char *buf)
497 {
498 	int count = 0;
499 	struct bonding *bond = to_bond(d);
500 
501 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
502 		struct ad_info ad_info;
503 		count = sprintf(buf, "%d\n",
504 				bond_3ad_get_active_agg_info(bond, &ad_info)
505 				?  0 : ad_info.aggregator_id);
506 	}
507 
508 	return count;
509 }
510 static DEVICE_ATTR(ad_aggregator, 0444, bonding_show_ad_aggregator, NULL);
511 
512 
513 /* Show number of active 802.3ad ports. */
bonding_show_ad_num_ports(struct device * d,struct device_attribute * attr,char * buf)514 static ssize_t bonding_show_ad_num_ports(struct device *d,
515 					 struct device_attribute *attr,
516 					 char *buf)
517 {
518 	int count = 0;
519 	struct bonding *bond = to_bond(d);
520 
521 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
522 		struct ad_info ad_info;
523 		count = sprintf(buf, "%d\n",
524 				bond_3ad_get_active_agg_info(bond, &ad_info)
525 				?  0 : ad_info.ports);
526 	}
527 
528 	return count;
529 }
530 static DEVICE_ATTR(ad_num_ports, 0444, bonding_show_ad_num_ports, NULL);
531 
532 
533 /* Show current 802.3ad actor key. */
bonding_show_ad_actor_key(struct device * d,struct device_attribute * attr,char * buf)534 static ssize_t bonding_show_ad_actor_key(struct device *d,
535 					 struct device_attribute *attr,
536 					 char *buf)
537 {
538 	int count = 0;
539 	struct bonding *bond = to_bond(d);
540 
541 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
542 		struct ad_info ad_info;
543 		count = sprintf(buf, "%d\n",
544 				bond_3ad_get_active_agg_info(bond, &ad_info)
545 				?  0 : ad_info.actor_key);
546 	}
547 
548 	return count;
549 }
550 static DEVICE_ATTR(ad_actor_key, 0444, bonding_show_ad_actor_key, NULL);
551 
552 
553 /* Show current 802.3ad partner key. */
bonding_show_ad_partner_key(struct device * d,struct device_attribute * attr,char * buf)554 static ssize_t bonding_show_ad_partner_key(struct device *d,
555 					   struct device_attribute *attr,
556 					   char *buf)
557 {
558 	int count = 0;
559 	struct bonding *bond = to_bond(d);
560 
561 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
562 		struct ad_info ad_info;
563 		count = sprintf(buf, "%d\n",
564 				bond_3ad_get_active_agg_info(bond, &ad_info)
565 				?  0 : ad_info.partner_key);
566 	}
567 
568 	return count;
569 }
570 static DEVICE_ATTR(ad_partner_key, 0444, bonding_show_ad_partner_key, NULL);
571 
572 
573 /* Show current 802.3ad partner mac. */
bonding_show_ad_partner_mac(struct device * d,struct device_attribute * attr,char * buf)574 static ssize_t bonding_show_ad_partner_mac(struct device *d,
575 					   struct device_attribute *attr,
576 					   char *buf)
577 {
578 	int count = 0;
579 	struct bonding *bond = to_bond(d);
580 
581 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
582 		struct ad_info ad_info;
583 		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
584 			count = sprintf(buf, "%pM\n", ad_info.partner_system);
585 	}
586 
587 	return count;
588 }
589 static DEVICE_ATTR(ad_partner_mac, 0444, bonding_show_ad_partner_mac, NULL);
590 
591 /* Show the queue_ids of the slaves in the current bond. */
bonding_show_queue_id(struct device * d,struct device_attribute * attr,char * buf)592 static ssize_t bonding_show_queue_id(struct device *d,
593 				     struct device_attribute *attr,
594 				     char *buf)
595 {
596 	struct bonding *bond = to_bond(d);
597 	struct list_head *iter;
598 	struct slave *slave;
599 	int res = 0;
600 
601 	if (!rtnl_trylock())
602 		return restart_syscall();
603 
604 	bond_for_each_slave(bond, slave, iter) {
605 		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
606 			/* not enough space for another interface_name:queue_id pair */
607 			if ((PAGE_SIZE - res) > 10)
608 				res = PAGE_SIZE - 10;
609 			res += sprintf(buf + res, "++more++ ");
610 			break;
611 		}
612 		res += sprintf(buf + res, "%s:%d ",
613 			       slave->dev->name, slave->queue_id);
614 	}
615 	if (res)
616 		buf[res-1] = '\n'; /* eat the leftover space */
617 
618 	rtnl_unlock();
619 
620 	return res;
621 }
622 static DEVICE_ATTR(queue_id, 0644, bonding_show_queue_id,
623 		   bonding_sysfs_store_option);
624 
625 
626 /* Show the all_slaves_active flag. */
bonding_show_slaves_active(struct device * d,struct device_attribute * attr,char * buf)627 static ssize_t bonding_show_slaves_active(struct device *d,
628 					  struct device_attribute *attr,
629 					  char *buf)
630 {
631 	struct bonding *bond = to_bond(d);
632 
633 	return sprintf(buf, "%d\n", bond->params.all_slaves_active);
634 }
635 static DEVICE_ATTR(all_slaves_active, 0644,
636 		   bonding_show_slaves_active, bonding_sysfs_store_option);
637 
638 /* Show the number of IGMP membership reports to send on link failure */
bonding_show_resend_igmp(struct device * d,struct device_attribute * attr,char * buf)639 static ssize_t bonding_show_resend_igmp(struct device *d,
640 					struct device_attribute *attr,
641 					char *buf)
642 {
643 	struct bonding *bond = to_bond(d);
644 
645 	return sprintf(buf, "%d\n", bond->params.resend_igmp);
646 }
647 static DEVICE_ATTR(resend_igmp, 0644,
648 		   bonding_show_resend_igmp, bonding_sysfs_store_option);
649 
650 
bonding_show_lp_interval(struct device * d,struct device_attribute * attr,char * buf)651 static ssize_t bonding_show_lp_interval(struct device *d,
652 					struct device_attribute *attr,
653 					char *buf)
654 {
655 	struct bonding *bond = to_bond(d);
656 
657 	return sprintf(buf, "%d\n", bond->params.lp_interval);
658 }
659 static DEVICE_ATTR(lp_interval, 0644,
660 		   bonding_show_lp_interval, bonding_sysfs_store_option);
661 
bonding_show_tlb_dynamic_lb(struct device * d,struct device_attribute * attr,char * buf)662 static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
663 					   struct device_attribute *attr,
664 					   char *buf)
665 {
666 	struct bonding *bond = to_bond(d);
667 	return sprintf(buf, "%d\n", bond->params.tlb_dynamic_lb);
668 }
669 static DEVICE_ATTR(tlb_dynamic_lb, 0644,
670 		   bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
671 
bonding_show_packets_per_slave(struct device * d,struct device_attribute * attr,char * buf)672 static ssize_t bonding_show_packets_per_slave(struct device *d,
673 					      struct device_attribute *attr,
674 					      char *buf)
675 {
676 	struct bonding *bond = to_bond(d);
677 	unsigned int packets_per_slave = bond->params.packets_per_slave;
678 
679 	return sprintf(buf, "%u\n", packets_per_slave);
680 }
681 static DEVICE_ATTR(packets_per_slave, 0644,
682 		   bonding_show_packets_per_slave, bonding_sysfs_store_option);
683 
bonding_show_ad_actor_sys_prio(struct device * d,struct device_attribute * attr,char * buf)684 static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
685 					      struct device_attribute *attr,
686 					      char *buf)
687 {
688 	struct bonding *bond = to_bond(d);
689 
690 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
691 		return sprintf(buf, "%hu\n", bond->params.ad_actor_sys_prio);
692 
693 	return 0;
694 }
695 static DEVICE_ATTR(ad_actor_sys_prio, 0644,
696 		   bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
697 
bonding_show_ad_actor_system(struct device * d,struct device_attribute * attr,char * buf)698 static ssize_t bonding_show_ad_actor_system(struct device *d,
699 					    struct device_attribute *attr,
700 					    char *buf)
701 {
702 	struct bonding *bond = to_bond(d);
703 
704 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
705 		return sprintf(buf, "%pM\n", bond->params.ad_actor_system);
706 
707 	return 0;
708 }
709 
710 static DEVICE_ATTR(ad_actor_system, 0644,
711 		   bonding_show_ad_actor_system, bonding_sysfs_store_option);
712 
bonding_show_ad_user_port_key(struct device * d,struct device_attribute * attr,char * buf)713 static ssize_t bonding_show_ad_user_port_key(struct device *d,
714 					     struct device_attribute *attr,
715 					     char *buf)
716 {
717 	struct bonding *bond = to_bond(d);
718 
719 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
720 		return sprintf(buf, "%hu\n", bond->params.ad_user_port_key);
721 
722 	return 0;
723 }
724 static DEVICE_ATTR(ad_user_port_key, 0644,
725 		   bonding_show_ad_user_port_key, bonding_sysfs_store_option);
726 
727 static struct attribute *per_bond_attrs[] = {
728 	&dev_attr_slaves.attr,
729 	&dev_attr_mode.attr,
730 	&dev_attr_fail_over_mac.attr,
731 	&dev_attr_arp_validate.attr,
732 	&dev_attr_arp_all_targets.attr,
733 	&dev_attr_arp_interval.attr,
734 	&dev_attr_arp_ip_target.attr,
735 	&dev_attr_downdelay.attr,
736 	&dev_attr_updelay.attr,
737 	&dev_attr_lacp_rate.attr,
738 	&dev_attr_ad_select.attr,
739 	&dev_attr_xmit_hash_policy.attr,
740 	&dev_attr_num_grat_arp.attr,
741 	&dev_attr_num_unsol_na.attr,
742 	&dev_attr_miimon.attr,
743 	&dev_attr_primary.attr,
744 	&dev_attr_primary_reselect.attr,
745 	&dev_attr_use_carrier.attr,
746 	&dev_attr_active_slave.attr,
747 	&dev_attr_mii_status.attr,
748 	&dev_attr_ad_aggregator.attr,
749 	&dev_attr_ad_num_ports.attr,
750 	&dev_attr_ad_actor_key.attr,
751 	&dev_attr_ad_partner_key.attr,
752 	&dev_attr_ad_partner_mac.attr,
753 	&dev_attr_queue_id.attr,
754 	&dev_attr_all_slaves_active.attr,
755 	&dev_attr_resend_igmp.attr,
756 	&dev_attr_min_links.attr,
757 	&dev_attr_lp_interval.attr,
758 	&dev_attr_packets_per_slave.attr,
759 	&dev_attr_tlb_dynamic_lb.attr,
760 	&dev_attr_ad_actor_sys_prio.attr,
761 	&dev_attr_ad_actor_system.attr,
762 	&dev_attr_ad_user_port_key.attr,
763 	NULL,
764 };
765 
766 static const struct attribute_group bonding_group = {
767 	.name = "bonding",
768 	.attrs = per_bond_attrs,
769 };
770 
771 /* Initialize sysfs.  This sets up the bonding_masters file in
772  * /sys/class/net.
773  */
bond_create_sysfs(struct bond_net * bn)774 int bond_create_sysfs(struct bond_net *bn)
775 {
776 	int ret;
777 
778 	bn->class_attr_bonding_masters = class_attr_bonding_masters;
779 	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
780 
781 	ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
782 					  bn->net);
783 	/* Permit multiple loads of the module by ignoring failures to
784 	 * create the bonding_masters sysfs file.  Bonding devices
785 	 * created by second or subsequent loads of the module will
786 	 * not be listed in, or controllable by, bonding_masters, but
787 	 * will have the usual "bonding" sysfs directory.
788 	 *
789 	 * This is done to preserve backwards compatibility for
790 	 * initscripts/sysconfig, which load bonding multiple times to
791 	 * configure multiple bonding devices.
792 	 */
793 	if (ret == -EEXIST) {
794 		/* Is someone being kinky and naming a device bonding_master? */
795 		if (__dev_get_by_name(bn->net,
796 				      class_attr_bonding_masters.attr.name))
797 			pr_err("network device named %s already exists in sysfs\n",
798 			       class_attr_bonding_masters.attr.name);
799 		ret = 0;
800 	}
801 
802 	return ret;
803 
804 }
805 
806 /* Remove /sys/class/net/bonding_masters. */
bond_destroy_sysfs(struct bond_net * bn)807 void bond_destroy_sysfs(struct bond_net *bn)
808 {
809 	netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
810 }
811 
812 /* Initialize sysfs for each bond.  This sets up and registers
813  * the 'bondctl' directory for each individual bond under /sys/class/net.
814  */
bond_prepare_sysfs_group(struct bonding * bond)815 void bond_prepare_sysfs_group(struct bonding *bond)
816 {
817 	bond->dev->sysfs_groups[0] = &bonding_group;
818 }
819 
820