br_stp_if.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Spanning tree protocol; interface code
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.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/kernel.h>
  14. #include <linux/kmod.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <net/switchdev.h>
  18. #include "br_private.h"
  19. #include "br_private_stp.h"
  20. /* Port id is composed of priority and port number.
  21. * NB: some bits of priority are dropped to
  22. * make room for more ports.
  23. */
  24. static inline port_id br_make_port_id(__u8 priority, __u16 port_no)
  25. {
  26. return ((u16)priority << BR_PORT_BITS)
  27. | (port_no & ((1<<BR_PORT_BITS)-1));
  28. }
  29. #define BR_MAX_PORT_PRIORITY ((u16)~0 >> BR_PORT_BITS)
  30. /* called under bridge lock */
  31. void br_init_port(struct net_bridge_port *p)
  32. {
  33. struct switchdev_attr attr = {
  34. .orig_dev = p->dev,
  35. .id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
  36. .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER,
  37. .u.ageing_time = jiffies_to_clock_t(p->br->ageing_time),
  38. };
  39. int err;
  40. p->port_id = br_make_port_id(p->priority, p->port_no);
  41. br_become_designated_port(p);
  42. br_set_state(p, BR_STATE_BLOCKING);
  43. p->topology_change_ack = 0;
  44. p->config_pending = 0;
  45. err = switchdev_port_attr_set(p->dev, &attr);
  46. if (err && err != -EOPNOTSUPP)
  47. netdev_err(p->dev, "failed to set HW ageing time\n");
  48. }
  49. /* NO locks held */
  50. void br_stp_enable_bridge(struct net_bridge *br)
  51. {
  52. struct net_bridge_port *p;
  53. spin_lock_bh(&br->lock);
  54. if (br->stp_enabled == BR_KERNEL_STP)
  55. mod_timer(&br->hello_timer, jiffies + br->hello_time);
  56. mod_timer(&br->gc_timer, jiffies + HZ/10);
  57. br_config_bpdu_generation(br);
  58. list_for_each_entry(p, &br->port_list, list) {
  59. if (netif_running(p->dev) && netif_oper_up(p->dev))
  60. br_stp_enable_port(p);
  61. }
  62. spin_unlock_bh(&br->lock);
  63. }
  64. /* NO locks held */
  65. void br_stp_disable_bridge(struct net_bridge *br)
  66. {
  67. struct net_bridge_port *p;
  68. spin_lock_bh(&br->lock);
  69. list_for_each_entry(p, &br->port_list, list) {
  70. if (p->state != BR_STATE_DISABLED)
  71. br_stp_disable_port(p);
  72. }
  73. br->topology_change = 0;
  74. br->topology_change_detected = 0;
  75. spin_unlock_bh(&br->lock);
  76. del_timer_sync(&br->hello_timer);
  77. del_timer_sync(&br->topology_change_timer);
  78. del_timer_sync(&br->tcn_timer);
  79. del_timer_sync(&br->gc_timer);
  80. }
  81. /* called under bridge lock */
  82. void br_stp_enable_port(struct net_bridge_port *p)
  83. {
  84. br_init_port(p);
  85. br_port_state_selection(p->br);
  86. br_ifinfo_notify(RTM_NEWLINK, p);
  87. }
  88. /* called under bridge lock */
  89. void br_stp_disable_port(struct net_bridge_port *p)
  90. {
  91. struct net_bridge *br = p->br;
  92. int wasroot;
  93. wasroot = br_is_root_bridge(br);
  94. br_become_designated_port(p);
  95. br_set_state(p, BR_STATE_DISABLED);
  96. p->topology_change_ack = 0;
  97. p->config_pending = 0;
  98. br_ifinfo_notify(RTM_NEWLINK, p);
  99. del_timer(&p->message_age_timer);
  100. del_timer(&p->forward_delay_timer);
  101. del_timer(&p->hold_timer);
  102. br_fdb_delete_by_port(br, p, 0, 0);
  103. br_multicast_disable_port(p);
  104. br_configuration_update(br);
  105. br_port_state_selection(br);
  106. if (br_is_root_bridge(br) && !wasroot)
  107. br_become_root_bridge(br);
  108. }
  109. static int br_stp_call_user(struct net_bridge *br, char *arg)
  110. {
  111. char *argv[] = { BR_STP_PROG, br->dev->name, arg, NULL };
  112. char *envp[] = { NULL };
  113. int rc;
  114. /* call userspace STP and report program errors */
  115. rc = call_usermodehelper(BR_STP_PROG, argv, envp, UMH_WAIT_PROC);
  116. if (rc > 0) {
  117. if (rc & 0xff)
  118. br_debug(br, BR_STP_PROG " received signal %d\n",
  119. rc & 0x7f);
  120. else
  121. br_debug(br, BR_STP_PROG " exited with code %d\n",
  122. (rc >> 8) & 0xff);
  123. }
  124. return rc;
  125. }
  126. static void br_stp_start(struct net_bridge *br)
  127. {
  128. struct net_bridge_port *p;
  129. int err = -ENOENT;
  130. if (net_eq(dev_net(br->dev), &init_net))
  131. err = br_stp_call_user(br, "start");
  132. if (err && err != -ENOENT)
  133. br_err(br, "failed to start userspace STP (%d)\n", err);
  134. spin_lock_bh(&br->lock);
  135. if (br->bridge_forward_delay < BR_MIN_FORWARD_DELAY)
  136. __br_set_forward_delay(br, BR_MIN_FORWARD_DELAY);
  137. else if (br->bridge_forward_delay > BR_MAX_FORWARD_DELAY)
  138. __br_set_forward_delay(br, BR_MAX_FORWARD_DELAY);
  139. if (!err) {
  140. br->stp_enabled = BR_USER_STP;
  141. br_debug(br, "userspace STP started\n");
  142. /* Stop hello and hold timers */
  143. del_timer(&br->hello_timer);
  144. list_for_each_entry(p, &br->port_list, list)
  145. del_timer(&p->hold_timer);
  146. } else {
  147. br->stp_enabled = BR_KERNEL_STP;
  148. br_debug(br, "using kernel STP\n");
  149. /* To start timers on any ports left in blocking */
  150. if (br->dev->flags & IFF_UP)
  151. mod_timer(&br->hello_timer, jiffies + br->hello_time);
  152. br_port_state_selection(br);
  153. }
  154. spin_unlock_bh(&br->lock);
  155. }
  156. static void br_stp_stop(struct net_bridge *br)
  157. {
  158. struct net_bridge_port *p;
  159. int err;
  160. if (br->stp_enabled == BR_USER_STP) {
  161. err = br_stp_call_user(br, "stop");
  162. if (err)
  163. br_err(br, "failed to stop userspace STP (%d)\n", err);
  164. /* To start timers on any ports left in blocking */
  165. mod_timer(&br->hello_timer, jiffies + br->hello_time);
  166. list_for_each_entry(p, &br->port_list, list)
  167. mod_timer(&p->hold_timer,
  168. round_jiffies(jiffies + BR_HOLD_TIME));
  169. spin_lock_bh(&br->lock);
  170. br_port_state_selection(br);
  171. spin_unlock_bh(&br->lock);
  172. }
  173. br->stp_enabled = BR_NO_STP;
  174. }
  175. void br_stp_set_enabled(struct net_bridge *br, unsigned long val)
  176. {
  177. ASSERT_RTNL();
  178. if (val) {
  179. if (br->stp_enabled == BR_NO_STP)
  180. br_stp_start(br);
  181. } else {
  182. if (br->stp_enabled != BR_NO_STP)
  183. br_stp_stop(br);
  184. }
  185. }
  186. /* called under bridge lock */
  187. void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *addr)
  188. {
  189. /* should be aligned on 2 bytes for ether_addr_equal() */
  190. unsigned short oldaddr_aligned[ETH_ALEN >> 1];
  191. unsigned char *oldaddr = (unsigned char *)oldaddr_aligned;
  192. struct net_bridge_port *p;
  193. int wasroot;
  194. wasroot = br_is_root_bridge(br);
  195. br_fdb_change_mac_address(br, addr);
  196. memcpy(oldaddr, br->bridge_id.addr, ETH_ALEN);
  197. memcpy(br->bridge_id.addr, addr, ETH_ALEN);
  198. memcpy(br->dev->dev_addr, addr, ETH_ALEN);
  199. list_for_each_entry(p, &br->port_list, list) {
  200. if (ether_addr_equal(p->designated_bridge.addr, oldaddr))
  201. memcpy(p->designated_bridge.addr, addr, ETH_ALEN);
  202. if (ether_addr_equal(p->designated_root.addr, oldaddr))
  203. memcpy(p->designated_root.addr, addr, ETH_ALEN);
  204. }
  205. br_configuration_update(br);
  206. br_port_state_selection(br);
  207. if (br_is_root_bridge(br) && !wasroot)
  208. br_become_root_bridge(br);
  209. }
  210. /* should be aligned on 2 bytes for ether_addr_equal() */
  211. static const unsigned short br_mac_zero_aligned[ETH_ALEN >> 1];
  212. /* called under bridge lock */
  213. bool br_stp_recalculate_bridge_id(struct net_bridge *br)
  214. {
  215. const unsigned char *br_mac_zero =
  216. (const unsigned char *)br_mac_zero_aligned;
  217. const unsigned char *addr = br_mac_zero;
  218. struct net_bridge_port *p;
  219. /* user has chosen a value so keep it */
  220. if (br->dev->addr_assign_type == NET_ADDR_SET)
  221. return false;
  222. list_for_each_entry(p, &br->port_list, list) {
  223. if (addr == br_mac_zero ||
  224. memcmp(p->dev->dev_addr, addr, ETH_ALEN) < 0)
  225. addr = p->dev->dev_addr;
  226. }
  227. if (ether_addr_equal(br->bridge_id.addr, addr))
  228. return false; /* no change */
  229. br_stp_change_bridge_id(br, addr);
  230. return true;
  231. }
  232. /* Acquires and releases bridge lock */
  233. void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio)
  234. {
  235. struct net_bridge_port *p;
  236. int wasroot;
  237. spin_lock_bh(&br->lock);
  238. wasroot = br_is_root_bridge(br);
  239. list_for_each_entry(p, &br->port_list, list) {
  240. if (p->state != BR_STATE_DISABLED &&
  241. br_is_designated_port(p)) {
  242. p->designated_bridge.prio[0] = (newprio >> 8) & 0xFF;
  243. p->designated_bridge.prio[1] = newprio & 0xFF;
  244. }
  245. }
  246. br->bridge_id.prio[0] = (newprio >> 8) & 0xFF;
  247. br->bridge_id.prio[1] = newprio & 0xFF;
  248. br_configuration_update(br);
  249. br_port_state_selection(br);
  250. if (br_is_root_bridge(br) && !wasroot)
  251. br_become_root_bridge(br);
  252. spin_unlock_bh(&br->lock);
  253. }
  254. /* called under bridge lock */
  255. int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio)
  256. {
  257. port_id new_port_id;
  258. if (newprio > BR_MAX_PORT_PRIORITY)
  259. return -ERANGE;
  260. new_port_id = br_make_port_id(newprio, p->port_no);
  261. if (br_is_designated_port(p))
  262. p->designated_port = new_port_id;
  263. p->port_id = new_port_id;
  264. p->priority = newprio;
  265. if (!memcmp(&p->br->bridge_id, &p->designated_bridge, 8) &&
  266. p->port_id < p->designated_port) {
  267. br_become_designated_port(p);
  268. br_port_state_selection(p->br);
  269. }
  270. return 0;
  271. }
  272. /* called under bridge lock */
  273. int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost)
  274. {
  275. if (path_cost < BR_MIN_PATH_COST ||
  276. path_cost > BR_MAX_PATH_COST)
  277. return -ERANGE;
  278. p->flags |= BR_ADMIN_COST;
  279. p->path_cost = path_cost;
  280. br_configuration_update(p->br);
  281. br_port_state_selection(p->br);
  282. return 0;
  283. }
  284. ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id)
  285. {
  286. return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
  287. id->prio[0], id->prio[1],
  288. id->addr[0], id->addr[1], id->addr[2],
  289. id->addr[3], id->addr[4], id->addr[5]);
  290. }