2 RFCOMM implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
39 #include <linux/socket.h>
40 #include <linux/skbuff.h>
41 #include <linux/list.h>
42 #include <linux/device.h>
45 #include <asm/system.h>
46 #include <asm/uaccess.h>
48 #include <net/bluetooth/bluetooth.h>
49 #include <net/bluetooth/hci_core.h>
50 #include <net/bluetooth/l2cap.h>
51 #include <net/bluetooth/rfcomm.h>
53 static const struct proto_ops rfcomm_sock_ops
;
55 static struct bt_sock_list rfcomm_sk_list
= {
56 .lock
= __RW_LOCK_UNLOCKED(rfcomm_sk_list
.lock
)
59 static void rfcomm_sock_close(struct sock
*sk
);
60 static void rfcomm_sock_kill(struct sock
*sk
);
62 /* ---- DLC callbacks ----
64 * called under rfcomm_dlc_lock()
66 static void rfcomm_sk_data_ready(struct rfcomm_dlc
*d
, struct sk_buff
*skb
)
68 struct sock
*sk
= d
->owner
;
72 atomic_add(skb
->len
, &sk
->sk_rmem_alloc
);
73 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
74 sk
->sk_data_ready(sk
, skb
->len
);
76 if (atomic_read(&sk
->sk_rmem_alloc
) >= sk
->sk_rcvbuf
)
77 rfcomm_dlc_throttle(d
);
80 static void rfcomm_sk_state_change(struct rfcomm_dlc
*d
, int err
)
82 struct sock
*sk
= d
->owner
, *parent
;
86 BT_DBG("dlc %p state %ld err %d", d
, d
->state
, err
);
93 sk
->sk_state
= d
->state
;
95 parent
= bt_sk(sk
)->parent
;
97 if (d
->state
== BT_CLOSED
) {
98 sock_set_flag(sk
, SOCK_ZAPPED
);
101 parent
->sk_data_ready(parent
, 0);
103 if (d
->state
== BT_CONNECTED
)
104 rfcomm_session_getaddr(d
->session
, &bt_sk(sk
)->src
, NULL
);
105 sk
->sk_state_change(sk
);
110 if (parent
&& sock_flag(sk
, SOCK_ZAPPED
)) {
111 /* We have to drop DLC lock here, otherwise
112 * rfcomm_sock_destruct() will dead lock. */
113 rfcomm_dlc_unlock(d
);
114 rfcomm_sock_kill(sk
);
119 /* ---- Socket functions ---- */
120 static struct sock
*__rfcomm_get_sock_by_addr(u8 channel
, bdaddr_t
*src
)
122 struct sock
*sk
= NULL
;
123 struct hlist_node
*node
;
125 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
126 if (rfcomm_pi(sk
)->channel
== channel
&&
127 !bacmp(&bt_sk(sk
)->src
, src
))
131 return node
? sk
: NULL
;
134 /* Find socket with channel and source bdaddr.
135 * Returns closest match.
137 static struct sock
*__rfcomm_get_sock_by_channel(int state
, u8 channel
, bdaddr_t
*src
)
139 struct sock
*sk
= NULL
, *sk1
= NULL
;
140 struct hlist_node
*node
;
142 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
143 if (state
&& sk
->sk_state
!= state
)
146 if (rfcomm_pi(sk
)->channel
== channel
) {
148 if (!bacmp(&bt_sk(sk
)->src
, src
))
152 if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
))
156 return node
? sk
: sk1
;
159 /* Find socket with given address (channel, src).
160 * Returns locked socket */
161 static inline struct sock
*rfcomm_get_sock_by_channel(int state
, u8 channel
, bdaddr_t
*src
)
164 read_lock(&rfcomm_sk_list
.lock
);
165 s
= __rfcomm_get_sock_by_channel(state
, channel
, src
);
166 if (s
) bh_lock_sock(s
);
167 read_unlock(&rfcomm_sk_list
.lock
);
171 static void rfcomm_sock_destruct(struct sock
*sk
)
173 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
175 BT_DBG("sk %p dlc %p", sk
, d
);
177 skb_queue_purge(&sk
->sk_receive_queue
);
178 skb_queue_purge(&sk
->sk_write_queue
);
181 rfcomm_pi(sk
)->dlc
= NULL
;
183 /* Detach DLC if it's owned by this socket */
186 rfcomm_dlc_unlock(d
);
191 static void rfcomm_sock_cleanup_listen(struct sock
*parent
)
195 BT_DBG("parent %p", parent
);
197 /* Close not yet accepted dlcs */
198 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
199 rfcomm_sock_close(sk
);
200 rfcomm_sock_kill(sk
);
203 parent
->sk_state
= BT_CLOSED
;
204 sock_set_flag(parent
, SOCK_ZAPPED
);
207 /* Kill socket (only if zapped and orphan)
208 * Must be called on unlocked socket.
210 static void rfcomm_sock_kill(struct sock
*sk
)
212 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
215 BT_DBG("sk %p state %d refcnt %d", sk
, sk
->sk_state
, atomic_read(&sk
->sk_refcnt
));
217 /* Kill poor orphan */
218 bt_sock_unlink(&rfcomm_sk_list
, sk
);
219 sock_set_flag(sk
, SOCK_DEAD
);
223 static void __rfcomm_sock_close(struct sock
*sk
)
225 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
227 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
229 switch (sk
->sk_state
) {
231 rfcomm_sock_cleanup_listen(sk
);
238 rfcomm_dlc_close(d
, 0);
241 sock_set_flag(sk
, SOCK_ZAPPED
);
247 * Must be called on unlocked socket.
249 static void rfcomm_sock_close(struct sock
*sk
)
252 __rfcomm_sock_close(sk
);
256 static void rfcomm_sock_init(struct sock
*sk
, struct sock
*parent
)
258 struct rfcomm_pinfo
*pi
= rfcomm_pi(sk
);
263 sk
->sk_type
= parent
->sk_type
;
264 pi
->dlc
->defer_setup
= bt_sk(parent
)->defer_setup
;
266 pi
->sec_level
= rfcomm_pi(parent
)->sec_level
;
267 pi
->role_switch
= rfcomm_pi(parent
)->role_switch
;
269 pi
->dlc
->defer_setup
= 0;
271 pi
->sec_level
= BT_SECURITY_LOW
;
275 pi
->dlc
->sec_level
= pi
->sec_level
;
276 pi
->dlc
->role_switch
= pi
->role_switch
;
279 static struct proto rfcomm_proto
= {
281 .owner
= THIS_MODULE
,
282 .obj_size
= sizeof(struct rfcomm_pinfo
)
285 static struct sock
*rfcomm_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
287 struct rfcomm_dlc
*d
;
290 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &rfcomm_proto
);
294 sock_init_data(sock
, sk
);
295 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
297 d
= rfcomm_dlc_alloc(prio
);
303 d
->data_ready
= rfcomm_sk_data_ready
;
304 d
->state_change
= rfcomm_sk_state_change
;
306 rfcomm_pi(sk
)->dlc
= d
;
309 sk
->sk_destruct
= rfcomm_sock_destruct
;
310 sk
->sk_sndtimeo
= RFCOMM_CONN_TIMEOUT
;
312 sk
->sk_sndbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
313 sk
->sk_rcvbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
315 sock_reset_flag(sk
, SOCK_ZAPPED
);
317 sk
->sk_protocol
= proto
;
318 sk
->sk_state
= BT_OPEN
;
320 bt_sock_link(&rfcomm_sk_list
, sk
);
326 static int rfcomm_sock_create(struct net
*net
, struct socket
*sock
,
327 int protocol
, int kern
)
331 BT_DBG("sock %p", sock
);
333 sock
->state
= SS_UNCONNECTED
;
335 if (sock
->type
!= SOCK_STREAM
&& sock
->type
!= SOCK_RAW
)
336 return -ESOCKTNOSUPPORT
;
338 sock
->ops
= &rfcomm_sock_ops
;
340 sk
= rfcomm_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
344 rfcomm_sock_init(sk
, NULL
);
348 static int rfcomm_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
350 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
351 struct sock
*sk
= sock
->sk
;
354 BT_DBG("sk %p %s", sk
, batostr(&sa
->rc_bdaddr
));
356 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
361 if (sk
->sk_state
!= BT_OPEN
) {
366 if (sk
->sk_type
!= SOCK_STREAM
) {
371 write_lock_bh(&rfcomm_sk_list
.lock
);
373 if (sa
->rc_channel
&& __rfcomm_get_sock_by_addr(sa
->rc_channel
, &sa
->rc_bdaddr
)) {
376 /* Save source address */
377 bacpy(&bt_sk(sk
)->src
, &sa
->rc_bdaddr
);
378 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
379 sk
->sk_state
= BT_BOUND
;
382 write_unlock_bh(&rfcomm_sk_list
.lock
);
389 static int rfcomm_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
391 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
392 struct sock
*sk
= sock
->sk
;
393 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
398 if (addr
->sa_family
!= AF_BLUETOOTH
|| alen
< sizeof(struct sockaddr_rc
))
403 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
) {
408 if (sk
->sk_type
!= SOCK_STREAM
) {
413 sk
->sk_state
= BT_CONNECT
;
414 bacpy(&bt_sk(sk
)->dst
, &sa
->rc_bdaddr
);
415 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
417 d
->sec_level
= rfcomm_pi(sk
)->sec_level
;
418 d
->role_switch
= rfcomm_pi(sk
)->role_switch
;
420 err
= rfcomm_dlc_open(d
, &bt_sk(sk
)->src
, &sa
->rc_bdaddr
, sa
->rc_channel
);
422 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
423 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
430 static int rfcomm_sock_listen(struct socket
*sock
, int backlog
)
432 struct sock
*sk
= sock
->sk
;
435 BT_DBG("sk %p backlog %d", sk
, backlog
);
439 if (sk
->sk_state
!= BT_BOUND
) {
444 if (sk
->sk_type
!= SOCK_STREAM
) {
449 if (!rfcomm_pi(sk
)->channel
) {
450 bdaddr_t
*src
= &bt_sk(sk
)->src
;
455 write_lock_bh(&rfcomm_sk_list
.lock
);
457 for (channel
= 1; channel
< 31; channel
++)
458 if (!__rfcomm_get_sock_by_addr(channel
, src
)) {
459 rfcomm_pi(sk
)->channel
= channel
;
464 write_unlock_bh(&rfcomm_sk_list
.lock
);
470 sk
->sk_max_ack_backlog
= backlog
;
471 sk
->sk_ack_backlog
= 0;
472 sk
->sk_state
= BT_LISTEN
;
479 static int rfcomm_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
481 DECLARE_WAITQUEUE(wait
, current
);
482 struct sock
*sk
= sock
->sk
, *nsk
;
488 if (sk
->sk_state
!= BT_LISTEN
) {
493 if (sk
->sk_type
!= SOCK_STREAM
) {
498 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
500 BT_DBG("sk %p timeo %ld", sk
, timeo
);
502 /* Wait for an incoming connection. (wake-one). */
503 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
504 while (!(nsk
= bt_accept_dequeue(sk
, newsock
))) {
505 set_current_state(TASK_INTERRUPTIBLE
);
512 timeo
= schedule_timeout(timeo
);
515 if (sk
->sk_state
!= BT_LISTEN
) {
520 if (signal_pending(current
)) {
521 err
= sock_intr_errno(timeo
);
525 set_current_state(TASK_RUNNING
);
526 remove_wait_queue(sk
->sk_sleep
, &wait
);
531 newsock
->state
= SS_CONNECTED
;
533 BT_DBG("new socket %p", nsk
);
540 static int rfcomm_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
542 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
543 struct sock
*sk
= sock
->sk
;
545 BT_DBG("sock %p, sk %p", sock
, sk
);
547 sa
->rc_family
= AF_BLUETOOTH
;
548 sa
->rc_channel
= rfcomm_pi(sk
)->channel
;
550 bacpy(&sa
->rc_bdaddr
, &bt_sk(sk
)->dst
);
552 bacpy(&sa
->rc_bdaddr
, &bt_sk(sk
)->src
);
554 *len
= sizeof(struct sockaddr_rc
);
558 static int rfcomm_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
559 struct msghdr
*msg
, size_t len
)
561 struct sock
*sk
= sock
->sk
;
562 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
566 if (test_bit(RFCOMM_DEFER_SETUP
, &d
->flags
))
569 if (msg
->msg_flags
& MSG_OOB
)
572 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
575 BT_DBG("sock %p, sk %p", sock
, sk
);
580 size_t size
= min_t(size_t, len
, d
->mtu
);
583 skb
= sock_alloc_send_skb(sk
, size
+ RFCOMM_SKB_RESERVE
,
584 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
590 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
592 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
600 err
= rfcomm_dlc_send(d
, skb
);
617 static long rfcomm_sock_data_wait(struct sock
*sk
, long timeo
)
619 DECLARE_WAITQUEUE(wait
, current
);
621 add_wait_queue(sk
->sk_sleep
, &wait
);
623 set_current_state(TASK_INTERRUPTIBLE
);
625 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
627 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
628 signal_pending(current
) ||
632 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
634 timeo
= schedule_timeout(timeo
);
636 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
639 __set_current_state(TASK_RUNNING
);
640 remove_wait_queue(sk
->sk_sleep
, &wait
);
644 static int rfcomm_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
645 struct msghdr
*msg
, size_t size
, int flags
)
647 struct sock
*sk
= sock
->sk
;
648 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
650 size_t target
, copied
= 0;
653 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
654 rfcomm_dlc_accept(d
);
661 msg
->msg_namelen
= 0;
663 BT_DBG("sk %p size %zu", sk
, size
);
667 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, size
);
668 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
674 skb
= skb_dequeue(&sk
->sk_receive_queue
);
676 if (copied
>= target
)
679 if ((err
= sock_error(sk
)) != 0)
681 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
688 timeo
= rfcomm_sock_data_wait(sk
, timeo
);
690 if (signal_pending(current
)) {
691 err
= sock_intr_errno(timeo
);
697 chunk
= min_t(unsigned int, skb
->len
, size
);
698 if (memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) {
699 skb_queue_head(&sk
->sk_receive_queue
, skb
);
707 sock_recv_ts_and_drops(msg
, sk
, skb
);
709 if (!(flags
& MSG_PEEK
)) {
710 atomic_sub(chunk
, &sk
->sk_rmem_alloc
);
712 skb_pull(skb
, chunk
);
714 skb_queue_head(&sk
->sk_receive_queue
, skb
);
720 /* put message back and return */
721 skb_queue_head(&sk
->sk_receive_queue
, skb
);
727 if (atomic_read(&sk
->sk_rmem_alloc
) <= (sk
->sk_rcvbuf
>> 2))
728 rfcomm_dlc_unthrottle(rfcomm_pi(sk
)->dlc
);
731 return copied
? : err
;
734 static int rfcomm_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
736 struct sock
*sk
= sock
->sk
;
746 if (get_user(opt
, (u32 __user
*) optval
)) {
751 if (opt
& RFCOMM_LM_AUTH
)
752 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_LOW
;
753 if (opt
& RFCOMM_LM_ENCRYPT
)
754 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_MEDIUM
;
755 if (opt
& RFCOMM_LM_SECURE
)
756 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_HIGH
;
758 rfcomm_pi(sk
)->role_switch
= (opt
& RFCOMM_LM_MASTER
);
770 static int rfcomm_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
772 struct sock
*sk
= sock
->sk
;
773 struct bt_security sec
;
779 if (level
== SOL_RFCOMM
)
780 return rfcomm_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
782 if (level
!= SOL_BLUETOOTH
)
789 if (sk
->sk_type
!= SOCK_STREAM
) {
794 sec
.level
= BT_SECURITY_LOW
;
796 len
= min_t(unsigned int, sizeof(sec
), optlen
);
797 if (copy_from_user((char *) &sec
, optval
, len
)) {
802 if (sec
.level
> BT_SECURITY_HIGH
) {
807 rfcomm_pi(sk
)->sec_level
= sec
.level
;
811 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
816 if (get_user(opt
, (u32 __user
*) optval
)) {
821 bt_sk(sk
)->defer_setup
= opt
;
833 static int rfcomm_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
835 struct sock
*sk
= sock
->sk
;
836 struct sock
*l2cap_sk
;
837 struct rfcomm_conninfo cinfo
;
843 if (get_user(len
, optlen
))
850 switch (rfcomm_pi(sk
)->sec_level
) {
851 case BT_SECURITY_LOW
:
852 opt
= RFCOMM_LM_AUTH
;
854 case BT_SECURITY_MEDIUM
:
855 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
;
857 case BT_SECURITY_HIGH
:
858 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
|
866 if (rfcomm_pi(sk
)->role_switch
)
867 opt
|= RFCOMM_LM_MASTER
;
869 if (put_user(opt
, (u32 __user
*) optval
))
873 case RFCOMM_CONNINFO
:
874 if (sk
->sk_state
!= BT_CONNECTED
&&
875 !rfcomm_pi(sk
)->dlc
->defer_setup
) {
880 l2cap_sk
= rfcomm_pi(sk
)->dlc
->session
->sock
->sk
;
882 cinfo
.hci_handle
= l2cap_pi(l2cap_sk
)->conn
->hcon
->handle
;
883 memcpy(cinfo
.dev_class
, l2cap_pi(l2cap_sk
)->conn
->hcon
->dev_class
, 3);
885 len
= min_t(unsigned int, len
, sizeof(cinfo
));
886 if (copy_to_user(optval
, (char *) &cinfo
, len
))
900 static int rfcomm_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
902 struct sock
*sk
= sock
->sk
;
903 struct bt_security sec
;
908 if (level
== SOL_RFCOMM
)
909 return rfcomm_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
911 if (level
!= SOL_BLUETOOTH
)
914 if (get_user(len
, optlen
))
921 if (sk
->sk_type
!= SOCK_STREAM
) {
926 sec
.level
= rfcomm_pi(sk
)->sec_level
;
928 len
= min_t(unsigned int, len
, sizeof(sec
));
929 if (copy_to_user(optval
, (char *) &sec
, len
))
935 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
940 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
954 static int rfcomm_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
956 struct sock
*sk __maybe_unused
= sock
->sk
;
959 BT_DBG("sk %p cmd %x arg %lx", sk
, cmd
, arg
);
961 err
= bt_sock_ioctl(sock
, cmd
, arg
);
963 if (err
== -ENOIOCTLCMD
) {
964 #ifdef CONFIG_BT_RFCOMM_TTY
966 err
= rfcomm_dev_ioctl(sk
, cmd
, (void __user
*) arg
);
976 static int rfcomm_sock_shutdown(struct socket
*sock
, int how
)
978 struct sock
*sk
= sock
->sk
;
981 BT_DBG("sock %p, sk %p", sock
, sk
);
986 if (!sk
->sk_shutdown
) {
987 sk
->sk_shutdown
= SHUTDOWN_MASK
;
988 __rfcomm_sock_close(sk
);
990 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
991 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
997 static int rfcomm_sock_release(struct socket
*sock
)
999 struct sock
*sk
= sock
->sk
;
1002 BT_DBG("sock %p, sk %p", sock
, sk
);
1007 err
= rfcomm_sock_shutdown(sock
, 2);
1010 rfcomm_sock_kill(sk
);
1014 /* ---- RFCOMM core layer callbacks ----
1016 * called under rfcomm_lock()
1018 int rfcomm_connect_ind(struct rfcomm_session
*s
, u8 channel
, struct rfcomm_dlc
**d
)
1020 struct sock
*sk
, *parent
;
1024 BT_DBG("session %p channel %d", s
, channel
);
1026 rfcomm_session_getaddr(s
, &src
, &dst
);
1028 /* Check if we have socket listening on channel */
1029 parent
= rfcomm_get_sock_by_channel(BT_LISTEN
, channel
, &src
);
1033 /* Check for backlog size */
1034 if (sk_acceptq_is_full(parent
)) {
1035 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
1039 sk
= rfcomm_sock_alloc(sock_net(parent
), NULL
, BTPROTO_RFCOMM
, GFP_ATOMIC
);
1043 rfcomm_sock_init(sk
, parent
);
1044 bacpy(&bt_sk(sk
)->src
, &src
);
1045 bacpy(&bt_sk(sk
)->dst
, &dst
);
1046 rfcomm_pi(sk
)->channel
= channel
;
1048 sk
->sk_state
= BT_CONFIG
;
1049 bt_accept_enqueue(parent
, sk
);
1051 /* Accept connection and return socket DLC */
1052 *d
= rfcomm_pi(sk
)->dlc
;
1056 bh_unlock_sock(parent
);
1058 if (bt_sk(parent
)->defer_setup
)
1059 parent
->sk_state_change(parent
);
1064 static ssize_t
rfcomm_sock_sysfs_show(struct class *dev
, char *buf
)
1067 struct hlist_node
*node
;
1070 read_lock_bh(&rfcomm_sk_list
.lock
);
1072 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
1073 str
+= sprintf(str
, "%s %s %d %d\n",
1074 batostr(&bt_sk(sk
)->src
), batostr(&bt_sk(sk
)->dst
),
1075 sk
->sk_state
, rfcomm_pi(sk
)->channel
);
1078 read_unlock_bh(&rfcomm_sk_list
.lock
);
1083 static CLASS_ATTR(rfcomm
, S_IRUGO
, rfcomm_sock_sysfs_show
, NULL
);
1085 static const struct proto_ops rfcomm_sock_ops
= {
1086 .family
= PF_BLUETOOTH
,
1087 .owner
= THIS_MODULE
,
1088 .release
= rfcomm_sock_release
,
1089 .bind
= rfcomm_sock_bind
,
1090 .connect
= rfcomm_sock_connect
,
1091 .listen
= rfcomm_sock_listen
,
1092 .accept
= rfcomm_sock_accept
,
1093 .getname
= rfcomm_sock_getname
,
1094 .sendmsg
= rfcomm_sock_sendmsg
,
1095 .recvmsg
= rfcomm_sock_recvmsg
,
1096 .shutdown
= rfcomm_sock_shutdown
,
1097 .setsockopt
= rfcomm_sock_setsockopt
,
1098 .getsockopt
= rfcomm_sock_getsockopt
,
1099 .ioctl
= rfcomm_sock_ioctl
,
1100 .poll
= bt_sock_poll
,
1101 .socketpair
= sock_no_socketpair
,
1102 .mmap
= sock_no_mmap
1105 static const struct net_proto_family rfcomm_sock_family_ops
= {
1106 .family
= PF_BLUETOOTH
,
1107 .owner
= THIS_MODULE
,
1108 .create
= rfcomm_sock_create
1111 int __init
rfcomm_init_sockets(void)
1115 err
= proto_register(&rfcomm_proto
, 0);
1119 err
= bt_sock_register(BTPROTO_RFCOMM
, &rfcomm_sock_family_ops
);
1123 if (class_create_file(bt_class
, &class_attr_rfcomm
) < 0)
1124 BT_ERR("Failed to create RFCOMM info file");
1126 BT_INFO("RFCOMM socket layer initialized");
1131 BT_ERR("RFCOMM socket layer registration failed");
1132 proto_unregister(&rfcomm_proto
);
1136 void rfcomm_cleanup_sockets(void)
1138 class_remove_file(bt_class
, &class_attr_rfcomm
);
1140 if (bt_sock_unregister(BTPROTO_RFCOMM
) < 0)
1141 BT_ERR("RFCOMM socket layer unregistration failed");
1143 proto_unregister(&rfcomm_proto
);