xref: /openbmc/linux/net/bridge/br_sysfs_br.c (revision 9b93eb47)
1 /*
2  *	Sysfs attributes of bridge
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Stephen Hemminger		<shemminger@osdl.org>
7  *
8  *	This program is free software; you can redistribute it and/or
9  *	modify it under the terms of the GNU General Public License
10  *	as published by the Free Software Foundation; either version
11  *	2 of the License, or (at your option) any later version.
12  */
13 
14 #include <linux/capability.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/if_bridge.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/spinlock.h>
21 #include <linux/times.h>
22 #include <linux/sched/signal.h>
23 
24 #include "br_private.h"
25 
26 #define to_bridge(cd)	((struct net_bridge *)netdev_priv(to_net_dev(cd)))
27 
28 /*
29  * Common code for storing bridge parameters.
30  */
31 static ssize_t store_bridge_parm(struct device *d,
32 				 const char *buf, size_t len,
33 				 int (*set)(struct net_bridge *, unsigned long))
34 {
35 	struct net_bridge *br = to_bridge(d);
36 	char *endp;
37 	unsigned long val;
38 	int err;
39 
40 	if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
41 		return -EPERM;
42 
43 	val = simple_strtoul(buf, &endp, 0);
44 	if (endp == buf)
45 		return -EINVAL;
46 
47 	if (!rtnl_trylock())
48 		return restart_syscall();
49 
50 	err = (*set)(br, val);
51 	if (!err)
52 		netdev_state_change(br->dev);
53 	rtnl_unlock();
54 
55 	return err ? err : len;
56 }
57 
58 
59 static ssize_t forward_delay_show(struct device *d,
60 				  struct device_attribute *attr, char *buf)
61 {
62 	struct net_bridge *br = to_bridge(d);
63 	return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
64 }
65 
66 static ssize_t forward_delay_store(struct device *d,
67 				   struct device_attribute *attr,
68 				   const char *buf, size_t len)
69 {
70 	return store_bridge_parm(d, buf, len, br_set_forward_delay);
71 }
72 static DEVICE_ATTR_RW(forward_delay);
73 
74 static ssize_t hello_time_show(struct device *d, struct device_attribute *attr,
75 			       char *buf)
76 {
77 	return sprintf(buf, "%lu\n",
78 		       jiffies_to_clock_t(to_bridge(d)->hello_time));
79 }
80 
81 static ssize_t hello_time_store(struct device *d,
82 				struct device_attribute *attr, const char *buf,
83 				size_t len)
84 {
85 	return store_bridge_parm(d, buf, len, br_set_hello_time);
86 }
87 static DEVICE_ATTR_RW(hello_time);
88 
89 static ssize_t max_age_show(struct device *d, struct device_attribute *attr,
90 			    char *buf)
91 {
92 	return sprintf(buf, "%lu\n",
93 		       jiffies_to_clock_t(to_bridge(d)->max_age));
94 }
95 
96 static ssize_t max_age_store(struct device *d, struct device_attribute *attr,
97 			     const char *buf, size_t len)
98 {
99 	return store_bridge_parm(d, buf, len, br_set_max_age);
100 }
101 static DEVICE_ATTR_RW(max_age);
102 
103 static ssize_t ageing_time_show(struct device *d,
104 				struct device_attribute *attr, char *buf)
105 {
106 	struct net_bridge *br = to_bridge(d);
107 	return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
108 }
109 
110 static int set_ageing_time(struct net_bridge *br, unsigned long val)
111 {
112 	return br_set_ageing_time(br, val);
113 }
114 
115 static ssize_t ageing_time_store(struct device *d,
116 				 struct device_attribute *attr,
117 				 const char *buf, size_t len)
118 {
119 	return store_bridge_parm(d, buf, len, set_ageing_time);
120 }
121 static DEVICE_ATTR_RW(ageing_time);
122 
123 static ssize_t stp_state_show(struct device *d,
124 			      struct device_attribute *attr, char *buf)
125 {
126 	struct net_bridge *br = to_bridge(d);
127 	return sprintf(buf, "%d\n", br->stp_enabled);
128 }
129 
130 
131 static int set_stp_state(struct net_bridge *br, unsigned long val)
132 {
133 	br_stp_set_enabled(br, val);
134 
135 	return 0;
136 }
137 
138 static ssize_t stp_state_store(struct device *d,
139 			       struct device_attribute *attr, const char *buf,
140 			       size_t len)
141 {
142 	return store_bridge_parm(d, buf, len, set_stp_state);
143 }
144 static DEVICE_ATTR_RW(stp_state);
145 
146 static ssize_t group_fwd_mask_show(struct device *d,
147 				   struct device_attribute *attr,
148 				   char *buf)
149 {
150 	struct net_bridge *br = to_bridge(d);
151 	return sprintf(buf, "%#x\n", br->group_fwd_mask);
152 }
153 
154 static int set_group_fwd_mask(struct net_bridge *br, unsigned long val)
155 {
156 	if (val & BR_GROUPFWD_RESTRICTED)
157 		return -EINVAL;
158 
159 	br->group_fwd_mask = val;
160 
161 	return 0;
162 }
163 
164 static ssize_t group_fwd_mask_store(struct device *d,
165 				    struct device_attribute *attr,
166 				    const char *buf,
167 				    size_t len)
168 {
169 	return store_bridge_parm(d, buf, len, set_group_fwd_mask);
170 }
171 static DEVICE_ATTR_RW(group_fwd_mask);
172 
173 static ssize_t priority_show(struct device *d, struct device_attribute *attr,
174 			     char *buf)
175 {
176 	struct net_bridge *br = to_bridge(d);
177 	return sprintf(buf, "%d\n",
178 		       (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
179 }
180 
181 static int set_priority(struct net_bridge *br, unsigned long val)
182 {
183 	br_stp_set_bridge_priority(br, (u16) val);
184 	return 0;
185 }
186 
187 static ssize_t priority_store(struct device *d, struct device_attribute *attr,
188 			      const char *buf, size_t len)
189 {
190 	return store_bridge_parm(d, buf, len, set_priority);
191 }
192 static DEVICE_ATTR_RW(priority);
193 
194 static ssize_t root_id_show(struct device *d, struct device_attribute *attr,
195 			    char *buf)
196 {
197 	return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
198 }
199 static DEVICE_ATTR_RO(root_id);
200 
201 static ssize_t bridge_id_show(struct device *d, struct device_attribute *attr,
202 			      char *buf)
203 {
204 	return br_show_bridge_id(buf, &to_bridge(d)->bridge_id);
205 }
206 static DEVICE_ATTR_RO(bridge_id);
207 
208 static ssize_t root_port_show(struct device *d, struct device_attribute *attr,
209 			      char *buf)
210 {
211 	return sprintf(buf, "%d\n", to_bridge(d)->root_port);
212 }
213 static DEVICE_ATTR_RO(root_port);
214 
215 static ssize_t root_path_cost_show(struct device *d,
216 				   struct device_attribute *attr, char *buf)
217 {
218 	return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
219 }
220 static DEVICE_ATTR_RO(root_path_cost);
221 
222 static ssize_t topology_change_show(struct device *d,
223 				    struct device_attribute *attr, char *buf)
224 {
225 	return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
226 }
227 static DEVICE_ATTR_RO(topology_change);
228 
229 static ssize_t topology_change_detected_show(struct device *d,
230 					     struct device_attribute *attr,
231 					     char *buf)
232 {
233 	struct net_bridge *br = to_bridge(d);
234 	return sprintf(buf, "%d\n", br->topology_change_detected);
235 }
236 static DEVICE_ATTR_RO(topology_change_detected);
237 
238 static ssize_t hello_timer_show(struct device *d,
239 				struct device_attribute *attr, char *buf)
240 {
241 	struct net_bridge *br = to_bridge(d);
242 	return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
243 }
244 static DEVICE_ATTR_RO(hello_timer);
245 
246 static ssize_t tcn_timer_show(struct device *d, struct device_attribute *attr,
247 			      char *buf)
248 {
249 	struct net_bridge *br = to_bridge(d);
250 	return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
251 }
252 static DEVICE_ATTR_RO(tcn_timer);
253 
254 static ssize_t topology_change_timer_show(struct device *d,
255 					  struct device_attribute *attr,
256 					  char *buf)
257 {
258 	struct net_bridge *br = to_bridge(d);
259 	return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
260 }
261 static DEVICE_ATTR_RO(topology_change_timer);
262 
263 static ssize_t gc_timer_show(struct device *d, struct device_attribute *attr,
264 			     char *buf)
265 {
266 	struct net_bridge *br = to_bridge(d);
267 	return sprintf(buf, "%ld\n", br_timer_value(&br->gc_work.timer));
268 }
269 static DEVICE_ATTR_RO(gc_timer);
270 
271 static ssize_t group_addr_show(struct device *d,
272 			       struct device_attribute *attr, char *buf)
273 {
274 	struct net_bridge *br = to_bridge(d);
275 	return sprintf(buf, "%pM\n", br->group_addr);
276 }
277 
278 static ssize_t group_addr_store(struct device *d,
279 				struct device_attribute *attr,
280 				const char *buf, size_t len)
281 {
282 	struct net_bridge *br = to_bridge(d);
283 	u8 new_addr[6];
284 
285 	if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
286 		return -EPERM;
287 
288 	if (!mac_pton(buf, new_addr))
289 		return -EINVAL;
290 
291 	if (!is_link_local_ether_addr(new_addr))
292 		return -EINVAL;
293 
294 	if (new_addr[5] == 1 ||		/* 802.3x Pause address */
295 	    new_addr[5] == 2 ||		/* 802.3ad Slow protocols */
296 	    new_addr[5] == 3)		/* 802.1X PAE address */
297 		return -EINVAL;
298 
299 	if (!rtnl_trylock())
300 		return restart_syscall();
301 
302 	spin_lock_bh(&br->lock);
303 	ether_addr_copy(br->group_addr, new_addr);
304 	spin_unlock_bh(&br->lock);
305 
306 	br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
307 	br_recalculate_fwd_mask(br);
308 	netdev_state_change(br->dev);
309 
310 	rtnl_unlock();
311 
312 	return len;
313 }
314 
315 static DEVICE_ATTR_RW(group_addr);
316 
317 static int set_flush(struct net_bridge *br, unsigned long val)
318 {
319 	br_fdb_flush(br);
320 	return 0;
321 }
322 
323 static ssize_t flush_store(struct device *d,
324 			   struct device_attribute *attr,
325 			   const char *buf, size_t len)
326 {
327 	return store_bridge_parm(d, buf, len, set_flush);
328 }
329 static DEVICE_ATTR_WO(flush);
330 
331 static ssize_t no_linklocal_learn_show(struct device *d,
332 				       struct device_attribute *attr,
333 				       char *buf)
334 {
335 	struct net_bridge *br = to_bridge(d);
336 	return sprintf(buf, "%d\n", br_boolopt_get(br, BR_BOOLOPT_NO_LL_LEARN));
337 }
338 
339 static int set_no_linklocal_learn(struct net_bridge *br, unsigned long val)
340 {
341 	return br_boolopt_toggle(br, BR_BOOLOPT_NO_LL_LEARN, !!val, NULL);
342 }
343 
344 static ssize_t no_linklocal_learn_store(struct device *d,
345 					struct device_attribute *attr,
346 					const char *buf, size_t len)
347 {
348 	return store_bridge_parm(d, buf, len, set_no_linklocal_learn);
349 }
350 static DEVICE_ATTR_RW(no_linklocal_learn);
351 
352 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
353 static ssize_t multicast_router_show(struct device *d,
354 				     struct device_attribute *attr, char *buf)
355 {
356 	struct net_bridge *br = to_bridge(d);
357 	return sprintf(buf, "%d\n", br->multicast_router);
358 }
359 
360 static ssize_t multicast_router_store(struct device *d,
361 				      struct device_attribute *attr,
362 				      const char *buf, size_t len)
363 {
364 	return store_bridge_parm(d, buf, len, br_multicast_set_router);
365 }
366 static DEVICE_ATTR_RW(multicast_router);
367 
368 static ssize_t multicast_snooping_show(struct device *d,
369 				       struct device_attribute *attr,
370 				       char *buf)
371 {
372 	struct net_bridge *br = to_bridge(d);
373 	return sprintf(buf, "%d\n", br_opt_get(br, BROPT_MULTICAST_ENABLED));
374 }
375 
376 static ssize_t multicast_snooping_store(struct device *d,
377 					struct device_attribute *attr,
378 					const char *buf, size_t len)
379 {
380 	return store_bridge_parm(d, buf, len, br_multicast_toggle);
381 }
382 static DEVICE_ATTR_RW(multicast_snooping);
383 
384 static ssize_t multicast_query_use_ifaddr_show(struct device *d,
385 					       struct device_attribute *attr,
386 					       char *buf)
387 {
388 	struct net_bridge *br = to_bridge(d);
389 	return sprintf(buf, "%d\n",
390 		       br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR));
391 }
392 
393 static int set_query_use_ifaddr(struct net_bridge *br, unsigned long val)
394 {
395 	br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
396 	return 0;
397 }
398 
399 static ssize_t
400 multicast_query_use_ifaddr_store(struct device *d,
401 				 struct device_attribute *attr,
402 				 const char *buf, size_t len)
403 {
404 	return store_bridge_parm(d, buf, len, set_query_use_ifaddr);
405 }
406 static DEVICE_ATTR_RW(multicast_query_use_ifaddr);
407 
408 static ssize_t multicast_querier_show(struct device *d,
409 				      struct device_attribute *attr,
410 				      char *buf)
411 {
412 	struct net_bridge *br = to_bridge(d);
413 	return sprintf(buf, "%d\n", br_opt_get(br, BROPT_MULTICAST_QUERIER));
414 }
415 
416 static ssize_t multicast_querier_store(struct device *d,
417 				       struct device_attribute *attr,
418 				       const char *buf, size_t len)
419 {
420 	return store_bridge_parm(d, buf, len, br_multicast_set_querier);
421 }
422 static DEVICE_ATTR_RW(multicast_querier);
423 
424 static ssize_t hash_elasticity_show(struct device *d,
425 				    struct device_attribute *attr, char *buf)
426 {
427 	return sprintf(buf, "%u\n", RHT_ELASTICITY);
428 }
429 
430 static int set_elasticity(struct net_bridge *br, unsigned long val)
431 {
432 	br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
433 		RHT_ELASTICITY);
434 	return 0;
435 }
436 
437 static ssize_t hash_elasticity_store(struct device *d,
438 				     struct device_attribute *attr,
439 				     const char *buf, size_t len)
440 {
441 	return store_bridge_parm(d, buf, len, set_elasticity);
442 }
443 static DEVICE_ATTR_RW(hash_elasticity);
444 
445 static ssize_t hash_max_show(struct device *d, struct device_attribute *attr,
446 			     char *buf)
447 {
448 	struct net_bridge *br = to_bridge(d);
449 	return sprintf(buf, "%u\n", br->hash_max);
450 }
451 
452 static int set_hash_max(struct net_bridge *br, unsigned long val)
453 {
454 	br->hash_max = val;
455 	return 0;
456 }
457 
458 static ssize_t hash_max_store(struct device *d, struct device_attribute *attr,
459 			      const char *buf, size_t len)
460 {
461 	return store_bridge_parm(d, buf, len, set_hash_max);
462 }
463 static DEVICE_ATTR_RW(hash_max);
464 
465 static ssize_t multicast_igmp_version_show(struct device *d,
466 					   struct device_attribute *attr,
467 					   char *buf)
468 {
469 	struct net_bridge *br = to_bridge(d);
470 
471 	return sprintf(buf, "%u\n", br->multicast_igmp_version);
472 }
473 
474 static ssize_t multicast_igmp_version_store(struct device *d,
475 					    struct device_attribute *attr,
476 					    const char *buf, size_t len)
477 {
478 	return store_bridge_parm(d, buf, len, br_multicast_set_igmp_version);
479 }
480 static DEVICE_ATTR_RW(multicast_igmp_version);
481 
482 static ssize_t multicast_last_member_count_show(struct device *d,
483 						struct device_attribute *attr,
484 						char *buf)
485 {
486 	struct net_bridge *br = to_bridge(d);
487 	return sprintf(buf, "%u\n", br->multicast_last_member_count);
488 }
489 
490 static int set_last_member_count(struct net_bridge *br, unsigned long val)
491 {
492 	br->multicast_last_member_count = val;
493 	return 0;
494 }
495 
496 static ssize_t multicast_last_member_count_store(struct device *d,
497 						 struct device_attribute *attr,
498 						 const char *buf, size_t len)
499 {
500 	return store_bridge_parm(d, buf, len, set_last_member_count);
501 }
502 static DEVICE_ATTR_RW(multicast_last_member_count);
503 
504 static ssize_t multicast_startup_query_count_show(
505 	struct device *d, struct device_attribute *attr, char *buf)
506 {
507 	struct net_bridge *br = to_bridge(d);
508 	return sprintf(buf, "%u\n", br->multicast_startup_query_count);
509 }
510 
511 static int set_startup_query_count(struct net_bridge *br, unsigned long val)
512 {
513 	br->multicast_startup_query_count = val;
514 	return 0;
515 }
516 
517 static ssize_t multicast_startup_query_count_store(
518 	struct device *d, struct device_attribute *attr, const char *buf,
519 	size_t len)
520 {
521 	return store_bridge_parm(d, buf, len, set_startup_query_count);
522 }
523 static DEVICE_ATTR_RW(multicast_startup_query_count);
524 
525 static ssize_t multicast_last_member_interval_show(
526 	struct device *d, struct device_attribute *attr, char *buf)
527 {
528 	struct net_bridge *br = to_bridge(d);
529 	return sprintf(buf, "%lu\n",
530 		       jiffies_to_clock_t(br->multicast_last_member_interval));
531 }
532 
533 static int set_last_member_interval(struct net_bridge *br, unsigned long val)
534 {
535 	br->multicast_last_member_interval = clock_t_to_jiffies(val);
536 	return 0;
537 }
538 
539 static ssize_t multicast_last_member_interval_store(
540 	struct device *d, struct device_attribute *attr, const char *buf,
541 	size_t len)
542 {
543 	return store_bridge_parm(d, buf, len, set_last_member_interval);
544 }
545 static DEVICE_ATTR_RW(multicast_last_member_interval);
546 
547 static ssize_t multicast_membership_interval_show(
548 	struct device *d, struct device_attribute *attr, char *buf)
549 {
550 	struct net_bridge *br = to_bridge(d);
551 	return sprintf(buf, "%lu\n",
552 		       jiffies_to_clock_t(br->multicast_membership_interval));
553 }
554 
555 static int set_membership_interval(struct net_bridge *br, unsigned long val)
556 {
557 	br->multicast_membership_interval = clock_t_to_jiffies(val);
558 	return 0;
559 }
560 
561 static ssize_t multicast_membership_interval_store(
562 	struct device *d, struct device_attribute *attr, const char *buf,
563 	size_t len)
564 {
565 	return store_bridge_parm(d, buf, len, set_membership_interval);
566 }
567 static DEVICE_ATTR_RW(multicast_membership_interval);
568 
569 static ssize_t multicast_querier_interval_show(struct device *d,
570 					       struct device_attribute *attr,
571 					       char *buf)
572 {
573 	struct net_bridge *br = to_bridge(d);
574 	return sprintf(buf, "%lu\n",
575 		       jiffies_to_clock_t(br->multicast_querier_interval));
576 }
577 
578 static int set_querier_interval(struct net_bridge *br, unsigned long val)
579 {
580 	br->multicast_querier_interval = clock_t_to_jiffies(val);
581 	return 0;
582 }
583 
584 static ssize_t multicast_querier_interval_store(struct device *d,
585 						struct device_attribute *attr,
586 						const char *buf, size_t len)
587 {
588 	return store_bridge_parm(d, buf, len, set_querier_interval);
589 }
590 static DEVICE_ATTR_RW(multicast_querier_interval);
591 
592 static ssize_t multicast_query_interval_show(struct device *d,
593 					     struct device_attribute *attr,
594 					     char *buf)
595 {
596 	struct net_bridge *br = to_bridge(d);
597 	return sprintf(buf, "%lu\n",
598 		       jiffies_to_clock_t(br->multicast_query_interval));
599 }
600 
601 static int set_query_interval(struct net_bridge *br, unsigned long val)
602 {
603 	br->multicast_query_interval = clock_t_to_jiffies(val);
604 	return 0;
605 }
606 
607 static ssize_t multicast_query_interval_store(struct device *d,
608 					      struct device_attribute *attr,
609 					      const char *buf, size_t len)
610 {
611 	return store_bridge_parm(d, buf, len, set_query_interval);
612 }
613 static DEVICE_ATTR_RW(multicast_query_interval);
614 
615 static ssize_t multicast_query_response_interval_show(
616 	struct device *d, struct device_attribute *attr, char *buf)
617 {
618 	struct net_bridge *br = to_bridge(d);
619 	return sprintf(
620 		buf, "%lu\n",
621 		jiffies_to_clock_t(br->multicast_query_response_interval));
622 }
623 
624 static int set_query_response_interval(struct net_bridge *br, unsigned long val)
625 {
626 	br->multicast_query_response_interval = clock_t_to_jiffies(val);
627 	return 0;
628 }
629 
630 static ssize_t multicast_query_response_interval_store(
631 	struct device *d, struct device_attribute *attr, const char *buf,
632 	size_t len)
633 {
634 	return store_bridge_parm(d, buf, len, set_query_response_interval);
635 }
636 static DEVICE_ATTR_RW(multicast_query_response_interval);
637 
638 static ssize_t multicast_startup_query_interval_show(
639 	struct device *d, struct device_attribute *attr, char *buf)
640 {
641 	struct net_bridge *br = to_bridge(d);
642 	return sprintf(
643 		buf, "%lu\n",
644 		jiffies_to_clock_t(br->multicast_startup_query_interval));
645 }
646 
647 static int set_startup_query_interval(struct net_bridge *br, unsigned long val)
648 {
649 	br->multicast_startup_query_interval = clock_t_to_jiffies(val);
650 	return 0;
651 }
652 
653 static ssize_t multicast_startup_query_interval_store(
654 	struct device *d, struct device_attribute *attr, const char *buf,
655 	size_t len)
656 {
657 	return store_bridge_parm(d, buf, len, set_startup_query_interval);
658 }
659 static DEVICE_ATTR_RW(multicast_startup_query_interval);
660 
661 static ssize_t multicast_stats_enabled_show(struct device *d,
662 					    struct device_attribute *attr,
663 					    char *buf)
664 {
665 	struct net_bridge *br = to_bridge(d);
666 
667 	return sprintf(buf, "%d\n",
668 		       br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED));
669 }
670 
671 static int set_stats_enabled(struct net_bridge *br, unsigned long val)
672 {
673 	br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!val);
674 	return 0;
675 }
676 
677 static ssize_t multicast_stats_enabled_store(struct device *d,
678 					     struct device_attribute *attr,
679 					     const char *buf,
680 					     size_t len)
681 {
682 	return store_bridge_parm(d, buf, len, set_stats_enabled);
683 }
684 static DEVICE_ATTR_RW(multicast_stats_enabled);
685 
686 #if IS_ENABLED(CONFIG_IPV6)
687 static ssize_t multicast_mld_version_show(struct device *d,
688 					  struct device_attribute *attr,
689 					  char *buf)
690 {
691 	struct net_bridge *br = to_bridge(d);
692 
693 	return sprintf(buf, "%u\n", br->multicast_mld_version);
694 }
695 
696 static ssize_t multicast_mld_version_store(struct device *d,
697 					   struct device_attribute *attr,
698 					   const char *buf, size_t len)
699 {
700 	return store_bridge_parm(d, buf, len, br_multicast_set_mld_version);
701 }
702 static DEVICE_ATTR_RW(multicast_mld_version);
703 #endif
704 #endif
705 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
706 static ssize_t nf_call_iptables_show(
707 	struct device *d, struct device_attribute *attr, char *buf)
708 {
709 	struct net_bridge *br = to_bridge(d);
710 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_IPTABLES));
711 }
712 
713 static int set_nf_call_iptables(struct net_bridge *br, unsigned long val)
714 {
715 	br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
716 	return 0;
717 }
718 
719 static ssize_t nf_call_iptables_store(
720 	struct device *d, struct device_attribute *attr, const char *buf,
721 	size_t len)
722 {
723 	return store_bridge_parm(d, buf, len, set_nf_call_iptables);
724 }
725 static DEVICE_ATTR_RW(nf_call_iptables);
726 
727 static ssize_t nf_call_ip6tables_show(
728 	struct device *d, struct device_attribute *attr, char *buf)
729 {
730 	struct net_bridge *br = to_bridge(d);
731 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_IP6TABLES));
732 }
733 
734 static int set_nf_call_ip6tables(struct net_bridge *br, unsigned long val)
735 {
736 	br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
737 	return 0;
738 }
739 
740 static ssize_t nf_call_ip6tables_store(
741 	struct device *d, struct device_attribute *attr, const char *buf,
742 	size_t len)
743 {
744 	return store_bridge_parm(d, buf, len, set_nf_call_ip6tables);
745 }
746 static DEVICE_ATTR_RW(nf_call_ip6tables);
747 
748 static ssize_t nf_call_arptables_show(
749 	struct device *d, struct device_attribute *attr, char *buf)
750 {
751 	struct net_bridge *br = to_bridge(d);
752 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_ARPTABLES));
753 }
754 
755 static int set_nf_call_arptables(struct net_bridge *br, unsigned long val)
756 {
757 	br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
758 	return 0;
759 }
760 
761 static ssize_t nf_call_arptables_store(
762 	struct device *d, struct device_attribute *attr, const char *buf,
763 	size_t len)
764 {
765 	return store_bridge_parm(d, buf, len, set_nf_call_arptables);
766 }
767 static DEVICE_ATTR_RW(nf_call_arptables);
768 #endif
769 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
770 static ssize_t vlan_filtering_show(struct device *d,
771 				   struct device_attribute *attr,
772 				   char *buf)
773 {
774 	struct net_bridge *br = to_bridge(d);
775 	return sprintf(buf, "%d\n", br_opt_get(br, BROPT_VLAN_ENABLED));
776 }
777 
778 static ssize_t vlan_filtering_store(struct device *d,
779 				    struct device_attribute *attr,
780 				    const char *buf, size_t len)
781 {
782 	return store_bridge_parm(d, buf, len, br_vlan_filter_toggle);
783 }
784 static DEVICE_ATTR_RW(vlan_filtering);
785 
786 static ssize_t vlan_protocol_show(struct device *d,
787 				  struct device_attribute *attr,
788 				  char *buf)
789 {
790 	struct net_bridge *br = to_bridge(d);
791 	return sprintf(buf, "%#06x\n", ntohs(br->vlan_proto));
792 }
793 
794 static ssize_t vlan_protocol_store(struct device *d,
795 				   struct device_attribute *attr,
796 				   const char *buf, size_t len)
797 {
798 	return store_bridge_parm(d, buf, len, br_vlan_set_proto);
799 }
800 static DEVICE_ATTR_RW(vlan_protocol);
801 
802 static ssize_t default_pvid_show(struct device *d,
803 				 struct device_attribute *attr,
804 				 char *buf)
805 {
806 	struct net_bridge *br = to_bridge(d);
807 	return sprintf(buf, "%d\n", br->default_pvid);
808 }
809 
810 static ssize_t default_pvid_store(struct device *d,
811 				  struct device_attribute *attr,
812 				  const char *buf, size_t len)
813 {
814 	return store_bridge_parm(d, buf, len, br_vlan_set_default_pvid);
815 }
816 static DEVICE_ATTR_RW(default_pvid);
817 
818 static ssize_t vlan_stats_enabled_show(struct device *d,
819 				       struct device_attribute *attr,
820 				       char *buf)
821 {
822 	struct net_bridge *br = to_bridge(d);
823 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_VLAN_STATS_ENABLED));
824 }
825 
826 static ssize_t vlan_stats_enabled_store(struct device *d,
827 					struct device_attribute *attr,
828 					const char *buf, size_t len)
829 {
830 	return store_bridge_parm(d, buf, len, br_vlan_set_stats);
831 }
832 static DEVICE_ATTR_RW(vlan_stats_enabled);
833 
834 static ssize_t vlan_stats_per_port_show(struct device *d,
835 					struct device_attribute *attr,
836 					char *buf)
837 {
838 	struct net_bridge *br = to_bridge(d);
839 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_VLAN_STATS_PER_PORT));
840 }
841 
842 static ssize_t vlan_stats_per_port_store(struct device *d,
843 					 struct device_attribute *attr,
844 					 const char *buf, size_t len)
845 {
846 	return store_bridge_parm(d, buf, len, br_vlan_set_stats_per_port);
847 }
848 static DEVICE_ATTR_RW(vlan_stats_per_port);
849 #endif
850 
851 static struct attribute *bridge_attrs[] = {
852 	&dev_attr_forward_delay.attr,
853 	&dev_attr_hello_time.attr,
854 	&dev_attr_max_age.attr,
855 	&dev_attr_ageing_time.attr,
856 	&dev_attr_stp_state.attr,
857 	&dev_attr_group_fwd_mask.attr,
858 	&dev_attr_priority.attr,
859 	&dev_attr_bridge_id.attr,
860 	&dev_attr_root_id.attr,
861 	&dev_attr_root_path_cost.attr,
862 	&dev_attr_root_port.attr,
863 	&dev_attr_topology_change.attr,
864 	&dev_attr_topology_change_detected.attr,
865 	&dev_attr_hello_timer.attr,
866 	&dev_attr_tcn_timer.attr,
867 	&dev_attr_topology_change_timer.attr,
868 	&dev_attr_gc_timer.attr,
869 	&dev_attr_group_addr.attr,
870 	&dev_attr_flush.attr,
871 	&dev_attr_no_linklocal_learn.attr,
872 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
873 	&dev_attr_multicast_router.attr,
874 	&dev_attr_multicast_snooping.attr,
875 	&dev_attr_multicast_querier.attr,
876 	&dev_attr_multicast_query_use_ifaddr.attr,
877 	&dev_attr_hash_elasticity.attr,
878 	&dev_attr_hash_max.attr,
879 	&dev_attr_multicast_last_member_count.attr,
880 	&dev_attr_multicast_startup_query_count.attr,
881 	&dev_attr_multicast_last_member_interval.attr,
882 	&dev_attr_multicast_membership_interval.attr,
883 	&dev_attr_multicast_querier_interval.attr,
884 	&dev_attr_multicast_query_interval.attr,
885 	&dev_attr_multicast_query_response_interval.attr,
886 	&dev_attr_multicast_startup_query_interval.attr,
887 	&dev_attr_multicast_stats_enabled.attr,
888 	&dev_attr_multicast_igmp_version.attr,
889 #if IS_ENABLED(CONFIG_IPV6)
890 	&dev_attr_multicast_mld_version.attr,
891 #endif
892 #endif
893 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
894 	&dev_attr_nf_call_iptables.attr,
895 	&dev_attr_nf_call_ip6tables.attr,
896 	&dev_attr_nf_call_arptables.attr,
897 #endif
898 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
899 	&dev_attr_vlan_filtering.attr,
900 	&dev_attr_vlan_protocol.attr,
901 	&dev_attr_default_pvid.attr,
902 	&dev_attr_vlan_stats_enabled.attr,
903 	&dev_attr_vlan_stats_per_port.attr,
904 #endif
905 	NULL
906 };
907 
908 static const struct attribute_group bridge_group = {
909 	.name = SYSFS_BRIDGE_ATTR,
910 	.attrs = bridge_attrs,
911 };
912 
913 /*
914  * Export the forwarding information table as a binary file
915  * The records are struct __fdb_entry.
916  *
917  * Returns the number of bytes read.
918  */
919 static ssize_t brforward_read(struct file *filp, struct kobject *kobj,
920 			      struct bin_attribute *bin_attr,
921 			      char *buf, loff_t off, size_t count)
922 {
923 	struct device *dev = kobj_to_dev(kobj);
924 	struct net_bridge *br = to_bridge(dev);
925 	int n;
926 
927 	/* must read whole records */
928 	if (off % sizeof(struct __fdb_entry) != 0)
929 		return -EINVAL;
930 
931 	n =  br_fdb_fillbuf(br, buf,
932 			    count / sizeof(struct __fdb_entry),
933 			    off / sizeof(struct __fdb_entry));
934 
935 	if (n > 0)
936 		n *= sizeof(struct __fdb_entry);
937 
938 	return n;
939 }
940 
941 static struct bin_attribute bridge_forward = {
942 	.attr = { .name = SYSFS_BRIDGE_FDB,
943 		  .mode = 0444, },
944 	.read = brforward_read,
945 };
946 
947 /*
948  * Add entries in sysfs onto the existing network class device
949  * for the bridge.
950  *   Adds a attribute group "bridge" containing tuning parameters.
951  *   Binary attribute containing the forward table
952  *   Sub directory to hold links to interfaces.
953  *
954  * Note: the ifobj exists only to be a subdirectory
955  *   to hold links.  The ifobj exists in same data structure
956  *   as it's parent the bridge so reference counting works.
957  */
958 int br_sysfs_addbr(struct net_device *dev)
959 {
960 	struct kobject *brobj = &dev->dev.kobj;
961 	struct net_bridge *br = netdev_priv(dev);
962 	int err;
963 
964 	err = sysfs_create_group(brobj, &bridge_group);
965 	if (err) {
966 		pr_info("%s: can't create group %s/%s\n",
967 			__func__, dev->name, bridge_group.name);
968 		goto out1;
969 	}
970 
971 	err = sysfs_create_bin_file(brobj, &bridge_forward);
972 	if (err) {
973 		pr_info("%s: can't create attribute file %s/%s\n",
974 			__func__, dev->name, bridge_forward.attr.name);
975 		goto out2;
976 	}
977 
978 	br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
979 	if (!br->ifobj) {
980 		pr_info("%s: can't add kobject (directory) %s/%s\n",
981 			__func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
982 		err = -ENOMEM;
983 		goto out3;
984 	}
985 	return 0;
986  out3:
987 	sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
988  out2:
989 	sysfs_remove_group(&dev->dev.kobj, &bridge_group);
990  out1:
991 	return err;
992 
993 }
994 
995 void br_sysfs_delbr(struct net_device *dev)
996 {
997 	struct kobject *kobj = &dev->dev.kobj;
998 	struct net_bridge *br = netdev_priv(dev);
999 
1000 	kobject_put(br->ifobj);
1001 	sysfs_remove_bin_file(kobj, &bridge_forward);
1002 	sysfs_remove_group(kobj, &bridge_group);
1003 }
1004