Merge tag 'clang-lto-v5.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / net / qrtr / tun.c
CommitLineData
28fb4e59
BA
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2018, Linaro Ltd */
3
4#include <linux/miscdevice.h>
5#include <linux/module.h>
6#include <linux/poll.h>
7#include <linux/skbuff.h>
8#include <linux/uaccess.h>
9
10#include "qrtr.h"
11
12struct qrtr_tun {
13 struct qrtr_endpoint ep;
14
15 struct sk_buff_head queue;
16 wait_queue_head_t readq;
17};
18
19static int qrtr_tun_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
20{
21 struct qrtr_tun *tun = container_of(ep, struct qrtr_tun, ep);
22
23 skb_queue_tail(&tun->queue, skb);
24
25 /* wake up any blocking processes, waiting for new data */
26 wake_up_interruptible(&tun->readq);
27
28 return 0;
29}
30
31static int qrtr_tun_open(struct inode *inode, struct file *filp)
32{
33 struct qrtr_tun *tun;
34
35 tun = kzalloc(sizeof(*tun), GFP_KERNEL);
36 if (!tun)
37 return -ENOMEM;
38
39 skb_queue_head_init(&tun->queue);
40 init_waitqueue_head(&tun->readq);
41
42 tun->ep.xmit = qrtr_tun_send;
43
44 filp->private_data = tun;
45
46 return qrtr_endpoint_register(&tun->ep, QRTR_EP_NID_AUTO);
47}
48
49static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to)
50{
51 struct file *filp = iocb->ki_filp;
52 struct qrtr_tun *tun = filp->private_data;
53 struct sk_buff *skb;
54 int count;
55
56 while (!(skb = skb_dequeue(&tun->queue))) {
57 if (filp->f_flags & O_NONBLOCK)
58 return -EAGAIN;
59
60 /* Wait until we get data or the endpoint goes away */
61 if (wait_event_interruptible(tun->readq,
62 !skb_queue_empty(&tun->queue)))
63 return -ERESTARTSYS;
64 }
65
66 count = min_t(size_t, iov_iter_count(to), skb->len);
67 if (copy_to_iter(skb->data, count, to) != count)
68 count = -EFAULT;
69
70 kfree_skb(skb);
71
72 return count;
73}
74
75static ssize_t qrtr_tun_write_iter(struct kiocb *iocb, struct iov_iter *from)
76{
77 struct file *filp = iocb->ki_filp;
78 struct qrtr_tun *tun = filp->private_data;
79 size_t len = iov_iter_count(from);
80 ssize_t ret;
81 void *kbuf;
82
2a80c158
ST
83 if (!len)
84 return -EINVAL;
85
86 if (len > KMALLOC_MAX_SIZE)
87 return -ENOMEM;
88
28fb4e59
BA
89 kbuf = kzalloc(len, GFP_KERNEL);
90 if (!kbuf)
91 return -ENOMEM;
92
a21b7f0c
NE
93 if (!copy_from_iter_full(kbuf, len, from)) {
94 kfree(kbuf);
28fb4e59 95 return -EFAULT;
a21b7f0c 96 }
28fb4e59
BA
97
98 ret = qrtr_endpoint_post(&tun->ep, kbuf, len);
99
a21b7f0c 100 kfree(kbuf);
28fb4e59
BA
101 return ret < 0 ? ret : len;
102}
103
104static __poll_t qrtr_tun_poll(struct file *filp, poll_table *wait)
105{
106 struct qrtr_tun *tun = filp->private_data;
107 __poll_t mask = 0;
108
109 poll_wait(filp, &tun->readq, wait);
110
111 if (!skb_queue_empty(&tun->queue))
112 mask |= EPOLLIN | EPOLLRDNORM;
113
114 return mask;
115}
116
117static int qrtr_tun_release(struct inode *inode, struct file *filp)
118{
119 struct qrtr_tun *tun = filp->private_data;
28fb4e59
BA
120
121 qrtr_endpoint_unregister(&tun->ep);
122
123 /* Discard all SKBs */
21d8bd12 124 skb_queue_purge(&tun->queue);
28fb4e59
BA
125
126 kfree(tun);
127
128 return 0;
129}
130
131static const struct file_operations qrtr_tun_ops = {
132 .owner = THIS_MODULE,
133 .open = qrtr_tun_open,
134 .poll = qrtr_tun_poll,
135 .read_iter = qrtr_tun_read_iter,
136 .write_iter = qrtr_tun_write_iter,
137 .release = qrtr_tun_release,
138};
139
140static struct miscdevice qrtr_tun_miscdev = {
141 MISC_DYNAMIC_MINOR,
142 "qrtr-tun",
143 &qrtr_tun_ops,
144};
145
146static int __init qrtr_tun_init(void)
147{
148 int ret;
149
150 ret = misc_register(&qrtr_tun_miscdev);
151 if (ret)
152 pr_err("failed to register Qualcomm IPC Router tun device\n");
153
154 return ret;
155}
156
157static void __exit qrtr_tun_exit(void)
158{
159 misc_deregister(&qrtr_tun_miscdev);
160}
161
162module_init(qrtr_tun_init);
163module_exit(qrtr_tun_exit);
164
165MODULE_DESCRIPTION("Qualcomm IPC Router TUN device");
166MODULE_LICENSE("GPL v2");