1 /*
2  *	Spanning tree protocol; generic parts
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Lennert Buytenhek		<buytenh@gnu.org>
7  *
8  *	$Id: br_stp.c,v 1.4 2000/06/19 10:13:35 davem Exp $
9  *
10  *	This program is free software; you can redistribute it and/or
11  *	modify it under the terms of the GNU General Public License
12  *	as published by the Free Software Foundation; either version
13  *	2 of the License, or (at your option) any later version.
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/if_bridge.h>
18 #include <linux/smp_lock.h>
19 #include <asm/uaccess.h>
20 #include "br_private.h"
21 #include "br_private_stp.h"
22 
23 /* since time values in bpdu are in jiffies and then scaled (1/256)
24  * before sending, make sure that is at least one.
25  */
26 #define MESSAGE_AGE_INCR	((HZ < 256) ? 1 : (HZ/256))
27 
28 /* called under ioctl_lock or bridge lock */
br_is_root_bridge(struct net_bridge * br)29 int br_is_root_bridge(struct net_bridge *br)
30 {
31 	return !memcmp(&br->bridge_id, &br->designated_root, 8);
32 }
33 
34 /* called under bridge lock */
br_is_designated_port(struct net_bridge_port * p)35 int br_is_designated_port(struct net_bridge_port *p)
36 {
37 	return !memcmp(&p->designated_bridge, &p->br->bridge_id, 8) &&
38 		(p->designated_port == p->port_id);
39 }
40 
41 /* called under ioctl_lock or bridge lock */
br_get_port(struct net_bridge * br,int port_no)42 struct net_bridge_port *br_get_port(struct net_bridge *br, int port_no)
43 {
44 	struct net_bridge_port *p;
45 
46 	p = br->port_list;
47 	while (p != NULL) {
48 		if (p->port_no == port_no)
49 			return p;
50 
51 		p = p->next;
52 	}
53 
54 	return NULL;
55 }
56 
57 /* called under bridge lock */
br_should_become_root_port(struct net_bridge_port * p,int root_port)58 static int br_should_become_root_port(struct net_bridge_port *p, int root_port)
59 {
60 	struct net_bridge *br;
61 	struct net_bridge_port *rp;
62 	int t;
63 
64 	br = p->br;
65 	if (p->state == BR_STATE_DISABLED ||
66 	    br_is_designated_port(p))
67 		return 0;
68 
69 	if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
70 		return 0;
71 
72 	if (!root_port)
73 		return 1;
74 
75 	rp = br_get_port(br, root_port);
76 
77 	t = memcmp(&p->designated_root, &rp->designated_root, 8);
78 	if (t < 0)
79 		return 1;
80 	else if (t > 0)
81 		return 0;
82 
83 	if (p->designated_cost + p->path_cost <
84 	    rp->designated_cost + rp->path_cost)
85 		return 1;
86 	else if (p->designated_cost + p->path_cost >
87 		 rp->designated_cost + rp->path_cost)
88 		return 0;
89 
90 	t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
91 	if (t < 0)
92 		return 1;
93 	else if (t > 0)
94 		return 0;
95 
96 	if (p->designated_port < rp->designated_port)
97 		return 1;
98 	else if (p->designated_port > rp->designated_port)
99 		return 0;
100 
101 	if (p->port_id < rp->port_id)
102 		return 1;
103 
104 	return 0;
105 }
106 
107 /* called under bridge lock */
br_root_selection(struct net_bridge * br)108 static void br_root_selection(struct net_bridge *br)
109 {
110 	struct net_bridge_port *p;
111 	int root_port;
112 
113 	root_port = 0;
114 
115 	p = br->port_list;
116 	while (p != NULL) {
117 		if (br_should_become_root_port(p, root_port))
118 			root_port = p->port_no;
119 
120 		p = p->next;
121 	}
122 
123 	br->root_port = root_port;
124 
125 	if (!root_port) {
126 		br->designated_root = br->bridge_id;
127 		br->root_path_cost = 0;
128 	} else {
129 		p = br_get_port(br, root_port);
130 		br->designated_root = p->designated_root;
131 		br->root_path_cost = p->designated_cost + p->path_cost;
132 	}
133 }
134 
135 /* called under bridge lock */
br_become_root_bridge(struct net_bridge * br)136 void br_become_root_bridge(struct net_bridge *br)
137 {
138 	br->max_age = br->bridge_max_age;
139 	br->hello_time = br->bridge_hello_time;
140 	br->forward_delay = br->bridge_forward_delay;
141 	br_topology_change_detection(br);
142 	br_timer_clear(&br->tcn_timer);
143 
144 	br_timer_set(&br->hello_timer, jiffies + br->hello_time);
145 }
146 
147 /* called under bridge lock */
br_transmit_config(struct net_bridge_port * p)148 void br_transmit_config(struct net_bridge_port *p)
149 {
150 	struct br_config_bpdu bpdu;
151 	struct net_bridge *br;
152 
153 	if (br_timer_is_running(&p->hold_timer)) {
154 		p->config_pending = 1;
155 		return;
156 	}
157 
158 	br = p->br;
159 
160 	bpdu.topology_change = br->topology_change;
161 	bpdu.topology_change_ack = p->topology_change_ack;
162 	bpdu.root = br->designated_root;
163 	bpdu.root_path_cost = br->root_path_cost;
164 	bpdu.bridge_id = br->bridge_id;
165 	bpdu.port_id = p->port_id;
166 	if (br_is_root_bridge(br))
167 		bpdu.message_age = 0;
168 	else {
169 		struct net_bridge_port *root;
170 
171 		root = br_get_port(br, br->root_port);
172 		bpdu.message_age =  br_timer_get_residue(&root->message_age_timer)
173 			+ MESSAGE_AGE_INCR;
174 	}
175 	bpdu.max_age = br->max_age;
176 	bpdu.hello_time = br->hello_time;
177 	bpdu.forward_delay = br->forward_delay;
178 
179 	if (bpdu.message_age < br->max_age) {
180 		br_send_config_bpdu(p, &bpdu);
181 
182 		p->topology_change_ack = 0;
183 		p->config_pending = 0;
184 		br_timer_set(&p->hold_timer, jiffies);
185 	}
186 }
187 
188 /* called under bridge lock */
br_record_config_information(struct net_bridge_port * p,struct br_config_bpdu * bpdu)189 static void br_record_config_information(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
190 {
191 	p->designated_root = bpdu->root;
192 	p->designated_cost = bpdu->root_path_cost;
193 	p->designated_bridge = bpdu->bridge_id;
194 	p->designated_port = bpdu->port_id;
195 
196 	br_timer_set(&p->message_age_timer, jiffies +
197 		     (p->br->max_age - bpdu->message_age));
198 }
199 
200 /* called under bridge lock */
br_record_config_timeout_values(struct net_bridge * br,struct br_config_bpdu * bpdu)201 static void br_record_config_timeout_values(struct net_bridge *br, struct br_config_bpdu *bpdu)
202 {
203 	br->max_age = bpdu->max_age;
204 	br->hello_time = bpdu->hello_time;
205 	br->forward_delay = bpdu->forward_delay;
206 	br->topology_change = bpdu->topology_change;
207 }
208 
209 /* called under bridge lock */
br_transmit_tcn(struct net_bridge * br)210 void br_transmit_tcn(struct net_bridge *br)
211 {
212 	br_send_tcn_bpdu(br_get_port(br, br->root_port));
213 }
214 
215 /* called under bridge lock */
br_should_become_designated_port(struct net_bridge_port * p)216 static int br_should_become_designated_port(struct net_bridge_port *p)
217 {
218 	struct net_bridge *br;
219 	int t;
220 
221 	br = p->br;
222 	if (br_is_designated_port(p))
223 		return 1;
224 
225 	if (memcmp(&p->designated_root, &br->designated_root, 8))
226 		return 1;
227 
228 	if (br->root_path_cost < p->designated_cost)
229 		return 1;
230 	else if (br->root_path_cost > p->designated_cost)
231 		return 0;
232 
233 	t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
234 	if (t < 0)
235 		return 1;
236 	else if (t > 0)
237 		return 0;
238 
239 	if (p->port_id < p->designated_port)
240 		return 1;
241 
242 	return 0;
243 }
244 
245 /* called under bridge lock */
br_designated_port_selection(struct net_bridge * br)246 static void br_designated_port_selection(struct net_bridge *br)
247 {
248 	struct net_bridge_port *p;
249 
250 	p = br->port_list;
251 	while (p != NULL) {
252 		if (p->state != BR_STATE_DISABLED &&
253 		    br_should_become_designated_port(p))
254 			br_become_designated_port(p);
255 
256 		p = p->next;
257 	}
258 }
259 
260 /* called under bridge lock */
br_supersedes_port_info(struct net_bridge_port * p,struct br_config_bpdu * bpdu)261 static int br_supersedes_port_info(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
262 {
263 	int t;
264 
265 	t = memcmp(&bpdu->root, &p->designated_root, 8);
266 	if (t < 0)
267 		return 1;
268 	else if (t > 0)
269 		return 0;
270 
271 	if (bpdu->root_path_cost < p->designated_cost)
272 		return 1;
273 	else if (bpdu->root_path_cost > p->designated_cost)
274 		return 0;
275 
276 	t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
277 	if (t < 0)
278 		return 1;
279 	else if (t > 0)
280 		return 0;
281 
282 	if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
283 		return 1;
284 
285 	if (bpdu->port_id <= p->designated_port)
286 		return 1;
287 
288 	return 0;
289 }
290 
291 /* called under bridge lock */
br_topology_change_acknowledged(struct net_bridge * br)292 static void br_topology_change_acknowledged(struct net_bridge *br)
293 {
294 	br->topology_change_detected = 0;
295 	br_timer_clear(&br->tcn_timer);
296 }
297 
298 /* called under bridge lock */
br_topology_change_detection(struct net_bridge * br)299 void br_topology_change_detection(struct net_bridge *br)
300 {
301 	printk(KERN_INFO "%s: topology change detected", br->dev.name);
302 
303 	if (br_is_root_bridge(br)) {
304 		printk(", propagating");
305 		br->topology_change = 1;
306 		br_timer_set(&br->topology_change_timer, jiffies);
307 	} else if (!br->topology_change_detected) {
308 		printk(", sending tcn bpdu");
309 		br_transmit_tcn(br);
310 		br_timer_set(&br->tcn_timer, jiffies);
311 	}
312 
313 	printk("\n");
314 	br->topology_change_detected = 1;
315 }
316 
317 /* called under bridge lock */
br_config_bpdu_generation(struct net_bridge * br)318 void br_config_bpdu_generation(struct net_bridge *br)
319 {
320 	struct net_bridge_port *p;
321 
322 	p = br->port_list;
323 	while (p != NULL) {
324 		if (p->state != BR_STATE_DISABLED &&
325 		    br_is_designated_port(p))
326 			br_transmit_config(p);
327 
328 		p = p->next;
329 	}
330 }
331 
332 /* called under bridge lock */
br_reply(struct net_bridge_port * p)333 static void br_reply(struct net_bridge_port *p)
334 {
335 	br_transmit_config(p);
336 }
337 
338 /* called under bridge lock */
br_configuration_update(struct net_bridge * br)339 void br_configuration_update(struct net_bridge *br)
340 {
341 	br_root_selection(br);
342 	br_designated_port_selection(br);
343 }
344 
345 /* called under bridge lock */
br_become_designated_port(struct net_bridge_port * p)346 void br_become_designated_port(struct net_bridge_port *p)
347 {
348 	struct net_bridge *br;
349 
350 	br = p->br;
351 	p->designated_root = br->designated_root;
352 	p->designated_cost = br->root_path_cost;
353 	p->designated_bridge = br->bridge_id;
354 	p->designated_port = p->port_id;
355 }
356 
357 /* called under bridge lock */
br_make_blocking(struct net_bridge_port * p)358 static void br_make_blocking(struct net_bridge_port *p)
359 {
360 	if (p->state != BR_STATE_DISABLED &&
361 	    p->state != BR_STATE_BLOCKING) {
362 		if (p->state == BR_STATE_FORWARDING ||
363 		    p->state == BR_STATE_LEARNING)
364 			br_topology_change_detection(p->br);
365 
366 		printk(KERN_INFO "%s: port %i(%s) entering %s state\n",
367 		       p->br->dev.name, p->port_no, p->dev->name, "blocking");
368 
369 		p->state = BR_STATE_BLOCKING;
370 		br_timer_clear(&p->forward_delay_timer);
371 	}
372 }
373 
374 /* called under bridge lock */
br_make_forwarding(struct net_bridge_port * p)375 static void br_make_forwarding(struct net_bridge_port *p)
376 {
377 	if (p->state == BR_STATE_BLOCKING) {
378 		if (p->br->stp_enabled) {
379 			printk(KERN_INFO "%s: port %i(%s) entering %s state\n",
380 			       p->br->dev.name, p->port_no, p->dev->name,
381 			       "listening");
382 
383 			p->state = BR_STATE_LISTENING;
384 		} else {
385 			printk(KERN_INFO "%s: port %i(%s) entering %s state\n",
386 			       p->br->dev.name, p->port_no, p->dev->name,
387 			       "learning");
388 
389 			p->state = BR_STATE_LEARNING;
390 		}
391 		br_timer_set(&p->forward_delay_timer, jiffies);
392 	}
393 }
394 
395 /* called under bridge lock */
br_port_state_selection(struct net_bridge * br)396 void br_port_state_selection(struct net_bridge *br)
397 {
398 	struct net_bridge_port *p;
399 
400 	p = br->port_list;
401 	while (p != NULL) {
402 		if (p->state != BR_STATE_DISABLED) {
403 			if (p->port_no == br->root_port) {
404 				p->config_pending = 0;
405 				p->topology_change_ack = 0;
406 				br_make_forwarding(p);
407 			} else if (br_is_designated_port(p)) {
408 				br_timer_clear(&p->message_age_timer);
409 				br_make_forwarding(p);
410 			} else {
411 				p->config_pending = 0;
412 				p->topology_change_ack = 0;
413 				br_make_blocking(p);
414 			}
415 		}
416 
417 		p = p->next;
418 	}
419 }
420 
421 /* called under bridge lock */
br_topology_change_acknowledge(struct net_bridge_port * p)422 static void br_topology_change_acknowledge(struct net_bridge_port *p)
423 {
424 	p->topology_change_ack = 1;
425 	br_transmit_config(p);
426 }
427 
428 /* lock-safe */
br_received_config_bpdu(struct net_bridge_port * p,struct br_config_bpdu * bpdu)429 void br_received_config_bpdu(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
430 {
431 	struct net_bridge *br;
432 	int was_root;
433 
434 	if (p->state == BR_STATE_DISABLED)
435 		return;
436 
437 	br = p->br;
438 	read_lock(&br->lock);
439 
440 	was_root = br_is_root_bridge(br);
441 	if (br_supersedes_port_info(p, bpdu)) {
442 		br_record_config_information(p, bpdu);
443 		br_configuration_update(br);
444 		br_port_state_selection(br);
445 
446 		if (!br_is_root_bridge(br) && was_root) {
447 			br_timer_clear(&br->hello_timer);
448 			if (br->topology_change_detected) {
449 				br_timer_clear(&br->topology_change_timer);
450 				br_transmit_tcn(br);
451 				br_timer_set(&br->tcn_timer, jiffies);
452 			}
453 		}
454 
455 		if (p->port_no == br->root_port) {
456 			br_record_config_timeout_values(br, bpdu);
457 			br_config_bpdu_generation(br);
458 			if (bpdu->topology_change_ack)
459 				br_topology_change_acknowledged(br);
460 		}
461 	} else if (br_is_designated_port(p)) {
462 		br_reply(p);
463 	}
464 
465 	read_unlock(&br->lock);
466 }
467 
468 /* lock-safe */
br_received_tcn_bpdu(struct net_bridge_port * p)469 void br_received_tcn_bpdu(struct net_bridge_port *p)
470 {
471 	read_lock(&p->br->lock);
472 	if (p->state != BR_STATE_DISABLED &&
473 	    br_is_designated_port(p)) {
474 		printk(KERN_INFO "%s: received tcn bpdu on port %i(%s)\n",
475 		       p->br->dev.name, p->port_no, p->dev->name);
476 
477 		br_topology_change_detection(p->br);
478 		br_topology_change_acknowledge(p);
479 	}
480 	read_unlock(&p->br->lock);
481 }
482