blob: 2c36f6ebad7926bd04b8a3a7454fb5e438eb2aea [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
yuan linyu9484dc72017-09-23 22:36:52 +0800777static struct tun_struct *tun_get(struct tun_file *tfile)
Eric W. Biederman631ab462009-01-20 11:00:40 +0000778{
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
Eric W. Biederman631ab462009-01-20 11:00:40 +0000790static void tun_put(struct tun_struct *tun)
791{
Jason Wang6e914fc2012-10-31 19:45:58 +0000792 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000793}
794
Joe Perches6b8a66e2011-03-02 07:18:10 +0000795/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700796static void addr_hash_set(u32 *mask, const u8 *addr)
797{
798 int n = ether_crc(ETH_ALEN, addr) >> 26;
799 mask[n >> 5] |= (1 << (n & 31));
800}
801
802static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
803{
804 int n = ether_crc(ETH_ALEN, addr) >> 26;
805 return mask[n >> 5] & (1 << (n & 31));
806}
807
808static int update_filter(struct tap_filter *filter, void __user *arg)
809{
810 struct { u8 u[ETH_ALEN]; } *addr;
811 struct tun_filter uf;
812 int err, alen, n, nexact;
813
814 if (copy_from_user(&uf, arg, sizeof(uf)))
815 return -EFAULT;
816
817 if (!uf.count) {
818 /* Disabled */
819 filter->count = 0;
820 return 0;
821 }
822
823 alen = ETH_ALEN * uf.count;
Markus Elfring28e81902016-08-20 08:54:15 +0200824 addr = memdup_user(arg + sizeof(uf), alen);
825 if (IS_ERR(addr))
826 return PTR_ERR(addr);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700827
828 /* The filter is updated without holding any locks. Which is
829 * perfectly safe. We disable it first and in the worst
830 * case we'll accept a few undesired packets. */
831 filter->count = 0;
832 wmb();
833
834 /* Use first set of addresses as an exact filter */
835 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
836 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
837
838 nexact = n;
839
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800840 /* Remaining multicast addresses are hashed,
841 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700842 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800843 for (; n < uf.count; n++) {
844 if (!is_multicast_ether_addr(addr[n].u)) {
845 err = 0; /* no filter */
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200846 goto free_addr;
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800847 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700848 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800849 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700850
851 /* For ALLMULTI just set the mask to all ones.
852 * This overrides the mask populated above. */
853 if ((uf.flags & TUN_FLT_ALLMULTI))
854 memset(filter->mask, ~0, sizeof(filter->mask));
855
856 /* Now enable the filter */
857 wmb();
858 filter->count = nexact;
859
860 /* Return the number of exact filters */
861 err = nexact;
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200862free_addr:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700863 kfree(addr);
864 return err;
865}
866
867/* Returns: 0 - drop, !=0 - accept */
868static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
869{
870 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
871 * at this point. */
872 struct ethhdr *eh = (struct ethhdr *) skb->data;
873 int i;
874
875 /* Exact match */
876 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000877 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700878 return 1;
879
880 /* Inexact match (multicast only) */
881 if (is_multicast_ether_addr(eh->h_dest))
882 return addr_hash_test(filter->mask, eh->h_dest);
883
884 return 0;
885}
886
887/*
888 * Checks whether the packet is accepted or not.
889 * Returns: 0 - drop, !=0 - accept
890 */
891static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
892{
893 if (!filter->count)
894 return 1;
895
896 return run_filter(filter, skb);
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899/* Network device part of the driver */
900
Jeff Garzik7282d492006-09-13 14:30:00 -0400901static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000903/* Net device detach from fd. */
904static void tun_net_uninit(struct net_device *dev)
905{
Jason Wangc8d68e62012-10-31 19:46:00 +0000906 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/* Net device open. */
910static int tun_net_open(struct net_device *dev)
911{
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +0100912 struct tun_struct *tun = netdev_priv(dev);
913 int i;
914
Jason Wangc8d68e62012-10-31 19:46:00 +0000915 netif_tx_start_all_queues(dev);
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +0100916
917 for (i = 0; i < tun->numqueues; i++) {
918 struct tun_file *tfile;
919
920 tfile = rtnl_dereference(tun->tfiles[i]);
921 tfile->socket.sk->sk_write_space(tfile->socket.sk);
922 }
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925}
926
927/* Net device close. */
928static int tun_net_close(struct net_device *dev)
929{
Jason Wangc8d68e62012-10-31 19:46:00 +0000930 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932}
933
934/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000935static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000938 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000939 struct tun_file *tfile;
Dominic Curranfa358642014-01-22 03:03:23 +0000940 u32 numqueues = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Jason Wang6e914fc2012-10-31 19:45:58 +0000942 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000943 tfile = rcu_dereference(tun->tfiles[txq]);
Dominic Curranfa358642014-01-22 03:03:23 +0000944 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* Drop packet if interface is not attached */
Dominic Curranfa358642014-01-22 03:03:23 +0000947 if (txq >= numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto drop;
949
Jason Wang3df97ba2016-04-25 23:13:42 -0400950#ifdef CONFIG_RPS
951 if (numqueues == 1 && static_key_false(&rps_needed)) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800952 /* Select queue was not called for the skbuff, so we extract the
953 * RPS hash and save it into the flow_table here.
954 */
955 __u32 rxhash;
956
Jason Wangfeec0842017-06-06 14:09:49 +0800957 rxhash = __skb_get_hash_symmetric(skb);
Tom Herbert9bc88932013-12-22 18:54:32 +0800958 if (rxhash) {
959 struct tun_flow_entry *e;
960 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
961 rxhash);
962 if (e)
963 tun_flow_save_rps_rxhash(e, rxhash);
964 }
965 }
Jason Wang3df97ba2016-04-25 23:13:42 -0400966#endif
Tom Herbert9bc88932013-12-22 18:54:32 +0800967
Jason Wang6e914fc2012-10-31 19:45:58 +0000968 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
969
Jason Wangc8d68e62012-10-31 19:46:00 +0000970 BUG_ON(!tfile);
971
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700972 /* Drop if the filter does not like it.
973 * This is a noop if the filter is disabled.
974 * Filter can be enabled only for the TAP devices. */
975 if (!check_filter(&tun->txflt, skb))
976 goto drop;
977
Jason Wang54f968d2012-10-31 19:45:57 +0000978 if (tfile->socket.sk->sk_filter &&
979 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000980 goto drop;
981
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400982 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Jason Wang7bf66302013-09-05 17:54:00 +0800983 goto drop;
984
Soheil Hassas Yeganeh7b996242016-08-23 18:22:33 -0400985 skb_tx_timestamp(skb);
Richard Cochraneda29772013-07-19 19:40:10 +0200986
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000987 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800988 * for indefinite time.
989 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000990 skb_orphan(skb);
991
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000992 nf_reset(skb);
993
Jason Wang1576d982016-06-30 14:45:36 +0800994 if (skb_array_produce(&tfile->tx_array, skb))
995 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000998 if (tfile->flags & TUN_FASYNC)
999 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -07001000 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +00001001
1002 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +00001003 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005drop:
Paolo Abeni608b9972016-04-13 10:52:20 +02001006 this_cpu_inc(tun->pcpu_stats->tx_dropped);
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +00001007 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +00001009 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +08001010 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001013static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001015 /*
1016 * This callback is supposed to deal with mc filter in
1017 * _rx_ path and has nothing to do with the _tx_ path.
1018 * In rx path we always accept everything userspace gives us.
1019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001022static netdev_features_t tun_net_fix_features(struct net_device *dev,
1023 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +00001024{
1025 struct tun_struct *tun = netdev_priv(dev);
1026
1027 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1028}
Neil Hormanbebd0972011-06-15 05:25:01 +00001029#ifdef CONFIG_NET_POLL_CONTROLLER
1030static void tun_poll_controller(struct net_device *dev)
1031{
1032 /*
1033 * Tun only receives frames when:
1034 * 1) the char device endpoint gets data from user space
1035 * 2) the tun socket gets a sendmsg call from user space
Petar Penkov94317092017-09-22 13:49:14 -07001036 * If NAPI is not enabled, since both of those are synchronous
1037 * operations, we are guaranteed never to have pending data when we poll
1038 * for it so there is nothing to do here but return.
Neil Hormanbebd0972011-06-15 05:25:01 +00001039 * We need this though so netpoll recognizes us as an interface that
1040 * supports polling, which enables bridge devices in virt setups to
1041 * still use netconsole
Petar Penkov94317092017-09-22 13:49:14 -07001042 * If NAPI is enabled, however, we need to schedule polling for all
Petar Penkov90e33d42017-09-22 13:49:15 -07001043 * queues unless we are using napi_gro_frags(), which we call in
1044 * process context and not in NAPI context.
Neil Hormanbebd0972011-06-15 05:25:01 +00001045 */
Petar Penkov94317092017-09-22 13:49:14 -07001046 struct tun_struct *tun = netdev_priv(dev);
1047
1048 if (tun->flags & IFF_NAPI) {
1049 struct tun_file *tfile;
1050 int i;
1051
Petar Penkov90e33d42017-09-22 13:49:15 -07001052 if (tun_napi_frags_enabled(tun))
1053 return;
1054
Petar Penkov94317092017-09-22 13:49:14 -07001055 rcu_read_lock();
1056 for (i = 0; i < tun->numqueues; i++) {
1057 tfile = rcu_dereference(tun->tfiles[i]);
1058 napi_schedule(&tfile->napi);
1059 }
1060 rcu_read_unlock();
1061 }
Neil Hormanbebd0972011-06-15 05:25:01 +00001062 return;
1063}
1064#endif
Paolo Abenieaea34b2016-02-26 10:45:40 +01001065
1066static void tun_set_headroom(struct net_device *dev, int new_hr)
1067{
1068 struct tun_struct *tun = netdev_priv(dev);
1069
1070 if (new_hr < NET_SKB_PAD)
1071 new_hr = NET_SKB_PAD;
1072
1073 tun->align = new_hr;
1074}
1075
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001076static void
Paolo Abeni608b9972016-04-13 10:52:20 +02001077tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1078{
1079 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
1080 struct tun_struct *tun = netdev_priv(dev);
1081 struct tun_pcpu_stats *p;
1082 int i;
1083
1084 for_each_possible_cpu(i) {
1085 u64 rxpackets, rxbytes, txpackets, txbytes;
1086 unsigned int start;
1087
1088 p = per_cpu_ptr(tun->pcpu_stats, i);
1089 do {
1090 start = u64_stats_fetch_begin(&p->syncp);
1091 rxpackets = p->rx_packets;
1092 rxbytes = p->rx_bytes;
1093 txpackets = p->tx_packets;
1094 txbytes = p->tx_bytes;
1095 } while (u64_stats_fetch_retry(&p->syncp, start));
1096
1097 stats->rx_packets += rxpackets;
1098 stats->rx_bytes += rxbytes;
1099 stats->tx_packets += txpackets;
1100 stats->tx_bytes += txbytes;
1101
1102 /* u32 counters */
1103 rx_dropped += p->rx_dropped;
1104 rx_frame_errors += p->rx_frame_errors;
1105 tx_dropped += p->tx_dropped;
1106 }
1107 stats->rx_dropped = rx_dropped;
1108 stats->rx_frame_errors = rx_frame_errors;
1109 stats->tx_dropped = tx_dropped;
Paolo Abeni608b9972016-04-13 10:52:20 +02001110}
1111
Jason Wang761876c2017-08-11 19:41:18 +08001112static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1113 struct netlink_ext_ack *extack)
1114{
1115 struct tun_struct *tun = netdev_priv(dev);
1116 struct bpf_prog *old_prog;
1117
1118 old_prog = rtnl_dereference(tun->xdp_prog);
1119 rcu_assign_pointer(tun->xdp_prog, prog);
1120 if (old_prog)
1121 bpf_prog_put(old_prog);
1122
1123 return 0;
1124}
1125
1126static u32 tun_xdp_query(struct net_device *dev)
1127{
1128 struct tun_struct *tun = netdev_priv(dev);
1129 const struct bpf_prog *xdp_prog;
1130
1131 xdp_prog = rtnl_dereference(tun->xdp_prog);
1132 if (xdp_prog)
1133 return xdp_prog->aux->id;
1134
1135 return 0;
1136}
1137
1138static int tun_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1139{
1140 switch (xdp->command) {
1141 case XDP_SETUP_PROG:
1142 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1143 case XDP_QUERY_PROG:
1144 xdp->prog_id = tun_xdp_query(dev);
1145 xdp->prog_attached = !!xdp->prog_id;
1146 return 0;
1147 default:
1148 return -EINVAL;
1149 }
1150}
1151
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001152static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001153 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001154 .ndo_open = tun_net_open,
1155 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001156 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001157 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +00001158 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +00001159#ifdef CONFIG_NET_POLL_CONTROLLER
1160 .ndo_poll_controller = tun_poll_controller,
1161#endif
Paolo Abenieaea34b2016-02-26 10:45:40 +01001162 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001163 .ndo_get_stats64 = tun_net_get_stats64,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001164};
1165
1166static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001167 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001168 .ndo_open = tun_net_open,
1169 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001170 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001171 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001172 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001173 .ndo_set_mac_address = eth_mac_addr,
1174 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +00001175 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +00001176#ifdef CONFIG_NET_POLL_CONTROLLER
1177 .ndo_poll_controller = tun_poll_controller,
1178#endif
Toshiaki Makita5e527962015-07-31 15:03:27 +09001179 .ndo_features_check = passthru_features_check,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001180 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001181 .ndo_get_stats64 = tun_net_get_stats64,
Jason Wang761876c2017-08-11 19:41:18 +08001182 .ndo_xdp = tun_xdp,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001183};
1184
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001185static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +00001186{
1187 int i;
1188
Jason Wang96442e422012-10-31 19:46:02 +00001189 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1190 INIT_HLIST_HEAD(&tun->flows[i]);
1191
1192 tun->ageing_time = TUN_FLOW_EXPIRE;
1193 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
1194 mod_timer(&tun->flow_gc_timer,
1195 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +00001196}
1197
1198static void tun_flow_uninit(struct tun_struct *tun)
1199{
1200 del_timer_sync(&tun->flow_gc_timer);
1201 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001202}
1203
Jarod Wilson91572082016-10-20 13:55:20 -04001204#define MIN_MTU 68
1205#define MAX_MTU 65535
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/* Initialize net device. */
1208static void tun_net_init(struct net_device *dev)
1209{
1210 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001213 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001214 dev->netdev_ops = &tun_netdev_ops;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /* Point-to-Point TUN Device */
1217 dev->hard_header_len = 0;
1218 dev->addr_len = 0;
1219 dev->mtu = 1500;
1220
1221 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001222 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 break;
1225
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001226 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -08001227 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001229 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +00001230 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +00001231 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001233 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236 }
Jarod Wilson91572082016-10-20 13:55:20 -04001237
1238 dev->min_mtu = MIN_MTU;
1239 dev->max_mtu = MAX_MTU - dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/* Character device part */
1243
1244/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +00001245static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001246{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +00001247 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001248 struct tun_struct *tun = tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001249 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001250 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +00001253 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jason Wang54f968d2012-10-31 19:45:57 +00001255 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001256
Joe Perches6b8a66e2011-03-02 07:18:10 +00001257 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Xi Wang9e641bd2014-05-16 15:11:48 -07001259 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001260
Jason Wang1576d982016-06-30 14:45:36 +08001261 if (!skb_array_empty(&tfile->tx_array))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 mask |= POLLIN | POLLRDNORM;
1263
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001264 if (tun->dev->flags & IFF_UP &&
1265 (sock_writeable(sk) ||
1266 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1267 sock_writeable(sk))))
Herbert Xu33dccbb2009-02-05 21:25:32 -08001268 mask |= POLLOUT | POLLWRNORM;
1269
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001270 if (tun->dev->reg_state != NETREG_REGISTERED)
1271 mask = POLLERR;
1272
Eric W. Biederman631ab462009-01-20 11:00:40 +00001273 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return mask;
1275}
1276
Petar Penkov90e33d42017-09-22 13:49:15 -07001277static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
1278 size_t len,
1279 const struct iov_iter *it)
1280{
1281 struct sk_buff *skb;
1282 size_t linear;
1283 int err;
1284 int i;
1285
1286 if (it->nr_segs > MAX_SKB_FRAGS + 1)
1287 return ERR_PTR(-ENOMEM);
1288
1289 local_bh_disable();
1290 skb = napi_get_frags(&tfile->napi);
1291 local_bh_enable();
1292 if (!skb)
1293 return ERR_PTR(-ENOMEM);
1294
1295 linear = iov_iter_single_seg_count(it);
1296 err = __skb_grow(skb, linear);
1297 if (err)
1298 goto free;
1299
1300 skb->len = len;
1301 skb->data_len = len - linear;
1302 skb->truesize += skb->data_len;
1303
1304 for (i = 1; i < it->nr_segs; i++) {
1305 size_t fragsz = it->iov[i].iov_len;
1306 unsigned long offset;
1307 struct page *page;
1308 void *data;
1309
1310 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1311 err = -EINVAL;
1312 goto free;
1313 }
1314
1315 local_bh_disable();
1316 data = napi_alloc_frag(fragsz);
1317 local_bh_enable();
1318 if (!data) {
1319 err = -ENOMEM;
1320 goto free;
1321 }
1322
1323 page = virt_to_head_page(data);
1324 offset = data - page_address(page);
1325 skb_fill_page_desc(skb, i - 1, page, offset, fragsz);
1326 }
1327
1328 return skb;
1329free:
1330 /* frees skb and all frags allocated with napi_alloc_frag() */
1331 napi_free_frags(&tfile->napi);
1332 return ERR_PTR(err);
1333}
1334
Rusty Russellf42157c2008-08-15 15:15:10 -07001335/* prepad is the amount to reserve at front. len is length after that.
1336 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001337static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001338 size_t prepad, size_t len,
1339 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001340{
Jason Wang54f968d2012-10-31 19:45:57 +00001341 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001342 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001343 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001344
1345 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001346 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001347 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001348
Herbert Xu33dccbb2009-02-05 21:25:32 -08001349 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001350 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001351 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001352 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001353
1354 skb_reserve(skb, prepad);
1355 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001356 skb->data_len = len - linear;
1357 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001358
1359 return skb;
1360}
1361
Jason Wang5503fce2017-01-18 15:02:03 +08001362static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1363 struct sk_buff *skb, int more)
1364{
1365 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1366 struct sk_buff_head process_queue;
1367 u32 rx_batched = tun->rx_batched;
1368 bool rcv = false;
1369
1370 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1371 local_bh_disable();
1372 netif_receive_skb(skb);
1373 local_bh_enable();
1374 return;
1375 }
1376
1377 spin_lock(&queue->lock);
1378 if (!more || skb_queue_len(queue) == rx_batched) {
1379 __skb_queue_head_init(&process_queue);
1380 skb_queue_splice_tail_init(queue, &process_queue);
1381 rcv = true;
1382 } else {
1383 __skb_queue_tail(queue, skb);
1384 }
1385 spin_unlock(&queue->lock);
1386
1387 if (rcv) {
1388 struct sk_buff *nskb;
1389
1390 local_bh_disable();
1391 while ((nskb = __skb_dequeue(&process_queue)))
1392 netif_receive_skb(nskb);
1393 netif_receive_skb(skb);
1394 local_bh_enable();
1395 }
1396}
1397
Jason Wang66ccbc92017-08-11 19:41:16 +08001398static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1399 int len, int noblock, bool zerocopy)
1400{
1401 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1402 return false;
1403
1404 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1405 return false;
1406
1407 if (!noblock)
1408 return false;
1409
1410 if (zerocopy)
1411 return false;
1412
1413 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1414 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1415 return false;
1416
1417 return true;
1418}
1419
Jason Wang761876c2017-08-11 19:41:18 +08001420static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1421 struct tun_file *tfile,
Jason Wang66ccbc92017-08-11 19:41:16 +08001422 struct iov_iter *from,
Jason Wang761876c2017-08-11 19:41:18 +08001423 struct virtio_net_hdr *hdr,
Jason Wang1cfe6e92017-09-04 11:36:09 +08001424 int len, int *skb_xdp)
Jason Wang66ccbc92017-08-11 19:41:16 +08001425{
Eric Dumazet0bbd7da2017-08-16 22:14:33 +08001426 struct page_frag *alloc_frag = &current->task_frag;
Jason Wang66ccbc92017-08-11 19:41:16 +08001427 struct sk_buff *skb;
Jason Wang761876c2017-08-11 19:41:18 +08001428 struct bpf_prog *xdp_prog;
Jason Wang7df13212017-09-04 11:36:08 +08001429 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jason Wang761876c2017-08-11 19:41:18 +08001430 unsigned int delta = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001431 char *buf;
1432 size_t copied;
Jason Wang761876c2017-08-11 19:41:18 +08001433 bool xdp_xmit = false;
Jason Wang7df13212017-09-04 11:36:08 +08001434 int err, pad = TUN_RX_PAD;
1435
1436 rcu_read_lock();
1437 xdp_prog = rcu_dereference(tun->xdp_prog);
1438 if (xdp_prog)
1439 pad += TUN_HEADROOM;
1440 buflen += SKB_DATA_ALIGN(len + pad);
1441 rcu_read_unlock();
Jason Wang66ccbc92017-08-11 19:41:16 +08001442
1443 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1444 return ERR_PTR(-ENOMEM);
1445
1446 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1447 copied = copy_page_from_iter(alloc_frag->page,
Jason Wang7df13212017-09-04 11:36:08 +08001448 alloc_frag->offset + pad,
Jason Wang66ccbc92017-08-11 19:41:16 +08001449 len, from);
1450 if (copied != len)
1451 return ERR_PTR(-EFAULT);
1452
Jason Wang7df13212017-09-04 11:36:08 +08001453 /* There's a small window that XDP may be set after the check
1454 * of xdp_prog above, this should be rare and for simplicity
1455 * we do XDP on skb in case the headroom is not enough.
1456 */
1457 if (hdr->gso_type || !xdp_prog)
Jason Wang1cfe6e92017-09-04 11:36:09 +08001458 *skb_xdp = 1;
Jason Wang761876c2017-08-11 19:41:18 +08001459 else
Jason Wang1cfe6e92017-09-04 11:36:09 +08001460 *skb_xdp = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001461
Jason Wang761876c2017-08-11 19:41:18 +08001462 rcu_read_lock();
1463 xdp_prog = rcu_dereference(tun->xdp_prog);
Jason Wang1cfe6e92017-09-04 11:36:09 +08001464 if (xdp_prog && !*skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001465 struct xdp_buff xdp;
1466 void *orig_data;
1467 u32 act;
1468
1469 xdp.data_hard_start = buf;
Jason Wang7df13212017-09-04 11:36:08 +08001470 xdp.data = buf + pad;
Jason Wang761876c2017-08-11 19:41:18 +08001471 xdp.data_end = xdp.data + len;
1472 orig_data = xdp.data;
1473 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1474
1475 switch (act) {
1476 case XDP_REDIRECT:
1477 get_page(alloc_frag->page);
1478 alloc_frag->offset += buflen;
1479 err = xdp_do_redirect(tun->dev, &xdp, xdp_prog);
1480 if (err)
1481 goto err_redirect;
1482 return NULL;
1483 case XDP_TX:
1484 xdp_xmit = true;
1485 /* fall through */
1486 case XDP_PASS:
1487 delta = orig_data - xdp.data;
1488 break;
1489 default:
1490 bpf_warn_invalid_xdp_action(act);
1491 /* fall through */
1492 case XDP_ABORTED:
1493 trace_xdp_exception(tun->dev, xdp_prog, act);
1494 /* fall through */
1495 case XDP_DROP:
1496 goto err_xdp;
1497 }
1498 }
1499
1500 skb = build_skb(buf, buflen);
1501 if (!skb) {
1502 rcu_read_unlock();
1503 return ERR_PTR(-ENOMEM);
1504 }
1505
Jason Wang7df13212017-09-04 11:36:08 +08001506 skb_reserve(skb, pad - delta);
Jason Wang761876c2017-08-11 19:41:18 +08001507 skb_put(skb, len + delta);
Jason Wang66ccbc92017-08-11 19:41:16 +08001508 get_page(alloc_frag->page);
1509 alloc_frag->offset += buflen;
1510
Jason Wang761876c2017-08-11 19:41:18 +08001511 if (xdp_xmit) {
1512 skb->dev = tun->dev;
1513 generic_xdp_tx(skb, xdp_prog);
1514 rcu_read_lock();
1515 return NULL;
1516 }
1517
1518 rcu_read_unlock();
1519
Jason Wang66ccbc92017-08-11 19:41:16 +08001520 return skb;
Jason Wang761876c2017-08-11 19:41:18 +08001521
1522err_redirect:
1523 put_page(alloc_frag->page);
1524err_xdp:
1525 rcu_read_unlock();
1526 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1527 return NULL;
Jason Wang66ccbc92017-08-11 19:41:16 +08001528}
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001531static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001532 void *msg_control, struct iov_iter *from,
Jason Wang5503fce2017-01-18 15:02:03 +08001533 int noblock, bool more)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Harvey Harrison09640e62009-02-01 00:45:17 -08001535 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001537 size_t total_len = iov_iter_count(from);
Paolo Abenieaea34b2016-02-26 10:45:40 +01001538 size_t len = total_len, align = tun->align, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001539 struct virtio_net_hdr gso = { 0 };
Paolo Abeni608b9972016-04-13 10:52:20 +02001540 struct tun_pcpu_stats *stats;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001541 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001542 int copylen;
1543 bool zerocopy = false;
1544 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001545 u32 rxhash;
Jason Wang1cfe6e92017-09-04 11:36:09 +08001546 int skb_xdp = 1;
Petar Penkov90e33d42017-09-22 13:49:15 -07001547 bool frags = tun_napi_frags_enabled(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Eric Dumazet1bd4978a2015-12-16 08:57:37 -08001549 if (!(tun->dev->flags & IFF_UP))
1550 return -EIO;
1551
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001552 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001553 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001555 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Al Virocbbd26b2016-11-01 22:09:04 -04001557 if (!copy_from_iter_full(&pi, sizeof(pi), from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return -EFAULT;
1559 }
1560
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001561 if (tun->flags & IFF_VNET_HDR) {
Willem de Bruijne1edab82017-02-03 18:20:48 -05001562 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1563
1564 if (len < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001565 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001566 len -= vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001567
Al Virocbbd26b2016-11-01 22:09:04 -04001568 if (!copy_from_iter_full(&gso, sizeof(gso), from))
Rusty Russellf43798c2008-07-03 03:48:02 -07001569 return -EFAULT;
1570
Herbert Xu49091222009-06-08 00:20:01 -07001571 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001572 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1573 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 -07001574
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001575 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001576 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001577 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001578 }
1579
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001580 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001581 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001582 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001583 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001584 return -EINVAL;
1585 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001586
Jason Wang96f8d9e2013-11-13 14:00:39 +08001587 good_linear = SKB_MAX_HEAD(align);
1588
Jason Wang88529172013-07-18 10:55:15 +08001589 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001590 struct iov_iter i = *from;
1591
Jason Wang88529172013-07-18 10:55:15 +08001592 /* There are 256 bytes to be copied in skb, so there is
1593 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001594 * The rest of the buffer is mapped from userspace.
1595 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001596 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001597 if (copylen > good_linear)
1598 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001599 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001600 iov_iter_advance(&i, copylen);
1601 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001602 zerocopy = true;
1603 }
1604
Petar Penkov90e33d42017-09-22 13:49:15 -07001605 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001606 /* For the packet that is not easy to be processed
1607 * (e.g gso or jumbo packet), we will do it at after
1608 * skb was created with generic XDP routine.
1609 */
1610 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
Jason Wang66ccbc92017-08-11 19:41:16 +08001611 if (IS_ERR(skb)) {
Paolo Abeni608b9972016-04-13 10:52:20 +02001612 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001613 return PTR_ERR(skb);
1614 }
Jason Wang761876c2017-08-11 19:41:18 +08001615 if (!skb)
1616 return total_len;
Jason Wang66ccbc92017-08-11 19:41:16 +08001617 } else {
1618 if (!zerocopy) {
1619 copylen = len;
1620 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1621 linear = good_linear;
1622 else
1623 linear = tun16_to_cpu(tun, gso.hdr_len);
1624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Petar Penkov90e33d42017-09-22 13:49:15 -07001626 if (frags) {
1627 mutex_lock(&tfile->napi_mutex);
1628 skb = tun_napi_alloc_frags(tfile, copylen, from);
1629 /* tun_napi_alloc_frags() enforces a layout for the skb.
1630 * If zerocopy is enabled, then this layout will be
1631 * overwritten by zerocopy_sg_from_iter().
1632 */
1633 zerocopy = false;
1634 } else {
1635 skb = tun_alloc_skb(tfile, align, copylen, linear,
1636 noblock);
1637 }
1638
Jason Wang66ccbc92017-08-11 19:41:16 +08001639 if (IS_ERR(skb)) {
1640 if (PTR_ERR(skb) != -EAGAIN)
1641 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Petar Penkov90e33d42017-09-22 13:49:15 -07001642 if (frags)
1643 mutex_unlock(&tfile->napi_mutex);
Jason Wang66ccbc92017-08-11 19:41:16 +08001644 return PTR_ERR(skb);
1645 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001646
Jason Wang66ccbc92017-08-11 19:41:16 +08001647 if (zerocopy)
1648 err = zerocopy_sg_from_iter(skb, from);
1649 else
1650 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1651
1652 if (err) {
1653 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1654 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001655 if (frags) {
1656 tfile->napi.skb = NULL;
1657 mutex_unlock(&tfile->napi_mutex);
1658 }
1659
Jason Wang66ccbc92017-08-11 19:41:16 +08001660 return -EFAULT;
1661 }
Dave Jones8f227572006-03-11 18:49:13 -08001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001664 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
Paolo Abenidf10db92016-06-14 00:00:04 +02001665 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1666 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001667 if (frags) {
1668 tfile->napi.skb = NULL;
1669 mutex_unlock(&tfile->napi_mutex);
1670 }
1671
Paolo Abenidf10db92016-06-14 00:00:04 +02001672 return -EINVAL;
1673 }
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001676 case IFF_TUN:
1677 if (tun->flags & IFF_NO_PI) {
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001678 switch (skb->data[0] & 0xf0) {
1679 case 0x40:
1680 pi.proto = htons(ETH_P_IP);
1681 break;
1682 case 0x60:
1683 pi.proto = htons(ETH_P_IPV6);
1684 break;
1685 default:
Paolo Abeni608b9972016-04-13 10:52:20 +02001686 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001687 kfree_skb(skb);
1688 return -EINVAL;
1689 }
1690 }
1691
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001692 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001694 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001696 case IFF_TAP:
Petar Penkov90e33d42017-09-22 13:49:15 -07001697 if (!frags)
1698 skb->protocol = eth_type_trans(skb, tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001702 /* copy skb_ubuf_info for callback when skb has no error */
1703 if (zerocopy) {
1704 skb_shinfo(skb)->destructor_arg = msg_control;
1705 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001706 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangaf1cc7a2016-11-30 13:17:51 +08001707 } else if (msg_control) {
1708 struct ubuf_info *uarg = msg_control;
1709 uarg->callback(uarg, false);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001710 }
1711
Vlad Yasevich72f65102015-02-03 16:36:16 -05001712 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001713 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001714
Jason Wang1cfe6e92017-09-04 11:36:09 +08001715 if (skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001716 struct bpf_prog *xdp_prog;
1717 int ret;
1718
1719 rcu_read_lock();
1720 xdp_prog = rcu_dereference(tun->xdp_prog);
1721 if (xdp_prog) {
1722 ret = do_xdp_generic(xdp_prog, skb);
1723 if (ret != XDP_PASS) {
1724 rcu_read_unlock();
1725 return total_len;
1726 }
1727 }
1728 rcu_read_unlock();
1729 }
1730
Jason Wangfeec0842017-06-06 14:09:49 +08001731 rxhash = __skb_get_hash_symmetric(skb);
Petar Penkov94317092017-09-22 13:49:14 -07001732
Petar Penkov90e33d42017-09-22 13:49:15 -07001733 if (frags) {
1734 /* Exercise flow dissector code path. */
1735 u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb));
1736
1737 if (headlen > skb_headlen(skb) || headlen < ETH_HLEN) {
1738 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1739 napi_free_frags(&tfile->napi);
1740 mutex_unlock(&tfile->napi_mutex);
1741 WARN_ON(1);
1742 return -ENOMEM;
1743 }
1744
1745 local_bh_disable();
1746 napi_gro_frags(&tfile->napi);
1747 local_bh_enable();
1748 mutex_unlock(&tfile->napi_mutex);
1749 } else if (tun->flags & IFF_NAPI) {
Petar Penkov94317092017-09-22 13:49:14 -07001750 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1751 int queue_len;
1752
1753 spin_lock_bh(&queue->lock);
1754 __skb_queue_tail(queue, skb);
1755 queue_len = skb_queue_len(queue);
1756 spin_unlock(&queue->lock);
1757
1758 if (!more || queue_len > NAPI_POLL_WEIGHT)
1759 napi_schedule(&tfile->napi);
1760
1761 local_bh_enable();
1762 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
1763 tun_rx_batched(tun, tfile, skb, more);
1764 } else {
1765 netif_rx_ni(skb);
1766 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001767
Paolo Abeni608b9972016-04-13 10:52:20 +02001768 stats = get_cpu_ptr(tun->pcpu_stats);
1769 u64_stats_update_begin(&stats->syncp);
1770 stats->rx_packets++;
1771 stats->rx_bytes += len;
1772 u64_stats_update_end(&stats->syncp);
1773 put_cpu_ptr(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Jason Wang9e857222013-01-28 01:05:19 +00001775 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001776 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001777}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Al Virof5ff53b2014-06-19 15:36:49 -04001779static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001781 struct file *file = iocb->ki_filp;
Jason Wang54f968d2012-10-31 19:45:57 +00001782 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001783 struct tun_struct *tun = tun_get(tfile);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001784 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if (!tun)
1787 return -EBADFD;
1788
Jason Wang5503fce2017-01-18 15:02:03 +08001789 result = tun_get_user(tun, tfile, NULL, from,
1790 file->f_flags & O_NONBLOCK, false);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001791
1792 tun_put(tun);
1793 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001797static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001798 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001799 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08001800 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 struct tun_pi pi = { 0, skb->protocol };
Paolo Abeni608b9972016-04-13 10:52:20 +02001803 struct tun_pcpu_stats *stats;
Herbert Xue0b46d02014-11-07 21:22:23 +08001804 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08001805 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001806 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001807 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001808
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001809 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001810 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001812 if (tun->flags & IFF_VNET_HDR)
Willem de Bruijne1edab82017-02-03 18:20:48 -05001813 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Herbert Xue0b46d02014-11-07 21:22:23 +08001815 total = skb->len + vlan_hlen + vnet_hdr_sz;
1816
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001817 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001818 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return -EINVAL;
1820
Herbert Xue0b46d02014-11-07 21:22:23 +08001821 total += sizeof(pi);
1822 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /* Packet will be striped */
1824 pi.flags |= TUN_PKT_STRIP;
1825 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001826
Herbert Xue0b46d02014-11-07 21:22:23 +08001827 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Herbert Xu2eb783c2014-11-03 04:30:14 +08001831 if (vnet_hdr_sz) {
Jarno Rajahalme9403cd72016-11-18 15:40:40 -08001832 struct virtio_net_hdr gso;
Mike Rapoport34166092016-06-08 16:09:20 +03001833
Herbert Xue0b46d02014-11-07 21:22:23 +08001834 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001835 return -EINVAL;
1836
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001837 if (virtio_net_hdr_from_skb(skb, &gso,
Jason Wang6391a442017-01-20 14:32:42 +08001838 tun_is_little_endian(tun), true)) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001839 struct skb_shared_info *sinfo = skb_shinfo(skb);
Mike Rapoport34166092016-06-08 16:09:20 +03001840 pr_err("unexpected GSO type: "
1841 "0x%x, gso_size %d, hdr_len %d\n",
1842 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1843 tun16_to_cpu(tun, gso.hdr_len));
1844 print_hex_dump(KERN_ERR, "tun: ",
1845 DUMP_PREFIX_NONE,
1846 16, 1, skb->head,
1847 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1848 WARN_ON_ONCE(1);
1849 return -EINVAL;
1850 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001851
Herbert Xue0b46d02014-11-07 21:22:23 +08001852 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07001853 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08001854
1855 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001856 }
1857
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001858 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001859 int ret;
Jason Wang6680ec62013-07-25 13:00:33 +08001860 struct {
1861 __be16 h_vlan_proto;
1862 __be16 h_vlan_TCI;
1863 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Jason Wang6680ec62013-07-25 13:00:33 +08001865 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001866 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Jason Wang6680ec62013-07-25 13:00:33 +08001868 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08001869
Herbert Xue0b46d02014-11-07 21:22:23 +08001870 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1871 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001872 goto done;
1873
Herbert Xue0b46d02014-11-07 21:22:23 +08001874 ret = copy_to_iter(&veth, sizeof(veth), iter);
1875 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001876 goto done;
1877 }
1878
Herbert Xue0b46d02014-11-07 21:22:23 +08001879 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08001880
1881done:
Paolo Abeni608b9972016-04-13 10:52:20 +02001882 /* caller is in process context, */
1883 stats = get_cpu_ptr(tun->pcpu_stats);
1884 u64_stats_update_begin(&stats->syncp);
1885 stats->tx_packets++;
1886 stats->tx_bytes += skb->len + vlan_hlen;
1887 u64_stats_update_end(&stats->syncp);
1888 put_cpu_ptr(tun->pcpu_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 return total;
1891}
1892
Jason Wang1576d982016-06-30 14:45:36 +08001893static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock,
1894 int *err)
1895{
1896 DECLARE_WAITQUEUE(wait, current);
1897 struct sk_buff *skb = NULL;
Jason Wangf48cc6b2016-07-04 13:53:38 +08001898 int error = 0;
Jason Wang1576d982016-06-30 14:45:36 +08001899
1900 skb = skb_array_consume(&tfile->tx_array);
1901 if (skb)
1902 goto out;
1903 if (noblock) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001904 error = -EAGAIN;
Jason Wang1576d982016-06-30 14:45:36 +08001905 goto out;
1906 }
1907
1908 add_wait_queue(&tfile->wq.wait, &wait);
1909 current->state = TASK_INTERRUPTIBLE;
1910
1911 while (1) {
1912 skb = skb_array_consume(&tfile->tx_array);
1913 if (skb)
1914 break;
1915 if (signal_pending(current)) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001916 error = -ERESTARTSYS;
Jason Wang1576d982016-06-30 14:45:36 +08001917 break;
1918 }
1919 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08001920 error = -EFAULT;
Jason Wang1576d982016-06-30 14:45:36 +08001921 break;
1922 }
1923
1924 schedule();
1925 }
1926
1927 current->state = TASK_RUNNING;
1928 remove_wait_queue(&tfile->wq.wait, &wait);
1929
1930out:
Jason Wangf48cc6b2016-07-04 13:53:38 +08001931 *err = error;
Jason Wang1576d982016-06-30 14:45:36 +08001932 return skb;
1933}
1934
Jason Wang54f968d2012-10-31 19:45:57 +00001935static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05001936 struct iov_iter *to,
Jason Wangac77cfd2017-05-17 12:14:43 +08001937 int noblock, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Al Viro9b067032014-11-07 13:52:07 -05001939 ssize_t ret;
Jason Wang1576d982016-06-30 14:45:36 +08001940 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Rami Rosen3872baf2012-11-25 22:07:41 +00001942 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Al Viro9b067032014-11-07 13:52:07 -05001944 if (!iov_iter_count(to))
1945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Jason Wangac77cfd2017-05-17 12:14:43 +08001947 if (!skb) {
1948 /* Read frames from ring */
1949 skb = tun_ring_recv(tfile, noblock, &err);
1950 if (!skb)
1951 return err;
1952 }
Herbert Xue0b46d02014-11-07 21:22:23 +08001953
Al Viro9b067032014-11-07 13:52:07 -05001954 ret = tun_put_user(tun, tfile, skb, to);
Jason Wangf51a5e82014-12-01 16:53:15 +08001955 if (unlikely(ret < 0))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001956 kfree_skb(skb);
Jason Wangf51a5e82014-12-01 16:53:15 +08001957 else
1958 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001960 return ret;
1961}
1962
Al Viro9b067032014-11-07 13:52:07 -05001963static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001964{
1965 struct file *file = iocb->ki_filp;
1966 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001967 struct tun_struct *tun = tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05001968 ssize_t len = iov_iter_count(to), ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001969
1970 if (!tun)
1971 return -EBADFD;
Jason Wangac77cfd2017-05-17 12:14:43 +08001972 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL);
David S. Miller42404c02013-12-10 22:05:45 -05001973 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08001974 if (ret > 0)
1975 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001976 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return ret;
1978}
1979
Jason Wang96442e422012-10-31 19:46:02 +00001980static void tun_free_netdev(struct net_device *dev)
1981{
1982 struct tun_struct *tun = netdev_priv(dev);
1983
Jason Wang4008e972012-12-13 23:53:30 +00001984 BUG_ON(!(list_empty(&tun->disabled)));
Paolo Abeni608b9972016-04-13 10:52:20 +02001985 free_percpu(tun->pcpu_stats);
Jason Wang96442e422012-10-31 19:46:02 +00001986 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001987 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001988}
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990static void tun_setup(struct net_device *dev)
1991{
1992 struct tun_struct *tun = netdev_priv(dev);
1993
Eric W. Biederman0625c882012-02-07 16:48:55 -08001994 tun->owner = INVALID_UID;
1995 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 dev->ethtool_ops = &tun_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001998 dev->needs_free_netdev = true;
1999 dev->priv_destructor = tun_free_netdev;
Jason Wang016adb72016-04-08 13:26:48 +08002000 /* We prefer our own queue length */
2001 dev->tx_queue_len = TUN_READQ_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002004/* Trivial set of netlink ops to allow deleting tun or tap
2005 * device with netlink.
2006 */
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002007static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2008 struct netlink_ext_ack *extack)
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002009{
2010 return -EINVAL;
2011}
2012
2013static struct rtnl_link_ops tun_link_ops __read_mostly = {
2014 .kind = DRV_NAME,
2015 .priv_size = sizeof(struct tun_struct),
2016 .setup = tun_setup,
2017 .validate = tun_validate,
2018};
2019
Herbert Xu33dccbb2009-02-05 21:25:32 -08002020static void tun_sock_write_space(struct sock *sk)
2021{
Jason Wang54f968d2012-10-31 19:45:57 +00002022 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00002023 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002024
2025 if (!sock_writeable(sk))
2026 return;
2027
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002028 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Herbert Xu33dccbb2009-02-05 21:25:32 -08002029 return;
2030
Eric Dumazet43815482010-04-29 11:01:49 +00002031 wqueue = sk_sleep(sk);
2032 if (wqueue && waitqueue_active(wqueue))
2033 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002034 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07002035
Jason Wang54f968d2012-10-31 19:45:57 +00002036 tfile = container_of(sk, struct tun_file, sk);
2037 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002038}
2039
Ying Xue1b784142015-03-02 15:37:48 +08002040static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002041{
Jason Wang54f968d2012-10-31 19:45:57 +00002042 int ret;
2043 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002044 struct tun_struct *tun = tun_get(tfile);
Jason Wang54f968d2012-10-31 19:45:57 +00002045
2046 if (!tun)
2047 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04002048
Al Viroc0371da2014-11-24 10:42:55 -05002049 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
Jason Wang5503fce2017-01-18 15:02:03 +08002050 m->msg_flags & MSG_DONTWAIT,
2051 m->msg_flags & MSG_MORE);
Jason Wang54f968d2012-10-31 19:45:57 +00002052 tun_put(tun);
2053 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002054}
2055
Ying Xue1b784142015-03-02 15:37:48 +08002056static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002057 int flags)
2058{
Jason Wang54f968d2012-10-31 19:45:57 +00002059 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002060 struct tun_struct *tun = tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002061 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00002062
2063 if (!tun)
2064 return -EBADFD;
2065
Richard Cochraneda29772013-07-19 19:40:10 +02002066 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00002067 ret = -EINVAL;
2068 goto out;
2069 }
Richard Cochraneda29772013-07-19 19:40:10 +02002070 if (flags & MSG_ERRQUEUE) {
2071 ret = sock_recv_errqueue(sock->sk, m, total_len,
2072 SOL_PACKET, TUN_TX_TIMESTAMP);
2073 goto out;
2074 }
Jason Wangac77cfd2017-05-17 12:14:43 +08002075 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT,
2076 m->msg_control);
Alex Gartrell87897932014-12-25 23:05:03 -08002077 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05002078 m->msg_flags |= MSG_TRUNC;
2079 ret = flags & MSG_TRUNC ? ret : total_len;
2080 }
Gao feng3811ae72013-04-24 21:59:23 +00002081out:
Jason Wang54f968d2012-10-31 19:45:57 +00002082 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002083 return ret;
2084}
2085
Jason Wang1576d982016-06-30 14:45:36 +08002086static int tun_peek_len(struct socket *sock)
2087{
2088 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2089 struct tun_struct *tun;
2090 int ret = 0;
2091
yuan linyu9484dc72017-09-23 22:36:52 +08002092 tun = tun_get(tfile);
Jason Wang1576d982016-06-30 14:45:36 +08002093 if (!tun)
2094 return 0;
2095
2096 ret = skb_array_peek_len(&tfile->tx_array);
2097 tun_put(tun);
2098
2099 return ret;
2100}
2101
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002102/* Ops structure to mimic raw sockets with tun */
2103static const struct proto_ops tun_socket_ops = {
Jason Wang1576d982016-06-30 14:45:36 +08002104 .peek_len = tun_peek_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002105 .sendmsg = tun_sendmsg,
2106 .recvmsg = tun_recvmsg,
2107};
2108
Herbert Xu33dccbb2009-02-05 21:25:32 -08002109static struct proto tun_proto = {
2110 .name = "tun",
2111 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00002112 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08002113};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002114
David Woodhouse980c9e82009-05-09 22:54:21 -07002115static int tun_flags(struct tun_struct *tun)
2116{
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002117 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07002118}
2119
2120static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
2121 char *buf)
2122{
2123 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2124 return sprintf(buf, "0x%x\n", tun_flags(tun));
2125}
2126
2127static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
2128 char *buf)
2129{
2130 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002131 return uid_valid(tun->owner)?
2132 sprintf(buf, "%u\n",
2133 from_kuid_munged(current_user_ns(), tun->owner)):
2134 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002135}
2136
2137static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
2138 char *buf)
2139{
2140 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002141 return gid_valid(tun->group) ?
2142 sprintf(buf, "%u\n",
2143 from_kgid_munged(current_user_ns(), tun->group)):
2144 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002145}
2146
2147static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
2148static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
2149static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
2150
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002151static struct attribute *tun_dev_attrs[] = {
2152 &dev_attr_tun_flags.attr,
2153 &dev_attr_owner.attr,
2154 &dev_attr_group.attr,
2155 NULL
2156};
2157
2158static const struct attribute_group tun_attr_group = {
2159 .attrs = tun_dev_attrs
2160};
2161
Pavel Emelyanovd647a592008-04-16 00:41:16 -07002162static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00002165 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 struct net_device *dev;
2167 int err;
2168
Jason Wang7c0c3b12013-01-11 16:59:33 +00002169 if (tfile->detached)
2170 return -EINVAL;
2171
Petar Penkov90e33d42017-09-22 13:49:15 -07002172 if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
2173 if (!capable(CAP_NET_ADMIN))
2174 return -EPERM;
2175
2176 if (!(ifr->ifr_flags & IFF_NAPI) ||
2177 (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
2178 return -EINVAL;
2179 }
2180
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002181 dev = __dev_get_by_name(net, ifr->ifr_name);
2182 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07002183 if (ifr->ifr_flags & IFF_TUN_EXCL)
2184 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002185 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2186 tun = netdev_priv(dev);
2187 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2188 tun = netdev_priv(dev);
2189 else
2190 return -EINVAL;
2191
Jason Wang8e6d91a2013-05-28 18:32:11 +00002192 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002193 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00002194 return -EINVAL;
2195
Jason Wangcde8b152012-10-31 19:46:01 +00002196 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04002197 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002198 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04002199 if (err < 0)
2200 return err;
2201
Petar Penkov94317092017-09-22 13:49:14 -07002202 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
2203 ifr->ifr_flags & IFF_NAPI);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00002204 if (err < 0)
2205 return err;
Jason Wang4008e972012-12-13 23:53:30 +00002206
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002207 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00002208 (tun->numqueues + tun->numdisabled > 1)) {
2209 /* One or more queue has already been attached, no need
2210 * to initialize the device again.
2211 */
2212 return 0;
2213 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 else {
2216 char *name;
2217 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00002218 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2219 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Eric W. Biedermanc260b772012-11-18 21:34:11 +00002221 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002222 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04002223 err = security_tun_dev_create();
2224 if (err < 0)
2225 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* Set dev type */
2228 if (ifr->ifr_flags & IFF_TUN) {
2229 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002230 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 name = "tun%d";
2232 } else if (ifr->ifr_flags & IFF_TAP) {
2233 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002234 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002236 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00002237 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (*ifr->ifr_name)
2240 name = ifr->ifr_name;
2241
Jason Wangc8d68e62012-10-31 19:46:00 +00002242 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02002243 NET_NAME_UNKNOWN, tun_setup, queues,
2244 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (!dev)
2247 return -ENOMEM;
2248
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07002249 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002250 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002251 dev->ifindex = tfile->ifindex;
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002252 dev->sysfs_groups[0] = &tun_attr_group;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 tun = netdev_priv(dev);
2255 tun->dev = dev;
2256 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002257 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002258 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Paolo Abenieaea34b2016-02-26 10:45:40 +01002260 tun->align = NET_SKB_PAD;
Jason Wang54f968d2012-10-31 19:45:57 +00002261 tun->filter_attached = false;
2262 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Jason Wang5503fce2017-01-18 15:02:03 +08002263 tun->rx_batched = 0;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002264
Paolo Abeni608b9972016-04-13 10:52:20 +02002265 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2266 if (!tun->pcpu_stats) {
2267 err = -ENOMEM;
2268 goto err_free_dev;
2269 }
2270
Jason Wang96442e422012-10-31 19:46:02 +00002271 spin_lock_init(&tun->lock);
2272
Paul Moore5dbbaf22013-01-14 07:12:19 +00002273 err = security_tun_dev_alloc_security(&tun->security);
2274 if (err < 0)
Paolo Abeni608b9972016-04-13 10:52:20 +02002275 goto err_free_stat;
Paul Moore2b980db2009-08-28 18:12:43 -04002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04002278 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00002279
Michał Mirosław88255372011-04-19 06:13:10 +00002280 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08002281 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2282 NETIF_F_HW_VLAN_STAG_TX;
Paolo Abeni2a2bbf12016-04-14 18:39:39 +02002283 dev->features = dev->hw_features | NETIF_F_LLTX;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09002284 dev->vlan_features = dev->features &
2285 ~(NETIF_F_HW_VLAN_CTAG_TX |
2286 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00002287
Jason Wang4008e972012-12-13 23:53:30 +00002288 INIT_LIST_HEAD(&tun->disabled);
Petar Penkov94317092017-09-22 13:49:14 -07002289 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI);
Jason Wangeb0fb362012-12-02 17:19:45 +00002290 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002291 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00002292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 err = register_netdevice(tun->dev);
2294 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002295 goto err_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00002298 netif_carrier_on(tun->dev);
2299
Joe Perches6b8a66e2011-03-02 07:18:10 +00002300 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002302 tun->flags = (tun->flags & ~TUN_FEATURES) |
2303 (ifr->ifr_flags & TUN_FEATURES);
Jason Wangc8d68e62012-10-31 19:46:00 +00002304
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002305 /* Make sure persistent devices do not get stuck in
2306 * xoff state.
2307 */
2308 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00002309 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 strcpy(ifr->ifr_name, tun->dev->name);
2312 return 0;
2313
Jason Wang662ca432013-09-11 18:09:48 +08002314err_detach:
2315 tun_detach_all(dev);
Eric Dumazetff244c62017-08-18 13:39:56 -07002316 /* register_netdevice() already called tun_free_netdev() */
2317 goto err_free_dev;
2318
Jason Wang662ca432013-09-11 18:09:48 +08002319err_free_flow:
2320 tun_flow_uninit(tun);
2321 security_tun_dev_free_security(tun->security);
Paolo Abeni608b9972016-04-13 10:52:20 +02002322err_free_stat:
2323 free_percpu(tun->pcpu_stats);
Jason Wang662ca432013-09-11 18:09:48 +08002324err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return err;
2327}
2328
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002329static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00002330 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07002331{
Joe Perches6b8a66e2011-03-02 07:18:10 +00002332 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07002333
2334 strcpy(ifr->ifr_name, tun->dev->name);
2335
David Woodhouse980c9e82009-05-09 22:54:21 -07002336 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07002337
Mark McLoughline3b99552008-08-15 15:09:56 -07002338}
2339
Rusty Russell5228ddc2008-07-03 03:46:16 -07002340/* This is like a cut-down ethtool ops, except done via tun fd so no
2341 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00002342static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07002343{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002344 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002345
2346 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00002347 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002348 arg &= ~TUN_F_CSUM;
2349
2350 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2351 if (arg & TUN_F_TSO_ECN) {
2352 features |= NETIF_F_TSO_ECN;
2353 arg &= ~TUN_F_TSO_ECN;
2354 }
2355 if (arg & TUN_F_TSO4)
2356 features |= NETIF_F_TSO;
2357 if (arg & TUN_F_TSO6)
2358 features |= NETIF_F_TSO6;
2359 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2360 }
2361 }
2362
2363 /* This gives the user a way to test for new features in future by
2364 * trying to set them. */
2365 if (arg)
2366 return -EINVAL;
2367
Michał Mirosław88255372011-04-19 06:13:10 +00002368 tun->set_features = features;
Yaroslav Isakov09050952017-03-16 22:44:10 +03002369 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2370 tun->dev->wanted_features |= features;
Michał Mirosław88255372011-04-19 06:13:10 +00002371 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07002372
2373 return 0;
2374}
2375
Jason Wangc8d68e62012-10-31 19:46:00 +00002376static void tun_detach_filter(struct tun_struct *tun, int n)
2377{
2378 int i;
2379 struct tun_file *tfile;
2380
2381 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002382 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002383 lock_sock(tfile->socket.sk);
2384 sk_detach_filter(tfile->socket.sk);
2385 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002386 }
2387
2388 tun->filter_attached = false;
2389}
2390
2391static int tun_attach_filter(struct tun_struct *tun)
2392{
2393 int i, ret = 0;
2394 struct tun_file *tfile;
2395
2396 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002397 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002398 lock_sock(tfile->socket.sk);
2399 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2400 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002401 if (ret) {
2402 tun_detach_filter(tun, i);
2403 return ret;
2404 }
2405 }
2406
2407 tun->filter_attached = true;
2408 return ret;
2409}
2410
2411static void tun_set_sndbuf(struct tun_struct *tun)
2412{
2413 struct tun_file *tfile;
2414 int i;
2415
2416 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002417 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00002418 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2419 }
2420}
2421
Jason Wangcde8b152012-10-31 19:46:01 +00002422static int tun_set_queue(struct file *file, struct ifreq *ifr)
2423{
2424 struct tun_file *tfile = file->private_data;
2425 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00002426 int ret = 0;
2427
2428 rtnl_lock();
2429
2430 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00002431 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002432 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00002433 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002434 goto unlock;
2435 }
2436 ret = security_tun_dev_attach_queue(tun->security);
2437 if (ret < 0)
2438 goto unlock;
Petar Penkov94317092017-09-22 13:49:14 -07002439 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI);
Jason Wang4008e972012-12-13 23:53:30 +00002440 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002441 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002442 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00002443 ret = -EINVAL;
2444 else
2445 __tun_detach(tfile, false);
2446 } else
Jason Wangcde8b152012-10-31 19:46:01 +00002447 ret = -EINVAL;
2448
Paul Moore5dbbaf22013-01-14 07:12:19 +00002449unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00002450 rtnl_unlock();
2451 return ret;
2452}
2453
Arnd Bergmann50857e22009-11-06 22:52:32 -08002454static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2455 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002457 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002458 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 void __user* argp = (void __user*)arg;
2460 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08002461 kuid_t owner;
2462 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002463 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002464 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002465 unsigned int ifindex;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002466 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002467 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Gao Feng20861f22016-10-27 09:05:22 +08002469 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08002470 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07002472 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00002473 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07002474 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002475 if (cmd == TUNGETFEATURES) {
2476 /* Currently this just means: "what IFF flags are valid?".
2477 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002478 * TUNSETIFF.
2479 */
2480 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00002481 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00002482 } else if (cmd == TUNSETQUEUE)
2483 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002484
Jason Wangc8d68e62012-10-31 19:46:00 +00002485 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00002486 rtnl_lock();
2487
yuan linyu9484dc72017-09-23 22:36:52 +08002488 tun = tun_get(tfile);
Gao Feng0f16bc12016-10-25 22:26:09 +08002489 if (cmd == TUNSETIFF) {
2490 ret = -EEXIST;
2491 if (tun)
2492 goto unlock;
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2495
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002496 ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Herbert Xu876bfd42009-08-06 14:22:44 +00002498 if (ret)
2499 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Arnd Bergmann50857e22009-11-06 22:52:32 -08002501 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00002502 ret = -EFAULT;
2503 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002505 if (cmd == TUNSETIFINDEX) {
2506 ret = -EPERM;
2507 if (tun)
2508 goto unlock;
2509
2510 ret = -EFAULT;
2511 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2512 goto unlock;
2513
2514 ret = 0;
2515 tfile->ifindex = ifindex;
2516 goto unlock;
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Herbert Xu876bfd42009-08-06 14:22:44 +00002519 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00002521 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Jason Wang1e588332012-10-31 19:45:56 +00002523 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Eric W. Biederman631ab462009-01-20 11:00:40 +00002525 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07002527 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002528 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07002529
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002530 if (tfile->detached)
2531 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04002532 if (!tfile->socket.sk->sk_filter)
2533 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002534
Arnd Bergmann50857e22009-11-06 22:52:32 -08002535 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002536 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07002537 break;
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 case TUNSETNOCSUM:
2540 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Michał Mirosław88255372011-04-19 06:13:10 +00002542 /* [unimplemented] */
2543 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00002544 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 break;
2546
2547 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00002548 /* Disable/Enable persist mode. Keep an extra reference to the
2549 * module to prevent the module being unprobed.
2550 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002551 if (arg && !(tun->flags & IFF_PERSIST)) {
2552 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002553 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00002554 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002555 if (!arg && (tun->flags & IFF_PERSIST)) {
2556 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002557 module_put(THIS_MODULE);
2558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Joe Perches6b8a66e2011-03-02 07:18:10 +00002560 tun_debug(KERN_INFO, tun, "persist %s\n",
2561 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 break;
2563
2564 case TUNSETOWNER:
2565 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002566 owner = make_kuid(current_user_ns(), arg);
2567 if (!uid_valid(owner)) {
2568 ret = -EINVAL;
2569 break;
2570 }
2571 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00002572 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002573 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 break;
2575
Guido Guenther8c644622007-07-02 22:50:25 -07002576 case TUNSETGROUP:
2577 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002578 group = make_kgid(current_user_ns(), arg);
2579 if (!gid_valid(group)) {
2580 ret = -EINVAL;
2581 break;
2582 }
2583 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00002584 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002585 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07002586 break;
2587
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002588 case TUNSETLINK:
2589 /* Only allow setting the type when the interface is down */
2590 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002591 tun_debug(KERN_INFO, tun,
2592 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07002593 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002594 } else {
2595 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00002596 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
2597 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07002598 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002599 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002600 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602#ifdef TUN_DEBUG
2603 case TUNSETDEBUG:
2604 tun->debug = arg;
2605 break;
2606#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07002607 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00002608 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002609 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002610
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002611 case TUNSETTXFILTER:
2612 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00002613 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002614 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00002615 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07002616 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002617 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002620 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002621 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2622 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08002623 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002624 ret = -EFAULT;
2625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002628 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00002629 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2630 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08002631
Kim B. Heino40102372008-02-29 12:26:21 -08002632 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002633 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002634
2635 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002636 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002637 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2638 ret = -EFAULT;
2639 break;
2640
2641 case TUNSETSNDBUF:
2642 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2643 ret = -EFAULT;
2644 break;
2645 }
2646
Jason Wangc8d68e62012-10-31 19:46:00 +00002647 tun->sndbuf = sndbuf;
2648 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002649 break;
2650
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002651 case TUNGETVNETHDRSZ:
2652 vnet_hdr_sz = tun->vnet_hdr_sz;
2653 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2654 ret = -EFAULT;
2655 break;
2656
2657 case TUNSETVNETHDRSZ:
2658 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2659 ret = -EFAULT;
2660 break;
2661 }
2662 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2663 ret = -EINVAL;
2664 break;
2665 }
2666
2667 tun->vnet_hdr_sz = vnet_hdr_sz;
2668 break;
2669
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002670 case TUNGETVNETLE:
2671 le = !!(tun->flags & TUN_VNET_LE);
2672 if (put_user(le, (int __user *)argp))
2673 ret = -EFAULT;
2674 break;
2675
2676 case TUNSETVNETLE:
2677 if (get_user(le, (int __user *)argp)) {
2678 ret = -EFAULT;
2679 break;
2680 }
2681 if (le)
2682 tun->flags |= TUN_VNET_LE;
2683 else
2684 tun->flags &= ~TUN_VNET_LE;
2685 break;
2686
Greg Kurz8b8e6582015-04-24 14:50:36 +02002687 case TUNGETVNETBE:
2688 ret = tun_get_vnet_be(tun, argp);
2689 break;
2690
2691 case TUNSETVNETBE:
2692 ret = tun_set_vnet_be(tun, argp);
2693 break;
2694
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002695 case TUNATTACHFILTER:
2696 /* Can be set only for TAPs */
2697 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002698 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002699 break;
2700 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002701 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002702 break;
2703
Jason Wangc8d68e62012-10-31 19:46:00 +00002704 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002705 break;
2706
2707 case TUNDETACHFILTER:
2708 /* Can be set only for TAPs */
2709 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002710 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002711 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002712 ret = 0;
2713 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002714 break;
2715
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002716 case TUNGETFILTER:
2717 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002718 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002719 break;
2720 ret = -EFAULT;
2721 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2722 break;
2723 ret = 0;
2724 break;
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002727 ret = -EINVAL;
2728 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Herbert Xu876bfd42009-08-06 14:22:44 +00002731unlock:
2732 rtnl_unlock();
2733 if (tun)
2734 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002735 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Arnd Bergmann50857e22009-11-06 22:52:32 -08002738static long tun_chr_ioctl(struct file *file,
2739 unsigned int cmd, unsigned long arg)
2740{
2741 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2742}
2743
2744#ifdef CONFIG_COMPAT
2745static long tun_chr_compat_ioctl(struct file *file,
2746 unsigned int cmd, unsigned long arg)
2747{
2748 switch (cmd) {
2749 case TUNSETIFF:
2750 case TUNGETIFF:
2751 case TUNSETTXFILTER:
2752 case TUNGETSNDBUF:
2753 case TUNSETSNDBUF:
2754 case SIOCGIFHWADDR:
2755 case SIOCSIFHWADDR:
2756 arg = (unsigned long)compat_ptr(arg);
2757 break;
2758 default:
2759 arg = (compat_ulong_t)arg;
2760 break;
2761 }
2762
2763 /*
2764 * compat_ifreq is shorter than ifreq, so we must not access beyond
2765 * the end of that structure. All fields that are used in this
2766 * driver are compatible though, we don't need to convert the
2767 * contents.
2768 */
2769 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2770}
2771#endif /* CONFIG_COMPAT */
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773static int tun_chr_fasync(int fd, struct file *file, int on)
2774{
Jason Wang54f968d2012-10-31 19:45:57 +00002775 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 int ret;
2777
Jason Wang54f968d2012-10-31 19:45:57 +00002778 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002779 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (on) {
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002782 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00002783 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002784 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002785 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002786 ret = 0;
2787out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002788 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
2791static int tun_chr_open(struct inode *inode, struct file * file)
2792{
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002793 struct net *net = current->nsproxy->net_ns;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002794 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002795
Joe Perches6b8a66e2011-03-02 07:18:10 +00002796 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002797
Eric W. Biederman140e807da2015-05-08 21:07:08 -05002798 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05002799 &tun_proto, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002800 if (!tfile)
2801 return -ENOMEM;
Monam Agarwalc9566742014-03-24 00:02:32 +05302802 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang54f968d2012-10-31 19:45:57 +00002803 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002804 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002805
Jason Wang54f968d2012-10-31 19:45:57 +00002806 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07002807 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00002808
2809 tfile->socket.file = file;
2810 tfile->socket.ops = &tun_socket_ops;
2811
2812 sock_init_data(&tfile->socket, &tfile->sk);
Jason Wang54f968d2012-10-31 19:45:57 +00002813
2814 tfile->sk.sk_write_space = tun_sock_write_space;
2815 tfile->sk.sk_sndbuf = INT_MAX;
2816
Eric W. Biederman631ab462009-01-20 11:00:40 +00002817 file->private_data = tfile;
Jason Wang4008e972012-12-13 23:53:30 +00002818 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002819
Jason Wang19a6afb2013-06-08 14:17:41 +08002820 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return 0;
2823}
2824
2825static int tun_chr_close(struct inode *inode, struct file *file)
2826{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002827 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Jason Wangc8d68e62012-10-31 19:46:00 +00002829 tun_detach(tfile, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 return 0;
2832}
2833
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002834#ifdef CONFIG_PROC_FS
yuan linyu9484dc72017-09-23 22:36:52 +08002835static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002836{
yuan linyu9484dc72017-09-23 22:36:52 +08002837 struct tun_file *tfile = file->private_data;
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002838 struct tun_struct *tun;
2839 struct ifreq ifr;
2840
2841 memset(&ifr, 0, sizeof(ifr));
2842
2843 rtnl_lock();
yuan linyu9484dc72017-09-23 22:36:52 +08002844 tun = tun_get(tfile);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002845 if (tun)
2846 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2847 rtnl_unlock();
2848
2849 if (tun)
2850 tun_put(tun);
2851
Joe Perchesa3816ab2014-09-29 16:08:25 -07002852 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002853}
2854#endif
2855
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002856static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002857 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05002859 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04002860 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002862 .unlocked_ioctl = tun_chr_ioctl,
2863#ifdef CONFIG_COMPAT
2864 .compat_ioctl = tun_chr_compat_ioctl,
2865#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 .open = tun_chr_open,
2867 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002868 .fasync = tun_chr_fasync,
2869#ifdef CONFIG_PROC_FS
2870 .show_fdinfo = tun_chr_show_fdinfo,
2871#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872};
2873
2874static struct miscdevice tun_miscdev = {
2875 .minor = TUN_MINOR,
2876 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002877 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879};
2880
2881/* ethtool interface */
2882
Philippe Reynes29ccc492017-03-11 22:03:50 +01002883static int tun_get_link_ksettings(struct net_device *dev,
2884 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
Philippe Reynes29ccc492017-03-11 22:03:50 +01002886 ethtool_link_ksettings_zero_link_mode(cmd, supported);
2887 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
2888 cmd->base.speed = SPEED_10;
2889 cmd->base.duplex = DUPLEX_FULL;
2890 cmd->base.port = PORT_TP;
2891 cmd->base.phy_address = 0;
2892 cmd->base.autoneg = AUTONEG_DISABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return 0;
2894}
2895
2896static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2897{
2898 struct tun_struct *tun = netdev_priv(dev);
2899
Rick Jones33a5ba12011-11-15 14:59:53 +00002900 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2901 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002904 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00002905 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002907 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00002908 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 break;
2910 }
2911}
2912
2913static u32 tun_get_msglevel(struct net_device *dev)
2914{
2915#ifdef TUN_DEBUG
2916 struct tun_struct *tun = netdev_priv(dev);
2917 return tun->debug;
2918#else
2919 return -EOPNOTSUPP;
2920#endif
2921}
2922
2923static void tun_set_msglevel(struct net_device *dev, u32 value)
2924{
2925#ifdef TUN_DEBUG
2926 struct tun_struct *tun = netdev_priv(dev);
2927 tun->debug = value;
2928#endif
2929}
2930
Jason Wang5503fce2017-01-18 15:02:03 +08002931static int tun_get_coalesce(struct net_device *dev,
2932 struct ethtool_coalesce *ec)
2933{
2934 struct tun_struct *tun = netdev_priv(dev);
2935
2936 ec->rx_max_coalesced_frames = tun->rx_batched;
2937
2938 return 0;
2939}
2940
2941static int tun_set_coalesce(struct net_device *dev,
2942 struct ethtool_coalesce *ec)
2943{
2944 struct tun_struct *tun = netdev_priv(dev);
2945
2946 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
2947 tun->rx_batched = NAPI_POLL_WEIGHT;
2948 else
2949 tun->rx_batched = ec->rx_max_coalesced_frames;
2950
2951 return 0;
2952}
2953
Jeff Garzik7282d492006-09-13 14:30:00 -04002954static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 .get_drvinfo = tun_get_drvinfo,
2956 .get_msglevel = tun_get_msglevel,
2957 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002958 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002959 .get_ts_info = ethtool_op_get_ts_info,
Jason Wang5503fce2017-01-18 15:02:03 +08002960 .get_coalesce = tun_get_coalesce,
2961 .set_coalesce = tun_set_coalesce,
Philippe Reynes29ccc492017-03-11 22:03:50 +01002962 .get_link_ksettings = tun_get_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963};
2964
Jason Wang1576d982016-06-30 14:45:36 +08002965static int tun_queue_resize(struct tun_struct *tun)
2966{
2967 struct net_device *dev = tun->dev;
2968 struct tun_file *tfile;
2969 struct skb_array **arrays;
2970 int n = tun->numqueues + tun->numdisabled;
2971 int ret, i;
2972
stephen hemminger12039042017-08-15 10:29:16 -07002973 arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL);
Jason Wang1576d982016-06-30 14:45:36 +08002974 if (!arrays)
2975 return -ENOMEM;
2976
2977 for (i = 0; i < tun->numqueues; i++) {
2978 tfile = rtnl_dereference(tun->tfiles[i]);
2979 arrays[i] = &tfile->tx_array;
2980 }
2981 list_for_each_entry(tfile, &tun->disabled, next)
2982 arrays[i++] = &tfile->tx_array;
2983
2984 ret = skb_array_resize_multiple(arrays, n,
2985 dev->tx_queue_len, GFP_KERNEL);
2986
2987 kfree(arrays);
2988 return ret;
2989}
2990
2991static int tun_device_event(struct notifier_block *unused,
2992 unsigned long event, void *ptr)
2993{
2994 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2995 struct tun_struct *tun = netdev_priv(dev);
2996
Craig Gallek86dfb4ac2016-07-06 18:44:20 -04002997 if (dev->rtnl_link_ops != &tun_link_ops)
2998 return NOTIFY_DONE;
2999
Jason Wang1576d982016-06-30 14:45:36 +08003000 switch (event) {
3001 case NETDEV_CHANGE_TX_QUEUE_LEN:
3002 if (tun_queue_resize(tun))
3003 return NOTIFY_BAD;
3004 break;
3005 default:
3006 break;
3007 }
3008
3009 return NOTIFY_DONE;
3010}
3011
3012static struct notifier_block tun_notifier_block __read_mostly = {
3013 .notifier_call = tun_device_event,
3014};
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016static int __init tun_init(void)
3017{
3018 int ret = 0;
3019
Joe Perches6b8a66e2011-03-02 07:18:10 +00003020 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003022 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003023 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003024 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003025 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003026 }
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003029 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003030 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003031 goto err_misc;
3032 }
Jason Wang1576d982016-06-30 14:45:36 +08003033
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003034 ret = register_netdevice_notifier(&tun_notifier_block);
3035 if (ret) {
3036 pr_err("Can't register netdevice notifier\n");
3037 goto err_notifier;
3038 }
3039
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003040 return 0;
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003041
3042err_notifier:
3043 misc_deregister(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003044err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003045 rtnl_link_unregister(&tun_link_ops);
3046err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return ret;
3048}
3049
3050static void tun_cleanup(void)
3051{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003052 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003053 rtnl_link_unregister(&tun_link_ops);
Jason Wang1576d982016-06-30 14:45:36 +08003054 unregister_netdevice_notifier(&tun_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003057/* Get an underlying socket object from tun file. Returns error unless file is
3058 * attached to a device. The returned object works like a packet socket, it
3059 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
3060 * holding a reference to the file for as long as the socket is in use. */
3061struct socket *tun_get_socket(struct file *file)
3062{
Jason Wang6e914fc2012-10-31 19:45:58 +00003063 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003064 if (file->f_op != &tun_fops)
3065 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00003066 tfile = file->private_data;
3067 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003068 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00003069 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003070}
3071EXPORT_SYMBOL_GPL(tun_get_socket);
3072
Jason Wang83339c62017-05-17 12:14:41 +08003073struct skb_array *tun_get_skb_array(struct file *file)
3074{
3075 struct tun_file *tfile;
3076
3077 if (file->f_op != &tun_fops)
3078 return ERR_PTR(-EINVAL);
3079 tfile = file->private_data;
3080 if (!tfile)
3081 return ERR_PTR(-EBADFD);
3082 return &tfile->tx_array;
3083}
3084EXPORT_SYMBOL_GPL(tun_get_skb_array);
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086module_init(tun_init);
3087module_exit(tun_cleanup);
3088MODULE_DESCRIPTION(DRV_DESCRIPTION);
3089MODULE_AUTHOR(DRV_COPYRIGHT);
3090MODULE_LICENSE("GPL");
3091MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02003092MODULE_ALIAS("devname:net/tun");