1 /*
2  *	Spanning tree protocol; timer-related code
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Lennert Buytenhek		<buytenh@gnu.org>
7  *
8  *	$Id: br_stp_timer.c,v 1.3 2000/05/05 02:17:17 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 
dump_bridge_id(bridge_id * id)23 static void dump_bridge_id(bridge_id *id)
24 {
25 	printk("%.2x%.2x.%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", id->prio[0],
26 	       id->prio[1], id->addr[0], id->addr[1], id->addr[2], id->addr[3],
27 	       id->addr[4], id->addr[5]);
28 }
29 
30 /* called under bridge lock */
br_is_designated_for_some_port(struct net_bridge * br)31 static int br_is_designated_for_some_port(struct net_bridge *br)
32 {
33 	struct net_bridge_port *p;
34 
35 	p = br->port_list;
36 	while (p != NULL) {
37 		if (p->state != BR_STATE_DISABLED &&
38 		    !memcmp(&p->designated_bridge, &br->bridge_id, 8))
39 			return 1;
40 
41 		p = p->next;
42 	}
43 
44 	return 0;
45 }
46 
47 /* called under bridge lock */
br_hello_timer_expired(struct net_bridge * br)48 static void br_hello_timer_expired(struct net_bridge *br)
49 {
50 	br_config_bpdu_generation(br);
51 	br_timer_set(&br->hello_timer, jiffies);
52 }
53 
54 /* called under bridge lock */
br_message_age_timer_expired(struct net_bridge_port * p)55 static void br_message_age_timer_expired(struct net_bridge_port *p)
56 {
57 	struct net_bridge *br;
58 	int was_root;
59 
60 	br = p->br;
61 	printk(KERN_INFO "%s: ", br->dev.name);
62 	printk("neighbour ");
63 	dump_bridge_id(&p->designated_bridge);
64 	printk(" lost on port %i(%s)\n", p->port_no, p->dev->name);
65 
66 	/*
67 	 * According to the spec, the message age timer cannot be
68 	 * running when we are the root bridge. So..  this was_root
69 	 * check is redundant. I'm leaving it in for now, though.
70 	 */
71 	was_root = br_is_root_bridge(br);
72 
73 	br_become_designated_port(p);
74 	br_configuration_update(br);
75 	br_port_state_selection(br);
76 	if (br_is_root_bridge(br) && !was_root)
77 		br_become_root_bridge(br);
78 }
79 
80 /* called under bridge lock */
br_forward_delay_timer_expired(struct net_bridge_port * p)81 static void br_forward_delay_timer_expired(struct net_bridge_port *p)
82 {
83 	if (p->state == BR_STATE_LISTENING) {
84 		printk(KERN_INFO "%s: port %i(%s) entering %s state\n",
85 		       p->br->dev.name, p->port_no, p->dev->name, "learning");
86 
87 		p->state = BR_STATE_LEARNING;
88 		br_timer_set(&p->forward_delay_timer, jiffies);
89 	} else if (p->state == BR_STATE_LEARNING) {
90 		printk(KERN_INFO "%s: port %i(%s) entering %s state\n",
91 		       p->br->dev.name, p->port_no, p->dev->name, "forwarding");
92 
93 		p->state = BR_STATE_FORWARDING;
94 		if (br_is_designated_for_some_port(p->br))
95 			br_topology_change_detection(p->br);
96 	}
97 }
98 
99 /* called under bridge lock */
br_tcn_timer_expired(struct net_bridge * br)100 static void br_tcn_timer_expired(struct net_bridge *br)
101 {
102 	printk(KERN_INFO "%s: retransmitting tcn bpdu\n", br->dev.name);
103 	br_transmit_tcn(br);
104 	br_timer_set(&br->tcn_timer, jiffies);
105 }
106 
107 /* called under bridge lock */
br_topology_change_timer_expired(struct net_bridge * br)108 static void br_topology_change_timer_expired(struct net_bridge *br)
109 {
110 	br->topology_change_detected = 0;
111 	br->topology_change = 0;
112 }
113 
114 /* called under bridge lock */
br_hold_timer_expired(struct net_bridge_port * p)115 static void br_hold_timer_expired(struct net_bridge_port *p)
116 {
117 	if (p->config_pending)
118 		br_transmit_config(p);
119 }
120 
121 /* called under bridge lock */
br_check_port_timers(struct net_bridge_port * p)122 static void br_check_port_timers(struct net_bridge_port *p)
123 {
124 	if (br_timer_has_expired(&p->message_age_timer, p->br->max_age)) {
125 		br_timer_clear(&p->message_age_timer);
126 		br_message_age_timer_expired(p);
127 	}
128 
129 	if (br_timer_has_expired(&p->forward_delay_timer, p->br->forward_delay)) {
130 		br_timer_clear(&p->forward_delay_timer);
131 		br_forward_delay_timer_expired(p);
132 	}
133 
134 	if (br_timer_has_expired(&p->hold_timer, BR_HOLD_TIME)) {
135 		br_timer_clear(&p->hold_timer);
136 		br_hold_timer_expired(p);
137 	}
138 }
139 
140 /* called under bridge lock */
br_check_timers(struct net_bridge * br)141 static void br_check_timers(struct net_bridge *br)
142 {
143 	struct net_bridge_port *p;
144 
145 	if (br_timer_has_expired(&br->gc_timer, br->gc_interval)) {
146 		br_timer_set(&br->gc_timer, jiffies);
147 		br_fdb_cleanup(br);
148 	}
149 
150 	if (br_timer_has_expired(&br->hello_timer, br->hello_time)) {
151 		br_timer_clear(&br->hello_timer);
152 		br_hello_timer_expired(br);
153 	}
154 
155 	if (br_timer_has_expired(&br->tcn_timer, br->bridge_hello_time)) {
156 		br_timer_clear(&br->tcn_timer);
157 		br_tcn_timer_expired(br);
158 	}
159 
160 	if (br_timer_has_expired(&br->topology_change_timer, br->bridge_forward_delay + br->bridge_max_age)) {
161 		br_timer_clear(&br->topology_change_timer);
162 		br_topology_change_timer_expired(br);
163 	}
164 
165 	p = br->port_list;
166 	while (p != NULL) {
167 		if (p->state != BR_STATE_DISABLED)
168 			br_check_port_timers(p);
169 
170 		p = p->next;
171 	}
172 }
173 
br_tick(unsigned long __data)174 void br_tick(unsigned long __data)
175 {
176 	struct net_bridge *br = (struct net_bridge *)__data;
177 
178 	read_lock(&br->lock);
179 	br_check_timers(br);
180 	read_unlock(&br->lock);
181 
182 	br->tick.expires = jiffies + 1;
183 	add_timer(&br->tick);
184 }
185