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>
47 #include <asm/system.h>
48 #include <asm/uaccess.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52 #include <net/bluetooth/l2cap.h>
53 #include <net/bluetooth/rfcomm.h>
55 static const struct proto_ops rfcomm_sock_ops
;
57 static struct bt_sock_list rfcomm_sk_list
= {
58 .lock
= __RW_LOCK_UNLOCKED(rfcomm_sk_list
.lock
)
61 static void rfcomm_sock_close(struct sock
*sk
);
62 static void rfcomm_sock_kill(struct sock
*sk
);
64 /* ---- DLC callbacks ----
66 * called under rfcomm_dlc_lock()
68 static void rfcomm_sk_data_ready(struct rfcomm_dlc
*d
, struct sk_buff
*skb
)
70 struct sock
*sk
= d
->owner
;
74 atomic_add(skb
->len
, &sk
->sk_rmem_alloc
);
75 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
76 sk
->sk_data_ready(sk
, skb
->len
);
78 if (atomic_read(&sk
->sk_rmem_alloc
) >= sk
->sk_rcvbuf
)
79 rfcomm_dlc_throttle(d
);
82 static void rfcomm_sk_state_change(struct rfcomm_dlc
*d
, int err
)
84 struct sock
*sk
= d
->owner
, *parent
;
90 BT_DBG("dlc %p state %ld err %d", d
, d
->state
, err
);
92 local_irq_save(flags
);
98 sk
->sk_state
= d
->state
;
100 parent
= bt_sk(sk
)->parent
;
102 if (d
->state
== BT_CLOSED
) {
103 sock_set_flag(sk
, SOCK_ZAPPED
);
104 bt_accept_unlink(sk
);
106 parent
->sk_data_ready(parent
, 0);
108 if (d
->state
== BT_CONNECTED
)
109 rfcomm_session_getaddr(d
->session
, &bt_sk(sk
)->src
, NULL
);
110 sk
->sk_state_change(sk
);
114 local_irq_restore(flags
);
116 if (parent
&& sock_flag(sk
, SOCK_ZAPPED
)) {
117 /* We have to drop DLC lock here, otherwise
118 * rfcomm_sock_destruct() will dead lock. */
119 rfcomm_dlc_unlock(d
);
120 rfcomm_sock_kill(sk
);
125 /* ---- Socket functions ---- */
126 static struct sock
*__rfcomm_get_sock_by_addr(u8 channel
, bdaddr_t
*src
)
128 struct sock
*sk
= NULL
;
129 struct hlist_node
*node
;
131 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
132 if (rfcomm_pi(sk
)->channel
== channel
&&
133 !bacmp(&bt_sk(sk
)->src
, src
))
137 return node
? sk
: NULL
;
140 /* Find socket with channel and source bdaddr.
141 * Returns closest match.
143 static struct sock
*__rfcomm_get_sock_by_channel(int state
, u8 channel
, bdaddr_t
*src
)
145 struct sock
*sk
= NULL
, *sk1
= NULL
;
146 struct hlist_node
*node
;
148 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
149 if (state
&& sk
->sk_state
!= state
)
152 if (rfcomm_pi(sk
)->channel
== channel
) {
154 if (!bacmp(&bt_sk(sk
)->src
, src
))
158 if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
))
162 return node
? sk
: sk1
;
165 /* Find socket with given address (channel, src).
166 * Returns locked socket */
167 static inline struct sock
*rfcomm_get_sock_by_channel(int state
, u8 channel
, bdaddr_t
*src
)
170 read_lock(&rfcomm_sk_list
.lock
);
171 s
= __rfcomm_get_sock_by_channel(state
, channel
, src
);
172 if (s
) bh_lock_sock(s
);
173 read_unlock(&rfcomm_sk_list
.lock
);
177 static void rfcomm_sock_destruct(struct sock
*sk
)
179 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
181 BT_DBG("sk %p dlc %p", sk
, d
);
183 skb_queue_purge(&sk
->sk_receive_queue
);
184 skb_queue_purge(&sk
->sk_write_queue
);
187 rfcomm_pi(sk
)->dlc
= NULL
;
189 /* Detach DLC if it's owned by this socket */
192 rfcomm_dlc_unlock(d
);
197 static void rfcomm_sock_cleanup_listen(struct sock
*parent
)
201 BT_DBG("parent %p", parent
);
203 /* Close not yet accepted dlcs */
204 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
205 rfcomm_sock_close(sk
);
206 rfcomm_sock_kill(sk
);
209 parent
->sk_state
= BT_CLOSED
;
210 sock_set_flag(parent
, SOCK_ZAPPED
);
213 /* Kill socket (only if zapped and orphan)
214 * Must be called on unlocked socket.
216 static void rfcomm_sock_kill(struct sock
*sk
)
218 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
221 BT_DBG("sk %p state %d refcnt %d", sk
, sk
->sk_state
, atomic_read(&sk
->sk_refcnt
));
223 /* Kill poor orphan */
224 bt_sock_unlink(&rfcomm_sk_list
, sk
);
225 sock_set_flag(sk
, SOCK_DEAD
);
229 static void __rfcomm_sock_close(struct sock
*sk
)
231 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
233 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
235 switch (sk
->sk_state
) {
237 rfcomm_sock_cleanup_listen(sk
);
244 rfcomm_dlc_close(d
, 0);
247 sock_set_flag(sk
, SOCK_ZAPPED
);
253 * Must be called on unlocked socket.
255 static void rfcomm_sock_close(struct sock
*sk
)
258 __rfcomm_sock_close(sk
);
262 static void rfcomm_sock_init(struct sock
*sk
, struct sock
*parent
)
264 struct rfcomm_pinfo
*pi
= rfcomm_pi(sk
);
269 sk
->sk_type
= parent
->sk_type
;
270 pi
->dlc
->defer_setup
= bt_sk(parent
)->defer_setup
;
272 pi
->sec_level
= rfcomm_pi(parent
)->sec_level
;
273 pi
->role_switch
= rfcomm_pi(parent
)->role_switch
;
275 pi
->dlc
->defer_setup
= 0;
277 pi
->sec_level
= BT_SECURITY_LOW
;
281 pi
->dlc
->sec_level
= pi
->sec_level
;
282 pi
->dlc
->role_switch
= pi
->role_switch
;
285 static struct proto rfcomm_proto
= {
287 .owner
= THIS_MODULE
,
288 .obj_size
= sizeof(struct rfcomm_pinfo
)
291 static struct sock
*rfcomm_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
293 struct rfcomm_dlc
*d
;
296 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &rfcomm_proto
);
300 sock_init_data(sock
, sk
);
301 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
303 d
= rfcomm_dlc_alloc(prio
);
309 d
->data_ready
= rfcomm_sk_data_ready
;
310 d
->state_change
= rfcomm_sk_state_change
;
312 rfcomm_pi(sk
)->dlc
= d
;
315 sk
->sk_destruct
= rfcomm_sock_destruct
;
316 sk
->sk_sndtimeo
= RFCOMM_CONN_TIMEOUT
;
318 sk
->sk_sndbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
319 sk
->sk_rcvbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
321 sock_reset_flag(sk
, SOCK_ZAPPED
);
323 sk
->sk_protocol
= proto
;
324 sk
->sk_state
= BT_OPEN
;
326 bt_sock_link(&rfcomm_sk_list
, sk
);
332 static int rfcomm_sock_create(struct net
*net
, struct socket
*sock
,
333 int protocol
, int kern
)
337 BT_DBG("sock %p", sock
);
339 sock
->state
= SS_UNCONNECTED
;
341 if (sock
->type
!= SOCK_STREAM
&& sock
->type
!= SOCK_RAW
)
342 return -ESOCKTNOSUPPORT
;
344 sock
->ops
= &rfcomm_sock_ops
;
346 sk
= rfcomm_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
350 rfcomm_sock_init(sk
, NULL
);
354 static int rfcomm_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
356 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
357 struct sock
*sk
= sock
->sk
;
360 BT_DBG("sk %p %s", sk
, batostr(&sa
->rc_bdaddr
));
362 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
367 if (sk
->sk_state
!= BT_OPEN
) {
372 if (sk
->sk_type
!= SOCK_STREAM
) {
377 write_lock_bh(&rfcomm_sk_list
.lock
);
379 if (sa
->rc_channel
&& __rfcomm_get_sock_by_addr(sa
->rc_channel
, &sa
->rc_bdaddr
)) {
382 /* Save source address */
383 bacpy(&bt_sk(sk
)->src
, &sa
->rc_bdaddr
);
384 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
385 sk
->sk_state
= BT_BOUND
;
388 write_unlock_bh(&rfcomm_sk_list
.lock
);
395 static int rfcomm_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
397 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
398 struct sock
*sk
= sock
->sk
;
399 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
404 if (alen
< sizeof(struct sockaddr_rc
) ||
405 addr
->sa_family
!= AF_BLUETOOTH
)
410 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
) {
415 if (sk
->sk_type
!= SOCK_STREAM
) {
420 sk
->sk_state
= BT_CONNECT
;
421 bacpy(&bt_sk(sk
)->dst
, &sa
->rc_bdaddr
);
422 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
424 d
->sec_level
= rfcomm_pi(sk
)->sec_level
;
425 d
->role_switch
= rfcomm_pi(sk
)->role_switch
;
427 err
= rfcomm_dlc_open(d
, &bt_sk(sk
)->src
, &sa
->rc_bdaddr
, sa
->rc_channel
);
429 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
430 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
437 static int rfcomm_sock_listen(struct socket
*sock
, int backlog
)
439 struct sock
*sk
= sock
->sk
;
442 BT_DBG("sk %p backlog %d", sk
, backlog
);
446 if (sk
->sk_state
!= BT_BOUND
) {
451 if (sk
->sk_type
!= SOCK_STREAM
) {
456 if (!rfcomm_pi(sk
)->channel
) {
457 bdaddr_t
*src
= &bt_sk(sk
)->src
;
462 write_lock_bh(&rfcomm_sk_list
.lock
);
464 for (channel
= 1; channel
< 31; channel
++)
465 if (!__rfcomm_get_sock_by_addr(channel
, src
)) {
466 rfcomm_pi(sk
)->channel
= channel
;
471 write_unlock_bh(&rfcomm_sk_list
.lock
);
477 sk
->sk_max_ack_backlog
= backlog
;
478 sk
->sk_ack_backlog
= 0;
479 sk
->sk_state
= BT_LISTEN
;
486 static int rfcomm_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
488 DECLARE_WAITQUEUE(wait
, current
);
489 struct sock
*sk
= sock
->sk
, *nsk
;
495 if (sk
->sk_state
!= BT_LISTEN
) {
500 if (sk
->sk_type
!= SOCK_STREAM
) {
505 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
507 BT_DBG("sk %p timeo %ld", sk
, timeo
);
509 /* Wait for an incoming connection. (wake-one). */
510 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
511 while (!(nsk
= bt_accept_dequeue(sk
, newsock
))) {
512 set_current_state(TASK_INTERRUPTIBLE
);
519 timeo
= schedule_timeout(timeo
);
522 if (sk
->sk_state
!= BT_LISTEN
) {
527 if (signal_pending(current
)) {
528 err
= sock_intr_errno(timeo
);
532 set_current_state(TASK_RUNNING
);
533 remove_wait_queue(sk_sleep(sk
), &wait
);
538 newsock
->state
= SS_CONNECTED
;
540 BT_DBG("new socket %p", nsk
);
547 static int rfcomm_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
549 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
550 struct sock
*sk
= sock
->sk
;
552 BT_DBG("sock %p, sk %p", sock
, sk
);
554 sa
->rc_family
= AF_BLUETOOTH
;
555 sa
->rc_channel
= rfcomm_pi(sk
)->channel
;
557 bacpy(&sa
->rc_bdaddr
, &bt_sk(sk
)->dst
);
559 bacpy(&sa
->rc_bdaddr
, &bt_sk(sk
)->src
);
561 *len
= sizeof(struct sockaddr_rc
);
565 static int rfcomm_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
566 struct msghdr
*msg
, size_t len
)
568 struct sock
*sk
= sock
->sk
;
569 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
573 if (test_bit(RFCOMM_DEFER_SETUP
, &d
->flags
))
576 if (msg
->msg_flags
& MSG_OOB
)
579 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
582 BT_DBG("sock %p, sk %p", sock
, sk
);
587 size_t size
= min_t(size_t, len
, d
->mtu
);
590 skb
= sock_alloc_send_skb(sk
, size
+ RFCOMM_SKB_RESERVE
,
591 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
597 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
599 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
607 err
= rfcomm_dlc_send(d
, skb
);
624 static int rfcomm_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
625 struct msghdr
*msg
, size_t size
, int flags
)
627 struct sock
*sk
= sock
->sk
;
628 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
631 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
632 rfcomm_dlc_accept(d
);
636 len
= bt_sock_stream_recvmsg(iocb
, sock
, msg
, size
, flags
);
639 if (!(flags
& MSG_PEEK
) && len
> 0)
640 atomic_sub(len
, &sk
->sk_rmem_alloc
);
642 if (atomic_read(&sk
->sk_rmem_alloc
) <= (sk
->sk_rcvbuf
>> 2))
643 rfcomm_dlc_unthrottle(rfcomm_pi(sk
)->dlc
);
649 static int rfcomm_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
651 struct sock
*sk
= sock
->sk
;
661 if (get_user(opt
, (u32 __user
*) optval
)) {
666 if (opt
& RFCOMM_LM_AUTH
)
667 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_LOW
;
668 if (opt
& RFCOMM_LM_ENCRYPT
)
669 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_MEDIUM
;
670 if (opt
& RFCOMM_LM_SECURE
)
671 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_HIGH
;
673 rfcomm_pi(sk
)->role_switch
= (opt
& RFCOMM_LM_MASTER
);
685 static int rfcomm_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
687 struct sock
*sk
= sock
->sk
;
688 struct bt_security sec
;
694 if (level
== SOL_RFCOMM
)
695 return rfcomm_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
697 if (level
!= SOL_BLUETOOTH
)
704 if (sk
->sk_type
!= SOCK_STREAM
) {
709 sec
.level
= BT_SECURITY_LOW
;
711 len
= min_t(unsigned int, sizeof(sec
), optlen
);
712 if (copy_from_user((char *) &sec
, optval
, len
)) {
717 if (sec
.level
> BT_SECURITY_HIGH
) {
722 rfcomm_pi(sk
)->sec_level
= sec
.level
;
726 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
731 if (get_user(opt
, (u32 __user
*) optval
)) {
736 bt_sk(sk
)->defer_setup
= opt
;
748 static int rfcomm_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
750 struct sock
*sk
= sock
->sk
;
751 struct sock
*l2cap_sk
;
752 struct rfcomm_conninfo cinfo
;
758 if (get_user(len
, optlen
))
765 switch (rfcomm_pi(sk
)->sec_level
) {
766 case BT_SECURITY_LOW
:
767 opt
= RFCOMM_LM_AUTH
;
769 case BT_SECURITY_MEDIUM
:
770 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
;
772 case BT_SECURITY_HIGH
:
773 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
|
781 if (rfcomm_pi(sk
)->role_switch
)
782 opt
|= RFCOMM_LM_MASTER
;
784 if (put_user(opt
, (u32 __user
*) optval
))
788 case RFCOMM_CONNINFO
:
789 if (sk
->sk_state
!= BT_CONNECTED
&&
790 !rfcomm_pi(sk
)->dlc
->defer_setup
) {
795 l2cap_sk
= rfcomm_pi(sk
)->dlc
->session
->sock
->sk
;
797 cinfo
.hci_handle
= l2cap_pi(l2cap_sk
)->conn
->hcon
->handle
;
798 memcpy(cinfo
.dev_class
, l2cap_pi(l2cap_sk
)->conn
->hcon
->dev_class
, 3);
800 len
= min_t(unsigned int, len
, sizeof(cinfo
));
801 if (copy_to_user(optval
, (char *) &cinfo
, len
))
815 static int rfcomm_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
817 struct sock
*sk
= sock
->sk
;
818 struct bt_security sec
;
823 if (level
== SOL_RFCOMM
)
824 return rfcomm_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
826 if (level
!= SOL_BLUETOOTH
)
829 if (get_user(len
, optlen
))
836 if (sk
->sk_type
!= SOCK_STREAM
) {
841 sec
.level
= rfcomm_pi(sk
)->sec_level
;
843 len
= min_t(unsigned int, len
, sizeof(sec
));
844 if (copy_to_user(optval
, (char *) &sec
, len
))
850 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
855 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
869 static int rfcomm_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
871 struct sock
*sk __maybe_unused
= sock
->sk
;
874 BT_DBG("sk %p cmd %x arg %lx", sk
, cmd
, arg
);
876 err
= bt_sock_ioctl(sock
, cmd
, arg
);
878 if (err
== -ENOIOCTLCMD
) {
879 #ifdef CONFIG_BT_RFCOMM_TTY
881 err
= rfcomm_dev_ioctl(sk
, cmd
, (void __user
*) arg
);
891 static int rfcomm_sock_shutdown(struct socket
*sock
, int how
)
893 struct sock
*sk
= sock
->sk
;
896 BT_DBG("sock %p, sk %p", sock
, sk
);
901 if (!sk
->sk_shutdown
) {
902 sk
->sk_shutdown
= SHUTDOWN_MASK
;
903 __rfcomm_sock_close(sk
);
905 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
906 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
912 static int rfcomm_sock_release(struct socket
*sock
)
914 struct sock
*sk
= sock
->sk
;
917 BT_DBG("sock %p, sk %p", sock
, sk
);
922 err
= rfcomm_sock_shutdown(sock
, 2);
925 rfcomm_sock_kill(sk
);
929 /* ---- RFCOMM core layer callbacks ----
931 * called under rfcomm_lock()
933 int rfcomm_connect_ind(struct rfcomm_session
*s
, u8 channel
, struct rfcomm_dlc
**d
)
935 struct sock
*sk
, *parent
;
939 BT_DBG("session %p channel %d", s
, channel
);
941 rfcomm_session_getaddr(s
, &src
, &dst
);
943 /* Check if we have socket listening on channel */
944 parent
= rfcomm_get_sock_by_channel(BT_LISTEN
, channel
, &src
);
948 /* Check for backlog size */
949 if (sk_acceptq_is_full(parent
)) {
950 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
954 sk
= rfcomm_sock_alloc(sock_net(parent
), NULL
, BTPROTO_RFCOMM
, GFP_ATOMIC
);
958 rfcomm_sock_init(sk
, parent
);
959 bacpy(&bt_sk(sk
)->src
, &src
);
960 bacpy(&bt_sk(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 (bt_sk(parent
)->defer_setup
)
974 parent
->sk_state_change(parent
);
979 static int rfcomm_sock_debugfs_show(struct seq_file
*f
, void *p
)
982 struct hlist_node
*node
;
984 read_lock_bh(&rfcomm_sk_list
.lock
);
986 sk_for_each(sk
, node
, &rfcomm_sk_list
.head
) {
987 seq_printf(f
, "%s %s %d %d\n",
988 batostr(&bt_sk(sk
)->src
),
989 batostr(&bt_sk(sk
)->dst
),
990 sk
->sk_state
, rfcomm_pi(sk
)->channel
);
993 read_unlock_bh(&rfcomm_sk_list
.lock
);
998 static int rfcomm_sock_debugfs_open(struct inode
*inode
, struct file
*file
)
1000 return single_open(file
, rfcomm_sock_debugfs_show
, inode
->i_private
);
1003 static const struct file_operations rfcomm_sock_debugfs_fops
= {
1004 .open
= rfcomm_sock_debugfs_open
,
1006 .llseek
= seq_lseek
,
1007 .release
= single_release
,
1010 static struct dentry
*rfcomm_sock_debugfs
;
1012 static const struct proto_ops rfcomm_sock_ops
= {
1013 .family
= PF_BLUETOOTH
,
1014 .owner
= THIS_MODULE
,
1015 .release
= rfcomm_sock_release
,
1016 .bind
= rfcomm_sock_bind
,
1017 .connect
= rfcomm_sock_connect
,
1018 .listen
= rfcomm_sock_listen
,
1019 .accept
= rfcomm_sock_accept
,
1020 .getname
= rfcomm_sock_getname
,
1021 .sendmsg
= rfcomm_sock_sendmsg
,
1022 .recvmsg
= rfcomm_sock_recvmsg
,
1023 .shutdown
= rfcomm_sock_shutdown
,
1024 .setsockopt
= rfcomm_sock_setsockopt
,
1025 .getsockopt
= rfcomm_sock_getsockopt
,
1026 .ioctl
= rfcomm_sock_ioctl
,
1027 .poll
= bt_sock_poll
,
1028 .socketpair
= sock_no_socketpair
,
1029 .mmap
= sock_no_mmap
1032 static const struct net_proto_family rfcomm_sock_family_ops
= {
1033 .family
= PF_BLUETOOTH
,
1034 .owner
= THIS_MODULE
,
1035 .create
= rfcomm_sock_create
1038 int __init
rfcomm_init_sockets(void)
1042 err
= proto_register(&rfcomm_proto
, 0);
1046 err
= bt_sock_register(BTPROTO_RFCOMM
, &rfcomm_sock_family_ops
);
1051 rfcomm_sock_debugfs
= debugfs_create_file("rfcomm", 0444,
1052 bt_debugfs
, NULL
, &rfcomm_sock_debugfs_fops
);
1053 if (!rfcomm_sock_debugfs
)
1054 BT_ERR("Failed to create RFCOMM debug file");
1057 BT_INFO("RFCOMM socket layer initialized");
1062 BT_ERR("RFCOMM socket layer registration failed");
1063 proto_unregister(&rfcomm_proto
);
1067 void __exit
rfcomm_cleanup_sockets(void)
1069 debugfs_remove(rfcomm_sock_debugfs
);
1071 if (bt_sock_unregister(BTPROTO_RFCOMM
) < 0)
1072 BT_ERR("RFCOMM socket layer unregistration failed");
1074 proto_unregister(&rfcomm_proto
);