blob: c5959ce503e6c857c98d5cb7583f48b42b3eac71 [file] [log] [blame]
Samuel Ortizd6469602011-12-14 16:43:12 +01001/*
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
Jeff Kirsher98b32de2013-12-06 08:56:16 -080015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Samuel Ortizd6469602011-12-14 16:43:12 +010016 */
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
Samuel Ortiz30cc4582013-04-26 11:49:40 +020027#include "nfc.h"
Samuel Ortizd6469602011-12-14 16:43:12 +010028#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
Samuel Ortiz76762b72012-05-14 17:38:54 +020052static u16 llcp_tlv16(u8 *tlv, u8 type)
Samuel Ortizd6469602011-12-14 16:43:12 +010053{
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{
Samuel Ortiz76762b72012-05-14 17:38:54 +020068 return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff;
Samuel Ortizd6469602011-12-14 16:43:12 +010069}
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;
Samuel Ortiz324b0af2012-04-10 19:43:15 +0200103 else if (length == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100104 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
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100118struct 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
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100145struct 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
Thierry Escande40213fa2013-03-04 15:43:32 +0100175 sdreq->time = jiffies;
176
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100177 INIT_HLIST_NODE(&sdreq->node);
178
179 return sdreq;
180}
181
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100182void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp)
183{
184 kfree(sdp->tlv);
185 kfree(sdp);
186}
187
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100188void 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
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200200int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
201 u8 *tlv_array, u16 tlv_array_len)
Samuel Ortizd6469602011-12-14 16:43:12 +0100202{
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;
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200232 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) {
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200266 case LLCP_TLV_MIUX:
Samuel Ortize4306be2013-02-22 01:12:28 +0100267 sock->remote_miu = llcp_tlv_miux(tlv) + 128;
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200268 break;
Samuel Ortizd6469602011-12-14 16:43:12 +0100269 case LLCP_TLV_RW:
Samuel Ortize4306be2013-02-22 01:12:28 +0100270 sock->remote_rw = llcp_tlv_rw(tlv);
Samuel Ortizd6469602011-12-14 16:43:12 +0100271 break;
Samuel Ortiz9dda50f2012-03-05 01:03:49 +0100272 case LLCP_TLV_SN:
273 break;
Samuel Ortizd6469602011-12-14 16:43:12 +0100274 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
Samuel Ortize4306be2013-02-22 01:12:28 +0100283 pr_debug("sock %p rw %d miu %d\n", sock,
284 sock->remote_rw, sock->remote_miu);
Samuel Ortizd6469602011-12-14 16:43:12 +0100285
286 return 0;
287}
288
289static struct sk_buff *llcp_add_header(struct sk_buff *pdu,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100290 u8 dsap, u8 ssap, u8 ptype)
Samuel Ortizd6469602011-12-14 16:43:12 +0100291{
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
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100306static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv,
307 u8 tlv_length)
Samuel Ortizd6469602011-12-14 16:43:12 +0100308{
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,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100320 u8 cmd, u16 size)
Samuel Ortizd6469602011-12-14 16:43:12 +0100321{
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,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100329 size + LLCP_HEADER_SIZE, &err);
Samuel Ortizd6469602011-12-14 16:43:12 +0100330 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
Thierry Escande58e3dd12013-06-04 11:34:50 +0200340int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100341{
342 struct sk_buff *skb;
343 struct nfc_dev *dev;
344 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +0100345
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
Samuel Ortiz92223902012-10-05 01:09:07 +0200356 skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
Samuel Ortizd6469602011-12-14 16:43:12 +0100357 if (skb == NULL)
358 return -ENOMEM;
359
Samuel Ortizd6469602011-12-14 16:43:12 +0100360 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
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100388 __net_timestamp(skb);
389
Hiren Tandel57be1f32014-05-05 19:43:31 +0900390 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX);
Thierry Escande4463523b2012-09-26 18:16:44 +0200391
Samuel Ortizd6469602011-12-14 16:43:12 +0100392 return nfc_data_exchange(dev, local->target_idx, skb,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100393 nfc_llcp_recv, local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100394}
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;
Samuel Ortizeda21f12012-03-05 01:03:43 +0100401 u8 *miux_tlv = NULL, miux_tlv_length;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100402 u8 *rw_tlv = NULL, rw_tlv_length, rw;
Samuel Ortizd6469602011-12-14 16:43:12 +0100403 int err;
Christophe Ricarde5b53c02014-12-02 21:27:57 +0100404 u16 size = 0;
405 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100406
407 pr_debug("Sending CONNECT\n");
408
409 local = sock->local;
410 if (local == NULL)
411 return -ENODEV;
412
413 if (sock->service_name != NULL) {
414 service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100415 sock->service_name,
416 sock->service_name_len,
417 &service_name_tlv_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100418 size += service_name_tlv_length;
419 }
420
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100421 /* If the socket parameters are not set, use the local ones */
Samuel Ortiz5eef6662013-03-20 16:06:12 +0100422 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
423 local->miux : sock->miux;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100424 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
425
426 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100427 &miux_tlv_length);
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);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100431 size += rw_tlv_length;
432
Samuel Ortizd6469602011-12-14 16:43:12 +0100433 pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len);
434
435 skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size);
436 if (skb == NULL) {
437 err = -ENOMEM;
438 goto error_tlv;
439 }
440
Thierry Escandede9e5ae2016-06-29 10:48:22 +0200441 llcp_add_tlv(skb, service_name_tlv, service_name_tlv_length);
442 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
443 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100444
Samuel Ortizd6469602011-12-14 16:43:12 +0100445 skb_queue_tail(&local->tx_queue, skb);
446
Thierry Escande256f3ee2016-06-29 10:48:23 +0200447 err = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100448
449error_tlv:
Thierry Escande256f3ee2016-06-29 10:48:23 +0200450 if (err)
451 pr_err("error %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +0100452
453 kfree(service_name_tlv);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100454 kfree(miux_tlv);
455 kfree(rw_tlv);
Samuel Ortizd6469602011-12-14 16:43:12 +0100456
457 return err;
458}
459
460int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
461{
462 struct nfc_llcp_local *local;
463 struct sk_buff *skb;
Samuel Ortizeda21f12012-03-05 01:03:43 +0100464 u8 *miux_tlv = NULL, miux_tlv_length;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100465 u8 *rw_tlv = NULL, rw_tlv_length, rw;
Samuel Ortizeda21f12012-03-05 01:03:43 +0100466 int err;
Christophe Ricarde5b53c02014-12-02 21:27:57 +0100467 u16 size = 0;
468 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100469
470 pr_debug("Sending CC\n");
471
472 local = sock->local;
473 if (local == NULL)
474 return -ENODEV;
475
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100476 /* If the socket parameters are not set, use the local ones */
Samuel Ortiz5eef6662013-03-20 16:06:12 +0100477 miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
478 local->miux : sock->miux;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100479 rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
480
481 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100482 &miux_tlv_length);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100483 size += miux_tlv_length;
484
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100485 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100486 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
Thierry Escandede9e5ae2016-06-29 10:48:22 +0200494 llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
495 llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100496
497 skb_queue_tail(&local->tx_queue, skb);
498
Thierry Escande256f3ee2016-06-29 10:48:23 +0200499 err = 0;
Samuel Ortizeda21f12012-03-05 01:03:43 +0100500
501error_tlv:
Thierry Escande256f3ee2016-06-29 10:48:23 +0200502 if (err)
503 pr_err("error %d\n", err);
Samuel Ortizeda21f12012-03-05 01:03:43 +0100504
505 kfree(miux_tlv);
506 kfree(rw_tlv);
507
508 return err;
Samuel Ortizd6469602011-12-14 16:43:12 +0100509}
510
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100511static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
512 size_t tlv_length)
Samuel Ortizc43bb032012-10-05 01:13:24 +0200513{
514 struct sk_buff *skb;
515 struct nfc_dev *dev;
Samuel Ortizc43bb032012-10-05 01:13:24 +0200516 u16 size = 0;
517
Samuel Ortizc43bb032012-10-05 01:13:24 +0200518 if (local == NULL)
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100519 return ERR_PTR(-ENODEV);
Samuel Ortizc43bb032012-10-05 01:13:24 +0200520
521 dev = local->dev;
522 if (dev == NULL)
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100523 return ERR_PTR(-ENODEV);
Samuel Ortizc43bb032012-10-05 01:13:24 +0200524
525 size += LLCP_HEADER_SIZE;
526 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100527 size += tlv_length;
Samuel Ortizc43bb032012-10-05 01:13:24 +0200528
529 skb = alloc_skb(size, GFP_KERNEL);
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100530 if (skb == NULL)
531 return ERR_PTR(-ENOMEM);
Samuel Ortizc43bb032012-10-05 01:13:24 +0200532
533 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
534
535 skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL);
536
Thierry Escandee0ae7ba2013-02-15 10:43:05 +0100537 return skb;
538}
539
540int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local,
541 struct hlist_head *tlv_list, size_t tlvs_len)
542{
543 struct nfc_llcp_sdp_tlv *sdp;
544 struct hlist_node *n;
545 struct sk_buff *skb;
546
547 skb = nfc_llcp_allocate_snl(local, tlvs_len);
548 if (IS_ERR(skb))
549 return PTR_ERR(skb);
550
551 hlist_for_each_entry_safe(sdp, n, tlv_list, node) {
552 memcpy(skb_put(skb, sdp->tlv_len), sdp->tlv, sdp->tlv_len);
553
554 hlist_del(&sdp->node);
555
556 nfc_llcp_free_sdp_tlv(sdp);
557 }
Samuel Ortizc43bb032012-10-05 01:13:24 +0200558
559 skb_queue_tail(&local->tx_queue, skb);
560
Samuel Ortizc43bb032012-10-05 01:13:24 +0200561 return 0;
562}
563
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100564int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local,
565 struct hlist_head *tlv_list, size_t tlvs_len)
566{
567 struct nfc_llcp_sdp_tlv *sdreq;
568 struct hlist_node *n;
569 struct sk_buff *skb;
570
571 skb = nfc_llcp_allocate_snl(local, tlvs_len);
572 if (IS_ERR(skb))
573 return PTR_ERR(skb);
574
575 mutex_lock(&local->sdreq_lock);
576
Thierry Escande40213fa2013-03-04 15:43:32 +0100577 if (hlist_empty(&local->pending_sdreqs))
578 mod_timer(&local->sdreq_timer,
579 jiffies + msecs_to_jiffies(3 * local->remote_lto));
580
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100581 hlist_for_each_entry_safe(sdreq, n, tlv_list, node) {
582 pr_debug("tid %d for %s\n", sdreq->tid, sdreq->uri);
583
584 memcpy(skb_put(skb, sdreq->tlv_len), sdreq->tlv,
585 sdreq->tlv_len);
586
587 hlist_del(&sdreq->node);
588
589 hlist_add_head(&sdreq->node, &local->pending_sdreqs);
590 }
591
592 mutex_unlock(&local->sdreq_lock);
593
594 skb_queue_tail(&local->tx_queue, skb);
595
596 return 0;
597}
598
Samuel Ortizd6469602011-12-14 16:43:12 +0100599int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason)
600{
601 struct sk_buff *skb;
602 struct nfc_dev *dev;
603 u16 size = 1; /* Reason code */
604
605 pr_debug("Sending DM reason 0x%x\n", reason);
606
607 if (local == NULL)
608 return -ENODEV;
609
610 dev = local->dev;
611 if (dev == NULL)
612 return -ENODEV;
613
614 size += LLCP_HEADER_SIZE;
615 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
616
617 skb = alloc_skb(size, GFP_KERNEL);
618 if (skb == NULL)
619 return -ENOMEM;
620
621 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
622
Samuel Ortizffc29312012-04-10 19:43:16 +0200623 skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM);
Samuel Ortizd6469602011-12-14 16:43:12 +0100624
625 memcpy(skb_put(skb, 1), &reason, 1);
626
627 skb_queue_head(&local->tx_queue, skb);
628
629 return 0;
630}
631
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100632int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100633 struct msghdr *msg, size_t len)
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100634{
635 struct sk_buff *pdu;
Samuel Ortize65b0f42012-03-05 01:03:44 +0100636 struct sock *sk = &sock->sk;
637 struct nfc_llcp_local *local;
638 size_t frag_len = 0, remaining_len;
639 u8 *msg_data, *msg_ptr;
Thierry Escande66cbfa12013-04-02 10:25:14 +0200640 u16 remote_miu;
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100641
Samuel Ortize65b0f42012-03-05 01:03:44 +0100642 pr_debug("Send I frame len %zd\n", len);
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100643
Samuel Ortize65b0f42012-03-05 01:03:44 +0100644 local = sock->local;
645 if (local == NULL)
646 return -ENODEV;
647
Samuel Ortizdd2bf432012-11-01 23:33:00 +0100648 /* Remote is ready but has not acknowledged our frames */
649 if((sock->remote_ready &&
Samuel Ortize4306be2013-02-22 01:12:28 +0100650 skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw &&
651 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
Samuel Ortizdd2bf432012-11-01 23:33:00 +0100652 pr_err("Pending queue is full %d frames\n",
653 skb_queue_len(&sock->tx_pending_queue));
654 return -ENOBUFS;
655 }
656
657 /* Remote is not ready and we've been queueing enough frames */
658 if ((!sock->remote_ready &&
Samuel Ortize4306be2013-02-22 01:12:28 +0100659 skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
Samuel Ortizdd2bf432012-11-01 23:33:00 +0100660 pr_err("Tx queue is full %d frames\n",
661 skb_queue_len(&sock->tx_queue));
662 return -ENOBUFS;
663 }
664
Cong Wang81ca7832016-01-29 11:24:24 -0800665 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100666 if (msg_data == NULL)
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100667 return -ENOMEM;
668
Al Viro6ce8e9c2014-04-06 21:25:44 -0400669 if (memcpy_from_msg(msg_data, msg, len)) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100670 kfree(msg_data);
671 return -EFAULT;
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100672 }
673
Samuel Ortize65b0f42012-03-05 01:03:44 +0100674 remaining_len = len;
675 msg_ptr = msg_data;
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100676
Olivier Guiter0b23d662013-03-25 11:24:21 +0100677 do {
Thierry Escande66cbfa12013-04-02 10:25:14 +0200678 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
Szymon Janc11bfb1c2013-11-30 16:59:23 +0100679 LLCP_DEFAULT_MIU : sock->remote_miu;
Thierry Escande66cbfa12013-04-02 10:25:14 +0200680
681 frag_len = min_t(size_t, remote_miu, remaining_len);
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100682
Samuel Ortize65b0f42012-03-05 01:03:44 +0100683 pr_debug("Fragment %zd bytes remaining %zd",
684 frag_len, remaining_len);
685
686 pdu = llcp_allocate_pdu(sock, LLCP_PDU_I,
687 frag_len + LLCP_SEQUENCE_SIZE);
Szymon Janc43d53c22013-11-30 16:14:57 +0100688 if (pdu == NULL) {
689 kfree(msg_data);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100690 return -ENOMEM;
Szymon Janc43d53c22013-11-30 16:14:57 +0100691 }
Samuel Ortize65b0f42012-03-05 01:03:44 +0100692
693 skb_put(pdu, LLCP_SEQUENCE_SIZE);
694
Olivier Guiter0b23d662013-03-25 11:24:21 +0100695 if (likely(frag_len > 0))
696 memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100697
Samuel Ortizbdbc59b2012-05-10 19:45:52 +0200698 skb_queue_tail(&sock->tx_queue, pdu);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100699
700 lock_sock(sk);
701
702 nfc_llcp_queue_i_frames(sock);
703
704 release_sock(sk);
705
706 remaining_len -= frag_len;
Samuel Ortizb4838d12012-04-10 19:43:03 +0200707 msg_ptr += frag_len;
Olivier Guiter0b23d662013-03-25 11:24:21 +0100708 } while (remaining_len > 0);
Samuel Ortize65b0f42012-03-05 01:03:44 +0100709
710 kfree(msg_data);
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100711
Samuel Ortiz43472ff2012-05-07 12:31:21 +0200712 return len;
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100713}
Samuel Ortizd094afa2012-03-05 01:03:42 +0100714
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200715int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap,
716 struct msghdr *msg, size_t len)
717{
718 struct sk_buff *pdu;
719 struct nfc_llcp_local *local;
720 size_t frag_len = 0, remaining_len;
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100721 u8 *msg_ptr, *msg_data;
Thierry Escande66cbfa12013-04-02 10:25:14 +0200722 u16 remote_miu;
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200723 int err;
724
725 pr_debug("Send UI frame len %zd\n", len);
726
727 local = sock->local;
728 if (local == NULL)
729 return -ENODEV;
730
Cong Wang81ca7832016-01-29 11:24:24 -0800731 msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100732 if (msg_data == NULL)
733 return -ENOMEM;
734
Al Viro6ce8e9c2014-04-06 21:25:44 -0400735 if (memcpy_from_msg(msg_data, msg, len)) {
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100736 kfree(msg_data);
737 return -EFAULT;
738 }
739
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200740 remaining_len = len;
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100741 msg_ptr = msg_data;
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200742
Olivier Guiter0b23d662013-03-25 11:24:21 +0100743 do {
Thierry Escande66cbfa12013-04-02 10:25:14 +0200744 remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
745 local->remote_miu : sock->remote_miu;
746
747 frag_len = min_t(size_t, remote_miu, remaining_len);
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200748
749 pr_debug("Fragment %zd bytes remaining %zd",
750 frag_len, remaining_len);
751
752 pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
753 frag_len + LLCP_HEADER_SIZE, &err);
754 if (pdu == NULL) {
755 pr_err("Could not allocate PDU\n");
756 continue;
757 }
758
759 pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI);
760
Olivier Guiter0b23d662013-03-25 11:24:21 +0100761 if (likely(frag_len > 0))
762 memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len);
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200763
764 /* No need to check for the peer RW for UI frames */
765 skb_queue_tail(&local->tx_queue, pdu);
766
767 remaining_len -= frag_len;
768 msg_ptr += frag_len;
Olivier Guiter0b23d662013-03-25 11:24:21 +0100769 } while (remaining_len > 0);
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200770
Samuel Ortiz6e950fd2012-10-29 14:02:17 +0100771 kfree(msg_data);
772
Samuel Ortiz94f418a2012-10-16 15:01:40 +0200773 return len;
774}
775
Samuel Ortizd094afa2012-03-05 01:03:42 +0100776int nfc_llcp_send_rr(struct nfc_llcp_sock *sock)
777{
778 struct sk_buff *skb;
779 struct nfc_llcp_local *local;
780
781 pr_debug("Send rr nr %d\n", sock->recv_n);
782
783 local = sock->local;
784 if (local == NULL)
785 return -ENODEV;
786
787 skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE);
788 if (skb == NULL)
789 return -ENOMEM;
790
791 skb_put(skb, LLCP_SEQUENCE_SIZE);
792
Samuel Ortiz279cf172012-04-10 19:43:14 +0200793 skb->data[2] = sock->recv_n;
Samuel Ortizd094afa2012-03-05 01:03:42 +0100794
795 skb_queue_head(&local->tx_queue, skb);
796
797 return 0;
798}