Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 1 Nov 2006 09:26:07 GMT
From:      John Birrell <jb@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 108903 for review
Message-ID:  <200611010926.kA19Q7CJ074105@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=108903

Change 108903 by jb@jb_freebsd8 on 2006/11/01 09:25:13

	IFC

Affected files ...

.. //depot/projects/dtrace/src/sys/net/bridgestp.c#8 integrate
.. //depot/projects/dtrace/src/sys/net/bridgestp.h#4 integrate
.. //depot/projects/dtrace/src/sys/net/if_bridge.c#15 integrate
.. //depot/projects/dtrace/src/sys/net/if_bridgevar.h#6 integrate
.. //depot/projects/dtrace/www/de/docs/books.sgml#8 integrate
.. //depot/projects/dtrace/www/de/security/security.sgml#8 integrate
.. //depot/projects/dtrace/www/de/share/sgml/news.xml#5 integrate
.. //depot/projects/dtrace/www/de/share/sgml/press.xml#5 integrate

Differences ...

==== //depot/projects/dtrace/src/sys/net/bridgestp.c#8 (text+ko) ====

@@ -2,6 +2,7 @@
 
 /*
  * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
+ * Copyright (c) 2006 Andrew Thompson (thompsa@FreeBSD.org)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -30,12 +31,11 @@
 
 /*
  * Implementation of the spanning tree protocol as defined in
- * ISO/IEC Final DIS 15802-3 (IEEE P802.1D/D17), May 25, 1998.
- * (In English: IEEE 802.1D, Draft 17, 1998)
+ * ISO/IEC 802.1D-2004, June 9, 2004.
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/net/bridgestp.c,v 1.20 2006/10/01 03:48:32 thompsa Exp $");
+__FBSDID("$FreeBSD: src/sys/net/bridgestp.c,v 1.21 2006/11/01 09:07:47 thompsa Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -62,276 +62,172 @@
 #include <netinet/if_ether.h>
 #include <net/bridgestp.h>
 
+#ifdef	BRIDGESTP_DEBUG
+#define	DPRINTF(fmt, arg...)	printf("bstp: " fmt, ##arg)
+#else
+#define	DPRINTF(fmt, arg...)
+#endif
+
+#define	PV2ADDR(pv, eaddr)	do {		\
+	eaddr[0] = pv >> 40;			\
+	eaddr[1] = pv >> 32;			\
+	eaddr[2] = pv >> 24;			\
+	eaddr[3] = pv >> 16;			\
+	eaddr[4] = pv >> 8;			\
+	eaddr[5] = pv >> 0;			\
+} while (0)
+
+#define	INFO_BETTER	1
+#define	INFO_SAME	0
+#define	INFO_WORSE	-1
+
 const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
 
 LIST_HEAD(, bstp_state) bstp_list;
 static struct mtx	bstp_list_mtx;
 
-static void	bstp_initialize_port(struct bstp_state *,
-		    struct bstp_port *);
+static void	bstp_transmit(struct bstp_state *, struct bstp_port *);
+static void	bstp_transmit_bpdu(struct bstp_state *, struct bstp_port *);
+static void	bstp_transmit_tcn(struct bstp_state *, struct bstp_port *);
+static void	bstp_decode_bpdu(struct bstp_port *, struct bstp_cbpdu *,
+		    struct bstp_config_unit *);
+static void	bstp_send_bpdu(struct bstp_state *, struct bstp_port *,
+		    struct bstp_cbpdu *);
+static void	bstp_enqueue(struct ifnet *, struct mbuf *);
+static int	bstp_pdu_flags(struct bstp_port *);
+static void	bstp_received_stp(struct bstp_state *, struct bstp_port *,
+		    struct mbuf *, struct bstp_tbpdu *);
+static void	bstp_received_rstp(struct bstp_state *, struct bstp_port *,
+		    struct mbuf *, struct bstp_tbpdu *);
+static void	bstp_received_tcn(struct bstp_state *, struct bstp_port *,
+		    struct bstp_tcn_unit *);
+static void	bstp_received_bpdu(struct bstp_state *, struct bstp_port *,
+		    struct bstp_config_unit *);
+static int	bstp_pdu_rcvtype(struct bstp_port *, struct bstp_config_unit *);
+static int	bstp_pdu_bettersame(struct bstp_port *, int);
+static int	bstp_info_cmp(struct bstp_pri_vector *,
+		    struct bstp_pri_vector *);
+static int	bstp_info_superior(struct bstp_pri_vector *,
+		    struct bstp_pri_vector *);
+static void	bstp_assign_roles(struct bstp_state *);
+static void	bstp_update_roles(struct bstp_state *, struct bstp_port *);
+static void	bstp_update_state(struct bstp_state *, struct bstp_port *);
+static void	bstp_update_tc(struct bstp_port *);
+static void	bstp_update_info(struct bstp_port *);
+static void	bstp_set_other_tcprop(struct bstp_port *);
+static void	bstp_set_all_reroot(struct bstp_state *);
+static void	bstp_set_all_sync(struct bstp_state *);
+static void	bstp_set_port_state(struct bstp_port *, int);
+static void	bstp_set_port_role(struct bstp_port *, int);
+static void	bstp_set_port_proto(struct bstp_port *, int);
+static void	bstp_set_port_tc(struct bstp_port *, int);
+static void	bstp_set_timer_tc(struct bstp_port *);
+static void	bstp_set_timer_msgage(struct bstp_port *);
+static int	bstp_rerooted(struct bstp_state *, struct bstp_port *);
+static uint32_t	bstp_calc_path_cost(struct bstp_port *);
+static void	bstp_notify_state(void *, int);
+static void	bstp_notify_rtage(void *, int);
 static void	bstp_ifupdstatus(struct bstp_state *, struct bstp_port *);
 static void	bstp_enable_port(struct bstp_state *, struct bstp_port *);
-static void	bstp_disable_port(struct bstp_state *,
-		    struct bstp_port *);
-#ifdef notused
-static void	bstp_enable_change_detection(struct bstp_port *);
-static void	bstp_disable_change_detection(struct bstp_port *);
-#endif /* notused */
-static int	bstp_root_bridge(struct bstp_state *bs);
-static int	bstp_supersedes_port_info(struct bstp_state *,
-		    struct bstp_port *, struct bstp_config_unit *);
-static int	bstp_designated_port(struct bstp_state *,
-		    struct bstp_port *);
-static int	bstp_designated_for_some_port(struct bstp_state *);
-static void	bstp_transmit_config(struct bstp_state *,
-		    struct bstp_port *);
-static void	bstp_transmit_tcn(struct bstp_state *);
-static void	bstp_received_config_bpdu(struct bstp_state *,
-		    struct bstp_port *, struct bstp_config_unit *);
-static void	bstp_received_tcn_bpdu(struct bstp_state *,
-		    struct bstp_port *, struct bstp_tcn_unit *);
-static void	bstp_record_config_information(struct bstp_state *,
-		    struct bstp_port *, struct bstp_config_unit *);
-static void	bstp_record_config_timeout_values(struct bstp_state *,
-		    struct bstp_config_unit *);
-static void	bstp_config_bpdu_generation(struct bstp_state *);
-static void	bstp_send_config_bpdu(struct bstp_state *,
-		    struct bstp_port *, struct bstp_config_unit *);
-static void	bstp_configuration_update(struct bstp_state *);
-static void	bstp_root_selection(struct bstp_state *);
-static void	bstp_designated_port_selection(struct bstp_state *);
-static void	bstp_become_designated_port(struct bstp_state *,
-		    struct bstp_port *);
-static void	bstp_port_state_selection(struct bstp_state *);
-static void	bstp_make_forwarding(struct bstp_state *,
-		    struct bstp_port *);
-static void	bstp_make_blocking(struct bstp_state *,
-		    struct bstp_port *);
-static void	bstp_set_port_state(struct bstp_port *, uint8_t);
-static void	bstp_state_change(void *, int);
-static void	bstp_update_forward_transitions(struct bstp_port *);
-#ifdef notused
-static void	bstp_set_bridge_priority(struct bstp_state *, uint64_t);
-static void	bstp_set_port_priority(struct bstp_state *,
-		    struct bstp_port *, uint16_t);
-static void	bstp_set_path_cost(struct bstp_state *,
-		    struct bstp_port *, uint32_t);
-#endif /* notused */
-static void	bstp_topology_change_detection(struct bstp_state *);
-static void	bstp_topology_change_acknowledged(struct bstp_state *);
-static void	bstp_acknowledge_topology_change(struct bstp_state *,
-		    struct bstp_port *);
-
-static void	bstp_enqueue(struct ifnet *, struct mbuf *);
+static void	bstp_disable_port(struct bstp_state *, struct bstp_port *);
 static void	bstp_tick(void *);
 static void	bstp_timer_start(struct bstp_timer *, uint16_t);
 static void	bstp_timer_stop(struct bstp_timer *);
