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 sockets. */
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31 #include <net/bluetooth/l2cap.h>
33 /* ---- L2CAP timers ---- */
34 static void l2cap_sock_timeout(unsigned long arg
)
36 struct sock
*sk
= (struct sock
*) arg
;
39 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
43 if (sock_owned_by_user(sk
)) {
44 /* sk is owned by user. Try again later */
45 l2cap_sock_set_timer(sk
, HZ
/ 5);
51 if (sk
->sk_state
== BT_CONNECTED
|| sk
->sk_state
== BT_CONFIG
)
52 reason
= ECONNREFUSED
;
53 else if (sk
->sk_state
== BT_CONNECT
&&
54 l2cap_pi(sk
)->sec_level
!= BT_SECURITY_SDP
)
55 reason
= ECONNREFUSED
;
59 __l2cap_sock_close(sk
, reason
);
67 void l2cap_sock_set_timer(struct sock
*sk
, long timeout
)
69 BT_DBG("sk %p state %d timeout %ld", sk
, sk
->sk_state
, timeout
);
70 sk_reset_timer(sk
, &sk
->sk_timer
, jiffies
+ timeout
);
73 void l2cap_sock_clear_timer(struct sock
*sk
)
75 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
76 sk_stop_timer(sk
, &sk
->sk_timer
);
79 static struct sock
*__l2cap_get_sock_by_addr(__le16 psm
, bdaddr_t
*src
)
82 struct hlist_node
*node
;
83 sk_for_each(sk
, node
, &l2cap_sk_list
.head
)
84 if (l2cap_pi(sk
)->sport
== psm
&& !bacmp(&bt_sk(sk
)->src
, src
))
91 static int l2cap_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int alen
)
93 struct sock
*sk
= sock
->sk
;
94 struct sockaddr_l2 la
;
99 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
102 memset(&la
, 0, sizeof(la
));
103 len
= min_t(unsigned int, sizeof(la
), alen
);
104 memcpy(&la
, addr
, len
);
106 if (la
.l2_cid
&& la
.l2_psm
)
111 if (sk
->sk_state
!= BT_OPEN
) {
117 __u16 psm
= __le16_to_cpu(la
.l2_psm
);
119 /* PSM must be odd and lsb of upper byte must be 0 */
120 if ((psm
& 0x0101) != 0x0001) {
125 /* Restrict usage of well-known PSMs */
126 if (psm
< 0x1001 && !capable(CAP_NET_BIND_SERVICE
)) {
132 write_lock_bh(&l2cap_sk_list
.lock
);
134 if (la
.l2_psm
&& __l2cap_get_sock_by_addr(la
.l2_psm
, &la
.l2_bdaddr
)) {
137 /* Save source address */
138 bacpy(&bt_sk(sk
)->src
, &la
.l2_bdaddr
);
139 l2cap_pi(sk
)->psm
= la
.l2_psm
;
140 l2cap_pi(sk
)->sport
= la
.l2_psm
;
141 sk
->sk_state
= BT_BOUND
;
143 if (__le16_to_cpu(la
.l2_psm
) == 0x0001 ||
144 __le16_to_cpu(la
.l2_psm
) == 0x0003)
145 l2cap_pi(sk
)->sec_level
= BT_SECURITY_SDP
;
149 l2cap_pi(sk
)->scid
= la
.l2_cid
;
151 write_unlock_bh(&l2cap_sk_list
.lock
);
158 static int l2cap_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
160 struct sock
*sk
= sock
->sk
;
161 struct sockaddr_l2 la
;
166 if (!addr
|| alen
< sizeof(addr
->sa_family
) ||
167 addr
->sa_family
!= AF_BLUETOOTH
)
170 memset(&la
, 0, sizeof(la
));
171 len
= min_t(unsigned int, sizeof(la
), alen
);
172 memcpy(&la
, addr
, len
);
174 if (la
.l2_cid
&& la
.l2_psm
)
179 if ((sk
->sk_type
== SOCK_SEQPACKET
|| sk
->sk_type
== SOCK_STREAM
)
180 && !(la
.l2_psm
|| la
.l2_cid
)) {
185 switch (l2cap_pi(sk
)->mode
) {
186 case L2CAP_MODE_BASIC
:
188 case L2CAP_MODE_ERTM
:
189 case L2CAP_MODE_STREAMING
:
198 switch (sk
->sk_state
) {
202 /* Already connecting */
206 /* Already connected */
220 /* PSM must be odd and lsb of upper byte must be 0 */
221 if ((__le16_to_cpu(la
.l2_psm
) & 0x0101) != 0x0001 &&
222 sk
->sk_type
!= SOCK_RAW
&& !la
.l2_cid
) {
227 /* Set destination address and psm */
228 bacpy(&bt_sk(sk
)->dst
, &la
.l2_bdaddr
);
229 l2cap_pi(sk
)->psm
= la
.l2_psm
;
230 l2cap_pi(sk
)->dcid
= la
.l2_cid
;
232 err
= l2cap_do_connect(sk
);
237 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
238 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
244 static int l2cap_sock_listen(struct socket
*sock
, int backlog
)
246 struct sock
*sk
= sock
->sk
;
249 BT_DBG("sk %p backlog %d", sk
, backlog
);
253 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
)
254 || sk
->sk_state
!= BT_BOUND
) {
259 switch (l2cap_pi(sk
)->mode
) {
260 case L2CAP_MODE_BASIC
:
262 case L2CAP_MODE_ERTM
:
263 case L2CAP_MODE_STREAMING
:
272 if (!l2cap_pi(sk
)->psm
&& !l2cap_pi(sk
)->dcid
) {
273 bdaddr_t
*src
= &bt_sk(sk
)->src
;
278 write_lock_bh(&l2cap_sk_list
.lock
);
280 for (psm
= 0x1001; psm
< 0x1100; psm
+= 2)
281 if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm
), src
)) {
282 l2cap_pi(sk
)->psm
= cpu_to_le16(psm
);
283 l2cap_pi(sk
)->sport
= cpu_to_le16(psm
);
288 write_unlock_bh(&l2cap_sk_list
.lock
);
294 sk
->sk_max_ack_backlog
= backlog
;
295 sk
->sk_ack_backlog
= 0;
296 sk
->sk_state
= BT_LISTEN
;
303 static int l2cap_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
305 DECLARE_WAITQUEUE(wait
, current
);
306 struct sock
*sk
= sock
->sk
, *nsk
;
310 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
312 if (sk
->sk_state
!= BT_LISTEN
) {
317 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
319 BT_DBG("sk %p timeo %ld", sk
, timeo
);
321 /* Wait for an incoming connection. (wake-one). */
322 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
323 while (!(nsk
= bt_accept_dequeue(sk
, newsock
))) {
324 set_current_state(TASK_INTERRUPTIBLE
);
331 timeo
= schedule_timeout(timeo
);
332 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
334 if (sk
->sk_state
!= BT_LISTEN
) {
339 if (signal_pending(current
)) {
340 err
= sock_intr_errno(timeo
);
344 set_current_state(TASK_RUNNING
);
345 remove_wait_queue(sk_sleep(sk
), &wait
);
350 newsock
->state
= SS_CONNECTED
;
352 BT_DBG("new socket %p", nsk
);
359 static int l2cap_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
361 struct sockaddr_l2
*la
= (struct sockaddr_l2
*) addr
;
362 struct sock
*sk
= sock
->sk
;
364 BT_DBG("sock %p, sk %p", sock
, sk
);
366 addr
->sa_family
= AF_BLUETOOTH
;
367 *len
= sizeof(struct sockaddr_l2
);
370 la
->l2_psm
= l2cap_pi(sk
)->psm
;
371 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->dst
);
372 la
->l2_cid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
374 la
->l2_psm
= l2cap_pi(sk
)->sport
;
375 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->src
);
376 la
->l2_cid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
382 static int l2cap_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
384 struct sock
*sk
= sock
->sk
;
385 struct l2cap_options opts
;
386 struct l2cap_conninfo cinfo
;
392 if (get_user(len
, optlen
))
399 memset(&opts
, 0, sizeof(opts
));
400 opts
.imtu
= l2cap_pi(sk
)->imtu
;
401 opts
.omtu
= l2cap_pi(sk
)->omtu
;
402 opts
.flush_to
= l2cap_pi(sk
)->flush_to
;
403 opts
.mode
= l2cap_pi(sk
)->mode
;
404 opts
.fcs
= l2cap_pi(sk
)->fcs
;
405 opts
.max_tx
= l2cap_pi(sk
)->max_tx
;
406 opts
.txwin_size
= (__u16
)l2cap_pi(sk
)->tx_win
;
408 len
= min_t(unsigned int, len
, sizeof(opts
));
409 if (copy_to_user(optval
, (char *) &opts
, len
))
415 switch (l2cap_pi(sk
)->sec_level
) {
416 case BT_SECURITY_LOW
:
419 case BT_SECURITY_MEDIUM
:
420 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
;
422 case BT_SECURITY_HIGH
:
423 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
|
431 if (l2cap_pi(sk
)->role_switch
)
432 opt
|= L2CAP_LM_MASTER
;
434 if (l2cap_pi(sk
)->force_reliable
)
435 opt
|= L2CAP_LM_RELIABLE
;
437 if (put_user(opt
, (u32 __user
*) optval
))
442 if (sk
->sk_state
!= BT_CONNECTED
&&
443 !(sk
->sk_state
== BT_CONNECT2
&&
444 bt_sk(sk
)->defer_setup
)) {
449 cinfo
.hci_handle
= l2cap_pi(sk
)->conn
->hcon
->handle
;
450 memcpy(cinfo
.dev_class
, l2cap_pi(sk
)->conn
->hcon
->dev_class
, 3);
452 len
= min_t(unsigned int, len
, sizeof(cinfo
));
453 if (copy_to_user(optval
, (char *) &cinfo
, len
))
467 static int l2cap_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
469 struct sock
*sk
= sock
->sk
;
470 struct bt_security sec
;
475 if (level
== SOL_L2CAP
)
476 return l2cap_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
478 if (level
!= SOL_BLUETOOTH
)
481 if (get_user(len
, optlen
))
488 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
489 && sk
->sk_type
!= SOCK_RAW
) {
494 sec
.level
= l2cap_pi(sk
)->sec_level
;
496 len
= min_t(unsigned int, len
, sizeof(sec
));
497 if (copy_to_user(optval
, (char *) &sec
, len
))
503 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
508 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
514 if (put_user(l2cap_pi(sk
)->flushable
, (u32 __user
*) optval
))
528 static int l2cap_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
530 struct sock
*sk
= sock
->sk
;
531 struct l2cap_options opts
;
541 if (sk
->sk_state
== BT_CONNECTED
) {
546 opts
.imtu
= l2cap_pi(sk
)->imtu
;
547 opts
.omtu
= l2cap_pi(sk
)->omtu
;
548 opts
.flush_to
= l2cap_pi(sk
)->flush_to
;
549 opts
.mode
= l2cap_pi(sk
)->mode
;
550 opts
.fcs
= l2cap_pi(sk
)->fcs
;
551 opts
.max_tx
= l2cap_pi(sk
)->max_tx
;
552 opts
.txwin_size
= (__u16
)l2cap_pi(sk
)->tx_win
;
554 len
= min_t(unsigned int, sizeof(opts
), optlen
);
555 if (copy_from_user((char *) &opts
, optval
, len
)) {
560 if (opts
.txwin_size
> L2CAP_DEFAULT_TX_WINDOW
) {
565 l2cap_pi(sk
)->mode
= opts
.mode
;
566 switch (l2cap_pi(sk
)->mode
) {
567 case L2CAP_MODE_BASIC
:
568 l2cap_pi(sk
)->conf_state
&= ~L2CAP_CONF_STATE2_DEVICE
;
570 case L2CAP_MODE_ERTM
:
571 case L2CAP_MODE_STREAMING
:
580 l2cap_pi(sk
)->imtu
= opts
.imtu
;
581 l2cap_pi(sk
)->omtu
= opts
.omtu
;
582 l2cap_pi(sk
)->fcs
= opts
.fcs
;
583 l2cap_pi(sk
)->max_tx
= opts
.max_tx
;
584 l2cap_pi(sk
)->tx_win
= (__u8
)opts
.txwin_size
;
588 if (get_user(opt
, (u32 __user
*) optval
)) {
593 if (opt
& L2CAP_LM_AUTH
)
594 l2cap_pi(sk
)->sec_level
= BT_SECURITY_LOW
;
595 if (opt
& L2CAP_LM_ENCRYPT
)
596 l2cap_pi(sk
)->sec_level
= BT_SECURITY_MEDIUM
;
597 if (opt
& L2CAP_LM_SECURE
)
598 l2cap_pi(sk
)->sec_level
= BT_SECURITY_HIGH
;
600 l2cap_pi(sk
)->role_switch
= (opt
& L2CAP_LM_MASTER
);
601 l2cap_pi(sk
)->force_reliable
= (opt
& L2CAP_LM_RELIABLE
);
613 static int l2cap_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
615 struct sock
*sk
= sock
->sk
;
616 struct bt_security sec
;
622 if (level
== SOL_L2CAP
)
623 return l2cap_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
625 if (level
!= SOL_BLUETOOTH
)
632 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
633 && sk
->sk_type
!= SOCK_RAW
) {
638 sec
.level
= BT_SECURITY_LOW
;
640 len
= min_t(unsigned int, sizeof(sec
), optlen
);
641 if (copy_from_user((char *) &sec
, optval
, len
)) {
646 if (sec
.level
< BT_SECURITY_LOW
||
647 sec
.level
> BT_SECURITY_HIGH
) {
652 l2cap_pi(sk
)->sec_level
= sec
.level
;
656 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
661 if (get_user(opt
, (u32 __user
*) optval
)) {
666 bt_sk(sk
)->defer_setup
= opt
;
670 if (get_user(opt
, (u32 __user
*) optval
)) {
675 if (opt
> BT_FLUSHABLE_ON
) {
680 if (opt
== BT_FLUSHABLE_OFF
) {
681 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
682 /* proceed futher only when we have l2cap_conn and
683 No Flush support in the LM */
684 if (!conn
|| !lmp_no_flush_capable(conn
->hcon
->hdev
)) {
690 l2cap_pi(sk
)->flushable
= opt
;
702 static int l2cap_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
)
704 struct sock
*sk
= sock
->sk
;
705 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
710 BT_DBG("sock %p, sk %p", sock
, sk
);
712 err
= sock_error(sk
);
716 if (msg
->msg_flags
& MSG_OOB
)
721 if (sk
->sk_state
!= BT_CONNECTED
) {
726 /* Connectionless channel */
727 if (sk
->sk_type
== SOCK_DGRAM
) {
728 skb
= l2cap_create_connless_pdu(sk
, msg
, len
);
732 l2cap_do_send(sk
, skb
);
739 case L2CAP_MODE_BASIC
:
740 /* Check outgoing MTU */
741 if (len
> pi
->omtu
) {
746 /* Create a basic PDU */
747 skb
= l2cap_create_basic_pdu(sk
, msg
, len
);
753 l2cap_do_send(sk
, skb
);
757 case L2CAP_MODE_ERTM
:
758 case L2CAP_MODE_STREAMING
:
759 /* Entire SDU fits into one PDU */
760 if (len
<= pi
->remote_mps
) {
761 control
= L2CAP_SDU_UNSEGMENTED
;
762 skb
= l2cap_create_iframe_pdu(sk
, msg
, len
, control
, 0);
767 __skb_queue_tail(TX_QUEUE(sk
), skb
);
769 if (sk
->sk_send_head
== NULL
)
770 sk
->sk_send_head
= skb
;
773 /* Segment SDU into multiples PDUs */
774 err
= l2cap_sar_segment_sdu(sk
, msg
, len
);
779 if (pi
->mode
== L2CAP_MODE_STREAMING
) {
780 l2cap_streaming_send(sk
);
782 if ((pi
->conn_state
& L2CAP_CONN_REMOTE_BUSY
) &&
783 (pi
->conn_state
& L2CAP_CONN_WAIT_F
)) {
787 err
= l2cap_ertm_send(sk
);
795 BT_DBG("bad state %1.1x", pi
->mode
);
804 static int l2cap_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
, int flags
)
806 struct sock
*sk
= sock
->sk
;
810 if (sk
->sk_state
== BT_CONNECT2
&& bt_sk(sk
)->defer_setup
) {
811 struct l2cap_conn_rsp rsp
;
812 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
815 sk
->sk_state
= BT_CONFIG
;
817 rsp
.scid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
818 rsp
.dcid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
819 rsp
.result
= cpu_to_le16(L2CAP_CR_SUCCESS
);
820 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
821 l2cap_send_cmd(l2cap_pi(sk
)->conn
, l2cap_pi(sk
)->ident
,
822 L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
824 if (l2cap_pi(sk
)->conf_state
& L2CAP_CONF_REQ_SENT
) {
829 l2cap_pi(sk
)->conf_state
|= L2CAP_CONF_REQ_SENT
;
830 l2cap_send_cmd(conn
, l2cap_get_ident(conn
), L2CAP_CONF_REQ
,
831 l2cap_build_conf_req(sk
, buf
), buf
);
832 l2cap_pi(sk
)->num_conf_req
++;
840 if (sock
->type
== SOCK_STREAM
)
841 return bt_sock_stream_recvmsg(iocb
, sock
, msg
, len
, flags
);
843 return bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
846 /* Kill socket (only if zapped and orphan)
847 * Must be called on unlocked socket.
849 void l2cap_sock_kill(struct sock
*sk
)
851 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
854 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
856 /* Kill poor orphan */
857 bt_sock_unlink(&l2cap_sk_list
, sk
);
858 sock_set_flag(sk
, SOCK_DEAD
);
862 /* Must be called on unlocked socket. */
863 static void l2cap_sock_close(struct sock
*sk
)
865 l2cap_sock_clear_timer(sk
);
867 __l2cap_sock_close(sk
, ECONNRESET
);
872 static void l2cap_sock_cleanup_listen(struct sock
*parent
)
876 BT_DBG("parent %p", parent
);
878 /* Close not yet accepted channels */
879 while ((sk
= bt_accept_dequeue(parent
, NULL
)))
880 l2cap_sock_close(sk
);
882 parent
->sk_state
= BT_CLOSED
;
883 sock_set_flag(parent
, SOCK_ZAPPED
);
886 void __l2cap_sock_close(struct sock
*sk
, int reason
)
888 struct l2cap_conn
*conn
= l2cap_pi(sk
)->conn
;
890 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
892 switch (sk
->sk_state
) {
894 l2cap_sock_cleanup_listen(sk
);
899 if ((sk
->sk_type
== SOCK_SEQPACKET
||
900 sk
->sk_type
== SOCK_STREAM
) &&
901 conn
->hcon
->type
== ACL_LINK
) {
902 l2cap_sock_set_timer(sk
, sk
->sk_sndtimeo
);
903 l2cap_send_disconn_req(conn
, sk
, reason
);
905 l2cap_chan_del(sk
, reason
);
909 if ((sk
->sk_type
== SOCK_SEQPACKET
||
910 sk
->sk_type
== SOCK_STREAM
) &&
911 conn
->hcon
->type
== ACL_LINK
) {
912 struct l2cap_conn_rsp rsp
;
915 if (bt_sk(sk
)->defer_setup
)
916 result
= L2CAP_CR_SEC_BLOCK
;
918 result
= L2CAP_CR_BAD_PSM
;
920 rsp
.scid
= cpu_to_le16(l2cap_pi(sk
)->dcid
);
921 rsp
.dcid
= cpu_to_le16(l2cap_pi(sk
)->scid
);
922 rsp
.result
= cpu_to_le16(result
);
923 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
924 l2cap_send_cmd(conn
, l2cap_pi(sk
)->ident
,
925 L2CAP_CONN_RSP
, sizeof(rsp
), &rsp
);
928 l2cap_chan_del(sk
, reason
);
933 l2cap_chan_del(sk
, reason
);
937 sock_set_flag(sk
, SOCK_ZAPPED
);
942 static int l2cap_sock_shutdown(struct socket
*sock
, int how
)
944 struct sock
*sk
= sock
->sk
;
947 BT_DBG("sock %p, sk %p", sock
, sk
);
953 if (!sk
->sk_shutdown
) {
954 if (l2cap_pi(sk
)->mode
== L2CAP_MODE_ERTM
)
955 err
= __l2cap_wait_ack(sk
);
957 sk
->sk_shutdown
= SHUTDOWN_MASK
;
958 l2cap_sock_clear_timer(sk
);
959 __l2cap_sock_close(sk
, 0);
961 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
962 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
966 if (!err
&& sk
->sk_err
)
973 static int l2cap_sock_release(struct socket
*sock
)
975 struct sock
*sk
= sock
->sk
;
978 BT_DBG("sock %p, sk %p", sock
, sk
);
983 err
= l2cap_sock_shutdown(sock
, 2);
990 static void l2cap_sock_destruct(struct sock
*sk
)
994 skb_queue_purge(&sk
->sk_receive_queue
);
995 skb_queue_purge(&sk
->sk_write_queue
);
998 void l2cap_sock_init(struct sock
*sk
, struct sock
*parent
)
1000 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
1002 BT_DBG("sk %p", sk
);
1005 sk
->sk_type
= parent
->sk_type
;
1006 bt_sk(sk
)->defer_setup
= bt_sk(parent
)->defer_setup
;
1008 pi
->imtu
= l2cap_pi(parent
)->imtu
;
1009 pi
->omtu
= l2cap_pi(parent
)->omtu
;
1010 pi
->conf_state
= l2cap_pi(parent
)->conf_state
;
1011 pi
->mode
= l2cap_pi(parent
)->mode
;
1012 pi
->fcs
= l2cap_pi(parent
)->fcs
;
1013 pi
->max_tx
= l2cap_pi(parent
)->max_tx
;
1014 pi
->tx_win
= l2cap_pi(parent
)->tx_win
;
1015 pi
->sec_level
= l2cap_pi(parent
)->sec_level
;
1016 pi
->role_switch
= l2cap_pi(parent
)->role_switch
;
1017 pi
->force_reliable
= l2cap_pi(parent
)->force_reliable
;
1018 pi
->flushable
= l2cap_pi(parent
)->flushable
;
1020 pi
->imtu
= L2CAP_DEFAULT_MTU
;
1022 if (!disable_ertm
&& sk
->sk_type
== SOCK_STREAM
) {
1023 pi
->mode
= L2CAP_MODE_ERTM
;
1024 pi
->conf_state
|= L2CAP_CONF_STATE2_DEVICE
;
1026 pi
->mode
= L2CAP_MODE_BASIC
;
1028 pi
->max_tx
= L2CAP_DEFAULT_MAX_TX
;
1029 pi
->fcs
= L2CAP_FCS_CRC16
;
1030 pi
->tx_win
= L2CAP_DEFAULT_TX_WINDOW
;
1031 pi
->sec_level
= BT_SECURITY_LOW
;
1032 pi
->role_switch
= 0;
1033 pi
->force_reliable
= 0;
1034 pi
->flushable
= BT_FLUSHABLE_OFF
;
1037 /* Default config options */
1039 pi
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
1040 skb_queue_head_init(TX_QUEUE(sk
));
1041 skb_queue_head_init(SREJ_QUEUE(sk
));
1042 skb_queue_head_init(BUSY_QUEUE(sk
));
1043 INIT_LIST_HEAD(SREJ_LIST(sk
));
1046 static struct proto l2cap_proto
= {
1048 .owner
= THIS_MODULE
,
1049 .obj_size
= sizeof(struct l2cap_pinfo
)
1052 struct sock
*l2cap_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
1056 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &l2cap_proto
);
1060 sock_init_data(sock
, sk
);
1061 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
1063 sk
->sk_destruct
= l2cap_sock_destruct
;
1064 sk
->sk_sndtimeo
= msecs_to_jiffies(L2CAP_CONN_TIMEOUT
);
1066 sock_reset_flag(sk
, SOCK_ZAPPED
);
1068 sk
->sk_protocol
= proto
;
1069 sk
->sk_state
= BT_OPEN
;
1071 setup_timer(&sk
->sk_timer
, l2cap_sock_timeout
, (unsigned long) sk
);
1073 bt_sock_link(&l2cap_sk_list
, sk
);
1077 static int l2cap_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
1082 BT_DBG("sock %p", sock
);
1084 sock
->state
= SS_UNCONNECTED
;
1086 if (sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
&&
1087 sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
)
1088 return -ESOCKTNOSUPPORT
;
1090 if (sock
->type
== SOCK_RAW
&& !kern
&& !capable(CAP_NET_RAW
))
1093 sock
->ops
= &l2cap_sock_ops
;
1095 sk
= l2cap_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
1099 l2cap_sock_init(sk
, NULL
);
1103 const struct proto_ops l2cap_sock_ops
= {
1104 .family
= PF_BLUETOOTH
,
1105 .owner
= THIS_MODULE
,
1106 .release
= l2cap_sock_release
,
1107 .bind
= l2cap_sock_bind
,
1108 .connect
= l2cap_sock_connect
,
1109 .listen
= l2cap_sock_listen
,
1110 .accept
= l2cap_sock_accept
,
1111 .getname
= l2cap_sock_getname
,
1112 .sendmsg
= l2cap_sock_sendmsg
,
1113 .recvmsg
= l2cap_sock_recvmsg
,
1114 .poll
= bt_sock_poll
,
1115 .ioctl
= bt_sock_ioctl
,
1116 .mmap
= sock_no_mmap
,
1117 .socketpair
= sock_no_socketpair
,
1118 .shutdown
= l2cap_sock_shutdown
,
1119 .setsockopt
= l2cap_sock_setsockopt
,
1120 .getsockopt
= l2cap_sock_getsockopt
1123 static const struct net_proto_family l2cap_sock_family_ops
= {
1124 .family
= PF_BLUETOOTH
,
1125 .owner
= THIS_MODULE
,
1126 .create
= l2cap_sock_create
,
1129 int __init
l2cap_init_sockets(void)
1133 err
= proto_register(&l2cap_proto
, 0);
1137 err
= bt_sock_register(BTPROTO_L2CAP
, &l2cap_sock_family_ops
);
1141 BT_INFO("L2CAP socket layer initialized");
1146 BT_ERR("L2CAP socket registration failed");
1147 proto_unregister(&l2cap_proto
);
1151 void l2cap_cleanup_sockets(void)
1153 if (bt_sock_unregister(BTPROTO_L2CAP
) < 0)
1154 BT_ERR("L2CAP socket unregistration failed");
1156 proto_unregister(&l2cap_proto
);