xref: /openbmc/linux/net/bridge/br_sysfs_br.c (revision 1406f0f3)
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 int toggle_multicast(struct net_bridge *br, unsigned long val,
413 			    struct netlink_ext_ack *extack)
414 {
415 	return br_multicast_toggle(br, val);
416 }
417 
418 static ssize_t multicast_snooping_store(struct device *d,
419 					struct device_attribute *attr,
420 					const char *buf, size_t len)
421 {
422 	return store_bridge_parm(d, buf, len, toggle_multicast);
423 }
424 static DEVICE_ATTR_RW(multicast_snooping);
425 
426 static ssize_t multicast_query_use_ifaddr_show(struct device *d,
427 					       struct device_attribute *attr,
428 					       char *buf)
429 {
430 	struct net_bridge *br = to_bridge(d);
431 	return sprintf(buf, "%d\n",
432 		       br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR));
433 }
434 
435 static int set_query_use_ifaddr(struct net_bridge *br, unsigned long val,
436 				struct netlink_ext_ack *extack)
437 {
438 	br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
439 	return 0;
440 }
441 
442 static ssize_t
443 multicast_query_use_ifaddr_store(struct device *d,
444 				 struct device_attribute *attr,
445 				 const char *buf, size_t len)
446 {
447 	return store_bridge_parm(d, buf, len, set_query_use_ifaddr);
448 }
449 static DEVICE_ATTR_RW(multicast_query_use_ifaddr);
450 
451 static ssize_t multicast_querier_show(struct device *d,
452 				      struct device_attribute *attr,
453 				      char *buf)
454 {
455 	struct net_bridge *br = to_bridge(d);
456 	return sprintf(buf, "%d\n", br_opt_get(br, BROPT_MULTICAST_QUERIER));
457 }
458 
459 static int set_multicast_querier(struct net_bridge *br, unsigned long val,
460 				 struct netlink_ext_ack *extack)
461 {
462 	return br_multicast_set_querier(br, val);
463 }
464 
465 static ssize_t multicast_querier_store(struct device *d,
466 				       struct device_attribute *attr,
467 				       const char *buf, size_t len)
468 {
469 	return store_bridge_parm(d, buf, len, set_multicast_querier);
470 }
471 static DEVICE_ATTR_RW(multicast_querier);
472 
473 static ssize_t hash_elasticity_show(struct device *d,
474 				    struct device_attribute *attr, char *buf)
475 {
476 	return sprintf(buf, "%u\n", RHT_ELASTICITY);
477 }
478 
479 static int set_elasticity(struct net_bridge *br, unsigned long val,
480 			  struct netlink_ext_ack *extack)
481 {
482 	/* 16 is RHT_ELASTICITY */
483 	NL_SET_ERR_MSG_MOD(extack,
484 			   "the hash_elasticity option has been deprecated and is always 16");
485 	return 0;
486 }
487 
488 static ssize_t hash_elasticity_store(struct device *d,
489 				     struct device_attribute *attr,
490 				     const char *buf, size_t len)
491 {
492 	return store_bridge_parm(d, buf, len, set_elasticity);
493 }
494 static DEVICE_ATTR_RW(hash_elasticity);
495 
496 static ssize_t hash_max_show(struct device *d, struct device_attribute *attr,
497 			     char *buf)
498 {
499 	struct net_bridge *br = to_bridge(d);
500 	return sprintf(buf, "%u\n", br->hash_max);
501 }
502 
503 static int set_hash_max(struct net_bridge *br, unsigned long val,
504 			struct netlink_ext_ack *extack)
505 {
506 	br->hash_max = val;
507 	return 0;
508 }
509 
510 static ssize_t hash_max_store(struct device *d, struct device_attribute *attr,
511 			      const char *buf, size_t len)
512 {
513 	return store_bridge_parm(d, buf, len, set_hash_max);
514 }
515 static DEVICE_ATTR_RW(hash_max);
516 
517 static ssize_t multicast_igmp_version_show(struct device *d,
518 					   struct device_attribute *attr,
519 					   char *buf)
520 {
521 	struct net_bridge *br = to_bridge(d);
522 
523 	return sprintf(buf, "%u\n", br->multicast_igmp_version);
524 }
525 
526 static int set_multicast_igmp_version(struct net_bridge *br, unsigned long val,
527 				      struct netlink_ext_ack *extack)
528 {
529 	return br_multicast_set_igmp_version(br, val);
530 }
531 
532 static ssize_t multicast_igmp_version_store(struct device *d,
533 					    struct device_attribute *attr,
534 					    const char *buf, size_t len)
535 {
536 	return store_bridge_parm(d, buf, len, set_multicast_igmp_version);
537 }
538 static DEVICE_ATTR_RW(multicast_igmp_version);
539 
540 static ssize_t multicast_last_member_count_show(struct device *d,
541 						struct device_attribute *attr,
542 						char *buf)
543 {
544 	struct net_bridge *br = to_bridge(d);
545 	return sprintf(buf, "%u\n", br->multicast_last_member_count);
546 }
547 
548 static int set_last_member_count(struct net_bridge *br, unsigned long val,
549 				 struct netlink_ext_ack *extack)
550 {
551 	br->multicast_last_member_count = val;
552 	return 0;
553 }
554 
555 static ssize_t multicast_last_member_count_store(struct device *d,
556 						 struct device_attribute *attr,
557 						 const char *buf, size_t len)
558 {
559 	return store_bridge_parm(d, buf, len, set_last_member_count);
560 }
561 static DEVICE_ATTR_RW(multicast_last_member_count);
562 
563 static ssize_t multicast_startup_query_count_show(
564 	struct device *d, struct device_attribute *attr, char *buf)
565 {
566 	struct net_bridge *br = to_bridge(d);
567 	return sprintf(buf, "%u\n", br->multicast_startup_query_count);
568 }
569 
570 static int set_startup_query_count(struct net_bridge *br, unsigned long val,
571 				   struct netlink_ext_ack *extack)
572 {
573 	br->multicast_startup_query_count = val;
574 	return 0;
575 }
576 
577 static ssize_t multicast_startup_query_count_store(
578 	struct device *d, struct device_attribute *attr, const char *buf,
579 	size_t len)
580 {
581 	return store_bridge_parm(d, buf, len, set_startup_query_count);
582 }
583 static DEVICE_ATTR_RW(multicast_startup_query_count);
584 
585 static ssize_t multicast_last_member_interval_show(
586 	struct device *d, struct device_attribute *attr, char *buf)
587 {
588 	struct net_bridge *br = to_bridge(d);
589 	return sprintf(buf, "%lu\n",
590 		       jiffies_to_clock_t(br->multicast_last_member_interval));
591 }
592 
593 static int set_last_member_interval(struct net_bridge *br, unsigned long val,
594 				    struct netlink_ext_ack *extack)
595 {
596 	br->multicast_last_member_interval = clock_t_to_jiffies(val);
597 	return 0;
598 }
599 
600 static ssize_t multicast_last_member_interval_store(
601 	struct device *d, struct device_attribute *attr, const char *buf,
602 	size_t len)
603 {
604 	return store_bridge_parm(d, buf, len, set_last_member_interval);
605 }
606 static DEVICE_ATTR_RW(multicast_last_member_interval);
607 
608 static ssize_t multicast_membership_interval_show(
609 	struct device *d, struct device_attribute *attr, char *buf)
610 {
611 	struct net_bridge *br = to_bridge(d);
612 	return sprintf(buf, "%lu\n",
613 		       jiffies_to_clock_t(br->multicast_membership_interval));
614 }
615 
616 static int set_membership_interval(struct net_bridge *br, unsigned long val,
617 				   struct netlink_ext_ack *extack)
618 {
619 	br->multicast_membership_interval = clock_t_to_jiffies(val);
620 	return 0;
621 }
622 
623 static ssize_t multicast_membership_interval_store(
624 	struct device *d, struct device_attribute *attr, const char *buf,
625 	size_t len)
626 {
627 	return store_bridge_parm(d, buf, len, set_membership_interval);
628 }
629 static DEVICE_ATTR_RW(multicast_membership_interval);
630 
631 static ssize_t multicast_querier_interval_show(struct device *d,
632 					       struct device_attribute *attr,
633 					       char *buf)
634 {
635 	struct net_bridge *br = to_bridge(d);
636 	return sprintf(buf, "%lu\n",
637 		       jiffies_to_clock_t(br->multicast_querier_interval));
638 }
639 
640 static int set_querier_interval(struct net_bridge *br, unsigned long val,
641 				struct netlink_ext_ack *extack)
642 {
643 	br->multicast_querier_interval = clock_t_to_jiffies(val);
644 	return 0;
645 }
646 
647 static ssize_t multicast_querier_interval_store(struct device *d,
648 						struct device_attribute *attr,
649 						const char *buf, size_t len)
650 {
651 	return store_bridge_parm(d, buf, len, set_querier_interval);
652 }
653 static DEVICE_ATTR_RW(multicast_querier_interval);
654 
655 static ssize_t multicast_query_interval_show(struct device *d,
656 					     struct device_attribute *attr,
657 					     char *buf)
658 {
659 	struct net_bridge *br = to_bridge(d);
660 	return sprintf(buf, "%lu\n",
661 		       jiffies_to_clock_t(br->multicast_query_interval));
662 }
663 
664 static int set_query_interval(struct net_bridge *br, unsigned long val,
665 			      struct netlink_ext_ack *extack)
666 {
667 	br->multicast_query_interval = clock_t_to_jiffies(val);
668 	return 0;
669 }
670 
671 static ssize_t multicast_query_interval_store(struct device *d,
672 					      struct device_attribute *attr,
673 					      const char *buf, size_t len)
674 {
675 	return store_bridge_parm(d, buf, len, set_query_interval);
676 }
677 static DEVICE_ATTR_RW(multicast_query_interval);
678 
679 static ssize_t multicast_query_response_interval_show(
680 	struct device *d, struct device_attribute *attr, char *buf)
681 {
682 	struct net_bridge *br = to_bridge(d);
683 	return sprintf(
684 		buf, "%lu\n",
685 		jiffies_to_clock_t(br->multicast_query_response_interval));
686 }
687 
688 static int set_query_response_interval(struct net_bridge *br, unsigned long val,
689 				       struct netlink_ext_ack *extack)
690 {
691 	br->multicast_query_response_interval = clock_t_to_jiffies(val);
692 	return 0;
693 }
694 
695 static ssize_t multicast_query_response_interval_store(
696 	struct device *d, struct device_attribute *attr, const char *buf,
697 	size_t len)
698 {
699 	return store_bridge_parm(d, buf, len, set_query_response_interval);
700 }
701 static DEVICE_ATTR_RW(multicast_query_response_interval);
702 
703 static ssize_t multicast_startup_query_interval_show(
704 	struct device *d, struct device_attribute *attr, char *buf)
705 {
706 	struct net_bridge *br = to_bridge(d);
707 	return sprintf(
708 		buf, "%lu\n",
709 		jiffies_to_clock_t(br->multicast_startup_query_interval));
710 }
711 
712 static int set_startup_query_interval(struct net_bridge *br, unsigned long val,
713 				      struct netlink_ext_ack *extack)
714 {
715 	br->multicast_startup_query_interval = clock_t_to_jiffies(val);
716 	return 0;
717 }
718 
719 static ssize_t multicast_startup_query_interval_store(
720 	struct device *d, struct device_attribute *attr, const char *buf,
721 	size_t len)
722 {
723 	return store_bridge_parm(d, buf, len, set_startup_query_interval);
724 }
725 static DEVICE_ATTR_RW(multicast_startup_query_interval);
726 
727 static ssize_t multicast_stats_enabled_show(struct device *d,
728 					    struct device_attribute *attr,
729 					    char *buf)
730 {
731 	struct net_bridge *br = to_bridge(d);
732 
733 	return sprintf(buf, "%d\n",
734 		       br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED));
735 }
736 
737 static int set_stats_enabled(struct net_bridge *br, unsigned long val,
738 			     struct netlink_ext_ack *extack)
739 {
740 	br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!val);
741 	return 0;
742 }
743 
744 static ssize_t multicast_stats_enabled_store(struct device *d,
745 					     struct device_attribute *attr,
746 					     const char *buf,
747 					     size_t len)
748 {
749 	return store_bridge_parm(d, buf, len, set_stats_enabled);
750 }
751 static DEVICE_ATTR_RW(multicast_stats_enabled);
752 
753 #if IS_ENABLED(CONFIG_IPV6)
754 static ssize_t multicast_mld_version_show(struct device *d,
755 					  struct device_attribute *attr,
756 					  char *buf)
757 {
758 	struct net_bridge *br = to_bridge(d);
759 
760 	return sprintf(buf, "%u\n", br->multicast_mld_version);
761 }
762 
763 static int set_multicast_mld_version(struct net_bridge *br, unsigned long val,
764 				     struct netlink_ext_ack *extack)
765 {
766 	return br_multicast_set_mld_version(br, val);
767 }
768 
769 static ssize_t multicast_mld_version_store(struct device *d,
770 					   struct device_attribute *attr,
771 					   const char *buf, size_t len)
772 {
773 	return store_bridge_parm(d, buf, len, set_multicast_mld_version);
774 }
775 static DEVICE_ATTR_RW(multicast_mld_version);
776 #endif
777 #endif
778 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
779 static ssize_t nf_call_iptables_show(
780 	struct device *d, struct device_attribute *attr, char *buf)
781 {
782 	struct net_bridge *br = to_bridge(d);
783 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_IPTABLES));
784 }
785 
786 static int set_nf_call_iptables(struct net_bridge *br, unsigned long val,
787 				struct netlink_ext_ack *extack)
788 {
789 	br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
790 	return 0;
791 }
792 
793 static ssize_t nf_call_iptables_store(
794 	struct device *d, struct device_attribute *attr, const char *buf,
795 	size_t len)
796 {
797 	return store_bridge_parm(d, buf, len, set_nf_call_iptables);
798 }
799 static DEVICE_ATTR_RW(nf_call_iptables);
800 
801 static ssize_t nf_call_ip6tables_show(
802 	struct device *d, struct device_attribute *attr, char *buf)
803 {
804 	struct net_bridge *br = to_bridge(d);
805 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_IP6TABLES));
806 }
807 
808 static int set_nf_call_ip6tables(struct net_bridge *br, unsigned long val,
809 				 struct netlink_ext_ack *extack)
810 {
811 	br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
812 	return 0;
813 }
814 
815 static ssize_t nf_call_ip6tables_store(
816 	struct device *d, struct device_attribute *attr, const char *buf,
817 	size_t len)
818 {
819 	return store_bridge_parm(d, buf, len, set_nf_call_ip6tables);
820 }
821 static DEVICE_ATTR_RW(nf_call_ip6tables);
822 
823 static ssize_t nf_call_arptables_show(
824 	struct device *d, struct device_attribute *attr, char *buf)
825 {
826 	struct net_bridge *br = to_bridge(d);
827 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_ARPTABLES));
828 }
829 
830 static int set_nf_call_arptables(struct net_bridge *br, unsigned long val,
831 				 struct netlink_ext_ack *extack)
832 {
833 	br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
834 	return 0;
835 }
836 
837 static ssize_t nf_call_arptables_store(
838 	struct device *d, struct device_attribute *attr, const char *buf,
839 	size_t len)
840 {
841 	return store_bridge_parm(d, buf, len, set_nf_call_arptables);
842 }
843 static DEVICE_ATTR_RW(nf_call_arptables);
844 #endif
845 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
846 static ssize_t vlan_filtering_show(struct device *d,
847 				   struct device_attribute *attr,
848 				   char *buf)
849 {
850 	struct net_bridge *br = to_bridge(d);
851 	return sprintf(buf, "%d\n", br_opt_get(br, BROPT_VLAN_ENABLED));
852 }
853 
854 static ssize_t vlan_filtering_store(struct device *d,
855 				    struct device_attribute *attr,
856 				    const char *buf, size_t len)
857 {
858 	return store_bridge_parm(d, buf, len, br_vlan_filter_toggle);
859 }
860 static DEVICE_ATTR_RW(vlan_filtering);
861 
862 static ssize_t vlan_protocol_show(struct device *d,
863 				  struct device_attribute *attr,
864 				  char *buf)
865 {
866 	struct net_bridge *br = to_bridge(d);
867 	return sprintf(buf, "%#06x\n", ntohs(br->vlan_proto));
868 }
869 
870 static ssize_t vlan_protocol_store(struct device *d,
871 				   struct device_attribute *attr,
872 				   const char *buf, size_t len)
873 {
874 	return store_bridge_parm(d, buf, len, br_vlan_set_proto);
875 }
876 static DEVICE_ATTR_RW(vlan_protocol);
877 
878 static ssize_t default_pvid_show(struct device *d,
879 				 struct device_attribute *attr,
880 				 char *buf)
881 {
882 	struct net_bridge *br = to_bridge(d);
883 	return sprintf(buf, "%d\n", br->default_pvid);
884 }
885 
886 static ssize_t default_pvid_store(struct device *d,
887 				  struct device_attribute *attr,
888 				  const char *buf, size_t len)
889 {
890 	return store_bridge_parm(d, buf, len, br_vlan_set_default_pvid);
891 }
892 static DEVICE_ATTR_RW(default_pvid);
893 
894 static ssize_t vlan_stats_enabled_show(struct device *d,
895 				       struct device_attribute *attr,
896 				       char *buf)
897 {
898 	struct net_bridge *br = to_bridge(d);
899 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_VLAN_STATS_ENABLED));
900 }
901 
902 static int set_vlan_stats_enabled(struct net_bridge *br, unsigned long val,
903 				  struct netlink_ext_ack *extack)
904 {
905 	return br_vlan_set_stats(br, val);
906 }
907 
908 static ssize_t vlan_stats_enabled_store(struct device *d,
909 					struct device_attribute *attr,
910 					const char *buf, size_t len)
911 {
912 	return store_bridge_parm(d, buf, len, set_vlan_stats_enabled);
913 }
914 static DEVICE_ATTR_RW(vlan_stats_enabled);
915 
916 static ssize_t vlan_stats_per_port_show(struct device *d,
917 					struct device_attribute *attr,
918 					char *buf)
919 {
920 	struct net_bridge *br = to_bridge(d);
921 	return sprintf(buf, "%u\n", br_opt_get(br, BROPT_VLAN_STATS_PER_PORT));
922 }
923 
924 static int set_vlan_stats_per_port(struct net_bridge *br, unsigned long val,
925 				   struct netlink_ext_ack *extack)
926 {
927 	return br_vlan_set_stats_per_port(br, val);
928 }
929 
930 static ssize_t vlan_stats_per_port_store(struct device *d,
931 					 struct device_attribute *attr,
932 					 const char *buf, size_t len)
933 {
934 	return store_bridge_parm(d, buf, len, set_vlan_stats_per_port);
935 }
936 static DEVICE_ATTR_RW(vlan_stats_per_port);
937 #endif
938 
939 static struct attribute *bridge_attrs[] = {
940 	&dev_attr_forward_delay.attr,
941 	&dev_attr_hello_time.attr,
942 	&dev_attr_max_age.attr,
943 	&dev_attr_ageing_time.attr,
944 	&dev_attr_stp_state.attr,
945 	&dev_attr_group_fwd_mask.attr,
946 	&dev_attr_priority.attr,
947 	&dev_attr_bridge_id.attr,
948 	&dev_attr_root_id.attr,
949 	&dev_attr_root_path_cost.attr,
950 	&dev_attr_root_port.attr,
951 	&dev_attr_topology_change.attr,
952 	&dev_attr_topology_change_detected.attr,
953 	&dev_attr_hello_timer.attr,
954 	&dev_attr_tcn_timer.attr,
955 	&dev_attr_topology_change_timer.attr,
956 	&dev_attr_gc_timer.attr,
957 	&dev_attr_group_addr.attr,
958 	&dev_attr_flush.attr,
959 	&dev_attr_no_linklocal_learn.attr,
960 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
961 	&dev_attr_multicast_router.attr,
962 	&dev_attr_multicast_snooping.attr,
963 	&dev_attr_multicast_querier.attr,
964 	&dev_attr_multicast_query_use_ifaddr.attr,
965 	&dev_attr_hash_elasticity.attr,
966 	&dev_attr_hash_max.attr,
967 	&dev_attr_multicast_last_member_count.attr,
968 	&dev_attr_multicast_startup_query_count.attr,
969 	&dev_attr_multicast_last_member_interval.attr,
970 	&dev_attr_multicast_membership_interval.attr,
971 	&dev_attr_multicast_querier_interval.attr,
972 	&dev_attr_multicast_query_interval.attr,
973 	&dev_attr_multicast_query_response_interval.attr,
974 	&dev_attr_multicast_startup_query_interval.attr,
975 	&dev_attr_multicast_stats_enabled.attr,
976 	&dev_attr_multicast_igmp_version.attr,
977 #if IS_ENABLED(CONFIG_IPV6)
978 	&dev_attr_multicast_mld_version.attr,
979 #endif
980 #endif
981 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
982 	&dev_attr_nf_call_iptables.attr,
983 	&dev_attr_nf_call_ip6tables.attr,
984 	&dev_attr_nf_call_arptables.attr,
985 #endif
986 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
987 	&dev_attr_vlan_filtering.attr,
988 	&dev_attr_vlan_protocol.attr,
989 	&dev_attr_default_pvid.attr,
990 	&dev_attr_vlan_stats_enabled.attr,
991 	&dev_attr_vlan_stats_per_port.attr,
992 #endif
993 	NULL
994 };
995 
996 static const struct attribute_group bridge_group = {
997 	.name = SYSFS_BRIDGE_ATTR,
998 	.attrs = bridge_attrs,
999 };
1000 
1001 /*
1002  * Export the forwarding information table as a binary file
1003  * The records are struct __fdb_entry.
1004  *
1005  * Returns the number of bytes read.
1006  */
1007 static ssize_t brforward_read(struct file *filp, struct kobject *kobj,
1008 			      struct bin_attribute *bin_attr,
1009 			      char *buf, loff_t off, size_t count)
1010 {
1011 	struct device *dev = kobj_to_dev(kobj);
1012 	struct net_bridge *br = to_bridge(dev);
1013 	int n;
1014 
1015 	/* must read whole records */
1016 	if (off % sizeof(struct __fdb_entry) != 0)
1017 		return -EINVAL;
1018 
1019 	n =  br_fdb_fillbuf(br, buf,
1020 			    count / sizeof(struct __fdb_entry),
1021 			    off / sizeof(struct __fdb_entry));
1022 
1023 	if (n > 0)
1024 		n *= sizeof(struct __fdb_entry);
1025 
1026 	return n;
1027 }
1028 
1029 static struct bin_attribute bridge_forward = {
1030 	.attr = { .name = SYSFS_BRIDGE_FDB,
1031 		  .mode = 0444, },
1032 	.read = brforward_read,
1033 };
1034 
1035 /*
1036  * Add entries in sysfs onto the existing network class device
1037  * for the bridge.
1038  *   Adds a attribute group "bridge" containing tuning parameters.
1039  *   Binary attribute containing the forward table
1040  *   Sub directory to hold links to interfaces.
1041  *
1042  * Note: the ifobj exists only to be a subdirectory
1043  *   to hold links.  The ifobj exists in same data structure
1044  *   as it's parent the bridge so reference counting works.
1045  */
1046 int br_sysfs_addbr(struct net_device *dev)
1047 {
1048 	struct kobject *brobj = &dev->dev.kobj;
1049 	struct net_bridge *br = netdev_priv(dev);
1050 	int err;
1051 
1052 	err = sysfs_create_group(brobj, &bridge_group);
1053 	if (err) {
1054 		pr_info("%s: can't create group %s/%s\n",
1055 			__func__, dev->name, bridge_group.name);
1056 		goto out1;
1057 	}
1058 
1059 	err = sysfs_create_bin_file(brobj, &bridge_forward);
1060 	if (err) {
1061 		pr_info("%s: can't create attribute file %s/%s\n",
1062 			__func__, dev->name, bridge_forward.attr.name);
1063 		goto out2;
1064 	}
1065 
1066 	br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
1067 	if (!br->ifobj) {
1068 		pr_info("%s: can't add kobject (directory) %s/%s\n",
1069 			__func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
1070 		err = -ENOMEM;
1071 		goto out3;
1072 	}
1073 	return 0;
1074  out3:
1075 	sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
1076  out2:
1077 	sysfs_remove_group(&dev->dev.kobj, &bridge_group);
1078  out1:
1079 	return err;
1080 
1081 }
1082 
1083 void br_sysfs_delbr(struct net_device *dev)
1084 {
1085 	struct kobject *kobj = &dev->dev.kobj;
1086 	struct net_bridge *br = netdev_priv(dev);
1087 
1088 	kobject_put(br->ifobj);
1089 	sysfs_remove_bin_file(kobj, &bridge_forward);
1090 	sysfs_remove_group(kobj, &bridge_group);
1091 }
1092