1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Intel Corporation. All rights reserved.
6 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/nfc.h>
12 #include <linux/sched/signal.h>
17 static int sock_wait_state(struct sock
*sk
, int state
, unsigned long timeo
)
19 DECLARE_WAITQUEUE(wait
, current
);
22 pr_debug("sk %p", sk
);
24 add_wait_queue(sk_sleep(sk
), &wait
);
25 set_current_state(TASK_INTERRUPTIBLE
);
27 while (sk
->sk_state
!= state
) {
33 if (signal_pending(current
)) {
34 err
= sock_intr_errno(timeo
);
39 timeo
= schedule_timeout(timeo
);
41 set_current_state(TASK_INTERRUPTIBLE
);
48 __set_current_state(TASK_RUNNING
);
49 remove_wait_queue(sk_sleep(sk
), &wait
);
53 static struct proto llcp_sock_proto
= {
56 .obj_size
= sizeof(struct nfc_llcp_sock
),
59 static int llcp_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int alen
)
61 struct sock
*sk
= sock
->sk
;
62 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
63 struct nfc_llcp_local
*local
;
65 struct sockaddr_nfc_llcp llcp_addr
;
68 if (!addr
|| alen
< offsetofend(struct sockaddr
, sa_family
) ||
69 addr
->sa_family
!= AF_NFC
)
72 pr_debug("sk %p addr %p family %d\n", sk
, addr
, addr
->sa_family
);
74 memset(&llcp_addr
, 0, sizeof(llcp_addr
));
75 len
= min_t(unsigned int, sizeof(llcp_addr
), alen
);
76 memcpy(&llcp_addr
, addr
, len
);
78 /* This is going to be a listening socket, dsap must be 0 */
79 if (llcp_addr
.dsap
!= 0)
84 if (sk
->sk_state
!= LLCP_CLOSED
) {
89 dev
= nfc_get_device(llcp_addr
.dev_idx
);
95 local
= nfc_llcp_find_local(dev
);
101 llcp_sock
->dev
= dev
;
102 llcp_sock
->local
= nfc_llcp_local_get(local
);
103 llcp_sock
->nfc_protocol
= llcp_addr
.nfc_protocol
;
104 llcp_sock
->service_name_len
= min_t(unsigned int,
105 llcp_addr
.service_name_len
,
106 NFC_LLCP_MAX_SERVICE_NAME
);
107 llcp_sock
->service_name
= kmemdup(llcp_addr
.service_name
,
108 llcp_sock
->service_name_len
,
110 if (!llcp_sock
->service_name
) {
114 llcp_sock
->ssap
= nfc_llcp_get_sdp_ssap(local
, llcp_sock
);
115 if (llcp_sock
->ssap
== LLCP_SAP_MAX
) {
116 kfree(llcp_sock
->service_name
);
117 llcp_sock
->service_name
= NULL
;
122 llcp_sock
->reserved_ssap
= llcp_sock
->ssap
;
124 nfc_llcp_sock_link(&local
->sockets
, sk
);
126 pr_debug("Socket bound to SAP %d\n", llcp_sock
->ssap
);
128 sk
->sk_state
= LLCP_BOUND
;
138 static int llcp_raw_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
141 struct sock
*sk
= sock
->sk
;
142 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
143 struct nfc_llcp_local
*local
;
145 struct sockaddr_nfc_llcp llcp_addr
;
148 if (!addr
|| alen
< offsetofend(struct sockaddr
, sa_family
) ||
149 addr
->sa_family
!= AF_NFC
)
152 pr_debug("sk %p addr %p family %d\n", sk
, addr
, addr
->sa_family
);
154 memset(&llcp_addr
, 0, sizeof(llcp_addr
));
155 len
= min_t(unsigned int, sizeof(llcp_addr
), alen
);
156 memcpy(&llcp_addr
, addr
, len
);
160 if (sk
->sk_state
!= LLCP_CLOSED
) {
165 dev
= nfc_get_device(llcp_addr
.dev_idx
);
171 local
= nfc_llcp_find_local(dev
);
177 llcp_sock
->dev
= dev
;
178 llcp_sock
->local
= nfc_llcp_local_get(local
);
179 llcp_sock
->nfc_protocol
= llcp_addr
.nfc_protocol
;
181 nfc_llcp_sock_link(&local
->raw_sockets
, sk
);
183 sk
->sk_state
= LLCP_BOUND
;
193 static int llcp_sock_listen(struct socket
*sock
, int backlog
)
195 struct sock
*sk
= sock
->sk
;
198 pr_debug("sk %p backlog %d\n", sk
, backlog
);
202 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
) ||
203 sk
->sk_state
!= LLCP_BOUND
) {
208 sk
->sk_max_ack_backlog
= backlog
;
209 sk
->sk_ack_backlog
= 0;
211 pr_debug("Socket listening\n");
212 sk
->sk_state
= LLCP_LISTEN
;
220 static int nfc_llcp_setsockopt(struct socket
*sock
, int level
, int optname
,
221 sockptr_t optval
, unsigned int optlen
)
223 struct sock
*sk
= sock
->sk
;
224 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
228 pr_debug("%p optname %d\n", sk
, optname
);
230 if (level
!= SOL_NFC
)
237 if (sk
->sk_state
== LLCP_CONNECTED
||
238 sk
->sk_state
== LLCP_BOUND
||
239 sk
->sk_state
== LLCP_LISTEN
) {
244 if (copy_from_sockptr(&opt
, optval
, sizeof(u32
))) {
249 if (opt
> LLCP_MAX_RW
) {
254 llcp_sock
->rw
= (u8
) opt
;
259 if (sk
->sk_state
== LLCP_CONNECTED
||
260 sk
->sk_state
== LLCP_BOUND
||
261 sk
->sk_state
== LLCP_LISTEN
) {
266 if (copy_from_sockptr(&opt
, optval
, sizeof(u32
))) {
271 if (opt
> LLCP_MAX_MIUX
) {
276 llcp_sock
->miux
= cpu_to_be16((u16
) opt
);
287 pr_debug("%p rw %d miux %d\n", llcp_sock
,
288 llcp_sock
->rw
, llcp_sock
->miux
);
293 static int nfc_llcp_getsockopt(struct socket
*sock
, int level
, int optname
,
294 char __user
*optval
, int __user
*optlen
)
296 struct nfc_llcp_local
*local
;
297 struct sock
*sk
= sock
->sk
;
298 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
300 u16 miux
, remote_miu
;
303 pr_debug("%p optname %d\n", sk
, optname
);
305 if (level
!= SOL_NFC
)
308 if (get_user(len
, optlen
))
311 local
= llcp_sock
->local
;
315 len
= min_t(u32
, len
, sizeof(u32
));
321 rw
= llcp_sock
->rw
> LLCP_MAX_RW
? local
->rw
: llcp_sock
->rw
;
322 if (put_user(rw
, (u32 __user
*) optval
))
328 miux
= be16_to_cpu(llcp_sock
->miux
) > LLCP_MAX_MIUX
?
329 be16_to_cpu(local
->miux
) : be16_to_cpu(llcp_sock
->miux
);
331 if (put_user(miux
, (u32 __user
*) optval
))
336 case NFC_LLCP_REMOTE_MIU
:
337 remote_miu
= llcp_sock
->remote_miu
> LLCP_MAX_MIU
?
338 local
->remote_miu
: llcp_sock
->remote_miu
;
340 if (put_user(remote_miu
, (u32 __user
*) optval
))
345 case NFC_LLCP_REMOTE_LTO
:
346 if (put_user(local
->remote_lto
/ 10, (u32 __user
*) optval
))
351 case NFC_LLCP_REMOTE_RW
:
352 if (put_user(llcp_sock
->remote_rw
, (u32 __user
*) optval
))
364 if (put_user(len
, optlen
))
370 void nfc_llcp_accept_unlink(struct sock
*sk
)
372 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
374 pr_debug("state %d\n", sk
->sk_state
);
376 list_del_init(&llcp_sock
->accept_queue
);
377 sk_acceptq_removed(llcp_sock
->parent
);
378 llcp_sock
->parent
= NULL
;
383 void nfc_llcp_accept_enqueue(struct sock
*parent
, struct sock
*sk
)
385 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
386 struct nfc_llcp_sock
*llcp_sock_parent
= nfc_llcp_sock(parent
);
388 /* Lock will be free from unlink */
391 list_add_tail(&llcp_sock
->accept_queue
,
392 &llcp_sock_parent
->accept_queue
);
393 llcp_sock
->parent
= parent
;
394 sk_acceptq_added(parent
);
397 struct sock
*nfc_llcp_accept_dequeue(struct sock
*parent
,
398 struct socket
*newsock
)
400 struct nfc_llcp_sock
*lsk
, *n
, *llcp_parent
;
403 llcp_parent
= nfc_llcp_sock(parent
);
405 list_for_each_entry_safe(lsk
, n
, &llcp_parent
->accept_queue
,
410 if (sk
->sk_state
== LLCP_CLOSED
) {
412 nfc_llcp_accept_unlink(sk
);
416 if (sk
->sk_state
== LLCP_CONNECTED
|| !newsock
) {
417 list_del_init(&lsk
->accept_queue
);
421 sock_graft(sk
, newsock
);
425 pr_debug("Returning sk state %d\n", sk
->sk_state
);
427 sk_acceptq_removed(parent
);
438 static int llcp_sock_accept(struct socket
*sock
, struct socket
*newsock
,
439 int flags
, bool kern
)
441 DECLARE_WAITQUEUE(wait
, current
);
442 struct sock
*sk
= sock
->sk
, *new_sk
;
446 pr_debug("parent %p\n", sk
);
448 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
450 if (sk
->sk_state
!= LLCP_LISTEN
) {
455 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
457 /* Wait for an incoming connection. */
458 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
459 while (!(new_sk
= nfc_llcp_accept_dequeue(sk
, newsock
))) {
460 set_current_state(TASK_INTERRUPTIBLE
);
467 if (signal_pending(current
)) {
468 ret
= sock_intr_errno(timeo
);
473 timeo
= schedule_timeout(timeo
);
474 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
476 __set_current_state(TASK_RUNNING
);
477 remove_wait_queue(sk_sleep(sk
), &wait
);
482 newsock
->state
= SS_CONNECTED
;
484 pr_debug("new socket %p\n", new_sk
);
492 static int llcp_sock_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
495 struct sock
*sk
= sock
->sk
;
496 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
497 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, llcp_addr
, uaddr
);
499 if (llcp_sock
== NULL
|| llcp_sock
->dev
== NULL
)
502 pr_debug("%p %d %d %d\n", sk
, llcp_sock
->target_idx
,
503 llcp_sock
->dsap
, llcp_sock
->ssap
);
505 memset(llcp_addr
, 0, sizeof(*llcp_addr
));
508 if (!llcp_sock
->dev
) {
512 llcp_addr
->sa_family
= AF_NFC
;
513 llcp_addr
->dev_idx
= llcp_sock
->dev
->idx
;
514 llcp_addr
->target_idx
= llcp_sock
->target_idx
;
515 llcp_addr
->nfc_protocol
= llcp_sock
->nfc_protocol
;
516 llcp_addr
->dsap
= llcp_sock
->dsap
;
517 llcp_addr
->ssap
= llcp_sock
->ssap
;
518 llcp_addr
->service_name_len
= llcp_sock
->service_name_len
;
519 memcpy(llcp_addr
->service_name
, llcp_sock
->service_name
,
520 llcp_addr
->service_name_len
);
523 return sizeof(struct sockaddr_nfc_llcp
);
526 static inline __poll_t
llcp_accept_poll(struct sock
*parent
)
528 struct nfc_llcp_sock
*llcp_sock
, *parent_sock
;
531 parent_sock
= nfc_llcp_sock(parent
);
533 list_for_each_entry(llcp_sock
, &parent_sock
->accept_queue
,
537 if (sk
->sk_state
== LLCP_CONNECTED
)
538 return EPOLLIN
| EPOLLRDNORM
;
544 static __poll_t
llcp_sock_poll(struct file
*file
, struct socket
*sock
,
547 struct sock
*sk
= sock
->sk
;
550 pr_debug("%p\n", sk
);
552 sock_poll_wait(file
, sock
, wait
);
554 if (sk
->sk_state
== LLCP_LISTEN
)
555 return llcp_accept_poll(sk
);
557 if (sk
->sk_err
|| !skb_queue_empty_lockless(&sk
->sk_error_queue
))
559 (sock_flag(sk
, SOCK_SELECT_ERR_QUEUE
) ? EPOLLPRI
: 0);
561 if (!skb_queue_empty_lockless(&sk
->sk_receive_queue
))
562 mask
|= EPOLLIN
| EPOLLRDNORM
;
564 if (sk
->sk_state
== LLCP_CLOSED
)
567 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
568 mask
|= EPOLLRDHUP
| EPOLLIN
| EPOLLRDNORM
;
570 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
573 if (sock_writeable(sk
) && sk
->sk_state
== LLCP_CONNECTED
)
574 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
576 sk_set_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
578 pr_debug("mask 0x%x\n", mask
);
583 static int llcp_sock_release(struct socket
*sock
)
585 struct sock
*sk
= sock
->sk
;
586 struct nfc_llcp_local
*local
;
587 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
593 pr_debug("%p\n", sk
);
595 local
= llcp_sock
->local
;
604 if (sk
->sk_state
== LLCP_CONNECTED
)
605 nfc_llcp_send_disconnect(llcp_sock
);
607 if (sk
->sk_state
== LLCP_LISTEN
) {
608 struct nfc_llcp_sock
*lsk
, *n
;
609 struct sock
*accept_sk
;
611 list_for_each_entry_safe(lsk
, n
, &llcp_sock
->accept_queue
,
613 accept_sk
= &lsk
->sk
;
614 lock_sock(accept_sk
);
616 nfc_llcp_send_disconnect(lsk
);
617 nfc_llcp_accept_unlink(accept_sk
);
619 release_sock(accept_sk
);
623 if (llcp_sock
->reserved_ssap
< LLCP_SAP_MAX
)
624 nfc_llcp_put_ssap(llcp_sock
->local
, llcp_sock
->ssap
);
628 /* Keep this sock alive and therefore do not remove it from the sockets
629 * list until the DISC PDU has been actually sent. Otherwise we would
630 * reply with DM PDUs before sending the DISC one.
632 if (sk
->sk_state
== LLCP_DISCONNECTING
)
635 if (sock
->type
== SOCK_RAW
)
636 nfc_llcp_sock_unlink(&local
->raw_sockets
, sk
);
638 nfc_llcp_sock_unlink(&local
->sockets
, sk
);
647 static int llcp_sock_connect(struct socket
*sock
, struct sockaddr
*_addr
,
650 struct sock
*sk
= sock
->sk
;
651 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
652 struct sockaddr_nfc_llcp
*addr
= (struct sockaddr_nfc_llcp
*)_addr
;
654 struct nfc_llcp_local
*local
;
657 pr_debug("sock %p sk %p flags 0x%x\n", sock
, sk
, flags
);
659 if (!addr
|| len
< sizeof(*addr
) || addr
->sa_family
!= AF_NFC
)
662 if (addr
->service_name_len
== 0 && addr
->dsap
== 0)
665 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr
->dev_idx
,
666 addr
->target_idx
, addr
->nfc_protocol
);
670 if (sk
->sk_state
== LLCP_CONNECTED
) {
675 dev
= nfc_get_device(addr
->dev_idx
);
681 local
= nfc_llcp_find_local(dev
);
687 device_lock(&dev
->dev
);
688 if (dev
->dep_link_up
== false) {
690 device_unlock(&dev
->dev
);
693 device_unlock(&dev
->dev
);
695 if (local
->rf_mode
== NFC_RF_INITIATOR
&&
696 addr
->target_idx
!= local
->target_idx
) {
701 llcp_sock
->dev
= dev
;
702 llcp_sock
->local
= nfc_llcp_local_get(local
);
703 llcp_sock
->ssap
= nfc_llcp_get_local_ssap(local
);
704 if (llcp_sock
->ssap
== LLCP_SAP_MAX
) {
709 llcp_sock
->reserved_ssap
= llcp_sock
->ssap
;
711 if (addr
->service_name_len
== 0)
712 llcp_sock
->dsap
= addr
->dsap
;
714 llcp_sock
->dsap
= LLCP_SAP_SDP
;
715 llcp_sock
->nfc_protocol
= addr
->nfc_protocol
;
716 llcp_sock
->service_name_len
= min_t(unsigned int,
717 addr
->service_name_len
,
718 NFC_LLCP_MAX_SERVICE_NAME
);
719 llcp_sock
->service_name
= kmemdup(addr
->service_name
,
720 llcp_sock
->service_name_len
,
722 if (!llcp_sock
->service_name
) {
724 goto sock_llcp_release
;
727 nfc_llcp_sock_link(&local
->connecting_sockets
, sk
);
729 ret
= nfc_llcp_send_connect(llcp_sock
);
733 sk
->sk_state
= LLCP_CONNECTING
;
735 ret
= sock_wait_state(sk
, LLCP_CONNECTED
,
736 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
737 if (ret
&& ret
!= -EINPROGRESS
)
745 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
748 nfc_llcp_put_ssap(local
, llcp_sock
->ssap
);
758 static int llcp_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
761 struct sock
*sk
= sock
->sk
;
762 struct nfc_llcp_sock
*llcp_sock
= nfc_llcp_sock(sk
);
765 pr_debug("sock %p sk %p", sock
, sk
);
767 ret
= sock_error(sk
);
771 if (msg
->msg_flags
& MSG_OOB
)
776 if (sk
->sk_type
== SOCK_DGRAM
) {
777 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, addr
,
780 if (msg
->msg_namelen
< sizeof(*addr
)) {
787 return nfc_llcp_send_ui_frame(llcp_sock
, addr
->dsap
, addr
->ssap
,
791 if (sk
->sk_state
!= LLCP_CONNECTED
) {
798 return nfc_llcp_send_i_frame(llcp_sock
, msg
, len
);
801 static int llcp_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
802 size_t len
, int flags
)
804 int noblock
= flags
& MSG_DONTWAIT
;
805 struct sock
*sk
= sock
->sk
;
806 unsigned int copied
, rlen
;
807 struct sk_buff
*skb
, *cskb
;
810 pr_debug("%p %zu\n", sk
, len
);
814 if (sk
->sk_state
== LLCP_CLOSED
&&
815 skb_queue_empty(&sk
->sk_receive_queue
)) {
822 if (flags
& (MSG_OOB
))
825 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
827 pr_err("Recv datagram failed state %d %d %d",
828 sk
->sk_state
, err
, sock_error(sk
));
830 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
836 rlen
= skb
->len
; /* real length of skb */
837 copied
= min_t(unsigned int, rlen
, len
);
840 if (skb_copy_datagram_msg(cskb
, 0, msg
, copied
)) {
841 if (!(flags
& MSG_PEEK
))
842 skb_queue_head(&sk
->sk_receive_queue
, skb
);
846 sock_recv_timestamp(msg
, sk
, skb
);
848 if (sk
->sk_type
== SOCK_DGRAM
&& msg
->msg_name
) {
849 struct nfc_llcp_ui_cb
*ui_cb
= nfc_llcp_ui_skb_cb(skb
);
850 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp
*, sockaddr
,
853 msg
->msg_namelen
= sizeof(struct sockaddr_nfc_llcp
);
855 pr_debug("Datagram socket %d %d\n", ui_cb
->dsap
, ui_cb
->ssap
);
857 memset(sockaddr
, 0, sizeof(*sockaddr
));
858 sockaddr
->sa_family
= AF_NFC
;
859 sockaddr
->nfc_protocol
= NFC_PROTO_NFC_DEP
;
860 sockaddr
->dsap
= ui_cb
->dsap
;
861 sockaddr
->ssap
= ui_cb
->ssap
;
864 /* Mark read part of skb as used */
865 if (!(flags
& MSG_PEEK
)) {
867 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
868 if (sk
->sk_type
== SOCK_STREAM
||
869 sk
->sk_type
== SOCK_DGRAM
||
870 sk
->sk_type
== SOCK_RAW
) {
871 skb_pull(skb
, copied
);
873 skb_queue_head(&sk
->sk_receive_queue
, skb
);
881 /* XXX Queue backlogged skbs */
884 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
885 if (sk
->sk_type
== SOCK_SEQPACKET
&& (flags
& MSG_TRUNC
))
891 static const struct proto_ops llcp_sock_ops
= {
893 .owner
= THIS_MODULE
,
894 .bind
= llcp_sock_bind
,
895 .connect
= llcp_sock_connect
,
896 .release
= llcp_sock_release
,
897 .socketpair
= sock_no_socketpair
,
898 .accept
= llcp_sock_accept
,
899 .getname
= llcp_sock_getname
,
900 .poll
= llcp_sock_poll
,
901 .ioctl
= sock_no_ioctl
,
902 .listen
= llcp_sock_listen
,
903 .shutdown
= sock_no_shutdown
,
904 .setsockopt
= nfc_llcp_setsockopt
,
905 .getsockopt
= nfc_llcp_getsockopt
,
906 .sendmsg
= llcp_sock_sendmsg
,
907 .recvmsg
= llcp_sock_recvmsg
,
908 .mmap
= sock_no_mmap
,
911 static const struct proto_ops llcp_rawsock_ops
= {
913 .owner
= THIS_MODULE
,
914 .bind
= llcp_raw_sock_bind
,
915 .connect
= sock_no_connect
,
916 .release
= llcp_sock_release
,
917 .socketpair
= sock_no_socketpair
,
918 .accept
= sock_no_accept
,
919 .getname
= llcp_sock_getname
,
920 .poll
= llcp_sock_poll
,
921 .ioctl
= sock_no_ioctl
,
922 .listen
= sock_no_listen
,
923 .shutdown
= sock_no_shutdown
,
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
);
948 struct sock
*nfc_llcp_sock_alloc(struct socket
*sock
, int type
, gfp_t gfp
, int kern
)
951 struct nfc_llcp_sock
*llcp_sock
;
953 sk
= sk_alloc(&init_net
, PF_NFC
, gfp
, &llcp_sock_proto
, kern
);
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
;
963 sk
->sk_destruct
= llcp_sock_destruct
;
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
);
979 sock
->state
= SS_UNCONNECTED
;
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
);
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
, int kern
)
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 if (!capable(CAP_NET_RAW
))
1013 sock
->ops
= &llcp_rawsock_ops
;
1015 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
);