blob: 96bcb2ff59abce05cd3be39c9384898c3ba93161 [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>
17#include <linux/if_bridge.h>
18#include <linux/rtnetlink.h>
19#include <linux/spinlock.h>
20#include <linux/times.h>
21
22#include "br_private.h"
23
24#define to_class_dev(obj) container_of(obj,struct class_device,kobj)
25#define to_net_dev(class) container_of(class, struct net_device, class_dev)
26#define to_bridge(cd) ((struct net_bridge *)(to_net_dev(cd)->priv))
27
28/*
29 * Common code for storing bridge parameters.
30 */
31static ssize_t store_bridge_parm(struct class_device *cd,
32 const char *buf, size_t len,
33 void (*set)(struct net_bridge *, unsigned long))
34{
35 struct net_bridge *br = to_bridge(cd);
36 char *endp;
37 unsigned long val;
38
39 if (!capable(CAP_NET_ADMIN))
40 return -EPERM;
41
42 val = simple_strtoul(buf, &endp, 0);
43 if (endp == buf)
44 return -EINVAL;
45
46 spin_lock_bh(&br->lock);
47 (*set)(br, val);
48 spin_unlock_bh(&br->lock);
49 return len;
50}
51
52
53static ssize_t show_forward_delay(struct class_device *cd, char *buf)
54{
55 struct net_bridge *br = to_bridge(cd);
56 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
57}
58
59static void set_forward_delay(struct net_bridge *br, unsigned long val)
60{
61 unsigned long delay = clock_t_to_jiffies(val);
62 br->forward_delay = delay;
63 if (br_is_root_bridge(br))
64 br->bridge_forward_delay = delay;
65}
66
67static ssize_t store_forward_delay(struct class_device *cd, const char *buf,
68 size_t len)
69{
70 return store_bridge_parm(cd, buf, len, set_forward_delay);
71}
72static CLASS_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
73 show_forward_delay, store_forward_delay);
74
75static ssize_t show_hello_time(struct class_device *cd, char *buf)
76{
77 return sprintf(buf, "%lu\n",
78 jiffies_to_clock_t(to_bridge(cd)->hello_time));
79}
80
81static void set_hello_time(struct net_bridge *br, unsigned long val)
82{
83 unsigned long t = clock_t_to_jiffies(val);
84 br->hello_time = t;
85 if (br_is_root_bridge(br))
86 br->bridge_hello_time = t;
87}
88
89static ssize_t store_hello_time(struct class_device *cd, const char *buf,
90 size_t len)
91{
92 return store_bridge_parm(cd, buf, len, set_hello_time);
93}
94
95static CLASS_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
96 store_hello_time);
97
98static ssize_t show_max_age(struct class_device *cd, char *buf)
99{
100 return sprintf(buf, "%lu\n",
101 jiffies_to_clock_t(to_bridge(cd)->max_age));
102}
103
104static void set_max_age(struct net_bridge *br, unsigned long val)
105{
106 unsigned long t = clock_t_to_jiffies(val);
107 br->max_age = t;
108 if (br_is_root_bridge(br))
109 br->bridge_max_age = t;
110}
111
112static ssize_t store_max_age(struct class_device *cd, const char *buf,
113 size_t len)
114{
115 return store_bridge_parm(cd, buf, len, set_max_age);
116}
117
118static CLASS_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age,
119 store_max_age);
120
121static ssize_t show_ageing_time(struct class_device *cd, char *buf)
122{
123 struct net_bridge *br = to_bridge(cd);
124 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
125}
126
127static void set_ageing_time(struct net_bridge *br, unsigned long val)
128{
129 br->ageing_time = clock_t_to_jiffies(val);
130}
131
132static ssize_t store_ageing_time(struct class_device *cd, const char *buf,
133 size_t len)
134{
135 return store_bridge_parm(cd, buf, len, set_ageing_time);
136}
137
138static CLASS_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
139 store_ageing_time);
140static ssize_t show_stp_state(struct class_device *cd, char *buf)
141{
142 struct net_bridge *br = to_bridge(cd);
143 return sprintf(buf, "%d\n", br->stp_enabled);
144}
145
146static void set_stp_state(struct net_bridge *br, unsigned long val)
147{
148 br->stp_enabled = val;
149}
150
151static ssize_t store_stp_state(struct class_device *cd,
152 const char *buf, size_t len)
153{
154 return store_bridge_parm(cd, buf, len, set_stp_state);
155}
156
157static CLASS_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
158 store_stp_state);
159
160static ssize_t show_priority(struct class_device *cd, char *buf)
161{
162 struct net_bridge *br = to_bridge(cd);
163 return sprintf(buf, "%d\n",
164 (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
165}
166
167static void set_priority(struct net_bridge *br, unsigned long val)
168{
169 br_stp_set_bridge_priority(br, (u16) val);
170}
171
172static ssize_t store_priority(struct class_device *cd,
173 const char *buf, size_t len)
174{
175 return store_bridge_parm(cd, buf, len, set_priority);
176}
177static CLASS_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority,
178 store_priority);
179
180static ssize_t show_root_id(struct class_device *cd, char *buf)
181{
182 return br_show_bridge_id(buf, &to_bridge(cd)->designated_root);
183}
184static CLASS_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
185
186static ssize_t show_bridge_id(struct class_device *cd, char *buf)
187{
188 return br_show_bridge_id(buf, &to_bridge(cd)->bridge_id);
189}
190static CLASS_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
191
192static ssize_t show_root_port(struct class_device *cd, char *buf)
193{
194 return sprintf(buf, "%d\n", to_bridge(cd)->root_port);
195}
196static CLASS_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
197
198static ssize_t show_root_path_cost(struct class_device *cd, char *buf)
199{
200 return sprintf(buf, "%d\n", to_bridge(cd)->root_path_cost);
201}
202static CLASS_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
203
204static ssize_t show_topology_change(struct class_device *cd, char *buf)
205{
206 return sprintf(buf, "%d\n", to_bridge(cd)->topology_change);
207}
208static CLASS_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
209
210static ssize_t show_topology_change_detected(struct class_device *cd, char *buf)
211{
212 struct net_bridge *br = to_bridge(cd);
213 return sprintf(buf, "%d\n", br->topology_change_detected);
214}
215static CLASS_DEVICE_ATTR(topology_change_detected, S_IRUGO, show_topology_change_detected, NULL);
216
217static ssize_t show_hello_timer(struct class_device *cd, char *buf)
218{
219 struct net_bridge *br = to_bridge(cd);
220 return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
221}
222static CLASS_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
223
224static ssize_t show_tcn_timer(struct class_device *cd, char *buf)
225{
226 struct net_bridge *br = to_bridge(cd);
227 return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
228}
229static CLASS_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
230
231static ssize_t show_topology_change_timer(struct class_device *cd, char *buf)
232{
233 struct net_bridge *br = to_bridge(cd);
234 return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
235}
236static CLASS_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer, NULL);
237
238static ssize_t show_gc_timer(struct class_device *cd, char *buf)
239{
240 struct net_bridge *br = to_bridge(cd);
241 return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
242}
243static CLASS_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
244
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800245static ssize_t show_group_addr(struct class_device *cd, char *buf)
246{
247 struct net_bridge *br = to_bridge(cd);
248 return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
249 br->group_addr[0], br->group_addr[1],
250 br->group_addr[2], br->group_addr[3],
251 br->group_addr[4], br->group_addr[5]);
252}
253
254static ssize_t store_group_addr(struct class_device *cd, const char *buf,
255 size_t len)
256{
257 struct net_bridge *br = to_bridge(cd);
258 unsigned new_addr[6];
259 int i;
260
261 if (!capable(CAP_NET_ADMIN))
262 return -EPERM;
263
264 if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
265 &new_addr[0], &new_addr[1], &new_addr[2],
266 &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
267 return -EINVAL;
268
269 /* Must be 01:80:c2:00:00:0X */
270 for (i = 0; i < 5; i++)
271 if (new_addr[i] != br_group_address[i])
272 return -EINVAL;
273
274 if (new_addr[5] & ~0xf)
275 return -EINVAL;
276
277 if (new_addr[5] == 1 /* 802.3x Pause address */
278 || new_addr[5] == 2 /* 802.3ad Slow protocols */
279 || new_addr[5] == 3) /* 802.1X PAE address */
280 return -EINVAL;
281
282 spin_lock_bh(&br->lock);
283 for (i = 0; i < 6; i++)
284 br->group_addr[i] = new_addr[i];
285 spin_unlock_bh(&br->lock);
286 return len;
287}
288
289static CLASS_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
290 show_group_addr, store_group_addr);
291
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static struct attribute *bridge_attrs[] = {
294 &class_device_attr_forward_delay.attr,
295 &class_device_attr_hello_time.attr,
296 &class_device_attr_max_age.attr,
297 &class_device_attr_ageing_time.attr,
298 &class_device_attr_stp_state.attr,
299 &class_device_attr_priority.attr,
300 &class_device_attr_bridge_id.attr,
301 &class_device_attr_root_id.attr,
302 &class_device_attr_root_path_cost.attr,
303 &class_device_attr_root_port.attr,
304 &class_device_attr_topology_change.attr,
305 &class_device_attr_topology_change_detected.attr,
306 &class_device_attr_hello_timer.attr,
307 &class_device_attr_tcn_timer.attr,
308 &class_device_attr_topology_change_timer.attr,
309 &class_device_attr_gc_timer.attr,
Stephen Hemmingerfda93d92006-03-20 22:59:21 -0800310 &class_device_attr_group_addr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 NULL
312};
313
314static struct attribute_group bridge_group = {
315 .name = SYSFS_BRIDGE_ATTR,
316 .attrs = bridge_attrs,
317};
318
319/*
320 * Export the forwarding information table as a binary file
321 * The records are struct __fdb_entry.
322 *
323 * Returns the number of bytes read.
324 */
325static ssize_t brforward_read(struct kobject *kobj, char *buf,
326 loff_t off, size_t count)
327{
328 struct class_device *cdev = to_class_dev(kobj);
329 struct net_bridge *br = to_bridge(cdev);
330 int n;
331
332 /* must read whole records */
333 if (off % sizeof(struct __fdb_entry) != 0)
334 return -EINVAL;
335
336 n = br_fdb_fillbuf(br, buf,
337 count / sizeof(struct __fdb_entry),
338 off / sizeof(struct __fdb_entry));
339
340 if (n > 0)
341 n *= sizeof(struct __fdb_entry);
342
343 return n;
344}
345
346static struct bin_attribute bridge_forward = {
347 .attr = { .name = SYSFS_BRIDGE_FDB,
348 .mode = S_IRUGO,
349 .owner = THIS_MODULE, },
350 .read = brforward_read,
351};
352
353/*
354 * Add entries in sysfs onto the existing network class device
355 * for the bridge.
356 * Adds a attribute group "bridge" containing tuning parameters.
357 * Binary attribute containing the forward table
358 * Sub directory to hold links to interfaces.
359 *
360 * Note: the ifobj exists only to be a subdirectory
361 * to hold links. The ifobj exists in same data structure
362 * as it's parent the bridge so reference counting works.
363 */
364int br_sysfs_addbr(struct net_device *dev)
365{
366 struct kobject *brobj = &dev->class_dev.kobj;
367 struct net_bridge *br = netdev_priv(dev);
368 int err;
369
370 err = sysfs_create_group(brobj, &bridge_group);
371 if (err) {
372 pr_info("%s: can't create group %s/%s\n",
373 __FUNCTION__, dev->name, bridge_group.name);
374 goto out1;
375 }
376
377 err = sysfs_create_bin_file(brobj, &bridge_forward);
378 if (err) {
379 pr_info("%s: can't create attribue file %s/%s\n",
380 __FUNCTION__, dev->name, bridge_forward.attr.name);
381 goto out2;
382 }
383
384
385 kobject_set_name(&br->ifobj, SYSFS_BRIDGE_PORT_SUBDIR);
386 br->ifobj.ktype = NULL;
387 br->ifobj.kset = NULL;
388 br->ifobj.parent = brobj;
389
390 err = kobject_register(&br->ifobj);
391 if (err) {
392 pr_info("%s: can't add kobject (directory) %s/%s\n",
393 __FUNCTION__, dev->name, br->ifobj.name);
394 goto out3;
395 }
396 return 0;
397 out3:
398 sysfs_remove_bin_file(&dev->class_dev.kobj, &bridge_forward);
399 out2:
400 sysfs_remove_group(&dev->class_dev.kobj, &bridge_group);
401 out1:
402 return err;
403
404}
405
406void br_sysfs_delbr(struct net_device *dev)
407{
408 struct kobject *kobj = &dev->class_dev.kobj;
409 struct net_bridge *br = netdev_priv(dev);
410
411 kobject_unregister(&br->ifobj);
412 sysfs_remove_bin_file(kobj, &bridge_forward);
413 sysfs_remove_group(kobj, &bridge_group);
414}