blob: f5017121cd57095b275459caf1cb61d66e77c250 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16 */
17
18/*
19 * Changes:
20 *
Mike Kershawff4cc3a2005-09-01 17:40:05 -070021 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * Mark Smith <markzzzsmith@yahoo.com.au>
Joe Perches344dc8e2012-07-12 19:33:09 +000025 * Use eth_random_addr() for tap MAC address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
28 * Fixes in packet dropping, queue length setting and queue wakeup.
29 * Increased default tx queue length.
30 * Added ethtool API.
31 * Minor cleanups
32 *
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
35 */
36
Joe Perches6b8a66e2011-03-02 07:18:10 +000037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define DRV_NAME "tun"
40#define DRV_VERSION "1.6"
41#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
42#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/errno.h>
46#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010047#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/major.h>
49#include <linux/slab.h>
50#include <linux/poll.h>
51#include <linux/fcntl.h>
52#include <linux/init.h>
53#include <linux/skbuff.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/miscdevice.h>
57#include <linux/ethtool.h>
58#include <linux/rtnetlink.h>
Arnd Bergmann50857e22009-11-06 22:52:32 -080059#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/if.h>
61#include <linux/if_arp.h>
62#include <linux/if_ether.h>
63#include <linux/if_tun.h>
Jason Wang6680ec62013-07-25 13:00:33 +080064#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070066#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070067#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000068#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070069#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070070#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080071#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080072#include <net/sock.h>
Masatake YAMATO93e14b62014-01-29 16:43:31 +090073#include <linux/seq_file.h>
Herbert Xue0b46d02014-11-07 21:22:23 +080074#include <linux/uio.h>
Jason Wang1576d982016-06-30 14:45:36 +080075#include <linux/skb_array.h>
Jason Wang761876c2017-08-11 19:41:18 +080076#include <linux/bpf.h>
77#include <linux/bpf_trace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080079#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Rusty Russell14daa022008-04-12 18:48:58 -070081/* Uncomment to enable debugging */
82/* #define TUN_DEBUG 1 */
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#ifdef TUN_DEBUG
85static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070086
Joe Perches6b8a66e2011-03-02 07:18:10 +000087#define tun_debug(level, tun, fmt, args...) \
88do { \
89 if (tun->debug) \
90 netdev_printk(level, tun->dev, fmt, ##args); \
91} while (0)
92#define DBG1(level, fmt, args...) \
93do { \
94 if (debug == 2) \
95 printk(level fmt, ##args); \
96} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070097#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000098#define tun_debug(level, tun, fmt, args...) \
99do { \
100 if (0) \
101 netdev_printk(level, tun->dev, fmt, ##args); \
102} while (0)
103#define DBG1(level, fmt, args...) \
104do { \
105 if (0) \
106 printk(level fmt, ##args); \
107} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#endif
109
Jason Wang761876c2017-08-11 19:41:18 +0800110#define TUN_HEADROOM 256
111#define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD + TUN_HEADROOM)
Jason Wang66ccbc92017-08-11 19:41:16 +0800112
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +0200113/* TUN device flags */
114
115/* IFF_ATTACH_QUEUE is never stored in device flags,
116 * overload it to mean fasync when stored there.
117 */
118#define TUN_FASYNC IFF_ATTACH_QUEUE
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200119/* High bits in flags field are unused. */
120#define TUN_VNET_LE 0x80000000
Greg Kurz8b8e6582015-04-24 14:50:36 +0200121#define TUN_VNET_BE 0x40000000
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +0200122
123#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200124 IFF_MULTI_QUEUE)
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000125#define GOODCOPY_LEN 128
126
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700127#define FLT_EXACT_COUNT 8
128struct tap_filter {
129 unsigned int count; /* Number of addrs. Zero means disabled */
130 u32 mask[2]; /* Mask of the hashed addrs */
131 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
132};
133
Pankaj Guptabaf71c52015-01-12 11:41:29 +0530134/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
135 * to max number of VCPUs in guest. */
136#define MAX_TAP_QUEUES 256
Jason Wangb8732fb2013-01-23 03:59:13 +0000137#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000138
Jason Wang96442e422012-10-31 19:46:02 +0000139#define TUN_FLOW_EXPIRE (3 * HZ)
140
Paolo Abeni608b9972016-04-13 10:52:20 +0200141struct tun_pcpu_stats {
142 u64 rx_packets;
143 u64 rx_bytes;
144 u64 tx_packets;
145 u64 tx_bytes;
146 struct u64_stats_sync syncp;
147 u32 rx_dropped;
148 u32 tx_dropped;
149 u32 rx_frame_errors;
150};
151
Jason Wang54f968d2012-10-31 19:45:57 +0000152/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800153 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000154 * to serve as one transmit queue for tuntap device. The sock_fprog and
155 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000156 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000157 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000158 *
159 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000160 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000161 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000162 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000163struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000164 struct sock sk;
165 struct socket socket;
166 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000167 struct tun_struct __rcu *tun;
Jason Wang54f968d2012-10-31 19:45:57 +0000168 struct fasync_struct *fasync;
169 /* only used for fasnyc */
170 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400171 union {
172 u16 queue_index;
173 unsigned int ifindex;
174 };
Jason Wang4008e972012-12-13 23:53:30 +0000175 struct list_head next;
176 struct tun_struct *detached;
Jason Wang1576d982016-06-30 14:45:36 +0800177 struct skb_array tx_array;
Jason Wang66ccbc92017-08-11 19:41:16 +0800178 struct page_frag alloc_frag;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000179};
180
Jason Wang96442e422012-10-31 19:46:02 +0000181struct tun_flow_entry {
182 struct hlist_node hash_link;
183 struct rcu_head rcu;
184 struct tun_struct *tun;
185
186 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800187 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000188 int queue_index;
189 unsigned long updated;
190};
191
192#define TUN_NUM_FLOW_ENTRIES 1024
193
Jason Wang54f968d2012-10-31 19:45:57 +0000194/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000195 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000196 * file were attached to a persist device.
197 */
Rusty Russell14daa022008-04-12 18:48:58 -0700198struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000199 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
200 unsigned int numqueues;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700201 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800202 kuid_t owner;
203 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700204
Rusty Russell14daa022008-04-12 18:48:58 -0700205 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000206 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000207#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
David S. Millerd591a1f2017-07-03 06:35:32 -0700208 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200209
Paolo Abenieaea34b2016-02-26 10:45:40 +0100210 int align;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200211 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000212 int sndbuf;
213 struct tap_filter txflt;
214 struct sock_fprog fprog;
215 /* protected by rtnl lock */
216 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700217#ifdef TUN_DEBUG
218 int debug;
219#endif
Jason Wang96442e422012-10-31 19:46:02 +0000220 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000221 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
222 struct timer_list flow_gc_timer;
223 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000224 unsigned int numdisabled;
225 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000226 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000227 u32 flow_count;
Jason Wang5503fce2017-01-18 15:02:03 +0800228 u32 rx_batched;
Paolo Abeni608b9972016-04-13 10:52:20 +0200229 struct tun_pcpu_stats __percpu *pcpu_stats;
Jason Wang761876c2017-08-11 19:41:18 +0800230 struct bpf_prog __rcu *xdp_prog;
Rusty Russell14daa022008-04-12 18:48:58 -0700231};
232
Greg Kurz8b8e6582015-04-24 14:50:36 +0200233#ifdef CONFIG_TUN_VNET_CROSS_LE
234static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
235{
236 return tun->flags & TUN_VNET_BE ? false :
237 virtio_legacy_is_little_endian();
238}
239
240static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
241{
242 int be = !!(tun->flags & TUN_VNET_BE);
243
244 if (put_user(be, argp))
245 return -EFAULT;
246
247 return 0;
248}
249
250static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
251{
252 int be;
253
254 if (get_user(be, argp))
255 return -EFAULT;
256
257 if (be)
258 tun->flags |= TUN_VNET_BE;
259 else
260 tun->flags &= ~TUN_VNET_BE;
261
262 return 0;
263}
264#else
265static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
266{
267 return virtio_legacy_is_little_endian();
268}
269
270static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
271{
272 return -EINVAL;
273}
274
275static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
276{
277 return -EINVAL;
278}
279#endif /* CONFIG_TUN_VNET_CROSS_LE */
280
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200281static inline bool tun_is_little_endian(struct tun_struct *tun)
282{
Greg Kurz7d824102015-04-24 14:26:24 +0200283 return tun->flags & TUN_VNET_LE ||
Greg Kurz8b8e6582015-04-24 14:50:36 +0200284 tun_legacy_is_little_endian(tun);
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200285}
286
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300287static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
288{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200289 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300290}
291
292static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
293{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200294 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300295}
296
Jason Wang96442e422012-10-31 19:46:02 +0000297static inline u32 tun_hashfn(u32 rxhash)
298{
299 return rxhash & 0x3ff;
300}
301
302static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
303{
304 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000305
Sasha Levinb67bfe02013-02-27 17:06:00 -0800306 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000307 if (e->rxhash == rxhash)
308 return e;
309 }
310 return NULL;
311}
312
313static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
314 struct hlist_head *head,
315 u32 rxhash, u16 queue_index)
316{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000317 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
318
Jason Wang96442e422012-10-31 19:46:02 +0000319 if (e) {
320 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
321 rxhash, queue_index);
322 e->updated = jiffies;
323 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800324 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000325 e->queue_index = queue_index;
326 e->tun = tun;
327 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000328 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000329 }
330 return e;
331}
332
Jason Wang96442e422012-10-31 19:46:02 +0000333static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
334{
335 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
336 e->rxhash, e->queue_index);
337 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000338 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000339 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000340}
341
342static void tun_flow_flush(struct tun_struct *tun)
343{
344 int i;
345
346 spin_lock_bh(&tun->lock);
347 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
348 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800349 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000350
Sasha Levinb67bfe02013-02-27 17:06:00 -0800351 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000352 tun_flow_delete(tun, e);
353 }
354 spin_unlock_bh(&tun->lock);
355}
356
357static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
358{
359 int i;
360
361 spin_lock_bh(&tun->lock);
362 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
363 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800364 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000365
Sasha Levinb67bfe02013-02-27 17:06:00 -0800366 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000367 if (e->queue_index == queue_index)
368 tun_flow_delete(tun, e);
369 }
370 }
371 spin_unlock_bh(&tun->lock);
372}
373
374static void tun_flow_cleanup(unsigned long data)
375{
376 struct tun_struct *tun = (struct tun_struct *)data;
377 unsigned long delay = tun->ageing_time;
378 unsigned long next_timer = jiffies + delay;
379 unsigned long count = 0;
380 int i;
381
382 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
383
384 spin_lock_bh(&tun->lock);
385 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
386 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800387 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000388
Sasha Levinb67bfe02013-02-27 17:06:00 -0800389 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000390 unsigned long this_timer;
391 count++;
392 this_timer = e->updated + delay;
393 if (time_before_eq(this_timer, jiffies))
394 tun_flow_delete(tun, e);
395 else if (time_before(this_timer, next_timer))
396 next_timer = this_timer;
397 }
398 }
399
400 if (count)
401 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
402 spin_unlock_bh(&tun->lock);
403}
404
Eric Dumazet49974422012-12-12 19:22:57 +0000405static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000406 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000407{
408 struct hlist_head *head;
409 struct tun_flow_entry *e;
410 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000411 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000412
413 if (!rxhash)
414 return;
415 else
416 head = &tun->flows[tun_hashfn(rxhash)];
417
418 rcu_read_lock();
419
Jason Wang9e857222013-01-28 01:05:19 +0000420 /* We may get a very small possibility of OOO during switching, not
421 * worth to optimize.*/
422 if (tun->numqueues == 1 || tfile->detached)
Jason Wang96442e422012-10-31 19:46:02 +0000423 goto unlock;
424
425 e = tun_flow_find(head, rxhash);
426 if (likely(e)) {
427 /* TODO: keep queueing to old queue until it's empty? */
428 e->queue_index = queue_index;
429 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800430 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000431 } else {
432 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000433 if (!tun_flow_find(head, rxhash) &&
434 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000435 tun_flow_create(tun, head, rxhash, queue_index);
436
437 if (!timer_pending(&tun->flow_gc_timer))
438 mod_timer(&tun->flow_gc_timer,
439 round_jiffies_up(jiffies + delay));
440 spin_unlock_bh(&tun->lock);
441 }
442
443unlock:
444 rcu_read_unlock();
445}
446
Tom Herbert9bc88932013-12-22 18:54:32 +0800447/**
448 * Save the hash received in the stack receive path and update the
449 * flow_hash table accordingly.
450 */
451static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
452{
Eric Dumazet567e4b72015-02-06 12:59:01 -0800453 if (unlikely(e->rps_rxhash != hash))
Tom Herbert9bc88932013-12-22 18:54:32 +0800454 e->rps_rxhash = hash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800455}
456
Jason Wangc8d68e62012-10-31 19:46:00 +0000457/* We try to identify a flow through its rxhash first. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800458 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000459 * the rxq based on the txq where the last packet of the flow comes. As
460 * the userspace application move between processors, we may get a
461 * different rxq no. here. If we could not get rxhash, then we would
462 * hope the rxq no. may help here.
463 */
Jason Wangf663dd92014-01-10 16:18:26 +0800464static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100465 void *accel_priv, select_queue_fallback_t fallback)
Jason Wangc8d68e62012-10-31 19:46:00 +0000466{
467 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000468 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000469 u32 txq = 0;
470 u32 numqueues = 0;
471
472 rcu_read_lock();
Jason Wang92bb73e2013-06-05 16:44:57 +0800473 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000474
Jason Wangfeec0842017-06-06 14:09:49 +0800475 txq = __skb_get_hash_symmetric(skb);
Jason Wangc8d68e62012-10-31 19:46:00 +0000476 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000477 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
Tom Herbert9bc88932013-12-22 18:54:32 +0800478 if (e) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800479 tun_flow_save_rps_rxhash(e, txq);
Zhi Yong Wufbe4d452014-01-02 13:24:28 +0800480 txq = e->queue_index;
Tom Herbert9bc88932013-12-22 18:54:32 +0800481 } else
Jason Wang96442e422012-10-31 19:46:02 +0000482 /* use multiply and shift instead of expensive divide */
483 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000484 } else if (likely(skb_rx_queue_recorded(skb))) {
485 txq = skb_get_rx_queue(skb);
486 while (unlikely(txq >= numqueues))
487 txq -= numqueues;
488 }
489
490 rcu_read_unlock();
491 return txq;
492}
493
Jason Wangcde8b152012-10-31 19:46:01 +0000494static inline bool tun_not_capable(struct tun_struct *tun)
495{
496 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000497 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000498
499 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
500 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000501 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000502}
503
Jason Wangc8d68e62012-10-31 19:46:00 +0000504static void tun_set_real_num_queues(struct tun_struct *tun)
505{
506 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
507 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
508}
509
Jason Wang4008e972012-12-13 23:53:30 +0000510static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
511{
512 tfile->detached = tun;
513 list_add_tail(&tfile->next, &tun->disabled);
514 ++tun->numdisabled;
515}
516
Jason Wangd32649d2012-12-18 11:00:27 +0800517static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000518{
519 struct tun_struct *tun = tfile->detached;
520
521 tfile->detached = NULL;
522 list_del_init(&tfile->next);
523 --tun->numdisabled;
524 return tun;
525}
526
Jason Wang4bfb0512013-09-05 17:53:59 +0800527static void tun_queue_purge(struct tun_file *tfile)
528{
Jason Wang1576d982016-06-30 14:45:36 +0800529 struct sk_buff *skb;
530
531 while ((skb = skb_array_consume(&tfile->tx_array)) != NULL)
532 kfree_skb(skb);
533
Jason Wang5503fce2017-01-18 15:02:03 +0800534 skb_queue_purge(&tfile->sk.sk_write_queue);
Jason Wang4bfb0512013-09-05 17:53:59 +0800535 skb_queue_purge(&tfile->sk.sk_error_queue);
536}
537
Jason Wangc8d68e62012-10-31 19:46:00 +0000538static void __tun_detach(struct tun_file *tfile, bool clean)
539{
540 struct tun_file *ntfile;
541 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000542
Jason Wangb8deabd2013-01-11 16:59:32 +0000543 tun = rtnl_dereference(tfile->tun);
544
Jason Wang9e857222013-01-28 01:05:19 +0000545 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000546 u16 index = tfile->queue_index;
547 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000548
549 rcu_assign_pointer(tun->tfiles[index],
550 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000551 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000552 ntfile->queue_index = index;
553
554 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000555 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530556 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000557 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000558 } else
Jason Wang4008e972012-12-13 23:53:30 +0000559 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000560
561 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000562 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000563 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800564 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000565 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000566 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000567 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000568 sock_put(&tfile->sk);
569 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000570
571 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000572 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
573 netif_carrier_off(tun->dev);
574
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200575 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000576 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000577 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000578 }
Jason Wang1576d982016-06-30 14:45:36 +0800579 if (tun)
580 skb_array_cleanup(&tfile->tx_array);
Jason Wang66ccbc92017-08-11 19:41:16 +0800581 if (tfile->alloc_frag.page)
582 put_page(tfile->alloc_frag.page);
Eric W. Biederman140e807da2015-05-08 21:07:08 -0500583 sock_put(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000584 }
585}
586
587static void tun_detach(struct tun_file *tfile, bool clean)
588{
589 rtnl_lock();
590 __tun_detach(tfile, clean);
591 rtnl_unlock();
592}
593
594static void tun_detach_all(struct net_device *dev)
595{
596 struct tun_struct *tun = netdev_priv(dev);
Jason Wang761876c2017-08-11 19:41:18 +0800597 struct bpf_prog *xdp_prog = rtnl_dereference(tun->xdp_prog);
Jason Wang4008e972012-12-13 23:53:30 +0000598 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000599 int i, n = tun->numqueues;
600
601 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000602 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000603 BUG_ON(!tfile);
Jason Wangaddf8fc2016-05-19 13:36:51 +0800604 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700605 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530606 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000607 --tun->numqueues;
608 }
Jason Wang9e857222013-01-28 01:05:19 +0000609 list_for_each_entry(tfile, &tun->disabled, next) {
Jason Wangaddf8fc2016-05-19 13:36:51 +0800610 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700611 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530612 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000613 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000614 BUG_ON(tun->numqueues != 0);
615
616 synchronize_net();
617 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000618 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000619 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800620 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000621 sock_put(&tfile->sk);
622 }
Jason Wang4008e972012-12-13 23:53:30 +0000623 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
624 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800625 tun_queue_purge(tfile);
Jason Wang4008e972012-12-13 23:53:30 +0000626 sock_put(&tfile->sk);
627 }
628 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000629
Jason Wang761876c2017-08-11 19:41:18 +0800630 if (xdp_prog)
631 bpf_prog_put(xdp_prog);
632
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200633 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000634 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000635}
636
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400637static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000638{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000639 struct tun_file *tfile = file->private_data;
Jason Wang1576d982016-06-30 14:45:36 +0800640 struct net_device *dev = tun->dev;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000641 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000642
Paul Moore5dbbaf22013-01-14 07:12:19 +0000643 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
644 if (err < 0)
645 goto out;
646
Eric W. Biederman38231b72009-01-20 11:02:28 +0000647 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000648 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000649 goto out;
650
651 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200652 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000653 goto out;
654
655 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000656 if (!tfile->detached &&
657 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000658 goto out;
659
660 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000661
stephen hemminger92d4ea62013-12-05 20:42:58 -0800662 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400663 if (!skip_filter && (tun->filter_attached == true)) {
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +0200664 lock_sock(tfile->socket.sk);
665 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
666 release_sock(tfile->socket.sk);
Jason Wang54f968d2012-10-31 19:45:57 +0000667 if (!err)
668 goto out;
669 }
Jason Wang1576d982016-06-30 14:45:36 +0800670
671 if (!tfile->detached &&
672 skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) {
673 err = -ENOMEM;
674 goto out;
675 }
676
Jason Wangc8d68e62012-10-31 19:46:00 +0000677 tfile->queue_index = tun->numqueues;
Jason Wangaddf8fc2016-05-19 13:36:51 +0800678 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
Jason Wang6e914fc2012-10-31 19:45:58 +0000679 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000680 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000681 tun->numqueues++;
682
Jason Wang4008e972012-12-13 23:53:30 +0000683 if (tfile->detached)
684 tun_enable_queue(tfile);
685 else
686 sock_hold(&tfile->sk);
687
Jason Wangc8d68e62012-10-31 19:46:00 +0000688 tun_set_real_num_queues(tun);
689
Jason Wangc8d68e62012-10-31 19:46:00 +0000690 /* device is allowed to go away first, so no need to hold extra
691 * refcnt.
692 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000693
Eric W. Biederman38231b72009-01-20 11:02:28 +0000694out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000695 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000696}
697
Eric W. Biederman631ab462009-01-20 11:00:40 +0000698static struct tun_struct *__tun_get(struct tun_file *tfile)
699{
Jason Wang6e914fc2012-10-31 19:45:58 +0000700 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000701
Jason Wang6e914fc2012-10-31 19:45:58 +0000702 rcu_read_lock();
703 tun = rcu_dereference(tfile->tun);
704 if (tun)
705 dev_hold(tun->dev);
706 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000707
708 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000709}
710
711static struct tun_struct *tun_get(struct file *file)
712{
713 return __tun_get(file->private_data);
714}
715
716static void tun_put(struct tun_struct *tun)
717{
Jason Wang6e914fc2012-10-31 19:45:58 +0000718 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000719}
720
Joe Perches6b8a66e2011-03-02 07:18:10 +0000721/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700722static void addr_hash_set(u32 *mask, const u8 *addr)
723{
724 int n = ether_crc(ETH_ALEN, addr) >> 26;
725 mask[n >> 5] |= (1 << (n & 31));
726}
727
728static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
729{
730 int n = ether_crc(ETH_ALEN, addr) >> 26;
731 return mask[n >> 5] & (1 << (n & 31));
732}
733
734static int update_filter(struct tap_filter *filter, void __user *arg)
735{
736 struct { u8 u[ETH_ALEN]; } *addr;
737 struct tun_filter uf;
738 int err, alen, n, nexact;
739
740 if (copy_from_user(&uf, arg, sizeof(uf)))
741 return -EFAULT;
742
743 if (!uf.count) {
744 /* Disabled */
745 filter->count = 0;
746 return 0;
747 }
748
749 alen = ETH_ALEN * uf.count;
Markus Elfring28e81902016-08-20 08:54:15 +0200750 addr = memdup_user(arg + sizeof(uf), alen);
751 if (IS_ERR(addr))
752 return PTR_ERR(addr);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700753
754 /* The filter is updated without holding any locks. Which is
755 * perfectly safe. We disable it first and in the worst
756 * case we'll accept a few undesired packets. */
757 filter->count = 0;
758 wmb();
759
760 /* Use first set of addresses as an exact filter */
761 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
762 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
763
764 nexact = n;
765
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800766 /* Remaining multicast addresses are hashed,
767 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700768 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800769 for (; n < uf.count; n++) {
770 if (!is_multicast_ether_addr(addr[n].u)) {
771 err = 0; /* no filter */
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200772 goto free_addr;
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800773 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700774 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800775 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700776
777 /* For ALLMULTI just set the mask to all ones.
778 * This overrides the mask populated above. */
779 if ((uf.flags & TUN_FLT_ALLMULTI))
780 memset(filter->mask, ~0, sizeof(filter->mask));
781
782 /* Now enable the filter */
783 wmb();
784 filter->count = nexact;
785
786 /* Return the number of exact filters */
787 err = nexact;
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200788free_addr:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700789 kfree(addr);
790 return err;
791}
792
793/* Returns: 0 - drop, !=0 - accept */
794static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
795{
796 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
797 * at this point. */
798 struct ethhdr *eh = (struct ethhdr *) skb->data;
799 int i;
800
801 /* Exact match */
802 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000803 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700804 return 1;
805
806 /* Inexact match (multicast only) */
807 if (is_multicast_ether_addr(eh->h_dest))
808 return addr_hash_test(filter->mask, eh->h_dest);
809
810 return 0;
811}
812
813/*
814 * Checks whether the packet is accepted or not.
815 * Returns: 0 - drop, !=0 - accept
816 */
817static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
818{
819 if (!filter->count)
820 return 1;
821
822 return run_filter(filter, skb);
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/* Network device part of the driver */
826
Jeff Garzik7282d492006-09-13 14:30:00 -0400827static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000829/* Net device detach from fd. */
830static void tun_net_uninit(struct net_device *dev)
831{
Jason Wangc8d68e62012-10-31 19:46:00 +0000832 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/* Net device open. */
836static int tun_net_open(struct net_device *dev)
837{
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +0100838 struct tun_struct *tun = netdev_priv(dev);
839 int i;
840
Jason Wangc8d68e62012-10-31 19:46:00 +0000841 netif_tx_start_all_queues(dev);
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +0100842
843 for (i = 0; i < tun->numqueues; i++) {
844 struct tun_file *tfile;
845
846 tfile = rtnl_dereference(tun->tfiles[i]);
847 tfile->socket.sk->sk_write_space(tfile->socket.sk);
848 }
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return 0;
851}
852
853/* Net device close. */
854static int tun_net_close(struct net_device *dev)
855{
Jason Wangc8d68e62012-10-31 19:46:00 +0000856 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return 0;
858}
859
860/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000861static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000864 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000865 struct tun_file *tfile;
Dominic Curranfa358642014-01-22 03:03:23 +0000866 u32 numqueues = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Jason Wang6e914fc2012-10-31 19:45:58 +0000868 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000869 tfile = rcu_dereference(tun->tfiles[txq]);
Dominic Curranfa358642014-01-22 03:03:23 +0000870 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* Drop packet if interface is not attached */
Dominic Curranfa358642014-01-22 03:03:23 +0000873 if (txq >= numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto drop;
875
Jason Wang3df97ba2016-04-25 23:13:42 -0400876#ifdef CONFIG_RPS
877 if (numqueues == 1 && static_key_false(&rps_needed)) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800878 /* Select queue was not called for the skbuff, so we extract the
879 * RPS hash and save it into the flow_table here.
880 */
881 __u32 rxhash;
882
Jason Wangfeec0842017-06-06 14:09:49 +0800883 rxhash = __skb_get_hash_symmetric(skb);
Tom Herbert9bc88932013-12-22 18:54:32 +0800884 if (rxhash) {
885 struct tun_flow_entry *e;
886 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
887 rxhash);
888 if (e)
889 tun_flow_save_rps_rxhash(e, rxhash);
890 }
891 }
Jason Wang3df97ba2016-04-25 23:13:42 -0400892#endif
Tom Herbert9bc88932013-12-22 18:54:32 +0800893
Jason Wang6e914fc2012-10-31 19:45:58 +0000894 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
895
Jason Wangc8d68e62012-10-31 19:46:00 +0000896 BUG_ON(!tfile);
897
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700898 /* Drop if the filter does not like it.
899 * This is a noop if the filter is disabled.
900 * Filter can be enabled only for the TAP devices. */
901 if (!check_filter(&tun->txflt, skb))
902 goto drop;
903
Jason Wang54f968d2012-10-31 19:45:57 +0000904 if (tfile->socket.sk->sk_filter &&
905 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000906 goto drop;
907
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400908 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Jason Wang7bf66302013-09-05 17:54:00 +0800909 goto drop;
910
Soheil Hassas Yeganeh7b996242016-08-23 18:22:33 -0400911 skb_tx_timestamp(skb);
Richard Cochraneda29772013-07-19 19:40:10 +0200912
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000913 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800914 * for indefinite time.
915 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000916 skb_orphan(skb);
917
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000918 nf_reset(skb);
919
Jason Wang1576d982016-06-30 14:45:36 +0800920 if (skb_array_produce(&tfile->tx_array, skb))
921 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000924 if (tfile->flags & TUN_FASYNC)
925 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -0700926 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +0000927
928 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000929 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931drop:
Paolo Abeni608b9972016-04-13 10:52:20 +0200932 this_cpu_inc(tun->pcpu_stats->tx_dropped);
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000933 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000935 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +0800936 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700939static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700941 /*
942 * This callback is supposed to deal with mc filter in
943 * _rx_ path and has nothing to do with the _tx_ path.
944 * In rx path we always accept everything userspace gives us.
945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000948static netdev_features_t tun_net_fix_features(struct net_device *dev,
949 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000950{
951 struct tun_struct *tun = netdev_priv(dev);
952
953 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
954}
Neil Hormanbebd0972011-06-15 05:25:01 +0000955#ifdef CONFIG_NET_POLL_CONTROLLER
956static void tun_poll_controller(struct net_device *dev)
957{
958 /*
959 * Tun only receives frames when:
960 * 1) the char device endpoint gets data from user space
961 * 2) the tun socket gets a sendmsg call from user space
stephen hemminger92d4ea62013-12-05 20:42:58 -0800962 * Since both of those are synchronous operations, we are guaranteed
Neil Hormanbebd0972011-06-15 05:25:01 +0000963 * never to have pending data when we poll for it
stephen hemminger92d4ea62013-12-05 20:42:58 -0800964 * so there is nothing to do here but return.
Neil Hormanbebd0972011-06-15 05:25:01 +0000965 * We need this though so netpoll recognizes us as an interface that
966 * supports polling, which enables bridge devices in virt setups to
967 * still use netconsole
968 */
969 return;
970}
971#endif
Paolo Abenieaea34b2016-02-26 10:45:40 +0100972
973static void tun_set_headroom(struct net_device *dev, int new_hr)
974{
975 struct tun_struct *tun = netdev_priv(dev);
976
977 if (new_hr < NET_SKB_PAD)
978 new_hr = NET_SKB_PAD;
979
980 tun->align = new_hr;
981}
982
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800983static void
Paolo Abeni608b9972016-04-13 10:52:20 +0200984tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
985{
986 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
987 struct tun_struct *tun = netdev_priv(dev);
988 struct tun_pcpu_stats *p;
989 int i;
990
991 for_each_possible_cpu(i) {
992 u64 rxpackets, rxbytes, txpackets, txbytes;
993 unsigned int start;
994
995 p = per_cpu_ptr(tun->pcpu_stats, i);
996 do {
997 start = u64_stats_fetch_begin(&p->syncp);
998 rxpackets = p->rx_packets;
999 rxbytes = p->rx_bytes;
1000 txpackets = p->tx_packets;
1001 txbytes = p->tx_bytes;
1002 } while (u64_stats_fetch_retry(&p->syncp, start));
1003
1004 stats->rx_packets += rxpackets;
1005 stats->rx_bytes += rxbytes;
1006 stats->tx_packets += txpackets;
1007 stats->tx_bytes += txbytes;
1008
1009 /* u32 counters */
1010 rx_dropped += p->rx_dropped;
1011 rx_frame_errors += p->rx_frame_errors;
1012 tx_dropped += p->tx_dropped;
1013 }
1014 stats->rx_dropped = rx_dropped;
1015 stats->rx_frame_errors = rx_frame_errors;
1016 stats->tx_dropped = tx_dropped;
Paolo Abeni608b9972016-04-13 10:52:20 +02001017}
1018
Jason Wang761876c2017-08-11 19:41:18 +08001019static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1020 struct netlink_ext_ack *extack)
1021{
1022 struct tun_struct *tun = netdev_priv(dev);
1023 struct bpf_prog *old_prog;
1024
1025 old_prog = rtnl_dereference(tun->xdp_prog);
1026 rcu_assign_pointer(tun->xdp_prog, prog);
1027 if (old_prog)
1028 bpf_prog_put(old_prog);
1029
1030 return 0;
1031}
1032
1033static u32 tun_xdp_query(struct net_device *dev)
1034{
1035 struct tun_struct *tun = netdev_priv(dev);
1036 const struct bpf_prog *xdp_prog;
1037
1038 xdp_prog = rtnl_dereference(tun->xdp_prog);
1039 if (xdp_prog)
1040 return xdp_prog->aux->id;
1041
1042 return 0;
1043}
1044
1045static int tun_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1046{
1047 switch (xdp->command) {
1048 case XDP_SETUP_PROG:
1049 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1050 case XDP_QUERY_PROG:
1051 xdp->prog_id = tun_xdp_query(dev);
1052 xdp->prog_attached = !!xdp->prog_id;
1053 return 0;
1054 default:
1055 return -EINVAL;
1056 }
1057}
1058
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001059static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001060 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001061 .ndo_open = tun_net_open,
1062 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001063 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001064 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +00001065 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +00001066#ifdef CONFIG_NET_POLL_CONTROLLER
1067 .ndo_poll_controller = tun_poll_controller,
1068#endif
Paolo Abenieaea34b2016-02-26 10:45:40 +01001069 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001070 .ndo_get_stats64 = tun_net_get_stats64,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001071};
1072
1073static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001074 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001075 .ndo_open = tun_net_open,
1076 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001077 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001078 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001079 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001080 .ndo_set_mac_address = eth_mac_addr,
1081 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +00001082 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +00001083#ifdef CONFIG_NET_POLL_CONTROLLER
1084 .ndo_poll_controller = tun_poll_controller,
1085#endif
Toshiaki Makita5e527962015-07-31 15:03:27 +09001086 .ndo_features_check = passthru_features_check,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001087 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001088 .ndo_get_stats64 = tun_net_get_stats64,
Jason Wang761876c2017-08-11 19:41:18 +08001089 .ndo_xdp = tun_xdp,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001090};
1091
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001092static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +00001093{
1094 int i;
1095
Jason Wang96442e422012-10-31 19:46:02 +00001096 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1097 INIT_HLIST_HEAD(&tun->flows[i]);
1098
1099 tun->ageing_time = TUN_FLOW_EXPIRE;
1100 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
1101 mod_timer(&tun->flow_gc_timer,
1102 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +00001103}
1104
1105static void tun_flow_uninit(struct tun_struct *tun)
1106{
1107 del_timer_sync(&tun->flow_gc_timer);
1108 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001109}
1110
Jarod Wilson91572082016-10-20 13:55:20 -04001111#define MIN_MTU 68
1112#define MAX_MTU 65535
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/* Initialize net device. */
1115static void tun_net_init(struct net_device *dev)
1116{
1117 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001120 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001121 dev->netdev_ops = &tun_netdev_ops;
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Point-to-Point TUN Device */
1124 dev->hard_header_len = 0;
1125 dev->addr_len = 0;
1126 dev->mtu = 1500;
1127
1128 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001129 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 break;
1132
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001133 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -08001134 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001136 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +00001137 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +00001138 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001140 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 break;
1143 }
Jarod Wilson91572082016-10-20 13:55:20 -04001144
1145 dev->min_mtu = MIN_MTU;
1146 dev->max_mtu = MAX_MTU - dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149/* Character device part */
1150
1151/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +00001152static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001153{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +00001154 struct tun_file *tfile = file->private_data;
1155 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001156 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001157 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +00001160 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jason Wang54f968d2012-10-31 19:45:57 +00001162 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001163
Joe Perches6b8a66e2011-03-02 07:18:10 +00001164 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Xi Wang9e641bd2014-05-16 15:11:48 -07001166 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001167
Jason Wang1576d982016-06-30 14:45:36 +08001168 if (!skb_array_empty(&tfile->tx_array))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 mask |= POLLIN | POLLRDNORM;
1170
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001171 if (tun->dev->flags & IFF_UP &&
1172 (sock_writeable(sk) ||
1173 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1174 sock_writeable(sk))))
Herbert Xu33dccbb2009-02-05 21:25:32 -08001175 mask |= POLLOUT | POLLWRNORM;
1176
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001177 if (tun->dev->reg_state != NETREG_REGISTERED)
1178 mask = POLLERR;
1179
Eric W. Biederman631ab462009-01-20 11:00:40 +00001180 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return mask;
1182}
1183
Rusty Russellf42157c2008-08-15 15:15:10 -07001184/* prepad is the amount to reserve at front. len is length after that.
1185 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001186static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001187 size_t prepad, size_t len,
1188 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001189{
Jason Wang54f968d2012-10-31 19:45:57 +00001190 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001191 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001192 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001193
1194 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001195 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001196 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001197
Herbert Xu33dccbb2009-02-05 21:25:32 -08001198 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001199 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001200 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001201 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001202
1203 skb_reserve(skb, prepad);
1204 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001205 skb->data_len = len - linear;
1206 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001207
1208 return skb;
1209}
1210
Jason Wang5503fce2017-01-18 15:02:03 +08001211static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1212 struct sk_buff *skb, int more)
1213{
1214 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1215 struct sk_buff_head process_queue;
1216 u32 rx_batched = tun->rx_batched;
1217 bool rcv = false;
1218
1219 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1220 local_bh_disable();
1221 netif_receive_skb(skb);
1222 local_bh_enable();
1223 return;
1224 }
1225
1226 spin_lock(&queue->lock);
1227 if (!more || skb_queue_len(queue) == rx_batched) {
1228 __skb_queue_head_init(&process_queue);
1229 skb_queue_splice_tail_init(queue, &process_queue);
1230 rcv = true;
1231 } else {
1232 __skb_queue_tail(queue, skb);
1233 }
1234 spin_unlock(&queue->lock);
1235
1236 if (rcv) {
1237 struct sk_buff *nskb;
1238
1239 local_bh_disable();
1240 while ((nskb = __skb_dequeue(&process_queue)))
1241 netif_receive_skb(nskb);
1242 netif_receive_skb(skb);
1243 local_bh_enable();
1244 }
1245}
1246
Jason Wang66ccbc92017-08-11 19:41:16 +08001247static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1248 int len, int noblock, bool zerocopy)
1249{
1250 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1251 return false;
1252
1253 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1254 return false;
1255
1256 if (!noblock)
1257 return false;
1258
1259 if (zerocopy)
1260 return false;
1261
1262 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1263 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1264 return false;
1265
1266 return true;
1267}
1268
Jason Wang761876c2017-08-11 19:41:18 +08001269static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1270 struct tun_file *tfile,
Jason Wang66ccbc92017-08-11 19:41:16 +08001271 struct iov_iter *from,
Jason Wang761876c2017-08-11 19:41:18 +08001272 struct virtio_net_hdr *hdr,
1273 int len, int *generic_xdp)
Jason Wang66ccbc92017-08-11 19:41:16 +08001274{
1275 struct page_frag *alloc_frag = &tfile->alloc_frag;
1276 struct sk_buff *skb;
Jason Wang761876c2017-08-11 19:41:18 +08001277 struct bpf_prog *xdp_prog;
Jason Wang66ccbc92017-08-11 19:41:16 +08001278 int buflen = SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1279 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jason Wang761876c2017-08-11 19:41:18 +08001280 unsigned int delta = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001281 char *buf;
1282 size_t copied;
Jason Wang761876c2017-08-11 19:41:18 +08001283 bool xdp_xmit = false;
1284 int err;
Jason Wang66ccbc92017-08-11 19:41:16 +08001285
1286 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1287 return ERR_PTR(-ENOMEM);
1288
1289 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1290 copied = copy_page_from_iter(alloc_frag->page,
1291 alloc_frag->offset + TUN_RX_PAD,
1292 len, from);
1293 if (copied != len)
1294 return ERR_PTR(-EFAULT);
1295
Jason Wang761876c2017-08-11 19:41:18 +08001296 if (hdr->gso_type)
1297 *generic_xdp = 1;
1298 else
1299 *generic_xdp = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001300
Jason Wang761876c2017-08-11 19:41:18 +08001301 rcu_read_lock();
1302 xdp_prog = rcu_dereference(tun->xdp_prog);
1303 if (xdp_prog && !*generic_xdp) {
1304 struct xdp_buff xdp;
1305 void *orig_data;
1306 u32 act;
1307
1308 xdp.data_hard_start = buf;
1309 xdp.data = buf + TUN_RX_PAD;
1310 xdp.data_end = xdp.data + len;
1311 orig_data = xdp.data;
1312 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1313
1314 switch (act) {
1315 case XDP_REDIRECT:
1316 get_page(alloc_frag->page);
1317 alloc_frag->offset += buflen;
1318 err = xdp_do_redirect(tun->dev, &xdp, xdp_prog);
1319 if (err)
1320 goto err_redirect;
1321 return NULL;
1322 case XDP_TX:
1323 xdp_xmit = true;
1324 /* fall through */
1325 case XDP_PASS:
1326 delta = orig_data - xdp.data;
1327 break;
1328 default:
1329 bpf_warn_invalid_xdp_action(act);
1330 /* fall through */
1331 case XDP_ABORTED:
1332 trace_xdp_exception(tun->dev, xdp_prog, act);
1333 /* fall through */
1334 case XDP_DROP:
1335 goto err_xdp;
1336 }
1337 }
1338
1339 skb = build_skb(buf, buflen);
1340 if (!skb) {
1341 rcu_read_unlock();
1342 return ERR_PTR(-ENOMEM);
1343 }
1344
1345 skb_reserve(skb, TUN_RX_PAD - delta);
1346 skb_put(skb, len + delta);
Jason Wang66ccbc92017-08-11 19:41:16 +08001347 get_page(alloc_frag->page);
1348 alloc_frag->offset += buflen;
1349
Jason Wang761876c2017-08-11 19:41:18 +08001350 if (xdp_xmit) {
1351 skb->dev = tun->dev;
1352 generic_xdp_tx(skb, xdp_prog);
1353 rcu_read_lock();
1354 return NULL;
1355 }
1356
1357 rcu_read_unlock();
1358
Jason Wang66ccbc92017-08-11 19:41:16 +08001359 return skb;
Jason Wang761876c2017-08-11 19:41:18 +08001360
1361err_redirect:
1362 put_page(alloc_frag->page);
1363err_xdp:
1364 rcu_read_unlock();
1365 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1366 return NULL;
Jason Wang66ccbc92017-08-11 19:41:16 +08001367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001370static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001371 void *msg_control, struct iov_iter *from,
Jason Wang5503fce2017-01-18 15:02:03 +08001372 int noblock, bool more)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Harvey Harrison09640e632009-02-01 00:45:17 -08001374 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001376 size_t total_len = iov_iter_count(from);
Paolo Abenieaea34b2016-02-26 10:45:40 +01001377 size_t len = total_len, align = tun->align, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001378 struct virtio_net_hdr gso = { 0 };
Paolo Abeni608b9972016-04-13 10:52:20 +02001379 struct tun_pcpu_stats *stats;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001380 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001381 int copylen;
1382 bool zerocopy = false;
1383 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001384 u32 rxhash;
Jason Wang761876c2017-08-11 19:41:18 +08001385 int generic_xdp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Eric Dumazet1bd4978a2015-12-16 08:57:37 -08001387 if (!(tun->dev->flags & IFF_UP))
1388 return -EIO;
1389
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001390 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001391 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001393 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Al Virocbbd26b2016-11-01 22:09:04 -04001395 if (!copy_from_iter_full(&pi, sizeof(pi), from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return -EFAULT;
1397 }
1398
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001399 if (tun->flags & IFF_VNET_HDR) {
Willem de Bruijne1edab82017-02-03 18:20:48 -05001400 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1401
1402 if (len < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001403 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001404 len -= vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001405
Al Virocbbd26b2016-11-01 22:09:04 -04001406 if (!copy_from_iter_full(&gso, sizeof(gso), from))
Rusty Russellf43798c2008-07-03 03:48:02 -07001407 return -EFAULT;
1408
Herbert Xu49091222009-06-08 00:20:01 -07001409 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001410 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1411 gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
Herbert Xu49091222009-06-08 00:20:01 -07001412
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001413 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001414 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001415 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001416 }
1417
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001418 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001419 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001420 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001421 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001422 return -EINVAL;
1423 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001424
Jason Wang96f8d9e2013-11-13 14:00:39 +08001425 good_linear = SKB_MAX_HEAD(align);
1426
Jason Wang88529172013-07-18 10:55:15 +08001427 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001428 struct iov_iter i = *from;
1429
Jason Wang88529172013-07-18 10:55:15 +08001430 /* There are 256 bytes to be copied in skb, so there is
1431 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001432 * The rest of the buffer is mapped from userspace.
1433 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001434 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001435 if (copylen > good_linear)
1436 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001437 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001438 iov_iter_advance(&i, copylen);
1439 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001440 zerocopy = true;
1441 }
1442
Jason Wang66ccbc92017-08-11 19:41:16 +08001443 if (tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
Jason Wang761876c2017-08-11 19:41:18 +08001444 skb = tun_build_skb(tun, tfile, from, &gso, len, &generic_xdp);
Jason Wang66ccbc92017-08-11 19:41:16 +08001445 if (IS_ERR(skb)) {
Paolo Abeni608b9972016-04-13 10:52:20 +02001446 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001447 return PTR_ERR(skb);
1448 }
Jason Wang761876c2017-08-11 19:41:18 +08001449 if (!skb)
1450 return total_len;
Jason Wang66ccbc92017-08-11 19:41:16 +08001451 } else {
1452 if (!zerocopy) {
1453 copylen = len;
1454 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1455 linear = good_linear;
1456 else
1457 linear = tun16_to_cpu(tun, gso.hdr_len);
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Jason Wang66ccbc92017-08-11 19:41:16 +08001460 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
1461 if (IS_ERR(skb)) {
1462 if (PTR_ERR(skb) != -EAGAIN)
1463 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1464 return PTR_ERR(skb);
1465 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001466
Jason Wang66ccbc92017-08-11 19:41:16 +08001467 if (zerocopy)
1468 err = zerocopy_sg_from_iter(skb, from);
1469 else
1470 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1471
1472 if (err) {
1473 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1474 kfree_skb(skb);
1475 return -EFAULT;
1476 }
Dave Jones8f227572006-03-11 18:49:13 -08001477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001479 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
Paolo Abenidf10db92016-06-14 00:00:04 +02001480 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1481 kfree_skb(skb);
1482 return -EINVAL;
1483 }
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001486 case IFF_TUN:
1487 if (tun->flags & IFF_NO_PI) {
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001488 switch (skb->data[0] & 0xf0) {
1489 case 0x40:
1490 pi.proto = htons(ETH_P_IP);
1491 break;
1492 case 0x60:
1493 pi.proto = htons(ETH_P_IPV6);
1494 break;
1495 default:
Paolo Abeni608b9972016-04-13 10:52:20 +02001496 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001497 kfree_skb(skb);
1498 return -EINVAL;
1499 }
1500 }
1501
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001502 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001504 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001506 case IFF_TAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 skb->protocol = eth_type_trans(skb, tun->dev);
1508 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001511 /* copy skb_ubuf_info for callback when skb has no error */
1512 if (zerocopy) {
1513 skb_shinfo(skb)->destructor_arg = msg_control;
1514 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001515 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangaf1cc7a2016-11-30 13:17:51 +08001516 } else if (msg_control) {
1517 struct ubuf_info *uarg = msg_control;
1518 uarg->callback(uarg, false);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001519 }
1520
Vlad Yasevich72f65102015-02-03 16:36:16 -05001521 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001522 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001523
Jason Wang761876c2017-08-11 19:41:18 +08001524 if (generic_xdp) {
1525 struct bpf_prog *xdp_prog;
1526 int ret;
1527
1528 rcu_read_lock();
1529 xdp_prog = rcu_dereference(tun->xdp_prog);
1530 if (xdp_prog) {
1531 ret = do_xdp_generic(xdp_prog, skb);
1532 if (ret != XDP_PASS) {
1533 rcu_read_unlock();
1534 return total_len;
1535 }
1536 }
1537 rcu_read_unlock();
1538 }
1539
Jason Wangfeec0842017-06-06 14:09:49 +08001540 rxhash = __skb_get_hash_symmetric(skb);
Andrey Konovalovd4aea202016-12-01 10:34:40 +01001541#ifndef CONFIG_4KSTACKS
Jason Wang5503fce2017-01-18 15:02:03 +08001542 tun_rx_batched(tun, tfile, skb, more);
Andrey Konovalovd4aea202016-12-01 10:34:40 +01001543#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 netif_rx_ni(skb);
Andrey Konovalovd4aea202016-12-01 10:34:40 +01001545#endif
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001546
Paolo Abeni608b9972016-04-13 10:52:20 +02001547 stats = get_cpu_ptr(tun->pcpu_stats);
1548 u64_stats_update_begin(&stats->syncp);
1549 stats->rx_packets++;
1550 stats->rx_bytes += len;
1551 u64_stats_update_end(&stats->syncp);
1552 put_cpu_ptr(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Jason Wang9e857222013-01-28 01:05:19 +00001554 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001555 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Al Virof5ff53b2014-06-19 15:36:49 -04001558static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001560 struct file *file = iocb->ki_filp;
Herbert Xuab46d7792009-02-14 20:46:39 -08001561 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001562 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001563 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 if (!tun)
1566 return -EBADFD;
1567
Jason Wang5503fce2017-01-18 15:02:03 +08001568 result = tun_get_user(tun, tfile, NULL, from,
1569 file->f_flags & O_NONBLOCK, false);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001570
1571 tun_put(tun);
1572 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001576static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001577 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001578 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08001579 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 struct tun_pi pi = { 0, skb->protocol };
Paolo Abeni608b9972016-04-13 10:52:20 +02001582 struct tun_pcpu_stats *stats;
Herbert Xue0b46d02014-11-07 21:22:23 +08001583 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08001584 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001585 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001586 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001587
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001588 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001589 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001591 if (tun->flags & IFF_VNET_HDR)
Willem de Bruijne1edab82017-02-03 18:20:48 -05001592 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Herbert Xue0b46d02014-11-07 21:22:23 +08001594 total = skb->len + vlan_hlen + vnet_hdr_sz;
1595
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001596 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001597 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return -EINVAL;
1599
Herbert Xue0b46d02014-11-07 21:22:23 +08001600 total += sizeof(pi);
1601 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /* Packet will be striped */
1603 pi.flags |= TUN_PKT_STRIP;
1604 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001605
Herbert Xue0b46d02014-11-07 21:22:23 +08001606 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Herbert Xu2eb783c2014-11-03 04:30:14 +08001610 if (vnet_hdr_sz) {
Jarno Rajahalme9403cd72016-11-18 15:40:40 -08001611 struct virtio_net_hdr gso;
Mike Rapoport34166092016-06-08 16:09:20 +03001612
Herbert Xue0b46d02014-11-07 21:22:23 +08001613 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001614 return -EINVAL;
1615
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001616 if (virtio_net_hdr_from_skb(skb, &gso,
Jason Wang6391a442017-01-20 14:32:42 +08001617 tun_is_little_endian(tun), true)) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001618 struct skb_shared_info *sinfo = skb_shinfo(skb);
Mike Rapoport34166092016-06-08 16:09:20 +03001619 pr_err("unexpected GSO type: "
1620 "0x%x, gso_size %d, hdr_len %d\n",
1621 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1622 tun16_to_cpu(tun, gso.hdr_len));
1623 print_hex_dump(KERN_ERR, "tun: ",
1624 DUMP_PREFIX_NONE,
1625 16, 1, skb->head,
1626 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1627 WARN_ON_ONCE(1);
1628 return -EINVAL;
1629 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001630
Herbert Xue0b46d02014-11-07 21:22:23 +08001631 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07001632 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08001633
1634 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001635 }
1636
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001637 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001638 int ret;
Jason Wang6680ec62013-07-25 13:00:33 +08001639 struct {
1640 __be16 h_vlan_proto;
1641 __be16 h_vlan_TCI;
1642 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Jason Wang6680ec62013-07-25 13:00:33 +08001644 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001645 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Jason Wang6680ec62013-07-25 13:00:33 +08001647 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08001648
Herbert Xue0b46d02014-11-07 21:22:23 +08001649 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1650 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001651 goto done;
1652
Herbert Xue0b46d02014-11-07 21:22:23 +08001653 ret = copy_to_iter(&veth, sizeof(veth), iter);
1654 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001655 goto done;
1656 }
1657
Herbert Xue0b46d02014-11-07 21:22:23 +08001658 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08001659
1660done:
Paolo Abeni608b9972016-04-13 10:52:20 +02001661 /* caller is in process context, */
1662 stats = get_cpu_ptr(tun->pcpu_stats);
1663 u64_stats_update_begin(&stats->syncp);
1664 stats->tx_packets++;
1665 stats->tx_bytes += skb->len + vlan_hlen;
1666 u64_stats_update_end(&stats->syncp);
1667 put_cpu_ptr(tun->pcpu_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 return total;
1670}
1671
Jason Wang1576d982016-06-30 14:45:36 +08001672static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock,
1673 int *err)
1674{
1675 DECLARE_WAITQUEUE(wait, current);
1676 struct sk_buff *skb = NULL;
Jason Wangf48cc6b2016-07-04 13:53:38 +08001677 int error = 0;
Jason Wang1576d982016-06-30 14:45:36 +08001678
1679 skb = skb_array_consume(&tfile->tx_array);
1680 if (skb)
1681 goto out;
1682 if (noblock) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001683 error = -EAGAIN;
Jason Wang1576d982016-06-30 14:45:36 +08001684 goto out;
1685 }
1686
1687 add_wait_queue(&tfile->wq.wait, &wait);
1688 current->state = TASK_INTERRUPTIBLE;
1689
1690 while (1) {
1691 skb = skb_array_consume(&tfile->tx_array);
1692 if (skb)
1693 break;
1694 if (signal_pending(current)) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001695 error = -ERESTARTSYS;
Jason Wang1576d982016-06-30 14:45:36 +08001696 break;
1697 }
1698 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001699 error = -EFAULT;
Jason Wang1576d982016-06-30 14:45:36 +08001700 break;
1701 }
1702
1703 schedule();
1704 }
1705
1706 current->state = TASK_RUNNING;
1707 remove_wait_queue(&tfile->wq.wait, &wait);
1708
1709out:
Jason Wangf48cc6b2016-07-04 13:53:38 +08001710 *err = error;
Jason Wang1576d982016-06-30 14:45:36 +08001711 return skb;
1712}
1713
Jason Wang54f968d2012-10-31 19:45:57 +00001714static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05001715 struct iov_iter *to,
Jason Wangac77cfd2017-05-17 12:14:43 +08001716 int noblock, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Al Viro9b067032014-11-07 13:52:07 -05001718 ssize_t ret;
Jason Wang1576d982016-06-30 14:45:36 +08001719 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Rami Rosen3872baf2012-11-25 22:07:41 +00001721 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Al Viro9b067032014-11-07 13:52:07 -05001723 if (!iov_iter_count(to))
1724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Jason Wangac77cfd2017-05-17 12:14:43 +08001726 if (!skb) {
1727 /* Read frames from ring */
1728 skb = tun_ring_recv(tfile, noblock, &err);
1729 if (!skb)
1730 return err;
1731 }
Herbert Xue0b46d02014-11-07 21:22:23 +08001732
Al Viro9b067032014-11-07 13:52:07 -05001733 ret = tun_put_user(tun, tfile, skb, to);
Jason Wangf51a5e82014-12-01 16:53:15 +08001734 if (unlikely(ret < 0))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001735 kfree_skb(skb);
Jason Wangf51a5e82014-12-01 16:53:15 +08001736 else
1737 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001739 return ret;
1740}
1741
Al Viro9b067032014-11-07 13:52:07 -05001742static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001743{
1744 struct file *file = iocb->ki_filp;
1745 struct tun_file *tfile = file->private_data;
1746 struct tun_struct *tun = __tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05001747 ssize_t len = iov_iter_count(to), ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001748
1749 if (!tun)
1750 return -EBADFD;
Jason Wangac77cfd2017-05-17 12:14:43 +08001751 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL);
David S. Miller42404c02013-12-10 22:05:45 -05001752 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08001753 if (ret > 0)
1754 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001755 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return ret;
1757}
1758
Jason Wang96442e422012-10-31 19:46:02 +00001759static void tun_free_netdev(struct net_device *dev)
1760{
1761 struct tun_struct *tun = netdev_priv(dev);
1762
Jason Wang4008e972012-12-13 23:53:30 +00001763 BUG_ON(!(list_empty(&tun->disabled)));
Paolo Abeni608b9972016-04-13 10:52:20 +02001764 free_percpu(tun->pcpu_stats);
Jason Wang96442e422012-10-31 19:46:02 +00001765 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001766 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001767}
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769static void tun_setup(struct net_device *dev)
1770{
1771 struct tun_struct *tun = netdev_priv(dev);
1772
Eric W. Biederman0625c882012-02-07 16:48:55 -08001773 tun->owner = INVALID_UID;
1774 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 dev->ethtool_ops = &tun_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001777 dev->needs_free_netdev = true;
1778 dev->priv_destructor = tun_free_netdev;
Jason Wang016adb72016-04-08 13:26:48 +08001779 /* We prefer our own queue length */
1780 dev->tx_queue_len = TUN_READQ_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001783/* Trivial set of netlink ops to allow deleting tun or tap
1784 * device with netlink.
1785 */
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001786static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
1787 struct netlink_ext_ack *extack)
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001788{
1789 return -EINVAL;
1790}
1791
1792static struct rtnl_link_ops tun_link_ops __read_mostly = {
1793 .kind = DRV_NAME,
1794 .priv_size = sizeof(struct tun_struct),
1795 .setup = tun_setup,
1796 .validate = tun_validate,
1797};
1798
Herbert Xu33dccbb2009-02-05 21:25:32 -08001799static void tun_sock_write_space(struct sock *sk)
1800{
Jason Wang54f968d2012-10-31 19:45:57 +00001801 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001802 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001803
1804 if (!sock_writeable(sk))
1805 return;
1806
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001807 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Herbert Xu33dccbb2009-02-05 21:25:32 -08001808 return;
1809
Eric Dumazet43815482010-04-29 11:01:49 +00001810 wqueue = sk_sleep(sk);
1811 if (wqueue && waitqueue_active(wqueue))
1812 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001813 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001814
Jason Wang54f968d2012-10-31 19:45:57 +00001815 tfile = container_of(sk, struct tun_file, sk);
1816 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001817}
1818
Ying Xue1b784142015-03-02 15:37:48 +08001819static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001820{
Jason Wang54f968d2012-10-31 19:45:57 +00001821 int ret;
1822 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1823 struct tun_struct *tun = __tun_get(tfile);
1824
1825 if (!tun)
1826 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04001827
Al Viroc0371da2014-11-24 10:42:55 -05001828 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
Jason Wang5503fce2017-01-18 15:02:03 +08001829 m->msg_flags & MSG_DONTWAIT,
1830 m->msg_flags & MSG_MORE);
Jason Wang54f968d2012-10-31 19:45:57 +00001831 tun_put(tun);
1832 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001833}
1834
Ying Xue1b784142015-03-02 15:37:48 +08001835static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001836 int flags)
1837{
Jason Wang54f968d2012-10-31 19:45:57 +00001838 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1839 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001840 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001841
1842 if (!tun)
1843 return -EBADFD;
1844
Richard Cochraneda29772013-07-19 19:40:10 +02001845 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00001846 ret = -EINVAL;
1847 goto out;
1848 }
Richard Cochraneda29772013-07-19 19:40:10 +02001849 if (flags & MSG_ERRQUEUE) {
1850 ret = sock_recv_errqueue(sock->sk, m, total_len,
1851 SOL_PACKET, TUN_TX_TIMESTAMP);
1852 goto out;
1853 }
Jason Wangac77cfd2017-05-17 12:14:43 +08001854 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT,
1855 m->msg_control);
Alex Gartrell87897932014-12-25 23:05:03 -08001856 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05001857 m->msg_flags |= MSG_TRUNC;
1858 ret = flags & MSG_TRUNC ? ret : total_len;
1859 }
Gao feng3811ae72013-04-24 21:59:23 +00001860out:
Jason Wang54f968d2012-10-31 19:45:57 +00001861 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001862 return ret;
1863}
1864
Jason Wang1576d982016-06-30 14:45:36 +08001865static int tun_peek_len(struct socket *sock)
1866{
1867 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1868 struct tun_struct *tun;
1869 int ret = 0;
1870
1871 tun = __tun_get(tfile);
1872 if (!tun)
1873 return 0;
1874
1875 ret = skb_array_peek_len(&tfile->tx_array);
1876 tun_put(tun);
1877
1878 return ret;
1879}
1880
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001881/* Ops structure to mimic raw sockets with tun */
1882static const struct proto_ops tun_socket_ops = {
Jason Wang1576d982016-06-30 14:45:36 +08001883 .peek_len = tun_peek_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001884 .sendmsg = tun_sendmsg,
1885 .recvmsg = tun_recvmsg,
1886};
1887
Herbert Xu33dccbb2009-02-05 21:25:32 -08001888static struct proto tun_proto = {
1889 .name = "tun",
1890 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001891 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001892};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001893
David Woodhouse980c9e82009-05-09 22:54:21 -07001894static int tun_flags(struct tun_struct *tun)
1895{
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02001896 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07001897}
1898
1899static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1900 char *buf)
1901{
1902 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1903 return sprintf(buf, "0x%x\n", tun_flags(tun));
1904}
1905
1906static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1907 char *buf)
1908{
1909 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001910 return uid_valid(tun->owner)?
1911 sprintf(buf, "%u\n",
1912 from_kuid_munged(current_user_ns(), tun->owner)):
1913 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001914}
1915
1916static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1917 char *buf)
1918{
1919 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001920 return gid_valid(tun->group) ?
1921 sprintf(buf, "%u\n",
1922 from_kgid_munged(current_user_ns(), tun->group)):
1923 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001924}
1925
1926static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1927static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1928static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1929
Takashi Iwaic4d33e22015-02-04 14:37:34 +01001930static struct attribute *tun_dev_attrs[] = {
1931 &dev_attr_tun_flags.attr,
1932 &dev_attr_owner.attr,
1933 &dev_attr_group.attr,
1934 NULL
1935};
1936
1937static const struct attribute_group tun_attr_group = {
1938 .attrs = tun_dev_attrs
1939};
1940
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001941static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001944 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 struct net_device *dev;
1946 int err;
1947
Jason Wang7c0c3b12013-01-11 16:59:33 +00001948 if (tfile->detached)
1949 return -EINVAL;
1950
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001951 dev = __dev_get_by_name(net, ifr->ifr_name);
1952 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001953 if (ifr->ifr_flags & IFF_TUN_EXCL)
1954 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001955 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1956 tun = netdev_priv(dev);
1957 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1958 tun = netdev_priv(dev);
1959 else
1960 return -EINVAL;
1961
Jason Wang8e6d91a2013-05-28 18:32:11 +00001962 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001963 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00001964 return -EINVAL;
1965
Jason Wangcde8b152012-10-31 19:46:01 +00001966 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001967 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001968 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001969 if (err < 0)
1970 return err;
1971
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001972 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001973 if (err < 0)
1974 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001975
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001976 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00001977 (tun->numqueues + tun->numdisabled > 1)) {
1978 /* One or more queue has already been attached, no need
1979 * to initialize the device again.
1980 */
1981 return 0;
1982 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 else {
1985 char *name;
1986 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001987 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1988 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001990 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001991 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001992 err = security_tun_dev_create();
1993 if (err < 0)
1994 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /* Set dev type */
1997 if (ifr->ifr_flags & IFF_TUN) {
1998 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001999 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 name = "tun%d";
2001 } else if (ifr->ifr_flags & IFF_TAP) {
2002 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002003 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002005 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00002006 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (*ifr->ifr_name)
2009 name = ifr->ifr_name;
2010
Jason Wangc8d68e62012-10-31 19:46:00 +00002011 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02002012 NET_NAME_UNKNOWN, tun_setup, queues,
2013 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!dev)
2016 return -ENOMEM;
2017
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07002018 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002019 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002020 dev->ifindex = tfile->ifindex;
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002021 dev->sysfs_groups[0] = &tun_attr_group;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 tun = netdev_priv(dev);
2024 tun->dev = dev;
2025 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002026 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002027 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Paolo Abenieaea34b2016-02-26 10:45:40 +01002029 tun->align = NET_SKB_PAD;
Jason Wang54f968d2012-10-31 19:45:57 +00002030 tun->filter_attached = false;
2031 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Jason Wang5503fce2017-01-18 15:02:03 +08002032 tun->rx_batched = 0;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002033
Paolo Abeni608b9972016-04-13 10:52:20 +02002034 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2035 if (!tun->pcpu_stats) {
2036 err = -ENOMEM;
2037 goto err_free_dev;
2038 }
2039
Jason Wang96442e422012-10-31 19:46:02 +00002040 spin_lock_init(&tun->lock);
2041
Paul Moore5dbbaf22013-01-14 07:12:19 +00002042 err = security_tun_dev_alloc_security(&tun->security);
2043 if (err < 0)
Paolo Abeni608b9972016-04-13 10:52:20 +02002044 goto err_free_stat;
Paul Moore2b980db2009-08-28 18:12:43 -04002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04002047 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00002048
Michał Mirosław88255372011-04-19 06:13:10 +00002049 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08002050 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2051 NETIF_F_HW_VLAN_STAG_TX;
Paolo Abeni2a2bbf12016-04-14 18:39:39 +02002052 dev->features = dev->hw_features | NETIF_F_LLTX;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09002053 dev->vlan_features = dev->features &
2054 ~(NETIF_F_HW_VLAN_CTAG_TX |
2055 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00002056
Jason Wang4008e972012-12-13 23:53:30 +00002057 INIT_LIST_HEAD(&tun->disabled);
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04002058 err = tun_attach(tun, file, false);
Jason Wangeb0fb362012-12-02 17:19:45 +00002059 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002060 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 err = register_netdevice(tun->dev);
2063 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002064 goto err_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00002067 netif_carrier_on(tun->dev);
2068
Joe Perches6b8a66e2011-03-02 07:18:10 +00002069 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02002071 tun->flags = (tun->flags & ~TUN_FEATURES) |
2072 (ifr->ifr_flags & TUN_FEATURES);
Jason Wangc8d68e62012-10-31 19:46:00 +00002073
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002074 /* Make sure persistent devices do not get stuck in
2075 * xoff state.
2076 */
2077 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00002078 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 strcpy(ifr->ifr_name, tun->dev->name);
2081 return 0;
2082
Jason Wang662ca432013-09-11 18:09:48 +08002083err_detach:
2084 tun_detach_all(dev);
2085err_free_flow:
2086 tun_flow_uninit(tun);
2087 security_tun_dev_free_security(tun->security);
Paolo Abeni608b9972016-04-13 10:52:20 +02002088err_free_stat:
2089 free_percpu(tun->pcpu_stats);
Jason Wang662ca432013-09-11 18:09:48 +08002090err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return err;
2093}
2094
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002095static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00002096 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07002097{
Joe Perches6b8a66e2011-03-02 07:18:10 +00002098 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07002099
2100 strcpy(ifr->ifr_name, tun->dev->name);
2101
David Woodhouse980c9e82009-05-09 22:54:21 -07002102 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07002103
Mark McLoughline3b99552008-08-15 15:09:56 -07002104}
2105
Rusty Russell5228ddc2008-07-03 03:46:16 -07002106/* This is like a cut-down ethtool ops, except done via tun fd so no
2107 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00002108static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07002109{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002110 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002111
2112 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00002113 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002114 arg &= ~TUN_F_CSUM;
2115
2116 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2117 if (arg & TUN_F_TSO_ECN) {
2118 features |= NETIF_F_TSO_ECN;
2119 arg &= ~TUN_F_TSO_ECN;
2120 }
2121 if (arg & TUN_F_TSO4)
2122 features |= NETIF_F_TSO;
2123 if (arg & TUN_F_TSO6)
2124 features |= NETIF_F_TSO6;
2125 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2126 }
2127 }
2128
2129 /* This gives the user a way to test for new features in future by
2130 * trying to set them. */
2131 if (arg)
2132 return -EINVAL;
2133
Michał Mirosław88255372011-04-19 06:13:10 +00002134 tun->set_features = features;
Yaroslav Isakov09050952017-03-16 22:44:10 +03002135 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2136 tun->dev->wanted_features |= features;
Michał Mirosław88255372011-04-19 06:13:10 +00002137 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07002138
2139 return 0;
2140}
2141
Jason Wangc8d68e62012-10-31 19:46:00 +00002142static void tun_detach_filter(struct tun_struct *tun, int n)
2143{
2144 int i;
2145 struct tun_file *tfile;
2146
2147 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002148 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002149 lock_sock(tfile->socket.sk);
2150 sk_detach_filter(tfile->socket.sk);
2151 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002152 }
2153
2154 tun->filter_attached = false;
2155}
2156
2157static int tun_attach_filter(struct tun_struct *tun)
2158{
2159 int i, ret = 0;
2160 struct tun_file *tfile;
2161
2162 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002163 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002164 lock_sock(tfile->socket.sk);
2165 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2166 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002167 if (ret) {
2168 tun_detach_filter(tun, i);
2169 return ret;
2170 }
2171 }
2172
2173 tun->filter_attached = true;
2174 return ret;
2175}
2176
2177static void tun_set_sndbuf(struct tun_struct *tun)
2178{
2179 struct tun_file *tfile;
2180 int i;
2181
2182 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002183 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00002184 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2185 }
2186}
2187
Jason Wangcde8b152012-10-31 19:46:01 +00002188static int tun_set_queue(struct file *file, struct ifreq *ifr)
2189{
2190 struct tun_file *tfile = file->private_data;
2191 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00002192 int ret = 0;
2193
2194 rtnl_lock();
2195
2196 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00002197 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002198 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00002199 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002200 goto unlock;
2201 }
2202 ret = security_tun_dev_attach_queue(tun->security);
2203 if (ret < 0)
2204 goto unlock;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04002205 ret = tun_attach(tun, file, false);
Jason Wang4008e972012-12-13 23:53:30 +00002206 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002207 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002208 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00002209 ret = -EINVAL;
2210 else
2211 __tun_detach(tfile, false);
2212 } else
Jason Wangcde8b152012-10-31 19:46:01 +00002213 ret = -EINVAL;
2214
Paul Moore5dbbaf22013-01-14 07:12:19 +00002215unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00002216 rtnl_unlock();
2217 return ret;
2218}
2219
Arnd Bergmann50857e22009-11-06 22:52:32 -08002220static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2221 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002223 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002224 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 void __user* argp = (void __user*)arg;
2226 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08002227 kuid_t owner;
2228 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002229 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002230 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002231 unsigned int ifindex;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002232 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002233 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Gao Feng20861f22016-10-27 09:05:22 +08002235 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08002236 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07002238 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00002239 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07002240 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002241 if (cmd == TUNGETFEATURES) {
2242 /* Currently this just means: "what IFF flags are valid?".
2243 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02002244 * TUNSETIFF.
2245 */
2246 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00002247 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00002248 } else if (cmd == TUNSETQUEUE)
2249 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002250
Jason Wangc8d68e62012-10-31 19:46:00 +00002251 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00002252 rtnl_lock();
2253
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002254 tun = __tun_get(tfile);
Gao Feng0f16bc12016-10-25 22:26:09 +08002255 if (cmd == TUNSETIFF) {
2256 ret = -EEXIST;
2257 if (tun)
2258 goto unlock;
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2261
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002262 ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Herbert Xu876bfd42009-08-06 14:22:44 +00002264 if (ret)
2265 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Arnd Bergmann50857e22009-11-06 22:52:32 -08002267 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00002268 ret = -EFAULT;
2269 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002271 if (cmd == TUNSETIFINDEX) {
2272 ret = -EPERM;
2273 if (tun)
2274 goto unlock;
2275
2276 ret = -EFAULT;
2277 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2278 goto unlock;
2279
2280 ret = 0;
2281 tfile->ifindex = ifindex;
2282 goto unlock;
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Herbert Xu876bfd42009-08-06 14:22:44 +00002285 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00002287 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Jason Wang1e588332012-10-31 19:45:56 +00002289 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Eric W. Biederman631ab462009-01-20 11:00:40 +00002291 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07002293 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002294 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07002295
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002296 if (tfile->detached)
2297 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04002298 if (!tfile->socket.sk->sk_filter)
2299 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002300
Arnd Bergmann50857e22009-11-06 22:52:32 -08002301 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002302 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07002303 break;
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 case TUNSETNOCSUM:
2306 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Michał Mirosław88255372011-04-19 06:13:10 +00002308 /* [unimplemented] */
2309 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00002310 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 break;
2312
2313 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00002314 /* Disable/Enable persist mode. Keep an extra reference to the
2315 * module to prevent the module being unprobed.
2316 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002317 if (arg && !(tun->flags & IFF_PERSIST)) {
2318 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002319 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00002320 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002321 if (!arg && (tun->flags & IFF_PERSIST)) {
2322 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002323 module_put(THIS_MODULE);
2324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Joe Perches6b8a66e2011-03-02 07:18:10 +00002326 tun_debug(KERN_INFO, tun, "persist %s\n",
2327 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 break;
2329
2330 case TUNSETOWNER:
2331 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002332 owner = make_kuid(current_user_ns(), arg);
2333 if (!uid_valid(owner)) {
2334 ret = -EINVAL;
2335 break;
2336 }
2337 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00002338 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002339 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 break;
2341
Guido Guenther8c644622007-07-02 22:50:25 -07002342 case TUNSETGROUP:
2343 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002344 group = make_kgid(current_user_ns(), arg);
2345 if (!gid_valid(group)) {
2346 ret = -EINVAL;
2347 break;
2348 }
2349 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00002350 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002351 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07002352 break;
2353
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002354 case TUNSETLINK:
2355 /* Only allow setting the type when the interface is down */
2356 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002357 tun_debug(KERN_INFO, tun,
2358 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07002359 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002360 } else {
2361 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00002362 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
2363 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07002364 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002365 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002366 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368#ifdef TUN_DEBUG
2369 case TUNSETDEBUG:
2370 tun->debug = arg;
2371 break;
2372#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07002373 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00002374 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002375 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002376
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002377 case TUNSETTXFILTER:
2378 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00002379 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002380 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00002381 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07002382 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002386 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002387 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2388 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08002389 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002390 ret = -EFAULT;
2391 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002394 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00002395 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2396 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08002397
Kim B. Heino40102372008-02-29 12:26:21 -08002398 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002399 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002400
2401 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002402 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002403 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2404 ret = -EFAULT;
2405 break;
2406
2407 case TUNSETSNDBUF:
2408 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2409 ret = -EFAULT;
2410 break;
2411 }
2412
Jason Wangc8d68e62012-10-31 19:46:00 +00002413 tun->sndbuf = sndbuf;
2414 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002415 break;
2416
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002417 case TUNGETVNETHDRSZ:
2418 vnet_hdr_sz = tun->vnet_hdr_sz;
2419 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2420 ret = -EFAULT;
2421 break;
2422
2423 case TUNSETVNETHDRSZ:
2424 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2425 ret = -EFAULT;
2426 break;
2427 }
2428 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2429 ret = -EINVAL;
2430 break;
2431 }
2432
2433 tun->vnet_hdr_sz = vnet_hdr_sz;
2434 break;
2435
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002436 case TUNGETVNETLE:
2437 le = !!(tun->flags & TUN_VNET_LE);
2438 if (put_user(le, (int __user *)argp))
2439 ret = -EFAULT;
2440 break;
2441
2442 case TUNSETVNETLE:
2443 if (get_user(le, (int __user *)argp)) {
2444 ret = -EFAULT;
2445 break;
2446 }
2447 if (le)
2448 tun->flags |= TUN_VNET_LE;
2449 else
2450 tun->flags &= ~TUN_VNET_LE;
2451 break;
2452
Greg Kurz8b8e6582015-04-24 14:50:36 +02002453 case TUNGETVNETBE:
2454 ret = tun_get_vnet_be(tun, argp);
2455 break;
2456
2457 case TUNSETVNETBE:
2458 ret = tun_set_vnet_be(tun, argp);
2459 break;
2460
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002461 case TUNATTACHFILTER:
2462 /* Can be set only for TAPs */
2463 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002464 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002465 break;
2466 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002467 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002468 break;
2469
Jason Wangc8d68e62012-10-31 19:46:00 +00002470 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002471 break;
2472
2473 case TUNDETACHFILTER:
2474 /* Can be set only for TAPs */
2475 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002476 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002477 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002478 ret = 0;
2479 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002480 break;
2481
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002482 case TUNGETFILTER:
2483 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002484 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002485 break;
2486 ret = -EFAULT;
2487 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2488 break;
2489 ret = 0;
2490 break;
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002493 ret = -EINVAL;
2494 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Herbert Xu876bfd42009-08-06 14:22:44 +00002497unlock:
2498 rtnl_unlock();
2499 if (tun)
2500 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002501 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Arnd Bergmann50857e22009-11-06 22:52:32 -08002504static long tun_chr_ioctl(struct file *file,
2505 unsigned int cmd, unsigned long arg)
2506{
2507 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2508}
2509
2510#ifdef CONFIG_COMPAT
2511static long tun_chr_compat_ioctl(struct file *file,
2512 unsigned int cmd, unsigned long arg)
2513{
2514 switch (cmd) {
2515 case TUNSETIFF:
2516 case TUNGETIFF:
2517 case TUNSETTXFILTER:
2518 case TUNGETSNDBUF:
2519 case TUNSETSNDBUF:
2520 case SIOCGIFHWADDR:
2521 case SIOCSIFHWADDR:
2522 arg = (unsigned long)compat_ptr(arg);
2523 break;
2524 default:
2525 arg = (compat_ulong_t)arg;
2526 break;
2527 }
2528
2529 /*
2530 * compat_ifreq is shorter than ifreq, so we must not access beyond
2531 * the end of that structure. All fields that are used in this
2532 * driver are compatible though, we don't need to convert the
2533 * contents.
2534 */
2535 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2536}
2537#endif /* CONFIG_COMPAT */
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539static int tun_chr_fasync(int fd, struct file *file, int on)
2540{
Jason Wang54f968d2012-10-31 19:45:57 +00002541 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 int ret;
2543
Jason Wang54f968d2012-10-31 19:45:57 +00002544 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002545 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (on) {
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002548 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00002549 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002550 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002551 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002552 ret = 0;
2553out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002554 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
2557static int tun_chr_open(struct inode *inode, struct file * file)
2558{
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002559 struct net *net = current->nsproxy->net_ns;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002560 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002561
Joe Perches6b8a66e2011-03-02 07:18:10 +00002562 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002563
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002564 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05002565 &tun_proto, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002566 if (!tfile)
2567 return -ENOMEM;
Monam Agarwalc9566742014-03-24 00:02:32 +05302568 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang54f968d2012-10-31 19:45:57 +00002569 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002570 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002571
Jason Wang54f968d2012-10-31 19:45:57 +00002572 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07002573 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00002574
2575 tfile->socket.file = file;
2576 tfile->socket.ops = &tun_socket_ops;
2577
2578 sock_init_data(&tfile->socket, &tfile->sk);
Jason Wang54f968d2012-10-31 19:45:57 +00002579
2580 tfile->sk.sk_write_space = tun_sock_write_space;
2581 tfile->sk.sk_sndbuf = INT_MAX;
2582
Jason Wang66ccbc92017-08-11 19:41:16 +08002583 tfile->alloc_frag.page = NULL;
2584
Eric W. Biederman631ab462009-01-20 11:00:40 +00002585 file->private_data = tfile;
Jason Wang4008e972012-12-13 23:53:30 +00002586 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002587
Jason Wang19a6afb2013-06-08 14:17:41 +08002588 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return 0;
2591}
2592
2593static int tun_chr_close(struct inode *inode, struct file *file)
2594{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002595 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Jason Wangc8d68e62012-10-31 19:46:00 +00002597 tun_detach(tfile, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 return 0;
2600}
2601
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002602#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -07002603static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002604{
2605 struct tun_struct *tun;
2606 struct ifreq ifr;
2607
2608 memset(&ifr, 0, sizeof(ifr));
2609
2610 rtnl_lock();
2611 tun = tun_get(f);
2612 if (tun)
2613 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2614 rtnl_unlock();
2615
2616 if (tun)
2617 tun_put(tun);
2618
Joe Perchesa3816ab2014-09-29 16:08:25 -07002619 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002620}
2621#endif
2622
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002623static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002624 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05002626 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04002627 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002629 .unlocked_ioctl = tun_chr_ioctl,
2630#ifdef CONFIG_COMPAT
2631 .compat_ioctl = tun_chr_compat_ioctl,
2632#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 .open = tun_chr_open,
2634 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002635 .fasync = tun_chr_fasync,
2636#ifdef CONFIG_PROC_FS
2637 .show_fdinfo = tun_chr_show_fdinfo,
2638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639};
2640
2641static struct miscdevice tun_miscdev = {
2642 .minor = TUN_MINOR,
2643 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002644 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646};
2647
2648/* ethtool interface */
2649
Philippe Reynes29ccc492017-03-11 22:03:50 +01002650static int tun_get_link_ksettings(struct net_device *dev,
2651 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
Philippe Reynes29ccc492017-03-11 22:03:50 +01002653 ethtool_link_ksettings_zero_link_mode(cmd, supported);
2654 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
2655 cmd->base.speed = SPEED_10;
2656 cmd->base.duplex = DUPLEX_FULL;
2657 cmd->base.port = PORT_TP;
2658 cmd->base.phy_address = 0;
2659 cmd->base.autoneg = AUTONEG_DISABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return 0;
2661}
2662
2663static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2664{
2665 struct tun_struct *tun = netdev_priv(dev);
2666
Rick Jones33a5ba12011-11-15 14:59:53 +00002667 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2668 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002671 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00002672 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002674 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00002675 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 break;
2677 }
2678}
2679
2680static u32 tun_get_msglevel(struct net_device *dev)
2681{
2682#ifdef TUN_DEBUG
2683 struct tun_struct *tun = netdev_priv(dev);
2684 return tun->debug;
2685#else
2686 return -EOPNOTSUPP;
2687#endif
2688}
2689
2690static void tun_set_msglevel(struct net_device *dev, u32 value)
2691{
2692#ifdef TUN_DEBUG
2693 struct tun_struct *tun = netdev_priv(dev);
2694 tun->debug = value;
2695#endif
2696}
2697
Jason Wang5503fce2017-01-18 15:02:03 +08002698static int tun_get_coalesce(struct net_device *dev,
2699 struct ethtool_coalesce *ec)
2700{
2701 struct tun_struct *tun = netdev_priv(dev);
2702
2703 ec->rx_max_coalesced_frames = tun->rx_batched;
2704
2705 return 0;
2706}
2707
2708static int tun_set_coalesce(struct net_device *dev,
2709 struct ethtool_coalesce *ec)
2710{
2711 struct tun_struct *tun = netdev_priv(dev);
2712
2713 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
2714 tun->rx_batched = NAPI_POLL_WEIGHT;
2715 else
2716 tun->rx_batched = ec->rx_max_coalesced_frames;
2717
2718 return 0;
2719}
2720
Jeff Garzik7282d492006-09-13 14:30:00 -04002721static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 .get_drvinfo = tun_get_drvinfo,
2723 .get_msglevel = tun_get_msglevel,
2724 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002725 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002726 .get_ts_info = ethtool_op_get_ts_info,
Jason Wang5503fce2017-01-18 15:02:03 +08002727 .get_coalesce = tun_get_coalesce,
2728 .set_coalesce = tun_set_coalesce,
Philippe Reynes29ccc492017-03-11 22:03:50 +01002729 .get_link_ksettings = tun_get_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730};
2731
Jason Wang1576d982016-06-30 14:45:36 +08002732static int tun_queue_resize(struct tun_struct *tun)
2733{
2734 struct net_device *dev = tun->dev;
2735 struct tun_file *tfile;
2736 struct skb_array **arrays;
2737 int n = tun->numqueues + tun->numdisabled;
2738 int ret, i;
2739
stephen hemminger12039042017-08-15 10:29:16 -07002740 arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL);
Jason Wang1576d982016-06-30 14:45:36 +08002741 if (!arrays)
2742 return -ENOMEM;
2743
2744 for (i = 0; i < tun->numqueues; i++) {
2745 tfile = rtnl_dereference(tun->tfiles[i]);
2746 arrays[i] = &tfile->tx_array;
2747 }
2748 list_for_each_entry(tfile, &tun->disabled, next)
2749 arrays[i++] = &tfile->tx_array;
2750
2751 ret = skb_array_resize_multiple(arrays, n,
2752 dev->tx_queue_len, GFP_KERNEL);
2753
2754 kfree(arrays);
2755 return ret;
2756}
2757
2758static int tun_device_event(struct notifier_block *unused,
2759 unsigned long event, void *ptr)
2760{
2761 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2762 struct tun_struct *tun = netdev_priv(dev);
2763
Craig Gallek86dfb4ac2016-07-06 18:44:20 -04002764 if (dev->rtnl_link_ops != &tun_link_ops)
2765 return NOTIFY_DONE;
2766
Jason Wang1576d982016-06-30 14:45:36 +08002767 switch (event) {
2768 case NETDEV_CHANGE_TX_QUEUE_LEN:
2769 if (tun_queue_resize(tun))
2770 return NOTIFY_BAD;
2771 break;
2772 default:
2773 break;
2774 }
2775
2776 return NOTIFY_DONE;
2777}
2778
2779static struct notifier_block tun_notifier_block __read_mostly = {
2780 .notifier_call = tun_device_event,
2781};
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783static int __init tun_init(void)
2784{
2785 int ret = 0;
2786
Joe Perches6b8a66e2011-03-02 07:18:10 +00002787 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002789 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002790 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002791 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002792 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002793 }
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002796 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002797 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002798 goto err_misc;
2799 }
Jason Wang1576d982016-06-30 14:45:36 +08002800
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07002801 ret = register_netdevice_notifier(&tun_notifier_block);
2802 if (ret) {
2803 pr_err("Can't register netdevice notifier\n");
2804 goto err_notifier;
2805 }
2806
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002807 return 0;
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07002808
2809err_notifier:
2810 misc_deregister(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002811err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002812 rtnl_link_unregister(&tun_link_ops);
2813err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return ret;
2815}
2816
2817static void tun_cleanup(void)
2818{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002819 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002820 rtnl_link_unregister(&tun_link_ops);
Jason Wang1576d982016-06-30 14:45:36 +08002821 unregister_netdevice_notifier(&tun_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002824/* Get an underlying socket object from tun file. Returns error unless file is
2825 * attached to a device. The returned object works like a packet socket, it
2826 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2827 * holding a reference to the file for as long as the socket is in use. */
2828struct socket *tun_get_socket(struct file *file)
2829{
Jason Wang6e914fc2012-10-31 19:45:58 +00002830 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002831 if (file->f_op != &tun_fops)
2832 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002833 tfile = file->private_data;
2834 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002835 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002836 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002837}
2838EXPORT_SYMBOL_GPL(tun_get_socket);
2839
Jason Wang83339c62017-05-17 12:14:41 +08002840struct skb_array *tun_get_skb_array(struct file *file)
2841{
2842 struct tun_file *tfile;
2843
2844 if (file->f_op != &tun_fops)
2845 return ERR_PTR(-EINVAL);
2846 tfile = file->private_data;
2847 if (!tfile)
2848 return ERR_PTR(-EBADFD);
2849 return &tfile->tx_array;
2850}
2851EXPORT_SYMBOL_GPL(tun_get_skb_array);
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853module_init(tun_init);
2854module_exit(tun_cleanup);
2855MODULE_DESCRIPTION(DRV_DESCRIPTION);
2856MODULE_AUTHOR(DRV_COPYRIGHT);
2857MODULE_LICENSE("GPL");
2858MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002859MODULE_ALIAS("devname:net/tun");