1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Spanning tree protocol; generic parts
4 * Linux ethernet bridge
7 * Lennert Buytenhek <buytenh@gnu.org>
9 #include <linux/kernel.h>
10 #include <linux/rculist.h>
11 #include <net/switchdev.h>
13 #include "br_private.h"
14 #include "br_private_stp.h"
16 /* since time values in bpdu are in jiffies and then scaled (1/256)
17 * before sending, make sure that is at least one STP tick.
19 #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",
29 void br_set_state(struct net_bridge_port *p, unsigned int state)
31 struct switchdev_attr attr = {
33 .id = SWITCHDEV_ATTR_ID_PORT_STP_STATE,
34 .flags = SWITCHDEV_F_DEFER,
39 /* Don't change the state of the ports if they are driven by a different
42 if (p->flags & BR_MRP_AWARE)
46 err = switchdev_port_attr_set(p->dev, &attr, NULL);
47 if (err && err != -EOPNOTSUPP)
48 br_warn(p->br, "error setting offload STP state on port %u(%s)\n",
49 (unsigned int) p->port_no, p->dev->name);
51 br_info(p->br, "port %u(%s) entered %s state\n",
52 (unsigned int) p->port_no, p->dev->name,
53 br_port_state_names[p->state]);
55 if (p->br->stp_enabled == BR_KERNEL_STP) {
57 case BR_STATE_BLOCKING:
58 p->stp_xstats.transition_blk++;
60 case BR_STATE_FORWARDING:
61 p->stp_xstats.transition_fwd++;
67 u8 br_port_get_stp_state(const struct net_device *dev)
69 struct net_bridge_port *p;
73 p = br_port_get_rtnl(dev);
75 return BR_STATE_DISABLED;
79 EXPORT_SYMBOL_GPL(br_port_get_stp_state);
81 /* called under bridge lock */
82 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no)
84 struct net_bridge_port *p;
86 list_for_each_entry_rcu(p, &br->port_list, list,
87 lockdep_is_held(&br->lock)) {
88 if (p->port_no == port_no)
95 /* called under bridge lock */
96 static int br_should_become_root_port(const struct net_bridge_port *p,
99 struct net_bridge *br;
100 struct net_bridge_port *rp;
104 if (p->state == BR_STATE_DISABLED ||
105 br_is_designated_port(p))
108 if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
114 rp = br_get_port(br, root_port);
116 t = memcmp(&p->designated_root, &rp->designated_root, 8);
122 if (p->designated_cost + p->path_cost <
123 rp->designated_cost + rp->path_cost)
125 else if (p->designated_cost + p->path_cost >
126 rp->designated_cost + rp->path_cost)
129 t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
135 if (p->designated_port < rp->designated_port)
137 else if (p->designated_port > rp->designated_port)
140 if (p->port_id < rp->port_id)
146 static void br_root_port_block(const struct net_bridge *br,
147 struct net_bridge_port *p)
150 br_notice(br, "port %u(%s) tried to become root port (blocked)",
151 (unsigned int) p->port_no, p->dev->name);
153 br_set_state(p, BR_STATE_LISTENING);
154 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
156 if (br->forward_delay > 0)
157 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
160 /* called under bridge lock */
161 static void br_root_selection(struct net_bridge *br)
163 struct net_bridge_port *p;
166 list_for_each_entry(p, &br->port_list, list) {
167 if (!br_should_become_root_port(p, root_port))
170 if (p->flags & BR_ROOT_BLOCK)
171 br_root_port_block(br, p);
173 root_port = p->port_no;
176 br->root_port = root_port;
179 br->designated_root = br->bridge_id;
180 br->root_path_cost = 0;
182 p = br_get_port(br, root_port);
183 br->designated_root = p->designated_root;
184 br->root_path_cost = p->designated_cost + p->path_cost;
188 /* called under bridge lock */
189 void br_become_root_bridge(struct net_bridge *br)
191 br->max_age = br->bridge_max_age;
192 br->hello_time = br->bridge_hello_time;
193 br->forward_delay = br->bridge_forward_delay;
194 br_topology_change_detection(br);
195 del_timer(&br->tcn_timer);
197 if (br->dev->flags & IFF_UP) {
198 br_config_bpdu_generation(br);
199 mod_timer(&br->hello_timer, jiffies + br->hello_time);
203 /* called under bridge lock */
204 void br_transmit_config(struct net_bridge_port *p)
206 struct br_config_bpdu bpdu;
207 struct net_bridge *br;
209 if (timer_pending(&p->hold_timer)) {
210 p->config_pending = 1;
216 bpdu.topology_change = br->topology_change;
217 bpdu.topology_change_ack = p->topology_change_ack;
218 bpdu.root = br->designated_root;
219 bpdu.root_path_cost = br->root_path_cost;
220 bpdu.bridge_id = br->bridge_id;
221 bpdu.port_id = p->port_id;
222 if (br_is_root_bridge(br))
223 bpdu.message_age = 0;
225 struct net_bridge_port *root
226 = br_get_port(br, br->root_port);
227 bpdu.message_age = (jiffies - root->designated_age)
230 bpdu.max_age = br->max_age;
231 bpdu.hello_time = br->hello_time;
232 bpdu.forward_delay = br->forward_delay;
234 if (bpdu.message_age < br->max_age) {
235 br_send_config_bpdu(p, &bpdu);
236 p->topology_change_ack = 0;
237 p->config_pending = 0;
238 if (p->br->stp_enabled == BR_KERNEL_STP)
239 mod_timer(&p->hold_timer,
240 round_jiffies(jiffies + BR_HOLD_TIME));
244 /* called under bridge lock */
245 static void br_record_config_information(struct net_bridge_port *p,
246 const struct br_config_bpdu *bpdu)
248 p->designated_root = bpdu->root;
249 p->designated_cost = bpdu->root_path_cost;
250 p->designated_bridge = bpdu->bridge_id;
251 p->designated_port = bpdu->port_id;
252 p->designated_age = jiffies - bpdu->message_age;
254 mod_timer(&p->message_age_timer, jiffies
255 + (bpdu->max_age - bpdu->message_age));
258 /* called under bridge lock */
259 static void br_record_config_timeout_values(struct net_bridge *br,
260 const struct br_config_bpdu *bpdu)
262 br->max_age = bpdu->max_age;
263 br->hello_time = bpdu->hello_time;
264 br->forward_delay = bpdu->forward_delay;
265 __br_set_topology_change(br, bpdu->topology_change);
268 /* called under bridge lock */
269 void br_transmit_tcn(struct net_bridge *br)
271 struct net_bridge_port *p;
273 p = br_get_port(br, br->root_port);
277 br_notice(br, "root port %u not found for topology notice\n",
281 /* called under bridge lock */
282 static int br_should_become_designated_port(const struct net_bridge_port *p)
284 struct net_bridge *br;
288 if (br_is_designated_port(p))
291 if (memcmp(&p->designated_root, &br->designated_root, 8))
294 if (br->root_path_cost < p->designated_cost)
296 else if (br->root_path_cost > p->designated_cost)
299 t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
305 if (p->port_id < p->designated_port)
311 /* called under bridge lock */
312 static void br_designated_port_selection(struct net_bridge *br)
314 struct net_bridge_port *p;
316 list_for_each_entry(p, &br->port_list, list) {
317 if (p->state != BR_STATE_DISABLED &&
318 br_should_become_designated_port(p))
319 br_become_designated_port(p);
324 /* called under bridge lock */
325 static int br_supersedes_port_info(const struct net_bridge_port *p,
326 const struct br_config_bpdu *bpdu)
330 t = memcmp(&bpdu->root, &p->designated_root, 8);
336 if (bpdu->root_path_cost < p->designated_cost)
338 else if (bpdu->root_path_cost > p->designated_cost)
341 t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
347 if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
350 if (bpdu->port_id <= p->designated_port)
356 /* called under bridge lock */
357 static void br_topology_change_acknowledged(struct net_bridge *br)
359 br->topology_change_detected = 0;
360 del_timer(&br->tcn_timer);
363 /* called under bridge lock */
364 void br_topology_change_detection(struct net_bridge *br)
366 int isroot = br_is_root_bridge(br);
368 if (br->stp_enabled != BR_KERNEL_STP)
371 br_info(br, "topology change detected, %s\n",
372 isroot ? "propagating" : "sending tcn bpdu");
375 __br_set_topology_change(br, 1);
376 mod_timer(&br->topology_change_timer, jiffies
377 + br->bridge_forward_delay + br->bridge_max_age);
378 } else if (!br->topology_change_detected) {
380 mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
383 br->topology_change_detected = 1;
386 /* called under bridge lock */
387 void br_config_bpdu_generation(struct net_bridge *br)
389 struct net_bridge_port *p;
391 list_for_each_entry(p, &br->port_list, list) {
392 if (p->state != BR_STATE_DISABLED &&
393 br_is_designated_port(p))
394 br_transmit_config(p);
398 /* called under bridge lock */
399 static void br_reply(struct net_bridge_port *p)
401 br_transmit_config(p);
404 /* called under bridge lock */
405 void br_configuration_update(struct net_bridge *br)
407 br_root_selection(br);
408 br_designated_port_selection(br);
411 /* called under bridge lock */
412 void br_become_designated_port(struct net_bridge_port *p)
414 struct net_bridge *br;
417 p->designated_root = br->designated_root;
418 p->designated_cost = br->root_path_cost;
419 p->designated_bridge = br->bridge_id;
420 p->designated_port = p->port_id;
424 /* called under bridge lock */
425 static void br_make_blocking(struct net_bridge_port *p)
427 if (p->state != BR_STATE_DISABLED &&
428 p->state != BR_STATE_BLOCKING) {
429 if (p->state == BR_STATE_FORWARDING ||
430 p->state == BR_STATE_LEARNING)
431 br_topology_change_detection(p->br);
433 br_set_state(p, BR_STATE_BLOCKING);
434 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
436 del_timer(&p->forward_delay_timer);
440 /* called under bridge lock */
441 static void br_make_forwarding(struct net_bridge_port *p)
443 struct net_bridge *br = p->br;
445 if (p->state != BR_STATE_BLOCKING)
448 if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) {
449 br_set_state(p, BR_STATE_FORWARDING);
450 br_topology_change_detection(br);
451 del_timer(&p->forward_delay_timer);
452 } else if (br->stp_enabled == BR_KERNEL_STP)
453 br_set_state(p, BR_STATE_LISTENING);
455 br_set_state(p, BR_STATE_LEARNING);
457 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
459 if (br->forward_delay != 0)
460 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
463 /* called under bridge lock */
464 void br_port_state_selection(struct net_bridge *br)
466 struct net_bridge_port *p;
467 unsigned int liveports = 0;
469 list_for_each_entry(p, &br->port_list, list) {
470 if (p->state == BR_STATE_DISABLED)
473 /* Don't change port states if userspace is handling STP */
474 if (br->stp_enabled != BR_USER_STP) {
475 if (p->port_no == br->root_port) {
476 p->config_pending = 0;
477 p->topology_change_ack = 0;
478 br_make_forwarding(p);
479 } else if (br_is_designated_port(p)) {
480 del_timer(&p->message_age_timer);
481 br_make_forwarding(p);
483 p->config_pending = 0;
484 p->topology_change_ack = 0;
489 if (p->state != BR_STATE_BLOCKING)
490 br_multicast_enable_port(p);
491 /* Multicast is not disabled for the port when it goes in
492 * blocking state because the timers will expire and stop by
493 * themselves without sending more queries.
495 if (p->state == BR_STATE_FORWARDING)
500 netif_carrier_off(br->dev);
502 netif_carrier_on(br->dev);
505 /* called under bridge lock */
506 static void br_topology_change_acknowledge(struct net_bridge_port *p)
508 p->topology_change_ack = 1;
509 br_transmit_config(p);
512 /* called under bridge lock */
513 void br_received_config_bpdu(struct net_bridge_port *p,
514 const struct br_config_bpdu *bpdu)
516 struct net_bridge *br;
519 p->stp_xstats.rx_bpdu++;
522 was_root = br_is_root_bridge(br);
524 if (br_supersedes_port_info(p, bpdu)) {
525 br_record_config_information(p, bpdu);
526 br_configuration_update(br);
527 br_port_state_selection(br);
529 if (!br_is_root_bridge(br) && was_root) {
530 del_timer(&br->hello_timer);
531 if (br->topology_change_detected) {
532 del_timer(&br->topology_change_timer);
535 mod_timer(&br->tcn_timer,
536 jiffies + br->bridge_hello_time);
540 if (p->port_no == br->root_port) {
541 br_record_config_timeout_values(br, bpdu);
542 br_config_bpdu_generation(br);
543 if (bpdu->topology_change_ack)
544 br_topology_change_acknowledged(br);
546 } else if (br_is_designated_port(p)) {
551 /* called under bridge lock */
552 void br_received_tcn_bpdu(struct net_bridge_port *p)
554 p->stp_xstats.rx_tcn++;
556 if (br_is_designated_port(p)) {
557 br_info(p->br, "port %u(%s) received tcn bpdu\n",
558 (unsigned int) p->port_no, p->dev->name);
560 br_topology_change_detection(p->br);
561 br_topology_change_acknowledge(p);
565 /* Change bridge STP parameter */
566 int br_set_hello_time(struct net_bridge *br, unsigned long val)
568 unsigned long t = clock_t_to_jiffies(val);
570 if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME)
573 spin_lock_bh(&br->lock);
574 br->bridge_hello_time = t;
575 if (br_is_root_bridge(br))
576 br->hello_time = br->bridge_hello_time;
577 spin_unlock_bh(&br->lock);
581 int br_set_max_age(struct net_bridge *br, unsigned long val)
583 unsigned long t = clock_t_to_jiffies(val);
585 if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE)
588 spin_lock_bh(&br->lock);
589 br->bridge_max_age = t;
590 if (br_is_root_bridge(br))
591 br->max_age = br->bridge_max_age;
592 spin_unlock_bh(&br->lock);
597 /* called under bridge lock */
598 int __set_ageing_time(struct net_device *dev, unsigned long t)
600 struct switchdev_attr attr = {
602 .id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
603 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER,
604 .u.ageing_time = jiffies_to_clock_t(t),
608 err = switchdev_port_attr_set(dev, &attr, NULL);
609 if (err && err != -EOPNOTSUPP)
615 /* Set time interval that dynamic forwarding entries live
616 * For pure software bridge, allow values outside the 802.1
617 * standard specification for special cases:
618 * 0 - entry never ages (all permanent)
619 * 1 - entry disappears (no persistence)
621 * Offloaded switch entries maybe more restrictive
623 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time)
625 unsigned long t = clock_t_to_jiffies(ageing_time);
628 err = __set_ageing_time(br->dev, t);
632 spin_lock_bh(&br->lock);
633 br->bridge_ageing_time = t;
635 spin_unlock_bh(&br->lock);
637 mod_delayed_work(system_long_wq, &br->gc_work, 0);
642 clock_t br_get_ageing_time(const struct net_device *br_dev)
644 const struct net_bridge *br;
646 if (!netif_is_bridge_master(br_dev))
649 br = netdev_priv(br_dev);
651 return jiffies_to_clock_t(br->ageing_time);
653 EXPORT_SYMBOL_GPL(br_get_ageing_time);
655 /* called under bridge lock */
656 void __br_set_topology_change(struct net_bridge *br, unsigned char val)
661 if (br->stp_enabled == BR_KERNEL_STP && br->topology_change != val) {
662 /* On topology change, set the bridge ageing time to twice the
663 * forward delay. Otherwise, restore its default ageing time.
667 t = 2 * br->forward_delay;
668 br_debug(br, "decreasing ageing time to %lu\n", t);
670 t = br->bridge_ageing_time;
671 br_debug(br, "restoring ageing time to %lu\n", t);
674 err = __set_ageing_time(br->dev, t);
676 br_warn(br, "error offloading ageing time\n");
681 br->topology_change = val;
684 void __br_set_forward_delay(struct net_bridge *br, unsigned long t)
686 br->bridge_forward_delay = t;
687 if (br_is_root_bridge(br))
688 br->forward_delay = br->bridge_forward_delay;
691 int br_set_forward_delay(struct net_bridge *br, unsigned long val)
693 unsigned long t = clock_t_to_jiffies(val);
696 spin_lock_bh(&br->lock);
697 if (br->stp_enabled != BR_NO_STP &&
698 (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY))
701 __br_set_forward_delay(br, t);
705 spin_unlock_bh(&br->lock);