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