28 #include <linux/export.h>
36 static const struct proto_ops rfcomm_sock_ops;
42 static void rfcomm_sock_close(
struct sock *
sk);
43 static void rfcomm_sock_kill(
struct sock *
sk);
60 rfcomm_dlc_throttle(d);
63 static void rfcomm_sk_state_change(
struct rfcomm_dlc *d,
int err)
71 BT_DBG(
"dlc %p state %ld err %d", d, d->
state, err);
79 sk->sk_state = d->
state;
87 parent->sk_data_ready(parent, 0);
101 rfcomm_sock_kill(sk);
114 !bacmp(&
bt_sk(sk)->src, src))
118 return node ? sk :
NULL;
124 static struct sock *rfcomm_get_sock_by_channel(
int state,
u8 channel, bdaddr_t *src)
132 if (state && sk->sk_state != state)
137 if (!bacmp(&
bt_sk(sk)->src, src))
148 return node ? sk : sk1;
151 static void rfcomm_sock_destruct(
struct sock *sk)
155 BT_DBG(
"sk %p dlc %p", sk, d);
171 static void rfcomm_sock_cleanup_listen(
struct sock *parent)
175 BT_DBG(
"parent %p", parent);
179 rfcomm_sock_close(sk);
180 rfcomm_sock_kill(sk);
190 static void rfcomm_sock_kill(
struct sock *sk)
195 BT_DBG(
"sk %p state %d refcnt %d", sk, sk->sk_state,
atomic_read(&sk->sk_refcnt));
203 static void __rfcomm_sock_close(
struct sock *sk)
209 switch (sk->sk_state) {
211 rfcomm_sock_cleanup_listen(sk);
229 static void rfcomm_sock_close(
struct sock *sk)
232 __rfcomm_sock_close(sk);
236 static void rfcomm_sock_init(
struct sock *sk,
struct sock *parent)
245 &
bt_sk(parent)->flags);
250 security_sk_clone(parent, sk);
252 pi->
dlc->defer_setup = 0;
262 static struct proto rfcomm_proto = {
278 INIT_LIST_HEAD(&
bt_sk(sk)->accept_q);
309 static int rfcomm_sock_create(
struct net *net,
struct socket *sock,
321 sock->
ops = &rfcomm_sock_ops;
323 sk = rfcomm_sock_alloc(net, sock, protocol,
GFP_ATOMIC);
327 rfcomm_sock_init(sk,
NULL);
334 struct sock *sk = sock->
sk;
372 static int rfcomm_sock_connect(
struct socket *sock,
struct sockaddr *addr,
int alen,
int flags)
375 struct sock *sk = sock->
sk;
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);
434 bdaddr_t *src = &
bt_sk(sk)->src;
441 for (channel = 1; channel < 31; channel++)
442 if (!__rfcomm_get_sock_by_addr(channel, src)) {
463 static int rfcomm_sock_accept(
struct socket *sock,
struct socket *newsock,
int flags)
466 struct sock *sk = sock->
sk, *nsk;
477 timeo = sock_rcvtimeo(sk, flags &
O_NONBLOCK);
479 BT_DBG(
"sk %p timeo %ld", sk, timeo);
501 err = sock_intr_errno(timeo);
517 BT_DBG(
"new socket %p", nsk);
524 static int rfcomm_sock_getname(
struct socket *sock,
struct sockaddr *addr,
int *
len,
int peer)
527 struct sock *sk = sock->
sk;
529 BT_DBG(
"sock %p, sk %p", sock, sk);
531 memset(sa, 0,
sizeof(*sa));
543 static int rfcomm_sock_sendmsg(
struct kiocb *
iocb,
struct socket *sock,
546 struct sock *sk = sock->
sk;
560 BT_DBG(
"sock %p, sk %p", sock, sk);
604 static int rfcomm_sock_recvmsg(
struct kiocb *iocb,
struct socket *sock,
605 struct msghdr *msg,
size_t size,
int flags)
607 struct sock *sk = sock->
sk;
623 rfcomm_dlc_unthrottle(
rfcomm_pi(sk)->dlc);
629 static int rfcomm_sock_setsockopt_old(
struct socket *sock,
int optname,
char __user *optval,
unsigned int optlen)
631 struct sock *sk = sock->
sk;
665 static int rfcomm_sock_setsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
unsigned int optlen)
667 struct sock *sk = sock->
sk;
676 return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen);
692 len =
min_t(
unsigned int,
sizeof(
sec), optlen);
733 static int rfcomm_sock_getsockopt_old(
struct socket *sock,
int optname,
char __user *optval,
int __user *optlen)
735 struct sock *sk = sock->
sk;
780 memset(&cinfo, 0,
sizeof(cinfo));
781 cinfo.hci_handle = conn->
hcon->handle;
782 memcpy(cinfo.dev_class, conn->
hcon->dev_class, 3);
784 len =
min_t(
unsigned int, len,
sizeof(cinfo));
799 static int rfcomm_sock_getsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
int __user *optlen)
801 struct sock *sk = sock->
sk;
808 return rfcomm_sock_getsockopt_old(sock, optname, optval, optlen);
828 len =
min_t(
unsigned int, len,
sizeof(
sec));
841 (
u32 __user *) optval))
855 static int rfcomm_sock_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
860 BT_DBG(
"sk %p cmd %x arg %lx", sk, cmd, arg);
865 #ifdef CONFIG_BT_RFCOMM_TTY
877 static int rfcomm_sock_shutdown(
struct socket *sock,
int how)
879 struct sock *sk = sock->
sk;
882 BT_DBG(
"sock %p, sk %p", sock, sk);
890 __rfcomm_sock_close(sk);
899 static int rfcomm_sock_release(
struct socket *sock)
901 struct sock *sk = sock->
sk;
904 BT_DBG(
"sock %p, sk %p", sock, sk);
909 err = rfcomm_sock_shutdown(sock, 2);
912 rfcomm_sock_kill(sk);
922 struct sock *
sk, *parent;
926 BT_DBG(
"session %p channel %d", s, channel);
931 parent = rfcomm_get_sock_by_channel(
BT_LISTEN, channel, &src);
938 if (sk_acceptq_is_full(parent)) {
949 rfcomm_sock_init(sk, parent);
950 bacpy(&
bt_sk(sk)->src, &src);
951 bacpy(&
bt_sk(sk)->dst, &dst);
970 static int rfcomm_sock_debugfs_show(
struct seq_file *
f,
void *
p)
995 .open = rfcomm_sock_debugfs_open,
1001 static struct dentry *rfcomm_sock_debugfs;
1003 static const struct proto_ops rfcomm_sock_ops = {
1006 .release = rfcomm_sock_release,
1007 .bind = rfcomm_sock_bind,
1008 .connect = rfcomm_sock_connect,
1009 .listen = rfcomm_sock_listen,
1010 .accept = rfcomm_sock_accept,
1011 .getname = rfcomm_sock_getname,
1012 .sendmsg = rfcomm_sock_sendmsg,
1013 .recvmsg = rfcomm_sock_recvmsg,
1014 .shutdown = rfcomm_sock_shutdown,
1015 .setsockopt = rfcomm_sock_setsockopt,
1016 .getsockopt = rfcomm_sock_getsockopt,
1017 .ioctl = rfcomm_sock_ioctl,
1026 .create = rfcomm_sock_create
1039 BT_ERR(
"RFCOMM socket layer registration failed");
1045 BT_ERR(
"Failed to create RFCOMM proc file");
1053 if (!rfcomm_sock_debugfs)
1054 BT_ERR(
"Failed to create RFCOMM debug file");
1057 BT_INFO(
"RFCOMM socket layer initialized");
1073 BT_ERR(
"RFCOMM socket layer unregistration failed");