From 5eef6669759f8e291ab0347894876b532c242324 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Wed, 20 Mar 2013 16:06:12 +0100 Subject: NFC: llcp: Socket miux is a big endian field The MIUX must be transmitted in big endian and as such we have to convert it properly. Signed-off-by: Samuel Ortiz --- net/nfc/llcp/commands.c | 6 ++++-- net/nfc/llcp/llcp.h | 2 +- net/nfc/llcp/sock.c | 7 ++++--- 3 files changed, 9 insertions(+), 6 deletions(-) (limited to 'net/nfc') diff --git a/net/nfc/llcp/commands.c b/net/nfc/llcp/commands.c index b75a9b3f9e89..c5535cc9ed3a 100644 --- a/net/nfc/llcp/commands.c +++ b/net/nfc/llcp/commands.c @@ -420,7 +420,8 @@ int nfc_llcp_send_connect(struct nfc_llcp_sock *sock) } /* If the socket parameters are not set, use the local ones */ - miux = sock->miux > LLCP_MAX_MIUX ? local->miux : sock->miux; + miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ? + local->miux : sock->miux; rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw; miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, @@ -475,7 +476,8 @@ int nfc_llcp_send_cc(struct nfc_llcp_sock *sock) return -ENODEV; /* If the socket parameters are not set, use the local ones */ - miux = sock->miux > LLCP_MAX_MIUX ? local->miux : sock->miux; + miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ? + local->miux : sock->miux; rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw; miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, diff --git a/net/nfc/llcp/llcp.h b/net/nfc/llcp/llcp.h index 7e87a66b02ec..53054d337bf9 100644 --- a/net/nfc/llcp/llcp.h +++ b/net/nfc/llcp/llcp.h @@ -124,7 +124,7 @@ struct nfc_llcp_sock { char *service_name; size_t service_name_len; u8 rw; - u16 miux; + __be16 miux; /* Remote link parameters */ diff --git a/net/nfc/llcp/sock.c b/net/nfc/llcp/sock.c index 6fa76704cb13..873c837e5c97 100644 --- a/net/nfc/llcp/sock.c +++ b/net/nfc/llcp/sock.c @@ -279,7 +279,7 @@ static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname, break; } - llcp_sock->miux = (u16) opt; + llcp_sock->miux = cpu_to_be16((u16) opt); break; @@ -323,7 +323,8 @@ static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname, break; case NFC_LLCP_MIUX: - if (put_user(llcp_sock->miux, (u32 __user *) optval)) + if (put_user(be16_to_cpu(llcp_sock->miux), + (u32 __user *) optval)) err = -EFAULT; break; @@ -921,7 +922,7 @@ struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp) llcp_sock->ssap = 0; llcp_sock->dsap = LLCP_SAP_SDP; llcp_sock->rw = LLCP_MAX_RW + 1; - llcp_sock->miux = LLCP_MAX_MIUX + 1; + llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1); llcp_sock->remote_rw = LLCP_DEFAULT_RW; llcp_sock->remote_miu = LLCP_DEFAULT_MIU; llcp_sock->send_n = llcp_sock->send_ack_n = 0; -- cgit v1.2.3 From 00e856db49bbaf0ec315bf81a3c4fc02e4d0beea Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Wed, 20 Mar 2013 16:36:13 +0100 Subject: NFC: llcp: Fall back to local values when getting socket options If a socket option has not been set by the user, fall back to the LLCP local ones. Signed-off-by: Samuel Ortiz --- net/nfc/llcp/sock.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'net/nfc') diff --git a/net/nfc/llcp/sock.c b/net/nfc/llcp/sock.c index 873c837e5c97..f3027c21c442 100644 --- a/net/nfc/llcp/sock.c +++ b/net/nfc/llcp/sock.c @@ -299,9 +299,12 @@ static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname, static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) { + struct nfc_llcp_local *local; struct sock *sk = sock->sk; struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); int len, err = 0; + u16 miux; + u8 rw; pr_debug("%p optname %d\n", sk, optname); @@ -311,20 +314,27 @@ static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname, if (get_user(len, optlen)) return -EFAULT; + local = llcp_sock->local; + if (!local) + return -ENODEV; + len = min_t(u32, len, sizeof(u32)); lock_sock(sk); switch (optname) { case NFC_LLCP_RW: - if (put_user(llcp_sock->rw, (u32 __user *) optval)) + rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw; + if (put_user(rw, (u32 __user *) optval)) err = -EFAULT; break; case NFC_LLCP_MIUX: - if (put_user(be16_to_cpu(llcp_sock->miux), - (u32 __user *) optval)) + miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ? + be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux); + + if (put_user(miux, (u32 __user *) optval)) err = -EFAULT; break; -- cgit v1.2.3 From 0b23d666a8857e521384d0eec75a7362b80a39b8 Mon Sep 17 00:00:00 2001 From: Olivier Guiter Date: Mon, 25 Mar 2013 11:24:21 +0100 Subject: NFC: llcp: Fix zero octets length SDU handling LLCP Validation test #2 (Connection-less information transfer) send a service data unit of zero octets length. This is now handled correctly. Signed-off-by: Olivier Guiter Signed-off-by: Samuel Ortiz --- net/nfc/llcp/commands.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'net/nfc') diff --git a/net/nfc/llcp/commands.c b/net/nfc/llcp/commands.c index c5535cc9ed3a..199e8b5514f9 100644 --- a/net/nfc/llcp/commands.c +++ b/net/nfc/llcp/commands.c @@ -694,8 +694,7 @@ int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, remaining_len = len; msg_ptr = msg_data; - while (remaining_len > 0) { - + do { frag_len = min_t(size_t, sock->remote_miu, remaining_len); pr_debug("Fragment %zd bytes remaining %zd", @@ -708,7 +707,8 @@ int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, skb_put(pdu, LLCP_SEQUENCE_SIZE); - memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); + if (likely(frag_len > 0)) + memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); skb_queue_tail(&sock->tx_queue, pdu); @@ -720,7 +720,7 @@ int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, remaining_len -= frag_len; msg_ptr += frag_len; - } + } while (remaining_len > 0); kfree(msg_data); @@ -754,8 +754,7 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, remaining_len = len; msg_ptr = msg_data; - while (remaining_len > 0) { - + do { frag_len = min_t(size_t, sock->remote_miu, remaining_len); pr_debug("Fragment %zd bytes remaining %zd", @@ -770,14 +769,15 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI); - memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); + if (likely(frag_len > 0)) + memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); /* No need to check for the peer RW for UI frames */ skb_queue_tail(&local->tx_queue, pdu); remaining_len -= frag_len; msg_ptr += frag_len; - } + } while (remaining_len > 0); kfree(msg_data); -- cgit v1.2.3 From 098dafcfb4db0d3c08cffec88c87bbb2f4513f20 Mon Sep 17 00:00:00 2001 From: Thierry Escande Date: Fri, 29 Mar 2013 11:47:43 +0100 Subject: NFC: llcp: Aggregated frames support This adds support for AGF PDUs. For each PDU contained in the AGF, a new sk_buff is allocated and dispatched to its corresponding handler. Signed-off-by: Thierry Escande Signed-off-by: Samuel Ortiz --- net/nfc/llcp/llcp.c | 80 +++++++++++++++++++++++++++++++++++++++++++++-------- net/nfc/llcp/llcp.h | 1 + 2 files changed, 69 insertions(+), 12 deletions(-) (limited to 'net/nfc') diff --git a/net/nfc/llcp/llcp.c b/net/nfc/llcp/llcp.c index 7de0368aff0c..79de8bafd426 100644 --- a/net/nfc/llcp/llcp.c +++ b/net/nfc/llcp/llcp.c @@ -31,6 +31,8 @@ static u8 llcp_magic[3] = {0x46, 0x66, 0x6d}; static struct list_head llcp_devices; +static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb); + void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk) { write_lock(&l->lock); @@ -1349,19 +1351,54 @@ exit: nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len); } -static void nfc_llcp_rx_work(struct work_struct *work) +static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb) { - struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, - rx_work); - u8 dsap, ssap, ptype; - struct sk_buff *skb; + u8 ptype; + u16 pdu_len; + struct sk_buff *new_skb; - skb = local->rx_pending; - if (skb == NULL) { - pr_debug("No pending SKB\n"); + if (skb->len <= LLCP_HEADER_SIZE) { + pr_err("Malformed AGF PDU\n"); return; } + skb_pull(skb, LLCP_HEADER_SIZE); + + while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) { + pdu_len = skb->data[0] << 8 | skb->data[1]; + + skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE); + + if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) { + pr_err("Malformed AGF PDU\n"); + return; + } + + ptype = nfc_llcp_ptype(skb); + + if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF) + goto next; + + new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL); + if (new_skb == NULL) { + pr_err("Could not allocate PDU\n"); + return; + } + + memcpy(skb_put(new_skb, pdu_len), skb->data, pdu_len); + + nfc_llcp_rx_skb(local, new_skb); + + kfree_skb(new_skb); +next: + skb_pull(skb, pdu_len); + } +} + +static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb) +{ + u8 dsap, ssap, ptype; + ptype = nfc_llcp_ptype(skb); dsap = nfc_llcp_dsap(skb); ssap = nfc_llcp_ssap(skb); @@ -1372,10 +1409,6 @@ static void nfc_llcp_rx_work(struct work_struct *work) print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET, 16, 1, skb->data, skb->len, true); - __net_timestamp(skb); - - nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX); - switch (ptype) { case LLCP_PDU_SYMM: pr_debug("SYMM\n"); @@ -1418,7 +1451,30 @@ static void nfc_llcp_rx_work(struct work_struct *work) nfc_llcp_recv_hdlc(local, skb); break; + case LLCP_PDU_AGF: + pr_debug("AGF frame\n"); + nfc_llcp_recv_agf(local, skb); + break; } +} + +static void nfc_llcp_rx_work(struct work_struct *work) +{ + struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, + rx_work); + struct sk_buff *skb; + + skb = local->rx_pending; + if (skb == NULL) { + pr_debug("No pending SKB\n"); + return; + } + + __net_timestamp(skb); + + nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX); + + nfc_llcp_rx_skb(local, skb); schedule_work(&local->tx_work); kfree_skb(local->rx_pending); diff --git a/net/nfc/llcp/llcp.h b/net/nfc/llcp/llcp.h index 53054d337bf9..6dfde1ed648f 100644 --- a/net/nfc/llcp/llcp.h +++ b/net/nfc/llcp/llcp.h @@ -162,6 +162,7 @@ struct nfc_llcp_ui_cb { #define LLCP_HEADER_SIZE 2 #define LLCP_SEQUENCE_SIZE 1 +#define LLCP_AGF_PDU_HEADER_SIZE 2 /* LLCP versions: 1.1 is 1.0 plus SDP */ #define LLCP_VERSION_10 0x10 -- cgit v1.2.3 From 66cbfa10f3bdbc86222598ac700c352da90e588f Mon Sep 17 00:00:00 2001 From: Thierry Escande Date: Tue, 2 Apr 2013 10:25:14 +0200 Subject: NFC: llcp: Use localy stored remote_miu value if not set at socket level If remote_miu value is not set in the socket (i.e. connection-less socket) the value stored in the local is used. Signed-off-by: Thierry Escande Signed-off-by: Samuel Ortiz --- net/nfc/llcp/commands.c | 12 ++++++++++-- net/nfc/llcp/llcp.h | 1 + net/nfc/llcp/sock.c | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) (limited to 'net/nfc') diff --git a/net/nfc/llcp/commands.c b/net/nfc/llcp/commands.c index 199e8b5514f9..094f7e27e910 100644 --- a/net/nfc/llcp/commands.c +++ b/net/nfc/llcp/commands.c @@ -658,6 +658,7 @@ int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, struct nfc_llcp_local *local; size_t frag_len = 0, remaining_len; u8 *msg_data, *msg_ptr; + u16 remote_miu; pr_debug("Send I frame len %zd\n", len); @@ -695,7 +696,10 @@ int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, msg_ptr = msg_data; do { - frag_len = min_t(size_t, sock->remote_miu, remaining_len); + remote_miu = sock->remote_miu > LLCP_MAX_MIU ? + local->remote_miu : sock->remote_miu; + + frag_len = min_t(size_t, remote_miu, remaining_len); pr_debug("Fragment %zd bytes remaining %zd", frag_len, remaining_len); @@ -734,6 +738,7 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, struct nfc_llcp_local *local; size_t frag_len = 0, remaining_len; u8 *msg_ptr, *msg_data; + u16 remote_miu; int err; pr_debug("Send UI frame len %zd\n", len); @@ -755,7 +760,10 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, msg_ptr = msg_data; do { - frag_len = min_t(size_t, sock->remote_miu, remaining_len); + remote_miu = sock->remote_miu > LLCP_MAX_MIU ? + local->remote_miu : sock->remote_miu; + + frag_len = min_t(size_t, remote_miu, remaining_len); pr_debug("Fragment %zd bytes remaining %zd", frag_len, remaining_len); diff --git a/net/nfc/llcp/llcp.h b/net/nfc/llcp/llcp.h index 6dfde1ed648f..3b2c67eb8efb 100644 --- a/net/nfc/llcp/llcp.h +++ b/net/nfc/llcp/llcp.h @@ -31,6 +31,7 @@ enum llcp_state { #define LLCP_MAX_LTO 0xff #define LLCP_MAX_RW 15 #define LLCP_MAX_MIUX 0x7ff +#define LLCP_MAX_MIU (LLCP_MAX_MIUX + 128) #define LLCP_WKS_NUM_SAP 16 #define LLCP_SDP_NUM_SAP 16 diff --git a/net/nfc/llcp/sock.c b/net/nfc/llcp/sock.c index f3027c21c442..dc94e397d22a 100644 --- a/net/nfc/llcp/sock.c +++ b/net/nfc/llcp/sock.c @@ -934,7 +934,7 @@ struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp) llcp_sock->rw = LLCP_MAX_RW + 1; llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1); llcp_sock->remote_rw = LLCP_DEFAULT_RW; - llcp_sock->remote_miu = LLCP_DEFAULT_MIU; + llcp_sock->remote_miu = LLCP_MAX_MIU + 1; llcp_sock->send_n = llcp_sock->send_ack_n = 0; llcp_sock->recv_n = llcp_sock->recv_ack_n = 0; llcp_sock->remote_ready = 1; -- cgit v1.2.3 From abd18d43302ae0e214d020c842b34e706cc3778e Mon Sep 17 00:00:00 2001 From: Thierry Escande Date: Tue, 2 Apr 2013 10:25:15 +0200 Subject: NFC: llcp: Reset RW, LTO, and MIU remote parameters when link goes down This resets remote parameters in both local and socket llcp structures when the link goes down. That way, nfc_llcp_getsockopt won't return values corresponding to the previous link parameters. Signed-off-by: Thierry Escande Signed-off-by: Samuel Ortiz --- net/nfc/llcp/llcp.c | 11 +++++++++++ net/nfc/llcp/llcp.h | 1 + net/nfc/llcp/sock.c | 3 +-- 3 files changed, 13 insertions(+), 2 deletions(-) (limited to 'net/nfc') diff --git a/net/nfc/llcp/llcp.c b/net/nfc/llcp/llcp.c index 79de8bafd426..83e788e840a0 100644 --- a/net/nfc/llcp/llcp.c +++ b/net/nfc/llcp/llcp.c @@ -47,6 +47,12 @@ void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk) write_unlock(&l->lock); } +void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock) +{ + sock->remote_rw = LLCP_DEFAULT_RW; + sock->remote_miu = LLCP_MAX_MIU + 1; +} + static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock) { struct nfc_llcp_local *local = sock->local; @@ -112,6 +118,7 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen, } if (listen == true) { + nfc_llcp_socket_remote_param_init(llcp_sock); bh_unlock_sock(sk); continue; } @@ -123,6 +130,7 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen, */ if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM && listen == true) { + nfc_llcp_socket_remote_param_init(llcp_sock); bh_unlock_sock(sk); continue; } @@ -1522,6 +1530,9 @@ void nfc_llcp_mac_is_down(struct nfc_dev *dev) if (local == NULL) return; + local->remote_miu = LLCP_DEFAULT_MIU; + local->remote_lto = LLCP_DEFAULT_LTO; + /* Close and purge all existing sockets */ nfc_llcp_socket_release(local, true, 0); } diff --git a/net/nfc/llcp/llcp.h b/net/nfc/llcp/llcp.h index 3b2c67eb8efb..ff8c434f7df8 100644 --- a/net/nfc/llcp/llcp.h +++ b/net/nfc/llcp/llcp.h @@ -212,6 +212,7 @@ struct nfc_llcp_ui_cb { void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *s); void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *s); +void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock); struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev); struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local); int nfc_llcp_local_put(struct nfc_llcp_local *local); diff --git a/net/nfc/llcp/sock.c b/net/nfc/llcp/sock.c index dc94e397d22a..641c535be3d4 100644 --- a/net/nfc/llcp/sock.c +++ b/net/nfc/llcp/sock.c @@ -933,12 +933,11 @@ struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp) llcp_sock->dsap = LLCP_SAP_SDP; llcp_sock->rw = LLCP_MAX_RW + 1; llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1); - llcp_sock->remote_rw = LLCP_DEFAULT_RW; - llcp_sock->remote_miu = LLCP_MAX_MIU + 1; llcp_sock->send_n = llcp_sock->send_ack_n = 0; llcp_sock->recv_n = llcp_sock->recv_ack_n = 0; llcp_sock->remote_ready = 1; llcp_sock->reserved_ssap = LLCP_SAP_MAX; + nfc_llcp_socket_remote_param_init(llcp_sock); skb_queue_head_init(&llcp_sock->tx_queue); skb_queue_head_init(&llcp_sock->tx_pending_queue); INIT_LIST_HEAD(&llcp_sock->accept_queue); -- cgit v1.2.3 From 064f370c5fd982e1264c03f5b704e00f5e41eb36 Mon Sep 17 00:00:00 2001 From: Thierry Escande Date: Tue, 2 Apr 2013 10:25:16 +0200 Subject: NFC: llcp: Add support in getsockopt for RW, LTO, and MIU remote parameters Useful for LLCP validation tests. Signed-off-by: Thierry Escande Signed-off-by: Samuel Ortiz --- include/uapi/linux/nfc.h | 7 +++++-- net/nfc/llcp/sock.c | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) (limited to 'net/nfc') diff --git a/include/uapi/linux/nfc.h b/include/uapi/linux/nfc.h index 7440bc81a04b..7c6f627a717d 100644 --- a/include/uapi/linux/nfc.h +++ b/include/uapi/linux/nfc.h @@ -233,7 +233,10 @@ struct sockaddr_nfc_llcp { #define NFC_LLCP_DIRECTION_TX 0x01 /* socket option names */ -#define NFC_LLCP_RW 0 -#define NFC_LLCP_MIUX 1 +#define NFC_LLCP_RW 0 +#define NFC_LLCP_MIUX 1 +#define NFC_LLCP_REMOTE_MIU 2 +#define NFC_LLCP_REMOTE_LTO 3 +#define NFC_LLCP_REMOTE_RW 4 #endif /*__LINUX_NFC_H */ diff --git a/net/nfc/llcp/sock.c b/net/nfc/llcp/sock.c index 641c535be3d4..fd01ac6e0bf4 100644 --- a/net/nfc/llcp/sock.c +++ b/net/nfc/llcp/sock.c @@ -303,7 +303,7 @@ static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname, struct sock *sk = sock->sk; struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); int len, err = 0; - u16 miux; + u16 miux, remote_miu; u8 rw; pr_debug("%p optname %d\n", sk, optname); @@ -339,6 +339,27 @@ static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname, break; + case NFC_LLCP_REMOTE_MIU: + remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ? + local->remote_miu : llcp_sock->remote_miu; + + if (put_user(remote_miu, (u32 __user *) optval)) + err = -EFAULT; + + break; + + case NFC_LLCP_REMOTE_LTO: + if (put_user(local->remote_lto / 10, (u32 __user *) optval)) + err = -EFAULT; + + break; + + case NFC_LLCP_REMOTE_RW: + if (put_user(llcp_sock->remote_rw, (u32 __user *) optval)) + err = -EFAULT; + + break; + default: err = -ENOPROTOOPT; break; -- cgit v1.2.3 From b436a13debec2b3d2c671d6bebcdb91dabcb0795 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Wed, 3 Apr 2013 16:34:19 +0200 Subject: NFC: llcp: Only keep raw sockets alive when the LLCP local leaves When the MAC goes down, connected and connection less sockets should be notified, but raw sockets should be kept alive. They will get notified only when the physical devices goes away. Signed-off-by: Samuel Ortiz --- net/nfc/llcp/llcp.c | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) (limited to 'net/nfc') diff --git a/net/nfc/llcp/llcp.c b/net/nfc/llcp/llcp.c index 83e788e840a0..99ec39d6e937 100644 --- a/net/nfc/llcp/llcp.c +++ b/net/nfc/llcp/llcp.c @@ -76,7 +76,7 @@ static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock) } } -static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen, +static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, int err) { struct sock *sk; @@ -116,23 +116,6 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen, bh_unlock_sock(accept_sk); } - - if (listen == true) { - nfc_llcp_socket_remote_param_init(llcp_sock); - bh_unlock_sock(sk); - continue; - } - } - - /* - * If we have a connection less socket bound, we keep it alive - * if the device is still present. - */ - if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM && - listen == true) { - nfc_llcp_socket_remote_param_init(llcp_sock); - bh_unlock_sock(sk); - continue; } if (err) @@ -147,11 +130,8 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen, write_unlock(&local->sockets.lock); - /* - * If we want to keep the listening sockets alive, - * we don't touch the RAW ones. - */ - if (listen == true) + /* If we still have a device, we keep the RAW sockets alive */ + if (device == true) return; write_lock(&local->raw_sockets.lock); -- cgit v1.2.3 From c470e319b48bf1aae6185f0c896e65c21c02bad3 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Wed, 3 Apr 2013 16:40:52 +0200 Subject: NFC: llcp: Remove local_cleanup last argument local_cleanup is always called with device set to false as it means the local LLCP is going away. So no need to pass this switch as an argument. Signed-off-by: Samuel Ortiz --- net/nfc/llcp/llcp.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'net/nfc') diff --git a/net/nfc/llcp/llcp.c b/net/nfc/llcp/llcp.c index 99ec39d6e937..3a161c87ef78 100644 --- a/net/nfc/llcp/llcp.c +++ b/net/nfc/llcp/llcp.c @@ -163,9 +163,9 @@ struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local) return local; } -static void local_cleanup(struct nfc_llcp_local *local, bool listen) +static void local_cleanup(struct nfc_llcp_local *local) { - nfc_llcp_socket_release(local, listen, ENXIO); + nfc_llcp_socket_release(local, false, ENXIO); del_timer_sync(&local->link_timer); skb_queue_purge(&local->tx_queue); cancel_work_sync(&local->tx_work); @@ -184,7 +184,7 @@ static void local_release(struct kref *ref) local = container_of(ref, struct nfc_llcp_local, ref); list_del(&local->list); - local_cleanup(local, false); + local_cleanup(local); kfree(local); } @@ -1600,7 +1600,7 @@ void nfc_llcp_unregister_device(struct nfc_dev *dev) return; } - local_cleanup(local, false); + local_cleanup(local); nfc_llcp_local_put(local); } -- cgit v1.2.3 From 6d2cd978e5e14c47fa4f8ab3a136e38aceb4940d Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Wed, 3 Apr 2013 16:44:44 +0200 Subject: NFC: llcp: Terminate connection when receiving a DISC on (0,0) According to the LLCP specs, we must terminate the LLCP link when receiving a DISC with both ssap and dsap set to 0. Signed-off-by: Samuel Ortiz --- net/nfc/llcp/llcp.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'net/nfc') diff --git a/net/nfc/llcp/llcp.c b/net/nfc/llcp/llcp.c index 3a161c87ef78..9e483c8e52f8 100644 --- a/net/nfc/llcp/llcp.c +++ b/net/nfc/llcp/llcp.c @@ -1106,6 +1106,12 @@ static void nfc_llcp_recv_disc(struct nfc_llcp_local *local, dsap = nfc_llcp_dsap(skb); ssap = nfc_llcp_ssap(skb); + if ((dsap == 0) && (ssap == 0)) { + pr_debug("Connection termination"); + nfc_dep_link_down(local->dev); + return; + } + llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); if (llcp_sock == NULL) { nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); -- cgit v1.2.3 From 7757dc8a3e7658abb6e5fc7d825a38b27961d0c8 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Wed, 10 Apr 2013 12:25:30 +0200 Subject: NFC: Prevent polling when device is down Some devices turn radio on whenever they're asked to start a poll. To prevent that from happening, we just don't call into the driver start_poll hook when the NFC device is down. Signed-off-by: Samuel Ortiz --- net/nfc/core.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'net/nfc') diff --git a/net/nfc/core.c b/net/nfc/core.c index 6ceee8e181ca..c571ca9a960c 100644 --- a/net/nfc/core.c +++ b/net/nfc/core.c @@ -143,6 +143,11 @@ int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols) goto error; } + if (!dev->dev_up) { + rc = -ENODEV; + goto error; + } + if (dev->polling) { rc = -EBUSY; goto error; -- cgit v1.2.3 From be055b2f89b5842f41363b5655a33dffb51a8294 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Thu, 11 Apr 2013 11:52:20 +0200 Subject: NFC: RFKILL support All NFC devices will now get proper RFKILL support as long as they provide some dev_up and dev_down hooks. Rfkilling an NFC device will bring it down while it is left to userspace to bring it back up when being rfkill unblocked. This is very similar to what Bluetooth does. Acked-by: Marcel Holtmann Signed-off-by: Samuel Ortiz --- include/net/nfc/nfc.h | 2 ++ net/nfc/core.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) (limited to 'net/nfc') diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h index 87a6417fc934..5eb80bb3cbb2 100644 --- a/include/net/nfc/nfc.h +++ b/include/net/nfc/nfc.h @@ -122,6 +122,8 @@ struct nfc_dev { bool shutting_down; + struct rfkill *rfkill; + struct nfc_ops *ops; }; #define to_nfc_dev(_dev) container_of(_dev, struct nfc_dev, dev) diff --git a/net/nfc/core.c b/net/nfc/core.c index c571ca9a960c..40d2527693da 100644 --- a/net/nfc/core.c +++ b/net/nfc/core.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -58,6 +59,11 @@ int nfc_dev_up(struct nfc_dev *dev) device_lock(&dev->dev); + if (dev->rfkill && rfkill_blocked(dev->rfkill)) { + rc = -ERFKILL; + goto error; + } + if (!device_is_registered(&dev->dev)) { rc = -ENODEV; goto error; @@ -117,6 +123,24 @@ error: return rc; } +static int nfc_rfkill_set_block(void *data, bool blocked) +{ + struct nfc_dev *dev = data; + + pr_debug("%s blocked %d", dev_name(&dev->dev), blocked); + + if (!blocked) + return 0; + + nfc_dev_down(dev); + + return 0; +} + +static const struct rfkill_ops nfc_rfkill_ops = { + .set_block = nfc_rfkill_set_block, +}; + /** * nfc_start_poll - start polling for nfc targets * @@ -840,6 +864,15 @@ int nfc_register_device(struct nfc_dev *dev) pr_debug("The userspace won't be notified that the device %s was added\n", dev_name(&dev->dev)); + dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev, + RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev); + if (dev->rfkill) { + if (rfkill_register(dev->rfkill) < 0) { + rfkill_destroy(dev->rfkill); + dev->rfkill = NULL; + } + } + return 0; } EXPORT_SYMBOL(nfc_register_device); @@ -857,6 +890,11 @@ void nfc_unregister_device(struct nfc_dev *dev) id = dev->idx; + if (dev->rfkill) { + rfkill_unregister(dev->rfkill); + rfkill_destroy(dev->rfkill); + } + if (dev->ops->check_presence) { device_lock(&dev->dev); dev->shutting_down = true; -- cgit v1.2.3