xref: /netbsd/sys/net/if_bridgevar.h (revision 662541b8)
1 /*	$NetBSD: if_bridgevar.h,v 1.37 2021/09/30 03:57:48 yamaguchi 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 
71 /*
72  * Data structure and control definitions for bridge interfaces.
73  */
74 
75 #ifndef _NET_IF_BRIDGEVAR_H_
76 #define _NET_IF_BRIDGEVAR_H_
77 
78 #include <sys/callout.h>
79 #include <sys/queue.h>
80 #include <sys/mutex.h>
81 #include <sys/condvar.h>
82 
83 /*
84  * Commands used in the SIOCSDRVSPEC ioctl.  Note the lookup of the
85  * bridge interface itself is keyed off the ifdrv structure.
86  */
87 #define	BRDGADD			0	/* add bridge member (ifbreq) */
88 #define	BRDGDEL			1	/* delete bridge member (ifbreq) */
89 #define	BRDGGIFFLGS		2	/* get member if flags (ifbreq) */
90 #define	BRDGSIFFLGS		3	/* set member if flags (ifbreq) */
91 #define	BRDGSCACHE		4	/* set cache size (ifbrparam) */
92 #define	BRDGGCACHE		5	/* get cache size (ifbrparam) */
93 #define	OBRDGGIFS		6	/* get member list (ifbifconf) */
94 #define	OBRDGRTS		7	/* get address list (ifbaconf) */
95 #define	BRDGSADDR		8	/* set static address (ifbareq) */
96 #define	BRDGSTO			9	/* set cache timeout (ifbrparam) */
97 #define	BRDGGTO			10	/* get cache timeout (ifbrparam) */
98 #define	BRDGDADDR		11	/* delete address (ifbareq) */
99 #define	BRDGFLUSH		12	/* flush address cache (ifbreq) */
100 
101 #define	BRDGGPRI		13	/* get priority (ifbrparam) */
102 #define	BRDGSPRI		14	/* set priority (ifbrparam) */
103 #define	BRDGGHT			15	/* get hello time (ifbrparam) */
104 #define	BRDGSHT			16	/* set hello time (ifbrparam) */
105 #define	BRDGGFD			17	/* get forward delay (ifbrparam) */
106 #define	BRDGSFD			18	/* set forward delay (ifbrparam) */
107 #define	BRDGGMA			19	/* get max age (ifbrparam) */
108 #define	BRDGSMA			20	/* set max age (ifbrparam) */
109 #define	BRDGSIFPRIO		21	/* set if priority (ifbreq) */
110 #define BRDGSIFCOST		22	/* set if path cost (ifbreq) */
111 #define BRDGGFILT	        23	/* get filter flags (ifbrparam) */
112 #define BRDGSFILT	        24	/* set filter flags (ifbrparam) */
113 
114 #define	BRDGGIFS		25	/* get member list */
115 #define	BRDGRTS			26	/* get address list */
116 
117 /*
118  * Generic bridge control request.
119  */
120 struct ifbreq {
121 	char		ifbr_ifsname[IFNAMSIZ];	/* member if name */
122 	uint32_t	ifbr_ifsflags;		/* member if flags */
123 	uint8_t		ifbr_state;		/* member if STP state */
124 	uint8_t		ifbr_priority;		/* member if STP priority */
125 	uint8_t		ifbr_path_cost;		/* member if STP cost */
126 	uint8_t		ifbr_portno;		/* member if port number */
127 };
128 
129 /* BRDGGIFFLAGS, BRDGSIFFLAGS */
130 #define	IFBIF_LEARNING		0x01	/* if can learn */
131 #define	IFBIF_DISCOVER		0x02	/* if sends packets w/ unknown dest. */
132 #define	IFBIF_STP		0x04	/* if participates in spanning tree */
133 
134 #define	IFBIFBITS	"\020\1LEARNING\2DISCOVER\3STP"
135 
136 /* BRDGFLUSH */
137 #define	IFBF_FLUSHDYN		0x00	/* flush learned addresses only */
138 #define	IFBF_FLUSHALL		0x01	/* flush all addresses */
139 
140 /* BRDGSFILT */
141 #define IFBF_FILT_USEIPF	0x00000001 /* enable ipf on bridge */
142 #define IFBF_FILT_MASK		0x00000001 /* mask of valid values */
143 
144 /* STP port states */
145 #define	BSTP_IFSTATE_DISABLED	0
146 #define	BSTP_IFSTATE_LISTENING	1
147 #define	BSTP_IFSTATE_LEARNING	2
148 #define	BSTP_IFSTATE_FORWARDING	3
149 #define	BSTP_IFSTATE_BLOCKING	4
150 
151 /*
152  * Interface list structure.
153  */
154 struct ifbifconf {
155 	uint32_t	ifbic_len;	/* buffer size */
156 	union {
157 		void *	ifbicu_buf;
158 		struct ifbreq *ifbicu_req;
159 	} ifbic_ifbicu;
160 #define	ifbic_buf	ifbic_ifbicu.ifbicu_buf
161 #define	ifbic_req	ifbic_ifbicu.ifbicu_req
162 };
163 
164 /*
165  * Bridge address request.
166  */
167 struct ifbareq {
168 	char		ifba_ifsname[IFNAMSIZ];	/* member if name */
169 	time_t		ifba_expire;		/* address expire time */
170 	uint8_t		ifba_flags;		/* address flags */
171 	uint8_t		ifba_dst[ETHER_ADDR_LEN];/* destination address */
172 };
173 
174 #define	IFBAF_TYPEMASK	0x03	/* address type mask */
175 #define	IFBAF_DYNAMIC	0x00	/* dynamically learned address */
176 #define	IFBAF_STATIC	0x01	/* static address */
177 
178 #define	IFBAFBITS	"\020\1STATIC"
179 
180 /*
181  * Address list structure.
182  */
183 struct ifbaconf {
184 	uint32_t	ifbac_len;	/* buffer size */
185 	union {
186 		void *ifbacu_buf;
187 		struct ifbareq *ifbacu_req;
188 	} ifbac_ifbacu;
189 #define	ifbac_buf	ifbac_ifbacu.ifbacu_buf
190 #define	ifbac_req	ifbac_ifbacu.ifbacu_req
191 };
192 
193 /*
194  * Bridge parameter structure.
195  */
196 struct ifbrparam {
197 	union {
198 		uint32_t ifbrpu_int32;
199 		uint16_t ifbrpu_int16;
200 		uint8_t ifbrpu_int8;
201 	} ifbrp_ifbrpu;
202 };
203 #define	ifbrp_csize	ifbrp_ifbrpu.ifbrpu_int32	/* cache size */
204 #define	ifbrp_ctime	ifbrp_ifbrpu.ifbrpu_int32	/* cache time (sec) */
205 #define	ifbrp_prio	ifbrp_ifbrpu.ifbrpu_int16	/* bridge priority */
206 #define	ifbrp_hellotime	ifbrp_ifbrpu.ifbrpu_int8	/* hello time (sec) */
207 #define	ifbrp_fwddelay	ifbrp_ifbrpu.ifbrpu_int8	/* fwd time (sec) */
208 #define	ifbrp_maxage	ifbrp_ifbrpu.ifbrpu_int8	/* max age (sec) */
209 #define	ifbrp_filter	ifbrp_ifbrpu.ifbrpu_int32	/* filtering flags */
210 
211 #ifdef _KERNEL
212 #ifdef _KERNEL_OPT
213 #include "opt_net_mpsafe.h"
214 #endif /* _KERNEL_OPT */
215 
216 #include <sys/pserialize.h>
217 #include <sys/pslist.h>
218 #include <sys/psref.h>
219 #include <sys/workqueue.h>
220 
221 #include <net/pktqueue.h>
222 
223 /*
224  * Timekeeping structure used in spanning tree code.
225  */
226 struct bridge_timer {
227 	uint16_t	active;
228 	uint16_t	value;
229 };
230 
231 struct bstp_config_unit {
232 	uint64_t	cu_rootid;
233 	uint64_t	cu_bridge_id;
234 	uint32_t	cu_root_path_cost;
235 	uint16_t	cu_message_age;
236 	uint16_t	cu_max_age;
237 	uint16_t	cu_hello_time;
238 	uint16_t	cu_forward_delay;
239 	uint16_t	cu_port_id;
240 	uint8_t		cu_message_type;
241 	uint8_t		cu_topology_change_acknowledgment;
242 	uint8_t		cu_topology_change;
243 };
244 
245 struct bstp_tcn_unit {
246 	uint8_t		tu_message_type;
247 };
248 
249 /*
250  * Bridge interface list entry.
251  */
252 struct bridge_iflist {
253 	struct pslist_entry	bif_next;
254 	uint64_t		bif_designated_root;
255 	uint64_t		bif_designated_bridge;
256 	uint32_t		bif_path_cost;
257 	uint32_t		bif_designated_cost;
258 	struct bridge_timer	bif_hold_timer;
259 	struct bridge_timer	bif_message_age_timer;
260 	struct bridge_timer	bif_forward_delay_timer;
261 	uint16_t		bif_port_id;
262 	uint16_t		bif_designated_port;
263 	struct bstp_config_unit	bif_config_bpdu;
264 	uint8_t			bif_state;
265 	uint8_t			bif_topology_change_acknowledge;
266 	uint8_t			bif_config_pending;
267 	uint8_t			bif_change_detection_enabled;
268 	uint8_t			bif_priority;
269 	struct ifnet		*bif_ifp;	/* member if */
270 	uint32_t		bif_flags;	/* member if flags */
271 	struct psref_target	bif_psref;
272 	void *			*bif_linkstate_hook;
273 	void *			*bif_ifdetach_hook;
274 };
275 
276 /*
277  * Bridge route node.
278  */
279 struct bridge_rtnode {
280 	struct pslist_entry     brt_hash;	/* hash table linkage */
281 	struct pslist_entry     brt_list;	/* list linkage */
282 	struct ifnet		*brt_ifp;	/* destination if */
283 	time_t			brt_expire;	/* expiration time */
284 	uint8_t			brt_flags;	/* address flags */
285 	uint8_t			brt_addr[ETHER_ADDR_LEN];
286 };
287 
288 struct bridge_iflist_psref {
289 	struct pslist_head	bip_iflist;	/* member interface list */
290 	kmutex_t		bip_lock;
291 	pserialize_t		bip_psz;
292 };
293 
294 /*
295  * Software state for each bridge.
296  */
297 struct bridge_softc {
298 	struct ifnet		sc_if;
299 	LIST_ENTRY(bridge_softc) sc_list;
300 	uint64_t		sc_designated_root;
301 	uint64_t		sc_bridge_id;
302 	struct bridge_iflist	*sc_root_port;
303 	uint32_t		sc_root_path_cost;
304 	uint16_t		sc_max_age;
305 	uint16_t		sc_hello_time;
306 	uint16_t		sc_forward_delay;
307 	uint16_t		sc_bridge_max_age;
308 	uint16_t		sc_bridge_hello_time;
309 	uint16_t		sc_bridge_forward_delay;
310 	uint16_t		sc_topology_change_time;
311 	uint16_t		sc_hold_time;
312 	uint16_t		sc_bridge_priority;
313 	uint8_t			sc_topology_change_detected;
314 	uint8_t			sc_topology_change;
315 	struct bridge_timer	sc_hello_timer;
316 	struct bridge_timer	sc_topology_change_timer;
317 	struct bridge_timer	sc_tcn_timer;
318 	uint32_t		sc_brtmax;	/* max # of addresses */
319 	uint32_t		sc_brtcnt;	/* cur. # of addresses */
320 	uint32_t		sc_brttimeout;	/* rt timeout in seconds */
321 	callout_t		sc_brcallout;	/* bridge callout */
322 	callout_t		sc_bstpcallout;	/* STP callout */
323 	struct bridge_iflist_psref	sc_iflist_psref;
324 	struct pslist_head	*sc_rthash;	/* our forwarding table */
325 	struct pslist_head	sc_rtlist;	/* list version of above */
326 	kmutex_t		*sc_rtlist_lock;
327 	pserialize_t		sc_rtlist_psz;
328 	struct workqueue	*sc_rtage_wq;
329 	struct work		sc_rtage_wk;
330 	uint32_t		sc_rthash_key;	/* key for hash */
331 	uint32_t		sc_filter_flags; /* ipf and flags */
332 	int			sc_csum_flags_tx;
333 	int			sc_capenable;
334 };
335 
336 extern const uint8_t bstp_etheraddr[];
337 
338 int	bridge_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
339 	    const struct rtentry *);
340 
341 void	bstp_initialization(struct bridge_softc *);
342 void	bstp_stop(struct bridge_softc *);
343 void	bstp_input(struct bridge_softc *, struct bridge_iflist *, struct mbuf *);
344 
345 void	bridge_enqueue(struct bridge_softc *, struct ifnet *, struct mbuf *,
346 	    int);
347 
348 void	bridge_calc_csum_flags(struct bridge_softc *);
349 
350 #define BRIDGE_LOCK_OBJ(_sc)	(&(_sc)->sc_iflist_psref.bip_lock)
351 #define BRIDGE_LOCK(_sc)	mutex_enter(BRIDGE_LOCK_OBJ(_sc))
352 #define BRIDGE_UNLOCK(_sc)	mutex_exit(BRIDGE_LOCK_OBJ(_sc))
353 #define BRIDGE_LOCKED(_sc)	mutex_owned(BRIDGE_LOCK_OBJ(_sc))
354 
355 #define BRIDGE_PSZ_RENTER(__s)	do { __s = pserialize_read_enter(); } while (0)
356 #define BRIDGE_PSZ_REXIT(__s)	do { pserialize_read_exit(__s); } while (0)
357 #define BRIDGE_PSZ_PERFORM(_sc)	pserialize_perform((_sc)->sc_iflist_psref.bip_psz)
358 
359 #define BRIDGE_IFLIST_READER_FOREACH(_bif, _sc) \
360 	PSLIST_READER_FOREACH((_bif), &((_sc)->sc_iflist_psref.bip_iflist), \
361 	    struct bridge_iflist, bif_next)
362 #define BRIDGE_IFLIST_WRITER_FOREACH(_bif, _sc) \
363 	PSLIST_WRITER_FOREACH((_bif), &((_sc)->sc_iflist_psref.bip_iflist), \
364 	    struct bridge_iflist, bif_next)
365 
366 /*
367  * Locking notes:
368  * - Updates of sc_iflist are serialized by sc_iflist_lock (an adaptive mutex)
369  *   - The mutex is also used for STP
370  * - Items of sc_iflist (bridge_iflist) is protected by both pserialize
371  *   (sc_iflist_psz) and reference counting (bridge_iflist#bif_refs)
372  * - Before destroying an item of sc_iflist, we have to do pserialize_perform
373  *   and synchronize with the reference counting via a conditional variable
374  *   (sc_iflist_cz)
375  * - Updates of sc_rtlist are serialized by sc_rtlist_lock (an adaptive mutex)
376  *   - The mutex is also used for pserialize
377  * - A workqueue is used to run bridge_rtage in LWP context via bridge_timer callout
378  *   - bridge_rtage uses pserialize that requires non-interrupt context
379  */
380 #endif /* _KERNEL */
381 #endif /* !_NET_IF_BRIDGEVAR_H_ */
382