From patchwork Thu Apr 19 05:03:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bjorn Andersson X-Patchwork-Id: 133694 Delivered-To: patch@linaro.org Received: by 10.46.66.142 with SMTP id h14csp218963ljf; Wed, 18 Apr 2018 22:03:55 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+V4hhuGvhtY7uD5glTJlm8X1eGN6DaKSrNTbMe7r/DkFsoxPZa1BAoH0xEBM8ZtLtPl2LU X-Received: by 10.167.133.66 with SMTP id y2mr4520866pfn.210.1524114235448; Wed, 18 Apr 2018 22:03:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524114235; cv=none; d=google.com; s=arc-20160816; b=Nn0xtl4DT6o1ouNqcDpjgwAza7F9f/9ytcPIeOmO280B2Po5RXXljrc45LK5rsWJ94 3ibiNNwDSwh2XwXPStUKj6humrRSk7iGcqW4Y2yEvT2UP3me/QEtOrJOjTHIdh6B3yn2 9UqOEX68Vv8daAp2IDkgl/xvIIHaR63YOPubM/Yua16Y9UV4krk4OrmfhwzHOhmuMwYA RNnkuvCs1JfqRfVzzlyYWizlT/3c0ZGDEi05liUcoZcoTvrvBO3/65TKuQS/4QIBzaCC ovbiLCoWrmQ7hCUfkvfJUehwMeJAhVO/TaS2Dq+H42raJaXV3OyJJuj1wW0Wcbtph8KF JU0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=3Pzncyidd3RUMU/VXdwkynYFO0Iw2Iwzqg1y7a9W5eI=; b=NZ/SVskCr1hXepoI0wITeBP4f5oaKzZ8lwUz8rXyQ/MlVuPf/9/KEo3wdTvYQLp5uw HVUV7gS+w/XI34+QeBRS9GQyxEGMXeH1Y2ppyRm1zjtKUELZ4LJXtIP7GK9YmgIsIE5D AuC/t+HK9P2rOwCt0d8NyETnGyHMzK4hormIEgxnCG/KAT8mfmn8vEiEGuRYgMacEa4D AtcuOP6ctqHk7Zi9NE52TE8UqSNzVh/eSDE0iYhh4qN9Yfqerupkf8iLXnRMZfVSc4iS BC7p5ROA0l22YKCsp/u7ExY7UWF3l6QrwzkYGXOqNscUAYzpPKhPfu7apjPDBwueQ2F7 HwKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZDROariC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 39-v6si2709238plc.515.2018.04.18.22.03.55; Wed, 18 Apr 2018 22:03:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZDROariC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753284AbeDSFDx (ORCPT + 29 others); Thu, 19 Apr 2018 01:03:53 -0400 Received: from mail-pl0-f67.google.com ([209.85.160.67]:36781 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753059AbeDSFDu (ORCPT ); Thu, 19 Apr 2018 01:03:50 -0400 Received: by mail-pl0-f67.google.com with SMTP id m7-v6so2505557plt.3 for ; Wed, 18 Apr 2018 22:03:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=3Pzncyidd3RUMU/VXdwkynYFO0Iw2Iwzqg1y7a9W5eI=; b=ZDROariCZVcq/LCmjds9BXl4JOsrcTI/8s+gAUVxLcGxQndoZOWvUynTnc6YEQePHQ mkRrV7bMf6dDWf+KPM2wxHAEbqnJLE8bUoa7+gEbAltDCy3sGHK1F75wRCHOI+cp6TOJ 4e3pbjrud31dnQz7zgT7vNxDsBWyml0IMb4Yg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=3Pzncyidd3RUMU/VXdwkynYFO0Iw2Iwzqg1y7a9W5eI=; b=icxU5idCx3TUfPqWKGuOsxHmotCo/+B5hRzBNUvIJAVFwZ7ND7u49El70uhJ8nyHYR 9k7Byzsl6icO4vxTb62wJNo33CokxozlXibnSJUbTsgH6xI+eHoTRKYSEo0tBJ2EZxVL 1FfjPpw3FzCz3iCdmmo5uV/M9wqiHLzE7NYKXDbBmVZjPboPLBThAuLn560sorRC3pb1 z6UqcnMv6JX0CmGPpeejRxHHtpeyp6qVtg56NkDC1KQvBM7TrdpphmLG4jDit7UUKFGf 1yQcK6/OKgkXOeDlhOaYe9azo9wMapexpG0fGFUxDgSbegLMT/ZopgzMODU87SpOFy/c bA5w== X-Gm-Message-State: ALQs6tDYN3ZC7oElFWOrVv8f7bCqTPYX3hYBa/Kp4ufV3fQQepcHSqRf m8GGFlj7NRT56kIueDoPCVNhQGarvlc= X-Received: by 2002:a17:902:362:: with SMTP id 89-v6mr4817753pld.270.1524114229374; Wed, 18 Apr 2018 22:03:49 -0700 (PDT) Received: from localhost.localdomain (104-188-17-28.lightspeed.sndgca.sbcglobal.net. [104.188.17.28]) by smtp.gmail.com with ESMTPSA id k70sm5601724pga.72.2018.04.18.22.03.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Apr 2018 22:03:48 -0700 (PDT) From: Bjorn Andersson To: "David S. Miller" Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-arm-msm@vger.kernel.org, Chris Lew Subject: [PATCH] net: qrtr: Expose tunneling endpoint to user space Date: Wed, 18 Apr 2018 22:03:46 -0700 Message-Id: <20180419050346.17054-1-bjorn.andersson@linaro.org> X-Mailer: git-send-email 2.16.2 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This implements a misc character device named "qrtr-tun" for the purpose of allowing user space applications to implement endpoints in the qrtr network. This allows more advanced (and dynamic) testing of the qrtr code as well as opens up the ability of tunneling qrtr over a network or USB link. Signed-off-by: Bjorn Andersson --- net/qrtr/Kconfig | 7 +++ net/qrtr/Makefile | 2 + net/qrtr/tun.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 net/qrtr/tun.c -- 2.16.2 diff --git a/net/qrtr/Kconfig b/net/qrtr/Kconfig index 326fd97444f5..1944834d225c 100644 --- a/net/qrtr/Kconfig +++ b/net/qrtr/Kconfig @@ -21,4 +21,11 @@ config QRTR_SMD Say Y here to support SMD based ipcrouter channels. SMD is the most common transport for IPC Router. +config QRTR_TUN + tristate "TUN device for Qualcomm IPC Router" + ---help--- + Say Y here to expose a character device that allows user space to + implement endpoints of QRTR, for purpose of tunneling data to other + hosts or testing purposes. + endif # QRTR diff --git a/net/qrtr/Makefile b/net/qrtr/Makefile index ab09e40f7c74..be012bfd3e52 100644 --- a/net/qrtr/Makefile +++ b/net/qrtr/Makefile @@ -2,3 +2,5 @@ obj-$(CONFIG_QRTR) := qrtr.o obj-$(CONFIG_QRTR_SMD) += qrtr-smd.o qrtr-smd-y := smd.o +obj-$(CONFIG_QRTR_TUN) += qrtr-tun.o +qrtr-tun-y := tun.o diff --git a/net/qrtr/tun.c b/net/qrtr/tun.c new file mode 100644 index 000000000000..ab87a12cbb17 --- /dev/null +++ b/net/qrtr/tun.c @@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2018, Linaro Ltd */ + +#include +#include +#include +#include + +#include "qrtr.h" + +struct qrtr_tun { + struct qrtr_endpoint ep; + + struct mutex queue_lock; + struct sk_buff_head queue; + wait_queue_head_t readq; +}; + +static int qrtr_tun_send(struct qrtr_endpoint *ep, struct sk_buff *skb) +{ + struct qrtr_tun *tun = container_of(ep, struct qrtr_tun, ep); + + mutex_lock(&tun->queue_lock); + skb_queue_tail(&tun->queue, skb); + mutex_unlock(&tun->queue_lock); + + /* wake up any blocking processes, waiting for new data */ + wake_up_interruptible(&tun->readq); + + return 0; +} + +static int qrtr_tun_open(struct inode *inode, struct file *filp) +{ + struct qrtr_tun *tun; + + tun = kzalloc(sizeof(*tun), GFP_KERNEL); + if (!tun) + return -ENOMEM; + + mutex_init(&tun->queue_lock); + skb_queue_head_init(&tun->queue); + init_waitqueue_head(&tun->readq); + + tun->ep.xmit = qrtr_tun_send; + + filp->private_data = tun; + + return qrtr_endpoint_register(&tun->ep, QRTR_EP_NID_AUTO); +} + +static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to) +{ + struct file *filp = iocb->ki_filp; + struct qrtr_tun *tun = filp->private_data; + struct sk_buff *skb; + int count; + + mutex_lock(&tun->queue_lock); + + /* Wait for data in the queue */ + if (skb_queue_empty(&tun->queue)) { + mutex_unlock(&tun->queue_lock); + + if (filp->f_flags & O_NONBLOCK) + return -EAGAIN; + + /* Wait until we get data or the endpoint goes away */ + if (wait_event_interruptible(tun->readq, + !skb_queue_empty(&tun->queue))) + return -ERESTARTSYS; + + mutex_lock(&tun->queue_lock); + } + + skb = skb_dequeue(&tun->queue); + mutex_unlock(&tun->queue_lock); + if (!skb) + return -EFAULT; + + count = min_t(size_t, iov_iter_count(to), skb->len); + if (copy_to_iter(skb->data, count, to) != count) + count = -EFAULT; + + kfree_skb(skb); + + return count; +} + +static ssize_t qrtr_tun_write_iter(struct kiocb *iocb, struct iov_iter *from) +{ + struct file *filp = iocb->ki_filp; + struct qrtr_tun *tun = filp->private_data; + size_t len = iov_iter_count(from); + ssize_t ret; + void *kbuf; + + kbuf = kzalloc(len, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + if (!copy_from_iter_full(kbuf, len, from)) + return -EFAULT; + + ret = qrtr_endpoint_post(&tun->ep, kbuf, len); + + return ret < 0 ? ret : len; +} + +static int qrtr_tun_release(struct inode *inode, struct file *filp) +{ + struct qrtr_tun *tun = filp->private_data; + struct sk_buff *skb; + + qrtr_endpoint_unregister(&tun->ep); + + /* Discard all SKBs */ + while (!skb_queue_empty(&tun->queue)) { + skb = skb_dequeue(&tun->queue); + kfree_skb(skb); + } + + kfree(tun); + + return 0; +} + +static const struct file_operations qrtr_tun_ops = { + .owner = THIS_MODULE, + .open = qrtr_tun_open, + .read_iter = qrtr_tun_read_iter, + .write_iter = qrtr_tun_write_iter, + .release = qrtr_tun_release, +}; + +static struct miscdevice qrtr_tun_miscdev = { + MISC_DYNAMIC_MINOR, + "qrtr-tun", + &qrtr_tun_ops, +}; + +static int __init qrtr_tun_init(void) +{ + int ret; + + ret = misc_register(&qrtr_tun_miscdev); + if (ret) + pr_err("failed to register Qualcomm IPC Router tun device\n"); + + return ret; +} + +static void __exit qrtr_tun_exit(void) +{ + misc_deregister(&qrtr_tun_miscdev); +} + +module_init(qrtr_tun_init); +module_exit(qrtr_tun_exit); + +MODULE_DESCRIPTION("Qualcomm IPC Router TUN device"); +MODULE_LICENSE("GPL v2");