1 /*
2  * ng_l2cap_var.h
3  */
4 
5 /*-
6  * Copyright (c) 2001 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
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  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: ng_l2cap_var.h,v 1.2 2003/04/28 21:44:59 max Exp $
31  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_var.h,v 1.6 2005/01/07 01:45:43 imp Exp $
32  */
33 
34 #ifndef _NETGRAPH_L2CAP_VAR_H_
35 #define _NETGRAPH_L2CAP_VAR_H_
36 
37 /* MALLOC decalation */
38 #ifdef NG_SEPARATE_MALLOC
39 MALLOC_DECLARE(M_NETGRAPH_L2CAP);
40 #else
41 #define M_NETGRAPH_L2CAP M_NETGRAPH
42 #endif /* NG_SEPARATE_MALLOC */
43 
44 /* Debug */
45 #define	NG_L2CAP_ALERT	if (l2cap->debug >= NG_L2CAP_ALERT_LEVEL) kprintf
46 #define	NG_L2CAP_ERR	if (l2cap->debug >= NG_L2CAP_ERR_LEVEL)   kprintf
47 #define	NG_L2CAP_WARN	if (l2cap->debug >= NG_L2CAP_WARN_LEVEL)  kprintf
48 #define	NG_L2CAP_INFO	if (l2cap->debug >= NG_L2CAP_INFO_LEVEL)  kprintf
49 
50 /* Wrapper around m_pullup */
51 #define NG_L2CAP_M_PULLUP(m, s) \
52 	do { \
53 		if ((m)->m_len < (s)) \
54 			(m) = m_pullup((m), (s)); \
55 		if ((m) == NULL) \
56 			NG_L2CAP_ALERT("%s: %s - m_pullup(%zd) failed\n", \
57 				__func__, NG_NODE_NAME(l2cap->node), (s)); \
58 	} while (0)
59 
60 /*
61  * L2CAP signaling command ident's are assigned relative to the connection,
62  * because there is only one signaling channel (cid == 0x01) for every
63  * connection. So up to 254 (0xff - 0x01) L2CAP commands can be pending at the
64  * same time for the same connection.
65  */
66 
67 #define NG_L2CAP_NULL_IDENT	0x00        /* DO NOT USE THIS IDENT */
68 #define NG_L2CAP_FIRST_IDENT	0x01        /* dynamically alloc. (start) */
69 #define NG_L2CAP_LAST_IDENT	0xff        /* dynamically alloc. (end) */
70 
71 /*
72  * L2CAP (Node private)
73  */
74 
75 struct ng_l2cap_con;
76 struct ng_l2cap_chan;
77 
78 typedef struct ng_l2cap {
79 	node_p				node;         /* node ptr */
80 
81 	ng_l2cap_node_debug_ep		debug;        /* debug level */
82 	ng_l2cap_node_flags_ep		flags;        /* L2CAP node flags */
83 	ng_l2cap_node_auto_discon_ep	discon_timo;  /* auto discon. timeout */
84 
85 	u_int16_t			pkt_size;     /* max. ACL packet size */
86 	u_int16_t			num_pkts;     /* out queue size */
87 	bdaddr_t			bdaddr;       /* unit BDADDR */
88 
89 	hook_p				hci;          /* HCI downstream hook */
90 	hook_p				l2c;          /* L2CAP upstream hook */
91 	hook_p				ctl;          /* control hook */
92 
93 	LIST_HEAD(, ng_l2cap_con)	con_list;     /* ACL connections */
94 
95 	u_int16_t			cid;          /* last allocated CID */
96 	LIST_HEAD(, ng_l2cap_chan)	chan_list;    /* L2CAP channels */
97 } ng_l2cap_t;
98 typedef ng_l2cap_t *			ng_l2cap_p;
99 
100 /*
101  * L2CAP connection descriptor
102  */
103 
104 struct ng_l2cap_cmd;
105 
106 typedef struct ng_l2cap_con {
107 	ng_l2cap_p			 l2cap;      /* pointer to L2CAP */
108 
109 	u_int16_t			 state;      /* ACL connection state */
110 	u_int16_t			 flags;      /* ACL connection flags */
111 
112 	int32_t				 refcnt;     /* reference count */
113 
114 	bdaddr_t			 remote;     /* remote unit address */
115 	u_int16_t			 con_handle; /* ACL connection handle */
116 	struct callout			 con_timo;   /* connection timeout */
117 
118 	u_int8_t			 ident;      /* last allocated ident */
119 	TAILQ_HEAD(, ng_l2cap_cmd)	 cmd_list;   /* pending L2CAP cmds */
120 
121 	struct mbuf			*tx_pkt;     /* xmitted L2CAP packet */
122 	int				 pending;    /* num. of pending pkts */
123 
124 	struct mbuf			*rx_pkt;     /* received L2CAP packet */
125 	int				 rx_pkt_len; /* packet len. so far */
126 
127 	LIST_ENTRY(ng_l2cap_con)	 next;       /* link */
128 } ng_l2cap_con_t;
129 typedef ng_l2cap_con_t *		ng_l2cap_con_p;
130 
131 /*
132  * L2CAP channel descriptor
133  */
134 
135 typedef struct ng_l2cap_chan {
136 	ng_l2cap_con_p			con;        /* pointer to connection */
137 
138 	u_int16_t			state;      /* channel state */
139 
140 	u_int8_t			cfg_state;  /* configuration state */
141 #define NG_L2CAP_CFG_IN			(1 << 0)    /* incoming cfg path done */
142 #define NG_L2CAP_CFG_OUT		(1 << 1)    /* outgoing cfg path done */
143 #define NG_L2CAP_CFG_BOTH		(NG_L2CAP_CFG_IN|NG_L2CAP_CFG_OUT)
144 
145 	u_int8_t			ident;      /* last L2CAP req. ident */
146 
147 	u_int16_t			psm;        /* channel PSM */
148 	u_int16_t			scid;       /* source channel ID */
149 	u_int16_t			dcid;       /* destination channel ID */
150 
151 	u_int16_t			imtu;       /* incoming channel MTU */
152 	ng_l2cap_flow_t			iflow;      /* incoming flow control */
153 
154 	u_int16_t			omtu;       /* outgoing channel MTU */
155 	ng_l2cap_flow_t			oflow;      /* outgoing flow control */
156 
157 	u_int16_t			flush_timo; /* flush timeout */
158 	u_int16_t			link_timo;  /* link timeout */
159 
160 	LIST_ENTRY(ng_l2cap_chan)	next;       /* link */
161 } ng_l2cap_chan_t;
162 typedef ng_l2cap_chan_t *		ng_l2cap_chan_p;
163 
164 /*
165  * L2CAP command descriptor
166  */
167 
168 typedef struct ng_l2cap_cmd {
169 	ng_l2cap_con_p			 con;       /* L2CAP connection */
170 	ng_l2cap_chan_p			 ch;        /* L2CAP channel */
171 
172 	u_int16_t 			 flags;     /* command flags */
173 #define NG_L2CAP_CMD_PENDING		 (1 << 0)   /* command is pending */
174 
175 	u_int8_t 			 code;      /* L2CAP command opcode */
176 	u_int8_t			 ident;     /* L2CAP command ident */
177 	u_int32_t			 token;     /* L2CA message token */
178 
179 	struct callout			 timo;      /* RTX/ERTX timeout */
180 
181 	struct mbuf			*aux;       /* optional data */
182 
183 	TAILQ_ENTRY(ng_l2cap_cmd)	 next;      /* link */
184 } ng_l2cap_cmd_t;
185 typedef ng_l2cap_cmd_t *		ng_l2cap_cmd_p;
186 
187 #endif /* ndef _NETGRAPH_L2CAP_VAR_H_ */
188 
189