llcp.c 29.9 KB
Newer Older
S
Samuel Ortiz 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Copyright (C) 2011  Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#define pr_fmt(fmt) "llcp: %s: " fmt, __func__

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/nfc.h>

#include "../nfc.h"
#include "llcp.h"

static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};

static struct list_head llcp_devices;

S
Samuel Ortiz 已提交
34
void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
S
Samuel Ortiz 已提交
35
{
S
Samuel Ortiz 已提交
36 37 38 39
	write_lock(&l->lock);
	sk_add_node(sk, &l->head);
	write_unlock(&l->lock);
}
S
Samuel Ortiz 已提交
40

S
Samuel Ortiz 已提交
41 42 43 44 45 46
void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
{
	write_lock(&l->lock);
	sk_del_node_init(sk);
	write_unlock(&l->lock);
}
S
Samuel Ortiz 已提交
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
{
	struct nfc_llcp_local *local = sock->local;
	struct sk_buff *s, *tmp;

	pr_debug("%p\n", &sock->sk);

	skb_queue_purge(&sock->tx_queue);
	skb_queue_purge(&sock->tx_pending_queue);
	skb_queue_purge(&sock->tx_backlog_queue);

	if (local == NULL)
		return;

	/* Search for local pending SKBs that are related to this socket */
	skb_queue_walk_safe(&local->tx_queue, s, tmp) {
		if (s->sk != &sock->sk)
			continue;

		skb_unlink(s, &local->tx_queue);
		kfree_skb(s);
	}
}

72
static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
S
Samuel Ortiz 已提交
73 74 75 76
{
	struct sock *sk;
	struct hlist_node *node, *tmp;
	struct nfc_llcp_sock *llcp_sock;
S
Samuel Ortiz 已提交
77

78 79
	skb_queue_purge(&local->tx_queue);

S
Samuel Ortiz 已提交
80
	write_lock(&local->sockets.lock);
81

S
Samuel Ortiz 已提交
82 83
	sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
		llcp_sock = nfc_llcp_sock(sk);
S
Samuel Ortiz 已提交
84

85
		bh_lock_sock(sk);
S
Samuel Ortiz 已提交
86

87 88
		nfc_llcp_socket_purge(llcp_sock);

S
Samuel Ortiz 已提交
89 90
		if (sk->sk_state == LLCP_CONNECTED)
			nfc_put_device(llcp_sock->dev);
S
Samuel Ortiz 已提交
91

S
Samuel Ortiz 已提交
92
		if (sk->sk_state == LLCP_LISTEN) {
S
Samuel Ortiz 已提交
93 94 95
			struct nfc_llcp_sock *lsk, *n;
			struct sock *accept_sk;

96 97
			list_for_each_entry_safe(lsk, n,
						 &llcp_sock->accept_queue,
S
Samuel Ortiz 已提交
98
						 accept_queue) {
S
Samuel Ortiz 已提交
99
				accept_sk = &lsk->sk;
100
				bh_lock_sock(accept_sk);
S
Samuel Ortiz 已提交
101 102 103 104 105

				nfc_llcp_accept_unlink(accept_sk);

				accept_sk->sk_state = LLCP_CLOSED;

106
				bh_unlock_sock(accept_sk);
S
Samuel Ortiz 已提交
107 108 109

				sock_orphan(accept_sk);
			}
110 111

			if (listen == true) {
112
				bh_unlock_sock(sk);
113 114
				continue;
			}
S
Samuel Ortiz 已提交
115 116
		}

117 118 119 120 121 122 123 124 125 126
		/*
		 * If we have a connection less socket bound, we keep it alive
		 * if the device is still present.
		 */
		if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM &&
		    listen == true) {
			bh_unlock_sock(sk);
			continue;
		}

S
Samuel Ortiz 已提交
127
		sk->sk_state = LLCP_CLOSED;
S
Samuel Ortiz 已提交
128

129
		bh_unlock_sock(sk);
S
Samuel Ortiz 已提交
130

S
Samuel Ortiz 已提交
131
		sock_orphan(sk);
132

S
Samuel Ortiz 已提交
133
		sk_del_node_init(sk);
S
Samuel Ortiz 已提交
134 135
	}

S
Samuel Ortiz 已提交
136
	write_unlock(&local->sockets.lock);
S
Samuel Ortiz 已提交
137 138
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152
struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
{
	kref_get(&local->ref);

	return local;
}

static void local_release(struct kref *ref)
{
	struct nfc_llcp_local *local;

	local = container_of(ref, struct nfc_llcp_local, ref);

	list_del(&local->list);
153
	nfc_llcp_socket_release(local, false);
154 155
	del_timer_sync(&local->link_timer);
	skb_queue_purge(&local->tx_queue);
156 157 158
	cancel_work_sync(&local->tx_work);
	cancel_work_sync(&local->rx_work);
	cancel_work_sync(&local->timeout_work);
159 160 161 162 163 164
	kfree_skb(local->rx_pending);
	kfree(local);
}

int nfc_llcp_local_put(struct nfc_llcp_local *local)
{
S
Samuel Ortiz 已提交
165 166 167
	if (local == NULL)
		return 0;

168 169 170
	return kref_put(&local->ref, local_release);
}

