xref: /netbsd-src/sys/netbt/l2cap.h (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: l2cap.h,v 1.1 2006/06/19 15:44:45 gdamore Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005 Iain Hibbert.
5  * Copyright (c) 2006 Itronix Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of Itronix Inc. may not be used to endorse
17  *    or promote products derived from this software without specific
18  *    prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*-
33  * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  * $Id: l2cap.h,v 1.1 2006/06/19 15:44:45 gdamore Exp $
58  * $FreeBSD: src/sys/netgraph/bluetooth/include/l2cap.h,v 1.4 2005/08/31 18:13:23 emax Exp $
59  */
60 
61 /*
62  * This file contains everything that application needs to know about
63  * Link Layer Control and Adaptation Protocol (L2CAP). All information
64  * was obtained from Bluetooth Specification Books (v1.1 and up)
65  *
66  * This file can be included by both kernel and userland applications.
67  */
68 
69 #ifndef _NETBT_L2CAP_H_
70 #define _NETBT_L2CAP_H_
71 
72 /**************************************************************************
73  **************************************************************************
74  **                   Common defines and types (L2CAP)
75  **************************************************************************
76  **************************************************************************/
77 
78 /*
79  * Channel IDs are assigned per machine. So the total number of channels that
80  * a machine can have open at the same time is 0xffff - 0x0040 = 0xffbf (65471).
81  * This number does not depend on number of HCI connections.
82  */
83 
84 #define L2CAP_NULL_CID			0x0000	/* DO NOT USE THIS CID */
85 #define L2CAP_SIGNAL_CID		0x0001	/* signaling channel ID */
86 #define L2CAP_CLT_CID			0x0002	/* connectionless channel ID */
87 	/* 0x0003 - 0x003f Reserved */
88 #define L2CAP_FIRST_CID			0x0040	/* dynamically alloc. (start) */
89 #define L2CAP_LAST_CID			0xffff	/* dynamically alloc. (end) */
90 
91 /* L2CAP MTU */
92 #define L2CAP_MTU_MINIMUM		48
93 #define L2CAP_MTU_DEFAULT		672
94 #define L2CAP_MTU_MAXIMUM		0xffff
95 
96 /* L2CAP flush and link timeouts */
97 #define L2CAP_FLUSH_TIMO_DEFAULT	0xffff /* always retransmit */
98 #define L2CAP_LINK_TIMO_DEFAULT		0xffff
99 
100 /* L2CAP Command Reject reasons */
101 #define L2CAP_REJ_NOT_UNDERSTOOD	0x0000
102 #define L2CAP_REJ_MTU_EXCEEDED		0x0001
103 #define L2CAP_REJ_INVALID_CID		0x0002
104 /* 0x0003 - 0xffff - reserved for future use */
105 
106 /* Protocol/Service Multiplexor (PSM) values */
107 #define L2CAP_PSM_ANY			0x0000	/* Any/Invalid PSM */
108 #define L2CAP_PSM_SDP			0x0001	/* Service Discovery Protocol */
109 #define L2CAP_PSM_RFCOMM		0x0003	/* RFCOMM protocol */
110 #define L2CAP_PSM_TCP			0x0005	/* Telephony Control Protocol */
111 #define L2CAP_PSM_TCS			0x0007	/* TCS cordless */
112 #define L2CAP_PSM_BNEP			0x000f	/* Bluetooth Network */
113 						/*	Encapsulation Protocol*/
114 #define L2CAP_PSM_HID_CNTL		0x0011	/* HID Control */
115 #define L2CAP_PSM_HID_INTR		0x0013	/* HID Interrupt */
116 #define L2CAP_PSM_ESDP			0x0015	/* Extended Service */
117 						/*	Discovery Profile */
118 #define L2CAP_PSM_AVCTP			0x0017	/* Audio/Visual Control */
119 						/*	Transport Protocol */
120 #define L2CAP_PSM_AVDTP			0x0019	/* Audio/Visual Distribution */
121 						/*	Transport Protocol */
122 /* 0x0019 - 0x1000 - reserved for future use */
123 
124 #define L2CAP_PSM_INVALID(psm)		(((psm) & 0x0101) != 0x0001)
125 
126 /* L2CAP Connection response command result codes */
127 #define L2CAP_SUCCESS			0x0000
128 #define L2CAP_PENDING			0x0001
129 #define L2CAP_PSM_NOT_SUPPORTED		0x0002
130 #define L2CAP_SECURITY_BLOCK		0x0003
131 #define L2CAP_NO_RESOURCES		0x0004
132 #define L2CAP_TIMEOUT			0xeeee
133 #define L2CAP_UNKNOWN			0xffff
134 /* 0x0005 - 0xffff - reserved for future use */
135 
136 /* L2CAP Connection response status codes */
137 #define L2CAP_NO_INFO			0x0000
138 #define L2CAP_AUTH_PENDING		0x0001
139 #define L2CAP_AUTZ_PENDING		0x0002
140 /* 0x0003 - 0xffff - reserved for future use */
141 
142 /* L2CAP Configuration response result codes */
143 #define L2CAP_UNACCEPTABLE_PARAMS	0x0001
144 #define L2CAP_REJECT			0x0002
145 #define L2CAP_UNKNOWN_OPTION		0x0003
146 /* 0x0003 - 0xffff - reserved for future use */
147 
148 /* L2CAP Configuration options */
149 #define L2CAP_OPT_CFLAG_BIT		0x0001
150 #define L2CAP_OPT_CFLAG(flags)		((flags) & L2CAP_OPT_CFLAG_BIT)
151 #define L2CAP_OPT_HINT_BIT		0x80
152 #define L2CAP_OPT_HINT(type)		((type) & L2CAP_OPT_HINT_BIT)
153 #define L2CAP_OPT_HINT_MASK		0x7f
154 #define L2CAP_OPT_MTU			0x01
155 #define L2CAP_OPT_MTU_SIZE		sizeof(uint16_t)
156 #define L2CAP_OPT_FLUSH_TIMO		0x02
157 #define L2CAP_OPT_FLUSH_TIMO_SIZE	sizeof(uint16_t)
158 #define L2CAP_OPT_QOS			0x03
159 #define L2CAP_OPT_QOS_SIZE		sizeof(l2cap_qos_t)
160 #define L2CAP_OPT_RFC			0x04
161 #define L2CAP_OPT_RFC_SIZE		sizeof(l2cap_rfc_t)
162 /* 0x05 - 0xff - reserved for future use */
163 
164 /* L2CAP Information request type codes */
165 #define L2CAP_CONNLESS_MTU		0x0001
166 #define L2CAP_EXTENDED_FEATURES		0x0002
167 /* 0x0003 - 0xffff - reserved for future use */
168 
169 /* L2CAP Information response codes */
170 #define L2CAP_NOT_SUPPORTED		0x0001
171 /* 0x0002 - 0xffff - reserved for future use */
172 
173 /* L2CAP Quality of Service option */
174 typedef struct {
175 	uint8_t  flags;			/* reserved for future use */
176 	uint8_t  service_type;		/* service type */
177 	uint32_t token_rate;		/* bytes per second */
178 	uint32_t token_bucket_size;	/* bytes */
179 	uint32_t peak_bandwidth;	/* bytes per second */
180 	uint32_t latency;		/* microseconds */
181 	uint32_t delay_variation;	/* microseconds */
182 } __attribute__ ((__packed__)) l2cap_qos_t;
183 
184 /* L2CAP QoS type */
185 #define L2CAP_QOS_NO_TRAFFIC	0x00
186 #define L2CAP_QOS_BEST_EFFORT	0x01       /* (default) */
187 #define L2CAP_QOS_GUARANTEED	0x02
188 /* 0x03 - 0xff - reserved for future use */
189 
190 /* L2CAP Retransmission & Flow Control option */
191 typedef struct {
192 	uint8_t	mode;		   /* RFC mode */
193 	uint8_t	window_size;	   /* bytes */
194 	uint8_t	max_transmit;	   /* max retransmissions */
195 	uint16_t	retransmit_timo;   /* milliseconds */
196 	uint16_t	monitor_timo;	   /* milliseconds */
197 	uint16_t	max_pdu_size;	   /* bytes */
198 } __attribute__ ((__packed__)) l2cap_rfc_t;
199 
200 /* L2CAP RFC mode */
201 #define L2CAP_RFC_BASIC		0x00	   /* (default) */
202 #define L2CAP_RFC_RETRANSMIT	0x01
203 #define L2CAP_RFC_FLOW		0x02
204 /* 0x03 - 0xff - reserved for future use */
205 
206 /**************************************************************************
207  **************************************************************************
208  **                 Link level defines, headers and types
209  **************************************************************************
210  **************************************************************************/
211 
212 /* L2CAP header */
213 typedef struct {
214 	uint16_t	length;	/* payload size */
215 	uint16_t	dcid;	/* destination channel ID */
216 } __attribute__ ((__packed__)) l2cap_hdr_t;
217 
218 /* L2CAP ConnectionLess Traffic		(dcid == L2CAP_CLT_CID) */
219 typedef struct {
220 	uint16_t	psm; /* Protocol/Service Multiplexor */
221 } __attribute__ ((__packed__)) l2cap_clt_hdr_t;
222 
223 #define L2CAP_CLT_MTU_MAXIMUM \
224 	(L2CAP_MTU_MAXIMUM - sizeof(l2cap_clt_hdr_t))
225 
226 /* L2CAP Command header			(dcid == L2CAP_SIGNAL_CID) */
227 typedef struct {
228 	uint8_t	code;   /* command OpCode */
229 	uint8_t	ident;  /* identifier to match request and response */
230 	uint16_t	length; /* command parameters length */
231 } __attribute__ ((__packed__)) l2cap_cmd_hdr_t;
232 
233 /* L2CAP Command Reject */
234 #define L2CAP_COMMAND_REJ			0x01
235 typedef struct {
236 	uint16_t	reason; /* reason to reject command */
237 	uint16_t	data[2];/* optional data */
238 } __attribute__ ((__packed__)) l2cap_cmd_rej_cp;
239 
240 /* L2CAP Connection Request */
241 #define L2CAP_CONNECT_REQ			0x02
242 typedef struct {
243 	uint16_t	psm;  /* Protocol/Service Multiplexor (PSM) */
244 	uint16_t	scid; /* source channel ID */
245 } __attribute__ ((__packed__)) l2cap_con_req_cp;
246 
247 /* L2CAP Connection Response */
248 #define L2CAP_CONNECT_RSP			0x03
249 typedef struct {
250 	uint16_t	dcid;   /* destination channel ID */
251 	uint16_t	scid;   /* source channel ID */
252 	uint16_t	result; /* 0x00 - success */
253 	uint16_t	status; /* more info if result != 0x00 */
254 } __attribute__ ((__packed__)) l2cap_con_rsp_cp;
255 
256 /* L2CAP Configuration Request */
257 #define L2CAP_CONFIG_REQ			0x04
258 typedef struct {
259 	uint16_t	dcid;  /* destination channel ID */
260 	uint16_t	flags; /* flags */
261 /*	uint8_t	options[] --  options */
262 } __attribute__ ((__packed__)) l2cap_cfg_req_cp;
263 
264 /* L2CAP Configuration Response */
265 #define L2CAP_CONFIG_RSP			0x05
266 typedef struct {
267 	uint16_t	scid;   /* source channel ID */
268 	uint16_t	flags;  /* flags */
269 	uint16_t	result; /* 0x00 - success */
270 /*	uint8_t	options[] -- options */
271 } __attribute__ ((__packed__)) l2cap_cfg_rsp_cp;
272 
273 /* L2CAP configuration option */
274 typedef struct {
275 	uint8_t	type;
276 	uint8_t	length;
277 /*	uint8_t	value[] -- option value (depends on type) */
278 } __attribute__ ((__packed__)) l2cap_cfg_opt_t;
279 
280 /* L2CAP configuration option value */
281 typedef union {
282 	uint16_t		mtu;		/* L2CAP_OPT_MTU */
283 	uint16_t		flush_timo;	/* L2CAP_OPT_FLUSH_TIMO */
284 	l2cap_qos_t		qos;		/* L2CAP_OPT_QOS */
285 	l2cap_rfc_t		rfc;		/* L2CAP_OPT_RFC */
286 } l2cap_cfg_opt_val_t;
287 
288 /* L2CAP Disconnect Request */
289 #define L2CAP_DISCONNECT_REQ			0x06
290 typedef struct {
291 	uint16_t	dcid; /* destination channel ID */
292 	uint16_t	scid; /* source channel ID */
293 } __attribute__ ((__packed__)) l2cap_discon_req_cp;
294 
295 /* L2CAP Disconnect Response */
296 #define L2CAP_DISCONNECT_RSP			0x07
297 typedef l2cap_discon_req_cp	l2cap_discon_rsp_cp;
298 
299 /* L2CAP Echo Request */
300 #define L2CAP_ECHO_REQ				0x08
301 /* No command parameters, only optional data */
302 
303 /* L2CAP Echo Response */
304 #define L2CAP_ECHO_RSP				0x09
305 #define L2CAP_MAX_ECHO_SIZE \
306 	(L2CAP_MTU_MAXIMUM - sizeof(l2cap_cmd_hdr_t))
307 /* No command parameters, only optional data */
308 
309 /* L2CAP Information Request */
310 #define L2CAP_INFO_REQ				0x0a
311 typedef struct {
312 	uint16_t	type; /* requested information type */
313 } __attribute__ ((__packed__)) l2cap_info_req_cp;
314 
315 /* L2CAP Information Response */
316 #define L2CAP_INFO_RSP				0x0b
317 typedef struct {
318 	uint16_t	type;   /* requested information type */
319 	uint16_t	result; /* 0x00 - success */
320 /*	uint8_t	info[]  -- info data (depends on type)
321  *
322  * L2CAP_CONNLESS_MTU - 2 bytes connectionless MTU
323  */
324 } __attribute__ ((__packed__)) l2cap_info_rsp_cp;
325 
326 typedef union {
327 	/* L2CAP_CONNLESS_MTU */
328 	struct {
329 		uint16_t	mtu;
330 	} __attribute__ ((__packed__)) mtu;
331 } l2cap_info_rsp_data_t;
332 
333 /**************************************************************************
334  **************************************************************************
335  **		L2CAP Socket Definitions
336  **************************************************************************
337  **************************************************************************/
338 
339 #define SO_L2CAP_IMTU		1
340 #define SO_L2CAP_OMTU		2
341 #define SO_L2CAP_IQOS		3
342 #define SO_L2CAP_OQOS		4
343 #define SO_L2CAP_FLUSH		5
344 
345 #ifdef _KERNEL
346 
347 LIST_HEAD(l2cap_channel_list, l2cap_channel);
348 
349 /* global variables */
350 extern struct l2cap_channel_list l2cap_active_list;
351 extern struct l2cap_channel_list l2cap_listen_list;
352 extern struct pool l2cap_pdu_pool;
353 extern struct pool l2cap_req_pool;
354 extern const l2cap_qos_t l2cap_default_qos;
355 
356 /* sysctl variables */
357 extern int l2cap_response_timeout;
358 extern int l2cap_response_extended_timeout;
359 extern int l2cap_sendspace, l2cap_recvspace;
360 
361 /*
362  * L2CAP Channel
363  */
364 struct l2cap_channel {
365 	struct hci_link		*lc_link;	/* ACL connection (down) */
366 	uint16_t		 lc_state;	/* channel state */
367 	uint16_t		 lc_flags;	/* channel flags */
368 
369 	uint16_t		 lc_lcid;	/* local channel ID */
370 	struct sockaddr_bt	 lc_laddr;	/* local address */
371 
372 	uint16_t		 lc_rcid;	/* remote channel ID */
373 	struct sockaddr_bt	 lc_raddr;	/* remote address */
374 
375 	uint16_t		 lc_imtu;	/* incoming mtu */
376 	uint16_t		 lc_omtu;	/* outgoing mtu */
377 	uint16_t		 lc_flush;	/* flush timeout */
378 	l2cap_qos_t		 lc_iqos;	/* incoming QoS flow control */
379 	l2cap_qos_t		 lc_oqos;	/* outgoing Qos flow control */
380 
381 	uint8_t			 lc_pending;	/* num of pending PDUs */
382 	MBUFQ_HEAD()		 lc_txq;	/* transmit queue */
383 
384 	const struct btproto	*lc_proto;	/* upper layer callbacks */
385 	void			*lc_upper;	/* upper layer argument */
386 
387 	LIST_ENTRY(l2cap_channel)lc_ncid;	/* next channel (ascending CID) */
388 };
389 
390 /* l2cap_channel state bits */
391 #define L2CAP_CLOSED		(1<<0)	/* closed */
392 #define L2CAP_WAIT_CONNECT	(1<<1)	/* have received connect request */
393 #define L2CAP_WAIT_CONNECT_RSP	(1<<2)	/* have sent connect request */
394 #define L2CAP_WAIT_CONFIG_REQ	(1<<3)	/* waiting for config request */
395 #define L2CAP_WAIT_CONFIG_RSP	(1<<4)	/* waiting for config response */
396 #define L2CAP_OPEN		(1<<5)	/* user data transfer state */
397 #define L2CAP_WAIT_DISCONNECT	(1<<6)	/* have sent disconnect request */
398 
399 /* l2cap_channel flags */
400 #define L2CAP_SHUTDOWN		(1<<0)	/* channel is closing */
401 
402 /*
403  * L2CAP Request
404  */
405 struct l2cap_req {
406 	struct hci_link		*lr_link;	/* ACL connection */
407 	struct l2cap_channel	*lr_chan;	/* channel pointer */
408 	uint8_t			 lr_code;	/* request code */
409 	uint8_t			 lr_id;		/* request id */
410 	struct callout		 lr_rtx;	/* response timer */
411 	TAILQ_ENTRY(l2cap_req)	 lr_next;	/* next request on link */
412 };
413 
414 /*
415  * L2CAP Protocol Data Unit
416  */
417 struct l2cap_pdu {
418 	struct l2cap_channel	*lp_chan;	/* PDU owner */
419 	MBUFQ_HEAD()		 lp_data;	/* PDU data */
420 	TAILQ_ENTRY(l2cap_pdu)	 lp_next;	/* next PDU on link */
421 	int			 lp_pending;	/* # of fragments pending */
422 };
423 
424 /*
425  * L2CAP function prototypes
426  */
427 
428 struct socket;
429 struct mbuf;
430 
431 /* l2cap_lower.c */
432 void l2cap_acl_open_cfm(struct l2cap_channel *, int);
433 void l2cap_close(struct l2cap_channel *, int);
434 void l2cap_recv_frame(struct mbuf *, struct hci_link *);
435 int l2cap_start(struct l2cap_channel *);
436 
437 /* l2cap_misc.c */
438 int l2cap_cid_alloc(struct l2cap_channel *);
439 struct l2cap_channel *l2cap_cid_lookup(uint16_t);
440 int l2cap_request_alloc(struct l2cap_channel *, uint8_t);
441 struct l2cap_req *l2cap_request_lookup(struct hci_link *, uint8_t);
442 void l2cap_request_free(struct l2cap_req *);
443 void l2cap_rtx(void *);
444 
445 /* l2cap_signal.c */
446 void l2cap_recv_signal(struct mbuf *, struct hci_link *);
447 int l2cap_send_connect_req(struct l2cap_channel *);
448 int l2cap_send_config_req(struct l2cap_channel *);
449 int l2cap_send_disconnect_req(struct l2cap_channel *);
450 
451 /* l2cap_socket.c */
452 int l2cap_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *, struct lwp *);
453 int l2cap_ctloutput(int, struct socket *, int, int, struct mbuf **);
454 
455 /* l2cap_upper.c */
456 int l2cap_attach(struct l2cap_channel **, const struct btproto *, void *);
457 int l2cap_bind(struct l2cap_channel *, struct sockaddr_bt *);
458 int l2cap_sockaddr(struct l2cap_channel *, struct sockaddr_bt *);
459 int l2cap_connect(struct l2cap_channel *, struct sockaddr_bt *);
460 int l2cap_peeraddr(struct l2cap_channel *, struct sockaddr_bt *);
461 int l2cap_disconnect(struct l2cap_channel *, int);
462 int l2cap_detach(struct l2cap_channel **);
463 int l2cap_listen(struct l2cap_channel *);
464 int l2cap_send(struct l2cap_channel *, struct mbuf *);
465 int l2cap_setopt(struct l2cap_channel *, int, void *);
466 int l2cap_getopt(struct l2cap_channel *, int, void *);
467 
468 #endif	/* _KERNEL */
469 
470 #endif	/* _NETBT_L2CAP_H_ */
471