~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/qrtr/tun.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 12 struct qrtr_tun {
 13         struct qrtr_endpoint ep;
 14 
 15         struct sk_buff_head queue;
 16         wait_queue_head_t readq;
 17 };
 18 
 19 static 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 
 31 static int qrtr_tun_open(struct inode *inode, struct file *filp)
 32 {
 33         struct qrtr_tun *tun;
 34         int ret;
 35 
 36         tun = kzalloc(sizeof(*tun), GFP_KERNEL);
 37         if (!tun)
 38                 return -ENOMEM;
 39 
 40         skb_queue_head_init(&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(&tun->ep, QRTR_EP_NID_AUTO);
 48         if (ret)
 49                 goto out;
 50 
 51         return 0;
 52 
 53 out:
 54         filp->private_data = NULL;
 55         kfree(tun);
 56         return ret;
 57 }
 58 
 59 static 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(&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(skb->data, count, to) != count)
 78                 count = -EFAULT;
 79 
 80         kfree_skb(skb);
 81 
 82         return count;
 83 }
 84 
 85 static 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(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(len, GFP_KERNEL);
100         if (!kbuf)
101                 return -ENOMEM;
102 
103         if (!copy_from_iter_full(kbuf, len, from)) {
104                 kfree(kbuf);
105                 return -EFAULT;
106         }
107 
108         ret = qrtr_endpoint_post(&tun->ep, kbuf, len);
109 
110         kfree(kbuf);
111         return ret < 0 ? ret : len;
112 }
113 
114 static __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, &tun->readq, wait);
120 
121         if (!skb_queue_empty(&tun->queue))
122                 mask |= EPOLLIN | EPOLLRDNORM;
123 
124         return mask;
125 }
126 
127 static int qrtr_tun_release(struct inode *inode, struct file *filp)
128 {
129         struct qrtr_tun *tun = filp->private_data;
130 
131         qrtr_endpoint_unregister(&tun->ep);
132 
133         /* Discard all SKBs */
134         skb_queue_purge(&tun->queue);
135 
136         kfree(tun);
137 
138         return 0;
139 }
140 
141 static 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 
150 static struct miscdevice qrtr_tun_miscdev = {
151         MISC_DYNAMIC_MINOR,
152         "qrtr-tun",
153         &qrtr_tun_ops,
154 };
155 
156 static int __init qrtr_tun_init(void)
157 {
158         int ret;
159 
160         ret = misc_register(&qrtr_tun_miscdev);
161         if (ret)
162                 pr_err("failed to register Qualcomm IPC Router tun device\n");
163 
164         return ret;
165 }
166 
167 static void __exit qrtr_tun_exit(void)
168 {
169         misc_deregister(&qrtr_tun_miscdev);
170 }
171 
172 module_init(qrtr_tun_init);
173 module_exit(qrtr_tun_exit);
174 
175 MODULE_DESCRIPTION("Qualcomm IPC Router TUN device");
176 MODULE_LICENSE("GPL v2");
177 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php