S
Samuel Ortiz 已提交
171 172 173 174 175
static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
					       u8 ssap, u8 dsap)
{
	struct sock *sk;
	struct hlist_node *node;
176
	struct nfc_llcp_sock *llcp_sock, *tmp_sock;
S
Samuel Ortiz 已提交
177 178 179 180 181 182 183 184 185 186 187

	pr_debug("ssap dsap %d %d\n", ssap, dsap);

	if (ssap == 0 && dsap == 0)
		return NULL;

	read_lock(&local->sockets.lock);

	llcp_sock = NULL;

	sk_for_each(sk, node, &local->sockets.head) {
188
		tmp_sock = nfc_llcp_sock(sk);
S
Samuel Ortiz 已提交
189

190 191
		if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
			llcp_sock = tmp_sock;
S
Samuel Ortiz 已提交
192
			break;
193
		}
S
Samuel Ortiz 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	}

	read_unlock(&local->sockets.lock);

	if (llcp_sock == NULL)
		return NULL;

	sock_hold(&llcp_sock->sk);

	return llcp_sock;
}

static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
{
	sock_put(&sock->sk);
}

S
Samuel Ortiz 已提交
211 212 213
static void nfc_llcp_timeout_work(struct work_struct *work)
{
	struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
S
Samuel Ortiz 已提交
214
						    timeout_work);
S
Samuel Ortiz 已提交
215 216 217 218 219 220 221 222 223 224

	nfc_dep_link_down(local->dev);
}

static void nfc_llcp_symm_timer(unsigned long data)
{
	struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;

	pr_err("SYMM timeout\n");

225
	schedule_work(&local->timeout_work);
S
Samuel Ortiz 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
}

struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
{
	struct nfc_llcp_local *local, *n;

	list_for_each_entry_safe(local, n, &llcp_devices, list)
		if (local->dev == dev)
			return local;

	pr_debug("No device found\n");

	return NULL;
}

static char *wks[] = {
	NULL,
	NULL, /* SDP */
	"urn:nfc:sn:ip",
	"urn:nfc:sn:obex",
	"urn:nfc:sn:snep",
};

static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
{
	int sap, num_wks;

	pr_debug("%s\n", service_name);

	if (service_name == NULL)
		return -EINVAL;

	num_wks = ARRAY_SIZE(wks);

S
Samuel Ortiz 已提交
260
	for (sap = 0; sap < num_wks; sap++) {
S
Samuel Ortiz 已提交
261 262 263 264 265 266 267 268 269 270
		if (wks[sap] == NULL)
			continue;

		if (strncmp(wks[sap], service_name, service_name_len) == 0)
			return sap;
	}

	return -EINVAL;
}

S
Samuel Ortiz 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static
struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
					    u8 *sn, size_t sn_len)
{
	struct sock *sk;
	struct hlist_node *node;
	struct nfc_llcp_sock *llcp_sock, *tmp_sock;

	pr_debug("sn %zd %p\n", sn_len, sn);

	if (sn == NULL || sn_len == 0)
		return NULL;

	read_lock(&local->sockets.lock);

	llcp_sock = NULL;

	sk_for_each(sk, node, &local->sockets.head) {
		tmp_sock = nfc_llcp_sock(sk);

		pr_debug("llcp sock %p\n", tmp_sock);

293 294 295 296 297 298
		if (tmp_sock->sk.sk_type == SOCK_STREAM &&
		    tmp_sock->sk.sk_state != LLCP_LISTEN)
			continue;

		if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
		    tmp_sock->sk.sk_state != LLCP_BOUND)
S
Samuel Ortiz 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
			continue;

		if (tmp_sock->service_name == NULL ||
		    tmp_sock->service_name_len == 0)
			continue;

		if (tmp_sock->service_name_len != sn_len)
			continue;

		if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
			llcp_sock = tmp_sock;
			break;
		}
	}

	read_unlock(&local->sockets.lock);

	pr_debug("Found llcp sock %p\n", llcp_sock);

	return llcp_sock;
}

S
Samuel Ortiz 已提交
321
u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
S
Samuel Ortiz 已提交
322
			 struct nfc_llcp_sock *sock)
S
Samuel Ortiz 已提交
323 324 325 326 327
{
	mutex_lock(&local->sdp_lock);

	if (sock->service_name != NULL && sock->service_name_len > 0) {
		int ssap = nfc_llcp_wks_sap(sock->service_name,
S
Samuel Ortiz 已提交
328
					    sock->service_name_len);
S
Samuel Ortiz 已提交
329 330 331 332 333 334 335 336 337 338 339

		if (ssap > 0) {
			pr_debug("WKS %d\n", ssap);

			/* This is a WKS, let's check if it's free */
			if (local->local_wks & BIT(ssap)) {
				mutex_unlock(&local->sdp_lock);

				return LLCP_SAP_MAX;
			}

S
Samuel Ortiz 已提交
340
			set_bit(ssap, &local->local_wks);
S
Samuel Ortiz 已提交
341 342 343 344 345 346
			mutex_unlock(&local->sdp_lock);

			return ssap;
		}

		/*
S
Samuel Ortiz 已提交
347 348
		 * Check if there already is a non WKS socket bound
		 * to this service name.
S
Samuel Ortiz 已提交
349
		 */
S
Samuel Ortiz 已提交
350 351
		if (nfc_llcp_sock_from_sn(local, sock->service_name,
					  sock->service_name_len) != NULL) {
S
Samuel Ortiz 已提交
352 353 354 355 356 357 358
			mutex_unlock(&local->sdp_lock);

			return LLCP_SAP_MAX;
		}

		mutex_unlock(&local->sdp_lock);

S
Samuel Ortiz 已提交
359
		return LLCP_SDP_UNBOUND;
S
Samuel Ortiz 已提交
360

361 362 363 364
	} else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
		if (!test_bit(sock->ssap, &local->local_wks)) {
			set_bit(sock->ssap, &local->local_wks);
			mutex_unlock(&local->sdp_lock);
S
Samuel Ortiz 已提交
365

366
			return sock->ssap;
S
Samuel Ortiz 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
		}
	}

	mutex_unlock(&local->sdp_lock);

	return LLCP_SAP_MAX;
}

