2 * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Jason L. Wright
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 * $OpenBSD: bridgestp.c,v 1.5 2001/03/22 03:48:29 jason Exp $
32 * $NetBSD: bridgestp.c,v 1.5 2003/11/28 08:56:48 keihan Exp $
33 * $FreeBSD: src/sys/net/bridgestp.c,v 1.7 2005/10/11 02:58:32 thompsa Exp $
37 * Implementation of the spanning tree protocol as defined in
38 * ISO/IEC Final DIS 15802-3 (IEEE P802.1D/D17), May 25, 1998.
39 * (In English: IEEE 802.1D, Draft 17, 1998)
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/callout.h>
51 #include <sys/thread.h>
52 #include <sys/thread2.h>
53 #include <sys/msgport2.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_llc.h>
59 #include <net/if_media.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/if_ether.h>
65 #include <net/bridge/if_bridgevar.h>
67 /* BPDU message types */
68 #define BSTP_MSGTYPE_CFG 0x00 /* Configuration */
69 #define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */
72 #define BSTP_FLAG_TC 0x01 /* Topology change */
73 #define BSTP_FLAG_TCA 0x80 /* Topology change ack */
75 #define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */
76 #define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */
79 * Because BPDU's do not make nicely aligned structures, two different
80 * declarations are used: bstp_?bpdu (wire representation, packed) and
81 * bstp_*_unit (internal, nicely aligned version).
84 /* configuration bridge protocol data unit */
86 uint8_t cbu_dsap; /* LLC: destination sap */
87 uint8_t cbu_ssap; /* LLC: source sap */
88 uint8_t cbu_ctl; /* LLC: control */
89 uint16_t cbu_protoid; /* protocol id */
90 uint8_t cbu_protover; /* protocol version */
91 uint8_t cbu_bpdutype; /* message type */
92 uint8_t cbu_flags; /* flags (below) */
95 uint16_t cbu_rootpri; /* root priority */
96 uint8_t cbu_rootaddr[6]; /* root address */
98 uint32_t cbu_rootpathcost; /* root path cost */
101 uint16_t cbu_bridgepri; /* bridge priority */
102 uint8_t cbu_bridgeaddr[6]; /* bridge address */
104 uint16_t cbu_portid; /* port id */
105 uint16_t cbu_messageage; /* current message age */
106 uint16_t cbu_maxage; /* maximum age */
107 uint16_t cbu_hellotime; /* hello time */
108 uint16_t cbu_forwarddelay; /* forwarding delay */
109 } __attribute__((__packed__));
111 /* topology change notification bridge protocol data unit */
113 uint8_t tbu_dsap; /* LLC: destination sap */
114 uint8_t tbu_ssap; /* LLC: source sap */
115 uint8_t tbu_ctl; /* LLC: control */
116 uint16_t tbu_protoid; /* protocol id */
117 uint8_t tbu_protover; /* protocol version */
118 uint8_t tbu_bpdutype; /* message type */
119 } __attribute__((__packed__));
121 const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
123 static void bstp_initialize_port(struct bridge_softc *,
124 struct bridge_iflist *);
125 static void bstp_ifupdstatus(struct bridge_softc *, struct bridge_iflist *);
126 static void bstp_enable_port(struct bridge_softc *, struct bridge_iflist *);
127 static void bstp_disable_port(struct bridge_softc *,
128 struct bridge_iflist *);
130 static void bstp_enable_change_detection(struct bridge_iflist *);
131 static void bstp_disable_change_detection(struct bridge_iflist *);
133 static int bstp_root_bridge(struct bridge_softc *sc);
134 static int bstp_supersedes_port_info(struct bridge_softc *,
135 struct bridge_iflist *, struct bstp_config_unit *);
136 static int bstp_designated_port(struct bridge_softc *,
137 struct bridge_iflist *);
138 static int bstp_designated_for_some_port(struct bridge_softc *);
139 static void bstp_transmit_config(struct bridge_softc *,
140 struct bridge_iflist *);
141 static void bstp_transmit_tcn(struct bridge_softc *);
142 static void bstp_received_config_bpdu(struct bridge_softc *,
143 struct bridge_iflist *, struct bstp_config_unit *);
144 static void bstp_received_tcn_bpdu(struct bridge_softc *,
145 struct bridge_iflist *, struct bstp_tcn_unit *);
146 static void bstp_record_config_information(struct bridge_softc *,
147 struct bridge_iflist *, struct bstp_config_unit *);
148 static void bstp_record_config_timeout_values(struct bridge_softc *,
149 struct bstp_config_unit *);
150 static void bstp_config_bpdu_generation(struct bridge_softc *);
151 static void bstp_send_config_bpdu(struct bridge_softc *,
152 struct bridge_iflist *, struct bstp_config_unit *);
153 static void bstp_configuration_update(struct bridge_softc *);
154 static void bstp_root_selection(struct bridge_softc *);
155 static void bstp_designated_port_selection(struct bridge_softc *);
156 static void bstp_become_designated_port(struct bridge_softc *,
157 struct bridge_iflist *);
158 static void bstp_port_state_selection(struct bridge_softc *);
159 static void bstp_make_forwarding(struct bridge_softc *,
160 struct bridge_iflist *);
161 static void bstp_make_blocking(struct bridge_softc *,
162 struct bridge_iflist *);
163 static void bstp_make_l1blocking(struct bridge_softc *sc,
164 struct bridge_iflist *bif);
165 static void bstp_set_port_state(struct bridge_iflist *, uint8_t);
167 static void bstp_set_bridge_priority(struct bridge_softc *, uint64_t);
168 static void bstp_set_port_priority(struct bridge_softc *,
169 struct bridge_iflist *, uint16_t);
170 static void bstp_set_path_cost(struct bridge_softc *,
171 struct bridge_iflist *, uint32_t);
173 static void bstp_topology_change_detection(struct bridge_softc *);
174 static void bstp_topology_change_acknowledged(struct bridge_softc *);
175 static void bstp_acknowledge_topology_change(struct bridge_softc *,
176 struct bridge_iflist *);
178 static void bstp_tick(void *);
179 static void bstp_timer_start(struct bridge_timer *, uint16_t);
180 static void bstp_timer_stop(struct bridge_timer *);
181 static int bstp_timer_expired(struct bridge_timer *, uint16_t);
183 static void bstp_hold_timer_expiry(struct bridge_softc *,
184 struct bridge_iflist *);
185 static void bstp_message_age_timer_expiry(struct bridge_softc *,
186 struct bridge_iflist *);
187 static void bstp_forward_delay_timer_expiry(struct bridge_softc *,
188 struct bridge_iflist *);
189 static void bstp_topology_change_timer_expiry(struct bridge_softc *);
190 static void bstp_tcn_timer_expiry(struct bridge_softc *);
191 static void bstp_hello_timer_expiry(struct bridge_softc *);
192 static int bstp_addr_cmp(const uint8_t *, const uint8_t *);
195 bstp_transmit_config(struct bridge_softc *sc, struct bridge_iflist *bif)
197 if (bif->bif_hold_timer.active) {
198 bif->bif_config_pending = 1;
202 bif->bif_config_bpdu.cu_message_type = BSTP_MSGTYPE_CFG;
203 bif->bif_config_bpdu.cu_rootid = sc->sc_designated_root;
204 bif->bif_config_bpdu.cu_root_path_cost = sc->sc_root_path_cost;
205 bif->bif_config_bpdu.cu_bridge_id = sc->sc_bridge_id;
206 bif->bif_config_bpdu.cu_port_id = bif->bif_port_id;
208 if (bstp_root_bridge(sc)) {
209 bif->bif_config_bpdu.cu_message_age = 0;
211 bif->bif_config_bpdu.cu_message_age =
212 sc->sc_root_port->bifi_message_age_timer.value +
213 BSTP_MESSAGE_AGE_INCR;
216 bif->bif_config_bpdu.cu_max_age = sc->sc_max_age;
217 bif->bif_config_bpdu.cu_hello_time = sc->sc_hello_time;
218 bif->bif_config_bpdu.cu_forward_delay = sc->sc_forward_delay;
219 bif->bif_config_bpdu.cu_topology_change_acknowledgment
220 = bif->bif_topology_change_acknowledge;
221 bif->bif_config_bpdu.cu_topology_change = sc->sc_topology_change;
223 if (bif->bif_config_bpdu.cu_message_age < sc->sc_max_age ||
224 (sc->sc_ifp->if_flags & IFF_LINK1)) {
225 bif->bif_topology_change_acknowledge = 0;
226 bif->bif_config_pending = 0;
227 bstp_send_config_bpdu(sc, bif, &bif->bif_config_bpdu);
228 bstp_timer_start(&bif->bif_hold_timer, 0);
233 bstp_send_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
234 struct bstp_config_unit *cu)
238 struct ether_header *eh;
239 struct bstp_cbpdu bpdu;
243 if ((ifp->if_flags & IFF_RUNNING) == 0)
246 MGETHDR(m, MB_DONTWAIT, MT_DATA);
250 eh = mtod(m, struct ether_header *);
252 m->m_pkthdr.rcvif = ifp;
253 m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
254 m->m_len = m->m_pkthdr.len;
256 bpdu.cbu_ssap = bpdu.cbu_dsap = LLC_8021D_LSAP;
257 bpdu.cbu_ctl = LLC_UI;
258 bpdu.cbu_protoid = htons(0);
259 bpdu.cbu_protover = 0;
260 bpdu.cbu_bpdutype = cu->cu_message_type;
261 bpdu.cbu_flags = (cu->cu_topology_change ? BSTP_FLAG_TC : 0) |
262 (cu->cu_topology_change_acknowledgment ? BSTP_FLAG_TCA : 0);
264 bpdu.cbu_rootpri = htons(cu->cu_rootid >> 48);
265 bpdu.cbu_rootaddr[0] = cu->cu_rootid >> 40;
266 bpdu.cbu_rootaddr[1] = cu->cu_rootid >> 32;
267 bpdu.cbu_rootaddr[2] = cu->cu_rootid >> 24;
268 bpdu.cbu_rootaddr[3] = cu->cu_rootid >> 16;
269 bpdu.cbu_rootaddr[4] = cu->cu_rootid >> 8;
270 bpdu.cbu_rootaddr[5] = cu->cu_rootid >> 0;
272 bpdu.cbu_rootpathcost = htonl(cu->cu_root_path_cost);
274 bpdu.cbu_bridgepri = htons(cu->cu_bridge_id >> 48);
275 bpdu.cbu_bridgeaddr[0] = cu->cu_bridge_id >> 40;
276 bpdu.cbu_bridgeaddr[1] = cu->cu_bridge_id >> 32;
277 bpdu.cbu_bridgeaddr[2] = cu->cu_bridge_id >> 24;
278 bpdu.cbu_bridgeaddr[3] = cu->cu_bridge_id >> 16;
279 bpdu.cbu_bridgeaddr[4] = cu->cu_bridge_id >> 8;
280 bpdu.cbu_bridgeaddr[5] = cu->cu_bridge_id >> 0;
282 bpdu.cbu_portid = htons(cu->cu_port_id);
283 bpdu.cbu_messageage = htons(cu->cu_message_age);
284 bpdu.cbu_maxage = htons(cu->cu_max_age);
285 bpdu.cbu_hellotime = htons(cu->cu_hello_time);
286 bpdu.cbu_forwarddelay = htons(cu->cu_forward_delay);
288 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
289 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
290 eh->ether_type = htons(sizeof(bpdu));
292 memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
294 bridge_enqueue(ifp, m);
298 bstp_root_bridge(struct bridge_softc *sc)
300 return (sc->sc_designated_root == sc->sc_bridge_id);
304 bstp_supersedes_port_info(struct bridge_softc *sc, struct bridge_iflist *bif,
305 struct bstp_config_unit *cu)
307 if (cu->cu_rootid < bif->bif_designated_root)
309 if (cu->cu_rootid > bif->bif_designated_root)
312 if (cu->cu_root_path_cost < bif->bif_designated_cost)
314 if (cu->cu_root_path_cost > bif->bif_designated_cost)
317 if (cu->cu_bridge_id < bif->bif_designated_bridge)
319 if (cu->cu_bridge_id > bif->bif_designated_bridge)
322 if (sc->sc_bridge_id != cu->cu_bridge_id)
324 if (cu->cu_port_id <= bif->bif_designated_port)
330 bstp_record_config_information(struct bridge_softc *sc,
331 struct bridge_iflist *bif, struct bstp_config_unit *cu)
333 bif->bif_designated_root = cu->cu_rootid;
334 bif->bif_designated_cost = cu->cu_root_path_cost;
335 bif->bif_designated_bridge = cu->cu_bridge_id;
336 bif->bif_designated_port = cu->cu_port_id;
337 bstp_timer_start(&bif->bif_message_age_timer, cu->cu_message_age);
341 bstp_record_config_timeout_values(struct bridge_softc *sc,
342 struct bstp_config_unit *config)
344 sc->sc_max_age = config->cu_max_age;
345 sc->sc_hello_time = config->cu_hello_time;
346 sc->sc_forward_delay = config->cu_forward_delay;
347 sc->sc_topology_change = config->cu_topology_change;
351 bstp_config_bpdu_generation(struct bridge_softc *sc)
353 struct bridge_iflist *bif, *nbif;
355 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
356 if ((bif->bif_flags & IFBIF_STP) == 0)
358 if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
359 ((sc->sc_ifp->if_flags & IFF_LINK1) ||
360 bstp_designated_port(sc, bif))) {
361 bstp_transmit_config(sc, bif);
364 if (nbif != NULL && !nbif->bif_onlist) {
365 KKASSERT(bif->bif_onlist);
366 nbif = LIST_NEXT(bif, bif_next);
372 bstp_designated_port(struct bridge_softc *sc, struct bridge_iflist *bif)
374 return ((bif->bif_designated_bridge == sc->sc_bridge_id)
375 && (bif->bif_designated_port == bif->bif_port_id));
379 bstp_transmit_tcn(struct bridge_softc *sc)
381 struct bstp_tbpdu bpdu;
382 struct ifnet *ifp = sc->sc_root_port->bifi_ifp;
383 struct ether_header *eh;
386 if ((ifp->if_flags & IFF_RUNNING) == 0)
389 MGETHDR(m, MB_DONTWAIT, MT_DATA);
393 m->m_pkthdr.rcvif = ifp;
394 m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
395 m->m_len = m->m_pkthdr.len;
397 eh = mtod(m, struct ether_header *);
399 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
400 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
401 eh->ether_type = htons(sizeof(bpdu));
403 bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP;
404 bpdu.tbu_ctl = LLC_UI;
405 bpdu.tbu_protoid = 0;
406 bpdu.tbu_protover = 0;
407 bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN;
409 memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
411 bridge_enqueue(ifp, m);
415 bstp_configuration_update(struct bridge_softc *sc)
417 bstp_root_selection(sc);
418 bstp_designated_port_selection(sc);
422 bstp_root_selection(struct bridge_softc *sc)
424 struct bridge_iflist *root_port = NULL, *bif;
426 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
427 if ((bif->bif_flags & IFBIF_STP) == 0)
429 if (bstp_designated_port(sc, bif))
431 if (bif->bif_state == BSTP_IFSTATE_DISABLED)
433 if (bif->bif_designated_root >= sc->sc_bridge_id)
435 if (root_port == NULL)
438 if (bif->bif_designated_root < root_port->bif_designated_root)
440 if (bif->bif_designated_root > root_port->bif_designated_root)
443 if ((bif->bif_designated_cost + bif->bif_path_cost) <
444 (root_port->bif_designated_cost + root_port->bif_path_cost))
446 if ((bif->bif_designated_cost + bif->bif_path_cost) >
447 (root_port->bif_designated_cost + root_port->bif_path_cost))
450 if (bif->bif_designated_bridge <
451 root_port->bif_designated_bridge)
453 if (bif->bif_designated_bridge >
454 root_port->bif_designated_bridge)
457 if (bif->bif_designated_port < root_port->bif_designated_port)
459 if (bif->bif_designated_port > root_port->bif_designated_port)
462 if (bif->bif_port_id >= root_port->bif_port_id)
468 if (root_port == NULL) {
469 sc->sc_root_port = NULL;
470 sc->sc_designated_root = sc->sc_bridge_id;
471 sc->sc_root_path_cost = 0;
473 sc->sc_root_port = root_port->bif_info;
474 sc->sc_designated_root = root_port->bif_designated_root;
475 sc->sc_root_path_cost = root_port->bif_designated_cost +
476 root_port->bif_path_cost;
481 bstp_designated_port_selection(struct bridge_softc *sc)
483 struct bridge_iflist *bif;
485 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
486 if ((bif->bif_flags & IFBIF_STP) == 0)
488 if (bstp_designated_port(sc, bif))
490 if (bif->bif_designated_root != sc->sc_designated_root)
493 if (sc->sc_root_path_cost < bif->bif_designated_cost)
495 if (sc->sc_root_path_cost > bif->bif_designated_cost)
498 if (sc->sc_bridge_id < bif->bif_designated_bridge)
500 if (sc->sc_bridge_id > bif->bif_designated_bridge)
503 if (bif->bif_port_id > bif->bif_designated_port)
506 bstp_become_designated_port(sc, bif);
511 bstp_become_designated_port(struct bridge_softc *sc, struct bridge_iflist *bif)
513 bif->bif_designated_root = sc->sc_designated_root;
514 bif->bif_designated_cost = sc->sc_root_path_cost;
515 bif->bif_designated_bridge = sc->sc_bridge_id;
516 bif->bif_designated_port = bif->bif_port_id;
520 bstp_port_state_selection(struct bridge_softc *sc)
522 struct bridge_iflist *bif, *nbif;
524 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
525 if ((bif->bif_flags & IFBIF_STP) == 0)
527 if (bif->bif_info == sc->sc_root_port) {
528 bif->bif_config_pending = 0;
529 bif->bif_topology_change_acknowledge = 0;
530 bstp_make_forwarding(sc, bif);
531 } else if (bstp_designated_port(sc, bif)) {
532 bstp_timer_stop(&bif->bif_message_age_timer);
533 bstp_make_forwarding(sc, bif);
535 bif->bif_config_pending = 0;
536 bif->bif_topology_change_acknowledge = 0;
537 bstp_make_blocking(sc, bif);
540 if (nbif != NULL && !nbif->bif_onlist) {
541 KKASSERT(bif->bif_onlist);
542 nbif = LIST_NEXT(bif, bif_next);
548 bstp_make_forwarding(struct bridge_softc *sc, struct bridge_iflist *bif)
550 if (bif->bif_state == BSTP_IFSTATE_BLOCKING) {
551 bstp_set_port_state(bif, BSTP_IFSTATE_LISTENING);
552 bstp_timer_start(&bif->bif_forward_delay_timer, 0);
557 bstp_make_blocking(struct bridge_softc *sc, struct bridge_iflist *bif)
559 if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
560 bif->bif_state != BSTP_IFSTATE_BLOCKING &&
561 bif->bif_state != BSTP_IFSTATE_L1BLOCKING) {
562 if ((bif->bif_state == BSTP_IFSTATE_FORWARDING) ||
563 (bif->bif_state == BSTP_IFSTATE_LEARNING)) {
564 if (bif->bif_change_detection_enabled) {
565 bstp_topology_change_detection(sc);
568 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
569 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
570 bstp_timer_stop(&bif->bif_forward_delay_timer);
575 bstp_make_l1blocking(struct bridge_softc *sc, struct bridge_iflist *bif)
577 switch(bif->bif_state) {
578 case BSTP_IFSTATE_LISTENING:
579 case BSTP_IFSTATE_LEARNING:
580 case BSTP_IFSTATE_FORWARDING:
581 case BSTP_IFSTATE_BLOCKING:
582 bstp_set_port_state(bif, BSTP_IFSTATE_L1BLOCKING);
583 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
584 bstp_timer_stop(&bif->bif_forward_delay_timer);
585 bstp_timer_stop(&bif->bif_link1_timer);
593 bstp_set_port_state(struct bridge_iflist *bif, uint8_t state)
595 bif->bif_state = state;
599 bstp_topology_change_detection(struct bridge_softc *sc)
601 if (bstp_root_bridge(sc)) {
602 sc->sc_topology_change = 1;
603 bstp_timer_start(&sc->sc_topology_change_timer, 0);
604 } else if (!sc->sc_topology_change_detected) {
605 bstp_transmit_tcn(sc);
606 bstp_timer_start(&sc->sc_tcn_timer, 0);
608 sc->sc_topology_change_detected = 1;
612 bstp_topology_change_acknowledged(struct bridge_softc *sc)
614 sc->sc_topology_change_detected = 0;
615 bstp_timer_stop(&sc->sc_tcn_timer);
619 bstp_acknowledge_topology_change(struct bridge_softc *sc,
620 struct bridge_iflist *bif)
622 bif->bif_topology_change_acknowledge = 1;
623 bstp_transmit_config(sc, bif);
627 bstp_input(struct bridge_softc *sc, struct bridge_iflist *bif, struct mbuf *m)
629 struct ether_header *eh;
630 struct bstp_tbpdu tpdu;
631 struct bstp_cbpdu cpdu;
632 struct bstp_config_unit cu;
633 struct bstp_tcn_unit tu;
636 if ((bif->bif_flags & IFBIF_STP) == 0)
640 * The L1BLOCKING (ping pong failover) test needs to reset the
641 * timer if LINK1 is active.
643 if (bif->bif_state == BSTP_IFSTATE_L1BLOCKING) {
644 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
645 if (sc->sc_ifp->if_flags & IFF_LINK1)
646 bstp_timer_start(&bif->bif_link1_timer, 0);
647 bstp_make_forwarding(sc, bif);
648 } else if (sc->sc_ifp->if_flags & IFF_LINK1) {
649 bstp_timer_start(&bif->bif_link1_timer, 0);
652 eh = mtod(m, struct ether_header *);
654 len = ntohs(eh->ether_type);
655 if (len < sizeof(tpdu))
658 m_adj(m, ETHER_HDR_LEN);
660 if (m->m_pkthdr.len > len)
661 m_adj(m, len - m->m_pkthdr.len);
662 if (m->m_len < sizeof(tpdu) &&
663 (m = m_pullup(m, sizeof(tpdu))) == NULL)
666 memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu));
668 if (tpdu.tbu_dsap != LLC_8021D_LSAP ||
669 tpdu.tbu_ssap != LLC_8021D_LSAP ||
670 tpdu.tbu_ctl != LLC_UI)
672 if (tpdu.tbu_protoid != 0 || tpdu.tbu_protover != 0)
675 switch (tpdu.tbu_bpdutype) {
676 case BSTP_MSGTYPE_TCN:
677 tu.tu_message_type = tpdu.tbu_bpdutype;
678 bstp_received_tcn_bpdu(sc, bif, &tu);
680 case BSTP_MSGTYPE_CFG:
681 if (m->m_len < sizeof(cpdu) &&
682 (m = m_pullup(m, sizeof(cpdu))) == NULL)
684 memcpy(&cpdu, mtod(m, caddr_t), sizeof(cpdu));
687 (((uint64_t)ntohs(cpdu.cbu_rootpri)) << 48) |
688 (((uint64_t)cpdu.cbu_rootaddr[0]) << 40) |
689 (((uint64_t)cpdu.cbu_rootaddr[1]) << 32) |
690 (((uint64_t)cpdu.cbu_rootaddr[2]) << 24) |
691 (((uint64_t)cpdu.cbu_rootaddr[3]) << 16) |
692 (((uint64_t)cpdu.cbu_rootaddr[4]) << 8) |
693 (((uint64_t)cpdu.cbu_rootaddr[5]) << 0);
696 (((uint64_t)ntohs(cpdu.cbu_bridgepri)) << 48) |
697 (((uint64_t)cpdu.cbu_bridgeaddr[0]) << 40) |
698 (((uint64_t)cpdu.cbu_bridgeaddr[1]) << 32) |
699 (((uint64_t)cpdu.cbu_bridgeaddr[2]) << 24) |
700 (((uint64_t)cpdu.cbu_bridgeaddr[3]) << 16) |
701 (((uint64_t)cpdu.cbu_bridgeaddr[4]) << 8) |
702 (((uint64_t)cpdu.cbu_bridgeaddr[5]) << 0);
704 cu.cu_root_path_cost = ntohl(cpdu.cbu_rootpathcost);
705 cu.cu_message_age = ntohs(cpdu.cbu_messageage);
706 cu.cu_max_age = ntohs(cpdu.cbu_maxage);
707 cu.cu_hello_time = ntohs(cpdu.cbu_hellotime);
708 cu.cu_forward_delay = ntohs(cpdu.cbu_forwarddelay);
709 cu.cu_port_id = ntohs(cpdu.cbu_portid);
710 cu.cu_message_type = cpdu.cbu_bpdutype;
711 cu.cu_topology_change_acknowledgment =
712 (cpdu.cbu_flags & BSTP_FLAG_TCA) ? 1 : 0;
713 cu.cu_topology_change =
714 (cpdu.cbu_flags & BSTP_FLAG_TC) ? 1 : 0;
715 bstp_received_config_bpdu(sc, bif, &cu);
726 bstp_received_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
727 struct bstp_config_unit *cu)
731 root = bstp_root_bridge(sc);
733 if (bif->bif_state != BSTP_IFSTATE_DISABLED) {
734 if (bstp_supersedes_port_info(sc, bif, cu)) {
735 bstp_record_config_information(sc, bif, cu);
736 bstp_configuration_update(sc);
737 bstp_port_state_selection(sc);
739 if ((bstp_root_bridge(sc) == 0) && root) {
741 * We continuously transmit hello's if
742 * link1 is set (topology change bit will
743 * be zero so they shouldn't propagate).
745 if ((sc->sc_ifp->if_flags & IFF_LINK1) == 0)
746 bstp_timer_stop(&sc->sc_hello_timer);
748 if (sc->sc_topology_change_detected) {
750 &sc->sc_topology_change_timer);
751 bstp_transmit_tcn(sc);
752 bstp_timer_start(&sc->sc_tcn_timer, 0);
756 if (bif->bif_info == sc->sc_root_port) {
757 bstp_record_config_timeout_values(sc, cu);
758 bstp_config_bpdu_generation(sc);
760 if (cu->cu_topology_change_acknowledgment)
761 bstp_topology_change_acknowledged(sc);
763 } else if (bstp_designated_port(sc, bif))
764 bstp_transmit_config(sc, bif);
769 bstp_received_tcn_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif,
770 struct bstp_tcn_unit *tcn)
772 if (bif->bif_state != BSTP_IFSTATE_DISABLED &&
773 bstp_designated_port(sc, bif)) {
774 bstp_topology_change_detection(sc);
775 bstp_acknowledge_topology_change(sc, bif);
780 * link1 forces continuous hello's (the bridge interface must be cycled
781 * to start them up), so keep the timer hot if that is the case, otherwise
782 * only send HELLO's if we are the root.
785 bstp_hello_timer_expiry(struct bridge_softc *sc)
787 bstp_config_bpdu_generation(sc);
789 if ((sc->sc_ifp->if_flags & IFF_LINK1) || bstp_root_bridge(sc))
790 bstp_timer_start(&sc->sc_hello_timer, 0);
794 bstp_message_age_timer_expiry(struct bridge_softc *sc,
795 struct bridge_iflist *bif)
799 root = bstp_root_bridge(sc);
800 bstp_become_designated_port(sc, bif);
801 bstp_configuration_update(sc);
802 bstp_port_state_selection(sc);
805 * If we've become the root and were not the root before
806 * we have some cleanup to do. This also occurs if we
807 * wind up being completely isolated.
809 if ((bstp_root_bridge(sc)) && (root == 0)) {
810 sc->sc_max_age = sc->sc_bridge_max_age;
811 sc->sc_hello_time = sc->sc_bridge_hello_time;
812 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
814 bstp_topology_change_detection(sc);
815 bstp_timer_stop(&sc->sc_tcn_timer);
816 bstp_config_bpdu_generation(sc);
817 bstp_timer_start(&sc->sc_hello_timer, 0);
822 bstp_forward_delay_timer_expiry(struct bridge_softc *sc,
823 struct bridge_iflist *bif)
825 if (bif->bif_state == BSTP_IFSTATE_LISTENING) {
826 bstp_set_port_state(bif, BSTP_IFSTATE_LEARNING);
827 bstp_timer_start(&bif->bif_forward_delay_timer, 0);
828 } else if (bif->bif_state == BSTP_IFSTATE_LEARNING) {
829 bstp_set_port_state(bif, BSTP_IFSTATE_FORWARDING);
830 if (bstp_designated_for_some_port(sc) &&
831 bif->bif_change_detection_enabled)
832 bstp_topology_change_detection(sc);
837 bstp_designated_for_some_port(struct bridge_softc *sc)
840 struct bridge_iflist *bif;
842 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
843 if ((bif->bif_flags & IFBIF_STP) == 0)
845 if (bif->bif_designated_bridge == sc->sc_bridge_id)
852 bstp_tcn_timer_expiry(struct bridge_softc *sc)
854 bstp_transmit_tcn(sc);
855 bstp_timer_start(&sc->sc_tcn_timer, 0);
859 bstp_topology_change_timer_expiry(struct bridge_softc *sc)
861 sc->sc_topology_change_detected = 0;
862 sc->sc_topology_change = 0;
866 bstp_hold_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif)
868 if (bif->bif_config_pending)
869 bstp_transmit_config(sc, bif);
873 * If no traffic received directly on this port for the specified
874 * period with link1 set we go into a special blocking mode to
875 * fail-over traffic to another port.
878 bstp_link1_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif)
880 if (sc->sc_ifp->if_flags & IFF_LINK1)
881 bstp_make_l1blocking(sc, bif);
885 bstp_addr_cmp(const uint8_t *a, const uint8_t *b)
889 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
890 d = ((int)a[i]) - ((int)b[i]);
897 bstp_initialization(struct bridge_softc *sc)
899 struct bridge_iflist *bif, *mif, *nbif;
902 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
905 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
906 if ((bif->bif_flags & IFBIF_STP) == 0)
908 if (bif->bif_ifp->if_type != IFT_ETHER)
910 bif->bif_port_id = (bif->bif_priority << 8) |
911 (bif->bif_ifp->if_index & 0xff);
917 if (bstp_addr_cmp(IF_LLADDR(bif->bif_ifp),
918 IF_LLADDR(mif->bif_ifp)) < 0) {
928 e_addr = IF_LLADDR(mif->bif_ifp);
930 (((uint64_t)sc->sc_bridge_priority) << 48) |
931 (((uint64_t)e_addr[0]) << 40) |
932 (((uint64_t)e_addr[1]) << 32) |
933 (((uint64_t)e_addr[2]) << 24) |
934 (((uint64_t)e_addr[3]) << 16) |
935 (((uint64_t)e_addr[4]) << 8) |
936 (((uint64_t)e_addr[5]));
938 sc->sc_designated_root = sc->sc_bridge_id;
939 sc->sc_root_path_cost = 0;
940 sc->sc_root_port = NULL;
942 sc->sc_max_age = sc->sc_bridge_max_age;
943 sc->sc_hello_time = sc->sc_bridge_hello_time;
944 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
945 sc->sc_topology_change_detected = 0;
946 sc->sc_topology_change = 0;
947 bstp_timer_stop(&sc->sc_tcn_timer);
948 bstp_timer_stop(&sc->sc_topology_change_timer);
950 if (callout_pending(&sc->sc_bstpcallout) == 0)
951 callout_reset(&sc->sc_bstpcallout, hz,
954 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
955 if (sc->sc_ifp->if_flags & IFF_LINK1)
956 bstp_timer_start(&bif->bif_link1_timer, 0);
957 if (bif->bif_flags & IFBIF_STP)
958 bstp_ifupdstatus(sc, bif);
960 bstp_disable_port(sc, bif);
962 if (nbif != NULL && !nbif->bif_onlist) {
963 KKASSERT(bif->bif_onlist);
964 nbif = LIST_NEXT(bif, bif_next);
968 bstp_port_state_selection(sc);
969 bstp_config_bpdu_generation(sc);
970 bstp_timer_start(&sc->sc_hello_timer, 0);
974 bstp_stop(struct bridge_softc *sc)
976 struct bridge_iflist *bif;
977 struct lwkt_msg *lmsg;
979 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
981 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
982 bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED);
983 bstp_timer_stop(&bif->bif_hold_timer);
984 bstp_timer_stop(&bif->bif_message_age_timer);
985 bstp_timer_stop(&bif->bif_forward_delay_timer);
986 bstp_timer_stop(&bif->bif_link1_timer);
989 callout_stop(&sc->sc_bstpcallout);
991 bstp_timer_stop(&sc->sc_topology_change_timer);
992 bstp_timer_stop(&sc->sc_tcn_timer);
993 bstp_timer_stop(&sc->sc_hello_timer);
996 lmsg = &sc->sc_bstptimemsg.lmsg;
997 if ((lmsg->ms_flags & MSGF_DONE) == 0) {
998 /* Pending to be processed; drop it */
1005 bstp_initialize_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1007 bstp_become_designated_port(sc, bif);
1008 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING);
1009 bif->bif_topology_change_acknowledge = 0;
1010 bif->bif_config_pending = 0;
1011 bif->bif_change_detection_enabled = 1;
1012 bstp_timer_stop(&bif->bif_message_age_timer);
1013 bstp_timer_stop(&bif->bif_forward_delay_timer);
1014 bstp_timer_stop(&bif->bif_hold_timer);
1015 bstp_timer_stop(&bif->bif_link1_timer);
1019 bstp_enable_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1021 bstp_initialize_port(sc, bif);
1022 if (sc->sc_ifp->if_flags & IFF_LINK1)
1023 bstp_timer_start(&bif->bif_link1_timer, 0);
1024 bstp_port_state_selection(sc);
1028 bstp_disable_port(struct bridge_softc *sc, struct bridge_iflist *bif)
1032 root = bstp_root_bridge(sc);
1033 bstp_become_designated_port(sc, bif);
1034 bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED);
1035 bif->bif_topology_change_acknowledge = 0;
1036 bif->bif_config_pending = 0;
1037 bstp_timer_stop(&bif->bif_message_age_timer);
1038 bstp_timer_stop(&bif->bif_forward_delay_timer);
1039 bstp_timer_stop(&bif->bif_link1_timer);
1040 bstp_configuration_update(sc);
1041 bstp_port_state_selection(sc);
1042 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN);
1044 if (bstp_root_bridge(sc) && (root == 0)) {
1045 sc->sc_max_age = sc->sc_bridge_max_age;
1046 sc->sc_hello_time = sc->sc_bridge_hello_time;
1047 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
1049 bstp_topology_change_detection(sc);
1050 bstp_timer_stop(&sc->sc_tcn_timer);
1051 bstp_config_bpdu_generation(sc);
1052 bstp_timer_start(&sc->sc_hello_timer, 0);
1058 bstp_set_bridge_priority(struct bridge_softc *sc, uint64_t new_bridge_id)
1060 struct bridge_iflist *bif;
1063 root = bstp_root_bridge(sc);
1065 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1066 if ((bif->bif_flags & IFBIF_STP) == 0)
1068 if (bstp_designated_port(sc, bif))
1069 bif->bif_designated_bridge = new_bridge_id;
1072 sc->sc_bridge_id = new_bridge_id;
1074 bstp_configuration_update(sc);
1075 bstp_port_state_selection(sc);
1077 if (bstp_root_bridge(sc) && (root == 0)) {
1078 sc->sc_max_age = sc->sc_bridge_max_age;
1079 sc->sc_hello_time = sc->sc_bridge_hello_time;
1080 sc->sc_forward_delay = sc->sc_bridge_forward_delay;
1082 bstp_topology_change_detection(sc);
1083 bstp_timer_stop(&sc->sc_tcn_timer);
1084 bstp_config_bpdu_generation(sc);
1085 bstp_timer_start(&sc->sc_hello_timer, 0);
1090 bstp_set_port_priority(struct bridge_softc *sc, struct bridge_iflist *bif,
1091 uint16_t new_port_id)
1093 if (bstp_designated_port(sc, bif))
1094 bif->bif_designated_port = new_port_id;
1096 bif->bif_port_id = new_port_id;
1098 if ((sc->sc_bridge_id == bif->bif_designated_bridge) &&
1099 (bif->bif_port_id < bif->bif_designated_port)) {
1100 bstp_become_designated_port(sc, bif);
1101 bstp_port_state_selection(sc);
1106 bstp_set_path_cost(struct bridge_softc *sc, struct bridge_iflist *bif,
1109 bif->bif_path_cost = path_cost;
1110 bstp_configuration_update(sc);
1111 bstp_port_state_selection(sc);
1115 bstp_enable_change_detection(struct bridge_iflist *bif)
1117 bif->bif_change_detection_enabled = 1;
1121 bstp_disable_change_detection(struct bridge_iflist *bif)
1123 bif->bif_change_detection_enabled = 0;
1125 #endif /* notused */
1128 bstp_linkstate(struct ifnet *ifp, int state)
1130 struct bridge_softc *sc;
1131 struct bridge_iflist *bif;
1133 sc = ifp->if_bridge;
1134 ifnet_serialize_all(sc->sc_ifp);
1137 * bstp_ifupdstatus() may block, but it is the last
1138 * operation of the member iface iteration, so we
1139 * don't need to use LIST_FOREACH_MUTABLE()+bif_onlist
1142 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1143 if ((bif->bif_flags & IFBIF_STP) == 0)
1146 if (bif->bif_ifp == ifp) {
1147 bstp_ifupdstatus(sc, bif);
1151 ifnet_deserialize_all(sc->sc_ifp);
1155 bstp_ifupdstatus(struct bridge_softc *sc, struct bridge_iflist *bif)
1157 struct ifnet *ifp = bif->bif_ifp;
1158 struct ifmediareq ifmr;
1161 bzero((char *)&ifmr, sizeof(ifmr));
1162 ifnet_serialize_all(ifp);
1163 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL);
1164 ifnet_deserialize_all(ifp);
1166 if ((error == 0) && (ifp->if_flags & IFF_UP)) {
1167 if (ifmr.ifm_status & IFM_ACTIVE) {
1168 if (bif->bif_state == BSTP_IFSTATE_DISABLED)
1169 bstp_enable_port(sc, bif);
1172 if (bif->bif_state != BSTP_IFSTATE_DISABLED)
1173 bstp_disable_port(sc, bif);
1178 if (bif->bif_state != BSTP_IFSTATE_DISABLED)
1179 bstp_disable_port(sc, bif);
1183 bstp_tick(void *arg)
1185 struct bridge_softc *sc = arg;
1186 struct lwkt_msg *lmsg;
1188 KKASSERT(mycpuid == BRIDGE_CFGCPU);
1192 if (callout_pending(&sc->sc_bstpcallout) ||
1193 !callout_active(&sc->sc_bstpcallout)) {
1197 callout_deactivate(&sc->sc_bstpcallout);
1199 lmsg = &sc->sc_bstptimemsg.lmsg;
1200 KKASSERT(lmsg->ms_flags & MSGF_DONE);
1201 lwkt_sendmsg(BRIDGE_CFGPORT, lmsg);
1207 bstp_tick_handler(netmsg_t msg)
1209 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
1210 struct bridge_iflist *bif;
1212 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
1215 lwkt_replymsg(&msg->lmsg, 0);
1218 ifnet_serialize_all(sc->sc_ifp);
1222 * We don't need to worry that member iface is ripped
1223 * from the per-cpu list during the blocking operation
1224 * in the loop body, since deletion is serialized by
1228 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1229 if ((bif->bif_flags & IFBIF_STP) == 0)
1232 * XXX This can cause a lag in "link does away"
1233 * XXX and "spanning tree gets updated". We need
1234 * XXX come sort of callback from the link state
1235 * XXX update code to kick spanning tree.
1236 * XXX --thorpej@NetBSD.org
1238 bstp_ifupdstatus(sc, bif);
1241 if (bstp_timer_expired(&sc->sc_hello_timer, sc->sc_hello_time))
1242 bstp_hello_timer_expiry(sc);
1244 if (bstp_timer_expired(&sc->sc_tcn_timer, sc->sc_bridge_hello_time))
1245 bstp_tcn_timer_expiry(sc);
1247 if (bstp_timer_expired(&sc->sc_topology_change_timer,
1248 sc->sc_topology_change_time))
1249 bstp_topology_change_timer_expiry(sc);
1251 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1252 if ((bif->bif_flags & IFBIF_STP) == 0)
1254 if (bstp_timer_expired(&bif->bif_message_age_timer,
1256 bstp_message_age_timer_expiry(sc, bif);
1259 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1260 if ((bif->bif_flags & IFBIF_STP) == 0)
1262 if (bstp_timer_expired(&bif->bif_forward_delay_timer,
1263 sc->sc_forward_delay))
1264 bstp_forward_delay_timer_expiry(sc, bif);
1266 if (bstp_timer_expired(&bif->bif_hold_timer,
1268 bstp_hold_timer_expiry(sc, bif);
1270 if (bstp_timer_expired(&bif->bif_link1_timer,
1271 sc->sc_hello_time * 10))
1272 bstp_link1_timer_expiry(sc, bif);
1275 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1276 callout_reset(&sc->sc_bstpcallout, hz, bstp_tick, sc);
1278 ifnet_deserialize_all(sc->sc_ifp);
1282 bstp_timer_start(struct bridge_timer *t, uint16_t v)
1289 bstp_timer_stop(struct bridge_timer *t)
1296 bstp_timer_expired(struct bridge_timer *t, uint16_t v)
1300 t->value += BSTP_TICK_VAL;
1301 if (t->value >= v) {