^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2011 Instituto Nokia de Tecnologia
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/tcp_states.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/nfc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "nfc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static struct nfc_sock_list raw_sk_list = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) .lock = __RW_LOCK_UNLOCKED(raw_sk_list.lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void nfc_sock_link(struct nfc_sock_list *l, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) write_lock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) sk_add_node(sk, &l->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) write_unlock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static void nfc_sock_unlink(struct nfc_sock_list *l, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) write_lock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sk_del_node_init(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) write_unlock(&l->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void rawsock_write_queue_purge(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) pr_debug("sk=%p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) spin_lock_bh(&sk->sk_write_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) __skb_queue_purge(&sk->sk_write_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) nfc_rawsock(sk)->tx_work_scheduled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) spin_unlock_bh(&sk->sk_write_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void rawsock_report_error(struct sock *sk, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pr_debug("sk=%p err=%d\n", sk, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) sk->sk_shutdown = SHUTDOWN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sk->sk_err = -err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) rawsock_write_queue_purge(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int rawsock_release(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pr_debug("sock=%p sk=%p\n", sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (sock->type == SOCK_RAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) nfc_sock_unlink(&raw_sk_list, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) sock_orphan(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int rawsock_connect(struct socket *sock, struct sockaddr *_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct nfc_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pr_debug("sock=%p sk=%p flags=%d\n", sock, sk, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!addr || len < sizeof(struct sockaddr_nfc) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) addr->sa_family != AF_NFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) addr->dev_idx, addr->target_idx, addr->nfc_protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (sock->state == SS_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) rc = -EISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) dev = nfc_get_device(addr->dev_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (addr->target_idx > dev->target_next_idx - 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) addr->target_idx < dev->target_next_idx - dev->n_targets) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) nfc_rawsock(sk)->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) nfc_rawsock(sk)->target_idx = addr->target_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) sock->state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) sk->sk_state = TCP_ESTABLISHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) put_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) nfc_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int rawsock_add_header(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) *(u8 *)skb_push(skb, NFC_HEADER_SIZE) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct sock *sk = (struct sock *) context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) BUG_ON(in_irq());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) pr_debug("sk=%p err=%d\n", sk, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) err = rawsock_add_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) goto error_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) err = sock_queue_rcv_skb(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto error_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) spin_lock_bh(&sk->sk_write_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!skb_queue_empty(&sk->sk_write_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) schedule_work(&nfc_rawsock(sk)->tx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) nfc_rawsock(sk)->tx_work_scheduled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) spin_unlock_bh(&sk->sk_write_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) error_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) rawsock_report_error(sk, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void rawsock_tx_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct sock *sk = to_rawsock_sk(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct nfc_dev *dev = nfc_rawsock(sk)->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) u32 target_idx = nfc_rawsock(sk)->target_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) pr_debug("sk=%p target_idx=%u\n", sk, target_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (sk->sk_shutdown & SEND_SHUTDOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) rawsock_write_queue_purge(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) skb = skb_dequeue(&sk->sk_write_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) rc = nfc_data_exchange(dev, target_idx, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) rawsock_data_exchange_complete, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) rawsock_report_error(sk, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct nfc_dev *dev = nfc_rawsock(sk)->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (msg->msg_namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (sock->state != SS_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) rc = memcpy_from_msg(skb_put(skb, len), msg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) spin_lock_bh(&sk->sk_write_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) __skb_queue_tail(&sk->sk_write_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!nfc_rawsock(sk)->tx_work_scheduled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) schedule_work(&nfc_rawsock(sk)->tx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) nfc_rawsock(sk)->tx_work_scheduled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) spin_unlock_bh(&sk->sk_write_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int rawsock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int noblock = flags & MSG_DONTWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock, sk, len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) skb = skb_recv_datagram(sk, flags, noblock, &rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) copied = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (len < copied) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) msg->msg_flags |= MSG_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) copied = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) rc = skb_copy_datagram_msg(skb, 0, msg, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) skb_free_datagram(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return rc ? : copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static const struct proto_ops rawsock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) .family = PF_NFC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) .release = rawsock_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) .bind = sock_no_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) .connect = rawsock_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) .accept = sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) .getname = sock_no_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) .poll = datagram_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) .ioctl = sock_no_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) .listen = sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) .shutdown = sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) .sendmsg = rawsock_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) .recvmsg = rawsock_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static const struct proto_ops rawsock_raw_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .family = PF_NFC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .release = rawsock_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) .bind = sock_no_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) .connect = sock_no_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) .accept = sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .getname = sock_no_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .poll = datagram_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .ioctl = sock_no_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) .listen = sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .shutdown = sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) .sendmsg = sock_no_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) .recvmsg = rawsock_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void rawsock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) pr_debug("sk=%p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (sk->sk_state == TCP_ESTABLISHED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) nfc_deactivate_target(nfc_rawsock(sk)->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) nfc_rawsock(sk)->target_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) NFC_TARGET_MODE_IDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) nfc_put_device(nfc_rawsock(sk)->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) skb_queue_purge(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!sock_flag(sk, SOCK_DEAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) pr_err("Freeing alive NFC raw socket %p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static int rawsock_create(struct net *net, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) const struct nfc_protocol *nfc_proto, int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) pr_debug("sock=%p\n", sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if ((sock->type != SOCK_SEQPACKET) && (sock->type != SOCK_RAW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return -ESOCKTNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (sock->type == SOCK_RAW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (!ns_capable(net->user_ns, CAP_NET_RAW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) sock->ops = &rawsock_raw_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) sock->ops = &rawsock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) sock_init_data(sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) sk->sk_protocol = nfc_proto->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) sk->sk_destruct = rawsock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) sock->state = SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (sock->type == SOCK_RAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) nfc_sock_link(&raw_sk_list, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) nfc_rawsock(sk)->tx_work_scheduled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) u8 payload_type, u8 direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct sk_buff *skb_copy = NULL, *nskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) read_lock(&raw_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) sk_for_each(sk, &raw_sk_list.head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (!skb_copy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) skb_copy = __pskb_copy_fclone(skb, NFC_RAW_HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) GFP_ATOMIC, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!skb_copy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) data[0] = dev ? dev->idx : 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) data[1] = direction & 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) data[1] |= (payload_type << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) nskb = skb_clone(skb_copy, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (!nskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (sock_queue_rcv_skb(sk, nskb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) kfree_skb(nskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) read_unlock(&raw_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) kfree_skb(skb_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) EXPORT_SYMBOL(nfc_send_to_raw_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static struct proto rawsock_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) .name = "NFC_RAW",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) .obj_size = sizeof(struct nfc_rawsock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static const struct nfc_protocol rawsock_nfc_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .id = NFC_SOCKPROTO_RAW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .proto = &rawsock_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) .create = rawsock_create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) int __init rawsock_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) rc = nfc_proto_register(&rawsock_nfc_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) void rawsock_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) nfc_proto_unregister(&rawsock_nfc_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }