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