br_sysfs_if.c 9.3 KB

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