if_bridgevar.h revision 164880
1262569Simp/*	$NetBSD: if_bridgevar.h,v 1.4 2003/07/08 07:13:50 itojun Exp $	*/
2262569Simp
3262569Simp/*
4262569Simp * Copyright 2001 Wasabi Systems, Inc.
5262569Simp * All rights reserved.
6262569Simp *
7262569Simp * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8262569Simp *
9262569Simp * Redistribution and use in source and binary forms, with or without
10262569Simp * modification, are permitted provided that the following conditions
11262569Simp * are met:
12262569Simp * 1. Redistributions of source code must retain the above copyright
13262569Simp *    notice, this list of conditions and the following disclaimer.
14262569Simp * 2. Redistributions in binary form must reproduce the above copyright
15262569Simp *    notice, this list of conditions and the following disclaimer in the
16262569Simp *    documentation and/or other materials provided with the distribution.
17262569Simp * 3. All advertising materials mentioning features or use of this software
18262569Simp *    must display the following acknowledgement:
19262569Simp *	This product includes software developed for the NetBSD Project by
20262569Simp *	Wasabi Systems, Inc.
21262569Simp * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22262569Simp *    or promote products derived from this software without specific prior
23262569Simp *    written permission.
24262569Simp *
25262569Simp * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26262569Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27262569Simp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28262569Simp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29262569Simp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30262569Simp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31262569Simp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32262569Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33262569Simp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34262569Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35262569Simp * POSSIBILITY OF SUCH DAMAGE.
36262569Simp */
37262569Simp
38262569Simp/*
39262569Simp * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
40262569Simp * All rights reserved.
41262569Simp *
42262569Simp * Redistribution and use in source and binary forms, with or without
43262569Simp * modification, are permitted provided that the following conditions
44262569Simp * are met:
45262569Simp * 1. Redistributions of source code must retain the above copyright
46262569Simp *    notice, this list of conditions and the following disclaimer.
47262569Simp * 2. Redistributions in binary form must reproduce the above copyright
48262569Simp *    notice, this list of conditions and the following disclaimer in the
49262569Simp *    documentation and/or other materials provided with the distribution.
50262569Simp * 3. All advertising materials mentioning features or use of this software
51262569Simp *    must display the following acknowledgement:
52262569Simp *	This product includes software developed by Jason L. Wright
53262569Simp * 4. The name of the author may not be used to endorse or promote products
54262569Simp *    derived from this software without specific prior written permission.
55262569Simp *
56262569Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57262569Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
58262569Simp * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
59262569Simp * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
60262569Simp * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
61262569Simp * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
62262569Simp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63262569Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64262569Simp * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
65262569Simp * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66262569Simp * POSSIBILITY OF SUCH DAMAGE.
67262569Simp *
68262569Simp * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp
69262569Simp *
70262569Simp * $FreeBSD: head/sys/net/if_bridgevar.h 164880 2006-12-04 14:45:02Z syrinx $
71262569Simp */
72262569Simp
73262569Simp/*
74262569Simp * Data structure and control definitions for bridge interfaces.
75262569Simp */
76262569Simp
77262569Simp#include <sys/callout.h>
78262569Simp#include <sys/queue.h>
79262569Simp#include <sys/condvar.h>
80262569Simp
81262569Simp/*
82262569Simp * Commands used in the SIOCSDRVSPEC ioctl.  Note the lookup of the
83262569Simp * bridge interface itself is keyed off the ifdrv structure.
84262569Simp */
85262569Simp#define	BRDGADD			0	/* add bridge member (ifbreq) */
86262569Simp#define	BRDGDEL			1	/* delete bridge member (ifbreq) */
87262569Simp#define	BRDGGIFFLGS		2	/* get member if flags (ifbreq) */
88262569Simp#define	BRDGSIFFLGS		3	/* set member if flags (ifbreq) */
89262569Simp#define	BRDGSCACHE		4	/* set cache size (ifbrparam) */
90262569Simp#define	BRDGGCACHE		5	/* get cache size (ifbrparam) */
91262569Simp#define	BRDGGIFS		6	/* get member list (ifbifconf) */
92262569Simp#define	BRDGRTS			7	/* get address list (ifbaconf) */
93262569Simp#define	BRDGSADDR		8	/* set static address (ifbareq) */
94262569Simp#define	BRDGSTO			9	/* set cache timeout (ifbrparam) */
95262569Simp#define	BRDGGTO			10	/* get cache timeout (ifbrparam) */
96262569Simp#define	BRDGDADDR		11	/* delete address (ifbareq) */
97262569Simp#define	BRDGFLUSH		12	/* flush address cache (ifbreq) */
98262569Simp
99262569Simp#define	BRDGGPRI		13	/* get priority (ifbrparam) */
100262569Simp#define	BRDGSPRI		14	/* set priority (ifbrparam) */
101262569Simp#define	BRDGGHT			15	/* get hello time (ifbrparam) */
102262569Simp#define	BRDGSHT			16	/* set hello time (ifbrparam) */
103262569Simp#define	BRDGGFD			17	/* get forward delay (ifbrparam) */
104262569Simp#define	BRDGSFD			18	/* set forward delay (ifbrparam) */
105262569Simp#define	BRDGGMA			19	/* get max age (ifbrparam) */
106262569Simp#define	BRDGSMA			20	/* set max age (ifbrparam) */
107262569Simp#define	BRDGSIFPRIO		21	/* set if priority (ifbreq) */
108262569Simp#define	BRDGSIFCOST		22	/* set if path cost (ifbreq) */
109262569Simp#define	BRDGADDS		23	/* add bridge span member (ifbreq) */
110262569Simp#define	BRDGDELS		24	/* delete bridge span member (ifbreq) */
111262569Simp#define	BRDGPARAM		25	/* get bridge STP params (ifbropreq) */
112262569Simp#define	BRDGGRTE		26	/* get cache drops (ifbrparam) */
113262569Simp#define	BRDGGIFSSTP		27	/* get member STP params list
114262569Simp					 * (ifbpstpconf) */
115262569Simp#define	BRDGSPROTO		28	/* set protocol (ifbrparam) */
116262569Simp#define	BRDGSTXHC		29	/* set tx hold count (ifbrparam) */
117262569Simp
118262569Simp/*
119262569Simp * Generic bridge control request.
120262569Simp */
121262569Simpstruct ifbreq {
122262569Simp	char		ifbr_ifsname[IFNAMSIZ];	/* member if name */
123262569Simp	uint32_t	ifbr_ifsflags;		/* member if flags */
124262569Simp	uint32_t	ifbr_stpflags;		/* member if STP flags */
125262569Simp	uint32_t	ifbr_path_cost;		/* member if STP cost */
126262569Simp	uint8_t		ifbr_portno;		/* member if port number */
127262569Simp	uint8_t		ifbr_priority;		/* member if STP priority */
128262569Simp	uint8_t		ifbr_proto;		/* member if STP protocol */
129262569Simp	uint8_t		ifbr_role;		/* member if STP role */
130262569Simp	uint8_t		ifbr_state;		/* member if STP state */
131262569Simp};
132262569Simp
133262569Simp/* BRDGGIFFLAGS, BRDGSIFFLAGS */
134262569Simp#define	IFBIF_LEARNING		0x0001	/* if can learn */
135262569Simp#define	IFBIF_DISCOVER		0x0002	/* if sends packets w/ unknown dest. */
136262569Simp#define	IFBIF_STP		0x0004	/* if participates in spanning tree */
137262569Simp#define	IFBIF_SPAN		0x0008	/* if is a span port */
138262569Simp#define	IFBIF_STICKY		0x0010	/* if learned addresses stick */
139262569Simp#define	IFBIF_BSTP_EDGE		0x0020	/* member stp edge port */
140262569Simp#define	IFBIF_BSTP_AUTOEDGE	0x0040	/* member stp autoedge enabled */
141262569Simp#define	IFBIF_BSTP_P2P		0x0080	/* member stp p2p */
142262569Simp#define	IFBIF_BSTP_AUTOP2P	0x0100	/* member stp autop2p enabled */
143262569Simp#define	IFBIF_BSTP_ADMEDGE	0x0200	/* member stp admin edge enabled */
144262569Simp#define	IFBIF_BSTP_ADMCOST	0x0400	/* member stp admin path cost */
145262569Simp
146262569Simp#define	IFBIFBITS	"\020\001LEARNING\002DISCOVER\003STP\004SPAN" \
147262569Simp			"\005STICKY\006EDGE\007AUTOEDGE\010P2P\011AUTOP2P"
148273712Sian#define	IFBIFMASK	~(IFBIF_BSTP_EDGE|IFBIF_BSTP_AUTOEDGE|IFBIF_BSTP_P2P| \
149273712Sian			IFBIF_BSTP_AUTOP2P|IFBIF_BSTP_ADMEDGE| \
150262569Simp			IFBIF_BSTP_ADMCOST)	/* not saved */
151262569Simp
152/* BRDGFLUSH */
153#define	IFBF_FLUSHDYN		0x00	/* flush learned addresses only */
154#define	IFBF_FLUSHALL		0x01	/* flush all addresses */
155
156/*
157 * Interface list structure.
158 */
159struct ifbifconf {
160	uint32_t	ifbic_len;	/* buffer size */
161	union {
162		caddr_t	ifbicu_buf;
163		struct ifbreq *ifbicu_req;
164	} ifbic_ifbicu;
165#define	ifbic_buf	ifbic_ifbicu.ifbicu_buf
166#define	ifbic_req	ifbic_ifbicu.ifbicu_req
167};
168
169/*
170 * Bridge address request.
171 */
172struct ifbareq {
173	char		ifba_ifsname[IFNAMSIZ];	/* member if name */
174	unsigned long	ifba_expire;		/* address expire time */
175	uint8_t		ifba_flags;		/* address flags */
176	uint8_t		ifba_dst[ETHER_ADDR_LEN];/* destination address */
177};
178
179#define	IFBAF_TYPEMASK	0x03	/* address type mask */
180#define	IFBAF_DYNAMIC	0x00	/* dynamically learned address */
181#define	IFBAF_STATIC	0x01	/* static address */
182#define	IFBAF_STICKY	0x02	/* sticky address */
183
184#define	IFBAFBITS	"\020\1STATIC\2STICKY"
185
186/*
187 * Address list structure.
188 */
189struct ifbaconf {
190	uint32_t	ifbac_len;	/* buffer size */
191	union {
192		caddr_t ifbacu_buf;
193		struct ifbareq *ifbacu_req;
194	} ifbac_ifbacu;
195#define	ifbac_buf	ifbac_ifbacu.ifbacu_buf
196#define	ifbac_req	ifbac_ifbacu.ifbacu_req
197};
198
199/*
200 * Bridge parameter structure.
201 */
202struct ifbrparam {
203	union {
204		uint32_t ifbrpu_int32;
205		uint16_t ifbrpu_int16;
206		uint8_t ifbrpu_int8;
207	} ifbrp_ifbrpu;
208};
209#define	ifbrp_csize	ifbrp_ifbrpu.ifbrpu_int32	/* cache size */
210#define	ifbrp_ctime	ifbrp_ifbrpu.ifbrpu_int32	/* cache time (sec) */
211#define	ifbrp_prio	ifbrp_ifbrpu.ifbrpu_int16	/* bridge priority */
212#define	ifbrp_proto	ifbrp_ifbrpu.ifbrpu_int8	/* bridge protocol */
213#define	ifbrp_txhc	ifbrp_ifbrpu.ifbrpu_int8	/* bpdu tx holdcount */
214#define	ifbrp_hellotime	ifbrp_ifbrpu.ifbrpu_int8	/* hello time (sec) */
215#define	ifbrp_fwddelay	ifbrp_ifbrpu.ifbrpu_int8	/* fwd time (sec) */
216#define	ifbrp_maxage	ifbrp_ifbrpu.ifbrpu_int8	/* max age (sec) */
217#define	ifbrp_cexceeded ifbrp_ifbrpu.ifbrpu_int32	/* # of cache dropped
218							 * adresses */
219/*
220 * Bridge current operational parameters structure.
221 */
222struct ifbropreq {
223	uint8_t		ifbop_holdcount;
224	uint8_t		ifbop_maxage;
225	uint8_t		ifbop_hellotime;
226	uint8_t		ifbop_fwddelay;
227	uint8_t		ifbop_protocol;
228	uint16_t	ifbop_priority;
229	uint16_t	ifbop_root_port;
230	uint32_t	ifbop_root_path_cost;
231	uint64_t	ifbop_bridgeid;
232	uint64_t	ifbop_designated_root;
233	uint64_t	ifbop_designated_bridge;
234	struct timeval	ifbop_last_tc_time;
235};
236
237/*
238 * Bridge member operational STP params structure.
239 */
240struct ifbpstpreq {
241	uint8_t		ifbp_portno;		/* bp STP port number */
242	uint32_t	ifbp_fwd_trans;		/* bp STP fwd transitions */
243	uint32_t	ifbp_design_cost;	/* bp STP designated cost */
244	uint32_t	ifbp_design_port;	/* bp STP designated port */
245	uint64_t	ifbp_design_bridge;	/* bp STP designated bridge */
246	uint64_t	ifbp_design_root;	/* bp STP designated root */
247};
248
249/*
250 * Bridge STP ports list structure.
251 */
252struct ifbpstpconf {
253	uint32_t	ifbpstp_len;	/* buffer size */
254	union {
255		caddr_t	ifbpstpu_buf;
256		struct ifbpstpreq *ifbpstpu_req;
257	} ifbpstp_ifbpstpu;
258#define	ifbpstp_buf	ifbpstp_ifbpstpu.ifbpstpu_buf
259#define	ifbpstp_req	ifbpstp_ifbpstpu.ifbpstpu_req
260};
261
262#ifdef _KERNEL
263
264#define BRIDGE_LOCK_INIT(_sc)		do {			\
265	mtx_init(&(_sc)->sc_mtx, "if_bridge", NULL, MTX_DEF);	\
266	cv_init(&(_sc)->sc_cv, "if_bridge_cv");			\
267} while (0)
268#define BRIDGE_LOCK_DESTROY(_sc)	do {	\
269	mtx_destroy(&(_sc)->sc_mtx);		\
270	cv_destroy(&(_sc)->sc_cv);		\
271} while (0)
272#define BRIDGE_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
273#define BRIDGE_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
274#define BRIDGE_LOCKED(_sc)		mtx_owned(&(_sc)->sc_mtx)
275#define BRIDGE_LOCK_ASSERT(_sc)		mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
276#define	BRIDGE_LOCK2REF(_sc, _err)	do {	\
277	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
278	if ((_sc)->sc_iflist_xcnt > 0)		\
279		(_err) = EBUSY;			\
280	else					\
281		(_sc)->sc_iflist_ref++;		\
282	mtx_unlock(&(_sc)->sc_mtx);		\
283} while (0)
284#define	BRIDGE_UNREF(_sc)		do {				\
285	mtx_lock(&(_sc)->sc_mtx);					\
286	(_sc)->sc_iflist_ref--;						\
287	if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0))	\
288		cv_broadcast(&(_sc)->sc_cv);				\
289	mtx_unlock(&(_sc)->sc_mtx);					\
290} while (0)
291#define	BRIDGE_XLOCK(_sc)		do {		\
292	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);		\
293	(_sc)->sc_iflist_xcnt++;			\
294	while ((_sc)->sc_iflist_ref > 0)		\
295		cv_wait(&(_sc)->sc_cv, &(_sc)->sc_mtx);	\
296} while (0)
297#define	BRIDGE_XDROP(_sc)		do {	\
298	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
299	(_sc)->sc_iflist_xcnt--;		\
300} while (0)
301
302#define BRIDGE_INPUT(_ifp, _m)		do {    	\
303	KASSERT(bridge_input_p != NULL,			\
304	    ("%s: if_bridge not loaded!", __func__));	\
305	_m = (*bridge_input_p)(_ifp, _m);		\
306	if (_m != NULL)					\
307		_ifp = _m->m_pkthdr.rcvif;		\
308} while (0)
309
310#define BRIDGE_OUTPUT(_ifp, _m, _err)	do {    		\
311	KASSERT(bridge_output_p != NULL,			\
312	    ("%s: if_bridge not loaded!", __func__));		\
313	_err = (*bridge_output_p)(_ifp, _m, NULL, NULL);	\
314} while (0)
315
316extern	struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
317extern	int (*bridge_output_p)(struct ifnet *, struct mbuf *,
318		struct sockaddr *, struct rtentry *);
319extern	void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
320
321#endif /* _KERNEL */
322