1 /* $NetBSD: bridgestp.c,v 1.5 2003/11/28 08:56:48 keihan Exp $ */
4 * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
5 * Copyright (c) 2006 Andrew Thompson (thompsa@FreeBSD.org)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * OpenBSD: bridgestp.c,v 1.5 2001/03/22 03:48:29 jason Exp
33 * Implementation of the spanning tree protocol as defined in
34 * ISO/IEC 802.1D-2004, June 9, 2004.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
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/callout.h>
47 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/taskqueue.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/if_llc.h>
57 #include <net/if_media.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/if_ether.h>
63 #include <net/bridgestp.h>
65 #ifdef BRIDGESTP_DEBUG
66 #define DPRINTF(fmt, arg...) printf("bstp: " fmt, ##arg)
68 #define DPRINTF(fmt, arg...)
71 #define PV2ADDR(pv, eaddr) do { \
72 eaddr[0] = pv >> 40; \
73 eaddr[1] = pv >> 32; \
74 eaddr[2] = pv >> 24; \
75 eaddr[3] = pv >> 16; \
84 const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
86 LIST_HEAD(, bstp_state) bstp_list;
87 static struct mtx bstp_list_mtx;
89 static void bstp_transmit(struct bstp_state *, struct bstp_port *);
90 static void bstp_transmit_bpdu(struct bstp_state *, struct bstp_port *);
91 static void bstp_transmit_tcn(struct bstp_state *, struct bstp_port *);
92 static void bstp_decode_bpdu(struct bstp_port *, struct bstp_cbpdu *,
93 struct bstp_config_unit *);
94 static void bstp_send_bpdu(struct bstp_state *, struct bstp_port *,
96 static void bstp_enqueue(struct ifnet *, struct mbuf *);
97 static int bstp_pdu_flags(struct bstp_port *);
98 static void bstp_received_stp(struct bstp_state *, struct bstp_port *,
99 struct mbuf **, struct bstp_tbpdu *);
100 static void bstp_received_rstp(struct bstp_state *, struct bstp_port *,
101 struct mbuf **, struct bstp_tbpdu *);
102 static void bstp_received_tcn(struct bstp_state *, struct bstp_port *,
103 struct bstp_tcn_unit *);
104 static void bstp_received_bpdu(struct bstp_state *, struct bstp_port *,
105 struct bstp_config_unit *);
106 static int bstp_pdu_rcvtype(struct bstp_port *, struct bstp_config_unit *);
107 static int bstp_pdu_bettersame(struct bstp_port *, int);
108 static int bstp_info_cmp(struct bstp_pri_vector *,
109 struct bstp_pri_vector *);
110 static int bstp_info_superior(struct bstp_pri_vector *,
111 struct bstp_pri_vector *);
112 static void bstp_assign_roles(struct bstp_state *);
113 static void bstp_update_roles(struct bstp_state *, struct bstp_port *);
114 static void bstp_update_state(struct bstp_state *, struct bstp_port *);
115 static void bstp_update_tc(struct bstp_port *);
116 static void bstp_update_info(struct bstp_port *);
117 static void bstp_set_other_tcprop(struct bstp_port *);
118 static void bstp_set_all_reroot(struct bstp_state *);
119 static void bstp_set_all_sync(struct bstp_state *);
120 static void bstp_set_port_state(struct bstp_port *, int);
121 static void bstp_set_port_role(struct bstp_port *, int);
122 static void bstp_set_port_proto(struct bstp_port *, int);
123 static void bstp_set_port_tc(struct bstp_port *, int);
124 static void bstp_set_timer_tc(struct bstp_port *);
125 static void bstp_set_timer_msgage(struct bstp_port *);
126 static int bstp_rerooted(struct bstp_state *, struct bstp_port *);
127 static uint32_t bstp_calc_path_cost(struct bstp_port *);
128 static void bstp_notify_state(void *, int);
129 static void bstp_notify_rtage(void *, int);
130 static void bstp_ifupdstatus(struct bstp_state *, struct bstp_port *);
131 static void bstp_enable_port(struct bstp_state *, struct bstp_port *);
132 static void bstp_disable_port(struct bstp_state *, struct bstp_port *);
133 static void bstp_tick(void *);
134 static void bstp_timer_start(struct bstp_timer *, uint16_t);
135 static void bstp_timer_stop(struct bstp_timer *);
136 static void bstp_timer_latch(struct bstp_timer *);
137 static int bstp_timer_expired(struct bstp_timer *);
138 static void bstp_hello_timer_expiry(struct bstp_state *,
140 static void bstp_message_age_expiry(struct bstp_state *,
142 static void bstp_migrate_delay_expiry(struct bstp_state *,
144 static void bstp_edge_delay_expiry(struct bstp_state *,
146 static int bstp_addr_cmp(const uint8_t *, const uint8_t *);
147 static int bstp_same_bridgeid(uint64_t, uint64_t);
148 static void bstp_reinit(struct bstp_state *);
151 bstp_transmit(struct bstp_state *bs, struct bstp_port *bp)
153 if (bs->bs_running == 0)
157 * a PDU can only be sent if we have tx quota left and the
158 * hello timer is running.
160 if (bp->bp_hello_timer.active == 0) {
161 /* Test if it needs to be reset */
162 bstp_hello_timer_expiry(bs, bp);
165 if (bp->bp_txcount > bs->bs_txholdcount)
166 /* Ran out of karma */
169 if (bp->bp_protover == BSTP_PROTO_RSTP) {
170 bstp_transmit_bpdu(bs, bp);
173 switch (bp->bp_role) {
174 case BSTP_ROLE_DESIGNATED:
175 bstp_transmit_bpdu(bs, bp);
180 bstp_transmit_tcn(bs, bp);
184 bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime);
185 bp->bp_flags &= ~BSTP_PORT_NEWINFO;
189 bstp_transmit_bpdu(struct bstp_state *bs, struct bstp_port *bp)
191 struct bstp_cbpdu bpdu;
193 BSTP_LOCK_ASSERT(bs);
195 bpdu.cbu_rootpri = htons(bp->bp_desg_pv.pv_root_id >> 48);
196 PV2ADDR(bp->bp_desg_pv.pv_root_id, bpdu.cbu_rootaddr);
198 bpdu.cbu_rootpathcost = htonl(bp->bp_desg_pv.pv_cost);
200 bpdu.cbu_bridgepri = htons(bp->bp_desg_pv.pv_dbridge_id >> 48);
201 PV2ADDR(bp->bp_desg_pv.pv_dbridge_id, bpdu.cbu_bridgeaddr);
203 bpdu.cbu_portid = htons(bp->bp_port_id);
204 bpdu.cbu_messageage = htons(bp->bp_desg_msg_age);
205 bpdu.cbu_maxage = htons(bp->bp_desg_max_age);
206 bpdu.cbu_hellotime = htons(bp->bp_desg_htime);
207 bpdu.cbu_forwarddelay = htons(bp->bp_desg_fdelay);
209 bpdu.cbu_flags = bstp_pdu_flags(bp);
211 switch (bp->bp_protover) {
213 bpdu.cbu_bpdutype = BSTP_MSGTYPE_CFG;
216 case BSTP_PROTO_RSTP:
217 bpdu.cbu_bpdutype = BSTP_MSGTYPE_RSTP;
221 bstp_send_bpdu(bs, bp, &bpdu);
225 bstp_transmit_tcn(struct bstp_state *bs, struct bstp_port *bp)
227 struct bstp_tbpdu bpdu;
228 struct ifnet *ifp = bp->bp_ifp;
229 struct ether_header *eh;
232 KASSERT(bp == bs->bs_root_port, ("%s: bad root port\n", __func__));
234 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
237 MGETHDR(m, M_DONTWAIT, MT_DATA);
241 m->m_pkthdr.rcvif = ifp;
242 m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
243 m->m_len = m->m_pkthdr.len;
245 eh = mtod(m, struct ether_header *);
247 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
248 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
249 eh->ether_type = htons(sizeof(bpdu));
251 bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP;
252 bpdu.tbu_ctl = LLC_UI;
253 bpdu.tbu_protoid = 0;
254 bpdu.tbu_protover = 0;
255 bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN;
257 memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
260 bstp_enqueue(ifp, m);
264 bstp_decode_bpdu(struct bstp_port *bp, struct bstp_cbpdu *cpdu,
265 struct bstp_config_unit *cu)
269 cu->cu_pv.pv_root_id =
270 (((uint64_t)ntohs(cpdu->cbu_rootpri)) << 48) |
271 (((uint64_t)cpdu->cbu_rootaddr[0]) << 40) |
272 (((uint64_t)cpdu->cbu_rootaddr[1]) << 32) |
273 (((uint64_t)cpdu->cbu_rootaddr[2]) << 24) |
274 (((uint64_t)cpdu->cbu_rootaddr[3]) << 16) |
275 (((uint64_t)cpdu->cbu_rootaddr[4]) << 8) |
276 (((uint64_t)cpdu->cbu_rootaddr[5]) << 0);
278 cu->cu_pv.pv_dbridge_id =
279 (((uint64_t)ntohs(cpdu->cbu_bridgepri)) << 48) |
280 (((uint64_t)cpdu->cbu_bridgeaddr[0]) << 40) |
281 (((uint64_t)cpdu->cbu_bridgeaddr[1]) << 32) |
282 (((uint64_t)cpdu->cbu_bridgeaddr[2]) << 24) |
283 (((uint64_t)cpdu->cbu_bridgeaddr[3]) << 16) |
284 (((uint64_t)cpdu->cbu_bridgeaddr[4]) << 8) |
285 (((uint64_t)cpdu->cbu_bridgeaddr[5]) << 0);
287 cu->cu_pv.pv_cost = ntohl(cpdu->cbu_rootpathcost);
288 cu->cu_message_age = ntohs(cpdu->cbu_messageage);
289 cu->cu_max_age = ntohs(cpdu->cbu_maxage);
290 cu->cu_hello_time = ntohs(cpdu->cbu_hellotime);
291 cu->cu_forward_delay = ntohs(cpdu->cbu_forwarddelay);
292 cu->cu_pv.pv_dport_id = ntohs(cpdu->cbu_portid);
293 cu->cu_pv.pv_port_id = bp->bp_port_id;
294 cu->cu_message_type = cpdu->cbu_bpdutype;
296 /* Strip off unused flags in STP mode */
297 flags = cpdu->cbu_flags;
298 switch (cpdu->cbu_protover) {
300 flags &= BSTP_PDU_STPMASK;
301 /* A STP BPDU explicitly conveys a Designated Port */
302 cu->cu_role = BSTP_ROLE_DESIGNATED;
305 case BSTP_PROTO_RSTP:
306 flags &= BSTP_PDU_RSTPMASK;
310 cu->cu_topology_change_ack =
311 (flags & BSTP_PDU_F_TCA) ? 1 : 0;
313 (flags & BSTP_PDU_F_P) ? 1 : 0;
315 (flags & BSTP_PDU_F_A) ? 1 : 0;
317 (flags & BSTP_PDU_F_L) ? 1 : 0;
319 (flags & BSTP_PDU_F_F) ? 1 : 0;
320 cu->cu_topology_change =
321 (flags & BSTP_PDU_F_TC) ? 1 : 0;
323 switch ((flags & BSTP_PDU_PRMASK) >> BSTP_PDU_PRSHIFT) {
324 case BSTP_PDU_F_ROOT:
325 cu->cu_role = BSTP_ROLE_ROOT;
328 cu->cu_role = BSTP_ROLE_ALTERNATE;
330 case BSTP_PDU_F_DESG:
331 cu->cu_role = BSTP_ROLE_DESIGNATED;
337 bstp_send_bpdu(struct bstp_state *bs, struct bstp_port *bp,
338 struct bstp_cbpdu *bpdu)
342 struct ether_header *eh;
344 BSTP_LOCK_ASSERT(bs);
348 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
351 MGETHDR(m, M_DONTWAIT, MT_DATA);
355 eh = mtod(m, struct ether_header *);
357 bpdu->cbu_ssap = bpdu->cbu_dsap = LLC_8021D_LSAP;
358 bpdu->cbu_ctl = LLC_UI;
359 bpdu->cbu_protoid = htons(BSTP_PROTO_ID);
361 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
362 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
364 switch (bpdu->cbu_bpdutype) {
365 case BSTP_MSGTYPE_CFG:
366 bpdu->cbu_protover = BSTP_PROTO_STP;
367 m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_STP_LEN;
368 eh->ether_type = htons(BSTP_BPDU_STP_LEN);
369 memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu,
373 case BSTP_MSGTYPE_RSTP:
374 bpdu->cbu_protover = BSTP_PROTO_RSTP;
375 bpdu->cbu_versionlen = htons(0);
376 m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_RSTP_LEN;
377 eh->ether_type = htons(BSTP_BPDU_RSTP_LEN);
378 memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu,
383 panic("not implemented");
385 m->m_pkthdr.rcvif = ifp;
386 m->m_len = m->m_pkthdr.len;
389 bstp_enqueue(ifp, m);
393 bstp_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
397 IFQ_ENQUEUE(&dst_ifp->if_snd, m, err);
399 if ((dst_ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
400 (*dst_ifp->if_start)(dst_ifp);
404 bstp_pdu_flags(struct bstp_port *bp)
408 if (bp->bp_proposing && bp->bp_state != BSTP_IFSTATE_FORWARDING)
409 flags |= BSTP_PDU_F_P;
412 flags |= BSTP_PDU_F_A;
414 if (bp->bp_tc_timer.active)
415 flags |= BSTP_PDU_F_TC;
418 flags |= BSTP_PDU_F_TCA;
420 switch (bp->bp_state) {
421 case BSTP_IFSTATE_LEARNING:
422 flags |= BSTP_PDU_F_L;
425 case BSTP_IFSTATE_FORWARDING:
426 flags |= (BSTP_PDU_F_L | BSTP_PDU_F_F);
430 switch (bp->bp_role) {
433 (BSTP_PDU_F_ROOT << BSTP_PDU_PRSHIFT);
436 case BSTP_ROLE_ALTERNATE:
437 case BSTP_ROLE_BACKUP: /* fall through */
439 (BSTP_PDU_F_ALT << BSTP_PDU_PRSHIFT);
442 case BSTP_ROLE_DESIGNATED:
444 (BSTP_PDU_F_DESG << BSTP_PDU_PRSHIFT);
448 /* Strip off unused flags in either mode */
449 switch (bp->bp_protover) {
451 flags &= BSTP_PDU_STPMASK;
453 case BSTP_PROTO_RSTP:
454 flags &= BSTP_PDU_RSTPMASK;
461 bstp_input(struct bstp_port *bp, struct ifnet *ifp, struct mbuf *m)
463 struct bstp_state *bs = bp->bp_bs;
464 struct ether_header *eh;
465 struct bstp_tbpdu tpdu;
468 if (bp->bp_active == 0) {
475 eh = mtod(m, struct ether_header *);
477 len = ntohs(eh->ether_type);
478 if (len < sizeof(tpdu))
481 m_adj(m, ETHER_HDR_LEN);
483 if (m->m_pkthdr.len > len)
484 m_adj(m, len - m->m_pkthdr.len);
485 if (m->m_len < sizeof(tpdu) &&
486 (m = m_pullup(m, sizeof(tpdu))) == NULL)
489 memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu));
491 /* basic packet checks */
492 if (tpdu.tbu_dsap != LLC_8021D_LSAP ||
493 tpdu.tbu_ssap != LLC_8021D_LSAP ||
494 tpdu.tbu_ctl != LLC_UI)
496 if (tpdu.tbu_protoid != BSTP_PROTO_ID)
500 * We can treat later versions of the PDU as the same as the maximum
501 * version we implement. All additional parameters/flags are ignored.
503 if (tpdu.tbu_protover > BSTP_PROTO_MAX)
504 tpdu.tbu_protover = BSTP_PROTO_MAX;
506 if (tpdu.tbu_protover != bp->bp_protover) {
508 * Wait for the migration delay timer to expire before changing
509 * protocol version to avoid flip-flops.
511 if (bp->bp_flags & BSTP_PORT_CANMIGRATE)
512 bstp_set_port_proto(bp, tpdu.tbu_protover);
517 /* Clear operedge upon receiving a PDU on the port */
519 bstp_timer_start(&bp->bp_edge_delay_timer,
520 BSTP_DEFAULT_MIGRATE_DELAY);
522 switch (tpdu.tbu_protover) {
524 bstp_received_stp(bs, bp, &m, &tpdu);
527 case BSTP_PROTO_RSTP:
528 bstp_received_rstp(bs, bp, &m, &tpdu);
539 bstp_received_stp(struct bstp_state *bs, struct bstp_port *bp,
540 struct mbuf **mp, struct bstp_tbpdu *tpdu)
542 struct bstp_cbpdu cpdu;
543 struct bstp_config_unit *cu = &bp->bp_msg_cu;
544 struct bstp_tcn_unit tu;
546 switch (tpdu->tbu_bpdutype) {
547 case BSTP_MSGTYPE_TCN:
548 tu.tu_message_type = tpdu->tbu_bpdutype;
549 bstp_received_tcn(bs, bp, &tu);
551 case BSTP_MSGTYPE_CFG:
552 if ((*mp)->m_len < BSTP_BPDU_STP_LEN &&
553 (*mp = m_pullup(*mp, BSTP_BPDU_STP_LEN)) == NULL)
555 memcpy(&cpdu, mtod(*mp, caddr_t), BSTP_BPDU_STP_LEN);
557 bstp_decode_bpdu(bp, &cpdu, cu);
558 bstp_received_bpdu(bs, bp, cu);
564 bstp_received_rstp(struct bstp_state *bs, struct bstp_port *bp,
565 struct mbuf **mp, struct bstp_tbpdu *tpdu)
567 struct bstp_cbpdu cpdu;
568 struct bstp_config_unit *cu = &bp->bp_msg_cu;
570 if (tpdu->tbu_bpdutype != BSTP_MSGTYPE_RSTP)
573 if ((*mp)->m_len < BSTP_BPDU_RSTP_LEN &&
574 (*mp = m_pullup(*mp, BSTP_BPDU_RSTP_LEN)) == NULL)
576 memcpy(&cpdu, mtod(*mp, caddr_t), BSTP_BPDU_RSTP_LEN);
578 bstp_decode_bpdu(bp, &cpdu, cu);
579 bstp_received_bpdu(bs, bp, cu);
583 bstp_received_tcn(struct bstp_state *bs, struct bstp_port *bp,
584 struct bstp_tcn_unit *tcn)
591 bstp_received_bpdu(struct bstp_state *bs, struct bstp_port *bp,
592 struct bstp_config_unit *cu)
596 BSTP_LOCK_ASSERT(bs);
598 /* We need to have transitioned to INFO_MINE before proceeding */
599 switch (bp->bp_infois) {
600 case BSTP_INFO_DISABLED:
605 type = bstp_pdu_rcvtype(bp, cu);
608 case BSTP_PDU_SUPERIOR:
609 bs->bs_allsynced = 0;
611 bp->bp_proposing = 0;
613 if (cu->cu_proposal && cu->cu_forwarding == 0)
615 if (cu->cu_topology_change)
617 if (cu->cu_topology_change_ack)
621 !bstp_pdu_bettersame(bp, BSTP_INFO_RECIEVED))
624 /* copy the received priority and timers to the port */
625 bp->bp_port_pv = cu->cu_pv;
626 bp->bp_port_msg_age = cu->cu_message_age;
627 bp->bp_port_max_age = cu->cu_max_age;
628 bp->bp_port_fdelay = cu->cu_forward_delay;
630 (cu->cu_hello_time > BSTP_MIN_HELLO_TIME ?
631 cu->cu_hello_time : BSTP_MIN_HELLO_TIME);
633 /* set expiry for the new info */
634 bstp_set_timer_msgage(bp);
636 bp->bp_infois = BSTP_INFO_RECIEVED;
637 bstp_assign_roles(bs);
640 case BSTP_PDU_REPEATED:
641 if (cu->cu_proposal && cu->cu_forwarding == 0)
643 if (cu->cu_topology_change)
645 if (cu->cu_topology_change_ack)
648 /* rearm the age timer */
649 bstp_set_timer_msgage(bp);
652 case BSTP_PDU_INFERIOR:
653 if (cu->cu_learning) {
655 bp->bp_proposing = 0;
659 case BSTP_PDU_INFERIORALT:
661 * only point to point links are allowed fast
662 * transitions to forwarding.
664 if (cu->cu_agree && bp->bp_ptp_link) {
666 bp->bp_proposing = 0;
670 if (cu->cu_topology_change)
672 if (cu->cu_topology_change_ack)
677 return; /* do nothing */
679 /* update the state machines with the new data */
680 bstp_update_state(bs, bp);
684 bstp_pdu_rcvtype(struct bstp_port *bp, struct bstp_config_unit *cu)
688 /* default return type */
689 type = BSTP_PDU_OTHER;
691 switch (cu->cu_role) {
692 case BSTP_ROLE_DESIGNATED:
693 if (bstp_info_superior(&bp->bp_port_pv, &cu->cu_pv))
694 /* bpdu priority is superior */
695 type = BSTP_PDU_SUPERIOR;
696 else if (bstp_info_cmp(&bp->bp_port_pv, &cu->cu_pv) ==
698 if (bp->bp_port_msg_age != cu->cu_message_age ||
699 bp->bp_port_max_age != cu->cu_max_age ||
700 bp->bp_port_fdelay != cu->cu_forward_delay ||
701 bp->bp_port_htime != cu->cu_hello_time)
702 /* bpdu priority is equal and timers differ */
703 type = BSTP_PDU_SUPERIOR;
706 type = BSTP_PDU_REPEATED;
708 /* bpdu priority is worse */
709 type = BSTP_PDU_INFERIOR;
714 case BSTP_ROLE_ALTERNATE:
715 case BSTP_ROLE_BACKUP:
716 if (bstp_info_cmp(&bp->bp_port_pv, &cu->cu_pv) <= INFO_SAME)
718 * not a designated port and priority is the same or
721 type = BSTP_PDU_INFERIORALT;
729 bstp_pdu_bettersame(struct bstp_port *bp, int newinfo)
731 if (newinfo == BSTP_INFO_RECIEVED &&
732 bp->bp_infois == BSTP_INFO_RECIEVED &&
733 bstp_info_cmp(&bp->bp_port_pv, &bp->bp_msg_cu.cu_pv) >= INFO_SAME)
736 if (newinfo == BSTP_INFO_MINE &&
737 bp->bp_infois == BSTP_INFO_MINE &&
738 bstp_info_cmp(&bp->bp_port_pv, &bp->bp_desg_pv) >= INFO_SAME)
745 bstp_info_cmp(struct bstp_pri_vector *pv,
746 struct bstp_pri_vector *cpv)
748 if (cpv->pv_root_id < pv->pv_root_id)
749 return (INFO_BETTER);
750 if (cpv->pv_root_id > pv->pv_root_id)
753 if (cpv->pv_cost < pv->pv_cost)
754 return (INFO_BETTER);
755 if (cpv->pv_cost > pv->pv_cost)
758 if (cpv->pv_dbridge_id < pv->pv_dbridge_id)
759 return (INFO_BETTER);
760 if (cpv->pv_dbridge_id > pv->pv_dbridge_id)
763 if (cpv->pv_dport_id < pv->pv_dport_id)
764 return (INFO_BETTER);
765 if (cpv->pv_dport_id > pv->pv_dport_id)
772 * This message priority vector is superior to the port priority vector and
773 * will replace it if, and only if, the message priority vector is better than
774 * the port priority vector, or the message has been transmitted from the same
775 * designated bridge and designated port as the port priority vector.
778 bstp_info_superior(struct bstp_pri_vector *pv,
779 struct bstp_pri_vector *cpv)
781 if (bstp_info_cmp(pv, cpv) == INFO_BETTER ||
782 (bstp_same_bridgeid(pv->pv_dbridge_id, cpv->pv_dbridge_id) &&
783 (cpv->pv_dport_id & 0xfff) == (pv->pv_dport_id & 0xfff)))
789 bstp_assign_roles(struct bstp_state *bs)
791 struct bstp_port *bp, *rbp = NULL;
792 struct bstp_pri_vector pv;
794 /* default to our priority vector */
795 bs->bs_root_pv = bs->bs_bridge_pv;
796 bs->bs_root_msg_age = 0;
797 bs->bs_root_max_age = bs->bs_bridge_max_age;
798 bs->bs_root_fdelay = bs->bs_bridge_fdelay;
799 bs->bs_root_htime = bs->bs_bridge_htime;
800 bs->bs_root_port = NULL;
802 /* check if any recieved info supersedes us */
803 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
804 if (bp->bp_infois != BSTP_INFO_RECIEVED)
808 pv.pv_cost += bp->bp_path_cost;
811 * The root priority vector is the best of the set comprising
812 * the bridge priority vector plus all root path priority
813 * vectors whose bridge address is not equal to us.
815 if (bstp_same_bridgeid(pv.pv_dbridge_id,
816 bs->bs_bridge_pv.pv_dbridge_id) == 0 &&
817 bstp_info_cmp(&bs->bs_root_pv, &pv) == INFO_BETTER) {
818 /* the port vector replaces the root */
820 bs->bs_root_msg_age = bp->bp_port_msg_age +
821 BSTP_MESSAGE_AGE_INCR;
822 bs->bs_root_max_age = bp->bp_port_max_age;
823 bs->bs_root_fdelay = bp->bp_port_fdelay;
824 bs->bs_root_htime = bp->bp_port_htime;
829 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
830 /* calculate the port designated vector */
831 bp->bp_desg_pv.pv_root_id = bs->bs_root_pv.pv_root_id;
832 bp->bp_desg_pv.pv_cost = bs->bs_root_pv.pv_cost;
833 bp->bp_desg_pv.pv_dbridge_id = bs->bs_bridge_pv.pv_dbridge_id;
834 bp->bp_desg_pv.pv_dport_id = bp->bp_port_id;
835 bp->bp_desg_pv.pv_port_id = bp->bp_port_id;
837 /* calculate designated times */
838 bp->bp_desg_msg_age = bs->bs_root_msg_age;
839 bp->bp_desg_max_age = bs->bs_root_max_age;
840 bp->bp_desg_fdelay = bs->bs_root_fdelay;
841 bp->bp_desg_htime = bs->bs_bridge_htime;
844 switch (bp->bp_infois) {
845 case BSTP_INFO_DISABLED:
846 bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
850 bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
851 bstp_update_info(bp);
855 bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
856 /* update the port info if stale */
857 if (bstp_info_cmp(&bp->bp_port_pv,
858 &bp->bp_desg_pv) != INFO_SAME ||
860 (bp->bp_port_msg_age != rbp->bp_port_msg_age ||
861 bp->bp_port_max_age != rbp->bp_port_max_age ||
862 bp->bp_port_fdelay != rbp->bp_port_fdelay ||
863 bp->bp_port_htime != rbp->bp_port_htime)))
864 bstp_update_info(bp);
867 case BSTP_INFO_RECIEVED:
870 * root priority is derived from this
871 * port, make it the root port.
873 bstp_set_port_role(bp, BSTP_ROLE_ROOT);
874 bs->bs_root_port = bp;
875 } else if (bstp_info_cmp(&bp->bp_port_pv,
876 &bp->bp_desg_pv) == INFO_BETTER) {
878 * the port priority is lower than the root
881 bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
882 bstp_update_info(bp);
884 if (bstp_same_bridgeid(
885 bp->bp_port_pv.pv_dbridge_id,
886 bs->bs_bridge_pv.pv_dbridge_id)) {
888 * the designated bridge refers to
889 * another port on this bridge.
891 bstp_set_port_role(bp,
895 * the port is an inferior path to the
898 bstp_set_port_role(bp,
899 BSTP_ROLE_ALTERNATE);
908 bstp_update_state(struct bstp_state *bs, struct bstp_port *bp)
910 struct bstp_port *bp2;
913 BSTP_LOCK_ASSERT(bs);
915 /* check if all the ports have syncronised again */
916 if (!bs->bs_allsynced) {
918 LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
919 if (!(bp->bp_synced ||
920 bp->bp_role == BSTP_ROLE_ROOT)) {
925 bs->bs_allsynced = synced;
928 bstp_update_roles(bs, bp);
933 bstp_update_roles(struct bstp_state *bs, struct bstp_port *bp)
935 switch (bp->bp_role) {
936 case BSTP_ROLE_DISABLED:
937 /* Clear any flags if set */
938 if (bp->bp_sync || !bp->bp_synced || bp->bp_reroot) {
945 case BSTP_ROLE_ALTERNATE:
946 case BSTP_ROLE_BACKUP:
947 if ((bs->bs_allsynced && !bp->bp_agree) ||
948 (bp->bp_proposed && bp->bp_agree)) {
951 bp->bp_flags |= BSTP_PORT_NEWINFO;
952 DPRINTF("%s -> ALTERNATE_AGREED\n",
953 bp->bp_ifp->if_xname);
956 if (bp->bp_proposed && !bp->bp_agree) {
957 bstp_set_all_sync(bs);
959 DPRINTF("%s -> ALTERNATE_PROPOSED\n",
960 bp->bp_ifp->if_xname);
963 /* Clear any flags if set */
964 if (bp->bp_sync || !bp->bp_synced || bp->bp_reroot) {
968 DPRINTF("%s -> ALTERNATE_PORT\n", bp->bp_ifp->if_xname);
973 if (bp->bp_state != BSTP_IFSTATE_FORWARDING && !bp->bp_reroot) {
974 bstp_set_all_reroot(bs);
975 DPRINTF("%s -> ROOT_REROOT\n", bp->bp_ifp->if_xname);
978 if ((bs->bs_allsynced && !bp->bp_agree) ||
979 (bp->bp_proposed && bp->bp_agree)) {
983 bp->bp_flags |= BSTP_PORT_NEWINFO;
984 DPRINTF("%s -> ROOT_AGREED\n", bp->bp_ifp->if_xname);
987 if (bp->bp_proposed && !bp->bp_agree) {
988 bstp_set_all_sync(bs);
990 DPRINTF("%s -> ROOT_PROPOSED\n", bp->bp_ifp->if_xname);
993 if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
994 (bp->bp_forward_delay_timer.active == 0 ||
995 (bstp_rerooted(bs, bp) &&
996 bp->bp_recent_backup_timer.active == 0 &&
997 bp->bp_protover == BSTP_PROTO_RSTP))) {
998 switch (bp->bp_state) {
999 case BSTP_IFSTATE_DISCARDING:
1000 bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING);
1002 case BSTP_IFSTATE_LEARNING:
1003 bstp_set_port_state(bp,
1004 BSTP_IFSTATE_FORWARDING);
1009 if (bp->bp_state == BSTP_IFSTATE_FORWARDING && bp->bp_reroot) {
1011 DPRINTF("%s -> ROOT_REROOTED\n", bp->bp_ifp->if_xname);
1015 case BSTP_ROLE_DESIGNATED:
1016 if (bp->bp_recent_root_timer.active == 0 && bp->bp_reroot) {
1018 DPRINTF("%s -> DESIGNATED_RETIRED\n",
1019 bp->bp_ifp->if_xname);
1022 if ((bp->bp_state == BSTP_IFSTATE_DISCARDING &&
1023 !bp->bp_synced) || (bp->bp_agreed && !bp->bp_synced) ||
1024 (bp->bp_operedge && !bp->bp_synced) ||
1025 (bp->bp_sync && bp->bp_synced)) {
1026 bstp_timer_stop(&bp->bp_recent_root_timer);
1029 DPRINTF("%s -> DESIGNATED_SYNCED\n",
1030 bp->bp_ifp->if_xname);
1033 if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
1034 !bp->bp_agreed && !bp->bp_proposing &&
1036 bp->bp_proposing = 1;
1037 bp->bp_flags |= BSTP_PORT_NEWINFO;
1038 bstp_timer_start(&bp->bp_edge_delay_timer,
1039 (bp->bp_ptp_link ? BSTP_DEFAULT_MIGRATE_DELAY :
1040 bp->bp_desg_max_age));
1041 DPRINTF("%s -> DESIGNATED_PROPOSE\n",
1042 bp->bp_ifp->if_xname);
1045 if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
1046 (bp->bp_forward_delay_timer.active == 0 || bp->bp_agreed ||
1048 (bp->bp_recent_root_timer.active == 0 || !bp->bp_reroot) &&
1051 DPRINTF("%s -> AGREED\n", bp->bp_ifp->if_xname);
1053 * If agreed|operedge then go straight to forwarding,
1054 * otherwise follow discard -> learn -> forward.
1056 if (bp->bp_agreed || bp->bp_operedge ||
1057 bp->bp_state == BSTP_IFSTATE_LEARNING) {
1058 bstp_set_port_state(bp,
1059 BSTP_IFSTATE_FORWARDING);
1060 bp->bp_agreed = bp->bp_protover;
1061 } else if (bp->bp_state == BSTP_IFSTATE_DISCARDING)
1062 bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING);
1065 if (((bp->bp_sync && !bp->bp_synced) ||
1066 (bp->bp_reroot && bp->bp_recent_root_timer.active) ||
1067 (bp->bp_flags & BSTP_PORT_DISPUTED)) && !bp->bp_operedge &&
1068 bp->bp_state != BSTP_IFSTATE_DISCARDING) {
1069 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1070 bp->bp_flags &= ~BSTP_PORT_DISPUTED;
1071 bstp_timer_start(&bp->bp_forward_delay_timer,
1072 bp->bp_protover == BSTP_PROTO_RSTP ?
1073 bp->bp_desg_htime : bp->bp_desg_fdelay);
1074 DPRINTF("%s -> DESIGNATED_DISCARD\n",
1075 bp->bp_ifp->if_xname);
1080 if (bp->bp_flags & BSTP_PORT_NEWINFO)
1081 bstp_transmit(bs, bp);
1085 bstp_update_tc(struct bstp_port *bp)
1087 switch (bp->bp_tcstate) {
1088 case BSTP_TCSTATE_ACTIVE:
1089 if ((bp->bp_role != BSTP_ROLE_DESIGNATED &&
1090 bp->bp_role != BSTP_ROLE_ROOT) || bp->bp_operedge)
1091 bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1094 bstp_set_port_tc(bp, BSTP_TCSTATE_TCN);
1096 bstp_set_port_tc(bp, BSTP_TCSTATE_TC);
1098 if (bp->bp_tc_prop && !bp->bp_operedge)
1099 bstp_set_port_tc(bp, BSTP_TCSTATE_PROPAG);
1102 bstp_set_port_tc(bp, BSTP_TCSTATE_ACK);
1105 case BSTP_TCSTATE_INACTIVE:
1106 if ((bp->bp_state == BSTP_IFSTATE_LEARNING ||
1107 bp->bp_state == BSTP_IFSTATE_FORWARDING) &&
1108 bp->bp_fdbflush == 0)
1109 bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1112 case BSTP_TCSTATE_LEARNING:
1113 if (bp->bp_rcvdtc || bp->bp_rcvdtcn || bp->bp_rcvdtca ||
1115 bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1116 else if (bp->bp_role != BSTP_ROLE_DESIGNATED &&
1117 bp->bp_role != BSTP_ROLE_ROOT &&
1118 bp->bp_state == BSTP_IFSTATE_DISCARDING)
1119 bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
1121 if ((bp->bp_role == BSTP_ROLE_DESIGNATED ||
1122 bp->bp_role == BSTP_ROLE_ROOT) &&
1123 bp->bp_state == BSTP_IFSTATE_FORWARDING &&
1125 bstp_set_port_tc(bp, BSTP_TCSTATE_DETECTED);
1128 /* these are transient states and go straight back to ACTIVE */
1129 case BSTP_TCSTATE_DETECTED:
1130 case BSTP_TCSTATE_TCN:
1131 case BSTP_TCSTATE_TC:
1132 case BSTP_TCSTATE_PROPAG:
1133 case BSTP_TCSTATE_ACK:
1134 DPRINTF("Invalid TC state for %s\n",
1135 bp->bp_ifp->if_xname);
1142 bstp_update_info(struct bstp_port *bp)
1144 struct bstp_state *bs = bp->bp_bs;
1146 bp->bp_proposing = 0;
1147 bp->bp_proposed = 0;
1149 if (bp->bp_agreed && !bstp_pdu_bettersame(bp, BSTP_INFO_MINE))
1152 if (bp->bp_synced && !bp->bp_agreed) {
1154 bs->bs_allsynced = 0;
1157 /* copy the designated pv to the port */
1158 bp->bp_port_pv = bp->bp_desg_pv;
1159 bp->bp_port_msg_age = bp->bp_desg_msg_age;
1160 bp->bp_port_max_age = bp->bp_desg_max_age;
1161 bp->bp_port_fdelay = bp->bp_desg_fdelay;
1162 bp->bp_port_htime = bp->bp_desg_htime;
1163 bp->bp_infois = BSTP_INFO_MINE;
1165 /* Set transmit flag but do not immediately send */
1166 bp->bp_flags |= BSTP_PORT_NEWINFO;
1169 /* set tcprop on every port other than the caller */
1171 bstp_set_other_tcprop(struct bstp_port *bp)
1173 struct bstp_state *bs = bp->bp_bs;
1174 struct bstp_port *bp2;
1176 BSTP_LOCK_ASSERT(bs);
1178 LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
1186 bstp_set_all_reroot(struct bstp_state *bs)
1188 struct bstp_port *bp;
1190 BSTP_LOCK_ASSERT(bs);
1192 LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
1197 bstp_set_all_sync(struct bstp_state *bs)
1199 struct bstp_port *bp;
1201 BSTP_LOCK_ASSERT(bs);
1203 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1205 bp->bp_synced = 0; /* Not explicit in spec */
1208 bs->bs_allsynced = 0;
1212 bstp_set_port_state(struct bstp_port *bp, int state)
1214 if (bp->bp_state == state)
1217 bp->bp_state = state;
1219 switch (bp->bp_state) {
1220 case BSTP_IFSTATE_DISCARDING:
1221 DPRINTF("state changed to DISCARDING on %s\n",
1222 bp->bp_ifp->if_xname);
1225 case BSTP_IFSTATE_LEARNING:
1226 DPRINTF("state changed to LEARNING on %s\n",
1227 bp->bp_ifp->if_xname);
1229 bstp_timer_start(&bp->bp_forward_delay_timer,
1230 bp->bp_protover == BSTP_PROTO_RSTP ?
1231 bp->bp_desg_htime : bp->bp_desg_fdelay);
1234 case BSTP_IFSTATE_FORWARDING:
1235 DPRINTF("state changed to FORWARDING on %s\n",
1236 bp->bp_ifp->if_xname);
1238 bstp_timer_stop(&bp->bp_forward_delay_timer);
1239 /* Record that we enabled forwarding */
1240 bp->bp_forward_transitions++;
1244 /* notify the parent bridge */
1245 taskqueue_enqueue(taskqueue_swi, &bp->bp_statetask);
1249 bstp_set_port_role(struct bstp_port *bp, int role)
1251 struct bstp_state *bs = bp->bp_bs;
1253 if (bp->bp_role == role)
1256 /* perform pre-change tasks */
1257 switch (bp->bp_role) {
1258 case BSTP_ROLE_DISABLED:
1259 bstp_timer_start(&bp->bp_forward_delay_timer,
1260 bp->bp_desg_max_age);
1263 case BSTP_ROLE_BACKUP:
1264 bstp_timer_start(&bp->bp_recent_backup_timer,
1265 bp->bp_desg_htime * 2);
1267 case BSTP_ROLE_ALTERNATE:
1268 bstp_timer_start(&bp->bp_forward_delay_timer,
1269 bp->bp_desg_fdelay);
1275 case BSTP_ROLE_ROOT:
1276 bstp_timer_start(&bp->bp_recent_root_timer,
1277 BSTP_DEFAULT_FORWARD_DELAY);
1282 /* clear values not carried between roles */
1283 bp->bp_proposing = 0;
1284 bs->bs_allsynced = 0;
1286 /* initialise the new role */
1287 switch (bp->bp_role) {
1288 case BSTP_ROLE_DISABLED:
1289 case BSTP_ROLE_ALTERNATE:
1290 case BSTP_ROLE_BACKUP:
1291 DPRINTF("%s role -> ALT/BACK/DISABLED\n",
1292 bp->bp_ifp->if_xname);
1293 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1294 bstp_timer_stop(&bp->bp_recent_root_timer);
1295 bstp_timer_latch(&bp->bp_forward_delay_timer);
1301 case BSTP_ROLE_ROOT:
1302 DPRINTF("%s role -> ROOT\n",
1303 bp->bp_ifp->if_xname);
1304 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1305 bstp_timer_latch(&bp->bp_recent_root_timer);
1306 bp->bp_proposing = 0;
1309 case BSTP_ROLE_DESIGNATED:
1310 DPRINTF("%s role -> DESIGNATED\n",
1311 bp->bp_ifp->if_xname);
1312 bstp_timer_start(&bp->bp_hello_timer,
1318 /* let the TC state know that the role changed */
1323 bstp_set_port_proto(struct bstp_port *bp, int proto)
1325 struct bstp_state *bs = bp->bp_bs;
1327 /* supported protocol versions */
1329 case BSTP_PROTO_STP:
1330 /* we can downgrade protocols only */
1331 bstp_timer_stop(&bp->bp_migrate_delay_timer);
1332 /* clear unsupported features */
1333 bp->bp_operedge = 0;
1336 case BSTP_PROTO_RSTP:
1337 bstp_timer_start(&bp->bp_migrate_delay_timer,
1338 bs->bs_migration_delay);
1342 DPRINTF("Unsupported STP version %d\n", proto);
1346 bp->bp_protover = proto;
1347 bp->bp_flags &= ~BSTP_PORT_CANMIGRATE;
1351 bstp_set_port_tc(struct bstp_port *bp, int state)
1353 struct bstp_state *bs = bp->bp_bs;
1355 bp->bp_tcstate = state;
1357 /* initialise the new state */
1358 switch (bp->bp_tcstate) {
1359 case BSTP_TCSTATE_ACTIVE:
1360 DPRINTF("%s -> TC_ACTIVE\n", bp->bp_ifp->if_xname);
1364 case BSTP_TCSTATE_INACTIVE:
1365 bstp_timer_stop(&bp->bp_tc_timer);
1366 /* flush routes on the parent bridge */
1367 bp->bp_fdbflush = 1;
1368 taskqueue_enqueue(taskqueue_swi, &bp->bp_rtagetask);
1370 DPRINTF("%s -> TC_INACTIVE\n", bp->bp_ifp->if_xname);
1373 case BSTP_TCSTATE_LEARNING:
1378 DPRINTF("%s -> TC_LEARNING\n", bp->bp_ifp->if_xname);
1381 case BSTP_TCSTATE_DETECTED:
1382 bstp_set_timer_tc(bp);
1383 bstp_set_other_tcprop(bp);
1384 /* send out notification */
1385 bp->bp_flags |= BSTP_PORT_NEWINFO;
1386 bstp_transmit(bs, bp);
1387 getmicrotime(&bs->bs_last_tc_time);
1388 DPRINTF("%s -> TC_DETECTED\n", bp->bp_ifp->if_xname);
1389 bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1392 case BSTP_TCSTATE_TCN:
1393 bstp_set_timer_tc(bp);
1394 DPRINTF("%s -> TC_TCN\n", bp->bp_ifp->if_xname);
1396 case BSTP_TCSTATE_TC:
1399 if (bp->bp_role == BSTP_ROLE_DESIGNATED)
1402 bstp_set_other_tcprop(bp);
1403 DPRINTF("%s -> TC_TC\n", bp->bp_ifp->if_xname);
1404 bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1407 case BSTP_TCSTATE_PROPAG:
1408 /* flush routes on the parent bridge */
1409 bp->bp_fdbflush = 1;
1410 taskqueue_enqueue(taskqueue_swi, &bp->bp_rtagetask);
1412 bstp_set_timer_tc(bp);
1413 DPRINTF("%s -> TC_PROPAG\n", bp->bp_ifp->if_xname);
1414 bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1417 case BSTP_TCSTATE_ACK:
1418 bstp_timer_stop(&bp->bp_tc_timer);
1420 DPRINTF("%s -> TC_ACK\n", bp->bp_ifp->if_xname);
1421 bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1427 bstp_set_timer_tc(struct bstp_port *bp)
1429 struct bstp_state *bs = bp->bp_bs;
1431 if (bp->bp_tc_timer.active)
1434 switch (bp->bp_protover) {
1435 case BSTP_PROTO_RSTP:
1436 bstp_timer_start(&bp->bp_tc_timer,
1437 bp->bp_desg_htime + BSTP_TICK_VAL);
1438 bp->bp_flags |= BSTP_PORT_NEWINFO;
1441 case BSTP_PROTO_STP:
1442 bstp_timer_start(&bp->bp_tc_timer,
1443 bs->bs_root_max_age + bs->bs_root_fdelay);
1449 bstp_set_timer_msgage(struct bstp_port *bp)
1451 if (bp->bp_port_msg_age + BSTP_MESSAGE_AGE_INCR <=
1452 bp->bp_port_max_age) {
1453 bstp_timer_start(&bp->bp_message_age_timer,
1454 bp->bp_port_htime * 3);
1456 /* expires immediately */
1457 bstp_timer_start(&bp->bp_message_age_timer, 0);
1461 bstp_rerooted(struct bstp_state *bs, struct bstp_port *bp)
1463 struct bstp_port *bp2;
1466 LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
1469 if (bp2->bp_recent_root_timer.active) {
1478 bstp_set_htime(struct bstp_state *bs, int t)
1480 /* convert seconds to ticks */
1483 /* value can only be changed in leagacy stp mode */
1484 if (bs->bs_protover != BSTP_PROTO_STP)
1487 if (t < BSTP_MIN_HELLO_TIME || t > BSTP_MAX_HELLO_TIME)
1491 bs->bs_bridge_htime = t;
1498 bstp_set_fdelay(struct bstp_state *bs, int t)
1500 /* convert seconds to ticks */
1503 if (t < BSTP_MIN_FORWARD_DELAY || t > BSTP_MAX_FORWARD_DELAY)
1507 bs->bs_bridge_fdelay = t;
1514 bstp_set_maxage(struct bstp_state *bs, int t)
1516 /* convert seconds to ticks */
1519 if (t < BSTP_MIN_MAX_AGE || t > BSTP_MAX_MAX_AGE)
1523 bs->bs_bridge_max_age = t;
1530 bstp_set_holdcount(struct bstp_state *bs, int count)
1532 struct bstp_port *bp;
1534 if (count < BSTP_MIN_HOLD_COUNT ||
1535 count > BSTP_MAX_HOLD_COUNT)
1539 bs->bs_txholdcount = count;
1540 LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
1547 bstp_set_protocol(struct bstp_state *bs, int proto)
1549 struct bstp_port *bp;
1552 /* Supported protocol versions */
1553 case BSTP_PROTO_STP:
1554 case BSTP_PROTO_RSTP:
1562 bs->bs_protover = proto;
1563 bs->bs_bridge_htime = BSTP_DEFAULT_HELLO_TIME;
1564 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1566 bp->bp_infois = BSTP_INFO_DISABLED;
1568 bstp_set_port_proto(bp, bs->bs_protover);
1569 bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
1570 bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
1571 bstp_timer_stop(&bp->bp_recent_backup_timer);
1579 bstp_set_priority(struct bstp_state *bs, int pri)
1581 if (pri < 0 || pri > BSTP_MAX_PRIORITY)
1584 /* Limit to steps of 4096 */
1588 bs->bs_bridge_priority = pri;
1595 bstp_set_port_priority(struct bstp_port *bp, int pri)
1597 struct bstp_state *bs = bp->bp_bs;
1599 if (pri < 0 || pri > BSTP_MAX_PORT_PRIORITY)
1602 /* Limit to steps of 16 */
1606 bp->bp_priority = pri;
1613 bstp_set_path_cost(struct bstp_port *bp, uint32_t path_cost)
1615 struct bstp_state *bs = bp->bp_bs;
1617 if (path_cost > BSTP_MAX_PATH_COST)
1622 if (path_cost == 0) { /* use auto */
1623 bp->bp_flags &= ~BSTP_PORT_ADMCOST;
1624 bp->bp_path_cost = bstp_calc_path_cost(bp);
1626 bp->bp_path_cost = path_cost;
1627 bp->bp_flags |= BSTP_PORT_ADMCOST;
1635 bstp_set_edge(struct bstp_port *bp, int set)
1637 struct bstp_state *bs = bp->bp_bs;
1640 if ((bp->bp_operedge = set) == 0)
1641 bp->bp_flags &= ~BSTP_PORT_ADMEDGE;
1643 bp->bp_flags |= BSTP_PORT_ADMEDGE;
1649 bstp_set_autoedge(struct bstp_port *bp, int set)
1651 struct bstp_state *bs = bp->bp_bs;
1655 bp->bp_flags |= BSTP_PORT_AUTOEDGE;
1656 /* we may be able to transition straight to edge */
1657 if (bp->bp_edge_delay_timer.active == 0)
1658 bstp_edge_delay_expiry(bs, bp);
1660 bp->bp_flags &= ~BSTP_PORT_AUTOEDGE;
1666 bstp_set_ptp(struct bstp_port *bp, int set)
1668 struct bstp_state *bs = bp->bp_bs;
1671 bp->bp_ptp_link = set;
1677 bstp_set_autoptp(struct bstp_port *bp, int set)
1679 struct bstp_state *bs = bp->bp_bs;
1683 bp->bp_flags |= BSTP_PORT_AUTOPTP;
1684 bstp_ifupdstatus(bs, bp);
1686 bp->bp_flags &= ~BSTP_PORT_AUTOPTP;
1692 * Calculate the path cost according to the link speed.
1695 bstp_calc_path_cost(struct bstp_port *bp)
1697 struct ifnet *ifp = bp->bp_ifp;
1700 /* If the priority has been manually set then retain the value */
1701 if (bp->bp_flags & BSTP_PORT_ADMCOST)
1702 return bp->bp_path_cost;
1704 if (ifp->if_baudrate < 1000)
1705 return (BSTP_DEFAULT_PATH_COST);
1707 /* formula from section 17.14, IEEE Std 802.1D-2004 */
1708 path_cost = 20000000000ULL / (ifp->if_baudrate / 1000);
1710 if (path_cost > BSTP_MAX_PATH_COST)
1711 path_cost = BSTP_MAX_PATH_COST;
1713 /* STP compat mode only uses 16 bits of the 32 */
1714 if (bp->bp_protover == BSTP_PROTO_STP && path_cost > 65535)
1721 * Notify the bridge that a port state has changed, we need to do this from a
1722 * taskqueue to avoid a LOR.
1725 bstp_notify_state(void *arg, int pending)
1727 struct bstp_port *bp = (struct bstp_port *)arg;
1728 struct bstp_state *bs = bp->bp_bs;
1730 if (bp->bp_active == 1 && bs->bs_state_cb != NULL)
1731 (*bs->bs_state_cb)(bp->bp_ifp, bp->bp_state);
1735 * Flush the routes on the bridge port, we need to do this from a
1736 * taskqueue to avoid a LOR.
1739 bstp_notify_rtage(void *arg, int pending)
1741 struct bstp_port *bp = (struct bstp_port *)arg;
1742 struct bstp_state *bs = bp->bp_bs;
1746 switch (bp->bp_protover) {
1747 case BSTP_PROTO_STP:
1748 /* convert to seconds */
1749 age = bp->bp_desg_fdelay / BSTP_TICK_VAL;
1752 case BSTP_PROTO_RSTP:
1758 if (bp->bp_active == 1 && bs->bs_rtage_cb != NULL)
1759 (*bs->bs_rtage_cb)(bp->bp_ifp, age);
1761 /* flush is complete */
1763 bp->bp_fdbflush = 0;
1768 bstp_linkstate(struct ifnet *ifp, int state)
1770 struct bstp_state *bs;
1771 struct bstp_port *bp;
1773 /* search for the stp port */
1774 mtx_lock(&bstp_list_mtx);
1775 LIST_FOREACH(bs, &bstp_list, bs_list) {
1777 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1778 if (bp->bp_ifp == ifp) {
1779 bstp_ifupdstatus(bs, bp);
1780 bstp_update_state(bs, bp);
1781 /* it only exists once so return */
1783 mtx_unlock(&bstp_list_mtx);
1789 mtx_unlock(&bstp_list_mtx);
1793 bstp_ifupdstatus(struct bstp_state *bs, struct bstp_port *bp)
1795 struct ifnet *ifp = bp->bp_ifp;
1796 struct ifmediareq ifmr;
1799 BSTP_LOCK_ASSERT(bs);
1801 bzero((char *)&ifmr, sizeof(ifmr));
1802 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
1804 if ((error == 0) && (ifp->if_flags & IFF_UP)) {
1805 if (ifmr.ifm_status & IFM_ACTIVE) {
1806 /* A full-duplex link is assumed to be point to point */
1807 if (bp->bp_flags & BSTP_PORT_AUTOPTP) {
1809 ifmr.ifm_active & IFM_FDX ? 1 : 0;
1812 if (bp->bp_role == BSTP_ROLE_DISABLED)
1813 bstp_enable_port(bs, bp);
1815 if (bp->bp_role != BSTP_ROLE_DISABLED) {
1816 bstp_disable_port(bs, bp);
1817 if ((bp->bp_flags & BSTP_PORT_ADMEDGE) &&
1818 bp->bp_protover == BSTP_PROTO_RSTP)
1819 bp->bp_operedge = 1;
1825 if (bp->bp_infois != BSTP_INFO_DISABLED)
1826 bstp_disable_port(bs, bp);
1830 bstp_enable_port(struct bstp_state *bs, struct bstp_port *bp)
1832 bp->bp_infois = BSTP_INFO_AGED;
1833 bstp_assign_roles(bs);
1837 bstp_disable_port(struct bstp_state *bs, struct bstp_port *bp)
1839 bp->bp_infois = BSTP_INFO_DISABLED;
1840 bstp_assign_roles(bs);
1844 bstp_tick(void *arg)
1846 struct bstp_state *bs = arg;
1847 struct bstp_port *bp;
1849 BSTP_LOCK_ASSERT(bs);
1851 if (bs->bs_running == 0)
1854 /* slow timer to catch missed link events */
1855 if (bstp_timer_expired(&bs->bs_link_timer)) {
1856 LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
1857 bstp_ifupdstatus(bs, bp);
1858 bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER);
1861 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1862 /* no events need to happen for these */
1863 bstp_timer_expired(&bp->bp_tc_timer);
1864 bstp_timer_expired(&bp->bp_recent_root_timer);
1865 bstp_timer_expired(&bp->bp_forward_delay_timer);
1866 bstp_timer_expired(&bp->bp_recent_backup_timer);
1868 if (bstp_timer_expired(&bp->bp_hello_timer))
1869 bstp_hello_timer_expiry(bs, bp);
1871 if (bstp_timer_expired(&bp->bp_message_age_timer))
1872 bstp_message_age_expiry(bs, bp);
1874 if (bstp_timer_expired(&bp->bp_migrate_delay_timer))
1875 bstp_migrate_delay_expiry(bs, bp);
1877 if (bstp_timer_expired(&bp->bp_edge_delay_timer))
1878 bstp_edge_delay_expiry(bs, bp);
1880 /* update the various state machines for the port */
1881 bstp_update_state(bs, bp);
1883 if (bp->bp_txcount > 0)
1887 callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
1891 bstp_timer_start(struct bstp_timer *t, uint16_t v)
1899 bstp_timer_stop(struct bstp_timer *t)
1907 bstp_timer_latch(struct bstp_timer *t)
1914 bstp_timer_expired(struct bstp_timer *t)
1916 if (t->active == 0 || t->latched)
1918 t->value -= BSTP_TICK_VAL;
1919 if (t->value <= 0) {
1927 bstp_hello_timer_expiry(struct bstp_state *bs, struct bstp_port *bp)
1929 if ((bp->bp_flags & BSTP_PORT_NEWINFO) ||
1930 bp->bp_role == BSTP_ROLE_DESIGNATED ||
1931 (bp->bp_role == BSTP_ROLE_ROOT &&
1932 bp->bp_tc_timer.active == 1)) {
1933 bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime);
1934 bp->bp_flags |= BSTP_PORT_NEWINFO;
1935 bstp_transmit(bs, bp);
1940 bstp_message_age_expiry(struct bstp_state *bs, struct bstp_port *bp)
1942 if (bp->bp_infois == BSTP_INFO_RECIEVED) {
1943 bp->bp_infois = BSTP_INFO_AGED;
1944 bstp_assign_roles(bs);
1945 DPRINTF("aged info on %s\n", bp->bp_ifp->if_xname);
1950 bstp_migrate_delay_expiry(struct bstp_state *bs, struct bstp_port *bp)
1952 bp->bp_flags |= BSTP_PORT_CANMIGRATE;
1956 bstp_edge_delay_expiry(struct bstp_state *bs, struct bstp_port *bp)
1958 if ((bp->bp_flags & BSTP_PORT_AUTOEDGE) &&
1959 bp->bp_protover == BSTP_PROTO_RSTP && bp->bp_proposing &&
1960 bp->bp_role == BSTP_ROLE_DESIGNATED) {
1961 bp->bp_operedge = 1;
1962 DPRINTF("%s -> edge port\n", bp->bp_ifp->if_xname);
1967 bstp_addr_cmp(const uint8_t *a, const uint8_t *b)
1971 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
1972 d = ((int)a[i]) - ((int)b[i]);
1979 * compare the bridge address component of the bridgeid
1982 bstp_same_bridgeid(uint64_t id1, uint64_t id2)
1984 u_char addr1[ETHER_ADDR_LEN];
1985 u_char addr2[ETHER_ADDR_LEN];
1987 PV2ADDR(id1, addr1);
1988 PV2ADDR(id2, addr2);
1990 if (bstp_addr_cmp(addr1, addr2) == 0)
1997 bstp_reinit(struct bstp_state *bs)
1999 struct bstp_port *bp;
2000 struct ifnet *ifp, *mif;
2003 BSTP_LOCK_ASSERT(bs);
2005 if (LIST_EMPTY(&bs->bs_bplist)) {
2006 callout_stop(&bs->bs_bstpcallout);
2012 * Search through the Ethernet adapters and find the one with the
2013 * lowest value. The adapter which we take the MAC address from does
2014 * not need to be part of the bridge, it just needs to be a unique
2015 * value. It is not possible for mif to be null, at this point we have
2016 * at least one stp port and hence at least one NIC.
2019 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2020 if (ifp->if_type != IFT_ETHER)
2027 if (bstp_addr_cmp(IF_LLADDR(ifp), IF_LLADDR(mif)) < 0) {
2034 e_addr = IF_LLADDR(mif);
2035 bs->bs_bridge_pv.pv_dbridge_id =
2036 (((uint64_t)bs->bs_bridge_priority) << 48) |
2037 (((uint64_t)e_addr[0]) << 40) |
2038 (((uint64_t)e_addr[1]) << 32) |
2039 (((uint64_t)e_addr[2]) << 24) |
2040 (((uint64_t)e_addr[3]) << 16) |
2041 (((uint64_t)e_addr[4]) << 8) |
2042 (((uint64_t)e_addr[5]));
2044 bs->bs_bridge_pv.pv_root_id = bs->bs_bridge_pv.pv_dbridge_id;
2045 bs->bs_bridge_pv.pv_cost = 0;
2046 bs->bs_bridge_pv.pv_dport_id = 0;
2047 bs->bs_bridge_pv.pv_port_id = 0;
2049 if (bs->bs_running && callout_pending(&bs->bs_bstpcallout) == 0)
2050 callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
2052 LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
2053 bp->bp_port_id = (bp->bp_priority << 8) |
2054 (bp->bp_ifp->if_index & 0xfff);
2055 bstp_ifupdstatus(bs, bp);
2058 bstp_assign_roles(bs);
2059 bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER);
2063 bstp_modevent(module_t mod, int type, void *data)
2067 mtx_init(&bstp_list_mtx, "bridgestp list", NULL, MTX_DEF);
2068 LIST_INIT(&bstp_list);
2069 bstp_linkstate_p = bstp_linkstate;
2072 mtx_destroy(&bstp_list_mtx);
2075 return (EOPNOTSUPP);
2080 static moduledata_t bstp_mod = {
2086 DECLARE_MODULE(bridgestp, bstp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2087 MODULE_VERSION(bridgestp, 1);
2090 bstp_attach(struct bstp_state *bs, bstp_state_cb_t state_callback,
2091 bstp_rtage_cb_t rtage_callback)
2094 callout_init_mtx(&bs->bs_bstpcallout, &bs->bs_mtx, 0);
2095 LIST_INIT(&bs->bs_bplist);
2097 bs->bs_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
2098 bs->bs_bridge_htime = BSTP_DEFAULT_HELLO_TIME;
2099 bs->bs_bridge_fdelay = BSTP_DEFAULT_FORWARD_DELAY;
2100 bs->bs_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
2101 bs->bs_hold_time = BSTP_DEFAULT_HOLD_TIME;
2102 bs->bs_migration_delay = BSTP_DEFAULT_MIGRATE_DELAY;
2103 bs->bs_txholdcount = BSTP_DEFAULT_HOLD_COUNT;
2104 bs->bs_protover = BSTP_PROTO_RSTP;
2105 bs->bs_state_cb = state_callback;
2106 bs->bs_rtage_cb = rtage_callback;
2108 getmicrotime(&bs->bs_last_tc_time);
2110 mtx_lock(&bstp_list_mtx);
2111 LIST_INSERT_HEAD(&bstp_list, bs, bs_list);
2112 mtx_unlock(&bstp_list_mtx);
2116 bstp_detach(struct bstp_state *bs)
2118 KASSERT(LIST_EMPTY(&bs->bs_bplist), ("bstp still active"));
2120 mtx_lock(&bstp_list_mtx);
2121 LIST_REMOVE(bs, bs_list);
2122 mtx_unlock(&bstp_list_mtx);
2123 callout_drain(&bs->bs_bstpcallout);
2124 BSTP_LOCK_DESTROY(bs);
2128 bstp_init(struct bstp_state *bs)
2131 callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
2138 bstp_stop(struct bstp_state *bs)
2140 struct bstp_port *bp;
2144 LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
2145 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
2148 callout_stop(&bs->bs_bstpcallout);
2153 bstp_create(struct bstp_state *bs, struct bstp_port *bp, struct ifnet *ifp)
2155 bzero(bp, sizeof(struct bstp_port));
2160 bp->bp_priority = BSTP_DEFAULT_PORT_PRIORITY;
2161 TASK_INIT(&bp->bp_statetask, 0, bstp_notify_state, bp);
2162 TASK_INIT(&bp->bp_rtagetask, 0, bstp_notify_rtage, bp);
2165 bp->bp_infois = BSTP_INFO_DISABLED;
2166 bp->bp_flags = BSTP_PORT_AUTOEDGE|BSTP_PORT_AUTOPTP;
2167 bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
2168 bstp_set_port_proto(bp, bs->bs_protover);
2169 bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
2170 bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
2171 bp->bp_path_cost = bstp_calc_path_cost(bp);
2177 bstp_enable(struct bstp_port *bp)
2179 struct bstp_state *bs = bp->bp_bs;
2180 struct ifnet *ifp = bp->bp_ifp;
2182 KASSERT(bp->bp_active == 0, ("already a bstp member"));
2184 switch (ifp->if_type) {
2185 case IFT_ETHER: /* These can do spanning tree. */
2188 /* Nothing else can. */
2193 LIST_INSERT_HEAD(&bs->bs_bplist, bp, bp_next);
2195 bp->bp_flags |= BSTP_PORT_NEWINFO;
2197 bstp_update_roles(bs, bp);
2203 bstp_disable(struct bstp_port *bp)
2205 struct bstp_state *bs = bp->bp_bs;
2207 KASSERT(bp->bp_active == 1, ("not a bstp member"));
2210 bstp_disable_port(bs, bp);
2211 LIST_REMOVE(bp, bp_next);
2218 * The bstp_port structure is about to be freed by the parent bridge.
2221 bstp_destroy(struct bstp_port *bp)
2223 KASSERT(bp->bp_active == 0, ("port is still attached"));
2224 taskqueue_drain(taskqueue_swi, &bp->bp_statetask);
2225 taskqueue_drain(taskqueue_swi, &bp->bp_rtagetask);