br_sysfs_br.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. rtnl_lock();
  131. spin_unlock_bh(&br->lock);
  132. br_stp_set_enabled(br, val);
  133. spin_lock_bh(&br->lock);
  134. rtnl_unlock();
  135. }
  136. static ssize_t store_stp_state(struct device *d,
  137. struct device_attribute *attr, const char *buf,
  138. size_t len)
  139. {
  140. return store_bridge_parm(d, buf, len, set_stp_state);
  141. }
  142. static DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
  143. store_stp_state);
  144. static ssize_t show_priority(struct device *d, struct device_attribute *attr,
  145. char *buf)
  146. {
  147. struct net_bridge *br = to_bridge(d);
  148. return sprintf(buf, "%d\n",
  149. (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
  150. }
  151. static void set_priority(struct net_bridge *br, unsigned long val)
  152. {
  153. br_stp_set_bridge_priority(br, (u16) val);
  154. }
  155. static ssize_t store_priority(struct device *d, struct device_attribute *attr,
  156. const char *buf, size_t len)
  157. {
  158. return store_bridge_parm(d, buf, len, set_priority);
  159. }
  160. static DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
  161. static ssize_t show_root_id(struct device *d, struct device_attribute *attr,
  162. char *buf)
  163. {
  164. return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
  165. }
  166. static DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
  167. static ssize_t show_bridge_id(struct device *d, struct device_attribute *attr,
  168. char *buf)
  169. {
  170. return br_show_bridge_id(buf, &to_bridge(d)->bridge_id);
  171. }
  172. static DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
  173. static ssize_t show_root_port(struct device *d, struct device_attribute *attr,
  174. char *buf)
  175. {
  176. return sprintf(buf, "%d\n", to_bridge(d)->root_port);
  177. }
  178. static DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
  179. static ssize_t show_root_path_cost(struct device *d,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
  183. }
  184. static DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
  185. static ssize_t show_topology_change(struct device *d,
  186. struct device_attribute *attr, char *buf)
  187. {
  188. return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
  189. }
  190. static DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
  191. static ssize_t show_topology_change_detected(struct device *d,
  192. struct device_attribute *attr,
  193. char *buf)
  194. {
  195. struct net_bridge *br = to_bridge(d);
  196. return sprintf(buf, "%d\n", br->topology_change_detected);
  197. }
  198. static DEVICE_ATTR(topology_change_detected, S_IRUGO,
  199. show_topology_change_detected, NULL);
  200. static ssize_t show_hello_timer(struct device *d,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. struct net_bridge *br = to_bridge(d);
  204. return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
  205. }
  206. static DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
  207. static ssize_t show_tcn_timer(struct device *d, struct device_attribute *attr,
  208. char *buf)
  209. {
  210. struct net_bridge *br = to_bridge(d);
  211. return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
  212. }
  213. static DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
  214. static ssize_t show_topology_change_timer(struct device *d,
  215. struct device_attribute *attr,
  216. char *buf)
  217. {
  218. struct net_bridge *br = to_bridge(d);
  219. return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
  220. }
  221. static DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
  222. NULL);
  223. static ssize_t show_gc_timer(struct device *d, struct device_attribute *attr,
  224. char *buf)
  225. {
  226. struct net_bridge *br = to_bridge(d);
  227. return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
  228. }
  229. static DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
  230. static ssize_t show_group_addr(struct device *d,
  231. struct device_attribute *attr, char *buf)
  232. {
  233. struct net_bridge *br = to_bridge(d);
  234. return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
  235. br->group_addr[0], br->group_addr[1],
  236. br->group_addr[2], br->group_addr[3],
  237. br->group_addr[4], br->group_addr[5]);
  238. }
  239. static ssize_t store_group_addr(struct device *d,
  240. struct device_attribute *attr,
  241. const char *buf, size_t len)
  242. {
  243. struct net_bridge *br = to_bridge(d);
  244. unsigned new_addr[6];
  245. int i;
  246. if (!capable(CAP_NET_ADMIN))
  247. return -EPERM;
  248. if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
  249. &new_addr[0], &new_addr[1], &new_addr[2],
  250. &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
  251. return -EINVAL;
  252. /* Must be 01:80:c2:00:00:0X */
  253. for (i = 0; i < 5; i++)
  254. if (new_addr[i] != br_group_address[i])
  255. return -EINVAL;
  256. if (new_addr[5] & ~0xf)
  257. return -EINVAL;
  258. if (new_addr[5] == 1 /* 802.3x Pause address */
  259. || new_addr[5] == 2 /* 802.3ad Slow protocols */
  260. || new_addr[5] == 3) /* 802.1X PAE address */
  261. return -EINVAL;
  262. spin_lock_bh(&br->lock);
  263. for (i = 0; i < 6; i++)
  264. br->group_addr[i] = new_addr[i];
  265. spin_unlock_bh(&br->lock);
  266. return len;
  267. }
  268. static DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
  269. show_group_addr, store_group_addr);
  270. static ssize_t store_flush(struct device *d,
  271. struct device_attribute *attr,
  272. const char *buf, size_t len)
  273. {
  274. struct net_bridge *br = to_bridge(d);
  275. if (!capable(CAP_NET_ADMIN))
  276. return -EPERM;
  277. br_fdb_flush(br);
  278. return len;
  279. }
  280. static DEVICE_ATTR(flush, S_IWUSR, NULL, store_flush);
  281. static struct attribute *bridge_attrs[] = {
  282. &dev_attr_forward_delay.attr,
  283. &dev_attr_hello_time.attr,
  284. &dev_attr_max_age.attr,
  285. &dev_attr_ageing_time.attr,
  286. &dev_attr_stp_state.attr,
  287. &dev_attr_priority.attr,
  288. &dev_attr_bridge_id.attr,
  289. &dev_attr_root_id.attr,
  290. &dev_attr_root_path_cost.attr,
  291. &dev_attr_root_port.attr,
  292. &dev_attr_topology_change.attr,
  293. &dev_attr_topology_change_detected.attr,
  294. &dev_attr_hello_timer.attr,
  295. &dev_attr_tcn_timer.attr,
  296. &dev_attr_topology_change_timer.attr,
  297. &dev_attr_gc_timer.attr,
  298. &dev_attr_group_addr.attr,
  299. &dev_attr_flush.attr,
  300. NULL
  301. };
  302. static struct attribute_group bridge_group = {
  303. .name = SYSFS_BRIDGE_ATTR,
  304. .attrs = bridge_attrs,
  305. };
  306. /*
  307. * Export the forwarding information table as a binary file
  308. * The records are struct __fdb_entry.
  309. *
  310. * Returns the number of bytes read.
  311. */
  312. static ssize_t brforward_read(struct kobject *kobj, char *buf,
  313. loff_t off, size_t count)
  314. {
  315. struct device *dev = to_dev(kobj);
  316. struct net_bridge *br = to_bridge(dev);
  317. int n;
  318. /* must read whole records */
  319. if (off % sizeof(struct __fdb_entry) != 0)
  320. return -EINVAL;
  321. n = br_fdb_fillbuf(br, buf,
  322. count / sizeof(struct __fdb_entry),
  323. off / sizeof(struct __fdb_entry));
  324. if (n > 0)
  325. n *= sizeof(struct __fdb_entry);
  326. return n;
  327. }
  328. static struct bin_attribute bridge_forward = {
  329. .attr = { .name = SYSFS_BRIDGE_FDB,
  330. .mode = S_IRUGO,
  331. .owner = THIS_MODULE, },
  332. .read = brforward_read,
  333. };
  334. /*
  335. * Add entries in sysfs onto the existing network class device
  336. * for the bridge.
  337. * Adds a attribute group "bridge" containing tuning parameters.
  338. * Binary attribute containing the forward table
  339. * Sub directory to hold links to interfaces.
  340. *
  341. * Note: the ifobj exists only to be a subdirectory
  342. * to hold links. The ifobj exists in same data structure
  343. * as it's parent the bridge so reference counting works.
  344. */
  345. int br_sysfs_addbr(struct net_device *dev)
  346. {
  347. struct kobject *brobj = &dev->dev.kobj;
  348. struct net_bridge *br = netdev_priv(dev);
  349. int err;
  350. err = sysfs_create_group(brobj, &bridge_group);
  351. if (err) {
  352. pr_info("%s: can't create group %s/%s\n",
  353. __FUNCTION__, dev->name, bridge_group.name);
  354. goto out1;
  355. }
  356. err = sysfs_create_bin_file(brobj, &bridge_forward);
  357. if (err) {
  358. pr_info("%s: can't create attribute file %s/%s\n",
  359. __FUNCTION__, dev->name, bridge_forward.attr.name);
  360. goto out2;
  361. }
  362. kobject_set_name(&br->ifobj, SYSFS_BRIDGE_PORT_SUBDIR);
  363. br->ifobj.ktype = NULL;
  364. br->ifobj.kset = NULL;
  365. br->ifobj.parent = brobj;
  366. err = kobject_register(&br->ifobj);
  367. if (err) {
  368. pr_info("%s: can't add kobject (directory) %s/%s\n",
  369. __FUNCTION__, dev->name, br->ifobj.name);
  370. goto out3;
  371. }
  372. return 0;
  373. out3:
  374. sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
  375. out2:
  376. sysfs_remove_group(&dev->dev.kobj, &bridge_group);
  377. out1:
  378. return err;
  379. }
  380. void br_sysfs_delbr(struct net_device *dev)
  381. {
  382. struct kobject *kobj = &dev->dev.kobj;
  383. struct net_bridge *br = netdev_priv(dev);
  384. kobject_unregister(&br->ifobj);
  385. sysfs_remove_bin_file(kobj, &bridge_forward);
  386. sysfs_remove_group(kobj, &bridge_group);
  387. }