1 /*
2 * Copyright (C) 2011 Intel Corporation. All rights reserved.
3 * Copyright (C) 2014 Marvell International Ltd.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
20
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/nfc.h>
25
26 #include "nfc.h"
27 #include "llcp.h"
28
29 static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
30
31 static LIST_HEAD(llcp_devices);
32
33 static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb);
34
nfc_llcp_sock_link(struct llcp_sock_list * l,struct sock * sk)35 void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
36 {
37 write_lock(&l->lock);
38 sk_add_node(sk, &l->head);
39 write_unlock(&l->lock);
40 }
41
nfc_llcp_sock_unlink(struct llcp_sock_list * l,struct sock * sk)42 void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
43 {
44 write_lock(&l->lock);
45 sk_del_node_init(sk);
46 write_unlock(&l->lock);
47 }
48
nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock * sock)49 void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock)
50 {
51 sock->remote_rw = LLCP_DEFAULT_RW;
52 sock->remote_miu = LLCP_MAX_MIU + 1;
53 }
54
nfc_llcp_socket_purge(struct nfc_llcp_sock * sock)55 static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
56 {
57 struct nfc_llcp_local *local = sock->local;
58 struct sk_buff *s, *tmp;
59
60 pr_debug("%p\n", &sock->sk);
61
62 skb_queue_purge(&sock->tx_queue);
63 skb_queue_purge(&sock->tx_pending_queue);
64
65 if (local == NULL)
66 return;
67
68 /* Search for local pending SKBs that are related to this socket */
69 skb_queue_walk_safe(&local->tx_queue, s, tmp) {
70 if (s->sk != &sock->sk)
71 continue;
72
73 skb_unlink(s, &local->tx_queue);
74 kfree_skb(s);
75 }
76 }
77
nfc_llcp_socket_release(struct nfc_llcp_local * local,bool device,int err)78 static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device,
79 int err)
80 {
81 struct sock *sk;
82 struct hlist_node *tmp;
83 struct nfc_llcp_sock *llcp_sock;
84
85 skb_queue_purge(&local->tx_queue);
86
87 write_lock(&local->sockets.lock);
88
89 sk_for_each_safe(sk, tmp, &local->sockets.head) {
90 llcp_sock = nfc_llcp_sock(sk);
91
92 bh_lock_sock(sk);
93
94 nfc_llcp_socket_purge(llcp_sock);
95
96 if (sk->sk_state == LLCP_CONNECTED)
97 nfc_put_device(llcp_sock->dev);
98
99 if (sk->sk_state == LLCP_LISTEN) {
100 struct nfc_llcp_sock *lsk, *n;
101 struct sock *accept_sk;
102
103 list_for_each_entry_safe(lsk, n,
104 &llcp_sock->accept_queue,
105 accept_queue) {
106 accept_sk = &lsk->sk;
107 bh_lock_sock(accept_sk);
108
109 nfc_llcp_accept_unlink(accept_sk);
110
111 if (err)
112 accept_sk->sk_err = err;
113 accept_sk->sk_state = LLCP_CLOSED;
114 accept_sk->sk_state_change(sk);
115
116 bh_unlock_sock(accept_sk);
117 }
118 }
119
120 if (err)
121 sk->sk_err = err;
122 sk->sk_state = LLCP_CLOSED;
123 sk->sk_state_change(sk);
124
125 bh_unlock_sock(sk);
126
127 sk_del_node_init(sk);
128 }
129
130 write_unlock(&local->sockets.lock);
131
132 /* If we still have a device, we keep the RAW sockets alive */
133 if (device == true)
134 return;
135
136 write_lock(&local->raw_sockets.lock);
137
138 sk_for_each_safe(sk, tmp, &local->raw_sockets.head) {
139 llcp_sock = nfc_llcp_sock(sk);
140
141 bh_lock_sock(sk);
142
143 nfc_llcp_socket_purge(llcp_sock);
144
145 if (err)
146 sk->sk_err = err;
147 sk->sk_state = LLCP_CLOSED;
148 sk->sk_state_change(sk);
149
150 bh_unlock_sock(sk);
151
152 sk_del_node_init(sk);
153 }
154
155 write_unlock(&local->raw_sockets.lock);
156 }
157
nfc_llcp_local_get(struct nfc_llcp_local * local)158 struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
159 {
160 kref_get(&local->ref);
161
162 return local;
163 }
164
local_cleanup(struct nfc_llcp_local * local)165 static void local_cleanup(struct nfc_llcp_local *local)
166 {
167 nfc_llcp_socket_release(local, false, ENXIO);
168 del_timer_sync(&local->link_timer);
169 skb_queue_purge(&local->tx_queue);
170 cancel_work_sync(&local->tx_work);
171 cancel_work_sync(&local->rx_work);
172 cancel_work_sync(&local->timeout_work);
173 kfree_skb(local->rx_pending);
174 del_timer_sync(&local->sdreq_timer);
175 cancel_work_sync(&local->sdreq_timeout_work);
176 nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs);
177 }
178
local_release(struct kref * ref)179 static void local_release(struct kref *ref)
180 {
181 struct nfc_llcp_local *local;
182
183 local = container_of(ref, struct nfc_llcp_local, ref);
184
185 list_del(&local->list);
186 local_cleanup(local);
187 kfree(local);
188 }
189
nfc_llcp_local_put(struct nfc_llcp_local * local)190 int nfc_llcp_local_put(struct nfc_llcp_local *local)
191 {
192 if (local == NULL)
193 return 0;
194
195 return kref_put(&local->ref, local_release);
196 }
197
nfc_llcp_sock_get(struct nfc_llcp_local * local,u8 ssap,u8 dsap)198 static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
199 u8 ssap, u8 dsap)
200 {
201 struct sock *sk;
202 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
203
204 pr_debug("ssap dsap %d %d\n", ssap, dsap);
205
206 if (ssap == 0 && dsap == 0)
207 return NULL;
208
209 read_lock(&local->sockets.lock);
210
211 llcp_sock = NULL;
212
213 sk_for_each(sk, &local->sockets.head) {
214 tmp_sock = nfc_llcp_sock(sk);
215
216 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
217 llcp_sock = tmp_sock;
218 break;
219 }
220 }
221
222 read_unlock(&local->sockets.lock);
223
224 if (llcp_sock == NULL)
225 return NULL;
226
227 sock_hold(&llcp_sock->sk);
228
229 return llcp_sock;
230 }
231
nfc_llcp_sock_put(struct nfc_llcp_sock * sock)232 static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
233 {
234 sock_put(&sock->sk);
235 }
236
nfc_llcp_timeout_work(struct work_struct * work)237 static void nfc_llcp_timeout_work(struct work_struct *work)
238 {
239 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
240 timeout_work);
241
242 nfc_dep_link_down(local->dev);
243 }
244
nfc_llcp_symm_timer(unsigned long data)245 static void nfc_llcp_symm_timer(unsigned long data)
246 {
247 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
248
249 pr_err("SYMM timeout\n");
250
251 schedule_work(&local->timeout_work);
252 }
253
nfc_llcp_sdreq_timeout_work(struct work_struct * work)254 static void nfc_llcp_sdreq_timeout_work(struct work_struct *work)
255 {
256 unsigned long time;
257 HLIST_HEAD(nl_sdres_list);
258 struct hlist_node *n;
259 struct nfc_llcp_sdp_tlv *sdp;
260 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
261 sdreq_timeout_work);
262
263 mutex_lock(&local->sdreq_lock);
264
265 time = jiffies - msecs_to_jiffies(3 * local->remote_lto);
266
267 hlist_for_each_entry_safe(sdp, n, &local->pending_sdreqs, node) {
268 if (time_after(sdp->time, time))
269 continue;
270
271 sdp->sap = LLCP_SDP_UNBOUND;
272
273 hlist_del(&sdp->node);
274
275 hlist_add_head(&sdp->node, &nl_sdres_list);
276 }
277
278 if (!hlist_empty(&local->pending_sdreqs))
279 mod_timer(&local->sdreq_timer,
280 jiffies + msecs_to_jiffies(3 * local->remote_lto));
281
282 mutex_unlock(&local->sdreq_lock);
283
284 if (!hlist_empty(&nl_sdres_list))
285 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
286 }
287
nfc_llcp_sdreq_timer(unsigned long data)288 static void nfc_llcp_sdreq_timer(unsigned long data)
289 {
290 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
291
292 schedule_work(&local->sdreq_timeout_work);
293 }
294
nfc_llcp_find_local(struct nfc_dev * dev)295 struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
296 {
297 struct nfc_llcp_local *local;
298
299 list_for_each_entry(local, &llcp_devices, list)
300 if (local->dev == dev)
301 return local;
302
303 pr_debug("No device found\n");
304
305 return NULL;
306 }
307
308 static char *wks[] = {
309 NULL,
310 NULL, /* SDP */
311 "urn:nfc:sn:ip",
312 "urn:nfc:sn:obex",
313 "urn:nfc:sn:snep",
314 };
315
nfc_llcp_wks_sap(char * service_name,size_t service_name_len)316 static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
317 {
318 int sap, num_wks;
319
320 pr_debug("%s\n", service_name);
321
322 if (service_name == NULL)
323 return -EINVAL;
324
325 num_wks = ARRAY_SIZE(wks);
326
327 for (sap = 0; sap < num_wks; sap++) {
328 if (wks[sap] == NULL)
329 continue;
330
331 if (strncmp(wks[sap], service_name, service_name_len) == 0)
332 return sap;
333 }
334
335 return -EINVAL;
336 }
337
338 static
nfc_llcp_sock_from_sn(struct nfc_llcp_local * local,u8 * sn,size_t sn_len)339 struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
340 u8 *sn, size_t sn_len)
341 {
342 struct sock *sk;
343 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
344
345 pr_debug("sn %zd %p\n", sn_len, sn);
346
347 if (sn == NULL || sn_len == 0)
348 return NULL;
349
350 read_lock(&local->sockets.lock);
351
352 llcp_sock = NULL;
353
354 sk_for_each(sk, &local->sockets.head) {
355 tmp_sock = nfc_llcp_sock(sk);
356
357 pr_debug("llcp sock %p\n", tmp_sock);
358
359 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
360 tmp_sock->sk.sk_state != LLCP_LISTEN)
361 continue;
362
363 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
364 tmp_sock->sk.sk_state != LLCP_BOUND)
365 continue;
366
367 if (tmp_sock->service_name == NULL ||
368 tmp_sock->service_name_len == 0)
369 continue;
370
371 if (tmp_sock->service_name_len != sn_len)
372 continue;
373
374 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
375 llcp_sock = tmp_sock;
376 break;
377 }
378 }
379
380 read_unlock(&local->sockets.lock);
381
382 pr_debug("Found llcp sock %p\n", llcp_sock);
383
384 return llcp_sock;
385 }
386
nfc_llcp_get_sdp_ssap(struct nfc_llcp_local * local,struct nfc_llcp_sock * sock)387 u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
388 struct nfc_llcp_sock *sock)
389 {
390 mutex_lock(&local->sdp_lock);
391
392 if (sock->service_name != NULL && sock->service_name_len > 0) {
393 int ssap = nfc_llcp_wks_sap(sock->service_name,
394 sock->service_name_len);
395
396 if (ssap > 0) {
397 pr_debug("WKS %d\n", ssap);
398
399 /* This is a WKS, let's check if it's free */
400 if (local->local_wks & BIT(ssap)) {
401 mutex_unlock(&local->sdp_lock);
402
403 return LLCP_SAP_MAX;
404 }
405
406 set_bit(ssap, &local->local_wks);
407 mutex_unlock(&local->sdp_lock);
408
409 return ssap;
410 }
411
412 /*
413 * Check if there already is a non WKS socket bound
414 * to this service name.
415 */
416 if (nfc_llcp_sock_from_sn(local, sock->service_name,
417 sock->service_name_len) != NULL) {
418 mutex_unlock(&local->sdp_lock);
419
420 return LLCP_SAP_MAX;
421 }
422
423 mutex_unlock(&local->sdp_lock);
424
425 return LLCP_SDP_UNBOUND;
426
427 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
428 if (!test_bit(sock->ssap, &local->local_wks)) {
429 set_bit(sock->ssap, &local->local_wks);
430 mutex_unlock(&local->sdp_lock);
431
432 return sock->ssap;
433 }
434 }
435
436 mutex_unlock(&local->sdp_lock);
437
438 return LLCP_SAP_MAX;
439 }
440
nfc_llcp_get_local_ssap(struct nfc_llcp_local * local)441 u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
442 {
443 u8 local_ssap;
444
445 mutex_lock(&local->sdp_lock);
446
447 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
448 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
449 mutex_unlock(&local->sdp_lock);
450 return LLCP_SAP_MAX;
451 }
452
453 set_bit(local_ssap, &local->local_sap);
454
455 mutex_unlock(&local->sdp_lock);
456
457 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
458 }
459
nfc_llcp_put_ssap(struct nfc_llcp_local * local,u8 ssap)460 void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
461 {
462 u8 local_ssap;
463 unsigned long *sdp;
464
465 if (ssap < LLCP_WKS_NUM_SAP) {
466 local_ssap = ssap;
467 sdp = &local->local_wks;
468 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
469 atomic_t *client_cnt;
470
471 local_ssap = ssap - LLCP_WKS_NUM_SAP;
472 sdp = &local->local_sdp;
473 client_cnt = &local->local_sdp_cnt[local_ssap];
474
475 pr_debug("%d clients\n", atomic_read(client_cnt));
476
477 mutex_lock(&local->sdp_lock);
478
479 if (atomic_dec_and_test(client_cnt)) {
480 struct nfc_llcp_sock *l_sock;
481
482 pr_debug("No more clients for SAP %d\n", ssap);
483
484 clear_bit(local_ssap, sdp);
485
486 /* Find the listening sock and set it back to UNBOUND */
487 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
488 if (l_sock) {
489 l_sock->ssap = LLCP_SDP_UNBOUND;
490 nfc_llcp_sock_put(l_sock);
491 }
492 }
493
494 mutex_unlock(&local->sdp_lock);
495
496 return;
497 } else if (ssap < LLCP_MAX_SAP) {
498 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
499 sdp = &local->local_sap;
500 } else {
501 return;
502 }
503
504 mutex_lock(&local->sdp_lock);
505
506 clear_bit(local_ssap, sdp);
507
508 mutex_unlock(&local->sdp_lock);
509 }
510
nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local * local)511 static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
512 {
513 u8 ssap;
514
515 mutex_lock(&local->sdp_lock);
516
517 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
518 if (ssap == LLCP_SDP_NUM_SAP) {
519 mutex_unlock(&local->sdp_lock);
520
521 return LLCP_SAP_MAX;
522 }
523
524 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
525
526 set_bit(ssap, &local->local_sdp);
527
528 mutex_unlock(&local->sdp_lock);
529
530 return LLCP_WKS_NUM_SAP + ssap;
531 }
532
nfc_llcp_build_gb(struct nfc_llcp_local * local)533 static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
534 {
535 u8 *gb_cur, version, version_length;
536 u8 lto_length, wks_length, miux_length;
537 u8 *version_tlv = NULL, *lto_tlv = NULL,
538 *wks_tlv = NULL, *miux_tlv = NULL;
539 __be16 wks = cpu_to_be16(local->local_wks);
540 u8 gb_len = 0;
541 int ret = 0;
542
543 version = LLCP_VERSION_11;
544 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
545 1, &version_length);
546 if (!version_tlv) {
547 ret = -ENOMEM;
548 goto out;
549 }
550 gb_len += version_length;
551
552 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, <o_length);
553 if (!lto_tlv) {
554 ret = -ENOMEM;
555 goto out;
556 }
557 gb_len += lto_length;
558
559 pr_debug("Local wks 0x%lx\n", local->local_wks);
560 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&wks, 2, &wks_length);
561 if (!wks_tlv) {
562 ret = -ENOMEM;
563 goto out;
564 }
565 gb_len += wks_length;
566
567 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
568 &miux_length);
569 if (!miux_tlv) {
570 ret = -ENOMEM;
571 goto out;
572 }
573 gb_len += miux_length;
574
575 gb_len += ARRAY_SIZE(llcp_magic);
576
577 if (gb_len > NFC_MAX_GT_LEN) {
578 ret = -EINVAL;
579 goto out;
580 }
581
582 gb_cur = local->gb;
583
584 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
585 gb_cur += ARRAY_SIZE(llcp_magic);
586
587 memcpy(gb_cur, version_tlv, version_length);
588 gb_cur += version_length;
589
590 memcpy(gb_cur, lto_tlv, lto_length);
591 gb_cur += lto_length;
592
593 memcpy(gb_cur, wks_tlv, wks_length);
594 gb_cur += wks_length;
595
596 memcpy(gb_cur, miux_tlv, miux_length);
597 gb_cur += miux_length;
598
599 local->gb_len = gb_len;
600
601 out:
602 kfree(version_tlv);
603 kfree(lto_tlv);
604 kfree(wks_tlv);
605 kfree(miux_tlv);
606
607 return ret;
608 }
609
nfc_llcp_general_bytes(struct nfc_dev * dev,size_t * general_bytes_len)610 u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
611 {
612 struct nfc_llcp_local *local;
613
614 local = nfc_llcp_find_local(dev);
615 if (local == NULL) {
616 *general_bytes_len = 0;
617 return NULL;
618 }
619
620 nfc_llcp_build_gb(local);
621
622 *general_bytes_len = local->gb_len;
623
624 return local->gb;
625 }
626
nfc_llcp_set_remote_gb(struct nfc_dev * dev,u8 * gb,u8 gb_len)627 int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
628 {
629 struct nfc_llcp_local *local;
630
631 if (gb_len < 3 || gb_len > NFC_MAX_GT_LEN)
632 return -EINVAL;
633
634 local = nfc_llcp_find_local(dev);
635 if (local == NULL) {
636 pr_err("No LLCP device\n");
637 return -ENODEV;
638 }
639
640 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
641 memcpy(local->remote_gb, gb, gb_len);
642 local->remote_gb_len = gb_len;
643
644 if (memcmp(local->remote_gb, llcp_magic, 3)) {
645 pr_err("MAC does not support LLCP\n");
646 return -EINVAL;
647 }
648
649 return nfc_llcp_parse_gb_tlv(local,
650 &local->remote_gb[3],
651 local->remote_gb_len - 3);
652 }
653
nfc_llcp_dsap(struct sk_buff * pdu)654 static u8 nfc_llcp_dsap(struct sk_buff *pdu)
655 {
656 return (pdu->data[0] & 0xfc) >> 2;
657 }
658
nfc_llcp_ptype(struct sk_buff * pdu)659 static u8 nfc_llcp_ptype(struct sk_buff *pdu)
660 {
661 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
662 }
663
nfc_llcp_ssap(struct sk_buff * pdu)664 static u8 nfc_llcp_ssap(struct sk_buff *pdu)
665 {
666 return pdu->data[1] & 0x3f;
667 }
668
nfc_llcp_ns(struct sk_buff * pdu)669 static u8 nfc_llcp_ns(struct sk_buff *pdu)
670 {
671 return pdu->data[2] >> 4;
672 }
673
nfc_llcp_nr(struct sk_buff * pdu)674 static u8 nfc_llcp_nr(struct sk_buff *pdu)
675 {
676 return pdu->data[2] & 0xf;
677 }
678
nfc_llcp_set_nrns(struct nfc_llcp_sock * sock,struct sk_buff * pdu)679 static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
680 {
681 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
682 sock->send_n = (sock->send_n + 1) % 16;
683 sock->recv_ack_n = (sock->recv_n - 1) % 16;
684 }
685
nfc_llcp_send_to_raw_sock(struct nfc_llcp_local * local,struct sk_buff * skb,u8 direction)686 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
687 struct sk_buff *skb, u8 direction)
688 {
689 struct sk_buff *skb_copy = NULL, *nskb;
690 struct sock *sk;
691 u8 *data;
692
693 read_lock(&local->raw_sockets.lock);
694
695 sk_for_each(sk, &local->raw_sockets.head) {
696 if (sk->sk_state != LLCP_BOUND)
697 continue;
698
699 if (skb_copy == NULL) {
700 skb_copy = __pskb_copy_fclone(skb, NFC_RAW_HEADER_SIZE,
701 GFP_ATOMIC, true);
702
703 if (skb_copy == NULL)
704 continue;
705
706 data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
707
708 data[0] = local->dev ? local->dev->idx : 0xFF;
709 data[1] = direction & 0x01;
710 data[1] |= (RAW_PAYLOAD_LLCP << 1);
711 }
712
713 nskb = skb_clone(skb_copy, GFP_ATOMIC);
714 if (!nskb)
715 continue;
716
717 if (sock_queue_rcv_skb(sk, nskb))
718 kfree_skb(nskb);
719 }
720
721 read_unlock(&local->raw_sockets.lock);
722
723 kfree_skb(skb_copy);
724 }
725
nfc_llcp_tx_work(struct work_struct * work)726 static void nfc_llcp_tx_work(struct work_struct *work)
727 {
728 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
729 tx_work);
730 struct sk_buff *skb;
731 struct sock *sk;
732 struct nfc_llcp_sock *llcp_sock;
733
734 skb = skb_dequeue(&local->tx_queue);
735 if (skb != NULL) {
736 sk = skb->sk;
737 llcp_sock = nfc_llcp_sock(sk);
738
739 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
740 kfree_skb(skb);
741 nfc_llcp_send_symm(local->dev);
742 } else if (llcp_sock && !llcp_sock->remote_ready) {
743 skb_queue_head(&local->tx_queue, skb);
744 nfc_llcp_send_symm(local->dev);
745 } else {
746 struct sk_buff *copy_skb = NULL;
747 u8 ptype = nfc_llcp_ptype(skb);
748 int ret;
749
750 pr_debug("Sending pending skb\n");
751 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
752 DUMP_PREFIX_OFFSET, 16, 1,
753 skb->data, skb->len, true);
754
755 if (ptype == LLCP_PDU_DISC && sk != NULL &&
756 sk->sk_state == LLCP_DISCONNECTING) {
757 nfc_llcp_sock_unlink(&local->sockets, sk);
758 sock_orphan(sk);
759 sock_put(sk);
760 }
761
762 if (ptype == LLCP_PDU_I)
763 copy_skb = skb_copy(skb, GFP_ATOMIC);
764
765 __net_timestamp(skb);
766
767 nfc_llcp_send_to_raw_sock(local, skb,
768 NFC_DIRECTION_TX);
769
770 ret = nfc_data_exchange(local->dev, local->target_idx,
771 skb, nfc_llcp_recv, local);
772
773 if (ret) {
774 kfree_skb(copy_skb);
775 goto out;
776 }
777
778 if (ptype == LLCP_PDU_I && copy_skb)
779 skb_queue_tail(&llcp_sock->tx_pending_queue,
780 copy_skb);
781 }
782 } else {
783 nfc_llcp_send_symm(local->dev);
784 }
785
786 out:
787 mod_timer(&local->link_timer,
788 jiffies + msecs_to_jiffies(2 * local->remote_lto));
789 }
790
nfc_llcp_connecting_sock_get(struct nfc_llcp_local * local,u8 ssap)791 static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
792 u8 ssap)
793 {
794 struct sock *sk;
795 struct nfc_llcp_sock *llcp_sock;
796
797 read_lock(&local->connecting_sockets.lock);
798
799 sk_for_each(sk, &local->connecting_sockets.head) {
800 llcp_sock = nfc_llcp_sock(sk);
801
802 if (llcp_sock->ssap == ssap) {
803 sock_hold(&llcp_sock->sk);
804 goto out;
805 }
806 }
807
808 llcp_sock = NULL;
809
810 out:
811 read_unlock(&local->connecting_sockets.lock);
812
813 return llcp_sock;
814 }
815
nfc_llcp_sock_get_sn(struct nfc_llcp_local * local,u8 * sn,size_t sn_len)816 static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
817 u8 *sn, size_t sn_len)
818 {
819 struct nfc_llcp_sock *llcp_sock;
820
821 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
822
823 if (llcp_sock == NULL)
824 return NULL;
825
826 sock_hold(&llcp_sock->sk);
827
828 return llcp_sock;
829 }
830
nfc_llcp_connect_sn(struct sk_buff * skb,size_t * sn_len)831 static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
832 {
833 u8 *tlv = &skb->data[2], type, length;
834 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
835
836 while (offset < tlv_array_len) {
837 type = tlv[0];
838 length = tlv[1];
839
840 pr_debug("type 0x%x length %d\n", type, length);
841
842 if (type == LLCP_TLV_SN) {
843 *sn_len = length;
844 return &tlv[2];
845 }
846
847 offset += length + 2;
848 tlv += length + 2;
849 }
850
851 return NULL;
852 }
853
nfc_llcp_recv_ui(struct nfc_llcp_local * local,struct sk_buff * skb)854 static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
855 struct sk_buff *skb)
856 {
857 struct nfc_llcp_sock *llcp_sock;
858 struct nfc_llcp_ui_cb *ui_cb;
859 u8 dsap, ssap;
860
861 dsap = nfc_llcp_dsap(skb);
862 ssap = nfc_llcp_ssap(skb);
863
864 ui_cb = nfc_llcp_ui_skb_cb(skb);
865 ui_cb->dsap = dsap;
866 ui_cb->ssap = ssap;
867
868 pr_debug("%d %d\n", dsap, ssap);
869
870 /* We're looking for a bound socket, not a client one */
871 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
872 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
873 return;
874
875 /* There is no sequence with UI frames */
876 skb_pull(skb, LLCP_HEADER_SIZE);
877 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
878 /*
879 * UI frames will be freed from the socket layer, so we
880 * need to keep them alive until someone receives them.
881 */
882 skb_get(skb);
883 } else {
884 pr_err("Receive queue is full\n");
885 }
886
887 nfc_llcp_sock_put(llcp_sock);
888 }
889
nfc_llcp_recv_connect(struct nfc_llcp_local * local,struct sk_buff * skb)890 static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
891 struct sk_buff *skb)
892 {
893 struct sock *new_sk, *parent;
894 struct nfc_llcp_sock *sock, *new_sock;
895 u8 dsap, ssap, reason;
896
897 dsap = nfc_llcp_dsap(skb);
898 ssap = nfc_llcp_ssap(skb);
899
900 pr_debug("%d %d\n", dsap, ssap);
901
902 if (dsap != LLCP_SAP_SDP) {
903 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
904 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
905 reason = LLCP_DM_NOBOUND;
906 goto fail;
907 }
908 } else {
909 u8 *sn;
910 size_t sn_len;
911
912 sn = nfc_llcp_connect_sn(skb, &sn_len);
913 if (sn == NULL) {
914 reason = LLCP_DM_NOBOUND;
915 goto fail;
916 }
917
918 pr_debug("Service name length %zu\n", sn_len);
919
920 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
921 if (sock == NULL) {
922 reason = LLCP_DM_NOBOUND;
923 goto fail;
924 }
925 }
926
927 lock_sock(&sock->sk);
928
929 parent = &sock->sk;
930
931 if (sk_acceptq_is_full(parent)) {
932 reason = LLCP_DM_REJ;
933 release_sock(&sock->sk);
934 sock_put(&sock->sk);
935 goto fail;
936 }
937
938 if (sock->ssap == LLCP_SDP_UNBOUND) {
939 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
940
941 pr_debug("First client, reserving %d\n", ssap);
942
943 if (ssap == LLCP_SAP_MAX) {
944 reason = LLCP_DM_REJ;
945 release_sock(&sock->sk);
946 sock_put(&sock->sk);
947 goto fail;
948 }
949
950 sock->ssap = ssap;
951 }
952
953 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC, 0);
954 if (new_sk == NULL) {
955 reason = LLCP_DM_REJ;
956 release_sock(&sock->sk);
957 sock_put(&sock->sk);
958 goto fail;
959 }
960
961 new_sock = nfc_llcp_sock(new_sk);
962 new_sock->dev = local->dev;
963 new_sock->local = nfc_llcp_local_get(local);
964 new_sock->rw = sock->rw;
965 new_sock->miux = sock->miux;
966 new_sock->nfc_protocol = sock->nfc_protocol;
967 new_sock->dsap = ssap;
968 new_sock->target_idx = local->target_idx;
969 new_sock->parent = parent;
970 new_sock->ssap = sock->ssap;
971 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
972 atomic_t *client_count;
973
974 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
975
976 client_count =
977 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
978
979 atomic_inc(client_count);
980 new_sock->reserved_ssap = sock->ssap;
981 }
982
983 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
984 skb->len - LLCP_HEADER_SIZE);
985
986 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
987
988 nfc_llcp_sock_link(&local->sockets, new_sk);
989
990 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
991
992 nfc_get_device(local->dev->idx);
993
994 new_sk->sk_state = LLCP_CONNECTED;
995
996 /* Wake the listening processes */
997 parent->sk_data_ready(parent);
998
999 /* Send CC */
1000 nfc_llcp_send_cc(new_sock);
1001
1002 release_sock(&sock->sk);
1003 sock_put(&sock->sk);
1004
1005 return;
1006
1007 fail:
1008 /* Send DM */
1009 nfc_llcp_send_dm(local, dsap, ssap, reason);
1010 }
1011
nfc_llcp_queue_i_frames(struct nfc_llcp_sock * sock)1012 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
1013 {
1014 int nr_frames = 0;
1015 struct nfc_llcp_local *local = sock->local;
1016
1017 pr_debug("Remote ready %d tx queue len %d remote rw %d",
1018 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
1019 sock->remote_rw);
1020
1021 /* Try to queue some I frames for transmission */
1022 while (sock->remote_ready &&
1023 skb_queue_len(&sock->tx_pending_queue) < sock->remote_rw) {
1024 struct sk_buff *pdu;
1025
1026 pdu = skb_dequeue(&sock->tx_queue);
1027 if (pdu == NULL)
1028 break;
1029
1030 /* Update N(S)/N(R) */
1031 nfc_llcp_set_nrns(sock, pdu);
1032
1033 skb_queue_tail(&local->tx_queue, pdu);
1034 nr_frames++;
1035 }
1036
1037 return nr_frames;
1038 }
1039
nfc_llcp_recv_hdlc(struct nfc_llcp_local * local,struct sk_buff * skb)1040 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
1041 struct sk_buff *skb)
1042 {
1043 struct nfc_llcp_sock *llcp_sock;
1044 struct sock *sk;
1045 u8 dsap, ssap, ptype, ns, nr;
1046
1047 ptype = nfc_llcp_ptype(skb);
1048 dsap = nfc_llcp_dsap(skb);
1049 ssap = nfc_llcp_ssap(skb);
1050 ns = nfc_llcp_ns(skb);
1051 nr = nfc_llcp_nr(skb);
1052
1053 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
1054
1055 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1056 if (llcp_sock == NULL) {
1057 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1058 return;
1059 }
1060
1061 sk = &llcp_sock->sk;
1062 lock_sock(sk);
1063 if (sk->sk_state == LLCP_CLOSED) {
1064 release_sock(sk);
1065 nfc_llcp_sock_put(llcp_sock);
1066 }
1067
1068 /* Pass the payload upstream */
1069 if (ptype == LLCP_PDU_I) {
1070 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
1071
1072 if (ns == llcp_sock->recv_n)
1073 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
1074 else
1075 pr_err("Received out of sequence I PDU\n");
1076
1077 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
1078 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
1079 /*
1080 * I frames will be freed from the socket layer, so we
1081 * need to keep them alive until someone receives them.
1082 */
1083 skb_get(skb);
1084 } else {
1085 pr_err("Receive queue is full\n");
1086 }
1087 }
1088
1089 /* Remove skbs from the pending queue */
1090 if (llcp_sock->send_ack_n != nr) {
1091 struct sk_buff *s, *tmp;
1092 u8 n;
1093
1094 llcp_sock->send_ack_n = nr;
1095
1096 /* Remove and free all skbs until ns == nr */
1097 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
1098 n = nfc_llcp_ns(s);
1099
1100 skb_unlink(s, &llcp_sock->tx_pending_queue);
1101 kfree_skb(s);
1102
1103 if (n == nr)
1104 break;
1105 }
1106
1107 /* Re-queue the remaining skbs for transmission */
1108 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
1109 s, tmp) {
1110 skb_unlink(s, &llcp_sock->tx_pending_queue);
1111 skb_queue_head(&local->tx_queue, s);
1112 }
1113 }
1114
1115 if (ptype == LLCP_PDU_RR)
1116 llcp_sock->remote_ready = true;
1117 else if (ptype == LLCP_PDU_RNR)
1118 llcp_sock->remote_ready = false;
1119
1120 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
1121 nfc_llcp_send_rr(llcp_sock);
1122
1123 release_sock(sk);
1124 nfc_llcp_sock_put(llcp_sock);
1125 }
1126
nfc_llcp_recv_disc(struct nfc_llcp_local * local,struct sk_buff * skb)1127 static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
1128 struct sk_buff *skb)
1129 {
1130 struct nfc_llcp_sock *llcp_sock;
1131 struct sock *sk;
1132 u8 dsap, ssap;
1133
1134 dsap = nfc_llcp_dsap(skb);
1135 ssap = nfc_llcp_ssap(skb);
1136
1137 if ((dsap == 0) && (ssap == 0)) {
1138 pr_debug("Connection termination");
1139 nfc_dep_link_down(local->dev);
1140 return;
1141 }
1142
1143 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1144 if (llcp_sock == NULL) {
1145 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1146 return;
1147 }
1148
1149 sk = &llcp_sock->sk;
1150 lock_sock(sk);
1151
1152 nfc_llcp_socket_purge(llcp_sock);
1153
1154 if (sk->sk_state == LLCP_CLOSED) {
1155 release_sock(sk);
1156 nfc_llcp_sock_put(llcp_sock);
1157 }
1158
1159 if (sk->sk_state == LLCP_CONNECTED) {
1160 nfc_put_device(local->dev);
1161 sk->sk_state = LLCP_CLOSED;
1162 sk->sk_state_change(sk);
1163 }
1164
1165 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1166
1167 release_sock(sk);
1168 nfc_llcp_sock_put(llcp_sock);
1169 }
1170
nfc_llcp_recv_cc(struct nfc_llcp_local * local,struct sk_buff * skb)1171 static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
1172 {
1173 struct nfc_llcp_sock *llcp_sock;
1174 struct sock *sk;
1175 u8 dsap, ssap;
1176
1177 dsap = nfc_llcp_dsap(skb);
1178 ssap = nfc_llcp_ssap(skb);
1179
1180 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1181 if (llcp_sock == NULL) {
1182 pr_err("Invalid CC\n");
1183 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1184
1185 return;
1186 }
1187
1188 sk = &llcp_sock->sk;
1189
1190 /* Unlink from connecting and link to the client array */
1191 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1192 nfc_llcp_sock_link(&local->sockets, sk);
1193 llcp_sock->dsap = ssap;
1194
1195 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1196 skb->len - LLCP_HEADER_SIZE);
1197
1198 sk->sk_state = LLCP_CONNECTED;
1199 sk->sk_state_change(sk);
1200
1201 nfc_llcp_sock_put(llcp_sock);
1202 }
1203
nfc_llcp_recv_dm(struct nfc_llcp_local * local,struct sk_buff * skb)1204 static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1205 {
1206 struct nfc_llcp_sock *llcp_sock;
1207 struct sock *sk;
1208 u8 dsap, ssap, reason;
1209
1210 dsap = nfc_llcp_dsap(skb);
1211 ssap = nfc_llcp_ssap(skb);
1212 reason = skb->data[2];
1213
1214 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1215
1216 switch (reason) {
1217 case LLCP_DM_NOBOUND:
1218 case LLCP_DM_REJ:
1219 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1220 break;
1221
1222 default:
1223 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1224 break;
1225 }
1226
1227 if (llcp_sock == NULL) {
1228 pr_debug("Already closed\n");
1229 return;
1230 }
1231
1232 sk = &llcp_sock->sk;
1233
1234 sk->sk_err = ENXIO;
1235 sk->sk_state = LLCP_CLOSED;
1236 sk->sk_state_change(sk);
1237
1238 nfc_llcp_sock_put(llcp_sock);
1239 }
1240
nfc_llcp_recv_snl(struct nfc_llcp_local * local,struct sk_buff * skb)1241 static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1242 struct sk_buff *skb)
1243 {
1244 struct nfc_llcp_sock *llcp_sock;
1245 u8 dsap, ssap, *tlv, type, length, tid, sap;
1246 u16 tlv_len, offset;
1247 char *service_name;
1248 size_t service_name_len;
1249 struct nfc_llcp_sdp_tlv *sdp;
1250 HLIST_HEAD(llc_sdres_list);
1251 size_t sdres_tlvs_len;
1252 HLIST_HEAD(nl_sdres_list);
1253
1254 dsap = nfc_llcp_dsap(skb);
1255 ssap = nfc_llcp_ssap(skb);
1256
1257 pr_debug("%d %d\n", dsap, ssap);
1258
1259 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1260 pr_err("Wrong SNL SAP\n");
1261 return;
1262 }
1263
1264 tlv = &skb->data[LLCP_HEADER_SIZE];
1265 tlv_len = skb->len - LLCP_HEADER_SIZE;
1266 offset = 0;
1267 sdres_tlvs_len = 0;
1268
1269 while (offset < tlv_len) {
1270 type = tlv[0];
1271 length = tlv[1];
1272
1273 switch (type) {
1274 case LLCP_TLV_SDREQ:
1275 tid = tlv[2];
1276 service_name = (char *) &tlv[3];
1277 service_name_len = length - 1;
1278
1279 pr_debug("Looking for %.16s\n", service_name);
1280
1281 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1282 !strncmp(service_name, "urn:nfc:sn:sdp",
1283 service_name_len)) {
1284 sap = 1;
1285 goto add_snl;
1286 }
1287
1288 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1289 service_name_len);
1290 if (!llcp_sock) {
1291 sap = 0;
1292 goto add_snl;
1293 }
1294
1295 /*
1296 * We found a socket but its ssap has not been reserved
1297 * yet. We need to assign it for good and send a reply.
1298 * The ssap will be freed when the socket is closed.
1299 */
1300 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1301 atomic_t *client_count;
1302
1303 sap = nfc_llcp_reserve_sdp_ssap(local);
1304
1305 pr_debug("Reserving %d\n", sap);
1306
1307 if (sap == LLCP_SAP_MAX) {
1308 sap = 0;
1309 goto add_snl;
1310 }
1311
1312 client_count =
1313 &local->local_sdp_cnt[sap -
1314 LLCP_WKS_NUM_SAP];
1315
1316 atomic_inc(client_count);
1317
1318 llcp_sock->ssap = sap;
1319 llcp_sock->reserved_ssap = sap;
1320 } else {
1321 sap = llcp_sock->ssap;
1322 }
1323
1324 pr_debug("%p %d\n", llcp_sock, sap);
1325
1326 add_snl:
1327 sdp = nfc_llcp_build_sdres_tlv(tid, sap);
1328 if (sdp == NULL)
1329 goto exit;
1330
1331 sdres_tlvs_len += sdp->tlv_len;
1332 hlist_add_head(&sdp->node, &llc_sdres_list);
1333 break;
1334
1335 case LLCP_TLV_SDRES:
1336 mutex_lock(&local->sdreq_lock);
1337
1338 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv[2]);
1339
1340 hlist_for_each_entry(sdp, &local->pending_sdreqs, node) {
1341 if (sdp->tid != tlv[2])
1342 continue;
1343
1344 sdp->sap = tlv[3];
1345
1346 pr_debug("Found: uri=%s, sap=%d\n",
1347 sdp->uri, sdp->sap);
1348
1349 hlist_del(&sdp->node);
1350
1351 hlist_add_head(&sdp->node, &nl_sdres_list);
1352
1353 break;
1354 }
1355
1356 mutex_unlock(&local->sdreq_lock);
1357 break;
1358
1359 default:
1360 pr_err("Invalid SNL tlv value 0x%x\n", type);
1361 break;
1362 }
1363
1364 offset += length + 2;
1365 tlv += length + 2;
1366 }
1367
1368 exit:
1369 if (!hlist_empty(&nl_sdres_list))
1370 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
1371
1372 if (!hlist_empty(&llc_sdres_list))
1373 nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);
1374 }
1375
nfc_llcp_recv_agf(struct nfc_llcp_local * local,struct sk_buff * skb)1376 static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb)
1377 {
1378 u8 ptype;
1379 u16 pdu_len;
1380 struct sk_buff *new_skb;
1381
1382 if (skb->len <= LLCP_HEADER_SIZE) {
1383 pr_err("Malformed AGF PDU\n");
1384 return;
1385 }
1386
1387 skb_pull(skb, LLCP_HEADER_SIZE);
1388
1389 while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) {
1390 pdu_len = skb->data[0] << 8 | skb->data[1];
1391
1392 skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE);
1393
1394 if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) {
1395 pr_err("Malformed AGF PDU\n");
1396 return;
1397 }
1398
1399 ptype = nfc_llcp_ptype(skb);
1400
1401 if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF)
1402 goto next;
1403
1404 new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL);
1405 if (new_skb == NULL) {
1406 pr_err("Could not allocate PDU\n");
1407 return;
1408 }
1409
1410 memcpy(skb_put(new_skb, pdu_len), skb->data, pdu_len);
1411
1412 nfc_llcp_rx_skb(local, new_skb);
1413
1414 kfree_skb(new_skb);
1415 next:
1416 skb_pull(skb, pdu_len);
1417 }
1418 }
1419
nfc_llcp_rx_skb(struct nfc_llcp_local * local,struct sk_buff * skb)1420 static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb)
1421 {
1422 u8 dsap, ssap, ptype;
1423
1424 ptype = nfc_llcp_ptype(skb);
1425 dsap = nfc_llcp_dsap(skb);
1426 ssap = nfc_llcp_ssap(skb);
1427
1428 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1429
1430 if (ptype != LLCP_PDU_SYMM)
1431 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1432 16, 1, skb->data, skb->len, true);
1433
1434 switch (ptype) {
1435 case LLCP_PDU_SYMM:
1436 pr_debug("SYMM\n");
1437 break;
1438
1439 case LLCP_PDU_UI:
1440 pr_debug("UI\n");
1441 nfc_llcp_recv_ui(local, skb);
1442 break;
1443
1444 case LLCP_PDU_CONNECT:
1445 pr_debug("CONNECT\n");
1446 nfc_llcp_recv_connect(local, skb);
1447 break;
1448
1449 case LLCP_PDU_DISC:
1450 pr_debug("DISC\n");
1451 nfc_llcp_recv_disc(local, skb);
1452 break;
1453
1454 case LLCP_PDU_CC:
1455 pr_debug("CC\n");
1456 nfc_llcp_recv_cc(local, skb);
1457 break;
1458
1459 case LLCP_PDU_DM:
1460 pr_debug("DM\n");
1461 nfc_llcp_recv_dm(local, skb);
1462 break;
1463
1464 case LLCP_PDU_SNL:
1465 pr_debug("SNL\n");
1466 nfc_llcp_recv_snl(local, skb);
1467 break;
1468
1469 case LLCP_PDU_I:
1470 case LLCP_PDU_RR:
1471 case LLCP_PDU_RNR:
1472 pr_debug("I frame\n");
1473 nfc_llcp_recv_hdlc(local, skb);
1474 break;
1475
1476 case LLCP_PDU_AGF:
1477 pr_debug("AGF frame\n");
1478 nfc_llcp_recv_agf(local, skb);
1479 break;
1480 }
1481 }
1482
nfc_llcp_rx_work(struct work_struct * work)1483 static void nfc_llcp_rx_work(struct work_struct *work)
1484 {
1485 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
1486 rx_work);
1487 struct sk_buff *skb;
1488
1489 skb = local->rx_pending;
1490 if (skb == NULL) {
1491 pr_debug("No pending SKB\n");
1492 return;
1493 }
1494
1495 __net_timestamp(skb);
1496
1497 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX);
1498
1499 nfc_llcp_rx_skb(local, skb);
1500
1501 schedule_work(&local->tx_work);
1502 kfree_skb(local->rx_pending);
1503 local->rx_pending = NULL;
1504 }
1505
__nfc_llcp_recv(struct nfc_llcp_local * local,struct sk_buff * skb)1506 static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
1507 {
1508 local->rx_pending = skb;
1509 del_timer(&local->link_timer);
1510 schedule_work(&local->rx_work);
1511 }
1512
nfc_llcp_recv(void * data,struct sk_buff * skb,int err)1513 void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1514 {
1515 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1516
1517 pr_debug("Received an LLCP PDU\n");
1518 if (err < 0) {
1519 pr_err("err %d\n", err);
1520 return;
1521 }
1522
1523 __nfc_llcp_recv(local, skb);
1524 }
1525
nfc_llcp_data_received(struct nfc_dev * dev,struct sk_buff * skb)1526 int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1527 {
1528 struct nfc_llcp_local *local;
1529
1530 local = nfc_llcp_find_local(dev);
1531 if (local == NULL) {
1532 kfree_skb(skb);
1533 return -ENODEV;
1534 }
1535
1536 __nfc_llcp_recv(local, skb);
1537
1538 return 0;
1539 }
1540
nfc_llcp_mac_is_down(struct nfc_dev * dev)1541 void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1542 {
1543 struct nfc_llcp_local *local;
1544
1545 local = nfc_llcp_find_local(dev);
1546 if (local == NULL)
1547 return;
1548
1549 local->remote_miu = LLCP_DEFAULT_MIU;
1550 local->remote_lto = LLCP_DEFAULT_LTO;
1551
1552 /* Close and purge all existing sockets */
1553 nfc_llcp_socket_release(local, true, 0);
1554 }
1555
nfc_llcp_mac_is_up(struct nfc_dev * dev,u32 target_idx,u8 comm_mode,u8 rf_mode)1556 void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1557 u8 comm_mode, u8 rf_mode)
1558 {
1559 struct nfc_llcp_local *local;
1560
1561 pr_debug("rf mode %d\n", rf_mode);
1562
1563 local = nfc_llcp_find_local(dev);
1564 if (local == NULL)
1565 return;
1566
1567 local->target_idx = target_idx;
1568 local->comm_mode = comm_mode;
1569 local->rf_mode = rf_mode;
1570
1571 if (rf_mode == NFC_RF_INITIATOR) {
1572 pr_debug("Queueing Tx work\n");
1573
1574 schedule_work(&local->tx_work);
1575 } else {
1576 mod_timer(&local->link_timer,
1577 jiffies + msecs_to_jiffies(local->remote_lto));
1578 }
1579 }
1580
nfc_llcp_register_device(struct nfc_dev * ndev)1581 int nfc_llcp_register_device(struct nfc_dev *ndev)
1582 {
1583 struct nfc_llcp_local *local;
1584
1585 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1586 if (local == NULL)
1587 return -ENOMEM;
1588
1589 local->dev = ndev;
1590 INIT_LIST_HEAD(&local->list);
1591 kref_init(&local->ref);
1592 mutex_init(&local->sdp_lock);
1593 init_timer(&local->link_timer);
1594 local->link_timer.data = (unsigned long) local;
1595 local->link_timer.function = nfc_llcp_symm_timer;
1596
1597 skb_queue_head_init(&local->tx_queue);
1598 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
1599
1600 local->rx_pending = NULL;
1601 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
1602
1603 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
1604
1605 rwlock_init(&local->sockets.lock);
1606 rwlock_init(&local->connecting_sockets.lock);
1607 rwlock_init(&local->raw_sockets.lock);
1608
1609 local->lto = 150; /* 1500 ms */
1610 local->rw = LLCP_MAX_RW;
1611 local->miux = cpu_to_be16(LLCP_MAX_MIUX);
1612 local->local_wks = 0x1; /* LLC Link Management */
1613
1614 nfc_llcp_build_gb(local);
1615
1616 local->remote_miu = LLCP_DEFAULT_MIU;
1617 local->remote_lto = LLCP_DEFAULT_LTO;
1618
1619 mutex_init(&local->sdreq_lock);
1620 INIT_HLIST_HEAD(&local->pending_sdreqs);
1621 init_timer(&local->sdreq_timer);
1622 local->sdreq_timer.data = (unsigned long) local;
1623 local->sdreq_timer.function = nfc_llcp_sdreq_timer;
1624 INIT_WORK(&local->sdreq_timeout_work, nfc_llcp_sdreq_timeout_work);
1625
1626 list_add(&local->list, &llcp_devices);
1627
1628 return 0;
1629 }
1630
nfc_llcp_unregister_device(struct nfc_dev * dev)1631 void nfc_llcp_unregister_device(struct nfc_dev *dev)
1632 {
1633 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1634
1635 if (local == NULL) {
1636 pr_debug("No such device\n");
1637 return;
1638 }
1639
1640 local_cleanup(local);
1641
1642 nfc_llcp_local_put(local);
1643 }
1644
nfc_llcp_init(void)1645 int __init nfc_llcp_init(void)
1646 {
1647 return nfc_llcp_sock_init();
1648 }
1649
nfc_llcp_exit(void)1650 void nfc_llcp_exit(void)
1651 {
1652 nfc_llcp_sock_exit();
1653 }
1654