1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 BlueZ - Bluetooth protocol stack for Linux
4 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
6 Copyright (C) 2010 Google Inc.
7
8 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
9
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
22 */
23
24 #ifndef __L2CAP_H
25 #define __L2CAP_H
26
27 #include <linux/unaligned.h>
28 #include <linux/atomic.h>
29
30 /* L2CAP defaults */
31 #define L2CAP_DEFAULT_MTU 672
32 #define L2CAP_DEFAULT_MIN_MTU 48
33 #define L2CAP_SIG_MTU 48 /* BR/EDR signaling MTU */
34 #define L2CAP_DEFAULT_FLUSH_TO 0xFFFF
35 #define L2CAP_EFS_DEFAULT_FLUSH_TO 0xFFFFFFFF
36 #define L2CAP_DEFAULT_TX_WINDOW 63
37 #define L2CAP_DEFAULT_EXT_WINDOW 0x3FFF
38 #define L2CAP_DEFAULT_MAX_TX 3
39 #define L2CAP_DEFAULT_RETRANS_TO 2000 /* 2 seconds */
40 #define L2CAP_DEFAULT_MONITOR_TO 12000 /* 12 seconds */
41 #define L2CAP_DEFAULT_MAX_PDU_SIZE 1492 /* Sized for AMP packet */
42 #define L2CAP_DEFAULT_ACK_TO 200
43 #define L2CAP_DEFAULT_MAX_SDU_SIZE 0xFFFF
44 #define L2CAP_DEFAULT_SDU_ITIME 0xFFFFFFFF
45 #define L2CAP_DEFAULT_ACC_LAT 0xFFFFFFFF
46 #define L2CAP_BREDR_MAX_PAYLOAD 1019 /* 3-DH5 packet */
47 #define L2CAP_LE_MIN_MTU 23
48 #define L2CAP_ECRED_CONN_SCID_MAX 5
49
50 #define L2CAP_DISC_TIMEOUT msecs_to_jiffies(100)
51 #define L2CAP_DISC_REJ_TIMEOUT msecs_to_jiffies(5000)
52 #define L2CAP_ENC_TIMEOUT msecs_to_jiffies(5000)
53 #define L2CAP_CONN_TIMEOUT msecs_to_jiffies(40000)
54 #define L2CAP_INFO_TIMEOUT msecs_to_jiffies(4000)
55 #define L2CAP_MOVE_TIMEOUT msecs_to_jiffies(4000)
56 #define L2CAP_MOVE_ERTX_TIMEOUT msecs_to_jiffies(60000)
57 #define L2CAP_WAIT_ACK_POLL_PERIOD msecs_to_jiffies(200)
58 #define L2CAP_WAIT_ACK_TIMEOUT msecs_to_jiffies(10000)
59
60 /* L2CAP socket address */
61 struct sockaddr_l2 {
62 sa_family_t l2_family;
63 __le16 l2_psm;
64 bdaddr_t l2_bdaddr;
65 __le16 l2_cid;
66 __u8 l2_bdaddr_type;
67 };
68
69 /* L2CAP socket options */
70 #define L2CAP_OPTIONS 0x01
71 struct l2cap_options {
72 __u16 omtu;
73 __u16 imtu;
74 __u16 flush_to;
75 __u8 mode;
76 __u8 fcs;
77 __u8 max_tx;
78 __u16 txwin_size;
79 };
80
81 #define L2CAP_CONNINFO 0x02
82 struct l2cap_conninfo {
83 __u16 hci_handle;
84 __u8 dev_class[3];
85 };
86
87 #define L2CAP_LM 0x03
88 #define L2CAP_LM_MASTER 0x0001
89 #define L2CAP_LM_AUTH 0x0002
90 #define L2CAP_LM_ENCRYPT 0x0004
91 #define L2CAP_LM_TRUSTED 0x0008
92 #define L2CAP_LM_RELIABLE 0x0010
93 #define L2CAP_LM_SECURE 0x0020
94 #define L2CAP_LM_FIPS 0x0040
95
96 /* L2CAP command codes */
97 #define L2CAP_COMMAND_REJ 0x01
98 #define L2CAP_CONN_REQ 0x02
99 #define L2CAP_CONN_RSP 0x03
100 #define L2CAP_CONF_REQ 0x04
101 #define L2CAP_CONF_RSP 0x05
102 #define L2CAP_DISCONN_REQ 0x06
103 #define L2CAP_DISCONN_RSP 0x07
104 #define L2CAP_ECHO_REQ 0x08
105 #define L2CAP_ECHO_RSP 0x09
106 #define L2CAP_INFO_REQ 0x0a
107 #define L2CAP_INFO_RSP 0x0b
108 #define L2CAP_CONN_PARAM_UPDATE_REQ 0x12
109 #define L2CAP_CONN_PARAM_UPDATE_RSP 0x13
110 #define L2CAP_LE_CONN_REQ 0x14
111 #define L2CAP_LE_CONN_RSP 0x15
112 #define L2CAP_LE_CREDITS 0x16
113 #define L2CAP_ECRED_CONN_REQ 0x17
114 #define L2CAP_ECRED_CONN_RSP 0x18
115 #define L2CAP_ECRED_RECONF_REQ 0x19
116 #define L2CAP_ECRED_RECONF_RSP 0x1a
117
118 /* L2CAP extended feature mask */
119 #define L2CAP_FEAT_FLOWCTL 0x00000001
120 #define L2CAP_FEAT_RETRANS 0x00000002
121 #define L2CAP_FEAT_BIDIR_QOS 0x00000004
122 #define L2CAP_FEAT_ERTM 0x00000008
123 #define L2CAP_FEAT_STREAMING 0x00000010
124 #define L2CAP_FEAT_FCS 0x00000020
125 #define L2CAP_FEAT_EXT_FLOW 0x00000040
126 #define L2CAP_FEAT_FIXED_CHAN 0x00000080
127 #define L2CAP_FEAT_EXT_WINDOW 0x00000100
128 #define L2CAP_FEAT_UCD 0x00000200
129
130 /* L2CAP checksum option */
131 #define L2CAP_FCS_NONE 0x00
132 #define L2CAP_FCS_CRC16 0x01
133
134 /* L2CAP fixed channels */
135 #define L2CAP_FC_SIG_BREDR 0x02
136 #define L2CAP_FC_CONNLESS 0x04
137 #define L2CAP_FC_ATT 0x10
138 #define L2CAP_FC_SIG_LE 0x20
139 #define L2CAP_FC_SMP_LE 0x40
140 #define L2CAP_FC_SMP_BREDR 0x80
141
142 /* L2CAP Control Field bit masks */
143 #define L2CAP_CTRL_SAR 0xC000
144 #define L2CAP_CTRL_REQSEQ 0x3F00
145 #define L2CAP_CTRL_TXSEQ 0x007E
146 #define L2CAP_CTRL_SUPERVISE 0x000C
147
148 #define L2CAP_CTRL_RETRANS 0x0080
149 #define L2CAP_CTRL_FINAL 0x0080
150 #define L2CAP_CTRL_POLL 0x0010
151 #define L2CAP_CTRL_FRAME_TYPE 0x0001 /* I- or S-Frame */
152
153 #define L2CAP_CTRL_TXSEQ_SHIFT 1
154 #define L2CAP_CTRL_SUPER_SHIFT 2
155 #define L2CAP_CTRL_POLL_SHIFT 4
156 #define L2CAP_CTRL_FINAL_SHIFT 7
157 #define L2CAP_CTRL_REQSEQ_SHIFT 8
158 #define L2CAP_CTRL_SAR_SHIFT 14
159
160 /* L2CAP Extended Control Field bit mask */
161 #define L2CAP_EXT_CTRL_TXSEQ 0xFFFC0000
162 #define L2CAP_EXT_CTRL_SAR 0x00030000
163 #define L2CAP_EXT_CTRL_SUPERVISE 0x00030000
164 #define L2CAP_EXT_CTRL_REQSEQ 0x0000FFFC
165
166 #define L2CAP_EXT_CTRL_POLL 0x00040000
167 #define L2CAP_EXT_CTRL_FINAL 0x00000002
168 #define L2CAP_EXT_CTRL_FRAME_TYPE 0x00000001 /* I- or S-Frame */
169
170 #define L2CAP_EXT_CTRL_FINAL_SHIFT 1
171 #define L2CAP_EXT_CTRL_REQSEQ_SHIFT 2
172 #define L2CAP_EXT_CTRL_SAR_SHIFT 16
173 #define L2CAP_EXT_CTRL_SUPER_SHIFT 16
174 #define L2CAP_EXT_CTRL_POLL_SHIFT 18
175 #define L2CAP_EXT_CTRL_TXSEQ_SHIFT 18
176
177 /* L2CAP Supervisory Function */
178 #define L2CAP_SUPER_RR 0x00
179 #define L2CAP_SUPER_REJ 0x01
180 #define L2CAP_SUPER_RNR 0x02
181 #define L2CAP_SUPER_SREJ 0x03
182
183 /* L2CAP Segmentation and Reassembly */
184 #define L2CAP_SAR_UNSEGMENTED 0x00
185 #define L2CAP_SAR_START 0x01
186 #define L2CAP_SAR_END 0x02
187 #define L2CAP_SAR_CONTINUE 0x03
188
189 /* L2CAP Command rej. reasons */
190 #define L2CAP_REJ_NOT_UNDERSTOOD 0x0000
191 #define L2CAP_REJ_MTU_EXCEEDED 0x0001
192 #define L2CAP_REJ_INVALID_CID 0x0002
193
194 /* L2CAP structures */
195 struct l2cap_hdr {
196 __le16 len;
197 __le16 cid;
198 } __packed;
199 #define L2CAP_LEN_SIZE 2
200 #define L2CAP_HDR_SIZE 4
201 #define L2CAP_ENH_HDR_SIZE 6
202 #define L2CAP_EXT_HDR_SIZE 8
203
204 #define L2CAP_FCS_SIZE 2
205 #define L2CAP_SDULEN_SIZE 2
206 #define L2CAP_PSMLEN_SIZE 2
207 #define L2CAP_ENH_CTRL_SIZE 2
208 #define L2CAP_EXT_CTRL_SIZE 4
209
210 struct l2cap_cmd_hdr {
211 __u8 code;
212 __u8 ident;
213 __le16 len;
214 } __packed;
215 #define L2CAP_CMD_HDR_SIZE 4
216
217 struct l2cap_cmd_rej_unk {
218 __le16 reason;
219 } __packed;
220
221 struct l2cap_cmd_rej_mtu {
222 __le16 reason;
223 __le16 max_mtu;
224 } __packed;
225
226 struct l2cap_cmd_rej_cid {
227 __le16 reason;
228 __le16 scid;
229 __le16 dcid;
230 } __packed;
231
232 struct l2cap_conn_req {
233 __le16 psm;
234 __le16 scid;
235 } __packed;
236
237 struct l2cap_conn_rsp {
238 __le16 dcid;
239 __le16 scid;
240 __le16 result;
241 __le16 status;
242 } __packed;
243
244 /* protocol/service multiplexer (PSM) */
245 #define L2CAP_PSM_SDP 0x0001
246 #define L2CAP_PSM_RFCOMM 0x0003
247 #define L2CAP_PSM_3DSP 0x0021
248 #define L2CAP_PSM_IPSP 0x0023 /* 6LoWPAN */
249
250 #define L2CAP_PSM_DYN_START 0x1001
251 #define L2CAP_PSM_DYN_END 0xffff
252 #define L2CAP_PSM_AUTO_END 0x10ff
253 #define L2CAP_PSM_LE_DYN_START 0x0080
254 #define L2CAP_PSM_LE_DYN_END 0x00ff
255
256 /* channel identifier */
257 #define L2CAP_CID_SIGNALING 0x0001
258 #define L2CAP_CID_CONN_LESS 0x0002
259 #define L2CAP_CID_ATT 0x0004
260 #define L2CAP_CID_LE_SIGNALING 0x0005
261 #define L2CAP_CID_SMP 0x0006
262 #define L2CAP_CID_SMP_BREDR 0x0007
263 #define L2CAP_CID_DYN_START 0x0040
264 #define L2CAP_CID_DYN_END 0xffff
265 #define L2CAP_CID_LE_DYN_END 0x007f
266
267 /* connect/create channel results */
268 #define L2CAP_CR_SUCCESS 0x0000
269 #define L2CAP_CR_PEND 0x0001
270 #define L2CAP_CR_BAD_PSM 0x0002
271 #define L2CAP_CR_SEC_BLOCK 0x0003
272 #define L2CAP_CR_NO_MEM 0x0004
273 #define L2CAP_CR_INVALID_SCID 0x0006
274 #define L2CAP_CR_SCID_IN_USE 0x0007
275
276 /* credit based connect results */
277 #define L2CAP_CR_LE_SUCCESS 0x0000
278 #define L2CAP_CR_LE_BAD_PSM 0x0002
279 #define L2CAP_CR_LE_NO_MEM 0x0004
280 #define L2CAP_CR_LE_AUTHENTICATION 0x0005
281 #define L2CAP_CR_LE_AUTHORIZATION 0x0006
282 #define L2CAP_CR_LE_BAD_KEY_SIZE 0x0007
283 #define L2CAP_CR_LE_ENCRYPTION 0x0008
284 #define L2CAP_CR_LE_INVALID_SCID 0x0009
285 #define L2CAP_CR_LE_SCID_IN_USE 0x000A
286 #define L2CAP_CR_LE_UNACCEPT_PARAMS 0x000B
287 #define L2CAP_CR_LE_INVALID_PARAMS 0x000C
288
289 /* connect/create channel status */
290 #define L2CAP_CS_NO_INFO 0x0000
291 #define L2CAP_CS_AUTHEN_PEND 0x0001
292 #define L2CAP_CS_AUTHOR_PEND 0x0002
293
294 struct l2cap_conf_req {
295 __le16 dcid;
296 __le16 flags;
297 __u8 data[];
298 } __packed;
299
300 struct l2cap_conf_rsp {
301 __le16 scid;
302 __le16 flags;
303 __le16 result;
304 __u8 data[];
305 } __packed;
306
307 #define L2CAP_CONF_SUCCESS 0x0000
308 #define L2CAP_CONF_UNACCEPT 0x0001
309 #define L2CAP_CONF_REJECT 0x0002
310 #define L2CAP_CONF_UNKNOWN 0x0003
311 #define L2CAP_CONF_PENDING 0x0004
312 #define L2CAP_CONF_EFS_REJECT 0x0005
313
314 /* configuration req/rsp continuation flag */
315 #define L2CAP_CONF_FLAG_CONTINUATION 0x0001
316
317 struct l2cap_conf_opt {
318 __u8 type;
319 __u8 len;
320 __u8 val[];
321 } __packed;
322 #define L2CAP_CONF_OPT_SIZE 2
323
324 #define L2CAP_CONF_HINT 0x80
325 #define L2CAP_CONF_MASK 0x7f
326
327 #define L2CAP_CONF_MTU 0x01
328 #define L2CAP_CONF_FLUSH_TO 0x02
329 #define L2CAP_CONF_QOS 0x03
330 #define L2CAP_CONF_RFC 0x04
331 #define L2CAP_CONF_FCS 0x05
332 #define L2CAP_CONF_EFS 0x06
333 #define L2CAP_CONF_EWS 0x07
334
335 #define L2CAP_CONF_MAX_SIZE 22
336
337 struct l2cap_conf_rfc {
338 __u8 mode;
339 __u8 txwin_size;
340 __u8 max_transmit;
341 __le16 retrans_timeout;
342 __le16 monitor_timeout;
343 __le16 max_pdu_size;
344 } __packed;
345
346 #define L2CAP_MODE_BASIC 0x00
347 #define L2CAP_MODE_RETRANS 0x01
348 #define L2CAP_MODE_FLOWCTL 0x02
349 #define L2CAP_MODE_ERTM 0x03
350 #define L2CAP_MODE_STREAMING 0x04
351
352 /* Unlike the above this one doesn't actually map to anything that would
353 * ever be sent over the air. Therefore, use a value that's unlikely to
354 * ever be used in the BR/EDR configuration phase.
355 */
356 #define L2CAP_MODE_LE_FLOWCTL 0x80
357 #define L2CAP_MODE_EXT_FLOWCTL 0x81
358
359 struct l2cap_conf_efs {
360 __u8 id;
361 __u8 stype;
362 __le16 msdu;
363 __le32 sdu_itime;
364 __le32 acc_lat;
365 __le32 flush_to;
366 } __packed;
367
368 #define L2CAP_SERV_NOTRAFIC 0x00
369 #define L2CAP_SERV_BESTEFFORT 0x01
370 #define L2CAP_SERV_GUARANTEED 0x02
371
372 #define L2CAP_BESTEFFORT_ID 0x01
373
374 struct l2cap_disconn_req {
375 __le16 dcid;
376 __le16 scid;
377 } __packed;
378
379 struct l2cap_disconn_rsp {
380 __le16 dcid;
381 __le16 scid;
382 } __packed;
383
384 struct l2cap_info_req {
385 __le16 type;
386 } __packed;
387
388 struct l2cap_info_rsp {
389 __le16 type;
390 __le16 result;
391 __u8 data[];
392 } __packed;
393
394 #define L2CAP_MR_SUCCESS 0x0000
395 #define L2CAP_MR_PEND 0x0001
396 #define L2CAP_MR_BAD_ID 0x0002
397 #define L2CAP_MR_SAME_ID 0x0003
398 #define L2CAP_MR_NOT_SUPP 0x0004
399 #define L2CAP_MR_COLLISION 0x0005
400 #define L2CAP_MR_NOT_ALLOWED 0x0006
401
402 struct l2cap_move_chan_cfm {
403 __le16 icid;
404 __le16 result;
405 } __packed;
406
407 #define L2CAP_MC_CONFIRMED 0x0000
408 #define L2CAP_MC_UNCONFIRMED 0x0001
409
410 struct l2cap_move_chan_cfm_rsp {
411 __le16 icid;
412 } __packed;
413
414 /* info type */
415 #define L2CAP_IT_CL_MTU 0x0001
416 #define L2CAP_IT_FEAT_MASK 0x0002
417 #define L2CAP_IT_FIXED_CHAN 0x0003
418
419 /* info result */
420 #define L2CAP_IR_SUCCESS 0x0000
421 #define L2CAP_IR_NOTSUPP 0x0001
422
423 struct l2cap_conn_param_update_req {
424 __le16 min;
425 __le16 max;
426 __le16 latency;
427 __le16 to_multiplier;
428 } __packed;
429
430 struct l2cap_conn_param_update_rsp {
431 __le16 result;
432 } __packed;
433
434 /* Connection Parameters result */
435 #define L2CAP_CONN_PARAM_ACCEPTED 0x0000
436 #define L2CAP_CONN_PARAM_REJECTED 0x0001
437
438 struct l2cap_le_conn_req {
439 __le16 psm;
440 __le16 scid;
441 __le16 mtu;
442 __le16 mps;
443 __le16 credits;
444 } __packed;
445
446 struct l2cap_le_conn_rsp {
447 __le16 dcid;
448 __le16 mtu;
449 __le16 mps;
450 __le16 credits;
451 __le16 result;
452 } __packed;
453
454 struct l2cap_le_credits {
455 __le16 cid;
456 __le16 credits;
457 } __packed;
458
459 #define L2CAP_ECRED_MIN_MTU 64
460 #define L2CAP_ECRED_MIN_MPS 64
461 #define L2CAP_ECRED_MAX_CID 5
462
463 struct l2cap_ecred_conn_req {
464 /* New members must be added within the struct_group() macro below. */
465 __struct_group(l2cap_ecred_conn_req_hdr, hdr, __packed,
466 __le16 psm;
467 __le16 mtu;
468 __le16 mps;
469 __le16 credits;
470 );
471 __le16 scid[];
472 } __packed;
473
474 struct l2cap_ecred_conn_rsp {
475 /* New members must be added within the struct_group() macro below. */
476 struct_group_tagged(l2cap_ecred_conn_rsp_hdr, hdr,
477 __le16 mtu;
478 __le16 mps;
479 __le16 credits;
480 __le16 result;
481 );
482 __le16 dcid[];
483 };
484
485 struct l2cap_ecred_reconf_req {
486 __le16 mtu;
487 __le16 mps;
488 __le16 scid[];
489 } __packed;
490
491 #define L2CAP_RECONF_SUCCESS 0x0000
492 #define L2CAP_RECONF_INVALID_MTU 0x0001
493 #define L2CAP_RECONF_INVALID_MPS 0x0002
494 #define L2CAP_RECONF_INVALID_CID 0x0003
495 #define L2CAP_RECONF_INVALID_PARAMS 0x0004
496
497 struct l2cap_ecred_reconf_rsp {
498 __le16 result;
499 } __packed;
500
501 /* ----- L2CAP channels and connections ----- */
502 struct l2cap_seq_list {
503 __u16 head;
504 __u16 tail;
505 __u16 mask;
506 __u16 *list;
507 };
508
509 #define L2CAP_SEQ_LIST_CLEAR 0xFFFF
510 #define L2CAP_SEQ_LIST_TAIL 0x8000
511
512 struct l2cap_chan {
513 struct l2cap_conn *conn;
514 struct kref kref;
515 atomic_t nesting;
516
517 __u8 state;
518
519 bdaddr_t dst;
520 __u8 dst_type;
521 bdaddr_t src;
522 __u8 src_type;
523 __le16 psm;
524 __le16 sport;
525 __u16 dcid;
526 __u16 scid;
527
528 __u16 imtu;
529 __u16 omtu;
530 __u16 flush_to;
531 __u8 mode;
532 __u8 chan_type;
533 __u8 chan_policy;
534
535 __u8 sec_level;
536
537 __u8 ident;
538
539 __u8 conf_req[64];
540 __u8 conf_len;
541 __u8 num_conf_req;
542 __u8 num_conf_rsp;
543
544 __u8 fcs;
545
546 __u16 tx_win;
547 __u16 tx_win_max;
548 __u16 ack_win;
549 __u8 max_tx;
550 __u16 retrans_timeout;
551 __u16 monitor_timeout;
552 __u16 mps;
553
554 __u16 tx_credits;
555 __u16 rx_credits;
556
557 /* estimated available receive buffer space or -1 if unknown */
558 ssize_t rx_avail;
559
560 __u8 tx_state;
561 __u8 rx_state;
562
563 unsigned long conf_state;
564 unsigned long conn_state;
565 unsigned long flags;
566
567 __u16 next_tx_seq;
568 __u16 expected_ack_seq;
569 __u16 expected_tx_seq;
570 __u16 buffer_seq;
571 __u16 srej_save_reqseq;
572 __u16 last_acked_seq;
573 __u16 frames_sent;
574 __u16 unacked_frames;
575 __u8 retry_count;
576 __u16 sdu_len;
577 struct sk_buff *sdu;
578 struct sk_buff *sdu_last_frag;
579
580 __u16 remote_tx_win;
581 __u8 remote_max_tx;
582 __u16 remote_mps;
583
584 __u8 local_id;
585 __u8 local_stype;
586 __u16 local_msdu;
587 __u32 local_sdu_itime;
588 __u32 local_acc_lat;
589 __u32 local_flush_to;
590
591 __u8 remote_id;
592 __u8 remote_stype;
593 __u16 remote_msdu;
594 __u32 remote_sdu_itime;
595 __u32 remote_acc_lat;
596 __u32 remote_flush_to;
597
598 struct delayed_work chan_timer;
599 struct delayed_work retrans_timer;
600 struct delayed_work monitor_timer;
601 struct delayed_work ack_timer;
602
603 struct sk_buff *tx_send_head;
604 struct sk_buff_head tx_q;
605 struct sk_buff_head srej_q;
606 struct l2cap_seq_list srej_list;
607 struct l2cap_seq_list retrans_list;
608
609 struct list_head list;
610 struct list_head global_l;
611
612 void *data;
613 const struct l2cap_ops *ops;
614 struct mutex lock;
615 };
616
617 struct l2cap_ops {
618 char *name;
619
620 int (*new_connection)(struct l2cap_chan *chan,
621 struct l2cap_chan *new_chan);
622 int (*recv) (struct l2cap_chan * chan,
623 struct sk_buff *skb);
624 void (*teardown) (struct l2cap_chan *chan, int err);
625 void (*close) (struct l2cap_chan *chan);
626 void (*state_change) (struct l2cap_chan *chan,
627 int state, int err);
628 void (*ready) (struct l2cap_chan *chan);
629 void (*defer) (struct l2cap_chan *chan);
630 void (*resume) (struct l2cap_chan *chan);
631 void (*suspend) (struct l2cap_chan *chan);
632 void (*set_shutdown) (struct l2cap_chan *chan);
633 long (*get_sndtimeo) (struct l2cap_chan *chan);
634 struct pid *(*get_peer_pid) (struct l2cap_chan *chan);
635 struct sk_buff *(*alloc_skb) (struct l2cap_chan *chan,
636 unsigned long hdr_len,
637 unsigned long len, int nb);
638 int (*filter) (struct l2cap_chan * chan,
639 struct sk_buff *skb);
640 };
641
642 struct l2cap_conn {
643 struct hci_conn *hcon;
644 struct hci_chan *hchan;
645
646 unsigned int mtu;
647
648 __u32 feat_mask;
649 __u8 remote_fixed_chan;
650 __u8 local_fixed_chan;
651
652 __u8 info_state;
653 __u8 info_ident;
654
655 struct delayed_work info_timer;
656
657 struct sk_buff *rx_skb;
658 __u32 rx_len;
659 struct ida tx_ida;
660 __u8 tx_ident;
661
662 struct sk_buff_head pending_rx;
663 struct work_struct pending_rx_work;
664
665 struct delayed_work id_addr_timer;
666
667 __u8 disc_reason;
668
669 struct l2cap_chan *smp;
670
671 struct list_head chan_l;
672 struct mutex lock;
673 struct kref ref;
674 struct list_head users;
675 };
676
677 struct l2cap_user {
678 struct list_head list;
679 int (*probe) (struct l2cap_conn *conn, struct l2cap_user *user);
680 void (*remove) (struct l2cap_conn *conn, struct l2cap_user *user);
681 };
682
683 #define L2CAP_INFO_CL_MTU_REQ_SENT 0x01
684 #define L2CAP_INFO_FEAT_MASK_REQ_SENT 0x04
685 #define L2CAP_INFO_FEAT_MASK_REQ_DONE 0x08
686
687 #define L2CAP_CHAN_RAW 1
688 #define L2CAP_CHAN_CONN_LESS 2
689 #define L2CAP_CHAN_CONN_ORIENTED 3
690 #define L2CAP_CHAN_FIXED 4
691
692 /* ----- L2CAP socket info ----- */
693 #define l2cap_pi(sk) ((struct l2cap_pinfo *) sk)
694
695 struct l2cap_rx_busy {
696 struct list_head list;
697 struct sk_buff *skb;
698 };
699
700 struct l2cap_pinfo {
701 struct bt_sock bt;
702 struct l2cap_chan *chan;
703 struct list_head rx_busy;
704 };
705
706 enum {
707 CONF_REQ_SENT,
708 CONF_INPUT_DONE,
709 CONF_OUTPUT_DONE,
710 CONF_MTU_DONE,
711 CONF_MODE_DONE,
712 CONF_CONNECT_PEND,
713 CONF_RECV_NO_FCS,
714 CONF_STATE2_DEVICE,
715 CONF_EWS_RECV,
716 CONF_LOC_CONF_PEND,
717 CONF_REM_CONF_PEND,
718 CONF_NOT_COMPLETE,
719 };
720
721 #define L2CAP_CONF_MAX_CONF_REQ 2
722 #define L2CAP_CONF_MAX_CONF_RSP 2
723
724 enum {
725 CONN_SREJ_SENT,
726 CONN_WAIT_F,
727 CONN_SREJ_ACT,
728 CONN_SEND_PBIT,
729 CONN_REMOTE_BUSY,
730 CONN_LOCAL_BUSY,
731 CONN_REJ_ACT,
732 CONN_SEND_FBIT,
733 CONN_RNR_SENT,
734 };
735
736 /* Definitions for flags in l2cap_chan */
737 enum {
738 FLAG_ROLE_SWITCH,
739 FLAG_FORCE_ACTIVE,
740 FLAG_FORCE_RELIABLE,
741 FLAG_FLUSHABLE,
742 FLAG_EXT_CTRL,
743 FLAG_EFS_ENABLE,
744 FLAG_DEFER_SETUP,
745 FLAG_LE_CONN_REQ_SENT,
746 FLAG_ECRED_CONN_REQ_SENT,
747 FLAG_PENDING_SECURITY,
748 FLAG_HOLD_HCI_CONN,
749 FLAG_DEL,
750 };
751
752 /* Lock nesting levels for L2CAP channels. We need these because lockdep
753 * otherwise considers all channels equal and will e.g. complain about a
754 * connection oriented channel triggering SMP procedures or a listening
755 * channel creating and locking a child channel.
756 */
757 enum {
758 L2CAP_NESTING_SMP,
759 L2CAP_NESTING_NORMAL,
760 L2CAP_NESTING_PARENT,
761 };
762
763 enum {
764 L2CAP_TX_STATE_XMIT,
765 L2CAP_TX_STATE_WAIT_F,
766 };
767
768 enum {
769 L2CAP_RX_STATE_RECV,
770 L2CAP_RX_STATE_SREJ_SENT,
771 L2CAP_RX_STATE_MOVE,
772 L2CAP_RX_STATE_WAIT_P,
773 L2CAP_RX_STATE_WAIT_F,
774 };
775
776 enum {
777 L2CAP_TXSEQ_EXPECTED,
778 L2CAP_TXSEQ_EXPECTED_SREJ,
779 L2CAP_TXSEQ_UNEXPECTED,
780 L2CAP_TXSEQ_UNEXPECTED_SREJ,
781 L2CAP_TXSEQ_DUPLICATE,
782 L2CAP_TXSEQ_DUPLICATE_SREJ,
783 L2CAP_TXSEQ_INVALID,
784 L2CAP_TXSEQ_INVALID_IGNORE,
785 };
786
787 enum {
788 L2CAP_EV_DATA_REQUEST,
789 L2CAP_EV_LOCAL_BUSY_DETECTED,
790 L2CAP_EV_LOCAL_BUSY_CLEAR,
791 L2CAP_EV_RECV_REQSEQ_AND_FBIT,
792 L2CAP_EV_RECV_FBIT,
793 L2CAP_EV_RETRANS_TO,
794 L2CAP_EV_MONITOR_TO,
795 L2CAP_EV_EXPLICIT_POLL,
796 L2CAP_EV_RECV_IFRAME,
797 L2CAP_EV_RECV_RR,
798 L2CAP_EV_RECV_REJ,
799 L2CAP_EV_RECV_RNR,
800 L2CAP_EV_RECV_SREJ,
801 L2CAP_EV_RECV_FRAME,
802 };
803
804 enum {
805 L2CAP_MOVE_ROLE_NONE,
806 L2CAP_MOVE_ROLE_INITIATOR,
807 L2CAP_MOVE_ROLE_RESPONDER,
808 };
809
810 enum {
811 L2CAP_MOVE_STABLE,
812 L2CAP_MOVE_WAIT_REQ,
813 L2CAP_MOVE_WAIT_RSP,
814 L2CAP_MOVE_WAIT_RSP_SUCCESS,
815 L2CAP_MOVE_WAIT_CONFIRM,
816 L2CAP_MOVE_WAIT_CONFIRM_RSP,
817 L2CAP_MOVE_WAIT_LOGICAL_COMP,
818 L2CAP_MOVE_WAIT_LOGICAL_CFM,
819 L2CAP_MOVE_WAIT_LOCAL_BUSY,
820 L2CAP_MOVE_WAIT_PREPARE,
821 };
822
823 void l2cap_chan_hold(struct l2cap_chan *c);
824 struct l2cap_chan *l2cap_chan_hold_unless_zero(struct l2cap_chan *c);
825 void l2cap_chan_put(struct l2cap_chan *c);
826
l2cap_chan_lock(struct l2cap_chan * chan)827 static inline void l2cap_chan_lock(struct l2cap_chan *chan)
828 {
829 mutex_lock_nested(&chan->lock, atomic_read(&chan->nesting));
830 }
831
l2cap_chan_unlock(struct l2cap_chan * chan)832 static inline void l2cap_chan_unlock(struct l2cap_chan *chan)
833 {
834 mutex_unlock(&chan->lock);
835 }
836
l2cap_set_timer(struct l2cap_chan * chan,struct delayed_work * work,long timeout)837 static inline void l2cap_set_timer(struct l2cap_chan *chan,
838 struct delayed_work *work, long timeout)
839 {
840 BT_DBG("chan %p state %s timeout %ld", chan,
841 state_to_string(chan->state), timeout);
842
843 /* If delayed work cancelled do not hold(chan)
844 since it is already done with previous set_timer */
845 if (!cancel_delayed_work(work))
846 l2cap_chan_hold(chan);
847
848 schedule_delayed_work(work, timeout);
849 }
850
l2cap_clear_timer(struct l2cap_chan * chan,struct delayed_work * work)851 static inline bool l2cap_clear_timer(struct l2cap_chan *chan,
852 struct delayed_work *work)
853 {
854 bool ret;
855
856 /* put(chan) if delayed work cancelled otherwise it
857 is done in delayed work function */
858 ret = cancel_delayed_work(work);
859 if (ret)
860 l2cap_chan_put(chan);
861
862 return ret;
863 }
864
865 #define __set_chan_timer(c, t) l2cap_set_timer(c, &c->chan_timer, (t))
866 #define __clear_chan_timer(c) l2cap_clear_timer(c, &c->chan_timer)
867 #define __clear_retrans_timer(c) l2cap_clear_timer(c, &c->retrans_timer)
868 #define __clear_monitor_timer(c) l2cap_clear_timer(c, &c->monitor_timer)
869 #define __set_ack_timer(c) l2cap_set_timer(c, &chan->ack_timer, \
870 msecs_to_jiffies(L2CAP_DEFAULT_ACK_TO));
871 #define __clear_ack_timer(c) l2cap_clear_timer(c, &c->ack_timer)
872
__seq_offset(struct l2cap_chan * chan,__u16 seq1,__u16 seq2)873 static inline int __seq_offset(struct l2cap_chan *chan, __u16 seq1, __u16 seq2)
874 {
875 if (seq1 >= seq2)
876 return seq1 - seq2;
877 else
878 return chan->tx_win_max + 1 - seq2 + seq1;
879 }
880
__next_seq(struct l2cap_chan * chan,__u16 seq)881 static inline __u16 __next_seq(struct l2cap_chan *chan, __u16 seq)
882 {
883 return (seq + 1) % (chan->tx_win_max + 1);
884 }
885
l2cap_chan_no_new_connection(struct l2cap_chan * chan,struct l2cap_chan * new_chan)886 static inline int l2cap_chan_no_new_connection(struct l2cap_chan *chan,
887 struct l2cap_chan *new_chan)
888 {
889 return -EOPNOTSUPP;
890 }
891
l2cap_chan_no_recv(struct l2cap_chan * chan,struct sk_buff * skb)892 static inline int l2cap_chan_no_recv(struct l2cap_chan *chan, struct sk_buff *skb)
893 {
894 return -ENOSYS;
895 }
896
l2cap_chan_no_alloc_skb(struct l2cap_chan * chan,unsigned long hdr_len,unsigned long len,int nb)897 static inline struct sk_buff *l2cap_chan_no_alloc_skb(struct l2cap_chan *chan,
898 unsigned long hdr_len,
899 unsigned long len, int nb)
900 {
901 return ERR_PTR(-ENOSYS);
902 }
903
l2cap_chan_no_teardown(struct l2cap_chan * chan,int err)904 static inline void l2cap_chan_no_teardown(struct l2cap_chan *chan, int err)
905 {
906 }
907
l2cap_chan_no_close(struct l2cap_chan * chan)908 static inline void l2cap_chan_no_close(struct l2cap_chan *chan)
909 {
910 }
911
l2cap_chan_no_ready(struct l2cap_chan * chan)912 static inline void l2cap_chan_no_ready(struct l2cap_chan *chan)
913 {
914 }
915
l2cap_chan_no_state_change(struct l2cap_chan * chan,int state,int err)916 static inline void l2cap_chan_no_state_change(struct l2cap_chan *chan,
917 int state, int err)
918 {
919 }
920
l2cap_chan_no_defer(struct l2cap_chan * chan)921 static inline void l2cap_chan_no_defer(struct l2cap_chan *chan)
922 {
923 }
924
l2cap_chan_no_suspend(struct l2cap_chan * chan)925 static inline void l2cap_chan_no_suspend(struct l2cap_chan *chan)
926 {
927 }
928
l2cap_chan_no_resume(struct l2cap_chan * chan)929 static inline void l2cap_chan_no_resume(struct l2cap_chan *chan)
930 {
931 }
932
l2cap_chan_no_set_shutdown(struct l2cap_chan * chan)933 static inline void l2cap_chan_no_set_shutdown(struct l2cap_chan *chan)
934 {
935 }
936
l2cap_chan_no_get_sndtimeo(struct l2cap_chan * chan)937 static inline long l2cap_chan_no_get_sndtimeo(struct l2cap_chan *chan)
938 {
939 return 0;
940 }
941
942 extern bool disable_ertm;
943 extern bool enable_ecred;
944
945 int l2cap_init_sockets(void);
946 void l2cap_cleanup_sockets(void);
947 bool l2cap_is_socket(struct socket *sock);
948
949 void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan);
950 void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan);
951 void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
952
953 int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);
954 int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid);
955
956 struct l2cap_chan *l2cap_chan_create(void);
957 void l2cap_chan_close(struct l2cap_chan *chan, int reason);
958 int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
959 bdaddr_t *dst, u8 dst_type, u16 timeout);
960 int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu);
961 int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
962 const struct sockcm_cookie *sockc);
963 void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
964 void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail);
965 int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator);
966 void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan);
967 int l2cap_ertm_init(struct l2cap_chan *chan);
968 void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
969 void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
970 typedef void (*l2cap_chan_func_t)(struct l2cap_chan *chan, void *data);
971 void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
972 void *data);
973 void l2cap_chan_del(struct l2cap_chan *chan, int err);
974 void l2cap_send_conn_req(struct l2cap_chan *chan);
975
976 struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn);
977 struct l2cap_conn *l2cap_conn_hold_unless_zero(struct l2cap_conn *conn);
978 void l2cap_conn_put(struct l2cap_conn *conn);
979
980 int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user);
981 void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user);
982
983 #endif /* __L2CAP_H */
984