Linux 3.12.28
[linux/fpc-iii.git] / net / nfc / llcp_sock.c
blob824c6056bf823b0852b116af45698da916cdc772
1 /*
2 * Copyright (C) 2011 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/nfc.h>
27 #include "nfc.h"
28 #include "llcp.h"
30 static int sock_wait_state(struct sock *sk, int state, unsigned long timeo)
32 DECLARE_WAITQUEUE(wait, current);
33 int err = 0;
35 pr_debug("sk %p", sk);
37 add_wait_queue(sk_sleep(sk), &wait);
38 set_current_state(TASK_INTERRUPTIBLE);
40 while (sk->sk_state != state) {
41 if (!timeo) {
42 err = -EINPROGRESS;
43 break;
46 if (signal_pending(current)) {
47 err = sock_intr_errno(timeo);
48 break;
51 release_sock(sk);
52 timeo = schedule_timeout(timeo);
53 lock_sock(sk);
54 set_current_state(TASK_INTERRUPTIBLE);
56 err = sock_error(sk);
57 if (err)
58 break;
61 __set_current_state(TASK_RUNNING);
62 remove_wait_queue(sk_sleep(sk), &wait);
63 return err;
66 static struct proto llcp_sock_proto = {
67 .name = "NFC_LLCP",
68 .owner = THIS_MODULE,
69 .obj_size = sizeof(struct nfc_llcp_sock),
72 static int llcp_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
74 struct sock *sk = sock->sk;
75 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
76 struct nfc_llcp_local *local;
77 struct nfc_dev *dev;
78 struct sockaddr_nfc_llcp llcp_addr;
79 int len, ret = 0;
81 if (!addr || addr->sa_family != AF_NFC)
82 return -EINVAL;
84 pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
86 memset(&llcp_addr, 0, sizeof(llcp_addr));
87 len = min_t(unsigned int, sizeof(llcp_addr), alen);
88 memcpy(&llcp_addr, addr, len);
90 /* This is going to be a listening socket, dsap must be 0 */
91 if (llcp_addr.dsap != 0)
92 return -EINVAL;
94 lock_sock(sk);
96 if (sk->sk_state != LLCP_CLOSED) {
97 ret = -EBADFD;
98 goto error;
101 dev = nfc_get_device(llcp_addr.dev_idx);
102 if (dev == NULL) {
103 ret = -ENODEV;
104 goto error;
107 local = nfc_llcp_find_local(dev);
108 if (local == NULL) {
109 ret = -ENODEV;
110 goto put_dev;
113 llcp_sock->dev = dev;
114 llcp_sock->local = nfc_llcp_local_get(local);
115 llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
116 llcp_sock->service_name_len = min_t(unsigned int,
117 llcp_addr.service_name_len,
118 NFC_LLCP_MAX_SERVICE_NAME);
119 llcp_sock->service_name = kmemdup(llcp_addr.service_name,
120 llcp_sock->service_name_len,
121 GFP_KERNEL);
123 llcp_sock->ssap = nfc_llcp_get_sdp_ssap(local, llcp_sock);
124 if (llcp_sock->ssap == LLCP_SAP_MAX) {
125 ret = -EADDRINUSE;
126 goto put_dev;
129 llcp_sock->reserved_ssap = llcp_sock->ssap;
131 nfc_llcp_sock_link(&local->sockets, sk);
133 pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap);
135 sk->sk_state = LLCP_BOUND;
137 put_dev:
138 nfc_put_device(dev);
140 error:
141 release_sock(sk);
142 return ret;
145 static int llcp_raw_sock_bind(struct socket *sock, struct sockaddr *addr,
146 int alen)
148 struct sock *sk = sock->sk;
149 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
150 struct nfc_llcp_local *local;
151 struct nfc_dev *dev;
152 struct sockaddr_nfc_llcp llcp_addr;
153 int len, ret = 0;
155 if (!addr || addr->sa_family != AF_NFC)
156 return -EINVAL;
158 pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
160 memset(&llcp_addr, 0, sizeof(llcp_addr));
161 len = min_t(unsigned int, sizeof(llcp_addr), alen);
162 memcpy(&llcp_addr, addr, len);
164 lock_sock(sk);
166 if (sk->sk_state != LLCP_CLOSED) {
167 ret = -EBADFD;
168 goto error;
171 dev = nfc_get_device(llcp_addr.dev_idx);
172 if (dev == NULL) {
173 ret = -ENODEV;
174 goto error;
177 local = nfc_llcp_find_local(dev);
178 if (local == NULL) {
179 ret = -ENODEV;
180 goto put_dev;
183 llcp_sock->dev = dev;
184 llcp_sock->local = nfc_llcp_local_get(local);
185 llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
187 nfc_llcp_sock_link(&local->raw_sockets, sk);
189 sk->sk_state = LLCP_BOUND;
191 put_dev:
192 nfc_put_device(dev);
194 error:
195 release_sock(sk);
196 return ret;
199 static int llcp_sock_listen(struct socket *sock, int backlog)
201 struct sock *sk = sock->sk;
202 int ret = 0;
204 pr_debug("sk %p backlog %d\n", sk, backlog);
206 lock_sock(sk);
208 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) ||
209 sk->sk_state != LLCP_BOUND) {
210 ret = -EBADFD;
211 goto error;
214 sk->sk_max_ack_backlog = backlog;
215 sk->sk_ack_backlog = 0;
217 pr_debug("Socket listening\n");
218 sk->sk_state = LLCP_LISTEN;
220 error:
221 release_sock(sk);
223 return ret;
226 static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname,
227 char __user *optval, unsigned int optlen)
229 struct sock *sk = sock->sk;
230 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
231 u32 opt;
232 int err = 0;
234 pr_debug("%p optname %d\n", sk, optname);
236 if (level != SOL_NFC)
237 return -ENOPROTOOPT;
239 lock_sock(sk);
241 switch (optname) {
242 case NFC_LLCP_RW:
243 if (sk->sk_state == LLCP_CONNECTED ||
244 sk->sk_state == LLCP_BOUND ||
245 sk->sk_state == LLCP_LISTEN) {
246 err = -EINVAL;
247 break;
250 if (get_user(opt, (u32 __user *) optval)) {
251 err = -EFAULT;
252 break;
255 if (opt > LLCP_MAX_RW) {
256 err = -EINVAL;
257 break;
260 llcp_sock->rw = (u8) opt;
262 break;
264 case NFC_LLCP_MIUX:
265 if (sk->sk_state == LLCP_CONNECTED ||
266 sk->sk_state == LLCP_BOUND ||
267 sk->sk_state == LLCP_LISTEN) {
268 err = -EINVAL;
269 break;
272 if (get_user(opt, (u32 __user *) optval)) {
273 err = -EFAULT;
274 break;
277 if (opt > LLCP_MAX_MIUX) {
278 err = -EINVAL;
279 break;
282 llcp_sock->miux = cpu_to_be16((u16) opt);
284 break;
286 default:
287 err = -ENOPROTOOPT;
288 break;
291 release_sock(sk);
293 pr_debug("%p rw %d miux %d\n", llcp_sock,
294 llcp_sock->rw, llcp_sock->miux);
296 return err;
299 static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
300 char __user *optval, int __user *optlen)
302 struct nfc_llcp_local *local;
303 struct sock *sk = sock->sk;
304 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
305 int len, err = 0;
306 u16 miux, remote_miu;
307 u8 rw;
309 pr_debug("%p optname %d\n", sk, optname);
311 if (level != SOL_NFC)
312 return -ENOPROTOOPT;
314 if (get_user(len, optlen))
315 return -EFAULT;
317 local = llcp_sock->local;
318 if (!local)
319 return -ENODEV;
321 len = min_t(u32, len, sizeof(u32));
323 lock_sock(sk);
325 switch (optname) {
326 case NFC_LLCP_RW:
327 rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw;
328 if (put_user(rw, (u32 __user *) optval))
329 err = -EFAULT;
331 break;
333 case NFC_LLCP_MIUX:
334 miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ?
335 be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux);
337 if (put_user(miux, (u32 __user *) optval))
338 err = -EFAULT;
340 break;
342 case NFC_LLCP_REMOTE_MIU:
343 remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ?
344 local->remote_miu : llcp_sock->remote_miu;
346 if (put_user(remote_miu, (u32 __user *) optval))
347 err = -EFAULT;
349 break;
351 case NFC_LLCP_REMOTE_LTO:
352 if (put_user(local->remote_lto / 10, (u32 __user *) optval))
353 err = -EFAULT;
355 break;
357 case NFC_LLCP_REMOTE_RW:
358 if (put_user(llcp_sock->remote_rw, (u32 __user *) optval))
359 err = -EFAULT;
361 break;
363 default:
364 err = -ENOPROTOOPT;
365 break;
368 release_sock(sk);
370 if (put_user(len, optlen))
371 return -EFAULT;
373 return err;
376 void nfc_llcp_accept_unlink(struct sock *sk)
378 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
380 pr_debug("state %d\n", sk->sk_state);
382 list_del_init(&llcp_sock->accept_queue);
383 sk_acceptq_removed(llcp_sock->parent);
384 llcp_sock->parent = NULL;
386 sock_put(sk);
389 void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk)
391 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
392 struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent);
394 /* Lock will be free from unlink */
395 sock_hold(sk);
397 list_add_tail(&llcp_sock->accept_queue,
398 &llcp_sock_parent->accept_queue);
399 llcp_sock->parent = parent;
400 sk_acceptq_added(parent);
403 struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
404 struct socket *newsock)
406 struct nfc_llcp_sock *lsk, *n, *llcp_parent;
407 struct sock *sk;
409 llcp_parent = nfc_llcp_sock(parent);
411 list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue,
412 accept_queue) {
413 sk = &lsk->sk;
414 lock_sock(sk);
416 if (sk->sk_state == LLCP_CLOSED) {
417 release_sock(sk);
418 nfc_llcp_accept_unlink(sk);
419 continue;
422 if (sk->sk_state == LLCP_CONNECTED || !newsock) {
423 list_del_init(&lsk->accept_queue);
424 sock_put(sk);
426 if (newsock)
427 sock_graft(sk, newsock);
429 release_sock(sk);
431 pr_debug("Returning sk state %d\n", sk->sk_state);
433 sk_acceptq_removed(parent);
435 return sk;
438 release_sock(sk);
441 return NULL;
444 static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
445 int flags)
447 DECLARE_WAITQUEUE(wait, current);
448 struct sock *sk = sock->sk, *new_sk;
449 long timeo;
450 int ret = 0;
452 pr_debug("parent %p\n", sk);
454 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
456 if (sk->sk_state != LLCP_LISTEN) {
457 ret = -EBADFD;
458 goto error;
461 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
463 /* Wait for an incoming connection. */
464 add_wait_queue_exclusive(sk_sleep(sk), &wait);
465 while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
466 set_current_state(TASK_INTERRUPTIBLE);
468 if (!timeo) {
469 ret = -EAGAIN;
470 break;
473 if (signal_pending(current)) {
474 ret = sock_intr_errno(timeo);
475 break;
478 release_sock(sk);
479 timeo = schedule_timeout(timeo);
480 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
482 __set_current_state(TASK_RUNNING);
483 remove_wait_queue(sk_sleep(sk), &wait);
485 if (ret)
486 goto error;
488 newsock->state = SS_CONNECTED;
490 pr_debug("new socket %p\n", new_sk);
492 error:
493 release_sock(sk);
495 return ret;
498 static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
499 int *len, int peer)
501 struct sock *sk = sock->sk;
502 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
503 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr);
505 if (llcp_sock == NULL || llcp_sock->dev == NULL)
506 return -EBADFD;
508 pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx,
509 llcp_sock->dsap, llcp_sock->ssap);
511 memset(llcp_addr, 0, sizeof(*llcp_addr));
512 *len = sizeof(struct sockaddr_nfc_llcp);
514 llcp_addr->sa_family = AF_NFC;
515 llcp_addr->dev_idx = llcp_sock->dev->idx;
516 llcp_addr->target_idx = llcp_sock->target_idx;
517 llcp_addr->nfc_protocol = llcp_sock->nfc_protocol;
518 llcp_addr->dsap = llcp_sock->dsap;
519 llcp_addr->ssap = llcp_sock->ssap;
520 llcp_addr->service_name_len = llcp_sock->service_name_len;
521 memcpy(llcp_addr->service_name, llcp_sock->service_name,
522 llcp_addr->service_name_len);
524 return 0;
527 static inline unsigned int llcp_accept_poll(struct sock *parent)
529 struct nfc_llcp_sock *llcp_sock, *n, *parent_sock;
530 struct sock *sk;
532 parent_sock = nfc_llcp_sock(parent);
534 list_for_each_entry_safe(llcp_sock, n, &parent_sock->accept_queue,
535 accept_queue) {
536 sk = &llcp_sock->sk;
538 if (sk->sk_state == LLCP_CONNECTED)
539 return POLLIN | POLLRDNORM;
542 return 0;
545 static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
546 poll_table *wait)
548 struct sock *sk = sock->sk;
549 unsigned int mask = 0;
551 pr_debug("%p\n", sk);
553 sock_poll_wait(file, sk_sleep(sk), wait);
555 if (sk->sk_state == LLCP_LISTEN)
556 return llcp_accept_poll(sk);
558 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
559 mask |= POLLERR |
560 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
562 if (!skb_queue_empty(&sk->sk_receive_queue))
563 mask |= POLLIN | POLLRDNORM;
565 if (sk->sk_state == LLCP_CLOSED)
566 mask |= POLLHUP;
568 if (sk->sk_shutdown & RCV_SHUTDOWN)
569 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
571 if (sk->sk_shutdown == SHUTDOWN_MASK)
572 mask |= POLLHUP;
574 if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED)
575 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
576 else
577 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
579 pr_debug("mask 0x%x\n", mask);
581 return mask;
584 static int llcp_sock_release(struct socket *sock)
586 struct sock *sk = sock->sk;
587 struct nfc_llcp_local *local;
588 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
589 int err = 0;
591 if (!sk)
592 return 0;
594 pr_debug("%p\n", sk);
596 local = llcp_sock->local;
597 if (local == NULL) {
598 err = -ENODEV;
599 goto out;
602 lock_sock(sk);
604 /* Send a DISC */
605 if (sk->sk_state == LLCP_CONNECTED)
606 nfc_llcp_send_disconnect(llcp_sock);
608 if (sk->sk_state == LLCP_LISTEN) {
609 struct nfc_llcp_sock *lsk, *n;
610 struct sock *accept_sk;
612 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
613 accept_queue) {
614 accept_sk = &lsk->sk;
615 lock_sock(accept_sk);
617 nfc_llcp_send_disconnect(lsk);
618 nfc_llcp_accept_unlink(accept_sk);
620 release_sock(accept_sk);
624 if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
625 nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
627 release_sock(sk);
629 /* Keep this sock alive and therefore do not remove it from the sockets
630 * list until the DISC PDU has been actually sent. Otherwise we would
631 * reply with DM PDUs before sending the DISC one.
633 if (sk->sk_state == LLCP_DISCONNECTING)
634 return err;
636 if (sock->type == SOCK_RAW)
637 nfc_llcp_sock_unlink(&local->raw_sockets, sk);
638 else
639 nfc_llcp_sock_unlink(&local->sockets, sk);
641 out:
642 sock_orphan(sk);
643 sock_put(sk);
645 return err;
648 static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
649 int len, int flags)
651 struct sock *sk = sock->sk;
652 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
653 struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
654 struct nfc_dev *dev;
655 struct nfc_llcp_local *local;
656 int ret = 0;
658 pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
660 if (!addr || len < sizeof(struct sockaddr_nfc) ||
661 addr->sa_family != AF_NFC)
662 return -EINVAL;
664 if (addr->service_name_len == 0 && addr->dsap == 0)
665 return -EINVAL;
667 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
668 addr->target_idx, addr->nfc_protocol);
670 lock_sock(sk);
672 if (sk->sk_state == LLCP_CONNECTED) {
673 ret = -EISCONN;
674 goto error;
677 dev = nfc_get_device(addr->dev_idx);
678 if (dev == NULL) {
679 ret = -ENODEV;
680 goto error;
683 local = nfc_llcp_find_local(dev);
684 if (local == NULL) {
685 ret = -ENODEV;
686 goto put_dev;
689 device_lock(&dev->dev);
690 if (dev->dep_link_up == false) {
691 ret = -ENOLINK;
692 device_unlock(&dev->dev);
693 goto put_dev;
695 device_unlock(&dev->dev);
697 if (local->rf_mode == NFC_RF_INITIATOR &&
698 addr->target_idx != local->target_idx) {
699 ret = -ENOLINK;
700 goto put_dev;
703 llcp_sock->dev = dev;
704 llcp_sock->local = nfc_llcp_local_get(local);
705 llcp_sock->remote_miu = llcp_sock->local->remote_miu;
706 llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
707 if (llcp_sock->ssap == LLCP_SAP_MAX) {
708 ret = -ENOMEM;
709 goto put_dev;
712 llcp_sock->reserved_ssap = llcp_sock->ssap;
714 if (addr->service_name_len == 0)
715 llcp_sock->dsap = addr->dsap;
716 else
717 llcp_sock->dsap = LLCP_SAP_SDP;
718 llcp_sock->nfc_protocol = addr->nfc_protocol;
719 llcp_sock->service_name_len = min_t(unsigned int,
720 addr->service_name_len,
721 NFC_LLCP_MAX_SERVICE_NAME);
722 llcp_sock->service_name = kmemdup(addr->service_name,
723 llcp_sock->service_name_len,
724 GFP_KERNEL);
726 nfc_llcp_sock_link(&local->connecting_sockets, sk);
728 ret = nfc_llcp_send_connect(llcp_sock);
729 if (ret)
730 goto sock_unlink;
732 sk->sk_state = LLCP_CONNECTING;
734 ret = sock_wait_state(sk, LLCP_CONNECTED,
735 sock_sndtimeo(sk, flags & O_NONBLOCK));
736 if (ret && ret != -EINPROGRESS)
737 goto sock_unlink;
739 release_sock(sk);
741 return ret;
743 sock_unlink:
744 nfc_llcp_put_ssap(local, llcp_sock->ssap);
746 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
748 put_dev:
749 nfc_put_device(dev);
751 error:
752 release_sock(sk);
753 return ret;
756 static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
757 struct msghdr *msg, size_t len)
759 struct sock *sk = sock->sk;
760 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
761 int ret;
763 pr_debug("sock %p sk %p", sock, sk);
765 ret = sock_error(sk);
766 if (ret)
767 return ret;
769 if (msg->msg_flags & MSG_OOB)
770 return -EOPNOTSUPP;
772 lock_sock(sk);
774 if (sk->sk_type == SOCK_DGRAM) {
775 struct sockaddr_nfc_llcp *addr =
776 (struct sockaddr_nfc_llcp *)msg->msg_name;
778 if (msg->msg_namelen < sizeof(*addr)) {
779 release_sock(sk);
780 return -EINVAL;
783 release_sock(sk);
785 return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
786 msg, len);
789 if (sk->sk_state != LLCP_CONNECTED) {
790 release_sock(sk);
791 return -ENOTCONN;
794 release_sock(sk);
796 return nfc_llcp_send_i_frame(llcp_sock, msg, len);
799 static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
800 struct msghdr *msg, size_t len, int flags)
802 int noblock = flags & MSG_DONTWAIT;
803 struct sock *sk = sock->sk;
804 unsigned int copied, rlen;
805 struct sk_buff *skb, *cskb;
806 int err = 0;
808 pr_debug("%p %zu\n", sk, len);
810 lock_sock(sk);
812 if (sk->sk_state == LLCP_CLOSED &&
813 skb_queue_empty(&sk->sk_receive_queue)) {
814 release_sock(sk);
815 return 0;
818 release_sock(sk);
820 if (flags & (MSG_OOB))
821 return -EOPNOTSUPP;
823 skb = skb_recv_datagram(sk, flags, noblock, &err);
824 if (!skb) {
825 pr_err("Recv datagram failed state %d %d %d",
826 sk->sk_state, err, sock_error(sk));
828 if (sk->sk_shutdown & RCV_SHUTDOWN)
829 return 0;
831 return err;
834 rlen = skb->len; /* real length of skb */
835 copied = min_t(unsigned int, rlen, len);
837 cskb = skb;
838 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
839 if (!(flags & MSG_PEEK))
840 skb_queue_head(&sk->sk_receive_queue, skb);
841 return -EFAULT;
844 sock_recv_timestamp(msg, sk, skb);
846 if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
847 struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
848 struct sockaddr_nfc_llcp *sockaddr =
849 (struct sockaddr_nfc_llcp *) msg->msg_name;
851 msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
853 pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
855 memset(sockaddr, 0, sizeof(*sockaddr));
856 sockaddr->sa_family = AF_NFC;
857 sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP;
858 sockaddr->dsap = ui_cb->dsap;
859 sockaddr->ssap = ui_cb->ssap;
862 /* Mark read part of skb as used */
863 if (!(flags & MSG_PEEK)) {
865 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
866 if (sk->sk_type == SOCK_STREAM ||
867 sk->sk_type == SOCK_DGRAM ||
868 sk->sk_type == SOCK_RAW) {
869 skb_pull(skb, copied);
870 if (skb->len) {
871 skb_queue_head(&sk->sk_receive_queue, skb);
872 goto done;
876 kfree_skb(skb);
879 /* XXX Queue backlogged skbs */
881 done:
882 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
883 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
884 copied = rlen;
886 return copied;
889 static const struct proto_ops llcp_sock_ops = {
890 .family = PF_NFC,
891 .owner = THIS_MODULE,
892 .bind = llcp_sock_bind,
893 .connect = llcp_sock_connect,
894 .release = llcp_sock_release,
895 .socketpair = sock_no_socketpair,
896 .accept = llcp_sock_accept,
897 .getname = llcp_sock_getname,
898 .poll = llcp_sock_poll,
899 .ioctl = sock_no_ioctl,
900 .listen = llcp_sock_listen,
901 .shutdown = sock_no_shutdown,
902 .setsockopt = nfc_llcp_setsockopt,
903 .getsockopt = nfc_llcp_getsockopt,
904 .sendmsg = llcp_sock_sendmsg,
905 .recvmsg = llcp_sock_recvmsg,
906 .mmap = sock_no_mmap,
909 static const struct proto_ops llcp_rawsock_ops = {
910 .family = PF_NFC,
911 .owner = THIS_MODULE,
912 .bind = llcp_raw_sock_bind,
913 .connect = sock_no_connect,
914 .release = llcp_sock_release,
915 .socketpair = sock_no_socketpair,
916 .accept = sock_no_accept,
917 .getname = llcp_sock_getname,
918 .poll = llcp_sock_poll,
919 .ioctl = sock_no_ioctl,
920 .listen = sock_no_listen,
921 .shutdown = sock_no_shutdown,
922 .setsockopt = sock_no_setsockopt,
923 .getsockopt = sock_no_getsockopt,
924 .sendmsg = sock_no_sendmsg,
925 .recvmsg = llcp_sock_recvmsg,
926 .mmap = sock_no_mmap,
929 static void llcp_sock_destruct(struct sock *sk)
931 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
933 pr_debug("%p\n", sk);
935 if (sk->sk_state == LLCP_CONNECTED)
936 nfc_put_device(llcp_sock->dev);
938 skb_queue_purge(&sk->sk_receive_queue);
940 nfc_llcp_sock_free(llcp_sock);
942 if (!sock_flag(sk, SOCK_DEAD)) {
943 pr_err("Freeing alive NFC LLCP socket %p\n", sk);
944 return;
948 struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp)
950 struct sock *sk;
951 struct nfc_llcp_sock *llcp_sock;
953 sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto);
954 if (!sk)
955 return NULL;
957 llcp_sock = nfc_llcp_sock(sk);
959 sock_init_data(sock, sk);
960 sk->sk_state = LLCP_CLOSED;
961 sk->sk_protocol = NFC_SOCKPROTO_LLCP;
962 sk->sk_type = type;
963 sk->sk_destruct = llcp_sock_destruct;
965 llcp_sock->ssap = 0;
966 llcp_sock->dsap = LLCP_SAP_SDP;
967 llcp_sock->rw = LLCP_MAX_RW + 1;
968 llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1);
969 llcp_sock->send_n = llcp_sock->send_ack_n = 0;
970 llcp_sock->recv_n = llcp_sock->recv_ack_n = 0;
971 llcp_sock->remote_ready = 1;
972 llcp_sock->reserved_ssap = LLCP_SAP_MAX;
973 nfc_llcp_socket_remote_param_init(llcp_sock);
974 skb_queue_head_init(&llcp_sock->tx_queue);
975 skb_queue_head_init(&llcp_sock->tx_pending_queue);
976 INIT_LIST_HEAD(&llcp_sock->accept_queue);
978 if (sock != NULL)
979 sock->state = SS_UNCONNECTED;
981 return sk;
984 void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
986 kfree(sock->service_name);
988 skb_queue_purge(&sock->tx_queue);
989 skb_queue_purge(&sock->tx_pending_queue);
991 list_del_init(&sock->accept_queue);
993 sock->parent = NULL;
995 nfc_llcp_local_put(sock->local);
998 static int llcp_sock_create(struct net *net, struct socket *sock,
999 const struct nfc_protocol *nfc_proto)
1001 struct sock *sk;
1003 pr_debug("%p\n", sock);
1005 if (sock->type != SOCK_STREAM &&
1006 sock->type != SOCK_DGRAM &&
1007 sock->type != SOCK_RAW)
1008 return -ESOCKTNOSUPPORT;
1010 if (sock->type == SOCK_RAW)
1011 sock->ops = &llcp_rawsock_ops;
1012 else
1013 sock->ops = &llcp_sock_ops;
1015 sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC);
1016 if (sk == NULL)
1017 return -ENOMEM;
1019 return 0;
1022 static const struct nfc_protocol llcp_nfc_proto = {
1023 .id = NFC_SOCKPROTO_LLCP,
1024 .proto = &llcp_sock_proto,
1025 .owner = THIS_MODULE,
1026 .create = llcp_sock_create
1029 int __init nfc_llcp_sock_init(void)
1031 return nfc_proto_register(&llcp_nfc_proto);
1034 void nfc_llcp_sock_exit(void)
1036 nfc_proto_unregister(&llcp_nfc_proto);