aboutsummaryrefslogtreecommitdiffstats
path: root/net/nfc/llcp
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-12-12 21:07:07 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2012-12-12 21:07:07 -0500
commit6be35c700f742e911ecedd07fcc43d4439922334 (patch)
treeca9f37214d204465fcc2d79c82efd291e357c53c /net/nfc/llcp
parente37aa63e87bd581f9be5555ed0ba83f5295c92fc (diff)
parent520dfe3a3645257bf83660f672c47f8558f3d4c4 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
Pull networking changes from David Miller: 1) Allow to dump, monitor, and change the bridge multicast database using netlink. From Cong Wang. 2) RFC 5961 TCP blind data injection attack mitigation, from Eric Dumazet. 3) Networking user namespace support from Eric W. Biederman. 4) tuntap/virtio-net multiqueue support by Jason Wang. 5) Support for checksum offload of encapsulated packets (basically, tunneled traffic can still be checksummed by HW). From Joseph Gasparakis. 6) Allow BPF filter access to VLAN tags, from Eric Dumazet and Daniel Borkmann. 7) Bridge port parameters over netlink and BPDU blocking support from Stephen Hemminger. 8) Improve data access patterns during inet socket demux by rearranging socket layout, from Eric Dumazet. 9) TIPC protocol updates and cleanups from Ying Xue, Paul Gortmaker, and Jon Maloy. 10) Update TCP socket hash sizing to be more in line with current day realities. The existing heurstics were choosen a decade ago. From Eric Dumazet. 11) Fix races, queue bloat, and excessive wakeups in ATM and associated drivers, from Krzysztof Mazur and David Woodhouse. 12) Support DOVE (Distributed Overlay Virtual Ethernet) extensions in VXLAN driver, from David Stevens. 13) Add "oops_only" mode to netconsole, from Amerigo Wang. 14) Support set and query of VEB/VEPA bridge mode via PF_BRIDGE, also allow DCB netlink to work on namespaces other than the initial namespace. From John Fastabend. 15) Support PTP in the Tigon3 driver, from Matt Carlson. 16) tun/vhost zero copy fixes and improvements, plus turn it on by default, from Michael S. Tsirkin. 17) Support per-association statistics in SCTP, from Michele Baldessari. And many, many, driver updates, cleanups, and improvements. Too numerous to mention individually. * git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next: (1722 commits) net/mlx4_en: Add support for destination MAC in steering rules net/mlx4_en: Use generic etherdevice.h functions. net: ethtool: Add destination MAC address to flow steering API bridge: add support of adding and deleting mdb entries bridge: notify mdb changes via netlink ndisc: Unexport ndisc_{build,send}_skb(). uapi: add missing netconf.h to export list pkt_sched: avoid requeues if possible solos-pci: fix double-free of TX skb in DMA mode bnx2: Fix accidental reversions. bna: Driver Version Updated to 3.1.2.1 bna: Firmware update bna: Add RX State bna: Rx Page Based Allocation bna: TX Intr Coalescing Fix bna: Tx and Rx Optimizations bna: Code Cleanup and Enhancements ath9k: check pdata variable before dereferencing it ath5k: RX timestamp is reported at end of frame ath9k_htc: RX timestamp is reported at end of frame ...
Diffstat (limited to 'net/nfc/llcp')
-rw-r--r--net/nfc/llcp/Kconfig4
-rw-r--r--net/nfc/llcp/commands.c148
-rw-r--r--net/nfc/llcp/llcp.c248
-rw-r--r--net/nfc/llcp/llcp.h13
-rw-r--r--net/nfc/llcp/sock.c42
5 files changed, 400 insertions, 55 deletions
diff --git a/net/nfc/llcp/Kconfig b/net/nfc/llcp/Kconfig
index fbf5e8150908..a1a41cd68255 100644
--- a/net/nfc/llcp/Kconfig
+++ b/net/nfc/llcp/Kconfig
@@ -1,6 +1,6 @@
1config NFC_LLCP 1config NFC_LLCP
2 depends on NFC && EXPERIMENTAL 2 depends on NFC
3 bool "NFC LLCP support (EXPERIMENTAL)" 3 bool "NFC LLCP support"
4 default n 4 default n
5 help 5 help
6 Say Y here if you want to build support for a kernel NFC LLCP 6 Say Y here if you want to build support for a kernel NFC LLCP
diff --git a/net/nfc/llcp/commands.c b/net/nfc/llcp/commands.c
index c45ccd6c094c..df24be48d4da 100644
--- a/net/nfc/llcp/commands.c
+++ b/net/nfc/llcp/commands.c
@@ -261,7 +261,6 @@ int nfc_llcp_disconnect(struct nfc_llcp_sock *sock)
261 struct sk_buff *skb; 261 struct sk_buff *skb;
262 struct nfc_dev *dev; 262 struct nfc_dev *dev;
263 struct nfc_llcp_local *local; 263 struct nfc_llcp_local *local;
264 u16 size = 0;
265 264
266 pr_debug("Sending DISC\n"); 265 pr_debug("Sending DISC\n");
267 266
@@ -273,17 +272,10 @@ int nfc_llcp_disconnect(struct nfc_llcp_sock *sock)
273 if (dev == NULL) 272 if (dev == NULL)
274 return -ENODEV; 273 return -ENODEV;
275 274
276 size += LLCP_HEADER_SIZE; 275 skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
277 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
278
279 skb = alloc_skb(size, GFP_ATOMIC);
280 if (skb == NULL) 276 if (skb == NULL)
281 return -ENOMEM; 277 return -ENOMEM;
282 278
283 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
284
285 skb = llcp_add_header(skb, sock->dsap, sock->ssap, LLCP_PDU_DISC);
286
287 skb_queue_tail(&local->tx_queue, skb); 279 skb_queue_tail(&local->tx_queue, skb);
288 280
289 return 0; 281 return 0;
@@ -324,8 +316,7 @@ int nfc_llcp_send_connect(struct nfc_llcp_sock *sock)
324 struct sk_buff *skb; 316 struct sk_buff *skb;
325 u8 *service_name_tlv = NULL, service_name_tlv_length; 317 u8 *service_name_tlv = NULL, service_name_tlv_length;
326 u8 *miux_tlv = NULL, miux_tlv_length; 318 u8 *miux_tlv = NULL, miux_tlv_length;
327 u8 *rw_tlv = NULL, rw_tlv_length, rw; 319 u8 *rw_tlv = NULL, rw_tlv_length;
328 __be16 miux;
329 int err; 320 int err;
330 u16 size = 0; 321 u16 size = 0;
331 322
@@ -343,13 +334,11 @@ int nfc_llcp_send_connect(struct nfc_llcp_sock *sock)
343 size += service_name_tlv_length; 334 size += service_name_tlv_length;
344 } 335 }
345 336
346 miux = cpu_to_be16(LLCP_MAX_MIUX); 337 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
347 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
348 &miux_tlv_length); 338 &miux_tlv_length);
349 size += miux_tlv_length; 339 size += miux_tlv_length;
350 340
351 rw = LLCP_MAX_RW; 341 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &local->rw, 0, &rw_tlv_length);
352 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
353 size += rw_tlv_length; 342 size += rw_tlv_length;
354 343
355 pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len); 344 pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len);
@@ -386,8 +375,7 @@ int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
386 struct nfc_llcp_local *local; 375 struct nfc_llcp_local *local;
387 struct sk_buff *skb; 376 struct sk_buff *skb;
388 u8 *miux_tlv = NULL, miux_tlv_length; 377 u8 *miux_tlv = NULL, miux_tlv_length;
389 u8 *rw_tlv = NULL, rw_tlv_length, rw; 378 u8 *rw_tlv = NULL, rw_tlv_length;
390 __be16 miux;
391 int err; 379 int err;
392 u16 size = 0; 380 u16 size = 0;
393 381
@@ -397,13 +385,11 @@ int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
397 if (local == NULL) 385 if (local == NULL)
398 return -ENODEV; 386 return -ENODEV;
399 387
400 miux = cpu_to_be16(LLCP_MAX_MIUX); 388 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
401 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
402 &miux_tlv_length); 389 &miux_tlv_length);
403 size += miux_tlv_length; 390 size += miux_tlv_length;
404 391
405 rw = LLCP_MAX_RW; 392 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &local->rw, 0, &rw_tlv_length);
406 rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
407 size += rw_tlv_length; 393 size += rw_tlv_length;
408 394
409 skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size); 395 skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size);
@@ -428,6 +414,52 @@ error_tlv:
428 return err; 414 return err;
429} 415}
430 416
417int nfc_llcp_send_snl(struct nfc_llcp_local *local, u8 tid, u8 sap)
418{
419 struct sk_buff *skb;
420 struct nfc_dev *dev;
421 u8 *sdres_tlv = NULL, sdres_tlv_length, sdres[2];
422 u16 size = 0;
423
424 pr_debug("Sending SNL tid 0x%x sap 0x%x\n", tid, sap);
425
426 if (local == NULL)
427 return -ENODEV;
428
429 dev = local->dev;
430 if (dev == NULL)
431 return -ENODEV;
432
433 sdres[0] = tid;
434 sdres[1] = sap;
435 sdres_tlv = nfc_llcp_build_tlv(LLCP_TLV_SDRES, sdres, 0,
436 &sdres_tlv_length);
437 if (sdres_tlv == NULL)
438 return -ENOMEM;
439
440 size += LLCP_HEADER_SIZE;
441 size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
442 size += sdres_tlv_length;
443
444 skb = alloc_skb(size, GFP_KERNEL);
445 if (skb == NULL) {
446 kfree(sdres_tlv);
447 return -ENOMEM;
448 }
449
450 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
451
452 skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL);
453
454 memcpy(skb_put(skb, sdres_tlv_length), sdres_tlv, sdres_tlv_length);
455
456 skb_queue_tail(&local->tx_queue, skb);
457
458 kfree(sdres_tlv);
459
460 return 0;
461}
462
431int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason) 463int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason)
432{ 464{
433 struct sk_buff *skb; 465 struct sk_buff *skb;
@@ -496,6 +528,23 @@ int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
496 if (local == NULL) 528 if (local == NULL)
497 return -ENODEV; 529 return -ENODEV;
498 530
531 /* Remote is ready but has not acknowledged our frames */
532 if((sock->remote_ready &&
533 skb_queue_len(&sock->tx_pending_queue) >= sock->rw &&
534 skb_queue_len(&sock->tx_queue) >= 2 * sock->rw)) {
535 pr_err("Pending queue is full %d frames\n",
536 skb_queue_len(&sock->tx_pending_queue));
537 return -ENOBUFS;
538 }
539
540 /* Remote is not ready and we've been queueing enough frames */
541 if ((!sock->remote_ready &&
542 skb_queue_len(&sock->tx_queue) >= 2 * sock->rw)) {
543 pr_err("Tx queue is full %d frames\n",
544 skb_queue_len(&sock->tx_queue));
545 return -ENOBUFS;
546 }
547
499 msg_data = kzalloc(len, GFP_KERNEL); 548 msg_data = kzalloc(len, GFP_KERNEL);
500 if (msg_data == NULL) 549 if (msg_data == NULL)
501 return -ENOMEM; 550 return -ENOMEM;
@@ -541,6 +590,63 @@ int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
541 return len; 590 return len;
542} 591}
543 592
593int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap,
594 struct msghdr *msg, size_t len)
595{
596 struct sk_buff *pdu;
597 struct nfc_llcp_local *local;
598 size_t frag_len = 0, remaining_len;
599 u8 *msg_ptr, *msg_data;
600 int err;
601
602 pr_debug("Send UI frame len %zd\n", len);
603
604 local = sock->local;
605 if (local == NULL)
606 return -ENODEV;
607
608 msg_data = kzalloc(len, GFP_KERNEL);
609 if (msg_data == NULL)
610 return -ENOMEM;
611
612 if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) {
613 kfree(msg_data);
614 return -EFAULT;
615 }
616
617 remaining_len = len;
618 msg_ptr = msg_data;
619
620 while (remaining_len > 0) {
621
622 frag_len = min_t(size_t, sock->miu, remaining_len);
623
624 pr_debug("Fragment %zd bytes remaining %zd",
625 frag_len, remaining_len);
626
627 pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
628 frag_len + LLCP_HEADER_SIZE, &err);
629 if (pdu == NULL) {
630 pr_err("Could not allocate PDU\n");
631 continue;
632 }
633
634 pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI);
635
636 memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len);
637
638 /* No need to check for the peer RW for UI frames */
639 skb_queue_tail(&local->tx_queue, pdu);
640
641 remaining_len -= frag_len;
642 msg_ptr += frag_len;
643 }
644
645 kfree(msg_data);
646
647 return len;
648}
649
544int nfc_llcp_send_rr(struct nfc_llcp_sock *sock) 650int nfc_llcp_send_rr(struct nfc_llcp_sock *sock)
545{ 651{
546 struct sk_buff *skb; 652 struct sk_buff *skb;
diff --git a/net/nfc/llcp/llcp.c b/net/nfc/llcp/llcp.c
index 9e8f4b2801f6..ec43914c92a9 100644
--- a/net/nfc/llcp/llcp.c
+++ b/net/nfc/llcp/llcp.c
@@ -45,12 +45,38 @@ void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
45 write_unlock(&l->lock); 45 write_unlock(&l->lock);
46} 46}
47 47
48static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
49{
50 struct nfc_llcp_local *local = sock->local;
51 struct sk_buff *s, *tmp;
52
53 pr_debug("%p\n", &sock->sk);
54
55 skb_queue_purge(&sock->tx_queue);
56 skb_queue_purge(&sock->tx_pending_queue);
57 skb_queue_purge(&sock->tx_backlog_queue);
58
59 if (local == NULL)
60 return;
61
62 /* Search for local pending SKBs that are related to this socket */
63 skb_queue_walk_safe(&local->tx_queue, s, tmp) {
64 if (s->sk != &sock->sk)
65 continue;
66
67 skb_unlink(s, &local->tx_queue);
68 kfree_skb(s);
69 }
70}
71
48static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen) 72static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
49{ 73{
50 struct sock *sk; 74 struct sock *sk;
51 struct hlist_node *node, *tmp; 75 struct hlist_node *node, *tmp;
52 struct nfc_llcp_sock *llcp_sock; 76 struct nfc_llcp_sock *llcp_sock;
53 77
78 skb_queue_purge(&local->tx_queue);
79
54 write_lock(&local->sockets.lock); 80 write_lock(&local->sockets.lock);
55 81
56 sk_for_each_safe(sk, node, tmp, &local->sockets.head) { 82 sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
@@ -58,6 +84,8 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
58 84
59 bh_lock_sock(sk); 85 bh_lock_sock(sk);
60 86
87 nfc_llcp_socket_purge(llcp_sock);
88
61 if (sk->sk_state == LLCP_CONNECTED) 89 if (sk->sk_state == LLCP_CONNECTED)
62 nfc_put_device(llcp_sock->dev); 90 nfc_put_device(llcp_sock->dev);
63 91
@@ -65,7 +93,8 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
65 struct nfc_llcp_sock *lsk, *n; 93 struct nfc_llcp_sock *lsk, *n;
66 struct sock *accept_sk; 94 struct sock *accept_sk;
67 95
68 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue, 96 list_for_each_entry_safe(lsk, n,
97 &llcp_sock->accept_queue,
69 accept_queue) { 98 accept_queue) {
70 accept_sk = &lsk->sk; 99 accept_sk = &lsk->sk;
71 bh_lock_sock(accept_sk); 100 bh_lock_sock(accept_sk);
@@ -85,6 +114,16 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
85 } 114 }
86 } 115 }
87 116
117 /*
118 * If we have a connection less socket bound, we keep it alive
119 * if the device is still present.
120 */
121 if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM &&
122 listen == true) {
123 bh_unlock_sock(sk);
124 continue;
125 }
126
88 sk->sk_state = LLCP_CLOSED; 127 sk->sk_state = LLCP_CLOSED;
89 128
90 bh_unlock_sock(sk); 129 bh_unlock_sock(sk);
@@ -134,7 +173,7 @@ static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
134{ 173{
135 struct sock *sk; 174 struct sock *sk;
136 struct hlist_node *node; 175 struct hlist_node *node;
137 struct nfc_llcp_sock *llcp_sock; 176 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
138 177
139 pr_debug("ssap dsap %d %d\n", ssap, dsap); 178 pr_debug("ssap dsap %d %d\n", ssap, dsap);
140 179
@@ -146,10 +185,12 @@ static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
146 llcp_sock = NULL; 185 llcp_sock = NULL;
147 186
148 sk_for_each(sk, node, &local->sockets.head) { 187 sk_for_each(sk, node, &local->sockets.head) {
149 llcp_sock = nfc_llcp_sock(sk); 188 tmp_sock = nfc_llcp_sock(sk);
150 189
151 if (llcp_sock->ssap == ssap && llcp_sock->dsap == dsap) 190 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
191 llcp_sock = tmp_sock;
152 break; 192 break;
193 }
153 } 194 }
154 195
155 read_unlock(&local->sockets.lock); 196 read_unlock(&local->sockets.lock);
@@ -249,7 +290,12 @@ struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
249 290
250 pr_debug("llcp sock %p\n", tmp_sock); 291 pr_debug("llcp sock %p\n", tmp_sock);
251 292
252 if (tmp_sock->sk.sk_state != LLCP_LISTEN) 293 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
294 tmp_sock->sk.sk_state != LLCP_LISTEN)
295 continue;
296
297 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
298 tmp_sock->sk.sk_state != LLCP_BOUND)
253 continue; 299 continue;
254 300
255 if (tmp_sock->service_name == NULL || 301 if (tmp_sock->service_name == NULL ||
@@ -421,10 +467,9 @@ static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
421static int nfc_llcp_build_gb(struct nfc_llcp_local *local) 467static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
422{ 468{
423 u8 *gb_cur, *version_tlv, version, version_length; 469 u8 *gb_cur, *version_tlv, version, version_length;
424 u8 *lto_tlv, lto, lto_length; 470 u8 *lto_tlv, lto_length;
425 u8 *wks_tlv, wks_length; 471 u8 *wks_tlv, wks_length;
426 u8 *miux_tlv, miux_length; 472 u8 *miux_tlv, miux_length;
427 __be16 miux;
428 u8 gb_len = 0; 473 u8 gb_len = 0;
429 int ret = 0; 474 int ret = 0;
430 475
@@ -433,9 +478,7 @@ static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
433 1, &version_length); 478 1, &version_length);
434 gb_len += version_length; 479 gb_len += version_length;
435 480
436 /* 1500 ms */ 481 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
437 lto = 150;
438 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &lto, 1, &lto_length);
439 gb_len += lto_length; 482 gb_len += lto_length;
440 483
441 pr_debug("Local wks 0x%lx\n", local->local_wks); 484 pr_debug("Local wks 0x%lx\n", local->local_wks);
@@ -443,8 +486,7 @@ static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
443 &wks_length); 486 &wks_length);
444 gb_len += wks_length; 487 gb_len += wks_length;
445 488
446 miux = cpu_to_be16(LLCP_MAX_MIUX); 489 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
447 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
448 &miux_length); 490 &miux_length);
449 gb_len += miux_length; 491 gb_len += miux_length;
450 492
@@ -610,7 +652,12 @@ static void nfc_llcp_tx_work(struct work_struct *work)
610 if (skb != NULL) { 652 if (skb != NULL) {
611 sk = skb->sk; 653 sk = skb->sk;
612 llcp_sock = nfc_llcp_sock(sk); 654 llcp_sock = nfc_llcp_sock(sk);
613 if (llcp_sock != NULL) { 655
656 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
657 nfc_llcp_send_symm(local->dev);
658 } else {
659 struct sk_buff *copy_skb = NULL;
660 u8 ptype = nfc_llcp_ptype(skb);
614 int ret; 661 int ret;
615 662
616 pr_debug("Sending pending skb\n"); 663 pr_debug("Sending pending skb\n");
@@ -618,24 +665,29 @@ static void nfc_llcp_tx_work(struct work_struct *work)
618 DUMP_PREFIX_OFFSET, 16, 1, 665 DUMP_PREFIX_OFFSET, 16, 1,
619 skb->data, skb->len, true); 666 skb->data, skb->len, true);
620 667
668 if (ptype == LLCP_PDU_I)
669 copy_skb = skb_copy(skb, GFP_ATOMIC);
670
621 nfc_llcp_send_to_raw_sock(local, skb, 671 nfc_llcp_send_to_raw_sock(local, skb,
622 NFC_LLCP_DIRECTION_TX); 672 NFC_LLCP_DIRECTION_TX);
623 673
624 ret = nfc_data_exchange(local->dev, local->target_idx, 674 ret = nfc_data_exchange(local->dev, local->target_idx,
625 skb, nfc_llcp_recv, local); 675 skb, nfc_llcp_recv, local);
626 676
627 if (!ret && nfc_llcp_ptype(skb) == LLCP_PDU_I) { 677 if (ret) {
628 skb = skb_get(skb); 678 kfree_skb(copy_skb);
629 skb_queue_tail(&llcp_sock->tx_pending_queue, 679 goto out;
630 skb);
631 } 680 }
632 } else { 681
633 nfc_llcp_send_symm(local->dev); 682 if (ptype == LLCP_PDU_I && copy_skb)
683 skb_queue_tail(&llcp_sock->tx_pending_queue,
684 copy_skb);
634 } 685 }
635 } else { 686 } else {
636 nfc_llcp_send_symm(local->dev); 687 nfc_llcp_send_symm(local->dev);
637 } 688 }
638 689
690out:
639 mod_timer(&local->link_timer, 691 mod_timer(&local->link_timer,
640 jiffies + msecs_to_jiffies(2 * local->remote_lto)); 692 jiffies + msecs_to_jiffies(2 * local->remote_lto));
641} 693}
@@ -704,6 +756,39 @@ static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
704 return NULL; 756 return NULL;
705} 757}
706 758
759static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
760 struct sk_buff *skb)
761{
762 struct nfc_llcp_sock *llcp_sock;
763 struct nfc_llcp_ui_cb *ui_cb;
764 u8 dsap, ssap;
765
766 dsap = nfc_llcp_dsap(skb);
767 ssap = nfc_llcp_ssap(skb);
768
769 ui_cb = nfc_llcp_ui_skb_cb(skb);
770 ui_cb->dsap = dsap;
771 ui_cb->ssap = ssap;
772
773 printk("%s %d %d\n", __func__, dsap, ssap);
774
775 pr_debug("%d %d\n", dsap, ssap);
776
777 /* We're looking for a bound socket, not a client one */
778 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
779 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
780 return;
781
782 /* There is no sequence with UI frames */
783 skb_pull(skb, LLCP_HEADER_SIZE);
784 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
785 pr_err("receive queue is full\n");
786 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
787 }
788
789 nfc_llcp_sock_put(llcp_sock);
790}
791
707static void nfc_llcp_recv_connect(struct nfc_llcp_local *local, 792static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
708 struct sk_buff *skb) 793 struct sk_buff *skb)
709{ 794{
@@ -823,9 +908,6 @@ static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
823fail: 908fail:
824 /* Send DM */ 909 /* Send DM */
825 nfc_llcp_send_dm(local, dsap, ssap, reason); 910 nfc_llcp_send_dm(local, dsap, ssap, reason);
826
827 return;
828
829} 911}
830 912
831int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock) 913int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
@@ -903,15 +985,18 @@ static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
903 /* Remove skbs from the pending queue */ 985 /* Remove skbs from the pending queue */
904 if (llcp_sock->send_ack_n != nr) { 986 if (llcp_sock->send_ack_n != nr) {
905 struct sk_buff *s, *tmp; 987 struct sk_buff *s, *tmp;
988 u8 n;
906 989
907 llcp_sock->send_ack_n = nr; 990 llcp_sock->send_ack_n = nr;
908 991
909 /* Remove and free all skbs until ns == nr */ 992 /* Remove and free all skbs until ns == nr */
910 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) { 993 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
994 n = nfc_llcp_ns(s);
995
911 skb_unlink(s, &llcp_sock->tx_pending_queue); 996 skb_unlink(s, &llcp_sock->tx_pending_queue);
912 kfree_skb(s); 997 kfree_skb(s);
913 998
914 if (nfc_llcp_ns(s) == nr) 999 if (n == nr)
915 break; 1000 break;
916 } 1001 }
917 1002
@@ -953,6 +1038,9 @@ static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
953 1038
954 sk = &llcp_sock->sk; 1039 sk = &llcp_sock->sk;
955 lock_sock(sk); 1040 lock_sock(sk);
1041
1042 nfc_llcp_socket_purge(llcp_sock);
1043
956 if (sk->sk_state == LLCP_CLOSED) { 1044 if (sk->sk_state == LLCP_CLOSED) {
957 release_sock(sk); 1045 release_sock(sk);
958 nfc_llcp_sock_put(llcp_sock); 1046 nfc_llcp_sock_put(llcp_sock);
@@ -1027,7 +1115,7 @@ static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1027 } 1115 }
1028 1116
1029 if (llcp_sock == NULL) { 1117 if (llcp_sock == NULL) {
1030 pr_err("Invalid DM\n"); 1118 pr_debug("Already closed\n");
1031 return; 1119 return;
1032 } 1120 }
1033 1121
@@ -1038,8 +1126,100 @@ static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1038 sk->sk_state_change(sk); 1126 sk->sk_state_change(sk);
1039 1127
1040 nfc_llcp_sock_put(llcp_sock); 1128 nfc_llcp_sock_put(llcp_sock);
1129}
1041 1130
1042 return; 1131static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1132 struct sk_buff *skb)
1133{
1134 struct nfc_llcp_sock *llcp_sock;
1135 u8 dsap, ssap, *tlv, type, length, tid, sap;
1136 u16 tlv_len, offset;
1137 char *service_name;
1138 size_t service_name_len;
1139
1140 dsap = nfc_llcp_dsap(skb);
1141 ssap = nfc_llcp_ssap(skb);
1142
1143 pr_debug("%d %d\n", dsap, ssap);
1144
1145 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1146 pr_err("Wrong SNL SAP\n");
1147 return;
1148 }
1149
1150 tlv = &skb->data[LLCP_HEADER_SIZE];
1151 tlv_len = skb->len - LLCP_HEADER_SIZE;
1152 offset = 0;
1153
1154 while (offset < tlv_len) {
1155 type = tlv[0];
1156 length = tlv[1];
1157
1158 switch (type) {
1159 case LLCP_TLV_SDREQ:
1160 tid = tlv[2];
1161 service_name = (char *) &tlv[3];
1162 service_name_len = length - 1;
1163
1164 pr_debug("Looking for %.16s\n", service_name);
1165
1166 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1167 !strncmp(service_name, "urn:nfc:sn:sdp",
1168 service_name_len)) {
1169 sap = 1;
1170 goto send_snl;
1171 }
1172
1173 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1174 service_name_len);
1175 if (!llcp_sock) {
1176 sap = 0;
1177 goto send_snl;
1178 }
1179
1180 /*
1181 * We found a socket but its ssap has not been reserved
1182 * yet. We need to assign it for good and send a reply.
1183 * The ssap will be freed when the socket is closed.
1184 */
1185 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1186 atomic_t *client_count;
1187
1188 sap = nfc_llcp_reserve_sdp_ssap(local);
1189
1190 pr_debug("Reserving %d\n", sap);
1191
1192 if (sap == LLCP_SAP_MAX) {
1193 sap = 0;
1194 goto send_snl;
1195 }
1196
1197 client_count =
1198 &local->local_sdp_cnt[sap -
1199 LLCP_WKS_NUM_SAP];
1200
1201 atomic_inc(client_count);
1202
1203 llcp_sock->ssap = sap;
1204 llcp_sock->reserved_ssap = sap;
1205 } else {
1206 sap = llcp_sock->ssap;
1207 }
1208
1209 pr_debug("%p %d\n", llcp_sock, sap);
1210
1211send_snl:
1212 nfc_llcp_send_snl(local, tid, sap);
1213 break;
1214
1215 default:
1216 pr_err("Invalid SNL tlv value 0x%x\n", type);
1217 break;
1218 }
1219
1220 offset += length + 2;
1221 tlv += length + 2;
1222 }
1043} 1223}
1044 1224
1045static void nfc_llcp_rx_work(struct work_struct *work) 1225static void nfc_llcp_rx_work(struct work_struct *work)
@@ -1072,6 +1252,11 @@ static void nfc_llcp_rx_work(struct work_struct *work)
1072 pr_debug("SYMM\n"); 1252 pr_debug("SYMM\n");
1073 break; 1253 break;
1074 1254
1255 case LLCP_PDU_UI:
1256 pr_debug("UI\n");
1257 nfc_llcp_recv_ui(local, skb);
1258 break;
1259
1075 case LLCP_PDU_CONNECT: 1260 case LLCP_PDU_CONNECT:
1076 pr_debug("CONNECT\n"); 1261 pr_debug("CONNECT\n");
1077 nfc_llcp_recv_connect(local, skb); 1262 nfc_llcp_recv_connect(local, skb);
@@ -1092,6 +1277,11 @@ static void nfc_llcp_rx_work(struct work_struct *work)
1092 nfc_llcp_recv_dm(local, skb); 1277 nfc_llcp_recv_dm(local, skb);
1093 break; 1278 break;
1094 1279
1280 case LLCP_PDU_SNL:
1281 pr_debug("SNL\n");
1282 nfc_llcp_recv_snl(local, skb);
1283 break;
1284
1095 case LLCP_PDU_I: 1285 case LLCP_PDU_I:
1096 case LLCP_PDU_RR: 1286 case LLCP_PDU_RR:
1097 case LLCP_PDU_RNR: 1287 case LLCP_PDU_RNR:
@@ -1104,8 +1294,6 @@ static void nfc_llcp_rx_work(struct work_struct *work)
1104 schedule_work(&local->tx_work); 1294 schedule_work(&local->tx_work);
1105 kfree_skb(local->rx_pending); 1295 kfree_skb(local->rx_pending);
1106 local->rx_pending = NULL; 1296 local->rx_pending = NULL;
1107
1108 return;
1109} 1297}
1110 1298
1111void nfc_llcp_recv(void *data, struct sk_buff *skb, int err) 1299void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
@@ -1121,8 +1309,6 @@ void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1121 local->rx_pending = skb_get(skb); 1309 local->rx_pending = skb_get(skb);
1122 del_timer(&local->link_timer); 1310 del_timer(&local->link_timer);
1123 schedule_work(&local->rx_work); 1311 schedule_work(&local->rx_work);
1124
1125 return;
1126} 1312}
1127 1313
1128int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb) 1314int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
@@ -1205,6 +1391,10 @@ int nfc_llcp_register_device(struct nfc_dev *ndev)
1205 rwlock_init(&local->connecting_sockets.lock); 1391 rwlock_init(&local->connecting_sockets.lock);
1206 rwlock_init(&local->raw_sockets.lock); 1392 rwlock_init(&local->raw_sockets.lock);
1207 1393
1394 local->lto = 150; /* 1500 ms */
1395 local->rw = LLCP_MAX_RW;
1396 local->miux = cpu_to_be16(LLCP_MAX_MIUX);
1397
1208 nfc_llcp_build_gb(local); 1398 nfc_llcp_build_gb(local);
1209 1399
1210 local->remote_miu = LLCP_DEFAULT_MIU; 1400 local->remote_miu = LLCP_DEFAULT_MIU;
diff --git a/net/nfc/llcp/llcp.h b/net/nfc/llcp/llcp.h
index fdb2d24e60bd..0d62366f8cc3 100644
--- a/net/nfc/llcp/llcp.h
+++ b/net/nfc/llcp/llcp.h
@@ -64,6 +64,9 @@ struct nfc_llcp_local {
64 u32 target_idx; 64 u32 target_idx;
65 u8 rf_mode; 65 u8 rf_mode;
66 u8 comm_mode; 66 u8 comm_mode;
67 u8 lto;
68 u8 rw;
69 __be16 miux;
67 unsigned long local_wks; /* Well known services */ 70 unsigned long local_wks; /* Well known services */
68 unsigned long local_sdp; /* Local services */ 71 unsigned long local_sdp; /* Local services */
69 unsigned long local_sap; /* Local SAPs, not available for discovery */ 72 unsigned long local_sap; /* Local SAPs, not available for discovery */
@@ -124,6 +127,13 @@ struct nfc_llcp_sock {
124 struct sock *parent; 127 struct sock *parent;
125}; 128};
126 129
130struct nfc_llcp_ui_cb {
131 __u8 dsap;
132 __u8 ssap;
133};
134
135#define nfc_llcp_ui_skb_cb(__skb) ((struct nfc_llcp_ui_cb *)&((__skb)->cb[0]))
136
127#define nfc_llcp_sock(sk) ((struct nfc_llcp_sock *) (sk)) 137#define nfc_llcp_sock(sk) ((struct nfc_llcp_sock *) (sk))
128#define nfc_llcp_dev(sk) (nfc_llcp_sock((sk))->dev) 138#define nfc_llcp_dev(sk) (nfc_llcp_sock((sk))->dev)
129 139
@@ -209,10 +219,13 @@ int nfc_llcp_disconnect(struct nfc_llcp_sock *sock);
209int nfc_llcp_send_symm(struct nfc_dev *dev); 219int nfc_llcp_send_symm(struct nfc_dev *dev);
210int nfc_llcp_send_connect(struct nfc_llcp_sock *sock); 220int nfc_llcp_send_connect(struct nfc_llcp_sock *sock);
211int nfc_llcp_send_cc(struct nfc_llcp_sock *sock); 221int nfc_llcp_send_cc(struct nfc_llcp_sock *sock);
222int nfc_llcp_send_snl(struct nfc_llcp_local *local, u8 tid, u8 sap);
212int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason); 223int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason);
213int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock); 224int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock);
214int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, 225int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
215 struct msghdr *msg, size_t len); 226 struct msghdr *msg, size_t len);
227int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap,
228 struct msghdr *msg, size_t len);
216int nfc_llcp_send_rr(struct nfc_llcp_sock *sock); 229int nfc_llcp_send_rr(struct nfc_llcp_sock *sock);
217 230
218/* Socket API */ 231/* Socket API */
diff --git a/net/nfc/llcp/sock.c b/net/nfc/llcp/sock.c
index 63e4cdc92376..0fa1e92ceac8 100644
--- a/net/nfc/llcp/sock.c
+++ b/net/nfc/llcp/sock.c
@@ -205,8 +205,8 @@ static int llcp_sock_listen(struct socket *sock, int backlog)
205 205
206 lock_sock(sk); 206 lock_sock(sk);
207 207
208 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) 208 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) ||
209 || sk->sk_state != LLCP_BOUND) { 209 sk->sk_state != LLCP_BOUND) {
210 ret = -EBADFD; 210 ret = -EBADFD;
211 goto error; 211 goto error;
212 } 212 }
@@ -608,6 +608,25 @@ static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
608 608
609 lock_sock(sk); 609 lock_sock(sk);
610 610
611 if (sk->sk_type == SOCK_DGRAM) {
612 struct sockaddr_nfc_llcp *addr =
613 (struct sockaddr_nfc_llcp *)msg->msg_name;
614
615 if (msg->msg_namelen < sizeof(*addr)) {
616 release_sock(sk);
617
618 pr_err("Invalid socket address length %d\n",
619 msg->msg_namelen);
620
621 return -EINVAL;
622 }
623
624 release_sock(sk);
625
626 return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
627 msg, len);
628 }
629
611 if (sk->sk_state != LLCP_CONNECTED) { 630 if (sk->sk_state != LLCP_CONNECTED) {
612 release_sock(sk); 631 release_sock(sk);
613 return -ENOTCONN; 632 return -ENOTCONN;
@@ -663,11 +682,28 @@ static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
663 return -EFAULT; 682 return -EFAULT;
664 } 683 }
665 684
685 if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
686 struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
687 struct sockaddr_nfc_llcp sockaddr;
688
689 pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
690
691 sockaddr.sa_family = AF_NFC;
692 sockaddr.nfc_protocol = NFC_PROTO_NFC_DEP;
693 sockaddr.dsap = ui_cb->dsap;
694 sockaddr.ssap = ui_cb->ssap;
695
696 memcpy(msg->msg_name, &sockaddr, sizeof(sockaddr));
697 msg->msg_namelen = sizeof(sockaddr);
698 }
699
666 /* Mark read part of skb as used */ 700 /* Mark read part of skb as used */
667 if (!(flags & MSG_PEEK)) { 701 if (!(flags & MSG_PEEK)) {
668 702
669 /* SOCK_STREAM: re-queue skb if it contains unreceived data */ 703 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
670 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_RAW) { 704 if (sk->sk_type == SOCK_STREAM ||
705 sk->sk_type == SOCK_DGRAM ||
706 sk->sk_type == SOCK_RAW) {
671 skb_pull(skb, copied); 707 skb_pull(skb, copied);
672 if (skb->len) { 708 if (skb->len) {
673 skb_queue_head(&sk->sk_receive_queue, skb); 709 skb_queue_head(&sk->sk_receive_queue, skb);