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