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
, &bt_sk(sk
)->src
, NULL
);
91 sk
->sk_state_change(sk
);
95 local_irq_restore(flags
);
97 if (parent
&& sock_flag(sk
, SOCK_ZAPPED
)) {
98 /* We have to drop DLC lock here, otherwise
99 * rfcomm_sock_destruct() will dead lock. */
100 rfcomm_dlc_unlock(d
);
101 rfcomm_sock_kill(sk
);
106 /* ---- Socket functions ---- */
107 static struct sock
*__rfcomm_get_sock_by_addr(u8 channel
, bdaddr_t
*src
)
109 struct sock
*sk
= NULL
;
111 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
112 if (rfcomm_pi(sk
)->channel
== channel
&&
113 !bacmp(&bt_sk(sk
)->src
, src
))
117 return sk
? sk
: NULL
;
120 /* Find socket with channel and source bdaddr.
121 * Returns closest match.
123 static struct sock
*rfcomm_get_sock_by_channel(int state
, u8 channel
, bdaddr_t
*src
)
125 struct sock
*sk
= NULL
, *sk1
= NULL
;
127 read_lock(&rfcomm_sk_list
.lock
);
129 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
130 if (state
&& sk
->sk_state
!= state
)
133 if (rfcomm_pi(sk
)->channel
== channel
) {
135 if (!bacmp(&bt_sk(sk
)->src
, src
))
139 if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
))
144 read_unlock(&rfcomm_sk_list
.lock
);
146 return sk
? sk
: sk1
;
149 static void rfcomm_sock_destruct(struct sock
*sk
)
151 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
153 BT_DBG("sk %p dlc %p", sk
, d
);
155 skb_queue_purge(&sk
->sk_receive_queue
);
156 skb_queue_purge(&sk
->sk_write_queue
);
159 rfcomm_pi(sk
)->dlc
= NULL
;
161 /* Detach DLC if it's owned by this socket */
164 rfcomm_dlc_unlock(d
);
169 static void rfcomm_sock_cleanup_listen(struct sock
*parent
)
173 BT_DBG("parent %p", parent
);
175 /* Close not yet accepted dlcs */
176 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
177 rfcomm_sock_close(sk
);
178 rfcomm_sock_kill(sk
);
181 parent
->sk_state
= BT_CLOSED
;
182 sock_set_flag(parent
, SOCK_ZAPPED
);
185 /* Kill socket (only if zapped and orphan)
186 * Must be called on unlocked socket.
188 static void rfcomm_sock_kill(struct sock
*sk
)
190 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
193 BT_DBG("sk %p state %d refcnt %d", sk
, sk
->sk_state
, atomic_read(&sk
->sk_refcnt
));
195 /* Kill poor orphan */
196 bt_sock_unlink(&rfcomm_sk_list
, sk
);
197 sock_set_flag(sk
, SOCK_DEAD
);
201 static void __rfcomm_sock_close(struct sock
*sk
)
203 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
205 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
207 switch (sk
->sk_state
) {
209 rfcomm_sock_cleanup_listen(sk
);
216 rfcomm_dlc_close(d
, 0);
219 sock_set_flag(sk
, SOCK_ZAPPED
);
225 * Must be called on unlocked socket.
227 static void rfcomm_sock_close(struct sock
*sk
)
230 __rfcomm_sock_close(sk
);
234 static void rfcomm_sock_init(struct sock
*sk
, struct sock
*parent
)
236 struct rfcomm_pinfo
*pi
= rfcomm_pi(sk
);
241 sk
->sk_type
= parent
->sk_type
;
242 pi
->dlc
->defer_setup
= test_bit(BT_SK_DEFER_SETUP
,
243 &bt_sk(parent
)->flags
);
245 pi
->sec_level
= rfcomm_pi(parent
)->sec_level
;
246 pi
->role_switch
= rfcomm_pi(parent
)->role_switch
;
248 security_sk_clone(parent
, sk
);
250 pi
->dlc
->defer_setup
= 0;
252 pi
->sec_level
= BT_SECURITY_LOW
;
256 pi
->dlc
->sec_level
= pi
->sec_level
;
257 pi
->dlc
->role_switch
= pi
->role_switch
;
260 static struct proto rfcomm_proto
= {
262 .owner
= THIS_MODULE
,
263 .obj_size
= sizeof(struct rfcomm_pinfo
)
266 static struct sock
*rfcomm_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
268 struct rfcomm_dlc
*d
;
271 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &rfcomm_proto
);
275 sock_init_data(sock
, sk
);
276 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
278 d
= rfcomm_dlc_alloc(prio
);
284 d
->data_ready
= rfcomm_sk_data_ready
;
285 d
->state_change
= rfcomm_sk_state_change
;
287 rfcomm_pi(sk
)->dlc
= d
;
290 sk
->sk_destruct
= rfcomm_sock_destruct
;
291 sk
->sk_sndtimeo
= RFCOMM_CONN_TIMEOUT
;
293 sk
->sk_sndbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
294 sk
->sk_rcvbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
296 sock_reset_flag(sk
, SOCK_ZAPPED
);
298 sk
->sk_protocol
= proto
;
299 sk
->sk_state
= BT_OPEN
;
301 bt_sock_link(&rfcomm_sk_list
, sk
);
307 static int rfcomm_sock_create(struct net
*net
, struct socket
*sock
,
308 int protocol
, int kern
)
312 BT_DBG("sock %p", sock
);
314 sock
->state
= SS_UNCONNECTED
;
316 if (sock
->type
!= SOCK_STREAM
&& sock
->type
!= SOCK_RAW
)
317 return -ESOCKTNOSUPPORT
;
319 sock
->ops
= &rfcomm_sock_ops
;
321 sk
= rfcomm_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
325 rfcomm_sock_init(sk
, NULL
);
329 static int rfcomm_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
331 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
332 struct sock
*sk
= sock
->sk
;
335 BT_DBG("sk %p %pMR", sk
, &sa
->rc_bdaddr
);
337 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
342 if (sk
->sk_state
!= BT_OPEN
) {
347 if (sk
->sk_type
!= SOCK_STREAM
) {
352 write_lock(&rfcomm_sk_list
.lock
);
354 if (sa
->rc_channel
&& __rfcomm_get_sock_by_addr(sa
->rc_channel
, &sa
->rc_bdaddr
)) {
357 /* Save source address */
358 bacpy(&bt_sk(sk
)->src
, &sa
->rc_bdaddr
);
359 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
360 sk
->sk_state
= BT_BOUND
;
363 write_unlock(&rfcomm_sk_list
.lock
);
370 static int rfcomm_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
372 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
373 struct sock
*sk
= sock
->sk
;
374 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
379 if (alen
< sizeof(struct sockaddr_rc
) ||
380 addr
->sa_family
!= AF_BLUETOOTH
)
385 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
) {
390 if (sk
->sk_type
!= SOCK_STREAM
) {
395 sk
->sk_state
= BT_CONNECT
;
396 bacpy(&bt_sk(sk
)->dst
, &sa
->rc_bdaddr
);
397 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
399 d
->sec_level
= rfcomm_pi(sk
)->sec_level
;
400 d
->role_switch
= rfcomm_pi(sk
)->role_switch
;
402 err
= rfcomm_dlc_open(d
, &bt_sk(sk
)->src
, &sa
->rc_bdaddr
, sa
->rc_channel
);
404 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
405 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
412 static int rfcomm_sock_listen(struct socket
*sock
, int backlog
)
414 struct sock
*sk
= sock
->sk
;
417 BT_DBG("sk %p backlog %d", sk
, backlog
);
421 if (sk
->sk_state
!= BT_BOUND
) {
426 if (sk
->sk_type
!= SOCK_STREAM
) {
431 if (!rfcomm_pi(sk
)->channel
) {
432 bdaddr_t
*src
= &bt_sk(sk
)->src
;
437 write_lock(&rfcomm_sk_list
.lock
);
439 for (channel
= 1; channel
< 31; channel
++)
440 if (!__rfcomm_get_sock_by_addr(channel
, src
)) {
441 rfcomm_pi(sk
)->channel
= channel
;
446 write_unlock(&rfcomm_sk_list
.lock
);
452 sk
->sk_max_ack_backlog
= backlog
;
453 sk
->sk_ack_backlog
= 0;
454 sk
->sk_state
= BT_LISTEN
;
461 static int rfcomm_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
463 DECLARE_WAITQUEUE(wait
, current
);
464 struct sock
*sk
= sock
->sk
, *nsk
;
468 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
470 if (sk
->sk_type
!= SOCK_STREAM
) {
475 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
477 BT_DBG("sk %p timeo %ld", sk
, timeo
);
479 /* Wait for an incoming connection. (wake-one). */
480 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
482 set_current_state(TASK_INTERRUPTIBLE
);
484 if (sk
->sk_state
!= BT_LISTEN
) {
489 nsk
= bt_accept_dequeue(sk
, newsock
);
498 if (signal_pending(current
)) {
499 err
= sock_intr_errno(timeo
);
504 timeo
= schedule_timeout(timeo
);
505 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
507 __set_current_state(TASK_RUNNING
);
508 remove_wait_queue(sk_sleep(sk
), &wait
);
513 newsock
->state
= SS_CONNECTED
;
515 BT_DBG("new socket %p", nsk
);
522 static int rfcomm_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
524 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
525 struct sock
*sk
= sock
->sk
;
527 BT_DBG("sock %p, sk %p", sock
, sk
);
529 memset(sa
, 0, sizeof(*sa
));
530 sa
->rc_family
= AF_BLUETOOTH
;
531 sa
->rc_channel
= rfcomm_pi(sk
)->channel
;
533 bacpy(&sa
->rc_bdaddr
, &bt_sk(sk
)->dst
);
535 bacpy(&sa
->rc_bdaddr
, &bt_sk(sk
)->src
);
537 *len
= sizeof(struct sockaddr_rc
);
541 static int rfcomm_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
542 struct msghdr
*msg
, size_t len
)
544 struct sock
*sk
= sock
->sk
;
545 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
549 if (test_bit(RFCOMM_DEFER_SETUP
, &d
->flags
))
552 if (msg
->msg_flags
& MSG_OOB
)
555 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
558 BT_DBG("sock %p, sk %p", sock
, sk
);
563 size_t size
= min_t(size_t, len
, d
->mtu
);
566 skb
= sock_alloc_send_skb(sk
, size
+ RFCOMM_SKB_RESERVE
,
567 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
573 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
575 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
583 skb
->priority
= sk
->sk_priority
;
585 err
= rfcomm_dlc_send(d
, skb
);
602 static int rfcomm_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
603 struct msghdr
*msg
, size_t size
, int flags
)
605 struct sock
*sk
= sock
->sk
;
606 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
609 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
610 rfcomm_dlc_accept(d
);
614 len
= bt_sock_stream_recvmsg(iocb
, sock
, msg
, size
, flags
);
617 if (!(flags
& MSG_PEEK
) && len
> 0)
618 atomic_sub(len
, &sk
->sk_rmem_alloc
);
620 if (atomic_read(&sk
->sk_rmem_alloc
) <= (sk
->sk_rcvbuf
>> 2))
621 rfcomm_dlc_unthrottle(rfcomm_pi(sk
)->dlc
);
627 static int rfcomm_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
629 struct sock
*sk
= sock
->sk
;
639 if (get_user(opt
, (u32 __user
*) optval
)) {
644 if (opt
& RFCOMM_LM_AUTH
)
645 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_LOW
;
646 if (opt
& RFCOMM_LM_ENCRYPT
)
647 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_MEDIUM
;
648 if (opt
& RFCOMM_LM_SECURE
)
649 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_HIGH
;
651 rfcomm_pi(sk
)->role_switch
= (opt
& RFCOMM_LM_MASTER
);
663 static int rfcomm_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
665 struct sock
*sk
= sock
->sk
;
666 struct bt_security sec
;
673 if (level
== SOL_RFCOMM
)
674 return rfcomm_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
676 if (level
!= SOL_BLUETOOTH
)
683 if (sk
->sk_type
!= SOCK_STREAM
) {
688 sec
.level
= BT_SECURITY_LOW
;
690 len
= min_t(unsigned int, sizeof(sec
), optlen
);
691 if (copy_from_user((char *) &sec
, optval
, len
)) {
696 if (sec
.level
> BT_SECURITY_HIGH
) {
701 rfcomm_pi(sk
)->sec_level
= sec
.level
;
705 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
710 if (get_user(opt
, (u32 __user
*) optval
)) {
716 set_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
718 clear_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
731 static int rfcomm_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
733 struct sock
*sk
= sock
->sk
;
734 struct rfcomm_conninfo cinfo
;
735 struct l2cap_conn
*conn
= l2cap_pi(sk
)->chan
->conn
;
741 if (get_user(len
, optlen
))
748 switch (rfcomm_pi(sk
)->sec_level
) {
749 case BT_SECURITY_LOW
:
750 opt
= RFCOMM_LM_AUTH
;
752 case BT_SECURITY_MEDIUM
:
753 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
;
755 case BT_SECURITY_HIGH
:
756 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
|
764 if (rfcomm_pi(sk
)->role_switch
)
765 opt
|= RFCOMM_LM_MASTER
;
767 if (put_user(opt
, (u32 __user
*) optval
))
771 case RFCOMM_CONNINFO
:
772 if (sk
->sk_state
!= BT_CONNECTED
&&
773 !rfcomm_pi(sk
)->dlc
->defer_setup
) {
778 memset(&cinfo
, 0, sizeof(cinfo
));
779 cinfo
.hci_handle
= conn
->hcon
->handle
;
780 memcpy(cinfo
.dev_class
, conn
->hcon
->dev_class
, 3);
782 len
= min_t(unsigned int, len
, sizeof(cinfo
));
783 if (copy_to_user(optval
, (char *) &cinfo
, len
))
797 static int rfcomm_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
799 struct sock
*sk
= sock
->sk
;
800 struct bt_security sec
;
805 if (level
== SOL_RFCOMM
)
806 return rfcomm_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
808 if (level
!= SOL_BLUETOOTH
)
811 if (get_user(len
, optlen
))
818 if (sk
->sk_type
!= SOCK_STREAM
) {
823 sec
.level
= rfcomm_pi(sk
)->sec_level
;
826 len
= min_t(unsigned int, len
, sizeof(sec
));
827 if (copy_to_user(optval
, (char *) &sec
, len
))
833 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
838 if (put_user(test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
),
839 (u32 __user
*) optval
))
853 static int rfcomm_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
855 struct sock
*sk __maybe_unused
= sock
->sk
;
858 BT_DBG("sk %p cmd %x arg %lx", sk
, cmd
, arg
);
860 err
= bt_sock_ioctl(sock
, cmd
, arg
);
862 if (err
== -ENOIOCTLCMD
) {
863 #ifdef CONFIG_BT_RFCOMM_TTY
865 err
= rfcomm_dev_ioctl(sk
, cmd
, (void __user
*) arg
);
875 static int rfcomm_sock_shutdown(struct socket
*sock
, int how
)
877 struct sock
*sk
= sock
->sk
;
880 BT_DBG("sock %p, sk %p", sock
, sk
);
886 if (!sk
->sk_shutdown
) {
887 sk
->sk_shutdown
= SHUTDOWN_MASK
;
888 __rfcomm_sock_close(sk
);
890 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
891 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
897 static int rfcomm_sock_release(struct socket
*sock
)
899 struct sock
*sk
= sock
->sk
;
902 BT_DBG("sock %p, sk %p", sock
, sk
);
907 err
= rfcomm_sock_shutdown(sock
, 2);
910 rfcomm_sock_kill(sk
);
914 /* ---- RFCOMM core layer callbacks ----
916 * called under rfcomm_lock()
918 int rfcomm_connect_ind(struct rfcomm_session
*s
, u8 channel
, struct rfcomm_dlc
**d
)
920 struct sock
*sk
, *parent
;
924 BT_DBG("session %p channel %d", s
, channel
);
926 rfcomm_session_getaddr(s
, &src
, &dst
);
928 /* Check if we have socket listening on channel */
929 parent
= rfcomm_get_sock_by_channel(BT_LISTEN
, channel
, &src
);
933 bh_lock_sock(parent
);
935 /* Check for backlog size */
936 if (sk_acceptq_is_full(parent
)) {
937 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
941 sk
= rfcomm_sock_alloc(sock_net(parent
), NULL
, BTPROTO_RFCOMM
, GFP_ATOMIC
);
945 bt_sock_reclassify_lock(sk
, BTPROTO_RFCOMM
);
947 rfcomm_sock_init(sk
, parent
);
948 bacpy(&bt_sk(sk
)->src
, &src
);
949 bacpy(&bt_sk(sk
)->dst
, &dst
);
950 rfcomm_pi(sk
)->channel
= channel
;
952 sk
->sk_state
= BT_CONFIG
;
953 bt_accept_enqueue(parent
, sk
);
955 /* Accept connection and return socket DLC */
956 *d
= rfcomm_pi(sk
)->dlc
;
960 bh_unlock_sock(parent
);
962 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(parent
)->flags
))
963 parent
->sk_state_change(parent
);
968 static int rfcomm_sock_debugfs_show(struct seq_file
*f
, void *p
)
972 read_lock(&rfcomm_sk_list
.lock
);
974 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
975 seq_printf(f
, "%pMR %pMR %d %d\n",
976 &bt_sk(sk
)->src
, &bt_sk(sk
)->dst
,
977 sk
->sk_state
, rfcomm_pi(sk
)->channel
);
980 read_unlock(&rfcomm_sk_list
.lock
);
985 static int rfcomm_sock_debugfs_open(struct inode
*inode
, struct file
*file
)
987 return single_open(file
, rfcomm_sock_debugfs_show
, inode
->i_private
);
990 static const struct file_operations rfcomm_sock_debugfs_fops
= {
991 .open
= rfcomm_sock_debugfs_open
,
994 .release
= single_release
,
997 static struct dentry
*rfcomm_sock_debugfs
;
999 static const struct proto_ops rfcomm_sock_ops
= {
1000 .family
= PF_BLUETOOTH
,
1001 .owner
= THIS_MODULE
,
1002 .release
= rfcomm_sock_release
,
1003 .bind
= rfcomm_sock_bind
,
1004 .connect
= rfcomm_sock_connect
,
1005 .listen
= rfcomm_sock_listen
,
1006 .accept
= rfcomm_sock_accept
,
1007 .getname
= rfcomm_sock_getname
,
1008 .sendmsg
= rfcomm_sock_sendmsg
,
1009 .recvmsg
= rfcomm_sock_recvmsg
,
1010 .shutdown
= rfcomm_sock_shutdown
,
1011 .setsockopt
= rfcomm_sock_setsockopt
,
1012 .getsockopt
= rfcomm_sock_getsockopt
,
1013 .ioctl
= rfcomm_sock_ioctl
,
1014 .poll
= bt_sock_poll
,
1015 .socketpair
= sock_no_socketpair
,
1016 .mmap
= sock_no_mmap
1019 static const struct net_proto_family rfcomm_sock_family_ops
= {
1020 .family
= PF_BLUETOOTH
,
1021 .owner
= THIS_MODULE
,
1022 .create
= rfcomm_sock_create
1025 int __init
rfcomm_init_sockets(void)
1029 err
= proto_register(&rfcomm_proto
, 0);
1033 err
= bt_sock_register(BTPROTO_RFCOMM
, &rfcomm_sock_family_ops
);
1035 BT_ERR("RFCOMM socket layer registration failed");
1039 err
= bt_procfs_init(&init_net
, "rfcomm", &rfcomm_sk_list
, NULL
);
1041 BT_ERR("Failed to create RFCOMM proc file");
1042 bt_sock_unregister(BTPROTO_RFCOMM
);
1047 rfcomm_sock_debugfs
= debugfs_create_file("rfcomm", 0444,
1048 bt_debugfs
, NULL
, &rfcomm_sock_debugfs_fops
);
1049 if (!rfcomm_sock_debugfs
)
1050 BT_ERR("Failed to create RFCOMM debug file");
1053 BT_INFO("RFCOMM socket layer initialized");
1058 proto_unregister(&rfcomm_proto
);
1062 void __exit
rfcomm_cleanup_sockets(void)
1064 bt_procfs_cleanup(&init_net
, "rfcomm");
1066 debugfs_remove(rfcomm_sock_debugfs
);
1068 bt_sock_unregister(BTPROTO_RFCOMM
);
1070 proto_unregister(&rfcomm_proto
);