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