u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
{
	u8 local_ssap;

	mutex_lock(&local->sdp_lock);

	local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
	if (local_ssap == LLCP_LOCAL_NUM_SAP) {
		mutex_unlock(&local->sdp_lock);
		return LLCP_SAP_MAX;
	}

S
Samuel Ortiz 已提交
387
	set_bit(local_ssap, &local->local_sap);
S
Samuel Ortiz 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

	mutex_unlock(&local->sdp_lock);

	return local_ssap + LLCP_LOCAL_SAP_OFFSET;
}

void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
{
	u8 local_ssap;
	unsigned long *sdp;

	if (ssap < LLCP_WKS_NUM_SAP) {
		local_ssap = ssap;
		sdp = &local->local_wks;
	} else if (ssap < LLCP_LOCAL_NUM_SAP) {
S
Samuel Ortiz 已提交
403 404
		atomic_t *client_cnt;

S
Samuel Ortiz 已提交
405 406
		local_ssap = ssap - LLCP_WKS_NUM_SAP;
		sdp = &local->local_sdp;
S
Samuel Ortiz 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
		client_cnt = &local->local_sdp_cnt[local_ssap];

		pr_debug("%d clients\n", atomic_read(client_cnt));

		mutex_lock(&local->sdp_lock);

		if (atomic_dec_and_test(client_cnt)) {
			struct nfc_llcp_sock *l_sock;

			pr_debug("No more clients for SAP %d\n", ssap);

			clear_bit(local_ssap, sdp);

			/* Find the listening sock and set it back to UNBOUND */
			l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
			if (l_sock) {
				l_sock->ssap = LLCP_SDP_UNBOUND;
				nfc_llcp_sock_put(l_sock);
			}
		}

		mutex_unlock(&local->sdp_lock);

		return;
S
Samuel Ortiz 已提交
431 432 433 434 435 436 437 438 439
	} else if (ssap < LLCP_MAX_SAP) {
		local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
		sdp = &local->local_sap;
	} else {
		return;
	}

	mutex_lock(&local->sdp_lock);

S
Samuel Ortiz 已提交
440
	clear_bit(local_ssap, sdp);
S
Samuel Ortiz 已提交
441 442 443 444

	mutex_unlock(&local->sdp_lock);
}

S
Samuel Ortiz 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
{
	u8 ssap;

	mutex_lock(&local->sdp_lock);

	ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
	if (ssap == LLCP_SDP_NUM_SAP) {
		mutex_unlock(&local->sdp_lock);

		return LLCP_SAP_MAX;
	}

	pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);

	set_bit(ssap, &local->local_sdp);

	mutex_unlock(&local->sdp_lock);

	return LLCP_WKS_NUM_SAP + ssap;
}

S
Samuel Ortiz 已提交
467 468 469
static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
{
	u8 *gb_cur, *version_tlv, version, version_length;
470
	u8 *lto_tlv, lto_length;
S
Samuel Ortiz 已提交
471
	u8 *wks_tlv, wks_length;
472
	u8 *miux_tlv, miux_length;
S
Samuel Ortiz 已提交
473
	u8 gb_len = 0;
W
Wei Yongjun 已提交
474
	int ret = 0;
S
Samuel Ortiz 已提交
475 476 477

	version = LLCP_VERSION_11;
	version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
S
Samuel Ortiz 已提交
478
					 1, &version_length);
S
Samuel Ortiz 已提交
479 480
	gb_len += version_length;

481
	lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
S
Samuel Ortiz 已提交
482 483 484 485
	gb_len += lto_length;

	pr_debug("Local wks 0x%lx\n", local->local_wks);
	wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
S
Samuel Ortiz 已提交
486
				     &wks_length);
S
Samuel Ortiz 已提交
487 488
	gb_len += wks_length;

489
	miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
490 491 492
				      &miux_length);
	gb_len += miux_length;

S
Samuel Ortiz 已提交
493 494 495
	gb_len += ARRAY_SIZE(llcp_magic);

	if (gb_len > NFC_MAX_GT_LEN) {
W
Wei Yongjun 已提交
496 497
		ret = -EINVAL;
		goto out;
S
Samuel Ortiz 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	}

	gb_cur = local->gb;

	memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
	gb_cur += ARRAY_SIZE(llcp_magic);

	memcpy(gb_cur, version_tlv, version_length);
	gb_cur += version_length;

	memcpy(gb_cur, lto_tlv, lto_length);
	gb_cur += lto_length;

	memcpy(gb_cur, wks_tlv, wks_length);
	gb_cur += wks_length;

514 515 516
	memcpy(gb_cur, miux_tlv, miux_length);
	gb_cur += miux_length;

W
Wei Yongjun 已提交
517 518 519
	local->gb_len = gb_len;

out:
S
Samuel Ortiz 已提交
520 521
	kfree(version_tlv);
	kfree(lto_tlv);
W
Wei Yongjun 已提交
522 523
	kfree(wks_tlv);
	kfree(miux_tlv);
S
Samuel Ortiz 已提交
524

W
Wei Yongjun 已提交
525
	return ret;
S
Samuel Ortiz 已提交
526 527
}

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
{
	struct nfc_llcp_local *local;

	local = nfc_llcp_find_local(dev);
	if (local == NULL) {
		*general_bytes_len = 0;
		return NULL;
	}

	nfc_llcp_build_gb(local);

	*general_bytes_len = local->gb_len;

	return local->gb;
}

