br_stp.c 13 KB

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