xref: /freebsd/sys/net/bridgestp.c (revision 0e4025bf)
1 /*	$NetBSD: bridgestp.c,v 1.5 2003/11/28 08:56:48 keihan Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
7  * Copyright (c) 2006 Andrew Thompson (thompsa@FreeBSD.org)
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  *
31  * OpenBSD: bridgestp.c,v 1.5 2001/03/22 03:48:29 jason Exp
32  */
33 
34 /*
35  * Implementation of the spanning tree protocol as defined in
36  * ISO/IEC 802.1D-2004, June 9, 2004.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/callout.h>
50 #include <sys/module.h>
51 #include <sys/proc.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/taskqueue.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/if_llc.h>
61 #include <net/if_media.h>
62 #include <net/vnet.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
68 #include <net/bridgestp.h>
69 
70 #ifdef	BRIDGESTP_DEBUG
71 #define	DPRINTF(fmt, arg...)	printf("bstp: " fmt, ##arg)
72 #else
73 #define	DPRINTF(fmt, arg...)	(void)0
74 #endif
75 
76 #define	PV2ADDR(pv, eaddr)	do {		\
77 	eaddr[0] = pv >> 40;			\
78 	eaddr[1] = pv >> 32;			\
79 	eaddr[2] = pv >> 24;			\
80 	eaddr[3] = pv >> 16;			\
81 	eaddr[4] = pv >> 8;			\
82 	eaddr[5] = pv >> 0;			\
83 } while (0)
84 
85 #define	INFO_BETTER	1
86 #define	INFO_SAME	0
87 #define	INFO_WORSE	-1
88 
89 const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
90 
91 LIST_HEAD(, bstp_state) bstp_list;
92 static struct mtx	bstp_list_mtx;
93 
94 static void	bstp_transmit(struct bstp_state *, struct bstp_port *);
95 static void	bstp_transmit_bpdu(struct bstp_state *, struct bstp_port *);
96 static void	bstp_transmit_tcn(struct bstp_state *, struct bstp_port *);
97 static void	bstp_decode_bpdu(struct bstp_port *, struct bstp_cbpdu *,
98 		    struct bstp_config_unit *);
99 static void	bstp_send_bpdu(struct bstp_state *, struct bstp_port *,
100 		    struct bstp_cbpdu *);
101 static int	bstp_pdu_flags(struct bstp_port *);
102 static void	bstp_received_stp(struct bstp_state *, struct bstp_port *,
103 		    struct mbuf **, struct bstp_tbpdu *);
104 static void	bstp_received_rstp(struct bstp_state *, struct bstp_port *,
105 		    struct mbuf **, struct bstp_tbpdu *);
106 static void	bstp_received_tcn(struct bstp_state *, struct bstp_port *,
107 		    struct bstp_tcn_unit *);
108 static void	bstp_received_bpdu(struct bstp_state *, struct bstp_port *,
109 		    struct bstp_config_unit *);
110 static int	bstp_pdu_rcvtype(struct bstp_port *, struct bstp_config_unit *);
111 static int	bstp_pdu_bettersame(struct bstp_port *, int);
112 static int	bstp_info_cmp(struct bstp_pri_vector *,
113 		    struct bstp_pri_vector *);
114 static int	bstp_info_superior(struct bstp_pri_vector *,
115 		    struct bstp_pri_vector *);
116 static void	bstp_assign_roles(struct bstp_state *);
117 static void	bstp_update_roles(struct bstp_state *, struct bstp_port *);
118 static void	bstp_update_state(struct bstp_state *, struct bstp_port *);
119 static void	bstp_update_tc(struct bstp_port *);
120 static void	bstp_update_info(struct bstp_port *);
121 static void	bstp_set_other_tcprop(struct bstp_port *);
122 static void	bstp_set_all_reroot(struct bstp_state *);
123 static void	bstp_set_all_sync(struct bstp_state *);
124 static void	bstp_set_port_state(struct bstp_port *, int);
125 static void	bstp_set_port_role(struct bstp_port *, int);
126 static void	bstp_set_port_proto(struct bstp_port *, int);
127 static void	bstp_set_port_tc(struct bstp_port *, int);
128 static void	bstp_set_timer_tc(struct bstp_port *);
129 static void	bstp_set_timer_msgage(struct bstp_port *);
130 static int	bstp_rerooted(struct bstp_state *, struct bstp_port *);
131 static uint32_t	bstp_calc_path_cost(struct bstp_port *);
132 static void	bstp_notify_state(void *, int);
133 static void	bstp_notify_rtage(void *, int);
134 static void	bstp_ifupdstatus(void *, int);
135 static void	bstp_enable_port(struct bstp_state *, struct bstp_port *);
136 static void	bstp_disable_port(struct bstp_state *, struct bstp_port *);
137 static void	bstp_tick(void *);
138 static void	bstp_timer_start(struct bstp_timer *, uint16_t);
139 static void	bstp_timer_stop(struct bstp_timer *);
140 static void	bstp_timer_latch(struct bstp_timer *);
141 static int	bstp_timer_dectest(struct bstp_timer *);
142 static void	bstp_hello_timer_expiry(struct bstp_state *,
143 		    struct bstp_port *);
144 static void	bstp_message_age_expiry(struct bstp_state *,
145 		    struct bstp_port *);
146 static void	bstp_migrate_delay_expiry(struct bstp_state *,
147 		    struct bstp_port *);
148 static void	bstp_edge_delay_expiry(struct bstp_state *,
149 		    struct bstp_port *);
150 static int	bstp_addr_cmp(const uint8_t *, const uint8_t *);
151 static int	bstp_same_bridgeid(uint64_t, uint64_t);
152 static void	bstp_reinit(struct bstp_state *);
153 
154 static void
155 bstp_transmit(struct bstp_state *bs, struct bstp_port *bp)
156 {
157 	NET_EPOCH_ASSERT();
158 
159 	if (bs->bs_running == 0)
160 		return;
161 
162 	/*
163 	 * a PDU can only be sent if we have tx quota left and the
164 	 * hello timer is running.
165 	 */
166 	if (bp->bp_hello_timer.active == 0) {
167 		/* Test if it needs to be reset */
168 		bstp_hello_timer_expiry(bs, bp);
169 		return;
170 	}
171 	if (bp->bp_txcount > bs->bs_txholdcount)
172 		/* Ran out of karma */
173 		return;
174 
175 	if (bp->bp_protover == BSTP_PROTO_RSTP) {
176 		bstp_transmit_bpdu(bs, bp);
177 		bp->bp_tc_ack = 0;
178 	} else { /* STP */
179 		switch (bp->bp_role) {
180 			case BSTP_ROLE_DESIGNATED:
181 				bstp_transmit_bpdu(bs, bp);
182 				bp->bp_tc_ack = 0;
183 				break;
184 
185 			case BSTP_ROLE_ROOT:
186 				bstp_transmit_tcn(bs, bp);
187 				break;
188 		}
189 	}
190 	bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime);
191 	bp->bp_flags &= ~BSTP_PORT_NEWINFO;
192 }
193 
194 static void
195 bstp_transmit_bpdu(struct bstp_state *bs, struct bstp_port *bp)
196 {
197 	struct bstp_cbpdu bpdu;
198 
199 	BSTP_LOCK_ASSERT(bs);
200 
201 	bpdu.cbu_rootpri = htons(bp->bp_desg_pv.pv_root_id >> 48);
202 	PV2ADDR(bp->bp_desg_pv.pv_root_id, bpdu.cbu_rootaddr);
203 
204 	bpdu.cbu_rootpathcost = htonl(bp->bp_desg_pv.pv_cost);
205 
206 	bpdu.cbu_bridgepri = htons(bp->bp_desg_pv.pv_dbridge_id >> 48);
207 	PV2ADDR(bp->bp_desg_pv.pv_dbridge_id, bpdu.cbu_bridgeaddr);
208 
209 	bpdu.cbu_portid = htons(bp->bp_port_id);
210 	bpdu.cbu_messageage = htons(bp->bp_desg_msg_age);
211 	bpdu.cbu_maxage = htons(bp->bp_desg_max_age);
212 	bpdu.cbu_hellotime = htons(bp->bp_desg_htime);
213 	bpdu.cbu_forwarddelay = htons(bp->bp_desg_fdelay);
214 
215 	bpdu.cbu_flags = bstp_pdu_flags(bp);
216 
217 	switch (bp->bp_protover) {
218 		case BSTP_PROTO_STP:
219 			bpdu.cbu_bpdutype = BSTP_MSGTYPE_CFG;
220 			break;
221 
222 		case BSTP_PROTO_RSTP:
223 			bpdu.cbu_bpdutype = BSTP_MSGTYPE_RSTP;
224 			break;
225 	}
226 
227 	bstp_send_bpdu(bs, bp, &bpdu);
228 }
229 
230 static void
231 bstp_transmit_tcn(struct bstp_state *bs, struct bstp_port *bp)
232 {
233 	struct bstp_tbpdu bpdu;
234 	struct ifnet *ifp = bp->bp_ifp;
235 	struct ether_header *eh;
236 	struct mbuf *m;
237 
238 	KASSERT(bp == bs->bs_root_port, ("%s: bad root port\n", __func__));
239 
240 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
241 		return;
242 
243 	m = m_gethdr(M_NOWAIT, MT_DATA);
244 	if (m == NULL)
245 		return;
246 
247 	m->m_pkthdr.rcvif = ifp;
248 	m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
249 	m->m_len = m->m_pkthdr.len;
250 
251 	eh = mtod(m, struct ether_header *);
252 
253 	memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
254 	memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
255 	eh->ether_type = htons(sizeof(bpdu));
256 
257 	bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP;
258 	bpdu.tbu_ctl = LLC_UI;
259 	bpdu.tbu_protoid = 0;
260 	bpdu.tbu_protover = 0;
261 	bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN;
262 
263 	memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
264 
265 	bp->bp_txcount++;
266 	ifp->if_transmit(ifp, m);
267 }
268 
269 static void
270 bstp_decode_bpdu(struct bstp_port *bp, struct bstp_cbpdu *cpdu,
271     struct bstp_config_unit *cu)
272 {
273 	int flags;
274 
275 	cu->cu_pv.pv_root_id =
276 	    (((uint64_t)ntohs(cpdu->cbu_rootpri)) << 48) |
277 	    (((uint64_t)cpdu->cbu_rootaddr[0]) << 40) |
278 	    (((uint64_t)cpdu->cbu_rootaddr[1]) << 32) |
279 	    (((uint64_t)cpdu->cbu_rootaddr[2]) << 24) |
280 	    (((uint64_t)cpdu->cbu_rootaddr[3]) << 16) |
281 	    (((uint64_t)cpdu->cbu_rootaddr[4]) << 8) |
282 	    (((uint64_t)cpdu->cbu_rootaddr[5]) << 0);
283 
284 	cu->cu_pv.pv_dbridge_id =
285 	    (((uint64_t)ntohs(cpdu->cbu_bridgepri)) << 48) |
286 	    (((uint64_t)cpdu->cbu_bridgeaddr[0]) << 40) |
287 	    (((uint64_t)cpdu->cbu_bridgeaddr[1]) << 32) |
288 	    (((uint64_t)cpdu->cbu_bridgeaddr[2]) << 24) |
289 	    (((uint64_t)cpdu->cbu_bridgeaddr[3]) << 16) |
290 	    (((uint64_t)cpdu->cbu_bridgeaddr[4]) << 8) |
291 	    (((uint64_t)cpdu->cbu_bridgeaddr[5]) << 0);
292 
293 	cu->cu_pv.pv_cost = ntohl(cpdu->cbu_rootpathcost);
294 	cu->cu_message_age = ntohs(cpdu->cbu_messageage);
295 	cu->cu_max_age = ntohs(cpdu->cbu_maxage);
296 	cu->cu_hello_time = ntohs(cpdu->cbu_hellotime);
297 	cu->cu_forward_delay = ntohs(cpdu->cbu_forwarddelay);
298 	cu->cu_pv.pv_dport_id = ntohs(cpdu->cbu_portid);
299 	cu->cu_pv.pv_port_id = bp->bp_port_id;
300 	cu->cu_message_type = cpdu->cbu_bpdutype;
301 
302 	/* Strip off unused flags in STP mode */
303 	flags = cpdu->cbu_flags;
304 	switch (cpdu->cbu_protover) {
305 		case BSTP_PROTO_STP:
306 			flags &= BSTP_PDU_STPMASK;
307 			/* A STP BPDU explicitly conveys a Designated Port */
308 			cu->cu_role = BSTP_ROLE_DESIGNATED;
309 			break;
310 
311 		case BSTP_PROTO_RSTP:
312 			flags &= BSTP_PDU_RSTPMASK;
313 			break;
314 	}
315 
316 	cu->cu_topology_change_ack =
317 		(flags & BSTP_PDU_F_TCA) ? 1 : 0;
318 	cu->cu_proposal =
319 		(flags & BSTP_PDU_F_P) ? 1 : 0;
320 	cu->cu_agree =
321 		(flags & BSTP_PDU_F_A) ? 1 : 0;
322 	cu->cu_learning =
323 		(flags & BSTP_PDU_F_L) ? 1 : 0;
324 	cu->cu_forwarding =
325 		(flags & BSTP_PDU_F_F) ? 1 : 0;
326 	cu->cu_topology_change =
327 		(flags & BSTP_PDU_F_TC) ? 1 : 0;
328 
329 	switch ((flags & BSTP_PDU_PRMASK) >> BSTP_PDU_PRSHIFT) {
330 		case BSTP_PDU_F_ROOT:
331 			cu->cu_role = BSTP_ROLE_ROOT;
332 			break;
333 		case BSTP_PDU_F_ALT:
334 			cu->cu_role = BSTP_ROLE_ALTERNATE;
335 			break;
336 		case BSTP_PDU_F_DESG:
337 			cu->cu_role = BSTP_ROLE_DESIGNATED;
338 			break;
339 	}
340 }
341 
342 static void
343 bstp_send_bpdu(struct bstp_state *bs, struct bstp_port *bp,
344     struct bstp_cbpdu *bpdu)
345 {
346 	struct ifnet *ifp;
347 	struct mbuf *m;
348 	struct ether_header *eh;
349 
350 	BSTP_LOCK_ASSERT(bs);
351 	NET_EPOCH_ASSERT();
352 
353 	ifp = bp->bp_ifp;
354 
355 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
356 		return;
357 
358 	m = m_gethdr(M_NOWAIT, MT_DATA);
359 	if (m == NULL)
360 		return;
361 
362 	eh = mtod(m, struct ether_header *);
363 
364 	bpdu->cbu_ssap = bpdu->cbu_dsap = LLC_8021D_LSAP;
365 	bpdu->cbu_ctl = LLC_UI;
366 	bpdu->cbu_protoid = htons(BSTP_PROTO_ID);
367 
368 	memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
369 	memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
370 
371 	switch (bpdu->cbu_bpdutype) {
372 		case BSTP_MSGTYPE_CFG:
373 			bpdu->cbu_protover = BSTP_PROTO_STP;
374 			m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_STP_LEN;
375 			eh->ether_type = htons(BSTP_BPDU_STP_LEN);
376 			memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu,
377 			    BSTP_BPDU_STP_LEN);
378 			break;
379 
380 		case BSTP_MSGTYPE_RSTP:
381 			bpdu->cbu_protover = BSTP_PROTO_RSTP;
382 			bpdu->cbu_versionlen = htons(0);
383 			m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_RSTP_LEN;
384 			eh->ether_type = htons(BSTP_BPDU_RSTP_LEN);
385 			memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu,
386 			    BSTP_BPDU_RSTP_LEN);
387 			break;
388 
389 		default:
390 			panic("not implemented");
391 	}
392 	m->m_pkthdr.rcvif = ifp;
393 	m->m_len = m->m_pkthdr.len;
394 
395 	bp->bp_txcount++;
396 	ifp->if_transmit(ifp, m);
397 }
398 
399 static int
400 bstp_pdu_flags(struct bstp_port *bp)
401 {
402 	int flags = 0;
403 
404 	if (bp->bp_proposing && bp->bp_state != BSTP_IFSTATE_FORWARDING)
405 		flags |= BSTP_PDU_F_P;
406 
407 	if (bp->bp_agree)
408 		flags |= BSTP_PDU_F_A;
409 
410 	if (bp->bp_tc_timer.active)
411 		flags |= BSTP_PDU_F_TC;
412 
413 	if (bp->bp_tc_ack)
414 		flags |= BSTP_PDU_F_TCA;
415 
416 	switch (bp->bp_state) {
417 		case BSTP_IFSTATE_LEARNING:
418 			flags |= BSTP_PDU_F_L;
419 			break;
420 
421 		case BSTP_IFSTATE_FORWARDING:
422 			flags |= (BSTP_PDU_F_L | BSTP_PDU_F_F);
423 			break;
424 	}
425 
426 	switch (bp->bp_role) {
427 		case BSTP_ROLE_ROOT:
428 			flags |=
429 				(BSTP_PDU_F_ROOT << BSTP_PDU_PRSHIFT);
430 			break;
431 
432 		case BSTP_ROLE_ALTERNATE:
433 		case BSTP_ROLE_BACKUP:	/* fall through */
434 			flags |=
435 				(BSTP_PDU_F_ALT << BSTP_PDU_PRSHIFT);
436 			break;
437 
438 		case BSTP_ROLE_DESIGNATED:
439 			flags |=
440 				(BSTP_PDU_F_DESG << BSTP_PDU_PRSHIFT);
441 			break;
442 	}
443 
444 	/* Strip off unused flags in either mode */
445 	switch (bp->bp_protover) {
446 		case BSTP_PROTO_STP:
447 			flags &= BSTP_PDU_STPMASK;
448 			break;
449 		case BSTP_PROTO_RSTP:
450 			flags &= BSTP_PDU_RSTPMASK;
451 			break;
452 	}
453 	return (flags);
454 }
455 
456 void
457 bstp_input(struct bstp_port *bp, struct ifnet *ifp, struct mbuf *m)
458 {
459 	struct bstp_state *bs = bp->bp_bs;
460 	struct ether_header *eh;
461 	struct bstp_tbpdu tpdu;
462 	uint16_t len;
463 
464 	if (bp->bp_active == 0) {
465 		m_freem(m);
466 		return;
467 	}
468 
469 	BSTP_LOCK(bs);
470 
471 	eh = mtod(m, struct ether_header *);
472 
473 	len = ntohs(eh->ether_type);
474 	if (len < sizeof(tpdu))
475 		goto out;
476 
477 	m_adj(m, ETHER_HDR_LEN);
478 
479 	if (m->m_pkthdr.len > len)
480 		m_adj(m, len - m->m_pkthdr.len);
481 	if (m->m_len < sizeof(tpdu) &&
482 	    (m = m_pullup(m, sizeof(tpdu))) == NULL)
483 		goto out;
484 
485 	memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu));
486 
487 	/* basic packet checks */
488 	if (tpdu.tbu_dsap != LLC_8021D_LSAP ||
489 	    tpdu.tbu_ssap != LLC_8021D_LSAP ||
490 	    tpdu.tbu_ctl != LLC_UI)
491 		goto out;
492 	if (tpdu.tbu_protoid != BSTP_PROTO_ID)
493 		goto out;
494 
495 	/*
496 	 * We can treat later versions of the PDU as the same as the maximum
497 	 * version we implement. All additional parameters/flags are ignored.
498 	 */
499 	if (tpdu.tbu_protover > BSTP_PROTO_MAX)
500 		tpdu.tbu_protover = BSTP_PROTO_MAX;
501 
502 	if (tpdu.tbu_protover != bp->bp_protover) {
503 		/*
504 		 * Wait for the migration delay timer to expire before changing
505 		 * protocol version to avoid flip-flops.
506 		 */
507 		if (bp->bp_flags & BSTP_PORT_CANMIGRATE)
508 			bstp_set_port_proto(bp, tpdu.tbu_protover);
509 		else
510 			goto out;
511 	}
512 
513 	/* Clear operedge upon receiving a PDU on the port */
514 	bp->bp_operedge = 0;
515 	bstp_timer_start(&bp->bp_edge_delay_timer,
516 	    BSTP_DEFAULT_MIGRATE_DELAY);
517 
518 	switch (tpdu.tbu_protover) {
519 		case BSTP_PROTO_STP:
520 			bstp_received_stp(bs, bp, &m, &tpdu);
521 			break;
522 
523 		case BSTP_PROTO_RSTP:
524 			bstp_received_rstp(bs, bp, &m, &tpdu);
525 			break;
526 	}
527 out:
528 	BSTP_UNLOCK(bs);
529 	if (m)
530 		m_freem(m);
531 }
532 
533 static void
534 bstp_received_stp(struct bstp_state *bs, struct bstp_port *bp,
535     struct mbuf **mp, struct bstp_tbpdu *tpdu)
536 {
537 	struct bstp_cbpdu cpdu;
538 	struct bstp_config_unit *cu = &bp->bp_msg_cu;
539 	struct bstp_tcn_unit tu;
540 
541 	switch (tpdu->tbu_bpdutype) {
542 	case BSTP_MSGTYPE_TCN:
543 		tu.tu_message_type = tpdu->tbu_bpdutype;
544 		bstp_received_tcn(bs, bp, &tu);
545 		break;
546 	case BSTP_MSGTYPE_CFG:
547 		if ((*mp)->m_len < BSTP_BPDU_STP_LEN &&
548 		    (*mp = m_pullup(*mp, BSTP_BPDU_STP_LEN)) == NULL)
549 			return;
550 		memcpy(&cpdu, mtod(*mp, caddr_t), BSTP_BPDU_STP_LEN);
551 
552 		bstp_decode_bpdu(bp, &cpdu, cu);
553 		bstp_received_bpdu(bs, bp, cu);
554 		break;
555 	}
556 }
557 
558 static void
559 bstp_received_rstp(struct bstp_state *bs, struct bstp_port *bp,
560     struct mbuf **mp, struct bstp_tbpdu *tpdu)
561 {
562 	struct bstp_cbpdu cpdu;
563 	struct bstp_config_unit *cu = &bp->bp_msg_cu;
564 
565 	if (tpdu->tbu_bpdutype != BSTP_MSGTYPE_RSTP)
566 		return;
567 
568 	if ((*mp)->m_len < BSTP_BPDU_RSTP_LEN &&
569 	    (*mp = m_pullup(*mp, BSTP_BPDU_RSTP_LEN)) == NULL)
570 		return;
571 	memcpy(&cpdu, mtod(*mp, caddr_t), BSTP_BPDU_RSTP_LEN);
572 
573 	bstp_decode_bpdu(bp, &cpdu, cu);
574 	bstp_received_bpdu(bs, bp, cu);
575 }
576 
577 static void
578 bstp_received_tcn(struct bstp_state *bs, struct bstp_port *bp,
579     struct bstp_tcn_unit *tcn)
580 {
581 	bp->bp_rcvdtcn = 1;
582 	bstp_update_tc(bp);
583 }
584 
585 static void
586 bstp_received_bpdu(struct bstp_state *bs, struct bstp_port *bp,
587     struct bstp_config_unit *cu)
588 {
589 	int type;
590 
591 	BSTP_LOCK_ASSERT(bs);
592 
593 	/* We need to have transitioned to INFO_MINE before proceeding */
594 	switch (bp->bp_infois) {
595 		case BSTP_INFO_DISABLED:
596 		case BSTP_INFO_AGED:
597 			return;
598 	}
599 
600 	/* range checks */
601 	if (cu->cu_message_age >= cu->cu_max_age) {
602 		return;
603 	}
604 	if (cu->cu_max_age < BSTP_MIN_MAX_AGE ||
605 	    cu->cu_max_age > BSTP_MAX_MAX_AGE) {
606 		return;
607 	}
608 	if (cu->cu_forward_delay < BSTP_MIN_FORWARD_DELAY ||
609 	    cu->cu_forward_delay > BSTP_MAX_FORWARD_DELAY) {
610 		return;
611 	}
612 	if (cu->cu_hello_time < BSTP_MIN_HELLO_TIME ||
613 	    cu->cu_hello_time > BSTP_MAX_HELLO_TIME) {
614 		return;
615 	}
616 
617 	type = bstp_pdu_rcvtype(bp, cu);
618 
619 	switch (type) {
620 		case BSTP_PDU_SUPERIOR:
621 			bs->bs_allsynced = 0;
622 			bp->bp_agreed = 0;
623 			bp->bp_proposing = 0;
624 
625 			if (cu->cu_proposal && cu->cu_forwarding == 0)
626 				bp->bp_proposed = 1;
627 			if (cu->cu_topology_change)
628 				bp->bp_rcvdtc = 1;
629 			if (cu->cu_topology_change_ack)
630 				bp->bp_rcvdtca = 1;
631 
632 			if (bp->bp_agree &&
633 			    !bstp_pdu_bettersame(bp, BSTP_INFO_RECEIVED))
634 				bp->bp_agree = 0;
635 
636 			/* copy the received priority and timers to the port */
637 			bp->bp_port_pv = cu->cu_pv;
638 			bp->bp_port_msg_age = cu->cu_message_age;
639 			bp->bp_port_max_age = cu->cu_max_age;
640 			bp->bp_port_fdelay = cu->cu_forward_delay;
641 			bp->bp_port_htime =
642 				(cu->cu_hello_time > BSTP_MIN_HELLO_TIME ?
643 				 cu->cu_hello_time : BSTP_MIN_HELLO_TIME);
644 
645 			/* set expiry for the new info */
646 			bstp_set_timer_msgage(bp);
647 
648 			bp->bp_infois = BSTP_INFO_RECEIVED;
649 			bstp_assign_roles(bs);
650 			break;
651 
652 		case BSTP_PDU_REPEATED:
653 			if (cu->cu_proposal && cu->cu_forwarding == 0)
654 				bp->bp_proposed = 1;
655 			if (cu->cu_topology_change)
656 				bp->bp_rcvdtc = 1;
657 			if (cu->cu_topology_change_ack)
658 				bp->bp_rcvdtca = 1;
659 
660 			/* rearm the age timer */
661 			bstp_set_timer_msgage(bp);
662 			break;
663 
664 		case BSTP_PDU_INFERIOR:
665 			if (cu->cu_learning) {
666 				bp->bp_agreed = 1;
667 				bp->bp_proposing = 0;
668 			}
669 			break;
670 
671 		case BSTP_PDU_INFERIORALT:
672 			/*
673 			 * only point to point links are allowed fast
674 			 * transitions to forwarding.
675 			 */
676 			if (cu->cu_agree && bp->bp_ptp_link) {
677 				bp->bp_agreed = 1;
678 				bp->bp_proposing = 0;
679 			} else
680 				bp->bp_agreed = 0;
681 
682 			if (cu->cu_topology_change)
683 				bp->bp_rcvdtc = 1;
684 			if (cu->cu_topology_change_ack)
685 				bp->bp_rcvdtca = 1;
686 			break;
687 
688 		case BSTP_PDU_OTHER:
689 			return;	/* do nothing */
690 	}
691 	/* update the state machines with the new data */
692 	bstp_update_state(bs, bp);
693 }
694 
695 static int
696 bstp_pdu_rcvtype(struct bstp_port *bp, struct bstp_config_unit *cu)
697 {
698 	int type;
699 
700 	/* default return type */
701 	type = BSTP_PDU_OTHER;
702 
703 	switch (cu->cu_role) {
704 	case BSTP_ROLE_DESIGNATED:
705 		if (bstp_info_superior(&bp->bp_port_pv, &cu->cu_pv))
706 			/* bpdu priority is superior */
707 			type = BSTP_PDU_SUPERIOR;
708 		else if (bstp_info_cmp(&bp->bp_port_pv, &cu->cu_pv) ==
709 		    INFO_SAME) {
710 			if (bp->bp_port_msg_age != cu->cu_message_age ||
711 			    bp->bp_port_max_age != cu->cu_max_age ||
712 			    bp->bp_port_fdelay != cu->cu_forward_delay ||
713 			    bp->bp_port_htime != cu->cu_hello_time)
714 				/* bpdu priority is equal and timers differ */
715 				type = BSTP_PDU_SUPERIOR;
716 			else
717 				/* bpdu is equal */
718 				type = BSTP_PDU_REPEATED;
719 		} else
720 			/* bpdu priority is worse */
721 			type = BSTP_PDU_INFERIOR;
722 
723 		break;
724 
725 	case BSTP_ROLE_ROOT:
726 	case BSTP_ROLE_ALTERNATE:
727 	case BSTP_ROLE_BACKUP:
728 		if (bstp_info_cmp(&bp->bp_port_pv, &cu->cu_pv) <= INFO_SAME)
729 			/*
730 			 * not a designated port and priority is the same or
731 			 * worse
732 			 */
733 			type = BSTP_PDU_INFERIORALT;
734 		break;
735 	}
736 
737 	return (type);
738 }
739 
740 static int
741 bstp_pdu_bettersame(struct bstp_port *bp, int newinfo)
742 {
743 	if (newinfo == BSTP_INFO_RECEIVED &&
744 	    bp->bp_infois == BSTP_INFO_RECEIVED &&
745 	    bstp_info_cmp(&bp->bp_port_pv, &bp->bp_msg_cu.cu_pv) >= INFO_SAME)
746 		return (1);
747 
748 	if (newinfo == BSTP_INFO_MINE &&
749 	    bp->bp_infois == BSTP_INFO_MINE &&
750 	    bstp_info_cmp(&bp->bp_port_pv, &bp->bp_desg_pv) >= INFO_SAME)
751 		return (1);
752 
753 	return (0);
754 }
755 
756 static int
757 bstp_info_cmp(struct bstp_pri_vector *pv,
758     struct bstp_pri_vector *cpv)
759 {
760 	if (cpv->pv_root_id < pv->pv_root_id)
761 		return (INFO_BETTER);
762 	if (cpv->pv_root_id > pv->pv_root_id)
763 		return (INFO_WORSE);
764 
765 	if (cpv->pv_cost < pv->pv_cost)
766 		return (INFO_BETTER);
767 	if (cpv->pv_cost > pv->pv_cost)
768 		return (INFO_WORSE);
769 
770 	if (cpv->pv_dbridge_id < pv->pv_dbridge_id)
771 		return (INFO_BETTER);
772 	if (cpv->pv_dbridge_id > pv->pv_dbridge_id)
773 		return (INFO_WORSE);
774 
775 	if (cpv->pv_dport_id < pv->pv_dport_id)
776 		return (INFO_BETTER);
777 	if (cpv->pv_dport_id > pv->pv_dport_id)
778 		return (INFO_WORSE);
779 
780 	return (INFO_SAME);
781 }
782 
783 /*
784  * This message priority vector is superior to the port priority vector and
785  * will replace it if, and only if, the message priority vector is better than
786  * the port priority vector, or the message has been transmitted from the same
787  * designated bridge and designated port as the port priority vector.
788  */
789 static int
790 bstp_info_superior(struct bstp_pri_vector *pv,
791     struct bstp_pri_vector *cpv)
792 {
793 	if (bstp_info_cmp(pv, cpv) == INFO_BETTER ||
794 	    (bstp_same_bridgeid(pv->pv_dbridge_id, cpv->pv_dbridge_id) &&
795 	    (cpv->pv_dport_id & 0xfff) == (pv->pv_dport_id & 0xfff)))
796 		return (1);
797 	return (0);
798 }
799 
800 static void
801 bstp_assign_roles(struct bstp_state *bs)
802 {
803 	struct bstp_port *bp, *rbp = NULL;
804 	struct bstp_pri_vector pv;
805 
806 	/* default to our priority vector */
807 	bs->bs_root_pv = bs->bs_bridge_pv;
808 	bs->bs_root_msg_age = 0;
809 	bs->bs_root_max_age = bs->bs_bridge_max_age;
810 	bs->bs_root_fdelay = bs->bs_bridge_fdelay;
811 	bs->bs_root_htime = bs->bs_bridge_htime;
812 	bs->bs_root_port = NULL;
813 
814 	/* check if any received info supersedes us */
815 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
816 		if (bp->bp_infois != BSTP_INFO_RECEIVED)
817 			continue;
818 
819 		pv = bp->bp_port_pv;
820 		pv.pv_cost += bp->bp_path_cost;
821 
822 		/*
823 		 * The root priority vector is the best of the set comprising
824 		 * the bridge priority vector plus all root path priority
825 		 * vectors whose bridge address is not equal to us.
826 		 */
827 		if (bstp_same_bridgeid(pv.pv_dbridge_id,
828 		    bs->bs_bridge_pv.pv_dbridge_id) == 0 &&
829 		    bstp_info_cmp(&bs->bs_root_pv, &pv) == INFO_BETTER) {
830 			/* the port vector replaces the root */
831 			bs->bs_root_pv = pv;
832 			bs->bs_root_msg_age = bp->bp_port_msg_age +
833 			    BSTP_MESSAGE_AGE_INCR;
834 			bs->bs_root_max_age = bp->bp_port_max_age;
835 			bs->bs_root_fdelay = bp->bp_port_fdelay;
836 			bs->bs_root_htime = bp->bp_port_htime;
837 			rbp = bp;
838 		}
839 	}
840 
841 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
842 		/* calculate the port designated vector */
843 		bp->bp_desg_pv.pv_root_id = bs->bs_root_pv.pv_root_id;
844 		bp->bp_desg_pv.pv_cost = bs->bs_root_pv.pv_cost;
845 		bp->bp_desg_pv.pv_dbridge_id = bs->bs_bridge_pv.pv_dbridge_id;
846 		bp->bp_desg_pv.pv_dport_id = bp->bp_port_id;
847 		bp->bp_desg_pv.pv_port_id = bp->bp_port_id;
848 
849 		/* calculate designated times */
850 		bp->bp_desg_msg_age = bs->bs_root_msg_age;
851 		bp->bp_desg_max_age = bs->bs_root_max_age;
852 		bp->bp_desg_fdelay = bs->bs_root_fdelay;
853 		bp->bp_desg_htime = bs->bs_bridge_htime;
854 
855 		switch (bp->bp_infois) {
856 		case BSTP_INFO_DISABLED:
857 			bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
858 			break;
859 
860 		case BSTP_INFO_AGED:
861 			bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
862 			bstp_update_info(bp);
863 			break;
864 
865 		case BSTP_INFO_MINE:
866 			bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
867 			/* update the port info if stale */
868 			if (bstp_info_cmp(&bp->bp_port_pv,
869 			    &bp->bp_desg_pv) != INFO_SAME ||
870 			    (rbp != NULL &&
871 			    (bp->bp_port_msg_age != rbp->bp_port_msg_age ||
872 			    bp->bp_port_max_age != rbp->bp_port_max_age ||
873 			    bp->bp_port_fdelay != rbp->bp_port_fdelay ||
874 			    bp->bp_port_htime != rbp->bp_port_htime)))
875 				bstp_update_info(bp);
876 			break;
877 
878 		case BSTP_INFO_RECEIVED:
879 			if (bp == rbp) {
880 				/*
881 				 * root priority is derived from this
882 				 * port, make it the root port.
883 				 */
884 				bstp_set_port_role(bp, BSTP_ROLE_ROOT);
885 				bs->bs_root_port = bp;
886 			} else if (bstp_info_cmp(&bp->bp_port_pv,
887 				    &bp->bp_desg_pv) == INFO_BETTER) {
888 				/*
889 				 * the port priority is lower than the root
890 				 * port.
891 				 */
892 				bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED);
893 				bstp_update_info(bp);
894 			} else {
895 				if (bstp_same_bridgeid(
896 				    bp->bp_port_pv.pv_dbridge_id,
897 				    bs->bs_bridge_pv.pv_dbridge_id)) {
898 					/*
899 					 * the designated bridge refers to
900 					 * another port on this bridge.
901 					 */
902 					bstp_set_port_role(bp,
903 					    BSTP_ROLE_BACKUP);
904 				} else {
905 					/*
906 					 * the port is an inferior path to the
907 					 * root bridge.
908 					 */
909 					bstp_set_port_role(bp,
910 					    BSTP_ROLE_ALTERNATE);
911 				}
912 			}
913 			break;
914 		}
915 	}
916 }
917 
918 static void
919 bstp_update_state(struct bstp_state *bs, struct bstp_port *bp)
920 {
921 	struct bstp_port *bp2;
922 	int synced;
923 
924 	BSTP_LOCK_ASSERT(bs);
925 
926 	/* check if all the ports have syncronised again */
927 	if (!bs->bs_allsynced) {
928 		synced = 1;
929 		LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
930 			if (!(bp2->bp_synced ||
931 			     bp2->bp_role == BSTP_ROLE_ROOT)) {
932 				synced = 0;
933 				break;
934 			}
935 		}
936 		bs->bs_allsynced = synced;
937 	}
938 
939 	bstp_update_roles(bs, bp);
940 	bstp_update_tc(bp);
941 }
942 
943 static void
944 bstp_update_roles(struct bstp_state *bs, struct bstp_port *bp)
945 {
946 	NET_EPOCH_ASSERT();
947 
948 	switch (bp->bp_role) {
949 	case BSTP_ROLE_DISABLED:
950 		/* Clear any flags if set */
951 		if (bp->bp_sync || !bp->bp_synced || bp->bp_reroot) {
952 			bp->bp_sync = 0;
953 			bp->bp_synced = 1;
954 			bp->bp_reroot = 0;
955 		}
956 		break;
957 
958 	case BSTP_ROLE_ALTERNATE:
959 	case BSTP_ROLE_BACKUP:
960 		if ((bs->bs_allsynced && !bp->bp_agree) ||
961 		    (bp->bp_proposed && bp->bp_agree)) {
962 			bp->bp_proposed = 0;
963 			bp->bp_agree = 1;
964 			bp->bp_flags |= BSTP_PORT_NEWINFO;
965 			DPRINTF("%s -> ALTERNATE_AGREED\n",
966 			    bp->bp_ifp->if_xname);
967 		}
968 
969 		if (bp->bp_proposed && !bp->bp_agree) {
970 			bstp_set_all_sync(bs);
971 			bp->bp_proposed = 0;
972 			DPRINTF("%s -> ALTERNATE_PROPOSED\n",
973 			    bp->bp_ifp->if_xname);
974 		}
975 
976 		/* Clear any flags if set */
977 		if (bp->bp_sync || !bp->bp_synced || bp->bp_reroot) {
978 			bp->bp_sync = 0;
979 			bp->bp_synced = 1;
980 			bp->bp_reroot = 0;
981 			DPRINTF("%s -> ALTERNATE_PORT\n", bp->bp_ifp->if_xname);
982 		}
983 		break;
984 
985 	case BSTP_ROLE_ROOT:
986 		if (bp->bp_state != BSTP_IFSTATE_FORWARDING && !bp->bp_reroot) {
987 			bstp_set_all_reroot(bs);
988 			DPRINTF("%s -> ROOT_REROOT\n", bp->bp_ifp->if_xname);
989 		}
990 
991 		if ((bs->bs_allsynced && !bp->bp_agree) ||
992 		    (bp->bp_proposed && bp->bp_agree)) {
993 			bp->bp_proposed = 0;
994 			bp->bp_sync = 0;
995 			bp->bp_agree = 1;
996 			bp->bp_flags |= BSTP_PORT_NEWINFO;
997 			DPRINTF("%s -> ROOT_AGREED\n", bp->bp_ifp->if_xname);
998 		}
999 
1000 		if (bp->bp_proposed && !bp->bp_agree) {
1001 			bstp_set_all_sync(bs);
1002 			bp->bp_proposed = 0;
1003 			DPRINTF("%s -> ROOT_PROPOSED\n", bp->bp_ifp->if_xname);
1004 		}
1005 
1006 		if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
1007 		    (bp->bp_forward_delay_timer.active == 0 ||
1008 		    (bstp_rerooted(bs, bp) &&
1009 		    bp->bp_recent_backup_timer.active == 0 &&
1010 		    bp->bp_protover == BSTP_PROTO_RSTP))) {
1011 			switch (bp->bp_state) {
1012 			case BSTP_IFSTATE_DISCARDING:
1013 				bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING);
1014 				break;
1015 			case BSTP_IFSTATE_LEARNING:
1016 				bstp_set_port_state(bp,
1017 				    BSTP_IFSTATE_FORWARDING);
1018 				break;
1019 			}
1020 		}
1021 
1022 		if (bp->bp_state == BSTP_IFSTATE_FORWARDING && bp->bp_reroot) {
1023 			bp->bp_reroot = 0;
1024 			DPRINTF("%s -> ROOT_REROOTED\n", bp->bp_ifp->if_xname);
1025 		}
1026 		break;
1027 
1028 	case BSTP_ROLE_DESIGNATED:
1029 		if (bp->bp_recent_root_timer.active == 0 && bp->bp_reroot) {
1030 			bp->bp_reroot = 0;
1031 			DPRINTF("%s -> DESIGNATED_RETIRED\n",
1032 			    bp->bp_ifp->if_xname);
1033 		}
1034 
1035 		if ((bp->bp_state == BSTP_IFSTATE_DISCARDING &&
1036 		    !bp->bp_synced) || (bp->bp_agreed && !bp->bp_synced) ||
1037 		    (bp->bp_operedge && !bp->bp_synced) ||
1038 		    (bp->bp_sync && bp->bp_synced)) {
1039 			bstp_timer_stop(&bp->bp_recent_root_timer);
1040 			bp->bp_synced = 1;
1041 			bp->bp_sync = 0;
1042 			DPRINTF("%s -> DESIGNATED_SYNCED\n",
1043 			    bp->bp_ifp->if_xname);
1044 		}
1045 
1046 		if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
1047 		    !bp->bp_agreed && !bp->bp_proposing &&
1048 		    !bp->bp_operedge) {
1049 			bp->bp_proposing = 1;
1050 			bp->bp_flags |= BSTP_PORT_NEWINFO;
1051 			bstp_timer_start(&bp->bp_edge_delay_timer,
1052 			    (bp->bp_ptp_link ? BSTP_DEFAULT_MIGRATE_DELAY :
1053 			     bp->bp_desg_max_age));
1054 			DPRINTF("%s -> DESIGNATED_PROPOSE\n",
1055 			    bp->bp_ifp->if_xname);
1056 		}
1057 
1058 		if (bp->bp_state != BSTP_IFSTATE_FORWARDING &&
1059 		    (bp->bp_forward_delay_timer.active == 0 || bp->bp_agreed ||
1060 		    bp->bp_operedge) &&
1061 		    (bp->bp_recent_root_timer.active == 0 || !bp->bp_reroot) &&
1062 		    !bp->bp_sync) {
1063 			if (bp->bp_agreed)
1064 				DPRINTF("%s -> AGREED\n", bp->bp_ifp->if_xname);
1065 			/*
1066 			 * If agreed|operedge then go straight to forwarding,
1067 			 * otherwise follow discard -> learn -> forward.
1068 			 */
1069 			if (bp->bp_agreed || bp->bp_operedge ||
1070 			    bp->bp_state == BSTP_IFSTATE_LEARNING) {
1071 				bstp_set_port_state(bp,
1072 				    BSTP_IFSTATE_FORWARDING);
1073 				bp->bp_agreed = bp->bp_protover;
1074 			} else if (bp->bp_state == BSTP_IFSTATE_DISCARDING)
1075 				bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING);
1076 		}
1077 
1078 		if (((bp->bp_sync && !bp->bp_synced) ||
1079 		    (bp->bp_reroot && bp->bp_recent_root_timer.active) ||
1080 		    (bp->bp_flags & BSTP_PORT_DISPUTED)) && !bp->bp_operedge &&
1081 		    bp->bp_state != BSTP_IFSTATE_DISCARDING) {
1082 			bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1083 			bp->bp_flags &= ~BSTP_PORT_DISPUTED;
1084 			bstp_timer_start(&bp->bp_forward_delay_timer,
1085 			    bp->bp_protover == BSTP_PROTO_RSTP ?
1086 			    bp->bp_desg_htime : bp->bp_desg_fdelay);
1087 			DPRINTF("%s -> DESIGNATED_DISCARD\n",
1088 			    bp->bp_ifp->if_xname);
1089 		}
1090 		break;
1091 	}
1092 
1093 	if (bp->bp_flags & BSTP_PORT_NEWINFO)
1094 		bstp_transmit(bs, bp);
1095 }
1096 
1097 static void
1098 bstp_update_tc(struct bstp_port *bp)
1099 {
1100 	switch (bp->bp_tcstate) {
1101 		case BSTP_TCSTATE_ACTIVE:
1102 			if ((bp->bp_role != BSTP_ROLE_DESIGNATED &&
1103 			    bp->bp_role != BSTP_ROLE_ROOT) || bp->bp_operedge)
1104 				bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1105 
1106 			if (bp->bp_rcvdtcn)
1107 				bstp_set_port_tc(bp, BSTP_TCSTATE_TCN);
1108 			if (bp->bp_rcvdtc)
1109 				bstp_set_port_tc(bp, BSTP_TCSTATE_TC);
1110 
1111 			if (bp->bp_tc_prop && !bp->bp_operedge)
1112 				bstp_set_port_tc(bp, BSTP_TCSTATE_PROPAG);
1113 
1114 			if (bp->bp_rcvdtca)
1115 				bstp_set_port_tc(bp, BSTP_TCSTATE_ACK);
1116 			break;
1117 
1118 		case BSTP_TCSTATE_INACTIVE:
1119 			if ((bp->bp_state == BSTP_IFSTATE_LEARNING ||
1120 			    bp->bp_state == BSTP_IFSTATE_FORWARDING) &&
1121 			    bp->bp_fdbflush == 0)
1122 				bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1123 			break;
1124 
1125 		case BSTP_TCSTATE_LEARNING:
1126 			if (bp->bp_rcvdtc || bp->bp_rcvdtcn || bp->bp_rcvdtca ||
1127 			    bp->bp_tc_prop)
1128 				bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING);
1129 			else if (bp->bp_role != BSTP_ROLE_DESIGNATED &&
1130 				 bp->bp_role != BSTP_ROLE_ROOT &&
1131 				 bp->bp_state == BSTP_IFSTATE_DISCARDING)
1132 				bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
1133 
1134 			if ((bp->bp_role == BSTP_ROLE_DESIGNATED ||
1135 			    bp->bp_role == BSTP_ROLE_ROOT) &&
1136 			    bp->bp_state == BSTP_IFSTATE_FORWARDING &&
1137 			    !bp->bp_operedge)
1138 				bstp_set_port_tc(bp, BSTP_TCSTATE_DETECTED);
1139 			break;
1140 
1141 		/* these are transient states and go straight back to ACTIVE */
1142 		case BSTP_TCSTATE_DETECTED:
1143 		case BSTP_TCSTATE_TCN:
1144 		case BSTP_TCSTATE_TC:
1145 		case BSTP_TCSTATE_PROPAG:
1146 		case BSTP_TCSTATE_ACK:
1147 			DPRINTF("Invalid TC state for %s\n",
1148 			    bp->bp_ifp->if_xname);
1149 			break;
1150 	}
1151 
1152 }
1153 
1154 static void
1155 bstp_update_info(struct bstp_port *bp)
1156 {
1157 	struct bstp_state *bs = bp->bp_bs;
1158 
1159 	bp->bp_proposing = 0;
1160 	bp->bp_proposed = 0;
1161 
1162 	if (bp->bp_agreed && !bstp_pdu_bettersame(bp, BSTP_INFO_MINE))
1163 		bp->bp_agreed = 0;
1164 
1165 	if (bp->bp_synced && !bp->bp_agreed) {
1166 		bp->bp_synced = 0;
1167 		bs->bs_allsynced = 0;
1168 	}
1169 
1170 	/* copy the designated pv to the port */
1171 	bp->bp_port_pv = bp->bp_desg_pv;
1172 	bp->bp_port_msg_age = bp->bp_desg_msg_age;
1173 	bp->bp_port_max_age = bp->bp_desg_max_age;
1174 	bp->bp_port_fdelay = bp->bp_desg_fdelay;
1175 	bp->bp_port_htime = bp->bp_desg_htime;
1176 	bp->bp_infois = BSTP_INFO_MINE;
1177 
1178 	/* Set transmit flag but do not immediately send */
1179 	bp->bp_flags |= BSTP_PORT_NEWINFO;
1180 }
1181 
1182 /* set tcprop on every port other than the caller */
1183 static void
1184 bstp_set_other_tcprop(struct bstp_port *bp)
1185 {
1186 	struct bstp_state *bs = bp->bp_bs;
1187 	struct bstp_port *bp2;
1188 
1189 	BSTP_LOCK_ASSERT(bs);
1190 
1191 	LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
1192 		if (bp2 == bp)
1193 			continue;
1194 		bp2->bp_tc_prop = 1;
1195 	}
1196 }
1197 
1198 static void
1199 bstp_set_all_reroot(struct bstp_state *bs)
1200 {
1201 	struct bstp_port *bp;
1202 
1203 	BSTP_LOCK_ASSERT(bs);
1204 
1205 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
1206 		bp->bp_reroot = 1;
1207 }
1208 
1209 static void
1210 bstp_set_all_sync(struct bstp_state *bs)
1211 {
1212 	struct bstp_port *bp;
1213 
1214 	BSTP_LOCK_ASSERT(bs);
1215 
1216 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1217 		bp->bp_sync = 1;
1218 		bp->bp_synced = 0;	/* Not explicit in spec */
1219 	}
1220 
1221 	bs->bs_allsynced = 0;
1222 }
1223 
1224 static void
1225 bstp_set_port_state(struct bstp_port *bp, int state)
1226 {
1227 	if (bp->bp_state == state)
1228 		return;
1229 
1230 	bp->bp_state = state;
1231 
1232 	switch (bp->bp_state) {
1233 		case BSTP_IFSTATE_DISCARDING:
1234 			DPRINTF("state changed to DISCARDING on %s\n",
1235 			    bp->bp_ifp->if_xname);
1236 			break;
1237 
1238 		case BSTP_IFSTATE_LEARNING:
1239 			DPRINTF("state changed to LEARNING on %s\n",
1240 			    bp->bp_ifp->if_xname);
1241 
1242 			bstp_timer_start(&bp->bp_forward_delay_timer,
1243 			    bp->bp_protover == BSTP_PROTO_RSTP ?
1244 			    bp->bp_desg_htime : bp->bp_desg_fdelay);
1245 			break;
1246 
1247 		case BSTP_IFSTATE_FORWARDING:
1248 			DPRINTF("state changed to FORWARDING on %s\n",
1249 			    bp->bp_ifp->if_xname);
1250 
1251 			bstp_timer_stop(&bp->bp_forward_delay_timer);
1252 			/* Record that we enabled forwarding */
1253 			bp->bp_forward_transitions++;
1254 			break;
1255 	}
1256 
1257 	/* notify the parent bridge */
1258 	taskqueue_enqueue(taskqueue_swi, &bp->bp_statetask);
1259 }
1260 
1261 static void
1262 bstp_set_port_role(struct bstp_port *bp, int role)
1263 {
1264 	struct bstp_state *bs = bp->bp_bs;
1265 
1266 	if (bp->bp_role == role)
1267 		return;
1268 
1269 	/* perform pre-change tasks */
1270 	switch (bp->bp_role) {
1271 		case BSTP_ROLE_DISABLED:
1272 			bstp_timer_start(&bp->bp_forward_delay_timer,
1273 			    bp->bp_desg_max_age);
1274 			break;
1275 
1276 		case BSTP_ROLE_BACKUP:
1277 			bstp_timer_start(&bp->bp_recent_backup_timer,
1278 			    bp->bp_desg_htime * 2);
1279 			/* fall through */
1280 		case BSTP_ROLE_ALTERNATE:
1281 			bstp_timer_start(&bp->bp_forward_delay_timer,
1282 			    bp->bp_desg_fdelay);
1283 			bp->bp_sync = 0;
1284 			bp->bp_synced = 1;
1285 			bp->bp_reroot = 0;
1286 			break;
1287 
1288 		case BSTP_ROLE_ROOT:
1289 			bstp_timer_start(&bp->bp_recent_root_timer,
1290 			    BSTP_DEFAULT_FORWARD_DELAY);
1291 			break;
1292 	}
1293 
1294 	bp->bp_role = role;
1295 	/* clear values not carried between roles */
1296 	bp->bp_proposing = 0;
1297 	bs->bs_allsynced = 0;
1298 
1299 	/* initialise the new role */
1300 	switch (bp->bp_role) {
1301 		case BSTP_ROLE_DISABLED:
1302 		case BSTP_ROLE_ALTERNATE:
1303 		case BSTP_ROLE_BACKUP:
1304 			DPRINTF("%s role -> ALT/BACK/DISABLED\n",
1305 			    bp->bp_ifp->if_xname);
1306 			bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1307 			bstp_timer_stop(&bp->bp_recent_root_timer);
1308 			bstp_timer_latch(&bp->bp_forward_delay_timer);
1309 			bp->bp_sync = 0;
1310 			bp->bp_synced = 1;
1311 			bp->bp_reroot = 0;
1312 			break;
1313 
1314 		case BSTP_ROLE_ROOT:
1315 			DPRINTF("%s role -> ROOT\n",
1316 			    bp->bp_ifp->if_xname);
1317 			bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
1318 			bstp_timer_latch(&bp->bp_recent_root_timer);
1319 			bp->bp_proposing = 0;
1320 			break;
1321 
1322 		case BSTP_ROLE_DESIGNATED:
1323 			DPRINTF("%s role -> DESIGNATED\n",
1324 			    bp->bp_ifp->if_xname);
1325 			bstp_timer_start(&bp->bp_hello_timer,
1326 			    bp->bp_desg_htime);
1327 			bp->bp_agree = 0;
1328 			break;
1329 	}
1330 
1331 	/* let the TC state know that the role changed */
1332 	bstp_update_tc(bp);
1333 }
1334 
1335 static void
1336 bstp_set_port_proto(struct bstp_port *bp, int proto)
1337 {
1338 	struct bstp_state *bs = bp->bp_bs;
1339 
1340 	/* supported protocol versions */
1341 	switch (proto) {
1342 		case BSTP_PROTO_STP:
1343 			/* we can downgrade protocols only */
1344 			bstp_timer_stop(&bp->bp_migrate_delay_timer);
1345 			/* clear unsupported features */
1346 			bp->bp_operedge = 0;
1347 			/* STP compat mode only uses 16 bits of the 32 */
1348 			if (bp->bp_path_cost > 65535)
1349 				bp->bp_path_cost = 65535;
1350 			break;
1351 
1352 		case BSTP_PROTO_RSTP:
1353 			bstp_timer_start(&bp->bp_migrate_delay_timer,
1354 			    bs->bs_migration_delay);
1355 			break;
1356 
1357 		default:
1358 			DPRINTF("Unsupported STP version %d\n", proto);
1359 			return;
1360 	}
1361 
1362 	bp->bp_protover = proto;
1363 	bp->bp_flags &= ~BSTP_PORT_CANMIGRATE;
1364 }
1365 
1366 static void
1367 bstp_set_port_tc(struct bstp_port *bp, int state)
1368 {
1369 	struct bstp_state *bs = bp->bp_bs;
1370 
1371 	bp->bp_tcstate = state;
1372 
1373 	/* initialise the new state */
1374 	switch (bp->bp_tcstate) {
1375 		case BSTP_TCSTATE_ACTIVE:
1376 			DPRINTF("%s -> TC_ACTIVE\n", bp->bp_ifp->if_xname);
1377 			/* nothing to do */
1378 			break;
1379 
1380 		case BSTP_TCSTATE_INACTIVE:
1381 			bstp_timer_stop(&bp->bp_tc_timer);
1382 			/* flush routes on the parent bridge */
1383 			bp->bp_fdbflush = 1;
1384 			taskqueue_enqueue(taskqueue_swi, &bp->bp_rtagetask);
1385 			bp->bp_tc_ack = 0;
1386 			DPRINTF("%s -> TC_INACTIVE\n", bp->bp_ifp->if_xname);
1387 			break;
1388 
1389 		case BSTP_TCSTATE_LEARNING:
1390 			bp->bp_rcvdtc = 0;
1391 			bp->bp_rcvdtcn = 0;
1392 			bp->bp_rcvdtca = 0;
1393 			bp->bp_tc_prop = 0;
1394 			DPRINTF("%s -> TC_LEARNING\n", bp->bp_ifp->if_xname);
1395 			break;
1396 
1397 		case BSTP_TCSTATE_DETECTED:
1398 			bstp_set_timer_tc(bp);
1399 			bstp_set_other_tcprop(bp);
1400 			/* send out notification */
1401 			bp->bp_flags |= BSTP_PORT_NEWINFO;
1402 			bstp_transmit(bs, bp);
1403 			getmicrotime(&bs->bs_last_tc_time);
1404 			DPRINTF("%s -> TC_DETECTED\n", bp->bp_ifp->if_xname);
1405 			bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1406 			break;
1407 
1408 		case BSTP_TCSTATE_TCN:
1409 			bstp_set_timer_tc(bp);
1410 			DPRINTF("%s -> TC_TCN\n", bp->bp_ifp->if_xname);
1411 			/* fall through */
1412 		case BSTP_TCSTATE_TC:
1413 			bp->bp_rcvdtc = 0;
1414 			bp->bp_rcvdtcn = 0;
1415 			if (bp->bp_role == BSTP_ROLE_DESIGNATED)
1416 				bp->bp_tc_ack = 1;
1417 
1418 			bstp_set_other_tcprop(bp);
1419 			DPRINTF("%s -> TC_TC\n", bp->bp_ifp->if_xname);
1420 			bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1421 			break;
1422 
1423 		case BSTP_TCSTATE_PROPAG:
1424 			/* flush routes on the parent bridge */
1425 			bp->bp_fdbflush = 1;
1426 			taskqueue_enqueue(taskqueue_swi, &bp->bp_rtagetask);
1427 			bp->bp_tc_prop = 0;
1428 			bstp_set_timer_tc(bp);
1429 			DPRINTF("%s -> TC_PROPAG\n", bp->bp_ifp->if_xname);
1430 			bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1431 			break;
1432 
1433 		case BSTP_TCSTATE_ACK:
1434 			bstp_timer_stop(&bp->bp_tc_timer);
1435 			bp->bp_rcvdtca = 0;
1436 			DPRINTF("%s -> TC_ACK\n", bp->bp_ifp->if_xname);
1437 			bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */
1438 			break;
1439 	}
1440 }
1441 
1442 static void
1443 bstp_set_timer_tc(struct bstp_port *bp)
1444 {
1445 	struct bstp_state *bs = bp->bp_bs;
1446 
1447 	if (bp->bp_tc_timer.active)
1448 		return;
1449 
1450 	switch (bp->bp_protover) {
1451 		case BSTP_PROTO_RSTP:
1452 			bstp_timer_start(&bp->bp_tc_timer,
1453 			    bp->bp_desg_htime + BSTP_TICK_VAL);
1454 			bp->bp_flags |= BSTP_PORT_NEWINFO;
1455 			break;
1456 
1457 		case BSTP_PROTO_STP:
1458 			bstp_timer_start(&bp->bp_tc_timer,
1459 			    bs->bs_root_max_age + bs->bs_root_fdelay);
1460 			break;
1461 	}
1462 }
1463 
1464 static void
1465 bstp_set_timer_msgage(struct bstp_port *bp)
1466 {
1467 	if (bp->bp_port_msg_age + BSTP_MESSAGE_AGE_INCR <=
1468 	    bp->bp_port_max_age) {
1469 		bstp_timer_start(&bp->bp_message_age_timer,
1470 		    bp->bp_port_htime * 3);
1471 	} else
1472 		/* expires immediately */
1473 		bstp_timer_start(&bp->bp_message_age_timer, 0);
1474 }
1475 
1476 static int
1477 bstp_rerooted(struct bstp_state *bs, struct bstp_port *bp)
1478 {
1479 	struct bstp_port *bp2;
1480 	int rr_set = 0;
1481 
1482 	LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) {
1483 		if (bp2 == bp)
1484 			continue;
1485 		if (bp2->bp_recent_root_timer.active) {
1486 			rr_set = 1;
1487 			break;
1488 		}
1489 	}
1490 	return (!rr_set);
1491 }
1492 
1493 int
1494 bstp_set_htime(struct bstp_state *bs, int t)
1495 {
1496 	/* convert seconds to ticks */
1497 	t *=  BSTP_TICK_VAL;
1498 
1499 	/* value can only be changed in leagacy stp mode */
1500 	if (bs->bs_protover != BSTP_PROTO_STP)
1501 		return (EPERM);
1502 
1503 	if (t < BSTP_MIN_HELLO_TIME || t > BSTP_MAX_HELLO_TIME)
1504 		return (EINVAL);
1505 
1506 	BSTP_LOCK(bs);
1507 	bs->bs_bridge_htime = t;
1508 	bstp_reinit(bs);
1509 	BSTP_UNLOCK(bs);
1510 	return (0);
1511 }
1512 
1513 int
1514 bstp_set_fdelay(struct bstp_state *bs, int t)
1515 {
1516 	/* convert seconds to ticks */
1517 	t *= BSTP_TICK_VAL;
1518 
1519 	if (t < BSTP_MIN_FORWARD_DELAY || t > BSTP_MAX_FORWARD_DELAY)
1520 		return (EINVAL);
1521 
1522 	BSTP_LOCK(bs);
1523 	bs->bs_bridge_fdelay = t;
1524 	bstp_reinit(bs);
1525 	BSTP_UNLOCK(bs);
1526 	return (0);
1527 }
1528 
1529 int
1530 bstp_set_maxage(struct bstp_state *bs, int t)
1531 {
1532 	/* convert seconds to ticks */
1533 	t *= BSTP_TICK_VAL;
1534 
1535 	if (t < BSTP_MIN_MAX_AGE || t > BSTP_MAX_MAX_AGE)
1536 		return (EINVAL);
1537 
1538 	BSTP_LOCK(bs);
1539 	bs->bs_bridge_max_age = t;
1540 	bstp_reinit(bs);
1541 	BSTP_UNLOCK(bs);
1542 	return (0);
1543 }
1544 
1545 int
1546 bstp_set_holdcount(struct bstp_state *bs, int count)
1547 {
1548 	struct bstp_port *bp;
1549 
1550 	if (count < BSTP_MIN_HOLD_COUNT ||
1551 	    count > BSTP_MAX_HOLD_COUNT)
1552 		return (EINVAL);
1553 
1554 	BSTP_LOCK(bs);
1555 	bs->bs_txholdcount = count;
1556 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
1557 		bp->bp_txcount = 0;
1558 	BSTP_UNLOCK(bs);
1559 	return (0);
1560 }
1561 
1562 int
1563 bstp_set_protocol(struct bstp_state *bs, int proto)
1564 {
1565 	struct bstp_port *bp;
1566 
1567 	switch (proto) {
1568 		/* Supported protocol versions */
1569 		case BSTP_PROTO_STP:
1570 		case BSTP_PROTO_RSTP:
1571 			break;
1572 
1573 		default:
1574 			return (EINVAL);
1575 	}
1576 
1577 	BSTP_LOCK(bs);
1578 	bs->bs_protover = proto;
1579 	bs->bs_bridge_htime = BSTP_DEFAULT_HELLO_TIME;
1580 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1581 		/* reinit state */
1582 		bp->bp_infois = BSTP_INFO_DISABLED;
1583 		bp->bp_txcount = 0;
1584 		bstp_set_port_proto(bp, bs->bs_protover);
1585 		bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
1586 		bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
1587 		bstp_timer_stop(&bp->bp_recent_backup_timer);
1588 	}
1589 	bstp_reinit(bs);
1590 	BSTP_UNLOCK(bs);
1591 	return (0);
1592 }
1593 
1594 int
1595 bstp_set_priority(struct bstp_state *bs, int pri)
1596 {
1597 	if (pri < 0 || pri > BSTP_MAX_PRIORITY)
1598 		return (EINVAL);
1599 
1600 	/* Limit to steps of 4096 */
1601 	pri -= pri % 4096;
1602 
1603 	BSTP_LOCK(bs);
1604 	bs->bs_bridge_priority = pri;
1605 	bstp_reinit(bs);
1606 	BSTP_UNLOCK(bs);
1607 	return (0);
1608 }
1609 
1610 int
1611 bstp_set_port_priority(struct bstp_port *bp, int pri)
1612 {
1613 	struct bstp_state *bs = bp->bp_bs;
1614 
1615 	if (pri < 0 || pri > BSTP_MAX_PORT_PRIORITY)
1616 		return (EINVAL);
1617 
1618 	/* Limit to steps of 16 */
1619 	pri -= pri % 16;
1620 
1621 	BSTP_LOCK(bs);
1622 	bp->bp_priority = pri;
1623 	bstp_reinit(bs);
1624 	BSTP_UNLOCK(bs);
1625 	return (0);
1626 }
1627 
1628 int
1629 bstp_set_path_cost(struct bstp_port *bp, uint32_t path_cost)
1630 {
1631 	struct bstp_state *bs = bp->bp_bs;
1632 
1633 	if (path_cost > BSTP_MAX_PATH_COST)
1634 		return (EINVAL);
1635 
1636 	/* STP compat mode only uses 16 bits of the 32 */
1637 	if (bp->bp_protover == BSTP_PROTO_STP && path_cost > 65535)
1638 		path_cost = 65535;
1639 
1640 	BSTP_LOCK(bs);
1641 
1642 	if (path_cost == 0) {	/* use auto */
1643 		bp->bp_flags &= ~BSTP_PORT_ADMCOST;
1644 		bp->bp_path_cost = bstp_calc_path_cost(bp);
1645 	} else {
1646 		bp->bp_path_cost = path_cost;
1647 		bp->bp_flags |= BSTP_PORT_ADMCOST;
1648 	}
1649 	bstp_reinit(bs);
1650 	BSTP_UNLOCK(bs);
1651 	return (0);
1652 }
1653 
1654 int
1655 bstp_set_edge(struct bstp_port *bp, int set)
1656 {
1657 	struct bstp_state *bs = bp->bp_bs;
1658 
1659 	BSTP_LOCK(bs);
1660 	if ((bp->bp_operedge = set) == 0)
1661 		bp->bp_flags &= ~BSTP_PORT_ADMEDGE;
1662 	else
1663 		bp->bp_flags |= BSTP_PORT_ADMEDGE;
1664 	BSTP_UNLOCK(bs);
1665 	return (0);
1666 }
1667 
1668 int
1669 bstp_set_autoedge(struct bstp_port *bp, int set)
1670 {
1671 	struct bstp_state *bs = bp->bp_bs;
1672 
1673 	BSTP_LOCK(bs);
1674 	if (set) {
1675 		bp->bp_flags |= BSTP_PORT_AUTOEDGE;
1676 		/* we may be able to transition straight to edge */
1677 		if (bp->bp_edge_delay_timer.active == 0)
1678 			bstp_edge_delay_expiry(bs, bp);
1679 	} else
1680 		bp->bp_flags &= ~BSTP_PORT_AUTOEDGE;
1681 	BSTP_UNLOCK(bs);
1682 	return (0);
1683 }
1684 
1685 int
1686 bstp_set_ptp(struct bstp_port *bp, int set)
1687 {
1688 	struct bstp_state *bs = bp->bp_bs;
1689 
1690 	BSTP_LOCK(bs);
1691 	bp->bp_ptp_link = set;
1692 	BSTP_UNLOCK(bs);
1693 	return (0);
1694 }
1695 
1696 int
1697 bstp_set_autoptp(struct bstp_port *bp, int set)
1698 {
1699 	struct bstp_state *bs = bp->bp_bs;
1700 
1701 	BSTP_LOCK(bs);
1702 	if (set) {
1703 		bp->bp_flags |= BSTP_PORT_AUTOPTP;
1704 		if (bp->bp_role != BSTP_ROLE_DISABLED)
1705 			taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
1706 	} else
1707 		bp->bp_flags &= ~BSTP_PORT_AUTOPTP;
1708 	BSTP_UNLOCK(bs);
1709 	return (0);
1710 }
1711 
1712 /*
1713  * Calculate the path cost according to the link speed.
1714  */
1715 static uint32_t
1716 bstp_calc_path_cost(struct bstp_port *bp)
1717 {
1718 	struct ifnet *ifp = bp->bp_ifp;
1719 	uint32_t path_cost;
1720 
1721 	/* If the priority has been manually set then retain the value */
1722 	if (bp->bp_flags & BSTP_PORT_ADMCOST)
1723 		return bp->bp_path_cost;
1724 
1725 	if (ifp->if_link_state == LINK_STATE_DOWN) {
1726 		/* Recalc when the link comes up again */
1727 		bp->bp_flags |= BSTP_PORT_PNDCOST;
1728 		return (BSTP_DEFAULT_PATH_COST);
1729 	}
1730 
1731 	if (ifp->if_baudrate < 1000)
1732 		return (BSTP_DEFAULT_PATH_COST);
1733 
1734  	/* formula from section 17.14, IEEE Std 802.1D-2004 */
1735 	path_cost = 20000000000ULL / (ifp->if_baudrate / 1000);
1736 
1737 	if (path_cost > BSTP_MAX_PATH_COST)
1738 		path_cost = BSTP_MAX_PATH_COST;
1739 
1740 	/* STP compat mode only uses 16 bits of the 32 */
1741 	if (bp->bp_protover == BSTP_PROTO_STP && path_cost > 65535)
1742 		path_cost = 65535;
1743 
1744 	return (path_cost);
1745 }
1746 
1747 /*
1748  * Notify the bridge that a port state has changed, we need to do this from a
1749  * taskqueue to avoid a LOR.
1750  */
1751 static void
1752 bstp_notify_state(void *arg, int pending)
1753 {
1754 	struct bstp_port *bp = (struct bstp_port *)arg;
1755 	struct bstp_state *bs = bp->bp_bs;
1756 
1757 	if (bp->bp_active == 1 && bs->bs_state_cb != NULL)
1758 		(*bs->bs_state_cb)(bp->bp_ifp, bp->bp_state);
1759 }
1760 
1761 /*
1762  * Flush the routes on the bridge port, we need to do this from a
1763  * taskqueue to avoid a LOR.
1764  */
1765 static void
1766 bstp_notify_rtage(void *arg, int pending)
1767 {
1768 	struct bstp_port *bp = (struct bstp_port *)arg;
1769 	struct bstp_state *bs = bp->bp_bs;
1770 	int age = 0;
1771 
1772 	BSTP_LOCK(bs);
1773 	switch (bp->bp_protover) {
1774 		case BSTP_PROTO_STP:
1775 			/* convert to seconds */
1776 			age = bp->bp_desg_fdelay / BSTP_TICK_VAL;
1777 			break;
1778 
1779 		case BSTP_PROTO_RSTP:
1780 			age = 0;
1781 			break;
1782 	}
1783 	BSTP_UNLOCK(bs);
1784 
1785 	if (bp->bp_active == 1 && bs->bs_rtage_cb != NULL)
1786 		(*bs->bs_rtage_cb)(bp->bp_ifp, age);
1787 
1788 	/* flush is complete */
1789 	BSTP_LOCK(bs);
1790 	bp->bp_fdbflush = 0;
1791 	BSTP_UNLOCK(bs);
1792 }
1793 
1794 void
1795 bstp_linkstate(struct bstp_port *bp)
1796 {
1797 	struct bstp_state *bs = bp->bp_bs;
1798 
1799 	if (!bp->bp_active)
1800 		return;
1801 
1802 	bstp_ifupdstatus(bp, 0);
1803 	BSTP_LOCK(bs);
1804 	bstp_update_state(bs, bp);
1805 	BSTP_UNLOCK(bs);
1806 }
1807 
1808 static void
1809 bstp_ifupdstatus(void *arg, int pending)
1810 {
1811 	struct bstp_port *bp = (struct bstp_port *)arg;
1812 	struct bstp_state *bs = bp->bp_bs;
1813 	struct ifnet *ifp = bp->bp_ifp;
1814 	struct ifmediareq ifmr;
1815 	int error, changed;
1816 
1817 	if (!bp->bp_active)
1818 		return;
1819 
1820 	bzero((char *)&ifmr, sizeof(ifmr));
1821 	error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
1822 
1823 	BSTP_LOCK(bs);
1824 	changed = 0;
1825 	if ((error == 0) && (ifp->if_flags & IFF_UP)) {
1826 		if (ifmr.ifm_status & IFM_ACTIVE) {
1827 			/* A full-duplex link is assumed to be point to point */
1828 			if (bp->bp_flags & BSTP_PORT_AUTOPTP) {
1829 				int fdx;
1830 
1831 				fdx = ifmr.ifm_active & IFM_FDX ? 1 : 0;
1832 				if (bp->bp_ptp_link ^ fdx) {
1833 					bp->bp_ptp_link = fdx;
1834 					changed = 1;
1835 				}
1836 			}
1837 
1838 			/* Calc the cost if the link was down previously */
1839 			if (bp->bp_flags & BSTP_PORT_PNDCOST) {
1840 				uint32_t cost;
1841 
1842 				cost = bstp_calc_path_cost(bp);
1843 				if (bp->bp_path_cost != cost) {
1844 					bp->bp_path_cost = cost;
1845 					changed = 1;
1846 				}
1847 				bp->bp_flags &= ~BSTP_PORT_PNDCOST;
1848 			}
1849 
1850 			if (bp->bp_role == BSTP_ROLE_DISABLED) {
1851 				bstp_enable_port(bs, bp);
1852 				changed = 1;
1853 			}
1854 		} else {
1855 			if (bp->bp_role != BSTP_ROLE_DISABLED) {
1856 				bstp_disable_port(bs, bp);
1857 				changed = 1;
1858 				if ((bp->bp_flags & BSTP_PORT_ADMEDGE) &&
1859 				    bp->bp_protover == BSTP_PROTO_RSTP)
1860 					bp->bp_operedge = 1;
1861 			}
1862 		}
1863 	} else if (bp->bp_infois != BSTP_INFO_DISABLED) {
1864 		bstp_disable_port(bs, bp);
1865 		changed = 1;
1866 	}
1867 	if (changed)
1868 		bstp_assign_roles(bs);
1869 	BSTP_UNLOCK(bs);
1870 }
1871 
1872 static void
1873 bstp_enable_port(struct bstp_state *bs, struct bstp_port *bp)
1874 {
1875 	bp->bp_infois = BSTP_INFO_AGED;
1876 }
1877 
1878 static void
1879 bstp_disable_port(struct bstp_state *bs, struct bstp_port *bp)
1880 {
1881 	bp->bp_infois = BSTP_INFO_DISABLED;
1882 }
1883 
1884 static void
1885 bstp_tick(void *arg)
1886 {
1887 	struct epoch_tracker et;
1888 	struct bstp_state *bs = arg;
1889 	struct bstp_port *bp;
1890 
1891 	BSTP_LOCK_ASSERT(bs);
1892 
1893 	if (bs->bs_running == 0)
1894 		return;
1895 
1896 	NET_EPOCH_ENTER(et);
1897 	CURVNET_SET(bs->bs_vnet);
1898 
1899 	/* poll link events on interfaces that do not support linkstate */
1900 	if (bstp_timer_dectest(&bs->bs_link_timer)) {
1901 		LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1902 			if (!(bp->bp_ifp->if_capabilities & IFCAP_LINKSTATE))
1903 				taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
1904 		}
1905 		bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER);
1906 	}
1907 
1908 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
1909 		/* no events need to happen for these */
1910 		bstp_timer_dectest(&bp->bp_tc_timer);
1911 		bstp_timer_dectest(&bp->bp_recent_root_timer);
1912 		bstp_timer_dectest(&bp->bp_forward_delay_timer);
1913 		bstp_timer_dectest(&bp->bp_recent_backup_timer);
1914 
1915 		if (bstp_timer_dectest(&bp->bp_hello_timer))
1916 			bstp_hello_timer_expiry(bs, bp);
1917 
1918 		if (bstp_timer_dectest(&bp->bp_message_age_timer))
1919 			bstp_message_age_expiry(bs, bp);
1920 
1921 		if (bstp_timer_dectest(&bp->bp_migrate_delay_timer))
1922 			bstp_migrate_delay_expiry(bs, bp);
1923 
1924 		if (bstp_timer_dectest(&bp->bp_edge_delay_timer))
1925 			bstp_edge_delay_expiry(bs, bp);
1926 
1927 		/* update the various state machines for the port */
1928 		bstp_update_state(bs, bp);
1929 
1930 		if (bp->bp_txcount > 0)
1931 			bp->bp_txcount--;
1932 	}
1933 
1934 	CURVNET_RESTORE();
1935 	NET_EPOCH_EXIT(et);
1936 
1937 	callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
1938 }
1939 
1940 static void
1941 bstp_timer_start(struct bstp_timer *t, uint16_t v)
1942 {
1943 	t->value = v;
1944 	t->active = 1;
1945 	t->latched = 0;
1946 }
1947 
1948 static void
1949 bstp_timer_stop(struct bstp_timer *t)
1950 {
1951 	t->value = 0;
1952 	t->active = 0;
1953 	t->latched = 0;
1954 }
1955 
1956 static void
1957 bstp_timer_latch(struct bstp_timer *t)
1958 {
1959 	t->latched = 1;
1960 	t->active = 1;
1961 }
1962 
1963 static int
1964 bstp_timer_dectest(struct bstp_timer *t)
1965 {
1966 	if (t->active == 0 || t->latched)
1967 		return (0);
1968 	t->value -= BSTP_TICK_VAL;
1969 	if (t->value <= 0) {
1970 		bstp_timer_stop(t);
1971 		return (1);
1972 	}
1973 	return (0);
1974 }
1975 
1976 static void
1977 bstp_hello_timer_expiry(struct bstp_state *bs, struct bstp_port *bp)
1978 {
1979 	if ((bp->bp_flags & BSTP_PORT_NEWINFO) ||
1980 	    bp->bp_role == BSTP_ROLE_DESIGNATED ||
1981 	    (bp->bp_role == BSTP_ROLE_ROOT &&
1982 	     bp->bp_tc_timer.active == 1)) {
1983 		bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime);
1984 		bp->bp_flags |= BSTP_PORT_NEWINFO;
1985 		bstp_transmit(bs, bp);
1986 	}
1987 }
1988 
1989 static void
1990 bstp_message_age_expiry(struct bstp_state *bs, struct bstp_port *bp)
1991 {
1992 	if (bp->bp_infois == BSTP_INFO_RECEIVED) {
1993 		bp->bp_infois = BSTP_INFO_AGED;
1994 		bstp_assign_roles(bs);
1995 		DPRINTF("aged info on %s\n", bp->bp_ifp->if_xname);
1996 	}
1997 }
1998 
1999 static void
2000 bstp_migrate_delay_expiry(struct bstp_state *bs, struct bstp_port *bp)
2001 {
2002 	bp->bp_flags |= BSTP_PORT_CANMIGRATE;
2003 }
2004 
2005 static void
2006 bstp_edge_delay_expiry(struct bstp_state *bs, struct bstp_port *bp)
2007 {
2008 	if ((bp->bp_flags & BSTP_PORT_AUTOEDGE) &&
2009 	    bp->bp_protover == BSTP_PROTO_RSTP && bp->bp_proposing &&
2010 	    bp->bp_role == BSTP_ROLE_DESIGNATED) {
2011 		bp->bp_operedge = 1;
2012 		DPRINTF("%s -> edge port\n", bp->bp_ifp->if_xname);
2013 	}
2014 }
2015 
2016 static int
2017 bstp_addr_cmp(const uint8_t *a, const uint8_t *b)
2018 {
2019 	int i, d;
2020 
2021 	for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2022 		d = ((int)a[i]) - ((int)b[i]);
2023 	}
2024 
2025 	return (d);
2026 }
2027 
2028 /*
2029  * compare the bridge address component of the bridgeid
2030  */
2031 static int
2032 bstp_same_bridgeid(uint64_t id1, uint64_t id2)
2033 {
2034 	u_char addr1[ETHER_ADDR_LEN];
2035 	u_char addr2[ETHER_ADDR_LEN];
2036 
2037 	PV2ADDR(id1, addr1);
2038 	PV2ADDR(id2, addr2);
2039 
2040 	if (bstp_addr_cmp(addr1, addr2) == 0)
2041 		return (1);
2042 
2043 	return (0);
2044 }
2045 
2046 void
2047 bstp_reinit(struct bstp_state *bs)
2048 {
2049 	struct epoch_tracker et;
2050 	struct bstp_port *bp;
2051 	struct ifnet *ifp, *mif;
2052 	u_char *e_addr;
2053 	void *bridgeptr;
2054 	static const u_char llzero[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
2055 
2056 	BSTP_LOCK_ASSERT(bs);
2057 
2058 	if (LIST_EMPTY(&bs->bs_bplist))
2059 		goto disablestp;
2060 
2061 	mif = NULL;
2062 	bridgeptr = LIST_FIRST(&bs->bs_bplist)->bp_ifp->if_bridge;
2063 	KASSERT(bridgeptr != NULL, ("Invalid bridge pointer"));
2064 	/*
2065 	 * Search through the Ethernet adapters and find the one with the
2066 	 * lowest value. Make sure the adapter which we take the MAC address
2067 	 * from is part of this bridge, so we can have more than one independent
2068 	 * bridges in the same STP domain.
2069 	 */
2070 	NET_EPOCH_ENTER(et);
2071 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2072 		if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
2073 			continue;	/* Not Ethernet */
2074 
2075 		if (ifp->if_bridge != bridgeptr)
2076 			continue;	/* Not part of our bridge */
2077 
2078 		if (bstp_addr_cmp(IF_LLADDR(ifp), llzero) == 0)
2079 			continue;	/* No mac address set */
2080 
2081 		if (mif == NULL) {
2082 			mif = ifp;
2083 			continue;
2084 		}
2085 		if (bstp_addr_cmp(IF_LLADDR(ifp), IF_LLADDR(mif)) < 0) {
2086 			mif = ifp;
2087 			continue;
2088 		}
2089 	}
2090 	NET_EPOCH_EXIT(et);
2091 	if (mif == NULL)
2092 		goto disablestp;
2093 
2094 	e_addr = IF_LLADDR(mif);
2095 	bs->bs_bridge_pv.pv_dbridge_id =
2096 	    (((uint64_t)bs->bs_bridge_priority) << 48) |
2097 	    (((uint64_t)e_addr[0]) << 40) |
2098 	    (((uint64_t)e_addr[1]) << 32) |
2099 	    (((uint64_t)e_addr[2]) << 24) |
2100 	    (((uint64_t)e_addr[3]) << 16) |
2101 	    (((uint64_t)e_addr[4]) << 8) |
2102 	    (((uint64_t)e_addr[5]));
2103 
2104 	bs->bs_bridge_pv.pv_root_id = bs->bs_bridge_pv.pv_dbridge_id;
2105 	bs->bs_bridge_pv.pv_cost = 0;
2106 	bs->bs_bridge_pv.pv_dport_id = 0;
2107 	bs->bs_bridge_pv.pv_port_id = 0;
2108 
2109 	if (bs->bs_running && callout_pending(&bs->bs_bstpcallout) == 0)
2110 		callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
2111 
2112 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
2113 		bp->bp_port_id = (bp->bp_priority << 8) |
2114 		    (bp->bp_ifp->if_index  & 0xfff);
2115 		taskqueue_enqueue(taskqueue_swi, &bp->bp_mediatask);
2116 	}
2117 
2118 	bstp_assign_roles(bs);
2119 	bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER);
2120 	return;
2121 
2122 disablestp:
2123 	/* Set the bridge and root id (lower bits) to zero */
2124 	bs->bs_bridge_pv.pv_dbridge_id =
2125 	    ((uint64_t)bs->bs_bridge_priority) << 48;
2126 	bs->bs_bridge_pv.pv_root_id = bs->bs_bridge_pv.pv_dbridge_id;
2127 	bs->bs_root_pv = bs->bs_bridge_pv;
2128 	/* Disable any remaining ports, they will have no MAC address */
2129 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
2130 		bp->bp_infois = BSTP_INFO_DISABLED;
2131 		bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
2132 	}
2133 	callout_stop(&bs->bs_bstpcallout);
2134 }
2135 
2136 static int
2137 bstp_modevent(module_t mod, int type, void *data)
2138 {
2139 	switch (type) {
2140 	case MOD_LOAD:
2141 		mtx_init(&bstp_list_mtx, "bridgestp list", NULL, MTX_DEF);
2142 		LIST_INIT(&bstp_list);
2143 		break;
2144 	case MOD_UNLOAD:
2145 		mtx_destroy(&bstp_list_mtx);
2146 		break;
2147 	default:
2148 		return (EOPNOTSUPP);
2149 	}
2150 	return (0);
2151 }
2152 
2153 static moduledata_t bstp_mod = {
2154 	"bridgestp",
2155 	bstp_modevent,
2156 	0
2157 };
2158 
2159 DECLARE_MODULE(bridgestp, bstp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2160 MODULE_VERSION(bridgestp, 1);
2161 
2162 void
2163 bstp_attach(struct bstp_state *bs, struct bstp_cb_ops *cb)
2164 {
2165 	BSTP_LOCK_INIT(bs);
2166 	callout_init_mtx(&bs->bs_bstpcallout, &bs->bs_mtx, 0);
2167 	LIST_INIT(&bs->bs_bplist);
2168 
2169 	bs->bs_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
2170 	bs->bs_bridge_htime = BSTP_DEFAULT_HELLO_TIME;
2171 	bs->bs_bridge_fdelay = BSTP_DEFAULT_FORWARD_DELAY;
2172 	bs->bs_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
2173 	bs->bs_hold_time = BSTP_DEFAULT_HOLD_TIME;
2174 	bs->bs_migration_delay = BSTP_DEFAULT_MIGRATE_DELAY;
2175 	bs->bs_txholdcount = BSTP_DEFAULT_HOLD_COUNT;
2176 	bs->bs_protover = BSTP_PROTO_RSTP;
2177 	bs->bs_state_cb = cb->bcb_state;
2178 	bs->bs_rtage_cb = cb->bcb_rtage;
2179 	bs->bs_vnet = curvnet;
2180 
2181 	getmicrotime(&bs->bs_last_tc_time);
2182 
2183 	mtx_lock(&bstp_list_mtx);
2184 	LIST_INSERT_HEAD(&bstp_list, bs, bs_list);
2185 	mtx_unlock(&bstp_list_mtx);
2186 }
2187 
2188 void
2189 bstp_detach(struct bstp_state *bs)
2190 {
2191 	KASSERT(LIST_EMPTY(&bs->bs_bplist), ("bstp still active"));
2192 
2193 	mtx_lock(&bstp_list_mtx);
2194 	LIST_REMOVE(bs, bs_list);
2195 	mtx_unlock(&bstp_list_mtx);
2196 	callout_drain(&bs->bs_bstpcallout);
2197 	BSTP_LOCK_DESTROY(bs);
2198 }
2199 
2200 void
2201 bstp_init(struct bstp_state *bs)
2202 {
2203 	BSTP_LOCK(bs);
2204 	callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs);
2205 	bs->bs_running = 1;
2206 	bstp_reinit(bs);
2207 	BSTP_UNLOCK(bs);
2208 }
2209 
2210 void
2211 bstp_stop(struct bstp_state *bs)
2212 {
2213 	struct bstp_port *bp;
2214 
2215 	BSTP_LOCK(bs);
2216 
2217 	LIST_FOREACH(bp, &bs->bs_bplist, bp_next)
2218 		bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
2219 
2220 	bs->bs_running = 0;
2221 	callout_stop(&bs->bs_bstpcallout);
2222 	BSTP_UNLOCK(bs);
2223 }
2224 
2225 int
2226 bstp_create(struct bstp_state *bs, struct bstp_port *bp, struct ifnet *ifp)
2227 {
2228 	bzero(bp, sizeof(struct bstp_port));
2229 
2230 	BSTP_LOCK(bs);
2231 	bp->bp_ifp = ifp;
2232 	bp->bp_bs = bs;
2233 	bp->bp_priority = BSTP_DEFAULT_PORT_PRIORITY;
2234 	TASK_INIT(&bp->bp_statetask, 0, bstp_notify_state, bp);
2235 	TASK_INIT(&bp->bp_rtagetask, 0, bstp_notify_rtage, bp);
2236 	TASK_INIT(&bp->bp_mediatask, 0, bstp_ifupdstatus, bp);
2237 
2238 	/* Init state */
2239 	bp->bp_infois = BSTP_INFO_DISABLED;
2240 	bp->bp_flags = BSTP_PORT_AUTOEDGE|BSTP_PORT_AUTOPTP;
2241 	bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING);
2242 	bstp_set_port_proto(bp, bs->bs_protover);
2243 	bstp_set_port_role(bp, BSTP_ROLE_DISABLED);
2244 	bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE);
2245 	bp->bp_path_cost = bstp_calc_path_cost(bp);
2246 	BSTP_UNLOCK(bs);
2247 	return (0);
2248 }
2249 
2250 int
2251 bstp_enable(struct bstp_port *bp)
2252 {
2253 	struct bstp_state *bs = bp->bp_bs;
2254 	struct ifnet *ifp = bp->bp_ifp;
2255 
2256 	KASSERT(bp->bp_active == 0, ("already a bstp member"));
2257 	NET_EPOCH_ASSERT(); /* Because bstp_update_roles() causes traffic. */
2258 
2259 	switch (ifp->if_type) {
2260 		case IFT_ETHER:	/* These can do spanning tree. */
2261 		case IFT_L2VLAN:
2262 			break;
2263 		default:
2264 			/* Nothing else can. */
2265 			return (EINVAL);
2266 	}
2267 
2268 	BSTP_LOCK(bs);
2269 	LIST_INSERT_HEAD(&bs->bs_bplist, bp, bp_next);
2270 	bp->bp_active = 1;
2271 	bp->bp_flags |= BSTP_PORT_NEWINFO;
2272 	bstp_reinit(bs);
2273 	bstp_update_roles(bs, bp);
2274 	BSTP_UNLOCK(bs);
2275 	return (0);
2276 }
2277 
2278 void
2279 bstp_disable(struct bstp_port *bp)
2280 {
2281 	struct bstp_state *bs = bp->bp_bs;
2282 
2283 	KASSERT(bp->bp_active == 1, ("not a bstp member"));
2284 
2285 	BSTP_LOCK(bs);
2286 	bstp_disable_port(bs, bp);
2287 	LIST_REMOVE(bp, bp_next);
2288 	bp->bp_active = 0;
2289 	bstp_reinit(bs);
2290 	BSTP_UNLOCK(bs);
2291 }
2292 
2293 /*
2294  * The bstp_port structure is about to be freed by the parent bridge.
2295  */
2296 void
2297 bstp_destroy(struct bstp_port *bp)
2298 {
2299 	KASSERT(bp->bp_active == 0, ("port is still attached"));
2300 	taskqueue_drain(taskqueue_swi, &bp->bp_statetask);
2301 	taskqueue_drain(taskqueue_swi, &bp->bp_rtagetask);
2302 	taskqueue_drain(taskqueue_swi, &bp->bp_mediatask);
2303 
2304 	if (bp->bp_bs->bs_root_port == bp)
2305 		bstp_assign_roles(bp->bp_bs);
2306 }
2307