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 and sockets. */
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>
58 #define VERSION "2.15"
60 static int disable_ertm
;
62 static u32 l2cap_feat_mask
= L2CAP_FEAT_FIXED_CHAN
;
63 static u8 l2cap_fixed_chan
[8] = { 0x02, };
65 static const struct proto_ops l2cap_sock_ops
;
67 static struct workqueue_struct
*_busy_wq
;
69 static struct bt_sock_list l2cap_sk_list
= {
70 .lock
= __RW_LOCK_UNLOCKED(l2cap_sk_list
.lock
)
73 static void l2cap_busy_work(struct work_struct
*work
);
75 static void __l2cap_sock_close(struct sock
*sk
, int reason
);
76 static void l2cap_sock_close(struct sock
*sk
);
77 static void l2cap_sock_kill(struct sock
*sk
);
79 static int l2cap_build_conf_req(struct sock
*sk
, void *data
);
80 static struct sk_buff
*l2cap_build_cmd(struct l2cap_conn
*conn
,
81 u8 code
, u8 ident
, u16 dlen
, void *data
);
83 static int l2cap_ertm_data_rcv(struct sock
*sk
, struct sk_buff
*skb
);
85 /* ---- L2CAP timers ---- */
86 static void l2cap_sock_set_timer(struct sock
*sk
, long timeout
)
88 BT_DBG("sk %p state %d timeout %ld", sk
, sk
->sk_state
, timeout
);
89 sk_reset_timer(sk
, &sk
->sk_timer
, jiffies
+ timeout
);
92 static void l2cap_sock_clear_timer(struct sock
*sk
)
94 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
95 sk_stop_timer(sk
, &sk
->sk_timer
);
98 static void l2cap_sock_timeout(unsigned long arg
)
100 struct sock
*sk
= (struct sock
*) arg
;
103 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
107 if (sock_owned_by_user(sk
)) {
108 /* sk is owned by user. Try again later */
109 l2cap_sock_set_timer(sk
, HZ
/ 5);
115 if (sk
->sk_state
== BT_CONNECTED
|| sk
->sk_state
== BT_CONFIG
)
116 reason
= ECONNREFUSED
;
117 else if (sk
->sk_state
== BT_CONNECT
&&
118 l2cap_pi(sk
)->sec_level
!= BT_SECURITY_SDP
)
119 reason
= ECONNREFUSED
;
123 __l2cap_sock_close(sk
, reason
);
131 /* ---- L2CAP channels ---- */
132 static struct sock
*__l2cap_get_chan_by_dcid(struct l2cap_chan_list
*l
, u16 cid
)
135 for (s
= l
->head
; s
; s
= l2cap_pi(s
)->next_c
) {
136 if (l2cap_pi(s
)->dcid
== cid
)
142 static struct sock
*__l2cap_get_chan_by_scid(struct l2cap_chan_list
*l
, u16 cid
)
145 for (s
= l
->head
; s
; s
= l2cap_pi(s
)->next_c
) {
146 if (l2cap_pi(s
)->scid
== cid
)
152 /* Find channel with given SCID.
153 * Returns locked socket */
154 static inline struct sock
*l2cap_get_chan_by_scid(struct l2cap_chan_list
*l
, u16 cid
)
158 s
= __l2cap_get_chan_by_scid(l
, cid
);
161 read_unlock(&l
->lock
);
165 static struct sock
*__l2cap_get_chan_by_ident(struct l2cap_chan_list
*l
, u8 ident
)
168 for (s
= l
->head
; s
; s
= l2cap_pi(s
)->next_c
) {
169 if (l2cap_pi(s
)->ident
== ident
)
175 static inline struct sock
*l2cap_get_chan_by_ident(struct l2cap_chan_list
*l
, u8 ident
)
179 s
= __l2cap_get_chan_by_ident(l
, ident
);
182 read_unlock(&l
->lock
);
186 static u16
l2cap_alloc_cid(struct l2cap_chan_list
*l
)
188 u16 cid
= L2CAP_CID_DYN_START
;
190 for (; cid
< L2CAP_CID_DYN_END
; cid
++) {
191 if (!__l2cap_get_chan_by_scid(l
, cid
))
198 static inline void __l2cap_chan_link(struct l2cap_chan_list
*l
, struct sock
*sk
)
203 l2cap_pi(l
->head
)->prev_c
= sk
;
205 l2cap_pi(sk
)->next_c
= l
->head
;
206 l2cap_pi(sk
)->prev_c
= NULL
;
210 static inline void l2cap_chan_unlink(struct l2cap_chan_list
*l
, struct sock
*sk
)
212 struct sock
*next
= l2cap_pi(sk
)->next_c
, *prev
= l2cap_pi(sk
)->prev_c
;
214 write_lock_bh(&l
->lock
);
219 l2cap_pi(next
)->prev_c
= prev
;
221 l2cap_pi(prev
)->next_c
= next
;
222 write_unlock_bh(&l
->lock
);
227 static void __l2cap_chan_add(struct l2cap_conn
*conn
, struct sock
*sk
, struct sock
*parent
)
229 struct l2cap_chan_list
*l
= &conn
->chan_list
;
231 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn
,
232 l2cap_pi(sk
)->psm
, l2cap_pi(sk
)->dcid
);
234 conn
->disc_reason
= 0x13;
236 l2cap_pi(sk
)->conn
= conn
;
238 if (sk
->sk_type
== SOCK_SEQPACKET
|| sk
->sk_type
== SOCK_STREAM
) {
239 /* Alloc CID for connection-oriented socket */
240 l2cap_pi(sk
)->scid
= l2cap_alloc_cid(l
);
241 } else if (sk
->sk_type
== SOCK_DGRAM
) {
242 /* Connectionless socket */
243 l2cap_pi(sk
)->scid
= L2CAP_CID_CONN_LESS
;
244 l2cap_pi(sk
)->dcid
= L2CAP_CID_CONN_LESS
;
245 l2cap_pi(sk
)->omtu
= L2CAP_DEFAULT_MTU
;
247 /* Raw socket can send/recv signalling messages only */
248 l2cap_pi(sk
)->scid
= L2CAP_CID_SIGNALING
;
249 l2cap_pi(sk
)->dcid
= L2CAP_CID_SIGNALING
;
250 l2cap_pi(sk
)->omtu
= L2CAP_DEFAULT_MTU
;
253 __l2cap_chan_link(l
, sk
);
256 bt_accept_enqueue(parent
, sk
);
260 * Must be called on the locked socket. */
261 static void l2cap_chan_del(struct sock
*sk
, int err
)
263 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
264 struct sock
*parent
= bt_sk(sk
)->parent
;
266 l2cap_sock_clear_timer(sk
);
268 BT_DBG("sk %p, conn %p, err %d", sk
, conn
, err
);
271 /* Unlink from channel list */
272 l2cap_chan_unlink(&conn
->chan_list
, sk
);
273 l2cap_pi(sk
)->conn
= NULL
;
274 hci_conn_put(conn
->hcon
);
277 sk
->sk_state
= BT_CLOSED
;
278 sock_set_flag(sk
, SOCK_ZAPPED
);
284 bt_accept_unlink(sk
);
285 parent
->sk_data_ready(parent
, 0);
287 sk
->sk_state_change(sk
);
289 skb_queue_purge(TX_QUEUE(sk
));
291 if (l2cap_pi(sk
)->mode
== L2CAP_MODE_ERTM
) {
292 struct srej_list
*l
, *tmp
;
294 del_timer(&l2cap_pi(sk
)->retrans_timer
);
295 del_timer(&l2cap_pi(sk
)->monitor_timer
);
296 del_timer(&l2cap_pi(sk
)->ack_timer
);
298 skb_queue_purge(SREJ_QUEUE(sk
));
299 skb_queue_purge(BUSY_QUEUE(sk
));
301 list_for_each_entry_safe(l
, tmp
, SREJ_LIST(sk
), list
) {
308 static inline u8
l2cap_get_auth_type(struct sock
*sk
)
310 if (sk
->sk_type
== SOCK_RAW
) {
311 switch (l2cap_pi(sk
)->sec_level
) {
312 case BT_SECURITY_HIGH
:
313 return HCI_AT_DEDICATED_BONDING_MITM
;
314 case BT_SECURITY_MEDIUM
:
315 return HCI_AT_DEDICATED_BONDING
;
317 return HCI_AT_NO_BONDING
;
319 } else if (l2cap_pi(sk
)->psm
== cpu_to_le16(0x0001)) {
320 if (l2cap_pi(sk
)->sec_level
== BT_SECURITY_LOW
)
321 l2cap_pi(sk
)->sec_level
= BT_SECURITY_SDP
;
323 if (l2cap_pi(sk
)->sec_level
== BT_SECURITY_HIGH
)
324 return HCI_AT_NO_BONDING_MITM
;
326 return HCI_AT_NO_BONDING
;
328 switch (l2cap_pi(sk
)->sec_level
) {
329 case BT_SECURITY_HIGH
:
330 return HCI_AT_GENERAL_BONDING_MITM
;
331 case BT_SECURITY_MEDIUM
:
332 return HCI_AT_GENERAL_BONDING
;
334 return HCI_AT_NO_BONDING
;
339 /* Service level security */
340 static inline int l2cap_check_security(struct sock
*sk
)
342 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
345 auth_type
= l2cap_get_auth_type(sk
);
347 return hci_conn_security(conn
->hcon
, l2cap_pi(sk
)->sec_level
,
351 static inline u8
l2cap_get_ident(struct l2cap_conn
*conn
)
355 /* Get next available identificator.
356 * 1 - 128 are used by kernel.
357 * 129 - 199 are reserved.
358 * 200 - 254 are used by utilities like l2ping, etc.
361 spin_lock_bh(&conn
->lock
);
363 if (++conn
->tx_ident
> 128)
368 spin_unlock_bh(&conn
->lock
);
373 static inline void l2cap_send_cmd(struct l2cap_conn
*conn
, u8 ident
, u8 code
, u16 len
, void *data
)
375 struct sk_buff
*skb
= l2cap_build_cmd(conn
, code
, ident
, len
, data
);
377 BT_DBG("code 0x%2.2x", code
);
382 hci_send_acl(conn
->hcon
, skb
, 0);
385 static inline void l2cap_send_sframe(struct l2cap_pinfo
*pi
, u16 control
)
388 struct l2cap_hdr
*lh
;
389 struct l2cap_conn
*conn
= pi
->conn
;
390 struct sock
*sk
= (struct sock
*)pi
;
391 int count
, hlen
= L2CAP_HDR_SIZE
+ 2;
393 if (sk
->sk_state
!= BT_CONNECTED
)
396 if (pi
->fcs
== L2CAP_FCS_CRC16
)
399 BT_DBG("pi %p, control 0x%2.2x", pi
, control
);
401 count
= min_t(unsigned int, conn
->mtu
, hlen
);
402 control
|= L2CAP_CTRL_FRAME_TYPE
;
404 if (pi
->conn_state
& L2CAP_CONN_SEND_FBIT
) {
405 control
|= L2CAP_CTRL_FINAL
;
406 pi
->conn_state
&= ~L2CAP_CONN_SEND_FBIT
;
409 if (pi
->conn_state
& L2CAP_CONN_SEND_PBIT
) {
410 control
|= L2CAP_CTRL_POLL
;
411 pi
->conn_state
&= ~L2CAP_CONN_SEND_PBIT
;
414 skb
= bt_skb_alloc(count
, GFP_ATOMIC
);
418 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
419 lh
->len
= cpu_to_le16(hlen
- L2CAP_HDR_SIZE
);
420 lh
->cid
= cpu_to_le16(pi
->dcid
);
421 put_unaligned_le16(control
, skb_put(skb
, 2));
423 if (pi
->fcs
== L2CAP_FCS_CRC16
) {
424 u16 fcs
= crc16(0, (u8
*)lh
, count
- 2);
425 put_unaligned_le16(fcs
, skb_put(skb
, 2));
428 hci_send_acl(pi
->conn
->hcon
, skb
, 0);
431 static inline void l2cap_send_rr_or_rnr(struct l2cap_pinfo
*pi
, u16 control
)
433 if (pi
->conn_state
& L2CAP_CONN_LOCAL_BUSY
) {
434 control
|= L2CAP_SUPER_RCV_NOT_READY
;
435 pi
->conn_state
|= L2CAP_CONN_RNR_SENT
;
437 control
|= L2CAP_SUPER_RCV_READY
;
439 control
|= pi
->buffer_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
;
441 l2cap_send_sframe(pi
, control
);
444 static inline int __l2cap_no_conn_pending(struct sock
*sk
)
446 return !(l2cap_pi(sk
)->conf_state
& L2CAP_CONF_CONNECT_PEND
);
449 static void l2cap_do_start(struct sock
*sk
)
451 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
453 if (conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_SENT
) {
454 if (!(conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_DONE
))
457 if (l2cap_check_security(sk
) && __l2cap_no_conn_pending(sk
)) {
458 struct l2cap_conn_req req
;
459 req
.scid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
460 req
.psm
= l2cap_pi(sk
)->psm
;
462 l2cap_pi(sk
)->ident
= l2cap_get_ident(conn
);
463 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_CONNECT_PEND
;
465 l2cap_send_cmd(conn
, l2cap_pi(sk
)->ident
,
466 L2CAP_CONN_REQ
, sizeof(req
), &req
);
469 struct l2cap_info_req req
;
470 req
.type
= cpu_to_le16(L2CAP_IT_FEAT_MASK
);
472 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_SENT
;
473 conn
->info_ident
= l2cap_get_ident(conn
);
475 mod_timer(&conn
->info_timer
, jiffies
+
476 msecs_to_jiffies(L2CAP_INFO_TIMEOUT
));
478 l2cap_send_cmd(conn
, conn
->info_ident
,
479 L2CAP_INFO_REQ
, sizeof(req
), &req
);
483 static inline int l2cap_mode_supported(__u8 mode
, __u32 feat_mask
)
485 u32 local_feat_mask
= l2cap_feat_mask
;
487 local_feat_mask
|= L2CAP_FEAT_ERTM
| L2CAP_FEAT_STREAMING
;
490 case L2CAP_MODE_ERTM
:
491 return L2CAP_FEAT_ERTM
& feat_mask
& local_feat_mask
;
492 case L2CAP_MODE_STREAMING
:
493 return L2CAP_FEAT_STREAMING
& feat_mask
& local_feat_mask
;
499 static void l2cap_send_disconn_req(struct l2cap_conn
*conn
, struct sock
*sk
, int err
)
501 struct l2cap_disconn_req req
;
506 skb_queue_purge(TX_QUEUE(sk
));
508 if (l2cap_pi(sk
)->mode
== L2CAP_MODE_ERTM
) {
509 del_timer(&l2cap_pi(sk
)->retrans_timer
);
510 del_timer(&l2cap_pi(sk
)->monitor_timer
);
511 del_timer(&l2cap_pi(sk
)->ack_timer
);
514 req
.dcid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
515 req
.scid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
516 l2cap_send_cmd(conn
, l2cap_get_ident(conn
),
517 L2CAP_DISCONN_REQ
, sizeof(req
), &req
);
519 sk
->sk_state
= BT_DISCONN
;
523 /* ---- L2CAP connections ---- */
524 static void l2cap_conn_start(struct l2cap_conn
*conn
)
526 struct l2cap_chan_list
*l
= &conn
->chan_list
;
527 struct sock_del_list del
, *tmp1
, *tmp2
;
530 BT_DBG("conn %p", conn
);
532 INIT_LIST_HEAD(&del
.list
);
536 for (sk
= l
->head
; sk
; sk
= l2cap_pi(sk
)->next_c
) {
539 if (sk
->sk_type
!= SOCK_SEQPACKET
&&
540 sk
->sk_type
!= SOCK_STREAM
) {
545 if (sk
->sk_state
== BT_CONNECT
) {
546 struct l2cap_conn_req req
;
548 if (!l2cap_check_security(sk
) ||
549 !__l2cap_no_conn_pending(sk
)) {
554 if (!l2cap_mode_supported(l2cap_pi(sk
)->mode
,
556 && l2cap_pi(sk
)->conf_state
&
557 L2CAP_CONF_STATE2_DEVICE
) {
558 tmp1
= kzalloc(sizeof(struct sock_del_list
),
561 list_add_tail(&tmp1
->list
, &del
.list
);
566 req
.scid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
567 req
.psm
= l2cap_pi(sk
)->psm
;
569 l2cap_pi(sk
)->ident
= l2cap_get_ident(conn
);
570 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_CONNECT_PEND
;
572 l2cap_send_cmd(conn
, l2cap_pi(sk
)->ident
,
573 L2CAP_CONN_REQ
, sizeof(req
), &req
);
575 } else if (sk
->sk_state
== BT_CONNECT2
) {
576 struct l2cap_conn_rsp rsp
;
578 rsp
.scid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
579 rsp
.dcid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
581 if (l2cap_check_security(sk
)) {
582 if (bt_sk(sk
)->defer_setup
) {
583 struct sock
*parent
= bt_sk(sk
)->parent
;
584 rsp
.result
= cpu_to_le16(L2CAP_CR_PEND
);
585 rsp
.status
= cpu_to_le16(L2CAP_CS_AUTHOR_PEND
);
586 parent
->sk_data_ready(parent
, 0);
589 sk
->sk_state
= BT_CONFIG
;
590 rsp
.result
= cpu_to_le16(L2CAP_CR_SUCCESS
);
591 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
594 rsp
.result
= cpu_to_le16(L2CAP_CR_PEND
);
595 rsp
.status
= cpu_to_le16(L2CAP_CS_AUTHEN_PEND
);
598 l2cap_send_cmd(conn
, l2cap_pi(sk
)->ident
,
599 L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
601 if (l2cap_pi(sk
)->conf_state
& L2CAP_CONF_REQ_SENT
||
602 rsp
.result
!= L2CAP_CR_SUCCESS
) {
607 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_REQ_SENT
;
608 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
609 l2cap_build_conf_req(sk
, buf
), buf
);
610 l2cap_pi(sk
)->num_conf_req
++;
616 read_unlock(&l
->lock
);
618 list_for_each_entry_safe(tmp1
, tmp2
, &del
.list
, list
) {
619 bh_lock_sock(tmp1
->sk
);
620 __l2cap_sock_close(tmp1
->sk
, ECONNRESET
);
621 bh_unlock_sock(tmp1
->sk
);
622 list_del(&tmp1
->list
);
627 static void l2cap_conn_ready(struct l2cap_conn
*conn
)
629 struct l2cap_chan_list
*l
= &conn
->chan_list
;
632 BT_DBG("conn %p", conn
);
636 for (sk
= l
->head
; sk
; sk
= l2cap_pi(sk
)->next_c
) {
639 if (sk
->sk_type
!= SOCK_SEQPACKET
&&
640 sk
->sk_type
!= SOCK_STREAM
) {
641 l2cap_sock_clear_timer(sk
);
642 sk
->sk_state
= BT_CONNECTED
;
643 sk
->sk_state_change(sk
);
644 } else if (sk
->sk_state
== BT_CONNECT
)
650 read_unlock(&l
->lock
);
653 /* Notify sockets that we cannot guaranty reliability anymore */
654 static void l2cap_conn_unreliable(struct l2cap_conn
*conn
, int err
)
656 struct l2cap_chan_list
*l
= &conn
->chan_list
;
659 BT_DBG("conn %p", conn
);
663 for (sk
= l
->head
; sk
; sk
= l2cap_pi(sk
)->next_c
) {
664 if (l2cap_pi(sk
)->force_reliable
)
668 read_unlock(&l
->lock
);
671 static void l2cap_info_timeout(unsigned long arg
)
673 struct l2cap_conn
*conn
= (void *) arg
;
675 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
676 conn
->info_ident
= 0;
678 l2cap_conn_start(conn
);
681 static struct l2cap_conn
*l2cap_conn_add(struct hci_conn
*hcon
, u8 status
)
683 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
688 conn
= kzalloc(sizeof(struct l2cap_conn
), GFP_ATOMIC
);
692 hcon
->l2cap_data
= conn
;
695 BT_DBG("hcon %p conn %p", hcon
, conn
);
697 conn
->mtu
= hcon
->hdev
->acl_mtu
;
698 conn
->src
= &hcon
->hdev
->bdaddr
;
699 conn
->dst
= &hcon
->dst
;
703 spin_lock_init(&conn
->lock
);
704 rwlock_init(&conn
->chan_list
.lock
);
706 setup_timer(&conn
->info_timer
, l2cap_info_timeout
,
707 (unsigned long) conn
);
709 conn
->disc_reason
= 0x13;
714 static void l2cap_conn_del(struct hci_conn
*hcon
, int err
)
716 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
722 BT_DBG("hcon %p conn %p, err %d", hcon
, conn
, err
);
724 kfree_skb(conn
->rx_skb
);
727 while ((sk
= conn
->chan_list
.head
)) {
729 l2cap_chan_del(sk
, err
);
734 if (conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_SENT
)
735 del_timer_sync(&conn
->info_timer
);
737 hcon
->l2cap_data
= NULL
;
741 static inline void l2cap_chan_add(struct l2cap_conn
*conn
, struct sock
*sk
, struct sock
*parent
)
743 struct l2cap_chan_list
*l
= &conn
->chan_list
;
744 write_lock_bh(&l
->lock
);
745 __l2cap_chan_add(conn
, sk
, parent
);
746 write_unlock_bh(&l
->lock
);
749 /* ---- Socket interface ---- */
750 static struct sock
*__l2cap_get_sock_by_addr(__le16 psm
, bdaddr_t
*src
)
753 struct hlist_node
*node
;
754 sk_for_each(sk
, node
, &l2cap_sk_list
.head
)
755 if (l2cap_pi(sk
)->sport
== psm
&& !bacmp(&bt_sk(sk
)->src
, src
))
762 /* Find socket with psm and source bdaddr.
763 * Returns closest match.
765 static struct sock
*l2cap_get_sock_by_psm(int state
, __le16 psm
, bdaddr_t
*src
)
767 struct sock
*sk
= NULL
, *sk1
= NULL
;
768 struct hlist_node
*node
;
770 read_lock(&l2cap_sk_list
.lock
);
772 sk_for_each(sk
, node
, &l2cap_sk_list
.head
) {
773 if (state
&& sk
->sk_state
!= state
)
776 if (l2cap_pi(sk
)->psm
== psm
) {
778 if (!bacmp(&bt_sk(sk
)->src
, src
))
782 if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
))
787 read_unlock(&l2cap_sk_list
.lock
);
789 return node
? sk
: sk1
;
792 static void l2cap_sock_destruct(struct sock
*sk
)
796 skb_queue_purge(&sk
->sk_receive_queue
);
797 skb_queue_purge(&sk
->sk_write_queue
);
800 static void l2cap_sock_cleanup_listen(struct sock
*parent
)
804 BT_DBG("parent %p", parent
);
806 /* Close not yet accepted channels */
807 while ((sk
= bt_accept_dequeue(parent
, NULL
)))
808 l2cap_sock_close(sk
);
810 parent
->sk_state
= BT_CLOSED
;
811 sock_set_flag(parent
, SOCK_ZAPPED
);
814 /* Kill socket (only if zapped and orphan)
815 * Must be called on unlocked socket.
817 static void l2cap_sock_kill(struct sock
*sk
)
819 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
822 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
824 /* Kill poor orphan */
825 bt_sock_unlink(&l2cap_sk_list
, sk
);
826 sock_set_flag(sk
, SOCK_DEAD
);
830 static void __l2cap_sock_close(struct sock
*sk
, int reason
)
832 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
834 switch (sk
->sk_state
) {
836 l2cap_sock_cleanup_listen(sk
);
841 if (sk
->sk_type
== SOCK_SEQPACKET
||
842 sk
->sk_type
== SOCK_STREAM
) {
843 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
845 l2cap_sock_set_timer(sk
, sk
->sk_sndtimeo
);
846 l2cap_send_disconn_req(conn
, sk
, reason
);
848 l2cap_chan_del(sk
, reason
);
852 if (sk
->sk_type
== SOCK_SEQPACKET
||
853 sk
->sk_type
== SOCK_STREAM
) {
854 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
855 struct l2cap_conn_rsp rsp
;
858 if (bt_sk(sk
)->defer_setup
)
859 result
= L2CAP_CR_SEC_BLOCK
;
861 result
= L2CAP_CR_BAD_PSM
;
863 rsp
.scid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
864 rsp
.dcid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
865 rsp
.result
= cpu_to_le16(result
);
866 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
867 l2cap_send_cmd(conn
, l2cap_pi(sk
)->ident
,
868 L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
870 l2cap_chan_del(sk
, reason
);
875 l2cap_chan_del(sk
, reason
);
879 sock_set_flag(sk
, SOCK_ZAPPED
);
884 /* Must be called on unlocked socket. */
885 static void l2cap_sock_close(struct sock
*sk
)
887 l2cap_sock_clear_timer(sk
);
889 __l2cap_sock_close(sk
, ECONNRESET
);
894 static void l2cap_sock_init(struct sock
*sk
, struct sock
*parent
)
896 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
901 sk
->sk_type
= parent
->sk_type
;
902 bt_sk(sk
)->defer_setup
= bt_sk(parent
)->defer_setup
;
904 pi
->imtu
= l2cap_pi(parent
)->imtu
;
905 pi
->omtu
= l2cap_pi(parent
)->omtu
;
906 pi
->conf_state
= l2cap_pi(parent
)->conf_state
;
907 pi
->mode
= l2cap_pi(parent
)->mode
;
908 pi
->fcs
= l2cap_pi(parent
)->fcs
;
909 pi
->max_tx
= l2cap_pi(parent
)->max_tx
;
910 pi
->tx_win
= l2cap_pi(parent
)->tx_win
;
911 pi
->sec_level
= l2cap_pi(parent
)->sec_level
;
912 pi
->role_switch
= l2cap_pi(parent
)->role_switch
;
913 pi
->force_reliable
= l2cap_pi(parent
)->force_reliable
;
915 pi
->imtu
= L2CAP_DEFAULT_MTU
;
917 if (!disable_ertm
&& sk
->sk_type
== SOCK_STREAM
) {
918 pi
->mode
= L2CAP_MODE_ERTM
;
919 pi
->conf_state
|= L2CAP_CONF_STATE2_DEVICE
;
921 pi
->mode
= L2CAP_MODE_BASIC
;
923 pi
->max_tx
= L2CAP_DEFAULT_MAX_TX
;
924 pi
->fcs
= L2CAP_FCS_CRC16
;
925 pi
->tx_win
= L2CAP_DEFAULT_TX_WINDOW
;
926 pi
->sec_level
= BT_SECURITY_LOW
;
928 pi
->force_reliable
= 0;
931 /* Default config options */
933 pi
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
934 skb_queue_head_init(TX_QUEUE(sk
));
935 skb_queue_head_init(SREJ_QUEUE(sk
));
936 skb_queue_head_init(BUSY_QUEUE(sk
));
937 INIT_LIST_HEAD(SREJ_LIST(sk
));
940 static struct proto l2cap_proto
= {
942 .owner
= THIS_MODULE
,
943 .obj_size
= sizeof(struct l2cap_pinfo
)
946 static struct sock
*l2cap_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
950 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &l2cap_proto
);
954 sock_init_data(sock
, sk
);
955 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
957 sk
->sk_destruct
= l2cap_sock_destruct
;
958 sk
->sk_sndtimeo
= msecs_to_jiffies(L2CAP_CONN_TIMEOUT
);
960 sock_reset_flag(sk
, SOCK_ZAPPED
);
962 sk
->sk_protocol
= proto
;
963 sk
->sk_state
= BT_OPEN
;
965 setup_timer(&sk
->sk_timer
, l2cap_sock_timeout
, (unsigned long) sk
);
967 bt_sock_link(&l2cap_sk_list
, sk
);
971 static int l2cap_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
976 BT_DBG("sock %p", sock
);
978 sock
->state
= SS_UNCONNECTED
;
980 if (sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
&&
981 sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
)
982 return -ESOCKTNOSUPPORT
;
984 if (sock
->type
== SOCK_RAW
&& !kern
&& !capable(CAP_NET_RAW
))
987 sock
->ops
= &l2cap_sock_ops
;
989 sk
= l2cap_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
993 l2cap_sock_init(sk
, NULL
);
997 static int l2cap_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int alen
)
999 struct sock
*sk
= sock
->sk
;
1000 struct sockaddr_l2 la
;
1003 BT_DBG("sk %p", sk
);
1005 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
1008 memset(&la
, 0, sizeof(la
));
1009 len
= min_t(unsigned int, sizeof(la
), alen
);
1010 memcpy(&la
, addr
, len
);
1017 if (sk
->sk_state
!= BT_OPEN
) {
1023 __u16 psm
= __le16_to_cpu(la
.l2_psm
);
1025 /* PSM must be odd and lsb of upper byte must be 0 */
1026 if ((psm
& 0x0101) != 0x0001) {
1031 /* Restrict usage of well-known PSMs */
1032 if (psm
< 0x1001 && !capable(CAP_NET_BIND_SERVICE
)) {
1038 write_lock_bh(&l2cap_sk_list
.lock
);
1040 if (la
.l2_psm
&& __l2cap_get_sock_by_addr(la
.l2_psm
, &la
.l2_bdaddr
)) {
1043 /* Save source address */
1044 bacpy(&bt_sk(sk
)->src
, &la
.l2_bdaddr
);
1045 l2cap_pi(sk
)->psm
= la
.l2_psm
;
1046 l2cap_pi(sk
)->sport
= la
.l2_psm
;
1047 sk
->sk_state
= BT_BOUND
;
1049 if (__le16_to_cpu(la
.l2_psm
) == 0x0001 ||
1050 __le16_to_cpu(la
.l2_psm
) == 0x0003)
1051 l2cap_pi(sk
)->sec_level
= BT_SECURITY_SDP
;
1054 write_unlock_bh(&l2cap_sk_list
.lock
);
1061 static int l2cap_do_connect(struct sock
*sk
)
1063 bdaddr_t
*src
= &bt_sk(sk
)->src
;
1064 bdaddr_t
*dst
= &bt_sk(sk
)->dst
;
1065 struct l2cap_conn
*conn
;
1066 struct hci_conn
*hcon
;
1067 struct hci_dev
*hdev
;
1071 BT_DBG("%s -> %s psm 0x%2.2x", batostr(src
), batostr(dst
),
1074 hdev
= hci_get_route(dst
, src
);
1076 return -EHOSTUNREACH
;
1078 hci_dev_lock_bh(hdev
);
1082 auth_type
= l2cap_get_auth_type(sk
);
1084 hcon
= hci_connect(hdev
, ACL_LINK
, dst
,
1085 l2cap_pi(sk
)->sec_level
, auth_type
);
1089 conn
= l2cap_conn_add(hcon
, 0);
1097 /* Update source addr of the socket */
1098 bacpy(src
, conn
->src
);
1100 l2cap_chan_add(conn
, sk
, NULL
);
1102 sk
->sk_state
= BT_CONNECT
;
1103 l2cap_sock_set_timer(sk
, sk
->sk_sndtimeo
);
1105 if (hcon
->state
== BT_CONNECTED
) {
1106 if (sk
->sk_type
!= SOCK_SEQPACKET
&&
1107 sk
->sk_type
!= SOCK_STREAM
) {
1108 l2cap_sock_clear_timer(sk
);
1109 if (l2cap_check_security(sk
))
1110 sk
->sk_state
= BT_CONNECTED
;
1116 hci_dev_unlock_bh(hdev
);
1121 static int l2cap_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
1123 struct sock
*sk
= sock
->sk
;
1124 struct sockaddr_l2 la
;
1127 BT_DBG("sk %p", sk
);
1129 if (!addr
|| alen
< sizeof(addr
->sa_family
) ||
1130 addr
->sa_family
!= AF_BLUETOOTH
)
1133 memset(&la
, 0, sizeof(la
));
1134 len
= min_t(unsigned int, sizeof(la
), alen
);
1135 memcpy(&la
, addr
, len
);
1142 if ((sk
->sk_type
== SOCK_SEQPACKET
|| sk
->sk_type
== SOCK_STREAM
)
1148 switch (l2cap_pi(sk
)->mode
) {
1149 case L2CAP_MODE_BASIC
:
1151 case L2CAP_MODE_ERTM
:
1152 case L2CAP_MODE_STREAMING
:
1161 switch (sk
->sk_state
) {
1165 /* Already connecting */
1169 /* Already connected */
1183 /* PSM must be odd and lsb of upper byte must be 0 */
1184 if ((__le16_to_cpu(la
.l2_psm
) & 0x0101) != 0x0001 &&
1185 sk
->sk_type
!= SOCK_RAW
) {
1190 /* Set destination address and psm */
1191 bacpy(&bt_sk(sk
)->dst
, &la
.l2_bdaddr
);
1192 l2cap_pi(sk
)->psm
= la
.l2_psm
;
1194 err
= l2cap_do_connect(sk
);
1199 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
1200 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
1206 static int l2cap_sock_listen(struct socket
*sock
, int backlog
)
1208 struct sock
*sk
= sock
->sk
;
1211 BT_DBG("sk %p backlog %d", sk
, backlog
);
1215 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
)
1216 || sk
->sk_state
!= BT_BOUND
) {
1221 switch (l2cap_pi(sk
)->mode
) {
1222 case L2CAP_MODE_BASIC
:
1224 case L2CAP_MODE_ERTM
:
1225 case L2CAP_MODE_STREAMING
:
1234 if (!l2cap_pi(sk
)->psm
) {
1235 bdaddr_t
*src
= &bt_sk(sk
)->src
;
1240 write_lock_bh(&l2cap_sk_list
.lock
);
1242 for (psm
= 0x1001; psm
< 0x1100; psm
+= 2)
1243 if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm
), src
)) {
1244 l2cap_pi(sk
)->psm
= cpu_to_le16(psm
);
1245 l2cap_pi(sk
)->sport
= cpu_to_le16(psm
);
1250 write_unlock_bh(&l2cap_sk_list
.lock
);
1256 sk
->sk_max_ack_backlog
= backlog
;
1257 sk
->sk_ack_backlog
= 0;
1258 sk
->sk_state
= BT_LISTEN
;
1265 static int l2cap_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1267 DECLARE_WAITQUEUE(wait
, current
);
1268 struct sock
*sk
= sock
->sk
, *nsk
;
1272 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
1274 if (sk
->sk_state
!= BT_LISTEN
) {
1279 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1281 BT_DBG("sk %p timeo %ld", sk
, timeo
);
1283 /* Wait for an incoming connection. (wake-one). */
1284 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
1285 while (!(nsk
= bt_accept_dequeue(sk
, newsock
))) {
1286 set_current_state(TASK_INTERRUPTIBLE
);
1293 timeo
= schedule_timeout(timeo
);
1294 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
1296 if (sk
->sk_state
!= BT_LISTEN
) {
1301 if (signal_pending(current
)) {
1302 err
= sock_intr_errno(timeo
);
1306 set_current_state(TASK_RUNNING
);
1307 remove_wait_queue(sk_sleep(sk
), &wait
);
1312 newsock
->state
= SS_CONNECTED
;
1314 BT_DBG("new socket %p", nsk
);
1321 static int l2cap_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
1323 struct sockaddr_l2
*la
= (struct sockaddr_l2
*) addr
;
1324 struct sock
*sk
= sock
->sk
;
1326 BT_DBG("sock %p, sk %p", sock
, sk
);
1328 addr
->sa_family
= AF_BLUETOOTH
;
1329 *len
= sizeof(struct sockaddr_l2
);
1332 la
->l2_psm
= l2cap_pi(sk
)->psm
;
1333 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->dst
);
1334 la
->l2_cid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
1336 la
->l2_psm
= l2cap_pi(sk
)->sport
;
1337 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->src
);
1338 la
->l2_cid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
1344 static int __l2cap_wait_ack(struct sock
*sk
)
1346 DECLARE_WAITQUEUE(wait
, current
);
1350 add_wait_queue(sk_sleep(sk
), &wait
);
1351 while ((l2cap_pi(sk
)->unacked_frames
> 0 && l2cap_pi(sk
)->conn
)) {
1352 set_current_state(TASK_INTERRUPTIBLE
);
1357 if (signal_pending(current
)) {
1358 err
= sock_intr_errno(timeo
);
1363 timeo
= schedule_timeout(timeo
);
1366 err
= sock_error(sk
);
1370 set_current_state(TASK_RUNNING
);
1371 remove_wait_queue(sk_sleep(sk
), &wait
);
1375 static void l2cap_monitor_timeout(unsigned long arg
)
1377 struct sock
*sk
= (void *) arg
;
1379 BT_DBG("sk %p", sk
);
1382 if (l2cap_pi(sk
)->retry_count
>= l2cap_pi(sk
)->remote_max_tx
) {
1383 l2cap_send_disconn_req(l2cap_pi(sk
)->conn
, sk
, ECONNABORTED
);
1388 l2cap_pi(sk
)->retry_count
++;
1389 __mod_monitor_timer();
1391 l2cap_send_rr_or_rnr(l2cap_pi(sk
), L2CAP_CTRL_POLL
);
1395 static void l2cap_retrans_timeout(unsigned long arg
)
1397 struct sock
*sk
= (void *) arg
;
1399 BT_DBG("sk %p", sk
);
1402 l2cap_pi(sk
)->retry_count
= 1;
1403 __mod_monitor_timer();
1405 l2cap_pi(sk
)->conn_state
|= L2CAP_CONN_WAIT_F
;
1407 l2cap_send_rr_or_rnr(l2cap_pi(sk
), L2CAP_CTRL_POLL
);
1411 static void l2cap_drop_acked_frames(struct sock
*sk
)
1413 struct sk_buff
*skb
;
1415 while ((skb
= skb_peek(TX_QUEUE(sk
))) &&
1416 l2cap_pi(sk
)->unacked_frames
) {
1417 if (bt_cb(skb
)->tx_seq
== l2cap_pi(sk
)->expected_ack_seq
)
1420 skb
= skb_dequeue(TX_QUEUE(sk
));
1423 l2cap_pi(sk
)->unacked_frames
--;
1426 if (!l2cap_pi(sk
)->unacked_frames
)
1427 del_timer(&l2cap_pi(sk
)->retrans_timer
);
1430 static inline void l2cap_do_send(struct sock
*sk
, struct sk_buff
*skb
)
1432 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1434 BT_DBG("sk %p, skb %p len %d", sk
, skb
, skb
->len
);
1436 hci_send_acl(pi
->conn
->hcon
, skb
, 0);
1439 static void l2cap_streaming_send(struct sock
*sk
)
1441 struct sk_buff
*skb
;
1442 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1445 while ((skb
= skb_dequeue(TX_QUEUE(sk
)))) {
1446 control
= get_unaligned_le16(skb
->data
+ L2CAP_HDR_SIZE
);
1447 control
|= pi
->next_tx_seq
<< L2CAP_CTRL_TXSEQ_SHIFT
;
1448 put_unaligned_le16(control
, skb
->data
+ L2CAP_HDR_SIZE
);
1450 if (pi
->fcs
== L2CAP_FCS_CRC16
) {
1451 fcs
= crc16(0, (u8
*)skb
->data
, skb
->len
- 2);
1452 put_unaligned_le16(fcs
, skb
->data
+ skb
->len
- 2);
1455 l2cap_do_send(sk
, skb
);
1457 pi
->next_tx_seq
= (pi
->next_tx_seq
+ 1) % 64;
1461 static void l2cap_retransmit_one_frame(struct sock
*sk
, u8 tx_seq
)
1463 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1464 struct sk_buff
*skb
, *tx_skb
;
1467 skb
= skb_peek(TX_QUEUE(sk
));
1472 if (bt_cb(skb
)->tx_seq
== tx_seq
)
1475 if (skb_queue_is_last(TX_QUEUE(sk
), skb
))
1478 } while ((skb
= skb_queue_next(TX_QUEUE(sk
), skb
)));
1480 if (pi
->remote_max_tx
&&
1481 bt_cb(skb
)->retries
== pi
->remote_max_tx
) {
1482 l2cap_send_disconn_req(pi
->conn
, sk
, ECONNABORTED
);
1486 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
1487 bt_cb(skb
)->retries
++;
1488 control
= get_unaligned_le16(tx_skb
->data
+ L2CAP_HDR_SIZE
);
1490 if (pi
->conn_state
& L2CAP_CONN_SEND_FBIT
) {
1491 control
|= L2CAP_CTRL_FINAL
;
1492 pi
->conn_state
&= ~L2CAP_CONN_SEND_FBIT
;
1495 control
|= (pi
->buffer_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
)
1496 | (tx_seq
<< L2CAP_CTRL_TXSEQ_SHIFT
);
1498 put_unaligned_le16(control
, tx_skb
->data
+ L2CAP_HDR_SIZE
);
1500 if (pi
->fcs
== L2CAP_FCS_CRC16
) {
1501 fcs
= crc16(0, (u8
*)tx_skb
->data
, tx_skb
->len
- 2);
1502 put_unaligned_le16(fcs
, tx_skb
->data
+ tx_skb
->len
- 2);
1505 l2cap_do_send(sk
, tx_skb
);
1508 static int l2cap_ertm_send(struct sock
*sk
)
1510 struct sk_buff
*skb
, *tx_skb
;
1511 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1515 if (sk
->sk_state
!= BT_CONNECTED
)
1518 while ((skb
= sk
->sk_send_head
) && (!l2cap_tx_window_full(sk
))) {
1520 if (pi
->remote_max_tx
&&
1521 bt_cb(skb
)->retries
== pi
->remote_max_tx
) {
1522 l2cap_send_disconn_req(pi
->conn
, sk
, ECONNABORTED
);
1526 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
1528 bt_cb(skb
)->retries
++;
1530 control
= get_unaligned_le16(tx_skb
->data
+ L2CAP_HDR_SIZE
);
1531 control
&= L2CAP_CTRL_SAR
;
1533 if (pi
->conn_state
& L2CAP_CONN_SEND_FBIT
) {
1534 control
|= L2CAP_CTRL_FINAL
;
1535 pi
->conn_state
&= ~L2CAP_CONN_SEND_FBIT
;
1537 control
|= (pi
->buffer_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
)
1538 | (pi
->next_tx_seq
<< L2CAP_CTRL_TXSEQ_SHIFT
);
1539 put_unaligned_le16(control
, tx_skb
->data
+ L2CAP_HDR_SIZE
);
1542 if (pi
->fcs
== L2CAP_FCS_CRC16
) {
1543 fcs
= crc16(0, (u8
*)skb
->data
, tx_skb
->len
- 2);
1544 put_unaligned_le16(fcs
, skb
->data
+ tx_skb
->len
- 2);
1547 l2cap_do_send(sk
, tx_skb
);
1549 __mod_retrans_timer();
1551 bt_cb(skb
)->tx_seq
= pi
->next_tx_seq
;
1552 pi
->next_tx_seq
= (pi
->next_tx_seq
+ 1) % 64;
1554 pi
->unacked_frames
++;
1557 if (skb_queue_is_last(TX_QUEUE(sk
), skb
))
1558 sk
->sk_send_head
= NULL
;
1560 sk
->sk_send_head
= skb_queue_next(TX_QUEUE(sk
), skb
);
1568 static int l2cap_retransmit_frames(struct sock
*sk
)
1570 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1573 if (!skb_queue_empty(TX_QUEUE(sk
)))
1574 sk
->sk_send_head
= TX_QUEUE(sk
)->next
;
1576 pi
->next_tx_seq
= pi
->expected_ack_seq
;
1577 ret
= l2cap_ertm_send(sk
);
1581 static void l2cap_send_ack(struct l2cap_pinfo
*pi
)
1583 struct sock
*sk
= (struct sock
*)pi
;
1586 control
|= pi
->buffer_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
;
1588 if (pi
->conn_state
& L2CAP_CONN_LOCAL_BUSY
) {
1589 control
|= L2CAP_SUPER_RCV_NOT_READY
;
1590 pi
->conn_state
|= L2CAP_CONN_RNR_SENT
;
1591 l2cap_send_sframe(pi
, control
);
1595 if (l2cap_ertm_send(sk
) > 0)
1598 control
|= L2CAP_SUPER_RCV_READY
;
1599 l2cap_send_sframe(pi
, control
);
1602 static void l2cap_send_srejtail(struct sock
*sk
)
1604 struct srej_list
*tail
;
1607 control
= L2CAP_SUPER_SELECT_REJECT
;
1608 control
|= L2CAP_CTRL_FINAL
;
1610 tail
= list_entry(SREJ_LIST(sk
)->prev
, struct srej_list
, list
);
1611 control
|= tail
->tx_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
;
1613 l2cap_send_sframe(l2cap_pi(sk
), control
);
1616 static inline int l2cap_skbuff_fromiovec(struct sock
*sk
, struct msghdr
*msg
, int len
, int count
, struct sk_buff
*skb
)
1618 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
1619 struct sk_buff
**frag
;
1622 if (memcpy_fromiovec(skb_put(skb
, count
), msg
->msg_iov
, count
))
1628 /* Continuation fragments (no L2CAP header) */
1629 frag
= &skb_shinfo(skb
)->frag_list
;
1631 count
= min_t(unsigned int, conn
->mtu
, len
);
1633 *frag
= bt_skb_send_alloc(sk
, count
, msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1636 if (memcpy_fromiovec(skb_put(*frag
, count
), msg
->msg_iov
, count
))
1642 frag
= &(*frag
)->next
;
1648 static struct sk_buff
*l2cap_create_connless_pdu(struct sock
*sk
, struct msghdr
*msg
, size_t len
)
1650 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
1651 struct sk_buff
*skb
;
1652 int err
, count
, hlen
= L2CAP_HDR_SIZE
+ 2;
1653 struct l2cap_hdr
*lh
;
1655 BT_DBG("sk %p len %d", sk
, (int)len
);
1657 count
= min_t(unsigned int, (conn
->mtu
- hlen
), len
);
1658 skb
= bt_skb_send_alloc(sk
, count
+ hlen
,
1659 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1661 return ERR_PTR(err
);
1663 /* Create L2CAP header */
1664 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
1665 lh
->cid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
1666 lh
->len
= cpu_to_le16(len
+ (hlen
- L2CAP_HDR_SIZE
));
1667 put_unaligned_le16(l2cap_pi(sk
)->psm
, skb_put(skb
, 2));
1669 err
= l2cap_skbuff_fromiovec(sk
, msg
, len
, count
, skb
);
1670 if (unlikely(err
< 0)) {
1672 return ERR_PTR(err
);
1677 static struct sk_buff
*l2cap_create_basic_pdu(struct sock
*sk
, struct msghdr
*msg
, size_t len
)
1679 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
1680 struct sk_buff
*skb
;
1681 int err
, count
, hlen
= L2CAP_HDR_SIZE
;
1682 struct l2cap_hdr
*lh
;
1684 BT_DBG("sk %p len %d", sk
, (int)len
);
1686 count
= min_t(unsigned int, (conn
->mtu
- hlen
), len
);
1687 skb
= bt_skb_send_alloc(sk
, count
+ hlen
,
1688 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1690 return ERR_PTR(err
);
1692 /* Create L2CAP header */
1693 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
1694 lh
->cid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
1695 lh
->len
= cpu_to_le16(len
+ (hlen
- L2CAP_HDR_SIZE
));
1697 err
= l2cap_skbuff_fromiovec(sk
, msg
, len
, count
, skb
);
1698 if (unlikely(err
< 0)) {
1700 return ERR_PTR(err
);
1705 static struct sk_buff
*l2cap_create_iframe_pdu(struct sock
*sk
, struct msghdr
*msg
, size_t len
, u16 control
, u16 sdulen
)
1707 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
1708 struct sk_buff
*skb
;
1709 int err
, count
, hlen
= L2CAP_HDR_SIZE
+ 2;
1710 struct l2cap_hdr
*lh
;
1712 BT_DBG("sk %p len %d", sk
, (int)len
);
1715 return ERR_PTR(-ENOTCONN
);
1720 if (l2cap_pi(sk
)->fcs
== L2CAP_FCS_CRC16
)
1723 count
= min_t(unsigned int, (conn
->mtu
- hlen
), len
);
1724 skb
= bt_skb_send_alloc(sk
, count
+ hlen
,
1725 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
1727 return ERR_PTR(err
);
1729 /* Create L2CAP header */
1730 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
1731 lh
->cid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
1732 lh
->len
= cpu_to_le16(len
+ (hlen
- L2CAP_HDR_SIZE
));
1733 put_unaligned_le16(control
, skb_put(skb
, 2));
1735 put_unaligned_le16(sdulen
, skb_put(skb
, 2));
1737 err
= l2cap_skbuff_fromiovec(sk
, msg
, len
, count
, skb
);
1738 if (unlikely(err
< 0)) {
1740 return ERR_PTR(err
);
1743 if (l2cap_pi(sk
)->fcs
== L2CAP_FCS_CRC16
)
1744 put_unaligned_le16(0, skb_put(skb
, 2));
1746 bt_cb(skb
)->retries
= 0;
1750 static inline int l2cap_sar_segment_sdu(struct sock
*sk
, struct msghdr
*msg
, size_t len
)
1752 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1753 struct sk_buff
*skb
;
1754 struct sk_buff_head sar_queue
;
1758 skb_queue_head_init(&sar_queue
);
1759 control
= L2CAP_SDU_START
;
1760 skb
= l2cap_create_iframe_pdu(sk
, msg
, pi
->remote_mps
, control
, len
);
1762 return PTR_ERR(skb
);
1764 __skb_queue_tail(&sar_queue
, skb
);
1765 len
-= pi
->remote_mps
;
1766 size
+= pi
->remote_mps
;
1771 if (len
> pi
->remote_mps
) {
1772 control
= L2CAP_SDU_CONTINUE
;
1773 buflen
= pi
->remote_mps
;
1775 control
= L2CAP_SDU_END
;
1779 skb
= l2cap_create_iframe_pdu(sk
, msg
, buflen
, control
, 0);
1781 skb_queue_purge(&sar_queue
);
1782 return PTR_ERR(skb
);
1785 __skb_queue_tail(&sar_queue
, skb
);
1789 skb_queue_splice_tail(&sar_queue
, TX_QUEUE(sk
));
1790 if (sk
->sk_send_head
== NULL
)
1791 sk
->sk_send_head
= sar_queue
.next
;
1796 static int l2cap_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
)
1798 struct sock
*sk
= sock
->sk
;
1799 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1800 struct sk_buff
*skb
;
1804 BT_DBG("sock %p, sk %p", sock
, sk
);
1806 err
= sock_error(sk
);
1810 if (msg
->msg_flags
& MSG_OOB
)
1815 if (sk
->sk_state
!= BT_CONNECTED
) {
1820 /* Connectionless channel */
1821 if (sk
->sk_type
== SOCK_DGRAM
) {
1822 skb
= l2cap_create_connless_pdu(sk
, msg
, len
);
1826 l2cap_do_send(sk
, skb
);
1833 case L2CAP_MODE_BASIC
:
1834 /* Check outgoing MTU */
1835 if (len
> pi
->omtu
) {
1840 /* Create a basic PDU */
1841 skb
= l2cap_create_basic_pdu(sk
, msg
, len
);
1847 l2cap_do_send(sk
, skb
);
1851 case L2CAP_MODE_ERTM
:
1852 case L2CAP_MODE_STREAMING
:
1853 /* Entire SDU fits into one PDU */
1854 if (len
<= pi
->remote_mps
) {
1855 control
= L2CAP_SDU_UNSEGMENTED
;
1856 skb
= l2cap_create_iframe_pdu(sk
, msg
, len
, control
, 0);
1861 __skb_queue_tail(TX_QUEUE(sk
), skb
);
1863 if (sk
->sk_send_head
== NULL
)
1864 sk
->sk_send_head
= skb
;
1867 /* Segment SDU into multiples PDUs */
1868 err
= l2cap_sar_segment_sdu(sk
, msg
, len
);
1873 if (pi
->mode
== L2CAP_MODE_STREAMING
) {
1874 l2cap_streaming_send(sk
);
1876 if ((pi
->conn_state
& L2CAP_CONN_REMOTE_BUSY
) &&
1877 (pi
->conn_state
& L2CAP_CONN_WAIT_F
)) {
1881 err
= l2cap_ertm_send(sk
);
1889 BT_DBG("bad state %1.1x", pi
->mode
);
1898 static int l2cap_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
, int flags
)
1900 struct sock
*sk
= sock
->sk
;
1904 if (sk
->sk_state
== BT_CONNECT2
&& bt_sk(sk
)->defer_setup
) {
1905 struct l2cap_conn_rsp rsp
;
1906 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
1909 sk
->sk_state
= BT_CONFIG
;
1911 rsp
.scid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
1912 rsp
.dcid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
1913 rsp
.result
= cpu_to_le16(L2CAP_CR_SUCCESS
);
1914 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
1915 l2cap_send_cmd(l2cap_pi(sk
)->conn
, l2cap_pi(sk
)->ident
,
1916 L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
1918 if (l2cap_pi(sk
)->conf_state
& L2CAP_CONF_REQ_SENT
) {
1923 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_REQ_SENT
;
1924 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
1925 l2cap_build_conf_req(sk
, buf
), buf
);
1926 l2cap_pi(sk
)->num_conf_req
++;
1934 if (sock
->type
== SOCK_STREAM
)
1935 return bt_sock_stream_recvmsg(iocb
, sock
, msg
, len
, flags
);
1937 return bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
1940 static int l2cap_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
1942 struct sock
*sk
= sock
->sk
;
1943 struct l2cap_options opts
;
1947 BT_DBG("sk %p", sk
);
1953 if (sk
->sk_state
== BT_CONNECTED
) {
1958 opts
.imtu
= l2cap_pi(sk
)->imtu
;
1959 opts
.omtu
= l2cap_pi(sk
)->omtu
;
1960 opts
.flush_to
= l2cap_pi(sk
)->flush_to
;
1961 opts
.mode
= l2cap_pi(sk
)->mode
;
1962 opts
.fcs
= l2cap_pi(sk
)->fcs
;
1963 opts
.max_tx
= l2cap_pi(sk
)->max_tx
;
1964 opts
.txwin_size
= (__u16
)l2cap_pi(sk
)->tx_win
;
1966 len
= min_t(unsigned int, sizeof(opts
), optlen
);
1967 if (copy_from_user((char *) &opts
, optval
, len
)) {
1972 if (opts
.txwin_size
> L2CAP_DEFAULT_TX_WINDOW
) {
1977 l2cap_pi(sk
)->mode
= opts
.mode
;
1978 switch (l2cap_pi(sk
)->mode
) {
1979 case L2CAP_MODE_BASIC
:
1980 l2cap_pi(sk
)->conf_state
&= ~L2CAP_CONF_STATE2_DEVICE
;
1982 case L2CAP_MODE_ERTM
:
1983 case L2CAP_MODE_STREAMING
:
1992 l2cap_pi(sk
)->imtu
= opts
.imtu
;
1993 l2cap_pi(sk
)->omtu
= opts
.omtu
;
1994 l2cap_pi(sk
)->fcs
= opts
.fcs
;
1995 l2cap_pi(sk
)->max_tx
= opts
.max_tx
;
1996 l2cap_pi(sk
)->tx_win
= (__u8
)opts
.txwin_size
;
2000 if (get_user(opt
, (u32 __user
*) optval
)) {
2005 if (opt
& L2CAP_LM_AUTH
)
2006 l2cap_pi(sk
)->sec_level
= BT_SECURITY_LOW
;
2007 if (opt
& L2CAP_LM_ENCRYPT
)
2008 l2cap_pi(sk
)->sec_level
= BT_SECURITY_MEDIUM
;
2009 if (opt
& L2CAP_LM_SECURE
)
2010 l2cap_pi(sk
)->sec_level
= BT_SECURITY_HIGH
;
2012 l2cap_pi(sk
)->role_switch
= (opt
& L2CAP_LM_MASTER
);
2013 l2cap_pi(sk
)->force_reliable
= (opt
& L2CAP_LM_RELIABLE
);
2025 static int l2cap_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
2027 struct sock
*sk
= sock
->sk
;
2028 struct bt_security sec
;
2032 BT_DBG("sk %p", sk
);
2034 if (level
== SOL_L2CAP
)
2035 return l2cap_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
2037 if (level
!= SOL_BLUETOOTH
)
2038 return -ENOPROTOOPT
;
2044 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
2045 && sk
->sk_type
!= SOCK_RAW
) {
2050 sec
.level
= BT_SECURITY_LOW
;
2052 len
= min_t(unsigned int, sizeof(sec
), optlen
);
2053 if (copy_from_user((char *) &sec
, optval
, len
)) {
2058 if (sec
.level
< BT_SECURITY_LOW
||
2059 sec
.level
> BT_SECURITY_HIGH
) {
2064 l2cap_pi(sk
)->sec_level
= sec
.level
;
2067 case BT_DEFER_SETUP
:
2068 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
2073 if (get_user(opt
, (u32 __user
*) optval
)) {
2078 bt_sk(sk
)->defer_setup
= opt
;
2090 static int l2cap_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
2092 struct sock
*sk
= sock
->sk
;
2093 struct l2cap_options opts
;
2094 struct l2cap_conninfo cinfo
;
2098 BT_DBG("sk %p", sk
);
2100 if (get_user(len
, optlen
))
2107 opts
.imtu
= l2cap_pi(sk
)->imtu
;
2108 opts
.omtu
= l2cap_pi(sk
)->omtu
;
2109 opts
.flush_to
= l2cap_pi(sk
)->flush_to
;
2110 opts
.mode
= l2cap_pi(sk
)->mode
;
2111 opts
.fcs
= l2cap_pi(sk
)->fcs
;
2112 opts
.max_tx
= l2cap_pi(sk
)->max_tx
;
2113 opts
.txwin_size
= (__u16
)l2cap_pi(sk
)->tx_win
;
2115 len
= min_t(unsigned int, len
, sizeof(opts
));
2116 if (copy_to_user(optval
, (char *) &opts
, len
))
2122 switch (l2cap_pi(sk
)->sec_level
) {
2123 case BT_SECURITY_LOW
:
2124 opt
= L2CAP_LM_AUTH
;
2126 case BT_SECURITY_MEDIUM
:
2127 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
;
2129 case BT_SECURITY_HIGH
:
2130 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
|
2138 if (l2cap_pi(sk
)->role_switch
)
2139 opt
|= L2CAP_LM_MASTER
;
2141 if (l2cap_pi(sk
)->force_reliable
)
2142 opt
|= L2CAP_LM_RELIABLE
;
2144 if (put_user(opt
, (u32 __user
*) optval
))
2148 case L2CAP_CONNINFO
:
2149 if (sk
->sk_state
!= BT_CONNECTED
&&
2150 !(sk
->sk_state
== BT_CONNECT2
&&
2151 bt_sk(sk
)->defer_setup
)) {
2156 cinfo
.hci_handle
= l2cap_pi(sk
)->conn
->hcon
->handle
;
2157 memcpy(cinfo
.dev_class
, l2cap_pi(sk
)->conn
->hcon
->dev_class
, 3);
2159 len
= min_t(unsigned int, len
, sizeof(cinfo
));
2160 if (copy_to_user(optval
, (char *) &cinfo
, len
))
2174 static int l2cap_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
2176 struct sock
*sk
= sock
->sk
;
2177 struct bt_security sec
;
2180 BT_DBG("sk %p", sk
);
2182 if (level
== SOL_L2CAP
)
2183 return l2cap_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
2185 if (level
!= SOL_BLUETOOTH
)
2186 return -ENOPROTOOPT
;
2188 if (get_user(len
, optlen
))
2195 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
2196 && sk
->sk_type
!= SOCK_RAW
) {
2201 sec
.level
= l2cap_pi(sk
)->sec_level
;
2203 len
= min_t(unsigned int, len
, sizeof(sec
));
2204 if (copy_to_user(optval
, (char *) &sec
, len
))
2209 case BT_DEFER_SETUP
:
2210 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
2215 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
2229 static int l2cap_sock_shutdown(struct socket
*sock
, int how
)
2231 struct sock
*sk
= sock
->sk
;
2234 BT_DBG("sock %p, sk %p", sock
, sk
);
2240 if (!sk
->sk_shutdown
) {
2241 if (l2cap_pi(sk
)->mode
== L2CAP_MODE_ERTM
)
2242 err
= __l2cap_wait_ack(sk
);
2244 sk
->sk_shutdown
= SHUTDOWN_MASK
;
2245 l2cap_sock_clear_timer(sk
);
2246 __l2cap_sock_close(sk
, 0);
2248 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
2249 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
2253 if (!err
&& sk
->sk_err
)
2260 static int l2cap_sock_release(struct socket
*sock
)
2262 struct sock
*sk
= sock
->sk
;
2265 BT_DBG("sock %p, sk %p", sock
, sk
);
2270 err
= l2cap_sock_shutdown(sock
, 2);
2273 l2cap_sock_kill(sk
);
2277 static void l2cap_chan_ready(struct sock
*sk
)
2279 struct sock
*parent
= bt_sk(sk
)->parent
;
2281 BT_DBG("sk %p, parent %p", sk
, parent
);
2283 l2cap_pi(sk
)->conf_state
= 0;
2284 l2cap_sock_clear_timer(sk
);
2287 /* Outgoing channel.
2288 * Wake up socket sleeping on connect.
2290 sk
->sk_state
= BT_CONNECTED
;
2291 sk
->sk_state_change(sk
);
2293 /* Incoming channel.
2294 * Wake up socket sleeping on accept.
2296 parent
->sk_data_ready(parent
, 0);
2300 /* Copy frame to all raw sockets on that connection */
2301 static void l2cap_raw_recv(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2303 struct l2cap_chan_list
*l
= &conn
->chan_list
;
2304 struct sk_buff
*nskb
;
2307 BT_DBG("conn %p", conn
);
2309 read_lock(&l
->lock
);
2310 for (sk
= l
->head
; sk
; sk
= l2cap_pi(sk
)->next_c
) {
2311 if (sk
->sk_type
!= SOCK_RAW
)
2314 /* Don't send frame to the socket it came from */
2317 nskb
= skb_clone(skb
, GFP_ATOMIC
);
2321 if (sock_queue_rcv_skb(sk
, nskb
))
2324 read_unlock(&l
->lock
);
2327 /* ---- L2CAP signalling commands ---- */
2328 static struct sk_buff
*l2cap_build_cmd(struct l2cap_conn
*conn
,
2329 u8 code
, u8 ident
, u16 dlen
, void *data
)
2331 struct sk_buff
*skb
, **frag
;
2332 struct l2cap_cmd_hdr
*cmd
;
2333 struct l2cap_hdr
*lh
;
2336 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
2337 conn
, code
, ident
, dlen
);
2339 len
= L2CAP_HDR_SIZE
+ L2CAP_CMD_HDR_SIZE
+ dlen
;
2340 count
= min_t(unsigned int, conn
->mtu
, len
);
2342 skb
= bt_skb_alloc(count
, GFP_ATOMIC
);
2346 lh
= (struct l2cap_hdr
*) skb_put(skb
, L2CAP_HDR_SIZE
);
2347 lh
->len
= cpu_to_le16(L2CAP_CMD_HDR_SIZE
+ dlen
);
2348 lh
->cid
= cpu_to_le16(L2CAP_CID_SIGNALING
);
2350 cmd
= (struct l2cap_cmd_hdr
*) skb_put(skb
, L2CAP_CMD_HDR_SIZE
);
2353 cmd
->len
= cpu_to_le16(dlen
);
2356 count
-= L2CAP_HDR_SIZE
+ L2CAP_CMD_HDR_SIZE
;
2357 memcpy(skb_put(skb
, count
), data
, count
);
2363 /* Continuation fragments (no L2CAP header) */
2364 frag
= &skb_shinfo(skb
)->frag_list
;
2366 count
= min_t(unsigned int, conn
->mtu
, len
);
2368 *frag
= bt_skb_alloc(count
, GFP_ATOMIC
);
2372 memcpy(skb_put(*frag
, count
), data
, count
);
2377 frag
= &(*frag
)->next
;
2387 static inline int l2cap_get_conf_opt(void **ptr
, int *type
, int *olen
, unsigned long *val
)
2389 struct l2cap_conf_opt
*opt
= *ptr
;
2392 len
= L2CAP_CONF_OPT_SIZE
+ opt
->len
;
2400 *val
= *((u8
*) opt
->val
);
2404 *val
= get_unaligned_le16(opt
->val
);
2408 *val
= get_unaligned_le32(opt
->val
);
2412 *val
= (unsigned long) opt
->val
;
2416 BT_DBG("type 0x%2.2x len %d val 0x%lx", *type
, opt
->len
, *val
);
2420 static void l2cap_add_conf_opt(void **ptr
, u8 type
, u8 len
, unsigned long val
)
2422 struct l2cap_conf_opt
*opt
= *ptr
;
2424 BT_DBG("type 0x%2.2x len %d val 0x%lx", type
, len
, val
);
2431 *((u8
*) opt
->val
) = val
;
2435 put_unaligned_le16(val
, opt
->val
);
2439 put_unaligned_le32(val
, opt
->val
);
2443 memcpy(opt
->val
, (void *) val
, len
);
2447 *ptr
+= L2CAP_CONF_OPT_SIZE
+ len
;
2450 static void l2cap_ack_timeout(unsigned long arg
)
2452 struct sock
*sk
= (void *) arg
;
2455 l2cap_send_ack(l2cap_pi(sk
));
2459 static inline void l2cap_ertm_init(struct sock
*sk
)
2461 l2cap_pi(sk
)->expected_ack_seq
= 0;
2462 l2cap_pi(sk
)->unacked_frames
= 0;
2463 l2cap_pi(sk
)->buffer_seq
= 0;
2464 l2cap_pi(sk
)->num_acked
= 0;
2465 l2cap_pi(sk
)->frames_sent
= 0;
2467 setup_timer(&l2cap_pi(sk
)->retrans_timer
,
2468 l2cap_retrans_timeout
, (unsigned long) sk
);
2469 setup_timer(&l2cap_pi(sk
)->monitor_timer
,
2470 l2cap_monitor_timeout
, (unsigned long) sk
);
2471 setup_timer(&l2cap_pi(sk
)->ack_timer
,
2472 l2cap_ack_timeout
, (unsigned long) sk
);
2474 __skb_queue_head_init(SREJ_QUEUE(sk
));
2475 __skb_queue_head_init(BUSY_QUEUE(sk
));
2477 INIT_WORK(&l2cap_pi(sk
)->busy_work
, l2cap_busy_work
);
2479 sk
->sk_backlog_rcv
= l2cap_ertm_data_rcv
;
2482 static inline __u8
l2cap_select_mode(__u8 mode
, __u16 remote_feat_mask
)
2485 case L2CAP_MODE_STREAMING
:
2486 case L2CAP_MODE_ERTM
:
2487 if (l2cap_mode_supported(mode
, remote_feat_mask
))
2491 return L2CAP_MODE_BASIC
;
2495 static int l2cap_build_conf_req(struct sock
*sk
, void *data
)
2497 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
2498 struct l2cap_conf_req
*req
= data
;
2499 struct l2cap_conf_rfc rfc
= { .mode
= pi
->mode
};
2500 void *ptr
= req
->data
;
2502 BT_DBG("sk %p", sk
);
2504 if (pi
->num_conf_req
|| pi
->num_conf_rsp
)
2508 case L2CAP_MODE_STREAMING
:
2509 case L2CAP_MODE_ERTM
:
2510 if (pi
->conf_state
& L2CAP_CONF_STATE2_DEVICE
)
2515 pi
->mode
= l2cap_select_mode(rfc
.mode
, pi
->conn
->feat_mask
);
2521 case L2CAP_MODE_BASIC
:
2522 if (pi
->imtu
!= L2CAP_DEFAULT_MTU
)
2523 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_MTU
, 2, pi
->imtu
);
2525 if (!(pi
->conn
->feat_mask
& L2CAP_FEAT_ERTM
) &&
2526 !(pi
->conn
->feat_mask
& L2CAP_FEAT_STREAMING
))
2529 rfc
.mode
= L2CAP_MODE_BASIC
;
2531 rfc
.max_transmit
= 0;
2532 rfc
.retrans_timeout
= 0;
2533 rfc
.monitor_timeout
= 0;
2534 rfc
.max_pdu_size
= 0;
2536 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
, sizeof(rfc
),
2537 (unsigned long) &rfc
);
2540 case L2CAP_MODE_ERTM
:
2541 rfc
.mode
= L2CAP_MODE_ERTM
;
2542 rfc
.txwin_size
= pi
->tx_win
;
2543 rfc
.max_transmit
= pi
->max_tx
;
2544 rfc
.retrans_timeout
= 0;
2545 rfc
.monitor_timeout
= 0;
2546 rfc
.max_pdu_size
= cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE
);
2547 if (L2CAP_DEFAULT_MAX_PDU_SIZE
> pi
->conn
->mtu
- 10)
2548 rfc
.max_pdu_size
= cpu_to_le16(pi
->conn
->mtu
- 10);
2550 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
, sizeof(rfc
),
2551 (unsigned long) &rfc
);
2553 if (!(pi
->conn
->feat_mask
& L2CAP_FEAT_FCS
))
2556 if (pi
->fcs
== L2CAP_FCS_NONE
||
2557 pi
->conf_state
& L2CAP_CONF_NO_FCS_RECV
) {
2558 pi
->fcs
= L2CAP_FCS_NONE
;
2559 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_FCS
, 1, pi
->fcs
);
2563 case L2CAP_MODE_STREAMING
:
2564 rfc
.mode
= L2CAP_MODE_STREAMING
;
2566 rfc
.max_transmit
= 0;
2567 rfc
.retrans_timeout
= 0;
2568 rfc
.monitor_timeout
= 0;
2569 rfc
.max_pdu_size
= cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE
);
2570 if (L2CAP_DEFAULT_MAX_PDU_SIZE
> pi
->conn
->mtu
- 10)
2571 rfc
.max_pdu_size
= cpu_to_le16(pi
->conn
->mtu
- 10);
2573 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
, sizeof(rfc
),
2574 (unsigned long) &rfc
);
2576 if (!(pi
->conn
->feat_mask
& L2CAP_FEAT_FCS
))
2579 if (pi
->fcs
== L2CAP_FCS_NONE
||
2580 pi
->conf_state
& L2CAP_CONF_NO_FCS_RECV
) {
2581 pi
->fcs
= L2CAP_FCS_NONE
;
2582 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_FCS
, 1, pi
->fcs
);
2587 /* FIXME: Need actual value of the flush timeout */
2588 //if (flush_to != L2CAP_DEFAULT_FLUSH_TO)
2589 // l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2, pi->flush_to);
2591 req
->dcid
= cpu_to_le16(pi
->dcid
);
2592 req
->flags
= cpu_to_le16(0);
2597 static int l2cap_parse_conf_req(struct sock
*sk
, void *data
)
2599 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
2600 struct l2cap_conf_rsp
*rsp
= data
;
2601 void *ptr
= rsp
->data
;
2602 void *req
= pi
->conf_req
;
2603 int len
= pi
->conf_len
;
2604 int type
, hint
, olen
;
2606 struct l2cap_conf_rfc rfc
= { .mode
= L2CAP_MODE_BASIC
};
2607 u16 mtu
= L2CAP_DEFAULT_MTU
;
2608 u16 result
= L2CAP_CONF_SUCCESS
;
2610 BT_DBG("sk %p", sk
);
2612 while (len
>= L2CAP_CONF_OPT_SIZE
) {
2613 len
-= l2cap_get_conf_opt(&req
, &type
, &olen
, &val
);
2615 hint
= type
& L2CAP_CONF_HINT
;
2616 type
&= L2CAP_CONF_MASK
;
2619 case L2CAP_CONF_MTU
:
2623 case L2CAP_CONF_FLUSH_TO
:
2627 case L2CAP_CONF_QOS
:
2630 case L2CAP_CONF_RFC
:
2631 if (olen
== sizeof(rfc
))
2632 memcpy(&rfc
, (void *) val
, olen
);
2635 case L2CAP_CONF_FCS
:
2636 if (val
== L2CAP_FCS_NONE
)
2637 pi
->conf_state
|= L2CAP_CONF_NO_FCS_RECV
;
2645 result
= L2CAP_CONF_UNKNOWN
;
2646 *((u8
*) ptr
++) = type
;
2651 if (pi
->num_conf_rsp
|| pi
->num_conf_req
> 1)
2655 case L2CAP_MODE_STREAMING
:
2656 case L2CAP_MODE_ERTM
:
2657 if (!(pi
->conf_state
& L2CAP_CONF_STATE2_DEVICE
)) {
2658 pi
->mode
= l2cap_select_mode(rfc
.mode
,
2659 pi
->conn
->feat_mask
);
2663 if (pi
->mode
!= rfc
.mode
)
2664 return -ECONNREFUSED
;
2670 if (pi
->mode
!= rfc
.mode
) {
2671 result
= L2CAP_CONF_UNACCEPT
;
2672 rfc
.mode
= pi
->mode
;
2674 if (pi
->num_conf_rsp
== 1)
2675 return -ECONNREFUSED
;
2677 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
,
2678 sizeof(rfc
), (unsigned long) &rfc
);
2682 if (result
== L2CAP_CONF_SUCCESS
) {
2683 /* Configure output options and let the other side know
2684 * which ones we don't like. */
2686 if (mtu
< L2CAP_DEFAULT_MIN_MTU
)
2687 result
= L2CAP_CONF_UNACCEPT
;
2690 pi
->conf_state
|= L2CAP_CONF_MTU_DONE
;
2692 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_MTU
, 2, pi
->omtu
);
2695 case L2CAP_MODE_BASIC
:
2696 pi
->fcs
= L2CAP_FCS_NONE
;
2697 pi
->conf_state
|= L2CAP_CONF_MODE_DONE
;
2700 case L2CAP_MODE_ERTM
:
2701 pi
->remote_tx_win
= rfc
.txwin_size
;
2702 pi
->remote_max_tx
= rfc
.max_transmit
;
2704 if (le16_to_cpu(rfc
.max_pdu_size
) > pi
->conn
->mtu
- 10)
2705 rfc
.max_pdu_size
= cpu_to_le16(pi
->conn
->mtu
- 10);
2707 pi
->remote_mps
= le16_to_cpu(rfc
.max_pdu_size
);
2709 rfc
.retrans_timeout
=
2710 le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO
);
2711 rfc
.monitor_timeout
=
2712 le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO
);
2714 pi
->conf_state
|= L2CAP_CONF_MODE_DONE
;
2716 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
,
2717 sizeof(rfc
), (unsigned long) &rfc
);
2721 case L2CAP_MODE_STREAMING
:
2722 if (le16_to_cpu(rfc
.max_pdu_size
) > pi
->conn
->mtu
- 10)
2723 rfc
.max_pdu_size
= cpu_to_le16(pi
->conn
->mtu
- 10);
2725 pi
->remote_mps
= le16_to_cpu(rfc
.max_pdu_size
);
2727 pi
->conf_state
|= L2CAP_CONF_MODE_DONE
;
2729 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
,
2730 sizeof(rfc
), (unsigned long) &rfc
);
2735 result
= L2CAP_CONF_UNACCEPT
;
2737 memset(&rfc
, 0, sizeof(rfc
));
2738 rfc
.mode
= pi
->mode
;
2741 if (result
== L2CAP_CONF_SUCCESS
)
2742 pi
->conf_state
|= L2CAP_CONF_OUTPUT_DONE
;
2744 rsp
->scid
= cpu_to_le16(pi
->dcid
);
2745 rsp
->result
= cpu_to_le16(result
);
2746 rsp
->flags
= cpu_to_le16(0x0000);
2751 static int l2cap_parse_conf_rsp(struct sock
*sk
, void *rsp
, int len
, void *data
, u16
*result
)
2753 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
2754 struct l2cap_conf_req
*req
= data
;
2755 void *ptr
= req
->data
;
2758 struct l2cap_conf_rfc rfc
;
2760 BT_DBG("sk %p, rsp %p, len %d, req %p", sk
, rsp
, len
, data
);
2762 while (len
>= L2CAP_CONF_OPT_SIZE
) {
2763 len
-= l2cap_get_conf_opt(&rsp
, &type
, &olen
, &val
);
2766 case L2CAP_CONF_MTU
:
2767 if (val
< L2CAP_DEFAULT_MIN_MTU
) {
2768 *result
= L2CAP_CONF_UNACCEPT
;
2769 pi
->imtu
= L2CAP_DEFAULT_MIN_MTU
;
2772 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_MTU
, 2, pi
->imtu
);
2775 case L2CAP_CONF_FLUSH_TO
:
2777 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_FLUSH_TO
,
2781 case L2CAP_CONF_RFC
:
2782 if (olen
== sizeof(rfc
))
2783 memcpy(&rfc
, (void *)val
, olen
);
2785 if ((pi
->conf_state
& L2CAP_CONF_STATE2_DEVICE
) &&
2786 rfc
.mode
!= pi
->mode
)
2787 return -ECONNREFUSED
;
2791 l2cap_add_conf_opt(&ptr
, L2CAP_CONF_RFC
,
2792 sizeof(rfc
), (unsigned long) &rfc
);
2797 if (pi
->mode
== L2CAP_MODE_BASIC
&& pi
->mode
!= rfc
.mode
)
2798 return -ECONNREFUSED
;
2800 pi
->mode
= rfc
.mode
;
2802 if (*result
== L2CAP_CONF_SUCCESS
) {
2804 case L2CAP_MODE_ERTM
:
2805 pi
->retrans_timeout
= le16_to_cpu(rfc
.retrans_timeout
);
2806 pi
->monitor_timeout
= le16_to_cpu(rfc
.monitor_timeout
);
2807 pi
->mps
= le16_to_cpu(rfc
.max_pdu_size
);
2809 case L2CAP_MODE_STREAMING
:
2810 pi
->mps
= le16_to_cpu(rfc
.max_pdu_size
);
2814 req
->dcid
= cpu_to_le16(pi
->dcid
);
2815 req
->flags
= cpu_to_le16(0x0000);
2820 static int l2cap_build_conf_rsp(struct sock
*sk
, void *data
, u16 result
, u16 flags
)
2822 struct l2cap_conf_rsp
*rsp
= data
;
2823 void *ptr
= rsp
->data
;
2825 BT_DBG("sk %p", sk
);
2827 rsp
->scid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
2828 rsp
->result
= cpu_to_le16(result
);
2829 rsp
->flags
= cpu_to_le16(flags
);
2834 static void l2cap_conf_rfc_get(struct sock
*sk
, void *rsp
, int len
)
2836 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
2839 struct l2cap_conf_rfc rfc
;
2841 BT_DBG("sk %p, rsp %p, len %d", sk
, rsp
, len
);
2843 if ((pi
->mode
!= L2CAP_MODE_ERTM
) && (pi
->mode
!= L2CAP_MODE_STREAMING
))
2846 while (len
>= L2CAP_CONF_OPT_SIZE
) {
2847 len
-= l2cap_get_conf_opt(&rsp
, &type
, &olen
, &val
);
2850 case L2CAP_CONF_RFC
:
2851 if (olen
== sizeof(rfc
))
2852 memcpy(&rfc
, (void *)val
, olen
);
2859 case L2CAP_MODE_ERTM
:
2860 pi
->retrans_timeout
= le16_to_cpu(rfc
.retrans_timeout
);
2861 pi
->monitor_timeout
= le16_to_cpu(rfc
.monitor_timeout
);
2862 pi
->mps
= le16_to_cpu(rfc
.max_pdu_size
);
2864 case L2CAP_MODE_STREAMING
:
2865 pi
->mps
= le16_to_cpu(rfc
.max_pdu_size
);
2869 static inline int l2cap_command_rej(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2871 struct l2cap_cmd_rej
*rej
= (struct l2cap_cmd_rej
*) data
;
2873 if (rej
->reason
!= 0x0000)
2876 if ((conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_SENT
) &&
2877 cmd
->ident
== conn
->info_ident
) {
2878 del_timer(&conn
->info_timer
);
2880 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
2881 conn
->info_ident
= 0;
2883 l2cap_conn_start(conn
);
2889 static inline int l2cap_connect_req(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
2891 struct l2cap_chan_list
*list
= &conn
->chan_list
;
2892 struct l2cap_conn_req
*req
= (struct l2cap_conn_req
*) data
;
2893 struct l2cap_conn_rsp rsp
;
2894 struct sock
*parent
, *sk
= NULL
;
2895 int result
, status
= L2CAP_CS_NO_INFO
;
2897 u16 dcid
= 0, scid
= __le16_to_cpu(req
->scid
);
2898 __le16 psm
= req
->psm
;
2900 BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm
, scid
);
2902 /* Check if we have socket listening on psm */
2903 parent
= l2cap_get_sock_by_psm(BT_LISTEN
, psm
, conn
->src
);
2905 result
= L2CAP_CR_BAD_PSM
;
2909 bh_lock_sock(parent
);
2911 /* Check if the ACL is secure enough (if not SDP) */
2912 if (psm
!= cpu_to_le16(0x0001) &&
2913 !hci_conn_check_link_mode(conn
->hcon
)) {
2914 conn
->disc_reason
= 0x05;
2915 result
= L2CAP_CR_SEC_BLOCK
;
2919 result
= L2CAP_CR_NO_MEM
;
2921 /* Check for backlog size */
2922 if (sk_acceptq_is_full(parent
)) {
2923 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
2927 sk
= l2cap_sock_alloc(sock_net(parent
), NULL
, BTPROTO_L2CAP
, GFP_ATOMIC
);
2931 write_lock_bh(&list
->lock
);
2933 /* Check if we already have channel with that dcid */
2934 if (__l2cap_get_chan_by_dcid(list
, scid
)) {
2935 write_unlock_bh(&list
->lock
);
2936 sock_set_flag(sk
, SOCK_ZAPPED
);
2937 l2cap_sock_kill(sk
);
2941 hci_conn_hold(conn
->hcon
);
2943 l2cap_sock_init(sk
, parent
);
2944 bacpy(&bt_sk(sk
)->src
, conn
->src
);
2945 bacpy(&bt_sk(sk
)->dst
, conn
->dst
);
2946 l2cap_pi(sk
)->psm
= psm
;
2947 l2cap_pi(sk
)->dcid
= scid
;
2949 __l2cap_chan_add(conn
, sk
, parent
);
2950 dcid
= l2cap_pi(sk
)->scid
;
2952 l2cap_sock_set_timer(sk
, sk
->sk_sndtimeo
);
2954 l2cap_pi(sk
)->ident
= cmd
->ident
;
2956 if (conn
->info_state
& L2CAP_INFO_FEAT_MASK_REQ_DONE
) {
2957 if (l2cap_check_security(sk
)) {
2958 if (bt_sk(sk
)->defer_setup
) {
2959 sk
->sk_state
= BT_CONNECT2
;
2960 result
= L2CAP_CR_PEND
;
2961 status
= L2CAP_CS_AUTHOR_PEND
;
2962 parent
->sk_data_ready(parent
, 0);
2964 sk
->sk_state
= BT_CONFIG
;
2965 result
= L2CAP_CR_SUCCESS
;
2966 status
= L2CAP_CS_NO_INFO
;
2969 sk
->sk_state
= BT_CONNECT2
;
2970 result
= L2CAP_CR_PEND
;
2971 status
= L2CAP_CS_AUTHEN_PEND
;
2974 sk
->sk_state
= BT_CONNECT2
;
2975 result
= L2CAP_CR_PEND
;
2976 status
= L2CAP_CS_NO_INFO
;
2979 write_unlock_bh(&list
->lock
);
2982 bh_unlock_sock(parent
);
2985 rsp
.scid
= cpu_to_le16(scid
);
2986 rsp
.dcid
= cpu_to_le16(dcid
);
2987 rsp
.result
= cpu_to_le16(result
);
2988 rsp
.status
= cpu_to_le16(status
);
2989 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
2991 if (result
== L2CAP_CR_PEND
&& status
== L2CAP_CS_NO_INFO
) {
2992 struct l2cap_info_req info
;
2993 info
.type
= cpu_to_le16(L2CAP_IT_FEAT_MASK
);
2995 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_SENT
;
2996 conn
->info_ident
= l2cap_get_ident(conn
);
2998 mod_timer(&conn
->info_timer
, jiffies
+
2999 msecs_to_jiffies(L2CAP_INFO_TIMEOUT
));
3001 l2cap_send_cmd(conn
, conn
->info_ident
,
3002 L2CAP_INFO_REQ
, sizeof(info
), &info
);
3005 if (sk
&& !(l2cap_pi(sk
)->conf_state
& L2CAP_CONF_REQ_SENT
) &&
3006 result
== L2CAP_CR_SUCCESS
) {
3008 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_REQ_SENT
;
3009 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
3010 l2cap_build_conf_req(sk
, buf
), buf
);
3011 l2cap_pi(sk
)->num_conf_req
++;
3017 static inline int l2cap_connect_rsp(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
3019 struct l2cap_conn_rsp
*rsp
= (struct l2cap_conn_rsp
*) data
;
3020 u16 scid
, dcid
, result
, status
;
3024 scid
= __le16_to_cpu(rsp
->scid
);
3025 dcid
= __le16_to_cpu(rsp
->dcid
);
3026 result
= __le16_to_cpu(rsp
->result
);
3027 status
= __le16_to_cpu(rsp
->status
);
3029 BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid
, scid
, result
, status
);
3032 sk
= l2cap_get_chan_by_scid(&conn
->chan_list
, scid
);
3036 sk
= l2cap_get_chan_by_ident(&conn
->chan_list
, cmd
->ident
);
3042 case L2CAP_CR_SUCCESS
:
3043 sk
->sk_state
= BT_CONFIG
;
3044 l2cap_pi(sk
)->ident
= 0;
3045 l2cap_pi(sk
)->dcid
= dcid
;
3046 l2cap_pi(sk
)->conf_state
&= ~L2CAP_CONF_CONNECT_PEND
;
3048 if (l2cap_pi(sk
)->conf_state
& L2CAP_CONF_REQ_SENT
)
3051 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_REQ_SENT
;
3053 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
3054 l2cap_build_conf_req(sk
, req
), req
);
3055 l2cap_pi(sk
)->num_conf_req
++;
3059 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_CONNECT_PEND
;
3063 /* don't delete l2cap channel if sk is owned by user */
3064 if (sock_owned_by_user(sk
)) {
3065 sk
->sk_state
= BT_DISCONN
;
3066 l2cap_sock_clear_timer(sk
);
3067 l2cap_sock_set_timer(sk
, HZ
/ 5);
3071 l2cap_chan_del(sk
, ECONNREFUSED
);
3079 static inline void set_default_fcs(struct l2cap_pinfo
*pi
)
3081 /* FCS is enabled only in ERTM or streaming mode, if one or both
3084 if (pi
->mode
!= L2CAP_MODE_ERTM
&& pi
->mode
!= L2CAP_MODE_STREAMING
)
3085 pi
->fcs
= L2CAP_FCS_NONE
;
3086 else if (!(pi
->conf_state
& L2CAP_CONF_NO_FCS_RECV
))
3087 pi
->fcs
= L2CAP_FCS_CRC16
;
3090 static inline int l2cap_config_req(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u16 cmd_len
, u8
*data
)
3092 struct l2cap_conf_req
*req
= (struct l2cap_conf_req
*) data
;
3098 dcid
= __le16_to_cpu(req
->dcid
);
3099 flags
= __le16_to_cpu(req
->flags
);
3101 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid
, flags
);
3103 sk
= l2cap_get_chan_by_scid(&conn
->chan_list
, dcid
);
3107 if (sk
->sk_state
!= BT_CONFIG
) {
3108 struct l2cap_cmd_rej rej
;
3110 rej
.reason
= cpu_to_le16(0x0002);
3111 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_COMMAND_REJ
,
3116 /* Reject if config buffer is too small. */
3117 len
= cmd_len
- sizeof(*req
);
3118 if (l2cap_pi(sk
)->conf_len
+ len
> sizeof(l2cap_pi(sk
)->conf_req
)) {
3119 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONF_RSP
,
3120 l2cap_build_conf_rsp(sk
, rsp
,
3121 L2CAP_CONF_REJECT
, flags
), rsp
);
3126 memcpy(l2cap_pi(sk
)->conf_req
+ l2cap_pi(sk
)->conf_len
, req
->data
, len
);
3127 l2cap_pi(sk
)->conf_len
+= len
;
3129 if (flags
& 0x0001) {
3130 /* Incomplete config. Send empty response. */
3131 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONF_RSP
,
3132 l2cap_build_conf_rsp(sk
, rsp
,
3133 L2CAP_CONF_SUCCESS
, 0x0001), rsp
);
3137 /* Complete config. */
3138 len
= l2cap_parse_conf_req(sk
, rsp
);
3140 l2cap_send_disconn_req(conn
, sk
, ECONNRESET
);
3144 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_CONF_RSP
, len
, rsp
);
3145 l2cap_pi(sk
)->num_conf_rsp
++;
3147 /* Reset config buffer. */
3148 l2cap_pi(sk
)->conf_len
= 0;
3150 if (!(l2cap_pi(sk
)->conf_state
& L2CAP_CONF_OUTPUT_DONE
))
3153 if (l2cap_pi(sk
)->conf_state
& L2CAP_CONF_INPUT_DONE
) {
3154 set_default_fcs(l2cap_pi(sk
));
3156 sk
->sk_state
= BT_CONNECTED
;
3158 l2cap_pi(sk
)->next_tx_seq
= 0;
3159 l2cap_pi(sk
)->expected_tx_seq
= 0;
3160 __skb_queue_head_init(TX_QUEUE(sk
));
3161 if (l2cap_pi(sk
)->mode
== L2CAP_MODE_ERTM
)
3162 l2cap_ertm_init(sk
);
3164 l2cap_chan_ready(sk
);
3168 if (!(l2cap_pi(sk
)->conf_state
& L2CAP_CONF_REQ_SENT
)) {
3170 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_REQ_SENT
;
3171 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
3172 l2cap_build_conf_req(sk
, buf
), buf
);
3173 l2cap_pi(sk
)->num_conf_req
++;
3181 static inline int l2cap_config_rsp(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
3183 struct l2cap_conf_rsp
*rsp
= (struct l2cap_conf_rsp
*)data
;
3184 u16 scid
, flags
, result
;
3186 int len
= cmd
->len
- sizeof(*rsp
);
3188 scid
= __le16_to_cpu(rsp
->scid
);
3189 flags
= __le16_to_cpu(rsp
->flags
);
3190 result
= __le16_to_cpu(rsp
->result
);
3192 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
3193 scid
, flags
, result
);
3195 sk
= l2cap_get_chan_by_scid(&conn
->chan_list
, scid
);
3200 case L2CAP_CONF_SUCCESS
:
3201 l2cap_conf_rfc_get(sk
, rsp
->data
, len
);
3204 case L2CAP_CONF_UNACCEPT
:
3205 if (l2cap_pi(sk
)->num_conf_rsp
<= L2CAP_CONF_MAX_CONF_RSP
) {
3208 if (len
> sizeof(req
) - sizeof(struct l2cap_conf_req
)) {
3209 l2cap_send_disconn_req(conn
, sk
, ECONNRESET
);
3213 /* throw out any old stored conf requests */
3214 result
= L2CAP_CONF_SUCCESS
;
3215 len
= l2cap_parse_conf_rsp(sk
, rsp
->data
,
3218 l2cap_send_disconn_req(conn
, sk
, ECONNRESET
);
3222 l2cap_send_cmd(conn
, l2cap_get_ident(conn
),
3223 L2CAP_CONF_REQ
, len
, req
);
3224 l2cap_pi(sk
)->num_conf_req
++;
3225 if (result
!= L2CAP_CONF_SUCCESS
)
3231 sk
->sk_err
= ECONNRESET
;
3232 l2cap_sock_set_timer(sk
, HZ
* 5);
3233 l2cap_send_disconn_req(conn
, sk
, ECONNRESET
);
3240 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_INPUT_DONE
;
3242 if (l2cap_pi(sk
)->conf_state
& L2CAP_CONF_OUTPUT_DONE
) {
3243 set_default_fcs(l2cap_pi(sk
));
3245 sk
->sk_state
= BT_CONNECTED
;
3246 l2cap_pi(sk
)->next_tx_seq
= 0;
3247 l2cap_pi(sk
)->expected_tx_seq
= 0;
3248 __skb_queue_head_init(TX_QUEUE(sk
));
3249 if (l2cap_pi(sk
)->mode
== L2CAP_MODE_ERTM
)
3250 l2cap_ertm_init(sk
);
3252 l2cap_chan_ready(sk
);
3260 static inline int l2cap_disconnect_req(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
3262 struct l2cap_disconn_req
*req
= (struct l2cap_disconn_req
*) data
;
3263 struct l2cap_disconn_rsp rsp
;
3267 scid
= __le16_to_cpu(req
->scid
);
3268 dcid
= __le16_to_cpu(req
->dcid
);
3270 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid
, dcid
);
3272 sk
= l2cap_get_chan_by_scid(&conn
->chan_list
, dcid
);
3276 rsp
.dcid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
3277 rsp
.scid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
3278 l2cap_send_cmd(conn
, cmd
->ident
, L2CAP_DISCONN_RSP
, sizeof(rsp
), &rsp
);
3280 sk
->sk_shutdown
= SHUTDOWN_MASK
;
3282 /* don't delete l2cap channel if sk is owned by user */
3283 if (sock_owned_by_user(sk
)) {
3284 sk
->sk_state
= BT_DISCONN
;
3285 l2cap_sock_clear_timer(sk
);
3286 l2cap_sock_set_timer(sk
, HZ
/ 5);
3291 l2cap_chan_del(sk
, ECONNRESET
);
3294 l2cap_sock_kill(sk
);
3298 static inline int l2cap_disconnect_rsp(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
3300 struct l2cap_disconn_rsp
*rsp
= (struct l2cap_disconn_rsp
*) data
;
3304 scid
= __le16_to_cpu(rsp
->scid
);
3305 dcid
= __le16_to_cpu(rsp
->dcid
);
3307 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid
, scid
);
3309 sk
= l2cap_get_chan_by_scid(&conn
->chan_list
, scid
);
3313 /* don't delete l2cap channel if sk is owned by user */
3314 if (sock_owned_by_user(sk
)) {
3315 sk
->sk_state
= BT_DISCONN
;
3316 l2cap_sock_clear_timer(sk
);
3317 l2cap_sock_set_timer(sk
, HZ
/ 5);
3322 l2cap_chan_del(sk
, 0);
3325 l2cap_sock_kill(sk
);
3329 static inline int l2cap_information_req(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
3331 struct l2cap_info_req
*req
= (struct l2cap_info_req
*) data
;
3334 type
= __le16_to_cpu(req
->type
);
3336 BT_DBG("type 0x%4.4x", type
);
3338 if (type
== L2CAP_IT_FEAT_MASK
) {
3340 u32 feat_mask
= l2cap_feat_mask
;
3341 struct l2cap_info_rsp
*rsp
= (struct l2cap_info_rsp
*) buf
;
3342 rsp
->type
= cpu_to_le16(L2CAP_IT_FEAT_MASK
);
3343 rsp
->result
= cpu_to_le16(L2CAP_IR_SUCCESS
);
3345 feat_mask
|= L2CAP_FEAT_ERTM
| L2CAP_FEAT_STREAMING
3347 put_unaligned_le32(feat_mask
, rsp
->data
);
3348 l2cap_send_cmd(conn
, cmd
->ident
,
3349 L2CAP_INFO_RSP
, sizeof(buf
), buf
);
3350 } else if (type
== L2CAP_IT_FIXED_CHAN
) {
3352 struct l2cap_info_rsp
*rsp
= (struct l2cap_info_rsp
*) buf
;
3353 rsp
->type
= cpu_to_le16(L2CAP_IT_FIXED_CHAN
);
3354 rsp
->result
= cpu_to_le16(L2CAP_IR_SUCCESS
);
3355 memcpy(buf
+ 4, l2cap_fixed_chan
, 8);
3356 l2cap_send_cmd(conn
, cmd
->ident
,
3357 L2CAP_INFO_RSP
, sizeof(buf
), buf
);
3359 struct l2cap_info_rsp rsp
;
3360 rsp
.type
= cpu_to_le16(type
);
3361 rsp
.result
= cpu_to_le16(L2CAP_IR_NOTSUPP
);
3362 l2cap_send_cmd(conn
, cmd
->ident
,
3363 L2CAP_INFO_RSP
, sizeof(rsp
), &rsp
);
3369 static inline int l2cap_information_rsp(struct l2cap_conn
*conn
, struct l2cap_cmd_hdr
*cmd
, u8
*data
)
3371 struct l2cap_info_rsp
*rsp
= (struct l2cap_info_rsp
*) data
;
3374 type
= __le16_to_cpu(rsp
->type
);
3375 result
= __le16_to_cpu(rsp
->result
);
3377 BT_DBG("type 0x%4.4x result 0x%2.2x", type
, result
);
3379 del_timer(&conn
->info_timer
);
3381 if (result
!= L2CAP_IR_SUCCESS
) {
3382 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
3383 conn
->info_ident
= 0;
3385 l2cap_conn_start(conn
);
3390 if (type
== L2CAP_IT_FEAT_MASK
) {
3391 conn
->feat_mask
= get_unaligned_le32(rsp
->data
);
3393 if (conn
->feat_mask
& L2CAP_FEAT_FIXED_CHAN
) {
3394 struct l2cap_info_req req
;
3395 req
.type
= cpu_to_le16(L2CAP_IT_FIXED_CHAN
);
3397 conn
->info_ident
= l2cap_get_ident(conn
);
3399 l2cap_send_cmd(conn
, conn
->info_ident
,
3400 L2CAP_INFO_REQ
, sizeof(req
), &req
);
3402 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
3403 conn
->info_ident
= 0;
3405 l2cap_conn_start(conn
);
3407 } else if (type
== L2CAP_IT_FIXED_CHAN
) {
3408 conn
->info_state
|= L2CAP_INFO_FEAT_MASK_REQ_DONE
;
3409 conn
->info_ident
= 0;
3411 l2cap_conn_start(conn
);
3417 static inline void l2cap_sig_channel(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
3419 u8
*data
= skb
->data
;
3421 struct l2cap_cmd_hdr cmd
;
3424 l2cap_raw_recv(conn
, skb
);
3426 while (len
>= L2CAP_CMD_HDR_SIZE
) {
3428 memcpy(&cmd
, data
, L2CAP_CMD_HDR_SIZE
);
3429 data
+= L2CAP_CMD_HDR_SIZE
;
3430 len
-= L2CAP_CMD_HDR_SIZE
;
3432 cmd_len
= le16_to_cpu(cmd
.len
);
3434 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd
.code
, cmd_len
, cmd
.ident
);
3436 if (cmd_len
> len
|| !cmd
.ident
) {
3437 BT_DBG("corrupted command");
3442 case L2CAP_COMMAND_REJ
:
3443 l2cap_command_rej(conn
, &cmd
, data
);
3446 case L2CAP_CONN_REQ
:
3447 err
= l2cap_connect_req(conn
, &cmd
, data
);
3450 case L2CAP_CONN_RSP
:
3451 err
= l2cap_connect_rsp(conn
, &cmd
, data
);
3454 case L2CAP_CONF_REQ
:
3455 err
= l2cap_config_req(conn
, &cmd
, cmd_len
, data
);
3458 case L2CAP_CONF_RSP
:
3459 err
= l2cap_config_rsp(conn
, &cmd
, data
);
3462 case L2CAP_DISCONN_REQ
:
3463 err
= l2cap_disconnect_req(conn
, &cmd
, data
);
3466 case L2CAP_DISCONN_RSP
:
3467 err
= l2cap_disconnect_rsp(conn
, &cmd
, data
);
3470 case L2CAP_ECHO_REQ
:
3471 l2cap_send_cmd(conn
, cmd
.ident
, L2CAP_ECHO_RSP
, cmd_len
, data
);
3474 case L2CAP_ECHO_RSP
:
3477 case L2CAP_INFO_REQ
:
3478 err
= l2cap_information_req(conn
, &cmd
, data
);
3481 case L2CAP_INFO_RSP
:
3482 err
= l2cap_information_rsp(conn
, &cmd
, data
);
3486 BT_ERR("Unknown signaling command 0x%2.2x", cmd
.code
);
3492 struct l2cap_cmd_rej rej
;
3493 BT_DBG("error %d", err
);
3495 /* FIXME: Map err to a valid reason */
3496 rej
.reason
= cpu_to_le16(0);
3497 l2cap_send_cmd(conn
, cmd
.ident
, L2CAP_COMMAND_REJ
, sizeof(rej
), &rej
);
3507 static int l2cap_check_fcs(struct l2cap_pinfo
*pi
, struct sk_buff
*skb
)
3509 u16 our_fcs
, rcv_fcs
;
3510 int hdr_size
= L2CAP_HDR_SIZE
+ 2;
3512 if (pi
->fcs
== L2CAP_FCS_CRC16
) {
3513 skb_trim(skb
, skb
->len
- 2);
3514 rcv_fcs
= get_unaligned_le16(skb
->data
+ skb
->len
);
3515 our_fcs
= crc16(0, skb
->data
- hdr_size
, skb
->len
+ hdr_size
);
3517 if (our_fcs
!= rcv_fcs
)
3523 static inline void l2cap_send_i_or_rr_or_rnr(struct sock
*sk
)
3525 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3528 pi
->frames_sent
= 0;
3530 control
|= pi
->buffer_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
;
3532 if (pi
->conn_state
& L2CAP_CONN_LOCAL_BUSY
) {
3533 control
|= L2CAP_SUPER_RCV_NOT_READY
;
3534 l2cap_send_sframe(pi
, control
);
3535 pi
->conn_state
|= L2CAP_CONN_RNR_SENT
;
3538 if (pi
->conn_state
& L2CAP_CONN_REMOTE_BUSY
)
3539 l2cap_retransmit_frames(sk
);
3541 l2cap_ertm_send(sk
);
3543 if (!(pi
->conn_state
& L2CAP_CONN_LOCAL_BUSY
) &&
3544 pi
->frames_sent
== 0) {
3545 control
|= L2CAP_SUPER_RCV_READY
;
3546 l2cap_send_sframe(pi
, control
);
3550 static int l2cap_add_to_srej_queue(struct sock
*sk
, struct sk_buff
*skb
, u8 tx_seq
, u8 sar
)
3552 struct sk_buff
*next_skb
;
3553 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3554 int tx_seq_offset
, next_tx_seq_offset
;
3556 bt_cb(skb
)->tx_seq
= tx_seq
;
3557 bt_cb(skb
)->sar
= sar
;
3559 next_skb
= skb_peek(SREJ_QUEUE(sk
));
3561 __skb_queue_tail(SREJ_QUEUE(sk
), skb
);
3565 tx_seq_offset
= (tx_seq
- pi
->buffer_seq
) % 64;
3566 if (tx_seq_offset
< 0)
3567 tx_seq_offset
+= 64;
3570 if (bt_cb(next_skb
)->tx_seq
== tx_seq
)
3573 next_tx_seq_offset
= (bt_cb(next_skb
)->tx_seq
-
3574 pi
->buffer_seq
) % 64;
3575 if (next_tx_seq_offset
< 0)
3576 next_tx_seq_offset
+= 64;
3578 if (next_tx_seq_offset
> tx_seq_offset
) {
3579 __skb_queue_before(SREJ_QUEUE(sk
), next_skb
, skb
);
3583 if (skb_queue_is_last(SREJ_QUEUE(sk
), next_skb
))
3586 } while ((next_skb
= skb_queue_next(SREJ_QUEUE(sk
), next_skb
)));
3588 __skb_queue_tail(SREJ_QUEUE(sk
), skb
);
3593 static int l2cap_ertm_reassembly_sdu(struct sock
*sk
, struct sk_buff
*skb
, u16 control
)
3595 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3596 struct sk_buff
*_skb
;
3599 switch (control
& L2CAP_CTRL_SAR
) {
3600 case L2CAP_SDU_UNSEGMENTED
:
3601 if (pi
->conn_state
& L2CAP_CONN_SAR_SDU
)
3604 err
= sock_queue_rcv_skb(sk
, skb
);
3610 case L2CAP_SDU_START
:
3611 if (pi
->conn_state
& L2CAP_CONN_SAR_SDU
)
3614 pi
->sdu_len
= get_unaligned_le16(skb
->data
);
3616 if (pi
->sdu_len
> pi
->imtu
)
3619 pi
->sdu
= bt_skb_alloc(pi
->sdu_len
, GFP_ATOMIC
);
3623 /* pull sdu_len bytes only after alloc, because of Local Busy
3624 * condition we have to be sure that this will be executed
3625 * only once, i.e., when alloc does not fail */
3628 memcpy(skb_put(pi
->sdu
, skb
->len
), skb
->data
, skb
->len
);
3630 pi
->conn_state
|= L2CAP_CONN_SAR_SDU
;
3631 pi
->partial_sdu_len
= skb
->len
;
3634 case L2CAP_SDU_CONTINUE
:
3635 if (!(pi
->conn_state
& L2CAP_CONN_SAR_SDU
))
3641 pi
->partial_sdu_len
+= skb
->len
;
3642 if (pi
->partial_sdu_len
> pi
->sdu_len
)
3645 memcpy(skb_put(pi
->sdu
, skb
->len
), skb
->data
, skb
->len
);
3650 if (!(pi
->conn_state
& L2CAP_CONN_SAR_SDU
))
3656 if (!(pi
->conn_state
& L2CAP_CONN_SAR_RETRY
)) {
3657 pi
->partial_sdu_len
+= skb
->len
;
3659 if (pi
->partial_sdu_len
> pi
->imtu
)
3662 if (pi
->partial_sdu_len
!= pi
->sdu_len
)
3665 memcpy(skb_put(pi
->sdu
, skb
->len
), skb
->data
, skb
->len
);
3668 _skb
= skb_clone(pi
->sdu
, GFP_ATOMIC
);
3670 pi
->conn_state
|= L2CAP_CONN_SAR_RETRY
;
3674 err
= sock_queue_rcv_skb(sk
, _skb
);
3677 pi
->conn_state
|= L2CAP_CONN_SAR_RETRY
;
3681 pi
->conn_state
&= ~L2CAP_CONN_SAR_RETRY
;
3682 pi
->conn_state
&= ~L2CAP_CONN_SAR_SDU
;
3696 l2cap_send_disconn_req(pi
->conn
, sk
, ECONNRESET
);
3701 static int l2cap_try_push_rx_skb(struct sock
*sk
)
3703 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3704 struct sk_buff
*skb
;
3708 while ((skb
= skb_dequeue(BUSY_QUEUE(sk
)))) {
3709 control
= bt_cb(skb
)->sar
<< L2CAP_CTRL_SAR_SHIFT
;
3710 err
= l2cap_ertm_reassembly_sdu(sk
, skb
, control
);
3712 skb_queue_head(BUSY_QUEUE(sk
), skb
);
3716 pi
->buffer_seq
= (pi
->buffer_seq
+ 1) % 64;
3719 if (!(pi
->conn_state
& L2CAP_CONN_RNR_SENT
))
3722 control
= pi
->buffer_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
;
3723 control
|= L2CAP_SUPER_RCV_READY
| L2CAP_CTRL_POLL
;
3724 l2cap_send_sframe(pi
, control
);
3725 l2cap_pi(sk
)->retry_count
= 1;
3727 del_timer(&pi
->retrans_timer
);
3728 __mod_monitor_timer();
3730 l2cap_pi(sk
)->conn_state
|= L2CAP_CONN_WAIT_F
;
3733 pi
->conn_state
&= ~L2CAP_CONN_LOCAL_BUSY
;
3734 pi
->conn_state
&= ~L2CAP_CONN_RNR_SENT
;
3736 BT_DBG("sk %p, Exit local busy", sk
);
3741 static void l2cap_busy_work(struct work_struct
*work
)
3743 DECLARE_WAITQUEUE(wait
, current
);
3744 struct l2cap_pinfo
*pi
=
3745 container_of(work
, struct l2cap_pinfo
, busy_work
);
3746 struct sock
*sk
= (struct sock
*)pi
;
3747 int n_tries
= 0, timeo
= HZ
/5, err
;
3748 struct sk_buff
*skb
;
3752 add_wait_queue(sk_sleep(sk
), &wait
);
3753 while ((skb
= skb_peek(BUSY_QUEUE(sk
)))) {
3754 set_current_state(TASK_INTERRUPTIBLE
);
3756 if (n_tries
++ > L2CAP_LOCAL_BUSY_TRIES
) {
3758 l2cap_send_disconn_req(pi
->conn
, sk
, EBUSY
);
3765 if (signal_pending(current
)) {
3766 err
= sock_intr_errno(timeo
);
3771 timeo
= schedule_timeout(timeo
);
3774 err
= sock_error(sk
);
3778 if (l2cap_try_push_rx_skb(sk
) == 0)
3782 set_current_state(TASK_RUNNING
);
3783 remove_wait_queue(sk_sleep(sk
), &wait
);
3788 static int l2cap_push_rx_skb(struct sock
*sk
, struct sk_buff
*skb
, u16 control
)
3790 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3793 if (pi
->conn_state
& L2CAP_CONN_LOCAL_BUSY
) {
3794 bt_cb(skb
)->sar
= control
>> L2CAP_CTRL_SAR_SHIFT
;
3795 __skb_queue_tail(BUSY_QUEUE(sk
), skb
);
3796 return l2cap_try_push_rx_skb(sk
);
3801 err
= l2cap_ertm_reassembly_sdu(sk
, skb
, control
);
3803 pi
->buffer_seq
= (pi
->buffer_seq
+ 1) % 64;
3807 /* Busy Condition */
3808 BT_DBG("sk %p, Enter local busy", sk
);
3810 pi
->conn_state
|= L2CAP_CONN_LOCAL_BUSY
;
3811 bt_cb(skb
)->sar
= control
>> L2CAP_CTRL_SAR_SHIFT
;
3812 __skb_queue_tail(BUSY_QUEUE(sk
), skb
);
3814 sctrl
= pi
->buffer_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
;
3815 sctrl
|= L2CAP_SUPER_RCV_NOT_READY
;
3816 l2cap_send_sframe(pi
, sctrl
);
3818 pi
->conn_state
|= L2CAP_CONN_RNR_SENT
;
3820 del_timer(&pi
->ack_timer
);
3822 queue_work(_busy_wq
, &pi
->busy_work
);
3827 static int l2cap_streaming_reassembly_sdu(struct sock
*sk
, struct sk_buff
*skb
, u16 control
)
3829 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3830 struct sk_buff
*_skb
;
3834 * TODO: We have to notify the userland if some data is lost with the
3838 switch (control
& L2CAP_CTRL_SAR
) {
3839 case L2CAP_SDU_UNSEGMENTED
:
3840 if (pi
->conn_state
& L2CAP_CONN_SAR_SDU
) {
3845 err
= sock_queue_rcv_skb(sk
, skb
);
3851 case L2CAP_SDU_START
:
3852 if (pi
->conn_state
& L2CAP_CONN_SAR_SDU
) {
3857 pi
->sdu_len
= get_unaligned_le16(skb
->data
);
3860 if (pi
->sdu_len
> pi
->imtu
) {
3865 pi
->sdu
= bt_skb_alloc(pi
->sdu_len
, GFP_ATOMIC
);
3871 memcpy(skb_put(pi
->sdu
, skb
->len
), skb
->data
, skb
->len
);
3873 pi
->conn_state
|= L2CAP_CONN_SAR_SDU
;
3874 pi
->partial_sdu_len
= skb
->len
;
3878 case L2CAP_SDU_CONTINUE
:
3879 if (!(pi
->conn_state
& L2CAP_CONN_SAR_SDU
))
3882 memcpy(skb_put(pi
->sdu
, skb
->len
), skb
->data
, skb
->len
);
3884 pi
->partial_sdu_len
+= skb
->len
;
3885 if (pi
->partial_sdu_len
> pi
->sdu_len
)
3893 if (!(pi
->conn_state
& L2CAP_CONN_SAR_SDU
))
3896 memcpy(skb_put(pi
->sdu
, skb
->len
), skb
->data
, skb
->len
);
3898 pi
->conn_state
&= ~L2CAP_CONN_SAR_SDU
;
3899 pi
->partial_sdu_len
+= skb
->len
;
3901 if (pi
->partial_sdu_len
> pi
->imtu
)
3904 if (pi
->partial_sdu_len
== pi
->sdu_len
) {
3905 _skb
= skb_clone(pi
->sdu
, GFP_ATOMIC
);
3906 err
= sock_queue_rcv_skb(sk
, _skb
);
3921 static void l2cap_check_srej_gap(struct sock
*sk
, u8 tx_seq
)
3923 struct sk_buff
*skb
;
3926 while ((skb
= skb_peek(SREJ_QUEUE(sk
)))) {
3927 if (bt_cb(skb
)->tx_seq
!= tx_seq
)
3930 skb
= skb_dequeue(SREJ_QUEUE(sk
));
3931 control
= bt_cb(skb
)->sar
<< L2CAP_CTRL_SAR_SHIFT
;
3932 l2cap_ertm_reassembly_sdu(sk
, skb
, control
);
3933 l2cap_pi(sk
)->buffer_seq_srej
=
3934 (l2cap_pi(sk
)->buffer_seq_srej
+ 1) % 64;
3935 tx_seq
= (tx_seq
+ 1) % 64;
3939 static void l2cap_resend_srejframe(struct sock
*sk
, u8 tx_seq
)
3941 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3942 struct srej_list
*l
, *tmp
;
3945 list_for_each_entry_safe(l
, tmp
, SREJ_LIST(sk
), list
) {
3946 if (l
->tx_seq
== tx_seq
) {
3951 control
= L2CAP_SUPER_SELECT_REJECT
;
3952 control
|= l
->tx_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
;
3953 l2cap_send_sframe(pi
, control
);
3955 list_add_tail(&l
->list
, SREJ_LIST(sk
));
3959 static void l2cap_send_srejframe(struct sock
*sk
, u8 tx_seq
)
3961 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3962 struct srej_list
*new;
3965 while (tx_seq
!= pi
->expected_tx_seq
) {
3966 control
= L2CAP_SUPER_SELECT_REJECT
;
3967 control
|= pi
->expected_tx_seq
<< L2CAP_CTRL_REQSEQ_SHIFT
;
3968 l2cap_send_sframe(pi
, control
);
3970 new = kzalloc(sizeof(struct srej_list
), GFP_ATOMIC
);
3971 new->tx_seq
= pi
->expected_tx_seq
;
3972 pi
->expected_tx_seq
= (pi
->expected_tx_seq
+ 1) % 64;
3973 list_add_tail(&new->list
, SREJ_LIST(sk
));
3975 pi
->expected_tx_seq
= (pi
->expected_tx_seq
+ 1) % 64;
3978 static inline int l2cap_data_channel_iframe(struct sock
*sk
, u16 rx_control
, struct sk_buff
*skb
)
3980 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
3981 u8 tx_seq
= __get_txseq(rx_control
);
3982 u8 req_seq
= __get_reqseq(rx_control
);
3983 u8 sar
= rx_control
>> L2CAP_CTRL_SAR_SHIFT
;
3984 int tx_seq_offset
, expected_tx_seq_offset
;
3985 int num_to_ack
= (pi
->tx_win
/6) + 1;
3988 BT_DBG("sk %p len %d tx_seq %d rx_control 0x%4.4x", sk
, skb
->len
, tx_seq
,
3991 if (L2CAP_CTRL_FINAL
& rx_control
&&
3992 l2cap_pi(sk
)->conn_state
& L2CAP_CONN_WAIT_F
) {
3993 del_timer(&pi
->monitor_timer
);
3994 if (pi
->unacked_frames
> 0)
3995 __mod_retrans_timer();
3996 pi
->conn_state
&= ~L2CAP_CONN_WAIT_F
;
3999 pi
->expected_ack_seq
= req_seq
;
4000 l2cap_drop_acked_frames(sk
);
4002 if (tx_seq
== pi
->expected_tx_seq
)
4005 tx_seq_offset
= (tx_seq
- pi
->buffer_seq
) % 64;
4006 if (tx_seq_offset
< 0)
4007 tx_seq_offset
+= 64;
4009 /* invalid tx_seq */
4010 if (tx_seq_offset
>= pi
->tx_win
) {
4011 l2cap_send_disconn_req(pi
->conn
, sk
, ECONNRESET
);
4015 if (pi
->conn_state
== L2CAP_CONN_LOCAL_BUSY
)
4018 if (pi
->conn_state
& L2CAP_CONN_SREJ_SENT
) {
4019 struct srej_list
*first
;
4021 first
= list_first_entry(SREJ_LIST(sk
),
4022 struct srej_list
, list
);
4023 if (tx_seq
== first
->tx_seq
) {
4024 l2cap_add_to_srej_queue(sk
, skb
, tx_seq
, sar
);
4025 l2cap_check_srej_gap(sk
, tx_seq
);
4027 list_del(&first
->list
);
4030 if (list_empty(SREJ_LIST(sk
))) {
4031 pi
->buffer_seq
= pi
->buffer_seq_srej
;
4032 pi
->conn_state
&= ~L2CAP_CONN_SREJ_SENT
;
4034 BT_DBG("sk %p, Exit SREJ_SENT", sk
);
4037 struct srej_list
*l
;
4039 /* duplicated tx_seq */
4040 if (l2cap_add_to_srej_queue(sk
, skb
, tx_seq
, sar
) < 0)
4043 list_for_each_entry(l
, SREJ_LIST(sk
), list
) {
4044 if (l
->tx_seq
== tx_seq
) {
4045 l2cap_resend_srejframe(sk
, tx_seq
);
4049 l2cap_send_srejframe(sk
, tx_seq
);
4052 expected_tx_seq_offset
=
4053 (pi
->expected_tx_seq
- pi
->buffer_seq
) % 64;
4054 if (expected_tx_seq_offset
< 0)
4055 expected_tx_seq_offset
+= 64;
4057 /* duplicated tx_seq */
4058 if (tx_seq_offset
< expected_tx_seq_offset
)
4061 pi
->conn_state
|= L2CAP_CONN_SREJ_SENT
;
4063 BT_DBG("sk %p, Enter SREJ", sk
);
4065 INIT_LIST_HEAD(SREJ_LIST(sk
));
4066 pi
->buffer_seq_srej
= pi
->buffer_seq
;
4068 __skb_queue_head_init(SREJ_QUEUE(sk
));
4069 __skb_queue_head_init(BUSY_QUEUE(sk
));
4070 l2cap_add_to_srej_queue(sk
, skb
, tx_seq
, sar
);
4072 pi
->conn_state
|= L2CAP_CONN_SEND_PBIT
;
4074 l2cap_send_srejframe(sk
, tx_seq
);
4076 del_timer(&pi
->ack_timer
);
4081 pi
->expected_tx_seq
= (pi
->expected_tx_seq
+ 1) % 64;
4083 if (pi
->conn_state
& L2CAP_CONN_SREJ_SENT
) {
4084 bt_cb(skb
)->tx_seq
= tx_seq
;
4085 bt_cb(skb
)->sar
= sar
;
4086 __skb_queue_tail(SREJ_QUEUE(sk
), skb
);
4090 err
= l2cap_push_rx_skb(sk
, skb
, rx_control
);
4094 if (rx_control
& L2CAP_CTRL_FINAL
) {
4095 if (pi
->conn_state
& L2CAP_CONN_REJ_ACT
)
4096 pi
->conn_state
&= ~L2CAP_CONN_REJ_ACT
;
4098 l2cap_retransmit_frames(sk
);
4103 pi
->num_acked
= (pi
->num_acked
+ 1) % num_to_ack
;
4104 if (pi
->num_acked
== num_to_ack
- 1)
4114 static inline void l2cap_data_channel_rrframe(struct sock
*sk
, u16 rx_control
)
4116 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
4118 BT_DBG("sk %p, req_seq %d ctrl 0x%4.4x", sk
, __get_reqseq(rx_control
),
4121 pi
->expected_ack_seq
= __get_reqseq(rx_control
);
4122 l2cap_drop_acked_frames(sk
);
4124 if (rx_control
& L2CAP_CTRL_POLL
) {
4125 pi
->conn_state
|= L2CAP_CONN_SEND_FBIT
;
4126 if (pi
->conn_state
& L2CAP_CONN_SREJ_SENT
) {
4127 if ((pi
->conn_state
& L2CAP_CONN_REMOTE_BUSY
) &&
4128 (pi
->unacked_frames
> 0))
4129 __mod_retrans_timer();
4131 pi
->conn_state
&= ~L2CAP_CONN_REMOTE_BUSY
;
4132 l2cap_send_srejtail(sk
);
4134 l2cap_send_i_or_rr_or_rnr(sk
);
4137 } else if (rx_control
& L2CAP_CTRL_FINAL
) {
4138 pi
->conn_state
&= ~L2CAP_CONN_REMOTE_BUSY
;
4140 if (pi
->conn_state
& L2CAP_CONN_REJ_ACT
)
4141 pi
->conn_state
&= ~L2CAP_CONN_REJ_ACT
;
4143 l2cap_retransmit_frames(sk
);
4146 if ((pi
->conn_state
& L2CAP_CONN_REMOTE_BUSY
) &&
4147 (pi
->unacked_frames
> 0))
4148 __mod_retrans_timer();
4150 pi
->conn_state
&= ~L2CAP_CONN_REMOTE_BUSY
;
4151 if (pi
->conn_state
& L2CAP_CONN_SREJ_SENT
)
4154 l2cap_ertm_send(sk
);
4158 static inline void l2cap_data_channel_rejframe(struct sock
*sk
, u16 rx_control
)
4160 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
4161 u8 tx_seq
= __get_reqseq(rx_control
);
4163 BT_DBG("sk %p, req_seq %d ctrl 0x%4.4x", sk
, tx_seq
, rx_control
);
4165 pi
->conn_state
&= ~L2CAP_CONN_REMOTE_BUSY
;
4167 pi
->expected_ack_seq
= tx_seq
;
4168 l2cap_drop_acked_frames(sk
);
4170 if (rx_control
& L2CAP_CTRL_FINAL
) {
4171 if (pi
->conn_state
& L2CAP_CONN_REJ_ACT
)
4172 pi
->conn_state
&= ~L2CAP_CONN_REJ_ACT
;
4174 l2cap_retransmit_frames(sk
);
4176 l2cap_retransmit_frames(sk
);
4178 if (pi
->conn_state
& L2CAP_CONN_WAIT_F
)
4179 pi
->conn_state
|= L2CAP_CONN_REJ_ACT
;
4182 static inline void l2cap_data_channel_srejframe(struct sock
*sk
, u16 rx_control
)
4184 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
4185 u8 tx_seq
= __get_reqseq(rx_control
);
4187 BT_DBG("sk %p, req_seq %d ctrl 0x%4.4x", sk
, tx_seq
, rx_control
);
4189 pi
->conn_state
&= ~L2CAP_CONN_REMOTE_BUSY
;
4191 if (rx_control
& L2CAP_CTRL_POLL
) {
4192 pi
->expected_ack_seq
= tx_seq
;
4193 l2cap_drop_acked_frames(sk
);
4195 pi
->conn_state
|= L2CAP_CONN_SEND_FBIT
;
4196 l2cap_retransmit_one_frame(sk
, tx_seq
);
4198 l2cap_ertm_send(sk
);
4200 if (pi
->conn_state
& L2CAP_CONN_WAIT_F
) {
4201 pi
->srej_save_reqseq
= tx_seq
;
4202 pi
->conn_state
|= L2CAP_CONN_SREJ_ACT
;
4204 } else if (rx_control
& L2CAP_CTRL_FINAL
) {
4205 if ((pi
->conn_state
& L2CAP_CONN_SREJ_ACT
) &&
4206 pi
->srej_save_reqseq
== tx_seq
)
4207 pi
->conn_state
&= ~L2CAP_CONN_SREJ_ACT
;
4209 l2cap_retransmit_one_frame(sk
, tx_seq
);
4211 l2cap_retransmit_one_frame(sk
, tx_seq
);
4212 if (pi
->conn_state
& L2CAP_CONN_WAIT_F
) {
4213 pi
->srej_save_reqseq
= tx_seq
;
4214 pi
->conn_state
|= L2CAP_CONN_SREJ_ACT
;
4219 static inline void l2cap_data_channel_rnrframe(struct sock
*sk
, u16 rx_control
)
4221 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
4222 u8 tx_seq
= __get_reqseq(rx_control
);
4224 BT_DBG("sk %p, req_seq %d ctrl 0x%4.4x", sk
, tx_seq
, rx_control
);
4226 pi
->conn_state
|= L2CAP_CONN_REMOTE_BUSY
;
4227 pi
->expected_ack_seq
= tx_seq
;
4228 l2cap_drop_acked_frames(sk
);
4230 if (rx_control
& L2CAP_CTRL_POLL
)
4231 pi
->conn_state
|= L2CAP_CONN_SEND_FBIT
;
4233 if (!(pi
->conn_state
& L2CAP_CONN_SREJ_SENT
)) {
4234 del_timer(&pi
->retrans_timer
);
4235 if (rx_control
& L2CAP_CTRL_POLL
)
4236 l2cap_send_rr_or_rnr(pi
, L2CAP_CTRL_FINAL
);
4240 if (rx_control
& L2CAP_CTRL_POLL
)
4241 l2cap_send_srejtail(sk
);
4243 l2cap_send_sframe(pi
, L2CAP_SUPER_RCV_READY
);
4246 static inline int l2cap_data_channel_sframe(struct sock
*sk
, u16 rx_control
, struct sk_buff
*skb
)
4248 BT_DBG("sk %p rx_control 0x%4.4x len %d", sk
, rx_control
, skb
->len
);
4250 if (L2CAP_CTRL_FINAL
& rx_control
&&
4251 l2cap_pi(sk
)->conn_state
& L2CAP_CONN_WAIT_F
) {
4252 del_timer(&l2cap_pi(sk
)->monitor_timer
);
4253 if (l2cap_pi(sk
)->unacked_frames
> 0)
4254 __mod_retrans_timer();
4255 l2cap_pi(sk
)->conn_state
&= ~L2CAP_CONN_WAIT_F
;
4258 switch (rx_control
& L2CAP_CTRL_SUPERVISE
) {
4259 case L2CAP_SUPER_RCV_READY
:
4260 l2cap_data_channel_rrframe(sk
, rx_control
);
4263 case L2CAP_SUPER_REJECT
:
4264 l2cap_data_channel_rejframe(sk
, rx_control
);
4267 case L2CAP_SUPER_SELECT_REJECT
:
4268 l2cap_data_channel_srejframe(sk
, rx_control
);
4271 case L2CAP_SUPER_RCV_NOT_READY
:
4272 l2cap_data_channel_rnrframe(sk
, rx_control
);
4280 static int l2cap_ertm_data_rcv(struct sock
*sk
, struct sk_buff
*skb
)
4282 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
4285 int len
, next_tx_seq_offset
, req_seq_offset
;
4287 control
= get_unaligned_le16(skb
->data
);
4292 * We can just drop the corrupted I-frame here.
4293 * Receiver will miss it and start proper recovery
4294 * procedures and ask retransmission.
4296 if (l2cap_check_fcs(pi
, skb
))
4299 if (__is_sar_start(control
) && __is_iframe(control
))
4302 if (pi
->fcs
== L2CAP_FCS_CRC16
)
4305 if (len
> pi
->mps
) {
4306 l2cap_send_disconn_req(pi
->conn
, sk
, ECONNRESET
);
4310 req_seq
= __get_reqseq(control
);
4311 req_seq_offset
= (req_seq
- pi
->expected_ack_seq
) % 64;
4312 if (req_seq_offset
< 0)
4313 req_seq_offset
+= 64;
4315 next_tx_seq_offset
=
4316 (pi
->next_tx_seq
- pi
->expected_ack_seq
) % 64;
4317 if (next_tx_seq_offset
< 0)
4318 next_tx_seq_offset
+= 64;
4320 /* check for invalid req-seq */
4321 if (req_seq_offset
> next_tx_seq_offset
) {
4322 l2cap_send_disconn_req(pi
->conn
, sk
, ECONNRESET
);
4326 if (__is_iframe(control
)) {
4328 l2cap_send_disconn_req(pi
->conn
, sk
, ECONNRESET
);
4332 l2cap_data_channel_iframe(sk
, control
, skb
);
4336 l2cap_send_disconn_req(pi
->conn
, sk
, ECONNRESET
);
4340 l2cap_data_channel_sframe(sk
, control
, skb
);
4350 static inline int l2cap_data_channel(struct l2cap_conn
*conn
, u16 cid
, struct sk_buff
*skb
)
4353 struct l2cap_pinfo
*pi
;
4358 sk
= l2cap_get_chan_by_scid(&conn
->chan_list
, cid
);
4360 BT_DBG("unknown cid 0x%4.4x", cid
);
4366 BT_DBG("sk %p, len %d", sk
, skb
->len
);
4368 if (sk
->sk_state
!= BT_CONNECTED
)
4372 case L2CAP_MODE_BASIC
:
4373 /* If socket recv buffers overflows we drop data here
4374 * which is *bad* because L2CAP has to be reliable.
4375 * But we don't have any other choice. L2CAP doesn't
4376 * provide flow control mechanism. */
4378 if (pi
->imtu
< skb
->len
)
4381 if (!sock_queue_rcv_skb(sk
, skb
))
4385 case L2CAP_MODE_ERTM
:
4386 if (!sock_owned_by_user(sk
)) {
4387 l2cap_ertm_data_rcv(sk
, skb
);
4389 if (sk_add_backlog(sk
, skb
))
4395 case L2CAP_MODE_STREAMING
:
4396 control
= get_unaligned_le16(skb
->data
);
4400 if (l2cap_check_fcs(pi
, skb
))
4403 if (__is_sar_start(control
))
4406 if (pi
->fcs
== L2CAP_FCS_CRC16
)
4409 if (len
> pi
->mps
|| len
< 0 || __is_sframe(control
))
4412 tx_seq
= __get_txseq(control
);
4414 if (pi
->expected_tx_seq
== tx_seq
)
4415 pi
->expected_tx_seq
= (pi
->expected_tx_seq
+ 1) % 64;
4417 pi
->expected_tx_seq
= (tx_seq
+ 1) % 64;
4419 l2cap_streaming_reassembly_sdu(sk
, skb
, control
);
4424 BT_DBG("sk %p: bad mode 0x%2.2x", sk
, pi
->mode
);
4438 static inline int l2cap_conless_channel(struct l2cap_conn
*conn
, __le16 psm
, struct sk_buff
*skb
)
4442 sk
= l2cap_get_sock_by_psm(0, psm
, conn
->src
);
4448 BT_DBG("sk %p, len %d", sk
, skb
->len
);
4450 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_CONNECTED
)
4453 if (l2cap_pi(sk
)->imtu
< skb
->len
)
4456 if (!sock_queue_rcv_skb(sk
, skb
))
4468 static void l2cap_recv_frame(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
4470 struct l2cap_hdr
*lh
= (void *) skb
->data
;
4474 skb_pull(skb
, L2CAP_HDR_SIZE
);
4475 cid
= __le16_to_cpu(lh
->cid
);
4476 len
= __le16_to_cpu(lh
->len
);
4478 if (len
!= skb
->len
) {
4483 BT_DBG("len %d, cid 0x%4.4x", len
, cid
);
4486 case L2CAP_CID_SIGNALING
:
4487 l2cap_sig_channel(conn
, skb
);
4490 case L2CAP_CID_CONN_LESS
:
4491 psm
= get_unaligned_le16(skb
->data
);
4493 l2cap_conless_channel(conn
, psm
, skb
);
4497 l2cap_data_channel(conn
, cid
, skb
);
4502 /* ---- L2CAP interface with lower layer (HCI) ---- */
4504 static int l2cap_connect_ind(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
4506 int exact
= 0, lm1
= 0, lm2
= 0;
4507 register struct sock
*sk
;
4508 struct hlist_node
*node
;
4510 if (type
!= ACL_LINK
)
4513 BT_DBG("hdev %s, bdaddr %s", hdev
->name
, batostr(bdaddr
));
4515 /* Find listening sockets and check their link_mode */
4516 read_lock(&l2cap_sk_list
.lock
);
4517 sk_for_each(sk
, node
, &l2cap_sk_list
.head
) {
4518 if (sk
->sk_state
!= BT_LISTEN
)
4521 if (!bacmp(&bt_sk(sk
)->src
, &hdev
->bdaddr
)) {
4522 lm1
|= HCI_LM_ACCEPT
;
4523 if (l2cap_pi(sk
)->role_switch
)
4524 lm1
|= HCI_LM_MASTER
;
4526 } else if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
)) {
4527 lm2
|= HCI_LM_ACCEPT
;
4528 if (l2cap_pi(sk
)->role_switch
)
4529 lm2
|= HCI_LM_MASTER
;
4532 read_unlock(&l2cap_sk_list
.lock
);
4534 return exact
? lm1
: lm2
;
4537 static int l2cap_connect_cfm(struct hci_conn
*hcon
, u8 status
)
4539 struct l2cap_conn
*conn
;
4541 BT_DBG("hcon %p bdaddr %s status %d", hcon
, batostr(&hcon
->dst
), status
);
4543 if (hcon
->type
!= ACL_LINK
)
4547 conn
= l2cap_conn_add(hcon
, status
);
4549 l2cap_conn_ready(conn
);
4551 l2cap_conn_del(hcon
, bt_err(status
));
4556 static int l2cap_disconn_ind(struct hci_conn
*hcon
)
4558 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
4560 BT_DBG("hcon %p", hcon
);
4562 if (hcon
->type
!= ACL_LINK
|| !conn
)
4565 return conn
->disc_reason
;
4568 static int l2cap_disconn_cfm(struct hci_conn
*hcon
, u8 reason
)
4570 BT_DBG("hcon %p reason %d", hcon
, reason
);
4572 if (hcon
->type
!= ACL_LINK
)
4575 l2cap_conn_del(hcon
, bt_err(reason
));
4580 static inline void l2cap_check_encryption(struct sock
*sk
, u8 encrypt
)
4582 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
)
4585 if (encrypt
== 0x00) {
4586 if (l2cap_pi(sk
)->sec_level
== BT_SECURITY_MEDIUM
) {
4587 l2cap_sock_clear_timer(sk
);
4588 l2cap_sock_set_timer(sk
, HZ
* 5);
4589 } else if (l2cap_pi(sk
)->sec_level
== BT_SECURITY_HIGH
)
4590 __l2cap_sock_close(sk
, ECONNREFUSED
);
4592 if (l2cap_pi(sk
)->sec_level
== BT_SECURITY_MEDIUM
)
4593 l2cap_sock_clear_timer(sk
);
4597 static int l2cap_security_cfm(struct hci_conn
*hcon
, u8 status
, u8 encrypt
)
4599 struct l2cap_chan_list
*l
;
4600 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
4606 l
= &conn
->chan_list
;
4608 BT_DBG("conn %p", conn
);
4610 read_lock(&l
->lock
);
4612 for (sk
= l
->head
; sk
; sk
= l2cap_pi(sk
)->next_c
) {
4615 if (l2cap_pi(sk
)->conf_state
& L2CAP_CONF_CONNECT_PEND
) {
4620 if (!status
&& (sk
->sk_state
== BT_CONNECTED
||
4621 sk
->sk_state
== BT_CONFIG
)) {
4622 l2cap_check_encryption(sk
, encrypt
);
4627 if (sk
->sk_state
== BT_CONNECT
) {
4629 struct l2cap_conn_req req
;
4630 req
.scid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
4631 req
.psm
= l2cap_pi(sk
)->psm
;
4633 l2cap_pi(sk
)->ident
= l2cap_get_ident(conn
);
4634 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_CONNECT_PEND
;
4636 l2cap_send_cmd(conn
, l2cap_pi(sk
)->ident
,
4637 L2CAP_CONN_REQ
, sizeof(req
), &req
);
4639 l2cap_sock_clear_timer(sk
);
4640 l2cap_sock_set_timer(sk
, HZ
/ 10);
4642 } else if (sk
->sk_state
== BT_CONNECT2
) {
4643 struct l2cap_conn_rsp rsp
;
4647 sk
->sk_state
= BT_CONFIG
;
4648 result
= L2CAP_CR_SUCCESS
;
4650 sk
->sk_state
= BT_DISCONN
;
4651 l2cap_sock_set_timer(sk
, HZ
/ 10);
4652 result
= L2CAP_CR_SEC_BLOCK
;
4655 rsp
.scid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
4656 rsp
.dcid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
4657 rsp
.result
= cpu_to_le16(result
);
4658 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
4659 l2cap_send_cmd(conn
, l2cap_pi(sk
)->ident
,
4660 L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
4666 read_unlock(&l
->lock
);
4671 static int l2cap_recv_acldata(struct hci_conn
*hcon
, struct sk_buff
*skb
, u16 flags
)
4673 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
4675 if (!conn
&& !(conn
= l2cap_conn_add(hcon
, 0)))
4678 BT_DBG("conn %p len %d flags 0x%x", conn
, skb
->len
, flags
);
4680 if (flags
& ACL_START
) {
4681 struct l2cap_hdr
*hdr
;
4687 BT_ERR("Unexpected start frame (len %d)", skb
->len
);
4688 kfree_skb(conn
->rx_skb
);
4689 conn
->rx_skb
= NULL
;
4691 l2cap_conn_unreliable(conn
, ECOMM
);
4694 /* Start fragment always begin with Basic L2CAP header */
4695 if (skb
->len
< L2CAP_HDR_SIZE
) {
4696 BT_ERR("Frame is too short (len %d)", skb
->len
);
4697 l2cap_conn_unreliable(conn
, ECOMM
);
4701 hdr
= (struct l2cap_hdr
*) skb
->data
;
4702 len
= __le16_to_cpu(hdr
->len
) + L2CAP_HDR_SIZE
;
4703 cid
= __le16_to_cpu(hdr
->cid
);
4705 if (len
== skb
->len
) {
4706 /* Complete frame received */
4707 l2cap_recv_frame(conn
, skb
);
4711 BT_DBG("Start: total len %d, frag len %d", len
, skb
->len
);
4713 if (skb
->len
> len
) {
4714 BT_ERR("Frame is too long (len %d, expected len %d)",
4716 l2cap_conn_unreliable(conn
, ECOMM
);
4720 sk
= l2cap_get_chan_by_scid(&conn
->chan_list
, cid
);
4722 if (sk
&& l2cap_pi(sk
)->imtu
< len
- L2CAP_HDR_SIZE
) {
4723 BT_ERR("Frame exceeding recv MTU (len %d, MTU %d)",
4724 len
, l2cap_pi(sk
)->imtu
);
4726 l2cap_conn_unreliable(conn
, ECOMM
);
4733 /* Allocate skb for the complete frame (with header) */
4734 conn
->rx_skb
= bt_skb_alloc(len
, GFP_ATOMIC
);
4738 skb_copy_from_linear_data(skb
, skb_put(conn
->rx_skb
, skb
->len
),
4740 conn
->rx_len
= len
- skb
->len
;
4742 BT_DBG("Cont: frag len %d (expecting %d)", skb
->len
, conn
->rx_len
);
4744 if (!conn
->rx_len
) {
4745 BT_ERR("Unexpected continuation frame (len %d)", skb
->len
);
4746 l2cap_conn_unreliable(conn
, ECOMM
);
4750 if (skb
->len
> conn
->rx_len
) {
4751 BT_ERR("Fragment is too long (len %d, expected %d)",
4752 skb
->len
, conn
->rx_len
);
4753 kfree_skb(conn
->rx_skb
);
4754 conn
->rx_skb
= NULL
;
4756 l2cap_conn_unreliable(conn
, ECOMM
);
4760 skb_copy_from_linear_data(skb
, skb_put(conn
->rx_skb
, skb
->len
),
4762 conn
->rx_len
-= skb
->len
;
4764 if (!conn
->rx_len
) {
4765 /* Complete frame received */
4766 l2cap_recv_frame(conn
, conn
->rx_skb
);
4767 conn
->rx_skb
= NULL
;
4776 static int l2cap_debugfs_show(struct seq_file
*f
, void *p
)
4779 struct hlist_node
*node
;
4781 read_lock_bh(&l2cap_sk_list
.lock
);
4783 sk_for_each(sk
, node
, &l2cap_sk_list
.head
) {
4784 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
4786 seq_printf(f
, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d\n",
4787 batostr(&bt_sk(sk
)->src
),
4788 batostr(&bt_sk(sk
)->dst
),
4789 sk
->sk_state
, __le16_to_cpu(pi
->psm
),
4791 pi
->imtu
, pi
->omtu
, pi
->sec_level
);
4794 read_unlock_bh(&l2cap_sk_list
.lock
);
4799 static int l2cap_debugfs_open(struct inode
*inode
, struct file
*file
)
4801 return single_open(file
, l2cap_debugfs_show
, inode
->i_private
);
4804 static const struct file_operations l2cap_debugfs_fops
= {
4805 .open
= l2cap_debugfs_open
,
4807 .llseek
= seq_lseek
,
4808 .release
= single_release
,
4811 static struct dentry
*l2cap_debugfs
;
4813 static const struct proto_ops l2cap_sock_ops
= {
4814 .family
= PF_BLUETOOTH
,
4815 .owner
= THIS_MODULE
,
4816 .release
= l2cap_sock_release
,
4817 .bind
= l2cap_sock_bind
,
4818 .connect
= l2cap_sock_connect
,
4819 .listen
= l2cap_sock_listen
,
4820 .accept
= l2cap_sock_accept
,
4821 .getname
= l2cap_sock_getname
,
4822 .sendmsg
= l2cap_sock_sendmsg
,
4823 .recvmsg
= l2cap_sock_recvmsg
,
4824 .poll
= bt_sock_poll
,
4825 .ioctl
= bt_sock_ioctl
,
4826 .mmap
= sock_no_mmap
,
4827 .socketpair
= sock_no_socketpair
,
4828 .shutdown
= l2cap_sock_shutdown
,
4829 .setsockopt
= l2cap_sock_setsockopt
,
4830 .getsockopt
= l2cap_sock_getsockopt
4833 static const struct net_proto_family l2cap_sock_family_ops
= {
4834 .family
= PF_BLUETOOTH
,
4835 .owner
= THIS_MODULE
,
4836 .create
= l2cap_sock_create
,
4839 static struct hci_proto l2cap_hci_proto
= {
4841 .id
= HCI_PROTO_L2CAP
,
4842 .connect_ind
= l2cap_connect_ind
,
4843 .connect_cfm
= l2cap_connect_cfm
,
4844 .disconn_ind
= l2cap_disconn_ind
,
4845 .disconn_cfm
= l2cap_disconn_cfm
,
4846 .security_cfm
= l2cap_security_cfm
,
4847 .recv_acldata
= l2cap_recv_acldata
4850 static int __init
l2cap_init(void)
4854 err
= proto_register(&l2cap_proto
, 0);
4858 _busy_wq
= create_singlethread_workqueue("l2cap");
4860 proto_unregister(&l2cap_proto
);
4864 err
= bt_sock_register(BTPROTO_L2CAP
, &l2cap_sock_family_ops
);
4866 BT_ERR("L2CAP socket registration failed");
4870 err
= hci_register_proto(&l2cap_hci_proto
);
4872 BT_ERR("L2CAP protocol registration failed");
4873 bt_sock_unregister(BTPROTO_L2CAP
);
4878 l2cap_debugfs
= debugfs_create_file("l2cap", 0444,
4879 bt_debugfs
, NULL
, &l2cap_debugfs_fops
);
4881 BT_ERR("Failed to create L2CAP debug file");
4884 BT_INFO("L2CAP ver %s", VERSION
);
4885 BT_INFO("L2CAP socket layer initialized");
4890 destroy_workqueue(_busy_wq
);
4891 proto_unregister(&l2cap_proto
);
4895 static void __exit
l2cap_exit(void)
4897 debugfs_remove(l2cap_debugfs
);
4899 flush_workqueue(_busy_wq
);
4900 destroy_workqueue(_busy_wq
);
4902 if (bt_sock_unregister(BTPROTO_L2CAP
) < 0)
4903 BT_ERR("L2CAP socket unregistration failed");
4905 if (hci_unregister_proto(&l2cap_hci_proto
) < 0)
4906 BT_ERR("L2CAP protocol unregistration failed");
4908 proto_unregister(&l2cap_proto
);
4911 void l2cap_load(void)
4913 /* Dummy function to trigger automatic L2CAP module loading by
4914 * other modules that use L2CAP sockets but don't use any other
4915 * symbols from it. */
4917 EXPORT_SYMBOL(l2cap_load
);
4919 module_init(l2cap_init
);
4920 module_exit(l2cap_exit
);
4922 module_param(disable_ertm
, bool, 0644);
4923 MODULE_PARM_DESC(disable_ertm
, "Disable enhanced retransmission mode");
4925 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4926 MODULE_DESCRIPTION("Bluetooth L2CAP ver " VERSION
);
4927 MODULE_VERSION(VERSION
);
4928 MODULE_LICENSE("GPL");
4929 MODULE_ALIAS("bt-proto-0");