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, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/nfc.h>
30 static u8 llcp_magic
[3] = {0x46, 0x66, 0x6d};
32 static struct list_head llcp_devices
;
34 static void nfc_llcp_rx_skb(struct nfc_llcp_local
*local
, struct sk_buff
*skb
);
36 void nfc_llcp_sock_link(struct llcp_sock_list
*l
, struct sock
*sk
)
39 sk_add_node(sk
, &l
->head
);
40 write_unlock(&l
->lock
);
43 void nfc_llcp_sock_unlink(struct llcp_sock_list
*l
, struct sock
*sk
)
47 write_unlock(&l
->lock
);
50 void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock
*sock
)
52 sock
->remote_rw
= LLCP_DEFAULT_RW
;
53 sock
->remote_miu
= LLCP_MAX_MIU
+ 1;
56 static void nfc_llcp_socket_purge(struct nfc_llcp_sock
*sock
)
58 struct nfc_llcp_local
*local
= sock
->local
;
59 struct sk_buff
*s
, *tmp
;
61 pr_debug("%p\n", &sock
->sk
);
63 skb_queue_purge(&sock
->tx_queue
);
64 skb_queue_purge(&sock
->tx_pending_queue
);
69 /* Search for local pending SKBs that are related to this socket */
70 skb_queue_walk_safe(&local
->tx_queue
, s
, tmp
) {
71 if (s
->sk
!= &sock
->sk
)
74 skb_unlink(s
, &local
->tx_queue
);
79 static void nfc_llcp_socket_release(struct nfc_llcp_local
*local
, bool device
,
83 struct hlist_node
*tmp
;
84 struct nfc_llcp_sock
*llcp_sock
;
86 skb_queue_purge(&local
->tx_queue
);
88 write_lock(&local
->sockets
.lock
);
90 sk_for_each_safe(sk
, tmp
, &local
->sockets
.head
) {
91 llcp_sock
= nfc_llcp_sock(sk
);
95 nfc_llcp_socket_purge(llcp_sock
);
97 if (sk
->sk_state
== LLCP_CONNECTED
)
98 nfc_put_device(llcp_sock
->dev
);
100 if (sk
->sk_state
== LLCP_LISTEN
) {
101 struct nfc_llcp_sock
*lsk
, *n
;
102 struct sock
*accept_sk
;
104 list_for_each_entry_safe(lsk
, n
,
105 &llcp_sock
->accept_queue
,
107 accept_sk
= &lsk
->sk
;
108 bh_lock_sock(accept_sk
);
110 nfc_llcp_accept_unlink(accept_sk
);
113 accept_sk
->sk_err
= err
;
114 accept_sk
->sk_state
= LLCP_CLOSED
;
115 accept_sk
->sk_state_change(sk
);
117 bh_unlock_sock(accept_sk
);
123 sk
->sk_state
= LLCP_CLOSED
;
124 sk
->sk_state_change(sk
);
128 sk_del_node_init(sk
);
131 write_unlock(&local
->sockets
.lock
);
133 /* If we still have a device, we keep the RAW sockets alive */
137 write_lock(&local
->raw_sockets
.lock
);
139 sk_for_each_safe(sk
, tmp
, &local
->raw_sockets
.head
) {
140 llcp_sock
= nfc_llcp_sock(sk
);
144 nfc_llcp_socket_purge(llcp_sock
);
148 sk
->sk_state
= LLCP_CLOSED
;
149 sk
->sk_state_change(sk
);
153 sk_del_node_init(sk
);
156 write_unlock(&local
->raw_sockets
.lock
);
159 struct nfc_llcp_local
*nfc_llcp_local_get(struct nfc_llcp_local
*local
)
161 kref_get(&local
->ref
);
166 static void local_cleanup(struct nfc_llcp_local
*local
)
168 nfc_llcp_socket_release(local
, false, ENXIO
);
169 del_timer_sync(&local
->link_timer
);
170 skb_queue_purge(&local
->tx_queue
);
171 cancel_work_sync(&local
->tx_work
);
172 cancel_work_sync(&local
->rx_work
);
173 cancel_work_sync(&local
->timeout_work
);
174 kfree_skb(local
->rx_pending
);
175 del_timer_sync(&local
->sdreq_timer
);
176 cancel_work_sync(&local
->sdreq_timeout_work
);
177 nfc_llcp_free_sdp_tlv_list(&local
->pending_sdreqs
);
180 static void local_release(struct kref
*ref
)
182 struct nfc_llcp_local
*local
;
184 local
= container_of(ref
, struct nfc_llcp_local
, ref
);
186 list_del(&local
->list
);
187 local_cleanup(local
);
191 int nfc_llcp_local_put(struct nfc_llcp_local
*local
)
196 return kref_put(&local
->ref
, local_release
);
199 static struct nfc_llcp_sock
*nfc_llcp_sock_get(struct nfc_llcp_local
*local
,
203 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
205 pr_debug("ssap dsap %d %d\n", ssap
, dsap
);
207 if (ssap
== 0 && dsap
== 0)
210 read_lock(&local
->sockets
.lock
);
214 sk_for_each(sk
, &local
->sockets
.head
) {
215 tmp_sock
= nfc_llcp_sock(sk
);
217 if (tmp_sock
->ssap
== ssap
&& tmp_sock
->dsap
== dsap
) {
218 llcp_sock
= tmp_sock
;
223 read_unlock(&local
->sockets
.lock
);
225 if (llcp_sock
== NULL
)
228 sock_hold(&llcp_sock
->sk
);
233 static void nfc_llcp_sock_put(struct nfc_llcp_sock
*sock
)
238 static void nfc_llcp_timeout_work(struct work_struct
*work
)
240 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
243 nfc_dep_link_down(local
->dev
);
246 static void nfc_llcp_symm_timer(unsigned long data
)
248 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
250 pr_err("SYMM timeout\n");
252 schedule_work(&local
->timeout_work
);
255 static void nfc_llcp_sdreq_timeout_work(struct work_struct
*work
)
258 HLIST_HEAD(nl_sdres_list
);
259 struct hlist_node
*n
;
260 struct nfc_llcp_sdp_tlv
*sdp
;
261 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
264 mutex_lock(&local
->sdreq_lock
);
266 time
= jiffies
- msecs_to_jiffies(3 * local
->remote_lto
);
268 hlist_for_each_entry_safe(sdp
, n
, &local
->pending_sdreqs
, node
) {
269 if (time_after(sdp
->time
, time
))
272 sdp
->sap
= LLCP_SDP_UNBOUND
;
274 hlist_del(&sdp
->node
);
276 hlist_add_head(&sdp
->node
, &nl_sdres_list
);
279 if (!hlist_empty(&local
->pending_sdreqs
))
280 mod_timer(&local
->sdreq_timer
,
281 jiffies
+ msecs_to_jiffies(3 * local
->remote_lto
));
283 mutex_unlock(&local
->sdreq_lock
);
285 if (!hlist_empty(&nl_sdres_list
))
286 nfc_genl_llc_send_sdres(local
->dev
, &nl_sdres_list
);
289 static void nfc_llcp_sdreq_timer(unsigned long data
)
291 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
293 schedule_work(&local
->sdreq_timeout_work
);
296 struct nfc_llcp_local
*nfc_llcp_find_local(struct nfc_dev
*dev
)
298 struct nfc_llcp_local
*local
, *n
;
300 list_for_each_entry_safe(local
, n
, &llcp_devices
, list
)
301 if (local
->dev
== dev
)
304 pr_debug("No device found\n");
309 static char *wks
[] = {
317 static int nfc_llcp_wks_sap(char *service_name
, size_t service_name_len
)
321 pr_debug("%s\n", service_name
);
323 if (service_name
== NULL
)
326 num_wks
= ARRAY_SIZE(wks
);
328 for (sap
= 0; sap
< num_wks
; sap
++) {
329 if (wks
[sap
] == NULL
)
332 if (strncmp(wks
[sap
], service_name
, service_name_len
) == 0)
340 struct nfc_llcp_sock
*nfc_llcp_sock_from_sn(struct nfc_llcp_local
*local
,
341 u8
*sn
, size_t sn_len
)
344 struct nfc_llcp_sock
*llcp_sock
, *tmp_sock
;
346 pr_debug("sn %zd %p\n", sn_len
, sn
);
348 if (sn
== NULL
|| sn_len
== 0)
351 read_lock(&local
->sockets
.lock
);
355 sk_for_each(sk
, &local
->sockets
.head
) {
356 tmp_sock
= nfc_llcp_sock(sk
);
358 pr_debug("llcp sock %p\n", tmp_sock
);
360 if (tmp_sock
->sk
.sk_type
== SOCK_STREAM
&&
361 tmp_sock
->sk
.sk_state
!= LLCP_LISTEN
)
364 if (tmp_sock
->sk
.sk_type
== SOCK_DGRAM
&&
365 tmp_sock
->sk
.sk_state
!= LLCP_BOUND
)
368 if (tmp_sock
->service_name
== NULL
||
369 tmp_sock
->service_name_len
== 0)
372 if (tmp_sock
->service_name_len
!= sn_len
)
375 if (memcmp(sn
, tmp_sock
->service_name
, sn_len
) == 0) {
376 llcp_sock
= tmp_sock
;
381 read_unlock(&local
->sockets
.lock
);
383 pr_debug("Found llcp sock %p\n", llcp_sock
);
388 u8
nfc_llcp_get_sdp_ssap(struct nfc_llcp_local
*local
,
389 struct nfc_llcp_sock
*sock
)
391 mutex_lock(&local
->sdp_lock
);
393 if (sock
->service_name
!= NULL
&& sock
->service_name_len
> 0) {
394 int ssap
= nfc_llcp_wks_sap(sock
->service_name
,
395 sock
->service_name_len
);
398 pr_debug("WKS %d\n", ssap
);
400 /* This is a WKS, let's check if it's free */
401 if (local
->local_wks
& BIT(ssap
)) {
402 mutex_unlock(&local
->sdp_lock
);
407 set_bit(ssap
, &local
->local_wks
);
408 mutex_unlock(&local
->sdp_lock
);
414 * Check if there already is a non WKS socket bound
415 * to this service name.
417 if (nfc_llcp_sock_from_sn(local
, sock
->service_name
,
418 sock
->service_name_len
) != NULL
) {
419 mutex_unlock(&local
->sdp_lock
);
424 mutex_unlock(&local
->sdp_lock
);
426 return LLCP_SDP_UNBOUND
;
428 } else if (sock
->ssap
!= 0 && sock
->ssap
< LLCP_WKS_NUM_SAP
) {
429 if (!test_bit(sock
->ssap
, &local
->local_wks
)) {
430 set_bit(sock
->ssap
, &local
->local_wks
);
431 mutex_unlock(&local
->sdp_lock
);
437 mutex_unlock(&local
->sdp_lock
);
442 u8
nfc_llcp_get_local_ssap(struct nfc_llcp_local
*local
)
446 mutex_lock(&local
->sdp_lock
);
448 local_ssap
= find_first_zero_bit(&local
->local_sap
, LLCP_LOCAL_NUM_SAP
);
449 if (local_ssap
== LLCP_LOCAL_NUM_SAP
) {
450 mutex_unlock(&local
->sdp_lock
);
454 set_bit(local_ssap
, &local
->local_sap
);
456 mutex_unlock(&local
->sdp_lock
);
458 return local_ssap
+ LLCP_LOCAL_SAP_OFFSET
;
461 void nfc_llcp_put_ssap(struct nfc_llcp_local
*local
, u8 ssap
)
466 if (ssap
< LLCP_WKS_NUM_SAP
) {
468 sdp
= &local
->local_wks
;
469 } else if (ssap
< LLCP_LOCAL_NUM_SAP
) {
470 atomic_t
*client_cnt
;
472 local_ssap
= ssap
- LLCP_WKS_NUM_SAP
;
473 sdp
= &local
->local_sdp
;
474 client_cnt
= &local
->local_sdp_cnt
[local_ssap
];
476 pr_debug("%d clients\n", atomic_read(client_cnt
));
478 mutex_lock(&local
->sdp_lock
);
480 if (atomic_dec_and_test(client_cnt
)) {
481 struct nfc_llcp_sock
*l_sock
;
483 pr_debug("No more clients for SAP %d\n", ssap
);
485 clear_bit(local_ssap
, sdp
);
487 /* Find the listening sock and set it back to UNBOUND */
488 l_sock
= nfc_llcp_sock_get(local
, ssap
, LLCP_SAP_SDP
);
490 l_sock
->ssap
= LLCP_SDP_UNBOUND
;
491 nfc_llcp_sock_put(l_sock
);
495 mutex_unlock(&local
->sdp_lock
);
498 } else if (ssap
< LLCP_MAX_SAP
) {
499 local_ssap
= ssap
- LLCP_LOCAL_NUM_SAP
;
500 sdp
= &local
->local_sap
;
505 mutex_lock(&local
->sdp_lock
);
507 clear_bit(local_ssap
, sdp
);
509 mutex_unlock(&local
->sdp_lock
);
512 static u8
nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local
*local
)
516 mutex_lock(&local
->sdp_lock
);
518 ssap
= find_first_zero_bit(&local
->local_sdp
, LLCP_SDP_NUM_SAP
);
519 if (ssap
== LLCP_SDP_NUM_SAP
) {
520 mutex_unlock(&local
->sdp_lock
);
525 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP
+ ssap
);
527 set_bit(ssap
, &local
->local_sdp
);
529 mutex_unlock(&local
->sdp_lock
);
531 return LLCP_WKS_NUM_SAP
+ ssap
;
534 static int nfc_llcp_build_gb(struct nfc_llcp_local
*local
)
536 u8
*gb_cur
, *version_tlv
, version
, version_length
;
537 u8
*lto_tlv
, lto_length
;
538 u8
*wks_tlv
, wks_length
;
539 u8
*miux_tlv
, miux_length
;
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
*)&local
->local_wks
, 2,
554 gb_len
+= wks_length
;
556 miux_tlv
= nfc_llcp_build_tlv(LLCP_TLV_MIUX
, (u8
*)&local
->miux
, 0,
558 gb_len
+= miux_length
;
560 gb_len
+= ARRAY_SIZE(llcp_magic
);
562 if (gb_len
> NFC_MAX_GT_LEN
) {
569 memcpy(gb_cur
, llcp_magic
, ARRAY_SIZE(llcp_magic
));
570 gb_cur
+= ARRAY_SIZE(llcp_magic
);
572 memcpy(gb_cur
, version_tlv
, version_length
);
573 gb_cur
+= version_length
;
575 memcpy(gb_cur
, lto_tlv
, lto_length
);
576 gb_cur
+= lto_length
;
578 memcpy(gb_cur
, wks_tlv
, wks_length
);
579 gb_cur
+= wks_length
;
581 memcpy(gb_cur
, miux_tlv
, miux_length
);
582 gb_cur
+= miux_length
;
584 local
->gb_len
= gb_len
;
595 u8
*nfc_llcp_general_bytes(struct nfc_dev
*dev
, size_t *general_bytes_len
)
597 struct nfc_llcp_local
*local
;
599 local
= nfc_llcp_find_local(dev
);
601 *general_bytes_len
= 0;
605 nfc_llcp_build_gb(local
);
607 *general_bytes_len
= local
->gb_len
;
612 int nfc_llcp_set_remote_gb(struct nfc_dev
*dev
, u8
*gb
, u8 gb_len
)
614 struct nfc_llcp_local
*local
= nfc_llcp_find_local(dev
);
617 pr_err("No LLCP device\n");
623 memset(local
->remote_gb
, 0, NFC_MAX_GT_LEN
);
624 memcpy(local
->remote_gb
, gb
, gb_len
);
625 local
->remote_gb_len
= gb_len
;
627 if (memcmp(local
->remote_gb
, llcp_magic
, 3)) {
628 pr_err("MAC does not support LLCP\n");
632 return nfc_llcp_parse_gb_tlv(local
,
633 &local
->remote_gb
[3],
634 local
->remote_gb_len
- 3);
637 static u8
nfc_llcp_dsap(struct sk_buff
*pdu
)
639 return (pdu
->data
[0] & 0xfc) >> 2;
642 static u8
nfc_llcp_ptype(struct sk_buff
*pdu
)
644 return ((pdu
->data
[0] & 0x03) << 2) | ((pdu
->data
[1] & 0xc0) >> 6);
647 static u8
nfc_llcp_ssap(struct sk_buff
*pdu
)
649 return pdu
->data
[1] & 0x3f;
652 static u8
nfc_llcp_ns(struct sk_buff
*pdu
)
654 return pdu
->data
[2] >> 4;
657 static u8
nfc_llcp_nr(struct sk_buff
*pdu
)
659 return pdu
->data
[2] & 0xf;
662 static void nfc_llcp_set_nrns(struct nfc_llcp_sock
*sock
, struct sk_buff
*pdu
)
664 pdu
->data
[2] = (sock
->send_n
<< 4) | (sock
->recv_n
);
665 sock
->send_n
= (sock
->send_n
+ 1) % 16;
666 sock
->recv_ack_n
= (sock
->recv_n
- 1) % 16;
669 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local
*local
,
670 struct sk_buff
*skb
, u8 direction
)
672 struct sk_buff
*skb_copy
= NULL
, *nskb
;
676 read_lock(&local
->raw_sockets
.lock
);
678 sk_for_each(sk
, &local
->raw_sockets
.head
) {
679 if (sk
->sk_state
!= LLCP_BOUND
)
682 if (skb_copy
== NULL
) {
683 skb_copy
= __pskb_copy(skb
, NFC_LLCP_RAW_HEADER_SIZE
,
686 if (skb_copy
== NULL
)
689 data
= skb_push(skb_copy
, NFC_LLCP_RAW_HEADER_SIZE
);
691 data
[0] = local
->dev
? local
->dev
->idx
: 0xFF;
695 nskb
= skb_clone(skb_copy
, GFP_ATOMIC
);
699 if (sock_queue_rcv_skb(sk
, nskb
))
703 read_unlock(&local
->raw_sockets
.lock
);
708 static void nfc_llcp_tx_work(struct work_struct
*work
)
710 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
714 struct nfc_llcp_sock
*llcp_sock
;
716 skb
= skb_dequeue(&local
->tx_queue
);
719 llcp_sock
= nfc_llcp_sock(sk
);
721 if (llcp_sock
== NULL
&& nfc_llcp_ptype(skb
) == LLCP_PDU_I
) {
722 nfc_llcp_send_symm(local
->dev
);
724 struct sk_buff
*copy_skb
= NULL
;
725 u8 ptype
= nfc_llcp_ptype(skb
);
728 pr_debug("Sending pending skb\n");
729 print_hex_dump(KERN_DEBUG
, "LLCP Tx: ",
730 DUMP_PREFIX_OFFSET
, 16, 1,
731 skb
->data
, skb
->len
, true);
733 if (ptype
== LLCP_PDU_I
)
734 copy_skb
= skb_copy(skb
, GFP_ATOMIC
);
736 __net_timestamp(skb
);
738 nfc_llcp_send_to_raw_sock(local
, skb
,
739 NFC_LLCP_DIRECTION_TX
);
741 ret
= nfc_data_exchange(local
->dev
, local
->target_idx
,
742 skb
, nfc_llcp_recv
, local
);
749 if (ptype
== LLCP_PDU_I
&& copy_skb
)
750 skb_queue_tail(&llcp_sock
->tx_pending_queue
,
754 nfc_llcp_send_symm(local
->dev
);
758 mod_timer(&local
->link_timer
,
759 jiffies
+ msecs_to_jiffies(2 * local
->remote_lto
));
762 static struct nfc_llcp_sock
*nfc_llcp_connecting_sock_get(struct nfc_llcp_local
*local
,
766 struct nfc_llcp_sock
*llcp_sock
;
768 read_lock(&local
->connecting_sockets
.lock
);
770 sk_for_each(sk
, &local
->connecting_sockets
.head
) {
771 llcp_sock
= nfc_llcp_sock(sk
);
773 if (llcp_sock
->ssap
== ssap
) {
774 sock_hold(&llcp_sock
->sk
);
782 read_unlock(&local
->connecting_sockets
.lock
);
787 static struct nfc_llcp_sock
*nfc_llcp_sock_get_sn(struct nfc_llcp_local
*local
,
788 u8
*sn
, size_t sn_len
)
790 struct nfc_llcp_sock
*llcp_sock
;
792 llcp_sock
= nfc_llcp_sock_from_sn(local
, sn
, sn_len
);
794 if (llcp_sock
== NULL
)
797 sock_hold(&llcp_sock
->sk
);
802 static u8
*nfc_llcp_connect_sn(struct sk_buff
*skb
, size_t *sn_len
)
804 u8
*tlv
= &skb
->data
[2], type
, length
;
805 size_t tlv_array_len
= skb
->len
- LLCP_HEADER_SIZE
, offset
= 0;
807 while (offset
< tlv_array_len
) {
811 pr_debug("type 0x%x length %d\n", type
, length
);
813 if (type
== LLCP_TLV_SN
) {
818 offset
+= length
+ 2;
825 static void nfc_llcp_recv_ui(struct nfc_llcp_local
*local
,
828 struct nfc_llcp_sock
*llcp_sock
;
829 struct nfc_llcp_ui_cb
*ui_cb
;
832 dsap
= nfc_llcp_dsap(skb
);
833 ssap
= nfc_llcp_ssap(skb
);
835 ui_cb
= nfc_llcp_ui_skb_cb(skb
);
839 pr_debug("%d %d\n", dsap
, ssap
);
841 /* We're looking for a bound socket, not a client one */
842 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
843 if (llcp_sock
== NULL
|| llcp_sock
->sk
.sk_type
!= SOCK_DGRAM
)
846 /* There is no sequence with UI frames */
847 skb_pull(skb
, LLCP_HEADER_SIZE
);
848 if (!sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
850 * UI frames will be freed from the socket layer, so we
851 * need to keep them alive until someone receives them.
855 pr_err("Receive queue is full\n");
858 nfc_llcp_sock_put(llcp_sock
);
861 static void nfc_llcp_recv_connect(struct nfc_llcp_local
*local
,
864 struct sock
*new_sk
, *parent
;
865 struct nfc_llcp_sock
*sock
, *new_sock
;
866 u8 dsap
, ssap
, reason
;
868 dsap
= nfc_llcp_dsap(skb
);
869 ssap
= nfc_llcp_ssap(skb
);
871 pr_debug("%d %d\n", dsap
, ssap
);
873 if (dsap
!= LLCP_SAP_SDP
) {
874 sock
= nfc_llcp_sock_get(local
, dsap
, LLCP_SAP_SDP
);
875 if (sock
== NULL
|| sock
->sk
.sk_state
!= LLCP_LISTEN
) {
876 reason
= LLCP_DM_NOBOUND
;
883 sn
= nfc_llcp_connect_sn(skb
, &sn_len
);
885 reason
= LLCP_DM_NOBOUND
;
889 pr_debug("Service name length %zu\n", sn_len
);
891 sock
= nfc_llcp_sock_get_sn(local
, sn
, sn_len
);
893 reason
= LLCP_DM_NOBOUND
;
898 lock_sock(&sock
->sk
);
902 if (sk_acceptq_is_full(parent
)) {
903 reason
= LLCP_DM_REJ
;
904 release_sock(&sock
->sk
);
909 if (sock
->ssap
== LLCP_SDP_UNBOUND
) {
910 u8 ssap
= nfc_llcp_reserve_sdp_ssap(local
);
912 pr_debug("First client, reserving %d\n", ssap
);
914 if (ssap
== LLCP_SAP_MAX
) {
915 reason
= LLCP_DM_REJ
;
916 release_sock(&sock
->sk
);
924 new_sk
= nfc_llcp_sock_alloc(NULL
, parent
->sk_type
, GFP_ATOMIC
);
925 if (new_sk
== NULL
) {
926 reason
= LLCP_DM_REJ
;
927 release_sock(&sock
->sk
);
932 new_sock
= nfc_llcp_sock(new_sk
);
933 new_sock
->dev
= local
->dev
;
934 new_sock
->local
= nfc_llcp_local_get(local
);
935 new_sock
->rw
= sock
->rw
;
936 new_sock
->miux
= sock
->miux
;
937 new_sock
->remote_miu
= local
->remote_miu
;
938 new_sock
->nfc_protocol
= sock
->nfc_protocol
;
939 new_sock
->dsap
= ssap
;
940 new_sock
->target_idx
= local
->target_idx
;
941 new_sock
->parent
= parent
;
942 new_sock
->ssap
= sock
->ssap
;
943 if (sock
->ssap
< LLCP_LOCAL_NUM_SAP
&& sock
->ssap
>= LLCP_WKS_NUM_SAP
) {
944 atomic_t
*client_count
;
946 pr_debug("reserved_ssap %d for %p\n", sock
->ssap
, new_sock
);
949 &local
->local_sdp_cnt
[sock
->ssap
- LLCP_WKS_NUM_SAP
];
951 atomic_inc(client_count
);
952 new_sock
->reserved_ssap
= sock
->ssap
;
955 nfc_llcp_parse_connection_tlv(new_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
956 skb
->len
- LLCP_HEADER_SIZE
);
958 pr_debug("new sock %p sk %p\n", new_sock
, &new_sock
->sk
);
960 nfc_llcp_sock_link(&local
->sockets
, new_sk
);
962 nfc_llcp_accept_enqueue(&sock
->sk
, new_sk
);
964 nfc_get_device(local
->dev
->idx
);
966 new_sk
->sk_state
= LLCP_CONNECTED
;
968 /* Wake the listening processes */
969 parent
->sk_data_ready(parent
, 0);
972 nfc_llcp_send_cc(new_sock
);
974 release_sock(&sock
->sk
);
981 nfc_llcp_send_dm(local
, dsap
, ssap
, reason
);
984 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock
*sock
)
987 struct nfc_llcp_local
*local
= sock
->local
;
989 pr_debug("Remote ready %d tx queue len %d remote rw %d",
990 sock
->remote_ready
, skb_queue_len(&sock
->tx_pending_queue
),
993 /* Try to queue some I frames for transmission */
994 while (sock
->remote_ready
&&
995 skb_queue_len(&sock
->tx_pending_queue
) < sock
->remote_rw
) {
998 pdu
= skb_dequeue(&sock
->tx_queue
);
1002 /* Update N(S)/N(R) */
1003 nfc_llcp_set_nrns(sock
, pdu
);
1005 skb_queue_tail(&local
->tx_queue
, pdu
);
1012 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local
*local
,
1013 struct sk_buff
*skb
)
1015 struct nfc_llcp_sock
*llcp_sock
;
1017 u8 dsap
, ssap
, ptype
, ns
, nr
;
1019 ptype
= nfc_llcp_ptype(skb
);
1020 dsap
= nfc_llcp_dsap(skb
);
1021 ssap
= nfc_llcp_ssap(skb
);
1022 ns
= nfc_llcp_ns(skb
);
1023 nr
= nfc_llcp_nr(skb
);
1025 pr_debug("%d %d R %d S %d\n", dsap
, ssap
, nr
, ns
);
1027 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1028 if (llcp_sock
== NULL
) {
1029 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1033 sk
= &llcp_sock
->sk
;
1035 if (sk
->sk_state
== LLCP_CLOSED
) {
1037 nfc_llcp_sock_put(llcp_sock
);
1040 /* Pass the payload upstream */
1041 if (ptype
== LLCP_PDU_I
) {
1042 pr_debug("I frame, queueing on %p\n", &llcp_sock
->sk
);
1044 if (ns
== llcp_sock
->recv_n
)
1045 llcp_sock
->recv_n
= (llcp_sock
->recv_n
+ 1) % 16;
1047 pr_err("Received out of sequence I PDU\n");
1049 skb_pull(skb
, LLCP_HEADER_SIZE
+ LLCP_SEQUENCE_SIZE
);
1050 if (!sock_queue_rcv_skb(&llcp_sock
->sk
, skb
)) {
1052 * I frames will be freed from the socket layer, so we
1053 * need to keep them alive until someone receives them.
1057 pr_err("Receive queue is full\n");
1061 /* Remove skbs from the pending queue */
1062 if (llcp_sock
->send_ack_n
!= nr
) {
1063 struct sk_buff
*s
, *tmp
;
1066 llcp_sock
->send_ack_n
= nr
;
1068 /* Remove and free all skbs until ns == nr */
1069 skb_queue_walk_safe(&llcp_sock
->tx_pending_queue
, s
, tmp
) {
1072 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
1079 /* Re-queue the remaining skbs for transmission */
1080 skb_queue_reverse_walk_safe(&llcp_sock
->tx_pending_queue
,
1082 skb_unlink(s
, &llcp_sock
->tx_pending_queue
);
1083 skb_queue_head(&local
->tx_queue
, s
);
1087 if (ptype
== LLCP_PDU_RR
)
1088 llcp_sock
->remote_ready
= true;
1089 else if (ptype
== LLCP_PDU_RNR
)
1090 llcp_sock
->remote_ready
= false;
1092 if (nfc_llcp_queue_i_frames(llcp_sock
) == 0 && ptype
== LLCP_PDU_I
)
1093 nfc_llcp_send_rr(llcp_sock
);
1096 nfc_llcp_sock_put(llcp_sock
);
1099 static void nfc_llcp_recv_disc(struct nfc_llcp_local
*local
,
1100 struct sk_buff
*skb
)
1102 struct nfc_llcp_sock
*llcp_sock
;
1106 dsap
= nfc_llcp_dsap(skb
);
1107 ssap
= nfc_llcp_ssap(skb
);
1109 if ((dsap
== 0) && (ssap
== 0)) {
1110 pr_debug("Connection termination");
1111 nfc_dep_link_down(local
->dev
);
1115 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1116 if (llcp_sock
== NULL
) {
1117 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1121 sk
= &llcp_sock
->sk
;
1124 nfc_llcp_socket_purge(llcp_sock
);
1126 if (sk
->sk_state
== LLCP_CLOSED
) {
1128 nfc_llcp_sock_put(llcp_sock
);
1131 if (sk
->sk_state
== LLCP_CONNECTED
) {
1132 nfc_put_device(local
->dev
);
1133 sk
->sk_state
= LLCP_CLOSED
;
1134 sk
->sk_state_change(sk
);
1137 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_DISC
);
1140 nfc_llcp_sock_put(llcp_sock
);
1143 static void nfc_llcp_recv_cc(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1145 struct nfc_llcp_sock
*llcp_sock
;
1149 dsap
= nfc_llcp_dsap(skb
);
1150 ssap
= nfc_llcp_ssap(skb
);
1152 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
1153 if (llcp_sock
== NULL
) {
1154 pr_err("Invalid CC\n");
1155 nfc_llcp_send_dm(local
, dsap
, ssap
, LLCP_DM_NOCONN
);
1160 sk
= &llcp_sock
->sk
;
1162 /* Unlink from connecting and link to the client array */
1163 nfc_llcp_sock_unlink(&local
->connecting_sockets
, sk
);
1164 nfc_llcp_sock_link(&local
->sockets
, sk
);
1165 llcp_sock
->dsap
= ssap
;
1167 nfc_llcp_parse_connection_tlv(llcp_sock
, &skb
->data
[LLCP_HEADER_SIZE
],
1168 skb
->len
- LLCP_HEADER_SIZE
);
1170 sk
->sk_state
= LLCP_CONNECTED
;
1171 sk
->sk_state_change(sk
);
1173 nfc_llcp_sock_put(llcp_sock
);
1176 static void nfc_llcp_recv_dm(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1178 struct nfc_llcp_sock
*llcp_sock
;
1180 u8 dsap
, ssap
, reason
;
1182 dsap
= nfc_llcp_dsap(skb
);
1183 ssap
= nfc_llcp_ssap(skb
);
1184 reason
= skb
->data
[2];
1186 pr_debug("%d %d reason %d\n", ssap
, dsap
, reason
);
1189 case LLCP_DM_NOBOUND
:
1191 llcp_sock
= nfc_llcp_connecting_sock_get(local
, dsap
);
1195 llcp_sock
= nfc_llcp_sock_get(local
, dsap
, ssap
);
1199 if (llcp_sock
== NULL
) {
1200 pr_debug("Already closed\n");
1204 sk
= &llcp_sock
->sk
;
1207 sk
->sk_state
= LLCP_CLOSED
;
1208 sk
->sk_state_change(sk
);
1210 nfc_llcp_sock_put(llcp_sock
);
1213 static void nfc_llcp_recv_snl(struct nfc_llcp_local
*local
,
1214 struct sk_buff
*skb
)
1216 struct nfc_llcp_sock
*llcp_sock
;
1217 u8 dsap
, ssap
, *tlv
, type
, length
, tid
, sap
;
1218 u16 tlv_len
, offset
;
1220 size_t service_name_len
;
1221 struct nfc_llcp_sdp_tlv
*sdp
;
1222 HLIST_HEAD(llc_sdres_list
);
1223 size_t sdres_tlvs_len
;
1224 HLIST_HEAD(nl_sdres_list
);
1226 dsap
= nfc_llcp_dsap(skb
);
1227 ssap
= nfc_llcp_ssap(skb
);
1229 pr_debug("%d %d\n", dsap
, ssap
);
1231 if (dsap
!= LLCP_SAP_SDP
|| ssap
!= LLCP_SAP_SDP
) {
1232 pr_err("Wrong SNL SAP\n");
1236 tlv
= &skb
->data
[LLCP_HEADER_SIZE
];
1237 tlv_len
= skb
->len
- LLCP_HEADER_SIZE
;
1241 while (offset
< tlv_len
) {
1246 case LLCP_TLV_SDREQ
:
1248 service_name
= (char *) &tlv
[3];
1249 service_name_len
= length
- 1;
1251 pr_debug("Looking for %.16s\n", service_name
);
1253 if (service_name_len
== strlen("urn:nfc:sn:sdp") &&
1254 !strncmp(service_name
, "urn:nfc:sn:sdp",
1255 service_name_len
)) {
1260 llcp_sock
= nfc_llcp_sock_from_sn(local
, service_name
,
1268 * We found a socket but its ssap has not been reserved
1269 * yet. We need to assign it for good and send a reply.
1270 * The ssap will be freed when the socket is closed.
1272 if (llcp_sock
->ssap
== LLCP_SDP_UNBOUND
) {
1273 atomic_t
*client_count
;
1275 sap
= nfc_llcp_reserve_sdp_ssap(local
);
1277 pr_debug("Reserving %d\n", sap
);
1279 if (sap
== LLCP_SAP_MAX
) {
1285 &local
->local_sdp_cnt
[sap
-
1288 atomic_inc(client_count
);
1290 llcp_sock
->ssap
= sap
;
1291 llcp_sock
->reserved_ssap
= sap
;
1293 sap
= llcp_sock
->ssap
;
1296 pr_debug("%p %d\n", llcp_sock
, sap
);
1299 sdp
= nfc_llcp_build_sdres_tlv(tid
, sap
);
1303 sdres_tlvs_len
+= sdp
->tlv_len
;
1304 hlist_add_head(&sdp
->node
, &llc_sdres_list
);
1307 case LLCP_TLV_SDRES
:
1308 mutex_lock(&local
->sdreq_lock
);
1310 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv
[2]);
1312 hlist_for_each_entry(sdp
, &local
->pending_sdreqs
, node
) {
1313 if (sdp
->tid
!= tlv
[2])
1318 pr_debug("Found: uri=%s, sap=%d\n",
1319 sdp
->uri
, sdp
->sap
);
1321 hlist_del(&sdp
->node
);
1323 hlist_add_head(&sdp
->node
, &nl_sdres_list
);
1328 mutex_unlock(&local
->sdreq_lock
);
1332 pr_err("Invalid SNL tlv value 0x%x\n", type
);
1336 offset
+= length
+ 2;
1341 if (!hlist_empty(&nl_sdres_list
))
1342 nfc_genl_llc_send_sdres(local
->dev
, &nl_sdres_list
);
1344 if (!hlist_empty(&llc_sdres_list
))
1345 nfc_llcp_send_snl_sdres(local
, &llc_sdres_list
, sdres_tlvs_len
);
1348 static void nfc_llcp_recv_agf(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1352 struct sk_buff
*new_skb
;
1354 if (skb
->len
<= LLCP_HEADER_SIZE
) {
1355 pr_err("Malformed AGF PDU\n");
1359 skb_pull(skb
, LLCP_HEADER_SIZE
);
1361 while (skb
->len
> LLCP_AGF_PDU_HEADER_SIZE
) {
1362 pdu_len
= skb
->data
[0] << 8 | skb
->data
[1];
1364 skb_pull(skb
, LLCP_AGF_PDU_HEADER_SIZE
);
1366 if (pdu_len
< LLCP_HEADER_SIZE
|| pdu_len
> skb
->len
) {
1367 pr_err("Malformed AGF PDU\n");
1371 ptype
= nfc_llcp_ptype(skb
);
1373 if (ptype
== LLCP_PDU_SYMM
|| ptype
== LLCP_PDU_AGF
)
1376 new_skb
= nfc_alloc_recv_skb(pdu_len
, GFP_KERNEL
);
1377 if (new_skb
== NULL
) {
1378 pr_err("Could not allocate PDU\n");
1382 memcpy(skb_put(new_skb
, pdu_len
), skb
->data
, pdu_len
);
1384 nfc_llcp_rx_skb(local
, new_skb
);
1388 skb_pull(skb
, pdu_len
);
1392 static void nfc_llcp_rx_skb(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1394 u8 dsap
, ssap
, ptype
;
1396 ptype
= nfc_llcp_ptype(skb
);
1397 dsap
= nfc_llcp_dsap(skb
);
1398 ssap
= nfc_llcp_ssap(skb
);
1400 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype
, dsap
, ssap
);
1402 if (ptype
!= LLCP_PDU_SYMM
)
1403 print_hex_dump(KERN_DEBUG
, "LLCP Rx: ", DUMP_PREFIX_OFFSET
,
1404 16, 1, skb
->data
, skb
->len
, true);
1413 nfc_llcp_recv_ui(local
, skb
);
1416 case LLCP_PDU_CONNECT
:
1417 pr_debug("CONNECT\n");
1418 nfc_llcp_recv_connect(local
, skb
);
1423 nfc_llcp_recv_disc(local
, skb
);
1428 nfc_llcp_recv_cc(local
, skb
);
1433 nfc_llcp_recv_dm(local
, skb
);
1438 nfc_llcp_recv_snl(local
, skb
);
1444 pr_debug("I frame\n");
1445 nfc_llcp_recv_hdlc(local
, skb
);
1449 pr_debug("AGF frame\n");
1450 nfc_llcp_recv_agf(local
, skb
);
1455 static void nfc_llcp_rx_work(struct work_struct
*work
)
1457 struct nfc_llcp_local
*local
= container_of(work
, struct nfc_llcp_local
,
1459 struct sk_buff
*skb
;
1461 skb
= local
->rx_pending
;
1463 pr_debug("No pending SKB\n");
1467 __net_timestamp(skb
);
1469 nfc_llcp_send_to_raw_sock(local
, skb
, NFC_LLCP_DIRECTION_RX
);
1471 nfc_llcp_rx_skb(local
, skb
);
1473 schedule_work(&local
->tx_work
);
1474 kfree_skb(local
->rx_pending
);
1475 local
->rx_pending
= NULL
;
1478 static void __nfc_llcp_recv(struct nfc_llcp_local
*local
, struct sk_buff
*skb
)
1480 local
->rx_pending
= skb
;
1481 del_timer(&local
->link_timer
);
1482 schedule_work(&local
->rx_work
);
1485 void nfc_llcp_recv(void *data
, struct sk_buff
*skb
, int err
)
1487 struct nfc_llcp_local
*local
= (struct nfc_llcp_local
*) data
;
1489 pr_debug("Received an LLCP PDU\n");
1491 pr_err("err %d\n", err
);
1495 __nfc_llcp_recv(local
, skb
);
1498 int nfc_llcp_data_received(struct nfc_dev
*dev
, struct sk_buff
*skb
)
1500 struct nfc_llcp_local
*local
;
1502 local
= nfc_llcp_find_local(dev
);
1506 __nfc_llcp_recv(local
, skb
);
1511 void nfc_llcp_mac_is_down(struct nfc_dev
*dev
)
1513 struct nfc_llcp_local
*local
;
1515 local
= nfc_llcp_find_local(dev
);
1519 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1520 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1522 /* Close and purge all existing sockets */
1523 nfc_llcp_socket_release(local
, true, 0);
1526 void nfc_llcp_mac_is_up(struct nfc_dev
*dev
, u32 target_idx
,
1527 u8 comm_mode
, u8 rf_mode
)
1529 struct nfc_llcp_local
*local
;
1531 pr_debug("rf mode %d\n", rf_mode
);
1533 local
= nfc_llcp_find_local(dev
);
1537 local
->target_idx
= target_idx
;
1538 local
->comm_mode
= comm_mode
;
1539 local
->rf_mode
= rf_mode
;
1541 if (rf_mode
== NFC_RF_INITIATOR
) {
1542 pr_debug("Queueing Tx work\n");
1544 schedule_work(&local
->tx_work
);
1546 mod_timer(&local
->link_timer
,
1547 jiffies
+ msecs_to_jiffies(local
->remote_lto
));
1551 int nfc_llcp_register_device(struct nfc_dev
*ndev
)
1553 struct nfc_llcp_local
*local
;
1555 local
= kzalloc(sizeof(struct nfc_llcp_local
), GFP_KERNEL
);
1560 INIT_LIST_HEAD(&local
->list
);
1561 kref_init(&local
->ref
);
1562 mutex_init(&local
->sdp_lock
);
1563 init_timer(&local
->link_timer
);
1564 local
->link_timer
.data
= (unsigned long) local
;
1565 local
->link_timer
.function
= nfc_llcp_symm_timer
;
1567 skb_queue_head_init(&local
->tx_queue
);
1568 INIT_WORK(&local
->tx_work
, nfc_llcp_tx_work
);
1570 local
->rx_pending
= NULL
;
1571 INIT_WORK(&local
->rx_work
, nfc_llcp_rx_work
);
1573 INIT_WORK(&local
->timeout_work
, nfc_llcp_timeout_work
);
1575 rwlock_init(&local
->sockets
.lock
);
1576 rwlock_init(&local
->connecting_sockets
.lock
);
1577 rwlock_init(&local
->raw_sockets
.lock
);
1579 local
->lto
= 150; /* 1500 ms */
1580 local
->rw
= LLCP_MAX_RW
;
1581 local
->miux
= cpu_to_be16(LLCP_MAX_MIUX
);
1583 nfc_llcp_build_gb(local
);
1585 local
->remote_miu
= LLCP_DEFAULT_MIU
;
1586 local
->remote_lto
= LLCP_DEFAULT_LTO
;
1588 mutex_init(&local
->sdreq_lock
);
1589 INIT_HLIST_HEAD(&local
->pending_sdreqs
);
1590 init_timer(&local
->sdreq_timer
);
1591 local
->sdreq_timer
.data
= (unsigned long) local
;
1592 local
->sdreq_timer
.function
= nfc_llcp_sdreq_timer
;
1593 INIT_WORK(&local
->sdreq_timeout_work
, nfc_llcp_sdreq_timeout_work
);
1595 list_add(&local
->list
, &llcp_devices
);
1600 void nfc_llcp_unregister_device(struct nfc_dev
*dev
)
1602 struct nfc_llcp_local
*local
= nfc_llcp_find_local(dev
);
1604 if (local
== NULL
) {
1605 pr_debug("No such device\n");
1609 local_cleanup(local
);
1611 nfc_llcp_local_put(local
);
1614 int __init
nfc_llcp_init(void)
1616 INIT_LIST_HEAD(&llcp_devices
);
1618 return nfc_llcp_sock_init();
1621 void nfc_llcp_exit(void)
1623 nfc_llcp_sock_exit();