2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
5 Copyright (C) 2010 Google Inc.
6 Copyright (C) 2011 ProFUSION Embedded Systems
8 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation;
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
18 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25 SOFTWARE IS DISCLAIMED.
28 /* Bluetooth L2CAP sockets. */
30 #include <linux/export.h>
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 #include <net/bluetooth/l2cap.h>
35 #include <net/bluetooth/smp.h>
37 static const struct proto_ops l2cap_sock_ops
;
38 static void l2cap_sock_init(struct sock
*sk
, struct sock
*parent
);
39 static struct sock
*l2cap_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
);
41 static int l2cap_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int alen
)
43 struct sock
*sk
= sock
->sk
;
44 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
45 struct sockaddr_l2 la
;
50 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
53 memset(&la
, 0, sizeof(la
));
54 len
= min_t(unsigned int, sizeof(la
), alen
);
55 memcpy(&la
, addr
, len
);
57 if (la
.l2_cid
&& la
.l2_psm
)
62 if (sk
->sk_state
!= BT_OPEN
) {
68 __u16 psm
= __le16_to_cpu(la
.l2_psm
);
70 /* PSM must be odd and lsb of upper byte must be 0 */
71 if ((psm
& 0x0101) != 0x0001) {
76 /* Restrict usage of well-known PSMs */
77 if (psm
< 0x1001 && !capable(CAP_NET_BIND_SERVICE
)) {
84 err
= l2cap_add_scid(chan
, __le16_to_cpu(la
.l2_cid
));
86 err
= l2cap_add_psm(chan
, &la
.l2_bdaddr
, la
.l2_psm
);
91 if (__le16_to_cpu(la
.l2_psm
) == L2CAP_PSM_SDP
||
92 __le16_to_cpu(la
.l2_psm
) == L2CAP_PSM_RFCOMM
)
93 chan
->sec_level
= BT_SECURITY_SDP
;
95 bacpy(&bt_sk(sk
)->src
, &la
.l2_bdaddr
);
97 chan
->state
= BT_BOUND
;
98 sk
->sk_state
= BT_BOUND
;
105 static int l2cap_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
107 struct sock
*sk
= sock
->sk
;
108 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
109 struct sockaddr_l2 la
;
114 if (!addr
|| alen
< sizeof(addr
->sa_family
) ||
115 addr
->sa_family
!= AF_BLUETOOTH
)
118 memset(&la
, 0, sizeof(la
));
119 len
= min_t(unsigned int, sizeof(la
), alen
);
120 memcpy(&la
, addr
, len
);
122 if (la
.l2_cid
&& la
.l2_psm
)
125 err
= l2cap_chan_connect(chan
, la
.l2_psm
, __le16_to_cpu(la
.l2_cid
),
126 &la
.l2_bdaddr
, la
.l2_bdaddr_type
);
132 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
133 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
140 static int l2cap_sock_listen(struct socket
*sock
, int backlog
)
142 struct sock
*sk
= sock
->sk
;
143 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
146 BT_DBG("sk %p backlog %d", sk
, backlog
);
150 if (sk
->sk_state
!= BT_BOUND
) {
155 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
) {
160 switch (chan
->mode
) {
161 case L2CAP_MODE_BASIC
:
163 case L2CAP_MODE_ERTM
:
164 case L2CAP_MODE_STREAMING
:
173 sk
->sk_max_ack_backlog
= backlog
;
174 sk
->sk_ack_backlog
= 0;
176 chan
->state
= BT_LISTEN
;
177 sk
->sk_state
= BT_LISTEN
;
184 static int l2cap_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
186 DECLARE_WAITQUEUE(wait
, current
);
187 struct sock
*sk
= sock
->sk
, *nsk
;
191 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
193 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
195 BT_DBG("sk %p timeo %ld", sk
, timeo
);
197 /* Wait for an incoming connection. (wake-one). */
198 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
200 set_current_state(TASK_INTERRUPTIBLE
);
202 if (sk
->sk_state
!= BT_LISTEN
) {
207 nsk
= bt_accept_dequeue(sk
, newsock
);
216 if (signal_pending(current
)) {
217 err
= sock_intr_errno(timeo
);
222 timeo
= schedule_timeout(timeo
);
223 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
225 __set_current_state(TASK_RUNNING
);
226 remove_wait_queue(sk_sleep(sk
), &wait
);
231 newsock
->state
= SS_CONNECTED
;
233 BT_DBG("new socket %p", nsk
);
240 static int l2cap_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
242 struct sockaddr_l2
*la
= (struct sockaddr_l2
*) addr
;
243 struct sock
*sk
= sock
->sk
;
244 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
246 BT_DBG("sock %p, sk %p", sock
, sk
);
248 memset(la
, 0, sizeof(struct sockaddr_l2
));
249 addr
->sa_family
= AF_BLUETOOTH
;
250 *len
= sizeof(struct sockaddr_l2
);
253 la
->l2_psm
= chan
->psm
;
254 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->dst
);
255 la
->l2_cid
= cpu_to_le16(chan
->dcid
);
257 la
->l2_psm
= chan
->sport
;
258 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->src
);
259 la
->l2_cid
= cpu_to_le16(chan
->scid
);
265 static int l2cap_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
267 struct sock
*sk
= sock
->sk
;
268 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
269 struct l2cap_options opts
;
270 struct l2cap_conninfo cinfo
;
276 if (get_user(len
, optlen
))
283 memset(&opts
, 0, sizeof(opts
));
284 opts
.imtu
= chan
->imtu
;
285 opts
.omtu
= chan
->omtu
;
286 opts
.flush_to
= chan
->flush_to
;
287 opts
.mode
= chan
->mode
;
288 opts
.fcs
= chan
->fcs
;
289 opts
.max_tx
= chan
->max_tx
;
290 opts
.txwin_size
= chan
->tx_win
;
292 len
= min_t(unsigned int, len
, sizeof(opts
));
293 if (copy_to_user(optval
, (char *) &opts
, len
))
299 switch (chan
->sec_level
) {
300 case BT_SECURITY_LOW
:
303 case BT_SECURITY_MEDIUM
:
304 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
;
306 case BT_SECURITY_HIGH
:
307 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
|
315 if (test_bit(FLAG_ROLE_SWITCH
, &chan
->flags
))
316 opt
|= L2CAP_LM_MASTER
;
318 if (test_bit(FLAG_FORCE_RELIABLE
, &chan
->flags
))
319 opt
|= L2CAP_LM_RELIABLE
;
321 if (put_user(opt
, (u32 __user
*) optval
))
326 if (sk
->sk_state
!= BT_CONNECTED
&&
327 !(sk
->sk_state
== BT_CONNECT2
&&
328 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
333 memset(&cinfo
, 0, sizeof(cinfo
));
334 cinfo
.hci_handle
= chan
->conn
->hcon
->handle
;
335 memcpy(cinfo
.dev_class
, chan
->conn
->hcon
->dev_class
, 3);
337 len
= min_t(unsigned int, len
, sizeof(cinfo
));
338 if (copy_to_user(optval
, (char *) &cinfo
, len
))
352 static int l2cap_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
354 struct sock
*sk
= sock
->sk
;
355 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
356 struct bt_security sec
;
362 if (level
== SOL_L2CAP
)
363 return l2cap_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
365 if (level
!= SOL_BLUETOOTH
)
368 if (get_user(len
, optlen
))
375 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
376 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
381 memset(&sec
, 0, sizeof(sec
));
383 sec
.level
= chan
->conn
->hcon
->sec_level
;
385 sec
.level
= chan
->sec_level
;
387 if (sk
->sk_state
== BT_CONNECTED
)
388 sec
.key_size
= chan
->conn
->hcon
->enc_key_size
;
390 len
= min_t(unsigned int, len
, sizeof(sec
));
391 if (copy_to_user(optval
, (char *) &sec
, len
))
397 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
402 if (put_user(test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
),
403 (u32 __user
*) optval
))
409 if (put_user(test_bit(FLAG_FLUSHABLE
, &chan
->flags
),
410 (u32 __user
*) optval
))
416 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
417 && sk
->sk_type
!= SOCK_RAW
) {
422 pwr
.force_active
= test_bit(FLAG_FORCE_ACTIVE
, &chan
->flags
);
424 len
= min_t(unsigned int, len
, sizeof(pwr
));
425 if (copy_to_user(optval
, (char *) &pwr
, len
))
430 case BT_CHANNEL_POLICY
:
436 if (put_user(chan
->chan_policy
, (u32 __user
*) optval
))
449 static bool l2cap_valid_mtu(struct l2cap_chan
*chan
, u16 mtu
)
451 switch (chan
->scid
) {
452 case L2CAP_CID_LE_DATA
:
453 if (mtu
< L2CAP_LE_MIN_MTU
)
458 if (mtu
< L2CAP_DEFAULT_MIN_MTU
)
465 static int l2cap_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
467 struct sock
*sk
= sock
->sk
;
468 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
469 struct l2cap_options opts
;
479 if (sk
->sk_state
== BT_CONNECTED
) {
484 opts
.imtu
= chan
->imtu
;
485 opts
.omtu
= chan
->omtu
;
486 opts
.flush_to
= chan
->flush_to
;
487 opts
.mode
= chan
->mode
;
488 opts
.fcs
= chan
->fcs
;
489 opts
.max_tx
= chan
->max_tx
;
490 opts
.txwin_size
= chan
->tx_win
;
492 len
= min_t(unsigned int, sizeof(opts
), optlen
);
493 if (copy_from_user((char *) &opts
, optval
, len
)) {
498 if (opts
.txwin_size
> L2CAP_DEFAULT_EXT_WINDOW
) {
503 if (!l2cap_valid_mtu(chan
, opts
.imtu
)) {
508 chan
->mode
= opts
.mode
;
509 switch (chan
->mode
) {
510 case L2CAP_MODE_BASIC
:
511 clear_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
);
513 case L2CAP_MODE_ERTM
:
514 case L2CAP_MODE_STREAMING
:
523 chan
->imtu
= opts
.imtu
;
524 chan
->omtu
= opts
.omtu
;
525 chan
->fcs
= opts
.fcs
;
526 chan
->max_tx
= opts
.max_tx
;
527 chan
->tx_win
= opts
.txwin_size
;
531 if (get_user(opt
, (u32 __user
*) optval
)) {
536 if (opt
& L2CAP_LM_AUTH
)
537 chan
->sec_level
= BT_SECURITY_LOW
;
538 if (opt
& L2CAP_LM_ENCRYPT
)
539 chan
->sec_level
= BT_SECURITY_MEDIUM
;
540 if (opt
& L2CAP_LM_SECURE
)
541 chan
->sec_level
= BT_SECURITY_HIGH
;
543 if (opt
& L2CAP_LM_MASTER
)
544 set_bit(FLAG_ROLE_SWITCH
, &chan
->flags
);
546 clear_bit(FLAG_ROLE_SWITCH
, &chan
->flags
);
548 if (opt
& L2CAP_LM_RELIABLE
)
549 set_bit(FLAG_FORCE_RELIABLE
, &chan
->flags
);
551 clear_bit(FLAG_FORCE_RELIABLE
, &chan
->flags
);
563 static int l2cap_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
565 struct sock
*sk
= sock
->sk
;
566 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
567 struct bt_security sec
;
569 struct l2cap_conn
*conn
;
575 if (level
== SOL_L2CAP
)
576 return l2cap_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
578 if (level
!= SOL_BLUETOOTH
)
585 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
586 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
591 sec
.level
= BT_SECURITY_LOW
;
593 len
= min_t(unsigned int, sizeof(sec
), optlen
);
594 if (copy_from_user((char *) &sec
, optval
, len
)) {
599 if (sec
.level
< BT_SECURITY_LOW
||
600 sec
.level
> BT_SECURITY_HIGH
) {
605 chan
->sec_level
= sec
.level
;
612 /*change security for LE channels */
613 if (chan
->scid
== L2CAP_CID_LE_DATA
) {
614 if (!conn
->hcon
->out
) {
619 if (smp_conn_security(conn
->hcon
, sec
.level
))
621 sk
->sk_state
= BT_CONFIG
;
622 chan
->state
= BT_CONFIG
;
624 /* or for ACL link */
625 } else if ((sk
->sk_state
== BT_CONNECT2
&&
626 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
)) ||
627 sk
->sk_state
== BT_CONNECTED
) {
628 if (!l2cap_chan_check_security(chan
))
629 set_bit(BT_SK_SUSPEND
, &bt_sk(sk
)->flags
);
631 sk
->sk_state_change(sk
);
638 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
643 if (get_user(opt
, (u32 __user
*) optval
)) {
649 set_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
651 clear_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
655 if (get_user(opt
, (u32 __user
*) optval
)) {
660 if (opt
> BT_FLUSHABLE_ON
) {
665 if (opt
== BT_FLUSHABLE_OFF
) {
666 struct l2cap_conn
*conn
= chan
->conn
;
667 /* proceed further only when we have l2cap_conn and
668 No Flush support in the LM */
669 if (!conn
|| !lmp_no_flush_capable(conn
->hcon
->hdev
)) {
676 set_bit(FLAG_FLUSHABLE
, &chan
->flags
);
678 clear_bit(FLAG_FLUSHABLE
, &chan
->flags
);
682 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
683 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
688 pwr
.force_active
= BT_POWER_FORCE_ACTIVE_ON
;
690 len
= min_t(unsigned int, sizeof(pwr
), optlen
);
691 if (copy_from_user((char *) &pwr
, optval
, len
)) {
696 if (pwr
.force_active
)
697 set_bit(FLAG_FORCE_ACTIVE
, &chan
->flags
);
699 clear_bit(FLAG_FORCE_ACTIVE
, &chan
->flags
);
702 case BT_CHANNEL_POLICY
:
708 if (get_user(opt
, (u32 __user
*) optval
)) {
713 if (opt
> BT_CHANNEL_POLICY_AMP_PREFERRED
) {
718 if (chan
->mode
!= L2CAP_MODE_ERTM
&&
719 chan
->mode
!= L2CAP_MODE_STREAMING
) {
724 chan
->chan_policy
= (u8
) opt
;
736 static int l2cap_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
)
738 struct sock
*sk
= sock
->sk
;
739 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
742 BT_DBG("sock %p, sk %p", sock
, sk
);
744 err
= sock_error(sk
);
748 if (msg
->msg_flags
& MSG_OOB
)
751 if (sk
->sk_state
!= BT_CONNECTED
)
754 l2cap_chan_lock(chan
);
755 err
= l2cap_chan_send(chan
, msg
, len
, sk
->sk_priority
);
756 l2cap_chan_unlock(chan
);
761 static int l2cap_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
, int flags
)
763 struct sock
*sk
= sock
->sk
;
764 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
769 if (sk
->sk_state
== BT_CONNECT2
&& test_bit(BT_SK_DEFER_SETUP
,
770 &bt_sk(sk
)->flags
)) {
771 sk
->sk_state
= BT_CONFIG
;
772 pi
->chan
->state
= BT_CONFIG
;
774 __l2cap_connect_rsp_defer(pi
->chan
);
781 if (sock
->type
== SOCK_STREAM
)
782 err
= bt_sock_stream_recvmsg(iocb
, sock
, msg
, len
, flags
);
784 err
= bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
786 if (pi
->chan
->mode
!= L2CAP_MODE_ERTM
)
789 /* Attempt to put pending rx data in the socket buffer */
793 if (!test_bit(CONN_LOCAL_BUSY
, &pi
->chan
->conn_state
))
796 if (pi
->rx_busy_skb
) {
797 if (!sock_queue_rcv_skb(sk
, pi
->rx_busy_skb
))
798 pi
->rx_busy_skb
= NULL
;
803 /* Restore data flow when half of the receive buffer is
804 * available. This avoids resending large numbers of
807 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
>> 1)
808 l2cap_chan_busy(pi
->chan
, 0);
815 /* Kill socket (only if zapped and orphan)
816 * Must be called on unlocked socket.
818 static void l2cap_sock_kill(struct sock
*sk
)
820 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
823 BT_DBG("sk %p state %s", sk
, state_to_string(sk
->sk_state
));
825 /* Kill poor orphan */
827 l2cap_chan_destroy(l2cap_pi(sk
)->chan
);
828 sock_set_flag(sk
, SOCK_DEAD
);
832 static int l2cap_sock_shutdown(struct socket
*sock
, int how
)
834 struct sock
*sk
= sock
->sk
;
835 struct l2cap_chan
*chan
;
836 struct l2cap_conn
*conn
;
839 BT_DBG("sock %p, sk %p", sock
, sk
);
844 chan
= l2cap_pi(sk
)->chan
;
848 mutex_lock(&conn
->chan_lock
);
850 l2cap_chan_lock(chan
);
853 if (!sk
->sk_shutdown
) {
854 if (chan
->mode
== L2CAP_MODE_ERTM
)
855 err
= __l2cap_wait_ack(sk
);
857 sk
->sk_shutdown
= SHUTDOWN_MASK
;
860 l2cap_chan_close(chan
, 0);
863 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
864 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
868 if (!err
&& sk
->sk_err
)
872 l2cap_chan_unlock(chan
);
875 mutex_unlock(&conn
->chan_lock
);
880 static int l2cap_sock_release(struct socket
*sock
)
882 struct sock
*sk
= sock
->sk
;
885 BT_DBG("sock %p, sk %p", sock
, sk
);
890 err
= l2cap_sock_shutdown(sock
, 2);
897 static void l2cap_sock_cleanup_listen(struct sock
*parent
)
901 BT_DBG("parent %p", parent
);
903 /* Close not yet accepted channels */
904 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
905 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
907 l2cap_chan_lock(chan
);
908 __clear_chan_timer(chan
);
909 l2cap_chan_close(chan
, ECONNRESET
);
910 l2cap_chan_unlock(chan
);
916 static struct l2cap_chan
*l2cap_sock_new_connection_cb(struct l2cap_chan
*chan
)
918 struct sock
*sk
, *parent
= chan
->data
;
920 /* Check for backlog size */
921 if (sk_acceptq_is_full(parent
)) {
922 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
926 sk
= l2cap_sock_alloc(sock_net(parent
), NULL
, BTPROTO_L2CAP
,
931 bt_sock_reclassify_lock(sk
, BTPROTO_L2CAP
);
933 l2cap_sock_init(sk
, parent
);
935 return l2cap_pi(sk
)->chan
;
938 static int l2cap_sock_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
941 struct sock
*sk
= chan
->data
;
942 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
946 if (pi
->rx_busy_skb
) {
951 err
= sock_queue_rcv_skb(sk
, skb
);
953 /* For ERTM, handle one skb that doesn't fit into the recv
954 * buffer. This is important to do because the data frames
955 * have already been acked, so the skb cannot be discarded.
957 * Notify the l2cap core that the buffer is full, so the
958 * LOCAL_BUSY state is entered and no more frames are
959 * acked and reassembled until there is buffer space
962 if (err
< 0 && pi
->chan
->mode
== L2CAP_MODE_ERTM
) {
963 pi
->rx_busy_skb
= skb
;
964 l2cap_chan_busy(pi
->chan
, 1);
974 static void l2cap_sock_close_cb(struct l2cap_chan
*chan
)
976 struct sock
*sk
= chan
->data
;
981 static void l2cap_sock_teardown_cb(struct l2cap_chan
*chan
, int err
)
983 struct sock
*sk
= chan
->data
;
988 parent
= bt_sk(sk
)->parent
;
990 sock_set_flag(sk
, SOCK_ZAPPED
);
992 switch (chan
->state
) {
998 l2cap_sock_cleanup_listen(sk
);
999 sk
->sk_state
= BT_CLOSED
;
1000 chan
->state
= BT_CLOSED
;
1004 sk
->sk_state
= BT_CLOSED
;
1005 chan
->state
= BT_CLOSED
;
1010 bt_accept_unlink(sk
);
1011 parent
->sk_data_ready(parent
, 0);
1013 sk
->sk_state_change(sk
);
1022 static void l2cap_sock_state_change_cb(struct l2cap_chan
*chan
, int state
)
1024 struct sock
*sk
= chan
->data
;
1026 sk
->sk_state
= state
;
1029 static struct sk_buff
*l2cap_sock_alloc_skb_cb(struct l2cap_chan
*chan
,
1030 unsigned long len
, int nb
)
1032 struct sk_buff
*skb
;
1035 l2cap_chan_unlock(chan
);
1036 skb
= bt_skb_send_alloc(chan
->sk
, len
, nb
, &err
);
1037 l2cap_chan_lock(chan
);
1040 return ERR_PTR(err
);
1045 static void l2cap_sock_ready_cb(struct l2cap_chan
*chan
)
1047 struct sock
*sk
= chan
->data
;
1048 struct sock
*parent
;
1052 parent
= bt_sk(sk
)->parent
;
1054 BT_DBG("sk %p, parent %p", sk
, parent
);
1056 sk
->sk_state
= BT_CONNECTED
;
1057 sk
->sk_state_change(sk
);
1060 parent
->sk_data_ready(parent
, 0);
1065 static struct l2cap_ops l2cap_chan_ops
= {
1066 .name
= "L2CAP Socket Interface",
1067 .new_connection
= l2cap_sock_new_connection_cb
,
1068 .recv
= l2cap_sock_recv_cb
,
1069 .close
= l2cap_sock_close_cb
,
1070 .teardown
= l2cap_sock_teardown_cb
,
1071 .state_change
= l2cap_sock_state_change_cb
,
1072 .ready
= l2cap_sock_ready_cb
,
1073 .alloc_skb
= l2cap_sock_alloc_skb_cb
,
1076 static void l2cap_sock_destruct(struct sock
*sk
)
1078 BT_DBG("sk %p", sk
);
1080 l2cap_chan_put(l2cap_pi(sk
)->chan
);
1081 if (l2cap_pi(sk
)->rx_busy_skb
) {
1082 kfree_skb(l2cap_pi(sk
)->rx_busy_skb
);
1083 l2cap_pi(sk
)->rx_busy_skb
= NULL
;
1086 skb_queue_purge(&sk
->sk_receive_queue
);
1087 skb_queue_purge(&sk
->sk_write_queue
);
1090 static void l2cap_sock_init(struct sock
*sk
, struct sock
*parent
)
1092 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1093 struct l2cap_chan
*chan
= pi
->chan
;
1095 BT_DBG("sk %p", sk
);
1098 struct l2cap_chan
*pchan
= l2cap_pi(parent
)->chan
;
1100 sk
->sk_type
= parent
->sk_type
;
1101 bt_sk(sk
)->flags
= bt_sk(parent
)->flags
;
1103 chan
->chan_type
= pchan
->chan_type
;
1104 chan
->imtu
= pchan
->imtu
;
1105 chan
->omtu
= pchan
->omtu
;
1106 chan
->conf_state
= pchan
->conf_state
;
1107 chan
->mode
= pchan
->mode
;
1108 chan
->fcs
= pchan
->fcs
;
1109 chan
->max_tx
= pchan
->max_tx
;
1110 chan
->tx_win
= pchan
->tx_win
;
1111 chan
->tx_win_max
= pchan
->tx_win_max
;
1112 chan
->sec_level
= pchan
->sec_level
;
1113 chan
->flags
= pchan
->flags
;
1115 security_sk_clone(parent
, sk
);
1118 switch (sk
->sk_type
) {
1120 chan
->chan_type
= L2CAP_CHAN_RAW
;
1123 chan
->chan_type
= L2CAP_CHAN_CONN_LESS
;
1125 case SOCK_SEQPACKET
:
1127 chan
->chan_type
= L2CAP_CHAN_CONN_ORIENTED
;
1131 chan
->imtu
= L2CAP_DEFAULT_MTU
;
1133 if (!disable_ertm
&& sk
->sk_type
== SOCK_STREAM
) {
1134 chan
->mode
= L2CAP_MODE_ERTM
;
1135 set_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
);
1137 chan
->mode
= L2CAP_MODE_BASIC
;
1140 l2cap_chan_set_defaults(chan
);
1143 /* Default config options */
1144 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
1147 chan
->ops
= &l2cap_chan_ops
;
1150 static struct proto l2cap_proto
= {
1152 .owner
= THIS_MODULE
,
1153 .obj_size
= sizeof(struct l2cap_pinfo
)
1156 static struct sock
*l2cap_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
1159 struct l2cap_chan
*chan
;
1161 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &l2cap_proto
);
1165 sock_init_data(sock
, sk
);
1166 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
1168 sk
->sk_destruct
= l2cap_sock_destruct
;
1169 sk
->sk_sndtimeo
= L2CAP_CONN_TIMEOUT
;
1171 sock_reset_flag(sk
, SOCK_ZAPPED
);
1173 sk
->sk_protocol
= proto
;
1174 sk
->sk_state
= BT_OPEN
;
1176 chan
= l2cap_chan_create();
1182 l2cap_chan_hold(chan
);
1186 l2cap_pi(sk
)->chan
= chan
;
1191 static int l2cap_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
1196 BT_DBG("sock %p", sock
);
1198 sock
->state
= SS_UNCONNECTED
;
1200 if (sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
&&
1201 sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
)
1202 return -ESOCKTNOSUPPORT
;
1204 if (sock
->type
== SOCK_RAW
&& !kern
&& !capable(CAP_NET_RAW
))
1207 sock
->ops
= &l2cap_sock_ops
;
1209 sk
= l2cap_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
1213 l2cap_sock_init(sk
, NULL
);
1217 static const struct proto_ops l2cap_sock_ops
= {
1218 .family
= PF_BLUETOOTH
,
1219 .owner
= THIS_MODULE
,
1220 .release
= l2cap_sock_release
,
1221 .bind
= l2cap_sock_bind
,
1222 .connect
= l2cap_sock_connect
,
1223 .listen
= l2cap_sock_listen
,
1224 .accept
= l2cap_sock_accept
,
1225 .getname
= l2cap_sock_getname
,
1226 .sendmsg
= l2cap_sock_sendmsg
,
1227 .recvmsg
= l2cap_sock_recvmsg
,
1228 .poll
= bt_sock_poll
,
1229 .ioctl
= bt_sock_ioctl
,
1230 .mmap
= sock_no_mmap
,
1231 .socketpair
= sock_no_socketpair
,
1232 .shutdown
= l2cap_sock_shutdown
,
1233 .setsockopt
= l2cap_sock_setsockopt
,
1234 .getsockopt
= l2cap_sock_getsockopt
1237 static const struct net_proto_family l2cap_sock_family_ops
= {
1238 .family
= PF_BLUETOOTH
,
1239 .owner
= THIS_MODULE
,
1240 .create
= l2cap_sock_create
,
1243 int __init
l2cap_init_sockets(void)
1247 err
= proto_register(&l2cap_proto
, 0);
1251 err
= bt_sock_register(BTPROTO_L2CAP
, &l2cap_sock_family_ops
);
1255 BT_INFO("L2CAP socket layer initialized");
1260 BT_ERR("L2CAP socket registration failed");
1261 proto_unregister(&l2cap_proto
);
1265 void l2cap_cleanup_sockets(void)
1267 if (bt_sock_unregister(BTPROTO_L2CAP
) < 0)
1268 BT_ERR("L2CAP socket unregistration failed");
1270 proto_unregister(&l2cap_proto
);