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(list: &tun->queue, newsk: 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 int ret;
35
36 tun = kzalloc(size: sizeof(*tun), GFP_KERNEL);
37 if (!tun)
38 return -ENOMEM;
39
40 skb_queue_head_init(list: &tun->queue);
41 init_waitqueue_head(&tun->readq);
42
43 tun->ep.xmit = qrtr_tun_send;
44
45 filp->private_data = tun;
46
47 ret = qrtr_endpoint_register(ep: &tun->ep, QRTR_EP_NID_AUTO);
48 if (ret)
49 goto out;
50
51 return 0;
52
53out:
54 filp->private_data = NULL;
55 kfree(objp: tun);
56 return ret;
57}
58
59static ssize_t qrtr_tun_read_iter(struct kiocb *iocb, struct iov_iter *to)
60{
61 struct file *filp = iocb->ki_filp;
62 struct qrtr_tun *tun = filp->private_data;
63 struct sk_buff *skb;
64 int count;
65
66 while (!(skb = skb_dequeue(list: &tun->queue))) {
67 if (filp->f_flags & O_NONBLOCK)
68 return -EAGAIN;
69
70 /* Wait until we get data or the endpoint goes away */
71 if (wait_event_interruptible(tun->readq,
72 !skb_queue_empty(&tun->queue)))
73 return -ERESTARTSYS;
74 }
75
76 count = min_t(size_t, iov_iter_count(to), skb->len);
77 if (copy_to_iter(addr: skb->data, bytes: count, i: to) != count)
78 count = -EFAULT;
79
80 kfree_skb(skb);
81
82 return count;
83}
84
85static ssize_t qrtr_tun_write_iter(struct kiocb *iocb, struct iov_iter *from)
86{
87 struct file *filp = iocb->ki_filp;
88 struct qrtr_tun *tun = filp->private_data;
89 size_t len = iov_iter_count(i: from);
90 ssize_t ret;
91 void *kbuf;
92
93 if (!len)
94 return -EINVAL;
95
96 if (len > KMALLOC_MAX_SIZE)
97 return -ENOMEM;
98
99 kbuf = kzalloc(size: len, GFP_KERNEL);
100 if (!kbuf)
101 return -ENOMEM;
102
103 if (!copy_from_iter_full(addr: kbuf, bytes: len, i: from)) {
104 kfree(objp: kbuf);
105 return -EFAULT;
106 }
107
108 ret = qrtr_endpoint_post(ep: &tun->ep, data: kbuf, len);
109
110 kfree(objp: kbuf);
111 return ret < 0 ? ret : len;
112}
113
114static __poll_t qrtr_tun_poll(struct file *filp, poll_table *wait)
115{
116 struct qrtr_tun *tun = filp->private_data;
117 __poll_t mask = 0;
118
119 poll_wait(filp, wait_address: &tun->readq, p: wait);
120
121 if (!skb_queue_empty(list: &tun->queue))
122 mask |= EPOLLIN | EPOLLRDNORM;
123
124 return mask;
125}
126
127static int qrtr_tun_release(struct inode *inode, struct file *filp)
128{
129 struct qrtr_tun *tun = filp->private_data;
130
131 qrtr_endpoint_unregister(ep: &tun->ep);
132
133 /* Discard all SKBs */
134 skb_queue_purge(list: &tun->queue);
135
136 kfree(objp: tun);
137
138 return 0;
139}
140
141static const struct file_operations qrtr_tun_ops = {
142 .owner = THIS_MODULE,
143 .open = qrtr_tun_open,
144 .poll = qrtr_tun_poll,
145 .read_iter = qrtr_tun_read_iter,
146 .write_iter = qrtr_tun_write_iter,
147 .release = qrtr_tun_release,
148};
149
150static struct miscdevice qrtr_tun_miscdev = {
151 MISC_DYNAMIC_MINOR,
152 "qrtr-tun",
153 &qrtr_tun_ops,
154};
155
156static int __init qrtr_tun_init(void)
157{
158 int ret;
159
160 ret = misc_register(misc: &qrtr_tun_miscdev);
161 if (ret)
162 pr_err("failed to register Qualcomm IPC Router tun device\n");
163
164 return ret;
165}
166
167static void __exit qrtr_tun_exit(void)
168{
169 misc_deregister(misc: &qrtr_tun_miscdev);
170}
171
172module_init(qrtr_tun_init);
173module_exit(qrtr_tun_exit);
174
175MODULE_DESCRIPTION("Qualcomm IPC Router TUN device");
176MODULE_LICENSE("GPL v2");
177

source code of linux/net/qrtr/tun.c