bridgestp.h revision 160899
1160703Sthompsa/* $NetBSD: if_bridgevar.h,v 1.4 2003/07/08 07:13:50 itojun Exp $ */ 2160703Sthompsa 3160703Sthompsa/* 4160703Sthompsa * Copyright 2001 Wasabi Systems, Inc. 5160703Sthompsa * All rights reserved. 6160703Sthompsa * 7160703Sthompsa * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8160703Sthompsa * 9160703Sthompsa * Redistribution and use in source and binary forms, with or without 10160703Sthompsa * modification, are permitted provided that the following conditions 11160703Sthompsa * are met: 12160703Sthompsa * 1. Redistributions of source code must retain the above copyright 13160703Sthompsa * notice, this list of conditions and the following disclaimer. 14160703Sthompsa * 2. Redistributions in binary form must reproduce the above copyright 15160703Sthompsa * notice, this list of conditions and the following disclaimer in the 16160703Sthompsa * documentation and/or other materials provided with the distribution. 17160703Sthompsa * 3. All advertising materials mentioning features or use of this software 18160703Sthompsa * must display the following acknowledgement: 19160703Sthompsa * This product includes software developed for the NetBSD Project by 20160703Sthompsa * Wasabi Systems, Inc. 21160703Sthompsa * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22160703Sthompsa * or promote products derived from this software without specific prior 23160703Sthompsa * written permission. 24160703Sthompsa * 25160703Sthompsa * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26160703Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27160703Sthompsa * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28160703Sthompsa * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29160703Sthompsa * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30160703Sthompsa * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31160703Sthompsa * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32160703Sthompsa * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33160703Sthompsa * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34160703Sthompsa * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35160703Sthompsa * POSSIBILITY OF SUCH DAMAGE. 36160703Sthompsa */ 37160703Sthompsa 38160703Sthompsa/* 39160703Sthompsa * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net) 40160703Sthompsa * All rights reserved. 41160703Sthompsa * 42160703Sthompsa * Redistribution and use in source and binary forms, with or without 43160703Sthompsa * modification, are permitted provided that the following conditions 44160703Sthompsa * are met: 45160703Sthompsa * 1. Redistributions of source code must retain the above copyright 46160703Sthompsa * notice, this list of conditions and the following disclaimer. 47160703Sthompsa * 2. Redistributions in binary form must reproduce the above copyright 48160703Sthompsa * notice, this list of conditions and the following disclaimer in the 49160703Sthompsa * documentation and/or other materials provided with the distribution. 50160703Sthompsa * 3. All advertising materials mentioning features or use of this software 51160703Sthompsa * must display the following acknowledgement: 52160703Sthompsa * This product includes software developed by Jason L. Wright 53160703Sthompsa * 4. The name of the author may not be used to endorse or promote products 54160703Sthompsa * derived from this software without specific prior written permission. 55160703Sthompsa * 56160703Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 57160703Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 58160703Sthompsa * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 59160703Sthompsa * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 60160703Sthompsa * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 61160703Sthompsa * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 62160703Sthompsa * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63160703Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 64160703Sthompsa * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 65160703Sthompsa * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66160703Sthompsa * POSSIBILITY OF SUCH DAMAGE. 67160703Sthompsa * 68160703Sthompsa * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp 69160703Sthompsa * 70160703Sthompsa * $FreeBSD: head/sys/net/bridgestp.h 160899 2006-08-02 02:47:27Z thompsa $ 71160703Sthompsa */ 72160703Sthompsa 73160703Sthompsa/* 74160703Sthompsa * Data structure and control definitions for STP interfaces. 75160703Sthompsa */ 76160703Sthompsa 77160703Sthompsa#include <sys/callout.h> 78160703Sthompsa#include <sys/queue.h> 79160703Sthompsa 80160703Sthompsa/* STP port states */ 81160703Sthompsa#define BSTP_IFSTATE_DISABLED 0 82160703Sthompsa#define BSTP_IFSTATE_LISTENING 1 83160703Sthompsa#define BSTP_IFSTATE_LEARNING 2 84160703Sthompsa#define BSTP_IFSTATE_FORWARDING 3 85160703Sthompsa#define BSTP_IFSTATE_BLOCKING 4 86160703Sthompsa 87160703Sthompsa#ifdef _KERNEL 88160703Sthompsa 89160703Sthompsa/* 90160703Sthompsa * Spanning tree defaults. 91160703Sthompsa */ 92160703Sthompsa#define BSTP_DEFAULT_MAX_AGE (20 * 256) 93160703Sthompsa#define BSTP_DEFAULT_HELLO_TIME (2 * 256) 94160703Sthompsa#define BSTP_DEFAULT_FORWARD_DELAY (15 * 256) 95160703Sthompsa#define BSTP_DEFAULT_HOLD_TIME (1 * 256) 96160703Sthompsa#define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000 97160703Sthompsa#define BSTP_DEFAULT_PORT_PRIORITY 0x80 98160703Sthompsa#define BSTP_DEFAULT_PATH_COST 55 99160703Sthompsa 100160703Sthompsa/* BPDU message types */ 101160703Sthompsa#define BSTP_MSGTYPE_CFG 0x00 /* Configuration */ 102160703Sthompsa#define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */ 103160703Sthompsa 104160703Sthompsa/* BPDU flags */ 105160703Sthompsa#define BSTP_FLAG_TC 0x01 /* Topology change */ 106160703Sthompsa#define BSTP_FLAG_TCA 0x80 /* Topology change ack */ 107160703Sthompsa 108160703Sthompsa#define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */ 109160703Sthompsa#define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */ 110160703Sthompsa#define BSTP_LINK_TIMER (BSTP_TICK_VAL * 30) 111160703Sthompsa 112160703Sthompsa/* 113160899Sthompsa * * Driver callbacks for STP state changes 114160899Sthompsa * */ 115160899Sthompsatypedef void (*bstp_state_cb_t)(struct ifnet *, int); 116160899Sthompsa 117160899Sthompsa/* 118160703Sthompsa * Because BPDU's do not make nicely aligned structures, two different 119160703Sthompsa * declarations are used: bstp_?bpdu (wire representation, packed) and 120160703Sthompsa * bstp_*_unit (internal, nicely aligned version). 121160703Sthompsa */ 122160703Sthompsa 123160703Sthompsa/* configuration bridge protocol data unit */ 124160703Sthompsastruct bstp_cbpdu { 125160703Sthompsa uint8_t cbu_dsap; /* LLC: destination sap */ 126160703Sthompsa uint8_t cbu_ssap; /* LLC: source sap */ 127160703Sthompsa uint8_t cbu_ctl; /* LLC: control */ 128160703Sthompsa uint16_t cbu_protoid; /* protocol id */ 129160703Sthompsa uint8_t cbu_protover; /* protocol version */ 130160703Sthompsa uint8_t cbu_bpdutype; /* message type */ 131160703Sthompsa uint8_t cbu_flags; /* flags (below) */ 132160703Sthompsa 133160703Sthompsa /* root id */ 134160703Sthompsa uint16_t cbu_rootpri; /* root priority */ 135160703Sthompsa uint8_t cbu_rootaddr[6]; /* root address */ 136160703Sthompsa 137160703Sthompsa uint32_t cbu_rootpathcost; /* root path cost */ 138160703Sthompsa 139160703Sthompsa /* bridge id */ 140160703Sthompsa uint16_t cbu_bridgepri; /* bridge priority */ 141160703Sthompsa uint8_t cbu_bridgeaddr[6]; /* bridge address */ 142160703Sthompsa 143160703Sthompsa uint16_t cbu_portid; /* port id */ 144160703Sthompsa uint16_t cbu_messageage; /* current message age */ 145160703Sthompsa uint16_t cbu_maxage; /* maximum age */ 146160703Sthompsa uint16_t cbu_hellotime; /* hello time */ 147160703Sthompsa uint16_t cbu_forwarddelay; /* forwarding delay */ 148160703Sthompsa} __attribute__((__packed__)); 149160703Sthompsa 150160703Sthompsa/* topology change notification bridge protocol data unit */ 151160703Sthompsastruct bstp_tbpdu { 152160703Sthompsa uint8_t tbu_dsap; /* LLC: destination sap */ 153160703Sthompsa uint8_t tbu_ssap; /* LLC: source sap */ 154160703Sthompsa uint8_t tbu_ctl; /* LLC: control */ 155160703Sthompsa uint16_t tbu_protoid; /* protocol id */ 156160703Sthompsa uint8_t tbu_protover; /* protocol version */ 157160703Sthompsa uint8_t tbu_bpdutype; /* message type */ 158160703Sthompsa} __attribute__((__packed__)); 159160703Sthompsa 160160703Sthompsa/* 161160703Sthompsa * Timekeeping structure used in spanning tree code. 162160703Sthompsa */ 163160703Sthompsastruct bstp_timer { 164160703Sthompsa uint16_t active; 165160703Sthompsa uint16_t value; 166160703Sthompsa}; 167160703Sthompsa 168160703Sthompsastruct bstp_config_unit { 169160703Sthompsa uint64_t cu_rootid; 170160703Sthompsa uint64_t cu_bridge_id; 171160703Sthompsa uint32_t cu_root_path_cost; 172160703Sthompsa uint16_t cu_message_age; 173160703Sthompsa uint16_t cu_max_age; 174160703Sthompsa uint16_t cu_hello_time; 175160703Sthompsa uint16_t cu_forward_delay; 176160703Sthompsa uint16_t cu_port_id; 177160703Sthompsa uint8_t cu_message_type; 178160703Sthompsa uint8_t cu_topology_change_acknowledgment; 179160703Sthompsa uint8_t cu_topology_change; 180160703Sthompsa}; 181160703Sthompsa 182160703Sthompsastruct bstp_tcn_unit { 183160703Sthompsa uint8_t tu_message_type; 184160703Sthompsa}; 185160703Sthompsa 186160703Sthompsa/* 187160703Sthompsa * Bridge interface list entry. 188160703Sthompsa */ 189160703Sthompsastruct bstp_port { 190160703Sthompsa LIST_ENTRY(bstp_port) bp_next; 191160703Sthompsa struct ifnet *bp_ifp; /* parent if */ 192160703Sthompsa struct bstp_state *bp_bs; 193160703Sthompsa int bp_active; 194160703Sthompsa uint64_t bp_designated_root; 195160703Sthompsa uint64_t bp_designated_bridge; 196160703Sthompsa uint32_t bp_path_cost; 197160703Sthompsa uint32_t bp_designated_cost; 198160703Sthompsa struct bstp_timer bp_hold_timer; 199160703Sthompsa struct bstp_timer bp_message_age_timer; 200160703Sthompsa struct bstp_timer bp_forward_delay_timer; 201160703Sthompsa struct bstp_config_unit bp_config_bpdu; 202160703Sthompsa uint16_t bp_port_id; 203160703Sthompsa uint16_t bp_designated_port; 204160703Sthompsa uint8_t bp_state; 205160703Sthompsa uint8_t bp_topology_change_acknowledge; 206160703Sthompsa uint8_t bp_config_pending; 207160703Sthompsa uint8_t bp_change_detection_enabled; 208160703Sthompsa uint8_t bp_priority; 209160867Sthompsa uint32_t bp_forward_transitions; 210160899Sthompsa struct task bp_statetask; 211160703Sthompsa}; 212160703Sthompsa 213160703Sthompsa/* 214160703Sthompsa * Software state for each bridge STP. 215160703Sthompsa */ 216160703Sthompsastruct bstp_state { 217160703Sthompsa LIST_ENTRY(bstp_state) bs_list; 218160703Sthompsa struct mtx bs_mtx; 219160703Sthompsa uint64_t bs_designated_root; 220160703Sthompsa uint64_t bs_bridge_id; 221160703Sthompsa struct bstp_port *bs_root_port; 222160703Sthompsa uint32_t bs_root_path_cost; 223160703Sthompsa uint16_t bs_max_age; 224160703Sthompsa uint16_t bs_hello_time; 225160703Sthompsa uint16_t bs_forward_delay; 226160703Sthompsa uint16_t bs_bridge_max_age; 227160703Sthompsa uint16_t bs_bridge_hello_time; 228160703Sthompsa uint16_t bs_bridge_forward_delay; 229160703Sthompsa uint16_t bs_topology_change_time; 230160703Sthompsa uint16_t bs_hold_time; 231160703Sthompsa uint16_t bs_bridge_priority; 232160703Sthompsa uint8_t bs_topology_change_detected; 233160703Sthompsa uint8_t bs_topology_change; 234160703Sthompsa struct bstp_timer bs_hello_timer; 235160703Sthompsa struct bstp_timer bs_topology_change_timer; 236160703Sthompsa struct bstp_timer bs_tcn_timer; 237160703Sthompsa struct callout bs_bstpcallout; /* STP callout */ 238160703Sthompsa struct bstp_timer bs_link_timer; 239160867Sthompsa struct timeval bs_last_tc_time; 240160703Sthompsa LIST_HEAD(, bstp_port) bs_bplist; 241160899Sthompsa bstp_state_cb_t bs_state_cb; 242160703Sthompsa}; 243160703Sthompsa 244160703Sthompsa#define BSTP_LOCK_INIT(_bs) mtx_init(&(_bs)->bs_mtx, "bstp", \ 245160703Sthompsa NULL, MTX_DEF) 246160703Sthompsa#define BSTP_LOCK_DESTROY(_bs) mtx_destroy(&(_bs)->bs_mtx) 247160703Sthompsa#define BSTP_LOCK(_bs) mtx_lock(&(_bs)->bs_mtx) 248160703Sthompsa#define BSTP_UNLOCK(_bs) mtx_unlock(&(_bs)->bs_mtx) 249160703Sthompsa#define BSTP_LOCK_ASSERT(_bs) mtx_assert(&(_bs)->bs_mtx, MA_OWNED) 250160703Sthompsa 251160703Sthompsaextern const uint8_t bstp_etheraddr[]; 252160703Sthompsa 253160703Sthompsaextern void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 254160703Sthompsa 255160899Sthompsavoid bstp_attach(struct bstp_state *, bstp_state_cb_t); 256160703Sthompsavoid bstp_detach(struct bstp_state *); 257160703Sthompsavoid bstp_init(struct bstp_state *); 258160703Sthompsavoid bstp_reinit(struct bstp_state *); 259160703Sthompsavoid bstp_stop(struct bstp_state *); 260160703Sthompsaint bstp_add(struct bstp_state *, struct bstp_port *, struct ifnet *); 261160703Sthompsavoid bstp_delete(struct bstp_port *); 262160899Sthompsavoid bstp_drain(struct bstp_port *); 263160703Sthompsavoid bstp_linkstate(struct ifnet *, int); 264160703Sthompsastruct mbuf *bstp_input(struct bstp_port *, struct ifnet *, struct mbuf *); 265160703Sthompsa 266160703Sthompsa#endif /* _KERNEL */ 267