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