br_sysfs_if.c 11 KB

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