1146985Sthompsa/*	$NetBSD: if_bridgevar.h,v 1.4 2003/07/08 07:13:50 itojun Exp $	*/
2146985Sthompsa
3146985Sthompsa/*
4146985Sthompsa * Copyright 2001 Wasabi Systems, Inc.
5146985Sthompsa * All rights reserved.
6146985Sthompsa *
7146985Sthompsa * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8146985Sthompsa *
9146985Sthompsa * Redistribution and use in source and binary forms, with or without
10146985Sthompsa * modification, are permitted provided that the following conditions
11146985Sthompsa * are met:
12146985Sthompsa * 1. Redistributions of source code must retain the above copyright
13146985Sthompsa *    notice, this list of conditions and the following disclaimer.
14146985Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
15146985Sthompsa *    notice, this list of conditions and the following disclaimer in the
16146985Sthompsa *    documentation and/or other materials provided with the distribution.
17146985Sthompsa * 3. All advertising materials mentioning features or use of this software
18146985Sthompsa *    must display the following acknowledgement:
19146985Sthompsa *	This product includes software developed for the NetBSD Project by
20146985Sthompsa *	Wasabi Systems, Inc.
21146985Sthompsa * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22146985Sthompsa *    or promote products derived from this software without specific prior
23146985Sthompsa *    written permission.
24146985Sthompsa *
25146985Sthompsa * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26146985Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27146985Sthompsa * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28146985Sthompsa * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29146985Sthompsa * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30146985Sthompsa * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31146985Sthompsa * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32146985Sthompsa * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33146985Sthompsa * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34146985Sthompsa * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35146985Sthompsa * POSSIBILITY OF SUCH DAMAGE.
36146985Sthompsa */
37146985Sthompsa
38146985Sthompsa/*
39146985Sthompsa * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
40146985Sthompsa * All rights reserved.
41146985Sthompsa *
42146985Sthompsa * Redistribution and use in source and binary forms, with or without
43146985Sthompsa * modification, are permitted provided that the following conditions
44146985Sthompsa * are met:
45146985Sthompsa * 1. Redistributions of source code must retain the above copyright
46146985Sthompsa *    notice, this list of conditions and the following disclaimer.
47146985Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
48146985Sthompsa *    notice, this list of conditions and the following disclaimer in the
49146985Sthompsa *    documentation and/or other materials provided with the distribution.
50146985Sthompsa * 3. All advertising materials mentioning features or use of this software
51146985Sthompsa *    must display the following acknowledgement:
52146985Sthompsa *	This product includes software developed by Jason L. Wright
53146985Sthompsa * 4. The name of the author may not be used to endorse or promote products
54146985Sthompsa *    derived from this software without specific prior written permission.
55146985Sthompsa *
56146985Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57146985Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
58146985Sthompsa * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
59146985Sthompsa * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
60146985Sthompsa * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
61146985Sthompsa * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
62146985Sthompsa * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63146985Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64146985Sthompsa * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
65146985Sthompsa * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66146985Sthompsa * POSSIBILITY OF SUCH DAMAGE.
67146985Sthompsa *
68146985Sthompsa * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp
69146985Sthompsa *
70146985Sthompsa * $FreeBSD: stable/10/sys/net/if_bridgevar.h 313066 2017-02-01 21:44:50Z kp $
71146985Sthompsa */
72146985Sthompsa
73146985Sthompsa/*
74146985Sthompsa * Data structure and control definitions for bridge interfaces.
75146985Sthompsa */
76146985Sthompsa
77146985Sthompsa#include <sys/callout.h>
78146985Sthompsa#include <sys/queue.h>
79151305Sthompsa#include <sys/condvar.h>
80146985Sthompsa
81146985Sthompsa/*
82146985Sthompsa * Commands used in the SIOCSDRVSPEC ioctl.  Note the lookup of the
83146985Sthompsa * bridge interface itself is keyed off the ifdrv structure.
84146985Sthompsa */
85146985Sthompsa#define	BRDGADD			0	/* add bridge member (ifbreq) */
86146985Sthompsa#define	BRDGDEL			1	/* delete bridge member (ifbreq) */
87146985Sthompsa#define	BRDGGIFFLGS		2	/* get member if flags (ifbreq) */
88146985Sthompsa#define	BRDGSIFFLGS		3	/* set member if flags (ifbreq) */
89146985Sthompsa#define	BRDGSCACHE		4	/* set cache size (ifbrparam) */
90146985Sthompsa#define	BRDGGCACHE		5	/* get cache size (ifbrparam) */
91146985Sthompsa#define	BRDGGIFS		6	/* get member list (ifbifconf) */
92146985Sthompsa#define	BRDGRTS			7	/* get address list (ifbaconf) */
93146985Sthompsa#define	BRDGSADDR		8	/* set static address (ifbareq) */
94146985Sthompsa#define	BRDGSTO			9	/* set cache timeout (ifbrparam) */
95146985Sthompsa#define	BRDGGTO			10	/* get cache timeout (ifbrparam) */
96146985Sthompsa#define	BRDGDADDR		11	/* delete address (ifbareq) */
97146985Sthompsa#define	BRDGFLUSH		12	/* flush address cache (ifbreq) */
98146985Sthompsa
99146985Sthompsa#define	BRDGGPRI		13	/* get priority (ifbrparam) */
100146985Sthompsa#define	BRDGSPRI		14	/* set priority (ifbrparam) */
101146985Sthompsa#define	BRDGGHT			15	/* get hello time (ifbrparam) */
102146985Sthompsa#define	BRDGSHT			16	/* set hello time (ifbrparam) */
103146985Sthompsa#define	BRDGGFD			17	/* get forward delay (ifbrparam) */
104146985Sthompsa#define	BRDGSFD			18	/* set forward delay (ifbrparam) */
105146985Sthompsa#define	BRDGGMA			19	/* get max age (ifbrparam) */
106146985Sthompsa#define	BRDGSMA			20	/* set max age (ifbrparam) */
107146985Sthompsa#define	BRDGSIFPRIO		21	/* set if priority (ifbreq) */
108153967Sthompsa#define	BRDGSIFCOST		22	/* set if path cost (ifbreq) */
109153408Sthompsa#define	BRDGADDS		23	/* add bridge span member (ifbreq) */
110153408Sthompsa#define	BRDGDELS		24	/* delete bridge span member (ifbreq) */
111160867Sthompsa#define	BRDGPARAM		25	/* get bridge STP params (ifbropreq) */
112160867Sthompsa#define	BRDGGRTE		26	/* get cache drops (ifbrparam) */
113160867Sthompsa#define	BRDGGIFSSTP		27	/* get member STP params list
114160867Sthompsa					 * (ifbpstpconf) */
115163863Sthompsa#define	BRDGSPROTO		28	/* set protocol (ifbrparam) */
116163863Sthompsa#define	BRDGSTXHC		29	/* set tx hold count (ifbrparam) */
117173320Sthompsa#define	BRDGSIFAMAX		30	/* set max interface addrs (ifbreq) */
118146985Sthompsa
119146985Sthompsa/*
120146985Sthompsa * Generic bridge control request.
121146985Sthompsa */
122146985Sthompsastruct ifbreq {
123146985Sthompsa	char		ifbr_ifsname[IFNAMSIZ];	/* member if name */
124146985Sthompsa	uint32_t	ifbr_ifsflags;		/* member if flags */
125163863Sthompsa	uint32_t	ifbr_stpflags;		/* member if STP flags */
126163863Sthompsa	uint32_t	ifbr_path_cost;		/* member if STP cost */
127163863Sthompsa	uint8_t		ifbr_portno;		/* member if port number */
128163863Sthompsa	uint8_t		ifbr_priority;		/* member if STP priority */
129163863Sthompsa	uint8_t		ifbr_proto;		/* member if STP protocol */
130163863Sthompsa	uint8_t		ifbr_role;		/* member if STP role */
131146985Sthompsa	uint8_t		ifbr_state;		/* member if STP state */
132173320Sthompsa	uint32_t	ifbr_addrcnt;		/* member if addr number */
133173320Sthompsa	uint32_t	ifbr_addrmax;		/* member if addr max */
134173320Sthompsa	uint32_t	ifbr_addrexceeded;	/* member if addr violations */
135173320Sthompsa	uint8_t		pad[32];
136146985Sthompsa};
137146985Sthompsa
138146985Sthompsa/* BRDGGIFFLAGS, BRDGSIFFLAGS */
139164653Sthompsa#define	IFBIF_LEARNING		0x0001	/* if can learn */
140164653Sthompsa#define	IFBIF_DISCOVER		0x0002	/* if sends packets w/ unknown dest. */
141164653Sthompsa#define	IFBIF_STP		0x0004	/* if participates in spanning tree */
142164653Sthompsa#define	IFBIF_SPAN		0x0008	/* if is a span port */
143164653Sthompsa#define	IFBIF_STICKY		0x0010	/* if learned addresses stick */
144164653Sthompsa#define	IFBIF_BSTP_EDGE		0x0020	/* member stp edge port */
145164653Sthompsa#define	IFBIF_BSTP_AUTOEDGE	0x0040	/* member stp autoedge enabled */
146165105Sthompsa#define	IFBIF_BSTP_PTP		0x0080	/* member stp point to point */
147165105Sthompsa#define	IFBIF_BSTP_AUTOPTP	0x0100	/* member stp autoptp enabled */
148164880Ssyrinx#define	IFBIF_BSTP_ADMEDGE	0x0200	/* member stp admin edge enabled */
149164880Ssyrinx#define	IFBIF_BSTP_ADMCOST	0x0400	/* member stp admin path cost */
150171678Sthompsa#define	IFBIF_PRIVATE		0x0800	/* if is a private segment */
151146985Sthompsa
152164653Sthompsa#define	IFBIFBITS	"\020\001LEARNING\002DISCOVER\003STP\004SPAN" \
153171678Sthompsa			"\005STICKY\014PRIVATE\006EDGE\007AUTOEDGE\010PTP" \
154171678Sthompsa			"\011AUTOPTP"
155165105Sthompsa#define	IFBIFMASK	~(IFBIF_BSTP_EDGE|IFBIF_BSTP_AUTOEDGE|IFBIF_BSTP_PTP| \
156165105Sthompsa			IFBIF_BSTP_AUTOPTP|IFBIF_BSTP_ADMEDGE| \
157164880Ssyrinx			IFBIF_BSTP_ADMCOST)	/* not saved */
158146985Sthompsa
159146985Sthompsa/* BRDGFLUSH */
160146985Sthompsa#define	IFBF_FLUSHDYN		0x00	/* flush learned addresses only */
161146985Sthompsa#define	IFBF_FLUSHALL		0x01	/* flush all addresses */
162146985Sthompsa
163146985Sthompsa/*
164146985Sthompsa * Interface list structure.
165146985Sthompsa */
166146985Sthompsastruct ifbifconf {
167146985Sthompsa	uint32_t	ifbic_len;	/* buffer size */
168146985Sthompsa	union {
169146985Sthompsa		caddr_t	ifbicu_buf;
170146985Sthompsa		struct ifbreq *ifbicu_req;
171146985Sthompsa	} ifbic_ifbicu;
172146985Sthompsa#define	ifbic_buf	ifbic_ifbicu.ifbicu_buf
173146985Sthompsa#define	ifbic_req	ifbic_ifbicu.ifbicu_req
174146985Sthompsa};
175146985Sthompsa
176146985Sthompsa/*
177146985Sthompsa * Bridge address request.
178146985Sthompsa */
179146985Sthompsastruct ifbareq {
180146985Sthompsa	char		ifba_ifsname[IFNAMSIZ];	/* member if name */
181146985Sthompsa	unsigned long	ifba_expire;		/* address expire time */
182146985Sthompsa	uint8_t		ifba_flags;		/* address flags */
183146985Sthompsa	uint8_t		ifba_dst[ETHER_ADDR_LEN];/* destination address */
184170681Sthompsa	uint16_t	ifba_vlan;		/* vlan id */
185146985Sthompsa};
186146985Sthompsa
187146985Sthompsa#define	IFBAF_TYPEMASK	0x03	/* address type mask */
188146985Sthompsa#define	IFBAF_DYNAMIC	0x00	/* dynamically learned address */
189146985Sthompsa#define	IFBAF_STATIC	0x01	/* static address */
190164112Sthompsa#define	IFBAF_STICKY	0x02	/* sticky address */
191146985Sthompsa
192164112Sthompsa#define	IFBAFBITS	"\020\1STATIC\2STICKY"
193146985Sthompsa
194146985Sthompsa/*
195146985Sthompsa * Address list structure.
196146985Sthompsa */
197146985Sthompsastruct ifbaconf {
198146985Sthompsa	uint32_t	ifbac_len;	/* buffer size */
199146985Sthompsa	union {
200146985Sthompsa		caddr_t ifbacu_buf;
201146985Sthompsa		struct ifbareq *ifbacu_req;
202146985Sthompsa	} ifbac_ifbacu;
203146985Sthompsa#define	ifbac_buf	ifbac_ifbacu.ifbacu_buf
204146985Sthompsa#define	ifbac_req	ifbac_ifbacu.ifbacu_req
205146985Sthompsa};
206146985Sthompsa
207146985Sthompsa/*
208146985Sthompsa * Bridge parameter structure.
209146985Sthompsa */
210146985Sthompsastruct ifbrparam {
211146985Sthompsa	union {
212146985Sthompsa		uint32_t ifbrpu_int32;
213146985Sthompsa		uint16_t ifbrpu_int16;
214146985Sthompsa		uint8_t ifbrpu_int8;
215146985Sthompsa	} ifbrp_ifbrpu;
216146985Sthompsa};
217146985Sthompsa#define	ifbrp_csize	ifbrp_ifbrpu.ifbrpu_int32	/* cache size */
218146985Sthompsa#define	ifbrp_ctime	ifbrp_ifbrpu.ifbrpu_int32	/* cache time (sec) */
219146985Sthompsa#define	ifbrp_prio	ifbrp_ifbrpu.ifbrpu_int16	/* bridge priority */
220163863Sthompsa#define	ifbrp_proto	ifbrp_ifbrpu.ifbrpu_int8	/* bridge protocol */
221163863Sthompsa#define	ifbrp_txhc	ifbrp_ifbrpu.ifbrpu_int8	/* bpdu tx holdcount */
222146985Sthompsa#define	ifbrp_hellotime	ifbrp_ifbrpu.ifbrpu_int8	/* hello time (sec) */
223146985Sthompsa#define	ifbrp_fwddelay	ifbrp_ifbrpu.ifbrpu_int8	/* fwd time (sec) */
224146985Sthompsa#define	ifbrp_maxage	ifbrp_ifbrpu.ifbrpu_int8	/* max age (sec) */
225160867Sthompsa#define	ifbrp_cexceeded ifbrp_ifbrpu.ifbrpu_int32	/* # of cache dropped
226160867Sthompsa							 * adresses */
227160867Sthompsa/*
228160867Sthompsa * Bridge current operational parameters structure.
229160867Sthompsa */
230160867Sthompsastruct ifbropreq {
231163863Sthompsa	uint8_t		ifbop_holdcount;
232160867Sthompsa	uint8_t		ifbop_maxage;
233160867Sthompsa	uint8_t		ifbop_hellotime;
234160867Sthompsa	uint8_t		ifbop_fwddelay;
235163863Sthompsa	uint8_t		ifbop_protocol;
236163863Sthompsa	uint16_t	ifbop_priority;
237160867Sthompsa	uint16_t	ifbop_root_port;
238160867Sthompsa	uint32_t	ifbop_root_path_cost;
239164653Sthompsa	uint64_t	ifbop_bridgeid;
240160867Sthompsa	uint64_t	ifbop_designated_root;
241164653Sthompsa	uint64_t	ifbop_designated_bridge;
242160867Sthompsa	struct timeval	ifbop_last_tc_time;
243160867Sthompsa};
244146985Sthompsa
245160867Sthompsa/*
246160867Sthompsa * Bridge member operational STP params structure.
247160867Sthompsa */
248160867Sthompsastruct ifbpstpreq {
249160867Sthompsa	uint8_t		ifbp_portno;		/* bp STP port number */
250160867Sthompsa	uint32_t	ifbp_fwd_trans;		/* bp STP fwd transitions */
251160867Sthompsa	uint32_t	ifbp_design_cost;	/* bp STP designated cost */
252160867Sthompsa	uint32_t	ifbp_design_port;	/* bp STP designated port */
253160867Sthompsa	uint64_t	ifbp_design_bridge;	/* bp STP designated bridge */
254160867Sthompsa	uint64_t	ifbp_design_root;	/* bp STP designated root */
255160867Sthompsa};
256160867Sthompsa
257160867Sthompsa/*
258160867Sthompsa * Bridge STP ports list structure.
259160867Sthompsa */
260160867Sthompsastruct ifbpstpconf {
261160867Sthompsa	uint32_t	ifbpstp_len;	/* buffer size */
262160867Sthompsa	union {
263160867Sthompsa		caddr_t	ifbpstpu_buf;
264160867Sthompsa		struct ifbpstpreq *ifbpstpu_req;
265160867Sthompsa	} ifbpstp_ifbpstpu;
266160867Sthompsa#define	ifbpstp_buf	ifbpstp_ifbpstpu.ifbpstpu_buf
267160867Sthompsa#define	ifbpstp_req	ifbpstp_ifbpstpu.ifbpstpu_req
268160867Sthompsa};
269160867Sthompsa
270146985Sthompsa#ifdef _KERNEL
271146985Sthompsa
272146985Sthompsa#define BRIDGE_LOCK_INIT(_sc)		do {			\
273146985Sthompsa	mtx_init(&(_sc)->sc_mtx, "if_bridge", NULL, MTX_DEF);	\
274146985Sthompsa	cv_init(&(_sc)->sc_cv, "if_bridge_cv");			\
275146985Sthompsa} while (0)
276146985Sthompsa#define BRIDGE_LOCK_DESTROY(_sc)	do {	\
277146985Sthompsa	mtx_destroy(&(_sc)->sc_mtx);		\
278146985Sthompsa	cv_destroy(&(_sc)->sc_cv);		\
279146985Sthompsa} while (0)
280146985Sthompsa#define BRIDGE_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
281146985Sthompsa#define BRIDGE_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
282146985Sthompsa#define BRIDGE_LOCK_ASSERT(_sc)		mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
283313066Skp#define BRIDGE_UNLOCK_ASSERT(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED)
284146985Sthompsa#define	BRIDGE_LOCK2REF(_sc, _err)	do {	\
285146985Sthompsa	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
286146985Sthompsa	if ((_sc)->sc_iflist_xcnt > 0)		\
287146985Sthompsa		(_err) = EBUSY;			\
288146985Sthompsa	else					\
289146985Sthompsa		(_sc)->sc_iflist_ref++;		\
290146985Sthompsa	mtx_unlock(&(_sc)->sc_mtx);		\
291146985Sthompsa} while (0)
292146985Sthompsa#define	BRIDGE_UNREF(_sc)		do {				\
293146985Sthompsa	mtx_lock(&(_sc)->sc_mtx);					\
294146985Sthompsa	(_sc)->sc_iflist_ref--;						\
295146985Sthompsa	if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0))	\
296146985Sthompsa		cv_broadcast(&(_sc)->sc_cv);				\
297146985Sthompsa	mtx_unlock(&(_sc)->sc_mtx);					\
298146985Sthompsa} while (0)
299146985Sthompsa#define	BRIDGE_XLOCK(_sc)		do {		\
300146985Sthompsa	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);		\
301146985Sthompsa	(_sc)->sc_iflist_xcnt++;			\
302146985Sthompsa	while ((_sc)->sc_iflist_ref > 0)		\
303146985Sthompsa		cv_wait(&(_sc)->sc_cv, &(_sc)->sc_mtx);	\
304146985Sthompsa} while (0)
305146985Sthompsa#define	BRIDGE_XDROP(_sc)		do {	\
306146985Sthompsa	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
307146985Sthompsa	(_sc)->sc_iflist_xcnt--;		\
308146985Sthompsa} while (0)
309146985Sthompsa
310151305Sthompsa#define BRIDGE_INPUT(_ifp, _m)		do {    	\
311151305Sthompsa	KASSERT(bridge_input_p != NULL,			\
312151305Sthompsa	    ("%s: if_bridge not loaded!", __func__));	\
313151305Sthompsa	_m = (*bridge_input_p)(_ifp, _m);		\
314151305Sthompsa	if (_m != NULL)					\
315151305Sthompsa		_ifp = _m->m_pkthdr.rcvif;		\
316151305Sthompsa} while (0)
317151305Sthompsa
318151305Sthompsa#define BRIDGE_OUTPUT(_ifp, _m, _err)	do {    		\
319151305Sthompsa	KASSERT(bridge_output_p != NULL,			\
320151305Sthompsa	    ("%s: if_bridge not loaded!", __func__));		\
321151305Sthompsa	_err = (*bridge_output_p)(_ifp, _m, NULL, NULL);	\
322151305Sthompsa} while (0)
323151305Sthompsa
324151305Sthompsaextern	struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
325151305Sthompsaextern	int (*bridge_output_p)(struct ifnet *, struct mbuf *,
326151305Sthompsa		struct sockaddr *, struct rtentry *);
327151305Sthompsaextern	void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
328146985Sthompsa
329146985Sthompsa#endif /* _KERNEL */
330