blob: f549d3a8e59c0380c7f2d375d88900bf422b4675 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Arnd Bergmann20d29d72010-01-30 12:24:26 +00002#include <linux/etherdevice.h>
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08003#include <linux/if_tap.h>
Basil Gorf09e2242012-05-03 22:55:24 +00004#include <linux/if_vlan.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +00005#include <linux/interrupt.h>
6#include <linux/nsproxy.h>
7#include <linux/compat.h>
8#include <linux/if_tun.h>
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/cache.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010012#include <linux/sched/signal.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000013#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000015#include <linux/wait.h>
16#include <linux/cdev.h>
Al Viro40401532012-02-13 03:58:52 +000017#include <linux/idr.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000018#include <linux/fs.h>
Herbert Xu6c36d2e2014-11-07 21:22:25 +080019#include <linux/uio.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000020
21#include <net/net_namespace.h>
22#include <net/rtnetlink.h>
23#include <net/sock.h>
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +000024#include <linux/virtio_net.h>
Jason Wang362899b2016-07-15 03:46:31 -040025#include <linux/skb_array.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000026
Sainath Grandhi635b8c82017-02-10 16:03:47 -080027#define TAP_IFFEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
Michael S. Tsirkin01b07fb2014-12-16 15:05:10 +020028
Sainath Grandhi635b8c82017-02-10 16:03:47 -080029#define TAP_VNET_LE 0x80000000
30#define TAP_VNET_BE 0x40000000
Greg Kurz8b8e6582015-04-24 14:50:36 +020031
32#ifdef CONFIG_TUN_VNET_CROSS_LE
Sainath Grandhi635b8c82017-02-10 16:03:47 -080033static inline bool tap_legacy_is_little_endian(struct tap_queue *q)
Greg Kurz8b8e6582015-04-24 14:50:36 +020034{
Sainath Grandhi635b8c82017-02-10 16:03:47 -080035 return q->flags & TAP_VNET_BE ? false :
Greg Kurz8b8e6582015-04-24 14:50:36 +020036 virtio_legacy_is_little_endian();
37}
38
Sainath Grandhi635b8c82017-02-10 16:03:47 -080039static long tap_get_vnet_be(struct tap_queue *q, int __user *sp)
Greg Kurz8b8e6582015-04-24 14:50:36 +020040{
Sainath Grandhi635b8c82017-02-10 16:03:47 -080041 int s = !!(q->flags & TAP_VNET_BE);
Greg Kurz8b8e6582015-04-24 14:50:36 +020042
43 if (put_user(s, sp))
44 return -EFAULT;
45
46 return 0;
47}
48
Sainath Grandhi635b8c82017-02-10 16:03:47 -080049static long tap_set_vnet_be(struct tap_queue *q, int __user *sp)
Greg Kurz8b8e6582015-04-24 14:50:36 +020050{
51 int s;
52
53 if (get_user(s, sp))
54 return -EFAULT;
55
56 if (s)
Sainath Grandhi635b8c82017-02-10 16:03:47 -080057 q->flags |= TAP_VNET_BE;
Greg Kurz8b8e6582015-04-24 14:50:36 +020058 else
Sainath Grandhi635b8c82017-02-10 16:03:47 -080059 q->flags &= ~TAP_VNET_BE;
Greg Kurz8b8e6582015-04-24 14:50:36 +020060
61 return 0;
62}
63#else
Sainath Grandhi635b8c82017-02-10 16:03:47 -080064static inline bool tap_legacy_is_little_endian(struct tap_queue *q)
Greg Kurz8b8e6582015-04-24 14:50:36 +020065{
66 return virtio_legacy_is_little_endian();
67}
68
Sainath Grandhi635b8c82017-02-10 16:03:47 -080069static long tap_get_vnet_be(struct tap_queue *q, int __user *argp)
Greg Kurz8b8e6582015-04-24 14:50:36 +020070{
71 return -EINVAL;
72}
73
Sainath Grandhi635b8c82017-02-10 16:03:47 -080074static long tap_set_vnet_be(struct tap_queue *q, int __user *argp)
Greg Kurz8b8e6582015-04-24 14:50:36 +020075{
76 return -EINVAL;
77}
78#endif /* CONFIG_TUN_VNET_CROSS_LE */
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +020079
Sainath Grandhi635b8c82017-02-10 16:03:47 -080080static inline bool tap_is_little_endian(struct tap_queue *q)
Greg Kurz5b11e152015-04-24 14:24:48 +020081{
Sainath Grandhi635b8c82017-02-10 16:03:47 -080082 return q->flags & TAP_VNET_LE ||
83 tap_legacy_is_little_endian(q);
Greg Kurz5b11e152015-04-24 14:24:48 +020084}
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +020085
Sainath Grandhi635b8c82017-02-10 16:03:47 -080086static inline u16 tap16_to_cpu(struct tap_queue *q, __virtio16 val)
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +020087{
Sainath Grandhi635b8c82017-02-10 16:03:47 -080088 return __virtio16_to_cpu(tap_is_little_endian(q), val);
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +020089}
90
Sainath Grandhi635b8c82017-02-10 16:03:47 -080091static inline __virtio16 cpu_to_tap16(struct tap_queue *q, u16 val)
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +020092{
Sainath Grandhi635b8c82017-02-10 16:03:47 -080093 return __cpu_to_virtio16(tap_is_little_endian(q), val);
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +020094}
95
Sainath Grandhi635b8c82017-02-10 16:03:47 -080096static struct proto tap_proto = {
97 .name = "tap",
Arnd Bergmann20d29d72010-01-30 12:24:26 +000098 .owner = THIS_MODULE,
Sainath Grandhi635b8c82017-02-10 16:03:47 -080099 .obj_size = sizeof(struct tap_queue),
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000100};
101
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800102#define TAP_NUM_DEVS (1U << MINORBITS)
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800103
104static LIST_HEAD(major_list);
105
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800106struct major_info {
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800107 struct rcu_head rcu;
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800108 dev_t major;
109 struct idr minor_idr;
WANG Congffa423f2017-07-10 10:05:50 -0700110 spinlock_t minor_lock;
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800111 const char *device_name;
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800112 struct list_head next;
113};
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000114
Shirley Ma97bc3632011-07-06 12:26:11 +0000115#define GOODCOPY_LEN 128
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000116
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800117static const struct proto_ops tap_socket_ops;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000118
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400119#define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
Jason Wangf23d5382015-10-23 00:57:05 -0400120#define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST)
Vlad Yasevicha567dd62013-08-16 15:25:00 -0400121
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800122static struct tap_dev *tap_dev_get_rcu(const struct net_device *dev)
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500123{
124 return rcu_dereference(dev->rx_handler_data);
125}
126
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000127/*
128 * RCU usage:
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800129 * The tap_queue and the macvlan_dev are loosely coupled, the
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000130 * pointers from one to the other can only be read while rcu_read_lock
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400131 * or rtnl is held.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000132 *
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800133 * Both the file and the macvlan_dev hold a reference on the tap_queue
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000134 * through sock_hold(&q->sk). When the macvlan_dev goes away first,
135 * q->vlan becomes inaccessible. When the files gets closed,
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800136 * tap_get_queue() fails.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000137 *
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000138 * There may still be references to the struct sock inside of the
139 * queue from outbound SKBs, but these never reference back to the
140 * file or the dev. The data structure is freed through __sk_free
141 * when both our references and any pending SKBs are gone.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000142 */
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000143
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800144static int tap_enable_queue(struct tap_dev *tap, struct file *file,
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800145 struct tap_queue *q)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000146{
Jason Wang815f2362013-06-05 23:54:39 +0000147 int err = -EINVAL;
148
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400149 ASSERT_RTNL();
Jason Wang815f2362013-06-05 23:54:39 +0000150
151 if (q->enabled)
152 goto out;
153
154 err = 0;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800155 rcu_assign_pointer(tap->taps[tap->numvtaps], q);
156 q->queue_index = tap->numvtaps;
Jason Wang815f2362013-06-05 23:54:39 +0000157 q->enabled = true;
158
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800159 tap->numvtaps++;
Jason Wang815f2362013-06-05 23:54:39 +0000160out:
Jason Wang815f2362013-06-05 23:54:39 +0000161 return err;
162}
163
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400164/* Requires RTNL */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800165static int tap_set_queue(struct tap_dev *tap, struct file *file,
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800166 struct tap_queue *q)
Jason Wang815f2362013-06-05 23:54:39 +0000167{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800168 if (tap->numqueues == MAX_TAP_QUEUES)
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400169 return -EBUSY;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000170
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800171 rcu_assign_pointer(q->tap, tap);
172 rcu_assign_pointer(tap->taps[tap->numvtaps], q);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000173 sock_hold(&q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000174
175 q->file = file;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800176 q->queue_index = tap->numvtaps;
Jason Wang815f2362013-06-05 23:54:39 +0000177 q->enabled = true;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000178 file->private_data = q;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800179 list_add_tail(&q->next, &tap->queue_list);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000180
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800181 tap->numvtaps++;
182 tap->numqueues++;
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000183
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400184 return 0;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000185}
186
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800187static int tap_disable_queue(struct tap_queue *q)
Jason Wang815f2362013-06-05 23:54:39 +0000188{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800189 struct tap_dev *tap;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800190 struct tap_queue *nq;
Jason Wang815f2362013-06-05 23:54:39 +0000191
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400192 ASSERT_RTNL();
Jason Wang815f2362013-06-05 23:54:39 +0000193 if (!q->enabled)
194 return -EINVAL;
195
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800196 tap = rtnl_dereference(q->tap);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400197
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800198 if (tap) {
Jason Wang815f2362013-06-05 23:54:39 +0000199 int index = q->queue_index;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800200 BUG_ON(index >= tap->numvtaps);
201 nq = rtnl_dereference(tap->taps[tap->numvtaps - 1]);
Jason Wang815f2362013-06-05 23:54:39 +0000202 nq->queue_index = index;
203
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800204 rcu_assign_pointer(tap->taps[index], nq);
205 RCU_INIT_POINTER(tap->taps[tap->numvtaps - 1], NULL);
Jason Wang815f2362013-06-05 23:54:39 +0000206 q->enabled = false;
207
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800208 tap->numvtaps--;
Jason Wang815f2362013-06-05 23:54:39 +0000209 }
210
211 return 0;
212}
213
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000214/*
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000215 * The file owning the queue got closed, give up both
216 * the reference that the files holds as well as the
217 * one from the macvlan_dev if that still exists.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000218 *
219 * Using the spinlock makes sure that we don't get
220 * to the queue again after destroying it.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000221 */
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800222static void tap_put_queue(struct tap_queue *q)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000223{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800224 struct tap_dev *tap;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000225
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400226 rtnl_lock();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800227 tap = rtnl_dereference(q->tap);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400228
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800229 if (tap) {
Jason Wang815f2362013-06-05 23:54:39 +0000230 if (q->enabled)
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800231 BUG_ON(tap_disable_queue(q));
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000232
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800233 tap->numqueues--;
234 RCU_INIT_POINTER(q->tap, NULL);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000235 sock_put(&q->sk);
Jason Wang815f2362013-06-05 23:54:39 +0000236 list_del_init(&q->next);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000237 }
238
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400239 rtnl_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000240
241 synchronize_rcu();
242 sock_put(&q->sk);
243}
244
245/*
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000246 * Select a queue based on the rxq of the device on which this packet
247 * arrived. If the incoming device is not mq, calculate a flow hash
248 * to select a queue. If all fails, find the first available queue.
249 * Cache vlan->numvtaps since it can become zero during the execution
250 * of this function.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000251 */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800252static struct tap_queue *tap_get_queue(struct tap_dev *tap,
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800253 struct sk_buff *skb)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000254{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800255 struct tap_queue *queue = NULL;
Jason Wang815f2362013-06-05 23:54:39 +0000256 /* Access to taps array is protected by rcu, but access to numvtaps
257 * isn't. Below we use it to lookup a queue, but treat it as a hint
258 * and validate that the result isn't NULL - in case we are
259 * racing against queue removal.
260 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700261 int numvtaps = READ_ONCE(tap->numvtaps);
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000262 __u32 rxq;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000263
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000264 if (!numvtaps)
265 goto out;
266
Jason Wang1b16bf42016-07-15 03:46:30 -0400267 if (numvtaps == 1)
268 goto single;
269
Krishna Kumaref0002b2011-11-23 22:17:14 +0000270 /* Check if we can use flow to select a queue */
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800271 rxq = skb_get_hash(skb);
Krishna Kumaref0002b2011-11-23 22:17:14 +0000272 if (rxq) {
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800273 queue = rcu_dereference(tap->taps[rxq % numvtaps]);
Jason Wang376b1aa2013-06-05 23:54:38 +0000274 goto out;
Krishna Kumaref0002b2011-11-23 22:17:14 +0000275 }
276
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000277 if (likely(skb_rx_queue_recorded(skb))) {
278 rxq = skb_get_rx_queue(skb);
279
280 while (unlikely(rxq >= numvtaps))
281 rxq -= numvtaps;
282
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800283 queue = rcu_dereference(tap->taps[rxq]);
Jason Wang376b1aa2013-06-05 23:54:38 +0000284 goto out;
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000285 }
286
Jason Wang1b16bf42016-07-15 03:46:30 -0400287single:
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800288 queue = rcu_dereference(tap->taps[0]);
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000289out:
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800290 return queue;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000291}
292
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000293/*
294 * The net_device is going away, give up the reference
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000295 * that it holds on all queues and safely set the pointer
296 * from the queues to NULL.
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000297 */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800298void tap_del_queues(struct tap_dev *tap)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000299{
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800300 struct tap_queue *q, *tmp;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000301
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400302 ASSERT_RTNL();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800303 list_for_each_entry_safe(q, tmp, &tap->queue_list, next) {
Jason Wang815f2362013-06-05 23:54:39 +0000304 list_del_init(&q->next);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800305 RCU_INIT_POINTER(q->tap, NULL);
Jason Wang815f2362013-06-05 23:54:39 +0000306 if (q->enabled)
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800307 tap->numvtaps--;
308 tap->numqueues--;
Pankaj Guptadfe816c2015-06-19 19:47:53 +0530309 sock_put(&q->sk);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000310 }
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800311 BUG_ON(tap->numvtaps);
312 BUG_ON(tap->numqueues);
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800313 /* guarantee that any future tap_set_queue will fail */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800314 tap->numvtaps = MAX_TAP_QUEUES;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000315}
Sainath Grandhi9a393b52017-02-10 16:03:51 -0800316EXPORT_SYMBOL_GPL(tap_del_queues);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000317
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800318rx_handler_result_t tap_handle_frame(struct sk_buff **pskb)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000319{
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500320 struct sk_buff *skb = *pskb;
321 struct net_device *dev = skb->dev;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800322 struct tap_dev *tap;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800323 struct tap_queue *q;
Vlad Yasevicha567dd62013-08-16 15:25:00 -0400324 netdev_features_t features = TAP_FEATURES;
325
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800326 tap = tap_dev_get_rcu(dev);
327 if (!tap)
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500328 return RX_HANDLER_PASS;
329
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800330 q = tap_get_queue(tap, skb);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000331 if (!q)
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500332 return RX_HANDLER_PASS;
Herbert Xu8a357472010-07-21 21:44:31 +0000333
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500334 skb_push(skb, ETH_HLEN);
335
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400336 /* Apply the forward feature mask so that we perform segmentation
Vlad Yaseviche5733322013-08-16 15:25:02 -0400337 * according to users wishes. This only works if VNET_HDR is
338 * enabled.
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400339 */
Vlad Yaseviche5733322013-08-16 15:25:02 -0400340 if (q->flags & IFF_VNET_HDR)
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800341 features |= tap->tap_features;
Johannes Berg8b86a612015-04-17 15:45:04 +0200342 if (netif_needs_gso(skb, features)) {
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400343 struct sk_buff *segs = __skb_gso_segment(skb, features, false);
Jason A. Donenfeld5643a552020-01-08 16:59:03 -0500344 struct sk_buff *next;
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400345
346 if (IS_ERR(segs))
347 goto drop;
348
349 if (!segs) {
Jason Wang5990a302018-01-04 11:14:27 +0800350 if (ptr_ring_produce(&q->ring, skb))
Jason Wang362899b2016-07-15 03:46:31 -0400351 goto drop;
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400352 goto wake_up;
353 }
354
Eric Dumazetbe0bd312016-05-06 05:58:21 -0700355 consume_skb(skb);
Jason A. Donenfeld5643a552020-01-08 16:59:03 -0500356 skb_list_walk_safe(segs, skb, next) {
357 skb_mark_not_on_list(skb);
358 if (ptr_ring_produce(&q->ring, skb)) {
359 kfree_skb(skb);
360 kfree_skb_list(next);
Jason Wang362899b2016-07-15 03:46:31 -0400361 break;
362 }
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400363 }
364 } else {
Vlad Yasevichcbdb0422014-04-29 10:09:50 -0400365 /* If we receive a partial checksum and the tap side
366 * doesn't support checksum offload, compute the checksum.
367 * Note: it doesn't matter which checksum feature to
Sainath Grandhia8e04692017-02-10 16:03:46 -0800368 * check, we either support them all or none.
Vlad Yasevichcbdb0422014-04-29 10:09:50 -0400369 */
370 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Tom Herberta1882222015-12-14 11:19:43 -0800371 !(features & NETIF_F_CSUM_MASK) &&
Vlad Yasevichcbdb0422014-04-29 10:09:50 -0400372 skb_checksum_help(skb))
373 goto drop;
Jason Wang5990a302018-01-04 11:14:27 +0800374 if (ptr_ring_produce(&q->ring, skb))
Jason Wang362899b2016-07-15 03:46:31 -0400375 goto drop;
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400376 }
377
378wake_up:
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800379 wake_up_interruptible_poll(sk_sleep(&q->sk), EPOLLIN | EPOLLRDNORM | EPOLLRDBAND);
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500380 return RX_HANDLER_CONSUMED;
Herbert Xu8a357472010-07-21 21:44:31 +0000381
382drop:
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500383 /* Count errors/drops only here, thus don't care about args. */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800384 if (tap->count_rx_dropped)
385 tap->count_rx_dropped(tap);
Herbert Xu8a357472010-07-21 21:44:31 +0000386 kfree_skb(skb);
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500387 return RX_HANDLER_CONSUMED;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000388}
Sainath Grandhi9a393b52017-02-10 16:03:51 -0800389EXPORT_SYMBOL_GPL(tap_handle_frame);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000390
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800391static struct major_info *tap_get_major(int major)
392{
393 struct major_info *tap_major;
394
395 list_for_each_entry_rcu(tap_major, &major_list, next) {
396 if (tap_major->major == major)
397 return tap_major;
398 }
399
400 return NULL;
401}
402
403int tap_get_minor(dev_t major, struct tap_dev *tap)
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000404{
405 int retval = -ENOMEM;
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800406 struct major_info *tap_major;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000407
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800408 rcu_read_lock();
409 tap_major = tap_get_major(MAJOR(major));
410 if (!tap_major) {
411 retval = -EINVAL;
412 goto unlock;
413 }
414
WANG Congffa423f2017-07-10 10:05:50 -0700415 spin_lock(&tap_major->minor_lock);
416 retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_ATOMIC);
Tejun Heoec09ebc2013-02-27 17:04:34 -0800417 if (retval >= 0) {
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800418 tap->minor = retval;
Tejun Heoec09ebc2013-02-27 17:04:34 -0800419 } else if (retval == -ENOSPC) {
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800420 netdev_err(tap->dev, "Too many tap devices\n");
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000421 retval = -EINVAL;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000422 }
WANG Congffa423f2017-07-10 10:05:50 -0700423 spin_unlock(&tap_major->minor_lock);
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800424
425unlock:
426 rcu_read_unlock();
Tejun Heoec09ebc2013-02-27 17:04:34 -0800427 return retval < 0 ? retval : 0;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000428}
Sainath Grandhi9a393b52017-02-10 16:03:51 -0800429EXPORT_SYMBOL_GPL(tap_get_minor);
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000430
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800431void tap_free_minor(dev_t major, struct tap_dev *tap)
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000432{
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800433 struct major_info *tap_major;
434
435 rcu_read_lock();
436 tap_major = tap_get_major(MAJOR(major));
437 if (!tap_major) {
438 goto unlock;
439 }
440
WANG Congffa423f2017-07-10 10:05:50 -0700441 spin_lock(&tap_major->minor_lock);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800442 if (tap->minor) {
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800443 idr_remove(&tap_major->minor_idr, tap->minor);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800444 tap->minor = 0;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000445 }
WANG Congffa423f2017-07-10 10:05:50 -0700446 spin_unlock(&tap_major->minor_lock);
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800447
448unlock:
449 rcu_read_unlock();
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000450}
Sainath Grandhi9a393b52017-02-10 16:03:51 -0800451EXPORT_SYMBOL_GPL(tap_free_minor);
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000452
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800453static struct tap_dev *dev_get_by_tap_file(int major, int minor)
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000454{
455 struct net_device *dev = NULL;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800456 struct tap_dev *tap;
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800457 struct major_info *tap_major;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000458
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800459 rcu_read_lock();
460 tap_major = tap_get_major(major);
461 if (!tap_major) {
462 tap = NULL;
463 goto unlock;
464 }
465
WANG Congffa423f2017-07-10 10:05:50 -0700466 spin_lock(&tap_major->minor_lock);
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800467 tap = idr_find(&tap_major->minor_idr, minor);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800468 if (tap) {
469 dev = tap->dev;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000470 dev_hold(dev);
471 }
WANG Congffa423f2017-07-10 10:05:50 -0700472 spin_unlock(&tap_major->minor_lock);
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800473
474unlock:
475 rcu_read_unlock();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800476 return tap;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000477}
478
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800479static void tap_sock_write_space(struct sock *sk)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000480{
Eric Dumazet43815482010-04-29 11:01:49 +0000481 wait_queue_head_t *wqueue;
482
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000483 if (!sock_writeable(sk) ||
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800484 !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000485 return;
486
Eric Dumazet43815482010-04-29 11:01:49 +0000487 wqueue = sk_sleep(sk);
488 if (wqueue && waitqueue_active(wqueue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800489 wake_up_interruptible_poll(wqueue, EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000490}
491
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800492static void tap_sock_destruct(struct sock *sk)
Eric W. Biederman2259fef2011-10-20 04:27:24 +0000493{
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800494 struct tap_queue *q = container_of(sk, struct tap_queue, sk);
Jason Wang362899b2016-07-15 03:46:31 -0400495
Jason Wang5990a302018-01-04 11:14:27 +0800496 ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
Eric W. Biederman2259fef2011-10-20 04:27:24 +0000497}
498
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800499static int tap_open(struct inode *inode, struct file *file)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000500{
501 struct net *net = current->nsproxy->net_ns;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800502 struct tap_dev *tap;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800503 struct tap_queue *q;
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400504 int err = -ENODEV;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000505
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400506 rtnl_lock();
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800507 tap = dev_get_by_tap_file(imajor(inode), iminor(inode));
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800508 if (!tap)
Jason Wang362899b2016-07-15 03:46:31 -0400509 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000510
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000511 err = -ENOMEM;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800512 q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
513 &tap_proto, 0);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000514 if (!q)
Jason Wang362899b2016-07-15 03:46:31 -0400515 goto err;
Jason Wang5990a302018-01-04 11:14:27 +0800516 if (ptr_ring_init(&q->ring, tap->dev->tx_queue_len, GFP_KERNEL)) {
Girish Moodalbail78e0ea62017-10-25 00:23:04 -0700517 sk_free(&q->sk);
518 goto err;
519 }
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000520
Al Viro333f7902019-07-05 20:14:16 +0100521 init_waitqueue_head(&q->sock.wq.wait);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000522 q->sock.type = SOCK_RAW;
523 q->sock.state = SS_CONNECTED;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000524 q->sock.file = file;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800525 q->sock.ops = &tap_socket_ops;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000526 sock_init_data(&q->sock, &q->sk);
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800527 q->sk.sk_write_space = tap_sock_write_space;
528 q->sk.sk_destruct = tap_sock_destruct;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000529 q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300530 q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000531
Shirley Ma97bc3632011-07-06 12:26:11 +0000532 /*
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800533 * so far only KVM virtio_net uses tap, enable zero copy between
Shirley Ma97bc3632011-07-06 12:26:11 +0000534 * guest kernel and host kernel when lower device supports zerocopy
Eric W. Biederman047af9cf2011-10-20 04:26:39 +0000535 *
536 * The macvlan supports zerocopy iff the lower device supports zero
537 * copy so we don't have to look at the lower device directly.
Shirley Ma97bc3632011-07-06 12:26:11 +0000538 */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800539 if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
Eric W. Biederman047af9cf2011-10-20 04:26:39 +0000540 sock_set_flag(&q->sk, SOCK_ZEROCOPY);
Shirley Ma97bc3632011-07-06 12:26:11 +0000541
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800542 err = tap_set_queue(tap, file, q);
Girish Moodalbail78e0ea62017-10-25 00:23:04 -0700543 if (err) {
Jason Wang5990a302018-01-04 11:14:27 +0800544 /* tap_sock_destruct() will take care of freeing ptr_ring */
Girish Moodalbail78e0ea62017-10-25 00:23:04 -0700545 goto err_put;
546 }
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000547
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800548 dev_put(tap->dev);
Jason Wang362899b2016-07-15 03:46:31 -0400549
550 rtnl_unlock();
551 return err;
552
Girish Moodalbail78e0ea62017-10-25 00:23:04 -0700553err_put:
Jason Wang362899b2016-07-15 03:46:31 -0400554 sock_put(&q->sk);
555err:
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800556 if (tap)
557 dev_put(tap->dev);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000558
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400559 rtnl_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000560 return err;
561}
562
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800563static int tap_release(struct inode *inode, struct file *file)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000564{
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800565 struct tap_queue *q = file->private_data;
566 tap_put_queue(q);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000567 return 0;
568}
569
Al Viroafc9a422017-07-03 06:39:46 -0400570static __poll_t tap_poll(struct file *file, poll_table *wait)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000571{
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800572 struct tap_queue *q = file->private_data;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800573 __poll_t mask = EPOLLERR;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000574
575 if (!q)
576 goto out;
577
578 mask = 0;
Al Viro333f7902019-07-05 20:14:16 +0100579 poll_wait(file, &q->sock.wq.wait, wait);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000580
Jason Wang5990a302018-01-04 11:14:27 +0800581 if (!ptr_ring_empty(&q->ring))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800582 mask |= EPOLLIN | EPOLLRDNORM;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000583
584 if (sock_writeable(&q->sk) ||
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800585 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000586 sock_writeable(&q->sk)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800587 mask |= EPOLLOUT | EPOLLWRNORM;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000588
589out:
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000590 return mask;
591}
592
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800593static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
594 size_t len, size_t linear,
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000595 int noblock, int *err)
596{
597 struct sk_buff *skb;
598
599 /* Under a page? Don't bother with paged skb. */
600 if (prepad + len < PAGE_SIZE || !linear)
601 linear = len;
602
603 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -0700604 err, 0);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000605 if (!skb)
606 return NULL;
607
608 skb_reserve(skb, prepad);
609 skb_put(skb, linear);
610 skb->data_len = len - linear;
611 skb->len += len - linear;
612
613 return skb;
614}
615
Eric Dumazet2f1d8b92015-02-27 18:35:35 -0800616/* Neighbour code has some assumptions on HH_DATA_MOD alignment */
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800617#define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
Eric Dumazet2f1d8b92015-02-27 18:35:35 -0800618
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000619/* Get packet from user space buffer */
Jason Wangfe8dd452018-09-12 11:17:06 +0800620static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800621 struct iov_iter *from, int noblock)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000622{
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800623 int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000624 struct sk_buff *skb;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800625 struct tap_dev *tap;
Al Virof5ff53b2014-06-19 15:36:49 -0400626 unsigned long total_len = iov_iter_count(from);
Shirley Ma97bc3632011-07-06 12:26:11 +0000627 unsigned long len = total_len;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000628 int err;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000629 struct virtio_net_hdr vnet_hdr = { 0 };
630 int vnet_hdr_len = 0;
Jason Wangb92946e2012-05-02 11:42:15 +0800631 int copylen = 0;
Ivan Vecerac5c62f12015-07-23 16:37:43 +0200632 int depth;
Shirley Ma97bc3632011-07-06 12:26:11 +0000633 bool zerocopy = false;
Jason Wang61d46bf2013-07-10 13:43:28 +0800634 size_t linear;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000635
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000636 if (q->flags & IFF_VNET_HDR) {
Willem de Bruijn837585a2017-02-03 18:20:49 -0500637 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000638
639 err = -EINVAL;
Nicolas Kaiserce3c8692011-03-04 13:49:41 +0000640 if (len < vnet_hdr_len)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000641 goto err;
Nicolas Kaiserce3c8692011-03-04 13:49:41 +0000642 len -= vnet_hdr_len;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000643
Al Virof5ff53b2014-06-19 15:36:49 -0400644 err = -EFAULT;
Al Virocbbd26b2016-11-01 22:09:04 -0400645 if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000646 goto err;
Al Virof5ff53b2014-06-19 15:36:49 -0400647 iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000648 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800649 tap16_to_cpu(q, vnet_hdr.csum_start) +
650 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
651 tap16_to_cpu(q, vnet_hdr.hdr_len))
652 vnet_hdr.hdr_len = cpu_to_tap16(q,
653 tap16_to_cpu(q, vnet_hdr.csum_start) +
654 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000655 err = -EINVAL;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800656 if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000657 goto err;
658 }
659
660 err = -EINVAL;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000661 if (unlikely(len < ETH_HLEN))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000662 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000663
Jason Wangfe8dd452018-09-12 11:17:06 +0800664 if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
Al Virof5ff53b2014-06-19 15:36:49 -0400665 struct iov_iter i;
666
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200667 copylen = vnet_hdr.hdr_len ?
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800668 tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
Jason Wang16a3fa22013-11-13 14:00:40 +0800669 if (copylen > good_linear)
670 copylen = good_linear;
Willem de Bruijn8e2ad412016-03-08 15:18:54 -0500671 else if (copylen < ETH_HLEN)
672 copylen = ETH_HLEN;
Jason Wang61d46bf2013-07-10 13:43:28 +0800673 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -0400674 i = *from;
675 iov_iter_advance(&i, copylen);
676 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wangece793f2013-07-18 10:55:16 +0800677 zerocopy = true;
678 }
679
680 if (!zerocopy) {
Shirley Ma97bc3632011-07-06 12:26:11 +0000681 copylen = len;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800682 linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
Willem de Bruijn8e2ad412016-03-08 15:18:54 -0500683 if (linear > good_linear)
Jason Wang16a3fa22013-11-13 14:00:40 +0800684 linear = good_linear;
Willem de Bruijn8e2ad412016-03-08 15:18:54 -0500685 else if (linear < ETH_HLEN)
686 linear = ETH_HLEN;
Jason Wang61d46bf2013-07-10 13:43:28 +0800687 }
Shirley Ma97bc3632011-07-06 12:26:11 +0000688
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800689 skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
690 linear, noblock, &err);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000691 if (!skb)
692 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000693
Jason Wang01d66572012-05-02 11:42:06 +0800694 if (zerocopy)
Al Virof5ff53b2014-06-19 15:36:49 -0400695 err = zerocopy_sg_from_iter(skb, from);
Jason Wangaa196ee2016-11-30 13:17:52 +0800696 else
Al Virof5ff53b2014-06-19 15:36:49 -0400697 err = skb_copy_datagram_from_iter(skb, 0, from, len);
Jason Wangece793f2013-07-18 10:55:16 +0800698
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000699 if (err)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000700 goto err_kfree;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000701
702 skb_set_network_header(skb, ETH_HLEN);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000703 skb_reset_mac_header(skb);
704 skb->protocol = eth_hdr(skb)->h_proto;
705
706 if (vnet_hdr_len) {
Mike Rapoportfd88d682016-06-08 16:09:19 +0300707 err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800708 tap_is_little_endian(q));
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000709 if (err)
710 goto err_kfree;
711 }
712
Maxim Mikityanskiyd2aa1252019-02-21 12:39:57 +0000713 skb_probe_transport_header(skb);
Jason Wang9b4d6692013-03-25 20:19:55 +0000714
Ivan Vecerac5c62f12015-07-23 16:37:43 +0200715 /* Move network header to the right position for VLAN tagged packets */
716 if ((skb->protocol == htons(ETH_P_8021Q) ||
717 skb->protocol == htons(ETH_P_8021AD)) &&
718 __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
719 skb_set_network_header(skb, depth);
720
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400721 rcu_read_lock();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800722 tap = rcu_dereference(q->tap);
Shirley Ma97bc3632011-07-06 12:26:11 +0000723 /* copy skb_ubuf_info for callback when skb has no error */
Jason Wang01d66572012-05-02 11:42:06 +0800724 if (zerocopy) {
Jason Wangfe8dd452018-09-12 11:17:06 +0800725 skb_shinfo(skb)->destructor_arg = msg_control;
Jason Wang01d66572012-05-02 11:42:06 +0800726 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000727 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangfe8dd452018-09-12 11:17:06 +0800728 } else if (msg_control) {
729 struct ubuf_info *uarg = msg_control;
Jason Wangaa196ee2016-11-30 13:17:52 +0800730 uarg->callback(uarg, false);
Jason Wang01d66572012-05-02 11:42:06 +0800731 }
Jason Wangaa196ee2016-11-30 13:17:52 +0800732
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800733 if (tap) {
734 skb->dev = tap->dev;
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500735 dev_queue_xmit(skb);
Eric Dumazet29d79192013-08-08 08:06:14 -0700736 } else {
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000737 kfree_skb(skb);
Eric Dumazet29d79192013-08-08 08:06:14 -0700738 }
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400739 rcu_read_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000740
Shirley Ma97bc3632011-07-06 12:26:11 +0000741 return total_len;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000742
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000743err_kfree:
744 kfree_skb(skb);
745
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000746err:
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400747 rcu_read_lock();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800748 tap = rcu_dereference(q->tap);
749 if (tap && tap->count_tx_dropped)
750 tap->count_tx_dropped(tap);
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400751 rcu_read_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000752
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000753 return err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000754}
755
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800756static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000757{
758 struct file *file = iocb->ki_filp;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800759 struct tap_queue *q = file->private_data;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000760
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800761 return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000762}
763
764/* Put packet to the user space buffer */
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800765static ssize_t tap_put_user(struct tap_queue *q,
766 const struct sk_buff *skb,
767 struct iov_iter *iter)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000768{
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000769 int ret;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000770 int vnet_hdr_len = 0;
Basil Gorf09e2242012-05-03 22:55:24 +0000771 int vlan_offset = 0;
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800772 int total;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000773
774 if (q->flags & IFF_VNET_HDR) {
Willem de Bruijnfd3a8862018-06-06 11:23:01 -0400775 int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000776 struct virtio_net_hdr vnet_hdr;
Willem de Bruijnfd3a8862018-06-06 11:23:01 -0400777
Willem de Bruijn837585a2017-02-03 18:20:49 -0500778 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800779 if (iov_iter_count(iter) < vnet_hdr_len)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000780 return -EINVAL;
781
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -0800782 if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -0400783 tap_is_little_endian(q), true,
784 vlan_hlen))
Mike Rapoportfd88d682016-06-08 16:09:19 +0300785 BUG();
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000786
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800787 if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
788 sizeof(vnet_hdr))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000789 return -EFAULT;
Jason Wang7cc76f52014-11-20 16:31:05 +0800790
791 iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000792 }
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800793 total = vnet_hdr_len;
Jason Wangce232ce2013-12-11 13:08:34 +0800794 total += skb->len;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000795
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100796 if (skb_vlan_tag_present(skb)) {
Basil Gorf09e2242012-05-03 22:55:24 +0000797 struct {
798 __be16 h_vlan_proto;
799 __be16 h_vlan_TCI;
800 } veth;
Jason Wang0fbe0d42013-07-16 13:36:34 +0800801 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100802 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000803
Basil Gorf09e2242012-05-03 22:55:24 +0000804 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wangce232ce2013-12-11 13:08:34 +0800805 total += VLAN_HLEN;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000806
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800807 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
808 if (ret || !iov_iter_count(iter))
Basil Gorf09e2242012-05-03 22:55:24 +0000809 goto done;
810
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800811 ret = copy_to_iter(&veth, sizeof(veth), iter);
812 if (ret != sizeof(veth) || !iov_iter_count(iter))
Basil Gorf09e2242012-05-03 22:55:24 +0000813 goto done;
814 }
815
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800816 ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
817 skb->len - vlan_offset);
Basil Gorf09e2242012-05-03 22:55:24 +0000818
819done:
Jason Wangce232ce2013-12-11 13:08:34 +0800820 return ret ? ret : total;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000821}
822
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800823static ssize_t tap_do_read(struct tap_queue *q,
824 struct iov_iter *to,
Jason Wang3b4ba042017-05-17 12:14:44 +0800825 int noblock, struct sk_buff *skb)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000826{
Hong zhi guoccf7e722012-06-06 22:36:27 +0000827 DEFINE_WAIT(wait);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000828 ssize_t ret = 0;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000829
Wei Xu61d78532017-12-01 05:10:38 -0500830 if (!iov_iter_count(to)) {
zhong jiang144a6ad2018-09-20 17:37:41 +0800831 kfree_skb(skb);
Al Viro3af0bfe2014-11-07 14:13:53 -0500832 return 0;
Wei Xu61d78532017-12-01 05:10:38 -0500833 }
Al Viro3af0bfe2014-11-07 14:13:53 -0500834
Jason Wang3b4ba042017-05-17 12:14:44 +0800835 if (skb)
836 goto put;
837
Al Viro3af0bfe2014-11-07 14:13:53 -0500838 while (1) {
Jason Wang89cee912013-06-05 23:54:34 +0000839 if (!noblock)
840 prepare_to_wait(sk_sleep(&q->sk), &wait,
841 TASK_INTERRUPTIBLE);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000842
843 /* Read frames from the queue */
Jason Wang5990a302018-01-04 11:14:27 +0800844 skb = ptr_ring_consume(&q->ring);
Al Viro3af0bfe2014-11-07 14:13:53 -0500845 if (skb)
846 break;
847 if (noblock) {
848 ret = -EAGAIN;
849 break;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000850 }
Al Viro3af0bfe2014-11-07 14:13:53 -0500851 if (signal_pending(current)) {
852 ret = -ERESTARTSYS;
853 break;
854 }
855 /* Nothing to read, let's sleep */
856 schedule();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000857 }
Vlad Yasevicha499a2e2015-11-09 09:14:17 -0500858 if (!noblock)
859 finish_wait(sk_sleep(&q->sk), &wait);
860
Jason Wang3b4ba042017-05-17 12:14:44 +0800861put:
Al Viro3af0bfe2014-11-07 14:13:53 -0500862 if (skb) {
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800863 ret = tap_put_user(q, skb, to);
Jason Wangf51a5e82014-12-01 16:53:15 +0800864 if (unlikely(ret < 0))
865 kfree_skb(skb);
866 else
867 consume_skb(skb);
Al Viro3af0bfe2014-11-07 14:13:53 -0500868 }
Arnd Bergmann501c7742010-02-18 05:46:50 +0000869 return ret;
870}
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000871
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800872static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
Arnd Bergmann501c7742010-02-18 05:46:50 +0000873{
874 struct file *file = iocb->ki_filp;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800875 struct tap_queue *q = file->private_data;
Al Viro3af0bfe2014-11-07 14:13:53 -0500876 ssize_t len = iov_iter_count(to), ret;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000877
Jason Wang3b4ba042017-05-17 12:14:44 +0800878 ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL);
Jason Wangce232ce2013-12-11 13:08:34 +0800879 ret = min_t(ssize_t, ret, len);
Zhi Yong Wue6ebc7f2013-12-06 14:16:50 +0800880 if (ret > 0)
881 iocb->ki_pos = ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000882 return ret;
883}
884
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800885static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
Jason Wang8f475a32013-06-05 23:54:36 +0000886{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800887 struct tap_dev *tap;
Jason Wang8f475a32013-06-05 23:54:36 +0000888
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400889 ASSERT_RTNL();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800890 tap = rtnl_dereference(q->tap);
891 if (tap)
892 dev_hold(tap->dev);
Jason Wang8f475a32013-06-05 23:54:36 +0000893
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800894 return tap;
Jason Wang8f475a32013-06-05 23:54:36 +0000895}
896
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800897static void tap_put_tap_dev(struct tap_dev *tap)
Jason Wang8f475a32013-06-05 23:54:36 +0000898{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800899 dev_put(tap->dev);
Jason Wang8f475a32013-06-05 23:54:36 +0000900}
901
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800902static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
Jason Wang815f2362013-06-05 23:54:39 +0000903{
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800904 struct tap_queue *q = file->private_data;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800905 struct tap_dev *tap;
Jason Wang815f2362013-06-05 23:54:39 +0000906 int ret;
907
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800908 tap = tap_get_tap_dev(q);
909 if (!tap)
Jason Wang815f2362013-06-05 23:54:39 +0000910 return -EINVAL;
911
912 if (flags & IFF_ATTACH_QUEUE)
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800913 ret = tap_enable_queue(tap, file, q);
Jason Wang815f2362013-06-05 23:54:39 +0000914 else if (flags & IFF_DETACH_QUEUE)
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800915 ret = tap_disable_queue(q);
Jason Wangf57855a2013-06-13 14:23:36 +0800916 else
917 ret = -EINVAL;
Jason Wang815f2362013-06-05 23:54:39 +0000918
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800919 tap_put_tap_dev(tap);
Jason Wang815f2362013-06-05 23:54:39 +0000920 return ret;
921}
922
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800923static int set_offload(struct tap_queue *q, unsigned long arg)
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400924{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800925 struct tap_dev *tap;
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400926 netdev_features_t features;
927 netdev_features_t feature_mask = 0;
928
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800929 tap = rtnl_dereference(q->tap);
930 if (!tap)
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400931 return -ENOLINK;
932
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800933 features = tap->dev->features;
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400934
935 if (arg & TUN_F_CSUM) {
936 feature_mask = NETIF_F_HW_CSUM;
937
938 if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
939 if (arg & TUN_F_TSO_ECN)
940 feature_mask |= NETIF_F_TSO_ECN;
941 if (arg & TUN_F_TSO4)
942 feature_mask |= NETIF_F_TSO;
943 if (arg & TUN_F_TSO6)
944 feature_mask |= NETIF_F_TSO6;
945 }
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400946 }
947
948 /* tun/tap driver inverts the usage for TSO offloads, where
949 * setting the TSO bit means that the userspace wants to
950 * accept TSO frames and turning it off means that user space
951 * does not support TSO.
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800952 * For tap, we have to invert it to mean the same thing.
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400953 * When user space turns off TSO, we turn off GSO/LRO so that
954 * user-space will not receive TSO frames.
955 */
David S. Millerd591a1f2017-07-03 06:35:32 -0700956 if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6))
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400957 features |= RX_OFFLOADS;
958 else
959 features &= ~RX_OFFLOADS;
960
961 /* tap_features are the same as features on tun/tap and
962 * reflect user expectations.
963 */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800964 tap->tap_features = feature_mask;
965 if (tap->update_features)
966 tap->update_features(tap, features);
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400967
968 return 0;
969}
970
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000971/*
972 * provide compatibility with generic tun/tap interface
973 */
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800974static long tap_ioctl(struct file *file, unsigned int cmd,
975 unsigned long arg)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000976{
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800977 struct tap_queue *q = file->private_data;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800978 struct tap_dev *tap;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000979 void __user *argp = (void __user *)arg;
980 struct ifreq __user *ifr = argp;
981 unsigned int __user *up = argp;
Michael S. Tsirkin39ec7de2014-12-16 15:04:56 +0200982 unsigned short u;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300983 int __user *sp = argp;
Justin Cormack7f460d32015-05-13 19:19:02 +0100984 struct sockaddr sa;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300985 int s;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000986 int ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000987
988 switch (cmd) {
989 case TUNSETIFF:
990 /* ignore the name, just look at flags */
991 if (get_user(u, &ifr->ifr_flags))
992 return -EFAULT;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000993
994 ret = 0;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800995 if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000996 ret = -EINVAL;
997 else
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800998 q->flags = (q->flags & ~TAP_IFFEATURES) | u;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000999
1000 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001001
1002 case TUNGETIFF:
Vlad Yasevich441ac0f2013-06-25 16:04:19 -04001003 rtnl_lock();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001004 tap = tap_get_tap_dev(q);
1005 if (!tap) {
Vlad Yasevich441ac0f2013-06-25 16:04:19 -04001006 rtnl_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +00001007 return -ENOLINK;
Vlad Yasevich441ac0f2013-06-25 16:04:19 -04001008 }
Arnd Bergmann02df55d2010-02-18 05:45:36 +00001009
1010 ret = 0;
Michael S. Tsirkin39ec7de2014-12-16 15:04:56 +02001011 u = q->flags;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001012 if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
Michael S. Tsirkin39ec7de2014-12-16 15:04:56 +02001013 put_user(u, &ifr->ifr_flags))
Arnd Bergmann02df55d2010-02-18 05:45:36 +00001014 ret = -EFAULT;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001015 tap_put_tap_dev(tap);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -04001016 rtnl_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +00001017 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001018
Jason Wang815f2362013-06-05 23:54:39 +00001019 case TUNSETQUEUE:
1020 if (get_user(u, &ifr->ifr_flags))
1021 return -EFAULT;
Vlad Yasevich441ac0f2013-06-25 16:04:19 -04001022 rtnl_lock();
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001023 ret = tap_ioctl_set_queue(file, u);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -04001024 rtnl_unlock();
Jason Wang82a19eb2013-07-16 13:36:33 +08001025 return ret;
Jason Wang815f2362013-06-05 23:54:39 +00001026
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001027 case TUNGETFEATURES:
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001028 if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001029 return -EFAULT;
1030 return 0;
1031
1032 case TUNSETSNDBUF:
Michael S. Tsirkin3ea79242015-09-18 13:41:09 +03001033 if (get_user(s, sp))
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001034 return -EFAULT;
Craig Gallek931619222017-10-30 18:50:11 -04001035 if (s <= 0)
1036 return -EINVAL;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001037
Michael S. Tsirkin3ea79242015-09-18 13:41:09 +03001038 q->sk.sk_sndbuf = s;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001039 return 0;
1040
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +03001041 case TUNGETVNETHDRSZ:
1042 s = q->vnet_hdr_sz;
1043 if (put_user(s, sp))
1044 return -EFAULT;
1045 return 0;
1046
1047 case TUNSETVNETHDRSZ:
1048 if (get_user(s, sp))
1049 return -EFAULT;
1050 if (s < (int)sizeof(struct virtio_net_hdr))
1051 return -EINVAL;
1052
1053 q->vnet_hdr_sz = s;
1054 return 0;
1055
Michael S. Tsirkin01b07fb2014-12-16 15:05:10 +02001056 case TUNGETVNETLE:
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001057 s = !!(q->flags & TAP_VNET_LE);
Michael S. Tsirkin01b07fb2014-12-16 15:05:10 +02001058 if (put_user(s, sp))
1059 return -EFAULT;
1060 return 0;
1061
1062 case TUNSETVNETLE:
1063 if (get_user(s, sp))
1064 return -EFAULT;
1065 if (s)
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001066 q->flags |= TAP_VNET_LE;
Michael S. Tsirkin01b07fb2014-12-16 15:05:10 +02001067 else
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001068 q->flags &= ~TAP_VNET_LE;
Michael S. Tsirkin01b07fb2014-12-16 15:05:10 +02001069 return 0;
1070
Greg Kurz8b8e6582015-04-24 14:50:36 +02001071 case TUNGETVNETBE:
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001072 return tap_get_vnet_be(q, sp);
Greg Kurz8b8e6582015-04-24 14:50:36 +02001073
1074 case TUNSETVNETBE:
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001075 return tap_set_vnet_be(q, sp);
Greg Kurz8b8e6582015-04-24 14:50:36 +02001076
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001077 case TUNSETOFFLOAD:
1078 /* let the user check for future flags */
1079 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05001080 TUN_F_TSO_ECN | TUN_F_UFO))
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001081 return -EINVAL;
1082
Vlad Yasevich2be5c762013-06-25 16:04:21 -04001083 rtnl_lock();
1084 ret = set_offload(q, arg);
1085 rtnl_unlock();
1086 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001087
Justin Cormackb5082082015-05-11 20:00:10 +01001088 case SIOCGIFHWADDR:
1089 rtnl_lock();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001090 tap = tap_get_tap_dev(q);
1091 if (!tap) {
Justin Cormackb5082082015-05-11 20:00:10 +01001092 rtnl_unlock();
1093 return -ENOLINK;
1094 }
1095 ret = 0;
Cong Wang1fc205d2021-02-11 11:34:10 -08001096 dev_get_mac_address(&sa, dev_net(tap->dev), tap->dev->name);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001097 if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
Cong Wang1fc205d2021-02-11 11:34:10 -08001098 copy_to_user(&ifr->ifr_hwaddr, &sa, sizeof(sa)))
Justin Cormackb5082082015-05-11 20:00:10 +01001099 ret = -EFAULT;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001100 tap_put_tap_dev(tap);
Justin Cormackb5082082015-05-11 20:00:10 +01001101 rtnl_unlock();
1102 return ret;
1103
1104 case SIOCSIFHWADDR:
Justin Cormack7f460d32015-05-13 19:19:02 +01001105 if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
1106 return -EFAULT;
Justin Cormackb5082082015-05-11 20:00:10 +01001107 rtnl_lock();
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001108 tap = tap_get_tap_dev(q);
1109 if (!tap) {
Justin Cormackb5082082015-05-11 20:00:10 +01001110 rtnl_unlock();
1111 return -ENOLINK;
1112 }
Cong Wang1fc205d2021-02-11 11:34:10 -08001113 ret = dev_set_mac_address_user(tap->dev, &sa, NULL);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001114 tap_put_tap_dev(tap);
Justin Cormackb5082082015-05-11 20:00:10 +01001115 rtnl_unlock();
1116 return ret;
1117
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001118 default:
1119 return -EINVAL;
1120 }
1121}
1122
Colin Ian Kingd17eb732017-08-12 22:52:31 +01001123static const struct file_operations tap_fops = {
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001124 .owner = THIS_MODULE,
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001125 .open = tap_open,
1126 .release = tap_release,
1127 .read_iter = tap_read_iter,
1128 .write_iter = tap_write_iter,
1129 .poll = tap_poll,
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001130 .llseek = no_llseek,
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001131 .unlocked_ioctl = tap_ioctl,
Arnd Bergmann407e9ef2018-09-11 17:23:00 +02001132 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001133};
1134
Jason Wang0efac272018-09-12 11:17:08 +08001135static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
1136{
1137 struct tun_xdp_hdr *hdr = xdp->data_hard_start;
1138 struct virtio_net_hdr *gso = &hdr->gso;
1139 int buflen = hdr->buflen;
1140 int vnet_hdr_len = 0;
1141 struct tap_dev *tap;
1142 struct sk_buff *skb;
1143 int err, depth;
1144
1145 if (q->flags & IFF_VNET_HDR)
1146 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
1147
1148 skb = build_skb(xdp->data_hard_start, buflen);
1149 if (!skb) {
1150 err = -ENOMEM;
1151 goto err;
1152 }
1153
1154 skb_reserve(skb, xdp->data - xdp->data_hard_start);
1155 skb_put(skb, xdp->data_end - xdp->data);
1156
1157 skb_set_network_header(skb, ETH_HLEN);
1158 skb_reset_mac_header(skb);
1159 skb->protocol = eth_hdr(skb)->h_proto;
1160
1161 if (vnet_hdr_len) {
1162 err = virtio_net_hdr_to_skb(skb, gso, tap_is_little_endian(q));
1163 if (err)
1164 goto err_kfree;
1165 }
1166
Jason Wang0efac272018-09-12 11:17:08 +08001167 /* Move network header to the right position for VLAN tagged packets */
1168 if ((skb->protocol == htons(ETH_P_8021Q) ||
1169 skb->protocol == htons(ETH_P_8021AD)) &&
1170 __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
1171 skb_set_network_header(skb, depth);
1172
1173 rcu_read_lock();
1174 tap = rcu_dereference(q->tap);
1175 if (tap) {
1176 skb->dev = tap->dev;
Maxim Mikityanskiyd2aa1252019-02-21 12:39:57 +00001177 skb_probe_transport_header(skb);
Jason Wang0efac272018-09-12 11:17:08 +08001178 dev_queue_xmit(skb);
1179 } else {
1180 kfree_skb(skb);
1181 }
1182 rcu_read_unlock();
1183
1184 return 0;
1185
1186err_kfree:
1187 kfree_skb(skb);
1188err:
1189 rcu_read_lock();
Colin Ian Kingfaeacb62019-09-27 10:40:39 +01001190 tap = rcu_dereference(q->tap);
Jason Wang0efac272018-09-12 11:17:08 +08001191 if (tap && tap->count_tx_dropped)
1192 tap->count_tx_dropped(tap);
1193 rcu_read_unlock();
1194 return err;
1195}
1196
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001197static int tap_sendmsg(struct socket *sock, struct msghdr *m,
1198 size_t total_len)
Arnd Bergmann501c7742010-02-18 05:46:50 +00001199{
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001200 struct tap_queue *q = container_of(sock, struct tap_queue, sock);
Jason Wangfe8dd452018-09-12 11:17:06 +08001201 struct tun_msg_ctl *ctl = m->msg_control;
Jason Wang0efac272018-09-12 11:17:08 +08001202 struct xdp_buff *xdp;
1203 int i;
Jason Wangfe8dd452018-09-12 11:17:06 +08001204
Jason Wang0efac272018-09-12 11:17:08 +08001205 if (ctl && (ctl->type == TUN_MSG_PTR)) {
1206 for (i = 0; i < ctl->num; i++) {
1207 xdp = &((struct xdp_buff *)ctl->ptr)[i];
1208 tap_get_user_xdp(q, xdp);
1209 }
1210 return 0;
1211 }
Jason Wangfe8dd452018-09-12 11:17:06 +08001212
1213 return tap_get_user(q, ctl ? ctl->ptr : NULL, &m->msg_iter,
1214 m->msg_flags & MSG_DONTWAIT);
Arnd Bergmann501c7742010-02-18 05:46:50 +00001215}
1216
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001217static int tap_recvmsg(struct socket *sock, struct msghdr *m,
1218 size_t total_len, int flags)
Arnd Bergmann501c7742010-02-18 05:46:50 +00001219{
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001220 struct tap_queue *q = container_of(sock, struct tap_queue, sock);
Wei Xu61d78532017-12-01 05:10:38 -05001221 struct sk_buff *skb = m->msg_control;
Arnd Bergmann501c7742010-02-18 05:46:50 +00001222 int ret;
Wei Xu61d78532017-12-01 05:10:38 -05001223 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
zhong jiang144a6ad2018-09-20 17:37:41 +08001224 kfree_skb(skb);
Arnd Bergmann501c7742010-02-18 05:46:50 +00001225 return -EINVAL;
Wei Xu61d78532017-12-01 05:10:38 -05001226 }
1227 ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb);
David S. Millerde2aa472013-12-10 22:06:18 -05001228 if (ret > total_len) {
1229 m->msg_flags |= MSG_TRUNC;
1230 ret = flags & MSG_TRUNC ? ret : total_len;
1231 }
Arnd Bergmann501c7742010-02-18 05:46:50 +00001232 return ret;
1233}
1234
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001235static int tap_peek_len(struct socket *sock)
Jason Wang362899b2016-07-15 03:46:31 -04001236{
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001237 struct tap_queue *q = container_of(sock, struct tap_queue,
Jason Wang362899b2016-07-15 03:46:31 -04001238 sock);
Jason Wang5990a302018-01-04 11:14:27 +08001239 return PTR_RING_PEEK_CALL(&q->ring, __skb_array_len_with_tag);
Jason Wang362899b2016-07-15 03:46:31 -04001240}
1241
Arnd Bergmann501c7742010-02-18 05:46:50 +00001242/* Ops structure to mimic raw sockets with tun */
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001243static const struct proto_ops tap_socket_ops = {
1244 .sendmsg = tap_sendmsg,
1245 .recvmsg = tap_recvmsg,
1246 .peek_len = tap_peek_len,
Arnd Bergmann501c7742010-02-18 05:46:50 +00001247};
1248
1249/* Get an underlying socket object from tun file. Returns error unless file is
1250 * attached to a device. The returned object works like a packet socket, it
1251 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1252 * holding a reference to the file for as long as the socket is in use. */
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001253struct socket *tap_get_socket(struct file *file)
Arnd Bergmann501c7742010-02-18 05:46:50 +00001254{
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001255 struct tap_queue *q;
1256 if (file->f_op != &tap_fops)
Arnd Bergmann501c7742010-02-18 05:46:50 +00001257 return ERR_PTR(-EINVAL);
1258 q = file->private_data;
1259 if (!q)
1260 return ERR_PTR(-EBADFD);
1261 return &q->sock;
1262}
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001263EXPORT_SYMBOL_GPL(tap_get_socket);
Arnd Bergmann501c7742010-02-18 05:46:50 +00001264
Jason Wang5990a302018-01-04 11:14:27 +08001265struct ptr_ring *tap_get_ptr_ring(struct file *file)
Jason Wang49f96fd2017-05-17 12:14:42 +08001266{
1267 struct tap_queue *q;
1268
1269 if (file->f_op != &tap_fops)
1270 return ERR_PTR(-EINVAL);
1271 q = file->private_data;
1272 if (!q)
1273 return ERR_PTR(-EBADFD);
Jason Wang5990a302018-01-04 11:14:27 +08001274 return &q->ring;
Jason Wang49f96fd2017-05-17 12:14:42 +08001275}
Jason Wang5990a302018-01-04 11:14:27 +08001276EXPORT_SYMBOL_GPL(tap_get_ptr_ring);
Jason Wang49f96fd2017-05-17 12:14:42 +08001277
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001278int tap_queue_resize(struct tap_dev *tap)
Jason Wang362899b2016-07-15 03:46:31 -04001279{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001280 struct net_device *dev = tap->dev;
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001281 struct tap_queue *q;
Jason Wang5990a302018-01-04 11:14:27 +08001282 struct ptr_ring **rings;
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001283 int n = tap->numqueues;
Jason Wang362899b2016-07-15 03:46:31 -04001284 int ret, i = 0;
1285
Jason Wang5990a302018-01-04 11:14:27 +08001286 rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
1287 if (!rings)
Jason Wang362899b2016-07-15 03:46:31 -04001288 return -ENOMEM;
1289
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -08001290 list_for_each_entry(q, &tap->queue_list, next)
Jason Wang5990a302018-01-04 11:14:27 +08001291 rings[i++] = &q->ring;
Jason Wang362899b2016-07-15 03:46:31 -04001292
Jason Wang5990a302018-01-04 11:14:27 +08001293 ret = ptr_ring_resize_multiple(rings, n,
1294 dev->tx_queue_len, GFP_KERNEL,
1295 __skb_array_destroy_skb);
Jason Wang362899b2016-07-15 03:46:31 -04001296
Jason Wang5990a302018-01-04 11:14:27 +08001297 kfree(rings);
Jason Wang362899b2016-07-15 03:46:31 -04001298 return ret;
1299}
Sainath Grandhi9a393b52017-02-10 16:03:51 -08001300EXPORT_SYMBOL_GPL(tap_queue_resize);
Sainath Grandhiebc05ba2017-02-10 16:03:48 -08001301
Sainath Grandhid9f1f612017-02-10 16:03:50 -08001302static int tap_list_add(dev_t major, const char *device_name)
1303{
1304 struct major_info *tap_major;
1305
1306 tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
1307 if (!tap_major)
1308 return -ENOMEM;
1309
1310 tap_major->major = MAJOR(major);
1311
1312 idr_init(&tap_major->minor_idr);
WANG Congffa423f2017-07-10 10:05:50 -07001313 spin_lock_init(&tap_major->minor_lock);
Sainath Grandhid9f1f612017-02-10 16:03:50 -08001314
1315 tap_major->device_name = device_name;
1316
1317 list_add_tail_rcu(&tap_major->next, &major_list);
1318 return 0;
1319}
1320
Girish Moodalbaildea6e192017-10-27 00:00:16 -07001321int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
1322 const char *device_name, struct module *module)
Sainath Grandhiebc05ba2017-02-10 16:03:48 -08001323{
1324 int err;
1325
1326 err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
1327 if (err)
1328 goto out1;
1329
1330 cdev_init(tap_cdev, &tap_fops);
Girish Moodalbaildea6e192017-10-27 00:00:16 -07001331 tap_cdev->owner = module;
Sainath Grandhiebc05ba2017-02-10 16:03:48 -08001332 err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
1333 if (err)
1334 goto out2;
1335
Sainath Grandhid9f1f612017-02-10 16:03:50 -08001336 err = tap_list_add(*tap_major, device_name);
1337 if (err)
1338 goto out3;
Sainath Grandhiebc05ba2017-02-10 16:03:48 -08001339
1340 return 0;
1341
Sainath Grandhid9f1f612017-02-10 16:03:50 -08001342out3:
1343 cdev_del(tap_cdev);
Sainath Grandhiebc05ba2017-02-10 16:03:48 -08001344out2:
1345 unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
1346out1:
1347 return err;
1348}
Sainath Grandhi9a393b52017-02-10 16:03:51 -08001349EXPORT_SYMBOL_GPL(tap_create_cdev);
Sainath Grandhiebc05ba2017-02-10 16:03:48 -08001350
1351void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
1352{
Sainath Grandhid9f1f612017-02-10 16:03:50 -08001353 struct major_info *tap_major, *tmp;
1354
Sainath Grandhiebc05ba2017-02-10 16:03:48 -08001355 cdev_del(tap_cdev);
1356 unregister_chrdev_region(major, TAP_NUM_DEVS);
Sainath Grandhid9f1f612017-02-10 16:03:50 -08001357 list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
1358 if (tap_major->major == MAJOR(major)) {
1359 idr_destroy(&tap_major->minor_idr);
1360 list_del_rcu(&tap_major->next);
1361 kfree_rcu(tap_major, rcu);
1362 }
1363 }
Sainath Grandhiebc05ba2017-02-10 16:03:48 -08001364}
Sainath Grandhi9a393b52017-02-10 16:03:51 -08001365EXPORT_SYMBOL_GPL(tap_destroy_cdev);
1366
1367MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
1368MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
1369MODULE_LICENSE("GPL");