1 /* $NetBSD: bridgestp.c,v 1.5 2003/11/28 08:56:48 keihan Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
7 * Copyright (c) 2006 Andrew Thompson (thompsa@FreeBSD.org)
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
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
35 * Implementation of the spanning tree protocol as defined in
36 * ISO/IEC 802.1D-2004, June 9, 2004.
39 #include <sys/cdefs.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/callout.h>
48 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/taskqueue.h>
55 #include <net/if_var.h>
56 #include <net/if_private.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/if_llc.h>
60 #include <net/if_media.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/if_ether.h>
67 #include <net/bridgestp.h>
69 #ifdef BRIDGESTP_DEBUG
70 #define DPRINTF(fmt, arg...) printf("bstp: " fmt, ##arg)
72 #define DPRINTF(fmt, arg...) (void)0
75 #define PV2ADDR(pv, eaddr) do { \
76 eaddr[0] = pv >> 40; \
77 eaddr[1] = pv >> 32; \
78 eaddr[2] = pv >> 24; \
79 eaddr[3] = pv >> 16; \
88 const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
90 LIST_HEAD(, bstp_state) bstp_list;
91 static struct mtx bstp_list_mtx;
93 static void bstp_transmit(struct bstp_state *, struct bstp_port *);
94 static void bstp_transmit_bpdu(struct bstp_state *, struct bstp_port *);
95 static void bstp_transmit_tcn(struct bstp_state *, struct bstp_port *);
96 static void bstp_decode_bpdu(struct bstp_port *, struct bstp_cbpdu *,
97 struct bstp_config_unit *);
98 static void bstp_send_bpdu(struct bstp_state *, struct bstp_port *,
100 static int bstp_pdu_flags(struct bstp_port *);
101 static void bstp_received_stp(struct bstp_state *, struct bstp_port *,
102 struct mbuf **, struct bstp_tbpdu *);
103 static void bstp_received_rstp(struct bstp_state *, struct bstp_port *,
104 struct mbuf **, struct bstp_tbpdu *);
105 static void bstp_received_tcn(struct bstp_state *, struct bstp_port *,
106 struct bstp_tcn_unit *);
107 static void bstp_received_bpdu(struct bstp_state *, struct bstp_port *,
108 struct bstp_config_unit *);
109 static int bstp_pdu_rcvtype(struct bstp_port *, struct bstp_config_unit *);
110 static int bstp_pdu_bettersame(struct bstp_port *, int);
111 static int bstp_info_cmp(struct bstp_pri_vector *,
112 struct bstp_pri_vector *);
113 static int bstp_info_superior(struct bstp_pri_vector *,
114 struct bstp_pri_vector *);
115 static void bstp_assign_roles(struct bstp_state *);
116 static void bstp_update_roles(struct bstp_state *, struct bstp_port *);
117 static void bstp_update_state(struct bstp_state *, struct bstp_port *);
118 static void bstp_update_tc(struct bstp_port *);
119 static void bstp_update_info(struct bstp_port *);
120 static void bstp_set_other_tcprop(struct bstp_port *);
121 static void bstp_set_all_reroot(struct bstp_state *);
122 static void bstp_set_all_sync(struct bstp_state *);
123 static void bstp_set_port_state(struct bstp_port *, int);
124 static void bstp_set_port_role(struct bstp_port *, int);
125 static void bstp_set_port_proto(struct bstp_port *, int);
126 static void bstp_set_port_tc(struct bstp_port *, int);
127 static void bstp_set_timer_tc(struct bstp_port *);
128 static void bstp_set_timer_msgage(struct bstp_port *);
129 static int bstp_rerooted(struct bstp_state *, struct bstp_port *);
130 static uint32_t bstp_calc_path_cost(struct bstp_port *);
131 static void bstp_notify_state(void *, int);
132 static void bstp_notify_rtage(void *, int);
133 static void bstp_ifupdstatus(void *, int);
134 static void bstp_enable_port(struct bstp_state *, struct bstp_port *);
135 static void bstp_disable_port(struct bstp_state *, struct bstp_port *);
136 static void bstp_tick(void *);
137 static void bstp_timer_start(struct bstp_timer *, uint16_t);
138 static void bstp_timer_stop(struct bstp_timer *);
139 static void bstp_timer_latch(struct bstp_timer *);
140 static int bstp_timer_dectest(struct bstp_timer *);
141 static void bstp_hello_timer_expiry(struct bstp_state *,
143 static void bstp_message_age_expiry(struct bstp_state *,
145 static void bstp_migrate_delay_expiry(struct bstp_state *,
147 static void bstp_edge_delay_expiry(struct bstp_state *,
149 static int bstp_addr_cmp(const uint8_t *, const uint8_t *);
150 static int bstp_same_bridgeid(uint64_t, uint64_t);
151 static void bstp_reinit(struct bstp_state *);
154 bstp_transmit(struct bstp_state *bs, struct bstp_port *bp)
158 if (bs->bs_running == 0)
162 * a PDU can only be sent if we have tx quota left and the
163 * hello timer is running.
165 if (bp->bp_hello_timer.active == 0) {
166 /* Test if it needs to be reset */
167 bstp_hello_timer_expiry(bs, bp);
170 if (bp->bp_txcount > bs->bs_txholdcount)
171 /* Ran out of karma */
174 if (bp->bp_protover == BSTP_PROTO_RSTP) {
175 bstp_transmit_bpdu(bs, bp);
178 switch (bp->bp_role) {
179 case BSTP_ROLE_DESIGNATED:
180 bstp_transmit_bpdu(bs, bp);
185 bstp_transmit_tcn(bs, bp);
189 bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime);
190 bp->bp_flags &= ~BSTP_PORT_NEWINFO;
194 bstp_transmit_bpdu(struct bstp_state *bs, struct bstp_port *bp)
196 struct bstp_cbpdu bpdu;
198 BSTP_LOCK_ASSERT(bs);
200 bpdu.cbu_rootpri = htons(bp->bp_desg_pv.pv_root_id >> 48);
201 PV2ADDR(bp->bp_desg_pv.pv_root_id, bpdu.cbu_rootaddr);
203 bpdu.cbu_rootpathcost = htonl(bp->bp_desg_pv.pv_cost);
205 bpdu.cbu_bridgepri = htons(bp->bp_desg_pv.pv_dbridge_id >> 48);
206 PV2ADDR(bp->bp_desg_pv.pv_dbridge_id, bpdu.cbu_bridgeaddr);
208 bpdu.cbu_portid = htons(bp->bp_port_id);
209 bpdu.cbu_messageage = htons(bp->bp_desg_msg_age);
210 bpdu.cbu_maxage = htons(bp->bp_desg_max_age);
211 bpdu.cbu_hellotime = htons(bp->bp_desg_htime);
212 bpdu.cbu_forwarddelay = htons(bp->bp_desg_fdelay);
214 bpdu.cbu_flags = bstp_pdu_flags(bp);
216 switch (bp->bp_protover) {
218 bpdu.cbu_bpdutype = BSTP_MSGTYPE_CFG;
221 case BSTP_PROTO_RSTP:
222 bpdu.cbu_bpdutype = BSTP_MSGTYPE_RSTP;
226 bstp_send_bpdu(bs, bp, &bpdu);
230 bstp_transmit_tcn(struct bstp_state *bs, struct bstp_port *bp)
232 struct bstp_tbpdu bpdu;
233 struct ifnet *ifp = bp->bp_ifp;
234 struct ether_header *eh;
237 KASSERT(bp == bs->bs_root_port, ("%s: bad root port\n", __func__));
239 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
242 m = m_gethdr(M_NOWAIT, MT_DATA);
246 m->m_pkthdr.rcvif = ifp;
247 m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
248 m->m_len = m->m_pkthdr.len;
250 eh = mtod(m, struct ether_header *);
252 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
253 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
254 eh->ether_type = htons(sizeof(bpdu));
256 bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP;
257 bpdu.tbu_ctl = LLC_UI;
258 bpdu.tbu_protoid = 0;
259 bpdu.tbu_protover = 0;
260 bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN;
262 memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
265 ifp->if_transmit(ifp, m);
269 bstp_decode_bpdu(struct bstp_port *bp, struct bstp_cbpdu *cpdu,
270 struct bstp_config_unit *cu)
274 cu->cu_pv.pv_root_id =
275 (((uint64_t)ntohs(cpdu->cbu_rootpri)) << 48) |
276 (((uint64_t)cpdu->cbu_rootaddr[0]) << 40) |
277 (((uint64_t)cpdu->cbu_rootaddr[1]) << 32) |
278 (((uint64_t)cpdu->cbu_rootaddr[2]) << 24) |
279 (((uint64_t)cpdu->cbu_rootaddr[3]) << 16) |
280 (((uint64_t)cpdu->cbu_rootaddr[4]) << 8) |
281 (((uint64_t)cpdu->cbu_rootaddr[5]) << 0);
283 cu->cu_pv.pv_dbridge_id =
284 (((uint64_t)ntohs(cpdu->cbu_bridgepri)) << 48) |
285 (((uint64_t)cpdu->cbu_bridgeaddr[0]) << 40) |
286 (((uint64_t)cpdu->cbu_bridgeaddr[1]) << 32) |
287 (((uint64_t)cpdu->cbu_bridgeaddr[2]) << 24) |
288 (((uint64_t)cpdu->cbu_bridgeaddr[3]) << 16) |
289 (((uint64_t)cpdu->cbu_bridgeaddr[4]) << 8) |
290 (((uint64_t)cpdu->cbu_bridgeaddr[5]) << 0);
292 cu->cu_pv.pv_cost = ntohl(cpdu->cbu_rootpathcost);
293 cu->cu_message_age = ntohs(cpdu->cbu_messageage);
294 cu->cu_max_age = ntohs(cpdu->cbu_maxage);
295 cu->cu_hello_time = ntohs(cpdu->cbu_hellotime);
296 cu->cu_forward_delay = ntohs(cpdu->cbu_forwarddelay);
297 cu->cu_pv.pv_dport_id = ntohs(cpdu->cbu_portid);
298 cu->cu_pv.pv_port_id = bp->bp_port_id;
299 cu->cu_message_type = cpdu->cbu_bpdutype;
301 /* Strip off unused flags in STP mode */
302 flags = cpdu->cbu_flags;
303 switch (cpdu->cbu_protover) {
305 flags &= BSTP_PDU_STPMASK;
306 /* A STP BPDU explicitly conveys a Designated Port */
307 cu->cu_role = BSTP_ROLE_DESIGNATED;
310 case BSTP_PROTO_RSTP:
311 flags &= BSTP_PDU_RSTPMASK;
315 cu->cu_topology_change_ack =
316 (flags & BSTP_PDU_F_TCA) ? 1 : 0;
318 (flags & BSTP_PDU_F_P) ? 1 : 0;
320 (flags & BSTP_PDU_F_A) ? 1 : 0;
322 (flags & BSTP_PDU_F_L) ? 1 : 0;
324 (flags & BSTP_PDU_F_F) ? 1 : 0;
325 cu->cu_topology_change =
326 (flags & BSTP_PDU_F_TC) ? 1 : 0;
328 switch ((flags & BSTP_PDU_PRMASK) >> BSTP_PDU_PRSHIFT) {
329 case BSTP_PDU_F_ROOT:
330 cu->cu_role = BSTP_ROLE_ROOT;
333 cu->cu_role = BSTP_ROLE_ALTERNATE;
335 case BSTP_PDU_F_DESG:
336 cu->cu_role = BSTP_ROLE_DESIGNATED;
342 bstp_send_bpdu(struct bstp_state *bs, struct bstp_port *bp,
343 struct bstp_cbpdu *bpdu)
347 struct ether_header *eh;
349 BSTP_LOCK_ASSERT(bs);
354 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
357 m = m_gethdr(M_NOWAIT, MT_DATA);
361 eh = mtod(m, struct ether_header *);
363 bpdu->cbu_ssap = bpdu->cbu_dsap = LLC_8021D_LSAP;
364 bpdu->cbu_ctl = LLC_UI;
365 bpdu->cbu_protoid = htons(BSTP_PROTO_ID);
367 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
368 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
370 switch (bpdu->cbu_bpdutype) {
371 case BSTP_MSGTYPE_CFG:
372 bpdu->cbu_protover = BSTP_PROTO_STP;
373 m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_STP_LEN;
374 eh->ether_type = htons(BSTP_BPDU_STP_LEN);
375 memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu,
379 case BSTP_MSGTYPE_RSTP:
380 bpdu->cbu_protover = BSTP_PROTO_RSTP;
381 bpdu->cbu_versionlen = htons(0);
382 m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_RSTP_LEN;
383 eh->ether_type = htons(BSTP_BPDU_RSTP_LEN);
384 memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu,
389 panic("not implemented");
391 m->m_pkthdr.rcvif = ifp;
392 m->m_len = m->m_pkthdr.len;
395 ifp->if_transmit(ifp, m);
399 bstp_pdu_flags(struct bstp_port *bp)
403 if (bp->bp_proposing && bp->bp_state != BSTP_IFSTATE_FORWARDING)
404 flags |= BSTP_PDU_F_P;
407 flags |= BSTP_PDU_F_A;
409 if (bp->bp_tc_timer.active)
410 flags |= BSTP_PDU_F_TC;
413 flags |= BSTP_PDU_F_TCA;
415 switch (bp->bp_state) {
416 case BSTP_IFSTATE_LEARNING:
417 flags |= BSTP_PDU_F_L;
420 case BSTP_IFSTATE_FORWARDING:
421 flags |= (BSTP_PDU_F_L | BSTP_PDU_F_F);
425 switch (bp->bp_role) {
428 (BSTP_PDU_F_ROOT << BSTP_PDU_PRSHIFT);
431 case BSTP_ROLE_ALTERNATE:
432 case BSTP_ROLE_BACKUP: /* fall through */
434 (BSTP_PDU_F_ALT << BSTP_PDU_PRSHIFT);
437 case BSTP_ROLE_DESIGNATED:
439 (BSTP_PDU_F_DESG << BSTP_PDU_PRSHIFT);
443 /* Strip off unused flags in either mode */
444 switch (bp->bp_protover) {
446 flags &= BSTP_PDU_STPMASK;
448 case BSTP_PROTO_RSTP:
449 flags &= BSTP_PDU_RSTPMASK;
456 bstp_input(struct bstp_port *bp, struct ifnet *ifp, struct mbuf *m)
458 struct bstp_state *bs = bp->bp_bs;
459 struct ether_header *eh;
460 struct bstp_tbpdu tpdu;
463 if (bp->bp_active == 0) {
470 eh = mtod(m, struct ether_header *);
472 len = ntohs(eh->ether_type);
473 if (len < sizeof(tpdu))
476 m_adj(m, ETHER_HDR_LEN);
478 if (m->m_pkthdr.len > len)
479 m_adj(m, len - m->m_pkthdr.len);
480 if (m->m_len < sizeof(tpdu) &&
481 (m = m_pullup(m, sizeof(tpdu))) == NULL)
484 memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu));
486 /* basic packet checks */
487 if (tpdu.tbu_dsap != LLC_8021D_LSAP ||
488 tpdu.tbu_ssap != LLC_8021D_LSAP ||
489 tpdu.tbu_ctl != LLC_UI)
491 if (tpdu.tbu_protoid != BSTP_PROTO_ID)
495 * We can treat later versions of the PDU as the same as the maximum
496 * version we implement. All additional parameters/flags are ignored.
498 if (tpdu.tbu_protover > BSTP_PROTO_MAX)
499 tpdu.tbu_protover = BSTP_PROTO_MAX;
501 if (tpdu.tbu_protover != bp->bp_protover) {
503 * Wait for the migration delay timer to expire before changing
504 * protocol version to avoid flip-flops.
506 if (bp->bp_flags & BSTP_PORT_CANMIGRATE)
507 bstp_set_port_proto(bp, tpdu.tbu_protover);
512 /* Clear operedge upon receiving a PDU on the port */
514 bstp_timer_start(&bp->bp_edge_delay_timer,
515 BSTP_DEFAULT_MIGRATE_DELAY);
517 switch (tpdu.tbu_protover) {
519 bstp_received_stp(bs, bp, &m, &tpdu);
522 case BSTP_PROTO_RSTP:
523 bstp_received_rstp(bs, bp, &m, &tpdu);
533 bstp_received_stp(struct bstp_state *bs, struct bstp_port *bp,
534 struct mbuf **mp, struct bstp_tbpdu *tpdu)
536 struct bstp_cbpdu cpdu;
537 struct bstp_config_unit *cu = &bp->bp_msg_cu;
538 struct bstp_tcn_unit tu;
540 switch (tpdu->tbu_bpdutype) {
541 case BSTP_MSGTYPE_TCN:
542 tu.tu_message_type = tpdu->tbu_bpdutype;
543 bstp_received_tcn(bs, bp, &tu);
545 case BSTP_MSGTYPE_CFG:
546 if ((*mp)->m_len < BSTP_BPDU_STP_LEN &&
547 (*mp = m_pullup(*mp, BSTP_BPDU_STP_LEN)) == NULL)
549 memcpy(&cpdu, mtod(*mp, caddr_t), BSTP_BPDU_STP_LEN);
551 bstp_decode_bpdu(bp, &cpdu, cu);
552 bstp_received_bpdu(bs, bp, cu);
558 bstp_received_rstp(struct bstp_state *bs, struct bstp_port *bp,
559 struct mbuf **mp, struct bstp_tbpdu *tpdu)
561 struct bstp_cbpdu cpdu;
562 struct bstp_config_unit *cu = &bp->bp_msg_cu;
564 if (tpdu->tbu_bpdutype != BSTP_MSGTYPE_RSTP)
567 if ((*mp)->m_len < BSTP_BPDU_RSTP_LEN &&
568 (*mp = m_pullup(*mp, BSTP_BPDU_RSTP_LEN)) == NULL)
570 memcpy(&cpdu, mtod(*mp, caddr_t), BSTP_BPDU_RSTP_LEN);
572 bstp_decode_bpdu(bp, &cpdu, cu);
573 bstp_received_bpdu(bs, bp, cu);
577 bstp_received_tcn(struct bstp_state *bs, struct bstp_port *bp,
578 struct bstp_tcn_unit *tcn)
585 bstp_received_bpdu(struct bstp_state *bs, struct bstp_port *bp,
586 struct bstp_config_unit *cu)
590 BSTP_LOCK_ASSERT(bs);
592 /* We need to have transitioned to INFO_MINE before proceeding */
593 switch (bp->bp_infois) {
594 case BSTP_INFO_DISABLED:
600 if (cu->cu_message_age >= cu->cu_max_age) {
603 if (cu->cu_max_age < BSTP_MIN_MAX_AGE ||
604 cu->cu_max_age > BSTP_MAX_MAX_AGE) {
607 if (cu->cu_forward_delay < BSTP_MIN_FORWARD_DELAY ||
608 cu->cu_forward_delay > BSTP_MAX_FORWARD_DELAY) {
611 if (cu->cu_hello_time < BSTP_MIN_HELLO_TIME ||
612 cu->cu_hello_time > BSTP_MAX_HELLO_TIME) {
616 type = bstp_pdu_rcvtype(bp, cu);
619 case BSTP_PDU_SUPERIOR:
620 bs->bs_allsynced = 0;
622 bp->bp_proposing = 0;
624 if (cu->cu_proposal && cu->cu_forwarding == 0)
626 if (cu->cu_topology_change)
628 if (cu->cu_topology_change_ack)
632 !bstp_pdu_bettersame(bp, BSTP_INFO_RECEIVED))
635 /* copy the received priority and timers to the port */
636 bp->bp_port_pv = cu->cu_pv;
637 bp->bp_port_msg_age = cu->cu_message_age;
638 bp->bp_port_max_age = cu->cu_max_age;
639 bp->bp_port_fdelay = cu->cu_forward_delay;
641 (cu->cu_hello_time > BSTP_MIN_HELLO_TIME ?
642 cu->cu_hello_time : BSTP_MIN_HELLO_TIME);
644 /* set expiry for the new info */
645 bstp_set_timer_msgage(bp);
647 bp->bp_infois = BSTP_INFO_RECEIVED;
648 bstp_assign_roles(bs);
651 case BSTP_PDU_REPEATED:
652 if (cu->cu_proposal && cu->cu_forwarding == 0)
654 if (cu->cu_topology_change)
656 if (cu->cu_topology_change_ack)
659 /* rearm the age timer */
660 bstp_set_timer_msgage(bp);
663 case BSTP_PDU_INFERIOR:
664 if (cu->cu_learning) {
666 bp->bp_proposing = 0;
670 case BSTP_PDU_INFERIORALT:
672 * only point to point links are allowed fast
673 * transitions to forwarding.
675 if (cu->cu_agree && bp->bp_ptp_link) {
677 bp->bp_proposing = 0;
681 if (cu->cu_topology_change)
683 if (cu->cu_topology_change_ack)
688 return; /* do nothing */
690 /* update the state machines with the new data */
691 bstp_update_state(bs, bp);
695 bstp_pdu_rcvtype(struct bstp_port *bp, struct bstp_config_unit *cu)
699 /* default return type */
700 type = BSTP_PDU_OTHER;
702 switch (cu->cu_role) {
703 case BSTP_ROLE_DESIGNATED:
704 if (bstp_info_superior(&bp->bp_port_pv, &cu->cu_pv))
705 /* bpdu priority is superior */
706 type = BSTP_PDU_SUPERIOR;
707 else if (bstp_info_cmp(&bp->bp_port_pv, &cu->cu_pv) ==
709 if (bp->bp_port_msg_age != cu->cu_message_age ||
710 bp->bp_port_max_age != cu->cu_max_age ||
711 bp->bp_port_fdelay != cu->cu_forward_delay ||
712 bp->bp_port_htime != cu->cu_hello_time)
713 /* bpdu priority is equal and timers differ */
714 type = BSTP_PDU_SUPERIOR;
717 type = BSTP_PDU_REPEATED;
719 /* bpdu priority is worse */
720 type = BSTP_PDU_INFERIOR;
725 case BSTP_ROLE_ALTERNATE:
726 case BSTP_ROLE_BACKUP:
727 if (bstp_info_cmp(&bp->bp_port_pv, &cu->cu_pv) <= INFO_SAME)
729 * not a designated port and priority is the same or
732 type = BSTP_PDU_INFERIORALT;
740 bstp_pdu_bettersame(struct bstp_port *bp, int newinfo)
742 if (newinfo == BSTP_INFO_RECEIVED &&
743 bp->bp_infois == BSTP_INFO_RECEIVED &&
744 bstp_info_cmp(&bp->bp_port_pv, &bp->bp_msg_cu.cu_pv) >= INFO_SAME)
747 if (newinfo == BSTP_INFO_MINE &&
748 bp->bp_infois == BSTP_INFO_MINE &&
749 bstp_info_cmp(&bp->bp_port_pv, &bp->bp_desg_pv) >= INFO_SAME)
756 bstp_info_cmp(struct bstp_pri_vector *pv,
757 struct bstp_pri_vector *cpv)
759 if (cpv->pv_root_id < pv->pv_root_id)
760 return (INFO_BETTER);
761 if (cpv->pv_root_id > pv->pv_root_id)
764 if (cpv->pv_cost < pv->pv_cost)
765 return (INFO_BETTER);
766 if (cpv->pv_cost > pv->pv_cost)
769 if (cpv->pv_dbridge_id < pv->pv_dbridge_id)
770 return (INFO_BETTER);
771 if (cpv->pv_dbridge_id > pv->pv_dbridge_id)
774 if (cpv->pv_dport_id < pv->pv_dport_id)
775 return (INFO_BETTER);
776 if (cpv->pv_dport_id > pv->pv_dport_id)
783 * This message priority vector is superior to the port priority vector and
784 * will replace it if, and only if, the message priority vector is better than
785 * the port priority vector, or the message has been transmitted from the same
786 * designated bridge and designated port as the port priority vector.
789 bstp_info_superior(struct bstp_pri_vector *pv,
790 struct bstp_pri_vector *cpv)
792 if (bstp_info_cmp(pv, cpv) == INFO_BETTER ||
793 (bstp_same_bridgeid(pv->pv_dbridge_id, cpv->pv_dbridge_id) &&
794 (cpv->pv_dport_id & 0xfff) == (pv->pv_dport_id & 0xfff)))
800 bstp_assign_roles(struct bstp_state *bs)
802 struct bstp_port *bp, *rbp = NULL;
803 struct bstp_pri_vector pv;
805 /* default to our priority vector */
806 bs->bs_root_pv = bs->bs_bridge_pv;
807 bs->bs_root_msg_age = 0;
808 bs->bs_root_max_age = bs->bs_bridge_max_age;
809 bs->bs_root_fdelay = bs->bs_bridge_fdelay;
810 bs->bs_root_htime = bs->bs_bridge_htime;
811 bs->bs_root_port = NULL;
813 /* check if any received info supersedes us */
814 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
815 if (bp->bp_infois != BSTP_INFO_RECEIVED)
819 pv.pv_cost += bp->bp_path_cost;
822 * The root priority vector is the best of the set comprising
823 * the bridge priority vector plus all root path priority
824 * vectors whose bridge address is not equal to us.
826 if (bstp_same_bridgeid(pv.pv_dbridge_id,
827 bs->bs_bridge_pv.pv_dbridge_id) == 0 &&
828 bstp_info_cmp(&bs->bs_root_pv, &pv) == INFO_BETTER) {
829 /* the port vector replaces the root */
831 bs->bs_root_msg_age = bp->bp_port_msg_age +
832 BSTP_MESSAGE_AGE_INCR;
833 bs->bs_root_max_age = bp->bp_port_max_age;
834 bs->bs_root_fdelay = bp->bp_port_fdelay;
835 bs->bs_root_htime = bp->bp_port_htime;
840 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
841 /* calculate the port designated vector */
842 bp->bp_desg_pv.pv_root_id = bs->bs_root_pv.pv_root_id;
843 bp->bp_desg_pv.pv_cost = bs->bs_root_pv.pv_cost;
844 bp->bp_desg_pv.pv_dbridge_id = bs->bs_bridge_pv.pv_dbridge_id;
845 bp->bp_desg_pv.pv_dport_id = bp->bp_port_id;
846 bp->bp_desg_pv.pv_port_id = bp->bp_port_id;
848 /* calculate designated times */
849 bp->bp_desg_msg_age = bs->bs_root_msg_age;
850 bp->bp_desg_max_age = bs->bs_root_max_age;
851 bp->bp_desg_fdelay = bs->bs_root_fdelay;
852 bp->bp_desg_htime = bs->bs_bridge_htime;
854 switch (bp->bp_infois) {
855 case BSTP_INFO_DISABLED:
856 bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
860 bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
861 bstp_update_info(bp);
865 bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
866 /* update the port info if stale */
867 if (bstp_info_cmp(&bp->bp_port_pv,
868 &bp->bp_desg_pv) != INFO_SAME ||
870 (bp->bp_port_msg_age != rbp->bp_port_msg_age ||
871 bp->bp_port_max_age != rbp->bp_port_max_age ||
872 bp->bp_port_fdelay != rbp->bp_port_fdelay ||
873 bp->bp_port_htime != rbp->bp_port_htime)))
874 bstp_update_info(bp);
877 case BSTP_INFO_RECEIVED:
880 * root priority is derived from this
881 * port, make it the root port.
883 bstp_set_port_role(bp, BSTP_ROLE_ROOT);
884 bs->bs_root_port = bp;
885 } else if (bstp_info_cmp(&bp->bp_port_pv,
886 &bp->bp_desg_pv) == INFO_BETTER) {
888 * the port priority is lower than the root
891 bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
892 bstp_update_info(bp);
894 if (bstp_same_bridgeid(
895 bp->bp_port_pv.pv_dbridge_id,
896 bs->bs_bridge_pv.pv_dbridge_id)) {
898 * the designated bridge refers to
899 * another port on this bridge.
901 bstp_set_port_role(bp,
905 * the port is an inferior path to the
908 bstp_set_port_role(bp,
909 BSTP_ROLE_ALTERNATE);
918 bstp_update_state(struct bstp_state *bs, struct bstp_port *bp)
920 struct bstp_port *bp2;
923 BSTP_LOCK_ASSERT(bs);
925 /* check if all the ports have syncronised again */
926 if (!bs->bs_allsynced) {
928 LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
929 if (!(bp2->bp_synced ||
930 bp2->bp_role == BSTP_ROLE_ROOT)) {
935 bs->bs_allsynced = synced;
938 bstp_update_roles(bs, bp);
943 bstp_update_roles(struct bstp_state *bs, struct bstp_port *bp)
947 switch (bp->bp_role) {
948 case BSTP_ROLE_DISABLED:
949 /* Clear any flags if set */
950 if (bp->bp_sync || !bp->bp_synced || bp->bp_reroot) {
957 case BSTP_ROLE_ALTERNATE:
958 case BSTP_ROLE_BACKUP:
959 if ((bs->bs_allsynced && !bp->bp_agree) ||
960 (bp->bp_proposed && bp->bp_agree)) {
963 bp->bp_flags |= BSTP_PORT_NEWINFO;
964 DPRINTF("%s -> ALTERNATE_AGREED\n",
965 bp->bp_ifp->if_xname);
968 if (bp->bp_proposed && !bp->bp_agree) {
969 bstp_set_all_sync(bs);
971 DPRINTF("%s -> ALTERNATE_PROPOSED\n",
972 bp->bp_ifp->if_xname);
975 /* Clear any flags if set */
976 if (bp->bp_sync || !bp->bp_synced || bp->bp_reroot) {
980 DPRINTF("%s -> ALTERNATE_PORT\n", bp->bp_ifp->if_xname);
985 if (bp->bp_state != BSTP_IFSTATE_FORWARDING && !bp->bp_reroot) {
986 bstp_set_all_reroot(bs);
987 DPRINTF("%s -> ROOT_REROOT\n", bp->bp_ifp->if_xname);
990 if ((bs->bs_allsynced && !bp->bp_agree) ||
991 (bp->bp_proposed && bp->bp_agree)) {
995 bp->bp_flags |= BSTP_PORT_NEWINFO;
996 DPRINTF("%s -> ROOT_AGREED\n", bp->bp_ifp->if_xname);
999 if (bp->bp_proposed && !bp->bp_agree) {
1000 bstp_set_all_sync(bs);
1001 bp->bp_proposed = 0;
1002 DPRINTF("%s -> ROOT_PROPOSED\n", bp->bp_ifp->if_xname);
1005 if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
1006 (bp->bp_forward_delay_timer.active == 0 ||
1007 (bstp_rerooted(bs, bp) &&
1008 bp->bp_recent_backup_timer.active == 0 &&
1009 bp->bp_protover == BSTP_PROTO_RSTP))) {
1010 switch (bp->bp_state) {
1011 case BSTP_IFSTATE_DISCARDING:
1012 bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING);
1014 case BSTP_IFSTATE_LEARNING:
1015 bstp_set_port_state(bp,
1016 BSTP_IFSTATE_FORWARDING);
1021 if (bp->bp_state == BSTP_IFSTATE_FORWARDING && bp->bp_reroot) {
1023 DPRINTF("%s -> ROOT_REROOTED\n", bp->bp_ifp->if_xname);
1027 case BSTP_ROLE_DESIGNATED:
1028 if (bp->bp_recent_root_timer.active == 0 && bp->bp_reroot) {
1030 DPRINTF("%s -> DESIGNATED_RETIRED\n",
1031 bp->bp_ifp->if_xname);
1034 if ((bp->bp_state == BSTP_IFSTATE_DISCARDING &&
1035 !bp->bp_synced) || (bp->bp_agreed && !bp->bp_synced) ||
1036 (bp->bp_operedge && !bp->bp_synced) ||
1037 (bp->bp_sync && bp->bp_synced)) {
1038 bstp_timer_stop(&bp->bp_recent_root_timer);
1041 DPRINTF("%s -> DESIGNATED_SYNCED\n",
1042 bp->bp_ifp->if_xname);
1045 if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
1046 !bp->bp_agreed && !bp->bp_proposing &&
1048 bp->bp_proposing = 1;
1049 bp->bp_flags |= BSTP_PORT_NEWINFO;
1050 bstp_timer_start(&bp->bp_edge_delay_timer,
1051 (bp->bp_ptp_link ? BSTP_DEFAULT_MIGRATE_DELAY :
1052 bp->bp_desg_max_age));
1053 DPRINTF("%s -> DESIGNATED_PROPOSE\n",
1054 bp->bp_ifp->if_xname);
1057 if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
1058 (bp->bp_forward_delay_timer.active == 0 || bp->bp_agreed ||
1060 (bp->bp_recent_root_timer.active == 0 || !bp->bp_reroot) &&
1063 DPRINTF("%s -> AGREED\n", bp->bp_ifp->if_xname);
1065 * If agreed|operedge then go straight to forwarding,
1066 * otherwise follow discard -> learn -> forward.
1068 if (bp->bp_agreed || bp->bp_operedge ||
1069 bp->bp_state == BSTP_IFSTATE_LEARNING) {
1070 bstp_set_port_state(bp,
1071 BSTP_IFSTATE_FORWARDING);
1072 bp->bp_agreed = bp->bp_protover;
1073 } else if (bp->bp_state == BSTP_IFSTATE_DISCARDING)
1074 bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING);
1077 if (((bp->bp_sync && !bp->bp_synced) ||
1078 (bp->bp_reroot && bp->bp_recent_root_timer.active) ||
1079 (bp->bp_flags & BSTP_PORT_DISPUTED)) && !bp->bp_operedge &&
1080 bp->bp_state != BSTP_IFSTATE_DISCARDING) {
1081 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1082 bp->bp_flags &= ~BSTP_PORT_DISPUTED;
1083 bstp_timer_start(&bp->bp_forward_delay_timer,
1084 bp->bp_protover == BSTP_PROTO_RSTP ?
1085 bp->bp_desg_htime : bp->bp_desg_fdelay);
1086 DPRINTF("%s -> DESIGNATED_DISCARD\n",
1087 bp->bp_ifp->if_xname);
1092 if (bp->bp_flags & BSTP_PORT_NEWINFO)
1093 bstp_transmit(bs, bp);
1097 bstp_update_tc(struct bstp_port *bp)
1099 switch (bp->bp_tcstate) {
1100 case BSTP_TCSTATE_ACTIVE:
1101 if ((bp->bp_role != BSTP_ROLE_DESIGNATED &&
1102 bp->bp_role != BSTP_ROLE_ROOT) || bp->bp_operedge)
1103 bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1106 bstp_set_port_tc(bp, BSTP_TCSTATE_TCN);
1108 bstp_set_port_tc(bp, BSTP_TCSTATE_TC);
1110 if (bp->bp_tc_prop && !bp->bp_operedge)
1111 bstp_set_port_tc(bp, BSTP_TCSTATE_PROPAG);
1114 bstp_set_port_tc(bp, BSTP_TCSTATE_ACK);
1117 case BSTP_TCSTATE_INACTIVE:
1118 if ((bp->bp_state == BSTP_IFSTATE_LEARNING ||
1119 bp->bp_state == BSTP_IFSTATE_FORWARDING) &&
1120 bp->bp_fdbflush == 0)
1121 bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1124 case BSTP_TCSTATE_LEARNING:
1125 if (bp->bp_rcvdtc || bp->bp_rcvdtcn || bp->bp_rcvdtca ||
1127 bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1128 else if (bp->bp_role != BSTP_ROLE_DESIGNATED &&
1129 bp->bp_role != BSTP_ROLE_ROOT &&
1130 bp->bp_state == BSTP_IFSTATE_DISCARDING)
1131 bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
1133 if ((bp->bp_role == BSTP_ROLE_DESIGNATED ||
1134 bp->bp_role == BSTP_ROLE_ROOT) &&
1135 bp->bp_state == BSTP_IFSTATE_FORWARDING &&
1137 bstp_set_port_tc(bp, BSTP_TCSTATE_DETECTED);
1140 /* these are transient states and go straight back to ACTIVE */
1141 case BSTP_TCSTATE_DETECTED:
1142 case BSTP_TCSTATE_TCN:
1143 case BSTP_TCSTATE_TC:
1144 case BSTP_TCSTATE_PROPAG:
1145 case BSTP_TCSTATE_ACK:
1146 DPRINTF("Invalid TC state for %s\n",
1147 bp->bp_ifp->if_xname);
1154 bstp_update_info(struct bstp_port *bp)
1156 struct bstp_state *bs = bp->bp_bs;
1158 bp->bp_proposing = 0;
1159 bp->bp_proposed = 0;
1161 if (bp->bp_agreed && !bstp_pdu_bettersame(bp, BSTP_INFO_MINE))
1164 if (bp->bp_synced && !bp->bp_agreed) {
1166 bs->bs_allsynced = 0;
1169 /* copy the designated pv to the port */
1170 bp->bp_port_pv = bp->bp_desg_pv;
1171 bp->bp_port_msg_age = bp->bp_desg_msg_age;
1172 bp->bp_port_max_age = bp->bp_desg_max_age;
1173 bp->bp_port_fdelay = bp->bp_desg_fdelay;
1174 bp->bp_port_htime = bp->bp_desg_htime;
1175 bp->bp_infois = BSTP_INFO_MINE;
1177 /* Set transmit flag but do not immediately send */
1178 bp->bp_flags |= BSTP_PORT_NEWINFO;
1181 /* set tcprop on every port other than the caller */
1183 bstp_set_other_tcprop(struct bstp_port *bp)
1185 struct bstp_state *bs = bp->bp_bs;
1186 struct bstp_port *bp2;
1188 BSTP_LOCK_ASSERT(bs);
1190 LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
1193 bp2->bp_tc_prop = 1;
1198 bstp_set_all_reroot(struct bstp_state *bs)
1200 struct bstp_port *bp;
1202 BSTP_LOCK_ASSERT(bs);
1204 LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
1209 bstp_set_all_sync(struct bstp_state *bs)
1211 struct bstp_port *bp;
1213 BSTP_LOCK_ASSERT(bs);
1215 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1217 bp->bp_synced = 0; /* Not explicit in spec */
1220 bs->bs_allsynced = 0;
1224 bstp_set_port_state(struct bstp_port *bp, int state)
1226 if (bp->bp_state == state)
1229 bp->bp_state = state;
1231 switch (bp->bp_state) {
1232 case BSTP_IFSTATE_DISCARDING:
1233 DPRINTF("state changed to DISCARDING on %s\n",
1234 bp->bp_ifp->if_xname);
1237 case BSTP_IFSTATE_LEARNING:
1238 DPRINTF("state changed to LEARNING on %s\n",
1239 bp->bp_ifp->if_xname);
1241 bstp_timer_start(&bp->bp_forward_delay_timer,
1242 bp->bp_protover == BSTP_PROTO_RSTP ?
1243 bp->bp_desg_htime : bp->bp_desg_fdelay);
1246 case BSTP_IFSTATE_FORWARDING:
1247 DPRINTF("state changed to FORWARDING on %s\n",
1248 bp->bp_ifp->if_xname);
1250 bstp_timer_stop(&bp->bp_forward_delay_timer);
1251 /* Record that we enabled forwarding */
1252 bp->bp_forward_transitions++;
1256 /* notify the parent bridge */
1257 taskqueue_enqueue(taskqueue_swi, &bp->bp_statetask);
1261 bstp_set_port_role(struct bstp_port *bp, int role)
1263 struct bstp_state *bs = bp->bp_bs;
1265 if (bp->bp_role == role)
1268 /* perform pre-change tasks */
1269 switch (bp->bp_role) {
1270 case BSTP_ROLE_DISABLED:
1271 bstp_timer_start(&bp->bp_forward_delay_timer,
1272 bp->bp_desg_max_age);
1275 case BSTP_ROLE_BACKUP:
1276 bstp_timer_start(&bp->bp_recent_backup_timer,
1277 bp->bp_desg_htime * 2);
1279 case BSTP_ROLE_ALTERNATE:
1280 bstp_timer_start(&bp->bp_forward_delay_timer,
1281 bp->bp_desg_fdelay);
1287 case BSTP_ROLE_ROOT:
1288 bstp_timer_start(&bp->bp_recent_root_timer,
1289 BSTP_DEFAULT_FORWARD_DELAY);
1294 /* clear values not carried between roles */
1295 bp->bp_proposing = 0;
1296 bs->bs_allsynced = 0;
1298 /* initialise the new role */
1299 switch (bp->bp_role) {
1300 case BSTP_ROLE_DISABLED:
1301 case BSTP_ROLE_ALTERNATE:
1302 case BSTP_ROLE_BACKUP:
1303 DPRINTF("%s role -> ALT/BACK/DISABLED\n",
1304 bp->bp_ifp->if_xname);
1305 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1306 bstp_timer_stop(&bp->bp_recent_root_timer);
1307 bstp_timer_latch(&bp->bp_forward_delay_timer);
1313 case BSTP_ROLE_ROOT:
1314 DPRINTF("%s role -> ROOT\n",
1315 bp->bp_ifp->if_xname);
1316 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1317 bstp_timer_latch(&bp->bp_recent_root_timer);
1318 bp->bp_proposing = 0;
1321 case BSTP_ROLE_DESIGNATED:
1322 DPRINTF("%s role -> DESIGNATED\n",
1323 bp->bp_ifp->if_xname);
1324 bstp_timer_start(&bp->bp_hello_timer,
1330 /* let the TC state know that the role changed */
1335 bstp_set_port_proto(struct bstp_port *bp, int proto)
1337 struct bstp_state *bs = bp->bp_bs;
1339 /* supported protocol versions */
1341 case BSTP_PROTO_STP:
1342 /* we can downgrade protocols only */
1343 bstp_timer_stop(&bp->bp_migrate_delay_timer);
1344 /* clear unsupported features */
1345 bp->bp_operedge = 0;
1346 /* STP compat mode only uses 16 bits of the 32 */
1347 if (bp->bp_path_cost > 65535)
1348 bp->bp_path_cost = 65535;
1351 case BSTP_PROTO_RSTP:
1352 bstp_timer_start(&bp->bp_migrate_delay_timer,
1353 bs->bs_migration_delay);
1357 DPRINTF("Unsupported STP version %d\n", proto);
1361 bp->bp_protover = proto;
1362 bp->bp_flags &= ~BSTP_PORT_CANMIGRATE;
1366 bstp_set_port_tc(struct bstp_port *bp, int state)
1368 struct bstp_state *bs = bp->bp_bs;
1370 bp->bp_tcstate = state;
1372 /* initialise the new state */
1373 switch (bp->bp_tcstate) {
1374 case BSTP_TCSTATE_ACTIVE:
1375 DPRINTF("%s -> TC_ACTIVE\n", bp->bp_ifp->if_xname);
1379 case BSTP_TCSTATE_INACTIVE:
1380 bstp_timer_stop(&bp->bp_tc_timer);
1381 /* flush routes on the parent bridge */
1382 bp->bp_fdbflush = 1;
1383 taskqueue_enqueue(taskqueue_swi, &bp->bp_rtagetask);
1385 DPRINTF("%s -> TC_INACTIVE\n", bp->bp_ifp->if_xname);
1388 case BSTP_TCSTATE_LEARNING:
1393 DPRINTF("%s -> TC_LEARNING\n", bp->bp_ifp->if_xname);
1396 case BSTP_TCSTATE_DETECTED:
1397 bstp_set_timer_tc(bp);
1398 bstp_set_other_tcprop(bp);
1399 /* send out notification */
1400 bp->bp_flags |= BSTP_PORT_NEWINFO;
1401 bstp_transmit(bs, bp);
1402 getmicrotime(&bs->bs_last_tc_time);
1403 DPRINTF("%s -> TC_DETECTED\n", bp->bp_ifp->if_xname);
1404 bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1407 case BSTP_TCSTATE_TCN:
1408 bstp_set_timer_tc(bp);
1409 DPRINTF("%s -> TC_TCN\n", bp->bp_ifp->if_xname);
1411 case BSTP_TCSTATE_TC:
1414 if (bp->bp_role == BSTP_ROLE_DESIGNATED)
1417 bstp_set_other_tcprop(bp);
1418 DPRINTF("%s -> TC_TC\n", bp->bp_ifp->if_xname);
1419 bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1422 case BSTP_TCSTATE_PROPAG:
1423 /* flush routes on the parent bridge */
1424 bp->bp_fdbflush = 1;
1425 taskqueue_enqueue(taskqueue_swi, &bp->bp_rtagetask);
1427 bstp_set_timer_tc(bp);
1428 DPRINTF("%s -> TC_PROPAG\n", bp->bp_ifp->if_xname);
1429 bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1432 case BSTP_TCSTATE_ACK:
1433 bstp_timer_stop(&bp->bp_tc_timer);
1435 DPRINTF("%s -> TC_ACK\n", bp->bp_ifp->if_xname);
1436 bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1442 bstp_set_timer_tc(struct bstp_port *bp)
1444 struct bstp_state *bs = bp->bp_bs;
1446 if (bp->bp_tc_timer.active)
1449 switch (bp->bp_protover) {
1450 case BSTP_PROTO_RSTP:
1451 bstp_timer_start(&bp->bp_tc_timer,
1452 bp->bp_desg_htime + BSTP_TICK_VAL);
1453 bp->bp_flags |= BSTP_PORT_NEWINFO;
1456 case BSTP_PROTO_STP:
1457 bstp_timer_start(&bp->bp_tc_timer,
1458 bs->bs_root_max_age + bs->bs_root_fdelay);
1464 bstp_set_timer_msgage(struct bstp_port *bp)
1466 if (bp->bp_port_msg_age + BSTP_MESSAGE_AGE_INCR <=
1467 bp->bp_port_max_age) {
1468 bstp_timer_start(&bp->bp_message_age_timer,
1469 bp->bp_port_htime * 3);
1471 /* expires immediately */
1472 bstp_timer_start(&bp->bp_message_age_timer, 0);
1476 bstp_rerooted(struct bstp_state *bs, struct bstp_port *bp)
1478 struct bstp_port *bp2;
1481 LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
1484 if (bp2->bp_recent_root_timer.active) {
1493 bstp_set_htime(struct bstp_state *bs, int t)
1495 /* convert seconds to ticks */
1498 /* value can only be changed in leagacy stp mode */
1499 if (bs->bs_protover != BSTP_PROTO_STP)
1502 if (t < BSTP_MIN_HELLO_TIME || t > BSTP_MAX_HELLO_TIME)
1506 bs->bs_bridge_htime = t;
1513 bstp_set_fdelay(struct bstp_state *bs, int t)
1515 /* convert seconds to ticks */
1518 if (t < BSTP_MIN_FORWARD_DELAY || t > BSTP_MAX_FORWARD_DELAY)
1522 bs->bs_bridge_fdelay = t;
1529 bstp_set_maxage(struct bstp_state *bs, int t)
1531 /* convert seconds to ticks */
1534 if (t < BSTP_MIN_MAX_AGE || t > BSTP_MAX_MAX_AGE)
1538 bs->bs_bridge_max_age = t;
1545 bstp_set_holdcount(struct bstp_state *bs, int count)
1547 struct bstp_port *bp;
1549 if (count < BSTP_MIN_HOLD_COUNT ||
1550 count > BSTP_MAX_HOLD_COUNT)
1554 bs->bs_txholdcount = count;
1555 LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
1562 bstp_set_protocol(struct bstp_state *bs, int proto)
1564 struct bstp_port *bp;
1567 /* Supported protocol versions */
1568 case BSTP_PROTO_STP:
1569 case BSTP_PROTO_RSTP:
1577 bs->bs_protover = proto;
1578 bs->bs_bridge_htime = BSTP_DEFAULT_HELLO_TIME;
1579 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1581 bp->bp_infois = BSTP_INFO_DISABLED;
1583 bstp_set_port_proto(bp, bs->bs_protover);
1584 bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
1585 bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
1586 bstp_timer_stop(&bp->bp_recent_backup_timer);
1594 bstp_set_priority(struct bstp_state *bs, int pri)
1596 if (pri < 0 || pri > BSTP_MAX_PRIORITY)
1599 /* Limit to steps of 4096 */
1603 bs->bs_bridge_priority = pri;
1610 bstp_set_port_priority(struct bstp_port *bp, int pri)
1612 struct bstp_state *bs = bp->bp_bs;
1614 if (pri < 0 || pri > BSTP_MAX_PORT_PRIORITY)
1617 /* Limit to steps of 16 */
1621 bp->bp_priority = pri;
1628 bstp_set_path_cost(struct bstp_port *bp, uint32_t path_cost)
1630 struct bstp_state *bs = bp->bp_bs;
1632 if (path_cost > BSTP_MAX_PATH_COST)
1635 /* STP compat mode only uses 16 bits of the 32 */
1636 if (bp->bp_protover == BSTP_PROTO_STP && path_cost > 65535)
1641 if (path_cost == 0) { /* use auto */
1642 bp->bp_flags &= ~BSTP_PORT_ADMCOST;
1643 bp->bp_path_cost = bstp_calc_path_cost(bp);
1645 bp->bp_path_cost = path_cost;
1646 bp->bp_flags |= BSTP_PORT_ADMCOST;
1654 bstp_set_edge(struct bstp_port *bp, int set)
1656 struct bstp_state *bs = bp->bp_bs;
1659 if ((bp->bp_operedge = set) == 0)
1660 bp->bp_flags &= ~BSTP_PORT_ADMEDGE;
1662 bp->bp_flags |= BSTP_PORT_ADMEDGE;
1668 bstp_set_autoedge(struct bstp_port *bp, int set)
1670 struct bstp_state *bs = bp->bp_bs;
1674 bp->bp_flags |= BSTP_PORT_AUTOEDGE;
1675 /* we may be able to transition straight to edge */
1676 if (bp->bp_edge_delay_timer.active == 0)
1677 bstp_edge_delay_expiry(bs, bp);
1679 bp->bp_flags &= ~BSTP_PORT_AUTOEDGE;
1685 bstp_set_ptp(struct bstp_port *bp, int set)
1687 struct bstp_state *bs = bp->bp_bs;
1690 bp->bp_ptp_link = set;
1696 bstp_set_autoptp(struct bstp_port *bp, int set)
1698 struct bstp_state *bs = bp->bp_bs;
1702 bp->bp_flags |= BSTP_PORT_AUTOPTP;
1703 if (bp->bp_role != BSTP_ROLE_DISABLED)
1704 taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
1706 bp->bp_flags &= ~BSTP_PORT_AUTOPTP;
1712 * Calculate the path cost according to the link speed.
1715 bstp_calc_path_cost(struct bstp_port *bp)
1717 struct ifnet *ifp = bp->bp_ifp;
1720 /* If the priority has been manually set then retain the value */
1721 if (bp->bp_flags & BSTP_PORT_ADMCOST)
1722 return bp->bp_path_cost;
1724 if (ifp->if_link_state == LINK_STATE_DOWN) {
1725 /* Recalc when the link comes up again */
1726 bp->bp_flags |= BSTP_PORT_PNDCOST;
1727 return (BSTP_DEFAULT_PATH_COST);
1730 if (ifp->if_baudrate < 1000)
1731 return (BSTP_DEFAULT_PATH_COST);
1733 /* formula from section 17.14, IEEE Std 802.1D-2004 */
1734 path_cost = 20000000000ULL / (ifp->if_baudrate / 1000);
1736 if (path_cost > BSTP_MAX_PATH_COST)
1737 path_cost = BSTP_MAX_PATH_COST;
1739 /* STP compat mode only uses 16 bits of the 32 */
1740 if (bp->bp_protover == BSTP_PROTO_STP && path_cost > 65535)
1747 * Notify the bridge that a port state has changed, we need to do this from a
1748 * taskqueue to avoid a LOR.
1751 bstp_notify_state(void *arg, int pending)
1753 struct bstp_port *bp = (struct bstp_port *)arg;
1754 struct bstp_state *bs = bp->bp_bs;
1756 if (bp->bp_active == 1 && bs->bs_state_cb != NULL)
1757 (*bs->bs_state_cb)(bp->bp_ifp, bp->bp_state);
1761 * Flush the routes on the bridge port, we need to do this from a
1762 * taskqueue to avoid a LOR.
1765 bstp_notify_rtage(void *arg, int pending)
1767 struct bstp_port *bp = (struct bstp_port *)arg;
1768 struct bstp_state *bs = bp->bp_bs;
1772 switch (bp->bp_protover) {
1773 case BSTP_PROTO_STP:
1774 /* convert to seconds */
1775 age = bp->bp_desg_fdelay / BSTP_TICK_VAL;
1778 case BSTP_PROTO_RSTP:
1784 if (bp->bp_active == 1 && bs->bs_rtage_cb != NULL)
1785 (*bs->bs_rtage_cb)(bp->bp_ifp, age);
1787 /* flush is complete */
1789 bp->bp_fdbflush = 0;
1794 bstp_linkstate(struct bstp_port *bp)
1796 struct bstp_state *bs = bp->bp_bs;
1801 bstp_ifupdstatus(bp, 0);
1803 bstp_update_state(bs, bp);
1808 bstp_ifupdstatus(void *arg, int pending)
1810 struct bstp_port *bp = (struct bstp_port *)arg;
1811 struct bstp_state *bs = bp->bp_bs;
1812 struct ifnet *ifp = bp->bp_ifp;
1813 struct ifmediareq ifmr;
1819 bzero((char *)&ifmr, sizeof(ifmr));
1820 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
1824 if ((error == 0) && (ifp->if_flags & IFF_UP)) {
1825 if (ifmr.ifm_status & IFM_ACTIVE) {
1826 /* A full-duplex link is assumed to be point to point */
1827 if (bp->bp_flags & BSTP_PORT_AUTOPTP) {
1830 fdx = ifmr.ifm_active & IFM_FDX ? 1 : 0;
1831 if (bp->bp_ptp_link ^ fdx) {
1832 bp->bp_ptp_link = fdx;
1837 /* Calc the cost if the link was down previously */
1838 if (bp->bp_flags & BSTP_PORT_PNDCOST) {
1841 cost = bstp_calc_path_cost(bp);
1842 if (bp->bp_path_cost != cost) {
1843 bp->bp_path_cost = cost;
1846 bp->bp_flags &= ~BSTP_PORT_PNDCOST;
1849 if (bp->bp_role == BSTP_ROLE_DISABLED) {
1850 bstp_enable_port(bs, bp);
1854 if (bp->bp_role != BSTP_ROLE_DISABLED) {
1855 bstp_disable_port(bs, bp);
1857 if ((bp->bp_flags & BSTP_PORT_ADMEDGE) &&
1858 bp->bp_protover == BSTP_PROTO_RSTP)
1859 bp->bp_operedge = 1;
1862 } else if (bp->bp_infois != BSTP_INFO_DISABLED) {
1863 bstp_disable_port(bs, bp);
1867 bstp_assign_roles(bs);
1872 bstp_enable_port(struct bstp_state *bs, struct bstp_port *bp)
1874 bp->bp_infois = BSTP_INFO_AGED;
1878 bstp_disable_port(struct bstp_state *bs, struct bstp_port *bp)
1880 bp->bp_infois = BSTP_INFO_DISABLED;
1884 bstp_tick(void *arg)
1886 struct epoch_tracker et;
1887 struct bstp_state *bs = arg;
1888 struct bstp_port *bp;
1890 BSTP_LOCK_ASSERT(bs);
1892 if (bs->bs_running == 0)
1895 NET_EPOCH_ENTER(et);
1896 CURVNET_SET(bs->bs_vnet);
1898 /* poll link events on interfaces that do not support linkstate */
1899 if (bstp_timer_dectest(&bs->bs_link_timer)) {
1900 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1901 if (!(bp->bp_ifp->if_capabilities & IFCAP_LINKSTATE))
1902 taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
1904 bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER);
1907 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1908 /* no events need to happen for these */
1909 bstp_timer_dectest(&bp->bp_tc_timer);
1910 bstp_timer_dectest(&bp->bp_recent_root_timer);
1911 bstp_timer_dectest(&bp->bp_forward_delay_timer);
1912 bstp_timer_dectest(&bp->bp_recent_backup_timer);
1914 if (bstp_timer_dectest(&bp->bp_hello_timer))
1915 bstp_hello_timer_expiry(bs, bp);
1917 if (bstp_timer_dectest(&bp->bp_message_age_timer))
1918 bstp_message_age_expiry(bs, bp);
1920 if (bstp_timer_dectest(&bp->bp_migrate_delay_timer))
1921 bstp_migrate_delay_expiry(bs, bp);
1923 if (bstp_timer_dectest(&bp->bp_edge_delay_timer))
1924 bstp_edge_delay_expiry(bs, bp);
1926 /* update the various state machines for the port */
1927 bstp_update_state(bs, bp);
1929 if (bp->bp_txcount > 0)
1936 callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
1940 bstp_timer_start(struct bstp_timer *t, uint16_t v)
1948 bstp_timer_stop(struct bstp_timer *t)
1956 bstp_timer_latch(struct bstp_timer *t)
1963 bstp_timer_dectest(struct bstp_timer *t)
1965 if (t->active == 0 || t->latched)
1967 t->value -= BSTP_TICK_VAL;
1968 if (t->value <= 0) {
1976 bstp_hello_timer_expiry(struct bstp_state *bs, struct bstp_port *bp)
1978 if ((bp->bp_flags & BSTP_PORT_NEWINFO) ||
1979 bp->bp_role == BSTP_ROLE_DESIGNATED ||
1980 (bp->bp_role == BSTP_ROLE_ROOT &&
1981 bp->bp_tc_timer.active == 1)) {
1982 bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime);
1983 bp->bp_flags |= BSTP_PORT_NEWINFO;
1984 bstp_transmit(bs, bp);
1989 bstp_message_age_expiry(struct bstp_state *bs, struct bstp_port *bp)
1991 if (bp->bp_infois == BSTP_INFO_RECEIVED) {
1992 bp->bp_infois = BSTP_INFO_AGED;
1993 bstp_assign_roles(bs);
1994 DPRINTF("aged info on %s\n", bp->bp_ifp->if_xname);
1999 bstp_migrate_delay_expiry(struct bstp_state *bs, struct bstp_port *bp)
2001 bp->bp_flags |= BSTP_PORT_CANMIGRATE;
2005 bstp_edge_delay_expiry(struct bstp_state *bs, struct bstp_port *bp)
2007 if ((bp->bp_flags & BSTP_PORT_AUTOEDGE) &&
2008 bp->bp_protover == BSTP_PROTO_RSTP && bp->bp_proposing &&
2009 bp->bp_role == BSTP_ROLE_DESIGNATED) {
2010 bp->bp_operedge = 1;
2011 DPRINTF("%s -> edge port\n", bp->bp_ifp->if_xname);
2016 bstp_addr_cmp(const uint8_t *a, const uint8_t *b)
2020 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2021 d = ((int)a[i]) - ((int)b[i]);
2028 * compare the bridge address component of the bridgeid
2031 bstp_same_bridgeid(uint64_t id1, uint64_t id2)
2033 u_char addr1[ETHER_ADDR_LEN];
2034 u_char addr2[ETHER_ADDR_LEN];
2036 PV2ADDR(id1, addr1);
2037 PV2ADDR(id2, addr2);
2039 if (bstp_addr_cmp(addr1, addr2) == 0)
2046 bstp_reinit(struct bstp_state *bs)
2048 struct epoch_tracker et;
2049 struct bstp_port *bp;
2050 struct ifnet *ifp, *mif;
2053 static const u_char llzero[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
2055 BSTP_LOCK_ASSERT(bs);
2057 if (LIST_EMPTY(&bs->bs_bplist))
2061 bridgeptr = LIST_FIRST(&bs->bs_bplist)->bp_ifp->if_bridge;
2062 KASSERT(bridgeptr != NULL, ("Invalid bridge pointer"));
2064 * Search through the Ethernet adapters and find the one with the
2065 * lowest value. Make sure the adapter which we take the MAC address
2066 * from is part of this bridge, so we can have more than one independent
2067 * bridges in the same STP domain.
2069 NET_EPOCH_ENTER(et);
2070 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2071 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
2072 continue; /* Not Ethernet */
2074 if (ifp->if_bridge != bridgeptr)
2075 continue; /* Not part of our bridge */
2077 if (bstp_addr_cmp(IF_LLADDR(ifp), llzero) == 0)
2078 continue; /* No mac address set */
2084 if (bstp_addr_cmp(IF_LLADDR(ifp), IF_LLADDR(mif)) < 0) {
2093 e_addr = IF_LLADDR(mif);
2094 bs->bs_bridge_pv.pv_dbridge_id =
2095 (((uint64_t)bs->bs_bridge_priority) << 48) |
2096 (((uint64_t)e_addr[0]) << 40) |
2097 (((uint64_t)e_addr[1]) << 32) |
2098 (((uint64_t)e_addr[2]) << 24) |
2099 (((uint64_t)e_addr[3]) << 16) |
2100 (((uint64_t)e_addr[4]) << 8) |
2101 (((uint64_t)e_addr[5]));
2103 bs->bs_bridge_pv.pv_root_id = bs->bs_bridge_pv.pv_dbridge_id;
2104 bs->bs_bridge_pv.pv_cost = 0;
2105 bs->bs_bridge_pv.pv_dport_id = 0;
2106 bs->bs_bridge_pv.pv_port_id = 0;
2108 if (bs->bs_running && callout_pending(&bs->bs_bstpcallout) == 0)
2109 callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
2111 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
2112 bp->bp_port_id = (bp->bp_priority << 8) |
2113 (bp->bp_ifp->if_index & 0xfff);
2114 taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
2117 bstp_assign_roles(bs);
2118 bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER);
2122 /* Set the bridge and root id (lower bits) to zero */
2123 bs->bs_bridge_pv.pv_dbridge_id =
2124 ((uint64_t)bs->bs_bridge_priority) << 48;
2125 bs->bs_bridge_pv.pv_root_id = bs->bs_bridge_pv.pv_dbridge_id;
2126 bs->bs_root_pv = bs->bs_bridge_pv;
2127 /* Disable any remaining ports, they will have no MAC address */
2128 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
2129 bp->bp_infois = BSTP_INFO_DISABLED;
2130 bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
2132 callout_stop(&bs->bs_bstpcallout);
2136 bstp_modevent(module_t mod, int type, void *data)
2140 mtx_init(&bstp_list_mtx, "bridgestp list", NULL, MTX_DEF);
2141 LIST_INIT(&bstp_list);
2144 mtx_destroy(&bstp_list_mtx);
2147 return (EOPNOTSUPP);
2152 static moduledata_t bstp_mod = {
2158 DECLARE_MODULE(bridgestp, bstp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2159 MODULE_VERSION(bridgestp, 1);
2162 bstp_attach(struct bstp_state *bs, struct bstp_cb_ops *cb)
2165 callout_init_mtx(&bs->bs_bstpcallout, &bs->bs_mtx, 0);
2166 LIST_INIT(&bs->bs_bplist);
2168 bs->bs_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
2169 bs->bs_bridge_htime = BSTP_DEFAULT_HELLO_TIME;
2170 bs->bs_bridge_fdelay = BSTP_DEFAULT_FORWARD_DELAY;
2171 bs->bs_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
2172 bs->bs_hold_time = BSTP_DEFAULT_HOLD_TIME;
2173 bs->bs_migration_delay = BSTP_DEFAULT_MIGRATE_DELAY;
2174 bs->bs_txholdcount = BSTP_DEFAULT_HOLD_COUNT;
2175 bs->bs_protover = BSTP_PROTO_RSTP;
2176 bs->bs_state_cb = cb->bcb_state;
2177 bs->bs_rtage_cb = cb->bcb_rtage;
2178 bs->bs_vnet = curvnet;
2180 getmicrotime(&bs->bs_last_tc_time);
2182 mtx_lock(&bstp_list_mtx);
2183 LIST_INSERT_HEAD(&bstp_list, bs, bs_list);
2184 mtx_unlock(&bstp_list_mtx);
2188 bstp_detach(struct bstp_state *bs)
2190 KASSERT(LIST_EMPTY(&bs->bs_bplist), ("bstp still active"));
2192 mtx_lock(&bstp_list_mtx);
2193 LIST_REMOVE(bs, bs_list);
2194 mtx_unlock(&bstp_list_mtx);
2195 callout_drain(&bs->bs_bstpcallout);
2196 BSTP_LOCK_DESTROY(bs);
2200 bstp_init(struct bstp_state *bs)
2203 callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
2210 bstp_stop(struct bstp_state *bs)
2212 struct bstp_port *bp;
2216 LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
2217 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
2220 callout_stop(&bs->bs_bstpcallout);
2225 bstp_create(struct bstp_state *bs, struct bstp_port *bp, struct ifnet *ifp)
2227 bzero(bp, sizeof(struct bstp_port));
2232 bp->bp_priority = BSTP_DEFAULT_PORT_PRIORITY;
2233 TASK_INIT(&bp->bp_statetask, 0, bstp_notify_state, bp);
2234 TASK_INIT(&bp->bp_rtagetask, 0, bstp_notify_rtage, bp);
2235 TASK_INIT(&bp->bp_mediatask, 0, bstp_ifupdstatus, bp);
2238 bp->bp_infois = BSTP_INFO_DISABLED;
2239 bp->bp_flags = BSTP_PORT_AUTOEDGE|BSTP_PORT_AUTOPTP;
2240 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
2241 bstp_set_port_proto(bp, bs->bs_protover);
2242 bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
2243 bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
2244 bp->bp_path_cost = bstp_calc_path_cost(bp);
2250 bstp_enable(struct bstp_port *bp)
2252 struct bstp_state *bs = bp->bp_bs;
2253 struct ifnet *ifp = bp->bp_ifp;
2255 KASSERT(bp->bp_active == 0, ("already a bstp member"));
2256 NET_EPOCH_ASSERT(); /* Because bstp_update_roles() causes traffic. */
2258 switch (ifp->if_type) {
2259 case IFT_ETHER: /* These can do spanning tree. */
2263 /* Nothing else can. */
2268 LIST_INSERT_HEAD(&bs->bs_bplist, bp, bp_next);
2270 bp->bp_flags |= BSTP_PORT_NEWINFO;
2272 bstp_update_roles(bs, bp);
2278 bstp_disable(struct bstp_port *bp)
2280 struct bstp_state *bs = bp->bp_bs;
2282 KASSERT(bp->bp_active == 1, ("not a bstp member"));
2285 bstp_disable_port(bs, bp);
2286 LIST_REMOVE(bp, bp_next);
2293 * The bstp_port structure is about to be freed by the parent bridge.
2296 bstp_destroy(struct bstp_port *bp)
2298 KASSERT(bp->bp_active == 0, ("port is still attached"));
2299 taskqueue_drain(taskqueue_swi, &bp->bp_statetask);
2300 taskqueue_drain(taskqueue_swi, &bp->bp_rtagetask);
2301 taskqueue_drain(taskqueue_swi, &bp->bp_mediatask);
2303 if (bp->bp_bs->bs_root_port == bp)
2304 bstp_assign_roles(bp->bp_bs);