20 #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
23 #include <linux/kernel.h>
24 #include <linux/list.h>
30 static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
37 sk_add_node(sk, &l->
head);
48 static void nfc_llcp_socket_release(
struct nfc_llcp_local *local,
bool listen)
62 nfc_put_device(llcp_sock->
dev);
66 struct sock *accept_sk;
79 sock_orphan(accept_sk);
102 kref_get(&local->
ref);
107 static void local_release(
struct kref *
ref)
114 nfc_llcp_socket_release(local,
false);
129 return kref_put(&local->
ref, local_release);
139 pr_debug(
"ssap dsap %d %d\n", ssap, dsap);
141 if (ssap == 0 && dsap == 0)
151 if (llcp_sock->
ssap == ssap && llcp_sock->
dsap == dsap)
157 if (llcp_sock ==
NULL)
160 sock_hold(&llcp_sock->
sk);
178 static void nfc_llcp_symm_timer(
unsigned long data)
192 if (local->
dev == dev)
200 static char *wks[] = {
208 static int nfc_llcp_wks_sap(
char *service_name,
size_t service_name_len)
214 if (service_name ==
NULL)
219 for (sap = 0; sap < num_wks; sap++) {
220 if (wks[sap] ==
NULL)
223 if (
strncmp(wks[sap], service_name, service_name_len) == 0)
232 u8 *
sn,
size_t sn_len)
238 pr_debug(
"sn %zd %p\n", sn_len, sn);
240 if (sn ==
NULL || sn_len == 0)
250 pr_debug(
"llcp sock %p\n", tmp_sock);
263 llcp_sock = tmp_sock;
270 pr_debug(
"Found llcp sock %p\n", llcp_sock);
370 pr_debug(
"No more clients for SAP %d\n", ssap);
378 nfc_llcp_sock_put(l_sock);
423 u8 *gb_cur, *version_tlv,
version, version_length;
424 u8 *lto_tlv, lto, lto_length;
425 u8 *wks_tlv, wks_length;
426 u8 *miux_tlv, miux_length;
434 gb_len += version_length;
439 gb_len += lto_length;
444 gb_len += wks_length;
449 gb_len += miux_length;
463 memcpy(gb_cur, version_tlv, version_length);
464 gb_cur += version_length;
466 memcpy(gb_cur, lto_tlv, lto_length);
467 gb_cur += lto_length;
469 memcpy(gb_cur, wks_tlv, wks_length);
470 gb_cur += wks_length;
472 memcpy(gb_cur, miux_tlv, miux_length);
473 gb_cur += miux_length;
492 *general_bytes_len = 0;
496 nfc_llcp_build_gb(local);
498 *general_bytes_len = local->
gb_len;
508 pr_err(
"No LLCP device\n");
520 pr_err(
"MAC does not support LLCP\n");
531 return (pdu->
data[0] & 0xfc) >> 2;
534 static u8 nfc_llcp_ptype(
struct sk_buff *pdu)
536 return ((pdu->
data[0] & 0x03) << 2) | ((pdu->
data[1] & 0xc0) >> 6);
539 static u8 nfc_llcp_ssap(
struct sk_buff *pdu)
541 return pdu->
data[1] & 0x3f;
544 static u8 nfc_llcp_ns(
struct sk_buff *pdu)
546 return pdu->
data[2] >> 4;
549 static u8 nfc_llcp_nr(
struct sk_buff *pdu)
551 return pdu->
data[2] & 0xf;
575 if (skb_copy ==
NULL) {
579 if (skb_copy ==
NULL)
584 data[0] = local->
dev ? local->
dev->idx : 0xFF;
613 if (llcp_sock !=
NULL) {
627 if (!ret && nfc_llcp_ptype(skb) ==
LLCP_PDU_I) {
655 if (llcp_sock->
ssap == ssap) {
656 sock_hold(&llcp_sock->
sk);
670 u8 *sn,
size_t sn_len)
674 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
676 if (llcp_sock ==
NULL)
679 sock_hold(&llcp_sock->
sk);
684 static u8 *nfc_llcp_connect_sn(
struct sk_buff *skb,
size_t *sn_len)
689 while (offset < tlv_array_len) {
700 offset += length + 2;
710 struct sock *new_sk, *
parent;
714 dsap = nfc_llcp_dsap(skb);
715 ssap = nfc_llcp_ssap(skb);
729 sn = nfc_llcp_connect_sn(skb, &sn_len);
735 pr_debug(
"Service name length %zu\n", sn_len);
737 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
744 lock_sock(&sock->
sk);
748 if (sk_acceptq_is_full(parent)) {
756 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
758 pr_debug(
"First client, reserving %d\n", ssap);
771 if (new_sk ==
NULL) {
779 new_sock->
dev = local->
dev;
790 pr_debug(
"reserved_ssap %d for %p\n", sock->
ssap, new_sock);
802 pr_debug(
"new sock %p sk %p\n", new_sock, &new_sock->
sk);
836 pr_debug(
"Remote ready %d tx queue len %d remote rw %d",
850 nfc_llcp_set_nrns(sock, pdu);
866 ptype = nfc_llcp_ptype(skb);
867 dsap = nfc_llcp_dsap(skb);
868 ssap = nfc_llcp_ssap(skb);
869 ns = nfc_llcp_ns(skb);
870 nr = nfc_llcp_nr(skb);
872 pr_debug(
"%d %d R %d S %d\n", dsap, ssap, nr, ns);
874 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
875 if (llcp_sock ==
NULL) {
884 nfc_llcp_sock_put(llcp_sock);
889 pr_debug(
"I frame, queueing on %p\n", &llcp_sock->
sk);
891 if (ns == llcp_sock->
recv_n)
894 pr_err(
"Received out of sequence I PDU\n");
898 pr_err(
"receive queue is full\n");
914 if (nfc_llcp_ns(s) == nr)
935 nfc_llcp_sock_put(llcp_sock);
945 dsap = nfc_llcp_dsap(skb);
946 ssap = nfc_llcp_ssap(skb);
948 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
949 if (llcp_sock ==
NULL) {
958 nfc_llcp_sock_put(llcp_sock);
962 nfc_put_device(local->
dev);
970 nfc_llcp_sock_put(llcp_sock);
979 dsap = nfc_llcp_dsap(skb);
980 ssap = nfc_llcp_ssap(skb);
982 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
983 if (llcp_sock ==
NULL) {
1003 nfc_llcp_sock_put(llcp_sock);
1012 dsap = nfc_llcp_dsap(skb);
1013 ssap = nfc_llcp_ssap(skb);
1014 reason = skb->
data[2];
1016 pr_debug(
"%d %d reason %d\n", ssap, dsap, reason);
1021 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1025 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1029 if (llcp_sock ==
NULL) {
1034 sk = &llcp_sock->
sk;
1040 nfc_llcp_sock_put(llcp_sock);
1045 static void nfc_llcp_rx_work(
struct work_struct *work)
1058 ptype = nfc_llcp_ptype(skb);
1059 dsap = nfc_llcp_dsap(skb);
1060 ssap = nfc_llcp_ssap(skb);
1062 pr_debug(
"ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1066 16, 1, skb->
data, skb->
len,
true);
1077 nfc_llcp_recv_connect(local, skb);
1082 nfc_llcp_recv_disc(local, skb);
1087 nfc_llcp_recv_cc(local, skb);
1092 nfc_llcp_recv_dm(local, skb);
1099 nfc_llcp_recv_hdlc(local, skb);
1115 pr_debug(
"Received an LLCP PDU\n");
1152 nfc_llcp_socket_release(local,
true);
1189 INIT_LIST_HEAD(&local->
list);
1190 kref_init(&local->
ref);
1194 local->
link_timer.function = nfc_llcp_symm_timer;
1196 skb_queue_head_init(&local->
tx_queue);
1208 nfc_llcp_build_gb(local);
1213 list_add(&local->
list, &llcp_devices);
1222 if (local ==
NULL) {
1232 INIT_LIST_HEAD(&llcp_devices);