br_sysfs_br.c 12 KB

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