S
Samuel Ortiz 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557
int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
{
	struct nfc_llcp_local *local = nfc_llcp_find_local(dev);

	if (local == NULL) {
		pr_err("No LLCP device\n");
		return -ENODEV;
	}

	memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
	memcpy(local->remote_gb, gb, gb_len);
	local->remote_gb_len = gb_len;

S
Samuel Ortiz 已提交
558
	if (local->remote_gb == NULL || local->remote_gb_len == 0)
S
Samuel Ortiz 已提交
559 560 561 562 563 564 565
		return -ENODEV;

	if (memcmp(local->remote_gb, llcp_magic, 3)) {
		pr_err("MAC does not support LLCP\n");
		return -EINVAL;
	}

566 567 568
	return nfc_llcp_parse_gb_tlv(local,
				     &local->remote_gb[3],
				     local->remote_gb_len - 3);
S
Samuel Ortiz 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
}

static u8 nfc_llcp_dsap(struct sk_buff *pdu)
{
	return (pdu->data[0] & 0xfc) >> 2;
}

static u8 nfc_llcp_ptype(struct sk_buff *pdu)
{
	return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
}

static u8 nfc_llcp_ssap(struct sk_buff *pdu)
{
	return pdu->data[1] & 0x3f;
}

static u8 nfc_llcp_ns(struct sk_buff *pdu)
{
	return pdu->data[2] >> 4;
}

static u8 nfc_llcp_nr(struct sk_buff *pdu)
{
	return pdu->data[2] & 0xf;
}

static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
{
598
	pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
S
Samuel Ortiz 已提交
599 600 601 602
	sock->send_n = (sock->send_n + 1) % 16;
	sock->recv_ack_n = (sock->recv_n - 1) % 16;
}

T
Thierry Escande 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
			       struct sk_buff *skb, u8 direction)
{
	struct hlist_node *node;
	struct sk_buff *skb_copy = NULL, *nskb;
	struct sock *sk;
	u8 *data;

	read_lock(&local->raw_sockets.lock);

	sk_for_each(sk, node, &local->raw_sockets.head) {
		if (sk->sk_state != LLCP_BOUND)
			continue;

		if (skb_copy == NULL) {
			skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
					       GFP_ATOMIC);

			if (skb_copy == NULL)
				continue;

			data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);

			data[0] = local->dev ? local->dev->idx : 0xFF;
			data[1] = direction;
		}

		nskb = skb_clone(skb_copy, GFP_ATOMIC);
		if (!nskb)
			continue;

		if (sock_queue_rcv_skb(sk, nskb))
			kfree_skb(nskb);
	}

	read_unlock(&local->raw_sockets.lock);

	kfree_skb(skb_copy);
}

S
Samuel Ortiz 已提交
643 644 645 646 647 648 649 650 651 652 653 654
static void nfc_llcp_tx_work(struct work_struct *work)
{
	struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
						    tx_work);
	struct sk_buff *skb;
	struct sock *sk;
	struct nfc_llcp_sock *llcp_sock;

	skb = skb_dequeue(&local->tx_queue);
	if (skb != NULL) {
		sk = skb->sk;
		llcp_sock = nfc_llcp_sock(sk);
655 656 657 658

		if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
			nfc_llcp_send_symm(local->dev);
		} else {
659 660
			struct sk_buff *copy_skb = NULL;
			u8 ptype = nfc_llcp_ptype(skb);
S
Samuel Ortiz 已提交
661 662 663 664 665 666 667
			int ret;

			pr_debug("Sending pending skb\n");
			print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
				       DUMP_PREFIX_OFFSET, 16, 1,
				       skb->data, skb->len, true);

668 669 670
			if (ptype == LLCP_PDU_I)
				copy_skb = skb_copy(skb, GFP_ATOMIC);

T
Thierry Escande 已提交
671 672 673
			nfc_llcp_send_to_raw_sock(local, skb,
						  NFC_LLCP_DIRECTION_TX);

S
Samuel Ortiz 已提交
674 675 676
			ret = nfc_data_exchange(local->dev, local->target_idx,
						skb, nfc_llcp_recv, local);

677 678 679
			if (ret) {
				kfree_skb(copy_skb);
				goto out;
S
Samuel Ortiz 已提交
680
			}
681 682 683 684

			if (ptype == LLCP_PDU_I && copy_skb)
				skb_queue_tail(&llcp_sock->tx_pending_queue,
					       copy_skb);
S
Samuel Ortiz 已提交
685 686 687 688 689
		}
	} else {
		nfc_llcp_send_symm(local->dev);
	}

690
out:
S
Samuel Ortiz 已提交
691 692 693 694
	mod_timer(&local->link_timer,
		  jiffies + msecs_to_jiffies(2 * local->remote_lto));
}

S
Samuel Ortiz 已提交
695 696 697 698 699 700 701 702 703 704 705 706
static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
							  u8 ssap)
{
	struct sock *sk;
	struct nfc_llcp_sock *llcp_sock;
	struct hlist_node *node;

	read_lock(&local->connecting_sockets.lock);

	sk_for_each(sk, node, &local->connecting_sockets.head) {
		llcp_sock = nfc_llcp_sock(sk);

707 708
		if (llcp_sock->ssap == ssap) {
			sock_hold(&llcp_sock->sk);
S
Samuel Ortiz 已提交
709
			goto out;
710
		}
S
Samuel Ortiz 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	}

	llcp_sock = NULL;

out:
	read_unlock(&local->connecting_sockets.lock);

	return llcp_sock;
}

