2 * Copyright (C) 2011 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/nfc.h>
28 static u8 llcp_magic
[3] = {0x46, 0x66, 0x6d};
30 static struct list_head llcp_devices
;
32 static void nfc_llcp_rx_skb(struct nfc_llcp_local
*local
, struct sk_buff
*skb
);
34 void nfc_llcp_sock_link(struct llcp_sock_list
*l
, struct sock
*sk
)
37 sk_add_node(sk
, &l
->head
);
38 write_unlock(&l
->lock
);
41 void nfc_llcp_sock_unlink(struct llcp_sock_list
*l
, struct sock
*sk
)
45 write_unlock(&l
->lock
);
48 void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock
*sock
)
50 sock
->remote_rw
= LLCP_DEFAULT_RW
;
51 sock
->remote_miu
= LLCP_MAX_MIU
+ 1;
54 static void nfc_llcp_socket_purge(struct nfc_llcp_sock
*sock
)
56 struct nfc_llcp_local
*local
= sock
->local
;
57 struct sk_buff
*s
, *tmp
;
59 pr_debug("%p\n", &sock
->sk
);
61 skb_queue_purge(&sock
->tx_queue
);
62 skb_queue_purge(&sock
->tx_pending_queue
);
67 /* Search for local pending SKBs that are related to this socket */
68 skb_queue_walk_safe(&local
->tx_queue
, s
, tmp
) {
69 if (s
->sk
!= &sock
->sk
)
72 skb_unlink(s
, &local
->tx_queue
);
77 static void nfc_llcp_socket_release(struct nfc_llcp_local
*local
, bool device
,
81 struct hlist_node
*tmp
;
82 struct nfc_llcp_sock
*llcp_sock
;
84 skb_queue_purge(&local
->tx_queue
);
86 write_lock(&local
->sockets
.lock
);
88 sk_for_each_safe(sk
, tmp
, &local
->sockets
.head
) {
89 llcp_sock
= nfc_llcp_sock(sk
);
93 nfc_llcp_socket_purge(llcp_sock
);
95 if (sk
->sk_state
== LLCP_CONNECTED
)
96 nfc_put_device(llcp_sock
->dev
);
98 if (sk
->sk_state
== LLCP_LISTEN
) {
99 struct nfc_llcp_sock
*lsk
, *n
;
100 struct sock
*accept_sk
;
102 list_for_each_entry_safe(lsk
, n
,
103 &llcp_sock
->accept_queue
,
105 accept_sk
= &lsk
->sk
;
106 bh_lock_sock(accept_sk
);
108 nfc_llcp_accept_unlink(accept_sk
);
111 accept_sk
->sk_err
= err
;
112 accept_sk
->sk_state
= LLCP_CLOSED
;
113 accept_sk
->sk_state_change(sk
);
115 bh_unlock_sock(accept_sk
);
121 sk
->sk_state
= LLCP_CLOSED
;
122 sk
->sk_state_change(sk
);
126 sk_del_node_init(sk
);
129 write_unlock(&local
->sockets
.lock
);
131 /* If we still have a device, we keep the RAW sockets alive */
135 write_lock(&local
->raw_sockets
.lock
);
137 sk_for_each_safe(sk
, tmp
, &local
->raw_sockets
.head
) {
138 llcp_sock
= nfc_llcp_sock(sk
);
142 nfc_llcp_socket_purge(llcp_sock
);
146 sk
->sk_state
= LLCP_CLOSED
;
147 sk
->sk_state_change(sk
);
151 sk_del_node_init(sk
);
154 write_unlock(&local
->raw_sockets
.lock
);
157 struct nfc_llcp_local
*nfc_llcp_local_get(struct nfc_llcp_local
*local
)
159 kref_get(&local
->ref
);
164 static void local_cleanup(struct nfc_llcp_local
*local
)
166 nfc_llcp_socket_release(local
, false, ENXIO
);
167 del_timer_sync(&local
->link_timer
);
168 skb_queue_purge(&local
->tx_queue
);
169 cancel_work_sync(&local
->tx_work
);
170 cancel_work_sync(&local
->rx_work
);
171 cancel_work_sync(&local
->timeout_work
);
172 kfree_skb(local
->rx_pending
);
173 del_timer_sync(&local
->sdreq_timer
);
174 cancel_work_sync(&local
->sdreq_timeout_work
);
175 nfc_llcp_free_sdp_tlv_list(&local
->pending_sdreqs
);
178 static void local_release(struct kref
*ref
)
180 struct nfc_llcp_local
*local
;
182 local
= container_of(ref
, struct nfc_llcp_local
, ref
);
184 list_del(&local
->list
);
185 local_cleanup(local
);
189 int nfc_llcp_local_put(struct nfc_llcp_local
*local
)
194 return kref_put(&local
->ref
, local_release
);
197 static struct nfc_llcp_sock
*nfc_llcp_sock_get(struct nfc_llcp_local
*local
,
201 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
203 pr_debug("ssap dsap %d %d\n", ssap
, dsap
);
205 if (ssap
== 0 && dsap
== 0)
208 read_lock(&local
->sockets
.lock
);
212 sk_for_each(sk
, &local
->sockets
.head
) {
213 tmp_sock
= nfc_llcp_sock(sk
);
215 if (tmp_sock
->ssap
== ssap
&& tmp_sock
->dsap
== dsap
) {
216 llcp_sock
= tmp_sock
;
221 read_unlock(&local
->sockets
.lock
);
223 if (llcp_sock
== NULL
)
226 sock_hold(&llcp_sock
->sk
);
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(unsigned long data
)
246 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
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(unsigned long data
)
289 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
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
, *n
;
298 list_for_each_entry_safe(local
, n
, &llcp_devices
, list
)
299 if (local
->dev
== dev
)
302 pr_debug("No device found\n");
307 static char *wks
[] = {
315 static int nfc_llcp_wks_sap(char *service_name
, size_t service_name_len
)
319 pr_debug("%s\n", service_name
);
321 if (service_name
== NULL
)
324 num_wks
= ARRAY_SIZE(wks
);
326 for (sap
= 0; sap
< num_wks
; sap
++) {
327 if (wks
[sap
] == NULL
)
330 if (strncmp(wks
[sap
], service_name
, service_name_len
) == 0)
338 struct nfc_llcp_sock
*nfc_llcp_sock_from_sn(struct nfc_llcp_local
*local
,
339 u8
*sn
, size_t sn_len
)
342 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
344 pr_debug("sn %zd %p\n", sn_len
, sn
);
346 if (sn
== NULL
|| sn_len
== 0)
349 read_lock(&local
->sockets
.lock
);
353 sk_for_each(sk
, &local
->sockets
.head
) {
354 tmp_sock
= nfc_llcp_sock(sk
);
356 pr_debug("llcp sock %p\n", tmp_sock
);
358 if (tmp_sock
->sk
.sk_type
== SOCK_STREAM
&&
359 tmp_sock
->sk
.sk_state
!= LLCP_LISTEN
)
362 if (tmp_sock
->sk
.sk_type
== SOCK_DGRAM
&&
363 tmp_sock
->sk
.sk_state
!= LLCP_BOUND
)
366 if (tmp_sock
->service_name
== NULL
||
367 tmp_sock
->service_name_len
== 0)
370 if (tmp_sock
->service_name_len
!= sn_len
)
373 if (memcmp(sn
, tmp_sock
->service_name
, sn_len
) == 0) {
374 llcp_sock
= tmp_sock
;
379 read_unlock(&local
->sockets
.lock
);
381 pr_debug("Found llcp sock %p\n", llcp_sock
);
386 u8
nfc_llcp_get_sdp_ssap(struct nfc_llcp_local
*local
,
387 struct nfc_llcp_sock
*sock
)
389 mutex_lock(&local
->sdp_lock
);
391 if (sock
->service_name
!= NULL
&& sock
->service_name_len
> 0) {
392 int ssap
= nfc_llcp_wks_sap(sock
->service_name
,
393 sock
->service_name_len
);
396 pr_debug("WKS %d\n", ssap
);
398 /* This is a WKS, let's check if it's free */
399 if (local
->local_wks
& BIT(ssap
)) {
400 mutex_unlock(&local
->sdp_lock
);
405 set_bit(ssap
, &local
->local_wks
);
406 mutex_unlock(&local
->sdp_lock
);
412 * Check if there already is a non WKS socket bound
413 * to this service name.
415 if (nfc_llcp_sock_from_sn(local
, sock
->service_name
,
416 sock
->service_name_len
) != NULL
) {
417 mutex_unlock(&local
->sdp_lock
);
422 mutex_unlock(&local
->sdp_lock
);
424 return LLCP_SDP_UNBOUND
;
426 } else if (sock
->ssap
!= 0 && sock
->ssap
< LLCP_WKS_NUM_SAP
) {
427 if (!test_bit(sock
->ssap
, &local
->local_wks
)) {
428 set_bit(sock
->ssap
, &local
->local_wks
);
429 mutex_unlock(&local
->sdp_lock
);
435 mutex_unlock(&local
->sdp_lock
);
440 u8
nfc_llcp_get_local_ssap(struct nfc_llcp_local
*local
)
444 mutex_lock(&local
->sdp_lock
);
446 local_ssap
= find_first_zero_bit(&local
->local_sap
, LLCP_LOCAL_NUM_SAP
);
447 if (local_ssap
== LLCP_LOCAL_NUM_SAP
) {
448 mutex_unlock(&local
->sdp_lock
);
452 set_bit(local_ssap
, &local
->local_sap
);
454 mutex_unlock(&local
->sdp_lock
);
456 return local_ssap
+ LLCP_LOCAL_SAP_OFFSET
;
459 void nfc_llcp_put_ssap(struct nfc_llcp_local
*local
, u8 ssap
)
464 if (ssap
< LLCP_WKS_NUM_SAP
) {
466 sdp
= &local
->local_wks
;
467 } else if (ssap
< LLCP_LOCAL_NUM_SAP
) {
468 atomic_t
*client_cnt
;
470 local_ssap
= ssap
- LLCP_WKS_NUM_SAP
;
471 sdp
= &local
->local_sdp
;
472 client_cnt
= &local
->local_sdp_cnt
[local_ssap
];
474 pr_debug("%d clients\n", atomic_read(client_cnt
));
476 mutex_lock(&local
->sdp_lock
);
478 if (atomic_dec_and_test(client_cnt
)) {
479 struct nfc_llcp_sock
*l_sock
;
481 pr_debug("No more clients for SAP %d\n", ssap
);
483 clear_bit(local_ssap
, sdp
);
485 /* Find the listening sock and set it back to UNBOUND */
486 l_sock
= nfc_llcp_sock_get(local
, ssap
, LLCP_SAP_SDP
);
488 l_sock
->ssap
= LLCP_SDP_UNBOUND
;
489 nfc_llcp_sock_put(l_sock
);
493 mutex_unlock(&local
->sdp_lock
);
496 } else if (ssap
< LLCP_MAX_SAP
) {
497 local_ssap
= ssap
- LLCP_LOCAL_NUM_SAP
;
498 sdp
= &local
->local_sap
;
503 mutex_lock(&local
->sdp_lock
);
505 clear_bit(local_ssap
, sdp
);
507 mutex_unlock(&local
->sdp_lock
);
510 static u8
nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local
*local
)
514 mutex_lock(&local
->sdp_lock
);
516 ssap
= find_first_zero_bit(&local
->local_sdp
, LLCP_SDP_NUM_SAP
);
517 if (ssap
== LLCP_SDP_NUM_SAP
) {
518 mutex_unlock(&local
->sdp_lock
);
523 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP
+ ssap
);
525 set_bit(ssap
, &local
->local_sdp
);
527 mutex_unlock(&local
->sdp_lock
);
529 return LLCP_WKS_NUM_SAP
+ ssap
;
532 static int nfc_llcp_build_gb(struct nfc_llcp_local
*local
)
534 u8
*gb_cur
, *version_tlv
, version
, version_length
;
535 u8
*lto_tlv
, lto_length
;
536 u8
*wks_tlv
, wks_length
;
537 u8
*miux_tlv
, miux_length
;
538 __be16 wks
= cpu_to_be16(local
->local_wks
);
542 version
= LLCP_VERSION_11
;
543 version_tlv
= nfc_llcp_build_tlv(LLCP_TLV_VERSION
, &version
,
545 gb_len
+= version_length
;
547 lto_tlv
= nfc_llcp_build_tlv(LLCP_TLV_LTO
, &local
->lto
, 1, <o_length
);
548 gb_len
+= lto_length
;
550 pr_debug("Local wks 0x%lx\n", local
->local_wks
);
551 wks_tlv
= nfc_llcp_build_tlv(LLCP_TLV_WKS
, (u8
*)&wks
, 2, &wks_length
);
552 gb_len
+= wks_length
;
554 miux_tlv
= nfc_llcp_build_tlv(LLCP_TLV_MIUX
, (u8
*)&local
->miux
, 0,
556 gb_len
+= miux_length
;
558 gb_len
+= ARRAY_SIZE(llcp_magic
);
560 if (gb_len
> NFC_MAX_GT_LEN
) {
567 memcpy(gb_cur
, llcp_magic
, ARRAY_SIZE(llcp_magic
));
568 gb_cur
+= ARRAY_SIZE(llcp_magic
);
570 memcpy(gb_cur
, version_tlv
, version_length
);
571 gb_cur
+= version_length
;
573 memcpy(gb_cur
, lto_tlv
, lto_length
);
574 gb_cur
+= lto_length
;
576 memcpy(gb_cur
, wks_tlv
, wks_length
);
577 gb_cur
+= wks_length
;
579 memcpy(gb_cur
, miux_tlv
, miux_length
);
580 gb_cur
+= miux_length
;
582 local
->gb_len
= gb_len
;
593 u8
*nfc_llcp_general_bytes(struct nfc_dev
*dev
, size_t *general_bytes_len
)
595 struct nfc_llcp_local
*local
;
597 local
= nfc_llcp_find_local(dev
);
599 *general_bytes_len
= 0;
603 nfc_llcp_build_gb(local
);
605 *general_bytes_len
= local
->gb_len
;
610 int nfc_llcp_set_remote_gb(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
612 struct nfc_llcp_local
*local
= nfc_llcp_find_local(dev
);
615 pr_err("No LLCP device\n");
621 memset(local
->remote_gb
, 0, NFC_MAX_GT_LEN
);
622 memcpy(local
->remote_gb
, gb
, gb_len
);
623 local
->remote_gb_len
= gb_len
;
625 if (memcmp(local
->remote_gb
, llcp_magic
, 3)) {
626 pr_err("MAC does not support LLCP\n");
630 return nfc_llcp_parse_gb_tlv(local
,
631 &local
->remote_gb
[3],
632 local
->remote_gb_len
- 3);
635 static u8
nfc_llcp_dsap(struct sk_buff
*pdu
)
637 return (pdu
->data
[0] & 0xfc) >> 2;
640 static u8
nfc_llcp_ptype(struct sk_buff
*pdu
)
642 return ((pdu
->data
[0] & 0x03) << 2) | ((pdu
->data
[1] & 0xc0) >> 6);
645 static u8
nfc_llcp_ssap(struct sk_buff
*pdu
)
647 return pdu
->data
[1] & 0x3f;
650 static u8
nfc_llcp_ns(struct sk_buff
*pdu
)
652 return pdu
->data
[2] >> 4;
655 static u8
nfc_llcp_nr(struct sk_buff
*pdu
)
657 return pdu
->data
[2] & 0xf;
660 static void nfc_llcp_set_nrns(struct nfc_llcp_sock
*sock
, struct sk_buff
*pdu
)
662 pdu
->data
[2] = (sock
->send_n
<< 4) | (sock
->recv_n
);
663 sock
->send_n
= (sock
->send_n
+ 1) % 16;
664 sock
->recv_ack_n
= (sock
->recv_n
- 1) % 16;
667 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local
*local
,
668 struct sk_buff
*skb
, u8 direction
)
670 struct sk_buff
*skb_copy
= NULL
, *nskb
;
674 read_lock(&local
->raw_sockets
.lock
);
676 sk_for_each(sk
, &local
->raw_sockets
.head
) {
677 if (sk
->sk_state
!= LLCP_BOUND
)
680 if (skb_copy
== NULL
) {
681 skb_copy
= __pskb_copy(skb
, NFC_LLCP_RAW_HEADER_SIZE
,
684 if (skb_copy
== NULL
)
687 data
= skb_push(skb_copy
, NFC_LLCP_RAW_HEADER_SIZE
);
689 data
[0] = local
->dev
? local
->dev
->idx
: 0xFF;
693 nskb
= skb_clone(skb_copy
, GFP_ATOMIC
);
697 if (sock_queue_rcv_skb(sk
, nskb
))
701 read_unlock(&local
->raw_sockets
.lock
);
706 static void nfc_llcp_tx_work(struct work_struct
*work
)
708 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
712 struct nfc_llcp_sock
*llcp_sock
;
714 skb
= skb_dequeue(&local
->tx_queue
);
717 llcp_sock
= nfc_llcp_sock(sk
);
719 if (llcp_sock
== NULL
&& nfc_llcp_ptype(skb
) == LLCP_PDU_I
) {
721 nfc_llcp_send_symm(local
->dev
);
722 } else if (llcp_sock
&& !llcp_sock
->remote_ready
) {
723 skb_queue_head(&local
->tx_queue
, skb
);
724 nfc_llcp_send_symm(local
->dev
);
726 struct sk_buff
*copy_skb
= NULL
;
727 u8 ptype
= nfc_llcp_ptype(skb
);
730 pr_debug("Sending pending skb\n");
731 print_hex_dump(KERN_DEBUG
, "LLCP Tx: ",
732 DUMP_PREFIX_OFFSET
, 16, 1,
733 skb
->data
, skb
->len
, true);
735 if (ptype
== LLCP_PDU_DISC
&& sk
!= NULL
&&
736 sk
->sk_state
== LLCP_DISCONNECTING
) {
737 nfc_llcp_sock_unlink(&local
->sockets
, sk
);
742 if (ptype
== LLCP_PDU_I
)
743 copy_skb
= skb_copy(skb
, GFP_ATOMIC
);
745 __net_timestamp(skb
);
747 nfc_llcp_send_to_raw_sock(local
, skb
,
748 NFC_LLCP_DIRECTION_TX
);
750 ret
= nfc_data_exchange(local
->dev
, local
->target_idx
,
751 skb
, nfc_llcp_recv
, local
);
758 if (ptype
== LLCP_PDU_I
&& copy_skb
)
759 skb_queue_tail(&llcp_sock
->tx_pending_queue
,
763 nfc_llcp_send_symm(local
->dev
);
767 mod_timer(&local
->link_timer
,
768 jiffies
+ msecs_to_jiffies(2 * local
->remote_lto
));
771 static struct nfc_llcp_sock
*nfc_llcp_connecting_sock_get(struct nfc_llcp_local
*local
,
775 struct nfc_llcp_sock
*llcp_sock
;
777 read_lock(&local
->connecting_sockets
.lock
);
779 sk_for_each(sk
, &local
->connecting_sockets
.head
) {
780 llcp_sock
= nfc_llcp_sock(sk
);
782 if (llcp_sock
->ssap
== ssap
) {
783 sock_hold(&llcp_sock
->sk
);
791 read_unlock(&local
->connecting_sockets
.lock
);
796 static struct nfc_llcp_sock
*nfc_llcp_sock_get_sn(struct nfc_llcp_local
*local
,
797 u8
*sn
, size_t sn_len
)
799 struct nfc_llcp_sock
*llcp_sock
;
801 llcp_sock
= nfc_llcp_sock_from_sn(local
, sn
, sn_len
);
803 if (llcp_sock
== NULL
)
806 sock_hold(&llcp_sock
->sk
);
811 static u8
*nfc_llcp_connect_sn(struct sk_buff
*skb
, size_t *sn_len
)
813 u8
*tlv
= &skb
->data
[2], type
, length
;
814 size_t tlv_array_len
= skb
->len
- LLCP_HEADER_SIZE
, offset
= 0;
816 while (offset
< tlv_array_len
) {
820 pr_debug("type 0x%x length %d\n", type
, length
);
822 if (type
== LLCP_TLV_SN
) {
827 offset
+= length
+ 2;
834 static void nfc_llcp_recv_ui(struct nfc_llcp_local
*local
,
837 struct nfc_llcp_sock
*llcp_sock
;
838 struct nfc_llcp_ui_cb
*ui_cb
;
841 dsap
= nfc_llcp_dsap(skb
);
842 ssap
= nfc_llcp_ssap(skb
);
844 ui_cb
= nfc_llcp_ui_skb_cb(skb
);
848 pr_debug("%d %d\n", dsap
, ssap
);
850 /* We're looking for a bound socket, not a client one */
851 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
852 if (llcp_sock
== NULL
|| llcp_sock
->sk
.sk_type
!= SOCK_DGRAM
)
855 /* There is no sequence with UI frames */
856 skb_pull(skb
, LLCP_HEADER_SIZE
);
857 if (!sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
859 * UI frames will be freed from the socket layer, so we
860 * need to keep them alive until someone receives them.
864 pr_err("Receive queue is full\n");
867 nfc_llcp_sock_put(llcp_sock
);
870 static void nfc_llcp_recv_connect(struct nfc_llcp_local
*local
,
873 struct sock
*new_sk
, *parent
;
874 struct nfc_llcp_sock
*sock
, *new_sock
;
875 u8 dsap
, ssap
, reason
;
877 dsap
= nfc_llcp_dsap(skb
);
878 ssap
= nfc_llcp_ssap(skb
);
880 pr_debug("%d %d\n", dsap
, ssap
);
882 if (dsap
!= LLCP_SAP_SDP
) {
883 sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
884 if (sock
== NULL
|| sock
->sk
.sk_state
!= LLCP_LISTEN
) {
885 reason
= LLCP_DM_NOBOUND
;
892 sn
= nfc_llcp_connect_sn(skb
, &sn_len
);
894 reason
= LLCP_DM_NOBOUND
;
898 pr_debug("Service name length %zu\n", sn_len
);
900 sock
= nfc_llcp_sock_get_sn(local
, sn
, sn_len
);
902 reason
= LLCP_DM_NOBOUND
;
907 lock_sock(&sock
->sk
);
911 if (sk_acceptq_is_full(parent
)) {
912 reason
= LLCP_DM_REJ
;
913 release_sock(&sock
->sk
);
918 if (sock
->ssap
== LLCP_SDP_UNBOUND
) {
919 u8 ssap
= nfc_llcp_reserve_sdp_ssap(local
);
921 pr_debug("First client, reserving %d\n", ssap
);
923 if (ssap
== LLCP_SAP_MAX
) {
924 reason
= LLCP_DM_REJ
;
925 release_sock(&sock
->sk
);
933 new_sk
= nfc_llcp_sock_alloc(NULL
, parent
->sk_type
, GFP_ATOMIC
);
934 if (new_sk
== NULL
) {
935 reason
= LLCP_DM_REJ
;
936 release_sock(&sock
->sk
);
941 new_sock
= nfc_llcp_sock(new_sk
);
942 new_sock
->dev
= local
->dev
;
943 new_sock
->local
= nfc_llcp_local_get(local
);
944 new_sock
->rw
= sock
->rw
;
945 new_sock
->miux
= sock
->miux
;
946 new_sock
->nfc_protocol
= sock
->nfc_protocol
;
947 new_sock
->dsap
= ssap
;
948 new_sock
->target_idx
= local
->target_idx
;
949 new_sock
->parent
= parent
;
950 new_sock
->ssap
= sock
->ssap
;
951 if (sock
->ssap
< LLCP_LOCAL_NUM_SAP
&& sock
->ssap
>= LLCP_WKS_NUM_SAP
) {
952 atomic_t
*client_count
;
954 pr_debug("reserved_ssap %d for %p\n", sock
->ssap
, new_sock
);
957 &local
->local_sdp_cnt
[sock
->ssap
- LLCP_WKS_NUM_SAP
];
959 atomic_inc(client_count
);
960 new_sock
->reserved_ssap
= sock
->ssap
;
963 nfc_llcp_parse_connection_tlv(new_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
964 skb
->len
- LLCP_HEADER_SIZE
);
966 pr_debug("new sock %p sk %p\n", new_sock
, &new_sock
->sk
);
968 nfc_llcp_sock_link(&local
->sockets
, new_sk
);
970 nfc_llcp_accept_enqueue(&sock
->sk
, new_sk
);
972 nfc_get_device(local
->dev
->idx
);
974 new_sk
->sk_state
= LLCP_CONNECTED
;
976 /* Wake the listening processes */
977 parent
->sk_data_ready(parent
, 0);
980 nfc_llcp_send_cc(new_sock
);
982 release_sock(&sock
->sk
);
989 nfc_llcp_send_dm(local
, dsap
, ssap
, reason
);
992 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock
*sock
)
995 struct nfc_llcp_local
*local
= sock
->local
;
997 pr_debug("Remote ready %d tx queue len %d remote rw %d",
998 sock
->remote_ready
, skb_queue_len(&sock
->tx_pending_queue
),
1001 /* Try to queue some I frames for transmission */
1002 while (sock
->remote_ready
&&
1003 skb_queue_len(&sock
->tx_pending_queue
) < sock
->remote_rw
) {
1004 struct sk_buff
*pdu
;
1006 pdu
= skb_dequeue(&sock
->tx_queue
);
1010 /* Update N(S)/N(R) */
1011 nfc_llcp_set_nrns(sock
, pdu
);
1013 skb_queue_tail(&local
->tx_queue
, pdu
);
1020 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local
*local
,
1021 struct sk_buff
*skb
)
1023 struct nfc_llcp_sock
*llcp_sock
;
1025 u8 dsap
, ssap
, ptype
, ns
, nr
;
1027 ptype
= nfc_llcp_ptype(skb
);
1028 dsap
= nfc_llcp_dsap(skb
);
1029 ssap
= nfc_llcp_ssap(skb
);
1030 ns
= nfc_llcp_ns(skb
);
1031 nr
= nfc_llcp_nr(skb
);
1033 pr_debug("%d %d R %d S %d\n", dsap
, ssap
, nr
, ns
);
1035 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1036 if (llcp_sock
== NULL
) {
1037 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1041 sk
= &llcp_sock
->sk
;
1043 if (sk
->sk_state
== LLCP_CLOSED
) {
1045 nfc_llcp_sock_put(llcp_sock
);
1048 /* Pass the payload upstream */
1049 if (ptype
== LLCP_PDU_I
) {
1050 pr_debug("I frame, queueing on %p\n", &llcp_sock
->sk
);
1052 if (ns
== llcp_sock
->recv_n
)
1053 llcp_sock
->recv_n
= (llcp_sock
->recv_n
+ 1) % 16;
1055 pr_err("Received out of sequence I PDU\n");
1057 skb_pull(skb
, LLCP_HEADER_SIZE
+ LLCP_SEQUENCE_SIZE
);
1058 if (!sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
1060 * I frames will be freed from the socket layer, so we
1061 * need to keep them alive until someone receives them.
1065 pr_err("Receive queue is full\n");
1069 /* Remove skbs from the pending queue */
1070 if (llcp_sock
->send_ack_n
!= nr
) {
1071 struct sk_buff
*s
, *tmp
;
1074 llcp_sock
->send_ack_n
= nr
;
1076 /* Remove and free all skbs until ns == nr */
1077 skb_queue_walk_safe(&llcp_sock
->tx_pending_queue
, s
, tmp
) {
1080 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
1087 /* Re-queue the remaining skbs for transmission */
1088 skb_queue_reverse_walk_safe(&llcp_sock
->tx_pending_queue
,
1090 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
1091 skb_queue_head(&local
->tx_queue
, s
);
1095 if (ptype
== LLCP_PDU_RR
)
1096 llcp_sock
->remote_ready
= true;
1097 else if (ptype
== LLCP_PDU_RNR
)
1098 llcp_sock
->remote_ready
= false;
1100 if (nfc_llcp_queue_i_frames(llcp_sock
) == 0 && ptype
== LLCP_PDU_I
)
1101 nfc_llcp_send_rr(llcp_sock
);
1104 nfc_llcp_sock_put(llcp_sock
);
1107 static void nfc_llcp_recv_disc(struct nfc_llcp_local
*local
,
1108 struct sk_buff
*skb
)
1110 struct nfc_llcp_sock
*llcp_sock
;
1114 dsap
= nfc_llcp_dsap(skb
);
1115 ssap
= nfc_llcp_ssap(skb
);
1117 if ((dsap
== 0) && (ssap
== 0)) {
1118 pr_debug("Connection termination");
1119 nfc_dep_link_down(local
->dev
);
1123 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1124 if (llcp_sock
== NULL
) {
1125 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1129 sk
= &llcp_sock
->sk
;
1132 nfc_llcp_socket_purge(llcp_sock
);
1134 if (sk
->sk_state
== LLCP_CLOSED
) {
1136 nfc_llcp_sock_put(llcp_sock
);
1139 if (sk
->sk_state
== LLCP_CONNECTED
) {
1140 nfc_put_device(local
->dev
);
1141 sk
->sk_state
= LLCP_CLOSED
;
1142 sk
->sk_state_change(sk
);
1145 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_DISC
);
1148 nfc_llcp_sock_put(llcp_sock
);
1151 static void nfc_llcp_recv_cc(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1153 struct nfc_llcp_sock
*llcp_sock
;
1157 dsap
= nfc_llcp_dsap(skb
);
1158 ssap
= nfc_llcp_ssap(skb
);
1160 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
1161 if (llcp_sock
== NULL
) {
1162 pr_err("Invalid CC\n");
1163 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1168 sk
= &llcp_sock
->sk
;
1170 /* Unlink from connecting and link to the client array */
1171 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
1172 nfc_llcp_sock_link(&local
->sockets
, sk
);
1173 llcp_sock
->dsap
= ssap
;
1175 nfc_llcp_parse_connection_tlv(llcp_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
1176 skb
->len
- LLCP_HEADER_SIZE
);
1178 sk
->sk_state
= LLCP_CONNECTED
;
1179 sk
->sk_state_change(sk
);
1181 nfc_llcp_sock_put(llcp_sock
);
1184 static void nfc_llcp_recv_dm(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1186 struct nfc_llcp_sock
*llcp_sock
;
1188 u8 dsap
, ssap
, reason
;
1190 dsap
= nfc_llcp_dsap(skb
);
1191 ssap
= nfc_llcp_ssap(skb
);
1192 reason
= skb
->data
[2];
1194 pr_debug("%d %d reason %d\n", ssap
, dsap
, reason
);
1197 case LLCP_DM_NOBOUND
:
1199 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
1203 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1207 if (llcp_sock
== NULL
) {
1208 pr_debug("Already closed\n");
1212 sk
= &llcp_sock
->sk
;
1215 sk
->sk_state
= LLCP_CLOSED
;
1216 sk
->sk_state_change(sk
);
1218 nfc_llcp_sock_put(llcp_sock
);
1221 static void nfc_llcp_recv_snl(struct nfc_llcp_local
*local
,
1222 struct sk_buff
*skb
)
1224 struct nfc_llcp_sock
*llcp_sock
;
1225 u8 dsap
, ssap
, *tlv
, type
, length
, tid
, sap
;
1226 u16 tlv_len
, offset
;
1228 size_t service_name_len
;
1229 struct nfc_llcp_sdp_tlv
*sdp
;
1230 HLIST_HEAD(llc_sdres_list
);
1231 size_t sdres_tlvs_len
;
1232 HLIST_HEAD(nl_sdres_list
);
1234 dsap
= nfc_llcp_dsap(skb
);
1235 ssap
= nfc_llcp_ssap(skb
);
1237 pr_debug("%d %d\n", dsap
, ssap
);
1239 if (dsap
!= LLCP_SAP_SDP
|| ssap
!= LLCP_SAP_SDP
) {
1240 pr_err("Wrong SNL SAP\n");
1244 tlv
= &skb
->data
[LLCP_HEADER_SIZE
];
1245 tlv_len
= skb
->len
- LLCP_HEADER_SIZE
;
1249 while (offset
< tlv_len
) {
1254 case LLCP_TLV_SDREQ
:
1256 service_name
= (char *) &tlv
[3];
1257 service_name_len
= length
- 1;
1259 pr_debug("Looking for %.16s\n", service_name
);
1261 if (service_name_len
== strlen("urn:nfc:sn:sdp") &&
1262 !strncmp(service_name
, "urn:nfc:sn:sdp",
1263 service_name_len
)) {
1268 llcp_sock
= nfc_llcp_sock_from_sn(local
, service_name
,
1276 * We found a socket but its ssap has not been reserved
1277 * yet. We need to assign it for good and send a reply.
1278 * The ssap will be freed when the socket is closed.
1280 if (llcp_sock
->ssap
== LLCP_SDP_UNBOUND
) {
1281 atomic_t
*client_count
;
1283 sap
= nfc_llcp_reserve_sdp_ssap(local
);
1285 pr_debug("Reserving %d\n", sap
);
1287 if (sap
== LLCP_SAP_MAX
) {
1293 &local
->local_sdp_cnt
[sap
-
1296 atomic_inc(client_count
);
1298 llcp_sock
->ssap
= sap
;
1299 llcp_sock
->reserved_ssap
= sap
;
1301 sap
= llcp_sock
->ssap
;
1304 pr_debug("%p %d\n", llcp_sock
, sap
);
1307 sdp
= nfc_llcp_build_sdres_tlv(tid
, sap
);
1311 sdres_tlvs_len
+= sdp
->tlv_len
;
1312 hlist_add_head(&sdp
->node
, &llc_sdres_list
);
1315 case LLCP_TLV_SDRES
:
1316 mutex_lock(&local
->sdreq_lock
);
1318 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv
[2]);
1320 hlist_for_each_entry(sdp
, &local
->pending_sdreqs
, node
) {
1321 if (sdp
->tid
!= tlv
[2])
1326 pr_debug("Found: uri=%s, sap=%d\n",
1327 sdp
->uri
, sdp
->sap
);
1329 hlist_del(&sdp
->node
);
1331 hlist_add_head(&sdp
->node
, &nl_sdres_list
);
1336 mutex_unlock(&local
->sdreq_lock
);
1340 pr_err("Invalid SNL tlv value 0x%x\n", type
);
1344 offset
+= length
+ 2;
1349 if (!hlist_empty(&nl_sdres_list
))
1350 nfc_genl_llc_send_sdres(local
->dev
, &nl_sdres_list
);
1352 if (!hlist_empty(&llc_sdres_list
))
1353 nfc_llcp_send_snl_sdres(local
, &llc_sdres_list
, sdres_tlvs_len
);
1356 static void nfc_llcp_recv_agf(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1360 struct sk_buff
*new_skb
;
1362 if (skb
->len
<= LLCP_HEADER_SIZE
) {
1363 pr_err("Malformed AGF PDU\n");
1367 skb_pull(skb
, LLCP_HEADER_SIZE
);
1369 while (skb
->len
> LLCP_AGF_PDU_HEADER_SIZE
) {
1370 pdu_len
= skb
->data
[0] << 8 | skb
->data
[1];
1372 skb_pull(skb
, LLCP_AGF_PDU_HEADER_SIZE
);
1374 if (pdu_len
< LLCP_HEADER_SIZE
|| pdu_len
> skb
->len
) {
1375 pr_err("Malformed AGF PDU\n");
1379 ptype
= nfc_llcp_ptype(skb
);
1381 if (ptype
== LLCP_PDU_SYMM
|| ptype
== LLCP_PDU_AGF
)
1384 new_skb
= nfc_alloc_recv_skb(pdu_len
, GFP_KERNEL
);
1385 if (new_skb
== NULL
) {
1386 pr_err("Could not allocate PDU\n");
1390 memcpy(skb_put(new_skb
, pdu_len
), skb
->data
, pdu_len
);
1392 nfc_llcp_rx_skb(local
, new_skb
);
1396 skb_pull(skb
, pdu_len
);
1400 static void nfc_llcp_rx_skb(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1402 u8 dsap
, ssap
, ptype
;
1404 ptype
= nfc_llcp_ptype(skb
);
1405 dsap
= nfc_llcp_dsap(skb
);
1406 ssap
= nfc_llcp_ssap(skb
);
1408 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype
, dsap
, ssap
);
1410 if (ptype
!= LLCP_PDU_SYMM
)
1411 print_hex_dump(KERN_DEBUG
, "LLCP Rx: ", DUMP_PREFIX_OFFSET
,
1412 16, 1, skb
->data
, skb
->len
, true);
1421 nfc_llcp_recv_ui(local
, skb
);
1424 case LLCP_PDU_CONNECT
:
1425 pr_debug("CONNECT\n");
1426 nfc_llcp_recv_connect(local
, skb
);
1431 nfc_llcp_recv_disc(local
, skb
);
1436 nfc_llcp_recv_cc(local
, skb
);
1441 nfc_llcp_recv_dm(local
, skb
);
1446 nfc_llcp_recv_snl(local
, skb
);
1452 pr_debug("I frame\n");
1453 nfc_llcp_recv_hdlc(local
, skb
);
1457 pr_debug("AGF frame\n");
1458 nfc_llcp_recv_agf(local
, skb
);
1463 static void nfc_llcp_rx_work(struct work_struct
*work
)
1465 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
1467 struct sk_buff
*skb
;
1469 skb
= local
->rx_pending
;
1471 pr_debug("No pending SKB\n");
1475 __net_timestamp(skb
);
1477 nfc_llcp_send_to_raw_sock(local
, skb
, NFC_LLCP_DIRECTION_RX
);
1479 nfc_llcp_rx_skb(local
, skb
);
1481 schedule_work(&local
->tx_work
);
1482 kfree_skb(local
->rx_pending
);
1483 local
->rx_pending
= NULL
;
1486 static void __nfc_llcp_recv(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1488 local
->rx_pending
= skb
;
1489 del_timer(&local
->link_timer
);
1490 schedule_work(&local
->rx_work
);
1493 void nfc_llcp_recv(void *data
, struct sk_buff
*skb
, int err
)
1495 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
1497 pr_debug("Received an LLCP PDU\n");
1499 pr_err("err %d\n", err
);
1503 __nfc_llcp_recv(local
, skb
);
1506 int nfc_llcp_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
1508 struct nfc_llcp_local
*local
;
1510 local
= nfc_llcp_find_local(dev
);
1514 __nfc_llcp_recv(local
, skb
);
1519 void nfc_llcp_mac_is_down(struct nfc_dev
*dev
)
1521 struct nfc_llcp_local
*local
;
1523 local
= nfc_llcp_find_local(dev
);
1527 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1528 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1530 /* Close and purge all existing sockets */
1531 nfc_llcp_socket_release(local
, true, 0);
1534 void nfc_llcp_mac_is_up(struct nfc_dev
*dev
, u32 target_idx
,
1535 u8 comm_mode
, u8 rf_mode
)
1537 struct nfc_llcp_local
*local
;
1539 pr_debug("rf mode %d\n", rf_mode
);
1541 local
= nfc_llcp_find_local(dev
);
1545 local
->target_idx
= target_idx
;
1546 local
->comm_mode
= comm_mode
;
1547 local
->rf_mode
= rf_mode
;
1549 if (rf_mode
== NFC_RF_INITIATOR
) {
1550 pr_debug("Queueing Tx work\n");
1552 schedule_work(&local
->tx_work
);
1554 mod_timer(&local
->link_timer
,
1555 jiffies
+ msecs_to_jiffies(local
->remote_lto
));
1559 int nfc_llcp_register_device(struct nfc_dev
*ndev
)
1561 struct nfc_llcp_local
*local
;
1563 local
= kzalloc(sizeof(struct nfc_llcp_local
), GFP_KERNEL
);
1568 INIT_LIST_HEAD(&local
->list
);
1569 kref_init(&local
->ref
);
1570 mutex_init(&local
->sdp_lock
);
1571 init_timer(&local
->link_timer
);
1572 local
->link_timer
.data
= (unsigned long) local
;
1573 local
->link_timer
.function
= nfc_llcp_symm_timer
;
1575 skb_queue_head_init(&local
->tx_queue
);
1576 INIT_WORK(&local
->tx_work
, nfc_llcp_tx_work
);
1578 local
->rx_pending
= NULL
;
1579 INIT_WORK(&local
->rx_work
, nfc_llcp_rx_work
);
1581 INIT_WORK(&local
->timeout_work
, nfc_llcp_timeout_work
);
1583 rwlock_init(&local
->sockets
.lock
);
1584 rwlock_init(&local
->connecting_sockets
.lock
);
1585 rwlock_init(&local
->raw_sockets
.lock
);
1587 local
->lto
= 150; /* 1500 ms */
1588 local
->rw
= LLCP_MAX_RW
;
1589 local
->miux
= cpu_to_be16(LLCP_MAX_MIUX
);
1590 local
->local_wks
= 0x1; /* LLC Link Management */
1592 nfc_llcp_build_gb(local
);
1594 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1595 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1597 mutex_init(&local
->sdreq_lock
);
1598 INIT_HLIST_HEAD(&local
->pending_sdreqs
);
1599 init_timer(&local
->sdreq_timer
);
1600 local
->sdreq_timer
.data
= (unsigned long) local
;
1601 local
->sdreq_timer
.function
= nfc_llcp_sdreq_timer
;
1602 INIT_WORK(&local
->sdreq_timeout_work
, nfc_llcp_sdreq_timeout_work
);
1604 list_add(&local
->list
, &llcp_devices
);
1609 void nfc_llcp_unregister_device(struct nfc_dev
*dev
)
1611 struct nfc_llcp_local
*local
= nfc_llcp_find_local(dev
);
1613 if (local
== NULL
) {
1614 pr_debug("No such device\n");
1618 local_cleanup(local
);
1620 nfc_llcp_local_put(local
);
1623 int __init
nfc_llcp_init(void)
1625 INIT_LIST_HEAD(&llcp_devices
);
1627 return nfc_llcp_sock_init();
1630 void nfc_llcp_exit(void)
1632 nfc_llcp_sock_exit();