bridgestp.h revision 160703
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 160703 2006-07-26 10:43:02Z 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/*
113160703Sthompsa * Because BPDU's do not make nicely aligned structures, two different
114160703Sthompsa * declarations are used: bstp_?bpdu (wire representation, packed) and
115160703Sthompsa * bstp_*_unit (internal, nicely aligned version).
116160703Sthompsa */
117160703Sthompsa
118160703Sthompsa/* configuration bridge protocol data unit */
119160703Sthompsastruct bstp_cbpdu {
120160703Sthompsa	uint8_t		cbu_dsap;		/* LLC: destination sap */
121160703Sthompsa	uint8_t		cbu_ssap;		/* LLC: source sap */
122160703Sthompsa	uint8_t		cbu_ctl;		/* LLC: control */
123160703Sthompsa	uint16_t	cbu_protoid;		/* protocol id */
124160703Sthompsa	uint8_t		cbu_protover;		/* protocol version */
125160703Sthompsa	uint8_t		cbu_bpdutype;		/* message type */
126160703Sthompsa	uint8_t		cbu_flags;		/* flags (below) */
127160703Sthompsa
128160703Sthompsa	/* root id */
129160703Sthompsa	uint16_t	cbu_rootpri;		/* root priority */
130160703Sthompsa	uint8_t	cbu_rootaddr[6];	/* root address */
131160703Sthompsa
132160703Sthompsa	uint32_t	cbu_rootpathcost;	/* root path cost */
133160703Sthompsa
134160703Sthompsa	/* bridge id */
135160703Sthompsa	uint16_t	cbu_bridgepri;		/* bridge priority */
136160703Sthompsa	uint8_t		cbu_bridgeaddr[6];	/* bridge address */
137160703Sthompsa
138160703Sthompsa	uint16_t	cbu_portid;		/* port id */
139160703Sthompsa	uint16_t	cbu_messageage;		/* current message age */
140160703Sthompsa	uint16_t	cbu_maxage;		/* maximum age */
141160703Sthompsa	uint16_t	cbu_hellotime;		/* hello time */
142160703Sthompsa	uint16_t	cbu_forwarddelay;	/* forwarding delay */
143160703Sthompsa} __attribute__((__packed__));
144160703Sthompsa
145160703Sthompsa/* topology change notification bridge protocol data unit */
146160703Sthompsastruct bstp_tbpdu {
147160703Sthompsa	uint8_t		tbu_dsap;		/* LLC: destination sap */
148160703Sthompsa	uint8_t		tbu_ssap;		/* LLC: source sap */
149160703Sthompsa	uint8_t		tbu_ctl;		/* LLC: control */
150160703Sthompsa	uint16_t	tbu_protoid;		/* protocol id */
151160703Sthompsa	uint8_t		tbu_protover;		/* protocol version */
152160703Sthompsa	uint8_t		tbu_bpdutype;		/* message type */
153160703Sthompsa} __attribute__((__packed__));
154160703Sthompsa
155160703Sthompsa/*
156160703Sthompsa * Timekeeping structure used in spanning tree code.
157160703Sthompsa */
158160703Sthompsastruct bstp_timer {
159160703Sthompsa	uint16_t	active;
160160703Sthompsa	uint16_t	value;
161160703Sthompsa};
162160703Sthompsa
163160703Sthompsastruct bstp_config_unit {
164160703Sthompsa	uint64_t	cu_rootid;
165160703Sthompsa	uint64_t	cu_bridge_id;
166160703Sthompsa	uint32_t	cu_root_path_cost;
167160703Sthompsa	uint16_t	cu_message_age;
168160703Sthompsa	uint16_t	cu_max_age;
169160703Sthompsa	uint16_t	cu_hello_time;
170160703Sthompsa	uint16_t	cu_forward_delay;
171160703Sthompsa	uint16_t	cu_port_id;
172160703Sthompsa	uint8_t		cu_message_type;
173160703Sthompsa	uint8_t		cu_topology_change_acknowledgment;
174160703Sthompsa	uint8_t		cu_topology_change;
175160703Sthompsa};
176160703Sthompsa
177160703Sthompsastruct bstp_tcn_unit {
178160703Sthompsa	uint8_t		tu_message_type;
179160703Sthompsa};
180160703Sthompsa
181160703Sthompsa/*
182160703Sthompsa * Bridge interface list entry.
183160703Sthompsa */
184160703Sthompsastruct bstp_port {
185160703Sthompsa	LIST_ENTRY(bstp_port)	bp_next;
186160703Sthompsa	struct ifnet		*bp_ifp;	/* parent if */
187160703Sthompsa	struct bstp_state	*bp_bs;
188160703Sthompsa	int			bp_active;
189160703Sthompsa	uint64_t		bp_designated_root;
190160703Sthompsa	uint64_t		bp_designated_bridge;
191160703Sthompsa	uint32_t		bp_path_cost;
192160703Sthompsa	uint32_t		bp_designated_cost;
193160703Sthompsa	struct bstp_timer	bp_hold_timer;
194160703Sthompsa	struct bstp_timer	bp_message_age_timer;
195160703Sthompsa	struct bstp_timer	bp_forward_delay_timer;
196160703Sthompsa	struct bstp_config_unit	bp_config_bpdu;
197160703Sthompsa	uint16_t		bp_port_id;
198160703Sthompsa	uint16_t		bp_designated_port;
199160703Sthompsa	uint8_t			bp_state;
200160703Sthompsa	uint8_t			bp_topology_change_acknowledge;
201160703Sthompsa	uint8_t			bp_config_pending;
202160703Sthompsa	uint8_t			bp_change_detection_enabled;
203160703Sthompsa	uint8_t			bp_priority;
204160703Sthompsa};
205160703Sthompsa
206160703Sthompsa/*
207160703Sthompsa * Software state for each bridge STP.
208160703Sthompsa */
209160703Sthompsastruct bstp_state {
210160703Sthompsa	LIST_ENTRY(bstp_state)	bs_list;
211160703Sthompsa	struct mtx		bs_mtx;
212160703Sthompsa	uint64_t		bs_designated_root;
213160703Sthompsa	uint64_t		bs_bridge_id;
214160703Sthompsa	struct bstp_port	*bs_root_port;
215160703Sthompsa	uint32_t		bs_root_path_cost;
216160703Sthompsa	uint16_t		bs_max_age;
217160703Sthompsa	uint16_t		bs_hello_time;
218160703Sthompsa	uint16_t		bs_forward_delay;
219160703Sthompsa	uint16_t		bs_bridge_max_age;
220160703Sthompsa	uint16_t		bs_bridge_hello_time;
221160703Sthompsa	uint16_t		bs_bridge_forward_delay;
222160703Sthompsa	uint16_t		bs_topology_change_time;
223160703Sthompsa	uint16_t		bs_hold_time;
224160703Sthompsa	uint16_t		bs_bridge_priority;
225160703Sthompsa	uint8_t			bs_topology_change_detected;
226160703Sthompsa	uint8_t			bs_topology_change;
227160703Sthompsa	struct bstp_timer	bs_hello_timer;
228160703Sthompsa	struct bstp_timer	bs_topology_change_timer;
229160703Sthompsa	struct bstp_timer	bs_tcn_timer;
230160703Sthompsa	struct callout		bs_bstpcallout;	/* STP callout */
231160703Sthompsa	struct bstp_timer	bs_link_timer;
232160703Sthompsa	LIST_HEAD(, bstp_port)	bs_bplist;
233160703Sthompsa};
234160703Sthompsa
235160703Sthompsa#define BSTP_LOCK_INIT(_bs)	mtx_init(&(_bs)->bs_mtx, "bstp", \
236160703Sthompsa					    NULL, MTX_DEF)
237160703Sthompsa#define BSTP_LOCK_DESTROY(_bs)	mtx_destroy(&(_bs)->bs_mtx)
238160703Sthompsa#define BSTP_LOCK(_bs)		mtx_lock(&(_bs)->bs_mtx)
239160703Sthompsa#define BSTP_UNLOCK(_bs)	mtx_unlock(&(_bs)->bs_mtx)
240160703Sthompsa#define BSTP_LOCK_ASSERT(_bs)	mtx_assert(&(_bs)->bs_mtx, MA_OWNED)
241160703Sthompsa
242160703Sthompsaextern const uint8_t bstp_etheraddr[];
243160703Sthompsa
244160703Sthompsaextern	void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
245160703Sthompsa
246160703Sthompsavoid	bstp_attach(struct bstp_state *);
247160703Sthompsavoid	bstp_detach(struct bstp_state *);
248160703Sthompsavoid	bstp_init(struct bstp_state *);
249160703Sthompsavoid	bstp_reinit(struct bstp_state *);
250160703Sthompsavoid	bstp_stop(struct bstp_state *);
251160703Sthompsaint	bstp_add(struct bstp_state *, struct bstp_port *, struct ifnet *);
252160703Sthompsavoid	bstp_delete(struct bstp_port *);
253160703Sthompsavoid	bstp_linkstate(struct ifnet *, int);
254160703Sthompsastruct mbuf *bstp_input(struct bstp_port *, struct ifnet *, struct mbuf *);
255160703Sthompsa
256160703Sthompsa#endif /* _KERNEL */
257