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