xref: /openbmc/linux/net/bridge/br_stp.c (revision 15e3ae36)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Spanning tree protocol; generic parts
4  *	Linux ethernet bridge
5  *
6  *	Authors:
7  *	Lennert Buytenhek		<buytenh@gnu.org>
8  */
9 #include <linux/kernel.h>
10 #include <linux/rculist.h>
11 #include <net/switchdev.h>
12 
13 #include "br_private.h"
14 #include "br_private_stp.h"
15 
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.
18  */
19 #define MESSAGE_AGE_INCR	((HZ / 256) + 1)
20 
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 
29 void br_set_state(struct net_bridge_port *p, unsigned int state)
30 {
31 	struct switchdev_attr attr = {
32 		.orig_dev = p->dev,
33 		.id = SWITCHDEV_ATTR_ID_PORT_STP_STATE,
34 		.flags = SWITCHDEV_F_DEFER,
35 		.u.stp_state = state,
36 	};
37 	int err;
38 
39 	p->state = state;
40 	err = switchdev_port_attr_set(p->dev, &attr);
41 	if (err && err != -EOPNOTSUPP)
42 		br_warn(p->br, "error setting offload STP state on port %u(%s)\n",
43 				(unsigned int) p->port_no, p->dev->name);
44 	else
45 		br_info(p->br, "port %u(%s) entered %s state\n",
46 				(unsigned int) p->port_no, p->dev->name,
47 				br_port_state_names[p->state]);
48 
49 	if (p->br->stp_enabled == BR_KERNEL_STP) {
50 		switch (p->state) {
51 		case BR_STATE_BLOCKING:
52 			p->stp_xstats.transition_blk++;
53 			break;
54 		case BR_STATE_FORWARDING:
55 			p->stp_xstats.transition_fwd++;
56 			break;
57 		}
58 	}
59 }
60 
61 /* called under bridge lock */
62 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no)
63 {
64 	struct net_bridge_port *p;
65 
66 	list_for_each_entry_rcu(p, &br->port_list, list,
67 				lockdep_is_held(&br->lock)) {
68 		if (p->port_no == port_no)
69 			return p;
70 	}
71 
72 	return NULL;
73 }
74 
75 /* called under bridge lock */
76 static int br_should_become_root_port(const struct net_bridge_port *p,
77 				      u16 root_port)
78 {
79 	struct net_bridge *br;
80 	struct net_bridge_port *rp;
81 	int t;
82 
83 	br = p->br;
84 	if (p->state == BR_STATE_DISABLED ||
85 	    br_is_designated_port(p))
86 		return 0;
87 
88 	if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
89 		return 0;
90 
91 	if (!root_port)
92 		return 1;
93 
94 	rp = br_get_port(br, root_port);
95 
96 	t = memcmp(&p->designated_root, &rp->designated_root, 8);
97 	if (t < 0)
98 		return 1;
99 	else if (t > 0)
100 		return 0;
101 
102 	if (p->designated_cost + p->path_cost <
103 	    rp->designated_cost + rp->path_cost)
104 		return 1;
105 	else if (p->designated_cost + p->path_cost >
106 		 rp->designated_cost + rp->path_cost)
107 		return 0;
108 
109 	t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
110 	if (t < 0)
111 		return 1;
112 	else if (t > 0)
113 		return 0;
114 
115 	if (p->designated_port < rp->designated_port)
116 		return 1;
117 	else if (p->designated_port > rp->designated_port)
118 		return 0;
119 
120 	if (p->port_id < rp->port_id)
121 		return 1;
122 
123 	return 0;
124 }
125 
126 static void br_root_port_block(const struct net_bridge *br,
127 			       struct net_bridge_port *p)
128 {
129 
130 	br_notice(br, "port %u(%s) tried to become root port (blocked)",
131 		  (unsigned int) p->port_no, p->dev->name);
132 
133 	br_set_state(p, BR_STATE_LISTENING);
134 	br_ifinfo_notify(RTM_NEWLINK, NULL, p);
135 
136 	if (br->forward_delay > 0)
137 		mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
138 }
139 
140 /* called under bridge lock */
141 static void br_root_selection(struct net_bridge *br)
142 {
143 	struct net_bridge_port *p;
144 	u16 root_port = 0;
145 
146 	list_for_each_entry(p, &br->port_list, list) {
147 		if (!br_should_become_root_port(p, root_port))
148 			continue;
149 
150 		if (p->flags & BR_ROOT_BLOCK)
151 			br_root_port_block(br, p);
152 		else
153 			root_port = p->port_no;
154 	}
155 
156 	br->root_port = root_port;
157 
158 	if (!root_port) {
159 		br->designated_root = br->bridge_id;
160 		br->root_path_cost = 0;
161 	} else {
162 		p = br_get_port(br, root_port);
163 		br->designated_root = p->designated_root;
164 		br->root_path_cost = p->designated_cost + p->path_cost;
165 	}
166 }
167 
168 /* called under bridge lock */
169 void br_become_root_bridge(struct net_bridge *br)
170 {
171 	br->max_age = br->bridge_max_age;
172 	br->hello_time = br->bridge_hello_time;
173 	br->forward_delay = br->bridge_forward_delay;
174 	br_topology_change_detection(br);
175 	del_timer(&br->tcn_timer);
176 
177 	if (br->dev->flags & IFF_UP) {
178 		br_config_bpdu_generation(br);
179 		mod_timer(&br->hello_timer, jiffies + br->hello_time);
180 	}
181 }
182 
183 /* called under bridge lock */
184 void br_transmit_config(struct net_bridge_port *p)
185 {
186 	struct br_config_bpdu bpdu;
187 	struct net_bridge *br;
188 
189 	if (timer_pending(&p->hold_timer)) {
190 		p->config_pending = 1;
191 		return;
192 	}
193 
194 	br = p->br;
195 
196 	bpdu.topology_change = br->topology_change;
197 	bpdu.topology_change_ack = p->topology_change_ack;
198 	bpdu.root = br->designated_root;
199 	bpdu.root_path_cost = br->root_path_cost;
200 	bpdu.bridge_id = br->bridge_id;
201 	bpdu.port_id = p->port_id;
202 	if (br_is_root_bridge(br))
203 		bpdu.message_age = 0;
204 	else {
205 		struct net_bridge_port *root
206 			= br_get_port(br, br->root_port);
207 		bpdu.message_age = (jiffies - root->designated_age)
208 			+ MESSAGE_AGE_INCR;
209 	}
210 	bpdu.max_age = br->max_age;
211 	bpdu.hello_time = br->hello_time;
212 	bpdu.forward_delay = br->forward_delay;
213 
214 	if (bpdu.message_age < br->max_age) {
215 		br_send_config_bpdu(p, &bpdu);
216 		p->topology_change_ack = 0;
217 		p->config_pending = 0;
218 		if (p->br->stp_enabled == BR_KERNEL_STP)
219 			mod_timer(&p->hold_timer,
220 				  round_jiffies(jiffies + BR_HOLD_TIME));
221 	}
222 }
223 
224 /* called under bridge lock */
225 static void br_record_config_information(struct net_bridge_port *p,
226 					 const struct br_config_bpdu *bpdu)
227 {
228 	p->designated_root = bpdu->root;
229 	p->designated_cost = bpdu->root_path_cost;
230 	p->designated_bridge = bpdu->bridge_id;
231 	p->designated_port = bpdu->port_id;
232 	p->designated_age = jiffies - bpdu->message_age;
233 
234 	mod_timer(&p->message_age_timer, jiffies
235 		  + (bpdu->max_age - bpdu->message_age));
236 }
237 
238 /* called under bridge lock */
239 static void br_record_config_timeout_values(struct net_bridge *br,
240 					    const struct br_config_bpdu *bpdu)
241 {
242 	br->max_age = bpdu->max_age;
243 	br->hello_time = bpdu->hello_time;
244 	br->forward_delay = bpdu->forward_delay;
245 	__br_set_topology_change(br, bpdu->topology_change);
246 }
247 
248 /* called under bridge lock */
249 void br_transmit_tcn(struct net_bridge *br)
250 {
251 	struct net_bridge_port *p;
252 
253 	p = br_get_port(br, br->root_port);
254 	if (p)
255 		br_send_tcn_bpdu(p);
256 	else
257 		br_notice(br, "root port %u not found for topology notice\n",
258 			  br->root_port);
259 }
260 
261 /* called under bridge lock */
262 static int br_should_become_designated_port(const struct net_bridge_port *p)
263 {
264 	struct net_bridge *br;
265 	int t;
266 
267 	br = p->br;
268 	if (br_is_designated_port(p))
269 		return 1;
270 
271 	if (memcmp(&p->designated_root, &br->designated_root, 8))
272 		return 1;
273 
274 	if (br->root_path_cost < p->designated_cost)
275 		return 1;
276 	else if (br->root_path_cost > p->designated_cost)
277 		return 0;
278 
279 	t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
280 	if (t < 0)
281 		return 1;
282 	else if (t > 0)
283 		return 0;
284 
285 	if (p->port_id < p->designated_port)
286 		return 1;
287 
288 	return 0;
289 }
290 
291 /* called under bridge lock */
292 static void br_designated_port_selection(struct net_bridge *br)
293 {
294 	struct net_bridge_port *p;
295 
296 	list_for_each_entry(p, &br->port_list, list) {
297 		if (p->state != BR_STATE_DISABLED &&
298 		    br_should_become_designated_port(p))
299 			br_become_designated_port(p);
300 
301 	}
302 }
303 
304 /* called under bridge lock */
305 static int br_supersedes_port_info(const struct net_bridge_port *p,
306 				   const struct br_config_bpdu *bpdu)
307 {
308 	int t;
309 
310 	t = memcmp(&bpdu->root, &p->designated_root, 8);
311 	if (t < 0)
312 		return 1;
313 	else if (t > 0)
314 		return 0;
315 
316 	if (bpdu->root_path_cost < p->designated_cost)
317 		return 1;
318 	else if (bpdu->root_path_cost > p->designated_cost)
319 		return 0;
320 
321 	t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
322 	if (t < 0)
323 		return 1;
324 	else if (t > 0)
325 		return 0;
326 
327 	if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
328 		return 1;
329 
330 	if (bpdu->port_id <= p->designated_port)
331 		return 1;
332 
333 	return 0;
334 }
335 
336 /* called under bridge lock */
337 static void br_topology_change_acknowledged(struct net_bridge *br)
338 {
339 	br->topology_change_detected = 0;
340 	del_timer(&br->tcn_timer);
341 }
342 
343 /* called under bridge lock */
344 void br_topology_change_detection(struct net_bridge *br)
345 {
346 	int isroot = br_is_root_bridge(br);
347 
348 	if (br->stp_enabled != BR_KERNEL_STP)
349 		return;
350 
351 	br_info(br, "topology change detected, %s\n",
352 		isroot ? "propagating" : "sending tcn bpdu");
353 
354 	if (isroot) {
355 		__br_set_topology_change(br, 1);
356 		mod_timer(&br->topology_change_timer, jiffies
357 			  + br->bridge_forward_delay + br->bridge_max_age);
358 	} else if (!br->topology_change_detected) {
359 		br_transmit_tcn(br);
360 		mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
361 	}
362 
363 	br->topology_change_detected = 1;
364 }
365 
366 /* called under bridge lock */
367 void br_config_bpdu_generation(struct net_bridge *br)
368 {
369 	struct net_bridge_port *p;
370 
371 	list_for_each_entry(p, &br->port_list, list) {
372 		if (p->state != BR_STATE_DISABLED &&
373 		    br_is_designated_port(p))
374 			br_transmit_config(p);
375 	}
376 }
377 
378 /* called under bridge lock */
379 static void br_reply(struct net_bridge_port *p)
380 {
381 	br_transmit_config(p);
382 }
383 
384 /* called under bridge lock */
385 void br_configuration_update(struct net_bridge *br)
386 {
387 	br_root_selection(br);
388 	br_designated_port_selection(br);
389 }
390 
391 /* called under bridge lock */
392 void br_become_designated_port(struct net_bridge_port *p)
393 {
394 	struct net_bridge *br;
395 
396 	br = p->br;
397 	p->designated_root = br->designated_root;
398 	p->designated_cost = br->root_path_cost;
399 	p->designated_bridge = br->bridge_id;
400 	p->designated_port = p->port_id;
401 }
402 
403 
404 /* called under bridge lock */
405 static void br_make_blocking(struct net_bridge_port *p)
406 {
407 	if (p->state != BR_STATE_DISABLED &&
408 	    p->state != BR_STATE_BLOCKING) {
409 		if (p->state == BR_STATE_FORWARDING ||
410 		    p->state == BR_STATE_LEARNING)
411 			br_topology_change_detection(p->br);
412 
413 		br_set_state(p, BR_STATE_BLOCKING);
414 		br_ifinfo_notify(RTM_NEWLINK, NULL, p);
415 
416 		del_timer(&p->forward_delay_timer);
417 	}
418 }
419 
420 /* called under bridge lock */
421 static void br_make_forwarding(struct net_bridge_port *p)
422 {
423 	struct net_bridge *br = p->br;
424 
425 	if (p->state != BR_STATE_BLOCKING)
426 		return;
427 
428 	if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) {
429 		br_set_state(p, BR_STATE_FORWARDING);
430 		br_topology_change_detection(br);
431 		del_timer(&p->forward_delay_timer);
432 	} else if (br->stp_enabled == BR_KERNEL_STP)
433 		br_set_state(p, BR_STATE_LISTENING);
434 	else
435 		br_set_state(p, BR_STATE_LEARNING);
436 
437 	br_ifinfo_notify(RTM_NEWLINK, NULL, p);
438 
439 	if (br->forward_delay != 0)
440 		mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
441 }
442 
443 /* called under bridge lock */
444 void br_port_state_selection(struct net_bridge *br)
445 {
446 	struct net_bridge_port *p;
447 	unsigned int liveports = 0;
448 
449 	list_for_each_entry(p, &br->port_list, list) {
450 		if (p->state == BR_STATE_DISABLED)
451 			continue;
452 
453 		/* Don't change port states if userspace is handling STP */
454 		if (br->stp_enabled != BR_USER_STP) {
455 			if (p->port_no == br->root_port) {
456 				p->config_pending = 0;
457 				p->topology_change_ack = 0;
458 				br_make_forwarding(p);
459 			} else if (br_is_designated_port(p)) {
460 				del_timer(&p->message_age_timer);
461 				br_make_forwarding(p);
462 			} else {
463 				p->config_pending = 0;
464 				p->topology_change_ack = 0;
465 				br_make_blocking(p);
466 			}
467 		}
468 
469 		if (p->state != BR_STATE_BLOCKING)
470 			br_multicast_enable_port(p);
471 		/* Multicast is not disabled for the port when it goes in
472 		 * blocking state because the timers will expire and stop by
473 		 * themselves without sending more queries.
474 		 */
475 		if (p->state == BR_STATE_FORWARDING)
476 			++liveports;
477 	}
478 
479 	if (liveports == 0)
480 		netif_carrier_off(br->dev);
481 	else
482 		netif_carrier_on(br->dev);
483 }
484 
485 /* called under bridge lock */
486 static void br_topology_change_acknowledge(struct net_bridge_port *p)
487 {
488 	p->topology_change_ack = 1;
489 	br_transmit_config(p);
490 }
491 
492 /* called under bridge lock */
493 void br_received_config_bpdu(struct net_bridge_port *p,
494 			     const struct br_config_bpdu *bpdu)
495 {
496 	struct net_bridge *br;
497 	int was_root;
498 
499 	p->stp_xstats.rx_bpdu++;
500 
501 	br = p->br;
502 	was_root = br_is_root_bridge(br);
503 
504 	if (br_supersedes_port_info(p, bpdu)) {
505 		br_record_config_information(p, bpdu);
506 		br_configuration_update(br);
507 		br_port_state_selection(br);
508 
509 		if (!br_is_root_bridge(br) && was_root) {
510 			del_timer(&br->hello_timer);
511 			if (br->topology_change_detected) {
512 				del_timer(&br->topology_change_timer);
513 				br_transmit_tcn(br);
514 
515 				mod_timer(&br->tcn_timer,
516 					  jiffies + br->bridge_hello_time);
517 			}
518 		}
519 
520 		if (p->port_no == br->root_port) {
521 			br_record_config_timeout_values(br, bpdu);
522 			br_config_bpdu_generation(br);
523 			if (bpdu->topology_change_ack)
524 				br_topology_change_acknowledged(br);
525 		}
526 	} else if (br_is_designated_port(p)) {
527 		br_reply(p);
528 	}
529 }
530 
531 /* called under bridge lock */
532 void br_received_tcn_bpdu(struct net_bridge_port *p)
533 {
534 	p->stp_xstats.rx_tcn++;
535 
536 	if (br_is_designated_port(p)) {
537 		br_info(p->br, "port %u(%s) received tcn bpdu\n",
538 			(unsigned int) p->port_no, p->dev->name);
539 
540 		br_topology_change_detection(p->br);
541 		br_topology_change_acknowledge(p);
542 	}
543 }
544 
545 /* Change bridge STP parameter */
546 int br_set_hello_time(struct net_bridge *br, unsigned long val)
547 {
548 	unsigned long t = clock_t_to_jiffies(val);
549 
550 	if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME)
551 		return -ERANGE;
552 
553 	spin_lock_bh(&br->lock);
554 	br->bridge_hello_time = t;
555 	if (br_is_root_bridge(br))
556 		br->hello_time = br->bridge_hello_time;
557 	spin_unlock_bh(&br->lock);
558 	return 0;
559 }
560 
561 int br_set_max_age(struct net_bridge *br, unsigned long val)
562 {
563 	unsigned long t = clock_t_to_jiffies(val);
564 
565 	if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE)
566 		return -ERANGE;
567 
568 	spin_lock_bh(&br->lock);
569 	br->bridge_max_age = t;
570 	if (br_is_root_bridge(br))
571 		br->max_age = br->bridge_max_age;
572 	spin_unlock_bh(&br->lock);
573 	return 0;
574 
575 }
576 
577 /* called under bridge lock */
578 int __set_ageing_time(struct net_device *dev, unsigned long t)
579 {
580 	struct switchdev_attr attr = {
581 		.orig_dev = dev,
582 		.id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
583 		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER,
584 		.u.ageing_time = jiffies_to_clock_t(t),
585 	};
586 	int err;
587 
588 	err = switchdev_port_attr_set(dev, &attr);
589 	if (err && err != -EOPNOTSUPP)
590 		return err;
591 
592 	return 0;
593 }
594 
595 /* Set time interval that dynamic forwarding entries live
596  * For pure software bridge, allow values outside the 802.1
597  * standard specification for special cases:
598  *  0 - entry never ages (all permanant)
599  *  1 - entry disappears (no persistance)
600  *
601  * Offloaded switch entries maybe more restrictive
602  */
603 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time)
604 {
605 	unsigned long t = clock_t_to_jiffies(ageing_time);
606 	int err;
607 
608 	err = __set_ageing_time(br->dev, t);
609 	if (err)
610 		return err;
611 
612 	spin_lock_bh(&br->lock);
613 	br->bridge_ageing_time = t;
614 	br->ageing_time = t;
615 	spin_unlock_bh(&br->lock);
616 
617 	mod_delayed_work(system_long_wq, &br->gc_work, 0);
618 
619 	return 0;
620 }
621 
622 /* called under bridge lock */
623 void __br_set_topology_change(struct net_bridge *br, unsigned char val)
624 {
625 	unsigned long t;
626 	int err;
627 
628 	if (br->stp_enabled == BR_KERNEL_STP && br->topology_change != val) {
629 		/* On topology change, set the bridge ageing time to twice the
630 		 * forward delay. Otherwise, restore its default ageing time.
631 		 */
632 
633 		if (val) {
634 			t = 2 * br->forward_delay;
635 			br_debug(br, "decreasing ageing time to %lu\n", t);
636 		} else {
637 			t = br->bridge_ageing_time;
638 			br_debug(br, "restoring ageing time to %lu\n", t);
639 		}
640 
641 		err = __set_ageing_time(br->dev, t);
642 		if (err)
643 			br_warn(br, "error offloading ageing time\n");
644 		else
645 			br->ageing_time = t;
646 	}
647 
648 	br->topology_change = val;
649 }
650 
651 void __br_set_forward_delay(struct net_bridge *br, unsigned long t)
652 {
653 	br->bridge_forward_delay = t;
654 	if (br_is_root_bridge(br))
655 		br->forward_delay = br->bridge_forward_delay;
656 }
657 
658 int br_set_forward_delay(struct net_bridge *br, unsigned long val)
659 {
660 	unsigned long t = clock_t_to_jiffies(val);
661 	int err = -ERANGE;
662 
663 	spin_lock_bh(&br->lock);
664 	if (br->stp_enabled != BR_NO_STP &&
665 	    (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY))
666 		goto unlock;
667 
668 	__br_set_forward_delay(br, t);
669 	err = 0;
670 
671 unlock:
672 	spin_unlock_bh(&br->lock);
673 	return err;
674 }
675