br_stp_if.c 8.4 KB

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