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