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/export.h>
29 #include <linux/debugfs.h>
31 #include <net/bluetooth/bluetooth.h>
32 #include <net/bluetooth/hci_core.h>
33 #include <net/bluetooth/l2cap.h>
34 #include <net/bluetooth/rfcomm.h>
36 static const struct proto_ops rfcomm_sock_ops
;
38 static struct bt_sock_list rfcomm_sk_list
= {
39 .lock
= __RW_LOCK_UNLOCKED(rfcomm_sk_list
.lock
)
42 static void rfcomm_sock_close(struct sock
*sk
);
43 static void rfcomm_sock_kill(struct sock
*sk
);
45 /* ---- DLC callbacks ----
47 * called under rfcomm_dlc_lock()
49 static void rfcomm_sk_data_ready(struct rfcomm_dlc
*d
, struct sk_buff
*skb
)
51 struct sock
*sk
= d
->owner
;
55 atomic_add(skb
->len
, &sk
->sk_rmem_alloc
);
56 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
57 sk
->sk_data_ready(sk
, skb
->len
);
59 if (atomic_read(&sk
->sk_rmem_alloc
) >= sk
->sk_rcvbuf
)
60 rfcomm_dlc_throttle(d
);
63 static void rfcomm_sk_state_change(struct rfcomm_dlc
*d
, int err
)
65 struct sock
*sk
= d
->owner
, *parent
;
71 BT_DBG("dlc %p state %ld err %d", d
, d
->state
, err
);
73 local_irq_save(flags
);
79 sk
->sk_state
= d
->state
;
81 parent
= bt_sk(sk
)->parent
;
83 if (d
->state
== BT_CLOSED
) {
84 sock_set_flag(sk
, SOCK_ZAPPED
);
87 parent
->sk_data_ready(parent
, 0);
89 if (d
->state
== BT_CONNECTED
)
90 rfcomm_session_getaddr(d
->session
,
91 &rfcomm_pi(sk
)->src
, NULL
);
92 sk
->sk_state_change(sk
);
96 local_irq_restore(flags
);
98 if (parent
&& sock_flag(sk
, SOCK_ZAPPED
)) {
99 /* We have to drop DLC lock here, otherwise
100 * rfcomm_sock_destruct() will dead lock. */
101 rfcomm_dlc_unlock(d
);
102 rfcomm_sock_kill(sk
);
107 /* ---- Socket functions ---- */
108 static struct sock
*__rfcomm_get_sock_by_addr(u8 channel
, bdaddr_t
*src
)
110 struct sock
*sk
= NULL
;
112 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
113 if (rfcomm_pi(sk
)->channel
== channel
&&
114 !bacmp(&rfcomm_pi(sk
)->src
, src
))
118 return sk
? sk
: NULL
;
121 /* Find socket with channel and source bdaddr.
122 * Returns closest match.
124 static struct sock
*rfcomm_get_sock_by_channel(int state
, u8 channel
, bdaddr_t
*src
)
126 struct sock
*sk
= NULL
, *sk1
= NULL
;
128 read_lock(&rfcomm_sk_list
.lock
);
130 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
131 if (state
&& sk
->sk_state
!= state
)
134 if (rfcomm_pi(sk
)->channel
== channel
) {
136 if (!bacmp(&rfcomm_pi(sk
)->src
, src
))
140 if (!bacmp(&rfcomm_pi(sk
)->src
, BDADDR_ANY
))
145 read_unlock(&rfcomm_sk_list
.lock
);
147 return sk
? sk
: sk1
;
150 static void rfcomm_sock_destruct(struct sock
*sk
)
152 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
154 BT_DBG("sk %p dlc %p", sk
, d
);
156 skb_queue_purge(&sk
->sk_receive_queue
);
157 skb_queue_purge(&sk
->sk_write_queue
);
160 rfcomm_pi(sk
)->dlc
= NULL
;
162 /* Detach DLC if it's owned by this socket */
165 rfcomm_dlc_unlock(d
);
170 static void rfcomm_sock_cleanup_listen(struct sock
*parent
)
174 BT_DBG("parent %p", parent
);
176 /* Close not yet accepted dlcs */
177 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
178 rfcomm_sock_close(sk
);
179 rfcomm_sock_kill(sk
);
182 parent
->sk_state
= BT_CLOSED
;
183 sock_set_flag(parent
, SOCK_ZAPPED
);
186 /* Kill socket (only if zapped and orphan)
187 * Must be called on unlocked socket.
189 static void rfcomm_sock_kill(struct sock
*sk
)
191 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
194 BT_DBG("sk %p state %d refcnt %d", sk
, sk
->sk_state
, atomic_read(&sk
->sk_refcnt
));
196 /* Kill poor orphan */
197 bt_sock_unlink(&rfcomm_sk_list
, sk
);
198 sock_set_flag(sk
, SOCK_DEAD
);
202 static void __rfcomm_sock_close(struct sock
*sk
)
204 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
206 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
208 switch (sk
->sk_state
) {
210 rfcomm_sock_cleanup_listen(sk
);
217 rfcomm_dlc_close(d
, 0);
220 sock_set_flag(sk
, SOCK_ZAPPED
);
226 * Must be called on unlocked socket.
228 static void rfcomm_sock_close(struct sock
*sk
)
231 __rfcomm_sock_close(sk
);
235 static void rfcomm_sock_init(struct sock
*sk
, struct sock
*parent
)
237 struct rfcomm_pinfo
*pi
= rfcomm_pi(sk
);
242 sk
->sk_type
= parent
->sk_type
;
243 pi
->dlc
->defer_setup
= test_bit(BT_SK_DEFER_SETUP
,
244 &bt_sk(parent
)->flags
);
246 pi
->sec_level
= rfcomm_pi(parent
)->sec_level
;
247 pi
->role_switch
= rfcomm_pi(parent
)->role_switch
;
249 security_sk_clone(parent
, sk
);
251 pi
->dlc
->defer_setup
= 0;
253 pi
->sec_level
= BT_SECURITY_LOW
;
257 pi
->dlc
->sec_level
= pi
->sec_level
;
258 pi
->dlc
->role_switch
= pi
->role_switch
;
261 static struct proto rfcomm_proto
= {
263 .owner
= THIS_MODULE
,
264 .obj_size
= sizeof(struct rfcomm_pinfo
)
267 static struct sock
*rfcomm_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
269 struct rfcomm_dlc
*d
;
272 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &rfcomm_proto
);
276 sock_init_data(sock
, sk
);
277 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
279 d
= rfcomm_dlc_alloc(prio
);
285 d
->data_ready
= rfcomm_sk_data_ready
;
286 d
->state_change
= rfcomm_sk_state_change
;
288 rfcomm_pi(sk
)->dlc
= d
;
291 sk
->sk_destruct
= rfcomm_sock_destruct
;
292 sk
->sk_sndtimeo
= RFCOMM_CONN_TIMEOUT
;
294 sk
->sk_sndbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
295 sk
->sk_rcvbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
297 sock_reset_flag(sk
, SOCK_ZAPPED
);
299 sk
->sk_protocol
= proto
;
300 sk
->sk_state
= BT_OPEN
;
302 bt_sock_link(&rfcomm_sk_list
, sk
);
308 static int rfcomm_sock_create(struct net
*net
, struct socket
*sock
,
309 int protocol
, int kern
)
313 BT_DBG("sock %p", sock
);
315 sock
->state
= SS_UNCONNECTED
;
317 if (sock
->type
!= SOCK_STREAM
&& sock
->type
!= SOCK_RAW
)
318 return -ESOCKTNOSUPPORT
;
320 sock
->ops
= &rfcomm_sock_ops
;
322 sk
= rfcomm_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
326 rfcomm_sock_init(sk
, NULL
);
330 static int rfcomm_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
332 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
333 struct sock
*sk
= sock
->sk
;
336 BT_DBG("sk %p %pMR", sk
, &sa
->rc_bdaddr
);
338 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
343 if (sk
->sk_state
!= BT_OPEN
) {
348 if (sk
->sk_type
!= SOCK_STREAM
) {
353 write_lock(&rfcomm_sk_list
.lock
);
355 if (sa
->rc_channel
&& __rfcomm_get_sock_by_addr(sa
->rc_channel
, &sa
->rc_bdaddr
)) {
358 /* Save source address */
359 bacpy(&rfcomm_pi(sk
)->src
, &sa
->rc_bdaddr
);
360 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
361 sk
->sk_state
= BT_BOUND
;
364 write_unlock(&rfcomm_sk_list
.lock
);
371 static int rfcomm_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
373 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
374 struct sock
*sk
= sock
->sk
;
375 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
380 if (alen
< sizeof(struct sockaddr_rc
) ||
381 addr
->sa_family
!= AF_BLUETOOTH
)
386 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
) {
391 if (sk
->sk_type
!= SOCK_STREAM
) {
396 sk
->sk_state
= BT_CONNECT
;
397 bacpy(&rfcomm_pi(sk
)->dst
, &sa
->rc_bdaddr
);
398 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
400 d
->sec_level
= rfcomm_pi(sk
)->sec_level
;
401 d
->role_switch
= rfcomm_pi(sk
)->role_switch
;
403 err
= rfcomm_dlc_open(d
, &rfcomm_pi(sk
)->src
, &sa
->rc_bdaddr
,
406 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
407 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
414 static int rfcomm_sock_listen(struct socket
*sock
, int backlog
)
416 struct sock
*sk
= sock
->sk
;
419 BT_DBG("sk %p backlog %d", sk
, backlog
);
423 if (sk
->sk_state
!= BT_BOUND
) {
428 if (sk
->sk_type
!= SOCK_STREAM
) {
433 if (!rfcomm_pi(sk
)->channel
) {
434 bdaddr_t
*src
= &rfcomm_pi(sk
)->src
;
439 write_lock(&rfcomm_sk_list
.lock
);
441 for (channel
= 1; channel
< 31; channel
++)
442 if (!__rfcomm_get_sock_by_addr(channel
, src
)) {
443 rfcomm_pi(sk
)->channel
= channel
;
448 write_unlock(&rfcomm_sk_list
.lock
);
454 sk
->sk_max_ack_backlog
= backlog
;
455 sk
->sk_ack_backlog
= 0;
456 sk
->sk_state
= BT_LISTEN
;
463 static int rfcomm_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
465 DECLARE_WAITQUEUE(wait
, current
);
466 struct sock
*sk
= sock
->sk
, *nsk
;
470 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
472 if (sk
->sk_type
!= SOCK_STREAM
) {
477 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
479 BT_DBG("sk %p timeo %ld", sk
, timeo
);
481 /* Wait for an incoming connection. (wake-one). */
482 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
484 set_current_state(TASK_INTERRUPTIBLE
);
486 if (sk
->sk_state
!= BT_LISTEN
) {
491 nsk
= bt_accept_dequeue(sk
, newsock
);
500 if (signal_pending(current
)) {
501 err
= sock_intr_errno(timeo
);
506 timeo
= schedule_timeout(timeo
);
507 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
509 __set_current_state(TASK_RUNNING
);
510 remove_wait_queue(sk_sleep(sk
), &wait
);
515 newsock
->state
= SS_CONNECTED
;
517 BT_DBG("new socket %p", nsk
);
524 static int rfcomm_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
526 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
527 struct sock
*sk
= sock
->sk
;
529 BT_DBG("sock %p, sk %p", sock
, sk
);
531 memset(sa
, 0, sizeof(*sa
));
532 sa
->rc_family
= AF_BLUETOOTH
;
533 sa
->rc_channel
= rfcomm_pi(sk
)->channel
;
535 bacpy(&sa
->rc_bdaddr
, &rfcomm_pi(sk
)->dst
);
537 bacpy(&sa
->rc_bdaddr
, &rfcomm_pi(sk
)->src
);
539 *len
= sizeof(struct sockaddr_rc
);
543 static int rfcomm_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
544 struct msghdr
*msg
, size_t len
)
546 struct sock
*sk
= sock
->sk
;
547 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
551 if (test_bit(RFCOMM_DEFER_SETUP
, &d
->flags
))
554 if (msg
->msg_flags
& MSG_OOB
)
557 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
560 BT_DBG("sock %p, sk %p", sock
, sk
);
564 sent
= bt_sock_wait_ready(sk
, msg
->msg_flags
);
569 size_t size
= min_t(size_t, len
, d
->mtu
);
572 skb
= sock_alloc_send_skb(sk
, size
+ RFCOMM_SKB_RESERVE
,
573 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
579 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
581 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
589 skb
->priority
= sk
->sk_priority
;
591 err
= rfcomm_dlc_send(d
, skb
);
609 static int rfcomm_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
610 struct msghdr
*msg
, size_t size
, int flags
)
612 struct sock
*sk
= sock
->sk
;
613 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
616 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
617 rfcomm_dlc_accept(d
);
621 len
= bt_sock_stream_recvmsg(iocb
, sock
, msg
, size
, flags
);
624 if (!(flags
& MSG_PEEK
) && len
> 0)
625 atomic_sub(len
, &sk
->sk_rmem_alloc
);
627 if (atomic_read(&sk
->sk_rmem_alloc
) <= (sk
->sk_rcvbuf
>> 2))
628 rfcomm_dlc_unthrottle(rfcomm_pi(sk
)->dlc
);
634 static int rfcomm_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
636 struct sock
*sk
= sock
->sk
;
646 if (get_user(opt
, (u32 __user
*) optval
)) {
651 if (opt
& RFCOMM_LM_AUTH
)
652 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_LOW
;
653 if (opt
& RFCOMM_LM_ENCRYPT
)
654 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_MEDIUM
;
655 if (opt
& RFCOMM_LM_SECURE
)
656 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_HIGH
;
658 rfcomm_pi(sk
)->role_switch
= (opt
& RFCOMM_LM_MASTER
);
670 static int rfcomm_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
672 struct sock
*sk
= sock
->sk
;
673 struct bt_security sec
;
680 if (level
== SOL_RFCOMM
)
681 return rfcomm_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
683 if (level
!= SOL_BLUETOOTH
)
690 if (sk
->sk_type
!= SOCK_STREAM
) {
695 sec
.level
= BT_SECURITY_LOW
;
697 len
= min_t(unsigned int, sizeof(sec
), optlen
);
698 if (copy_from_user((char *) &sec
, optval
, len
)) {
703 if (sec
.level
> BT_SECURITY_HIGH
) {
708 rfcomm_pi(sk
)->sec_level
= sec
.level
;
712 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
717 if (get_user(opt
, (u32 __user
*) optval
)) {
723 set_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
725 clear_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
738 static int rfcomm_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
740 struct sock
*sk
= sock
->sk
;
741 struct sock
*l2cap_sk
;
742 struct l2cap_conn
*conn
;
743 struct rfcomm_conninfo cinfo
;
749 if (get_user(len
, optlen
))
756 switch (rfcomm_pi(sk
)->sec_level
) {
757 case BT_SECURITY_LOW
:
758 opt
= RFCOMM_LM_AUTH
;
760 case BT_SECURITY_MEDIUM
:
761 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
;
763 case BT_SECURITY_HIGH
:
764 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
|
772 if (rfcomm_pi(sk
)->role_switch
)
773 opt
|= RFCOMM_LM_MASTER
;
775 if (put_user(opt
, (u32 __user
*) optval
))
779 case RFCOMM_CONNINFO
:
780 if (sk
->sk_state
!= BT_CONNECTED
&&
781 !rfcomm_pi(sk
)->dlc
->defer_setup
) {
786 l2cap_sk
= rfcomm_pi(sk
)->dlc
->session
->sock
->sk
;
787 conn
= l2cap_pi(l2cap_sk
)->chan
->conn
;
789 memset(&cinfo
, 0, sizeof(cinfo
));
790 cinfo
.hci_handle
= conn
->hcon
->handle
;
791 memcpy(cinfo
.dev_class
, conn
->hcon
->dev_class
, 3);
793 len
= min_t(unsigned int, len
, sizeof(cinfo
));
794 if (copy_to_user(optval
, (char *) &cinfo
, len
))
808 static int rfcomm_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
810 struct sock
*sk
= sock
->sk
;
811 struct bt_security sec
;
816 if (level
== SOL_RFCOMM
)
817 return rfcomm_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
819 if (level
!= SOL_BLUETOOTH
)
822 if (get_user(len
, optlen
))
829 if (sk
->sk_type
!= SOCK_STREAM
) {
834 sec
.level
= rfcomm_pi(sk
)->sec_level
;
837 len
= min_t(unsigned int, len
, sizeof(sec
));
838 if (copy_to_user(optval
, (char *) &sec
, len
))
844 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
849 if (put_user(test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
),
850 (u32 __user
*) optval
))
864 static int rfcomm_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
866 struct sock
*sk __maybe_unused
= sock
->sk
;
869 BT_DBG("sk %p cmd %x arg %lx", sk
, cmd
, arg
);
871 err
= bt_sock_ioctl(sock
, cmd
, arg
);
873 if (err
== -ENOIOCTLCMD
) {
874 #ifdef CONFIG_BT_RFCOMM_TTY
876 err
= rfcomm_dev_ioctl(sk
, cmd
, (void __user
*) arg
);
886 static int rfcomm_sock_shutdown(struct socket
*sock
, int how
)
888 struct sock
*sk
= sock
->sk
;
891 BT_DBG("sock %p, sk %p", sock
, sk
);
897 if (!sk
->sk_shutdown
) {
898 sk
->sk_shutdown
= SHUTDOWN_MASK
;
899 __rfcomm_sock_close(sk
);
901 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
902 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
908 static int rfcomm_sock_release(struct socket
*sock
)
910 struct sock
*sk
= sock
->sk
;
913 BT_DBG("sock %p, sk %p", sock
, sk
);
918 err
= rfcomm_sock_shutdown(sock
, 2);
921 rfcomm_sock_kill(sk
);
925 /* ---- RFCOMM core layer callbacks ----
927 * called under rfcomm_lock()
929 int rfcomm_connect_ind(struct rfcomm_session
*s
, u8 channel
, struct rfcomm_dlc
**d
)
931 struct sock
*sk
, *parent
;
935 BT_DBG("session %p channel %d", s
, channel
);
937 rfcomm_session_getaddr(s
, &src
, &dst
);
939 /* Check if we have socket listening on channel */
940 parent
= rfcomm_get_sock_by_channel(BT_LISTEN
, channel
, &src
);
944 bh_lock_sock(parent
);
946 /* Check for backlog size */
947 if (sk_acceptq_is_full(parent
)) {
948 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
952 sk
= rfcomm_sock_alloc(sock_net(parent
), NULL
, BTPROTO_RFCOMM
, GFP_ATOMIC
);
956 bt_sock_reclassify_lock(sk
, BTPROTO_RFCOMM
);
958 rfcomm_sock_init(sk
, parent
);
959 bacpy(&rfcomm_pi(sk
)->src
, &src
);
960 bacpy(&rfcomm_pi(sk
)->dst
, &dst
);
961 rfcomm_pi(sk
)->channel
= channel
;
963 sk
->sk_state
= BT_CONFIG
;
964 bt_accept_enqueue(parent
, sk
);
966 /* Accept connection and return socket DLC */
967 *d
= rfcomm_pi(sk
)->dlc
;
971 bh_unlock_sock(parent
);
973 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(parent
)->flags
))
974 parent
->sk_state_change(parent
);
979 static int rfcomm_sock_debugfs_show(struct seq_file
*f
, void *p
)
983 read_lock(&rfcomm_sk_list
.lock
);
985 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
986 seq_printf(f
, "%pMR %pMR %d %d\n",
987 &rfcomm_pi(sk
)->src
, &rfcomm_pi(sk
)->dst
,
988 sk
->sk_state
, rfcomm_pi(sk
)->channel
);
991 read_unlock(&rfcomm_sk_list
.lock
);
996 static int rfcomm_sock_debugfs_open(struct inode
*inode
, struct file
*file
)
998 return single_open(file
, rfcomm_sock_debugfs_show
, inode
->i_private
);
1001 static const struct file_operations rfcomm_sock_debugfs_fops
= {
1002 .open
= rfcomm_sock_debugfs_open
,
1004 .llseek
= seq_lseek
,
1005 .release
= single_release
,
1008 static struct dentry
*rfcomm_sock_debugfs
;
1010 static const struct proto_ops rfcomm_sock_ops
= {
1011 .family
= PF_BLUETOOTH
,
1012 .owner
= THIS_MODULE
,
1013 .release
= rfcomm_sock_release
,
1014 .bind
= rfcomm_sock_bind
,
1015 .connect
= rfcomm_sock_connect
,
1016 .listen
= rfcomm_sock_listen
,
1017 .accept
= rfcomm_sock_accept
,
1018 .getname
= rfcomm_sock_getname
,
1019 .sendmsg
= rfcomm_sock_sendmsg
,
1020 .recvmsg
= rfcomm_sock_recvmsg
,
1021 .shutdown
= rfcomm_sock_shutdown
,
1022 .setsockopt
= rfcomm_sock_setsockopt
,
1023 .getsockopt
= rfcomm_sock_getsockopt
,
1024 .ioctl
= rfcomm_sock_ioctl
,
1025 .poll
= bt_sock_poll
,
1026 .socketpair
= sock_no_socketpair
,
1027 .mmap
= sock_no_mmap
1030 static const struct net_proto_family rfcomm_sock_family_ops
= {
1031 .family
= PF_BLUETOOTH
,
1032 .owner
= THIS_MODULE
,
1033 .create
= rfcomm_sock_create
1036 int __init
rfcomm_init_sockets(void)
1040 err
= proto_register(&rfcomm_proto
, 0);
1044 err
= bt_sock_register(BTPROTO_RFCOMM
, &rfcomm_sock_family_ops
);
1046 BT_ERR("RFCOMM socket layer registration failed");
1050 err
= bt_procfs_init(&init_net
, "rfcomm", &rfcomm_sk_list
, NULL
);
1052 BT_ERR("Failed to create RFCOMM proc file");
1053 bt_sock_unregister(BTPROTO_RFCOMM
);
1057 BT_INFO("RFCOMM socket layer initialized");
1059 if (IS_ERR_OR_NULL(bt_debugfs
))
1062 rfcomm_sock_debugfs
= debugfs_create_file("rfcomm", 0444,
1064 &rfcomm_sock_debugfs_fops
);
1069 proto_unregister(&rfcomm_proto
);
1073 void __exit
rfcomm_cleanup_sockets(void)
1075 bt_procfs_cleanup(&init_net
, "rfcomm");
1077 debugfs_remove(rfcomm_sock_debugfs
);
1079 bt_sock_unregister(BTPROTO_RFCOMM
);
1081 proto_unregister(&rfcomm_proto
);