2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
5 Copyright (C) 2010 Google Inc.
6 Copyright (C) 2011 ProFUSION Embedded Systems
7 Copyright (c) 2012 Code Aurora Forum. All rights reserved.
9 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License version 2 as
13 published by the Free Software Foundation;
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
18 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
19 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
25 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
26 SOFTWARE IS DISCLAIMED.
29 /* Bluetooth L2CAP core. */
31 #include <linux/module.h>
33 #include <linux/debugfs.h>
34 #include <linux/crc16.h>
35 #include <linux/filter.h>
37 #include <net/bluetooth/bluetooth.h>
38 #include <net/bluetooth/hci_core.h>
39 #include <net/bluetooth/l2cap.h>
45 #define LE_FLOWCTL_MAX_CREDITS 65535
50 static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD;
52 static LIST_HEAD(chan_list);
53 static DEFINE_RWLOCK(chan_list_lock);
55 static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
56 u8 code, u8 ident, u16 dlen, void *data);
57 static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
59 static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data_size);
60 static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err);
62 static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
63 struct sk_buff_head *skbs, u8 event);
64 static void l2cap_retrans_timeout(struct work_struct *work);
65 static void l2cap_monitor_timeout(struct work_struct *work);
66 static void l2cap_ack_timeout(struct work_struct *work);
68 static inline u8 bdaddr_type(u8 link_type, u8 bdaddr_type)
70 if (link_type == LE_LINK) {
71 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
72 return BDADDR_LE_PUBLIC;
74 return BDADDR_LE_RANDOM;
80 static inline u8 bdaddr_src_type(struct hci_conn *hcon)
82 return bdaddr_type(hcon->type, hcon->src_type);
85 static inline u8 bdaddr_dst_type(struct hci_conn *hcon)
87 return bdaddr_type(hcon->type, hcon->dst_type);
90 /* ---- L2CAP channels ---- */
92 static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
97 list_for_each_entry(c, &conn->chan_l, list) {
104 static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn,
107 struct l2cap_chan *c;
109 list_for_each_entry(c, &conn->chan_l, list) {
116 /* Find channel with given SCID.
117 * Returns a reference locked channel.
119 static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
122 struct l2cap_chan *c;
124 mutex_lock(&conn->chan_lock);
125 c = __l2cap_get_chan_by_scid(conn, cid);
127 /* Only lock if chan reference is not 0 */
128 c = l2cap_chan_hold_unless_zero(c);
132 mutex_unlock(&conn->chan_lock);
137 /* Find channel with given DCID.
138 * Returns a reference locked channel.
140 static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
143 struct l2cap_chan *c;
145 mutex_lock(&conn->chan_lock);
146 c = __l2cap_get_chan_by_dcid(conn, cid);
148 /* Only lock if chan reference is not 0 */
149 c = l2cap_chan_hold_unless_zero(c);
153 mutex_unlock(&conn->chan_lock);
158 static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn,
161 struct l2cap_chan *c;
163 list_for_each_entry(c, &conn->chan_l, list) {
164 if (c->ident == ident)
170 static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn,
173 struct l2cap_chan *c;
175 mutex_lock(&conn->chan_lock);
176 c = __l2cap_get_chan_by_ident(conn, ident);
178 /* Only lock if chan reference is not 0 */
179 c = l2cap_chan_hold_unless_zero(c);
183 mutex_unlock(&conn->chan_lock);
188 static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src,
191 struct l2cap_chan *c;
193 list_for_each_entry(c, &chan_list, global_l) {
194 if (src_type == BDADDR_BREDR && c->src_type != BDADDR_BREDR)
197 if (src_type != BDADDR_BREDR && c->src_type == BDADDR_BREDR)
200 if (c->sport == psm && !bacmp(&c->src, src))
206 int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
210 write_lock(&chan_list_lock);
212 if (psm && __l2cap_global_chan_by_addr(psm, src, chan->src_type)) {
222 u16 p, start, end, incr;
224 if (chan->src_type == BDADDR_BREDR) {
225 start = L2CAP_PSM_DYN_START;
226 end = L2CAP_PSM_AUTO_END;
229 start = L2CAP_PSM_LE_DYN_START;
230 end = L2CAP_PSM_LE_DYN_END;
235 for (p = start; p <= end; p += incr)
236 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src,
238 chan->psm = cpu_to_le16(p);
239 chan->sport = cpu_to_le16(p);
246 write_unlock(&chan_list_lock);
249 EXPORT_SYMBOL_GPL(l2cap_add_psm);
251 int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid)
253 write_lock(&chan_list_lock);
255 /* Override the defaults (which are for conn-oriented) */
256 chan->omtu = L2CAP_DEFAULT_MTU;
257 chan->chan_type = L2CAP_CHAN_FIXED;
261 write_unlock(&chan_list_lock);
266 static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
270 if (conn->hcon->type == LE_LINK)
271 dyn_end = L2CAP_CID_LE_DYN_END;
273 dyn_end = L2CAP_CID_DYN_END;
275 for (cid = L2CAP_CID_DYN_START; cid <= dyn_end; cid++) {
276 if (!__l2cap_get_chan_by_scid(conn, cid))
283 static void l2cap_state_change(struct l2cap_chan *chan, int state)
285 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
286 state_to_string(state));
289 chan->ops->state_change(chan, state, 0);
292 static inline void l2cap_state_change_and_error(struct l2cap_chan *chan,
296 chan->ops->state_change(chan, chan->state, err);
299 static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
301 chan->ops->state_change(chan, chan->state, err);
304 static void __set_retrans_timer(struct l2cap_chan *chan)
306 if (!delayed_work_pending(&chan->monitor_timer) &&
307 chan->retrans_timeout) {
308 l2cap_set_timer(chan, &chan->retrans_timer,
309 msecs_to_jiffies(chan->retrans_timeout));
313 static void __set_monitor_timer(struct l2cap_chan *chan)
315 __clear_retrans_timer(chan);
316 if (chan->monitor_timeout) {
317 l2cap_set_timer(chan, &chan->monitor_timer,
318 msecs_to_jiffies(chan->monitor_timeout));
322 static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
327 skb_queue_walk(head, skb) {
328 if (bt_cb(skb)->l2cap.txseq == seq)
335 /* ---- L2CAP sequence number lists ---- */
337 /* For ERTM, ordered lists of sequence numbers must be tracked for
338 * SREJ requests that are received and for frames that are to be
339 * retransmitted. These seq_list functions implement a singly-linked
340 * list in an array, where membership in the list can also be checked
341 * in constant time. Items can also be added to the tail of the list
342 * and removed from the head in constant time, without further memory
346 static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
348 size_t alloc_size, i;
350 /* Allocated size is a power of 2 to map sequence numbers
351 * (which may be up to 14 bits) in to a smaller array that is
352 * sized for the negotiated ERTM transmit windows.
354 alloc_size = roundup_pow_of_two(size);
356 seq_list->list = kmalloc_array(alloc_size, sizeof(u16), GFP_KERNEL);
360 seq_list->mask = alloc_size - 1;
361 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
362 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
363 for (i = 0; i < alloc_size; i++)
364 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
369 static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
371 kfree(seq_list->list);
374 static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
377 /* Constant-time check for list membership */
378 return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
381 static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
383 u16 seq = seq_list->head;
384 u16 mask = seq_list->mask;
386 seq_list->head = seq_list->list[seq & mask];
387 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
389 if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
390 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
391 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
397 static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
401 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR)
404 for (i = 0; i <= seq_list->mask; i++)
405 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
407 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
408 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
411 static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
413 u16 mask = seq_list->mask;
415 /* All appends happen in constant time */
417 if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR)
420 if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
421 seq_list->head = seq;
423 seq_list->list[seq_list->tail & mask] = seq;
425 seq_list->tail = seq;
426 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
429 static void l2cap_chan_timeout(struct work_struct *work)
431 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
433 struct l2cap_conn *conn = chan->conn;
436 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
438 mutex_lock(&conn->chan_lock);
439 /* __set_chan_timer() calls l2cap_chan_hold(chan) while scheduling
440 * this work. No need to call l2cap_chan_hold(chan) here again.
442 l2cap_chan_lock(chan);
444 if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
445 reason = ECONNREFUSED;
446 else if (chan->state == BT_CONNECT &&
447 chan->sec_level != BT_SECURITY_SDP)
448 reason = ECONNREFUSED;
452 l2cap_chan_close(chan, reason);
454 chan->ops->close(chan);
456 l2cap_chan_unlock(chan);
457 l2cap_chan_put(chan);
459 mutex_unlock(&conn->chan_lock);
462 struct l2cap_chan *l2cap_chan_create(void)
464 struct l2cap_chan *chan;
466 chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
470 skb_queue_head_init(&chan->tx_q);
471 skb_queue_head_init(&chan->srej_q);
472 mutex_init(&chan->lock);
474 /* Set default lock nesting level */
475 atomic_set(&chan->nesting, L2CAP_NESTING_NORMAL);
477 write_lock(&chan_list_lock);
478 list_add(&chan->global_l, &chan_list);
479 write_unlock(&chan_list_lock);
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);
486 chan->state = BT_OPEN;
488 kref_init(&chan->kref);
490 /* This flag is cleared in l2cap_chan_ready() */
491 set_bit(CONF_NOT_COMPLETE, &chan->conf_state);
493 BT_DBG("chan %p", chan);
497 EXPORT_SYMBOL_GPL(l2cap_chan_create);
499 static void l2cap_chan_destroy(struct kref *kref)
501 struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref);
503 BT_DBG("chan %p", chan);
505 write_lock(&chan_list_lock);
506 list_del(&chan->global_l);
507 write_unlock(&chan_list_lock);
512 void l2cap_chan_hold(struct l2cap_chan *c)
514 BT_DBG("chan %p orig refcnt %u", c, kref_read(&c->kref));
519 struct l2cap_chan *l2cap_chan_hold_unless_zero(struct l2cap_chan *c)
521 BT_DBG("chan %p orig refcnt %u", c, kref_read(&c->kref));
523 if (!kref_get_unless_zero(&c->kref))
529 void l2cap_chan_put(struct l2cap_chan *c)
531 BT_DBG("chan %p orig refcnt %u", c, kref_read(&c->kref));
533 kref_put(&c->kref, l2cap_chan_destroy);
535 EXPORT_SYMBOL_GPL(l2cap_chan_put);
537 void l2cap_chan_set_defaults(struct l2cap_chan *chan)
539 chan->fcs = L2CAP_FCS_CRC16;
540 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
541 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
542 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
543 chan->remote_max_tx = chan->max_tx;
544 chan->remote_tx_win = chan->tx_win;
545 chan->ack_win = L2CAP_DEFAULT_TX_WINDOW;
546 chan->sec_level = BT_SECURITY_LOW;
547 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
548 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
549 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
551 chan->conf_state = 0;
552 set_bit(CONF_NOT_COMPLETE, &chan->conf_state);
554 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
556 EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults);
558 static void l2cap_le_flowctl_init(struct l2cap_chan *chan, u16 tx_credits)
561 chan->sdu_last_frag = NULL;
563 chan->tx_credits = tx_credits;
564 /* Derive MPS from connection MTU to stop HCI fragmentation */
565 chan->mps = min_t(u16, chan->imtu, chan->conn->mtu - L2CAP_HDR_SIZE);
566 /* Give enough credits for a full packet */
567 chan->rx_credits = (chan->imtu / chan->mps) + 1;
569 skb_queue_head_init(&chan->tx_q);
572 static void l2cap_ecred_init(struct l2cap_chan *chan, u16 tx_credits)
574 l2cap_le_flowctl_init(chan, tx_credits);
576 /* L2CAP implementations shall support a minimum MPS of 64 octets */
577 if (chan->mps < L2CAP_ECRED_MIN_MPS) {
578 chan->mps = L2CAP_ECRED_MIN_MPS;
579 chan->rx_credits = (chan->imtu / chan->mps) + 1;
583 void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
585 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
586 __le16_to_cpu(chan->psm), chan->dcid);
588 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
592 switch (chan->chan_type) {
593 case L2CAP_CHAN_CONN_ORIENTED:
594 /* Alloc CID for connection-oriented socket */
595 chan->scid = l2cap_alloc_cid(conn);
596 if (conn->hcon->type == ACL_LINK)
597 chan->omtu = L2CAP_DEFAULT_MTU;
600 case L2CAP_CHAN_CONN_LESS:
601 /* Connectionless socket */
602 chan->scid = L2CAP_CID_CONN_LESS;
603 chan->dcid = L2CAP_CID_CONN_LESS;
604 chan->omtu = L2CAP_DEFAULT_MTU;
607 case L2CAP_CHAN_FIXED:
608 /* Caller will set CID and CID specific MTU values */
612 /* Raw socket can send/recv signalling messages only */
613 chan->scid = L2CAP_CID_SIGNALING;
614 chan->dcid = L2CAP_CID_SIGNALING;
615 chan->omtu = L2CAP_DEFAULT_MTU;
618 chan->local_id = L2CAP_BESTEFFORT_ID;
619 chan->local_stype = L2CAP_SERV_BESTEFFORT;
620 chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE;
621 chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME;
622 chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT;
623 chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO;
625 l2cap_chan_hold(chan);
627 /* Only keep a reference for fixed channels if they requested it */
628 if (chan->chan_type != L2CAP_CHAN_FIXED ||
629 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags))
630 hci_conn_hold(conn->hcon);
632 list_add(&chan->list, &conn->chan_l);
635 void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
637 mutex_lock(&conn->chan_lock);
638 __l2cap_chan_add(conn, chan);
639 mutex_unlock(&conn->chan_lock);
642 void l2cap_chan_del(struct l2cap_chan *chan, int err)
644 struct l2cap_conn *conn = chan->conn;
646 __clear_chan_timer(chan);
648 BT_DBG("chan %p, conn %p, err %d, state %s", chan, conn, err,
649 state_to_string(chan->state));
651 chan->ops->teardown(chan, err);
654 struct amp_mgr *mgr = conn->hcon->amp_mgr;
655 /* Delete from channel list */
656 list_del(&chan->list);
658 l2cap_chan_put(chan);
662 /* Reference was only held for non-fixed channels or
663 * fixed channels that explicitly requested it using the
664 * FLAG_HOLD_HCI_CONN flag.
666 if (chan->chan_type != L2CAP_CHAN_FIXED ||
667 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags))
668 hci_conn_drop(conn->hcon);
670 if (mgr && mgr->bredr_chan == chan)
671 mgr->bredr_chan = NULL;
674 if (chan->hs_hchan) {
675 struct hci_chan *hs_hchan = chan->hs_hchan;
677 BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan);
678 amp_disconnect_logical_link(hs_hchan);
681 if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state))
684 switch (chan->mode) {
685 case L2CAP_MODE_BASIC:
688 case L2CAP_MODE_LE_FLOWCTL:
689 case L2CAP_MODE_EXT_FLOWCTL:
690 skb_queue_purge(&chan->tx_q);
693 case L2CAP_MODE_ERTM:
694 __clear_retrans_timer(chan);
695 __clear_monitor_timer(chan);
696 __clear_ack_timer(chan);
698 skb_queue_purge(&chan->srej_q);
700 l2cap_seq_list_free(&chan->srej_list);
701 l2cap_seq_list_free(&chan->retrans_list);
704 case L2CAP_MODE_STREAMING:
705 skb_queue_purge(&chan->tx_q);
709 EXPORT_SYMBOL_GPL(l2cap_chan_del);
711 static void __l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
714 struct l2cap_chan *chan;
716 list_for_each_entry(chan, &conn->chan_l, list) {
721 void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
727 mutex_lock(&conn->chan_lock);
728 __l2cap_chan_list(conn, func, data);
729 mutex_unlock(&conn->chan_lock);
732 EXPORT_SYMBOL_GPL(l2cap_chan_list);
734 static void l2cap_conn_update_id_addr(struct work_struct *work)
736 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
737 id_addr_update_work);
738 struct hci_conn *hcon = conn->hcon;
739 struct l2cap_chan *chan;
741 mutex_lock(&conn->chan_lock);
743 list_for_each_entry(chan, &conn->chan_l, list) {
744 l2cap_chan_lock(chan);
745 bacpy(&chan->dst, &hcon->dst);
746 chan->dst_type = bdaddr_dst_type(hcon);
747 l2cap_chan_unlock(chan);
750 mutex_unlock(&conn->chan_lock);
753 static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan)
755 struct l2cap_conn *conn = chan->conn;
756 struct l2cap_le_conn_rsp rsp;
759 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
760 result = L2CAP_CR_LE_AUTHORIZATION;
762 result = L2CAP_CR_LE_BAD_PSM;
764 l2cap_state_change(chan, BT_DISCONN);
766 rsp.dcid = cpu_to_le16(chan->scid);
767 rsp.mtu = cpu_to_le16(chan->imtu);
768 rsp.mps = cpu_to_le16(chan->mps);
769 rsp.credits = cpu_to_le16(chan->rx_credits);
770 rsp.result = cpu_to_le16(result);
772 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
776 static void l2cap_chan_ecred_connect_reject(struct l2cap_chan *chan)
778 struct l2cap_conn *conn = chan->conn;
779 struct l2cap_ecred_conn_rsp rsp;
782 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
783 result = L2CAP_CR_LE_AUTHORIZATION;
785 result = L2CAP_CR_LE_BAD_PSM;
787 l2cap_state_change(chan, BT_DISCONN);
789 memset(&rsp, 0, sizeof(rsp));
791 rsp.result = cpu_to_le16(result);
793 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
797 static void l2cap_chan_connect_reject(struct l2cap_chan *chan)
799 struct l2cap_conn *conn = chan->conn;
800 struct l2cap_conn_rsp rsp;
803 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
804 result = L2CAP_CR_SEC_BLOCK;
806 result = L2CAP_CR_BAD_PSM;
808 l2cap_state_change(chan, BT_DISCONN);
810 rsp.scid = cpu_to_le16(chan->dcid);
811 rsp.dcid = cpu_to_le16(chan->scid);
812 rsp.result = cpu_to_le16(result);
813 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
815 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
818 void l2cap_chan_close(struct l2cap_chan *chan, int reason)
820 struct l2cap_conn *conn = chan->conn;
822 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
824 switch (chan->state) {
826 chan->ops->teardown(chan, 0);
831 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
832 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
833 l2cap_send_disconn_req(chan, reason);
835 l2cap_chan_del(chan, reason);
839 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
840 if (conn->hcon->type == ACL_LINK)
841 l2cap_chan_connect_reject(chan);
842 else if (conn->hcon->type == LE_LINK) {
843 switch (chan->mode) {
844 case L2CAP_MODE_LE_FLOWCTL:
845 l2cap_chan_le_connect_reject(chan);
847 case L2CAP_MODE_EXT_FLOWCTL:
848 l2cap_chan_ecred_connect_reject(chan);
854 l2cap_chan_del(chan, reason);
859 l2cap_chan_del(chan, reason);
863 chan->ops->teardown(chan, 0);
867 EXPORT_SYMBOL(l2cap_chan_close);
869 static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
871 switch (chan->chan_type) {
873 switch (chan->sec_level) {
874 case BT_SECURITY_HIGH:
875 case BT_SECURITY_FIPS:
876 return HCI_AT_DEDICATED_BONDING_MITM;
877 case BT_SECURITY_MEDIUM:
878 return HCI_AT_DEDICATED_BONDING;
880 return HCI_AT_NO_BONDING;
883 case L2CAP_CHAN_CONN_LESS:
884 if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) {
885 if (chan->sec_level == BT_SECURITY_LOW)
886 chan->sec_level = BT_SECURITY_SDP;
888 if (chan->sec_level == BT_SECURITY_HIGH ||
889 chan->sec_level == BT_SECURITY_FIPS)
890 return HCI_AT_NO_BONDING_MITM;
892 return HCI_AT_NO_BONDING;
894 case L2CAP_CHAN_CONN_ORIENTED:
895 if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) {
896 if (chan->sec_level == BT_SECURITY_LOW)
897 chan->sec_level = BT_SECURITY_SDP;
899 if (chan->sec_level == BT_SECURITY_HIGH ||
900 chan->sec_level == BT_SECURITY_FIPS)
901 return HCI_AT_NO_BONDING_MITM;
903 return HCI_AT_NO_BONDING;
908 switch (chan->sec_level) {
909 case BT_SECURITY_HIGH:
910 case BT_SECURITY_FIPS:
911 return HCI_AT_GENERAL_BONDING_MITM;
912 case BT_SECURITY_MEDIUM:
913 return HCI_AT_GENERAL_BONDING;
915 return HCI_AT_NO_BONDING;
921 /* Service level security */
922 int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator)
924 struct l2cap_conn *conn = chan->conn;
927 if (conn->hcon->type == LE_LINK)
928 return smp_conn_security(conn->hcon, chan->sec_level);
930 auth_type = l2cap_get_auth_type(chan);
932 return hci_conn_security(conn->hcon, chan->sec_level, auth_type,
936 static u8 l2cap_get_ident(struct l2cap_conn *conn)
940 /* Get next available identificator.
941 * 1 - 128 are used by kernel.
942 * 129 - 199 are reserved.
943 * 200 - 254 are used by utilities like l2ping, etc.
946 mutex_lock(&conn->ident_lock);
948 if (++conn->tx_ident > 128)
953 mutex_unlock(&conn->ident_lock);
958 static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
961 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
964 BT_DBG("code 0x%2.2x", code);
969 /* Use NO_FLUSH if supported or we have an LE link (which does
970 * not support auto-flushing packets) */
971 if (lmp_no_flush_capable(conn->hcon->hdev) ||
972 conn->hcon->type == LE_LINK)
973 flags = ACL_START_NO_FLUSH;
977 bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
978 skb->priority = HCI_PRIO_MAX;
980 hci_send_acl(conn->hchan, skb, flags);
983 static bool __chan_is_moving(struct l2cap_chan *chan)
985 return chan->move_state != L2CAP_MOVE_STABLE &&
986 chan->move_state != L2CAP_MOVE_WAIT_PREPARE;
989 static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
991 struct hci_conn *hcon = chan->conn->hcon;
994 BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
997 if (chan->hs_hcon && !__chan_is_moving(chan)) {
999 hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE);
1006 /* Use NO_FLUSH for LE links (where this is the only option) or
1007 * if the BR/EDR link supports it and flushing has not been
1008 * explicitly requested (through FLAG_FLUSHABLE).
1010 if (hcon->type == LE_LINK ||
1011 (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
1012 lmp_no_flush_capable(hcon->hdev)))
1013 flags = ACL_START_NO_FLUSH;
1017 bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
1018 hci_send_acl(chan->conn->hchan, skb, flags);
1021 static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control)
1023 control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
1024 control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT;
1026 if (enh & L2CAP_CTRL_FRAME_TYPE) {
1028 control->sframe = 1;
1029 control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT;
1030 control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
1036 control->sframe = 0;
1037 control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
1038 control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
1045 static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control)
1047 control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT;
1048 control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT;
1050 if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) {
1052 control->sframe = 1;
1053 control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT;
1054 control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT;
1060 control->sframe = 0;
1061 control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
1062 control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT;
1069 static inline void __unpack_control(struct l2cap_chan *chan,
1070 struct sk_buff *skb)
1072 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
1073 __unpack_extended_control(get_unaligned_le32(skb->data),
1074 &bt_cb(skb)->l2cap);
1075 skb_pull(skb, L2CAP_EXT_CTRL_SIZE);
1077 __unpack_enhanced_control(get_unaligned_le16(skb->data),
1078 &bt_cb(skb)->l2cap);
1079 skb_pull(skb, L2CAP_ENH_CTRL_SIZE);
1083 static u32 __pack_extended_control(struct l2cap_ctrl *control)
1087 packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT;
1088 packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT;
1090 if (control->sframe) {
1091 packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT;
1092 packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT;
1093 packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
1095 packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT;
1096 packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT;
1102 static u16 __pack_enhanced_control(struct l2cap_ctrl *control)
1106 packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT;
1107 packed |= control->final << L2CAP_CTRL_FINAL_SHIFT;
1109 if (control->sframe) {
1110 packed |= control->poll << L2CAP_CTRL_POLL_SHIFT;
1111 packed |= control->super << L2CAP_CTRL_SUPER_SHIFT;
1112 packed |= L2CAP_CTRL_FRAME_TYPE;
1114 packed |= control->sar << L2CAP_CTRL_SAR_SHIFT;
1115 packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT;
1121 static inline void __pack_control(struct l2cap_chan *chan,
1122 struct l2cap_ctrl *control,
1123 struct sk_buff *skb)
1125 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
1126 put_unaligned_le32(__pack_extended_control(control),
1127 skb->data + L2CAP_HDR_SIZE);
1129 put_unaligned_le16(__pack_enhanced_control(control),
1130 skb->data + L2CAP_HDR_SIZE);
1134 static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan)
1136 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1137 return L2CAP_EXT_HDR_SIZE;
1139 return L2CAP_ENH_HDR_SIZE;
1142 static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan,
1145 struct sk_buff *skb;
1146 struct l2cap_hdr *lh;
1147 int hlen = __ertm_hdr_size(chan);
1149 if (chan->fcs == L2CAP_FCS_CRC16)
1150 hlen += L2CAP_FCS_SIZE;
1152 skb = bt_skb_alloc(hlen, GFP_KERNEL);
1155 return ERR_PTR(-ENOMEM);
1157 lh = skb_put(skb, L2CAP_HDR_SIZE);
1158 lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
1159 lh->cid = cpu_to_le16(chan->dcid);
1161 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1162 put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
1164 put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
1166 if (chan->fcs == L2CAP_FCS_CRC16) {
1167 u16 fcs = crc16(0, (u8 *)skb->data, skb->len);
1168 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
1171 skb->priority = HCI_PRIO_MAX;
1175 static void l2cap_send_sframe(struct l2cap_chan *chan,
1176 struct l2cap_ctrl *control)
1178 struct sk_buff *skb;
1181 BT_DBG("chan %p, control %p", chan, control);
1183 if (!control->sframe)
1186 if (__chan_is_moving(chan))
1189 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) &&
1193 if (control->super == L2CAP_SUPER_RR)
1194 clear_bit(CONN_RNR_SENT, &chan->conn_state);
1195 else if (control->super == L2CAP_SUPER_RNR)
1196 set_bit(CONN_RNR_SENT, &chan->conn_state);
1198 if (control->super != L2CAP_SUPER_SREJ) {
1199 chan->last_acked_seq = control->reqseq;
1200 __clear_ack_timer(chan);
1203 BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq,
1204 control->final, control->poll, control->super);
1206 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1207 control_field = __pack_extended_control(control);
1209 control_field = __pack_enhanced_control(control);
1211 skb = l2cap_create_sframe_pdu(chan, control_field);
1213 l2cap_do_send(chan, skb);
1216 static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll)
1218 struct l2cap_ctrl control;
1220 BT_DBG("chan %p, poll %d", chan, poll);
1222 memset(&control, 0, sizeof(control));
1224 control.poll = poll;
1226 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
1227 control.super = L2CAP_SUPER_RNR;
1229 control.super = L2CAP_SUPER_RR;
1231 control.reqseq = chan->buffer_seq;
1232 l2cap_send_sframe(chan, &control);
1235 static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
1237 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
1240 return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
1243 static bool __amp_capable(struct l2cap_chan *chan)
1245 struct l2cap_conn *conn = chan->conn;
1246 struct hci_dev *hdev;
1247 bool amp_available = false;
1249 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
1252 if (!(conn->remote_fixed_chan & L2CAP_FC_A2MP))
1255 read_lock(&hci_dev_list_lock);
1256 list_for_each_entry(hdev, &hci_dev_list, list) {
1257 if (hdev->amp_type != AMP_TYPE_BREDR &&
1258 test_bit(HCI_UP, &hdev->flags)) {
1259 amp_available = true;
1263 read_unlock(&hci_dev_list_lock);
1265 if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED)
1266 return amp_available;
1271 static bool l2cap_check_efs(struct l2cap_chan *chan)
1273 /* Check EFS parameters */
1277 void l2cap_send_conn_req(struct l2cap_chan *chan)
1279 struct l2cap_conn *conn = chan->conn;
1280 struct l2cap_conn_req req;
1282 req.scid = cpu_to_le16(chan->scid);
1283 req.psm = chan->psm;
1285 chan->ident = l2cap_get_ident(conn);
1287 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
1289 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req);
1292 static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id)
1294 struct l2cap_create_chan_req req;
1295 req.scid = cpu_to_le16(chan->scid);
1296 req.psm = chan->psm;
1297 req.amp_id = amp_id;
1299 chan->ident = l2cap_get_ident(chan->conn);
1301 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ,
1305 static void l2cap_move_setup(struct l2cap_chan *chan)
1307 struct sk_buff *skb;
1309 BT_DBG("chan %p", chan);
1311 if (chan->mode != L2CAP_MODE_ERTM)
1314 __clear_retrans_timer(chan);
1315 __clear_monitor_timer(chan);
1316 __clear_ack_timer(chan);
1318 chan->retry_count = 0;
1319 skb_queue_walk(&chan->tx_q, skb) {
1320 if (bt_cb(skb)->l2cap.retries)
1321 bt_cb(skb)->l2cap.retries = 1;
1326 chan->expected_tx_seq = chan->buffer_seq;
1328 clear_bit(CONN_REJ_ACT, &chan->conn_state);
1329 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
1330 l2cap_seq_list_clear(&chan->retrans_list);
1331 l2cap_seq_list_clear(&chan->srej_list);
1332 skb_queue_purge(&chan->srej_q);
1334 chan->tx_state = L2CAP_TX_STATE_XMIT;
1335 chan->rx_state = L2CAP_RX_STATE_MOVE;
1337 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
1340 static void l2cap_move_done(struct l2cap_chan *chan)
1342 u8 move_role = chan->move_role;
1343 BT_DBG("chan %p", chan);
1345 chan->move_state = L2CAP_MOVE_STABLE;
1346 chan->move_role = L2CAP_MOVE_ROLE_NONE;
1348 if (chan->mode != L2CAP_MODE_ERTM)
1351 switch (move_role) {
1352 case L2CAP_MOVE_ROLE_INITIATOR:
1353 l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL);
1354 chan->rx_state = L2CAP_RX_STATE_WAIT_F;
1356 case L2CAP_MOVE_ROLE_RESPONDER:
1357 chan->rx_state = L2CAP_RX_STATE_WAIT_P;
1362 static void l2cap_chan_ready(struct l2cap_chan *chan)
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.
1368 if (chan->state == BT_CONNECTED)
1371 /* This clears all conf flags, including CONF_NOT_COMPLETE */
1372 chan->conf_state = 0;
1373 __clear_chan_timer(chan);
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);
1383 chan->state = BT_CONNECTED;
1385 chan->ops->ready(chan);
1388 static void l2cap_le_connect(struct l2cap_chan *chan)
1390 struct l2cap_conn *conn = chan->conn;
1391 struct l2cap_le_conn_req req;
1393 if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags))
1397 chan->imtu = chan->conn->mtu;
1399 l2cap_le_flowctl_init(chan, 0);
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);
1408 chan->ident = l2cap_get_ident(conn);
1410 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ,
1414 struct l2cap_ecred_conn_data {
1416 struct l2cap_ecred_conn_req req;
1419 struct l2cap_chan *chan;
1424 static void l2cap_ecred_defer_connect(struct l2cap_chan *chan, void *data)
1426 struct l2cap_ecred_conn_data *conn = data;
1429 if (chan == conn->chan)
1432 if (!test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags))
1435 pid = chan->ops->get_peer_pid(chan);
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)
1442 if (test_and_set_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))
1445 l2cap_ecred_init(chan, 0);
1447 /* Set the same ident so we can match on the rsp */
1448 chan->ident = conn->chan->ident;
1450 /* Include all channels deferred */
1451 conn->pdu.scid[conn->count] = cpu_to_le16(chan->scid);
1456 static void l2cap_ecred_connect(struct l2cap_chan *chan)
1458 struct l2cap_conn *conn = chan->conn;
1459 struct l2cap_ecred_conn_data data;
1461 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
1464 if (test_and_set_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))
1467 l2cap_ecred_init(chan, 0);
1469 memset(&data, 0, sizeof(data));
1470 data.pdu.req.psm = chan->psm;
1471 data.pdu.req.mtu = cpu_to_le16(chan->imtu);
1472 data.pdu.req.mps = cpu_to_le16(chan->mps);
1473 data.pdu.req.credits = cpu_to_le16(chan->rx_credits);
1474 data.pdu.scid[0] = cpu_to_le16(chan->scid);
1476 chan->ident = l2cap_get_ident(conn);
1480 data.pid = chan->ops->get_peer_pid(chan);
1482 __l2cap_chan_list(conn, l2cap_ecred_defer_connect, &data);
1484 l2cap_send_cmd(conn, chan->ident, L2CAP_ECRED_CONN_REQ,
1485 sizeof(data.pdu.req) + data.count * sizeof(__le16),
1489 static void l2cap_le_start(struct l2cap_chan *chan)
1491 struct l2cap_conn *conn = chan->conn;
1493 if (!smp_conn_security(conn->hcon, chan->sec_level))
1497 l2cap_chan_ready(chan);
1501 if (chan->state == BT_CONNECT) {
1502 if (chan->mode == L2CAP_MODE_EXT_FLOWCTL)
1503 l2cap_ecred_connect(chan);
1505 l2cap_le_connect(chan);
1509 static void l2cap_start_connection(struct l2cap_chan *chan)
1511 if (__amp_capable(chan)) {
1512 BT_DBG("chan %p AMP capable: discover AMPs", chan);
1513 a2mp_discover_amp(chan);
1514 } else if (chan->conn->hcon->type == LE_LINK) {
1515 l2cap_le_start(chan);
1517 l2cap_send_conn_req(chan);
1521 static void l2cap_request_info(struct l2cap_conn *conn)
1523 struct l2cap_info_req req;
1525 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
1528 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
1530 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
1531 conn->info_ident = l2cap_get_ident(conn);
1533 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
1535 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
1539 static bool l2cap_check_enc_key_size(struct hci_conn *hcon)
1541 /* The minimum encryption key size needs to be enforced by the
1542 * host stack before establishing any L2CAP connections. The
1543 * specification in theory allows a minimum of 1, but to align
1544 * BR/EDR and LE transports, a minimum of 7 is chosen.
1546 * This check might also be called for unencrypted connections
1547 * that have no key size requirements. Ensure that the link is
1548 * actually encrypted before enforcing a key size.
1550 int min_key_size = hcon->hdev->min_enc_key_size;
1552 /* On FIPS security level, key size must be 16 bytes */
1553 if (hcon->sec_level == BT_SECURITY_FIPS)
1556 return (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags) ||
1557 hcon->enc_key_size >= min_key_size);
1560 static void l2cap_do_start(struct l2cap_chan *chan)
1562 struct l2cap_conn *conn = chan->conn;
1564 if (conn->hcon->type == LE_LINK) {
1565 l2cap_le_start(chan);
1569 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) {
1570 l2cap_request_info(conn);
1574 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
1577 if (!l2cap_chan_check_security(chan, true) ||
1578 !__l2cap_no_conn_pending(chan))
1581 if (l2cap_check_enc_key_size(conn->hcon))
1582 l2cap_start_connection(chan);
1584 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
1587 static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
1589 u32 local_feat_mask = l2cap_feat_mask;
1591 local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
1594 case L2CAP_MODE_ERTM:
1595 return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
1596 case L2CAP_MODE_STREAMING:
1597 return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
1603 static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
1605 struct l2cap_conn *conn = chan->conn;
1606 struct l2cap_disconn_req req;
1611 if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) {
1612 __clear_retrans_timer(chan);
1613 __clear_monitor_timer(chan);
1614 __clear_ack_timer(chan);
1617 if (chan->scid == L2CAP_CID_A2MP) {
1618 l2cap_state_change(chan, BT_DISCONN);
1622 req.dcid = cpu_to_le16(chan->dcid);
1623 req.scid = cpu_to_le16(chan->scid);
1624 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ,
1627 l2cap_state_change_and_error(chan, BT_DISCONN, err);
1630 /* ---- L2CAP connections ---- */
1631 static void l2cap_conn_start(struct l2cap_conn *conn)
1633 struct l2cap_chan *chan, *tmp;
1635 BT_DBG("conn %p", conn);
1637 mutex_lock(&conn->chan_lock);
1639 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
1640 l2cap_chan_lock(chan);
1642 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1643 l2cap_chan_ready(chan);
1644 l2cap_chan_unlock(chan);
1648 if (chan->state == BT_CONNECT) {
1649 if (!l2cap_chan_check_security(chan, true) ||
1650 !__l2cap_no_conn_pending(chan)) {
1651 l2cap_chan_unlock(chan);
1655 if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
1656 && test_bit(CONF_STATE2_DEVICE,
1657 &chan->conf_state)) {
1658 l2cap_chan_close(chan, ECONNRESET);
1659 l2cap_chan_unlock(chan);
1663 if (l2cap_check_enc_key_size(conn->hcon))
1664 l2cap_start_connection(chan);
1666 l2cap_chan_close(chan, ECONNREFUSED);
1668 } else if (chan->state == BT_CONNECT2) {
1669 struct l2cap_conn_rsp rsp;
1671 rsp.scid = cpu_to_le16(chan->dcid);
1672 rsp.dcid = cpu_to_le16(chan->scid);
1674 if (l2cap_chan_check_security(chan, false)) {
1675 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
1676 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1677 rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
1678 chan->ops->defer(chan);
1681 l2cap_state_change(chan, BT_CONFIG);
1682 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
1683 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
1686 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1687 rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
1690 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
1693 if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
1694 rsp.result != L2CAP_CR_SUCCESS) {
1695 l2cap_chan_unlock(chan);
1699 set_bit(CONF_REQ_SENT, &chan->conf_state);
1700 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
1701 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
1702 chan->num_conf_req++;
1705 l2cap_chan_unlock(chan);
1708 mutex_unlock(&conn->chan_lock);
1711 static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1713 struct hci_conn *hcon = conn->hcon;
1714 struct hci_dev *hdev = hcon->hdev;
1716 BT_DBG("%s conn %p", hdev->name, conn);
1718 /* For outgoing pairing which doesn't necessarily have an
1719 * associated socket (e.g. mgmt_pair_device).
1722 smp_conn_security(hcon, hcon->pending_sec_level);
1724 /* For LE peripheral connections, make sure the connection interval
1725 * is in the range of the minimum and maximum interval that has
1726 * been configured for this connection. If not, then trigger
1727 * the connection update procedure.
1729 if (hcon->role == HCI_ROLE_SLAVE &&
1730 (hcon->le_conn_interval < hcon->le_conn_min_interval ||
1731 hcon->le_conn_interval > hcon->le_conn_max_interval)) {
1732 struct l2cap_conn_param_update_req req;
1734 req.min = cpu_to_le16(hcon->le_conn_min_interval);
1735 req.max = cpu_to_le16(hcon->le_conn_max_interval);
1736 req.latency = cpu_to_le16(hcon->le_conn_latency);
1737 req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout);
1739 l2cap_send_cmd(conn, l2cap_get_ident(conn),
1740 L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req);
1744 static void l2cap_conn_ready(struct l2cap_conn *conn)
1746 struct l2cap_chan *chan;
1747 struct hci_conn *hcon = conn->hcon;
1749 BT_DBG("conn %p", conn);
1751 if (hcon->type == ACL_LINK)
1752 l2cap_request_info(conn);
1754 mutex_lock(&conn->chan_lock);
1756 list_for_each_entry(chan, &conn->chan_l, list) {
1758 l2cap_chan_lock(chan);
1760 if (chan->scid == L2CAP_CID_A2MP) {
1761 l2cap_chan_unlock(chan);
1765 if (hcon->type == LE_LINK) {
1766 l2cap_le_start(chan);
1767 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1768 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
1769 l2cap_chan_ready(chan);
1770 } else if (chan->state == BT_CONNECT) {
1771 l2cap_do_start(chan);
1774 l2cap_chan_unlock(chan);
1777 mutex_unlock(&conn->chan_lock);
1779 if (hcon->type == LE_LINK)
1780 l2cap_le_conn_ready(conn);
1782 queue_work(hcon->hdev->workqueue, &conn->pending_rx_work);
1785 /* Notify sockets that we cannot guaranty reliability anymore */
1786 static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
1788 struct l2cap_chan *chan;
1790 BT_DBG("conn %p", conn);
1792 mutex_lock(&conn->chan_lock);
1794 list_for_each_entry(chan, &conn->chan_l, list) {
1795 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
1796 l2cap_chan_set_err(chan, err);
1799 mutex_unlock(&conn->chan_lock);
1802 static void l2cap_info_timeout(struct work_struct *work)
1804 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
1807 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
1808 conn->info_ident = 0;
1810 l2cap_conn_start(conn);
1815 * External modules can register l2cap_user objects on l2cap_conn. The ->probe
1816 * callback is called during registration. The ->remove callback is called
1817 * during unregistration.
1818 * An l2cap_user object can either be explicitly unregistered or when the
1819 * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon,
1820 * l2cap->hchan, .. are valid as long as the remove callback hasn't been called.
1821 * External modules must own a reference to the l2cap_conn object if they intend
1822 * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at
1823 * any time if they don't.
1826 int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user)
1828 struct hci_dev *hdev = conn->hcon->hdev;
1831 /* We need to check whether l2cap_conn is registered. If it is not, we
1832 * must not register the l2cap_user. l2cap_conn_del() is unregisters
1833 * l2cap_conn objects, but doesn't provide its own locking. Instead, it
1834 * relies on the parent hci_conn object to be locked. This itself relies
1835 * on the hci_dev object to be locked. So we must lock the hci device
1840 if (!list_empty(&user->list)) {
1845 /* conn->hchan is NULL after l2cap_conn_del() was called */
1851 ret = user->probe(conn, user);
1855 list_add(&user->list, &conn->users);
1859 hci_dev_unlock(hdev);
1862 EXPORT_SYMBOL(l2cap_register_user);
1864 void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user)
1866 struct hci_dev *hdev = conn->hcon->hdev;
1870 if (list_empty(&user->list))
1873 list_del_init(&user->list);
1874 user->remove(conn, user);
1877 hci_dev_unlock(hdev);
1879 EXPORT_SYMBOL(l2cap_unregister_user);
1881 static void l2cap_unregister_all_users(struct l2cap_conn *conn)
1883 struct l2cap_user *user;
1885 while (!list_empty(&conn->users)) {
1886 user = list_first_entry(&conn->users, struct l2cap_user, list);
1887 list_del_init(&user->list);
1888 user->remove(conn, user);
1892 static void l2cap_conn_del(struct hci_conn *hcon, int err)
1894 struct l2cap_conn *conn = hcon->l2cap_data;
1895 struct l2cap_chan *chan, *l;
1900 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
1902 kfree_skb(conn->rx_skb);
1904 skb_queue_purge(&conn->pending_rx);
1906 /* We can not call flush_work(&conn->pending_rx_work) here since we
1907 * might block if we are running on a worker from the same workqueue
1908 * pending_rx_work is waiting on.
1910 if (work_pending(&conn->pending_rx_work))
1911 cancel_work_sync(&conn->pending_rx_work);
1913 if (work_pending(&conn->id_addr_update_work))
1914 cancel_work_sync(&conn->id_addr_update_work);
1916 l2cap_unregister_all_users(conn);
1918 /* Force the connection to be immediately dropped */
1919 hcon->disc_timeout = 0;
1921 mutex_lock(&conn->chan_lock);
1924 list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
1925 l2cap_chan_hold(chan);
1926 l2cap_chan_lock(chan);
1928 l2cap_chan_del(chan, err);
1930 chan->ops->close(chan);
1932 l2cap_chan_unlock(chan);
1933 l2cap_chan_put(chan);
1936 mutex_unlock(&conn->chan_lock);
1938 hci_chan_del(conn->hchan);
1940 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
1941 cancel_delayed_work_sync(&conn->info_timer);
1943 hcon->l2cap_data = NULL;
1945 l2cap_conn_put(conn);
1948 static void l2cap_conn_free(struct kref *ref)
1950 struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref);
1952 hci_conn_put(conn->hcon);
1956 struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn)
1958 kref_get(&conn->ref);
1961 EXPORT_SYMBOL(l2cap_conn_get);
1963 void l2cap_conn_put(struct l2cap_conn *conn)
1965 kref_put(&conn->ref, l2cap_conn_free);
1967 EXPORT_SYMBOL(l2cap_conn_put);
1969 /* ---- Socket interface ---- */
1971 /* Find socket with psm and source / destination bdaddr.
1972 * Returns closest match.
1974 static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1979 struct l2cap_chan *c, *tmp, *c1 = NULL;
1981 read_lock(&chan_list_lock);
1983 list_for_each_entry_safe(c, tmp, &chan_list, global_l) {
1984 if (state && c->state != state)
1987 if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR)
1990 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR)
1993 if (c->chan_type != L2CAP_CHAN_FIXED && c->psm == psm) {
1994 int src_match, dst_match;
1995 int src_any, dst_any;
1998 src_match = !bacmp(&c->src, src);
1999 dst_match = !bacmp(&c->dst, dst);
2000 if (src_match && dst_match) {
2001 if (!l2cap_chan_hold_unless_zero(c))
2004 read_unlock(&chan_list_lock);
2009 src_any = !bacmp(&c->src, BDADDR_ANY);
2010 dst_any = !bacmp(&c->dst, BDADDR_ANY);
2011 if ((src_match && dst_any) || (src_any && dst_match) ||
2012 (src_any && dst_any))
2018 c1 = l2cap_chan_hold_unless_zero(c1);
2020 read_unlock(&chan_list_lock);
2025 static void l2cap_monitor_timeout(struct work_struct *work)
2027 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
2028 monitor_timer.work);
2030 BT_DBG("chan %p", chan);
2032 l2cap_chan_lock(chan);
2035 l2cap_chan_unlock(chan);
2036 l2cap_chan_put(chan);
2040 l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO);
2042 l2cap_chan_unlock(chan);
2043 l2cap_chan_put(chan);
2046 static void l2cap_retrans_timeout(struct work_struct *work)
2048 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
2049 retrans_timer.work);
2051 BT_DBG("chan %p", chan);
2053 l2cap_chan_lock(chan);
2056 l2cap_chan_unlock(chan);
2057 l2cap_chan_put(chan);
2061 l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO);
2062 l2cap_chan_unlock(chan);
2063 l2cap_chan_put(chan);
2066 static void l2cap_streaming_send(struct l2cap_chan *chan,
2067 struct sk_buff_head *skbs)
2069 struct sk_buff *skb;
2070 struct l2cap_ctrl *control;
2072 BT_DBG("chan %p, skbs %p", chan, skbs);
2074 if (__chan_is_moving(chan))
2077 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2079 while (!skb_queue_empty(&chan->tx_q)) {
2081 skb = skb_dequeue(&chan->tx_q);
2083 bt_cb(skb)->l2cap.retries = 1;
2084 control = &bt_cb(skb)->l2cap;
2086 control->reqseq = 0;
2087 control->txseq = chan->next_tx_seq;
2089 __pack_control(chan, control, skb);
2091 if (chan->fcs == L2CAP_FCS_CRC16) {
2092 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
2093 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
2096 l2cap_do_send(chan, skb);
2098 BT_DBG("Sent txseq %u", control->txseq);
2100 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
2101 chan->frames_sent++;
2105 static int l2cap_ertm_send(struct l2cap_chan *chan)
2107 struct sk_buff *skb, *tx_skb;
2108 struct l2cap_ctrl *control;
2111 BT_DBG("chan %p", chan);
2113 if (chan->state != BT_CONNECTED)
2116 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2119 if (__chan_is_moving(chan))
2122 while (chan->tx_send_head &&
2123 chan->unacked_frames < chan->remote_tx_win &&
2124 chan->tx_state == L2CAP_TX_STATE_XMIT) {
2126 skb = chan->tx_send_head;
2128 bt_cb(skb)->l2cap.retries = 1;
2129 control = &bt_cb(skb)->l2cap;
2131 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
2134 control->reqseq = chan->buffer_seq;
2135 chan->last_acked_seq = chan->buffer_seq;
2136 control->txseq = chan->next_tx_seq;
2138 __pack_control(chan, control, skb);
2140 if (chan->fcs == L2CAP_FCS_CRC16) {
2141 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
2142 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
2145 /* Clone after data has been modified. Data is assumed to be
2146 read-only (for locking purposes) on cloned sk_buffs.
2148 tx_skb = skb_clone(skb, GFP_KERNEL);
2153 __set_retrans_timer(chan);
2155 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
2156 chan->unacked_frames++;
2157 chan->frames_sent++;
2160 if (skb_queue_is_last(&chan->tx_q, skb))
2161 chan->tx_send_head = NULL;
2163 chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
2165 l2cap_do_send(chan, tx_skb);
2166 BT_DBG("Sent txseq %u", control->txseq);
2169 BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent,
2170 chan->unacked_frames, skb_queue_len(&chan->tx_q));
2175 static void l2cap_ertm_resend(struct l2cap_chan *chan)
2177 struct l2cap_ctrl control;
2178 struct sk_buff *skb;
2179 struct sk_buff *tx_skb;
2182 BT_DBG("chan %p", chan);
2184 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2187 if (__chan_is_moving(chan))
2190 while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
2191 seq = l2cap_seq_list_pop(&chan->retrans_list);
2193 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq);
2195 BT_DBG("Error: Can't retransmit seq %d, frame missing",
2200 bt_cb(skb)->l2cap.retries++;
2201 control = bt_cb(skb)->l2cap;
2203 if (chan->max_tx != 0 &&
2204 bt_cb(skb)->l2cap.retries > chan->max_tx) {
2205 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
2206 l2cap_send_disconn_req(chan, ECONNRESET);
2207 l2cap_seq_list_clear(&chan->retrans_list);
2211 control.reqseq = chan->buffer_seq;
2212 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
2217 if (skb_cloned(skb)) {
2218 /* Cloned sk_buffs are read-only, so we need a
2221 tx_skb = skb_copy(skb, GFP_KERNEL);
2223 tx_skb = skb_clone(skb, GFP_KERNEL);
2227 l2cap_seq_list_clear(&chan->retrans_list);
2231 /* Update skb contents */
2232 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
2233 put_unaligned_le32(__pack_extended_control(&control),
2234 tx_skb->data + L2CAP_HDR_SIZE);
2236 put_unaligned_le16(__pack_enhanced_control(&control),
2237 tx_skb->data + L2CAP_HDR_SIZE);
2241 if (chan->fcs == L2CAP_FCS_CRC16) {
2242 u16 fcs = crc16(0, (u8 *) tx_skb->data,
2243 tx_skb->len - L2CAP_FCS_SIZE);
2244 put_unaligned_le16(fcs, skb_tail_pointer(tx_skb) -
2248 l2cap_do_send(chan, tx_skb);
2250 BT_DBG("Resent txseq %d", control.txseq);
2252 chan->last_acked_seq = chan->buffer_seq;
2256 static void l2cap_retransmit(struct l2cap_chan *chan,
2257 struct l2cap_ctrl *control)
2259 BT_DBG("chan %p, control %p", chan, control);
2261 l2cap_seq_list_append(&chan->retrans_list, control->reqseq);
2262 l2cap_ertm_resend(chan);
2265 static void l2cap_retransmit_all(struct l2cap_chan *chan,
2266 struct l2cap_ctrl *control)
2268 struct sk_buff *skb;
2270 BT_DBG("chan %p, control %p", chan, control);
2273 set_bit(CONN_SEND_FBIT, &chan->conn_state);
2275 l2cap_seq_list_clear(&chan->retrans_list);
2277 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2280 if (chan->unacked_frames) {
2281 skb_queue_walk(&chan->tx_q, skb) {
2282 if (bt_cb(skb)->l2cap.txseq == control->reqseq ||
2283 skb == chan->tx_send_head)
2287 skb_queue_walk_from(&chan->tx_q, skb) {
2288 if (skb == chan->tx_send_head)
2291 l2cap_seq_list_append(&chan->retrans_list,
2292 bt_cb(skb)->l2cap.txseq);
2295 l2cap_ertm_resend(chan);
2299 static void l2cap_send_ack(struct l2cap_chan *chan)
2301 struct l2cap_ctrl control;
2302 u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
2303 chan->last_acked_seq);
2306 BT_DBG("chan %p last_acked_seq %d buffer_seq %d",
2307 chan, chan->last_acked_seq, chan->buffer_seq);
2309 memset(&control, 0, sizeof(control));
2312 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
2313 chan->rx_state == L2CAP_RX_STATE_RECV) {
2314 __clear_ack_timer(chan);
2315 control.super = L2CAP_SUPER_RNR;
2316 control.reqseq = chan->buffer_seq;
2317 l2cap_send_sframe(chan, &control);
2319 if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) {
2320 l2cap_ertm_send(chan);
2321 /* If any i-frames were sent, they included an ack */
2322 if (chan->buffer_seq == chan->last_acked_seq)
2326 /* Ack now if the window is 3/4ths full.
2327 * Calculate without mul or div
2329 threshold = chan->ack_win;
2330 threshold += threshold << 1;
2333 BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack,
2336 if (frames_to_ack >= threshold) {
2337 __clear_ack_timer(chan);
2338 control.super = L2CAP_SUPER_RR;
2339 control.reqseq = chan->buffer_seq;
2340 l2cap_send_sframe(chan, &control);
2345 __set_ack_timer(chan);
2349 static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan,
2350 struct msghdr *msg, int len,
2351 int count, struct sk_buff *skb)
2353 struct l2cap_conn *conn = chan->conn;
2354 struct sk_buff **frag;
2357 if (!copy_from_iter_full(skb_put(skb, count), count, &msg->msg_iter))
2363 /* Continuation fragments (no L2CAP header) */
2364 frag = &skb_shinfo(skb)->frag_list;
2366 struct sk_buff *tmp;
2368 count = min_t(unsigned int, conn->mtu, len);
2370 tmp = chan->ops->alloc_skb(chan, 0, count,
2371 msg->msg_flags & MSG_DONTWAIT);
2373 return PTR_ERR(tmp);
2377 if (!copy_from_iter_full(skb_put(*frag, count), count,
2384 skb->len += (*frag)->len;
2385 skb->data_len += (*frag)->len;
2387 frag = &(*frag)->next;
2393 static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
2394 struct msghdr *msg, size_t len)
2396 struct l2cap_conn *conn = chan->conn;
2397 struct sk_buff *skb;
2398 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
2399 struct l2cap_hdr *lh;
2401 BT_DBG("chan %p psm 0x%2.2x len %zu", chan,
2402 __le16_to_cpu(chan->psm), len);
2404 count = min_t(unsigned int, (conn->mtu - hlen), len);
2406 skb = chan->ops->alloc_skb(chan, hlen, count,
2407 msg->msg_flags & MSG_DONTWAIT);
2411 /* Create L2CAP header */
2412 lh = skb_put(skb, L2CAP_HDR_SIZE);
2413 lh->cid = cpu_to_le16(chan->dcid);
2414 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
2415 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE));
2417 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2418 if (unlikely(err < 0)) {
2420 return ERR_PTR(err);
2425 static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
2426 struct msghdr *msg, size_t len)
2428 struct l2cap_conn *conn = chan->conn;
2429 struct sk_buff *skb;
2431 struct l2cap_hdr *lh;
2433 BT_DBG("chan %p len %zu", chan, len);
2435 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len);
2437 skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count,
2438 msg->msg_flags & MSG_DONTWAIT);
2442 /* Create L2CAP header */
2443 lh = skb_put(skb, L2CAP_HDR_SIZE);
2444 lh->cid = cpu_to_le16(chan->dcid);
2445 lh->len = cpu_to_le16(len);
2447 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2448 if (unlikely(err < 0)) {
2450 return ERR_PTR(err);
2455 static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
2456 struct msghdr *msg, size_t len,
2459 struct l2cap_conn *conn = chan->conn;
2460 struct sk_buff *skb;
2461 int err, count, hlen;
2462 struct l2cap_hdr *lh;
2464 BT_DBG("chan %p len %zu", chan, len);
2467 return ERR_PTR(-ENOTCONN);
2469 hlen = __ertm_hdr_size(chan);
2472 hlen += L2CAP_SDULEN_SIZE;
2474 if (chan->fcs == L2CAP_FCS_CRC16)
2475 hlen += L2CAP_FCS_SIZE;
2477 count = min_t(unsigned int, (conn->mtu - hlen), len);
2479 skb = chan->ops->alloc_skb(chan, hlen, count,
2480 msg->msg_flags & MSG_DONTWAIT);
2484 /* Create L2CAP header */
2485 lh = skb_put(skb, L2CAP_HDR_SIZE);
2486 lh->cid = cpu_to_le16(chan->dcid);
2487 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
2489 /* Control header is populated later */
2490 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
2491 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
2493 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
2496 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
2498 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2499 if (unlikely(err < 0)) {
2501 return ERR_PTR(err);
2504 bt_cb(skb)->l2cap.fcs = chan->fcs;
2505 bt_cb(skb)->l2cap.retries = 0;
2509 static int l2cap_segment_sdu(struct l2cap_chan *chan,
2510 struct sk_buff_head *seg_queue,
2511 struct msghdr *msg, size_t len)
2513 struct sk_buff *skb;
2518 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
2520 /* It is critical that ERTM PDUs fit in a single HCI fragment,
2521 * so fragmented skbs are not used. The HCI layer's handling
2522 * of fragmented skbs is not compatible with ERTM's queueing.
2525 /* PDU size is derived from the HCI MTU */
2526 pdu_len = chan->conn->mtu;
2528 /* Constrain PDU size for BR/EDR connections */
2530 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
2532 /* Adjust for largest possible L2CAP overhead. */
2534 pdu_len -= L2CAP_FCS_SIZE;
2536 pdu_len -= __ertm_hdr_size(chan);
2538 /* Remote device may have requested smaller PDUs */
2539 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2541 if (len <= pdu_len) {
2542 sar = L2CAP_SAR_UNSEGMENTED;
2546 sar = L2CAP_SAR_START;
2551 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
2554 __skb_queue_purge(seg_queue);
2555 return PTR_ERR(skb);
2558 bt_cb(skb)->l2cap.sar = sar;
2559 __skb_queue_tail(seg_queue, skb);
2565 if (len <= pdu_len) {
2566 sar = L2CAP_SAR_END;
2569 sar = L2CAP_SAR_CONTINUE;
2576 static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan,
2578 size_t len, u16 sdulen)
2580 struct l2cap_conn *conn = chan->conn;
2581 struct sk_buff *skb;
2582 int err, count, hlen;
2583 struct l2cap_hdr *lh;
2585 BT_DBG("chan %p len %zu", chan, len);
2588 return ERR_PTR(-ENOTCONN);
2590 hlen = L2CAP_HDR_SIZE;
2593 hlen += L2CAP_SDULEN_SIZE;
2595 count = min_t(unsigned int, (conn->mtu - hlen), len);
2597 skb = chan->ops->alloc_skb(chan, hlen, count,
2598 msg->msg_flags & MSG_DONTWAIT);
2602 /* Create L2CAP header */
2603 lh = skb_put(skb, L2CAP_HDR_SIZE);
2604 lh->cid = cpu_to_le16(chan->dcid);
2605 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
2608 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
2610 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2611 if (unlikely(err < 0)) {
2613 return ERR_PTR(err);
2619 static int l2cap_segment_le_sdu(struct l2cap_chan *chan,
2620 struct sk_buff_head *seg_queue,
2621 struct msghdr *msg, size_t len)
2623 struct sk_buff *skb;
2627 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
2630 pdu_len = chan->remote_mps - L2CAP_SDULEN_SIZE;
2636 skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len);
2638 __skb_queue_purge(seg_queue);
2639 return PTR_ERR(skb);
2642 __skb_queue_tail(seg_queue, skb);
2648 pdu_len += L2CAP_SDULEN_SIZE;
2655 static void l2cap_le_flowctl_send(struct l2cap_chan *chan)
2659 BT_DBG("chan %p", chan);
2661 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
2662 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
2667 BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits,
2668 skb_queue_len(&chan->tx_q));
2671 int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
2673 struct sk_buff *skb;
2675 struct sk_buff_head seg_queue;
2680 /* Connectionless channel */
2681 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
2682 skb = l2cap_create_connless_pdu(chan, msg, len);
2684 return PTR_ERR(skb);
2686 /* Channel lock is released before requesting new skb and then
2687 * reacquired thus we need to recheck channel state.
2689 if (chan->state != BT_CONNECTED) {
2694 l2cap_do_send(chan, skb);
2698 switch (chan->mode) {
2699 case L2CAP_MODE_LE_FLOWCTL:
2700 case L2CAP_MODE_EXT_FLOWCTL:
2701 /* Check outgoing MTU */
2702 if (len > chan->omtu)
2705 __skb_queue_head_init(&seg_queue);
2707 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len);
2709 if (chan->state != BT_CONNECTED) {
2710 __skb_queue_purge(&seg_queue);
2717 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q);
2719 l2cap_le_flowctl_send(chan);
2721 if (!chan->tx_credits)
2722 chan->ops->suspend(chan);
2728 case L2CAP_MODE_BASIC:
2729 /* Check outgoing MTU */
2730 if (len > chan->omtu)
2733 /* Create a basic PDU */
2734 skb = l2cap_create_basic_pdu(chan, msg, len);
2736 return PTR_ERR(skb);
2738 /* Channel lock is released before requesting new skb and then
2739 * reacquired thus we need to recheck channel state.
2741 if (chan->state != BT_CONNECTED) {
2746 l2cap_do_send(chan, skb);
2750 case L2CAP_MODE_ERTM:
2751 case L2CAP_MODE_STREAMING:
2752 /* Check outgoing MTU */
2753 if (len > chan->omtu) {
2758 __skb_queue_head_init(&seg_queue);
2760 /* Do segmentation before calling in to the state machine,
2761 * since it's possible to block while waiting for memory
2764 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2766 /* The channel could have been closed while segmenting,
2767 * check that it is still connected.
2769 if (chan->state != BT_CONNECTED) {
2770 __skb_queue_purge(&seg_queue);
2777 if (chan->mode == L2CAP_MODE_ERTM)
2778 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST);
2780 l2cap_streaming_send(chan, &seg_queue);
2784 /* If the skbs were not queued for sending, they'll still be in
2785 * seg_queue and need to be purged.
2787 __skb_queue_purge(&seg_queue);
2791 BT_DBG("bad state %1.1x", chan->mode);
2797 EXPORT_SYMBOL_GPL(l2cap_chan_send);
2799 static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq)
2801 struct l2cap_ctrl control;
2804 BT_DBG("chan %p, txseq %u", chan, txseq);
2806 memset(&control, 0, sizeof(control));
2808 control.super = L2CAP_SUPER_SREJ;
2810 for (seq = chan->expected_tx_seq; seq != txseq;
2811 seq = __next_seq(chan, seq)) {
2812 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) {
2813 control.reqseq = seq;
2814 l2cap_send_sframe(chan, &control);
2815 l2cap_seq_list_append(&chan->srej_list, seq);
2819 chan->expected_tx_seq = __next_seq(chan, txseq);
2822 static void l2cap_send_srej_tail(struct l2cap_chan *chan)
2824 struct l2cap_ctrl control;
2826 BT_DBG("chan %p", chan);
2828 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
2831 memset(&control, 0, sizeof(control));
2833 control.super = L2CAP_SUPER_SREJ;
2834 control.reqseq = chan->srej_list.tail;
2835 l2cap_send_sframe(chan, &control);
2838 static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq)
2840 struct l2cap_ctrl control;
2844 BT_DBG("chan %p, txseq %u", chan, txseq);
2846 memset(&control, 0, sizeof(control));
2848 control.super = L2CAP_SUPER_SREJ;
2850 /* Capture initial list head to allow only one pass through the list. */
2851 initial_head = chan->srej_list.head;
2854 seq = l2cap_seq_list_pop(&chan->srej_list);
2855 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR)
2858 control.reqseq = seq;
2859 l2cap_send_sframe(chan, &control);
2860 l2cap_seq_list_append(&chan->srej_list, seq);
2861 } while (chan->srej_list.head != initial_head);
2864 static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq)
2866 struct sk_buff *acked_skb;
2869 BT_DBG("chan %p, reqseq %u", chan, reqseq);
2871 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq)
2874 BT_DBG("expected_ack_seq %u, unacked_frames %u",
2875 chan->expected_ack_seq, chan->unacked_frames);
2877 for (ackseq = chan->expected_ack_seq; ackseq != reqseq;
2878 ackseq = __next_seq(chan, ackseq)) {
2880 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq);
2882 skb_unlink(acked_skb, &chan->tx_q);
2883 kfree_skb(acked_skb);
2884 chan->unacked_frames--;
2888 chan->expected_ack_seq = reqseq;
2890 if (chan->unacked_frames == 0)
2891 __clear_retrans_timer(chan);
2893 BT_DBG("unacked_frames %u", chan->unacked_frames);
2896 static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan)
2898 BT_DBG("chan %p", chan);
2900 chan->expected_tx_seq = chan->buffer_seq;
2901 l2cap_seq_list_clear(&chan->srej_list);
2902 skb_queue_purge(&chan->srej_q);
2903 chan->rx_state = L2CAP_RX_STATE_RECV;
2906 static void l2cap_tx_state_xmit(struct l2cap_chan *chan,
2907 struct l2cap_ctrl *control,
2908 struct sk_buff_head *skbs, u8 event)
2910 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2914 case L2CAP_EV_DATA_REQUEST:
2915 if (chan->tx_send_head == NULL)
2916 chan->tx_send_head = skb_peek(skbs);
2918 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2919 l2cap_ertm_send(chan);
2921 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2922 BT_DBG("Enter LOCAL_BUSY");
2923 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2925 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2926 /* The SREJ_SENT state must be aborted if we are to
2927 * enter the LOCAL_BUSY state.
2929 l2cap_abort_rx_srej_sent(chan);
2932 l2cap_send_ack(chan);
2935 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2936 BT_DBG("Exit LOCAL_BUSY");
2937 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2939 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2940 struct l2cap_ctrl local_control;
2942 memset(&local_control, 0, sizeof(local_control));
2943 local_control.sframe = 1;
2944 local_control.super = L2CAP_SUPER_RR;
2945 local_control.poll = 1;
2946 local_control.reqseq = chan->buffer_seq;
2947 l2cap_send_sframe(chan, &local_control);
2949 chan->retry_count = 1;
2950 __set_monitor_timer(chan);
2951 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2954 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2955 l2cap_process_reqseq(chan, control->reqseq);
2957 case L2CAP_EV_EXPLICIT_POLL:
2958 l2cap_send_rr_or_rnr(chan, 1);
2959 chan->retry_count = 1;
2960 __set_monitor_timer(chan);
2961 __clear_ack_timer(chan);
2962 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2964 case L2CAP_EV_RETRANS_TO:
2965 l2cap_send_rr_or_rnr(chan, 1);
2966 chan->retry_count = 1;
2967 __set_monitor_timer(chan);
2968 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2970 case L2CAP_EV_RECV_FBIT:
2971 /* Nothing to process */
2978 static void l2cap_tx_state_wait_f(struct l2cap_chan *chan,
2979 struct l2cap_ctrl *control,
2980 struct sk_buff_head *skbs, u8 event)
2982 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2986 case L2CAP_EV_DATA_REQUEST:
2987 if (chan->tx_send_head == NULL)
2988 chan->tx_send_head = skb_peek(skbs);
2989 /* Queue data, but don't send. */
2990 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2992 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2993 BT_DBG("Enter LOCAL_BUSY");
2994 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2996 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2997 /* The SREJ_SENT state must be aborted if we are to
2998 * enter the LOCAL_BUSY state.
3000 l2cap_abort_rx_srej_sent(chan);
3003 l2cap_send_ack(chan);
3006 case L2CAP_EV_LOCAL_BUSY_CLEAR:
3007 BT_DBG("Exit LOCAL_BUSY");
3008 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
3010 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
3011 struct l2cap_ctrl local_control;
3012 memset(&local_control, 0, sizeof(local_control));
3013 local_control.sframe = 1;
3014 local_control.super = L2CAP_SUPER_RR;
3015 local_control.poll = 1;
3016 local_control.reqseq = chan->buffer_seq;
3017 l2cap_send_sframe(chan, &local_control);
3019 chan->retry_count = 1;
3020 __set_monitor_timer(chan);
3021 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
3024 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
3025 l2cap_process_reqseq(chan, control->reqseq);
3028 case L2CAP_EV_RECV_FBIT:
3029 if (control && control->final) {
3030 __clear_monitor_timer(chan);
3031 if (chan->unacked_frames > 0)
3032 __set_retrans_timer(chan);
3033 chan->retry_count = 0;
3034 chan->tx_state = L2CAP_TX_STATE_XMIT;
3035 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state);
3038 case L2CAP_EV_EXPLICIT_POLL:
3041 case L2CAP_EV_MONITOR_TO:
3042 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) {
3043 l2cap_send_rr_or_rnr(chan, 1);
3044 __set_monitor_timer(chan);
3045 chan->retry_count++;
3047 l2cap_send_disconn_req(chan, ECONNABORTED);
3055 static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
3056 struct sk_buff_head *skbs, u8 event)
3058 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d",
3059 chan, control, skbs, event, chan->tx_state);
3061 switch (chan->tx_state) {
3062 case L2CAP_TX_STATE_XMIT:
3063 l2cap_tx_state_xmit(chan, control, skbs, event);
3065 case L2CAP_TX_STATE_WAIT_F:
3066 l2cap_tx_state_wait_f(chan, control, skbs, event);
3074 static void l2cap_pass_to_tx(struct l2cap_chan *chan,
3075 struct l2cap_ctrl *control)
3077 BT_DBG("chan %p, control %p", chan, control);
3078 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT);
3081 static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
3082 struct l2cap_ctrl *control)
3084 BT_DBG("chan %p, control %p", chan, control);
3085 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT);
3088 /* Copy frame to all raw sockets on that connection */
3089 static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
3091 struct sk_buff *nskb;
3092 struct l2cap_chan *chan;
3094 BT_DBG("conn %p", conn);
3096 mutex_lock(&conn->chan_lock);
3098 list_for_each_entry(chan, &conn->chan_l, list) {
3099 if (chan->chan_type != L2CAP_CHAN_RAW)
3102 /* Don't send frame to the channel it came from */
3103 if (bt_cb(skb)->l2cap.chan == chan)
3106 nskb = skb_clone(skb, GFP_KERNEL);
3109 if (chan->ops->recv(chan, nskb))
3113 mutex_unlock(&conn->chan_lock);
3116 /* ---- L2CAP signalling commands ---- */
3117 static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,
3118 u8 ident, u16 dlen, void *data)
3120 struct sk_buff *skb, **frag;
3121 struct l2cap_cmd_hdr *cmd;
3122 struct l2cap_hdr *lh;
3125 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
3126 conn, code, ident, dlen);
3128 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
3131 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
3132 count = min_t(unsigned int, conn->mtu, len);
3134 skb = bt_skb_alloc(count, GFP_KERNEL);
3138 lh = skb_put(skb, L2CAP_HDR_SIZE);
3139 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
3141 if (conn->hcon->type == LE_LINK)
3142 lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
3144 lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
3146 cmd = skb_put(skb, L2CAP_CMD_HDR_SIZE);
3149 cmd->len = cpu_to_le16(dlen);
3152 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
3153 skb_put_data(skb, data, count);
3159 /* Continuation fragments (no L2CAP header) */
3160 frag = &skb_shinfo(skb)->frag_list;
3162 count = min_t(unsigned int, conn->mtu, len);
3164 *frag = bt_skb_alloc(count, GFP_KERNEL);
3168 skb_put_data(*frag, data, count);
3173 frag = &(*frag)->next;
3183 static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
3186 struct l2cap_conf_opt *opt = *ptr;
3189 len = L2CAP_CONF_OPT_SIZE + opt->len;
3197 *val = *((u8 *) opt->val);
3201 *val = get_unaligned_le16(opt->val);
3205 *val = get_unaligned_le32(opt->val);
3209 *val = (unsigned long) opt->val;
3213 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val);
3217 static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val, size_t size)
3219 struct l2cap_conf_opt *opt = *ptr;
3221 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val);
3223 if (size < L2CAP_CONF_OPT_SIZE + len)
3231 *((u8 *) opt->val) = val;
3235 put_unaligned_le16(val, opt->val);
3239 put_unaligned_le32(val, opt->val);
3243 memcpy(opt->val, (void *) val, len);
3247 *ptr += L2CAP_CONF_OPT_SIZE + len;
3250 static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan, size_t size)
3252 struct l2cap_conf_efs efs;
3254 switch (chan->mode) {
3255 case L2CAP_MODE_ERTM:
3256 efs.id = chan->local_id;
3257 efs.stype = chan->local_stype;
3258 efs.msdu = cpu_to_le16(chan->local_msdu);
3259 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
3260 efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
3261 efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO);
3264 case L2CAP_MODE_STREAMING:
3266 efs.stype = L2CAP_SERV_BESTEFFORT;
3267 efs.msdu = cpu_to_le16(chan->local_msdu);
3268 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
3277 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
3278 (unsigned long) &efs, size);
3281 static void l2cap_ack_timeout(struct work_struct *work)
3283 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
3287 BT_DBG("chan %p", chan);
3289 l2cap_chan_lock(chan);
3291 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
3292 chan->last_acked_seq);
3295 l2cap_send_rr_or_rnr(chan, 0);
3297 l2cap_chan_unlock(chan);
3298 l2cap_chan_put(chan);
3301 int l2cap_ertm_init(struct l2cap_chan *chan)
3305 chan->next_tx_seq = 0;
3306 chan->expected_tx_seq = 0;
3307 chan->expected_ack_seq = 0;
3308 chan->unacked_frames = 0;
3309 chan->buffer_seq = 0;
3310 chan->frames_sent = 0;
3311 chan->last_acked_seq = 0;
3313 chan->sdu_last_frag = NULL;
3316 skb_queue_head_init(&chan->tx_q);
3318 chan->local_amp_id = AMP_ID_BREDR;
3319 chan->move_id = AMP_ID_BREDR;
3320 chan->move_state = L2CAP_MOVE_STABLE;
3321 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3323 if (chan->mode != L2CAP_MODE_ERTM)
3326 chan->rx_state = L2CAP_RX_STATE_RECV;
3327 chan->tx_state = L2CAP_TX_STATE_XMIT;
3329 skb_queue_head_init(&chan->srej_q);
3331 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
3335 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
3337 l2cap_seq_list_free(&chan->srej_list);
3342 static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3345 case L2CAP_MODE_STREAMING:
3346 case L2CAP_MODE_ERTM:
3347 if (l2cap_mode_supported(mode, remote_feat_mask))
3351 return L2CAP_MODE_BASIC;
3355 static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
3357 return ((conn->local_fixed_chan & L2CAP_FC_A2MP) &&
3358 (conn->feat_mask & L2CAP_FEAT_EXT_WINDOW));
3361 static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
3363 return ((conn->local_fixed_chan & L2CAP_FC_A2MP) &&
3364 (conn->feat_mask & L2CAP_FEAT_EXT_FLOW));
3367 static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3368 struct l2cap_conf_rfc *rfc)
3370 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
3371 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3373 /* Class 1 devices have must have ERTM timeouts
3374 * exceeding the Link Supervision Timeout. The
3375 * default Link Supervision Timeout for AMP
3376 * controllers is 10 seconds.
3378 * Class 1 devices use 0xffffffff for their
3379 * best-effort flush timeout, so the clamping logic
3380 * will result in a timeout that meets the above
3381 * requirement. ERTM timeouts are 16-bit values, so
3382 * the maximum timeout is 65.535 seconds.
3385 /* Convert timeout to milliseconds and round */
3386 ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000);
3388 /* This is the recommended formula for class 2 devices
3389 * that start ERTM timers when packets are sent to the
3392 ertm_to = 3 * ertm_to + 500;
3394 if (ertm_to > 0xffff)
3397 rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
3398 rfc->monitor_timeout = rfc->retrans_timeout;
3400 rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
3401 rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
3405 static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3407 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
3408 __l2cap_ews_supported(chan->conn)) {
3409 /* use extended control field */
3410 set_bit(FLAG_EXT_CTRL, &chan->flags);
3411 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3413 chan->tx_win = min_t(u16, chan->tx_win,
3414 L2CAP_DEFAULT_TX_WINDOW);
3415 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
3417 chan->ack_win = chan->tx_win;
3420 static void l2cap_mtu_auto(struct l2cap_chan *chan)
3422 struct hci_conn *conn = chan->conn->hcon;
3424 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
3426 /* The 2-DH1 packet has between 2 and 56 information bytes
3427 * (including the 2-byte payload header)
3429 if (!(conn->pkt_type & HCI_2DH1))
3432 /* The 3-DH1 packet has between 2 and 85 information bytes
3433 * (including the 2-byte payload header)
3435 if (!(conn->pkt_type & HCI_3DH1))
3438 /* The 2-DH3 packet has between 2 and 369 information bytes
3439 * (including the 2-byte payload header)
3441 if (!(conn->pkt_type & HCI_2DH3))
3444 /* The 3-DH3 packet has between 2 and 554 information bytes
3445 * (including the 2-byte payload header)
3447 if (!(conn->pkt_type & HCI_3DH3))
3450 /* The 2-DH5 packet has between 2 and 681 information bytes
3451 * (including the 2-byte payload header)
3453 if (!(conn->pkt_type & HCI_2DH5))
3456 /* The 3-DH5 packet has between 2 and 1023 information bytes
3457 * (including the 2-byte payload header)
3459 if (!(conn->pkt_type & HCI_3DH5))
3463 static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data_size)
3465 struct l2cap_conf_req *req = data;
3466 struct l2cap_conf_rfc rfc = { .mode = chan->mode };
3467 void *ptr = req->data;
3468 void *endptr = data + data_size;
3471 BT_DBG("chan %p", chan);
3473 if (chan->num_conf_req || chan->num_conf_rsp)
3476 switch (chan->mode) {
3477 case L2CAP_MODE_STREAMING:
3478 case L2CAP_MODE_ERTM:
3479 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
3482 if (__l2cap_efs_supported(chan->conn))
3483 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3487 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
3492 if (chan->imtu != L2CAP_DEFAULT_MTU) {
3494 l2cap_mtu_auto(chan);
3495 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu,
3499 switch (chan->mode) {
3500 case L2CAP_MODE_BASIC:
3504 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
3505 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
3508 rfc.mode = L2CAP_MODE_BASIC;
3510 rfc.max_transmit = 0;
3511 rfc.retrans_timeout = 0;
3512 rfc.monitor_timeout = 0;
3513 rfc.max_pdu_size = 0;
3515 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3516 (unsigned long) &rfc, endptr - ptr);
3519 case L2CAP_MODE_ERTM:
3520 rfc.mode = L2CAP_MODE_ERTM;
3521 rfc.max_transmit = chan->max_tx;
3523 __l2cap_set_ertm_timeouts(chan, &rfc);
3525 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
3526 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3528 rfc.max_pdu_size = cpu_to_le16(size);
3530 l2cap_txwin_setup(chan);
3532 rfc.txwin_size = min_t(u16, chan->tx_win,
3533 L2CAP_DEFAULT_TX_WINDOW);
3535 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3536 (unsigned long) &rfc, endptr - ptr);
3538 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3539 l2cap_add_opt_efs(&ptr, chan, endptr - ptr);
3541 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3542 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
3543 chan->tx_win, endptr - ptr);
3545 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3546 if (chan->fcs == L2CAP_FCS_NONE ||
3547 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
3548 chan->fcs = L2CAP_FCS_NONE;
3549 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3550 chan->fcs, endptr - ptr);
3554 case L2CAP_MODE_STREAMING:
3555 l2cap_txwin_setup(chan);
3556 rfc.mode = L2CAP_MODE_STREAMING;
3558 rfc.max_transmit = 0;
3559 rfc.retrans_timeout = 0;
3560 rfc.monitor_timeout = 0;
3562 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
3563 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3565 rfc.max_pdu_size = cpu_to_le16(size);
3567 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3568 (unsigned long) &rfc, endptr - ptr);
3570 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3571 l2cap_add_opt_efs(&ptr, chan, endptr - ptr);
3573 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3574 if (chan->fcs == L2CAP_FCS_NONE ||
3575 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
3576 chan->fcs = L2CAP_FCS_NONE;
3577 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3578 chan->fcs, endptr - ptr);
3583 req->dcid = cpu_to_le16(chan->dcid);
3584 req->flags = cpu_to_le16(0);
3589 static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data_size)
3591 struct l2cap_conf_rsp *rsp = data;
3592 void *ptr = rsp->data;
3593 void *endptr = data + data_size;
3594 void *req = chan->conf_req;
3595 int len = chan->conf_len;
3596 int type, hint, olen;
3598 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
3599 struct l2cap_conf_efs efs;
3601 u16 mtu = L2CAP_DEFAULT_MTU;
3602 u16 result = L2CAP_CONF_SUCCESS;
3605 BT_DBG("chan %p", chan);
3607 while (len >= L2CAP_CONF_OPT_SIZE) {
3608 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
3612 hint = type & L2CAP_CONF_HINT;
3613 type &= L2CAP_CONF_MASK;
3616 case L2CAP_CONF_MTU:
3622 case L2CAP_CONF_FLUSH_TO:
3625 chan->flush_to = val;
3628 case L2CAP_CONF_QOS:
3631 case L2CAP_CONF_RFC:
3632 if (olen != sizeof(rfc))
3634 memcpy(&rfc, (void *) val, olen);
3637 case L2CAP_CONF_FCS:
3640 if (val == L2CAP_FCS_NONE)
3641 set_bit(CONF_RECV_NO_FCS, &chan->conf_state);
3644 case L2CAP_CONF_EFS:
3645 if (olen != sizeof(efs))
3648 memcpy(&efs, (void *) val, olen);
3651 case L2CAP_CONF_EWS:
3654 if (!(chan->conn->local_fixed_chan & L2CAP_FC_A2MP))
3655 return -ECONNREFUSED;
3656 set_bit(FLAG_EXT_CTRL, &chan->flags);
3657 set_bit(CONF_EWS_RECV, &chan->conf_state);
3658 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3659 chan->remote_tx_win = val;
3665 result = L2CAP_CONF_UNKNOWN;
3666 l2cap_add_conf_opt(&ptr, (u8)type, sizeof(u8), type, endptr - ptr);
3671 if (chan->num_conf_rsp || chan->num_conf_req > 1)
3674 switch (chan->mode) {
3675 case L2CAP_MODE_STREAMING:
3676 case L2CAP_MODE_ERTM:
3677 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
3678 chan->mode = l2cap_select_mode(rfc.mode,
3679 chan->conn->feat_mask);
3684 if (__l2cap_efs_supported(chan->conn))
3685 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3687 return -ECONNREFUSED;
3690 if (chan->mode != rfc.mode)
3691 return -ECONNREFUSED;
3697 if (chan->mode != rfc.mode) {
3698 result = L2CAP_CONF_UNACCEPT;
3699 rfc.mode = chan->mode;
3701 if (chan->num_conf_rsp == 1)
3702 return -ECONNREFUSED;
3704 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3705 (unsigned long) &rfc, endptr - ptr);
3708 if (result == L2CAP_CONF_SUCCESS) {
3709 /* Configure output options and let the other side know
3710 * which ones we don't like. */
3712 if (mtu < L2CAP_DEFAULT_MIN_MTU)
3713 result = L2CAP_CONF_UNACCEPT;
3716 set_bit(CONF_MTU_DONE, &chan->conf_state);
3718 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu, endptr - ptr);
3721 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
3722 efs.stype != L2CAP_SERV_NOTRAFIC &&
3723 efs.stype != chan->local_stype) {
3725 result = L2CAP_CONF_UNACCEPT;
3727 if (chan->num_conf_req >= 1)
3728 return -ECONNREFUSED;
3730 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
3732 (unsigned long) &efs, endptr - ptr);
3734 /* Send PENDING Conf Rsp */
3735 result = L2CAP_CONF_PENDING;
3736 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
3741 case L2CAP_MODE_BASIC:
3742 chan->fcs = L2CAP_FCS_NONE;
3743 set_bit(CONF_MODE_DONE, &chan->conf_state);
3746 case L2CAP_MODE_ERTM:
3747 if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
3748 chan->remote_tx_win = rfc.txwin_size;
3750 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
3752 chan->remote_max_tx = rfc.max_transmit;
3754 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
3755 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3756 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
3757 rfc.max_pdu_size = cpu_to_le16(size);
3758 chan->remote_mps = size;
3760 __l2cap_set_ertm_timeouts(chan, &rfc);
3762 set_bit(CONF_MODE_DONE, &chan->conf_state);
3764 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
3765 sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
3768 test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3769 chan->remote_id = efs.id;
3770 chan->remote_stype = efs.stype;
3771 chan->remote_msdu = le16_to_cpu(efs.msdu);
3772 chan->remote_flush_to =
3773 le32_to_cpu(efs.flush_to);
3774 chan->remote_acc_lat =
3775 le32_to_cpu(efs.acc_lat);
3776 chan->remote_sdu_itime =
3777 le32_to_cpu(efs.sdu_itime);
3778 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
3780 (unsigned long) &efs, endptr - ptr);
3784 case L2CAP_MODE_STREAMING:
3785 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
3786 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3787 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
3788 rfc.max_pdu_size = cpu_to_le16(size);
3789 chan->remote_mps = size;
3791 set_bit(CONF_MODE_DONE, &chan->conf_state);
3793 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3794 (unsigned long) &rfc, endptr - ptr);
3799 result = L2CAP_CONF_UNACCEPT;
3801 memset(&rfc, 0, sizeof(rfc));
3802 rfc.mode = chan->mode;
3805 if (result == L2CAP_CONF_SUCCESS)
3806 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
3808 rsp->scid = cpu_to_le16(chan->dcid);
3809 rsp->result = cpu_to_le16(result);
3810 rsp->flags = cpu_to_le16(0);
3815 static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
3816 void *data, size_t size, u16 *result)
3818 struct l2cap_conf_req *req = data;
3819 void *ptr = req->data;
3820 void *endptr = data + size;
3823 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
3824 struct l2cap_conf_efs efs;
3826 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
3828 while (len >= L2CAP_CONF_OPT_SIZE) {
3829 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3834 case L2CAP_CONF_MTU:
3837 if (val < L2CAP_DEFAULT_MIN_MTU) {
3838 *result = L2CAP_CONF_UNACCEPT;
3839 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
3842 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu,
3846 case L2CAP_CONF_FLUSH_TO:
3849 chan->flush_to = val;
3850 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2,
3851 chan->flush_to, endptr - ptr);
3854 case L2CAP_CONF_RFC:
3855 if (olen != sizeof(rfc))
3857 memcpy(&rfc, (void *)val, olen);
3858 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
3859 rfc.mode != chan->mode)
3860 return -ECONNREFUSED;
3862 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3863 (unsigned long) &rfc, endptr - ptr);
3866 case L2CAP_CONF_EWS:
3869 chan->ack_win = min_t(u16, val, chan->ack_win);
3870 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
3871 chan->tx_win, endptr - ptr);
3874 case L2CAP_CONF_EFS:
3875 if (olen != sizeof(efs))
3877 memcpy(&efs, (void *)val, olen);
3878 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
3879 efs.stype != L2CAP_SERV_NOTRAFIC &&
3880 efs.stype != chan->local_stype)
3881 return -ECONNREFUSED;
3882 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs),
3883 (unsigned long) &efs, endptr - ptr);
3886 case L2CAP_CONF_FCS:
3889 if (*result == L2CAP_CONF_PENDING)
3890 if (val == L2CAP_FCS_NONE)
3891 set_bit(CONF_RECV_NO_FCS,
3897 if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
3898 return -ECONNREFUSED;
3900 chan->mode = rfc.mode;
3902 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
3904 case L2CAP_MODE_ERTM:
3905 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3906 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
3907 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3908 if (!test_bit(FLAG_EXT_CTRL, &chan->flags))
3909 chan->ack_win = min_t(u16, chan->ack_win,
3912 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3913 chan->local_msdu = le16_to_cpu(efs.msdu);
3914 chan->local_sdu_itime =
3915 le32_to_cpu(efs.sdu_itime);
3916 chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
3917 chan->local_flush_to =
3918 le32_to_cpu(efs.flush_to);
3922 case L2CAP_MODE_STREAMING:
3923 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3927 req->dcid = cpu_to_le16(chan->dcid);
3928 req->flags = cpu_to_le16(0);
3933 static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
3934 u16 result, u16 flags)
3936 struct l2cap_conf_rsp *rsp = data;
3937 void *ptr = rsp->data;
3939 BT_DBG("chan %p", chan);
3941 rsp->scid = cpu_to_le16(chan->dcid);
3942 rsp->result = cpu_to_le16(result);
3943 rsp->flags = cpu_to_le16(flags);
3948 void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
3950 struct l2cap_le_conn_rsp rsp;
3951 struct l2cap_conn *conn = chan->conn;
3953 BT_DBG("chan %p", chan);
3955 rsp.dcid = cpu_to_le16(chan->scid);
3956 rsp.mtu = cpu_to_le16(chan->imtu);
3957 rsp.mps = cpu_to_le16(chan->mps);
3958 rsp.credits = cpu_to_le16(chan->rx_credits);
3959 rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS);
3961 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
3965 void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
3968 struct l2cap_ecred_conn_rsp rsp;
3971 struct l2cap_conn *conn = chan->conn;
3972 u16 ident = chan->ident;
3978 BT_DBG("chan %p ident %d", chan, ident);
3980 pdu.rsp.mtu = cpu_to_le16(chan->imtu);
3981 pdu.rsp.mps = cpu_to_le16(chan->mps);
3982 pdu.rsp.credits = cpu_to_le16(chan->rx_credits);
3983 pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS);
3985 mutex_lock(&conn->chan_lock);
3987 list_for_each_entry(chan, &conn->chan_l, list) {
3988 if (chan->ident != ident)
3991 /* Reset ident so only one response is sent */
3994 /* Include all channels pending with the same ident */
3995 pdu.dcid[i++] = cpu_to_le16(chan->scid);
3998 mutex_unlock(&conn->chan_lock);
4000 l2cap_send_cmd(conn, ident, L2CAP_ECRED_CONN_RSP,
4001 sizeof(pdu.rsp) + i * sizeof(__le16), &pdu);
4004 void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
4006 struct l2cap_conn_rsp rsp;
4007 struct l2cap_conn *conn = chan->conn;
4011 rsp.scid = cpu_to_le16(chan->dcid);
4012 rsp.dcid = cpu_to_le16(chan->scid);
4013 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
4014 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4017 rsp_code = L2CAP_CREATE_CHAN_RSP;
4019 rsp_code = L2CAP_CONN_RSP;
4021 BT_DBG("chan %p rsp_code %u", chan, rsp_code);
4023 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp);
4025 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
4028 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
4029 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
4030 chan->num_conf_req++;
4033 static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
4037 /* Use sane default values in case a misbehaving remote device
4038 * did not send an RFC or extended window size option.
4040 u16 txwin_ext = chan->ack_win;
4041 struct l2cap_conf_rfc rfc = {
4043 .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO),
4044 .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO),
4045 .max_pdu_size = cpu_to_le16(chan->imtu),
4046 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW),
4049 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
4051 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
4054 while (len >= L2CAP_CONF_OPT_SIZE) {
4055 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
4060 case L2CAP_CONF_RFC:
4061 if (olen != sizeof(rfc))
4063 memcpy(&rfc, (void *)val, olen);
4065 case L2CAP_CONF_EWS:
4074 case L2CAP_MODE_ERTM:
4075 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
4076 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
4077 chan->mps = le16_to_cpu(rfc.max_pdu_size);
4078 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
4079 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext);
4081 chan->ack_win = min_t(u16, chan->ack_win,
4084 case L2CAP_MODE_STREAMING:
4085 chan->mps = le16_to_cpu(rfc.max_pdu_size);
4089 static inline int l2cap_command_rej(struct l2cap_conn *conn,
4090 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4093 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
4095 if (cmd_len < sizeof(*rej))
4098 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
4101 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
4102 cmd->ident == conn->info_ident) {
4103 cancel_delayed_work(&conn->info_timer);
4105 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4106 conn->info_ident = 0;
4108 l2cap_conn_start(conn);
4114 static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
4115 struct l2cap_cmd_hdr *cmd,
4116 u8 *data, u8 rsp_code, u8 amp_id)
4118 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
4119 struct l2cap_conn_rsp rsp;
4120 struct l2cap_chan *chan = NULL, *pchan;
4121 int result, status = L2CAP_CS_NO_INFO;
4123 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
4124 __le16 psm = req->psm;
4126 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
4128 /* Check if we have socket listening on psm */
4129 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
4130 &conn->hcon->dst, ACL_LINK);
4132 result = L2CAP_CR_BAD_PSM;
4136 mutex_lock(&conn->chan_lock);
4137 l2cap_chan_lock(pchan);
4139 /* Check if the ACL is secure enough (if not SDP) */
4140 if (psm != cpu_to_le16(L2CAP_PSM_SDP) &&
4141 !hci_conn_check_link_mode(conn->hcon)) {
4142 conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
4143 result = L2CAP_CR_SEC_BLOCK;
4147 result = L2CAP_CR_NO_MEM;
4149 /* Check for valid dynamic CID range (as per Erratum 3253) */
4150 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_DYN_END) {
4151 result = L2CAP_CR_INVALID_SCID;
4155 /* Check if we already have channel with that dcid */
4156 if (__l2cap_get_chan_by_dcid(conn, scid)) {
4157 result = L2CAP_CR_SCID_IN_USE;
4161 chan = pchan->ops->new_connection(pchan);
4165 /* For certain devices (ex: HID mouse), support for authentication,
4166 * pairing and bonding is optional. For such devices, inorder to avoid
4167 * the ACL alive for too long after L2CAP disconnection, reset the ACL
4168 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect.
4170 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
4172 bacpy(&chan->src, &conn->hcon->src);
4173 bacpy(&chan->dst, &conn->hcon->dst);
4174 chan->src_type = bdaddr_src_type(conn->hcon);
4175 chan->dst_type = bdaddr_dst_type(conn->hcon);
4178 chan->local_amp_id = amp_id;
4180 __l2cap_chan_add(conn, chan);
4184 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
4186 chan->ident = cmd->ident;
4188 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
4189 if (l2cap_chan_check_security(chan, false)) {
4190 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
4191 l2cap_state_change(chan, BT_CONNECT2);
4192 result = L2CAP_CR_PEND;
4193 status = L2CAP_CS_AUTHOR_PEND;
4194 chan->ops->defer(chan);
4196 /* Force pending result for AMP controllers.
4197 * The connection will succeed after the
4198 * physical link is up.
4200 if (amp_id == AMP_ID_BREDR) {
4201 l2cap_state_change(chan, BT_CONFIG);
4202 result = L2CAP_CR_SUCCESS;
4204 l2cap_state_change(chan, BT_CONNECT2);
4205 result = L2CAP_CR_PEND;
4207 status = L2CAP_CS_NO_INFO;
4210 l2cap_state_change(chan, BT_CONNECT2);
4211 result = L2CAP_CR_PEND;
4212 status = L2CAP_CS_AUTHEN_PEND;
4215 l2cap_state_change(chan, BT_CONNECT2);
4216 result = L2CAP_CR_PEND;
4217 status = L2CAP_CS_NO_INFO;
4221 l2cap_chan_unlock(pchan);
4222 mutex_unlock(&conn->chan_lock);
4223 l2cap_chan_put(pchan);
4226 rsp.scid = cpu_to_le16(scid);
4227 rsp.dcid = cpu_to_le16(dcid);
4228 rsp.result = cpu_to_le16(result);
4229 rsp.status = cpu_to_le16(status);
4230 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
4232 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
4233 struct l2cap_info_req info;
4234 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
4236 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
4237 conn->info_ident = l2cap_get_ident(conn);
4239 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
4241 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
4242 sizeof(info), &info);
4245 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
4246 result == L2CAP_CR_SUCCESS) {
4248 set_bit(CONF_REQ_SENT, &chan->conf_state);
4249 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
4250 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
4251 chan->num_conf_req++;
4257 static int l2cap_connect_req(struct l2cap_conn *conn,
4258 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
4260 struct hci_dev *hdev = conn->hcon->hdev;
4261 struct hci_conn *hcon = conn->hcon;
4263 if (cmd_len < sizeof(struct l2cap_conn_req))
4267 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
4268 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
4269 mgmt_device_connected(hdev, hcon, NULL, 0);
4270 hci_dev_unlock(hdev);
4272 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0);
4276 static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
4277 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4280 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
4281 u16 scid, dcid, result, status;
4282 struct l2cap_chan *chan;
4286 if (cmd_len < sizeof(*rsp))
4289 scid = __le16_to_cpu(rsp->scid);
4290 dcid = __le16_to_cpu(rsp->dcid);
4291 result = __le16_to_cpu(rsp->result);
4292 status = __le16_to_cpu(rsp->status);
4294 BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x",
4295 dcid, scid, result, status);
4297 mutex_lock(&conn->chan_lock);
4300 chan = __l2cap_get_chan_by_scid(conn, scid);
4306 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
4313 chan = l2cap_chan_hold_unless_zero(chan);
4321 l2cap_chan_lock(chan);
4324 case L2CAP_CR_SUCCESS:
4325 l2cap_state_change(chan, BT_CONFIG);
4328 clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
4330 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
4333 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
4334 l2cap_build_conf_req(chan, req, sizeof(req)), req);
4335 chan->num_conf_req++;
4339 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
4343 l2cap_chan_del(chan, ECONNREFUSED);
4347 l2cap_chan_unlock(chan);
4348 l2cap_chan_put(chan);
4351 mutex_unlock(&conn->chan_lock);
4356 static inline void set_default_fcs(struct l2cap_chan *chan)
4358 /* FCS is enabled only in ERTM or streaming mode, if one or both
4361 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
4362 chan->fcs = L2CAP_FCS_NONE;
4363 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state))
4364 chan->fcs = L2CAP_FCS_CRC16;
4367 static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
4368 u8 ident, u16 flags)
4370 struct l2cap_conn *conn = chan->conn;
4372 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident,
4375 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
4376 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
4378 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP,
4379 l2cap_build_conf_rsp(chan, data,
4380 L2CAP_CONF_SUCCESS, flags), data);
4383 static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
4386 struct l2cap_cmd_rej_cid rej;
4388 rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
4389 rej.scid = __cpu_to_le16(scid);
4390 rej.dcid = __cpu_to_le16(dcid);
4392 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
4395 static inline int l2cap_config_req(struct l2cap_conn *conn,
4396 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4399 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
4402 struct l2cap_chan *chan;
4405 if (cmd_len < sizeof(*req))
4408 dcid = __le16_to_cpu(req->dcid);
4409 flags = __le16_to_cpu(req->flags);
4411 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
4413 chan = l2cap_get_chan_by_scid(conn, dcid);
4415 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
4419 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2 &&
4420 chan->state != BT_CONNECTED) {
4421 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4426 /* Reject if config buffer is too small. */
4427 len = cmd_len - sizeof(*req);
4428 if (chan->conf_len + len > sizeof(chan->conf_req)) {
4429 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
4430 l2cap_build_conf_rsp(chan, rsp,
4431 L2CAP_CONF_REJECT, flags), rsp);
4436 memcpy(chan->conf_req + chan->conf_len, req->data, len);
4437 chan->conf_len += len;
4439 if (flags & L2CAP_CONF_FLAG_CONTINUATION) {
4440 /* Incomplete config. Send empty response. */
4441 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
4442 l2cap_build_conf_rsp(chan, rsp,
4443 L2CAP_CONF_SUCCESS, flags), rsp);
4447 /* Complete config. */
4448 len = l2cap_parse_conf_req(chan, rsp, sizeof(rsp));
4450 l2cap_send_disconn_req(chan, ECONNRESET);
4454 chan->ident = cmd->ident;
4455 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
4456 chan->num_conf_rsp++;
4458 /* Reset config buffer. */
4461 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
4464 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
4465 set_default_fcs(chan);
4467 if (chan->mode == L2CAP_MODE_ERTM ||
4468 chan->mode == L2CAP_MODE_STREAMING)
4469 err = l2cap_ertm_init(chan);
4472 l2cap_send_disconn_req(chan, -err);
4474 l2cap_chan_ready(chan);
4479 if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
4481 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
4482 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
4483 chan->num_conf_req++;
4486 /* Got Conf Rsp PENDING from remote side and assume we sent
4487 Conf Rsp PENDING in the code above */
4488 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
4489 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4491 /* check compatibility */
4493 /* Send rsp for BR/EDR channel */
4495 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags);
4497 chan->ident = cmd->ident;
4501 l2cap_chan_unlock(chan);
4502 l2cap_chan_put(chan);
4506 static inline int l2cap_config_rsp(struct l2cap_conn *conn,
4507 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4510 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
4511 u16 scid, flags, result;
4512 struct l2cap_chan *chan;
4513 int len = cmd_len - sizeof(*rsp);
4516 if (cmd_len < sizeof(*rsp))
4519 scid = __le16_to_cpu(rsp->scid);
4520 flags = __le16_to_cpu(rsp->flags);
4521 result = __le16_to_cpu(rsp->result);
4523 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
4526 chan = l2cap_get_chan_by_scid(conn, scid);
4531 case L2CAP_CONF_SUCCESS:
4532 l2cap_conf_rfc_get(chan, rsp->data, len);
4533 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4536 case L2CAP_CONF_PENDING:
4537 set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4539 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4542 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
4543 buf, sizeof(buf), &result);
4545 l2cap_send_disconn_req(chan, ECONNRESET);
4549 if (!chan->hs_hcon) {
4550 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident,
4553 if (l2cap_check_efs(chan)) {
4554 amp_create_logical_link(chan);
4555 chan->ident = cmd->ident;
4561 case L2CAP_CONF_UNKNOWN:
4562 case L2CAP_CONF_UNACCEPT:
4563 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
4566 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
4567 l2cap_send_disconn_req(chan, ECONNRESET);
4571 /* throw out any old stored conf requests */
4572 result = L2CAP_CONF_SUCCESS;
4573 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
4574 req, sizeof(req), &result);
4576 l2cap_send_disconn_req(chan, ECONNRESET);
4580 l2cap_send_cmd(conn, l2cap_get_ident(conn),
4581 L2CAP_CONF_REQ, len, req);
4582 chan->num_conf_req++;
4583 if (result != L2CAP_CONF_SUCCESS)
4590 l2cap_chan_set_err(chan, ECONNRESET);
4592 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
4593 l2cap_send_disconn_req(chan, ECONNRESET);
4597 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
4600 set_bit(CONF_INPUT_DONE, &chan->conf_state);
4602 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
4603 set_default_fcs(chan);
4605 if (chan->mode == L2CAP_MODE_ERTM ||
4606 chan->mode == L2CAP_MODE_STREAMING)
4607 err = l2cap_ertm_init(chan);
4610 l2cap_send_disconn_req(chan, -err);
4612 l2cap_chan_ready(chan);
4616 l2cap_chan_unlock(chan);
4617 l2cap_chan_put(chan);
4621 static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
4622 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4625 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
4626 struct l2cap_disconn_rsp rsp;
4628 struct l2cap_chan *chan;
4630 if (cmd_len != sizeof(*req))
4633 scid = __le16_to_cpu(req->scid);
4634 dcid = __le16_to_cpu(req->dcid);
4636 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
4638 mutex_lock(&conn->chan_lock);
4640 chan = __l2cap_get_chan_by_scid(conn, dcid);
4642 mutex_unlock(&conn->chan_lock);
4643 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4647 l2cap_chan_hold(chan);
4648 l2cap_chan_lock(chan);
4650 rsp.dcid = cpu_to_le16(chan->scid);
4651 rsp.scid = cpu_to_le16(chan->dcid);
4652 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4654 chan->ops->set_shutdown(chan);
4656 l2cap_chan_del(chan, ECONNRESET);
4658 chan->ops->close(chan);
4660 l2cap_chan_unlock(chan);
4661 l2cap_chan_put(chan);
4663 mutex_unlock(&conn->chan_lock);
4668 static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
4669 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4672 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
4674 struct l2cap_chan *chan;
4676 if (cmd_len != sizeof(*rsp))
4679 scid = __le16_to_cpu(rsp->scid);
4680 dcid = __le16_to_cpu(rsp->dcid);
4682 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
4684 mutex_lock(&conn->chan_lock);
4686 chan = __l2cap_get_chan_by_scid(conn, scid);
4688 mutex_unlock(&conn->chan_lock);
4692 l2cap_chan_hold(chan);
4693 l2cap_chan_lock(chan);
4695 if (chan->state != BT_DISCONN) {
4696 l2cap_chan_unlock(chan);
4697 l2cap_chan_put(chan);
4698 mutex_unlock(&conn->chan_lock);
4702 l2cap_chan_del(chan, 0);
4704 chan->ops->close(chan);
4706 l2cap_chan_unlock(chan);
4707 l2cap_chan_put(chan);
4709 mutex_unlock(&conn->chan_lock);
4714 static inline int l2cap_information_req(struct l2cap_conn *conn,
4715 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4718 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
4721 if (cmd_len != sizeof(*req))
4724 type = __le16_to_cpu(req->type);
4726 BT_DBG("type 0x%4.4x", type);
4728 if (type == L2CAP_IT_FEAT_MASK) {
4730 u32 feat_mask = l2cap_feat_mask;
4731 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
4732 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
4733 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
4735 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
4737 if (conn->local_fixed_chan & L2CAP_FC_A2MP)
4738 feat_mask |= L2CAP_FEAT_EXT_FLOW
4739 | L2CAP_FEAT_EXT_WINDOW;
4741 put_unaligned_le32(feat_mask, rsp->data);
4742 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4744 } else if (type == L2CAP_IT_FIXED_CHAN) {
4746 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
4748 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4749 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
4750 rsp->data[0] = conn->local_fixed_chan;
4751 memset(rsp->data + 1, 0, 7);
4752 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4755 struct l2cap_info_rsp rsp;
4756 rsp.type = cpu_to_le16(type);
4757 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
4758 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp),
4765 static inline int l2cap_information_rsp(struct l2cap_conn *conn,
4766 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4769 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
4772 if (cmd_len < sizeof(*rsp))
4775 type = __le16_to_cpu(rsp->type);
4776 result = __le16_to_cpu(rsp->result);
4778 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
4780 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
4781 if (cmd->ident != conn->info_ident ||
4782 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
4785 cancel_delayed_work(&conn->info_timer);
4787 if (result != L2CAP_IR_SUCCESS) {
4788 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4789 conn->info_ident = 0;
4791 l2cap_conn_start(conn);
4797 case L2CAP_IT_FEAT_MASK:
4798 conn->feat_mask = get_unaligned_le32(rsp->data);
4800 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
4801 struct l2cap_info_req req;
4802 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4804 conn->info_ident = l2cap_get_ident(conn);
4806 l2cap_send_cmd(conn, conn->info_ident,
4807 L2CAP_INFO_REQ, sizeof(req), &req);
4809 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4810 conn->info_ident = 0;
4812 l2cap_conn_start(conn);
4816 case L2CAP_IT_FIXED_CHAN:
4817 conn->remote_fixed_chan = rsp->data[0];
4818 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4819 conn->info_ident = 0;
4821 l2cap_conn_start(conn);
4828 static int l2cap_create_channel_req(struct l2cap_conn *conn,
4829 struct l2cap_cmd_hdr *cmd,
4830 u16 cmd_len, void *data)
4832 struct l2cap_create_chan_req *req = data;
4833 struct l2cap_create_chan_rsp rsp;
4834 struct l2cap_chan *chan;
4835 struct hci_dev *hdev;
4838 if (cmd_len != sizeof(*req))
4841 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
4844 psm = le16_to_cpu(req->psm);
4845 scid = le16_to_cpu(req->scid);
4847 BT_DBG("psm 0x%2.2x, scid 0x%4.4x, amp_id %d", psm, scid, req->amp_id);
4849 /* For controller id 0 make BR/EDR connection */
4850 if (req->amp_id == AMP_ID_BREDR) {
4851 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4856 /* Validate AMP controller id */
4857 hdev = hci_dev_get(req->amp_id);
4861 if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) {
4866 chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4869 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4870 struct hci_conn *hs_hcon;
4872 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4876 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4881 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
4883 mgr->bredr_chan = chan;
4884 chan->hs_hcon = hs_hcon;
4885 chan->fcs = L2CAP_FCS_NONE;
4886 conn->mtu = hdev->block_mtu;
4895 rsp.scid = cpu_to_le16(scid);
4896 rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP);
4897 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4899 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4905 static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
4907 struct l2cap_move_chan_req req;
4910 BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id);
4912 ident = l2cap_get_ident(chan->conn);
4913 chan->ident = ident;
4915 req.icid = cpu_to_le16(chan->scid);
4916 req.dest_amp_id = dest_amp_id;
4918 l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req),
4921 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4924 static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result)
4926 struct l2cap_move_chan_rsp rsp;
4928 BT_DBG("chan %p, result 0x%4.4x", chan, result);
4930 rsp.icid = cpu_to_le16(chan->dcid);
4931 rsp.result = cpu_to_le16(result);
4933 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP,
4937 static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result)
4939 struct l2cap_move_chan_cfm cfm;
4941 BT_DBG("chan %p, result 0x%4.4x", chan, result);
4943 chan->ident = l2cap_get_ident(chan->conn);
4945 cfm.icid = cpu_to_le16(chan->scid);
4946 cfm.result = cpu_to_le16(result);
4948 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM,
4951 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4954 static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid)
4956 struct l2cap_move_chan_cfm cfm;
4958 BT_DBG("conn %p, icid 0x%4.4x", conn, icid);
4960 cfm.icid = cpu_to_le16(icid);
4961 cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED);
4963 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM,
4967 static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
4970 struct l2cap_move_chan_cfm_rsp rsp;
4972 BT_DBG("icid 0x%4.4x", icid);
4974 rsp.icid = cpu_to_le16(icid);
4975 l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
4978 static void __release_logical_link(struct l2cap_chan *chan)
4980 chan->hs_hchan = NULL;
4981 chan->hs_hcon = NULL;
4983 /* Placeholder - release the logical link */
4986 static void l2cap_logical_fail(struct l2cap_chan *chan)
4988 /* Logical link setup failed */
4989 if (chan->state != BT_CONNECTED) {
4990 /* Create channel failure, disconnect */
4991 l2cap_send_disconn_req(chan, ECONNRESET);
4995 switch (chan->move_role) {
4996 case L2CAP_MOVE_ROLE_RESPONDER:
4997 l2cap_move_done(chan);
4998 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP);
5000 case L2CAP_MOVE_ROLE_INITIATOR:
5001 if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP ||
5002 chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) {
5003 /* Remote has only sent pending or
5004 * success responses, clean up
5006 l2cap_move_done(chan);
5009 /* Other amp move states imply that the move
5010 * has already aborted
5012 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5017 static void l2cap_logical_finish_create(struct l2cap_chan *chan,
5018 struct hci_chan *hchan)
5020 struct l2cap_conf_rsp rsp;
5022 chan->hs_hchan = hchan;
5023 chan->hs_hcon->l2cap_data = chan->conn;
5025 l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0);
5027 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
5030 set_default_fcs(chan);
5032 err = l2cap_ertm_init(chan);
5034 l2cap_send_disconn_req(chan, -err);
5036 l2cap_chan_ready(chan);
5040 static void l2cap_logical_finish_move(struct l2cap_chan *chan,
5041 struct hci_chan *hchan)
5043 chan->hs_hcon = hchan->conn;
5044 chan->hs_hcon->l2cap_data = chan->conn;
5046 BT_DBG("move_state %d", chan->move_state);
5048 switch (chan->move_state) {
5049 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
5050 /* Move confirm will be sent after a success
5051 * response is received
5053 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5055 case L2CAP_MOVE_WAIT_LOGICAL_CFM:
5056 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5057 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
5058 } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5059 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
5060 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5061 } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
5062 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
5063 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
5067 /* Move was not in expected state, free the channel */
5068 __release_logical_link(chan);
5070 chan->move_state = L2CAP_MOVE_STABLE;
5074 /* Call with chan locked */
5075 void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
5078 BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status);
5081 l2cap_logical_fail(chan);
5082 __release_logical_link(chan);
5086 if (chan->state != BT_CONNECTED) {
5087 /* Ignore logical link if channel is on BR/EDR */
5088 if (chan->local_amp_id != AMP_ID_BREDR)
5089 l2cap_logical_finish_create(chan, hchan);
5091 l2cap_logical_finish_move(chan, hchan);
5095 void l2cap_move_start(struct l2cap_chan *chan)
5097 BT_DBG("chan %p", chan);
5099 if (chan->local_amp_id == AMP_ID_BREDR) {
5100 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
5102 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
5103 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
5104 /* Placeholder - start physical link setup */
5106 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
5107 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5109 l2cap_move_setup(chan);
5110 l2cap_send_move_chan_req(chan, 0);
5114 static void l2cap_do_create(struct l2cap_chan *chan, int result,
5115 u8 local_amp_id, u8 remote_amp_id)
5117 BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state),
5118 local_amp_id, remote_amp_id);
5120 chan->fcs = L2CAP_FCS_NONE;
5122 /* Outgoing channel on AMP */
5123 if (chan->state == BT_CONNECT) {
5124 if (result == L2CAP_CR_SUCCESS) {
5125 chan->local_amp_id = local_amp_id;
5126 l2cap_send_create_chan_req(chan, remote_amp_id);
5128 /* Revert to BR/EDR connect */
5129 l2cap_send_conn_req(chan);
5135 /* Incoming channel on AMP */
5136 if (__l2cap_no_conn_pending(chan)) {
5137 struct l2cap_conn_rsp rsp;
5139 rsp.scid = cpu_to_le16(chan->dcid);
5140 rsp.dcid = cpu_to_le16(chan->scid);
5142 if (result == L2CAP_CR_SUCCESS) {
5143 /* Send successful response */
5144 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
5145 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
5147 /* Send negative response */
5148 rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
5149 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
5152 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP,
5155 if (result == L2CAP_CR_SUCCESS) {
5156 l2cap_state_change(chan, BT_CONFIG);
5157 set_bit(CONF_REQ_SENT, &chan->conf_state);
5158 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
5160 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
5161 chan->num_conf_req++;
5166 static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id,
5169 l2cap_move_setup(chan);
5170 chan->move_id = local_amp_id;
5171 chan->move_state = L2CAP_MOVE_WAIT_RSP;
5173 l2cap_send_move_chan_req(chan, remote_amp_id);
5176 static void l2cap_do_move_respond(struct l2cap_chan *chan, int result)
5178 struct hci_chan *hchan = NULL;
5180 /* Placeholder - get hci_chan for logical link */
5183 if (hchan->state == BT_CONNECTED) {
5184 /* Logical link is ready to go */
5185 chan->hs_hcon = hchan->conn;
5186 chan->hs_hcon->l2cap_data = chan->conn;
5187 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
5188 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
5190 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5192 /* Wait for logical link to be ready */
5193 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5196 /* Logical link not available */
5197 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED);
5201 static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result)
5203 if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
5205 if (result == -EINVAL)
5206 rsp_result = L2CAP_MR_BAD_ID;
5208 rsp_result = L2CAP_MR_NOT_ALLOWED;
5210 l2cap_send_move_chan_rsp(chan, rsp_result);
5213 chan->move_role = L2CAP_MOVE_ROLE_NONE;
5214 chan->move_state = L2CAP_MOVE_STABLE;
5216 /* Restart data transmission */
5217 l2cap_ertm_send(chan);
5220 /* Invoke with locked chan */
5221 void __l2cap_physical_cfm(struct l2cap_chan *chan, int result)
5223 u8 local_amp_id = chan->local_amp_id;
5224 u8 remote_amp_id = chan->remote_amp_id;
5226 BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d",
5227 chan, result, local_amp_id, remote_amp_id);
5229 if (chan->state == BT_DISCONN || chan->state == BT_CLOSED)
5232 if (chan->state != BT_CONNECTED) {
5233 l2cap_do_create(chan, result, local_amp_id, remote_amp_id);
5234 } else if (result != L2CAP_MR_SUCCESS) {
5235 l2cap_do_move_cancel(chan, result);
5237 switch (chan->move_role) {
5238 case L2CAP_MOVE_ROLE_INITIATOR:
5239 l2cap_do_move_initiate(chan, local_amp_id,
5242 case L2CAP_MOVE_ROLE_RESPONDER:
5243 l2cap_do_move_respond(chan, result);
5246 l2cap_do_move_cancel(chan, result);
5252 static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
5253 struct l2cap_cmd_hdr *cmd,
5254 u16 cmd_len, void *data)
5256 struct l2cap_move_chan_req *req = data;
5257 struct l2cap_move_chan_rsp rsp;
5258 struct l2cap_chan *chan;
5260 u16 result = L2CAP_MR_NOT_ALLOWED;
5262 if (cmd_len != sizeof(*req))
5265 icid = le16_to_cpu(req->icid);
5267 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
5269 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
5272 chan = l2cap_get_chan_by_dcid(conn, icid);
5274 rsp.icid = cpu_to_le16(icid);
5275 rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED);
5276 l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP,
5281 chan->ident = cmd->ident;
5283 if (chan->scid < L2CAP_CID_DYN_START ||
5284 chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY ||
5285 (chan->mode != L2CAP_MODE_ERTM &&
5286 chan->mode != L2CAP_MODE_STREAMING)) {
5287 result = L2CAP_MR_NOT_ALLOWED;
5288 goto send_move_response;
5291 if (chan->local_amp_id == req->dest_amp_id) {
5292 result = L2CAP_MR_SAME_ID;
5293 goto send_move_response;
5296 if (req->dest_amp_id != AMP_ID_BREDR) {
5297 struct hci_dev *hdev;
5298 hdev = hci_dev_get(req->dest_amp_id);
5299 if (!hdev || hdev->dev_type != HCI_AMP ||
5300 !test_bit(HCI_UP, &hdev->flags)) {
5304 result = L2CAP_MR_BAD_ID;
5305 goto send_move_response;
5310 /* Detect a move collision. Only send a collision response
5311 * if this side has "lost", otherwise proceed with the move.
5312 * The winner has the larger bd_addr.
5314 if ((__chan_is_moving(chan) ||
5315 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
5316 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
5317 result = L2CAP_MR_COLLISION;
5318 goto send_move_response;
5321 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5322 l2cap_move_setup(chan);
5323 chan->move_id = req->dest_amp_id;
5325 if (req->dest_amp_id == AMP_ID_BREDR) {
5326 /* Moving to BR/EDR */
5327 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5328 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
5329 result = L2CAP_MR_PEND;
5331 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
5332 result = L2CAP_MR_SUCCESS;
5335 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
5336 /* Placeholder - uncomment when amp functions are available */
5337 /*amp_accept_physical(chan, req->dest_amp_id);*/
5338 result = L2CAP_MR_PEND;
5342 l2cap_send_move_chan_rsp(chan, result);
5344 l2cap_chan_unlock(chan);
5345 l2cap_chan_put(chan);
5350 static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result)
5352 struct l2cap_chan *chan;
5353 struct hci_chan *hchan = NULL;
5355 chan = l2cap_get_chan_by_scid(conn, icid);
5357 l2cap_send_move_chan_cfm_icid(conn, icid);
5361 __clear_chan_timer(chan);
5362 if (result == L2CAP_MR_PEND)
5363 __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT);
5365 switch (chan->move_state) {
5366 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
5367 /* Move confirm will be sent when logical link
5370 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5372 case L2CAP_MOVE_WAIT_RSP_SUCCESS:
5373 if (result == L2CAP_MR_PEND) {
5375 } else if (test_bit(CONN_LOCAL_BUSY,
5376 &chan->conn_state)) {
5377 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
5379 /* Logical link is up or moving to BR/EDR,
5382 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
5383 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5386 case L2CAP_MOVE_WAIT_RSP:
5388 if (result == L2CAP_MR_SUCCESS) {
5389 /* Remote is ready, send confirm immediately
5390 * after logical link is ready
5392 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5394 /* Both logical link and move success
5395 * are required to confirm
5397 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP;
5400 /* Placeholder - get hci_chan for logical link */
5402 /* Logical link not available */
5403 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5407 /* If the logical link is not yet connected, do not
5408 * send confirmation.
5410 if (hchan->state != BT_CONNECTED)
5413 /* Logical link is already ready to go */
5415 chan->hs_hcon = hchan->conn;
5416 chan->hs_hcon->l2cap_data = chan->conn;
5418 if (result == L2CAP_MR_SUCCESS) {
5419 /* Can confirm now */
5420 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5422 /* Now only need move success
5425 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5428 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5431 /* Any other amp move state means the move failed. */
5432 chan->move_id = chan->local_amp_id;
5433 l2cap_move_done(chan);
5434 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5437 l2cap_chan_unlock(chan);
5438 l2cap_chan_put(chan);
5441 static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
5444 struct l2cap_chan *chan;
5446 chan = l2cap_get_chan_by_ident(conn, ident);
5448 /* Could not locate channel, icid is best guess */
5449 l2cap_send_move_chan_cfm_icid(conn, icid);
5453 __clear_chan_timer(chan);
5455 if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5456 if (result == L2CAP_MR_COLLISION) {
5457 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5459 /* Cleanup - cancel move */
5460 chan->move_id = chan->local_amp_id;
5461 l2cap_move_done(chan);
5465 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5467 l2cap_chan_unlock(chan);
5468 l2cap_chan_put(chan);
5471 static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
5472 struct l2cap_cmd_hdr *cmd,
5473 u16 cmd_len, void *data)
5475 struct l2cap_move_chan_rsp *rsp = data;
5478 if (cmd_len != sizeof(*rsp))
5481 icid = le16_to_cpu(rsp->icid);
5482 result = le16_to_cpu(rsp->result);
5484 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
5486 if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND)
5487 l2cap_move_continue(conn, icid, result);
5489 l2cap_move_fail(conn, cmd->ident, icid, result);
5494 static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5495 struct l2cap_cmd_hdr *cmd,
5496 u16 cmd_len, void *data)
5498 struct l2cap_move_chan_cfm *cfm = data;
5499 struct l2cap_chan *chan;
5502 if (cmd_len != sizeof(*cfm))
5505 icid = le16_to_cpu(cfm->icid);
5506 result = le16_to_cpu(cfm->result);
5508 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
5510 chan = l2cap_get_chan_by_dcid(conn, icid);
5512 /* Spec requires a response even if the icid was not found */
5513 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5517 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5518 if (result == L2CAP_MC_CONFIRMED) {
5519 chan->local_amp_id = chan->move_id;
5520 if (chan->local_amp_id == AMP_ID_BREDR)
5521 __release_logical_link(chan);
5523 chan->move_id = chan->local_amp_id;
5526 l2cap_move_done(chan);
5529 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5531 l2cap_chan_unlock(chan);
5532 l2cap_chan_put(chan);
5537 static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
5538 struct l2cap_cmd_hdr *cmd,
5539 u16 cmd_len, void *data)
5541 struct l2cap_move_chan_cfm_rsp *rsp = data;
5542 struct l2cap_chan *chan;
5545 if (cmd_len != sizeof(*rsp))
5548 icid = le16_to_cpu(rsp->icid);
5550 BT_DBG("icid 0x%4.4x", icid);
5552 chan = l2cap_get_chan_by_scid(conn, icid);
5556 __clear_chan_timer(chan);
5558 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5559 chan->local_amp_id = chan->move_id;
5561 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
5562 __release_logical_link(chan);
5564 l2cap_move_done(chan);
5567 l2cap_chan_unlock(chan);
5568 l2cap_chan_put(chan);
5573 static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
5574 struct l2cap_cmd_hdr *cmd,
5575 u16 cmd_len, u8 *data)
5577 struct hci_conn *hcon = conn->hcon;
5578 struct l2cap_conn_param_update_req *req;
5579 struct l2cap_conn_param_update_rsp rsp;
5580 u16 min, max, latency, to_multiplier;
5583 if (hcon->role != HCI_ROLE_MASTER)
5586 if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
5589 req = (struct l2cap_conn_param_update_req *) data;
5590 min = __le16_to_cpu(req->min);
5591 max = __le16_to_cpu(req->max);
5592 latency = __le16_to_cpu(req->latency);
5593 to_multiplier = __le16_to_cpu(req->to_multiplier);
5595 BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
5596 min, max, latency, to_multiplier);
5598 memset(&rsp, 0, sizeof(rsp));
5600 err = hci_check_conn_params(min, max, latency, to_multiplier);
5602 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
5604 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
5606 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
5612 store_hint = hci_le_conn_update(hcon, min, max, latency,
5614 mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type,
5615 store_hint, min, max, latency,
5623 static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
5624 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5627 struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
5628 struct hci_conn *hcon = conn->hcon;
5629 u16 dcid, mtu, mps, credits, result;
5630 struct l2cap_chan *chan;
5633 if (cmd_len < sizeof(*rsp))
5636 dcid = __le16_to_cpu(rsp->dcid);
5637 mtu = __le16_to_cpu(rsp->mtu);
5638 mps = __le16_to_cpu(rsp->mps);
5639 credits = __le16_to_cpu(rsp->credits);
5640 result = __le16_to_cpu(rsp->result);
5642 if (result == L2CAP_CR_LE_SUCCESS && (mtu < 23 || mps < 23 ||
5643 dcid < L2CAP_CID_DYN_START ||
5644 dcid > L2CAP_CID_LE_DYN_END))
5647 BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x",
5648 dcid, mtu, mps, credits, result);
5650 mutex_lock(&conn->chan_lock);
5652 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5660 l2cap_chan_lock(chan);
5663 case L2CAP_CR_LE_SUCCESS:
5664 if (__l2cap_get_chan_by_dcid(conn, dcid)) {
5672 chan->remote_mps = mps;
5673 chan->tx_credits = credits;
5674 l2cap_chan_ready(chan);
5677 case L2CAP_CR_LE_AUTHENTICATION:
5678 case L2CAP_CR_LE_ENCRYPTION:
5679 /* If we already have MITM protection we can't do
5682 if (hcon->sec_level > BT_SECURITY_MEDIUM) {
5683 l2cap_chan_del(chan, ECONNREFUSED);
5687 sec_level = hcon->sec_level + 1;
5688 if (chan->sec_level < sec_level)
5689 chan->sec_level = sec_level;
5691 /* We'll need to send a new Connect Request */
5692 clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags);
5694 smp_conn_security(hcon, chan->sec_level);
5698 l2cap_chan_del(chan, ECONNREFUSED);
5702 l2cap_chan_unlock(chan);
5705 mutex_unlock(&conn->chan_lock);
5710 static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5711 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5716 switch (cmd->code) {
5717 case L2CAP_COMMAND_REJ:
5718 l2cap_command_rej(conn, cmd, cmd_len, data);
5721 case L2CAP_CONN_REQ:
5722 err = l2cap_connect_req(conn, cmd, cmd_len, data);
5725 case L2CAP_CONN_RSP:
5726 case L2CAP_CREATE_CHAN_RSP:
5727 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
5730 case L2CAP_CONF_REQ:
5731 err = l2cap_config_req(conn, cmd, cmd_len, data);
5734 case L2CAP_CONF_RSP:
5735 l2cap_config_rsp(conn, cmd, cmd_len, data);
5738 case L2CAP_DISCONN_REQ:
5739 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
5742 case L2CAP_DISCONN_RSP:
5743 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
5746 case L2CAP_ECHO_REQ:
5747 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
5750 case L2CAP_ECHO_RSP:
5753 case L2CAP_INFO_REQ:
5754 err = l2cap_information_req(conn, cmd, cmd_len, data);
5757 case L2CAP_INFO_RSP:
5758 l2cap_information_rsp(conn, cmd, cmd_len, data);
5761 case L2CAP_CREATE_CHAN_REQ:
5762 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
5765 case L2CAP_MOVE_CHAN_REQ:
5766 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
5769 case L2CAP_MOVE_CHAN_RSP:
5770 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
5773 case L2CAP_MOVE_CHAN_CFM:
5774 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
5777 case L2CAP_MOVE_CHAN_CFM_RSP:
5778 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
5782 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
5790 static int l2cap_le_connect_req(struct l2cap_conn *conn,
5791 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5794 struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data;
5795 struct l2cap_le_conn_rsp rsp;
5796 struct l2cap_chan *chan, *pchan;
5797 u16 dcid, scid, credits, mtu, mps;
5801 if (cmd_len != sizeof(*req))
5804 scid = __le16_to_cpu(req->scid);
5805 mtu = __le16_to_cpu(req->mtu);
5806 mps = __le16_to_cpu(req->mps);
5811 if (mtu < 23 || mps < 23)
5814 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm),
5817 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 3, Part A
5820 * Valid range: 0x0001-0x00ff
5822 * Table 4.15: L2CAP_LE_CREDIT_BASED_CONNECTION_REQ SPSM ranges
5824 if (!psm || __le16_to_cpu(psm) > L2CAP_PSM_LE_DYN_END) {
5825 result = L2CAP_CR_LE_BAD_PSM;
5830 /* Check if we have socket listening on psm */
5831 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
5832 &conn->hcon->dst, LE_LINK);
5834 result = L2CAP_CR_LE_BAD_PSM;
5839 mutex_lock(&conn->chan_lock);
5840 l2cap_chan_lock(pchan);
5842 if (!smp_sufficient_security(conn->hcon, pchan->sec_level,
5844 result = L2CAP_CR_LE_AUTHENTICATION;
5846 goto response_unlock;
5849 /* Check for valid dynamic CID range */
5850 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) {
5851 result = L2CAP_CR_LE_INVALID_SCID;
5853 goto response_unlock;
5856 /* Check if we already have channel with that dcid */
5857 if (__l2cap_get_chan_by_dcid(conn, scid)) {
5858 result = L2CAP_CR_LE_SCID_IN_USE;
5860 goto response_unlock;
5863 chan = pchan->ops->new_connection(pchan);
5865 result = L2CAP_CR_LE_NO_MEM;
5866 goto response_unlock;
5869 bacpy(&chan->src, &conn->hcon->src);
5870 bacpy(&chan->dst, &conn->hcon->dst);
5871 chan->src_type = bdaddr_src_type(conn->hcon);
5872 chan->dst_type = bdaddr_dst_type(conn->hcon);
5876 chan->remote_mps = mps;
5878 __l2cap_chan_add(conn, chan);
5880 l2cap_le_flowctl_init(chan, __le16_to_cpu(req->credits));
5883 credits = chan->rx_credits;
5885 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
5887 chan->ident = cmd->ident;
5889 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
5890 l2cap_state_change(chan, BT_CONNECT2);
5891 /* The following result value is actually not defined
5892 * for LE CoC but we use it to let the function know
5893 * that it should bail out after doing its cleanup
5894 * instead of sending a response.
5896 result = L2CAP_CR_PEND;
5897 chan->ops->defer(chan);
5899 l2cap_chan_ready(chan);
5900 result = L2CAP_CR_LE_SUCCESS;
5904 l2cap_chan_unlock(pchan);
5905 mutex_unlock(&conn->chan_lock);
5906 l2cap_chan_put(pchan);
5908 if (result == L2CAP_CR_PEND)
5913 rsp.mtu = cpu_to_le16(chan->imtu);
5914 rsp.mps = cpu_to_le16(chan->mps);
5920 rsp.dcid = cpu_to_le16(dcid);
5921 rsp.credits = cpu_to_le16(credits);
5922 rsp.result = cpu_to_le16(result);
5924 l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp);
5929 static inline int l2cap_le_credits(struct l2cap_conn *conn,
5930 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5933 struct l2cap_le_credits *pkt;
5934 struct l2cap_chan *chan;
5935 u16 cid, credits, max_credits;
5937 if (cmd_len != sizeof(*pkt))
5940 pkt = (struct l2cap_le_credits *) data;
5941 cid = __le16_to_cpu(pkt->cid);
5942 credits = __le16_to_cpu(pkt->credits);
5944 BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits);
5946 chan = l2cap_get_chan_by_dcid(conn, cid);
5950 max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
5951 if (credits > max_credits) {
5952 BT_ERR("LE credits overflow");
5953 l2cap_send_disconn_req(chan, ECONNRESET);
5955 /* Return 0 so that we don't trigger an unnecessary
5956 * command reject packet.
5961 chan->tx_credits += credits;
5963 /* Resume sending */
5964 l2cap_le_flowctl_send(chan);
5966 if (chan->tx_credits)
5967 chan->ops->resume(chan);
5970 l2cap_chan_unlock(chan);
5971 l2cap_chan_put(chan);
5976 static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,
5977 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5980 struct l2cap_ecred_conn_req *req = (void *) data;
5982 struct l2cap_ecred_conn_rsp rsp;
5983 __le16 dcid[L2CAP_ECRED_MAX_CID];
5985 struct l2cap_chan *chan, *pchan;
5995 if (cmd_len < sizeof(*req) || (cmd_len - sizeof(*req)) % sizeof(u16)) {
5996 result = L2CAP_CR_LE_INVALID_PARAMS;
6000 cmd_len -= sizeof(*req);
6001 num_scid = cmd_len / sizeof(u16);
6003 if (num_scid > ARRAY_SIZE(pdu.dcid)) {
6004 result = L2CAP_CR_LE_INVALID_PARAMS;
6008 mtu = __le16_to_cpu(req->mtu);
6009 mps = __le16_to_cpu(req->mps);
6011 if (mtu < L2CAP_ECRED_MIN_MTU || mps < L2CAP_ECRED_MIN_MPS) {
6012 result = L2CAP_CR_LE_UNACCEPT_PARAMS;
6018 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 3, Part A
6021 * Valid range: 0x0001-0x00ff
6023 * Table 4.15: L2CAP_LE_CREDIT_BASED_CONNECTION_REQ SPSM ranges
6025 if (!psm || __le16_to_cpu(psm) > L2CAP_PSM_LE_DYN_END) {
6026 result = L2CAP_CR_LE_BAD_PSM;
6030 BT_DBG("psm 0x%2.2x mtu %u mps %u", __le16_to_cpu(psm), mtu, mps);
6032 memset(&pdu, 0, sizeof(pdu));
6034 /* Check if we have socket listening on psm */
6035 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
6036 &conn->hcon->dst, LE_LINK);
6038 result = L2CAP_CR_LE_BAD_PSM;
6042 mutex_lock(&conn->chan_lock);
6043 l2cap_chan_lock(pchan);
6045 if (!smp_sufficient_security(conn->hcon, pchan->sec_level,
6047 result = L2CAP_CR_LE_AUTHENTICATION;
6051 result = L2CAP_CR_LE_SUCCESS;
6053 for (i = 0; i < num_scid; i++) {
6054 u16 scid = __le16_to_cpu(req->scid[i]);
6056 BT_DBG("scid[%d] 0x%4.4x", i, scid);
6058 pdu.dcid[i] = 0x0000;
6059 len += sizeof(*pdu.dcid);
6061 /* Check for valid dynamic CID range */
6062 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) {
6063 result = L2CAP_CR_LE_INVALID_SCID;
6067 /* Check if we already have channel with that dcid */
6068 if (__l2cap_get_chan_by_dcid(conn, scid)) {
6069 result = L2CAP_CR_LE_SCID_IN_USE;
6073 chan = pchan->ops->new_connection(pchan);
6075 result = L2CAP_CR_LE_NO_MEM;
6079 bacpy(&chan->src, &conn->hcon->src);
6080 bacpy(&chan->dst, &conn->hcon->dst);
6081 chan->src_type = bdaddr_src_type(conn->hcon);
6082 chan->dst_type = bdaddr_dst_type(conn->hcon);
6086 chan->remote_mps = mps;
6088 __l2cap_chan_add(conn, chan);
6090 l2cap_ecred_init(chan, __le16_to_cpu(req->credits));
6093 if (!pdu.rsp.credits) {
6094 pdu.rsp.mtu = cpu_to_le16(chan->imtu);
6095 pdu.rsp.mps = cpu_to_le16(chan->mps);
6096 pdu.rsp.credits = cpu_to_le16(chan->rx_credits);
6099 pdu.dcid[i] = cpu_to_le16(chan->scid);
6101 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
6103 chan->ident = cmd->ident;
6105 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
6106 l2cap_state_change(chan, BT_CONNECT2);
6108 chan->ops->defer(chan);
6110 l2cap_chan_ready(chan);
6115 l2cap_chan_unlock(pchan);
6116 mutex_unlock(&conn->chan_lock);
6117 l2cap_chan_put(pchan);
6120 pdu.rsp.result = cpu_to_le16(result);
6125 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECRED_CONN_RSP,
6126 sizeof(pdu.rsp) + len, &pdu);
6131 static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn,
6132 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
6135 struct l2cap_ecred_conn_rsp *rsp = (void *) data;
6136 struct hci_conn *hcon = conn->hcon;
6137 u16 mtu, mps, credits, result;
6138 struct l2cap_chan *chan, *tmp;
6139 int err = 0, sec_level;
6142 if (cmd_len < sizeof(*rsp))
6145 mtu = __le16_to_cpu(rsp->mtu);
6146 mps = __le16_to_cpu(rsp->mps);
6147 credits = __le16_to_cpu(rsp->credits);
6148 result = __le16_to_cpu(rsp->result);
6150 BT_DBG("mtu %u mps %u credits %u result 0x%4.4x", mtu, mps, credits,
6153 mutex_lock(&conn->chan_lock);
6155 cmd_len -= sizeof(*rsp);
6157 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
6160 if (chan->ident != cmd->ident ||
6161 chan->mode != L2CAP_MODE_EXT_FLOWCTL ||
6162 chan->state == BT_CONNECTED)
6165 l2cap_chan_lock(chan);
6167 /* Check that there is a dcid for each pending channel */
6168 if (cmd_len < sizeof(dcid)) {
6169 l2cap_chan_del(chan, ECONNREFUSED);
6170 l2cap_chan_unlock(chan);
6174 dcid = __le16_to_cpu(rsp->dcid[i++]);
6175 cmd_len -= sizeof(u16);
6177 BT_DBG("dcid[%d] 0x%4.4x", i, dcid);
6179 /* Check if dcid is already in use */
6180 if (dcid && __l2cap_get_chan_by_dcid(conn, dcid)) {
6181 /* If a device receives a
6182 * L2CAP_CREDIT_BASED_CONNECTION_RSP packet with an
6183 * already-assigned Destination CID, then both the
6184 * original channel and the new channel shall be
6185 * immediately discarded and not used.
6187 l2cap_chan_del(chan, ECONNREFUSED);
6188 l2cap_chan_unlock(chan);
6189 chan = __l2cap_get_chan_by_dcid(conn, dcid);
6190 l2cap_chan_lock(chan);
6191 l2cap_chan_del(chan, ECONNRESET);
6192 l2cap_chan_unlock(chan);
6197 case L2CAP_CR_LE_AUTHENTICATION:
6198 case L2CAP_CR_LE_ENCRYPTION:
6199 /* If we already have MITM protection we can't do
6202 if (hcon->sec_level > BT_SECURITY_MEDIUM) {
6203 l2cap_chan_del(chan, ECONNREFUSED);
6207 sec_level = hcon->sec_level + 1;
6208 if (chan->sec_level < sec_level)
6209 chan->sec_level = sec_level;
6211 /* We'll need to send a new Connect Request */
6212 clear_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags);
6214 smp_conn_security(hcon, chan->sec_level);
6217 case L2CAP_CR_LE_BAD_PSM:
6218 l2cap_chan_del(chan, ECONNREFUSED);
6222 /* If dcid was not set it means channels was refused */
6224 l2cap_chan_del(chan, ECONNREFUSED);
6231 chan->remote_mps = mps;
6232 chan->tx_credits = credits;
6233 l2cap_chan_ready(chan);
6237 l2cap_chan_unlock(chan);
6240 mutex_unlock(&conn->chan_lock);
6245 static inline int l2cap_ecred_reconf_req(struct l2cap_conn *conn,
6246 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
6249 struct l2cap_ecred_reconf_req *req = (void *) data;
6250 struct l2cap_ecred_reconf_rsp rsp;
6251 u16 mtu, mps, result;
6252 struct l2cap_chan *chan;
6258 if (cmd_len < sizeof(*req) || cmd_len - sizeof(*req) % sizeof(u16)) {
6259 result = L2CAP_CR_LE_INVALID_PARAMS;
6263 mtu = __le16_to_cpu(req->mtu);
6264 mps = __le16_to_cpu(req->mps);
6266 BT_DBG("mtu %u mps %u", mtu, mps);
6268 if (mtu < L2CAP_ECRED_MIN_MTU) {
6269 result = L2CAP_RECONF_INVALID_MTU;
6273 if (mps < L2CAP_ECRED_MIN_MPS) {
6274 result = L2CAP_RECONF_INVALID_MPS;
6278 cmd_len -= sizeof(*req);
6279 num_scid = cmd_len / sizeof(u16);
6280 result = L2CAP_RECONF_SUCCESS;
6282 for (i = 0; i < num_scid; i++) {
6285 scid = __le16_to_cpu(req->scid[i]);
6289 chan = __l2cap_get_chan_by_dcid(conn, scid);
6293 /* If the MTU value is decreased for any of the included
6294 * channels, then the receiver shall disconnect all
6295 * included channels.
6297 if (chan->omtu > mtu) {
6298 BT_ERR("chan %p decreased MTU %u -> %u", chan,
6300 result = L2CAP_RECONF_INVALID_MTU;
6304 chan->remote_mps = mps;
6308 rsp.result = cpu_to_le16(result);
6310 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECRED_RECONF_RSP, sizeof(rsp),
6316 static inline int l2cap_ecred_reconf_rsp(struct l2cap_conn *conn,
6317 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
6320 struct l2cap_chan *chan, *tmp;
6321 struct l2cap_ecred_conn_rsp *rsp = (void *) data;
6324 if (cmd_len < sizeof(*rsp))
6327 result = __le16_to_cpu(rsp->result);
6329 BT_DBG("result 0x%4.4x", rsp->result);
6334 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
6335 if (chan->ident != cmd->ident)
6338 l2cap_chan_del(chan, ECONNRESET);
6344 static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
6345 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
6348 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
6349 struct l2cap_chan *chan;
6351 if (cmd_len < sizeof(*rej))
6354 mutex_lock(&conn->chan_lock);
6356 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
6360 l2cap_chan_lock(chan);
6361 l2cap_chan_del(chan, ECONNREFUSED);
6362 l2cap_chan_unlock(chan);
6365 mutex_unlock(&conn->chan_lock);
6369 static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
6370 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
6375 switch (cmd->code) {
6376 case L2CAP_COMMAND_REJ:
6377 l2cap_le_command_rej(conn, cmd, cmd_len, data);
6380 case L2CAP_CONN_PARAM_UPDATE_REQ:
6381 err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data);
6384 case L2CAP_CONN_PARAM_UPDATE_RSP:
6387 case L2CAP_LE_CONN_RSP:
6388 l2cap_le_connect_rsp(conn, cmd, cmd_len, data);
6391 case L2CAP_LE_CONN_REQ:
6392 err = l2cap_le_connect_req(conn, cmd, cmd_len, data);
6395 case L2CAP_LE_CREDITS:
6396 err = l2cap_le_credits(conn, cmd, cmd_len, data);
6399 case L2CAP_ECRED_CONN_REQ:
6400 err = l2cap_ecred_conn_req(conn, cmd, cmd_len, data);
6403 case L2CAP_ECRED_CONN_RSP:
6404 err = l2cap_ecred_conn_rsp(conn, cmd, cmd_len, data);
6407 case L2CAP_ECRED_RECONF_REQ:
6408 err = l2cap_ecred_reconf_req(conn, cmd, cmd_len, data);
6411 case L2CAP_ECRED_RECONF_RSP:
6412 err = l2cap_ecred_reconf_rsp(conn, cmd, cmd_len, data);
6415 case L2CAP_DISCONN_REQ:
6416 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
6419 case L2CAP_DISCONN_RSP:
6420 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
6424 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
6432 static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
6433 struct sk_buff *skb)
6435 struct hci_conn *hcon = conn->hcon;
6436 struct l2cap_cmd_hdr *cmd;
6440 if (hcon->type != LE_LINK)
6443 if (skb->len < L2CAP_CMD_HDR_SIZE)
6446 cmd = (void *) skb->data;
6447 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
6449 len = le16_to_cpu(cmd->len);
6451 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident);
6453 if (len != skb->len || !cmd->ident) {
6454 BT_DBG("corrupted command");
6458 err = l2cap_le_sig_cmd(conn, cmd, len, skb->data);
6460 struct l2cap_cmd_rej_unk rej;
6462 BT_ERR("Wrong link type (%d)", err);
6464 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
6465 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
6473 static inline void l2cap_sig_channel(struct l2cap_conn *conn,
6474 struct sk_buff *skb)
6476 struct hci_conn *hcon = conn->hcon;
6477 struct l2cap_cmd_hdr *cmd;
6480 l2cap_raw_recv(conn, skb);
6482 if (hcon->type != ACL_LINK)
6485 while (skb->len >= L2CAP_CMD_HDR_SIZE) {
6488 cmd = (void *) skb->data;
6489 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
6491 len = le16_to_cpu(cmd->len);
6493 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len,
6496 if (len > skb->len || !cmd->ident) {
6497 BT_DBG("corrupted command");
6501 err = l2cap_bredr_sig_cmd(conn, cmd, len, skb->data);
6503 struct l2cap_cmd_rej_unk rej;
6505 BT_ERR("Wrong link type (%d)", err);
6507 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
6508 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
6519 static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb)
6521 u16 our_fcs, rcv_fcs;
6524 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
6525 hdr_size = L2CAP_EXT_HDR_SIZE;
6527 hdr_size = L2CAP_ENH_HDR_SIZE;
6529 if (chan->fcs == L2CAP_FCS_CRC16) {
6530 skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
6531 rcv_fcs = get_unaligned_le16(skb->data + skb->len);
6532 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
6534 if (our_fcs != rcv_fcs)
6540 static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
6542 struct l2cap_ctrl control;
6544 BT_DBG("chan %p", chan);
6546 memset(&control, 0, sizeof(control));
6549 control.reqseq = chan->buffer_seq;
6550 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6552 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6553 control.super = L2CAP_SUPER_RNR;
6554 l2cap_send_sframe(chan, &control);
6557 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
6558 chan->unacked_frames > 0)
6559 __set_retrans_timer(chan);
6561 /* Send pending iframes */
6562 l2cap_ertm_send(chan);
6564 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
6565 test_bit(CONN_SEND_FBIT, &chan->conn_state)) {
6566 /* F-bit wasn't sent in an s-frame or i-frame yet, so
6569 control.super = L2CAP_SUPER_RR;
6570 l2cap_send_sframe(chan, &control);
6574 static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
6575 struct sk_buff **last_frag)
6577 /* skb->len reflects data in skb as well as all fragments
6578 * skb->data_len reflects only data in fragments
6580 if (!skb_has_frag_list(skb))
6581 skb_shinfo(skb)->frag_list = new_frag;
6583 new_frag->next = NULL;
6585 (*last_frag)->next = new_frag;
6586 *last_frag = new_frag;
6588 skb->len += new_frag->len;
6589 skb->data_len += new_frag->len;
6590 skb->truesize += new_frag->truesize;
6593 static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
6594 struct l2cap_ctrl *control)
6598 switch (control->sar) {
6599 case L2CAP_SAR_UNSEGMENTED:
6603 err = chan->ops->recv(chan, skb);
6606 case L2CAP_SAR_START:
6610 if (!pskb_may_pull(skb, L2CAP_SDULEN_SIZE))
6613 chan->sdu_len = get_unaligned_le16(skb->data);
6614 skb_pull(skb, L2CAP_SDULEN_SIZE);
6616 if (chan->sdu_len > chan->imtu) {
6621 if (skb->len >= chan->sdu_len)
6625 chan->sdu_last_frag = skb;
6631 case L2CAP_SAR_CONTINUE:
6635 append_skb_frag(chan->sdu, skb,
6636 &chan->sdu_last_frag);
6639 if (chan->sdu->len >= chan->sdu_len)
6649 append_skb_frag(chan->sdu, skb,
6650 &chan->sdu_last_frag);
6653 if (chan->sdu->len != chan->sdu_len)
6656 err = chan->ops->recv(chan, chan->sdu);
6659 /* Reassembly complete */
6661 chan->sdu_last_frag = NULL;
6669 kfree_skb(chan->sdu);
6671 chan->sdu_last_frag = NULL;
6678 static int l2cap_resegment(struct l2cap_chan *chan)
6684 void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
6688 if (chan->mode != L2CAP_MODE_ERTM)
6691 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR;
6692 l2cap_tx(chan, NULL, NULL, event);
6695 static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
6698 /* Pass sequential frames to l2cap_reassemble_sdu()
6699 * until a gap is encountered.
6702 BT_DBG("chan %p", chan);
6704 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6705 struct sk_buff *skb;
6706 BT_DBG("Searching for skb with txseq %d (queue len %d)",
6707 chan->buffer_seq, skb_queue_len(&chan->srej_q));
6709 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq);
6714 skb_unlink(skb, &chan->srej_q);
6715 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
6716 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->l2cap);
6721 if (skb_queue_empty(&chan->srej_q)) {
6722 chan->rx_state = L2CAP_RX_STATE_RECV;
6723 l2cap_send_ack(chan);
6729 static void l2cap_handle_srej(struct l2cap_chan *chan,
6730 struct l2cap_ctrl *control)
6732 struct sk_buff *skb;
6734 BT_DBG("chan %p, control %p", chan, control);
6736 if (control->reqseq == chan->next_tx_seq) {
6737 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
6738 l2cap_send_disconn_req(chan, ECONNRESET);
6742 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
6745 BT_DBG("Seq %d not available for retransmission",
6750 if (chan->max_tx != 0 && bt_cb(skb)->l2cap.retries >= chan->max_tx) {
6751 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
6752 l2cap_send_disconn_req(chan, ECONNRESET);
6756 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6758 if (control->poll) {
6759 l2cap_pass_to_tx(chan, control);
6761 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6762 l2cap_retransmit(chan, control);
6763 l2cap_ertm_send(chan);
6765 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
6766 set_bit(CONN_SREJ_ACT, &chan->conn_state);
6767 chan->srej_save_reqseq = control->reqseq;
6770 l2cap_pass_to_tx_fbit(chan, control);
6772 if (control->final) {
6773 if (chan->srej_save_reqseq != control->reqseq ||
6774 !test_and_clear_bit(CONN_SREJ_ACT,
6776 l2cap_retransmit(chan, control);
6778 l2cap_retransmit(chan, control);
6779 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
6780 set_bit(CONN_SREJ_ACT, &chan->conn_state);
6781 chan->srej_save_reqseq = control->reqseq;
6787 static void l2cap_handle_rej(struct l2cap_chan *chan,
6788 struct l2cap_ctrl *control)
6790 struct sk_buff *skb;
6792 BT_DBG("chan %p, control %p", chan, control);
6794 if (control->reqseq == chan->next_tx_seq) {
6795 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
6796 l2cap_send_disconn_req(chan, ECONNRESET);
6800 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
6802 if (chan->max_tx && skb &&
6803 bt_cb(skb)->l2cap.retries >= chan->max_tx) {
6804 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
6805 l2cap_send_disconn_req(chan, ECONNRESET);
6809 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6811 l2cap_pass_to_tx(chan, control);
6813 if (control->final) {
6814 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
6815 l2cap_retransmit_all(chan, control);
6817 l2cap_retransmit_all(chan, control);
6818 l2cap_ertm_send(chan);
6819 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F)
6820 set_bit(CONN_REJ_ACT, &chan->conn_state);
6824 static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq)
6826 BT_DBG("chan %p, txseq %d", chan, txseq);
6828 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq,
6829 chan->expected_tx_seq);
6831 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
6832 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
6834 /* See notes below regarding "double poll" and
6837 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6838 BT_DBG("Invalid/Ignore - after SREJ");
6839 return L2CAP_TXSEQ_INVALID_IGNORE;
6841 BT_DBG("Invalid - in window after SREJ sent");
6842 return L2CAP_TXSEQ_INVALID;
6846 if (chan->srej_list.head == txseq) {
6847 BT_DBG("Expected SREJ");
6848 return L2CAP_TXSEQ_EXPECTED_SREJ;
6851 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) {
6852 BT_DBG("Duplicate SREJ - txseq already stored");
6853 return L2CAP_TXSEQ_DUPLICATE_SREJ;
6856 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) {
6857 BT_DBG("Unexpected SREJ - not requested");
6858 return L2CAP_TXSEQ_UNEXPECTED_SREJ;
6862 if (chan->expected_tx_seq == txseq) {
6863 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
6865 BT_DBG("Invalid - txseq outside tx window");
6866 return L2CAP_TXSEQ_INVALID;
6869 return L2CAP_TXSEQ_EXPECTED;
6873 if (__seq_offset(chan, txseq, chan->last_acked_seq) <
6874 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) {
6875 BT_DBG("Duplicate - expected_tx_seq later than txseq");
6876 return L2CAP_TXSEQ_DUPLICATE;
6879 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) {
6880 /* A source of invalid packets is a "double poll" condition,
6881 * where delays cause us to send multiple poll packets. If
6882 * the remote stack receives and processes both polls,
6883 * sequence numbers can wrap around in such a way that a
6884 * resent frame has a sequence number that looks like new data
6885 * with a sequence gap. This would trigger an erroneous SREJ
6888 * Fortunately, this is impossible with a tx window that's
6889 * less than half of the maximum sequence number, which allows
6890 * invalid frames to be safely ignored.
6892 * With tx window sizes greater than half of the tx window
6893 * maximum, the frame is invalid and cannot be ignored. This
6894 * causes a disconnect.
6897 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6898 BT_DBG("Invalid/Ignore - txseq outside tx window");
6899 return L2CAP_TXSEQ_INVALID_IGNORE;
6901 BT_DBG("Invalid - txseq outside tx window");
6902 return L2CAP_TXSEQ_INVALID;
6905 BT_DBG("Unexpected - txseq indicates missing frames");
6906 return L2CAP_TXSEQ_UNEXPECTED;
6910 static int l2cap_rx_state_recv(struct l2cap_chan *chan,
6911 struct l2cap_ctrl *control,
6912 struct sk_buff *skb, u8 event)
6914 struct l2cap_ctrl local_control;
6916 bool skb_in_use = false;
6918 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6922 case L2CAP_EV_RECV_IFRAME:
6923 switch (l2cap_classify_txseq(chan, control->txseq)) {
6924 case L2CAP_TXSEQ_EXPECTED:
6925 l2cap_pass_to_tx(chan, control);
6927 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6928 BT_DBG("Busy, discarding expected seq %d",
6933 chan->expected_tx_seq = __next_seq(chan,
6936 chan->buffer_seq = chan->expected_tx_seq;
6939 /* l2cap_reassemble_sdu may free skb, hence invalidate
6940 * control, so make a copy in advance to use it after
6941 * l2cap_reassemble_sdu returns and to avoid the race
6942 * condition, for example:
6944 * The current thread calls:
6945 * l2cap_reassemble_sdu
6946 * chan->ops->recv == l2cap_sock_recv_cb
6947 * __sock_queue_rcv_skb
6948 * Another thread calls:
6952 * Then the current thread tries to access control, but
6953 * it was freed by skb_free_datagram.
6955 local_control = *control;
6956 err = l2cap_reassemble_sdu(chan, skb, control);
6960 if (local_control.final) {
6961 if (!test_and_clear_bit(CONN_REJ_ACT,
6962 &chan->conn_state)) {
6963 local_control.final = 0;
6964 l2cap_retransmit_all(chan, &local_control);
6965 l2cap_ertm_send(chan);
6969 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
6970 l2cap_send_ack(chan);
6972 case L2CAP_TXSEQ_UNEXPECTED:
6973 l2cap_pass_to_tx(chan, control);
6975 /* Can't issue SREJ frames in the local busy state.
6976 * Drop this frame, it will be seen as missing
6977 * when local busy is exited.
6979 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6980 BT_DBG("Busy, discarding unexpected seq %d",
6985 /* There was a gap in the sequence, so an SREJ
6986 * must be sent for each missing frame. The
6987 * current frame is stored for later use.
6989 skb_queue_tail(&chan->srej_q, skb);
6991 BT_DBG("Queued %p (queue len %d)", skb,
6992 skb_queue_len(&chan->srej_q));
6994 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
6995 l2cap_seq_list_clear(&chan->srej_list);
6996 l2cap_send_srej(chan, control->txseq);
6998 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT;
7000 case L2CAP_TXSEQ_DUPLICATE:
7001 l2cap_pass_to_tx(chan, control);
7003 case L2CAP_TXSEQ_INVALID_IGNORE:
7005 case L2CAP_TXSEQ_INVALID:
7007 l2cap_send_disconn_req(chan, ECONNRESET);
7011 case L2CAP_EV_RECV_RR:
7012 l2cap_pass_to_tx(chan, control);
7013 if (control->final) {
7014 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
7016 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) &&
7017 !__chan_is_moving(chan)) {
7019 l2cap_retransmit_all(chan, control);
7022 l2cap_ertm_send(chan);
7023 } else if (control->poll) {
7024 l2cap_send_i_or_rr_or_rnr(chan);
7026 if (test_and_clear_bit(CONN_REMOTE_BUSY,
7027 &chan->conn_state) &&
7028 chan->unacked_frames)
7029 __set_retrans_timer(chan);
7031 l2cap_ertm_send(chan);
7034 case L2CAP_EV_RECV_RNR:
7035 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
7036 l2cap_pass_to_tx(chan, control);
7037 if (control && control->poll) {
7038 set_bit(CONN_SEND_FBIT, &chan->conn_state);
7039 l2cap_send_rr_or_rnr(chan, 0);
7041 __clear_retrans_timer(chan);
7042 l2cap_seq_list_clear(&chan->retrans_list);
7044 case L2CAP_EV_RECV_REJ:
7045 l2cap_handle_rej(chan, control);
7047 case L2CAP_EV_RECV_SREJ:
7048 l2cap_handle_srej(chan, control);
7054 if (skb && !skb_in_use) {
7055 BT_DBG("Freeing %p", skb);
7062 static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
7063 struct l2cap_ctrl *control,
7064 struct sk_buff *skb, u8 event)
7067 u16 txseq = control->txseq;
7068 bool skb_in_use = false;
7070 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
7074 case L2CAP_EV_RECV_IFRAME:
7075 switch (l2cap_classify_txseq(chan, txseq)) {
7076 case L2CAP_TXSEQ_EXPECTED:
7077 /* Keep frame for reassembly later */
7078 l2cap_pass_to_tx(chan, control);
7079 skb_queue_tail(&chan->srej_q, skb);
7081 BT_DBG("Queued %p (queue len %d)", skb,
7082 skb_queue_len(&chan->srej_q));
7084 chan->expected_tx_seq = __next_seq(chan, txseq);
7086 case L2CAP_TXSEQ_EXPECTED_SREJ:
7087 l2cap_seq_list_pop(&chan->srej_list);
7089 l2cap_pass_to_tx(chan, control);
7090 skb_queue_tail(&chan->srej_q, skb);
7092 BT_DBG("Queued %p (queue len %d)", skb,
7093 skb_queue_len(&chan->srej_q));
7095 err = l2cap_rx_queued_iframes(chan);
7100 case L2CAP_TXSEQ_UNEXPECTED:
7101 /* Got a frame that can't be reassembled yet.
7102 * Save it for later, and send SREJs to cover
7103 * the missing frames.
7105 skb_queue_tail(&chan->srej_q, skb);
7107 BT_DBG("Queued %p (queue len %d)", skb,
7108 skb_queue_len(&chan->srej_q));
7110 l2cap_pass_to_tx(chan, control);
7111 l2cap_send_srej(chan, control->txseq);
7113 case L2CAP_TXSEQ_UNEXPECTED_SREJ:
7114 /* This frame was requested with an SREJ, but
7115 * some expected retransmitted frames are
7116 * missing. Request retransmission of missing
7119 skb_queue_tail(&chan->srej_q, skb);
7121 BT_DBG("Queued %p (queue len %d)", skb,
7122 skb_queue_len(&chan->srej_q));
7124 l2cap_pass_to_tx(chan, control);
7125 l2cap_send_srej_list(chan, control->txseq);
7127 case L2CAP_TXSEQ_DUPLICATE_SREJ:
7128 /* We've already queued this frame. Drop this copy. */
7129 l2cap_pass_to_tx(chan, control);
7131 case L2CAP_TXSEQ_DUPLICATE:
7132 /* Expecting a later sequence number, so this frame
7133 * was already received. Ignore it completely.
7136 case L2CAP_TXSEQ_INVALID_IGNORE:
7138 case L2CAP_TXSEQ_INVALID:
7140 l2cap_send_disconn_req(chan, ECONNRESET);
7144 case L2CAP_EV_RECV_RR:
7145 l2cap_pass_to_tx(chan, control);
7146 if (control->final) {
7147 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
7149 if (!test_and_clear_bit(CONN_REJ_ACT,
7150 &chan->conn_state)) {
7152 l2cap_retransmit_all(chan, control);
7155 l2cap_ertm_send(chan);
7156 } else if (control->poll) {
7157 if (test_and_clear_bit(CONN_REMOTE_BUSY,
7158 &chan->conn_state) &&
7159 chan->unacked_frames) {
7160 __set_retrans_timer(chan);
7163 set_bit(CONN_SEND_FBIT, &chan->conn_state);
7164 l2cap_send_srej_tail(chan);
7166 if (test_and_clear_bit(CONN_REMOTE_BUSY,
7167 &chan->conn_state) &&
7168 chan->unacked_frames)
7169 __set_retrans_timer(chan);
7171 l2cap_send_ack(chan);
7174 case L2CAP_EV_RECV_RNR:
7175 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
7176 l2cap_pass_to_tx(chan, control);
7177 if (control->poll) {
7178 l2cap_send_srej_tail(chan);
7180 struct l2cap_ctrl rr_control;
7181 memset(&rr_control, 0, sizeof(rr_control));
7182 rr_control.sframe = 1;
7183 rr_control.super = L2CAP_SUPER_RR;
7184 rr_control.reqseq = chan->buffer_seq;
7185 l2cap_send_sframe(chan, &rr_control);
7189 case L2CAP_EV_RECV_REJ:
7190 l2cap_handle_rej(chan, control);
7192 case L2CAP_EV_RECV_SREJ:
7193 l2cap_handle_srej(chan, control);
7197 if (skb && !skb_in_use) {
7198 BT_DBG("Freeing %p", skb);
7205 static int l2cap_finish_move(struct l2cap_chan *chan)
7207 BT_DBG("chan %p", chan);
7209 chan->rx_state = L2CAP_RX_STATE_RECV;
7212 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
7214 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
7216 return l2cap_resegment(chan);
7219 static int l2cap_rx_state_wait_p(struct l2cap_chan *chan,
7220 struct l2cap_ctrl *control,
7221 struct sk_buff *skb, u8 event)
7225 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
7231 l2cap_process_reqseq(chan, control->reqseq);
7233 if (!skb_queue_empty(&chan->tx_q))
7234 chan->tx_send_head = skb_peek(&chan->tx_q);
7236 chan->tx_send_head = NULL;
7238 /* Rewind next_tx_seq to the point expected
7241 chan->next_tx_seq = control->reqseq;
7242 chan->unacked_frames = 0;
7244 err = l2cap_finish_move(chan);
7248 set_bit(CONN_SEND_FBIT, &chan->conn_state);
7249 l2cap_send_i_or_rr_or_rnr(chan);
7251 if (event == L2CAP_EV_RECV_IFRAME)
7254 return l2cap_rx_state_recv(chan, control, NULL, event);
7257 static int l2cap_rx_state_wait_f(struct l2cap_chan *chan,
7258 struct l2cap_ctrl *control,
7259 struct sk_buff *skb, u8 event)
7263 if (!control->final)
7266 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
7268 chan->rx_state = L2CAP_RX_STATE_RECV;
7269 l2cap_process_reqseq(chan, control->reqseq);
7271 if (!skb_queue_empty(&chan->tx_q))
7272 chan->tx_send_head = skb_peek(&chan->tx_q);
7274 chan->tx_send_head = NULL;
7276 /* Rewind next_tx_seq to the point expected
7279 chan->next_tx_seq = control->reqseq;
7280 chan->unacked_frames = 0;
7283 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
7285 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
7287 err = l2cap_resegment(chan);
7290 err = l2cap_rx_state_recv(chan, control, skb, event);
7295 static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq)
7297 /* Make sure reqseq is for a packet that has been sent but not acked */
7300 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq);
7301 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked;
7304 static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
7305 struct sk_buff *skb, u8 event)
7309 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan,
7310 control, skb, event, chan->rx_state);
7312 if (__valid_reqseq(chan, control->reqseq)) {
7313 switch (chan->rx_state) {
7314 case L2CAP_RX_STATE_RECV:
7315 err = l2cap_rx_state_recv(chan, control, skb, event);
7317 case L2CAP_RX_STATE_SREJ_SENT:
7318 err = l2cap_rx_state_srej_sent(chan, control, skb,
7321 case L2CAP_RX_STATE_WAIT_P:
7322 err = l2cap_rx_state_wait_p(chan, control, skb, event);
7324 case L2CAP_RX_STATE_WAIT_F:
7325 err = l2cap_rx_state_wait_f(chan, control, skb, event);
7332 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
7333 control->reqseq, chan->next_tx_seq,
7334 chan->expected_ack_seq);
7335 l2cap_send_disconn_req(chan, ECONNRESET);
7341 static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
7342 struct sk_buff *skb)
7344 /* l2cap_reassemble_sdu may free skb, hence invalidate control, so store
7345 * the txseq field in advance to use it after l2cap_reassemble_sdu
7346 * returns and to avoid the race condition, for example:
7348 * The current thread calls:
7349 * l2cap_reassemble_sdu
7350 * chan->ops->recv == l2cap_sock_recv_cb
7351 * __sock_queue_rcv_skb
7352 * Another thread calls:
7356 * Then the current thread tries to access control, but it was freed by
7357 * skb_free_datagram.
7359 u16 txseq = control->txseq;
7361 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
7364 if (l2cap_classify_txseq(chan, txseq) == L2CAP_TXSEQ_EXPECTED) {
7365 l2cap_pass_to_tx(chan, control);
7367 BT_DBG("buffer_seq %u->%u", chan->buffer_seq,
7368 __next_seq(chan, chan->buffer_seq));
7370 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
7372 l2cap_reassemble_sdu(chan, skb, control);
7375 kfree_skb(chan->sdu);
7378 chan->sdu_last_frag = NULL;
7382 BT_DBG("Freeing %p", skb);
7387 chan->last_acked_seq = txseq;
7388 chan->expected_tx_seq = __next_seq(chan, txseq);
7393 static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
7395 struct l2cap_ctrl *control = &bt_cb(skb)->l2cap;
7399 __unpack_control(chan, skb);
7404 * We can just drop the corrupted I-frame here.
7405 * Receiver will miss it and start proper recovery
7406 * procedures and ask for retransmission.
7408 if (l2cap_check_fcs(chan, skb))
7411 if (!control->sframe && control->sar == L2CAP_SAR_START)
7412 len -= L2CAP_SDULEN_SIZE;
7414 if (chan->fcs == L2CAP_FCS_CRC16)
7415 len -= L2CAP_FCS_SIZE;
7417 if (len > chan->mps) {
7418 l2cap_send_disconn_req(chan, ECONNRESET);
7422 if (chan->ops->filter) {
7423 if (chan->ops->filter(chan, skb))
7427 if (!control->sframe) {
7430 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
7431 control->sar, control->reqseq, control->final,
7434 /* Validate F-bit - F=0 always valid, F=1 only
7435 * valid in TX WAIT_F
7437 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F)
7440 if (chan->mode != L2CAP_MODE_STREAMING) {
7441 event = L2CAP_EV_RECV_IFRAME;
7442 err = l2cap_rx(chan, control, skb, event);
7444 err = l2cap_stream_rx(chan, control, skb);
7448 l2cap_send_disconn_req(chan, ECONNRESET);
7450 const u8 rx_func_to_event[4] = {
7451 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ,
7452 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ
7455 /* Only I-frames are expected in streaming mode */
7456 if (chan->mode == L2CAP_MODE_STREAMING)
7459 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
7460 control->reqseq, control->final, control->poll,
7464 BT_ERR("Trailing bytes: %d in sframe", len);
7465 l2cap_send_disconn_req(chan, ECONNRESET);
7469 /* Validate F and P bits */
7470 if (control->final && (control->poll ||
7471 chan->tx_state != L2CAP_TX_STATE_WAIT_F))
7474 event = rx_func_to_event[control->super];
7475 if (l2cap_rx(chan, control, skb, event))
7476 l2cap_send_disconn_req(chan, ECONNRESET);
7486 static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
7488 struct l2cap_conn *conn = chan->conn;
7489 struct l2cap_le_credits pkt;
7492 return_credits = (chan->imtu / chan->mps) + 1;
7494 if (chan->rx_credits >= return_credits)
7497 return_credits -= chan->rx_credits;
7499 BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
7501 chan->rx_credits += return_credits;
7503 pkt.cid = cpu_to_le16(chan->scid);
7504 pkt.credits = cpu_to_le16(return_credits);
7506 chan->ident = l2cap_get_ident(conn);
7508 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
7511 static int l2cap_ecred_recv(struct l2cap_chan *chan, struct sk_buff *skb)
7515 BT_DBG("SDU reassemble complete: chan %p skb->len %u", chan, skb->len);
7517 /* Wait recv to confirm reception before updating the credits */
7518 err = chan->ops->recv(chan, skb);
7520 /* Update credits whenever an SDU is received */
7521 l2cap_chan_le_send_credits(chan);
7526 static int l2cap_ecred_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
7530 if (!chan->rx_credits) {
7531 BT_ERR("No credits to receive LE L2CAP data");
7532 l2cap_send_disconn_req(chan, ECONNRESET);
7536 if (chan->imtu < skb->len) {
7537 BT_ERR("Too big LE L2CAP PDU");
7542 BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
7544 /* Update if remote had run out of credits, this should only happens
7545 * if the remote is not using the entire MPS.
7547 if (!chan->rx_credits)
7548 l2cap_chan_le_send_credits(chan);
7555 sdu_len = get_unaligned_le16(skb->data);
7556 skb_pull(skb, L2CAP_SDULEN_SIZE);
7558 BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u",
7559 sdu_len, skb->len, chan->imtu);
7561 if (sdu_len > chan->imtu) {
7562 BT_ERR("Too big LE L2CAP SDU length received");
7567 if (skb->len > sdu_len) {
7568 BT_ERR("Too much LE L2CAP data received");
7573 if (skb->len == sdu_len)
7574 return l2cap_ecred_recv(chan, skb);
7577 chan->sdu_len = sdu_len;
7578 chan->sdu_last_frag = skb;
7580 /* Detect if remote is not able to use the selected MPS */
7581 if (skb->len + L2CAP_SDULEN_SIZE < chan->mps) {
7582 u16 mps_len = skb->len + L2CAP_SDULEN_SIZE;
7584 /* Adjust the number of credits */
7585 BT_DBG("chan->mps %u -> %u", chan->mps, mps_len);
7586 chan->mps = mps_len;
7587 l2cap_chan_le_send_credits(chan);
7593 BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u",
7594 chan->sdu->len, skb->len, chan->sdu_len);
7596 if (chan->sdu->len + skb->len > chan->sdu_len) {
7597 BT_ERR("Too much LE L2CAP data received");
7602 append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
7605 if (chan->sdu->len == chan->sdu_len) {
7606 err = l2cap_ecred_recv(chan, chan->sdu);
7609 chan->sdu_last_frag = NULL;
7617 kfree_skb(chan->sdu);
7619 chan->sdu_last_frag = NULL;
7623 /* We can't return an error here since we took care of the skb
7624 * freeing internally. An error return would cause the caller to
7625 * do a double-free of the skb.
7630 static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
7631 struct sk_buff *skb)
7633 struct l2cap_chan *chan;
7635 chan = l2cap_get_chan_by_scid(conn, cid);
7637 if (cid == L2CAP_CID_A2MP) {
7638 chan = a2mp_channel_create(conn, skb);
7644 l2cap_chan_hold(chan);
7645 l2cap_chan_lock(chan);
7647 BT_DBG("unknown cid 0x%4.4x", cid);
7648 /* Drop packet and return */
7654 BT_DBG("chan %p, len %d", chan, skb->len);
7656 /* If we receive data on a fixed channel before the info req/rsp
7657 * procedure is done simply assume that the channel is supported
7658 * and mark it as ready.
7660 if (chan->chan_type == L2CAP_CHAN_FIXED)
7661 l2cap_chan_ready(chan);
7663 if (chan->state != BT_CONNECTED)
7666 switch (chan->mode) {
7667 case L2CAP_MODE_LE_FLOWCTL:
7668 case L2CAP_MODE_EXT_FLOWCTL:
7669 if (l2cap_ecred_data_rcv(chan, skb) < 0)
7674 case L2CAP_MODE_BASIC:
7675 /* If socket recv buffers overflows we drop data here
7676 * which is *bad* because L2CAP has to be reliable.
7677 * But we don't have any other choice. L2CAP doesn't
7678 * provide flow control mechanism. */
7680 if (chan->imtu < skb->len) {
7681 BT_ERR("Dropping L2CAP data: receive buffer overflow");
7685 if (!chan->ops->recv(chan, skb))
7689 case L2CAP_MODE_ERTM:
7690 case L2CAP_MODE_STREAMING:
7691 l2cap_data_rcv(chan, skb);
7695 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
7703 l2cap_chan_unlock(chan);
7704 l2cap_chan_put(chan);
7707 static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
7708 struct sk_buff *skb)
7710 struct hci_conn *hcon = conn->hcon;
7711 struct l2cap_chan *chan;
7713 if (hcon->type != ACL_LINK)
7716 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst,
7721 BT_DBG("chan %p, len %d", chan, skb->len);
7723 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
7726 if (chan->imtu < skb->len)
7729 /* Store remote BD_ADDR and PSM for msg_name */
7730 bacpy(&bt_cb(skb)->l2cap.bdaddr, &hcon->dst);
7731 bt_cb(skb)->l2cap.psm = psm;
7733 if (!chan->ops->recv(chan, skb)) {
7734 l2cap_chan_put(chan);
7739 l2cap_chan_put(chan);
7744 static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
7746 struct l2cap_hdr *lh = (void *) skb->data;
7747 struct hci_conn *hcon = conn->hcon;
7751 if (hcon->state != BT_CONNECTED) {
7752 BT_DBG("queueing pending rx skb");
7753 skb_queue_tail(&conn->pending_rx, skb);
7757 skb_pull(skb, L2CAP_HDR_SIZE);
7758 cid = __le16_to_cpu(lh->cid);
7759 len = __le16_to_cpu(lh->len);
7761 if (len != skb->len) {
7766 /* Since we can't actively block incoming LE connections we must
7767 * at least ensure that we ignore incoming data from them.
7769 if (hcon->type == LE_LINK &&
7770 hci_bdaddr_list_lookup(&hcon->hdev->reject_list, &hcon->dst,
7771 bdaddr_dst_type(hcon))) {
7776 BT_DBG("len %d, cid 0x%4.4x", len, cid);
7779 case L2CAP_CID_SIGNALING:
7780 l2cap_sig_channel(conn, skb);
7783 case L2CAP_CID_CONN_LESS:
7784 psm = get_unaligned((__le16 *) skb->data);
7785 skb_pull(skb, L2CAP_PSMLEN_SIZE);
7786 l2cap_conless_channel(conn, psm, skb);
7789 case L2CAP_CID_LE_SIGNALING:
7790 l2cap_le_sig_channel(conn, skb);
7794 l2cap_data_channel(conn, cid, skb);
7799 static void process_pending_rx(struct work_struct *work)
7801 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
7803 struct sk_buff *skb;
7807 while ((skb = skb_dequeue(&conn->pending_rx)))
7808 l2cap_recv_frame(conn, skb);
7811 static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
7813 struct l2cap_conn *conn = hcon->l2cap_data;
7814 struct hci_chan *hchan;
7819 hchan = hci_chan_create(hcon);
7823 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
7825 hci_chan_del(hchan);
7829 kref_init(&conn->ref);
7830 hcon->l2cap_data = conn;
7831 conn->hcon = hci_conn_get(hcon);
7832 conn->hchan = hchan;
7834 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
7836 switch (hcon->type) {
7838 if (hcon->hdev->le_mtu) {
7839 conn->mtu = hcon->hdev->le_mtu;
7844 conn->mtu = hcon->hdev->acl_mtu;
7848 conn->feat_mask = 0;
7850 conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS;
7852 if (hcon->type == ACL_LINK &&
7853 hci_dev_test_flag(hcon->hdev, HCI_HS_ENABLED))
7854 conn->local_fixed_chan |= L2CAP_FC_A2MP;
7856 if (hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED) &&
7857 (bredr_sc_enabled(hcon->hdev) ||
7858 hci_dev_test_flag(hcon->hdev, HCI_FORCE_BREDR_SMP)))
7859 conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR;
7861 mutex_init(&conn->ident_lock);
7862 mutex_init(&conn->chan_lock);
7864 INIT_LIST_HEAD(&conn->chan_l);
7865 INIT_LIST_HEAD(&conn->users);
7867 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
7869 skb_queue_head_init(&conn->pending_rx);
7870 INIT_WORK(&conn->pending_rx_work, process_pending_rx);
7871 INIT_WORK(&conn->id_addr_update_work, l2cap_conn_update_id_addr);
7873 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
7878 static bool is_valid_psm(u16 psm, u8 dst_type)
7883 if (bdaddr_type_is_le(dst_type))
7884 return (psm <= 0x00ff);
7886 /* PSM must be odd and lsb of upper byte must be 0 */
7887 return ((psm & 0x0101) == 0x0001);
7890 struct l2cap_chan_data {
7891 struct l2cap_chan *chan;
7896 static void l2cap_chan_by_pid(struct l2cap_chan *chan, void *data)
7898 struct l2cap_chan_data *d = data;
7901 if (chan == d->chan)
7904 if (!test_bit(FLAG_DEFER_SETUP, &chan->flags))
7907 pid = chan->ops->get_peer_pid(chan);
7909 /* Only count deferred channels with the same PID/PSM */
7910 if (d->pid != pid || chan->psm != d->chan->psm || chan->ident ||
7911 chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT)
7917 int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
7918 bdaddr_t *dst, u8 dst_type)
7920 struct l2cap_conn *conn;
7921 struct hci_conn *hcon;
7922 struct hci_dev *hdev;
7925 BT_DBG("%pMR -> %pMR (type %u) psm 0x%4.4x mode 0x%2.2x", &chan->src,
7926 dst, dst_type, __le16_to_cpu(psm), chan->mode);
7928 hdev = hci_get_route(dst, &chan->src, chan->src_type);
7930 return -EHOSTUNREACH;
7934 if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid &&
7935 chan->chan_type != L2CAP_CHAN_RAW) {
7940 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) {
7945 if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) {
7950 switch (chan->mode) {
7951 case L2CAP_MODE_BASIC:
7953 case L2CAP_MODE_LE_FLOWCTL:
7955 case L2CAP_MODE_EXT_FLOWCTL:
7956 if (!enable_ecred) {
7961 case L2CAP_MODE_ERTM:
7962 case L2CAP_MODE_STREAMING:
7971 switch (chan->state) {
7975 /* Already connecting */
7980 /* Already connected */
7994 /* Set destination address and psm */
7995 bacpy(&chan->dst, dst);
7996 chan->dst_type = dst_type;
8001 if (bdaddr_type_is_le(dst_type)) {
8002 /* Convert from L2CAP channel address type to HCI address type
8004 if (dst_type == BDADDR_LE_PUBLIC)
8005 dst_type = ADDR_LE_DEV_PUBLIC;
8007 dst_type = ADDR_LE_DEV_RANDOM;
8009 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
8010 hcon = hci_connect_le(hdev, dst, dst_type, false,
8012 HCI_LE_CONN_TIMEOUT,
8015 hcon = hci_connect_le_scan(hdev, dst, dst_type,
8017 HCI_LE_CONN_TIMEOUT,
8018 CONN_REASON_L2CAP_CHAN);
8021 u8 auth_type = l2cap_get_auth_type(chan);
8022 hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type,
8023 CONN_REASON_L2CAP_CHAN);
8027 err = PTR_ERR(hcon);
8031 conn = l2cap_conn_add(hcon);
8033 hci_conn_drop(hcon);
8038 if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) {
8039 struct l2cap_chan_data data;
8042 data.pid = chan->ops->get_peer_pid(chan);
8045 l2cap_chan_list(conn, l2cap_chan_by_pid, &data);
8047 /* Check if there isn't too many channels being connected */
8048 if (data.count > L2CAP_ECRED_CONN_SCID_MAX) {
8049 hci_conn_drop(hcon);
8055 mutex_lock(&conn->chan_lock);
8056 l2cap_chan_lock(chan);
8058 if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
8059 hci_conn_drop(hcon);
8064 /* Update source addr of the socket */
8065 bacpy(&chan->src, &hcon->src);
8066 chan->src_type = bdaddr_src_type(hcon);
8068 __l2cap_chan_add(conn, chan);
8070 /* l2cap_chan_add takes its own ref so we can drop this one */
8071 hci_conn_drop(hcon);
8073 l2cap_state_change(chan, BT_CONNECT);
8074 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
8076 /* Release chan->sport so that it can be reused by other
8077 * sockets (as it's only used for listening sockets).
8079 write_lock(&chan_list_lock);
8081 write_unlock(&chan_list_lock);
8083 if (hcon->state == BT_CONNECTED) {
8084 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
8085 __clear_chan_timer(chan);
8086 if (l2cap_chan_check_security(chan, true))
8087 l2cap_state_change(chan, BT_CONNECTED);
8089 l2cap_do_start(chan);
8095 l2cap_chan_unlock(chan);
8096 mutex_unlock(&conn->chan_lock);
8098 hci_dev_unlock(hdev);
8102 EXPORT_SYMBOL_GPL(l2cap_chan_connect);
8104 static void l2cap_ecred_reconfigure(struct l2cap_chan *chan)
8106 struct l2cap_conn *conn = chan->conn;
8108 struct l2cap_ecred_reconf_req req;
8112 pdu.req.mtu = cpu_to_le16(chan->imtu);
8113 pdu.req.mps = cpu_to_le16(chan->mps);
8114 pdu.scid = cpu_to_le16(chan->scid);
8116 chan->ident = l2cap_get_ident(conn);
8118 l2cap_send_cmd(conn, chan->ident, L2CAP_ECRED_RECONF_REQ,
8122 int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu)
8124 if (chan->imtu > mtu)
8127 BT_DBG("chan %p mtu 0x%4.4x", chan, mtu);
8131 l2cap_ecred_reconfigure(chan);
8136 /* ---- L2CAP interface with lower layer (HCI) ---- */
8138 int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
8140 int exact = 0, lm1 = 0, lm2 = 0;
8141 struct l2cap_chan *c;
8143 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
8145 /* Find listening sockets and check their link_mode */
8146 read_lock(&chan_list_lock);
8147 list_for_each_entry(c, &chan_list, global_l) {
8148 if (c->state != BT_LISTEN)
8151 if (!bacmp(&c->src, &hdev->bdaddr)) {
8152 lm1 |= HCI_LM_ACCEPT;
8153 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
8154 lm1 |= HCI_LM_MASTER;
8156 } else if (!bacmp(&c->src, BDADDR_ANY)) {
8157 lm2 |= HCI_LM_ACCEPT;
8158 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
8159 lm2 |= HCI_LM_MASTER;
8162 read_unlock(&chan_list_lock);
8164 return exact ? lm1 : lm2;
8167 /* Find the next fixed channel in BT_LISTEN state, continue iteration
8168 * from an existing channel in the list or from the beginning of the
8169 * global list (by passing NULL as first parameter).
8171 static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c,
8172 struct hci_conn *hcon)
8174 u8 src_type = bdaddr_src_type(hcon);
8176 read_lock(&chan_list_lock);
8179 c = list_next_entry(c, global_l);
8181 c = list_entry(chan_list.next, typeof(*c), global_l);
8183 list_for_each_entry_from(c, &chan_list, global_l) {
8184 if (c->chan_type != L2CAP_CHAN_FIXED)
8186 if (c->state != BT_LISTEN)
8188 if (bacmp(&c->src, &hcon->src) && bacmp(&c->src, BDADDR_ANY))
8190 if (src_type != c->src_type)
8193 c = l2cap_chan_hold_unless_zero(c);
8194 read_unlock(&chan_list_lock);
8198 read_unlock(&chan_list_lock);
8203 static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
8205 struct hci_dev *hdev = hcon->hdev;
8206 struct l2cap_conn *conn;
8207 struct l2cap_chan *pchan;
8210 if (hcon->type != ACL_LINK && hcon->type != LE_LINK)
8213 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
8216 l2cap_conn_del(hcon, bt_to_errno(status));
8220 conn = l2cap_conn_add(hcon);
8224 dst_type = bdaddr_dst_type(hcon);
8226 /* If device is blocked, do not create channels for it */
8227 if (hci_bdaddr_list_lookup(&hdev->reject_list, &hcon->dst, dst_type))
8230 /* Find fixed channels and notify them of the new connection. We
8231 * use multiple individual lookups, continuing each time where
8232 * we left off, because the list lock would prevent calling the
8233 * potentially sleeping l2cap_chan_lock() function.
8235 pchan = l2cap_global_fixed_chan(NULL, hcon);
8237 struct l2cap_chan *chan, *next;
8239 /* Client fixed channels should override server ones */
8240 if (__l2cap_get_chan_by_dcid(conn, pchan->scid))
8243 l2cap_chan_lock(pchan);
8244 chan = pchan->ops->new_connection(pchan);
8246 bacpy(&chan->src, &hcon->src);
8247 bacpy(&chan->dst, &hcon->dst);
8248 chan->src_type = bdaddr_src_type(hcon);
8249 chan->dst_type = dst_type;
8251 __l2cap_chan_add(conn, chan);
8254 l2cap_chan_unlock(pchan);
8256 next = l2cap_global_fixed_chan(pchan, hcon);
8257 l2cap_chan_put(pchan);
8261 l2cap_conn_ready(conn);
8264 int l2cap_disconn_ind(struct hci_conn *hcon)
8266 struct l2cap_conn *conn = hcon->l2cap_data;
8268 BT_DBG("hcon %p", hcon);
8271 return HCI_ERROR_REMOTE_USER_TERM;
8272 return conn->disc_reason;
8275 static void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
8277 if (hcon->type != ACL_LINK && hcon->type != LE_LINK)
8280 BT_DBG("hcon %p reason %d", hcon, reason);
8282 l2cap_conn_del(hcon, bt_to_errno(reason));
8285 static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
8287 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
8290 if (encrypt == 0x00) {
8291 if (chan->sec_level == BT_SECURITY_MEDIUM) {
8292 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
8293 } else if (chan->sec_level == BT_SECURITY_HIGH ||
8294 chan->sec_level == BT_SECURITY_FIPS)
8295 l2cap_chan_close(chan, ECONNREFUSED);
8297 if (chan->sec_level == BT_SECURITY_MEDIUM)
8298 __clear_chan_timer(chan);
8302 static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
8304 struct l2cap_conn *conn = hcon->l2cap_data;
8305 struct l2cap_chan *chan;
8310 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
8312 mutex_lock(&conn->chan_lock);
8314 list_for_each_entry(chan, &conn->chan_l, list) {
8315 l2cap_chan_lock(chan);
8317 BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
8318 state_to_string(chan->state));
8320 if (chan->scid == L2CAP_CID_A2MP) {
8321 l2cap_chan_unlock(chan);
8325 if (!status && encrypt)
8326 chan->sec_level = hcon->sec_level;
8328 if (!__l2cap_no_conn_pending(chan)) {
8329 l2cap_chan_unlock(chan);
8333 if (!status && (chan->state == BT_CONNECTED ||
8334 chan->state == BT_CONFIG)) {
8335 chan->ops->resume(chan);
8336 l2cap_check_encryption(chan, encrypt);
8337 l2cap_chan_unlock(chan);
8341 if (chan->state == BT_CONNECT) {
8342 if (!status && l2cap_check_enc_key_size(hcon))
8343 l2cap_start_connection(chan);
8345 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
8346 } else if (chan->state == BT_CONNECT2 &&
8347 !(chan->mode == L2CAP_MODE_EXT_FLOWCTL ||
8348 chan->mode == L2CAP_MODE_LE_FLOWCTL)) {
8349 struct l2cap_conn_rsp rsp;
8352 if (!status && l2cap_check_enc_key_size(hcon)) {
8353 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
8354 res = L2CAP_CR_PEND;
8355 stat = L2CAP_CS_AUTHOR_PEND;
8356 chan->ops->defer(chan);
8358 l2cap_state_change(chan, BT_CONFIG);
8359 res = L2CAP_CR_SUCCESS;
8360 stat = L2CAP_CS_NO_INFO;
8363 l2cap_state_change(chan, BT_DISCONN);
8364 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
8365 res = L2CAP_CR_SEC_BLOCK;
8366 stat = L2CAP_CS_NO_INFO;
8369 rsp.scid = cpu_to_le16(chan->dcid);
8370 rsp.dcid = cpu_to_le16(chan->scid);
8371 rsp.result = cpu_to_le16(res);
8372 rsp.status = cpu_to_le16(stat);
8373 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
8376 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) &&
8377 res == L2CAP_CR_SUCCESS) {
8379 set_bit(CONF_REQ_SENT, &chan->conf_state);
8380 l2cap_send_cmd(conn, l2cap_get_ident(conn),
8382 l2cap_build_conf_req(chan, buf, sizeof(buf)),
8384 chan->num_conf_req++;
8388 l2cap_chan_unlock(chan);
8391 mutex_unlock(&conn->chan_lock);
8394 /* Append fragment into frame respecting the maximum len of rx_skb */
8395 static int l2cap_recv_frag(struct l2cap_conn *conn, struct sk_buff *skb,
8398 if (!conn->rx_skb) {
8399 /* Allocate skb for the complete frame (with header) */
8400 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
8407 /* Copy as much as the rx_skb can hold */
8408 len = min_t(u16, len, skb->len);
8409 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, len), len);
8411 conn->rx_len -= len;
8416 static int l2cap_recv_len(struct l2cap_conn *conn, struct sk_buff *skb)
8418 struct sk_buff *rx_skb;
8421 /* Append just enough to complete the header */
8422 len = l2cap_recv_frag(conn, skb, L2CAP_LEN_SIZE - conn->rx_skb->len);
8424 /* If header could not be read just continue */
8425 if (len < 0 || conn->rx_skb->len < L2CAP_LEN_SIZE)
8428 rx_skb = conn->rx_skb;
8429 len = get_unaligned_le16(rx_skb->data);
8431 /* Check if rx_skb has enough space to received all fragments */
8432 if (len + (L2CAP_HDR_SIZE - L2CAP_LEN_SIZE) <= skb_tailroom(rx_skb)) {
8433 /* Update expected len */
8434 conn->rx_len = len + (L2CAP_HDR_SIZE - L2CAP_LEN_SIZE);
8435 return L2CAP_LEN_SIZE;
8438 /* Reset conn->rx_skb since it will need to be reallocated in order to
8439 * fit all fragments.
8441 conn->rx_skb = NULL;
8443 /* Reallocates rx_skb using the exact expected length */
8444 len = l2cap_recv_frag(conn, rx_skb,
8445 len + (L2CAP_HDR_SIZE - L2CAP_LEN_SIZE));
8451 static void l2cap_recv_reset(struct l2cap_conn *conn)
8453 kfree_skb(conn->rx_skb);
8454 conn->rx_skb = NULL;
8458 void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
8460 struct l2cap_conn *conn = hcon->l2cap_data;
8463 /* For AMP controller do not create l2cap conn */
8464 if (!conn && hcon->hdev->dev_type != HCI_PRIMARY)
8468 conn = l2cap_conn_add(hcon);
8473 BT_DBG("conn %p len %u flags 0x%x", conn, skb->len, flags);
8477 case ACL_START_NO_FLUSH:
8480 BT_ERR("Unexpected start frame (len %d)", skb->len);
8481 l2cap_recv_reset(conn);
8482 l2cap_conn_unreliable(conn, ECOMM);
8485 /* Start fragment may not contain the L2CAP length so just
8486 * copy the initial byte when that happens and use conn->mtu as
8489 if (skb->len < L2CAP_LEN_SIZE) {
8490 l2cap_recv_frag(conn, skb, conn->mtu);
8494 len = get_unaligned_le16(skb->data) + L2CAP_HDR_SIZE;
8496 if (len == skb->len) {
8497 /* Complete frame received */
8498 l2cap_recv_frame(conn, skb);
8502 BT_DBG("Start: total len %d, frag len %u", len, skb->len);
8504 if (skb->len > len) {
8505 BT_ERR("Frame is too long (len %u, expected len %d)",
8507 l2cap_conn_unreliable(conn, ECOMM);
8511 /* Append fragment into frame (with header) */
8512 if (l2cap_recv_frag(conn, skb, len) < 0)
8518 BT_DBG("Cont: frag len %u (expecting %u)", skb->len, conn->rx_len);
8520 if (!conn->rx_skb) {
8521 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
8522 l2cap_conn_unreliable(conn, ECOMM);
8526 /* Complete the L2CAP length if it has not been read */
8527 if (conn->rx_skb->len < L2CAP_LEN_SIZE) {
8528 if (l2cap_recv_len(conn, skb) < 0) {
8529 l2cap_conn_unreliable(conn, ECOMM);
8533 /* Header still could not be read just continue */
8534 if (conn->rx_skb->len < L2CAP_LEN_SIZE)
8538 if (skb->len > conn->rx_len) {
8539 BT_ERR("Fragment is too long (len %u, expected %u)",
8540 skb->len, conn->rx_len);
8541 l2cap_recv_reset(conn);
8542 l2cap_conn_unreliable(conn, ECOMM);
8546 /* Append fragment into frame (with header) */
8547 l2cap_recv_frag(conn, skb, skb->len);
8549 if (!conn->rx_len) {
8550 /* Complete frame received. l2cap_recv_frame
8551 * takes ownership of the skb so set the global
8552 * rx_skb pointer to NULL first.
8554 struct sk_buff *rx_skb = conn->rx_skb;
8555 conn->rx_skb = NULL;
8556 l2cap_recv_frame(conn, rx_skb);
8565 static struct hci_cb l2cap_cb = {
8567 .connect_cfm = l2cap_connect_cfm,
8568 .disconn_cfm = l2cap_disconn_cfm,
8569 .security_cfm = l2cap_security_cfm,
8572 static int l2cap_debugfs_show(struct seq_file *f, void *p)
8574 struct l2cap_chan *c;
8576 read_lock(&chan_list_lock);
8578 list_for_each_entry(c, &chan_list, global_l) {
8579 seq_printf(f, "%pMR (%u) %pMR (%u) %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
8580 &c->src, c->src_type, &c->dst, c->dst_type,
8581 c->state, __le16_to_cpu(c->psm),
8582 c->scid, c->dcid, c->imtu, c->omtu,
8583 c->sec_level, c->mode);
8586 read_unlock(&chan_list_lock);
8591 DEFINE_SHOW_ATTRIBUTE(l2cap_debugfs);
8593 static struct dentry *l2cap_debugfs;
8595 int __init l2cap_init(void)
8599 err = l2cap_init_sockets();
8603 hci_register_cb(&l2cap_cb);
8605 if (IS_ERR_OR_NULL(bt_debugfs))
8608 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
8609 NULL, &l2cap_debugfs_fops);
8614 void l2cap_exit(void)
8616 debugfs_remove(l2cap_debugfs);
8617 hci_unregister_cb(&l2cap_cb);
8618 l2cap_cleanup_sockets();
8621 module_param(disable_ertm, bool, 0644);
8622 MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
8624 module_param(enable_ecred, bool, 0644);
8625 MODULE_PARM_DESC(enable_ecred, "Enable enhanced credit flow control mode");