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, see <http://www.gnu.org/licenses/>.
18 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/nfc.h>
24 #include <linux/sched/signal.h>
29 static int sock_wait_state(struct sock
*sk
, int state
, unsigned long timeo
)
31 DECLARE_WAITQUEUE(wait
, current
);
34 pr_debug("sk %p", sk
);
36 add_wait_queue(sk_sleep(sk
), &wait
);
37 set_current_state(TASK_INTERRUPTIBLE
);
39 while (sk
->sk_state
!= state
) {
45 if (signal_pending(current
)) {
46 err
= sock_intr_errno(timeo
);
51 timeo
= schedule_timeout(timeo
);
53 set_current_state(TASK_INTERRUPTIBLE
);
60 __set_current_state(TASK_RUNNING
);
61 remove_wait_queue(sk_sleep(sk
), &wait
);
65 static struct proto llcp_sock_proto
= {
68 .obj_size
= sizeof(struct nfc_llcp_sock
),
71 static int llcp_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int alen
)
73 struct sock
*sk
= sock
->sk
;
74 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
75 struct nfc_llcp_local
*local
;
77 struct sockaddr_nfc_llcp llcp_addr
;
80 if (!addr
|| alen
< offsetofend(struct sockaddr
, sa_family
) ||
81 addr
->sa_family
!= AF_NFC
)
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)
96 if (sk
->sk_state
!= LLCP_CLOSED
) {
101 dev
= nfc_get_device(llcp_addr
.dev_idx
);
107 local
= nfc_llcp_find_local(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
,
123 llcp_sock
->ssap
= nfc_llcp_get_sdp_ssap(local
, llcp_sock
);
124 if (llcp_sock
->ssap
== LLCP_SAP_MAX
) {
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
;
145 static int llcp_raw_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
148 struct sock
*sk
= sock
->sk
;
149 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
150 struct nfc_llcp_local
*local
;
152 struct sockaddr_nfc_llcp llcp_addr
;
155 if (!addr
|| alen
< offsetofend(struct sockaddr
, sa_family
) ||
156 addr
->sa_family
!= AF_NFC
)
159 pr_debug("sk %p addr %p family %d\n", sk
, addr
, addr
->sa_family
);
161 memset(&llcp_addr
, 0, sizeof(llcp_addr
));
162 len
= min_t(unsigned int, sizeof(llcp_addr
), alen
);
163 memcpy(&llcp_addr
, addr
, len
);
167 if (sk
->sk_state
!= LLCP_CLOSED
) {
172 dev
= nfc_get_device(llcp_addr
.dev_idx
);
178 local
= nfc_llcp_find_local(dev
);
184 llcp_sock
->dev
= dev
;
185 llcp_sock
->local
= nfc_llcp_local_get(local
);
186 llcp_sock
->nfc_protocol
= llcp_addr
.nfc_protocol
;
188 nfc_llcp_sock_link(&local
->raw_sockets
, sk
);
190 sk
->sk_state
= LLCP_BOUND
;
200 static int llcp_sock_listen(struct socket
*sock
, int backlog
)
202 struct sock
*sk
= sock
->sk
;
205 pr_debug("sk %p backlog %d\n", sk
, backlog
);
209 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
) ||
210 sk
->sk_state
!= LLCP_BOUND
) {
215 sk
->sk_max_ack_backlog
= backlog
;
216 sk
->sk_ack_backlog
= 0;
218 pr_debug("Socket listening\n");
219 sk
->sk_state
= LLCP_LISTEN
;
227 static int nfc_llcp_setsockopt(struct socket
*sock
, int level
, int optname
,
228 char __user
*optval
, unsigned int optlen
)
230 struct sock
*sk
= sock
->sk
;
231 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
235 pr_debug("%p optname %d\n", sk
, optname
);
237 if (level
!= SOL_NFC
)
244 if (sk
->sk_state
== LLCP_CONNECTED
||
245 sk
->sk_state
== LLCP_BOUND
||
246 sk
->sk_state
== LLCP_LISTEN
) {
251 if (get_user(opt
, (u32 __user
*) optval
)) {
256 if (opt
> LLCP_MAX_RW
) {
261 llcp_sock
->rw
= (u8
) opt
;
266 if (sk
->sk_state
== LLCP_CONNECTED
||
267 sk
->sk_state
== LLCP_BOUND
||
268 sk
->sk_state
== LLCP_LISTEN
) {
273 if (get_user(opt
, (u32 __user
*) optval
)) {
278 if (opt
> LLCP_MAX_MIUX
) {
283 llcp_sock
->miux
= cpu_to_be16((u16
) opt
);
294 pr_debug("%p rw %d miux %d\n", llcp_sock
,
295 llcp_sock
->rw
, llcp_sock
->miux
);
300 static int nfc_llcp_getsockopt(struct socket
*sock
, int level
, int optname
,
301 char __user
*optval
, int __user
*optlen
)
303 struct nfc_llcp_local
*local
;
304 struct sock
*sk
= sock
->sk
;
305 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
307 u16 miux
, remote_miu
;
310 pr_debug("%p optname %d\n", sk
, optname
);
312 if (level
!= SOL_NFC
)
315 if (get_user(len
, optlen
))
318 local
= llcp_sock
->local
;
322 len
= min_t(u32
, len
, sizeof(u32
));
328 rw
= llcp_sock
->rw
> LLCP_MAX_RW
? local
->rw
: llcp_sock
->rw
;
329 if (put_user(rw
, (u32 __user
*) optval
))
335 miux
= be16_to_cpu(llcp_sock
->miux
) > LLCP_MAX_MIUX
?
336 be16_to_cpu(local
->miux
) : be16_to_cpu(llcp_sock
->miux
);
338 if (put_user(miux
, (u32 __user
*) optval
))
343 case NFC_LLCP_REMOTE_MIU
:
344 remote_miu
= llcp_sock
->remote_miu
> LLCP_MAX_MIU
?
345 local
->remote_miu
: llcp_sock
->remote_miu
;
347 if (put_user(remote_miu
, (u32 __user
*) optval
))
352 case NFC_LLCP_REMOTE_LTO
:
353 if (put_user(local
->remote_lto
/ 10, (u32 __user
*) optval
))
358 case NFC_LLCP_REMOTE_RW
:
359 if (put_user(llcp_sock
->remote_rw
, (u32 __user
*) optval
))
371 if (put_user(len
, optlen
))
377 void nfc_llcp_accept_unlink(struct sock
*sk
)
379 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
381 pr_debug("state %d\n", sk
->sk_state
);
383 list_del_init(&llcp_sock
->accept_queue
);
384 sk_acceptq_removed(llcp_sock
->parent
);
385 llcp_sock
->parent
= NULL
;
390 void nfc_llcp_accept_enqueue(struct sock
*parent
, struct sock
*sk
)
392 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
393 struct nfc_llcp_sock
*llcp_sock_parent
= nfc_llcp_sock(parent
);
395 /* Lock will be free from unlink */
398 list_add_tail(&llcp_sock
->accept_queue
,
399 &llcp_sock_parent
->accept_queue
);
400 llcp_sock
->parent
= parent
;
401 sk_acceptq_added(parent
);
404 struct sock
*nfc_llcp_accept_dequeue(struct sock
*parent
,
405 struct socket
*newsock
)
407 struct nfc_llcp_sock
*lsk
, *n
, *llcp_parent
;
410 llcp_parent
= nfc_llcp_sock(parent
);
412 list_for_each_entry_safe(lsk
, n
, &llcp_parent
->accept_queue
,
417 if (sk
->sk_state
== LLCP_CLOSED
) {
419 nfc_llcp_accept_unlink(sk
);
423 if (sk
->sk_state
== LLCP_CONNECTED
|| !newsock
) {
424 list_del_init(&lsk
->accept_queue
);
428 sock_graft(sk
, newsock
);
432 pr_debug("Returning sk state %d\n", sk
->sk_state
);
434 sk_acceptq_removed(parent
);
445 static int llcp_sock_accept(struct socket
*sock
, struct socket
*newsock
,
446 int flags
, bool kern
)
448 DECLARE_WAITQUEUE(wait
, current
);
449 struct sock
*sk
= sock
->sk
, *new_sk
;
453 pr_debug("parent %p\n", sk
);
455 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
457 if (sk
->sk_state
!= LLCP_LISTEN
) {
462 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
464 /* Wait for an incoming connection. */
465 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
466 while (!(new_sk
= nfc_llcp_accept_dequeue(sk
, newsock
))) {
467 set_current_state(TASK_INTERRUPTIBLE
);
474 if (signal_pending(current
)) {
475 ret
= sock_intr_errno(timeo
);
480 timeo
= schedule_timeout(timeo
);
481 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
483 __set_current_state(TASK_RUNNING
);
484 remove_wait_queue(sk_sleep(sk
), &wait
);
489 newsock
->state
= SS_CONNECTED
;
491 pr_debug("new socket %p\n", new_sk
);
499 static int llcp_sock_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
502 struct sock
*sk
= sock
->sk
;
503 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
504 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, llcp_addr
, uaddr
);
506 if (llcp_sock
== NULL
|| llcp_sock
->dev
== NULL
)
509 pr_debug("%p %d %d %d\n", sk
, llcp_sock
->target_idx
,
510 llcp_sock
->dsap
, llcp_sock
->ssap
);
512 memset(llcp_addr
, 0, sizeof(*llcp_addr
));
513 *len
= sizeof(struct sockaddr_nfc_llcp
);
516 if (!llcp_sock
->dev
) {
520 llcp_addr
->sa_family
= AF_NFC
;
521 llcp_addr
->dev_idx
= llcp_sock
->dev
->idx
;
522 llcp_addr
->target_idx
= llcp_sock
->target_idx
;
523 llcp_addr
->nfc_protocol
= llcp_sock
->nfc_protocol
;
524 llcp_addr
->dsap
= llcp_sock
->dsap
;
525 llcp_addr
->ssap
= llcp_sock
->ssap
;
526 llcp_addr
->service_name_len
= llcp_sock
->service_name_len
;
527 memcpy(llcp_addr
->service_name
, llcp_sock
->service_name
,
528 llcp_addr
->service_name_len
);
534 static inline unsigned int llcp_accept_poll(struct sock
*parent
)
536 struct nfc_llcp_sock
*llcp_sock
, *parent_sock
;
539 parent_sock
= nfc_llcp_sock(parent
);
541 list_for_each_entry(llcp_sock
, &parent_sock
->accept_queue
,
545 if (sk
->sk_state
== LLCP_CONNECTED
)
546 return POLLIN
| POLLRDNORM
;
552 static unsigned int llcp_sock_poll(struct file
*file
, struct socket
*sock
,
555 struct sock
*sk
= sock
->sk
;
556 unsigned int mask
= 0;
558 pr_debug("%p\n", sk
);
560 sock_poll_wait(file
, sk_sleep(sk
), wait
);
562 if (sk
->sk_state
== LLCP_LISTEN
)
563 return llcp_accept_poll(sk
);
565 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
567 (sock_flag(sk
, SOCK_SELECT_ERR_QUEUE
) ? POLLPRI
: 0);
569 if (!skb_queue_empty(&sk
->sk_receive_queue
))
570 mask
|= POLLIN
| POLLRDNORM
;
572 if (sk
->sk_state
== LLCP_CLOSED
)
575 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
576 mask
|= POLLRDHUP
| POLLIN
| POLLRDNORM
;
578 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
581 if (sock_writeable(sk
) && sk
->sk_state
== LLCP_CONNECTED
)
582 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
584 sk_set_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
586 pr_debug("mask 0x%x\n", mask
);
591 static int llcp_sock_release(struct socket
*sock
)
593 struct sock
*sk
= sock
->sk
;
594 struct nfc_llcp_local
*local
;
595 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
601 pr_debug("%p\n", sk
);
603 local
= llcp_sock
->local
;
612 if (sk
->sk_state
== LLCP_CONNECTED
)
613 nfc_llcp_send_disconnect(llcp_sock
);
615 if (sk
->sk_state
== LLCP_LISTEN
) {
616 struct nfc_llcp_sock
*lsk
, *n
;
617 struct sock
*accept_sk
;
619 list_for_each_entry_safe(lsk
, n
, &llcp_sock
->accept_queue
,
621 accept_sk
= &lsk
->sk
;
622 lock_sock(accept_sk
);
624 nfc_llcp_send_disconnect(lsk
);
625 nfc_llcp_accept_unlink(accept_sk
);
627 release_sock(accept_sk
);
631 if (llcp_sock
->reserved_ssap
< LLCP_SAP_MAX
)
632 nfc_llcp_put_ssap(llcp_sock
->local
, llcp_sock
->ssap
);
636 /* Keep this sock alive and therefore do not remove it from the sockets
637 * list until the DISC PDU has been actually sent. Otherwise we would
638 * reply with DM PDUs before sending the DISC one.
640 if (sk
->sk_state
== LLCP_DISCONNECTING
)
643 if (sock
->type
== SOCK_RAW
)
644 nfc_llcp_sock_unlink(&local
->raw_sockets
, sk
);
646 nfc_llcp_sock_unlink(&local
->sockets
, sk
);
655 static int llcp_sock_connect(struct socket
*sock
, struct sockaddr
*_addr
,
658 struct sock
*sk
= sock
->sk
;
659 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
660 struct sockaddr_nfc_llcp
*addr
= (struct sockaddr_nfc_llcp
*)_addr
;
662 struct nfc_llcp_local
*local
;
665 pr_debug("sock %p sk %p flags 0x%x\n", sock
, sk
, flags
);
667 if (!addr
|| len
< sizeof(*addr
) || addr
->sa_family
!= AF_NFC
)
670 if (addr
->service_name_len
== 0 && addr
->dsap
== 0)
673 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr
->dev_idx
,
674 addr
->target_idx
, addr
->nfc_protocol
);
678 if (sk
->sk_state
== LLCP_CONNECTED
) {
683 dev
= nfc_get_device(addr
->dev_idx
);
689 local
= nfc_llcp_find_local(dev
);
695 device_lock(&dev
->dev
);
696 if (dev
->dep_link_up
== false) {
698 device_unlock(&dev
->dev
);
701 device_unlock(&dev
->dev
);
703 if (local
->rf_mode
== NFC_RF_INITIATOR
&&
704 addr
->target_idx
!= local
->target_idx
) {
709 llcp_sock
->dev
= dev
;
710 llcp_sock
->local
= nfc_llcp_local_get(local
);
711 llcp_sock
->ssap
= nfc_llcp_get_local_ssap(local
);
712 if (llcp_sock
->ssap
== LLCP_SAP_MAX
) {
717 llcp_sock
->reserved_ssap
= llcp_sock
->ssap
;
719 if (addr
->service_name_len
== 0)
720 llcp_sock
->dsap
= addr
->dsap
;
722 llcp_sock
->dsap
= LLCP_SAP_SDP
;
723 llcp_sock
->nfc_protocol
= addr
->nfc_protocol
;
724 llcp_sock
->service_name_len
= min_t(unsigned int,
725 addr
->service_name_len
,
726 NFC_LLCP_MAX_SERVICE_NAME
);
727 llcp_sock
->service_name
= kmemdup(addr
->service_name
,
728 llcp_sock
->service_name_len
,
731 nfc_llcp_sock_link(&local
->connecting_sockets
, sk
);
733 ret
= nfc_llcp_send_connect(llcp_sock
);
737 sk
->sk_state
= LLCP_CONNECTING
;
739 ret
= sock_wait_state(sk
, LLCP_CONNECTED
,
740 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
741 if (ret
&& ret
!= -EINPROGRESS
)
749 nfc_llcp_put_ssap(local
, llcp_sock
->ssap
);
751 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
761 static int llcp_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
764 struct sock
*sk
= sock
->sk
;
765 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
768 pr_debug("sock %p sk %p", sock
, sk
);
770 ret
= sock_error(sk
);
774 if (msg
->msg_flags
& MSG_OOB
)
779 if (sk
->sk_type
== SOCK_DGRAM
) {
780 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, addr
,
783 if (msg
->msg_namelen
< sizeof(*addr
)) {
790 return nfc_llcp_send_ui_frame(llcp_sock
, addr
->dsap
, addr
->ssap
,
794 if (sk
->sk_state
!= LLCP_CONNECTED
) {
801 return nfc_llcp_send_i_frame(llcp_sock
, msg
, len
);
804 static int llcp_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
805 size_t len
, int flags
)
807 int noblock
= flags
& MSG_DONTWAIT
;
808 struct sock
*sk
= sock
->sk
;
809 unsigned int copied
, rlen
;
810 struct sk_buff
*skb
, *cskb
;
813 pr_debug("%p %zu\n", sk
, len
);
817 if (sk
->sk_state
== LLCP_CLOSED
&&
818 skb_queue_empty(&sk
->sk_receive_queue
)) {
825 if (flags
& (MSG_OOB
))
828 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
830 pr_err("Recv datagram failed state %d %d %d",
831 sk
->sk_state
, err
, sock_error(sk
));
833 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
839 rlen
= skb
->len
; /* real length of skb */
840 copied
= min_t(unsigned int, rlen
, len
);
843 if (skb_copy_datagram_msg(cskb
, 0, msg
, copied
)) {
844 if (!(flags
& MSG_PEEK
))
845 skb_queue_head(&sk
->sk_receive_queue
, skb
);
849 sock_recv_timestamp(msg
, sk
, skb
);
851 if (sk
->sk_type
== SOCK_DGRAM
&& msg
->msg_name
) {
852 struct nfc_llcp_ui_cb
*ui_cb
= nfc_llcp_ui_skb_cb(skb
);
853 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, sockaddr
,
856 msg
->msg_namelen
= sizeof(struct sockaddr_nfc_llcp
);
858 pr_debug("Datagram socket %d %d\n", ui_cb
->dsap
, ui_cb
->ssap
);
860 memset(sockaddr
, 0, sizeof(*sockaddr
));
861 sockaddr
->sa_family
= AF_NFC
;
862 sockaddr
->nfc_protocol
= NFC_PROTO_NFC_DEP
;
863 sockaddr
->dsap
= ui_cb
->dsap
;
864 sockaddr
->ssap
= ui_cb
->ssap
;
867 /* Mark read part of skb as used */
868 if (!(flags
& MSG_PEEK
)) {
870 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
871 if (sk
->sk_type
== SOCK_STREAM
||
872 sk
->sk_type
== SOCK_DGRAM
||
873 sk
->sk_type
== SOCK_RAW
) {
874 skb_pull(skb
, copied
);
876 skb_queue_head(&sk
->sk_receive_queue
, skb
);
884 /* XXX Queue backlogged skbs */
887 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
888 if (sk
->sk_type
== SOCK_SEQPACKET
&& (flags
& MSG_TRUNC
))
894 static const struct proto_ops llcp_sock_ops
= {
896 .owner
= THIS_MODULE
,
897 .bind
= llcp_sock_bind
,
898 .connect
= llcp_sock_connect
,
899 .release
= llcp_sock_release
,
900 .socketpair
= sock_no_socketpair
,
901 .accept
= llcp_sock_accept
,
902 .getname
= llcp_sock_getname
,
903 .poll
= llcp_sock_poll
,
904 .ioctl
= sock_no_ioctl
,
905 .listen
= llcp_sock_listen
,
906 .shutdown
= sock_no_shutdown
,
907 .setsockopt
= nfc_llcp_setsockopt
,
908 .getsockopt
= nfc_llcp_getsockopt
,
909 .sendmsg
= llcp_sock_sendmsg
,
910 .recvmsg
= llcp_sock_recvmsg
,
911 .mmap
= sock_no_mmap
,
914 static const struct proto_ops llcp_rawsock_ops
= {
916 .owner
= THIS_MODULE
,
917 .bind
= llcp_raw_sock_bind
,
918 .connect
= sock_no_connect
,
919 .release
= llcp_sock_release
,
920 .socketpair
= sock_no_socketpair
,
921 .accept
= sock_no_accept
,
922 .getname
= llcp_sock_getname
,
923 .poll
= llcp_sock_poll
,
924 .ioctl
= sock_no_ioctl
,
925 .listen
= sock_no_listen
,
926 .shutdown
= sock_no_shutdown
,
927 .setsockopt
= sock_no_setsockopt
,
928 .getsockopt
= sock_no_getsockopt
,
929 .sendmsg
= sock_no_sendmsg
,
930 .recvmsg
= llcp_sock_recvmsg
,
931 .mmap
= sock_no_mmap
,
934 static void llcp_sock_destruct(struct sock
*sk
)
936 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
938 pr_debug("%p\n", sk
);
940 if (sk
->sk_state
== LLCP_CONNECTED
)
941 nfc_put_device(llcp_sock
->dev
);
943 skb_queue_purge(&sk
->sk_receive_queue
);
945 nfc_llcp_sock_free(llcp_sock
);
947 if (!sock_flag(sk
, SOCK_DEAD
)) {
948 pr_err("Freeing alive NFC LLCP socket %p\n", sk
);
953 struct sock
*nfc_llcp_sock_alloc(struct socket
*sock
, int type
, gfp_t gfp
, int kern
)
956 struct nfc_llcp_sock
*llcp_sock
;
958 sk
= sk_alloc(&init_net
, PF_NFC
, gfp
, &llcp_sock_proto
, kern
);
962 llcp_sock
= nfc_llcp_sock(sk
);
964 sock_init_data(sock
, sk
);
965 sk
->sk_state
= LLCP_CLOSED
;
966 sk
->sk_protocol
= NFC_SOCKPROTO_LLCP
;
968 sk
->sk_destruct
= llcp_sock_destruct
;
971 llcp_sock
->dsap
= LLCP_SAP_SDP
;
972 llcp_sock
->rw
= LLCP_MAX_RW
+ 1;
973 llcp_sock
->miux
= cpu_to_be16(LLCP_MAX_MIUX
+ 1);
974 llcp_sock
->send_n
= llcp_sock
->send_ack_n
= 0;
975 llcp_sock
->recv_n
= llcp_sock
->recv_ack_n
= 0;
976 llcp_sock
->remote_ready
= 1;
977 llcp_sock
->reserved_ssap
= LLCP_SAP_MAX
;
978 nfc_llcp_socket_remote_param_init(llcp_sock
);
979 skb_queue_head_init(&llcp_sock
->tx_queue
);
980 skb_queue_head_init(&llcp_sock
->tx_pending_queue
);
981 INIT_LIST_HEAD(&llcp_sock
->accept_queue
);
984 sock
->state
= SS_UNCONNECTED
;
989 void nfc_llcp_sock_free(struct nfc_llcp_sock
*sock
)
991 kfree(sock
->service_name
);
993 skb_queue_purge(&sock
->tx_queue
);
994 skb_queue_purge(&sock
->tx_pending_queue
);
996 list_del_init(&sock
->accept_queue
);
1000 nfc_llcp_local_put(sock
->local
);
1003 static int llcp_sock_create(struct net
*net
, struct socket
*sock
,
1004 const struct nfc_protocol
*nfc_proto
, int kern
)
1008 pr_debug("%p\n", sock
);
1010 if (sock
->type
!= SOCK_STREAM
&&
1011 sock
->type
!= SOCK_DGRAM
&&
1012 sock
->type
!= SOCK_RAW
)
1013 return -ESOCKTNOSUPPORT
;
1015 if (sock
->type
== SOCK_RAW
)
1016 sock
->ops
= &llcp_rawsock_ops
;
1018 sock
->ops
= &llcp_sock_ops
;
1020 sk
= nfc_llcp_sock_alloc(sock
, sock
->type
, GFP_ATOMIC
, kern
);
1027 static const struct nfc_protocol llcp_nfc_proto
= {
1028 .id
= NFC_SOCKPROTO_LLCP
,
1029 .proto
= &llcp_sock_proto
,
1030 .owner
= THIS_MODULE
,
1031 .create
= llcp_sock_create
1034 int __init
nfc_llcp_sock_init(void)
1036 return nfc_proto_register(&llcp_nfc_proto
);
1039 void nfc_llcp_sock_exit(void)
1041 nfc_proto_unregister(&llcp_nfc_proto
);