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