static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
						  u8 *sn, size_t sn_len)
{
	struct nfc_llcp_sock *llcp_sock;

S
Samuel Ortiz 已提交
726
	llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
S
Samuel Ortiz 已提交
727

S
Samuel Ortiz 已提交
728 729
	if (llcp_sock == NULL)
		return NULL;
S
Samuel Ortiz 已提交
730

S
Samuel Ortiz 已提交
731 732 733
	sock_hold(&llcp_sock->sk);

	return llcp_sock;
S
Samuel Ortiz 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
}

static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
{
	u8 *tlv = &skb->data[2], type, length;
	size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;

	while (offset < tlv_array_len) {
		type = tlv[0];
		length = tlv[1];

		pr_debug("type 0x%x length %d\n", type, length);

		if (type == LLCP_TLV_SN) {
			*sn_len = length;
			return &tlv[2];
		}

		offset += length + 2;
		tlv += length + 2;
	}

	return NULL;
}

S
Samuel Ortiz 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
			     struct sk_buff *skb)
{
	struct nfc_llcp_sock *llcp_sock;
	struct nfc_llcp_ui_cb *ui_cb;
	u8 dsap, ssap;

	dsap = nfc_llcp_dsap(skb);
	ssap = nfc_llcp_ssap(skb);

	ui_cb = nfc_llcp_ui_skb_cb(skb);
	ui_cb->dsap = dsap;
	ui_cb->ssap = ssap;

	printk("%s %d %d\n", __func__, dsap, ssap);

	pr_debug("%d %d\n", dsap, ssap);

	/* We're looking for a bound socket, not a client one */
	llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
	if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
		return;

	/* There is no sequence with UI frames */
	skb_pull(skb, LLCP_HEADER_SIZE);
	if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
		pr_err("receive queue is full\n");
		skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
	}

	nfc_llcp_sock_put(llcp_sock);
}

S
Samuel Ortiz 已提交
792
static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
S
Samuel Ortiz 已提交
793
				  struct sk_buff *skb)
S
Samuel Ortiz 已提交
794 795 796
{
	struct sock *new_sk, *parent;
	struct nfc_llcp_sock *sock, *new_sock;
S
Samuel Ortiz 已提交
797
	u8 dsap, ssap, reason;
S
Samuel Ortiz 已提交
798 799 800 801 802 803 804

	dsap = nfc_llcp_dsap(skb);
	ssap = nfc_llcp_ssap(skb);

	pr_debug("%d %d\n", dsap, ssap);

	if (dsap != LLCP_SAP_SDP) {
S
Samuel Ortiz 已提交
805 806
		sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
		if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
S
Samuel Ortiz 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
			reason = LLCP_DM_NOBOUND;
			goto fail;
		}
	} else {
		u8 *sn;
		size_t sn_len;

		sn = nfc_llcp_connect_sn(skb, &sn_len);
		if (sn == NULL) {
			reason = LLCP_DM_NOBOUND;
			goto fail;
		}

		pr_debug("Service name length %zu\n", sn_len);

S
Samuel Ortiz 已提交
822 823 824 825
		sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
		if (sock == NULL) {
			reason = LLCP_DM_NOBOUND;
			goto fail;
S
Samuel Ortiz 已提交
826 827 828
		}
	}

S
Samuel Ortiz 已提交
829
	lock_sock(&sock->sk);
S
Samuel Ortiz 已提交
830 831 832 833 834 835 836 837 838 839

	parent = &sock->sk;

	if (sk_acceptq_is_full(parent)) {
		reason = LLCP_DM_REJ;
		release_sock(&sock->sk);
		sock_put(&sock->sk);
		goto fail;
	}

S
Samuel Ortiz 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	if (sock->ssap == LLCP_SDP_UNBOUND) {
		u8 ssap = nfc_llcp_reserve_sdp_ssap(local);

		pr_debug("First client, reserving %d\n", ssap);

		if (ssap == LLCP_SAP_MAX) {
			reason = LLCP_DM_REJ;
			release_sock(&sock->sk);
			sock_put(&sock->sk);
			goto fail;
		}

		sock->ssap = ssap;
	}

S
Samuel Ortiz 已提交
855
	new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
S
Samuel Ortiz 已提交
856 857 858 859 860 861 862 863 864
	if (new_sk == NULL) {
		reason = LLCP_DM_REJ;
		release_sock(&sock->sk);
		sock_put(&sock->sk);
		goto fail;
	}

	new_sock = nfc_llcp_sock(new_sk);
	new_sock->dev = local->dev;
865
	new_sock->local = nfc_llcp_local_get(local);
866
	new_sock->miu = local->remote_miu;
S
Samuel Ortiz 已提交
867 868
	new_sock->nfc_protocol = sock->nfc_protocol;
	new_sock->dsap = ssap;
869
	new_sock->target_idx = local->target_idx;
S
Samuel Ortiz 已提交
870
	new_sock->parent = parent;
S
Samuel Ortiz 已提交
871 872 873 874 875 876 877 878 879 880 881 882
	new_sock->ssap = sock->ssap;
	if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
		atomic_t *client_count;

		pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);

		client_count =
			&local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];

		atomic_inc(client_count);
		new_sock->reserved_ssap = sock->ssap;
	}
S
Samuel Ortiz 已提交
883

884 885 886
	nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
				      skb->len - LLCP_HEADER_SIZE);

S
Samuel Ortiz 已提交
887 888
	pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);

S
Samuel Ortiz 已提交
889
	nfc_llcp_sock_link(&local->sockets, new_sk);
