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