1/* $NetBSD: l2cap.h,v 1.19 2015/11/28 07:50:37 plunky 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.19 2015/11/28 07:50:37 plunky 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#include <sys/types.h>
73
74/**************************************************************************
75 **************************************************************************
76 ** Common defines and types (L2CAP)
77 **************************************************************************
78 **************************************************************************/
79
80/*
81 * Channel IDs are assigned per machine. So the total number of channels that
82 * a machine can have open at the same time is 0xffff - 0x0040 = 0xffbf (65471).
83 * This number does not depend on number of HCI connections.
84 */
85
86#define L2CAP_NULL_CID 0x0000 /* DO NOT USE THIS CID */
87#define L2CAP_SIGNAL_CID 0x0001 /* signaling channel ID */
88#define L2CAP_CLT_CID 0x0002 /* connectionless channel ID */
89 /* 0x0003 - 0x003f Reserved */
90#define L2CAP_FIRST_CID 0x0040 /* dynamically alloc. (start) */
91#define L2CAP_LAST_CID 0xffff /* dynamically alloc. (end) */
92
93/* L2CAP MTU */
94#define L2CAP_MTU_MINIMUM 48
95#define L2CAP_MTU_DEFAULT 672
96#define L2CAP_MTU_MAXIMUM 0xffff
97
98/* L2CAP flush and link timeouts */
99#define L2CAP_FLUSH_TIMO_DEFAULT 0xffff /* always retransmit */
100#define L2CAP_LINK_TIMO_DEFAULT 0xffff
101
102/* L2CAP Command Reject reasons */
103#define L2CAP_REJ_NOT_UNDERSTOOD 0x0000
104#define L2CAP_REJ_MTU_EXCEEDED 0x0001
105#define L2CAP_REJ_INVALID_CID 0x0002
106/* 0x0003 - 0xffff - reserved for future use */
107
108/* Protocol/Service Multiplexer (PSM) values */
109#define L2CAP_PSM_ANY 0x0000 /* Any/Invalid PSM */
110#define L2CAP_PSM_SDP 0x0001 /* Service Discovery Protocol */
111#define L2CAP_PSM_RFCOMM 0x0003 /* RFCOMM protocol */
112#define L2CAP_PSM_TCP 0x0005 /* Telephony Control Protocol */
113#define L2CAP_PSM_TCS 0x0007 /* TCS cordless */
114#define L2CAP_PSM_BNEP 0x000f /* Bluetooth Network */
115 /* Encapsulation Protocol*/
116#define L2CAP_PSM_HID_CNTL 0x0011 /* HID Control */
117#define L2CAP_PSM_HID_INTR 0x0013 /* HID Interrupt */
118#define L2CAP_PSM_ESDP 0x0015 /* Extended Service */
119 /* Discovery Profile */
120#define L2CAP_PSM_AVCTP 0x0017 /* Audio/Visual Control */
121 /* Transport Protocol */
122#define L2CAP_PSM_AVDTP 0x0019 /* Audio/Visual Distribution */
123 /* Transport Protocol */
124#define L2CAP_PSM_UDI_C_PLANE 0x001d /* Unrestricted Digital */
125 /* Information Profile */
126#define L2CAP_PSM_ATT 0x001f /* Attribute Protocol */
127#define L2CAP_PSM_3DSP 0x0021 /* 3D Synchronization Profile */
128#define L2CAP_PSM_IPSP 0x0023 /* Internet Protocol */
129 /* Support Profile */
130/* 0x0025 - 0x1000 - reserved for future use */
131
132#define L2CAP_PSM_INVALID(psm) (((psm) & 0x0101) != 0x0001)
133
134/* L2CAP Connection response command result codes */
135#define L2CAP_SUCCESS 0x0000
136#define L2CAP_PENDING 0x0001
137#define L2CAP_PSM_NOT_SUPPORTED 0x0002
138#define L2CAP_SECURITY_BLOCK 0x0003
139#define L2CAP_NO_RESOURCES 0x0004
140#define L2CAP_TIMEOUT 0xeeee
141#define L2CAP_UNKNOWN 0xffff
142/* 0x0005 - 0xffff - reserved for future use */
143
144/* L2CAP Connection response status codes */
145#define L2CAP_NO_INFO 0x0000
146#define L2CAP_AUTH_PENDING 0x0001
147#define L2CAP_AUTZ_PENDING 0x0002
148/* 0x0003 - 0xffff - reserved for future use */
149
150/* L2CAP Configuration response result codes */
151#define L2CAP_UNACCEPTABLE_PARAMS 0x0001
152#define L2CAP_REJECT 0x0002
153#define L2CAP_UNKNOWN_OPTION 0x0003
154/* 0x0003 - 0xffff - reserved for future use */
155
156/* L2CAP Configuration options */
157#define L2CAP_OPT_CFLAG_BIT 0x0001
158#define L2CAP_OPT_CFLAG(flags) ((flags) & L2CAP_OPT_CFLAG_BIT)
159#define L2CAP_OPT_HINT_BIT 0x80
160#define L2CAP_OPT_HINT(type) ((type) & L2CAP_OPT_HINT_BIT)
161#define L2CAP_OPT_HINT_MASK 0x7f
162#define L2CAP_OPT_MTU 0x01
163#define L2CAP_OPT_MTU_SIZE sizeof(uint16_t)
164#define L2CAP_OPT_FLUSH_TIMO 0x02
165#define L2CAP_OPT_FLUSH_TIMO_SIZE sizeof(uint16_t)
166#define L2CAP_OPT_QOS 0x03
167#define L2CAP_OPT_QOS_SIZE sizeof(l2cap_qos_t)
168#define L2CAP_OPT_RFC 0x04
169#define L2CAP_OPT_RFC_SIZE sizeof(l2cap_rfc_t)
170/* 0x05 - 0xff - reserved for future use */
171
172/* L2CAP Information request type codes */
173#define L2CAP_CONNLESS_MTU 0x0001
174#define L2CAP_EXTENDED_FEATURES 0x0002
175#define L2CAP_FIXED_CHANNELS 0x0003
176/* 0x0004 - 0xffff - reserved for future use */
177
178/* L2CAP Information response codes */
179#define L2CAP_NOT_SUPPORTED 0x0001
180/* 0x0002 - 0xffff - reserved for future use */
181
182/* L2CAP Quality of Service option */
183typedef struct {
184 uint8_t flags; /* reserved for future use */
185 uint8_t service_type; /* service type */
186 uint32_t token_rate; /* bytes per second */
187 uint32_t token_bucket_size; /* bytes */
188 uint32_t peak_bandwidth; /* bytes per second */
189 uint32_t latency; /* microseconds */
190 uint32_t delay_variation; /* microseconds */
191} __packed l2cap_qos_t;
192
193/* L2CAP QoS type */
194#define L2CAP_QOS_NO_TRAFFIC 0x00
195#define L2CAP_QOS_BEST_EFFORT 0x01 /* (default) */
196#define L2CAP_QOS_GUARANTEED 0x02
197/* 0x03 - 0xff - reserved for future use */
198
199/* L2CAP Retransmission & Flow Control option */
200typedef struct {
201 uint8_t mode; /* RFC mode */
202 uint8_t window_size; /* bytes */
203 uint8_t max_transmit; /* max retransmissions */
204 uint16_t retransmit_timo; /* milliseconds */
205 uint16_t monitor_timo; /* milliseconds */
206 uint16_t max_pdu_size; /* bytes */
207} __packed l2cap_rfc_t;
208
209/* L2CAP RFC mode */
210#define L2CAP_RFC_BASIC 0x00 /* (default) */
211#define L2CAP_RFC_RETRANSMIT 0x01
212#define L2CAP_RFC_FLOW 0x02
213/* 0x03 - 0xff - reserved for future use */
214
215/**************************************************************************
216 **************************************************************************
217 ** Link level defines, headers and types
218 **************************************************************************
219 **************************************************************************/
220
221/* L2CAP header */
222typedef struct {
223 uint16_t length; /* payload size */
224 uint16_t dcid; /* destination channel ID */
225} __packed l2cap_hdr_t;
226
227/* L2CAP ConnectionLess Traffic (dcid == L2CAP_CLT_CID) */
228typedef struct {
229 uint16_t psm; /* Protocol/Service Multiplexer */
230} __packed l2cap_clt_hdr_t;
231
232#define L2CAP_CLT_MTU_MAXIMUM \
233 (L2CAP_MTU_MAXIMUM - sizeof(l2cap_clt_hdr_t))
234
235/* L2CAP Command header (dcid == L2CAP_SIGNAL_CID) */
236typedef struct {
237 uint8_t code; /* command OpCode */
238 uint8_t ident; /* identifier to match request and response */
239 uint16_t length; /* command parameters length */
240} __packed l2cap_cmd_hdr_t;
241
242/* L2CAP Command Reject */
243#define L2CAP_COMMAND_REJ 0x01
244typedef struct {
245 uint16_t reason; /* reason to reject command */
246 uint16_t data[2];/* optional data */
247} __packed l2cap_cmd_rej_cp;
248
249/* L2CAP Connection Request */
250#define L2CAP_CONNECT_REQ 0x02
251typedef struct {
252 uint16_t psm; /* Protocol/Service Multiplexer */
253 uint16_t scid; /* source channel ID */
254} __packed l2cap_con_req_cp;
255
256/* L2CAP Connection Response */
257#define L2CAP_CONNECT_RSP 0x03
258typedef struct {
259 uint16_t dcid; /* destination channel ID */
260 uint16_t scid; /* source channel ID */
261 uint16_t result; /* 0x00 - success */
262 uint16_t status; /* more info if result != 0x00 */
263} __packed l2cap_con_rsp_cp;
264
265/* L2CAP Configuration Request */
266#define L2CAP_CONFIG_REQ 0x04
267typedef struct {
268 uint16_t dcid; /* destination channel ID */
269 uint16_t flags; /* flags */
270/* uint8_t options[] -- options */
271} __packed l2cap_cfg_req_cp;
272
273/* L2CAP Configuration Response */
274#define L2CAP_CONFIG_RSP 0x05
275typedef struct {
276 uint16_t scid; /* source channel ID */
277 uint16_t flags; /* flags */
278 uint16_t result; /* 0x00 - success */
279/* uint8_t options[] -- options */
280} __packed l2cap_cfg_rsp_cp;
281
282/* L2CAP configuration option */
283typedef struct {
284 uint8_t type;
285 uint8_t length;
286/* uint8_t value[] -- option value (depends on type) */
287} __packed l2cap_cfg_opt_t;
288
289/* L2CAP configuration option value */
290typedef union {
291 uint16_t mtu; /* L2CAP_OPT_MTU */
292 uint16_t flush_timo; /* L2CAP_OPT_FLUSH_TIMO */
293 l2cap_qos_t qos; /* L2CAP_OPT_QOS */
294 l2cap_rfc_t rfc; /* L2CAP_OPT_RFC */
295} l2cap_cfg_opt_val_t;
296
297/* L2CAP Disconnect Request */
298#define L2CAP_DISCONNECT_REQ 0x06
299typedef struct {
300 uint16_t dcid; /* destination channel ID */
301 uint16_t scid; /* source channel ID */
302} __packed l2cap_discon_req_cp;
303
304/* L2CAP Disconnect Response */
305#define L2CAP_DISCONNECT_RSP 0x07
306typedef l2cap_discon_req_cp l2cap_discon_rsp_cp;
307
308/* L2CAP Echo Request */
309#define L2CAP_ECHO_REQ 0x08
310/* No command parameters, only optional data */
311
312/* L2CAP Echo Response */
313#define L2CAP_ECHO_RSP 0x09
314#define L2CAP_MAX_ECHO_SIZE \
315 (L2CAP_MTU_MAXIMUM - sizeof(l2cap_cmd_hdr_t))
316/* No command parameters, only optional data */
317
318/* L2CAP Information Request */
319#define L2CAP_INFO_REQ 0x0a
320typedef struct {
321 uint16_t type; /* requested information type */
322} __packed l2cap_info_req_cp;
323
324/* L2CAP Information Response */
325#define L2CAP_INFO_RSP 0x0b
326typedef struct {
327 uint16_t type; /* requested information type */
328 uint16_t result; /* 0x00 - success */
329/* uint8_t info[] -- info data (depends on type)
330 */
331} __packed l2cap_info_rsp_cp;
332
333
334/**************************************************************************
335 **************************************************************************
336 ** L2CAP Socket Definitions
337 **************************************************************************
338 **************************************************************************/
339
340/* Socket options */
341#define SO_L2CAP_IMTU 1 /* incoming MTU */
342#define SO_L2CAP_OMTU 2 /* outgoing MTU */
343#define SO_L2CAP_IQOS 3 /* incoming QoS */
344#define SO_L2CAP_OQOS 4 /* outgoing QoS */
345#define SO_L2CAP_FLUSH 5 /* flush timeout */
346#define SO_L2CAP_LM 6 /* link mode */
347
348/* L2CAP link mode flags */
349#define L2CAP_LM_AUTH (1<<0) /* want authentication */
350#define L2CAP_LM_ENCRYPT (1<<1) /* want encryption */
351#define L2CAP_LM_SECURE (1<<2) /* want secured link */
352
353#ifdef _KERNEL
354
355LIST_HEAD(l2cap_channel_list, l2cap_channel);
356
357/* global variables */
358extern struct l2cap_channel_list l2cap_active_list;
359extern struct l2cap_channel_list l2cap_listen_list;
360extern struct pool l2cap_pdu_pool;
361extern struct pool l2cap_req_pool;
362extern const l2cap_qos_t l2cap_default_qos;
363
364/* sysctl variables */
365extern int l2cap_response_timeout;
366extern int l2cap_response_extended_timeout;
367extern int l2cap_sendspace, l2cap_recvspace;
368
369/*
370 * L2CAP Channel
371 */
372struct l2cap_channel {
373 struct hci_link *lc_link; /* ACL connection (down) */
374 uint16_t lc_state; /* channel state */
375 uint16_t lc_flags; /* channel flags */
376 uint8_t lc_ident; /* cached request id */
377
378 uint16_t lc_lcid; /* local channel ID */
379 struct sockaddr_bt lc_laddr; /* local address */
380
381 uint16_t lc_rcid; /* remote channel ID */
382 struct sockaddr_bt lc_raddr; /* remote address */
383
384 int lc_mode; /* link mode */
385 uint16_t lc_imtu; /* incoming mtu */
386 uint16_t lc_omtu; /* outgoing mtu */
387 uint16_t lc_flush; /* flush timeout */
388 l2cap_qos_t lc_iqos; /* incoming QoS flow control */
389 l2cap_qos_t lc_oqos; /* outgoing Qos flow control */
390
391 uint8_t lc_pending; /* num of pending PDUs */
392 MBUFQ_HEAD() lc_txq; /* transmit queue */
393
394 const struct btproto *lc_proto; /* upper layer callbacks */
395 void *lc_upper; /* upper layer argument */
396
397 LIST_ENTRY(l2cap_channel)lc_ncid; /* next channel (ascending CID) */
398};
399
400/* l2cap_channel state */
401#define L2CAP_CLOSED 0 /* closed */
402#define L2CAP_WAIT_SEND_CONNECT_REQ 1 /* waiting to send connect request */
403#define L2CAP_WAIT_RECV_CONNECT_RSP 2 /* waiting to recv connect response */
404#define L2CAP_WAIT_SEND_CONNECT_RSP 3 /* waiting to send connect response */
405#define L2CAP_WAIT_CONFIG 4 /* waiting for configuration */
406#define L2CAP_OPEN 5 /* user data transfer state */
407#define L2CAP_WAIT_DISCONNECT 6 /* have sent disconnect request */
408
409/* l2cap_channel flags */
410#define L2CAP_SHUTDOWN (1<<0) /* channel is closing */
411#define L2CAP_WAIT_CONFIG_REQ (1<<1) /* waiting for config request */
412#define L2CAP_WAIT_CONFIG_RSP (1<<2) /* waiting for config response */
413
414/*
415 * L2CAP Request
416 */
417struct l2cap_req {
418 struct hci_link *lr_link; /* ACL connection */
419 struct l2cap_channel *lr_chan; /* channel pointer */
420 uint8_t lr_code; /* request code */
421 uint8_t lr_id; /* request id */
422 callout_t lr_rtx; /* response timer */
423 TAILQ_ENTRY(l2cap_req) lr_next; /* next request on link */
424};
425
426/*
427 * L2CAP Protocol Data Unit
428 */
429struct l2cap_pdu {
430 struct l2cap_channel *lp_chan; /* PDU owner */
431 MBUFQ_HEAD() lp_data; /* PDU data */
432 TAILQ_ENTRY(l2cap_pdu) lp_next; /* next PDU on link */
433 int lp_pending; /* # of fragments pending */
434};
435
436/*
437 * L2CAP function prototypes
438 */
439
440struct socket;
441struct sockopt;
442struct mbuf;
443
444/* l2cap_lower.c */
445void l2cap_close(struct l2cap_channel *, int);
446void l2cap_recv_frame(struct mbuf *, struct hci_link *);
447int l2cap_start(struct l2cap_channel *);
448
449/* l2cap_misc.c */
450int l2cap_setmode(struct l2cap_channel *);
451int l2cap_cid_alloc(struct l2cap_channel *);
452struct l2cap_channel *l2cap_cid_lookup(uint16_t);
453int l2cap_request_alloc(struct l2cap_channel *, uint8_t);
454struct l2cap_req *l2cap_request_lookup(struct hci_link *, uint8_t);
455void l2cap_request_free(struct l2cap_req *);
456void l2cap_rtx(void *);
457void l2cap_init(void);
458
459/* l2cap_signal.c */
460void l2cap_recv_signal(struct mbuf *, struct hci_link *);
461int l2cap_send_connect_req(struct l2cap_channel *);
462int l2cap_send_config_req(struct l2cap_channel *);
463int l2cap_send_disconnect_req(struct l2cap_channel *);
464int l2cap_send_connect_rsp(struct hci_link *, uint8_t, uint16_t, uint16_t, uint16_t);
465
466/* l2cap_socket.c */
467int l2cap_ctloutput(int, struct socket *, struct sockopt *);
468
469/* l2cap_upper.c */
470int l2cap_attach_pcb(struct l2cap_channel **, const struct btproto *, void *);
471int l2cap_bind_pcb(struct l2cap_channel *, struct sockaddr_bt *);
472int l2cap_sockaddr_pcb(struct l2cap_channel *, struct sockaddr_bt *);
473int l2cap_connect_pcb(struct l2cap_channel *, struct sockaddr_bt *);
474int l2cap_peeraddr_pcb(struct l2cap_channel *, struct sockaddr_bt *);
475int l2cap_disconnect_pcb(struct l2cap_channel *, int);
476void l2cap_detach_pcb(struct l2cap_channel **);
477int l2cap_listen_pcb(struct l2cap_channel *);
478int l2cap_send_pcb(struct l2cap_channel *, struct mbuf *);
479int l2cap_setopt(struct l2cap_channel *, const struct sockopt *);
480int l2cap_getopt(struct l2cap_channel *, struct sockopt *);
481
482#endif /* _KERNEL */
483
484#endif /* _NETBT_L2CAP_H_ */
485