xref: /freebsd/sys/net/if_bridgevar.h (revision 3642298923e528d795e3a30ec165d2b469e28b40)
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$
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 
80 /*
81  * Commands used in the SIOCSDRVSPEC ioctl.  Note the lookup of the
82  * bridge interface itself is keyed off the ifdrv structure.
83  */
84 #define	BRDGADD			0	/* add bridge member (ifbreq) */
85 #define	BRDGDEL			1	/* delete bridge member (ifbreq) */
86 #define	BRDGGIFFLGS		2	/* get member if flags (ifbreq) */
87 #define	BRDGSIFFLGS		3	/* set member if flags (ifbreq) */
88 #define	BRDGSCACHE		4	/* set cache size (ifbrparam) */
89 #define	BRDGGCACHE		5	/* get cache size (ifbrparam) */
90 #define	BRDGGIFS		6	/* get member list (ifbifconf) */
91 #define	BRDGRTS			7	/* get address list (ifbaconf) */
92 #define	BRDGSADDR		8	/* set static address (ifbareq) */
93 #define	BRDGSTO			9	/* set cache timeout (ifbrparam) */
94 #define	BRDGGTO			10	/* get cache timeout (ifbrparam) */
95 #define	BRDGDADDR		11	/* delete address (ifbareq) */
96 #define	BRDGFLUSH		12	/* flush address cache (ifbreq) */
97 
98 #define	BRDGGPRI		13	/* get priority (ifbrparam) */
99 #define	BRDGSPRI		14	/* set priority (ifbrparam) */
100 #define	BRDGGHT			15	/* get hello time (ifbrparam) */
101 #define	BRDGSHT			16	/* set hello time (ifbrparam) */
102 #define	BRDGGFD			17	/* get forward delay (ifbrparam) */
103 #define	BRDGSFD			18	/* set forward delay (ifbrparam) */
104 #define	BRDGGMA			19	/* get max age (ifbrparam) */
105 #define	BRDGSMA			20	/* set max age (ifbrparam) */
106 #define	BRDGSIFPRIO		21	/* set if priority (ifbreq) */
107 #define BRDGSIFCOST		22	/* set if path cost (ifbreq) */
108 
109 /*
110  * Generic bridge control request.
111  */
112 struct ifbreq {
113 	char		ifbr_ifsname[IFNAMSIZ];	/* member if name */
114 	uint32_t	ifbr_ifsflags;		/* member if flags */
115 	uint8_t		ifbr_state;		/* member if STP state */
116 	uint8_t		ifbr_priority;		/* member if STP priority */
117 	uint8_t		ifbr_path_cost;		/* member if STP cost */
118 	uint8_t		ifbr_portno;		/* member if port number */
119 };
120 
121 /* BRDGGIFFLAGS, BRDGSIFFLAGS */
122 #define	IFBIF_LEARNING		0x01	/* if can learn */
123 #define	IFBIF_DISCOVER		0x02	/* if sends packets w/ unknown dest. */
124 #define	IFBIF_STP		0x04	/* if participates in spanning tree */
125 
126 #define	IFBIFBITS	"\020\1LEARNING\2DISCOVER\3STP"
127 
128 /* BRDGFLUSH */
129 #define	IFBF_FLUSHDYN		0x00	/* flush learned addresses only */
130 #define	IFBF_FLUSHALL		0x01	/* flush all addresses */
131 
132 /* STP port states */
133 #define	BSTP_IFSTATE_DISABLED	0
134 #define	BSTP_IFSTATE_LISTENING	1
135 #define	BSTP_IFSTATE_LEARNING	2
136 #define	BSTP_IFSTATE_FORWARDING	3
137 #define	BSTP_IFSTATE_BLOCKING	4
138 
139 /*
140  * Interface list structure.
141  */
142 struct ifbifconf {
143 	uint32_t	ifbic_len;	/* buffer size */
144 	union {
145 		caddr_t	ifbicu_buf;
146 		struct ifbreq *ifbicu_req;
147 	} ifbic_ifbicu;
148 #define	ifbic_buf	ifbic_ifbicu.ifbicu_buf
149 #define	ifbic_req	ifbic_ifbicu.ifbicu_req
150 };
151 
152 /*
153  * Bridge address request.
154  */
155 struct ifbareq {
156 	char		ifba_ifsname[IFNAMSIZ];	/* member if name */
157 	unsigned long	ifba_expire;		/* address expire time */
158 	uint8_t		ifba_flags;		/* address flags */
159 	uint8_t		ifba_dst[ETHER_ADDR_LEN];/* destination address */
160 };
161 
162 #define	IFBAF_TYPEMASK	0x03	/* address type mask */
163 #define	IFBAF_DYNAMIC	0x00	/* dynamically learned address */
164 #define	IFBAF_STATIC	0x01	/* static address */
165 
166 #define	IFBAFBITS	"\020\1STATIC"
167 
168 /*
169  * Address list structure.
170  */
171 struct ifbaconf {
172 	uint32_t	ifbac_len;	/* buffer size */
173 	union {
174 		caddr_t ifbacu_buf;
175 		struct ifbareq *ifbacu_req;
176 	} ifbac_ifbacu;
177 #define	ifbac_buf	ifbac_ifbacu.ifbacu_buf
178 #define	ifbac_req	ifbac_ifbacu.ifbacu_req
179 };
180 
181 /*
182  * Bridge parameter structure.
183  */
184 struct ifbrparam {
185 	union {
186 		uint32_t ifbrpu_int32;
187 		uint16_t ifbrpu_int16;
188 		uint8_t ifbrpu_int8;
189 	} ifbrp_ifbrpu;
190 };
191 #define	ifbrp_csize	ifbrp_ifbrpu.ifbrpu_int32	/* cache size */
192 #define	ifbrp_ctime	ifbrp_ifbrpu.ifbrpu_int32	/* cache time (sec) */
193 #define	ifbrp_prio	ifbrp_ifbrpu.ifbrpu_int16	/* bridge priority */
194 #define	ifbrp_hellotime	ifbrp_ifbrpu.ifbrpu_int8	/* hello time (sec) */
195 #define	ifbrp_fwddelay	ifbrp_ifbrpu.ifbrpu_int8	/* fwd time (sec) */
196 #define	ifbrp_maxage	ifbrp_ifbrpu.ifbrpu_int8	/* max age (sec) */
197 
198 #ifdef _KERNEL
199 /*
200  * Timekeeping structure used in spanning tree code.
201  */
202 struct bridge_timer {
203 	uint16_t	active;
204 	uint16_t	value;
205 };
206 
207 struct bstp_config_unit {
208 	uint64_t	cu_rootid;
209 	uint64_t	cu_bridge_id;
210 	uint32_t	cu_root_path_cost;
211 	uint16_t	cu_message_age;
212 	uint16_t	cu_max_age;
213 	uint16_t	cu_hello_time;
214 	uint16_t	cu_forward_delay;
215 	uint16_t	cu_port_id;
216 	uint8_t		cu_message_type;
217 	uint8_t		cu_topology_change_acknowledgment;
218 	uint8_t		cu_topology_change;
219 };
220 
221 struct bstp_tcn_unit {
222 	uint8_t		tu_message_type;
223 };
224 
225 /*
226  * Bridge interface list entry.
227  */
228 struct bridge_iflist {
229 	LIST_ENTRY(bridge_iflist) bif_next;
230 	uint64_t		bif_designated_root;
231 	uint64_t		bif_designated_bridge;
232 	uint32_t		bif_path_cost;
233 	uint32_t		bif_designated_cost;
234 	struct bridge_timer	bif_hold_timer;
235 	struct bridge_timer	bif_message_age_timer;
236 	struct bridge_timer	bif_forward_delay_timer;
237 	struct bstp_config_unit	bif_config_bpdu;
238 	uint16_t		bif_port_id;
239 	uint16_t		bif_designated_port;
240 	uint8_t			bif_state;
241 	uint8_t			bif_topology_change_acknowledge;
242 	uint8_t			bif_config_pending;
243 	uint8_t			bif_change_detection_enabled;
244 	uint8_t			bif_priority;
245 	struct ifnet		*bif_ifp;	/* member if */
246 	uint32_t		bif_flags;	/* member if flags */
247 };
248 
249 /*
250  * Bridge route node.
251  */
252 struct bridge_rtnode {
253 	LIST_ENTRY(bridge_rtnode) brt_hash;	/* hash table linkage */
254 	LIST_ENTRY(bridge_rtnode) brt_list;	/* list linkage */
255 	struct ifnet		*brt_ifp;	/* destination if */
256 	unsigned long		brt_expire;	/* expiration time */
257 	uint8_t			brt_flags;	/* address flags */
258 	uint8_t			brt_addr[ETHER_ADDR_LEN];
259 };
260 
261 /*
262  * Software state for each bridge.
263  */
264 struct bridge_softc {
265 	struct ifnet		*sc_ifp;	/* make this an interface */
266 	LIST_ENTRY(bridge_softc) sc_list;
267 	struct mtx		sc_mtx;
268 	struct cv		sc_cv;
269 	uint64_t		sc_designated_root;
270 	uint64_t		sc_bridge_id;
271 	struct bridge_iflist	*sc_root_port;
272 	uint32_t		sc_root_path_cost;
273 	uint16_t		sc_max_age;
274 	uint16_t		sc_hello_time;
275 	uint16_t		sc_forward_delay;
276 	uint16_t		sc_bridge_max_age;
277 	uint16_t		sc_bridge_hello_time;
278 	uint16_t		sc_bridge_forward_delay;
279 	uint16_t		sc_topology_change_time;
280 	uint16_t		sc_hold_time;
281 	uint16_t		sc_bridge_priority;
282 	uint8_t			sc_topology_change_detected;
283 	uint8_t			sc_topology_change;
284 	struct bridge_timer	sc_hello_timer;
285 	struct bridge_timer	sc_topology_change_timer;
286 	struct bridge_timer	sc_tcn_timer;
287 	uint32_t		sc_brtmax;	/* max # of addresses */
288 	uint32_t		sc_brtcnt;	/* cur. # of addresses */
289 	uint32_t		sc_brttimeout;	/* rt timeout in seconds */
290 	struct callout		sc_brcallout;	/* bridge callout */
291 	struct callout		sc_bstpcallout;	/* STP callout */
292 	uint32_t		sc_iflist_ref;	/* refcount for sc_iflist */
293 	uint32_t		sc_iflist_xcnt;	/* refcount for sc_iflist */
294 	LIST_HEAD(, bridge_iflist) sc_iflist;	/* member interface list */
295 	LIST_HEAD(, bridge_rtnode) *sc_rthash;	/* our forwarding table */
296 	LIST_HEAD(, bridge_rtnode) sc_rtlist;	/* list version of above */
297 	uint32_t		sc_rthash_key;	/* key for hash */
298 };
299 
300 #define BRIDGE_LOCK_INIT(_sc)		do {			\
301 	mtx_init(&(_sc)->sc_mtx, "if_bridge", NULL, MTX_DEF);	\
302 	cv_init(&(_sc)->sc_cv, "if_bridge_cv");			\
303 } while (0)
304 #define BRIDGE_LOCK_DESTROY(_sc)	do {	\
305 	mtx_destroy(&(_sc)->sc_mtx);		\
306 	cv_destroy(&(_sc)->sc_cv);		\
307 } while (0)
308 #define BRIDGE_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
309 #define BRIDGE_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
310 #define BRIDGE_LOCKED(_sc)		mtx_owned(&(_sc)->sc_mtx)
311 #define BRIDGE_LOCK_ASSERT(_sc)		mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
312 #define	BRIDGE_LOCK2REF(_sc, _err)	do {	\
313 	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
314 	if ((_sc)->sc_iflist_xcnt > 0)		\
315 		(_err) = EBUSY;			\
316 	else					\
317 		(_sc)->sc_iflist_ref++;		\
318 	mtx_unlock(&(_sc)->sc_mtx);		\
319 } while (0)
320 #define	BRIDGE_UNREF(_sc)		do {				\
321 	mtx_lock(&(_sc)->sc_mtx);					\
322 	(_sc)->sc_iflist_ref--;						\
323 	if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0))	\
324 		cv_broadcast(&(_sc)->sc_cv);				\
325 	mtx_unlock(&(_sc)->sc_mtx);					\
326 } while (0)
327 #define	BRIDGE_XLOCK(_sc)		do {		\
328 	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);		\
329 	(_sc)->sc_iflist_xcnt++;			\
330 	while ((_sc)->sc_iflist_ref > 0)		\
331 		cv_wait(&(_sc)->sc_cv, &(_sc)->sc_mtx);	\
332 } while (0)
333 #define	BRIDGE_XDROP(_sc)		do {	\
334 	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
335 	(_sc)->sc_iflist_xcnt--;		\
336 } while (0)
337 
338 extern const uint8_t bstp_etheraddr[];
339 
340 void	bridge_ifdetach(struct ifnet *);
341 void	bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
342 
343 int	bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
344 	    struct rtentry *);
345 void	bridge_dummynet(struct mbuf *, struct ifnet *);
346 struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
347 
348 extern	void	(*bstp_linkstate_p)(struct ifnet *ifp, int state);
349 
350 void	bstp_initialization(struct bridge_softc *);
351 void	bstp_linkstate(struct ifnet *, int);
352 void	bstp_stop(struct bridge_softc *);
353 struct mbuf *bstp_input(struct ifnet *, struct mbuf *);
354 
355 void	bridge_enqueue(struct bridge_softc *, struct ifnet *, struct mbuf *);
356 
357 #endif /* _KERNEL */
358