linux/net/nfc/rawsock.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
   3 *
   4 * Authors:
   5 *    Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
   6 *    Lauro Ramos Venancio <lauro.venancio@openbossa.org>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the
  20 * Free Software Foundation, Inc.,
  21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22 */
  23
  24#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  25
  26#include <net/tcp_states.h>
  27#include <linux/nfc.h>
  28#include <linux/export.h>
  29
  30#include "nfc.h"
  31
  32static void rawsock_write_queue_purge(struct sock *sk)
  33{
  34        pr_debug("sk=%p\n", sk);
  35
  36        spin_lock_bh(&sk->sk_write_queue.lock);
  37        __skb_queue_purge(&sk->sk_write_queue);
  38        nfc_rawsock(sk)->tx_work_scheduled = false;
  39        spin_unlock_bh(&sk->sk_write_queue.lock);
  40}
  41
  42static void rawsock_report_error(struct sock *sk, int err)
  43{
  44        pr_debug("sk=%p err=%d\n", sk, err);
  45
  46        sk->sk_shutdown = SHUTDOWN_MASK;
  47        sk->sk_err = -err;
  48        sk->sk_error_report(sk);
  49
  50        rawsock_write_queue_purge(sk);
  51}
  52
  53static int rawsock_release(struct socket *sock)
  54{
  55        struct sock *sk = sock->sk;
  56
  57        pr_debug("sock=%p\n", sock);
  58
  59        sock_orphan(sk);
  60        sock_put(sk);
  61
  62        return 0;
  63}
  64
  65static int rawsock_connect(struct socket *sock, struct sockaddr *_addr,
  66                                                        int len, int flags)
  67{
  68        struct sock *sk = sock->sk;
  69        struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr;
  70        struct nfc_dev *dev;
  71        int rc = 0;
  72
  73        pr_debug("sock=%p sk=%p flags=%d\n", sock, sk, flags);
  74
  75        if (!addr || len < sizeof(struct sockaddr_nfc) ||
  76                addr->sa_family != AF_NFC)
  77                return -EINVAL;
  78
  79        pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n",
  80                 addr->dev_idx, addr->target_idx, addr->nfc_protocol);
  81
  82        lock_sock(sk);
  83
  84        if (sock->state == SS_CONNECTED) {
  85                rc = -EISCONN;
  86                goto error;
  87        }
  88
  89        dev = nfc_get_device(addr->dev_idx);
  90        if (!dev) {
  91                rc = -ENODEV;
  92                goto error;
  93        }
  94
  95        if (addr->target_idx > dev->target_idx - 1 ||
  96                addr->target_idx < dev->target_idx - dev->n_targets) {
  97                rc = -EINVAL;
  98                goto error;
  99        }
 100
 101        if (addr->target_idx > dev->target_idx - 1 ||
 102                addr->target_idx < dev->target_idx - dev->n_targets) {
 103                rc = -EINVAL;
 104                goto error;
 105        }
 106
 107        rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol);
 108        if (rc)
 109                goto put_dev;
 110
 111        nfc_rawsock(sk)->dev = dev;
 112        nfc_rawsock(sk)->target_idx = addr->target_idx;
 113        sock->state = SS_CONNECTED;
 114        sk->sk_state = TCP_ESTABLISHED;
 115        sk->sk_state_change(sk);
 116
 117        release_sock(sk);
 118        return 0;
 119
 120put_dev:
 121        nfc_put_device(dev);
 122error:
 123        release_sock(sk);
 124        return rc;
 125}
 126
 127static int rawsock_add_header(struct sk_buff *skb)
 128{
 129        *skb_push(skb, NFC_HEADER_SIZE) = 0;
 130
 131        return 0;
 132}
 133
 134static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
 135                                                                int err)
 136{
 137        struct sock *sk = (struct sock *) context;
 138
 139        BUG_ON(in_irq());
 140
 141        pr_debug("sk=%p err=%d\n", sk, err);
 142
 143        if (err)
 144                goto error;
 145
 146        err = rawsock_add_header(skb);
 147        if (err)
 148                goto error;
 149
 150        err = sock_queue_rcv_skb(sk, skb);
 151        if (err)
 152                goto error;
 153
 154        spin_lock_bh(&sk->sk_write_queue.lock);
 155        if (!skb_queue_empty(&sk->sk_write_queue))
 156                schedule_work(&nfc_rawsock(sk)->tx_work);
 157        else
 158                nfc_rawsock(sk)->tx_work_scheduled = false;
 159        spin_unlock_bh(&sk->sk_write_queue.lock);
 160
 161        sock_put(sk);
 162        return;
 163
 164error:
 165        rawsock_report_error(sk, err);
 166        sock_put(sk);
 167}
 168
 169static void rawsock_tx_work(struct work_struct *work)
 170{
 171        struct sock *sk = to_rawsock_sk(work);
 172        struct nfc_dev *dev = nfc_rawsock(sk)->dev;
 173        u32 target_idx = nfc_rawsock(sk)->target_idx;
 174        struct sk_buff *skb;
 175        int rc;
 176
 177        pr_debug("sk=%p target_idx=%u\n", sk, target_idx);
 178
 179        if (sk->sk_shutdown & SEND_SHUTDOWN) {
 180                rawsock_write_queue_purge(sk);
 181                return;
 182        }
 183
 184        skb = skb_dequeue(&sk->sk_write_queue);
 185
 186        sock_hold(sk);
 187        rc = nfc_data_exchange(dev, target_idx, skb,
 188                                rawsock_data_exchange_complete, sk);
 189        if (rc) {
 190                rawsock_report_error(sk, rc);
 191                sock_put(sk);
 192        }
 193}
 194
 195static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
 196                                        struct msghdr *msg, size_t len)
 197{
 198        struct sock *sk = sock->sk;
 199        struct nfc_dev *dev = nfc_rawsock(sk)->dev;
 200        struct sk_buff *skb;
 201        int rc;
 202
 203        pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len);
 204
 205        if (msg->msg_namelen)
 206                return -EOPNOTSUPP;
 207
 208        if (sock->state != SS_CONNECTED)
 209                return -ENOTCONN;
 210
 211        skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
 212        if (skb == NULL)
 213                return rc;
 214
 215        rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
 216        if (rc < 0) {
 217                kfree_skb(skb);
 218                return rc;
 219        }
 220
 221        spin_lock_bh(&sk->sk_write_queue.lock);
 222        __skb_queue_tail(&sk->sk_write_queue, skb);
 223        if (!nfc_rawsock(sk)->tx_work_scheduled) {
 224                schedule_work(&nfc_rawsock(sk)->tx_work);
 225                nfc_rawsock(sk)->tx_work_scheduled = true;
 226        }
 227        spin_unlock_bh(&sk->sk_write_queue.lock);
 228
 229        return len;
 230}
 231
 232static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock,
 233                                struct msghdr *msg, size_t len, int flags)
 234{
 235        int noblock = flags & MSG_DONTWAIT;
 236        struct sock *sk = sock->sk;
 237        struct sk_buff *skb;
 238        int copied;
 239        int rc;
 240
 241        pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock, sk, len, flags);
 242
 243        skb = skb_recv_datagram(sk, flags, noblock, &rc);
 244        if (!skb)
 245                return rc;
 246
 247        msg->msg_namelen = 0;
 248
 249        copied = skb->len;
 250        if (len < copied) {
 251                msg->msg_flags |= MSG_TRUNC;
 252                copied = len;
 253        }
 254
 255        rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
 256
 257        skb_free_datagram(sk, skb);
 258
 259        return rc ? : copied;
 260}
 261
 262
 263static const struct proto_ops rawsock_ops = {
 264        .family         = PF_NFC,
 265        .owner          = THIS_MODULE,
 266        .release        = rawsock_release,
 267        .bind           = sock_no_bind,
 268        .connect        = rawsock_connect,
 269        .socketpair     = sock_no_socketpair,
 270        .accept         = sock_no_accept,
 271        .getname        = sock_no_getname,
 272        .poll           = datagram_poll,
 273        .ioctl          = sock_no_ioctl,
 274        .listen         = sock_no_listen,
 275        .shutdown       = sock_no_shutdown,
 276        .setsockopt     = sock_no_setsockopt,
 277        .getsockopt     = sock_no_getsockopt,
 278        .sendmsg        = rawsock_sendmsg,
 279        .recvmsg        = rawsock_recvmsg,
 280        .mmap           = sock_no_mmap,
 281};
 282
 283static void rawsock_destruct(struct sock *sk)
 284{
 285        pr_debug("sk=%p\n", sk);
 286
 287        if (sk->sk_state == TCP_ESTABLISHED) {
 288                nfc_deactivate_target(nfc_rawsock(sk)->dev,
 289                                        nfc_rawsock(sk)->target_idx);
 290                nfc_put_device(nfc_rawsock(sk)->dev);
 291        }
 292
 293        skb_queue_purge(&sk->sk_receive_queue);
 294
 295        if (!sock_flag(sk, SOCK_DEAD)) {
 296                pr_err("Freeing alive NFC raw socket %p\n", sk);
 297                return;
 298        }
 299}
 300
 301static int rawsock_create(struct net *net, struct socket *sock,
 302                                const struct nfc_protocol *nfc_proto)
 303{
 304        struct sock *sk;
 305
 306        pr_debug("sock=%p\n", sock);
 307
 308        if (sock->type != SOCK_SEQPACKET)
 309                return -ESOCKTNOSUPPORT;
 310
 311        sock->ops = &rawsock_ops;
 312
 313        sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto);
 314        if (!sk)
 315                return -ENOMEM;
 316
 317        sock_init_data(sock, sk);
 318        sk->sk_protocol = nfc_proto->id;
 319        sk->sk_destruct = rawsock_destruct;
 320        sock->state = SS_UNCONNECTED;
 321
 322        INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
 323        nfc_rawsock(sk)->tx_work_scheduled = false;
 324
 325        return 0;
 326}
 327
 328static struct proto rawsock_proto = {
 329        .name     = "NFC_RAW",
 330        .owner    = THIS_MODULE,
 331        .obj_size = sizeof(struct nfc_rawsock),
 332};
 333
 334static const struct nfc_protocol rawsock_nfc_proto = {
 335        .id       = NFC_SOCKPROTO_RAW,
 336        .proto    = &rawsock_proto,
 337        .owner    = THIS_MODULE,
 338        .create   = rawsock_create
 339};
 340
 341int __init rawsock_init(void)
 342{
 343        int rc;
 344
 345        rc = nfc_proto_register(&rawsock_nfc_proto);
 346
 347        return rc;
 348}
 349
 350void rawsock_exit(void)
 351{
 352        nfc_proto_unregister(&rawsock_nfc_proto);
 353}
 354