br_stp_if.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 "br_private.h"
  18. #include "br_private_stp.h"
  19. /* Port id is composed of priority and port number.
  20. * NB: some bits of priority are dropped to
  21. * make room for more ports.
  22. */
  23. static inline port_id br_make_port_id(__u8 priority, __u16 port_no)
  24. {
  25. return ((u16)priority << BR_PORT_BITS)
  26. | (port_no & ((1<<BR_PORT_BITS)-1));
  27. }
  28. #define BR_MAX_PORT_PRIORITY ((u16)~0 >> BR_PORT_BITS)
  29. /* called under bridge lock */
  30. void br_init_port(struct net_bridge_port *p)
  31. {
  32. p->port_id = br_make_port_id(p->priority, p->port_no);
  33. br_become_designated_port(p);
  34. p->state = BR_STATE_BLOCKING;
  35. p->topology_change_ack = 0;
  36. p->config_pending = 0;
  37. }
  38. /* called under bridge lock */
  39. void br_stp_enable_bridge(struct net_bridge *br)
  40. {
  41. struct net_bridge_port *p;
  42. spin_lock_bh(&br->lock);
  43. mod_timer(&br->hello_timer, jiffies + br->hello_time);
  44. mod_timer(&br->gc_timer, jiffies + HZ/10);
  45. br_config_bpdu_generation(br);
  46. list_for_each_entry(p, &br->port_list, list) {
  47. if ((p->dev->flags & IFF_UP) && netif_carrier_ok(p->dev))
  48. br_stp_enable_port(p);
  49. }
  50. spin_unlock_bh(&br->lock);
  51. }
  52. /* NO locks held */
  53. void br_stp_disable_bridge(struct net_bridge *br)
  54. {
  55. struct net_bridge_port *p;
  56. spin_lock_bh(&br->lock);
  57. list_for_each_entry(p, &br->port_list, list) {
  58. if (p->state != BR_STATE_DISABLED)
  59. br_stp_disable_port(p);
  60. }
  61. br->topology_change = 0;
  62. br->topology_change_detected = 0;
  63. spin_unlock_bh(&br->lock);
  64. del_timer_sync(&br->hello_timer);
  65. del_timer_sync(&br->topology_change_timer);
  66. del_timer_sync(&br->tcn_timer);
  67. del_timer_sync(&br->gc_timer);
  68. }
  69. /* called under bridge lock */
  70. void br_stp_enable_port(struct net_bridge_port *p)
  71. {
  72. br_init_port(p);
  73. br_port_state_selection(p->br);
  74. br_log_state(p);
  75. br_ifinfo_notify(RTM_NEWLINK, p);
  76. }
  77. /* called under bridge lock */
  78. void br_stp_disable_port(struct net_bridge_port *p)
  79. {
  80. struct net_bridge *br = p->br;
  81. int wasroot;
  82. wasroot = br_is_root_bridge(br);
  83. br_become_designated_port(p);
  84. p->state = BR_STATE_DISABLED;
  85. p->topology_change_ack = 0;
  86. p->config_pending = 0;
  87. br_log_state(p);
  88. br_ifinfo_notify(RTM_NEWLINK, p);
  89. del_timer(&p->message_age_timer);
  90. del_timer(&p->forward_delay_timer);
  91. del_timer(&p->hold_timer);
  92. br_fdb_delete_by_port(br, p, 0);
  93. br_multicast_disable_port(p);
  94. br_configuration_update(br);
  95. br_port_state_selection(br);
  96. if (br_is_root_bridge(br) && !wasroot)
  97. br_become_root_bridge(br);
  98. }
  99. static void br_stp_start(struct net_bridge *br)
  100. {
  101. int r;
  102. char *argv[] = { BR_STP_PROG, br->dev->name, "start", NULL };
  103. char *envp[] = { NULL };
  104. r = call_usermodehelper(BR_STP_PROG, argv, envp, UMH_WAIT_PROC);
  105. spin_lock_bh(&br->lock);
  106. if (br->bridge_forward_delay < BR_MIN_FORWARD_DELAY)
  107. __br_set_forward_delay(br, BR_MIN_FORWARD_DELAY);
  108. else if (br->bridge_forward_delay > BR_MAX_FORWARD_DELAY)
  109. __br_set_forward_delay(br, BR_MAX_FORWARD_DELAY);
  110. if (r == 0) {
  111. br->stp_enabled = BR_USER_STP;
  112. br_debug(br, "userspace STP started\n");
  113. } else {
  114. br->stp_enabled = BR_KERNEL_STP;
  115. br_debug(br, "using kernel STP\n");
  116. /* To start timers on any ports left in blocking */
  117. br_port_state_selection(br);
  118. }
  119. spin_unlock_bh(&br->lock);
  120. }
  121. static void br_stp_stop(struct net_bridge *br)
  122. {
  123. int r;
  124. char *argv[] = { BR_STP_PROG, br->dev->name, "stop", NULL };
  125. char *envp[] = { NULL };
  126. if (br->stp_enabled == BR_USER_STP) {
  127. r = call_usermodehelper(BR_STP_PROG, argv, envp, UMH_WAIT_PROC);
  128. br_info(br, "userspace STP stopped, return code %d\n", r);
  129. /* To start timers on any ports left in blocking */
  130. spin_lock_bh(&br->lock);
  131. br_port_state_selection(br);
  132. spin_unlock_bh(&br->lock);
  133. }
  134. br->stp_enabled = BR_NO_STP;
  135. }
  136. void br_stp_set_enabled(struct net_bridge *br, unsigned long val)
  137. {
  138. ASSERT_RTNL();
  139. if (val) {
  140. if (br->stp_enabled == BR_NO_STP)
  141. br_stp_start(br);
  142. } else {
  143. if (br->stp_enabled != BR_NO_STP)
  144. br_stp_stop(br);
  145. }
  146. }
  147. /* called under bridge lock */
  148. void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *addr)
  149. {
  150. /* should be aligned on 2 bytes for compare_ether_addr() */
  151. unsigned short oldaddr_aligned[ETH_ALEN >> 1];
  152. unsigned char *oldaddr = (unsigned char *)oldaddr_aligned;
  153. struct net_bridge_port *p;
  154. int wasroot;
  155. wasroot = br_is_root_bridge(br);
  156. memcpy(oldaddr, br->bridge_id.addr, ETH_ALEN);
  157. memcpy(br->bridge_id.addr, addr, ETH_ALEN);
  158. memcpy(br->dev->dev_addr, addr, ETH_ALEN);
  159. list_for_each_entry(p, &br->port_list, list) {
  160. if (!compare_ether_addr(p->designated_bridge.addr, oldaddr))
  161. memcpy(p->designated_bridge.addr, addr, ETH_ALEN);
  162. if (!compare_ether_addr(p->designated_root.addr, oldaddr))
  163. memcpy(p->designated_root.addr, addr, ETH_ALEN);
  164. }
  165. br_configuration_update(br);
  166. br_port_state_selection(br);
  167. if (br_is_root_bridge(br) && !wasroot)
  168. br_become_root_bridge(br);
  169. }
  170. /* should be aligned on 2 bytes for compare_ether_addr() */
  171. static const unsigned short br_mac_zero_aligned[ETH_ALEN >> 1];
  172. /* called under bridge lock */
  173. bool br_stp_recalculate_bridge_id(struct net_bridge *br)
  174. {
  175. const unsigned char *br_mac_zero =
  176. (const unsigned char *)br_mac_zero_aligned;
  177. const unsigned char *addr = br_mac_zero;
  178. struct net_bridge_port *p;
  179. /* user has chosen a value so keep it */
  180. if (br->flags & BR_SET_MAC_ADDR)
  181. return false;
  182. list_for_each_entry(p, &br->port_list, list) {
  183. if (addr == br_mac_zero ||
  184. memcmp(p->dev->dev_addr, addr, ETH_ALEN) < 0)
  185. addr = p->dev->dev_addr;
  186. }
  187. if (compare_ether_addr(br->bridge_id.addr, addr) == 0)
  188. return false; /* no change */
  189. br_stp_change_bridge_id(br, addr);
  190. return true;
  191. }
  192. /* Acquires and releases bridge lock */
  193. void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio)
  194. {
  195. struct net_bridge_port *p;
  196. int wasroot;
  197. spin_lock_bh(&br->lock);
  198. wasroot = br_is_root_bridge(br);
  199. list_for_each_entry(p, &br->port_list, list) {
  200. if (p->state != BR_STATE_DISABLED &&
  201. br_is_designated_port(p)) {
  202. p->designated_bridge.prio[0] = (newprio >> 8) & 0xFF;
  203. p->designated_bridge.prio[1] = newprio & 0xFF;
  204. }
  205. }
  206. br->bridge_id.prio[0] = (newprio >> 8) & 0xFF;
  207. br->bridge_id.prio[1] = newprio & 0xFF;
  208. br_configuration_update(br);
  209. br_port_state_selection(br);
  210. if (br_is_root_bridge(br) && !wasroot)
  211. br_become_root_bridge(br);
  212. spin_unlock_bh(&br->lock);
  213. }
  214. /* called under bridge lock */
  215. int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio)
  216. {
  217. port_id new_port_id;
  218. if (newprio > BR_MAX_PORT_PRIORITY)
  219. return -ERANGE;
  220. new_port_id = br_make_port_id(newprio, p->port_no);
  221. if (br_is_designated_port(p))
  222. p->designated_port = new_port_id;
  223. p->port_id = new_port_id;
  224. p->priority = newprio;
  225. if (!memcmp(&p->br->bridge_id, &p->designated_bridge, 8) &&
  226. p->port_id < p->designated_port) {
  227. br_become_designated_port(p);
  228. br_port_state_selection(p->br);
  229. }
  230. return 0;
  231. }
  232. /* called under bridge lock */
  233. int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost)
  234. {
  235. if (path_cost < BR_MIN_PATH_COST ||
  236. path_cost > BR_MAX_PATH_COST)
  237. return -ERANGE;
  238. p->path_cost = path_cost;
  239. br_configuration_update(p->br);
  240. br_port_state_selection(p->br);
  241. return 0;
  242. }
  243. ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id)
  244. {
  245. return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
  246. id->prio[0], id->prio[1],
  247. id->addr[0], id->addr[1], id->addr[2],
  248. id->addr[3], id->addr[4], id->addr[5]);
  249. }