blob: 475061c79c442b3ba88d2f218b9ff410cf270b89 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Samuel Ortizd6469602011-12-14 16:43:12 +01002/*
3 * Copyright (C) 2011 Intel Corporation. All rights reserved.
Samuel Ortizd6469602011-12-14 16:43:12 +01004 */
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
Samuel Ortiz30cc4582013-04-26 11:49:40 +020015#include "nfc.h"
Samuel Ortizd6469602011-12-14 16:43:12 +010016#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
Samuel Ortiz76762b72012-05-14 17:38:54 +020040static u16 llcp_tlv16(u8 *tlv, u8 type)
Samuel Ortizd6469602011-12-14 16:43:12 +010041{
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{
Samuel Ortiz76762b72012-05-14 17:38:54 +020056 return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff;
Samuel Ortizd6469602011-12-14 16:43:12 +010057}
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;
Samuel Ortiz324b0af2012-04-10 19:43:15 +020091 else if (length == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +010092 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
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100106struct 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
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100133struct 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
Kees Cookfe9c8422018-02-14 15:45:07 -0800140 /* 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
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100144 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
Thierry Escande40213fa2013-03-04 15:43:32 +0100167 sdreq->time = jiffies;
168
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100169 INIT_HLIST_NODE(&sdreq->node);
170
171 return sdreq;
172}
173
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100174void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp)
175{
176 kfree(sdp->tlv);
177 kfree(sdp);
178}
179
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100180void 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
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200192int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
193 u8 *tlv_array, u16 tlv_array_len)
Samuel Ortizd6469602011-12-14 16:43:12 +0100194{
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;
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200224 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) {
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200258 case LLCP_TLV_MIUX:
Samuel Ortize4306be2013-02-22 01:12:28 +0100259 sock->remote_miu = llcp_tlv_miux(tlv) + 128;
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200260 break;
Samuel Ortizd6469602011-12-14 16:43:12 +0100261 case LLCP_TLV_RW:
Samuel Ortize4306be2013-02-22 01:12:28 +0100262 sock->remote_rw = llcp_tlv_rw(tlv);
Samuel Ortizd6469602011-12-14 16:43:12 +0100263 break;
Samuel Ortiz9dda50f2012-03-05 01:03:49 +0100264 case LLCP_TLV_SN:
265 break;
Samuel Ortizd6469602011-12-14 16:43:12 +0100266 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
Samuel Ortize4306be2013-02-22 01:12:28 +0100275 pr_debug("sock %p rw %d miu %d\n", sock,
276 sock->remote_rw, sock->remote_miu);
Samuel Ortizd6469602011-12-14 16:43:12 +0100277
278 return 0;
279}
280
281static struct sk_buff *llcp_add_header(struct sk_buff *pdu,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100282 u8 dsap, u8 ssap, u8 ptype)
Samuel Ortizd6469602011-12-14 16:43:12 +0100283{
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
Johannes Berg59ae1d12017-06-16 14:29:20 +0200293 skb_put_data(pdu, header, LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +0100294
295 return pdu;
296}
297
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100298static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv,
299 u8 tlv_length)
Samuel Ortizd6469602011-12-14 16:43:12 +0100300{
301 /* XXX Add an skb length check */
302
303 if (tlv == NULL)
304 return NULL;
305
Johannes Berg59ae1d12017-06-16 14:29:20 +0200306 skb_put_data(pdu, tlv, tlv_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100307
308 return pdu;
309}
310
311static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100312 u8 cmd, u16 size)
Samuel Ortizd6469602011-12-14 16:43:12 +0100313{
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,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100321 size + LLCP_HEADER_SIZE, &err);
Samuel Ortizd6469602011-12-14 16:43:12 +0100322 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
Thierry Escande58e3dd12013-06-04 11:34:50 +0200332int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100333{
334 struct sk_buff *skb;
335 struct nfc_dev *dev;
336 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +0100337
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
Samuel Ortiz92223902012-10-05 01:09:07 +0200348 skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
Samuel Ortizd6469602011-12-14 16:43:12 +0100349 if (skb == NULL)
350 return -ENOMEM;
351
Samuel Ortizd6469602011-12-14 16:43:12 +0100352 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
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100380 __net_timestamp(skb);
381
Hiren Tandel57be1f32014-05-05 19:43:31 +0900382 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX);
Thierry Escande44635232012-09-26 18:16:44 +0200383
Samuel Ortizd6469602011-12-14 16:43:12 +0100384 return nfc_data_exchange(dev, local->target_idx, skb,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100385 nfc_llcp_recv, local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100386}
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;
Samuel Ortizeda21f12012-03-05 01:03:43 +0100393 u8 *miux_tlv = NULL, miux_tlv_length;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100394 u8 *rw_tlv = NULL, rw_tlv_length, rw;
Samuel Ortizd6469602011-12-14 16:43:12 +0100395 int err;
Christophe Ricarde5b53c02014-12-02 21:27:57 +0100396 u16 size = 0;
397 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100398
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,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100407 sock->service_name,
408 sock->service_name_len,
409 &service_name_tlv_length);
YueHaibing58bdd542019-02-22 15:37:58 +0800410 if (!service_name_tlv) {
411 err = -ENOMEM;
412 goto error_tlv;
413 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100414 size += service_name_tlv_length;
415 }
416
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100417 /* If the socket parameters are not set, use the local ones */
Samuel Ortiz5eef6662013-03-20 16:06:12 +0100418 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
419 local->miux : sock->miux;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100420 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
421
422 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100423 &miux_tlv_length);
YueHaibing58bdd542019-02-22 15:37:58 +0800424 if (!miux_tlv) {
425 err = -ENOMEM;
426 goto error_tlv;
427 }
Samuel Ortizeda21f12012-03-05 01:03:43 +0100428 size += miux_tlv_length;
429
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100430 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
YueHaibing58bdd542019-02-22 15:37:58 +0800431 if (!rw_tlv) {
432 err = -ENOMEM;
433 goto error_tlv;
434 }
Samuel Ortizeda21f12012-03-05 01:03:43 +0100435 size += rw_tlv_length;
436
Samuel Ortizd6469602011-12-14 16:43:12 +0100437 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
Thierry Escandede9e5ae2016-06-29 10:48:22 +0200445 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);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100448
Samuel Ortizd6469602011-12-14 16:43:12 +0100449 skb_queue_tail(&local->tx_queue, skb);
450
Thierry Escande256f3ee2016-06-29 10:48:23 +0200451 err = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100452
453error_tlv:
Thierry Escande256f3ee2016-06-29 10:48:23 +0200454 if (err)
455 pr_err("error %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +0100456
457 kfree(service_name_tlv);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100458 kfree(miux_tlv);
459 kfree(rw_tlv);
Samuel Ortizd6469602011-12-14 16:43:12 +0100460
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;
Samuel Ortizeda21f12012-03-05 01:03:43 +0100468 u8 *miux_tlv = NULL, miux_tlv_length;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100469 u8 *rw_tlv = NULL, rw_tlv_length, rw;
Samuel Ortizeda21f12012-03-05 01:03:43 +0100470 int err;
Christophe Ricarde5b53c02014-12-02 21:27:57 +0100471 u16 size = 0;
472 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100473
474 pr_debug("Sending CC\n");
475
476 local = sock->local;
477 if (local == NULL)
478 return -ENODEV;
479
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100480 /* If the socket parameters are not set, use the local ones */
Samuel Ortiz5eef6662013-03-20 16:06:12 +0100481 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
482 local->miux : sock->miux;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100483 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
484
485 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100486 &miux_tlv_length);
YueHaibing58bdd542019-02-22 15:37:58 +0800487 if (!miux_tlv) {
488 err = -ENOMEM;
489 goto error_tlv;
490 }
Samuel Ortizeda21f12012-03-05 01:03:43 +0100491 size += miux_tlv_length;
492
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100493 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
YueHaibing58bdd542019-02-22 15:37:58 +0800494 if (!rw_tlv) {
495 err = -ENOMEM;
496 goto error_tlv;
497 }
Samuel Ortizeda21f12012-03-05 01:03:43 +0100498 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
Thierry Escandede9e5ae2016-06-29 10:48:22 +0200506 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
507 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100508
509 skb_queue_tail(&local->tx_queue, skb);
510
Thierry Escande256f3ee2016-06-29 10:48:23 +0200511 err = 0;
Samuel Ortizeda21f12012-03-05 01:03:43 +0100512
513error_tlv:
Thierry Escande256f3ee2016-06-29 10:48:23 +0200514 if (err)
515 pr_err("error %d\n", err);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100516
517 kfree(miux_tlv);
518 kfree(rw_tlv);
519
520 return err;
Samuel Ortizd6469602011-12-14 16:43:12 +0100521}
522
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100523static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
524 size_t tlv_length)
Samuel Ortizc43bb032012-10-05 01:13:24 +0200525{
526 struct sk_buff *skb;
527 struct nfc_dev *dev;
Samuel Ortizc43bb032012-10-05 01:13:24 +0200528 u16 size = 0;
529
Samuel Ortizc43bb032012-10-05 01:13:24 +0200530 if (local == NULL)
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100531 return ERR_PTR(-ENODEV);
Samuel Ortizc43bb032012-10-05 01:13:24 +0200532
533 dev = local->dev;
534 if (dev == NULL)
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100535 return ERR_PTR(-ENODEV);
Samuel Ortizc43bb032012-10-05 01:13:24 +0200536
537 size += LLCP_HEADER_SIZE;
538 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100539 size += tlv_length;
Samuel Ortizc43bb032012-10-05 01:13:24 +0200540
541 skb = alloc_skb(size, GFP_KERNEL);
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100542 if (skb == NULL)
543 return ERR_PTR(-ENOMEM);
Samuel Ortizc43bb032012-10-05 01:13:24 +0200544
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
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100549 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) {
Johannes Berg59ae1d12017-06-16 14:29:20 +0200564 skb_put_data(skb, sdp->tlv, sdp->tlv_len);
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100565
566 hlist_del(&sdp->node);
567
568 nfc_llcp_free_sdp_tlv(sdp);
569 }
Samuel Ortizc43bb032012-10-05 01:13:24 +0200570
571 skb_queue_tail(&local->tx_queue, skb);
572
Samuel Ortizc43bb032012-10-05 01:13:24 +0200573 return 0;
574}
575
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100576int 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
Thierry Escande40213fa2013-03-04 15:43:32 +0100589 if (hlist_empty(&local->pending_sdreqs))
590 mod_timer(&local->sdreq_timer,
591 jiffies + msecs_to_jiffies(3 * local->remote_lto));
592
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100593 hlist_for_each_entry_safe(sdreq, n, tlv_list, node) {
594 pr_debug("tid %d for %s\n", sdreq->tid, sdreq->uri);
595
Johannes Berg59ae1d12017-06-16 14:29:20 +0200596 skb_put_data(skb, sdreq->tlv, sdreq->tlv_len);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100597
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
Samuel Ortizd6469602011-12-14 16:43:12 +0100610int 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
Samuel Ortizffc29312012-04-10 19:43:16 +0200634 skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM);
Samuel Ortizd6469602011-12-14 16:43:12 +0100635
Johannes Berg59ae1d12017-06-16 14:29:20 +0200636 skb_put_data(skb, &reason, 1);
Samuel Ortizd6469602011-12-14 16:43:12 +0100637
638 skb_queue_head(&local->tx_queue, skb);
639
640 return 0;
641}
642
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100643int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100644 struct msghdr *msg, size_t len)
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100645{
646 struct sk_buff *pdu;
Samuel Ortize65b0f42012-03-05 01:03:44 +0100647 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;
Thierry Escande66cbfa12013-04-02 10:25:14 +0200651 u16 remote_miu;
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100652
Samuel Ortize65b0f42012-03-05 01:03:44 +0100653 pr_debug("Send I frame len %zd\n", len);
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100654
Samuel Ortize65b0f42012-03-05 01:03:44 +0100655 local = sock->local;
656 if (local == NULL)
657 return -ENODEV;
658
Samuel Ortizdd2bf432012-11-01 23:33:00 +0100659 /* Remote is ready but has not acknowledged our frames */
660 if((sock->remote_ready &&
Samuel Ortize4306be2013-02-22 01:12:28 +0100661 skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw &&
662 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
Samuel Ortizdd2bf432012-11-01 23:33:00 +0100663 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 &&
Samuel Ortize4306be2013-02-22 01:12:28 +0100670 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
Samuel Ortizdd2bf432012-11-01 23:33:00 +0100671 pr_err("Tx queue is full %d frames\n",
672 skb_queue_len(&sock->tx_queue));
673 return -ENOBUFS;
674 }
675
Cong Wang81ca7832016-01-29 11:24:24 -0800676 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100677 if (msg_data == NULL)
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100678 return -ENOMEM;
679
Al Viro6ce8e9c2014-04-06 21:25:44 -0400680 if (memcpy_from_msg(msg_data, msg, len)) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100681 kfree(msg_data);
682 return -EFAULT;
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100683 }
684
Samuel Ortize65b0f42012-03-05 01:03:44 +0100685 remaining_len = len;
686 msg_ptr = msg_data;
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100687
Olivier Guiter0b23d662013-03-25 11:24:21 +0100688 do {
Thierry Escande66cbfa12013-04-02 10:25:14 +0200689 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
Szymon Janc11bfb1c2013-11-30 16:59:23 +0100690 LLCP_DEFAULT_MIU : sock->remote_miu;
Thierry Escande66cbfa12013-04-02 10:25:14 +0200691
692 frag_len = min_t(size_t, remote_miu, remaining_len);
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100693
Samuel Ortize65b0f42012-03-05 01:03:44 +0100694 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);
Szymon Janc43d53c22013-11-30 16:14:57 +0100699 if (pdu == NULL) {
700 kfree(msg_data);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100701 return -ENOMEM;
Szymon Janc43d53c22013-11-30 16:14:57 +0100702 }
Samuel Ortize65b0f42012-03-05 01:03:44 +0100703
704 skb_put(pdu, LLCP_SEQUENCE_SIZE);
705
Olivier Guiter0b23d662013-03-25 11:24:21 +0100706 if (likely(frag_len > 0))
Johannes Berg59ae1d12017-06-16 14:29:20 +0200707 skb_put_data(pdu, msg_ptr, frag_len);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100708
Samuel Ortizbdbc59b2012-05-10 19:45:52 +0200709 skb_queue_tail(&sock->tx_queue, pdu);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100710
711 lock_sock(sk);
712
713 nfc_llcp_queue_i_frames(sock);
714
715 release_sock(sk);
716
717 remaining_len -= frag_len;
Samuel Ortizb4838d12012-04-10 19:43:03 +0200718 msg_ptr += frag_len;
Olivier Guiter0b23d662013-03-25 11:24:21 +0100719 } while (remaining_len > 0);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100720
721 kfree(msg_data);
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100722
Samuel Ortiz43472ff2012-05-07 12:31:21 +0200723 return len;
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100724}
Samuel Ortizd094afa2012-03-05 01:03:42 +0100725
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200726int 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;
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100732 u8 *msg_ptr, *msg_data;
Thierry Escande66cbfa12013-04-02 10:25:14 +0200733 u16 remote_miu;
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200734 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
Cong Wang81ca7832016-01-29 11:24:24 -0800742 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100743 if (msg_data == NULL)
744 return -ENOMEM;
745
Al Viro6ce8e9c2014-04-06 21:25:44 -0400746 if (memcpy_from_msg(msg_data, msg, len)) {
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100747 kfree(msg_data);
748 return -EFAULT;
749 }
750
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200751 remaining_len = len;
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100752 msg_ptr = msg_data;
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200753
Olivier Guiter0b23d662013-03-25 11:24:21 +0100754 do {
Thierry Escande66cbfa12013-04-02 10:25:14 +0200755 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);
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200759
760 pr_debug("Fragment %zd bytes remaining %zd",
761 frag_len, remaining_len);
762
Tetsuo Handa3bc53be2018-07-18 18:57:27 +0900763 pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, 0,
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200764 frag_len + LLCP_HEADER_SIZE, &err);
765 if (pdu == NULL) {
Tetsuo Handa3bc53be2018-07-18 18:57:27 +0900766 pr_err("Could not allocate PDU (error=%d)\n", err);
767 len -= remaining_len;
768 if (len == 0)
769 len = err;
770 break;
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200771 }
772
773 pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI);
774
Olivier Guiter0b23d662013-03-25 11:24:21 +0100775 if (likely(frag_len > 0))
Johannes Berg59ae1d12017-06-16 14:29:20 +0200776 skb_put_data(pdu, msg_ptr, frag_len);
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200777
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;
Olivier Guiter0b23d662013-03-25 11:24:21 +0100783 } while (remaining_len > 0);
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200784
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100785 kfree(msg_data);
786
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200787 return len;
788}
789
Samuel Ortizd094afa2012-03-05 01:03:42 +0100790int 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
Samuel Ortiz279cf172012-04-10 19:43:14 +0200807 skb->data[2] = sock->recv_n;
Samuel Ortizd094afa2012-03-05 01:03:42 +0100808
809 skb_queue_head(&local->tx_queue, skb);
810
811 return 0;
812}