1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Intel Corporation. All rights reserved.
6 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/nfc.h>
13 #include <net/nfc/nfc.h>
18 static const u8 llcp_tlv_length[LLCP_TLV_MAX] = {
32 static u8 llcp_tlv8(const u8 *tlv, u8 type)
34 if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
40 static u16 llcp_tlv16(const u8 *tlv, u8 type)
42 if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
45 return be16_to_cpu(*((__be16 *)(tlv + 2)));
49 static u8 llcp_tlv_version(const u8 *tlv)
51 return llcp_tlv8(tlv, LLCP_TLV_VERSION);
54 static u16 llcp_tlv_miux(const u8 *tlv)
56 return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff;
59 static u16 llcp_tlv_wks(const u8 *tlv)
61 return llcp_tlv16(tlv, LLCP_TLV_WKS);
64 static u16 llcp_tlv_lto(const u8 *tlv)
66 return llcp_tlv8(tlv, LLCP_TLV_LTO);
69 static u8 llcp_tlv_opt(const u8 *tlv)
71 return llcp_tlv8(tlv, LLCP_TLV_OPT);
74 static u8 llcp_tlv_rw(const u8 *tlv)
76 return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf;
79 u8 *nfc_llcp_build_tlv(u8 type, const u8 *value, u8 value_length, u8 *tlv_length)
83 pr_debug("type %d\n", type);
85 if (type >= LLCP_TLV_MAX)
88 length = llcp_tlv_length[type];
89 if (length == 0 && value_length == 0)
92 length = value_length;
94 *tlv_length = 2 + length;
95 tlv = kzalloc(2 + length, GFP_KERNEL);
101 memcpy(tlv + 2, value, length);
106 struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdres_tlv(u8 tid, u8 sap)
108 struct nfc_llcp_sdp_tlv *sdres;
111 sdres = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL);
118 sdres->tlv = nfc_llcp_build_tlv(LLCP_TLV_SDRES, value, 2,
120 if (sdres->tlv == NULL) {
128 INIT_HLIST_NODE(&sdres->node);
133 struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdreq_tlv(u8 tid, const char *uri,
136 struct nfc_llcp_sdp_tlv *sdreq;
138 pr_debug("uri: %s, len: %zu\n", uri, uri_len);
140 /* sdreq->tlv_len is u8, takes uri_len, + 3 for header, + 1 for NULL */
141 if (WARN_ON_ONCE(uri_len > U8_MAX - 4))
144 sdreq = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL);
148 sdreq->tlv_len = uri_len + 3;
150 if (uri[uri_len - 1] == 0)
153 sdreq->tlv = kzalloc(sdreq->tlv_len + 1, GFP_KERNEL);
154 if (sdreq->tlv == NULL) {
159 sdreq->tlv[0] = LLCP_TLV_SDREQ;
160 sdreq->tlv[1] = sdreq->tlv_len - 2;
164 sdreq->uri = sdreq->tlv + 3;
165 memcpy(sdreq->uri, uri, uri_len);
167 sdreq->time = jiffies;
169 INIT_HLIST_NODE(&sdreq->node);
174 void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp)
180 void nfc_llcp_free_sdp_tlv_list(struct hlist_head *head)
182 struct nfc_llcp_sdp_tlv *sdp;
183 struct hlist_node *n;
185 hlist_for_each_entry_safe(sdp, n, head, node) {
186 hlist_del(&sdp->node);
188 nfc_llcp_free_sdp_tlv(sdp);
192 int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
193 const u8 *tlv_array, u16 tlv_array_len)
195 const u8 *tlv = tlv_array;
196 u8 type, length, offset = 0;
198 pr_debug("TLV array length %d\n", tlv_array_len);
203 while (offset < tlv_array_len) {
207 pr_debug("type 0x%x length %d\n", type, length);
210 case LLCP_TLV_VERSION:
211 local->remote_version = llcp_tlv_version(tlv);
214 local->remote_miu = llcp_tlv_miux(tlv) + 128;
217 local->remote_wks = llcp_tlv_wks(tlv);
220 local->remote_lto = llcp_tlv_lto(tlv) * 10;
223 local->remote_opt = llcp_tlv_opt(tlv);
226 pr_err("Invalid gt tlv value 0x%x\n", type);
230 offset += length + 2;
234 pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x\n",
235 local->remote_version, local->remote_miu,
236 local->remote_lto, local->remote_opt,
242 int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock,
243 const u8 *tlv_array, u16 tlv_array_len)
245 const u8 *tlv = tlv_array;
246 u8 type, length, offset = 0;
248 pr_debug("TLV array length %d\n", tlv_array_len);
253 while (offset < tlv_array_len) {
257 pr_debug("type 0x%x length %d\n", type, length);
261 sock->remote_miu = llcp_tlv_miux(tlv) + 128;
264 sock->remote_rw = llcp_tlv_rw(tlv);
269 pr_err("Invalid gt tlv value 0x%x\n", type);
273 offset += length + 2;
277 pr_debug("sock %p rw %d miu %d\n", sock,
278 sock->remote_rw, sock->remote_miu);
283 static struct sk_buff *llcp_add_header(struct sk_buff *pdu,
284 u8 dsap, u8 ssap, u8 ptype)
288 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
290 header[0] = (u8)((dsap << 2) | (ptype >> 2));
291 header[1] = (u8)((ptype << 6) | ssap);
293 pr_debug("header 0x%x 0x%x\n", header[0], header[1]);
295 skb_put_data(pdu, header, LLCP_HEADER_SIZE);
300 static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, const u8 *tlv,
303 /* XXX Add an skb length check */
308 skb_put_data(pdu, tlv, tlv_length);
313 static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock,
322 skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
323 size + LLCP_HEADER_SIZE, &err);
325 pr_err("Could not allocate PDU\n");
329 skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd);
334 int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock)
338 struct nfc_llcp_local *local;
348 skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
352 skb_queue_tail(&local->tx_queue, skb);
357 int nfc_llcp_send_symm(struct nfc_dev *dev)
360 struct nfc_llcp_local *local;
364 local = nfc_llcp_find_local(dev);
368 size += LLCP_HEADER_SIZE;
369 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
371 skb = alloc_skb(size, GFP_KERNEL);
377 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
379 skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM);
381 __net_timestamp(skb);
383 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX);
385 err = nfc_data_exchange(dev, local->target_idx, skb,
386 nfc_llcp_recv, local);
388 nfc_llcp_local_put(local);
392 int nfc_llcp_send_connect(struct nfc_llcp_sock *sock)
394 struct nfc_llcp_local *local;
396 const u8 *service_name_tlv = NULL;
397 const u8 *miux_tlv = NULL;
398 const u8 *rw_tlv = NULL;
399 u8 service_name_tlv_length = 0;
400 u8 miux_tlv_length, rw_tlv_length, rw;
409 if (sock->service_name != NULL) {
410 service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN,
412 sock->service_name_len,
413 &service_name_tlv_length);
414 if (!service_name_tlv) {
418 size += service_name_tlv_length;
421 /* If the socket parameters are not set, use the local ones */
422 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
423 local->miux : sock->miux;
424 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
426 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
432 size += miux_tlv_length;
434 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
439 size += rw_tlv_length;
441 pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len);
443 skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size);
449 llcp_add_tlv(skb, service_name_tlv, service_name_tlv_length);
450 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
451 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
453 skb_queue_tail(&local->tx_queue, skb);
459 pr_err("error %d\n", err);
461 kfree(service_name_tlv);
468 int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
470 struct nfc_llcp_local *local;
472 const u8 *miux_tlv = NULL;
473 const u8 *rw_tlv = NULL;
474 u8 miux_tlv_length, rw_tlv_length, rw;
483 /* If the socket parameters are not set, use the local ones */
484 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
485 local->miux : sock->miux;
486 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
488 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
494 size += miux_tlv_length;
496 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
501 size += rw_tlv_length;
503 skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size);
509 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
510 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
512 skb_queue_tail(&local->tx_queue, skb);
518 pr_err("error %d\n", err);
526 static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
534 return ERR_PTR(-ENODEV);
538 return ERR_PTR(-ENODEV);
540 size += LLCP_HEADER_SIZE;
541 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
544 skb = alloc_skb(size, GFP_KERNEL);
546 return ERR_PTR(-ENOMEM);
548 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
550 skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL);
555 int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local,
556 struct hlist_head *tlv_list, size_t tlvs_len)
558 struct nfc_llcp_sdp_tlv *sdp;
559 struct hlist_node *n;
562 skb = nfc_llcp_allocate_snl(local, tlvs_len);
566 hlist_for_each_entry_safe(sdp, n, tlv_list, node) {
567 skb_put_data(skb, sdp->tlv, sdp->tlv_len);
569 hlist_del(&sdp->node);
571 nfc_llcp_free_sdp_tlv(sdp);
574 skb_queue_tail(&local->tx_queue, skb);
579 int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local,
580 struct hlist_head *tlv_list, size_t tlvs_len)
582 struct nfc_llcp_sdp_tlv *sdreq;
583 struct hlist_node *n;
586 skb = nfc_llcp_allocate_snl(local, tlvs_len);
590 mutex_lock(&local->sdreq_lock);
592 if (hlist_empty(&local->pending_sdreqs))
593 mod_timer(&local->sdreq_timer,
594 jiffies + msecs_to_jiffies(3 * local->remote_lto));
596 hlist_for_each_entry_safe(sdreq, n, tlv_list, node) {
597 pr_debug("tid %d for %s\n", sdreq->tid, sdreq->uri);
599 skb_put_data(skb, sdreq->tlv, sdreq->tlv_len);
601 hlist_del(&sdreq->node);
603 hlist_add_head(&sdreq->node, &local->pending_sdreqs);
606 mutex_unlock(&local->sdreq_lock);
608 skb_queue_tail(&local->tx_queue, skb);
613 int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason)
617 u16 size = 1; /* Reason code */
619 pr_debug("Sending DM reason 0x%x\n", reason);
628 size += LLCP_HEADER_SIZE;
629 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
631 skb = alloc_skb(size, GFP_KERNEL);
635 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
637 skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM);
639 skb_put_data(skb, &reason, 1);
641 skb_queue_head(&local->tx_queue, skb);
646 int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
647 struct msghdr *msg, size_t len)
650 struct sock *sk = &sock->sk;
651 struct nfc_llcp_local *local;
652 size_t frag_len = 0, remaining_len;
653 u8 *msg_data, *msg_ptr;
656 pr_debug("Send I frame len %zd\n", len);
662 /* Remote is ready but has not acknowledged our frames */
663 if((sock->remote_ready &&
664 skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw &&
665 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
666 pr_err("Pending queue is full %d frames\n",
667 skb_queue_len(&sock->tx_pending_queue));
671 /* Remote is not ready and we've been queueing enough frames */
672 if ((!sock->remote_ready &&
673 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
674 pr_err("Tx queue is full %d frames\n",
675 skb_queue_len(&sock->tx_queue));
679 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
680 if (msg_data == NULL)
683 if (memcpy_from_msg(msg_data, msg, len)) {
692 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
693 LLCP_DEFAULT_MIU : sock->remote_miu;
695 frag_len = min_t(size_t, remote_miu, remaining_len);
697 pr_debug("Fragment %zd bytes remaining %zd",
698 frag_len, remaining_len);
700 pdu = llcp_allocate_pdu(sock, LLCP_PDU_I,
701 frag_len + LLCP_SEQUENCE_SIZE);
707 skb_put(pdu, LLCP_SEQUENCE_SIZE);
709 if (likely(frag_len > 0))
710 skb_put_data(pdu, msg_ptr, frag_len);
712 skb_queue_tail(&sock->tx_queue, pdu);
716 nfc_llcp_queue_i_frames(sock);
720 remaining_len -= frag_len;
722 } while (remaining_len > 0);
729 int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap,
730 struct msghdr *msg, size_t len)
733 struct nfc_llcp_local *local;
734 size_t frag_len = 0, remaining_len;
735 u8 *msg_ptr, *msg_data;
739 pr_debug("Send UI frame len %zd\n", len);
745 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
746 if (msg_data == NULL)
749 if (memcpy_from_msg(msg_data, msg, len)) {
758 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
759 local->remote_miu : sock->remote_miu;
761 frag_len = min_t(size_t, remote_miu, remaining_len);
763 pr_debug("Fragment %zd bytes remaining %zd",
764 frag_len, remaining_len);
766 pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, 0,
767 frag_len + LLCP_HEADER_SIZE, &err);
769 pr_err("Could not allocate PDU (error=%d)\n", err);
770 len -= remaining_len;
776 pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI);
778 if (likely(frag_len > 0))
779 skb_put_data(pdu, msg_ptr, frag_len);
781 /* No need to check for the peer RW for UI frames */
782 skb_queue_tail(&local->tx_queue, pdu);
784 remaining_len -= frag_len;
786 } while (remaining_len > 0);
793 int nfc_llcp_send_rr(struct nfc_llcp_sock *sock)
796 struct nfc_llcp_local *local;
798 pr_debug("Send rr nr %d\n", sock->recv_n);
804 skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE);
808 skb_put(skb, LLCP_SEQUENCE_SIZE);
810 skb->data[2] = sock->recv_n;
812 skb_queue_head(&local->tx_queue, skb);