blob: 9880b3bc8fa5d15958ba0ac4cbd56d7700c5906e [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>
Petar Penkov90e33d42017-09-22 13:49:15 -070078#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080080#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Rusty Russell14daa022008-04-12 18:48:58 -070082/* Uncomment to enable debugging */
83/* #define TUN_DEBUG 1 */
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef TUN_DEBUG
86static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070087
Joe Perches6b8a66e2011-03-02 07:18:10 +000088#define tun_debug(level, tun, fmt, args...) \
89do { \
90 if (tun->debug) \
91 netdev_printk(level, tun->dev, fmt, ##args); \
92} while (0)
93#define DBG1(level, fmt, args...) \
94do { \
95 if (debug == 2) \
96 printk(level fmt, ##args); \
97} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070098#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000099#define tun_debug(level, tun, fmt, args...) \
100do { \
101 if (0) \
102 netdev_printk(level, tun->dev, fmt, ##args); \
103} while (0)
104#define DBG1(level, fmt, args...) \
105do { \
106 if (0) \
107 printk(level fmt, ##args); \
108} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
110
Jason Wang761876c2017-08-11 19:41:18 +0800111#define TUN_HEADROOM 256
Jason Wang7df13212017-09-04 11:36:08 +0800112#define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
Jason Wang66ccbc92017-08-11 19:41:16 +0800113
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +0200114/* TUN device flags */
115
116/* IFF_ATTACH_QUEUE is never stored in device flags,
117 * overload it to mean fasync when stored there.
118 */
119#define TUN_FASYNC IFF_ATTACH_QUEUE
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200120/* High bits in flags field are unused. */
121#define TUN_VNET_LE 0x80000000
Greg Kurz8b8e6582015-04-24 14:50:36 +0200122#define TUN_VNET_BE 0x40000000
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +0200123
124#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Petar Penkov90e33d42017-09-22 13:49:15 -0700125 IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS)
126
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000127#define GOODCOPY_LEN 128
128
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700129#define FLT_EXACT_COUNT 8
130struct tap_filter {
131 unsigned int count; /* Number of addrs. Zero means disabled */
132 u32 mask[2]; /* Mask of the hashed addrs */
133 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
134};
135
Pankaj Guptabaf71c52015-01-12 11:41:29 +0530136/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
137 * to max number of VCPUs in guest. */
138#define MAX_TAP_QUEUES 256
Jason Wangb8732fb2013-01-23 03:59:13 +0000139#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000140
Jason Wang96442e422012-10-31 19:46:02 +0000141#define TUN_FLOW_EXPIRE (3 * HZ)
142
Paolo Abeni608b9972016-04-13 10:52:20 +0200143struct tun_pcpu_stats {
144 u64 rx_packets;
145 u64 rx_bytes;
146 u64 tx_packets;
147 u64 tx_bytes;
148 struct u64_stats_sync syncp;
149 u32 rx_dropped;
150 u32 tx_dropped;
151 u32 rx_frame_errors;
152};
153
Jason Wang54f968d2012-10-31 19:45:57 +0000154/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800155 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000156 * to serve as one transmit queue for tuntap device. The sock_fprog and
157 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000158 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000159 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000160 *
161 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000162 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000163 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000164 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000165struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000166 struct sock sk;
167 struct socket socket;
168 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000169 struct tun_struct __rcu *tun;
Jason Wang54f968d2012-10-31 19:45:57 +0000170 struct fasync_struct *fasync;
171 /* only used for fasnyc */
172 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400173 union {
174 u16 queue_index;
175 unsigned int ifindex;
176 };
Petar Penkov94317092017-09-22 13:49:14 -0700177 struct napi_struct napi;
Petar Penkov90e33d42017-09-22 13:49:15 -0700178 struct mutex napi_mutex; /* Protects access to the above napi */
Jason Wang4008e972012-12-13 23:53:30 +0000179 struct list_head next;
180 struct tun_struct *detached;
Jason Wang1576d982016-06-30 14:45:36 +0800181 struct skb_array tx_array;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000182};
183
Jason Wang96442e422012-10-31 19:46:02 +0000184struct tun_flow_entry {
185 struct hlist_node hash_link;
186 struct rcu_head rcu;
187 struct tun_struct *tun;
188
189 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800190 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000191 int queue_index;
192 unsigned long updated;
193};
194
195#define TUN_NUM_FLOW_ENTRIES 1024
196
Jason Wang54f968d2012-10-31 19:45:57 +0000197/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000198 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000199 * file were attached to a persist device.
200 */
Rusty Russell14daa022008-04-12 18:48:58 -0700201struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000202 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
203 unsigned int numqueues;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700204 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800205 kuid_t owner;
206 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700207
Rusty Russell14daa022008-04-12 18:48:58 -0700208 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000209 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000210#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
David S. Millerd591a1f2017-07-03 06:35:32 -0700211 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200212
Paolo Abenieaea34b2016-02-26 10:45:40 +0100213 int align;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200214 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000215 int sndbuf;
216 struct tap_filter txflt;
217 struct sock_fprog fprog;
218 /* protected by rtnl lock */
219 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700220#ifdef TUN_DEBUG
221 int debug;
222#endif
Jason Wang96442e422012-10-31 19:46:02 +0000223 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000224 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
225 struct timer_list flow_gc_timer;
226 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000227 unsigned int numdisabled;
228 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000229 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000230 u32 flow_count;
Jason Wang5503fce2017-01-18 15:02:03 +0800231 u32 rx_batched;
Paolo Abeni608b9972016-04-13 10:52:20 +0200232 struct tun_pcpu_stats __percpu *pcpu_stats;
Jason Wang761876c2017-08-11 19:41:18 +0800233 struct bpf_prog __rcu *xdp_prog;
Rusty Russell14daa022008-04-12 18:48:58 -0700234};
235
Petar Penkov94317092017-09-22 13:49:14 -0700236static int tun_napi_receive(struct napi_struct *napi, int budget)
237{
238 struct tun_file *tfile = container_of(napi, struct tun_file, napi);
239 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
240 struct sk_buff_head process_queue;
241 struct sk_buff *skb;
242 int received = 0;
243
244 __skb_queue_head_init(&process_queue);
245
246 spin_lock(&queue->lock);
247 skb_queue_splice_tail_init(queue, &process_queue);
248 spin_unlock(&queue->lock);
249
250 while (received < budget && (skb = __skb_dequeue(&process_queue))) {
251 napi_gro_receive(napi, skb);
252 ++received;
253 }
254
255 if (!skb_queue_empty(&process_queue)) {
256 spin_lock(&queue->lock);
257 skb_queue_splice(&process_queue, queue);
258 spin_unlock(&queue->lock);
259 }
260
261 return received;
262}
263
264static int tun_napi_poll(struct napi_struct *napi, int budget)
265{
266 unsigned int received;
267
268 received = tun_napi_receive(napi, budget);
269
270 if (received < budget)
271 napi_complete_done(napi, received);
272
273 return received;
274}
275
276static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
277 bool napi_en)
278{
279 if (napi_en) {
280 netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
281 NAPI_POLL_WEIGHT);
282 napi_enable(&tfile->napi);
Petar Penkov90e33d42017-09-22 13:49:15 -0700283 mutex_init(&tfile->napi_mutex);
Petar Penkov94317092017-09-22 13:49:14 -0700284 }
285}
286
287static void tun_napi_disable(struct tun_struct *tun, struct tun_file *tfile)
288{
289 if (tun->flags & IFF_NAPI)
290 napi_disable(&tfile->napi);
291}
292
293static void tun_napi_del(struct tun_struct *tun, struct tun_file *tfile)
294{
295 if (tun->flags & IFF_NAPI)
296 netif_napi_del(&tfile->napi);
297}
298
Petar Penkov90e33d42017-09-22 13:49:15 -0700299static bool tun_napi_frags_enabled(const struct tun_struct *tun)
300{
301 return READ_ONCE(tun->flags) & IFF_NAPI_FRAGS;
302}
303
Greg Kurz8b8e6582015-04-24 14:50:36 +0200304#ifdef CONFIG_TUN_VNET_CROSS_LE
305static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
306{
307 return tun->flags & TUN_VNET_BE ? false :
308 virtio_legacy_is_little_endian();
309}
310
311static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
312{
313 int be = !!(tun->flags & TUN_VNET_BE);
314
315 if (put_user(be, argp))
316 return -EFAULT;
317
318 return 0;
319}
320
321static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
322{
323 int be;
324
325 if (get_user(be, argp))
326 return -EFAULT;
327
328 if (be)
329 tun->flags |= TUN_VNET_BE;
330 else
331 tun->flags &= ~TUN_VNET_BE;
332
333 return 0;
334}
335#else
336static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
337{
338 return virtio_legacy_is_little_endian();
339}
340
341static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
342{
343 return -EINVAL;
344}
345
346static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
347{
348 return -EINVAL;
349}
350#endif /* CONFIG_TUN_VNET_CROSS_LE */
351
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200352static inline bool tun_is_little_endian(struct tun_struct *tun)
353{
Greg Kurz7d824102015-04-24 14:26:24 +0200354 return tun->flags & TUN_VNET_LE ||
Greg Kurz8b8e6582015-04-24 14:50:36 +0200355 tun_legacy_is_little_endian(tun);
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200356}
357
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300358static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
359{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200360 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300361}
362
363static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
364{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200365 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300366}
367
Jason Wang96442e422012-10-31 19:46:02 +0000368static inline u32 tun_hashfn(u32 rxhash)
369{
370 return rxhash & 0x3ff;
371}
372
373static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
374{
375 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000376
Sasha Levinb67bfe02013-02-27 17:06:00 -0800377 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000378 if (e->rxhash == rxhash)
379 return e;
380 }
381 return NULL;
382}
383
384static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
385 struct hlist_head *head,
386 u32 rxhash, u16 queue_index)
387{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000388 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
389
Jason Wang96442e422012-10-31 19:46:02 +0000390 if (e) {
391 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
392 rxhash, queue_index);
393 e->updated = jiffies;
394 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800395 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000396 e->queue_index = queue_index;
397 e->tun = tun;
398 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000399 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000400 }
401 return e;
402}
403
Jason Wang96442e422012-10-31 19:46:02 +0000404static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
405{
406 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
407 e->rxhash, e->queue_index);
408 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000409 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000410 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000411}
412
413static void tun_flow_flush(struct tun_struct *tun)
414{
415 int i;
416
417 spin_lock_bh(&tun->lock);
418 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
419 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800420 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000421
Sasha Levinb67bfe02013-02-27 17:06:00 -0800422 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000423 tun_flow_delete(tun, e);
424 }
425 spin_unlock_bh(&tun->lock);
426}
427
428static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
429{
430 int i;
431
432 spin_lock_bh(&tun->lock);
433 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
434 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800435 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000436
Sasha Levinb67bfe02013-02-27 17:06:00 -0800437 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000438 if (e->queue_index == queue_index)
439 tun_flow_delete(tun, e);
440 }
441 }
442 spin_unlock_bh(&tun->lock);
443}
444
445static void tun_flow_cleanup(unsigned long data)
446{
447 struct tun_struct *tun = (struct tun_struct *)data;
448 unsigned long delay = tun->ageing_time;
449 unsigned long next_timer = jiffies + delay;
450 unsigned long count = 0;
451 int i;
452
453 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
454
455 spin_lock_bh(&tun->lock);
456 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
457 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800458 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000459
Sasha Levinb67bfe02013-02-27 17:06:00 -0800460 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000461 unsigned long this_timer;
462 count++;
463 this_timer = e->updated + delay;
464 if (time_before_eq(this_timer, jiffies))
465 tun_flow_delete(tun, e);
466 else if (time_before(this_timer, next_timer))
467 next_timer = this_timer;
468 }
469 }
470
471 if (count)
472 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
473 spin_unlock_bh(&tun->lock);
474}
475
Eric Dumazet49974422012-12-12 19:22:57 +0000476static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000477 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000478{
479 struct hlist_head *head;
480 struct tun_flow_entry *e;
481 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000482 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000483
484 if (!rxhash)
485 return;
486 else
487 head = &tun->flows[tun_hashfn(rxhash)];
488
489 rcu_read_lock();
490
Jason Wang9e857222013-01-28 01:05:19 +0000491 /* We may get a very small possibility of OOO during switching, not
492 * worth to optimize.*/
493 if (tun->numqueues == 1 || tfile->detached)
Jason Wang96442e422012-10-31 19:46:02 +0000494 goto unlock;
495
496 e = tun_flow_find(head, rxhash);
497 if (likely(e)) {
498 /* TODO: keep queueing to old queue until it's empty? */
499 e->queue_index = queue_index;
500 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800501 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000502 } else {
503 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000504 if (!tun_flow_find(head, rxhash) &&
505 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000506 tun_flow_create(tun, head, rxhash, queue_index);
507
508 if (!timer_pending(&tun->flow_gc_timer))
509 mod_timer(&tun->flow_gc_timer,
510 round_jiffies_up(jiffies + delay));
511 spin_unlock_bh(&tun->lock);
512 }
513
514unlock:
515 rcu_read_unlock();
516}
517
Tom Herbert9bc88932013-12-22 18:54:32 +0800518/**
519 * Save the hash received in the stack receive path and update the
520 * flow_hash table accordingly.
521 */
522static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
523{
Eric Dumazet567e4b72015-02-06 12:59:01 -0800524 if (unlikely(e->rps_rxhash != hash))
Tom Herbert9bc88932013-12-22 18:54:32 +0800525 e->rps_rxhash = hash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800526}
527
Jason Wangc8d68e62012-10-31 19:46:00 +0000528/* We try to identify a flow through its rxhash first. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800529 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000530 * the rxq based on the txq where the last packet of the flow comes. As
531 * the userspace application move between processors, we may get a
532 * different rxq no. here. If we could not get rxhash, then we would
533 * hope the rxq no. may help here.
534 */
Jason Wangf663dd92014-01-10 16:18:26 +0800535static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100536 void *accel_priv, select_queue_fallback_t fallback)
Jason Wangc8d68e62012-10-31 19:46:00 +0000537{
538 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000539 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000540 u32 txq = 0;
541 u32 numqueues = 0;
542
543 rcu_read_lock();
Jason Wang92bb73e2013-06-05 16:44:57 +0800544 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000545
Jason Wangfeec0842017-06-06 14:09:49 +0800546 txq = __skb_get_hash_symmetric(skb);
Jason Wangc8d68e62012-10-31 19:46:00 +0000547 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000548 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
Tom Herbert9bc88932013-12-22 18:54:32 +0800549 if (e) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800550 tun_flow_save_rps_rxhash(e, txq);
Zhi Yong Wufbe4d452014-01-02 13:24:28 +0800551 txq = e->queue_index;
Tom Herbert9bc88932013-12-22 18:54:32 +0800552 } else
Jason Wang96442e422012-10-31 19:46:02 +0000553 /* use multiply and shift instead of expensive divide */
554 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000555 } else if (likely(skb_rx_queue_recorded(skb))) {
556 txq = skb_get_rx_queue(skb);
557 while (unlikely(txq >= numqueues))
558 txq -= numqueues;
559 }
560
561 rcu_read_unlock();
562 return txq;
563}
564
Jason Wangcde8b152012-10-31 19:46:01 +0000565static inline bool tun_not_capable(struct tun_struct *tun)
566{
567 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000568 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000569
570 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
571 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000572 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000573}
574
Jason Wangc8d68e62012-10-31 19:46:00 +0000575static void tun_set_real_num_queues(struct tun_struct *tun)
576{
577 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
578 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
579}
580
Jason Wang4008e972012-12-13 23:53:30 +0000581static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
582{
583 tfile->detached = tun;
584 list_add_tail(&tfile->next, &tun->disabled);
585 ++tun->numdisabled;
586}
587
Jason Wangd32649d2012-12-18 11:00:27 +0800588static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000589{
590 struct tun_struct *tun = tfile->detached;
591
592 tfile->detached = NULL;
593 list_del_init(&tfile->next);
594 --tun->numdisabled;
595 return tun;
596}
597
Jason Wang4bfb0512013-09-05 17:53:59 +0800598static void tun_queue_purge(struct tun_file *tfile)
599{
Jason Wang1576d982016-06-30 14:45:36 +0800600 struct sk_buff *skb;
601
602 while ((skb = skb_array_consume(&tfile->tx_array)) != NULL)
603 kfree_skb(skb);
604
Jason Wang5503fce2017-01-18 15:02:03 +0800605 skb_queue_purge(&tfile->sk.sk_write_queue);
Jason Wang4bfb0512013-09-05 17:53:59 +0800606 skb_queue_purge(&tfile->sk.sk_error_queue);
607}
608
Jason Wangc8d68e62012-10-31 19:46:00 +0000609static void __tun_detach(struct tun_file *tfile, bool clean)
610{
611 struct tun_file *ntfile;
612 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000613
Jason Wangb8deabd2013-01-11 16:59:32 +0000614 tun = rtnl_dereference(tfile->tun);
615
Petar Penkov94317092017-09-22 13:49:14 -0700616 if (tun && clean) {
617 tun_napi_disable(tun, tfile);
618 tun_napi_del(tun, tfile);
619 }
620
Jason Wang9e857222013-01-28 01:05:19 +0000621 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000622 u16 index = tfile->queue_index;
623 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000624
625 rcu_assign_pointer(tun->tfiles[index],
626 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000627 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000628 ntfile->queue_index = index;
629
630 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000631 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530632 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000633 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000634 } else
Jason Wang4008e972012-12-13 23:53:30 +0000635 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000636
637 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000638 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000639 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800640 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000641 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000642 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000643 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000644 sock_put(&tfile->sk);
645 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000646
647 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000648 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
649 netif_carrier_off(tun->dev);
650
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200651 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000652 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000653 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000654 }
Jason Wang1576d982016-06-30 14:45:36 +0800655 if (tun)
656 skb_array_cleanup(&tfile->tx_array);
Eric W. Biederman140e807da2015-05-08 21:07:08 -0500657 sock_put(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000658 }
659}
660
661static void tun_detach(struct tun_file *tfile, bool clean)
662{
663 rtnl_lock();
664 __tun_detach(tfile, clean);
665 rtnl_unlock();
666}
667
668static void tun_detach_all(struct net_device *dev)
669{
670 struct tun_struct *tun = netdev_priv(dev);
Jason Wang761876c2017-08-11 19:41:18 +0800671 struct bpf_prog *xdp_prog = rtnl_dereference(tun->xdp_prog);
Jason Wang4008e972012-12-13 23:53:30 +0000672 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000673 int i, n = tun->numqueues;
674
675 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000676 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000677 BUG_ON(!tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700678 tun_napi_disable(tun, tfile);
Jason Wangaddf8fc2016-05-19 13:36:51 +0800679 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700680 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530681 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000682 --tun->numqueues;
683 }
Jason Wang9e857222013-01-28 01:05:19 +0000684 list_for_each_entry(tfile, &tun->disabled, next) {
Jason Wangaddf8fc2016-05-19 13:36:51 +0800685 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700686 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530687 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000688 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000689 BUG_ON(tun->numqueues != 0);
690
691 synchronize_net();
692 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000693 tfile = rtnl_dereference(tun->tfiles[i]);
Petar Penkov94317092017-09-22 13:49:14 -0700694 tun_napi_del(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000695 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800696 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000697 sock_put(&tfile->sk);
698 }
Jason Wang4008e972012-12-13 23:53:30 +0000699 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
700 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800701 tun_queue_purge(tfile);
Jason Wang4008e972012-12-13 23:53:30 +0000702 sock_put(&tfile->sk);
703 }
704 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000705
Jason Wang761876c2017-08-11 19:41:18 +0800706 if (xdp_prog)
707 bpf_prog_put(xdp_prog);
708
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200709 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000710 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000711}
712
Petar Penkov94317092017-09-22 13:49:14 -0700713static int tun_attach(struct tun_struct *tun, struct file *file,
714 bool skip_filter, bool napi)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000715{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000716 struct tun_file *tfile = file->private_data;
Jason Wang1576d982016-06-30 14:45:36 +0800717 struct net_device *dev = tun->dev;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000718 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000719
Paul Moore5dbbaf22013-01-14 07:12:19 +0000720 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
721 if (err < 0)
722 goto out;
723
Eric W. Biederman38231b72009-01-20 11:02:28 +0000724 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000725 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000726 goto out;
727
728 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200729 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000730 goto out;
731
732 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000733 if (!tfile->detached &&
734 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000735 goto out;
736
737 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000738
stephen hemminger92d4ea62013-12-05 20:42:58 -0800739 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400740 if (!skip_filter && (tun->filter_attached == true)) {
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +0200741 lock_sock(tfile->socket.sk);
742 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
743 release_sock(tfile->socket.sk);
Jason Wang54f968d2012-10-31 19:45:57 +0000744 if (!err)
745 goto out;
746 }
Jason Wang1576d982016-06-30 14:45:36 +0800747
748 if (!tfile->detached &&
749 skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) {
750 err = -ENOMEM;
751 goto out;
752 }
753
Jason Wangc8d68e62012-10-31 19:46:00 +0000754 tfile->queue_index = tun->numqueues;
Jason Wangaddf8fc2016-05-19 13:36:51 +0800755 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
Jason Wang6e914fc2012-10-31 19:45:58 +0000756 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000757 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000758 tun->numqueues++;
759
Petar Penkov94317092017-09-22 13:49:14 -0700760 if (tfile->detached) {
Jason Wang4008e972012-12-13 23:53:30 +0000761 tun_enable_queue(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700762 } else {
Jason Wang4008e972012-12-13 23:53:30 +0000763 sock_hold(&tfile->sk);
Petar Penkov94317092017-09-22 13:49:14 -0700764 tun_napi_init(tun, tfile, napi);
765 }
Jason Wang4008e972012-12-13 23:53:30 +0000766
Jason Wangc8d68e62012-10-31 19:46:00 +0000767 tun_set_real_num_queues(tun);
768
Jason Wangc8d68e62012-10-31 19:46:00 +0000769 /* device is allowed to go away first, so no need to hold extra
770 * refcnt.
771 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000772
Eric W. Biederman38231b72009-01-20 11:02:28 +0000773out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000774 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000775}
776
Eric W. Biederman631ab462009-01-20 11:00:40 +0000777static struct tun_struct *__tun_get(struct tun_file *tfile)
778{
Jason Wang6e914fc2012-10-31 19:45:58 +0000779 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000780
Jason Wang6e914fc2012-10-31 19:45:58 +0000781 rcu_read_lock();
782 tun = rcu_dereference(tfile->tun);
783 if (tun)
784 dev_hold(tun->dev);
785 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000786
787 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000788}
789
790static struct tun_struct *tun_get(struct file *file)
791{
792 return __tun_get(file->private_data);
793}
794
795static void tun_put(struct tun_struct *tun)
796{
Jason Wang6e914fc2012-10-31 19:45:58 +0000797 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000798}
799
Joe Perches6b8a66e2011-03-02 07:18:10 +0000800/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700801static void addr_hash_set(u32 *mask, const u8 *addr)
802{
803 int n = ether_crc(ETH_ALEN, addr) >> 26;
804 mask[n >> 5] |= (1 << (n & 31));
805}
806
807static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
808{
809 int n = ether_crc(ETH_ALEN, addr) >> 26;
810 return mask[n >> 5] & (1 << (n & 31));
811}
812
813static int update_filter(struct tap_filter *filter, void __user *arg)
814{
815 struct { u8 u[ETH_ALEN]; } *addr;
816 struct tun_filter uf;
817 int err, alen, n, nexact;
818
819 if (copy_from_user(&uf, arg, sizeof(uf)))
820 return -EFAULT;
821
822 if (!uf.count) {
823 /* Disabled */
824 filter->count = 0;
825 return 0;
826 }
827
828 alen = ETH_ALEN * uf.count;
Markus Elfring28e81902016-08-20 08:54:15 +0200829 addr = memdup_user(arg + sizeof(uf), alen);
830 if (IS_ERR(addr))
831 return PTR_ERR(addr);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700832
833 /* The filter is updated without holding any locks. Which is
834 * perfectly safe. We disable it first and in the worst
835 * case we'll accept a few undesired packets. */
836 filter->count = 0;
837 wmb();
838
839 /* Use first set of addresses as an exact filter */
840 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
841 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
842
843 nexact = n;
844
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800845 /* Remaining multicast addresses are hashed,
846 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700847 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800848 for (; n < uf.count; n++) {
849 if (!is_multicast_ether_addr(addr[n].u)) {
850 err = 0; /* no filter */
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200851 goto free_addr;
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800852 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700853 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800854 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700855
856 /* For ALLMULTI just set the mask to all ones.
857 * This overrides the mask populated above. */
858 if ((uf.flags & TUN_FLT_ALLMULTI))
859 memset(filter->mask, ~0, sizeof(filter->mask));
860
861 /* Now enable the filter */
862 wmb();
863 filter->count = nexact;
864
865 /* Return the number of exact filters */
866 err = nexact;
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200867free_addr:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700868 kfree(addr);
869 return err;
870}
871
872/* Returns: 0 - drop, !=0 - accept */
873static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
874{
875 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
876 * at this point. */
877 struct ethhdr *eh = (struct ethhdr *) skb->data;
878 int i;
879
880 /* Exact match */
881 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000882 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700883 return 1;
884
885 /* Inexact match (multicast only) */
886 if (is_multicast_ether_addr(eh->h_dest))
887 return addr_hash_test(filter->mask, eh->h_dest);
888
889 return 0;
890}
891
892/*
893 * Checks whether the packet is accepted or not.
894 * Returns: 0 - drop, !=0 - accept
895 */
896static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
897{
898 if (!filter->count)
899 return 1;
900
901 return run_filter(filter, skb);
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/* Network device part of the driver */
905
Jeff Garzik7282d492006-09-13 14:30:00 -0400906static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000908/* Net device detach from fd. */
909static void tun_net_uninit(struct net_device *dev)
910{
Jason Wangc8d68e62012-10-31 19:46:00 +0000911 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914/* Net device open. */
915static int tun_net_open(struct net_device *dev)
916{
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +0100917 struct tun_struct *tun = netdev_priv(dev);
918 int i;
919
Jason Wangc8d68e62012-10-31 19:46:00 +0000920 netif_tx_start_all_queues(dev);
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +0100921
922 for (i = 0; i < tun->numqueues; i++) {
923 struct tun_file *tfile;
924
925 tfile = rtnl_dereference(tun->tfiles[i]);
926 tfile->socket.sk->sk_write_space(tfile->socket.sk);
927 }
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930}
931
932/* Net device close. */
933static int tun_net_close(struct net_device *dev)
934{
Jason Wangc8d68e62012-10-31 19:46:00 +0000935 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
939/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000940static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000943 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000944 struct tun_file *tfile;
Dominic Curranfa358642014-01-22 03:03:23 +0000945 u32 numqueues = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Jason Wang6e914fc2012-10-31 19:45:58 +0000947 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000948 tfile = rcu_dereference(tun->tfiles[txq]);
Dominic Curranfa358642014-01-22 03:03:23 +0000949 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /* Drop packet if interface is not attached */
Dominic Curranfa358642014-01-22 03:03:23 +0000952 if (txq >= numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 goto drop;
954
Jason Wang3df97ba2016-04-25 23:13:42 -0400955#ifdef CONFIG_RPS
956 if (numqueues == 1 && static_key_false(&rps_needed)) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800957 /* Select queue was not called for the skbuff, so we extract the
958 * RPS hash and save it into the flow_table here.
959 */
960 __u32 rxhash;
961
Jason Wangfeec0842017-06-06 14:09:49 +0800962 rxhash = __skb_get_hash_symmetric(skb);
Tom Herbert9bc88932013-12-22 18:54:32 +0800963 if (rxhash) {
964 struct tun_flow_entry *e;
965 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
966 rxhash);
967 if (e)
968 tun_flow_save_rps_rxhash(e, rxhash);
969 }
970 }
Jason Wang3df97ba2016-04-25 23:13:42 -0400971#endif
Tom Herbert9bc88932013-12-22 18:54:32 +0800972
Jason Wang6e914fc2012-10-31 19:45:58 +0000973 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
974
Jason Wangc8d68e62012-10-31 19:46:00 +0000975 BUG_ON(!tfile);
976
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700977 /* Drop if the filter does not like it.
978 * This is a noop if the filter is disabled.
979 * Filter can be enabled only for the TAP devices. */
980 if (!check_filter(&tun->txflt, skb))
981 goto drop;
982
Jason Wang54f968d2012-10-31 19:45:57 +0000983 if (tfile->socket.sk->sk_filter &&
984 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000985 goto drop;
986
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400987 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Jason Wang7bf66302013-09-05 17:54:00 +0800988 goto drop;
989
Soheil Hassas Yeganeh7b996242016-08-23 18:22:33 -0400990 skb_tx_timestamp(skb);
Richard Cochraneda29772013-07-19 19:40:10 +0200991
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000992 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800993 * for indefinite time.
994 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000995 skb_orphan(skb);
996
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000997 nf_reset(skb);
998
Jason Wang1576d982016-06-30 14:45:36 +0800999 if (skb_array_produce(&tfile->tx_array, skb))
1000 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +00001003 if (tfile->flags & TUN_FASYNC)
1004 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -07001005 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +00001006
1007 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +00001008 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010drop:
Paolo Abeni608b9972016-04-13 10:52:20 +02001011 this_cpu_inc(tun->pcpu_stats->tx_dropped);
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +00001012 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +00001014 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +08001015 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001018static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001020 /*
1021 * This callback is supposed to deal with mc filter in
1022 * _rx_ path and has nothing to do with the _tx_ path.
1023 * In rx path we always accept everything userspace gives us.
1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001027static netdev_features_t tun_net_fix_features(struct net_device *dev,
1028 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +00001029{
1030 struct tun_struct *tun = netdev_priv(dev);
1031
1032 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1033}
Neil Hormanbebd0972011-06-15 05:25:01 +00001034#ifdef CONFIG_NET_POLL_CONTROLLER
1035static void tun_poll_controller(struct net_device *dev)
1036{
1037 /*
1038 * Tun only receives frames when:
1039 * 1) the char device endpoint gets data from user space
1040 * 2) the tun socket gets a sendmsg call from user space
Petar Penkov94317092017-09-22 13:49:14 -07001041 * If NAPI is not enabled, since both of those are synchronous
1042 * operations, we are guaranteed never to have pending data when we poll
1043 * for it so there is nothing to do here but return.
Neil Hormanbebd0972011-06-15 05:25:01 +00001044 * We need this though so netpoll recognizes us as an interface that
1045 * supports polling, which enables bridge devices in virt setups to
1046 * still use netconsole
Petar Penkov94317092017-09-22 13:49:14 -07001047 * If NAPI is enabled, however, we need to schedule polling for all
Petar Penkov90e33d42017-09-22 13:49:15 -07001048 * queues unless we are using napi_gro_frags(), which we call in
1049 * process context and not in NAPI context.
Neil Hormanbebd0972011-06-15 05:25:01 +00001050 */
Petar Penkov94317092017-09-22 13:49:14 -07001051 struct tun_struct *tun = netdev_priv(dev);
1052
1053 if (tun->flags & IFF_NAPI) {
1054 struct tun_file *tfile;
1055 int i;
1056
Petar Penkov90e33d42017-09-22 13:49:15 -07001057 if (tun_napi_frags_enabled(tun))
1058 return;
1059
Petar Penkov94317092017-09-22 13:49:14 -07001060 rcu_read_lock();
1061 for (i = 0; i < tun->numqueues; i++) {
1062 tfile = rcu_dereference(tun->tfiles[i]);
1063 napi_schedule(&tfile->napi);
1064 }
1065 rcu_read_unlock();
1066 }
Neil Hormanbebd0972011-06-15 05:25:01 +00001067 return;
1068}
1069#endif
Paolo Abenieaea34b2016-02-26 10:45:40 +01001070
1071static void tun_set_headroom(struct net_device *dev, int new_hr)
1072{
1073 struct tun_struct *tun = netdev_priv(dev);
1074
1075 if (new_hr < NET_SKB_PAD)
1076 new_hr = NET_SKB_PAD;
1077
1078 tun->align = new_hr;
1079}
1080
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001081static void
Paolo Abeni608b9972016-04-13 10:52:20 +02001082tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1083{
1084 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
1085 struct tun_struct *tun = netdev_priv(dev);
1086 struct tun_pcpu_stats *p;
1087 int i;
1088
1089 for_each_possible_cpu(i) {
1090 u64 rxpackets, rxbytes, txpackets, txbytes;
1091 unsigned int start;
1092
1093 p = per_cpu_ptr(tun->pcpu_stats, i);
1094 do {
1095 start = u64_stats_fetch_begin(&p->syncp);
1096 rxpackets = p->rx_packets;
1097 rxbytes = p->rx_bytes;
1098 txpackets = p->tx_packets;
1099 txbytes = p->tx_bytes;
1100 } while (u64_stats_fetch_retry(&p->syncp, start));
1101
1102 stats->rx_packets += rxpackets;
1103 stats->rx_bytes += rxbytes;
1104 stats->tx_packets += txpackets;
1105 stats->tx_bytes += txbytes;
1106
1107 /* u32 counters */
1108 rx_dropped += p->rx_dropped;
1109 rx_frame_errors += p->rx_frame_errors;
1110 tx_dropped += p->tx_dropped;
1111 }
1112 stats->rx_dropped = rx_dropped;
1113 stats->rx_frame_errors = rx_frame_errors;
1114 stats->tx_dropped = tx_dropped;
Paolo Abeni608b9972016-04-13 10:52:20 +02001115}
1116
Jason Wang761876c2017-08-11 19:41:18 +08001117static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1118 struct netlink_ext_ack *extack)
1119{
1120 struct tun_struct *tun = netdev_priv(dev);
1121 struct bpf_prog *old_prog;
1122
1123 old_prog = rtnl_dereference(tun->xdp_prog);
1124 rcu_assign_pointer(tun->xdp_prog, prog);
1125 if (old_prog)
1126 bpf_prog_put(old_prog);
1127
1128 return 0;
1129}
1130
1131static u32 tun_xdp_query(struct net_device *dev)
1132{
1133 struct tun_struct *tun = netdev_priv(dev);
1134 const struct bpf_prog *xdp_prog;
1135
1136 xdp_prog = rtnl_dereference(tun->xdp_prog);
1137 if (xdp_prog)
1138 return xdp_prog->aux->id;
1139
1140 return 0;
1141}
1142
1143static int tun_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1144{
1145 switch (xdp->command) {
1146 case XDP_SETUP_PROG:
1147 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1148 case XDP_QUERY_PROG:
1149 xdp->prog_id = tun_xdp_query(dev);
1150 xdp->prog_attached = !!xdp->prog_id;
1151 return 0;
1152 default:
1153 return -EINVAL;
1154 }
1155}
1156
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001157static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001158 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001159 .ndo_open = tun_net_open,
1160 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001161 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001162 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +00001163 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +00001164#ifdef CONFIG_NET_POLL_CONTROLLER
1165 .ndo_poll_controller = tun_poll_controller,
1166#endif
Paolo Abenieaea34b2016-02-26 10:45:40 +01001167 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001168 .ndo_get_stats64 = tun_net_get_stats64,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001169};
1170
1171static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001172 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001173 .ndo_open = tun_net_open,
1174 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001175 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001176 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001177 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001178 .ndo_set_mac_address = eth_mac_addr,
1179 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +00001180 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +00001181#ifdef CONFIG_NET_POLL_CONTROLLER
1182 .ndo_poll_controller = tun_poll_controller,
1183#endif
Toshiaki Makita5e527962015-07-31 15:03:27 +09001184 .ndo_features_check = passthru_features_check,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001185 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001186 .ndo_get_stats64 = tun_net_get_stats64,
Jason Wang761876c2017-08-11 19:41:18 +08001187 .ndo_xdp = tun_xdp,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001188};
1189
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001190static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +00001191{
1192 int i;
1193
Jason Wang96442e422012-10-31 19:46:02 +00001194 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1195 INIT_HLIST_HEAD(&tun->flows[i]);
1196
1197 tun->ageing_time = TUN_FLOW_EXPIRE;
1198 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
1199 mod_timer(&tun->flow_gc_timer,
1200 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +00001201}
1202
1203static void tun_flow_uninit(struct tun_struct *tun)
1204{
1205 del_timer_sync(&tun->flow_gc_timer);
1206 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001207}
1208
Jarod Wilson91572082016-10-20 13:55:20 -04001209#define MIN_MTU 68
1210#define MAX_MTU 65535
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/* Initialize net device. */
1213static void tun_net_init(struct net_device *dev)
1214{
1215 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001218 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001219 dev->netdev_ops = &tun_netdev_ops;
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* Point-to-Point TUN Device */
1222 dev->hard_header_len = 0;
1223 dev->addr_len = 0;
1224 dev->mtu = 1500;
1225
1226 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001227 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 break;
1230
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001231 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -08001232 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001234 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +00001235 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +00001236 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001238 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 break;
1241 }
Jarod Wilson91572082016-10-20 13:55:20 -04001242
1243 dev->min_mtu = MIN_MTU;
1244 dev->max_mtu = MAX_MTU - dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
1247/* Character device part */
1248
1249/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +00001250static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001251{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +00001252 struct tun_file *tfile = file->private_data;
1253 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001254 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001255 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +00001258 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Jason Wang54f968d2012-10-31 19:45:57 +00001260 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001261
Joe Perches6b8a66e2011-03-02 07:18:10 +00001262 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Xi Wang9e641bd2014-05-16 15:11:48 -07001264 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001265
Jason Wang1576d982016-06-30 14:45:36 +08001266 if (!skb_array_empty(&tfile->tx_array))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 mask |= POLLIN | POLLRDNORM;
1268
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001269 if (tun->dev->flags & IFF_UP &&
1270 (sock_writeable(sk) ||
1271 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1272 sock_writeable(sk))))
Herbert Xu33dccbb2009-02-05 21:25:32 -08001273 mask |= POLLOUT | POLLWRNORM;
1274
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001275 if (tun->dev->reg_state != NETREG_REGISTERED)
1276 mask = POLLERR;
1277
Eric W. Biederman631ab462009-01-20 11:00:40 +00001278 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return mask;
1280}
1281
Petar Penkov90e33d42017-09-22 13:49:15 -07001282static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
1283 size_t len,
1284 const struct iov_iter *it)
1285{
1286 struct sk_buff *skb;
1287 size_t linear;
1288 int err;
1289 int i;
1290
1291 if (it->nr_segs > MAX_SKB_FRAGS + 1)
1292 return ERR_PTR(-ENOMEM);
1293
1294 local_bh_disable();
1295 skb = napi_get_frags(&tfile->napi);
1296 local_bh_enable();
1297 if (!skb)
1298 return ERR_PTR(-ENOMEM);
1299
1300 linear = iov_iter_single_seg_count(it);
1301 err = __skb_grow(skb, linear);
1302 if (err)
1303 goto free;
1304
1305 skb->len = len;
1306 skb->data_len = len - linear;
1307 skb->truesize += skb->data_len;
1308
1309 for (i = 1; i < it->nr_segs; i++) {
1310 size_t fragsz = it->iov[i].iov_len;
1311 unsigned long offset;
1312 struct page *page;
1313 void *data;
1314
1315 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1316 err = -EINVAL;
1317 goto free;
1318 }
1319
1320 local_bh_disable();
1321 data = napi_alloc_frag(fragsz);
1322 local_bh_enable();
1323 if (!data) {
1324 err = -ENOMEM;
1325 goto free;
1326 }
1327
1328 page = virt_to_head_page(data);
1329 offset = data - page_address(page);
1330 skb_fill_page_desc(skb, i - 1, page, offset, fragsz);
1331 }
1332
1333 return skb;
1334free:
1335 /* frees skb and all frags allocated with napi_alloc_frag() */
1336 napi_free_frags(&tfile->napi);
1337 return ERR_PTR(err);
1338}
1339
Rusty Russellf42157c2008-08-15 15:15:10 -07001340/* prepad is the amount to reserve at front. len is length after that.
1341 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001342static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001343 size_t prepad, size_t len,
1344 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001345{
Jason Wang54f968d2012-10-31 19:45:57 +00001346 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001347 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001348 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001349
1350 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001351 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001352 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001353
Herbert Xu33dccbb2009-02-05 21:25:32 -08001354 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001355 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001356 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001357 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001358
1359 skb_reserve(skb, prepad);
1360 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001361 skb->data_len = len - linear;
1362 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001363
1364 return skb;
1365}
1366
Jason Wang5503fce2017-01-18 15:02:03 +08001367static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1368 struct sk_buff *skb, int more)
1369{
1370 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1371 struct sk_buff_head process_queue;
1372 u32 rx_batched = tun->rx_batched;
1373 bool rcv = false;
1374
1375 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1376 local_bh_disable();
1377 netif_receive_skb(skb);
1378 local_bh_enable();
1379 return;
1380 }
1381
1382 spin_lock(&queue->lock);
1383 if (!more || skb_queue_len(queue) == rx_batched) {
1384 __skb_queue_head_init(&process_queue);
1385 skb_queue_splice_tail_init(queue, &process_queue);
1386 rcv = true;
1387 } else {
1388 __skb_queue_tail(queue, skb);
1389 }
1390 spin_unlock(&queue->lock);
1391
1392 if (rcv) {
1393 struct sk_buff *nskb;
1394
1395 local_bh_disable();
1396 while ((nskb = __skb_dequeue(&process_queue)))
1397 netif_receive_skb(nskb);
1398 netif_receive_skb(skb);
1399 local_bh_enable();
1400 }
1401}
1402
Jason Wang66ccbc92017-08-11 19:41:16 +08001403static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1404 int len, int noblock, bool zerocopy)
1405{
1406 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1407 return false;
1408
1409 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1410 return false;
1411
1412 if (!noblock)
1413 return false;
1414
1415 if (zerocopy)
1416 return false;
1417
1418 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1419 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1420 return false;
1421
1422 return true;
1423}
1424
Jason Wang761876c2017-08-11 19:41:18 +08001425static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1426 struct tun_file *tfile,
Jason Wang66ccbc92017-08-11 19:41:16 +08001427 struct iov_iter *from,
Jason Wang761876c2017-08-11 19:41:18 +08001428 struct virtio_net_hdr *hdr,
Jason Wang1cfe6e92017-09-04 11:36:09 +08001429 int len, int *skb_xdp)
Jason Wang66ccbc92017-08-11 19:41:16 +08001430{
Eric Dumazet0bbd7da2017-08-16 22:14:33 +08001431 struct page_frag *alloc_frag = &current->task_frag;
Jason Wang66ccbc92017-08-11 19:41:16 +08001432 struct sk_buff *skb;
Jason Wang761876c2017-08-11 19:41:18 +08001433 struct bpf_prog *xdp_prog;
Jason Wang7df13212017-09-04 11:36:08 +08001434 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jason Wang761876c2017-08-11 19:41:18 +08001435 unsigned int delta = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001436 char *buf;
1437 size_t copied;
Jason Wang761876c2017-08-11 19:41:18 +08001438 bool xdp_xmit = false;
Jason Wang7df13212017-09-04 11:36:08 +08001439 int err, pad = TUN_RX_PAD;
1440
1441 rcu_read_lock();
1442 xdp_prog = rcu_dereference(tun->xdp_prog);
1443 if (xdp_prog)
1444 pad += TUN_HEADROOM;
1445 buflen += SKB_DATA_ALIGN(len + pad);
1446 rcu_read_unlock();
Jason Wang66ccbc92017-08-11 19:41:16 +08001447
1448 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1449 return ERR_PTR(-ENOMEM);
1450
1451 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1452 copied = copy_page_from_iter(alloc_frag->page,
Jason Wang7df13212017-09-04 11:36:08 +08001453 alloc_frag->offset + pad,
Jason Wang66ccbc92017-08-11 19:41:16 +08001454 len, from);
1455 if (copied != len)
1456 return ERR_PTR(-EFAULT);
1457
Jason Wang7df13212017-09-04 11:36:08 +08001458 /* There's a small window that XDP may be set after the check
1459 * of xdp_prog above, this should be rare and for simplicity
1460 * we do XDP on skb in case the headroom is not enough.
1461 */
1462 if (hdr->gso_type || !xdp_prog)
Jason Wang1cfe6e92017-09-04 11:36:09 +08001463 *skb_xdp = 1;
Jason Wang761876c2017-08-11 19:41:18 +08001464 else
Jason Wang1cfe6e92017-09-04 11:36:09 +08001465 *skb_xdp = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001466
Jason Wang761876c2017-08-11 19:41:18 +08001467 rcu_read_lock();
1468 xdp_prog = rcu_dereference(tun->xdp_prog);
Jason Wang1cfe6e92017-09-04 11:36:09 +08001469 if (xdp_prog && !*skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001470 struct xdp_buff xdp;
1471 void *orig_data;
1472 u32 act;
1473
1474 xdp.data_hard_start = buf;
Jason Wang7df13212017-09-04 11:36:08 +08001475 xdp.data = buf + pad;
Jason Wang761876c2017-08-11 19:41:18 +08001476 xdp.data_end = xdp.data + len;
1477 orig_data = xdp.data;
1478 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1479
1480 switch (act) {
1481 case XDP_REDIRECT:
1482 get_page(alloc_frag->page);
1483 alloc_frag->offset += buflen;
1484 err = xdp_do_redirect(tun->dev, &xdp, xdp_prog);
1485 if (err)
1486 goto err_redirect;
1487 return NULL;
1488 case XDP_TX:
1489 xdp_xmit = true;
1490 /* fall through */
1491 case XDP_PASS:
1492 delta = orig_data - xdp.data;
1493 break;
1494 default:
1495 bpf_warn_invalid_xdp_action(act);
1496 /* fall through */
1497 case XDP_ABORTED:
1498 trace_xdp_exception(tun->dev, xdp_prog, act);
1499 /* fall through */
1500 case XDP_DROP:
1501 goto err_xdp;
1502 }
1503 }
1504
1505 skb = build_skb(buf, buflen);
1506 if (!skb) {
1507 rcu_read_unlock();
1508 return ERR_PTR(-ENOMEM);
1509 }
1510
Jason Wang7df13212017-09-04 11:36:08 +08001511 skb_reserve(skb, pad - delta);
Jason Wang761876c2017-08-11 19:41:18 +08001512 skb_put(skb, len + delta);
Jason Wang66ccbc92017-08-11 19:41:16 +08001513 get_page(alloc_frag->page);
1514 alloc_frag->offset += buflen;
1515
Jason Wang761876c2017-08-11 19:41:18 +08001516 if (xdp_xmit) {
1517 skb->dev = tun->dev;
1518 generic_xdp_tx(skb, xdp_prog);
1519 rcu_read_lock();
1520 return NULL;
1521 }
1522
1523 rcu_read_unlock();
1524
Jason Wang66ccbc92017-08-11 19:41:16 +08001525 return skb;
Jason Wang761876c2017-08-11 19:41:18 +08001526
1527err_redirect:
1528 put_page(alloc_frag->page);
1529err_xdp:
1530 rcu_read_unlock();
1531 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1532 return NULL;
Jason Wang66ccbc92017-08-11 19:41:16 +08001533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001536static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001537 void *msg_control, struct iov_iter *from,
Jason Wang5503fce2017-01-18 15:02:03 +08001538 int noblock, bool more)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Harvey Harrison09640e62009-02-01 00:45:17 -08001540 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001542 size_t total_len = iov_iter_count(from);
Paolo Abenieaea34b2016-02-26 10:45:40 +01001543 size_t len = total_len, align = tun->align, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001544 struct virtio_net_hdr gso = { 0 };
Paolo Abeni608b9972016-04-13 10:52:20 +02001545 struct tun_pcpu_stats *stats;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001546 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001547 int copylen;
1548 bool zerocopy = false;
1549 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001550 u32 rxhash;
Jason Wang1cfe6e92017-09-04 11:36:09 +08001551 int skb_xdp = 1;
Petar Penkov90e33d42017-09-22 13:49:15 -07001552 bool frags = tun_napi_frags_enabled(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Eric Dumazet1bd4978a2015-12-16 08:57:37 -08001554 if (!(tun->dev->flags & IFF_UP))
1555 return -EIO;
1556
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001557 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001558 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001560 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Al Virocbbd26b2016-11-01 22:09:04 -04001562 if (!copy_from_iter_full(&pi, sizeof(pi), from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return -EFAULT;
1564 }
1565
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001566 if (tun->flags & IFF_VNET_HDR) {
Willem de Bruijne1edab82017-02-03 18:20:48 -05001567 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1568
1569 if (len < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001570 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001571 len -= vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001572
Al Virocbbd26b2016-11-01 22:09:04 -04001573 if (!copy_from_iter_full(&gso, sizeof(gso), from))
Rusty Russellf43798c2008-07-03 03:48:02 -07001574 return -EFAULT;
1575
Herbert Xu49091222009-06-08 00:20:01 -07001576 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001577 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1578 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 -07001579
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001580 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001581 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001582 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001583 }
1584
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001585 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001586 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001587 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001588 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001589 return -EINVAL;
1590 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001591
Jason Wang96f8d9e2013-11-13 14:00:39 +08001592 good_linear = SKB_MAX_HEAD(align);
1593
Jason Wang88529172013-07-18 10:55:15 +08001594 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001595 struct iov_iter i = *from;
1596
Jason Wang88529172013-07-18 10:55:15 +08001597 /* There are 256 bytes to be copied in skb, so there is
1598 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001599 * The rest of the buffer is mapped from userspace.
1600 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001601 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001602 if (copylen > good_linear)
1603 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001604 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001605 iov_iter_advance(&i, copylen);
1606 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001607 zerocopy = true;
1608 }
1609
Petar Penkov90e33d42017-09-22 13:49:15 -07001610 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001611 /* For the packet that is not easy to be processed
1612 * (e.g gso or jumbo packet), we will do it at after
1613 * skb was created with generic XDP routine.
1614 */
1615 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
Jason Wang66ccbc92017-08-11 19:41:16 +08001616 if (IS_ERR(skb)) {
Paolo Abeni608b9972016-04-13 10:52:20 +02001617 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001618 return PTR_ERR(skb);
1619 }
Jason Wang761876c2017-08-11 19:41:18 +08001620 if (!skb)
1621 return total_len;
Jason Wang66ccbc92017-08-11 19:41:16 +08001622 } else {
1623 if (!zerocopy) {
1624 copylen = len;
1625 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1626 linear = good_linear;
1627 else
1628 linear = tun16_to_cpu(tun, gso.hdr_len);
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Petar Penkov90e33d42017-09-22 13:49:15 -07001631 if (frags) {
1632 mutex_lock(&tfile->napi_mutex);
1633 skb = tun_napi_alloc_frags(tfile, copylen, from);
1634 /* tun_napi_alloc_frags() enforces a layout for the skb.
1635 * If zerocopy is enabled, then this layout will be
1636 * overwritten by zerocopy_sg_from_iter().
1637 */
1638 zerocopy = false;
1639 } else {
1640 skb = tun_alloc_skb(tfile, align, copylen, linear,
1641 noblock);
1642 }
1643
Jason Wang66ccbc92017-08-11 19:41:16 +08001644 if (IS_ERR(skb)) {
1645 if (PTR_ERR(skb) != -EAGAIN)
1646 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Petar Penkov90e33d42017-09-22 13:49:15 -07001647 if (frags)
1648 mutex_unlock(&tfile->napi_mutex);
Jason Wang66ccbc92017-08-11 19:41:16 +08001649 return PTR_ERR(skb);
1650 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001651
Jason Wang66ccbc92017-08-11 19:41:16 +08001652 if (zerocopy)
1653 err = zerocopy_sg_from_iter(skb, from);
1654 else
1655 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1656
1657 if (err) {
1658 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1659 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001660 if (frags) {
1661 tfile->napi.skb = NULL;
1662 mutex_unlock(&tfile->napi_mutex);
1663 }
1664
Jason Wang66ccbc92017-08-11 19:41:16 +08001665 return -EFAULT;
1666 }
Dave Jones8f227572006-03-11 18:49:13 -08001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001669 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
Paolo Abenidf10db92016-06-14 00:00:04 +02001670 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1671 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001672 if (frags) {
1673 tfile->napi.skb = NULL;
1674 mutex_unlock(&tfile->napi_mutex);
1675 }
1676
Paolo Abenidf10db92016-06-14 00:00:04 +02001677 return -EINVAL;
1678 }
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001681 case IFF_TUN:
1682 if (tun->flags & IFF_NO_PI) {
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001683 switch (skb->data[0] & 0xf0) {
1684 case 0x40:
1685 pi.proto = htons(ETH_P_IP);
1686 break;
1687 case 0x60:
1688 pi.proto = htons(ETH_P_IPV6);
1689 break;
1690 default:
Paolo Abeni608b9972016-04-13 10:52:20 +02001691 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001692 kfree_skb(skb);
1693 return -EINVAL;
1694 }
1695 }
1696
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001697 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001699 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001701 case IFF_TAP:
Petar Penkov90e33d42017-09-22 13:49:15 -07001702 if (!frags)
1703 skb->protocol = eth_type_trans(skb, tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001707 /* copy skb_ubuf_info for callback when skb has no error */
1708 if (zerocopy) {
1709 skb_shinfo(skb)->destructor_arg = msg_control;
1710 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001711 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangaf1cc7a2016-11-30 13:17:51 +08001712 } else if (msg_control) {
1713 struct ubuf_info *uarg = msg_control;
1714 uarg->callback(uarg, false);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001715 }
1716
Vlad Yasevich72f65102015-02-03 16:36:16 -05001717 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001718 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001719
Jason Wang1cfe6e92017-09-04 11:36:09 +08001720 if (skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001721 struct bpf_prog *xdp_prog;
1722 int ret;
1723
1724 rcu_read_lock();
1725 xdp_prog = rcu_dereference(tun->xdp_prog);
1726 if (xdp_prog) {
1727 ret = do_xdp_generic(xdp_prog, skb);
1728 if (ret != XDP_PASS) {
1729 rcu_read_unlock();
1730 return total_len;
1731 }
1732 }
1733 rcu_read_unlock();
1734 }
1735
Jason Wangfeec0842017-06-06 14:09:49 +08001736 rxhash = __skb_get_hash_symmetric(skb);
Petar Penkov94317092017-09-22 13:49:14 -07001737
Petar Penkov90e33d42017-09-22 13:49:15 -07001738 if (frags) {
1739 /* Exercise flow dissector code path. */
1740 u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb));
1741
1742 if (headlen > skb_headlen(skb) || headlen < ETH_HLEN) {
1743 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1744 napi_free_frags(&tfile->napi);
1745 mutex_unlock(&tfile->napi_mutex);
1746 WARN_ON(1);
1747 return -ENOMEM;
1748 }
1749
1750 local_bh_disable();
1751 napi_gro_frags(&tfile->napi);
1752 local_bh_enable();
1753 mutex_unlock(&tfile->napi_mutex);
1754 } else if (tun->flags & IFF_NAPI) {
Petar Penkov94317092017-09-22 13:49:14 -07001755 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1756 int queue_len;
1757
1758 spin_lock_bh(&queue->lock);
1759 __skb_queue_tail(queue, skb);
1760 queue_len = skb_queue_len(queue);
1761 spin_unlock(&queue->lock);
1762
1763 if (!more || queue_len > NAPI_POLL_WEIGHT)
1764 napi_schedule(&tfile->napi);
1765
1766 local_bh_enable();
1767 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
1768 tun_rx_batched(tun, tfile, skb, more);
1769 } else {
1770 netif_rx_ni(skb);
1771 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001772
Paolo Abeni608b9972016-04-13 10:52:20 +02001773 stats = get_cpu_ptr(tun->pcpu_stats);
1774 u64_stats_update_begin(&stats->syncp);
1775 stats->rx_packets++;
1776 stats->rx_bytes += len;
1777 u64_stats_update_end(&stats->syncp);
1778 put_cpu_ptr(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Jason Wang9e857222013-01-28 01:05:19 +00001780 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001781 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001782}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Al Virof5ff53b2014-06-19 15:36:49 -04001784static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001786 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -08001787 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001788 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001789 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 if (!tun)
1792 return -EBADFD;
1793
Jason Wang5503fce2017-01-18 15:02:03 +08001794 result = tun_get_user(tun, tfile, NULL, from,
1795 file->f_flags & O_NONBLOCK, false);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001796
1797 tun_put(tun);
1798 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001802static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001803 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001804 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08001805 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 struct tun_pi pi = { 0, skb->protocol };
Paolo Abeni608b9972016-04-13 10:52:20 +02001808 struct tun_pcpu_stats *stats;
Herbert Xue0b46d02014-11-07 21:22:23 +08001809 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08001810 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001811 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001812 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001813
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001814 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001815 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001817 if (tun->flags & IFF_VNET_HDR)
Willem de Bruijne1edab82017-02-03 18:20:48 -05001818 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Herbert Xue0b46d02014-11-07 21:22:23 +08001820 total = skb->len + vlan_hlen + vnet_hdr_sz;
1821
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001822 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001823 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return -EINVAL;
1825
Herbert Xue0b46d02014-11-07 21:22:23 +08001826 total += sizeof(pi);
1827 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* Packet will be striped */
1829 pi.flags |= TUN_PKT_STRIP;
1830 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001831
Herbert Xue0b46d02014-11-07 21:22:23 +08001832 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Herbert Xu2eb783c2014-11-03 04:30:14 +08001836 if (vnet_hdr_sz) {
Jarno Rajahalme9403cd72016-11-18 15:40:40 -08001837 struct virtio_net_hdr gso;
Mike Rapoport34166092016-06-08 16:09:20 +03001838
Herbert Xue0b46d02014-11-07 21:22:23 +08001839 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001840 return -EINVAL;
1841
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001842 if (virtio_net_hdr_from_skb(skb, &gso,
Jason Wang6391a442017-01-20 14:32:42 +08001843 tun_is_little_endian(tun), true)) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001844 struct skb_shared_info *sinfo = skb_shinfo(skb);
Mike Rapoport34166092016-06-08 16:09:20 +03001845 pr_err("unexpected GSO type: "
1846 "0x%x, gso_size %d, hdr_len %d\n",
1847 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1848 tun16_to_cpu(tun, gso.hdr_len));
1849 print_hex_dump(KERN_ERR, "tun: ",
1850 DUMP_PREFIX_NONE,
1851 16, 1, skb->head,
1852 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1853 WARN_ON_ONCE(1);
1854 return -EINVAL;
1855 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001856
Herbert Xue0b46d02014-11-07 21:22:23 +08001857 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07001858 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08001859
1860 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001861 }
1862
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001863 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001864 int ret;
Jason Wang6680ec62013-07-25 13:00:33 +08001865 struct {
1866 __be16 h_vlan_proto;
1867 __be16 h_vlan_TCI;
1868 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Jason Wang6680ec62013-07-25 13:00:33 +08001870 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001871 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Jason Wang6680ec62013-07-25 13:00:33 +08001873 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08001874
Herbert Xue0b46d02014-11-07 21:22:23 +08001875 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1876 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001877 goto done;
1878
Herbert Xue0b46d02014-11-07 21:22:23 +08001879 ret = copy_to_iter(&veth, sizeof(veth), iter);
1880 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001881 goto done;
1882 }
1883
Herbert Xue0b46d02014-11-07 21:22:23 +08001884 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08001885
1886done:
Paolo Abeni608b9972016-04-13 10:52:20 +02001887 /* caller is in process context, */
1888 stats = get_cpu_ptr(tun->pcpu_stats);
1889 u64_stats_update_begin(&stats->syncp);
1890 stats->tx_packets++;
1891 stats->tx_bytes += skb->len + vlan_hlen;
1892 u64_stats_update_end(&stats->syncp);
1893 put_cpu_ptr(tun->pcpu_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 return total;
1896}
1897
Jason Wang1576d982016-06-30 14:45:36 +08001898static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock,
1899 int *err)
1900{
1901 DECLARE_WAITQUEUE(wait, current);
1902 struct sk_buff *skb = NULL;
Jason Wangf48cc6b2016-07-04 13:53:38 +08001903 int error = 0;
Jason Wang1576d982016-06-30 14:45:36 +08001904
1905 skb = skb_array_consume(&tfile->tx_array);
1906 if (skb)
1907 goto out;
1908 if (noblock) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001909 error = -EAGAIN;
Jason Wang1576d982016-06-30 14:45:36 +08001910 goto out;
1911 }
1912
1913 add_wait_queue(&tfile->wq.wait, &wait);
1914 current->state = TASK_INTERRUPTIBLE;
1915
1916 while (1) {
1917 skb = skb_array_consume(&tfile->tx_array);
1918 if (skb)
1919 break;
1920 if (signal_pending(current)) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001921 error = -ERESTARTSYS;
Jason Wang1576d982016-06-30 14:45:36 +08001922 break;
1923 }
1924 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001925 error = -EFAULT;
Jason Wang1576d982016-06-30 14:45:36 +08001926 break;
1927 }
1928
1929 schedule();
1930 }
1931
1932 current->state = TASK_RUNNING;
1933 remove_wait_queue(&tfile->wq.wait, &wait);
1934
1935out:
Jason Wangf48cc6b2016-07-04 13:53:38 +08001936 *err = error;
Jason Wang1576d982016-06-30 14:45:36 +08001937 return skb;
1938}
1939
Jason Wang54f968d2012-10-31 19:45:57 +00001940static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05001941 struct iov_iter *to,
Jason Wangac77cfd2017-05-17 12:14:43 +08001942 int noblock, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Al Viro9b067032014-11-07 13:52:07 -05001944 ssize_t ret;
Jason Wang1576d982016-06-30 14:45:36 +08001945 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Rami Rosen3872baf2012-11-25 22:07:41 +00001947 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Al Viro9b067032014-11-07 13:52:07 -05001949 if (!iov_iter_count(to))
1950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Jason Wangac77cfd2017-05-17 12:14:43 +08001952 if (!skb) {
1953 /* Read frames from ring */
1954 skb = tun_ring_recv(tfile, noblock, &err);
1955 if (!skb)
1956 return err;
1957 }
Herbert Xue0b46d02014-11-07 21:22:23 +08001958
Al Viro9b067032014-11-07 13:52:07 -05001959 ret = tun_put_user(tun, tfile, skb, to);
Jason Wangf51a5e82014-12-01 16:53:15 +08001960 if (unlikely(ret < 0))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001961 kfree_skb(skb);
Jason Wangf51a5e82014-12-01 16:53:15 +08001962 else
1963 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001965 return ret;
1966}
1967
Al Viro9b067032014-11-07 13:52:07 -05001968static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001969{
1970 struct file *file = iocb->ki_filp;
1971 struct tun_file *tfile = file->private_data;
1972 struct tun_struct *tun = __tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05001973 ssize_t len = iov_iter_count(to), ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001974
1975 if (!tun)
1976 return -EBADFD;
Jason Wangac77cfd2017-05-17 12:14:43 +08001977 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL);
David S. Miller42404c02013-12-10 22:05:45 -05001978 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08001979 if (ret > 0)
1980 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001981 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return ret;
1983}
1984
Jason Wang96442e422012-10-31 19:46:02 +00001985static void tun_free_netdev(struct net_device *dev)
1986{
1987 struct tun_struct *tun = netdev_priv(dev);
1988
Jason Wang4008e972012-12-13 23:53:30 +00001989 BUG_ON(!(list_empty(&tun->disabled)));
Paolo Abeni608b9972016-04-13 10:52:20 +02001990 free_percpu(tun->pcpu_stats);
Jason Wang96442e422012-10-31 19:46:02 +00001991 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001992 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001993}
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995static void tun_setup(struct net_device *dev)
1996{
1997 struct tun_struct *tun = netdev_priv(dev);
1998
Eric W. Biederman0625c882012-02-07 16:48:55 -08001999 tun->owner = INVALID_UID;
2000 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 dev->ethtool_ops = &tun_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04002003 dev->needs_free_netdev = true;
2004 dev->priv_destructor = tun_free_netdev;
Jason Wang016adb72016-04-08 13:26:48 +08002005 /* We prefer our own queue length */
2006 dev->tx_queue_len = TUN_READQ_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002009/* Trivial set of netlink ops to allow deleting tun or tap
2010 * device with netlink.
2011 */
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002012static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2013 struct netlink_ext_ack *extack)
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002014{
2015 return -EINVAL;
2016}
2017
2018static struct rtnl_link_ops tun_link_ops __read_mostly = {
2019 .kind = DRV_NAME,
2020 .priv_size = sizeof(struct tun_struct),
2021 .setup = tun_setup,
2022 .validate = tun_validate,
2023};
2024
Herbert Xu33dccbb2009-02-05 21:25:32 -08002025static void tun_sock_write_space(struct sock *sk)
2026{
Jason Wang54f968d2012-10-31 19:45:57 +00002027 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00002028 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002029
2030 if (!sock_writeable(sk))
2031 return;
2032
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002033 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Herbert Xu33dccbb2009-02-05 21:25:32 -08002034 return;
2035
Eric Dumazet43815482010-04-29 11:01:49 +00002036 wqueue = sk_sleep(sk);
2037 if (wqueue && waitqueue_active(wqueue))
2038 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002039 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07002040
Jason Wang54f968d2012-10-31 19:45:57 +00002041 tfile = container_of(sk, struct tun_file, sk);
2042 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002043}
2044
Ying Xue1b784142015-03-02 15:37:48 +08002045static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002046{
Jason Wang54f968d2012-10-31 19:45:57 +00002047 int ret;
2048 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2049 struct tun_struct *tun = __tun_get(tfile);
2050
2051 if (!tun)
2052 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04002053
Al Viroc0371da2014-11-24 10:42:55 -05002054 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
Jason Wang5503fce2017-01-18 15:02:03 +08002055 m->msg_flags & MSG_DONTWAIT,
2056 m->msg_flags & MSG_MORE);
Jason Wang54f968d2012-10-31 19:45:57 +00002057 tun_put(tun);
2058 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002059}
2060
Ying Xue1b784142015-03-02 15:37:48 +08002061static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002062 int flags)
2063{
Jason Wang54f968d2012-10-31 19:45:57 +00002064 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2065 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002066 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00002067
2068 if (!tun)
2069 return -EBADFD;
2070
Richard Cochraneda29772013-07-19 19:40:10 +02002071 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00002072 ret = -EINVAL;
2073 goto out;
2074 }
Richard Cochraneda29772013-07-19 19:40:10 +02002075 if (flags & MSG_ERRQUEUE) {
2076 ret = sock_recv_errqueue(sock->sk, m, total_len,
2077 SOL_PACKET, TUN_TX_TIMESTAMP);
2078 goto out;
2079 }
Jason Wangac77cfd2017-05-17 12:14:43 +08002080 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT,
2081 m->msg_control);
Alex Gartrell87897932014-12-25 23:05:03 -08002082 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05002083 m->msg_flags |= MSG_TRUNC;
2084 ret = flags & MSG_TRUNC ? ret : total_len;
2085 }
Gao feng3811ae72013-04-24 21:59:23 +00002086out:
Jason Wang54f968d2012-10-31 19:45:57 +00002087 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002088 return ret;
2089}
2090
Jason Wang1576d982016-06-30 14:45:36 +08002091static int tun_peek_len(struct socket *sock)
2092{
2093 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2094 struct tun_struct *tun;
2095 int ret = 0;
2096
2097 tun = __tun_get(tfile);
2098 if (!tun)
2099 return 0;
2100
2101 ret = skb_array_peek_len(&tfile->tx_array);
2102 tun_put(tun);
2103
2104 return ret;
2105}
2106
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002107/* Ops structure to mimic raw sockets with tun */
2108static const struct proto_ops tun_socket_ops = {
Jason Wang1576d982016-06-30 14:45:36 +08002109 .peek_len = tun_peek_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002110 .sendmsg = tun_sendmsg,
2111 .recvmsg = tun_recvmsg,
2112};
2113
Herbert Xu33dccbb2009-02-05 21:25:32 -08002114static struct proto tun_proto = {
2115 .name = "tun",
2116 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00002117 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08002118};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002119
David Woodhouse980c9e82009-05-09 22:54:21 -07002120static int tun_flags(struct tun_struct *tun)
2121{
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002122 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07002123}
2124
2125static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
2126 char *buf)
2127{
2128 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2129 return sprintf(buf, "0x%x\n", tun_flags(tun));
2130}
2131
2132static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
2133 char *buf)
2134{
2135 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002136 return uid_valid(tun->owner)?
2137 sprintf(buf, "%u\n",
2138 from_kuid_munged(current_user_ns(), tun->owner)):
2139 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002140}
2141
2142static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
2143 char *buf)
2144{
2145 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002146 return gid_valid(tun->group) ?
2147 sprintf(buf, "%u\n",
2148 from_kgid_munged(current_user_ns(), tun->group)):
2149 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002150}
2151
2152static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
2153static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
2154static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
2155
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002156static struct attribute *tun_dev_attrs[] = {
2157 &dev_attr_tun_flags.attr,
2158 &dev_attr_owner.attr,
2159 &dev_attr_group.attr,
2160 NULL
2161};
2162
2163static const struct attribute_group tun_attr_group = {
2164 .attrs = tun_dev_attrs
2165};
2166
Pavel Emelyanovd647a592008-04-16 00:41:16 -07002167static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00002170 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct net_device *dev;
2172 int err;
2173
Jason Wang7c0c3b12013-01-11 16:59:33 +00002174 if (tfile->detached)
2175 return -EINVAL;
2176
Petar Penkov90e33d42017-09-22 13:49:15 -07002177 if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
2178 if (!capable(CAP_NET_ADMIN))
2179 return -EPERM;
2180
2181 if (!(ifr->ifr_flags & IFF_NAPI) ||
2182 (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
2183 return -EINVAL;
2184 }
2185
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002186 dev = __dev_get_by_name(net, ifr->ifr_name);
2187 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07002188 if (ifr->ifr_flags & IFF_TUN_EXCL)
2189 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002190 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2191 tun = netdev_priv(dev);
2192 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2193 tun = netdev_priv(dev);
2194 else
2195 return -EINVAL;
2196
Jason Wang8e6d91a2013-05-28 18:32:11 +00002197 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002198 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00002199 return -EINVAL;
2200
Jason Wangcde8b152012-10-31 19:46:01 +00002201 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04002202 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002203 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04002204 if (err < 0)
2205 return err;
2206
Petar Penkov94317092017-09-22 13:49:14 -07002207 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
2208 ifr->ifr_flags & IFF_NAPI);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00002209 if (err < 0)
2210 return err;
Jason Wang4008e972012-12-13 23:53:30 +00002211
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002212 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00002213 (tun->numqueues + tun->numdisabled > 1)) {
2214 /* One or more queue has already been attached, no need
2215 * to initialize the device again.
2216 */
2217 return 0;
2218 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 else {
2221 char *name;
2222 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00002223 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2224 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Eric W. Biedermanc260b772012-11-18 21:34:11 +00002226 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002227 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04002228 err = security_tun_dev_create();
2229 if (err < 0)
2230 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /* Set dev type */
2233 if (ifr->ifr_flags & IFF_TUN) {
2234 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002235 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 name = "tun%d";
2237 } else if (ifr->ifr_flags & IFF_TAP) {
2238 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002239 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002241 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00002242 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (*ifr->ifr_name)
2245 name = ifr->ifr_name;
2246
Jason Wangc8d68e62012-10-31 19:46:00 +00002247 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02002248 NET_NAME_UNKNOWN, tun_setup, queues,
2249 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (!dev)
2252 return -ENOMEM;
2253
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07002254 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002255 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002256 dev->ifindex = tfile->ifindex;
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002257 dev->sysfs_groups[0] = &tun_attr_group;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 tun = netdev_priv(dev);
2260 tun->dev = dev;
2261 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002262 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002263 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Paolo Abenieaea34b2016-02-26 10:45:40 +01002265 tun->align = NET_SKB_PAD;
Jason Wang54f968d2012-10-31 19:45:57 +00002266 tun->filter_attached = false;
2267 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Jason Wang5503fce2017-01-18 15:02:03 +08002268 tun->rx_batched = 0;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002269
Paolo Abeni608b9972016-04-13 10:52:20 +02002270 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2271 if (!tun->pcpu_stats) {
2272 err = -ENOMEM;
2273 goto err_free_dev;
2274 }
2275
Jason Wang96442e422012-10-31 19:46:02 +00002276 spin_lock_init(&tun->lock);
2277
Paul Moore5dbbaf22013-01-14 07:12:19 +00002278 err = security_tun_dev_alloc_security(&tun->security);
2279 if (err < 0)
Paolo Abeni608b9972016-04-13 10:52:20 +02002280 goto err_free_stat;
Paul Moore2b980db2009-08-28 18:12:43 -04002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04002283 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00002284
Michał Mirosław88255372011-04-19 06:13:10 +00002285 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08002286 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2287 NETIF_F_HW_VLAN_STAG_TX;
Paolo Abeni2a2bbf12016-04-14 18:39:39 +02002288 dev->features = dev->hw_features | NETIF_F_LLTX;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09002289 dev->vlan_features = dev->features &
2290 ~(NETIF_F_HW_VLAN_CTAG_TX |
2291 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00002292
Jason Wang4008e972012-12-13 23:53:30 +00002293 INIT_LIST_HEAD(&tun->disabled);
Petar Penkov94317092017-09-22 13:49:14 -07002294 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI);
Jason Wangeb0fb362012-12-02 17:19:45 +00002295 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002296 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 err = register_netdevice(tun->dev);
2299 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002300 goto err_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00002303 netif_carrier_on(tun->dev);
2304
Joe Perches6b8a66e2011-03-02 07:18:10 +00002305 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002307 tun->flags = (tun->flags & ~TUN_FEATURES) |
2308 (ifr->ifr_flags & TUN_FEATURES);
Jason Wangc8d68e62012-10-31 19:46:00 +00002309
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002310 /* Make sure persistent devices do not get stuck in
2311 * xoff state.
2312 */
2313 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00002314 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 strcpy(ifr->ifr_name, tun->dev->name);
2317 return 0;
2318
Jason Wang662ca432013-09-11 18:09:48 +08002319err_detach:
2320 tun_detach_all(dev);
Eric Dumazetff244c62017-08-18 13:39:56 -07002321 /* register_netdevice() already called tun_free_netdev() */
2322 goto err_free_dev;
2323
Jason Wang662ca432013-09-11 18:09:48 +08002324err_free_flow:
2325 tun_flow_uninit(tun);
2326 security_tun_dev_free_security(tun->security);
Paolo Abeni608b9972016-04-13 10:52:20 +02002327err_free_stat:
2328 free_percpu(tun->pcpu_stats);
Jason Wang662ca432013-09-11 18:09:48 +08002329err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return err;
2332}
2333
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002334static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00002335 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07002336{
Joe Perches6b8a66e2011-03-02 07:18:10 +00002337 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07002338
2339 strcpy(ifr->ifr_name, tun->dev->name);
2340
David Woodhouse980c9e82009-05-09 22:54:21 -07002341 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07002342
Mark McLoughline3b99552008-08-15 15:09:56 -07002343}
2344
Rusty Russell5228ddc2008-07-03 03:46:16 -07002345/* This is like a cut-down ethtool ops, except done via tun fd so no
2346 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00002347static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07002348{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002349 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002350
2351 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00002352 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002353 arg &= ~TUN_F_CSUM;
2354
2355 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2356 if (arg & TUN_F_TSO_ECN) {
2357 features |= NETIF_F_TSO_ECN;
2358 arg &= ~TUN_F_TSO_ECN;
2359 }
2360 if (arg & TUN_F_TSO4)
2361 features |= NETIF_F_TSO;
2362 if (arg & TUN_F_TSO6)
2363 features |= NETIF_F_TSO6;
2364 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2365 }
2366 }
2367
2368 /* This gives the user a way to test for new features in future by
2369 * trying to set them. */
2370 if (arg)
2371 return -EINVAL;
2372
Michał Mirosław88255372011-04-19 06:13:10 +00002373 tun->set_features = features;
Yaroslav Isakov09050952017-03-16 22:44:10 +03002374 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2375 tun->dev->wanted_features |= features;
Michał Mirosław88255372011-04-19 06:13:10 +00002376 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07002377
2378 return 0;
2379}
2380
Jason Wangc8d68e62012-10-31 19:46:00 +00002381static void tun_detach_filter(struct tun_struct *tun, int n)
2382{
2383 int i;
2384 struct tun_file *tfile;
2385
2386 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002387 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002388 lock_sock(tfile->socket.sk);
2389 sk_detach_filter(tfile->socket.sk);
2390 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002391 }
2392
2393 tun->filter_attached = false;
2394}
2395
2396static int tun_attach_filter(struct tun_struct *tun)
2397{
2398 int i, ret = 0;
2399 struct tun_file *tfile;
2400
2401 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002402 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002403 lock_sock(tfile->socket.sk);
2404 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2405 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002406 if (ret) {
2407 tun_detach_filter(tun, i);
2408 return ret;
2409 }
2410 }
2411
2412 tun->filter_attached = true;
2413 return ret;
2414}
2415
2416static void tun_set_sndbuf(struct tun_struct *tun)
2417{
2418 struct tun_file *tfile;
2419 int i;
2420
2421 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002422 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00002423 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2424 }
2425}
2426
Jason Wangcde8b152012-10-31 19:46:01 +00002427static int tun_set_queue(struct file *file, struct ifreq *ifr)
2428{
2429 struct tun_file *tfile = file->private_data;
2430 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00002431 int ret = 0;
2432
2433 rtnl_lock();
2434
2435 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00002436 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002437 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00002438 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002439 goto unlock;
2440 }
2441 ret = security_tun_dev_attach_queue(tun->security);
2442 if (ret < 0)
2443 goto unlock;
Petar Penkov94317092017-09-22 13:49:14 -07002444 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI);
Jason Wang4008e972012-12-13 23:53:30 +00002445 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002446 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002447 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00002448 ret = -EINVAL;
2449 else
2450 __tun_detach(tfile, false);
2451 } else
Jason Wangcde8b152012-10-31 19:46:01 +00002452 ret = -EINVAL;
2453
Paul Moore5dbbaf22013-01-14 07:12:19 +00002454unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00002455 rtnl_unlock();
2456 return ret;
2457}
2458
Arnd Bergmann50857e22009-11-06 22:52:32 -08002459static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2460 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002462 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002463 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 void __user* argp = (void __user*)arg;
2465 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08002466 kuid_t owner;
2467 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002468 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002469 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002470 unsigned int ifindex;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002471 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002472 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Gao Feng20861f22016-10-27 09:05:22 +08002474 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08002475 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07002477 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00002478 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07002479 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002480 if (cmd == TUNGETFEATURES) {
2481 /* Currently this just means: "what IFF flags are valid?".
2482 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002483 * TUNSETIFF.
2484 */
2485 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00002486 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00002487 } else if (cmd == TUNSETQUEUE)
2488 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002489
Jason Wangc8d68e62012-10-31 19:46:00 +00002490 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00002491 rtnl_lock();
2492
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002493 tun = __tun_get(tfile);
Gao Feng0f16bc12016-10-25 22:26:09 +08002494 if (cmd == TUNSETIFF) {
2495 ret = -EEXIST;
2496 if (tun)
2497 goto unlock;
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2500
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002501 ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Herbert Xu876bfd42009-08-06 14:22:44 +00002503 if (ret)
2504 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Arnd Bergmann50857e22009-11-06 22:52:32 -08002506 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00002507 ret = -EFAULT;
2508 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002510 if (cmd == TUNSETIFINDEX) {
2511 ret = -EPERM;
2512 if (tun)
2513 goto unlock;
2514
2515 ret = -EFAULT;
2516 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2517 goto unlock;
2518
2519 ret = 0;
2520 tfile->ifindex = ifindex;
2521 goto unlock;
2522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Herbert Xu876bfd42009-08-06 14:22:44 +00002524 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00002526 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Jason Wang1e588332012-10-31 19:45:56 +00002528 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Eric W. Biederman631ab462009-01-20 11:00:40 +00002530 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07002532 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002533 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07002534
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002535 if (tfile->detached)
2536 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04002537 if (!tfile->socket.sk->sk_filter)
2538 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002539
Arnd Bergmann50857e22009-11-06 22:52:32 -08002540 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002541 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07002542 break;
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 case TUNSETNOCSUM:
2545 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Michał Mirosław88255372011-04-19 06:13:10 +00002547 /* [unimplemented] */
2548 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00002549 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 break;
2551
2552 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00002553 /* Disable/Enable persist mode. Keep an extra reference to the
2554 * module to prevent the module being unprobed.
2555 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002556 if (arg && !(tun->flags & IFF_PERSIST)) {
2557 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002558 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00002559 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002560 if (!arg && (tun->flags & IFF_PERSIST)) {
2561 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002562 module_put(THIS_MODULE);
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Joe Perches6b8a66e2011-03-02 07:18:10 +00002565 tun_debug(KERN_INFO, tun, "persist %s\n",
2566 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 break;
2568
2569 case TUNSETOWNER:
2570 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002571 owner = make_kuid(current_user_ns(), arg);
2572 if (!uid_valid(owner)) {
2573 ret = -EINVAL;
2574 break;
2575 }
2576 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00002577 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002578 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 break;
2580
Guido Guenther8c644622007-07-02 22:50:25 -07002581 case TUNSETGROUP:
2582 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002583 group = make_kgid(current_user_ns(), arg);
2584 if (!gid_valid(group)) {
2585 ret = -EINVAL;
2586 break;
2587 }
2588 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00002589 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002590 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07002591 break;
2592
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002593 case TUNSETLINK:
2594 /* Only allow setting the type when the interface is down */
2595 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002596 tun_debug(KERN_INFO, tun,
2597 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07002598 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002599 } else {
2600 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00002601 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
2602 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07002603 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002604 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002605 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607#ifdef TUN_DEBUG
2608 case TUNSETDEBUG:
2609 tun->debug = arg;
2610 break;
2611#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07002612 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00002613 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002614 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002615
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002616 case TUNSETTXFILTER:
2617 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00002618 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002619 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00002620 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07002621 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002625 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002626 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2627 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08002628 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002629 ret = -EFAULT;
2630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002633 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00002634 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2635 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08002636
Kim B. Heino40102372008-02-29 12:26:21 -08002637 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002638 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002639
2640 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002641 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002642 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2643 ret = -EFAULT;
2644 break;
2645
2646 case TUNSETSNDBUF:
2647 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2648 ret = -EFAULT;
2649 break;
2650 }
2651
Jason Wangc8d68e62012-10-31 19:46:00 +00002652 tun->sndbuf = sndbuf;
2653 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002654 break;
2655
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002656 case TUNGETVNETHDRSZ:
2657 vnet_hdr_sz = tun->vnet_hdr_sz;
2658 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2659 ret = -EFAULT;
2660 break;
2661
2662 case TUNSETVNETHDRSZ:
2663 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2664 ret = -EFAULT;
2665 break;
2666 }
2667 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2668 ret = -EINVAL;
2669 break;
2670 }
2671
2672 tun->vnet_hdr_sz = vnet_hdr_sz;
2673 break;
2674
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002675 case TUNGETVNETLE:
2676 le = !!(tun->flags & TUN_VNET_LE);
2677 if (put_user(le, (int __user *)argp))
2678 ret = -EFAULT;
2679 break;
2680
2681 case TUNSETVNETLE:
2682 if (get_user(le, (int __user *)argp)) {
2683 ret = -EFAULT;
2684 break;
2685 }
2686 if (le)
2687 tun->flags |= TUN_VNET_LE;
2688 else
2689 tun->flags &= ~TUN_VNET_LE;
2690 break;
2691
Greg Kurz8b8e6582015-04-24 14:50:36 +02002692 case TUNGETVNETBE:
2693 ret = tun_get_vnet_be(tun, argp);
2694 break;
2695
2696 case TUNSETVNETBE:
2697 ret = tun_set_vnet_be(tun, argp);
2698 break;
2699
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002700 case TUNATTACHFILTER:
2701 /* Can be set only for TAPs */
2702 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002703 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002704 break;
2705 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002706 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002707 break;
2708
Jason Wangc8d68e62012-10-31 19:46:00 +00002709 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002710 break;
2711
2712 case TUNDETACHFILTER:
2713 /* Can be set only for TAPs */
2714 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002715 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002716 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002717 ret = 0;
2718 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002719 break;
2720
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002721 case TUNGETFILTER:
2722 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002723 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002724 break;
2725 ret = -EFAULT;
2726 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2727 break;
2728 ret = 0;
2729 break;
2730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002732 ret = -EINVAL;
2733 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Herbert Xu876bfd42009-08-06 14:22:44 +00002736unlock:
2737 rtnl_unlock();
2738 if (tun)
2739 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002740 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
Arnd Bergmann50857e22009-11-06 22:52:32 -08002743static long tun_chr_ioctl(struct file *file,
2744 unsigned int cmd, unsigned long arg)
2745{
2746 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2747}
2748
2749#ifdef CONFIG_COMPAT
2750static long tun_chr_compat_ioctl(struct file *file,
2751 unsigned int cmd, unsigned long arg)
2752{
2753 switch (cmd) {
2754 case TUNSETIFF:
2755 case TUNGETIFF:
2756 case TUNSETTXFILTER:
2757 case TUNGETSNDBUF:
2758 case TUNSETSNDBUF:
2759 case SIOCGIFHWADDR:
2760 case SIOCSIFHWADDR:
2761 arg = (unsigned long)compat_ptr(arg);
2762 break;
2763 default:
2764 arg = (compat_ulong_t)arg;
2765 break;
2766 }
2767
2768 /*
2769 * compat_ifreq is shorter than ifreq, so we must not access beyond
2770 * the end of that structure. All fields that are used in this
2771 * driver are compatible though, we don't need to convert the
2772 * contents.
2773 */
2774 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2775}
2776#endif /* CONFIG_COMPAT */
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778static int tun_chr_fasync(int fd, struct file *file, int on)
2779{
Jason Wang54f968d2012-10-31 19:45:57 +00002780 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 int ret;
2782
Jason Wang54f968d2012-10-31 19:45:57 +00002783 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002784 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (on) {
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002787 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00002788 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002789 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002790 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002791 ret = 0;
2792out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002793 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796static int tun_chr_open(struct inode *inode, struct file * file)
2797{
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002798 struct net *net = current->nsproxy->net_ns;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002799 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002800
Joe Perches6b8a66e2011-03-02 07:18:10 +00002801 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002802
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002803 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05002804 &tun_proto, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002805 if (!tfile)
2806 return -ENOMEM;
Monam Agarwalc9566742014-03-24 00:02:32 +05302807 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang54f968d2012-10-31 19:45:57 +00002808 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002809 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002810
Jason Wang54f968d2012-10-31 19:45:57 +00002811 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07002812 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00002813
2814 tfile->socket.file = file;
2815 tfile->socket.ops = &tun_socket_ops;
2816
2817 sock_init_data(&tfile->socket, &tfile->sk);
Jason Wang54f968d2012-10-31 19:45:57 +00002818
2819 tfile->sk.sk_write_space = tun_sock_write_space;
2820 tfile->sk.sk_sndbuf = INT_MAX;
2821
Eric W. Biederman631ab462009-01-20 11:00:40 +00002822 file->private_data = tfile;
Jason Wang4008e972012-12-13 23:53:30 +00002823 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002824
Jason Wang19a6afb2013-06-08 14:17:41 +08002825 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return 0;
2828}
2829
2830static int tun_chr_close(struct inode *inode, struct file *file)
2831{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002832 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Jason Wangc8d68e62012-10-31 19:46:00 +00002834 tun_detach(tfile, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 return 0;
2837}
2838
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002839#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -07002840static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002841{
2842 struct tun_struct *tun;
2843 struct ifreq ifr;
2844
2845 memset(&ifr, 0, sizeof(ifr));
2846
2847 rtnl_lock();
2848 tun = tun_get(f);
2849 if (tun)
2850 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2851 rtnl_unlock();
2852
2853 if (tun)
2854 tun_put(tun);
2855
Joe Perchesa3816ab2014-09-29 16:08:25 -07002856 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002857}
2858#endif
2859
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002860static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002861 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05002863 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04002864 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002866 .unlocked_ioctl = tun_chr_ioctl,
2867#ifdef CONFIG_COMPAT
2868 .compat_ioctl = tun_chr_compat_ioctl,
2869#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 .open = tun_chr_open,
2871 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002872 .fasync = tun_chr_fasync,
2873#ifdef CONFIG_PROC_FS
2874 .show_fdinfo = tun_chr_show_fdinfo,
2875#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876};
2877
2878static struct miscdevice tun_miscdev = {
2879 .minor = TUN_MINOR,
2880 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002881 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883};
2884
2885/* ethtool interface */
2886
Philippe Reynes29ccc492017-03-11 22:03:50 +01002887static int tun_get_link_ksettings(struct net_device *dev,
2888 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Philippe Reynes29ccc492017-03-11 22:03:50 +01002890 ethtool_link_ksettings_zero_link_mode(cmd, supported);
2891 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
2892 cmd->base.speed = SPEED_10;
2893 cmd->base.duplex = DUPLEX_FULL;
2894 cmd->base.port = PORT_TP;
2895 cmd->base.phy_address = 0;
2896 cmd->base.autoneg = AUTONEG_DISABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return 0;
2898}
2899
2900static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2901{
2902 struct tun_struct *tun = netdev_priv(dev);
2903
Rick Jones33a5ba12011-11-15 14:59:53 +00002904 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2905 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002908 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00002909 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002911 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00002912 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 break;
2914 }
2915}
2916
2917static u32 tun_get_msglevel(struct net_device *dev)
2918{
2919#ifdef TUN_DEBUG
2920 struct tun_struct *tun = netdev_priv(dev);
2921 return tun->debug;
2922#else
2923 return -EOPNOTSUPP;
2924#endif
2925}
2926
2927static void tun_set_msglevel(struct net_device *dev, u32 value)
2928{
2929#ifdef TUN_DEBUG
2930 struct tun_struct *tun = netdev_priv(dev);
2931 tun->debug = value;
2932#endif
2933}
2934
Jason Wang5503fce2017-01-18 15:02:03 +08002935static int tun_get_coalesce(struct net_device *dev,
2936 struct ethtool_coalesce *ec)
2937{
2938 struct tun_struct *tun = netdev_priv(dev);
2939
2940 ec->rx_max_coalesced_frames = tun->rx_batched;
2941
2942 return 0;
2943}
2944
2945static int tun_set_coalesce(struct net_device *dev,
2946 struct ethtool_coalesce *ec)
2947{
2948 struct tun_struct *tun = netdev_priv(dev);
2949
2950 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
2951 tun->rx_batched = NAPI_POLL_WEIGHT;
2952 else
2953 tun->rx_batched = ec->rx_max_coalesced_frames;
2954
2955 return 0;
2956}
2957
Jeff Garzik7282d492006-09-13 14:30:00 -04002958static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 .get_drvinfo = tun_get_drvinfo,
2960 .get_msglevel = tun_get_msglevel,
2961 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002962 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002963 .get_ts_info = ethtool_op_get_ts_info,
Jason Wang5503fce2017-01-18 15:02:03 +08002964 .get_coalesce = tun_get_coalesce,
2965 .set_coalesce = tun_set_coalesce,
Philippe Reynes29ccc492017-03-11 22:03:50 +01002966 .get_link_ksettings = tun_get_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967};
2968
Jason Wang1576d982016-06-30 14:45:36 +08002969static int tun_queue_resize(struct tun_struct *tun)
2970{
2971 struct net_device *dev = tun->dev;
2972 struct tun_file *tfile;
2973 struct skb_array **arrays;
2974 int n = tun->numqueues + tun->numdisabled;
2975 int ret, i;
2976
stephen hemminger12039042017-08-15 10:29:16 -07002977 arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL);
Jason Wang1576d982016-06-30 14:45:36 +08002978 if (!arrays)
2979 return -ENOMEM;
2980
2981 for (i = 0; i < tun->numqueues; i++) {
2982 tfile = rtnl_dereference(tun->tfiles[i]);
2983 arrays[i] = &tfile->tx_array;
2984 }
2985 list_for_each_entry(tfile, &tun->disabled, next)
2986 arrays[i++] = &tfile->tx_array;
2987
2988 ret = skb_array_resize_multiple(arrays, n,
2989 dev->tx_queue_len, GFP_KERNEL);
2990
2991 kfree(arrays);
2992 return ret;
2993}
2994
2995static int tun_device_event(struct notifier_block *unused,
2996 unsigned long event, void *ptr)
2997{
2998 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2999 struct tun_struct *tun = netdev_priv(dev);
3000
Craig Gallek86dfb4ac2016-07-06 18:44:20 -04003001 if (dev->rtnl_link_ops != &tun_link_ops)
3002 return NOTIFY_DONE;
3003
Jason Wang1576d982016-06-30 14:45:36 +08003004 switch (event) {
3005 case NETDEV_CHANGE_TX_QUEUE_LEN:
3006 if (tun_queue_resize(tun))
3007 return NOTIFY_BAD;
3008 break;
3009 default:
3010 break;
3011 }
3012
3013 return NOTIFY_DONE;
3014}
3015
3016static struct notifier_block tun_notifier_block __read_mostly = {
3017 .notifier_call = tun_device_event,
3018};
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020static int __init tun_init(void)
3021{
3022 int ret = 0;
3023
Joe Perches6b8a66e2011-03-02 07:18:10 +00003024 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003026 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003027 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003028 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003029 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003030 }
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003033 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003034 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003035 goto err_misc;
3036 }
Jason Wang1576d982016-06-30 14:45:36 +08003037
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003038 ret = register_netdevice_notifier(&tun_notifier_block);
3039 if (ret) {
3040 pr_err("Can't register netdevice notifier\n");
3041 goto err_notifier;
3042 }
3043
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003044 return 0;
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003045
3046err_notifier:
3047 misc_deregister(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003048err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003049 rtnl_link_unregister(&tun_link_ops);
3050err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return ret;
3052}
3053
3054static void tun_cleanup(void)
3055{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003056 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003057 rtnl_link_unregister(&tun_link_ops);
Jason Wang1576d982016-06-30 14:45:36 +08003058 unregister_netdevice_notifier(&tun_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003061/* Get an underlying socket object from tun file. Returns error unless file is
3062 * attached to a device. The returned object works like a packet socket, it
3063 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
3064 * holding a reference to the file for as long as the socket is in use. */
3065struct socket *tun_get_socket(struct file *file)
3066{
Jason Wang6e914fc2012-10-31 19:45:58 +00003067 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003068 if (file->f_op != &tun_fops)
3069 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00003070 tfile = file->private_data;
3071 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003072 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00003073 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003074}
3075EXPORT_SYMBOL_GPL(tun_get_socket);
3076
Jason Wang83339c62017-05-17 12:14:41 +08003077struct skb_array *tun_get_skb_array(struct file *file)
3078{
3079 struct tun_file *tfile;
3080
3081 if (file->f_op != &tun_fops)
3082 return ERR_PTR(-EINVAL);
3083 tfile = file->private_data;
3084 if (!tfile)
3085 return ERR_PTR(-EBADFD);
3086 return &tfile->tx_array;
3087}
3088EXPORT_SYMBOL_GPL(tun_get_skb_array);
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090module_init(tun_init);
3091module_exit(tun_cleanup);
3092MODULE_DESCRIPTION(DRV_DESCRIPTION);
3093MODULE_AUTHOR(DRV_COPYRIGHT);
3094MODULE_LICENSE("GPL");
3095MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02003096MODULE_ALIAS("devname:net/tun");