1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Intel Corporation. All rights reserved.
4 * Copyright (C) 2014 Marvell International Ltd.
7 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/nfc.h>
17 static u8 llcp_magic
[3] = {0x46, 0x66, 0x6d};
19 static LIST_HEAD(llcp_devices
);
20 /* Protects llcp_devices list */
21 static DEFINE_SPINLOCK(llcp_devices_lock
);
23 static void nfc_llcp_rx_skb(struct nfc_llcp_local
*local
, struct sk_buff
*skb
);
25 void nfc_llcp_sock_link(struct llcp_sock_list
*l
, struct sock
*sk
)
28 sk_add_node(sk
, &l
->head
);
29 write_unlock(&l
->lock
);
32 void nfc_llcp_sock_unlink(struct llcp_sock_list
*l
, struct sock
*sk
)
36 write_unlock(&l
->lock
);
39 void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock
*sock
)
41 sock
->remote_rw
= LLCP_DEFAULT_RW
;
42 sock
->remote_miu
= LLCP_MAX_MIU
+ 1;
45 static void nfc_llcp_socket_purge(struct nfc_llcp_sock
*sock
)
47 struct nfc_llcp_local
*local
= sock
->local
;
48 struct sk_buff
*s
, *tmp
;
50 skb_queue_purge(&sock
->tx_queue
);
51 skb_queue_purge(&sock
->tx_pending_queue
);
56 /* Search for local pending SKBs that are related to this socket */
57 skb_queue_walk_safe(&local
->tx_queue
, s
, tmp
) {
58 if (s
->sk
!= &sock
->sk
)
61 skb_unlink(s
, &local
->tx_queue
);
66 static void nfc_llcp_socket_release(struct nfc_llcp_local
*local
, bool device
,
70 struct hlist_node
*tmp
;
71 struct nfc_llcp_sock
*llcp_sock
;
73 skb_queue_purge(&local
->tx_queue
);
75 write_lock(&local
->sockets
.lock
);
77 sk_for_each_safe(sk
, tmp
, &local
->sockets
.head
) {
78 llcp_sock
= nfc_llcp_sock(sk
);
82 nfc_llcp_socket_purge(llcp_sock
);
84 if (sk
->sk_state
== LLCP_CONNECTED
)
85 nfc_put_device(llcp_sock
->dev
);
87 if (sk
->sk_state
== LLCP_LISTEN
) {
88 struct nfc_llcp_sock
*lsk
, *n
;
89 struct sock
*accept_sk
;
91 list_for_each_entry_safe(lsk
, n
,
92 &llcp_sock
->accept_queue
,
95 bh_lock_sock(accept_sk
);
97 nfc_llcp_accept_unlink(accept_sk
);
100 accept_sk
->sk_err
= err
;
101 accept_sk
->sk_state
= LLCP_CLOSED
;
102 accept_sk
->sk_state_change(sk
);
104 bh_unlock_sock(accept_sk
);
110 sk
->sk_state
= LLCP_CLOSED
;
111 sk
->sk_state_change(sk
);
115 sk_del_node_init(sk
);
118 write_unlock(&local
->sockets
.lock
);
120 /* If we still have a device, we keep the RAW sockets alive */
124 write_lock(&local
->raw_sockets
.lock
);
126 sk_for_each_safe(sk
, tmp
, &local
->raw_sockets
.head
) {
127 llcp_sock
= nfc_llcp_sock(sk
);
131 nfc_llcp_socket_purge(llcp_sock
);
135 sk
->sk_state
= LLCP_CLOSED
;
136 sk
->sk_state_change(sk
);
140 sk_del_node_init(sk
);
143 write_unlock(&local
->raw_sockets
.lock
);
146 static struct nfc_llcp_local
*nfc_llcp_local_get(struct nfc_llcp_local
*local
)
148 /* Since using nfc_llcp_local may result in usage of nfc_dev, whenever
149 * we hold a reference to local, we also need to hold a reference to
150 * the device to avoid UAF.
152 if (!nfc_get_device(local
->dev
->idx
))
155 kref_get(&local
->ref
);
160 static void local_cleanup(struct nfc_llcp_local
*local
)
162 nfc_llcp_socket_release(local
, false, ENXIO
);
163 del_timer_sync(&local
->link_timer
);
164 skb_queue_purge(&local
->tx_queue
);
165 cancel_work_sync(&local
->tx_work
);
166 cancel_work_sync(&local
->rx_work
);
167 cancel_work_sync(&local
->timeout_work
);
168 kfree_skb(local
->rx_pending
);
169 local
->rx_pending
= NULL
;
170 del_timer_sync(&local
->sdreq_timer
);
171 cancel_work_sync(&local
->sdreq_timeout_work
);
172 nfc_llcp_free_sdp_tlv_list(&local
->pending_sdreqs
);
175 static void local_release(struct kref
*ref
)
177 struct nfc_llcp_local
*local
;
179 local
= container_of(ref
, struct nfc_llcp_local
, ref
);
181 local_cleanup(local
);
185 int nfc_llcp_local_put(struct nfc_llcp_local
*local
)
195 ret
= kref_put(&local
->ref
, local_release
);
201 static struct nfc_llcp_sock
*nfc_llcp_sock_get(struct nfc_llcp_local
*local
,
205 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
207 pr_debug("ssap dsap %d %d\n", ssap
, dsap
);
209 if (ssap
== 0 && dsap
== 0)
212 read_lock(&local
->sockets
.lock
);
216 sk_for_each(sk
, &local
->sockets
.head
) {
217 tmp_sock
= nfc_llcp_sock(sk
);
219 if (tmp_sock
->ssap
== ssap
&& tmp_sock
->dsap
== dsap
) {
220 llcp_sock
= tmp_sock
;
221 sock_hold(&llcp_sock
->sk
);
226 read_unlock(&local
->sockets
.lock
);
231 static void nfc_llcp_sock_put(struct nfc_llcp_sock
*sock
)
236 static void nfc_llcp_timeout_work(struct work_struct
*work
)
238 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
241 nfc_dep_link_down(local
->dev
);
244 static void nfc_llcp_symm_timer(struct timer_list
*t
)
246 struct nfc_llcp_local
*local
= from_timer(local
, t
, link_timer
);
248 pr_err("SYMM timeout\n");
250 schedule_work(&local
->timeout_work
);
253 static void nfc_llcp_sdreq_timeout_work(struct work_struct
*work
)
256 HLIST_HEAD(nl_sdres_list
);
257 struct hlist_node
*n
;
258 struct nfc_llcp_sdp_tlv
*sdp
;
259 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
262 mutex_lock(&local
->sdreq_lock
);
264 time
= jiffies
- msecs_to_jiffies(3 * local
->remote_lto
);
266 hlist_for_each_entry_safe(sdp
, n
, &local
->pending_sdreqs
, node
) {
267 if (time_after(sdp
->time
, time
))
270 sdp
->sap
= LLCP_SDP_UNBOUND
;
272 hlist_del(&sdp
->node
);
274 hlist_add_head(&sdp
->node
, &nl_sdres_list
);
277 if (!hlist_empty(&local
->pending_sdreqs
))
278 mod_timer(&local
->sdreq_timer
,
279 jiffies
+ msecs_to_jiffies(3 * local
->remote_lto
));
281 mutex_unlock(&local
->sdreq_lock
);
283 if (!hlist_empty(&nl_sdres_list
))
284 nfc_genl_llc_send_sdres(local
->dev
, &nl_sdres_list
);
287 static void nfc_llcp_sdreq_timer(struct timer_list
*t
)
289 struct nfc_llcp_local
*local
= from_timer(local
, t
, sdreq_timer
);
291 schedule_work(&local
->sdreq_timeout_work
);
294 struct nfc_llcp_local
*nfc_llcp_find_local(struct nfc_dev
*dev
)
296 struct nfc_llcp_local
*local
;
297 struct nfc_llcp_local
*res
= NULL
;
299 spin_lock(&llcp_devices_lock
);
300 list_for_each_entry(local
, &llcp_devices
, list
)
301 if (local
->dev
== dev
) {
302 res
= nfc_llcp_local_get(local
);
305 spin_unlock(&llcp_devices_lock
);
310 static struct nfc_llcp_local
*nfc_llcp_remove_local(struct nfc_dev
*dev
)
312 struct nfc_llcp_local
*local
, *tmp
;
314 spin_lock(&llcp_devices_lock
);
315 list_for_each_entry_safe(local
, tmp
, &llcp_devices
, list
)
316 if (local
->dev
== dev
) {
317 list_del(&local
->list
);
318 spin_unlock(&llcp_devices_lock
);
321 spin_unlock(&llcp_devices_lock
);
323 pr_warn("Shutting down device not found\n");
328 static char *wks
[] = {
336 static int nfc_llcp_wks_sap(const char *service_name
, size_t service_name_len
)
340 pr_debug("%s\n", service_name
);
342 if (service_name
== NULL
)
345 num_wks
= ARRAY_SIZE(wks
);
347 for (sap
= 0; sap
< num_wks
; sap
++) {
348 if (wks
[sap
] == NULL
)
351 if (strncmp(wks
[sap
], service_name
, service_name_len
) == 0)
359 struct nfc_llcp_sock
*nfc_llcp_sock_from_sn(struct nfc_llcp_local
*local
,
360 const u8
*sn
, size_t sn_len
,
364 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
366 pr_debug("sn %zd %p\n", sn_len
, sn
);
368 if (sn
== NULL
|| sn_len
== 0)
371 read_lock(&local
->sockets
.lock
);
375 sk_for_each(sk
, &local
->sockets
.head
) {
376 tmp_sock
= nfc_llcp_sock(sk
);
378 pr_debug("llcp sock %p\n", tmp_sock
);
380 if (tmp_sock
->sk
.sk_type
== SOCK_STREAM
&&
381 tmp_sock
->sk
.sk_state
!= LLCP_LISTEN
)
384 if (tmp_sock
->sk
.sk_type
== SOCK_DGRAM
&&
385 tmp_sock
->sk
.sk_state
!= LLCP_BOUND
)
388 if (tmp_sock
->service_name
== NULL
||
389 tmp_sock
->service_name_len
== 0)
392 if (tmp_sock
->service_name_len
!= sn_len
)
395 if (memcmp(sn
, tmp_sock
->service_name
, sn_len
) == 0) {
396 llcp_sock
= tmp_sock
;
398 sock_hold(&llcp_sock
->sk
);
403 read_unlock(&local
->sockets
.lock
);
405 pr_debug("Found llcp sock %p\n", llcp_sock
);
410 u8
nfc_llcp_get_sdp_ssap(struct nfc_llcp_local
*local
,
411 struct nfc_llcp_sock
*sock
)
413 mutex_lock(&local
->sdp_lock
);
415 if (sock
->service_name
!= NULL
&& sock
->service_name_len
> 0) {
416 int ssap
= nfc_llcp_wks_sap(sock
->service_name
,
417 sock
->service_name_len
);
420 pr_debug("WKS %d\n", ssap
);
422 /* This is a WKS, let's check if it's free */
423 if (test_bit(ssap
, &local
->local_wks
)) {
424 mutex_unlock(&local
->sdp_lock
);
429 set_bit(ssap
, &local
->local_wks
);
430 mutex_unlock(&local
->sdp_lock
);
436 * Check if there already is a non WKS socket bound
437 * to this service name.
439 if (nfc_llcp_sock_from_sn(local
, sock
->service_name
,
440 sock
->service_name_len
,
442 mutex_unlock(&local
->sdp_lock
);
447 mutex_unlock(&local
->sdp_lock
);
449 return LLCP_SDP_UNBOUND
;
451 } else if (sock
->ssap
!= 0 && sock
->ssap
< LLCP_WKS_NUM_SAP
) {
452 if (!test_bit(sock
->ssap
, &local
->local_wks
)) {
453 set_bit(sock
->ssap
, &local
->local_wks
);
454 mutex_unlock(&local
->sdp_lock
);
460 mutex_unlock(&local
->sdp_lock
);
465 u8
nfc_llcp_get_local_ssap(struct nfc_llcp_local
*local
)
469 mutex_lock(&local
->sdp_lock
);
471 local_ssap
= find_first_zero_bit(&local
->local_sap
, LLCP_LOCAL_NUM_SAP
);
472 if (local_ssap
== LLCP_LOCAL_NUM_SAP
) {
473 mutex_unlock(&local
->sdp_lock
);
477 set_bit(local_ssap
, &local
->local_sap
);
479 mutex_unlock(&local
->sdp_lock
);
481 return local_ssap
+ LLCP_LOCAL_SAP_OFFSET
;
484 void nfc_llcp_put_ssap(struct nfc_llcp_local
*local
, u8 ssap
)
489 if (ssap
< LLCP_WKS_NUM_SAP
) {
491 sdp
= &local
->local_wks
;
492 } else if (ssap
< LLCP_LOCAL_NUM_SAP
) {
493 atomic_t
*client_cnt
;
495 local_ssap
= ssap
- LLCP_WKS_NUM_SAP
;
496 sdp
= &local
->local_sdp
;
497 client_cnt
= &local
->local_sdp_cnt
[local_ssap
];
499 pr_debug("%d clients\n", atomic_read(client_cnt
));
501 mutex_lock(&local
->sdp_lock
);
503 if (atomic_dec_and_test(client_cnt
)) {
504 struct nfc_llcp_sock
*l_sock
;
506 pr_debug("No more clients for SAP %d\n", ssap
);
508 clear_bit(local_ssap
, sdp
);
510 /* Find the listening sock and set it back to UNBOUND */
511 l_sock
= nfc_llcp_sock_get(local
, ssap
, LLCP_SAP_SDP
);
513 l_sock
->ssap
= LLCP_SDP_UNBOUND
;
514 nfc_llcp_sock_put(l_sock
);
518 mutex_unlock(&local
->sdp_lock
);
521 } else if (ssap
< LLCP_MAX_SAP
) {
522 local_ssap
= ssap
- LLCP_LOCAL_NUM_SAP
;
523 sdp
= &local
->local_sap
;
528 mutex_lock(&local
->sdp_lock
);
530 clear_bit(local_ssap
, sdp
);
532 mutex_unlock(&local
->sdp_lock
);
535 static u8
nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local
*local
)
539 mutex_lock(&local
->sdp_lock
);
541 ssap
= find_first_zero_bit(&local
->local_sdp
, LLCP_SDP_NUM_SAP
);
542 if (ssap
== LLCP_SDP_NUM_SAP
) {
543 mutex_unlock(&local
->sdp_lock
);
548 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP
+ ssap
);
550 set_bit(ssap
, &local
->local_sdp
);
552 mutex_unlock(&local
->sdp_lock
);
554 return LLCP_WKS_NUM_SAP
+ ssap
;
557 static int nfc_llcp_build_gb(struct nfc_llcp_local
*local
)
559 u8
*gb_cur
, version
, version_length
;
560 u8 lto_length
, wks_length
, miux_length
;
561 const u8
*version_tlv
= NULL
, *lto_tlv
= NULL
,
562 *wks_tlv
= NULL
, *miux_tlv
= NULL
;
563 __be16 wks
= cpu_to_be16(local
->local_wks
);
567 version
= LLCP_VERSION_11
;
568 version_tlv
= nfc_llcp_build_tlv(LLCP_TLV_VERSION
, &version
,
574 gb_len
+= version_length
;
576 lto_tlv
= nfc_llcp_build_tlv(LLCP_TLV_LTO
, &local
->lto
, 1, <o_length
);
581 gb_len
+= lto_length
;
583 pr_debug("Local wks 0x%lx\n", local
->local_wks
);
584 wks_tlv
= nfc_llcp_build_tlv(LLCP_TLV_WKS
, (u8
*)&wks
, 2, &wks_length
);
589 gb_len
+= wks_length
;
591 miux_tlv
= nfc_llcp_build_tlv(LLCP_TLV_MIUX
, (u8
*)&local
->miux
, 0,
597 gb_len
+= miux_length
;
599 gb_len
+= ARRAY_SIZE(llcp_magic
);
601 if (gb_len
> NFC_MAX_GT_LEN
) {
608 memcpy(gb_cur
, llcp_magic
, ARRAY_SIZE(llcp_magic
));
609 gb_cur
+= ARRAY_SIZE(llcp_magic
);
611 memcpy(gb_cur
, version_tlv
, version_length
);
612 gb_cur
+= version_length
;
614 memcpy(gb_cur
, lto_tlv
, lto_length
);
615 gb_cur
+= lto_length
;
617 memcpy(gb_cur
, wks_tlv
, wks_length
);
618 gb_cur
+= wks_length
;
620 memcpy(gb_cur
, miux_tlv
, miux_length
);
621 gb_cur
+= miux_length
;
623 local
->gb_len
= gb_len
;
634 u8
*nfc_llcp_general_bytes(struct nfc_dev
*dev
, size_t *general_bytes_len
)
636 struct nfc_llcp_local
*local
;
638 local
= nfc_llcp_find_local(dev
);
640 *general_bytes_len
= 0;
644 nfc_llcp_build_gb(local
);
646 *general_bytes_len
= local
->gb_len
;
648 nfc_llcp_local_put(local
);
653 int nfc_llcp_set_remote_gb(struct nfc_dev
*dev
, const u8
*gb
, u8 gb_len
)
655 struct nfc_llcp_local
*local
;
658 if (gb_len
< 3 || gb_len
> NFC_MAX_GT_LEN
)
661 local
= nfc_llcp_find_local(dev
);
663 pr_err("No LLCP device\n");
667 memset(local
->remote_gb
, 0, NFC_MAX_GT_LEN
);
668 memcpy(local
->remote_gb
, gb
, gb_len
);
669 local
->remote_gb_len
= gb_len
;
671 if (memcmp(local
->remote_gb
, llcp_magic
, 3)) {
672 pr_err("MAC does not support LLCP\n");
677 err
= nfc_llcp_parse_gb_tlv(local
,
678 &local
->remote_gb
[3],
679 local
->remote_gb_len
- 3);
681 nfc_llcp_local_put(local
);
685 static u8
nfc_llcp_dsap(const struct sk_buff
*pdu
)
687 return (pdu
->data
[0] & 0xfc) >> 2;
690 static u8
nfc_llcp_ptype(const struct sk_buff
*pdu
)
692 return ((pdu
->data
[0] & 0x03) << 2) | ((pdu
->data
[1] & 0xc0) >> 6);
695 static u8
nfc_llcp_ssap(const struct sk_buff
*pdu
)
697 return pdu
->data
[1] & 0x3f;
700 static u8
nfc_llcp_ns(const struct sk_buff
*pdu
)
702 return pdu
->data
[2] >> 4;
705 static u8
nfc_llcp_nr(const struct sk_buff
*pdu
)
707 return pdu
->data
[2] & 0xf;
710 static void nfc_llcp_set_nrns(struct nfc_llcp_sock
*sock
, struct sk_buff
*pdu
)
712 pdu
->data
[2] = (sock
->send_n
<< 4) | (sock
->recv_n
);
713 sock
->send_n
= (sock
->send_n
+ 1) % 16;
714 sock
->recv_ack_n
= (sock
->recv_n
- 1) % 16;
717 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local
*local
,
718 struct sk_buff
*skb
, u8 direction
)
720 struct sk_buff
*skb_copy
= NULL
, *nskb
;
724 read_lock(&local
->raw_sockets
.lock
);
726 sk_for_each(sk
, &local
->raw_sockets
.head
) {
727 if (sk
->sk_state
!= LLCP_BOUND
)
730 if (skb_copy
== NULL
) {
731 skb_copy
= __pskb_copy_fclone(skb
, NFC_RAW_HEADER_SIZE
,
734 if (skb_copy
== NULL
)
737 data
= skb_push(skb_copy
, NFC_RAW_HEADER_SIZE
);
739 data
[0] = local
->dev
? local
->dev
->idx
: 0xFF;
740 data
[1] = direction
& 0x01;
741 data
[1] |= (RAW_PAYLOAD_LLCP
<< 1);
744 nskb
= skb_clone(skb_copy
, GFP_ATOMIC
);
748 if (sock_queue_rcv_skb(sk
, nskb
))
752 read_unlock(&local
->raw_sockets
.lock
);
757 static void nfc_llcp_tx_work(struct work_struct
*work
)
759 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
763 struct nfc_llcp_sock
*llcp_sock
;
765 skb
= skb_dequeue(&local
->tx_queue
);
768 llcp_sock
= nfc_llcp_sock(sk
);
770 if (llcp_sock
== NULL
&& nfc_llcp_ptype(skb
) == LLCP_PDU_I
) {
772 nfc_llcp_send_symm(local
->dev
);
773 } else if (llcp_sock
&& !llcp_sock
->remote_ready
) {
774 skb_queue_head(&local
->tx_queue
, skb
);
775 nfc_llcp_send_symm(local
->dev
);
777 struct sk_buff
*copy_skb
= NULL
;
778 u8 ptype
= nfc_llcp_ptype(skb
);
781 pr_debug("Sending pending skb\n");
782 print_hex_dump_debug("LLCP Tx: ", DUMP_PREFIX_OFFSET
,
783 16, 1, skb
->data
, skb
->len
, true);
785 if (ptype
== LLCP_PDU_I
)
786 copy_skb
= skb_copy(skb
, GFP_ATOMIC
);
788 __net_timestamp(skb
);
790 nfc_llcp_send_to_raw_sock(local
, skb
,
793 ret
= nfc_data_exchange(local
->dev
, local
->target_idx
,
794 skb
, nfc_llcp_recv
, local
);
801 if (ptype
== LLCP_PDU_I
&& copy_skb
)
802 skb_queue_tail(&llcp_sock
->tx_pending_queue
,
806 nfc_llcp_send_symm(local
->dev
);
810 mod_timer(&local
->link_timer
,
811 jiffies
+ msecs_to_jiffies(2 * local
->remote_lto
));
814 static struct nfc_llcp_sock
*nfc_llcp_connecting_sock_get(struct nfc_llcp_local
*local
,
818 struct nfc_llcp_sock
*llcp_sock
;
820 read_lock(&local
->connecting_sockets
.lock
);
822 sk_for_each(sk
, &local
->connecting_sockets
.head
) {
823 llcp_sock
= nfc_llcp_sock(sk
);
825 if (llcp_sock
->ssap
== ssap
) {
826 sock_hold(&llcp_sock
->sk
);
834 read_unlock(&local
->connecting_sockets
.lock
);
839 static struct nfc_llcp_sock
*nfc_llcp_sock_get_sn(struct nfc_llcp_local
*local
,
840 const u8
*sn
, size_t sn_len
)
842 return nfc_llcp_sock_from_sn(local
, sn
, sn_len
, true);
845 static const u8
*nfc_llcp_connect_sn(const struct sk_buff
*skb
, size_t *sn_len
)
848 const u8
*tlv
= &skb
->data
[2];
849 size_t tlv_array_len
= skb
->len
- LLCP_HEADER_SIZE
, offset
= 0;
851 while (offset
< tlv_array_len
) {
855 pr_debug("type 0x%x length %d\n", type
, length
);
857 if (type
== LLCP_TLV_SN
) {
862 offset
+= length
+ 2;
869 static void nfc_llcp_recv_ui(struct nfc_llcp_local
*local
,
872 struct nfc_llcp_sock
*llcp_sock
;
873 struct nfc_llcp_ui_cb
*ui_cb
;
876 dsap
= nfc_llcp_dsap(skb
);
877 ssap
= nfc_llcp_ssap(skb
);
879 ui_cb
= nfc_llcp_ui_skb_cb(skb
);
883 pr_debug("%d %d\n", dsap
, ssap
);
885 /* We're looking for a bound socket, not a client one */
886 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
887 if (llcp_sock
== NULL
|| llcp_sock
->sk
.sk_type
!= SOCK_DGRAM
)
890 /* There is no sequence with UI frames */
891 skb_pull(skb
, LLCP_HEADER_SIZE
);
892 if (!sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
894 * UI frames will be freed from the socket layer, so we
895 * need to keep them alive until someone receives them.
899 pr_err("Receive queue is full\n");
902 nfc_llcp_sock_put(llcp_sock
);
905 static void nfc_llcp_recv_connect(struct nfc_llcp_local
*local
,
906 const struct sk_buff
*skb
)
908 struct sock
*new_sk
, *parent
;
909 struct nfc_llcp_sock
*sock
, *new_sock
;
910 u8 dsap
, ssap
, reason
;
912 dsap
= nfc_llcp_dsap(skb
);
913 ssap
= nfc_llcp_ssap(skb
);
915 pr_debug("%d %d\n", dsap
, ssap
);
917 if (dsap
!= LLCP_SAP_SDP
) {
918 sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
919 if (sock
== NULL
|| sock
->sk
.sk_state
!= LLCP_LISTEN
) {
920 reason
= LLCP_DM_NOBOUND
;
927 sn
= nfc_llcp_connect_sn(skb
, &sn_len
);
929 reason
= LLCP_DM_NOBOUND
;
933 pr_debug("Service name length %zu\n", sn_len
);
935 sock
= nfc_llcp_sock_get_sn(local
, sn
, sn_len
);
937 reason
= LLCP_DM_NOBOUND
;
942 lock_sock(&sock
->sk
);
946 if (sk_acceptq_is_full(parent
)) {
947 reason
= LLCP_DM_REJ
;
948 release_sock(&sock
->sk
);
953 if (sock
->ssap
== LLCP_SDP_UNBOUND
) {
954 u8 ssap
= nfc_llcp_reserve_sdp_ssap(local
);
956 pr_debug("First client, reserving %d\n", ssap
);
958 if (ssap
== LLCP_SAP_MAX
) {
959 reason
= LLCP_DM_REJ
;
960 release_sock(&sock
->sk
);
968 new_sk
= nfc_llcp_sock_alloc(NULL
, parent
->sk_type
, GFP_ATOMIC
, 0);
969 if (new_sk
== NULL
) {
970 reason
= LLCP_DM_REJ
;
971 release_sock(&sock
->sk
);
976 new_sock
= nfc_llcp_sock(new_sk
);
978 new_sock
->local
= nfc_llcp_local_get(local
);
979 if (!new_sock
->local
) {
980 reason
= LLCP_DM_REJ
;
981 sock_put(&new_sock
->sk
);
982 release_sock(&sock
->sk
);
987 new_sock
->dev
= local
->dev
;
988 new_sock
->rw
= sock
->rw
;
989 new_sock
->miux
= sock
->miux
;
990 new_sock
->nfc_protocol
= sock
->nfc_protocol
;
991 new_sock
->dsap
= ssap
;
992 new_sock
->target_idx
= local
->target_idx
;
993 new_sock
->parent
= parent
;
994 new_sock
->ssap
= sock
->ssap
;
995 if (sock
->ssap
< LLCP_LOCAL_NUM_SAP
&& sock
->ssap
>= LLCP_WKS_NUM_SAP
) {
996 atomic_t
*client_count
;
998 pr_debug("reserved_ssap %d for %p\n", sock
->ssap
, new_sock
);
1001 &local
->local_sdp_cnt
[sock
->ssap
- LLCP_WKS_NUM_SAP
];
1003 atomic_inc(client_count
);
1004 new_sock
->reserved_ssap
= sock
->ssap
;
1007 nfc_llcp_parse_connection_tlv(new_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
1008 skb
->len
- LLCP_HEADER_SIZE
);
1010 pr_debug("new sock %p sk %p\n", new_sock
, &new_sock
->sk
);
1012 nfc_llcp_sock_link(&local
->sockets
, new_sk
);
1014 nfc_llcp_accept_enqueue(&sock
->sk
, new_sk
);
1016 nfc_get_device(local
->dev
->idx
);
1018 new_sk
->sk_state
= LLCP_CONNECTED
;
1020 /* Wake the listening processes */
1021 parent
->sk_data_ready(parent
);
1024 nfc_llcp_send_cc(new_sock
);
1026 release_sock(&sock
->sk
);
1027 sock_put(&sock
->sk
);
1033 nfc_llcp_send_dm(local
, dsap
, ssap
, reason
);
1036 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock
*sock
)
1039 struct nfc_llcp_local
*local
= sock
->local
;
1041 pr_debug("Remote ready %d tx queue len %d remote rw %d",
1042 sock
->remote_ready
, skb_queue_len(&sock
->tx_pending_queue
),
1045 /* Try to queue some I frames for transmission */
1046 while (sock
->remote_ready
&&
1047 skb_queue_len(&sock
->tx_pending_queue
) < sock
->remote_rw
) {
1048 struct sk_buff
*pdu
;
1050 pdu
= skb_dequeue(&sock
->tx_queue
);
1054 /* Update N(S)/N(R) */
1055 nfc_llcp_set_nrns(sock
, pdu
);
1057 skb_queue_tail(&local
->tx_queue
, pdu
);
1064 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local
*local
,
1065 struct sk_buff
*skb
)
1067 struct nfc_llcp_sock
*llcp_sock
;
1069 u8 dsap
, ssap
, ptype
, ns
, nr
;
1071 ptype
= nfc_llcp_ptype(skb
);
1072 dsap
= nfc_llcp_dsap(skb
);
1073 ssap
= nfc_llcp_ssap(skb
);
1074 ns
= nfc_llcp_ns(skb
);
1075 nr
= nfc_llcp_nr(skb
);
1077 pr_debug("%d %d R %d S %d\n", dsap
, ssap
, nr
, ns
);
1079 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1080 if (llcp_sock
== NULL
) {
1081 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1085 sk
= &llcp_sock
->sk
;
1087 if (sk
->sk_state
== LLCP_CLOSED
) {
1089 nfc_llcp_sock_put(llcp_sock
);
1092 /* Pass the payload upstream */
1093 if (ptype
== LLCP_PDU_I
) {
1094 pr_debug("I frame, queueing on %p\n", &llcp_sock
->sk
);
1096 if (ns
== llcp_sock
->recv_n
)
1097 llcp_sock
->recv_n
= (llcp_sock
->recv_n
+ 1) % 16;
1099 pr_err("Received out of sequence I PDU\n");
1101 skb_pull(skb
, LLCP_HEADER_SIZE
+ LLCP_SEQUENCE_SIZE
);
1102 if (!sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
1104 * I frames will be freed from the socket layer, so we
1105 * need to keep them alive until someone receives them.
1109 pr_err("Receive queue is full\n");
1113 /* Remove skbs from the pending queue */
1114 if (llcp_sock
->send_ack_n
!= nr
) {
1115 struct sk_buff
*s
, *tmp
;
1118 llcp_sock
->send_ack_n
= nr
;
1120 /* Remove and free all skbs until ns == nr */
1121 skb_queue_walk_safe(&llcp_sock
->tx_pending_queue
, s
, tmp
) {
1124 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
1131 /* Re-queue the remaining skbs for transmission */
1132 skb_queue_reverse_walk_safe(&llcp_sock
->tx_pending_queue
,
1134 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
1135 skb_queue_head(&local
->tx_queue
, s
);
1139 if (ptype
== LLCP_PDU_RR
)
1140 llcp_sock
->remote_ready
= true;
1141 else if (ptype
== LLCP_PDU_RNR
)
1142 llcp_sock
->remote_ready
= false;
1144 if (nfc_llcp_queue_i_frames(llcp_sock
) == 0 && ptype
== LLCP_PDU_I
)
1145 nfc_llcp_send_rr(llcp_sock
);
1148 nfc_llcp_sock_put(llcp_sock
);
1151 static void nfc_llcp_recv_disc(struct nfc_llcp_local
*local
,
1152 const struct sk_buff
*skb
)
1154 struct nfc_llcp_sock
*llcp_sock
;
1158 dsap
= nfc_llcp_dsap(skb
);
1159 ssap
= nfc_llcp_ssap(skb
);
1161 if ((dsap
== 0) && (ssap
== 0)) {
1162 pr_debug("Connection termination");
1163 nfc_dep_link_down(local
->dev
);
1167 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1168 if (llcp_sock
== NULL
) {
1169 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1173 sk
= &llcp_sock
->sk
;
1176 nfc_llcp_socket_purge(llcp_sock
);
1178 if (sk
->sk_state
== LLCP_CLOSED
) {
1180 nfc_llcp_sock_put(llcp_sock
);
1183 if (sk
->sk_state
== LLCP_CONNECTED
) {
1184 nfc_put_device(local
->dev
);
1185 sk
->sk_state
= LLCP_CLOSED
;
1186 sk
->sk_state_change(sk
);
1189 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_DISC
);
1192 nfc_llcp_sock_put(llcp_sock
);
1195 static void nfc_llcp_recv_cc(struct nfc_llcp_local
*local
,
1196 const struct sk_buff
*skb
)
1198 struct nfc_llcp_sock
*llcp_sock
;
1202 dsap
= nfc_llcp_dsap(skb
);
1203 ssap
= nfc_llcp_ssap(skb
);
1205 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
1206 if (llcp_sock
== NULL
) {
1207 pr_err("Invalid CC\n");
1208 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1213 sk
= &llcp_sock
->sk
;
1215 /* Unlink from connecting and link to the client array */
1216 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
1217 nfc_llcp_sock_link(&local
->sockets
, sk
);
1218 llcp_sock
->dsap
= ssap
;
1220 nfc_llcp_parse_connection_tlv(llcp_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
1221 skb
->len
- LLCP_HEADER_SIZE
);
1223 sk
->sk_state
= LLCP_CONNECTED
;
1224 sk
->sk_state_change(sk
);
1226 nfc_llcp_sock_put(llcp_sock
);
1229 static void nfc_llcp_recv_dm(struct nfc_llcp_local
*local
,
1230 const struct sk_buff
*skb
)
1232 struct nfc_llcp_sock
*llcp_sock
;
1234 u8 dsap
, ssap
, reason
;
1236 dsap
= nfc_llcp_dsap(skb
);
1237 ssap
= nfc_llcp_ssap(skb
);
1238 reason
= skb
->data
[2];
1240 pr_debug("%d %d reason %d\n", ssap
, dsap
, reason
);
1243 case LLCP_DM_NOBOUND
:
1245 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
1249 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1253 if (llcp_sock
== NULL
) {
1254 pr_debug("Already closed\n");
1258 sk
= &llcp_sock
->sk
;
1261 sk
->sk_state
= LLCP_CLOSED
;
1262 sk
->sk_state_change(sk
);
1264 nfc_llcp_sock_put(llcp_sock
);
1267 static void nfc_llcp_recv_snl(struct nfc_llcp_local
*local
,
1268 const struct sk_buff
*skb
)
1270 struct nfc_llcp_sock
*llcp_sock
;
1271 u8 dsap
, ssap
, type
, length
, tid
, sap
;
1273 u16 tlv_len
, offset
;
1274 const char *service_name
;
1275 size_t service_name_len
;
1276 struct nfc_llcp_sdp_tlv
*sdp
;
1277 HLIST_HEAD(llc_sdres_list
);
1278 size_t sdres_tlvs_len
;
1279 HLIST_HEAD(nl_sdres_list
);
1281 dsap
= nfc_llcp_dsap(skb
);
1282 ssap
= nfc_llcp_ssap(skb
);
1284 pr_debug("%d %d\n", dsap
, ssap
);
1286 if (dsap
!= LLCP_SAP_SDP
|| ssap
!= LLCP_SAP_SDP
) {
1287 pr_err("Wrong SNL SAP\n");
1291 tlv
= &skb
->data
[LLCP_HEADER_SIZE
];
1292 tlv_len
= skb
->len
- LLCP_HEADER_SIZE
;
1296 while (offset
< tlv_len
) {
1301 case LLCP_TLV_SDREQ
:
1303 service_name
= (char *) &tlv
[3];
1304 service_name_len
= length
- 1;
1306 pr_debug("Looking for %.16s\n", service_name
);
1308 if (service_name_len
== strlen("urn:nfc:sn:sdp") &&
1309 !strncmp(service_name
, "urn:nfc:sn:sdp",
1310 service_name_len
)) {
1315 llcp_sock
= nfc_llcp_sock_from_sn(local
, service_name
,
1324 * We found a socket but its ssap has not been reserved
1325 * yet. We need to assign it for good and send a reply.
1326 * The ssap will be freed when the socket is closed.
1328 if (llcp_sock
->ssap
== LLCP_SDP_UNBOUND
) {
1329 atomic_t
*client_count
;
1331 sap
= nfc_llcp_reserve_sdp_ssap(local
);
1333 pr_debug("Reserving %d\n", sap
);
1335 if (sap
== LLCP_SAP_MAX
) {
1337 nfc_llcp_sock_put(llcp_sock
);
1342 &local
->local_sdp_cnt
[sap
-
1345 atomic_inc(client_count
);
1347 llcp_sock
->ssap
= sap
;
1348 llcp_sock
->reserved_ssap
= sap
;
1350 sap
= llcp_sock
->ssap
;
1353 pr_debug("%p %d\n", llcp_sock
, sap
);
1355 nfc_llcp_sock_put(llcp_sock
);
1357 sdp
= nfc_llcp_build_sdres_tlv(tid
, sap
);
1361 sdres_tlvs_len
+= sdp
->tlv_len
;
1362 hlist_add_head(&sdp
->node
, &llc_sdres_list
);
1365 case LLCP_TLV_SDRES
:
1366 mutex_lock(&local
->sdreq_lock
);
1368 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv
[2]);
1370 hlist_for_each_entry(sdp
, &local
->pending_sdreqs
, node
) {
1371 if (sdp
->tid
!= tlv
[2])
1376 pr_debug("Found: uri=%s, sap=%d\n",
1377 sdp
->uri
, sdp
->sap
);
1379 hlist_del(&sdp
->node
);
1381 hlist_add_head(&sdp
->node
, &nl_sdres_list
);
1386 mutex_unlock(&local
->sdreq_lock
);
1390 pr_err("Invalid SNL tlv value 0x%x\n", type
);
1394 offset
+= length
+ 2;
1399 if (!hlist_empty(&nl_sdres_list
))
1400 nfc_genl_llc_send_sdres(local
->dev
, &nl_sdres_list
);
1402 if (!hlist_empty(&llc_sdres_list
))
1403 nfc_llcp_send_snl_sdres(local
, &llc_sdres_list
, sdres_tlvs_len
);
1406 static void nfc_llcp_recv_agf(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1410 struct sk_buff
*new_skb
;
1412 if (skb
->len
<= LLCP_HEADER_SIZE
) {
1413 pr_err("Malformed AGF PDU\n");
1417 skb_pull(skb
, LLCP_HEADER_SIZE
);
1419 while (skb
->len
> LLCP_AGF_PDU_HEADER_SIZE
) {
1420 pdu_len
= skb
->data
[0] << 8 | skb
->data
[1];
1422 skb_pull(skb
, LLCP_AGF_PDU_HEADER_SIZE
);
1424 if (pdu_len
< LLCP_HEADER_SIZE
|| pdu_len
> skb
->len
) {
1425 pr_err("Malformed AGF PDU\n");
1429 ptype
= nfc_llcp_ptype(skb
);
1431 if (ptype
== LLCP_PDU_SYMM
|| ptype
== LLCP_PDU_AGF
)
1434 new_skb
= nfc_alloc_recv_skb(pdu_len
, GFP_KERNEL
);
1435 if (new_skb
== NULL
) {
1436 pr_err("Could not allocate PDU\n");
1440 skb_put_data(new_skb
, skb
->data
, pdu_len
);
1442 nfc_llcp_rx_skb(local
, new_skb
);
1446 skb_pull(skb
, pdu_len
);
1450 static void nfc_llcp_rx_skb(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1452 u8 dsap
, ssap
, ptype
;
1454 ptype
= nfc_llcp_ptype(skb
);
1455 dsap
= nfc_llcp_dsap(skb
);
1456 ssap
= nfc_llcp_ssap(skb
);
1458 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype
, dsap
, ssap
);
1460 if (ptype
!= LLCP_PDU_SYMM
)
1461 print_hex_dump_debug("LLCP Rx: ", DUMP_PREFIX_OFFSET
, 16, 1,
1462 skb
->data
, skb
->len
, true);
1471 nfc_llcp_recv_ui(local
, skb
);
1474 case LLCP_PDU_CONNECT
:
1475 pr_debug("CONNECT\n");
1476 nfc_llcp_recv_connect(local
, skb
);
1481 nfc_llcp_recv_disc(local
, skb
);
1486 nfc_llcp_recv_cc(local
, skb
);
1491 nfc_llcp_recv_dm(local
, skb
);
1496 nfc_llcp_recv_snl(local
, skb
);
1502 pr_debug("I frame\n");
1503 nfc_llcp_recv_hdlc(local
, skb
);
1507 pr_debug("AGF frame\n");
1508 nfc_llcp_recv_agf(local
, skb
);
1513 static void nfc_llcp_rx_work(struct work_struct
*work
)
1515 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
1517 struct sk_buff
*skb
;
1519 skb
= local
->rx_pending
;
1521 pr_debug("No pending SKB\n");
1525 __net_timestamp(skb
);
1527 nfc_llcp_send_to_raw_sock(local
, skb
, NFC_DIRECTION_RX
);
1529 nfc_llcp_rx_skb(local
, skb
);
1531 schedule_work(&local
->tx_work
);
1532 kfree_skb(local
->rx_pending
);
1533 local
->rx_pending
= NULL
;
1536 static void __nfc_llcp_recv(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1538 local
->rx_pending
= skb
;
1539 del_timer(&local
->link_timer
);
1540 schedule_work(&local
->rx_work
);
1543 void nfc_llcp_recv(void *data
, struct sk_buff
*skb
, int err
)
1545 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
1548 pr_err("LLCP PDU receive err %d\n", err
);
1552 __nfc_llcp_recv(local
, skb
);
1555 int nfc_llcp_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
1557 struct nfc_llcp_local
*local
;
1559 local
= nfc_llcp_find_local(dev
);
1560 if (local
== NULL
) {
1565 __nfc_llcp_recv(local
, skb
);
1567 nfc_llcp_local_put(local
);
1572 void nfc_llcp_mac_is_down(struct nfc_dev
*dev
)
1574 struct nfc_llcp_local
*local
;
1576 local
= nfc_llcp_find_local(dev
);
1580 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1581 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1583 /* Close and purge all existing sockets */
1584 nfc_llcp_socket_release(local
, true, 0);
1586 nfc_llcp_local_put(local
);
1589 void nfc_llcp_mac_is_up(struct nfc_dev
*dev
, u32 target_idx
,
1590 u8 comm_mode
, u8 rf_mode
)
1592 struct nfc_llcp_local
*local
;
1594 pr_debug("rf mode %d\n", rf_mode
);
1596 local
= nfc_llcp_find_local(dev
);
1600 local
->target_idx
= target_idx
;
1601 local
->comm_mode
= comm_mode
;
1602 local
->rf_mode
= rf_mode
;
1604 if (rf_mode
== NFC_RF_INITIATOR
) {
1605 pr_debug("Queueing Tx work\n");
1607 schedule_work(&local
->tx_work
);
1609 mod_timer(&local
->link_timer
,
1610 jiffies
+ msecs_to_jiffies(local
->remote_lto
));
1613 nfc_llcp_local_put(local
);
1616 int nfc_llcp_register_device(struct nfc_dev
*ndev
)
1618 struct nfc_llcp_local
*local
;
1620 local
= kzalloc(sizeof(struct nfc_llcp_local
), GFP_KERNEL
);
1624 /* As we are going to initialize local's refcount, we need to get the
1625 * nfc_dev to avoid UAF, otherwise there is no point in continuing.
1626 * See nfc_llcp_local_get().
1628 local
->dev
= nfc_get_device(ndev
->idx
);
1634 INIT_LIST_HEAD(&local
->list
);
1635 kref_init(&local
->ref
);
1636 mutex_init(&local
->sdp_lock
);
1637 timer_setup(&local
->link_timer
, nfc_llcp_symm_timer
, 0);
1639 skb_queue_head_init(&local
->tx_queue
);
1640 INIT_WORK(&local
->tx_work
, nfc_llcp_tx_work
);
1642 local
->rx_pending
= NULL
;
1643 INIT_WORK(&local
->rx_work
, nfc_llcp_rx_work
);
1645 INIT_WORK(&local
->timeout_work
, nfc_llcp_timeout_work
);
1647 rwlock_init(&local
->sockets
.lock
);
1648 rwlock_init(&local
->connecting_sockets
.lock
);
1649 rwlock_init(&local
->raw_sockets
.lock
);
1651 local
->lto
= 150; /* 1500 ms */
1652 local
->rw
= LLCP_MAX_RW
;
1653 local
->miux
= cpu_to_be16(LLCP_MAX_MIUX
);
1654 local
->local_wks
= 0x1; /* LLC Link Management */
1656 nfc_llcp_build_gb(local
);
1658 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1659 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1661 mutex_init(&local
->sdreq_lock
);
1662 INIT_HLIST_HEAD(&local
->pending_sdreqs
);
1663 timer_setup(&local
->sdreq_timer
, nfc_llcp_sdreq_timer
, 0);
1664 INIT_WORK(&local
->sdreq_timeout_work
, nfc_llcp_sdreq_timeout_work
);
1666 spin_lock(&llcp_devices_lock
);
1667 list_add(&local
->list
, &llcp_devices
);
1668 spin_unlock(&llcp_devices_lock
);
1673 void nfc_llcp_unregister_device(struct nfc_dev
*dev
)
1675 struct nfc_llcp_local
*local
= nfc_llcp_remove_local(dev
);
1677 if (local
== NULL
) {
1678 pr_debug("No such device\n");
1682 local_cleanup(local
);
1684 nfc_llcp_local_put(local
);
1687 int __init
nfc_llcp_init(void)
1689 return nfc_llcp_sock_init();
1692 void nfc_llcp_exit(void)
1694 nfc_llcp_sock_exit();