1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Copyright (C) 2011 Intel Corporation. All rights reserved. 4*4882a593Smuzhiyun */ 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun enum llcp_state { 7*4882a593Smuzhiyun LLCP_CONNECTED = 1, /* wait_for_packet() wants that */ 8*4882a593Smuzhiyun LLCP_CONNECTING, 9*4882a593Smuzhiyun LLCP_DISCONNECTING, 10*4882a593Smuzhiyun LLCP_CLOSED, 11*4882a593Smuzhiyun LLCP_BOUND, 12*4882a593Smuzhiyun LLCP_LISTEN, 13*4882a593Smuzhiyun }; 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun #define LLCP_DEFAULT_LTO 100 16*4882a593Smuzhiyun #define LLCP_DEFAULT_RW 1 17*4882a593Smuzhiyun #define LLCP_DEFAULT_MIU 128 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun #define LLCP_MAX_LTO 0xff 20*4882a593Smuzhiyun #define LLCP_MAX_RW 15 21*4882a593Smuzhiyun #define LLCP_MAX_MIUX 0x7ff 22*4882a593Smuzhiyun #define LLCP_MAX_MIU (LLCP_MAX_MIUX + 128) 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun #define LLCP_WKS_NUM_SAP 16 25*4882a593Smuzhiyun #define LLCP_SDP_NUM_SAP 16 26*4882a593Smuzhiyun #define LLCP_LOCAL_NUM_SAP 32 27*4882a593Smuzhiyun #define LLCP_LOCAL_SAP_OFFSET (LLCP_WKS_NUM_SAP + LLCP_SDP_NUM_SAP) 28*4882a593Smuzhiyun #define LLCP_MAX_SAP (LLCP_WKS_NUM_SAP + LLCP_SDP_NUM_SAP + LLCP_LOCAL_NUM_SAP) 29*4882a593Smuzhiyun #define LLCP_SDP_UNBOUND (LLCP_MAX_SAP + 1) 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun struct nfc_llcp_sock; 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun struct llcp_sock_list { 34*4882a593Smuzhiyun struct hlist_head head; 35*4882a593Smuzhiyun rwlock_t lock; 36*4882a593Smuzhiyun }; 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun struct nfc_llcp_sdp_tlv { 39*4882a593Smuzhiyun u8 *tlv; 40*4882a593Smuzhiyun u8 tlv_len; 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun char *uri; 43*4882a593Smuzhiyun u8 tid; 44*4882a593Smuzhiyun u8 sap; 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun unsigned long time; 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun struct hlist_node node; 49*4882a593Smuzhiyun }; 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun struct nfc_llcp_local { 52*4882a593Smuzhiyun struct list_head list; 53*4882a593Smuzhiyun struct nfc_dev *dev; 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun struct kref ref; 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun struct mutex sdp_lock; 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun struct timer_list link_timer; 60*4882a593Smuzhiyun struct sk_buff_head tx_queue; 61*4882a593Smuzhiyun struct work_struct tx_work; 62*4882a593Smuzhiyun struct work_struct rx_work; 63*4882a593Smuzhiyun struct sk_buff *rx_pending; 64*4882a593Smuzhiyun struct work_struct timeout_work; 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun u32 target_idx; 67*4882a593Smuzhiyun u8 rf_mode; 68*4882a593Smuzhiyun u8 comm_mode; 69*4882a593Smuzhiyun u8 lto; 70*4882a593Smuzhiyun u8 rw; 71*4882a593Smuzhiyun __be16 miux; 72*4882a593Smuzhiyun unsigned long local_wks; /* Well known services */ 73*4882a593Smuzhiyun unsigned long local_sdp; /* Local services */ 74*4882a593Smuzhiyun unsigned long local_sap; /* Local SAPs, not available for discovery */ 75*4882a593Smuzhiyun atomic_t local_sdp_cnt[LLCP_SDP_NUM_SAP]; 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun /* local */ 78*4882a593Smuzhiyun u8 gb[NFC_MAX_GT_LEN]; 79*4882a593Smuzhiyun u8 gb_len; 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun /* remote */ 82*4882a593Smuzhiyun u8 remote_gb[NFC_MAX_GT_LEN]; 83*4882a593Smuzhiyun u8 remote_gb_len; 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun u8 remote_version; 86*4882a593Smuzhiyun u16 remote_miu; 87*4882a593Smuzhiyun u16 remote_lto; 88*4882a593Smuzhiyun u8 remote_opt; 89*4882a593Smuzhiyun u16 remote_wks; 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun struct mutex sdreq_lock; 92*4882a593Smuzhiyun struct hlist_head pending_sdreqs; 93*4882a593Smuzhiyun struct timer_list sdreq_timer; 94*4882a593Smuzhiyun struct work_struct sdreq_timeout_work; 95*4882a593Smuzhiyun u8 sdreq_next_tid; 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun /* sockets array */ 98*4882a593Smuzhiyun struct llcp_sock_list sockets; 99*4882a593Smuzhiyun struct llcp_sock_list connecting_sockets; 100*4882a593Smuzhiyun struct llcp_sock_list raw_sockets; 101*4882a593Smuzhiyun }; 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun struct nfc_llcp_sock { 104*4882a593Smuzhiyun struct sock sk; 105*4882a593Smuzhiyun struct nfc_dev *dev; 106*4882a593Smuzhiyun struct nfc_llcp_local *local; 107*4882a593Smuzhiyun u32 target_idx; 108*4882a593Smuzhiyun u32 nfc_protocol; 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun /* Link parameters */ 111*4882a593Smuzhiyun u8 ssap; 112*4882a593Smuzhiyun u8 dsap; 113*4882a593Smuzhiyun char *service_name; 114*4882a593Smuzhiyun size_t service_name_len; 115*4882a593Smuzhiyun u8 rw; 116*4882a593Smuzhiyun __be16 miux; 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun /* Remote link parameters */ 120*4882a593Smuzhiyun u8 remote_rw; 121*4882a593Smuzhiyun u16 remote_miu; 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun /* Link variables */ 124*4882a593Smuzhiyun u8 send_n; 125*4882a593Smuzhiyun u8 send_ack_n; 126*4882a593Smuzhiyun u8 recv_n; 127*4882a593Smuzhiyun u8 recv_ack_n; 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun /* Is the remote peer ready to receive */ 130*4882a593Smuzhiyun u8 remote_ready; 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun /* Reserved source SAP */ 133*4882a593Smuzhiyun u8 reserved_ssap; 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun struct sk_buff_head tx_queue; 136*4882a593Smuzhiyun struct sk_buff_head tx_pending_queue; 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun struct list_head accept_queue; 139*4882a593Smuzhiyun struct sock *parent; 140*4882a593Smuzhiyun }; 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun struct nfc_llcp_ui_cb { 143*4882a593Smuzhiyun __u8 dsap; 144*4882a593Smuzhiyun __u8 ssap; 145*4882a593Smuzhiyun }; 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun #define nfc_llcp_ui_skb_cb(__skb) ((struct nfc_llcp_ui_cb *)&((__skb)->cb[0])) 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun #define nfc_llcp_sock(sk) ((struct nfc_llcp_sock *) (sk)) 150*4882a593Smuzhiyun #define nfc_llcp_dev(sk) (nfc_llcp_sock((sk))->dev) 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun #define LLCP_HEADER_SIZE 2 153*4882a593Smuzhiyun #define LLCP_SEQUENCE_SIZE 1 154*4882a593Smuzhiyun #define LLCP_AGF_PDU_HEADER_SIZE 2 155*4882a593Smuzhiyun 156*4882a593Smuzhiyun /* LLCP versions: 1.1 is 1.0 plus SDP */ 157*4882a593Smuzhiyun #define LLCP_VERSION_10 0x10 158*4882a593Smuzhiyun #define LLCP_VERSION_11 0x11 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun /* LLCP PDU types */ 161*4882a593Smuzhiyun #define LLCP_PDU_SYMM 0x0 162*4882a593Smuzhiyun #define LLCP_PDU_PAX 0x1 163*4882a593Smuzhiyun #define LLCP_PDU_AGF 0x2 164*4882a593Smuzhiyun #define LLCP_PDU_UI 0x3 165*4882a593Smuzhiyun #define LLCP_PDU_CONNECT 0x4 166*4882a593Smuzhiyun #define LLCP_PDU_DISC 0x5 167*4882a593Smuzhiyun #define LLCP_PDU_CC 0x6 168*4882a593Smuzhiyun #define LLCP_PDU_DM 0x7 169*4882a593Smuzhiyun #define LLCP_PDU_FRMR 0x8 170*4882a593Smuzhiyun #define LLCP_PDU_SNL 0x9 171*4882a593Smuzhiyun #define LLCP_PDU_I 0xc 172*4882a593Smuzhiyun #define LLCP_PDU_RR 0xd 173*4882a593Smuzhiyun #define LLCP_PDU_RNR 0xe 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun /* Parameters TLV types */ 176*4882a593Smuzhiyun #define LLCP_TLV_VERSION 0x1 177*4882a593Smuzhiyun #define LLCP_TLV_MIUX 0x2 178*4882a593Smuzhiyun #define LLCP_TLV_WKS 0x3 179*4882a593Smuzhiyun #define LLCP_TLV_LTO 0x4 180*4882a593Smuzhiyun #define LLCP_TLV_RW 0x5 181*4882a593Smuzhiyun #define LLCP_TLV_SN 0x6 182*4882a593Smuzhiyun #define LLCP_TLV_OPT 0x7 183*4882a593Smuzhiyun #define LLCP_TLV_SDREQ 0x8 184*4882a593Smuzhiyun #define LLCP_TLV_SDRES 0x9 185*4882a593Smuzhiyun #define LLCP_TLV_MAX 0xa 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun /* Well known LLCP SAP */ 188*4882a593Smuzhiyun #define LLCP_SAP_SDP 0x1 189*4882a593Smuzhiyun #define LLCP_SAP_IP 0x2 190*4882a593Smuzhiyun #define LLCP_SAP_OBEX 0x3 191*4882a593Smuzhiyun #define LLCP_SAP_SNEP 0x4 192*4882a593Smuzhiyun #define LLCP_SAP_MAX 0xff 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun /* Disconnection reason code */ 195*4882a593Smuzhiyun #define LLCP_DM_DISC 0x00 196*4882a593Smuzhiyun #define LLCP_DM_NOCONN 0x01 197*4882a593Smuzhiyun #define LLCP_DM_NOBOUND 0x02 198*4882a593Smuzhiyun #define LLCP_DM_REJ 0x03 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun 201*4882a593Smuzhiyun void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *s); 202*4882a593Smuzhiyun void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *s); 203*4882a593Smuzhiyun void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock); 204*4882a593Smuzhiyun struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev); 205*4882a593Smuzhiyun struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local); 206*4882a593Smuzhiyun int nfc_llcp_local_put(struct nfc_llcp_local *local); 207*4882a593Smuzhiyun u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local, 208*4882a593Smuzhiyun struct nfc_llcp_sock *sock); 209*4882a593Smuzhiyun u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local); 210*4882a593Smuzhiyun void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap); 211*4882a593Smuzhiyun int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock); 212*4882a593Smuzhiyun void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local, 213*4882a593Smuzhiyun struct sk_buff *skb, u8 direction); 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun /* Sock API */ 216*4882a593Smuzhiyun struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp, int kern); 217*4882a593Smuzhiyun void nfc_llcp_sock_free(struct nfc_llcp_sock *sock); 218*4882a593Smuzhiyun void nfc_llcp_accept_unlink(struct sock *sk); 219*4882a593Smuzhiyun void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk); 220*4882a593Smuzhiyun struct sock *nfc_llcp_accept_dequeue(struct sock *sk, struct socket *newsock); 221*4882a593Smuzhiyun 222*4882a593Smuzhiyun /* TLV API */ 223*4882a593Smuzhiyun int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local, 224*4882a593Smuzhiyun u8 *tlv_array, u16 tlv_array_len); 225*4882a593Smuzhiyun int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock, 226*4882a593Smuzhiyun u8 *tlv_array, u16 tlv_array_len); 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun /* Commands API */ 229*4882a593Smuzhiyun void nfc_llcp_recv(void *data, struct sk_buff *skb, int err); 230*4882a593Smuzhiyun u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length); 231*4882a593Smuzhiyun struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdres_tlv(u8 tid, u8 sap); 232*4882a593Smuzhiyun struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdreq_tlv(u8 tid, char *uri, 233*4882a593Smuzhiyun size_t uri_len); 234*4882a593Smuzhiyun void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp); 235*4882a593Smuzhiyun void nfc_llcp_free_sdp_tlv_list(struct hlist_head *sdp_head); 236*4882a593Smuzhiyun void nfc_llcp_recv(void *data, struct sk_buff *skb, int err); 237*4882a593Smuzhiyun int nfc_llcp_send_symm(struct nfc_dev *dev); 238*4882a593Smuzhiyun int nfc_llcp_send_connect(struct nfc_llcp_sock *sock); 239*4882a593Smuzhiyun int nfc_llcp_send_cc(struct nfc_llcp_sock *sock); 240*4882a593Smuzhiyun int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local, 241*4882a593Smuzhiyun struct hlist_head *tlv_list, size_t tlvs_len); 242*4882a593Smuzhiyun int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local, 243*4882a593Smuzhiyun struct hlist_head *tlv_list, size_t tlvs_len); 244*4882a593Smuzhiyun int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason); 245*4882a593Smuzhiyun int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock); 246*4882a593Smuzhiyun int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, 247*4882a593Smuzhiyun struct msghdr *msg, size_t len); 248*4882a593Smuzhiyun int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, 249*4882a593Smuzhiyun struct msghdr *msg, size_t len); 250*4882a593Smuzhiyun int nfc_llcp_send_rr(struct nfc_llcp_sock *sock); 251*4882a593Smuzhiyun 252*4882a593Smuzhiyun /* Socket API */ 253*4882a593Smuzhiyun int __init nfc_llcp_sock_init(void); 254*4882a593Smuzhiyun void nfc_llcp_sock_exit(void); 255