-static int	bstp_timer_expired(struct bstp_timer *, uint16_t);
-
-static void	bstp_hold_timer_expiry(struct bstp_state *,
+static void	bstp_timer_latch(struct bstp_timer *);
+static int	bstp_timer_expired(struct bstp_timer *);
+static void	bstp_hello_timer_expiry(struct bstp_state *,
+		    struct bstp_port *);
+static void	bstp_message_age_expiry(struct bstp_state *,
 		    struct bstp_port *);
-static void	bstp_message_age_timer_expiry(struct bstp_state *,
+static void	bstp_migrate_delay_expiry(struct bstp_state *,
 		    struct bstp_port *);
-static void	bstp_forward_delay_timer_expiry(struct bstp_state *,
+static void	bstp_edge_delay_expiry(struct bstp_state *,
 		    struct bstp_port *);
-static void	bstp_topology_change_timer_expiry(struct bstp_state *);
-static void	bstp_tcn_timer_expiry(struct bstp_state *);
-static void	bstp_hello_timer_expiry(struct bstp_state *);
 static int	bstp_addr_cmp(const uint8_t *, const uint8_t *);
+static int	bstp_same_bridgeid(uint64_t, uint64_t);
+static void	bstp_reinit(struct bstp_state *);
+static void	bstp_stop_locked(struct bstp_state *);
 
 static void
-bstp_transmit_config(struct bstp_state *bs, struct bstp_port *bp)
+bstp_transmit(struct bstp_state *bs, struct bstp_port *bp)
 {
-	BSTP_LOCK_ASSERT(bs);
-
-	if (bp->bp_hold_timer.active) {
-		bp->bp_config_pending = 1;
+	/*
+	 * a PDU can only be sent if we have tx quota left and the
+	 * hello timer is running.
+	 */
+	if (bp->bp_hello_timer.active == 0) {
+		/* Test if it needs to be reset */
+		bstp_hello_timer_expiry(bs, bp);
 		return;
 	}
+	if (bp->bp_txcount > bs->bs_txholdcount)
+		/* Ran out of karma */
+		return;
 
-	bp->bp_config_bpdu.cu_message_type = BSTP_MSGTYPE_CFG;
-	bp->bp_config_bpdu.cu_rootid = bs->bs_designated_root;
-	bp->bp_config_bpdu.cu_root_path_cost = bs->bs_root_path_cost;
-	bp->bp_config_bpdu.cu_bridge_id = bs->bs_bridge_id;
-	bp->bp_config_bpdu.cu_port_id = bp->bp_port_id;
+	if (bp->bp_protover == BSTP_PROTO_RSTP) {
+		bstp_transmit_bpdu(bs, bp);
+		bp->bp_tc_ack = 0;
+	} else { /* STP */
+		switch (bp->bp_role) {
+			case BSTP_ROLE_DESIGNATED:
+				bstp_transmit_bpdu(bs, bp);
+				bp->bp_tc_ack = 0;
+				break;
 
-	if (bstp_root_bridge(bs))
-		bp->bp_config_bpdu.cu_message_age = 0;
-	else
-		bp->bp_config_bpdu.cu_message_age =
-		    bs->bs_root_port->bp_message_age_timer.value +
-		    BSTP_MESSAGE_AGE_INCR;
-
-	bp->bp_config_bpdu.cu_max_age = bs->bs_max_age;
-	bp->bp_config_bpdu.cu_hello_time = bs->bs_hello_time;
-	bp->bp_config_bpdu.cu_forward_delay = bs->bs_forward_delay;
-	bp->bp_config_bpdu.cu_topology_change_acknowledgment
-	    = bp->bp_topology_change_acknowledge;
-	bp->bp_config_bpdu.cu_topology_change = bs->bs_topology_change;
-
-	if (bp->bp_config_bpdu.cu_message_age < bs->bs_max_age) {
-		bp->bp_topology_change_acknowledge = 0;
-		bp->bp_config_pending = 0;
-		bstp_send_config_bpdu(bs, bp, &bp->bp_config_bpdu);
-		bstp_timer_start(&bp->bp_hold_timer, 0);
+			case BSTP_ROLE_ROOT:
+				bstp_transmit_tcn(bs, bp);
+				break;
+		}
 	}
+	bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime);
+	bp->bp_flags &= ~BSTP_PORT_NEWINFO;
 }
 
 static void
-bstp_send_config_bpdu(struct bstp_state *bs, struct bstp_port *bp,
-    struct bstp_config_unit *cu)
+bstp_transmit_bpdu(struct bstp_state *bs, struct bstp_port *bp)
 {
-	struct ifnet *ifp;
-	struct mbuf *m;
-	struct ether_header *eh;
 	struct bstp_cbpdu bpdu;
 
 	BSTP_LOCK_ASSERT(bs);
 
-	ifp = bp->bp_ifp;
+	bpdu.cbu_rootpri = htons(bp->bp_desg_pv.pv_root_id >> 48);
+	PV2ADDR(bp->bp_desg_pv.pv_root_id, bpdu.cbu_rootaddr);
 
-	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
-		return;
+	bpdu.cbu_rootpathcost = htonl(bp->bp_desg_pv.pv_cost);
 
-	MGETHDR(m, M_DONTWAIT, MT_DATA);
-	if (m == NULL)
-		return;
+	bpdu.cbu_bridgepri = htons(bp->bp_desg_pv.pv_dbridge_id >> 48);
+	PV2ADDR(bp->bp_desg_pv.pv_dbridge_id, bpdu.cbu_bridgeaddr);
 
-	eh = mtod(m, struct ether_header *);
+	bpdu.cbu_portid = htons(bp->bp_port_id);
+	bpdu.cbu_messageage = htons(bp->bp_desg_msg_age);
+	bpdu.cbu_maxage = htons(bp->bp_desg_max_age);
+	bpdu.cbu_hellotime = htons(bp->bp_desg_htime);
+	bpdu.cbu_forwarddelay = htons(bp->bp_desg_fdelay);
 
-	m->m_pkthdr.rcvif = ifp;
-	m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu);
-	m->m_len = m->m_pkthdr.len;
+	bpdu.cbu_flags = bstp_pdu_flags(bp);
 
-	bpdu.cbu_ssap = bpdu.cbu_dsap = LLC_8021D_LSAP;
-	bpdu.cbu_ctl = LLC_UI;
-	bpdu.cbu_protoid = htons(0);
-	bpdu.cbu_protover = 0;
-	bpdu.cbu_bpdutype = cu->cu_message_type;
-	bpdu.cbu_flags = (cu->cu_topology_change ? BSTP_FLAG_TC : 0) |
-	    (cu->cu_topology_change_acknowledgment ? BSTP_FLAG_TCA : 0);
+	switch (bp->bp_protover) {
+		case BSTP_PROTO_STP:
+			bpdu.cbu_bpdutype = BSTP_MSGTYPE_CFG;
+			break;
 
-	bpdu.cbu_rootpri = htons(cu->cu_rootid >> 48);
-	bpdu.cbu_rootaddr[0] = cu->cu_rootid >> 40;
-	bpdu.cbu_rootaddr[1] = cu->cu_rootid >> 32;
-	bpdu.cbu_rootaddr[2] = cu->cu_rootid >> 24;
-	bpdu.cbu_rootaddr[3] = cu->cu_rootid >> 16;
-	bpdu.cbu_rootaddr[4] = cu->cu_rootid >> 8;
-	bpdu.cbu_rootaddr[5] = cu->cu_rootid >> 0;
-
-	bpdu.cbu_rootpathcost = htonl(cu->cu_root_path_cost);
-
-	bpdu.cbu_bridgepri = htons(cu->cu_bridge_id >> 48);
-	bpdu.cbu_bridgeaddr[0] = cu->cu_bridge_id >> 40;
-	bpdu.cbu_bridgeaddr[1] = cu->cu_bridge_id >> 32;
-	bpdu.cbu_bridgeaddr[2] = cu->cu_bridge_id >> 24;
-	bpdu.cbu_bridgeaddr[3] = cu->cu_bridge_id >> 16;
-	bpdu.cbu_bridgeaddr[4] = cu->cu_bridge_id >> 8;
-	bpdu.cbu_bridgeaddr[5] = cu->cu_bridge_id >> 0;
-
-	bpdu.cbu_portid = htons(cu->cu_port_id);
-	bpdu.cbu_messageage = htons(cu->cu_message_age);
-	bpdu.cbu_maxage = htons(cu->cu_max_age);
-	bpdu.cbu_hellotime = htons(cu->cu_hello_time);
-	bpdu.cbu_forwarddelay = htons(cu->cu_forward_delay);
-
-	memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
-	memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
-	eh->ether_type = htons(sizeof(bpdu));
-
-	memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
-
-	bstp_enqueue(ifp, m);
-}
-
-static int
-bstp_root_bridge(struct bstp_state *bs)
-{
-	return (bs->bs_designated_root == bs->bs_bridge_id);
-}
-
-static int
-bstp_supersedes_port_info(struct bstp_state *bs, struct bstp_port *bp,
-    struct bstp_config_unit *cu)
-{
-	if (cu->cu_rootid < bp->bp_designated_root)
-		return (1);
-	if (cu->cu_rootid > bp->bp_designated_root)
-		return (0);
-
-	if (cu->cu_root_path_cost < bp->bp_designated_cost)
-		return (1);
-	if (cu->cu_root_path_cost > bp->bp_designated_cost)
-		return (0);
-
-	if (cu->cu_bridge_id < bp->bp_designated_bridge)
-		return (1);
-	if (cu->cu_bridge_id > bp->bp_designated_bridge)
-		return (0);
-
-	if (bs->bs_bridge_id != cu->cu_bridge_id)
-		return (1);
-	if (cu->cu_port_id <= bp->bp_designated_port)
-		return (1);
-	return (0);
-}
-
-static void
-bstp_record_config_information(struct bstp_state *bs,
-    struct bstp_port *bp, struct bstp_config_unit *cu)
-{
-	BSTP_LOCK_ASSERT(bs);
-
-	bp->bp_designated_root = cu->cu_rootid;
-	bp->bp_designated_cost = cu->cu_root_path_cost;
-	bp->bp_designated_bridge = cu->cu_bridge_id;
-	bp->bp_designated_port = cu->cu_port_id;
-	bstp_timer_start(&bp->bp_message_age_timer, cu->cu_message_age);
-}
-
-static void
-bstp_record_config_timeout_values(struct bstp_state *bs,
-    struct bstp_config_unit *config)
-{
-	BSTP_LOCK_ASSERT(bs);
-
-	bs->bs_max_age = config->cu_max_age;
-	bs->bs_hello_time = config->cu_hello_time;
-	bs->bs_forward_delay = config->cu_forward_delay;
-	bs->bs_topology_change = config->cu_topology_change;
-}
-
-static void
-bstp_config_bpdu_generation(struct bstp_state *bs)
-{
-	struct bstp_port *bp;
-
-	BSTP_LOCK_ASSERT(bs);
-
-	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
-		if (bstp_designated_port(bs, bp) &&
-		    (bp->bp_state != BSTP_IFSTATE_DISABLED))
-			bstp_transmit_config(bs, bp);
+		case BSTP_PROTO_RSTP:
+			bpdu.cbu_bpdutype = BSTP_MSGTYPE_RSTP;
+			break;
 	}
-}
 
-static int
-bstp_designated_port(struct bstp_state *bs, struct bstp_port *bp)
-{
-	return ((bp->bp_designated_bridge == bs->bs_bridge_id)
-	    && (bp->bp_designated_port == bp->bp_port_id));
+	bstp_send_bpdu(bs, bp, &bpdu);
 }
 
 static void
-bstp_transmit_tcn(struct bstp_state *bs)
+bstp_transmit_tcn(struct bstp_state *bs, struct bstp_port *bp)
 {
 	struct bstp_tbpdu bpdu;
-	struct bstp_port *bp = bs->bs_root_port;
 	struct ifnet *ifp = bp->bp_ifp;
 	struct ether_header *eh;
 	struct mbuf *m;
 
-	BSTP_LOCK_ASSERT(bs);
+	KASSERT(bp == bs->bs_root_port, ("%s: bad root port\n", __func__));
 
 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
 		return;
@@ -358,234 +254,205 @@
 
 	memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu));
 
+	bp->bp_txcount++;
 	bstp_enqueue(ifp, m);
 }
 
 static void
-bstp_configuration_update(struct bstp_state *bs)
+bstp_decode_bpdu(struct bstp_port *bp, struct bstp_cbpdu *cpdu,
+    struct bstp_config_unit *cu)
 {
-	BSTP_LOCK_ASSERT(bs);
+	int flags;
 
-	bstp_root_selection(bs);
-	bstp_designated_port_selection(bs);
-}
+	cu->cu_pv.pv_root_id =
+	    (((uint64_t)ntohs(cpdu->cbu_rootpri)) << 48) |
+	    (((uint64_t)cpdu->cbu_rootaddr[0]) << 40) |
+	    (((uint64_t)cpdu->cbu_rootaddr[1]) << 32) |
+	    (((uint64_t)cpdu->cbu_rootaddr[2]) << 24) |
+	    (((uint64_t)cpdu->cbu_rootaddr[3]) << 16) |
+	    (((uint64_t)cpdu->cbu_rootaddr[4]) << 8) |
+	    (((uint64_t)cpdu->cbu_rootaddr[5]) << 0);
 
-static void
-bstp_root_selection(struct bstp_state *bs)
-{
-	struct bstp_port *root_port = NULL, *bp;
+	cu->cu_pv.pv_dbridge_id =
+	    (((uint64_t)ntohs(cpdu->cbu_bridgepri)) << 48) |
+	    (((uint64_t)cpdu->cbu_bridgeaddr[0]) << 40) |
+	    (((uint64_t)cpdu->cbu_bridgeaddr[1]) << 32) |
+	    (((uint64_t)cpdu->cbu_bridgeaddr[2]) << 24) |
+	    (((uint64_t)cpdu->cbu_bridgeaddr[3]) << 16) |
+	    (((uint64_t)cpdu->cbu_bridgeaddr[4]) << 8) |
+	    (((uint64_t)cpdu->cbu_bridgeaddr[5]) << 0);
 
-	BSTP_LOCK_ASSERT(bs);
+	cu->cu_pv.pv_cost = ntohl(cpdu->cbu_rootpathcost);
+	cu->cu_message_age = ntohs(cpdu->cbu_messageage);
+	cu->cu_max_age = ntohs(cpdu->cbu_maxage);
+	cu->cu_hello_time = ntohs(cpdu->cbu_hellotime);
+	cu->cu_forward_delay = ntohs(cpdu->cbu_forwarddelay);
+	cu->cu_pv.pv_dport_id = ntohs(cpdu->cbu_portid);
+	cu->cu_pv.pv_port_id = bp->bp_port_id;
+	cu->cu_message_type = cpdu->cbu_bpdutype;
 
-	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
-		if (bstp_designated_port(bs, bp))
-			continue;
-		if (bp->bp_state == BSTP_IFSTATE_DISABLED)
-			continue;
-		if (bp->bp_designated_root >= bs->bs_bridge_id)
-			continue;
-		if (root_port == NULL)
-			goto set_port;
+	/* Strip off unused flags in STP mode */
+	flags = cpdu->cbu_flags;
+	switch (cpdu->cbu_protover) {
+		case BSTP_PROTO_STP:
+			flags &= BSTP_PDU_STPMASK;
+			/* A STP BPDU explicitly conveys a Designated Port */
+			cu->cu_role = BSTP_ROLE_DESIGNATED;
+			break;
 
-		if (bp->bp_designated_root < root_port->bp_designated_root)
-			goto set_port;
-		if (bp->bp_designated_root > root_port->bp_designated_root)
-			continue;
+		case BSTP_PROTO_RSTP:
+			flags &= BSTP_PDU_RSTPMASK;
+			break;
+	}
 
-		if ((bp->bp_designated_cost + bp->bp_path_cost) <
-		    (root_port->bp_designated_cost + root_port->bp_path_cost))
-			goto set_port;
-		if ((bp->bp_designated_cost + bp->bp_path_cost) >
-		    (root_port->bp_designated_cost + root_port->bp_path_cost))
-			continue;
+	cu->cu_topology_change_ack =
+		(flags & BSTP_PDU_F_TCA) ? 1 : 0;
+	cu->cu_proposal =
+		(flags & BSTP_PDU_F_P) ? 1 : 0;
+	cu->cu_agree =
+		(flags & BSTP_PDU_F_A) ? 1 : 0;
+	cu->cu_learning =
+		(flags & BSTP_PDU_F_L) ? 1 : 0;
+	cu->cu_forwarding =
+		(flags & BSTP_PDU_F_F) ? 1 : 0;
+	cu->cu_topology_change =
+		(flags & BSTP_PDU_F_TC) ? 1 : 0;
 
-		if (bp->bp_designated_bridge <
-		    root_port->bp_designated_bridge)
-			goto set_port;
-		if (bp->bp_designated_bridge >
-		    root_port->bp_designated_bridge)
-			continue;
-
-		if (bp->bp_designated_port < root_port->bp_designated_port)
-			goto set_port;
-		if (bp->bp_designated_port > root_port->bp_designated_port)
-			continue;
-
-		if (bp->bp_port_id >= root_port->bp_port_id)
-			continue;
-set_port:
-		root_port = bp;
+	switch ((flags & BSTP_PDU_PRMASK) >> BSTP_PDU_PRSHIFT) {
+		case BSTP_PDU_F_ROOT:
+			cu->cu_role = BSTP_ROLE_ROOT;
+			break;
+		case BSTP_PDU_F_ALT:
+			cu->cu_role = BSTP_ROLE_ALTERNATE;
+			break;
+		case BSTP_PDU_F_DESG:
+			cu->cu_role = BSTP_ROLE_DESIGNATED;
+			break;
 	}
-
-	bs->bs_root_port = root_port;
-	if (root_port == NULL) {
-		bs->bs_designated_root = bs->bs_bridge_id;
-		bs->bs_root_path_cost = 0;
-	} else {
-		bs->bs_designated_root = root_port->bp_designated_root;
-		bs->bs_root_path_cost = root_port->bp_designated_cost +
-		    root_port->bp_path_cost;
-	}
 }
 
 static void
-bstp_designated_port_selection(struct bstp_state *bs)
+bstp_send_bpdu(struct bstp_state *bs, struct bstp_port *bp,
+    struct bstp_cbpdu *bpdu)
 {
-	struct bstp_port *bp;
+	struct ifnet *ifp;
+	struct mbuf *m;
+	struct ether_header *eh;
 
 	BSTP_LOCK_ASSERT(bs);
 
-	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
-		if (bstp_designated_port(bs, bp))
-			goto designated;
-		if (bp->bp_designated_root != bs->bs_designated_root)
-			goto designated;
+	ifp = bp->bp_ifp;
 
-		if (bs->bs_root_path_cost < bp->bp_designated_cost)
-			goto designated;
-		if (bs->bs_root_path_cost > bp->bp_designated_cost)
-			continue;
+	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
+		return;
 
-		if (bs->bs_bridge_id < bp->bp_designated_bridge)
-			goto designated;
-		if (bs->bs_bridge_id > bp->bp_designated_bridge)
-			continue;
+	MGETHDR(m, M_DONTWAIT, MT_DATA);
+	if (m == NULL)
+		return;
 
-		if (bp->bp_port_id > bp->bp_designated_port)
-			continue;
-designated:
-		bstp_become_designated_port(bs, bp);
-	}
-}
+	eh = mtod(m, struct ether_header *);
 
