1 /*
2  * ng_l2cap_cmds.h
3  */
4 
5 /*-
6  * Copyright (c) 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_cmds.h,v 1.4 2003/04/01 18:15:26 max Exp $
31  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_cmds.h,v 1.5 2005/01/07 01:45:43 imp Exp $
32  * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
33  */
34 
35 #ifndef _NETGRAPH_L2CAP_CMDS_H_
36 #define _NETGRAPH_L2CAP_CMDS_H_
37 
38 /******************************************************************************
39  ******************************************************************************
40  **                L2CAP to L2CAP signaling command macros
41  ******************************************************************************
42  ******************************************************************************/
43 
44 /*
45  * Note: All L2CAP implementations are required to support minimal signaling
46  *       MTU of 48 bytes. In order to simplify things we will send one command
47  *       per one L2CAP packet. Given evrything above we can assume that one
48  *       signaling packet will fit into single mbuf.
49  */
50 
51 /* L2CAP_CommandRej */
52 #define	_ng_l2cap_cmd_rej(_m, _ident, _reason, _mtu, _scid, _dcid)	\
53 do {									\
54 	struct _cmd_rej {						\
55 		ng_l2cap_cmd_hdr_t	 hdr;				\
56 		ng_l2cap_cmd_rej_cp	 param;				\
57 		ng_l2cap_cmd_rej_data_t	 data;				\
58 	} __attribute__ ((packed))	*c = NULL;			\
59 									\
60 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
61 	if ((_m) == NULL) 						\
62 		break;							\
63 									\
64 	c = mtod((_m), struct _cmd_rej *);				\
65 	c->hdr.code = NG_L2CAP_CMD_REJ;					\
66 	c->hdr.ident = (_ident);					\
67 	c->hdr.length = sizeof(c->param);				\
68 									\
69 	c->param.reason = htole16((_reason));				\
70 									\
71 	if ((_reason) == NG_L2CAP_REJ_MTU_EXCEEDED) {			\
72 		c->data.mtu.mtu = htole16((_mtu));			\
73 		c->hdr.length += sizeof(c->data.mtu);			\
74 	} else if ((_reason) == NG_L2CAP_REJ_INVALID_CID) {		\
75 		c->data.cid.scid = htole16((_scid));			\
76 		c->data.cid.dcid = htole16((_dcid));			\
77 		c->hdr.length += sizeof(c->data.cid);			\
78 	}								\
79 									\
80 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(c->hdr) + 		\
81 					c->hdr.length;			\
82 									\
83 	c->hdr.length = htole16(c->hdr.length);				\
84 } while (0)
85 
86 /* L2CAP_ConnectReq */
87 #define	_ng_l2cap_con_req(_m, _ident, _psm, _scid)			\
88 do {									\
89 	struct _con_req {						\
90 		ng_l2cap_cmd_hdr_t	 hdr;				\
91 		ng_l2cap_con_req_cp	 param;				\
92 	} __attribute__ ((packed)) 	*c = NULL;			\
93 									\
94 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
95 	if ((_m) == NULL) 						\
96 		break;							\
97 									\
98 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c);			\
99 									\
100 	c = mtod((_m), struct _con_req *);				\
101 	c->hdr.code = NG_L2CAP_CON_REQ;					\
102 	c->hdr.ident = (_ident);					\
103 	c->hdr.length = htole16(sizeof(c->param));			\
104 									\
105 	c->param.psm = htole16((_psm));					\
106 	c->param.scid = htole16((_scid));				\
107 } while (0)
108 
109 /* L2CAP_ConnectRsp */
110 #define _ng_l2cap_con_rsp(_m, _ident, _dcid, _scid, _result, _status)	\
111 do {									\
112 	struct _con_rsp {						\
113 		ng_l2cap_cmd_hdr_t	 hdr;				\
114 		ng_l2cap_con_rsp_cp	 param;				\
115 	} __attribute__ ((packed))	*c = NULL;			\
116 									\
117 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
118 	if ((_m) == NULL) 						\
119 		break;							\
120 									\
121 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c);			\
122 									\
123 	c = mtod((_m), struct _con_rsp *);				\
124 	c->hdr.code = NG_L2CAP_CON_RSP;					\
125 	c->hdr.ident = (_ident);					\
126 	c->hdr.length = htole16(sizeof(c->param));			\
127 									\
128 	c->param.dcid = htole16((_dcid));				\
129 	c->param.scid = htole16((_scid));				\
130 	c->param.result = htole16((_result));				\
131 	c->param.status = htole16((_status));				\
132 } while (0)
133 
134 /* L2CAP_ConfigReq */
135 #define	_ng_l2cap_cfg_req(_m, _ident, _dcid, _flags, _data)		\
136 do {									\
137 	struct _cfg_req {						\
138 		ng_l2cap_cmd_hdr_t	 hdr;				\
139 		ng_l2cap_cfg_req_cp	 param;				\
140 	} __attribute__ ((packed))	*c = NULL;			\
141 									\
142 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
143 	if ((_m) == NULL) { 						\
144 		NG_FREE_M((_data));					\
145 		break;							\
146 	}								\
147 									\
148 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c);			\
149 									\
150 	c = mtod((_m), struct _cfg_req *);				\
151 	c->hdr.code = NG_L2CAP_CFG_REQ;					\
152 	c->hdr.ident = (_ident);					\
153 	c->hdr.length = sizeof(c->param);				\
154 									\
155 	c->param.dcid = htole16((_dcid));				\
156 	c->param.flags = htole16((_flags));				\
157 	if ((_data) != NULL) {						\
158 		int	l = (_data)->m_pkthdr.len;			\
159 									\
160 		m_cat((_m), (_data));					\
161 		c->hdr.length += l;					\
162 		(_m)->m_pkthdr.len += l;				\
163 	}								\
164 									\
165 	c->hdr.length = htole16(c->hdr.length);				\
166 } while (0)
167 
168 /* L2CAP_ConfigRsp */
169 #define _ng_l2cap_cfg_rsp(_m, _ident, _scid, _flags, _result, _data)	\
170 do {									\
171 	struct _cfg_rsp {						\
172 		ng_l2cap_cmd_hdr_t	 hdr;				\
173 		ng_l2cap_cfg_rsp_cp	 param;				\
174 	} __attribute__ ((packed))	*c = NULL;			\
175 									\
176 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
177 	if ((_m) == NULL) { 						\
178 		NG_FREE_M((_data));					\
179 		break;							\
180 	}								\
181 									\
182 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c);			\
183 									\
184 	c = mtod((_m), struct _cfg_rsp *);				\
185 	c->hdr.code = NG_L2CAP_CFG_RSP;					\
186 	c->hdr.ident = (_ident);					\
187 	c->hdr.length = sizeof(c->param);				\
188 									\
189 	c->param.scid = htole16((_scid));				\
190 	c->param.flags = htole16((_flags));				\
191 	c->param.result = htole16((_result));				\
192 	if ((_data) != NULL) {						\
193 		int	l = (_data)->m_pkthdr.len;			\
194 									\
195 		m_cat((_m), (_data));					\
196 		c->hdr.length += l;					\
197 		(_m)->m_pkthdr.len += l;				\
198 	}								\
199 									\
200 	c->hdr.length = htole16(c->hdr.length);				\
201 } while (0)
202 
203 /* Build configuration options */
204 #define _ng_l2cap_build_cfg_options(_m, _mtu, _flush_timo, _flow)	\
205 do {									\
206 	u_int8_t	*p = NULL;					\
207 									\
208 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
209 	if ((_m) == NULL)						\
210 		break;							\
211 									\
212 	(_m)->m_pkthdr.len = (_m)->m_len = 0;				\
213 	p = mtod((_m), u_int8_t *);					\
214 									\
215 	if ((_mtu) != NULL) {						\
216 		struct _cfg_opt_mtu {					\
217 			ng_l2cap_cfg_opt_t	 hdr;			\
218 			u_int16_t		 val;			\
219 		} __attribute__ ((packed))	*o = NULL;		\
220 									\
221 		o = (struct _cfg_opt_mtu *) p;				\
222 		o->hdr.type = NG_L2CAP_OPT_MTU;				\
223 		o->hdr.length = sizeof(o->val);				\
224 		o->val = htole16(*(u_int16_t *)(_mtu));			\
225 									\
226 		(_m)->m_pkthdr.len += sizeof(*o);			\
227 		p += sizeof(*o);					\
228 	}								\
229 									\
230 	if ((_flush_timo) != NULL) {					\
231 		struct _cfg_opt_flush {					\
232 			ng_l2cap_cfg_opt_t	 hdr;			\
233 			u_int16_t		 val;			\
234 		} __attribute__ ((packed))	*o = NULL;		\
235 									\
236 		o = (struct _cfg_opt_flush *) p;			\
237 		o->hdr.type = NG_L2CAP_OPT_FLUSH_TIMO;			\
238 		o->hdr.length = sizeof(o->val);				\
239 		o->val = htole16(*(u_int16_t *)(_flush_timo));		\
240 									\
241 		(_m)->m_pkthdr.len += sizeof(*o);			\
242 		p += sizeof(*o);					\
243 	}								\
244 									\
245 	if ((_flow) != NULL) {						\
246 		struct _cfg_opt_flow {					\
247 			ng_l2cap_cfg_opt_t	 hdr;			\
248 			ng_l2cap_flow_t		 val;			\
249 		} __attribute__ ((packed))	*o = NULL;		\
250 									\
251 		o = (struct _cfg_opt_flow *) p;				\
252 		o->hdr.type = NG_L2CAP_OPT_QOS;				\
253 		o->hdr.length = sizeof(o->val);				\
254 		o->val.flags = ((ng_l2cap_flow_p)(_flow))->flags;	\
255 		o->val.service_type = ((ng_l2cap_flow_p)		\
256 				(_flow))->service_type;			\
257 		o->val.token_rate =					\
258 			htole32(((ng_l2cap_flow_p)(_flow))->token_rate);\
259 		o->val.token_bucket_size =				\
260 			htole32(((ng_l2cap_flow_p)			\
261 				(_flow))->token_bucket_size);		\
262 		o->val.peak_bandwidth = 				\
263 			htole32(((ng_l2cap_flow_p)			\
264 				(_flow))->peak_bandwidth);		\
265 		o->val.latency = htole32(((ng_l2cap_flow_p)		\
266 				(_flow))->latency);			\
267 		o->val.delay_variation = 				\
268 			htole32(((ng_l2cap_flow_p)			\
269 				(_flow))->delay_variation);		\
270 									\
271 		(_m)->m_pkthdr.len += sizeof(*o);			\
272 	}								\
273 									\
274 	(_m)->m_len = (_m)->m_pkthdr.len;				\
275 } while (0)
276 
277 /* L2CAP_DisconnectReq */
278 #define	_ng_l2cap_discon_req(_m, _ident, _dcid, _scid)			\
279 do {									\
280 	struct _discon_req {						\
281 		ng_l2cap_cmd_hdr_t	 hdr;				\
282 		ng_l2cap_discon_req_cp	 param;				\
283 	} __attribute__ ((packed))	*c = NULL;			\
284 									\
285 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
286 	if ((_m) == NULL)						\
287 		break;							\
288 									\
289 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c);			\
290 									\
291 	c = mtod((_m), struct _discon_req *);				\
292 	c->hdr.code = NG_L2CAP_DISCON_REQ;				\
293 	c->hdr.ident = (_ident);					\
294 	c->hdr.length = htole16(sizeof(c->param));			\
295 									\
296 	c->param.dcid = htole16((_dcid));				\
297 	c->param.scid = htole16((_scid));				\
298 } while (0)
299 
300 /* L2CA_DisconnectRsp */
301 #define	_ng_l2cap_discon_rsp(_m, _ident, _dcid, _scid)			\
302 do {									\
303 	struct _discon_rsp {						\
304 		ng_l2cap_cmd_hdr_t	 hdr;				\
305 		ng_l2cap_discon_rsp_cp	 param;				\
306 	} __attribute__ ((packed))	*c = NULL;			\
307 									\
308 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
309 	if ((_m) == NULL)						\
310 		break;							\
311 									\
312 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c);			\
313 									\
314 	c = mtod((_m), struct _discon_rsp *);				\
315 	c->hdr.code = NG_L2CAP_DISCON_RSP;				\
316 	c->hdr.ident = (_ident);					\
317 	c->hdr.length = htole16(sizeof(c->param));			\
318 									\
319 	c->param.dcid = htole16((_dcid));				\
320 	c->param.scid = htole16((_scid));				\
321 } while (0)
322 
323 /* L2CAP_EchoReq */
324 #define	_ng_l2cap_echo_req(_m, _ident, _data, _size)			\
325 do {									\
326 	ng_l2cap_cmd_hdr_t	*c = NULL;				\
327 									\
328 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
329 	if ((_m) == NULL) 						\
330 		break;							\
331 									\
332 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c);			\
333 									\
334 	c = mtod((_m), ng_l2cap_cmd_hdr_t *);				\
335 	c->code = NG_L2CAP_ECHO_REQ;					\
336 	c->ident = (_ident);						\
337 	c->length = 0;							\
338 									\
339 	if ((_data) != NULL) {						\
340 		m_copyback((_m), sizeof(*c), (_size), (_data));		\
341 		c->length += (_size);					\
342 	}								\
343 									\
344 	c->length = htole16(c->length);					\
345 } while (0)
346 
347 /* L2CAP_InfoReq */
348 #define	_ng_l2cap_info_req(_m, _ident, _type)				\
349 do {									\
350 	struct _info_req {						\
351 		ng_l2cap_cmd_hdr_t	 hdr;				\
352 		ng_l2cap_info_req_cp	 param;				\
353 	} __attribute__ ((packed))	*c = NULL;			\
354 									\
355 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
356 	if ((_m) == NULL)						\
357 		break;							\
358 									\
359 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c);			\
360 									\
361 	c = mtod((_m), struct _info_req *);				\
362 	c->hdr.code = NG_L2CAP_INFO_REQ;				\
363 	c->hdr.ident = (_ident);					\
364 	c->hdr.length = htole16(sizeof(c->param));			\
365 									\
366 	c->param.type = htole16((_type));				\
367 } while (0)
368 
369 /* L2CAP_InfoRsp */
370 #define	_ng_l2cap_info_rsp(_m, _ident, _type, _result, _mtu)		\
371 do {									\
372 	struct _info_rsp {						\
373 		ng_l2cap_cmd_hdr_t	 hdr;				\
374 		ng_l2cap_info_rsp_cp	 param;				\
375 		ng_l2cap_info_rsp_data_t data;				\
376 	} __attribute__ ((packed))	*c = NULL;			\
377 									\
378 	MGETHDR((_m), MB_DONTWAIT, MT_DATA);				\
379 	if ((_m) == NULL) 						\
380 		break;							\
381 									\
382 	c = mtod((_m), struct _info_rsp *);				\
383 	c->hdr.code = NG_L2CAP_INFO_REQ;				\
384 	c->hdr.ident = (_ident);					\
385 	c->hdr.length = sizeof(c->param);				\
386 									\
387 	c->param.type = htole16((_type));				\
388 	c->param.result = htole16((_result));				\
389 									\
390 	if ((_result) == NG_L2CAP_SUCCESS) {				\
391 		switch ((_type)) {					\
392 		case NG_L2CAP_CONNLESS_MTU:				\
393 			c->data.mtu.mtu = htole16((_mtu));		\
394 			c->hdr.length += sizeof((c->data.mtu.mtu));	\
395 			break;						\
396 		}							\
397 	}								\
398 									\
399 	(_m)->m_pkthdr.len = (_m)->m_len = sizeof(c->hdr) +		\
400 					c->hdr.length;			\
401 									\
402 	c->hdr.length = htole16(c->hdr.length);		 		\
403 } while (0)
404 
405 void ng_l2cap_con_wakeup              (ng_l2cap_con_p);
406 void ng_l2cap_con_fail                (ng_l2cap_con_p, u_int16_t);
407 void ng_l2cap_process_command_timeout (node_p, hook_p, void *, int);
408 
409 #endif /* ndef _NETGRAPH_L2CAP_CMDS_H_ */
410 
411