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