bridgestp.h revision 163863
162587Sitojun/* $NetBSD: if_bridgevar.h,v 1.4 2003/07/08 07:13:50 itojun Exp $ */ 278064Sume 362587Sitojun/* 4139826Simp * Copyright 2001 Wasabi Systems, Inc. 553541Sshin * All rights reserved. 653541Sshin * 753541Sshin * Written by Jason R. Thorpe for Wasabi Systems, Inc. 853541Sshin * 953541Sshin * Redistribution and use in source and binary forms, with or without 1053541Sshin * modification, are permitted provided that the following conditions 1153541Sshin * are met: 1253541Sshin * 1. Redistributions of source code must retain the above copyright 1353541Sshin * notice, this list of conditions and the following disclaimer. 1453541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1553541Sshin * notice, this list of conditions and the following disclaimer in the 1653541Sshin * documentation and/or other materials provided with the distribution. 1753541Sshin * 3. All advertising materials mentioning features or use of this software 1853541Sshin * must display the following acknowledgement: 1953541Sshin * This product includes software developed for the NetBSD Project by 2053541Sshin * Wasabi Systems, Inc. 2153541Sshin * 4. The name of Wasabi Systems, Inc. may not be used to endorse 2253541Sshin * or promote products derived from this software without specific prior 2353541Sshin * written permission. 2453541Sshin * 2553541Sshin * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 2653541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2753541Sshin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2853541Sshin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 2953541Sshin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3053541Sshin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3153541Sshin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3253541Sshin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3362587Sitojun * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3462587Sitojun * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3555009Sshin * POSSIBILITY OF SUCH DAMAGE. 36148921Ssuz */ 3753541Sshin 3853541Sshin/* 3953541Sshin * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net) 4078064Sume * All rights reserved. 4153541Sshin * 4253541Sshin * Redistribution and use in source and binary forms, with or without 4353541Sshin * modification, are permitted provided that the following conditions 4453541Sshin * are met: 4553541Sshin * 1. Redistributions of source code must retain the above copyright 4653541Sshin * notice, this list of conditions and the following disclaimer. 4778064Sume * 2. Redistributions in binary form must reproduce the above copyright 4853541Sshin * notice, this list of conditions and the following disclaimer in the 4953541Sshin * documentation and/or other materials provided with the distribution. 5053541Sshin * 3. All advertising materials mentioning features or use of this software 5153541Sshin * must display the following acknowledgement: 5284994Sdarrenr * This product includes software developed by Jason L. Wright 5353541Sshin * 4. The name of the author may not be used to endorse or promote products 5453541Sshin * derived from this software without specific prior written permission. 5553541Sshin * 5678064Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 5778064Sume * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 5862587Sitojun * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 5978064Sume * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 6062587Sitojun * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 6153541Sshin * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 62148385Sume * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6362587Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 6453541Sshin * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 6553541Sshin * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 6678064Sume * POSSIBILITY OF SUCH DAMAGE. 6778064Sume * 6863256Sitojun * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp 6953541Sshin * 7078064Sume * $FreeBSD: head/sys/net/bridgestp.h 163863 2006-11-01 09:07:47Z thompsa $ 7162601Sitojun */ 7278064Sume 7353541Sshin/* 7463256Sitojun * Data structure and control definitions for STP interfaces. 7553541Sshin */ 76105199Ssam 77105199Ssam#include <sys/callout.h> 78105199Ssam#include <sys/queue.h> 79105199Ssam 80105199Ssam/* STP port states */ 81105199Ssam#define BSTP_IFSTATE_DISABLED 0 82105199Ssam#define BSTP_IFSTATE_LISTENING 1 8384994Sdarrenr#define BSTP_IFSTATE_LEARNING 2 8484994Sdarrenr#define BSTP_IFSTATE_FORWARDING 3 8553541Sshin#define BSTP_IFSTATE_BLOCKING 4 8653541Sshin#define BSTP_IFSTATE_DISCARDING 5 8753541Sshin 8853541Sshin#define BSTP_TCSTATE_ACTIVE 1 8953541Sshin#define BSTP_TCSTATE_DETECTED 2 9053541Sshin#define BSTP_TCSTATE_INACTIVE 3 9153541Sshin#define BSTP_TCSTATE_LEARNING 4 9253541Sshin#define BSTP_TCSTATE_PROPAG 5 9353541Sshin#define BSTP_TCSTATE_ACK 6 9453541Sshin#define BSTP_TCSTATE_TC 7 9553541Sshin#define BSTP_TCSTATE_TCN 8 9653541Sshin 9753541Sshin#define BSTP_ROLE_DISABLED 0 9853541Sshin#define BSTP_ROLE_ROOT 1 9953541Sshin#define BSTP_ROLE_DESIGNATED 2 10053541Sshin#define BSTP_ROLE_ALTERNATE 3 10153541Sshin#define BSTP_ROLE_BACKUP 4 10253541Sshin 10353541Sshin#ifdef _KERNEL 10453541Sshin 10553541Sshin/* STP port flags */ 106122062Sume#define BSTP_PORT_CANMIGRATE 0x0001 107122062Sume#define BSTP_PORT_NEWINFO 0x0002 10853541Sshin#define BSTP_PORT_DISPUTED 0x0004 10962587Sitojun#define BSTP_PORT_ADMCOST 0x0008 11062587Sitojun#define BSTP_PORT_AUTOEDGE 0x0010 111148385Sume 112148385Sume/* BPDU priority */ 11363256Sitojun#define BSTP_PDU_SUPERIOR 1 11453541Sshin#define BSTP_PDU_REPEATED 2 115122062Sume#define BSTP_PDU_INFERIOR 3 11653541Sshin#define BSTP_PDU_INFERIORALT 4 11753541Sshin#define BSTP_PDU_OTHER 5 118158295Sbz 119158295Sbz/* BPDU flags */ 12063256Sitojun#define BSTP_PDU_PRMASK 0x0c /* Port Role */ 12153541Sshin#define BSTP_PDU_PRSHIFT 2 /* Port Role offset */ 12253541Sshin#define BSTP_PDU_F_UNKN 0x00 /* Unknown port (00) */ 12353541Sshin#define BSTP_PDU_F_ALT 0x01 /* Alt/Backup port (01) */ 12453541Sshin#define BSTP_PDU_F_ROOT 0x02 /* Root port (10) */ 12553541Sshin#define BSTP_PDU_F_DESG 0x03 /* Designated port (11) */ 12653541Sshin 12753541Sshin#define BSTP_PDU_STPMASK 0x81 /* strip unused STP flags */ 12853541Sshin#define BSTP_PDU_RSTPMASK 0x7f /* strip unused RSTP flags */ 129105199Ssam#define BSTP_PDU_F_TC 0x01 /* Topology change */ 13053541Sshin#define BSTP_PDU_F_P 0x02 /* Proposal flag */ 131105199Ssam#define BSTP_PDU_F_L 0x10 /* Learning flag */ 13253541Sshin#define BSTP_PDU_F_F 0x20 /* Forwarding flag */ 13353541Sshin#define BSTP_PDU_F_A 0x40 /* Agreement flag */ 13453541Sshin#define BSTP_PDU_F_TCA 0x80 /* Topology change ack */ 13595023Ssuz 13653541Sshin/* 13778064Sume * Spanning tree defaults. 13878064Sume */ 13978064Sume#define BSTP_DEFAULT_MAX_AGE (20 * 256) 14078064Sume#define BSTP_DEFAULT_HELLO_TIME (2 * 256) 141120913Sume#define BSTP_DEFAULT_FORWARD_DELAY (15 * 256) 14278064Sume#define BSTP_DEFAULT_HOLD_TIME (1 * 256) 14362587Sitojun#define BSTP_DEFAULT_MIGRATE_DELAY (3 * 256) 14478064Sume#define BSTP_DEFAULT_HOLD_COUNT 6 14578064Sume#define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000 14653541Sshin#define BSTP_DEFAULT_PORT_PRIORITY 0x80 14753541Sshin#define BSTP_DEFAULT_PATH_COST 55 14853541Sshin#define BSTP_MIN_HELLO_TIME (1 * 256) 14953541Sshin#define BSTP_MIN_MAX_AGE (6 * 256) 15053541Sshin#define BSTP_MIN_FORWARD_DELAY (4 * 256) 15153541Sshin#define BSTP_MIN_HOLD_COUNT 1 15253541Sshin#define BSTP_MAX_HELLO_TIME (2 * 256) 15362587Sitojun#define BSTP_MAX_MAX_AGE (40 * 256) 15462587Sitojun#define BSTP_MAX_FORWARD_DELAY (30 * 256) 15553541Sshin#define BSTP_MAX_HOLD_COUNT 10 15653541Sshin#define BSTP_MAX_PRIORITY 61440 15753541Sshin#define BSTP_MAX_PORT_PRIORITY 240 15853541Sshin#define BSTP_MAX_PATH_COST 200000000 15953541Sshin 16053541Sshin/* BPDU message types */ 16153541Sshin#define BSTP_MSGTYPE_CFG 0x00 /* Configuration */ 162148921Ssuz#define BSTP_MSGTYPE_RSTP 0x02 /* Rapid STP */ 163148921Ssuz#define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */ 164148921Ssuz 16553541Sshin/* Protocol versions */ 16653541Sshin#define BSTP_PROTO_ID 0x00 16753541Sshin#define BSTP_PROTO_STP 0x00 16853541Sshin#define BSTP_PROTO_RSTP 0x02 16953541Sshin#define BSTP_PROTO_MAX BSTP_PROTO_RSTP 17053541Sshin 17153541Sshin#define BSTP_INFO_RECIEVED 1 17253541Sshin#define BSTP_INFO_MINE 2 173148921Ssuz#define BSTP_INFO_AGED 3 174148921Ssuz#define BSTP_INFO_DISABLED 4 175148921Ssuz 176148921Ssuz 17762587Sitojun#define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */ 17862587Sitojun#define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */ 17962587Sitojun#define BSTP_LINK_TIMER (BSTP_TICK_VAL * 15) 18062587Sitojun 18162587Sitojun/* 18262587Sitojun * Driver callbacks for STP state changes 18362587Sitojun */ 18462587Sitojuntypedef void (*bstp_state_cb_t)(struct ifnet *, int); 18562587Sitojuntypedef void (*bstp_rtage_cb_t)(struct ifnet *, int); 18662587Sitojun 18762587Sitojun/* 18863256Sitojun * Because BPDU's do not make nicely aligned structures, two different 18953541Sshin * declarations are used: bstp_?bpdu (wire representation, packed) and 190120913Sume * bstp_*_unit (internal, nicely aligned version). 191120913Sume */ 19253541Sshin 19353541Sshin/* configuration bridge protocol data unit */ 19453541Sshinstruct bstp_cbpdu { 19562587Sitojun uint8_t cbu_dsap; /* LLC: destination sap */ 19662587Sitojun uint8_t cbu_ssap; /* LLC: source sap */ 19762587Sitojun uint8_t cbu_ctl; /* LLC: control */ 19862587Sitojun uint16_t cbu_protoid; /* protocol id */ 19962587Sitojun uint8_t cbu_protover; /* protocol version */ 20062587Sitojun uint8_t cbu_bpdutype; /* message type */ 20162587Sitojun uint8_t cbu_flags; /* flags (below) */ 20253541Sshin 20353541Sshin /* root id */ 20453541Sshin uint16_t cbu_rootpri; /* root priority */ 20553541Sshin uint8_t cbu_rootaddr[6]; /* root address */ 20653541Sshin 20753541Sshin uint32_t cbu_rootpathcost; /* root path cost */ 20853541Sshin 20953541Sshin /* bridge id */ 21053541Sshin uint16_t cbu_bridgepri; /* bridge priority */ 21153541Sshin uint8_t cbu_bridgeaddr[6]; /* bridge address */ 21253541Sshin 21353541Sshin uint16_t cbu_portid; /* port id */ 21453541Sshin uint16_t cbu_messageage; /* current message age */ 21553541Sshin uint16_t cbu_maxage; /* maximum age */ 21653541Sshin uint16_t cbu_hellotime; /* hello time */ 21762587Sitojun uint16_t cbu_forwarddelay; /* forwarding delay */ 21862587Sitojun uint8_t cbu_versionlen; /* version 1 length */ 21962587Sitojun} __attribute__((__packed__)); 22062587Sitojun#define BSTP_BPDU_STP_LEN (3 + 35) /* LLC + STP pdu */ 22162587Sitojun#define BSTP_BPDU_RSTP_LEN (3 + 36) /* LLC + RSTP pdu */ 22262587Sitojun 22362587Sitojun/* topology change notification bridge protocol data unit */ 22453541Sshinstruct bstp_tbpdu { 22553541Sshin uint8_t tbu_dsap; /* LLC: destination sap */ 22653541Sshin uint8_t tbu_ssap; /* LLC: source sap */ 22753541Sshin uint8_t tbu_ctl; /* LLC: control */ 22853541Sshin uint16_t tbu_protoid; /* protocol id */ 22953541Sshin uint8_t tbu_protover; /* protocol version */ 23053541Sshin uint8_t tbu_bpdutype; /* message type */ 23153541Sshin} __attribute__((__packed__)); 23262587Sitojun 23353541Sshin/* 23453541Sshin * Timekeeping structure used in spanning tree code. 23553541Sshin */ 23653541Sshinstruct bstp_timer { 23753541Sshin int active; 23853541Sshin int latched; 23962587Sitojun int value; 24062587Sitojun}; 24162587Sitojun 24262587Sitojunstruct bstp_pri_vector { 24362587Sitojun uint64_t pv_root_id; 24462587Sitojun uint32_t pv_cost; 24562587Sitojun uint64_t pv_dbridge_id; 24653541Sshin uint16_t pv_dport_id; 24753541Sshin uint16_t pv_port_id; 24853541Sshin}; 24953541Sshin 25053541Sshinstruct bstp_config_unit { 25153541Sshin struct bstp_pri_vector cu_pv; 25253541Sshin uint16_t cu_message_age; 25353541Sshin uint16_t cu_max_age; 25453541Sshin uint16_t cu_forward_delay; 25553541Sshin uint16_t cu_hello_time; 25653541Sshin uint8_t cu_message_type; 25753541Sshin uint8_t cu_topology_change_ack; 25853541Sshin uint8_t cu_topology_change; 25953541Sshin uint8_t cu_proposal; 26053541Sshin uint8_t cu_agree; 261122062Sume uint8_t cu_learning; 26253541Sshin uint8_t cu_forwarding; 26353541Sshin uint8_t cu_role; 26453541Sshin}; 265122062Sume 266122062Sumestruct bstp_tcn_unit { 267122062Sume uint8_t tu_message_type; 268122062Sume}; 269122062Sume 270122062Sumestruct bstp_port { 271122062Sume LIST_ENTRY(bstp_port) bp_next; 272122062Sume struct ifnet *bp_ifp; /* parent if */ 273122062Sume struct bstp_state *bp_bs; 274126006Sume uint8_t bp_active; 275126006Sume uint8_t bp_protover; 276126006Sume uint32_t bp_flags; 277126006Sume uint32_t bp_path_cost; 278122062Sume uint16_t bp_port_msg_age; 279122062Sume uint16_t bp_port_max_age; 280122062Sume uint16_t bp_port_fdelay; 281126006Sume uint16_t bp_port_htime; 282126006Sume uint16_t bp_desg_msg_age; 283126006Sume uint16_t bp_desg_max_age; 284126006Sume uint16_t bp_desg_fdelay; 285126006Sume uint16_t bp_desg_htime; 286126006Sume struct bstp_timer bp_edge_delay_timer; 287126006Sume struct bstp_timer bp_forward_delay_timer; 28853541Sshin struct bstp_timer bp_hello_timer; 28953541Sshin struct bstp_timer bp_message_age_timer; 29053541Sshin struct bstp_timer bp_migrate_delay_timer; 29153541Sshin struct bstp_timer bp_recent_backup_timer; 29253541Sshin struct bstp_timer bp_recent_root_timer; 29353541Sshin struct bstp_timer bp_tc_timer; 29453541Sshin struct bstp_config_unit bp_msg_cu; 29553541Sshin struct bstp_pri_vector bp_desg_pv; 29653541Sshin struct bstp_pri_vector bp_port_pv; 29753541Sshin uint16_t bp_port_id; 29853541Sshin uint8_t bp_state; 29953541Sshin uint8_t bp_tcstate; 30053541Sshin uint8_t bp_role; 30153541Sshin uint8_t bp_infois; 30253541Sshin uint8_t bp_tc_ack; 30353541Sshin uint8_t bp_tc_prop; 30453541Sshin uint8_t bp_fdbflush; 30553541Sshin uint8_t bp_priority; 30653541Sshin uint8_t bp_p2p_link; 30753541Sshin uint8_t bp_agree; 30853541Sshin uint8_t bp_agreed; 30953541Sshin uint8_t bp_sync; 31053541Sshin uint8_t bp_synced; 31153541Sshin uint8_t bp_proposing; 31253541Sshin uint8_t bp_proposed; 31353541Sshin uint8_t bp_operedge; 31453541Sshin uint8_t bp_reroot; 31553541Sshin uint8_t bp_rcvdtc; 316120913Sume uint8_t bp_rcvdtca; 31753541Sshin uint8_t bp_rcvdtcn; 31853541Sshin uint32_t bp_forward_transitions; 31953541Sshin uint8_t bp_txcount; 32053541Sshin struct task bp_statetask; 32153541Sshin struct task bp_rtagetask; 32262587Sitojun}; 32362587Sitojun 32462587Sitojun/* 32562587Sitojun * Software state for each bridge STP. 32662587Sitojun */ 32762587Sitojunstruct bstp_state { 32862587Sitojun LIST_ENTRY(bstp_state) bs_list; 32953541Sshin struct mtx bs_mtx; 33053541Sshin struct bstp_pri_vector bs_bridge_pv; 33153541Sshin struct bstp_pri_vector bs_root_pv; 332122062Sume struct bstp_port *bs_root_port; 333126006Sume uint8_t bs_protover; 334126006Sume uint16_t bs_migration_delay; 335126006Sume uint16_t bs_edge_delay; 336126006Sume uint16_t bs_bridge_max_age; 337126006Sume uint16_t bs_bridge_fdelay; 338126006Sume uint16_t bs_bridge_htime; 339126006Sume uint16_t bs_root_msg_age; 340126006Sume uint16_t bs_root_max_age; 341126006Sume uint16_t bs_root_fdelay; 342126006Sume uint16_t bs_root_htime; 343122062Sume uint16_t bs_hold_time; 344122062Sume uint16_t bs_bridge_priority; 345122062Sume uint8_t bs_txholdcount; 346122062Sume uint8_t bs_allsynced; 347122062Sume struct callout bs_bstpcallout; /* STP callout */ 34853541Sshin struct bstp_timer bs_link_timer; 34953541Sshin struct timeval bs_last_tc_time; 35063256Sitojun LIST_HEAD(, bstp_port) bs_bplist; 35153541Sshin bstp_state_cb_t bs_state_cb; 352122062Sume bstp_rtage_cb_t bs_rtage_cb; 353122062Sume}; 354122062Sume 355122062Sume#define BSTP_LOCK_INIT(_bs) mtx_init(&(_bs)->bs_mtx, "bstp", NULL, MTX_DEF) 356122062Sume#define BSTP_LOCK_DESTROY(_bs) mtx_destroy(&(_bs)->bs_mtx) 35778064Sume#define BSTP_LOCK(_bs) mtx_lock(&(_bs)->bs_mtx) 35853541Sshin#define BSTP_UNLOCK(_bs) mtx_unlock(&(_bs)->bs_mtx) 359120913Sume#define BSTP_LOCK_ASSERT(_bs) mtx_assert(&(_bs)->bs_mtx, MA_OWNED) 36053541Sshin 36153541Sshinextern const uint8_t bstp_etheraddr[]; 36253541Sshin 36353541Sshinextern void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 36453541Sshin 36553541Sshinvoid bstp_attach(struct bstp_state *, bstp_state_cb_t, bstp_rtage_cb_t); 366120913Sumevoid bstp_detach(struct bstp_state *); 36753541Sshinvoid bstp_init(struct bstp_state *); 368122921Sandrevoid bstp_stop(struct bstp_state *); 36953541Sshinint bstp_add(struct bstp_state *, struct bstp_port *, struct ifnet *); 37062587Sitojunvoid bstp_delete(struct bstp_port *); 37153541Sshinvoid bstp_drain(struct bstp_port *); 37253541Sshinvoid bstp_linkstate(struct ifnet *, int); 37378064Sumeint bstp_set_htime(struct bstp_state *, int); 37462587Sitojunint bstp_set_fdelay(struct bstp_state *, int); 37562587Sitojunint bstp_set_maxage(struct bstp_state *, int); 37662587Sitojunint bstp_set_holdcount(struct bstp_state *, int); 37762587Sitojunint bstp_set_protocol(struct bstp_state *, int); 37862587Sitojunint bstp_set_priority(struct bstp_state *, int); 37953541Sshinint bstp_set_port_priority(struct bstp_port *, int); 38053541Sshinint bstp_set_path_cost(struct bstp_port *, uint32_t); 38153541Sshinint bstp_set_edge(struct bstp_port *, int); 382120913Sumeint bstp_set_autoedge(struct bstp_port *, int); 38353541Sshinstruct mbuf *bstp_input(struct bstp_port *, struct ifnet *, struct mbuf *); 38453541Sshin 38553541Sshin#endif /* _KERNEL */ 38653541Sshin