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.
7 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 2 as
11 published by the Free Software Foundation;
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
16 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
18 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
23 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
24 SOFTWARE IS DISCLAIMED.
27 /* Bluetooth L2CAP core. */
29 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/capability.h>
33 #include <linux/errno.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/slab.h>
37 #include <linux/poll.h>
38 #include <linux/fcntl.h>
39 #include <linux/init.h>
40 #include <linux/interrupt.h>
41 #include <linux/socket.h>
42 #include <linux/skbuff.h>
43 #include <linux/list.h>
44 #include <linux/device.h>
45 #include <linux/debugfs.h>
46 #include <linux/seq_file.h>
47 #include <linux/uaccess.h>
48 #include <linux/crc16.h>
51 #include <asm/system.h>
52 #include <asm/unaligned.h>
54 #include <net/bluetooth/bluetooth.h>
55 #include <net/bluetooth/hci_core.h>
56 #include <net/bluetooth/l2cap.h>
57 #include <net/bluetooth/smp.h>
62 static u32 l2cap_feat_mask
= L2CAP_FEAT_FIXED_CHAN
;
63 static u8 l2cap_fixed_chan
[8] = { 0x02, };
65 static LIST_HEAD(chan_list
);
66 static DEFINE_RWLOCK(chan_list_lock
);
68 static struct sk_buff
*l2cap_build_cmd(struct l2cap_conn
*conn
,
69 u8 code
, u8 ident
, u16 dlen
, void *data
);
70 static void l2cap_send_cmd(struct l2cap_conn
*conn
, u8 ident
, u8 code
, u16 len
,
72 static int l2cap_build_conf_req(struct l2cap_chan
*chan
, void *data
);
73 static void l2cap_send_disconn_req(struct l2cap_conn
*conn
,
74 struct l2cap_chan
*chan
, int err
);
76 static int l2cap_ertm_data_rcv(struct sock
*sk
, struct sk_buff
*skb
);
78 /* ---- L2CAP channels ---- */
80 static inline void chan_hold(struct l2cap_chan
*c
)
82 atomic_inc(&c
->refcnt
);
85 static inline void chan_put(struct l2cap_chan
*c
)
87 if (atomic_dec_and_test(&c
->refcnt
))
91 static struct l2cap_chan
*__l2cap_get_chan_by_dcid(struct l2cap_conn
*conn
, u16 cid
)
95 list_for_each_entry(c
, &conn
->chan_l
, list
) {
103 static struct l2cap_chan
*__l2cap_get_chan_by_scid(struct l2cap_conn
*conn
, u16 cid
)
105 struct l2cap_chan
*c
;
107 list_for_each_entry(c
, &conn
->chan_l
, list
) {
114 /* Find channel with given SCID.
115 * Returns locked socket */
116 static struct l2cap_chan
*l2cap_get_chan_by_scid(struct l2cap_conn
*conn
, u16 cid
)
118 struct l2cap_chan
*c
;
120 read_lock(&conn
->chan_lock
);
121 c
= __l2cap_get_chan_by_scid(conn
, cid
);
124 read_unlock(&conn
->chan_lock
);
128 static struct l2cap_chan
*__l2cap_get_chan_by_ident(struct l2cap_conn
*conn
, u8 ident
)
130 struct l2cap_chan
*c
;
132 list_for_each_entry(c
, &conn
->chan_l
, list
) {
133 if (c
->ident
== ident
)
139 static inline struct l2cap_chan
*l2cap_get_chan_by_ident(struct l2cap_conn
*conn
, u8 ident
)
141 struct l2cap_chan
*c
;
143 read_lock(&conn
->chan_lock
);
144 c
= __l2cap_get_chan_by_ident(conn
, ident
);
147 read_unlock(&conn
->chan_lock
);
151 static struct l2cap_chan
*__l2cap_global_chan_by_addr(__le16 psm
, bdaddr_t
*src
)
153 struct l2cap_chan
*c
;
155 list_for_each_entry(c
, &chan_list
, global_l
) {
156 if (c
->sport
== psm
&& !bacmp(&bt_sk(c
->sk
)->src
, src
))
165 int l2cap_add_psm(struct l2cap_chan
*chan
, bdaddr_t
*src
, __le16 psm
)
169 write_lock_bh(&chan_list_lock
);
171 if (psm
&& __l2cap_global_chan_by_addr(psm
, src
)) {
184 for (p
= 0x1001; p
< 0x1100; p
+= 2)
185 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p
), src
)) {
186 chan
->psm
= cpu_to_le16(p
);
187 chan
->sport
= cpu_to_le16(p
);
194 write_unlock_bh(&chan_list_lock
);
198 int l2cap_add_scid(struct l2cap_chan
*chan
, __u16 scid
)
200 write_lock_bh(&chan_list_lock
);
204 write_unlock_bh(&chan_list_lock
);
209 static u16
l2cap_alloc_cid(struct l2cap_conn
*conn
)
211 u16 cid
= L2CAP_CID_DYN_START
;
213 for (; cid
< L2CAP_CID_DYN_END
; cid
++) {
214 if (!__l2cap_get_chan_by_scid(conn
, cid
))
221 static void l2cap_set_timer(struct l2cap_chan
*chan
, struct timer_list
*timer
, long timeout
)
223 BT_DBG("chan %p state %d timeout %ld", chan
->sk
, chan
->state
, timeout
);
225 if (!mod_timer(timer
, jiffies
+ msecs_to_jiffies(timeout
)))
229 static void l2cap_clear_timer(struct l2cap_chan
*chan
, struct timer_list
*timer
)
231 BT_DBG("chan %p state %d", chan
, chan
->state
);
233 if (timer_pending(timer
) && del_timer(timer
))
237 static void l2cap_state_change(struct l2cap_chan
*chan
, int state
)
240 chan
->ops
->state_change(chan
->data
, state
);
243 static void l2cap_chan_timeout(unsigned long arg
)
245 struct l2cap_chan
*chan
= (struct l2cap_chan
*) arg
;
246 struct sock
*sk
= chan
->sk
;
249 BT_DBG("chan %p state %d", chan
, chan
->state
);
253 if (sock_owned_by_user(sk
)) {
254 /* sk is owned by user. Try again later */
255 __set_chan_timer(chan
, HZ
/ 5);
261 if (chan
->state
== BT_CONNECTED
|| chan
->state
== BT_CONFIG
)
262 reason
= ECONNREFUSED
;
263 else if (chan
->state
== BT_CONNECT
&&
264 chan
->sec_level
!= BT_SECURITY_SDP
)
265 reason
= ECONNREFUSED
;
269 l2cap_chan_close(chan
, reason
);
273 chan
->ops
->close(chan
->data
);
277 struct l2cap_chan
*l2cap_chan_create(struct sock
*sk
)
279 struct l2cap_chan
*chan
;
281 chan
= kzalloc(sizeof(*chan
), GFP_ATOMIC
);
287 write_lock_bh(&chan_list_lock
);
288 list_add(&chan
->global_l
, &chan_list
);
289 write_unlock_bh(&chan_list_lock
);
291 setup_timer(&chan
->chan_timer
, l2cap_chan_timeout
, (unsigned long) chan
);
293 chan
->state
= BT_OPEN
;
295 atomic_set(&chan
->refcnt
, 1);
300 void l2cap_chan_destroy(struct l2cap_chan
*chan
)
302 write_lock_bh(&chan_list_lock
);
303 list_del(&chan
->global_l
);
304 write_unlock_bh(&chan_list_lock
);
309 static void __l2cap_chan_add(struct l2cap_conn
*conn
, struct l2cap_chan
*chan
)
311 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn
,
312 chan
->psm
, chan
->dcid
);
314 conn
->disc_reason
= 0x13;
318 if (chan
->chan_type
== L2CAP_CHAN_CONN_ORIENTED
) {
319 if (conn
->hcon
->type
== LE_LINK
) {
321 chan
->omtu
= L2CAP_LE_DEFAULT_MTU
;
322 chan
->scid
= L2CAP_CID_LE_DATA
;
323 chan
->dcid
= L2CAP_CID_LE_DATA
;
325 /* Alloc CID for connection-oriented socket */
326 chan
->scid
= l2cap_alloc_cid(conn
);
327 chan
->omtu
= L2CAP_DEFAULT_MTU
;
329 } else if (chan
->chan_type
== L2CAP_CHAN_CONN_LESS
) {
330 /* Connectionless socket */
331 chan
->scid
= L2CAP_CID_CONN_LESS
;
332 chan
->dcid
= L2CAP_CID_CONN_LESS
;
333 chan
->omtu
= L2CAP_DEFAULT_MTU
;
335 /* Raw socket can send/recv signalling messages only */
336 chan
->scid
= L2CAP_CID_SIGNALING
;
337 chan
->dcid
= L2CAP_CID_SIGNALING
;
338 chan
->omtu
= L2CAP_DEFAULT_MTU
;
341 chan
->local_id
= L2CAP_BESTEFFORT_ID
;
342 chan
->local_stype
= L2CAP_SERV_BESTEFFORT
;
343 chan
->local_msdu
= L2CAP_DEFAULT_MAX_SDU_SIZE
;
344 chan
->local_sdu_itime
= L2CAP_DEFAULT_SDU_ITIME
;
345 chan
->local_acc_lat
= L2CAP_DEFAULT_ACC_LAT
;
346 chan
->local_flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
350 list_add(&chan
->list
, &conn
->chan_l
);
354 * Must be called on the locked socket. */
355 static void l2cap_chan_del(struct l2cap_chan
*chan
, int err
)
357 struct sock
*sk
= chan
->sk
;
358 struct l2cap_conn
*conn
= chan
->conn
;
359 struct sock
*parent
= bt_sk(sk
)->parent
;
361 __clear_chan_timer(chan
);
363 BT_DBG("chan %p, conn %p, err %d", chan
, conn
, err
);
366 /* Delete from channel list */
367 write_lock_bh(&conn
->chan_lock
);
368 list_del(&chan
->list
);
369 write_unlock_bh(&conn
->chan_lock
);
373 hci_conn_put(conn
->hcon
);
376 l2cap_state_change(chan
, BT_CLOSED
);
377 sock_set_flag(sk
, SOCK_ZAPPED
);
383 bt_accept_unlink(sk
);
384 parent
->sk_data_ready(parent
, 0);
386 sk
->sk_state_change(sk
);
388 if (!(test_bit(CONF_OUTPUT_DONE
, &chan
->conf_state
) &&
389 test_bit(CONF_INPUT_DONE
, &chan
->conf_state
)))
392 skb_queue_purge(&chan
->tx_q
);
394 if (chan
->mode
== L2CAP_MODE_ERTM
) {
395 struct srej_list
*l
, *tmp
;
397 __clear_retrans_timer(chan
);
398 __clear_monitor_timer(chan
);
399 __clear_ack_timer(chan
);
401 skb_queue_purge(&chan
->srej_q
);
403 list_for_each_entry_safe(l
, tmp
, &chan
->srej_l
, list
) {
410 static void l2cap_chan_cleanup_listen(struct sock
*parent
)
414 BT_DBG("parent %p", parent
);
416 /* Close not yet accepted channels */
417 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
418 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
419 __clear_chan_timer(chan
);
421 l2cap_chan_close(chan
, ECONNRESET
);
423 chan
->ops
->close(chan
->data
);
427 void l2cap_chan_close(struct l2cap_chan
*chan
, int reason
)
429 struct l2cap_conn
*conn
= chan
->conn
;
430 struct sock
*sk
= chan
->sk
;
432 BT_DBG("chan %p state %d socket %p", chan
, chan
->state
, sk
->sk_socket
);
434 switch (chan
->state
) {
436 l2cap_chan_cleanup_listen(sk
);
438 l2cap_state_change(chan
, BT_CLOSED
);
439 sock_set_flag(sk
, SOCK_ZAPPED
);
444 if (chan
->chan_type
== L2CAP_CHAN_CONN_ORIENTED
&&
445 conn
->hcon
->type
== ACL_LINK
) {
446 __clear_chan_timer(chan
);
447 __set_chan_timer(chan
, sk
->sk_sndtimeo
);
448 l2cap_send_disconn_req(conn
, chan
, reason
);
450 l2cap_chan_del(chan
, reason
);
454 if (chan
->chan_type
== L2CAP_CHAN_CONN_ORIENTED
&&
455 conn
->hcon
->type
== ACL_LINK
) {
456 struct l2cap_conn_rsp rsp
;
459 if (bt_sk(sk
)->defer_setup
)
460 result
= L2CAP_CR_SEC_BLOCK
;
462 result
= L2CAP_CR_BAD_PSM
;
463 l2cap_state_change(chan
, BT_DISCONN
);
465 rsp
.scid
= cpu_to_le16(chan
->dcid
);
466 rsp
.dcid
= cpu_to_le16(chan
->scid
);
467 rsp
.result
= cpu_to_le16(result
);
468 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
469 l2cap_send_cmd(conn
, chan
->ident
, L2CAP_CONN_RSP
,
473 l2cap_chan_del(chan
, reason
);
478 l2cap_chan_del(chan
, reason
);
482 sock_set_flag(sk
, SOCK_ZAPPED
);
487 static inline u8
l2cap_get_auth_type(struct l2cap_chan
*chan
)
489 if (chan
->chan_type
== L2CAP_CHAN_RAW
) {
490 switch (chan
->sec_level
) {
491 case BT_SECURITY_HIGH
:
492 return HCI_AT_DEDICATED_BONDING_MITM
;
493 case BT_SECURITY_MEDIUM
:
494 return HCI_AT_DEDICATED_BONDING
;
496 return HCI_AT_NO_BONDING
;
498 } else if (chan
->psm
== cpu_to_le16(0x0001)) {
499 if (chan
->sec_level
== BT_SECURITY_LOW
)
500 chan
->sec_level
= BT_SECURITY_SDP
;
502 if (chan
->sec_level
== BT_SECURITY_HIGH
)
503 return HCI_AT_NO_BONDING_MITM
;
505 return HCI_AT_NO_BONDING
;
507 switch (chan
->sec_level
) {
508 case BT_SECURITY_HIGH
:
509 return HCI_AT_GENERAL_BONDING_MITM
;
510 case BT_SECURITY_MEDIUM
:
511 return HCI_AT_GENERAL_BONDING
;
513 return HCI_AT_NO_BONDING
;
518 /* Service level security */
519 static inline int l2cap_check_security(struct l2cap_chan
*chan
)
521 struct l2cap_conn
*conn
= chan
->conn
;
524 auth_type
= l2cap_get_auth_type(chan
);
526 return hci_conn_security(conn
->hcon
, chan
->sec_level
, auth_type
);
529 static u8
l2cap_get_ident(struct l2cap_conn
*conn
)
533 /* Get next available identificator.
534 * 1 - 128 are used by kernel.
535 * 129 - 199 are reserved.
536 * 200 - 254 are used by utilities like l2ping, etc.
539 spin_lock_bh(&conn
->lock
);
541 if (++conn
->tx_ident
> 128)
546 spin_unlock_bh(&conn
->lock
);
551 static void l2cap_send_cmd(struct l2cap_conn
*conn
, u8 ident
, u8 code
, u16 len
, void *data
)
553 struct sk_buff
*skb
= l2cap_build_cmd(conn
, code
, ident
, len
, data
);
556 BT_DBG("code 0x%2.2x", code
);
561 if (lmp_no_flush_capable(conn
->hcon
->hdev
))
562 flags
= ACL_START_NO_FLUSH
;
566 bt_cb(skb
)->force_active
= BT_POWER_FORCE_ACTIVE_ON
;
568 hci_send_acl(conn
->hcon
, skb
, flags
);
571 static inline void l2cap_send_sframe(struct l2cap_chan
*chan
, u16 control
)
574 struct l2cap_hdr
*lh
;
575 struct l2cap_conn
*conn
= chan
->conn
;
579 if (chan
->state
!= BT_CONNECTED
)
582 if (test_bit(FLAG_EXT_CTRL
, &chan
->flags
))
583 hlen
= L2CAP_EXT_HDR_SIZE
;
585 hlen
= L2CAP_ENH_HDR_SIZE
;
587 if (chan
->fcs
== L2CAP_FCS_CRC16
)
590 BT_DBG("chan %p, control 0x%2.2x", chan
, control
);
592 count
= min_t(unsigned int, conn
->mtu
, hlen
);
594 control
|= __set_sframe(chan
);
596 if (test_and_clear_bit(CONN_SEND_FBIT
, &chan
->conn_state
))
597 control
|= __set_ctrl_final(chan
);
599 if (test_and_clear_bit(CONN_SEND_PBIT
, &chan
->conn_state
))
600 control
|= __set_ctrl_poll(chan
);
602 skb
= bt_skb_alloc(count
, GFP_ATOMIC
);
606 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
607 lh
->len
= cpu_to_le16(hlen
- L2CAP_HDR_SIZE
);
608 lh
->cid
= cpu_to_le16(chan
->dcid
);
609 put_unaligned_le16(control
, skb_put(skb
, 2));
611 if (chan
->fcs
== L2CAP_FCS_CRC16
) {
612 u16 fcs
= crc16(0, (u8
*)lh
, count
- 2);
613 put_unaligned_le16(fcs
, skb_put(skb
, 2));
616 if (lmp_no_flush_capable(conn
->hcon
->hdev
))
617 flags
= ACL_START_NO_FLUSH
;
621 bt_cb(skb
)->force_active
= test_bit(FLAG_FORCE_ACTIVE
, &chan
->flags
);
623 hci_send_acl(chan
->conn
->hcon
, skb
, flags
);
626 static inline void l2cap_send_rr_or_rnr(struct l2cap_chan
*chan
, u16 control
)
628 if (test_bit(CONN_LOCAL_BUSY
, &chan
->conn_state
)) {
629 control
|= __set_ctrl_super(chan
, L2CAP_SUPER_RNR
);
630 set_bit(CONN_RNR_SENT
, &chan
->conn_state
);
632 control
|= __set_ctrl_super(chan
, L2CAP_SUPER_RR
);
634 control
|= __set_reqseq(chan
, chan
->buffer_seq
);
636 l2cap_send_sframe(chan
, control
);
639 static inline int __l2cap_no_conn_pending(struct l2cap_chan
*chan
)
641 return !test_bit(CONF_CONNECT_PEND
, &chan
->conf_state
);
644 static void l2cap_do_start(struct l2cap_chan
*chan
)
646 struct l2cap_conn
*conn
= chan
->conn
;
648 if (conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_SENT
) {
649 if (!(conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_DONE
))
652 if (l2cap_check_security(chan
) &&
653 __l2cap_no_conn_pending(chan
)) {
654 struct l2cap_conn_req req
;
655 req
.scid
= cpu_to_le16(chan
->scid
);
658 chan
->ident
= l2cap_get_ident(conn
);
659 set_bit(CONF_CONNECT_PEND
, &chan
->conf_state
);
661 l2cap_send_cmd(conn
, chan
->ident
, L2CAP_CONN_REQ
,
665 struct l2cap_info_req req
;
666 req
.type
= cpu_to_le16(L2CAP_IT_FEAT_MASK
);
668 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_SENT
;
669 conn
->info_ident
= l2cap_get_ident(conn
);
671 mod_timer(&conn
->info_timer
, jiffies
+
672 msecs_to_jiffies(L2CAP_INFO_TIMEOUT
));
674 l2cap_send_cmd(conn
, conn
->info_ident
,
675 L2CAP_INFO_REQ
, sizeof(req
), &req
);
679 static inline int l2cap_mode_supported(__u8 mode
, __u32 feat_mask
)
681 u32 local_feat_mask
= l2cap_feat_mask
;
683 local_feat_mask
|= L2CAP_FEAT_ERTM
| L2CAP_FEAT_STREAMING
;
686 case L2CAP_MODE_ERTM
:
687 return L2CAP_FEAT_ERTM
& feat_mask
& local_feat_mask
;
688 case L2CAP_MODE_STREAMING
:
689 return L2CAP_FEAT_STREAMING
& feat_mask
& local_feat_mask
;
695 static void l2cap_send_disconn_req(struct l2cap_conn
*conn
, struct l2cap_chan
*chan
, int err
)
698 struct l2cap_disconn_req req
;
705 if (chan
->mode
== L2CAP_MODE_ERTM
) {
706 __clear_retrans_timer(chan
);
707 __clear_monitor_timer(chan
);
708 __clear_ack_timer(chan
);
711 req
.dcid
= cpu_to_le16(chan
->dcid
);
712 req
.scid
= cpu_to_le16(chan
->scid
);
713 l2cap_send_cmd(conn
, l2cap_get_ident(conn
),
714 L2CAP_DISCONN_REQ
, sizeof(req
), &req
);
716 l2cap_state_change(chan
, BT_DISCONN
);
720 /* ---- L2CAP connections ---- */
721 static void l2cap_conn_start(struct l2cap_conn
*conn
)
723 struct l2cap_chan
*chan
, *tmp
;
725 BT_DBG("conn %p", conn
);
727 read_lock(&conn
->chan_lock
);
729 list_for_each_entry_safe(chan
, tmp
, &conn
->chan_l
, list
) {
730 struct sock
*sk
= chan
->sk
;
734 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
) {
739 if (chan
->state
== BT_CONNECT
) {
740 struct l2cap_conn_req req
;
742 if (!l2cap_check_security(chan
) ||
743 !__l2cap_no_conn_pending(chan
)) {
748 if (!l2cap_mode_supported(chan
->mode
, conn
->feat_mask
)
749 && test_bit(CONF_STATE2_DEVICE
,
750 &chan
->conf_state
)) {
751 /* l2cap_chan_close() calls list_del(chan)
752 * so release the lock */
753 read_unlock(&conn
->chan_lock
);
754 l2cap_chan_close(chan
, ECONNRESET
);
755 read_lock(&conn
->chan_lock
);
760 req
.scid
= cpu_to_le16(chan
->scid
);
763 chan
->ident
= l2cap_get_ident(conn
);
764 set_bit(CONF_CONNECT_PEND
, &chan
->conf_state
);
766 l2cap_send_cmd(conn
, chan
->ident
, L2CAP_CONN_REQ
,
769 } else if (chan
->state
== BT_CONNECT2
) {
770 struct l2cap_conn_rsp rsp
;
772 rsp
.scid
= cpu_to_le16(chan
->dcid
);
773 rsp
.dcid
= cpu_to_le16(chan
->scid
);
775 if (l2cap_check_security(chan
)) {
776 if (bt_sk(sk
)->defer_setup
) {
777 struct sock
*parent
= bt_sk(sk
)->parent
;
778 rsp
.result
= cpu_to_le16(L2CAP_CR_PEND
);
779 rsp
.status
= cpu_to_le16(L2CAP_CS_AUTHOR_PEND
);
781 parent
->sk_data_ready(parent
, 0);
784 l2cap_state_change(chan
, BT_CONFIG
);
785 rsp
.result
= cpu_to_le16(L2CAP_CR_SUCCESS
);
786 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
789 rsp
.result
= cpu_to_le16(L2CAP_CR_PEND
);
790 rsp
.status
= cpu_to_le16(L2CAP_CS_AUTHEN_PEND
);
793 l2cap_send_cmd(conn
, chan
->ident
, L2CAP_CONN_RSP
,
796 if (test_bit(CONF_REQ_SENT
, &chan
->conf_state
) ||
797 rsp
.result
!= L2CAP_CR_SUCCESS
) {
802 set_bit(CONF_REQ_SENT
, &chan
->conf_state
);
803 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
804 l2cap_build_conf_req(chan
, buf
), buf
);
805 chan
->num_conf_req
++;
811 read_unlock(&conn
->chan_lock
);
814 /* Find socket with cid and source bdaddr.
815 * Returns closest match, locked.
817 static struct l2cap_chan
*l2cap_global_chan_by_scid(int state
, __le16 cid
, bdaddr_t
*src
)
819 struct l2cap_chan
*c
, *c1
= NULL
;
821 read_lock(&chan_list_lock
);
823 list_for_each_entry(c
, &chan_list
, global_l
) {
824 struct sock
*sk
= c
->sk
;
826 if (state
&& c
->state
!= state
)
829 if (c
->scid
== cid
) {
831 if (!bacmp(&bt_sk(sk
)->src
, src
)) {
832 read_unlock(&chan_list_lock
);
837 if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
))
842 read_unlock(&chan_list_lock
);
847 static void l2cap_le_conn_ready(struct l2cap_conn
*conn
)
849 struct sock
*parent
, *sk
;
850 struct l2cap_chan
*chan
, *pchan
;
854 /* Check if we have socket listening on cid */
855 pchan
= l2cap_global_chan_by_scid(BT_LISTEN
, L2CAP_CID_LE_DATA
,
862 bh_lock_sock(parent
);
864 /* Check for backlog size */
865 if (sk_acceptq_is_full(parent
)) {
866 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
870 chan
= pchan
->ops
->new_connection(pchan
->data
);
876 write_lock_bh(&conn
->chan_lock
);
878 hci_conn_hold(conn
->hcon
);
880 bacpy(&bt_sk(sk
)->src
, conn
->src
);
881 bacpy(&bt_sk(sk
)->dst
, conn
->dst
);
883 bt_accept_enqueue(parent
, sk
);
885 __l2cap_chan_add(conn
, chan
);
887 __set_chan_timer(chan
, sk
->sk_sndtimeo
);
889 l2cap_state_change(chan
, BT_CONNECTED
);
890 parent
->sk_data_ready(parent
, 0);
892 write_unlock_bh(&conn
->chan_lock
);
895 bh_unlock_sock(parent
);
898 static void l2cap_chan_ready(struct sock
*sk
)
900 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
901 struct sock
*parent
= bt_sk(sk
)->parent
;
903 BT_DBG("sk %p, parent %p", sk
, parent
);
905 chan
->conf_state
= 0;
906 __clear_chan_timer(chan
);
908 l2cap_state_change(chan
, BT_CONNECTED
);
909 sk
->sk_state_change(sk
);
912 parent
->sk_data_ready(parent
, 0);
915 static void l2cap_conn_ready(struct l2cap_conn
*conn
)
917 struct l2cap_chan
*chan
;
919 BT_DBG("conn %p", conn
);
921 if (!conn
->hcon
->out
&& conn
->hcon
->type
== LE_LINK
)
922 l2cap_le_conn_ready(conn
);
924 if (conn
->hcon
->out
&& conn
->hcon
->type
== LE_LINK
)
925 smp_conn_security(conn
, conn
->hcon
->pending_sec_level
);
927 read_lock(&conn
->chan_lock
);
929 list_for_each_entry(chan
, &conn
->chan_l
, list
) {
930 struct sock
*sk
= chan
->sk
;
934 if (conn
->hcon
->type
== LE_LINK
) {
935 if (smp_conn_security(conn
, chan
->sec_level
))
936 l2cap_chan_ready(sk
);
938 } else if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
) {
939 __clear_chan_timer(chan
);
940 l2cap_state_change(chan
, BT_CONNECTED
);
941 sk
->sk_state_change(sk
);
943 } else if (chan
->state
== BT_CONNECT
)
944 l2cap_do_start(chan
);
949 read_unlock(&conn
->chan_lock
);
952 /* Notify sockets that we cannot guaranty reliability anymore */
953 static void l2cap_conn_unreliable(struct l2cap_conn
*conn
, int err
)
955 struct l2cap_chan
*chan
;
957 BT_DBG("conn %p", conn
);
959 read_lock(&conn
->chan_lock
);
961 list_for_each_entry(chan
, &conn
->chan_l
, list
) {
962 struct sock
*sk
= chan
->sk
;
964 if (test_bit(FLAG_FORCE_RELIABLE
, &chan
->flags
))
968 read_unlock(&conn
->chan_lock
);
971 static void l2cap_info_timeout(unsigned long arg
)
973 struct l2cap_conn
*conn
= (void *) arg
;
975 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
976 conn
->info_ident
= 0;
978 l2cap_conn_start(conn
);
981 static void l2cap_conn_del(struct hci_conn
*hcon
, int err
)
983 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
984 struct l2cap_chan
*chan
, *l
;
990 BT_DBG("hcon %p conn %p, err %d", hcon
, conn
, err
);
992 kfree_skb(conn
->rx_skb
);
995 list_for_each_entry_safe(chan
, l
, &conn
->chan_l
, list
) {
998 l2cap_chan_del(chan
, err
);
1000 chan
->ops
->close(chan
->data
);
1003 if (conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_SENT
)
1004 del_timer_sync(&conn
->info_timer
);
1006 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND
, &hcon
->pend
)) {
1007 del_timer(&conn
->security_timer
);
1008 smp_chan_destroy(conn
);
1011 hcon
->l2cap_data
= NULL
;
1015 static void security_timeout(unsigned long arg
)
1017 struct l2cap_conn
*conn
= (void *) arg
;
1019 l2cap_conn_del(conn
->hcon
, ETIMEDOUT
);
1022 static struct l2cap_conn
*l2cap_conn_add(struct hci_conn
*hcon
, u8 status
)
1024 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
1029 conn
= kzalloc(sizeof(struct l2cap_conn
), GFP_ATOMIC
);
1033 hcon
->l2cap_data
= conn
;
1036 BT_DBG("hcon %p conn %p", hcon
, conn
);
1038 if (hcon
->hdev
->le_mtu
&& hcon
->type
== LE_LINK
)
1039 conn
->mtu
= hcon
->hdev
->le_mtu
;
1041 conn
->mtu
= hcon
->hdev
->acl_mtu
;
1043 conn
->src
= &hcon
->hdev
->bdaddr
;
1044 conn
->dst
= &hcon
->dst
;
1046 conn
->feat_mask
= 0;
1048 spin_lock_init(&conn
->lock
);
1049 rwlock_init(&conn
->chan_lock
);
1051 INIT_LIST_HEAD(&conn
->chan_l
);
1053 if (hcon
->type
== LE_LINK
)
1054 setup_timer(&conn
->security_timer
, security_timeout
,
1055 (unsigned long) conn
);
1057 setup_timer(&conn
->info_timer
, l2cap_info_timeout
,
1058 (unsigned long) conn
);
1060 conn
->disc_reason
= 0x13;
1065 static inline void l2cap_chan_add(struct l2cap_conn
*conn
, struct l2cap_chan
*chan
)
1067 write_lock_bh(&conn
->chan_lock
);
1068 __l2cap_chan_add(conn
, chan
);
1069 write_unlock_bh(&conn
->chan_lock
);
1072 /* ---- Socket interface ---- */
1074 /* Find socket with psm and source bdaddr.
1075 * Returns closest match.
1077 static struct l2cap_chan
*l2cap_global_chan_by_psm(int state
, __le16 psm
, bdaddr_t
*src
)
1079 struct l2cap_chan
*c
, *c1
= NULL
;
1081 read_lock(&chan_list_lock
);
1083 list_for_each_entry(c
, &chan_list
, global_l
) {
1084 struct sock
*sk
= c
->sk
;
1086 if (state
&& c
->state
!= state
)
1089 if (c
->psm
== psm
) {
1091 if (!bacmp(&bt_sk(sk
)->src
, src
)) {
1092 read_unlock(&chan_list_lock
);
1097 if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
))
1102 read_unlock(&chan_list_lock
);
1107 int l2cap_chan_connect(struct l2cap_chan
*chan
)
1109 struct sock
*sk
= chan
->sk
;
1110 bdaddr_t
*src
= &bt_sk(sk
)->src
;
1111 bdaddr_t
*dst
= &bt_sk(sk
)->dst
;
1112 struct l2cap_conn
*conn
;
1113 struct hci_conn
*hcon
;
1114 struct hci_dev
*hdev
;
1118 BT_DBG("%s -> %s psm 0x%2.2x", batostr(src
), batostr(dst
),
1121 hdev
= hci_get_route(dst
, src
);
1123 return -EHOSTUNREACH
;
1125 hci_dev_lock_bh(hdev
);
1127 auth_type
= l2cap_get_auth_type(chan
);
1129 if (chan
->dcid
== L2CAP_CID_LE_DATA
)
1130 hcon
= hci_connect(hdev
, LE_LINK
, dst
,
1131 chan
->sec_level
, auth_type
);
1133 hcon
= hci_connect(hdev
, ACL_LINK
, dst
,
1134 chan
->sec_level
, auth_type
);
1137 err
= PTR_ERR(hcon
);
1141 conn
= l2cap_conn_add(hcon
, 0);
1148 /* Update source addr of the socket */
1149 bacpy(src
, conn
->src
);
1151 l2cap_chan_add(conn
, chan
);
1153 l2cap_state_change(chan
, BT_CONNECT
);
1154 __set_chan_timer(chan
, sk
->sk_sndtimeo
);
1156 if (hcon
->state
== BT_CONNECTED
) {
1157 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
) {
1158 __clear_chan_timer(chan
);
1159 if (l2cap_check_security(chan
))
1160 l2cap_state_change(chan
, BT_CONNECTED
);
1162 l2cap_do_start(chan
);
1168 hci_dev_unlock_bh(hdev
);
1173 int __l2cap_wait_ack(struct sock
*sk
)
1175 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
1176 DECLARE_WAITQUEUE(wait
, current
);
1180 add_wait_queue(sk_sleep(sk
), &wait
);
1181 set_current_state(TASK_INTERRUPTIBLE
);
1182 while (chan
->unacked_frames
> 0 && chan
->conn
) {
1186 if (signal_pending(current
)) {
1187 err
= sock_intr_errno(timeo
);
1192 timeo
= schedule_timeout(timeo
);
1194 set_current_state(TASK_INTERRUPTIBLE
);
1196 err
= sock_error(sk
);
1200 set_current_state(TASK_RUNNING
);
1201 remove_wait_queue(sk_sleep(sk
), &wait
);
1205 static void l2cap_monitor_timeout(unsigned long arg
)
1207 struct l2cap_chan
*chan
= (void *) arg
;
1208 struct sock
*sk
= chan
->sk
;
1210 BT_DBG("chan %p", chan
);
1213 if (chan
->retry_count
>= chan
->remote_max_tx
) {
1214 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNABORTED
);
1219 chan
->retry_count
++;
1220 __set_monitor_timer(chan
);
1222 l2cap_send_rr_or_rnr(chan
, L2CAP_CTRL_POLL
);
1226 static void l2cap_retrans_timeout(unsigned long arg
)
1228 struct l2cap_chan
*chan
= (void *) arg
;
1229 struct sock
*sk
= chan
->sk
;
1231 BT_DBG("chan %p", chan
);
1234 chan
->retry_count
= 1;
1235 __set_monitor_timer(chan
);
1237 set_bit(CONN_WAIT_F
, &chan
->conn_state
);
1239 l2cap_send_rr_or_rnr(chan
, L2CAP_CTRL_POLL
);
1243 static void l2cap_drop_acked_frames(struct l2cap_chan
*chan
)
1245 struct sk_buff
*skb
;
1247 while ((skb
= skb_peek(&chan
->tx_q
)) &&
1248 chan
->unacked_frames
) {
1249 if (bt_cb(skb
)->tx_seq
== chan
->expected_ack_seq
)
1252 skb
= skb_dequeue(&chan
->tx_q
);
1255 chan
->unacked_frames
--;
1258 if (!chan
->unacked_frames
)
1259 __clear_retrans_timer(chan
);
1262 static void l2cap_do_send(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
1264 struct hci_conn
*hcon
= chan
->conn
->hcon
;
1267 BT_DBG("chan %p, skb %p len %d", chan
, skb
, skb
->len
);
1269 if (!test_bit(FLAG_FLUSHABLE
, &chan
->flags
) &&
1270 lmp_no_flush_capable(hcon
->hdev
))
1271 flags
= ACL_START_NO_FLUSH
;
1275 bt_cb(skb
)->force_active
= test_bit(FLAG_FORCE_ACTIVE
, &chan
->flags
);
1276 hci_send_acl(hcon
, skb
, flags
);
1279 static void l2cap_streaming_send(struct l2cap_chan
*chan
)
1281 struct sk_buff
*skb
;
1284 while ((skb
= skb_dequeue(&chan
->tx_q
))) {
1285 control
= get_unaligned_le16(skb
->data
+ L2CAP_HDR_SIZE
);
1286 control
|= __set_txseq(chan
, chan
->next_tx_seq
);
1287 put_unaligned_le16(control
, skb
->data
+ L2CAP_HDR_SIZE
);
1289 if (chan
->fcs
== L2CAP_FCS_CRC16
) {
1290 fcs
= crc16(0, (u8
*)skb
->data
, skb
->len
- 2);
1291 put_unaligned_le16(fcs
, skb
->data
+ skb
->len
- 2);
1294 l2cap_do_send(chan
, skb
);
1296 chan
->next_tx_seq
= (chan
->next_tx_seq
+ 1) % 64;
1300 static void l2cap_retransmit_one_frame(struct l2cap_chan
*chan
, u16 tx_seq
)
1302 struct sk_buff
*skb
, *tx_skb
;
1305 skb
= skb_peek(&chan
->tx_q
);
1310 if (bt_cb(skb
)->tx_seq
== tx_seq
)
1313 if (skb_queue_is_last(&chan
->tx_q
, skb
))
1316 } while ((skb
= skb_queue_next(&chan
->tx_q
, skb
)));
1318 if (chan
->remote_max_tx
&&
1319 bt_cb(skb
)->retries
== chan
->remote_max_tx
) {
1320 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNABORTED
);
1324 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
1325 bt_cb(skb
)->retries
++;
1326 control
= get_unaligned_le16(tx_skb
->data
+ L2CAP_HDR_SIZE
);
1327 control
&= __get_sar_mask(chan
);
1329 if (test_and_clear_bit(CONN_SEND_FBIT
, &chan
->conn_state
))
1330 control
|= __set_ctrl_final(chan
);
1332 control
|= __set_reqseq(chan
, chan
->buffer_seq
);
1333 control
|= __set_txseq(chan
, tx_seq
);
1335 put_unaligned_le16(control
, tx_skb
->data
+ L2CAP_HDR_SIZE
);
1337 if (chan
->fcs
== L2CAP_FCS_CRC16
) {
1338 fcs
= crc16(0, (u8
*)tx_skb
->data
, tx_skb
->len
- 2);
1339 put_unaligned_le16(fcs
, tx_skb
->data
+ tx_skb
->len
- 2);
1342 l2cap_do_send(chan
, tx_skb
);
1345 static int l2cap_ertm_send(struct l2cap_chan
*chan
)
1347 struct sk_buff
*skb
, *tx_skb
;
1351 if (chan
->state
!= BT_CONNECTED
)
1354 while ((skb
= chan
->tx_send_head
) && (!l2cap_tx_window_full(chan
))) {
1356 if (chan
->remote_max_tx
&&
1357 bt_cb(skb
)->retries
== chan
->remote_max_tx
) {
1358 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNABORTED
);
1362 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
1364 bt_cb(skb
)->retries
++;
1366 control
= get_unaligned_le16(tx_skb
->data
+ L2CAP_HDR_SIZE
);
1367 control
&= __get_sar_mask(chan
);
1369 if (test_and_clear_bit(CONN_SEND_FBIT
, &chan
->conn_state
))
1370 control
|= __set_ctrl_final(chan
);
1372 control
|= __set_reqseq(chan
, chan
->buffer_seq
);
1373 control
|= __set_txseq(chan
, chan
->next_tx_seq
);
1374 put_unaligned_le16(control
, tx_skb
->data
+ L2CAP_HDR_SIZE
);
1377 if (chan
->fcs
== L2CAP_FCS_CRC16
) {
1378 fcs
= crc16(0, (u8
*)skb
->data
, tx_skb
->len
- 2);
1379 put_unaligned_le16(fcs
, skb
->data
+ tx_skb
->len
- 2);
1382 l2cap_do_send(chan
, tx_skb
);
1384 __set_retrans_timer(chan
);
1386 bt_cb(skb
)->tx_seq
= chan
->next_tx_seq
;
1387 chan
->next_tx_seq
= (chan
->next_tx_seq
+ 1) % 64;
1389 if (bt_cb(skb
)->retries
== 1)
1390 chan
->unacked_frames
++;
1392 chan
->frames_sent
++;
1394 if (skb_queue_is_last(&chan
->tx_q
, skb
))
1395 chan
->tx_send_head
= NULL
;
1397 chan
->tx_send_head
= skb_queue_next(&chan
->tx_q
, skb
);
1405 static int l2cap_retransmit_frames(struct l2cap_chan
*chan
)
1409 if (!skb_queue_empty(&chan
->tx_q
))
1410 chan
->tx_send_head
= chan
->tx_q
.next
;
1412 chan
->next_tx_seq
= chan
->expected_ack_seq
;
1413 ret
= l2cap_ertm_send(chan
);
1417 static void l2cap_send_ack(struct l2cap_chan
*chan
)
1421 control
|= __set_reqseq(chan
, chan
->buffer_seq
);
1423 if (test_bit(CONN_LOCAL_BUSY
, &chan
->conn_state
)) {
1424 control
|= __set_ctrl_super(chan
, L2CAP_SUPER_RNR
);
1425 set_bit(CONN_RNR_SENT
, &chan
->conn_state
);
1426 l2cap_send_sframe(chan
, control
);
1430 if (l2cap_ertm_send(chan
) > 0)
1433 control
|= __set_ctrl_super(chan
, L2CAP_SUPER_RR
);
1434 l2cap_send_sframe(chan
, control
);
1437 static void l2cap_send_srejtail(struct l2cap_chan
*chan
)
1439 struct srej_list
*tail
;
1442 control
= __set_ctrl_super(chan
, L2CAP_SUPER_SREJ
);
1443 control
|= __set_ctrl_final(chan
);
1445 tail
= list_entry((&chan
->srej_l
)->prev
, struct srej_list
, list
);
1446 control
|= __set_reqseq(chan
, tail
->tx_seq
);
1448 l2cap_send_sframe(chan
, control
);
1451 static inline int l2cap_skbuff_fromiovec(struct sock
*sk
, struct msghdr
*msg
, int len
, int count
, struct sk_buff
*skb
)
1453 struct l2cap_conn
*conn
= l2cap_pi(sk
)->chan
->conn
;
1454 struct sk_buff
**frag
;
1457 if (memcpy_fromiovec(skb_put(skb
, count
), msg
->msg_iov
, count
))
1463 /* Continuation fragments (no L2CAP header) */
1464 frag
= &skb_shinfo(skb
)->frag_list
;
1466 count
= min_t(unsigned int, conn
->mtu
, len
);
1468 *frag
= bt_skb_send_alloc(sk
, count
, msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1471 if (memcpy_fromiovec(skb_put(*frag
, count
), msg
->msg_iov
, count
))
1477 frag
= &(*frag
)->next
;
1483 static struct sk_buff
*l2cap_create_connless_pdu(struct l2cap_chan
*chan
, struct msghdr
*msg
, size_t len
)
1485 struct sock
*sk
= chan
->sk
;
1486 struct l2cap_conn
*conn
= chan
->conn
;
1487 struct sk_buff
*skb
;
1488 int err
, count
, hlen
= L2CAP_HDR_SIZE
+ 2;
1489 struct l2cap_hdr
*lh
;
1491 BT_DBG("sk %p len %d", sk
, (int)len
);
1493 count
= min_t(unsigned int, (conn
->mtu
- hlen
), len
);
1494 skb
= bt_skb_send_alloc(sk
, count
+ hlen
,
1495 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1497 return ERR_PTR(err
);
1499 /* Create L2CAP header */
1500 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
1501 lh
->cid
= cpu_to_le16(chan
->dcid
);
1502 lh
->len
= cpu_to_le16(len
+ (hlen
- L2CAP_HDR_SIZE
));
1503 put_unaligned_le16(chan
->psm
, skb_put(skb
, 2));
1505 err
= l2cap_skbuff_fromiovec(sk
, msg
, len
, count
, skb
);
1506 if (unlikely(err
< 0)) {
1508 return ERR_PTR(err
);
1513 static struct sk_buff
*l2cap_create_basic_pdu(struct l2cap_chan
*chan
, struct msghdr
*msg
, size_t len
)
1515 struct sock
*sk
= chan
->sk
;
1516 struct l2cap_conn
*conn
= chan
->conn
;
1517 struct sk_buff
*skb
;
1518 int err
, count
, hlen
= L2CAP_HDR_SIZE
;
1519 struct l2cap_hdr
*lh
;
1521 BT_DBG("sk %p len %d", sk
, (int)len
);
1523 count
= min_t(unsigned int, (conn
->mtu
- hlen
), len
);
1524 skb
= bt_skb_send_alloc(sk
, count
+ hlen
,
1525 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1527 return ERR_PTR(err
);
1529 /* Create L2CAP header */
1530 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
1531 lh
->cid
= cpu_to_le16(chan
->dcid
);
1532 lh
->len
= cpu_to_le16(len
+ (hlen
- L2CAP_HDR_SIZE
));
1534 err
= l2cap_skbuff_fromiovec(sk
, msg
, len
, count
, skb
);
1535 if (unlikely(err
< 0)) {
1537 return ERR_PTR(err
);
1542 static struct sk_buff
*l2cap_create_iframe_pdu(struct l2cap_chan
*chan
,
1543 struct msghdr
*msg
, size_t len
,
1544 u16 control
, u16 sdulen
)
1546 struct sock
*sk
= chan
->sk
;
1547 struct l2cap_conn
*conn
= chan
->conn
;
1548 struct sk_buff
*skb
;
1549 int err
, count
, hlen
;
1550 struct l2cap_hdr
*lh
;
1552 BT_DBG("sk %p len %d", sk
, (int)len
);
1555 return ERR_PTR(-ENOTCONN
);
1557 if (test_bit(FLAG_EXT_CTRL
, &chan
->flags
))
1558 hlen
= L2CAP_EXT_HDR_SIZE
;
1560 hlen
= L2CAP_ENH_HDR_SIZE
;
1565 if (chan
->fcs
== L2CAP_FCS_CRC16
)
1568 count
= min_t(unsigned int, (conn
->mtu
- hlen
), len
);
1569 skb
= bt_skb_send_alloc(sk
, count
+ hlen
,
1570 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1572 return ERR_PTR(err
);
1574 /* Create L2CAP header */
1575 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
1576 lh
->cid
= cpu_to_le16(chan
->dcid
);
1577 lh
->len
= cpu_to_le16(len
+ (hlen
- L2CAP_HDR_SIZE
));
1578 put_unaligned_le16(control
, skb_put(skb
, 2));
1580 put_unaligned_le16(sdulen
, skb_put(skb
, 2));
1582 err
= l2cap_skbuff_fromiovec(sk
, msg
, len
, count
, skb
);
1583 if (unlikely(err
< 0)) {
1585 return ERR_PTR(err
);
1588 if (chan
->fcs
== L2CAP_FCS_CRC16
)
1589 put_unaligned_le16(0, skb_put(skb
, 2));
1591 bt_cb(skb
)->retries
= 0;
1595 static int l2cap_sar_segment_sdu(struct l2cap_chan
*chan
, struct msghdr
*msg
, size_t len
)
1597 struct sk_buff
*skb
;
1598 struct sk_buff_head sar_queue
;
1602 skb_queue_head_init(&sar_queue
);
1603 control
= __set_ctrl_sar(chan
, L2CAP_SAR_START
);
1604 skb
= l2cap_create_iframe_pdu(chan
, msg
, chan
->remote_mps
, control
, len
);
1606 return PTR_ERR(skb
);
1608 __skb_queue_tail(&sar_queue
, skb
);
1609 len
-= chan
->remote_mps
;
1610 size
+= chan
->remote_mps
;
1615 if (len
> chan
->remote_mps
) {
1616 control
= __set_ctrl_sar(chan
, L2CAP_SAR_CONTINUE
);
1617 buflen
= chan
->remote_mps
;
1619 control
= __set_ctrl_sar(chan
, L2CAP_SAR_END
);
1623 skb
= l2cap_create_iframe_pdu(chan
, msg
, buflen
, control
, 0);
1625 skb_queue_purge(&sar_queue
);
1626 return PTR_ERR(skb
);
1629 __skb_queue_tail(&sar_queue
, skb
);
1633 skb_queue_splice_tail(&sar_queue
, &chan
->tx_q
);
1634 if (chan
->tx_send_head
== NULL
)
1635 chan
->tx_send_head
= sar_queue
.next
;
1640 int l2cap_chan_send(struct l2cap_chan
*chan
, struct msghdr
*msg
, size_t len
)
1642 struct sk_buff
*skb
;
1646 /* Connectionless channel */
1647 if (chan
->chan_type
== L2CAP_CHAN_CONN_LESS
) {
1648 skb
= l2cap_create_connless_pdu(chan
, msg
, len
);
1650 return PTR_ERR(skb
);
1652 l2cap_do_send(chan
, skb
);
1656 switch (chan
->mode
) {
1657 case L2CAP_MODE_BASIC
:
1658 /* Check outgoing MTU */
1659 if (len
> chan
->omtu
)
1662 /* Create a basic PDU */
1663 skb
= l2cap_create_basic_pdu(chan
, msg
, len
);
1665 return PTR_ERR(skb
);
1667 l2cap_do_send(chan
, skb
);
1671 case L2CAP_MODE_ERTM
:
1672 case L2CAP_MODE_STREAMING
:
1673 /* Entire SDU fits into one PDU */
1674 if (len
<= chan
->remote_mps
) {
1675 control
= __set_ctrl_sar(chan
, L2CAP_SAR_UNSEGMENTED
);
1676 skb
= l2cap_create_iframe_pdu(chan
, msg
, len
, control
,
1679 return PTR_ERR(skb
);
1681 __skb_queue_tail(&chan
->tx_q
, skb
);
1683 if (chan
->tx_send_head
== NULL
)
1684 chan
->tx_send_head
= skb
;
1687 /* Segment SDU into multiples PDUs */
1688 err
= l2cap_sar_segment_sdu(chan
, msg
, len
);
1693 if (chan
->mode
== L2CAP_MODE_STREAMING
) {
1694 l2cap_streaming_send(chan
);
1699 if (test_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
) &&
1700 test_bit(CONN_WAIT_F
, &chan
->conn_state
)) {
1705 err
= l2cap_ertm_send(chan
);
1712 BT_DBG("bad state %1.1x", chan
->mode
);
1719 /* Copy frame to all raw sockets on that connection */
1720 static void l2cap_raw_recv(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
1722 struct sk_buff
*nskb
;
1723 struct l2cap_chan
*chan
;
1725 BT_DBG("conn %p", conn
);
1727 read_lock(&conn
->chan_lock
);
1728 list_for_each_entry(chan
, &conn
->chan_l
, list
) {
1729 struct sock
*sk
= chan
->sk
;
1730 if (chan
->chan_type
!= L2CAP_CHAN_RAW
)
1733 /* Don't send frame to the socket it came from */
1736 nskb
= skb_clone(skb
, GFP_ATOMIC
);
1740 if (chan
->ops
->recv(chan
->data
, nskb
))
1743 read_unlock(&conn
->chan_lock
);
1746 /* ---- L2CAP signalling commands ---- */
1747 static struct sk_buff
*l2cap_build_cmd(struct l2cap_conn
*conn
,
1748 u8 code
, u8 ident
, u16 dlen
, void *data
)
1750 struct sk_buff
*skb
, **frag
;
1751 struct l2cap_cmd_hdr
*cmd
;
1752 struct l2cap_hdr
*lh
;
1755 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
1756 conn
, code
, ident
, dlen
);
1758 len
= L2CAP_HDR_SIZE
+ L2CAP_CMD_HDR_SIZE
+ dlen
;
1759 count
= min_t(unsigned int, conn
->mtu
, len
);
1761 skb
= bt_skb_alloc(count
, GFP_ATOMIC
);
1765 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
1766 lh
->len
= cpu_to_le16(L2CAP_CMD_HDR_SIZE
+ dlen
);
1768 if (conn
->hcon
->type
== LE_LINK
)
1769 lh
->cid
= cpu_to_le16(L2CAP_CID_LE_SIGNALING
);
1771 lh
->cid
= cpu_to_le16(L2CAP_CID_SIGNALING
);
1773 cmd
= (struct l2cap_cmd_hdr
*) skb_put(skb
, L2CAP_CMD_HDR_SIZE
);
1776 cmd
->len
= cpu_to_le16(dlen
);
1779 count
-= L2CAP_HDR_SIZE
+ L2CAP_CMD_HDR_SIZE
;
1780 memcpy(skb_put(skb
, count
), data
, count
);
1786 /* Continuation fragments (no L2CAP header) */
1787 frag
= &skb_shinfo(skb
)->frag_list
;
1789 count
= min_t(unsigned int, conn
->mtu
, len
);
1791 *frag
= bt_skb_alloc(count
, GFP_ATOMIC
);
1795 memcpy(skb_put(*frag
, count
), data
, count
);
1800 frag
= &(*frag
)->next
;
1810 static inline int l2cap_get_conf_opt(void **ptr
, int *type
, int *olen
, unsigned long *val
)
1812 struct l2cap_conf_opt
*opt
= *ptr
;
1815 len
= L2CAP_CONF_OPT_SIZE
+ opt
->len
;
1823 *val
= *((u8
*) opt
->val
);
1827 *val
= get_unaligned_le16(opt
->val
);
1831 *val
= get_unaligned_le32(opt
->val
);
1835 *val
= (unsigned long) opt
->val
;
1839 BT_DBG("type 0x%2.2x len %d val 0x%lx", *type
, opt
->len
, *val
);
1843 static void l2cap_add_conf_opt(void **ptr
, u8 type
, u8 len
, unsigned long val
)
1845 struct l2cap_conf_opt
*opt
= *ptr
;
1847 BT_DBG("type 0x%2.2x len %d val 0x%lx", type
, len
, val
);
1854 *((u8
*) opt
->val
) = val
;
1858 put_unaligned_le16(val
, opt
->val
);
1862 put_unaligned_le32(val
, opt
->val
);
1866 memcpy(opt
->val
, (void *) val
, len
);
1870 *ptr
+= L2CAP_CONF_OPT_SIZE
+ len
;
1873 static void l2cap_ack_timeout(unsigned long arg
)
1875 struct l2cap_chan
*chan
= (void *) arg
;
1877 bh_lock_sock(chan
->sk
);
1878 l2cap_send_ack(chan
);
1879 bh_unlock_sock(chan
->sk
);
1882 static inline void l2cap_ertm_init(struct l2cap_chan
*chan
)
1884 struct sock
*sk
= chan
->sk
;
1886 chan
->expected_ack_seq
= 0;
1887 chan
->unacked_frames
= 0;
1888 chan
->buffer_seq
= 0;
1889 chan
->num_acked
= 0;
1890 chan
->frames_sent
= 0;
1892 setup_timer(&chan
->retrans_timer
, l2cap_retrans_timeout
,
1893 (unsigned long) chan
);
1894 setup_timer(&chan
->monitor_timer
, l2cap_monitor_timeout
,
1895 (unsigned long) chan
);
1896 setup_timer(&chan
->ack_timer
, l2cap_ack_timeout
, (unsigned long) chan
);
1898 skb_queue_head_init(&chan
->srej_q
);
1900 INIT_LIST_HEAD(&chan
->srej_l
);
1903 sk
->sk_backlog_rcv
= l2cap_ertm_data_rcv
;
1906 static inline __u8
l2cap_select_mode(__u8 mode
, __u16 remote_feat_mask
)
1909 case L2CAP_MODE_STREAMING
:
1910 case L2CAP_MODE_ERTM
:
1911 if (l2cap_mode_supported(mode
, remote_feat_mask
))
1915 return L2CAP_MODE_BASIC
;
1919 static inline bool __l2cap_ews_supported(struct l2cap_chan
*chan
)
1921 return enable_hs
&& chan
->conn
->feat_mask
& L2CAP_FEAT_EXT_WINDOW
;
1924 static inline void l2cap_txwin_setup(struct l2cap_chan
*chan
)
1926 if (chan
->tx_win
> L2CAP_DEFAULT_TX_WINDOW
&&
1927 __l2cap_ews_supported(chan
))
1928 /* use extended control field */
1929 set_bit(FLAG_EXT_CTRL
, &chan
->flags
);
1931 chan
->tx_win
= min_t(u16
, chan
->tx_win
,
1932 L2CAP_DEFAULT_TX_WINDOW
);
1935 static int l2cap_build_conf_req(struct l2cap_chan
*chan
, void *data
)
1937 struct l2cap_conf_req
*req
= data
;
1938 struct l2cap_conf_rfc rfc
= { .mode
= chan
->mode
};
1939 void *ptr
= req
->data
;
1941 BT_DBG("chan %p", chan
);
1943 if (chan
->num_conf_req
|| chan
->num_conf_rsp
)
1946 switch (chan
->mode
) {
1947 case L2CAP_MODE_STREAMING
:
1948 case L2CAP_MODE_ERTM
:
1949 if (test_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
))
1954 chan
->mode
= l2cap_select_mode(rfc
.mode
, chan
->conn
->feat_mask
);
1959 if (chan
->imtu
!= L2CAP_DEFAULT_MTU
)
1960 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_MTU
, 2, chan
->imtu
);
1962 switch (chan
->mode
) {
1963 case L2CAP_MODE_BASIC
:
1964 if (!(chan
->conn
->feat_mask
& L2CAP_FEAT_ERTM
) &&
1965 !(chan
->conn
->feat_mask
& L2CAP_FEAT_STREAMING
))
1968 rfc
.mode
= L2CAP_MODE_BASIC
;
1970 rfc
.max_transmit
= 0;
1971 rfc
.retrans_timeout
= 0;
1972 rfc
.monitor_timeout
= 0;
1973 rfc
.max_pdu_size
= 0;
1975 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
, sizeof(rfc
),
1976 (unsigned long) &rfc
);
1979 case L2CAP_MODE_ERTM
:
1980 rfc
.mode
= L2CAP_MODE_ERTM
;
1981 rfc
.max_transmit
= chan
->max_tx
;
1982 rfc
.retrans_timeout
= 0;
1983 rfc
.monitor_timeout
= 0;
1984 rfc
.max_pdu_size
= cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE
);
1985 if (L2CAP_DEFAULT_MAX_PDU_SIZE
> chan
->conn
->mtu
- 10)
1986 rfc
.max_pdu_size
= cpu_to_le16(chan
->conn
->mtu
- 10);
1988 l2cap_txwin_setup(chan
);
1990 rfc
.txwin_size
= min_t(u16
, chan
->tx_win
,
1991 L2CAP_DEFAULT_TX_WINDOW
);
1993 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
, sizeof(rfc
),
1994 (unsigned long) &rfc
);
1996 if (!(chan
->conn
->feat_mask
& L2CAP_FEAT_FCS
))
1999 if (chan
->fcs
== L2CAP_FCS_NONE
||
2000 test_bit(CONF_NO_FCS_RECV
, &chan
->conf_state
)) {
2001 chan
->fcs
= L2CAP_FCS_NONE
;
2002 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_FCS
, 1, chan
->fcs
);
2005 if (test_bit(FLAG_EXT_CTRL
, &chan
->flags
))
2006 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_EWS
, 2,
2010 case L2CAP_MODE_STREAMING
:
2011 rfc
.mode
= L2CAP_MODE_STREAMING
;
2013 rfc
.max_transmit
= 0;
2014 rfc
.retrans_timeout
= 0;
2015 rfc
.monitor_timeout
= 0;
2016 rfc
.max_pdu_size
= cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE
);
2017 if (L2CAP_DEFAULT_MAX_PDU_SIZE
> chan
->conn
->mtu
- 10)
2018 rfc
.max_pdu_size
= cpu_to_le16(chan
->conn
->mtu
- 10);
2020 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
, sizeof(rfc
),
2021 (unsigned long) &rfc
);
2023 if (!(chan
->conn
->feat_mask
& L2CAP_FEAT_FCS
))
2026 if (chan
->fcs
== L2CAP_FCS_NONE
||
2027 test_bit(CONF_NO_FCS_RECV
, &chan
->conf_state
)) {
2028 chan
->fcs
= L2CAP_FCS_NONE
;
2029 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_FCS
, 1, chan
->fcs
);
2034 req
->dcid
= cpu_to_le16(chan
->dcid
);
2035 req
->flags
= cpu_to_le16(0);
2040 static int l2cap_parse_conf_req(struct l2cap_chan
*chan
, void *data
)
2042 struct l2cap_conf_rsp
*rsp
= data
;
2043 void *ptr
= rsp
->data
;
2044 void *req
= chan
->conf_req
;
2045 int len
= chan
->conf_len
;
2046 int type
, hint
, olen
;
2048 struct l2cap_conf_rfc rfc
= { .mode
= L2CAP_MODE_BASIC
};
2049 u16 mtu
= L2CAP_DEFAULT_MTU
;
2050 u16 result
= L2CAP_CONF_SUCCESS
;
2052 BT_DBG("chan %p", chan
);
2054 while (len
>= L2CAP_CONF_OPT_SIZE
) {
2055 len
-= l2cap_get_conf_opt(&req
, &type
, &olen
, &val
);
2057 hint
= type
& L2CAP_CONF_HINT
;
2058 type
&= L2CAP_CONF_MASK
;
2061 case L2CAP_CONF_MTU
:
2065 case L2CAP_CONF_FLUSH_TO
:
2066 chan
->flush_to
= val
;
2069 case L2CAP_CONF_QOS
:
2072 case L2CAP_CONF_RFC
:
2073 if (olen
== sizeof(rfc
))
2074 memcpy(&rfc
, (void *) val
, olen
);
2077 case L2CAP_CONF_FCS
:
2078 if (val
== L2CAP_FCS_NONE
)
2079 set_bit(CONF_NO_FCS_RECV
, &chan
->conf_state
);
2083 case L2CAP_CONF_EWS
:
2085 return -ECONNREFUSED
;
2087 set_bit(FLAG_EXT_CTRL
, &chan
->flags
);
2088 set_bit(CONF_EWS_RECV
, &chan
->conf_state
);
2089 chan
->remote_tx_win
= val
;
2096 result
= L2CAP_CONF_UNKNOWN
;
2097 *((u8
*) ptr
++) = type
;
2102 if (chan
->num_conf_rsp
|| chan
->num_conf_req
> 1)
2105 switch (chan
->mode
) {
2106 case L2CAP_MODE_STREAMING
:
2107 case L2CAP_MODE_ERTM
:
2108 if (!test_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
)) {
2109 chan
->mode
= l2cap_select_mode(rfc
.mode
,
2110 chan
->conn
->feat_mask
);
2114 if (chan
->mode
!= rfc
.mode
)
2115 return -ECONNREFUSED
;
2121 if (chan
->mode
!= rfc
.mode
) {
2122 result
= L2CAP_CONF_UNACCEPT
;
2123 rfc
.mode
= chan
->mode
;
2125 if (chan
->num_conf_rsp
== 1)
2126 return -ECONNREFUSED
;
2128 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
,
2129 sizeof(rfc
), (unsigned long) &rfc
);
2133 if (result
== L2CAP_CONF_SUCCESS
) {
2134 /* Configure output options and let the other side know
2135 * which ones we don't like. */
2137 if (mtu
< L2CAP_DEFAULT_MIN_MTU
)
2138 result
= L2CAP_CONF_UNACCEPT
;
2141 set_bit(CONF_MTU_DONE
, &chan
->conf_state
);
2143 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_MTU
, 2, chan
->omtu
);
2146 case L2CAP_MODE_BASIC
:
2147 chan
->fcs
= L2CAP_FCS_NONE
;
2148 set_bit(CONF_MODE_DONE
, &chan
->conf_state
);
2151 case L2CAP_MODE_ERTM
:
2152 if (!test_bit(CONF_EWS_RECV
, &chan
->conf_state
))
2153 chan
->remote_tx_win
= rfc
.txwin_size
;
2155 rfc
.txwin_size
= L2CAP_DEFAULT_TX_WINDOW
;
2157 chan
->remote_max_tx
= rfc
.max_transmit
;
2159 if (le16_to_cpu(rfc
.max_pdu_size
) > chan
->conn
->mtu
- 10)
2160 rfc
.max_pdu_size
= cpu_to_le16(chan
->conn
->mtu
- 10);
2162 chan
->remote_mps
= le16_to_cpu(rfc
.max_pdu_size
);
2164 rfc
.retrans_timeout
=
2165 le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO
);
2166 rfc
.monitor_timeout
=
2167 le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO
);
2169 set_bit(CONF_MODE_DONE
, &chan
->conf_state
);
2171 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
,
2172 sizeof(rfc
), (unsigned long) &rfc
);
2176 case L2CAP_MODE_STREAMING
:
2177 if (le16_to_cpu(rfc
.max_pdu_size
) > chan
->conn
->mtu
- 10)
2178 rfc
.max_pdu_size
= cpu_to_le16(chan
->conn
->mtu
- 10);
2180 chan
->remote_mps
= le16_to_cpu(rfc
.max_pdu_size
);
2182 set_bit(CONF_MODE_DONE
, &chan
->conf_state
);
2184 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
,
2185 sizeof(rfc
), (unsigned long) &rfc
);
2190 result
= L2CAP_CONF_UNACCEPT
;
2192 memset(&rfc
, 0, sizeof(rfc
));
2193 rfc
.mode
= chan
->mode
;
2196 if (result
== L2CAP_CONF_SUCCESS
)
2197 set_bit(CONF_OUTPUT_DONE
, &chan
->conf_state
);
2199 rsp
->scid
= cpu_to_le16(chan
->dcid
);
2200 rsp
->result
= cpu_to_le16(result
);
2201 rsp
->flags
= cpu_to_le16(0x0000);
2206 static int l2cap_parse_conf_rsp(struct l2cap_chan
*chan
, void *rsp
, int len
, void *data
, u16
*result
)
2208 struct l2cap_conf_req
*req
= data
;
2209 void *ptr
= req
->data
;
2212 struct l2cap_conf_rfc rfc
;
2214 BT_DBG("chan %p, rsp %p, len %d, req %p", chan
, rsp
, len
, data
);
2216 while (len
>= L2CAP_CONF_OPT_SIZE
) {
2217 len
-= l2cap_get_conf_opt(&rsp
, &type
, &olen
, &val
);
2220 case L2CAP_CONF_MTU
:
2221 if (val
< L2CAP_DEFAULT_MIN_MTU
) {
2222 *result
= L2CAP_CONF_UNACCEPT
;
2223 chan
->imtu
= L2CAP_DEFAULT_MIN_MTU
;
2226 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_MTU
, 2, chan
->imtu
);
2229 case L2CAP_CONF_FLUSH_TO
:
2230 chan
->flush_to
= val
;
2231 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_FLUSH_TO
,
2235 case L2CAP_CONF_RFC
:
2236 if (olen
== sizeof(rfc
))
2237 memcpy(&rfc
, (void *)val
, olen
);
2239 if (test_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
) &&
2240 rfc
.mode
!= chan
->mode
)
2241 return -ECONNREFUSED
;
2245 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
,
2246 sizeof(rfc
), (unsigned long) &rfc
);
2249 case L2CAP_CONF_EWS
:
2250 chan
->tx_win
= min_t(u16
, val
,
2251 L2CAP_DEFAULT_EXT_WINDOW
);
2252 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_EWS
,
2258 if (chan
->mode
== L2CAP_MODE_BASIC
&& chan
->mode
!= rfc
.mode
)
2259 return -ECONNREFUSED
;
2261 chan
->mode
= rfc
.mode
;
2263 if (*result
== L2CAP_CONF_SUCCESS
) {
2265 case L2CAP_MODE_ERTM
:
2266 chan
->retrans_timeout
= le16_to_cpu(rfc
.retrans_timeout
);
2267 chan
->monitor_timeout
= le16_to_cpu(rfc
.monitor_timeout
);
2268 chan
->mps
= le16_to_cpu(rfc
.max_pdu_size
);
2270 case L2CAP_MODE_STREAMING
:
2271 chan
->mps
= le16_to_cpu(rfc
.max_pdu_size
);
2275 req
->dcid
= cpu_to_le16(chan
->dcid
);
2276 req
->flags
= cpu_to_le16(0x0000);
2281 static int l2cap_build_conf_rsp(struct l2cap_chan
*chan
, void *data
, u16 result
, u16 flags
)
2283 struct l2cap_conf_rsp
*rsp
= data
;
2284 void *ptr
= rsp
->data
;
2286 BT_DBG("chan %p", chan
);
2288 rsp
->scid
= cpu_to_le16(chan
->dcid
);
2289 rsp
->result
= cpu_to_le16(result
);
2290 rsp
->flags
= cpu_to_le16(flags
);
2295 void __l2cap_connect_rsp_defer(struct l2cap_chan
*chan
)
2297 struct l2cap_conn_rsp rsp
;
2298 struct l2cap_conn
*conn
= chan
->conn
;
2301 rsp
.scid
= cpu_to_le16(chan
->dcid
);
2302 rsp
.dcid
= cpu_to_le16(chan
->scid
);
2303 rsp
.result
= cpu_to_le16(L2CAP_CR_SUCCESS
);
2304 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
2305 l2cap_send_cmd(conn
, chan
->ident
,
2306 L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
2308 if (test_and_set_bit(CONF_REQ_SENT
, &chan
->conf_state
))
2311 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
2312 l2cap_build_conf_req(chan
, buf
), buf
);
2313 chan
->num_conf_req
++;
2316 static void l2cap_conf_rfc_get(struct l2cap_chan
*chan
, void *rsp
, int len
)
2320 struct l2cap_conf_rfc rfc
;
2322 BT_DBG("chan %p, rsp %p, len %d", chan
, rsp
, len
);
2324 if ((chan
->mode
!= L2CAP_MODE_ERTM
) && (chan
->mode
!= L2CAP_MODE_STREAMING
))
2327 while (len
>= L2CAP_CONF_OPT_SIZE
) {
2328 len
-= l2cap_get_conf_opt(&rsp
, &type
, &olen
, &val
);
2331 case L2CAP_CONF_RFC
:
2332 if (olen
== sizeof(rfc
))
2333 memcpy(&rfc
, (void *)val
, olen
);
2340 case L2CAP_MODE_ERTM
:
2341 chan
->retrans_timeout
= le16_to_cpu(rfc
.retrans_timeout
);
2342 chan
->monitor_timeout
= le16_to_cpu(rfc
.monitor_timeout
);
2343 chan
->mps
= le16_to_cpu(rfc
.max_pdu_size
);
2345 case L2CAP_MODE_STREAMING
:
2346 chan
->mps
= le16_to_cpu(rfc
.max_pdu_size
);
2350 static inline int l2cap_command_rej(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2352 struct l2cap_cmd_rej_unk
*rej
= (struct l2cap_cmd_rej_unk
*) data
;
2354 if (rej
->reason
!= L2CAP_REJ_NOT_UNDERSTOOD
)
2357 if ((conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_SENT
) &&
2358 cmd
->ident
== conn
->info_ident
) {
2359 del_timer(&conn
->info_timer
);
2361 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
2362 conn
->info_ident
= 0;
2364 l2cap_conn_start(conn
);
2370 static inline int l2cap_connect_req(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2372 struct l2cap_conn_req
*req
= (struct l2cap_conn_req
*) data
;
2373 struct l2cap_conn_rsp rsp
;
2374 struct l2cap_chan
*chan
= NULL
, *pchan
;
2375 struct sock
*parent
, *sk
= NULL
;
2376 int result
, status
= L2CAP_CS_NO_INFO
;
2378 u16 dcid
= 0, scid
= __le16_to_cpu(req
->scid
);
2379 __le16 psm
= req
->psm
;
2381 BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm
, scid
);
2383 /* Check if we have socket listening on psm */
2384 pchan
= l2cap_global_chan_by_psm(BT_LISTEN
, psm
, conn
->src
);
2386 result
= L2CAP_CR_BAD_PSM
;
2392 bh_lock_sock(parent
);
2394 /* Check if the ACL is secure enough (if not SDP) */
2395 if (psm
!= cpu_to_le16(0x0001) &&
2396 !hci_conn_check_link_mode(conn
->hcon
)) {
2397 conn
->disc_reason
= 0x05;
2398 result
= L2CAP_CR_SEC_BLOCK
;
2402 result
= L2CAP_CR_NO_MEM
;
2404 /* Check for backlog size */
2405 if (sk_acceptq_is_full(parent
)) {
2406 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
2410 chan
= pchan
->ops
->new_connection(pchan
->data
);
2416 write_lock_bh(&conn
->chan_lock
);
2418 /* Check if we already have channel with that dcid */
2419 if (__l2cap_get_chan_by_dcid(conn
, scid
)) {
2420 write_unlock_bh(&conn
->chan_lock
);
2421 sock_set_flag(sk
, SOCK_ZAPPED
);
2422 chan
->ops
->close(chan
->data
);
2426 hci_conn_hold(conn
->hcon
);
2428 bacpy(&bt_sk(sk
)->src
, conn
->src
);
2429 bacpy(&bt_sk(sk
)->dst
, conn
->dst
);
2433 bt_accept_enqueue(parent
, sk
);
2435 __l2cap_chan_add(conn
, chan
);
2439 __set_chan_timer(chan
, sk
->sk_sndtimeo
);
2441 chan
->ident
= cmd
->ident
;
2443 if (conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_DONE
) {
2444 if (l2cap_check_security(chan
)) {
2445 if (bt_sk(sk
)->defer_setup
) {
2446 l2cap_state_change(chan
, BT_CONNECT2
);
2447 result
= L2CAP_CR_PEND
;
2448 status
= L2CAP_CS_AUTHOR_PEND
;
2449 parent
->sk_data_ready(parent
, 0);
2451 l2cap_state_change(chan
, BT_CONFIG
);
2452 result
= L2CAP_CR_SUCCESS
;
2453 status
= L2CAP_CS_NO_INFO
;
2456 l2cap_state_change(chan
, BT_CONNECT2
);
2457 result
= L2CAP_CR_PEND
;
2458 status
= L2CAP_CS_AUTHEN_PEND
;
2461 l2cap_state_change(chan
, BT_CONNECT2
);
2462 result
= L2CAP_CR_PEND
;
2463 status
= L2CAP_CS_NO_INFO
;
2466 write_unlock_bh(&conn
->chan_lock
);
2469 bh_unlock_sock(parent
);
2472 rsp
.scid
= cpu_to_le16(scid
);
2473 rsp
.dcid
= cpu_to_le16(dcid
);
2474 rsp
.result
= cpu_to_le16(result
);
2475 rsp
.status
= cpu_to_le16(status
);
2476 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
2478 if (result
== L2CAP_CR_PEND
&& status
== L2CAP_CS_NO_INFO
) {
2479 struct l2cap_info_req info
;
2480 info
.type
= cpu_to_le16(L2CAP_IT_FEAT_MASK
);
2482 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_SENT
;
2483 conn
->info_ident
= l2cap_get_ident(conn
);
2485 mod_timer(&conn
->info_timer
, jiffies
+
2486 msecs_to_jiffies(L2CAP_INFO_TIMEOUT
));
2488 l2cap_send_cmd(conn
, conn
->info_ident
,
2489 L2CAP_INFO_REQ
, sizeof(info
), &info
);
2492 if (chan
&& !test_bit(CONF_REQ_SENT
, &chan
->conf_state
) &&
2493 result
== L2CAP_CR_SUCCESS
) {
2495 set_bit(CONF_REQ_SENT
, &chan
->conf_state
);
2496 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
2497 l2cap_build_conf_req(chan
, buf
), buf
);
2498 chan
->num_conf_req
++;
2504 static inline int l2cap_connect_rsp(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2506 struct l2cap_conn_rsp
*rsp
= (struct l2cap_conn_rsp
*) data
;
2507 u16 scid
, dcid
, result
, status
;
2508 struct l2cap_chan
*chan
;
2512 scid
= __le16_to_cpu(rsp
->scid
);
2513 dcid
= __le16_to_cpu(rsp
->dcid
);
2514 result
= __le16_to_cpu(rsp
->result
);
2515 status
= __le16_to_cpu(rsp
->status
);
2517 BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid
, scid
, result
, status
);
2520 chan
= l2cap_get_chan_by_scid(conn
, scid
);
2524 chan
= l2cap_get_chan_by_ident(conn
, cmd
->ident
);
2532 case L2CAP_CR_SUCCESS
:
2533 l2cap_state_change(chan
, BT_CONFIG
);
2536 clear_bit(CONF_CONNECT_PEND
, &chan
->conf_state
);
2538 if (test_and_set_bit(CONF_REQ_SENT
, &chan
->conf_state
))
2541 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
2542 l2cap_build_conf_req(chan
, req
), req
);
2543 chan
->num_conf_req
++;
2547 set_bit(CONF_CONNECT_PEND
, &chan
->conf_state
);
2551 /* don't delete l2cap channel if sk is owned by user */
2552 if (sock_owned_by_user(sk
)) {
2553 l2cap_state_change(chan
, BT_DISCONN
);
2554 __clear_chan_timer(chan
);
2555 __set_chan_timer(chan
, HZ
/ 5);
2559 l2cap_chan_del(chan
, ECONNREFUSED
);
2567 static inline void set_default_fcs(struct l2cap_chan
*chan
)
2569 /* FCS is enabled only in ERTM or streaming mode, if one or both
2572 if (chan
->mode
!= L2CAP_MODE_ERTM
&& chan
->mode
!= L2CAP_MODE_STREAMING
)
2573 chan
->fcs
= L2CAP_FCS_NONE
;
2574 else if (!test_bit(CONF_NO_FCS_RECV
, &chan
->conf_state
))
2575 chan
->fcs
= L2CAP_FCS_CRC16
;
2578 static inline int l2cap_config_req(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u16 cmd_len
, u8
*data
)
2580 struct l2cap_conf_req
*req
= (struct l2cap_conf_req
*) data
;
2583 struct l2cap_chan
*chan
;
2587 dcid
= __le16_to_cpu(req
->dcid
);
2588 flags
= __le16_to_cpu(req
->flags
);
2590 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid
, flags
);
2592 chan
= l2cap_get_chan_by_scid(conn
, dcid
);
2598 if (chan
->state
!= BT_CONFIG
&& chan
->state
!= BT_CONNECT2
) {
2599 struct l2cap_cmd_rej_cid rej
;
2601 rej
.reason
= cpu_to_le16(L2CAP_REJ_INVALID_CID
);
2602 rej
.scid
= cpu_to_le16(chan
->scid
);
2603 rej
.dcid
= cpu_to_le16(chan
->dcid
);
2605 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_COMMAND_REJ
,
2610 /* Reject if config buffer is too small. */
2611 len
= cmd_len
- sizeof(*req
);
2612 if (len
< 0 || chan
->conf_len
+ len
> sizeof(chan
->conf_req
)) {
2613 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONF_RSP
,
2614 l2cap_build_conf_rsp(chan
, rsp
,
2615 L2CAP_CONF_REJECT
, flags
), rsp
);
2620 memcpy(chan
->conf_req
+ chan
->conf_len
, req
->data
, len
);
2621 chan
->conf_len
+= len
;
2623 if (flags
& 0x0001) {
2624 /* Incomplete config. Send empty response. */
2625 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONF_RSP
,
2626 l2cap_build_conf_rsp(chan
, rsp
,
2627 L2CAP_CONF_SUCCESS
, 0x0001), rsp
);
2631 /* Complete config. */
2632 len
= l2cap_parse_conf_req(chan
, rsp
);
2634 l2cap_send_disconn_req(conn
, chan
, ECONNRESET
);
2638 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONF_RSP
, len
, rsp
);
2639 chan
->num_conf_rsp
++;
2641 /* Reset config buffer. */
2644 if (!test_bit(CONF_OUTPUT_DONE
, &chan
->conf_state
))
2647 if (test_bit(CONF_INPUT_DONE
, &chan
->conf_state
)) {
2648 set_default_fcs(chan
);
2650 l2cap_state_change(chan
, BT_CONNECTED
);
2652 chan
->next_tx_seq
= 0;
2653 chan
->expected_tx_seq
= 0;
2654 skb_queue_head_init(&chan
->tx_q
);
2655 if (chan
->mode
== L2CAP_MODE_ERTM
)
2656 l2cap_ertm_init(chan
);
2658 l2cap_chan_ready(sk
);
2662 if (!test_and_set_bit(CONF_REQ_SENT
, &chan
->conf_state
)) {
2664 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
2665 l2cap_build_conf_req(chan
, buf
), buf
);
2666 chan
->num_conf_req
++;
2674 static inline int l2cap_config_rsp(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2676 struct l2cap_conf_rsp
*rsp
= (struct l2cap_conf_rsp
*)data
;
2677 u16 scid
, flags
, result
;
2678 struct l2cap_chan
*chan
;
2680 int len
= cmd
->len
- sizeof(*rsp
);
2682 scid
= __le16_to_cpu(rsp
->scid
);
2683 flags
= __le16_to_cpu(rsp
->flags
);
2684 result
= __le16_to_cpu(rsp
->result
);
2686 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
2687 scid
, flags
, result
);
2689 chan
= l2cap_get_chan_by_scid(conn
, scid
);
2696 case L2CAP_CONF_SUCCESS
:
2697 l2cap_conf_rfc_get(chan
, rsp
->data
, len
);
2700 case L2CAP_CONF_UNACCEPT
:
2701 if (chan
->num_conf_rsp
<= L2CAP_CONF_MAX_CONF_RSP
) {
2704 if (len
> sizeof(req
) - sizeof(struct l2cap_conf_req
)) {
2705 l2cap_send_disconn_req(conn
, chan
, ECONNRESET
);
2709 /* throw out any old stored conf requests */
2710 result
= L2CAP_CONF_SUCCESS
;
2711 len
= l2cap_parse_conf_rsp(chan
, rsp
->data
, len
,
2714 l2cap_send_disconn_req(conn
, chan
, ECONNRESET
);
2718 l2cap_send_cmd(conn
, l2cap_get_ident(conn
),
2719 L2CAP_CONF_REQ
, len
, req
);
2720 chan
->num_conf_req
++;
2721 if (result
!= L2CAP_CONF_SUCCESS
)
2727 sk
->sk_err
= ECONNRESET
;
2728 __set_chan_timer(chan
, HZ
* 5);
2729 l2cap_send_disconn_req(conn
, chan
, ECONNRESET
);
2736 set_bit(CONF_INPUT_DONE
, &chan
->conf_state
);
2738 if (test_bit(CONF_OUTPUT_DONE
, &chan
->conf_state
)) {
2739 set_default_fcs(chan
);
2741 l2cap_state_change(chan
, BT_CONNECTED
);
2742 chan
->next_tx_seq
= 0;
2743 chan
->expected_tx_seq
= 0;
2744 skb_queue_head_init(&chan
->tx_q
);
2745 if (chan
->mode
== L2CAP_MODE_ERTM
)
2746 l2cap_ertm_init(chan
);
2748 l2cap_chan_ready(sk
);
2756 static inline int l2cap_disconnect_req(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2758 struct l2cap_disconn_req
*req
= (struct l2cap_disconn_req
*) data
;
2759 struct l2cap_disconn_rsp rsp
;
2761 struct l2cap_chan
*chan
;
2764 scid
= __le16_to_cpu(req
->scid
);
2765 dcid
= __le16_to_cpu(req
->dcid
);
2767 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid
, dcid
);
2769 chan
= l2cap_get_chan_by_scid(conn
, dcid
);
2775 rsp
.dcid
= cpu_to_le16(chan
->scid
);
2776 rsp
.scid
= cpu_to_le16(chan
->dcid
);
2777 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_DISCONN_RSP
, sizeof(rsp
), &rsp
);
2779 sk
->sk_shutdown
= SHUTDOWN_MASK
;
2781 /* don't delete l2cap channel if sk is owned by user */
2782 if (sock_owned_by_user(sk
)) {
2783 l2cap_state_change(chan
, BT_DISCONN
);
2784 __clear_chan_timer(chan
);
2785 __set_chan_timer(chan
, HZ
/ 5);
2790 l2cap_chan_del(chan
, ECONNRESET
);
2793 chan
->ops
->close(chan
->data
);
2797 static inline int l2cap_disconnect_rsp(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2799 struct l2cap_disconn_rsp
*rsp
= (struct l2cap_disconn_rsp
*) data
;
2801 struct l2cap_chan
*chan
;
2804 scid
= __le16_to_cpu(rsp
->scid
);
2805 dcid
= __le16_to_cpu(rsp
->dcid
);
2807 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid
, scid
);
2809 chan
= l2cap_get_chan_by_scid(conn
, scid
);
2815 /* don't delete l2cap channel if sk is owned by user */
2816 if (sock_owned_by_user(sk
)) {
2817 l2cap_state_change(chan
,BT_DISCONN
);
2818 __clear_chan_timer(chan
);
2819 __set_chan_timer(chan
, HZ
/ 5);
2824 l2cap_chan_del(chan
, 0);
2827 chan
->ops
->close(chan
->data
);
2831 static inline int l2cap_information_req(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2833 struct l2cap_info_req
*req
= (struct l2cap_info_req
*) data
;
2836 type
= __le16_to_cpu(req
->type
);
2838 BT_DBG("type 0x%4.4x", type
);
2840 if (type
== L2CAP_IT_FEAT_MASK
) {
2842 u32 feat_mask
= l2cap_feat_mask
;
2843 struct l2cap_info_rsp
*rsp
= (struct l2cap_info_rsp
*) buf
;
2844 rsp
->type
= cpu_to_le16(L2CAP_IT_FEAT_MASK
);
2845 rsp
->result
= cpu_to_le16(L2CAP_IR_SUCCESS
);
2847 feat_mask
|= L2CAP_FEAT_ERTM
| L2CAP_FEAT_STREAMING
2850 feat_mask
|= L2CAP_FEAT_EXT_FLOW
2851 | L2CAP_FEAT_EXT_WINDOW
;
2853 put_unaligned_le32(feat_mask
, rsp
->data
);
2854 l2cap_send_cmd(conn
, cmd
->ident
,
2855 L2CAP_INFO_RSP
, sizeof(buf
), buf
);
2856 } else if (type
== L2CAP_IT_FIXED_CHAN
) {
2858 struct l2cap_info_rsp
*rsp
= (struct l2cap_info_rsp
*) buf
;
2859 rsp
->type
= cpu_to_le16(L2CAP_IT_FIXED_CHAN
);
2860 rsp
->result
= cpu_to_le16(L2CAP_IR_SUCCESS
);
2861 memcpy(buf
+ 4, l2cap_fixed_chan
, 8);
2862 l2cap_send_cmd(conn
, cmd
->ident
,
2863 L2CAP_INFO_RSP
, sizeof(buf
), buf
);
2865 struct l2cap_info_rsp rsp
;
2866 rsp
.type
= cpu_to_le16(type
);
2867 rsp
.result
= cpu_to_le16(L2CAP_IR_NOTSUPP
);
2868 l2cap_send_cmd(conn
, cmd
->ident
,
2869 L2CAP_INFO_RSP
, sizeof(rsp
), &rsp
);
2875 static inline int l2cap_information_rsp(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2877 struct l2cap_info_rsp
*rsp
= (struct l2cap_info_rsp
*) data
;
2880 type
= __le16_to_cpu(rsp
->type
);
2881 result
= __le16_to_cpu(rsp
->result
);
2883 BT_DBG("type 0x%4.4x result 0x%2.2x", type
, result
);
2885 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
2886 if (cmd
->ident
!= conn
->info_ident
||
2887 conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_DONE
)
2890 del_timer(&conn
->info_timer
);
2892 if (result
!= L2CAP_IR_SUCCESS
) {
2893 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
2894 conn
->info_ident
= 0;
2896 l2cap_conn_start(conn
);
2901 if (type
== L2CAP_IT_FEAT_MASK
) {
2902 conn
->feat_mask
= get_unaligned_le32(rsp
->data
);
2904 if (conn
->feat_mask
& L2CAP_FEAT_FIXED_CHAN
) {
2905 struct l2cap_info_req req
;
2906 req
.type
= cpu_to_le16(L2CAP_IT_FIXED_CHAN
);
2908 conn
->info_ident
= l2cap_get_ident(conn
);
2910 l2cap_send_cmd(conn
, conn
->info_ident
,
2911 L2CAP_INFO_REQ
, sizeof(req
), &req
);
2913 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
2914 conn
->info_ident
= 0;
2916 l2cap_conn_start(conn
);
2918 } else if (type
== L2CAP_IT_FIXED_CHAN
) {
2919 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
2920 conn
->info_ident
= 0;
2922 l2cap_conn_start(conn
);
2928 static inline int l2cap_check_conn_param(u16 min
, u16 max
, u16 latency
,
2933 if (min
> max
|| min
< 6 || max
> 3200)
2936 if (to_multiplier
< 10 || to_multiplier
> 3200)
2939 if (max
>= to_multiplier
* 8)
2942 max_latency
= (to_multiplier
* 8 / max
) - 1;
2943 if (latency
> 499 || latency
> max_latency
)
2949 static inline int l2cap_conn_param_update_req(struct l2cap_conn
*conn
,
2950 struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2952 struct hci_conn
*hcon
= conn
->hcon
;
2953 struct l2cap_conn_param_update_req
*req
;
2954 struct l2cap_conn_param_update_rsp rsp
;
2955 u16 min
, max
, latency
, to_multiplier
, cmd_len
;
2958 if (!(hcon
->link_mode
& HCI_LM_MASTER
))
2961 cmd_len
= __le16_to_cpu(cmd
->len
);
2962 if (cmd_len
!= sizeof(struct l2cap_conn_param_update_req
))
2965 req
= (struct l2cap_conn_param_update_req
*) data
;
2966 min
= __le16_to_cpu(req
->min
);
2967 max
= __le16_to_cpu(req
->max
);
2968 latency
= __le16_to_cpu(req
->latency
);
2969 to_multiplier
= __le16_to_cpu(req
->to_multiplier
);
2971 BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
2972 min
, max
, latency
, to_multiplier
);
2974 memset(&rsp
, 0, sizeof(rsp
));
2976 err
= l2cap_check_conn_param(min
, max
, latency
, to_multiplier
);
2978 rsp
.result
= cpu_to_le16(L2CAP_CONN_PARAM_REJECTED
);
2980 rsp
.result
= cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED
);
2982 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONN_PARAM_UPDATE_RSP
,
2986 hci_le_conn_update(hcon
, min
, max
, latency
, to_multiplier
);
2991 static inline int l2cap_bredr_sig_cmd(struct l2cap_conn
*conn
,
2992 struct l2cap_cmd_hdr
*cmd
, u16 cmd_len
, u8
*data
)
2996 switch (cmd
->code
) {
2997 case L2CAP_COMMAND_REJ
:
2998 l2cap_command_rej(conn
, cmd
, data
);
3001 case L2CAP_CONN_REQ
:
3002 err
= l2cap_connect_req(conn
, cmd
, data
);
3005 case L2CAP_CONN_RSP
:
3006 err
= l2cap_connect_rsp(conn
, cmd
, data
);
3009 case L2CAP_CONF_REQ
:
3010 err
= l2cap_config_req(conn
, cmd
, cmd_len
, data
);
3013 case L2CAP_CONF_RSP
:
3014 err
= l2cap_config_rsp(conn
, cmd
, data
);
3017 case L2CAP_DISCONN_REQ
:
3018 err
= l2cap_disconnect_req(conn
, cmd
, data
);
3021 case L2CAP_DISCONN_RSP
:
3022 err
= l2cap_disconnect_rsp(conn
, cmd
, data
);
3025 case L2CAP_ECHO_REQ
:
3026 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_ECHO_RSP
, cmd_len
, data
);
3029 case L2CAP_ECHO_RSP
:
3032 case L2CAP_INFO_REQ
:
3033 err
= l2cap_information_req(conn
, cmd
, data
);
3036 case L2CAP_INFO_RSP
:
3037 err
= l2cap_information_rsp(conn
, cmd
, data
);
3041 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd
->code
);
3049 static inline int l2cap_le_sig_cmd(struct l2cap_conn
*conn
,
3050 struct l2cap_cmd_hdr
*cmd
, u8
*data
)
3052 switch (cmd
->code
) {
3053 case L2CAP_COMMAND_REJ
:
3056 case L2CAP_CONN_PARAM_UPDATE_REQ
:
3057 return l2cap_conn_param_update_req(conn
, cmd
, data
);
3059 case L2CAP_CONN_PARAM_UPDATE_RSP
:
3063 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd
->code
);
3068 static inline void l2cap_sig_channel(struct l2cap_conn
*conn
,
3069 struct sk_buff
*skb
)
3071 u8
*data
= skb
->data
;
3073 struct l2cap_cmd_hdr cmd
;
3076 l2cap_raw_recv(conn
, skb
);
3078 while (len
>= L2CAP_CMD_HDR_SIZE
) {
3080 memcpy(&cmd
, data
, L2CAP_CMD_HDR_SIZE
);
3081 data
+= L2CAP_CMD_HDR_SIZE
;
3082 len
-= L2CAP_CMD_HDR_SIZE
;
3084 cmd_len
= le16_to_cpu(cmd
.len
);
3086 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd
.code
, cmd_len
, cmd
.ident
);
3088 if (cmd_len
> len
|| !cmd
.ident
) {
3089 BT_DBG("corrupted command");
3093 if (conn
->hcon
->type
== LE_LINK
)
3094 err
= l2cap_le_sig_cmd(conn
, &cmd
, data
);
3096 err
= l2cap_bredr_sig_cmd(conn
, &cmd
, cmd_len
, data
);
3099 struct l2cap_cmd_rej_unk rej
;
3101 BT_ERR("Wrong link type (%d)", err
);
3103 /* FIXME: Map err to a valid reason */
3104 rej
.reason
= cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD
);
3105 l2cap_send_cmd(conn
, cmd
.ident
, L2CAP_COMMAND_REJ
, sizeof(rej
), &rej
);
3115 static int l2cap_check_fcs(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
3117 u16 our_fcs
, rcv_fcs
;
3120 if (test_bit(FLAG_EXT_CTRL
, &chan
->flags
))
3121 hdr_size
= L2CAP_EXT_HDR_SIZE
;
3123 hdr_size
= L2CAP_ENH_HDR_SIZE
;
3125 if (chan
->fcs
== L2CAP_FCS_CRC16
) {
3126 skb_trim(skb
, skb
->len
- 2);
3127 rcv_fcs
= get_unaligned_le16(skb
->data
+ skb
->len
);
3128 our_fcs
= crc16(0, skb
->data
- hdr_size
, skb
->len
+ hdr_size
);
3130 if (our_fcs
!= rcv_fcs
)
3136 static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan
*chan
)
3140 chan
->frames_sent
= 0;
3142 control
|= __set_reqseq(chan
, chan
->buffer_seq
);
3144 if (test_bit(CONN_LOCAL_BUSY
, &chan
->conn_state
)) {
3145 control
|= __set_ctrl_super(chan
, L2CAP_SUPER_RNR
);
3146 l2cap_send_sframe(chan
, control
);
3147 set_bit(CONN_RNR_SENT
, &chan
->conn_state
);
3150 if (test_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
))
3151 l2cap_retransmit_frames(chan
);
3153 l2cap_ertm_send(chan
);
3155 if (!test_bit(CONN_LOCAL_BUSY
, &chan
->conn_state
) &&
3156 chan
->frames_sent
== 0) {
3157 control
|= __set_ctrl_super(chan
, L2CAP_SUPER_RR
);
3158 l2cap_send_sframe(chan
, control
);
3162 static int l2cap_add_to_srej_queue(struct l2cap_chan
*chan
, struct sk_buff
*skb
, u16 tx_seq
, u8 sar
)
3164 struct sk_buff
*next_skb
;
3165 int tx_seq_offset
, next_tx_seq_offset
;
3167 bt_cb(skb
)->tx_seq
= tx_seq
;
3168 bt_cb(skb
)->sar
= sar
;
3170 next_skb
= skb_peek(&chan
->srej_q
);
3172 __skb_queue_tail(&chan
->srej_q
, skb
);
3176 tx_seq_offset
= (tx_seq
- chan
->buffer_seq
) % 64;
3177 if (tx_seq_offset
< 0)
3178 tx_seq_offset
+= 64;
3181 if (bt_cb(next_skb
)->tx_seq
== tx_seq
)
3184 next_tx_seq_offset
= (bt_cb(next_skb
)->tx_seq
-
3185 chan
->buffer_seq
) % 64;
3186 if (next_tx_seq_offset
< 0)
3187 next_tx_seq_offset
+= 64;
3189 if (next_tx_seq_offset
> tx_seq_offset
) {
3190 __skb_queue_before(&chan
->srej_q
, next_skb
, skb
);
3194 if (skb_queue_is_last(&chan
->srej_q
, next_skb
))
3197 } while ((next_skb
= skb_queue_next(&chan
->srej_q
, next_skb
)));
3199 __skb_queue_tail(&chan
->srej_q
, skb
);
3204 static void append_skb_frag(struct sk_buff
*skb
,
3205 struct sk_buff
*new_frag
, struct sk_buff
**last_frag
)
3207 /* skb->len reflects data in skb as well as all fragments
3208 * skb->data_len reflects only data in fragments
3210 if (!skb_has_frag_list(skb
))
3211 skb_shinfo(skb
)->frag_list
= new_frag
;
3213 new_frag
->next
= NULL
;
3215 (*last_frag
)->next
= new_frag
;
3216 *last_frag
= new_frag
;
3218 skb
->len
+= new_frag
->len
;
3219 skb
->data_len
+= new_frag
->len
;
3220 skb
->truesize
+= new_frag
->truesize
;
3223 static int l2cap_reassemble_sdu(struct l2cap_chan
*chan
, struct sk_buff
*skb
, u16 control
)
3227 switch (__get_ctrl_sar(chan
, control
)) {
3228 case L2CAP_SAR_UNSEGMENTED
:
3232 err
= chan
->ops
->recv(chan
->data
, skb
);
3235 case L2CAP_SAR_START
:
3239 chan
->sdu_len
= get_unaligned_le16(skb
->data
);
3242 if (chan
->sdu_len
> chan
->imtu
) {
3247 if (skb
->len
>= chan
->sdu_len
)
3251 chan
->sdu_last_frag
= skb
;
3257 case L2CAP_SAR_CONTINUE
:
3261 append_skb_frag(chan
->sdu
, skb
,
3262 &chan
->sdu_last_frag
);
3265 if (chan
->sdu
->len
>= chan
->sdu_len
)
3275 append_skb_frag(chan
->sdu
, skb
,
3276 &chan
->sdu_last_frag
);
3279 if (chan
->sdu
->len
!= chan
->sdu_len
)
3282 err
= chan
->ops
->recv(chan
->data
, chan
->sdu
);
3285 /* Reassembly complete */
3287 chan
->sdu_last_frag
= NULL
;
3295 kfree_skb(chan
->sdu
);
3297 chan
->sdu_last_frag
= NULL
;
3304 static void l2cap_ertm_enter_local_busy(struct l2cap_chan
*chan
)
3308 BT_DBG("chan %p, Enter local busy", chan
);
3310 set_bit(CONN_LOCAL_BUSY
, &chan
->conn_state
);
3312 control
= __set_reqseq(chan
, chan
->buffer_seq
);
3313 control
|= __set_ctrl_super(chan
, L2CAP_SUPER_RNR
);
3314 l2cap_send_sframe(chan
, control
);
3316 set_bit(CONN_RNR_SENT
, &chan
->conn_state
);
3318 __clear_ack_timer(chan
);
3321 static void l2cap_ertm_exit_local_busy(struct l2cap_chan
*chan
)
3325 if (!test_bit(CONN_RNR_SENT
, &chan
->conn_state
))
3328 control
= __set_reqseq(chan
, chan
->buffer_seq
);
3329 control
|= __set_ctrl_poll(chan
);
3330 control
|= __set_ctrl_super(chan
, L2CAP_SUPER_RR
);
3331 l2cap_send_sframe(chan
, control
);
3332 chan
->retry_count
= 1;
3334 __clear_retrans_timer(chan
);
3335 __set_monitor_timer(chan
);
3337 set_bit(CONN_WAIT_F
, &chan
->conn_state
);
3340 clear_bit(CONN_LOCAL_BUSY
, &chan
->conn_state
);
3341 clear_bit(CONN_RNR_SENT
, &chan
->conn_state
);
3343 BT_DBG("chan %p, Exit local busy", chan
);
3346 void l2cap_chan_busy(struct l2cap_chan
*chan
, int busy
)
3348 if (chan
->mode
== L2CAP_MODE_ERTM
) {
3350 l2cap_ertm_enter_local_busy(chan
);
3352 l2cap_ertm_exit_local_busy(chan
);
3356 static void l2cap_check_srej_gap(struct l2cap_chan
*chan
, u16 tx_seq
)
3358 struct sk_buff
*skb
;
3361 while ((skb
= skb_peek(&chan
->srej_q
)) &&
3362 !test_bit(CONN_LOCAL_BUSY
, &chan
->conn_state
)) {
3365 if (bt_cb(skb
)->tx_seq
!= tx_seq
)
3368 skb
= skb_dequeue(&chan
->srej_q
);
3369 control
= __set_ctrl_sar(chan
, bt_cb(skb
)->sar
);
3370 err
= l2cap_reassemble_sdu(chan
, skb
, control
);
3373 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNRESET
);
3377 chan
->buffer_seq_srej
=
3378 (chan
->buffer_seq_srej
+ 1) % 64;
3379 tx_seq
= (tx_seq
+ 1) % 64;
3383 static void l2cap_resend_srejframe(struct l2cap_chan
*chan
, u16 tx_seq
)
3385 struct srej_list
*l
, *tmp
;
3388 list_for_each_entry_safe(l
, tmp
, &chan
->srej_l
, list
) {
3389 if (l
->tx_seq
== tx_seq
) {
3394 control
= __set_ctrl_super(chan
, L2CAP_SUPER_SREJ
);
3395 control
|= __set_reqseq(chan
, l
->tx_seq
);
3396 l2cap_send_sframe(chan
, control
);
3398 list_add_tail(&l
->list
, &chan
->srej_l
);
3402 static void l2cap_send_srejframe(struct l2cap_chan
*chan
, u16 tx_seq
)
3404 struct srej_list
*new;
3407 while (tx_seq
!= chan
->expected_tx_seq
) {
3408 control
= __set_ctrl_super(chan
, L2CAP_SUPER_SREJ
);
3409 control
|= __set_reqseq(chan
, chan
->expected_tx_seq
);
3410 l2cap_send_sframe(chan
, control
);
3412 new = kzalloc(sizeof(struct srej_list
), GFP_ATOMIC
);
3413 new->tx_seq
= chan
->expected_tx_seq
;
3414 chan
->expected_tx_seq
= (chan
->expected_tx_seq
+ 1) % 64;
3415 list_add_tail(&new->list
, &chan
->srej_l
);
3417 chan
->expected_tx_seq
= (chan
->expected_tx_seq
+ 1) % 64;
3420 static inline int l2cap_data_channel_iframe(struct l2cap_chan
*chan
, u16 rx_control
, struct sk_buff
*skb
)
3422 u16 tx_seq
= __get_txseq(chan
, rx_control
);
3423 u16 req_seq
= __get_reqseq(chan
, rx_control
);
3424 u8 sar
= __get_ctrl_sar(chan
, rx_control
);
3425 int tx_seq_offset
, expected_tx_seq_offset
;
3426 int num_to_ack
= (chan
->tx_win
/6) + 1;
3429 BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan
, skb
->len
,
3430 tx_seq
, rx_control
);
3432 if (__is_ctrl_final(chan
, rx_control
) &&
3433 test_bit(CONN_WAIT_F
, &chan
->conn_state
)) {
3434 __clear_monitor_timer(chan
);
3435 if (chan
->unacked_frames
> 0)
3436 __set_retrans_timer(chan
);
3437 clear_bit(CONN_WAIT_F
, &chan
->conn_state
);
3440 chan
->expected_ack_seq
= req_seq
;
3441 l2cap_drop_acked_frames(chan
);
3443 tx_seq_offset
= (tx_seq
- chan
->buffer_seq
) % 64;
3444 if (tx_seq_offset
< 0)
3445 tx_seq_offset
+= 64;
3447 /* invalid tx_seq */
3448 if (tx_seq_offset
>= chan
->tx_win
) {
3449 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNRESET
);
3453 if (test_bit(CONN_LOCAL_BUSY
, &chan
->conn_state
))
3456 if (tx_seq
== chan
->expected_tx_seq
)
3459 if (test_bit(CONN_SREJ_SENT
, &chan
->conn_state
)) {
3460 struct srej_list
*first
;
3462 first
= list_first_entry(&chan
->srej_l
,
3463 struct srej_list
, list
);
3464 if (tx_seq
== first
->tx_seq
) {
3465 l2cap_add_to_srej_queue(chan
, skb
, tx_seq
, sar
);
3466 l2cap_check_srej_gap(chan
, tx_seq
);
3468 list_del(&first
->list
);
3471 if (list_empty(&chan
->srej_l
)) {
3472 chan
->buffer_seq
= chan
->buffer_seq_srej
;
3473 clear_bit(CONN_SREJ_SENT
, &chan
->conn_state
);
3474 l2cap_send_ack(chan
);
3475 BT_DBG("chan %p, Exit SREJ_SENT", chan
);
3478 struct srej_list
*l
;
3480 /* duplicated tx_seq */
3481 if (l2cap_add_to_srej_queue(chan
, skb
, tx_seq
, sar
) < 0)
3484 list_for_each_entry(l
, &chan
->srej_l
, list
) {
3485 if (l
->tx_seq
== tx_seq
) {
3486 l2cap_resend_srejframe(chan
, tx_seq
);
3490 l2cap_send_srejframe(chan
, tx_seq
);
3493 expected_tx_seq_offset
=
3494 (chan
->expected_tx_seq
- chan
->buffer_seq
) % 64;
3495 if (expected_tx_seq_offset
< 0)
3496 expected_tx_seq_offset
+= 64;
3498 /* duplicated tx_seq */
3499 if (tx_seq_offset
< expected_tx_seq_offset
)
3502 set_bit(CONN_SREJ_SENT
, &chan
->conn_state
);
3504 BT_DBG("chan %p, Enter SREJ", chan
);
3506 INIT_LIST_HEAD(&chan
->srej_l
);
3507 chan
->buffer_seq_srej
= chan
->buffer_seq
;
3509 __skb_queue_head_init(&chan
->srej_q
);
3510 l2cap_add_to_srej_queue(chan
, skb
, tx_seq
, sar
);
3512 set_bit(CONN_SEND_PBIT
, &chan
->conn_state
);
3514 l2cap_send_srejframe(chan
, tx_seq
);
3516 __clear_ack_timer(chan
);
3521 chan
->expected_tx_seq
= (chan
->expected_tx_seq
+ 1) % 64;
3523 if (test_bit(CONN_SREJ_SENT
, &chan
->conn_state
)) {
3524 bt_cb(skb
)->tx_seq
= tx_seq
;
3525 bt_cb(skb
)->sar
= sar
;
3526 __skb_queue_tail(&chan
->srej_q
, skb
);
3530 err
= l2cap_reassemble_sdu(chan
, skb
, rx_control
);
3531 chan
->buffer_seq
= (chan
->buffer_seq
+ 1) % 64;
3533 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNRESET
);
3537 if (__is_ctrl_final(chan
, rx_control
)) {
3538 if (!test_and_clear_bit(CONN_REJ_ACT
, &chan
->conn_state
))
3539 l2cap_retransmit_frames(chan
);
3542 __set_ack_timer(chan
);
3544 chan
->num_acked
= (chan
->num_acked
+ 1) % num_to_ack
;
3545 if (chan
->num_acked
== num_to_ack
- 1)
3546 l2cap_send_ack(chan
);
3555 static inline void l2cap_data_channel_rrframe(struct l2cap_chan
*chan
, u16 rx_control
)
3557 BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan
,
3558 __get_reqseq(chan
, rx_control
), rx_control
);
3560 chan
->expected_ack_seq
= __get_reqseq(chan
, rx_control
);
3561 l2cap_drop_acked_frames(chan
);
3563 if (__is_ctrl_poll(chan
, rx_control
)) {
3564 set_bit(CONN_SEND_FBIT
, &chan
->conn_state
);
3565 if (test_bit(CONN_SREJ_SENT
, &chan
->conn_state
)) {
3566 if (test_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
) &&
3567 (chan
->unacked_frames
> 0))
3568 __set_retrans_timer(chan
);
3570 clear_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
);
3571 l2cap_send_srejtail(chan
);
3573 l2cap_send_i_or_rr_or_rnr(chan
);
3576 } else if (__is_ctrl_final(chan
, rx_control
)) {
3577 clear_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
);
3579 if (!test_and_clear_bit(CONN_REJ_ACT
, &chan
->conn_state
))
3580 l2cap_retransmit_frames(chan
);
3583 if (test_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
) &&
3584 (chan
->unacked_frames
> 0))
3585 __set_retrans_timer(chan
);
3587 clear_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
);
3588 if (test_bit(CONN_SREJ_SENT
, &chan
->conn_state
))
3589 l2cap_send_ack(chan
);
3591 l2cap_ertm_send(chan
);
3595 static inline void l2cap_data_channel_rejframe(struct l2cap_chan
*chan
, u16 rx_control
)
3597 u16 tx_seq
= __get_reqseq(chan
, rx_control
);
3599 BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan
, tx_seq
, rx_control
);
3601 clear_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
);
3603 chan
->expected_ack_seq
= tx_seq
;
3604 l2cap_drop_acked_frames(chan
);
3606 if (__is_ctrl_final(chan
, rx_control
)) {
3607 if (!test_and_clear_bit(CONN_REJ_ACT
, &chan
->conn_state
))
3608 l2cap_retransmit_frames(chan
);
3610 l2cap_retransmit_frames(chan
);
3612 if (test_bit(CONN_WAIT_F
, &chan
->conn_state
))
3613 set_bit(CONN_REJ_ACT
, &chan
->conn_state
);
3616 static inline void l2cap_data_channel_srejframe(struct l2cap_chan
*chan
, u16 rx_control
)
3618 u16 tx_seq
= __get_reqseq(chan
, rx_control
);
3620 BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan
, tx_seq
, rx_control
);
3622 clear_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
);
3624 if (__is_ctrl_poll(chan
, rx_control
)) {
3625 chan
->expected_ack_seq
= tx_seq
;
3626 l2cap_drop_acked_frames(chan
);
3628 set_bit(CONN_SEND_FBIT
, &chan
->conn_state
);
3629 l2cap_retransmit_one_frame(chan
, tx_seq
);
3631 l2cap_ertm_send(chan
);
3633 if (test_bit(CONN_WAIT_F
, &chan
->conn_state
)) {
3634 chan
->srej_save_reqseq
= tx_seq
;
3635 set_bit(CONN_SREJ_ACT
, &chan
->conn_state
);
3637 } else if (__is_ctrl_final(chan
, rx_control
)) {
3638 if (test_bit(CONN_SREJ_ACT
, &chan
->conn_state
) &&
3639 chan
->srej_save_reqseq
== tx_seq
)
3640 clear_bit(CONN_SREJ_ACT
, &chan
->conn_state
);
3642 l2cap_retransmit_one_frame(chan
, tx_seq
);
3644 l2cap_retransmit_one_frame(chan
, tx_seq
);
3645 if (test_bit(CONN_WAIT_F
, &chan
->conn_state
)) {
3646 chan
->srej_save_reqseq
= tx_seq
;
3647 set_bit(CONN_SREJ_ACT
, &chan
->conn_state
);
3652 static inline void l2cap_data_channel_rnrframe(struct l2cap_chan
*chan
, u16 rx_control
)
3654 u16 tx_seq
= __get_reqseq(chan
, rx_control
);
3656 BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan
, tx_seq
, rx_control
);
3658 set_bit(CONN_REMOTE_BUSY
, &chan
->conn_state
);
3659 chan
->expected_ack_seq
= tx_seq
;
3660 l2cap_drop_acked_frames(chan
);
3662 if (__is_ctrl_poll(chan
, rx_control
))
3663 set_bit(CONN_SEND_FBIT
, &chan
->conn_state
);
3665 if (!test_bit(CONN_SREJ_SENT
, &chan
->conn_state
)) {
3666 __clear_retrans_timer(chan
);
3667 if (__is_ctrl_poll(chan
, rx_control
))
3668 l2cap_send_rr_or_rnr(chan
, L2CAP_CTRL_FINAL
);
3672 if (__is_ctrl_poll(chan
, rx_control
)) {
3673 l2cap_send_srejtail(chan
);
3675 rx_control
= __set_ctrl_super(chan
, L2CAP_SUPER_RR
);
3676 l2cap_send_sframe(chan
, rx_control
);
3680 static inline int l2cap_data_channel_sframe(struct l2cap_chan
*chan
, u16 rx_control
, struct sk_buff
*skb
)
3682 BT_DBG("chan %p rx_control 0x%4.4x len %d", chan
, rx_control
, skb
->len
);
3684 if (__is_ctrl_final(chan
, rx_control
) &&
3685 test_bit(CONN_WAIT_F
, &chan
->conn_state
)) {
3686 __clear_monitor_timer(chan
);
3687 if (chan
->unacked_frames
> 0)
3688 __set_retrans_timer(chan
);
3689 clear_bit(CONN_WAIT_F
, &chan
->conn_state
);
3692 switch (__get_ctrl_super(chan
, rx_control
)) {
3693 case L2CAP_SUPER_RR
:
3694 l2cap_data_channel_rrframe(chan
, rx_control
);
3697 case L2CAP_SUPER_REJ
:
3698 l2cap_data_channel_rejframe(chan
, rx_control
);
3701 case L2CAP_SUPER_SREJ
:
3702 l2cap_data_channel_srejframe(chan
, rx_control
);
3705 case L2CAP_SUPER_RNR
:
3706 l2cap_data_channel_rnrframe(chan
, rx_control
);
3714 static int l2cap_ertm_data_rcv(struct sock
*sk
, struct sk_buff
*skb
)
3716 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
3719 int len
, next_tx_seq_offset
, req_seq_offset
;
3721 control
= get_unaligned_le16(skb
->data
);
3726 * We can just drop the corrupted I-frame here.
3727 * Receiver will miss it and start proper recovery
3728 * procedures and ask retransmission.
3730 if (l2cap_check_fcs(chan
, skb
))
3733 if (__is_sar_start(chan
, control
) && !__is_sframe(chan
, control
))
3736 if (chan
->fcs
== L2CAP_FCS_CRC16
)
3739 if (len
> chan
->mps
) {
3740 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNRESET
);
3744 req_seq
= __get_reqseq(chan
, control
);
3745 req_seq_offset
= (req_seq
- chan
->expected_ack_seq
) % 64;
3746 if (req_seq_offset
< 0)
3747 req_seq_offset
+= 64;
3749 next_tx_seq_offset
=
3750 (chan
->next_tx_seq
- chan
->expected_ack_seq
) % 64;
3751 if (next_tx_seq_offset
< 0)
3752 next_tx_seq_offset
+= 64;
3754 /* check for invalid req-seq */
3755 if (req_seq_offset
> next_tx_seq_offset
) {
3756 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNRESET
);
3760 if (!__is_sframe(chan
, control
)) {
3762 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNRESET
);
3766 l2cap_data_channel_iframe(chan
, control
, skb
);
3770 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNRESET
);
3774 l2cap_data_channel_sframe(chan
, control
, skb
);
3784 static inline int l2cap_data_channel(struct l2cap_conn
*conn
, u16 cid
, struct sk_buff
*skb
)
3786 struct l2cap_chan
*chan
;
3787 struct sock
*sk
= NULL
;
3792 chan
= l2cap_get_chan_by_scid(conn
, cid
);
3794 BT_DBG("unknown cid 0x%4.4x", cid
);
3800 BT_DBG("chan %p, len %d", chan
, skb
->len
);
3802 if (chan
->state
!= BT_CONNECTED
)
3805 switch (chan
->mode
) {
3806 case L2CAP_MODE_BASIC
:
3807 /* If socket recv buffers overflows we drop data here
3808 * which is *bad* because L2CAP has to be reliable.
3809 * But we don't have any other choice. L2CAP doesn't
3810 * provide flow control mechanism. */
3812 if (chan
->imtu
< skb
->len
)
3815 if (!chan
->ops
->recv(chan
->data
, skb
))
3819 case L2CAP_MODE_ERTM
:
3820 if (!sock_owned_by_user(sk
)) {
3821 l2cap_ertm_data_rcv(sk
, skb
);
3823 if (sk_add_backlog(sk
, skb
))
3829 case L2CAP_MODE_STREAMING
:
3830 control
= get_unaligned_le16(skb
->data
);
3834 if (l2cap_check_fcs(chan
, skb
))
3837 if (__is_sar_start(chan
, control
))
3840 if (chan
->fcs
== L2CAP_FCS_CRC16
)
3843 if (len
> chan
->mps
|| len
< 0 || __is_sframe(chan
, control
))
3846 tx_seq
= __get_txseq(chan
, control
);
3848 if (chan
->expected_tx_seq
!= tx_seq
) {
3849 /* Frame(s) missing - must discard partial SDU */
3850 kfree_skb(chan
->sdu
);
3852 chan
->sdu_last_frag
= NULL
;
3855 /* TODO: Notify userland of missing data */
3858 chan
->expected_tx_seq
= (tx_seq
+ 1) % 64;
3860 if (l2cap_reassemble_sdu(chan
, skb
, control
) == -EMSGSIZE
)
3861 l2cap_send_disconn_req(chan
->conn
, chan
, ECONNRESET
);
3866 BT_DBG("chan %p: bad mode 0x%2.2x", chan
, chan
->mode
);
3880 static inline int l2cap_conless_channel(struct l2cap_conn
*conn
, __le16 psm
, struct sk_buff
*skb
)
3882 struct sock
*sk
= NULL
;
3883 struct l2cap_chan
*chan
;
3885 chan
= l2cap_global_chan_by_psm(0, psm
, conn
->src
);
3893 BT_DBG("sk %p, len %d", sk
, skb
->len
);
3895 if (chan
->state
!= BT_BOUND
&& chan
->state
!= BT_CONNECTED
)
3898 if (chan
->imtu
< skb
->len
)
3901 if (!chan
->ops
->recv(chan
->data
, skb
))
3913 static inline int l2cap_att_channel(struct l2cap_conn
*conn
, __le16 cid
, struct sk_buff
*skb
)
3915 struct sock
*sk
= NULL
;
3916 struct l2cap_chan
*chan
;
3918 chan
= l2cap_global_chan_by_scid(0, cid
, conn
->src
);
3926 BT_DBG("sk %p, len %d", sk
, skb
->len
);
3928 if (chan
->state
!= BT_BOUND
&& chan
->state
!= BT_CONNECTED
)
3931 if (chan
->imtu
< skb
->len
)
3934 if (!chan
->ops
->recv(chan
->data
, skb
))
3946 static void l2cap_recv_frame(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
3948 struct l2cap_hdr
*lh
= (void *) skb
->data
;
3952 skb_pull(skb
, L2CAP_HDR_SIZE
);
3953 cid
= __le16_to_cpu(lh
->cid
);
3954 len
= __le16_to_cpu(lh
->len
);
3956 if (len
!= skb
->len
) {
3961 BT_DBG("len %d, cid 0x%4.4x", len
, cid
);
3964 case L2CAP_CID_LE_SIGNALING
:
3965 case L2CAP_CID_SIGNALING
:
3966 l2cap_sig_channel(conn
, skb
);
3969 case L2CAP_CID_CONN_LESS
:
3970 psm
= get_unaligned_le16(skb
->data
);
3972 l2cap_conless_channel(conn
, psm
, skb
);
3975 case L2CAP_CID_LE_DATA
:
3976 l2cap_att_channel(conn
, cid
, skb
);
3980 if (smp_sig_channel(conn
, skb
))
3981 l2cap_conn_del(conn
->hcon
, EACCES
);
3985 l2cap_data_channel(conn
, cid
, skb
);
3990 /* ---- L2CAP interface with lower layer (HCI) ---- */
3992 static int l2cap_connect_ind(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
3994 int exact
= 0, lm1
= 0, lm2
= 0;
3995 struct l2cap_chan
*c
;
3997 if (type
!= ACL_LINK
)
4000 BT_DBG("hdev %s, bdaddr %s", hdev
->name
, batostr(bdaddr
));
4002 /* Find listening sockets and check their link_mode */
4003 read_lock(&chan_list_lock
);
4004 list_for_each_entry(c
, &chan_list
, global_l
) {
4005 struct sock
*sk
= c
->sk
;
4007 if (c
->state
!= BT_LISTEN
)
4010 if (!bacmp(&bt_sk(sk
)->src
, &hdev
->bdaddr
)) {
4011 lm1
|= HCI_LM_ACCEPT
;
4012 if (test_bit(FLAG_ROLE_SWITCH
, &c
->flags
))
4013 lm1
|= HCI_LM_MASTER
;
4015 } else if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
)) {
4016 lm2
|= HCI_LM_ACCEPT
;
4017 if (test_bit(FLAG_ROLE_SWITCH
, &c
->flags
))
4018 lm2
|= HCI_LM_MASTER
;
4021 read_unlock(&chan_list_lock
);
4023 return exact
? lm1
: lm2
;
4026 static int l2cap_connect_cfm(struct hci_conn
*hcon
, u8 status
)
4028 struct l2cap_conn
*conn
;
4030 BT_DBG("hcon %p bdaddr %s status %d", hcon
, batostr(&hcon
->dst
), status
);
4032 if (!(hcon
->type
== ACL_LINK
|| hcon
->type
== LE_LINK
))
4036 conn
= l2cap_conn_add(hcon
, status
);
4038 l2cap_conn_ready(conn
);
4040 l2cap_conn_del(hcon
, bt_to_errno(status
));
4045 static int l2cap_disconn_ind(struct hci_conn
*hcon
)
4047 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
4049 BT_DBG("hcon %p", hcon
);
4051 if ((hcon
->type
!= ACL_LINK
&& hcon
->type
!= LE_LINK
) || !conn
)
4054 return conn
->disc_reason
;
4057 static int l2cap_disconn_cfm(struct hci_conn
*hcon
, u8 reason
)
4059 BT_DBG("hcon %p reason %d", hcon
, reason
);
4061 if (!(hcon
->type
== ACL_LINK
|| hcon
->type
== LE_LINK
))
4064 l2cap_conn_del(hcon
, bt_to_errno(reason
));
4069 static inline void l2cap_check_encryption(struct l2cap_chan
*chan
, u8 encrypt
)
4071 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
)
4074 if (encrypt
== 0x00) {
4075 if (chan
->sec_level
== BT_SECURITY_MEDIUM
) {
4076 __clear_chan_timer(chan
);
4077 __set_chan_timer(chan
, HZ
* 5);
4078 } else if (chan
->sec_level
== BT_SECURITY_HIGH
)
4079 l2cap_chan_close(chan
, ECONNREFUSED
);
4081 if (chan
->sec_level
== BT_SECURITY_MEDIUM
)
4082 __clear_chan_timer(chan
);
4086 static int l2cap_security_cfm(struct hci_conn
*hcon
, u8 status
, u8 encrypt
)
4088 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
4089 struct l2cap_chan
*chan
;
4094 BT_DBG("conn %p", conn
);
4096 if (hcon
->type
== LE_LINK
) {
4097 smp_distribute_keys(conn
, 0);
4098 del_timer(&conn
->security_timer
);
4101 read_lock(&conn
->chan_lock
);
4103 list_for_each_entry(chan
, &conn
->chan_l
, list
) {
4104 struct sock
*sk
= chan
->sk
;
4108 BT_DBG("chan->scid %d", chan
->scid
);
4110 if (chan
->scid
== L2CAP_CID_LE_DATA
) {
4111 if (!status
&& encrypt
) {
4112 chan
->sec_level
= hcon
->sec_level
;
4113 l2cap_chan_ready(sk
);
4120 if (test_bit(CONF_CONNECT_PEND
, &chan
->conf_state
)) {
4125 if (!status
&& (chan
->state
== BT_CONNECTED
||
4126 chan
->state
== BT_CONFIG
)) {
4127 l2cap_check_encryption(chan
, encrypt
);
4132 if (chan
->state
== BT_CONNECT
) {
4134 struct l2cap_conn_req req
;
4135 req
.scid
= cpu_to_le16(chan
->scid
);
4136 req
.psm
= chan
->psm
;
4138 chan
->ident
= l2cap_get_ident(conn
);
4139 set_bit(CONF_CONNECT_PEND
, &chan
->conf_state
);
4141 l2cap_send_cmd(conn
, chan
->ident
,
4142 L2CAP_CONN_REQ
, sizeof(req
), &req
);
4144 __clear_chan_timer(chan
);
4145 __set_chan_timer(chan
, HZ
/ 10);
4147 } else if (chan
->state
== BT_CONNECT2
) {
4148 struct l2cap_conn_rsp rsp
;
4152 if (bt_sk(sk
)->defer_setup
) {
4153 struct sock
*parent
= bt_sk(sk
)->parent
;
4154 res
= L2CAP_CR_PEND
;
4155 stat
= L2CAP_CS_AUTHOR_PEND
;
4157 parent
->sk_data_ready(parent
, 0);
4159 l2cap_state_change(chan
, BT_CONFIG
);
4160 res
= L2CAP_CR_SUCCESS
;
4161 stat
= L2CAP_CS_NO_INFO
;
4164 l2cap_state_change(chan
, BT_DISCONN
);
4165 __set_chan_timer(chan
, HZ
/ 10);
4166 res
= L2CAP_CR_SEC_BLOCK
;
4167 stat
= L2CAP_CS_NO_INFO
;
4170 rsp
.scid
= cpu_to_le16(chan
->dcid
);
4171 rsp
.dcid
= cpu_to_le16(chan
->scid
);
4172 rsp
.result
= cpu_to_le16(res
);
4173 rsp
.status
= cpu_to_le16(stat
);
4174 l2cap_send_cmd(conn
, chan
->ident
, L2CAP_CONN_RSP
,
4181 read_unlock(&conn
->chan_lock
);
4186 static int l2cap_recv_acldata(struct hci_conn
*hcon
, struct sk_buff
*skb
, u16 flags
)
4188 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
4191 conn
= l2cap_conn_add(hcon
, 0);
4196 BT_DBG("conn %p len %d flags 0x%x", conn
, skb
->len
, flags
);
4198 if (!(flags
& ACL_CONT
)) {
4199 struct l2cap_hdr
*hdr
;
4200 struct l2cap_chan
*chan
;
4205 BT_ERR("Unexpected start frame (len %d)", skb
->len
);
4206 kfree_skb(conn
->rx_skb
);
4207 conn
->rx_skb
= NULL
;
4209 l2cap_conn_unreliable(conn
, ECOMM
);
4212 /* Start fragment always begin with Basic L2CAP header */
4213 if (skb
->len
< L2CAP_HDR_SIZE
) {
4214 BT_ERR("Frame is too short (len %d)", skb
->len
);
4215 l2cap_conn_unreliable(conn
, ECOMM
);
4219 hdr
= (struct l2cap_hdr
*) skb
->data
;
4220 len
= __le16_to_cpu(hdr
->len
) + L2CAP_HDR_SIZE
;
4221 cid
= __le16_to_cpu(hdr
->cid
);
4223 if (len
== skb
->len
) {
4224 /* Complete frame received */
4225 l2cap_recv_frame(conn
, skb
);
4229 BT_DBG("Start: total len %d, frag len %d", len
, skb
->len
);
4231 if (skb
->len
> len
) {
4232 BT_ERR("Frame is too long (len %d, expected len %d)",
4234 l2cap_conn_unreliable(conn
, ECOMM
);
4238 chan
= l2cap_get_chan_by_scid(conn
, cid
);
4240 if (chan
&& chan
->sk
) {
4241 struct sock
*sk
= chan
->sk
;
4243 if (chan
->imtu
< len
- L2CAP_HDR_SIZE
) {
4244 BT_ERR("Frame exceeding recv MTU (len %d, "
4248 l2cap_conn_unreliable(conn
, ECOMM
);
4254 /* Allocate skb for the complete frame (with header) */
4255 conn
->rx_skb
= bt_skb_alloc(len
, GFP_ATOMIC
);
4259 skb_copy_from_linear_data(skb
, skb_put(conn
->rx_skb
, skb
->len
),
4261 conn
->rx_len
= len
- skb
->len
;
4263 BT_DBG("Cont: frag len %d (expecting %d)", skb
->len
, conn
->rx_len
);
4265 if (!conn
->rx_len
) {
4266 BT_ERR("Unexpected continuation frame (len %d)", skb
->len
);
4267 l2cap_conn_unreliable(conn
, ECOMM
);
4271 if (skb
->len
> conn
->rx_len
) {
4272 BT_ERR("Fragment is too long (len %d, expected %d)",
4273 skb
->len
, conn
->rx_len
);
4274 kfree_skb(conn
->rx_skb
);
4275 conn
->rx_skb
= NULL
;
4277 l2cap_conn_unreliable(conn
, ECOMM
);
4281 skb_copy_from_linear_data(skb
, skb_put(conn
->rx_skb
, skb
->len
),
4283 conn
->rx_len
-= skb
->len
;
4285 if (!conn
->rx_len
) {
4286 /* Complete frame received */
4287 l2cap_recv_frame(conn
, conn
->rx_skb
);
4288 conn
->rx_skb
= NULL
;
4297 static int l2cap_debugfs_show(struct seq_file
*f
, void *p
)
4299 struct l2cap_chan
*c
;
4301 read_lock_bh(&chan_list_lock
);
4303 list_for_each_entry(c
, &chan_list
, global_l
) {
4304 struct sock
*sk
= c
->sk
;
4306 seq_printf(f
, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
4307 batostr(&bt_sk(sk
)->src
),
4308 batostr(&bt_sk(sk
)->dst
),
4309 c
->state
, __le16_to_cpu(c
->psm
),
4310 c
->scid
, c
->dcid
, c
->imtu
, c
->omtu
,
4311 c
->sec_level
, c
->mode
);
4314 read_unlock_bh(&chan_list_lock
);
4319 static int l2cap_debugfs_open(struct inode
*inode
, struct file
*file
)
4321 return single_open(file
, l2cap_debugfs_show
, inode
->i_private
);
4324 static const struct file_operations l2cap_debugfs_fops
= {
4325 .open
= l2cap_debugfs_open
,
4327 .llseek
= seq_lseek
,
4328 .release
= single_release
,
4331 static struct dentry
*l2cap_debugfs
;
4333 static struct hci_proto l2cap_hci_proto
= {
4335 .id
= HCI_PROTO_L2CAP
,
4336 .connect_ind
= l2cap_connect_ind
,
4337 .connect_cfm
= l2cap_connect_cfm
,
4338 .disconn_ind
= l2cap_disconn_ind
,
4339 .disconn_cfm
= l2cap_disconn_cfm
,
4340 .security_cfm
= l2cap_security_cfm
,
4341 .recv_acldata
= l2cap_recv_acldata
4344 int __init
l2cap_init(void)
4348 err
= l2cap_init_sockets();
4352 err
= hci_register_proto(&l2cap_hci_proto
);
4354 BT_ERR("L2CAP protocol registration failed");
4355 bt_sock_unregister(BTPROTO_L2CAP
);
4360 l2cap_debugfs
= debugfs_create_file("l2cap", 0444,
4361 bt_debugfs
, NULL
, &l2cap_debugfs_fops
);
4363 BT_ERR("Failed to create L2CAP debug file");
4369 l2cap_cleanup_sockets();
4373 void l2cap_exit(void)
4375 debugfs_remove(l2cap_debugfs
);
4377 if (hci_unregister_proto(&l2cap_hci_proto
) < 0)
4378 BT_ERR("L2CAP protocol unregistration failed");
4380 l2cap_cleanup_sockets();
4383 module_param(disable_ertm
, bool, 0644);
4384 MODULE_PARM_DESC(disable_ertm
, "Disable enhanced retransmission mode");
4386 module_param(enable_hs
, bool, 0644);
4387 MODULE_PARM_DESC(enable_hs
, "Enable High Speed");