if_bridgevar.h revision 170681
1/*	$NetBSD: if_bridgevar.h,v 1.4 2003/07/08 07:13:50 itojun Exp $	*/
2
3/*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed for the NetBSD Project by
20 *	Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 *    or promote products derived from this software without specific prior
23 *    written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
40 * All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 *    notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 *    notice, this list of conditions and the following disclaimer in the
49 *    documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 *    must display the following acknowledgement:
52 *	This product includes software developed by Jason L. Wright
53 * 4. The name of the author may not be used to endorse or promote products
54 *    derived from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
58 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
59 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
60 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
61 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
62 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
65 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 *
68 * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp
69 *
70 * $FreeBSD: head/sys/net/if_bridgevar.h 170681 2007-06-13 18:58:04Z thompsa $
71 */
72
73/*
74 * Data structure and control definitions for bridge interfaces.
75 */
76
77#include <sys/callout.h>
78#include <sys/queue.h>
79#include <sys/condvar.h>
80
81/*
82 * Commands used in the SIOCSDRVSPEC ioctl.  Note the lookup of the
83 * bridge interface itself is keyed off the ifdrv structure.
84 */
85#define	BRDGADD			0	/* add bridge member (ifbreq) */
86#define	BRDGDEL			1	/* delete bridge member (ifbreq) */
87#define	BRDGGIFFLGS		2	/* get member if flags (ifbreq) */
88#define	BRDGSIFFLGS		3	/* set member if flags (ifbreq) */
89#define	BRDGSCACHE		4	/* set cache size (ifbrparam) */
90#define	BRDGGCACHE		5	/* get cache size (ifbrparam) */
91#define	BRDGGIFS		6	/* get member list (ifbifconf) */
92#define	BRDGRTS			7	/* get address list (ifbaconf) */
93#define	BRDGSADDR		8	/* set static address (ifbareq) */
94#define	BRDGSTO			9	/* set cache timeout (ifbrparam) */
95#define	BRDGGTO			10	/* get cache timeout (ifbrparam) */
96#define	BRDGDADDR		11	/* delete address (ifbareq) */
97#define	BRDGFLUSH		12	/* flush address cache (ifbreq) */
98
99#define	BRDGGPRI		13	/* get priority (ifbrparam) */
100#define	BRDGSPRI		14	/* set priority (ifbrparam) */
101#define	BRDGGHT			15	/* get hello time (ifbrparam) */
102#define	BRDGSHT			16	/* set hello time (ifbrparam) */
103#define	BRDGGFD			17	/* get forward delay (ifbrparam) */
104#define	BRDGSFD			18	/* set forward delay (ifbrparam) */
105#define	BRDGGMA			19	/* get max age (ifbrparam) */
106#define	BRDGSMA			20	/* set max age (ifbrparam) */
107#define	BRDGSIFPRIO		21	/* set if priority (ifbreq) */
108#define	BRDGSIFCOST		22	/* set if path cost (ifbreq) */
109#define	BRDGADDS		23	/* add bridge span member (ifbreq) */
110#define	BRDGDELS		24	/* delete bridge span member (ifbreq) */
111#define	BRDGPARAM		25	/* get bridge STP params (ifbropreq) */
112#define	BRDGGRTE		26	/* get cache drops (ifbrparam) */
113#define	BRDGGIFSSTP		27	/* get member STP params list
114					 * (ifbpstpconf) */
115#define	BRDGSPROTO		28	/* set protocol (ifbrparam) */
116#define	BRDGSTXHC		29	/* set tx hold count (ifbrparam) */
117
118/*
119 * Generic bridge control request.
120 */
121struct ifbreq {
122	char		ifbr_ifsname[IFNAMSIZ];	/* member if name */
123	uint32_t	ifbr_ifsflags;		/* member if flags */
124	uint32_t	ifbr_stpflags;		/* member if STP flags */
125	uint32_t	ifbr_path_cost;		/* member if STP cost */
126	uint8_t		ifbr_portno;		/* member if port number */
127	uint8_t		ifbr_priority;		/* member if STP priority */
128	uint8_t		ifbr_proto;		/* member if STP protocol */
129	uint8_t		ifbr_role;		/* member if STP role */
130	uint8_t		ifbr_state;		/* member if STP state */
131};
132
133/* BRDGGIFFLAGS, BRDGSIFFLAGS */
134#define	IFBIF_LEARNING		0x0001	/* if can learn */
135#define	IFBIF_DISCOVER		0x0002	/* if sends packets w/ unknown dest. */
136#define	IFBIF_STP		0x0004	/* if participates in spanning tree */
137#define	IFBIF_SPAN		0x0008	/* if is a span port */
138#define	IFBIF_STICKY		0x0010	/* if learned addresses stick */
139#define	IFBIF_BSTP_EDGE		0x0020	/* member stp edge port */
140#define	IFBIF_BSTP_AUTOEDGE	0x0040	/* member stp autoedge enabled */
141#define	IFBIF_BSTP_PTP		0x0080	/* member stp point to point */
142#define	IFBIF_BSTP_AUTOPTP	0x0100	/* member stp autoptp enabled */
143#define	IFBIF_BSTP_ADMEDGE	0x0200	/* member stp admin edge enabled */
144#define	IFBIF_BSTP_ADMCOST	0x0400	/* member stp admin path cost */
145
146#define	IFBIFBITS	"\020\001LEARNING\002DISCOVER\003STP\004SPAN" \
147			"\005STICKY\006EDGE\007AUTOEDGE\010PTP\011AUTOPTP"
148#define	IFBIFMASK	~(IFBIF_BSTP_EDGE|IFBIF_BSTP_AUTOEDGE|IFBIF_BSTP_PTP| \
149			IFBIF_BSTP_AUTOPTP|IFBIF_BSTP_ADMEDGE| \
150			IFBIF_BSTP_ADMCOST)	/* not saved */
151
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	uint16_t	ifba_vlan;		/* vlan id */
178};
179
180#define	IFBAF_TYPEMASK	0x03	/* address type mask */
181#define	IFBAF_DYNAMIC	0x00	/* dynamically learned address */
182#define	IFBAF_STATIC	0x01	/* static address */
183#define	IFBAF_STICKY	0x02	/* sticky address */
184
185#define	IFBAFBITS	"\020\1STATIC\2STICKY"
186
187/*
188 * Address list structure.
189 */
190struct ifbaconf {
191	uint32_t	ifbac_len;	/* buffer size */
192	union {
193		caddr_t ifbacu_buf;
194		struct ifbareq *ifbacu_req;
195	} ifbac_ifbacu;
196#define	ifbac_buf	ifbac_ifbacu.ifbacu_buf
197#define	ifbac_req	ifbac_ifbacu.ifbacu_req
198};
199
200/*
201 * Bridge parameter structure.
202 */
203struct ifbrparam {
204	union {
205		uint32_t ifbrpu_int32;
206		uint16_t ifbrpu_int16;
207		uint8_t ifbrpu_int8;
208	} ifbrp_ifbrpu;
209};
210#define	ifbrp_csize	ifbrp_ifbrpu.ifbrpu_int32	/* cache size */
211#define	ifbrp_ctime	ifbrp_ifbrpu.ifbrpu_int32	/* cache time (sec) */
212#define	ifbrp_prio	ifbrp_ifbrpu.ifbrpu_int16	/* bridge priority */
213#define	ifbrp_proto	ifbrp_ifbrpu.ifbrpu_int8	/* bridge protocol */
214#define	ifbrp_txhc	ifbrp_ifbrpu.ifbrpu_int8	/* bpdu tx holdcount */
215#define	ifbrp_hellotime	ifbrp_ifbrpu.ifbrpu_int8	/* hello time (sec) */
216#define	ifbrp_fwddelay	ifbrp_ifbrpu.ifbrpu_int8	/* fwd time (sec) */
217#define	ifbrp_maxage	ifbrp_ifbrpu.ifbrpu_int8	/* max age (sec) */
218#define	ifbrp_cexceeded ifbrp_ifbrpu.ifbrpu_int32	/* # of cache dropped
219							 * adresses */
220/*
221 * Bridge current operational parameters structure.
222 */
223struct ifbropreq {
224	uint8_t		ifbop_holdcount;
225	uint8_t		ifbop_maxage;
226	uint8_t		ifbop_hellotime;
227	uint8_t		ifbop_fwddelay;
228	uint8_t		ifbop_protocol;
229	uint16_t	ifbop_priority;
230	uint16_t	ifbop_root_port;
231	uint32_t	ifbop_root_path_cost;
232	uint64_t	ifbop_bridgeid;
233	uint64_t	ifbop_designated_root;
234	uint64_t	ifbop_designated_bridge;
235	struct timeval	ifbop_last_tc_time;
236};
237
238/*
239 * Bridge member operational STP params structure.
240 */
241struct ifbpstpreq {
242	uint8_t		ifbp_portno;		/* bp STP port number */
243	uint32_t	ifbp_fwd_trans;		/* bp STP fwd transitions */
244	uint32_t	ifbp_design_cost;	/* bp STP designated cost */
245	uint32_t	ifbp_design_port;	/* bp STP designated port */
246	uint64_t	ifbp_design_bridge;	/* bp STP designated bridge */
247	uint64_t	ifbp_design_root;	/* bp STP designated root */
248};
249
250/*
251 * Bridge STP ports list structure.
252 */
253struct ifbpstpconf {
254	uint32_t	ifbpstp_len;	/* buffer size */
255	union {
256		caddr_t	ifbpstpu_buf;
257		struct ifbpstpreq *ifbpstpu_req;
258	} ifbpstp_ifbpstpu;
259#define	ifbpstp_buf	ifbpstp_ifbpstpu.ifbpstpu_buf
260#define	ifbpstp_req	ifbpstp_ifbpstpu.ifbpstpu_req
261};
262
263#ifdef _KERNEL
264
265#define BRIDGE_LOCK_INIT(_sc)		do {			\
266	mtx_init(&(_sc)->sc_mtx, "if_bridge", NULL, MTX_DEF);	\
267	cv_init(&(_sc)->sc_cv, "if_bridge_cv");			\
268} while (0)
269#define BRIDGE_LOCK_DESTROY(_sc)	do {	\
270	mtx_destroy(&(_sc)->sc_mtx);		\
271	cv_destroy(&(_sc)->sc_cv);		\
272} while (0)
273#define BRIDGE_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
274#define BRIDGE_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
275#define BRIDGE_LOCKED(_sc)		mtx_owned(&(_sc)->sc_mtx)
276#define BRIDGE_LOCK_ASSERT(_sc)		mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
277#define	BRIDGE_LOCK2REF(_sc, _err)	do {	\
278	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
279	if ((_sc)->sc_iflist_xcnt > 0)		\
280		(_err) = EBUSY;			\
281	else					\
282		(_sc)->sc_iflist_ref++;		\
283	mtx_unlock(&(_sc)->sc_mtx);		\
284} while (0)
285#define	BRIDGE_UNREF(_sc)		do {				\
286	mtx_lock(&(_sc)->sc_mtx);					\
287	(_sc)->sc_iflist_ref--;						\
288	if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0))	\
289		cv_broadcast(&(_sc)->sc_cv);				\
290	mtx_unlock(&(_sc)->sc_mtx);					\
291} while (0)
292#define	BRIDGE_XLOCK(_sc)		do {		\
293	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);		\
294	(_sc)->sc_iflist_xcnt++;			\
295	while ((_sc)->sc_iflist_ref > 0)		\
296		cv_wait(&(_sc)->sc_cv, &(_sc)->sc_mtx);	\
297} while (0)
298#define	BRIDGE_XDROP(_sc)		do {	\
299	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
300	(_sc)->sc_iflist_xcnt--;		\
301} while (0)
302
303#define BRIDGE_INPUT(_ifp, _m)		do {    	\
304	KASSERT(bridge_input_p != NULL,			\
305	    ("%s: if_bridge not loaded!", __func__));	\
306	_m = (*bridge_input_p)(_ifp, _m);		\
307	if (_m != NULL)					\
308		_ifp = _m->m_pkthdr.rcvif;		\
309} while (0)
310
311#define BRIDGE_OUTPUT(_ifp, _m, _err)	do {    		\
312	KASSERT(bridge_output_p != NULL,			\
313	    ("%s: if_bridge not loaded!", __func__));		\
314	_err = (*bridge_output_p)(_ifp, _m, NULL, NULL);	\
315} while (0)
316
317extern	struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
318extern	int (*bridge_output_p)(struct ifnet *, struct mbuf *,
319		struct sockaddr *, struct rtentry *);
320extern	void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
321
322#endif /* _KERNEL */
323