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>
43 #include <linux/debugfs.h>
44 #include <linux/seq_file.h>
45 #include <linux/security.h>
48 #include <asm/system.h>
49 #include <linux/uaccess.h>
51 #include <net/bluetooth/bluetooth.h>
52 #include <net/bluetooth/hci_core.h>
53 #include <net/bluetooth/l2cap.h>
54 #include <net/bluetooth/rfcomm.h>
56 static const struct proto_ops rfcomm_sock_ops
;
58 static struct bt_sock_list rfcomm_sk_list
= {
59 .lock
= __RW_LOCK_UNLOCKED(rfcomm_sk_list
.lock
)
62 static void rfcomm_sock_close(struct sock
*sk
);
63 static void rfcomm_sock_kill(struct sock
*sk
);
65 /* ---- DLC callbacks ----
67 * called under rfcomm_dlc_lock()
69 static void rfcomm_sk_data_ready(struct rfcomm_dlc
*d
, struct sk_buff
*skb
)
71 struct sock
*sk
= d
->owner
;
75 atomic_add(skb
->len
, &sk
->sk_rmem_alloc
);
76 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
77 sk
->sk_data_ready(sk
, skb
->len
);
79 if (atomic_read(&sk
->sk_rmem_alloc
) >= sk
->sk_rcvbuf
)
80 rfcomm_dlc_throttle(d
);
83 static void rfcomm_sk_state_change(struct rfcomm_dlc
*d
, int err
)
85 struct sock
*sk
= d
->owner
, *parent
;
91 BT_DBG("dlc %p state %ld err %d", d
, d
->state
, err
);
93 local_irq_save(flags
);
99 sk
->sk_state
= d
->state
;
101 parent
= bt_sk(sk
)->parent
;
103 if (d
->state
== BT_CLOSED
) {
104 sock_set_flag(sk
, SOCK_ZAPPED
);
105 bt_accept_unlink(sk
);
107 parent
->sk_data_ready(parent
, 0);
109 if (d
->state
== BT_CONNECTED
)
110 rfcomm_session_getaddr(d
->session
, &bt_sk(sk
)->src
, NULL
);
111 sk
->sk_state_change(sk
);
115 local_irq_restore(flags
);
117 if (parent
&& sock_flag(sk
, SOCK_ZAPPED
)) {
118 /* We have to drop DLC lock here, otherwise
119 * rfcomm_sock_destruct() will dead lock. */
120 rfcomm_dlc_unlock(d
);
121 rfcomm_sock_kill(sk
);
126 /* ---- Socket functions ---- */
127 static struct sock
*__rfcomm_get_sock_by_addr(u8 channel
, bdaddr_t
*src
)
129 struct sock
*sk
= NULL
;
130 struct hlist_node
*node
;
132 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
133 if (rfcomm_pi(sk
)->channel
== channel
&&
134 !bacmp(&bt_sk(sk
)->src
, src
))
138 return node
? sk
: NULL
;
141 /* Find socket with channel and source bdaddr.
142 * Returns closest match.
144 static struct sock
*rfcomm_get_sock_by_channel(int state
, u8 channel
, bdaddr_t
*src
)
146 struct sock
*sk
= NULL
, *sk1
= NULL
;
147 struct hlist_node
*node
;
149 read_lock(&rfcomm_sk_list
.lock
);
151 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
152 if (state
&& sk
->sk_state
!= state
)
155 if (rfcomm_pi(sk
)->channel
== channel
) {
157 if (!bacmp(&bt_sk(sk
)->src
, src
))
161 if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
))
166 read_unlock(&rfcomm_sk_list
.lock
);
168 return node
? sk
: sk1
;
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 security_sk_clone(parent
, sk
);
271 pi
->dlc
->defer_setup
= 0;
273 pi
->sec_level
= BT_SECURITY_LOW
;
277 pi
->dlc
->sec_level
= pi
->sec_level
;
278 pi
->dlc
->role_switch
= pi
->role_switch
;
281 static struct proto rfcomm_proto
= {
283 .owner
= THIS_MODULE
,
284 .obj_size
= sizeof(struct rfcomm_pinfo
)
287 static struct sock
*rfcomm_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
289 struct rfcomm_dlc
*d
;
292 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &rfcomm_proto
);
296 sock_init_data(sock
, sk
);
297 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
299 d
= rfcomm_dlc_alloc(prio
);
305 d
->data_ready
= rfcomm_sk_data_ready
;
306 d
->state_change
= rfcomm_sk_state_change
;
308 rfcomm_pi(sk
)->dlc
= d
;
311 sk
->sk_destruct
= rfcomm_sock_destruct
;
312 sk
->sk_sndtimeo
= RFCOMM_CONN_TIMEOUT
;
314 sk
->sk_sndbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
315 sk
->sk_rcvbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
317 sock_reset_flag(sk
, SOCK_ZAPPED
);
319 sk
->sk_protocol
= proto
;
320 sk
->sk_state
= BT_OPEN
;
322 bt_sock_link(&rfcomm_sk_list
, sk
);
328 static int rfcomm_sock_create(struct net
*net
, struct socket
*sock
,
329 int protocol
, int kern
)
333 BT_DBG("sock %p", sock
);
335 sock
->state
= SS_UNCONNECTED
;
337 if (sock
->type
!= SOCK_STREAM
&& sock
->type
!= SOCK_RAW
)
338 return -ESOCKTNOSUPPORT
;
340 sock
->ops
= &rfcomm_sock_ops
;
342 sk
= rfcomm_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
346 rfcomm_sock_init(sk
, NULL
);
350 static int rfcomm_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
352 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
353 struct sock
*sk
= sock
->sk
;
356 BT_DBG("sk %p %s", sk
, batostr(&sa
->rc_bdaddr
));
358 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
363 if (sk
->sk_state
!= BT_OPEN
) {
368 if (sk
->sk_type
!= SOCK_STREAM
) {
373 write_lock(&rfcomm_sk_list
.lock
);
375 if (sa
->rc_channel
&& __rfcomm_get_sock_by_addr(sa
->rc_channel
, &sa
->rc_bdaddr
)) {
378 /* Save source address */
379 bacpy(&bt_sk(sk
)->src
, &sa
->rc_bdaddr
);
380 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
381 sk
->sk_state
= BT_BOUND
;
384 write_unlock(&rfcomm_sk_list
.lock
);
391 static int rfcomm_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
393 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
394 struct sock
*sk
= sock
->sk
;
395 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
400 if (alen
< sizeof(struct sockaddr_rc
) ||
401 addr
->sa_family
!= AF_BLUETOOTH
)
406 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
) {
411 if (sk
->sk_type
!= SOCK_STREAM
) {
416 sk
->sk_state
= BT_CONNECT
;
417 bacpy(&bt_sk(sk
)->dst
, &sa
->rc_bdaddr
);
418 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
420 d
->sec_level
= rfcomm_pi(sk
)->sec_level
;
421 d
->role_switch
= rfcomm_pi(sk
)->role_switch
;
423 err
= rfcomm_dlc_open(d
, &bt_sk(sk
)->src
, &sa
->rc_bdaddr
, sa
->rc_channel
);
425 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
426 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
433 static int rfcomm_sock_listen(struct socket
*sock
, int backlog
)
435 struct sock
*sk
= sock
->sk
;
438 BT_DBG("sk %p backlog %d", sk
, backlog
);
442 if (sk
->sk_state
!= BT_BOUND
) {
447 if (sk
->sk_type
!= SOCK_STREAM
) {
452 if (!rfcomm_pi(sk
)->channel
) {
453 bdaddr_t
*src
= &bt_sk(sk
)->src
;
458 write_lock(&rfcomm_sk_list
.lock
);
460 for (channel
= 1; channel
< 31; channel
++)
461 if (!__rfcomm_get_sock_by_addr(channel
, src
)) {
462 rfcomm_pi(sk
)->channel
= channel
;
467 write_unlock(&rfcomm_sk_list
.lock
);
473 sk
->sk_max_ack_backlog
= backlog
;
474 sk
->sk_ack_backlog
= 0;
475 sk
->sk_state
= BT_LISTEN
;
482 static int rfcomm_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
484 DECLARE_WAITQUEUE(wait
, current
);
485 struct sock
*sk
= sock
->sk
, *nsk
;
491 if (sk
->sk_type
!= SOCK_STREAM
) {
496 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
498 BT_DBG("sk %p timeo %ld", sk
, timeo
);
500 /* Wait for an incoming connection. (wake-one). */
501 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
503 set_current_state(TASK_INTERRUPTIBLE
);
505 if (sk
->sk_state
!= BT_LISTEN
) {
510 nsk
= bt_accept_dequeue(sk
, newsock
);
519 if (signal_pending(current
)) {
520 err
= sock_intr_errno(timeo
);
525 timeo
= schedule_timeout(timeo
);
528 __set_current_state(TASK_RUNNING
);
529 remove_wait_queue(sk_sleep(sk
), &wait
);
534 newsock
->state
= SS_CONNECTED
;
536 BT_DBG("new socket %p", nsk
);
543 static int rfcomm_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
545 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
546 struct sock
*sk
= sock
->sk
;
548 BT_DBG("sock %p, sk %p", sock
, sk
);
550 sa
->rc_family
= AF_BLUETOOTH
;
551 sa
->rc_channel
= rfcomm_pi(sk
)->channel
;
553 bacpy(&sa
->rc_bdaddr
, &bt_sk(sk
)->dst
);
555 bacpy(&sa
->rc_bdaddr
, &bt_sk(sk
)->src
);
557 *len
= sizeof(struct sockaddr_rc
);
561 static int rfcomm_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
562 struct msghdr
*msg
, size_t len
)
564 struct sock
*sk
= sock
->sk
;
565 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
569 if (test_bit(RFCOMM_DEFER_SETUP
, &d
->flags
))
572 if (msg
->msg_flags
& MSG_OOB
)
575 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
578 BT_DBG("sock %p, sk %p", sock
, sk
);
583 size_t size
= min_t(size_t, len
, d
->mtu
);
586 skb
= sock_alloc_send_skb(sk
, size
+ RFCOMM_SKB_RESERVE
,
587 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
593 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
595 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
603 skb
->priority
= sk
->sk_priority
;
605 err
= rfcomm_dlc_send(d
, skb
);
622 static int rfcomm_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
623 struct msghdr
*msg
, size_t size
, int flags
)
625 struct sock
*sk
= sock
->sk
;
626 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
629 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
630 rfcomm_dlc_accept(d
);
634 len
= bt_sock_stream_recvmsg(iocb
, sock
, msg
, size
, flags
);
637 if (!(flags
& MSG_PEEK
) && len
> 0)
638 atomic_sub(len
, &sk
->sk_rmem_alloc
);
640 if (atomic_read(&sk
->sk_rmem_alloc
) <= (sk
->sk_rcvbuf
>> 2))
641 rfcomm_dlc_unthrottle(rfcomm_pi(sk
)->dlc
);
647 static int rfcomm_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
649 struct sock
*sk
= sock
->sk
;
659 if (get_user(opt
, (u32 __user
*) optval
)) {
664 if (opt
& RFCOMM_LM_AUTH
)
665 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_LOW
;
666 if (opt
& RFCOMM_LM_ENCRYPT
)
667 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_MEDIUM
;
668 if (opt
& RFCOMM_LM_SECURE
)
669 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_HIGH
;
671 rfcomm_pi(sk
)->role_switch
= (opt
& RFCOMM_LM_MASTER
);
683 static int rfcomm_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
685 struct sock
*sk
= sock
->sk
;
686 struct bt_security sec
;
693 if (level
== SOL_RFCOMM
)
694 return rfcomm_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
696 if (level
!= SOL_BLUETOOTH
)
703 if (sk
->sk_type
!= SOCK_STREAM
) {
708 sec
.level
= BT_SECURITY_LOW
;
710 len
= min_t(unsigned int, sizeof(sec
), optlen
);
711 if (copy_from_user((char *) &sec
, optval
, len
)) {
716 if (sec
.level
> BT_SECURITY_HIGH
) {
721 rfcomm_pi(sk
)->sec_level
= sec
.level
;
725 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
730 if (get_user(opt
, (u32 __user
*) optval
)) {
735 bt_sk(sk
)->defer_setup
= opt
;
747 static int rfcomm_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
749 struct sock
*sk
= sock
->sk
;
750 struct rfcomm_conninfo cinfo
;
751 struct l2cap_conn
*conn
= l2cap_pi(sk
)->chan
->conn
;
757 if (get_user(len
, optlen
))
764 switch (rfcomm_pi(sk
)->sec_level
) {
765 case BT_SECURITY_LOW
:
766 opt
= RFCOMM_LM_AUTH
;
768 case BT_SECURITY_MEDIUM
:
769 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
;
771 case BT_SECURITY_HIGH
:
772 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
|
780 if (rfcomm_pi(sk
)->role_switch
)
781 opt
|= RFCOMM_LM_MASTER
;
783 if (put_user(opt
, (u32 __user
*) optval
))
787 case RFCOMM_CONNINFO
:
788 if (sk
->sk_state
!= BT_CONNECTED
&&
789 !rfcomm_pi(sk
)->dlc
->defer_setup
) {
794 memset(&cinfo
, 0, sizeof(cinfo
));
795 cinfo
.hci_handle
= conn
->hcon
->handle
;
796 memcpy(cinfo
.dev_class
, conn
->hcon
->dev_class
, 3);
798 len
= min_t(unsigned int, len
, sizeof(cinfo
));
799 if (copy_to_user(optval
, (char *) &cinfo
, len
))
813 static int rfcomm_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
815 struct sock
*sk
= sock
->sk
;
816 struct bt_security sec
;
821 if (level
== SOL_RFCOMM
)
822 return rfcomm_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
824 if (level
!= SOL_BLUETOOTH
)
827 if (get_user(len
, optlen
))
834 if (sk
->sk_type
!= SOCK_STREAM
) {
839 sec
.level
= rfcomm_pi(sk
)->sec_level
;
841 len
= min_t(unsigned int, len
, sizeof(sec
));
842 if (copy_to_user(optval
, (char *) &sec
, len
))
848 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
853 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
867 static int rfcomm_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
869 struct sock
*sk __maybe_unused
= sock
->sk
;
872 BT_DBG("sk %p cmd %x arg %lx", sk
, cmd
, arg
);
874 err
= bt_sock_ioctl(sock
, cmd
, arg
);
876 if (err
== -ENOIOCTLCMD
) {
877 #ifdef CONFIG_BT_RFCOMM_TTY
879 err
= rfcomm_dev_ioctl(sk
, cmd
, (void __user
*) arg
);
889 static int rfcomm_sock_shutdown(struct socket
*sock
, int how
)
891 struct sock
*sk
= sock
->sk
;
894 BT_DBG("sock %p, sk %p", sock
, sk
);
900 if (!sk
->sk_shutdown
) {
901 sk
->sk_shutdown
= SHUTDOWN_MASK
;
902 __rfcomm_sock_close(sk
);
904 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
905 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
911 static int rfcomm_sock_release(struct socket
*sock
)
913 struct sock
*sk
= sock
->sk
;
916 BT_DBG("sock %p, sk %p", sock
, sk
);
921 err
= rfcomm_sock_shutdown(sock
, 2);
924 rfcomm_sock_kill(sk
);
928 /* ---- RFCOMM core layer callbacks ----
930 * called under rfcomm_lock()
932 int rfcomm_connect_ind(struct rfcomm_session
*s
, u8 channel
, struct rfcomm_dlc
**d
)
934 struct sock
*sk
, *parent
;
938 BT_DBG("session %p channel %d", s
, channel
);
940 rfcomm_session_getaddr(s
, &src
, &dst
);
942 /* Check if we have socket listening on channel */
943 parent
= rfcomm_get_sock_by_channel(BT_LISTEN
, channel
, &src
);
947 bh_lock_sock(parent
);
949 /* Check for backlog size */
950 if (sk_acceptq_is_full(parent
)) {
951 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
955 sk
= rfcomm_sock_alloc(sock_net(parent
), NULL
, BTPROTO_RFCOMM
, GFP_ATOMIC
);
959 bt_sock_reclassify_lock(sk
, BTPROTO_RFCOMM
);
961 rfcomm_sock_init(sk
, parent
);
962 bacpy(&bt_sk(sk
)->src
, &src
);
963 bacpy(&bt_sk(sk
)->dst
, &dst
);
964 rfcomm_pi(sk
)->channel
= channel
;
966 sk
->sk_state
= BT_CONFIG
;
967 bt_accept_enqueue(parent
, sk
);
969 /* Accept connection and return socket DLC */
970 *d
= rfcomm_pi(sk
)->dlc
;
974 bh_unlock_sock(parent
);
976 if (bt_sk(parent
)->defer_setup
)
977 parent
->sk_state_change(parent
);
982 static int rfcomm_sock_debugfs_show(struct seq_file
*f
, void *p
)
985 struct hlist_node
*node
;
987 read_lock(&rfcomm_sk_list
.lock
);
989 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
990 seq_printf(f
, "%s %s %d %d\n",
991 batostr(&bt_sk(sk
)->src
),
992 batostr(&bt_sk(sk
)->dst
),
993 sk
->sk_state
, rfcomm_pi(sk
)->channel
);
996 read_unlock(&rfcomm_sk_list
.lock
);
1001 static int rfcomm_sock_debugfs_open(struct inode
*inode
, struct file
*file
)
1003 return single_open(file
, rfcomm_sock_debugfs_show
, inode
->i_private
);
1006 static const struct file_operations rfcomm_sock_debugfs_fops
= {
1007 .open
= rfcomm_sock_debugfs_open
,
1009 .llseek
= seq_lseek
,
1010 .release
= single_release
,
1013 static struct dentry
*rfcomm_sock_debugfs
;
1015 static const struct proto_ops rfcomm_sock_ops
= {
1016 .family
= PF_BLUETOOTH
,
1017 .owner
= THIS_MODULE
,
1018 .release
= rfcomm_sock_release
,
1019 .bind
= rfcomm_sock_bind
,
1020 .connect
= rfcomm_sock_connect
,
1021 .listen
= rfcomm_sock_listen
,
1022 .accept
= rfcomm_sock_accept
,
1023 .getname
= rfcomm_sock_getname
,
1024 .sendmsg
= rfcomm_sock_sendmsg
,
1025 .recvmsg
= rfcomm_sock_recvmsg
,
1026 .shutdown
= rfcomm_sock_shutdown
,
1027 .setsockopt
= rfcomm_sock_setsockopt
,
1028 .getsockopt
= rfcomm_sock_getsockopt
,
1029 .ioctl
= rfcomm_sock_ioctl
,
1030 .poll
= bt_sock_poll
,
1031 .socketpair
= sock_no_socketpair
,
1032 .mmap
= sock_no_mmap
1035 static const struct net_proto_family rfcomm_sock_family_ops
= {
1036 .family
= PF_BLUETOOTH
,
1037 .owner
= THIS_MODULE
,
1038 .create
= rfcomm_sock_create
1041 int __init
rfcomm_init_sockets(void)
1045 err
= proto_register(&rfcomm_proto
, 0);
1049 err
= bt_sock_register(BTPROTO_RFCOMM
, &rfcomm_sock_family_ops
);
1054 rfcomm_sock_debugfs
= debugfs_create_file("rfcomm", 0444,
1055 bt_debugfs
, NULL
, &rfcomm_sock_debugfs_fops
);
1056 if (!rfcomm_sock_debugfs
)
1057 BT_ERR("Failed to create RFCOMM debug file");
1060 BT_INFO("RFCOMM socket layer initialized");
1065 BT_ERR("RFCOMM socket layer registration failed");
1066 proto_unregister(&rfcomm_proto
);
1070 void __exit
rfcomm_cleanup_sockets(void)
1072 debugfs_remove(rfcomm_sock_debugfs
);
1074 if (bt_sock_unregister(BTPROTO_RFCOMM
) < 0)
1075 BT_ERR("RFCOMM socket layer unregistration failed");
1077 proto_unregister(&rfcomm_proto
);