xref: /linux/net/core/net-sysfs.c (revision 6b2d2cec1081a979e0efd6a1e9559e5a01a3c10e)
1 /*
2  * net-sysfs.c - network device class and attributes
3  *
4  * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License
8  *	as published by the Free Software Foundation; either version
9  *	2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/capability.h>
13 #include <linux/kernel.h>
14 #include <linux/netdevice.h>
15 #include <linux/if_arp.h>
16 #include <net/sock.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/wireless.h>
19 #include <net/iw_handler.h>
20 
21 #include "net-sysfs.h"
22 
23 #ifdef CONFIG_SYSFS
24 static const char fmt_hex[] = "%#x\n";
25 static const char fmt_long_hex[] = "%#lx\n";
26 static const char fmt_dec[] = "%d\n";
27 static const char fmt_ulong[] = "%lu\n";
28 
29 static inline int dev_isalive(const struct net_device *dev)
30 {
31 	return dev->reg_state <= NETREG_REGISTERED;
32 }
33 
34 /* use same locking rules as GIF* ioctl's */
35 static ssize_t netdev_show(const struct device *dev,
36 			   struct device_attribute *attr, char *buf,
37 			   ssize_t (*format)(const struct net_device *, char *))
38 {
39 	struct net_device *net = to_net_dev(dev);
40 	ssize_t ret = -EINVAL;
41 
42 	read_lock(&dev_base_lock);
43 	if (dev_isalive(net))
44 		ret = (*format)(net, buf);
45 	read_unlock(&dev_base_lock);
46 
47 	return ret;
48 }
49 
50 /* generate a show function for simple field */
51 #define NETDEVICE_SHOW(field, format_string)				\
52 static ssize_t format_##field(const struct net_device *net, char *buf)	\
53 {									\
54 	return sprintf(buf, format_string, net->field);			\
55 }									\
56 static ssize_t show_##field(struct device *dev,				\
57 			    struct device_attribute *attr, char *buf)	\
58 {									\
59 	return netdev_show(dev, attr, buf, format_##field);		\
60 }
61 
62 
63 /* use same locking and permission rules as SIF* ioctl's */
64 static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
65 			    const char *buf, size_t len,
66 			    int (*set)(struct net_device *, unsigned long))
67 {
68 	struct net_device *net = to_net_dev(dev);
69 	char *endp;
70 	unsigned long new;
71 	int ret = -EINVAL;
72 
73 	if (!capable(CAP_NET_ADMIN))
74 		return -EPERM;
75 
76 	new = simple_strtoul(buf, &endp, 0);
77 	if (endp == buf)
78 		goto err;
79 
80 	rtnl_lock();
81 	if (dev_isalive(net)) {
82 		if ((ret = (*set)(net, new)) == 0)
83 			ret = len;
84 	}
85 	rtnl_unlock();
86  err:
87 	return ret;
88 }
89 
90 NETDEVICE_SHOW(addr_len, fmt_dec);
91 NETDEVICE_SHOW(iflink, fmt_dec);
92 NETDEVICE_SHOW(ifindex, fmt_dec);
93 NETDEVICE_SHOW(features, fmt_long_hex);
94 NETDEVICE_SHOW(type, fmt_dec);
95 NETDEVICE_SHOW(link_mode, fmt_dec);
96 
97 /* use same locking rules as GIFHWADDR ioctl's */
98 static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
99 {
100 	int i;
101 	char *cp = buf;
102 
103 	for (i = 0; i < len; i++)
104 		cp += sprintf(cp, "%02x%c", addr[i],
105 			      i == (len - 1) ? '\n' : ':');
106 	return cp - buf;
107 }
108 
109 static ssize_t show_address(struct device *dev, struct device_attribute *attr,
110 			    char *buf)
111 {
112 	struct net_device *net = to_net_dev(dev);
113 	ssize_t ret = -EINVAL;
114 
115 	read_lock(&dev_base_lock);
116 	if (dev_isalive(net))
117 	    ret = format_addr(buf, net->dev_addr, net->addr_len);
118 	read_unlock(&dev_base_lock);
119 	return ret;
120 }
121 
122 static ssize_t show_broadcast(struct device *dev,
123 			    struct device_attribute *attr, char *buf)
124 {
125 	struct net_device *net = to_net_dev(dev);
126 	if (dev_isalive(net))
127 		return format_addr(buf, net->broadcast, net->addr_len);
128 	return -EINVAL;
129 }
130 
131 static ssize_t show_carrier(struct device *dev,
132 			    struct device_attribute *attr, char *buf)
133 {
134 	struct net_device *netdev = to_net_dev(dev);
135 	if (netif_running(netdev)) {
136 		return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
137 	}
138 	return -EINVAL;
139 }
140 
141 static ssize_t show_dormant(struct device *dev,
142 			    struct device_attribute *attr, char *buf)
143 {
144 	struct net_device *netdev = to_net_dev(dev);
145 
146 	if (netif_running(netdev))
147 		return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
148 
149 	return -EINVAL;
150 }
151 
152 static const char *operstates[] = {
153 	"unknown",
154 	"notpresent", /* currently unused */
155 	"down",
156 	"lowerlayerdown",
157 	"testing", /* currently unused */
158 	"dormant",
159 	"up"
160 };
161 
162 static ssize_t show_operstate(struct device *dev,
163 			      struct device_attribute *attr, char *buf)
164 {
165 	const struct net_device *netdev = to_net_dev(dev);
166 	unsigned char operstate;
167 
168 	read_lock(&dev_base_lock);
169 	operstate = netdev->operstate;
170 	if (!netif_running(netdev))
171 		operstate = IF_OPER_DOWN;
172 	read_unlock(&dev_base_lock);
173 
174 	if (operstate >= ARRAY_SIZE(operstates))
175 		return -EINVAL; /* should not happen */
176 
177 	return sprintf(buf, "%s\n", operstates[operstate]);
178 }
179 
180 /* read-write attributes */
181 NETDEVICE_SHOW(mtu, fmt_dec);
182 
183 static int change_mtu(struct net_device *net, unsigned long new_mtu)
184 {
185 	return dev_set_mtu(net, (int) new_mtu);
186 }
187 
188 static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
189 			 const char *buf, size_t len)
190 {
191 	return netdev_store(dev, attr, buf, len, change_mtu);
192 }
193 
194 NETDEVICE_SHOW(flags, fmt_hex);
195 
196 static int change_flags(struct net_device *net, unsigned long new_flags)
197 {
198 	return dev_change_flags(net, (unsigned) new_flags);
199 }
200 
201 static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
202 			   const char *buf, size_t len)
203 {
204 	return netdev_store(dev, attr, buf, len, change_flags);
205 }
206 
207 NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
208 
209 static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
210 {
211 	net->tx_queue_len = new_len;
212 	return 0;
213 }
214 
215 static ssize_t store_tx_queue_len(struct device *dev,
216 				  struct device_attribute *attr,
217 				  const char *buf, size_t len)
218 {
219 	return netdev_store(dev, attr, buf, len, change_tx_queue_len);
220 }
221 
222 static struct device_attribute net_class_attributes[] = {
223 	__ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
224 	__ATTR(iflink, S_IRUGO, show_iflink, NULL),
225 	__ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
226 	__ATTR(features, S_IRUGO, show_features, NULL),
227 	__ATTR(type, S_IRUGO, show_type, NULL),
228 	__ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
229 	__ATTR(address, S_IRUGO, show_address, NULL),
230 	__ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
231 	__ATTR(carrier, S_IRUGO, show_carrier, NULL),
232 	__ATTR(dormant, S_IRUGO, show_dormant, NULL),
233 	__ATTR(operstate, S_IRUGO, show_operstate, NULL),
234 	__ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
235 	__ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
236 	__ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
237 	       store_tx_queue_len),
238 	{}
239 };
240 
241 /* Show a given an attribute in the statistics group */
242 static ssize_t netstat_show(const struct device *d,
243 			    struct device_attribute *attr, char *buf,
244 			    unsigned long offset)
245 {
246 	struct net_device *dev = to_net_dev(d);
247 	struct net_device_stats *stats;
248 	ssize_t ret = -EINVAL;
249 
250 	if (offset > sizeof(struct net_device_stats) ||
251 	    offset % sizeof(unsigned long) != 0)
252 		WARN_ON(1);
253 
254 	read_lock(&dev_base_lock);
255 	if (dev_isalive(dev) && dev->get_stats &&
256 	    (stats = (*dev->get_stats)(dev)))
257 		ret = sprintf(buf, fmt_ulong,
258 			      *(unsigned long *)(((u8 *) stats) + offset));
259 
260 	read_unlock(&dev_base_lock);
261 	return ret;
262 }
263 
264 /* generate a read-only statistics attribute */
265 #define NETSTAT_ENTRY(name)						\
266 static ssize_t show_##name(struct device *d,				\
267 			   struct device_attribute *attr, char *buf) 	\
268 {									\
269 	return netstat_show(d, attr, buf,				\
270 			    offsetof(struct net_device_stats, name));	\
271 }									\
272 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
273 
274 NETSTAT_ENTRY(rx_packets);
275 NETSTAT_ENTRY(tx_packets);
276 NETSTAT_ENTRY(rx_bytes);
277 NETSTAT_ENTRY(tx_bytes);
278 NETSTAT_ENTRY(rx_errors);
279 NETSTAT_ENTRY(tx_errors);
280 NETSTAT_ENTRY(rx_dropped);
281 NETSTAT_ENTRY(tx_dropped);
282 NETSTAT_ENTRY(multicast);
283 NETSTAT_ENTRY(collisions);
284 NETSTAT_ENTRY(rx_length_errors);
285 NETSTAT_ENTRY(rx_over_errors);
286 NETSTAT_ENTRY(rx_crc_errors);
287 NETSTAT_ENTRY(rx_frame_errors);
288 NETSTAT_ENTRY(rx_fifo_errors);
289 NETSTAT_ENTRY(rx_missed_errors);
290 NETSTAT_ENTRY(tx_aborted_errors);
291 NETSTAT_ENTRY(tx_carrier_errors);
292 NETSTAT_ENTRY(tx_fifo_errors);
293 NETSTAT_ENTRY(tx_heartbeat_errors);
294 NETSTAT_ENTRY(tx_window_errors);
295 NETSTAT_ENTRY(rx_compressed);
296 NETSTAT_ENTRY(tx_compressed);
297 
298 static struct attribute *netstat_attrs[] = {
299 	&dev_attr_rx_packets.attr,
300 	&dev_attr_tx_packets.attr,
301 	&dev_attr_rx_bytes.attr,
302 	&dev_attr_tx_bytes.attr,
303 	&dev_attr_rx_errors.attr,
304 	&dev_attr_tx_errors.attr,
305 	&dev_attr_rx_dropped.attr,
306 	&dev_attr_tx_dropped.attr,
307 	&dev_attr_multicast.attr,
308 	&dev_attr_collisions.attr,
309 	&dev_attr_rx_length_errors.attr,
310 	&dev_attr_rx_over_errors.attr,
311 	&dev_attr_rx_crc_errors.attr,
312 	&dev_attr_rx_frame_errors.attr,
313 	&dev_attr_rx_fifo_errors.attr,
314 	&dev_attr_rx_missed_errors.attr,
315 	&dev_attr_tx_aborted_errors.attr,
316 	&dev_attr_tx_carrier_errors.attr,
317 	&dev_attr_tx_fifo_errors.attr,
318 	&dev_attr_tx_heartbeat_errors.attr,
319 	&dev_attr_tx_window_errors.attr,
320 	&dev_attr_rx_compressed.attr,
321 	&dev_attr_tx_compressed.attr,
322 	NULL
323 };
324 
325 
326 static struct attribute_group netstat_group = {
327 	.name  = "statistics",
328 	.attrs  = netstat_attrs,
329 };
330 
331 #ifdef CONFIG_WIRELESS_EXT
332 /* helper function that does all the locking etc for wireless stats */
333 static ssize_t wireless_show(struct device *d, char *buf,
334 			     ssize_t (*format)(const struct iw_statistics *,
335 					       char *))
336 {
337 	struct net_device *dev = to_net_dev(d);
338 	const struct iw_statistics *iw = NULL;
339 	ssize_t ret = -EINVAL;
340 
341 	read_lock(&dev_base_lock);
342 	if (dev_isalive(dev)) {
343 		if (dev->wireless_handlers &&
344 		    dev->wireless_handlers->get_wireless_stats)
345 			iw = dev->wireless_handlers->get_wireless_stats(dev);
346 		if (iw != NULL)
347 			ret = (*format)(iw, buf);
348 	}
349 	read_unlock(&dev_base_lock);
350 
351 	return ret;
352 }
353 
354 /* show function template for wireless fields */
355 #define WIRELESS_SHOW(name, field, format_string)			\
356 static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
357 {									\
358 	return sprintf(buf, format_string, iw->field);			\
359 }									\
360 static ssize_t show_iw_##name(struct device *d,				\
361 			      struct device_attribute *attr, char *buf)	\
362 {									\
363 	return wireless_show(d, buf, format_iw_##name);			\
364 }									\
365 static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
366 
367 WIRELESS_SHOW(status, status, fmt_hex);
368 WIRELESS_SHOW(link, qual.qual, fmt_dec);
369 WIRELESS_SHOW(level, qual.level, fmt_dec);
370 WIRELESS_SHOW(noise, qual.noise, fmt_dec);
371 WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
372 WIRELESS_SHOW(crypt, discard.code, fmt_dec);
373 WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
374 WIRELESS_SHOW(misc, discard.misc, fmt_dec);
375 WIRELESS_SHOW(retries, discard.retries, fmt_dec);
376 WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
377 
378 static struct attribute *wireless_attrs[] = {
379 	&dev_attr_status.attr,
380 	&dev_attr_link.attr,
381 	&dev_attr_level.attr,
382 	&dev_attr_noise.attr,
383 	&dev_attr_nwid.attr,
384 	&dev_attr_crypt.attr,
385 	&dev_attr_fragment.attr,
386 	&dev_attr_retries.attr,
387 	&dev_attr_misc.attr,
388 	&dev_attr_beacon.attr,
389 	NULL
390 };
391 
392 static struct attribute_group wireless_group = {
393 	.name = "wireless",
394 	.attrs = wireless_attrs,
395 };
396 #endif
397 
398 #endif /* CONFIG_SYSFS */
399 
400 #ifdef CONFIG_HOTPLUG
401 static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
402 {
403 	struct net_device *dev = to_net_dev(d);
404 	int retval;
405 
406 	/* pass interface to uevent. */
407 	retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
408 	if (retval)
409 		goto exit;
410 
411 	/* pass ifindex to uevent.
412 	 * ifindex is useful as it won't change (interface name may change)
413 	 * and is what RtNetlink uses natively. */
414 	retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
415 
416 exit:
417 	return retval;
418 }
419 #endif
420 
421 /*
422  *	netdev_release -- destroy and free a dead device.
423  *	Called when last reference to device kobject is gone.
424  */
425 static void netdev_release(struct device *d)
426 {
427 	struct net_device *dev = to_net_dev(d);
428 
429 	BUG_ON(dev->reg_state != NETREG_RELEASED);
430 
431 	kfree((char *)dev - dev->padded);
432 }
433 
434 static struct class net_class = {
435 	.name = "net",
436 	.dev_release = netdev_release,
437 #ifdef CONFIG_SYSFS
438 	.dev_attrs = net_class_attributes,
439 #endif /* CONFIG_SYSFS */
440 #ifdef CONFIG_HOTPLUG
441 	.dev_uevent = netdev_uevent,
442 #endif
443 };
444 
445 /* Delete sysfs entries but hold kobject reference until after all
446  * netdev references are gone.
447  */
448 void netdev_unregister_kobject(struct net_device * net)
449 {
450 	struct device *dev = &(net->dev);
451 
452 	kobject_get(&dev->kobj);
453 	device_del(dev);
454 }
455 
456 /* Create sysfs entries for network device. */
457 int netdev_register_kobject(struct net_device *net)
458 {
459 	struct device *dev = &(net->dev);
460 	struct attribute_group **groups = net->sysfs_groups;
461 
462 	device_initialize(dev);
463 	dev->class = &net_class;
464 	dev->platform_data = net;
465 	dev->groups = groups;
466 
467 	BUILD_BUG_ON(BUS_ID_SIZE < IFNAMSIZ);
468 	strlcpy(dev->bus_id, net->name, BUS_ID_SIZE);
469 
470 #ifdef CONFIG_SYSFS
471 	if (net->get_stats)
472 		*groups++ = &netstat_group;
473 
474 #ifdef CONFIG_WIRELESS_EXT
475 	if (net->wireless_handlers && net->wireless_handlers->get_wireless_stats)
476 		*groups++ = &wireless_group;
477 #endif
478 #endif /* CONFIG_SYSFS */
479 
480 	return device_add(dev);
481 }
482 
483 int netdev_kobject_init(void)
484 {
485 	return class_register(&net_class);
486 }
487