From patchwork Fri Apr 27 05:28:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bjorn Andersson X-Patchwork-Id: 134556 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp299892lji; Thu, 26 Apr 2018 22:29:00 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo/Hz+FBPClsEhtVPNf9QNX7UA5wGMasn8FIrOQyPNoVa4eihqwY4BMr531CqJVV/jFmPUt X-Received: by 10.98.253.9 with SMTP id p9mr921816pfh.152.1524806940295; Thu, 26 Apr 2018 22:29:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524806940; cv=none; d=google.com; s=arc-20160816; b=oj8t5CW5EaOFTg3QlNDcJIg459rDhN3IxhZgUAA+4HkMD6Yw/2tfota4wDIa+GQmqJ DzNS9cB71vtEnUC7+iscQlOdPfQdU3oUha1mhtDSUOsHFxZFqTr7rzhtH4mGW2n7TkSG IaKLPkXgO83dbOQk6vTIyix6/ZvRVHudlqwMNEDA0CfchMSc5YxVST3MFyT01+l5I/OT YuxwjZGQYPVBHzIR1Ks0eWXsdw16SY8wEAYBbPBavsnlQi3I4lFaVsVJhTJ8bhwHhtEy gd+BWXxRoToCSjAlbcg9H4T/ySuQBxFgM9unQQp00JWTl1flgjnhZRKABltCmYT3r16P qR3Q== 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=XqtM1sN+4vbxkxqqBtY10IKiFMVH1X22lI2WFQOm4BA=; b=oUVeGexmkd5N1ufvf6PaFxKuhbmfgInsHMykjaafYODBjYWTb0fDaHxPcz0rF7LBme eTydR9ELGPCdqIi4xPSowkIXKYyEBR9Gm/gcl3HeTML3CMmS0yyD+zebfsjzWgfAi8sf khxlpFmhwfSVinsBWXA83LWJapTixKoM46i9eI5G0viCmw0P9zRhdaMV8NPtupX9KGmj VOnl5BhLChT0xbmI3Qlf1lZ8ipdv4sZzRQnV5AWXBZ7HpC5cR96zJeotsKAlNTVymZJO p7KDdVDV9dIWjFE1Xp8xiukg6NCFQBYVI9oiRkruYSvy8BZjzy45yYbilYoA88tYMAx9 0ZIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RZRKeqg4; 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 31-v6si564316plz.364.2018.04.26.22.29.00; Thu, 26 Apr 2018 22:29:00 -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=RZRKeqg4; 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 S1757345AbeD0F2z (ORCPT + 29 others); Fri, 27 Apr 2018 01:28:55 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:45505 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751988AbeD0F2x (ORCPT ); Fri, 27 Apr 2018 01:28:53 -0400 Received: by mail-pg0-f66.google.com with SMTP id i29-v6so654077pgn.12 for ; Thu, 26 Apr 2018 22:28:53 -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=XqtM1sN+4vbxkxqqBtY10IKiFMVH1X22lI2WFQOm4BA=; b=RZRKeqg4mHTdYyUrHo00rWCoEAwhkohTM1hFRppMrLV44zhbZyRGz9/xoHMrRGRpfM /1swe0RzFpfjMfo8TG7MBUdxOh/9IW+Vsn+xIm2RNK0szI9I/PsZgTtDEX5q1sXirDob xLt8N//KkJfhrfv+0QgLpjqS9JEjJGmdE5nAk= 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=XqtM1sN+4vbxkxqqBtY10IKiFMVH1X22lI2WFQOm4BA=; b=b9d+sELUdMYt4I9sEMqtsXScyi10lTdMGGnurJqPahwO25/EFvkf2QShxdScfRSKtR 723XBeOu1Eah4NNhY0kprqj+uiiNWxNiczvBzGEDQ/CKIIIGnWEqO+7qSTj76/7P0owt 6sD5iWCmUjyZ0iIzzp2aJtz994ATKknGGsMyE6jg5spbAr/xEltfe2IuA84hgqfn3h4u gLXkv9mq0u2HuPXgY0x4Ej6Jjd+5Ned++3YWiyQ5S/NgJ20HP2TshrL6h4hINrso+o6Q c9bzP5OeqHH/ByxlpIpvg5D9nggqdA3pPRl/uZkeF0tJCfw4OiqewWbsnajVcBM73FJt ywKw== X-Gm-Message-State: ALQs6tCCoS37plihrFYhhMovix/ijVQMleTsBJlHkSWzMER8M2aq/xO5 FRypHpNY0MfJfnwveKLp0u0Gbg== X-Received: by 2002:a63:6fc8:: with SMTP id k191-v6mr901130pgc.330.1524806932793; Thu, 26 Apr 2018 22:28:52 -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 d23sm806152pfn.22.2018.04.26.22.28.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Apr 2018 22:28:51 -0700 (PDT) From: Bjorn Andersson To: "David S. Miller" , Chris Lew Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-arm-msm@vger.kernel.org Subject: [PATCH v3] net: qrtr: Expose tunneling endpoint to user space Date: Thu, 26 Apr 2018 22:28:49 -0700 Message-Id: <20180427052849.24899-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 --- Changes since v2: - Add support for read poll Changes since v1: - Dropped queue lock net/qrtr/Kconfig | 7 +++ net/qrtr/Makefile | 2 + net/qrtr/tun.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 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..ccff1e544c21 --- /dev/null +++ b/net/qrtr/tun.c @@ -0,0 +1,161 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2018, Linaro Ltd */ + +#include +#include +#include +#include +#include + +#include "qrtr.h" + +struct qrtr_tun { + struct qrtr_endpoint ep; + + 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); + + skb_queue_tail(&tun->queue, skb); + + /* 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; + + 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; + + while (!(skb = skb_dequeue(&tun->queue))) { + 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; + } + + 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 __poll_t qrtr_tun_poll(struct file *filp, poll_table *wait) +{ + struct qrtr_tun *tun = filp->private_data; + __poll_t mask = 0; + + poll_wait(filp, &tun->readq, wait); + + if (!skb_queue_empty(&tun->queue)) + mask |= EPOLLIN | EPOLLRDNORM; + + return mask; +} + +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, + .poll = qrtr_tun_poll, + .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");