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 Copyright (C) 2011 ProFUSION Embedded Systems 8 Copyright (c) 2012 Code Aurora Forum. All rights reserved. 9 10 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 11 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 16 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 23 SOFTWARE IS DISCLAIMED. 24 */ 25 26 /* Bluetooth L2CAP core. */ 27 28 #include <linux/module.h> 29 30 #include <linux/debugfs.h> 31 #include <linux/crc16.h> 32 #include <linux/filter.h> 33 34 #include <net/bluetooth/bluetooth.h> 35 #include <net/bluetooth/hci_core.h> 36 #include <net/bluetooth/l2cap.h> 37 38 #include "smp.h" 39 40 #define LE_FLOWCTL_MAX_CREDITS 65535 41 42 bool disable_ertm; 43 bool enable_ecred = IS_ENABLED(CONFIG_BT_LE_L2CAP_ECRED); 44 45 static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD; 46 47 static DEFINE_RWLOCK(chan_list_lock); 48 static __guarded_by(&chan_list_lock) LIST_HEAD(chan_list); 49 50 static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 51 u8 code, u8 ident, u16 dlen, void *data); 52 static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, 53 void *data); 54 static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data_size); 55 static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err); 56 57 static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, 58 struct sk_buff_head *skbs, u8 event); 59 static void l2cap_retrans_timeout(struct work_struct *work); 60 static void l2cap_monitor_timeout(struct work_struct *work); 61 static void l2cap_ack_timeout(struct work_struct *work); 62 static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) 63 __must_hold(&chan->lock) __must_hold(&chan->conn->lock); 64 65 static inline u8 bdaddr_type(u8 link_type, u8 bdaddr_type) 66 { 67 if (link_type == LE_LINK) { 68 if (bdaddr_type == ADDR_LE_DEV_PUBLIC) 69 return BDADDR_LE_PUBLIC; 70 else 71 return BDADDR_LE_RANDOM; 72 } 73 74 return BDADDR_BREDR; 75 } 76 77 static inline u8 bdaddr_src_type(struct hci_conn *hcon) 78 { 79 return bdaddr_type(hcon->type, hcon->src_type); 80 } 81 82 static inline u8 bdaddr_dst_type(struct hci_conn *hcon) 83 { 84 return bdaddr_type(hcon->type, hcon->dst_type); 85 } 86 87 /* ---- L2CAP channels ---- */ 88 89 static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, 90 u16 cid) 91 __must_hold(&conn->lock) 92 { 93 struct l2cap_chan *c; 94 95 list_for_each_entry(c, &conn->chan_l, list) { 96 if (c->dcid == cid) 97 return c; 98 } 99 return NULL; 100 } 101 102 static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, 103 u16 cid) 104 __must_hold(&conn->lock) 105 { 106 struct l2cap_chan *c; 107 108 list_for_each_entry(c, &conn->chan_l, list) { 109 if (c->scid == cid) 110 return c; 111 } 112 return NULL; 113 } 114 115 /* Find channel with given SCID. 116 * Returns a reference. 117 */ 118 static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, 119 u16 cid) 120 __must_hold(&conn->lock) 121 { 122 struct l2cap_chan *c; 123 124 c = __l2cap_get_chan_by_scid(conn, cid); 125 if (c) 126 c = l2cap_chan_hold_unless_zero(c); 127 128 return c; 129 } 130 131 /* Find channel with given DCID. 132 * Returns a reference. 133 */ 134 static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, 135 u16 cid) 136 __must_hold(&conn->lock) 137 { 138 struct l2cap_chan *c; 139 140 c = __l2cap_get_chan_by_dcid(conn, cid); 141 if (c) 142 c = l2cap_chan_hold_unless_zero(c); 143 144 return c; 145 } 146 147 /* Our connection request has been sent and is still outstanding */ 148 static bool l2cap_chan_connect_pending(struct l2cap_chan *chan) 149 { 150 return chan->state == BT_CONNECT && 151 (test_bit(CONF_CONNECT_PEND, &chan->conf_state) || 152 test_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags)); 153 } 154 155 /* Other channels may hold the same ident for a response they still have 156 * to send, so only look at channels waiting for a connection response. 157 */ 158 static struct l2cap_chan *__l2cap_get_connect_chan_by_ident(struct l2cap_conn *conn, 159 u8 ident) 160 __must_hold(&conn->lock) 161 { 162 struct l2cap_chan *c; 163 164 list_for_each_entry(c, &conn->chan_l, list) { 165 if (l2cap_chan_connect_pending(c) && c->ident == ident) 166 return c; 167 } 168 return NULL; 169 } 170 171 static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src, 172 u8 src_type) 173 __must_hold_shared(&chan_list_lock) 174 { 175 struct l2cap_chan *c; 176 177 list_for_each_entry(c, &chan_list, global_l) { 178 if (src_type == BDADDR_BREDR && c->src_type != BDADDR_BREDR) 179 continue; 180 181 if (src_type != BDADDR_BREDR && c->src_type == BDADDR_BREDR) 182 continue; 183 184 if (c->sport == psm && !bacmp(&c->src, src)) 185 return c; 186 } 187 return NULL; 188 } 189 190 int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) 191 { 192 int err; 193 194 write_lock(&chan_list_lock); 195 196 if (psm && __l2cap_global_chan_by_addr(psm, src, chan->src_type)) { 197 err = -EADDRINUSE; 198 goto done; 199 } 200 201 if (psm) { 202 chan->psm = psm; 203 chan->sport = psm; 204 err = 0; 205 } else { 206 u16 p, start, end, incr; 207 208 if (chan->src_type == BDADDR_BREDR) { 209 start = L2CAP_PSM_DYN_START; 210 end = L2CAP_PSM_AUTO_END; 211 incr = 2; 212 } else { 213 start = L2CAP_PSM_LE_DYN_START; 214 end = L2CAP_PSM_LE_DYN_END; 215 incr = 1; 216 } 217 218 err = -EINVAL; 219 for (p = start; p <= end; p += incr) 220 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src, 221 chan->src_type)) { 222 chan->psm = cpu_to_le16(p); 223 chan->sport = cpu_to_le16(p); 224 err = 0; 225 break; 226 } 227 } 228 229 done: 230 write_unlock(&chan_list_lock); 231 return err; 232 } 233 EXPORT_SYMBOL_GPL(l2cap_add_psm); 234 235 int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) 236 { 237 write_lock(&chan_list_lock); 238 239 /* Override the defaults (which are for conn-oriented) */ 240 chan->omtu = L2CAP_DEFAULT_MTU; 241 chan->chan_type = L2CAP_CHAN_FIXED; 242 243 chan->scid = scid; 244 245 write_unlock(&chan_list_lock); 246 247 return 0; 248 } 249 250 static u16 l2cap_alloc_cid(struct l2cap_conn *conn) 251 __must_hold(&conn->lock) 252 { 253 u16 cid, dyn_end; 254 255 if (conn->hcon->type == LE_LINK) 256 dyn_end = L2CAP_CID_LE_DYN_END; 257 else 258 dyn_end = L2CAP_CID_DYN_END; 259 260 for (cid = L2CAP_CID_DYN_START; cid <= dyn_end; cid++) { 261 if (!__l2cap_get_chan_by_scid(conn, cid)) 262 return cid; 263 } 264 265 return 0; 266 } 267 268 static void l2cap_state_change(struct l2cap_chan *chan, int state) 269 { 270 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), 271 state_to_string(state)); 272 273 chan->state = state; 274 chan->ops->state_change(chan, state, 0); 275 } 276 277 static inline void l2cap_state_change_and_error(struct l2cap_chan *chan, 278 int state, int err) 279 { 280 chan->state = state; 281 chan->ops->state_change(chan, chan->state, err); 282 } 283 284 static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) 285 { 286 chan->ops->state_change(chan, chan->state, err); 287 } 288 289 static void __set_retrans_timer(struct l2cap_chan *chan) 290 { 291 if (!delayed_work_pending(&chan->monitor_timer) && 292 chan->retrans_timeout) { 293 l2cap_set_timer(chan, &chan->retrans_timer, 294 msecs_to_jiffies(chan->retrans_timeout)); 295 } 296 } 297 298 static void __set_monitor_timer(struct l2cap_chan *chan) 299 { 300 __clear_retrans_timer(chan); 301 if (chan->monitor_timeout) { 302 l2cap_set_timer(chan, &chan->monitor_timer, 303 msecs_to_jiffies(chan->monitor_timeout)); 304 } 305 } 306 307 static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head, 308 u16 seq) 309 { 310 struct sk_buff *skb; 311 312 skb_queue_walk(head, skb) { 313 if (bt_cb(skb)->l2cap.txseq == seq) 314 return skb; 315 } 316 317 return NULL; 318 } 319 320 /* ---- L2CAP sequence number lists ---- */ 321 322 /* For ERTM, ordered lists of sequence numbers must be tracked for 323 * SREJ requests that are received and for frames that are to be 324 * retransmitted. These seq_list functions implement a singly-linked 325 * list in an array, where membership in the list can also be checked 326 * in constant time. Items can also be added to the tail of the list 327 * and removed from the head in constant time, without further memory 328 * allocs or frees. 329 */ 330 331 static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size) 332 { 333 size_t alloc_size, i; 334 335 /* roundup_pow_of_two(0) is undefined */ 336 if (!size) 337 return -EINVAL; 338 339 /* Allocated size is a power of 2 to map sequence numbers 340 * (which may be up to 14 bits) in to a smaller array that is 341 * sized for the negotiated ERTM transmit windows. 342 */ 343 alloc_size = roundup_pow_of_two(size); 344 345 seq_list->list = kmalloc_array(alloc_size, sizeof(u16), GFP_KERNEL); 346 if (!seq_list->list) 347 return -ENOMEM; 348 349 seq_list->mask = alloc_size - 1; 350 seq_list->head = L2CAP_SEQ_LIST_CLEAR; 351 seq_list->tail = L2CAP_SEQ_LIST_CLEAR; 352 for (i = 0; i < alloc_size; i++) 353 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; 354 355 return 0; 356 } 357 358 static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) 359 { 360 kfree(seq_list->list); 361 } 362 363 static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list, 364 u16 seq) 365 { 366 /* Constant-time check for list membership */ 367 return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR; 368 } 369 370 static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list) 371 { 372 u16 seq = seq_list->head; 373 u16 mask = seq_list->mask; 374 375 seq_list->head = seq_list->list[seq & mask]; 376 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; 377 378 if (seq_list->head == L2CAP_SEQ_LIST_TAIL) { 379 seq_list->head = L2CAP_SEQ_LIST_CLEAR; 380 seq_list->tail = L2CAP_SEQ_LIST_CLEAR; 381 } 382 383 return seq; 384 } 385 386 static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) 387 { 388 u16 i; 389 390 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) 391 return; 392 393 for (i = 0; i <= seq_list->mask; i++) 394 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; 395 396 seq_list->head = L2CAP_SEQ_LIST_CLEAR; 397 seq_list->tail = L2CAP_SEQ_LIST_CLEAR; 398 } 399 400 static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq) 401 { 402 u16 mask = seq_list->mask; 403 404 /* All appends happen in constant time */ 405 406 if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) 407 return; 408 409 if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR) 410 seq_list->head = seq; 411 else 412 seq_list->list[seq_list->tail & mask] = seq; 413 414 seq_list->tail = seq; 415 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL; 416 } 417 418 static void l2cap_chan_timeout(struct work_struct *work) 419 { 420 struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 421 chan_timer.work); 422 struct l2cap_conn *conn = chan->conn; 423 int reason; 424 425 BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); 426 427 if (test_bit(FLAG_DEL, &chan->flags)) { 428 l2cap_chan_put(chan); 429 return; 430 } 431 432 mutex_lock(&conn->lock); 433 /* __set_chan_timer() calls l2cap_chan_hold(chan) while scheduling 434 * this work. No need to call l2cap_chan_hold(chan) here again. 435 */ 436 l2cap_chan_lock(chan); 437 438 if (test_bit(FLAG_DEL, &chan->flags)) 439 goto unlock; 440 441 if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) 442 reason = ECONNREFUSED; 443 else if (chan->state == BT_CONNECT && 444 chan->sec_level != BT_SECURITY_SDP) 445 reason = ECONNREFUSED; 446 else 447 reason = ETIMEDOUT; 448 449 __l2cap_chan_close(chan, reason); 450 451 chan->ops->close(chan); 452 453 unlock: 454 l2cap_chan_unlock(chan); 455 mutex_unlock(&conn->lock); 456 l2cap_chan_put(chan); 457 } 458 459 struct l2cap_chan *l2cap_chan_create(void) 460 { 461 struct l2cap_chan *chan; 462 463 chan = kzalloc_obj(*chan, GFP_ATOMIC); 464 if (!chan) 465 return NULL; 466 467 skb_queue_head_init(&chan->tx_q); 468 skb_queue_head_init(&chan->srej_q); 469 mutex_init(&chan->lock); 470 471 /* Set default lock nesting level */ 472 atomic_set(&chan->nesting, L2CAP_NESTING_NORMAL); 473 474 /* Available receive buffer space is initially unknown */ 475 chan->rx_avail = -1; 476 477 write_lock(&chan_list_lock); 478 list_add(&chan->global_l, &chan_list); 479 write_unlock(&chan_list_lock); 480 481 INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); 482 INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); 483 INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); 484 INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); 485 486 chan->state = BT_OPEN; 487 488 kref_init(&chan->kref); 489 490 /* This flag is cleared in l2cap_chan_ready() */ 491 set_bit(CONF_NOT_COMPLETE, &chan->conf_state); 492 493 BT_DBG("chan %p", chan); 494 495 return chan; 496 } 497 EXPORT_SYMBOL_GPL(l2cap_chan_create); 498 499 static void l2cap_chan_destroy(struct kref *kref) 500 { 501 struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); 502 503 BT_DBG("chan %p", chan); 504 505 write_lock(&chan_list_lock); 506 list_del(&chan->global_l); 507 write_unlock(&chan_list_lock); 508 509 if (chan->conn) 510 l2cap_conn_put(chan->conn); 511 512 kfree(chan); 513 } 514 515 void l2cap_chan_hold(struct l2cap_chan *c) 516 { 517 BT_DBG("chan %p orig refcnt %u", c, kref_read(&c->kref)); 518 519 kref_get(&c->kref); 520 } 521 EXPORT_SYMBOL_GPL(l2cap_chan_hold); 522 523 struct l2cap_chan *l2cap_chan_hold_unless_zero(struct l2cap_chan *c) 524 { 525 BT_DBG("chan %p orig refcnt %u", c, kref_read(&c->kref)); 526 527 if (!kref_get_unless_zero(&c->kref)) 528 return NULL; 529 530 return c; 531 } 532 533 void l2cap_chan_put(struct l2cap_chan *c) 534 { 535 BT_DBG("chan %p orig refcnt %u", c, kref_read(&c->kref)); 536 537 kref_put(&c->kref, l2cap_chan_destroy); 538 } 539 EXPORT_SYMBOL_GPL(l2cap_chan_put); 540 541 /* Initialise @chan with default values, inheriting from the parent channel 542 * @pchan when it is given. 543 */ 544 void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan) 545 { 546 chan->fcs = L2CAP_FCS_CRC16; 547 chan->max_tx = L2CAP_DEFAULT_MAX_TX; 548 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; 549 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; 550 chan->remote_max_tx = chan->max_tx; 551 chan->remote_tx_win = chan->tx_win; 552 chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; 553 chan->sec_level = BT_SECURITY_LOW; 554 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; 555 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; 556 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; 557 558 if (pchan) { 559 BT_DBG("chan %p pchan %p", chan, pchan); 560 561 chan->chan_type = pchan->chan_type; 562 chan->imtu = pchan->imtu; 563 chan->omtu = pchan->omtu; 564 chan->mode = pchan->mode; 565 chan->fcs = pchan->fcs; 566 chan->max_tx = pchan->max_tx; 567 chan->tx_win = pchan->tx_win; 568 chan->tx_win_max = pchan->tx_win_max; 569 chan->sec_level = pchan->sec_level; 570 chan->conf_state = pchan->conf_state; 571 chan->flags = pchan->flags; 572 chan->tx_credits = pchan->tx_credits; 573 chan->rx_credits = pchan->rx_credits; 574 575 if (chan->chan_type == L2CAP_CHAN_FIXED) { 576 chan->scid = pchan->scid; 577 chan->dcid = pchan->scid; 578 } 579 580 return; 581 } 582 583 chan->conf_state = 0; 584 set_bit(CONF_NOT_COMPLETE, &chan->conf_state); 585 586 set_bit(FLAG_FORCE_ACTIVE, &chan->flags); 587 } 588 EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults); 589 590 static __u16 l2cap_le_rx_credits(struct l2cap_chan *chan) 591 { 592 size_t sdu_len = chan->sdu ? chan->sdu->len : 0; 593 594 if (chan->mps == 0) 595 return 0; 596 597 /* If we don't know the available space in the receiver buffer, give 598 * enough credits for a full packet. 599 */ 600 if (chan->rx_avail == -1) 601 return (chan->imtu / chan->mps) + 1; 602 603 /* If we know how much space is available in the receive buffer, give 604 * out as many credits as would fill the buffer. 605 */ 606 if (chan->rx_avail <= sdu_len) 607 return 0; 608 609 return DIV_ROUND_UP(chan->rx_avail - sdu_len, chan->mps); 610 } 611 612 static void l2cap_le_flowctl_init(struct l2cap_chan *chan, u16 tx_credits) 613 { 614 chan->sdu = NULL; 615 chan->sdu_last_frag = NULL; 616 chan->sdu_len = 0; 617 chan->tx_credits = tx_credits; 618 /* Derive MPS from connection MTU to stop HCI fragmentation */ 619 chan->mps = min_t(u16, chan->imtu, chan->conn->mtu - L2CAP_HDR_SIZE); 620 chan->rx_credits = l2cap_le_rx_credits(chan); 621 622 skb_queue_head_init(&chan->tx_q); 623 } 624 625 static void l2cap_ecred_init(struct l2cap_chan *chan, u16 tx_credits) 626 { 627 l2cap_le_flowctl_init(chan, tx_credits); 628 629 /* L2CAP implementations shall support a minimum MPS of 64 octets */ 630 if (chan->mps < L2CAP_ECRED_MIN_MPS) { 631 chan->mps = L2CAP_ECRED_MIN_MPS; 632 chan->rx_credits = l2cap_le_rx_credits(chan); 633 } 634 } 635 636 void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 637 { 638 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 639 __le16_to_cpu(chan->psm), chan->dcid); 640 641 /* Caller must ensure l2cap_chan is linked to l2cap_conn only once */ 642 if (WARN_ON_ONCE(chan->conn || test_bit(FLAG_DEL, &chan->flags))) 643 return; 644 645 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 646 647 chan->conn = l2cap_conn_get(conn); 648 649 switch (chan->chan_type) { 650 case L2CAP_CHAN_CONN_ORIENTED: 651 /* Alloc CID for connection-oriented socket */ 652 chan->scid = l2cap_alloc_cid(conn); 653 if (conn->hcon->type == ACL_LINK) 654 chan->omtu = L2CAP_DEFAULT_MTU; 655 break; 656 657 case L2CAP_CHAN_CONN_LESS: 658 /* Connectionless socket */ 659 chan->scid = L2CAP_CID_CONN_LESS; 660 chan->dcid = L2CAP_CID_CONN_LESS; 661 chan->omtu = L2CAP_DEFAULT_MTU; 662 break; 663 664 case L2CAP_CHAN_FIXED: 665 /* Caller will set CID and CID specific MTU values */ 666 break; 667 668 default: 669 /* Raw socket can send/recv signalling messages only */ 670 chan->scid = L2CAP_CID_SIGNALING; 671 chan->dcid = L2CAP_CID_SIGNALING; 672 chan->omtu = L2CAP_DEFAULT_MTU; 673 } 674 675 chan->local_id = L2CAP_BESTEFFORT_ID; 676 chan->local_stype = L2CAP_SERV_BESTEFFORT; 677 chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; 678 chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; 679 chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; 680 chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; 681 682 l2cap_chan_hold(chan); 683 684 /* Only keep a reference for fixed channels if they requested it */ 685 if (chan->chan_type != L2CAP_CHAN_FIXED || 686 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags)) 687 hci_conn_hold(conn->hcon); 688 689 /* Append to the list since the order matters for ECRED */ 690 list_add_tail(&chan->list, &conn->chan_l); 691 } 692 693 void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 694 { 695 mutex_lock(&conn->lock); 696 l2cap_chan_lock(chan); 697 __l2cap_chan_add(conn, chan); 698 l2cap_chan_unlock(chan); 699 mutex_unlock(&conn->lock); 700 } 701 702 void l2cap_chan_del(struct l2cap_chan *chan, int err) 703 { 704 lockdep_assert(!chan->conn || lockdep_is_held(&chan->conn->lock)); 705 706 __clear_chan_timer(chan); 707 708 BT_DBG("chan %p, err %d, state %s", chan, err, 709 state_to_string(chan->state)); 710 711 chan->ops->teardown(chan, err); 712 713 if (!test_and_set_bit(FLAG_DEL, &chan->flags)) { 714 /* Delete from channel list */ 715 list_del(&chan->list); 716 717 l2cap_chan_put(chan); 718 719 /* Reference was only held for non-fixed channels or 720 * fixed channels that explicitly requested it using the 721 * FLAG_HOLD_HCI_CONN flag. 722 */ 723 if (chan->chan_type != L2CAP_CHAN_FIXED || 724 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags)) 725 hci_conn_drop(chan->conn->hcon); 726 } 727 728 if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) 729 return; 730 731 switch (chan->mode) { 732 case L2CAP_MODE_BASIC: 733 break; 734 735 case L2CAP_MODE_LE_FLOWCTL: 736 case L2CAP_MODE_EXT_FLOWCTL: 737 skb_queue_purge(&chan->tx_q); 738 break; 739 740 case L2CAP_MODE_ERTM: 741 __clear_retrans_timer(chan); 742 __clear_monitor_timer(chan); 743 __clear_ack_timer(chan); 744 745 skb_queue_purge(&chan->srej_q); 746 747 l2cap_seq_list_free(&chan->srej_list); 748 l2cap_seq_list_free(&chan->retrans_list); 749 fallthrough; 750 751 case L2CAP_MODE_STREAMING: 752 skb_queue_purge(&chan->tx_q); 753 break; 754 } 755 } 756 EXPORT_SYMBOL_GPL(l2cap_chan_del); 757 758 static void __l2cap_chan_list_id(struct l2cap_conn *conn, u16 id, 759 l2cap_chan_func_t func, void *data) 760 __must_hold(&conn->lock) 761 { 762 struct l2cap_chan *chan, *l; 763 764 list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 765 if (chan->ident == id) 766 func(chan, data); 767 } 768 } 769 770 static void __l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func, 771 void *data) 772 __must_hold(&conn->lock) 773 { 774 struct l2cap_chan *chan; 775 776 list_for_each_entry(chan, &conn->chan_l, list) { 777 func(chan, data); 778 } 779 } 780 781 void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func, 782 void *data) 783 { 784 if (!conn) 785 return; 786 787 mutex_lock(&conn->lock); 788 __l2cap_chan_list(conn, func, data); 789 mutex_unlock(&conn->lock); 790 } 791 792 EXPORT_SYMBOL_GPL(l2cap_chan_list); 793 794 static void l2cap_conn_update_id_addr(struct work_struct *work) 795 { 796 struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 797 id_addr_timer.work); 798 struct hci_conn *hcon = conn->hcon; 799 struct l2cap_chan *chan; 800 801 mutex_lock(&conn->lock); 802 803 list_for_each_entry(chan, &conn->chan_l, list) { 804 l2cap_chan_lock(chan); 805 bacpy(&chan->dst, &hcon->dst); 806 chan->dst_type = bdaddr_dst_type(hcon); 807 l2cap_chan_unlock(chan); 808 } 809 810 mutex_unlock(&conn->lock); 811 } 812 813 static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan) 814 { 815 struct l2cap_conn *conn = chan->conn; 816 struct l2cap_le_conn_rsp rsp; 817 u16 result; 818 819 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) 820 result = L2CAP_CR_LE_AUTHORIZATION; 821 else 822 result = L2CAP_CR_LE_BAD_PSM; 823 824 l2cap_state_change(chan, BT_DISCONN); 825 826 rsp.dcid = cpu_to_le16(chan->scid); 827 rsp.mtu = cpu_to_le16(chan->imtu); 828 rsp.mps = cpu_to_le16(chan->mps); 829 rsp.credits = cpu_to_le16(chan->rx_credits); 830 rsp.result = cpu_to_le16(result); 831 832 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), 833 &rsp); 834 } 835 836 static void l2cap_chan_ecred_connect_reject(struct l2cap_chan *chan) 837 __must_hold(&chan->lock) 838 __must_hold(&chan->conn->lock) 839 { 840 l2cap_state_change(chan, BT_DISCONN); 841 842 __l2cap_ecred_conn_rsp_defer(chan); 843 } 844 845 static void l2cap_chan_connect_reject(struct l2cap_chan *chan) 846 { 847 struct l2cap_conn *conn = chan->conn; 848 struct l2cap_conn_rsp rsp; 849 u16 result; 850 851 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) 852 result = L2CAP_CR_SEC_BLOCK; 853 else 854 result = L2CAP_CR_BAD_PSM; 855 856 l2cap_state_change(chan, BT_DISCONN); 857 858 rsp.scid = cpu_to_le16(chan->dcid); 859 rsp.dcid = cpu_to_le16(chan->scid); 860 rsp.result = cpu_to_le16(result); 861 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 862 863 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 864 } 865 866 static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) 867 { 868 struct l2cap_conn *conn = chan->conn; 869 870 BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); 871 872 switch (chan->state) { 873 case BT_LISTEN: 874 chan->ops->teardown(chan, 0); 875 break; 876 877 case BT_CONNECTED: 878 case BT_CONFIG: 879 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { 880 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); 881 l2cap_send_disconn_req(chan, reason); 882 } else 883 l2cap_chan_del(chan, reason); 884 break; 885 886 case BT_CONNECT2: 887 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { 888 if (conn->hcon->type == ACL_LINK) 889 l2cap_chan_connect_reject(chan); 890 else if (conn->hcon->type == LE_LINK) { 891 switch (chan->mode) { 892 case L2CAP_MODE_LE_FLOWCTL: 893 l2cap_chan_le_connect_reject(chan); 894 break; 895 case L2CAP_MODE_EXT_FLOWCTL: 896 l2cap_chan_ecred_connect_reject(chan); 897 return; 898 } 899 } 900 } 901 902 l2cap_chan_del(chan, reason); 903 break; 904 905 case BT_CONNECT: 906 case BT_DISCONN: 907 l2cap_chan_del(chan, reason); 908 break; 909 910 default: 911 chan->ops->teardown(chan, 0); 912 break; 913 } 914 } 915 916 /* Take chan->lock. If chan->conn is non-NULL, take new reference on it, take 917 * chan->conn->lock, and return true. Otherwise return false. 918 */ 919 bool l2cap_chan_lock_conn(struct l2cap_chan *chan) 920 __context_unsafe(/* conditional locking */) 921 { 922 /* Handle conn->lock > chan->lock ordering + race on chan->conn */ 923 for (;;) { 924 struct l2cap_conn *conn; 925 926 l2cap_chan_lock(chan); 927 conn = chan->conn; 928 if (conn) 929 l2cap_conn_get(conn); 930 l2cap_chan_unlock(chan); 931 932 if (conn) 933 mutex_lock(&conn->lock); 934 935 l2cap_chan_lock(chan); 936 937 if (chan->conn != conn) { 938 l2cap_chan_unlock(chan); 939 if (conn) { 940 mutex_unlock(&conn->lock); 941 l2cap_conn_put(conn); 942 } 943 schedule(); 944 continue; 945 } 946 947 return chan->conn; 948 } 949 } 950 951 void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) 952 { 953 bool have_conn; 954 955 have_conn = l2cap_chan_lock_conn(chan); 956 957 /* Context analysis: consider chan->conn->lock held also if conn NULL */ 958 context_unsafe(__l2cap_chan_close(chan, reason)); 959 960 l2cap_chan_unlock_conn(chan, have_conn); 961 } 962 EXPORT_SYMBOL(l2cap_chan_close_unlocked); 963 964 static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 965 { 966 switch (chan->chan_type) { 967 case L2CAP_CHAN_RAW: 968 switch (chan->sec_level) { 969 case BT_SECURITY_HIGH: 970 case BT_SECURITY_FIPS: 971 return HCI_AT_DEDICATED_BONDING_MITM; 972 case BT_SECURITY_MEDIUM: 973 return HCI_AT_DEDICATED_BONDING; 974 default: 975 return HCI_AT_NO_BONDING; 976 } 977 break; 978 case L2CAP_CHAN_CONN_LESS: 979 if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) { 980 if (chan->sec_level == BT_SECURITY_LOW) 981 chan->sec_level = BT_SECURITY_SDP; 982 } 983 if (chan->sec_level == BT_SECURITY_HIGH || 984 chan->sec_level == BT_SECURITY_FIPS) 985 return HCI_AT_NO_BONDING_MITM; 986 else 987 return HCI_AT_NO_BONDING; 988 break; 989 case L2CAP_CHAN_CONN_ORIENTED: 990 if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) { 991 if (chan->sec_level == BT_SECURITY_LOW) 992 chan->sec_level = BT_SECURITY_SDP; 993 994 if (chan->sec_level == BT_SECURITY_HIGH || 995 chan->sec_level == BT_SECURITY_FIPS) 996 return HCI_AT_NO_BONDING_MITM; 997 else 998 return HCI_AT_NO_BONDING; 999 } 1000 fallthrough; 1001 1002 default: 1003 switch (chan->sec_level) { 1004 case BT_SECURITY_HIGH: 1005 case BT_SECURITY_FIPS: 1006 return HCI_AT_GENERAL_BONDING_MITM; 1007 case BT_SECURITY_MEDIUM: 1008 return HCI_AT_GENERAL_BONDING; 1009 default: 1010 return HCI_AT_NO_BONDING; 1011 } 1012 break; 1013 } 1014 } 1015 1016 /* Service level security */ 1017 int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator) 1018 { 1019 struct l2cap_conn *conn = chan->conn; 1020 __u8 auth_type; 1021 1022 if (conn->hcon->type == LE_LINK) 1023 return smp_conn_security(conn->hcon, chan->sec_level); 1024 1025 auth_type = l2cap_get_auth_type(chan); 1026 1027 return hci_conn_security(conn->hcon, chan->sec_level, auth_type, 1028 initiator); 1029 } 1030 1031 static int l2cap_get_ident(struct l2cap_conn *conn) 1032 { 1033 u8 max; 1034 int ident; 1035 1036 /* LE link does not support tools like l2ping so use the full range */ 1037 if (conn->hcon->type == LE_LINK) 1038 max = 255; 1039 /* Get next available identificator. 1040 * 1 - 128 are used by kernel. 1041 * 129 - 199 are reserved. 1042 * 200 - 254 are used by utilities like l2ping, etc. 1043 */ 1044 else 1045 max = 128; 1046 1047 /* Allocate ident using min as last used + 1 (cyclic) */ 1048 ident = ida_alloc_range(&conn->tx_ida, READ_ONCE(conn->tx_ident) + 1, 1049 max, GFP_ATOMIC); 1050 /* Force min 1 to start over */ 1051 if (ident <= 0) { 1052 ident = ida_alloc_range(&conn->tx_ida, 1, max, GFP_ATOMIC); 1053 if (ident <= 0) { 1054 /* If all idents are in use, log an error, this is 1055 * extremely unlikely to happen and would indicate a bug 1056 * in the code that idents are not being freed properly. 1057 */ 1058 BT_ERR("Unable to allocate ident: %d", ident); 1059 return 0; 1060 } 1061 } 1062 1063 WRITE_ONCE(conn->tx_ident, ident); 1064 1065 return ident; 1066 } 1067 1068 static void l2cap_send_acl(struct l2cap_conn *conn, struct sk_buff *skb, 1069 u8 flags) 1070 { 1071 /* Check if the hcon still valid before attempting to send */ 1072 if (hci_conn_valid(conn->hcon->hdev, conn->hcon)) 1073 hci_send_acl(conn->hchan, skb, flags); 1074 else 1075 kfree_skb(skb); 1076 } 1077 1078 static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, 1079 void *data) 1080 { 1081 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 1082 u8 flags; 1083 1084 BT_DBG("code 0x%2.2x", code); 1085 1086 if (!skb) 1087 return; 1088 1089 /* Use NO_FLUSH if supported or we have an LE link (which does 1090 * not support auto-flushing packets) */ 1091 if (lmp_no_flush_capable(conn->hcon->hdev) || 1092 conn->hcon->type == LE_LINK) 1093 flags = ACL_START_NO_FLUSH; 1094 else 1095 flags = ACL_START; 1096 1097 bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; 1098 skb->priority = HCI_PRIO_MAX; 1099 1100 l2cap_send_acl(conn, skb, flags); 1101 } 1102 1103 static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 1104 { 1105 struct hci_conn *hcon = chan->conn->hcon; 1106 u16 flags; 1107 1108 BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, 1109 skb->priority); 1110 1111 /* Use NO_FLUSH for LE links (where this is the only option) or 1112 * if the BR/EDR link supports it and flushing has not been 1113 * explicitly requested (through FLAG_FLUSHABLE). 1114 */ 1115 if (hcon->type == LE_LINK || 1116 (!test_bit(FLAG_FLUSHABLE, &chan->flags) && 1117 lmp_no_flush_capable(hcon->hdev))) 1118 flags = ACL_START_NO_FLUSH; 1119 else 1120 flags = ACL_START; 1121 1122 bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); 1123 hci_send_acl(chan->conn->hchan, skb, flags); 1124 } 1125 1126 static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) 1127 { 1128 control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; 1129 control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; 1130 1131 if (enh & L2CAP_CTRL_FRAME_TYPE) { 1132 /* S-Frame */ 1133 control->sframe = 1; 1134 control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; 1135 control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; 1136 1137 control->sar = 0; 1138 control->txseq = 0; 1139 } else { 1140 /* I-Frame */ 1141 control->sframe = 0; 1142 control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; 1143 control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; 1144 1145 control->poll = 0; 1146 control->super = 0; 1147 } 1148 } 1149 1150 static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) 1151 { 1152 control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; 1153 control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; 1154 1155 if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { 1156 /* S-Frame */ 1157 control->sframe = 1; 1158 control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; 1159 control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; 1160 1161 control->sar = 0; 1162 control->txseq = 0; 1163 } else { 1164 /* I-Frame */ 1165 control->sframe = 0; 1166 control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; 1167 control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; 1168 1169 control->poll = 0; 1170 control->super = 0; 1171 } 1172 } 1173 1174 static inline void __unpack_control(struct l2cap_chan *chan, 1175 struct sk_buff *skb) 1176 { 1177 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { 1178 __unpack_extended_control(get_unaligned_le32(skb->data), 1179 &bt_cb(skb)->l2cap); 1180 skb_pull(skb, L2CAP_EXT_CTRL_SIZE); 1181 } else { 1182 __unpack_enhanced_control(get_unaligned_le16(skb->data), 1183 &bt_cb(skb)->l2cap); 1184 skb_pull(skb, L2CAP_ENH_CTRL_SIZE); 1185 } 1186 } 1187 1188 static u32 __pack_extended_control(struct l2cap_ctrl *control) 1189 { 1190 u32 packed; 1191 1192 packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; 1193 packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; 1194 1195 if (control->sframe) { 1196 packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; 1197 packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; 1198 packed |= L2CAP_EXT_CTRL_FRAME_TYPE; 1199 } else { 1200 packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; 1201 packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; 1202 } 1203 1204 return packed; 1205 } 1206 1207 static u16 __pack_enhanced_control(struct l2cap_ctrl *control) 1208 { 1209 u16 packed; 1210 1211 packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; 1212 packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; 1213 1214 if (control->sframe) { 1215 packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; 1216 packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; 1217 packed |= L2CAP_CTRL_FRAME_TYPE; 1218 } else { 1219 packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; 1220 packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; 1221 } 1222 1223 return packed; 1224 } 1225 1226 static inline void __pack_control(struct l2cap_chan *chan, 1227 struct l2cap_ctrl *control, 1228 struct sk_buff *skb) 1229 { 1230 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { 1231 put_unaligned_le32(__pack_extended_control(control), 1232 skb->data + L2CAP_HDR_SIZE); 1233 } else { 1234 put_unaligned_le16(__pack_enhanced_control(control), 1235 skb->data + L2CAP_HDR_SIZE); 1236 } 1237 } 1238 1239 static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) 1240 { 1241 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 1242 return L2CAP_EXT_HDR_SIZE; 1243 else 1244 return L2CAP_ENH_HDR_SIZE; 1245 } 1246 1247 static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, 1248 u32 control) 1249 { 1250 struct sk_buff *skb; 1251 struct l2cap_hdr *lh; 1252 int hlen = __ertm_hdr_size(chan); 1253 1254 if (chan->fcs == L2CAP_FCS_CRC16) 1255 hlen += L2CAP_FCS_SIZE; 1256 1257 skb = bt_skb_alloc(hlen, GFP_KERNEL); 1258 1259 if (!skb) 1260 return ERR_PTR(-ENOMEM); 1261 1262 lh = skb_put(skb, L2CAP_HDR_SIZE); 1263 lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 1264 lh->cid = cpu_to_le16(chan->dcid); 1265 1266 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 1267 put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); 1268 else 1269 put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); 1270 1271 if (chan->fcs == L2CAP_FCS_CRC16) { 1272 u16 fcs = crc16(0, (u8 *)skb->data, skb->len); 1273 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); 1274 } 1275 1276 skb->priority = HCI_PRIO_MAX; 1277 return skb; 1278 } 1279 1280 static void l2cap_send_sframe(struct l2cap_chan *chan, 1281 struct l2cap_ctrl *control) 1282 { 1283 struct sk_buff *skb; 1284 u32 control_field; 1285 1286 BT_DBG("chan %p, control %p", chan, control); 1287 1288 if (!control->sframe) 1289 return; 1290 1291 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && 1292 !control->poll) 1293 control->final = 1; 1294 1295 if (control->super == L2CAP_SUPER_RR) 1296 clear_bit(CONN_RNR_SENT, &chan->conn_state); 1297 else if (control->super == L2CAP_SUPER_RNR) 1298 set_bit(CONN_RNR_SENT, &chan->conn_state); 1299 1300 if (control->super != L2CAP_SUPER_SREJ) { 1301 chan->last_acked_seq = control->reqseq; 1302 __clear_ack_timer(chan); 1303 } 1304 1305 BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, 1306 control->final, control->poll, control->super); 1307 1308 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 1309 control_field = __pack_extended_control(control); 1310 else 1311 control_field = __pack_enhanced_control(control); 1312 1313 skb = l2cap_create_sframe_pdu(chan, control_field); 1314 if (!IS_ERR(skb)) 1315 l2cap_do_send(chan, skb); 1316 } 1317 1318 static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll) 1319 { 1320 struct l2cap_ctrl control; 1321 1322 BT_DBG("chan %p, poll %d", chan, poll); 1323 1324 memset(&control, 0, sizeof(control)); 1325 control.sframe = 1; 1326 control.poll = poll; 1327 1328 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) 1329 control.super = L2CAP_SUPER_RNR; 1330 else 1331 control.super = L2CAP_SUPER_RR; 1332 1333 control.reqseq = chan->buffer_seq; 1334 l2cap_send_sframe(chan, &control); 1335 } 1336 1337 static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 1338 { 1339 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 1340 return true; 1341 1342 return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); 1343 } 1344 1345 void l2cap_send_conn_req(struct l2cap_chan *chan) 1346 { 1347 struct l2cap_conn *conn = chan->conn; 1348 struct l2cap_conn_req req; 1349 1350 req.scid = cpu_to_le16(chan->scid); 1351 req.psm = chan->psm; 1352 1353 chan->ident = l2cap_get_ident(conn); 1354 1355 set_bit(CONF_CONNECT_PEND, &chan->conf_state); 1356 1357 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); 1358 } 1359 1360 static void l2cap_chan_ready(struct l2cap_chan *chan) 1361 __must_hold(&chan->lock) 1362 __must_hold(&chan->conn->lock) 1363 { 1364 /* The channel may have already been flagged as connected in 1365 * case of receiving data before the L2CAP info req/rsp 1366 * procedure is complete. 1367 */ 1368 if (chan->state == BT_CONNECTED) 1369 return; 1370 1371 /* This clears all conf flags, including CONF_NOT_COMPLETE */ 1372 chan->conf_state = 0; 1373 __clear_chan_timer(chan); 1374 1375 switch (chan->mode) { 1376 case L2CAP_MODE_LE_FLOWCTL: 1377 case L2CAP_MODE_EXT_FLOWCTL: 1378 if (!chan->tx_credits) 1379 chan->ops->suspend(chan); 1380 break; 1381 } 1382 1383 chan->state = BT_CONNECTED; 1384 1385 chan->ops->ready(chan); 1386 } 1387 1388 static void l2cap_le_connect(struct l2cap_chan *chan) 1389 { 1390 struct l2cap_conn *conn = chan->conn; 1391 struct l2cap_le_conn_req req; 1392 1393 if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags)) 1394 return; 1395 1396 if (!chan->imtu) 1397 chan->imtu = chan->conn->mtu; 1398 1399 l2cap_le_flowctl_init(chan, 0); 1400 1401 memset(&req, 0, sizeof(req)); 1402 req.psm = chan->psm; 1403 req.scid = cpu_to_le16(chan->scid); 1404 req.mtu = cpu_to_le16(chan->imtu); 1405 req.mps = cpu_to_le16(chan->mps); 1406 req.credits = cpu_to_le16(chan->rx_credits); 1407 1408 chan->ident = l2cap_get_ident(conn); 1409 1410 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ, 1411 sizeof(req), &req); 1412 } 1413 1414 struct l2cap_ecred_conn_data { 1415 struct { 1416 struct l2cap_ecred_conn_req_hdr req; 1417 __le16 scid[L2CAP_ECRED_CONN_SCID_MAX]; 1418 } __packed pdu; 1419 struct l2cap_chan *chan; 1420 struct pid *pid; 1421 int count; 1422 }; 1423 1424 static void l2cap_ecred_defer_connect(struct l2cap_chan *chan, void *data) 1425 { 1426 struct l2cap_ecred_conn_data *conn = data; 1427 struct pid *pid; 1428 1429 if (chan == conn->chan) 1430 return; 1431 1432 if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) 1433 return; 1434 1435 pid = chan->ops->get_peer_pid(chan); 1436 1437 /* Only add deferred channels with the same PID/PSM */ 1438 if (conn->pid != pid || chan->psm != conn->chan->psm || chan->ident || 1439 chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT) 1440 return; 1441 1442 if (!test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) 1443 return; 1444 1445 if (test_and_set_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) 1446 return; 1447 1448 /* Unreachable, checked in l2cap_connect (+timer drops it if reached) */ 1449 if (WARN_ON_ONCE(conn->count >= ARRAY_SIZE(conn->pdu.scid))) 1450 return; 1451 1452 l2cap_ecred_init(chan, 0); 1453 1454 /* Set the same ident so we can match on the rsp */ 1455 chan->ident = conn->chan->ident; 1456 1457 /* Include all channels deferred */ 1458 conn->pdu.scid[conn->count] = cpu_to_le16(chan->scid); 1459 1460 conn->count++; 1461 } 1462 1463 static void l2cap_ecred_connect(struct l2cap_chan *chan) 1464 __must_hold(&chan->conn->lock) 1465 { 1466 struct l2cap_conn *conn = chan->conn; 1467 struct l2cap_ecred_conn_data data; 1468 1469 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) 1470 return; 1471 1472 if (test_and_set_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) 1473 return; 1474 1475 l2cap_ecred_init(chan, 0); 1476 1477 memset(&data, 0, sizeof(data)); 1478 data.pdu.req.psm = chan->psm; 1479 data.pdu.req.mtu = cpu_to_le16(chan->imtu); 1480 data.pdu.req.mps = cpu_to_le16(chan->mps); 1481 data.pdu.req.credits = cpu_to_le16(chan->rx_credits); 1482 data.pdu.scid[0] = cpu_to_le16(chan->scid); 1483 1484 chan->ident = l2cap_get_ident(conn); 1485 1486 data.count = 1; 1487 data.chan = chan; 1488 data.pid = chan->ops->get_peer_pid(chan); 1489 1490 __l2cap_chan_list(conn, l2cap_ecred_defer_connect, &data); 1491 1492 l2cap_send_cmd(conn, chan->ident, L2CAP_ECRED_CONN_REQ, 1493 sizeof(data.pdu.req) + data.count * sizeof(__le16), 1494 &data.pdu); 1495 } 1496 1497 static void l2cap_le_start(struct l2cap_chan *chan) 1498 __must_hold(&chan->lock) 1499 __must_hold(&chan->conn->lock) 1500 { 1501 struct l2cap_conn *conn = chan->conn; 1502 1503 if (!smp_conn_security(conn->hcon, chan->sec_level)) 1504 return; 1505 1506 if (!chan->psm) { 1507 l2cap_chan_ready(chan); 1508 return; 1509 } 1510 1511 if (chan->state == BT_CONNECT) { 1512 if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) 1513 l2cap_ecred_connect(chan); 1514 else 1515 l2cap_le_connect(chan); 1516 } 1517 } 1518 1519 static void l2cap_start_connection(struct l2cap_chan *chan) 1520 __must_hold(&chan->lock) 1521 __must_hold(&chan->conn->lock) 1522 { 1523 if (chan->conn->hcon->type == LE_LINK) { 1524 l2cap_le_start(chan); 1525 } else { 1526 l2cap_send_conn_req(chan); 1527 } 1528 } 1529 1530 static void l2cap_request_info(struct l2cap_conn *conn) 1531 { 1532 struct l2cap_info_req req; 1533 1534 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 1535 return; 1536 1537 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 1538 1539 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 1540 conn->info_ident = l2cap_get_ident(conn); 1541 1542 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); 1543 1544 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, 1545 sizeof(req), &req); 1546 } 1547 1548 static bool l2cap_check_enc_key_size(struct hci_conn *hcon, 1549 struct l2cap_chan *chan) 1550 { 1551 /* The minimum encryption key size needs to be enforced by the 1552 * host stack before establishing any L2CAP connections. The 1553 * specification in theory allows a minimum of 1, but to align 1554 * BR/EDR and LE transports, a minimum of 7 is chosen. 1555 * 1556 * This check might also be called for unencrypted connections 1557 * that have no key size requirements. Ensure that the link is 1558 * actually encrypted before enforcing a key size. 1559 */ 1560 int min_key_size = hcon->hdev->min_enc_key_size; 1561 1562 /* On FIPS security level, key size must be 16 bytes */ 1563 if (chan->sec_level == BT_SECURITY_FIPS) 1564 min_key_size = 16; 1565 1566 return (!test_bit(HCI_CONN_ENCRYPT, hcon->flags) || 1567 hcon->enc_key_size >= min_key_size); 1568 } 1569 1570 static void l2cap_do_start(struct l2cap_chan *chan) 1571 __must_hold(&chan->lock) 1572 __must_hold(&chan->conn->lock) 1573 { 1574 struct l2cap_conn *conn = chan->conn; 1575 1576 if (conn->hcon->type == LE_LINK) { 1577 l2cap_le_start(chan); 1578 return; 1579 } 1580 1581 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) { 1582 l2cap_request_info(conn); 1583 return; 1584 } 1585 1586 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 1587 return; 1588 1589 if (!l2cap_chan_check_security(chan, true) || 1590 !__l2cap_no_conn_pending(chan)) 1591 return; 1592 1593 if (l2cap_check_enc_key_size(conn->hcon, chan)) 1594 l2cap_start_connection(chan); 1595 else 1596 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 1597 } 1598 1599 static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 1600 { 1601 u32 local_feat_mask = l2cap_feat_mask; 1602 if (!disable_ertm) 1603 local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 1604 1605 switch (mode) { 1606 case L2CAP_MODE_ERTM: 1607 return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 1608 case L2CAP_MODE_STREAMING: 1609 return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 1610 default: 1611 return 0x00; 1612 } 1613 } 1614 1615 static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err) 1616 { 1617 struct l2cap_conn *conn = chan->conn; 1618 struct l2cap_disconn_req req; 1619 1620 if (!conn) 1621 return; 1622 1623 if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { 1624 __clear_retrans_timer(chan); 1625 __clear_monitor_timer(chan); 1626 __clear_ack_timer(chan); 1627 } 1628 1629 req.dcid = cpu_to_le16(chan->dcid); 1630 req.scid = cpu_to_le16(chan->scid); 1631 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, 1632 sizeof(req), &req); 1633 1634 l2cap_state_change_and_error(chan, BT_DISCONN, err); 1635 } 1636 1637 /* ---- L2CAP connections ---- */ 1638 static void l2cap_conn_start(struct l2cap_conn *conn) 1639 __must_hold(&conn->lock) 1640 { 1641 struct l2cap_chan *chan, *tmp; 1642 1643 BT_DBG("conn %p", conn); 1644 1645 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 1646 l2cap_chan_lock(chan); 1647 1648 lockdep_assert_held(&chan->conn->lock); 1649 1650 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1651 l2cap_chan_ready(chan); 1652 l2cap_chan_unlock(chan); 1653 continue; 1654 } 1655 1656 if (chan->state == BT_CONNECT) { 1657 if (!l2cap_chan_check_security(chan, true) || 1658 !__l2cap_no_conn_pending(chan)) { 1659 l2cap_chan_unlock(chan); 1660 continue; 1661 } 1662 1663 if (!l2cap_mode_supported(chan->mode, conn->feat_mask) 1664 && test_bit(CONF_STATE2_DEVICE, 1665 &chan->conf_state)) { 1666 __l2cap_chan_close(chan, ECONNRESET); 1667 l2cap_chan_unlock(chan); 1668 continue; 1669 } 1670 1671 if (l2cap_check_enc_key_size(conn->hcon, chan)) 1672 l2cap_start_connection(chan); 1673 else 1674 __l2cap_chan_close(chan, ECONNREFUSED); 1675 1676 } else if (chan->state == BT_CONNECT2) { 1677 struct l2cap_conn_rsp rsp; 1678 char buf[128]; 1679 rsp.scid = cpu_to_le16(chan->dcid); 1680 rsp.dcid = cpu_to_le16(chan->scid); 1681 1682 if (l2cap_chan_check_security(chan, false)) { 1683 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { 1684 rsp.result = cpu_to_le16(L2CAP_CR_PEND); 1685 rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 1686 chan->ops->defer(chan); 1687 1688 } else { 1689 l2cap_state_change(chan, BT_CONFIG); 1690 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 1691 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 1692 } 1693 } else { 1694 rsp.result = cpu_to_le16(L2CAP_CR_PEND); 1695 rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 1696 } 1697 1698 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 1699 sizeof(rsp), &rsp); 1700 1701 if (test_bit(CONF_REQ_SENT, &chan->conf_state) || 1702 rsp.result != L2CAP_CR_SUCCESS) { 1703 l2cap_chan_unlock(chan); 1704 continue; 1705 } 1706 1707 set_bit(CONF_REQ_SENT, &chan->conf_state); 1708 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 1709 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf); 1710 chan->num_conf_req++; 1711 } 1712 1713 l2cap_chan_unlock(chan); 1714 } 1715 } 1716 1717 static void l2cap_le_conn_ready(struct l2cap_conn *conn) 1718 { 1719 struct hci_conn *hcon = conn->hcon; 1720 struct hci_dev *hdev = hcon->hdev; 1721 1722 BT_DBG("%s conn %p", hdev->name, conn); 1723 1724 /* For outgoing pairing which doesn't necessarily have an 1725 * associated socket (e.g. mgmt_pair_device). 1726 */ 1727 if (hcon->out) 1728 smp_conn_security(hcon, hcon->pending_sec_level); 1729 1730 /* For LE peripheral connections, make sure the connection interval 1731 * is in the range of the minimum and maximum interval that has 1732 * been configured for this connection. If not, then trigger 1733 * the connection update procedure. 1734 */ 1735 if (hcon->role == HCI_ROLE_SLAVE && 1736 (hcon->le_conn_interval < hcon->le_conn_min_interval || 1737 hcon->le_conn_interval > hcon->le_conn_max_interval)) { 1738 struct l2cap_conn_param_update_req req; 1739 1740 req.min = cpu_to_le16(hcon->le_conn_min_interval); 1741 req.max = cpu_to_le16(hcon->le_conn_max_interval); 1742 req.latency = cpu_to_le16(hcon->le_conn_latency); 1743 req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout); 1744 1745 l2cap_send_cmd(conn, l2cap_get_ident(conn), 1746 L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req); 1747 } 1748 } 1749 1750 static bool l2cap_is_blocked(struct hci_conn *hcon) 1751 __must_hold(&hcon->hdev->lock) 1752 { 1753 /* Since we can't actively block incoming LE connections we must 1754 * at least ensure that we ignore incoming data from them. 1755 */ 1756 return hcon->type == LE_LINK && 1757 hci_bdaddr_list_lookup(&hcon->hdev->reject_list, &hcon->dst, 1758 bdaddr_dst_type(hcon)); 1759 } 1760 1761 static void l2cap_queue_pending_rx(struct hci_conn *hcon) 1762 __must_hold(&hcon->hdev->lock) 1763 { 1764 struct l2cap_conn *conn = hcon->l2cap_data; 1765 1766 mutex_lock(&conn->lock); 1767 1768 if (!skb_queue_empty(&conn->pending_rx)) { 1769 /* Teardown holds hdev->lock while cancelling the worker. 1770 * Pass the decision under conn->lock to avoid that lock. 1771 */ 1772 conn->pending_rx_blocked = l2cap_is_blocked(hcon); 1773 queue_work(hcon->hdev->workqueue, &conn->pending_rx_work); 1774 } 1775 1776 mutex_unlock(&conn->lock); 1777 } 1778 1779 static void l2cap_conn_ready(struct hci_conn *hcon) 1780 __must_hold(&hcon->hdev->lock) 1781 { 1782 struct l2cap_conn *conn = hcon->l2cap_data; 1783 struct l2cap_chan *chan; 1784 1785 BT_DBG("conn %p", conn); 1786 1787 if (hcon->type == ACL_LINK) 1788 l2cap_request_info(conn); 1789 1790 mutex_lock(&conn->lock); 1791 1792 list_for_each_entry(chan, &conn->chan_l, list) { 1793 1794 l2cap_chan_lock(chan); 1795 1796 lockdep_assert_held(&chan->conn->lock); 1797 1798 if (hcon->type == LE_LINK) { 1799 l2cap_le_start(chan); 1800 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1801 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 1802 l2cap_chan_ready(chan); 1803 } else if (chan->state == BT_CONNECT) { 1804 l2cap_do_start(chan); 1805 } 1806 1807 l2cap_chan_unlock(chan); 1808 } 1809 1810 mutex_unlock(&conn->lock); 1811 1812 if (hcon->type == LE_LINK) 1813 l2cap_le_conn_ready(conn); 1814 1815 l2cap_queue_pending_rx(hcon); 1816 } 1817 1818 /* Notify sockets that we cannot guaranty reliability anymore */ 1819 static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 1820 __must_hold(&conn->lock) 1821 { 1822 struct l2cap_chan *chan; 1823 1824 BT_DBG("conn %p", conn); 1825 1826 list_for_each_entry(chan, &conn->chan_l, list) { 1827 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) 1828 l2cap_chan_set_err(chan, err); 1829 } 1830 } 1831 1832 static void l2cap_info_timeout(struct work_struct *work) 1833 { 1834 struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 1835 info_timer.work); 1836 1837 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 1838 conn->info_ident = 0; 1839 1840 mutex_lock(&conn->lock); 1841 l2cap_conn_start(conn); 1842 mutex_unlock(&conn->lock); 1843 } 1844 1845 /* 1846 * l2cap_user 1847 * External modules can register l2cap_user objects on l2cap_conn. The ->probe 1848 * callback is called during registration. The ->remove callback is called 1849 * during unregistration. 1850 * An l2cap_user object can either be explicitly unregistered or when the 1851 * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon, 1852 * l2cap->hchan, .. are valid as long as the remove callback hasn't been called. 1853 * External modules must own a reference to the l2cap_conn object if they intend 1854 * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at 1855 * any time if they don't. 1856 */ 1857 1858 int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user) 1859 { 1860 int ret; 1861 1862 /* We need to check whether l2cap_conn is registered. If it is not, we 1863 * must not register the l2cap_user. l2cap_conn_del() unregisters 1864 * l2cap_conn objects under conn->lock, and we use the same lock here 1865 * to protect access to conn->users and conn->hchan. 1866 */ 1867 1868 mutex_lock(&conn->lock); 1869 1870 if (!list_empty(&user->list)) { 1871 ret = -EINVAL; 1872 goto out_unlock; 1873 } 1874 1875 /* conn->hchan is NULL after l2cap_conn_del() was called */ 1876 if (!conn->hchan) { 1877 ret = -ENODEV; 1878 goto out_unlock; 1879 } 1880 1881 ret = user->probe(conn, user); 1882 if (ret) 1883 goto out_unlock; 1884 1885 list_add(&user->list, &conn->users); 1886 ret = 0; 1887 1888 out_unlock: 1889 mutex_unlock(&conn->lock); 1890 return ret; 1891 } 1892 EXPORT_SYMBOL(l2cap_register_user); 1893 1894 void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user) 1895 { 1896 mutex_lock(&conn->lock); 1897 1898 if (list_empty(&user->list)) 1899 goto out_unlock; 1900 1901 list_del_init(&user->list); 1902 user->remove(conn, user); 1903 1904 out_unlock: 1905 mutex_unlock(&conn->lock); 1906 } 1907 EXPORT_SYMBOL(l2cap_unregister_user); 1908 1909 static void l2cap_unregister_all_users(struct l2cap_conn *conn) 1910 { 1911 struct l2cap_user *user; 1912 1913 while (!list_empty(&conn->users)) { 1914 user = list_first_entry(&conn->users, struct l2cap_user, list); 1915 list_del_init(&user->list); 1916 user->remove(conn, user); 1917 } 1918 } 1919 1920 static void l2cap_conn_del(struct hci_conn *hcon, int err) 1921 __must_hold(&hcon->hdev->lock) 1922 { 1923 struct l2cap_conn *conn = hcon->l2cap_data; 1924 struct l2cap_chan *chan, *l; 1925 1926 if (!conn) 1927 return; 1928 1929 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 1930 1931 disable_delayed_work_sync(&conn->info_timer); 1932 disable_delayed_work_sync(&conn->id_addr_timer); 1933 1934 cancel_work_sync(&conn->pending_rx_work); 1935 1936 mutex_lock(&conn->lock); 1937 1938 kfree_skb(conn->rx_skb); 1939 1940 skb_queue_purge(&conn->pending_rx); 1941 ida_destroy(&conn->tx_ida); 1942 1943 l2cap_unregister_all_users(conn); 1944 1945 /* Force the connection to be immediately dropped */ 1946 hcon->disc_timeout = 0; 1947 1948 /* Kill channels */ 1949 list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 1950 l2cap_chan_hold(chan); 1951 l2cap_chan_lock(chan); 1952 1953 lockdep_assert_held(&chan->conn->lock); 1954 1955 l2cap_chan_del(chan, err); 1956 1957 chan->ops->close(chan); 1958 1959 l2cap_chan_unlock(chan); 1960 l2cap_chan_put(chan); 1961 } 1962 1963 hci_chan_del(conn->hchan); 1964 conn->hchan = NULL; 1965 1966 spin_lock(&hcon->proto_lock); 1967 hcon->l2cap_data = NULL; 1968 spin_unlock(&hcon->proto_lock); 1969 1970 mutex_unlock(&conn->lock); 1971 l2cap_conn_put(conn); 1972 } 1973 1974 static void l2cap_conn_free(struct kref *ref) 1975 { 1976 struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); 1977 1978 hci_conn_put(conn->hcon); 1979 kfree(conn); 1980 } 1981 1982 struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn) 1983 { 1984 kref_get(&conn->ref); 1985 return conn; 1986 } 1987 EXPORT_SYMBOL(l2cap_conn_get); 1988 1989 void l2cap_conn_put(struct l2cap_conn *conn) 1990 { 1991 kref_put(&conn->ref, l2cap_conn_free); 1992 } 1993 EXPORT_SYMBOL(l2cap_conn_put); 1994 1995 /* ---- Socket interface ---- */ 1996 1997 /* Find socket with psm and source / destination bdaddr. 1998 * Returns closest match. 1999 */ 2000 static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, 2001 bdaddr_t *src, 2002 bdaddr_t *dst, 2003 u8 link_type) 2004 { 2005 struct l2cap_chan *c, *tmp, *c1 = NULL; 2006 2007 read_lock(&chan_list_lock); 2008 2009 list_for_each_entry_safe(c, tmp, &chan_list, global_l) { 2010 if (state && c->state != state) 2011 continue; 2012 2013 if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR) 2014 continue; 2015 2016 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR) 2017 continue; 2018 2019 if (c->chan_type != L2CAP_CHAN_FIXED && c->psm == psm) { 2020 int src_match, dst_match; 2021 int src_any, dst_any; 2022 2023 /* Exact match. */ 2024 src_match = !bacmp(&c->src, src); 2025 dst_match = !bacmp(&c->dst, dst); 2026 if (src_match && dst_match) { 2027 if (!l2cap_chan_hold_unless_zero(c)) 2028 continue; 2029 2030 read_unlock(&chan_list_lock); 2031 return c; 2032 } 2033 2034 /* Closest match */ 2035 src_any = !bacmp(&c->src, BDADDR_ANY); 2036 dst_any = !bacmp(&c->dst, BDADDR_ANY); 2037 if ((src_match && dst_any) || (src_any && dst_match) || 2038 (src_any && dst_any)) 2039 c1 = c; 2040 } 2041 } 2042 2043 if (c1) 2044 c1 = l2cap_chan_hold_unless_zero(c1); 2045 2046 read_unlock(&chan_list_lock); 2047 2048 return c1; 2049 } 2050 2051 static void l2cap_monitor_timeout(struct work_struct *work) 2052 { 2053 struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 2054 monitor_timer.work); 2055 2056 BT_DBG("chan %p", chan); 2057 2058 l2cap_chan_lock(chan); 2059 2060 if (test_bit(FLAG_DEL, &chan->flags)) { 2061 l2cap_chan_unlock(chan); 2062 l2cap_chan_put(chan); 2063 return; 2064 } 2065 2066 l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); 2067 2068 l2cap_chan_unlock(chan); 2069 l2cap_chan_put(chan); 2070 } 2071 2072 static void l2cap_retrans_timeout(struct work_struct *work) 2073 { 2074 struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 2075 retrans_timer.work); 2076 2077 BT_DBG("chan %p", chan); 2078 2079 l2cap_chan_lock(chan); 2080 2081 if (test_bit(FLAG_DEL, &chan->flags)) { 2082 l2cap_chan_unlock(chan); 2083 l2cap_chan_put(chan); 2084 return; 2085 } 2086 2087 l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); 2088 l2cap_chan_unlock(chan); 2089 l2cap_chan_put(chan); 2090 } 2091 2092 static void l2cap_streaming_send(struct l2cap_chan *chan, 2093 struct sk_buff_head *skbs) 2094 { 2095 struct sk_buff *skb; 2096 struct l2cap_ctrl *control; 2097 2098 BT_DBG("chan %p, skbs %p", chan, skbs); 2099 2100 skb_queue_splice_tail_init(skbs, &chan->tx_q); 2101 2102 while (!skb_queue_empty(&chan->tx_q)) { 2103 2104 skb = skb_dequeue(&chan->tx_q); 2105 2106 bt_cb(skb)->l2cap.retries = 1; 2107 control = &bt_cb(skb)->l2cap; 2108 2109 control->reqseq = 0; 2110 control->txseq = chan->next_tx_seq; 2111 2112 __pack_control(chan, control, skb); 2113 2114 if (chan->fcs == L2CAP_FCS_CRC16) { 2115 u16 fcs = crc16(0, (u8 *) skb->data, skb->len); 2116 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); 2117 } 2118 2119 l2cap_do_send(chan, skb); 2120 2121 BT_DBG("Sent txseq %u", control->txseq); 2122 2123 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 2124 chan->frames_sent++; 2125 } 2126 } 2127 2128 static int l2cap_ertm_send(struct l2cap_chan *chan) 2129 { 2130 struct sk_buff *skb, *tx_skb; 2131 struct l2cap_ctrl *control; 2132 int sent = 0; 2133 2134 BT_DBG("chan %p", chan); 2135 2136 if (chan->state != BT_CONNECTED) 2137 return -ENOTCONN; 2138 2139 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) 2140 return 0; 2141 2142 while (chan->tx_send_head && 2143 chan->unacked_frames < chan->remote_tx_win && 2144 chan->tx_state == L2CAP_TX_STATE_XMIT) { 2145 2146 skb = chan->tx_send_head; 2147 2148 bt_cb(skb)->l2cap.retries = 1; 2149 control = &bt_cb(skb)->l2cap; 2150 2151 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 2152 control->final = 1; 2153 2154 control->reqseq = chan->buffer_seq; 2155 chan->last_acked_seq = chan->buffer_seq; 2156 control->txseq = chan->next_tx_seq; 2157 2158 __pack_control(chan, control, skb); 2159 2160 if (chan->fcs == L2CAP_FCS_CRC16) { 2161 u16 fcs = crc16(0, (u8 *) skb->data, skb->len); 2162 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); 2163 } 2164 2165 /* Clone after data has been modified. Data is assumed to be 2166 read-only (for locking purposes) on cloned sk_buffs. 2167 */ 2168 tx_skb = skb_clone(skb, GFP_KERNEL); 2169 2170 if (!tx_skb) 2171 break; 2172 2173 __set_retrans_timer(chan); 2174 2175 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 2176 chan->unacked_frames++; 2177 chan->frames_sent++; 2178 sent++; 2179 2180 if (skb_queue_is_last(&chan->tx_q, skb)) 2181 chan->tx_send_head = NULL; 2182 else 2183 chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 2184 2185 l2cap_do_send(chan, tx_skb); 2186 BT_DBG("Sent txseq %u", control->txseq); 2187 } 2188 2189 BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, 2190 chan->unacked_frames, skb_queue_len(&chan->tx_q)); 2191 2192 return sent; 2193 } 2194 2195 static void l2cap_ertm_resend(struct l2cap_chan *chan) 2196 { 2197 struct l2cap_ctrl control; 2198 struct sk_buff *skb; 2199 struct sk_buff *tx_skb; 2200 u16 seq; 2201 2202 BT_DBG("chan %p", chan); 2203 2204 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) 2205 return; 2206 2207 while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { 2208 seq = l2cap_seq_list_pop(&chan->retrans_list); 2209 2210 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); 2211 if (!skb) { 2212 BT_DBG("Error: Can't retransmit seq %d, frame missing", 2213 seq); 2214 continue; 2215 } 2216 2217 bt_cb(skb)->l2cap.retries++; 2218 control = bt_cb(skb)->l2cap; 2219 2220 if (chan->max_tx != 0 && 2221 bt_cb(skb)->l2cap.retries > chan->max_tx) { 2222 BT_DBG("Retry limit exceeded (%d)", chan->max_tx); 2223 l2cap_send_disconn_req(chan, ECONNRESET); 2224 l2cap_seq_list_clear(&chan->retrans_list); 2225 break; 2226 } 2227 2228 control.reqseq = chan->buffer_seq; 2229 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 2230 control.final = 1; 2231 else 2232 control.final = 0; 2233 2234 if (skb_cloned(skb)) { 2235 /* Cloned sk_buffs are read-only, so we need a 2236 * writeable copy 2237 */ 2238 tx_skb = skb_copy(skb, GFP_KERNEL); 2239 } else { 2240 tx_skb = skb_clone(skb, GFP_KERNEL); 2241 } 2242 2243 if (!tx_skb) { 2244 l2cap_seq_list_clear(&chan->retrans_list); 2245 break; 2246 } 2247 2248 /* Update skb contents */ 2249 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { 2250 put_unaligned_le32(__pack_extended_control(&control), 2251 tx_skb->data + L2CAP_HDR_SIZE); 2252 } else { 2253 put_unaligned_le16(__pack_enhanced_control(&control), 2254 tx_skb->data + L2CAP_HDR_SIZE); 2255 } 2256 2257 /* Update FCS */ 2258 if (chan->fcs == L2CAP_FCS_CRC16) { 2259 u16 fcs = crc16(0, (u8 *) tx_skb->data, 2260 tx_skb->len - L2CAP_FCS_SIZE); 2261 put_unaligned_le16(fcs, skb_tail_pointer(tx_skb) - 2262 L2CAP_FCS_SIZE); 2263 } 2264 2265 l2cap_do_send(chan, tx_skb); 2266 2267 BT_DBG("Resent txseq %d", control.txseq); 2268 2269 chan->last_acked_seq = chan->buffer_seq; 2270 } 2271 } 2272 2273 static void l2cap_retransmit(struct l2cap_chan *chan, 2274 struct l2cap_ctrl *control) 2275 { 2276 BT_DBG("chan %p, control %p", chan, control); 2277 2278 l2cap_seq_list_append(&chan->retrans_list, control->reqseq); 2279 l2cap_ertm_resend(chan); 2280 } 2281 2282 static void l2cap_retransmit_all(struct l2cap_chan *chan, 2283 struct l2cap_ctrl *control) 2284 { 2285 struct sk_buff *skb; 2286 2287 BT_DBG("chan %p, control %p", chan, control); 2288 2289 if (control->poll) 2290 set_bit(CONN_SEND_FBIT, &chan->conn_state); 2291 2292 l2cap_seq_list_clear(&chan->retrans_list); 2293 2294 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) 2295 return; 2296 2297 if (chan->unacked_frames) { 2298 skb_queue_walk(&chan->tx_q, skb) { 2299 if (bt_cb(skb)->l2cap.txseq == control->reqseq || 2300 skb == chan->tx_send_head) 2301 break; 2302 } 2303 2304 skb_queue_walk_from(&chan->tx_q, skb) { 2305 if (skb == chan->tx_send_head) 2306 break; 2307 2308 l2cap_seq_list_append(&chan->retrans_list, 2309 bt_cb(skb)->l2cap.txseq); 2310 } 2311 2312 l2cap_ertm_resend(chan); 2313 } 2314 } 2315 2316 static void l2cap_send_ack(struct l2cap_chan *chan) 2317 { 2318 struct l2cap_ctrl control; 2319 u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, 2320 chan->last_acked_seq); 2321 int threshold; 2322 2323 BT_DBG("chan %p last_acked_seq %d buffer_seq %d", 2324 chan, chan->last_acked_seq, chan->buffer_seq); 2325 2326 memset(&control, 0, sizeof(control)); 2327 control.sframe = 1; 2328 2329 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && 2330 chan->rx_state == L2CAP_RX_STATE_RECV) { 2331 __clear_ack_timer(chan); 2332 control.super = L2CAP_SUPER_RNR; 2333 control.reqseq = chan->buffer_seq; 2334 l2cap_send_sframe(chan, &control); 2335 } else { 2336 if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { 2337 l2cap_ertm_send(chan); 2338 /* If any i-frames were sent, they included an ack */ 2339 if (chan->buffer_seq == chan->last_acked_seq) 2340 frames_to_ack = 0; 2341 } 2342 2343 /* Ack now if the window is 3/4ths full. 2344 * Calculate without mul or div 2345 */ 2346 threshold = chan->ack_win; 2347 threshold += threshold << 1; 2348 threshold >>= 2; 2349 2350 BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, 2351 threshold); 2352 2353 if (frames_to_ack >= threshold) { 2354 __clear_ack_timer(chan); 2355 control.super = L2CAP_SUPER_RR; 2356 control.reqseq = chan->buffer_seq; 2357 l2cap_send_sframe(chan, &control); 2358 frames_to_ack = 0; 2359 } 2360 2361 if (frames_to_ack) 2362 __set_ack_timer(chan); 2363 } 2364 } 2365 2366 static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, 2367 struct msghdr *msg, int len, 2368 int count, struct sk_buff *skb) 2369 { 2370 struct l2cap_conn *conn = chan->conn; 2371 struct sk_buff **frag; 2372 int sent = 0; 2373 2374 if (!copy_from_iter_full(skb_put(skb, count), count, &msg->msg_iter)) 2375 return -EFAULT; 2376 2377 sent += count; 2378 len -= count; 2379 2380 /* Continuation fragments (no L2CAP header) */ 2381 frag = &skb_shinfo(skb)->frag_list; 2382 while (len) { 2383 struct sk_buff *tmp; 2384 2385 count = min_t(unsigned int, conn->mtu, len); 2386 2387 tmp = chan->ops->alloc_skb(chan, 0, count, 2388 msg->msg_flags & MSG_DONTWAIT); 2389 if (IS_ERR(tmp)) 2390 return PTR_ERR(tmp); 2391 2392 *frag = tmp; 2393 2394 if (!copy_from_iter_full(skb_put(*frag, count), count, 2395 &msg->msg_iter)) 2396 return -EFAULT; 2397 2398 sent += count; 2399 len -= count; 2400 2401 skb->len += (*frag)->len; 2402 skb->data_len += (*frag)->len; 2403 2404 frag = &(*frag)->next; 2405 } 2406 2407 return sent; 2408 } 2409 2410 static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, 2411 struct msghdr *msg, size_t len) 2412 { 2413 struct l2cap_conn *conn = chan->conn; 2414 struct sk_buff *skb; 2415 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; 2416 struct l2cap_hdr *lh; 2417 2418 BT_DBG("chan %p psm 0x%2.2x len %zu", chan, 2419 __le16_to_cpu(chan->psm), len); 2420 2421 count = min_t(unsigned int, (conn->mtu - hlen), len); 2422 2423 skb = chan->ops->alloc_skb(chan, hlen, count, 2424 msg->msg_flags & MSG_DONTWAIT); 2425 if (IS_ERR(skb)) 2426 return skb; 2427 2428 /* Create L2CAP header */ 2429 lh = skb_put(skb, L2CAP_HDR_SIZE); 2430 lh->cid = cpu_to_le16(chan->dcid); 2431 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); 2432 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE)); 2433 2434 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 2435 if (unlikely(err < 0)) { 2436 kfree_skb(skb); 2437 return ERR_PTR(err); 2438 } 2439 return skb; 2440 } 2441 2442 static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, 2443 struct msghdr *msg, size_t len) 2444 { 2445 struct l2cap_conn *conn = chan->conn; 2446 struct sk_buff *skb; 2447 int err, count; 2448 struct l2cap_hdr *lh; 2449 2450 BT_DBG("chan %p len %zu", chan, len); 2451 2452 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); 2453 2454 skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count, 2455 msg->msg_flags & MSG_DONTWAIT); 2456 if (IS_ERR(skb)) 2457 return skb; 2458 2459 /* Create L2CAP header */ 2460 lh = skb_put(skb, L2CAP_HDR_SIZE); 2461 lh->cid = cpu_to_le16(chan->dcid); 2462 lh->len = cpu_to_le16(len); 2463 2464 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 2465 if (unlikely(err < 0)) { 2466 kfree_skb(skb); 2467 return ERR_PTR(err); 2468 } 2469 return skb; 2470 } 2471 2472 static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, 2473 struct msghdr *msg, size_t len, 2474 u16 sdulen) 2475 { 2476 struct l2cap_conn *conn = chan->conn; 2477 struct sk_buff *skb; 2478 int err, count, hlen; 2479 struct l2cap_hdr *lh; 2480 2481 BT_DBG("chan %p len %zu", chan, len); 2482 2483 if (!conn) 2484 return ERR_PTR(-ENOTCONN); 2485 2486 hlen = __ertm_hdr_size(chan); 2487 2488 if (sdulen) 2489 hlen += L2CAP_SDULEN_SIZE; 2490 2491 if (chan->fcs == L2CAP_FCS_CRC16) 2492 hlen += L2CAP_FCS_SIZE; 2493 2494 count = min_t(unsigned int, (conn->mtu - hlen), len); 2495 2496 skb = chan->ops->alloc_skb(chan, hlen, count, 2497 msg->msg_flags & MSG_DONTWAIT); 2498 if (IS_ERR(skb)) 2499 return skb; 2500 2501 /* Create L2CAP header */ 2502 lh = skb_put(skb, L2CAP_HDR_SIZE); 2503 lh->cid = cpu_to_le16(chan->dcid); 2504 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 2505 2506 /* Control header is populated later */ 2507 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 2508 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); 2509 else 2510 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); 2511 2512 if (sdulen) 2513 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); 2514 2515 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 2516 if (unlikely(err < 0)) { 2517 kfree_skb(skb); 2518 return ERR_PTR(err); 2519 } 2520 2521 bt_cb(skb)->l2cap.fcs = chan->fcs; 2522 bt_cb(skb)->l2cap.retries = 0; 2523 return skb; 2524 } 2525 2526 static int l2cap_segment_sdu(struct l2cap_chan *chan, 2527 struct sk_buff_head *seg_queue, 2528 struct msghdr *msg, size_t len) 2529 { 2530 struct sk_buff *skb; 2531 u16 sdu_len; 2532 size_t pdu_len; 2533 u8 sar; 2534 2535 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); 2536 2537 /* It is critical that ERTM PDUs fit in a single HCI fragment, 2538 * so fragmented skbs are not used. The HCI layer's handling 2539 * of fragmented skbs is not compatible with ERTM's queueing. 2540 */ 2541 2542 /* PDU size is derived from the HCI MTU */ 2543 pdu_len = chan->conn->mtu; 2544 2545 /* Constrain PDU size for BR/EDR connections */ 2546 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); 2547 2548 /* Adjust for largest possible L2CAP overhead. */ 2549 if (chan->fcs) 2550 pdu_len -= L2CAP_FCS_SIZE; 2551 2552 pdu_len -= __ertm_hdr_size(chan); 2553 2554 /* Remote device may have requested smaller PDUs */ 2555 pdu_len = min_t(size_t, pdu_len, chan->remote_mps); 2556 2557 if (!pdu_len) 2558 return -EINVAL; 2559 2560 if (len <= pdu_len) { 2561 sar = L2CAP_SAR_UNSEGMENTED; 2562 sdu_len = 0; 2563 pdu_len = len; 2564 } else { 2565 sar = L2CAP_SAR_START; 2566 sdu_len = len; 2567 } 2568 2569 while (len > 0) { 2570 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); 2571 2572 if (IS_ERR(skb)) { 2573 __skb_queue_purge(seg_queue); 2574 return PTR_ERR(skb); 2575 } 2576 2577 bt_cb(skb)->l2cap.sar = sar; 2578 __skb_queue_tail(seg_queue, skb); 2579 2580 len -= pdu_len; 2581 if (sdu_len) 2582 sdu_len = 0; 2583 2584 if (len <= pdu_len) { 2585 sar = L2CAP_SAR_END; 2586 pdu_len = len; 2587 } else { 2588 sar = L2CAP_SAR_CONTINUE; 2589 } 2590 } 2591 2592 return 0; 2593 } 2594 2595 static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan, 2596 struct msghdr *msg, 2597 size_t len, u16 sdulen) 2598 { 2599 struct l2cap_conn *conn = chan->conn; 2600 struct sk_buff *skb; 2601 int err, count, hlen; 2602 struct l2cap_hdr *lh; 2603 2604 BT_DBG("chan %p len %zu", chan, len); 2605 2606 if (!conn) 2607 return ERR_PTR(-ENOTCONN); 2608 2609 hlen = L2CAP_HDR_SIZE; 2610 2611 if (sdulen) 2612 hlen += L2CAP_SDULEN_SIZE; 2613 2614 count = min_t(unsigned int, (conn->mtu - hlen), len); 2615 2616 skb = chan->ops->alloc_skb(chan, hlen, count, 2617 msg->msg_flags & MSG_DONTWAIT); 2618 if (IS_ERR(skb)) 2619 return skb; 2620 2621 /* Create L2CAP header */ 2622 lh = skb_put(skb, L2CAP_HDR_SIZE); 2623 lh->cid = cpu_to_le16(chan->dcid); 2624 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 2625 2626 if (sdulen) 2627 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); 2628 2629 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 2630 if (unlikely(err < 0)) { 2631 kfree_skb(skb); 2632 return ERR_PTR(err); 2633 } 2634 2635 return skb; 2636 } 2637 2638 static int l2cap_segment_le_sdu(struct l2cap_chan *chan, 2639 struct sk_buff_head *seg_queue, 2640 struct msghdr *msg, size_t len) 2641 { 2642 struct sk_buff *skb; 2643 size_t pdu_len; 2644 u16 sdu_len; 2645 2646 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); 2647 2648 sdu_len = len; 2649 pdu_len = chan->remote_mps - L2CAP_SDULEN_SIZE; 2650 2651 while (len > 0) { 2652 if (len <= pdu_len) 2653 pdu_len = len; 2654 2655 skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len); 2656 if (IS_ERR(skb)) { 2657 __skb_queue_purge(seg_queue); 2658 return PTR_ERR(skb); 2659 } 2660 2661 __skb_queue_tail(seg_queue, skb); 2662 2663 len -= pdu_len; 2664 2665 if (sdu_len) { 2666 sdu_len = 0; 2667 pdu_len += L2CAP_SDULEN_SIZE; 2668 } 2669 } 2670 2671 return 0; 2672 } 2673 2674 static void l2cap_le_flowctl_send(struct l2cap_chan *chan) 2675 { 2676 int sent = 0; 2677 2678 BT_DBG("chan %p", chan); 2679 2680 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { 2681 l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); 2682 chan->tx_credits--; 2683 sent++; 2684 } 2685 2686 BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, 2687 skb_queue_len(&chan->tx_q)); 2688 } 2689 2690 static void l2cap_tx_timestamp(struct sk_buff *skb, 2691 const struct sockcm_cookie *sockc, 2692 size_t len) 2693 { 2694 struct sock *sk = skb ? skb->sk : NULL; 2695 2696 if (sk && sk->sk_type == SOCK_STREAM) 2697 hci_setup_tx_timestamp(skb, len, sockc); 2698 else 2699 hci_setup_tx_timestamp(skb, 1, sockc); 2700 } 2701 2702 static void l2cap_tx_timestamp_seg(struct sk_buff_head *queue, 2703 const struct sockcm_cookie *sockc, 2704 size_t len) 2705 { 2706 struct sk_buff *skb = skb_peek(queue); 2707 struct sock *sk = skb ? skb->sk : NULL; 2708 2709 if (sk && sk->sk_type == SOCK_STREAM) 2710 l2cap_tx_timestamp(skb_peek_tail(queue), sockc, len); 2711 else 2712 l2cap_tx_timestamp(skb, sockc, len); 2713 } 2714 2715 int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, 2716 const struct sockcm_cookie *sockc) 2717 { 2718 struct sk_buff *skb; 2719 int err; 2720 struct sk_buff_head seg_queue; 2721 2722 if (test_bit(FLAG_DEL, &chan->flags)) 2723 return -ENOTCONN; 2724 2725 /* Connectionless channel */ 2726 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 2727 skb = l2cap_create_connless_pdu(chan, msg, len); 2728 if (IS_ERR(skb)) 2729 return PTR_ERR(skb); 2730 2731 l2cap_tx_timestamp(skb, sockc, len); 2732 2733 l2cap_do_send(chan, skb); 2734 return len; 2735 } 2736 2737 switch (chan->mode) { 2738 case L2CAP_MODE_LE_FLOWCTL: 2739 case L2CAP_MODE_EXT_FLOWCTL: 2740 /* Check outgoing MTU */ 2741 if (len > chan->omtu) 2742 return -EMSGSIZE; 2743 2744 __skb_queue_head_init(&seg_queue); 2745 2746 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len); 2747 2748 if (chan->state != BT_CONNECTED) { 2749 __skb_queue_purge(&seg_queue); 2750 err = -ENOTCONN; 2751 } 2752 2753 if (err) 2754 return err; 2755 2756 l2cap_tx_timestamp_seg(&seg_queue, sockc, len); 2757 2758 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); 2759 2760 l2cap_le_flowctl_send(chan); 2761 2762 if (!chan->tx_credits) 2763 chan->ops->suspend(chan); 2764 2765 err = len; 2766 2767 break; 2768 2769 case L2CAP_MODE_BASIC: 2770 /* Check outgoing MTU */ 2771 if (len > chan->omtu) 2772 return -EMSGSIZE; 2773 2774 /* Create a basic PDU */ 2775 skb = l2cap_create_basic_pdu(chan, msg, len); 2776 if (IS_ERR(skb)) 2777 return PTR_ERR(skb); 2778 2779 l2cap_tx_timestamp(skb, sockc, len); 2780 2781 l2cap_do_send(chan, skb); 2782 err = len; 2783 break; 2784 2785 case L2CAP_MODE_ERTM: 2786 case L2CAP_MODE_STREAMING: 2787 /* Check outgoing MTU */ 2788 if (len > chan->omtu) { 2789 err = -EMSGSIZE; 2790 break; 2791 } 2792 2793 __skb_queue_head_init(&seg_queue); 2794 2795 /* Do segmentation before calling in to the state machine, 2796 * since it's possible to block while waiting for memory 2797 * allocation. 2798 */ 2799 err = l2cap_segment_sdu(chan, &seg_queue, msg, len); 2800 2801 if (err) 2802 break; 2803 2804 if (chan->mode == L2CAP_MODE_ERTM) { 2805 /* TODO: ERTM mode timestamping */ 2806 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); 2807 } else { 2808 l2cap_tx_timestamp_seg(&seg_queue, sockc, len); 2809 l2cap_streaming_send(chan, &seg_queue); 2810 } 2811 2812 err = len; 2813 2814 /* If the skbs were not queued for sending, they'll still be in 2815 * seg_queue and need to be purged. 2816 */ 2817 __skb_queue_purge(&seg_queue); 2818 break; 2819 2820 default: 2821 BT_DBG("bad state %1.1x", chan->mode); 2822 err = -EBADFD; 2823 } 2824 2825 return err; 2826 } 2827 EXPORT_SYMBOL_GPL(l2cap_chan_send); 2828 2829 static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) 2830 { 2831 struct l2cap_ctrl control; 2832 u16 seq; 2833 2834 BT_DBG("chan %p, txseq %u", chan, txseq); 2835 2836 memset(&control, 0, sizeof(control)); 2837 control.sframe = 1; 2838 control.super = L2CAP_SUPER_SREJ; 2839 2840 for (seq = chan->expected_tx_seq; seq != txseq; 2841 seq = __next_seq(chan, seq)) { 2842 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { 2843 control.reqseq = seq; 2844 l2cap_send_sframe(chan, &control); 2845 l2cap_seq_list_append(&chan->srej_list, seq); 2846 } 2847 } 2848 2849 chan->expected_tx_seq = __next_seq(chan, txseq); 2850 } 2851 2852 static void l2cap_send_srej_tail(struct l2cap_chan *chan) 2853 { 2854 struct l2cap_ctrl control; 2855 2856 BT_DBG("chan %p", chan); 2857 2858 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) 2859 return; 2860 2861 memset(&control, 0, sizeof(control)); 2862 control.sframe = 1; 2863 control.super = L2CAP_SUPER_SREJ; 2864 control.reqseq = chan->srej_list.tail; 2865 l2cap_send_sframe(chan, &control); 2866 } 2867 2868 static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) 2869 { 2870 struct l2cap_ctrl control; 2871 u16 initial_head; 2872 u16 seq; 2873 2874 BT_DBG("chan %p, txseq %u", chan, txseq); 2875 2876 memset(&control, 0, sizeof(control)); 2877 control.sframe = 1; 2878 control.super = L2CAP_SUPER_SREJ; 2879 2880 /* Capture initial list head to allow only one pass through the list. */ 2881 initial_head = chan->srej_list.head; 2882 2883 do { 2884 seq = l2cap_seq_list_pop(&chan->srej_list); 2885 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) 2886 break; 2887 2888 control.reqseq = seq; 2889 l2cap_send_sframe(chan, &control); 2890 l2cap_seq_list_append(&chan->srej_list, seq); 2891 } while (chan->srej_list.head != initial_head); 2892 } 2893 2894 static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) 2895 { 2896 struct sk_buff *acked_skb; 2897 u16 ackseq; 2898 2899 BT_DBG("chan %p, reqseq %u", chan, reqseq); 2900 2901 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) 2902 return; 2903 2904 BT_DBG("expected_ack_seq %u, unacked_frames %u", 2905 chan->expected_ack_seq, chan->unacked_frames); 2906 2907 for (ackseq = chan->expected_ack_seq; ackseq != reqseq; 2908 ackseq = __next_seq(chan, ackseq)) { 2909 2910 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); 2911 if (acked_skb) { 2912 skb_unlink(acked_skb, &chan->tx_q); 2913 kfree_skb(acked_skb); 2914 chan->unacked_frames--; 2915 } 2916 } 2917 2918 chan->expected_ack_seq = reqseq; 2919 2920 if (chan->unacked_frames == 0) 2921 __clear_retrans_timer(chan); 2922 2923 BT_DBG("unacked_frames %u", chan->unacked_frames); 2924 } 2925 2926 static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) 2927 { 2928 BT_DBG("chan %p", chan); 2929 2930 chan->expected_tx_seq = chan->buffer_seq; 2931 l2cap_seq_list_clear(&chan->srej_list); 2932 skb_queue_purge(&chan->srej_q); 2933 chan->rx_state = L2CAP_RX_STATE_RECV; 2934 } 2935 2936 static void l2cap_tx_state_xmit(struct l2cap_chan *chan, 2937 struct l2cap_ctrl *control, 2938 struct sk_buff_head *skbs, u8 event) 2939 { 2940 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, 2941 event); 2942 2943 switch (event) { 2944 case L2CAP_EV_DATA_REQUEST: 2945 if (chan->tx_send_head == NULL) 2946 chan->tx_send_head = skb_peek(skbs); 2947 2948 skb_queue_splice_tail_init(skbs, &chan->tx_q); 2949 l2cap_ertm_send(chan); 2950 break; 2951 case L2CAP_EV_LOCAL_BUSY_DETECTED: 2952 BT_DBG("Enter LOCAL_BUSY"); 2953 set_bit(CONN_LOCAL_BUSY, &chan->conn_state); 2954 2955 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { 2956 /* The SREJ_SENT state must be aborted if we are to 2957 * enter the LOCAL_BUSY state. 2958 */ 2959 l2cap_abort_rx_srej_sent(chan); 2960 } 2961 2962 l2cap_send_ack(chan); 2963 2964 break; 2965 case L2CAP_EV_LOCAL_BUSY_CLEAR: 2966 BT_DBG("Exit LOCAL_BUSY"); 2967 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); 2968 2969 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { 2970 struct l2cap_ctrl local_control; 2971 2972 memset(&local_control, 0, sizeof(local_control)); 2973 local_control.sframe = 1; 2974 local_control.super = L2CAP_SUPER_RR; 2975 local_control.poll = 1; 2976 local_control.reqseq = chan->buffer_seq; 2977 l2cap_send_sframe(chan, &local_control); 2978 2979 chan->retry_count = 1; 2980 __set_monitor_timer(chan); 2981 chan->tx_state = L2CAP_TX_STATE_WAIT_F; 2982 } 2983 break; 2984 case L2CAP_EV_RECV_REQSEQ_AND_FBIT: 2985 l2cap_process_reqseq(chan, control->reqseq); 2986 break; 2987 case L2CAP_EV_EXPLICIT_POLL: 2988 l2cap_send_rr_or_rnr(chan, 1); 2989 chan->retry_count = 1; 2990 __set_monitor_timer(chan); 2991 __clear_ack_timer(chan); 2992 chan->tx_state = L2CAP_TX_STATE_WAIT_F; 2993 break; 2994 case L2CAP_EV_RETRANS_TO: 2995 l2cap_send_rr_or_rnr(chan, 1); 2996 chan->retry_count = 1; 2997 __set_monitor_timer(chan); 2998 chan->tx_state = L2CAP_TX_STATE_WAIT_F; 2999 break; 3000 case L2CAP_EV_RECV_FBIT: 3001 /* Nothing to process */ 3002 break; 3003 default: 3004 break; 3005 } 3006 } 3007 3008 static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, 3009 struct l2cap_ctrl *control, 3010 struct sk_buff_head *skbs, u8 event) 3011 { 3012 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, 3013 event); 3014 3015 switch (event) { 3016 case L2CAP_EV_DATA_REQUEST: 3017 if (chan->tx_send_head == NULL) 3018 chan->tx_send_head = skb_peek(skbs); 3019 /* Queue data, but don't send. */ 3020 skb_queue_splice_tail_init(skbs, &chan->tx_q); 3021 break; 3022 case L2CAP_EV_LOCAL_BUSY_DETECTED: 3023 BT_DBG("Enter LOCAL_BUSY"); 3024 set_bit(CONN_LOCAL_BUSY, &chan->conn_state); 3025 3026 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { 3027 /* The SREJ_SENT state must be aborted if we are to 3028 * enter the LOCAL_BUSY state. 3029 */ 3030 l2cap_abort_rx_srej_sent(chan); 3031 } 3032 3033 l2cap_send_ack(chan); 3034 3035 break; 3036 case L2CAP_EV_LOCAL_BUSY_CLEAR: 3037 BT_DBG("Exit LOCAL_BUSY"); 3038 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); 3039 3040 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { 3041 struct l2cap_ctrl local_control; 3042 memset(&local_control, 0, sizeof(local_control)); 3043 local_control.sframe = 1; 3044 local_control.super = L2CAP_SUPER_RR; 3045 local_control.poll = 1; 3046 local_control.reqseq = chan->buffer_seq; 3047 l2cap_send_sframe(chan, &local_control); 3048 3049 chan->retry_count = 1; 3050 __set_monitor_timer(chan); 3051 chan->tx_state = L2CAP_TX_STATE_WAIT_F; 3052 } 3053 break; 3054 case L2CAP_EV_RECV_REQSEQ_AND_FBIT: 3055 l2cap_process_reqseq(chan, control->reqseq); 3056 fallthrough; 3057 3058 case L2CAP_EV_RECV_FBIT: 3059 if (control && control->final) { 3060 __clear_monitor_timer(chan); 3061 if (chan->unacked_frames > 0) 3062 __set_retrans_timer(chan); 3063 chan->retry_count = 0; 3064 chan->tx_state = L2CAP_TX_STATE_XMIT; 3065 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); 3066 } 3067 break; 3068 case L2CAP_EV_EXPLICIT_POLL: 3069 /* Ignore */ 3070 break; 3071 case L2CAP_EV_MONITOR_TO: 3072 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { 3073 l2cap_send_rr_or_rnr(chan, 1); 3074 __set_monitor_timer(chan); 3075 chan->retry_count++; 3076 } else { 3077 l2cap_send_disconn_req(chan, ECONNABORTED); 3078 } 3079 break; 3080 default: 3081 break; 3082 } 3083 } 3084 3085 static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, 3086 struct sk_buff_head *skbs, u8 event) 3087 { 3088 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", 3089 chan, control, skbs, event, chan->tx_state); 3090 3091 switch (chan->tx_state) { 3092 case L2CAP_TX_STATE_XMIT: 3093 l2cap_tx_state_xmit(chan, control, skbs, event); 3094 break; 3095 case L2CAP_TX_STATE_WAIT_F: 3096 l2cap_tx_state_wait_f(chan, control, skbs, event); 3097 break; 3098 default: 3099 /* Ignore event */ 3100 break; 3101 } 3102 } 3103 3104 static void l2cap_pass_to_tx(struct l2cap_chan *chan, 3105 struct l2cap_ctrl *control) 3106 { 3107 BT_DBG("chan %p, control %p", chan, control); 3108 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); 3109 } 3110 3111 static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, 3112 struct l2cap_ctrl *control) 3113 { 3114 BT_DBG("chan %p, control %p", chan, control); 3115 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); 3116 } 3117 3118 /* Copy frame to all raw sockets on that connection */ 3119 static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 3120 __must_hold(&conn->lock) 3121 { 3122 struct sk_buff *nskb; 3123 struct l2cap_chan *chan; 3124 3125 BT_DBG("conn %p", conn); 3126 3127 list_for_each_entry(chan, &conn->chan_l, list) { 3128 if (chan->chan_type != L2CAP_CHAN_RAW) 3129 continue; 3130 3131 /* Don't send frame to the channel it came from */ 3132 if (bt_cb(skb)->l2cap.chan == chan) 3133 continue; 3134 3135 nskb = skb_clone(skb, GFP_KERNEL); 3136 if (!nskb) 3137 continue; 3138 if (chan->ops->recv(chan, nskb)) 3139 kfree_skb(nskb); 3140 } 3141 } 3142 3143 /* ---- L2CAP signalling commands ---- */ 3144 static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, 3145 u8 ident, u16 dlen, void *data) 3146 { 3147 struct sk_buff *skb, **frag; 3148 struct l2cap_cmd_hdr *cmd; 3149 struct l2cap_hdr *lh; 3150 int len, count; 3151 3152 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", 3153 conn, code, ident, dlen); 3154 3155 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) 3156 return NULL; 3157 3158 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 3159 count = min_t(unsigned int, conn->mtu, len); 3160 3161 skb = bt_skb_alloc(count, GFP_KERNEL); 3162 if (!skb) 3163 return NULL; 3164 3165 lh = skb_put(skb, L2CAP_HDR_SIZE); 3166 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 3167 3168 if (conn->hcon->type == LE_LINK) 3169 lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 3170 else 3171 lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 3172 3173 cmd = skb_put(skb, L2CAP_CMD_HDR_SIZE); 3174 cmd->code = code; 3175 cmd->ident = ident; 3176 cmd->len = cpu_to_le16(dlen); 3177 3178 if (dlen) { 3179 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 3180 skb_put_data(skb, data, count); 3181 data += count; 3182 } 3183 3184 len -= skb->len; 3185 3186 /* Continuation fragments (no L2CAP header) */ 3187 frag = &skb_shinfo(skb)->frag_list; 3188 while (len) { 3189 count = min_t(unsigned int, conn->mtu, len); 3190 3191 *frag = bt_skb_alloc(count, GFP_KERNEL); 3192 if (!*frag) 3193 goto fail; 3194 3195 skb_put_data(*frag, data, count); 3196 3197 len -= count; 3198 data += count; 3199 3200 frag = &(*frag)->next; 3201 } 3202 3203 return skb; 3204 3205 fail: 3206 kfree_skb(skb); 3207 return NULL; 3208 } 3209 3210 static inline int l2cap_get_conf_opt(void **ptr, void *end, int *type, 3211 int *olen, unsigned long *val) 3212 { 3213 struct l2cap_conf_opt *opt = *ptr; 3214 int len; 3215 3216 /* opt->len is attacker-controlled. Validate that the full option 3217 * (header + value) actually fits in the buffer before touching 3218 * opt->val, otherwise the switch below reads past the end of the 3219 * caller's buffer. 3220 */ 3221 if (end - *ptr < L2CAP_CONF_OPT_SIZE) 3222 return -EINVAL; 3223 3224 len = L2CAP_CONF_OPT_SIZE + opt->len; 3225 if (end - *ptr < len) 3226 return -EINVAL; 3227 3228 *ptr += len; 3229 3230 *type = opt->type; 3231 *olen = opt->len; 3232 3233 switch (opt->len) { 3234 case 1: 3235 *val = *((u8 *) opt->val); 3236 break; 3237 3238 case 2: 3239 *val = get_unaligned_le16(opt->val); 3240 break; 3241 3242 case 4: 3243 *val = get_unaligned_le32(opt->val); 3244 break; 3245 3246 default: 3247 *val = (unsigned long) opt->val; 3248 break; 3249 } 3250 3251 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val); 3252 return len; 3253 } 3254 3255 static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val, size_t size) 3256 { 3257 struct l2cap_conf_opt *opt = *ptr; 3258 3259 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val); 3260 3261 if (size < L2CAP_CONF_OPT_SIZE + len) 3262 return; 3263 3264 opt->type = type; 3265 opt->len = len; 3266 3267 switch (len) { 3268 case 1: 3269 *((u8 *) opt->val) = val; 3270 break; 3271 3272 case 2: 3273 put_unaligned_le16(val, opt->val); 3274 break; 3275 3276 case 4: 3277 put_unaligned_le32(val, opt->val); 3278 break; 3279 3280 default: 3281 memcpy(opt->val, (void *) val, len); 3282 break; 3283 } 3284 3285 *ptr += L2CAP_CONF_OPT_SIZE + len; 3286 } 3287 3288 static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan, size_t size) 3289 { 3290 struct l2cap_conf_efs efs; 3291 3292 switch (chan->mode) { 3293 case L2CAP_MODE_ERTM: 3294 efs.id = chan->local_id; 3295 efs.stype = chan->local_stype; 3296 efs.msdu = cpu_to_le16(chan->local_msdu); 3297 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 3298 efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); 3299 efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); 3300 break; 3301 3302 case L2CAP_MODE_STREAMING: 3303 efs.id = 1; 3304 efs.stype = L2CAP_SERV_BESTEFFORT; 3305 efs.msdu = cpu_to_le16(chan->local_msdu); 3306 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 3307 efs.acc_lat = 0; 3308 efs.flush_to = 0; 3309 break; 3310 3311 default: 3312 return; 3313 } 3314 3315 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), 3316 (unsigned long) &efs, size); 3317 } 3318 3319 static void l2cap_ack_timeout(struct work_struct *work) 3320 { 3321 struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 3322 ack_timer.work); 3323 u16 frames_to_ack; 3324 3325 BT_DBG("chan %p", chan); 3326 3327 l2cap_chan_lock(chan); 3328 3329 if (test_bit(FLAG_DEL, &chan->flags)) 3330 goto unlock; 3331 3332 frames_to_ack = __seq_offset(chan, chan->buffer_seq, 3333 chan->last_acked_seq); 3334 3335 if (frames_to_ack) 3336 l2cap_send_rr_or_rnr(chan, 0); 3337 3338 unlock: 3339 l2cap_chan_unlock(chan); 3340 l2cap_chan_put(chan); 3341 } 3342 3343 int l2cap_ertm_init(struct l2cap_chan *chan) 3344 { 3345 int err; 3346 3347 chan->next_tx_seq = 0; 3348 chan->expected_tx_seq = 0; 3349 chan->expected_ack_seq = 0; 3350 chan->unacked_frames = 0; 3351 chan->buffer_seq = 0; 3352 chan->frames_sent = 0; 3353 chan->last_acked_seq = 0; 3354 chan->sdu = NULL; 3355 chan->sdu_last_frag = NULL; 3356 chan->sdu_len = 0; 3357 3358 skb_queue_head_init(&chan->tx_q); 3359 3360 if (chan->mode != L2CAP_MODE_ERTM) 3361 return 0; 3362 3363 chan->rx_state = L2CAP_RX_STATE_RECV; 3364 chan->tx_state = L2CAP_TX_STATE_XMIT; 3365 3366 skb_queue_head_init(&chan->srej_q); 3367 3368 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); 3369 if (err < 0) 3370 return err; 3371 3372 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); 3373 if (err < 0) 3374 l2cap_seq_list_free(&chan->srej_list); 3375 3376 return err; 3377 } 3378 3379 static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 3380 { 3381 switch (mode) { 3382 case L2CAP_MODE_STREAMING: 3383 case L2CAP_MODE_ERTM: 3384 if (l2cap_mode_supported(mode, remote_feat_mask)) 3385 return mode; 3386 fallthrough; 3387 default: 3388 return L2CAP_MODE_BASIC; 3389 } 3390 } 3391 3392 static inline bool __l2cap_ews_supported(struct l2cap_conn *conn) 3393 { 3394 return (conn->feat_mask & L2CAP_FEAT_EXT_WINDOW); 3395 } 3396 3397 static inline bool __l2cap_efs_supported(struct l2cap_conn *conn) 3398 { 3399 return (conn->feat_mask & L2CAP_FEAT_EXT_FLOW); 3400 } 3401 3402 static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, 3403 struct l2cap_conf_rfc *rfc) 3404 { 3405 rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); 3406 rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); 3407 } 3408 3409 static inline void l2cap_txwin_setup(struct l2cap_chan *chan) 3410 { 3411 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && 3412 __l2cap_ews_supported(chan->conn)) { 3413 /* use extended control field */ 3414 set_bit(FLAG_EXT_CTRL, &chan->flags); 3415 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 3416 } else { 3417 chan->tx_win = min_t(u16, chan->tx_win, 3418 L2CAP_DEFAULT_TX_WINDOW); 3419 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; 3420 } 3421 chan->ack_win = chan->tx_win; 3422 } 3423 3424 static void l2cap_mtu_auto(struct l2cap_chan *chan) 3425 { 3426 struct hci_conn *conn = chan->conn->hcon; 3427 3428 chan->imtu = L2CAP_DEFAULT_MIN_MTU; 3429 3430 /* The 2-DH1 packet has between 2 and 56 information bytes 3431 * (including the 2-byte payload header) 3432 */ 3433 if (!(conn->pkt_type & HCI_2DH1)) 3434 chan->imtu = 54; 3435 3436 /* The 3-DH1 packet has between 2 and 85 information bytes 3437 * (including the 2-byte payload header) 3438 */ 3439 if (!(conn->pkt_type & HCI_3DH1)) 3440 chan->imtu = 83; 3441 3442 /* The 2-DH3 packet has between 2 and 369 information bytes 3443 * (including the 2-byte payload header) 3444 */ 3445 if (!(conn->pkt_type & HCI_2DH3)) 3446 chan->imtu = 367; 3447 3448 /* The 3-DH3 packet has between 2 and 554 information bytes 3449 * (including the 2-byte payload header) 3450 */ 3451 if (!(conn->pkt_type & HCI_3DH3)) 3452 chan->imtu = 552; 3453 3454 /* The 2-DH5 packet has between 2 and 681 information bytes 3455 * (including the 2-byte payload header) 3456 */ 3457 if (!(conn->pkt_type & HCI_2DH5)) 3458 chan->imtu = 679; 3459 3460 /* The 3-DH5 packet has between 2 and 1023 information bytes 3461 * (including the 2-byte payload header) 3462 */ 3463 if (!(conn->pkt_type & HCI_3DH5)) 3464 chan->imtu = 1021; 3465 } 3466 3467 static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data_size) 3468 { 3469 struct l2cap_conf_req *req = data; 3470 struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 3471 void *ptr = req->data; 3472 void *endptr = data + data_size; 3473 u16 size; 3474 3475 BT_DBG("chan %p", chan); 3476 3477 if (chan->num_conf_req || chan->num_conf_rsp) 3478 goto done; 3479 3480 switch (chan->mode) { 3481 case L2CAP_MODE_STREAMING: 3482 case L2CAP_MODE_ERTM: 3483 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) 3484 break; 3485 3486 if (__l2cap_efs_supported(chan->conn)) 3487 set_bit(FLAG_EFS_ENABLE, &chan->flags); 3488 3489 fallthrough; 3490 default: 3491 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 3492 break; 3493 } 3494 3495 done: 3496 if (chan->imtu != L2CAP_DEFAULT_MTU) { 3497 if (!chan->imtu) 3498 l2cap_mtu_auto(chan); 3499 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu, 3500 endptr - ptr); 3501 } 3502 3503 switch (chan->mode) { 3504 case L2CAP_MODE_BASIC: 3505 if (disable_ertm) 3506 break; 3507 3508 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 3509 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 3510 break; 3511 3512 rfc.mode = L2CAP_MODE_BASIC; 3513 rfc.txwin_size = 0; 3514 rfc.max_transmit = 0; 3515 rfc.retrans_timeout = 0; 3516 rfc.monitor_timeout = 0; 3517 rfc.max_pdu_size = 0; 3518 3519 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 3520 (unsigned long) &rfc, endptr - ptr); 3521 break; 3522 3523 case L2CAP_MODE_ERTM: 3524 rfc.mode = L2CAP_MODE_ERTM; 3525 rfc.max_transmit = chan->max_tx; 3526 3527 __l2cap_set_ertm_timeouts(chan, &rfc); 3528 3529 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 3530 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - 3531 L2CAP_FCS_SIZE); 3532 rfc.max_pdu_size = cpu_to_le16(size); 3533 3534 l2cap_txwin_setup(chan); 3535 3536 rfc.txwin_size = min_t(u16, chan->tx_win, 3537 L2CAP_DEFAULT_TX_WINDOW); 3538 3539 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 3540 (unsigned long) &rfc, endptr - ptr); 3541 3542 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 3543 l2cap_add_opt_efs(&ptr, chan, endptr - ptr); 3544 3545 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 3546 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 3547 chan->tx_win, endptr - ptr); 3548 3549 if (chan->conn->feat_mask & L2CAP_FEAT_FCS) 3550 if (chan->fcs == L2CAP_FCS_NONE || 3551 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { 3552 chan->fcs = L2CAP_FCS_NONE; 3553 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, 3554 chan->fcs, endptr - ptr); 3555 } 3556 break; 3557 3558 case L2CAP_MODE_STREAMING: 3559 l2cap_txwin_setup(chan); 3560 rfc.mode = L2CAP_MODE_STREAMING; 3561 rfc.txwin_size = 0; 3562 rfc.max_transmit = 0; 3563 rfc.retrans_timeout = 0; 3564 rfc.monitor_timeout = 0; 3565 3566 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 3567 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - 3568 L2CAP_FCS_SIZE); 3569 rfc.max_pdu_size = cpu_to_le16(size); 3570 3571 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 3572 (unsigned long) &rfc, endptr - ptr); 3573 3574 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 3575 l2cap_add_opt_efs(&ptr, chan, endptr - ptr); 3576 3577 if (chan->conn->feat_mask & L2CAP_FEAT_FCS) 3578 if (chan->fcs == L2CAP_FCS_NONE || 3579 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { 3580 chan->fcs = L2CAP_FCS_NONE; 3581 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, 3582 chan->fcs, endptr - ptr); 3583 } 3584 break; 3585 } 3586 3587 req->dcid = cpu_to_le16(chan->dcid); 3588 req->flags = cpu_to_le16(0); 3589 3590 return ptr - data; 3591 } 3592 3593 static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data_size) 3594 { 3595 struct l2cap_conf_rsp *rsp = data; 3596 void *ptr = rsp->data; 3597 void *endptr = data + data_size; 3598 void *req = chan->conf_req; 3599 void *req_end = req + chan->conf_len; 3600 int len = chan->conf_len; 3601 int type, hint, olen; 3602 unsigned long val; 3603 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 3604 struct l2cap_conf_efs efs; 3605 u8 remote_efs = 0; 3606 u16 mtu = 0; 3607 u16 result = L2CAP_CONF_SUCCESS; 3608 u16 size; 3609 3610 BT_DBG("chan %p", chan); 3611 3612 while (len >= L2CAP_CONF_OPT_SIZE) { 3613 int ret = l2cap_get_conf_opt(&req, req_end, &type, &olen, &val); 3614 3615 if (ret < 0) 3616 break; 3617 len -= ret; 3618 3619 hint = type & L2CAP_CONF_HINT; 3620 type &= L2CAP_CONF_MASK; 3621 3622 switch (type) { 3623 case L2CAP_CONF_MTU: 3624 if (olen != 2) 3625 break; 3626 mtu = val; 3627 break; 3628 3629 case L2CAP_CONF_FLUSH_TO: 3630 if (olen != 2) 3631 break; 3632 chan->flush_to = val; 3633 break; 3634 3635 case L2CAP_CONF_QOS: 3636 break; 3637 3638 case L2CAP_CONF_RFC: 3639 if (olen != sizeof(rfc)) 3640 break; 3641 memcpy(&rfc, (void *) val, olen); 3642 break; 3643 3644 case L2CAP_CONF_FCS: 3645 if (olen != 1) 3646 break; 3647 if (val == L2CAP_FCS_NONE) 3648 set_bit(CONF_RECV_NO_FCS, &chan->conf_state); 3649 break; 3650 3651 case L2CAP_CONF_EFS: 3652 if (olen != sizeof(efs)) 3653 break; 3654 remote_efs = 1; 3655 memcpy(&efs, (void *) val, olen); 3656 break; 3657 3658 case L2CAP_CONF_EWS: 3659 if (olen != 2) 3660 break; 3661 return -ECONNREFUSED; 3662 3663 default: 3664 if (hint) 3665 break; 3666 result = L2CAP_CONF_UNKNOWN; 3667 l2cap_add_conf_opt(&ptr, (u8)type, sizeof(u8), type, endptr - ptr); 3668 break; 3669 } 3670 } 3671 3672 if (chan->num_conf_rsp || chan->num_conf_req > 1) 3673 goto done; 3674 3675 switch (chan->mode) { 3676 case L2CAP_MODE_STREAMING: 3677 case L2CAP_MODE_ERTM: 3678 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { 3679 chan->mode = l2cap_select_mode(rfc.mode, 3680 chan->conn->feat_mask); 3681 break; 3682 } 3683 3684 if (remote_efs) { 3685 if (__l2cap_efs_supported(chan->conn)) 3686 set_bit(FLAG_EFS_ENABLE, &chan->flags); 3687 else 3688 return -ECONNREFUSED; 3689 } 3690 3691 if (chan->mode != rfc.mode) 3692 return -ECONNREFUSED; 3693 3694 break; 3695 } 3696 3697 done: 3698 if (chan->mode != rfc.mode) { 3699 result = L2CAP_CONF_UNACCEPT; 3700 rfc.mode = chan->mode; 3701 3702 if (chan->num_conf_rsp == 1) 3703 return -ECONNREFUSED; 3704 3705 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 3706 (unsigned long) &rfc, endptr - ptr); 3707 } 3708 3709 if (result == L2CAP_CONF_SUCCESS) { 3710 /* Configure output options and let the other side know 3711 * which ones we don't like. */ 3712 3713 /* If MTU is not provided in configure request, try adjusting it 3714 * to the current output MTU if it has been set 3715 * 3716 * Bluetooth Core 6.1, Vol 3, Part A, Section 4.5 3717 * 3718 * Each configuration parameter value (if any is present) in an 3719 * L2CAP_CONFIGURATION_RSP packet reflects an ‘adjustment’ to a 3720 * configuration parameter value that has been sent (or, in case 3721 * of default values, implied) in the corresponding 3722 * L2CAP_CONFIGURATION_REQ packet. 3723 */ 3724 if (!mtu) { 3725 /* Only adjust for ERTM channels as for older modes the 3726 * remote stack may not be able to detect that the 3727 * adjustment causing it to silently drop packets. 3728 */ 3729 if (chan->mode == L2CAP_MODE_ERTM && 3730 chan->omtu && chan->omtu != L2CAP_DEFAULT_MTU) 3731 mtu = chan->omtu; 3732 else 3733 mtu = L2CAP_DEFAULT_MTU; 3734 } 3735 3736 if (mtu < L2CAP_DEFAULT_MIN_MTU) 3737 result = L2CAP_CONF_UNACCEPT; 3738 else { 3739 chan->omtu = mtu; 3740 set_bit(CONF_MTU_DONE, &chan->conf_state); 3741 } 3742 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu, endptr - ptr); 3743 3744 if (remote_efs) { 3745 if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 3746 efs.stype != L2CAP_SERV_NOTRAFIC && 3747 efs.stype != chan->local_stype) { 3748 3749 result = L2CAP_CONF_UNACCEPT; 3750 3751 if (chan->num_conf_req >= 1) 3752 return -ECONNREFUSED; 3753 3754 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 3755 sizeof(efs), 3756 (unsigned long) &efs, endptr - ptr); 3757 } else { 3758 /* Send PENDING Conf Rsp */ 3759 result = L2CAP_CONF_PENDING; 3760 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 3761 } 3762 } 3763 3764 switch (rfc.mode) { 3765 case L2CAP_MODE_BASIC: 3766 chan->fcs = L2CAP_FCS_NONE; 3767 set_bit(CONF_MODE_DONE, &chan->conf_state); 3768 break; 3769 3770 case L2CAP_MODE_ERTM: 3771 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 3772 chan->conn->mtu - L2CAP_EXT_HDR_SIZE - 3773 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); 3774 rfc.max_pdu_size = cpu_to_le16(size); 3775 3776 /* The TxWindow must be within 1 to 63, otherwise 3777 * propose the default one and keep the parameters of 3778 * an earlier request. This overrides a PENDING result 3779 * from the EFS check above, so the request is no 3780 * longer pending, nor done if an earlier one was. 3781 */ 3782 if (!rfc.txwin_size || 3783 rfc.txwin_size > L2CAP_DEFAULT_TX_WINDOW) { 3784 result = L2CAP_CONF_UNACCEPT; 3785 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 3786 clear_bit(CONF_OUTPUT_DONE, &chan->conf_state); 3787 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; 3788 remote_efs = 0; 3789 } else { 3790 chan->remote_tx_win = rfc.txwin_size; 3791 chan->remote_max_tx = rfc.max_transmit; 3792 chan->remote_mps = size; 3793 } 3794 3795 __l2cap_set_ertm_timeouts(chan, &rfc); 3796 3797 set_bit(CONF_MODE_DONE, &chan->conf_state); 3798 3799 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 3800 sizeof(rfc), (unsigned long) &rfc, endptr - ptr); 3801 3802 if (remote_efs && 3803 test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 3804 chan->remote_id = efs.id; 3805 chan->remote_stype = efs.stype; 3806 chan->remote_msdu = le16_to_cpu(efs.msdu); 3807 chan->remote_flush_to = 3808 le32_to_cpu(efs.flush_to); 3809 chan->remote_acc_lat = 3810 le32_to_cpu(efs.acc_lat); 3811 chan->remote_sdu_itime = 3812 le32_to_cpu(efs.sdu_itime); 3813 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 3814 sizeof(efs), 3815 (unsigned long) &efs, endptr - ptr); 3816 } 3817 break; 3818 3819 case L2CAP_MODE_STREAMING: 3820 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 3821 chan->conn->mtu - L2CAP_EXT_HDR_SIZE - 3822 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); 3823 rfc.max_pdu_size = cpu_to_le16(size); 3824 chan->remote_mps = size; 3825 3826 set_bit(CONF_MODE_DONE, &chan->conf_state); 3827 3828 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 3829 (unsigned long) &rfc, endptr - ptr); 3830 3831 break; 3832 3833 default: 3834 result = L2CAP_CONF_UNACCEPT; 3835 3836 memset(&rfc, 0, sizeof(rfc)); 3837 rfc.mode = chan->mode; 3838 } 3839 3840 if (result == L2CAP_CONF_SUCCESS) 3841 set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 3842 } 3843 rsp->scid = cpu_to_le16(chan->dcid); 3844 rsp->result = cpu_to_le16(result); 3845 rsp->flags = cpu_to_le16(0); 3846 3847 return ptr - data; 3848 } 3849 3850 static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, 3851 void *data, size_t size, u16 *result) 3852 { 3853 struct l2cap_conf_req *req = data; 3854 void *ptr = req->data; 3855 void *endptr = data + size; 3856 void *rsp_end = rsp + len; 3857 int type, olen; 3858 unsigned long val; 3859 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 3860 struct l2cap_conf_efs efs; 3861 3862 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 3863 3864 while (len >= L2CAP_CONF_OPT_SIZE) { 3865 int ret = l2cap_get_conf_opt(&rsp, rsp_end, &type, &olen, &val); 3866 3867 if (ret < 0) 3868 break; 3869 len -= ret; 3870 3871 switch (type) { 3872 case L2CAP_CONF_MTU: 3873 if (olen != 2) 3874 break; 3875 if (val < L2CAP_DEFAULT_MIN_MTU) { 3876 *result = L2CAP_CONF_UNACCEPT; 3877 chan->imtu = L2CAP_DEFAULT_MIN_MTU; 3878 } else 3879 chan->imtu = val; 3880 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu, 3881 endptr - ptr); 3882 break; 3883 3884 case L2CAP_CONF_FLUSH_TO: 3885 if (olen != 2) 3886 break; 3887 chan->flush_to = val; 3888 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2, 3889 chan->flush_to, endptr - ptr); 3890 break; 3891 3892 case L2CAP_CONF_RFC: 3893 if (olen != sizeof(rfc)) 3894 break; 3895 memcpy(&rfc, (void *)val, olen); 3896 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && 3897 rfc.mode != chan->mode) 3898 return -ECONNREFUSED; 3899 chan->fcs = 0; 3900 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 3901 (unsigned long) &rfc, endptr - ptr); 3902 break; 3903 3904 case L2CAP_CONF_EWS: 3905 if (olen != 2) 3906 break; 3907 chan->ack_win = min_t(u16, val, chan->ack_win); 3908 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 3909 chan->tx_win, endptr - ptr); 3910 break; 3911 3912 case L2CAP_CONF_EFS: 3913 if (olen != sizeof(efs)) 3914 break; 3915 memcpy(&efs, (void *)val, olen); 3916 if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 3917 efs.stype != L2CAP_SERV_NOTRAFIC && 3918 efs.stype != chan->local_stype) 3919 return -ECONNREFUSED; 3920 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), 3921 (unsigned long) &efs, endptr - ptr); 3922 break; 3923 3924 case L2CAP_CONF_FCS: 3925 if (olen != 1) 3926 break; 3927 if (*result == L2CAP_CONF_PENDING) 3928 if (val == L2CAP_FCS_NONE) 3929 set_bit(CONF_RECV_NO_FCS, 3930 &chan->conf_state); 3931 break; 3932 } 3933 } 3934 3935 /* A connected channel cannot change its mode, as the new mode would 3936 * not be initialized. 3937 */ 3938 if (chan->mode != rfc.mode && 3939 (chan->mode == L2CAP_MODE_BASIC || chan->state == BT_CONNECTED)) 3940 return -ECONNREFUSED; 3941 3942 chan->mode = rfc.mode; 3943 3944 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { 3945 switch (rfc.mode) { 3946 case L2CAP_MODE_ERTM: 3947 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 3948 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 3949 chan->mps = le16_to_cpu(rfc.max_pdu_size); 3950 if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) 3951 chan->ack_win = min_t(u16, chan->ack_win, 3952 rfc.txwin_size); 3953 3954 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 3955 chan->local_msdu = le16_to_cpu(efs.msdu); 3956 chan->local_sdu_itime = 3957 le32_to_cpu(efs.sdu_itime); 3958 chan->local_acc_lat = le32_to_cpu(efs.acc_lat); 3959 chan->local_flush_to = 3960 le32_to_cpu(efs.flush_to); 3961 } 3962 break; 3963 3964 case L2CAP_MODE_STREAMING: 3965 chan->mps = le16_to_cpu(rfc.max_pdu_size); 3966 } 3967 } 3968 3969 req->dcid = cpu_to_le16(chan->dcid); 3970 req->flags = cpu_to_le16(0); 3971 3972 return ptr - data; 3973 } 3974 3975 static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, 3976 u16 result, u16 flags) 3977 { 3978 struct l2cap_conf_rsp *rsp = data; 3979 void *ptr = rsp->data; 3980 3981 BT_DBG("chan %p", chan); 3982 3983 rsp->scid = cpu_to_le16(chan->dcid); 3984 rsp->result = cpu_to_le16(result); 3985 rsp->flags = cpu_to_le16(flags); 3986 3987 return ptr - data; 3988 } 3989 3990 void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan) 3991 { 3992 struct l2cap_le_conn_rsp rsp; 3993 struct l2cap_conn *conn = chan->conn; 3994 3995 BT_DBG("chan %p", chan); 3996 3997 rsp.dcid = cpu_to_le16(chan->scid); 3998 rsp.mtu = cpu_to_le16(chan->imtu); 3999 rsp.mps = cpu_to_le16(chan->mps); 4000 rsp.credits = cpu_to_le16(chan->rx_credits); 4001 rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS); 4002 4003 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), 4004 &rsp); 4005 chan->ident = 0; 4006 } 4007 4008 static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) 4009 { 4010 int *result = data; 4011 4012 if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) 4013 return; 4014 4015 switch (chan->state) { 4016 case BT_CONNECT2: 4017 /* If channel still pending accept add to result */ 4018 (*result)++; 4019 return; 4020 case BT_CONNECTED: 4021 return; 4022 default: 4023 /* If not connected or pending accept it has been refused */ 4024 *result = -ECONNREFUSED; 4025 return; 4026 } 4027 } 4028 4029 struct l2cap_ecred_rsp_data { 4030 struct l2cap_chan *locked_chan; 4031 struct { 4032 struct l2cap_ecred_conn_rsp_hdr rsp; 4033 __le16 scid[L2CAP_ECRED_MAX_CID]; 4034 } __packed pdu; 4035 int count; 4036 }; 4037 4038 /* Lock @chan if it is not @locked_chan, and has same or lower nesting level. 4039 * 4040 * They must have the same chan->conn, and conn->lock must be held. 4041 * 4042 * Caller must ensure @chan has lock nesting level <= that of @locked_chan, as 4043 * nested locking of l2cap_chan of different levels is allowed also without 4044 * holding conn->lock. 4045 * 4046 * See l2cap.h for the global l2cap_chan locking rules. 4047 */ 4048 static bool l2cap_chan_try_sibling_lock(struct l2cap_chan *chan, 4049 struct l2cap_chan *locked_chan) 4050 __must_hold(&locked_chan->lock) 4051 __must_hold(&locked_chan->conn->lock) 4052 __cond_acquires(true, &chan->lock) 4053 { 4054 if (chan == locked_chan) 4055 return false; 4056 4057 if (WARN_ON_ONCE(locked_chan->conn != chan->conn)) 4058 return false; 4059 4060 if (WARN_ON_ONCE(atomic_read(&locked_chan->nesting) 4061 < atomic_read(&chan->nesting))) 4062 return false; 4063 4064 mutex_lock_nest_lock(&chan->lock, &locked_chan->conn->lock); 4065 return true; 4066 } 4067 4068 static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) 4069 { 4070 struct l2cap_ecred_rsp_data *rsp = data; 4071 struct l2cap_ecred_conn_rsp *rsp_flex = 4072 container_of(&rsp->pdu.rsp, struct l2cap_ecred_conn_rsp, hdr); 4073 bool locked; 4074 4075 if (chan->mode != L2CAP_MODE_EXT_FLOWCTL) 4076 return; 4077 4078 /* Check if channel for outgoing connection or if it wasn't deferred 4079 * since in those cases it must be skipped. 4080 */ 4081 if (test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || 4082 !test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) 4083 return; 4084 4085 lockdep_assert_held(&rsp->locked_chan->lock); 4086 lockdep_assert_held(&rsp->locked_chan->conn->lock); 4087 4088 l2cap_chan_hold(chan); 4089 4090 locked = l2cap_chan_try_sibling_lock(chan, rsp->locked_chan); 4091 4092 /* Cannot occur: PARENT channels do not appear in chan_l, and SMP 4093 * channels never have FLAG_DEFER_SETUP. 4094 */ 4095 if (context_unsafe(!locked && chan != rsp->locked_chan)) 4096 goto done; 4097 4098 lockdep_assert_held(&chan->lock); 4099 lockdep_assert_held(&chan->conn->lock); 4100 4101 /* Reset ident so only one response is sent */ 4102 chan->ident = 0; 4103 4104 /* Unreachable, check in l2cap_ecred_conn_req. If reached, drop rest */ 4105 if (WARN_ON_ONCE(rsp->count >= ARRAY_SIZE(rsp->pdu.scid))) 4106 rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); 4107 4108 /* Include all channels pending with the same ident */ 4109 if (!rsp->pdu.rsp.result) 4110 rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); 4111 else 4112 l2cap_chan_del(chan, ECONNRESET); 4113 4114 done: 4115 if (locked) 4116 l2cap_chan_unlock(chan); 4117 4118 l2cap_chan_put(chan); 4119 } 4120 4121 void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) 4122 { 4123 struct l2cap_conn *conn = chan->conn; 4124 struct l2cap_ecred_rsp_data data; 4125 u16 id = chan->ident; 4126 int result = 0; 4127 4128 if (!id) 4129 return; 4130 if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) 4131 return; 4132 4133 BT_DBG("chan %p id %d", chan, id); 4134 4135 memset(&data, 0, sizeof(data)); 4136 4137 data.locked_chan = chan; 4138 4139 data.pdu.rsp.mtu = cpu_to_le16(chan->imtu); 4140 data.pdu.rsp.mps = cpu_to_le16(chan->mps); 4141 data.pdu.rsp.credits = cpu_to_le16(chan->rx_credits); 4142 data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS); 4143 4144 /* Verify that all channels are ready */ 4145 __l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result); 4146 4147 if (result > 0) 4148 return; 4149 4150 if (result < 0) 4151 data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); 4152 4153 /* Build response */ 4154 __l2cap_chan_list_id(conn, id, l2cap_ecred_rsp_defer, &data); 4155 4156 l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, 4157 sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), 4158 &data.pdu); 4159 } 4160 4161 void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 4162 { 4163 struct l2cap_conn_rsp rsp; 4164 struct l2cap_conn *conn = chan->conn; 4165 u8 buf[128]; 4166 u8 rsp_code; 4167 4168 rsp.scid = cpu_to_le16(chan->dcid); 4169 rsp.dcid = cpu_to_le16(chan->scid); 4170 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 4171 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4172 rsp_code = L2CAP_CONN_RSP; 4173 4174 BT_DBG("chan %p rsp_code %u", chan, rsp_code); 4175 4176 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); 4177 4178 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 4179 return; 4180 4181 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 4182 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf); 4183 chan->num_conf_req++; 4184 } 4185 4186 static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 4187 { 4188 int type, olen; 4189 unsigned long val; 4190 void *rsp_end = rsp + len; 4191 /* Use sane default values in case a misbehaving remote device 4192 * did not send an RFC or extended window size option. 4193 */ 4194 u16 txwin_ext = chan->ack_win; 4195 struct l2cap_conf_rfc rfc = { 4196 .mode = chan->mode, 4197 .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), 4198 .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), 4199 .max_pdu_size = cpu_to_le16(chan->imtu), 4200 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), 4201 }; 4202 4203 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 4204 4205 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 4206 return; 4207 4208 while (len >= L2CAP_CONF_OPT_SIZE) { 4209 int ret = l2cap_get_conf_opt(&rsp, rsp_end, &type, &olen, &val); 4210 4211 if (ret < 0) 4212 break; 4213 len -= ret; 4214 4215 switch (type) { 4216 case L2CAP_CONF_RFC: 4217 if (olen != sizeof(rfc)) 4218 break; 4219 memcpy(&rfc, (void *)val, olen); 4220 break; 4221 case L2CAP_CONF_EWS: 4222 if (olen != 2) 4223 break; 4224 txwin_ext = val; 4225 break; 4226 } 4227 } 4228 4229 switch (rfc.mode) { 4230 case L2CAP_MODE_ERTM: 4231 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 4232 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 4233 chan->mps = le16_to_cpu(rfc.max_pdu_size); 4234 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 4235 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); 4236 else 4237 chan->ack_win = min_t(u16, chan->ack_win, 4238 rfc.txwin_size); 4239 break; 4240 case L2CAP_MODE_STREAMING: 4241 chan->mps = le16_to_cpu(rfc.max_pdu_size); 4242 } 4243 } 4244 4245 static inline int l2cap_command_rej(struct l2cap_conn *conn, 4246 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 4247 u8 *data) 4248 __must_hold(&conn->lock) 4249 { 4250 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; 4251 4252 if (cmd_len < sizeof(*rej)) 4253 return -EPROTO; 4254 4255 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) 4256 return 0; 4257 4258 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 4259 cmd->ident == conn->info_ident) { 4260 cancel_delayed_work(&conn->info_timer); 4261 4262 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 4263 conn->info_ident = 0; 4264 4265 l2cap_conn_start(conn); 4266 } 4267 4268 return 0; 4269 } 4270 4271 /* Allocate and initialise a channel for an incoming connection. 4272 * 4273 * The channel inherits its configuration from @pchan and is linked into @conn 4274 * before ->new_connection() runs, so the conn list reference keeps it alive if 4275 * the callback exposes it (e.g. via the socket accept queue) before this 4276 * returns. The l2cap_chan_create() reference is taken over by the subsystem on 4277 * success and dropped here on failure. 4278 */ 4279 static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn, 4280 struct l2cap_chan *pchan) 4281 __must_hold(&conn->lock) 4282 __must_hold(&pchan->lock) 4283 { 4284 struct l2cap_chan *chan; 4285 4286 chan = l2cap_chan_create(); 4287 if (!chan) 4288 return NULL; 4289 4290 l2cap_chan_lock(chan); 4291 4292 l2cap_chan_set_defaults(chan, pchan); 4293 chan->ops = pchan->ops; 4294 4295 __l2cap_chan_add(conn, chan); 4296 4297 lockdep_assert_held(&chan->conn->lock); 4298 4299 if (pchan->ops->new_connection && 4300 pchan->ops->new_connection(pchan, chan) < 0) { 4301 l2cap_chan_del(chan, 0); 4302 l2cap_chan_unlock(chan); 4303 l2cap_chan_put(chan); 4304 return NULL; 4305 } 4306 4307 l2cap_chan_unlock(chan); 4308 4309 return chan; 4310 } 4311 4312 static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, 4313 u8 *data, u8 rsp_code) 4314 __must_hold(&conn->lock) 4315 { 4316 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 4317 struct l2cap_conn_rsp rsp; 4318 struct l2cap_chan *chan = NULL, *pchan = NULL; 4319 int result, status = L2CAP_CS_NO_INFO; 4320 4321 u16 dcid = 0, scid = __le16_to_cpu(req->scid); 4322 __le16 psm = req->psm; 4323 4324 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid); 4325 4326 /* Check if we have socket listening on psm */ 4327 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, 4328 &conn->hcon->dst, ACL_LINK); 4329 if (!pchan) { 4330 result = L2CAP_CR_BAD_PSM; 4331 4332 rsp.scid = cpu_to_le16(scid); 4333 rsp.dcid = cpu_to_le16(dcid); 4334 rsp.result = cpu_to_le16(result); 4335 rsp.status = cpu_to_le16(status); 4336 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); 4337 return; 4338 } 4339 4340 l2cap_chan_lock(pchan); 4341 4342 /* Check if the ACL is secure enough (if not SDP) */ 4343 if (psm != cpu_to_le16(L2CAP_PSM_SDP) && 4344 (!hci_conn_check_link_mode(conn->hcon) || 4345 !l2cap_check_enc_key_size(conn->hcon, pchan))) { 4346 conn->disc_reason = HCI_ERROR_AUTH_FAILURE; 4347 result = L2CAP_CR_SEC_BLOCK; 4348 goto response; 4349 } 4350 4351 result = L2CAP_CR_NO_MEM; 4352 4353 /* Check for valid dynamic CID range (as per Erratum 3253) */ 4354 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_DYN_END) { 4355 result = L2CAP_CR_INVALID_SCID; 4356 goto response; 4357 } 4358 4359 /* Check if we already have channel with that dcid */ 4360 if (__l2cap_get_chan_by_dcid(conn, scid)) { 4361 result = L2CAP_CR_SCID_IN_USE; 4362 goto response; 4363 } 4364 4365 chan = l2cap_new_connection(conn, pchan); 4366 if (!chan) 4367 goto response; 4368 4369 /* For certain devices (ex: HID mouse), support for authentication, 4370 * pairing and bonding is optional. For such devices, inorder to avoid 4371 * the ACL alive for too long after L2CAP disconnection, reset the ACL 4372 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect. 4373 */ 4374 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; 4375 4376 bacpy(&chan->src, &conn->hcon->src); 4377 bacpy(&chan->dst, &conn->hcon->dst); 4378 chan->src_type = bdaddr_src_type(conn->hcon); 4379 chan->dst_type = bdaddr_dst_type(conn->hcon); 4380 chan->psm = psm; 4381 chan->dcid = scid; 4382 4383 dcid = chan->scid; 4384 4385 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); 4386 4387 chan->ident = cmd->ident; 4388 4389 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 4390 if (l2cap_chan_check_security(chan, false)) { 4391 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { 4392 l2cap_state_change(chan, BT_CONNECT2); 4393 result = L2CAP_CR_PEND; 4394 status = L2CAP_CS_AUTHOR_PEND; 4395 chan->ops->defer(chan); 4396 } else { 4397 l2cap_state_change(chan, BT_CONFIG); 4398 result = L2CAP_CR_SUCCESS; 4399 status = L2CAP_CS_NO_INFO; 4400 } 4401 } else { 4402 l2cap_state_change(chan, BT_CONNECT2); 4403 result = L2CAP_CR_PEND; 4404 status = L2CAP_CS_AUTHEN_PEND; 4405 } 4406 } else { 4407 l2cap_state_change(chan, BT_CONNECT2); 4408 result = L2CAP_CR_PEND; 4409 status = L2CAP_CS_NO_INFO; 4410 } 4411 4412 response: 4413 rsp.scid = cpu_to_le16(scid); 4414 rsp.dcid = cpu_to_le16(dcid); 4415 rsp.result = cpu_to_le16(result); 4416 rsp.status = cpu_to_le16(status); 4417 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); 4418 4419 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 4420 struct l2cap_info_req info; 4421 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 4422 4423 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 4424 conn->info_ident = l2cap_get_ident(conn); 4425 4426 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); 4427 4428 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, 4429 sizeof(info), &info); 4430 } 4431 4432 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && 4433 result == L2CAP_CR_SUCCESS) { 4434 u8 buf[128]; 4435 set_bit(CONF_REQ_SENT, &chan->conf_state); 4436 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 4437 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf); 4438 chan->num_conf_req++; 4439 } 4440 4441 l2cap_chan_unlock(pchan); 4442 l2cap_chan_put(pchan); 4443 } 4444 4445 static int l2cap_connect_req(struct l2cap_conn *conn, 4446 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 4447 __must_hold(&conn->lock) 4448 { 4449 if (cmd_len < sizeof(struct l2cap_conn_req)) 4450 return -EPROTO; 4451 4452 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP); 4453 return 0; 4454 } 4455 4456 static int l2cap_connect_create_rsp(struct l2cap_conn *conn, 4457 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 4458 u8 *data) 4459 __must_hold(&conn->lock) 4460 { 4461 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 4462 u16 scid, dcid, result, status; 4463 struct l2cap_chan *chan; 4464 u8 req[128]; 4465 int err; 4466 4467 if (cmd_len < sizeof(*rsp)) 4468 return -EPROTO; 4469 4470 scid = __le16_to_cpu(rsp->scid); 4471 dcid = __le16_to_cpu(rsp->dcid); 4472 result = __le16_to_cpu(rsp->result); 4473 status = __le16_to_cpu(rsp->status); 4474 4475 if (result == L2CAP_CR_SUCCESS && (dcid < L2CAP_CID_DYN_START || 4476 dcid > L2CAP_CID_DYN_END)) 4477 return -EPROTO; 4478 4479 BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", 4480 dcid, scid, result, status); 4481 4482 if (scid) { 4483 chan = __l2cap_get_chan_by_scid(conn, scid); 4484 if (!chan) 4485 return -EBADSLT; 4486 } else { 4487 chan = __l2cap_get_connect_chan_by_ident(conn, cmd->ident); 4488 if (!chan) 4489 return -EBADSLT; 4490 } 4491 4492 chan = l2cap_chan_hold_unless_zero(chan); 4493 if (!chan) 4494 return -EBADSLT; 4495 4496 err = 0; 4497 4498 l2cap_chan_lock(chan); 4499 4500 lockdep_assert_held(&chan->conn->lock); 4501 4502 /* Only accept a response matching a connection request we sent and 4503 * that is still outstanding. A channel waiting in BT_CONNECT for 4504 * security or the feature mask has not sent one yet. 4505 */ 4506 if (!l2cap_chan_connect_pending(chan) || chan->ident != cmd->ident) 4507 goto done; 4508 4509 switch (result) { 4510 case L2CAP_CR_SUCCESS: 4511 if (__l2cap_get_chan_by_dcid(conn, dcid)) { 4512 err = -EBADSLT; 4513 break; 4514 } 4515 4516 l2cap_state_change(chan, BT_CONFIG); 4517 chan->ident = 0; 4518 chan->dcid = dcid; 4519 clear_bit(CONF_CONNECT_PEND, &chan->conf_state); 4520 4521 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 4522 break; 4523 4524 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 4525 l2cap_build_conf_req(chan, req, sizeof(req)), req); 4526 chan->num_conf_req++; 4527 break; 4528 4529 case L2CAP_CR_PEND: 4530 /* Our request is still outstanding */ 4531 break; 4532 4533 default: 4534 l2cap_chan_del(chan, ECONNREFUSED); 4535 break; 4536 } 4537 4538 done: 4539 l2cap_chan_unlock(chan); 4540 l2cap_chan_put(chan); 4541 4542 return err; 4543 } 4544 4545 static inline void set_default_fcs(struct l2cap_chan *chan) 4546 { 4547 /* FCS is enabled only in ERTM or streaming mode, if one or both 4548 * sides request it. 4549 */ 4550 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 4551 chan->fcs = L2CAP_FCS_NONE; 4552 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) 4553 chan->fcs = L2CAP_FCS_CRC16; 4554 } 4555 4556 static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, 4557 u8 ident, u16 flags) 4558 { 4559 struct l2cap_conn *conn = chan->conn; 4560 4561 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, 4562 flags); 4563 4564 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 4565 set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 4566 4567 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, 4568 l2cap_build_conf_rsp(chan, data, 4569 L2CAP_CONF_SUCCESS, flags), data); 4570 } 4571 4572 static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident, 4573 u16 scid, u16 dcid) 4574 { 4575 struct l2cap_cmd_rej_cid rej; 4576 4577 rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); 4578 rej.scid = __cpu_to_le16(scid); 4579 rej.dcid = __cpu_to_le16(dcid); 4580 4581 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 4582 } 4583 4584 static inline int l2cap_config_req(struct l2cap_conn *conn, 4585 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 4586 u8 *data) 4587 __must_hold(&conn->lock) 4588 { 4589 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 4590 u16 dcid, flags; 4591 u8 rsp[64]; 4592 struct l2cap_chan *chan; 4593 int len, err = 0; 4594 4595 if (cmd_len < sizeof(*req)) 4596 return -EPROTO; 4597 4598 dcid = __le16_to_cpu(req->dcid); 4599 flags = __le16_to_cpu(req->flags); 4600 4601 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 4602 4603 chan = l2cap_get_chan_by_scid(conn, dcid); 4604 if (!chan) { 4605 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0); 4606 return 0; 4607 } 4608 4609 l2cap_chan_lock(chan); 4610 4611 lockdep_assert_held(&chan->conn->lock); 4612 4613 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2 && 4614 chan->state != BT_CONNECTED) { 4615 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, 4616 chan->dcid); 4617 goto unlock; 4618 } 4619 4620 /* Reject if config buffer is too small. */ 4621 len = cmd_len - sizeof(*req); 4622 if (chan->conf_len + len > sizeof(chan->conf_req)) { 4623 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 4624 l2cap_build_conf_rsp(chan, rsp, 4625 L2CAP_CONF_REJECT, flags), rsp); 4626 goto unlock; 4627 } 4628 4629 /* Store config. */ 4630 memcpy(chan->conf_req + chan->conf_len, req->data, len); 4631 chan->conf_len += len; 4632 4633 if (flags & L2CAP_CONF_FLAG_CONTINUATION) { 4634 /* Incomplete config. Send empty response. */ 4635 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 4636 l2cap_build_conf_rsp(chan, rsp, 4637 L2CAP_CONF_SUCCESS, flags), rsp); 4638 goto unlock; 4639 } 4640 4641 /* Complete config. */ 4642 len = l2cap_parse_conf_req(chan, rsp, sizeof(rsp)); 4643 if (len < 0) { 4644 l2cap_send_disconn_req(chan, ECONNRESET); 4645 goto unlock; 4646 } 4647 4648 chan->ident = cmd->ident; 4649 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 4650 if (chan->num_conf_rsp < L2CAP_CONF_MAX_CONF_RSP) 4651 chan->num_conf_rsp++; 4652 4653 /* Reset config buffer. */ 4654 chan->conf_len = 0; 4655 4656 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) 4657 goto unlock; 4658 4659 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { 4660 set_default_fcs(chan); 4661 4662 if (chan->state != BT_CONNECTED) { 4663 if (chan->mode == L2CAP_MODE_ERTM || 4664 chan->mode == L2CAP_MODE_STREAMING) 4665 err = l2cap_ertm_init(chan); 4666 4667 if (err < 0) 4668 l2cap_send_disconn_req(chan, -err); 4669 else 4670 l2cap_chan_ready(chan); 4671 } 4672 4673 goto unlock; 4674 } 4675 4676 /* A channel in BT_CONNECT2 has not passed security or userspace 4677 * accept yet. Answer the peer's request, but hold our own until 4678 * the channel reaches BT_CONFIG: the CONNECT2 -> CONFIG sites send 4679 * it. Connected channels may also need to reconfigure. 4680 */ 4681 if ((chan->state == BT_CONFIG || chan->state == BT_CONNECTED) && 4682 !test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { 4683 u8 buf[64]; 4684 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 4685 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf); 4686 chan->num_conf_req++; 4687 } 4688 4689 /* Got Conf Rsp PENDING from remote side and assume we sent 4690 Conf Rsp PENDING in the code above */ 4691 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && 4692 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 4693 4694 /* check compatibility */ 4695 4696 /* Send rsp for BR/EDR channel */ 4697 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); 4698 } 4699 4700 unlock: 4701 l2cap_chan_unlock(chan); 4702 l2cap_chan_put(chan); 4703 return err; 4704 } 4705 4706 static inline int l2cap_config_rsp(struct l2cap_conn *conn, 4707 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 4708 u8 *data) 4709 __must_hold(&conn->lock) 4710 { 4711 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 4712 u16 scid, flags, result; 4713 struct l2cap_chan *chan; 4714 int len = cmd_len - sizeof(*rsp); 4715 int err = 0; 4716 4717 if (cmd_len < sizeof(*rsp)) 4718 return -EPROTO; 4719 4720 scid = __le16_to_cpu(rsp->scid); 4721 flags = __le16_to_cpu(rsp->flags); 4722 result = __le16_to_cpu(rsp->result); 4723 4724 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, 4725 result, len); 4726 4727 chan = l2cap_get_chan_by_scid(conn, scid); 4728 if (!chan) 4729 return 0; 4730 4731 l2cap_chan_lock(chan); 4732 4733 lockdep_assert_held(&chan->conn->lock); 4734 4735 /* Connected channels may also receive reconfiguration responses. */ 4736 if (chan->state != BT_CONFIG && chan->state != BT_CONNECTED) 4737 goto done; 4738 4739 switch (result) { 4740 case L2CAP_CONF_SUCCESS: 4741 l2cap_conf_rfc_get(chan, rsp->data, len); 4742 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); 4743 break; 4744 4745 case L2CAP_CONF_PENDING: 4746 set_bit(CONF_REM_CONF_PEND, &chan->conf_state); 4747 4748 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 4749 char buf[64]; 4750 4751 len = l2cap_parse_conf_rsp(chan, rsp->data, len, 4752 buf, sizeof(buf), &result); 4753 if (len < 0) { 4754 l2cap_send_disconn_req(chan, ECONNRESET); 4755 goto done; 4756 } 4757 4758 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, 0); 4759 } 4760 goto done; 4761 4762 case L2CAP_CONF_UNKNOWN: 4763 case L2CAP_CONF_UNACCEPT: 4764 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 4765 char req[64]; 4766 4767 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 4768 l2cap_send_disconn_req(chan, ECONNRESET); 4769 goto done; 4770 } 4771 4772 /* throw out any old stored conf requests */ 4773 result = L2CAP_CONF_SUCCESS; 4774 len = l2cap_parse_conf_rsp(chan, rsp->data, len, 4775 req, sizeof(req), &result); 4776 if (len < 0) { 4777 l2cap_send_disconn_req(chan, ECONNRESET); 4778 goto done; 4779 } 4780 4781 l2cap_send_cmd(conn, l2cap_get_ident(conn), 4782 L2CAP_CONF_REQ, len, req); 4783 chan->num_conf_req++; 4784 if (result != L2CAP_CONF_SUCCESS) 4785 goto done; 4786 break; 4787 } 4788 fallthrough; 4789 4790 default: 4791 l2cap_chan_set_err(chan, ECONNRESET); 4792 4793 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); 4794 l2cap_send_disconn_req(chan, ECONNRESET); 4795 goto done; 4796 } 4797 4798 if (flags & L2CAP_CONF_FLAG_CONTINUATION) 4799 goto done; 4800 4801 set_bit(CONF_INPUT_DONE, &chan->conf_state); 4802 4803 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { 4804 set_default_fcs(chan); 4805 4806 if (chan->state != BT_CONFIG) 4807 goto done; 4808 4809 if (chan->mode == L2CAP_MODE_ERTM || 4810 chan->mode == L2CAP_MODE_STREAMING) 4811 err = l2cap_ertm_init(chan); 4812 4813 if (err < 0) 4814 l2cap_send_disconn_req(chan, -err); 4815 else 4816 l2cap_chan_ready(chan); 4817 } 4818 4819 done: 4820 l2cap_chan_unlock(chan); 4821 l2cap_chan_put(chan); 4822 return err; 4823 } 4824 4825 static inline int l2cap_disconnect_req(struct l2cap_conn *conn, 4826 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 4827 u8 *data) 4828 __must_hold(&conn->lock) 4829 { 4830 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 4831 struct l2cap_disconn_rsp rsp; 4832 u16 dcid, scid; 4833 struct l2cap_chan *chan; 4834 4835 if (cmd_len != sizeof(*req)) 4836 return -EPROTO; 4837 4838 scid = __le16_to_cpu(req->scid); 4839 dcid = __le16_to_cpu(req->dcid); 4840 4841 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 4842 4843 chan = l2cap_get_chan_by_scid(conn, dcid); 4844 if (!chan) { 4845 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid); 4846 return 0; 4847 } 4848 4849 l2cap_chan_lock(chan); 4850 4851 lockdep_assert_held(&chan->conn->lock); 4852 4853 rsp.dcid = cpu_to_le16(chan->scid); 4854 rsp.scid = cpu_to_le16(chan->dcid); 4855 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 4856 4857 chan->ops->set_shutdown(chan); 4858 4859 l2cap_chan_del(chan, ECONNRESET); 4860 4861 chan->ops->close(chan); 4862 4863 l2cap_chan_unlock(chan); 4864 l2cap_chan_put(chan); 4865 4866 return 0; 4867 } 4868 4869 static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, 4870 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 4871 u8 *data) 4872 __must_hold(&conn->lock) 4873 { 4874 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 4875 u16 dcid, scid; 4876 struct l2cap_chan *chan; 4877 4878 if (cmd_len != sizeof(*rsp)) 4879 return -EPROTO; 4880 4881 scid = __le16_to_cpu(rsp->scid); 4882 dcid = __le16_to_cpu(rsp->dcid); 4883 4884 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 4885 4886 chan = l2cap_get_chan_by_scid(conn, scid); 4887 if (!chan) { 4888 return 0; 4889 } 4890 4891 l2cap_chan_lock(chan); 4892 4893 lockdep_assert_held(&chan->conn->lock); 4894 4895 if (chan->state != BT_DISCONN) { 4896 l2cap_chan_unlock(chan); 4897 l2cap_chan_put(chan); 4898 return 0; 4899 } 4900 4901 l2cap_chan_del(chan, 0); 4902 4903 chan->ops->close(chan); 4904 4905 l2cap_chan_unlock(chan); 4906 l2cap_chan_put(chan); 4907 4908 return 0; 4909 } 4910 4911 static inline int l2cap_information_req(struct l2cap_conn *conn, 4912 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 4913 u8 *data) 4914 { 4915 struct l2cap_info_req *req = (struct l2cap_info_req *) data; 4916 u16 type; 4917 4918 if (cmd_len != sizeof(*req)) 4919 return -EPROTO; 4920 4921 type = __le16_to_cpu(req->type); 4922 4923 BT_DBG("type 0x%4.4x", type); 4924 4925 if (type == L2CAP_IT_FEAT_MASK) { 4926 u8 buf[8]; 4927 u32 feat_mask = l2cap_feat_mask; 4928 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 4929 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 4930 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 4931 if (!disable_ertm) 4932 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 4933 | L2CAP_FEAT_FCS; 4934 4935 put_unaligned_le32(feat_mask, rsp->data); 4936 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), 4937 buf); 4938 } else if (type == L2CAP_IT_FIXED_CHAN) { 4939 u8 buf[12]; 4940 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 4941 4942 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 4943 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 4944 rsp->data[0] = conn->local_fixed_chan; 4945 memset(rsp->data + 1, 0, 7); 4946 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), 4947 buf); 4948 } else { 4949 struct l2cap_info_rsp rsp; 4950 rsp.type = cpu_to_le16(type); 4951 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 4952 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), 4953 &rsp); 4954 } 4955 4956 return 0; 4957 } 4958 4959 static inline int l2cap_information_rsp(struct l2cap_conn *conn, 4960 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 4961 u8 *data) 4962 __must_hold(&conn->lock) 4963 { 4964 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 4965 u16 type, result; 4966 4967 if (cmd_len < sizeof(*rsp)) 4968 return -EPROTO; 4969 4970 type = __le16_to_cpu(rsp->type); 4971 result = __le16_to_cpu(rsp->result); 4972 4973 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 4974 4975 /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 4976 if (cmd->ident != conn->info_ident || 4977 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 4978 return 0; 4979 4980 cancel_delayed_work(&conn->info_timer); 4981 4982 if (result != L2CAP_IR_SUCCESS) { 4983 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 4984 conn->info_ident = 0; 4985 4986 l2cap_conn_start(conn); 4987 4988 return 0; 4989 } 4990 4991 switch (type) { 4992 case L2CAP_IT_FEAT_MASK: 4993 if (cmd_len >= sizeof(*rsp) + sizeof(u32)) 4994 conn->feat_mask = get_unaligned_le32(rsp->data); 4995 4996 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 4997 struct l2cap_info_req req; 4998 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 4999 5000 conn->info_ident = l2cap_get_ident(conn); 5001 5002 l2cap_send_cmd(conn, conn->info_ident, 5003 L2CAP_INFO_REQ, sizeof(req), &req); 5004 } else { 5005 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 5006 conn->info_ident = 0; 5007 5008 l2cap_conn_start(conn); 5009 } 5010 break; 5011 5012 case L2CAP_IT_FIXED_CHAN: 5013 if (cmd_len >= sizeof(*rsp) + sizeof(rsp->data[0])) 5014 conn->remote_fixed_chan = rsp->data[0]; 5015 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 5016 conn->info_ident = 0; 5017 5018 l2cap_conn_start(conn); 5019 break; 5020 } 5021 5022 return 0; 5023 } 5024 5025 static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 5026 struct l2cap_cmd_hdr *cmd, 5027 u16 cmd_len, u8 *data) 5028 { 5029 struct hci_conn *hcon = conn->hcon; 5030 struct l2cap_conn_param_update_req *req; 5031 struct l2cap_conn_param_update_rsp rsp; 5032 u16 min, max, latency, to_multiplier; 5033 int err; 5034 5035 if (hcon->role != HCI_ROLE_MASTER) 5036 return -EINVAL; 5037 5038 if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 5039 return -EPROTO; 5040 5041 req = (struct l2cap_conn_param_update_req *) data; 5042 min = __le16_to_cpu(req->min); 5043 max = __le16_to_cpu(req->max); 5044 latency = __le16_to_cpu(req->latency); 5045 to_multiplier = __le16_to_cpu(req->to_multiplier); 5046 5047 BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 5048 min, max, latency, to_multiplier); 5049 5050 memset(&rsp, 0, sizeof(rsp)); 5051 5052 err = hci_check_conn_params(min, max, latency, to_multiplier); 5053 if (err) 5054 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 5055 else 5056 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 5057 5058 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 5059 sizeof(rsp), &rsp); 5060 5061 if (!err) 5062 hci_le_conn_update(hcon, min, max, latency, to_multiplier); 5063 5064 return 0; 5065 } 5066 5067 static int l2cap_le_connect_rsp(struct l2cap_conn *conn, 5068 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5069 u8 *data) 5070 __must_hold(&conn->lock) 5071 { 5072 struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data; 5073 struct hci_conn *hcon = conn->hcon; 5074 u16 dcid, mtu, mps, credits, result; 5075 struct l2cap_chan *chan; 5076 int err, sec_level; 5077 5078 if (cmd_len < sizeof(*rsp)) 5079 return -EPROTO; 5080 5081 dcid = __le16_to_cpu(rsp->dcid); 5082 mtu = __le16_to_cpu(rsp->mtu); 5083 mps = __le16_to_cpu(rsp->mps); 5084 credits = __le16_to_cpu(rsp->credits); 5085 result = __le16_to_cpu(rsp->result); 5086 5087 if (result == L2CAP_CR_LE_SUCCESS && (mtu < 23 || mps < 23 || 5088 dcid < L2CAP_CID_DYN_START || 5089 dcid > L2CAP_CID_LE_DYN_END)) 5090 return -EPROTO; 5091 5092 BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x", 5093 dcid, mtu, mps, credits, result); 5094 5095 chan = __l2cap_get_connect_chan_by_ident(conn, cmd->ident); 5096 if (!chan) 5097 return -EBADSLT; 5098 5099 chan = l2cap_chan_hold_unless_zero(chan); 5100 if (!chan) 5101 return -EBADSLT; 5102 5103 err = 0; 5104 5105 l2cap_chan_lock(chan); 5106 5107 lockdep_assert_held(&chan->conn->lock); 5108 5109 switch (result) { 5110 case L2CAP_CR_LE_SUCCESS: 5111 if (__l2cap_get_chan_by_dcid(conn, dcid)) { 5112 err = -EBADSLT; 5113 break; 5114 } 5115 5116 chan->ident = 0; 5117 chan->dcid = dcid; 5118 chan->omtu = mtu; 5119 chan->remote_mps = mps; 5120 chan->tx_credits = credits; 5121 l2cap_chan_ready(chan); 5122 break; 5123 5124 case L2CAP_CR_LE_AUTHENTICATION: 5125 case L2CAP_CR_LE_ENCRYPTION: 5126 /* If we already have MITM protection we can't do 5127 * anything. 5128 */ 5129 if (hcon->sec_level > BT_SECURITY_MEDIUM) { 5130 l2cap_chan_del(chan, ECONNREFUSED); 5131 break; 5132 } 5133 5134 sec_level = hcon->sec_level + 1; 5135 if (chan->sec_level < sec_level) 5136 chan->sec_level = sec_level; 5137 5138 /* We'll need to send a new Connect Request */ 5139 clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags); 5140 5141 smp_conn_security(hcon, chan->sec_level); 5142 break; 5143 5144 default: 5145 l2cap_chan_del(chan, ECONNREFUSED); 5146 break; 5147 } 5148 5149 l2cap_chan_unlock(chan); 5150 l2cap_chan_put(chan); 5151 5152 return err; 5153 } 5154 5155 static void l2cap_put_ident(struct l2cap_conn *conn, u8 code, u8 id) 5156 { 5157 switch (code) { 5158 case L2CAP_COMMAND_REJ: 5159 case L2CAP_CONN_RSP: 5160 case L2CAP_CONF_RSP: 5161 case L2CAP_DISCONN_RSP: 5162 case L2CAP_ECHO_RSP: 5163 case L2CAP_INFO_RSP: 5164 case L2CAP_CONN_PARAM_UPDATE_RSP: 5165 case L2CAP_LE_CONN_RSP: 5166 case L2CAP_ECRED_CONN_RSP: 5167 case L2CAP_ECRED_RECONF_RSP: 5168 /* First do a lookup since the remote may send bogus ids that 5169 * would make ida_free to generate warnings. 5170 */ 5171 if (ida_find_first_range(&conn->tx_ida, id, id) >= 0) 5172 ida_free(&conn->tx_ida, id); 5173 } 5174 } 5175 5176 static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 5177 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5178 u8 *data) 5179 __must_hold(&conn->lock) 5180 { 5181 int err = 0; 5182 5183 l2cap_put_ident(conn, cmd->code, cmd->ident); 5184 5185 switch (cmd->code) { 5186 case L2CAP_COMMAND_REJ: 5187 l2cap_command_rej(conn, cmd, cmd_len, data); 5188 break; 5189 5190 case L2CAP_CONN_REQ: 5191 err = l2cap_connect_req(conn, cmd, cmd_len, data); 5192 break; 5193 5194 case L2CAP_CONN_RSP: 5195 l2cap_connect_create_rsp(conn, cmd, cmd_len, data); 5196 break; 5197 5198 case L2CAP_CONF_REQ: 5199 err = l2cap_config_req(conn, cmd, cmd_len, data); 5200 break; 5201 5202 case L2CAP_CONF_RSP: 5203 l2cap_config_rsp(conn, cmd, cmd_len, data); 5204 break; 5205 5206 case L2CAP_DISCONN_REQ: 5207 err = l2cap_disconnect_req(conn, cmd, cmd_len, data); 5208 break; 5209 5210 case L2CAP_DISCONN_RSP: 5211 l2cap_disconnect_rsp(conn, cmd, cmd_len, data); 5212 break; 5213 5214 case L2CAP_ECHO_REQ: 5215 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 5216 break; 5217 5218 case L2CAP_ECHO_RSP: 5219 break; 5220 5221 case L2CAP_INFO_REQ: 5222 err = l2cap_information_req(conn, cmd, cmd_len, data); 5223 break; 5224 5225 case L2CAP_INFO_RSP: 5226 l2cap_information_rsp(conn, cmd, cmd_len, data); 5227 break; 5228 5229 default: 5230 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 5231 err = -EINVAL; 5232 break; 5233 } 5234 5235 return err; 5236 } 5237 5238 static int l2cap_le_connect_req(struct l2cap_conn *conn, 5239 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5240 u8 *data) 5241 __must_hold(&conn->lock) 5242 { 5243 struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data; 5244 struct l2cap_le_conn_rsp rsp; 5245 struct l2cap_chan *chan, *pchan; 5246 u16 dcid, scid, credits, mtu, mps; 5247 __le16 psm; 5248 u8 result; 5249 5250 if (cmd_len != sizeof(*req)) 5251 return -EPROTO; 5252 5253 scid = __le16_to_cpu(req->scid); 5254 mtu = __le16_to_cpu(req->mtu); 5255 mps = __le16_to_cpu(req->mps); 5256 psm = req->psm; 5257 dcid = 0; 5258 credits = 0; 5259 5260 if (mtu < 23 || mps < 23) 5261 return -EPROTO; 5262 5263 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm), 5264 scid, mtu, mps); 5265 5266 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 3, Part A 5267 * page 1059: 5268 * 5269 * Valid range: 0x0001-0x00ff 5270 * 5271 * Table 4.15: L2CAP_LE_CREDIT_BASED_CONNECTION_REQ SPSM ranges 5272 */ 5273 if (!psm || __le16_to_cpu(psm) > L2CAP_PSM_LE_DYN_END) { 5274 result = L2CAP_CR_LE_BAD_PSM; 5275 chan = NULL; 5276 goto response; 5277 } 5278 5279 /* Check if we have socket listening on psm */ 5280 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, 5281 &conn->hcon->dst, LE_LINK); 5282 if (!pchan) { 5283 result = L2CAP_CR_LE_BAD_PSM; 5284 chan = NULL; 5285 goto response; 5286 } 5287 5288 l2cap_chan_lock(pchan); 5289 5290 if (!smp_sufficient_security(conn->hcon, pchan->sec_level, 5291 SMP_ALLOW_STK)) { 5292 result = pchan->sec_level == BT_SECURITY_MEDIUM ? 5293 L2CAP_CR_LE_ENCRYPTION : L2CAP_CR_LE_AUTHENTICATION; 5294 chan = NULL; 5295 goto response_unlock; 5296 } 5297 5298 /* Check if Key Size is sufficient for the security level */ 5299 if (!l2cap_check_enc_key_size(conn->hcon, pchan)) { 5300 result = L2CAP_CR_LE_BAD_KEY_SIZE; 5301 chan = NULL; 5302 goto response_unlock; 5303 } 5304 5305 /* Check for valid dynamic CID range */ 5306 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) { 5307 result = L2CAP_CR_LE_INVALID_SCID; 5308 chan = NULL; 5309 goto response_unlock; 5310 } 5311 5312 /* Check if we already have channel with that dcid */ 5313 if (__l2cap_get_chan_by_dcid(conn, scid)) { 5314 result = L2CAP_CR_LE_SCID_IN_USE; 5315 chan = NULL; 5316 goto response_unlock; 5317 } 5318 5319 chan = l2cap_new_connection(conn, pchan); 5320 if (!chan) { 5321 result = L2CAP_CR_LE_NO_MEM; 5322 goto response_unlock; 5323 } 5324 5325 l2cap_chan_lock(chan); 5326 5327 lockdep_assert_held(&chan->conn->lock); 5328 5329 bacpy(&chan->src, &conn->hcon->src); 5330 bacpy(&chan->dst, &conn->hcon->dst); 5331 chan->src_type = bdaddr_src_type(conn->hcon); 5332 chan->dst_type = bdaddr_dst_type(conn->hcon); 5333 chan->psm = psm; 5334 chan->dcid = scid; 5335 chan->omtu = mtu; 5336 chan->remote_mps = mps; 5337 5338 l2cap_le_flowctl_init(chan, __le16_to_cpu(req->credits)); 5339 5340 dcid = chan->scid; 5341 credits = chan->rx_credits; 5342 5343 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); 5344 5345 chan->mode = L2CAP_MODE_LE_FLOWCTL; 5346 5347 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { 5348 chan->ident = cmd->ident; 5349 l2cap_state_change(chan, BT_CONNECT2); 5350 /* The following result value is actually not defined 5351 * for LE CoC but we use it to let the function know 5352 * that it should bail out after doing its cleanup 5353 * instead of sending a response. 5354 */ 5355 result = L2CAP_CR_PEND; 5356 chan->ops->defer(chan); 5357 } else { 5358 l2cap_chan_ready(chan); 5359 result = L2CAP_CR_LE_SUCCESS; 5360 } 5361 5362 l2cap_chan_unlock(chan); 5363 5364 response_unlock: 5365 l2cap_chan_unlock(pchan); 5366 l2cap_chan_put(pchan); 5367 5368 if (result == L2CAP_CR_PEND) 5369 return 0; 5370 5371 response: 5372 if (chan) { 5373 rsp.mtu = cpu_to_le16(chan->imtu); 5374 rsp.mps = cpu_to_le16(chan->mps); 5375 } else { 5376 rsp.mtu = 0; 5377 rsp.mps = 0; 5378 } 5379 5380 rsp.dcid = cpu_to_le16(dcid); 5381 rsp.credits = cpu_to_le16(credits); 5382 rsp.result = cpu_to_le16(result); 5383 5384 l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp); 5385 5386 return 0; 5387 } 5388 5389 static inline int l2cap_le_credits(struct l2cap_conn *conn, 5390 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5391 u8 *data) 5392 __must_hold(&conn->lock) 5393 { 5394 struct l2cap_le_credits *pkt; 5395 struct l2cap_chan *chan; 5396 u16 cid, credits, max_credits; 5397 5398 if (cmd_len != sizeof(*pkt)) 5399 return -EPROTO; 5400 5401 pkt = (struct l2cap_le_credits *) data; 5402 cid = __le16_to_cpu(pkt->cid); 5403 credits = __le16_to_cpu(pkt->credits); 5404 5405 BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits); 5406 5407 chan = l2cap_get_chan_by_dcid(conn, cid); 5408 if (!chan) 5409 return -EBADSLT; 5410 5411 l2cap_chan_lock(chan); 5412 5413 max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; 5414 if (credits > max_credits) { 5415 BT_ERR("LE credits overflow"); 5416 l2cap_send_disconn_req(chan, ECONNRESET); 5417 5418 /* Return 0 so that we don't trigger an unnecessary 5419 * command reject packet. 5420 */ 5421 goto unlock; 5422 } 5423 5424 chan->tx_credits += credits; 5425 5426 /* Resume sending */ 5427 l2cap_le_flowctl_send(chan); 5428 5429 if (chan->tx_credits) 5430 chan->ops->resume(chan); 5431 5432 unlock: 5433 l2cap_chan_unlock(chan); 5434 l2cap_chan_put(chan); 5435 5436 return 0; 5437 } 5438 5439 static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn, 5440 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5441 u8 *data) 5442 __must_hold(&conn->lock) 5443 { 5444 struct l2cap_ecred_conn_req *req = (void *) data; 5445 DEFINE_RAW_FLEX(struct l2cap_ecred_conn_rsp, pdu, dcid, L2CAP_ECRED_MAX_CID); 5446 struct l2cap_chan *chan, *pchan; 5447 u16 mtu, mps; 5448 __le16 psm; 5449 u8 result, rsp_len = 0; 5450 int i, num_scid = 0; 5451 bool defer = false; 5452 5453 if (!enable_ecred) 5454 return -EINVAL; 5455 5456 memset(pdu, 0, sizeof(*pdu)); 5457 5458 if (cmd_len < sizeof(*req) || (cmd_len - sizeof(*req)) % sizeof(u16)) { 5459 result = L2CAP_CR_LE_INVALID_PARAMS; 5460 goto response; 5461 } 5462 5463 /* Check if there are no pending channels with the same ident */ 5464 __l2cap_chan_list_id(conn, cmd->ident, l2cap_ecred_list_defer, 5465 &num_scid); 5466 if (num_scid) { 5467 result = L2CAP_CR_LE_INVALID_PARAMS; 5468 goto response; 5469 } 5470 5471 cmd_len -= sizeof(*req); 5472 num_scid = cmd_len / sizeof(u16); 5473 5474 if (num_scid > L2CAP_ECRED_MAX_CID) { 5475 result = L2CAP_CR_LE_INVALID_PARAMS; 5476 goto response; 5477 } 5478 5479 /* Always respond with the same number of scids as in the request */ 5480 rsp_len = cmd_len; 5481 5482 mtu = __le16_to_cpu(req->mtu); 5483 mps = __le16_to_cpu(req->mps); 5484 5485 if (mtu < L2CAP_ECRED_MIN_MTU || mps < L2CAP_ECRED_MIN_MPS) { 5486 result = L2CAP_CR_LE_INVALID_PARAMS; 5487 goto response; 5488 } 5489 5490 psm = req->psm; 5491 5492 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 3, Part A 5493 * page 1059: 5494 * 5495 * Valid range: 0x0001-0x00ff 5496 * 5497 * Table 4.15: L2CAP_LE_CREDIT_BASED_CONNECTION_REQ SPSM ranges 5498 */ 5499 if (!psm || __le16_to_cpu(psm) > L2CAP_PSM_LE_DYN_END) { 5500 result = L2CAP_CR_LE_BAD_PSM; 5501 goto response; 5502 } 5503 5504 BT_DBG("psm 0x%2.2x mtu %u mps %u", __le16_to_cpu(psm), mtu, mps); 5505 5506 /* Check if we have socket listening on psm */ 5507 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, 5508 &conn->hcon->dst, LE_LINK); 5509 if (!pchan) { 5510 result = L2CAP_CR_LE_BAD_PSM; 5511 goto response; 5512 } 5513 5514 l2cap_chan_lock(pchan); 5515 5516 if (!smp_sufficient_security(conn->hcon, pchan->sec_level, 5517 SMP_ALLOW_STK)) { 5518 result = pchan->sec_level == BT_SECURITY_MEDIUM ? 5519 L2CAP_CR_LE_ENCRYPTION : L2CAP_CR_LE_AUTHENTICATION; 5520 goto unlock; 5521 } 5522 5523 /* Check if the listening channel has set an output MTU then the 5524 * requested MTU shall be less than or equal to that value. 5525 */ 5526 if (pchan->omtu && mtu < pchan->omtu) { 5527 result = L2CAP_CR_LE_UNACCEPT_PARAMS; 5528 goto unlock; 5529 } 5530 5531 result = L2CAP_CR_LE_SUCCESS; 5532 5533 for (i = 0; i < num_scid; i++) { 5534 u16 scid = __le16_to_cpu(req->scid[i]); 5535 5536 BT_DBG("scid[%d] 0x%4.4x", i, scid); 5537 5538 pdu->dcid[i] = 0x0000; 5539 5540 /* Check for valid dynamic CID range */ 5541 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) { 5542 result = L2CAP_CR_LE_INVALID_SCID; 5543 continue; 5544 } 5545 5546 /* Check if we already have channel with that dcid */ 5547 if (__l2cap_get_chan_by_dcid(conn, scid)) { 5548 result = L2CAP_CR_LE_SCID_IN_USE; 5549 continue; 5550 } 5551 5552 chan = l2cap_new_connection(conn, pchan); 5553 if (!chan) { 5554 result = L2CAP_CR_LE_NO_MEM; 5555 continue; 5556 } 5557 5558 l2cap_chan_lock(chan); 5559 5560 lockdep_assert_held(&chan->conn->lock); 5561 5562 bacpy(&chan->src, &conn->hcon->src); 5563 bacpy(&chan->dst, &conn->hcon->dst); 5564 chan->src_type = bdaddr_src_type(conn->hcon); 5565 chan->dst_type = bdaddr_dst_type(conn->hcon); 5566 chan->psm = psm; 5567 chan->dcid = scid; 5568 chan->omtu = mtu; 5569 chan->remote_mps = mps; 5570 5571 l2cap_ecred_init(chan, __le16_to_cpu(req->credits)); 5572 5573 /* Init response */ 5574 if (!pdu->credits) { 5575 pdu->mtu = cpu_to_le16(chan->imtu); 5576 pdu->mps = cpu_to_le16(chan->mps); 5577 pdu->credits = cpu_to_le16(chan->rx_credits); 5578 } 5579 5580 pdu->dcid[i] = cpu_to_le16(chan->scid); 5581 5582 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); 5583 5584 chan->mode = L2CAP_MODE_EXT_FLOWCTL; 5585 5586 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { 5587 chan->ident = cmd->ident; 5588 l2cap_state_change(chan, BT_CONNECT2); 5589 defer = true; 5590 chan->ops->defer(chan); 5591 } else { 5592 l2cap_chan_ready(chan); 5593 } 5594 5595 l2cap_chan_unlock(chan); 5596 } 5597 5598 unlock: 5599 l2cap_chan_unlock(pchan); 5600 l2cap_chan_put(pchan); 5601 5602 response: 5603 pdu->result = cpu_to_le16(result); 5604 5605 if (defer) 5606 return 0; 5607 5608 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECRED_CONN_RSP, 5609 sizeof(*pdu) + rsp_len, pdu); 5610 5611 return 0; 5612 } 5613 5614 static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn, 5615 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5616 u8 *data) 5617 __must_hold(&conn->lock) 5618 { 5619 struct l2cap_ecred_conn_rsp *rsp = (void *) data; 5620 struct hci_conn *hcon = conn->hcon; 5621 u16 mtu, mps, credits, result; 5622 struct l2cap_chan *chan, *tmp; 5623 int err = 0, sec_level; 5624 int i = 0; 5625 5626 if (cmd_len < sizeof(*rsp)) 5627 return -EPROTO; 5628 5629 mtu = __le16_to_cpu(rsp->mtu); 5630 mps = __le16_to_cpu(rsp->mps); 5631 credits = __le16_to_cpu(rsp->credits); 5632 result = __le16_to_cpu(rsp->result); 5633 5634 BT_DBG("mtu %u mps %u credits %u result 0x%4.4x", mtu, mps, credits, 5635 result); 5636 5637 cmd_len -= sizeof(*rsp); 5638 5639 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 5640 struct l2cap_chan *orig; 5641 u16 dcid; 5642 5643 if (chan->state != BT_CONNECT || 5644 chan->mode != L2CAP_MODE_EXT_FLOWCTL || 5645 !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || 5646 chan->ident != cmd->ident) 5647 continue; 5648 5649 l2cap_chan_hold(chan); 5650 l2cap_chan_lock(chan); 5651 5652 lockdep_assert_held(&chan->conn->lock); 5653 5654 /* Check that there is a dcid for each pending channel */ 5655 if (cmd_len < sizeof(dcid)) { 5656 l2cap_chan_del(chan, ECONNREFUSED); 5657 l2cap_chan_unlock(chan); 5658 l2cap_chan_put(chan); 5659 continue; 5660 } 5661 5662 dcid = __le16_to_cpu(rsp->dcid[i++]); 5663 cmd_len -= sizeof(u16); 5664 5665 BT_DBG("dcid[%d] 0x%4.4x", i, dcid); 5666 5667 orig = __l2cap_get_chan_by_dcid(conn, dcid); 5668 5669 /* Check if dcid is already in use */ 5670 if (dcid && orig) { 5671 /* If a device receives a 5672 * L2CAP_CREDIT_BASED_CONNECTION_RSP packet with an 5673 * already-assigned Destination CID, then both the 5674 * original channel and the new channel shall be 5675 * immediately discarded and not used. 5676 */ 5677 l2cap_chan_del(chan, ECONNREFUSED); 5678 l2cap_chan_unlock(chan); 5679 5680 /* Check that the dcid channel mode is 5681 * L2CAP_MODE_EXT_FLOWCTL since this procedure is only 5682 * valid for that mode and shouldn't disconnect a dcid 5683 * in other modes. 5684 */ 5685 if (orig->mode == L2CAP_MODE_EXT_FLOWCTL) { 5686 l2cap_chan_lock(orig); 5687 /* Disconnect the original channel as it may be 5688 * considered connected since dcid has already 5689 * been assigned; don't call l2cap_chan_close 5690 * directly since that could lead to 5691 * l2cap_chan_del and then removing the channel 5692 * from the list while we're iterating over it. 5693 */ 5694 __set_chan_timer(orig, 0); 5695 l2cap_chan_unlock(orig); 5696 } 5697 5698 l2cap_chan_put(chan); 5699 continue; 5700 } 5701 5702 switch (result) { 5703 case L2CAP_CR_LE_AUTHENTICATION: 5704 case L2CAP_CR_LE_ENCRYPTION: 5705 /* If we already have MITM protection we can't do 5706 * anything. 5707 */ 5708 if (hcon->sec_level > BT_SECURITY_MEDIUM) { 5709 l2cap_chan_del(chan, ECONNREFUSED); 5710 break; 5711 } 5712 5713 sec_level = hcon->sec_level + 1; 5714 if (chan->sec_level < sec_level) 5715 chan->sec_level = sec_level; 5716 5717 /* We'll need to send a new Connect Request */ 5718 clear_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags); 5719 5720 smp_conn_security(hcon, chan->sec_level); 5721 break; 5722 5723 case L2CAP_CR_LE_BAD_PSM: 5724 l2cap_chan_del(chan, ECONNREFUSED); 5725 break; 5726 5727 default: 5728 /* If dcid was not set it means channels was refused */ 5729 if (!dcid) { 5730 l2cap_chan_del(chan, ECONNREFUSED); 5731 break; 5732 } 5733 5734 /* 5735 * Validate MPS and MTU per BT Core Spec 5736 * Vol 3 Part A Sec 10.2 5737 */ 5738 if (mtu < L2CAP_ECRED_MIN_MTU || 5739 mps < L2CAP_ECRED_MIN_MPS) { 5740 l2cap_chan_del(chan, ECONNREFUSED); 5741 break; 5742 } 5743 5744 chan->ident = 0; 5745 chan->dcid = dcid; 5746 chan->omtu = mtu; 5747 chan->remote_mps = mps; 5748 chan->tx_credits = credits; 5749 l2cap_chan_ready(chan); 5750 break; 5751 } 5752 5753 l2cap_chan_unlock(chan); 5754 l2cap_chan_put(chan); 5755 } 5756 5757 return err; 5758 } 5759 5760 static inline int l2cap_ecred_reconf_req(struct l2cap_conn *conn, 5761 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5762 u8 *data) 5763 __must_hold(&conn->lock) 5764 { 5765 struct l2cap_ecred_reconf_req *req = (void *) data; 5766 struct l2cap_ecred_reconf_rsp rsp; 5767 u16 mtu, mps, result; 5768 struct l2cap_chan *chan[L2CAP_ECRED_MAX_CID] = {}; 5769 int i, num_scid; 5770 5771 if (!enable_ecred) 5772 return -EINVAL; 5773 5774 if (cmd_len < sizeof(*req) || (cmd_len - sizeof(*req)) % sizeof(u16)) { 5775 result = L2CAP_RECONF_INVALID_CID; 5776 goto respond; 5777 } 5778 5779 mtu = __le16_to_cpu(req->mtu); 5780 mps = __le16_to_cpu(req->mps); 5781 5782 BT_DBG("mtu %u mps %u", mtu, mps); 5783 5784 if (mtu < L2CAP_ECRED_MIN_MTU) { 5785 result = L2CAP_RECONF_INVALID_PARAMS; 5786 goto respond; 5787 } 5788 5789 if (mps < L2CAP_ECRED_MIN_MPS) { 5790 result = L2CAP_RECONF_INVALID_PARAMS; 5791 goto respond; 5792 } 5793 5794 cmd_len -= sizeof(*req); 5795 num_scid = cmd_len / sizeof(u16); 5796 5797 if (num_scid > L2CAP_ECRED_MAX_CID) { 5798 result = L2CAP_RECONF_INVALID_PARAMS; 5799 goto respond; 5800 } 5801 5802 result = L2CAP_RECONF_SUCCESS; 5803 5804 /* Check if each SCID, MTU and MPS are valid */ 5805 for (i = 0; i < num_scid; i++) { 5806 u16 scid; 5807 5808 scid = __le16_to_cpu(req->scid[i]); 5809 if (!scid) { 5810 result = L2CAP_RECONF_INVALID_CID; 5811 goto respond; 5812 } 5813 5814 chan[i] = __l2cap_get_chan_by_dcid(conn, scid); 5815 if (!chan[i]) { 5816 result = L2CAP_RECONF_INVALID_CID; 5817 goto respond; 5818 } 5819 5820 /* The MTU field shall be greater than or equal to the greatest 5821 * current MTU size of these channels. 5822 */ 5823 if (chan[i]->omtu > mtu) { 5824 BT_ERR("chan %p decreased MTU %u -> %u", chan[i], 5825 chan[i]->omtu, mtu); 5826 result = L2CAP_RECONF_INVALID_MTU; 5827 goto respond; 5828 } 5829 5830 /* If more than one channel is being configured, the MPS field 5831 * shall be greater than or equal to the current MPS size of 5832 * each of these channels. If only one channel is being 5833 * configured, the MPS field may be less than the current MPS 5834 * of that channel. 5835 */ 5836 if (chan[i]->remote_mps > mps && num_scid > 1) { 5837 BT_ERR("chan %p decreased MPS %u -> %u", chan[i], 5838 chan[i]->remote_mps, mps); 5839 result = L2CAP_RECONF_INVALID_MPS; 5840 goto respond; 5841 } 5842 } 5843 5844 /* Commit the new MTU and MPS values after checking they are valid */ 5845 for (i = 0; i < num_scid; i++) { 5846 chan[i]->omtu = mtu; 5847 chan[i]->remote_mps = mps; 5848 } 5849 5850 respond: 5851 rsp.result = cpu_to_le16(result); 5852 5853 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECRED_RECONF_RSP, sizeof(rsp), 5854 &rsp); 5855 5856 return 0; 5857 } 5858 5859 static inline int l2cap_ecred_reconf_rsp(struct l2cap_conn *conn, 5860 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5861 u8 *data) 5862 __must_hold(&conn->lock) 5863 { 5864 struct l2cap_chan *chan, *tmp; 5865 struct l2cap_ecred_reconf_rsp *rsp = (void *)data; 5866 u16 result; 5867 5868 if (cmd_len < sizeof(*rsp)) 5869 return -EPROTO; 5870 5871 result = __le16_to_cpu(rsp->result); 5872 5873 BT_DBG("result 0x%4.4x", result); 5874 5875 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 5876 /* Reconfiguration updates ident with only chan->lock held. */ 5877 if (data_race(READ_ONCE(chan->ident)) != cmd->ident) 5878 continue; 5879 5880 if (!l2cap_chan_hold_unless_zero(chan)) 5881 continue; 5882 l2cap_chan_lock(chan); 5883 5884 lockdep_assert_held(&chan->conn->lock); 5885 5886 if (chan->ident == cmd->ident && 5887 test_and_clear_bit(FLAG_ECRED_RECONF_REQ_SENT, &chan->flags)) { 5888 if (result) { 5889 l2cap_chan_del(chan, ECONNRESET); 5890 chan->ops->close(chan); 5891 } else { 5892 chan->ident = 0; 5893 } 5894 } 5895 5896 l2cap_chan_unlock(chan); 5897 l2cap_chan_put(chan); 5898 } 5899 5900 return 0; 5901 } 5902 5903 static inline int l2cap_le_command_rej(struct l2cap_conn *conn, 5904 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5905 u8 *data) 5906 __must_hold(&conn->lock) 5907 { 5908 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; 5909 struct l2cap_chan *chan, *tmp; 5910 5911 if (cmd_len < sizeof(*rej)) 5912 return -EPROTO; 5913 5914 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 5915 /* Reconfiguration updates ident with only chan->lock held. */ 5916 if (data_race(READ_ONCE(chan->ident)) != cmd->ident) 5917 continue; 5918 5919 if (!l2cap_chan_hold_unless_zero(chan)) 5920 continue; 5921 5922 l2cap_chan_lock(chan); 5923 lockdep_assert_held(&chan->conn->lock); 5924 5925 if (chan->ident == cmd->ident && 5926 ((chan->state == BT_CONNECT && 5927 (test_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags) || 5928 test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))) || 5929 test_bit(FLAG_ECRED_RECONF_REQ_SENT, &chan->flags))) { 5930 l2cap_chan_del(chan, ECONNREFUSED); 5931 chan->ops->close(chan); 5932 } 5933 5934 l2cap_chan_unlock(chan); 5935 l2cap_chan_put(chan); 5936 } 5937 5938 return 0; 5939 } 5940 5941 static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 5942 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 5943 u8 *data) 5944 __must_hold(&conn->lock) 5945 { 5946 int err = 0; 5947 5948 l2cap_put_ident(conn, cmd->code, cmd->ident); 5949 5950 switch (cmd->code) { 5951 case L2CAP_COMMAND_REJ: 5952 l2cap_le_command_rej(conn, cmd, cmd_len, data); 5953 break; 5954 5955 case L2CAP_CONN_PARAM_UPDATE_REQ: 5956 err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data); 5957 break; 5958 5959 case L2CAP_CONN_PARAM_UPDATE_RSP: 5960 break; 5961 5962 case L2CAP_LE_CONN_RSP: 5963 l2cap_le_connect_rsp(conn, cmd, cmd_len, data); 5964 break; 5965 5966 case L2CAP_LE_CONN_REQ: 5967 err = l2cap_le_connect_req(conn, cmd, cmd_len, data); 5968 break; 5969 5970 case L2CAP_LE_CREDITS: 5971 err = l2cap_le_credits(conn, cmd, cmd_len, data); 5972 break; 5973 5974 case L2CAP_ECRED_CONN_REQ: 5975 err = l2cap_ecred_conn_req(conn, cmd, cmd_len, data); 5976 break; 5977 5978 case L2CAP_ECRED_CONN_RSP: 5979 err = l2cap_ecred_conn_rsp(conn, cmd, cmd_len, data); 5980 break; 5981 5982 case L2CAP_ECRED_RECONF_REQ: 5983 err = l2cap_ecred_reconf_req(conn, cmd, cmd_len, data); 5984 break; 5985 5986 case L2CAP_ECRED_RECONF_RSP: 5987 err = l2cap_ecred_reconf_rsp(conn, cmd, cmd_len, data); 5988 break; 5989 5990 case L2CAP_DISCONN_REQ: 5991 err = l2cap_disconnect_req(conn, cmd, cmd_len, data); 5992 break; 5993 5994 case L2CAP_DISCONN_RSP: 5995 l2cap_disconnect_rsp(conn, cmd, cmd_len, data); 5996 break; 5997 5998 default: 5999 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 6000 err = -EINVAL; 6001 break; 6002 } 6003 6004 return err; 6005 } 6006 6007 static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, 6008 struct sk_buff *skb) 6009 __must_hold(&conn->lock) 6010 { 6011 struct hci_conn *hcon = conn->hcon; 6012 struct l2cap_cmd_hdr *cmd; 6013 u16 len; 6014 int err; 6015 6016 if (hcon->type != LE_LINK) 6017 goto drop; 6018 6019 if (skb->len < L2CAP_CMD_HDR_SIZE) 6020 goto drop; 6021 6022 cmd = (void *) skb->data; 6023 skb_pull(skb, L2CAP_CMD_HDR_SIZE); 6024 6025 len = le16_to_cpu(cmd->len); 6026 6027 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident); 6028 6029 if (len != skb->len || !cmd->ident) { 6030 BT_DBG("corrupted command"); 6031 goto drop; 6032 } 6033 6034 err = l2cap_le_sig_cmd(conn, cmd, len, skb->data); 6035 if (err) { 6036 struct l2cap_cmd_rej_unk rej; 6037 6038 BT_ERR("Wrong link type (%d)", err); 6039 6040 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 6041 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 6042 sizeof(rej), &rej); 6043 } 6044 6045 drop: 6046 kfree_skb(skb); 6047 } 6048 6049 static inline void l2cap_sig_send_rej(struct l2cap_conn *conn, u16 ident) 6050 { 6051 struct l2cap_cmd_rej_unk rej; 6052 6053 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 6054 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 6055 } 6056 6057 static inline void l2cap_sig_send_mtu_rej(struct l2cap_conn *conn, u8 ident) 6058 { 6059 struct l2cap_cmd_rej_mtu rej; 6060 6061 rej.reason = cpu_to_le16(L2CAP_REJ_MTU_EXCEEDED); 6062 rej.max_mtu = cpu_to_le16(L2CAP_SIG_MTU); 6063 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 6064 } 6065 6066 static inline void l2cap_sig_channel(struct l2cap_conn *conn, 6067 struct sk_buff *skb) 6068 __must_hold(&conn->lock) 6069 { 6070 struct hci_conn *hcon = conn->hcon; 6071 struct l2cap_cmd_hdr *cmd; 6072 int err; 6073 6074 l2cap_raw_recv(conn, skb); 6075 6076 if (hcon->type != ACL_LINK) 6077 goto drop; 6078 6079 /* 6080 * Bluetooth Core v5.4, Vol 3, Part A, Section 4: the BR/EDR 6081 * signaling channel has a fixed signaling MTU (MTUsig) whose 6082 * minimum and default is 48 octets. Section 4.1 says that on 6083 * an MTUExceeded command reject the identifier "shall match 6084 * the first request command in the L2CAP packet" and that 6085 * packets containing only response commands "shall be 6086 * silently discarded". 6087 * 6088 * Linux intentionally deviates from that prescription: 6089 * 6090 * 1. Silently discarding desynchronizes the peer. The 6091 * remote stack never learns its responses were dropped, 6092 * so any state machine waiting on a paired response 6093 * stalls until its own timer fires. 6094 * 6095 * 2. Locating "the first request command" requires walking 6096 * command headers past MTUsig, i.e. processing bytes 6097 * from a packet we have already decided is too large to 6098 * process. 6099 * 6100 * Reject every over-MTUsig signaling packet with one 6101 * L2CAP_REJ_MTU_EXCEEDED command reject. The reject's 6102 * reason field is what tells the peer that the whole packet 6103 * was discarded; the identifier value is informational, so 6104 * we use the identifier from the first command header, a 6105 * single fixed-offset byte read. 6106 */ 6107 if (skb->len > L2CAP_SIG_MTU) { 6108 u8 ident = skb->data[1]; 6109 6110 BT_DBG("signaling packet exceeds MTU: %u > %u", 6111 skb->len, L2CAP_SIG_MTU); 6112 l2cap_sig_send_mtu_rej(conn, ident); 6113 goto drop; 6114 } 6115 6116 while (skb->len >= L2CAP_CMD_HDR_SIZE) { 6117 u16 len; 6118 6119 cmd = (void *) skb->data; 6120 skb_pull(skb, L2CAP_CMD_HDR_SIZE); 6121 6122 len = le16_to_cpu(cmd->len); 6123 6124 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, 6125 cmd->ident); 6126 6127 if (len > skb->len || !cmd->ident) { 6128 BT_DBG("corrupted command"); 6129 l2cap_sig_send_rej(conn, cmd->ident); 6130 skb_pull(skb, len > skb->len ? skb->len : len); 6131 continue; 6132 } 6133 6134 err = l2cap_bredr_sig_cmd(conn, cmd, len, skb->data); 6135 if (err) { 6136 BT_ERR("Wrong link type (%d)", err); 6137 l2cap_sig_send_rej(conn, cmd->ident); 6138 } 6139 6140 skb_pull(skb, len); 6141 } 6142 6143 if (skb->len > 0) { 6144 BT_DBG("corrupted command"); 6145 l2cap_sig_send_rej(conn, 0); 6146 } 6147 6148 drop: 6149 kfree_skb(skb); 6150 } 6151 6152 static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 6153 { 6154 u16 our_fcs, rcv_fcs; 6155 int hdr_size; 6156 6157 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 6158 hdr_size = L2CAP_EXT_HDR_SIZE; 6159 else 6160 hdr_size = L2CAP_ENH_HDR_SIZE; 6161 6162 if (chan->fcs == L2CAP_FCS_CRC16) { 6163 skb_trim(skb, skb->len - L2CAP_FCS_SIZE); 6164 rcv_fcs = get_unaligned_le16(skb->data + skb->len); 6165 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 6166 6167 if (our_fcs != rcv_fcs) 6168 return -EBADMSG; 6169 } 6170 return 0; 6171 } 6172 6173 static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 6174 { 6175 struct l2cap_ctrl control; 6176 6177 BT_DBG("chan %p", chan); 6178 6179 memset(&control, 0, sizeof(control)); 6180 control.sframe = 1; 6181 control.final = 1; 6182 control.reqseq = chan->buffer_seq; 6183 set_bit(CONN_SEND_FBIT, &chan->conn_state); 6184 6185 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 6186 control.super = L2CAP_SUPER_RNR; 6187 l2cap_send_sframe(chan, &control); 6188 } 6189 6190 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 6191 chan->unacked_frames > 0) 6192 __set_retrans_timer(chan); 6193 6194 /* Send pending iframes */ 6195 l2cap_ertm_send(chan); 6196 6197 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && 6198 test_bit(CONN_SEND_FBIT, &chan->conn_state)) { 6199 /* F-bit wasn't sent in an s-frame or i-frame yet, so 6200 * send it now. 6201 */ 6202 control.super = L2CAP_SUPER_RR; 6203 l2cap_send_sframe(chan, &control); 6204 } 6205 } 6206 6207 static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, 6208 struct sk_buff **last_frag) 6209 { 6210 /* skb->len reflects data in skb as well as all fragments 6211 * skb->data_len reflects only data in fragments 6212 */ 6213 if (!skb_has_frag_list(skb)) 6214 skb_shinfo(skb)->frag_list = new_frag; 6215 6216 new_frag->next = NULL; 6217 6218 (*last_frag)->next = new_frag; 6219 *last_frag = new_frag; 6220 6221 skb->len += new_frag->len; 6222 skb->data_len += new_frag->len; 6223 skb->truesize += new_frag->truesize; 6224 } 6225 6226 static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, 6227 struct l2cap_ctrl *control) 6228 { 6229 struct sk_buff *sdu; 6230 int err = -EINVAL; 6231 6232 switch (control->sar) { 6233 case L2CAP_SAR_UNSEGMENTED: 6234 if (chan->sdu) 6235 break; 6236 6237 err = chan->ops->recv(chan, skb); 6238 break; 6239 6240 case L2CAP_SAR_START: 6241 if (chan->sdu) 6242 break; 6243 6244 if (!pskb_may_pull(skb, L2CAP_SDULEN_SIZE)) 6245 break; 6246 6247 chan->sdu_len = get_unaligned_le16(skb->data); 6248 skb_pull(skb, L2CAP_SDULEN_SIZE); 6249 6250 if (chan->sdu_len > chan->imtu) { 6251 err = -EMSGSIZE; 6252 break; 6253 } 6254 6255 if (skb->len >= chan->sdu_len) 6256 break; 6257 6258 chan->sdu = skb; 6259 chan->sdu_last_frag = skb; 6260 6261 skb = NULL; 6262 err = 0; 6263 break; 6264 6265 case L2CAP_SAR_CONTINUE: 6266 if (!chan->sdu) 6267 break; 6268 6269 append_skb_frag(chan->sdu, skb, 6270 &chan->sdu_last_frag); 6271 skb = NULL; 6272 6273 if (chan->sdu->len >= chan->sdu_len) 6274 break; 6275 6276 err = 0; 6277 break; 6278 6279 case L2CAP_SAR_END: 6280 if (!chan->sdu) 6281 break; 6282 6283 append_skb_frag(chan->sdu, skb, 6284 &chan->sdu_last_frag); 6285 skb = NULL; 6286 6287 if (chan->sdu->len != chan->sdu_len) 6288 break; 6289 6290 /* Reassembly complete. Hand the SDU over before the 6291 * callback, for the reason given in l2cap_ecred_data_rcv(): 6292 * the callback takes ownership and may free it, while 6293 * l2cap_chan_rx_avail() -> l2cap_chan_le_send_credits() reads 6294 * chan->sdu->len on the way out - it does so before the mode 6295 * test that makes the rest of that function a no-op here. 6296 * Return directly, since the state this leaves is what the 6297 * trailing error block would otherwise set again. 6298 */ 6299 sdu = chan->sdu; 6300 chan->sdu = NULL; 6301 chan->sdu_last_frag = NULL; 6302 chan->sdu_len = 0; 6303 6304 err = chan->ops->recv(chan, sdu); 6305 if (err) 6306 kfree_skb(sdu); 6307 6308 return err; 6309 } 6310 6311 if (err) { 6312 kfree_skb(skb); 6313 kfree_skb(chan->sdu); 6314 chan->sdu = NULL; 6315 chan->sdu_last_frag = NULL; 6316 chan->sdu_len = 0; 6317 } 6318 6319 return err; 6320 } 6321 6322 static int l2cap_resegment(struct l2cap_chan *chan) 6323 { 6324 /* Placeholder */ 6325 return 0; 6326 } 6327 6328 void l2cap_chan_busy(struct l2cap_chan *chan, int busy) 6329 { 6330 u8 event; 6331 6332 if (chan->mode != L2CAP_MODE_ERTM) 6333 return; 6334 6335 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; 6336 l2cap_tx(chan, NULL, NULL, event); 6337 } 6338 6339 static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) 6340 { 6341 int err = 0; 6342 /* Pass sequential frames to l2cap_reassemble_sdu() 6343 * until a gap is encountered. 6344 */ 6345 6346 BT_DBG("chan %p", chan); 6347 6348 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 6349 struct sk_buff *skb; 6350 BT_DBG("Searching for skb with txseq %d (queue len %d)", 6351 chan->buffer_seq, skb_queue_len(&chan->srej_q)); 6352 6353 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); 6354 6355 if (!skb) 6356 break; 6357 6358 skb_unlink(skb, &chan->srej_q); 6359 chan->buffer_seq = __next_seq(chan, chan->buffer_seq); 6360 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->l2cap); 6361 if (err) 6362 break; 6363 } 6364 6365 if (skb_queue_empty(&chan->srej_q)) { 6366 chan->rx_state = L2CAP_RX_STATE_RECV; 6367 l2cap_send_ack(chan); 6368 } 6369 6370 return err; 6371 } 6372 6373 static void l2cap_handle_srej(struct l2cap_chan *chan, 6374 struct l2cap_ctrl *control) 6375 { 6376 struct sk_buff *skb; 6377 6378 BT_DBG("chan %p, control %p", chan, control); 6379 6380 if (control->reqseq == chan->next_tx_seq) { 6381 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); 6382 l2cap_send_disconn_req(chan, ECONNRESET); 6383 return; 6384 } 6385 6386 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); 6387 6388 if (skb == NULL) { 6389 BT_DBG("Seq %d not available for retransmission", 6390 control->reqseq); 6391 return; 6392 } 6393 6394 if (chan->max_tx != 0 && bt_cb(skb)->l2cap.retries >= chan->max_tx) { 6395 BT_DBG("Retry limit exceeded (%d)", chan->max_tx); 6396 l2cap_send_disconn_req(chan, ECONNRESET); 6397 return; 6398 } 6399 6400 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 6401 6402 if (control->poll) { 6403 l2cap_pass_to_tx(chan, control); 6404 6405 set_bit(CONN_SEND_FBIT, &chan->conn_state); 6406 l2cap_retransmit(chan, control); 6407 l2cap_ertm_send(chan); 6408 6409 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { 6410 set_bit(CONN_SREJ_ACT, &chan->conn_state); 6411 chan->srej_save_reqseq = control->reqseq; 6412 } 6413 } else { 6414 l2cap_pass_to_tx_fbit(chan, control); 6415 6416 if (control->final) { 6417 if (chan->srej_save_reqseq != control->reqseq || 6418 !test_and_clear_bit(CONN_SREJ_ACT, 6419 &chan->conn_state)) 6420 l2cap_retransmit(chan, control); 6421 } else { 6422 l2cap_retransmit(chan, control); 6423 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { 6424 set_bit(CONN_SREJ_ACT, &chan->conn_state); 6425 chan->srej_save_reqseq = control->reqseq; 6426 } 6427 } 6428 } 6429 } 6430 6431 static void l2cap_handle_rej(struct l2cap_chan *chan, 6432 struct l2cap_ctrl *control) 6433 { 6434 struct sk_buff *skb; 6435 6436 BT_DBG("chan %p, control %p", chan, control); 6437 6438 if (control->reqseq == chan->next_tx_seq) { 6439 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); 6440 l2cap_send_disconn_req(chan, ECONNRESET); 6441 return; 6442 } 6443 6444 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); 6445 6446 if (chan->max_tx && skb && 6447 bt_cb(skb)->l2cap.retries >= chan->max_tx) { 6448 BT_DBG("Retry limit exceeded (%d)", chan->max_tx); 6449 l2cap_send_disconn_req(chan, ECONNRESET); 6450 return; 6451 } 6452 6453 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 6454 6455 l2cap_pass_to_tx(chan, control); 6456 6457 if (control->final) { 6458 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 6459 l2cap_retransmit_all(chan, control); 6460 } else { 6461 l2cap_retransmit_all(chan, control); 6462 l2cap_ertm_send(chan); 6463 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) 6464 set_bit(CONN_REJ_ACT, &chan->conn_state); 6465 } 6466 } 6467 6468 static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) 6469 { 6470 BT_DBG("chan %p, txseq %d", chan, txseq); 6471 6472 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, 6473 chan->expected_tx_seq); 6474 6475 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { 6476 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= 6477 chan->tx_win) { 6478 /* See notes below regarding "double poll" and 6479 * invalid packets. 6480 */ 6481 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { 6482 BT_DBG("Invalid/Ignore - after SREJ"); 6483 return L2CAP_TXSEQ_INVALID_IGNORE; 6484 } else { 6485 BT_DBG("Invalid - in window after SREJ sent"); 6486 return L2CAP_TXSEQ_INVALID; 6487 } 6488 } 6489 6490 if (chan->srej_list.head == txseq) { 6491 BT_DBG("Expected SREJ"); 6492 return L2CAP_TXSEQ_EXPECTED_SREJ; 6493 } 6494 6495 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { 6496 BT_DBG("Duplicate SREJ - txseq already stored"); 6497 return L2CAP_TXSEQ_DUPLICATE_SREJ; 6498 } 6499 6500 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { 6501 BT_DBG("Unexpected SREJ - not requested"); 6502 return L2CAP_TXSEQ_UNEXPECTED_SREJ; 6503 } 6504 } 6505 6506 if (chan->expected_tx_seq == txseq) { 6507 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= 6508 chan->tx_win) { 6509 BT_DBG("Invalid - txseq outside tx window"); 6510 return L2CAP_TXSEQ_INVALID; 6511 } else { 6512 BT_DBG("Expected"); 6513 return L2CAP_TXSEQ_EXPECTED; 6514 } 6515 } 6516 6517 if (__seq_offset(chan, txseq, chan->last_acked_seq) < 6518 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { 6519 BT_DBG("Duplicate - expected_tx_seq later than txseq"); 6520 return L2CAP_TXSEQ_DUPLICATE; 6521 } 6522 6523 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { 6524 /* A source of invalid packets is a "double poll" condition, 6525 * where delays cause us to send multiple poll packets. If 6526 * the remote stack receives and processes both polls, 6527 * sequence numbers can wrap around in such a way that a 6528 * resent frame has a sequence number that looks like new data 6529 * with a sequence gap. This would trigger an erroneous SREJ 6530 * request. 6531 * 6532 * Fortunately, this is impossible with a tx window that's 6533 * less than half of the maximum sequence number, which allows 6534 * invalid frames to be safely ignored. 6535 * 6536 * With tx window sizes greater than half of the tx window 6537 * maximum, the frame is invalid and cannot be ignored. This 6538 * causes a disconnect. 6539 */ 6540 6541 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { 6542 BT_DBG("Invalid/Ignore - txseq outside tx window"); 6543 return L2CAP_TXSEQ_INVALID_IGNORE; 6544 } else { 6545 BT_DBG("Invalid - txseq outside tx window"); 6546 return L2CAP_TXSEQ_INVALID; 6547 } 6548 } else { 6549 BT_DBG("Unexpected - txseq indicates missing frames"); 6550 return L2CAP_TXSEQ_UNEXPECTED; 6551 } 6552 } 6553 6554 static int l2cap_rx_state_recv(struct l2cap_chan *chan, 6555 struct l2cap_ctrl *control, 6556 struct sk_buff *skb, u8 event) 6557 { 6558 struct l2cap_ctrl local_control; 6559 int err = 0; 6560 bool skb_in_use = false; 6561 6562 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, 6563 event); 6564 6565 switch (event) { 6566 case L2CAP_EV_RECV_IFRAME: 6567 switch (l2cap_classify_txseq(chan, control->txseq)) { 6568 case L2CAP_TXSEQ_EXPECTED: 6569 l2cap_pass_to_tx(chan, control); 6570 6571 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 6572 BT_DBG("Busy, discarding expected seq %d", 6573 control->txseq); 6574 break; 6575 } 6576 6577 chan->expected_tx_seq = __next_seq(chan, 6578 control->txseq); 6579 6580 chan->buffer_seq = chan->expected_tx_seq; 6581 skb_in_use = true; 6582 6583 /* l2cap_reassemble_sdu may free skb, hence invalidate 6584 * control, so make a copy in advance to use it after 6585 * l2cap_reassemble_sdu returns and to avoid the race 6586 * condition, for example: 6587 * 6588 * The current thread calls: 6589 * l2cap_reassemble_sdu 6590 * chan->ops->recv == l2cap_sock_recv_cb 6591 * __sock_queue_rcv_skb 6592 * Another thread calls: 6593 * bt_sock_recvmsg 6594 * skb_recv_datagram 6595 * skb_free_datagram 6596 * Then the current thread tries to access control, but 6597 * it was freed by skb_free_datagram. 6598 */ 6599 local_control = *control; 6600 err = l2cap_reassemble_sdu(chan, skb, control); 6601 if (err) 6602 break; 6603 6604 if (local_control.final) { 6605 if (!test_and_clear_bit(CONN_REJ_ACT, 6606 &chan->conn_state)) { 6607 local_control.final = 0; 6608 l2cap_retransmit_all(chan, &local_control); 6609 l2cap_ertm_send(chan); 6610 } 6611 } 6612 6613 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) 6614 l2cap_send_ack(chan); 6615 break; 6616 case L2CAP_TXSEQ_UNEXPECTED: 6617 l2cap_pass_to_tx(chan, control); 6618 6619 /* Can't issue SREJ frames in the local busy state. 6620 * Drop this frame, it will be seen as missing 6621 * when local busy is exited. 6622 */ 6623 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 6624 BT_DBG("Busy, discarding unexpected seq %d", 6625 control->txseq); 6626 break; 6627 } 6628 6629 /* There was a gap in the sequence, so an SREJ 6630 * must be sent for each missing frame. The 6631 * current frame is stored for later use. 6632 */ 6633 skb_queue_tail(&chan->srej_q, skb); 6634 skb_in_use = true; 6635 BT_DBG("Queued %p (queue len %d)", skb, 6636 skb_queue_len(&chan->srej_q)); 6637 6638 clear_bit(CONN_SREJ_ACT, &chan->conn_state); 6639 l2cap_seq_list_clear(&chan->srej_list); 6640 l2cap_send_srej(chan, control->txseq); 6641 6642 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; 6643 break; 6644 case L2CAP_TXSEQ_DUPLICATE: 6645 l2cap_pass_to_tx(chan, control); 6646 break; 6647 case L2CAP_TXSEQ_INVALID_IGNORE: 6648 break; 6649 case L2CAP_TXSEQ_INVALID: 6650 default: 6651 l2cap_send_disconn_req(chan, ECONNRESET); 6652 break; 6653 } 6654 break; 6655 case L2CAP_EV_RECV_RR: 6656 l2cap_pass_to_tx(chan, control); 6657 if (control->final) { 6658 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 6659 6660 if (!test_and_clear_bit(CONN_REJ_ACT, 6661 &chan->conn_state)) { 6662 control->final = 0; 6663 l2cap_retransmit_all(chan, control); 6664 } 6665 6666 l2cap_ertm_send(chan); 6667 } else if (control->poll) { 6668 l2cap_send_i_or_rr_or_rnr(chan); 6669 } else { 6670 if (test_and_clear_bit(CONN_REMOTE_BUSY, 6671 &chan->conn_state) && 6672 chan->unacked_frames) 6673 __set_retrans_timer(chan); 6674 6675 l2cap_ertm_send(chan); 6676 } 6677 break; 6678 case L2CAP_EV_RECV_RNR: 6679 set_bit(CONN_REMOTE_BUSY, &chan->conn_state); 6680 l2cap_pass_to_tx(chan, control); 6681 if (control && control->poll) { 6682 set_bit(CONN_SEND_FBIT, &chan->conn_state); 6683 l2cap_send_rr_or_rnr(chan, 0); 6684 } 6685 __clear_retrans_timer(chan); 6686 l2cap_seq_list_clear(&chan->retrans_list); 6687 break; 6688 case L2CAP_EV_RECV_REJ: 6689 l2cap_handle_rej(chan, control); 6690 break; 6691 case L2CAP_EV_RECV_SREJ: 6692 l2cap_handle_srej(chan, control); 6693 break; 6694 default: 6695 break; 6696 } 6697 6698 if (skb && !skb_in_use) { 6699 BT_DBG("Freeing %p", skb); 6700 kfree_skb(skb); 6701 } 6702 6703 return err; 6704 } 6705 6706 static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, 6707 struct l2cap_ctrl *control, 6708 struct sk_buff *skb, u8 event) 6709 { 6710 int err = 0; 6711 u16 txseq = control->txseq; 6712 bool skb_in_use = false; 6713 6714 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, 6715 event); 6716 6717 switch (event) { 6718 case L2CAP_EV_RECV_IFRAME: 6719 switch (l2cap_classify_txseq(chan, txseq)) { 6720 case L2CAP_TXSEQ_EXPECTED: 6721 /* Keep frame for reassembly later */ 6722 l2cap_pass_to_tx(chan, control); 6723 skb_queue_tail(&chan->srej_q, skb); 6724 skb_in_use = true; 6725 BT_DBG("Queued %p (queue len %d)", skb, 6726 skb_queue_len(&chan->srej_q)); 6727 6728 chan->expected_tx_seq = __next_seq(chan, txseq); 6729 break; 6730 case L2CAP_TXSEQ_EXPECTED_SREJ: 6731 l2cap_seq_list_pop(&chan->srej_list); 6732 6733 l2cap_pass_to_tx(chan, control); 6734 skb_queue_tail(&chan->srej_q, skb); 6735 skb_in_use = true; 6736 BT_DBG("Queued %p (queue len %d)", skb, 6737 skb_queue_len(&chan->srej_q)); 6738 6739 err = l2cap_rx_queued_iframes(chan); 6740 if (err) 6741 break; 6742 6743 break; 6744 case L2CAP_TXSEQ_UNEXPECTED: 6745 /* Got a frame that can't be reassembled yet. 6746 * Save it for later, and send SREJs to cover 6747 * the missing frames. 6748 */ 6749 skb_queue_tail(&chan->srej_q, skb); 6750 skb_in_use = true; 6751 BT_DBG("Queued %p (queue len %d)", skb, 6752 skb_queue_len(&chan->srej_q)); 6753 6754 l2cap_pass_to_tx(chan, control); 6755 l2cap_send_srej(chan, control->txseq); 6756 break; 6757 case L2CAP_TXSEQ_UNEXPECTED_SREJ: 6758 /* This frame was requested with an SREJ, but 6759 * some expected retransmitted frames are 6760 * missing. Request retransmission of missing 6761 * SREJ'd frames. 6762 */ 6763 skb_queue_tail(&chan->srej_q, skb); 6764 skb_in_use = true; 6765 BT_DBG("Queued %p (queue len %d)", skb, 6766 skb_queue_len(&chan->srej_q)); 6767 6768 l2cap_pass_to_tx(chan, control); 6769 l2cap_send_srej_list(chan, control->txseq); 6770 break; 6771 case L2CAP_TXSEQ_DUPLICATE_SREJ: 6772 /* We've already queued this frame. Drop this copy. */ 6773 l2cap_pass_to_tx(chan, control); 6774 break; 6775 case L2CAP_TXSEQ_DUPLICATE: 6776 /* Expecting a later sequence number, so this frame 6777 * was already received. Ignore it completely. 6778 */ 6779 break; 6780 case L2CAP_TXSEQ_INVALID_IGNORE: 6781 break; 6782 case L2CAP_TXSEQ_INVALID: 6783 default: 6784 l2cap_send_disconn_req(chan, ECONNRESET); 6785 break; 6786 } 6787 break; 6788 case L2CAP_EV_RECV_RR: 6789 l2cap_pass_to_tx(chan, control); 6790 if (control->final) { 6791 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 6792 6793 if (!test_and_clear_bit(CONN_REJ_ACT, 6794 &chan->conn_state)) { 6795 control->final = 0; 6796 l2cap_retransmit_all(chan, control); 6797 } 6798 6799 l2cap_ertm_send(chan); 6800 } else if (control->poll) { 6801 if (test_and_clear_bit(CONN_REMOTE_BUSY, 6802 &chan->conn_state) && 6803 chan->unacked_frames) { 6804 __set_retrans_timer(chan); 6805 } 6806 6807 set_bit(CONN_SEND_FBIT, &chan->conn_state); 6808 l2cap_send_srej_tail(chan); 6809 } else { 6810 if (test_and_clear_bit(CONN_REMOTE_BUSY, 6811 &chan->conn_state) && 6812 chan->unacked_frames) 6813 __set_retrans_timer(chan); 6814 6815 l2cap_send_ack(chan); 6816 } 6817 break; 6818 case L2CAP_EV_RECV_RNR: 6819 set_bit(CONN_REMOTE_BUSY, &chan->conn_state); 6820 l2cap_pass_to_tx(chan, control); 6821 if (control->poll) { 6822 l2cap_send_srej_tail(chan); 6823 } else { 6824 struct l2cap_ctrl rr_control; 6825 memset(&rr_control, 0, sizeof(rr_control)); 6826 rr_control.sframe = 1; 6827 rr_control.super = L2CAP_SUPER_RR; 6828 rr_control.reqseq = chan->buffer_seq; 6829 l2cap_send_sframe(chan, &rr_control); 6830 } 6831 6832 break; 6833 case L2CAP_EV_RECV_REJ: 6834 l2cap_handle_rej(chan, control); 6835 break; 6836 case L2CAP_EV_RECV_SREJ: 6837 l2cap_handle_srej(chan, control); 6838 break; 6839 } 6840 6841 if (skb && !skb_in_use) { 6842 BT_DBG("Freeing %p", skb); 6843 kfree_skb(skb); 6844 } 6845 6846 return err; 6847 } 6848 6849 static int l2cap_finish_move(struct l2cap_chan *chan) 6850 { 6851 BT_DBG("chan %p", chan); 6852 6853 chan->rx_state = L2CAP_RX_STATE_RECV; 6854 chan->conn->mtu = chan->conn->hcon->mtu; 6855 6856 return l2cap_resegment(chan); 6857 } 6858 6859 static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, 6860 struct l2cap_ctrl *control, 6861 struct sk_buff *skb, u8 event) 6862 { 6863 int err; 6864 6865 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, 6866 event); 6867 6868 if (!control->poll) 6869 return -EPROTO; 6870 6871 l2cap_process_reqseq(chan, control->reqseq); 6872 6873 if (!skb_queue_empty(&chan->tx_q)) 6874 chan->tx_send_head = skb_peek(&chan->tx_q); 6875 else 6876 chan->tx_send_head = NULL; 6877 6878 /* Rewind next_tx_seq to the point expected 6879 * by the receiver. 6880 */ 6881 chan->next_tx_seq = control->reqseq; 6882 chan->unacked_frames = 0; 6883 6884 err = l2cap_finish_move(chan); 6885 if (err) 6886 return err; 6887 6888 set_bit(CONN_SEND_FBIT, &chan->conn_state); 6889 l2cap_send_i_or_rr_or_rnr(chan); 6890 6891 if (event == L2CAP_EV_RECV_IFRAME) 6892 return -EPROTO; 6893 6894 return l2cap_rx_state_recv(chan, control, NULL, event); 6895 } 6896 6897 static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, 6898 struct l2cap_ctrl *control, 6899 struct sk_buff *skb, u8 event) 6900 { 6901 int err; 6902 6903 if (!control->final) 6904 return -EPROTO; 6905 6906 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 6907 6908 chan->rx_state = L2CAP_RX_STATE_RECV; 6909 l2cap_process_reqseq(chan, control->reqseq); 6910 6911 if (!skb_queue_empty(&chan->tx_q)) 6912 chan->tx_send_head = skb_peek(&chan->tx_q); 6913 else 6914 chan->tx_send_head = NULL; 6915 6916 /* Rewind next_tx_seq to the point expected 6917 * by the receiver. 6918 */ 6919 chan->next_tx_seq = control->reqseq; 6920 chan->unacked_frames = 0; 6921 chan->conn->mtu = chan->conn->hcon->mtu; 6922 6923 err = l2cap_resegment(chan); 6924 6925 if (!err) 6926 err = l2cap_rx_state_recv(chan, control, skb, event); 6927 6928 return err; 6929 } 6930 6931 static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) 6932 { 6933 /* Make sure reqseq is for a packet that has been sent but not acked */ 6934 u16 unacked; 6935 6936 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); 6937 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; 6938 } 6939 6940 static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, 6941 struct sk_buff *skb, u8 event) 6942 { 6943 int err = 0; 6944 6945 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, 6946 control, skb, event, chan->rx_state); 6947 6948 if (__valid_reqseq(chan, control->reqseq)) { 6949 switch (chan->rx_state) { 6950 case L2CAP_RX_STATE_RECV: 6951 err = l2cap_rx_state_recv(chan, control, skb, event); 6952 break; 6953 case L2CAP_RX_STATE_SREJ_SENT: 6954 err = l2cap_rx_state_srej_sent(chan, control, skb, 6955 event); 6956 break; 6957 case L2CAP_RX_STATE_WAIT_P: 6958 err = l2cap_rx_state_wait_p(chan, control, skb, event); 6959 break; 6960 case L2CAP_RX_STATE_WAIT_F: 6961 err = l2cap_rx_state_wait_f(chan, control, skb, event); 6962 break; 6963 default: 6964 /* shut it down */ 6965 break; 6966 } 6967 } else { 6968 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", 6969 control->reqseq, chan->next_tx_seq, 6970 chan->expected_ack_seq); 6971 l2cap_send_disconn_req(chan, ECONNRESET); 6972 } 6973 6974 return err; 6975 } 6976 6977 static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, 6978 struct sk_buff *skb) 6979 { 6980 /* l2cap_reassemble_sdu may free skb, hence invalidate control, so store 6981 * the txseq field in advance to use it after l2cap_reassemble_sdu 6982 * returns and to avoid the race condition, for example: 6983 * 6984 * The current thread calls: 6985 * l2cap_reassemble_sdu 6986 * chan->ops->recv == l2cap_sock_recv_cb 6987 * __sock_queue_rcv_skb 6988 * Another thread calls: 6989 * bt_sock_recvmsg 6990 * skb_recv_datagram 6991 * skb_free_datagram 6992 * Then the current thread tries to access control, but it was freed by 6993 * skb_free_datagram. 6994 */ 6995 u16 txseq = control->txseq; 6996 6997 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, 6998 chan->rx_state); 6999 7000 if (l2cap_classify_txseq(chan, txseq) == L2CAP_TXSEQ_EXPECTED) { 7001 l2cap_pass_to_tx(chan, control); 7002 7003 BT_DBG("buffer_seq %u->%u", chan->buffer_seq, 7004 __next_seq(chan, chan->buffer_seq)); 7005 7006 chan->buffer_seq = __next_seq(chan, chan->buffer_seq); 7007 7008 l2cap_reassemble_sdu(chan, skb, control); 7009 } else { 7010 if (chan->sdu) { 7011 kfree_skb(chan->sdu); 7012 chan->sdu = NULL; 7013 } 7014 chan->sdu_last_frag = NULL; 7015 chan->sdu_len = 0; 7016 7017 if (skb) { 7018 BT_DBG("Freeing %p", skb); 7019 kfree_skb(skb); 7020 } 7021 } 7022 7023 chan->last_acked_seq = txseq; 7024 chan->expected_tx_seq = __next_seq(chan, txseq); 7025 7026 return 0; 7027 } 7028 7029 static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) 7030 { 7031 struct l2cap_ctrl *control = &bt_cb(skb)->l2cap; 7032 u16 len, min_len; 7033 u8 event; 7034 7035 min_len = test_bit(FLAG_EXT_CTRL, &chan->flags) ? 7036 L2CAP_EXT_CTRL_SIZE : L2CAP_ENH_CTRL_SIZE; 7037 if (chan->fcs == L2CAP_FCS_CRC16) 7038 min_len += L2CAP_FCS_SIZE; 7039 7040 if (skb->len < min_len) 7041 goto drop; 7042 7043 __unpack_control(chan, skb); 7044 7045 len = skb->len; 7046 7047 /* 7048 * We can just drop the corrupted I-frame here. 7049 * Receiver will miss it and start proper recovery 7050 * procedures and ask for retransmission. 7051 */ 7052 if (l2cap_check_fcs(chan, skb)) 7053 goto drop; 7054 7055 if (!control->sframe && control->sar == L2CAP_SAR_START) 7056 len -= L2CAP_SDULEN_SIZE; 7057 7058 if (chan->fcs == L2CAP_FCS_CRC16) 7059 len -= L2CAP_FCS_SIZE; 7060 7061 if (len > chan->mps) { 7062 l2cap_send_disconn_req(chan, ECONNRESET); 7063 goto drop; 7064 } 7065 7066 if (chan->ops->filter) { 7067 if (chan->ops->filter(chan, skb)) 7068 goto drop; 7069 } 7070 7071 if (!control->sframe) { 7072 int err; 7073 7074 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", 7075 control->sar, control->reqseq, control->final, 7076 control->txseq); 7077 7078 /* Validate F-bit - F=0 always valid, F=1 only 7079 * valid in TX WAIT_F 7080 */ 7081 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) 7082 goto drop; 7083 7084 if (chan->mode != L2CAP_MODE_STREAMING) { 7085 event = L2CAP_EV_RECV_IFRAME; 7086 err = l2cap_rx(chan, control, skb, event); 7087 } else { 7088 err = l2cap_stream_rx(chan, control, skb); 7089 } 7090 7091 if (err) 7092 l2cap_send_disconn_req(chan, ECONNRESET); 7093 } else { 7094 const u8 rx_func_to_event[4] = { 7095 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, 7096 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ 7097 }; 7098 7099 /* Only I-frames are expected in streaming mode */ 7100 if (chan->mode == L2CAP_MODE_STREAMING) 7101 goto drop; 7102 7103 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", 7104 control->reqseq, control->final, control->poll, 7105 control->super); 7106 7107 if (len != 0) { 7108 BT_ERR("Trailing bytes: %d in sframe", len); 7109 l2cap_send_disconn_req(chan, ECONNRESET); 7110 goto drop; 7111 } 7112 7113 /* Validate F and P bits */ 7114 if (control->final && (control->poll || 7115 chan->tx_state != L2CAP_TX_STATE_WAIT_F)) 7116 goto drop; 7117 7118 event = rx_func_to_event[control->super]; 7119 if (l2cap_rx(chan, control, skb, event)) 7120 l2cap_send_disconn_req(chan, ECONNRESET); 7121 } 7122 7123 return 0; 7124 7125 drop: 7126 kfree_skb(skb); 7127 return 0; 7128 } 7129 7130 static void l2cap_chan_le_send_credits(struct l2cap_chan *chan) 7131 { 7132 struct l2cap_conn *conn = chan->conn; 7133 struct l2cap_le_credits pkt; 7134 u16 return_credits = l2cap_le_rx_credits(chan); 7135 int ident; 7136 7137 if (chan->mode != L2CAP_MODE_LE_FLOWCTL && 7138 chan->mode != L2CAP_MODE_EXT_FLOWCTL) 7139 return; 7140 7141 if (chan->rx_credits >= return_credits) 7142 return; 7143 7144 return_credits -= chan->rx_credits; 7145 7146 BT_DBG("chan %p returning %u credits to sender", chan, return_credits); 7147 7148 chan->rx_credits += return_credits; 7149 7150 pkt.cid = cpu_to_le16(chan->scid); 7151 pkt.credits = cpu_to_le16(return_credits); 7152 7153 /* Keeps l2cap_put_ident() from releasing ident before we do */ 7154 lockdep_assert_held(&conn->lock); 7155 7156 ident = l2cap_get_ident(conn); 7157 7158 l2cap_send_cmd(conn, ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt); 7159 7160 /* L2CAP_LE_CREDITS has no response so the ident is never released by 7161 * l2cap_put_ident() - release it right away, otherwise the tx_ida 7162 * range is exhausted after 254 packets and from then on credits are 7163 * sent with the invalid ident 0, which some remote stacks ignore, 7164 * stalling the channel. 7165 */ 7166 if (ident > 0) 7167 ida_free(&conn->tx_ida, ident); 7168 } 7169 7170 void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail) 7171 { 7172 if (chan->rx_avail == rx_avail) 7173 return; 7174 7175 BT_DBG("chan %p has %zd bytes avail for rx", chan, rx_avail); 7176 7177 chan->rx_avail = rx_avail; 7178 7179 if (chan->state == BT_CONNECTED) 7180 l2cap_chan_le_send_credits(chan); 7181 } 7182 7183 static int l2cap_ecred_recv(struct l2cap_chan *chan, struct sk_buff *skb) 7184 { 7185 int err; 7186 7187 BT_DBG("SDU reassemble complete: chan %p skb->len %u", chan, skb->len); 7188 7189 /* Wait recv to confirm reception before updating the credits */ 7190 err = chan->ops->recv(chan, skb); 7191 7192 if (err < 0 && chan->rx_avail != -1) { 7193 BT_ERR("Queueing received LE L2CAP data failed"); 7194 l2cap_send_disconn_req(chan, ECONNRESET); 7195 return err; 7196 } 7197 7198 /* Update credits whenever an SDU is received */ 7199 l2cap_chan_le_send_credits(chan); 7200 7201 return err; 7202 } 7203 7204 static int l2cap_ecred_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) 7205 { 7206 int err; 7207 7208 if (!chan->rx_credits) { 7209 BT_ERR("No credits to receive LE L2CAP data"); 7210 l2cap_send_disconn_req(chan, ECONNRESET); 7211 return -ENOBUFS; 7212 } 7213 7214 if (skb->len > chan->imtu) { 7215 BT_ERR("Too big LE L2CAP PDU: len %u > %u", skb->len, 7216 chan->imtu); 7217 l2cap_send_disconn_req(chan, ECONNRESET); 7218 return -ENOBUFS; 7219 } 7220 7221 if (skb->len > chan->mps) { 7222 BT_ERR("Too big LE L2CAP MPS: len %u > %u", skb->len, 7223 chan->mps); 7224 l2cap_send_disconn_req(chan, ECONNRESET); 7225 return -ENOBUFS; 7226 } 7227 7228 chan->rx_credits--; 7229 BT_DBG("chan %p: rx_credits %u -> %u", 7230 chan, chan->rx_credits + 1, chan->rx_credits); 7231 7232 /* Update if remote had run out of credits, this should only happens 7233 * if the remote is not using the entire MPS. 7234 */ 7235 if (!chan->rx_credits) 7236 l2cap_chan_le_send_credits(chan); 7237 7238 err = 0; 7239 7240 if (!chan->sdu) { 7241 u16 sdu_len; 7242 7243 if (!pskb_may_pull(skb, L2CAP_SDULEN_SIZE)) { 7244 err = -EINVAL; 7245 goto failed; 7246 } 7247 7248 sdu_len = get_unaligned_le16(skb->data); 7249 skb_pull(skb, L2CAP_SDULEN_SIZE); 7250 7251 BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u", 7252 sdu_len, skb->len, chan->imtu); 7253 7254 if (sdu_len > chan->imtu) { 7255 BT_ERR("Too big LE L2CAP SDU length: len %u > %u", 7256 sdu_len, chan->imtu); 7257 l2cap_send_disconn_req(chan, ECONNRESET); 7258 err = -EMSGSIZE; 7259 goto failed; 7260 } 7261 7262 if (skb->len > sdu_len) { 7263 BT_ERR("Too much LE L2CAP data received"); 7264 err = -EINVAL; 7265 goto failed; 7266 } 7267 7268 if (skb->len == sdu_len) 7269 return l2cap_ecred_recv(chan, skb); 7270 7271 chan->sdu = skb; 7272 chan->sdu_len = sdu_len; 7273 chan->sdu_last_frag = skb; 7274 7275 /* Detect if remote is not able to use the selected MPS */ 7276 if (skb->len + L2CAP_SDULEN_SIZE < chan->mps) { 7277 u16 mps_len = skb->len + L2CAP_SDULEN_SIZE; 7278 7279 /* Adjust the number of credits */ 7280 BT_DBG("chan->mps %u -> %u", chan->mps, mps_len); 7281 chan->mps = mps_len; 7282 l2cap_chan_le_send_credits(chan); 7283 } 7284 7285 return 0; 7286 } 7287 7288 BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u", 7289 chan->sdu->len, skb->len, chan->sdu_len); 7290 7291 if (chan->sdu->len + skb->len > chan->sdu_len) { 7292 BT_ERR("Too much LE L2CAP data received"); 7293 l2cap_send_disconn_req(chan, ECONNRESET); 7294 err = -EINVAL; 7295 goto failed; 7296 } 7297 7298 append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag); 7299 skb = NULL; 7300 7301 if (chan->sdu->len == chan->sdu_len) { 7302 struct sk_buff *sdu = chan->sdu; 7303 7304 /* The receive callback takes ownership of a reassembled SDU and 7305 * may free it, while l2cap_ecred_recv() goes on to update the 7306 * credits - which reads chan->sdu->len. Drop the reassembly 7307 * state before handing the SDU over, and release it here if the 7308 * callback refused it, so that it is still freed exactly once. 7309 * The reassembly state is already clear at that point, so 7310 * return instead of falling into the failed: block, whose 7311 * kfree_skb(chan->sdu) would have nothing left to free. 7312 */ 7313 chan->sdu = NULL; 7314 chan->sdu_last_frag = NULL; 7315 chan->sdu_len = 0; 7316 7317 err = l2cap_ecred_recv(chan, sdu); 7318 if (err) 7319 kfree_skb(sdu); 7320 7321 return 0; 7322 } 7323 7324 failed: 7325 if (err) { 7326 kfree_skb(skb); 7327 kfree_skb(chan->sdu); 7328 chan->sdu = NULL; 7329 chan->sdu_last_frag = NULL; 7330 chan->sdu_len = 0; 7331 } 7332 7333 /* We can't return an error here since we took care of the skb 7334 * freeing internally. An error return would cause the caller to 7335 * do a double-free of the skb. 7336 */ 7337 return 0; 7338 } 7339 7340 static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, 7341 struct sk_buff *skb) 7342 __must_hold(&conn->lock) 7343 { 7344 struct l2cap_chan *chan; 7345 7346 chan = l2cap_get_chan_by_scid(conn, cid); 7347 if (!chan) { 7348 BT_DBG("unknown cid 0x%4.4x", cid); 7349 /* Drop packet and return */ 7350 kfree_skb(skb); 7351 return; 7352 } 7353 7354 l2cap_chan_lock(chan); 7355 7356 lockdep_assert_held(&chan->conn->lock); 7357 7358 BT_DBG("chan %p, len %d", chan, skb->len); 7359 7360 /* If we receive data on a fixed channel before the info req/rsp 7361 * procedure is done simply assume that the channel is supported 7362 * and mark it as ready. 7363 */ 7364 if (chan->chan_type == L2CAP_CHAN_FIXED) 7365 l2cap_chan_ready(chan); 7366 7367 if (chan->state != BT_CONNECTED) 7368 goto drop; 7369 7370 switch (chan->mode) { 7371 case L2CAP_MODE_LE_FLOWCTL: 7372 case L2CAP_MODE_EXT_FLOWCTL: 7373 if (l2cap_ecred_data_rcv(chan, skb) < 0) 7374 goto drop; 7375 7376 goto done; 7377 7378 case L2CAP_MODE_BASIC: 7379 /* If socket recv buffers overflows we drop data here 7380 * which is *bad* because L2CAP has to be reliable. 7381 * But we don't have any other choice. L2CAP doesn't 7382 * provide flow control mechanism. */ 7383 7384 if (chan->imtu < skb->len) { 7385 BT_ERR("Dropping L2CAP data: receive buffer overflow"); 7386 goto drop; 7387 } 7388 7389 if (!chan->ops->recv(chan, skb)) 7390 goto done; 7391 break; 7392 7393 case L2CAP_MODE_ERTM: 7394 case L2CAP_MODE_STREAMING: 7395 l2cap_data_rcv(chan, skb); 7396 goto done; 7397 7398 default: 7399 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 7400 break; 7401 } 7402 7403 drop: 7404 kfree_skb(skb); 7405 7406 done: 7407 l2cap_chan_unlock(chan); 7408 l2cap_chan_put(chan); 7409 } 7410 7411 static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, 7412 struct sk_buff *skb) 7413 { 7414 struct hci_conn *hcon = conn->hcon; 7415 struct l2cap_chan *chan; 7416 7417 if (hcon->type != ACL_LINK) 7418 goto free_skb; 7419 7420 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst, 7421 ACL_LINK); 7422 if (!chan) 7423 goto free_skb; 7424 7425 BT_DBG("chan %p, len %d", chan, skb->len); 7426 7427 l2cap_chan_lock(chan); 7428 7429 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 7430 goto drop; 7431 7432 if (chan->imtu < skb->len) 7433 goto drop; 7434 7435 /* Store remote BD_ADDR and PSM for msg_name */ 7436 bacpy(&bt_cb(skb)->l2cap.bdaddr, &hcon->dst); 7437 bt_cb(skb)->l2cap.psm = psm; 7438 7439 if (!chan->ops->recv(chan, skb)) { 7440 l2cap_chan_unlock(chan); 7441 l2cap_chan_put(chan); 7442 return; 7443 } 7444 7445 drop: 7446 l2cap_chan_unlock(chan); 7447 l2cap_chan_put(chan); 7448 free_skb: 7449 kfree_skb(skb); 7450 } 7451 7452 static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb, 7453 bool blocked) 7454 __must_hold(&conn->lock) 7455 { 7456 struct l2cap_hdr *lh = (void *) skb->data; 7457 struct hci_conn *hcon = conn->hcon; 7458 u16 cid, len; 7459 __le16 psm; 7460 7461 /* Also hold data back while the encryption key size is read, as 7462 * the security of the link, e.g. for SMP over BR/EDR, depends on it. 7463 */ 7464 if (hcon->state != BT_CONNECTED || 7465 test_bit(HCI_CONN_ENC_KEY_SIZE_PEND, hcon->flags)) { 7466 BT_DBG("queueing pending rx skb"); 7467 skb_queue_tail(&conn->pending_rx, skb); 7468 return; 7469 } 7470 7471 skb_pull(skb, L2CAP_HDR_SIZE); 7472 cid = __le16_to_cpu(lh->cid); 7473 len = __le16_to_cpu(lh->len); 7474 7475 if (len != skb->len) { 7476 kfree_skb(skb); 7477 return; 7478 } 7479 7480 if (blocked) { 7481 kfree_skb(skb); 7482 return; 7483 } 7484 7485 BT_DBG("len %d, cid 0x%4.4x", len, cid); 7486 7487 switch (cid) { 7488 case L2CAP_CID_SIGNALING: 7489 l2cap_sig_channel(conn, skb); 7490 break; 7491 7492 case L2CAP_CID_CONN_LESS: 7493 if (skb->len < L2CAP_PSMLEN_SIZE) { 7494 kfree_skb(skb); 7495 break; 7496 } 7497 7498 psm = get_unaligned((__le16 *) skb->data); 7499 skb_pull(skb, L2CAP_PSMLEN_SIZE); 7500 l2cap_conless_channel(conn, psm, skb); 7501 break; 7502 7503 case L2CAP_CID_LE_SIGNALING: 7504 l2cap_le_sig_channel(conn, skb); 7505 break; 7506 7507 default: 7508 l2cap_data_channel(conn, cid, skb); 7509 break; 7510 } 7511 } 7512 7513 static void process_pending_rx(struct work_struct *work) 7514 { 7515 struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 7516 pending_rx_work); 7517 struct sk_buff *skb; 7518 7519 BT_DBG(""); 7520 7521 mutex_lock(&conn->lock); 7522 7523 /* l2cap_recv_frame() would queue it all again while the encryption 7524 * key size is read, l2cap_security_cfm() schedules this once known. 7525 */ 7526 while (!test_bit(HCI_CONN_ENC_KEY_SIZE_PEND, conn->hcon->flags) && 7527 (skb = skb_dequeue(&conn->pending_rx))) 7528 l2cap_recv_frame(conn, skb, conn->pending_rx_blocked); 7529 7530 mutex_unlock(&conn->lock); 7531 } 7532 7533 static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) 7534 __must_hold(&hcon->hdev->lock) 7535 { 7536 struct l2cap_conn *conn = hcon->l2cap_data; 7537 struct hci_chan *hchan; 7538 7539 if (conn) 7540 return conn; 7541 7542 hchan = hci_chan_create(hcon); 7543 if (!hchan) 7544 return NULL; 7545 7546 conn = kzalloc_obj(*conn); 7547 if (!conn) { 7548 hci_chan_del(hchan); 7549 return NULL; 7550 } 7551 7552 kref_init(&conn->ref); 7553 conn->hchan = hchan; 7554 7555 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); 7556 7557 conn->mtu = hcon->mtu; 7558 conn->feat_mask = 0; 7559 7560 conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS; 7561 7562 if (hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED) && 7563 (bredr_sc_enabled(hcon->hdev) || 7564 hci_dev_test_flag(hcon->hdev, HCI_FORCE_BREDR_SMP))) 7565 conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR; 7566 7567 scoped_guard(mutex_init, &conn->lock) { 7568 INIT_LIST_HEAD(&conn->chan_l); 7569 } 7570 INIT_LIST_HEAD(&conn->users); 7571 7572 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); 7573 ida_init(&conn->tx_ida); 7574 7575 skb_queue_head_init(&conn->pending_rx); 7576 INIT_WORK(&conn->pending_rx_work, process_pending_rx); 7577 INIT_DELAYED_WORK(&conn->id_addr_timer, l2cap_conn_update_id_addr); 7578 7579 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 7580 7581 spin_lock(&hcon->proto_lock); 7582 conn->hcon = hci_conn_get(hcon); 7583 hcon->l2cap_data = conn; 7584 spin_unlock(&hcon->proto_lock); 7585 7586 return conn; 7587 } 7588 7589 static bool is_valid_psm(u16 psm, u8 dst_type) 7590 { 7591 if (!psm) 7592 return false; 7593 7594 if (bdaddr_type_is_le(dst_type)) 7595 return (psm <= 0x00ff); 7596 7597 /* PSM must be odd and lsb of upper byte must be 0 */ 7598 return ((psm & 0x0101) == 0x0001); 7599 } 7600 7601 struct l2cap_chan_data { 7602 struct l2cap_chan *chan; 7603 struct pid *pid; 7604 int count; 7605 }; 7606 7607 static void l2cap_chan_by_pid(struct l2cap_chan *chan, void *data) 7608 { 7609 struct l2cap_chan_data *d = data; 7610 struct pid *pid; 7611 7612 if (chan == d->chan) 7613 return; 7614 7615 if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) 7616 return; 7617 7618 pid = chan->ops->get_peer_pid(chan); 7619 7620 /* Only count deferred channels with the same PID/PSM */ 7621 if (d->pid != pid || chan->psm != d->chan->psm || chan->ident || 7622 chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT) 7623 return; 7624 7625 d->count++; 7626 } 7627 7628 int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, 7629 bdaddr_t *dst, u8 dst_type, u16 timeout) 7630 { 7631 struct l2cap_conn *conn; 7632 struct hci_conn *hcon; 7633 struct hci_dev *hdev; 7634 int err; 7635 7636 BT_DBG("%pMR -> %pMR (type %u) psm 0x%4.4x mode 0x%2.2x", &chan->src, 7637 dst, dst_type, __le16_to_cpu(psm), chan->mode); 7638 7639 hdev = hci_get_route(dst, &chan->src, chan->src_type); 7640 if (!hdev) 7641 return -EHOSTUNREACH; 7642 7643 hci_dev_lock(hdev); 7644 7645 if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid && 7646 chan->chan_type != L2CAP_CHAN_RAW) { 7647 err = -EINVAL; 7648 goto done; 7649 } 7650 7651 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) { 7652 err = -EINVAL; 7653 goto done; 7654 } 7655 7656 if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) { 7657 err = -EINVAL; 7658 goto done; 7659 } 7660 7661 switch (chan->mode) { 7662 case L2CAP_MODE_BASIC: 7663 break; 7664 case L2CAP_MODE_LE_FLOWCTL: 7665 break; 7666 case L2CAP_MODE_EXT_FLOWCTL: 7667 if (!enable_ecred) { 7668 err = -EOPNOTSUPP; 7669 goto done; 7670 } 7671 break; 7672 case L2CAP_MODE_ERTM: 7673 case L2CAP_MODE_STREAMING: 7674 if (!disable_ertm) 7675 break; 7676 fallthrough; 7677 default: 7678 err = -EOPNOTSUPP; 7679 goto done; 7680 } 7681 7682 switch (chan->state) { 7683 case BT_CONNECT: 7684 case BT_CONNECT2: 7685 case BT_CONFIG: 7686 /* Already connecting */ 7687 err = 0; 7688 goto done; 7689 7690 case BT_CONNECTED: 7691 /* Already connected */ 7692 err = -EISCONN; 7693 goto done; 7694 7695 case BT_OPEN: 7696 case BT_BOUND: 7697 /* Can connect */ 7698 break; 7699 7700 default: 7701 err = -EBADFD; 7702 goto done; 7703 } 7704 7705 /* Set destination address and psm */ 7706 bacpy(&chan->dst, dst); 7707 chan->dst_type = dst_type; 7708 7709 chan->psm = psm; 7710 chan->dcid = cid; 7711 7712 if (bdaddr_type_is_le(dst_type)) { 7713 /* Convert from L2CAP channel address type to HCI address type 7714 */ 7715 if (dst_type == BDADDR_LE_PUBLIC) 7716 dst_type = ADDR_LE_DEV_PUBLIC; 7717 else 7718 dst_type = ADDR_LE_DEV_RANDOM; 7719 7720 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) 7721 hcon = hci_connect_le(hdev, dst, dst_type, false, 7722 chan->sec_level, timeout, 7723 HCI_ROLE_SLAVE, 0, 0); 7724 else 7725 hcon = hci_connect_le_scan(hdev, dst, dst_type, 7726 chan->sec_level, timeout, 7727 CONN_REASON_L2CAP_CHAN); 7728 7729 } else { 7730 u8 auth_type = l2cap_get_auth_type(chan); 7731 hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type, 7732 CONN_REASON_L2CAP_CHAN, timeout); 7733 } 7734 7735 if (IS_ERR(hcon)) { 7736 err = PTR_ERR(hcon); 7737 goto done; 7738 } 7739 7740 lockdep_assert_held(&hcon->hdev->lock); 7741 7742 conn = l2cap_conn_add(hcon); 7743 if (!conn) { 7744 hci_conn_drop(hcon); 7745 err = -ENOMEM; 7746 goto done; 7747 } 7748 7749 mutex_lock(&conn->lock); 7750 l2cap_chan_lock(chan); 7751 7752 if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { 7753 struct l2cap_chan_data data; 7754 7755 data.chan = chan; 7756 data.pid = chan->ops->get_peer_pid(chan); 7757 data.count = 1; 7758 7759 __l2cap_chan_list(conn, l2cap_chan_by_pid, &data); 7760 7761 /* Leave room for non-deferred channel that ends the group. */ 7762 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) 7763 data.count += 1; 7764 7765 /* Check if there isn't too many channels being connected */ 7766 if (data.count > L2CAP_ECRED_CONN_SCID_MAX) { 7767 hci_conn_drop(hcon); 7768 err = -EPROTO; 7769 goto chan_unlock; 7770 } 7771 } 7772 7773 if ((cid && __l2cap_get_chan_by_dcid(conn, cid)) || chan->conn || 7774 test_bit(FLAG_DEL, &chan->flags)) { 7775 hci_conn_drop(hcon); 7776 err = -EBUSY; 7777 goto chan_unlock; 7778 } 7779 7780 /* Update source addr of the socket */ 7781 bacpy(&chan->src, &hcon->src); 7782 chan->src_type = bdaddr_src_type(hcon); 7783 7784 __l2cap_chan_add(conn, chan); 7785 7786 lockdep_assert_held(&chan->conn->lock); 7787 7788 /* l2cap_chan_add takes its own ref so we can drop this one */ 7789 hci_conn_drop(hcon); 7790 7791 l2cap_state_change(chan, BT_CONNECT); 7792 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); 7793 7794 /* Release chan->sport so that it can be reused by other 7795 * sockets (as it's only used for listening sockets). 7796 */ 7797 write_lock(&chan_list_lock); 7798 chan->sport = 0; 7799 write_unlock(&chan_list_lock); 7800 7801 if (hcon->state == BT_CONNECTED) { 7802 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7803 __clear_chan_timer(chan); 7804 if (l2cap_chan_check_security(chan, true)) 7805 l2cap_state_change(chan, BT_CONNECTED); 7806 } else 7807 l2cap_do_start(chan); 7808 } 7809 7810 err = 0; 7811 7812 chan_unlock: 7813 l2cap_chan_unlock(chan); 7814 mutex_unlock(&conn->lock); 7815 done: 7816 hci_dev_unlock(hdev); 7817 hci_dev_put(hdev); 7818 return err; 7819 } 7820 EXPORT_SYMBOL_GPL(l2cap_chan_connect); 7821 7822 static void l2cap_ecred_reconfigure(struct l2cap_chan *chan) 7823 { 7824 struct l2cap_conn *conn = chan->conn; 7825 DEFINE_RAW_FLEX(struct l2cap_ecred_reconf_req, pdu, scid, 1); 7826 7827 pdu->mtu = cpu_to_le16(chan->imtu); 7828 pdu->mps = cpu_to_le16(chan->mps); 7829 pdu->scid[0] = cpu_to_le16(chan->scid); 7830 7831 chan->ident = l2cap_get_ident(conn); 7832 set_bit(FLAG_ECRED_RECONF_REQ_SENT, &chan->flags); 7833 7834 l2cap_send_cmd(conn, chan->ident, L2CAP_ECRED_RECONF_REQ, 7835 struct_size(pdu, scid, 1), pdu); 7836 } 7837 7838 int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu) 7839 { 7840 if (chan->imtu > mtu) 7841 return -EINVAL; 7842 7843 BT_DBG("chan %p mtu 0x%4.4x", chan, mtu); 7844 7845 chan->imtu = mtu; 7846 7847 l2cap_ecred_reconfigure(chan); 7848 7849 return 0; 7850 } 7851 7852 /* ---- L2CAP interface with lower layer (HCI) ---- */ 7853 7854 int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) 7855 { 7856 int exact = 0, lm1 = 0, lm2 = 0; 7857 struct l2cap_chan *c; 7858 7859 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); 7860 7861 /* Find listening sockets and check their link_mode */ 7862 read_lock(&chan_list_lock); 7863 list_for_each_entry(c, &chan_list, global_l) { 7864 if (c->state != BT_LISTEN) 7865 continue; 7866 7867 if (!bacmp(&c->src, &hdev->bdaddr)) { 7868 lm1 |= HCI_LM_ACCEPT; 7869 if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 7870 lm1 |= HCI_LM_MASTER; 7871 exact++; 7872 } else if (!bacmp(&c->src, BDADDR_ANY)) { 7873 lm2 |= HCI_LM_ACCEPT; 7874 if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 7875 lm2 |= HCI_LM_MASTER; 7876 } 7877 } 7878 read_unlock(&chan_list_lock); 7879 7880 return exact ? lm1 : lm2; 7881 } 7882 7883 /* Find the next fixed channel in BT_LISTEN state, continue iteration 7884 * from an existing channel in the list or from the beginning of the 7885 * global list (by passing NULL as first parameter). 7886 */ 7887 static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c, 7888 struct hci_conn *hcon) 7889 { 7890 u8 src_type = bdaddr_src_type(hcon); 7891 7892 read_lock(&chan_list_lock); 7893 7894 if (c) 7895 c = list_next_entry(c, global_l); 7896 else 7897 c = list_entry(chan_list.next, typeof(*c), global_l); 7898 7899 list_for_each_entry_from(c, &chan_list, global_l) { 7900 if (c->chan_type != L2CAP_CHAN_FIXED) 7901 continue; 7902 if (c->state != BT_LISTEN) 7903 continue; 7904 if (bacmp(&c->src, &hcon->src) && bacmp(&c->src, BDADDR_ANY)) 7905 continue; 7906 if (src_type != c->src_type) 7907 continue; 7908 7909 c = l2cap_chan_hold_unless_zero(c); 7910 read_unlock(&chan_list_lock); 7911 return c; 7912 } 7913 7914 read_unlock(&chan_list_lock); 7915 7916 return NULL; 7917 } 7918 7919 static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 7920 __must_hold(&hcon->hdev->lock) 7921 { 7922 struct hci_dev *hdev = hcon->hdev; 7923 struct l2cap_conn *conn; 7924 struct l2cap_chan *pchan; 7925 u8 dst_type; 7926 7927 if (hcon->type != ACL_LINK && hcon->type != LE_LINK) 7928 return; 7929 7930 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); 7931 7932 if (status) { 7933 l2cap_conn_del(hcon, bt_to_errno(status)); 7934 return; 7935 } 7936 7937 conn = l2cap_conn_add(hcon); 7938 if (!conn) 7939 return; 7940 7941 dst_type = bdaddr_dst_type(hcon); 7942 7943 /* If device is blocked, do not create channels for it */ 7944 if (hci_bdaddr_list_lookup(&hdev->reject_list, &hcon->dst, dst_type)) 7945 return; 7946 7947 /* Find fixed channels and notify them of the new connection. We 7948 * use multiple individual lookups, continuing each time where 7949 * we left off, because the list lock would prevent calling the 7950 * potentially sleeping l2cap_chan_lock() function. 7951 */ 7952 mutex_lock(&conn->lock); 7953 7954 pchan = l2cap_global_fixed_chan(NULL, hcon); 7955 while (pchan) { 7956 struct l2cap_chan *chan, *next; 7957 7958 /* Client fixed channels should override server ones */ 7959 if (__l2cap_get_chan_by_dcid(conn, pchan->scid)) 7960 goto next; 7961 7962 l2cap_chan_lock(pchan); 7963 chan = l2cap_new_connection(conn, pchan); 7964 if (chan) { 7965 bacpy(&chan->src, &hcon->src); 7966 bacpy(&chan->dst, &hcon->dst); 7967 chan->src_type = bdaddr_src_type(hcon); 7968 chan->dst_type = dst_type; 7969 } 7970 7971 l2cap_chan_unlock(pchan); 7972 next: 7973 next = l2cap_global_fixed_chan(pchan, hcon); 7974 l2cap_chan_put(pchan); 7975 pchan = next; 7976 } 7977 7978 mutex_unlock(&conn->lock); 7979 7980 l2cap_conn_ready(hcon); 7981 } 7982 7983 int l2cap_disconn_ind(struct hci_conn *hcon) 7984 { 7985 struct l2cap_conn *conn; 7986 int ret = HCI_ERROR_REMOTE_USER_TERM; 7987 7988 BT_DBG("hcon %p", hcon); 7989 7990 spin_lock(&hcon->proto_lock); 7991 conn = hcon->l2cap_data; 7992 if (conn) 7993 ret = conn->disc_reason; 7994 spin_unlock(&hcon->proto_lock); 7995 7996 return ret; 7997 } 7998 7999 static void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 8000 __must_hold(&hcon->hdev->lock) 8001 { 8002 if (hcon->type != ACL_LINK && hcon->type != LE_LINK) 8003 return; 8004 8005 BT_DBG("hcon %p reason %d", hcon, reason); 8006 8007 l2cap_conn_del(hcon, bt_to_errno(reason)); 8008 } 8009 8010 static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 8011 __must_hold(&chan->lock) 8012 __must_hold(&chan->conn->lock) 8013 { 8014 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 8015 return; 8016 8017 if (encrypt == 0x00) { 8018 if (chan->sec_level == BT_SECURITY_MEDIUM) { 8019 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); 8020 } else if (chan->sec_level == BT_SECURITY_HIGH || 8021 chan->sec_level == BT_SECURITY_FIPS) 8022 __l2cap_chan_close(chan, ECONNREFUSED); 8023 } else { 8024 if (chan->sec_level == BT_SECURITY_MEDIUM) 8025 __clear_chan_timer(chan); 8026 } 8027 } 8028 8029 static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 8030 __must_hold(&hcon->hdev->lock) 8031 { 8032 struct l2cap_conn *conn = hcon->l2cap_data; 8033 struct l2cap_chan *chan; 8034 8035 if (!conn) 8036 return; 8037 8038 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt); 8039 8040 mutex_lock(&conn->lock); 8041 8042 list_for_each_entry(chan, &conn->chan_l, list) { 8043 l2cap_chan_lock(chan); 8044 8045 lockdep_assert_held(&chan->conn->lock); 8046 8047 BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, 8048 state_to_string(chan->state)); 8049 8050 if (!status && encrypt) 8051 chan->sec_level = hcon->sec_level; 8052 8053 if (!__l2cap_no_conn_pending(chan)) { 8054 l2cap_chan_unlock(chan); 8055 continue; 8056 } 8057 8058 if (!status && (chan->state == BT_CONNECTED || 8059 chan->state == BT_CONFIG)) { 8060 chan->ops->resume(chan); 8061 l2cap_check_encryption(chan, encrypt); 8062 l2cap_chan_unlock(chan); 8063 continue; 8064 } 8065 8066 if (chan->state == BT_CONNECT) { 8067 if (!status && l2cap_check_enc_key_size(hcon, chan)) 8068 l2cap_start_connection(chan); 8069 else 8070 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 8071 } else if (chan->state == BT_CONNECT2 && 8072 !(chan->mode == L2CAP_MODE_EXT_FLOWCTL || 8073 chan->mode == L2CAP_MODE_LE_FLOWCTL)) { 8074 struct l2cap_conn_rsp rsp; 8075 __u16 res, stat; 8076 8077 if (!status && l2cap_check_enc_key_size(hcon, chan)) { 8078 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { 8079 res = L2CAP_CR_PEND; 8080 stat = L2CAP_CS_AUTHOR_PEND; 8081 chan->ops->defer(chan); 8082 } else { 8083 l2cap_state_change(chan, BT_CONFIG); 8084 res = L2CAP_CR_SUCCESS; 8085 stat = L2CAP_CS_NO_INFO; 8086 } 8087 } else { 8088 l2cap_state_change(chan, BT_DISCONN); 8089 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 8090 res = L2CAP_CR_SEC_BLOCK; 8091 stat = L2CAP_CS_NO_INFO; 8092 } 8093 8094 rsp.scid = cpu_to_le16(chan->dcid); 8095 rsp.dcid = cpu_to_le16(chan->scid); 8096 rsp.result = cpu_to_le16(res); 8097 rsp.status = cpu_to_le16(stat); 8098 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 8099 sizeof(rsp), &rsp); 8100 8101 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && 8102 res == L2CAP_CR_SUCCESS) { 8103 char buf[128]; 8104 set_bit(CONF_REQ_SENT, &chan->conf_state); 8105 l2cap_send_cmd(conn, l2cap_get_ident(conn), 8106 L2CAP_CONF_REQ, 8107 l2cap_build_conf_req(chan, buf, sizeof(buf)), 8108 buf); 8109 chan->num_conf_req++; 8110 } 8111 } 8112 8113 l2cap_chan_unlock(chan); 8114 } 8115 8116 mutex_unlock(&conn->lock); 8117 8118 /* Process data held back while the encryption key size was read */ 8119 l2cap_queue_pending_rx(hcon); 8120 } 8121 8122 /* Append fragment into frame respecting the maximum len of rx_skb */ 8123 static int l2cap_recv_frag(struct l2cap_conn *conn, struct sk_buff *skb, 8124 u16 len) 8125 { 8126 if (!conn->rx_skb) { 8127 /* Allocate skb for the complete frame (with header) */ 8128 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); 8129 if (!conn->rx_skb) 8130 return -ENOMEM; 8131 /* Init rx_len */ 8132 conn->rx_len = len; 8133 8134 skb_set_delivery_time(conn->rx_skb, skb->tstamp, 8135 skb->tstamp_type); 8136 } 8137 8138 /* Copy as much as the rx_skb can hold */ 8139 len = min_t(u16, len, skb->len); 8140 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, len), len); 8141 skb_pull(skb, len); 8142 conn->rx_len -= len; 8143 8144 return len; 8145 } 8146 8147 static int l2cap_recv_len(struct l2cap_conn *conn, struct sk_buff *skb) 8148 { 8149 struct sk_buff *rx_skb; 8150 int len; 8151 8152 /* Append just enough to complete the header */ 8153 len = l2cap_recv_frag(conn, skb, L2CAP_LEN_SIZE - conn->rx_skb->len); 8154 8155 /* If header could not be read just continue */ 8156 if (len < 0 || conn->rx_skb->len < L2CAP_LEN_SIZE) 8157 return len; 8158 8159 rx_skb = conn->rx_skb; 8160 len = get_unaligned_le16(rx_skb->data); 8161 8162 /* Check if rx_skb has enough space to received all fragments */ 8163 if (len + (L2CAP_HDR_SIZE - L2CAP_LEN_SIZE) <= skb_tailroom(rx_skb)) { 8164 /* Update expected len */ 8165 conn->rx_len = len + (L2CAP_HDR_SIZE - L2CAP_LEN_SIZE); 8166 return L2CAP_LEN_SIZE; 8167 } 8168 8169 /* Reset conn->rx_skb since it will need to be reallocated in order to 8170 * fit all fragments. 8171 */ 8172 conn->rx_skb = NULL; 8173 8174 /* Reallocates rx_skb using the exact expected length */ 8175 len = l2cap_recv_frag(conn, rx_skb, 8176 len + (L2CAP_HDR_SIZE - L2CAP_LEN_SIZE)); 8177 kfree_skb(rx_skb); 8178 8179 return len; 8180 } 8181 8182 static void l2cap_recv_reset(struct l2cap_conn *conn) 8183 { 8184 kfree_skb(conn->rx_skb); 8185 conn->rx_skb = NULL; 8186 conn->rx_len = 0; 8187 } 8188 8189 struct l2cap_conn *l2cap_conn_hold_unless_zero(struct l2cap_conn *c) 8190 { 8191 if (!c) 8192 return NULL; 8193 8194 BT_DBG("conn %p orig refcnt %u", c, kref_read(&c->ref)); 8195 8196 if (!kref_get_unless_zero(&c->ref)) 8197 return NULL; 8198 8199 return c; 8200 } 8201 EXPORT_SYMBOL(l2cap_conn_hold_unless_zero); 8202 8203 int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, 8204 struct sk_buff *skb, u16 flags) 8205 { 8206 struct hci_conn *hcon; 8207 struct l2cap_conn *conn; 8208 bool blocked; 8209 int len; 8210 8211 /* Lock hdev for hci_conn, and race on l2cap_data vs. l2cap_conn_del */ 8212 hci_dev_lock(hdev); 8213 8214 hcon = hci_conn_hash_lookup_handle(hdev, handle); 8215 if (!hcon) { 8216 hci_dev_unlock(hdev); 8217 kfree_skb(skb); 8218 return -ENOENT; 8219 } 8220 8221 lockdep_assert_held(&hcon->hdev->lock); 8222 8223 hci_conn_enter_active_mode(hcon, BT_POWER_FORCE_ACTIVE_OFF); 8224 8225 conn = hcon->l2cap_data; 8226 8227 if (!conn) 8228 conn = l2cap_conn_add(hcon); 8229 8230 conn = l2cap_conn_hold_unless_zero(conn); 8231 blocked = l2cap_is_blocked(hcon); 8232 hcon = NULL; 8233 8234 hci_dev_unlock(hdev); 8235 8236 if (!conn) { 8237 kfree_skb(skb); 8238 return -EINVAL; 8239 } 8240 8241 BT_DBG("conn %p len %u flags 0x%x", conn, skb->len, flags); 8242 8243 mutex_lock(&conn->lock); 8244 8245 switch (flags) { 8246 case ACL_START: 8247 case ACL_START_NO_FLUSH: 8248 case ACL_COMPLETE: 8249 if (conn->rx_skb) { 8250 BT_ERR("Unexpected start frame (len %d)", skb->len); 8251 l2cap_recv_reset(conn); 8252 l2cap_conn_unreliable(conn, ECOMM); 8253 } 8254 8255 /* Start fragment may not contain the L2CAP length so just 8256 * copy the initial byte when that happens and use conn->mtu as 8257 * expected length. 8258 */ 8259 if (skb->len < L2CAP_LEN_SIZE) { 8260 l2cap_recv_frag(conn, skb, conn->mtu); 8261 break; 8262 } 8263 8264 len = get_unaligned_le16(skb->data) + L2CAP_HDR_SIZE; 8265 8266 if (len == skb->len) { 8267 /* Complete frame received */ 8268 l2cap_recv_frame(conn, skb, blocked); 8269 goto unlock; 8270 } 8271 8272 BT_DBG("Start: total len %d, frag len %u", len, skb->len); 8273 8274 if (skb->len > len) { 8275 BT_ERR("Frame is too long (len %u, expected len %d)", 8276 skb->len, len); 8277 /* PTS test cases L2CAP/COS/CED/BI-14-C and BI-15-C 8278 * (Multiple Signaling Command in one PDU, Data 8279 * Truncated, BR/EDR) send a C-frame to the IUT with 8280 * PDU Length set to 8 and Channel ID set to the 8281 * correct signaling channel for the logical link. 8282 * The Information payload contains one L2CAP_ECHO_REQ 8283 * packet with Data Length set to 0 with 0 octets of 8284 * echo data and one invalid command packet due to 8285 * data truncated in PDU but present in HCI packet. 8286 * 8287 * Shorter the socket buffer to the PDU length to 8288 * allow to process valid commands from the PDU before 8289 * setting the socket unreliable. 8290 */ 8291 skb->len = len; 8292 l2cap_recv_frame(conn, skb, blocked); 8293 l2cap_conn_unreliable(conn, ECOMM); 8294 goto unlock; 8295 } 8296 8297 /* Append fragment into frame (with header) */ 8298 if (l2cap_recv_frag(conn, skb, len) < 0) 8299 goto drop; 8300 8301 break; 8302 8303 case ACL_CONT: 8304 BT_DBG("Cont: frag len %u (expecting %u)", skb->len, conn->rx_len); 8305 8306 if (!conn->rx_skb) { 8307 BT_ERR("Unexpected continuation frame (len %d)", skb->len); 8308 l2cap_conn_unreliable(conn, ECOMM); 8309 goto drop; 8310 } 8311 8312 /* Complete the L2CAP length if it has not been read */ 8313 if (conn->rx_skb->len < L2CAP_LEN_SIZE) { 8314 if (l2cap_recv_len(conn, skb) < 0) { 8315 l2cap_conn_unreliable(conn, ECOMM); 8316 goto drop; 8317 } 8318 8319 /* Header still could not be read just continue */ 8320 if (conn->rx_skb->len < L2CAP_LEN_SIZE) 8321 break; 8322 } 8323 8324 if (skb->len > conn->rx_len) { 8325 BT_ERR("Fragment is too long (len %u, expected %u)", 8326 skb->len, conn->rx_len); 8327 l2cap_recv_reset(conn); 8328 l2cap_conn_unreliable(conn, ECOMM); 8329 goto drop; 8330 } 8331 8332 /* Append fragment into frame (with header) */ 8333 l2cap_recv_frag(conn, skb, skb->len); 8334 8335 if (!conn->rx_len) { 8336 /* Complete frame received. l2cap_recv_frame 8337 * takes ownership of the skb so set the global 8338 * rx_skb pointer to NULL first. 8339 */ 8340 struct sk_buff *rx_skb = conn->rx_skb; 8341 conn->rx_skb = NULL; 8342 l2cap_recv_frame(conn, rx_skb, blocked); 8343 } 8344 break; 8345 } 8346 8347 drop: 8348 kfree_skb(skb); 8349 unlock: 8350 mutex_unlock(&conn->lock); 8351 l2cap_conn_put(conn); 8352 return 0; 8353 } 8354 8355 static struct hci_cb l2cap_cb = { 8356 .name = "L2CAP", 8357 .connect_cfm = l2cap_connect_cfm, 8358 .disconn_cfm = l2cap_disconn_cfm, 8359 .security_cfm = l2cap_security_cfm, 8360 }; 8361 8362 static int l2cap_debugfs_show(struct seq_file *f, void *p) 8363 { 8364 struct l2cap_chan *c; 8365 8366 read_lock(&chan_list_lock); 8367 8368 list_for_each_entry(c, &chan_list, global_l) { 8369 seq_printf(f, "%pMR (%u) %pMR (%u) %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 8370 &c->src, c->src_type, &c->dst, c->dst_type, 8371 c->state, __le16_to_cpu(c->psm), 8372 c->scid, c->dcid, c->imtu, c->omtu, 8373 c->sec_level, c->mode); 8374 } 8375 8376 read_unlock(&chan_list_lock); 8377 8378 return 0; 8379 } 8380 8381 DEFINE_SHOW_ATTRIBUTE(l2cap_debugfs); 8382 8383 static struct dentry *l2cap_debugfs; 8384 8385 int __init l2cap_init(void) 8386 { 8387 int err; 8388 8389 err = l2cap_init_sockets(); 8390 if (err < 0) 8391 return err; 8392 8393 hci_register_cb(&l2cap_cb); 8394 8395 if (IS_ERR_OR_NULL(bt_debugfs)) 8396 return 0; 8397 8398 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, 8399 NULL, &l2cap_debugfs_fops); 8400 8401 return 0; 8402 } 8403 8404 void l2cap_exit(void) 8405 { 8406 debugfs_remove(l2cap_debugfs); 8407 hci_unregister_cb(&l2cap_cb); 8408 l2cap_cleanup_sockets(); 8409 } 8410 8411 module_param(disable_ertm, bool, 0644); 8412 MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 8413 8414 module_param(enable_ecred, bool, 0644); 8415 MODULE_PARM_DESC(enable_ecred, "Enable enhanced credit flow control mode"); 8416