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