br_stp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Spanning tree protocol; generic parts
  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/rculist.h>
  15. #include "br_private.h"
  16. #include "br_private_stp.h"
  17. /* since time values in bpdu are in jiffies and then scaled (1/256)
  18. * before sending, make sure that is at least one STP tick.
  19. */
  20. #define MESSAGE_AGE_INCR ((HZ / 256) + 1)
  21. static const char *const br_port_state_names[] = {
  22. [BR_STATE_DISABLED] = "disabled",
  23. [BR_STATE_LISTENING] = "listening",
  24. [BR_STATE_LEARNING] = "learning",
  25. [BR_STATE_FORWARDING] = "forwarding",
  26. [BR_STATE_BLOCKING] = "blocking",
  27. };
  28. void br_log_state(const struct net_bridge_port *p)
  29. {
  30. br_info(p->br, "port %u(%s) entered %s state\n",
  31. (unsigned) p->port_no, p->dev->name,
  32. br_port_state_names[p->state]);
  33. }
  34. /* called under bridge lock */
  35. struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no)
  36. {
  37. struct net_bridge_port *p;
  38. list_for_each_entry_rcu(p, &br->port_list, list) {
  39. if (p->port_no == port_no)
  40. return p;
  41. }
  42. return NULL;
  43. }
  44. /* called under bridge lock */
  45. static int br_should_become_root_port(const struct net_bridge_port *p,
  46. u16 root_port)
  47. {
  48. struct net_bridge *br;
  49. struct net_bridge_port *rp;
  50. int t;
  51. br = p->br;
  52. if (p->state == BR_STATE_DISABLED ||
  53. br_is_designated_port(p))
  54. return 0;
  55. if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
  56. return 0;
  57. if (!root_port)
  58. return 1;
  59. rp = br_get_port(br, root_port);
  60. t = memcmp(&p->designated_root, &rp->designated_root, 8);
  61. if (t < 0)
  62. return 1;
  63. else if (t > 0)
  64. return 0;
  65. if (p->designated_cost + p->path_cost <
  66. rp->designated_cost + rp->path_cost)
  67. return 1;
  68. else if (p->designated_cost + p->path_cost >
  69. rp->designated_cost + rp->path_cost)
  70. return 0;
  71. t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
  72. if (t < 0)
  73. return 1;
  74. else if (t > 0)
  75. return 0;
  76. if (p->designated_port < rp->designated_port)
  77. return 1;
  78. else if (p->designated_port > rp->designated_port)
  79. return 0;
  80. if (p->port_id < rp->port_id)
  81. return 1;
  82. return 0;
  83. }
  84. /* called under bridge lock */
  85. static void br_root_selection(struct net_bridge *br)
  86. {
  87. struct net_bridge_port *p;
  88. u16 root_port = 0;
  89. list_for_each_entry(p, &br->port_list, list) {
  90. if (br_should_become_root_port(p, root_port))
  91. root_port = p->port_no;
  92. }
  93. br->root_port = root_port;
  94. if (!root_port) {
  95. br->designated_root = br->bridge_id;
  96. br->root_path_cost = 0;
  97. } else {
  98. p = br_get_port(br, root_port);
  99. br->designated_root = p->designated_root;
  100. br->root_path_cost = p->designated_cost + p->path_cost;
  101. }
  102. }
  103. /* called under bridge lock */
  104. void br_become_root_bridge(struct net_bridge *br)
  105. {
  106. br->max_age = br->bridge_max_age;
  107. br->hello_time = br->bridge_hello_time;
  108. br->forward_delay = br->bridge_forward_delay;
  109. br_topology_change_detection(br);
  110. del_timer(&br->tcn_timer);
  111. if (br->dev->flags & IFF_UP) {
  112. br_config_bpdu_generation(br);
  113. mod_timer(&br->hello_timer, jiffies + br->hello_time);
  114. }
  115. }
  116. /* called under bridge lock */
  117. void br_transmit_config(struct net_bridge_port *p)
  118. {
  119. struct br_config_bpdu bpdu;
  120. struct net_bridge *br;
  121. if (timer_pending(&p->hold_timer)) {
  122. p->config_pending = 1;
  123. return;
  124. }
  125. br = p->br;
  126. bpdu.topology_change = br->topology_change;
  127. bpdu.topology_change_ack = p->topology_change_ack;
  128. bpdu.root = br->designated_root;
  129. bpdu.root_path_cost = br->root_path_cost;
  130. bpdu.bridge_id = br->bridge_id;
  131. bpdu.port_id = p->port_id;
  132. if (br_is_root_bridge(br))
  133. bpdu.message_age = 0;
  134. else {
  135. struct net_bridge_port *root
  136. = br_get_port(br, br->root_port);
  137. bpdu.message_age = (jiffies - root->designated_age)
  138. + MESSAGE_AGE_INCR;
  139. }
  140. bpdu.max_age = br->max_age;
  141. bpdu.hello_time = br->hello_time;
  142. bpdu.forward_delay = br->forward_delay;
  143. if (bpdu.message_age < br->max_age) {
  144. br_send_config_bpdu(p, &bpdu);
  145. p->topology_change_ack = 0;
  146. p->config_pending = 0;
  147. mod_timer(&p->hold_timer,
  148. round_jiffies(jiffies + BR_HOLD_TIME));
  149. }
  150. }
  151. /* called under bridge lock */
  152. static void br_record_config_information(struct net_bridge_port *p,
  153. const struct br_config_bpdu *bpdu)
  154. {
  155. p->designated_root = bpdu->root;
  156. p->designated_cost = bpdu->root_path_cost;
  157. p->designated_bridge = bpdu->bridge_id;
  158. p->designated_port = bpdu->port_id;
  159. p->designated_age = jiffies - bpdu->message_age;
  160. mod_timer(&p->message_age_timer, jiffies
  161. + (bpdu->max_age - bpdu->message_age));
  162. }
  163. /* called under bridge lock */
  164. static void br_record_config_timeout_values(struct net_bridge *br,
  165. const struct br_config_bpdu *bpdu)
  166. {
  167. br->max_age = bpdu->max_age;
  168. br->hello_time = bpdu->hello_time;
  169. br->forward_delay = bpdu->forward_delay;
  170. br->topology_change = bpdu->topology_change;
  171. }
  172. /* called under bridge lock */
  173. void br_transmit_tcn(struct net_bridge *br)
  174. {
  175. br_send_tcn_bpdu(br_get_port(br, br->root_port));
  176. }
  177. /* called under bridge lock */
  178. static int br_should_become_designated_port(const struct net_bridge_port *p)
  179. {
  180. struct net_bridge *br;
  181. int t;
  182. br = p->br;
  183. if (br_is_designated_port(p))
  184. return 1;
  185. if (memcmp(&p->designated_root, &br->designated_root, 8))
  186. return 1;
  187. if (br->root_path_cost < p->designated_cost)
  188. return 1;
  189. else if (br->root_path_cost > p->designated_cost)
  190. return 0;
  191. t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
  192. if (t < 0)
  193. return 1;
  194. else if (t > 0)
  195. return 0;
  196. if (p->port_id < p->designated_port)
  197. return 1;
  198. return 0;
  199. }
  200. /* called under bridge lock */
  201. static void br_designated_port_selection(struct net_bridge *br)
  202. {
  203. struct net_bridge_port *p;
  204. list_for_each_entry(p, &br->port_list, list) {
  205. if (p->state != BR_STATE_DISABLED &&
  206. br_should_become_designated_port(p))
  207. br_become_designated_port(p);
  208. }
  209. }
  210. /* called under bridge lock */
  211. static int br_supersedes_port_info(const struct net_bridge_port *p,
  212. const struct br_config_bpdu *bpdu)
  213. {
  214. int t;
  215. t = memcmp(&bpdu->root, &p->designated_root, 8);
  216. if (t < 0)
  217. return 1;
  218. else if (t > 0)
  219. return 0;
  220. if (bpdu->root_path_cost < p->designated_cost)
  221. return 1;
  222. else if (bpdu->root_path_cost > p->designated_cost)
  223. return 0;
  224. t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
  225. if (t < 0)
  226. return 1;
  227. else if (t > 0)
  228. return 0;
  229. if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
  230. return 1;
  231. if (bpdu->port_id <= p->designated_port)
  232. return 1;
  233. return 0;
  234. }
  235. /* called under bridge lock */
  236. static void br_topology_change_acknowledged(struct net_bridge *br)
  237. {
  238. br->topology_change_detected = 0;
  239. del_timer(&br->tcn_timer);
  240. }
  241. /* called under bridge lock */
  242. void br_topology_change_detection(struct net_bridge *br)
  243. {
  244. int isroot = br_is_root_bridge(br);
  245. if (br->stp_enabled != BR_KERNEL_STP)
  246. return;
  247. br_info(br, "topology change detected, %s\n",
  248. isroot ? "propagating" : "sending tcn bpdu");
  249. if (isroot) {
  250. br->topology_change = 1;
  251. mod_timer(&br->topology_change_timer, jiffies
  252. + br->bridge_forward_delay + br->bridge_max_age);
  253. } else if (!br->topology_change_detected) {
  254. br_transmit_tcn(br);
  255. mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
  256. }
  257. br->topology_change_detected = 1;
  258. }
  259. /* called under bridge lock */
  260. void br_config_bpdu_generation(struct net_bridge *br)
  261. {
  262. struct net_bridge_port *p;
  263. list_for_each_entry(p, &br->port_list, list) {
  264. if (p->state != BR_STATE_DISABLED &&
  265. br_is_designated_port(p))
  266. br_transmit_config(p);
  267. }
  268. }
  269. /* called under bridge lock */
  270. static void br_reply(struct net_bridge_port *p)
  271. {
  272. br_transmit_config(p);
  273. }
  274. /* called under bridge lock */
  275. void br_configuration_update(struct net_bridge *br)
  276. {
  277. br_root_selection(br);
  278. br_designated_port_selection(br);
  279. }
  280. /* called under bridge lock */
  281. void br_become_designated_port(struct net_bridge_port *p)
  282. {
  283. struct net_bridge *br;
  284. br = p->br;
  285. p->designated_root = br->designated_root;
  286. p->designated_cost = br->root_path_cost;
  287. p->designated_bridge = br->bridge_id;
  288. p->designated_port = p->port_id;
  289. }
  290. /* called under bridge lock */
  291. static void br_make_blocking(struct net_bridge_port *p)
  292. {
  293. if (p->state != BR_STATE_DISABLED &&
  294. p->state != BR_STATE_BLOCKING) {
  295. if (p->state == BR_STATE_FORWARDING ||
  296. p->state == BR_STATE_LEARNING)
  297. br_topology_change_detection(p->br);
  298. p->state = BR_STATE_BLOCKING;
  299. br_log_state(p);
  300. br_ifinfo_notify(RTM_NEWLINK, p);
  301. del_timer(&p->forward_delay_timer);
  302. }
  303. }
  304. /* called under bridge lock */
  305. static void br_make_forwarding(struct net_bridge_port *p)
  306. {
  307. struct net_bridge *br = p->br;
  308. if (p->state != BR_STATE_BLOCKING)
  309. return;
  310. if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) {
  311. p->state = BR_STATE_FORWARDING;
  312. br_topology_change_detection(br);
  313. del_timer(&p->forward_delay_timer);
  314. } else if (br->stp_enabled == BR_KERNEL_STP)
  315. p->state = BR_STATE_LISTENING;
  316. else
  317. p->state = BR_STATE_LEARNING;
  318. br_multicast_enable_port(p);
  319. br_log_state(p);
  320. br_ifinfo_notify(RTM_NEWLINK, p);
  321. if (br->forward_delay != 0)
  322. mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
  323. }
  324. /* called under bridge lock */
  325. void br_port_state_selection(struct net_bridge *br)
  326. {
  327. struct net_bridge_port *p;
  328. unsigned int liveports = 0;
  329. list_for_each_entry(p, &br->port_list, list) {
  330. if (p->state == BR_STATE_DISABLED)
  331. continue;
  332. /* Don't change port states if userspace is handling STP */
  333. if (br->stp_enabled != BR_USER_STP) {
  334. if (p->port_no == br->root_port) {
  335. p->config_pending = 0;
  336. p->topology_change_ack = 0;
  337. br_make_forwarding(p);
  338. } else if (br_is_designated_port(p)) {
  339. del_timer(&p->message_age_timer);
  340. br_make_forwarding(p);
  341. } else {
  342. p->config_pending = 0;
  343. p->topology_change_ack = 0;
  344. br_make_blocking(p);
  345. }
  346. }
  347. if (p->state == BR_STATE_FORWARDING)
  348. ++liveports;
  349. }
  350. if (liveports == 0)
  351. netif_carrier_off(br->dev);
  352. else
  353. netif_carrier_on(br->dev);
  354. }
  355. /* called under bridge lock */
  356. static void br_topology_change_acknowledge(struct net_bridge_port *p)
  357. {
  358. p->topology_change_ack = 1;
  359. br_transmit_config(p);
  360. }
  361. /* called under bridge lock */
  362. void br_received_config_bpdu(struct net_bridge_port *p,
  363. const struct br_config_bpdu *bpdu)
  364. {
  365. struct net_bridge *br;
  366. int was_root;
  367. br = p->br;
  368. was_root = br_is_root_bridge(br);
  369. if (br_supersedes_port_info(p, bpdu)) {
  370. br_record_config_information(p, bpdu);
  371. br_configuration_update(br);
  372. br_port_state_selection(br);
  373. if (!br_is_root_bridge(br) && was_root) {
  374. del_timer(&br->hello_timer);
  375. if (br->topology_change_detected) {
  376. del_timer(&br->topology_change_timer);
  377. br_transmit_tcn(br);
  378. mod_timer(&br->tcn_timer,
  379. jiffies + br->bridge_hello_time);
  380. }
  381. }
  382. if (p->port_no == br->root_port) {
  383. br_record_config_timeout_values(br, bpdu);
  384. br_config_bpdu_generation(br);
  385. if (bpdu->topology_change_ack)
  386. br_topology_change_acknowledged(br);
  387. }
  388. } else if (br_is_designated_port(p)) {
  389. br_reply(p);
  390. }
  391. }
  392. /* called under bridge lock */
  393. void br_received_tcn_bpdu(struct net_bridge_port *p)
  394. {
  395. if (br_is_designated_port(p)) {
  396. br_info(p->br, "port %u(%s) received tcn bpdu\n",
  397. (unsigned) p->port_no, p->dev->name);
  398. br_topology_change_detection(p->br);
  399. br_topology_change_acknowledge(p);
  400. }
  401. }
  402. /* Change bridge STP parameter */
  403. int br_set_hello_time(struct net_bridge *br, unsigned long val)
  404. {
  405. unsigned long t = clock_t_to_jiffies(val);
  406. if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME)
  407. return -ERANGE;
  408. spin_lock_bh(&br->lock);
  409. br->bridge_hello_time = t;
  410. if (br_is_root_bridge(br))
  411. br->hello_time = br->bridge_hello_time;
  412. spin_unlock_bh(&br->lock);
  413. return 0;
  414. }
  415. int br_set_max_age(struct net_bridge *br, unsigned long val)
  416. {
  417. unsigned long t = clock_t_to_jiffies(val);
  418. if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE)
  419. return -ERANGE;
  420. spin_lock_bh(&br->lock);
  421. br->bridge_max_age = t;
  422. if (br_is_root_bridge(br))
  423. br->max_age = br->bridge_max_age;
  424. spin_unlock_bh(&br->lock);
  425. return 0;
  426. }
  427. void __br_set_forward_delay(struct net_bridge *br, unsigned long t)
  428. {
  429. br->bridge_forward_delay = t;
  430. if (br_is_root_bridge(br))
  431. br->forward_delay = br->bridge_forward_delay;
  432. }
  433. int br_set_forward_delay(struct net_bridge *br, unsigned long val)
  434. {
  435. unsigned long t = clock_t_to_jiffies(val);
  436. int err = -ERANGE;
  437. spin_lock_bh(&br->lock);
  438. if (br->stp_enabled != BR_NO_STP &&
  439. (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY))
  440. goto unlock;
  441. __br_set_forward_delay(br, t);
  442. err = 0;
  443. unlock:
  444. spin_unlock_bh(&br->lock);
  445. return err;
  446. }