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