1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Intel Corporation. All rights reserved.
4 * Copyright (C) 2014 Marvell International Ltd.
7 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/nfc.h>
17 static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
19 static LIST_HEAD(llcp_devices);
21 static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb);
23 void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
26 sk_add_node(sk, &l->head);
27 write_unlock(&l->lock);
30 void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
34 write_unlock(&l->lock);
37 void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock)
39 sock->remote_rw = LLCP_DEFAULT_RW;
40 sock->remote_miu = LLCP_MAX_MIU + 1;
43 static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
45 struct nfc_llcp_local *local = sock->local;
46 struct sk_buff *s, *tmp;
48 skb_queue_purge(&sock->tx_queue);
49 skb_queue_purge(&sock->tx_pending_queue);
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)
59 skb_unlink(s, &local->tx_queue);
64 static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device,
68 struct hlist_node *tmp;
69 struct nfc_llcp_sock *llcp_sock;
71 skb_queue_purge(&local->tx_queue);
73 write_lock(&local->sockets.lock);
75 sk_for_each_safe(sk, tmp, &local->sockets.head) {
76 llcp_sock = nfc_llcp_sock(sk);
80 nfc_llcp_socket_purge(llcp_sock);
82 if (sk->sk_state == LLCP_CONNECTED)
83 nfc_put_device(llcp_sock->dev);
85 if (sk->sk_state == LLCP_LISTEN) {
86 struct nfc_llcp_sock *lsk, *n;
87 struct sock *accept_sk;
89 list_for_each_entry_safe(lsk, n,
90 &llcp_sock->accept_queue,
93 bh_lock_sock(accept_sk);
95 nfc_llcp_accept_unlink(accept_sk);
98 accept_sk->sk_err = err;
99 accept_sk->sk_state = LLCP_CLOSED;
100 accept_sk->sk_state_change(sk);
102 bh_unlock_sock(accept_sk);
108 sk->sk_state = LLCP_CLOSED;
109 sk->sk_state_change(sk);
113 sk_del_node_init(sk);
116 write_unlock(&local->sockets.lock);
118 /* If we still have a device, we keep the RAW sockets alive */
122 write_lock(&local->raw_sockets.lock);
124 sk_for_each_safe(sk, tmp, &local->raw_sockets.head) {
125 llcp_sock = nfc_llcp_sock(sk);
129 nfc_llcp_socket_purge(llcp_sock);
133 sk->sk_state = LLCP_CLOSED;
134 sk->sk_state_change(sk);
138 sk_del_node_init(sk);
141 write_unlock(&local->raw_sockets.lock);
144 struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
146 kref_get(&local->ref);
151 static void local_cleanup(struct nfc_llcp_local *local)
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);
165 static void local_release(struct kref *ref)
167 struct nfc_llcp_local *local;
169 local = container_of(ref, struct nfc_llcp_local, ref);
171 list_del(&local->list);
172 local_cleanup(local);
176 int nfc_llcp_local_put(struct nfc_llcp_local *local)
181 return kref_put(&local->ref, local_release);
184 static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
188 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
190 pr_debug("ssap dsap %d %d\n", ssap, dsap);
192 if (ssap == 0 && dsap == 0)
195 read_lock(&local->sockets.lock);
199 sk_for_each(sk, &local->sockets.head) {
200 tmp_sock = nfc_llcp_sock(sk);
202 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
203 llcp_sock = tmp_sock;
208 read_unlock(&local->sockets.lock);
210 if (llcp_sock == NULL)
213 sock_hold(&llcp_sock->sk);
218 static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
223 static void nfc_llcp_timeout_work(struct work_struct *work)
225 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
228 nfc_dep_link_down(local->dev);
231 static void nfc_llcp_symm_timer(struct timer_list *t)
233 struct nfc_llcp_local *local = from_timer(local, t, link_timer);
235 pr_err("SYMM timeout\n");
237 schedule_work(&local->timeout_work);
240 static void nfc_llcp_sdreq_timeout_work(struct work_struct *work)
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,
249 mutex_lock(&local->sdreq_lock);
251 time = jiffies - msecs_to_jiffies(3 * local->remote_lto);
253 hlist_for_each_entry_safe(sdp, n, &local->pending_sdreqs, node) {
254 if (time_after(sdp->time, time))
257 sdp->sap = LLCP_SDP_UNBOUND;
259 hlist_del(&sdp->node);
261 hlist_add_head(&sdp->node, &nl_sdres_list);
264 if (!hlist_empty(&local->pending_sdreqs))
265 mod_timer(&local->sdreq_timer,
266 jiffies + msecs_to_jiffies(3 * local->remote_lto));
268 mutex_unlock(&local->sdreq_lock);
270 if (!hlist_empty(&nl_sdres_list))
271 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
274 static void nfc_llcp_sdreq_timer(struct timer_list *t)
276 struct nfc_llcp_local *local = from_timer(local, t, sdreq_timer);
278 schedule_work(&local->sdreq_timeout_work);
281 struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
283 struct nfc_llcp_local *local;
285 list_for_each_entry(local, &llcp_devices, list)
286 if (local->dev == dev)
289 pr_debug("No device found\n");
294 static char *wks[] = {
302 static int nfc_llcp_wks_sap(const char *service_name, size_t service_name_len)
306 pr_debug("%s\n", service_name);
308 if (service_name == NULL)
311 num_wks = ARRAY_SIZE(wks);
313 for (sap = 0; sap < num_wks; sap++) {
314 if (wks[sap] == NULL)
317 if (strncmp(wks[sap], service_name, service_name_len) == 0)
325 struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
326 const u8 *sn, size_t sn_len)
329 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
331 pr_debug("sn %zd %p\n", sn_len, sn);
333 if (sn == NULL || sn_len == 0)
336 read_lock(&local->sockets.lock);
340 sk_for_each(sk, &local->sockets.head) {
341 tmp_sock = nfc_llcp_sock(sk);
343 pr_debug("llcp sock %p\n", tmp_sock);
345 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
346 tmp_sock->sk.sk_state != LLCP_LISTEN)
349 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
350 tmp_sock->sk.sk_state != LLCP_BOUND)
353 if (tmp_sock->service_name == NULL ||
354 tmp_sock->service_name_len == 0)
357 if (tmp_sock->service_name_len != sn_len)
360 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
361 llcp_sock = tmp_sock;
366 read_unlock(&local->sockets.lock);
368 pr_debug("Found llcp sock %p\n", llcp_sock);
373 u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
374 struct nfc_llcp_sock *sock)
376 mutex_lock(&local->sdp_lock);
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);
383 pr_debug("WKS %d\n", ssap);
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);
392 set_bit(ssap, &local->local_wks);
393 mutex_unlock(&local->sdp_lock);
399 * Check if there already is a non WKS socket bound
400 * to this service name.
402 if (nfc_llcp_sock_from_sn(local, sock->service_name,
403 sock->service_name_len) != NULL) {
404 mutex_unlock(&local->sdp_lock);
409 mutex_unlock(&local->sdp_lock);
411 return LLCP_SDP_UNBOUND;
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);
422 mutex_unlock(&local->sdp_lock);
427 u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
431 mutex_lock(&local->sdp_lock);
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);
439 set_bit(local_ssap, &local->local_sap);
441 mutex_unlock(&local->sdp_lock);
443 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
446 void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
451 if (ssap < LLCP_WKS_NUM_SAP) {
453 sdp = &local->local_wks;
454 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
455 atomic_t *client_cnt;
457 local_ssap = ssap - LLCP_WKS_NUM_SAP;
458 sdp = &local->local_sdp;
459 client_cnt = &local->local_sdp_cnt[local_ssap];
461 pr_debug("%d clients\n", atomic_read(client_cnt));
463 mutex_lock(&local->sdp_lock);
465 if (atomic_dec_and_test(client_cnt)) {
466 struct nfc_llcp_sock *l_sock;
468 pr_debug("No more clients for SAP %d\n", ssap);
470 clear_bit(local_ssap, sdp);
472 /* Find the listening sock and set it back to UNBOUND */
473 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
475 l_sock->ssap = LLCP_SDP_UNBOUND;
476 nfc_llcp_sock_put(l_sock);
480 mutex_unlock(&local->sdp_lock);
483 } else if (ssap < LLCP_MAX_SAP) {
484 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
485 sdp = &local->local_sap;
490 mutex_lock(&local->sdp_lock);
492 clear_bit(local_ssap, sdp);
494 mutex_unlock(&local->sdp_lock);
497 static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
501 mutex_lock(&local->sdp_lock);
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);
510 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
512 set_bit(ssap, &local->local_sdp);
514 mutex_unlock(&local->sdp_lock);
516 return LLCP_WKS_NUM_SAP + ssap;
519 static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
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);
529 version = LLCP_VERSION_11;
530 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
536 gb_len += version_length;
538 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, <o_length);
543 gb_len += lto_length;
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);
551 gb_len += wks_length;
553 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
559 gb_len += miux_length;
561 gb_len += ARRAY_SIZE(llcp_magic);
563 if (gb_len > NFC_MAX_GT_LEN) {
570 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
571 gb_cur += ARRAY_SIZE(llcp_magic);
573 memcpy(gb_cur, version_tlv, version_length);
574 gb_cur += version_length;
576 memcpy(gb_cur, lto_tlv, lto_length);
577 gb_cur += lto_length;
579 memcpy(gb_cur, wks_tlv, wks_length);
580 gb_cur += wks_length;
582 memcpy(gb_cur, miux_tlv, miux_length);
583 gb_cur += miux_length;
585 local->gb_len = gb_len;
596 u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
598 struct nfc_llcp_local *local;
600 local = nfc_llcp_find_local(dev);
602 *general_bytes_len = 0;
606 nfc_llcp_build_gb(local);
608 *general_bytes_len = local->gb_len;
613 int nfc_llcp_set_remote_gb(struct nfc_dev *dev, const u8 *gb, u8 gb_len)
615 struct nfc_llcp_local *local;
617 if (gb_len < 3 || gb_len > NFC_MAX_GT_LEN)
620 local = nfc_llcp_find_local(dev);
622 pr_err("No LLCP device\n");
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;
630 if (memcmp(local->remote_gb, llcp_magic, 3)) {
631 pr_err("MAC does not support LLCP\n");
635 return nfc_llcp_parse_gb_tlv(local,
636 &local->remote_gb[3],
637 local->remote_gb_len - 3);
640 static u8 nfc_llcp_dsap(const struct sk_buff *pdu)
642 return (pdu->data[0] & 0xfc) >> 2;
645 static u8 nfc_llcp_ptype(const struct sk_buff *pdu)
647 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
650 static u8 nfc_llcp_ssap(const struct sk_buff *pdu)
652 return pdu->data[1] & 0x3f;
655 static u8 nfc_llcp_ns(const struct sk_buff *pdu)
657 return pdu->data[2] >> 4;
660 static u8 nfc_llcp_nr(const struct sk_buff *pdu)
662 return pdu->data[2] & 0xf;
665 static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
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;
672 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
673 struct sk_buff *skb, u8 direction)
675 struct sk_buff *skb_copy = NULL, *nskb;
679 read_lock(&local->raw_sockets.lock);
681 sk_for_each(sk, &local->raw_sockets.head) {
682 if (sk->sk_state != LLCP_BOUND)
685 if (skb_copy == NULL) {
686 skb_copy = __pskb_copy_fclone(skb, NFC_RAW_HEADER_SIZE,
689 if (skb_copy == NULL)
692 data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
694 data[0] = local->dev ? local->dev->idx : 0xFF;
695 data[1] = direction & 0x01;
696 data[1] |= (RAW_PAYLOAD_LLCP << 1);
699 nskb = skb_clone(skb_copy, GFP_ATOMIC);
703 if (sock_queue_rcv_skb(sk, nskb))
707 read_unlock(&local->raw_sockets.lock);
712 static void nfc_llcp_tx_work(struct work_struct *work)
714 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
718 struct nfc_llcp_sock *llcp_sock;
720 skb = skb_dequeue(&local->tx_queue);
723 llcp_sock = nfc_llcp_sock(sk);
725 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
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);
732 struct sk_buff *copy_skb = NULL;
733 u8 ptype = nfc_llcp_ptype(skb);
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);
740 if (ptype == LLCP_PDU_DISC && sk != NULL &&
741 sk->sk_state == LLCP_DISCONNECTING) {
742 nfc_llcp_sock_unlink(&local->sockets, sk);
747 if (ptype == LLCP_PDU_I)
748 copy_skb = skb_copy(skb, GFP_ATOMIC);
750 __net_timestamp(skb);
752 nfc_llcp_send_to_raw_sock(local, skb,
755 ret = nfc_data_exchange(local->dev, local->target_idx,
756 skb, nfc_llcp_recv, local);
763 if (ptype == LLCP_PDU_I && copy_skb)
764 skb_queue_tail(&llcp_sock->tx_pending_queue,
768 nfc_llcp_send_symm(local->dev);
772 mod_timer(&local->link_timer,
773 jiffies + msecs_to_jiffies(2 * local->remote_lto));
776 static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
780 struct nfc_llcp_sock *llcp_sock;
782 read_lock(&local->connecting_sockets.lock);
784 sk_for_each(sk, &local->connecting_sockets.head) {
785 llcp_sock = nfc_llcp_sock(sk);
787 if (llcp_sock->ssap == ssap) {
788 sock_hold(&llcp_sock->sk);
796 read_unlock(&local->connecting_sockets.lock);
801 static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
802 const u8 *sn, size_t sn_len)
804 struct nfc_llcp_sock *llcp_sock;
806 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
808 if (llcp_sock == NULL)
811 sock_hold(&llcp_sock->sk);
816 static const u8 *nfc_llcp_connect_sn(const struct sk_buff *skb, size_t *sn_len)
819 const u8 *tlv = &skb->data[2];
820 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
822 while (offset < tlv_array_len) {
826 pr_debug("type 0x%x length %d\n", type, length);
828 if (type == LLCP_TLV_SN) {
833 offset += length + 2;
840 static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
843 struct nfc_llcp_sock *llcp_sock;
844 struct nfc_llcp_ui_cb *ui_cb;
847 dsap = nfc_llcp_dsap(skb);
848 ssap = nfc_llcp_ssap(skb);
850 ui_cb = nfc_llcp_ui_skb_cb(skb);
854 pr_debug("%d %d\n", dsap, ssap);
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)
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)) {
865 * UI frames will be freed from the socket layer, so we
866 * need to keep them alive until someone receives them.
870 pr_err("Receive queue is full\n");
873 nfc_llcp_sock_put(llcp_sock);
876 static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
877 const struct sk_buff *skb)
879 struct sock *new_sk, *parent;
880 struct nfc_llcp_sock *sock, *new_sock;
881 u8 dsap, ssap, reason;
883 dsap = nfc_llcp_dsap(skb);
884 ssap = nfc_llcp_ssap(skb);
886 pr_debug("%d %d\n", dsap, ssap);
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;
898 sn = nfc_llcp_connect_sn(skb, &sn_len);
900 reason = LLCP_DM_NOBOUND;
904 pr_debug("Service name length %zu\n", sn_len);
906 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
908 reason = LLCP_DM_NOBOUND;
913 lock_sock(&sock->sk);
917 if (sk_acceptq_is_full(parent)) {
918 reason = LLCP_DM_REJ;
919 release_sock(&sock->sk);
924 if (sock->ssap == LLCP_SDP_UNBOUND) {
925 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
927 pr_debug("First client, reserving %d\n", ssap);
929 if (ssap == LLCP_SAP_MAX) {
930 reason = LLCP_DM_REJ;
931 release_sock(&sock->sk);
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);
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;
960 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
963 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
965 atomic_inc(client_count);
966 new_sock->reserved_ssap = sock->ssap;
969 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
970 skb->len - LLCP_HEADER_SIZE);
972 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
974 nfc_llcp_sock_link(&local->sockets, new_sk);
976 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
978 nfc_get_device(local->dev->idx);
980 new_sk->sk_state = LLCP_CONNECTED;
982 /* Wake the listening processes */
983 parent->sk_data_ready(parent);
986 nfc_llcp_send_cc(new_sock);
988 release_sock(&sock->sk);
995 nfc_llcp_send_dm(local, dsap, ssap, reason);
998 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
1001 struct nfc_llcp_local *local = sock->local;
1003 pr_debug("Remote ready %d tx queue len %d remote rw %d",
1004 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
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;
1012 pdu = skb_dequeue(&sock->tx_queue);
1016 /* Update N(S)/N(R) */
1017 nfc_llcp_set_nrns(sock, pdu);
1019 skb_queue_tail(&local->tx_queue, pdu);
1026 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
1027 struct sk_buff *skb)
1029 struct nfc_llcp_sock *llcp_sock;
1031 u8 dsap, ssap, ptype, ns, nr;
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);
1039 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
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);
1047 sk = &llcp_sock->sk;
1049 if (sk->sk_state == LLCP_CLOSED) {
1051 nfc_llcp_sock_put(llcp_sock);
1054 /* Pass the payload upstream */
1055 if (ptype == LLCP_PDU_I) {
1056 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
1058 if (ns == llcp_sock->recv_n)
1059 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
1061 pr_err("Received out of sequence I PDU\n");
1063 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
1064 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
1066 * I frames will be freed from the socket layer, so we
1067 * need to keep them alive until someone receives them.
1071 pr_err("Receive queue is full\n");
1075 /* Remove skbs from the pending queue */
1076 if (llcp_sock->send_ack_n != nr) {
1077 struct sk_buff *s, *tmp;
1080 llcp_sock->send_ack_n = nr;
1082 /* Remove and free all skbs until ns == nr */
1083 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
1086 skb_unlink(s, &llcp_sock->tx_pending_queue);
1093 /* Re-queue the remaining skbs for transmission */
1094 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
1096 skb_unlink(s, &llcp_sock->tx_pending_queue);
1097 skb_queue_head(&local->tx_queue, s);
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;
1106 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
1107 nfc_llcp_send_rr(llcp_sock);
1110 nfc_llcp_sock_put(llcp_sock);
1113 static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
1114 const struct sk_buff *skb)
1116 struct nfc_llcp_sock *llcp_sock;
1120 dsap = nfc_llcp_dsap(skb);
1121 ssap = nfc_llcp_ssap(skb);
1123 if ((dsap == 0) && (ssap == 0)) {
1124 pr_debug("Connection termination");
1125 nfc_dep_link_down(local->dev);
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);
1135 sk = &llcp_sock->sk;
1138 nfc_llcp_socket_purge(llcp_sock);
1140 if (sk->sk_state == LLCP_CLOSED) {
1142 nfc_llcp_sock_put(llcp_sock);
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);
1151 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1154 nfc_llcp_sock_put(llcp_sock);
1157 static void nfc_llcp_recv_cc(struct nfc_llcp_local *local,
1158 const struct sk_buff *skb)
1160 struct nfc_llcp_sock *llcp_sock;
1164 dsap = nfc_llcp_dsap(skb);
1165 ssap = nfc_llcp_ssap(skb);
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);
1175 sk = &llcp_sock->sk;
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;
1182 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1183 skb->len - LLCP_HEADER_SIZE);
1185 sk->sk_state = LLCP_CONNECTED;
1186 sk->sk_state_change(sk);
1188 nfc_llcp_sock_put(llcp_sock);
1191 static void nfc_llcp_recv_dm(struct nfc_llcp_local *local,
1192 const struct sk_buff *skb)
1194 struct nfc_llcp_sock *llcp_sock;
1196 u8 dsap, ssap, reason;
1198 dsap = nfc_llcp_dsap(skb);
1199 ssap = nfc_llcp_ssap(skb);
1200 reason = skb->data[2];
1202 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1205 case LLCP_DM_NOBOUND:
1207 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1211 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1215 if (llcp_sock == NULL) {
1216 pr_debug("Already closed\n");
1220 sk = &llcp_sock->sk;
1223 sk->sk_state = LLCP_CLOSED;
1224 sk->sk_state_change(sk);
1226 nfc_llcp_sock_put(llcp_sock);
1229 static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1230 const struct sk_buff *skb)
1232 struct nfc_llcp_sock *llcp_sock;
1233 u8 dsap, ssap, type, length, tid, sap;
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);
1243 dsap = nfc_llcp_dsap(skb);
1244 ssap = nfc_llcp_ssap(skb);
1246 pr_debug("%d %d\n", dsap, ssap);
1248 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1249 pr_err("Wrong SNL SAP\n");
1253 tlv = &skb->data[LLCP_HEADER_SIZE];
1254 tlv_len = skb->len - LLCP_HEADER_SIZE;
1258 while (offset < tlv_len) {
1263 case LLCP_TLV_SDREQ:
1265 service_name = (char *) &tlv[3];
1266 service_name_len = length - 1;
1268 pr_debug("Looking for %.16s\n", service_name);
1270 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1271 !strncmp(service_name, "urn:nfc:sn:sdp",
1272 service_name_len)) {
1277 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
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.
1289 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1290 atomic_t *client_count;
1292 sap = nfc_llcp_reserve_sdp_ssap(local);
1294 pr_debug("Reserving %d\n", sap);
1296 if (sap == LLCP_SAP_MAX) {
1302 &local->local_sdp_cnt[sap -
1305 atomic_inc(client_count);
1307 llcp_sock->ssap = sap;
1308 llcp_sock->reserved_ssap = sap;
1310 sap = llcp_sock->ssap;
1313 pr_debug("%p %d\n", llcp_sock, sap);
1316 sdp = nfc_llcp_build_sdres_tlv(tid, sap);
1320 sdres_tlvs_len += sdp->tlv_len;
1321 hlist_add_head(&sdp->node, &llc_sdres_list);
1324 case LLCP_TLV_SDRES:
1325 mutex_lock(&local->sdreq_lock);
1327 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv[2]);
1329 hlist_for_each_entry(sdp, &local->pending_sdreqs, node) {
1330 if (sdp->tid != tlv[2])
1335 pr_debug("Found: uri=%s, sap=%d\n",
1336 sdp->uri, sdp->sap);
1338 hlist_del(&sdp->node);
1340 hlist_add_head(&sdp->node, &nl_sdres_list);
1345 mutex_unlock(&local->sdreq_lock);
1349 pr_err("Invalid SNL tlv value 0x%x\n", type);
1353 offset += length + 2;
1358 if (!hlist_empty(&nl_sdres_list))
1359 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
1361 if (!hlist_empty(&llc_sdres_list))
1362 nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);
1365 static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb)
1369 struct sk_buff *new_skb;
1371 if (skb->len <= LLCP_HEADER_SIZE) {
1372 pr_err("Malformed AGF PDU\n");
1376 skb_pull(skb, LLCP_HEADER_SIZE);
1378 while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) {
1379 pdu_len = skb->data[0] << 8 | skb->data[1];
1381 skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE);
1383 if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) {
1384 pr_err("Malformed AGF PDU\n");
1388 ptype = nfc_llcp_ptype(skb);
1390 if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF)
1393 new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL);
1394 if (new_skb == NULL) {
1395 pr_err("Could not allocate PDU\n");
1399 skb_put_data(new_skb, skb->data, pdu_len);
1401 nfc_llcp_rx_skb(local, new_skb);
1405 skb_pull(skb, pdu_len);
1409 static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb)
1411 u8 dsap, ssap, ptype;
1413 ptype = nfc_llcp_ptype(skb);
1414 dsap = nfc_llcp_dsap(skb);
1415 ssap = nfc_llcp_ssap(skb);
1417 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1419 if (ptype != LLCP_PDU_SYMM)
1420 print_hex_dump_debug("LLCP Rx: ", DUMP_PREFIX_OFFSET, 16, 1,
1421 skb->data, skb->len, true);
1430 nfc_llcp_recv_ui(local, skb);
1433 case LLCP_PDU_CONNECT:
1434 pr_debug("CONNECT\n");
1435 nfc_llcp_recv_connect(local, skb);
1440 nfc_llcp_recv_disc(local, skb);
1445 nfc_llcp_recv_cc(local, skb);
1450 nfc_llcp_recv_dm(local, skb);
1455 nfc_llcp_recv_snl(local, skb);
1461 pr_debug("I frame\n");
1462 nfc_llcp_recv_hdlc(local, skb);
1466 pr_debug("AGF frame\n");
1467 nfc_llcp_recv_agf(local, skb);
1472 static void nfc_llcp_rx_work(struct work_struct *work)
1474 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
1476 struct sk_buff *skb;
1478 skb = local->rx_pending;
1480 pr_debug("No pending SKB\n");
1484 __net_timestamp(skb);
1486 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX);
1488 nfc_llcp_rx_skb(local, skb);
1490 schedule_work(&local->tx_work);
1491 kfree_skb(local->rx_pending);
1492 local->rx_pending = NULL;
1495 static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
1497 local->rx_pending = skb;
1498 del_timer(&local->link_timer);
1499 schedule_work(&local->rx_work);
1502 void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1504 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1507 pr_err("LLCP PDU receive err %d\n", err);
1511 __nfc_llcp_recv(local, skb);
1514 int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1516 struct nfc_llcp_local *local;
1518 local = nfc_llcp_find_local(dev);
1519 if (local == NULL) {
1524 __nfc_llcp_recv(local, skb);
1529 void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1531 struct nfc_llcp_local *local;
1533 local = nfc_llcp_find_local(dev);
1537 local->remote_miu = LLCP_DEFAULT_MIU;
1538 local->remote_lto = LLCP_DEFAULT_LTO;
1540 /* Close and purge all existing sockets */
1541 nfc_llcp_socket_release(local, true, 0);
1544 void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1545 u8 comm_mode, u8 rf_mode)
1547 struct nfc_llcp_local *local;
1549 pr_debug("rf mode %d\n", rf_mode);
1551 local = nfc_llcp_find_local(dev);
1555 local->target_idx = target_idx;
1556 local->comm_mode = comm_mode;
1557 local->rf_mode = rf_mode;
1559 if (rf_mode == NFC_RF_INITIATOR) {
1560 pr_debug("Queueing Tx work\n");
1562 schedule_work(&local->tx_work);
1564 mod_timer(&local->link_timer,
1565 jiffies + msecs_to_jiffies(local->remote_lto));
1569 int nfc_llcp_register_device(struct nfc_dev *ndev)
1571 struct nfc_llcp_local *local;
1573 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
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);
1583 skb_queue_head_init(&local->tx_queue);
1584 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
1586 local->rx_pending = NULL;
1587 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
1589 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
1591 rwlock_init(&local->sockets.lock);
1592 rwlock_init(&local->connecting_sockets.lock);
1593 rwlock_init(&local->raw_sockets.lock);
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 */
1600 nfc_llcp_build_gb(local);
1602 local->remote_miu = LLCP_DEFAULT_MIU;
1603 local->remote_lto = LLCP_DEFAULT_LTO;
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);
1610 list_add(&local->list, &llcp_devices);
1615 void nfc_llcp_unregister_device(struct nfc_dev *dev)
1617 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1619 if (local == NULL) {
1620 pr_debug("No such device\n");
1624 local_cleanup(local);
1626 nfc_llcp_local_put(local);
1629 int __init nfc_llcp_init(void)
1631 return nfc_llcp_sock_init();
1634 void nfc_llcp_exit(void)
1636 nfc_llcp_sock_exit();