The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/bridgestp.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 2085b1663af3865761456b5d58549b11


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.