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