1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2011 Intel Corporation. All rights reserved. 4 * Copyright (C) 2014 Marvell International Ltd. 5 */ 6 7 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__ 8 9 #include <linux/init.h> 10 #include <linux/kernel.h> 11 #include <linux/list.h> 12 #include <linux/nfc.h> 13 14 #include "nfc.h" 15 #include "llcp.h" 16 17 static u8 llcp_magic[3] = {0x46, 0x66, 0x6d}; 18 19 static LIST_HEAD(llcp_devices); 20 21 static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb); 22 23 void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk) 24 { 25 write_lock(&l->lock); 26 sk_add_node(sk, &l->head); 27 write_unlock(&l->lock); 28 } 29 30 void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk) 31 { 32 write_lock(&l->lock); 33 sk_del_node_init(sk); 34 write_unlock(&l->lock); 35 } 36 37 void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock) 38 { 39 sock->remote_rw = LLCP_DEFAULT_RW; 40 sock->remote_miu = LLCP_MAX_MIU + 1; 41 } 42 43 static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock) 44 { 45 struct nfc_llcp_local *local = sock->local; 46 struct sk_buff *s, *tmp; 47 48 skb_queue_purge(&sock->tx_queue); 49 skb_queue_purge(&sock->tx_pending_queue); 50 51 if (local == NULL) 52 return; 53 54 /* Search for local pending SKBs that are related to this socket */ 55 skb_queue_walk_safe(&local->tx_queue, s, tmp) { 56 if (s->sk != &sock->sk) 57 continue; 58 59 skb_unlink(s, &local->tx_queue); 60 kfree_skb(s); 61 } 62 } 63 64 static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, 65 int err) 66 { 67 struct sock *sk; 68 struct hlist_node *tmp; 69 struct nfc_llcp_sock *llcp_sock; 70 71 skb_queue_purge(&local->tx_queue); 72 73 write_lock(&local->sockets.lock); 74 75 sk_for_each_safe(sk, tmp, &local->sockets.head) { 76 llcp_sock = nfc_llcp_sock(sk); 77 78 bh_lock_sock(sk); 79 80 nfc_llcp_socket_purge(llcp_sock); 81 82 if (sk->sk_state == LLCP_CONNECTED) 83 nfc_put_device(llcp_sock->dev); 84 85 if (sk->sk_state == LLCP_LISTEN) { 86 struct nfc_llcp_sock *lsk, *n; 87 struct sock *accept_sk; 88 89 list_for_each_entry_safe(lsk, n, 90 &llcp_sock->accept_queue, 91 accept_queue) { 92 accept_sk = &lsk->sk; 93 bh_lock_sock(accept_sk); 94 95 nfc_llcp_accept_unlink(accept_sk); 96 97 if (err) 98 accept_sk->sk_err = err; 99 accept_sk->sk_state = LLCP_CLOSED; 100 accept_sk->sk_state_change(sk); 101 102 bh_unlock_sock(accept_sk); 103 } 104 } 105 106 if (err) 107 sk->sk_err = err; 108 sk->sk_state = LLCP_CLOSED; 109 sk->sk_state_change(sk); 110 111 bh_unlock_sock(sk); 112 113 sk_del_node_init(sk); 114 } 115 116 write_unlock(&local->sockets.lock); 117 118 /* If we still have a device, we keep the RAW sockets alive */ 119 if (device == true) 120 return; 121 122 write_lock(&local->raw_sockets.lock); 123 124 sk_for_each_safe(sk, tmp, &local->raw_sockets.head) { 125 llcp_sock = nfc_llcp_sock(sk); 126 127 bh_lock_sock(sk); 128 129 nfc_llcp_socket_purge(llcp_sock); 130 131 if (err) 132 sk->sk_err = err; 133 sk->sk_state = LLCP_CLOSED; 134 sk->sk_state_change(sk); 135 136 bh_unlock_sock(sk); 137 138 sk_del_node_init(sk); 139 } 140 141 write_unlock(&local->raw_sockets.lock); 142 } 143 144 struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local) 145 { 146 kref_get(&local->ref); 147 148 return local; 149 } 150 151 static void local_cleanup(struct nfc_llcp_local *local) 152 { 153 nfc_llcp_socket_release(local, false, ENXIO); 154 del_timer_sync(&local->link_timer); 155 skb_queue_purge(&local->tx_queue); 156 cancel_work_sync(&local->tx_work); 157 cancel_work_sync(&local->rx_work); 158 cancel_work_sync(&local->timeout_work); 159 kfree_skb(local->rx_pending); 160 del_timer_sync(&local->sdreq_timer); 161 cancel_work_sync(&local->sdreq_timeout_work); 162 nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs); 163 } 164 165 static void local_release(struct kref *ref) 166 { 167 struct nfc_llcp_local *local; 168 169 local = container_of(ref, struct nfc_llcp_local, ref); 170 171 list_del(&local->list); 172 local_cleanup(local); 173 kfree(local); 174 } 175 176 int nfc_llcp_local_put(struct nfc_llcp_local *local) 177 { 178 if (local == NULL) 179 return 0; 180 181 return kref_put(&local->ref, local_release); 182 } 183 184 static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local, 185 u8 ssap, u8 dsap) 186 { 187 struct sock *sk; 188 struct nfc_llcp_sock *llcp_sock, *tmp_sock; 189 190 pr_debug("ssap dsap %d %d\n", ssap, dsap); 191 192 if (ssap == 0 && dsap == 0) 193 return NULL; 194 195 read_lock(&local->sockets.lock); 196 197 llcp_sock = NULL; 198 199 sk_for_each(sk, &local->sockets.head) { 200 tmp_sock = nfc_llcp_sock(sk); 201 202 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) { 203 llcp_sock = tmp_sock; 204 break; 205 } 206 } 207 208 read_unlock(&local->sockets.lock); 209 210 if (llcp_sock == NULL) 211 return NULL; 212 213 sock_hold(&llcp_sock->sk); 214 215 return llcp_sock; 216 } 217 218 static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock) 219 { 220 sock_put(&sock->sk); 221 } 222 223 static void nfc_llcp_timeout_work(struct work_struct *work) 224 { 225 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, 226 timeout_work); 227 228 nfc_dep_link_down(local->dev); 229 } 230 231 static void nfc_llcp_symm_timer(struct timer_list *t) 232 { 233 struct nfc_llcp_local *local = from_timer(local, t, link_timer); 234 235 pr_err("SYMM timeout\n"); 236 237 schedule_work(&local->timeout_work); 238 } 239 240 static void nfc_llcp_sdreq_timeout_work(struct work_struct *work) 241 { 242 unsigned long time; 243 HLIST_HEAD(nl_sdres_list); 244 struct hlist_node *n; 245 struct nfc_llcp_sdp_tlv *sdp; 246 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, 247 sdreq_timeout_work); 248 249 mutex_lock(&local->sdreq_lock); 250 251 time = jiffies - msecs_to_jiffies(3 * local->remote_lto); 252 253 hlist_for_each_entry_safe(sdp, n, &local->pending_sdreqs, node) { 254 if (time_after(sdp->time, time)) 255 continue; 256 257 sdp->sap = LLCP_SDP_UNBOUND; 258 259 hlist_del(&sdp->node); 260 261 hlist_add_head(&sdp->node, &nl_sdres_list); 262 } 263 264 if (!hlist_empty(&local->pending_sdreqs)) 265 mod_timer(&local->sdreq_timer, 266 jiffies + msecs_to_jiffies(3 * local->remote_lto)); 267 268 mutex_unlock(&local->sdreq_lock); 269 270 if (!hlist_empty(&nl_sdres_list)) 271 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list); 272 } 273 274 static void nfc_llcp_sdreq_timer(struct timer_list *t) 275 { 276 struct nfc_llcp_local *local = from_timer(local, t, sdreq_timer); 277 278 schedule_work(&local->sdreq_timeout_work); 279 } 280 281 struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev) 282 { 283 struct nfc_llcp_local *local; 284 285 list_for_each_entry(local, &llcp_devices, list) 286 if (local->dev == dev) 287 return local; 288 289 pr_debug("No device found\n"); 290 291 return NULL; 292 } 293 294 static char *wks[] = { 295 NULL, 296 NULL, /* SDP */ 297 "urn:nfc:sn:ip", 298 "urn:nfc:sn:obex", 299 "urn:nfc:sn:snep", 300 }; 301 302 static int nfc_llcp_wks_sap(const char *service_name, size_t service_name_len) 303 { 304 int sap, num_wks; 305 306 pr_debug("%s\n", service_name); 307 308 if (service_name == NULL) 309 return -EINVAL; 310 311 num_wks = ARRAY_SIZE(wks); 312 313 for (sap = 0; sap < num_wks; sap++) { 314 if (wks[sap] == NULL) 315 continue; 316 317 if (strncmp(wks[sap], service_name, service_name_len) == 0) 318 return sap; 319 } 320 321 return -EINVAL; 322 } 323 324 static 325 struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local, 326 const u8 *sn, size_t sn_len) 327 { 328 struct sock *sk; 329 struct nfc_llcp_sock *llcp_sock, *tmp_sock; 330 331 pr_debug("sn %zd %p\n", sn_len, sn); 332 333 if (sn == NULL || sn_len == 0) 334 return NULL; 335 336 read_lock(&local->sockets.lock); 337 338 llcp_sock = NULL; 339 340 sk_for_each(sk, &local->sockets.head) { 341 tmp_sock = nfc_llcp_sock(sk); 342 343 pr_debug("llcp sock %p\n", tmp_sock); 344 345 if (tmp_sock->sk.sk_type == SOCK_STREAM && 346 tmp_sock->sk.sk_state != LLCP_LISTEN) 347 continue; 348 349 if (tmp_sock->sk.sk_type == SOCK_DGRAM && 350 tmp_sock->sk.sk_state != LLCP_BOUND) 351 continue; 352 353 if (tmp_sock->service_name == NULL || 354 tmp_sock->service_name_len == 0) 355 continue; 356 357 if (tmp_sock->service_name_len != sn_len) 358 continue; 359 360 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) { 361 llcp_sock = tmp_sock; 362 break; 363 } 364 } 365 366 read_unlock(&local->sockets.lock); 367 368 pr_debug("Found llcp sock %p\n", llcp_sock); 369 370 return llcp_sock; 371 } 372 373 u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local, 374 struct nfc_llcp_sock *sock) 375 { 376 mutex_lock(&local->sdp_lock); 377 378 if (sock->service_name != NULL && sock->service_name_len > 0) { 379 int ssap = nfc_llcp_wks_sap(sock->service_name, 380 sock->service_name_len); 381 382 if (ssap > 0) { 383 pr_debug("WKS %d\n", ssap); 384 385 /* This is a WKS, let's check if it's free */ 386 if (local->local_wks & BIT(ssap)) { 387 mutex_unlock(&local->sdp_lock); 388 389 return LLCP_SAP_MAX; 390 } 391 392 set_bit(ssap, &local->local_wks); 393 mutex_unlock(&local->sdp_lock); 394 395 return ssap; 396 } 397 398 /* 399 * Check if there already is a non WKS socket bound 400 * to this service name. 401 */ 402 if (nfc_llcp_sock_from_sn(local, sock->service_name, 403 sock->service_name_len) != NULL) { 404 mutex_unlock(&local->sdp_lock); 405 406 return LLCP_SAP_MAX; 407 } 408 409 mutex_unlock(&local->sdp_lock); 410 411 return LLCP_SDP_UNBOUND; 412 413 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) { 414 if (!test_bit(sock->ssap, &local->local_wks)) { 415 set_bit(sock->ssap, &local->local_wks); 416 mutex_unlock(&local->sdp_lock); 417 418 return sock->ssap; 419 } 420 } 421 422 mutex_unlock(&local->sdp_lock); 423 424 return LLCP_SAP_MAX; 425 } 426 427 u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local) 428 { 429 u8 local_ssap; 430 431 mutex_lock(&local->sdp_lock); 432 433 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP); 434 if (local_ssap == LLCP_LOCAL_NUM_SAP) { 435 mutex_unlock(&local->sdp_lock); 436 return LLCP_SAP_MAX; 437 } 438 439 set_bit(local_ssap, &local->local_sap); 440 441 mutex_unlock(&local->sdp_lock); 442 443 return local_ssap + LLCP_LOCAL_SAP_OFFSET; 444 } 445 446 void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap) 447 { 448 u8 local_ssap; 449 unsigned long *sdp; 450 451 if (ssap < LLCP_WKS_NUM_SAP) { 452 local_ssap = ssap; 453 sdp = &local->local_wks; 454 } else if (ssap < LLCP_LOCAL_NUM_SAP) { 455 atomic_t *client_cnt; 456 457 local_ssap = ssap - LLCP_WKS_NUM_SAP; 458 sdp = &local->local_sdp; 459 client_cnt = &local->local_sdp_cnt[local_ssap]; 460 461 pr_debug("%d clients\n", atomic_read(client_cnt)); 462 463 mutex_lock(&local->sdp_lock); 464 465 if (atomic_dec_and_test(client_cnt)) { 466 struct nfc_llcp_sock *l_sock; 467 468 pr_debug("No more clients for SAP %d\n", ssap); 469 470 clear_bit(local_ssap, sdp); 471 472 /* Find the listening sock and set it back to UNBOUND */ 473 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP); 474 if (l_sock) { 475 l_sock->ssap = LLCP_SDP_UNBOUND; 476 nfc_llcp_sock_put(l_sock); 477 } 478 } 479 480 mutex_unlock(&local->sdp_lock); 481 482 return; 483 } else if (ssap < LLCP_MAX_SAP) { 484 local_ssap = ssap - LLCP_LOCAL_NUM_SAP; 485 sdp = &local->local_sap; 486 } else { 487 return; 488 } 489 490 mutex_lock(&local->sdp_lock); 491 492 clear_bit(local_ssap, sdp); 493 494 mutex_unlock(&local->sdp_lock); 495 } 496 497 static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local) 498 { 499 u8 ssap; 500 501 mutex_lock(&local->sdp_lock); 502 503 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP); 504 if (ssap == LLCP_SDP_NUM_SAP) { 505 mutex_unlock(&local->sdp_lock); 506 507 return LLCP_SAP_MAX; 508 } 509 510 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap); 511 512 set_bit(ssap, &local->local_sdp); 513 514 mutex_unlock(&local->sdp_lock); 515 516 return LLCP_WKS_NUM_SAP + ssap; 517 } 518 519 static int nfc_llcp_build_gb(struct nfc_llcp_local *local) 520 { 521 u8 *gb_cur, version, version_length; 522 u8 lto_length, wks_length, miux_length; 523 const u8 *version_tlv = NULL, *lto_tlv = NULL, 524 *wks_tlv = NULL, *miux_tlv = NULL; 525 __be16 wks = cpu_to_be16(local->local_wks); 526 u8 gb_len = 0; 527 int ret = 0; 528 529 version = LLCP_VERSION_11; 530 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version, 531 1, &version_length); 532 if (!version_tlv) { 533 ret = -ENOMEM; 534 goto out; 535 } 536 gb_len += version_length; 537 538 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, <o_length); 539 if (!lto_tlv) { 540 ret = -ENOMEM; 541 goto out; 542 } 543 gb_len += lto_length; 544 545 pr_debug("Local wks 0x%lx\n", local->local_wks); 546 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&wks, 2, &wks_length); 547 if (!wks_tlv) { 548 ret = -ENOMEM; 549 goto out; 550 } 551 gb_len += wks_length; 552 553 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0, 554 &miux_length); 555 if (!miux_tlv) { 556 ret = -ENOMEM; 557 goto out; 558 } 559 gb_len += miux_length; 560 561 gb_len += ARRAY_SIZE(llcp_magic); 562 563 if (gb_len > NFC_MAX_GT_LEN) { 564 ret = -EINVAL; 565 goto out; 566 } 567 568 gb_cur = local->gb; 569 570 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic)); 571 gb_cur += ARRAY_SIZE(llcp_magic); 572 573 memcpy(gb_cur, version_tlv, version_length); 574 gb_cur += version_length; 575 576 memcpy(gb_cur, lto_tlv, lto_length); 577 gb_cur += lto_length; 578 579 memcpy(gb_cur, wks_tlv, wks_length); 580 gb_cur += wks_length; 581 582 memcpy(gb_cur, miux_tlv, miux_length); 583 gb_cur += miux_length; 584 585 local->gb_len = gb_len; 586 587 out: 588 kfree(version_tlv); 589 kfree(lto_tlv); 590 kfree(wks_tlv); 591 kfree(miux_tlv); 592 593 return ret; 594 } 595 596 u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len) 597 { 598 struct nfc_llcp_local *local; 599 600 local = nfc_llcp_find_local(dev); 601 if (local == NULL) { 602 *general_bytes_len = 0; 603 return NULL; 604 } 605 606 nfc_llcp_build_gb(local); 607 608 *general_bytes_len = local->gb_len; 609 610 return local->gb; 611 } 612 613 int nfc_llcp_set_remote_gb(struct nfc_dev *dev, const u8 *gb, u8 gb_len) 614 { 615 struct nfc_llcp_local *local; 616 617 if (gb_len < 3 || gb_len > NFC_MAX_GT_LEN) 618 return -EINVAL; 619 620 local = nfc_llcp_find_local(dev); 621 if (local == NULL) { 622 pr_err("No LLCP device\n"); 623 return -ENODEV; 624 } 625 626 memset(local->remote_gb, 0, NFC_MAX_GT_LEN); 627 memcpy(local->remote_gb, gb, gb_len); 628 local->remote_gb_len = gb_len; 629 630 if (memcmp(local->remote_gb, llcp_magic, 3)) { 631 pr_err("MAC does not support LLCP\n"); 632 return -EINVAL; 633 } 634 635 return nfc_llcp_parse_gb_tlv(local, 636 &local->remote_gb[3], 637 local->remote_gb_len - 3); 638 } 639 640 static u8 nfc_llcp_dsap(const struct sk_buff *pdu) 641 { 642 return (pdu->data[0] & 0xfc) >> 2; 643 } 644 645 static u8 nfc_llcp_ptype(const struct sk_buff *pdu) 646 { 647 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6); 648 } 649 650 static u8 nfc_llcp_ssap(const struct sk_buff *pdu) 651 { 652 return pdu->data[1] & 0x3f; 653 } 654 655 static u8 nfc_llcp_ns(const struct sk_buff *pdu) 656 { 657 return pdu->data[2] >> 4; 658 } 659 660 static u8 nfc_llcp_nr(const struct sk_buff *pdu) 661 { 662 return pdu->data[2] & 0xf; 663 } 664 665 static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu) 666 { 667 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n); 668 sock->send_n = (sock->send_n + 1) % 16; 669 sock->recv_ack_n = (sock->recv_n - 1) % 16; 670 } 671 672 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local, 673 struct sk_buff *skb, u8 direction) 674 { 675 struct sk_buff *skb_copy = NULL, *nskb; 676 struct sock *sk; 677 u8 *data; 678 679 read_lock(&local->raw_sockets.lock); 680 681 sk_for_each(sk, &local->raw_sockets.head) { 682 if (sk->sk_state != LLCP_BOUND) 683 continue; 684 685 if (skb_copy == NULL) { 686 skb_copy = __pskb_copy_fclone(skb, NFC_RAW_HEADER_SIZE, 687 GFP_ATOMIC, true); 688 689 if (skb_copy == NULL) 690 continue; 691 692 data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE); 693 694 data[0] = local->dev ? local->dev->idx : 0xFF; 695 data[1] = direction & 0x01; 696 data[1] |= (RAW_PAYLOAD_LLCP << 1); 697 } 698 699 nskb = skb_clone(skb_copy, GFP_ATOMIC); 700 if (!nskb) 701 continue; 702 703 if (sock_queue_rcv_skb(sk, nskb)) 704 kfree_skb(nskb); 705 } 706 707 read_unlock(&local->raw_sockets.lock); 708 709 kfree_skb(skb_copy); 710 } 711 712 static void nfc_llcp_tx_work(struct work_struct *work) 713 { 714 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, 715 tx_work); 716 struct sk_buff *skb; 717 struct sock *sk; 718 struct nfc_llcp_sock *llcp_sock; 719 720 skb = skb_dequeue(&local->tx_queue); 721 if (skb != NULL) { 722 sk = skb->sk; 723 llcp_sock = nfc_llcp_sock(sk); 724 725 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) { 726 kfree_skb(skb); 727 nfc_llcp_send_symm(local->dev); 728 } else if (llcp_sock && !llcp_sock->remote_ready) { 729 skb_queue_head(&local->tx_queue, skb); 730 nfc_llcp_send_symm(local->dev); 731 } else { 732 struct sk_buff *copy_skb = NULL; 733 u8 ptype = nfc_llcp_ptype(skb); 734 int ret; 735 736 pr_debug("Sending pending skb\n"); 737 print_hex_dump_debug("LLCP Tx: ", DUMP_PREFIX_OFFSET, 738 16, 1, skb->data, skb->len, true); 739 740 if (ptype == LLCP_PDU_DISC && sk != NULL && 741 sk->sk_state == LLCP_DISCONNECTING) { 742 nfc_llcp_sock_unlink(&local->sockets, sk); 743 sock_orphan(sk); 744 sock_put(sk); 745 } 746 747 if (ptype == LLCP_PDU_I) 748 copy_skb = skb_copy(skb, GFP_ATOMIC); 749 750 __net_timestamp(skb); 751 752 nfc_llcp_send_to_raw_sock(local, skb, 753 NFC_DIRECTION_TX); 754 755 ret = nfc_data_exchange(local->dev, local->target_idx, 756 skb, nfc_llcp_recv, local); 757 758 if (ret) { 759 kfree_skb(copy_skb); 760 goto out; 761 } 762 763 if (ptype == LLCP_PDU_I && copy_skb) 764 skb_queue_tail(&llcp_sock->tx_pending_queue, 765 copy_skb); 766 } 767 } else { 768 nfc_llcp_send_symm(local->dev); 769 } 770 771 out: 772 mod_timer(&local->link_timer, 773 jiffies + msecs_to_jiffies(2 * local->remote_lto)); 774 } 775 776 static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local, 777 u8 ssap) 778 { 779 struct sock *sk; 780 struct nfc_llcp_sock *llcp_sock; 781 782 read_lock(&local->connecting_sockets.lock); 783 784 sk_for_each(sk, &local->connecting_sockets.head) { 785 llcp_sock = nfc_llcp_sock(sk); 786 787 if (llcp_sock->ssap == ssap) { 788 sock_hold(&llcp_sock->sk); 789 goto out; 790 } 791 } 792 793 llcp_sock = NULL; 794 795 out: 796 read_unlock(&local->connecting_sockets.lock); 797 798 return llcp_sock; 799 } 800 801 static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local, 802 const u8 *sn, size_t sn_len) 803 { 804 struct nfc_llcp_sock *llcp_sock; 805 806 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len); 807 808 if (llcp_sock == NULL) 809 return NULL; 810 811 sock_hold(&llcp_sock->sk); 812 813 return llcp_sock; 814 } 815 816 static const u8 *nfc_llcp_connect_sn(const struct sk_buff *skb, size_t *sn_len) 817 { 818 u8 type, length; 819 const u8 *tlv = &skb->data[2]; 820 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0; 821 822 while (offset < tlv_array_len) { 823 type = tlv[0]; 824 length = tlv[1]; 825 826 pr_debug("type 0x%x length %d\n", type, length); 827 828 if (type == LLCP_TLV_SN) { 829 *sn_len = length; 830 return &tlv[2]; 831 } 832 833 offset += length + 2; 834 tlv += length + 2; 835 } 836 837 return NULL; 838 } 839 840 static void nfc_llcp_recv_ui(struct nfc_llcp_local *local, 841 struct sk_buff *skb) 842 { 843 struct nfc_llcp_sock *llcp_sock; 844 struct nfc_llcp_ui_cb *ui_cb; 845 u8 dsap, ssap; 846 847 dsap = nfc_llcp_dsap(skb); 848 ssap = nfc_llcp_ssap(skb); 849 850 ui_cb = nfc_llcp_ui_skb_cb(skb); 851 ui_cb->dsap = dsap; 852 ui_cb->ssap = ssap; 853 854 pr_debug("%d %d\n", dsap, ssap); 855 856 /* We're looking for a bound socket, not a client one */ 857 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); 858 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM) 859 return; 860 861 /* There is no sequence with UI frames */ 862 skb_pull(skb, LLCP_HEADER_SIZE); 863 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) { 864 /* 865 * UI frames will be freed from the socket layer, so we 866 * need to keep them alive until someone receives them. 867 */ 868 skb_get(skb); 869 } else { 870 pr_err("Receive queue is full\n"); 871 } 872 873 nfc_llcp_sock_put(llcp_sock); 874 } 875 876 static void nfc_llcp_recv_connect(struct nfc_llcp_local *local, 877 const struct sk_buff *skb) 878 { 879 struct sock *new_sk, *parent; 880 struct nfc_llcp_sock *sock, *new_sock; 881 u8 dsap, ssap, reason; 882 883 dsap = nfc_llcp_dsap(skb); 884 ssap = nfc_llcp_ssap(skb); 885 886 pr_debug("%d %d\n", dsap, ssap); 887 888 if (dsap != LLCP_SAP_SDP) { 889 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); 890 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) { 891 reason = LLCP_DM_NOBOUND; 892 goto fail; 893 } 894 } else { 895 const u8 *sn; 896 size_t sn_len; 897 898 sn = nfc_llcp_connect_sn(skb, &sn_len); 899 if (sn == NULL) { 900 reason = LLCP_DM_NOBOUND; 901 goto fail; 902 } 903 904 pr_debug("Service name length %zu\n", sn_len); 905 906 sock = nfc_llcp_sock_get_sn(local, sn, sn_len); 907 if (sock == NULL) { 908 reason = LLCP_DM_NOBOUND; 909 goto fail; 910 } 911 } 912 913 lock_sock(&sock->sk); 914 915 parent = &sock->sk; 916 917 if (sk_acceptq_is_full(parent)) { 918 reason = LLCP_DM_REJ; 919 release_sock(&sock->sk); 920 sock_put(&sock->sk); 921 goto fail; 922 } 923 924 if (sock->ssap == LLCP_SDP_UNBOUND) { 925 u8 ssap = nfc_llcp_reserve_sdp_ssap(local); 926 927 pr_debug("First client, reserving %d\n", ssap); 928 929 if (ssap == LLCP_SAP_MAX) { 930 reason = LLCP_DM_REJ; 931 release_sock(&sock->sk); 932 sock_put(&sock->sk); 933 goto fail; 934 } 935 936 sock->ssap = ssap; 937 } 938 939 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC, 0); 940 if (new_sk == NULL) { 941 reason = LLCP_DM_REJ; 942 release_sock(&sock->sk); 943 sock_put(&sock->sk); 944 goto fail; 945 } 946 947 new_sock = nfc_llcp_sock(new_sk); 948 new_sock->dev = local->dev; 949 new_sock->local = nfc_llcp_local_get(local); 950 new_sock->rw = sock->rw; 951 new_sock->miux = sock->miux; 952 new_sock->nfc_protocol = sock->nfc_protocol; 953 new_sock->dsap = ssap; 954 new_sock->target_idx = local->target_idx; 955 new_sock->parent = parent; 956 new_sock->ssap = sock->ssap; 957 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) { 958 atomic_t *client_count; 959 960 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock); 961 962 client_count = 963 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP]; 964 965 atomic_inc(client_count); 966 new_sock->reserved_ssap = sock->ssap; 967 } 968 969 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE], 970 skb->len - LLCP_HEADER_SIZE); 971 972 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk); 973 974 nfc_llcp_sock_link(&local->sockets, new_sk); 975 976 nfc_llcp_accept_enqueue(&sock->sk, new_sk); 977 978 nfc_get_device(local->dev->idx); 979 980 new_sk->sk_state = LLCP_CONNECTED; 981 982 /* Wake the listening processes */ 983 parent->sk_data_ready(parent); 984 985 /* Send CC */ 986 nfc_llcp_send_cc(new_sock); 987 988 release_sock(&sock->sk); 989 sock_put(&sock->sk); 990 991 return; 992 993 fail: 994 /* Send DM */ 995 nfc_llcp_send_dm(local, dsap, ssap, reason); 996 } 997 998 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock) 999 { 1000 int nr_frames = 0; 1001 struct nfc_llcp_local *local = sock->local; 1002 1003 pr_debug("Remote ready %d tx queue len %d remote rw %d", 1004 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue), 1005 sock->remote_rw); 1006 1007 /* Try to queue some I frames for transmission */ 1008 while (sock->remote_ready && 1009 skb_queue_len(&sock->tx_pending_queue) < sock->remote_rw) { 1010 struct sk_buff *pdu; 1011 1012 pdu = skb_dequeue(&sock->tx_queue); 1013 if (pdu == NULL) 1014 break; 1015 1016 /* Update N(S)/N(R) */ 1017 nfc_llcp_set_nrns(sock, pdu); 1018 1019 skb_queue_tail(&local->tx_queue, pdu); 1020 nr_frames++; 1021 } 1022 1023 return nr_frames; 1024 } 1025 1026 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local, 1027 struct sk_buff *skb) 1028 { 1029 struct nfc_llcp_sock *llcp_sock; 1030 struct sock *sk; 1031 u8 dsap, ssap, ptype, ns, nr; 1032 1033 ptype = nfc_llcp_ptype(skb); 1034 dsap = nfc_llcp_dsap(skb); 1035 ssap = nfc_llcp_ssap(skb); 1036 ns = nfc_llcp_ns(skb); 1037 nr = nfc_llcp_nr(skb); 1038 1039 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns); 1040 1041 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); 1042 if (llcp_sock == NULL) { 1043 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); 1044 return; 1045 } 1046 1047 sk = &llcp_sock->sk; 1048 lock_sock(sk); 1049 if (sk->sk_state == LLCP_CLOSED) { 1050 release_sock(sk); 1051 nfc_llcp_sock_put(llcp_sock); 1052 } 1053 1054 /* Pass the payload upstream */ 1055 if (ptype == LLCP_PDU_I) { 1056 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk); 1057 1058 if (ns == llcp_sock->recv_n) 1059 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16; 1060 else 1061 pr_err("Received out of sequence I PDU\n"); 1062 1063 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE); 1064 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) { 1065 /* 1066 * I frames will be freed from the socket layer, so we 1067 * need to keep them alive until someone receives them. 1068 */ 1069 skb_get(skb); 1070 } else { 1071 pr_err("Receive queue is full\n"); 1072 } 1073 } 1074 1075 /* Remove skbs from the pending queue */ 1076 if (llcp_sock->send_ack_n != nr) { 1077 struct sk_buff *s, *tmp; 1078 u8 n; 1079 1080 llcp_sock->send_ack_n = nr; 1081 1082 /* Remove and free all skbs until ns == nr */ 1083 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) { 1084 n = nfc_llcp_ns(s); 1085 1086 skb_unlink(s, &llcp_sock->tx_pending_queue); 1087 kfree_skb(s); 1088 1089 if (n == nr) 1090 break; 1091 } 1092 1093 /* Re-queue the remaining skbs for transmission */ 1094 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue, 1095 s, tmp) { 1096 skb_unlink(s, &llcp_sock->tx_pending_queue); 1097 skb_queue_head(&local->tx_queue, s); 1098 } 1099 } 1100 1101 if (ptype == LLCP_PDU_RR) 1102 llcp_sock->remote_ready = true; 1103 else if (ptype == LLCP_PDU_RNR) 1104 llcp_sock->remote_ready = false; 1105 1106 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I) 1107 nfc_llcp_send_rr(llcp_sock); 1108 1109 release_sock(sk); 1110 nfc_llcp_sock_put(llcp_sock); 1111 } 1112 1113 static void nfc_llcp_recv_disc(struct nfc_llcp_local *local, 1114 const struct sk_buff *skb) 1115 { 1116 struct nfc_llcp_sock *llcp_sock; 1117 struct sock *sk; 1118 u8 dsap, ssap; 1119 1120 dsap = nfc_llcp_dsap(skb); 1121 ssap = nfc_llcp_ssap(skb); 1122 1123 if ((dsap == 0) && (ssap == 0)) { 1124 pr_debug("Connection termination"); 1125 nfc_dep_link_down(local->dev); 1126 return; 1127 } 1128 1129 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); 1130 if (llcp_sock == NULL) { 1131 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); 1132 return; 1133 } 1134 1135 sk = &llcp_sock->sk; 1136 lock_sock(sk); 1137 1138 nfc_llcp_socket_purge(llcp_sock); 1139 1140 if (sk->sk_state == LLCP_CLOSED) { 1141 release_sock(sk); 1142 nfc_llcp_sock_put(llcp_sock); 1143 } 1144 1145 if (sk->sk_state == LLCP_CONNECTED) { 1146 nfc_put_device(local->dev); 1147 sk->sk_state = LLCP_CLOSED; 1148 sk->sk_state_change(sk); 1149 } 1150 1151 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC); 1152 1153 release_sock(sk); 1154 nfc_llcp_sock_put(llcp_sock); 1155 } 1156 1157 static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, 1158 const struct sk_buff *skb) 1159 { 1160 struct nfc_llcp_sock *llcp_sock; 1161 struct sock *sk; 1162 u8 dsap, ssap; 1163 1164 dsap = nfc_llcp_dsap(skb); 1165 ssap = nfc_llcp_ssap(skb); 1166 1167 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); 1168 if (llcp_sock == NULL) { 1169 pr_err("Invalid CC\n"); 1170 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); 1171 1172 return; 1173 } 1174 1175 sk = &llcp_sock->sk; 1176 1177 /* Unlink from connecting and link to the client array */ 1178 nfc_llcp_sock_unlink(&local->connecting_sockets, sk); 1179 nfc_llcp_sock_link(&local->sockets, sk); 1180 llcp_sock->dsap = ssap; 1181 1182 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE], 1183 skb->len - LLCP_HEADER_SIZE); 1184 1185 sk->sk_state = LLCP_CONNECTED; 1186 sk->sk_state_change(sk); 1187 1188 nfc_llcp_sock_put(llcp_sock); 1189 } 1190 1191 static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, 1192 const struct sk_buff *skb) 1193 { 1194 struct nfc_llcp_sock *llcp_sock; 1195 struct sock *sk; 1196 u8 dsap, ssap, reason; 1197 1198 dsap = nfc_llcp_dsap(skb); 1199 ssap = nfc_llcp_ssap(skb); 1200 reason = skb->data[2]; 1201 1202 pr_debug("%d %d reason %d\n", ssap, dsap, reason); 1203 1204 switch (reason) { 1205 case LLCP_DM_NOBOUND: 1206 case LLCP_DM_REJ: 1207 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); 1208 break; 1209 1210 default: 1211 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); 1212 break; 1213 } 1214 1215 if (llcp_sock == NULL) { 1216 pr_debug("Already closed\n"); 1217 return; 1218 } 1219 1220 sk = &llcp_sock->sk; 1221 1222 sk->sk_err = ENXIO; 1223 sk->sk_state = LLCP_CLOSED; 1224 sk->sk_state_change(sk); 1225 1226 nfc_llcp_sock_put(llcp_sock); 1227 } 1228 1229 static void nfc_llcp_recv_snl(struct nfc_llcp_local *local, 1230 const struct sk_buff *skb) 1231 { 1232 struct nfc_llcp_sock *llcp_sock; 1233 u8 dsap, ssap, type, length, tid, sap; 1234 const u8 *tlv; 1235 u16 tlv_len, offset; 1236 const char *service_name; 1237 size_t service_name_len; 1238 struct nfc_llcp_sdp_tlv *sdp; 1239 HLIST_HEAD(llc_sdres_list); 1240 size_t sdres_tlvs_len; 1241 HLIST_HEAD(nl_sdres_list); 1242 1243 dsap = nfc_llcp_dsap(skb); 1244 ssap = nfc_llcp_ssap(skb); 1245 1246 pr_debug("%d %d\n", dsap, ssap); 1247 1248 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) { 1249 pr_err("Wrong SNL SAP\n"); 1250 return; 1251 } 1252 1253 tlv = &skb->data[LLCP_HEADER_SIZE]; 1254 tlv_len = skb->len - LLCP_HEADER_SIZE; 1255 offset = 0; 1256 sdres_tlvs_len = 0; 1257 1258 while (offset < tlv_len) { 1259 type = tlv[0]; 1260 length = tlv[1]; 1261 1262 switch (type) { 1263 case LLCP_TLV_SDREQ: 1264 tid = tlv[2]; 1265 service_name = (char *) &tlv[3]; 1266 service_name_len = length - 1; 1267 1268 pr_debug("Looking for %.16s\n", service_name); 1269 1270 if (service_name_len == strlen("urn:nfc:sn:sdp") && 1271 !strncmp(service_name, "urn:nfc:sn:sdp", 1272 service_name_len)) { 1273 sap = 1; 1274 goto add_snl; 1275 } 1276 1277 llcp_sock = nfc_llcp_sock_from_sn(local, service_name, 1278 service_name_len); 1279 if (!llcp_sock) { 1280 sap = 0; 1281 goto add_snl; 1282 } 1283 1284 /* 1285 * We found a socket but its ssap has not been reserved 1286 * yet. We need to assign it for good and send a reply. 1287 * The ssap will be freed when the socket is closed. 1288 */ 1289 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) { 1290 atomic_t *client_count; 1291 1292 sap = nfc_llcp_reserve_sdp_ssap(local); 1293 1294 pr_debug("Reserving %d\n", sap); 1295 1296 if (sap == LLCP_SAP_MAX) { 1297 sap = 0; 1298 goto add_snl; 1299 } 1300 1301 client_count = 1302 &local->local_sdp_cnt[sap - 1303 LLCP_WKS_NUM_SAP]; 1304 1305 atomic_inc(client_count); 1306 1307 llcp_sock->ssap = sap; 1308 llcp_sock->reserved_ssap = sap; 1309 } else { 1310 sap = llcp_sock->ssap; 1311 } 1312 1313 pr_debug("%p %d\n", llcp_sock, sap); 1314 1315 add_snl: 1316 sdp = nfc_llcp_build_sdres_tlv(tid, sap); 1317 if (sdp == NULL) 1318 goto exit; 1319 1320 sdres_tlvs_len += sdp->tlv_len; 1321 hlist_add_head(&sdp->node, &llc_sdres_list); 1322 break; 1323 1324 case LLCP_TLV_SDRES: 1325 mutex_lock(&local->sdreq_lock); 1326 1327 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv[2]); 1328 1329 hlist_for_each_entry(sdp, &local->pending_sdreqs, node) { 1330 if (sdp->tid != tlv[2]) 1331 continue; 1332 1333 sdp->sap = tlv[3]; 1334 1335 pr_debug("Found: uri=%s, sap=%d\n", 1336 sdp->uri, sdp->sap); 1337 1338 hlist_del(&sdp->node); 1339 1340 hlist_add_head(&sdp->node, &nl_sdres_list); 1341 1342 break; 1343 } 1344 1345 mutex_unlock(&local->sdreq_lock); 1346 break; 1347 1348 default: 1349 pr_err("Invalid SNL tlv value 0x%x\n", type); 1350 break; 1351 } 1352 1353 offset += length + 2; 1354 tlv += length + 2; 1355 } 1356 1357 exit: 1358 if (!hlist_empty(&nl_sdres_list)) 1359 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list); 1360 1361 if (!hlist_empty(&llc_sdres_list)) 1362 nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len); 1363 } 1364 1365 static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb) 1366 { 1367 u8 ptype; 1368 u16 pdu_len; 1369 struct sk_buff *new_skb; 1370 1371 if (skb->len <= LLCP_HEADER_SIZE) { 1372 pr_err("Malformed AGF PDU\n"); 1373 return; 1374 } 1375 1376 skb_pull(skb, LLCP_HEADER_SIZE); 1377 1378 while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) { 1379 pdu_len = skb->data[0] << 8 | skb->data[1]; 1380 1381 skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE); 1382 1383 if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) { 1384 pr_err("Malformed AGF PDU\n"); 1385 return; 1386 } 1387 1388 ptype = nfc_llcp_ptype(skb); 1389 1390 if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF) 1391 goto next; 1392 1393 new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL); 1394 if (new_skb == NULL) { 1395 pr_err("Could not allocate PDU\n"); 1396 return; 1397 } 1398 1399 skb_put_data(new_skb, skb->data, pdu_len); 1400 1401 nfc_llcp_rx_skb(local, new_skb); 1402 1403 kfree_skb(new_skb); 1404 next: 1405 skb_pull(skb, pdu_len); 1406 } 1407 } 1408 1409 static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb) 1410 { 1411 u8 dsap, ssap, ptype; 1412 1413 ptype = nfc_llcp_ptype(skb); 1414 dsap = nfc_llcp_dsap(skb); 1415 ssap = nfc_llcp_ssap(skb); 1416 1417 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); 1418 1419 if (ptype != LLCP_PDU_SYMM) 1420 print_hex_dump_debug("LLCP Rx: ", DUMP_PREFIX_OFFSET, 16, 1, 1421 skb->data, skb->len, true); 1422 1423 switch (ptype) { 1424 case LLCP_PDU_SYMM: 1425 pr_debug("SYMM\n"); 1426 break; 1427 1428 case LLCP_PDU_UI: 1429 pr_debug("UI\n"); 1430 nfc_llcp_recv_ui(local, skb); 1431 break; 1432 1433 case LLCP_PDU_CONNECT: 1434 pr_debug("CONNECT\n"); 1435 nfc_llcp_recv_connect(local, skb); 1436 break; 1437 1438 case LLCP_PDU_DISC: 1439 pr_debug("DISC\n"); 1440 nfc_llcp_recv_disc(local, skb); 1441 break; 1442 1443 case LLCP_PDU_CC: 1444 pr_debug("CC\n"); 1445 nfc_llcp_recv_cc(local, skb); 1446 break; 1447 1448 case LLCP_PDU_DM: 1449 pr_debug("DM\n"); 1450 nfc_llcp_recv_dm(local, skb); 1451 break; 1452 1453 case LLCP_PDU_SNL: 1454 pr_debug("SNL\n"); 1455 nfc_llcp_recv_snl(local, skb); 1456 break; 1457 1458 case LLCP_PDU_I: 1459 case LLCP_PDU_RR: 1460 case LLCP_PDU_RNR: 1461 pr_debug("I frame\n"); 1462 nfc_llcp_recv_hdlc(local, skb); 1463 break; 1464 1465 case LLCP_PDU_AGF: 1466 pr_debug("AGF frame\n"); 1467 nfc_llcp_recv_agf(local, skb); 1468 break; 1469 } 1470 } 1471 1472 static void nfc_llcp_rx_work(struct work_struct *work) 1473 { 1474 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, 1475 rx_work); 1476 struct sk_buff *skb; 1477 1478 skb = local->rx_pending; 1479 if (skb == NULL) { 1480 pr_debug("No pending SKB\n"); 1481 return; 1482 } 1483 1484 __net_timestamp(skb); 1485 1486 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX); 1487 1488 nfc_llcp_rx_skb(local, skb); 1489 1490 schedule_work(&local->tx_work); 1491 kfree_skb(local->rx_pending); 1492 local->rx_pending = NULL; 1493 } 1494 1495 static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb) 1496 { 1497 local->rx_pending = skb; 1498 del_timer(&local->link_timer); 1499 schedule_work(&local->rx_work); 1500 } 1501 1502 void nfc_llcp_recv(void *data, struct sk_buff *skb, int err) 1503 { 1504 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; 1505 1506 if (err < 0) { 1507 pr_err("LLCP PDU receive err %d\n", err); 1508 return; 1509 } 1510 1511 __nfc_llcp_recv(local, skb); 1512 } 1513 1514 int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb) 1515 { 1516 struct nfc_llcp_local *local; 1517 1518 local = nfc_llcp_find_local(dev); 1519 if (local == NULL) { 1520 kfree_skb(skb); 1521 return -ENODEV; 1522 } 1523 1524 __nfc_llcp_recv(local, skb); 1525 1526 return 0; 1527 } 1528 1529 void nfc_llcp_mac_is_down(struct nfc_dev *dev) 1530 { 1531 struct nfc_llcp_local *local; 1532 1533 local = nfc_llcp_find_local(dev); 1534 if (local == NULL) 1535 return; 1536 1537 local->remote_miu = LLCP_DEFAULT_MIU; 1538 local->remote_lto = LLCP_DEFAULT_LTO; 1539 1540 /* Close and purge all existing sockets */ 1541 nfc_llcp_socket_release(local, true, 0); 1542 } 1543 1544 void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx, 1545 u8 comm_mode, u8 rf_mode) 1546 { 1547 struct nfc_llcp_local *local; 1548 1549 pr_debug("rf mode %d\n", rf_mode); 1550 1551 local = nfc_llcp_find_local(dev); 1552 if (local == NULL) 1553 return; 1554 1555 local->target_idx = target_idx; 1556 local->comm_mode = comm_mode; 1557 local->rf_mode = rf_mode; 1558 1559 if (rf_mode == NFC_RF_INITIATOR) { 1560 pr_debug("Queueing Tx work\n"); 1561 1562 schedule_work(&local->tx_work); 1563 } else { 1564 mod_timer(&local->link_timer, 1565 jiffies + msecs_to_jiffies(local->remote_lto)); 1566 } 1567 } 1568 1569 int nfc_llcp_register_device(struct nfc_dev *ndev) 1570 { 1571 struct nfc_llcp_local *local; 1572 1573 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL); 1574 if (local == NULL) 1575 return -ENOMEM; 1576 1577 local->dev = ndev; 1578 INIT_LIST_HEAD(&local->list); 1579 kref_init(&local->ref); 1580 mutex_init(&local->sdp_lock); 1581 timer_setup(&local->link_timer, nfc_llcp_symm_timer, 0); 1582 1583 skb_queue_head_init(&local->tx_queue); 1584 INIT_WORK(&local->tx_work, nfc_llcp_tx_work); 1585 1586 local->rx_pending = NULL; 1587 INIT_WORK(&local->rx_work, nfc_llcp_rx_work); 1588 1589 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work); 1590 1591 rwlock_init(&local->sockets.lock); 1592 rwlock_init(&local->connecting_sockets.lock); 1593 rwlock_init(&local->raw_sockets.lock); 1594 1595 local->lto = 150; /* 1500 ms */ 1596 local->rw = LLCP_MAX_RW; 1597 local->miux = cpu_to_be16(LLCP_MAX_MIUX); 1598 local->local_wks = 0x1; /* LLC Link Management */ 1599 1600 nfc_llcp_build_gb(local); 1601 1602 local->remote_miu = LLCP_DEFAULT_MIU; 1603 local->remote_lto = LLCP_DEFAULT_LTO; 1604 1605 mutex_init(&local->sdreq_lock); 1606 INIT_HLIST_HEAD(&local->pending_sdreqs); 1607 timer_setup(&local->sdreq_timer, nfc_llcp_sdreq_timer, 0); 1608 INIT_WORK(&local->sdreq_timeout_work, nfc_llcp_sdreq_timeout_work); 1609 1610 list_add(&local->list, &llcp_devices); 1611 1612 return 0; 1613 } 1614 1615 void nfc_llcp_unregister_device(struct nfc_dev *dev) 1616 { 1617 struct nfc_llcp_local *local = nfc_llcp_find_local(dev); 1618 1619 if (local == NULL) { 1620 pr_debug("No such device\n"); 1621 return; 1622 } 1623 1624 local_cleanup(local); 1625 1626 nfc_llcp_local_put(local); 1627 } 1628 1629 int __init nfc_llcp_init(void) 1630 { 1631 return nfc_llcp_sock_init(); 1632 } 1633 1634 void nfc_llcp_exit(void) 1635 { 1636 nfc_llcp_sock_exit(); 1637 } 1638