net-sysfs.c 15 KB

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