xref: /openbmc/linux/net/bridge/br_sysfs_br.c (revision 8569c914)
1 /*
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 
14 #include <linux/capability.h>
15 #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_dev(obj)	container_of(obj, struct device, kobj)
25 #define to_bridge(cd)	((struct net_bridge *)(to_net_dev(cd)->priv))
26 
27 /*
28  * Common code for storing bridge parameters.
29  */
30 static ssize_t store_bridge_parm(struct device *d,
31 				 const char *buf, size_t len,
32 				 int (*set)(struct net_bridge *, unsigned long))
33 {
34 	struct net_bridge *br = to_bridge(d);
35 	char *endp;
36 	unsigned long val;
37 	int err;
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 	err = (*set)(br, val);
48 	spin_unlock_bh(&br->lock);
49 	return err ? err : len;
50 }
51 
52 
53 static ssize_t show_forward_delay(struct device *d,
54 				  struct device_attribute *attr, char *buf)
55 {
56 	struct net_bridge *br = to_bridge(d);
57 	return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
58 }
59 
60 static int set_forward_delay(struct net_bridge *br, unsigned long val)
61 {
62 	unsigned long delay = clock_t_to_jiffies(val);
63 	br->forward_delay = delay;
64 	if (br_is_root_bridge(br))
65 		br->bridge_forward_delay = delay;
66 	return 0;
67 }
68 
69 static ssize_t store_forward_delay(struct device *d,
70 				   struct device_attribute *attr,
71 				   const char *buf, size_t len)
72 {
73 	return store_bridge_parm(d, buf, len, set_forward_delay);
74 }
75 static DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
76 		   show_forward_delay, store_forward_delay);
77 
78 static ssize_t show_hello_time(struct device *d, struct device_attribute *attr,
79 			       char *buf)
80 {
81 	return sprintf(buf, "%lu\n",
82 		       jiffies_to_clock_t(to_bridge(d)->hello_time));
83 }
84 
85 static int set_hello_time(struct net_bridge *br, unsigned long val)
86 {
87 	unsigned long t = clock_t_to_jiffies(val);
88 
89 	if (t < HZ)
90 		return -EINVAL;
91 
92 	br->hello_time = t;
93 	if (br_is_root_bridge(br))
94 		br->bridge_hello_time = t;
95 	return 0;
96 }
97 
98 static ssize_t store_hello_time(struct device *d,
99 				struct device_attribute *attr, const char *buf,
100 				size_t len)
101 {
102 	return store_bridge_parm(d, buf, len, set_hello_time);
103 }
104 static DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
105 		   store_hello_time);
106 
107 static ssize_t show_max_age(struct device *d, struct device_attribute *attr,
108 			    char *buf)
109 {
110 	return sprintf(buf, "%lu\n",
111 		       jiffies_to_clock_t(to_bridge(d)->max_age));
112 }
113 
114 static int set_max_age(struct net_bridge *br, unsigned long val)
115 {
116 	unsigned long t = clock_t_to_jiffies(val);
117 	br->max_age = t;
118 	if (br_is_root_bridge(br))
119 		br->bridge_max_age = t;
120 	return 0;
121 }
122 
123 static ssize_t store_max_age(struct device *d, struct device_attribute *attr,
124 			     const char *buf, size_t len)
125 {
126 	return store_bridge_parm(d, buf, len, set_max_age);
127 }
128 static DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
129 
130 static ssize_t show_ageing_time(struct device *d,
131 				struct device_attribute *attr, char *buf)
132 {
133 	struct net_bridge *br = to_bridge(d);
134 	return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
135 }
136 
137 static int set_ageing_time(struct net_bridge *br, unsigned long val)
138 {
139 	br->ageing_time = clock_t_to_jiffies(val);
140 	return 0;
141 }
142 
143 static ssize_t store_ageing_time(struct device *d,
144 				 struct device_attribute *attr,
145 				 const char *buf, size_t len)
146 {
147 	return store_bridge_parm(d, buf, len, set_ageing_time);
148 }
149 static DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
150 		   store_ageing_time);
151 
152 static ssize_t show_stp_state(struct device *d,
153 			      struct device_attribute *attr, char *buf)
154 {
155 	struct net_bridge *br = to_bridge(d);
156 	return sprintf(buf, "%d\n", br->stp_enabled);
157 }
158 
159 
160 static ssize_t store_stp_state(struct device *d,
161 			       struct device_attribute *attr, const char *buf,
162 			       size_t len)
163 {
164 	struct net_bridge *br = to_bridge(d);
165 	char *endp;
166 	unsigned long val;
167 
168 	if (!capable(CAP_NET_ADMIN))
169 		return -EPERM;
170 
171 	val = simple_strtoul(buf, &endp, 0);
172 	if (endp == buf)
173 		return -EINVAL;
174 
175 	rtnl_lock();
176 	br_stp_set_enabled(br, val);
177 	rtnl_unlock();
178 
179 	return len;
180 }
181 static DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
182 		   store_stp_state);
183 
184 static ssize_t show_priority(struct device *d, struct device_attribute *attr,
185 			     char *buf)
186 {
187 	struct net_bridge *br = to_bridge(d);
188 	return sprintf(buf, "%d\n",
189 		       (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
190 }
191 
192 static int set_priority(struct net_bridge *br, unsigned long val)
193 {
194 	br_stp_set_bridge_priority(br, (u16) val);
195 	return 0;
196 }
197 
198 static ssize_t store_priority(struct device *d, struct device_attribute *attr,
199 			       const char *buf, size_t len)
200 {
201 	return store_bridge_parm(d, buf, len, set_priority);
202 }
203 static DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
204 
205 static ssize_t show_root_id(struct device *d, struct device_attribute *attr,
206 			    char *buf)
207 {
208 	return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
209 }
210 static DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
211 
212 static ssize_t show_bridge_id(struct device *d, struct device_attribute *attr,
213 			      char *buf)
214 {
215 	return br_show_bridge_id(buf, &to_bridge(d)->bridge_id);
216 }
217 static DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
218 
219 static ssize_t show_root_port(struct device *d, struct device_attribute *attr,
220 			      char *buf)
221 {
222 	return sprintf(buf, "%d\n", to_bridge(d)->root_port);
223 }
224 static DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
225 
226 static ssize_t show_root_path_cost(struct device *d,
227 				   struct device_attribute *attr, char *buf)
228 {
229 	return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
230 }
231 static DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
232 
233 static ssize_t show_topology_change(struct device *d,
234 				    struct device_attribute *attr, char *buf)
235 {
236 	return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
237 }
238 static DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
239 
240 static ssize_t show_topology_change_detected(struct device *d,
241 					     struct device_attribute *attr,
242 					     char *buf)
243 {
244 	struct net_bridge *br = to_bridge(d);
245 	return sprintf(buf, "%d\n", br->topology_change_detected);
246 }
247 static DEVICE_ATTR(topology_change_detected, S_IRUGO,
248 		   show_topology_change_detected, NULL);
249 
250 static ssize_t show_hello_timer(struct device *d,
251 				struct device_attribute *attr, char *buf)
252 {
253 	struct net_bridge *br = to_bridge(d);
254 	return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
255 }
256 static DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
257 
258 static ssize_t show_tcn_timer(struct device *d, struct device_attribute *attr,
259 			      char *buf)
260 {
261 	struct net_bridge *br = to_bridge(d);
262 	return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
263 }
264 static DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
265 
266 static ssize_t show_topology_change_timer(struct device *d,
267 					  struct device_attribute *attr,
268 					  char *buf)
269 {
270 	struct net_bridge *br = to_bridge(d);
271 	return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
272 }
273 static DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
274 		   NULL);
275 
276 static ssize_t show_gc_timer(struct device *d, struct device_attribute *attr,
277 			     char *buf)
278 {
279 	struct net_bridge *br = to_bridge(d);
280 	return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
281 }
282 static DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
283 
284 static ssize_t show_group_addr(struct device *d,
285 			       struct device_attribute *attr, char *buf)
286 {
287 	struct net_bridge *br = to_bridge(d);
288 	return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
289 		       br->group_addr[0], br->group_addr[1],
290 		       br->group_addr[2], br->group_addr[3],
291 		       br->group_addr[4], br->group_addr[5]);
292 }
293 
294 static ssize_t store_group_addr(struct device *d,
295 				struct device_attribute *attr,
296 				const char *buf, size_t len)
297 {
298 	struct net_bridge *br = to_bridge(d);
299 	unsigned new_addr[6];
300 	int i;
301 
302 	if (!capable(CAP_NET_ADMIN))
303 		return -EPERM;
304 
305 	if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
306 		   &new_addr[0], &new_addr[1], &new_addr[2],
307 		   &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
308 		return -EINVAL;
309 
310 	/* Must be 01:80:c2:00:00:0X */
311 	for (i = 0; i < 5; i++)
312 		if (new_addr[i] != br_group_address[i])
313 			return -EINVAL;
314 
315 	if (new_addr[5] & ~0xf)
316 		return -EINVAL;
317 
318 	if (new_addr[5] == 1 	/* 802.3x Pause address */
319 	    || new_addr[5] == 2 /* 802.3ad Slow protocols */
320 	    || new_addr[5] == 3) /* 802.1X PAE address */
321 		return -EINVAL;
322 
323 	spin_lock_bh(&br->lock);
324 	for (i = 0; i < 6; i++)
325 		br->group_addr[i] = new_addr[i];
326 	spin_unlock_bh(&br->lock);
327 	return len;
328 }
329 
330 static DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
331 		   show_group_addr, store_group_addr);
332 
333 static ssize_t store_flush(struct device *d,
334 			   struct device_attribute *attr,
335 			   const char *buf, size_t len)
336 {
337 	struct net_bridge *br = to_bridge(d);
338 
339 	if (!capable(CAP_NET_ADMIN))
340 		return -EPERM;
341 
342 	br_fdb_flush(br);
343 	return len;
344 }
345 static DEVICE_ATTR(flush, S_IWUSR, NULL, store_flush);
346 
347 static struct attribute *bridge_attrs[] = {
348 	&dev_attr_forward_delay.attr,
349 	&dev_attr_hello_time.attr,
350 	&dev_attr_max_age.attr,
351 	&dev_attr_ageing_time.attr,
352 	&dev_attr_stp_state.attr,
353 	&dev_attr_priority.attr,
354 	&dev_attr_bridge_id.attr,
355 	&dev_attr_root_id.attr,
356 	&dev_attr_root_path_cost.attr,
357 	&dev_attr_root_port.attr,
358 	&dev_attr_topology_change.attr,
359 	&dev_attr_topology_change_detected.attr,
360 	&dev_attr_hello_timer.attr,
361 	&dev_attr_tcn_timer.attr,
362 	&dev_attr_topology_change_timer.attr,
363 	&dev_attr_gc_timer.attr,
364 	&dev_attr_group_addr.attr,
365 	&dev_attr_flush.attr,
366 	NULL
367 };
368 
369 static struct attribute_group bridge_group = {
370 	.name = SYSFS_BRIDGE_ATTR,
371 	.attrs = bridge_attrs,
372 };
373 
374 /*
375  * Export the forwarding information table as a binary file
376  * The records are struct __fdb_entry.
377  *
378  * Returns the number of bytes read.
379  */
380 static ssize_t brforward_read(struct kobject *kobj,
381 			      struct bin_attribute *bin_attr,
382 			      char *buf, loff_t off, size_t count)
383 {
384 	struct device *dev = to_dev(kobj);
385 	struct net_bridge *br = to_bridge(dev);
386 	int n;
387 
388 	/* must read whole records */
389 	if (off % sizeof(struct __fdb_entry) != 0)
390 		return -EINVAL;
391 
392 	n =  br_fdb_fillbuf(br, buf,
393 			    count / sizeof(struct __fdb_entry),
394 			    off / sizeof(struct __fdb_entry));
395 
396 	if (n > 0)
397 		n *= sizeof(struct __fdb_entry);
398 
399 	return n;
400 }
401 
402 static struct bin_attribute bridge_forward = {
403 	.attr = { .name = SYSFS_BRIDGE_FDB,
404 		  .mode = S_IRUGO, },
405 	.read = brforward_read,
406 };
407 
408 /*
409  * Add entries in sysfs onto the existing network class device
410  * for the bridge.
411  *   Adds a attribute group "bridge" containing tuning parameters.
412  *   Binary attribute containing the forward table
413  *   Sub directory to hold links to interfaces.
414  *
415  * Note: the ifobj exists only to be a subdirectory
416  *   to hold links.  The ifobj exists in same data structure
417  *   as it's parent the bridge so reference counting works.
418  */
419 int br_sysfs_addbr(struct net_device *dev)
420 {
421 	struct kobject *brobj = &dev->dev.kobj;
422 	struct net_bridge *br = netdev_priv(dev);
423 	int err;
424 
425 	err = sysfs_create_group(brobj, &bridge_group);
426 	if (err) {
427 		pr_info("%s: can't create group %s/%s\n",
428 			__func__, dev->name, bridge_group.name);
429 		goto out1;
430 	}
431 
432 	err = sysfs_create_bin_file(brobj, &bridge_forward);
433 	if (err) {
434 		pr_info("%s: can't create attribute file %s/%s\n",
435 			__func__, dev->name, bridge_forward.attr.name);
436 		goto out2;
437 	}
438 
439 	br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
440 	if (!br->ifobj) {
441 		pr_info("%s: can't add kobject (directory) %s/%s\n",
442 			__func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
443 		goto out3;
444 	}
445 	return 0;
446  out3:
447 	sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
448  out2:
449 	sysfs_remove_group(&dev->dev.kobj, &bridge_group);
450  out1:
451 	return err;
452 
453 }
454 
455 void br_sysfs_delbr(struct net_device *dev)
456 {
457 	struct kobject *kobj = &dev->dev.kobj;
458 	struct net_bridge *br = netdev_priv(dev);
459 
460 	kobject_put(br->ifobj);
461 	sysfs_remove_bin_file(kobj, &bridge_forward);
462 	sysfs_remove_group(kobj, &bridge_group);
463 }
464