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