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>
30 #include <linux/sched/signal.h>
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 #include <net/bluetooth/l2cap.h>
35 #include <net/bluetooth/rfcomm.h>
37 static const struct proto_ops rfcomm_sock_ops
;
39 static struct bt_sock_list rfcomm_sk_list
= {
40 .lock
= __RW_LOCK_UNLOCKED(rfcomm_sk_list
.lock
)
43 static void rfcomm_sock_close(struct sock
*sk
);
44 static void rfcomm_sock_kill(struct sock
*sk
);
46 /* ---- DLC callbacks ----
48 * called under rfcomm_dlc_lock()
50 static void rfcomm_sk_data_ready(struct rfcomm_dlc
*d
, struct sk_buff
*skb
)
52 struct sock
*sk
= d
->owner
;
56 atomic_add(skb
->len
, &sk
->sk_rmem_alloc
);
57 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
58 sk
->sk_data_ready(sk
);
60 if (atomic_read(&sk
->sk_rmem_alloc
) >= sk
->sk_rcvbuf
)
61 rfcomm_dlc_throttle(d
);
64 static void rfcomm_sk_state_change(struct rfcomm_dlc
*d
, int err
)
66 struct sock
*sk
= d
->owner
, *parent
;
72 BT_DBG("dlc %p state %ld err %d", d
, d
->state
, err
);
74 local_irq_save(flags
);
80 sk
->sk_state
= d
->state
;
82 parent
= bt_sk(sk
)->parent
;
84 if (d
->state
== BT_CLOSED
) {
85 sock_set_flag(sk
, SOCK_ZAPPED
);
88 parent
->sk_data_ready(parent
);
90 if (d
->state
== BT_CONNECTED
)
91 rfcomm_session_getaddr(d
->session
,
92 &rfcomm_pi(sk
)->src
, NULL
);
93 sk
->sk_state_change(sk
);
97 local_irq_restore(flags
);
99 if (parent
&& sock_flag(sk
, SOCK_ZAPPED
)) {
100 /* We have to drop DLC lock here, otherwise
101 * rfcomm_sock_destruct() will dead lock. */
102 rfcomm_dlc_unlock(d
);
103 rfcomm_sock_kill(sk
);
108 /* ---- Socket functions ---- */
109 static struct sock
*__rfcomm_get_listen_sock_by_addr(u8 channel
, bdaddr_t
*src
)
111 struct sock
*sk
= NULL
;
113 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
114 if (rfcomm_pi(sk
)->channel
!= channel
)
117 if (bacmp(&rfcomm_pi(sk
)->src
, src
))
120 if (sk
->sk_state
== BT_BOUND
|| sk
->sk_state
== BT_LISTEN
)
124 return sk
? sk
: NULL
;
127 /* Find socket with channel and source bdaddr.
128 * Returns closest match.
130 static struct sock
*rfcomm_get_sock_by_channel(int state
, u8 channel
, bdaddr_t
*src
)
132 struct sock
*sk
= NULL
, *sk1
= NULL
;
134 read_lock(&rfcomm_sk_list
.lock
);
136 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
137 if (state
&& sk
->sk_state
!= state
)
140 if (rfcomm_pi(sk
)->channel
== channel
) {
142 if (!bacmp(&rfcomm_pi(sk
)->src
, src
))
146 if (!bacmp(&rfcomm_pi(sk
)->src
, BDADDR_ANY
))
151 read_unlock(&rfcomm_sk_list
.lock
);
153 return sk
? sk
: sk1
;
156 static void rfcomm_sock_destruct(struct sock
*sk
)
158 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
160 BT_DBG("sk %p dlc %p", sk
, d
);
162 skb_queue_purge(&sk
->sk_receive_queue
);
163 skb_queue_purge(&sk
->sk_write_queue
);
166 rfcomm_pi(sk
)->dlc
= NULL
;
168 /* Detach DLC if it's owned by this socket */
171 rfcomm_dlc_unlock(d
);
176 static void rfcomm_sock_cleanup_listen(struct sock
*parent
)
180 BT_DBG("parent %p", parent
);
182 /* Close not yet accepted dlcs */
183 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
184 rfcomm_sock_close(sk
);
185 rfcomm_sock_kill(sk
);
188 parent
->sk_state
= BT_CLOSED
;
189 sock_set_flag(parent
, SOCK_ZAPPED
);
192 /* Kill socket (only if zapped and orphan)
193 * Must be called on unlocked socket.
195 static void rfcomm_sock_kill(struct sock
*sk
)
197 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
200 BT_DBG("sk %p state %d refcnt %d", sk
, sk
->sk_state
, refcount_read(&sk
->sk_refcnt
));
202 /* Kill poor orphan */
203 bt_sock_unlink(&rfcomm_sk_list
, sk
);
204 sock_set_flag(sk
, SOCK_DEAD
);
208 static void __rfcomm_sock_close(struct sock
*sk
)
210 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
212 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
214 switch (sk
->sk_state
) {
216 rfcomm_sock_cleanup_listen(sk
);
223 rfcomm_dlc_close(d
, 0);
227 sock_set_flag(sk
, SOCK_ZAPPED
);
233 * Must be called on unlocked socket.
235 static void rfcomm_sock_close(struct sock
*sk
)
238 __rfcomm_sock_close(sk
);
242 static void rfcomm_sock_init(struct sock
*sk
, struct sock
*parent
)
244 struct rfcomm_pinfo
*pi
= rfcomm_pi(sk
);
249 sk
->sk_type
= parent
->sk_type
;
250 pi
->dlc
->defer_setup
= test_bit(BT_SK_DEFER_SETUP
,
251 &bt_sk(parent
)->flags
);
253 pi
->sec_level
= rfcomm_pi(parent
)->sec_level
;
254 pi
->role_switch
= rfcomm_pi(parent
)->role_switch
;
256 security_sk_clone(parent
, sk
);
258 pi
->dlc
->defer_setup
= 0;
260 pi
->sec_level
= BT_SECURITY_LOW
;
264 pi
->dlc
->sec_level
= pi
->sec_level
;
265 pi
->dlc
->role_switch
= pi
->role_switch
;
268 static struct proto rfcomm_proto
= {
270 .owner
= THIS_MODULE
,
271 .obj_size
= sizeof(struct rfcomm_pinfo
)
274 static struct sock
*rfcomm_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
, int kern
)
276 struct rfcomm_dlc
*d
;
279 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &rfcomm_proto
, kern
);
283 sock_init_data(sock
, sk
);
284 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
286 d
= rfcomm_dlc_alloc(prio
);
292 d
->data_ready
= rfcomm_sk_data_ready
;
293 d
->state_change
= rfcomm_sk_state_change
;
295 rfcomm_pi(sk
)->dlc
= d
;
298 sk
->sk_destruct
= rfcomm_sock_destruct
;
299 sk
->sk_sndtimeo
= RFCOMM_CONN_TIMEOUT
;
301 sk
->sk_sndbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
302 sk
->sk_rcvbuf
= RFCOMM_MAX_CREDITS
* RFCOMM_DEFAULT_MTU
* 10;
304 sock_reset_flag(sk
, SOCK_ZAPPED
);
306 sk
->sk_protocol
= proto
;
307 sk
->sk_state
= BT_OPEN
;
309 bt_sock_link(&rfcomm_sk_list
, sk
);
315 static int rfcomm_sock_create(struct net
*net
, struct socket
*sock
,
316 int protocol
, int kern
)
320 BT_DBG("sock %p", sock
);
322 sock
->state
= SS_UNCONNECTED
;
324 if (sock
->type
!= SOCK_STREAM
&& sock
->type
!= SOCK_RAW
)
325 return -ESOCKTNOSUPPORT
;
327 sock
->ops
= &rfcomm_sock_ops
;
329 sk
= rfcomm_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
, kern
);
333 rfcomm_sock_init(sk
, NULL
);
337 static int rfcomm_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
339 struct sockaddr_rc sa
;
340 struct sock
*sk
= sock
->sk
;
343 if (!addr
|| addr_len
< offsetofend(struct sockaddr
, sa_family
) ||
344 addr
->sa_family
!= AF_BLUETOOTH
)
347 memset(&sa
, 0, sizeof(sa
));
348 len
= min_t(unsigned int, sizeof(sa
), addr_len
);
349 memcpy(&sa
, addr
, len
);
351 BT_DBG("sk %p %pMR", sk
, &sa
.rc_bdaddr
);
355 if (sk
->sk_state
!= BT_OPEN
) {
360 if (sk
->sk_type
!= SOCK_STREAM
) {
365 write_lock(&rfcomm_sk_list
.lock
);
368 __rfcomm_get_listen_sock_by_addr(sa
.rc_channel
, &sa
.rc_bdaddr
)) {
371 /* Save source address */
372 bacpy(&rfcomm_pi(sk
)->src
, &sa
.rc_bdaddr
);
373 rfcomm_pi(sk
)->channel
= sa
.rc_channel
;
374 sk
->sk_state
= BT_BOUND
;
377 write_unlock(&rfcomm_sk_list
.lock
);
384 static int rfcomm_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
386 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
387 struct sock
*sk
= sock
->sk
;
388 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
393 if (alen
< sizeof(struct sockaddr_rc
) ||
394 addr
->sa_family
!= AF_BLUETOOTH
)
399 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
) {
404 if (sk
->sk_type
!= SOCK_STREAM
) {
409 sk
->sk_state
= BT_CONNECT
;
410 bacpy(&rfcomm_pi(sk
)->dst
, &sa
->rc_bdaddr
);
411 rfcomm_pi(sk
)->channel
= sa
->rc_channel
;
413 d
->sec_level
= rfcomm_pi(sk
)->sec_level
;
414 d
->role_switch
= rfcomm_pi(sk
)->role_switch
;
416 err
= rfcomm_dlc_open(d
, &rfcomm_pi(sk
)->src
, &sa
->rc_bdaddr
,
419 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
420 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
427 static int rfcomm_sock_listen(struct socket
*sock
, int backlog
)
429 struct sock
*sk
= sock
->sk
;
432 BT_DBG("sk %p backlog %d", sk
, backlog
);
436 if (sk
->sk_state
!= BT_BOUND
) {
441 if (sk
->sk_type
!= SOCK_STREAM
) {
446 if (!rfcomm_pi(sk
)->channel
) {
447 bdaddr_t
*src
= &rfcomm_pi(sk
)->src
;
452 write_lock(&rfcomm_sk_list
.lock
);
454 for (channel
= 1; channel
< 31; channel
++)
455 if (!__rfcomm_get_listen_sock_by_addr(channel
, src
)) {
456 rfcomm_pi(sk
)->channel
= channel
;
461 write_unlock(&rfcomm_sk_list
.lock
);
467 sk
->sk_max_ack_backlog
= backlog
;
468 sk
->sk_ack_backlog
= 0;
469 sk
->sk_state
= BT_LISTEN
;
476 static int rfcomm_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
,
479 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
480 struct sock
*sk
= sock
->sk
, *nsk
;
484 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
486 if (sk
->sk_type
!= SOCK_STREAM
) {
491 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
493 BT_DBG("sk %p timeo %ld", sk
, timeo
);
495 /* Wait for an incoming connection. (wake-one). */
496 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
498 if (sk
->sk_state
!= BT_LISTEN
) {
503 nsk
= bt_accept_dequeue(sk
, newsock
);
512 if (signal_pending(current
)) {
513 err
= sock_intr_errno(timeo
);
519 timeo
= wait_woken(&wait
, TASK_INTERRUPTIBLE
, timeo
);
521 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
523 remove_wait_queue(sk_sleep(sk
), &wait
);
528 newsock
->state
= SS_CONNECTED
;
530 BT_DBG("new socket %p", nsk
);
537 static int rfcomm_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int peer
)
539 struct sockaddr_rc
*sa
= (struct sockaddr_rc
*) addr
;
540 struct sock
*sk
= sock
->sk
;
542 BT_DBG("sock %p, sk %p", sock
, sk
);
544 if (peer
&& sk
->sk_state
!= BT_CONNECTED
&&
545 sk
->sk_state
!= BT_CONNECT
&& sk
->sk_state
!= BT_CONNECT2
)
548 memset(sa
, 0, sizeof(*sa
));
549 sa
->rc_family
= AF_BLUETOOTH
;
550 sa
->rc_channel
= rfcomm_pi(sk
)->channel
;
552 bacpy(&sa
->rc_bdaddr
, &rfcomm_pi(sk
)->dst
);
554 bacpy(&sa
->rc_bdaddr
, &rfcomm_pi(sk
)->src
);
556 return sizeof(struct sockaddr_rc
);
559 static int rfcomm_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
562 struct sock
*sk
= sock
->sk
;
563 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
567 if (test_bit(RFCOMM_DEFER_SETUP
, &d
->flags
))
570 if (msg
->msg_flags
& MSG_OOB
)
573 if (sk
->sk_shutdown
& SEND_SHUTDOWN
)
576 BT_DBG("sock %p, sk %p", sock
, sk
);
580 sent
= bt_sock_wait_ready(sk
, msg
->msg_flags
);
585 size_t size
= min_t(size_t, len
, d
->mtu
);
588 skb
= sock_alloc_send_skb(sk
, size
+ RFCOMM_SKB_RESERVE
,
589 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
595 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
597 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
605 skb
->priority
= sk
->sk_priority
;
607 err
= rfcomm_dlc_send(d
, skb
);
625 static int rfcomm_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
626 size_t size
, int flags
)
628 struct sock
*sk
= sock
->sk
;
629 struct rfcomm_dlc
*d
= rfcomm_pi(sk
)->dlc
;
632 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
633 rfcomm_dlc_accept(d
);
637 len
= bt_sock_stream_recvmsg(sock
, msg
, size
, flags
);
640 if (!(flags
& MSG_PEEK
) && len
> 0)
641 atomic_sub(len
, &sk
->sk_rmem_alloc
);
643 if (atomic_read(&sk
->sk_rmem_alloc
) <= (sk
->sk_rcvbuf
>> 2))
644 rfcomm_dlc_unthrottle(rfcomm_pi(sk
)->dlc
);
650 static int rfcomm_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
652 struct sock
*sk
= sock
->sk
;
662 if (get_user(opt
, (u32 __user
*) optval
)) {
667 if (opt
& RFCOMM_LM_FIPS
) {
672 if (opt
& RFCOMM_LM_AUTH
)
673 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_LOW
;
674 if (opt
& RFCOMM_LM_ENCRYPT
)
675 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_MEDIUM
;
676 if (opt
& RFCOMM_LM_SECURE
)
677 rfcomm_pi(sk
)->sec_level
= BT_SECURITY_HIGH
;
679 rfcomm_pi(sk
)->role_switch
= (opt
& RFCOMM_LM_MASTER
);
691 static int rfcomm_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
693 struct sock
*sk
= sock
->sk
;
694 struct bt_security sec
;
701 if (level
== SOL_RFCOMM
)
702 return rfcomm_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
704 if (level
!= SOL_BLUETOOTH
)
711 if (sk
->sk_type
!= SOCK_STREAM
) {
716 sec
.level
= BT_SECURITY_LOW
;
718 len
= min_t(unsigned int, sizeof(sec
), optlen
);
719 if (copy_from_user((char *) &sec
, optval
, len
)) {
724 if (sec
.level
> BT_SECURITY_HIGH
) {
729 rfcomm_pi(sk
)->sec_level
= sec
.level
;
733 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
738 if (get_user(opt
, (u32 __user
*) optval
)) {
744 set_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
746 clear_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
759 static int rfcomm_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
761 struct sock
*sk
= sock
->sk
;
762 struct sock
*l2cap_sk
;
763 struct l2cap_conn
*conn
;
764 struct rfcomm_conninfo cinfo
;
770 if (get_user(len
, optlen
))
777 switch (rfcomm_pi(sk
)->sec_level
) {
778 case BT_SECURITY_LOW
:
779 opt
= RFCOMM_LM_AUTH
;
781 case BT_SECURITY_MEDIUM
:
782 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
;
784 case BT_SECURITY_HIGH
:
785 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
|
788 case BT_SECURITY_FIPS
:
789 opt
= RFCOMM_LM_AUTH
| RFCOMM_LM_ENCRYPT
|
790 RFCOMM_LM_SECURE
| RFCOMM_LM_FIPS
;
797 if (rfcomm_pi(sk
)->role_switch
)
798 opt
|= RFCOMM_LM_MASTER
;
800 if (put_user(opt
, (u32 __user
*) optval
))
805 case RFCOMM_CONNINFO
:
806 if (sk
->sk_state
!= BT_CONNECTED
&&
807 !rfcomm_pi(sk
)->dlc
->defer_setup
) {
812 l2cap_sk
= rfcomm_pi(sk
)->dlc
->session
->sock
->sk
;
813 conn
= l2cap_pi(l2cap_sk
)->chan
->conn
;
815 memset(&cinfo
, 0, sizeof(cinfo
));
816 cinfo
.hci_handle
= conn
->hcon
->handle
;
817 memcpy(cinfo
.dev_class
, conn
->hcon
->dev_class
, 3);
819 len
= min_t(unsigned int, len
, sizeof(cinfo
));
820 if (copy_to_user(optval
, (char *) &cinfo
, len
))
834 static int rfcomm_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
836 struct sock
*sk
= sock
->sk
;
837 struct bt_security sec
;
842 if (level
== SOL_RFCOMM
)
843 return rfcomm_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
845 if (level
!= SOL_BLUETOOTH
)
848 if (get_user(len
, optlen
))
855 if (sk
->sk_type
!= SOCK_STREAM
) {
860 sec
.level
= rfcomm_pi(sk
)->sec_level
;
863 len
= min_t(unsigned int, len
, sizeof(sec
));
864 if (copy_to_user(optval
, (char *) &sec
, len
))
870 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
875 if (put_user(test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
),
876 (u32 __user
*) optval
))
890 static int rfcomm_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
892 struct sock
*sk __maybe_unused
= sock
->sk
;
895 BT_DBG("sk %p cmd %x arg %lx", sk
, cmd
, arg
);
897 err
= bt_sock_ioctl(sock
, cmd
, arg
);
899 if (err
== -ENOIOCTLCMD
) {
900 #ifdef CONFIG_BT_RFCOMM_TTY
902 err
= rfcomm_dev_ioctl(sk
, cmd
, (void __user
*) arg
);
912 static int rfcomm_sock_shutdown(struct socket
*sock
, int how
)
914 struct sock
*sk
= sock
->sk
;
917 BT_DBG("sock %p, sk %p", sock
, sk
);
923 if (!sk
->sk_shutdown
) {
924 sk
->sk_shutdown
= SHUTDOWN_MASK
;
925 __rfcomm_sock_close(sk
);
927 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
&&
928 !(current
->flags
& PF_EXITING
))
929 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
935 static int rfcomm_sock_release(struct socket
*sock
)
937 struct sock
*sk
= sock
->sk
;
940 BT_DBG("sock %p, sk %p", sock
, sk
);
945 err
= rfcomm_sock_shutdown(sock
, 2);
948 rfcomm_sock_kill(sk
);
952 /* ---- RFCOMM core layer callbacks ----
954 * called under rfcomm_lock()
956 int rfcomm_connect_ind(struct rfcomm_session
*s
, u8 channel
, struct rfcomm_dlc
**d
)
958 struct sock
*sk
, *parent
;
962 BT_DBG("session %p channel %d", s
, channel
);
964 rfcomm_session_getaddr(s
, &src
, &dst
);
966 /* Check if we have socket listening on channel */
967 parent
= rfcomm_get_sock_by_channel(BT_LISTEN
, channel
, &src
);
971 bh_lock_sock(parent
);
973 /* Check for backlog size */
974 if (sk_acceptq_is_full(parent
)) {
975 BT_DBG("backlog full %d", parent
->sk_ack_backlog
);
979 sk
= rfcomm_sock_alloc(sock_net(parent
), NULL
, BTPROTO_RFCOMM
, GFP_ATOMIC
, 0);
983 bt_sock_reclassify_lock(sk
, BTPROTO_RFCOMM
);
985 rfcomm_sock_init(sk
, parent
);
986 bacpy(&rfcomm_pi(sk
)->src
, &src
);
987 bacpy(&rfcomm_pi(sk
)->dst
, &dst
);
988 rfcomm_pi(sk
)->channel
= channel
;
990 sk
->sk_state
= BT_CONFIG
;
991 bt_accept_enqueue(parent
, sk
, true);
993 /* Accept connection and return socket DLC */
994 *d
= rfcomm_pi(sk
)->dlc
;
998 bh_unlock_sock(parent
);
1000 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(parent
)->flags
))
1001 parent
->sk_state_change(parent
);
1006 static int rfcomm_sock_debugfs_show(struct seq_file
*f
, void *p
)
1010 read_lock(&rfcomm_sk_list
.lock
);
1012 sk_for_each(sk
, &rfcomm_sk_list
.head
) {
1013 seq_printf(f
, "%pMR %pMR %d %d\n",
1014 &rfcomm_pi(sk
)->src
, &rfcomm_pi(sk
)->dst
,
1015 sk
->sk_state
, rfcomm_pi(sk
)->channel
);
1018 read_unlock(&rfcomm_sk_list
.lock
);
1023 DEFINE_SHOW_ATTRIBUTE(rfcomm_sock_debugfs
);
1025 static struct dentry
*rfcomm_sock_debugfs
;
1027 static const struct proto_ops rfcomm_sock_ops
= {
1028 .family
= PF_BLUETOOTH
,
1029 .owner
= THIS_MODULE
,
1030 .release
= rfcomm_sock_release
,
1031 .bind
= rfcomm_sock_bind
,
1032 .connect
= rfcomm_sock_connect
,
1033 .listen
= rfcomm_sock_listen
,
1034 .accept
= rfcomm_sock_accept
,
1035 .getname
= rfcomm_sock_getname
,
1036 .sendmsg
= rfcomm_sock_sendmsg
,
1037 .recvmsg
= rfcomm_sock_recvmsg
,
1038 .shutdown
= rfcomm_sock_shutdown
,
1039 .setsockopt
= rfcomm_sock_setsockopt
,
1040 .getsockopt
= rfcomm_sock_getsockopt
,
1041 .ioctl
= rfcomm_sock_ioctl
,
1042 .gettstamp
= sock_gettstamp
,
1043 .poll
= bt_sock_poll
,
1044 .socketpair
= sock_no_socketpair
,
1045 .mmap
= sock_no_mmap
1048 static const struct net_proto_family rfcomm_sock_family_ops
= {
1049 .family
= PF_BLUETOOTH
,
1050 .owner
= THIS_MODULE
,
1051 .create
= rfcomm_sock_create
1054 int __init
rfcomm_init_sockets(void)
1058 BUILD_BUG_ON(sizeof(struct sockaddr_rc
) > sizeof(struct sockaddr
));
1060 err
= proto_register(&rfcomm_proto
, 0);
1064 err
= bt_sock_register(BTPROTO_RFCOMM
, &rfcomm_sock_family_ops
);
1066 BT_ERR("RFCOMM socket layer registration failed");
1070 err
= bt_procfs_init(&init_net
, "rfcomm", &rfcomm_sk_list
, NULL
);
1072 BT_ERR("Failed to create RFCOMM proc file");
1073 bt_sock_unregister(BTPROTO_RFCOMM
);
1077 BT_INFO("RFCOMM socket layer initialized");
1079 if (IS_ERR_OR_NULL(bt_debugfs
))
1082 rfcomm_sock_debugfs
= debugfs_create_file("rfcomm", 0444,
1084 &rfcomm_sock_debugfs_fops
);
1089 proto_unregister(&rfcomm_proto
);
1093 void __exit
rfcomm_cleanup_sockets(void)
1095 bt_procfs_cleanup(&init_net
, "rfcomm");
1097 debugfs_remove(rfcomm_sock_debugfs
);
1099 bt_sock_unregister(BTPROTO_RFCOMM
);
1101 proto_unregister(&rfcomm_proto
);