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