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 | |
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 | |
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 | |
49 | static u8 llcp_tlv_version(const u8 *tlv) |
50 | { |
51 | return llcp_tlv8(tlv, LLCP_TLV_VERSION); |
52 | } |
53 | |
54 | static u16 llcp_tlv_miux(const u8 *tlv) |
55 | { |
56 | return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff; |
57 | } |
58 | |
59 | static u16 llcp_tlv_wks(const u8 *tlv) |
60 | { |
61 | return llcp_tlv16(tlv, LLCP_TLV_WKS); |
62 | } |
63 | |
64 | static u16 llcp_tlv_lto(const u8 *tlv) |
65 | { |
66 | return llcp_tlv8(tlv, LLCP_TLV_LTO); |
67 | } |
68 | |
69 | static u8 llcp_tlv_opt(const u8 *tlv) |
70 | { |
71 | return llcp_tlv8(tlv, LLCP_TLV_OPT); |
72 | } |
73 | |
74 | static u8 llcp_tlv_rw(const u8 *tlv) |
75 | { |
76 | return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf; |
77 | } |
78 | |
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(size: 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 | |
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(size: 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, value_length: 2, |
119 | tlv_length: &sdres->tlv_len); |
120 | if (sdres->tlv == NULL) { |
121 | kfree(objp: sdres); |
122 | return NULL; |
123 | } |
124 | |
125 | sdres->tid = tid; |
126 | sdres->sap = sap; |
127 | |
128 | INIT_HLIST_NODE(h: &sdres->node); |
129 | |
130 | return sdres; |
131 | } |
132 | |
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(size: 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(size: sdreq->tlv_len + 1, GFP_KERNEL); |
154 | if (sdreq->tlv == NULL) { |
155 | kfree(objp: 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(h: &sdreq->node); |
170 | |
171 | return sdreq; |
172 | } |
173 | |
174 | void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp) |
175 | { |
176 | kfree(objp: sdp->tlv); |
177 | kfree(objp: sdp); |
178 | } |
179 | |
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(n: &sdp->node); |
187 | |
188 | nfc_llcp_free_sdp_tlv(sdp); |
189 | } |
190 | } |
191 | |
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 | |
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 | |
283 | static struct sk_buff *(struct sk_buff *pdu, |
284 | u8 dsap, u8 ssap, u8 ptype) |
285 | { |
286 | u8 [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(skb: pdu, data: header, LLCP_HEADER_SIZE); |
296 | |
297 | return pdu; |
298 | } |
299 | |
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(skb: pdu, data: tlv, len: tlv_length); |
309 | |
310 | return pdu; |
311 | } |
312 | |
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(dev: sock->dev, sk: &sock->sk, MSG_DONTWAIT, |
323 | size: size + LLCP_HEADER_SIZE, err: &err); |
324 | if (skb == NULL) { |
325 | pr_err("Could not allocate PDU\n" ); |
326 | return NULL; |
327 | } |
328 | |
329 | skb = llcp_add_header(pdu: skb, dsap: sock->dsap, ssap: sock->ssap, ptype: cmd); |
330 | |
331 | return skb; |
332 | } |
333 | |
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 | 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, size: 0); |
349 | if (skb == NULL) |
350 | return -ENOMEM; |
351 | |
352 | skb_queue_tail(list: &local->tx_queue, newsk: skb); |
353 | |
354 | return 0; |
355 | } |
356 | |
357 | int 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 | int err; |
363 | |
364 | local = nfc_llcp_find_local(dev); |
365 | if (local == NULL) |
366 | return -ENODEV; |
367 | |
368 | size += LLCP_HEADER_SIZE; |
369 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
370 | |
371 | skb = alloc_skb(size, GFP_KERNEL); |
372 | if (skb == NULL) { |
373 | err = -ENOMEM; |
374 | goto out; |
375 | } |
376 | |
377 | skb_reserve(skb, len: dev->tx_headroom + NFC_HEADER_SIZE); |
378 | |
379 | skb = llcp_add_header(pdu: skb, dsap: 0, ssap: 0, LLCP_PDU_SYMM); |
380 | |
381 | __net_timestamp(skb); |
382 | |
383 | nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX); |
384 | |
385 | err = nfc_data_exchange(dev, target_idx: local->target_idx, skb, |
386 | cb: nfc_llcp_recv, cb_context: local); |
387 | out: |
388 | nfc_llcp_local_put(local); |
389 | return err; |
390 | } |
391 | |
392 | int nfc_llcp_send_connect(struct nfc_llcp_sock *sock) |
393 | { |
394 | struct nfc_llcp_local *local; |
395 | struct sk_buff *skb; |
396 | const u8 *service_name_tlv = NULL; |
397 | const u8 *miux_tlv = NULL; |
398 | const u8 *rw_tlv = NULL; |
399 | u8 service_name_tlv_length = 0; |
400 | u8 miux_tlv_length, rw_tlv_length, rw; |
401 | int err; |
402 | u16 size = 0; |
403 | __be16 miux; |
404 | |
405 | local = sock->local; |
406 | if (local == NULL) |
407 | return -ENODEV; |
408 | |
409 | if (sock->service_name != NULL) { |
410 | service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN, |
411 | value: sock->service_name, |
412 | value_length: sock->service_name_len, |
413 | tlv_length: &service_name_tlv_length); |
414 | if (!service_name_tlv) { |
415 | err = -ENOMEM; |
416 | goto error_tlv; |
417 | } |
418 | size += service_name_tlv_length; |
419 | } |
420 | |
421 | /* If the socket parameters are not set, use the local ones */ |
422 | miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ? |
423 | local->miux : sock->miux; |
424 | rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw; |
425 | |
426 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, value: (u8 *)&miux, value_length: 0, |
427 | tlv_length: &miux_tlv_length); |
428 | if (!miux_tlv) { |
429 | err = -ENOMEM; |
430 | goto error_tlv; |
431 | } |
432 | size += miux_tlv_length; |
433 | |
434 | rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, value: &rw, value_length: 0, tlv_length: &rw_tlv_length); |
435 | if (!rw_tlv) { |
436 | err = -ENOMEM; |
437 | goto error_tlv; |
438 | } |
439 | size += rw_tlv_length; |
440 | |
441 | pr_debug("SKB size %d SN length %zu\n" , size, sock->service_name_len); |
442 | |
443 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size); |
444 | if (skb == NULL) { |
445 | err = -ENOMEM; |
446 | goto error_tlv; |
447 | } |
448 | |
449 | llcp_add_tlv(pdu: skb, tlv: service_name_tlv, tlv_length: service_name_tlv_length); |
450 | llcp_add_tlv(pdu: skb, tlv: miux_tlv, tlv_length: miux_tlv_length); |
451 | llcp_add_tlv(pdu: skb, tlv: rw_tlv, tlv_length: rw_tlv_length); |
452 | |
453 | skb_queue_tail(list: &local->tx_queue, newsk: skb); |
454 | |
455 | err = 0; |
456 | |
457 | error_tlv: |
458 | if (err) |
459 | pr_err("error %d\n" , err); |
460 | |
461 | kfree(objp: service_name_tlv); |
462 | kfree(objp: miux_tlv); |
463 | kfree(objp: rw_tlv); |
464 | |
465 | return err; |
466 | } |
467 | |
468 | int nfc_llcp_send_cc(struct nfc_llcp_sock *sock) |
469 | { |
470 | struct nfc_llcp_local *local; |
471 | struct sk_buff *skb; |
472 | const u8 *miux_tlv = NULL; |
473 | const u8 *rw_tlv = NULL; |
474 | u8 miux_tlv_length, rw_tlv_length, rw; |
475 | int err; |
476 | u16 size = 0; |
477 | __be16 miux; |
478 | |
479 | local = sock->local; |
480 | if (local == NULL) |
481 | return -ENODEV; |
482 | |
483 | /* If the socket parameters are not set, use the local ones */ |
484 | miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ? |
485 | local->miux : sock->miux; |
486 | rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw; |
487 | |
488 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, value: (u8 *)&miux, value_length: 0, |
489 | tlv_length: &miux_tlv_length); |
490 | if (!miux_tlv) { |
491 | err = -ENOMEM; |
492 | goto error_tlv; |
493 | } |
494 | size += miux_tlv_length; |
495 | |
496 | rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, value: &rw, value_length: 0, tlv_length: &rw_tlv_length); |
497 | if (!rw_tlv) { |
498 | err = -ENOMEM; |
499 | goto error_tlv; |
500 | } |
501 | size += rw_tlv_length; |
502 | |
503 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size); |
504 | if (skb == NULL) { |
505 | err = -ENOMEM; |
506 | goto error_tlv; |
507 | } |
508 | |
509 | llcp_add_tlv(pdu: skb, tlv: miux_tlv, tlv_length: miux_tlv_length); |
510 | llcp_add_tlv(pdu: skb, tlv: rw_tlv, tlv_length: rw_tlv_length); |
511 | |
512 | skb_queue_tail(list: &local->tx_queue, newsk: skb); |
513 | |
514 | err = 0; |
515 | |
516 | error_tlv: |
517 | if (err) |
518 | pr_err("error %d\n" , err); |
519 | |
520 | kfree(objp: miux_tlv); |
521 | kfree(objp: rw_tlv); |
522 | |
523 | return err; |
524 | } |
525 | |
526 | static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local, |
527 | size_t tlv_length) |
528 | { |
529 | struct sk_buff *skb; |
530 | struct nfc_dev *dev; |
531 | u16 size = 0; |
532 | |
533 | if (local == NULL) |
534 | return ERR_PTR(error: -ENODEV); |
535 | |
536 | dev = local->dev; |
537 | if (dev == NULL) |
538 | return ERR_PTR(error: -ENODEV); |
539 | |
540 | size += LLCP_HEADER_SIZE; |
541 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
542 | size += tlv_length; |
543 | |
544 | skb = alloc_skb(size, GFP_KERNEL); |
545 | if (skb == NULL) |
546 | return ERR_PTR(error: -ENOMEM); |
547 | |
548 | skb_reserve(skb, len: dev->tx_headroom + NFC_HEADER_SIZE); |
549 | |
550 | skb = llcp_add_header(pdu: skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL); |
551 | |
552 | return skb; |
553 | } |
554 | |
555 | int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local, |
556 | struct hlist_head *tlv_list, size_t tlvs_len) |
557 | { |
558 | struct nfc_llcp_sdp_tlv *sdp; |
559 | struct hlist_node *n; |
560 | struct sk_buff *skb; |
561 | |
562 | skb = nfc_llcp_allocate_snl(local, tlv_length: tlvs_len); |
563 | if (IS_ERR(ptr: skb)) |
564 | return PTR_ERR(ptr: skb); |
565 | |
566 | hlist_for_each_entry_safe(sdp, n, tlv_list, node) { |
567 | skb_put_data(skb, data: sdp->tlv, len: sdp->tlv_len); |
568 | |
569 | hlist_del(n: &sdp->node); |
570 | |
571 | nfc_llcp_free_sdp_tlv(sdp); |
572 | } |
573 | |
574 | skb_queue_tail(list: &local->tx_queue, newsk: skb); |
575 | |
576 | return 0; |
577 | } |
578 | |
579 | int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local, |
580 | struct hlist_head *tlv_list, size_t tlvs_len) |
581 | { |
582 | struct nfc_llcp_sdp_tlv *sdreq; |
583 | struct hlist_node *n; |
584 | struct sk_buff *skb; |
585 | |
586 | skb = nfc_llcp_allocate_snl(local, tlv_length: tlvs_len); |
587 | if (IS_ERR(ptr: skb)) |
588 | return PTR_ERR(ptr: skb); |
589 | |
590 | mutex_lock(&local->sdreq_lock); |
591 | |
592 | if (hlist_empty(h: &local->pending_sdreqs)) |
593 | mod_timer(timer: &local->sdreq_timer, |
594 | expires: jiffies + msecs_to_jiffies(m: 3 * local->remote_lto)); |
595 | |
596 | hlist_for_each_entry_safe(sdreq, n, tlv_list, node) { |
597 | pr_debug("tid %d for %s\n" , sdreq->tid, sdreq->uri); |
598 | |
599 | skb_put_data(skb, data: sdreq->tlv, len: sdreq->tlv_len); |
600 | |
601 | hlist_del(n: &sdreq->node); |
602 | |
603 | hlist_add_head(n: &sdreq->node, h: &local->pending_sdreqs); |
604 | } |
605 | |
606 | mutex_unlock(lock: &local->sdreq_lock); |
607 | |
608 | skb_queue_tail(list: &local->tx_queue, newsk: skb); |
609 | |
610 | return 0; |
611 | } |
612 | |
613 | int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason) |
614 | { |
615 | struct sk_buff *skb; |
616 | struct nfc_dev *dev; |
617 | u16 size = 1; /* Reason code */ |
618 | |
619 | pr_debug("Sending DM reason 0x%x\n" , reason); |
620 | |
621 | if (local == NULL) |
622 | return -ENODEV; |
623 | |
624 | dev = local->dev; |
625 | if (dev == NULL) |
626 | return -ENODEV; |
627 | |
628 | size += LLCP_HEADER_SIZE; |
629 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
630 | |
631 | skb = alloc_skb(size, GFP_KERNEL); |
632 | if (skb == NULL) |
633 | return -ENOMEM; |
634 | |
635 | skb_reserve(skb, len: dev->tx_headroom + NFC_HEADER_SIZE); |
636 | |
637 | skb = llcp_add_header(pdu: skb, dsap, ssap, LLCP_PDU_DM); |
638 | |
639 | skb_put_data(skb, data: &reason, len: 1); |
640 | |
641 | skb_queue_head(list: &local->tx_queue, newsk: skb); |
642 | |
643 | return 0; |
644 | } |
645 | |
646 | int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, |
647 | struct msghdr *msg, size_t len) |
648 | { |
649 | struct sk_buff *pdu; |
650 | struct sock *sk = &sock->sk; |
651 | struct nfc_llcp_local *local; |
652 | size_t frag_len = 0, remaining_len; |
653 | u8 *msg_data, *msg_ptr; |
654 | u16 remote_miu; |
655 | |
656 | pr_debug("Send I frame len %zd\n" , len); |
657 | |
658 | local = sock->local; |
659 | if (local == NULL) |
660 | return -ENODEV; |
661 | |
662 | /* Remote is ready but has not acknowledged our frames */ |
663 | if((sock->remote_ready && |
664 | skb_queue_len(list_: &sock->tx_pending_queue) >= sock->remote_rw && |
665 | skb_queue_len(list_: &sock->tx_queue) >= 2 * sock->remote_rw)) { |
666 | pr_err("Pending queue is full %d frames\n" , |
667 | skb_queue_len(&sock->tx_pending_queue)); |
668 | return -ENOBUFS; |
669 | } |
670 | |
671 | /* Remote is not ready and we've been queueing enough frames */ |
672 | if ((!sock->remote_ready && |
673 | skb_queue_len(list_: &sock->tx_queue) >= 2 * sock->remote_rw)) { |
674 | pr_err("Tx queue is full %d frames\n" , |
675 | skb_queue_len(&sock->tx_queue)); |
676 | return -ENOBUFS; |
677 | } |
678 | |
679 | msg_data = kmalloc(size: len, GFP_USER | __GFP_NOWARN); |
680 | if (msg_data == NULL) |
681 | return -ENOMEM; |
682 | |
683 | if (memcpy_from_msg(data: msg_data, msg, len)) { |
684 | kfree(objp: msg_data); |
685 | return -EFAULT; |
686 | } |
687 | |
688 | remaining_len = len; |
689 | msg_ptr = msg_data; |
690 | |
691 | do { |
692 | remote_miu = sock->remote_miu > LLCP_MAX_MIU ? |
693 | LLCP_DEFAULT_MIU : sock->remote_miu; |
694 | |
695 | frag_len = min_t(size_t, remote_miu, remaining_len); |
696 | |
697 | pr_debug("Fragment %zd bytes remaining %zd" , |
698 | frag_len, remaining_len); |
699 | |
700 | pdu = llcp_allocate_pdu(sock, LLCP_PDU_I, |
701 | size: frag_len + LLCP_SEQUENCE_SIZE); |
702 | if (pdu == NULL) { |
703 | kfree(objp: msg_data); |
704 | return -ENOMEM; |
705 | } |
706 | |
707 | skb_put(skb: pdu, LLCP_SEQUENCE_SIZE); |
708 | |
709 | if (likely(frag_len > 0)) |
710 | skb_put_data(skb: pdu, data: msg_ptr, len: frag_len); |
711 | |
712 | skb_queue_tail(list: &sock->tx_queue, newsk: pdu); |
713 | |
714 | lock_sock(sk); |
715 | |
716 | nfc_llcp_queue_i_frames(sock); |
717 | |
718 | release_sock(sk); |
719 | |
720 | remaining_len -= frag_len; |
721 | msg_ptr += frag_len; |
722 | } while (remaining_len > 0); |
723 | |
724 | kfree(objp: msg_data); |
725 | |
726 | return len; |
727 | } |
728 | |
729 | int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, |
730 | struct msghdr *msg, size_t len) |
731 | { |
732 | struct sk_buff *pdu; |
733 | struct nfc_llcp_local *local; |
734 | size_t frag_len = 0, remaining_len; |
735 | u8 *msg_ptr, *msg_data; |
736 | u16 remote_miu; |
737 | int err; |
738 | |
739 | pr_debug("Send UI frame len %zd\n" , len); |
740 | |
741 | local = sock->local; |
742 | if (local == NULL) |
743 | return -ENODEV; |
744 | |
745 | msg_data = kmalloc(size: len, GFP_USER | __GFP_NOWARN); |
746 | if (msg_data == NULL) |
747 | return -ENOMEM; |
748 | |
749 | if (memcpy_from_msg(data: msg_data, msg, len)) { |
750 | kfree(objp: msg_data); |
751 | return -EFAULT; |
752 | } |
753 | |
754 | remaining_len = len; |
755 | msg_ptr = msg_data; |
756 | |
757 | do { |
758 | remote_miu = sock->remote_miu > LLCP_MAX_MIU ? |
759 | local->remote_miu : sock->remote_miu; |
760 | |
761 | frag_len = min_t(size_t, remote_miu, remaining_len); |
762 | |
763 | pr_debug("Fragment %zd bytes remaining %zd" , |
764 | frag_len, remaining_len); |
765 | |
766 | pdu = nfc_alloc_send_skb(dev: sock->dev, sk: &sock->sk, flags: 0, |
767 | size: frag_len + LLCP_HEADER_SIZE, err: &err); |
768 | if (pdu == NULL) { |
769 | pr_err("Could not allocate PDU (error=%d)\n" , err); |
770 | len -= remaining_len; |
771 | if (len == 0) |
772 | len = err; |
773 | break; |
774 | } |
775 | |
776 | pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI); |
777 | |
778 | if (likely(frag_len > 0)) |
779 | skb_put_data(skb: pdu, data: msg_ptr, len: frag_len); |
780 | |
781 | /* No need to check for the peer RW for UI frames */ |
782 | skb_queue_tail(list: &local->tx_queue, newsk: pdu); |
783 | |
784 | remaining_len -= frag_len; |
785 | msg_ptr += frag_len; |
786 | } while (remaining_len > 0); |
787 | |
788 | kfree(objp: msg_data); |
789 | |
790 | return len; |
791 | } |
792 | |
793 | int nfc_llcp_send_rr(struct nfc_llcp_sock *sock) |
794 | { |
795 | struct sk_buff *skb; |
796 | struct nfc_llcp_local *local; |
797 | |
798 | pr_debug("Send rr nr %d\n" , sock->recv_n); |
799 | |
800 | local = sock->local; |
801 | if (local == NULL) |
802 | return -ENODEV; |
803 | |
804 | skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE); |
805 | if (skb == NULL) |
806 | return -ENOMEM; |
807 | |
808 | skb_put(skb, LLCP_SEQUENCE_SIZE); |
809 | |
810 | skb->data[2] = sock->recv_n; |
811 | |
812 | skb_queue_head(list: &local->tx_queue, newsk: skb); |
813 | |
814 | return 0; |
815 | } |
816 | |