1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
12 #include <net/tcp_states.h>
13 #include <linux/nfc.h>
14 #include <linux/export.h>
18 static struct nfc_sock_list raw_sk_list
= {
19 .lock
= __RW_LOCK_UNLOCKED(raw_sk_list
.lock
)
22 static void nfc_sock_link(struct nfc_sock_list
*l
, struct sock
*sk
)
25 sk_add_node(sk
, &l
->head
);
26 write_unlock(&l
->lock
);
29 static void nfc_sock_unlink(struct nfc_sock_list
*l
, struct sock
*sk
)
33 write_unlock(&l
->lock
);
36 static void rawsock_write_queue_purge(struct sock
*sk
)
38 pr_debug("sk=%p\n", sk
);
40 spin_lock_bh(&sk
->sk_write_queue
.lock
);
41 __skb_queue_purge(&sk
->sk_write_queue
);
42 nfc_rawsock(sk
)->tx_work_scheduled
= false;
43 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
46 static void rawsock_report_error(struct sock
*sk
, int err
)
48 pr_debug("sk=%p err=%d\n", sk
, err
);
50 sk
->sk_shutdown
= SHUTDOWN_MASK
;
52 sk
->sk_error_report(sk
);
54 rawsock_write_queue_purge(sk
);
57 static int rawsock_release(struct socket
*sock
)
59 struct sock
*sk
= sock
->sk
;
61 pr_debug("sock=%p sk=%p\n", sock
, sk
);
66 if (sock
->type
== SOCK_RAW
)
67 nfc_sock_unlink(&raw_sk_list
, sk
);
75 static int rawsock_connect(struct socket
*sock
, struct sockaddr
*_addr
,
78 struct sock
*sk
= sock
->sk
;
79 struct sockaddr_nfc
*addr
= (struct sockaddr_nfc
*)_addr
;
83 pr_debug("sock=%p sk=%p flags=%d\n", sock
, sk
, flags
);
85 if (!addr
|| len
< sizeof(struct sockaddr_nfc
) ||
86 addr
->sa_family
!= AF_NFC
)
89 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n",
90 addr
->dev_idx
, addr
->target_idx
, addr
->nfc_protocol
);
94 if (sock
->state
== SS_CONNECTED
) {
99 dev
= nfc_get_device(addr
->dev_idx
);
105 if (addr
->target_idx
> dev
->target_next_idx
- 1 ||
106 addr
->target_idx
< dev
->target_next_idx
- dev
->n_targets
) {
111 rc
= nfc_activate_target(dev
, addr
->target_idx
, addr
->nfc_protocol
);
115 nfc_rawsock(sk
)->dev
= dev
;
116 nfc_rawsock(sk
)->target_idx
= addr
->target_idx
;
117 sock
->state
= SS_CONNECTED
;
118 sk
->sk_state
= TCP_ESTABLISHED
;
119 sk
->sk_state_change(sk
);
131 static int rawsock_add_header(struct sk_buff
*skb
)
133 *(u8
*)skb_push(skb
, NFC_HEADER_SIZE
) = 0;
138 static void rawsock_data_exchange_complete(void *context
, struct sk_buff
*skb
,
141 struct sock
*sk
= (struct sock
*) context
;
145 pr_debug("sk=%p err=%d\n", sk
, err
);
150 err
= rawsock_add_header(skb
);
154 err
= sock_queue_rcv_skb(sk
, skb
);
158 spin_lock_bh(&sk
->sk_write_queue
.lock
);
159 if (!skb_queue_empty(&sk
->sk_write_queue
))
160 schedule_work(&nfc_rawsock(sk
)->tx_work
);
162 nfc_rawsock(sk
)->tx_work_scheduled
= false;
163 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
172 rawsock_report_error(sk
, err
);
176 static void rawsock_tx_work(struct work_struct
*work
)
178 struct sock
*sk
= to_rawsock_sk(work
);
179 struct nfc_dev
*dev
= nfc_rawsock(sk
)->dev
;
180 u32 target_idx
= nfc_rawsock(sk
)->target_idx
;
184 pr_debug("sk=%p target_idx=%u\n", sk
, target_idx
);
186 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
187 rawsock_write_queue_purge(sk
);
191 skb
= skb_dequeue(&sk
->sk_write_queue
);
194 rc
= nfc_data_exchange(dev
, target_idx
, skb
,
195 rawsock_data_exchange_complete
, sk
);
197 rawsock_report_error(sk
, rc
);
202 static int rawsock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
204 struct sock
*sk
= sock
->sk
;
205 struct nfc_dev
*dev
= nfc_rawsock(sk
)->dev
;
209 pr_debug("sock=%p sk=%p len=%zu\n", sock
, sk
, len
);
211 if (msg
->msg_namelen
)
214 if (sock
->state
!= SS_CONNECTED
)
217 skb
= nfc_alloc_send_skb(dev
, sk
, msg
->msg_flags
, len
, &rc
);
221 rc
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
227 spin_lock_bh(&sk
->sk_write_queue
.lock
);
228 __skb_queue_tail(&sk
->sk_write_queue
, skb
);
229 if (!nfc_rawsock(sk
)->tx_work_scheduled
) {
230 schedule_work(&nfc_rawsock(sk
)->tx_work
);
231 nfc_rawsock(sk
)->tx_work_scheduled
= true;
233 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
238 static int rawsock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
241 int noblock
= flags
& MSG_DONTWAIT
;
242 struct sock
*sk
= sock
->sk
;
247 pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock
, sk
, len
, flags
);
249 skb
= skb_recv_datagram(sk
, flags
, noblock
, &rc
);
255 msg
->msg_flags
|= MSG_TRUNC
;
259 rc
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
261 skb_free_datagram(sk
, skb
);
263 return rc
? : copied
;
266 static const struct proto_ops rawsock_ops
= {
268 .owner
= THIS_MODULE
,
269 .release
= rawsock_release
,
270 .bind
= sock_no_bind
,
271 .connect
= rawsock_connect
,
272 .socketpair
= sock_no_socketpair
,
273 .accept
= sock_no_accept
,
274 .getname
= sock_no_getname
,
275 .poll
= datagram_poll
,
276 .ioctl
= sock_no_ioctl
,
277 .listen
= sock_no_listen
,
278 .shutdown
= sock_no_shutdown
,
279 .sendmsg
= rawsock_sendmsg
,
280 .recvmsg
= rawsock_recvmsg
,
281 .mmap
= sock_no_mmap
,
284 static const struct proto_ops rawsock_raw_ops
= {
286 .owner
= THIS_MODULE
,
287 .release
= rawsock_release
,
288 .bind
= sock_no_bind
,
289 .connect
= sock_no_connect
,
290 .socketpair
= sock_no_socketpair
,
291 .accept
= sock_no_accept
,
292 .getname
= sock_no_getname
,
293 .poll
= datagram_poll
,
294 .ioctl
= sock_no_ioctl
,
295 .listen
= sock_no_listen
,
296 .shutdown
= sock_no_shutdown
,
297 .sendmsg
= sock_no_sendmsg
,
298 .recvmsg
= rawsock_recvmsg
,
299 .mmap
= sock_no_mmap
,
302 static void rawsock_destruct(struct sock
*sk
)
304 pr_debug("sk=%p\n", sk
);
306 if (sk
->sk_state
== TCP_ESTABLISHED
) {
307 nfc_deactivate_target(nfc_rawsock(sk
)->dev
,
308 nfc_rawsock(sk
)->target_idx
,
309 NFC_TARGET_MODE_IDLE
);
310 nfc_put_device(nfc_rawsock(sk
)->dev
);
313 skb_queue_purge(&sk
->sk_receive_queue
);
315 if (!sock_flag(sk
, SOCK_DEAD
)) {
316 pr_err("Freeing alive NFC raw socket %p\n", sk
);
321 static int rawsock_create(struct net
*net
, struct socket
*sock
,
322 const struct nfc_protocol
*nfc_proto
, int kern
)
326 pr_debug("sock=%p\n", sock
);
328 if ((sock
->type
!= SOCK_SEQPACKET
) && (sock
->type
!= SOCK_RAW
))
329 return -ESOCKTNOSUPPORT
;
331 if (sock
->type
== SOCK_RAW
) {
332 if (!capable(CAP_NET_RAW
))
334 sock
->ops
= &rawsock_raw_ops
;
336 sock
->ops
= &rawsock_ops
;
339 sk
= sk_alloc(net
, PF_NFC
, GFP_ATOMIC
, nfc_proto
->proto
, kern
);
343 sock_init_data(sock
, sk
);
344 sk
->sk_protocol
= nfc_proto
->id
;
345 sk
->sk_destruct
= rawsock_destruct
;
346 sock
->state
= SS_UNCONNECTED
;
347 if (sock
->type
== SOCK_RAW
)
348 nfc_sock_link(&raw_sk_list
, sk
);
350 INIT_WORK(&nfc_rawsock(sk
)->tx_work
, rawsock_tx_work
);
351 nfc_rawsock(sk
)->tx_work_scheduled
= false;
357 void nfc_send_to_raw_sock(struct nfc_dev
*dev
, struct sk_buff
*skb
,
358 u8 payload_type
, u8 direction
)
360 struct sk_buff
*skb_copy
= NULL
, *nskb
;
364 read_lock(&raw_sk_list
.lock
);
366 sk_for_each(sk
, &raw_sk_list
.head
) {
368 skb_copy
= __pskb_copy_fclone(skb
, NFC_RAW_HEADER_SIZE
,
373 data
= skb_push(skb_copy
, NFC_RAW_HEADER_SIZE
);
375 data
[0] = dev
? dev
->idx
: 0xFF;
376 data
[1] = direction
& 0x01;
377 data
[1] |= (payload_type
<< 1);
380 nskb
= skb_clone(skb_copy
, GFP_ATOMIC
);
384 if (sock_queue_rcv_skb(sk
, nskb
))
388 read_unlock(&raw_sk_list
.lock
);
392 EXPORT_SYMBOL(nfc_send_to_raw_sock
);
394 static struct proto rawsock_proto
= {
396 .owner
= THIS_MODULE
,
397 .obj_size
= sizeof(struct nfc_rawsock
),
400 static const struct nfc_protocol rawsock_nfc_proto
= {
401 .id
= NFC_SOCKPROTO_RAW
,
402 .proto
= &rawsock_proto
,
403 .owner
= THIS_MODULE
,
404 .create
= rawsock_create
407 int __init
rawsock_init(void)
411 rc
= nfc_proto_register(&rawsock_nfc_proto
);
416 void rawsock_exit(void)
418 nfc_proto_unregister(&rawsock_nfc_proto
);