br_sysfs_if.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "br_private.h"
  20. struct brport_attribute {
  21. struct attribute attr;
  22. ssize_t (*show)(struct net_bridge_port *, char *);
  23. int (*store)(struct net_bridge_port *, unsigned long);
  24. };
  25. #define BRPORT_ATTR(_name, _mode, _show, _store) \
  26. const struct brport_attribute brport_attr_##_name = { \
  27. .attr = {.name = __stringify(_name), \
  28. .mode = _mode }, \
  29. .show = _show, \
  30. .store = _store, \
  31. };
  32. #define BRPORT_ATTR_FLAG(_name, _mask) \
  33. static ssize_t show_##_name(struct net_bridge_port *p, char *buf) \
  34. { \
  35. return sprintf(buf, "%d\n", !!(p->flags & _mask)); \
  36. } \
  37. static int store_##_name(struct net_bridge_port *p, unsigned long v) \
  38. { \
  39. return store_flag(p, v, _mask); \
  40. } \
  41. static BRPORT_ATTR(_name, S_IRUGO | S_IWUSR, \
  42. show_##_name, store_##_name)
  43. static int store_flag(struct net_bridge_port *p, unsigned long v,
  44. unsigned long mask)
  45. {
  46. unsigned long flags;
  47. flags = p->flags;
  48. if (v)
  49. flags |= mask;
  50. else
  51. flags &= ~mask;
  52. if (flags != p->flags) {
  53. p->flags = flags;
  54. br_port_flags_change(p, mask);
  55. }
  56. return 0;
  57. }
  58. static ssize_t show_path_cost(struct net_bridge_port *p, char *buf)
  59. {
  60. return sprintf(buf, "%d\n", p->path_cost);
  61. }
  62. static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
  63. show_path_cost, br_stp_set_path_cost);
  64. static ssize_t show_priority(struct net_bridge_port *p, char *buf)
  65. {
  66. return sprintf(buf, "%d\n", p->priority);
  67. }
  68. static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
  69. show_priority, br_stp_set_port_priority);
  70. static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
  71. {
  72. return br_show_bridge_id(buf, &p->designated_root);
  73. }
  74. static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
  75. static ssize_t show_designated_bridge(struct net_bridge_port *p, char *buf)
  76. {
  77. return br_show_bridge_id(buf, &p->designated_bridge);
  78. }
  79. static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
  80. static ssize_t show_designated_port(struct net_bridge_port *p, char *buf)
  81. {
  82. return sprintf(buf, "%d\n", p->designated_port);
  83. }
  84. static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
  85. static ssize_t show_designated_cost(struct net_bridge_port *p, char *buf)
  86. {
  87. return sprintf(buf, "%d\n", p->designated_cost);
  88. }
  89. static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
  90. static ssize_t show_port_id(struct net_bridge_port *p, char *buf)
  91. {
  92. return sprintf(buf, "0x%x\n", p->port_id);
  93. }
  94. static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
  95. static ssize_t show_port_no(struct net_bridge_port *p, char *buf)
  96. {
  97. return sprintf(buf, "0x%x\n", p->port_no);
  98. }
  99. static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
  100. static ssize_t show_change_ack(struct net_bridge_port *p, char *buf)
  101. {
  102. return sprintf(buf, "%d\n", p->topology_change_ack);
  103. }
  104. static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
  105. static ssize_t show_config_pending(struct net_bridge_port *p, char *buf)
  106. {
  107. return sprintf(buf, "%d\n", p->config_pending);
  108. }
  109. static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
  110. static ssize_t show_port_state(struct net_bridge_port *p, char *buf)
  111. {
  112. return sprintf(buf, "%d\n", p->state);
  113. }
  114. static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
  115. static ssize_t show_message_age_timer(struct net_bridge_port *p,
  116. char *buf)
  117. {
  118. return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
  119. }
  120. static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
  121. static ssize_t show_forward_delay_timer(struct net_bridge_port *p,
  122. char *buf)
  123. {
  124. return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
  125. }
  126. static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
  127. static ssize_t show_hold_timer(struct net_bridge_port *p,
  128. char *buf)
  129. {
  130. return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
  131. }
  132. static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
  133. static int store_flush(struct net_bridge_port *p, unsigned long v)
  134. {
  135. br_fdb_delete_by_port(p->br, p, 0, 0); // Don't delete local entry
  136. return 0;
  137. }
  138. static BRPORT_ATTR(flush, S_IWUSR, NULL, store_flush);
  139. BRPORT_ATTR_FLAG(hairpin_mode, BR_HAIRPIN_MODE);
  140. BRPORT_ATTR_FLAG(bpdu_guard, BR_BPDU_GUARD);
  141. BRPORT_ATTR_FLAG(root_block, BR_ROOT_BLOCK);
  142. BRPORT_ATTR_FLAG(learning, BR_LEARNING);
  143. BRPORT_ATTR_FLAG(unicast_flood, BR_FLOOD);
  144. BRPORT_ATTR_FLAG(proxyarp, BR_PROXYARP);
  145. BRPORT_ATTR_FLAG(proxyarp_wifi, BR_PROXYARP_WIFI);
  146. BRPORT_ATTR_FLAG(multicast_flood, BR_MCAST_FLOOD);
  147. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  148. static ssize_t show_multicast_router(struct net_bridge_port *p, char *buf)
  149. {
  150. return sprintf(buf, "%d\n", p->multicast_router);
  151. }
  152. static int store_multicast_router(struct net_bridge_port *p,
  153. unsigned long v)
  154. {
  155. return br_multicast_set_port_router(p, v);
  156. }
  157. static BRPORT_ATTR(multicast_router, S_IRUGO | S_IWUSR, show_multicast_router,
  158. store_multicast_router);
  159. BRPORT_ATTR_FLAG(multicast_fast_leave, BR_MULTICAST_FAST_LEAVE);
  160. #endif
  161. static const struct brport_attribute *brport_attrs[] = {
  162. &brport_attr_path_cost,
  163. &brport_attr_priority,
  164. &brport_attr_port_id,
  165. &brport_attr_port_no,
  166. &brport_attr_designated_root,
  167. &brport_attr_designated_bridge,
  168. &brport_attr_designated_port,
  169. &brport_attr_designated_cost,
  170. &brport_attr_state,
  171. &brport_attr_change_ack,
  172. &brport_attr_config_pending,
  173. &brport_attr_message_age_timer,
  174. &brport_attr_forward_delay_timer,
  175. &brport_attr_hold_timer,
  176. &brport_attr_flush,
  177. &brport_attr_hairpin_mode,
  178. &brport_attr_bpdu_guard,
  179. &brport_attr_root_block,
  180. &brport_attr_learning,
  181. &brport_attr_unicast_flood,
  182. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  183. &brport_attr_multicast_router,
  184. &brport_attr_multicast_fast_leave,
  185. #endif
  186. &brport_attr_proxyarp,
  187. &brport_attr_proxyarp_wifi,
  188. &brport_attr_multicast_flood,
  189. NULL
  190. };
  191. #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
  192. #define to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
  193. static ssize_t brport_show(struct kobject *kobj,
  194. struct attribute *attr, char *buf)
  195. {
  196. struct brport_attribute *brport_attr = to_brport_attr(attr);
  197. struct net_bridge_port *p = to_brport(kobj);
  198. if (!brport_attr->show)
  199. return -EINVAL;
  200. return brport_attr->show(p, buf);
  201. }
  202. static ssize_t brport_store(struct kobject *kobj,
  203. struct attribute *attr,
  204. const char *buf, size_t count)
  205. {
  206. struct brport_attribute *brport_attr = to_brport_attr(attr);
  207. struct net_bridge_port *p = to_brport(kobj);
  208. ssize_t ret = -EINVAL;
  209. char *endp;
  210. unsigned long val;
  211. if (!ns_capable(dev_net(p->dev)->user_ns, CAP_NET_ADMIN))
  212. return -EPERM;
  213. val = simple_strtoul(buf, &endp, 0);
  214. if (endp != buf) {
  215. if (!rtnl_trylock())
  216. return restart_syscall();
  217. if (p->dev && p->br && brport_attr->store) {
  218. spin_lock_bh(&p->br->lock);
  219. ret = brport_attr->store(p, val);
  220. spin_unlock_bh(&p->br->lock);
  221. if (!ret) {
  222. br_ifinfo_notify(RTM_NEWLINK, p);
  223. ret = count;
  224. }
  225. }
  226. rtnl_unlock();
  227. }
  228. return ret;
  229. }
  230. const struct sysfs_ops brport_sysfs_ops = {
  231. .show = brport_show,
  232. .store = brport_store,
  233. };
  234. /*
  235. * Add sysfs entries to ethernet device added to a bridge.
  236. * Creates a brport subdirectory with bridge attributes.
  237. * Puts symlink in bridge's brif subdirectory
  238. */
  239. int br_sysfs_addif(struct net_bridge_port *p)
  240. {
  241. struct net_bridge *br = p->br;
  242. const struct brport_attribute **a;
  243. int err;
  244. err = sysfs_create_link(&p->kobj, &br->dev->dev.kobj,
  245. SYSFS_BRIDGE_PORT_LINK);
  246. if (err)
  247. return err;
  248. for (a = brport_attrs; *a; ++a) {
  249. err = sysfs_create_file(&p->kobj, &((*a)->attr));
  250. if (err)
  251. return err;
  252. }
  253. strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
  254. return sysfs_create_link(br->ifobj, &p->kobj, p->sysfs_name);
  255. }
  256. /* Rename bridge's brif symlink */
  257. int br_sysfs_renameif(struct net_bridge_port *p)
  258. {
  259. struct net_bridge *br = p->br;
  260. int err;
  261. /* If a rename fails, the rollback will cause another
  262. * rename call with the existing name.
  263. */
  264. if (!strncmp(p->sysfs_name, p->dev->name, IFNAMSIZ))
  265. return 0;
  266. err = sysfs_rename_link(br->ifobj, &p->kobj,
  267. p->sysfs_name, p->dev->name);
  268. if (err)
  269. netdev_notice(br->dev, "unable to rename link %s to %s",
  270. p->sysfs_name, p->dev->name);
  271. else
  272. strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
  273. return err;
  274. }