blob: f16407242b189124c8c869970a6c758414bdd766 [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
Jason Wang7df13212017-09-04 11:36:08 +0800111#define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
Jason Wang66ccbc92017-08-11 19:41:16 +0800112
Michael S. Tsirkin031f5e02014-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. Tsirkin031f5e02014-11-19 14:44:40 +0200122
123#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Petar Penkov94317092017-09-22 13:49:14 -0700124 IFF_MULTI_QUEUE | IFF_NAPI)
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 };
Petar Penkov94317092017-09-22 13:49:14 -0700175 struct napi_struct napi;
Jason Wang4008e972012-12-13 23:53:30 +0000176 struct list_head next;
177 struct tun_struct *detached;
Jason Wang1576d982016-06-30 14:45:36 +0800178 struct skb_array tx_array;
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
Petar Penkov94317092017-09-22 13:49:14 -0700233static int tun_napi_receive(struct napi_struct *napi, int budget)
234{
235 struct tun_file *tfile = container_of(napi, struct tun_file, napi);
236 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
237 struct sk_buff_head process_queue;
238 struct sk_buff *skb;
239 int received = 0;
240
241 __skb_queue_head_init(&process_queue);
242
243 spin_lock(&queue->lock);
244 skb_queue_splice_tail_init(queue, &process_queue);
245 spin_unlock(&queue->lock);
246
247 while (received < budget && (skb = __skb_dequeue(&process_queue))) {
248 napi_gro_receive(napi, skb);
249 ++received;
250 }
251
252 if (!skb_queue_empty(&process_queue)) {
253 spin_lock(&queue->lock);
254 skb_queue_splice(&process_queue, queue);
255 spin_unlock(&queue->lock);
256 }
257
258 return received;
259}
260
261static int tun_napi_poll(struct napi_struct *napi, int budget)
262{
263 unsigned int received;
264
265 received = tun_napi_receive(napi, budget);
266
267 if (received < budget)
268 napi_complete_done(napi, received);
269
270 return received;
271}
272
273static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
274 bool napi_en)
275{
276 if (napi_en) {
277 netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
278 NAPI_POLL_WEIGHT);
279 napi_enable(&tfile->napi);
280 }
281}
282
283static void tun_napi_disable(struct tun_struct *tun, struct tun_file *tfile)
284{
285 if (tun->flags & IFF_NAPI)
286 napi_disable(&tfile->napi);
287}
288
289static void tun_napi_del(struct tun_struct *tun, struct tun_file *tfile)
290{
291 if (tun->flags & IFF_NAPI)
292 netif_napi_del(&tfile->napi);
293}
294
Greg Kurz8b8e6582015-04-24 14:50:36 +0200295#ifdef CONFIG_TUN_VNET_CROSS_LE
296static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
297{
298 return tun->flags & TUN_VNET_BE ? false :
299 virtio_legacy_is_little_endian();
300}
301
302static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
303{
304 int be = !!(tun->flags & TUN_VNET_BE);
305
306 if (put_user(be, argp))
307 return -EFAULT;
308
309 return 0;
310}
311
312static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
313{
314 int be;
315
316 if (get_user(be, argp))
317 return -EFAULT;
318
319 if (be)
320 tun->flags |= TUN_VNET_BE;
321 else
322 tun->flags &= ~TUN_VNET_BE;
323
324 return 0;
325}
326#else
327static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
328{
329 return virtio_legacy_is_little_endian();
330}
331
332static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
333{
334 return -EINVAL;
335}
336
337static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
338{
339 return -EINVAL;
340}
341#endif /* CONFIG_TUN_VNET_CROSS_LE */
342
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200343static inline bool tun_is_little_endian(struct tun_struct *tun)
344{
Greg Kurz7d824102015-04-24 14:26:24 +0200345 return tun->flags & TUN_VNET_LE ||
Greg Kurz8b8e6582015-04-24 14:50:36 +0200346 tun_legacy_is_little_endian(tun);
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200347}
348
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300349static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
350{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200351 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300352}
353
354static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
355{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200356 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300357}
358
Jason Wang96442e422012-10-31 19:46:02 +0000359static inline u32 tun_hashfn(u32 rxhash)
360{
361 return rxhash & 0x3ff;
362}
363
364static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
365{
366 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000367
Sasha Levinb67bfe02013-02-27 17:06:00 -0800368 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000369 if (e->rxhash == rxhash)
370 return e;
371 }
372 return NULL;
373}
374
375static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
376 struct hlist_head *head,
377 u32 rxhash, u16 queue_index)
378{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000379 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
380
Jason Wang96442e422012-10-31 19:46:02 +0000381 if (e) {
382 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
383 rxhash, queue_index);
384 e->updated = jiffies;
385 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800386 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000387 e->queue_index = queue_index;
388 e->tun = tun;
389 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000390 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000391 }
392 return e;
393}
394
Jason Wang96442e422012-10-31 19:46:02 +0000395static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
396{
397 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
398 e->rxhash, e->queue_index);
399 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000400 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000401 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000402}
403
404static void tun_flow_flush(struct tun_struct *tun)
405{
406 int i;
407
408 spin_lock_bh(&tun->lock);
409 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
410 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800411 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000412
Sasha Levinb67bfe02013-02-27 17:06:00 -0800413 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000414 tun_flow_delete(tun, e);
415 }
416 spin_unlock_bh(&tun->lock);
417}
418
419static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
420{
421 int i;
422
423 spin_lock_bh(&tun->lock);
424 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
425 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800426 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000427
Sasha Levinb67bfe02013-02-27 17:06:00 -0800428 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000429 if (e->queue_index == queue_index)
430 tun_flow_delete(tun, e);
431 }
432 }
433 spin_unlock_bh(&tun->lock);
434}
435
436static void tun_flow_cleanup(unsigned long data)
437{
438 struct tun_struct *tun = (struct tun_struct *)data;
439 unsigned long delay = tun->ageing_time;
440 unsigned long next_timer = jiffies + delay;
441 unsigned long count = 0;
442 int i;
443
444 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
445
446 spin_lock_bh(&tun->lock);
447 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
448 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800449 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000450
Sasha Levinb67bfe02013-02-27 17:06:00 -0800451 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000452 unsigned long this_timer;
453 count++;
454 this_timer = e->updated + delay;
455 if (time_before_eq(this_timer, jiffies))
456 tun_flow_delete(tun, e);
457 else if (time_before(this_timer, next_timer))
458 next_timer = this_timer;
459 }
460 }
461
462 if (count)
463 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
464 spin_unlock_bh(&tun->lock);
465}
466
Eric Dumazet49974422012-12-12 19:22:57 +0000467static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000468 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000469{
470 struct hlist_head *head;
471 struct tun_flow_entry *e;
472 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000473 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000474
475 if (!rxhash)
476 return;
477 else
478 head = &tun->flows[tun_hashfn(rxhash)];
479
480 rcu_read_lock();
481
Jason Wang9e857222013-01-28 01:05:19 +0000482 /* We may get a very small possibility of OOO during switching, not
483 * worth to optimize.*/
484 if (tun->numqueues == 1 || tfile->detached)
Jason Wang96442e422012-10-31 19:46:02 +0000485 goto unlock;
486
487 e = tun_flow_find(head, rxhash);
488 if (likely(e)) {
489 /* TODO: keep queueing to old queue until it's empty? */
490 e->queue_index = queue_index;
491 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800492 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000493 } else {
494 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000495 if (!tun_flow_find(head, rxhash) &&
496 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000497 tun_flow_create(tun, head, rxhash, queue_index);
498
499 if (!timer_pending(&tun->flow_gc_timer))
500 mod_timer(&tun->flow_gc_timer,
501 round_jiffies_up(jiffies + delay));
502 spin_unlock_bh(&tun->lock);
503 }
504
505unlock:
506 rcu_read_unlock();
507}
508
Tom Herbert9bc88932013-12-22 18:54:32 +0800509/**
510 * Save the hash received in the stack receive path and update the
511 * flow_hash table accordingly.
512 */
513static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
514{
Eric Dumazet567e4b72015-02-06 12:59:01 -0800515 if (unlikely(e->rps_rxhash != hash))
Tom Herbert9bc88932013-12-22 18:54:32 +0800516 e->rps_rxhash = hash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800517}
518
Jason Wangc8d68e62012-10-31 19:46:00 +0000519/* We try to identify a flow through its rxhash first. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800520 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000521 * the rxq based on the txq where the last packet of the flow comes. As
522 * the userspace application move between processors, we may get a
523 * different rxq no. here. If we could not get rxhash, then we would
524 * hope the rxq no. may help here.
525 */
Jason Wangf663dd92014-01-10 16:18:26 +0800526static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100527 void *accel_priv, select_queue_fallback_t fallback)
Jason Wangc8d68e62012-10-31 19:46:00 +0000528{
529 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000530 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000531 u32 txq = 0;
532 u32 numqueues = 0;
533
534 rcu_read_lock();
Jason Wang92bb73e2013-06-05 16:44:57 +0800535 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000536
Jason Wangfeec0842017-06-06 14:09:49 +0800537 txq = __skb_get_hash_symmetric(skb);
Jason Wangc8d68e62012-10-31 19:46:00 +0000538 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000539 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
Tom Herbert9bc88932013-12-22 18:54:32 +0800540 if (e) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800541 tun_flow_save_rps_rxhash(e, txq);
Zhi Yong Wufbe4d452014-01-02 13:24:28 +0800542 txq = e->queue_index;
Tom Herbert9bc88932013-12-22 18:54:32 +0800543 } else
Jason Wang96442e422012-10-31 19:46:02 +0000544 /* use multiply and shift instead of expensive divide */
545 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000546 } else if (likely(skb_rx_queue_recorded(skb))) {
547 txq = skb_get_rx_queue(skb);
548 while (unlikely(txq >= numqueues))
549 txq -= numqueues;
550 }
551
552 rcu_read_unlock();
553 return txq;
554}
555
Jason Wangcde8b152012-10-31 19:46:01 +0000556static inline bool tun_not_capable(struct tun_struct *tun)
557{
558 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000559 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000560
561 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
562 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000563 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000564}
565
Jason Wangc8d68e62012-10-31 19:46:00 +0000566static void tun_set_real_num_queues(struct tun_struct *tun)
567{
568 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
569 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
570}
571
Jason Wang4008e972012-12-13 23:53:30 +0000572static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
573{
574 tfile->detached = tun;
575 list_add_tail(&tfile->next, &tun->disabled);
576 ++tun->numdisabled;
577}
578
Jason Wangd32649d2012-12-18 11:00:27 +0800579static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000580{
581 struct tun_struct *tun = tfile->detached;
582
583 tfile->detached = NULL;
584 list_del_init(&tfile->next);
585 --tun->numdisabled;
586 return tun;
587}
588
Jason Wang4bfb0512013-09-05 17:53:59 +0800589static void tun_queue_purge(struct tun_file *tfile)
590{
Jason Wang1576d982016-06-30 14:45:36 +0800591 struct sk_buff *skb;
592
593 while ((skb = skb_array_consume(&tfile->tx_array)) != NULL)
594 kfree_skb(skb);
595
Jason Wang5503fce2017-01-18 15:02:03 +0800596 skb_queue_purge(&tfile->sk.sk_write_queue);
Jason Wang4bfb0512013-09-05 17:53:59 +0800597 skb_queue_purge(&tfile->sk.sk_error_queue);
598}
599
Jason Wangc8d68e62012-10-31 19:46:00 +0000600static void __tun_detach(struct tun_file *tfile, bool clean)
601{
602 struct tun_file *ntfile;
603 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000604
Jason Wangb8deabd2013-01-11 16:59:32 +0000605 tun = rtnl_dereference(tfile->tun);
606
Petar Penkov94317092017-09-22 13:49:14 -0700607 if (tun && clean) {
608 tun_napi_disable(tun, tfile);
609 tun_napi_del(tun, tfile);
610 }
611
Jason Wang9e857222013-01-28 01:05:19 +0000612 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000613 u16 index = tfile->queue_index;
614 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000615
616 rcu_assign_pointer(tun->tfiles[index],
617 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000618 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000619 ntfile->queue_index = index;
620
621 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000622 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530623 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000624 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000625 } else
Jason Wang4008e972012-12-13 23:53:30 +0000626 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000627
628 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000629 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000630 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800631 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000632 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000633 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000634 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000635 sock_put(&tfile->sk);
636 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000637
638 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000639 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
640 netif_carrier_off(tun->dev);
641
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200642 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000643 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000644 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000645 }
Jason Wang1576d982016-06-30 14:45:36 +0800646 if (tun)
647 skb_array_cleanup(&tfile->tx_array);
Eric W. Biederman140e807da2015-05-08 21:07:08 -0500648 sock_put(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000649 }
650}
651
652static void tun_detach(struct tun_file *tfile, bool clean)
653{
654 rtnl_lock();
655 __tun_detach(tfile, clean);
656 rtnl_unlock();
657}
658
659static void tun_detach_all(struct net_device *dev)
660{
661 struct tun_struct *tun = netdev_priv(dev);
Jason Wang761876c2017-08-11 19:41:18 +0800662 struct bpf_prog *xdp_prog = rtnl_dereference(tun->xdp_prog);
Jason Wang4008e972012-12-13 23:53:30 +0000663 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000664 int i, n = tun->numqueues;
665
666 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000667 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000668 BUG_ON(!tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700669 tun_napi_disable(tun, tfile);
Jason Wangaddf8fc2016-05-19 13:36:51 +0800670 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700671 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530672 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000673 --tun->numqueues;
674 }
Jason Wang9e857222013-01-28 01:05:19 +0000675 list_for_each_entry(tfile, &tun->disabled, next) {
Jason Wangaddf8fc2016-05-19 13:36:51 +0800676 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700677 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530678 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000679 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000680 BUG_ON(tun->numqueues != 0);
681
682 synchronize_net();
683 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000684 tfile = rtnl_dereference(tun->tfiles[i]);
Petar Penkov94317092017-09-22 13:49:14 -0700685 tun_napi_del(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000686 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800687 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000688 sock_put(&tfile->sk);
689 }
Jason Wang4008e972012-12-13 23:53:30 +0000690 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
691 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800692 tun_queue_purge(tfile);
Jason Wang4008e972012-12-13 23:53:30 +0000693 sock_put(&tfile->sk);
694 }
695 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000696
Jason Wang761876c2017-08-11 19:41:18 +0800697 if (xdp_prog)
698 bpf_prog_put(xdp_prog);
699
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200700 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000701 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000702}
703
Petar Penkov94317092017-09-22 13:49:14 -0700704static int tun_attach(struct tun_struct *tun, struct file *file,
705 bool skip_filter, bool napi)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000706{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000707 struct tun_file *tfile = file->private_data;
Jason Wang1576d982016-06-30 14:45:36 +0800708 struct net_device *dev = tun->dev;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000709 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000710
Paul Moore5dbbaf22013-01-14 07:12:19 +0000711 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
712 if (err < 0)
713 goto out;
714
Eric W. Biederman38231b72009-01-20 11:02:28 +0000715 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000716 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000717 goto out;
718
719 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200720 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000721 goto out;
722
723 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000724 if (!tfile->detached &&
725 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000726 goto out;
727
728 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000729
stephen hemminger92d4ea62013-12-05 20:42:58 -0800730 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400731 if (!skip_filter && (tun->filter_attached == true)) {
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +0200732 lock_sock(tfile->socket.sk);
733 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
734 release_sock(tfile->socket.sk);
Jason Wang54f968d2012-10-31 19:45:57 +0000735 if (!err)
736 goto out;
737 }
Jason Wang1576d982016-06-30 14:45:36 +0800738
739 if (!tfile->detached &&
740 skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) {
741 err = -ENOMEM;
742 goto out;
743 }
744
Jason Wangc8d68e62012-10-31 19:46:00 +0000745 tfile->queue_index = tun->numqueues;
Jason Wangaddf8fc2016-05-19 13:36:51 +0800746 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
Jason Wang6e914fc2012-10-31 19:45:58 +0000747 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000748 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000749 tun->numqueues++;
750
Petar Penkov94317092017-09-22 13:49:14 -0700751 if (tfile->detached) {
Jason Wang4008e972012-12-13 23:53:30 +0000752 tun_enable_queue(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700753 } else {
Jason Wang4008e972012-12-13 23:53:30 +0000754 sock_hold(&tfile->sk);
Petar Penkov94317092017-09-22 13:49:14 -0700755 tun_napi_init(tun, tfile, napi);
756 }
Jason Wang4008e972012-12-13 23:53:30 +0000757
Jason Wangc8d68e62012-10-31 19:46:00 +0000758 tun_set_real_num_queues(tun);
759
Jason Wangc8d68e62012-10-31 19:46:00 +0000760 /* device is allowed to go away first, so no need to hold extra
761 * refcnt.
762 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000763
Eric W. Biederman38231b72009-01-20 11:02:28 +0000764out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000765 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000766}
767
Eric W. Biederman631ab462009-01-20 11:00:40 +0000768static struct tun_struct *__tun_get(struct tun_file *tfile)
769{
Jason Wang6e914fc2012-10-31 19:45:58 +0000770 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000771
Jason Wang6e914fc2012-10-31 19:45:58 +0000772 rcu_read_lock();
773 tun = rcu_dereference(tfile->tun);
774 if (tun)
775 dev_hold(tun->dev);
776 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000777
778 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000779}
780
781static struct tun_struct *tun_get(struct file *file)
782{
783 return __tun_get(file->private_data);
784}
785
786static void tun_put(struct tun_struct *tun)
787{
Jason Wang6e914fc2012-10-31 19:45:58 +0000788 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000789}
790
Joe Perches6b8a66e2011-03-02 07:18:10 +0000791/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700792static void addr_hash_set(u32 *mask, const u8 *addr)
793{
794 int n = ether_crc(ETH_ALEN, addr) >> 26;
795 mask[n >> 5] |= (1 << (n & 31));
796}
797
798static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
799{
800 int n = ether_crc(ETH_ALEN, addr) >> 26;
801 return mask[n >> 5] & (1 << (n & 31));
802}
803
804static int update_filter(struct tap_filter *filter, void __user *arg)
805{
806 struct { u8 u[ETH_ALEN]; } *addr;
807 struct tun_filter uf;
808 int err, alen, n, nexact;
809
810 if (copy_from_user(&uf, arg, sizeof(uf)))
811 return -EFAULT;
812
813 if (!uf.count) {
814 /* Disabled */
815 filter->count = 0;
816 return 0;
817 }
818
819 alen = ETH_ALEN * uf.count;
Markus Elfring28e81902016-08-20 08:54:15 +0200820 addr = memdup_user(arg + sizeof(uf), alen);
821 if (IS_ERR(addr))
822 return PTR_ERR(addr);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700823
824 /* The filter is updated without holding any locks. Which is
825 * perfectly safe. We disable it first and in the worst
826 * case we'll accept a few undesired packets. */
827 filter->count = 0;
828 wmb();
829
830 /* Use first set of addresses as an exact filter */
831 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
832 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
833
834 nexact = n;
835
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800836 /* Remaining multicast addresses are hashed,
837 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700838 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800839 for (; n < uf.count; n++) {
840 if (!is_multicast_ether_addr(addr[n].u)) {
841 err = 0; /* no filter */
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200842 goto free_addr;
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800843 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700844 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800845 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700846
847 /* For ALLMULTI just set the mask to all ones.
848 * This overrides the mask populated above. */
849 if ((uf.flags & TUN_FLT_ALLMULTI))
850 memset(filter->mask, ~0, sizeof(filter->mask));
851
852 /* Now enable the filter */
853 wmb();
854 filter->count = nexact;
855
856 /* Return the number of exact filters */
857 err = nexact;
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200858free_addr:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700859 kfree(addr);
860 return err;
861}
862
863/* Returns: 0 - drop, !=0 - accept */
864static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
865{
866 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
867 * at this point. */
868 struct ethhdr *eh = (struct ethhdr *) skb->data;
869 int i;
870
871 /* Exact match */
872 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000873 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700874 return 1;
875
876 /* Inexact match (multicast only) */
877 if (is_multicast_ether_addr(eh->h_dest))
878 return addr_hash_test(filter->mask, eh->h_dest);
879
880 return 0;
881}
882
883/*
884 * Checks whether the packet is accepted or not.
885 * Returns: 0 - drop, !=0 - accept
886 */
887static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
888{
889 if (!filter->count)
890 return 1;
891
892 return run_filter(filter, skb);
893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/* Network device part of the driver */
896
Jeff Garzik7282d492006-09-13 14:30:00 -0400897static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000899/* Net device detach from fd. */
900static void tun_net_uninit(struct net_device *dev)
901{
Jason Wangc8d68e62012-10-31 19:46:00 +0000902 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/* Net device open. */
906static int tun_net_open(struct net_device *dev)
907{
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +0100908 struct tun_struct *tun = netdev_priv(dev);
909 int i;
910
Jason Wangc8d68e62012-10-31 19:46:00 +0000911 netif_tx_start_all_queues(dev);
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +0100912
913 for (i = 0; i < tun->numqueues; i++) {
914 struct tun_file *tfile;
915
916 tfile = rtnl_dereference(tun->tfiles[i]);
917 tfile->socket.sk->sk_write_space(tfile->socket.sk);
918 }
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921}
922
923/* Net device close. */
924static int tun_net_close(struct net_device *dev)
925{
Jason Wangc8d68e62012-10-31 19:46:00 +0000926 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return 0;
928}
929
930/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000931static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000934 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000935 struct tun_file *tfile;
Dominic Curranfa358642014-01-22 03:03:23 +0000936 u32 numqueues = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Jason Wang6e914fc2012-10-31 19:45:58 +0000938 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000939 tfile = rcu_dereference(tun->tfiles[txq]);
Dominic Curranfa358642014-01-22 03:03:23 +0000940 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* Drop packet if interface is not attached */
Dominic Curranfa358642014-01-22 03:03:23 +0000943 if (txq >= numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto drop;
945
Jason Wang3df97ba2016-04-25 23:13:42 -0400946#ifdef CONFIG_RPS
947 if (numqueues == 1 && static_key_false(&rps_needed)) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800948 /* Select queue was not called for the skbuff, so we extract the
949 * RPS hash and save it into the flow_table here.
950 */
951 __u32 rxhash;
952
Jason Wangfeec0842017-06-06 14:09:49 +0800953 rxhash = __skb_get_hash_symmetric(skb);
Tom Herbert9bc88932013-12-22 18:54:32 +0800954 if (rxhash) {
955 struct tun_flow_entry *e;
956 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
957 rxhash);
958 if (e)
959 tun_flow_save_rps_rxhash(e, rxhash);
960 }
961 }
Jason Wang3df97ba2016-04-25 23:13:42 -0400962#endif
Tom Herbert9bc88932013-12-22 18:54:32 +0800963
Jason Wang6e914fc2012-10-31 19:45:58 +0000964 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
965
Jason Wangc8d68e62012-10-31 19:46:00 +0000966 BUG_ON(!tfile);
967
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700968 /* Drop if the filter does not like it.
969 * This is a noop if the filter is disabled.
970 * Filter can be enabled only for the TAP devices. */
971 if (!check_filter(&tun->txflt, skb))
972 goto drop;
973
Jason Wang54f968d2012-10-31 19:45:57 +0000974 if (tfile->socket.sk->sk_filter &&
975 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000976 goto drop;
977
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400978 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Jason Wang7bf66302013-09-05 17:54:00 +0800979 goto drop;
980
Soheil Hassas Yeganeh7b996242016-08-23 18:22:33 -0400981 skb_tx_timestamp(skb);
Richard Cochraneda29772013-07-19 19:40:10 +0200982
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000983 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800984 * for indefinite time.
985 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000986 skb_orphan(skb);
987
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000988 nf_reset(skb);
989
Jason Wang1576d982016-06-30 14:45:36 +0800990 if (skb_array_produce(&tfile->tx_array, skb))
991 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000994 if (tfile->flags & TUN_FASYNC)
995 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -0700996 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +0000997
998 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000999 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001drop:
Paolo Abeni608b9972016-04-13 10:52:20 +02001002 this_cpu_inc(tun->pcpu_stats->tx_dropped);
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +00001003 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +00001005 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +08001006 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001009static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001011 /*
1012 * This callback is supposed to deal with mc filter in
1013 * _rx_ path and has nothing to do with the _tx_ path.
1014 * In rx path we always accept everything userspace gives us.
1015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001018static netdev_features_t tun_net_fix_features(struct net_device *dev,
1019 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +00001020{
1021 struct tun_struct *tun = netdev_priv(dev);
1022
1023 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1024}
Neil Hormanbebd0972011-06-15 05:25:01 +00001025#ifdef CONFIG_NET_POLL_CONTROLLER
1026static void tun_poll_controller(struct net_device *dev)
1027{
1028 /*
1029 * Tun only receives frames when:
1030 * 1) the char device endpoint gets data from user space
1031 * 2) the tun socket gets a sendmsg call from user space
Petar Penkov94317092017-09-22 13:49:14 -07001032 * If NAPI is not enabled, since both of those are synchronous
1033 * operations, we are guaranteed never to have pending data when we poll
1034 * for it so there is nothing to do here but return.
Neil Hormanbebd0972011-06-15 05:25:01 +00001035 * We need this though so netpoll recognizes us as an interface that
1036 * supports polling, which enables bridge devices in virt setups to
1037 * still use netconsole
Petar Penkov94317092017-09-22 13:49:14 -07001038 * If NAPI is enabled, however, we need to schedule polling for all
1039 * queues.
Neil Hormanbebd0972011-06-15 05:25:01 +00001040 */
Petar Penkov94317092017-09-22 13:49:14 -07001041 struct tun_struct *tun = netdev_priv(dev);
1042
1043 if (tun->flags & IFF_NAPI) {
1044 struct tun_file *tfile;
1045 int i;
1046
1047 rcu_read_lock();
1048 for (i = 0; i < tun->numqueues; i++) {
1049 tfile = rcu_dereference(tun->tfiles[i]);
1050 napi_schedule(&tfile->napi);
1051 }
1052 rcu_read_unlock();
1053 }
Neil Hormanbebd0972011-06-15 05:25:01 +00001054 return;
1055}
1056#endif
Paolo Abenieaea34b2016-02-26 10:45:40 +01001057
1058static void tun_set_headroom(struct net_device *dev, int new_hr)
1059{
1060 struct tun_struct *tun = netdev_priv(dev);
1061
1062 if (new_hr < NET_SKB_PAD)
1063 new_hr = NET_SKB_PAD;
1064
1065 tun->align = new_hr;
1066}
1067
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001068static void
Paolo Abeni608b9972016-04-13 10:52:20 +02001069tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1070{
1071 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
1072 struct tun_struct *tun = netdev_priv(dev);
1073 struct tun_pcpu_stats *p;
1074 int i;
1075
1076 for_each_possible_cpu(i) {
1077 u64 rxpackets, rxbytes, txpackets, txbytes;
1078 unsigned int start;
1079
1080 p = per_cpu_ptr(tun->pcpu_stats, i);
1081 do {
1082 start = u64_stats_fetch_begin(&p->syncp);
1083 rxpackets = p->rx_packets;
1084 rxbytes = p->rx_bytes;
1085 txpackets = p->tx_packets;
1086 txbytes = p->tx_bytes;
1087 } while (u64_stats_fetch_retry(&p->syncp, start));
1088
1089 stats->rx_packets += rxpackets;
1090 stats->rx_bytes += rxbytes;
1091 stats->tx_packets += txpackets;
1092 stats->tx_bytes += txbytes;
1093
1094 /* u32 counters */
1095 rx_dropped += p->rx_dropped;
1096 rx_frame_errors += p->rx_frame_errors;
1097 tx_dropped += p->tx_dropped;
1098 }
1099 stats->rx_dropped = rx_dropped;
1100 stats->rx_frame_errors = rx_frame_errors;
1101 stats->tx_dropped = tx_dropped;
Paolo Abeni608b9972016-04-13 10:52:20 +02001102}
1103
Jason Wang761876c2017-08-11 19:41:18 +08001104static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1105 struct netlink_ext_ack *extack)
1106{
1107 struct tun_struct *tun = netdev_priv(dev);
1108 struct bpf_prog *old_prog;
1109
1110 old_prog = rtnl_dereference(tun->xdp_prog);
1111 rcu_assign_pointer(tun->xdp_prog, prog);
1112 if (old_prog)
1113 bpf_prog_put(old_prog);
1114
1115 return 0;
1116}
1117
1118static u32 tun_xdp_query(struct net_device *dev)
1119{
1120 struct tun_struct *tun = netdev_priv(dev);
1121 const struct bpf_prog *xdp_prog;
1122
1123 xdp_prog = rtnl_dereference(tun->xdp_prog);
1124 if (xdp_prog)
1125 return xdp_prog->aux->id;
1126
1127 return 0;
1128}
1129
1130static int tun_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1131{
1132 switch (xdp->command) {
1133 case XDP_SETUP_PROG:
1134 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1135 case XDP_QUERY_PROG:
1136 xdp->prog_id = tun_xdp_query(dev);
1137 xdp->prog_attached = !!xdp->prog_id;
1138 return 0;
1139 default:
1140 return -EINVAL;
1141 }
1142}
1143
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001144static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001145 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001146 .ndo_open = tun_net_open,
1147 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001148 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001149 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +00001150 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +00001151#ifdef CONFIG_NET_POLL_CONTROLLER
1152 .ndo_poll_controller = tun_poll_controller,
1153#endif
Paolo Abenieaea34b2016-02-26 10:45:40 +01001154 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001155 .ndo_get_stats64 = tun_net_get_stats64,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001156};
1157
1158static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001159 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001160 .ndo_open = tun_net_open,
1161 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001162 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001163 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001164 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001165 .ndo_set_mac_address = eth_mac_addr,
1166 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +00001167 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +00001168#ifdef CONFIG_NET_POLL_CONTROLLER
1169 .ndo_poll_controller = tun_poll_controller,
1170#endif
Toshiaki Makita5e527962015-07-31 15:03:27 +09001171 .ndo_features_check = passthru_features_check,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001172 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001173 .ndo_get_stats64 = tun_net_get_stats64,
Jason Wang761876c2017-08-11 19:41:18 +08001174 .ndo_xdp = tun_xdp,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001175};
1176
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001177static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +00001178{
1179 int i;
1180
Jason Wang96442e422012-10-31 19:46:02 +00001181 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1182 INIT_HLIST_HEAD(&tun->flows[i]);
1183
1184 tun->ageing_time = TUN_FLOW_EXPIRE;
1185 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
1186 mod_timer(&tun->flow_gc_timer,
1187 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +00001188}
1189
1190static void tun_flow_uninit(struct tun_struct *tun)
1191{
1192 del_timer_sync(&tun->flow_gc_timer);
1193 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001194}
1195
Jarod Wilson91572082016-10-20 13:55:20 -04001196#define MIN_MTU 68
1197#define MAX_MTU 65535
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199/* Initialize net device. */
1200static void tun_net_init(struct net_device *dev)
1201{
1202 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001205 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001206 dev->netdev_ops = &tun_netdev_ops;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /* Point-to-Point TUN Device */
1209 dev->hard_header_len = 0;
1210 dev->addr_len = 0;
1211 dev->mtu = 1500;
1212
1213 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001214 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 break;
1217
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001218 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -08001219 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001221 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +00001222 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +00001223 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001225 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
1228 }
Jarod Wilson91572082016-10-20 13:55:20 -04001229
1230 dev->min_mtu = MIN_MTU;
1231 dev->max_mtu = MAX_MTU - dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234/* Character device part */
1235
1236/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +00001237static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001238{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +00001239 struct tun_file *tfile = file->private_data;
1240 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001241 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001242 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +00001245 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Jason Wang54f968d2012-10-31 19:45:57 +00001247 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001248
Joe Perches6b8a66e2011-03-02 07:18:10 +00001249 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Xi Wang9e641bd2014-05-16 15:11:48 -07001251 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001252
Jason Wang1576d982016-06-30 14:45:36 +08001253 if (!skb_array_empty(&tfile->tx_array))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 mask |= POLLIN | POLLRDNORM;
1255
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001256 if (tun->dev->flags & IFF_UP &&
1257 (sock_writeable(sk) ||
1258 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1259 sock_writeable(sk))))
Herbert Xu33dccbb2009-02-05 21:25:32 -08001260 mask |= POLLOUT | POLLWRNORM;
1261
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001262 if (tun->dev->reg_state != NETREG_REGISTERED)
1263 mask = POLLERR;
1264
Eric W. Biederman631ab462009-01-20 11:00:40 +00001265 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return mask;
1267}
1268
Rusty Russellf42157c2008-08-15 15:15:10 -07001269/* prepad is the amount to reserve at front. len is length after that.
1270 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001271static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001272 size_t prepad, size_t len,
1273 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001274{
Jason Wang54f968d2012-10-31 19:45:57 +00001275 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001276 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001277 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001278
1279 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001280 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001281 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001282
Herbert Xu33dccbb2009-02-05 21:25:32 -08001283 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001284 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001285 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001286 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001287
1288 skb_reserve(skb, prepad);
1289 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001290 skb->data_len = len - linear;
1291 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001292
1293 return skb;
1294}
1295
Jason Wang5503fce2017-01-18 15:02:03 +08001296static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1297 struct sk_buff *skb, int more)
1298{
1299 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1300 struct sk_buff_head process_queue;
1301 u32 rx_batched = tun->rx_batched;
1302 bool rcv = false;
1303
1304 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1305 local_bh_disable();
1306 netif_receive_skb(skb);
1307 local_bh_enable();
1308 return;
1309 }
1310
1311 spin_lock(&queue->lock);
1312 if (!more || skb_queue_len(queue) == rx_batched) {
1313 __skb_queue_head_init(&process_queue);
1314 skb_queue_splice_tail_init(queue, &process_queue);
1315 rcv = true;
1316 } else {
1317 __skb_queue_tail(queue, skb);
1318 }
1319 spin_unlock(&queue->lock);
1320
1321 if (rcv) {
1322 struct sk_buff *nskb;
1323
1324 local_bh_disable();
1325 while ((nskb = __skb_dequeue(&process_queue)))
1326 netif_receive_skb(nskb);
1327 netif_receive_skb(skb);
1328 local_bh_enable();
1329 }
1330}
1331
Jason Wang66ccbc92017-08-11 19:41:16 +08001332static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1333 int len, int noblock, bool zerocopy)
1334{
1335 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1336 return false;
1337
1338 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1339 return false;
1340
1341 if (!noblock)
1342 return false;
1343
1344 if (zerocopy)
1345 return false;
1346
1347 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1348 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1349 return false;
1350
1351 return true;
1352}
1353
Jason Wang761876c2017-08-11 19:41:18 +08001354static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1355 struct tun_file *tfile,
Jason Wang66ccbc92017-08-11 19:41:16 +08001356 struct iov_iter *from,
Jason Wang761876c2017-08-11 19:41:18 +08001357 struct virtio_net_hdr *hdr,
Jason Wang1cfe6e92017-09-04 11:36:09 +08001358 int len, int *skb_xdp)
Jason Wang66ccbc92017-08-11 19:41:16 +08001359{
Eric Dumazet0bbd7da2017-08-16 22:14:33 +08001360 struct page_frag *alloc_frag = &current->task_frag;
Jason Wang66ccbc92017-08-11 19:41:16 +08001361 struct sk_buff *skb;
Jason Wang761876c2017-08-11 19:41:18 +08001362 struct bpf_prog *xdp_prog;
Jason Wang7df13212017-09-04 11:36:08 +08001363 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jason Wang761876c2017-08-11 19:41:18 +08001364 unsigned int delta = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001365 char *buf;
1366 size_t copied;
Jason Wang761876c2017-08-11 19:41:18 +08001367 bool xdp_xmit = false;
Jason Wang7df13212017-09-04 11:36:08 +08001368 int err, pad = TUN_RX_PAD;
1369
1370 rcu_read_lock();
1371 xdp_prog = rcu_dereference(tun->xdp_prog);
1372 if (xdp_prog)
1373 pad += TUN_HEADROOM;
1374 buflen += SKB_DATA_ALIGN(len + pad);
1375 rcu_read_unlock();
Jason Wang66ccbc92017-08-11 19:41:16 +08001376
1377 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1378 return ERR_PTR(-ENOMEM);
1379
1380 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1381 copied = copy_page_from_iter(alloc_frag->page,
Jason Wang7df13212017-09-04 11:36:08 +08001382 alloc_frag->offset + pad,
Jason Wang66ccbc92017-08-11 19:41:16 +08001383 len, from);
1384 if (copied != len)
1385 return ERR_PTR(-EFAULT);
1386
Jason Wang7df13212017-09-04 11:36:08 +08001387 /* There's a small window that XDP may be set after the check
1388 * of xdp_prog above, this should be rare and for simplicity
1389 * we do XDP on skb in case the headroom is not enough.
1390 */
1391 if (hdr->gso_type || !xdp_prog)
Jason Wang1cfe6e92017-09-04 11:36:09 +08001392 *skb_xdp = 1;
Jason Wang761876c2017-08-11 19:41:18 +08001393 else
Jason Wang1cfe6e92017-09-04 11:36:09 +08001394 *skb_xdp = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001395
Jason Wang761876c2017-08-11 19:41:18 +08001396 rcu_read_lock();
1397 xdp_prog = rcu_dereference(tun->xdp_prog);
Jason Wang1cfe6e92017-09-04 11:36:09 +08001398 if (xdp_prog && !*skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001399 struct xdp_buff xdp;
1400 void *orig_data;
1401 u32 act;
1402
1403 xdp.data_hard_start = buf;
Jason Wang7df13212017-09-04 11:36:08 +08001404 xdp.data = buf + pad;
Jason Wang761876c2017-08-11 19:41:18 +08001405 xdp.data_end = xdp.data + len;
1406 orig_data = xdp.data;
1407 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1408
1409 switch (act) {
1410 case XDP_REDIRECT:
1411 get_page(alloc_frag->page);
1412 alloc_frag->offset += buflen;
1413 err = xdp_do_redirect(tun->dev, &xdp, xdp_prog);
1414 if (err)
1415 goto err_redirect;
1416 return NULL;
1417 case XDP_TX:
1418 xdp_xmit = true;
1419 /* fall through */
1420 case XDP_PASS:
1421 delta = orig_data - xdp.data;
1422 break;
1423 default:
1424 bpf_warn_invalid_xdp_action(act);
1425 /* fall through */
1426 case XDP_ABORTED:
1427 trace_xdp_exception(tun->dev, xdp_prog, act);
1428 /* fall through */
1429 case XDP_DROP:
1430 goto err_xdp;
1431 }
1432 }
1433
1434 skb = build_skb(buf, buflen);
1435 if (!skb) {
1436 rcu_read_unlock();
1437 return ERR_PTR(-ENOMEM);
1438 }
1439
Jason Wang7df13212017-09-04 11:36:08 +08001440 skb_reserve(skb, pad - delta);
Jason Wang761876c2017-08-11 19:41:18 +08001441 skb_put(skb, len + delta);
Jason Wang66ccbc92017-08-11 19:41:16 +08001442 get_page(alloc_frag->page);
1443 alloc_frag->offset += buflen;
1444
Jason Wang761876c2017-08-11 19:41:18 +08001445 if (xdp_xmit) {
1446 skb->dev = tun->dev;
1447 generic_xdp_tx(skb, xdp_prog);
1448 rcu_read_lock();
1449 return NULL;
1450 }
1451
1452 rcu_read_unlock();
1453
Jason Wang66ccbc92017-08-11 19:41:16 +08001454 return skb;
Jason Wang761876c2017-08-11 19:41:18 +08001455
1456err_redirect:
1457 put_page(alloc_frag->page);
1458err_xdp:
1459 rcu_read_unlock();
1460 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1461 return NULL;
Jason Wang66ccbc92017-08-11 19:41:16 +08001462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001465static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001466 void *msg_control, struct iov_iter *from,
Jason Wang5503fce2017-01-18 15:02:03 +08001467 int noblock, bool more)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Harvey Harrison09640e62009-02-01 00:45:17 -08001469 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001471 size_t total_len = iov_iter_count(from);
Paolo Abenieaea34b2016-02-26 10:45:40 +01001472 size_t len = total_len, align = tun->align, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001473 struct virtio_net_hdr gso = { 0 };
Paolo Abeni608b9972016-04-13 10:52:20 +02001474 struct tun_pcpu_stats *stats;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001475 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001476 int copylen;
1477 bool zerocopy = false;
1478 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001479 u32 rxhash;
Jason Wang1cfe6e92017-09-04 11:36:09 +08001480 int skb_xdp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Eric Dumazet1bd4978a2015-12-16 08:57:37 -08001482 if (!(tun->dev->flags & IFF_UP))
1483 return -EIO;
1484
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001485 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001486 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001488 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Al Virocbbd26b2016-11-01 22:09:04 -04001490 if (!copy_from_iter_full(&pi, sizeof(pi), from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -EFAULT;
1492 }
1493
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001494 if (tun->flags & IFF_VNET_HDR) {
Willem de Bruijne1edab82017-02-03 18:20:48 -05001495 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1496
1497 if (len < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001498 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001499 len -= vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001500
Al Virocbbd26b2016-11-01 22:09:04 -04001501 if (!copy_from_iter_full(&gso, sizeof(gso), from))
Rusty Russellf43798c2008-07-03 03:48:02 -07001502 return -EFAULT;
1503
Herbert Xu49091222009-06-08 00:20:01 -07001504 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001505 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1506 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 -07001507
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001508 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001509 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001510 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001511 }
1512
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001513 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001514 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001515 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001516 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001517 return -EINVAL;
1518 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001519
Jason Wang96f8d9e2013-11-13 14:00:39 +08001520 good_linear = SKB_MAX_HEAD(align);
1521
Jason Wang88529172013-07-18 10:55:15 +08001522 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001523 struct iov_iter i = *from;
1524
Jason Wang88529172013-07-18 10:55:15 +08001525 /* There are 256 bytes to be copied in skb, so there is
1526 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001527 * The rest of the buffer is mapped from userspace.
1528 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001529 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001530 if (copylen > good_linear)
1531 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001532 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001533 iov_iter_advance(&i, copylen);
1534 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001535 zerocopy = true;
1536 }
1537
Jason Wang66ccbc92017-08-11 19:41:16 +08001538 if (tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001539 /* For the packet that is not easy to be processed
1540 * (e.g gso or jumbo packet), we will do it at after
1541 * skb was created with generic XDP routine.
1542 */
1543 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
Jason Wang66ccbc92017-08-11 19:41:16 +08001544 if (IS_ERR(skb)) {
Paolo Abeni608b9972016-04-13 10:52:20 +02001545 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001546 return PTR_ERR(skb);
1547 }
Jason Wang761876c2017-08-11 19:41:18 +08001548 if (!skb)
1549 return total_len;
Jason Wang66ccbc92017-08-11 19:41:16 +08001550 } else {
1551 if (!zerocopy) {
1552 copylen = len;
1553 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1554 linear = good_linear;
1555 else
1556 linear = tun16_to_cpu(tun, gso.hdr_len);
1557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Jason Wang66ccbc92017-08-11 19:41:16 +08001559 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
1560 if (IS_ERR(skb)) {
1561 if (PTR_ERR(skb) != -EAGAIN)
1562 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1563 return PTR_ERR(skb);
1564 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001565
Jason Wang66ccbc92017-08-11 19:41:16 +08001566 if (zerocopy)
1567 err = zerocopy_sg_from_iter(skb, from);
1568 else
1569 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1570
1571 if (err) {
1572 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1573 kfree_skb(skb);
1574 return -EFAULT;
1575 }
Dave Jones8f227572006-03-11 18:49:13 -08001576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001578 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
Paolo Abenidf10db92016-06-14 00:00:04 +02001579 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1580 kfree_skb(skb);
1581 return -EINVAL;
1582 }
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001585 case IFF_TUN:
1586 if (tun->flags & IFF_NO_PI) {
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001587 switch (skb->data[0] & 0xf0) {
1588 case 0x40:
1589 pi.proto = htons(ETH_P_IP);
1590 break;
1591 case 0x60:
1592 pi.proto = htons(ETH_P_IPV6);
1593 break;
1594 default:
Paolo Abeni608b9972016-04-13 10:52:20 +02001595 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001596 kfree_skb(skb);
1597 return -EINVAL;
1598 }
1599 }
1600
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001601 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001603 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001605 case IFF_TAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 skb->protocol = eth_type_trans(skb, tun->dev);
1607 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001610 /* copy skb_ubuf_info for callback when skb has no error */
1611 if (zerocopy) {
1612 skb_shinfo(skb)->destructor_arg = msg_control;
1613 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001614 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangaf1cc7a2016-11-30 13:17:51 +08001615 } else if (msg_control) {
1616 struct ubuf_info *uarg = msg_control;
1617 uarg->callback(uarg, false);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001618 }
1619
Vlad Yasevich72f65102015-02-03 16:36:16 -05001620 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001621 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001622
Jason Wang1cfe6e92017-09-04 11:36:09 +08001623 if (skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001624 struct bpf_prog *xdp_prog;
1625 int ret;
1626
1627 rcu_read_lock();
1628 xdp_prog = rcu_dereference(tun->xdp_prog);
1629 if (xdp_prog) {
1630 ret = do_xdp_generic(xdp_prog, skb);
1631 if (ret != XDP_PASS) {
1632 rcu_read_unlock();
1633 return total_len;
1634 }
1635 }
1636 rcu_read_unlock();
1637 }
1638
Jason Wangfeec0842017-06-06 14:09:49 +08001639 rxhash = __skb_get_hash_symmetric(skb);
Petar Penkov94317092017-09-22 13:49:14 -07001640
1641 if (tun->flags & IFF_NAPI) {
1642 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1643 int queue_len;
1644
1645 spin_lock_bh(&queue->lock);
1646 __skb_queue_tail(queue, skb);
1647 queue_len = skb_queue_len(queue);
1648 spin_unlock(&queue->lock);
1649
1650 if (!more || queue_len > NAPI_POLL_WEIGHT)
1651 napi_schedule(&tfile->napi);
1652
1653 local_bh_enable();
1654 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
1655 tun_rx_batched(tun, tfile, skb, more);
1656 } else {
1657 netif_rx_ni(skb);
1658 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001659
Paolo Abeni608b9972016-04-13 10:52:20 +02001660 stats = get_cpu_ptr(tun->pcpu_stats);
1661 u64_stats_update_begin(&stats->syncp);
1662 stats->rx_packets++;
1663 stats->rx_bytes += len;
1664 u64_stats_update_end(&stats->syncp);
1665 put_cpu_ptr(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Jason Wang9e857222013-01-28 01:05:19 +00001667 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001668 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Al Virof5ff53b2014-06-19 15:36:49 -04001671static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001673 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -08001674 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001675 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001676 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (!tun)
1679 return -EBADFD;
1680
Jason Wang5503fce2017-01-18 15:02:03 +08001681 result = tun_get_user(tun, tfile, NULL, from,
1682 file->f_flags & O_NONBLOCK, false);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001683
1684 tun_put(tun);
1685 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001689static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001690 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001691 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08001692 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 struct tun_pi pi = { 0, skb->protocol };
Paolo Abeni608b9972016-04-13 10:52:20 +02001695 struct tun_pcpu_stats *stats;
Herbert Xue0b46d02014-11-07 21:22:23 +08001696 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08001697 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001698 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001699 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001700
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001701 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001702 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001704 if (tun->flags & IFF_VNET_HDR)
Willem de Bruijne1edab82017-02-03 18:20:48 -05001705 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Herbert Xue0b46d02014-11-07 21:22:23 +08001707 total = skb->len + vlan_hlen + vnet_hdr_sz;
1708
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001709 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001710 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return -EINVAL;
1712
Herbert Xue0b46d02014-11-07 21:22:23 +08001713 total += sizeof(pi);
1714 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* Packet will be striped */
1716 pi.flags |= TUN_PKT_STRIP;
1717 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001718
Herbert Xue0b46d02014-11-07 21:22:23 +08001719 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Herbert Xu2eb783c2014-11-03 04:30:14 +08001723 if (vnet_hdr_sz) {
Jarno Rajahalme9403cd72016-11-18 15:40:40 -08001724 struct virtio_net_hdr gso;
Mike Rapoport34166092016-06-08 16:09:20 +03001725
Herbert Xue0b46d02014-11-07 21:22:23 +08001726 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001727 return -EINVAL;
1728
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001729 if (virtio_net_hdr_from_skb(skb, &gso,
Jason Wang6391a442017-01-20 14:32:42 +08001730 tun_is_little_endian(tun), true)) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001731 struct skb_shared_info *sinfo = skb_shinfo(skb);
Mike Rapoport34166092016-06-08 16:09:20 +03001732 pr_err("unexpected GSO type: "
1733 "0x%x, gso_size %d, hdr_len %d\n",
1734 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1735 tun16_to_cpu(tun, gso.hdr_len));
1736 print_hex_dump(KERN_ERR, "tun: ",
1737 DUMP_PREFIX_NONE,
1738 16, 1, skb->head,
1739 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1740 WARN_ON_ONCE(1);
1741 return -EINVAL;
1742 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001743
Herbert Xue0b46d02014-11-07 21:22:23 +08001744 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07001745 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08001746
1747 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001748 }
1749
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001750 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001751 int ret;
Jason Wang6680ec62013-07-25 13:00:33 +08001752 struct {
1753 __be16 h_vlan_proto;
1754 __be16 h_vlan_TCI;
1755 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Jason Wang6680ec62013-07-25 13:00:33 +08001757 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001758 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Jason Wang6680ec62013-07-25 13:00:33 +08001760 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08001761
Herbert Xue0b46d02014-11-07 21:22:23 +08001762 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1763 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001764 goto done;
1765
Herbert Xue0b46d02014-11-07 21:22:23 +08001766 ret = copy_to_iter(&veth, sizeof(veth), iter);
1767 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001768 goto done;
1769 }
1770
Herbert Xue0b46d02014-11-07 21:22:23 +08001771 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08001772
1773done:
Paolo Abeni608b9972016-04-13 10:52:20 +02001774 /* caller is in process context, */
1775 stats = get_cpu_ptr(tun->pcpu_stats);
1776 u64_stats_update_begin(&stats->syncp);
1777 stats->tx_packets++;
1778 stats->tx_bytes += skb->len + vlan_hlen;
1779 u64_stats_update_end(&stats->syncp);
1780 put_cpu_ptr(tun->pcpu_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 return total;
1783}
1784
Jason Wang1576d982016-06-30 14:45:36 +08001785static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock,
1786 int *err)
1787{
1788 DECLARE_WAITQUEUE(wait, current);
1789 struct sk_buff *skb = NULL;
Jason Wangf48cc6b2016-07-04 13:53:38 +08001790 int error = 0;
Jason Wang1576d982016-06-30 14:45:36 +08001791
1792 skb = skb_array_consume(&tfile->tx_array);
1793 if (skb)
1794 goto out;
1795 if (noblock) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001796 error = -EAGAIN;
Jason Wang1576d982016-06-30 14:45:36 +08001797 goto out;
1798 }
1799
1800 add_wait_queue(&tfile->wq.wait, &wait);
1801 current->state = TASK_INTERRUPTIBLE;
1802
1803 while (1) {
1804 skb = skb_array_consume(&tfile->tx_array);
1805 if (skb)
1806 break;
1807 if (signal_pending(current)) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001808 error = -ERESTARTSYS;
Jason Wang1576d982016-06-30 14:45:36 +08001809 break;
1810 }
1811 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001812 error = -EFAULT;
Jason Wang1576d982016-06-30 14:45:36 +08001813 break;
1814 }
1815
1816 schedule();
1817 }
1818
1819 current->state = TASK_RUNNING;
1820 remove_wait_queue(&tfile->wq.wait, &wait);
1821
1822out:
Jason Wangf48cc6b2016-07-04 13:53:38 +08001823 *err = error;
Jason Wang1576d982016-06-30 14:45:36 +08001824 return skb;
1825}
1826
Jason Wang54f968d2012-10-31 19:45:57 +00001827static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05001828 struct iov_iter *to,
Jason Wangac77cfd2017-05-17 12:14:43 +08001829 int noblock, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Al Viro9b067032014-11-07 13:52:07 -05001831 ssize_t ret;
Jason Wang1576d982016-06-30 14:45:36 +08001832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Rami Rosen3872baf2012-11-25 22:07:41 +00001834 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Al Viro9b067032014-11-07 13:52:07 -05001836 if (!iov_iter_count(to))
1837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Jason Wangac77cfd2017-05-17 12:14:43 +08001839 if (!skb) {
1840 /* Read frames from ring */
1841 skb = tun_ring_recv(tfile, noblock, &err);
1842 if (!skb)
1843 return err;
1844 }
Herbert Xue0b46d02014-11-07 21:22:23 +08001845
Al Viro9b067032014-11-07 13:52:07 -05001846 ret = tun_put_user(tun, tfile, skb, to);
Jason Wangf51a5e82014-12-01 16:53:15 +08001847 if (unlikely(ret < 0))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001848 kfree_skb(skb);
Jason Wangf51a5e82014-12-01 16:53:15 +08001849 else
1850 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001852 return ret;
1853}
1854
Al Viro9b067032014-11-07 13:52:07 -05001855static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001856{
1857 struct file *file = iocb->ki_filp;
1858 struct tun_file *tfile = file->private_data;
1859 struct tun_struct *tun = __tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05001860 ssize_t len = iov_iter_count(to), ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001861
1862 if (!tun)
1863 return -EBADFD;
Jason Wangac77cfd2017-05-17 12:14:43 +08001864 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL);
David S. Miller42404c02013-12-10 22:05:45 -05001865 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08001866 if (ret > 0)
1867 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001868 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return ret;
1870}
1871
Jason Wang96442e422012-10-31 19:46:02 +00001872static void tun_free_netdev(struct net_device *dev)
1873{
1874 struct tun_struct *tun = netdev_priv(dev);
1875
Jason Wang4008e972012-12-13 23:53:30 +00001876 BUG_ON(!(list_empty(&tun->disabled)));
Paolo Abeni608b9972016-04-13 10:52:20 +02001877 free_percpu(tun->pcpu_stats);
Jason Wang96442e422012-10-31 19:46:02 +00001878 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001879 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001880}
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882static void tun_setup(struct net_device *dev)
1883{
1884 struct tun_struct *tun = netdev_priv(dev);
1885
Eric W. Biederman0625c882012-02-07 16:48:55 -08001886 tun->owner = INVALID_UID;
1887 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 dev->ethtool_ops = &tun_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001890 dev->needs_free_netdev = true;
1891 dev->priv_destructor = tun_free_netdev;
Jason Wang016adb72016-04-08 13:26:48 +08001892 /* We prefer our own queue length */
1893 dev->tx_queue_len = TUN_READQ_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001896/* Trivial set of netlink ops to allow deleting tun or tap
1897 * device with netlink.
1898 */
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001899static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
1900 struct netlink_ext_ack *extack)
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001901{
1902 return -EINVAL;
1903}
1904
1905static struct rtnl_link_ops tun_link_ops __read_mostly = {
1906 .kind = DRV_NAME,
1907 .priv_size = sizeof(struct tun_struct),
1908 .setup = tun_setup,
1909 .validate = tun_validate,
1910};
1911
Herbert Xu33dccbb2009-02-05 21:25:32 -08001912static void tun_sock_write_space(struct sock *sk)
1913{
Jason Wang54f968d2012-10-31 19:45:57 +00001914 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001915 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001916
1917 if (!sock_writeable(sk))
1918 return;
1919
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001920 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Herbert Xu33dccbb2009-02-05 21:25:32 -08001921 return;
1922
Eric Dumazet43815482010-04-29 11:01:49 +00001923 wqueue = sk_sleep(sk);
1924 if (wqueue && waitqueue_active(wqueue))
1925 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001926 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001927
Jason Wang54f968d2012-10-31 19:45:57 +00001928 tfile = container_of(sk, struct tun_file, sk);
1929 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001930}
1931
Ying Xue1b784142015-03-02 15:37:48 +08001932static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001933{
Jason Wang54f968d2012-10-31 19:45:57 +00001934 int ret;
1935 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1936 struct tun_struct *tun = __tun_get(tfile);
1937
1938 if (!tun)
1939 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04001940
Al Viroc0371da2014-11-24 10:42:55 -05001941 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
Jason Wang5503fce2017-01-18 15:02:03 +08001942 m->msg_flags & MSG_DONTWAIT,
1943 m->msg_flags & MSG_MORE);
Jason Wang54f968d2012-10-31 19:45:57 +00001944 tun_put(tun);
1945 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001946}
1947
Ying Xue1b784142015-03-02 15:37:48 +08001948static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001949 int flags)
1950{
Jason Wang54f968d2012-10-31 19:45:57 +00001951 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1952 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001953 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001954
1955 if (!tun)
1956 return -EBADFD;
1957
Richard Cochraneda29772013-07-19 19:40:10 +02001958 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00001959 ret = -EINVAL;
1960 goto out;
1961 }
Richard Cochraneda29772013-07-19 19:40:10 +02001962 if (flags & MSG_ERRQUEUE) {
1963 ret = sock_recv_errqueue(sock->sk, m, total_len,
1964 SOL_PACKET, TUN_TX_TIMESTAMP);
1965 goto out;
1966 }
Jason Wangac77cfd2017-05-17 12:14:43 +08001967 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT,
1968 m->msg_control);
Alex Gartrell87897932014-12-25 23:05:03 -08001969 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05001970 m->msg_flags |= MSG_TRUNC;
1971 ret = flags & MSG_TRUNC ? ret : total_len;
1972 }
Gao feng3811ae72013-04-24 21:59:23 +00001973out:
Jason Wang54f968d2012-10-31 19:45:57 +00001974 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001975 return ret;
1976}
1977
Jason Wang1576d982016-06-30 14:45:36 +08001978static int tun_peek_len(struct socket *sock)
1979{
1980 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1981 struct tun_struct *tun;
1982 int ret = 0;
1983
1984 tun = __tun_get(tfile);
1985 if (!tun)
1986 return 0;
1987
1988 ret = skb_array_peek_len(&tfile->tx_array);
1989 tun_put(tun);
1990
1991 return ret;
1992}
1993
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001994/* Ops structure to mimic raw sockets with tun */
1995static const struct proto_ops tun_socket_ops = {
Jason Wang1576d982016-06-30 14:45:36 +08001996 .peek_len = tun_peek_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001997 .sendmsg = tun_sendmsg,
1998 .recvmsg = tun_recvmsg,
1999};
2000
Herbert Xu33dccbb2009-02-05 21:25:32 -08002001static struct proto tun_proto = {
2002 .name = "tun",
2003 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00002004 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08002005};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002006
David Woodhouse980c9e82009-05-09 22:54:21 -07002007static int tun_flags(struct tun_struct *tun)
2008{
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002009 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07002010}
2011
2012static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
2013 char *buf)
2014{
2015 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2016 return sprintf(buf, "0x%x\n", tun_flags(tun));
2017}
2018
2019static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
2020 char *buf)
2021{
2022 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002023 return uid_valid(tun->owner)?
2024 sprintf(buf, "%u\n",
2025 from_kuid_munged(current_user_ns(), tun->owner)):
2026 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002027}
2028
2029static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
2030 char *buf)
2031{
2032 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002033 return gid_valid(tun->group) ?
2034 sprintf(buf, "%u\n",
2035 from_kgid_munged(current_user_ns(), tun->group)):
2036 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002037}
2038
2039static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
2040static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
2041static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
2042
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002043static struct attribute *tun_dev_attrs[] = {
2044 &dev_attr_tun_flags.attr,
2045 &dev_attr_owner.attr,
2046 &dev_attr_group.attr,
2047 NULL
2048};
2049
2050static const struct attribute_group tun_attr_group = {
2051 .attrs = tun_dev_attrs
2052};
2053
Pavel Emelyanovd647a592008-04-16 00:41:16 -07002054static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00002057 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct net_device *dev;
2059 int err;
2060
Jason Wang7c0c3b12013-01-11 16:59:33 +00002061 if (tfile->detached)
2062 return -EINVAL;
2063
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002064 dev = __dev_get_by_name(net, ifr->ifr_name);
2065 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07002066 if (ifr->ifr_flags & IFF_TUN_EXCL)
2067 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002068 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2069 tun = netdev_priv(dev);
2070 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2071 tun = netdev_priv(dev);
2072 else
2073 return -EINVAL;
2074
Jason Wang8e6d91a2013-05-28 18:32:11 +00002075 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002076 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00002077 return -EINVAL;
2078
Jason Wangcde8b152012-10-31 19:46:01 +00002079 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04002080 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002081 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04002082 if (err < 0)
2083 return err;
2084
Petar Penkov94317092017-09-22 13:49:14 -07002085 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
2086 ifr->ifr_flags & IFF_NAPI);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00002087 if (err < 0)
2088 return err;
Jason Wang4008e972012-12-13 23:53:30 +00002089
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002090 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00002091 (tun->numqueues + tun->numdisabled > 1)) {
2092 /* One or more queue has already been attached, no need
2093 * to initialize the device again.
2094 */
2095 return 0;
2096 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 else {
2099 char *name;
2100 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00002101 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2102 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Eric W. Biedermanc260b772012-11-18 21:34:11 +00002104 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002105 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04002106 err = security_tun_dev_create();
2107 if (err < 0)
2108 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /* Set dev type */
2111 if (ifr->ifr_flags & IFF_TUN) {
2112 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002113 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 name = "tun%d";
2115 } else if (ifr->ifr_flags & IFF_TAP) {
2116 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002117 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002119 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00002120 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (*ifr->ifr_name)
2123 name = ifr->ifr_name;
2124
Jason Wangc8d68e62012-10-31 19:46:00 +00002125 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02002126 NET_NAME_UNKNOWN, tun_setup, queues,
2127 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (!dev)
2130 return -ENOMEM;
2131
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07002132 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002133 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002134 dev->ifindex = tfile->ifindex;
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002135 dev->sysfs_groups[0] = &tun_attr_group;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 tun = netdev_priv(dev);
2138 tun->dev = dev;
2139 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002140 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002141 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Paolo Abenieaea34b2016-02-26 10:45:40 +01002143 tun->align = NET_SKB_PAD;
Jason Wang54f968d2012-10-31 19:45:57 +00002144 tun->filter_attached = false;
2145 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Jason Wang5503fce2017-01-18 15:02:03 +08002146 tun->rx_batched = 0;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002147
Paolo Abeni608b9972016-04-13 10:52:20 +02002148 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2149 if (!tun->pcpu_stats) {
2150 err = -ENOMEM;
2151 goto err_free_dev;
2152 }
2153
Jason Wang96442e422012-10-31 19:46:02 +00002154 spin_lock_init(&tun->lock);
2155
Paul Moore5dbbaf22013-01-14 07:12:19 +00002156 err = security_tun_dev_alloc_security(&tun->security);
2157 if (err < 0)
Paolo Abeni608b9972016-04-13 10:52:20 +02002158 goto err_free_stat;
Paul Moore2b980db2009-08-28 18:12:43 -04002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04002161 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00002162
Michał Mirosław88255372011-04-19 06:13:10 +00002163 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08002164 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2165 NETIF_F_HW_VLAN_STAG_TX;
Paolo Abeni2a2bbf12016-04-14 18:39:39 +02002166 dev->features = dev->hw_features | NETIF_F_LLTX;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09002167 dev->vlan_features = dev->features &
2168 ~(NETIF_F_HW_VLAN_CTAG_TX |
2169 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00002170
Jason Wang4008e972012-12-13 23:53:30 +00002171 INIT_LIST_HEAD(&tun->disabled);
Petar Penkov94317092017-09-22 13:49:14 -07002172 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI);
Jason Wangeb0fb362012-12-02 17:19:45 +00002173 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002174 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 err = register_netdevice(tun->dev);
2177 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002178 goto err_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00002181 netif_carrier_on(tun->dev);
2182
Joe Perches6b8a66e2011-03-02 07:18:10 +00002183 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002185 tun->flags = (tun->flags & ~TUN_FEATURES) |
2186 (ifr->ifr_flags & TUN_FEATURES);
Jason Wangc8d68e62012-10-31 19:46:00 +00002187
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002188 /* Make sure persistent devices do not get stuck in
2189 * xoff state.
2190 */
2191 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00002192 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 strcpy(ifr->ifr_name, tun->dev->name);
2195 return 0;
2196
Jason Wang662ca432013-09-11 18:09:48 +08002197err_detach:
2198 tun_detach_all(dev);
Eric Dumazetff244c62017-08-18 13:39:56 -07002199 /* register_netdevice() already called tun_free_netdev() */
2200 goto err_free_dev;
2201
Jason Wang662ca432013-09-11 18:09:48 +08002202err_free_flow:
2203 tun_flow_uninit(tun);
2204 security_tun_dev_free_security(tun->security);
Paolo Abeni608b9972016-04-13 10:52:20 +02002205err_free_stat:
2206 free_percpu(tun->pcpu_stats);
Jason Wang662ca432013-09-11 18:09:48 +08002207err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return err;
2210}
2211
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002212static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00002213 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07002214{
Joe Perches6b8a66e2011-03-02 07:18:10 +00002215 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07002216
2217 strcpy(ifr->ifr_name, tun->dev->name);
2218
David Woodhouse980c9e82009-05-09 22:54:21 -07002219 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07002220
Mark McLoughline3b99552008-08-15 15:09:56 -07002221}
2222
Rusty Russell5228ddc2008-07-03 03:46:16 -07002223/* This is like a cut-down ethtool ops, except done via tun fd so no
2224 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00002225static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07002226{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002227 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002228
2229 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00002230 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002231 arg &= ~TUN_F_CSUM;
2232
2233 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2234 if (arg & TUN_F_TSO_ECN) {
2235 features |= NETIF_F_TSO_ECN;
2236 arg &= ~TUN_F_TSO_ECN;
2237 }
2238 if (arg & TUN_F_TSO4)
2239 features |= NETIF_F_TSO;
2240 if (arg & TUN_F_TSO6)
2241 features |= NETIF_F_TSO6;
2242 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2243 }
2244 }
2245
2246 /* This gives the user a way to test for new features in future by
2247 * trying to set them. */
2248 if (arg)
2249 return -EINVAL;
2250
Michał Mirosław88255372011-04-19 06:13:10 +00002251 tun->set_features = features;
Yaroslav Isakov09050952017-03-16 22:44:10 +03002252 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2253 tun->dev->wanted_features |= features;
Michał Mirosław88255372011-04-19 06:13:10 +00002254 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07002255
2256 return 0;
2257}
2258
Jason Wangc8d68e62012-10-31 19:46:00 +00002259static void tun_detach_filter(struct tun_struct *tun, int n)
2260{
2261 int i;
2262 struct tun_file *tfile;
2263
2264 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002265 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002266 lock_sock(tfile->socket.sk);
2267 sk_detach_filter(tfile->socket.sk);
2268 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002269 }
2270
2271 tun->filter_attached = false;
2272}
2273
2274static int tun_attach_filter(struct tun_struct *tun)
2275{
2276 int i, ret = 0;
2277 struct tun_file *tfile;
2278
2279 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002280 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002281 lock_sock(tfile->socket.sk);
2282 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2283 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002284 if (ret) {
2285 tun_detach_filter(tun, i);
2286 return ret;
2287 }
2288 }
2289
2290 tun->filter_attached = true;
2291 return ret;
2292}
2293
2294static void tun_set_sndbuf(struct tun_struct *tun)
2295{
2296 struct tun_file *tfile;
2297 int i;
2298
2299 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002300 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00002301 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2302 }
2303}
2304
Jason Wangcde8b152012-10-31 19:46:01 +00002305static int tun_set_queue(struct file *file, struct ifreq *ifr)
2306{
2307 struct tun_file *tfile = file->private_data;
2308 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00002309 int ret = 0;
2310
2311 rtnl_lock();
2312
2313 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00002314 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002315 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00002316 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002317 goto unlock;
2318 }
2319 ret = security_tun_dev_attach_queue(tun->security);
2320 if (ret < 0)
2321 goto unlock;
Petar Penkov94317092017-09-22 13:49:14 -07002322 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI);
Jason Wang4008e972012-12-13 23:53:30 +00002323 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002324 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002325 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00002326 ret = -EINVAL;
2327 else
2328 __tun_detach(tfile, false);
2329 } else
Jason Wangcde8b152012-10-31 19:46:01 +00002330 ret = -EINVAL;
2331
Paul Moore5dbbaf22013-01-14 07:12:19 +00002332unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00002333 rtnl_unlock();
2334 return ret;
2335}
2336
Arnd Bergmann50857e22009-11-06 22:52:32 -08002337static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2338 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002340 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002341 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 void __user* argp = (void __user*)arg;
2343 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08002344 kuid_t owner;
2345 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002346 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002347 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002348 unsigned int ifindex;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002349 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002350 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Gao Feng20861f22016-10-27 09:05:22 +08002352 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08002353 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07002355 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00002356 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07002357 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002358 if (cmd == TUNGETFEATURES) {
2359 /* Currently this just means: "what IFF flags are valid?".
2360 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002361 * TUNSETIFF.
2362 */
2363 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00002364 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00002365 } else if (cmd == TUNSETQUEUE)
2366 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002367
Jason Wangc8d68e62012-10-31 19:46:00 +00002368 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00002369 rtnl_lock();
2370
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002371 tun = __tun_get(tfile);
Gao Feng0f16bc12016-10-25 22:26:09 +08002372 if (cmd == TUNSETIFF) {
2373 ret = -EEXIST;
2374 if (tun)
2375 goto unlock;
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2378
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002379 ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Herbert Xu876bfd42009-08-06 14:22:44 +00002381 if (ret)
2382 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Arnd Bergmann50857e22009-11-06 22:52:32 -08002384 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00002385 ret = -EFAULT;
2386 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002388 if (cmd == TUNSETIFINDEX) {
2389 ret = -EPERM;
2390 if (tun)
2391 goto unlock;
2392
2393 ret = -EFAULT;
2394 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2395 goto unlock;
2396
2397 ret = 0;
2398 tfile->ifindex = ifindex;
2399 goto unlock;
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Herbert Xu876bfd42009-08-06 14:22:44 +00002402 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00002404 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Jason Wang1e588332012-10-31 19:45:56 +00002406 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Eric W. Biederman631ab462009-01-20 11:00:40 +00002408 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07002410 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002411 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07002412
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002413 if (tfile->detached)
2414 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04002415 if (!tfile->socket.sk->sk_filter)
2416 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002417
Arnd Bergmann50857e22009-11-06 22:52:32 -08002418 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002419 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07002420 break;
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 case TUNSETNOCSUM:
2423 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Michał Mirosław88255372011-04-19 06:13:10 +00002425 /* [unimplemented] */
2426 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00002427 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 break;
2429
2430 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00002431 /* Disable/Enable persist mode. Keep an extra reference to the
2432 * module to prevent the module being unprobed.
2433 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002434 if (arg && !(tun->flags & IFF_PERSIST)) {
2435 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002436 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00002437 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002438 if (!arg && (tun->flags & IFF_PERSIST)) {
2439 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002440 module_put(THIS_MODULE);
2441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Joe Perches6b8a66e2011-03-02 07:18:10 +00002443 tun_debug(KERN_INFO, tun, "persist %s\n",
2444 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 break;
2446
2447 case TUNSETOWNER:
2448 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002449 owner = make_kuid(current_user_ns(), arg);
2450 if (!uid_valid(owner)) {
2451 ret = -EINVAL;
2452 break;
2453 }
2454 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00002455 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002456 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 break;
2458
Guido Guenther8c644622007-07-02 22:50:25 -07002459 case TUNSETGROUP:
2460 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002461 group = make_kgid(current_user_ns(), arg);
2462 if (!gid_valid(group)) {
2463 ret = -EINVAL;
2464 break;
2465 }
2466 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00002467 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002468 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07002469 break;
2470
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002471 case TUNSETLINK:
2472 /* Only allow setting the type when the interface is down */
2473 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002474 tun_debug(KERN_INFO, tun,
2475 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07002476 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002477 } else {
2478 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00002479 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
2480 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07002481 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002482 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002483 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485#ifdef TUN_DEBUG
2486 case TUNSETDEBUG:
2487 tun->debug = arg;
2488 break;
2489#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07002490 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00002491 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002492 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002493
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002494 case TUNSETTXFILTER:
2495 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00002496 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002497 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00002498 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07002499 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002503 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002504 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2505 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08002506 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002507 ret = -EFAULT;
2508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002511 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00002512 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2513 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08002514
Kim B. Heino40102372008-02-29 12:26:21 -08002515 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002516 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002517
2518 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002519 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002520 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2521 ret = -EFAULT;
2522 break;
2523
2524 case TUNSETSNDBUF:
2525 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2526 ret = -EFAULT;
2527 break;
2528 }
2529
Jason Wangc8d68e62012-10-31 19:46:00 +00002530 tun->sndbuf = sndbuf;
2531 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002532 break;
2533
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002534 case TUNGETVNETHDRSZ:
2535 vnet_hdr_sz = tun->vnet_hdr_sz;
2536 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2537 ret = -EFAULT;
2538 break;
2539
2540 case TUNSETVNETHDRSZ:
2541 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2542 ret = -EFAULT;
2543 break;
2544 }
2545 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2546 ret = -EINVAL;
2547 break;
2548 }
2549
2550 tun->vnet_hdr_sz = vnet_hdr_sz;
2551 break;
2552
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002553 case TUNGETVNETLE:
2554 le = !!(tun->flags & TUN_VNET_LE);
2555 if (put_user(le, (int __user *)argp))
2556 ret = -EFAULT;
2557 break;
2558
2559 case TUNSETVNETLE:
2560 if (get_user(le, (int __user *)argp)) {
2561 ret = -EFAULT;
2562 break;
2563 }
2564 if (le)
2565 tun->flags |= TUN_VNET_LE;
2566 else
2567 tun->flags &= ~TUN_VNET_LE;
2568 break;
2569
Greg Kurz8b8e6582015-04-24 14:50:36 +02002570 case TUNGETVNETBE:
2571 ret = tun_get_vnet_be(tun, argp);
2572 break;
2573
2574 case TUNSETVNETBE:
2575 ret = tun_set_vnet_be(tun, argp);
2576 break;
2577
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002578 case TUNATTACHFILTER:
2579 /* Can be set only for TAPs */
2580 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002581 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002582 break;
2583 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002584 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002585 break;
2586
Jason Wangc8d68e62012-10-31 19:46:00 +00002587 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002588 break;
2589
2590 case TUNDETACHFILTER:
2591 /* Can be set only for TAPs */
2592 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002593 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002594 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002595 ret = 0;
2596 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002597 break;
2598
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002599 case TUNGETFILTER:
2600 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002601 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002602 break;
2603 ret = -EFAULT;
2604 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2605 break;
2606 ret = 0;
2607 break;
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002610 ret = -EINVAL;
2611 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Herbert Xu876bfd42009-08-06 14:22:44 +00002614unlock:
2615 rtnl_unlock();
2616 if (tun)
2617 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002618 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
Arnd Bergmann50857e22009-11-06 22:52:32 -08002621static long tun_chr_ioctl(struct file *file,
2622 unsigned int cmd, unsigned long arg)
2623{
2624 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2625}
2626
2627#ifdef CONFIG_COMPAT
2628static long tun_chr_compat_ioctl(struct file *file,
2629 unsigned int cmd, unsigned long arg)
2630{
2631 switch (cmd) {
2632 case TUNSETIFF:
2633 case TUNGETIFF:
2634 case TUNSETTXFILTER:
2635 case TUNGETSNDBUF:
2636 case TUNSETSNDBUF:
2637 case SIOCGIFHWADDR:
2638 case SIOCSIFHWADDR:
2639 arg = (unsigned long)compat_ptr(arg);
2640 break;
2641 default:
2642 arg = (compat_ulong_t)arg;
2643 break;
2644 }
2645
2646 /*
2647 * compat_ifreq is shorter than ifreq, so we must not access beyond
2648 * the end of that structure. All fields that are used in this
2649 * driver are compatible though, we don't need to convert the
2650 * contents.
2651 */
2652 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2653}
2654#endif /* CONFIG_COMPAT */
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656static int tun_chr_fasync(int fd, struct file *file, int on)
2657{
Jason Wang54f968d2012-10-31 19:45:57 +00002658 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 int ret;
2660
Jason Wang54f968d2012-10-31 19:45:57 +00002661 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002662 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (on) {
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002665 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00002666 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002667 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002668 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002669 ret = 0;
2670out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
2674static int tun_chr_open(struct inode *inode, struct file * file)
2675{
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002676 struct net *net = current->nsproxy->net_ns;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002677 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002678
Joe Perches6b8a66e2011-03-02 07:18:10 +00002679 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002680
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002681 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05002682 &tun_proto, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002683 if (!tfile)
2684 return -ENOMEM;
Monam Agarwalc9566742014-03-24 00:02:32 +05302685 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang54f968d2012-10-31 19:45:57 +00002686 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002687 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002688
Jason Wang54f968d2012-10-31 19:45:57 +00002689 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07002690 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00002691
2692 tfile->socket.file = file;
2693 tfile->socket.ops = &tun_socket_ops;
2694
2695 sock_init_data(&tfile->socket, &tfile->sk);
Jason Wang54f968d2012-10-31 19:45:57 +00002696
2697 tfile->sk.sk_write_space = tun_sock_write_space;
2698 tfile->sk.sk_sndbuf = INT_MAX;
2699
Eric W. Biederman631ab462009-01-20 11:00:40 +00002700 file->private_data = tfile;
Jason Wang4008e972012-12-13 23:53:30 +00002701 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002702
Jason Wang19a6afb2013-06-08 14:17:41 +08002703 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return 0;
2706}
2707
2708static int tun_chr_close(struct inode *inode, struct file *file)
2709{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002710 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Jason Wangc8d68e62012-10-31 19:46:00 +00002712 tun_detach(tfile, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 return 0;
2715}
2716
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002717#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -07002718static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002719{
2720 struct tun_struct *tun;
2721 struct ifreq ifr;
2722
2723 memset(&ifr, 0, sizeof(ifr));
2724
2725 rtnl_lock();
2726 tun = tun_get(f);
2727 if (tun)
2728 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2729 rtnl_unlock();
2730
2731 if (tun)
2732 tun_put(tun);
2733
Joe Perchesa3816ab2014-09-29 16:08:25 -07002734 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002735}
2736#endif
2737
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002738static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002739 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05002741 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04002742 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002744 .unlocked_ioctl = tun_chr_ioctl,
2745#ifdef CONFIG_COMPAT
2746 .compat_ioctl = tun_chr_compat_ioctl,
2747#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 .open = tun_chr_open,
2749 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002750 .fasync = tun_chr_fasync,
2751#ifdef CONFIG_PROC_FS
2752 .show_fdinfo = tun_chr_show_fdinfo,
2753#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754};
2755
2756static struct miscdevice tun_miscdev = {
2757 .minor = TUN_MINOR,
2758 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002759 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761};
2762
2763/* ethtool interface */
2764
Philippe Reynes29ccc492017-03-11 22:03:50 +01002765static int tun_get_link_ksettings(struct net_device *dev,
2766 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Philippe Reynes29ccc492017-03-11 22:03:50 +01002768 ethtool_link_ksettings_zero_link_mode(cmd, supported);
2769 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
2770 cmd->base.speed = SPEED_10;
2771 cmd->base.duplex = DUPLEX_FULL;
2772 cmd->base.port = PORT_TP;
2773 cmd->base.phy_address = 0;
2774 cmd->base.autoneg = AUTONEG_DISABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return 0;
2776}
2777
2778static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2779{
2780 struct tun_struct *tun = netdev_priv(dev);
2781
Rick Jones33a5ba12011-11-15 14:59:53 +00002782 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2783 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002786 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00002787 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002789 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00002790 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 break;
2792 }
2793}
2794
2795static u32 tun_get_msglevel(struct net_device *dev)
2796{
2797#ifdef TUN_DEBUG
2798 struct tun_struct *tun = netdev_priv(dev);
2799 return tun->debug;
2800#else
2801 return -EOPNOTSUPP;
2802#endif
2803}
2804
2805static void tun_set_msglevel(struct net_device *dev, u32 value)
2806{
2807#ifdef TUN_DEBUG
2808 struct tun_struct *tun = netdev_priv(dev);
2809 tun->debug = value;
2810#endif
2811}
2812
Jason Wang5503fce2017-01-18 15:02:03 +08002813static int tun_get_coalesce(struct net_device *dev,
2814 struct ethtool_coalesce *ec)
2815{
2816 struct tun_struct *tun = netdev_priv(dev);
2817
2818 ec->rx_max_coalesced_frames = tun->rx_batched;
2819
2820 return 0;
2821}
2822
2823static int tun_set_coalesce(struct net_device *dev,
2824 struct ethtool_coalesce *ec)
2825{
2826 struct tun_struct *tun = netdev_priv(dev);
2827
2828 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
2829 tun->rx_batched = NAPI_POLL_WEIGHT;
2830 else
2831 tun->rx_batched = ec->rx_max_coalesced_frames;
2832
2833 return 0;
2834}
2835
Jeff Garzik7282d492006-09-13 14:30:00 -04002836static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 .get_drvinfo = tun_get_drvinfo,
2838 .get_msglevel = tun_get_msglevel,
2839 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002840 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002841 .get_ts_info = ethtool_op_get_ts_info,
Jason Wang5503fce2017-01-18 15:02:03 +08002842 .get_coalesce = tun_get_coalesce,
2843 .set_coalesce = tun_set_coalesce,
Philippe Reynes29ccc492017-03-11 22:03:50 +01002844 .get_link_ksettings = tun_get_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845};
2846
Jason Wang1576d982016-06-30 14:45:36 +08002847static int tun_queue_resize(struct tun_struct *tun)
2848{
2849 struct net_device *dev = tun->dev;
2850 struct tun_file *tfile;
2851 struct skb_array **arrays;
2852 int n = tun->numqueues + tun->numdisabled;
2853 int ret, i;
2854
stephen hemminger12039042017-08-15 10:29:16 -07002855 arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL);
Jason Wang1576d982016-06-30 14:45:36 +08002856 if (!arrays)
2857 return -ENOMEM;
2858
2859 for (i = 0; i < tun->numqueues; i++) {
2860 tfile = rtnl_dereference(tun->tfiles[i]);
2861 arrays[i] = &tfile->tx_array;
2862 }
2863 list_for_each_entry(tfile, &tun->disabled, next)
2864 arrays[i++] = &tfile->tx_array;
2865
2866 ret = skb_array_resize_multiple(arrays, n,
2867 dev->tx_queue_len, GFP_KERNEL);
2868
2869 kfree(arrays);
2870 return ret;
2871}
2872
2873static int tun_device_event(struct notifier_block *unused,
2874 unsigned long event, void *ptr)
2875{
2876 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2877 struct tun_struct *tun = netdev_priv(dev);
2878
Craig Gallek86dfb4ac2016-07-06 18:44:20 -04002879 if (dev->rtnl_link_ops != &tun_link_ops)
2880 return NOTIFY_DONE;
2881
Jason Wang1576d982016-06-30 14:45:36 +08002882 switch (event) {
2883 case NETDEV_CHANGE_TX_QUEUE_LEN:
2884 if (tun_queue_resize(tun))
2885 return NOTIFY_BAD;
2886 break;
2887 default:
2888 break;
2889 }
2890
2891 return NOTIFY_DONE;
2892}
2893
2894static struct notifier_block tun_notifier_block __read_mostly = {
2895 .notifier_call = tun_device_event,
2896};
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898static int __init tun_init(void)
2899{
2900 int ret = 0;
2901
Joe Perches6b8a66e2011-03-02 07:18:10 +00002902 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002904 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002905 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002906 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002907 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002908 }
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002911 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002912 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002913 goto err_misc;
2914 }
Jason Wang1576d982016-06-30 14:45:36 +08002915
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07002916 ret = register_netdevice_notifier(&tun_notifier_block);
2917 if (ret) {
2918 pr_err("Can't register netdevice notifier\n");
2919 goto err_notifier;
2920 }
2921
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002922 return 0;
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07002923
2924err_notifier:
2925 misc_deregister(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002926err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002927 rtnl_link_unregister(&tun_link_ops);
2928err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return ret;
2930}
2931
2932static void tun_cleanup(void)
2933{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002934 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002935 rtnl_link_unregister(&tun_link_ops);
Jason Wang1576d982016-06-30 14:45:36 +08002936 unregister_netdevice_notifier(&tun_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002939/* Get an underlying socket object from tun file. Returns error unless file is
2940 * attached to a device. The returned object works like a packet socket, it
2941 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2942 * holding a reference to the file for as long as the socket is in use. */
2943struct socket *tun_get_socket(struct file *file)
2944{
Jason Wang6e914fc2012-10-31 19:45:58 +00002945 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002946 if (file->f_op != &tun_fops)
2947 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002948 tfile = file->private_data;
2949 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002950 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002951 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002952}
2953EXPORT_SYMBOL_GPL(tun_get_socket);
2954
Jason Wang83339c62017-05-17 12:14:41 +08002955struct skb_array *tun_get_skb_array(struct file *file)
2956{
2957 struct tun_file *tfile;
2958
2959 if (file->f_op != &tun_fops)
2960 return ERR_PTR(-EINVAL);
2961 tfile = file->private_data;
2962 if (!tfile)
2963 return ERR_PTR(-EBADFD);
2964 return &tfile->tx_array;
2965}
2966EXPORT_SYMBOL_GPL(tun_get_skb_array);
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968module_init(tun_init);
2969module_exit(tun_cleanup);
2970MODULE_DESCRIPTION(DRV_DESCRIPTION);
2971MODULE_AUTHOR(DRV_COPYRIGHT);
2972MODULE_LICENSE("GPL");
2973MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002974MODULE_ALIAS("devname:net/tun");