S
Samuel Ortiz 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912

	nfc_llcp_accept_enqueue(&sock->sk, new_sk);

	nfc_get_device(local->dev->idx);

	new_sk->sk_state = LLCP_CONNECTED;

	/* Wake the listening processes */
	parent->sk_data_ready(parent, 0);

	/* Send CC */
	nfc_llcp_send_cc(new_sock);

	release_sock(&sock->sk);
	sock_put(&sock->sk);

	return;

fail:
	/* Send DM */
	nfc_llcp_send_dm(local, dsap, ssap, reason);
}

913
int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
914
{
915
	int nr_frames = 0;
916 917 918
	struct nfc_llcp_local *local = sock->local;

	pr_debug("Remote ready %d tx queue len %d remote rw %d",
S
Samuel Ortiz 已提交
919
		 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
920
		 sock->rw);
921 922 923

	/* Try to queue some I frames for transmission */
	while (sock->remote_ready &&
924
	       skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
S
Samuel Ortiz 已提交
925
		struct sk_buff *pdu;
926 927 928 929 930 931 932 933 934

		pdu = skb_dequeue(&sock->tx_queue);
		if (pdu == NULL)
			break;

		/* Update N(S)/N(R) */
		nfc_llcp_set_nrns(sock, pdu);

		skb_queue_tail(&local->tx_queue, pdu);
935
		nr_frames++;
936
	}
937 938

	return nr_frames;
939 940
}

S
Samuel Ortiz 已提交
941
static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
S
Samuel Ortiz 已提交
942
			       struct sk_buff *skb)
S
Samuel Ortiz 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
{
	struct nfc_llcp_sock *llcp_sock;
	struct sock *sk;
	u8 dsap, ssap, ptype, ns, nr;

	ptype = nfc_llcp_ptype(skb);
	dsap = nfc_llcp_dsap(skb);
	ssap = nfc_llcp_ssap(skb);
	ns = nfc_llcp_ns(skb);
	nr = nfc_llcp_nr(skb);

	pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);

	llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
	if (llcp_sock == NULL) {
		nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
		return;
	}

	sk = &llcp_sock->sk;
	lock_sock(sk);
	if (sk->sk_state == LLCP_CLOSED) {
		release_sock(sk);
		nfc_llcp_sock_put(llcp_sock);
	}

	/* Pass the payload upstream */
	if (ptype == LLCP_PDU_I) {
		pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);

973 974 975 976 977
		if (ns == llcp_sock->recv_n)
			llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
		else
			pr_err("Received out of sequence I PDU\n");

S
Samuel Ortiz 已提交
978 979 980 981 982 983 984 985 986 987
		skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
		if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
			pr_err("receive queue is full\n");
			skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
		}
	}

	/* Remove skbs from the pending queue */
	if (llcp_sock->send_ack_n != nr) {
		struct sk_buff *s, *tmp;
988
		u8 n;
S
Samuel Ortiz 已提交
989 990 991

		llcp_sock->send_ack_n = nr;

S
Samuel Ortiz 已提交
992 993
		/* Remove and free all skbs until ns == nr */
		skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
994 995
			n = nfc_llcp_ns(s);

S
Samuel Ortiz 已提交
996 997 998
			skb_unlink(s, &llcp_sock->tx_pending_queue);
			kfree_skb(s);

999
			if (n == nr)
S
Samuel Ortiz 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008
				break;
		}

		/* Re-queue the remaining skbs for transmission */
		skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
					    s, tmp) {
			skb_unlink(s, &llcp_sock->tx_pending_queue);
			skb_queue_head(&local->tx_queue, s);
		}
S
Samuel Ortiz 已提交
1009 1010
	}

1011 1012
	if (ptype == LLCP_PDU_RR)
		llcp_sock->remote_ready = true;
S
Samuel Ortiz 已提交
1013
	else if (ptype == LLCP_PDU_RNR)
1014 1015
		llcp_sock->remote_ready = false;

1016
	if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
1017
		nfc_llcp_send_rr(llcp_sock);
S
Samuel Ortiz 已提交
1018 1019 1020 1021 1022 1023

	release_sock(sk);
	nfc_llcp_sock_put(llcp_sock);
}

static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
S
Samuel Ortiz 已提交
1024
			       struct sk_buff *skb)
S
Samuel Ortiz 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
{
	struct nfc_llcp_sock *llcp_sock;
	struct sock *sk;
	u8 dsap, ssap;

	dsap = nfc_llcp_dsap(skb);
	ssap = nfc_llcp_ssap(skb);

	llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
	if (llcp_sock == NULL) {
		nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
		return;
	}

	sk = &llcp_sock->sk;
	lock_sock(sk);
1041 1042 1043

	nfc_llcp_socket_purge(llcp_sock);

S
Samuel Ortiz 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
	if (sk->sk_state == LLCP_CLOSED) {
		release_sock(sk);
		nfc_llcp_sock_put(llcp_sock);
	}

	if (sk->sk_state == LLCP_CONNECTED) {
		nfc_put_device(local->dev);
		sk->sk_state = LLCP_CLOSED;
		sk->sk_state_change(sk);
	}

	nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);

	release_sock(sk);
	nfc_llcp_sock_put(llcp_sock);
}

S
Samuel Ortiz 已提交
1061
static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
S
Samuel Ortiz 已提交
1062 1063
{
	struct nfc_llcp_sock *llcp_sock;
1064
	struct sock *sk;
S
Samuel Ortiz 已提交
1065 1066 1067 1068 1069
	u8 dsap, ssap;

	dsap = nfc_llcp_dsap(skb);
	ssap = nfc_llcp_ssap(skb);

S
Samuel Ortiz 已提交
1070
	llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
S
Samuel Ortiz 已提交
1071 1072 1073 1074 1075 1076 1077
	if (llcp_sock == NULL) {
		pr_err("Invalid CC\n");
		nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);

		return;
	}

