1 /* 2 * Copyright (c) 2000 Jason L. Wright (jason@thought.net) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Jason L. Wright 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $OpenBSD: bridgestp.c,v 1.5 2001/03/22 03:48:29 jason Exp $ 32 * $NetBSD: bridgestp.c,v 1.5 2003/11/28 08:56:48 keihan Exp $ 33 * $FreeBSD: src/sys/net/bridgestp.c,v 1.7 2005/10/11 02:58:32 thompsa Exp $ 34 */ 35 36 /* 37 * Implementation of the spanning tree protocol as defined in 38 * ISO/IEC Final DIS 15802-3 (IEEE P802.1D/D17), May 25, 1998. 39 * (In English: IEEE 802.1D, Draft 17, 1998) 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/mbuf.h> 45 #include <sys/socket.h> 46 #include <sys/sockio.h> 47 #include <sys/kernel.h> 48 #include <sys/callout.h> 49 #include <sys/proc.h> 50 #include <sys/lock.h> 51 #include <sys/thread.h> 52 #include <sys/thread2.h> 53 #include <sys/msgport2.h> 54 55 #include <net/if.h> 56 #include <net/if_dl.h> 57 #include <net/if_types.h> 58 #include <net/if_llc.h> 59 #include <net/if_media.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/bridge/if_bridgevar.h> 66 67 /* BPDU message types */ 68 #define BSTP_MSGTYPE_CFG 0x00 /* Configuration */ 69 #define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */ 70 71 /* BPDU flags */ 72 #define BSTP_FLAG_TC 0x01 /* Topology change */ 73 #define BSTP_FLAG_TCA 0x80 /* Topology change ack */ 74 75 #define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */ 76 #define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */ 77 78 /* 79 * Because BPDU's do not make nicely aligned structures, two different 80 * declarations are used: bstp_?bpdu (wire representation, packed) and 81 * bstp_*_unit (internal, nicely aligned version). 82 */ 83 84 /* configuration bridge protocol data unit */ 85 struct bstp_cbpdu { 86 uint8_t cbu_dsap; /* LLC: destination sap */ 87 uint8_t cbu_ssap; /* LLC: source sap */ 88 uint8_t cbu_ctl; /* LLC: control */ 89 uint16_t cbu_protoid; /* protocol id */ 90 uint8_t cbu_protover; /* protocol version */ 91 uint8_t cbu_bpdutype; /* message type */ 92 uint8_t cbu_flags; /* flags (below) */ 93 94 /* root id */ 95 uint16_t cbu_rootpri; /* root priority */ 96 uint8_t cbu_rootaddr[6]; /* root address */ 97 98 uint32_t cbu_rootpathcost; /* root path cost */ 99 100 /* bridge id */ 101 uint16_t cbu_bridgepri; /* bridge priority */ 102 uint8_t cbu_bridgeaddr[6]; /* bridge address */ 103 104 uint16_t cbu_portid; /* port id */ 105 uint16_t cbu_messageage; /* current message age */ 106 uint16_t cbu_maxage; /* maximum age */ 107 uint16_t cbu_hellotime; /* hello time */ 108 uint16_t cbu_forwarddelay; /* forwarding delay */ 109 } __attribute__((__packed__)); 110 111 /* topology change notification bridge protocol data unit */ 112 struct bstp_tbpdu { 113 uint8_t tbu_dsap; /* LLC: destination sap */ 114 uint8_t tbu_ssap; /* LLC: source sap */ 115 uint8_t tbu_ctl; /* LLC: control */ 116 uint16_t tbu_protoid; /* protocol id */ 117 uint8_t tbu_protover; /* protocol version */ 118 uint8_t tbu_bpdutype; /* message type */ 119 } __attribute__((__packed__)); 120 121 const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }; 122 123 static void bstp_initialize_port(struct bridge_softc *, 124 struct bridge_iflist *); 125 static void bstp_ifupdstatus(struct bridge_softc *, struct bridge_iflist *); 126 static void bstp_enable_port(struct bridge_softc *, struct bridge_iflist *); 127 static void bstp_disable_port(struct bridge_softc *, 128 struct bridge_iflist *); 129 #ifdef notused 130 static void bstp_enable_change_detection(struct bridge_iflist *); 131 static void bstp_disable_change_detection(struct bridge_iflist *); 132 #endif /* notused */ 133 static int bstp_root_bridge(struct bridge_softc *sc); 134 static void bstp_transmit_config(struct bridge_softc *, 135 struct bridge_iflist *); 136 static void bstp_transmit_tcn(struct bridge_softc *); 137 static void bstp_received_config_bpdu(struct bridge_softc *, 138 struct bridge_iflist *, struct bstp_config_unit *); 139 static void bstp_received_tcn_bpdu(struct bridge_softc *, 140 struct bridge_iflist *, struct bstp_tcn_unit *); 141 static void bstp_record_config_information(struct bridge_softc *, 142 struct bridge_iflist *, struct bstp_config_unit *); 143 static void bstp_record_config_timeout_values(struct bridge_softc *, 144 struct bstp_config_unit *); 145 static void bstp_config_bpdu_generation(struct bridge_softc *); 146 static void bstp_send_config_bpdu(struct bridge_softc *, 147 struct bridge_iflist *, struct bstp_config_unit *); 148 static void bstp_configuration_update(struct bridge_softc *); 149 static void bstp_port_state_selection(struct bridge_softc *); 150 static void bstp_clear_peer_info(struct bridge_softc *, 151 struct bridge_iflist *); 152 static void bstp_make_forwarding(struct bridge_softc *, 153 struct bridge_iflist *); 154 static void bstp_make_blocking(struct bridge_softc *, 155 struct bridge_iflist *); 156 static void bstp_make_l1blocking(struct bridge_softc *sc, 157 struct bridge_iflist *bif); 158 static void bstp_adjust_bonded_states(struct bridge_softc *sc, 159 struct bridge_iflist *obif); 160 static void bstp_set_port_state(struct bridge_iflist *, uint8_t); 161 #ifdef notused 162 static void bstp_set_bridge_priority(struct bridge_softc *, uint64_t); 163 static void bstp_set_port_priority(struct bridge_softc *, 164 struct bridge_iflist *, uint16_t); 165 static void bstp_set_path_cost(struct bridge_softc *, 166 struct bridge_iflist *, uint32_t); 167 #endif /* notused */ 168 static void bstp_topology_change_detection(struct bridge_softc *); 169 static void bstp_topology_change_acknowledged(struct bridge_softc *); 170 static void bstp_acknowledge_topology_change(struct bridge_softc *, 171 struct bridge_iflist *); 172 173 static void bstp_tick(void *); 174 static void bstp_timer_start(struct bridge_timer *, uint16_t); 175 static void bstp_timer_stop(struct bridge_timer *); 176 static int bstp_timer_expired(struct bridge_timer *, uint16_t); 177 178 static void bstp_hold_timer_expiry(struct bridge_softc *, 179 struct bridge_iflist *); 180 static void bstp_message_age_timer_expiry(struct bridge_softc *, 181 struct bridge_iflist *); 182 static void bstp_forward_delay_timer_expiry(struct bridge_softc *, 183 struct bridge_iflist *); 184 static void bstp_topology_change_timer_expiry(struct bridge_softc *); 185 static void bstp_tcn_timer_expiry(struct bridge_softc *); 186 static void bstp_hello_timer_expiry(struct bridge_softc *); 187 static int bstp_addr_cmp(const uint8_t *, const uint8_t *); 188 189 /* 190 * When transmitting a config we tack on our path cost to 191 * our aggregated path-to-root cost. 192 */ 193 static void 194 bstp_transmit_config(struct bridge_softc *sc, struct bridge_iflist *bif) 195 { 196 if (bif->bif_hold_timer.active) { 197 bif->bif_config_pending = 1; 198 return; 199 } 200 201 bif->bif_config_bpdu.cu_message_type = BSTP_MSGTYPE_CFG; 202 bif->bif_config_bpdu.cu_rootid = sc->sc_designated_root; 203 bif->bif_config_bpdu.cu_root_path_cost = sc->sc_designated_cost + 204 bif->bif_path_cost; 205 bif->bif_config_bpdu.cu_bridge_id = sc->sc_bridge_id; 206 bif->bif_config_bpdu.cu_port_id = bif->bif_port_id; 207 208 if (bstp_root_bridge(sc)) { 209 bif->bif_config_bpdu.cu_message_age = 0; 210 } else if (sc->sc_root_port) { 211 bif->bif_config_bpdu.cu_message_age = 212 sc->sc_root_port->bif_message_age_timer.value + 213 BSTP_MESSAGE_AGE_INCR; 214 } else { 215 bif->bif_config_bpdu.cu_message_age = BSTP_MESSAGE_AGE_INCR; 216 } 217 218 bif->bif_config_bpdu.cu_max_age = sc->sc_max_age; 219 bif->bif_config_bpdu.cu_hello_time = sc->sc_hello_time; 220 bif->bif_config_bpdu.cu_forward_delay = sc->sc_forward_delay; 221 bif->bif_config_bpdu.cu_topology_change_acknowledgment 222 = bif->bif_topology_change_acknowledge; 223 bif->bif_config_bpdu.cu_topology_change = sc->sc_topology_change; 224 225 if (bif->bif_config_bpdu.cu_message_age < sc->sc_max_age || 226 (sc->sc_ifp->if_flags & IFF_LINK1)) { 227 bif->bif_topology_change_acknowledge = 0; 228 bif->bif_config_pending = 0; 229 bstp_send_config_bpdu(sc, bif, &bif->bif_config_bpdu); 230 bstp_timer_start(&bif->bif_hold_timer, 0); 231 } 232 } 233 234 static void 235 bstp_send_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif, 236 struct bstp_config_unit *cu) 237 { 238 struct ifnet *ifp; 239 struct mbuf *m; 240 struct ether_header *eh; 241 struct bstp_cbpdu bpdu; 242 243 ifp = bif->bif_ifp; 244 245 if ((ifp->if_flags & IFF_RUNNING) == 0) 246 return; 247 248 MGETHDR(m, MB_DONTWAIT, MT_DATA); 249 if (m == NULL) 250 return; 251 252 eh = mtod(m, struct ether_header *); 253 254 m->m_pkthdr.rcvif = ifp; 255 m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu); 256 m->m_len = m->m_pkthdr.len; 257 258 bpdu.cbu_ssap = bpdu.cbu_dsap = LLC_8021D_LSAP; 259 bpdu.cbu_ctl = LLC_UI; 260 bpdu.cbu_protoid = htons(0); 261 bpdu.cbu_protover = 0; 262 bpdu.cbu_bpdutype = cu->cu_message_type; 263 bpdu.cbu_flags = (cu->cu_topology_change ? BSTP_FLAG_TC : 0) | 264 (cu->cu_topology_change_acknowledgment ? BSTP_FLAG_TCA : 0); 265 266 bpdu.cbu_rootpri = htons(cu->cu_rootid >> 48); 267 bpdu.cbu_rootaddr[0] = cu->cu_rootid >> 40; 268 bpdu.cbu_rootaddr[1] = cu->cu_rootid >> 32; 269 bpdu.cbu_rootaddr[2] = cu->cu_rootid >> 24; 270 bpdu.cbu_rootaddr[3] = cu->cu_rootid >> 16; 271 bpdu.cbu_rootaddr[4] = cu->cu_rootid >> 8; 272 bpdu.cbu_rootaddr[5] = cu->cu_rootid >> 0; 273 274 bpdu.cbu_rootpathcost = htonl(cu->cu_root_path_cost); 275 276 bpdu.cbu_bridgepri = htons(cu->cu_bridge_id >> 48); 277 bpdu.cbu_bridgeaddr[0] = cu->cu_bridge_id >> 40; 278 bpdu.cbu_bridgeaddr[1] = cu->cu_bridge_id >> 32; 279 bpdu.cbu_bridgeaddr[2] = cu->cu_bridge_id >> 24; 280 bpdu.cbu_bridgeaddr[3] = cu->cu_bridge_id >> 16; 281 bpdu.cbu_bridgeaddr[4] = cu->cu_bridge_id >> 8; 282 bpdu.cbu_bridgeaddr[5] = cu->cu_bridge_id >> 0; 283 284 bpdu.cbu_portid = htons(cu->cu_port_id); 285 bpdu.cbu_messageage = htons(cu->cu_message_age); 286 bpdu.cbu_maxage = htons(cu->cu_max_age); 287 bpdu.cbu_hellotime = htons(cu->cu_hello_time); 288 bpdu.cbu_forwarddelay = htons(cu->cu_forward_delay); 289 290 /* 291 * Packets sent from the bridge always use the bridge MAC 292 * as the source. 293 */ 294 memcpy(eh->ether_shost, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 295 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN); 296 eh->ether_type = htons(sizeof(bpdu)); 297 298 memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu)); 299 300 bridge_enqueue(ifp, m); 301 } 302 303 static int 304 bstp_root_bridge(struct bridge_softc *sc) 305 { 306 return (sc->sc_designated_root == sc->sc_bridge_id); 307 } 308 309 /* 310 * Returns TRUE if the recorded information from our peer has a shorter 311 * graph distance than our current best. 312 */ 313 int 314 bstp_supersedes_port_info(struct bridge_softc *sc, struct bridge_iflist *bif) 315 { 316 if (bif->bif_peer_root < sc->sc_designated_root) 317 return (1); 318 if (bif->bif_peer_root > sc->sc_designated_root) 319 return (0); 320 321 /* 322 * Both bif_peer_cost and sc_designated_cost have NOT added in 323 * bif->bif_path_cost, so we can optimize it out. 324 */ 325 if (bif->bif_peer_cost < sc->sc_designated_cost) 326 return (1); 327 if (bif->bif_peer_cost > sc->sc_designated_cost) 328 return (0); 329 330 if (bif->bif_peer_bridge < sc->sc_designated_bridge) 331 return (1); 332 if (bif->bif_peer_bridge > sc->sc_designated_bridge) 333 return (0); 334 335 /* bridge_id or bridge+port collision w/peer returns TRUE */ 336 if (bif->bif_peer_bridge != sc->sc_bridge_id) 337 return (1); 338 if (bif->bif_peer_port <= sc->sc_designated_port) 339 return (1); 340 return (0); 341 } 342 343 /* 344 * The shorter graph distance represented by cu (most of which is also 345 * already stored in our bif_peer_* fields) becomes the designated info. 346 * 347 * NOTE: sc_designated_cost does not include bif_path_cost, it is added 348 * in later on a port-by-port basis as needed. 349 */ 350 static void 351 bstp_record_config_information(struct bridge_softc *sc, 352 struct bridge_iflist *bif, 353 struct bstp_config_unit *cu) 354 { 355 sc->sc_designated_root = bif->bif_peer_root; 356 sc->sc_designated_cost = bif->bif_peer_cost; 357 sc->sc_designated_bridge = bif->bif_peer_bridge; 358 sc->sc_designated_port = bif->bif_peer_port; 359 bstp_timer_start(&bif->bif_message_age_timer, cu->cu_message_age); 360 } 361 362 static void 363 bstp_record_config_timeout_values(struct bridge_softc *sc, 364 struct bstp_config_unit *config) 365 { 366 sc->sc_max_age = config->cu_max_age; 367 sc->sc_hello_time = config->cu_hello_time; 368 sc->sc_forward_delay = config->cu_forward_delay; 369 sc->sc_topology_change = config->cu_topology_change; 370 } 371 372 static void 373 bstp_config_bpdu_generation(struct bridge_softc *sc) 374 { 375 struct bridge_iflist *bif, *nbif; 376 377 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) { 378 if ((bif->bif_flags & IFBIF_STP) == 0) 379 continue; 380 if (bif->bif_state != BSTP_IFSTATE_DISABLED && 381 ((sc->sc_ifp->if_flags & IFF_LINK1) || 382 (bif->bif_flags & IFBIF_DESIGNATED))) { 383 bstp_transmit_config(sc, bif); 384 } 385 386 if (nbif != NULL && !nbif->bif_onlist) { 387 KKASSERT(bif->bif_onlist); 388 nbif = TAILQ_NEXT(bif, bif_next); 389 } 390 } 391 } 392 393 static void 394 bstp_transmit_tcn(struct bridge_softc *sc) 395 { 396 struct bstp_tbpdu bpdu; 397 struct ifnet *ifp; 398 struct ether_header *eh; 399 struct mbuf *m; 400 401 if (sc->sc_root_port == NULL) /* all iterfaces disabled */ 402 return; 403 404 ifp = sc->sc_root_port->bif_ifp; 405 if ((ifp->if_flags & IFF_RUNNING) == 0) 406 return; 407 408 MGETHDR(m, MB_DONTWAIT, MT_DATA); 409 if (m == NULL) 410 return; 411 412 m->m_pkthdr.rcvif = ifp; 413 m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu); 414 m->m_len = m->m_pkthdr.len; 415 416 eh = mtod(m, struct ether_header *); 417 418 /* 419 * Packets sent from the bridge always use the bridge MAC 420 * as the source. 421 */ 422 memcpy(eh->ether_shost, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 423 memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN); 424 eh->ether_type = htons(sizeof(bpdu)); 425 426 bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP; 427 bpdu.tbu_ctl = LLC_UI; 428 bpdu.tbu_protoid = 0; 429 bpdu.tbu_protover = 0; 430 bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN; 431 432 memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu)); 433 434 bridge_enqueue(ifp, m); 435 } 436 437 /* 438 * Recalculate sc->sc_designated* and sc->sc_root_port (if our bridge 439 * is calculated to be the root bridge). We do this by initializing 440 * the designated variables to point at us and then scan our peers. 441 * Any uninitialized peers will have a max-value root. 442 * 443 * Clear IFBIF_DESIGNATED on any ports which no longer match the criteria 444 * required to be a designated port. Only aged out ports and the root 445 * port can be designated. 446 * 447 * If we win we do a second scan to determine which port on our bridge 448 * is the best. 449 */ 450 static void 451 bstp_configuration_update(struct bridge_softc *sc) 452 { 453 uint64_t designated_root = sc->sc_bridge_id; 454 uint64_t designated_bridge = sc->sc_bridge_id; 455 uint32_t designated_cost = 0xFFFFFFFFU; 456 uint16_t designated_port = 65535; 457 struct bridge_iflist *root_port = NULL; 458 struct bridge_iflist *bif; 459 460 /* 461 * Resolve information from our peers. Aged peers will have 462 * a maxed bif_peer_root and not come under consideration. 463 */ 464 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 465 if ((bif->bif_flags & IFBIF_STP) == 0) 466 continue; 467 if (bif->bif_state == BSTP_IFSTATE_DISABLED || 468 bif->bif_state == BSTP_IFSTATE_L1BLOCKING) { 469 continue; 470 } 471 472 if (bif->bif_peer_root > designated_root) 473 continue; 474 if (bif->bif_peer_root < designated_root) 475 goto set_port; 476 477 /* 478 * NOTE: The designated_cost temporary variable already added 479 * in the path code of the related root port. 480 */ 481 if (bif->bif_peer_cost + bif->bif_path_cost > designated_cost) 482 continue; 483 if (bif->bif_peer_cost + bif->bif_path_cost < designated_cost) 484 goto set_port; 485 486 if (bif->bif_peer_bridge > designated_bridge) 487 continue; 488 if (bif->bif_peer_bridge < designated_bridge) 489 goto set_port; 490 491 if (bif->bif_peer_port > designated_port) 492 continue; 493 if (bif->bif_peer_port < designated_port) 494 goto set_port; 495 496 /* 497 * Same root, path cost, bridge, and port. Set the root 498 * only if we do not already have it. 499 */ 500 if (root_port) 501 continue; 502 503 /* 504 * New root port (from peers) 505 * 506 * NOTE: Temporarily add bif_path_cost into the designated 507 * cost to reduce complexity in the loop, it will be 508 * subtracted out when we are done. 509 */ 510 set_port: 511 designated_root = bif->bif_peer_root; 512 designated_cost = bif->bif_peer_cost + bif->bif_path_cost; 513 designated_bridge = bif->bif_peer_bridge; 514 designated_port = bif->bif_peer_port; 515 root_port = bif; 516 } 517 518 /* 519 * root_port will be NULL at the start here if all of our 520 * peers are aged or are not as good a root as our bridge would 521 * be. It can also be NULL due to all related links being 522 * disabled. 523 * 524 * If the root winds up being our bridge scan again against local 525 * information. Unconditionally update IFBIF_DESIGNATED. 526 */ 527 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 528 bif->bif_flags &= ~(IFBIF_DESIGNATED | IFBIF_ROOT); 529 if ((bif->bif_flags & IFBIF_STP) == 0) 530 continue; 531 if (bif->bif_state == BSTP_IFSTATE_DISABLED || 532 bif->bif_state == BSTP_IFSTATE_L1BLOCKING) { 533 continue; 534 } 535 536 /* 537 * Set DESIGNATED for an aged or unknown peer. 538 */ 539 if (bif->bif_peer_bridge == 0xFFFFFFFFFFFFFFFFLLU) 540 bif->bif_flags |= IFBIF_DESIGNATED; 541 if (designated_root != sc->sc_bridge_id) 542 continue; 543 544 /* 545 * This is only reached if our bridge is the root bridge, 546 * select the root port (IFBIF_DESIGNATED is set at the 547 * end). 548 * 549 * We do NOT use peer info here. 550 */ 551 if (bif->bif_path_cost > designated_cost) 552 continue; 553 if (bif->bif_path_cost < designated_cost) 554 goto set_port2; 555 556 if (bif->bif_port_id > designated_port) 557 continue; 558 if (bif->bif_port_id < designated_port) 559 goto set_port2; 560 /* degenerate case (possible peer collision w/our key */ 561 562 /* 563 * New port 564 */ 565 set_port2: 566 designated_cost = bif->bif_path_cost; 567 designated_bridge = sc->sc_bridge_id; 568 designated_port = bif->bif_port_id; 569 root_port = bif; 570 } 571 572 /* 573 * Update aggregate information. The selected root port always 574 * becomes a designated port (along with aged ports). This can 575 * either be the port whos peer is closest to the root or it 576 * can be one of our ports if our bridge is the root. 577 * 578 * The root cost we record in sc_designated_root does not include 579 * bif_path_cost of the root port, since we may transmit this 580 * out of any port we will add the cost back in later on on 581 * a per-port basis. 582 * 583 * root_port can be NULL here (if all links are disabled) 584 */ 585 if (root_port) { 586 sc->sc_designated_root = designated_root; 587 sc->sc_designated_cost = designated_cost - 588 root_port->bif_path_cost; 589 sc->sc_designated_bridge = designated_bridge; 590 sc->sc_designated_port = designated_port; 591 root_port->bif_flags |= IFBIF_DESIGNATED | IFBIF_ROOT; 592 } else { 593 sc->sc_designated_root = designated_root; 594 sc->sc_designated_cost = designated_cost; 595 sc->sc_designated_bridge = designated_bridge; 596 sc->sc_designated_port = designated_port; 597 } 598 sc->sc_root_port = root_port; 599 } 600 601 /* 602 * Calculate the desired state for each interface link on our bridge. 603 * 604 * The best port will match against sc->sc_root_port (whether we are root 605 * or whether that port is the closest to the root). We push this port 606 * towards a FORWARDING state. 607 * 608 * Next come designated ports, either aged ports or ports with no peer info 609 * (yet), or the peer who is closest to the root. We push this port towards 610 * a FORWARDING state as well. 611 * 612 * Any remaining ports are pushed towards a BLOCKING state. Both sides of 613 * the port (us and our peer) should wind up placing the two ends in this 614 * state or bad things happen. 615 */ 616 static void 617 bstp_port_state_selection(struct bridge_softc *sc) 618 { 619 struct bridge_iflist *bif, *nbif; 620 621 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) { 622 if ((bif->bif_flags & IFBIF_STP) == 0) 623 continue; 624 if (sc->sc_root_port && 625 bif->bif_info == sc->sc_root_port->bif_info) { 626 bif->bif_config_pending = 0; 627 bif->bif_topology_change_acknowledge = 0; 628 bstp_make_forwarding(sc, bif); 629 } else if (bif->bif_flags & IFBIF_DESIGNATED) { 630 bstp_timer_stop(&bif->bif_message_age_timer); 631 bstp_make_forwarding(sc, bif); 632 } else { 633 bif->bif_config_pending = 0; 634 bif->bif_topology_change_acknowledge = 0; 635 bstp_make_blocking(sc, bif); 636 } 637 638 if (nbif != NULL && !nbif->bif_onlist) { 639 KKASSERT(bif->bif_onlist); 640 nbif = TAILQ_NEXT(bif, bif_next); 641 } 642 } 643 } 644 645 /* 646 * Clear peer info, effectively makes the port looked aged out. 647 * It becomes a designated go-to port. 648 */ 649 static void 650 bstp_clear_peer_info(struct bridge_softc *sc, struct bridge_iflist *bif) 651 { 652 bif->bif_peer_root = 0xFFFFFFFFFFFFFFFFLLU; 653 bif->bif_peer_cost = 0xFFFFFFFFU; 654 bif->bif_peer_bridge = 0xFFFFFFFFFFFFFFFFLLU; 655 bif->bif_peer_port = 0xFFFFU; 656 657 if (bif->bif_state != BSTP_IFSTATE_DISABLED && 658 bif->bif_state != BSTP_IFSTATE_L1BLOCKING) { 659 bif->bif_flags |= IFBIF_DESIGNATED; 660 } 661 } 662 663 static void 664 bstp_make_forwarding(struct bridge_softc *sc, struct bridge_iflist *bif) 665 { 666 if (bif->bif_state == BSTP_IFSTATE_BLOCKING || 667 bif->bif_state == BSTP_IFSTATE_BONDED) { 668 bstp_set_port_state(bif, BSTP_IFSTATE_LISTENING); 669 bstp_timer_start(&bif->bif_forward_delay_timer, 0); 670 } 671 } 672 673 static void 674 bstp_make_blocking(struct bridge_softc *sc, struct bridge_iflist *bif) 675 { 676 if (bif->bif_state != BSTP_IFSTATE_DISABLED && 677 bif->bif_state != BSTP_IFSTATE_BLOCKING && 678 bif->bif_state != BSTP_IFSTATE_BONDED && 679 bif->bif_state != BSTP_IFSTATE_L1BLOCKING) { 680 if ((bif->bif_state == BSTP_IFSTATE_FORWARDING) || 681 (bif->bif_state == BSTP_IFSTATE_LEARNING)) { 682 if (bif->bif_change_detection_enabled) { 683 bstp_topology_change_detection(sc); 684 } 685 } 686 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING); 687 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN); 688 bstp_timer_stop(&bif->bif_forward_delay_timer); 689 if (sc->sc_ifp->if_flags & IFF_LINK2) 690 bstp_adjust_bonded_states(sc, bif); 691 } 692 } 693 694 static void 695 bstp_make_l1blocking(struct bridge_softc *sc, struct bridge_iflist *bif) 696 { 697 int was_forwarding = (bif->bif_state == BSTP_IFSTATE_FORWARDING); 698 699 switch(bif->bif_state) { 700 case BSTP_IFSTATE_LISTENING: 701 case BSTP_IFSTATE_LEARNING: 702 case BSTP_IFSTATE_FORWARDING: 703 case BSTP_IFSTATE_BLOCKING: 704 case BSTP_IFSTATE_BONDED: 705 bstp_set_port_state(bif, BSTP_IFSTATE_L1BLOCKING); 706 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN); 707 bstp_timer_stop(&bif->bif_forward_delay_timer); 708 bstp_timer_stop(&bif->bif_link1_timer); 709 if (bif->bif_flags & IFBIF_DESIGNATED) { 710 bif->bif_flags &= ~IFBIF_DESIGNATED; 711 bstp_configuration_update(sc); 712 bstp_port_state_selection(sc); 713 } 714 if (was_forwarding && (sc->sc_ifp->if_flags & IFF_LINK2)) 715 bstp_adjust_bonded_states(sc, bif); 716 break; 717 default: 718 break; 719 } 720 } 721 722 /* 723 * Member (bif) changes to or from a FORWARDING state. All members in the 724 * same bonding group which are in a BLOCKING or BONDED state must be set 725 * to either BLOCKING or BONDED based on whether any members in the bonding 726 * group remain in the FORWARDING state. 727 * 728 * Going between the BLOCKING and BONDED states does not require a 729 * configuration update. 730 */ 731 static void 732 bstp_adjust_bonded_states(struct bridge_softc *sc, struct bridge_iflist *obif) 733 { 734 struct bridge_iflist *bif; 735 int state = BSTP_IFSTATE_BLOCKING; 736 737 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 738 if ((bif->bif_flags & IFBIF_STP) == 0) 739 continue; 740 if (bif->bif_state != BSTP_IFSTATE_FORWARDING) 741 continue; 742 if (memcmp(IF_LLADDR(bif->bif_ifp), IF_LLADDR(obif->bif_ifp), 743 ETHER_ADDR_LEN) != 0) { 744 continue; 745 } 746 state = BSTP_IFSTATE_BONDED; 747 break; 748 } 749 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 750 if ((bif->bif_flags & IFBIF_STP) == 0) 751 continue; 752 if (bif->bif_state != BSTP_IFSTATE_BLOCKING && 753 bif->bif_state != BSTP_IFSTATE_BONDED) { 754 continue; 755 } 756 if (memcmp(IF_LLADDR(bif->bif_ifp), IF_LLADDR(obif->bif_ifp), 757 ETHER_ADDR_LEN) != 0) { 758 continue; 759 } 760 if (bif->bif_bond_weight == 0) 761 bif->bif_state = BSTP_IFSTATE_BLOCKING; 762 else 763 bif->bif_state = state; 764 } 765 } 766 767 static void 768 bstp_set_port_state(struct bridge_iflist *bif, uint8_t state) 769 { 770 bif->bif_state = state; 771 } 772 773 static void 774 bstp_topology_change_detection(struct bridge_softc *sc) 775 { 776 if (bstp_root_bridge(sc)) { 777 sc->sc_topology_change = 1; 778 bstp_timer_start(&sc->sc_topology_change_timer, 0); 779 } else if (!sc->sc_topology_change_detected) { 780 bstp_transmit_tcn(sc); 781 bstp_timer_start(&sc->sc_tcn_timer, 0); 782 } 783 sc->sc_topology_change_detected = 1; 784 } 785 786 static void 787 bstp_topology_change_acknowledged(struct bridge_softc *sc) 788 { 789 sc->sc_topology_change_detected = 0; 790 bstp_timer_stop(&sc->sc_tcn_timer); 791 } 792 793 static void 794 bstp_acknowledge_topology_change(struct bridge_softc *sc, 795 struct bridge_iflist *bif) 796 { 797 bif->bif_topology_change_acknowledge = 1; 798 bstp_transmit_config(sc, bif); 799 } 800 801 void 802 bstp_input(struct bridge_softc *sc, struct bridge_iflist *bif, struct mbuf *m) 803 { 804 struct ether_header *eh; 805 struct bstp_tbpdu tpdu; 806 struct bstp_cbpdu cpdu; 807 struct bstp_config_unit cu; 808 struct bstp_tcn_unit tu; 809 uint16_t len; 810 811 if ((bif->bif_flags & IFBIF_STP) == 0) 812 goto out; 813 814 /* 815 * The L1BLOCKING (ping pong failover) test needs to reset the 816 * timer if LINK1 is active. 817 */ 818 if (bif->bif_state == BSTP_IFSTATE_L1BLOCKING) { 819 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING); 820 if (sc->sc_ifp->if_flags & IFF_LINK1) 821 bstp_timer_start(&bif->bif_link1_timer, 0); 822 bstp_make_forwarding(sc, bif); 823 } else if (sc->sc_ifp->if_flags & IFF_LINK1) { 824 bstp_timer_start(&bif->bif_link1_timer, 0); 825 } 826 827 eh = mtod(m, struct ether_header *); 828 829 len = ntohs(eh->ether_type); 830 if (len < sizeof(tpdu)) 831 goto out; 832 833 m_adj(m, ETHER_HDR_LEN); 834 835 if (m->m_pkthdr.len > len) 836 m_adj(m, len - m->m_pkthdr.len); 837 if (m->m_len < sizeof(tpdu) && 838 (m = m_pullup(m, sizeof(tpdu))) == NULL) 839 goto out; 840 841 memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu)); 842 843 if (tpdu.tbu_dsap != LLC_8021D_LSAP || 844 tpdu.tbu_ssap != LLC_8021D_LSAP || 845 tpdu.tbu_ctl != LLC_UI) 846 goto out; 847 if (tpdu.tbu_protoid != 0 || tpdu.tbu_protover != 0) 848 goto out; 849 850 switch (tpdu.tbu_bpdutype) { 851 case BSTP_MSGTYPE_TCN: 852 tu.tu_message_type = tpdu.tbu_bpdutype; 853 bstp_received_tcn_bpdu(sc, bif, &tu); 854 break; 855 case BSTP_MSGTYPE_CFG: 856 if (m->m_len < sizeof(cpdu) && 857 (m = m_pullup(m, sizeof(cpdu))) == NULL) 858 goto out; 859 memcpy(&cpdu, mtod(m, caddr_t), sizeof(cpdu)); 860 861 cu.cu_rootid = 862 (((uint64_t)ntohs(cpdu.cbu_rootpri)) << 48) | 863 (((uint64_t)cpdu.cbu_rootaddr[0]) << 40) | 864 (((uint64_t)cpdu.cbu_rootaddr[1]) << 32) | 865 (((uint64_t)cpdu.cbu_rootaddr[2]) << 24) | 866 (((uint64_t)cpdu.cbu_rootaddr[3]) << 16) | 867 (((uint64_t)cpdu.cbu_rootaddr[4]) << 8) | 868 (((uint64_t)cpdu.cbu_rootaddr[5]) << 0); 869 870 cu.cu_bridge_id = 871 (((uint64_t)ntohs(cpdu.cbu_bridgepri)) << 48) | 872 (((uint64_t)cpdu.cbu_bridgeaddr[0]) << 40) | 873 (((uint64_t)cpdu.cbu_bridgeaddr[1]) << 32) | 874 (((uint64_t)cpdu.cbu_bridgeaddr[2]) << 24) | 875 (((uint64_t)cpdu.cbu_bridgeaddr[3]) << 16) | 876 (((uint64_t)cpdu.cbu_bridgeaddr[4]) << 8) | 877 (((uint64_t)cpdu.cbu_bridgeaddr[5]) << 0); 878 879 cu.cu_root_path_cost = ntohl(cpdu.cbu_rootpathcost); 880 cu.cu_message_age = ntohs(cpdu.cbu_messageage); 881 cu.cu_max_age = ntohs(cpdu.cbu_maxage); 882 cu.cu_hello_time = ntohs(cpdu.cbu_hellotime); 883 cu.cu_forward_delay = ntohs(cpdu.cbu_forwarddelay); 884 cu.cu_port_id = ntohs(cpdu.cbu_portid); 885 cu.cu_message_type = cpdu.cbu_bpdutype; 886 cu.cu_topology_change_acknowledgment = 887 (cpdu.cbu_flags & BSTP_FLAG_TCA) ? 1 : 0; 888 cu.cu_topology_change = 889 (cpdu.cbu_flags & BSTP_FLAG_TC) ? 1 : 0; 890 bstp_received_config_bpdu(sc, bif, &cu); 891 break; 892 default: 893 goto out; 894 } 895 out: 896 if (m) 897 m_freem(m); 898 } 899 900 static void 901 bstp_received_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif, 902 struct bstp_config_unit *cu) 903 { 904 int iamroot; 905 906 iamroot = bstp_root_bridge(sc); 907 908 if (bif->bif_state != BSTP_IFSTATE_DISABLED) { 909 /* 910 * Record information from peer. The peer_cost field 911 * does not include the local bif->bif_path_cost, it will 912 * be added in as needed (since it can be modified manually 913 * this way we don't have to worry about fixups). 914 */ 915 bif->bif_peer_root = cu->cu_rootid; 916 bif->bif_peer_cost = cu->cu_root_path_cost; 917 bif->bif_peer_bridge = cu->cu_bridge_id; 918 bif->bif_peer_port = cu->cu_port_id; 919 920 if (bstp_supersedes_port_info(sc, bif)) { 921 bstp_record_config_information(sc, bif, cu); 922 bstp_configuration_update(sc); 923 bstp_port_state_selection(sc); 924 925 /* 926 * If our bridge loses its root status (?) 927 * 928 * Hello's (unsolicited CFG packets) are generated 929 * every hello period of LINK1 is set, otherwise 930 * we are no longer the root bridge and must stop 931 * generating unsolicited CFG packets. 932 */ 933 if (iamroot && bstp_root_bridge(sc) == 0) { 934 if ((sc->sc_ifp->if_flags & IFF_LINK1) == 0) 935 bstp_timer_stop(&sc->sc_hello_timer); 936 937 if (sc->sc_topology_change_detected) { 938 bstp_timer_stop( 939 &sc->sc_topology_change_timer); 940 bstp_transmit_tcn(sc); 941 bstp_timer_start(&sc->sc_tcn_timer, 0); 942 } 943 } 944 945 if (sc->sc_root_port && 946 bif->bif_info == sc->sc_root_port->bif_info) { 947 bstp_record_config_timeout_values(sc, cu); 948 bstp_config_bpdu_generation(sc); 949 950 if (cu->cu_topology_change_acknowledgment) 951 bstp_topology_change_acknowledged(sc); 952 } 953 } else if (bif->bif_flags & IFBIF_DESIGNATED) { 954 /* 955 * Update designated ports (aged out peers or 956 * the port closest to the root) at a faster pace. 957 * 958 * Clear our designated flag if we aren't marked 959 * as the root port. 960 */ 961 bstp_transmit_config(sc, bif); 962 if ((bif->bif_flags & IFBIF_ROOT) == 0) { 963 bif->bif_flags &= ~IFBIF_DESIGNATED; 964 bstp_configuration_update(sc); 965 bstp_port_state_selection(sc); 966 } 967 } 968 } 969 } 970 971 static void 972 bstp_received_tcn_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif, 973 struct bstp_tcn_unit *tcn) 974 { 975 if (bif->bif_state != BSTP_IFSTATE_DISABLED && 976 (bif->bif_flags & IFBIF_DESIGNATED)) { 977 bstp_topology_change_detection(sc); 978 bstp_acknowledge_topology_change(sc, bif); 979 } 980 } 981 982 /* 983 * link1 forces continuous hello's (the bridge interface must be cycled 984 * to start them up), so keep the timer hot if that is the case, otherwise 985 * only send HELLO's if we are the root. 986 */ 987 static void 988 bstp_hello_timer_expiry(struct bridge_softc *sc) 989 { 990 bstp_config_bpdu_generation(sc); 991 992 if ((sc->sc_ifp->if_flags & IFF_LINK1) || bstp_root_bridge(sc)) 993 bstp_timer_start(&sc->sc_hello_timer, 0); 994 } 995 996 static void 997 bstp_message_age_timer_expiry(struct bridge_softc *sc, 998 struct bridge_iflist *bif) 999 { 1000 int iamroot; 1001 1002 iamroot = bstp_root_bridge(sc); 1003 bstp_clear_peer_info(sc, bif); 1004 bstp_configuration_update(sc); 1005 bstp_port_state_selection(sc); 1006 1007 /* 1008 * If we've become the root and were not the root before 1009 * we have some cleanup to do. This also occurs if we 1010 * wind up being completely isolated. 1011 */ 1012 if (iamroot == 0 && bstp_root_bridge(sc)) { 1013 sc->sc_max_age = sc->sc_bridge_max_age; 1014 sc->sc_hello_time = sc->sc_bridge_hello_time; 1015 sc->sc_forward_delay = sc->sc_bridge_forward_delay; 1016 1017 bstp_topology_change_detection(sc); 1018 bstp_timer_stop(&sc->sc_tcn_timer); 1019 bstp_config_bpdu_generation(sc); 1020 bstp_timer_start(&sc->sc_hello_timer, 0); 1021 } 1022 } 1023 1024 static void 1025 bstp_forward_delay_timer_expiry(struct bridge_softc *sc, 1026 struct bridge_iflist *bif) 1027 { 1028 if (bif->bif_state == BSTP_IFSTATE_LISTENING) { 1029 bstp_set_port_state(bif, BSTP_IFSTATE_LEARNING); 1030 bstp_timer_start(&bif->bif_forward_delay_timer, 0); 1031 } else if (bif->bif_state == BSTP_IFSTATE_LEARNING) { 1032 bstp_set_port_state(bif, BSTP_IFSTATE_FORWARDING); 1033 if (sc->sc_designated_bridge == sc->sc_bridge_id && 1034 bif->bif_change_detection_enabled) { 1035 bstp_topology_change_detection(sc); 1036 } 1037 if (sc->sc_ifp->if_flags & IFF_LINK2) 1038 bstp_adjust_bonded_states(sc, bif); 1039 } 1040 } 1041 1042 static void 1043 bstp_tcn_timer_expiry(struct bridge_softc *sc) 1044 { 1045 bstp_transmit_tcn(sc); 1046 bstp_timer_start(&sc->sc_tcn_timer, 0); 1047 } 1048 1049 static void 1050 bstp_topology_change_timer_expiry(struct bridge_softc *sc) 1051 { 1052 sc->sc_topology_change_detected = 0; 1053 sc->sc_topology_change = 0; 1054 } 1055 1056 static void 1057 bstp_hold_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif) 1058 { 1059 if (bif->bif_config_pending) 1060 bstp_transmit_config(sc, bif); 1061 } 1062 1063 /* 1064 * If no traffic received directly on this port for the specified 1065 * period with link1 set we go into a special blocking mode to 1066 * fail-over traffic to another port. 1067 */ 1068 static void 1069 bstp_link1_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif) 1070 { 1071 if (sc->sc_ifp->if_flags & IFF_LINK1) 1072 bstp_make_l1blocking(sc, bif); 1073 } 1074 1075 static int 1076 bstp_addr_cmp(const uint8_t *a, const uint8_t *b) 1077 { 1078 int i, d; 1079 1080 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) { 1081 d = ((int)a[i]) - ((int)b[i]); 1082 } 1083 1084 return (d); 1085 } 1086 1087 void 1088 bstp_initialization(struct bridge_softc *sc) 1089 { 1090 struct bridge_iflist *bif, *mif, *nbif; 1091 u_char *e_addr; 1092 1093 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT); 1094 1095 /* 1096 * Figure out our bridge ID, use the lowest-valued MAC. 1097 * Include the bridge's own random MAC in the calculation. 1098 */ 1099 mif = NULL; 1100 1101 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 1102 if ((bif->bif_flags & IFBIF_STP) == 0) 1103 continue; 1104 if (bif->bif_ifp->if_type != IFT_ETHER) 1105 continue; 1106 if (mif == NULL) { 1107 mif = bif; 1108 continue; 1109 } 1110 1111 bif->bif_port_id = (bif->bif_priority << 8) | 1112 (bif->bif_ifp->if_index & 0xff); 1113 if (bstp_addr_cmp(IF_LLADDR(bif->bif_ifp), 1114 IF_LLADDR(mif->bif_ifp)) < 0) { 1115 mif = bif; 1116 continue; 1117 } 1118 } 1119 if (mif == NULL) { 1120 bstp_stop(sc); 1121 return; 1122 } 1123 1124 if (bstp_addr_cmp(IF_LLADDR(sc->sc_ifp), IF_LLADDR(mif->bif_ifp)) < 0) 1125 e_addr = IF_LLADDR(sc->sc_ifp); 1126 else 1127 e_addr = IF_LLADDR(mif->bif_ifp); 1128 1129 sc->sc_bridge_id = 1130 (((uint64_t)sc->sc_bridge_priority) << 48) | 1131 (((uint64_t)e_addr[0]) << 40) | 1132 (((uint64_t)e_addr[1]) << 32) | 1133 (((uint64_t)e_addr[2]) << 24) | 1134 (((uint64_t)e_addr[3]) << 16) | 1135 (((uint64_t)e_addr[4]) << 8) | 1136 (((uint64_t)e_addr[5])); 1137 1138 /* 1139 * Remainder of setup. 1140 */ 1141 1142 sc->sc_designated_root = sc->sc_bridge_id; 1143 sc->sc_designated_cost = 0; 1144 sc->sc_root_port = NULL; 1145 1146 sc->sc_max_age = sc->sc_bridge_max_age; 1147 sc->sc_hello_time = sc->sc_bridge_hello_time; 1148 sc->sc_forward_delay = sc->sc_bridge_forward_delay; 1149 sc->sc_topology_change_detected = 0; 1150 sc->sc_topology_change = 0; 1151 bstp_timer_stop(&sc->sc_tcn_timer); 1152 bstp_timer_stop(&sc->sc_topology_change_timer); 1153 1154 if (callout_pending(&sc->sc_bstpcallout) == 0) 1155 callout_reset(&sc->sc_bstpcallout, hz, 1156 bstp_tick, sc); 1157 1158 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) { 1159 if (sc->sc_ifp->if_flags & IFF_LINK1) 1160 bstp_timer_start(&bif->bif_link1_timer, 0); 1161 if (bif->bif_flags & IFBIF_STP) 1162 bstp_ifupdstatus(sc, bif); 1163 else 1164 bstp_disable_port(sc, bif); 1165 1166 if (nbif != NULL && !nbif->bif_onlist) { 1167 KKASSERT(bif->bif_onlist); 1168 nbif = TAILQ_NEXT(bif, bif_next); 1169 } 1170 } 1171 1172 bstp_port_state_selection(sc); 1173 bstp_config_bpdu_generation(sc); 1174 bstp_timer_start(&sc->sc_hello_timer, 0); 1175 } 1176 1177 void 1178 bstp_stop(struct bridge_softc *sc) 1179 { 1180 struct bridge_iflist *bif; 1181 struct lwkt_msg *lmsg; 1182 1183 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT); 1184 1185 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 1186 bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED); 1187 bstp_timer_stop(&bif->bif_hold_timer); 1188 bstp_timer_stop(&bif->bif_message_age_timer); 1189 bstp_timer_stop(&bif->bif_forward_delay_timer); 1190 bstp_timer_stop(&bif->bif_link1_timer); 1191 } 1192 1193 callout_stop(&sc->sc_bstpcallout); 1194 1195 bstp_timer_stop(&sc->sc_topology_change_timer); 1196 bstp_timer_stop(&sc->sc_tcn_timer); 1197 bstp_timer_stop(&sc->sc_hello_timer); 1198 1199 crit_enter(); 1200 lmsg = &sc->sc_bstptimemsg.lmsg; 1201 if ((lmsg->ms_flags & MSGF_DONE) == 0) { 1202 /* Pending to be processed; drop it */ 1203 lwkt_dropmsg(lmsg); 1204 } 1205 crit_exit(); 1206 } 1207 1208 static void 1209 bstp_initialize_port(struct bridge_softc *sc, struct bridge_iflist *bif) 1210 { 1211 int needs_adjust = (bif->bif_state == BSTP_IFSTATE_FORWARDING || 1212 bif->bif_state == BSTP_IFSTATE_BLOCKING || 1213 bif->bif_state == BSTP_IFSTATE_BONDED); 1214 1215 bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING); 1216 bstp_clear_peer_info(sc, bif); 1217 bif->bif_topology_change_acknowledge = 0; 1218 bif->bif_config_pending = 0; 1219 bif->bif_change_detection_enabled = 1; 1220 bstp_timer_stop(&bif->bif_message_age_timer); 1221 bstp_timer_stop(&bif->bif_forward_delay_timer); 1222 bstp_timer_stop(&bif->bif_hold_timer); 1223 bstp_timer_stop(&bif->bif_link1_timer); 1224 if (needs_adjust && (sc->sc_ifp->if_flags & IFF_LINK2)) 1225 bstp_adjust_bonded_states(sc, bif); 1226 } 1227 1228 static void 1229 bstp_enable_port(struct bridge_softc *sc, struct bridge_iflist *bif) 1230 { 1231 bstp_initialize_port(sc, bif); 1232 if (sc->sc_ifp->if_flags & IFF_LINK1) 1233 bstp_timer_start(&bif->bif_link1_timer, 0); 1234 bstp_port_state_selection(sc); 1235 } 1236 1237 static void 1238 bstp_disable_port(struct bridge_softc *sc, struct bridge_iflist *bif) 1239 { 1240 int was_forwarding = (bif->bif_state == BSTP_IFSTATE_FORWARDING); 1241 int iamroot; 1242 1243 iamroot = bstp_root_bridge(sc); 1244 1245 bstp_clear_peer_info(sc, bif); 1246 bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED); 1247 bif->bif_topology_change_acknowledge = 0; 1248 bif->bif_config_pending = 0; 1249 bstp_timer_stop(&bif->bif_message_age_timer); 1250 bstp_timer_stop(&bif->bif_forward_delay_timer); 1251 bstp_timer_stop(&bif->bif_link1_timer); 1252 bstp_configuration_update(sc); 1253 bstp_port_state_selection(sc); 1254 bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN); 1255 if (was_forwarding && (sc->sc_ifp->if_flags & IFF_LINK2)) 1256 bstp_adjust_bonded_states(sc, bif); 1257 1258 if (iamroot == 0 && bstp_root_bridge(sc)) { 1259 sc->sc_max_age = sc->sc_bridge_max_age; 1260 sc->sc_hello_time = sc->sc_bridge_hello_time; 1261 sc->sc_forward_delay = sc->sc_bridge_forward_delay; 1262 1263 bstp_topology_change_detection(sc); 1264 bstp_timer_stop(&sc->sc_tcn_timer); 1265 bstp_config_bpdu_generation(sc); 1266 bstp_timer_start(&sc->sc_hello_timer, 0); 1267 } 1268 } 1269 1270 void 1271 bstp_linkstate(struct ifnet *ifp, int state) 1272 { 1273 struct bridge_softc *sc; 1274 struct bridge_iflist *bif; 1275 1276 sc = ifp->if_bridge; 1277 ifnet_serialize_all(sc->sc_ifp); 1278 1279 /* 1280 * bstp_ifupdstatus() may block, but it is the last 1281 * operation of the member iface iteration, so we 1282 * don't need to use LIST_FOREACH_MUTABLE()+bif_onlist 1283 * check here. 1284 */ 1285 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 1286 if ((bif->bif_flags & IFBIF_STP) == 0) 1287 continue; 1288 1289 if (bif->bif_ifp == ifp) { 1290 bstp_ifupdstatus(sc, bif); 1291 break; 1292 } 1293 } 1294 ifnet_deserialize_all(sc->sc_ifp); 1295 } 1296 1297 static void 1298 bstp_ifupdstatus(struct bridge_softc *sc, struct bridge_iflist *bif) 1299 { 1300 struct ifnet *ifp = bif->bif_ifp; 1301 struct ifmediareq ifmr; 1302 int error = 0; 1303 1304 bzero((char *)&ifmr, sizeof(ifmr)); 1305 ifnet_serialize_all(ifp); 1306 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL); 1307 ifnet_deserialize_all(ifp); 1308 1309 if ((error == 0) && (ifp->if_flags & IFF_UP)) { 1310 if (ifmr.ifm_status & IFM_ACTIVE) { 1311 if (bif->bif_state == BSTP_IFSTATE_DISABLED) 1312 bstp_enable_port(sc, bif); 1313 1314 } else { 1315 if (bif->bif_state != BSTP_IFSTATE_DISABLED) 1316 bstp_disable_port(sc, bif); 1317 } 1318 return; 1319 } 1320 1321 if (bif->bif_state != BSTP_IFSTATE_DISABLED) 1322 bstp_disable_port(sc, bif); 1323 } 1324 1325 static void 1326 bstp_tick(void *arg) 1327 { 1328 struct bridge_softc *sc = arg; 1329 struct lwkt_msg *lmsg; 1330 1331 KKASSERT(mycpuid == BRIDGE_CFGCPU); 1332 1333 crit_enter(); 1334 1335 if (callout_pending(&sc->sc_bstpcallout) || 1336 !callout_active(&sc->sc_bstpcallout)) { 1337 crit_exit(); 1338 return; 1339 } 1340 callout_deactivate(&sc->sc_bstpcallout); 1341 1342 lmsg = &sc->sc_bstptimemsg.lmsg; 1343 KKASSERT(lmsg->ms_flags & MSGF_DONE); 1344 lwkt_sendmsg(BRIDGE_CFGPORT, lmsg); 1345 1346 crit_exit(); 1347 } 1348 1349 void 1350 bstp_tick_handler(netmsg_t msg) 1351 { 1352 struct bridge_softc *sc = msg->lmsg.u.ms_resultp; 1353 struct bridge_iflist *bif; 1354 1355 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT); 1356 crit_enter(); 1357 /* Reply ASAP */ 1358 lwkt_replymsg(&msg->lmsg, 0); 1359 crit_exit(); 1360 1361 ifnet_serialize_all(sc->sc_ifp); 1362 1363 /* 1364 * NOTE: 1365 * We don't need to worry that member iface is ripped 1366 * from the per-cpu list during the blocking operation 1367 * in the loop body, since deletion is serialized by 1368 * BRIDGE_CFGPORT 1369 */ 1370 1371 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 1372 if ((bif->bif_flags & IFBIF_STP) == 0) 1373 continue; 1374 /* 1375 * XXX This can cause a lag in "link does away" 1376 * XXX and "spanning tree gets updated". We need 1377 * XXX come sort of callback from the link state 1378 * XXX update code to kick spanning tree. 1379 * XXX --thorpej@NetBSD.org 1380 */ 1381 bstp_ifupdstatus(sc, bif); 1382 } 1383 1384 if (bstp_timer_expired(&sc->sc_hello_timer, sc->sc_hello_time)) 1385 bstp_hello_timer_expiry(sc); 1386 1387 if (bstp_timer_expired(&sc->sc_tcn_timer, sc->sc_bridge_hello_time)) 1388 bstp_tcn_timer_expiry(sc); 1389 1390 if (bstp_timer_expired(&sc->sc_topology_change_timer, 1391 sc->sc_topology_change_time)) 1392 bstp_topology_change_timer_expiry(sc); 1393 1394 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 1395 if ((bif->bif_flags & IFBIF_STP) == 0) 1396 continue; 1397 if (bstp_timer_expired(&bif->bif_message_age_timer, 1398 sc->sc_max_age)) 1399 bstp_message_age_timer_expiry(sc, bif); 1400 } 1401 1402 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 1403 if ((bif->bif_flags & IFBIF_STP) == 0) 1404 continue; 1405 if (bstp_timer_expired(&bif->bif_forward_delay_timer, 1406 sc->sc_forward_delay)) 1407 bstp_forward_delay_timer_expiry(sc, bif); 1408 1409 if (bstp_timer_expired(&bif->bif_hold_timer, 1410 sc->sc_hold_time)) 1411 bstp_hold_timer_expiry(sc, bif); 1412 1413 if (bstp_timer_expired(&bif->bif_link1_timer, 1414 sc->sc_hello_time * 10)) 1415 bstp_link1_timer_expiry(sc, bif); 1416 } 1417 1418 if (sc->sc_ifp->if_flags & IFF_RUNNING) 1419 callout_reset(&sc->sc_bstpcallout, hz, bstp_tick, sc); 1420 1421 ifnet_deserialize_all(sc->sc_ifp); 1422 } 1423 1424 static void 1425 bstp_timer_start(struct bridge_timer *t, uint16_t v) 1426 { 1427 t->value = v; 1428 t->active = 1; 1429 } 1430 1431 static void 1432 bstp_timer_stop(struct bridge_timer *t) 1433 { 1434 t->value = 0; 1435 t->active = 0; 1436 } 1437 1438 static int 1439 bstp_timer_expired(struct bridge_timer *t, uint16_t v) 1440 { 1441 if (t->active == 0) 1442 return (0); 1443 t->value += BSTP_TICK_VAL; 1444 if (t->value >= v) { 1445 bstp_timer_stop(t); 1446 return (1); 1447 } 1448 return (0); 1449 1450 } 1451