-static void
-bstp_become_designated_port(struct bstp_state *bs, struct bstp_port *bp)
-{
-	BSTP_LOCK_ASSERT(bs);
+	bpdu->cbu_ssap = bpdu->cbu_dsap = LLC_8021D_LSAP;
+	bpdu->cbu_ctl = LLC_UI;
+	bpdu->cbu_protoid = htons(BSTP_PROTO_ID);
 
-	bp->bp_designated_root = bs->bs_designated_root;
-	bp->bp_designated_cost = bs->bs_root_path_cost;
-	bp->bp_designated_bridge = bs->bs_bridge_id;
-	bp->bp_designated_port = bp->bp_port_id;
-}
+	memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
+	memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN);
 
-static void
-bstp_port_state_selection(struct bstp_state *bs)
-{
-	struct bstp_port *bp;
+	switch (bpdu->cbu_bpdutype) {
+		case BSTP_MSGTYPE_CFG:
+			bpdu->cbu_protover = BSTP_PROTO_STP;
+			m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_STP_LEN;
+			eh->ether_type = htons(BSTP_BPDU_STP_LEN);
+			memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu,
+			    BSTP_BPDU_STP_LEN);
+			break;
 
-	BSTP_LOCK_ASSERT(bs);
+		case BSTP_MSGTYPE_RSTP:
+			bpdu->cbu_protover = BSTP_PROTO_RSTP;
+			bpdu->cbu_versionlen = htons(0);
+			m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_RSTP_LEN;
+			eh->ether_type = htons(BSTP_BPDU_RSTP_LEN);
+			memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu,
+			    BSTP_BPDU_RSTP_LEN);
+			break;
 
