blob: e157b0dbcd8219c721b3e08e57821e7504d80338 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Sysfs attributes of bridge ports
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Stephen Hemminger <shemminger@osdl.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
Randy Dunlap4fc268d2006-01-11 12:17:47 -080014#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/netdevice.h>
John Fastabendb3343a22012-09-18 00:01:12 +000017#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/if_bridge.h>
19#include <linux/rtnetlink.h>
20#include <linux/spinlock.h>
21#include <linux/times.h>
22
23#include "br_private.h"
24
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070025#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen524ad0a2008-11-12 23:39:10 -080026#define to_bridge(cd) ((struct net_bridge *)netdev_priv(to_net_dev(cd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Common code for storing bridge parameters.
30 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070031static ssize_t store_bridge_parm(struct device *d,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 const char *buf, size_t len,
Stephen Hemminger8d4698f2008-09-08 13:44:40 -070033 int (*set)(struct net_bridge *, unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070035 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 char *endp;
37 unsigned long val;
Stephen Hemminger8d4698f2008-09-08 13:44:40 -070038 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 if (!capable(CAP_NET_ADMIN))
41 return -EPERM;
42
43 val = simple_strtoul(buf, &endp, 0);
44 if (endp == buf)
45 return -EINVAL;
46
Stephen Hemminger8d4698f2008-09-08 13:44:40 -070047 err = (*set)(br, val);
Stephen Hemminger8d4698f2008-09-08 13:44:40 -070048 return err ? err : len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070052static ssize_t show_forward_delay(struct device *d,
53 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070055 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
57}
58
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070059static ssize_t store_forward_delay(struct device *d,
60 struct device_attribute *attr,
61 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
stephen hemminger14f98f22011-04-04 14:03:33 +000063 return store_bridge_parm(d, buf, len, br_set_forward_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070065static DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
66 show_forward_delay, store_forward_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070068static ssize_t show_hello_time(struct device *d, struct device_attribute *attr,
69 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 return sprintf(buf, "%lu\n",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070072 jiffies_to_clock_t(to_bridge(d)->hello_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070075static ssize_t store_hello_time(struct device *d,
76 struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 size_t len)
78{
stephen hemminger14f98f22011-04-04 14:03:33 +000079 return store_bridge_parm(d, buf, len, br_set_hello_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070081static DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
82 store_hello_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070084static ssize_t show_max_age(struct device *d, struct device_attribute *attr,
85 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 return sprintf(buf, "%lu\n",
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070088 jiffies_to_clock_t(to_bridge(d)->max_age));
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070091static ssize_t store_max_age(struct device *d, struct device_attribute *attr,
92 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
stephen hemminger14f98f22011-04-04 14:03:33 +000094 return store_bridge_parm(d, buf, len, br_set_max_age);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070096static DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070098static ssize_t show_ageing_time(struct device *d,
99 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700101 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
103}
104
Stephen Hemminger8d4698f2008-09-08 13:44:40 -0700105static int set_ageing_time(struct net_bridge *br, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 br->ageing_time = clock_t_to_jiffies(val);
Stephen Hemminger8d4698f2008-09-08 13:44:40 -0700108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700111static ssize_t store_ageing_time(struct device *d,
112 struct device_attribute *attr,
113 const char *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700115 return store_bridge_parm(d, buf, len, set_ageing_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700117static DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
118 store_ageing_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700120static ssize_t show_stp_state(struct device *d,
121 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700123 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return sprintf(buf, "%d\n", br->stp_enabled);
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700128static ssize_t store_stp_state(struct device *d,
129 struct device_attribute *attr, const char *buf,
130 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Stephen Hemminger17120882007-08-14 13:21:34 -0700132 struct net_bridge *br = to_bridge(d);
133 char *endp;
134 unsigned long val;
135
136 if (!capable(CAP_NET_ADMIN))
137 return -EPERM;
138
139 val = simple_strtoul(buf, &endp, 0);
140 if (endp == buf)
141 return -EINVAL;
142
Eric W. Biedermanaf38f292009-05-13 17:00:41 +0000143 if (!rtnl_trylock())
144 return restart_syscall();
Stephen Hemminger17120882007-08-14 13:21:34 -0700145 br_stp_set_enabled(br, val);
146 rtnl_unlock();
147
Al Viro35b426c2007-08-19 04:51:26 +0100148 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700150static DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
151 store_stp_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
stephen hemminger515853c2011-10-03 18:14:46 +0000153static ssize_t show_group_fwd_mask(struct device *d,
154 struct device_attribute *attr, char *buf)
155{
156 struct net_bridge *br = to_bridge(d);
157 return sprintf(buf, "%#x\n", br->group_fwd_mask);
158}
159
160
161static ssize_t store_group_fwd_mask(struct device *d,
162 struct device_attribute *attr, const char *buf,
163 size_t len)
164{
165 struct net_bridge *br = to_bridge(d);
166 char *endp;
167 unsigned long val;
168
169 if (!capable(CAP_NET_ADMIN))
170 return -EPERM;
171
172 val = simple_strtoul(buf, &endp, 0);
173 if (endp == buf)
174 return -EINVAL;
175
176 if (val & BR_GROUPFWD_RESTRICTED)
177 return -EINVAL;
178
179 br->group_fwd_mask = val;
180
181 return len;
182}
183static DEVICE_ATTR(group_fwd_mask, S_IRUGO | S_IWUSR, show_group_fwd_mask,
184 store_group_fwd_mask);
185
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700186static ssize_t show_priority(struct device *d, struct device_attribute *attr,
187 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700189 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return sprintf(buf, "%d\n",
191 (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
192}
193
Stephen Hemminger8d4698f2008-09-08 13:44:40 -0700194static int set_priority(struct net_bridge *br, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 br_stp_set_bridge_priority(br, (u16) val);
Stephen Hemminger8d4698f2008-09-08 13:44:40 -0700197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700200static ssize_t store_priority(struct device *d, struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 const char *buf, size_t len)
202{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700203 return store_bridge_parm(d, buf, len, set_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700205static DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700207static ssize_t show_root_id(struct device *d, struct device_attribute *attr,
208 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700210 return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700212static DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700214static ssize_t show_bridge_id(struct device *d, struct device_attribute *attr,
215 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700217 return br_show_bridge_id(buf, &to_bridge(d)->bridge_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700219static DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700221static ssize_t show_root_port(struct device *d, struct device_attribute *attr,
222 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700224 return sprintf(buf, "%d\n", to_bridge(d)->root_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700226static DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700228static ssize_t show_root_path_cost(struct device *d,
229 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700231 return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700233static DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700235static ssize_t show_topology_change(struct device *d,
236 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700238 return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700240static DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700242static ssize_t show_topology_change_detected(struct device *d,
243 struct device_attribute *attr,
244 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700246 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return sprintf(buf, "%d\n", br->topology_change_detected);
248}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700249static DEVICE_ATTR(topology_change_detected, S_IRUGO,
250 show_topology_change_detected, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700252static ssize_t show_hello_timer(struct device *d,
253 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700255 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
257}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700258static DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700260static ssize_t show_tcn_timer(struct device *d, struct device_attribute *attr,
261 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700263 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
265}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700266static DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700268static ssize_t show_topology_change_timer(struct device *d,
269 struct device_attribute *attr,
270 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700272 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
274}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700275static DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
276 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700278static ssize_t show_gc_timer(struct device *d, struct device_attribute *attr,
279 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700281 struct net_bridge *br = to_bridge(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
283}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700284static DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700286static ssize_t show_group_addr(struct device *d,
287 struct device_attribute *attr, char *buf)
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800288{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700289 struct net_bridge *br = to_bridge(d);
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800290 return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
291 br->group_addr[0], br->group_addr[1],
292 br->group_addr[2], br->group_addr[3],
293 br->group_addr[4], br->group_addr[5]);
294}
295
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700296static ssize_t store_group_addr(struct device *d,
297 struct device_attribute *attr,
298 const char *buf, size_t len)
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800299{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700300 struct net_bridge *br = to_bridge(d);
Eric Dumazet95c96172012-04-15 05:58:06 +0000301 unsigned int new_addr[6];
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800302 int i;
303
304 if (!capable(CAP_NET_ADMIN))
305 return -EPERM;
306
307 if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
308 &new_addr[0], &new_addr[1], &new_addr[2],
309 &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
310 return -EINVAL;
311
312 /* Must be 01:80:c2:00:00:0X */
313 for (i = 0; i < 5; i++)
John Fastabendb3343a22012-09-18 00:01:12 +0000314 if (new_addr[i] != br_reserved_address[i])
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800315 return -EINVAL;
316
317 if (new_addr[5] & ~0xf)
318 return -EINVAL;
319
Joe Perchesf64f9e72009-11-29 16:55:45 -0800320 if (new_addr[5] == 1 || /* 802.3x Pause address */
321 new_addr[5] == 2 || /* 802.3ad Slow protocols */
322 new_addr[5] == 3) /* 802.1X PAE address */
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800323 return -EINVAL;
324
325 spin_lock_bh(&br->lock);
326 for (i = 0; i < 6; i++)
327 br->group_addr[i] = new_addr[i];
328 spin_unlock_bh(&br->lock);
329 return len;
330}
331
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700332static DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
333 show_group_addr, store_group_addr);
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800334
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700335static ssize_t store_flush(struct device *d,
336 struct device_attribute *attr,
337 const char *buf, size_t len)
338{
339 struct net_bridge *br = to_bridge(d);
340
341 if (!capable(CAP_NET_ADMIN))
342 return -EPERM;
343
344 br_fdb_flush(br);
345 return len;
346}
347static DEVICE_ATTR(flush, S_IWUSR, NULL, store_flush);
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800348
Herbert Xu0909e112010-02-27 19:41:49 +0000349#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
350static ssize_t show_multicast_router(struct device *d,
351 struct device_attribute *attr, char *buf)
352{
353 struct net_bridge *br = to_bridge(d);
354 return sprintf(buf, "%d\n", br->multicast_router);
355}
356
357static ssize_t store_multicast_router(struct device *d,
358 struct device_attribute *attr,
359 const char *buf, size_t len)
360{
361 return store_bridge_parm(d, buf, len, br_multicast_set_router);
362}
363static DEVICE_ATTR(multicast_router, S_IRUGO | S_IWUSR, show_multicast_router,
364 store_multicast_router);
Herbert Xu561f1102010-02-27 19:41:50 +0000365
366static ssize_t show_multicast_snooping(struct device *d,
367 struct device_attribute *attr,
368 char *buf)
369{
370 struct net_bridge *br = to_bridge(d);
371 return sprintf(buf, "%d\n", !br->multicast_disabled);
372}
373
374static ssize_t store_multicast_snooping(struct device *d,
375 struct device_attribute *attr,
376 const char *buf, size_t len)
377{
378 return store_bridge_parm(d, buf, len, br_multicast_toggle);
379}
380static DEVICE_ATTR(multicast_snooping, S_IRUGO | S_IWUSR,
381 show_multicast_snooping, store_multicast_snooping);
Herbert Xub1951672010-02-27 19:41:51 +0000382
Herbert Xuc5c23262012-04-13 02:37:42 +0000383static ssize_t show_multicast_querier(struct device *d,
384 struct device_attribute *attr,
385 char *buf)
386{
387 struct net_bridge *br = to_bridge(d);
388 return sprintf(buf, "%d\n", br->multicast_querier);
389}
390
391static ssize_t store_multicast_querier(struct device *d,
392 struct device_attribute *attr,
393 const char *buf, size_t len)
394{
395 return store_bridge_parm(d, buf, len, br_multicast_set_querier);
396}
397static DEVICE_ATTR(multicast_querier, S_IRUGO | S_IWUSR,
398 show_multicast_querier, store_multicast_querier);
399
Herbert Xub1951672010-02-27 19:41:51 +0000400static ssize_t show_hash_elasticity(struct device *d,
401 struct device_attribute *attr, char *buf)
402{
403 struct net_bridge *br = to_bridge(d);
404 return sprintf(buf, "%u\n", br->hash_elasticity);
405}
406
407static int set_elasticity(struct net_bridge *br, unsigned long val)
408{
409 br->hash_elasticity = val;
410 return 0;
411}
412
413static ssize_t store_hash_elasticity(struct device *d,
414 struct device_attribute *attr,
415 const char *buf, size_t len)
416{
417 return store_bridge_parm(d, buf, len, set_elasticity);
418}
419static DEVICE_ATTR(hash_elasticity, S_IRUGO | S_IWUSR, show_hash_elasticity,
420 store_hash_elasticity);
421
422static ssize_t show_hash_max(struct device *d, struct device_attribute *attr,
423 char *buf)
424{
425 struct net_bridge *br = to_bridge(d);
426 return sprintf(buf, "%u\n", br->hash_max);
427}
428
429static ssize_t store_hash_max(struct device *d, struct device_attribute *attr,
430 const char *buf, size_t len)
431{
432 return store_bridge_parm(d, buf, len, br_multicast_set_hash_max);
433}
434static DEVICE_ATTR(hash_max, S_IRUGO | S_IWUSR, show_hash_max,
435 store_hash_max);
Herbert Xud902eee2010-02-27 19:41:52 +0000436
437static ssize_t show_multicast_last_member_count(struct device *d,
438 struct device_attribute *attr,
439 char *buf)
440{
441 struct net_bridge *br = to_bridge(d);
442 return sprintf(buf, "%u\n", br->multicast_last_member_count);
443}
444
445static int set_last_member_count(struct net_bridge *br, unsigned long val)
446{
447 br->multicast_last_member_count = val;
448 return 0;
449}
450
451static ssize_t store_multicast_last_member_count(struct device *d,
452 struct device_attribute *attr,
453 const char *buf, size_t len)
454{
455 return store_bridge_parm(d, buf, len, set_last_member_count);
456}
457static DEVICE_ATTR(multicast_last_member_count, S_IRUGO | S_IWUSR,
458 show_multicast_last_member_count,
459 store_multicast_last_member_count);
460
461static ssize_t show_multicast_startup_query_count(
462 struct device *d, struct device_attribute *attr, char *buf)
463{
464 struct net_bridge *br = to_bridge(d);
465 return sprintf(buf, "%u\n", br->multicast_startup_query_count);
466}
467
468static int set_startup_query_count(struct net_bridge *br, unsigned long val)
469{
470 br->multicast_startup_query_count = val;
471 return 0;
472}
473
474static ssize_t store_multicast_startup_query_count(
475 struct device *d, struct device_attribute *attr, const char *buf,
476 size_t len)
477{
478 return store_bridge_parm(d, buf, len, set_startup_query_count);
479}
480static DEVICE_ATTR(multicast_startup_query_count, S_IRUGO | S_IWUSR,
481 show_multicast_startup_query_count,
482 store_multicast_startup_query_count);
483
484static ssize_t show_multicast_last_member_interval(
485 struct device *d, struct device_attribute *attr, char *buf)
486{
487 struct net_bridge *br = to_bridge(d);
488 return sprintf(buf, "%lu\n",
489 jiffies_to_clock_t(br->multicast_last_member_interval));
490}
491
492static int set_last_member_interval(struct net_bridge *br, unsigned long val)
493{
494 br->multicast_last_member_interval = clock_t_to_jiffies(val);
495 return 0;
496}
497
498static ssize_t store_multicast_last_member_interval(
499 struct device *d, struct device_attribute *attr, const char *buf,
500 size_t len)
501{
502 return store_bridge_parm(d, buf, len, set_last_member_interval);
503}
504static DEVICE_ATTR(multicast_last_member_interval, S_IRUGO | S_IWUSR,
505 show_multicast_last_member_interval,
506 store_multicast_last_member_interval);
507
508static ssize_t show_multicast_membership_interval(
509 struct device *d, struct device_attribute *attr, char *buf)
510{
511 struct net_bridge *br = to_bridge(d);
512 return sprintf(buf, "%lu\n",
513 jiffies_to_clock_t(br->multicast_membership_interval));
514}
515
516static int set_membership_interval(struct net_bridge *br, unsigned long val)
517{
518 br->multicast_membership_interval = clock_t_to_jiffies(val);
519 return 0;
520}
521
522static ssize_t store_multicast_membership_interval(
523 struct device *d, struct device_attribute *attr, const char *buf,
524 size_t len)
525{
526 return store_bridge_parm(d, buf, len, set_membership_interval);
527}
528static DEVICE_ATTR(multicast_membership_interval, S_IRUGO | S_IWUSR,
529 show_multicast_membership_interval,
530 store_multicast_membership_interval);
531
532static ssize_t show_multicast_querier_interval(struct device *d,
533 struct device_attribute *attr,
534 char *buf)
535{
536 struct net_bridge *br = to_bridge(d);
537 return sprintf(buf, "%lu\n",
538 jiffies_to_clock_t(br->multicast_querier_interval));
539}
540
541static int set_querier_interval(struct net_bridge *br, unsigned long val)
542{
543 br->multicast_querier_interval = clock_t_to_jiffies(val);
544 return 0;
545}
546
547static ssize_t store_multicast_querier_interval(struct device *d,
548 struct device_attribute *attr,
549 const char *buf, size_t len)
550{
551 return store_bridge_parm(d, buf, len, set_querier_interval);
552}
553static DEVICE_ATTR(multicast_querier_interval, S_IRUGO | S_IWUSR,
554 show_multicast_querier_interval,
555 store_multicast_querier_interval);
556
557static ssize_t show_multicast_query_interval(struct device *d,
558 struct device_attribute *attr,
559 char *buf)
560{
561 struct net_bridge *br = to_bridge(d);
562 return sprintf(buf, "%lu\n",
563 jiffies_to_clock_t(br->multicast_query_interval));
564}
565
566static int set_query_interval(struct net_bridge *br, unsigned long val)
567{
568 br->multicast_query_interval = clock_t_to_jiffies(val);
569 return 0;
570}
571
572static ssize_t store_multicast_query_interval(struct device *d,
573 struct device_attribute *attr,
574 const char *buf, size_t len)
575{
576 return store_bridge_parm(d, buf, len, set_query_interval);
577}
578static DEVICE_ATTR(multicast_query_interval, S_IRUGO | S_IWUSR,
579 show_multicast_query_interval,
580 store_multicast_query_interval);
581
582static ssize_t show_multicast_query_response_interval(
583 struct device *d, struct device_attribute *attr, char *buf)
584{
585 struct net_bridge *br = to_bridge(d);
586 return sprintf(
587 buf, "%lu\n",
588 jiffies_to_clock_t(br->multicast_query_response_interval));
589}
590
591static int set_query_response_interval(struct net_bridge *br, unsigned long val)
592{
593 br->multicast_query_response_interval = clock_t_to_jiffies(val);
594 return 0;
595}
596
597static ssize_t store_multicast_query_response_interval(
598 struct device *d, struct device_attribute *attr, const char *buf,
599 size_t len)
600{
601 return store_bridge_parm(d, buf, len, set_query_response_interval);
602}
603static DEVICE_ATTR(multicast_query_response_interval, S_IRUGO | S_IWUSR,
604 show_multicast_query_response_interval,
605 store_multicast_query_response_interval);
606
607static ssize_t show_multicast_startup_query_interval(
608 struct device *d, struct device_attribute *attr, char *buf)
609{
610 struct net_bridge *br = to_bridge(d);
611 return sprintf(
612 buf, "%lu\n",
613 jiffies_to_clock_t(br->multicast_startup_query_interval));
614}
615
616static int set_startup_query_interval(struct net_bridge *br, unsigned long val)
617{
618 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
619 return 0;
620}
621
622static ssize_t store_multicast_startup_query_interval(
623 struct device *d, struct device_attribute *attr, const char *buf,
624 size_t len)
625{
626 return store_bridge_parm(d, buf, len, set_startup_query_interval);
627}
628static DEVICE_ATTR(multicast_startup_query_interval, S_IRUGO | S_IWUSR,
629 show_multicast_startup_query_interval,
630 store_multicast_startup_query_interval);
Herbert Xu0909e112010-02-27 19:41:49 +0000631#endif
Patrick McHardy4df53d82010-07-02 09:32:57 +0200632#ifdef CONFIG_BRIDGE_NETFILTER
633static ssize_t show_nf_call_iptables(
634 struct device *d, struct device_attribute *attr, char *buf)
635{
636 struct net_bridge *br = to_bridge(d);
637 return sprintf(buf, "%u\n", br->nf_call_iptables);
638}
639
640static int set_nf_call_iptables(struct net_bridge *br, unsigned long val)
641{
642 br->nf_call_iptables = val ? true : false;
643 return 0;
644}
645
646static ssize_t store_nf_call_iptables(
647 struct device *d, struct device_attribute *attr, const char *buf,
648 size_t len)
649{
650 return store_bridge_parm(d, buf, len, set_nf_call_iptables);
651}
652static DEVICE_ATTR(nf_call_iptables, S_IRUGO | S_IWUSR,
653 show_nf_call_iptables, store_nf_call_iptables);
654
655static ssize_t show_nf_call_ip6tables(
656 struct device *d, struct device_attribute *attr, char *buf)
657{
658 struct net_bridge *br = to_bridge(d);
659 return sprintf(buf, "%u\n", br->nf_call_ip6tables);
660}
661
662static int set_nf_call_ip6tables(struct net_bridge *br, unsigned long val)
663{
664 br->nf_call_ip6tables = val ? true : false;
665 return 0;
666}
667
668static ssize_t store_nf_call_ip6tables(
669 struct device *d, struct device_attribute *attr, const char *buf,
670 size_t len)
671{
672 return store_bridge_parm(d, buf, len, set_nf_call_ip6tables);
673}
674static DEVICE_ATTR(nf_call_ip6tables, S_IRUGO | S_IWUSR,
675 show_nf_call_ip6tables, store_nf_call_ip6tables);
676
677static ssize_t show_nf_call_arptables(
678 struct device *d, struct device_attribute *attr, char *buf)
679{
680 struct net_bridge *br = to_bridge(d);
681 return sprintf(buf, "%u\n", br->nf_call_arptables);
682}
683
684static int set_nf_call_arptables(struct net_bridge *br, unsigned long val)
685{
686 br->nf_call_arptables = val ? true : false;
687 return 0;
688}
689
690static ssize_t store_nf_call_arptables(
691 struct device *d, struct device_attribute *attr, const char *buf,
692 size_t len)
693{
694 return store_bridge_parm(d, buf, len, set_nf_call_arptables);
695}
696static DEVICE_ATTR(nf_call_arptables, S_IRUGO | S_IWUSR,
697 show_nf_call_arptables, store_nf_call_arptables);
698#endif
Herbert Xu0909e112010-02-27 19:41:49 +0000699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static struct attribute *bridge_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700701 &dev_attr_forward_delay.attr,
702 &dev_attr_hello_time.attr,
703 &dev_attr_max_age.attr,
704 &dev_attr_ageing_time.attr,
705 &dev_attr_stp_state.attr,
stephen hemminger515853c2011-10-03 18:14:46 +0000706 &dev_attr_group_fwd_mask.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700707 &dev_attr_priority.attr,
708 &dev_attr_bridge_id.attr,
709 &dev_attr_root_id.attr,
710 &dev_attr_root_path_cost.attr,
711 &dev_attr_root_port.attr,
712 &dev_attr_topology_change.attr,
713 &dev_attr_topology_change_detected.attr,
714 &dev_attr_hello_timer.attr,
715 &dev_attr_tcn_timer.attr,
716 &dev_attr_topology_change_timer.attr,
717 &dev_attr_gc_timer.attr,
718 &dev_attr_group_addr.attr,
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700719 &dev_attr_flush.attr,
Herbert Xu0909e112010-02-27 19:41:49 +0000720#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
721 &dev_attr_multicast_router.attr,
Herbert Xu561f1102010-02-27 19:41:50 +0000722 &dev_attr_multicast_snooping.attr,
Herbert Xuc5c23262012-04-13 02:37:42 +0000723 &dev_attr_multicast_querier.attr,
Herbert Xub1951672010-02-27 19:41:51 +0000724 &dev_attr_hash_elasticity.attr,
725 &dev_attr_hash_max.attr,
Herbert Xud902eee2010-02-27 19:41:52 +0000726 &dev_attr_multicast_last_member_count.attr,
727 &dev_attr_multicast_startup_query_count.attr,
728 &dev_attr_multicast_last_member_interval.attr,
729 &dev_attr_multicast_membership_interval.attr,
730 &dev_attr_multicast_querier_interval.attr,
731 &dev_attr_multicast_query_interval.attr,
732 &dev_attr_multicast_query_response_interval.attr,
733 &dev_attr_multicast_startup_query_interval.attr,
Herbert Xu0909e112010-02-27 19:41:49 +0000734#endif
Patrick McHardy4df53d82010-07-02 09:32:57 +0200735#ifdef CONFIG_BRIDGE_NETFILTER
736 &dev_attr_nf_call_iptables.attr,
737 &dev_attr_nf_call_ip6tables.attr,
738 &dev_attr_nf_call_arptables.attr,
739#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 NULL
741};
742
743static struct attribute_group bridge_group = {
744 .name = SYSFS_BRIDGE_ATTR,
745 .attrs = bridge_attrs,
746};
747
748/*
749 * Export the forwarding information table as a binary file
750 * The records are struct __fdb_entry.
751 *
752 * Returns the number of bytes read.
753 */
Chris Wright2c3c8be2010-05-12 18:28:57 -0700754static ssize_t brforward_read(struct file *filp, struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +0800755 struct bin_attribute *bin_attr,
756 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700758 struct device *dev = to_dev(kobj);
759 struct net_bridge *br = to_bridge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int n;
761
762 /* must read whole records */
763 if (off % sizeof(struct __fdb_entry) != 0)
764 return -EINVAL;
765
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900766 n = br_fdb_fillbuf(br, buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 count / sizeof(struct __fdb_entry),
768 off / sizeof(struct __fdb_entry));
769
770 if (n > 0)
771 n *= sizeof(struct __fdb_entry);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return n;
774}
775
776static struct bin_attribute bridge_forward = {
777 .attr = { .name = SYSFS_BRIDGE_FDB,
Tejun Heo7b595752007-06-14 03:45:17 +0900778 .mode = S_IRUGO, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 .read = brforward_read,
780};
781
782/*
783 * Add entries in sysfs onto the existing network class device
784 * for the bridge.
785 * Adds a attribute group "bridge" containing tuning parameters.
786 * Binary attribute containing the forward table
787 * Sub directory to hold links to interfaces.
788 *
789 * Note: the ifobj exists only to be a subdirectory
790 * to hold links. The ifobj exists in same data structure
791 * as it's parent the bridge so reference counting works.
792 */
793int br_sysfs_addbr(struct net_device *dev)
794{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700795 struct kobject *brobj = &dev->dev.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct net_bridge *br = netdev_priv(dev);
797 int err;
798
799 err = sysfs_create_group(brobj, &bridge_group);
800 if (err) {
801 pr_info("%s: can't create group %s/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800802 __func__, dev->name, bridge_group.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto out1;
804 }
805
806 err = sysfs_create_bin_file(brobj, &bridge_forward);
807 if (err) {
Randy Dunlap1842c4b2006-10-25 23:07:37 -0700808 pr_info("%s: can't create attribute file %s/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800809 __func__, dev->name, bridge_forward.attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto out2;
811 }
812
Greg Kroah-Hartman43b98c42007-12-17 15:54:39 -0400813 br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
814 if (!br->ifobj) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 pr_info("%s: can't add kobject (directory) %s/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800816 __func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto out3;
818 }
819 return 0;
820 out3:
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700821 sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 out2:
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700823 sysfs_remove_group(&dev->dev.kobj, &bridge_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 out1:
825 return err;
826
827}
828
829void br_sysfs_delbr(struct net_device *dev)
830{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700831 struct kobject *kobj = &dev->dev.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct net_bridge *br = netdev_priv(dev);
833
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800834 kobject_put(br->ifobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 sysfs_remove_bin_file(kobj, &bridge_forward);
836 sysfs_remove_group(kobj, &bridge_group);
837}