xref: /openbmc/linux/net/bridge/br_sysfs_br.c (revision 561f1103a2b70de7e06e1e7fd072a5b142a4278c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Sysfs attributes of bridge ports
31da177e4SLinus Torvalds  *	Linux ethernet bridge
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Stephen Hemminger		<shemminger@osdl.org>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
144fc268d2SRandy Dunlap #include <linux/capability.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/netdevice.h>
171da177e4SLinus Torvalds #include <linux/if_bridge.h>
181da177e4SLinus Torvalds #include <linux/rtnetlink.h>
191da177e4SLinus Torvalds #include <linux/spinlock.h>
201da177e4SLinus Torvalds #include <linux/times.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "br_private.h"
231da177e4SLinus Torvalds 
2443cb76d9SGreg Kroah-Hartman #define to_dev(obj)	container_of(obj, struct device, kobj)
25524ad0a7SWang Chen #define to_bridge(cd)	((struct net_bridge *)netdev_priv(to_net_dev(cd)))
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /*
281da177e4SLinus Torvalds  * Common code for storing bridge parameters.
291da177e4SLinus Torvalds  */
3043cb76d9SGreg Kroah-Hartman static ssize_t store_bridge_parm(struct device *d,
311da177e4SLinus Torvalds 				 const char *buf, size_t len,
328d4698f7SStephen Hemminger 				 int (*set)(struct net_bridge *, unsigned long))
331da177e4SLinus Torvalds {
3443cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
351da177e4SLinus Torvalds 	char *endp;
361da177e4SLinus Torvalds 	unsigned long val;
378d4698f7SStephen Hemminger 	int err;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
401da177e4SLinus Torvalds 		return -EPERM;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	val = simple_strtoul(buf, &endp, 0);
431da177e4SLinus Torvalds 	if (endp == buf)
441da177e4SLinus Torvalds 		return -EINVAL;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	spin_lock_bh(&br->lock);
478d4698f7SStephen Hemminger 	err = (*set)(br, val);
481da177e4SLinus Torvalds 	spin_unlock_bh(&br->lock);
498d4698f7SStephen Hemminger 	return err ? err : len;
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 
5343cb76d9SGreg Kroah-Hartman static ssize_t show_forward_delay(struct device *d,
5443cb76d9SGreg Kroah-Hartman 				  struct device_attribute *attr, char *buf)
551da177e4SLinus Torvalds {
5643cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
571da177e4SLinus Torvalds 	return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
608d4698f7SStephen Hemminger static int set_forward_delay(struct net_bridge *br, unsigned long val)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	unsigned long delay = clock_t_to_jiffies(val);
631da177e4SLinus Torvalds 	br->forward_delay = delay;
641da177e4SLinus Torvalds 	if (br_is_root_bridge(br))
651da177e4SLinus Torvalds 		br->bridge_forward_delay = delay;
668d4698f7SStephen Hemminger 	return 0;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
6943cb76d9SGreg Kroah-Hartman static ssize_t store_forward_delay(struct device *d,
7043cb76d9SGreg Kroah-Hartman 				   struct device_attribute *attr,
7143cb76d9SGreg Kroah-Hartman 				   const char *buf, size_t len)
721da177e4SLinus Torvalds {
7343cb76d9SGreg Kroah-Hartman 	return store_bridge_parm(d, buf, len, set_forward_delay);
741da177e4SLinus Torvalds }
7543cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
761da177e4SLinus Torvalds 		   show_forward_delay, store_forward_delay);
771da177e4SLinus Torvalds 
7843cb76d9SGreg Kroah-Hartman static ssize_t show_hello_time(struct device *d, struct device_attribute *attr,
7943cb76d9SGreg Kroah-Hartman 			       char *buf)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	return sprintf(buf, "%lu\n",
8243cb76d9SGreg Kroah-Hartman 		       jiffies_to_clock_t(to_bridge(d)->hello_time));
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
858d4698f7SStephen Hemminger static int set_hello_time(struct net_bridge *br, unsigned long val)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	unsigned long t = clock_t_to_jiffies(val);
888d4698f7SStephen Hemminger 
898d4698f7SStephen Hemminger 	if (t < HZ)
908d4698f7SStephen Hemminger 		return -EINVAL;
918d4698f7SStephen Hemminger 
921da177e4SLinus Torvalds 	br->hello_time = t;
931da177e4SLinus Torvalds 	if (br_is_root_bridge(br))
941da177e4SLinus Torvalds 		br->bridge_hello_time = t;
958d4698f7SStephen Hemminger 	return 0;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
9843cb76d9SGreg Kroah-Hartman static ssize_t store_hello_time(struct device *d,
9943cb76d9SGreg Kroah-Hartman 				struct device_attribute *attr, const char *buf,
1001da177e4SLinus Torvalds 				size_t len)
1011da177e4SLinus Torvalds {
10243cb76d9SGreg Kroah-Hartman 	return store_bridge_parm(d, buf, len, set_hello_time);
1031da177e4SLinus Torvalds }
10443cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
1051da177e4SLinus Torvalds 		   store_hello_time);
1061da177e4SLinus Torvalds 
10743cb76d9SGreg Kroah-Hartman static ssize_t show_max_age(struct device *d, struct device_attribute *attr,
10843cb76d9SGreg Kroah-Hartman 			    char *buf)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	return sprintf(buf, "%lu\n",
11143cb76d9SGreg Kroah-Hartman 		       jiffies_to_clock_t(to_bridge(d)->max_age));
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1148d4698f7SStephen Hemminger static int set_max_age(struct net_bridge *br, unsigned long val)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	unsigned long t = clock_t_to_jiffies(val);
1171da177e4SLinus Torvalds 	br->max_age = t;
1181da177e4SLinus Torvalds 	if (br_is_root_bridge(br))
1191da177e4SLinus Torvalds 		br->bridge_max_age = t;
1208d4698f7SStephen Hemminger 	return 0;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
12343cb76d9SGreg Kroah-Hartman static ssize_t store_max_age(struct device *d, struct device_attribute *attr,
12443cb76d9SGreg Kroah-Hartman 			     const char *buf, size_t len)
1251da177e4SLinus Torvalds {
12643cb76d9SGreg Kroah-Hartman 	return store_bridge_parm(d, buf, len, set_max_age);
1271da177e4SLinus Torvalds }
12843cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
1291da177e4SLinus Torvalds 
13043cb76d9SGreg Kroah-Hartman static ssize_t show_ageing_time(struct device *d,
13143cb76d9SGreg Kroah-Hartman 				struct device_attribute *attr, char *buf)
1321da177e4SLinus Torvalds {
13343cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
1341da177e4SLinus Torvalds 	return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1378d4698f7SStephen Hemminger static int set_ageing_time(struct net_bridge *br, unsigned long val)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	br->ageing_time = clock_t_to_jiffies(val);
1408d4698f7SStephen Hemminger 	return 0;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
14343cb76d9SGreg Kroah-Hartman static ssize_t store_ageing_time(struct device *d,
14443cb76d9SGreg Kroah-Hartman 				 struct device_attribute *attr,
14543cb76d9SGreg Kroah-Hartman 				 const char *buf, size_t len)
1461da177e4SLinus Torvalds {
14743cb76d9SGreg Kroah-Hartman 	return store_bridge_parm(d, buf, len, set_ageing_time);
1481da177e4SLinus Torvalds }
14943cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
1501da177e4SLinus Torvalds 		   store_ageing_time);
15143cb76d9SGreg Kroah-Hartman 
15243cb76d9SGreg Kroah-Hartman static ssize_t show_stp_state(struct device *d,
15343cb76d9SGreg Kroah-Hartman 			      struct device_attribute *attr, char *buf)
1541da177e4SLinus Torvalds {
15543cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
1561da177e4SLinus Torvalds 	return sprintf(buf, "%d\n", br->stp_enabled);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 
16043cb76d9SGreg Kroah-Hartman static ssize_t store_stp_state(struct device *d,
16143cb76d9SGreg Kroah-Hartman 			       struct device_attribute *attr, const char *buf,
16243cb76d9SGreg Kroah-Hartman 			       size_t len)
1631da177e4SLinus Torvalds {
16417120889SStephen Hemminger 	struct net_bridge *br = to_bridge(d);
16517120889SStephen Hemminger 	char *endp;
16617120889SStephen Hemminger 	unsigned long val;
16717120889SStephen Hemminger 
16817120889SStephen Hemminger 	if (!capable(CAP_NET_ADMIN))
16917120889SStephen Hemminger 		return -EPERM;
17017120889SStephen Hemminger 
17117120889SStephen Hemminger 	val = simple_strtoul(buf, &endp, 0);
17217120889SStephen Hemminger 	if (endp == buf)
17317120889SStephen Hemminger 		return -EINVAL;
17417120889SStephen Hemminger 
175af38f298SEric W. Biederman 	if (!rtnl_trylock())
176af38f298SEric W. Biederman 		return restart_syscall();
17717120889SStephen Hemminger 	br_stp_set_enabled(br, val);
17817120889SStephen Hemminger 	rtnl_unlock();
17917120889SStephen Hemminger 
18035b426c3SAl Viro 	return len;
1811da177e4SLinus Torvalds }
18243cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
1831da177e4SLinus Torvalds 		   store_stp_state);
1841da177e4SLinus Torvalds 
18543cb76d9SGreg Kroah-Hartman static ssize_t show_priority(struct device *d, struct device_attribute *attr,
18643cb76d9SGreg Kroah-Hartman 			     char *buf)
1871da177e4SLinus Torvalds {
18843cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
1891da177e4SLinus Torvalds 	return sprintf(buf, "%d\n",
1901da177e4SLinus Torvalds 		       (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1938d4698f7SStephen Hemminger static int set_priority(struct net_bridge *br, unsigned long val)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	br_stp_set_bridge_priority(br, (u16) val);
1968d4698f7SStephen Hemminger 	return 0;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
19943cb76d9SGreg Kroah-Hartman static ssize_t store_priority(struct device *d, struct device_attribute *attr,
2001da177e4SLinus Torvalds 			       const char *buf, size_t len)
2011da177e4SLinus Torvalds {
20243cb76d9SGreg Kroah-Hartman 	return store_bridge_parm(d, buf, len, set_priority);
2031da177e4SLinus Torvalds }
20443cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
2051da177e4SLinus Torvalds 
20643cb76d9SGreg Kroah-Hartman static ssize_t show_root_id(struct device *d, struct device_attribute *attr,
20743cb76d9SGreg Kroah-Hartman 			    char *buf)
2081da177e4SLinus Torvalds {
20943cb76d9SGreg Kroah-Hartman 	return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
2101da177e4SLinus Torvalds }
21143cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
2121da177e4SLinus Torvalds 
21343cb76d9SGreg Kroah-Hartman static ssize_t show_bridge_id(struct device *d, struct device_attribute *attr,
21443cb76d9SGreg Kroah-Hartman 			      char *buf)
2151da177e4SLinus Torvalds {
21643cb76d9SGreg Kroah-Hartman 	return br_show_bridge_id(buf, &to_bridge(d)->bridge_id);
2171da177e4SLinus Torvalds }
21843cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
2191da177e4SLinus Torvalds 
22043cb76d9SGreg Kroah-Hartman static ssize_t show_root_port(struct device *d, struct device_attribute *attr,
22143cb76d9SGreg Kroah-Hartman 			      char *buf)
2221da177e4SLinus Torvalds {
22343cb76d9SGreg Kroah-Hartman 	return sprintf(buf, "%d\n", to_bridge(d)->root_port);
2241da177e4SLinus Torvalds }
22543cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
2261da177e4SLinus Torvalds 
22743cb76d9SGreg Kroah-Hartman static ssize_t show_root_path_cost(struct device *d,
22843cb76d9SGreg Kroah-Hartman 				   struct device_attribute *attr, char *buf)
2291da177e4SLinus Torvalds {
23043cb76d9SGreg Kroah-Hartman 	return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
2311da177e4SLinus Torvalds }
23243cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
2331da177e4SLinus Torvalds 
23443cb76d9SGreg Kroah-Hartman static ssize_t show_topology_change(struct device *d,
23543cb76d9SGreg Kroah-Hartman 				    struct device_attribute *attr, char *buf)
2361da177e4SLinus Torvalds {
23743cb76d9SGreg Kroah-Hartman 	return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
2381da177e4SLinus Torvalds }
23943cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
2401da177e4SLinus Torvalds 
24143cb76d9SGreg Kroah-Hartman static ssize_t show_topology_change_detected(struct device *d,
24243cb76d9SGreg Kroah-Hartman 					     struct device_attribute *attr,
24343cb76d9SGreg Kroah-Hartman 					     char *buf)
2441da177e4SLinus Torvalds {
24543cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
2461da177e4SLinus Torvalds 	return sprintf(buf, "%d\n", br->topology_change_detected);
2471da177e4SLinus Torvalds }
24843cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(topology_change_detected, S_IRUGO,
24943cb76d9SGreg Kroah-Hartman 		   show_topology_change_detected, NULL);
2501da177e4SLinus Torvalds 
25143cb76d9SGreg Kroah-Hartman static ssize_t show_hello_timer(struct device *d,
25243cb76d9SGreg Kroah-Hartman 				struct device_attribute *attr, char *buf)
2531da177e4SLinus Torvalds {
25443cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
2551da177e4SLinus Torvalds 	return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
2561da177e4SLinus Torvalds }
25743cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
2581da177e4SLinus Torvalds 
25943cb76d9SGreg Kroah-Hartman static ssize_t show_tcn_timer(struct device *d, struct device_attribute *attr,
26043cb76d9SGreg Kroah-Hartman 			      char *buf)
2611da177e4SLinus Torvalds {
26243cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
2631da177e4SLinus Torvalds 	return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
2641da177e4SLinus Torvalds }
26543cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
2661da177e4SLinus Torvalds 
26743cb76d9SGreg Kroah-Hartman static ssize_t show_topology_change_timer(struct device *d,
26843cb76d9SGreg Kroah-Hartman 					  struct device_attribute *attr,
26943cb76d9SGreg Kroah-Hartman 					  char *buf)
2701da177e4SLinus Torvalds {
27143cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
2721da177e4SLinus Torvalds 	return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
2731da177e4SLinus Torvalds }
27443cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
27543cb76d9SGreg Kroah-Hartman 		   NULL);
2761da177e4SLinus Torvalds 
27743cb76d9SGreg Kroah-Hartman static ssize_t show_gc_timer(struct device *d, struct device_attribute *attr,
27843cb76d9SGreg Kroah-Hartman 			     char *buf)
2791da177e4SLinus Torvalds {
28043cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
2811da177e4SLinus Torvalds 	return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
2821da177e4SLinus Torvalds }
28343cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
2841da177e4SLinus Torvalds 
28543cb76d9SGreg Kroah-Hartman static ssize_t show_group_addr(struct device *d,
28643cb76d9SGreg Kroah-Hartman 			       struct device_attribute *attr, char *buf)
287fda93d92SStephen Hemminger {
28843cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
289fda93d92SStephen Hemminger 	return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
290fda93d92SStephen Hemminger 		       br->group_addr[0], br->group_addr[1],
291fda93d92SStephen Hemminger 		       br->group_addr[2], br->group_addr[3],
292fda93d92SStephen Hemminger 		       br->group_addr[4], br->group_addr[5]);
293fda93d92SStephen Hemminger }
294fda93d92SStephen Hemminger 
29543cb76d9SGreg Kroah-Hartman static ssize_t store_group_addr(struct device *d,
29643cb76d9SGreg Kroah-Hartman 				struct device_attribute *attr,
29743cb76d9SGreg Kroah-Hartman 				const char *buf, size_t len)
298fda93d92SStephen Hemminger {
29943cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(d);
300fda93d92SStephen Hemminger 	unsigned new_addr[6];
301fda93d92SStephen Hemminger 	int i;
302fda93d92SStephen Hemminger 
303fda93d92SStephen Hemminger 	if (!capable(CAP_NET_ADMIN))
304fda93d92SStephen Hemminger 		return -EPERM;
305fda93d92SStephen Hemminger 
306fda93d92SStephen Hemminger 	if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
307fda93d92SStephen Hemminger 		   &new_addr[0], &new_addr[1], &new_addr[2],
308fda93d92SStephen Hemminger 		   &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
309fda93d92SStephen Hemminger 		return -EINVAL;
310fda93d92SStephen Hemminger 
311fda93d92SStephen Hemminger 	/* Must be 01:80:c2:00:00:0X */
312fda93d92SStephen Hemminger 	for (i = 0; i < 5; i++)
313fda93d92SStephen Hemminger 		if (new_addr[i] != br_group_address[i])
314fda93d92SStephen Hemminger 			return -EINVAL;
315fda93d92SStephen Hemminger 
316fda93d92SStephen Hemminger 	if (new_addr[5] & ~0xf)
317fda93d92SStephen Hemminger 		return -EINVAL;
318fda93d92SStephen Hemminger 
319f64f9e71SJoe Perches 	if (new_addr[5] == 1 ||		/* 802.3x Pause address */
320f64f9e71SJoe Perches 	    new_addr[5] == 2 ||		/* 802.3ad Slow protocols */
321f64f9e71SJoe Perches 	    new_addr[5] == 3)		/* 802.1X PAE address */
322fda93d92SStephen Hemminger 		return -EINVAL;
323fda93d92SStephen Hemminger 
324fda93d92SStephen Hemminger 	spin_lock_bh(&br->lock);
325fda93d92SStephen Hemminger 	for (i = 0; i < 6; i++)
326fda93d92SStephen Hemminger 		br->group_addr[i] = new_addr[i];
327fda93d92SStephen Hemminger 	spin_unlock_bh(&br->lock);
328fda93d92SStephen Hemminger 	return len;
329fda93d92SStephen Hemminger }
330fda93d92SStephen Hemminger 
33143cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
332fda93d92SStephen Hemminger 		   show_group_addr, store_group_addr);
333fda93d92SStephen Hemminger 
3349cf63747SStephen Hemminger static ssize_t store_flush(struct device *d,
3359cf63747SStephen Hemminger 			   struct device_attribute *attr,
3369cf63747SStephen Hemminger 			   const char *buf, size_t len)
3379cf63747SStephen Hemminger {
3389cf63747SStephen Hemminger 	struct net_bridge *br = to_bridge(d);
3399cf63747SStephen Hemminger 
3409cf63747SStephen Hemminger 	if (!capable(CAP_NET_ADMIN))
3419cf63747SStephen Hemminger 		return -EPERM;
3429cf63747SStephen Hemminger 
3439cf63747SStephen Hemminger 	br_fdb_flush(br);
3449cf63747SStephen Hemminger 	return len;
3459cf63747SStephen Hemminger }
3469cf63747SStephen Hemminger static DEVICE_ATTR(flush, S_IWUSR, NULL, store_flush);
347fda93d92SStephen Hemminger 
3480909e117SHerbert Xu #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
3490909e117SHerbert Xu static ssize_t show_multicast_router(struct device *d,
3500909e117SHerbert Xu 				     struct device_attribute *attr, char *buf)
3510909e117SHerbert Xu {
3520909e117SHerbert Xu 	struct net_bridge *br = to_bridge(d);
3530909e117SHerbert Xu 	return sprintf(buf, "%d\n", br->multicast_router);
3540909e117SHerbert Xu }
3550909e117SHerbert Xu 
3560909e117SHerbert Xu static ssize_t store_multicast_router(struct device *d,
3570909e117SHerbert Xu 				      struct device_attribute *attr,
3580909e117SHerbert Xu 				      const char *buf, size_t len)
3590909e117SHerbert Xu {
3600909e117SHerbert Xu 	return store_bridge_parm(d, buf, len, br_multicast_set_router);
3610909e117SHerbert Xu }
3620909e117SHerbert Xu static DEVICE_ATTR(multicast_router, S_IRUGO | S_IWUSR, show_multicast_router,
3630909e117SHerbert Xu 		   store_multicast_router);
364*561f1103SHerbert Xu 
365*561f1103SHerbert Xu static ssize_t show_multicast_snooping(struct device *d,
366*561f1103SHerbert Xu 				       struct device_attribute *attr,
367*561f1103SHerbert Xu 				       char *buf)
368*561f1103SHerbert Xu {
369*561f1103SHerbert Xu 	struct net_bridge *br = to_bridge(d);
370*561f1103SHerbert Xu 	return sprintf(buf, "%d\n", !br->multicast_disabled);
371*561f1103SHerbert Xu }
372*561f1103SHerbert Xu 
373*561f1103SHerbert Xu static ssize_t store_multicast_snooping(struct device *d,
374*561f1103SHerbert Xu 					struct device_attribute *attr,
375*561f1103SHerbert Xu 					const char *buf, size_t len)
376*561f1103SHerbert Xu {
377*561f1103SHerbert Xu 	return store_bridge_parm(d, buf, len, br_multicast_toggle);
378*561f1103SHerbert Xu }
379*561f1103SHerbert Xu static DEVICE_ATTR(multicast_snooping, S_IRUGO | S_IWUSR,
380*561f1103SHerbert Xu 		   show_multicast_snooping, store_multicast_snooping);
3810909e117SHerbert Xu #endif
3820909e117SHerbert Xu 
3831da177e4SLinus Torvalds static struct attribute *bridge_attrs[] = {
38443cb76d9SGreg Kroah-Hartman 	&dev_attr_forward_delay.attr,
38543cb76d9SGreg Kroah-Hartman 	&dev_attr_hello_time.attr,
38643cb76d9SGreg Kroah-Hartman 	&dev_attr_max_age.attr,
38743cb76d9SGreg Kroah-Hartman 	&dev_attr_ageing_time.attr,
38843cb76d9SGreg Kroah-Hartman 	&dev_attr_stp_state.attr,
38943cb76d9SGreg Kroah-Hartman 	&dev_attr_priority.attr,
39043cb76d9SGreg Kroah-Hartman 	&dev_attr_bridge_id.attr,
39143cb76d9SGreg Kroah-Hartman 	&dev_attr_root_id.attr,
39243cb76d9SGreg Kroah-Hartman 	&dev_attr_root_path_cost.attr,
39343cb76d9SGreg Kroah-Hartman 	&dev_attr_root_port.attr,
39443cb76d9SGreg Kroah-Hartman 	&dev_attr_topology_change.attr,
39543cb76d9SGreg Kroah-Hartman 	&dev_attr_topology_change_detected.attr,
39643cb76d9SGreg Kroah-Hartman 	&dev_attr_hello_timer.attr,
39743cb76d9SGreg Kroah-Hartman 	&dev_attr_tcn_timer.attr,
39843cb76d9SGreg Kroah-Hartman 	&dev_attr_topology_change_timer.attr,
39943cb76d9SGreg Kroah-Hartman 	&dev_attr_gc_timer.attr,
40043cb76d9SGreg Kroah-Hartman 	&dev_attr_group_addr.attr,
4019cf63747SStephen Hemminger 	&dev_attr_flush.attr,
4020909e117SHerbert Xu #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
4030909e117SHerbert Xu 	&dev_attr_multicast_router.attr,
404*561f1103SHerbert Xu 	&dev_attr_multicast_snooping.attr,
4050909e117SHerbert Xu #endif
4061da177e4SLinus Torvalds 	NULL
4071da177e4SLinus Torvalds };
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds static struct attribute_group bridge_group = {
4101da177e4SLinus Torvalds 	.name = SYSFS_BRIDGE_ATTR,
4111da177e4SLinus Torvalds 	.attrs = bridge_attrs,
4121da177e4SLinus Torvalds };
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  * Export the forwarding information table as a binary file
4161da177e4SLinus Torvalds  * The records are struct __fdb_entry.
4171da177e4SLinus Torvalds  *
4181da177e4SLinus Torvalds  * Returns the number of bytes read.
4191da177e4SLinus Torvalds  */
42091a69029SZhang Rui static ssize_t brforward_read(struct kobject *kobj,
42191a69029SZhang Rui 			      struct bin_attribute *bin_attr,
42291a69029SZhang Rui 			      char *buf, loff_t off, size_t count)
4231da177e4SLinus Torvalds {
42443cb76d9SGreg Kroah-Hartman 	struct device *dev = to_dev(kobj);
42543cb76d9SGreg Kroah-Hartman 	struct net_bridge *br = to_bridge(dev);
4261da177e4SLinus Torvalds 	int n;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/* must read whole records */
4291da177e4SLinus Torvalds 	if (off % sizeof(struct __fdb_entry) != 0)
4301da177e4SLinus Torvalds 		return -EINVAL;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	n =  br_fdb_fillbuf(br, buf,
4331da177e4SLinus Torvalds 			    count / sizeof(struct __fdb_entry),
4341da177e4SLinus Torvalds 			    off / sizeof(struct __fdb_entry));
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (n > 0)
4371da177e4SLinus Torvalds 		n *= sizeof(struct __fdb_entry);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	return n;
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds static struct bin_attribute bridge_forward = {
4431da177e4SLinus Torvalds 	.attr = { .name = SYSFS_BRIDGE_FDB,
4447b595756STejun Heo 		  .mode = S_IRUGO, },
4451da177e4SLinus Torvalds 	.read = brforward_read,
4461da177e4SLinus Torvalds };
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds /*
4491da177e4SLinus Torvalds  * Add entries in sysfs onto the existing network class device
4501da177e4SLinus Torvalds  * for the bridge.
4511da177e4SLinus Torvalds  *   Adds a attribute group "bridge" containing tuning parameters.
4521da177e4SLinus Torvalds  *   Binary attribute containing the forward table
4531da177e4SLinus Torvalds  *   Sub directory to hold links to interfaces.
4541da177e4SLinus Torvalds  *
4551da177e4SLinus Torvalds  * Note: the ifobj exists only to be a subdirectory
4561da177e4SLinus Torvalds  *   to hold links.  The ifobj exists in same data structure
4571da177e4SLinus Torvalds  *   as it's parent the bridge so reference counting works.
4581da177e4SLinus Torvalds  */
4591da177e4SLinus Torvalds int br_sysfs_addbr(struct net_device *dev)
4601da177e4SLinus Torvalds {
46143cb76d9SGreg Kroah-Hartman 	struct kobject *brobj = &dev->dev.kobj;
4621da177e4SLinus Torvalds 	struct net_bridge *br = netdev_priv(dev);
4631da177e4SLinus Torvalds 	int err;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	err = sysfs_create_group(brobj, &bridge_group);
4661da177e4SLinus Torvalds 	if (err) {
4671da177e4SLinus Torvalds 		pr_info("%s: can't create group %s/%s\n",
4680dc47877SHarvey Harrison 			__func__, dev->name, bridge_group.name);
4691da177e4SLinus Torvalds 		goto out1;
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	err = sysfs_create_bin_file(brobj, &bridge_forward);
4731da177e4SLinus Torvalds 	if (err) {
4741842c4beSRandy Dunlap 		pr_info("%s: can't create attribute file %s/%s\n",
4750dc47877SHarvey Harrison 			__func__, dev->name, bridge_forward.attr.name);
4761da177e4SLinus Torvalds 		goto out2;
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
47943b98c4aSGreg Kroah-Hartman 	br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
48043b98c4aSGreg Kroah-Hartman 	if (!br->ifobj) {
4811da177e4SLinus Torvalds 		pr_info("%s: can't add kobject (directory) %s/%s\n",
4820dc47877SHarvey Harrison 			__func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
4831da177e4SLinus Torvalds 		goto out3;
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 	return 0;
4861da177e4SLinus Torvalds  out3:
48743cb76d9SGreg Kroah-Hartman 	sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
4881da177e4SLinus Torvalds  out2:
48943cb76d9SGreg Kroah-Hartman 	sysfs_remove_group(&dev->dev.kobj, &bridge_group);
4901da177e4SLinus Torvalds  out1:
4911da177e4SLinus Torvalds 	return err;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds void br_sysfs_delbr(struct net_device *dev)
4961da177e4SLinus Torvalds {
49743cb76d9SGreg Kroah-Hartman 	struct kobject *kobj = &dev->dev.kobj;
4981da177e4SLinus Torvalds 	struct net_bridge *br = netdev_priv(dev);
4991da177e4SLinus Torvalds 
50078a2d906SGreg Kroah-Hartman 	kobject_put(br->ifobj);
5011da177e4SLinus Torvalds 	sysfs_remove_bin_file(kobj, &bridge_forward);
5021da177e4SLinus Torvalds 	sysfs_remove_group(kobj, &bridge_group);
5031da177e4SLinus Torvalds }
504