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