xref: /freebsd/sys/net/bridgestp.h (revision 0957b409)
1 /*	$NetBSD: if_bridgevar.h,v 1.4 2003/07/08 07:13:50 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright 2001 Wasabi Systems, Inc.
7  * All rights reserved.
8  *
9  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed for the NetBSD Project by
22  *	Wasabi Systems, Inc.
23  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24  *    or promote products derived from this software without specific prior
25  *    written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*-
41  * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by Jason L. Wright
55  * 4. The name of the author may not be used to endorse or promote products
56  *    derived from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
60  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
62  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
63  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
64  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
66  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
67  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68  * POSSIBILITY OF SUCH DAMAGE.
69  *
70  * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp
71  *
72  * $FreeBSD$
73  */
74 
75 /*
76  * Data structure and control definitions for STP interfaces.
77  */
78 
79 #include <sys/callout.h>
80 #include <sys/queue.h>
81 
82 /* STP port states */
83 #define	BSTP_IFSTATE_DISABLED	0
84 #define	BSTP_IFSTATE_LISTENING	1
85 #define	BSTP_IFSTATE_LEARNING	2
86 #define	BSTP_IFSTATE_FORWARDING	3
87 #define	BSTP_IFSTATE_BLOCKING	4
88 #define	BSTP_IFSTATE_DISCARDING	5
89 
90 #define	BSTP_TCSTATE_ACTIVE	1
91 #define	BSTP_TCSTATE_DETECTED	2
92 #define	BSTP_TCSTATE_INACTIVE	3
93 #define	BSTP_TCSTATE_LEARNING	4
94 #define	BSTP_TCSTATE_PROPAG	5
95 #define	BSTP_TCSTATE_ACK	6
96 #define	BSTP_TCSTATE_TC		7
97 #define	BSTP_TCSTATE_TCN	8
98 
99 #define	BSTP_ROLE_DISABLED	0
100 #define	BSTP_ROLE_ROOT		1
101 #define	BSTP_ROLE_DESIGNATED	2
102 #define	BSTP_ROLE_ALTERNATE	3
103 #define	BSTP_ROLE_BACKUP	4
104 
105 #ifdef _KERNEL
106 
107 /* STP port flags */
108 #define	BSTP_PORT_CANMIGRATE	0x0001
109 #define	BSTP_PORT_NEWINFO	0x0002
110 #define	BSTP_PORT_DISPUTED	0x0004
111 #define	BSTP_PORT_ADMCOST	0x0008
112 #define	BSTP_PORT_AUTOEDGE	0x0010
113 #define	BSTP_PORT_AUTOPTP	0x0020
114 #define	BSTP_PORT_ADMEDGE	0x0040
115 #define	BSTP_PORT_PNDCOST	0x0080
116 
117 /* BPDU priority */
118 #define	BSTP_PDU_SUPERIOR	1
119 #define	BSTP_PDU_REPEATED	2
120 #define	BSTP_PDU_INFERIOR	3
121 #define	BSTP_PDU_INFERIORALT	4
122 #define	BSTP_PDU_OTHER		5
123 
124 /* BPDU flags */
125 #define	BSTP_PDU_PRMASK		0x0c		/* Port Role */
126 #define	BSTP_PDU_PRSHIFT	2		/* Port Role offset */
127 #define	BSTP_PDU_F_UNKN		0x00		/* Unknown port    (00) */
128 #define	BSTP_PDU_F_ALT		0x01		/* Alt/Backup port (01) */
129 #define	BSTP_PDU_F_ROOT		0x02		/* Root port       (10) */
130 #define	BSTP_PDU_F_DESG		0x03		/* Designated port (11) */
131 
132 #define	BSTP_PDU_STPMASK	0x81		/* strip unused STP flags */
133 #define	BSTP_PDU_RSTPMASK	0x7f		/* strip unused RSTP flags */
134 #define	BSTP_PDU_F_TC		0x01		/* Topology change */
135 #define	BSTP_PDU_F_P		0x02		/* Proposal flag */
136 #define	BSTP_PDU_F_L		0x10		/* Learning flag */
137 #define	BSTP_PDU_F_F		0x20		/* Forwarding flag */
138 #define	BSTP_PDU_F_A		0x40		/* Agreement flag */
139 #define	BSTP_PDU_F_TCA		0x80		/* Topology change ack */
140 
141 /*
142  * Spanning tree defaults.
143  */
144 #define	BSTP_DEFAULT_MAX_AGE		(20 * 256)
145 #define	BSTP_DEFAULT_HELLO_TIME		(2 * 256)
146 #define	BSTP_DEFAULT_FORWARD_DELAY	(15 * 256)
147 #define	BSTP_DEFAULT_HOLD_TIME		(1 * 256)
148 #define	BSTP_DEFAULT_MIGRATE_DELAY	(3 * 256)
149 #define	BSTP_DEFAULT_HOLD_COUNT		6
150 #define	BSTP_DEFAULT_BRIDGE_PRIORITY	0x8000
151 #define	BSTP_DEFAULT_PORT_PRIORITY	0x80
152 #define	BSTP_DEFAULT_PATH_COST		55
153 #define	BSTP_MIN_HELLO_TIME		(1 * 256)
154 #define	BSTP_MIN_MAX_AGE		(6 * 256)
155 #define	BSTP_MIN_FORWARD_DELAY		(4 * 256)
156 #define	BSTP_MIN_HOLD_COUNT		1
157 #define	BSTP_MAX_HELLO_TIME		(2 * 256)
158 #define	BSTP_MAX_MAX_AGE		(40 * 256)
159 #define	BSTP_MAX_FORWARD_DELAY		(30 * 256)
160 #define	BSTP_MAX_HOLD_COUNT		10
161 #define	BSTP_MAX_PRIORITY		61440
162 #define	BSTP_MAX_PORT_PRIORITY		240
163 #define	BSTP_MAX_PATH_COST		200000000
164 
165 /* BPDU message types */
166 #define	BSTP_MSGTYPE_CFG	0x00		/* Configuration */
167 #define	BSTP_MSGTYPE_RSTP	0x02		/* Rapid STP */
168 #define	BSTP_MSGTYPE_TCN	0x80		/* Topology chg notification */
169 
170 /* Protocol versions */
171 #define	BSTP_PROTO_ID		0x00
172 #define	BSTP_PROTO_STP		0x00
173 #define	BSTP_PROTO_RSTP		0x02
174 #define	BSTP_PROTO_MAX		BSTP_PROTO_RSTP
175 
176 #define	BSTP_INFO_RECEIVED	1
177 #define	BSTP_INFO_MINE		2
178 #define	BSTP_INFO_AGED		3
179 #define	BSTP_INFO_DISABLED	4
180 
181 
182 #define	BSTP_MESSAGE_AGE_INCR	(1 * 256)	/* in 256ths of a second */
183 #define	BSTP_TICK_VAL		(1 * 256)	/* in 256ths of a second */
184 #define	BSTP_LINK_TIMER		(BSTP_TICK_VAL * 15)
185 
186 /*
187  * Driver callbacks for STP state changes
188  */
189 typedef void (*bstp_state_cb_t)(struct ifnet *, int);
190 typedef void (*bstp_rtage_cb_t)(struct ifnet *, int);
191 struct bstp_cb_ops {
192 	bstp_state_cb_t	bcb_state;
193 	bstp_rtage_cb_t	bcb_rtage;
194 };
195 
196 /*
197  * Because BPDU's do not make nicely aligned structures, two different
198  * declarations are used: bstp_?bpdu (wire representation, packed) and
199  * bstp_*_unit (internal, nicely aligned version).
200  */
201 
202 /* configuration bridge protocol data unit */
203 struct bstp_cbpdu {
204 	uint8_t		cbu_dsap;		/* LLC: destination sap */
205 	uint8_t		cbu_ssap;		/* LLC: source sap */
206 	uint8_t		cbu_ctl;		/* LLC: control */
207 	uint16_t	cbu_protoid;		/* protocol id */
208 	uint8_t		cbu_protover;		/* protocol version */
209 	uint8_t		cbu_bpdutype;		/* message type */
210 	uint8_t		cbu_flags;		/* flags (below) */
211 
212 	/* root id */
213 	uint16_t	cbu_rootpri;		/* root priority */
214 	uint8_t		cbu_rootaddr[6];	/* root address */
215 
216 	uint32_t	cbu_rootpathcost;	/* root path cost */
217 
218 	/* bridge id */
219 	uint16_t	cbu_bridgepri;		/* bridge priority */
220 	uint8_t		cbu_bridgeaddr[6];	/* bridge address */
221 
222 	uint16_t	cbu_portid;		/* port id */
223 	uint16_t	cbu_messageage;		/* current message age */
224 	uint16_t	cbu_maxage;		/* maximum age */
225 	uint16_t	cbu_hellotime;		/* hello time */
226 	uint16_t	cbu_forwarddelay;	/* forwarding delay */
227 	uint8_t		cbu_versionlen;		/* version 1 length */
228 } __packed;
229 #define	BSTP_BPDU_STP_LEN	(3 + 35)	/* LLC + STP pdu */
230 #define	BSTP_BPDU_RSTP_LEN	(3 + 36)	/* LLC + RSTP pdu */
231 
232 /* topology change notification bridge protocol data unit */
233 struct bstp_tbpdu {
234 	uint8_t		tbu_dsap;		/* LLC: destination sap */
235 	uint8_t		tbu_ssap;		/* LLC: source sap */
236 	uint8_t		tbu_ctl;		/* LLC: control */
237 	uint16_t	tbu_protoid;		/* protocol id */
238 	uint8_t		tbu_protover;		/* protocol version */
239 	uint8_t		tbu_bpdutype;		/* message type */
240 } __packed;
241 
242 /*
243  * Timekeeping structure used in spanning tree code.
244  */
245 struct bstp_timer {
246 	int		active;
247 	int		latched;
248 	int		value;
249 };
250 
251 struct bstp_pri_vector {
252 	uint64_t		pv_root_id;
253 	uint32_t		pv_cost;
254 	uint64_t		pv_dbridge_id;
255 	uint16_t		pv_dport_id;
256 	uint16_t		pv_port_id;
257 };
258 
259 struct bstp_config_unit {
260 	struct bstp_pri_vector	cu_pv;
261 	uint16_t	cu_message_age;
262 	uint16_t	cu_max_age;
263 	uint16_t	cu_forward_delay;
264 	uint16_t	cu_hello_time;
265 	uint8_t		cu_message_type;
266 	uint8_t		cu_topology_change_ack;
267 	uint8_t		cu_topology_change;
268 	uint8_t		cu_proposal;
269 	uint8_t		cu_agree;
270 	uint8_t		cu_learning;
271 	uint8_t		cu_forwarding;
272 	uint8_t		cu_role;
273 };
274 
275 struct bstp_tcn_unit {
276 	uint8_t		tu_message_type;
277 };
278 
279 struct bstp_port {
280 	LIST_ENTRY(bstp_port)	bp_next;
281 	struct ifnet		*bp_ifp;	/* parent if */
282 	struct bstp_state	*bp_bs;
283 	uint8_t			bp_active;
284 	uint8_t			bp_protover;
285 	uint32_t		bp_flags;
286 	uint32_t		bp_path_cost;
287 	uint16_t		bp_port_msg_age;
288 	uint16_t		bp_port_max_age;
289 	uint16_t		bp_port_fdelay;
290 	uint16_t		bp_port_htime;
291 	uint16_t		bp_desg_msg_age;
292 	uint16_t		bp_desg_max_age;
293 	uint16_t		bp_desg_fdelay;
294 	uint16_t		bp_desg_htime;
295 	struct bstp_timer	bp_edge_delay_timer;
296 	struct bstp_timer	bp_forward_delay_timer;
297 	struct bstp_timer	bp_hello_timer;
298 	struct bstp_timer	bp_message_age_timer;
299 	struct bstp_timer	bp_migrate_delay_timer;
300 	struct bstp_timer	bp_recent_backup_timer;
301 	struct bstp_timer	bp_recent_root_timer;
302 	struct bstp_timer	bp_tc_timer;
303 	struct bstp_config_unit bp_msg_cu;
304 	struct bstp_pri_vector	bp_desg_pv;
305 	struct bstp_pri_vector	bp_port_pv;
306 	uint16_t		bp_port_id;
307 	uint8_t			bp_state;
308 	uint8_t			bp_tcstate;
309 	uint8_t			bp_role;
310 	uint8_t			bp_infois;
311 	uint8_t			bp_tc_ack;
312 	uint8_t			bp_tc_prop;
313 	uint8_t			bp_fdbflush;
314 	uint8_t			bp_priority;
315 	uint8_t			bp_ptp_link;
316 	uint8_t			bp_agree;
317 	uint8_t			bp_agreed;
318 	uint8_t			bp_sync;
319 	uint8_t			bp_synced;
320 	uint8_t			bp_proposing;
321 	uint8_t			bp_proposed;
322 	uint8_t			bp_operedge;
323 	uint8_t			bp_reroot;
324 	uint8_t			bp_rcvdtc;
325 	uint8_t			bp_rcvdtca;
326 	uint8_t			bp_rcvdtcn;
327 	uint32_t		bp_forward_transitions;
328 	uint8_t			bp_txcount;
329 	struct task		bp_statetask;
330 	struct task		bp_rtagetask;
331 	struct task		bp_mediatask;
332 };
333 
334 /*
335  * Software state for each bridge STP.
336  */
337 struct bstp_state {
338 	LIST_ENTRY(bstp_state)	bs_list;
339 	uint8_t			bs_running;
340 	struct mtx		bs_mtx;
341 	struct bstp_pri_vector	bs_bridge_pv;
342 	struct bstp_pri_vector	bs_root_pv;
343 	struct bstp_port	*bs_root_port;
344 	uint8_t			bs_protover;
345 	uint16_t		bs_migration_delay;
346 	uint16_t		bs_edge_delay;
347 	uint16_t		bs_bridge_max_age;
348 	uint16_t		bs_bridge_fdelay;
349 	uint16_t		bs_bridge_htime;
350 	uint16_t		bs_root_msg_age;
351 	uint16_t		bs_root_max_age;
352 	uint16_t		bs_root_fdelay;
353 	uint16_t		bs_root_htime;
354 	uint16_t		bs_hold_time;
355 	uint16_t		bs_bridge_priority;
356 	uint8_t			bs_txholdcount;
357 	uint8_t			bs_allsynced;
358 	struct callout		bs_bstpcallout;	/* STP callout */
359 	struct bstp_timer	bs_link_timer;
360 	struct timeval		bs_last_tc_time;
361 	LIST_HEAD(, bstp_port)	bs_bplist;
362 	bstp_state_cb_t		bs_state_cb;
363 	bstp_rtage_cb_t		bs_rtage_cb;
364 	struct vnet		*bs_vnet;
365 };
366 
367 #define	BSTP_LOCK_INIT(_bs)	mtx_init(&(_bs)->bs_mtx, "bstp", NULL, MTX_DEF)
368 #define	BSTP_LOCK_DESTROY(_bs)	mtx_destroy(&(_bs)->bs_mtx)
369 #define	BSTP_LOCK(_bs)		mtx_lock(&(_bs)->bs_mtx)
370 #define	BSTP_UNLOCK(_bs)	mtx_unlock(&(_bs)->bs_mtx)
371 #define	BSTP_LOCK_ASSERT(_bs)	mtx_assert(&(_bs)->bs_mtx, MA_OWNED)
372 
373 extern const uint8_t bstp_etheraddr[];
374 
375 void	bstp_attach(struct bstp_state *, struct bstp_cb_ops *);
376 void	bstp_detach(struct bstp_state *);
377 void	bstp_init(struct bstp_state *);
378 void	bstp_stop(struct bstp_state *);
379 int	bstp_create(struct bstp_state *, struct bstp_port *, struct ifnet *);
380 int	bstp_enable(struct bstp_port *);
381 void	bstp_disable(struct bstp_port *);
382 void	bstp_destroy(struct bstp_port *);
383 void	bstp_linkstate(struct bstp_port *);
384 int	bstp_set_htime(struct bstp_state *, int);
385 int	bstp_set_fdelay(struct bstp_state *, int);
386 int	bstp_set_maxage(struct bstp_state *, int);
387 int	bstp_set_holdcount(struct bstp_state *, int);
388 int	bstp_set_protocol(struct bstp_state *, int);
389 int	bstp_set_priority(struct bstp_state *, int);
390 int	bstp_set_port_priority(struct bstp_port *, int);
391 int	bstp_set_path_cost(struct bstp_port *, uint32_t);
392 int	bstp_set_edge(struct bstp_port *, int);
393 int	bstp_set_autoedge(struct bstp_port *, int);
394 int	bstp_set_ptp(struct bstp_port *, int);
395 int	bstp_set_autoptp(struct bstp_port *, int);
396 void	bstp_input(struct bstp_port *, struct ifnet *, struct mbuf *);
397 
398 #endif /* _KERNEL */
399