Loading...
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
13#include <net/nfc/nfc.h>
14
15#include "nfc.h"
16#include "llcp.h"
17
18static u8 llcp_tlv_length[LLCP_TLV_MAX] = {
19 0,
20 1, /* VERSION */
21 2, /* MIUX */
22 2, /* WKS */
23 1, /* LTO */
24 1, /* RW */
25 0, /* SN */
26 1, /* OPT */
27 0, /* SDREQ */
28 2, /* SDRES */
29
30};
31
32static u8 llcp_tlv8(u8 *tlv, u8 type)
33{
34 if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
35 return 0;
36
37 return tlv[2];
38}
39
40static u16 llcp_tlv16(u8 *tlv, u8 type)
41{
42 if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
43 return 0;
44
45 return be16_to_cpu(*((__be16 *)(tlv + 2)));
46}
47
48
49static u8 llcp_tlv_version(u8 *tlv)
50{
51 return llcp_tlv8(tlv, LLCP_TLV_VERSION);
52}
53
54static u16 llcp_tlv_miux(u8 *tlv)
55{
56 return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff;
57}
58
59static u16 llcp_tlv_wks(u8 *tlv)
60{
61 return llcp_tlv16(tlv, LLCP_TLV_WKS);
62}
63
64static u16 llcp_tlv_lto(u8 *tlv)
65{
66 return llcp_tlv8(tlv, LLCP_TLV_LTO);
67}
68
69static u8 llcp_tlv_opt(u8 *tlv)
70{
71 return llcp_tlv8(tlv, LLCP_TLV_OPT);
72}
73
74static u8 llcp_tlv_rw(u8 *tlv)
75{
76 return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf;
77}
78
79u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length)
80{
81 u8 *tlv, length;
82
83 pr_debug("type %d\n", type);
84
85 if (type >= LLCP_TLV_MAX)
86 return NULL;
87
88 length = llcp_tlv_length[type];
89 if (length == 0 && value_length == 0)
90 return NULL;
91 else if (length == 0)
92 length = value_length;
93
94 *tlv_length = 2 + length;
95 tlv = kzalloc(2 + length, GFP_KERNEL);
96 if (tlv == NULL)
97 return tlv;
98
99 tlv[0] = type;
100 tlv[1] = length;
101 memcpy(tlv + 2, value, length);
102
103 return tlv;
104}
105
106struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdres_tlv(u8 tid, u8 sap)
107{
108 struct nfc_llcp_sdp_tlv *sdres;
109 u8 value[2];
110
111 sdres = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL);
112 if (sdres == NULL)
113 return NULL;
114
115 value[0] = tid;
116 value[1] = sap;
117
118 sdres->tlv = nfc_llcp_build_tlv(LLCP_TLV_SDRES, value, 2,
119 &sdres->tlv_len);
120 if (sdres->tlv == NULL) {
121 kfree(sdres);
122 return NULL;
123 }
124
125 sdres->tid = tid;
126 sdres->sap = sap;
127
128 INIT_HLIST_NODE(&sdres->node);
129
130 return sdres;
131}
132
133struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdreq_tlv(u8 tid, char *uri,
134 size_t uri_len)
135{
136 struct nfc_llcp_sdp_tlv *sdreq;
137
138 pr_debug("uri: %s, len: %zu\n", uri, uri_len);
139
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))
142 return NULL;
143
144 sdreq = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL);
145 if (sdreq == NULL)
146 return NULL;
147
148 sdreq->tlv_len = uri_len + 3;
149
150 if (uri[uri_len - 1] == 0)
151 sdreq->tlv_len--;
152
153 sdreq->tlv = kzalloc(sdreq->tlv_len + 1, GFP_KERNEL);
154 if (sdreq->tlv == NULL) {
155 kfree(sdreq);
156 return NULL;
157 }
158
159 sdreq->tlv[0] = LLCP_TLV_SDREQ;
160 sdreq->tlv[1] = sdreq->tlv_len - 2;
161 sdreq->tlv[2] = tid;
162
163 sdreq->tid = tid;
164 sdreq->uri = sdreq->tlv + 3;
165 memcpy(sdreq->uri, uri, uri_len);
166
167 sdreq->time = jiffies;
168
169 INIT_HLIST_NODE(&sdreq->node);
170
171 return sdreq;
172}
173
174void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp)
175{
176 kfree(sdp->tlv);
177 kfree(sdp);
178}
179
180void nfc_llcp_free_sdp_tlv_list(struct hlist_head *head)
181{
182 struct nfc_llcp_sdp_tlv *sdp;
183 struct hlist_node *n;
184
185 hlist_for_each_entry_safe(sdp, n, head, node) {
186 hlist_del(&sdp->node);
187
188 nfc_llcp_free_sdp_tlv(sdp);
189 }
190}
191
192int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
193 u8 *tlv_array, u16 tlv_array_len)
194{
195 u8 *tlv = tlv_array, type, length, offset = 0;
196
197 pr_debug("TLV array length %d\n", tlv_array_len);
198
199 if (local == NULL)
200 return -ENODEV;
201
202 while (offset < tlv_array_len) {
203 type = tlv[0];
204 length = tlv[1];
205
206 pr_debug("type 0x%x length %d\n", type, length);
207
208 switch (type) {
209 case LLCP_TLV_VERSION:
210 local->remote_version = llcp_tlv_version(tlv);
211 break;
212 case LLCP_TLV_MIUX:
213 local->remote_miu = llcp_tlv_miux(tlv) + 128;
214 break;
215 case LLCP_TLV_WKS:
216 local->remote_wks = llcp_tlv_wks(tlv);
217 break;
218 case LLCP_TLV_LTO:
219 local->remote_lto = llcp_tlv_lto(tlv) * 10;
220 break;
221 case LLCP_TLV_OPT:
222 local->remote_opt = llcp_tlv_opt(tlv);
223 break;
224 default:
225 pr_err("Invalid gt tlv value 0x%x\n", type);
226 break;
227 }
228
229 offset += length + 2;
230 tlv += length + 2;
231 }
232
233 pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x\n",
234 local->remote_version, local->remote_miu,
235 local->remote_lto, local->remote_opt,
236 local->remote_wks);
237
238 return 0;
239}
240
241int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock,
242 u8 *tlv_array, u16 tlv_array_len)
243{
244 u8 *tlv = tlv_array, type, length, offset = 0;
245
246 pr_debug("TLV array length %d\n", tlv_array_len);
247
248 if (sock == NULL)
249 return -ENOTCONN;
250
251 while (offset < tlv_array_len) {
252 type = tlv[0];
253 length = tlv[1];
254
255 pr_debug("type 0x%x length %d\n", type, length);
256
257 switch (type) {
258 case LLCP_TLV_MIUX:
259 sock->remote_miu = llcp_tlv_miux(tlv) + 128;
260 break;
261 case LLCP_TLV_RW:
262 sock->remote_rw = llcp_tlv_rw(tlv);
263 break;
264 case LLCP_TLV_SN:
265 break;
266 default:
267 pr_err("Invalid gt tlv value 0x%x\n", type);
268 break;
269 }
270
271 offset += length + 2;
272 tlv += length + 2;
273 }
274
275 pr_debug("sock %p rw %d miu %d\n", sock,
276 sock->remote_rw, sock->remote_miu);
277
278 return 0;
279}
280
281static struct sk_buff *llcp_add_header(struct sk_buff *pdu,
282 u8 dsap, u8 ssap, u8 ptype)
283{
284 u8 header[2];
285
286 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
287
288 header[0] = (u8)((dsap << 2) | (ptype >> 2));
289 header[1] = (u8)((ptype << 6) | ssap);
290
291 pr_debug("header 0x%x 0x%x\n", header[0], header[1]);
292
293 skb_put_data(pdu, header, LLCP_HEADER_SIZE);
294
295 return pdu;
296}
297
298static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv,
299 u8 tlv_length)
300{
301 /* XXX Add an skb length check */
302
303 if (tlv == NULL)
304 return NULL;
305
306 skb_put_data(pdu, tlv, tlv_length);
307
308 return pdu;
309}
310
311static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock,
312 u8 cmd, u16 size)
313{
314 struct sk_buff *skb;
315 int err;
316
317 if (sock->ssap == 0)
318 return NULL;
319
320 skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
321 size + LLCP_HEADER_SIZE, &err);
322 if (skb == NULL) {
323 pr_err("Could not allocate PDU\n");
324 return NULL;
325 }
326
327 skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd);
328
329 return skb;
330}
331
332int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock)
333{
334 struct sk_buff *skb;
335 struct nfc_dev *dev;
336 struct nfc_llcp_local *local;
337
338 pr_debug("Sending DISC\n");
339
340 local = sock->local;
341 if (local == NULL)
342 return -ENODEV;
343
344 dev = sock->dev;
345 if (dev == NULL)
346 return -ENODEV;
347
348 skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
349 if (skb == NULL)
350 return -ENOMEM;
351
352 skb_queue_tail(&local->tx_queue, skb);
353
354 return 0;
355}
356
357int nfc_llcp_send_symm(struct nfc_dev *dev)
358{
359 struct sk_buff *skb;
360 struct nfc_llcp_local *local;
361 u16 size = 0;
362
363 pr_debug("Sending SYMM\n");
364
365 local = nfc_llcp_find_local(dev);
366 if (local == NULL)
367 return -ENODEV;
368
369 size += LLCP_HEADER_SIZE;
370 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
371
372 skb = alloc_skb(size, GFP_KERNEL);
373 if (skb == NULL)
374 return -ENOMEM;
375
376 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
377
378 skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM);
379
380 __net_timestamp(skb);
381
382 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX);
383
384 return nfc_data_exchange(dev, local->target_idx, skb,
385 nfc_llcp_recv, local);
386}
387
388int nfc_llcp_send_connect(struct nfc_llcp_sock *sock)
389{
390 struct nfc_llcp_local *local;
391 struct sk_buff *skb;
392 u8 *service_name_tlv = NULL, service_name_tlv_length;
393 u8 *miux_tlv = NULL, miux_tlv_length;
394 u8 *rw_tlv = NULL, rw_tlv_length, rw;
395 int err;
396 u16 size = 0;
397 __be16 miux;
398
399 pr_debug("Sending CONNECT\n");
400
401 local = sock->local;
402 if (local == NULL)
403 return -ENODEV;
404
405 if (sock->service_name != NULL) {
406 service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN,
407 sock->service_name,
408 sock->service_name_len,
409 &service_name_tlv_length);
410 if (!service_name_tlv) {
411 err = -ENOMEM;
412 goto error_tlv;
413 }
414 size += service_name_tlv_length;
415 }
416
417 /* If the socket parameters are not set, use the local ones */
418 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
419 local->miux : sock->miux;
420 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
421
422 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
423 &miux_tlv_length);
424 if (!miux_tlv) {
425 err = -ENOMEM;
426 goto error_tlv;
427 }
428 size += miux_tlv_length;
429
430 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
431 if (!rw_tlv) {
432 err = -ENOMEM;
433 goto error_tlv;
434 }
435 size += rw_tlv_length;
436
437 pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len);
438
439 skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size);
440 if (skb == NULL) {
441 err = -ENOMEM;
442 goto error_tlv;
443 }
444
445 llcp_add_tlv(skb, service_name_tlv, service_name_tlv_length);
446 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
447 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
448
449 skb_queue_tail(&local->tx_queue, skb);
450
451 err = 0;
452
453error_tlv:
454 if (err)
455 pr_err("error %d\n", err);
456
457 kfree(service_name_tlv);
458 kfree(miux_tlv);
459 kfree(rw_tlv);
460
461 return err;
462}
463
464int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
465{
466 struct nfc_llcp_local *local;
467 struct sk_buff *skb;
468 u8 *miux_tlv = NULL, miux_tlv_length;
469 u8 *rw_tlv = NULL, rw_tlv_length, rw;
470 int err;
471 u16 size = 0;
472 __be16 miux;
473
474 pr_debug("Sending CC\n");
475
476 local = sock->local;
477 if (local == NULL)
478 return -ENODEV;
479
480 /* If the socket parameters are not set, use the local ones */
481 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
482 local->miux : sock->miux;
483 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
484
485 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
486 &miux_tlv_length);
487 if (!miux_tlv) {
488 err = -ENOMEM;
489 goto error_tlv;
490 }
491 size += miux_tlv_length;
492
493 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
494 if (!rw_tlv) {
495 err = -ENOMEM;
496 goto error_tlv;
497 }
498 size += rw_tlv_length;
499
500 skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size);
501 if (skb == NULL) {
502 err = -ENOMEM;
503 goto error_tlv;
504 }
505
506 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
507 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
508
509 skb_queue_tail(&local->tx_queue, skb);
510
511 err = 0;
512
513error_tlv:
514 if (err)
515 pr_err("error %d\n", err);
516
517 kfree(miux_tlv);
518 kfree(rw_tlv);
519
520 return err;
521}
522
523static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
524 size_t tlv_length)
525{
526 struct sk_buff *skb;
527 struct nfc_dev *dev;
528 u16 size = 0;
529
530 if (local == NULL)
531 return ERR_PTR(-ENODEV);
532
533 dev = local->dev;
534 if (dev == NULL)
535 return ERR_PTR(-ENODEV);
536
537 size += LLCP_HEADER_SIZE;
538 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
539 size += tlv_length;
540
541 skb = alloc_skb(size, GFP_KERNEL);
542 if (skb == NULL)
543 return ERR_PTR(-ENOMEM);
544
545 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
546
547 skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL);
548
549 return skb;
550}
551
552int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local,
553 struct hlist_head *tlv_list, size_t tlvs_len)
554{
555 struct nfc_llcp_sdp_tlv *sdp;
556 struct hlist_node *n;
557 struct sk_buff *skb;
558
559 skb = nfc_llcp_allocate_snl(local, tlvs_len);
560 if (IS_ERR(skb))
561 return PTR_ERR(skb);
562
563 hlist_for_each_entry_safe(sdp, n, tlv_list, node) {
564 skb_put_data(skb, sdp->tlv, sdp->tlv_len);
565
566 hlist_del(&sdp->node);
567
568 nfc_llcp_free_sdp_tlv(sdp);
569 }
570
571 skb_queue_tail(&local->tx_queue, skb);
572
573 return 0;
574}
575
576int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local,
577 struct hlist_head *tlv_list, size_t tlvs_len)
578{
579 struct nfc_llcp_sdp_tlv *sdreq;
580 struct hlist_node *n;
581 struct sk_buff *skb;
582
583 skb = nfc_llcp_allocate_snl(local, tlvs_len);
584 if (IS_ERR(skb))
585 return PTR_ERR(skb);
586
587 mutex_lock(&local->sdreq_lock);
588
589 if (hlist_empty(&local->pending_sdreqs))
590 mod_timer(&local->sdreq_timer,
591 jiffies + msecs_to_jiffies(3 * local->remote_lto));
592
593 hlist_for_each_entry_safe(sdreq, n, tlv_list, node) {
594 pr_debug("tid %d for %s\n", sdreq->tid, sdreq->uri);
595
596 skb_put_data(skb, sdreq->tlv, sdreq->tlv_len);
597
598 hlist_del(&sdreq->node);
599
600 hlist_add_head(&sdreq->node, &local->pending_sdreqs);
601 }
602
603 mutex_unlock(&local->sdreq_lock);
604
605 skb_queue_tail(&local->tx_queue, skb);
606
607 return 0;
608}
609
610int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason)
611{
612 struct sk_buff *skb;
613 struct nfc_dev *dev;
614 u16 size = 1; /* Reason code */
615
616 pr_debug("Sending DM reason 0x%x\n", reason);
617
618 if (local == NULL)
619 return -ENODEV;
620
621 dev = local->dev;
622 if (dev == NULL)
623 return -ENODEV;
624
625 size += LLCP_HEADER_SIZE;
626 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
627
628 skb = alloc_skb(size, GFP_KERNEL);
629 if (skb == NULL)
630 return -ENOMEM;
631
632 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
633
634 skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM);
635
636 skb_put_data(skb, &reason, 1);
637
638 skb_queue_head(&local->tx_queue, skb);
639
640 return 0;
641}
642
643int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
644 struct msghdr *msg, size_t len)
645{
646 struct sk_buff *pdu;
647 struct sock *sk = &sock->sk;
648 struct nfc_llcp_local *local;
649 size_t frag_len = 0, remaining_len;
650 u8 *msg_data, *msg_ptr;
651 u16 remote_miu;
652
653 pr_debug("Send I frame len %zd\n", len);
654
655 local = sock->local;
656 if (local == NULL)
657 return -ENODEV;
658
659 /* Remote is ready but has not acknowledged our frames */
660 if((sock->remote_ready &&
661 skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw &&
662 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
663 pr_err("Pending queue is full %d frames\n",
664 skb_queue_len(&sock->tx_pending_queue));
665 return -ENOBUFS;
666 }
667
668 /* Remote is not ready and we've been queueing enough frames */
669 if ((!sock->remote_ready &&
670 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
671 pr_err("Tx queue is full %d frames\n",
672 skb_queue_len(&sock->tx_queue));
673 return -ENOBUFS;
674 }
675
676 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
677 if (msg_data == NULL)
678 return -ENOMEM;
679
680 if (memcpy_from_msg(msg_data, msg, len)) {
681 kfree(msg_data);
682 return -EFAULT;
683 }
684
685 remaining_len = len;
686 msg_ptr = msg_data;
687
688 do {
689 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
690 LLCP_DEFAULT_MIU : sock->remote_miu;
691
692 frag_len = min_t(size_t, remote_miu, remaining_len);
693
694 pr_debug("Fragment %zd bytes remaining %zd",
695 frag_len, remaining_len);
696
697 pdu = llcp_allocate_pdu(sock, LLCP_PDU_I,
698 frag_len + LLCP_SEQUENCE_SIZE);
699 if (pdu == NULL) {
700 kfree(msg_data);
701 return -ENOMEM;
702 }
703
704 skb_put(pdu, LLCP_SEQUENCE_SIZE);
705
706 if (likely(frag_len > 0))
707 skb_put_data(pdu, msg_ptr, frag_len);
708
709 skb_queue_tail(&sock->tx_queue, pdu);
710
711 lock_sock(sk);
712
713 nfc_llcp_queue_i_frames(sock);
714
715 release_sock(sk);
716
717 remaining_len -= frag_len;
718 msg_ptr += frag_len;
719 } while (remaining_len > 0);
720
721 kfree(msg_data);
722
723 return len;
724}
725
726int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap,
727 struct msghdr *msg, size_t len)
728{
729 struct sk_buff *pdu;
730 struct nfc_llcp_local *local;
731 size_t frag_len = 0, remaining_len;
732 u8 *msg_ptr, *msg_data;
733 u16 remote_miu;
734 int err;
735
736 pr_debug("Send UI frame len %zd\n", len);
737
738 local = sock->local;
739 if (local == NULL)
740 return -ENODEV;
741
742 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
743 if (msg_data == NULL)
744 return -ENOMEM;
745
746 if (memcpy_from_msg(msg_data, msg, len)) {
747 kfree(msg_data);
748 return -EFAULT;
749 }
750
751 remaining_len = len;
752 msg_ptr = msg_data;
753
754 do {
755 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
756 local->remote_miu : sock->remote_miu;
757
758 frag_len = min_t(size_t, remote_miu, remaining_len);
759
760 pr_debug("Fragment %zd bytes remaining %zd",
761 frag_len, remaining_len);
762
763 pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, 0,
764 frag_len + LLCP_HEADER_SIZE, &err);
765 if (pdu == NULL) {
766 pr_err("Could not allocate PDU (error=%d)\n", err);
767 len -= remaining_len;
768 if (len == 0)
769 len = err;
770 break;
771 }
772
773 pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI);
774
775 if (likely(frag_len > 0))
776 skb_put_data(pdu, msg_ptr, frag_len);
777
778 /* No need to check for the peer RW for UI frames */
779 skb_queue_tail(&local->tx_queue, pdu);
780
781 remaining_len -= frag_len;
782 msg_ptr += frag_len;
783 } while (remaining_len > 0);
784
785 kfree(msg_data);
786
787 return len;
788}
789
790int nfc_llcp_send_rr(struct nfc_llcp_sock *sock)
791{
792 struct sk_buff *skb;
793 struct nfc_llcp_local *local;
794
795 pr_debug("Send rr nr %d\n", sock->recv_n);
796
797 local = sock->local;
798 if (local == NULL)
799 return -ENODEV;
800
801 skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE);
802 if (skb == NULL)
803 return -ENOMEM;
804
805 skb_put(skb, LLCP_SEQUENCE_SIZE);
806
807 skb->data[2] = sock->recv_n;
808
809 skb_queue_head(&local->tx_queue, skb);
810
811 return 0;
812}
1/*
2 * Copyright (C) 2011 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#define pr_fmt(fmt) "llcp: %s: " fmt, __func__
19
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/nfc.h>
24
25#include <net/nfc/nfc.h>
26
27#include "nfc.h"
28#include "llcp.h"
29
30static u8 llcp_tlv_length[LLCP_TLV_MAX] = {
31 0,
32 1, /* VERSION */
33 2, /* MIUX */
34 2, /* WKS */
35 1, /* LTO */
36 1, /* RW */
37 0, /* SN */
38 1, /* OPT */
39 0, /* SDREQ */
40 2, /* SDRES */
41
42};
43
44static u8 llcp_tlv8(u8 *tlv, u8 type)
45{
46 if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
47 return 0;
48
49 return tlv[2];
50}
51
52static u16 llcp_tlv16(u8 *tlv, u8 type)
53{
54 if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
55 return 0;
56
57 return be16_to_cpu(*((__be16 *)(tlv + 2)));
58}
59
60
61static u8 llcp_tlv_version(u8 *tlv)
62{
63 return llcp_tlv8(tlv, LLCP_TLV_VERSION);
64}
65
66static u16 llcp_tlv_miux(u8 *tlv)
67{
68 return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff;
69}
70
71static u16 llcp_tlv_wks(u8 *tlv)
72{
73 return llcp_tlv16(tlv, LLCP_TLV_WKS);
74}
75
76static u16 llcp_tlv_lto(u8 *tlv)
77{
78 return llcp_tlv8(tlv, LLCP_TLV_LTO);
79}
80
81static u8 llcp_tlv_opt(u8 *tlv)
82{
83 return llcp_tlv8(tlv, LLCP_TLV_OPT);
84}
85
86static u8 llcp_tlv_rw(u8 *tlv)
87{
88 return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf;
89}
90
91u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length)
92{
93 u8 *tlv, length;
94
95 pr_debug("type %d\n", type);
96
97 if (type >= LLCP_TLV_MAX)
98 return NULL;
99
100 length = llcp_tlv_length[type];
101 if (length == 0 && value_length == 0)
102 return NULL;
103 else if (length == 0)
104 length = value_length;
105
106 *tlv_length = 2 + length;
107 tlv = kzalloc(2 + length, GFP_KERNEL);
108 if (tlv == NULL)
109 return tlv;
110
111 tlv[0] = type;
112 tlv[1] = length;
113 memcpy(tlv + 2, value, length);
114
115 return tlv;
116}
117
118struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdres_tlv(u8 tid, u8 sap)
119{
120 struct nfc_llcp_sdp_tlv *sdres;
121 u8 value[2];
122
123 sdres = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL);
124 if (sdres == NULL)
125 return NULL;
126
127 value[0] = tid;
128 value[1] = sap;
129
130 sdres->tlv = nfc_llcp_build_tlv(LLCP_TLV_SDRES, value, 2,
131 &sdres->tlv_len);
132 if (sdres->tlv == NULL) {
133 kfree(sdres);
134 return NULL;
135 }
136
137 sdres->tid = tid;
138 sdres->sap = sap;
139
140 INIT_HLIST_NODE(&sdres->node);
141
142 return sdres;
143}
144
145struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdreq_tlv(u8 tid, char *uri,
146 size_t uri_len)
147{
148 struct nfc_llcp_sdp_tlv *sdreq;
149
150 pr_debug("uri: %s, len: %zu\n", uri, uri_len);
151
152 sdreq = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL);
153 if (sdreq == NULL)
154 return NULL;
155
156 sdreq->tlv_len = uri_len + 3;
157
158 if (uri[uri_len - 1] == 0)
159 sdreq->tlv_len--;
160
161 sdreq->tlv = kzalloc(sdreq->tlv_len + 1, GFP_KERNEL);
162 if (sdreq->tlv == NULL) {
163 kfree(sdreq);
164 return NULL;
165 }
166
167 sdreq->tlv[0] = LLCP_TLV_SDREQ;
168 sdreq->tlv[1] = sdreq->tlv_len - 2;
169 sdreq->tlv[2] = tid;
170
171 sdreq->tid = tid;
172 sdreq->uri = sdreq->tlv + 3;
173 memcpy(sdreq->uri, uri, uri_len);
174
175 sdreq->time = jiffies;
176
177 INIT_HLIST_NODE(&sdreq->node);
178
179 return sdreq;
180}
181
182void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp)
183{
184 kfree(sdp->tlv);
185 kfree(sdp);
186}
187
188void nfc_llcp_free_sdp_tlv_list(struct hlist_head *head)
189{
190 struct nfc_llcp_sdp_tlv *sdp;
191 struct hlist_node *n;
192
193 hlist_for_each_entry_safe(sdp, n, head, node) {
194 hlist_del(&sdp->node);
195
196 nfc_llcp_free_sdp_tlv(sdp);
197 }
198}
199
200int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
201 u8 *tlv_array, u16 tlv_array_len)
202{
203 u8 *tlv = tlv_array, type, length, offset = 0;
204
205 pr_debug("TLV array length %d\n", tlv_array_len);
206
207 if (local == NULL)
208 return -ENODEV;
209
210 while (offset < tlv_array_len) {
211 type = tlv[0];
212 length = tlv[1];
213
214 pr_debug("type 0x%x length %d\n", type, length);
215
216 switch (type) {
217 case LLCP_TLV_VERSION:
218 local->remote_version = llcp_tlv_version(tlv);
219 break;
220 case LLCP_TLV_MIUX:
221 local->remote_miu = llcp_tlv_miux(tlv) + 128;
222 break;
223 case LLCP_TLV_WKS:
224 local->remote_wks = llcp_tlv_wks(tlv);
225 break;
226 case LLCP_TLV_LTO:
227 local->remote_lto = llcp_tlv_lto(tlv) * 10;
228 break;
229 case LLCP_TLV_OPT:
230 local->remote_opt = llcp_tlv_opt(tlv);
231 break;
232 default:
233 pr_err("Invalid gt tlv value 0x%x\n", type);
234 break;
235 }
236
237 offset += length + 2;
238 tlv += length + 2;
239 }
240
241 pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x\n",
242 local->remote_version, local->remote_miu,
243 local->remote_lto, local->remote_opt,
244 local->remote_wks);
245
246 return 0;
247}
248
249int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock,
250 u8 *tlv_array, u16 tlv_array_len)
251{
252 u8 *tlv = tlv_array, type, length, offset = 0;
253
254 pr_debug("TLV array length %d\n", tlv_array_len);
255
256 if (sock == NULL)
257 return -ENOTCONN;
258
259 while (offset < tlv_array_len) {
260 type = tlv[0];
261 length = tlv[1];
262
263 pr_debug("type 0x%x length %d\n", type, length);
264
265 switch (type) {
266 case LLCP_TLV_MIUX:
267 sock->remote_miu = llcp_tlv_miux(tlv) + 128;
268 break;
269 case LLCP_TLV_RW:
270 sock->remote_rw = llcp_tlv_rw(tlv);
271 break;
272 case LLCP_TLV_SN:
273 break;
274 default:
275 pr_err("Invalid gt tlv value 0x%x\n", type);
276 break;
277 }
278
279 offset += length + 2;
280 tlv += length + 2;
281 }
282
283 pr_debug("sock %p rw %d miu %d\n", sock,
284 sock->remote_rw, sock->remote_miu);
285
286 return 0;
287}
288
289static struct sk_buff *llcp_add_header(struct sk_buff *pdu,
290 u8 dsap, u8 ssap, u8 ptype)
291{
292 u8 header[2];
293
294 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
295
296 header[0] = (u8)((dsap << 2) | (ptype >> 2));
297 header[1] = (u8)((ptype << 6) | ssap);
298
299 pr_debug("header 0x%x 0x%x\n", header[0], header[1]);
300
301 memcpy(skb_put(pdu, LLCP_HEADER_SIZE), header, LLCP_HEADER_SIZE);
302
303 return pdu;
304}
305
306static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv,
307 u8 tlv_length)
308{
309 /* XXX Add an skb length check */
310
311 if (tlv == NULL)
312 return NULL;
313
314 memcpy(skb_put(pdu, tlv_length), tlv, tlv_length);
315
316 return pdu;
317}
318
319static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock,
320 u8 cmd, u16 size)
321{
322 struct sk_buff *skb;
323 int err;
324
325 if (sock->ssap == 0)
326 return NULL;
327
328 skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
329 size + LLCP_HEADER_SIZE, &err);
330 if (skb == NULL) {
331 pr_err("Could not allocate PDU\n");
332 return NULL;
333 }
334
335 skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd);
336
337 return skb;
338}
339
340int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock)
341{
342 struct sk_buff *skb;
343 struct nfc_dev *dev;
344 struct nfc_llcp_local *local;
345
346 pr_debug("Sending DISC\n");
347
348 local = sock->local;
349 if (local == NULL)
350 return -ENODEV;
351
352 dev = sock->dev;
353 if (dev == NULL)
354 return -ENODEV;
355
356 skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
357 if (skb == NULL)
358 return -ENOMEM;
359
360 skb_queue_tail(&local->tx_queue, skb);
361
362 return 0;
363}
364
365int nfc_llcp_send_symm(struct nfc_dev *dev)
366{
367 struct sk_buff *skb;
368 struct nfc_llcp_local *local;
369 u16 size = 0;
370
371 pr_debug("Sending SYMM\n");
372
373 local = nfc_llcp_find_local(dev);
374 if (local == NULL)
375 return -ENODEV;
376
377 size += LLCP_HEADER_SIZE;
378 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
379
380 skb = alloc_skb(size, GFP_KERNEL);
381 if (skb == NULL)
382 return -ENOMEM;
383
384 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
385
386 skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM);
387
388 __net_timestamp(skb);
389
390 nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_TX);
391
392 return nfc_data_exchange(dev, local->target_idx, skb,
393 nfc_llcp_recv, local);
394}
395
396int nfc_llcp_send_connect(struct nfc_llcp_sock *sock)
397{
398 struct nfc_llcp_local *local;
399 struct sk_buff *skb;
400 u8 *service_name_tlv = NULL, service_name_tlv_length;
401 u8 *miux_tlv = NULL, miux_tlv_length;
402 u8 *rw_tlv = NULL, rw_tlv_length, rw;
403 int err;
404 u16 size = 0, miux;
405
406 pr_debug("Sending CONNECT\n");
407
408 local = sock->local;
409 if (local == NULL)
410 return -ENODEV;
411
412 if (sock->service_name != NULL) {
413 service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN,
414 sock->service_name,
415 sock->service_name_len,
416 &service_name_tlv_length);
417 size += service_name_tlv_length;
418 }
419
420 /* If the socket parameters are not set, use the local ones */
421 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
422 local->miux : sock->miux;
423 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
424
425 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
426 &miux_tlv_length);
427 size += miux_tlv_length;
428
429 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
430 size += rw_tlv_length;
431
432 pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len);
433
434 skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size);
435 if (skb == NULL) {
436 err = -ENOMEM;
437 goto error_tlv;
438 }
439
440 if (service_name_tlv != NULL)
441 skb = llcp_add_tlv(skb, service_name_tlv,
442 service_name_tlv_length);
443
444 skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
445 skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
446
447 skb_queue_tail(&local->tx_queue, skb);
448
449 return 0;
450
451error_tlv:
452 pr_err("error %d\n", err);
453
454 kfree(service_name_tlv);
455 kfree(miux_tlv);
456 kfree(rw_tlv);
457
458 return err;
459}
460
461int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
462{
463 struct nfc_llcp_local *local;
464 struct sk_buff *skb;
465 u8 *miux_tlv = NULL, miux_tlv_length;
466 u8 *rw_tlv = NULL, rw_tlv_length, rw;
467 int err;
468 u16 size = 0, miux;
469
470 pr_debug("Sending CC\n");
471
472 local = sock->local;
473 if (local == NULL)
474 return -ENODEV;
475
476 /* If the socket parameters are not set, use the local ones */
477 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
478 local->miux : sock->miux;
479 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
480
481 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
482 &miux_tlv_length);
483 size += miux_tlv_length;
484
485 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
486 size += rw_tlv_length;
487
488 skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size);
489 if (skb == NULL) {
490 err = -ENOMEM;
491 goto error_tlv;
492 }
493
494 skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
495 skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
496
497 skb_queue_tail(&local->tx_queue, skb);
498
499 return 0;
500
501error_tlv:
502 pr_err("error %d\n", err);
503
504 kfree(miux_tlv);
505 kfree(rw_tlv);
506
507 return err;
508}
509
510static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
511 size_t tlv_length)
512{
513 struct sk_buff *skb;
514 struct nfc_dev *dev;
515 u16 size = 0;
516
517 if (local == NULL)
518 return ERR_PTR(-ENODEV);
519
520 dev = local->dev;
521 if (dev == NULL)
522 return ERR_PTR(-ENODEV);
523
524 size += LLCP_HEADER_SIZE;
525 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
526 size += tlv_length;
527
528 skb = alloc_skb(size, GFP_KERNEL);
529 if (skb == NULL)
530 return ERR_PTR(-ENOMEM);
531
532 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
533
534 skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL);
535
536 return skb;
537}
538
539int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local,
540 struct hlist_head *tlv_list, size_t tlvs_len)
541{
542 struct nfc_llcp_sdp_tlv *sdp;
543 struct hlist_node *n;
544 struct sk_buff *skb;
545
546 skb = nfc_llcp_allocate_snl(local, tlvs_len);
547 if (IS_ERR(skb))
548 return PTR_ERR(skb);
549
550 hlist_for_each_entry_safe(sdp, n, tlv_list, node) {
551 memcpy(skb_put(skb, sdp->tlv_len), sdp->tlv, sdp->tlv_len);
552
553 hlist_del(&sdp->node);
554
555 nfc_llcp_free_sdp_tlv(sdp);
556 }
557
558 skb_queue_tail(&local->tx_queue, skb);
559
560 return 0;
561}
562
563int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local,
564 struct hlist_head *tlv_list, size_t tlvs_len)
565{
566 struct nfc_llcp_sdp_tlv *sdreq;
567 struct hlist_node *n;
568 struct sk_buff *skb;
569
570 skb = nfc_llcp_allocate_snl(local, tlvs_len);
571 if (IS_ERR(skb))
572 return PTR_ERR(skb);
573
574 mutex_lock(&local->sdreq_lock);
575
576 if (hlist_empty(&local->pending_sdreqs))
577 mod_timer(&local->sdreq_timer,
578 jiffies + msecs_to_jiffies(3 * local->remote_lto));
579
580 hlist_for_each_entry_safe(sdreq, n, tlv_list, node) {
581 pr_debug("tid %d for %s\n", sdreq->tid, sdreq->uri);
582
583 memcpy(skb_put(skb, sdreq->tlv_len), sdreq->tlv,
584 sdreq->tlv_len);
585
586 hlist_del(&sdreq->node);
587
588 hlist_add_head(&sdreq->node, &local->pending_sdreqs);
589 }
590
591 mutex_unlock(&local->sdreq_lock);
592
593 skb_queue_tail(&local->tx_queue, skb);
594
595 return 0;
596}
597
598int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason)
599{
600 struct sk_buff *skb;
601 struct nfc_dev *dev;
602 u16 size = 1; /* Reason code */
603
604 pr_debug("Sending DM reason 0x%x\n", reason);
605
606 if (local == NULL)
607 return -ENODEV;
608
609 dev = local->dev;
610 if (dev == NULL)
611 return -ENODEV;
612
613 size += LLCP_HEADER_SIZE;
614 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
615
616 skb = alloc_skb(size, GFP_KERNEL);
617 if (skb == NULL)
618 return -ENOMEM;
619
620 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
621
622 skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM);
623
624 memcpy(skb_put(skb, 1), &reason, 1);
625
626 skb_queue_head(&local->tx_queue, skb);
627
628 return 0;
629}
630
631int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
632 struct msghdr *msg, size_t len)
633{
634 struct sk_buff *pdu;
635 struct sock *sk = &sock->sk;
636 struct nfc_llcp_local *local;
637 size_t frag_len = 0, remaining_len;
638 u8 *msg_data, *msg_ptr;
639 u16 remote_miu;
640
641 pr_debug("Send I frame len %zd\n", len);
642
643 local = sock->local;
644 if (local == NULL)
645 return -ENODEV;
646
647 /* Remote is ready but has not acknowledged our frames */
648 if((sock->remote_ready &&
649 skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw &&
650 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
651 pr_err("Pending queue is full %d frames\n",
652 skb_queue_len(&sock->tx_pending_queue));
653 return -ENOBUFS;
654 }
655
656 /* Remote is not ready and we've been queueing enough frames */
657 if ((!sock->remote_ready &&
658 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
659 pr_err("Tx queue is full %d frames\n",
660 skb_queue_len(&sock->tx_queue));
661 return -ENOBUFS;
662 }
663
664 msg_data = kzalloc(len, GFP_KERNEL);
665 if (msg_data == NULL)
666 return -ENOMEM;
667
668 if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) {
669 kfree(msg_data);
670 return -EFAULT;
671 }
672
673 remaining_len = len;
674 msg_ptr = msg_data;
675
676 do {
677 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
678 LLCP_DEFAULT_MIU : sock->remote_miu;
679
680 frag_len = min_t(size_t, remote_miu, remaining_len);
681
682 pr_debug("Fragment %zd bytes remaining %zd",
683 frag_len, remaining_len);
684
685 pdu = llcp_allocate_pdu(sock, LLCP_PDU_I,
686 frag_len + LLCP_SEQUENCE_SIZE);
687 if (pdu == NULL) {
688 kfree(msg_data);
689 return -ENOMEM;
690 }
691
692 skb_put(pdu, LLCP_SEQUENCE_SIZE);
693
694 if (likely(frag_len > 0))
695 memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len);
696
697 skb_queue_tail(&sock->tx_queue, pdu);
698
699 lock_sock(sk);
700
701 nfc_llcp_queue_i_frames(sock);
702
703 release_sock(sk);
704
705 remaining_len -= frag_len;
706 msg_ptr += frag_len;
707 } while (remaining_len > 0);
708
709 kfree(msg_data);
710
711 return len;
712}
713
714int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap,
715 struct msghdr *msg, size_t len)
716{
717 struct sk_buff *pdu;
718 struct nfc_llcp_local *local;
719 size_t frag_len = 0, remaining_len;
720 u8 *msg_ptr, *msg_data;
721 u16 remote_miu;
722 int err;
723
724 pr_debug("Send UI frame len %zd\n", len);
725
726 local = sock->local;
727 if (local == NULL)
728 return -ENODEV;
729
730 msg_data = kzalloc(len, GFP_KERNEL);
731 if (msg_data == NULL)
732 return -ENOMEM;
733
734 if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) {
735 kfree(msg_data);
736 return -EFAULT;
737 }
738
739 remaining_len = len;
740 msg_ptr = msg_data;
741
742 do {
743 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
744 local->remote_miu : sock->remote_miu;
745
746 frag_len = min_t(size_t, remote_miu, remaining_len);
747
748 pr_debug("Fragment %zd bytes remaining %zd",
749 frag_len, remaining_len);
750
751 pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
752 frag_len + LLCP_HEADER_SIZE, &err);
753 if (pdu == NULL) {
754 pr_err("Could not allocate PDU\n");
755 continue;
756 }
757
758 pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI);
759
760 if (likely(frag_len > 0))
761 memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len);
762
763 /* No need to check for the peer RW for UI frames */
764 skb_queue_tail(&local->tx_queue, pdu);
765
766 remaining_len -= frag_len;
767 msg_ptr += frag_len;
768 } while (remaining_len > 0);
769
770 kfree(msg_data);
771
772 return len;
773}
774
775int nfc_llcp_send_rr(struct nfc_llcp_sock *sock)
776{
777 struct sk_buff *skb;
778 struct nfc_llcp_local *local;
779
780 pr_debug("Send rr nr %d\n", sock->recv_n);
781
782 local = sock->local;
783 if (local == NULL)
784 return -ENODEV;
785
786 skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE);
787 if (skb == NULL)
788 return -ENOMEM;
789
790 skb_put(skb, LLCP_SEQUENCE_SIZE);
791
792 skb->data[2] = sock->recv_n;
793
794 skb_queue_head(&local->tx_queue, skb);
795
796 return 0;
797}