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>
28 static int sock_wait_state(struct sock
*sk
, int state
, unsigned long timeo
)
30 DECLARE_WAITQUEUE(wait
, current
);
33 pr_debug("sk %p", sk
);
35 add_wait_queue(sk_sleep(sk
), &wait
);
36 set_current_state(TASK_INTERRUPTIBLE
);
38 while (sk
->sk_state
!= state
) {
44 if (signal_pending(current
)) {
45 err
= sock_intr_errno(timeo
);
50 timeo
= schedule_timeout(timeo
);
52 set_current_state(TASK_INTERRUPTIBLE
);
59 __set_current_state(TASK_RUNNING
);
60 remove_wait_queue(sk_sleep(sk
), &wait
);
64 static struct proto llcp_sock_proto
= {
67 .obj_size
= sizeof(struct nfc_llcp_sock
),
70 static int llcp_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int alen
)
72 struct sock
*sk
= sock
->sk
;
73 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
74 struct nfc_llcp_local
*local
;
76 struct sockaddr_nfc_llcp llcp_addr
;
79 if (!addr
|| addr
->sa_family
!= AF_NFC
)
82 pr_debug("sk %p addr %p family %d\n", sk
, addr
, addr
->sa_family
);
84 memset(&llcp_addr
, 0, sizeof(llcp_addr
));
85 len
= min_t(unsigned int, sizeof(llcp_addr
), alen
);
86 memcpy(&llcp_addr
, addr
, len
);
88 /* This is going to be a listening socket, dsap must be 0 */
89 if (llcp_addr
.dsap
!= 0)
94 if (sk
->sk_state
!= LLCP_CLOSED
) {
99 dev
= nfc_get_device(llcp_addr
.dev_idx
);
105 local
= nfc_llcp_find_local(dev
);
111 llcp_sock
->dev
= dev
;
112 llcp_sock
->local
= nfc_llcp_local_get(local
);
113 llcp_sock
->nfc_protocol
= llcp_addr
.nfc_protocol
;
114 llcp_sock
->service_name_len
= min_t(unsigned int,
115 llcp_addr
.service_name_len
,
116 NFC_LLCP_MAX_SERVICE_NAME
);
117 llcp_sock
->service_name
= kmemdup(llcp_addr
.service_name
,
118 llcp_sock
->service_name_len
,
121 llcp_sock
->ssap
= nfc_llcp_get_sdp_ssap(local
, llcp_sock
);
122 if (llcp_sock
->ssap
== LLCP_SAP_MAX
) {
127 llcp_sock
->reserved_ssap
= llcp_sock
->ssap
;
129 nfc_llcp_sock_link(&local
->sockets
, sk
);
131 pr_debug("Socket bound to SAP %d\n", llcp_sock
->ssap
);
133 sk
->sk_state
= LLCP_BOUND
;
143 static int llcp_raw_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
146 struct sock
*sk
= sock
->sk
;
147 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
148 struct nfc_llcp_local
*local
;
150 struct sockaddr_nfc_llcp llcp_addr
;
153 if (!addr
|| addr
->sa_family
!= AF_NFC
)
156 pr_debug("sk %p addr %p family %d\n", sk
, addr
, addr
->sa_family
);
158 memset(&llcp_addr
, 0, sizeof(llcp_addr
));
159 len
= min_t(unsigned int, sizeof(llcp_addr
), alen
);
160 memcpy(&llcp_addr
, addr
, len
);
164 if (sk
->sk_state
!= LLCP_CLOSED
) {
169 dev
= nfc_get_device(llcp_addr
.dev_idx
);
175 local
= nfc_llcp_find_local(dev
);
181 llcp_sock
->dev
= dev
;
182 llcp_sock
->local
= nfc_llcp_local_get(local
);
183 llcp_sock
->nfc_protocol
= llcp_addr
.nfc_protocol
;
185 nfc_llcp_sock_link(&local
->raw_sockets
, sk
);
187 sk
->sk_state
= LLCP_BOUND
;
197 static int llcp_sock_listen(struct socket
*sock
, int backlog
)
199 struct sock
*sk
= sock
->sk
;
202 pr_debug("sk %p backlog %d\n", sk
, backlog
);
206 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
) ||
207 sk
->sk_state
!= LLCP_BOUND
) {
212 sk
->sk_max_ack_backlog
= backlog
;
213 sk
->sk_ack_backlog
= 0;
215 pr_debug("Socket listening\n");
216 sk
->sk_state
= LLCP_LISTEN
;
224 static int nfc_llcp_setsockopt(struct socket
*sock
, int level
, int optname
,
225 char __user
*optval
, unsigned int optlen
)
227 struct sock
*sk
= sock
->sk
;
228 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
232 pr_debug("%p optname %d\n", sk
, optname
);
234 if (level
!= SOL_NFC
)
241 if (sk
->sk_state
== LLCP_CONNECTED
||
242 sk
->sk_state
== LLCP_BOUND
||
243 sk
->sk_state
== LLCP_LISTEN
) {
248 if (get_user(opt
, (u32 __user
*) optval
)) {
253 if (opt
> LLCP_MAX_RW
) {
258 llcp_sock
->rw
= (u8
) opt
;
263 if (sk
->sk_state
== LLCP_CONNECTED
||
264 sk
->sk_state
== LLCP_BOUND
||
265 sk
->sk_state
== LLCP_LISTEN
) {
270 if (get_user(opt
, (u32 __user
*) optval
)) {
275 if (opt
> LLCP_MAX_MIUX
) {
280 llcp_sock
->miux
= cpu_to_be16((u16
) opt
);
291 pr_debug("%p rw %d miux %d\n", llcp_sock
,
292 llcp_sock
->rw
, llcp_sock
->miux
);
297 static int nfc_llcp_getsockopt(struct socket
*sock
, int level
, int optname
,
298 char __user
*optval
, int __user
*optlen
)
300 struct nfc_llcp_local
*local
;
301 struct sock
*sk
= sock
->sk
;
302 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
304 u16 miux
, remote_miu
;
307 pr_debug("%p optname %d\n", sk
, optname
);
309 if (level
!= SOL_NFC
)
312 if (get_user(len
, optlen
))
315 local
= llcp_sock
->local
;
319 len
= min_t(u32
, len
, sizeof(u32
));
325 rw
= llcp_sock
->rw
> LLCP_MAX_RW
? local
->rw
: llcp_sock
->rw
;
326 if (put_user(rw
, (u32 __user
*) optval
))
332 miux
= be16_to_cpu(llcp_sock
->miux
) > LLCP_MAX_MIUX
?
333 be16_to_cpu(local
->miux
) : be16_to_cpu(llcp_sock
->miux
);
335 if (put_user(miux
, (u32 __user
*) optval
))
340 case NFC_LLCP_REMOTE_MIU
:
341 remote_miu
= llcp_sock
->remote_miu
> LLCP_MAX_MIU
?
342 local
->remote_miu
: llcp_sock
->remote_miu
;
344 if (put_user(remote_miu
, (u32 __user
*) optval
))
349 case NFC_LLCP_REMOTE_LTO
:
350 if (put_user(local
->remote_lto
/ 10, (u32 __user
*) optval
))
355 case NFC_LLCP_REMOTE_RW
:
356 if (put_user(llcp_sock
->remote_rw
, (u32 __user
*) optval
))
368 if (put_user(len
, optlen
))
374 void nfc_llcp_accept_unlink(struct sock
*sk
)
376 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
378 pr_debug("state %d\n", sk
->sk_state
);
380 list_del_init(&llcp_sock
->accept_queue
);
381 sk_acceptq_removed(llcp_sock
->parent
);
382 llcp_sock
->parent
= NULL
;
387 void nfc_llcp_accept_enqueue(struct sock
*parent
, struct sock
*sk
)
389 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
390 struct nfc_llcp_sock
*llcp_sock_parent
= nfc_llcp_sock(parent
);
392 /* Lock will be free from unlink */
395 list_add_tail(&llcp_sock
->accept_queue
,
396 &llcp_sock_parent
->accept_queue
);
397 llcp_sock
->parent
= parent
;
398 sk_acceptq_added(parent
);
401 struct sock
*nfc_llcp_accept_dequeue(struct sock
*parent
,
402 struct socket
*newsock
)
404 struct nfc_llcp_sock
*lsk
, *n
, *llcp_parent
;
407 llcp_parent
= nfc_llcp_sock(parent
);
409 list_for_each_entry_safe(lsk
, n
, &llcp_parent
->accept_queue
,
414 if (sk
->sk_state
== LLCP_CLOSED
) {
416 nfc_llcp_accept_unlink(sk
);
420 if (sk
->sk_state
== LLCP_CONNECTED
|| !newsock
) {
421 list_del_init(&lsk
->accept_queue
);
425 sock_graft(sk
, newsock
);
429 pr_debug("Returning sk state %d\n", sk
->sk_state
);
431 sk_acceptq_removed(parent
);
442 static int llcp_sock_accept(struct socket
*sock
, struct socket
*newsock
,
445 DECLARE_WAITQUEUE(wait
, current
);
446 struct sock
*sk
= sock
->sk
, *new_sk
;
450 pr_debug("parent %p\n", sk
);
452 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
454 if (sk
->sk_state
!= LLCP_LISTEN
) {
459 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
461 /* Wait for an incoming connection. */
462 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
463 while (!(new_sk
= nfc_llcp_accept_dequeue(sk
, newsock
))) {
464 set_current_state(TASK_INTERRUPTIBLE
);
471 if (signal_pending(current
)) {
472 ret
= sock_intr_errno(timeo
);
477 timeo
= schedule_timeout(timeo
);
478 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
480 __set_current_state(TASK_RUNNING
);
481 remove_wait_queue(sk_sleep(sk
), &wait
);
486 newsock
->state
= SS_CONNECTED
;
488 pr_debug("new socket %p\n", new_sk
);
496 static int llcp_sock_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
499 struct sock
*sk
= sock
->sk
;
500 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
501 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, llcp_addr
, uaddr
);
503 if (llcp_sock
== NULL
|| llcp_sock
->dev
== NULL
)
506 pr_debug("%p %d %d %d\n", sk
, llcp_sock
->target_idx
,
507 llcp_sock
->dsap
, llcp_sock
->ssap
);
509 memset(llcp_addr
, 0, sizeof(*llcp_addr
));
510 *len
= sizeof(struct sockaddr_nfc_llcp
);
513 if (!llcp_sock
->dev
) {
517 llcp_addr
->sa_family
= AF_NFC
;
518 llcp_addr
->dev_idx
= llcp_sock
->dev
->idx
;
519 llcp_addr
->target_idx
= llcp_sock
->target_idx
;
520 llcp_addr
->nfc_protocol
= llcp_sock
->nfc_protocol
;
521 llcp_addr
->dsap
= llcp_sock
->dsap
;
522 llcp_addr
->ssap
= llcp_sock
->ssap
;
523 llcp_addr
->service_name_len
= llcp_sock
->service_name_len
;
524 memcpy(llcp_addr
->service_name
, llcp_sock
->service_name
,
525 llcp_addr
->service_name_len
);
531 static inline unsigned int llcp_accept_poll(struct sock
*parent
)
533 struct nfc_llcp_sock
*llcp_sock
, *parent_sock
;
536 parent_sock
= nfc_llcp_sock(parent
);
538 list_for_each_entry(llcp_sock
, &parent_sock
->accept_queue
,
542 if (sk
->sk_state
== LLCP_CONNECTED
)
543 return POLLIN
| POLLRDNORM
;
549 static unsigned int llcp_sock_poll(struct file
*file
, struct socket
*sock
,
552 struct sock
*sk
= sock
->sk
;
553 unsigned int mask
= 0;
555 pr_debug("%p\n", sk
);
557 sock_poll_wait(file
, sk_sleep(sk
), wait
);
559 if (sk
->sk_state
== LLCP_LISTEN
)
560 return llcp_accept_poll(sk
);
562 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
564 (sock_flag(sk
, SOCK_SELECT_ERR_QUEUE
) ? POLLPRI
: 0);
566 if (!skb_queue_empty(&sk
->sk_receive_queue
))
567 mask
|= POLLIN
| POLLRDNORM
;
569 if (sk
->sk_state
== LLCP_CLOSED
)
572 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
573 mask
|= POLLRDHUP
| POLLIN
| POLLRDNORM
;
575 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
578 if (sock_writeable(sk
) && sk
->sk_state
== LLCP_CONNECTED
)
579 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
581 sk_set_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
583 pr_debug("mask 0x%x\n", mask
);
588 static int llcp_sock_release(struct socket
*sock
)
590 struct sock
*sk
= sock
->sk
;
591 struct nfc_llcp_local
*local
;
592 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
598 pr_debug("%p\n", sk
);
600 local
= llcp_sock
->local
;
609 if (sk
->sk_state
== LLCP_CONNECTED
)
610 nfc_llcp_send_disconnect(llcp_sock
);
612 if (sk
->sk_state
== LLCP_LISTEN
) {
613 struct nfc_llcp_sock
*lsk
, *n
;
614 struct sock
*accept_sk
;
616 list_for_each_entry_safe(lsk
, n
, &llcp_sock
->accept_queue
,
618 accept_sk
= &lsk
->sk
;
619 lock_sock(accept_sk
);
621 nfc_llcp_send_disconnect(lsk
);
622 nfc_llcp_accept_unlink(accept_sk
);
624 release_sock(accept_sk
);
628 if (llcp_sock
->reserved_ssap
< LLCP_SAP_MAX
)
629 nfc_llcp_put_ssap(llcp_sock
->local
, llcp_sock
->ssap
);
633 /* Keep this sock alive and therefore do not remove it from the sockets
634 * list until the DISC PDU has been actually sent. Otherwise we would
635 * reply with DM PDUs before sending the DISC one.
637 if (sk
->sk_state
== LLCP_DISCONNECTING
)
640 if (sock
->type
== SOCK_RAW
)
641 nfc_llcp_sock_unlink(&local
->raw_sockets
, sk
);
643 nfc_llcp_sock_unlink(&local
->sockets
, sk
);
652 static int llcp_sock_connect(struct socket
*sock
, struct sockaddr
*_addr
,
655 struct sock
*sk
= sock
->sk
;
656 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
657 struct sockaddr_nfc_llcp
*addr
= (struct sockaddr_nfc_llcp
*)_addr
;
659 struct nfc_llcp_local
*local
;
662 pr_debug("sock %p sk %p flags 0x%x\n", sock
, sk
, flags
);
664 if (!addr
|| len
< sizeof(struct sockaddr_nfc
) ||
665 addr
->sa_family
!= AF_NFC
)
668 if (addr
->service_name_len
== 0 && addr
->dsap
== 0)
671 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr
->dev_idx
,
672 addr
->target_idx
, addr
->nfc_protocol
);
676 if (sk
->sk_state
== LLCP_CONNECTED
) {
681 dev
= nfc_get_device(addr
->dev_idx
);
687 local
= nfc_llcp_find_local(dev
);
693 device_lock(&dev
->dev
);
694 if (dev
->dep_link_up
== false) {
696 device_unlock(&dev
->dev
);
699 device_unlock(&dev
->dev
);
701 if (local
->rf_mode
== NFC_RF_INITIATOR
&&
702 addr
->target_idx
!= local
->target_idx
) {
707 llcp_sock
->dev
= dev
;
708 llcp_sock
->local
= nfc_llcp_local_get(local
);
709 llcp_sock
->ssap
= nfc_llcp_get_local_ssap(local
);
710 if (llcp_sock
->ssap
== LLCP_SAP_MAX
) {
715 llcp_sock
->reserved_ssap
= llcp_sock
->ssap
;
717 if (addr
->service_name_len
== 0)
718 llcp_sock
->dsap
= addr
->dsap
;
720 llcp_sock
->dsap
= LLCP_SAP_SDP
;
721 llcp_sock
->nfc_protocol
= addr
->nfc_protocol
;
722 llcp_sock
->service_name_len
= min_t(unsigned int,
723 addr
->service_name_len
,
724 NFC_LLCP_MAX_SERVICE_NAME
);
725 llcp_sock
->service_name
= kmemdup(addr
->service_name
,
726 llcp_sock
->service_name_len
,
729 nfc_llcp_sock_link(&local
->connecting_sockets
, sk
);
731 ret
= nfc_llcp_send_connect(llcp_sock
);
735 sk
->sk_state
= LLCP_CONNECTING
;
737 ret
= sock_wait_state(sk
, LLCP_CONNECTED
,
738 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
739 if (ret
&& ret
!= -EINPROGRESS
)
747 nfc_llcp_put_ssap(local
, llcp_sock
->ssap
);
749 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
759 static int llcp_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
762 struct sock
*sk
= sock
->sk
;
763 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
766 pr_debug("sock %p sk %p", sock
, sk
);
768 ret
= sock_error(sk
);
772 if (msg
->msg_flags
& MSG_OOB
)
777 if (sk
->sk_type
== SOCK_DGRAM
) {
778 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, addr
,
781 if (msg
->msg_namelen
< sizeof(*addr
)) {
788 return nfc_llcp_send_ui_frame(llcp_sock
, addr
->dsap
, addr
->ssap
,
792 if (sk
->sk_state
!= LLCP_CONNECTED
) {
799 return nfc_llcp_send_i_frame(llcp_sock
, msg
, len
);
802 static int llcp_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
803 size_t len
, int flags
)
805 int noblock
= flags
& MSG_DONTWAIT
;
806 struct sock
*sk
= sock
->sk
;
807 unsigned int copied
, rlen
;
808 struct sk_buff
*skb
, *cskb
;
811 pr_debug("%p %zu\n", sk
, len
);
815 if (sk
->sk_state
== LLCP_CLOSED
&&
816 skb_queue_empty(&sk
->sk_receive_queue
)) {
823 if (flags
& (MSG_OOB
))
826 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
828 pr_err("Recv datagram failed state %d %d %d",
829 sk
->sk_state
, err
, sock_error(sk
));
831 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
837 rlen
= skb
->len
; /* real length of skb */
838 copied
= min_t(unsigned int, rlen
, len
);
841 if (skb_copy_datagram_msg(cskb
, 0, msg
, copied
)) {
842 if (!(flags
& MSG_PEEK
))
843 skb_queue_head(&sk
->sk_receive_queue
, skb
);
847 sock_recv_timestamp(msg
, sk
, skb
);
849 if (sk
->sk_type
== SOCK_DGRAM
&& msg
->msg_name
) {
850 struct nfc_llcp_ui_cb
*ui_cb
= nfc_llcp_ui_skb_cb(skb
);
851 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, sockaddr
,
854 msg
->msg_namelen
= sizeof(struct sockaddr_nfc_llcp
);
856 pr_debug("Datagram socket %d %d\n", ui_cb
->dsap
, ui_cb
->ssap
);
858 memset(sockaddr
, 0, sizeof(*sockaddr
));
859 sockaddr
->sa_family
= AF_NFC
;
860 sockaddr
->nfc_protocol
= NFC_PROTO_NFC_DEP
;
861 sockaddr
->dsap
= ui_cb
->dsap
;
862 sockaddr
->ssap
= ui_cb
->ssap
;
865 /* Mark read part of skb as used */
866 if (!(flags
& MSG_PEEK
)) {
868 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
869 if (sk
->sk_type
== SOCK_STREAM
||
870 sk
->sk_type
== SOCK_DGRAM
||
871 sk
->sk_type
== SOCK_RAW
) {
872 skb_pull(skb
, copied
);
874 skb_queue_head(&sk
->sk_receive_queue
, skb
);
882 /* XXX Queue backlogged skbs */
885 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
886 if (sk
->sk_type
== SOCK_SEQPACKET
&& (flags
& MSG_TRUNC
))
892 static const struct proto_ops llcp_sock_ops
= {
894 .owner
= THIS_MODULE
,
895 .bind
= llcp_sock_bind
,
896 .connect
= llcp_sock_connect
,
897 .release
= llcp_sock_release
,
898 .socketpair
= sock_no_socketpair
,
899 .accept
= llcp_sock_accept
,
900 .getname
= llcp_sock_getname
,
901 .poll
= llcp_sock_poll
,
902 .ioctl
= sock_no_ioctl
,
903 .listen
= llcp_sock_listen
,
904 .shutdown
= sock_no_shutdown
,
905 .setsockopt
= nfc_llcp_setsockopt
,
906 .getsockopt
= nfc_llcp_getsockopt
,
907 .sendmsg
= llcp_sock_sendmsg
,
908 .recvmsg
= llcp_sock_recvmsg
,
909 .mmap
= sock_no_mmap
,
912 static const struct proto_ops llcp_rawsock_ops
= {
914 .owner
= THIS_MODULE
,
915 .bind
= llcp_raw_sock_bind
,
916 .connect
= sock_no_connect
,
917 .release
= llcp_sock_release
,
918 .socketpair
= sock_no_socketpair
,
919 .accept
= sock_no_accept
,
920 .getname
= llcp_sock_getname
,
921 .poll
= llcp_sock_poll
,
922 .ioctl
= sock_no_ioctl
,
923 .listen
= sock_no_listen
,
924 .shutdown
= sock_no_shutdown
,
925 .setsockopt
= sock_no_setsockopt
,
926 .getsockopt
= sock_no_getsockopt
,
927 .sendmsg
= sock_no_sendmsg
,
928 .recvmsg
= llcp_sock_recvmsg
,
929 .mmap
= sock_no_mmap
,
932 static void llcp_sock_destruct(struct sock
*sk
)
934 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
936 pr_debug("%p\n", sk
);
938 if (sk
->sk_state
== LLCP_CONNECTED
)
939 nfc_put_device(llcp_sock
->dev
);
941 skb_queue_purge(&sk
->sk_receive_queue
);
943 nfc_llcp_sock_free(llcp_sock
);
945 if (!sock_flag(sk
, SOCK_DEAD
)) {
946 pr_err("Freeing alive NFC LLCP socket %p\n", sk
);
951 struct sock
*nfc_llcp_sock_alloc(struct socket
*sock
, int type
, gfp_t gfp
, int kern
)
954 struct nfc_llcp_sock
*llcp_sock
;
956 sk
= sk_alloc(&init_net
, PF_NFC
, gfp
, &llcp_sock_proto
, kern
);
960 llcp_sock
= nfc_llcp_sock(sk
);
962 sock_init_data(sock
, sk
);
963 sk
->sk_state
= LLCP_CLOSED
;
964 sk
->sk_protocol
= NFC_SOCKPROTO_LLCP
;
966 sk
->sk_destruct
= llcp_sock_destruct
;
969 llcp_sock
->dsap
= LLCP_SAP_SDP
;
970 llcp_sock
->rw
= LLCP_MAX_RW
+ 1;
971 llcp_sock
->miux
= cpu_to_be16(LLCP_MAX_MIUX
+ 1);
972 llcp_sock
->send_n
= llcp_sock
->send_ack_n
= 0;
973 llcp_sock
->recv_n
= llcp_sock
->recv_ack_n
= 0;
974 llcp_sock
->remote_ready
= 1;
975 llcp_sock
->reserved_ssap
= LLCP_SAP_MAX
;
976 nfc_llcp_socket_remote_param_init(llcp_sock
);
977 skb_queue_head_init(&llcp_sock
->tx_queue
);
978 skb_queue_head_init(&llcp_sock
->tx_pending_queue
);
979 INIT_LIST_HEAD(&llcp_sock
->accept_queue
);
982 sock
->state
= SS_UNCONNECTED
;
987 void nfc_llcp_sock_free(struct nfc_llcp_sock
*sock
)
989 kfree(sock
->service_name
);
991 skb_queue_purge(&sock
->tx_queue
);
992 skb_queue_purge(&sock
->tx_pending_queue
);
994 list_del_init(&sock
->accept_queue
);
998 nfc_llcp_local_put(sock
->local
);
1001 static int llcp_sock_create(struct net
*net
, struct socket
*sock
,
1002 const struct nfc_protocol
*nfc_proto
, int kern
)
1006 pr_debug("%p\n", sock
);
1008 if (sock
->type
!= SOCK_STREAM
&&
1009 sock
->type
!= SOCK_DGRAM
&&
1010 sock
->type
!= SOCK_RAW
)
1011 return -ESOCKTNOSUPPORT
;
1013 if (sock
->type
== SOCK_RAW
)
1014 sock
->ops
= &llcp_rawsock_ops
;
1016 sock
->ops
= &llcp_sock_ops
;
1018 sk
= nfc_llcp_sock_alloc(sock
, sock
->type
, GFP_ATOMIC
, kern
);
1025 static const struct nfc_protocol llcp_nfc_proto
= {
1026 .id
= NFC_SOCKPROTO_LLCP
,
1027 .proto
= &llcp_sock_proto
,
1028 .owner
= THIS_MODULE
,
1029 .create
= llcp_sock_create
1032 int __init
nfc_llcp_sock_init(void)
1034 return nfc_proto_register(&llcp_nfc_proto
);
1037 void nfc_llcp_sock_exit(void)
1039 nfc_proto_unregister(&llcp_nfc_proto
);