2 * Copyright (C) 2011 Intel Corporation. All rights reserved.
3 * Copyright (C) 2014 Marvell International Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/nfc.h>
29 static u8 llcp_magic
[3] = {0x46, 0x66, 0x6d};
31 static LIST_HEAD(llcp_devices
);
33 static void nfc_llcp_rx_skb(struct nfc_llcp_local
*local
, struct sk_buff
*skb
);
35 void nfc_llcp_sock_link(struct llcp_sock_list
*l
, struct sock
*sk
)
38 sk_add_node(sk
, &l
->head
);
39 write_unlock(&l
->lock
);
42 void nfc_llcp_sock_unlink(struct llcp_sock_list
*l
, struct sock
*sk
)
46 write_unlock(&l
->lock
);
49 void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock
*sock
)
51 sock
->remote_rw
= LLCP_DEFAULT_RW
;
52 sock
->remote_miu
= LLCP_MAX_MIU
+ 1;
55 static void nfc_llcp_socket_purge(struct nfc_llcp_sock
*sock
)
57 struct nfc_llcp_local
*local
= sock
->local
;
58 struct sk_buff
*s
, *tmp
;
60 pr_debug("%p\n", &sock
->sk
);
62 skb_queue_purge(&sock
->tx_queue
);
63 skb_queue_purge(&sock
->tx_pending_queue
);
68 /* Search for local pending SKBs that are related to this socket */
69 skb_queue_walk_safe(&local
->tx_queue
, s
, tmp
) {
70 if (s
->sk
!= &sock
->sk
)
73 skb_unlink(s
, &local
->tx_queue
);
78 static void nfc_llcp_socket_release(struct nfc_llcp_local
*local
, bool device
,
82 struct hlist_node
*tmp
;
83 struct nfc_llcp_sock
*llcp_sock
;
85 skb_queue_purge(&local
->tx_queue
);
87 write_lock(&local
->sockets
.lock
);
89 sk_for_each_safe(sk
, tmp
, &local
->sockets
.head
) {
90 llcp_sock
= nfc_llcp_sock(sk
);
94 nfc_llcp_socket_purge(llcp_sock
);
96 if (sk
->sk_state
== LLCP_CONNECTED
)
97 nfc_put_device(llcp_sock
->dev
);
99 if (sk
->sk_state
== LLCP_LISTEN
) {
100 struct nfc_llcp_sock
*lsk
, *n
;
101 struct sock
*accept_sk
;
103 list_for_each_entry_safe(lsk
, n
,
104 &llcp_sock
->accept_queue
,
106 accept_sk
= &lsk
->sk
;
107 bh_lock_sock(accept_sk
);
109 nfc_llcp_accept_unlink(accept_sk
);
112 accept_sk
->sk_err
= err
;
113 accept_sk
->sk_state
= LLCP_CLOSED
;
114 accept_sk
->sk_state_change(sk
);
116 bh_unlock_sock(accept_sk
);
122 sk
->sk_state
= LLCP_CLOSED
;
123 sk
->sk_state_change(sk
);
127 sk_del_node_init(sk
);
130 write_unlock(&local
->sockets
.lock
);
132 /* If we still have a device, we keep the RAW sockets alive */
136 write_lock(&local
->raw_sockets
.lock
);
138 sk_for_each_safe(sk
, tmp
, &local
->raw_sockets
.head
) {
139 llcp_sock
= nfc_llcp_sock(sk
);
143 nfc_llcp_socket_purge(llcp_sock
);
147 sk
->sk_state
= LLCP_CLOSED
;
148 sk
->sk_state_change(sk
);
152 sk_del_node_init(sk
);
155 write_unlock(&local
->raw_sockets
.lock
);
158 struct nfc_llcp_local
*nfc_llcp_local_get(struct nfc_llcp_local
*local
)
160 kref_get(&local
->ref
);
165 static void local_cleanup(struct nfc_llcp_local
*local
)
167 nfc_llcp_socket_release(local
, false, ENXIO
);
168 del_timer_sync(&local
->link_timer
);
169 skb_queue_purge(&local
->tx_queue
);
170 cancel_work_sync(&local
->tx_work
);
171 cancel_work_sync(&local
->rx_work
);
172 cancel_work_sync(&local
->timeout_work
);
173 kfree_skb(local
->rx_pending
);
174 del_timer_sync(&local
->sdreq_timer
);
175 cancel_work_sync(&local
->sdreq_timeout_work
);
176 nfc_llcp_free_sdp_tlv_list(&local
->pending_sdreqs
);
179 static void local_release(struct kref
*ref
)
181 struct nfc_llcp_local
*local
;
183 local
= container_of(ref
, struct nfc_llcp_local
, ref
);
185 list_del(&local
->list
);
186 local_cleanup(local
);
190 int nfc_llcp_local_put(struct nfc_llcp_local
*local
)
195 return kref_put(&local
->ref
, local_release
);
198 static struct nfc_llcp_sock
*nfc_llcp_sock_get(struct nfc_llcp_local
*local
,
202 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
204 pr_debug("ssap dsap %d %d\n", ssap
, dsap
);
206 if (ssap
== 0 && dsap
== 0)
209 read_lock(&local
->sockets
.lock
);
213 sk_for_each(sk
, &local
->sockets
.head
) {
214 tmp_sock
= nfc_llcp_sock(sk
);
216 if (tmp_sock
->ssap
== ssap
&& tmp_sock
->dsap
== dsap
) {
217 llcp_sock
= tmp_sock
;
222 read_unlock(&local
->sockets
.lock
);
224 if (llcp_sock
== NULL
)
227 sock_hold(&llcp_sock
->sk
);
232 static void nfc_llcp_sock_put(struct nfc_llcp_sock
*sock
)
237 static void nfc_llcp_timeout_work(struct work_struct
*work
)
239 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
242 nfc_dep_link_down(local
->dev
);
245 static void nfc_llcp_symm_timer(unsigned long data
)
247 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
249 pr_err("SYMM timeout\n");
251 schedule_work(&local
->timeout_work
);
254 static void nfc_llcp_sdreq_timeout_work(struct work_struct
*work
)
257 HLIST_HEAD(nl_sdres_list
);
258 struct hlist_node
*n
;
259 struct nfc_llcp_sdp_tlv
*sdp
;
260 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
263 mutex_lock(&local
->sdreq_lock
);
265 time
= jiffies
- msecs_to_jiffies(3 * local
->remote_lto
);
267 hlist_for_each_entry_safe(sdp
, n
, &local
->pending_sdreqs
, node
) {
268 if (time_after(sdp
->time
, time
))
271 sdp
->sap
= LLCP_SDP_UNBOUND
;
273 hlist_del(&sdp
->node
);
275 hlist_add_head(&sdp
->node
, &nl_sdres_list
);
278 if (!hlist_empty(&local
->pending_sdreqs
))
279 mod_timer(&local
->sdreq_timer
,
280 jiffies
+ msecs_to_jiffies(3 * local
->remote_lto
));
282 mutex_unlock(&local
->sdreq_lock
);
284 if (!hlist_empty(&nl_sdres_list
))
285 nfc_genl_llc_send_sdres(local
->dev
, &nl_sdres_list
);
288 static void nfc_llcp_sdreq_timer(unsigned long data
)
290 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
292 schedule_work(&local
->sdreq_timeout_work
);
295 struct nfc_llcp_local
*nfc_llcp_find_local(struct nfc_dev
*dev
)
297 struct nfc_llcp_local
*local
;
299 list_for_each_entry(local
, &llcp_devices
, list
)
300 if (local
->dev
== dev
)
303 pr_debug("No device found\n");
308 static char *wks
[] = {
316 static int nfc_llcp_wks_sap(char *service_name
, size_t service_name_len
)
320 pr_debug("%s\n", service_name
);
322 if (service_name
== NULL
)
325 num_wks
= ARRAY_SIZE(wks
);
327 for (sap
= 0; sap
< num_wks
; sap
++) {
328 if (wks
[sap
] == NULL
)
331 if (strncmp(wks
[sap
], service_name
, service_name_len
) == 0)
339 struct nfc_llcp_sock
*nfc_llcp_sock_from_sn(struct nfc_llcp_local
*local
,
340 u8
*sn
, size_t sn_len
)
343 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
345 pr_debug("sn %zd %p\n", sn_len
, sn
);
347 if (sn
== NULL
|| sn_len
== 0)
350 read_lock(&local
->sockets
.lock
);
354 sk_for_each(sk
, &local
->sockets
.head
) {
355 tmp_sock
= nfc_llcp_sock(sk
);
357 pr_debug("llcp sock %p\n", tmp_sock
);
359 if (tmp_sock
->sk
.sk_type
== SOCK_STREAM
&&
360 tmp_sock
->sk
.sk_state
!= LLCP_LISTEN
)
363 if (tmp_sock
->sk
.sk_type
== SOCK_DGRAM
&&
364 tmp_sock
->sk
.sk_state
!= LLCP_BOUND
)
367 if (tmp_sock
->service_name
== NULL
||
368 tmp_sock
->service_name_len
== 0)
371 if (tmp_sock
->service_name_len
!= sn_len
)
374 if (memcmp(sn
, tmp_sock
->service_name
, sn_len
) == 0) {
375 llcp_sock
= tmp_sock
;
380 read_unlock(&local
->sockets
.lock
);
382 pr_debug("Found llcp sock %p\n", llcp_sock
);
387 u8
nfc_llcp_get_sdp_ssap(struct nfc_llcp_local
*local
,
388 struct nfc_llcp_sock
*sock
)
390 mutex_lock(&local
->sdp_lock
);
392 if (sock
->service_name
!= NULL
&& sock
->service_name_len
> 0) {
393 int ssap
= nfc_llcp_wks_sap(sock
->service_name
,
394 sock
->service_name_len
);
397 pr_debug("WKS %d\n", ssap
);
399 /* This is a WKS, let's check if it's free */
400 if (local
->local_wks
& BIT(ssap
)) {
401 mutex_unlock(&local
->sdp_lock
);
406 set_bit(ssap
, &local
->local_wks
);
407 mutex_unlock(&local
->sdp_lock
);
413 * Check if there already is a non WKS socket bound
414 * to this service name.
416 if (nfc_llcp_sock_from_sn(local
, sock
->service_name
,
417 sock
->service_name_len
) != NULL
) {
418 mutex_unlock(&local
->sdp_lock
);
423 mutex_unlock(&local
->sdp_lock
);
425 return LLCP_SDP_UNBOUND
;
427 } else if (sock
->ssap
!= 0 && sock
->ssap
< LLCP_WKS_NUM_SAP
) {
428 if (!test_bit(sock
->ssap
, &local
->local_wks
)) {
429 set_bit(sock
->ssap
, &local
->local_wks
);
430 mutex_unlock(&local
->sdp_lock
);
436 mutex_unlock(&local
->sdp_lock
);
441 u8
nfc_llcp_get_local_ssap(struct nfc_llcp_local
*local
)
445 mutex_lock(&local
->sdp_lock
);
447 local_ssap
= find_first_zero_bit(&local
->local_sap
, LLCP_LOCAL_NUM_SAP
);
448 if (local_ssap
== LLCP_LOCAL_NUM_SAP
) {
449 mutex_unlock(&local
->sdp_lock
);
453 set_bit(local_ssap
, &local
->local_sap
);
455 mutex_unlock(&local
->sdp_lock
);
457 return local_ssap
+ LLCP_LOCAL_SAP_OFFSET
;
460 void nfc_llcp_put_ssap(struct nfc_llcp_local
*local
, u8 ssap
)
465 if (ssap
< LLCP_WKS_NUM_SAP
) {
467 sdp
= &local
->local_wks
;
468 } else if (ssap
< LLCP_LOCAL_NUM_SAP
) {
469 atomic_t
*client_cnt
;
471 local_ssap
= ssap
- LLCP_WKS_NUM_SAP
;
472 sdp
= &local
->local_sdp
;
473 client_cnt
= &local
->local_sdp_cnt
[local_ssap
];
475 pr_debug("%d clients\n", atomic_read(client_cnt
));
477 mutex_lock(&local
->sdp_lock
);
479 if (atomic_dec_and_test(client_cnt
)) {
480 struct nfc_llcp_sock
*l_sock
;
482 pr_debug("No more clients for SAP %d\n", ssap
);
484 clear_bit(local_ssap
, sdp
);
486 /* Find the listening sock and set it back to UNBOUND */
487 l_sock
= nfc_llcp_sock_get(local
, ssap
, LLCP_SAP_SDP
);
489 l_sock
->ssap
= LLCP_SDP_UNBOUND
;
490 nfc_llcp_sock_put(l_sock
);
494 mutex_unlock(&local
->sdp_lock
);
497 } else if (ssap
< LLCP_MAX_SAP
) {
498 local_ssap
= ssap
- LLCP_LOCAL_NUM_SAP
;
499 sdp
= &local
->local_sap
;
504 mutex_lock(&local
->sdp_lock
);
506 clear_bit(local_ssap
, sdp
);
508 mutex_unlock(&local
->sdp_lock
);
511 static u8
nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local
*local
)
515 mutex_lock(&local
->sdp_lock
);
517 ssap
= find_first_zero_bit(&local
->local_sdp
, LLCP_SDP_NUM_SAP
);
518 if (ssap
== LLCP_SDP_NUM_SAP
) {
519 mutex_unlock(&local
->sdp_lock
);
524 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP
+ ssap
);
526 set_bit(ssap
, &local
->local_sdp
);
528 mutex_unlock(&local
->sdp_lock
);
530 return LLCP_WKS_NUM_SAP
+ ssap
;
533 static int nfc_llcp_build_gb(struct nfc_llcp_local
*local
)
535 u8
*gb_cur
, *version_tlv
, version
, version_length
;
536 u8
*lto_tlv
, lto_length
;
537 u8
*wks_tlv
, wks_length
;
538 u8
*miux_tlv
, miux_length
;
539 __be16 wks
= cpu_to_be16(local
->local_wks
);
543 version
= LLCP_VERSION_11
;
544 version_tlv
= nfc_llcp_build_tlv(LLCP_TLV_VERSION
, &version
,
546 gb_len
+= version_length
;
548 lto_tlv
= nfc_llcp_build_tlv(LLCP_TLV_LTO
, &local
->lto
, 1, <o_length
);
549 gb_len
+= lto_length
;
551 pr_debug("Local wks 0x%lx\n", local
->local_wks
);
552 wks_tlv
= nfc_llcp_build_tlv(LLCP_TLV_WKS
, (u8
*)&wks
, 2, &wks_length
);
553 gb_len
+= wks_length
;
555 miux_tlv
= nfc_llcp_build_tlv(LLCP_TLV_MIUX
, (u8
*)&local
->miux
, 0,
557 gb_len
+= miux_length
;
559 gb_len
+= ARRAY_SIZE(llcp_magic
);
561 if (gb_len
> NFC_MAX_GT_LEN
) {
568 memcpy(gb_cur
, llcp_magic
, ARRAY_SIZE(llcp_magic
));
569 gb_cur
+= ARRAY_SIZE(llcp_magic
);
571 memcpy(gb_cur
, version_tlv
, version_length
);
572 gb_cur
+= version_length
;
574 memcpy(gb_cur
, lto_tlv
, lto_length
);
575 gb_cur
+= lto_length
;
577 memcpy(gb_cur
, wks_tlv
, wks_length
);
578 gb_cur
+= wks_length
;
580 memcpy(gb_cur
, miux_tlv
, miux_length
);
581 gb_cur
+= miux_length
;
583 local
->gb_len
= gb_len
;
594 u8
*nfc_llcp_general_bytes(struct nfc_dev
*dev
, size_t *general_bytes_len
)
596 struct nfc_llcp_local
*local
;
598 local
= nfc_llcp_find_local(dev
);
600 *general_bytes_len
= 0;
604 nfc_llcp_build_gb(local
);
606 *general_bytes_len
= local
->gb_len
;
611 int nfc_llcp_set_remote_gb(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
613 struct nfc_llcp_local
*local
;
615 if (gb_len
< 3 || gb_len
> NFC_MAX_GT_LEN
)
618 local
= nfc_llcp_find_local(dev
);
620 pr_err("No LLCP device\n");
624 memset(local
->remote_gb
, 0, NFC_MAX_GT_LEN
);
625 memcpy(local
->remote_gb
, gb
, gb_len
);
626 local
->remote_gb_len
= gb_len
;
628 if (memcmp(local
->remote_gb
, llcp_magic
, 3)) {
629 pr_err("MAC does not support LLCP\n");
633 return nfc_llcp_parse_gb_tlv(local
,
634 &local
->remote_gb
[3],
635 local
->remote_gb_len
- 3);
638 static u8
nfc_llcp_dsap(struct sk_buff
*pdu
)
640 return (pdu
->data
[0] & 0xfc) >> 2;
643 static u8
nfc_llcp_ptype(struct sk_buff
*pdu
)
645 return ((pdu
->data
[0] & 0x03) << 2) | ((pdu
->data
[1] & 0xc0) >> 6);
648 static u8
nfc_llcp_ssap(struct sk_buff
*pdu
)
650 return pdu
->data
[1] & 0x3f;
653 static u8
nfc_llcp_ns(struct sk_buff
*pdu
)
655 return pdu
->data
[2] >> 4;
658 static u8
nfc_llcp_nr(struct sk_buff
*pdu
)
660 return pdu
->data
[2] & 0xf;
663 static void nfc_llcp_set_nrns(struct nfc_llcp_sock
*sock
, struct sk_buff
*pdu
)
665 pdu
->data
[2] = (sock
->send_n
<< 4) | (sock
->recv_n
);
666 sock
->send_n
= (sock
->send_n
+ 1) % 16;
667 sock
->recv_ack_n
= (sock
->recv_n
- 1) % 16;
670 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local
*local
,
671 struct sk_buff
*skb
, u8 direction
)
673 struct sk_buff
*skb_copy
= NULL
, *nskb
;
677 read_lock(&local
->raw_sockets
.lock
);
679 sk_for_each(sk
, &local
->raw_sockets
.head
) {
680 if (sk
->sk_state
!= LLCP_BOUND
)
683 if (skb_copy
== NULL
) {
684 skb_copy
= __pskb_copy_fclone(skb
, NFC_RAW_HEADER_SIZE
,
687 if (skb_copy
== NULL
)
690 data
= skb_push(skb_copy
, NFC_RAW_HEADER_SIZE
);
692 data
[0] = local
->dev
? local
->dev
->idx
: 0xFF;
693 data
[1] = direction
& 0x01;
694 data
[1] |= (RAW_PAYLOAD_LLCP
<< 1);
697 nskb
= skb_clone(skb_copy
, GFP_ATOMIC
);
701 if (sock_queue_rcv_skb(sk
, nskb
))
705 read_unlock(&local
->raw_sockets
.lock
);
710 static void nfc_llcp_tx_work(struct work_struct
*work
)
712 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
716 struct nfc_llcp_sock
*llcp_sock
;
718 skb
= skb_dequeue(&local
->tx_queue
);
721 llcp_sock
= nfc_llcp_sock(sk
);
723 if (llcp_sock
== NULL
&& nfc_llcp_ptype(skb
) == LLCP_PDU_I
) {
725 nfc_llcp_send_symm(local
->dev
);
726 } else if (llcp_sock
&& !llcp_sock
->remote_ready
) {
727 skb_queue_head(&local
->tx_queue
, skb
);
728 nfc_llcp_send_symm(local
->dev
);
730 struct sk_buff
*copy_skb
= NULL
;
731 u8 ptype
= nfc_llcp_ptype(skb
);
734 pr_debug("Sending pending skb\n");
735 print_hex_dump(KERN_DEBUG
, "LLCP Tx: ",
736 DUMP_PREFIX_OFFSET
, 16, 1,
737 skb
->data
, skb
->len
, true);
739 if (ptype
== LLCP_PDU_DISC
&& sk
!= NULL
&&
740 sk
->sk_state
== LLCP_DISCONNECTING
) {
741 nfc_llcp_sock_unlink(&local
->sockets
, sk
);
746 if (ptype
== LLCP_PDU_I
)
747 copy_skb
= skb_copy(skb
, GFP_ATOMIC
);
749 __net_timestamp(skb
);
751 nfc_llcp_send_to_raw_sock(local
, skb
,
754 ret
= nfc_data_exchange(local
->dev
, local
->target_idx
,
755 skb
, nfc_llcp_recv
, local
);
762 if (ptype
== LLCP_PDU_I
&& copy_skb
)
763 skb_queue_tail(&llcp_sock
->tx_pending_queue
,
767 nfc_llcp_send_symm(local
->dev
);
771 mod_timer(&local
->link_timer
,
772 jiffies
+ msecs_to_jiffies(2 * local
->remote_lto
));
775 static struct nfc_llcp_sock
*nfc_llcp_connecting_sock_get(struct nfc_llcp_local
*local
,
779 struct nfc_llcp_sock
*llcp_sock
;
781 read_lock(&local
->connecting_sockets
.lock
);
783 sk_for_each(sk
, &local
->connecting_sockets
.head
) {
784 llcp_sock
= nfc_llcp_sock(sk
);
786 if (llcp_sock
->ssap
== ssap
) {
787 sock_hold(&llcp_sock
->sk
);
795 read_unlock(&local
->connecting_sockets
.lock
);
800 static struct nfc_llcp_sock
*nfc_llcp_sock_get_sn(struct nfc_llcp_local
*local
,
801 u8
*sn
, size_t sn_len
)
803 struct nfc_llcp_sock
*llcp_sock
;
805 llcp_sock
= nfc_llcp_sock_from_sn(local
, sn
, sn_len
);
807 if (llcp_sock
== NULL
)
810 sock_hold(&llcp_sock
->sk
);
815 static u8
*nfc_llcp_connect_sn(struct sk_buff
*skb
, size_t *sn_len
)
817 u8
*tlv
= &skb
->data
[2], type
, length
;
818 size_t tlv_array_len
= skb
->len
- LLCP_HEADER_SIZE
, offset
= 0;
820 while (offset
< tlv_array_len
) {
824 pr_debug("type 0x%x length %d\n", type
, length
);
826 if (type
== LLCP_TLV_SN
) {
831 offset
+= length
+ 2;
838 static void nfc_llcp_recv_ui(struct nfc_llcp_local
*local
,
841 struct nfc_llcp_sock
*llcp_sock
;
842 struct nfc_llcp_ui_cb
*ui_cb
;
845 dsap
= nfc_llcp_dsap(skb
);
846 ssap
= nfc_llcp_ssap(skb
);
848 ui_cb
= nfc_llcp_ui_skb_cb(skb
);
852 pr_debug("%d %d\n", dsap
, ssap
);
854 /* We're looking for a bound socket, not a client one */
855 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
856 if (llcp_sock
== NULL
|| llcp_sock
->sk
.sk_type
!= SOCK_DGRAM
)
859 /* There is no sequence with UI frames */
860 skb_pull(skb
, LLCP_HEADER_SIZE
);
861 if (!sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
863 * UI frames will be freed from the socket layer, so we
864 * need to keep them alive until someone receives them.
868 pr_err("Receive queue is full\n");
871 nfc_llcp_sock_put(llcp_sock
);
874 static void nfc_llcp_recv_connect(struct nfc_llcp_local
*local
,
877 struct sock
*new_sk
, *parent
;
878 struct nfc_llcp_sock
*sock
, *new_sock
;
879 u8 dsap
, ssap
, reason
;
881 dsap
= nfc_llcp_dsap(skb
);
882 ssap
= nfc_llcp_ssap(skb
);
884 pr_debug("%d %d\n", dsap
, ssap
);
886 if (dsap
!= LLCP_SAP_SDP
) {
887 sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
888 if (sock
== NULL
|| sock
->sk
.sk_state
!= LLCP_LISTEN
) {
889 reason
= LLCP_DM_NOBOUND
;
896 sn
= nfc_llcp_connect_sn(skb
, &sn_len
);
898 reason
= LLCP_DM_NOBOUND
;
902 pr_debug("Service name length %zu\n", sn_len
);
904 sock
= nfc_llcp_sock_get_sn(local
, sn
, sn_len
);
906 reason
= LLCP_DM_NOBOUND
;
911 lock_sock(&sock
->sk
);
915 if (sk_acceptq_is_full(parent
)) {
916 reason
= LLCP_DM_REJ
;
917 release_sock(&sock
->sk
);
922 if (sock
->ssap
== LLCP_SDP_UNBOUND
) {
923 u8 ssap
= nfc_llcp_reserve_sdp_ssap(local
);
925 pr_debug("First client, reserving %d\n", ssap
);
927 if (ssap
== LLCP_SAP_MAX
) {
928 reason
= LLCP_DM_REJ
;
929 release_sock(&sock
->sk
);
937 new_sk
= nfc_llcp_sock_alloc(NULL
, parent
->sk_type
, GFP_ATOMIC
, 0);
938 if (new_sk
== NULL
) {
939 reason
= LLCP_DM_REJ
;
940 release_sock(&sock
->sk
);
945 new_sock
= nfc_llcp_sock(new_sk
);
946 new_sock
->dev
= local
->dev
;
947 new_sock
->local
= nfc_llcp_local_get(local
);
948 new_sock
->rw
= sock
->rw
;
949 new_sock
->miux
= sock
->miux
;
950 new_sock
->nfc_protocol
= sock
->nfc_protocol
;
951 new_sock
->dsap
= ssap
;
952 new_sock
->target_idx
= local
->target_idx
;
953 new_sock
->parent
= parent
;
954 new_sock
->ssap
= sock
->ssap
;
955 if (sock
->ssap
< LLCP_LOCAL_NUM_SAP
&& sock
->ssap
>= LLCP_WKS_NUM_SAP
) {
956 atomic_t
*client_count
;
958 pr_debug("reserved_ssap %d for %p\n", sock
->ssap
, new_sock
);
961 &local
->local_sdp_cnt
[sock
->ssap
- LLCP_WKS_NUM_SAP
];
963 atomic_inc(client_count
);
964 new_sock
->reserved_ssap
= sock
->ssap
;
967 nfc_llcp_parse_connection_tlv(new_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
968 skb
->len
- LLCP_HEADER_SIZE
);
970 pr_debug("new sock %p sk %p\n", new_sock
, &new_sock
->sk
);
972 nfc_llcp_sock_link(&local
->sockets
, new_sk
);
974 nfc_llcp_accept_enqueue(&sock
->sk
, new_sk
);
976 nfc_get_device(local
->dev
->idx
);
978 new_sk
->sk_state
= LLCP_CONNECTED
;
980 /* Wake the listening processes */
981 parent
->sk_data_ready(parent
);
984 nfc_llcp_send_cc(new_sock
);
986 release_sock(&sock
->sk
);
993 nfc_llcp_send_dm(local
, dsap
, ssap
, reason
);
996 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock
*sock
)
999 struct nfc_llcp_local
*local
= sock
->local
;
1001 pr_debug("Remote ready %d tx queue len %d remote rw %d",
1002 sock
->remote_ready
, skb_queue_len(&sock
->tx_pending_queue
),
1005 /* Try to queue some I frames for transmission */
1006 while (sock
->remote_ready
&&
1007 skb_queue_len(&sock
->tx_pending_queue
) < sock
->remote_rw
) {
1008 struct sk_buff
*pdu
;
1010 pdu
= skb_dequeue(&sock
->tx_queue
);
1014 /* Update N(S)/N(R) */
1015 nfc_llcp_set_nrns(sock
, pdu
);
1017 skb_queue_tail(&local
->tx_queue
, pdu
);
1024 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local
*local
,
1025 struct sk_buff
*skb
)
1027 struct nfc_llcp_sock
*llcp_sock
;
1029 u8 dsap
, ssap
, ptype
, ns
, nr
;
1031 ptype
= nfc_llcp_ptype(skb
);
1032 dsap
= nfc_llcp_dsap(skb
);
1033 ssap
= nfc_llcp_ssap(skb
);
1034 ns
= nfc_llcp_ns(skb
);
1035 nr
= nfc_llcp_nr(skb
);
1037 pr_debug("%d %d R %d S %d\n", dsap
, ssap
, nr
, ns
);
1039 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1040 if (llcp_sock
== NULL
) {
1041 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1045 sk
= &llcp_sock
->sk
;
1047 if (sk
->sk_state
== LLCP_CLOSED
) {
1049 nfc_llcp_sock_put(llcp_sock
);
1052 /* Pass the payload upstream */
1053 if (ptype
== LLCP_PDU_I
) {
1054 pr_debug("I frame, queueing on %p\n", &llcp_sock
->sk
);
1056 if (ns
== llcp_sock
->recv_n
)
1057 llcp_sock
->recv_n
= (llcp_sock
->recv_n
+ 1) % 16;
1059 pr_err("Received out of sequence I PDU\n");
1061 skb_pull(skb
, LLCP_HEADER_SIZE
+ LLCP_SEQUENCE_SIZE
);
1062 if (!sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
1064 * I frames will be freed from the socket layer, so we
1065 * need to keep them alive until someone receives them.
1069 pr_err("Receive queue is full\n");
1073 /* Remove skbs from the pending queue */
1074 if (llcp_sock
->send_ack_n
!= nr
) {
1075 struct sk_buff
*s
, *tmp
;
1078 llcp_sock
->send_ack_n
= nr
;
1080 /* Remove and free all skbs until ns == nr */
1081 skb_queue_walk_safe(&llcp_sock
->tx_pending_queue
, s
, tmp
) {
1084 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
1091 /* Re-queue the remaining skbs for transmission */
1092 skb_queue_reverse_walk_safe(&llcp_sock
->tx_pending_queue
,
1094 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
1095 skb_queue_head(&local
->tx_queue
, s
);
1099 if (ptype
== LLCP_PDU_RR
)
1100 llcp_sock
->remote_ready
= true;
1101 else if (ptype
== LLCP_PDU_RNR
)
1102 llcp_sock
->remote_ready
= false;
1104 if (nfc_llcp_queue_i_frames(llcp_sock
) == 0 && ptype
== LLCP_PDU_I
)
1105 nfc_llcp_send_rr(llcp_sock
);
1108 nfc_llcp_sock_put(llcp_sock
);
1111 static void nfc_llcp_recv_disc(struct nfc_llcp_local
*local
,
1112 struct sk_buff
*skb
)
1114 struct nfc_llcp_sock
*llcp_sock
;
1118 dsap
= nfc_llcp_dsap(skb
);
1119 ssap
= nfc_llcp_ssap(skb
);
1121 if ((dsap
== 0) && (ssap
== 0)) {
1122 pr_debug("Connection termination");
1123 nfc_dep_link_down(local
->dev
);
1127 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1128 if (llcp_sock
== NULL
) {
1129 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1133 sk
= &llcp_sock
->sk
;
1136 nfc_llcp_socket_purge(llcp_sock
);
1138 if (sk
->sk_state
== LLCP_CLOSED
) {
1140 nfc_llcp_sock_put(llcp_sock
);
1143 if (sk
->sk_state
== LLCP_CONNECTED
) {
1144 nfc_put_device(local
->dev
);
1145 sk
->sk_state
= LLCP_CLOSED
;
1146 sk
->sk_state_change(sk
);
1149 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_DISC
);
1152 nfc_llcp_sock_put(llcp_sock
);
1155 static void nfc_llcp_recv_cc(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1157 struct nfc_llcp_sock
*llcp_sock
;
1161 dsap
= nfc_llcp_dsap(skb
);
1162 ssap
= nfc_llcp_ssap(skb
);
1164 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
1165 if (llcp_sock
== NULL
) {
1166 pr_err("Invalid CC\n");
1167 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1172 sk
= &llcp_sock
->sk
;
1174 /* Unlink from connecting and link to the client array */
1175 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
1176 nfc_llcp_sock_link(&local
->sockets
, sk
);
1177 llcp_sock
->dsap
= ssap
;
1179 nfc_llcp_parse_connection_tlv(llcp_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
1180 skb
->len
- LLCP_HEADER_SIZE
);
1182 sk
->sk_state
= LLCP_CONNECTED
;
1183 sk
->sk_state_change(sk
);
1185 nfc_llcp_sock_put(llcp_sock
);
1188 static void nfc_llcp_recv_dm(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1190 struct nfc_llcp_sock
*llcp_sock
;
1192 u8 dsap
, ssap
, reason
;
1194 dsap
= nfc_llcp_dsap(skb
);
1195 ssap
= nfc_llcp_ssap(skb
);
1196 reason
= skb
->data
[2];
1198 pr_debug("%d %d reason %d\n", ssap
, dsap
, reason
);
1201 case LLCP_DM_NOBOUND
:
1203 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
1207 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1211 if (llcp_sock
== NULL
) {
1212 pr_debug("Already closed\n");
1216 sk
= &llcp_sock
->sk
;
1219 sk
->sk_state
= LLCP_CLOSED
;
1220 sk
->sk_state_change(sk
);
1222 nfc_llcp_sock_put(llcp_sock
);
1225 static void nfc_llcp_recv_snl(struct nfc_llcp_local
*local
,
1226 struct sk_buff
*skb
)
1228 struct nfc_llcp_sock
*llcp_sock
;
1229 u8 dsap
, ssap
, *tlv
, type
, length
, tid
, sap
;
1230 u16 tlv_len
, offset
;
1232 size_t service_name_len
;
1233 struct nfc_llcp_sdp_tlv
*sdp
;
1234 HLIST_HEAD(llc_sdres_list
);
1235 size_t sdres_tlvs_len
;
1236 HLIST_HEAD(nl_sdres_list
);
1238 dsap
= nfc_llcp_dsap(skb
);
1239 ssap
= nfc_llcp_ssap(skb
);
1241 pr_debug("%d %d\n", dsap
, ssap
);
1243 if (dsap
!= LLCP_SAP_SDP
|| ssap
!= LLCP_SAP_SDP
) {
1244 pr_err("Wrong SNL SAP\n");
1248 tlv
= &skb
->data
[LLCP_HEADER_SIZE
];
1249 tlv_len
= skb
->len
- LLCP_HEADER_SIZE
;
1253 while (offset
< tlv_len
) {
1258 case LLCP_TLV_SDREQ
:
1260 service_name
= (char *) &tlv
[3];
1261 service_name_len
= length
- 1;
1263 pr_debug("Looking for %.16s\n", service_name
);
1265 if (service_name_len
== strlen("urn:nfc:sn:sdp") &&
1266 !strncmp(service_name
, "urn:nfc:sn:sdp",
1267 service_name_len
)) {
1272 llcp_sock
= nfc_llcp_sock_from_sn(local
, service_name
,
1280 * We found a socket but its ssap has not been reserved
1281 * yet. We need to assign it for good and send a reply.
1282 * The ssap will be freed when the socket is closed.
1284 if (llcp_sock
->ssap
== LLCP_SDP_UNBOUND
) {
1285 atomic_t
*client_count
;
1287 sap
= nfc_llcp_reserve_sdp_ssap(local
);
1289 pr_debug("Reserving %d\n", sap
);
1291 if (sap
== LLCP_SAP_MAX
) {
1297 &local
->local_sdp_cnt
[sap
-
1300 atomic_inc(client_count
);
1302 llcp_sock
->ssap
= sap
;
1303 llcp_sock
->reserved_ssap
= sap
;
1305 sap
= llcp_sock
->ssap
;
1308 pr_debug("%p %d\n", llcp_sock
, sap
);
1311 sdp
= nfc_llcp_build_sdres_tlv(tid
, sap
);
1315 sdres_tlvs_len
+= sdp
->tlv_len
;
1316 hlist_add_head(&sdp
->node
, &llc_sdres_list
);
1319 case LLCP_TLV_SDRES
:
1320 mutex_lock(&local
->sdreq_lock
);
1322 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv
[2]);
1324 hlist_for_each_entry(sdp
, &local
->pending_sdreqs
, node
) {
1325 if (sdp
->tid
!= tlv
[2])
1330 pr_debug("Found: uri=%s, sap=%d\n",
1331 sdp
->uri
, sdp
->sap
);
1333 hlist_del(&sdp
->node
);
1335 hlist_add_head(&sdp
->node
, &nl_sdres_list
);
1340 mutex_unlock(&local
->sdreq_lock
);
1344 pr_err("Invalid SNL tlv value 0x%x\n", type
);
1348 offset
+= length
+ 2;
1353 if (!hlist_empty(&nl_sdres_list
))
1354 nfc_genl_llc_send_sdres(local
->dev
, &nl_sdres_list
);
1356 if (!hlist_empty(&llc_sdres_list
))
1357 nfc_llcp_send_snl_sdres(local
, &llc_sdres_list
, sdres_tlvs_len
);
1360 static void nfc_llcp_recv_agf(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1364 struct sk_buff
*new_skb
;
1366 if (skb
->len
<= LLCP_HEADER_SIZE
) {
1367 pr_err("Malformed AGF PDU\n");
1371 skb_pull(skb
, LLCP_HEADER_SIZE
);
1373 while (skb
->len
> LLCP_AGF_PDU_HEADER_SIZE
) {
1374 pdu_len
= skb
->data
[0] << 8 | skb
->data
[1];
1376 skb_pull(skb
, LLCP_AGF_PDU_HEADER_SIZE
);
1378 if (pdu_len
< LLCP_HEADER_SIZE
|| pdu_len
> skb
->len
) {
1379 pr_err("Malformed AGF PDU\n");
1383 ptype
= nfc_llcp_ptype(skb
);
1385 if (ptype
== LLCP_PDU_SYMM
|| ptype
== LLCP_PDU_AGF
)
1388 new_skb
= nfc_alloc_recv_skb(pdu_len
, GFP_KERNEL
);
1389 if (new_skb
== NULL
) {
1390 pr_err("Could not allocate PDU\n");
1394 memcpy(skb_put(new_skb
, pdu_len
), skb
->data
, pdu_len
);
1396 nfc_llcp_rx_skb(local
, new_skb
);
1400 skb_pull(skb
, pdu_len
);
1404 static void nfc_llcp_rx_skb(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1406 u8 dsap
, ssap
, ptype
;
1408 ptype
= nfc_llcp_ptype(skb
);
1409 dsap
= nfc_llcp_dsap(skb
);
1410 ssap
= nfc_llcp_ssap(skb
);
1412 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype
, dsap
, ssap
);
1414 if (ptype
!= LLCP_PDU_SYMM
)
1415 print_hex_dump(KERN_DEBUG
, "LLCP Rx: ", DUMP_PREFIX_OFFSET
,
1416 16, 1, skb
->data
, skb
->len
, true);
1425 nfc_llcp_recv_ui(local
, skb
);
1428 case LLCP_PDU_CONNECT
:
1429 pr_debug("CONNECT\n");
1430 nfc_llcp_recv_connect(local
, skb
);
1435 nfc_llcp_recv_disc(local
, skb
);
1440 nfc_llcp_recv_cc(local
, skb
);
1445 nfc_llcp_recv_dm(local
, skb
);
1450 nfc_llcp_recv_snl(local
, skb
);
1456 pr_debug("I frame\n");
1457 nfc_llcp_recv_hdlc(local
, skb
);
1461 pr_debug("AGF frame\n");
1462 nfc_llcp_recv_agf(local
, skb
);
1467 static void nfc_llcp_rx_work(struct work_struct
*work
)
1469 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
1471 struct sk_buff
*skb
;
1473 skb
= local
->rx_pending
;
1475 pr_debug("No pending SKB\n");
1479 __net_timestamp(skb
);
1481 nfc_llcp_send_to_raw_sock(local
, skb
, NFC_DIRECTION_RX
);
1483 nfc_llcp_rx_skb(local
, skb
);
1485 schedule_work(&local
->tx_work
);
1486 kfree_skb(local
->rx_pending
);
1487 local
->rx_pending
= NULL
;
1490 static void __nfc_llcp_recv(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1492 local
->rx_pending
= skb
;
1493 del_timer(&local
->link_timer
);
1494 schedule_work(&local
->rx_work
);
1497 void nfc_llcp_recv(void *data
, struct sk_buff
*skb
, int err
)
1499 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
1501 pr_debug("Received an LLCP PDU\n");
1503 pr_err("err %d\n", err
);
1507 __nfc_llcp_recv(local
, skb
);
1510 int nfc_llcp_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
1512 struct nfc_llcp_local
*local
;
1514 local
= nfc_llcp_find_local(dev
);
1515 if (local
== NULL
) {
1520 __nfc_llcp_recv(local
, skb
);
1525 void nfc_llcp_mac_is_down(struct nfc_dev
*dev
)
1527 struct nfc_llcp_local
*local
;
1529 local
= nfc_llcp_find_local(dev
);
1533 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1534 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1536 /* Close and purge all existing sockets */
1537 nfc_llcp_socket_release(local
, true, 0);
1540 void nfc_llcp_mac_is_up(struct nfc_dev
*dev
, u32 target_idx
,
1541 u8 comm_mode
, u8 rf_mode
)
1543 struct nfc_llcp_local
*local
;
1545 pr_debug("rf mode %d\n", rf_mode
);
1547 local
= nfc_llcp_find_local(dev
);
1551 local
->target_idx
= target_idx
;
1552 local
->comm_mode
= comm_mode
;
1553 local
->rf_mode
= rf_mode
;
1555 if (rf_mode
== NFC_RF_INITIATOR
) {
1556 pr_debug("Queueing Tx work\n");
1558 schedule_work(&local
->tx_work
);
1560 mod_timer(&local
->link_timer
,
1561 jiffies
+ msecs_to_jiffies(local
->remote_lto
));
1565 int nfc_llcp_register_device(struct nfc_dev
*ndev
)
1567 struct nfc_llcp_local
*local
;
1569 local
= kzalloc(sizeof(struct nfc_llcp_local
), GFP_KERNEL
);
1574 INIT_LIST_HEAD(&local
->list
);
1575 kref_init(&local
->ref
);
1576 mutex_init(&local
->sdp_lock
);
1577 init_timer(&local
->link_timer
);
1578 local
->link_timer
.data
= (unsigned long) local
;
1579 local
->link_timer
.function
= nfc_llcp_symm_timer
;
1581 skb_queue_head_init(&local
->tx_queue
);
1582 INIT_WORK(&local
->tx_work
, nfc_llcp_tx_work
);
1584 local
->rx_pending
= NULL
;
1585 INIT_WORK(&local
->rx_work
, nfc_llcp_rx_work
);
1587 INIT_WORK(&local
->timeout_work
, nfc_llcp_timeout_work
);
1589 rwlock_init(&local
->sockets
.lock
);
1590 rwlock_init(&local
->connecting_sockets
.lock
);
1591 rwlock_init(&local
->raw_sockets
.lock
);
1593 local
->lto
= 150; /* 1500 ms */
1594 local
->rw
= LLCP_MAX_RW
;
1595 local
->miux
= cpu_to_be16(LLCP_MAX_MIUX
);
1596 local
->local_wks
= 0x1; /* LLC Link Management */
1598 nfc_llcp_build_gb(local
);
1600 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1601 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1603 mutex_init(&local
->sdreq_lock
);
1604 INIT_HLIST_HEAD(&local
->pending_sdreqs
);
1605 init_timer(&local
->sdreq_timer
);
1606 local
->sdreq_timer
.data
= (unsigned long) local
;
1607 local
->sdreq_timer
.function
= nfc_llcp_sdreq_timer
;
1608 INIT_WORK(&local
->sdreq_timeout_work
, nfc_llcp_sdreq_timeout_work
);
1610 list_add(&local
->list
, &llcp_devices
);
1615 void nfc_llcp_unregister_device(struct nfc_dev
*dev
)
1617 struct nfc_llcp_local
*local
= nfc_llcp_find_local(dev
);
1619 if (local
== NULL
) {
1620 pr_debug("No such device\n");
1624 local_cleanup(local
);
1626 nfc_llcp_local_put(local
);
1629 int __init
nfc_llcp_init(void)
1631 return nfc_llcp_sock_init();
1634 void nfc_llcp_exit(void)
1636 nfc_llcp_sock_exit();