Merge tag 'icc-5.4-rc1' of https://git.linaro.org/people/georgi.djakov/linux into...
[linux/fpc-iii.git] / net / bridge / br_sysfs_br.c
blob9ab0f00b10811fb339b39e8e1b075953a0a35249
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Sysfs attributes of bridge
4 * Linux ethernet bridge
6 * Authors:
7 * Stephen Hemminger <shemminger@osdl.org>
8 */
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>
20 #include "br_private.h"
22 #define to_bridge(cd) ((struct net_bridge *)netdev_priv(to_net_dev(cd)))
25 * Common code for storing bridge parameters.
27 static ssize_t store_bridge_parm(struct device *d,
28 const char *buf, size_t len,
29 int (*set)(struct net_bridge *, unsigned long))
31 struct net_bridge *br = to_bridge(d);
32 char *endp;
33 unsigned long val;
34 int err;
36 if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
37 return -EPERM;
39 val = simple_strtoul(buf, &endp, 0);
40 if (endp == buf)
41 return -EINVAL;
43 if (!rtnl_trylock())
44 return restart_syscall();
46 err = (*set)(br, val);
47 if (!err)
48 netdev_state_change(br->dev);
49 rtnl_unlock();
51 return err ? err : len;
55 static ssize_t forward_delay_show(struct device *d,
56 struct device_attribute *attr, char *buf)
58 struct net_bridge *br = to_bridge(d);
59 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
62 static ssize_t forward_delay_store(struct device *d,
63 struct device_attribute *attr,
64 const char *buf, size_t len)
66 return store_bridge_parm(d, buf, len, br_set_forward_delay);
68 static DEVICE_ATTR_RW(forward_delay);
70 static ssize_t hello_time_show(struct device *d, struct device_attribute *attr,
71 char *buf)
73 return sprintf(buf, "%lu\n",
74 jiffies_to_clock_t(to_bridge(d)->hello_time));
77 static ssize_t hello_time_store(struct device *d,
78 struct device_attribute *attr, const char *buf,
79 size_t len)
81 return store_bridge_parm(d, buf, len, br_set_hello_time);
83 static DEVICE_ATTR_RW(hello_time);
85 static ssize_t max_age_show(struct device *d, struct device_attribute *attr,
86 char *buf)
88 return sprintf(buf, "%lu\n",
89 jiffies_to_clock_t(to_bridge(d)->max_age));
92 static ssize_t max_age_store(struct device *d, struct device_attribute *attr,
93 const char *buf, size_t len)
95 return store_bridge_parm(d, buf, len, br_set_max_age);
97 static DEVICE_ATTR_RW(max_age);
99 static ssize_t ageing_time_show(struct device *d,
100 struct device_attribute *attr, char *buf)
102 struct net_bridge *br = to_bridge(d);
103 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
106 static int set_ageing_time(struct net_bridge *br, unsigned long val)
108 return br_set_ageing_time(br, val);
111 static ssize_t ageing_time_store(struct device *d,
112 struct device_attribute *attr,
113 const char *buf, size_t len)
115 return store_bridge_parm(d, buf, len, set_ageing_time);
117 static DEVICE_ATTR_RW(ageing_time);
119 static ssize_t stp_state_show(struct device *d,
120 struct device_attribute *attr, char *buf)
122 struct net_bridge *br = to_bridge(d);
123 return sprintf(buf, "%d\n", br->stp_enabled);
127 static int set_stp_state(struct net_bridge *br, unsigned long val)
129 br_stp_set_enabled(br, val);
131 return 0;
134 static ssize_t stp_state_store(struct device *d,
135 struct device_attribute *attr, const char *buf,
136 size_t len)
138 return store_bridge_parm(d, buf, len, set_stp_state);
140 static DEVICE_ATTR_RW(stp_state);
142 static ssize_t group_fwd_mask_show(struct device *d,
143 struct device_attribute *attr,
144 char *buf)
146 struct net_bridge *br = to_bridge(d);
147 return sprintf(buf, "%#x\n", br->group_fwd_mask);
150 static int set_group_fwd_mask(struct net_bridge *br, unsigned long val)
152 if (val & BR_GROUPFWD_RESTRICTED)
153 return -EINVAL;
155 br->group_fwd_mask = val;
157 return 0;
160 static ssize_t group_fwd_mask_store(struct device *d,
161 struct device_attribute *attr,
162 const char *buf,
163 size_t len)
165 return store_bridge_parm(d, buf, len, set_group_fwd_mask);
167 static DEVICE_ATTR_RW(group_fwd_mask);
169 static ssize_t priority_show(struct device *d, struct device_attribute *attr,
170 char *buf)
172 struct net_bridge *br = to_bridge(d);
173 return sprintf(buf, "%d\n",
174 (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
177 static int set_priority(struct net_bridge *br, unsigned long val)
179 br_stp_set_bridge_priority(br, (u16) val);
180 return 0;
183 static ssize_t priority_store(struct device *d, struct device_attribute *attr,
184 const char *buf, size_t len)
186 return store_bridge_parm(d, buf, len, set_priority);
188 static DEVICE_ATTR_RW(priority);
190 static ssize_t root_id_show(struct device *d, struct device_attribute *attr,
191 char *buf)
193 return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
195 static DEVICE_ATTR_RO(root_id);
197 static ssize_t bridge_id_show(struct device *d, struct device_attribute *attr,
198 char *buf)
200 return br_show_bridge_id(buf, &to_bridge(d)->bridge_id);
202 static DEVICE_ATTR_RO(bridge_id);
204 static ssize_t root_port_show(struct device *d, struct device_attribute *attr,
205 char *buf)
207 return sprintf(buf, "%d\n", to_bridge(d)->root_port);
209 static DEVICE_ATTR_RO(root_port);
211 static ssize_t root_path_cost_show(struct device *d,
212 struct device_attribute *attr, char *buf)
214 return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
216 static DEVICE_ATTR_RO(root_path_cost);
218 static ssize_t topology_change_show(struct device *d,
219 struct device_attribute *attr, char *buf)
221 return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
223 static DEVICE_ATTR_RO(topology_change);
225 static ssize_t topology_change_detected_show(struct device *d,
226 struct device_attribute *attr,
227 char *buf)
229 struct net_bridge *br = to_bridge(d);
230 return sprintf(buf, "%d\n", br->topology_change_detected);
232 static DEVICE_ATTR_RO(topology_change_detected);
234 static ssize_t hello_timer_show(struct device *d,
235 struct device_attribute *attr, char *buf)
237 struct net_bridge *br = to_bridge(d);
238 return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
240 static DEVICE_ATTR_RO(hello_timer);
242 static ssize_t tcn_timer_show(struct device *d, struct device_attribute *attr,
243 char *buf)
245 struct net_bridge *br = to_bridge(d);
246 return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
248 static DEVICE_ATTR_RO(tcn_timer);
250 static ssize_t topology_change_timer_show(struct device *d,
251 struct device_attribute *attr,
252 char *buf)
254 struct net_bridge *br = to_bridge(d);
255 return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
257 static DEVICE_ATTR_RO(topology_change_timer);
259 static ssize_t gc_timer_show(struct device *d, struct device_attribute *attr,
260 char *buf)
262 struct net_bridge *br = to_bridge(d);
263 return sprintf(buf, "%ld\n", br_timer_value(&br->gc_work.timer));
265 static DEVICE_ATTR_RO(gc_timer);
267 static ssize_t group_addr_show(struct device *d,
268 struct device_attribute *attr, char *buf)
270 struct net_bridge *br = to_bridge(d);
271 return sprintf(buf, "%pM\n", br->group_addr);
274 static ssize_t group_addr_store(struct device *d,
275 struct device_attribute *attr,
276 const char *buf, size_t len)
278 struct net_bridge *br = to_bridge(d);
279 u8 new_addr[6];
281 if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
282 return -EPERM;
284 if (!mac_pton(buf, new_addr))
285 return -EINVAL;
287 if (!is_link_local_ether_addr(new_addr))
288 return -EINVAL;
290 if (new_addr[5] == 1 || /* 802.3x Pause address */
291 new_addr[5] == 2 || /* 802.3ad Slow protocols */
292 new_addr[5] == 3) /* 802.1X PAE address */
293 return -EINVAL;
295 if (!rtnl_trylock())
296 return restart_syscall();
298 spin_lock_bh(&br->lock);
299 ether_addr_copy(br->group_addr, new_addr);
300 spin_unlock_bh(&br->lock);
302 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
303 br_recalculate_fwd_mask(br);
304 netdev_state_change(br->dev);
306 rtnl_unlock();
308 return len;
311 static DEVICE_ATTR_RW(group_addr);
313 static int set_flush(struct net_bridge *br, unsigned long val)
315 br_fdb_flush(br);
316 return 0;
319 static ssize_t flush_store(struct device *d,
320 struct device_attribute *attr,
321 const char *buf, size_t len)
323 return store_bridge_parm(d, buf, len, set_flush);
325 static DEVICE_ATTR_WO(flush);
327 static ssize_t no_linklocal_learn_show(struct device *d,
328 struct device_attribute *attr,
329 char *buf)
331 struct net_bridge *br = to_bridge(d);
332 return sprintf(buf, "%d\n", br_boolopt_get(br, BR_BOOLOPT_NO_LL_LEARN));
335 static int set_no_linklocal_learn(struct net_bridge *br, unsigned long val)
337 return br_boolopt_toggle(br, BR_BOOLOPT_NO_LL_LEARN, !!val, NULL);
340 static ssize_t no_linklocal_learn_store(struct device *d,
341 struct device_attribute *attr,
342 const char *buf, size_t len)
344 return store_bridge_parm(d, buf, len, set_no_linklocal_learn);
346 static DEVICE_ATTR_RW(no_linklocal_learn);
348 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
349 static ssize_t multicast_router_show(struct device *d,
350 struct device_attribute *attr, char *buf)
352 struct net_bridge *br = to_bridge(d);
353 return sprintf(buf, "%d\n", br->multicast_router);
356 static ssize_t multicast_router_store(struct device *d,
357 struct device_attribute *attr,
358 const char *buf, size_t len)
360 return store_bridge_parm(d, buf, len, br_multicast_set_router);
362 static DEVICE_ATTR_RW(multicast_router);
364 static ssize_t multicast_snooping_show(struct device *d,
365 struct device_attribute *attr,
366 char *buf)
368 struct net_bridge *br = to_bridge(d);
369 return sprintf(buf, "%d\n", br_opt_get(br, BROPT_MULTICAST_ENABLED));
372 static ssize_t multicast_snooping_store(struct device *d,
373 struct device_attribute *attr,
374 const char *buf, size_t len)
376 return store_bridge_parm(d, buf, len, br_multicast_toggle);
378 static DEVICE_ATTR_RW(multicast_snooping);
380 static ssize_t multicast_query_use_ifaddr_show(struct device *d,
381 struct device_attribute *attr,
382 char *buf)
384 struct net_bridge *br = to_bridge(d);
385 return sprintf(buf, "%d\n",
386 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR));
389 static int set_query_use_ifaddr(struct net_bridge *br, unsigned long val)
391 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
392 return 0;
395 static ssize_t
396 multicast_query_use_ifaddr_store(struct device *d,
397 struct device_attribute *attr,
398 const char *buf, size_t len)
400 return store_bridge_parm(d, buf, len, set_query_use_ifaddr);
402 static DEVICE_ATTR_RW(multicast_query_use_ifaddr);
404 static ssize_t multicast_querier_show(struct device *d,
405 struct device_attribute *attr,
406 char *buf)
408 struct net_bridge *br = to_bridge(d);
409 return sprintf(buf, "%d\n", br_opt_get(br, BROPT_MULTICAST_QUERIER));
412 static ssize_t multicast_querier_store(struct device *d,
413 struct device_attribute *attr,
414 const char *buf, size_t len)
416 return store_bridge_parm(d, buf, len, br_multicast_set_querier);
418 static DEVICE_ATTR_RW(multicast_querier);
420 static ssize_t hash_elasticity_show(struct device *d,
421 struct device_attribute *attr, char *buf)
423 return sprintf(buf, "%u\n", RHT_ELASTICITY);
426 static int set_elasticity(struct net_bridge *br, unsigned long val)
428 br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
429 RHT_ELASTICITY);
430 return 0;
433 static ssize_t hash_elasticity_store(struct device *d,
434 struct device_attribute *attr,
435 const char *buf, size_t len)
437 return store_bridge_parm(d, buf, len, set_elasticity);
439 static DEVICE_ATTR_RW(hash_elasticity);
441 static ssize_t hash_max_show(struct device *d, struct device_attribute *attr,
442 char *buf)
444 struct net_bridge *br = to_bridge(d);
445 return sprintf(buf, "%u\n", br->hash_max);
448 static int set_hash_max(struct net_bridge *br, unsigned long val)
450 br->hash_max = val;
451 return 0;
454 static ssize_t hash_max_store(struct device *d, struct device_attribute *attr,
455 const char *buf, size_t len)
457 return store_bridge_parm(d, buf, len, set_hash_max);
459 static DEVICE_ATTR_RW(hash_max);
461 static ssize_t multicast_igmp_version_show(struct device *d,
462 struct device_attribute *attr,
463 char *buf)
465 struct net_bridge *br = to_bridge(d);
467 return sprintf(buf, "%u\n", br->multicast_igmp_version);
470 static ssize_t multicast_igmp_version_store(struct device *d,
471 struct device_attribute *attr,
472 const char *buf, size_t len)
474 return store_bridge_parm(d, buf, len, br_multicast_set_igmp_version);
476 static DEVICE_ATTR_RW(multicast_igmp_version);
478 static ssize_t multicast_last_member_count_show(struct device *d,
479 struct device_attribute *attr,
480 char *buf)
482 struct net_bridge *br = to_bridge(d);
483 return sprintf(buf, "%u\n", br->multicast_last_member_count);
486 static int set_last_member_count(struct net_bridge *br, unsigned long val)
488 br->multicast_last_member_count = val;
489 return 0;
492 static ssize_t multicast_last_member_count_store(struct device *d,
493 struct device_attribute *attr,
494 const char *buf, size_t len)
496 return store_bridge_parm(d, buf, len, set_last_member_count);
498 static DEVICE_ATTR_RW(multicast_last_member_count);
500 static ssize_t multicast_startup_query_count_show(
501 struct device *d, struct device_attribute *attr, char *buf)
503 struct net_bridge *br = to_bridge(d);
504 return sprintf(buf, "%u\n", br->multicast_startup_query_count);
507 static int set_startup_query_count(struct net_bridge *br, unsigned long val)
509 br->multicast_startup_query_count = val;
510 return 0;
513 static ssize_t multicast_startup_query_count_store(
514 struct device *d, struct device_attribute *attr, const char *buf,
515 size_t len)
517 return store_bridge_parm(d, buf, len, set_startup_query_count);
519 static DEVICE_ATTR_RW(multicast_startup_query_count);
521 static ssize_t multicast_last_member_interval_show(
522 struct device *d, struct device_attribute *attr, char *buf)
524 struct net_bridge *br = to_bridge(d);
525 return sprintf(buf, "%lu\n",
526 jiffies_to_clock_t(br->multicast_last_member_interval));
529 static int set_last_member_interval(struct net_bridge *br, unsigned long val)
531 br->multicast_last_member_interval = clock_t_to_jiffies(val);
532 return 0;
535 static ssize_t multicast_last_member_interval_store(
536 struct device *d, struct device_attribute *attr, const char *buf,
537 size_t len)
539 return store_bridge_parm(d, buf, len, set_last_member_interval);
541 static DEVICE_ATTR_RW(multicast_last_member_interval);
543 static ssize_t multicast_membership_interval_show(
544 struct device *d, struct device_attribute *attr, char *buf)
546 struct net_bridge *br = to_bridge(d);
547 return sprintf(buf, "%lu\n",
548 jiffies_to_clock_t(br->multicast_membership_interval));
551 static int set_membership_interval(struct net_bridge *br, unsigned long val)
553 br->multicast_membership_interval = clock_t_to_jiffies(val);
554 return 0;
557 static ssize_t multicast_membership_interval_store(
558 struct device *d, struct device_attribute *attr, const char *buf,
559 size_t len)
561 return store_bridge_parm(d, buf, len, set_membership_interval);
563 static DEVICE_ATTR_RW(multicast_membership_interval);
565 static ssize_t multicast_querier_interval_show(struct device *d,
566 struct device_attribute *attr,
567 char *buf)
569 struct net_bridge *br = to_bridge(d);
570 return sprintf(buf, "%lu\n",
571 jiffies_to_clock_t(br->multicast_querier_interval));
574 static int set_querier_interval(struct net_bridge *br, unsigned long val)
576 br->multicast_querier_interval = clock_t_to_jiffies(val);
577 return 0;
580 static ssize_t multicast_querier_interval_store(struct device *d,
581 struct device_attribute *attr,
582 const char *buf, size_t len)
584 return store_bridge_parm(d, buf, len, set_querier_interval);
586 static DEVICE_ATTR_RW(multicast_querier_interval);
588 static ssize_t multicast_query_interval_show(struct device *d,
589 struct device_attribute *attr,
590 char *buf)
592 struct net_bridge *br = to_bridge(d);
593 return sprintf(buf, "%lu\n",
594 jiffies_to_clock_t(br->multicast_query_interval));
597 static int set_query_interval(struct net_bridge *br, unsigned long val)
599 br->multicast_query_interval = clock_t_to_jiffies(val);
600 return 0;
603 static ssize_t multicast_query_interval_store(struct device *d,
604 struct device_attribute *attr,
605 const char *buf, size_t len)
607 return store_bridge_parm(d, buf, len, set_query_interval);
609 static DEVICE_ATTR_RW(multicast_query_interval);
611 static ssize_t multicast_query_response_interval_show(
612 struct device *d, struct device_attribute *attr, char *buf)
614 struct net_bridge *br = to_bridge(d);
615 return sprintf(
616 buf, "%lu\n",
617 jiffies_to_clock_t(br->multicast_query_response_interval));
620 static int set_query_response_interval(struct net_bridge *br, unsigned long val)
622 br->multicast_query_response_interval = clock_t_to_jiffies(val);
623 return 0;
626 static ssize_t multicast_query_response_interval_store(
627 struct device *d, struct device_attribute *attr, const char *buf,
628 size_t len)
630 return store_bridge_parm(d, buf, len, set_query_response_interval);
632 static DEVICE_ATTR_RW(multicast_query_response_interval);
634 static ssize_t multicast_startup_query_interval_show(
635 struct device *d, struct device_attribute *attr, char *buf)
637 struct net_bridge *br = to_bridge(d);
638 return sprintf(
639 buf, "%lu\n",
640 jiffies_to_clock_t(br->multicast_startup_query_interval));
643 static int set_startup_query_interval(struct net_bridge *br, unsigned long val)
645 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
646 return 0;
649 static ssize_t multicast_startup_query_interval_store(
650 struct device *d, struct device_attribute *attr, const char *buf,
651 size_t len)
653 return store_bridge_parm(d, buf, len, set_startup_query_interval);
655 static DEVICE_ATTR_RW(multicast_startup_query_interval);
657 static ssize_t multicast_stats_enabled_show(struct device *d,
658 struct device_attribute *attr,
659 char *buf)
661 struct net_bridge *br = to_bridge(d);
663 return sprintf(buf, "%d\n",
664 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED));
667 static int set_stats_enabled(struct net_bridge *br, unsigned long val)
669 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!val);
670 return 0;
673 static ssize_t multicast_stats_enabled_store(struct device *d,
674 struct device_attribute *attr,
675 const char *buf,
676 size_t len)
678 return store_bridge_parm(d, buf, len, set_stats_enabled);
680 static DEVICE_ATTR_RW(multicast_stats_enabled);
682 #if IS_ENABLED(CONFIG_IPV6)
683 static ssize_t multicast_mld_version_show(struct device *d,
684 struct device_attribute *attr,
685 char *buf)
687 struct net_bridge *br = to_bridge(d);
689 return sprintf(buf, "%u\n", br->multicast_mld_version);
692 static ssize_t multicast_mld_version_store(struct device *d,
693 struct device_attribute *attr,
694 const char *buf, size_t len)
696 return store_bridge_parm(d, buf, len, br_multicast_set_mld_version);
698 static DEVICE_ATTR_RW(multicast_mld_version);
699 #endif
700 #endif
701 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
702 static ssize_t nf_call_iptables_show(
703 struct device *d, struct device_attribute *attr, char *buf)
705 struct net_bridge *br = to_bridge(d);
706 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_IPTABLES));
709 static int set_nf_call_iptables(struct net_bridge *br, unsigned long val)
711 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
712 return 0;
715 static ssize_t nf_call_iptables_store(
716 struct device *d, struct device_attribute *attr, const char *buf,
717 size_t len)
719 return store_bridge_parm(d, buf, len, set_nf_call_iptables);
721 static DEVICE_ATTR_RW(nf_call_iptables);
723 static ssize_t nf_call_ip6tables_show(
724 struct device *d, struct device_attribute *attr, char *buf)
726 struct net_bridge *br = to_bridge(d);
727 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_IP6TABLES));
730 static int set_nf_call_ip6tables(struct net_bridge *br, unsigned long val)
732 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
733 return 0;
736 static ssize_t nf_call_ip6tables_store(
737 struct device *d, struct device_attribute *attr, const char *buf,
738 size_t len)
740 return store_bridge_parm(d, buf, len, set_nf_call_ip6tables);
742 static DEVICE_ATTR_RW(nf_call_ip6tables);
744 static ssize_t nf_call_arptables_show(
745 struct device *d, struct device_attribute *attr, char *buf)
747 struct net_bridge *br = to_bridge(d);
748 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_NF_CALL_ARPTABLES));
751 static int set_nf_call_arptables(struct net_bridge *br, unsigned long val)
753 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
754 return 0;
757 static ssize_t nf_call_arptables_store(
758 struct device *d, struct device_attribute *attr, const char *buf,
759 size_t len)
761 return store_bridge_parm(d, buf, len, set_nf_call_arptables);
763 static DEVICE_ATTR_RW(nf_call_arptables);
764 #endif
765 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
766 static ssize_t vlan_filtering_show(struct device *d,
767 struct device_attribute *attr,
768 char *buf)
770 struct net_bridge *br = to_bridge(d);
771 return sprintf(buf, "%d\n", br_opt_get(br, BROPT_VLAN_ENABLED));
774 static ssize_t vlan_filtering_store(struct device *d,
775 struct device_attribute *attr,
776 const char *buf, size_t len)
778 return store_bridge_parm(d, buf, len, br_vlan_filter_toggle);
780 static DEVICE_ATTR_RW(vlan_filtering);
782 static ssize_t vlan_protocol_show(struct device *d,
783 struct device_attribute *attr,
784 char *buf)
786 struct net_bridge *br = to_bridge(d);
787 return sprintf(buf, "%#06x\n", ntohs(br->vlan_proto));
790 static ssize_t vlan_protocol_store(struct device *d,
791 struct device_attribute *attr,
792 const char *buf, size_t len)
794 return store_bridge_parm(d, buf, len, br_vlan_set_proto);
796 static DEVICE_ATTR_RW(vlan_protocol);
798 static ssize_t default_pvid_show(struct device *d,
799 struct device_attribute *attr,
800 char *buf)
802 struct net_bridge *br = to_bridge(d);
803 return sprintf(buf, "%d\n", br->default_pvid);
806 static ssize_t default_pvid_store(struct device *d,
807 struct device_attribute *attr,
808 const char *buf, size_t len)
810 return store_bridge_parm(d, buf, len, br_vlan_set_default_pvid);
812 static DEVICE_ATTR_RW(default_pvid);
814 static ssize_t vlan_stats_enabled_show(struct device *d,
815 struct device_attribute *attr,
816 char *buf)
818 struct net_bridge *br = to_bridge(d);
819 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_VLAN_STATS_ENABLED));
822 static ssize_t vlan_stats_enabled_store(struct device *d,
823 struct device_attribute *attr,
824 const char *buf, size_t len)
826 return store_bridge_parm(d, buf, len, br_vlan_set_stats);
828 static DEVICE_ATTR_RW(vlan_stats_enabled);
830 static ssize_t vlan_stats_per_port_show(struct device *d,
831 struct device_attribute *attr,
832 char *buf)
834 struct net_bridge *br = to_bridge(d);
835 return sprintf(buf, "%u\n", br_opt_get(br, BROPT_VLAN_STATS_PER_PORT));
838 static ssize_t vlan_stats_per_port_store(struct device *d,
839 struct device_attribute *attr,
840 const char *buf, size_t len)
842 return store_bridge_parm(d, buf, len, br_vlan_set_stats_per_port);
844 static DEVICE_ATTR_RW(vlan_stats_per_port);
845 #endif
847 static struct attribute *bridge_attrs[] = {
848 &dev_attr_forward_delay.attr,
849 &dev_attr_hello_time.attr,
850 &dev_attr_max_age.attr,
851 &dev_attr_ageing_time.attr,
852 &dev_attr_stp_state.attr,
853 &dev_attr_group_fwd_mask.attr,
854 &dev_attr_priority.attr,
855 &dev_attr_bridge_id.attr,
856 &dev_attr_root_id.attr,
857 &dev_attr_root_path_cost.attr,
858 &dev_attr_root_port.attr,
859 &dev_attr_topology_change.attr,
860 &dev_attr_topology_change_detected.attr,
861 &dev_attr_hello_timer.attr,
862 &dev_attr_tcn_timer.attr,
863 &dev_attr_topology_change_timer.attr,
864 &dev_attr_gc_timer.attr,
865 &dev_attr_group_addr.attr,
866 &dev_attr_flush.attr,
867 &dev_attr_no_linklocal_learn.attr,
868 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
869 &dev_attr_multicast_router.attr,
870 &dev_attr_multicast_snooping.attr,
871 &dev_attr_multicast_querier.attr,
872 &dev_attr_multicast_query_use_ifaddr.attr,
873 &dev_attr_hash_elasticity.attr,
874 &dev_attr_hash_max.attr,
875 &dev_attr_multicast_last_member_count.attr,
876 &dev_attr_multicast_startup_query_count.attr,
877 &dev_attr_multicast_last_member_interval.attr,
878 &dev_attr_multicast_membership_interval.attr,
879 &dev_attr_multicast_querier_interval.attr,
880 &dev_attr_multicast_query_interval.attr,
881 &dev_attr_multicast_query_response_interval.attr,
882 &dev_attr_multicast_startup_query_interval.attr,
883 &dev_attr_multicast_stats_enabled.attr,
884 &dev_attr_multicast_igmp_version.attr,
885 #if IS_ENABLED(CONFIG_IPV6)
886 &dev_attr_multicast_mld_version.attr,
887 #endif
888 #endif
889 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
890 &dev_attr_nf_call_iptables.attr,
891 &dev_attr_nf_call_ip6tables.attr,
892 &dev_attr_nf_call_arptables.attr,
893 #endif
894 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
895 &dev_attr_vlan_filtering.attr,
896 &dev_attr_vlan_protocol.attr,
897 &dev_attr_default_pvid.attr,
898 &dev_attr_vlan_stats_enabled.attr,
899 &dev_attr_vlan_stats_per_port.attr,
900 #endif
901 NULL
904 static const struct attribute_group bridge_group = {
905 .name = SYSFS_BRIDGE_ATTR,
906 .attrs = bridge_attrs,
910 * Export the forwarding information table as a binary file
911 * The records are struct __fdb_entry.
913 * Returns the number of bytes read.
915 static ssize_t brforward_read(struct file *filp, struct kobject *kobj,
916 struct bin_attribute *bin_attr,
917 char *buf, loff_t off, size_t count)
919 struct device *dev = kobj_to_dev(kobj);
920 struct net_bridge *br = to_bridge(dev);
921 int n;
923 /* must read whole records */
924 if (off % sizeof(struct __fdb_entry) != 0)
925 return -EINVAL;
927 n = br_fdb_fillbuf(br, buf,
928 count / sizeof(struct __fdb_entry),
929 off / sizeof(struct __fdb_entry));
931 if (n > 0)
932 n *= sizeof(struct __fdb_entry);
934 return n;
937 static struct bin_attribute bridge_forward = {
938 .attr = { .name = SYSFS_BRIDGE_FDB,
939 .mode = 0444, },
940 .read = brforward_read,
944 * Add entries in sysfs onto the existing network class device
945 * for the bridge.
946 * Adds a attribute group "bridge" containing tuning parameters.
947 * Binary attribute containing the forward table
948 * Sub directory to hold links to interfaces.
950 * Note: the ifobj exists only to be a subdirectory
951 * to hold links. The ifobj exists in same data structure
952 * as it's parent the bridge so reference counting works.
954 int br_sysfs_addbr(struct net_device *dev)
956 struct kobject *brobj = &dev->dev.kobj;
957 struct net_bridge *br = netdev_priv(dev);
958 int err;
960 err = sysfs_create_group(brobj, &bridge_group);
961 if (err) {
962 pr_info("%s: can't create group %s/%s\n",
963 __func__, dev->name, bridge_group.name);
964 goto out1;
967 err = sysfs_create_bin_file(brobj, &bridge_forward);
968 if (err) {
969 pr_info("%s: can't create attribute file %s/%s\n",
970 __func__, dev->name, bridge_forward.attr.name);
971 goto out2;
974 br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
975 if (!br->ifobj) {
976 pr_info("%s: can't add kobject (directory) %s/%s\n",
977 __func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
978 err = -ENOMEM;
979 goto out3;
981 return 0;
982 out3:
983 sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
984 out2:
985 sysfs_remove_group(&dev->dev.kobj, &bridge_group);
986 out1:
987 return err;
991 void br_sysfs_delbr(struct net_device *dev)
993 struct kobject *kobj = &dev->dev.kobj;
994 struct net_bridge *br = netdev_priv(dev);
996 kobject_put(br->ifobj);
997 sysfs_remove_bin_file(kobj, &bridge_forward);
998 sysfs_remove_group(kobj, &bridge_group);