-	LIST_FOREACH(bp, &bs->bs_bplist, bp_next) {
-		if (bp == bs->bs_root_port) {
-			bp->bp_config_pending = 0;
-			bp->bp_topology_change_acknowledge = 0;
-			bstp_make_forwarding(bs, bp);
-		} else if (bstp_designated_port(bs, bp)) {
-			bstp_timer_stop(&bp->bp_message_age_timer);
-			bstp_make_forwarding(bs, bp);
-		} else {
-			bp->bp_config_pending = 0;
-			bp->bp_topology_change_acknowledge = 0;
-			bstp_make_blocking(bs, bp);
-		}
+		default:
+			panic("not implemented");
 	}
+	m->m_pkthdr.rcvif = ifp;
+	m->m_len = m->m_pkthdr.len;
+
+	bp->bp_txcount++;
+	bstp_enqueue(ifp, m);
 }
 
 static void
-bstp_make_forwarding(struct bstp_state *bs, struct bstp_port *bp)
+bstp_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
 {
-	BSTP_LOCK_ASSERT(bs);
+	int err = 0;
 
-	if (bp->bp_state == BSTP_IFSTATE_BLOCKING) {
-		bstp_set_port_state(bp, BSTP_IFSTATE_LISTENING);
-		bstp_timer_start(&bp->bp_forward_delay_timer, 0);
-	}
-}
+	IFQ_ENQUEUE(&dst_ifp->if_snd, m, err);
 
