Commit b2e44aac authored by Dmitry Vyukov's avatar Dmitry Vyukov Committed by Paolo Abeni

NFC: nci: Allow to create multiple virtual nci devices

The current virtual nci driver is great for testing and fuzzing.
But it allows to create at most one "global" device which does not allow
to run parallel tests and harms fuzzing isolation and reproducibility.
Restructure the driver to allow creation of multiple independent devices.
This should be backwards compatible for existing tests.
Signed-off-by: default avatarDmitry Vyukov <dvyukov@google.com>
Reviewed-by: default avatarBongsu Jeon <bongsu.jeon@samsung.com>
Cc: Bongsu Jeon <bongsu.jeon@samsung.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: netdev@vger.kernel.org
Link: https://lore.kernel.org/r/20221115100017.787929-1-dvyukov@google.comSigned-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parent 710cfc6a
...@@ -13,12 +13,6 @@ ...@@ -13,12 +13,6 @@
#include <linux/wait.h> #include <linux/wait.h>
#include <net/nfc/nci_core.h> #include <net/nfc/nci_core.h>
enum virtual_ncidev_mode {
virtual_ncidev_enabled,
virtual_ncidev_disabled,
virtual_ncidev_disabling,
};
#define IOCTL_GET_NCIDEV_IDX 0 #define IOCTL_GET_NCIDEV_IDX 0
#define VIRTUAL_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ #define VIRTUAL_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
NFC_PROTO_MIFARE_MASK | \ NFC_PROTO_MIFARE_MASK | \
...@@ -27,12 +21,12 @@ enum virtual_ncidev_mode { ...@@ -27,12 +21,12 @@ enum virtual_ncidev_mode {
NFC_PROTO_ISO14443_B_MASK | \ NFC_PROTO_ISO14443_B_MASK | \
NFC_PROTO_ISO15693_MASK) NFC_PROTO_ISO15693_MASK)
static enum virtual_ncidev_mode state; struct virtual_nci_dev {
static DECLARE_WAIT_QUEUE_HEAD(wq); struct nci_dev *ndev;
static struct miscdevice miscdev; struct mutex mtx;
static struct sk_buff *send_buff; struct sk_buff *send_buff;
static struct nci_dev *ndev; struct wait_queue_head wq;
static DEFINE_MUTEX(nci_mutex); };
static int virtual_nci_open(struct nci_dev *ndev) static int virtual_nci_open(struct nci_dev *ndev)
{ {
...@@ -41,31 +35,34 @@ static int virtual_nci_open(struct nci_dev *ndev) ...@@ -41,31 +35,34 @@ static int virtual_nci_open(struct nci_dev *ndev)
static int virtual_nci_close(struct nci_dev *ndev) static int virtual_nci_close(struct nci_dev *ndev)
{ {
mutex_lock(&nci_mutex); struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
kfree_skb(send_buff);
send_buff = NULL; mutex_lock(&vdev->mtx);
mutex_unlock(&nci_mutex); kfree_skb(vdev->send_buff);
vdev->send_buff = NULL;
mutex_unlock(&vdev->mtx);
return 0; return 0;
} }
static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
{ {
mutex_lock(&nci_mutex); struct virtual_nci_dev *vdev = nci_get_drvdata(ndev);
if (state != virtual_ncidev_enabled) {
mutex_unlock(&nci_mutex); mutex_lock(&vdev->mtx);
if (vdev->send_buff) {
mutex_unlock(&vdev->mtx);
kfree_skb(skb); kfree_skb(skb);
return 0; return -1;
} }
vdev->send_buff = skb_copy(skb, GFP_KERNEL);
if (send_buff) { if (!vdev->send_buff) {
mutex_unlock(&nci_mutex); mutex_unlock(&vdev->mtx);
kfree_skb(skb); kfree_skb(skb);
return -1; return -1;
} }
send_buff = skb_copy(skb, GFP_KERNEL); mutex_unlock(&vdev->mtx);
mutex_unlock(&nci_mutex); wake_up_interruptible(&vdev->wq);
wake_up_interruptible(&wq);
consume_skb(skb); consume_skb(skb);
return 0; return 0;
...@@ -80,29 +77,30 @@ static const struct nci_ops virtual_nci_ops = { ...@@ -80,29 +77,30 @@ static const struct nci_ops virtual_nci_ops = {
static ssize_t virtual_ncidev_read(struct file *file, char __user *buf, static ssize_t virtual_ncidev_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct virtual_nci_dev *vdev = file->private_data;
size_t actual_len; size_t actual_len;
mutex_lock(&nci_mutex); mutex_lock(&vdev->mtx);
while (!send_buff) { while (!vdev->send_buff) {
mutex_unlock(&nci_mutex); mutex_unlock(&vdev->mtx);
if (wait_event_interruptible(wq, send_buff)) if (wait_event_interruptible(vdev->wq, vdev->send_buff))
return -EFAULT; return -EFAULT;
mutex_lock(&nci_mutex); mutex_lock(&vdev->mtx);
} }
actual_len = min_t(size_t, count, send_buff->len); actual_len = min_t(size_t, count, vdev->send_buff->len);
if (copy_to_user(buf, send_buff->data, actual_len)) { if (copy_to_user(buf, vdev->send_buff->data, actual_len)) {
mutex_unlock(&nci_mutex); mutex_unlock(&vdev->mtx);
return -EFAULT; return -EFAULT;
} }
skb_pull(send_buff, actual_len); skb_pull(vdev->send_buff, actual_len);
if (send_buff->len == 0) { if (vdev->send_buff->len == 0) {
consume_skb(send_buff); consume_skb(vdev->send_buff);
send_buff = NULL; vdev->send_buff = NULL;
} }
mutex_unlock(&nci_mutex); mutex_unlock(&vdev->mtx);
return actual_len; return actual_len;
} }
...@@ -111,6 +109,7 @@ static ssize_t virtual_ncidev_write(struct file *file, ...@@ -111,6 +109,7 @@ static ssize_t virtual_ncidev_write(struct file *file,
const char __user *buf, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct virtual_nci_dev *vdev = file->private_data;
struct sk_buff *skb; struct sk_buff *skb;
skb = alloc_skb(count, GFP_KERNEL); skb = alloc_skb(count, GFP_KERNEL);
...@@ -122,63 +121,58 @@ static ssize_t virtual_ncidev_write(struct file *file, ...@@ -122,63 +121,58 @@ static ssize_t virtual_ncidev_write(struct file *file,
return -EFAULT; return -EFAULT;
} }
nci_recv_frame(ndev, skb); nci_recv_frame(vdev->ndev, skb);
return count; return count;
} }
static int virtual_ncidev_open(struct inode *inode, struct file *file) static int virtual_ncidev_open(struct inode *inode, struct file *file)
{ {
int ret = 0; int ret = 0;
struct virtual_nci_dev *vdev;
mutex_lock(&nci_mutex); vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
if (state != virtual_ncidev_disabled) { if (!vdev)
mutex_unlock(&nci_mutex); return -ENOMEM;
return -EBUSY; vdev->ndev = nci_allocate_device(&virtual_nci_ops,
} VIRTUAL_NFC_PROTOCOLS, 0, 0);
if (!vdev->ndev) {
ndev = nci_allocate_device(&virtual_nci_ops, VIRTUAL_NFC_PROTOCOLS, kfree(vdev);
0, 0);
if (!ndev) {
mutex_unlock(&nci_mutex);
return -ENOMEM; return -ENOMEM;
} }
ret = nci_register_device(ndev); mutex_init(&vdev->mtx);
init_waitqueue_head(&vdev->wq);
file->private_data = vdev;
nci_set_drvdata(vdev->ndev, vdev);
ret = nci_register_device(vdev->ndev);
if (ret < 0) { if (ret < 0) {
nci_free_device(ndev); nci_free_device(vdev->ndev);
mutex_unlock(&nci_mutex); mutex_destroy(&vdev->mtx);
kfree(vdev);
return ret; return ret;
} }
state = virtual_ncidev_enabled;
mutex_unlock(&nci_mutex);
return 0; return 0;
} }
static int virtual_ncidev_close(struct inode *inode, struct file *file) static int virtual_ncidev_close(struct inode *inode, struct file *file)
{ {
mutex_lock(&nci_mutex); struct virtual_nci_dev *vdev = file->private_data;
if (state == virtual_ncidev_enabled) {
state = virtual_ncidev_disabling;
mutex_unlock(&nci_mutex);
nci_unregister_device(ndev); nci_unregister_device(vdev->ndev);
nci_free_device(ndev); nci_free_device(vdev->ndev);
mutex_destroy(&vdev->mtx);
mutex_lock(&nci_mutex); kfree(vdev);
}
state = virtual_ncidev_disabled;
mutex_unlock(&nci_mutex);
return 0; return 0;
} }
static long virtual_ncidev_ioctl(struct file *flip, unsigned int cmd, static long virtual_ncidev_ioctl(struct file *file, unsigned int cmd,
unsigned long arg) unsigned long arg)
{ {
const struct nfc_dev *nfc_dev = ndev->nfc_dev; struct virtual_nci_dev *vdev = file->private_data;
const struct nfc_dev *nfc_dev = vdev->ndev->nfc_dev;
void __user *p = (void __user *)arg; void __user *p = (void __user *)arg;
if (cmd != IOCTL_GET_NCIDEV_IDX) if (cmd != IOCTL_GET_NCIDEV_IDX)
...@@ -199,14 +193,15 @@ static const struct file_operations virtual_ncidev_fops = { ...@@ -199,14 +193,15 @@ static const struct file_operations virtual_ncidev_fops = {
.unlocked_ioctl = virtual_ncidev_ioctl .unlocked_ioctl = virtual_ncidev_ioctl
}; };
static struct miscdevice miscdev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "virtual_nci",
.fops = &virtual_ncidev_fops,
.mode = 0600,
};
static int __init virtual_ncidev_init(void) static int __init virtual_ncidev_init(void)
{ {
state = virtual_ncidev_disabled;
miscdev.minor = MISC_DYNAMIC_MINOR;
miscdev.name = "virtual_nci";
miscdev.fops = &virtual_ncidev_fops;
miscdev.mode = 0600;
return misc_register(&miscdev); return misc_register(&miscdev);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment