br_stp_if.c 8.7 KB

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