1078
	sk = &llcp_sock->sk;
S
Samuel Ortiz 已提交
1079

S
Samuel Ortiz 已提交
1080 1081 1082 1083 1084
	/* Unlink from connecting and link to the client array */
	nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
	nfc_llcp_sock_link(&local->sockets, sk);
	llcp_sock->dsap = ssap;

1085 1086
	nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
				      skb->len - LLCP_HEADER_SIZE);
S
Samuel Ortiz 已提交
1087

1088 1089 1090
	sk->sk_state = LLCP_CONNECTED;
	sk->sk_state_change(sk);

S
Samuel Ortiz 已提交
1091 1092 1093
	nfc_llcp_sock_put(llcp_sock);
}

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
{
	struct nfc_llcp_sock *llcp_sock;
	struct sock *sk;
	u8 dsap, ssap, reason;

	dsap = nfc_llcp_dsap(skb);
	ssap = nfc_llcp_ssap(skb);
	reason = skb->data[2];

	pr_debug("%d %d reason %d\n", ssap, dsap, reason);

	switch (reason) {
	case LLCP_DM_NOBOUND:
	case LLCP_DM_REJ:
		llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
		break;

	default:
		llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
		break;
	}

	if (llcp_sock == NULL) {
1118
		pr_debug("Already closed\n");
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
		return;
	}

	sk = &llcp_sock->sk;

	sk->sk_err = ENXIO;
	sk->sk_state = LLCP_CLOSED;
	sk->sk_state_change(sk);

	nfc_llcp_sock_put(llcp_sock);
}

S
Samuel Ortiz 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
			      struct sk_buff *skb)
{
	struct nfc_llcp_sock *llcp_sock;
	u8 dsap, ssap, *tlv, type, length, tid, sap;
	u16 tlv_len, offset;
	char *service_name;
	size_t service_name_len;

	dsap = nfc_llcp_dsap(skb);
	ssap = nfc_llcp_ssap(skb);

	pr_debug("%d %d\n", dsap, ssap);

	if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
		pr_err("Wrong SNL SAP\n");
		return;
	}

	tlv = &skb->data[LLCP_HEADER_SIZE];
	tlv_len = skb->len - LLCP_HEADER_SIZE;
	offset = 0;

1154
	while (offset < tlv_len) {
S
Samuel Ortiz 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163
		type = tlv[0];
		length = tlv[1];

		switch (type) {
		case LLCP_TLV_SDREQ:
			tid = tlv[2];
			service_name = (char *) &tlv[3];
			service_name_len = length - 1;

1164
			pr_debug("Looking for %.16s\n", service_name);
S
Samuel Ortiz 已提交
1165 1166 1167 1168 1169

			if (service_name_len == strlen("urn:nfc:sn:sdp") &&
			    !strncmp(service_name, "urn:nfc:sn:sdp",
				     service_name_len)) {
				sap = 1;
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
				goto send_snl;
			}

			llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
							  service_name_len);
			if (!llcp_sock) {
				sap = 0;
				goto send_snl;
			}

			/*
			 * We found a socket but its ssap has not been reserved
			 * yet. We need to assign it for good and send a reply.
			 * The ssap will be freed when the socket is closed.
			 */
			if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
				atomic_t *client_count;

				sap = nfc_llcp_reserve_sdp_ssap(local);

				pr_debug("Reserving %d\n", sap);

				if (sap == LLCP_SAP_MAX) {
					sap = 0;
					goto send_snl;
				}

				client_count =
					&local->local_sdp_cnt[sap -
							      LLCP_WKS_NUM_SAP];

				atomic_inc(client_count);

				llcp_sock->ssap = sap;
				llcp_sock->reserved_ssap = sap;
S
Samuel Ortiz 已提交
1205
			} else {
1206
				sap = llcp_sock->ssap;
S
Samuel Ortiz 已提交
1207 1208
			}

1209 1210
			pr_debug("%p %d\n", llcp_sock, sap);

1211
send_snl:
S
Samuel Ortiz 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
			nfc_llcp_send_snl(local, tid, sap);
			break;

		default:
			pr_err("Invalid SNL tlv value 0x%x\n", type);
			break;
		}

		offset += length + 2;
		tlv += length + 2;
	}
}

S
Samuel Ortiz 已提交
1225 1226 1227
static void nfc_llcp_rx_work(struct work_struct *work)
{
	struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
S
Samuel Ortiz 已提交
1228
						    rx_work);
S
Samuel Ortiz 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	u8 dsap, ssap, ptype;
	struct sk_buff *skb;

	skb = local->rx_pending;
	if (skb == NULL) {
		pr_debug("No pending SKB\n");
		return;
	}

	ptype = nfc_llcp_ptype(skb);
	dsap = nfc_llcp_dsap(skb);
	ssap = nfc_llcp_ssap(skb);

	pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);

S
Samuel Ortiz 已提交
1244 1245 1246 1247
	if (ptype != LLCP_PDU_SYMM)
		print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
			       16, 1, skb->data, skb->len, true);

T
Thierry Escande 已提交
1248 1249
	nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);

