20 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
23 #include <linux/kernel.h>
24 #include <linux/module.h>
30 static int sock_wait_state(
struct sock *
sk,
int state,
unsigned long timeo)
40 while (sk->sk_state != state) {
47 err = sock_intr_errno(timeo);
66 static struct proto llcp_sock_proto = {
74 struct sock *
sk = sock->
sk;
86 memset(&llcp_addr, 0,
sizeof(llcp_addr));
87 len =
min_t(
unsigned int,
sizeof(llcp_addr), alen);
88 memcpy(&llcp_addr, addr, len);
91 if (llcp_addr.dsap != 0)
117 llcp_addr.service_name_len,
145 static int llcp_raw_sock_bind(
struct socket *sock,
struct sockaddr *addr,
148 struct sock *sk = sock->
sk;
160 memset(&llcp_addr, 0,
sizeof(llcp_addr));
161 len =
min_t(
unsigned int,
sizeof(llcp_addr), alen);
162 memcpy(&llcp_addr, addr, len);
199 static int llcp_sock_listen(
struct socket *sock,
int backlog)
201 struct sock *sk = sock->
sk;
204 pr_debug(
"sk %p backlog %d\n", sk, backlog);
230 pr_debug(
"state %d\n", sk->sk_state);
233 sk_acceptq_removed(llcp_sock->
parent);
250 sk_acceptq_added(parent);
275 sock_graft(sk, newsock);
279 pr_debug(
"Returning sk state %d\n", sk->sk_state);
290 static int llcp_sock_accept(
struct socket *sock,
struct socket *newsock,
294 struct sock *sk = sock->
sk, *new_sk;
307 timeo = sock_rcvtimeo(sk, flags &
O_NONBLOCK);
320 ret = sock_intr_errno(timeo);
336 pr_debug(
"new socket %p\n", new_sk);
347 struct sock *sk = sock->
sk;
361 llcp_addr->dev_idx = llcp_sock->
dev->idx;
362 llcp_addr->target_idx = llcp_sock->
target_idx;
363 llcp_addr->dsap = llcp_sock->
dsap;
364 llcp_addr->ssap = llcp_sock->
ssap;
367 llcp_addr->service_name_len);
372 static inline unsigned int llcp_accept_poll(
struct sock *parent)
390 static unsigned int llcp_sock_poll(
struct file *
file,
struct socket *sock,
393 struct sock *sk = sock->
sk;
394 unsigned int mask = 0;
398 sock_poll_wait(file, sk_sleep(sk), wait);
401 return llcp_accept_poll(sk);
418 if (sock_writeable(sk))
428 static int llcp_sock_release(
struct socket *sock)
430 struct sock *sk = sock->
sk;
440 local = llcp_sock->
local;
454 struct sock *accept_sk;
458 accept_sk = &lsk->
sk;
459 lock_sock(accept_sk);
466 sock_orphan(accept_sk);
487 static int llcp_sock_connect(
struct socket *sock,
struct sockaddr *_addr,
490 struct sock *sk = sock->
sk;
497 pr_debug(
"sock %p sk %p flags 0x%x\n", sock, sk, flags);
528 device_lock(&dev->
dev);
531 device_unlock(&dev->
dev);
534 device_unlock(&dev->
dev);
544 llcp_sock->
miu = llcp_sock->
local->remote_miu;
596 struct sock *sk = sock->
sk;
600 pr_debug(
"sock %p sk %p", sock, sk);
602 ret = sock_error(sk);
621 static int llcp_sock_recvmsg(
struct kiocb *iocb,
struct socket *sock,
622 struct msghdr *msg,
size_t len,
int flags)
625 struct sock *sk = sock->
sk;
626 unsigned int copied, rlen;
647 pr_err(
"Recv datagram failed state %d %d %d",
648 sk->sk_state, err, sock_error(sk));
657 copied =
min_t(
unsigned int, rlen, len);
691 static const struct proto_ops llcp_sock_ops = {
694 .bind = llcp_sock_bind,
695 .connect = llcp_sock_connect,
696 .release = llcp_sock_release,
698 .accept = llcp_sock_accept,
699 .getname = llcp_sock_getname,
700 .poll = llcp_sock_poll,
702 .listen = llcp_sock_listen,
706 .sendmsg = llcp_sock_sendmsg,
707 .recvmsg = llcp_sock_recvmsg,
711 static const struct proto_ops llcp_rawsock_ops = {
714 .bind = llcp_raw_sock_bind,
716 .release = llcp_sock_release,
719 .getname = llcp_sock_getname,
720 .poll = llcp_sock_poll,
727 .recvmsg = llcp_sock_recvmsg,
731 static void llcp_sock_destruct(
struct sock *sk)
738 nfc_put_device(llcp_sock->
dev);
745 pr_err(
"Freeing alive NFC LLCP socket %p\n", sk);
775 skb_queue_head_init(&llcp_sock->
tx_queue);
801 static int llcp_sock_create(
struct net *
net,
struct socket *sock,
814 sock->
ops = &llcp_rawsock_ops;
816 sock->
ops = &llcp_sock_ops;
827 .proto = &llcp_sock_proto,
829 .create = llcp_sock_create