-static void
-bstp_make_blocking(struct bstp_state *bs, struct bstp_port *bp)
-{
-	BSTP_LOCK_ASSERT(bs);
-
-	if ((bp->bp_state != BSTP_IFSTATE_DISABLED) &&
-	    (bp->bp_state != BSTP_IFSTATE_BLOCKING)) {
-		if ((bp->bp_state == BSTP_IFSTATE_FORWARDING) ||
-		    (bp->bp_state == BSTP_IFSTATE_LEARNING)) {
-			if (bp->bp_change_detection_enabled) {
-				bstp_topology_change_detection(bs);
-			}
-		}
-		bstp_set_port_state(bp, BSTP_IFSTATE_BLOCKING);
-		bstp_timer_stop(&bp->bp_forward_delay_timer);
-	}
+	if ((dst_ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
+		(*dst_ifp->if_start)(dst_ifp);
 }
 
-static void
-bstp_set_port_state(struct bstp_port *bp, uint8_t state)
+static int
+bstp_pdu_flags(struct bstp_port *bp)
 {
-	struct bstp_state *bs = bp->bp_bs;
+	int flags = 0;
 
-	bp->bp_state = state;
+	if (bp->bp_proposing && bp->bp_state != BSTP_IFSTATE_FORWARDING)
+		flags |= BSTP_PDU_F_P;
 
-	/* notify the parent bridge */
-	if (bs->bs_state_cb != NULL)
-		taskqueue_enqueue(taskqueue_swi, &bp->bp_statetask);
-}
+	if (bp->bp_agree)
+		flags |= BSTP_PDU_F_A;
 
-/*
- * Notify the bridge that a port state has changed, we need to do this from a
- * taskqueue to avoid a LOR.
- */
-static void
-bstp_state_change(void *arg, int pending)
-{
-	struct bstp_port *bp = (struct bstp_port *)arg;
-	struct bstp_state *bs = bp->bp_bs;
+	if (bp->bp_tc_timer.active)
+		flags |= BSTP_PDU_F_TC;
 
-	if (bp->bp_active == 1)
-		(*bs->bs_state_cb)(bp->bp_ifp, bp->bp_state);
-}
+	if (bp->bp_tc_ack)
+		flags |= BSTP_PDU_F_TCA;
 
-static void
-bstp_update_forward_transitions(struct bstp_port *bp)
-{
-	bp->bp_forward_transitions++;
-}
+	switch (bp->bp_state) {
+		case BSTP_IFSTATE_LEARNING:
+			flags |= BSTP_PDU_F_L;
+			break;
 
-static void
-bstp_topology_change_detection(struct bstp_state *bs)
-{
-	BSTP_LOCK_ASSERT(bs);
-
-	if (bstp_root_bridge(bs)) {
-		bs->bs_topology_change = 1;
-		bstp_timer_start(&bs->bs_topology_change_timer, 0);
-	} else if (!bs->bs_topology_change_detected) {
-		bstp_transmit_tcn(bs);
-		bstp_timer_start(&bs->bs_tcn_timer, 0);
+		case BSTP_IFSTATE_FORWARDING:
+			flags |= (BSTP_PDU_F_L | BSTP_PDU_F_F);
+			break;
 	}
-	bs->bs_topology_change_detected = 1;
-	getmicrotime(&bs->bs_last_tc_time);
-}
 
-static void
-bstp_topology_change_acknowledged(struct bstp_state *bs)
-{
-	BSTP_LOCK_ASSERT(bs);
+	switch (bp->bp_role) {
+		case BSTP_ROLE_ROOT:
+			flags |=
+				(BSTP_PDU_F_ROOT << BSTP_PDU_PRSHIFT);
+			break;
 
-	bs->bs_topology_change_detected = 0;
-	bstp_timer_stop(&bs->bs_tcn_timer);
-}
+		case BSTP_ROLE_ALTERNATE:
+		case BSTP_ROLE_BACKUP:	/* fall through */
+			flags |=
+				(BSTP_PDU_F_ALT << BSTP_PDU_PRSHIFT);
+			break;
 
-static void
-bstp_acknowledge_topology_change(struct bstp_state *bs,
-    struct bstp_port *bp)
-{
-	BSTP_LOCK_ASSERT(bs);
+		case BSTP_ROLE_DESIGNATED:
+			flags |=
+				(BSTP_PDU_F_DESG << BSTP_PDU_PRSHIFT);
+			break;
+	}
 
-	bp->bp_topology_change_acknowledge = 1;
-	bstp_transmit_config(bs, bp);
+	/* Strip off unused flags in either mode */
+	switch (bp->bp_protover) {
+		case BSTP_PROTO_STP:
+			flags &= BSTP_PDU_STPMASK;
+			break;
+		case BSTP_PROTO_RSTP:
+			flags &= BSTP_PDU_RSTPMASK;
+			break;
+	}
+	return (flags);
 }
 
 struct mbuf *
@@ -594,9 +461,6 @@
 	struct bstp_state *bs = bp->bp_bs;
 	struct ether_header *eh;
 	struct bstp_tbpdu tpdu;
-	struct bstp_cbpdu cpdu;
-	struct bstp_config_unit cu;
-	struct bstp_tcn_unit tu;
 	uint16_t len;
 
 	if (bp->bp_active == 0) {
@@ -622,59 +486,46 @@
 
 	memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu));
 
+	/* basic packet checks */
 	if (tpdu.tbu_dsap != LLC_8021D_LSAP ||
 	    tpdu.tbu_ssap != LLC_8021D_LSAP ||
 	    tpdu.tbu_ctl != LLC_UI)
 		goto out;
-	if (tpdu.tbu_protoid != 0 || tpdu.tbu_protover != 0)
+	if (tpdu.tbu_protoid != BSTP_PROTO_ID)
 		goto out;
 
-	switch (tpdu.tbu_bpdutype) {
-	case BSTP_MSGTYPE_TCN:
-		tu.tu_message_type = tpdu.tbu_bpdutype;
-		bstp_received_tcn_bpdu(bs, bp, &tu);
-		break;
-	case BSTP_MSGTYPE_CFG:
-		if (m->m_len < sizeof(cpdu) &&
-		    (m = m_pullup(m, sizeof(cpdu))) == NULL)
+	/*
+	 * We can treat later versions of the PDU as the same as the maximum
+	 * version we implement. All additional parameters/flags are ignored.
+	 */
+	if (tpdu.tbu_protover > BSTP_PROTO_MAX)
+		tpdu.tbu_protover = BSTP_PROTO_MAX;
+
+	if (tpdu.tbu_protover != bp->bp_protover) {
+		/*
+		 * Wait for the migration delay timer to expire before changing
+		 * protocol version to avoid flip-flops.
+		 */
+		if (bp->bp_flags & BSTP_PORT_CANMIGRATE)
+			bstp_set_port_proto(bp, tpdu.tbu_protover);
+		else
 			goto out;
-		memcpy(&cpdu, mtod(m, caddr_t), sizeof(cpdu));
+	}
 
-		cu.cu_rootid =
-		    (((uint64_t)ntohs(cpdu.cbu_rootpri)) << 48) |
-		    (((uint64_t)cpdu.cbu_rootaddr[0]) << 40) |
-		    (((uint64_t)cpdu.cbu_rootaddr[1]) << 32) |
-		    (((uint64_t)cpdu.cbu_rootaddr[2]) << 24) |
-		    (((uint64_t)cpdu.cbu_rootaddr[3]) << 16) |
-		    (((uint64_t)cpdu.cbu_rootaddr[4]) << 8) |
-		    (((uint64_t)cpdu.cbu_rootaddr[5]) << 0);
+	/* Clear operedge upon receiving a PDU on the port */
+	bp->bp_operedge = 0;
+	bstp_timer_start(&bp->bp_edge_delay_timer,
+	    BSTP_DEFAULT_MIGRATE_DELAY);
 
-		cu.cu_bridge_id =
-		    (((uint64_t)ntohs(cpdu.cbu_bridgepri)) << 48) |
-		    (((uint64_t)cpdu.cbu_bridgeaddr[0]) << 40) |
-		    (((uint64_t)cpdu.cbu_bridgeaddr[1]) << 32) |
-		    (((uint64_t)cpdu.cbu_bridgeaddr[2]) << 24) |
-		    (((uint64_t)cpdu.cbu_bridgeaddr[3]) << 16) |
-		    (((uint64_t)cpdu.cbu_bridgeaddr[4]) << 8) |
-		    (((uint64_t)cpdu.cbu_bridgeaddr[5]) << 0);
+	switch (tpdu.tbu_protover) {
+		case BSTP_PROTO_STP:
+			bstp_received_stp(bs, bp, m, &tpdu);
+			break;
 
-		cu.cu_root_path_cost = ntohl(cpdu.cbu_rootpathcost);
-		cu.cu_message_age = ntohs(cpdu.cbu_messageage);
-		cu.cu_max_age = ntohs(cpdu.cbu_maxage);
-		cu.cu_hello_time = ntohs(cpdu.cbu_hellotime);
-		cu.cu_forward_delay = ntohs(cpdu.cbu_forwarddelay);
-		cu.cu_port_id = ntohs(cpdu.cbu_portid);
-		cu.cu_message_type = cpdu.cbu_bpdutype;
-		cu.cu_topology_change_acknowledgment =
-		    (cpdu.cbu_flags & BSTP_FLAG_TCA) ? 1 : 0;
-		cu.cu_topology_change =
-		    (cpdu.cbu_flags & BSTP_FLAG_TC) ? 1 : 0;
-		bstp_received_config_bpdu(bs, bp, &cu);
-		break;
-	default:
-		goto out;
+		case BSTP_PROTO_RSTP:
+			bstp_received_rstp(bs, bp, m, &tpdu);
+			break;
 	}
-
 out:
 	BSTP_UNLOCK(bs);
 	if (m)
@@ -683,441 +534,1199 @@
 }
 
 static void
-bstp_received_config_bpdu(struct bstp_state *bs, struct bstp_port *bp,
+bstp_received_stp(struct bstp_state *bs, struct bstp_port *bp,
+    struct mbuf *m, struct bstp_tbpdu *tpdu)
+{
+	struct bstp_cbpdu cpdu;
+	struct bstp_config_unit *cu = &bp->bp_msg_cu;
+	struct bstp_tcn_unit tu;
+
+	switch (tpdu->tbu_bpdutype) {
+	case BSTP_MSGTYPE_TCN:
+		tu.tu_message_type = tpdu->tbu_bpdutype;
+		bstp_received_tcn(bs, bp, &tu);
+		break;
+	case BSTP_MSGTYPE_CFG:
+		if (m->m_len < BSTP_BPDU_STP_LEN &&
+		    (m = m_pullup(m, BSTP_BPDU_STP_LEN)) == NULL)
+			return;
+		memcpy(&cpdu, mtod(m, caddr_t), BSTP_BPDU_STP_LEN);
+
+		bstp_decode_bpdu(bp, &cpdu, cu);
+		bstp_received_bpdu(bs, bp, cu);
+		break;
+	}
+}
+
+static void
+bstp_received_rstp(struct bstp_state *bs, struct bstp_port *bp,
+    struct mbuf *m, struct bstp_tbpdu *tpdu)
+{
+	struct bstp_cbpdu cpdu;
+	struct bstp_config_unit *cu = &bp->bp_msg_cu;
+
+	if (tpdu->tbu_bpdutype != BSTP_MSGTYPE_RSTP) {
+		m_freem(m);
+		return;
+	}
+
+	if (m->m_len < BSTP_BPDU_RSTP_LEN &&
+			(m = m_pullup(m, BSTP_BPDU_RSTP_LEN)) == NULL)
+		return;
+	memcpy(&cpdu, mtod(m, caddr_t), BSTP_BPDU_RSTP_LEN);
+
+	bstp_decode_bpdu(bp, &cpdu, cu);
+	bstp_received_bpdu(bs, bp, cu);
+}
+
+static void
+bstp_received_tcn(struct bstp_state *bs, struct bstp_port *bp,
+    struct bstp_tcn_unit *tcn)
+{
+	bp->bp_rcvdtcn = 1;
+	bstp_update_tc(bp);
+}
+
+static void
+bstp_received_bpdu(struct bstp_state *bs, struct bstp_port *bp,
     struct bstp_config_unit *cu)
 {
-	int root;
+	int type;
 
 	BSTP_LOCK_ASSERT(bs);
 
-	root = bstp_root_bridge(bs);
+	/* We need to have transitioned to INFO_MINE before proceeding */
+	switch (bp->bp_infois) {
+		case BSTP_INFO_DISABLED:
+		case BSTP_INFO_AGED:
+			return;
+	}
+
+	type = bstp_pdu_rcvtype(bp, cu);
+
+	switch (type) {

>>> TRUNCATED FOR MAIL (1000 lines) <<<



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200611010926.kA19Q7CJ074105>