br_stp.c 14 KB

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