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