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
18 static const 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
llcp_tlv8(const u8 * tlv,u8 type)32 static u8 llcp_tlv8(const 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
llcp_tlv16(const u8 * tlv,u8 type)40 static u16 llcp_tlv16(const 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
llcp_tlv_version(const u8 * tlv)49 static u8 llcp_tlv_version(const u8 *tlv)
50 {
51 return llcp_tlv8(tlv, LLCP_TLV_VERSION);
52 }
53
llcp_tlv_miux(const u8 * tlv)54 static u16 llcp_tlv_miux(const u8 *tlv)
55 {
56 return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff;
57 }
58
llcp_tlv_wks(const u8 * tlv)59 static u16 llcp_tlv_wks(const u8 *tlv)
60 {
61 return llcp_tlv16(tlv, LLCP_TLV_WKS);
62 }
63
llcp_tlv_lto(const u8 * tlv)64 static u16 llcp_tlv_lto(const u8 *tlv)
65 {
66 return llcp_tlv8(tlv, LLCP_TLV_LTO);
67 }
68
llcp_tlv_opt(const u8 * tlv)69 static u8 llcp_tlv_opt(const u8 *tlv)
70 {
71 return llcp_tlv8(tlv, LLCP_TLV_OPT);
72 }
73
llcp_tlv_rw(const u8 * tlv)74 static u8 llcp_tlv_rw(const u8 *tlv)
75 {
76 return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf;
77 }
78
nfc_llcp_build_tlv(u8 type,const u8 * value,u8 value_length,u8 * tlv_length)79 u8 *nfc_llcp_build_tlv(u8 type, const 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
nfc_llcp_build_sdres_tlv(u8 tid,u8 sap)106 struct 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
nfc_llcp_build_sdreq_tlv(u8 tid,const char * uri,size_t uri_len)133 struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdreq_tlv(u8 tid, const 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
nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv * sdp)174 void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp)
175 {
176 kfree(sdp->tlv);
177 kfree(sdp);
178 }
179
nfc_llcp_free_sdp_tlv_list(struct hlist_head * head)180 void 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
nfc_llcp_parse_gb_tlv(struct nfc_llcp_local * local,const u8 * tlv_array,u16 tlv_array_len)192 int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
193 const u8 *tlv_array, u16 tlv_array_len)
194 {
195 const u8 *tlv = tlv_array;
196 u8 type, length, offset = 0;
197
198 pr_debug("TLV array length %d\n", tlv_array_len);
199
200 if (local == NULL)
201 return -ENODEV;
202
203 while (offset < tlv_array_len) {
204 type = tlv[0];
205 length = tlv[1];
206
207 pr_debug("type 0x%x length %d\n", type, length);
208
209 switch (type) {
210 case LLCP_TLV_VERSION:
211 local->remote_version = llcp_tlv_version(tlv);
212 break;
213 case LLCP_TLV_MIUX:
214 local->remote_miu = llcp_tlv_miux(tlv) + 128;
215 break;
216 case LLCP_TLV_WKS:
217 local->remote_wks = llcp_tlv_wks(tlv);
218 break;
219 case LLCP_TLV_LTO:
220 local->remote_lto = llcp_tlv_lto(tlv) * 10;
221 break;
222 case LLCP_TLV_OPT:
223 local->remote_opt = llcp_tlv_opt(tlv);
224 break;
225 default:
226 pr_err("Invalid gt tlv value 0x%x\n", type);
227 break;
228 }
229
230 offset += length + 2;
231 tlv += length + 2;
232 }
233
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,
237 local->remote_wks);
238
239 return 0;
240 }
241
nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock * sock,const u8 * tlv_array,u16 tlv_array_len)242 int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock,
243 const u8 *tlv_array, u16 tlv_array_len)
244 {
245 const u8 *tlv = tlv_array;
246 u8 type, length, offset = 0;
247
248 pr_debug("TLV array length %d\n", tlv_array_len);
249
250 if (sock == NULL)
251 return -ENOTCONN;
252
253 while (offset < tlv_array_len) {
254 type = tlv[0];
255 length = tlv[1];
256
257 pr_debug("type 0x%x length %d\n", type, length);
258
259 switch (type) {
260 case LLCP_TLV_MIUX:
261 sock->remote_miu = llcp_tlv_miux(tlv) + 128;
262 break;
263 case LLCP_TLV_RW:
264 sock->remote_rw = llcp_tlv_rw(tlv);
265 break;
266 case LLCP_TLV_SN:
267 break;
268 default:
269 pr_err("Invalid gt tlv value 0x%x\n", type);
270 break;
271 }
272
273 offset += length + 2;
274 tlv += length + 2;
275 }
276
277 pr_debug("sock %p rw %d miu %d\n", sock,
278 sock->remote_rw, sock->remote_miu);
279
280 return 0;
281 }
282
llcp_add_header(struct sk_buff * pdu,u8 dsap,u8 ssap,u8 ptype)283 static struct sk_buff *llcp_add_header(struct sk_buff *pdu,
284 u8 dsap, u8 ssap, u8 ptype)
285 {
286 u8 header[2];
287
288 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
289
290 header[0] = (u8)((dsap << 2) | (ptype >> 2));
291 header[1] = (u8)((ptype << 6) | ssap);
292
293 pr_debug("header 0x%x 0x%x\n", header[0], header[1]);
294
295 skb_put_data(pdu, header, LLCP_HEADER_SIZE);
296
297 return pdu;
298 }
299
llcp_add_tlv(struct sk_buff * pdu,const u8 * tlv,u8 tlv_length)300 static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, const u8 *tlv,
301 u8 tlv_length)
302 {
303 /* XXX Add an skb length check */
304
305 if (tlv == NULL)
306 return NULL;
307
308 skb_put_data(pdu, tlv, tlv_length);
309
310 return pdu;
311 }
312
llcp_allocate_pdu(struct nfc_llcp_sock * sock,u8 cmd,u16 size)313 static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock,
314 u8 cmd, u16 size)
315 {
316 struct sk_buff *skb;
317 int err;
318
319 if (sock->ssap == 0)
320 return NULL;
321
322 skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
323 size + LLCP_HEADER_SIZE, &err);
324 if (skb == NULL) {
325 pr_err("Could not allocate PDU\n");
326 return NULL;
327 }
328
329 skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd);
330
331 return skb;
332 }
333
nfc_llcp_send_disconnect(struct nfc_llcp_sock * sock)334 int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock)
335 {
336 struct sk_buff *skb;
337 struct nfc_dev *dev;
338 struct nfc_llcp_local *local;
339
340 pr_debug("Sending DISC\n");
341
342 local = sock->local;
343 if (local == NULL)
344 return -ENODEV;
345
346 dev = sock->dev;
347 if (dev == NULL)
348 return -ENODEV;
349
350 skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
351 if (skb == NULL)
352 return -ENOMEM;
353
354 skb_queue_tail(&local->tx_queue, skb);
355
356 return 0;
357 }
358
nfc_llcp_send_symm(struct nfc_dev * dev)359 int nfc_llcp_send_symm(struct nfc_dev *dev)
360 {
361 struct sk_buff *skb;
362 struct nfc_llcp_local *local;
363 u16 size = 0;
364 int err;
365
366 pr_debug("Sending SYMM\n");
367
368 local = nfc_llcp_find_local(dev);
369 if (local == NULL)
370 return -ENODEV;
371
372 size += LLCP_HEADER_SIZE;
373 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
374
375 skb = alloc_skb(size, GFP_KERNEL);
376 if (skb == NULL) {
377 err = -ENOMEM;
378 goto out;
379 }
380
381 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
382
383 skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM);
384
385 __net_timestamp(skb);
386
387 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX);
388
389 err = nfc_data_exchange(dev, local->target_idx, skb,
390 nfc_llcp_recv, local);
391 out:
392 nfc_llcp_local_put(local);
393 return err;
394 }
395
nfc_llcp_send_connect(struct nfc_llcp_sock * sock)396 int nfc_llcp_send_connect(struct nfc_llcp_sock *sock)
397 {
398 struct nfc_llcp_local *local;
399 struct sk_buff *skb;
400 const u8 *service_name_tlv = NULL;
401 const u8 *miux_tlv = NULL;
402 const u8 *rw_tlv = NULL;
403 u8 service_name_tlv_length = 0;
404 u8 miux_tlv_length, rw_tlv_length, rw;
405 int err;
406 u16 size = 0;
407 __be16 miux;
408
409 pr_debug("Sending CONNECT\n");
410
411 local = sock->local;
412 if (local == NULL)
413 return -ENODEV;
414
415 if (sock->service_name != NULL) {
416 service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN,
417 sock->service_name,
418 sock->service_name_len,
419 &service_name_tlv_length);
420 if (!service_name_tlv) {
421 err = -ENOMEM;
422 goto error_tlv;
423 }
424 size += service_name_tlv_length;
425 }
426
427 /* If the socket parameters are not set, use the local ones */
428 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
429 local->miux : sock->miux;
430 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
431
432 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
433 &miux_tlv_length);
434 if (!miux_tlv) {
435 err = -ENOMEM;
436 goto error_tlv;
437 }
438 size += miux_tlv_length;
439
440 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
441 if (!rw_tlv) {
442 err = -ENOMEM;
443 goto error_tlv;
444 }
445 size += rw_tlv_length;
446
447 pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len);
448
449 skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size);
450 if (skb == NULL) {
451 err = -ENOMEM;
452 goto error_tlv;
453 }
454
455 llcp_add_tlv(skb, service_name_tlv, service_name_tlv_length);
456 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
457 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
458
459 skb_queue_tail(&local->tx_queue, skb);
460
461 err = 0;
462
463 error_tlv:
464 if (err)
465 pr_err("error %d\n", err);
466
467 kfree(service_name_tlv);
468 kfree(miux_tlv);
469 kfree(rw_tlv);
470
471 return err;
472 }
473
nfc_llcp_send_cc(struct nfc_llcp_sock * sock)474 int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
475 {
476 struct nfc_llcp_local *local;
477 struct sk_buff *skb;
478 const u8 *miux_tlv = NULL;
479 const u8 *rw_tlv = NULL;
480 u8 miux_tlv_length, rw_tlv_length, rw;
481 int err;
482 u16 size = 0;
483 __be16 miux;
484
485 pr_debug("Sending CC\n");
486
487 local = sock->local;
488 if (local == NULL)
489 return -ENODEV;
490
491 /* If the socket parameters are not set, use the local ones */
492 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
493 local->miux : sock->miux;
494 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
495
496 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
497 &miux_tlv_length);
498 if (!miux_tlv) {
499 err = -ENOMEM;
500 goto error_tlv;
501 }
502 size += miux_tlv_length;
503
504 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
505 if (!rw_tlv) {
506 err = -ENOMEM;
507 goto error_tlv;
508 }
509 size += rw_tlv_length;
510
511 skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size);
512 if (skb == NULL) {
513 err = -ENOMEM;
514 goto error_tlv;
515 }
516
517 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
518 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
519
520 skb_queue_tail(&local->tx_queue, skb);
521
522 err = 0;
523
524 error_tlv:
525 if (err)
526 pr_err("error %d\n", err);
527
528 kfree(miux_tlv);
529 kfree(rw_tlv);
530
531 return err;
532 }
533
nfc_llcp_allocate_snl(struct nfc_llcp_local * local,size_t tlv_length)534 static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
535 size_t tlv_length)
536 {
537 struct sk_buff *skb;
538 struct nfc_dev *dev;
539 u16 size = 0;
540
541 if (local == NULL)
542 return ERR_PTR(-ENODEV);
543
544 dev = local->dev;
545 if (dev == NULL)
546 return ERR_PTR(-ENODEV);
547
548 size += LLCP_HEADER_SIZE;
549 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
550 size += tlv_length;
551
552 skb = alloc_skb(size, GFP_KERNEL);
553 if (skb == NULL)
554 return ERR_PTR(-ENOMEM);
555
556 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
557
558 skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL);
559
560 return skb;
561 }
562
nfc_llcp_send_snl_sdres(struct nfc_llcp_local * local,struct hlist_head * tlv_list,size_t tlvs_len)563 int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local,
564 struct hlist_head *tlv_list, size_t tlvs_len)
565 {
566 struct nfc_llcp_sdp_tlv *sdp;
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 hlist_for_each_entry_safe(sdp, n, tlv_list, node) {
575 skb_put_data(skb, sdp->tlv, sdp->tlv_len);
576
577 hlist_del(&sdp->node);
578
579 nfc_llcp_free_sdp_tlv(sdp);
580 }
581
582 skb_queue_tail(&local->tx_queue, skb);
583
584 return 0;
585 }
586
nfc_llcp_send_snl_sdreq(struct nfc_llcp_local * local,struct hlist_head * tlv_list,size_t tlvs_len)587 int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local,
588 struct hlist_head *tlv_list, size_t tlvs_len)
589 {
590 struct nfc_llcp_sdp_tlv *sdreq;
591 struct hlist_node *n;
592 struct sk_buff *skb;
593
594 skb = nfc_llcp_allocate_snl(local, tlvs_len);
595 if (IS_ERR(skb))
596 return PTR_ERR(skb);
597
598 mutex_lock(&local->sdreq_lock);
599
600 if (hlist_empty(&local->pending_sdreqs))
601 mod_timer(&local->sdreq_timer,
602 jiffies + msecs_to_jiffies(3 * local->remote_lto));
603
604 hlist_for_each_entry_safe(sdreq, n, tlv_list, node) {
605 pr_debug("tid %d for %s\n", sdreq->tid, sdreq->uri);
606
607 skb_put_data(skb, sdreq->tlv, sdreq->tlv_len);
608
609 hlist_del(&sdreq->node);
610
611 hlist_add_head(&sdreq->node, &local->pending_sdreqs);
612 }
613
614 mutex_unlock(&local->sdreq_lock);
615
616 skb_queue_tail(&local->tx_queue, skb);
617
618 return 0;
619 }
620
nfc_llcp_send_dm(struct nfc_llcp_local * local,u8 ssap,u8 dsap,u8 reason)621 int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason)
622 {
623 struct sk_buff *skb;
624 struct nfc_dev *dev;
625 u16 size = 1; /* Reason code */
626
627 pr_debug("Sending DM reason 0x%x\n", reason);
628
629 if (local == NULL)
630 return -ENODEV;
631
632 dev = local->dev;
633 if (dev == NULL)
634 return -ENODEV;
635
636 size += LLCP_HEADER_SIZE;
637 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
638
639 skb = alloc_skb(size, GFP_KERNEL);
640 if (skb == NULL)
641 return -ENOMEM;
642
643 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
644
645 skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM);
646
647 skb_put_data(skb, &reason, 1);
648
649 skb_queue_head(&local->tx_queue, skb);
650
651 return 0;
652 }
653
nfc_llcp_send_i_frame(struct nfc_llcp_sock * sock,struct msghdr * msg,size_t len)654 int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
655 struct msghdr *msg, size_t len)
656 {
657 struct sk_buff *pdu;
658 struct sock *sk = &sock->sk;
659 struct nfc_llcp_local *local;
660 size_t frag_len = 0, remaining_len;
661 u8 *msg_data, *msg_ptr;
662 u16 remote_miu;
663
664 pr_debug("Send I frame len %zd\n", len);
665
666 local = sock->local;
667 if (local == NULL)
668 return -ENODEV;
669
670 /* Remote is ready but has not acknowledged our frames */
671 if((sock->remote_ready &&
672 skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw &&
673 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
674 pr_err("Pending queue is full %d frames\n",
675 skb_queue_len(&sock->tx_pending_queue));
676 return -ENOBUFS;
677 }
678
679 /* Remote is not ready and we've been queueing enough frames */
680 if ((!sock->remote_ready &&
681 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
682 pr_err("Tx queue is full %d frames\n",
683 skb_queue_len(&sock->tx_queue));
684 return -ENOBUFS;
685 }
686
687 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
688 if (msg_data == NULL)
689 return -ENOMEM;
690
691 if (memcpy_from_msg(msg_data, msg, len)) {
692 kfree(msg_data);
693 return -EFAULT;
694 }
695
696 remaining_len = len;
697 msg_ptr = msg_data;
698
699 do {
700 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
701 LLCP_DEFAULT_MIU : sock->remote_miu;
702
703 frag_len = min_t(size_t, remote_miu, remaining_len);
704
705 pr_debug("Fragment %zd bytes remaining %zd",
706 frag_len, remaining_len);
707
708 pdu = llcp_allocate_pdu(sock, LLCP_PDU_I,
709 frag_len + LLCP_SEQUENCE_SIZE);
710 if (pdu == NULL) {
711 kfree(msg_data);
712 return -ENOMEM;
713 }
714
715 skb_put(pdu, LLCP_SEQUENCE_SIZE);
716
717 if (likely(frag_len > 0))
718 skb_put_data(pdu, msg_ptr, frag_len);
719
720 skb_queue_tail(&sock->tx_queue, pdu);
721
722 lock_sock(sk);
723
724 nfc_llcp_queue_i_frames(sock);
725
726 release_sock(sk);
727
728 remaining_len -= frag_len;
729 msg_ptr += frag_len;
730 } while (remaining_len > 0);
731
732 kfree(msg_data);
733
734 return len;
735 }
736
nfc_llcp_send_ui_frame(struct nfc_llcp_sock * sock,u8 ssap,u8 dsap,struct msghdr * msg,size_t len)737 int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap,
738 struct msghdr *msg, size_t len)
739 {
740 struct sk_buff *pdu;
741 struct nfc_llcp_local *local;
742 size_t frag_len = 0, remaining_len;
743 u8 *msg_ptr, *msg_data;
744 u16 remote_miu;
745 int err;
746
747 pr_debug("Send UI frame len %zd\n", len);
748
749 local = sock->local;
750 if (local == NULL)
751 return -ENODEV;
752
753 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
754 if (msg_data == NULL)
755 return -ENOMEM;
756
757 if (memcpy_from_msg(msg_data, msg, len)) {
758 kfree(msg_data);
759 return -EFAULT;
760 }
761
762 remaining_len = len;
763 msg_ptr = msg_data;
764
765 do {
766 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
767 local->remote_miu : sock->remote_miu;
768
769 frag_len = min_t(size_t, remote_miu, remaining_len);
770
771 pr_debug("Fragment %zd bytes remaining %zd",
772 frag_len, remaining_len);
773
774 pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, 0,
775 frag_len + LLCP_HEADER_SIZE, &err);
776 if (pdu == NULL) {
777 pr_err("Could not allocate PDU (error=%d)\n", err);
778 len -= remaining_len;
779 if (len == 0)
780 len = err;
781 break;
782 }
783
784 pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI);
785
786 if (likely(frag_len > 0))
787 skb_put_data(pdu, msg_ptr, frag_len);
788
789 /* No need to check for the peer RW for UI frames */
790 skb_queue_tail(&local->tx_queue, pdu);
791
792 remaining_len -= frag_len;
793 msg_ptr += frag_len;
794 } while (remaining_len > 0);
795
796 kfree(msg_data);
797
798 return len;
799 }
800
nfc_llcp_send_rr(struct nfc_llcp_sock * sock)801 int nfc_llcp_send_rr(struct nfc_llcp_sock *sock)
802 {
803 struct sk_buff *skb;
804 struct nfc_llcp_local *local;
805
806 pr_debug("Send rr nr %d\n", sock->recv_n);
807
808 local = sock->local;
809 if (local == NULL)
810 return -ENODEV;
811
812 skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE);
813 if (skb == NULL)
814 return -ENOMEM;
815
816 skb_put(skb, LLCP_SEQUENCE_SIZE);
817
818 skb->data[2] = sock->recv_n;
819
820 skb_queue_head(&local->tx_queue, skb);
821
822 return 0;
823 }
824