^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 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/nfc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "nfc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "llcp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int sock_wait_state(struct sock *sk, int state, unsigned long timeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) pr_debug("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) add_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) while (sk->sk_state != state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (!timeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) err = -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) err = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) timeo = schedule_timeout(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) err = sock_error(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static struct proto llcp_sock_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .name = "NFC_LLCP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .obj_size = sizeof(struct nfc_llcp_sock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int llcp_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct nfc_llcp_local *local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct nfc_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct sockaddr_nfc_llcp llcp_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int len, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!addr || alen < offsetofend(struct sockaddr, sa_family) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) addr->sa_family != AF_NFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) memset(&llcp_addr, 0, sizeof(llcp_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) len = min_t(unsigned int, sizeof(llcp_addr), alen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) memcpy(&llcp_addr, addr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* This is going to be a listening socket, dsap must be 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (llcp_addr.dsap != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (sk->sk_state != LLCP_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ret = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dev = nfc_get_device(llcp_addr.dev_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (dev == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) local = nfc_llcp_find_local(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (local == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) llcp_sock->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) llcp_sock->local = nfc_llcp_local_get(local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) llcp_sock->service_name_len = min_t(unsigned int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) llcp_addr.service_name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) NFC_LLCP_MAX_SERVICE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) llcp_sock->service_name = kmemdup(llcp_addr.service_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) llcp_sock->service_name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!llcp_sock->service_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) nfc_llcp_local_put(llcp_sock->local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) llcp_sock->local = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) llcp_sock->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) llcp_sock->ssap = nfc_llcp_get_sdp_ssap(local, llcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (llcp_sock->ssap == LLCP_SAP_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) nfc_llcp_local_put(llcp_sock->local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) llcp_sock->local = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) kfree(llcp_sock->service_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) llcp_sock->service_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) llcp_sock->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ret = -EADDRINUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) llcp_sock->reserved_ssap = llcp_sock->ssap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) nfc_llcp_sock_link(&local->sockets, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sk->sk_state = LLCP_BOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) put_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) nfc_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int llcp_raw_sock_bind(struct socket *sock, struct sockaddr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int alen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct nfc_llcp_local *local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct nfc_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct sockaddr_nfc_llcp llcp_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int len, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!addr || alen < offsetofend(struct sockaddr, sa_family) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) addr->sa_family != AF_NFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) memset(&llcp_addr, 0, sizeof(llcp_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) len = min_t(unsigned int, sizeof(llcp_addr), alen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) memcpy(&llcp_addr, addr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (sk->sk_state != LLCP_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) dev = nfc_get_device(llcp_addr.dev_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (dev == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) local = nfc_llcp_find_local(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (local == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) llcp_sock->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) llcp_sock->local = nfc_llcp_local_get(local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) nfc_llcp_sock_link(&local->raw_sockets, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) sk->sk_state = LLCP_BOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) put_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) nfc_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int llcp_sock_listen(struct socket *sock, int backlog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) pr_debug("sk %p backlog %d\n", sk, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) sk->sk_state != LLCP_BOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ret = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) sk->sk_max_ack_backlog = backlog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) sk->sk_ack_backlog = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) pr_debug("Socket listening\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) sk->sk_state = LLCP_LISTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) u32 opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) pr_debug("%p optname %d\n", sk, optname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (level != SOL_NFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) case NFC_LLCP_RW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (sk->sk_state == LLCP_CONNECTED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) sk->sk_state == LLCP_BOUND ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) sk->sk_state == LLCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (opt > LLCP_MAX_RW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) llcp_sock->rw = (u8) opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) case NFC_LLCP_MIUX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (sk->sk_state == LLCP_CONNECTED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) sk->sk_state == LLCP_BOUND ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) sk->sk_state == LLCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (opt > LLCP_MAX_MIUX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) llcp_sock->miux = cpu_to_be16((u16) opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) pr_debug("%p rw %d miux %d\n", llcp_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) llcp_sock->rw, llcp_sock->miux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) char __user *optval, int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct nfc_llcp_local *local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int len, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) u16 miux, remote_miu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) u8 rw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) pr_debug("%p optname %d\n", sk, optname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (level != SOL_NFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (get_user(len, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) local = llcp_sock->local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (!local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) len = min_t(u32, len, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) case NFC_LLCP_RW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (put_user(rw, (u32 __user *) optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) case NFC_LLCP_MIUX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (put_user(miux, (u32 __user *) optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) case NFC_LLCP_REMOTE_MIU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) local->remote_miu : llcp_sock->remote_miu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (put_user(remote_miu, (u32 __user *) optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) case NFC_LLCP_REMOTE_LTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (put_user(local->remote_lto / 10, (u32 __user *) optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) case NFC_LLCP_REMOTE_RW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (put_user(llcp_sock->remote_rw, (u32 __user *) optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (put_user(len, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) void nfc_llcp_accept_unlink(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) pr_debug("state %d\n", sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) list_del_init(&llcp_sock->accept_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) sk_acceptq_removed(llcp_sock->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) llcp_sock->parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /* Lock will be free from unlink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) list_add_tail(&llcp_sock->accept_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) &llcp_sock_parent->accept_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) llcp_sock->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) sk_acceptq_added(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) struct socket *newsock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) struct nfc_llcp_sock *lsk, *n, *llcp_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) llcp_parent = nfc_llcp_sock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) accept_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) sk = &lsk->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (sk->sk_state == LLCP_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) nfc_llcp_accept_unlink(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (sk->sk_state == LLCP_CONNECTED || !newsock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) list_del_init(&lsk->accept_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (newsock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) sock_graft(sk, newsock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) pr_debug("Returning sk state %d\n", sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) sk_acceptq_removed(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) int flags, bool kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct sock *sk = sock->sk, *new_sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) pr_debug("parent %p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (sk->sk_state != LLCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) ret = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* Wait for an incoming connection. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) add_wait_queue_exclusive(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (!timeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) ret = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) timeo = schedule_timeout(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) newsock->state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) pr_debug("new socket %p\n", new_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) int peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (llcp_sock == NULL || llcp_sock->dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) llcp_sock->dsap, llcp_sock->ssap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) memset(llcp_addr, 0, sizeof(*llcp_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (!llcp_sock->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) llcp_addr->sa_family = AF_NFC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) llcp_addr->dev_idx = llcp_sock->dev->idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) llcp_addr->target_idx = llcp_sock->target_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) llcp_addr->nfc_protocol = llcp_sock->nfc_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) llcp_addr->dsap = llcp_sock->dsap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) llcp_addr->ssap = llcp_sock->ssap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) llcp_addr->service_name_len = llcp_sock->service_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) memcpy(llcp_addr->service_name, llcp_sock->service_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) llcp_addr->service_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return sizeof(struct sockaddr_nfc_llcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static inline __poll_t llcp_accept_poll(struct sock *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) struct nfc_llcp_sock *llcp_sock, *parent_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) parent_sock = nfc_llcp_sock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) list_for_each_entry(llcp_sock, &parent_sock->accept_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) accept_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) sk = &llcp_sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (sk->sk_state == LLCP_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static __poll_t llcp_sock_poll(struct file *file, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) __poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) pr_debug("%p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) sock_poll_wait(file, sock, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (sk->sk_state == LLCP_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return llcp_accept_poll(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) mask |= EPOLLERR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (sk->sk_state == LLCP_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (sk->sk_shutdown & RCV_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (sk->sk_shutdown == SHUTDOWN_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) pr_debug("mask 0x%x\n", mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static int llcp_sock_release(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) struct nfc_llcp_local *local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) pr_debug("%p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) local = llcp_sock->local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) if (local == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /* Send a DISC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (sk->sk_state == LLCP_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) nfc_llcp_send_disconnect(llcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (sk->sk_state == LLCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) struct nfc_llcp_sock *lsk, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct sock *accept_sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) accept_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) accept_sk = &lsk->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) lock_sock(accept_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) nfc_llcp_send_disconnect(lsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) nfc_llcp_accept_unlink(accept_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) release_sock(accept_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) /* Keep this sock alive and therefore do not remove it from the sockets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * list until the DISC PDU has been actually sent. Otherwise we would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * reply with DM PDUs before sending the DISC one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (sk->sk_state == LLCP_DISCONNECTING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (sock->type == SOCK_RAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) nfc_llcp_sock_unlink(&local->raw_sockets, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) nfc_llcp_sock_unlink(&local->sockets, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) sock_orphan(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) int len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) struct nfc_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) struct nfc_llcp_local *local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (!addr || len < sizeof(*addr) || addr->sa_family != AF_NFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (addr->service_name_len == 0 && addr->dsap == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) addr->target_idx, addr->nfc_protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (sk->sk_state == LLCP_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) ret = -EISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (sk->sk_state == LLCP_CONNECTING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) ret = -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) dev = nfc_get_device(addr->dev_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (dev == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) local = nfc_llcp_find_local(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (local == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) device_lock(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (dev->dep_link_up == false) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) ret = -ENOLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) device_unlock(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) device_unlock(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (local->rf_mode == NFC_RF_INITIATOR &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) addr->target_idx != local->target_idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) ret = -ENOLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) llcp_sock->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) llcp_sock->local = nfc_llcp_local_get(local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (llcp_sock->ssap == LLCP_SAP_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) nfc_llcp_local_put(llcp_sock->local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) llcp_sock->local = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) goto put_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) llcp_sock->reserved_ssap = llcp_sock->ssap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) if (addr->service_name_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) llcp_sock->dsap = addr->dsap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) llcp_sock->dsap = LLCP_SAP_SDP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) llcp_sock->nfc_protocol = addr->nfc_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) llcp_sock->service_name_len = min_t(unsigned int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) addr->service_name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) NFC_LLCP_MAX_SERVICE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) llcp_sock->service_name = kmemdup(addr->service_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) llcp_sock->service_name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (!llcp_sock->service_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) goto sock_llcp_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) nfc_llcp_sock_link(&local->connecting_sockets, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) ret = nfc_llcp_send_connect(llcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) goto sock_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) sk->sk_state = LLCP_CONNECTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) ret = sock_wait_state(sk, LLCP_CONNECTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) sock_sndtimeo(sk, flags & O_NONBLOCK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (ret && ret != -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) goto sock_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) sock_unlink:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) kfree(llcp_sock->service_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) llcp_sock->service_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) sock_llcp_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) nfc_llcp_put_ssap(local, llcp_sock->ssap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) nfc_llcp_local_put(llcp_sock->local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) llcp_sock->local = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) put_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) nfc_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) pr_debug("sock %p sk %p", sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) ret = sock_error(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (msg->msg_flags & MSG_OOB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (!llcp_sock->local) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (sk->sk_type == SOCK_DGRAM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) msg->msg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (msg->msg_namelen < sizeof(*addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) msg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (sk->sk_state != LLCP_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return nfc_llcp_send_i_frame(llcp_sock, msg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) static int llcp_sock_recvmsg(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) size_t len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) int noblock = flags & MSG_DONTWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) unsigned int copied, rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) struct sk_buff *skb, *cskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) pr_debug("%p %zu\n", sk, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) if (sk->sk_state == LLCP_CLOSED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) skb_queue_empty(&sk->sk_receive_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (flags & (MSG_OOB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) skb = skb_recv_datagram(sk, flags, noblock, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) pr_err("Recv datagram failed state %d %d %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) sk->sk_state, err, sock_error(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (sk->sk_shutdown & RCV_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) rlen = skb->len; /* real length of skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) copied = min_t(unsigned int, rlen, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) cskb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) if (skb_copy_datagram_msg(cskb, 0, msg, copied)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (!(flags & MSG_PEEK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) skb_queue_head(&sk->sk_receive_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) sock_recv_timestamp(msg, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, sockaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) msg->msg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) memset(sockaddr, 0, sizeof(*sockaddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) sockaddr->sa_family = AF_NFC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) sockaddr->dsap = ui_cb->dsap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) sockaddr->ssap = ui_cb->ssap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /* Mark read part of skb as used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) if (!(flags & MSG_PEEK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) /* SOCK_STREAM: re-queue skb if it contains unreceived data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (sk->sk_type == SOCK_STREAM ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) sk->sk_type == SOCK_DGRAM ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) sk->sk_type == SOCK_RAW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) skb_pull(skb, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) skb_queue_head(&sk->sk_receive_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) /* XXX Queue backlogged skbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) copied = rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) static const struct proto_ops llcp_sock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) .family = PF_NFC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) .bind = llcp_sock_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) .connect = llcp_sock_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) .release = llcp_sock_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) .accept = llcp_sock_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) .getname = llcp_sock_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) .poll = llcp_sock_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) .ioctl = sock_no_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) .listen = llcp_sock_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) .shutdown = sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) .setsockopt = nfc_llcp_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) .getsockopt = nfc_llcp_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) .sendmsg = llcp_sock_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) .recvmsg = llcp_sock_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) static const struct proto_ops llcp_rawsock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) .family = PF_NFC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) .bind = llcp_raw_sock_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) .connect = sock_no_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) .release = llcp_sock_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) .accept = sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) .getname = llcp_sock_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) .poll = llcp_sock_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) .ioctl = sock_no_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) .listen = sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) .shutdown = sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) .sendmsg = sock_no_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) .recvmsg = llcp_sock_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) static void llcp_sock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) pr_debug("%p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) if (sk->sk_state == LLCP_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) nfc_put_device(llcp_sock->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) skb_queue_purge(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) nfc_llcp_sock_free(llcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (!sock_flag(sk, SOCK_DEAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) pr_err("Freeing alive NFC LLCP socket %p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp, int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) struct nfc_llcp_sock *llcp_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) llcp_sock = nfc_llcp_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) sock_init_data(sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) sk->sk_state = LLCP_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) sk->sk_protocol = NFC_SOCKPROTO_LLCP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) sk->sk_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) sk->sk_destruct = llcp_sock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) llcp_sock->ssap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) llcp_sock->dsap = LLCP_SAP_SDP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) llcp_sock->rw = LLCP_MAX_RW + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) llcp_sock->send_n = llcp_sock->send_ack_n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) llcp_sock->recv_n = llcp_sock->recv_ack_n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) llcp_sock->remote_ready = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) llcp_sock->reserved_ssap = LLCP_SAP_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) nfc_llcp_socket_remote_param_init(llcp_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) skb_queue_head_init(&llcp_sock->tx_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) skb_queue_head_init(&llcp_sock->tx_pending_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) INIT_LIST_HEAD(&llcp_sock->accept_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (sock != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) sock->state = SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) kfree(sock->service_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) skb_queue_purge(&sock->tx_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) skb_queue_purge(&sock->tx_pending_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) list_del_init(&sock->accept_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) sock->parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) nfc_llcp_local_put(sock->local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) static int llcp_sock_create(struct net *net, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) const struct nfc_protocol *nfc_proto, int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) pr_debug("%p\n", sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (sock->type != SOCK_STREAM &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) sock->type != SOCK_DGRAM &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) sock->type != SOCK_RAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) return -ESOCKTNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (sock->type == SOCK_RAW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (!capable(CAP_NET_RAW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) sock->ops = &llcp_rawsock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) sock->ops = &llcp_sock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (sk == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) static const struct nfc_protocol llcp_nfc_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) .id = NFC_SOCKPROTO_LLCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) .proto = &llcp_sock_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) .create = llcp_sock_create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) int __init nfc_llcp_sock_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) return nfc_proto_register(&llcp_nfc_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) void nfc_llcp_sock_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) nfc_proto_unregister(&llcp_nfc_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }