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
|| alen
< offsetofend(struct sockaddr
, sa_family
) ||
80 addr
->sa_family
!= AF_NFC
)
83 pr_debug("sk %p addr %p family %d\n", sk
, addr
, addr
->sa_family
);
85 memset(&llcp_addr
, 0, sizeof(llcp_addr
));
86 len
= min_t(unsigned int, sizeof(llcp_addr
), alen
);
87 memcpy(&llcp_addr
, addr
, len
);
89 /* This is going to be a listening socket, dsap must be 0 */
90 if (llcp_addr
.dsap
!= 0)
95 if (sk
->sk_state
!= LLCP_CLOSED
) {
100 dev
= nfc_get_device(llcp_addr
.dev_idx
);
106 local
= nfc_llcp_find_local(dev
);
112 llcp_sock
->dev
= dev
;
113 llcp_sock
->local
= nfc_llcp_local_get(local
);
114 llcp_sock
->nfc_protocol
= llcp_addr
.nfc_protocol
;
115 llcp_sock
->service_name_len
= min_t(unsigned int,
116 llcp_addr
.service_name_len
,
117 NFC_LLCP_MAX_SERVICE_NAME
);
118 llcp_sock
->service_name
= kmemdup(llcp_addr
.service_name
,
119 llcp_sock
->service_name_len
,
122 llcp_sock
->ssap
= nfc_llcp_get_sdp_ssap(local
, llcp_sock
);
123 if (llcp_sock
->ssap
== LLCP_SAP_MAX
) {
128 llcp_sock
->reserved_ssap
= llcp_sock
->ssap
;
130 nfc_llcp_sock_link(&local
->sockets
, sk
);
132 pr_debug("Socket bound to SAP %d\n", llcp_sock
->ssap
);
134 sk
->sk_state
= LLCP_BOUND
;
144 static int llcp_raw_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
147 struct sock
*sk
= sock
->sk
;
148 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
149 struct nfc_llcp_local
*local
;
151 struct sockaddr_nfc_llcp llcp_addr
;
154 if (!addr
|| alen
< offsetofend(struct sockaddr
, sa_family
) ||
155 addr
->sa_family
!= AF_NFC
)
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
);
166 if (sk
->sk_state
!= LLCP_CLOSED
) {
171 dev
= nfc_get_device(llcp_addr
.dev_idx
);
177 local
= nfc_llcp_find_local(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
;
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
);
208 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
) ||
209 sk
->sk_state
!= LLCP_BOUND
) {
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
;
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
);
234 pr_debug("%p optname %d\n", sk
, optname
);
236 if (level
!= SOL_NFC
)
243 if (sk
->sk_state
== LLCP_CONNECTED
||
244 sk
->sk_state
== LLCP_BOUND
||
245 sk
->sk_state
== LLCP_LISTEN
) {
250 if (get_user(opt
, (u32 __user
*) optval
)) {
255 if (opt
> LLCP_MAX_RW
) {
260 llcp_sock
->rw
= (u8
) opt
;
265 if (sk
->sk_state
== LLCP_CONNECTED
||
266 sk
->sk_state
== LLCP_BOUND
||
267 sk
->sk_state
== LLCP_LISTEN
) {
272 if (get_user(opt
, (u32 __user
*) optval
)) {
277 if (opt
> LLCP_MAX_MIUX
) {
282 llcp_sock
->miux
= cpu_to_be16((u16
) opt
);
293 pr_debug("%p rw %d miux %d\n", llcp_sock
,
294 llcp_sock
->rw
, llcp_sock
->miux
);
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
);
306 u16 miux
, remote_miu
;
309 pr_debug("%p optname %d\n", sk
, optname
);
311 if (level
!= SOL_NFC
)
314 if (get_user(len
, optlen
))
317 local
= llcp_sock
->local
;
321 len
= min_t(u32
, len
, sizeof(u32
));
327 rw
= llcp_sock
->rw
> LLCP_MAX_RW
? local
->rw
: llcp_sock
->rw
;
328 if (put_user(rw
, (u32 __user
*) optval
))
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
))
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
))
351 case NFC_LLCP_REMOTE_LTO
:
352 if (put_user(local
->remote_lto
/ 10, (u32 __user
*) optval
))
357 case NFC_LLCP_REMOTE_RW
:
358 if (put_user(llcp_sock
->remote_rw
, (u32 __user
*) optval
))
370 if (put_user(len
, optlen
))
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
;
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 */
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
;
409 llcp_parent
= nfc_llcp_sock(parent
);
411 list_for_each_entry_safe(lsk
, n
, &llcp_parent
->accept_queue
,
416 if (sk
->sk_state
== LLCP_CLOSED
) {
418 nfc_llcp_accept_unlink(sk
);
422 if (sk
->sk_state
== LLCP_CONNECTED
|| !newsock
) {
423 list_del_init(&lsk
->accept_queue
);
427 sock_graft(sk
, newsock
);
431 pr_debug("Returning sk state %d\n", sk
->sk_state
);
433 sk_acceptq_removed(parent
);
444 static int llcp_sock_accept(struct socket
*sock
, struct socket
*newsock
,
447 DECLARE_WAITQUEUE(wait
, current
);
448 struct sock
*sk
= sock
->sk
, *new_sk
;
452 pr_debug("parent %p\n", sk
);
454 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
456 if (sk
->sk_state
!= LLCP_LISTEN
) {
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
);
473 if (signal_pending(current
)) {
474 ret
= sock_intr_errno(timeo
);
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
);
488 newsock
->state
= SS_CONNECTED
;
490 pr_debug("new socket %p\n", new_sk
);
498 static int llcp_sock_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
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
)
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
);
515 if (!llcp_sock
->dev
) {
519 llcp_addr
->sa_family
= AF_NFC
;
520 llcp_addr
->dev_idx
= llcp_sock
->dev
->idx
;
521 llcp_addr
->target_idx
= llcp_sock
->target_idx
;
522 llcp_addr
->nfc_protocol
= llcp_sock
->nfc_protocol
;
523 llcp_addr
->dsap
= llcp_sock
->dsap
;
524 llcp_addr
->ssap
= llcp_sock
->ssap
;
525 llcp_addr
->service_name_len
= llcp_sock
->service_name_len
;
526 memcpy(llcp_addr
->service_name
, llcp_sock
->service_name
,
527 llcp_addr
->service_name_len
);
533 static inline unsigned int llcp_accept_poll(struct sock
*parent
)
535 struct nfc_llcp_sock
*llcp_sock
, *parent_sock
;
538 parent_sock
= nfc_llcp_sock(parent
);
540 list_for_each_entry(llcp_sock
, &parent_sock
->accept_queue
,
544 if (sk
->sk_state
== LLCP_CONNECTED
)
545 return POLLIN
| POLLRDNORM
;
551 static unsigned int llcp_sock_poll(struct file
*file
, struct socket
*sock
,
554 struct sock
*sk
= sock
->sk
;
555 unsigned int mask
= 0;
557 pr_debug("%p\n", sk
);
559 sock_poll_wait(file
, sk_sleep(sk
), wait
);
561 if (sk
->sk_state
== LLCP_LISTEN
)
562 return llcp_accept_poll(sk
);
564 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
566 (sock_flag(sk
, SOCK_SELECT_ERR_QUEUE
) ? POLLPRI
: 0);
568 if (!skb_queue_empty(&sk
->sk_receive_queue
))
569 mask
|= POLLIN
| POLLRDNORM
;
571 if (sk
->sk_state
== LLCP_CLOSED
)
574 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
575 mask
|= POLLRDHUP
| POLLIN
| POLLRDNORM
;
577 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
580 if (sock_writeable(sk
) && sk
->sk_state
== LLCP_CONNECTED
)
581 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
583 sk_set_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
585 pr_debug("mask 0x%x\n", mask
);
590 static int llcp_sock_release(struct socket
*sock
)
592 struct sock
*sk
= sock
->sk
;
593 struct nfc_llcp_local
*local
;
594 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
600 pr_debug("%p\n", sk
);
602 local
= llcp_sock
->local
;
611 if (sk
->sk_state
== LLCP_CONNECTED
)
612 nfc_llcp_send_disconnect(llcp_sock
);
614 if (sk
->sk_state
== LLCP_LISTEN
) {
615 struct nfc_llcp_sock
*lsk
, *n
;
616 struct sock
*accept_sk
;
618 list_for_each_entry_safe(lsk
, n
, &llcp_sock
->accept_queue
,
620 accept_sk
= &lsk
->sk
;
621 lock_sock(accept_sk
);
623 nfc_llcp_send_disconnect(lsk
);
624 nfc_llcp_accept_unlink(accept_sk
);
626 release_sock(accept_sk
);
630 if (llcp_sock
->reserved_ssap
< LLCP_SAP_MAX
)
631 nfc_llcp_put_ssap(llcp_sock
->local
, llcp_sock
->ssap
);
635 /* Keep this sock alive and therefore do not remove it from the sockets
636 * list until the DISC PDU has been actually sent. Otherwise we would
637 * reply with DM PDUs before sending the DISC one.
639 if (sk
->sk_state
== LLCP_DISCONNECTING
)
642 if (sock
->type
== SOCK_RAW
)
643 nfc_llcp_sock_unlink(&local
->raw_sockets
, sk
);
645 nfc_llcp_sock_unlink(&local
->sockets
, sk
);
654 static int llcp_sock_connect(struct socket
*sock
, struct sockaddr
*_addr
,
657 struct sock
*sk
= sock
->sk
;
658 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
659 struct sockaddr_nfc_llcp
*addr
= (struct sockaddr_nfc_llcp
*)_addr
;
661 struct nfc_llcp_local
*local
;
664 pr_debug("sock %p sk %p flags 0x%x\n", sock
, sk
, flags
);
666 if (!addr
|| len
< sizeof(*addr
) || addr
->sa_family
!= AF_NFC
)
669 if (addr
->service_name_len
== 0 && addr
->dsap
== 0)
672 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr
->dev_idx
,
673 addr
->target_idx
, addr
->nfc_protocol
);
677 if (sk
->sk_state
== LLCP_CONNECTED
) {
682 dev
= nfc_get_device(addr
->dev_idx
);
688 local
= nfc_llcp_find_local(dev
);
694 device_lock(&dev
->dev
);
695 if (dev
->dep_link_up
== false) {
697 device_unlock(&dev
->dev
);
700 device_unlock(&dev
->dev
);
702 if (local
->rf_mode
== NFC_RF_INITIATOR
&&
703 addr
->target_idx
!= local
->target_idx
) {
708 llcp_sock
->dev
= dev
;
709 llcp_sock
->local
= nfc_llcp_local_get(local
);
710 llcp_sock
->ssap
= nfc_llcp_get_local_ssap(local
);
711 if (llcp_sock
->ssap
== LLCP_SAP_MAX
) {
716 llcp_sock
->reserved_ssap
= llcp_sock
->ssap
;
718 if (addr
->service_name_len
== 0)
719 llcp_sock
->dsap
= addr
->dsap
;
721 llcp_sock
->dsap
= LLCP_SAP_SDP
;
722 llcp_sock
->nfc_protocol
= addr
->nfc_protocol
;
723 llcp_sock
->service_name_len
= min_t(unsigned int,
724 addr
->service_name_len
,
725 NFC_LLCP_MAX_SERVICE_NAME
);
726 llcp_sock
->service_name
= kmemdup(addr
->service_name
,
727 llcp_sock
->service_name_len
,
730 nfc_llcp_sock_link(&local
->connecting_sockets
, sk
);
732 ret
= nfc_llcp_send_connect(llcp_sock
);
736 sk
->sk_state
= LLCP_CONNECTING
;
738 ret
= sock_wait_state(sk
, LLCP_CONNECTED
,
739 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
740 if (ret
&& ret
!= -EINPROGRESS
)
748 nfc_llcp_put_ssap(local
, llcp_sock
->ssap
);
750 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
760 static int llcp_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
763 struct sock
*sk
= sock
->sk
;
764 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
767 pr_debug("sock %p sk %p", sock
, sk
);
769 ret
= sock_error(sk
);
773 if (msg
->msg_flags
& MSG_OOB
)
778 if (sk
->sk_type
== SOCK_DGRAM
) {
779 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, addr
,
782 if (msg
->msg_namelen
< sizeof(*addr
)) {
789 return nfc_llcp_send_ui_frame(llcp_sock
, addr
->dsap
, addr
->ssap
,
793 if (sk
->sk_state
!= LLCP_CONNECTED
) {
800 return nfc_llcp_send_i_frame(llcp_sock
, msg
, len
);
803 static int llcp_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
804 size_t len
, int flags
)
806 int noblock
= flags
& MSG_DONTWAIT
;
807 struct sock
*sk
= sock
->sk
;
808 unsigned int copied
, rlen
;
809 struct sk_buff
*skb
, *cskb
;
812 pr_debug("%p %zu\n", sk
, len
);
816 if (sk
->sk_state
== LLCP_CLOSED
&&
817 skb_queue_empty(&sk
->sk_receive_queue
)) {
824 if (flags
& (MSG_OOB
))
827 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
829 pr_err("Recv datagram failed state %d %d %d",
830 sk
->sk_state
, err
, sock_error(sk
));
832 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
838 rlen
= skb
->len
; /* real length of skb */
839 copied
= min_t(unsigned int, rlen
, len
);
842 if (skb_copy_datagram_msg(cskb
, 0, msg
, copied
)) {
843 if (!(flags
& MSG_PEEK
))
844 skb_queue_head(&sk
->sk_receive_queue
, skb
);
848 sock_recv_timestamp(msg
, sk
, skb
);
850 if (sk
->sk_type
== SOCK_DGRAM
&& msg
->msg_name
) {
851 struct nfc_llcp_ui_cb
*ui_cb
= nfc_llcp_ui_skb_cb(skb
);
852 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, sockaddr
,
855 msg
->msg_namelen
= sizeof(struct sockaddr_nfc_llcp
);
857 pr_debug("Datagram socket %d %d\n", ui_cb
->dsap
, ui_cb
->ssap
);
859 memset(sockaddr
, 0, sizeof(*sockaddr
));
860 sockaddr
->sa_family
= AF_NFC
;
861 sockaddr
->nfc_protocol
= NFC_PROTO_NFC_DEP
;
862 sockaddr
->dsap
= ui_cb
->dsap
;
863 sockaddr
->ssap
= ui_cb
->ssap
;
866 /* Mark read part of skb as used */
867 if (!(flags
& MSG_PEEK
)) {
869 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
870 if (sk
->sk_type
== SOCK_STREAM
||
871 sk
->sk_type
== SOCK_DGRAM
||
872 sk
->sk_type
== SOCK_RAW
) {
873 skb_pull(skb
, copied
);
875 skb_queue_head(&sk
->sk_receive_queue
, skb
);
883 /* XXX Queue backlogged skbs */
886 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
887 if (sk
->sk_type
== SOCK_SEQPACKET
&& (flags
& MSG_TRUNC
))
893 static const struct proto_ops llcp_sock_ops
= {
895 .owner
= THIS_MODULE
,
896 .bind
= llcp_sock_bind
,
897 .connect
= llcp_sock_connect
,
898 .release
= llcp_sock_release
,
899 .socketpair
= sock_no_socketpair
,
900 .accept
= llcp_sock_accept
,
901 .getname
= llcp_sock_getname
,
902 .poll
= llcp_sock_poll
,
903 .ioctl
= sock_no_ioctl
,
904 .listen
= llcp_sock_listen
,
905 .shutdown
= sock_no_shutdown
,
906 .setsockopt
= nfc_llcp_setsockopt
,
907 .getsockopt
= nfc_llcp_getsockopt
,
908 .sendmsg
= llcp_sock_sendmsg
,
909 .recvmsg
= llcp_sock_recvmsg
,
910 .mmap
= sock_no_mmap
,
913 static const struct proto_ops llcp_rawsock_ops
= {
915 .owner
= THIS_MODULE
,
916 .bind
= llcp_raw_sock_bind
,
917 .connect
= sock_no_connect
,
918 .release
= llcp_sock_release
,
919 .socketpair
= sock_no_socketpair
,
920 .accept
= sock_no_accept
,
921 .getname
= llcp_sock_getname
,
922 .poll
= llcp_sock_poll
,
923 .ioctl
= sock_no_ioctl
,
924 .listen
= sock_no_listen
,
925 .shutdown
= sock_no_shutdown
,
926 .setsockopt
= sock_no_setsockopt
,
927 .getsockopt
= sock_no_getsockopt
,
928 .sendmsg
= sock_no_sendmsg
,
929 .recvmsg
= llcp_sock_recvmsg
,
930 .mmap
= sock_no_mmap
,
933 static void llcp_sock_destruct(struct sock
*sk
)
935 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
937 pr_debug("%p\n", sk
);
939 if (sk
->sk_state
== LLCP_CONNECTED
)
940 nfc_put_device(llcp_sock
->dev
);
942 skb_queue_purge(&sk
->sk_receive_queue
);
944 nfc_llcp_sock_free(llcp_sock
);
946 if (!sock_flag(sk
, SOCK_DEAD
)) {
947 pr_err("Freeing alive NFC LLCP socket %p\n", sk
);
952 struct sock
*nfc_llcp_sock_alloc(struct socket
*sock
, int type
, gfp_t gfp
, int kern
)
955 struct nfc_llcp_sock
*llcp_sock
;
957 sk
= sk_alloc(&init_net
, PF_NFC
, gfp
, &llcp_sock_proto
, kern
);
961 llcp_sock
= nfc_llcp_sock(sk
);
963 sock_init_data(sock
, sk
);
964 sk
->sk_state
= LLCP_CLOSED
;
965 sk
->sk_protocol
= NFC_SOCKPROTO_LLCP
;
967 sk
->sk_destruct
= llcp_sock_destruct
;
970 llcp_sock
->dsap
= LLCP_SAP_SDP
;
971 llcp_sock
->rw
= LLCP_MAX_RW
+ 1;
972 llcp_sock
->miux
= cpu_to_be16(LLCP_MAX_MIUX
+ 1);
973 llcp_sock
->send_n
= llcp_sock
->send_ack_n
= 0;
974 llcp_sock
->recv_n
= llcp_sock
->recv_ack_n
= 0;
975 llcp_sock
->remote_ready
= 1;
976 llcp_sock
->reserved_ssap
= LLCP_SAP_MAX
;
977 nfc_llcp_socket_remote_param_init(llcp_sock
);
978 skb_queue_head_init(&llcp_sock
->tx_queue
);
979 skb_queue_head_init(&llcp_sock
->tx_pending_queue
);
980 INIT_LIST_HEAD(&llcp_sock
->accept_queue
);
983 sock
->state
= SS_UNCONNECTED
;
988 void nfc_llcp_sock_free(struct nfc_llcp_sock
*sock
)
990 kfree(sock
->service_name
);
992 skb_queue_purge(&sock
->tx_queue
);
993 skb_queue_purge(&sock
->tx_pending_queue
);
995 list_del_init(&sock
->accept_queue
);
999 nfc_llcp_local_put(sock
->local
);
1002 static int llcp_sock_create(struct net
*net
, struct socket
*sock
,
1003 const struct nfc_protocol
*nfc_proto
, int kern
)
1007 pr_debug("%p\n", sock
);
1009 if (sock
->type
!= SOCK_STREAM
&&
1010 sock
->type
!= SOCK_DGRAM
&&
1011 sock
->type
!= SOCK_RAW
)
1012 return -ESOCKTNOSUPPORT
;
1014 if (sock
->type
== SOCK_RAW
)
1015 sock
->ops
= &llcp_rawsock_ops
;
1017 sock
->ops
= &llcp_sock_ops
;
1019 sk
= nfc_llcp_sock_alloc(sock
, sock
->type
, GFP_ATOMIC
, kern
);
1026 static const struct nfc_protocol llcp_nfc_proto
= {
1027 .id
= NFC_SOCKPROTO_LLCP
,
1028 .proto
= &llcp_sock_proto
,
1029 .owner
= THIS_MODULE
,
1030 .create
= llcp_sock_create
1033 int __init
nfc_llcp_sock_init(void)
1035 return nfc_proto_register(&llcp_nfc_proto
);
1038 void nfc_llcp_sock_exit(void)
1040 nfc_proto_unregister(&llcp_nfc_proto
);