提交 d646960f 编写于 作者: S Samuel Ortiz 提交者: John W. Linville

NFC: Initial LLCP support

This patch is an initial implementation for the NFC Logical Link Control
protocol. It's also known as NFC peer to peer mode.
This is a basic implementation as it lacks SDP (services Discovery
Protocol), frames aggregation support, and frame rejecion parsing.
Follow up patches will implement those missing features.
This code has been tested against a Nexus S phone implementing LLCP 1.0.
Signed-off-by: NSamuel Ortiz <sameo@linux.intel.com>
Signed-off-by: NJohn W. Linville <linville@tuxdriver.com>
上级 361f3cb7
...@@ -139,9 +139,22 @@ struct sockaddr_nfc { ...@@ -139,9 +139,22 @@ struct sockaddr_nfc {
__u32 nfc_protocol; __u32 nfc_protocol;
}; };
#define NFC_LLCP_MAX_SERVICE_NAME 63
struct sockaddr_nfc_llcp {
sa_family_t sa_family;
__u32 dev_idx;
__u32 target_idx;
__u32 nfc_protocol;
__u8 dsap; /* Destination SAP, if known */
__u8 ssap; /* Source SAP to be bound to */
char service_name[NFC_LLCP_MAX_SERVICE_NAME]; /* Service name URI */;
size_t service_name_len;
};
/* NFC socket protocols */ /* NFC socket protocols */
#define NFC_SOCKPROTO_RAW 0 #define NFC_SOCKPROTO_RAW 0
#define NFC_SOCKPROTO_MAX 1 #define NFC_SOCKPROTO_LLCP 1
#define NFC_SOCKPROTO_MAX 2
#define NFC_HEADER_SIZE 1 #define NFC_HEADER_SIZE 1
......
...@@ -14,5 +14,6 @@ menuconfig NFC ...@@ -14,5 +14,6 @@ menuconfig NFC
be called nfc. be called nfc.
source "net/nfc/nci/Kconfig" source "net/nfc/nci/Kconfig"
source "net/nfc/llcp/Kconfig"
source "drivers/nfc/Kconfig" source "drivers/nfc/Kconfig"
...@@ -6,3 +6,4 @@ obj-$(CONFIG_NFC) += nfc.o ...@@ -6,3 +6,4 @@ obj-$(CONFIG_NFC) += nfc.o
obj-$(CONFIG_NFC_NCI) += nci/ obj-$(CONFIG_NFC_NCI) += nci/
nfc-objs := core.o netlink.o af_nfc.o rawsock.o nfc-objs := core.o netlink.o af_nfc.o rawsock.o
nfc-$(CONFIG_NFC_LLCP) += llcp/llcp.o llcp/commands.o llcp/sock.o
...@@ -240,6 +240,7 @@ int nfc_dep_link_down(struct nfc_dev *dev) ...@@ -240,6 +240,7 @@ int nfc_dep_link_down(struct nfc_dev *dev)
rc = dev->ops->dep_link_down(dev); rc = dev->ops->dep_link_down(dev);
if (!rc) { if (!rc) {
dev->dep_link_up = false; dev->dep_link_up = false;
nfc_llcp_mac_is_down(dev);
nfc_genl_dep_link_down_event(dev); nfc_genl_dep_link_down_event(dev);
} }
...@@ -254,6 +255,8 @@ int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx, ...@@ -254,6 +255,8 @@ int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
dev->dep_link_up = true; dev->dep_link_up = true;
dev->dep_rf_mode = rf_mode; dev->dep_rf_mode = rf_mode;
nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
} }
EXPORT_SYMBOL(nfc_dep_link_is_up); EXPORT_SYMBOL(nfc_dep_link_is_up);
...@@ -360,13 +363,13 @@ int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) ...@@ -360,13 +363,13 @@ int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
if (gb_len > NFC_MAX_GT_LEN) if (gb_len > NFC_MAX_GT_LEN)
return -EINVAL; return -EINVAL;
return 0; return nfc_llcp_set_remote_gb(dev, gb, gb_len);
} }
EXPORT_SYMBOL(nfc_set_remote_general_bytes); EXPORT_SYMBOL(nfc_set_remote_general_bytes);
u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, u8 *gt_len) u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, u8 *gt_len)
{ {
return NULL; return nfc_llcp_general_bytes(dev, gt_len);
} }
EXPORT_SYMBOL(nfc_get_local_general_bytes); EXPORT_SYMBOL(nfc_get_local_general_bytes);
...@@ -560,6 +563,10 @@ int nfc_register_device(struct nfc_dev *dev) ...@@ -560,6 +563,10 @@ int nfc_register_device(struct nfc_dev *dev)
if (rc < 0) if (rc < 0)
return rc; return rc;
rc = nfc_llcp_register_device(dev);
if (rc)
pr_err("Could not register llcp device\n");
rc = nfc_genl_device_added(dev); rc = nfc_genl_device_added(dev);
if (rc) if (rc)
pr_debug("The userspace won't be notified that the device %s was added\n", pr_debug("The userspace won't be notified that the device %s was added\n",
...@@ -591,6 +598,8 @@ void nfc_unregister_device(struct nfc_dev *dev) ...@@ -591,6 +598,8 @@ void nfc_unregister_device(struct nfc_dev *dev)
mutex_unlock(&nfc_devlist_mutex); mutex_unlock(&nfc_devlist_mutex);
nfc_llcp_unregister_device(dev);
rc = nfc_genl_device_removed(dev); rc = nfc_genl_device_removed(dev);
if (rc) if (rc)
pr_debug("The userspace won't be notified that the device %s was removed\n", pr_debug("The userspace won't be notified that the device %s was removed\n",
...@@ -620,6 +629,10 @@ static int __init nfc_init(void) ...@@ -620,6 +629,10 @@ static int __init nfc_init(void)
if (rc) if (rc)
goto err_rawsock; goto err_rawsock;
rc = nfc_llcp_init();
if (rc)
goto err_llcp_sock;
rc = af_nfc_init(); rc = af_nfc_init();
if (rc) if (rc)
goto err_af_nfc; goto err_af_nfc;
...@@ -627,6 +640,8 @@ static int __init nfc_init(void) ...@@ -627,6 +640,8 @@ static int __init nfc_init(void)
return 0; return 0;
err_af_nfc: err_af_nfc:
nfc_llcp_exit();
err_llcp_sock:
rawsock_exit(); rawsock_exit();
err_rawsock: err_rawsock:
nfc_genl_exit(); nfc_genl_exit();
...@@ -638,6 +653,7 @@ static int __init nfc_init(void) ...@@ -638,6 +653,7 @@ static int __init nfc_init(void)
static void __exit nfc_exit(void) static void __exit nfc_exit(void)
{ {
af_nfc_exit(); af_nfc_exit();
nfc_llcp_exit();
rawsock_exit(); rawsock_exit();
nfc_genl_exit(); nfc_genl_exit();
class_unregister(&nfc_class); class_unregister(&nfc_class);
......
config NFC_LLCP
depends on NFC && EXPERIMENTAL
bool "NFC LLCP support (EXPERIMENTAL)"
default n
help
Say Y here if you want to build support for a kernel NFC LLCP
implementation.
\ No newline at end of file
/*
* Copyright (C) 2011 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define pr_fmt(fmt) "llcp: %s: " fmt, __func__
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/nfc.h>
#include <net/nfc/nfc.h>
#include "../nfc.h"
#include "llcp.h"
static u8 llcp_tlv_length[LLCP_TLV_MAX] = {
0,
1, /* VERSION */
2, /* MIUX */
2, /* WKS */
1, /* LTO */
1, /* RW */
0, /* SN */
1, /* OPT */
0, /* SDREQ */
2, /* SDRES */
};
static u8 llcp_tlv8(u8 *tlv, u8 type)
{
if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
return 0;
return tlv[2];
}
static u8 llcp_tlv16(u8 *tlv, u8 type)
{
if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
return 0;
return be16_to_cpu(*((__be16 *)(tlv + 2)));
}
static u8 llcp_tlv_version(u8 *tlv)
{
return llcp_tlv8(tlv, LLCP_TLV_VERSION);
}
static u16 llcp_tlv_miux(u8 *tlv)
{
return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7f;
}
static u16 llcp_tlv_wks(u8 *tlv)
{
return llcp_tlv16(tlv, LLCP_TLV_WKS);
}
static u16 llcp_tlv_lto(u8 *tlv)
{
return llcp_tlv8(tlv, LLCP_TLV_LTO);
}
static u8 llcp_tlv_opt(u8 *tlv)
{
return llcp_tlv8(tlv, LLCP_TLV_OPT);
}
static u8 llcp_tlv_rw(u8 *tlv)
{
return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf;
}
u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length)
{
u8 *tlv, length;
pr_debug("type %d\n", type);
if (type >= LLCP_TLV_MAX)
return NULL;
length = llcp_tlv_length[type];
if (length == 0 && value_length == 0)
return NULL;
else
length = value_length;
*tlv_length = 2 + length;
tlv = kzalloc(2 + length, GFP_KERNEL);
if (tlv == NULL)
return tlv;
tlv[0] = type;
tlv[1] = length;
memcpy(tlv + 2, value, length);
return tlv;
}
int nfc_llcp_parse_tlv(struct nfc_llcp_local *local,
u8 *tlv_array, u16 tlv_array_len)
{
u8 *tlv = tlv_array, type, length, offset = 0;
pr_debug("TLV array length %d\n", tlv_array_len);
if (local == NULL)
return -ENODEV;
while (offset < tlv_array_len) {
type = tlv[0];
length = tlv[1];
pr_debug("type 0x%x length %d\n", type, length);
switch (type) {
case LLCP_TLV_VERSION:
local->remote_version = llcp_tlv_version(tlv);
break;
case LLCP_TLV_MIUX:
local->remote_miu = llcp_tlv_miux(tlv) + 128;
break;
case LLCP_TLV_WKS:
local->remote_wks = llcp_tlv_wks(tlv);
break;
case LLCP_TLV_LTO:
local->remote_lto = llcp_tlv_lto(tlv) * 10;
break;
case LLCP_TLV_OPT:
local->remote_opt = llcp_tlv_opt(tlv);
break;
case LLCP_TLV_RW:
local->remote_rw = llcp_tlv_rw(tlv);
break;
default:
pr_err("Invalid gt tlv value 0x%x\n", type);
break;
}
offset += length + 2;
tlv += length + 2;
}
pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x rw %d\n",
local->remote_version, local->remote_miu,
local->remote_lto, local->remote_opt,
local->remote_wks, local->remote_rw);
return 0;
}
static struct sk_buff *llcp_add_header(struct sk_buff *pdu,
u8 dsap, u8 ssap, u8 ptype)
{
u8 header[2];
pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
header[0] = (u8)((dsap << 2) | (ptype >> 2));
header[1] = (u8)((ptype << 6) | ssap);
pr_debug("header 0x%x 0x%x\n", header[0], header[1]);
memcpy(skb_put(pdu, LLCP_HEADER_SIZE), header, LLCP_HEADER_SIZE);
return pdu;
}
static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv, u8 tlv_length)
{
/* XXX Add an skb length check */
if (tlv == NULL)
return NULL;
memcpy(skb_put(pdu, tlv_length), tlv, tlv_length);
return pdu;
}
static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock,
u8 cmd, u16 size)
{
struct sk_buff *skb;
int err;
if (sock->ssap == 0)
return NULL;
skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
size + LLCP_HEADER_SIZE, &err);
if (skb == NULL) {
pr_err("Could not allocate PDU\n");
return NULL;
}
skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd);
return skb;
}
int nfc_llcp_disconnect(struct nfc_llcp_sock *sock)
{
struct sk_buff *skb;
struct nfc_dev *dev;
struct nfc_llcp_local *local;
u16 size = 0;
pr_debug("Sending DISC\n");
local = sock->local;
if (local == NULL)
return -ENODEV;
dev = sock->dev;
if (dev == NULL)
return -ENODEV;
size += LLCP_HEADER_SIZE;
size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
skb = alloc_skb(size, GFP_ATOMIC);
if (skb == NULL)
return -ENOMEM;
skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
skb = llcp_add_header(skb, sock->ssap, sock->dsap, LLCP_PDU_DISC);
skb_queue_tail(&local->tx_queue, skb);
return 0;
}
int nfc_llcp_send_symm(struct nfc_dev *dev)
{
struct sk_buff *skb;
struct nfc_llcp_local *local;
u16 size = 0;
pr_debug("Sending SYMM\n");
local = nfc_llcp_find_local(dev);
if (local == NULL)
return -ENODEV;
size += LLCP_HEADER_SIZE;
size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
skb = alloc_skb(size, GFP_KERNEL);
if (skb == NULL)
return -ENOMEM;
skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM);
return nfc_data_exchange(dev, local->target_idx, skb,
nfc_llcp_recv, local);
}
int nfc_llcp_send_connect(struct nfc_llcp_sock *sock)
{
struct nfc_llcp_local *local;
struct sk_buff *skb;
u8 *service_name_tlv = NULL, service_name_tlv_length;
int err;
u16 size = 0;
pr_debug("Sending CONNECT\n");
local = sock->local;
if (local == NULL)
return -ENODEV;
if (sock->service_name != NULL) {
service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN,
sock->service_name,
sock->service_name_len,
&service_name_tlv_length);
size += service_name_tlv_length;
}
pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len);
skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size);
if (skb == NULL) {
err = -ENOMEM;
goto error_tlv;
}
if (service_name_tlv != NULL)
skb = llcp_add_tlv(skb, service_name_tlv,
service_name_tlv_length);
skb_queue_tail(&local->tx_queue, skb);
return 0;
error_tlv:
pr_err("error %d\n", err);
kfree(service_name_tlv);
return err;
}
int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
{
struct nfc_llcp_local *local;
struct sk_buff *skb;
pr_debug("Sending CC\n");
local = sock->local;
if (local == NULL)
return -ENODEV;
skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, 0);
if (skb == NULL)
return -ENOMEM;
skb_queue_tail(&local->tx_queue, skb);
return 0;
}
int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason)
{
struct sk_buff *skb;
struct nfc_dev *dev;
u16 size = 1; /* Reason code */
pr_debug("Sending DM reason 0x%x\n", reason);
if (local == NULL)
return -ENODEV;
dev = local->dev;
if (dev == NULL)
return -ENODEV;
size += LLCP_HEADER_SIZE;
size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
skb = alloc_skb(size, GFP_KERNEL);
if (skb == NULL)
return -ENOMEM;
skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
skb = llcp_add_header(skb, ssap, dsap, LLCP_PDU_DM);
memcpy(skb_put(skb, 1), &reason, 1);
skb_queue_head(&local->tx_queue, skb);
return 0;
}
int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock)
{
struct sk_buff *skb;
struct nfc_llcp_local *local;
pr_debug("Send DISC\n");
local = sock->local;
if (local == NULL)
return -ENODEV;
skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
if (skb == NULL)
return -ENOMEM;
skb_queue_head(&local->tx_queue, skb);
return 0;
}
此差异已折叠。
/*
* Copyright (C) 2011 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
enum llcp_state {
LLCP_CONNECTED = 1, /* wait_for_packet() wants that */
LLCP_CLOSED,
LLCP_BOUND,
LLCP_LISTEN,
};
#define LLCP_DEFAULT_LTO 100
#define LLCP_DEFAULT_RW 1
#define LLCP_DEFAULT_MIU 128
#define LLCP_WKS_NUM_SAP 16
#define LLCP_SDP_NUM_SAP 16
#define LLCP_LOCAL_NUM_SAP 32
#define LLCP_LOCAL_SAP_OFFSET (LLCP_WKS_NUM_SAP + LLCP_SDP_NUM_SAP)
#define LLCP_MAX_SAP (LLCP_WKS_NUM_SAP + LLCP_SDP_NUM_SAP + LLCP_LOCAL_NUM_SAP)
struct nfc_llcp_sock;
struct nfc_llcp_local {
struct list_head list;
struct nfc_dev *dev;
struct mutex sdp_lock;
struct mutex socket_lock;
struct timer_list link_timer;
struct sk_buff_head tx_queue;
struct workqueue_struct *tx_wq;
struct work_struct tx_work;
struct workqueue_struct *rx_wq;
struct work_struct rx_work;
struct sk_buff *rx_pending;
struct workqueue_struct *timeout_wq;
struct work_struct timeout_work;
u32 target_idx;
u8 rf_mode;
u8 comm_mode;
unsigned long local_wks; /* Well known services */
unsigned long local_sdp; /* Local services */
unsigned long local_sap; /* Local SAPs, not available for discovery */
/* local */
u8 gb[NFC_MAX_GT_LEN];
u8 gb_len;
/* remote */
u8 remote_gb[NFC_MAX_GT_LEN];
u8 remote_gb_len;
u8 remote_version;
u16 remote_miu;
u16 remote_lto;
u8 remote_opt;
u16 remote_wks;
u8 remote_rw;
/* sockets array */
struct nfc_llcp_sock *sockets[LLCP_MAX_SAP];
};
struct nfc_llcp_sock {
struct sock sk;
struct list_head list;
struct nfc_dev *dev;
struct nfc_llcp_local *local;
u32 target_idx;
u32 nfc_protocol;
u8 ssap;
u8 dsap;
char *service_name;
size_t service_name_len;
/* Link variables */
u8 send_n;
u8 send_ack_n;
u8 recv_n;
u8 recv_ack_n;
/* Is the remote peer ready to receive */
u8 remote_ready;
struct sk_buff_head tx_queue;
struct sk_buff_head tx_pending_queue;
struct sk_buff_head tx_backlog_queue;
struct list_head accept_queue;
struct sock *parent;
};
#define nfc_llcp_sock(sk) ((struct nfc_llcp_sock *) (sk))
#define nfc_llcp_dev(sk) (nfc_llcp_sock((sk))->dev)
#define LLCP_HEADER_SIZE 2
#define LLCP_SEQUENCE_SIZE 1
/* LLCP versions: 1.1 is 1.0 plus SDP */
#define LLCP_VERSION_10 0x10
#define LLCP_VERSION_11 0x11
/* LLCP PDU types */
#define LLCP_PDU_SYMM 0x0
#define LLCP_PDU_PAX 0x1
#define LLCP_PDU_AGF 0x2
#define LLCP_PDU_UI 0x3
#define LLCP_PDU_CONNECT 0x4
#define LLCP_PDU_DISC 0x5
#define LLCP_PDU_CC 0x6
#define LLCP_PDU_DM 0x7
#define LLCP_PDU_FRMR 0x8
#define LLCP_PDU_SNL 0x9
#define LLCP_PDU_I 0xc
#define LLCP_PDU_RR 0xd
#define LLCP_PDU_RNR 0xe
/* Parameters TLV types */
#define LLCP_TLV_VERSION 0x1
#define LLCP_TLV_MIUX 0x2
#define LLCP_TLV_WKS 0x3
#define LLCP_TLV_LTO 0x4
#define LLCP_TLV_RW 0x5
#define LLCP_TLV_SN 0x6
#define LLCP_TLV_OPT 0x7
#define LLCP_TLV_SDREQ 0x8
#define LLCP_TLV_SDRES 0x9
#define LLCP_TLV_MAX 0xa
/* Well known LLCP SAP */
#define LLCP_SAP_SDP 0x1
#define LLCP_SAP_IP 0x2
#define LLCP_SAP_OBEX 0x3
#define LLCP_SAP_SNEP 0x4
#define LLCP_SAP_MAX 0xff
/* Disconnection reason code */
#define LLCP_DM_DISC 0x00
#define LLCP_DM_NOCONN 0x01
#define LLCP_DM_NOBOUND 0x02
#define LLCP_DM_REJ 0x03
struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev);
u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
struct nfc_llcp_sock *sock);
u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local);
void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap);
/* Sock API */
struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp);
void nfc_llcp_sock_free(struct nfc_llcp_sock *sock);
void nfc_llcp_accept_unlink(struct sock *sk);
void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk);
struct sock *nfc_llcp_accept_dequeue(struct sock *sk, struct socket *newsock);
/* TLV API */
int nfc_llcp_parse_tlv(struct nfc_llcp_local *local,
u8 *tlv_array, u16 tlv_array_len);
/* Commands API */
void nfc_llcp_recv(void *data, struct sk_buff *skb, int err);
u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length);
void nfc_llcp_recv(void *data, struct sk_buff *skb, int err);
int nfc_llcp_disconnect(struct nfc_llcp_sock *sock);
int nfc_llcp_send_symm(struct nfc_dev *dev);
int nfc_llcp_send_connect(struct nfc_llcp_sock *sock);
int nfc_llcp_send_cc(struct nfc_llcp_sock *sock);
int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason);
int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock);
/* Socket API */
int __init nfc_llcp_sock_init(void);
void nfc_llcp_sock_exit(void);
/*
* Copyright (C) 2011 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define pr_fmt(fmt) "llcp: %s: " fmt, __func__
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/nfc.h>
#include "../nfc.h"
#include "llcp.h"
static struct proto llcp_sock_proto = {
.name = "NFC_LLCP",
.owner = THIS_MODULE,
.obj_size = sizeof(struct nfc_llcp_sock),
};
static int llcp_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
{
struct sock *sk = sock->sk;
struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
struct nfc_llcp_local *local;
struct nfc_dev *dev;
struct sockaddr_nfc_llcp llcp_addr;
int len, ret = 0;
pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
if (!addr || addr->sa_family != AF_NFC)
return -EINVAL;
memset(&llcp_addr, 0, sizeof(llcp_addr));
len = min_t(unsigned int, sizeof(llcp_addr), alen);
memcpy(&llcp_addr, addr, len);
/* This is going to be a listening socket, dsap must be 0 */
if (llcp_addr.dsap != 0)
return -EINVAL;
lock_sock(sk);
if (sk->sk_state != LLCP_CLOSED) {
ret = -EBADFD;
goto error;
}
dev = nfc_get_device(llcp_addr.dev_idx);
if (dev == NULL) {
ret = -ENODEV;
goto error;
}
local = nfc_llcp_find_local(dev);
if (local == NULL) {
ret = -ENODEV;
goto put_dev;
}
llcp_sock->dev = dev;
llcp_sock->local = local;
llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
llcp_sock->service_name_len = min_t(unsigned int,
llcp_addr.service_name_len, NFC_LLCP_MAX_SERVICE_NAME);
llcp_sock->service_name = kmemdup(llcp_addr.service_name,
llcp_sock->service_name_len, GFP_KERNEL);
llcp_sock->ssap = nfc_llcp_get_sdp_ssap(local, llcp_sock);
if (llcp_sock->ssap == LLCP_MAX_SAP)
goto put_dev;
local->sockets[llcp_sock->ssap] = llcp_sock;
pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap);
sk->sk_state = LLCP_BOUND;
put_dev:
nfc_put_device(dev);
error:
release_sock(sk);
return ret;
}
static int llcp_sock_listen(struct socket *sock, int backlog)
{
struct sock *sk = sock->sk;
int ret = 0;
pr_debug("sk %p backlog %d\n", sk, backlog);
lock_sock(sk);
if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
|| sk->sk_state != LLCP_BOUND) {
ret = -EBADFD;
goto error;
}
sk->sk_max_ack_backlog = backlog;
sk->sk_ack_backlog = 0;
pr_debug("Socket listening\n");
sk->sk_state = LLCP_LISTEN;
error:
release_sock(sk);
return ret;
}
void nfc_llcp_accept_unlink(struct sock *sk)
{
struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
pr_debug("state %d\n", sk->sk_state);
list_del_init(&llcp_sock->accept_queue);
sk_acceptq_removed(llcp_sock->parent);
llcp_sock->parent = NULL;
sock_put(sk);
}
void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk)
{
struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent);
/* Lock will be free from unlink */
sock_hold(sk);
list_add_tail(&llcp_sock->accept_queue,
&llcp_sock_parent->accept_queue);
llcp_sock->parent = parent;
sk_acceptq_added(parent);
}
struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
struct socket *newsock)
{
struct nfc_llcp_sock *lsk, *n, *llcp_parent;
struct sock *sk;
llcp_parent = nfc_llcp_sock(parent);
list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue,
accept_queue) {
sk = &lsk->sk;
lock_sock(sk);
if (sk->sk_state == LLCP_CLOSED) {
release_sock(sk);
nfc_llcp_accept_unlink(sk);
continue;
}
if (sk->sk_state == LLCP_CONNECTED || !newsock) {
nfc_llcp_accept_unlink(sk);
if (newsock)
sock_graft(sk, newsock);
release_sock(sk);
pr_debug("Returning sk state %d\n", sk->sk_state);
return sk;
}
release_sock(sk);
}
return NULL;
}
static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
int flags)
{
DECLARE_WAITQUEUE(wait, current);
struct sock *sk = sock->sk, *new_sk;
long timeo;
int ret = 0;
pr_debug("parent %p\n", sk);
lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
if (sk->sk_state != LLCP_LISTEN) {
ret = -EBADFD;
goto error;
}
timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
/* Wait for an incoming connection. */
add_wait_queue_exclusive(sk_sleep(sk), &wait);
while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
set_current_state(TASK_INTERRUPTIBLE);
if (!timeo) {
ret = -EAGAIN;
break;
}
if (signal_pending(current)) {
ret = sock_intr_errno(timeo);
break;
}
release_sock(sk);
timeo = schedule_timeout(timeo);
lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
}
__set_current_state(TASK_RUNNING);
remove_wait_queue(sk_sleep(sk), &wait);
if (ret)
goto error;
newsock->state = SS_CONNECTED;
pr_debug("new socket %p\n", new_sk);
error:
release_sock(sk);
return ret;
}
static int llcp_sock_getname(struct socket *sock, struct sockaddr *addr,
int *len, int peer)
{
struct sockaddr_nfc_llcp *llcp_addr = (struct sockaddr_nfc_llcp *) addr;
struct sock *sk = sock->sk;
struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
pr_debug("%p\n", sk);
addr->sa_family = AF_NFC;
*len = sizeof(struct sockaddr_nfc_llcp);
llcp_addr->dev_idx = llcp_sock->dev->idx;
llcp_addr->dsap = llcp_sock->dsap;
llcp_addr->ssap = llcp_sock->ssap;
llcp_addr->service_name_len = llcp_sock->service_name_len;
memcpy(llcp_addr->service_name, llcp_sock->service_name,
llcp_addr->service_name_len);
return 0;
}
static inline unsigned int llcp_accept_poll(struct sock *parent)
{
struct nfc_llcp_sock *llcp_sock, *n, *parent_sock;
struct sock *sk;
parent_sock = nfc_llcp_sock(parent);
list_for_each_entry_safe(llcp_sock, n, &parent_sock->accept_queue,
accept_queue) {
sk = &llcp_sock->sk;
if (sk->sk_state == LLCP_CONNECTED)
return POLLIN | POLLRDNORM;
}
return 0;
}
static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
poll_table *wait)
{
struct sock *sk = sock->sk;
unsigned int mask = 0;
pr_debug("%p\n", sk);
sock_poll_wait(file, sk_sleep(sk), wait);
if (sk->sk_state == LLCP_LISTEN)
return llcp_accept_poll(sk);
if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
mask |= POLLERR;
if (!skb_queue_empty(&sk->sk_receive_queue))
mask |= POLLIN;
if (sk->sk_state == LLCP_CLOSED)
mask |= POLLHUP;
return mask;
}
static int llcp_sock_release(struct socket *sock)
{
struct sock *sk = sock->sk;
struct nfc_llcp_local *local;
struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
if (!sk)
return 0;
pr_debug("%p\n", sk);
local = llcp_sock->local;
if (local == NULL)
return -ENODEV;
mutex_lock(&local->socket_lock);
if (llcp_sock == local->sockets[llcp_sock->ssap]) {
local->sockets[llcp_sock->ssap] = NULL;
} else {
struct nfc_llcp_sock *parent, *s, *n;
parent = local->sockets[llcp_sock->ssap];
list_for_each_entry_safe(s, n, &parent->list, list)
if (llcp_sock == s) {
list_del(&s->list);
break;
}
}
mutex_unlock(&local->socket_lock);
lock_sock(sk);
/* Send a DISC */
if (sk->sk_state == LLCP_CONNECTED)
nfc_llcp_disconnect(llcp_sock);
if (sk->sk_state == LLCP_LISTEN) {
struct nfc_llcp_sock *lsk, *n;
struct sock *accept_sk;
list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
accept_queue) {
accept_sk = &lsk->sk;
lock_sock(accept_sk);
nfc_llcp_disconnect(lsk);
nfc_llcp_accept_unlink(accept_sk);
release_sock(accept_sk);
sock_set_flag(sk, SOCK_DEAD);
sock_orphan(accept_sk);
sock_put(accept_sk);
}
}
/* Freeing the SAP */
if ((sk->sk_state == LLCP_CONNECTED
&& llcp_sock->ssap > LLCP_LOCAL_SAP_OFFSET) ||
sk->sk_state == LLCP_BOUND ||
sk->sk_state == LLCP_LISTEN)
nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
sock_set_flag(sk, SOCK_DEAD);
release_sock(sk);
sock_orphan(sk);
sock_put(sk);
return 0;
}
static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
int len, int flags)
{
struct sock *sk = sock->sk;
struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
struct nfc_dev *dev;
struct nfc_llcp_local *local;
int ret = 0;
pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
if (!addr || len < sizeof(struct sockaddr_nfc) ||
addr->sa_family != AF_NFC) {
pr_err("Invalid socket\n");
return -EINVAL;
}
if (addr->service_name_len == 0 && addr->dsap == 0) {
pr_err("Missing service name or dsap\n");
return -EINVAL;
}
pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
addr->target_idx, addr->nfc_protocol);
lock_sock(sk);
if (sk->sk_state == LLCP_CONNECTED) {
ret = -EISCONN;
goto error;
}
dev = nfc_get_device(addr->dev_idx);
if (dev == NULL) {
ret = -ENODEV;
goto error;
}
local = nfc_llcp_find_local(dev);
if (local == NULL) {
ret = -ENODEV;
goto put_dev;
}
device_lock(&dev->dev);
if (dev->dep_link_up == false) {
ret = -ENOLINK;
device_unlock(&dev->dev);
goto put_dev;
}
device_unlock(&dev->dev);
if (local->rf_mode == NFC_RF_INITIATOR &&
addr->target_idx != local->target_idx) {
ret = -ENOLINK;
goto put_dev;
}
llcp_sock->dev = dev;
llcp_sock->local = local;
llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
if (llcp_sock->ssap == LLCP_SAP_MAX) {
ret = -ENOMEM;
goto put_dev;
}
if (addr->service_name_len == 0)
llcp_sock->dsap = addr->dsap;
else
llcp_sock->dsap = LLCP_SAP_SDP;
llcp_sock->nfc_protocol = addr->nfc_protocol;
llcp_sock->service_name_len = min_t(unsigned int,
addr->service_name_len, NFC_LLCP_MAX_SERVICE_NAME);
llcp_sock->service_name = kmemdup(addr->service_name,
llcp_sock->service_name_len, GFP_KERNEL);
local->sockets[llcp_sock->ssap] = llcp_sock;
ret = nfc_llcp_send_connect(llcp_sock);
if (ret)
goto put_dev;
sk->sk_state = LLCP_CONNECTED;
release_sock(sk);
return 0;
put_dev:
nfc_put_device(dev);
error:
release_sock(sk);
return ret;
}
static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
struct msghdr *msg, size_t len, int flags)
{
int noblock = flags & MSG_DONTWAIT;
struct sock *sk = sock->sk;
unsigned int copied, rlen;
struct sk_buff *skb, *cskb;
int err = 0;
pr_debug("%p %zu\n", sk, len);
lock_sock(sk);
if (sk->sk_state == LLCP_CLOSED &&
skb_queue_empty(&sk->sk_receive_queue)) {
release_sock(sk);
return 0;
}
release_sock(sk);
if (flags & (MSG_OOB))
return -EOPNOTSUPP;
skb = skb_recv_datagram(sk, flags, noblock, &err);
if (!skb) {
pr_err("Recv datagram failed state %d %d %d",
sk->sk_state, err, sock_error(sk));
if (sk->sk_shutdown & RCV_SHUTDOWN)
return 0;
return err;
}
rlen = skb->len; /* real length of skb */
copied = min_t(unsigned int, rlen, len);
cskb = skb;
if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
if (!(flags & MSG_PEEK))
skb_queue_head(&sk->sk_receive_queue, skb);
return -EFAULT;
}
/* Mark read part of skb as used */
if (!(flags & MSG_PEEK)) {
/* SOCK_STREAM: re-queue skb if it contains unreceived data */
if (sk->sk_type == SOCK_STREAM) {
skb_pull(skb, copied);
if (skb->len) {
skb_queue_head(&sk->sk_receive_queue, skb);
goto done;
}
}
kfree_skb(skb);
}
/* XXX Queue backlogged skbs */
done:
/* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
copied = rlen;
return copied;
}
static const struct proto_ops llcp_sock_ops = {
.family = PF_NFC,
.owner = THIS_MODULE,
.bind = llcp_sock_bind,
.connect = llcp_sock_connect,
.release = llcp_sock_release,
.socketpair = sock_no_socketpair,
.accept = llcp_sock_accept,
.getname = llcp_sock_getname,
.poll = llcp_sock_poll,
.ioctl = sock_no_ioctl,
.listen = llcp_sock_listen,
.shutdown = sock_no_shutdown,
.setsockopt = sock_no_setsockopt,
.getsockopt = sock_no_getsockopt,
.sendmsg = sock_no_sendmsg,
.recvmsg = llcp_sock_recvmsg,
.mmap = sock_no_mmap,
};
static void llcp_sock_destruct(struct sock *sk)
{
struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
pr_debug("%p\n", sk);
if (sk->sk_state == LLCP_CONNECTED)
nfc_put_device(llcp_sock->dev);
skb_queue_purge(&sk->sk_receive_queue);
nfc_llcp_sock_free(llcp_sock);
if (!sock_flag(sk, SOCK_DEAD)) {
pr_err("Freeing alive NFC LLCP socket %p\n", sk);
return;
}
}
struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp)
{
struct sock *sk;
struct nfc_llcp_sock *llcp_sock;
sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto);
if (!sk)
return NULL;
llcp_sock = nfc_llcp_sock(sk);
sock_init_data(sock, sk);
sk->sk_state = LLCP_CLOSED;
sk->sk_protocol = NFC_SOCKPROTO_LLCP;
sk->sk_type = type;
sk->sk_destruct = llcp_sock_destruct;
llcp_sock->ssap = 0;
llcp_sock->dsap = LLCP_SAP_SDP;
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;
skb_queue_head_init(&llcp_sock->tx_queue);
skb_queue_head_init(&llcp_sock->tx_pending_queue);
skb_queue_head_init(&llcp_sock->tx_backlog_queue);
INIT_LIST_HEAD(&llcp_sock->list);
INIT_LIST_HEAD(&llcp_sock->accept_queue);
if (sock != NULL)
sock->state = SS_UNCONNECTED;
return sk;
}
void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
{
kfree(sock->service_name);
skb_queue_purge(&sock->tx_queue);
skb_queue_purge(&sock->tx_pending_queue);
skb_queue_purge(&sock->tx_backlog_queue);
list_del_init(&sock->accept_queue);
sock->parent = NULL;
}
static int llcp_sock_create(struct net *net, struct socket *sock,
const struct nfc_protocol *nfc_proto)
{
struct sock *sk;
pr_debug("%p\n", sock);
if (sock->type != SOCK_STREAM && sock->type != SOCK_DGRAM)
return -ESOCKTNOSUPPORT;
sock->ops = &llcp_sock_ops;
sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC);
if (sk == NULL)
return -ENOMEM;
return 0;
}
static const struct nfc_protocol llcp_nfc_proto = {
.id = NFC_SOCKPROTO_LLCP,
.proto = &llcp_sock_proto,
.owner = THIS_MODULE,
.create = llcp_sock_create
};
int __init nfc_llcp_sock_init(void)
{
return nfc_proto_register(&llcp_nfc_proto);
}
void nfc_llcp_sock_exit(void)
{
nfc_proto_unregister(&llcp_nfc_proto);
}
...@@ -46,6 +46,60 @@ struct nfc_rawsock { ...@@ -46,6 +46,60 @@ struct nfc_rawsock {
#define to_rawsock_sk(_tx_work) \ #define to_rawsock_sk(_tx_work) \
((struct sock *) container_of(_tx_work, struct nfc_rawsock, tx_work)) ((struct sock *) container_of(_tx_work, struct nfc_rawsock, tx_work))
#ifdef CONFIG_NFC_LLCP
void nfc_llcp_mac_is_down(struct nfc_dev *dev);
void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
u8 comm_mode, u8 rf_mode);
int nfc_llcp_register_device(struct nfc_dev *dev);
void nfc_llcp_unregister_device(struct nfc_dev *dev);
int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len);
u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, u8 *general_bytes_len);
int __init nfc_llcp_init(void);
void nfc_llcp_exit(void);
#else
void nfc_llcp_mac_is_down(struct nfc_dev *dev)
{
}
void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
u8 comm_mode, u8 rf_mode)
{
}
static inline int nfc_llcp_register_device(struct nfc_dev *dev)
{
return 0;
}
static inline void nfc_llcp_unregister_device(struct nfc_dev *dev)
{
}
static inline int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
{
return 0;
}
static inline u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, u8 *gb_len)
{
*gb_len = 0;
return NULL;
}
static inline int nfc_llcp_init(void)
{
return 0;
}
static inline void nfc_llcp_exit(void)
{
}
#endif
int __init rawsock_init(void); int __init rawsock_init(void);
void rawsock_exit(void); void rawsock_exit(void);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册