S
Samuel Ortiz 已提交
1250 1251 1252 1253 1254
	switch (ptype) {
	case LLCP_PDU_SYMM:
		pr_debug("SYMM\n");
		break;

S
Samuel Ortiz 已提交
1255 1256 1257 1258 1259
	case LLCP_PDU_UI:
		pr_debug("UI\n");
		nfc_llcp_recv_ui(local, skb);
		break;

S
Samuel Ortiz 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
	case LLCP_PDU_CONNECT:
		pr_debug("CONNECT\n");
		nfc_llcp_recv_connect(local, skb);
		break;

	case LLCP_PDU_DISC:
		pr_debug("DISC\n");
		nfc_llcp_recv_disc(local, skb);
		break;

	case LLCP_PDU_CC:
		pr_debug("CC\n");
		nfc_llcp_recv_cc(local, skb);
		break;

1275 1276 1277 1278 1279
	case LLCP_PDU_DM:
		pr_debug("DM\n");
		nfc_llcp_recv_dm(local, skb);
		break;

S
Samuel Ortiz 已提交
1280 1281 1282 1283 1284
	case LLCP_PDU_SNL:
		pr_debug("SNL\n");
		nfc_llcp_recv_snl(local, skb);
		break;

S
Samuel Ortiz 已提交
1285 1286
	case LLCP_PDU_I:
	case LLCP_PDU_RR:
1287
	case LLCP_PDU_RNR:
S
Samuel Ortiz 已提交
1288 1289 1290 1291 1292 1293
		pr_debug("I frame\n");
		nfc_llcp_recv_hdlc(local, skb);
		break;

	}

1294
	schedule_work(&local->tx_work);
S
Samuel Ortiz 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	kfree_skb(local->rx_pending);
	local->rx_pending = NULL;
}

void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
{
	struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;

	pr_debug("Received an LLCP PDU\n");
	if (err < 0) {
S
Samuel Ortiz 已提交
1305
		pr_err("err %d\n", err);
S
Samuel Ortiz 已提交
1306 1307 1308 1309 1310
		return;
	}

	local->rx_pending = skb_get(skb);
	del_timer(&local->link_timer);
1311
	schedule_work(&local->rx_work);
S
Samuel Ortiz 已提交
1312 1313
}

1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
{
	struct nfc_llcp_local *local;

	local = nfc_llcp_find_local(dev);
	if (local == NULL)
		return -ENODEV;

	local->rx_pending = skb_get(skb);
	del_timer(&local->link_timer);
1324
	schedule_work(&local->rx_work);
1325 1326 1327 1328

	return 0;
}

S
Samuel Ortiz 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337
void nfc_llcp_mac_is_down(struct nfc_dev *dev)
{
	struct nfc_llcp_local *local;

	local = nfc_llcp_find_local(dev);
	if (local == NULL)
		return;

	/* Close and purge all existing sockets */
1338
	nfc_llcp_socket_release(local, true);
S
Samuel Ortiz 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
}

void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
			u8 comm_mode, u8 rf_mode)
{
	struct nfc_llcp_local *local;

	pr_debug("rf mode %d\n", rf_mode);

	local = nfc_llcp_find_local(dev);
	if (local == NULL)
		return;

	local->target_idx = target_idx;
	local->comm_mode = comm_mode;
	local->rf_mode = rf_mode;

	if (rf_mode == NFC_RF_INITIATOR) {
		pr_debug("Queueing Tx work\n");

1359
		schedule_work(&local->tx_work);
S
Samuel Ortiz 已提交
1360 1361
	} else {
		mod_timer(&local->link_timer,
S
Samuel Ortiz 已提交
1362
			  jiffies + msecs_to_jiffies(local->remote_lto));
S
Samuel Ortiz 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
	}
}

int nfc_llcp_register_device(struct nfc_dev *ndev)
{
	struct nfc_llcp_local *local;

	local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
	if (local == NULL)
		return -ENOMEM;

	local->dev = ndev;
	INIT_LIST_HEAD(&local->list);
1376
	kref_init(&local->ref);
S
Samuel Ortiz 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
	mutex_init(&local->sdp_lock);
	init_timer(&local->link_timer);
	local->link_timer.data = (unsigned long) local;
	local->link_timer.function = nfc_llcp_symm_timer;

	skb_queue_head_init(&local->tx_queue);
	INIT_WORK(&local->tx_work, nfc_llcp_tx_work);

	local->rx_pending = NULL;
	INIT_WORK(&local->rx_work, nfc_llcp_rx_work);

	INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);

1390 1391
	rwlock_init(&local->sockets.lock);
	rwlock_init(&local->connecting_sockets.lock);
T
Thierry Escande 已提交
1392
	rwlock_init(&local->raw_sockets.lock);
S
Samuel Ortiz 已提交
1393

1394 1395 1396 1397
	local->lto = 150; /* 1500 ms */
	local->rw = LLCP_MAX_RW;
	local->miux = cpu_to_be16(LLCP_MAX_MIUX);

S
Samuel Ortiz 已提交
1398 1399 1400 1401 1402
	nfc_llcp_build_gb(local);

	local->remote_miu = LLCP_DEFAULT_MIU;
	local->remote_lto = LLCP_DEFAULT_LTO;

1403
	list_add(&local->list, &llcp_devices);
S
Samuel Ortiz 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416

	return 0;
}

void nfc_llcp_unregister_device(struct nfc_dev *dev)
{
	struct nfc_llcp_local *local = nfc_llcp_find_local(dev);

	if (local == NULL) {
		pr_debug("No such device\n");
		return;
	}

1417
	nfc_llcp_local_put(local);
S
Samuel Ortiz 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
}

int __init nfc_llcp_init(void)
{
	INIT_LIST_HEAD(&llcp_devices);

	return nfc_llcp_sock_init();
}

void nfc_llcp_exit(void)
{
	nfc_llcp_sock_exit();
}