br_sysfs_br.c 12 KB

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