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