br_sysfs_br.c 12 KB

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