bridgestp.c revision 1.1.2.2 1 1.1.2.2 nathanw /* $NetBSD: bridgestp.c,v 1.1.2.2 2001/08/24 00:12:05 nathanw Exp $ */
2 1.1.2.2 nathanw
3 1.1.2.2 nathanw /*
4 1.1.2.2 nathanw * Copyright (c) 2000 Jason L. Wright (jason (at) thought.net)
5 1.1.2.2 nathanw * All rights reserved.
6 1.1.2.2 nathanw *
7 1.1.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.1.2.2 nathanw * are met:
10 1.1.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.1.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
16 1.1.2.2 nathanw * must display the following acknowledgement:
17 1.1.2.2 nathanw * This product includes software developed by Jason L. Wright
18 1.1.2.2 nathanw * 4. The name of the author may not be used to endorse or promote products
19 1.1.2.2 nathanw * derived from this software without specific prior written permission.
20 1.1.2.2 nathanw *
21 1.1.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1.2.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1.2.2 nathanw * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 1.1.2.2 nathanw * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 1.1.2.2 nathanw * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.1.2.2 nathanw * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 1.1.2.2 nathanw * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 1.1.2.2 nathanw * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 1.1.2.2 nathanw * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
32 1.1.2.2 nathanw *
33 1.1.2.2 nathanw * OpenBSD: bridgestp.c,v 1.5 2001/03/22 03:48:29 jason Exp
34 1.1.2.2 nathanw */
35 1.1.2.2 nathanw
36 1.1.2.2 nathanw /*
37 1.1.2.2 nathanw * Implementation of the spanning tree protocol as defined in
38 1.1.2.2 nathanw * ISO/IEC Final DIS 15802-3 (IEEE P802.1D/D17), May 25, 1998.
39 1.1.2.2 nathanw * (In English: IEEE 802.1D, Draft 17, 1998)
40 1.1.2.2 nathanw */
41 1.1.2.2 nathanw
42 1.1.2.2 nathanw #include <sys/param.h>
43 1.1.2.2 nathanw #include <sys/systm.h>
44 1.1.2.2 nathanw #include <sys/mbuf.h>
45 1.1.2.2 nathanw #include <sys/socket.h>
46 1.1.2.2 nathanw #include <sys/ioctl.h>
47 1.1.2.2 nathanw #include <sys/device.h>
48 1.1.2.2 nathanw #include <sys/kernel.h>
49 1.1.2.2 nathanw #include <sys/callout.h>
50 1.1.2.2 nathanw
51 1.1.2.2 nathanw #include <net/if.h>
52 1.1.2.2 nathanw #include <net/if_dl.h>
53 1.1.2.2 nathanw #include <net/if_types.h>
54 1.1.2.2 nathanw #include <net/if_llc.h>
55 1.1.2.2 nathanw
56 1.1.2.2 nathanw #include <net/if_ether.h>
57 1.1.2.2 nathanw #include <net/if_bridgevar.h>
58 1.1.2.2 nathanw
59 1.1.2.2 nathanw /* BPDU message types */
60 1.1.2.2 nathanw #define BSTP_MSGTYPE_CFG 0x00 /* Configuration */
61 1.1.2.2 nathanw #define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */
62 1.1.2.2 nathanw
63 1.1.2.2 nathanw /* BPDU flags */
64 1.1.2.2 nathanw #define BSTP_FLAG_TC 0x01 /* Topology change */
65 1.1.2.2 nathanw #define BSTP_FLAG_TCA 0x80 /* Topology change ack */
66 1.1.2.2 nathanw
67 1.1.2.2 nathanw #define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */
68 1.1.2.2 nathanw #define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */
69 1.1.2.2 nathanw
70 1.1.2.2 nathanw /*
71 1.1.2.2 nathanw * Because BPDU's do not make nicely aligned structures, two different
72 1.1.2.2 nathanw * declarations are used: bstp_?bpdu (wire representation, packed) and
73 1.1.2.2 nathanw * bstp_*_unit (internal, nicely aligned version).
74 1.1.2.2 nathanw */
75 1.1.2.2 nathanw
76 1.1.2.2 nathanw /* configuration bridge protocol data unit */
77 1.1.2.2 nathanw struct bstp_cbpdu {
78 1.1.2.2 nathanw uint8_t cbu_dsap; /* LLC: destination sap */
79 1.1.2.2 nathanw uint8_t cbu_ssap; /* LLC: source sap */
80 1.1.2.2 nathanw uint8_t cbu_ctl; /* LLC: control */
81 1.1.2.2 nathanw uint16_t cbu_protoid; /* protocol id */
82 1.1.2.2 nathanw uint8_t cbu_protover; /* protocol version */
83 1.1.2.2 nathanw uint8_t cbu_bpdutype; /* message type */
84 1.1.2.2 nathanw uint8_t cbu_flags; /* flags (below) */
85 1.1.2.2 nathanw
86 1.1.2.2 nathanw /* root id */
87 1.1.2.2 nathanw uint16_t cbu_rootpri; /* root priority */
88 1.1.2.2 nathanw uint8_t cbu_rootaddr[6]; /* root address */
89 1.1.2.2 nathanw
90 1.1.2.2 nathanw uint32_t cbu_rootpathcost; /* root path cost */
91 1.1.2.2 nathanw
92 1.1.2.2 nathanw /* bridge id */
93 1.1.2.2 nathanw uint16_t cbu_bridgepri; /* bridge priority */
94 1.1.2.2 nathanw uint8_t cbu_bridgeaddr[6]; /* bridge address */
95 1.1.2.2 nathanw
96 1.1.2.2 nathanw uint16_t cbu_portid; /* port id */
97 1.1.2.2 nathanw uint16_t cbu_messageage; /* current message age */
98 1.1.2.2 nathanw uint16_t cbu_maxage; /* maximum age */
99 1.1.2.2 nathanw uint16_t cbu_hellotime; /* hello time */
100 1.1.2.2 nathanw uint16_t cbu_forwarddelay; /* forwarding delay */
101 1.1.2.2 nathanw } __attribute__((__packed__));
102 1.1.2.2 nathanw
103 1.1.2.2 nathanw /* topology change notification bridge protocol data unit */
104 1.1.2.2 nathanw struct bstp_tbpdu {
105 1.1.2.2 nathanw uint8_t tbu_dsap; /* LLC: destination sap */
106 1.1.2.2 nathanw uint8_t tbu_ssap; /* LLC: source sap */
107 1.1.2.2 nathanw uint8_t tbu_ctl; /* LLC: control */
108 1.1.2.2 nathanw uint16_t tbu_protoid; /* protocol id */
109 1.1.2.2 nathanw uint8_t tbu_protover; /* protocol version */
110 1.1.2.2 nathanw uint8_t tbu_bpdutype; /* message type */
111 1.1.2.2 nathanw } __attribute__((__packed__));
112 1.1.2.2 nathanw
113 1.1.2.2 nathanw const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
114 1.1.2.2 nathanw
115 1.1.2.2 nathanw void bstp_initialize_port(struct bridge_softc *, struct bridge_iflist *);
116 1.1.2.2 nathanw void bstp_ifupdstatus(struct bridge_softc *, struct bridge_iflist *);
117 1.1.2.2 nathanw void bstp_enable_port(struct bridge_softc *, struct bridge_iflist *);
118 1.1.2.2 nathanw void bstp_disable_port(struct bridge_softc *, struct bridge_iflist *);
119 1.1.2.2 nathanw void bstp_enable_change_detection(struct bridge_iflist *);
120 1.1.2.2 nathanw void bstp_disable_change_detection(struct bridge_iflist *);
121 1.1.2.2 nathanw int bstp_root_bridge(struct bridge_softc *sc);
122 1.1.2.2 nathanw int bstp_supersedes_port_info(struct bridge_softc *,
123 1.1.2.2 nathanw struct bridge_iflist *, struct bstp_config_unit *);
124 1.1.2.2 nathanw int bstp_designated_port(struct bridge_softc *, struct bridge_iflist *);
125 1.1.2.2 nathanw int bstp_designated_for_some_port(struct bridge_softc *);
126 1.1.2.2 nathanw void bstp_transmit_config(struct bridge_softc *, struct bridge_iflist *);
127 1.1.2.2 nathanw void bstp_transmit_tcn(struct bridge_softc *);
128 1.1.2.2 nathanw void bstp_received_config_bpdu(struct bridge_softc *,
129 1.1.2.2 nathanw struct bridge_iflist *, struct bstp_config_unit *);
130 1.1.2.2 nathanw void bstp_received_tcn_bpdu(struct bridge_softc *, struct bridge_iflist *,
131 1.1.2.2 nathanw struct bstp_tcn_unit *);
132 1.1.2.2 nathanw void bstp_record_config_information(struct bridge_softc *,
133 1.1.2.2 nathanw struct bridge_iflist *, struct bstp_config_unit *);
134 1.1.2.2 nathanw void bstp_record_config_timeout_values(struct bridge_softc *,
135 1.1.2.2 nathanw struct bstp_config_unit *);
136 1.1.2.2 nathanw void bstp_config_bpdu_generation(struct bridge_softc *);
137 1.1.2.2 nathanw void bstp_send_config_bpdu(struct bridge_softc *, struct bridge_iflist *,
138 1.1.2.2 nathanw struct bstp_config_unit *);
139 1.1.2.2 nathanw void bstp_configuration_update(struct bridge_softc *);
140 1.1.2.2 nathanw void bstp_root_selection(struct bridge_softc *);
141 1.1.2.2 nathanw void bstp_designated_port_selection(struct bridge_softc *);
142 1.1.2.2 nathanw void bstp_become_designated_port(struct bridge_softc *,
143 1.1.2.2 nathanw struct bridge_iflist *);
144 1.1.2.2 nathanw void bstp_port_state_selection(struct bridge_softc *);
145 1.1.2.2 nathanw void bstp_make_forwarding(struct bridge_softc *, struct bridge_iflist *);
146 1.1.2.2 nathanw void bstp_make_blocking(struct bridge_softc *, struct bridge_iflist *);
147 1.1.2.2 nathanw void bstp_set_port_state(struct bridge_iflist *, uint8_t);
148 1.1.2.2 nathanw void bstp_set_bridge_priority(struct bridge_softc *, uint64_t);
149 1.1.2.2 nathanw void bstp_set_port_priority(struct bridge_softc *, struct bridge_iflist *,
150 1.1.2.2 nathanw uint16_t);
151 1.1.2.2 nathanw void bstp_set_path_cost(struct bridge_softc *, struct bridge_iflist *,
152 1.1.2.2 nathanw uint32_t);
153 1.1.2.2 nathanw void bstp_topology_change_detection(struct bridge_softc *);
154 1.1.2.2 nathanw void bstp_topology_change_acknowledged(struct bridge_softc *);
155 1.1.2.2 nathanw void bstp_acknowledge_topology_change(struct bridge_softc *,
156 1.1.2.2 nathanw struct bridge_iflist *);
157 1.1.2.2 nathanw
158 1.1.2.2 nathanw void bstp_tick(void *);
159 1.1.2.2 nathanw void bstp_timer_start(struct bridge_timer *, uint16_t);
160 1.1.2.2 nathanw void bstp_timer_stop(struct bridge_timer *);
161 1.1.2.2 nathanw int bstp_timer_expired(struct bridge_timer *, uint16_t);
162 1.1.2.2 nathanw
163 1.1.2.2 nathanw void bstp_hold_timer_expiry(struct bridge_softc *, struct bridge_iflist *);
164 1.1.2.2 nathanw void bstp_message_age_timer_expiry(struct bridge_softc *,
165 1.1.2.2 nathanw struct bridge_iflist *);
166 1.1.2.2 nathanw void bstp_forward_delay_timer_expiry(struct bridge_softc *,
167 1.1.2.2 nathanw struct bridge_iflist *);
168 1.1.2.2 nathanw void bstp_topology_change_timer_expiry(struct bridge_softc *);
169 1.1.2.2 nathanw void bstp_tcn_timer_expiry(struct bridge_softc *);
170 1.1.2.2 nathanw void bstp_hello_timer_expiry(struct bridge_softc *);
171 1.1.2.2 nathanw
172 1.1.2.2 nathanw void
173 1.1.2.2 nathanw bstp_transmit_config(struct bridge_softc *sc, struct bridge_iflist *bif)
174 1.1.2.2 nathanw {
175 1.1.2.2 nathanw if (bif->bif_hold_timer.active) {
176 1.1.2.2 nathanw bif->bif_config_pending = 1;
177 1.1.2.2 nathanw return;
178 1.1.2.2 nathanw }
179 1.1.2.2 nathanw
180 1.1.2.2 nathanw bif->bif_config_bpdu.cu_message_type = BSTP_MSGTYPE_CFG;
181 1.1.2.2 nathanw bif->bif_config_bpdu.cu_rootid = sc->sc_designated_root;
182 1.1.2.2 nathanw bif->bif_config_bpdu.cu_root_path_cost = sc->sc_root_path_cost;
183 1.1.2.2 nathanw bif->bif_config_bpdu.cu_bridge_id = sc->sc_bridge_id;
184 1.1.2.2 nathanw bif->bif_config_bpdu.cu_port_id = bif->bif_port_id;
185 1.1.2.2 nathanw
186 1.1.2.2 nathanw if (bstp_root_bridge(sc))
187 1.1.2.2 nathanw bif->bif_config_bpdu.cu_message_age = 0;
188 1.1.2.2 nathanw else
189 1.1.2.2 nathanw bif->bif_config_bpdu.cu_message_age =
190 1.1.2.2 nathanw sc->sc_root_port->bif_message_age_timer.value +
191 1.1.2.2 nathanw BSTP_MESSAGE_AGE_INCR;
192 1.1.2.2 nathanw
193 1.1.2.2 nathanw bif->bif_config_bpdu.cu_max_age = sc->sc_max_age;
194 1.1.2.2 nathanw bif->bif_config_bpdu.cu_hello_time = sc->sc_hello_time;
195 1.1.2.2 nathanw bif->bif_config_bpdu.cu_forward_delay = sc->sc_forward_delay;
196 1.1.2.2 nathanw bif->bif_config_bpdu.cu_topology_change_acknowledgment
197 1.1.2.2 nathanw = bif->bif_topology_change_acknowledge;
198 1.1.2.2 nathanw bif->bif_config_bpdu.cu_topology_change = sc->sc_topology_change;
199 1.1.2.2 nathanw
200 1.1.2.2 nathanw if (bif->bif_config_bpdu.cu_message_age < sc->sc_max_age) {
201 1.1.2.2 nathanw bif->bif_topology_change_acknowledge = 0;
202 1.1.2.2 nathanw bif->bif_config_pending = 0;
203 1.1.2.2 nathanw bstp_send_config_bpdu(sc, bif, &bif->bif_config_bpdu);
204 1.1.2.2 nathanw bstp_timer_start(&bif->bif_hold_timer, 0);
205 1.1.2.2 nathanw }
206 1.1.2.2 nathanw }
207 1.1.2.2 nathanw
208 1.1.2.2 nathanw void
209 1.1.2.2 nathanw bstp_send_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
210 1.1.2.2 nathanw struct bstp_config_unit *cu)
211 1.1.2.2 nathanw {
212 1.1.2.2 nathanw struct ifnet *ifp;
213 1.1.2.2 nathanw struct mbuf *m;
214 1.1.2.2 nathanw struct ether_header *eh;
215 1.1.2.2 nathanw struct bstp_cbpdu bpdu;
216 1.1.2.2 nathanw int s;
217 1.1.2.2 nathanw
218 1.1.2.2 nathanw ifp = bif->bif_ifp;
219 1.1.2.2 nathanw
220 1.1.2.2 nathanw if ((ifp->if_flags & IFF_RUNNING) == 0)
221 1.1.2.2 nathanw return;
222 1.1.2.2 nathanw
223 1.1.2.2 nathanw MGETHDR(m, M_DONTWAIT, MT_DATA);
224 1.1.2.2 nathanw if (m == NULL)
225 1.1.2.2 nathanw return;
226 1.1.2.2 nathanw
227 1.1.2.2 nathanw eh = mtod(m, struct ether_header *);
228 1.1.2.2 nathanw
229 1.1.2.2 nathanw m->m_pkthdr.rcvif = ifp;
230 1.1.2.2 nathanw m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
231 1.1.2.2 nathanw m->m_len = m->m_pkthdr.len;
232 1.1.2.2 nathanw
233 1.1.2.2 nathanw bpdu.cbu_ssap = bpdu.cbu_dsap = LLC_8021D_LSAP;
234 1.1.2.2 nathanw bpdu.cbu_ctl = LLC_UI;
235 1.1.2.2 nathanw bpdu.cbu_protoid = htons(0);
236 1.1.2.2 nathanw bpdu.cbu_protover = 0;
237 1.1.2.2 nathanw bpdu.cbu_bpdutype = cu->cu_message_type;
238 1.1.2.2 nathanw bpdu.cbu_flags = (cu->cu_topology_change ? BSTP_FLAG_TC : 0) |
239 1.1.2.2 nathanw (cu->cu_topology_change_acknowledgment ? BSTP_FLAG_TCA : 0);
240 1.1.2.2 nathanw
241 1.1.2.2 nathanw bpdu.cbu_rootpri = htons(cu->cu_rootid >> 48);
242 1.1.2.2 nathanw bpdu.cbu_rootaddr[0] = cu->cu_rootid >> 40;
243 1.1.2.2 nathanw bpdu.cbu_rootaddr[1] = cu->cu_rootid >> 32;
244 1.1.2.2 nathanw bpdu.cbu_rootaddr[2] = cu->cu_rootid >> 24;
245 1.1.2.2 nathanw bpdu.cbu_rootaddr[3] = cu->cu_rootid >> 16;
246 1.1.2.2 nathanw bpdu.cbu_rootaddr[4] = cu->cu_rootid >> 8;
247 1.1.2.2 nathanw bpdu.cbu_rootaddr[5] = cu->cu_rootid >> 0;
248 1.1.2.2 nathanw
249 1.1.2.2 nathanw bpdu.cbu_rootpathcost = htonl(cu->cu_root_path_cost);
250 1.1.2.2 nathanw
251 1.1.2.2 nathanw bpdu.cbu_bridgepri = htons(cu->cu_rootid >> 48);
252 1.1.2.2 nathanw bpdu.cbu_bridgeaddr[0] = cu->cu_rootid >> 40;
253 1.1.2.2 nathanw bpdu.cbu_bridgeaddr[1] = cu->cu_rootid >> 32;
254 1.1.2.2 nathanw bpdu.cbu_bridgeaddr[2] = cu->cu_rootid >> 24;
255 1.1.2.2 nathanw bpdu.cbu_bridgeaddr[3] = cu->cu_rootid >> 16;
256 1.1.2.2 nathanw bpdu.cbu_bridgeaddr[4] = cu->cu_rootid >> 8;
257 1.1.2.2 nathanw bpdu.cbu_bridgeaddr[5] = cu->cu_rootid >> 0;
258 1.1.2.2 nathanw
259 1.1.2.2 nathanw bpdu.cbu_portid = htons(cu->cu_port_id);
260 1.1.2.2 nathanw bpdu.cbu_messageage = htons(cu->cu_message_age);
261 1.1.2.2 nathanw bpdu.cbu_maxage = htons(cu->cu_max_age);
262 1.1.2.2 nathanw bpdu.cbu_hellotime = htons(cu->cu_hello_time);
263 1.1.2.2 nathanw bpdu.cbu_forwarddelay = htons(cu->cu_forward_delay);
264 1.1.2.2 nathanw
265 1.1.2.2 nathanw memcpy(eh->ether_shost, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
266 1.1.2.2 nathanw memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
267 1.1.2.2 nathanw eh->ether_type = htons(sizeof(bpdu));
268 1.1.2.2 nathanw
269 1.1.2.2 nathanw memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
270 1.1.2.2 nathanw
271 1.1.2.2 nathanw s = splnet();
272 1.1.2.2 nathanw bridge_enqueue(sc, ifp, m);
273 1.1.2.2 nathanw splx(s);
274 1.1.2.2 nathanw }
275 1.1.2.2 nathanw
276 1.1.2.2 nathanw int
277 1.1.2.2 nathanw bstp_root_bridge(struct bridge_softc *sc)
278 1.1.2.2 nathanw {
279 1.1.2.2 nathanw return (sc->sc_designated_root == sc->sc_bridge_id);
280 1.1.2.2 nathanw }
281 1.1.2.2 nathanw
282 1.1.2.2 nathanw int
283 1.1.2.2 nathanw bstp_supersedes_port_info(struct bridge_softc *sc, struct bridge_iflist *bif,
284 1.1.2.2 nathanw struct bstp_config_unit *cu)
285 1.1.2.2 nathanw {
286 1.1.2.2 nathanw if (cu->cu_rootid < bif->bif_designated_root)
287 1.1.2.2 nathanw return (1);
288 1.1.2.2 nathanw if (cu->cu_rootid > bif->bif_designated_root)
289 1.1.2.2 nathanw return (0);
290 1.1.2.2 nathanw
291 1.1.2.2 nathanw if (cu->cu_root_path_cost < bif->bif_designated_cost)
292 1.1.2.2 nathanw return (1);
293 1.1.2.2 nathanw if (cu->cu_root_path_cost > bif->bif_designated_cost)
294 1.1.2.2 nathanw return (0);
295 1.1.2.2 nathanw
296 1.1.2.2 nathanw if (cu->cu_bridge_id < bif->bif_designated_bridge)
297 1.1.2.2 nathanw return (1);
298 1.1.2.2 nathanw if (cu->cu_bridge_id > bif->bif_designated_bridge)
299 1.1.2.2 nathanw return (0);
300 1.1.2.2 nathanw
301 1.1.2.2 nathanw if (sc->sc_bridge_id != cu->cu_bridge_id)
302 1.1.2.2 nathanw return (1);
303 1.1.2.2 nathanw if (cu->cu_port_id <= bif->bif_designated_port)
304 1.1.2.2 nathanw return (1);
305 1.1.2.2 nathanw return (0);
306 1.1.2.2 nathanw }
307 1.1.2.2 nathanw
308 1.1.2.2 nathanw void
309 1.1.2.2 nathanw bstp_record_config_information(struct bridge_softc *sc,
310 1.1.2.2 nathanw struct bridge_iflist *bif, struct bstp_config_unit *cu)
311 1.1.2.2 nathanw {
312 1.1.2.2 nathanw bif->bif_designated_root = cu->cu_rootid;
313 1.1.2.2 nathanw bif->bif_designated_cost = cu->cu_root_path_cost;
314 1.1.2.2 nathanw bif->bif_designated_bridge = cu->cu_bridge_id;
315 1.1.2.2 nathanw bif->bif_designated_port = cu->cu_port_id;
316 1.1.2.2 nathanw bstp_timer_start(&bif->bif_message_age_timer, cu->cu_message_age);
317 1.1.2.2 nathanw }
318 1.1.2.2 nathanw
319 1.1.2.2 nathanw void
320 1.1.2.2 nathanw bstp_record_config_timeout_values(struct bridge_softc *sc,
321 1.1.2.2 nathanw struct bstp_config_unit *config)
322 1.1.2.2 nathanw {
323 1.1.2.2 nathanw sc->sc_max_age = config->cu_max_age;
324 1.1.2.2 nathanw sc->sc_hello_time = config->cu_hello_time;
325 1.1.2.2 nathanw sc->sc_forward_delay = config->cu_forward_delay;
326 1.1.2.2 nathanw sc->sc_topology_change = config->cu_topology_change;
327 1.1.2.2 nathanw }
328 1.1.2.2 nathanw
329 1.1.2.2 nathanw void
330 1.1.2.2 nathanw bstp_config_bpdu_generation(struct bridge_softc *sc)
331 1.1.2.2 nathanw {
332 1.1.2.2 nathanw struct bridge_iflist *bif;
333 1.1.2.2 nathanw
334 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
335 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
336 1.1.2.2 nathanw continue;
337 1.1.2.2 nathanw if (bstp_designated_port(sc, bif) &&
338 1.1.2.2 nathanw (bif->bif_state != BSTP_IFSTATE_DISABLED))
339 1.1.2.2 nathanw bstp_transmit_config(sc, bif);
340 1.1.2.2 nathanw }
341 1.1.2.2 nathanw }
342 1.1.2.2 nathanw
343 1.1.2.2 nathanw int
344 1.1.2.2 nathanw bstp_designated_port(struct bridge_softc *sc, struct bridge_iflist *bif)
345 1.1.2.2 nathanw {
346 1.1.2.2 nathanw return ((bif->bif_designated_bridge == sc->sc_bridge_id)
347 1.1.2.2 nathanw && (bif->bif_designated_port == bif->bif_port_id));
348 1.1.2.2 nathanw }
349 1.1.2.2 nathanw
350 1.1.2.2 nathanw void
351 1.1.2.2 nathanw bstp_transmit_tcn(struct bridge_softc *sc)
352 1.1.2.2 nathanw {
353 1.1.2.2 nathanw struct bstp_tbpdu bpdu;
354 1.1.2.2 nathanw struct bridge_iflist *bif = sc->sc_root_port;
355 1.1.2.2 nathanw struct ifnet *ifp = bif->bif_ifp;
356 1.1.2.2 nathanw struct ether_header *eh;
357 1.1.2.2 nathanw struct mbuf *m;
358 1.1.2.2 nathanw int s;
359 1.1.2.2 nathanw
360 1.1.2.2 nathanw if ((ifp->if_flags & IFF_RUNNING) == 0)
361 1.1.2.2 nathanw return;
362 1.1.2.2 nathanw
363 1.1.2.2 nathanw MGETHDR(m, M_DONTWAIT, MT_DATA);
364 1.1.2.2 nathanw if (m == NULL)
365 1.1.2.2 nathanw return;
366 1.1.2.2 nathanw
367 1.1.2.2 nathanw m->m_pkthdr.rcvif = ifp;
368 1.1.2.2 nathanw m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
369 1.1.2.2 nathanw m->m_len = m->m_pkthdr.len;
370 1.1.2.2 nathanw
371 1.1.2.2 nathanw eh = mtod(m, struct ether_header *);
372 1.1.2.2 nathanw
373 1.1.2.2 nathanw memcpy(eh->ether_shost, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
374 1.1.2.2 nathanw memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
375 1.1.2.2 nathanw eh->ether_type = htons(sizeof(bpdu));
376 1.1.2.2 nathanw
377 1.1.2.2 nathanw bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP;
378 1.1.2.2 nathanw bpdu.tbu_ctl = LLC_UI;
379 1.1.2.2 nathanw bpdu.tbu_protoid = 0;
380 1.1.2.2 nathanw bpdu.tbu_protover = 0;
381 1.1.2.2 nathanw bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN;
382 1.1.2.2 nathanw
383 1.1.2.2 nathanw memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
384 1.1.2.2 nathanw
385 1.1.2.2 nathanw s = splnet();
386 1.1.2.2 nathanw bridge_enqueue(sc, ifp, m);
387 1.1.2.2 nathanw splx(s);
388 1.1.2.2 nathanw }
389 1.1.2.2 nathanw
390 1.1.2.2 nathanw void
391 1.1.2.2 nathanw bstp_configuration_update(struct bridge_softc *sc)
392 1.1.2.2 nathanw {
393 1.1.2.2 nathanw bstp_root_selection(sc);
394 1.1.2.2 nathanw bstp_designated_port_selection(sc);
395 1.1.2.2 nathanw }
396 1.1.2.2 nathanw
397 1.1.2.2 nathanw void
398 1.1.2.2 nathanw bstp_root_selection(struct bridge_softc *sc)
399 1.1.2.2 nathanw {
400 1.1.2.2 nathanw struct bridge_iflist *root_port = NULL, *bif;
401 1.1.2.2 nathanw
402 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
403 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
404 1.1.2.2 nathanw continue;
405 1.1.2.2 nathanw if (bstp_designated_port(sc, bif))
406 1.1.2.2 nathanw continue;
407 1.1.2.2 nathanw if (bif->bif_state == BSTP_IFSTATE_DISABLED)
408 1.1.2.2 nathanw continue;
409 1.1.2.2 nathanw if (bif->bif_designated_root >= sc->sc_bridge_id)
410 1.1.2.2 nathanw continue;
411 1.1.2.2 nathanw if (root_port == NULL)
412 1.1.2.2 nathanw goto set_port;
413 1.1.2.2 nathanw
414 1.1.2.2 nathanw if (bif->bif_designated_root < root_port->bif_designated_root)
415 1.1.2.2 nathanw goto set_port;
416 1.1.2.2 nathanw if (bif->bif_designated_root > root_port->bif_designated_root)
417 1.1.2.2 nathanw continue;
418 1.1.2.2 nathanw
419 1.1.2.2 nathanw if ((bif->bif_designated_cost + bif->bif_path_cost) <
420 1.1.2.2 nathanw (root_port->bif_designated_cost + root_port->bif_path_cost))
421 1.1.2.2 nathanw goto set_port;
422 1.1.2.2 nathanw if ((bif->bif_designated_cost + bif->bif_path_cost) >
423 1.1.2.2 nathanw (root_port->bif_designated_cost + root_port->bif_path_cost))
424 1.1.2.2 nathanw continue;
425 1.1.2.2 nathanw
426 1.1.2.2 nathanw if (bif->bif_designated_bridge <
427 1.1.2.2 nathanw root_port->bif_designated_bridge)
428 1.1.2.2 nathanw goto set_port;
429 1.1.2.2 nathanw if (bif->bif_designated_bridge >
430 1.1.2.2 nathanw root_port->bif_designated_bridge)
431 1.1.2.2 nathanw continue;
432 1.1.2.2 nathanw
433 1.1.2.2 nathanw if (bif->bif_designated_port < root_port->bif_designated_port)
434 1.1.2.2 nathanw goto set_port;
435 1.1.2.2 nathanw if (bif->bif_designated_port > root_port->bif_designated_port)
436 1.1.2.2 nathanw continue;
437 1.1.2.2 nathanw
438 1.1.2.2 nathanw if (bif->bif_port_id >= root_port->bif_port_id)
439 1.1.2.2 nathanw continue;
440 1.1.2.2 nathanw set_port:
441 1.1.2.2 nathanw root_port = bif;
442 1.1.2.2 nathanw }
443 1.1.2.2 nathanw
444 1.1.2.2 nathanw sc->sc_root_port = root_port;
445 1.1.2.2 nathanw if (root_port == NULL) {
446 1.1.2.2 nathanw sc->sc_designated_root = sc->sc_bridge_id;
447 1.1.2.2 nathanw sc->sc_root_path_cost = 0;
448 1.1.2.2 nathanw } else {
449 1.1.2.2 nathanw sc->sc_designated_root = root_port->bif_designated_root;
450 1.1.2.2 nathanw sc->sc_root_path_cost = root_port->bif_designated_cost +
451 1.1.2.2 nathanw root_port->bif_path_cost;
452 1.1.2.2 nathanw }
453 1.1.2.2 nathanw }
454 1.1.2.2 nathanw
455 1.1.2.2 nathanw void
456 1.1.2.2 nathanw bstp_designated_port_selection(struct bridge_softc *sc)
457 1.1.2.2 nathanw {
458 1.1.2.2 nathanw struct bridge_iflist *bif;
459 1.1.2.2 nathanw
460 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
461 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
462 1.1.2.2 nathanw continue;
463 1.1.2.2 nathanw if (bstp_designated_port(sc, bif))
464 1.1.2.2 nathanw goto designated;
465 1.1.2.2 nathanw if (bif->bif_designated_root != sc->sc_designated_root)
466 1.1.2.2 nathanw goto designated;
467 1.1.2.2 nathanw
468 1.1.2.2 nathanw if (sc->sc_root_path_cost < bif->bif_designated_cost)
469 1.1.2.2 nathanw goto designated;
470 1.1.2.2 nathanw if (sc->sc_root_path_cost > bif->bif_designated_cost)
471 1.1.2.2 nathanw continue;
472 1.1.2.2 nathanw
473 1.1.2.2 nathanw if (sc->sc_bridge_id < bif->bif_designated_bridge)
474 1.1.2.2 nathanw goto designated;
475 1.1.2.2 nathanw if (sc->sc_bridge_id > bif->bif_designated_bridge)
476 1.1.2.2 nathanw continue;
477 1.1.2.2 nathanw
478 1.1.2.2 nathanw if (bif->bif_port_id > bif->bif_designated_port)
479 1.1.2.2 nathanw continue;
480 1.1.2.2 nathanw designated:
481 1.1.2.2 nathanw bstp_become_designated_port(sc, bif);
482 1.1.2.2 nathanw }
483 1.1.2.2 nathanw }
484 1.1.2.2 nathanw
485 1.1.2.2 nathanw void
486 1.1.2.2 nathanw bstp_become_designated_port(struct bridge_softc *sc, struct bridge_iflist *bif)
487 1.1.2.2 nathanw {
488 1.1.2.2 nathanw bif->bif_designated_root = sc->sc_designated_root;
489 1.1.2.2 nathanw bif->bif_designated_cost = sc->sc_root_path_cost;
490 1.1.2.2 nathanw bif->bif_designated_bridge = sc->sc_bridge_id;
491 1.1.2.2 nathanw bif->bif_designated_port = bif->bif_port_id;
492 1.1.2.2 nathanw }
493 1.1.2.2 nathanw
494 1.1.2.2 nathanw void
495 1.1.2.2 nathanw bstp_port_state_selection(struct bridge_softc *sc)
496 1.1.2.2 nathanw {
497 1.1.2.2 nathanw struct bridge_iflist *bif;
498 1.1.2.2 nathanw
499 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
500 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
501 1.1.2.2 nathanw continue;
502 1.1.2.2 nathanw if (bif == sc->sc_root_port) {
503 1.1.2.2 nathanw bif->bif_config_pending = 0;
504 1.1.2.2 nathanw bif->bif_topology_change_acknowledge = 0;
505 1.1.2.2 nathanw bstp_make_forwarding(sc, bif);
506 1.1.2.2 nathanw } else if (bstp_designated_port(sc, bif)) {
507 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_message_age_timer);
508 1.1.2.2 nathanw bstp_make_forwarding(sc, bif);
509 1.1.2.2 nathanw } else {
510 1.1.2.2 nathanw bif->bif_config_pending = 0;
511 1.1.2.2 nathanw bif->bif_topology_change_acknowledge = 0;
512 1.1.2.2 nathanw bstp_make_blocking(sc, bif);
513 1.1.2.2 nathanw }
514 1.1.2.2 nathanw }
515 1.1.2.2 nathanw }
516 1.1.2.2 nathanw
517 1.1.2.2 nathanw void
518 1.1.2.2 nathanw bstp_make_forwarding(struct bridge_softc *sc, struct bridge_iflist *bif)
519 1.1.2.2 nathanw {
520 1.1.2.2 nathanw if (bif->bif_state == BSTP_IFSTATE_BLOCKING) {
521 1.1.2.2 nathanw bstp_set_port_state(bif, BSTP_IFSTATE_LISTENING);
522 1.1.2.2 nathanw bstp_timer_start(&bif->bif_forward_delay_timer, 0);
523 1.1.2.2 nathanw }
524 1.1.2.2 nathanw }
525 1.1.2.2 nathanw
526 1.1.2.2 nathanw void
527 1.1.2.2 nathanw bstp_make_blocking(struct bridge_softc *sc, struct bridge_iflist *bif)
528 1.1.2.2 nathanw {
529 1.1.2.2 nathanw if ((bif->bif_state != BSTP_IFSTATE_DISABLED) &&
530 1.1.2.2 nathanw (bif->bif_state != BSTP_IFSTATE_BLOCKING)) {
531 1.1.2.2 nathanw if ((bif->bif_state == BSTP_IFSTATE_FORWARDING) ||
532 1.1.2.2 nathanw (bif->bif_state == BSTP_IFSTATE_LEARNING)) {
533 1.1.2.2 nathanw if (bif->bif_change_detection_enabled) {
534 1.1.2.2 nathanw bstp_topology_change_detection(sc);
535 1.1.2.2 nathanw }
536 1.1.2.2 nathanw }
537 1.1.2.2 nathanw bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
538 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_forward_delay_timer);
539 1.1.2.2 nathanw }
540 1.1.2.2 nathanw }
541 1.1.2.2 nathanw
542 1.1.2.2 nathanw void
543 1.1.2.2 nathanw bstp_set_port_state(struct bridge_iflist *bif, uint8_t state)
544 1.1.2.2 nathanw {
545 1.1.2.2 nathanw bif->bif_state = state;
546 1.1.2.2 nathanw }
547 1.1.2.2 nathanw
548 1.1.2.2 nathanw void
549 1.1.2.2 nathanw bstp_topology_change_detection(struct bridge_softc *sc)
550 1.1.2.2 nathanw {
551 1.1.2.2 nathanw if (bstp_root_bridge(sc)) {
552 1.1.2.2 nathanw sc->sc_topology_change = 1;
553 1.1.2.2 nathanw bstp_timer_start(&sc->sc_topology_change_timer, 0);
554 1.1.2.2 nathanw } else if (!sc->sc_topology_change_detected) {
555 1.1.2.2 nathanw bstp_transmit_tcn(sc);
556 1.1.2.2 nathanw bstp_timer_start(&sc->sc_tcn_timer, 0);
557 1.1.2.2 nathanw }
558 1.1.2.2 nathanw sc->sc_topology_change_detected = 1;
559 1.1.2.2 nathanw }
560 1.1.2.2 nathanw
561 1.1.2.2 nathanw void
562 1.1.2.2 nathanw bstp_topology_change_acknowledged(struct bridge_softc *sc)
563 1.1.2.2 nathanw {
564 1.1.2.2 nathanw sc->sc_topology_change_detected = 0;
565 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_tcn_timer);
566 1.1.2.2 nathanw }
567 1.1.2.2 nathanw
568 1.1.2.2 nathanw void
569 1.1.2.2 nathanw bstp_acknowledge_topology_change(struct bridge_softc *sc,
570 1.1.2.2 nathanw struct bridge_iflist *bif)
571 1.1.2.2 nathanw {
572 1.1.2.2 nathanw bif->bif_topology_change_acknowledge = 1;
573 1.1.2.2 nathanw bstp_transmit_config(sc, bif);
574 1.1.2.2 nathanw }
575 1.1.2.2 nathanw
576 1.1.2.2 nathanw struct mbuf *
577 1.1.2.2 nathanw bstp_input(struct ifnet *ifp, struct mbuf *m)
578 1.1.2.2 nathanw {
579 1.1.2.2 nathanw struct bridge_softc *sc = ifp->if_bridge;
580 1.1.2.2 nathanw struct bridge_iflist *bif = NULL;
581 1.1.2.2 nathanw struct ether_header *eh;
582 1.1.2.2 nathanw struct bstp_tbpdu tpdu;
583 1.1.2.2 nathanw struct bstp_cbpdu cpdu;
584 1.1.2.2 nathanw struct bstp_config_unit cu;
585 1.1.2.2 nathanw struct bstp_tcn_unit tu;
586 1.1.2.2 nathanw uint16_t len;
587 1.1.2.2 nathanw
588 1.1.2.2 nathanw eh = mtod(m, struct ether_header *);
589 1.1.2.2 nathanw
590 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
591 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
592 1.1.2.2 nathanw continue;
593 1.1.2.2 nathanw if (bif->bif_ifp == ifp)
594 1.1.2.2 nathanw break;
595 1.1.2.2 nathanw }
596 1.1.2.2 nathanw if (bif == NULL)
597 1.1.2.2 nathanw goto out;
598 1.1.2.2 nathanw
599 1.1.2.2 nathanw len = ntohs(eh->ether_type);
600 1.1.2.2 nathanw if (len < sizeof(tpdu))
601 1.1.2.2 nathanw goto out;
602 1.1.2.2 nathanw
603 1.1.2.2 nathanw m_adj(m, ETHER_HDR_LEN);
604 1.1.2.2 nathanw
605 1.1.2.2 nathanw if (m->m_pkthdr.len > len)
606 1.1.2.2 nathanw m_adj(m, len - m->m_pkthdr.len);
607 1.1.2.2 nathanw if (m->m_len < sizeof(tpdu) &&
608 1.1.2.2 nathanw (m = m_pullup(m, sizeof(tpdu))) == NULL)
609 1.1.2.2 nathanw goto out;
610 1.1.2.2 nathanw
611 1.1.2.2 nathanw memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu));
612 1.1.2.2 nathanw
613 1.1.2.2 nathanw if (tpdu.tbu_dsap != LLC_8021D_LSAP ||
614 1.1.2.2 nathanw tpdu.tbu_ssap != LLC_8021D_LSAP ||
615 1.1.2.2 nathanw tpdu.tbu_ctl != LLC_UI)
616 1.1.2.2 nathanw goto out;
617 1.1.2.2 nathanw if (tpdu.tbu_protoid != 0 || tpdu.tbu_protover != 0)
618 1.1.2.2 nathanw goto out;
619 1.1.2.2 nathanw
620 1.1.2.2 nathanw switch (tpdu.tbu_bpdutype) {
621 1.1.2.2 nathanw case BSTP_MSGTYPE_TCN:
622 1.1.2.2 nathanw tu.tu_message_type = tpdu.tbu_bpdutype;
623 1.1.2.2 nathanw bstp_received_tcn_bpdu(sc, bif, &tu);
624 1.1.2.2 nathanw break;
625 1.1.2.2 nathanw case BSTP_MSGTYPE_CFG:
626 1.1.2.2 nathanw if (m->m_len < sizeof(cpdu) &&
627 1.1.2.2 nathanw (m = m_pullup(m, sizeof(cpdu))) == NULL)
628 1.1.2.2 nathanw goto out;
629 1.1.2.2 nathanw memcpy(&cpdu, mtod(m, caddr_t), sizeof(cpdu));
630 1.1.2.2 nathanw
631 1.1.2.2 nathanw cu.cu_rootid =
632 1.1.2.2 nathanw (((uint64_t)ntohs(cpdu.cbu_rootpri)) << 48) |
633 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_rootaddr[0]) << 40) |
634 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_rootaddr[1]) << 32) |
635 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_rootaddr[2]) << 24) |
636 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_rootaddr[3]) << 16) |
637 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_rootaddr[4]) << 8) |
638 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_rootaddr[5]) << 0);
639 1.1.2.2 nathanw
640 1.1.2.2 nathanw cu.cu_bridge_id =
641 1.1.2.2 nathanw (((uint64_t)ntohs(cpdu.cbu_bridgepri)) << 48) |
642 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_bridgeaddr[0]) << 40) |
643 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_bridgeaddr[1]) << 32) |
644 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_bridgeaddr[2]) << 24) |
645 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_bridgeaddr[3]) << 16) |
646 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_bridgeaddr[4]) << 8) |
647 1.1.2.2 nathanw (((uint64_t)cpdu.cbu_bridgeaddr[5]) << 0);
648 1.1.2.2 nathanw
649 1.1.2.2 nathanw cu.cu_root_path_cost = ntohl(cpdu.cbu_rootpathcost);
650 1.1.2.2 nathanw cu.cu_message_age = ntohs(cpdu.cbu_messageage);
651 1.1.2.2 nathanw cu.cu_max_age = ntohs(cpdu.cbu_maxage);
652 1.1.2.2 nathanw cu.cu_hello_time = ntohs(cpdu.cbu_hellotime);
653 1.1.2.2 nathanw cu.cu_forward_delay = ntohs(cpdu.cbu_forwarddelay);
654 1.1.2.2 nathanw cu.cu_port_id = ntohs(cpdu.cbu_portid);
655 1.1.2.2 nathanw cu.cu_message_type = cpdu.cbu_bpdutype;
656 1.1.2.2 nathanw cu.cu_topology_change_acknowledgment =
657 1.1.2.2 nathanw (cpdu.cbu_flags & BSTP_FLAG_TCA) ? 1 : 0;
658 1.1.2.2 nathanw cu.cu_topology_change =
659 1.1.2.2 nathanw (cpdu.cbu_flags & BSTP_FLAG_TC) ? 1 : 0;
660 1.1.2.2 nathanw bstp_received_config_bpdu(sc, bif, &cu);
661 1.1.2.2 nathanw break;
662 1.1.2.2 nathanw default:
663 1.1.2.2 nathanw goto out;
664 1.1.2.2 nathanw }
665 1.1.2.2 nathanw
666 1.1.2.2 nathanw out:
667 1.1.2.2 nathanw if (m)
668 1.1.2.2 nathanw m_freem(m);
669 1.1.2.2 nathanw return (NULL);
670 1.1.2.2 nathanw }
671 1.1.2.2 nathanw
672 1.1.2.2 nathanw void
673 1.1.2.2 nathanw bstp_received_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
674 1.1.2.2 nathanw struct bstp_config_unit *cu)
675 1.1.2.2 nathanw {
676 1.1.2.2 nathanw int root;
677 1.1.2.2 nathanw
678 1.1.2.2 nathanw root = bstp_root_bridge(sc);
679 1.1.2.2 nathanw
680 1.1.2.2 nathanw if (bif->bif_state != BSTP_IFSTATE_DISABLED) {
681 1.1.2.2 nathanw if (bstp_supersedes_port_info(sc, bif, cu)) {
682 1.1.2.2 nathanw bstp_record_config_information(sc, bif, cu);
683 1.1.2.2 nathanw bstp_configuration_update(sc);
684 1.1.2.2 nathanw bstp_port_state_selection(sc);
685 1.1.2.2 nathanw
686 1.1.2.2 nathanw if ((bstp_root_bridge(sc) == 0) && root) {
687 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_hello_timer);
688 1.1.2.2 nathanw
689 1.1.2.2 nathanw if (sc->sc_topology_change_detected) {
690 1.1.2.2 nathanw bstp_timer_stop(
691 1.1.2.2 nathanw &sc->sc_topology_change_timer);
692 1.1.2.2 nathanw bstp_transmit_tcn(sc);
693 1.1.2.2 nathanw bstp_timer_start(&sc->sc_tcn_timer, 0);
694 1.1.2.2 nathanw }
695 1.1.2.2 nathanw }
696 1.1.2.2 nathanw
697 1.1.2.2 nathanw if (bif == sc->sc_root_port) {
698 1.1.2.2 nathanw bstp_record_config_timeout_values(sc, cu);
699 1.1.2.2 nathanw bstp_config_bpdu_generation(sc);
700 1.1.2.2 nathanw
701 1.1.2.2 nathanw if (cu->cu_topology_change_acknowledgment)
702 1.1.2.2 nathanw bstp_topology_change_acknowledged(sc);
703 1.1.2.2 nathanw }
704 1.1.2.2 nathanw } else if (bstp_designated_port(sc, bif))
705 1.1.2.2 nathanw bstp_transmit_config(sc, bif);
706 1.1.2.2 nathanw }
707 1.1.2.2 nathanw }
708 1.1.2.2 nathanw
709 1.1.2.2 nathanw void
710 1.1.2.2 nathanw bstp_received_tcn_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
711 1.1.2.2 nathanw struct bstp_tcn_unit *tcn)
712 1.1.2.2 nathanw {
713 1.1.2.2 nathanw if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
714 1.1.2.2 nathanw bstp_designated_port(sc, bif)) {
715 1.1.2.2 nathanw bstp_topology_change_detection(sc);
716 1.1.2.2 nathanw bstp_acknowledge_topology_change(sc, bif);
717 1.1.2.2 nathanw }
718 1.1.2.2 nathanw }
719 1.1.2.2 nathanw
720 1.1.2.2 nathanw void
721 1.1.2.2 nathanw bstp_hello_timer_expiry(struct bridge_softc *sc)
722 1.1.2.2 nathanw {
723 1.1.2.2 nathanw bstp_config_bpdu_generation(sc);
724 1.1.2.2 nathanw bstp_timer_start(&sc->sc_hello_timer, 0);
725 1.1.2.2 nathanw }
726 1.1.2.2 nathanw
727 1.1.2.2 nathanw void
728 1.1.2.2 nathanw bstp_message_age_timer_expiry(struct bridge_softc *sc,
729 1.1.2.2 nathanw struct bridge_iflist *bif)
730 1.1.2.2 nathanw {
731 1.1.2.2 nathanw int root;
732 1.1.2.2 nathanw
733 1.1.2.2 nathanw root = bstp_root_bridge(sc);
734 1.1.2.2 nathanw bstp_become_designated_port(sc, bif);
735 1.1.2.2 nathanw bstp_configuration_update(sc);
736 1.1.2.2 nathanw bstp_port_state_selection(sc);
737 1.1.2.2 nathanw
738 1.1.2.2 nathanw if ((bstp_root_bridge(sc)) && (root == 0)) {
739 1.1.2.2 nathanw sc->sc_max_age = sc->sc_bridge_max_age;
740 1.1.2.2 nathanw sc->sc_hello_time = sc->sc_bridge_hello_time;
741 1.1.2.2 nathanw sc->sc_forward_delay = sc->sc_bridge_forward_delay;
742 1.1.2.2 nathanw
743 1.1.2.2 nathanw bstp_topology_change_detection(sc);
744 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_tcn_timer);
745 1.1.2.2 nathanw bstp_config_bpdu_generation(sc);
746 1.1.2.2 nathanw bstp_timer_start(&sc->sc_hello_timer, 0);
747 1.1.2.2 nathanw }
748 1.1.2.2 nathanw }
749 1.1.2.2 nathanw
750 1.1.2.2 nathanw void
751 1.1.2.2 nathanw bstp_forward_delay_timer_expiry(struct bridge_softc *sc,
752 1.1.2.2 nathanw struct bridge_iflist *bif)
753 1.1.2.2 nathanw {
754 1.1.2.2 nathanw if (bif->bif_state == BSTP_IFSTATE_LISTENING) {
755 1.1.2.2 nathanw bstp_set_port_state(bif, BSTP_IFSTATE_LEARNING);
756 1.1.2.2 nathanw bstp_timer_start(&bif->bif_forward_delay_timer, 0);
757 1.1.2.2 nathanw } else if (bif->bif_state == BSTP_IFSTATE_LEARNING) {
758 1.1.2.2 nathanw bstp_set_port_state(bif, BSTP_IFSTATE_FORWARDING);
759 1.1.2.2 nathanw if (bstp_designated_for_some_port(sc) &&
760 1.1.2.2 nathanw bif->bif_change_detection_enabled)
761 1.1.2.2 nathanw bstp_topology_change_detection(sc);
762 1.1.2.2 nathanw }
763 1.1.2.2 nathanw }
764 1.1.2.2 nathanw
765 1.1.2.2 nathanw int
766 1.1.2.2 nathanw bstp_designated_for_some_port(struct bridge_softc *sc)
767 1.1.2.2 nathanw {
768 1.1.2.2 nathanw
769 1.1.2.2 nathanw struct bridge_iflist *bif;
770 1.1.2.2 nathanw
771 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
772 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
773 1.1.2.2 nathanw continue;
774 1.1.2.2 nathanw if (bif->bif_designated_bridge == sc->sc_bridge_id)
775 1.1.2.2 nathanw return (1);
776 1.1.2.2 nathanw }
777 1.1.2.2 nathanw return (0);
778 1.1.2.2 nathanw }
779 1.1.2.2 nathanw
780 1.1.2.2 nathanw void
781 1.1.2.2 nathanw bstp_tcn_timer_expiry(struct bridge_softc *sc)
782 1.1.2.2 nathanw {
783 1.1.2.2 nathanw bstp_transmit_tcn(sc);
784 1.1.2.2 nathanw bstp_timer_start(&sc->sc_tcn_timer, 0);
785 1.1.2.2 nathanw }
786 1.1.2.2 nathanw
787 1.1.2.2 nathanw void
788 1.1.2.2 nathanw bstp_topology_change_timer_expiry(struct bridge_softc *sc)
789 1.1.2.2 nathanw {
790 1.1.2.2 nathanw sc->sc_topology_change_detected = 0;
791 1.1.2.2 nathanw sc->sc_topology_change = 0;
792 1.1.2.2 nathanw }
793 1.1.2.2 nathanw
794 1.1.2.2 nathanw void
795 1.1.2.2 nathanw bstp_hold_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif)
796 1.1.2.2 nathanw {
797 1.1.2.2 nathanw if (bif->bif_config_pending)
798 1.1.2.2 nathanw bstp_transmit_config(sc, bif);
799 1.1.2.2 nathanw }
800 1.1.2.2 nathanw
801 1.1.2.2 nathanw void
802 1.1.2.2 nathanw bstp_initialization(struct bridge_softc *sc)
803 1.1.2.2 nathanw {
804 1.1.2.2 nathanw struct bridge_iflist *bif, *mif;
805 1.1.2.2 nathanw
806 1.1.2.2 nathanw mif = NULL;
807 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
808 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
809 1.1.2.2 nathanw continue;
810 1.1.2.2 nathanw if (bif->bif_ifp->if_type != IFT_ETHER)
811 1.1.2.2 nathanw continue;
812 1.1.2.2 nathanw bif->bif_port_id = (bif->bif_priority << 8) |
813 1.1.2.2 nathanw (bif->bif_ifp->if_index & 0xff);
814 1.1.2.2 nathanw
815 1.1.2.2 nathanw if (mif == NULL) {
816 1.1.2.2 nathanw mif = bif;
817 1.1.2.2 nathanw continue;
818 1.1.2.2 nathanw }
819 1.1.2.2 nathanw if (memcmp(LLADDR(bif->bif_ifp->if_sadl),
820 1.1.2.2 nathanw LLADDR(mif->bif_ifp->if_sadl), ETHER_ADDR_LEN) < 0) {
821 1.1.2.2 nathanw mif = bif;
822 1.1.2.2 nathanw continue;
823 1.1.2.2 nathanw }
824 1.1.2.2 nathanw }
825 1.1.2.2 nathanw if (mif == NULL) {
826 1.1.2.2 nathanw bstp_stop(sc);
827 1.1.2.2 nathanw return;
828 1.1.2.2 nathanw }
829 1.1.2.2 nathanw
830 1.1.2.2 nathanw sc->sc_bridge_id =
831 1.1.2.2 nathanw (((uint64_t)sc->sc_bridge_priority) << 48) |
832 1.1.2.2 nathanw (((uint64_t)LLADDR(mif->bif_ifp->if_sadl)[0]) << 40) |
833 1.1.2.2 nathanw (((uint64_t)LLADDR(mif->bif_ifp->if_sadl)[1]) << 32) |
834 1.1.2.2 nathanw (LLADDR(mif->bif_ifp->if_sadl)[2] << 24) |
835 1.1.2.2 nathanw (LLADDR(mif->bif_ifp->if_sadl)[3] << 16) |
836 1.1.2.2 nathanw (LLADDR(mif->bif_ifp->if_sadl)[4] << 8) |
837 1.1.2.2 nathanw (LLADDR(mif->bif_ifp->if_sadl)[5]);
838 1.1.2.2 nathanw
839 1.1.2.2 nathanw sc->sc_designated_root = sc->sc_bridge_id;
840 1.1.2.2 nathanw sc->sc_root_path_cost = 0;
841 1.1.2.2 nathanw sc->sc_root_port = NULL;
842 1.1.2.2 nathanw
843 1.1.2.2 nathanw sc->sc_max_age = sc->sc_bridge_max_age;
844 1.1.2.2 nathanw sc->sc_hello_time = sc->sc_bridge_hello_time;
845 1.1.2.2 nathanw sc->sc_forward_delay = sc->sc_bridge_forward_delay;
846 1.1.2.2 nathanw sc->sc_topology_change_detected = 0;
847 1.1.2.2 nathanw sc->sc_topology_change = 0;
848 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_tcn_timer);
849 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_topology_change_timer);
850 1.1.2.2 nathanw
851 1.1.2.2 nathanw if (callout_active(&sc->sc_bstpcallout) == 0)
852 1.1.2.2 nathanw callout_reset(&sc->sc_bstpcallout, hz,
853 1.1.2.2 nathanw bstp_tick, sc);
854 1.1.2.2 nathanw
855 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
856 1.1.2.2 nathanw if (bif->bif_flags & IFBIF_STP)
857 1.1.2.2 nathanw bstp_enable_port(sc, bif);
858 1.1.2.2 nathanw else
859 1.1.2.2 nathanw bstp_disable_port(sc, bif);
860 1.1.2.2 nathanw }
861 1.1.2.2 nathanw
862 1.1.2.2 nathanw bstp_port_state_selection(sc);
863 1.1.2.2 nathanw bstp_config_bpdu_generation(sc);
864 1.1.2.2 nathanw bstp_timer_start(&sc->sc_hello_timer, 0);
865 1.1.2.2 nathanw }
866 1.1.2.2 nathanw
867 1.1.2.2 nathanw void
868 1.1.2.2 nathanw bstp_stop(struct bridge_softc *sc)
869 1.1.2.2 nathanw {
870 1.1.2.2 nathanw struct bridge_iflist *bif;
871 1.1.2.2 nathanw
872 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
873 1.1.2.2 nathanw bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED);
874 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_hold_timer);
875 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_message_age_timer);
876 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_forward_delay_timer);
877 1.1.2.2 nathanw }
878 1.1.2.2 nathanw
879 1.1.2.2 nathanw callout_stop(&sc->sc_bstpcallout);
880 1.1.2.2 nathanw
881 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_topology_change_timer);
882 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_tcn_timer);
883 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_hello_timer);
884 1.1.2.2 nathanw
885 1.1.2.2 nathanw }
886 1.1.2.2 nathanw
887 1.1.2.2 nathanw void
888 1.1.2.2 nathanw bstp_initialize_port(struct bridge_softc *sc, struct bridge_iflist *bif)
889 1.1.2.2 nathanw {
890 1.1.2.2 nathanw bstp_become_designated_port(sc, bif);
891 1.1.2.2 nathanw bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
892 1.1.2.2 nathanw bif->bif_topology_change_acknowledge = 0;
893 1.1.2.2 nathanw bif->bif_config_pending = 0;
894 1.1.2.2 nathanw bif->bif_change_detection_enabled = 1;
895 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_message_age_timer);
896 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_forward_delay_timer);
897 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_hold_timer);
898 1.1.2.2 nathanw }
899 1.1.2.2 nathanw
900 1.1.2.2 nathanw void
901 1.1.2.2 nathanw bstp_enable_port(struct bridge_softc *sc, struct bridge_iflist *bif)
902 1.1.2.2 nathanw {
903 1.1.2.2 nathanw bstp_initialize_port(sc, bif);
904 1.1.2.2 nathanw bstp_port_state_selection(sc);
905 1.1.2.2 nathanw }
906 1.1.2.2 nathanw
907 1.1.2.2 nathanw void
908 1.1.2.2 nathanw bstp_disable_port(struct bridge_softc *sc, struct bridge_iflist *bif)
909 1.1.2.2 nathanw {
910 1.1.2.2 nathanw int root;
911 1.1.2.2 nathanw
912 1.1.2.2 nathanw root = bstp_root_bridge(sc);
913 1.1.2.2 nathanw bstp_become_designated_port(sc, bif);
914 1.1.2.2 nathanw bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED);
915 1.1.2.2 nathanw bif->bif_topology_change_acknowledge = 0;
916 1.1.2.2 nathanw bif->bif_config_pending = 0;
917 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_message_age_timer);
918 1.1.2.2 nathanw bstp_timer_stop(&bif->bif_forward_delay_timer);
919 1.1.2.2 nathanw bstp_configuration_update(sc);
920 1.1.2.2 nathanw bstp_port_state_selection(sc);
921 1.1.2.2 nathanw
922 1.1.2.2 nathanw if (bstp_root_bridge(sc) && (root == 0)) {
923 1.1.2.2 nathanw sc->sc_max_age = sc->sc_bridge_max_age;
924 1.1.2.2 nathanw sc->sc_hello_time = sc->sc_bridge_hello_time;
925 1.1.2.2 nathanw sc->sc_forward_delay = sc->sc_bridge_forward_delay;
926 1.1.2.2 nathanw
927 1.1.2.2 nathanw bstp_topology_change_detection(sc);
928 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_tcn_timer);
929 1.1.2.2 nathanw bstp_config_bpdu_generation(sc);
930 1.1.2.2 nathanw bstp_timer_start(&sc->sc_hello_timer, 0);
931 1.1.2.2 nathanw }
932 1.1.2.2 nathanw }
933 1.1.2.2 nathanw
934 1.1.2.2 nathanw void
935 1.1.2.2 nathanw bstp_set_bridge_priority(struct bridge_softc *sc, uint64_t new_bridge_id)
936 1.1.2.2 nathanw {
937 1.1.2.2 nathanw struct bridge_iflist *bif;
938 1.1.2.2 nathanw int root;
939 1.1.2.2 nathanw
940 1.1.2.2 nathanw root = bstp_root_bridge(sc);
941 1.1.2.2 nathanw
942 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
943 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
944 1.1.2.2 nathanw continue;
945 1.1.2.2 nathanw if (bstp_designated_port(sc, bif))
946 1.1.2.2 nathanw bif->bif_designated_bridge = new_bridge_id;
947 1.1.2.2 nathanw }
948 1.1.2.2 nathanw
949 1.1.2.2 nathanw sc->sc_bridge_id = new_bridge_id;
950 1.1.2.2 nathanw
951 1.1.2.2 nathanw bstp_configuration_update(sc);
952 1.1.2.2 nathanw bstp_port_state_selection(sc);
953 1.1.2.2 nathanw
954 1.1.2.2 nathanw if (bstp_root_bridge(sc) && (root == 0)) {
955 1.1.2.2 nathanw sc->sc_max_age = sc->sc_bridge_max_age;
956 1.1.2.2 nathanw sc->sc_hello_time = sc->sc_bridge_hello_time;
957 1.1.2.2 nathanw sc->sc_forward_delay = sc->sc_bridge_forward_delay;
958 1.1.2.2 nathanw
959 1.1.2.2 nathanw bstp_topology_change_detection(sc);
960 1.1.2.2 nathanw bstp_timer_stop(&sc->sc_tcn_timer);
961 1.1.2.2 nathanw bstp_config_bpdu_generation(sc);
962 1.1.2.2 nathanw bstp_timer_start(&sc->sc_hello_timer, 0);
963 1.1.2.2 nathanw }
964 1.1.2.2 nathanw }
965 1.1.2.2 nathanw
966 1.1.2.2 nathanw void
967 1.1.2.2 nathanw bstp_set_port_priority(struct bridge_softc *sc, struct bridge_iflist *bif,
968 1.1.2.2 nathanw uint16_t new_port_id)
969 1.1.2.2 nathanw {
970 1.1.2.2 nathanw if (bstp_designated_port(sc, bif))
971 1.1.2.2 nathanw bif->bif_designated_port = new_port_id;
972 1.1.2.2 nathanw
973 1.1.2.2 nathanw bif->bif_port_id = new_port_id;
974 1.1.2.2 nathanw
975 1.1.2.2 nathanw if ((sc->sc_bridge_id == bif->bif_designated_bridge) &&
976 1.1.2.2 nathanw (bif->bif_port_id < bif->bif_designated_port)) {
977 1.1.2.2 nathanw bstp_become_designated_port(sc, bif);
978 1.1.2.2 nathanw bstp_port_state_selection(sc);
979 1.1.2.2 nathanw }
980 1.1.2.2 nathanw }
981 1.1.2.2 nathanw
982 1.1.2.2 nathanw void
983 1.1.2.2 nathanw bstp_set_path_cost(struct bridge_softc *sc, struct bridge_iflist *bif,
984 1.1.2.2 nathanw uint32_t path_cost)
985 1.1.2.2 nathanw {
986 1.1.2.2 nathanw bif->bif_path_cost = path_cost;
987 1.1.2.2 nathanw bstp_configuration_update(sc);
988 1.1.2.2 nathanw bstp_port_state_selection(sc);
989 1.1.2.2 nathanw }
990 1.1.2.2 nathanw
991 1.1.2.2 nathanw void
992 1.1.2.2 nathanw bstp_enable_change_detection(struct bridge_iflist *bif)
993 1.1.2.2 nathanw {
994 1.1.2.2 nathanw bif->bif_change_detection_enabled = 1;
995 1.1.2.2 nathanw }
996 1.1.2.2 nathanw
997 1.1.2.2 nathanw void
998 1.1.2.2 nathanw bstp_disable_change_detection(struct bridge_iflist *bif)
999 1.1.2.2 nathanw {
1000 1.1.2.2 nathanw bif->bif_change_detection_enabled = 0;
1001 1.1.2.2 nathanw }
1002 1.1.2.2 nathanw
1003 1.1.2.2 nathanw void
1004 1.1.2.2 nathanw bstp_ifupdstatus(struct bridge_softc *sc, struct bridge_iflist *bif)
1005 1.1.2.2 nathanw {
1006 1.1.2.2 nathanw struct ifnet *ifp = bif->bif_ifp;
1007 1.1.2.2 nathanw
1008 1.1.2.2 nathanw if (ifp->if_flags & IFF_UP) {
1009 1.1.2.2 nathanw switch (ifp->if_link_state) {
1010 1.1.2.2 nathanw case LINK_STATE_UNKNOWN:
1011 1.1.2.2 nathanw /*
1012 1.1.2.2 nathanw * Just enable the port if the link state is
1013 1.1.2.2 nathanw * unknown.
1014 1.1.2.2 nathanw */
1015 1.1.2.2 nathanw if (bif->bif_state == BSTP_IFSTATE_DISABLED)
1016 1.1.2.2 nathanw bstp_enable_port(sc, bif);
1017 1.1.2.2 nathanw break;
1018 1.1.2.2 nathanw
1019 1.1.2.2 nathanw case LINK_STATE_UP:
1020 1.1.2.2 nathanw if (bif->bif_state == BSTP_IFSTATE_DISABLED)
1021 1.1.2.2 nathanw bstp_enable_port(sc, bif);
1022 1.1.2.2 nathanw break;
1023 1.1.2.2 nathanw
1024 1.1.2.2 nathanw case LINK_STATE_DOWN:
1025 1.1.2.2 nathanw if (bif->bif_state != BSTP_IFSTATE_DISABLED)
1026 1.1.2.2 nathanw bstp_disable_port(sc, bif);
1027 1.1.2.2 nathanw break;
1028 1.1.2.2 nathanw }
1029 1.1.2.2 nathanw return;
1030 1.1.2.2 nathanw }
1031 1.1.2.2 nathanw
1032 1.1.2.2 nathanw if (bif->bif_state != BSTP_IFSTATE_DISABLED)
1033 1.1.2.2 nathanw bstp_disable_port(sc, bif);
1034 1.1.2.2 nathanw }
1035 1.1.2.2 nathanw
1036 1.1.2.2 nathanw void
1037 1.1.2.2 nathanw bstp_tick(void *arg)
1038 1.1.2.2 nathanw {
1039 1.1.2.2 nathanw struct bridge_softc *sc = arg;
1040 1.1.2.2 nathanw struct bridge_iflist *bif;
1041 1.1.2.2 nathanw int s;
1042 1.1.2.2 nathanw
1043 1.1.2.2 nathanw s = splnet();
1044 1.1.2.2 nathanw
1045 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1046 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
1047 1.1.2.2 nathanw continue;
1048 1.1.2.2 nathanw /*
1049 1.1.2.2 nathanw * XXX This can cause a lag in "link does away"
1050 1.1.2.2 nathanw * XXX and "spanning tree gets updated". We need
1051 1.1.2.2 nathanw * XXX come sort of callback from the link state
1052 1.1.2.2 nathanw * XXX update code to kick spanning tree.
1053 1.1.2.2 nathanw * XXX --thorpej (at) netbsd.org
1054 1.1.2.2 nathanw */
1055 1.1.2.2 nathanw bstp_ifupdstatus(sc, bif);
1056 1.1.2.2 nathanw }
1057 1.1.2.2 nathanw
1058 1.1.2.2 nathanw if (bstp_timer_expired(&sc->sc_hello_timer, sc->sc_hello_time))
1059 1.1.2.2 nathanw bstp_hello_timer_expiry(sc);
1060 1.1.2.2 nathanw
1061 1.1.2.2 nathanw if (bstp_timer_expired(&sc->sc_tcn_timer, sc->sc_bridge_hello_time))
1062 1.1.2.2 nathanw bstp_tcn_timer_expiry(sc);
1063 1.1.2.2 nathanw
1064 1.1.2.2 nathanw if (bstp_timer_expired(&sc->sc_topology_change_timer,
1065 1.1.2.2 nathanw sc->sc_topology_change_time))
1066 1.1.2.2 nathanw bstp_topology_change_timer_expiry(sc);
1067 1.1.2.2 nathanw
1068 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1069 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
1070 1.1.2.2 nathanw continue;
1071 1.1.2.2 nathanw if (bstp_timer_expired(&bif->bif_message_age_timer,
1072 1.1.2.2 nathanw sc->sc_max_age))
1073 1.1.2.2 nathanw bstp_message_age_timer_expiry(sc, bif);
1074 1.1.2.2 nathanw }
1075 1.1.2.2 nathanw
1076 1.1.2.2 nathanw LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1077 1.1.2.2 nathanw if ((bif->bif_flags & IFBIF_STP) == 0)
1078 1.1.2.2 nathanw continue;
1079 1.1.2.2 nathanw if (bstp_timer_expired(&bif->bif_forward_delay_timer,
1080 1.1.2.2 nathanw sc->sc_forward_delay))
1081 1.1.2.2 nathanw bstp_forward_delay_timer_expiry(sc, bif);
1082 1.1.2.2 nathanw
1083 1.1.2.2 nathanw if (bstp_timer_expired(&bif->bif_hold_timer,
1084 1.1.2.2 nathanw sc->sc_hold_time))
1085 1.1.2.2 nathanw bstp_hold_timer_expiry(sc, bif);
1086 1.1.2.2 nathanw }
1087 1.1.2.2 nathanw
1088 1.1.2.2 nathanw if (sc->sc_if.if_flags & IFF_RUNNING)
1089 1.1.2.2 nathanw callout_reset(&sc->sc_bstpcallout, hz, bstp_tick, sc);
1090 1.1.2.2 nathanw
1091 1.1.2.2 nathanw splx(s);
1092 1.1.2.2 nathanw }
1093 1.1.2.2 nathanw
1094 1.1.2.2 nathanw void
1095 1.1.2.2 nathanw bstp_timer_start(struct bridge_timer *t, uint16_t v)
1096 1.1.2.2 nathanw {
1097 1.1.2.2 nathanw t->value = v;
1098 1.1.2.2 nathanw t->active = 1;
1099 1.1.2.2 nathanw }
1100 1.1.2.2 nathanw
1101 1.1.2.2 nathanw void
1102 1.1.2.2 nathanw bstp_timer_stop(struct bridge_timer *t)
1103 1.1.2.2 nathanw {
1104 1.1.2.2 nathanw t->value = 0;
1105 1.1.2.2 nathanw t->active = 0;
1106 1.1.2.2 nathanw }
1107 1.1.2.2 nathanw
1108 1.1.2.2 nathanw int
1109 1.1.2.2 nathanw bstp_timer_expired(struct bridge_timer *t, uint16_t v)
1110 1.1.2.2 nathanw {
1111 1.1.2.2 nathanw if (t->active == 0)
1112 1.1.2.2 nathanw return (0);
1113 1.1.2.2 nathanw t->value += BSTP_TICK_VAL;
1114 1.1.2.2 nathanw if (t->value >= v) {
1115 1.1.2.2 nathanw bstp_timer_stop(t);
1116 1.1.2.2 nathanw return (1);
1117 1.1.2.2 nathanw }
1118 1.1.2.2 nathanw return (0);
1119 1.1.2.2 nathanw
1120 1.1.2.2 nathanw }
1121