blob: abae165dcca5aff91989dc78afe1ca33d85fc8f8 [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>
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +020073#include <net/xdp.h>
Masatake YAMATO93e14b62014-01-29 16:43:31 +090074#include <linux/seq_file.h>
Herbert Xue0b46d02014-11-07 21:22:23 +080075#include <linux/uio.h>
Jason Wang1576d982016-06-30 14:45:36 +080076#include <linux/skb_array.h>
Jason Wang761876c2017-08-11 19:41:18 +080077#include <linux/bpf.h>
78#include <linux/bpf_trace.h>
Petar Penkov90e33d42017-09-22 13:49:15 -070079#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080081#include <linux/uaccess.h>
Kirill Tkhaif2780d62018-02-14 16:40:14 +030082#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Chas Williams4e24f2d2018-06-02 17:49:53 -040084static void tun_default_link_ksettings(struct net_device *dev,
85 struct ethtool_link_ksettings *cmd);
86
Rusty Russell14daa022008-04-12 18:48:58 -070087/* Uncomment to enable debugging */
88/* #define TUN_DEBUG 1 */
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#ifdef TUN_DEBUG
91static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070092
Joe Perches6b8a66e2011-03-02 07:18:10 +000093#define tun_debug(level, tun, fmt, args...) \
94do { \
95 if (tun->debug) \
96 netdev_printk(level, tun->dev, fmt, ##args); \
97} while (0)
98#define DBG1(level, fmt, args...) \
99do { \
100 if (debug == 2) \
101 printk(level fmt, ##args); \
102} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -0700103#else
Joe Perches6b8a66e2011-03-02 07:18:10 +0000104#define tun_debug(level, tun, fmt, args...) \
105do { \
106 if (0) \
107 netdev_printk(level, tun->dev, fmt, ##args); \
108} while (0)
109#define DBG1(level, fmt, args...) \
110do { \
111 if (0) \
112 printk(level fmt, ##args); \
113} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115
Jason Wang7df13212017-09-04 11:36:08 +0800116#define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
Jason Wang66ccbc92017-08-11 19:41:16 +0800117
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +0200118/* TUN device flags */
119
120/* IFF_ATTACH_QUEUE is never stored in device flags,
121 * overload it to mean fasync when stored there.
122 */
123#define TUN_FASYNC IFF_ATTACH_QUEUE
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200124/* High bits in flags field are unused. */
125#define TUN_VNET_LE 0x80000000
Greg Kurz8b8e6582015-04-24 14:50:36 +0200126#define TUN_VNET_BE 0x40000000
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +0200127
128#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Petar Penkov90e33d42017-09-22 13:49:15 -0700129 IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS)
130
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000131#define GOODCOPY_LEN 128
132
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700133#define FLT_EXACT_COUNT 8
134struct tap_filter {
135 unsigned int count; /* Number of addrs. Zero means disabled */
136 u32 mask[2]; /* Mask of the hashed addrs */
137 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
138};
139
Pankaj Guptabaf71c52015-01-12 11:41:29 +0530140/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
141 * to max number of VCPUs in guest. */
142#define MAX_TAP_QUEUES 256
Jason Wangb8732fb2013-01-23 03:59:13 +0000143#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000144
Jason Wang96442e422012-10-31 19:46:02 +0000145#define TUN_FLOW_EXPIRE (3 * HZ)
146
Paolo Abeni608b9972016-04-13 10:52:20 +0200147struct tun_pcpu_stats {
148 u64 rx_packets;
149 u64 rx_bytes;
150 u64 tx_packets;
151 u64 tx_bytes;
152 struct u64_stats_sync syncp;
153 u32 rx_dropped;
154 u32 tx_dropped;
155 u32 rx_frame_errors;
156};
157
Jason Wang54f968d2012-10-31 19:45:57 +0000158/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800159 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000160 * to serve as one transmit queue for tuntap device. The sock_fprog and
161 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000162 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000163 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000164 *
165 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000166 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000167 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000168 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000169struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000170 struct sock sk;
171 struct socket socket;
172 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000173 struct tun_struct __rcu *tun;
Jason Wang54f968d2012-10-31 19:45:57 +0000174 struct fasync_struct *fasync;
175 /* only used for fasnyc */
176 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400177 union {
178 u16 queue_index;
179 unsigned int ifindex;
180 };
Petar Penkov94317092017-09-22 13:49:14 -0700181 struct napi_struct napi;
Eric Dumazetaec72f32017-10-18 12:12:09 -0700182 bool napi_enabled;
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700183 bool napi_frags_enabled;
Petar Penkov90e33d42017-09-22 13:49:15 -0700184 struct mutex napi_mutex; /* Protects access to the above napi */
Jason Wang4008e972012-12-13 23:53:30 +0000185 struct list_head next;
186 struct tun_struct *detached;
Jason Wang5990a302018-01-04 11:14:27 +0800187 struct ptr_ring tx_ring;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100188 struct xdp_rxq_info xdp_rxq;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000189};
190
Jason Wangf9e06c42018-11-15 17:43:10 +0800191struct tun_page {
192 struct page *page;
193 int count;
194};
195
Jason Wang96442e422012-10-31 19:46:02 +0000196struct tun_flow_entry {
197 struct hlist_node hash_link;
198 struct rcu_head rcu;
199 struct tun_struct *tun;
200
201 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800202 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000203 int queue_index;
Li RongQing83b1bc12018-12-06 16:08:17 +0800204 unsigned long updated ____cacheline_aligned_in_smp;
Jason Wang96442e422012-10-31 19:46:02 +0000205};
206
207#define TUN_NUM_FLOW_ENTRIES 1024
Li RongQingf13b5462018-08-03 15:50:02 +0800208#define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1)
Jason Wang96442e422012-10-31 19:46:02 +0000209
Jason Wangcd5681d2018-01-16 16:31:01 +0800210struct tun_prog {
Jason Wang96f84062017-12-04 17:31:23 +0800211 struct rcu_head rcu;
212 struct bpf_prog *prog;
213};
214
Jason Wang54f968d2012-10-31 19:45:57 +0000215/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000216 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000217 * file were attached to a persist device.
218 */
Rusty Russell14daa022008-04-12 18:48:58 -0700219struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000220 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
221 unsigned int numqueues;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700222 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800223 kuid_t owner;
224 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700225
Rusty Russell14daa022008-04-12 18:48:58 -0700226 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000227 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000228#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
David S. Millerd591a1f2017-07-03 06:35:32 -0700229 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200230
Paolo Abenieaea34b2016-02-26 10:45:40 +0100231 int align;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200232 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000233 int sndbuf;
234 struct tap_filter txflt;
235 struct sock_fprog fprog;
236 /* protected by rtnl lock */
237 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700238#ifdef TUN_DEBUG
239 int debug;
240#endif
Jason Wang96442e422012-10-31 19:46:02 +0000241 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000242 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
243 struct timer_list flow_gc_timer;
244 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000245 unsigned int numdisabled;
246 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000247 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000248 u32 flow_count;
Jason Wang5503fce2017-01-18 15:02:03 +0800249 u32 rx_batched;
Paolo Abeni608b9972016-04-13 10:52:20 +0200250 struct tun_pcpu_stats __percpu *pcpu_stats;
Jason Wang761876c2017-08-11 19:41:18 +0800251 struct bpf_prog __rcu *xdp_prog;
Jason Wangcd5681d2018-01-16 16:31:01 +0800252 struct tun_prog __rcu *steering_prog;
Jason Wangaff3d702018-01-16 16:31:02 +0800253 struct tun_prog __rcu *filter_prog;
Chas Williams4e24f2d2018-06-02 17:49:53 -0400254 struct ethtool_link_ksettings link_ksettings;
Rusty Russell14daa022008-04-12 18:48:58 -0700255};
256
Jason Wangaff3d702018-01-16 16:31:02 +0800257struct veth {
258 __be16 h_vlan_proto;
259 __be16 h_vlan_TCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200262bool tun_is_xdp_frame(void *ptr)
Jason Wangfc72d1d2018-01-04 11:14:28 +0800263{
264 return (unsigned long)ptr & TUN_XDP_FLAG;
265}
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200266EXPORT_SYMBOL(tun_is_xdp_frame);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800267
268void *tun_xdp_to_ptr(void *ptr)
269{
270 return (void *)((unsigned long)ptr | TUN_XDP_FLAG);
271}
272EXPORT_SYMBOL(tun_xdp_to_ptr);
273
274void *tun_ptr_to_xdp(void *ptr)
275{
276 return (void *)((unsigned long)ptr & ~TUN_XDP_FLAG);
277}
278EXPORT_SYMBOL(tun_ptr_to_xdp);
279
Petar Penkov94317092017-09-22 13:49:14 -0700280static int tun_napi_receive(struct napi_struct *napi, int budget)
281{
282 struct tun_file *tfile = container_of(napi, struct tun_file, napi);
283 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
284 struct sk_buff_head process_queue;
285 struct sk_buff *skb;
286 int received = 0;
287
288 __skb_queue_head_init(&process_queue);
289
290 spin_lock(&queue->lock);
291 skb_queue_splice_tail_init(queue, &process_queue);
292 spin_unlock(&queue->lock);
293
294 while (received < budget && (skb = __skb_dequeue(&process_queue))) {
295 napi_gro_receive(napi, skb);
296 ++received;
297 }
298
299 if (!skb_queue_empty(&process_queue)) {
300 spin_lock(&queue->lock);
301 skb_queue_splice(&process_queue, queue);
302 spin_unlock(&queue->lock);
303 }
304
305 return received;
306}
307
308static int tun_napi_poll(struct napi_struct *napi, int budget)
309{
310 unsigned int received;
311
312 received = tun_napi_receive(napi, budget);
313
314 if (received < budget)
315 napi_complete_done(napi, received);
316
317 return received;
318}
319
320static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700321 bool napi_en, bool napi_frags)
Petar Penkov94317092017-09-22 13:49:14 -0700322{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700323 tfile->napi_enabled = napi_en;
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700324 tfile->napi_frags_enabled = napi_en && napi_frags;
Petar Penkov94317092017-09-22 13:49:14 -0700325 if (napi_en) {
326 netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
327 NAPI_POLL_WEIGHT);
328 napi_enable(&tfile->napi);
329 }
330}
331
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700332static void tun_napi_disable(struct tun_file *tfile)
Petar Penkov94317092017-09-22 13:49:14 -0700333{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700334 if (tfile->napi_enabled)
Petar Penkov94317092017-09-22 13:49:14 -0700335 napi_disable(&tfile->napi);
336}
337
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700338static void tun_napi_del(struct tun_file *tfile)
Petar Penkov94317092017-09-22 13:49:14 -0700339{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700340 if (tfile->napi_enabled)
Petar Penkov94317092017-09-22 13:49:14 -0700341 netif_napi_del(&tfile->napi);
342}
343
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700344static bool tun_napi_frags_enabled(const struct tun_file *tfile)
Petar Penkov90e33d42017-09-22 13:49:15 -0700345{
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700346 return tfile->napi_frags_enabled;
Petar Penkov90e33d42017-09-22 13:49:15 -0700347}
348
Greg Kurz8b8e6582015-04-24 14:50:36 +0200349#ifdef CONFIG_TUN_VNET_CROSS_LE
350static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
351{
352 return tun->flags & TUN_VNET_BE ? false :
353 virtio_legacy_is_little_endian();
354}
355
356static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
357{
358 int be = !!(tun->flags & TUN_VNET_BE);
359
360 if (put_user(be, argp))
361 return -EFAULT;
362
363 return 0;
364}
365
366static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
367{
368 int be;
369
370 if (get_user(be, argp))
371 return -EFAULT;
372
373 if (be)
374 tun->flags |= TUN_VNET_BE;
375 else
376 tun->flags &= ~TUN_VNET_BE;
377
378 return 0;
379}
380#else
381static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
382{
383 return virtio_legacy_is_little_endian();
384}
385
386static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
387{
388 return -EINVAL;
389}
390
391static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
392{
393 return -EINVAL;
394}
395#endif /* CONFIG_TUN_VNET_CROSS_LE */
396
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200397static inline bool tun_is_little_endian(struct tun_struct *tun)
398{
Greg Kurz7d824102015-04-24 14:26:24 +0200399 return tun->flags & TUN_VNET_LE ||
Greg Kurz8b8e6582015-04-24 14:50:36 +0200400 tun_legacy_is_little_endian(tun);
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200401}
402
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300403static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
404{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200405 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300406}
407
408static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
409{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200410 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300411}
412
Jason Wang96442e422012-10-31 19:46:02 +0000413static inline u32 tun_hashfn(u32 rxhash)
414{
Li RongQingf13b5462018-08-03 15:50:02 +0800415 return rxhash & TUN_MASK_FLOW_ENTRIES;
Jason Wang96442e422012-10-31 19:46:02 +0000416}
417
418static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
419{
420 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000421
Sasha Levinb67bfe02013-02-27 17:06:00 -0800422 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000423 if (e->rxhash == rxhash)
424 return e;
425 }
426 return NULL;
427}
428
429static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
430 struct hlist_head *head,
431 u32 rxhash, u16 queue_index)
432{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000433 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
434
Jason Wang96442e422012-10-31 19:46:02 +0000435 if (e) {
436 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
437 rxhash, queue_index);
438 e->updated = jiffies;
439 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800440 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000441 e->queue_index = queue_index;
442 e->tun = tun;
443 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000444 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000445 }
446 return e;
447}
448
Jason Wang96442e422012-10-31 19:46:02 +0000449static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
450{
451 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
452 e->rxhash, e->queue_index);
453 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000454 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000455 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000456}
457
458static void tun_flow_flush(struct tun_struct *tun)
459{
460 int i;
461
462 spin_lock_bh(&tun->lock);
463 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
464 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800465 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000466
Sasha Levinb67bfe02013-02-27 17:06:00 -0800467 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000468 tun_flow_delete(tun, e);
469 }
470 spin_unlock_bh(&tun->lock);
471}
472
473static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
474{
475 int i;
476
477 spin_lock_bh(&tun->lock);
478 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
479 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800480 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000481
Sasha Levinb67bfe02013-02-27 17:06:00 -0800482 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000483 if (e->queue_index == queue_index)
484 tun_flow_delete(tun, e);
485 }
486 }
487 spin_unlock_bh(&tun->lock);
488}
489
Kees Cooke99e88a2017-10-16 14:43:17 -0700490static void tun_flow_cleanup(struct timer_list *t)
Jason Wang96442e422012-10-31 19:46:02 +0000491{
Kees Cooke99e88a2017-10-16 14:43:17 -0700492 struct tun_struct *tun = from_timer(tun, t, flow_gc_timer);
Jason Wang96442e422012-10-31 19:46:02 +0000493 unsigned long delay = tun->ageing_time;
494 unsigned long next_timer = jiffies + delay;
495 unsigned long count = 0;
496 int i;
497
498 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
499
Eric Dumazet7dbfb4e2017-10-20 11:29:55 -0700500 spin_lock(&tun->lock);
Jason Wang96442e422012-10-31 19:46:02 +0000501 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
502 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800503 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000504
Sasha Levinb67bfe02013-02-27 17:06:00 -0800505 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000506 unsigned long this_timer;
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700507
Jason Wang96442e422012-10-31 19:46:02 +0000508 this_timer = e->updated + delay;
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700509 if (time_before_eq(this_timer, jiffies)) {
Jason Wang96442e422012-10-31 19:46:02 +0000510 tun_flow_delete(tun, e);
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700511 continue;
512 }
513 count++;
514 if (time_before(this_timer, next_timer))
Jason Wang96442e422012-10-31 19:46:02 +0000515 next_timer = this_timer;
516 }
517 }
518
519 if (count)
520 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
Eric Dumazet7dbfb4e2017-10-20 11:29:55 -0700521 spin_unlock(&tun->lock);
Jason Wang96442e422012-10-31 19:46:02 +0000522}
523
Eric Dumazet49974422012-12-12 19:22:57 +0000524static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000525 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000526{
527 struct hlist_head *head;
528 struct tun_flow_entry *e;
529 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000530 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000531
Li RongQing5c327f62018-12-06 16:28:11 +0800532 head = &tun->flows[tun_hashfn(rxhash)];
Jason Wang96442e422012-10-31 19:46:02 +0000533
534 rcu_read_lock();
535
Jason Wang96442e422012-10-31 19:46:02 +0000536 e = tun_flow_find(head, rxhash);
537 if (likely(e)) {
538 /* TODO: keep queueing to old queue until it's empty? */
Li RongQing83b1bc12018-12-06 16:08:17 +0800539 if (e->queue_index != queue_index)
540 e->queue_index = queue_index;
541 if (e->updated != jiffies)
542 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800543 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000544 } else {
545 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000546 if (!tun_flow_find(head, rxhash) &&
547 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000548 tun_flow_create(tun, head, rxhash, queue_index);
549
550 if (!timer_pending(&tun->flow_gc_timer))
551 mod_timer(&tun->flow_gc_timer,
552 round_jiffies_up(jiffies + delay));
553 spin_unlock_bh(&tun->lock);
554 }
555
Jason Wang96442e422012-10-31 19:46:02 +0000556 rcu_read_unlock();
557}
558
Tom Herbert9bc88932013-12-22 18:54:32 +0800559/**
560 * Save the hash received in the stack receive path and update the
561 * flow_hash table accordingly.
562 */
563static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
564{
Eric Dumazet567e4b72015-02-06 12:59:01 -0800565 if (unlikely(e->rps_rxhash != hash))
Tom Herbert9bc88932013-12-22 18:54:32 +0800566 e->rps_rxhash = hash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800567}
568
Wang Li4b035272018-10-09 10:32:04 +0800569/* We try to identify a flow through its rxhash. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800570 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000571 * the rxq based on the txq where the last packet of the flow comes. As
572 * the userspace application move between processors, we may get a
Wang Li4b035272018-10-09 10:32:04 +0800573 * different rxq no. here.
Jason Wangc8d68e62012-10-31 19:46:00 +0000574 */
Jason Wang96f84062017-12-04 17:31:23 +0800575static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
Jason Wangc8d68e62012-10-31 19:46:00 +0000576{
Jason Wang96442e422012-10-31 19:46:02 +0000577 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000578 u32 txq = 0;
579 u32 numqueues = 0;
580
Mark Rutland6aa7de02017-10-23 14:07:29 -0700581 numqueues = READ_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000582
Jason Wangfeec0842017-06-06 14:09:49 +0800583 txq = __skb_get_hash_symmetric(skb);
Wang Li4b035272018-10-09 10:32:04 +0800584 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
585 if (e) {
586 tun_flow_save_rps_rxhash(e, txq);
587 txq = e->queue_index;
588 } else {
589 /* use multiply and shift instead of expensive divide */
590 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000591 }
592
Jason Wangc8d68e62012-10-31 19:46:00 +0000593 return txq;
594}
595
Jason Wang96f84062017-12-04 17:31:23 +0800596static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
597{
Jason Wangcd5681d2018-01-16 16:31:01 +0800598 struct tun_prog *prog;
Jason Wanga35d3102019-05-08 23:20:17 -0400599 u32 numqueues;
Jason Wang96f84062017-12-04 17:31:23 +0800600 u16 ret = 0;
601
Jason Wanga35d3102019-05-08 23:20:17 -0400602 numqueues = READ_ONCE(tun->numqueues);
603 if (!numqueues)
604 return 0;
605
Jason Wang96f84062017-12-04 17:31:23 +0800606 prog = rcu_dereference(tun->steering_prog);
607 if (prog)
608 ret = bpf_prog_run_clear_cb(prog->prog, skb);
609
Jason Wanga35d3102019-05-08 23:20:17 -0400610 return ret % numqueues;
Jason Wang96f84062017-12-04 17:31:23 +0800611}
612
613static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +0100614 struct net_device *sb_dev)
Jason Wang96f84062017-12-04 17:31:23 +0800615{
616 struct tun_struct *tun = netdev_priv(dev);
617 u16 ret;
618
619 rcu_read_lock();
620 if (rcu_dereference(tun->steering_prog))
621 ret = tun_ebpf_select_queue(tun, skb);
622 else
623 ret = tun_automq_select_queue(tun, skb);
624 rcu_read_unlock();
625
626 return ret;
627}
628
Jason Wangcde8b152012-10-31 19:46:01 +0000629static inline bool tun_not_capable(struct tun_struct *tun)
630{
631 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000632 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000633
634 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
635 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000636 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000637}
638
Jason Wangc8d68e62012-10-31 19:46:00 +0000639static void tun_set_real_num_queues(struct tun_struct *tun)
640{
641 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
642 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
643}
644
Jason Wang4008e972012-12-13 23:53:30 +0000645static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
646{
647 tfile->detached = tun;
648 list_add_tail(&tfile->next, &tun->disabled);
649 ++tun->numdisabled;
650}
651
Jason Wangd32649d2012-12-18 11:00:27 +0800652static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000653{
654 struct tun_struct *tun = tfile->detached;
655
656 tfile->detached = NULL;
657 list_del_init(&tfile->next);
658 --tun->numdisabled;
659 return tun;
660}
661
Jason Wang3a403072018-03-09 14:50:34 +0800662void tun_ptr_free(void *ptr)
Jason Wangfc72d1d2018-01-04 11:14:28 +0800663{
664 if (!ptr)
665 return;
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200666 if (tun_is_xdp_frame(ptr)) {
667 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800668
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +0200669 xdp_return_frame(xdpf);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800670 } else {
671 __skb_array_destroy_skb(ptr);
672 }
673}
Jason Wang3a403072018-03-09 14:50:34 +0800674EXPORT_SYMBOL_GPL(tun_ptr_free);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800675
Jason Wang4bfb0512013-09-05 17:53:59 +0800676static void tun_queue_purge(struct tun_file *tfile)
677{
Jason Wangfc72d1d2018-01-04 11:14:28 +0800678 void *ptr;
Jason Wang1576d982016-06-30 14:45:36 +0800679
Jason Wangfc72d1d2018-01-04 11:14:28 +0800680 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL)
681 tun_ptr_free(ptr);
Jason Wang1576d982016-06-30 14:45:36 +0800682
Jason Wang5503fce2017-01-18 15:02:03 +0800683 skb_queue_purge(&tfile->sk.sk_write_queue);
Jason Wang4bfb0512013-09-05 17:53:59 +0800684 skb_queue_purge(&tfile->sk.sk_error_queue);
685}
686
Jason Wangc8d68e62012-10-31 19:46:00 +0000687static void __tun_detach(struct tun_file *tfile, bool clean)
688{
689 struct tun_file *ntfile;
690 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000691
Jason Wangb8deabd2013-01-11 16:59:32 +0000692 tun = rtnl_dereference(tfile->tun);
693
Petar Penkov94317092017-09-22 13:49:14 -0700694 if (tun && clean) {
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700695 tun_napi_disable(tfile);
696 tun_napi_del(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700697 }
698
Jason Wang9e857222013-01-28 01:05:19 +0000699 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000700 u16 index = tfile->queue_index;
701 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000702
703 rcu_assign_pointer(tun->tfiles[index],
704 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000705 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000706 ntfile->queue_index = index;
Jason Wang9871a9e2019-05-08 23:20:18 -0400707 rcu_assign_pointer(tun->tfiles[tun->numqueues - 1],
708 NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000709
710 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000711 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530712 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000713 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000714 } else
Jason Wang4008e972012-12-13 23:53:30 +0000715 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000716
717 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000718 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000719 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800720 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000721 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000722 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000723 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000724 sock_put(&tfile->sk);
725 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000726
727 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000728 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
729 netif_carrier_off(tun->dev);
730
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200731 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000732 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000733 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000734 }
Jason Wangb196d882018-05-11 10:49:25 +0800735 if (tun)
736 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wang7063efd2018-05-16 20:39:33 +0800737 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free);
Eric W. Biederman140e807da2015-05-08 21:07:08 -0500738 sock_put(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000739 }
740}
741
742static void tun_detach(struct tun_file *tfile, bool clean)
743{
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200744 struct tun_struct *tun;
745 struct net_device *dev;
746
Jason Wangc8d68e62012-10-31 19:46:00 +0000747 rtnl_lock();
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200748 tun = rtnl_dereference(tfile->tun);
749 dev = tun ? tun->dev : NULL;
Jason Wangc8d68e62012-10-31 19:46:00 +0000750 __tun_detach(tfile, clean);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200751 if (dev)
752 netdev_state_change(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000753 rtnl_unlock();
754}
755
756static void tun_detach_all(struct net_device *dev)
757{
758 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000759 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000760 int i, n = tun->numqueues;
761
762 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000763 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000764 BUG_ON(!tfile);
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700765 tun_napi_disable(tfile);
Jason Wangaddf8fc2016-05-19 13:36:51 +0800766 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700767 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530768 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000769 --tun->numqueues;
770 }
Jason Wang9e857222013-01-28 01:05:19 +0000771 list_for_each_entry(tfile, &tun->disabled, next) {
Jason Wangaddf8fc2016-05-19 13:36:51 +0800772 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700773 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530774 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000775 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000776 BUG_ON(tun->numqueues != 0);
777
778 synchronize_net();
779 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000780 tfile = rtnl_dereference(tun->tfiles[i]);
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700781 tun_napi_del(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000782 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800783 tun_queue_purge(tfile);
Jason Wangb196d882018-05-11 10:49:25 +0800784 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wangc8d68e62012-10-31 19:46:00 +0000785 sock_put(&tfile->sk);
786 }
Jason Wang4008e972012-12-13 23:53:30 +0000787 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
788 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800789 tun_queue_purge(tfile);
Jason Wangb196d882018-05-11 10:49:25 +0800790 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wang4008e972012-12-13 23:53:30 +0000791 sock_put(&tfile->sk);
792 }
793 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000794
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200795 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000796 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000797}
798
Petar Penkov94317092017-09-22 13:49:14 -0700799static int tun_attach(struct tun_struct *tun, struct file *file,
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700800 bool skip_filter, bool napi, bool napi_frags)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000801{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000802 struct tun_file *tfile = file->private_data;
Jason Wang1576d982016-06-30 14:45:36 +0800803 struct net_device *dev = tun->dev;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000804 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000805
Paul Moore5dbbaf22013-01-14 07:12:19 +0000806 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
807 if (err < 0)
808 goto out;
809
Eric W. Biederman38231b72009-01-20 11:02:28 +0000810 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000811 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000812 goto out;
813
814 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200815 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000816 goto out;
817
818 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000819 if (!tfile->detached &&
820 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000821 goto out;
822
823 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000824
stephen hemminger92d4ea62013-12-05 20:42:58 -0800825 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400826 if (!skip_filter && (tun->filter_attached == true)) {
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +0200827 lock_sock(tfile->socket.sk);
828 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
829 release_sock(tfile->socket.sk);
Jason Wang54f968d2012-10-31 19:45:57 +0000830 if (!err)
831 goto out;
832 }
Jason Wang1576d982016-06-30 14:45:36 +0800833
834 if (!tfile->detached &&
Jason Wangb196d882018-05-11 10:49:25 +0800835 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len,
836 GFP_KERNEL, tun_ptr_free)) {
Jason Wang1576d982016-06-30 14:45:36 +0800837 err = -ENOMEM;
838 goto out;
839 }
840
Jason Wangc8d68e62012-10-31 19:46:00 +0000841 tfile->queue_index = tun->numqueues;
Jason Wangaddf8fc2016-05-19 13:36:51 +0800842 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100843
844 if (tfile->detached) {
845 /* Re-attach detached tfile, updating XDP queue_index */
846 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq));
847
848 if (tfile->xdp_rxq.queue_index != tfile->queue_index)
849 tfile->xdp_rxq.queue_index = tfile->queue_index;
850 } else {
851 /* Setup XDP RX-queue info, for new tfile getting attached */
852 err = xdp_rxq_info_reg(&tfile->xdp_rxq,
853 tun->dev, tfile->queue_index);
854 if (err < 0)
855 goto out;
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200856 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq,
857 MEM_TYPE_PAGE_SHARED, NULL);
858 if (err < 0) {
859 xdp_rxq_info_unreg(&tfile->xdp_rxq);
860 goto out;
861 }
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100862 err = 0;
863 }
864
Petar Penkov94317092017-09-22 13:49:14 -0700865 if (tfile->detached) {
Jason Wang4008e972012-12-13 23:53:30 +0000866 tun_enable_queue(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700867 } else {
Jason Wang4008e972012-12-13 23:53:30 +0000868 sock_hold(&tfile->sk);
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700869 tun_napi_init(tun, tfile, napi, napi_frags);
Petar Penkov94317092017-09-22 13:49:14 -0700870 }
Jason Wang4008e972012-12-13 23:53:30 +0000871
Jason Wange4a2a302018-09-12 11:16:59 +0800872 if (rtnl_dereference(tun->xdp_prog))
873 sock_set_flag(&tfile->sk, SOCK_XDP);
874
Jason Wangc8d68e62012-10-31 19:46:00 +0000875 /* device is allowed to go away first, so no need to hold extra
876 * refcnt.
877 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000878
Stanislav Fomichev0b7959b2019-01-07 13:38:38 -0800879 /* Publish tfile->tun and tun->tfiles only after we've fully
880 * initialized tfile; otherwise we risk using half-initialized
881 * object.
882 */
883 rcu_assign_pointer(tfile->tun, tun);
884 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
885 tun->numqueues++;
George Amanakis3a03cb82019-01-29 22:50:13 -0500886 tun_set_real_num_queues(tun);
Eric W. Biederman38231b72009-01-20 11:02:28 +0000887out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000888 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000889}
890
yuan linyu9484dc72017-09-23 22:36:52 +0800891static struct tun_struct *tun_get(struct tun_file *tfile)
Eric W. Biederman631ab462009-01-20 11:00:40 +0000892{
Jason Wang6e914fc2012-10-31 19:45:58 +0000893 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000894
Jason Wang6e914fc2012-10-31 19:45:58 +0000895 rcu_read_lock();
896 tun = rcu_dereference(tfile->tun);
897 if (tun)
898 dev_hold(tun->dev);
899 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000900
901 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000902}
903
Eric W. Biederman631ab462009-01-20 11:00:40 +0000904static void tun_put(struct tun_struct *tun)
905{
Jason Wang6e914fc2012-10-31 19:45:58 +0000906 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000907}
908
Joe Perches6b8a66e2011-03-02 07:18:10 +0000909/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700910static void addr_hash_set(u32 *mask, const u8 *addr)
911{
912 int n = ether_crc(ETH_ALEN, addr) >> 26;
913 mask[n >> 5] |= (1 << (n & 31));
914}
915
916static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
917{
918 int n = ether_crc(ETH_ALEN, addr) >> 26;
919 return mask[n >> 5] & (1 << (n & 31));
920}
921
922static int update_filter(struct tap_filter *filter, void __user *arg)
923{
924 struct { u8 u[ETH_ALEN]; } *addr;
925 struct tun_filter uf;
926 int err, alen, n, nexact;
927
928 if (copy_from_user(&uf, arg, sizeof(uf)))
929 return -EFAULT;
930
931 if (!uf.count) {
932 /* Disabled */
933 filter->count = 0;
934 return 0;
935 }
936
937 alen = ETH_ALEN * uf.count;
Markus Elfring28e81902016-08-20 08:54:15 +0200938 addr = memdup_user(arg + sizeof(uf), alen);
939 if (IS_ERR(addr))
940 return PTR_ERR(addr);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700941
942 /* The filter is updated without holding any locks. Which is
943 * perfectly safe. We disable it first and in the worst
944 * case we'll accept a few undesired packets. */
945 filter->count = 0;
946 wmb();
947
948 /* Use first set of addresses as an exact filter */
949 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
950 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
951
952 nexact = n;
953
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800954 /* Remaining multicast addresses are hashed,
955 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700956 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800957 for (; n < uf.count; n++) {
958 if (!is_multicast_ether_addr(addr[n].u)) {
959 err = 0; /* no filter */
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200960 goto free_addr;
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800961 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700962 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800963 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700964
965 /* For ALLMULTI just set the mask to all ones.
966 * This overrides the mask populated above. */
967 if ((uf.flags & TUN_FLT_ALLMULTI))
968 memset(filter->mask, ~0, sizeof(filter->mask));
969
970 /* Now enable the filter */
971 wmb();
972 filter->count = nexact;
973
974 /* Return the number of exact filters */
975 err = nexact;
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200976free_addr:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700977 kfree(addr);
978 return err;
979}
980
981/* Returns: 0 - drop, !=0 - accept */
982static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
983{
984 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
985 * at this point. */
986 struct ethhdr *eh = (struct ethhdr *) skb->data;
987 int i;
988
989 /* Exact match */
990 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000991 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700992 return 1;
993
994 /* Inexact match (multicast only) */
995 if (is_multicast_ether_addr(eh->h_dest))
996 return addr_hash_test(filter->mask, eh->h_dest);
997
998 return 0;
999}
1000
1001/*
1002 * Checks whether the packet is accepted or not.
1003 * Returns: 0 - drop, !=0 - accept
1004 */
1005static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
1006{
1007 if (!filter->count)
1008 return 1;
1009
1010 return run_filter(filter, skb);
1011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013/* Network device part of the driver */
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static const struct ethtool_ops tun_ethtool_ops;
1016
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001017/* Net device detach from fd. */
1018static void tun_net_uninit(struct net_device *dev)
1019{
Jason Wangc8d68e62012-10-31 19:46:00 +00001020 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023/* Net device open. */
1024static int tun_net_open(struct net_device *dev)
1025{
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001026 struct tun_struct *tun = netdev_priv(dev);
1027 int i;
1028
Jason Wangc8d68e62012-10-31 19:46:00 +00001029 netif_tx_start_all_queues(dev);
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001030
1031 for (i = 0; i < tun->numqueues; i++) {
1032 struct tun_file *tfile;
1033
1034 tfile = rtnl_dereference(tun->tfiles[i]);
1035 tfile->socket.sk->sk_write_space(tfile->socket.sk);
1036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039}
1040
1041/* Net device close. */
1042static int tun_net_close(struct net_device *dev)
1043{
Jason Wangc8d68e62012-10-31 19:46:00 +00001044 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
1046}
1047
1048/* Net device start xmit */
Jason Wang96f84062017-12-04 17:31:23 +08001049static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Jason Wang3df97ba2016-04-25 23:13:42 -04001051#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07001052 if (tun->numqueues == 1 && static_branch_unlikely(&rps_needed)) {
Tom Herbert9bc88932013-12-22 18:54:32 +08001053 /* Select queue was not called for the skbuff, so we extract the
1054 * RPS hash and save it into the flow_table here.
1055 */
Wang Li4b035272018-10-09 10:32:04 +08001056 struct tun_flow_entry *e;
Tom Herbert9bc88932013-12-22 18:54:32 +08001057 __u32 rxhash;
1058
Jason Wangfeec0842017-06-06 14:09:49 +08001059 rxhash = __skb_get_hash_symmetric(skb);
Wang Li4b035272018-10-09 10:32:04 +08001060 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], rxhash);
1061 if (e)
1062 tun_flow_save_rps_rxhash(e, rxhash);
Tom Herbert9bc88932013-12-22 18:54:32 +08001063 }
Jason Wang3df97ba2016-04-25 23:13:42 -04001064#endif
Jason Wang96f84062017-12-04 17:31:23 +08001065}
1066
Jason Wangaff3d702018-01-16 16:31:02 +08001067static unsigned int run_ebpf_filter(struct tun_struct *tun,
1068 struct sk_buff *skb,
1069 int len)
1070{
1071 struct tun_prog *prog = rcu_dereference(tun->filter_prog);
1072
1073 if (prog)
1074 len = bpf_prog_run_clear_cb(prog->prog, skb);
1075
1076 return len;
1077}
1078
Jason Wang96f84062017-12-04 17:31:23 +08001079/* Net device start xmit */
1080static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1081{
1082 struct tun_struct *tun = netdev_priv(dev);
1083 int txq = skb->queue_mapping;
1084 struct tun_file *tfile;
Jason Wangaff3d702018-01-16 16:31:02 +08001085 int len = skb->len;
Jason Wang96f84062017-12-04 17:31:23 +08001086
1087 rcu_read_lock();
1088 tfile = rcu_dereference(tun->tfiles[txq]);
Jason Wang96f84062017-12-04 17:31:23 +08001089
1090 /* Drop packet if interface is not attached */
Jason Wang9871a9e2019-05-08 23:20:18 -04001091 if (!tfile)
Jason Wang96f84062017-12-04 17:31:23 +08001092 goto drop;
1093
1094 if (!rcu_dereference(tun->steering_prog))
1095 tun_automq_xmit(tun, skb);
Tom Herbert9bc88932013-12-22 18:54:32 +08001096
Jason Wang6e914fc2012-10-31 19:45:58 +00001097 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
1098
Jason Wangc8d68e62012-10-31 19:46:00 +00001099 BUG_ON(!tfile);
1100
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001101 /* Drop if the filter does not like it.
1102 * This is a noop if the filter is disabled.
1103 * Filter can be enabled only for the TAP devices. */
1104 if (!check_filter(&tun->txflt, skb))
1105 goto drop;
1106
Jason Wang54f968d2012-10-31 19:45:57 +00001107 if (tfile->socket.sk->sk_filter &&
1108 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001109 goto drop;
1110
Jason Wangaff3d702018-01-16 16:31:02 +08001111 len = run_ebpf_filter(tun, skb, len);
Bjørn Mork81c89502018-04-17 22:46:38 +02001112 if (len == 0 || pskb_trim(skb, len))
Jason Wangaff3d702018-01-16 16:31:02 +08001113 goto drop;
1114
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001115 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Jason Wang7bf66302013-09-05 17:54:00 +08001116 goto drop;
1117
Soheil Hassas Yeganeh7b996242016-08-23 18:22:33 -04001118 skb_tx_timestamp(skb);
Richard Cochraneda29772013-07-19 19:40:10 +02001119
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +00001120 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +08001121 * for indefinite time.
1122 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +00001123 skb_orphan(skb);
1124
Eric Dumazetf8af75f2013-03-06 11:02:37 +00001125 nf_reset(skb);
1126
Jason Wang5990a302018-01-04 11:14:27 +08001127 if (ptr_ring_produce(&tfile->tx_ring, skb))
Jason Wang1576d982016-06-30 14:45:36 +08001128 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +00001131 if (tfile->flags & TUN_FASYNC)
1132 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -07001133 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +00001134
1135 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +00001136 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138drop:
Paolo Abeni608b9972016-04-13 10:52:20 +02001139 this_cpu_inc(tun->pcpu_stats->tx_dropped);
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +00001140 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +00001142 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +08001143 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001146static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001148 /*
1149 * This callback is supposed to deal with mc filter in
1150 * _rx_ path and has nothing to do with the _tx_ path.
1151 * In rx path we always accept everything userspace gives us.
1152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001155static netdev_features_t tun_net_fix_features(struct net_device *dev,
1156 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +00001157{
1158 struct tun_struct *tun = netdev_priv(dev);
1159
1160 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1161}
Paolo Abenieaea34b2016-02-26 10:45:40 +01001162
1163static void tun_set_headroom(struct net_device *dev, int new_hr)
1164{
1165 struct tun_struct *tun = netdev_priv(dev);
1166
1167 if (new_hr < NET_SKB_PAD)
1168 new_hr = NET_SKB_PAD;
1169
1170 tun->align = new_hr;
1171}
1172
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001173static void
Paolo Abeni608b9972016-04-13 10:52:20 +02001174tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1175{
1176 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
1177 struct tun_struct *tun = netdev_priv(dev);
1178 struct tun_pcpu_stats *p;
1179 int i;
1180
1181 for_each_possible_cpu(i) {
1182 u64 rxpackets, rxbytes, txpackets, txbytes;
1183 unsigned int start;
1184
1185 p = per_cpu_ptr(tun->pcpu_stats, i);
1186 do {
1187 start = u64_stats_fetch_begin(&p->syncp);
1188 rxpackets = p->rx_packets;
1189 rxbytes = p->rx_bytes;
1190 txpackets = p->tx_packets;
1191 txbytes = p->tx_bytes;
1192 } while (u64_stats_fetch_retry(&p->syncp, start));
1193
1194 stats->rx_packets += rxpackets;
1195 stats->rx_bytes += rxbytes;
1196 stats->tx_packets += txpackets;
1197 stats->tx_bytes += txbytes;
1198
1199 /* u32 counters */
1200 rx_dropped += p->rx_dropped;
1201 rx_frame_errors += p->rx_frame_errors;
1202 tx_dropped += p->tx_dropped;
1203 }
1204 stats->rx_dropped = rx_dropped;
1205 stats->rx_frame_errors = rx_frame_errors;
1206 stats->tx_dropped = tx_dropped;
Paolo Abeni608b9972016-04-13 10:52:20 +02001207}
1208
Jason Wang761876c2017-08-11 19:41:18 +08001209static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1210 struct netlink_ext_ack *extack)
1211{
1212 struct tun_struct *tun = netdev_priv(dev);
Jason Wange4a2a302018-09-12 11:16:59 +08001213 struct tun_file *tfile;
Jason Wang761876c2017-08-11 19:41:18 +08001214 struct bpf_prog *old_prog;
Jason Wange4a2a302018-09-12 11:16:59 +08001215 int i;
Jason Wang761876c2017-08-11 19:41:18 +08001216
1217 old_prog = rtnl_dereference(tun->xdp_prog);
1218 rcu_assign_pointer(tun->xdp_prog, prog);
1219 if (old_prog)
1220 bpf_prog_put(old_prog);
1221
Jason Wange4a2a302018-09-12 11:16:59 +08001222 for (i = 0; i < tun->numqueues; i++) {
1223 tfile = rtnl_dereference(tun->tfiles[i]);
1224 if (prog)
1225 sock_set_flag(&tfile->sk, SOCK_XDP);
1226 else
1227 sock_reset_flag(&tfile->sk, SOCK_XDP);
1228 }
1229 list_for_each_entry(tfile, &tun->disabled, next) {
1230 if (prog)
1231 sock_set_flag(&tfile->sk, SOCK_XDP);
1232 else
1233 sock_reset_flag(&tfile->sk, SOCK_XDP);
1234 }
1235
Jason Wang761876c2017-08-11 19:41:18 +08001236 return 0;
1237}
1238
1239static u32 tun_xdp_query(struct net_device *dev)
1240{
1241 struct tun_struct *tun = netdev_priv(dev);
1242 const struct bpf_prog *xdp_prog;
1243
1244 xdp_prog = rtnl_dereference(tun->xdp_prog);
1245 if (xdp_prog)
1246 return xdp_prog->aux->id;
1247
1248 return 0;
1249}
1250
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001251static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
Jason Wang761876c2017-08-11 19:41:18 +08001252{
1253 switch (xdp->command) {
1254 case XDP_SETUP_PROG:
1255 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1256 case XDP_QUERY_PROG:
1257 xdp->prog_id = tun_xdp_query(dev);
Jason Wang761876c2017-08-11 19:41:18 +08001258 return 0;
1259 default:
1260 return -EINVAL;
1261 }
1262}
1263
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001264static int tun_net_change_carrier(struct net_device *dev, bool new_carrier)
1265{
1266 if (new_carrier) {
1267 struct tun_struct *tun = netdev_priv(dev);
1268
1269 if (!tun->numqueues)
1270 return -EPERM;
1271
1272 netif_carrier_on(dev);
1273 } else {
1274 netif_carrier_off(dev);
1275 }
1276 return 0;
1277}
1278
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001279static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001280 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001281 .ndo_open = tun_net_open,
1282 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001283 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001284 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +00001285 .ndo_select_queue = tun_select_queue,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001286 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001287 .ndo_get_stats64 = tun_net_get_stats64,
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001288 .ndo_change_carrier = tun_net_change_carrier,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001289};
1290
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001291static void __tun_xdp_flush_tfile(struct tun_file *tfile)
1292{
1293 /* Notify and wake up reader process */
1294 if (tfile->flags & TUN_FASYNC)
1295 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
1296 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
1297}
1298
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02001299static int tun_xdp_xmit(struct net_device *dev, int n,
1300 struct xdp_frame **frames, u32 flags)
Jason Wangfc72d1d2018-01-04 11:14:28 +08001301{
1302 struct tun_struct *tun = netdev_priv(dev);
Jason Wangfc72d1d2018-01-04 11:14:28 +08001303 struct tun_file *tfile;
1304 u32 numqueues;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001305 int drops = 0;
1306 int cnt = n;
1307 int i;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001308
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001309 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02001310 return -EINVAL;
1311
Jason Wangfc72d1d2018-01-04 11:14:28 +08001312 rcu_read_lock();
1313
Jason Wang9871a9e2019-05-08 23:20:18 -04001314resample:
Jason Wangfc72d1d2018-01-04 11:14:28 +08001315 numqueues = READ_ONCE(tun->numqueues);
1316 if (!numqueues) {
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001317 rcu_read_unlock();
1318 return -ENXIO; /* Caller will free/return all frames */
Jason Wangfc72d1d2018-01-04 11:14:28 +08001319 }
1320
1321 tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
1322 numqueues]);
Jason Wang9871a9e2019-05-08 23:20:18 -04001323 if (unlikely(!tfile))
1324 goto resample;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001325
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001326 spin_lock(&tfile->tx_ring.producer_lock);
1327 for (i = 0; i < n; i++) {
1328 struct xdp_frame *xdp = frames[i];
1329 /* Encode the XDP flag into lowest bit for consumer to differ
1330 * XDP buffer from sk_buff.
1331 */
1332 void *frame = tun_xdp_to_ptr(xdp);
1333
1334 if (__ptr_ring_produce(&tfile->tx_ring, frame)) {
1335 this_cpu_inc(tun->pcpu_stats->tx_dropped);
1336 xdp_return_frame_rx_napi(xdp);
1337 drops++;
1338 }
1339 }
1340 spin_unlock(&tfile->tx_ring.producer_lock);
1341
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001342 if (flags & XDP_XMIT_FLUSH)
1343 __tun_xdp_flush_tfile(tfile);
1344
Jason Wangfc72d1d2018-01-04 11:14:28 +08001345 rcu_read_unlock();
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001346 return cnt - drops;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001347}
1348
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02001349static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
1350{
1351 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
1352
1353 if (unlikely(!frame))
1354 return -EOVERFLOW;
1355
Jesper Dangaard Brouer42421a52018-06-05 13:55:45 +02001356 return tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH);
Jason Wangfc72d1d2018-01-04 11:14:28 +08001357}
1358
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001359static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001360 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001361 .ndo_open = tun_net_open,
1362 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001363 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001364 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001365 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001366 .ndo_set_mac_address = eth_mac_addr,
1367 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +00001368 .ndo_select_queue = tun_select_queue,
Toshiaki Makita5e527962015-07-31 15:03:27 +09001369 .ndo_features_check = passthru_features_check,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001370 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001371 .ndo_get_stats64 = tun_net_get_stats64,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001372 .ndo_bpf = tun_xdp,
Jason Wangfc72d1d2018-01-04 11:14:28 +08001373 .ndo_xdp_xmit = tun_xdp_xmit,
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001374 .ndo_change_carrier = tun_net_change_carrier,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001375};
1376
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001377static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +00001378{
1379 int i;
1380
Jason Wang96442e422012-10-31 19:46:02 +00001381 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1382 INIT_HLIST_HEAD(&tun->flows[i]);
1383
1384 tun->ageing_time = TUN_FLOW_EXPIRE;
Kees Cooke99e88a2017-10-16 14:43:17 -07001385 timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0);
1386 mod_timer(&tun->flow_gc_timer,
1387 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +00001388}
1389
1390static void tun_flow_uninit(struct tun_struct *tun)
1391{
1392 del_timer_sync(&tun->flow_gc_timer);
1393 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001394}
1395
Jarod Wilson91572082016-10-20 13:55:20 -04001396#define MIN_MTU 68
1397#define MAX_MTU 65535
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/* Initialize net device. */
1400static void tun_net_init(struct net_device *dev)
1401{
1402 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001405 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001406 dev->netdev_ops = &tun_netdev_ops;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /* Point-to-Point TUN Device */
1409 dev->hard_header_len = 0;
1410 dev->addr_len = 0;
1411 dev->mtu = 1500;
1412
1413 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001414 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 break;
1417
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001418 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -08001419 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001421 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +00001422 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +00001423 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001425 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428 }
Jarod Wilson91572082016-10-20 13:55:20 -04001429
1430 dev->min_mtu = MIN_MTU;
1431 dev->max_mtu = MAX_MTU - dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Jason Wang2f3ab622018-05-22 14:21:04 +08001434static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile)
1435{
1436 struct sock *sk = tfile->socket.sk;
1437
1438 return (tun->dev->flags & IFF_UP) && sock_writeable(sk);
1439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441/* Character device part */
1442
1443/* Poll */
Al Viroafc9a422017-07-03 06:39:46 -04001444static __poll_t tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001445{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +00001446 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001447 struct tun_struct *tun = tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001448 struct sock *sk;
Al Viroafc9a422017-07-03 06:39:46 -04001449 __poll_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (!tun)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001452 return EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jason Wang54f968d2012-10-31 19:45:57 +00001454 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001455
Joe Perches6b8a66e2011-03-02 07:18:10 +00001456 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Xi Wang9e641bd2014-05-16 15:11:48 -07001458 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001459
Jason Wang5990a302018-01-04 11:14:27 +08001460 if (!ptr_ring_empty(&tfile->tx_ring))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001461 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Jason Wang2f3ab622018-05-22 14:21:04 +08001463 /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to
1464 * guarantee EPOLLOUT to be raised by either here or
1465 * tun_sock_write_space(). Then process could get notification
1466 * after it writes to a down device and meets -EIO.
1467 */
1468 if (tun_sock_writeable(tun, tfile) ||
1469 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1470 tun_sock_writeable(tun, tfile)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001471 mask |= EPOLLOUT | EPOLLWRNORM;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001472
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001473 if (tun->dev->reg_state != NETREG_REGISTERED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001474 mask = EPOLLERR;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001475
Eric W. Biederman631ab462009-01-20 11:00:40 +00001476 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return mask;
1478}
1479
Petar Penkov90e33d42017-09-22 13:49:15 -07001480static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
1481 size_t len,
1482 const struct iov_iter *it)
1483{
1484 struct sk_buff *skb;
1485 size_t linear;
1486 int err;
1487 int i;
1488
1489 if (it->nr_segs > MAX_SKB_FRAGS + 1)
1490 return ERR_PTR(-ENOMEM);
1491
1492 local_bh_disable();
1493 skb = napi_get_frags(&tfile->napi);
1494 local_bh_enable();
1495 if (!skb)
1496 return ERR_PTR(-ENOMEM);
1497
1498 linear = iov_iter_single_seg_count(it);
1499 err = __skb_grow(skb, linear);
1500 if (err)
1501 goto free;
1502
1503 skb->len = len;
1504 skb->data_len = len - linear;
1505 skb->truesize += skb->data_len;
1506
1507 for (i = 1; i < it->nr_segs; i++) {
1508 size_t fragsz = it->iov[i].iov_len;
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001509 struct page *page;
1510 void *frag;
Petar Penkov90e33d42017-09-22 13:49:15 -07001511
1512 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1513 err = -EINVAL;
1514 goto free;
1515 }
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001516 frag = netdev_alloc_frag(fragsz);
1517 if (!frag) {
Petar Penkov90e33d42017-09-22 13:49:15 -07001518 err = -ENOMEM;
1519 goto free;
1520 }
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001521 page = virt_to_head_page(frag);
1522 skb_fill_page_desc(skb, i - 1, page,
1523 frag - page_address(page), fragsz);
Petar Penkov90e33d42017-09-22 13:49:15 -07001524 }
1525
1526 return skb;
1527free:
1528 /* frees skb and all frags allocated with napi_alloc_frag() */
1529 napi_free_frags(&tfile->napi);
1530 return ERR_PTR(err);
1531}
1532
Rusty Russellf42157c2008-08-15 15:15:10 -07001533/* prepad is the amount to reserve at front. len is length after that.
1534 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001535static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001536 size_t prepad, size_t len,
1537 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001538{
Jason Wang54f968d2012-10-31 19:45:57 +00001539 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001540 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001541 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001542
1543 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001544 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001545 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001546
Herbert Xu33dccbb2009-02-05 21:25:32 -08001547 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001548 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001549 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001550 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001551
1552 skb_reserve(skb, prepad);
1553 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001554 skb->data_len = len - linear;
1555 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001556
1557 return skb;
1558}
1559
Jason Wang5503fce2017-01-18 15:02:03 +08001560static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1561 struct sk_buff *skb, int more)
1562{
1563 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1564 struct sk_buff_head process_queue;
1565 u32 rx_batched = tun->rx_batched;
1566 bool rcv = false;
1567
1568 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1569 local_bh_disable();
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001570 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001571 netif_receive_skb(skb);
1572 local_bh_enable();
1573 return;
1574 }
1575
1576 spin_lock(&queue->lock);
1577 if (!more || skb_queue_len(queue) == rx_batched) {
1578 __skb_queue_head_init(&process_queue);
1579 skb_queue_splice_tail_init(queue, &process_queue);
1580 rcv = true;
1581 } else {
1582 __skb_queue_tail(queue, skb);
1583 }
1584 spin_unlock(&queue->lock);
1585
1586 if (rcv) {
1587 struct sk_buff *nskb;
1588
1589 local_bh_disable();
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001590 while ((nskb = __skb_dequeue(&process_queue))) {
1591 skb_record_rx_queue(nskb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001592 netif_receive_skb(nskb);
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001593 }
1594 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001595 netif_receive_skb(skb);
1596 local_bh_enable();
1597 }
1598}
1599
Jason Wang66ccbc92017-08-11 19:41:16 +08001600static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1601 int len, int noblock, bool zerocopy)
1602{
1603 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1604 return false;
1605
1606 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1607 return false;
1608
1609 if (!noblock)
1610 return false;
1611
1612 if (zerocopy)
1613 return false;
1614
1615 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1616 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1617 return false;
1618
1619 return true;
1620}
1621
Jason Wangac1f1f62018-09-12 11:17:03 +08001622static struct sk_buff *__tun_build_skb(struct page_frag *alloc_frag, char *buf,
Jason Wang8ae1aff2018-09-12 11:17:04 +08001623 int buflen, int len, int pad)
Jason Wangac1f1f62018-09-12 11:17:03 +08001624{
1625 struct sk_buff *skb = build_skb(buf, buflen);
1626
1627 if (!skb)
1628 return ERR_PTR(-ENOMEM);
1629
Jason Wang8ae1aff2018-09-12 11:17:04 +08001630 skb_reserve(skb, pad);
Jason Wangac1f1f62018-09-12 11:17:03 +08001631 skb_put(skb, len);
1632
1633 get_page(alloc_frag->page);
1634 alloc_frag->offset += buflen;
1635
1636 return skb;
1637}
1638
Jason Wang8ae1aff2018-09-12 11:17:04 +08001639static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
1640 struct xdp_buff *xdp, u32 act)
1641{
1642 int err;
1643
1644 switch (act) {
1645 case XDP_REDIRECT:
1646 err = xdp_do_redirect(tun->dev, xdp, xdp_prog);
Jason Wang8ae1aff2018-09-12 11:17:04 +08001647 if (err)
1648 return err;
1649 break;
1650 case XDP_TX:
1651 err = tun_xdp_tx(tun->dev, xdp);
1652 if (err < 0)
1653 return err;
1654 break;
1655 case XDP_PASS:
1656 break;
1657 default:
1658 bpf_warn_invalid_xdp_action(act);
1659 /* fall through */
1660 case XDP_ABORTED:
1661 trace_xdp_exception(tun->dev, xdp_prog, act);
1662 /* fall through */
1663 case XDP_DROP:
1664 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1665 break;
1666 }
1667
1668 return act;
1669}
1670
Jason Wang761876c2017-08-11 19:41:18 +08001671static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1672 struct tun_file *tfile,
Jason Wang66ccbc92017-08-11 19:41:16 +08001673 struct iov_iter *from,
Jason Wang761876c2017-08-11 19:41:18 +08001674 struct virtio_net_hdr *hdr,
Jason Wang1cfe6e92017-09-04 11:36:09 +08001675 int len, int *skb_xdp)
Jason Wang66ccbc92017-08-11 19:41:16 +08001676{
Eric Dumazet0bbd7da2017-08-16 22:14:33 +08001677 struct page_frag *alloc_frag = &current->task_frag;
Jason Wang761876c2017-08-11 19:41:18 +08001678 struct bpf_prog *xdp_prog;
Jason Wang7df13212017-09-04 11:36:08 +08001679 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jason Wang66ccbc92017-08-11 19:41:16 +08001680 char *buf;
1681 size_t copied;
Jason Wang8ae1aff2018-09-12 11:17:04 +08001682 int pad = TUN_RX_PAD;
1683 int err = 0;
Jason Wang7df13212017-09-04 11:36:08 +08001684
1685 rcu_read_lock();
1686 xdp_prog = rcu_dereference(tun->xdp_prog);
1687 if (xdp_prog)
Jason Wang4f23aff2018-09-12 11:17:00 +08001688 pad += XDP_PACKET_HEADROOM;
Jason Wang7df13212017-09-04 11:36:08 +08001689 buflen += SKB_DATA_ALIGN(len + pad);
1690 rcu_read_unlock();
Jason Wang66ccbc92017-08-11 19:41:16 +08001691
Jason Wang63b9ab62017-10-27 11:05:44 +08001692 alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
Jason Wang66ccbc92017-08-11 19:41:16 +08001693 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1694 return ERR_PTR(-ENOMEM);
1695
1696 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1697 copied = copy_page_from_iter(alloc_frag->page,
Jason Wang7df13212017-09-04 11:36:08 +08001698 alloc_frag->offset + pad,
Jason Wang66ccbc92017-08-11 19:41:16 +08001699 len, from);
1700 if (copied != len)
1701 return ERR_PTR(-EFAULT);
1702
Jason Wang7df13212017-09-04 11:36:08 +08001703 /* There's a small window that XDP may be set after the check
1704 * of xdp_prog above, this should be rare and for simplicity
1705 * we do XDP on skb in case the headroom is not enough.
1706 */
Jason Wangac1f1f62018-09-12 11:17:03 +08001707 if (hdr->gso_type || !xdp_prog) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001708 *skb_xdp = 1;
Jason Wang8ae1aff2018-09-12 11:17:04 +08001709 return __tun_build_skb(alloc_frag, buf, buflen, len, pad);
Jason Wangac1f1f62018-09-12 11:17:03 +08001710 }
1711
1712 *skb_xdp = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001713
Toshiaki Makita6547e382018-05-28 19:37:49 +09001714 local_bh_disable();
Jason Wang761876c2017-08-11 19:41:18 +08001715 rcu_read_lock();
1716 xdp_prog = rcu_dereference(tun->xdp_prog);
Jason Wang8ae1aff2018-09-12 11:17:04 +08001717 if (xdp_prog) {
Jason Wang761876c2017-08-11 19:41:18 +08001718 struct xdp_buff xdp;
Jason Wang761876c2017-08-11 19:41:18 +08001719 u32 act;
1720
1721 xdp.data_hard_start = buf;
Jason Wang7df13212017-09-04 11:36:08 +08001722 xdp.data = buf + pad;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001723 xdp_set_data_meta_invalid(&xdp);
Jason Wang761876c2017-08-11 19:41:18 +08001724 xdp.data_end = xdp.data + len;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +01001725 xdp.rxq = &tfile->xdp_rxq;
Jason Wang761876c2017-08-11 19:41:18 +08001726
Jason Wang8ae1aff2018-09-12 11:17:04 +08001727 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1728 if (act == XDP_REDIRECT || act == XDP_TX) {
Jason Wang761876c2017-08-11 19:41:18 +08001729 get_page(alloc_frag->page);
1730 alloc_frag->offset += buflen;
Jason Wang761876c2017-08-11 19:41:18 +08001731 }
Jason Wang8ae1aff2018-09-12 11:17:04 +08001732 err = tun_xdp_act(tun, xdp_prog, &xdp, act);
1733 if (err < 0)
1734 goto err_xdp;
Jason Wang1a097912018-09-12 11:17:05 +08001735 if (err == XDP_REDIRECT)
1736 xdp_do_flush_map();
Jason Wang8ae1aff2018-09-12 11:17:04 +08001737 if (err != XDP_PASS)
1738 goto out;
1739
1740 pad = xdp.data - xdp.data_hard_start;
1741 len = xdp.data_end - xdp.data;
Jason Wang761876c2017-08-11 19:41:18 +08001742 }
Jason Wang291aeb22018-09-12 11:17:01 +08001743 rcu_read_unlock();
1744 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001745
Jason Wang8ae1aff2018-09-12 11:17:04 +08001746 return __tun_build_skb(alloc_frag, buf, buflen, len, pad);
Jason Wang761876c2017-08-11 19:41:18 +08001747
Jason Wang8ae1aff2018-09-12 11:17:04 +08001748err_xdp:
Jason Wang761876c2017-08-11 19:41:18 +08001749 put_page(alloc_frag->page);
Jason Wangf7053b62018-09-12 11:17:02 +08001750out:
Jason Wang761876c2017-08-11 19:41:18 +08001751 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001752 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001753 return NULL;
Jason Wang66ccbc92017-08-11 19:41:16 +08001754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001757static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001758 void *msg_control, struct iov_iter *from,
Jason Wang5503fce2017-01-18 15:02:03 +08001759 int noblock, bool more)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Harvey Harrison09640e632009-02-01 00:45:17 -08001761 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001763 size_t total_len = iov_iter_count(from);
Paolo Abenieaea34b2016-02-26 10:45:40 +01001764 size_t len = total_len, align = tun->align, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001765 struct virtio_net_hdr gso = { 0 };
Paolo Abeni608b9972016-04-13 10:52:20 +02001766 struct tun_pcpu_stats *stats;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001767 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001768 int copylen;
1769 bool zerocopy = false;
1770 int err;
Jason Wang96f84062017-12-04 17:31:23 +08001771 u32 rxhash = 0;
Jason Wang1cfe6e92017-09-04 11:36:09 +08001772 int skb_xdp = 1;
Eric Dumazetaf3fb242018-09-28 14:51:49 -07001773 bool frags = tun_napi_frags_enabled(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001775 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001776 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001778 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Al Virocbbd26b2016-11-01 22:09:04 -04001780 if (!copy_from_iter_full(&pi, sizeof(pi), from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return -EFAULT;
1782 }
1783
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001784 if (tun->flags & IFF_VNET_HDR) {
Willem de Bruijne1edab82017-02-03 18:20:48 -05001785 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1786
1787 if (len < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001788 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001789 len -= vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001790
Al Virocbbd26b2016-11-01 22:09:04 -04001791 if (!copy_from_iter_full(&gso, sizeof(gso), from))
Rusty Russellf43798c2008-07-03 03:48:02 -07001792 return -EFAULT;
1793
Herbert Xu49091222009-06-08 00:20:01 -07001794 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001795 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1796 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 -07001797
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001798 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001799 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001800 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001801 }
1802
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001803 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001804 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001805 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001806 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001807 return -EINVAL;
1808 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001809
Jason Wang96f8d9e2013-11-13 14:00:39 +08001810 good_linear = SKB_MAX_HEAD(align);
1811
Jason Wang88529172013-07-18 10:55:15 +08001812 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001813 struct iov_iter i = *from;
1814
Jason Wang88529172013-07-18 10:55:15 +08001815 /* There are 256 bytes to be copied in skb, so there is
1816 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001817 * The rest of the buffer is mapped from userspace.
1818 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001819 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001820 if (copylen > good_linear)
1821 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001822 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001823 iov_iter_advance(&i, copylen);
1824 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001825 zerocopy = true;
1826 }
1827
Petar Penkov90e33d42017-09-22 13:49:15 -07001828 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001829 /* For the packet that is not easy to be processed
1830 * (e.g gso or jumbo packet), we will do it at after
1831 * skb was created with generic XDP routine.
1832 */
1833 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
Jason Wang66ccbc92017-08-11 19:41:16 +08001834 if (IS_ERR(skb)) {
Paolo Abeni608b9972016-04-13 10:52:20 +02001835 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001836 return PTR_ERR(skb);
1837 }
Jason Wang761876c2017-08-11 19:41:18 +08001838 if (!skb)
1839 return total_len;
Jason Wang66ccbc92017-08-11 19:41:16 +08001840 } else {
1841 if (!zerocopy) {
1842 copylen = len;
1843 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1844 linear = good_linear;
1845 else
1846 linear = tun16_to_cpu(tun, gso.hdr_len);
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Petar Penkov90e33d42017-09-22 13:49:15 -07001849 if (frags) {
1850 mutex_lock(&tfile->napi_mutex);
1851 skb = tun_napi_alloc_frags(tfile, copylen, from);
1852 /* tun_napi_alloc_frags() enforces a layout for the skb.
1853 * If zerocopy is enabled, then this layout will be
1854 * overwritten by zerocopy_sg_from_iter().
1855 */
1856 zerocopy = false;
1857 } else {
1858 skb = tun_alloc_skb(tfile, align, copylen, linear,
1859 noblock);
1860 }
1861
Jason Wang66ccbc92017-08-11 19:41:16 +08001862 if (IS_ERR(skb)) {
1863 if (PTR_ERR(skb) != -EAGAIN)
1864 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Petar Penkov90e33d42017-09-22 13:49:15 -07001865 if (frags)
1866 mutex_unlock(&tfile->napi_mutex);
Jason Wang66ccbc92017-08-11 19:41:16 +08001867 return PTR_ERR(skb);
1868 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001869
Jason Wang66ccbc92017-08-11 19:41:16 +08001870 if (zerocopy)
1871 err = zerocopy_sg_from_iter(skb, from);
1872 else
1873 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1874
1875 if (err) {
Eric Dumazet44771382019-03-14 20:19:47 -07001876 err = -EFAULT;
1877drop:
Jason Wang66ccbc92017-08-11 19:41:16 +08001878 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1879 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001880 if (frags) {
1881 tfile->napi.skb = NULL;
1882 mutex_unlock(&tfile->napi_mutex);
1883 }
1884
Eric Dumazet44771382019-03-14 20:19:47 -07001885 return err;
Jason Wang66ccbc92017-08-11 19:41:16 +08001886 }
Dave Jones8f227572006-03-11 18:49:13 -08001887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001889 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
Paolo Abenidf10db92016-06-14 00:00:04 +02001890 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1891 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001892 if (frags) {
1893 tfile->napi.skb = NULL;
1894 mutex_unlock(&tfile->napi_mutex);
1895 }
1896
Paolo Abenidf10db92016-06-14 00:00:04 +02001897 return -EINVAL;
1898 }
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001901 case IFF_TUN:
1902 if (tun->flags & IFF_NO_PI) {
Alexander Potapenko2580c4c2017-09-28 11:32:37 +02001903 u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
1904
1905 switch (ip_version) {
1906 case 4:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001907 pi.proto = htons(ETH_P_IP);
1908 break;
Alexander Potapenko2580c4c2017-09-28 11:32:37 +02001909 case 6:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001910 pi.proto = htons(ETH_P_IPV6);
1911 break;
1912 default:
Paolo Abeni608b9972016-04-13 10:52:20 +02001913 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001914 kfree_skb(skb);
1915 return -EINVAL;
1916 }
1917 }
1918
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001919 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001921 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001923 case IFF_TAP:
Petar Penkov90e33d42017-09-22 13:49:15 -07001924 if (!frags)
1925 skb->protocol = eth_type_trans(skb, tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001929 /* copy skb_ubuf_info for callback when skb has no error */
1930 if (zerocopy) {
1931 skb_shinfo(skb)->destructor_arg = msg_control;
1932 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001933 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangaf1cc7a2016-11-30 13:17:51 +08001934 } else if (msg_control) {
1935 struct ubuf_info *uarg = msg_control;
1936 uarg->callback(uarg, false);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001937 }
1938
Vlad Yasevich72f65102015-02-03 16:36:16 -05001939 skb_reset_network_header(skb);
Maxim Mikityanskiyd2aa1252019-02-21 12:39:57 +00001940 skb_probe_transport_header(skb);
Jason Wang38502af2013-03-25 20:19:56 +00001941
Jason Wang1cfe6e92017-09-04 11:36:09 +08001942 if (skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001943 struct bpf_prog *xdp_prog;
1944 int ret;
1945
Toshiaki Makita6547e382018-05-28 19:37:49 +09001946 local_bh_disable();
Jason Wang761876c2017-08-11 19:41:18 +08001947 rcu_read_lock();
1948 xdp_prog = rcu_dereference(tun->xdp_prog);
1949 if (xdp_prog) {
1950 ret = do_xdp_generic(xdp_prog, skb);
1951 if (ret != XDP_PASS) {
1952 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001953 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001954 return total_len;
1955 }
1956 }
1957 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001958 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001959 }
1960
Paolo Abenicf1a1e02018-04-20 13:18:16 +02001961 /* Compute the costly rx hash only if needed for flow updates.
1962 * We may get a very small possibility of OOO during switching, not
1963 * worth to optimize.
1964 */
1965 if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 &&
1966 !tfile->detached)
Jason Wang96f84062017-12-04 17:31:23 +08001967 rxhash = __skb_get_hash_symmetric(skb);
Petar Penkov94317092017-09-22 13:49:14 -07001968
Eric Dumazet44771382019-03-14 20:19:47 -07001969 rcu_read_lock();
1970 if (unlikely(!(tun->dev->flags & IFF_UP))) {
1971 err = -EIO;
Eric Dumazet9180bb42019-03-16 13:09:53 -07001972 rcu_read_unlock();
Eric Dumazet44771382019-03-14 20:19:47 -07001973 goto drop;
1974 }
1975
Petar Penkov90e33d42017-09-22 13:49:15 -07001976 if (frags) {
1977 /* Exercise flow dissector code path. */
Stanislav Fomichevc43f1252019-04-22 08:55:48 -07001978 u32 headlen = eth_get_headlen(tun->dev, skb->data,
1979 skb_headlen(skb));
Petar Penkov90e33d42017-09-22 13:49:15 -07001980
Eric Dumazet010f2452017-10-17 10:07:44 -07001981 if (unlikely(headlen > skb_headlen(skb))) {
Petar Penkov90e33d42017-09-22 13:49:15 -07001982 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1983 napi_free_frags(&tfile->napi);
Eric Dumazet44771382019-03-14 20:19:47 -07001984 rcu_read_unlock();
Petar Penkov90e33d42017-09-22 13:49:15 -07001985 mutex_unlock(&tfile->napi_mutex);
1986 WARN_ON(1);
1987 return -ENOMEM;
1988 }
1989
1990 local_bh_disable();
1991 napi_gro_frags(&tfile->napi);
1992 local_bh_enable();
1993 mutex_unlock(&tfile->napi_mutex);
Eric Dumazetaec72f32017-10-18 12:12:09 -07001994 } else if (tfile->napi_enabled) {
Petar Penkov94317092017-09-22 13:49:14 -07001995 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1996 int queue_len;
1997
1998 spin_lock_bh(&queue->lock);
1999 __skb_queue_tail(queue, skb);
2000 queue_len = skb_queue_len(queue);
2001 spin_unlock(&queue->lock);
2002
2003 if (!more || queue_len > NAPI_POLL_WEIGHT)
2004 napi_schedule(&tfile->napi);
2005
2006 local_bh_enable();
2007 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
2008 tun_rx_batched(tun, tfile, skb, more);
2009 } else {
2010 netif_rx_ni(skb);
2011 }
Eric Dumazet44771382019-03-14 20:19:47 -07002012 rcu_read_unlock();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002013
Paolo Abeni608b9972016-04-13 10:52:20 +02002014 stats = get_cpu_ptr(tun->pcpu_stats);
2015 u64_stats_update_begin(&stats->syncp);
2016 stats->rx_packets++;
2017 stats->rx_bytes += len;
2018 u64_stats_update_end(&stats->syncp);
2019 put_cpu_ptr(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Jason Wang96f84062017-12-04 17:31:23 +08002021 if (rxhash)
2022 tun_flow_update(tun, rxhash, tfile);
2023
Michael S. Tsirkin06908992012-07-20 09:23:23 +00002024 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Al Virof5ff53b2014-06-19 15:36:49 -04002027static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Herbert Xu33dccbb2009-02-05 21:25:32 -08002029 struct file *file = iocb->ki_filp;
Jason Wang54f968d2012-10-31 19:45:57 +00002030 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08002031 struct tun_struct *tun = tun_get(tfile);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002032 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 if (!tun)
2035 return -EBADFD;
2036
Jason Wang5503fce2017-01-18 15:02:03 +08002037 result = tun_get_user(tun, tfile, NULL, from,
2038 file->f_flags & O_NONBLOCK, false);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002039
2040 tun_put(tun);
2041 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Jason Wangfc72d1d2018-01-04 11:14:28 +08002044static ssize_t tun_put_user_xdp(struct tun_struct *tun,
2045 struct tun_file *tfile,
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002046 struct xdp_frame *xdp_frame,
Jason Wangfc72d1d2018-01-04 11:14:28 +08002047 struct iov_iter *iter)
2048{
2049 int vnet_hdr_sz = 0;
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002050 size_t size = xdp_frame->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002051 struct tun_pcpu_stats *stats;
2052 size_t ret;
2053
2054 if (tun->flags & IFF_VNET_HDR) {
2055 struct virtio_net_hdr gso = { 0 };
2056
2057 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
2058 if (unlikely(iov_iter_count(iter) < vnet_hdr_sz))
2059 return -EINVAL;
2060 if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) !=
2061 sizeof(gso)))
2062 return -EFAULT;
2063 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
2064 }
2065
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002066 ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002067
2068 stats = get_cpu_ptr(tun->pcpu_stats);
2069 u64_stats_update_begin(&stats->syncp);
2070 stats->tx_packets++;
2071 stats->tx_bytes += ret;
2072 u64_stats_update_end(&stats->syncp);
2073 put_cpu_ptr(tun->pcpu_stats);
2074
2075 return ret;
2076}
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00002079static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00002080 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00002081 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08002082 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct tun_pi pi = { 0, skb->protocol };
Paolo Abeni608b9972016-04-13 10:52:20 +02002085 struct tun_pcpu_stats *stats;
Herbert Xue0b46d02014-11-07 21:22:23 +08002086 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08002087 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002088 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08002089 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002090
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002091 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002092 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002094 if (tun->flags & IFF_VNET_HDR)
Willem de Bruijne1edab82017-02-03 18:20:48 -05002095 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Herbert Xue0b46d02014-11-07 21:22:23 +08002097 total = skb->len + vlan_hlen + vnet_hdr_sz;
2098
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002099 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08002100 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return -EINVAL;
2102
Herbert Xue0b46d02014-11-07 21:22:23 +08002103 total += sizeof(pi);
2104 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /* Packet will be striped */
2106 pi.flags |= TUN_PKT_STRIP;
2107 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002108
Herbert Xue0b46d02014-11-07 21:22:23 +08002109 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Herbert Xu2eb783c2014-11-03 04:30:14 +08002113 if (vnet_hdr_sz) {
Jarno Rajahalme9403cd72016-11-18 15:40:40 -08002114 struct virtio_net_hdr gso;
Mike Rapoport34166092016-06-08 16:09:20 +03002115
Herbert Xue0b46d02014-11-07 21:22:23 +08002116 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07002117 return -EINVAL;
2118
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08002119 if (virtio_net_hdr_from_skb(skb, &gso,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002120 tun_is_little_endian(tun), true,
2121 vlan_hlen)) {
Rusty Russellf43798c2008-07-03 03:48:02 -07002122 struct skb_shared_info *sinfo = skb_shinfo(skb);
Mike Rapoport34166092016-06-08 16:09:20 +03002123 pr_err("unexpected GSO type: "
2124 "0x%x, gso_size %d, hdr_len %d\n",
2125 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
2126 tun16_to_cpu(tun, gso.hdr_len));
2127 print_hex_dump(KERN_ERR, "tun: ",
2128 DUMP_PREFIX_NONE,
2129 16, 1, skb->head,
2130 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
2131 WARN_ON_ONCE(1);
2132 return -EINVAL;
2133 }
Rusty Russellf43798c2008-07-03 03:48:02 -07002134
Herbert Xue0b46d02014-11-07 21:22:23 +08002135 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07002136 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08002137
2138 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07002139 }
2140
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002141 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08002142 int ret;
Jason Wangaff3d702018-01-16 16:31:02 +08002143 struct veth veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Jason Wang6680ec62013-07-25 13:00:33 +08002145 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002146 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Jason Wang6680ec62013-07-25 13:00:33 +08002148 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08002149
Herbert Xue0b46d02014-11-07 21:22:23 +08002150 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
2151 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08002152 goto done;
2153
Herbert Xue0b46d02014-11-07 21:22:23 +08002154 ret = copy_to_iter(&veth, sizeof(veth), iter);
2155 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08002156 goto done;
2157 }
2158
Herbert Xue0b46d02014-11-07 21:22:23 +08002159 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08002160
2161done:
Paolo Abeni608b9972016-04-13 10:52:20 +02002162 /* caller is in process context, */
2163 stats = get_cpu_ptr(tun->pcpu_stats);
2164 u64_stats_update_begin(&stats->syncp);
2165 stats->tx_packets++;
2166 stats->tx_bytes += skb->len + vlan_hlen;
2167 u64_stats_update_end(&stats->syncp);
2168 put_cpu_ptr(tun->pcpu_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 return total;
2171}
2172
Jason Wangfc72d1d2018-01-04 11:14:28 +08002173static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err)
Jason Wang1576d982016-06-30 14:45:36 +08002174{
2175 DECLARE_WAITQUEUE(wait, current);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002176 void *ptr = NULL;
Jason Wangf48cc6b2016-07-04 13:53:38 +08002177 int error = 0;
Jason Wang1576d982016-06-30 14:45:36 +08002178
Jason Wangfc72d1d2018-01-04 11:14:28 +08002179 ptr = ptr_ring_consume(&tfile->tx_ring);
2180 if (ptr)
Jason Wang1576d982016-06-30 14:45:36 +08002181 goto out;
2182 if (noblock) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002183 error = -EAGAIN;
Jason Wang1576d982016-06-30 14:45:36 +08002184 goto out;
2185 }
2186
2187 add_wait_queue(&tfile->wq.wait, &wait);
Jason Wang1576d982016-06-30 14:45:36 +08002188
2189 while (1) {
Timur Celik71828b22019-02-23 12:53:13 +01002190 set_current_state(TASK_INTERRUPTIBLE);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002191 ptr = ptr_ring_consume(&tfile->tx_ring);
2192 if (ptr)
Jason Wang1576d982016-06-30 14:45:36 +08002193 break;
2194 if (signal_pending(current)) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002195 error = -ERESTARTSYS;
Jason Wang1576d982016-06-30 14:45:36 +08002196 break;
2197 }
2198 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002199 error = -EFAULT;
Jason Wang1576d982016-06-30 14:45:36 +08002200 break;
2201 }
2202
2203 schedule();
2204 }
2205
Timur Celikecef67c2019-02-25 21:13:13 +01002206 __set_current_state(TASK_RUNNING);
Jason Wang1576d982016-06-30 14:45:36 +08002207 remove_wait_queue(&tfile->wq.wait, &wait);
2208
2209out:
Jason Wangf48cc6b2016-07-04 13:53:38 +08002210 *err = error;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002211 return ptr;
Jason Wang1576d982016-06-30 14:45:36 +08002212}
2213
Jason Wang54f968d2012-10-31 19:45:57 +00002214static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05002215 struct iov_iter *to,
Jason Wangfc72d1d2018-01-04 11:14:28 +08002216 int noblock, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Al Viro9b067032014-11-07 13:52:07 -05002218 ssize_t ret;
Jason Wang1576d982016-06-30 14:45:36 +08002219 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Rami Rosen3872baf2012-11-25 22:07:41 +00002221 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Wei Xuc33ee152017-12-01 05:10:37 -05002223 if (!iov_iter_count(to)) {
Jason Wangfc72d1d2018-01-04 11:14:28 +08002224 tun_ptr_free(ptr);
Al Viro9b067032014-11-07 13:52:07 -05002225 return 0;
Wei Xuc33ee152017-12-01 05:10:37 -05002226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Jason Wangfc72d1d2018-01-04 11:14:28 +08002228 if (!ptr) {
Jason Wangac77cfd2017-05-17 12:14:43 +08002229 /* Read frames from ring */
Jason Wangfc72d1d2018-01-04 11:14:28 +08002230 ptr = tun_ring_recv(tfile, noblock, &err);
2231 if (!ptr)
Jason Wangac77cfd2017-05-17 12:14:43 +08002232 return err;
2233 }
Herbert Xue0b46d02014-11-07 21:22:23 +08002234
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002235 if (tun_is_xdp_frame(ptr)) {
2236 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002237
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002238 ret = tun_put_user_xdp(tun, tfile, xdpf, to);
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +02002239 xdp_return_frame(xdpf);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002240 } else {
2241 struct sk_buff *skb = ptr;
2242
2243 ret = tun_put_user(tun, tfile, skb, to);
2244 if (unlikely(ret < 0))
2245 kfree_skb(skb);
2246 else
2247 consume_skb(skb);
2248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002250 return ret;
2251}
2252
Al Viro9b067032014-11-07 13:52:07 -05002253static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002254{
2255 struct file *file = iocb->ki_filp;
2256 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08002257 struct tun_struct *tun = tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05002258 ssize_t len = iov_iter_count(to), ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002259
2260 if (!tun)
2261 return -EBADFD;
Jason Wangac77cfd2017-05-17 12:14:43 +08002262 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL);
David S. Miller42404c02013-12-10 22:05:45 -05002263 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08002264 if (ret > 0)
2265 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002266 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return ret;
2268}
2269
Jason Wangcd5681d2018-01-16 16:31:01 +08002270static void tun_prog_free(struct rcu_head *rcu)
Jason Wang96f84062017-12-04 17:31:23 +08002271{
Jason Wangcd5681d2018-01-16 16:31:01 +08002272 struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu);
Jason Wang96f84062017-12-04 17:31:23 +08002273
2274 bpf_prog_destroy(prog->prog);
2275 kfree(prog);
2276}
2277
Jason Wang9d6474e2018-01-22 10:55:38 +08002278static int __tun_set_ebpf(struct tun_struct *tun,
2279 struct tun_prog __rcu **prog_p,
Jason Wangcd5681d2018-01-16 16:31:01 +08002280 struct bpf_prog *prog)
Jason Wang96f84062017-12-04 17:31:23 +08002281{
Jason Wangcd5681d2018-01-16 16:31:01 +08002282 struct tun_prog *old, *new = NULL;
Jason Wang96f84062017-12-04 17:31:23 +08002283
2284 if (prog) {
2285 new = kmalloc(sizeof(*new), GFP_KERNEL);
2286 if (!new)
2287 return -ENOMEM;
2288 new->prog = prog;
2289 }
2290
Jason Wang124da8f2017-12-08 12:02:30 +08002291 spin_lock_bh(&tun->lock);
Jason Wangcd5681d2018-01-16 16:31:01 +08002292 old = rcu_dereference_protected(*prog_p,
Jason Wang124da8f2017-12-08 12:02:30 +08002293 lockdep_is_held(&tun->lock));
Jason Wangcd5681d2018-01-16 16:31:01 +08002294 rcu_assign_pointer(*prog_p, new);
Jason Wang124da8f2017-12-08 12:02:30 +08002295 spin_unlock_bh(&tun->lock);
Jason Wang96f84062017-12-04 17:31:23 +08002296
2297 if (old)
Jason Wangcd5681d2018-01-16 16:31:01 +08002298 call_rcu(&old->rcu, tun_prog_free);
Jason Wang96f84062017-12-04 17:31:23 +08002299
2300 return 0;
2301}
2302
Jason Wang96442e422012-10-31 19:46:02 +00002303static void tun_free_netdev(struct net_device *dev)
2304{
2305 struct tun_struct *tun = netdev_priv(dev);
2306
Jason Wang4008e972012-12-13 23:53:30 +00002307 BUG_ON(!(list_empty(&tun->disabled)));
Paolo Abeni608b9972016-04-13 10:52:20 +02002308 free_percpu(tun->pcpu_stats);
Jason Wang96442e422012-10-31 19:46:02 +00002309 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00002310 security_tun_dev_free_security(tun->security);
Jason Wangcd5681d2018-01-16 16:31:01 +08002311 __tun_set_ebpf(tun, &tun->steering_prog, NULL);
Jason Wangaff3d702018-01-16 16:31:02 +08002312 __tun_set_ebpf(tun, &tun->filter_prog, NULL);
Jason Wang96442e422012-10-31 19:46:02 +00002313}
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315static void tun_setup(struct net_device *dev)
2316{
2317 struct tun_struct *tun = netdev_priv(dev);
2318
Eric W. Biederman0625c882012-02-07 16:48:55 -08002319 tun->owner = INVALID_UID;
2320 tun->group = INVALID_GID;
Chas Williams4e24f2d2018-06-02 17:49:53 -04002321 tun_default_link_ksettings(dev, &tun->link_ksettings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 dev->ethtool_ops = &tun_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04002324 dev->needs_free_netdev = true;
2325 dev->priv_destructor = tun_free_netdev;
Jason Wang016adb72016-04-08 13:26:48 +08002326 /* We prefer our own queue length */
2327 dev->tx_queue_len = TUN_READQ_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002330/* Trivial set of netlink ops to allow deleting tun or tap
2331 * device with netlink.
2332 */
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002333static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2334 struct netlink_ext_ack *extack)
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002335{
Nicolas Dichtel35b827b2018-11-29 14:45:39 +01002336 NL_SET_ERR_MSG(extack,
2337 "tun/tap creation via rtnetlink is not supported.");
2338 return -EOPNOTSUPP;
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002339}
2340
Sabrina Dubroca1ec010e2018-02-16 11:03:07 +01002341static size_t tun_get_size(const struct net_device *dev)
2342{
2343 BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t));
2344 BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t));
2345
2346 return nla_total_size(sizeof(uid_t)) + /* OWNER */
2347 nla_total_size(sizeof(gid_t)) + /* GROUP */
2348 nla_total_size(sizeof(u8)) + /* TYPE */
2349 nla_total_size(sizeof(u8)) + /* PI */
2350 nla_total_size(sizeof(u8)) + /* VNET_HDR */
2351 nla_total_size(sizeof(u8)) + /* PERSIST */
2352 nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */
2353 nla_total_size(sizeof(u32)) + /* NUM_QUEUES */
2354 nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */
2355 0;
2356}
2357
2358static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
2359{
2360 struct tun_struct *tun = netdev_priv(dev);
2361
2362 if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK))
2363 goto nla_put_failure;
2364 if (uid_valid(tun->owner) &&
2365 nla_put_u32(skb, IFLA_TUN_OWNER,
2366 from_kuid_munged(current_user_ns(), tun->owner)))
2367 goto nla_put_failure;
2368 if (gid_valid(tun->group) &&
2369 nla_put_u32(skb, IFLA_TUN_GROUP,
2370 from_kgid_munged(current_user_ns(), tun->group)))
2371 goto nla_put_failure;
2372 if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI)))
2373 goto nla_put_failure;
2374 if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR)))
2375 goto nla_put_failure;
2376 if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST)))
2377 goto nla_put_failure;
2378 if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE,
2379 !!(tun->flags & IFF_MULTI_QUEUE)))
2380 goto nla_put_failure;
2381 if (tun->flags & IFF_MULTI_QUEUE) {
2382 if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues))
2383 goto nla_put_failure;
2384 if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES,
2385 tun->numdisabled))
2386 goto nla_put_failure;
2387 }
2388
2389 return 0;
2390
2391nla_put_failure:
2392 return -EMSGSIZE;
2393}
2394
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002395static struct rtnl_link_ops tun_link_ops __read_mostly = {
2396 .kind = DRV_NAME,
2397 .priv_size = sizeof(struct tun_struct),
2398 .setup = tun_setup,
2399 .validate = tun_validate,
Sabrina Dubroca1ec010e2018-02-16 11:03:07 +01002400 .get_size = tun_get_size,
2401 .fill_info = tun_fill_info,
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002402};
2403
Herbert Xu33dccbb2009-02-05 21:25:32 -08002404static void tun_sock_write_space(struct sock *sk)
2405{
Jason Wang54f968d2012-10-31 19:45:57 +00002406 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00002407 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002408
2409 if (!sock_writeable(sk))
2410 return;
2411
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002412 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Herbert Xu33dccbb2009-02-05 21:25:32 -08002413 return;
2414
Eric Dumazet43815482010-04-29 11:01:49 +00002415 wqueue = sk_sleep(sk);
2416 if (wqueue && waitqueue_active(wqueue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002417 wake_up_interruptible_sync_poll(wqueue, EPOLLOUT |
2418 EPOLLWRNORM | EPOLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07002419
Jason Wang54f968d2012-10-31 19:45:57 +00002420 tfile = container_of(sk, struct tun_file, sk);
2421 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002422}
2423
Jason Wangf9e06c42018-11-15 17:43:10 +08002424static void tun_put_page(struct tun_page *tpage)
2425{
2426 if (tpage->page)
2427 __page_frag_cache_drain(tpage->page, tpage->count);
2428}
2429
Jason Wang043d2222018-09-12 11:17:07 +08002430static int tun_xdp_one(struct tun_struct *tun,
2431 struct tun_file *tfile,
Jason Wangf9e06c42018-11-15 17:43:10 +08002432 struct xdp_buff *xdp, int *flush,
2433 struct tun_page *tpage)
Jason Wang043d2222018-09-12 11:17:07 +08002434{
Prashant Bhole4e4b08e2018-12-03 18:09:24 +09002435 unsigned int datasize = xdp->data_end - xdp->data;
Jason Wang043d2222018-09-12 11:17:07 +08002436 struct tun_xdp_hdr *hdr = xdp->data_hard_start;
2437 struct virtio_net_hdr *gso = &hdr->gso;
2438 struct tun_pcpu_stats *stats;
2439 struct bpf_prog *xdp_prog;
2440 struct sk_buff *skb = NULL;
2441 u32 rxhash = 0, act;
2442 int buflen = hdr->buflen;
2443 int err = 0;
2444 bool skb_xdp = false;
Jason Wangf9e06c42018-11-15 17:43:10 +08002445 struct page *page;
Jason Wang043d2222018-09-12 11:17:07 +08002446
2447 xdp_prog = rcu_dereference(tun->xdp_prog);
2448 if (xdp_prog) {
2449 if (gso->gso_type) {
2450 skb_xdp = true;
2451 goto build;
2452 }
2453 xdp_set_data_meta_invalid(xdp);
2454 xdp->rxq = &tfile->xdp_rxq;
2455
2456 act = bpf_prog_run_xdp(xdp_prog, xdp);
2457 err = tun_xdp_act(tun, xdp_prog, xdp, act);
2458 if (err < 0) {
2459 put_page(virt_to_head_page(xdp->data));
2460 return err;
2461 }
2462
2463 switch (err) {
2464 case XDP_REDIRECT:
2465 *flush = true;
2466 /* fall through */
2467 case XDP_TX:
2468 return 0;
2469 case XDP_PASS:
2470 break;
2471 default:
Jason Wangf9e06c42018-11-15 17:43:10 +08002472 page = virt_to_head_page(xdp->data);
2473 if (tpage->page == page) {
2474 ++tpage->count;
2475 } else {
2476 tun_put_page(tpage);
2477 tpage->page = page;
2478 tpage->count = 1;
2479 }
Jason Wang043d2222018-09-12 11:17:07 +08002480 return 0;
2481 }
2482 }
2483
2484build:
2485 skb = build_skb(xdp->data_hard_start, buflen);
2486 if (!skb) {
2487 err = -ENOMEM;
2488 goto out;
2489 }
2490
2491 skb_reserve(skb, xdp->data - xdp->data_hard_start);
2492 skb_put(skb, xdp->data_end - xdp->data);
2493
2494 if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) {
2495 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
2496 kfree_skb(skb);
2497 err = -EINVAL;
2498 goto out;
2499 }
2500
2501 skb->protocol = eth_type_trans(skb, tun->dev);
2502 skb_reset_network_header(skb);
Maxim Mikityanskiyd2aa1252019-02-21 12:39:57 +00002503 skb_probe_transport_header(skb);
Jason Wang043d2222018-09-12 11:17:07 +08002504
2505 if (skb_xdp) {
2506 err = do_xdp_generic(xdp_prog, skb);
2507 if (err != XDP_PASS)
2508 goto out;
2509 }
2510
Paolo Abenif29eb2a2018-11-07 10:34:36 +01002511 if (!rcu_dereference(tun->steering_prog) && tun->numqueues > 1 &&
2512 !tfile->detached)
Jason Wang043d2222018-09-12 11:17:07 +08002513 rxhash = __skb_get_hash_symmetric(skb);
2514
Matthew Cover8ebebcb2018-11-18 00:46:00 -07002515 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang043d2222018-09-12 11:17:07 +08002516 netif_receive_skb(skb);
2517
Prashant Bhole6342ca62018-12-11 11:43:07 +09002518 /* No need for get_cpu_ptr() here since this function is
2519 * always called with bh disabled
2520 */
2521 stats = this_cpu_ptr(tun->pcpu_stats);
Jason Wang043d2222018-09-12 11:17:07 +08002522 u64_stats_update_begin(&stats->syncp);
2523 stats->rx_packets++;
Prashant Bhole4e4b08e2018-12-03 18:09:24 +09002524 stats->rx_bytes += datasize;
Jason Wang043d2222018-09-12 11:17:07 +08002525 u64_stats_update_end(&stats->syncp);
Jason Wang043d2222018-09-12 11:17:07 +08002526
2527 if (rxhash)
2528 tun_flow_update(tun, rxhash, tfile);
2529
2530out:
2531 return err;
2532}
2533
Ying Xue1b784142015-03-02 15:37:48 +08002534static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002535{
Jason Wang043d2222018-09-12 11:17:07 +08002536 int ret, i;
Jason Wang54f968d2012-10-31 19:45:57 +00002537 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002538 struct tun_struct *tun = tun_get(tfile);
Jason Wangfe8dd452018-09-12 11:17:06 +08002539 struct tun_msg_ctl *ctl = m->msg_control;
Jason Wang043d2222018-09-12 11:17:07 +08002540 struct xdp_buff *xdp;
Jason Wang54f968d2012-10-31 19:45:57 +00002541
2542 if (!tun)
2543 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04002544
Jason Wang043d2222018-09-12 11:17:07 +08002545 if (ctl && (ctl->type == TUN_MSG_PTR)) {
David S. Miller6f0271d2018-11-17 16:53:46 -08002546 struct tun_page tpage;
Jason Wang043d2222018-09-12 11:17:07 +08002547 int n = ctl->num;
2548 int flush = 0;
2549
David S. Miller6f0271d2018-11-17 16:53:46 -08002550 memset(&tpage, 0, sizeof(tpage));
2551
Jason Wang043d2222018-09-12 11:17:07 +08002552 local_bh_disable();
2553 rcu_read_lock();
2554
2555 for (i = 0; i < n; i++) {
2556 xdp = &((struct xdp_buff *)ctl->ptr)[i];
Jason Wangf9e06c42018-11-15 17:43:10 +08002557 tun_xdp_one(tun, tfile, xdp, &flush, &tpage);
Jason Wang043d2222018-09-12 11:17:07 +08002558 }
2559
2560 if (flush)
2561 xdp_do_flush_map();
2562
2563 rcu_read_unlock();
2564 local_bh_enable();
2565
Jason Wangf9e06c42018-11-15 17:43:10 +08002566 tun_put_page(&tpage);
2567
Jason Wang043d2222018-09-12 11:17:07 +08002568 ret = total_len;
2569 goto out;
2570 }
Jason Wangfe8dd452018-09-12 11:17:06 +08002571
2572 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter,
Jason Wang5503fce2017-01-18 15:02:03 +08002573 m->msg_flags & MSG_DONTWAIT,
2574 m->msg_flags & MSG_MORE);
Jason Wang043d2222018-09-12 11:17:07 +08002575out:
Jason Wang54f968d2012-10-31 19:45:57 +00002576 tun_put(tun);
2577 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002578}
2579
Ying Xue1b784142015-03-02 15:37:48 +08002580static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002581 int flags)
2582{
Jason Wang54f968d2012-10-31 19:45:57 +00002583 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002584 struct tun_struct *tun = tun_get(tfile);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002585 void *ptr = m->msg_control;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002586 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00002587
Wei Xuc33ee152017-12-01 05:10:37 -05002588 if (!tun) {
2589 ret = -EBADFD;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002590 goto out_free;
Wei Xuc33ee152017-12-01 05:10:37 -05002591 }
Jason Wang54f968d2012-10-31 19:45:57 +00002592
Richard Cochraneda29772013-07-19 19:40:10 +02002593 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00002594 ret = -EINVAL;
Wei Xuc33ee152017-12-01 05:10:37 -05002595 goto out_put_tun;
Gao feng3811ae72013-04-24 21:59:23 +00002596 }
Richard Cochraneda29772013-07-19 19:40:10 +02002597 if (flags & MSG_ERRQUEUE) {
2598 ret = sock_recv_errqueue(sock->sk, m, total_len,
2599 SOL_PACKET, TUN_TX_TIMESTAMP);
2600 goto out;
2601 }
Jason Wangfc72d1d2018-01-04 11:14:28 +08002602 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr);
Alex Gartrell87897932014-12-25 23:05:03 -08002603 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05002604 m->msg_flags |= MSG_TRUNC;
2605 ret = flags & MSG_TRUNC ? ret : total_len;
2606 }
Gao feng3811ae72013-04-24 21:59:23 +00002607out:
Jason Wang54f968d2012-10-31 19:45:57 +00002608 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002609 return ret;
Wei Xuc33ee152017-12-01 05:10:37 -05002610
2611out_put_tun:
2612 tun_put(tun);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002613out_free:
2614 tun_ptr_free(ptr);
Wei Xuc33ee152017-12-01 05:10:37 -05002615 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002616}
2617
Jason Wangfc72d1d2018-01-04 11:14:28 +08002618static int tun_ptr_peek_len(void *ptr)
2619{
2620 if (likely(ptr)) {
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002621 if (tun_is_xdp_frame(ptr)) {
2622 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002623
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002624 return xdpf->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002625 }
2626 return __skb_array_len_with_tag(ptr);
2627 } else {
2628 return 0;
2629 }
2630}
2631
Jason Wang1576d982016-06-30 14:45:36 +08002632static int tun_peek_len(struct socket *sock)
2633{
2634 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2635 struct tun_struct *tun;
2636 int ret = 0;
2637
yuan linyu9484dc72017-09-23 22:36:52 +08002638 tun = tun_get(tfile);
Jason Wang1576d982016-06-30 14:45:36 +08002639 if (!tun)
2640 return 0;
2641
Jason Wangfc72d1d2018-01-04 11:14:28 +08002642 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len);
Jason Wang1576d982016-06-30 14:45:36 +08002643 tun_put(tun);
2644
2645 return ret;
2646}
2647
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002648/* Ops structure to mimic raw sockets with tun */
2649static const struct proto_ops tun_socket_ops = {
Jason Wang1576d982016-06-30 14:45:36 +08002650 .peek_len = tun_peek_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002651 .sendmsg = tun_sendmsg,
2652 .recvmsg = tun_recvmsg,
2653};
2654
Herbert Xu33dccbb2009-02-05 21:25:32 -08002655static struct proto tun_proto = {
2656 .name = "tun",
2657 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00002658 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08002659};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002660
David Woodhouse980c9e82009-05-09 22:54:21 -07002661static int tun_flags(struct tun_struct *tun)
2662{
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02002663 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07002664}
2665
2666static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
2667 char *buf)
2668{
2669 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2670 return sprintf(buf, "0x%x\n", tun_flags(tun));
2671}
2672
2673static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
2674 char *buf)
2675{
2676 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002677 return uid_valid(tun->owner)?
2678 sprintf(buf, "%u\n",
2679 from_kuid_munged(current_user_ns(), tun->owner)):
2680 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002681}
2682
2683static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
2684 char *buf)
2685{
2686 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002687 return gid_valid(tun->group) ?
2688 sprintf(buf, "%u\n",
2689 from_kgid_munged(current_user_ns(), tun->group)):
2690 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002691}
2692
2693static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
2694static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
2695static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
2696
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002697static struct attribute *tun_dev_attrs[] = {
2698 &dev_attr_tun_flags.attr,
2699 &dev_attr_owner.attr,
2700 &dev_attr_group.attr,
2701 NULL
2702};
2703
2704static const struct attribute_group tun_attr_group = {
2705 .attrs = tun_dev_attrs
2706};
2707
Pavel Emelyanovd647a592008-04-16 00:41:16 -07002708static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
2710 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00002711 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 struct net_device *dev;
2713 int err;
2714
Jason Wang7c0c3b12013-01-11 16:59:33 +00002715 if (tfile->detached)
2716 return -EINVAL;
2717
Petar Penkov90e33d42017-09-22 13:49:15 -07002718 if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
2719 if (!capable(CAP_NET_ADMIN))
2720 return -EPERM;
2721
2722 if (!(ifr->ifr_flags & IFF_NAPI) ||
2723 (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
2724 return -EINVAL;
2725 }
2726
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002727 dev = __dev_get_by_name(net, ifr->ifr_name);
2728 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07002729 if (ifr->ifr_flags & IFF_TUN_EXCL)
2730 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002731 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2732 tun = netdev_priv(dev);
2733 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2734 tun = netdev_priv(dev);
2735 else
2736 return -EINVAL;
2737
Jason Wang8e6d91a2013-05-28 18:32:11 +00002738 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002739 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00002740 return -EINVAL;
2741
Jason Wangcde8b152012-10-31 19:46:01 +00002742 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04002743 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002744 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04002745 if (err < 0)
2746 return err;
2747
Petar Penkov94317092017-09-22 13:49:14 -07002748 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002749 ifr->ifr_flags & IFF_NAPI,
2750 ifr->ifr_flags & IFF_NAPI_FRAGS);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00002751 if (err < 0)
2752 return err;
Jason Wang4008e972012-12-13 23:53:30 +00002753
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002754 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00002755 (tun->numqueues + tun->numdisabled > 1)) {
2756 /* One or more queue has already been attached, no need
2757 * to initialize the device again.
2758 */
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002759 netdev_state_change(dev);
Jason Wange8dbad62013-04-22 20:40:39 +00002760 return 0;
2761 }
Sabrina Dubroca9fffc5c2018-04-10 16:28:55 +02002762
2763 tun->flags = (tun->flags & ~TUN_FEATURES) |
2764 (ifr->ifr_flags & TUN_FEATURES);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002765
2766 netdev_state_change(dev);
2767 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 char *name;
2769 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00002770 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2771 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Eric W. Biedermanc260b772012-11-18 21:34:11 +00002773 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002774 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04002775 err = security_tun_dev_create();
2776 if (err < 0)
2777 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 /* Set dev type */
2780 if (ifr->ifr_flags & IFF_TUN) {
2781 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002782 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 name = "tun%d";
2784 } else if (ifr->ifr_flags & IFF_TAP) {
2785 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002786 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002788 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00002789 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (*ifr->ifr_name)
2792 name = ifr->ifr_name;
2793
Jason Wangc8d68e62012-10-31 19:46:00 +00002794 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02002795 NET_NAME_UNKNOWN, tun_setup, queues,
2796 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (!dev)
2799 return -ENOMEM;
Cong Wang0ad646c2017-10-13 11:58:53 -07002800 err = dev_get_valid_name(net, dev, name);
Julien Gomes5c25f652017-10-25 11:50:50 -07002801 if (err < 0)
Cong Wang0ad646c2017-10-13 11:58:53 -07002802 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07002804 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002805 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002806 dev->ifindex = tfile->ifindex;
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002807 dev->sysfs_groups[0] = &tun_attr_group;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 tun = netdev_priv(dev);
2810 tun->dev = dev;
2811 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002812 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002813 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Paolo Abenieaea34b2016-02-26 10:45:40 +01002815 tun->align = NET_SKB_PAD;
Jason Wang54f968d2012-10-31 19:45:57 +00002816 tun->filter_attached = false;
2817 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Jason Wang5503fce2017-01-18 15:02:03 +08002818 tun->rx_batched = 0;
Jason Wang96f84062017-12-04 17:31:23 +08002819 RCU_INIT_POINTER(tun->steering_prog, NULL);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002820
Paolo Abeni608b9972016-04-13 10:52:20 +02002821 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2822 if (!tun->pcpu_stats) {
2823 err = -ENOMEM;
2824 goto err_free_dev;
2825 }
2826
Jason Wang96442e422012-10-31 19:46:02 +00002827 spin_lock_init(&tun->lock);
2828
Paul Moore5dbbaf22013-01-14 07:12:19 +00002829 err = security_tun_dev_alloc_security(&tun->security);
2830 if (err < 0)
Paolo Abeni608b9972016-04-13 10:52:20 +02002831 goto err_free_stat;
Paul Moore2b980db2009-08-28 18:12:43 -04002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04002834 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00002835
Michał Mirosław88255372011-04-19 06:13:10 +00002836 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08002837 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2838 NETIF_F_HW_VLAN_STAG_TX;
Paolo Abeni2a2bbf12016-04-14 18:39:39 +02002839 dev->features = dev->hw_features | NETIF_F_LLTX;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09002840 dev->vlan_features = dev->features &
2841 ~(NETIF_F_HW_VLAN_CTAG_TX |
2842 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00002843
Sabrina Dubroca9fffc5c2018-04-10 16:28:55 +02002844 tun->flags = (tun->flags & ~TUN_FEATURES) |
2845 (ifr->ifr_flags & TUN_FEATURES);
2846
Jason Wang4008e972012-12-13 23:53:30 +00002847 INIT_LIST_HEAD(&tun->disabled);
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002848 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI,
2849 ifr->ifr_flags & IFF_NAPI_FRAGS);
Jason Wangeb0fb362012-12-02 17:19:45 +00002850 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002851 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 err = register_netdevice(tun->dev);
2854 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002855 goto err_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
2857
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00002858 netif_carrier_on(tun->dev);
2859
Joe Perches6b8a66e2011-03-02 07:18:10 +00002860 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002862 /* Make sure persistent devices do not get stuck in
2863 * xoff state.
2864 */
2865 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00002866 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 strcpy(ifr->ifr_name, tun->dev->name);
2869 return 0;
2870
Jason Wang662ca432013-09-11 18:09:48 +08002871err_detach:
2872 tun_detach_all(dev);
Eric Dumazetff244c62017-08-18 13:39:56 -07002873 /* register_netdevice() already called tun_free_netdev() */
2874 goto err_free_dev;
2875
Jason Wang662ca432013-09-11 18:09:48 +08002876err_free_flow:
2877 tun_flow_uninit(tun);
2878 security_tun_dev_free_security(tun->security);
Paolo Abeni608b9972016-04-13 10:52:20 +02002879err_free_stat:
2880 free_percpu(tun->pcpu_stats);
Jason Wang662ca432013-09-11 18:09:48 +08002881err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return err;
2884}
2885
Kirill Tkhai12132762019-03-20 12:16:53 +03002886static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07002887{
Joe Perches6b8a66e2011-03-02 07:18:10 +00002888 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07002889
2890 strcpy(ifr->ifr_name, tun->dev->name);
2891
David Woodhouse980c9e82009-05-09 22:54:21 -07002892 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07002893
Mark McLoughline3b99552008-08-15 15:09:56 -07002894}
2895
Rusty Russell5228ddc2008-07-03 03:46:16 -07002896/* This is like a cut-down ethtool ops, except done via tun fd so no
2897 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00002898static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07002899{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002900 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002901
2902 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00002903 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002904 arg &= ~TUN_F_CSUM;
2905
2906 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2907 if (arg & TUN_F_TSO_ECN) {
2908 features |= NETIF_F_TSO_ECN;
2909 arg &= ~TUN_F_TSO_ECN;
2910 }
2911 if (arg & TUN_F_TSO4)
2912 features |= NETIF_F_TSO;
2913 if (arg & TUN_F_TSO6)
2914 features |= NETIF_F_TSO6;
2915 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2916 }
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05002917
2918 arg &= ~TUN_F_UFO;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002919 }
2920
2921 /* This gives the user a way to test for new features in future by
2922 * trying to set them. */
2923 if (arg)
2924 return -EINVAL;
2925
Michał Mirosław88255372011-04-19 06:13:10 +00002926 tun->set_features = features;
Yaroslav Isakov09050952017-03-16 22:44:10 +03002927 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2928 tun->dev->wanted_features |= features;
Michał Mirosław88255372011-04-19 06:13:10 +00002929 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07002930
2931 return 0;
2932}
2933
Jason Wangc8d68e62012-10-31 19:46:00 +00002934static void tun_detach_filter(struct tun_struct *tun, int n)
2935{
2936 int i;
2937 struct tun_file *tfile;
2938
2939 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002940 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002941 lock_sock(tfile->socket.sk);
2942 sk_detach_filter(tfile->socket.sk);
2943 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002944 }
2945
2946 tun->filter_attached = false;
2947}
2948
2949static int tun_attach_filter(struct tun_struct *tun)
2950{
2951 int i, ret = 0;
2952 struct tun_file *tfile;
2953
2954 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002955 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002956 lock_sock(tfile->socket.sk);
2957 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2958 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002959 if (ret) {
2960 tun_detach_filter(tun, i);
2961 return ret;
2962 }
2963 }
2964
2965 tun->filter_attached = true;
2966 return ret;
2967}
2968
2969static void tun_set_sndbuf(struct tun_struct *tun)
2970{
2971 struct tun_file *tfile;
2972 int i;
2973
2974 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002975 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00002976 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2977 }
2978}
2979
Jason Wangcde8b152012-10-31 19:46:01 +00002980static int tun_set_queue(struct file *file, struct ifreq *ifr)
2981{
2982 struct tun_file *tfile = file->private_data;
2983 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00002984 int ret = 0;
2985
2986 rtnl_lock();
2987
2988 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00002989 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002990 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00002991 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002992 goto unlock;
2993 }
2994 ret = security_tun_dev_attach_queue(tun->security);
2995 if (ret < 0)
2996 goto unlock;
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002997 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI,
2998 tun->flags & IFF_NAPI_FRAGS);
Jason Wang4008e972012-12-13 23:53:30 +00002999 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00003000 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003001 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00003002 ret = -EINVAL;
3003 else
3004 __tun_detach(tfile, false);
3005 } else
Jason Wangcde8b152012-10-31 19:46:01 +00003006 ret = -EINVAL;
3007
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003008 if (ret >= 0)
3009 netdev_state_change(tun->dev);
3010
Paul Moore5dbbaf22013-01-14 07:12:19 +00003011unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00003012 rtnl_unlock();
3013 return ret;
3014}
3015
Jason Wangcd5681d2018-01-16 16:31:01 +08003016static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog **prog_p,
3017 void __user *data)
Jason Wang96f84062017-12-04 17:31:23 +08003018{
3019 struct bpf_prog *prog;
3020 int fd;
3021
3022 if (copy_from_user(&fd, data, sizeof(fd)))
3023 return -EFAULT;
3024
3025 if (fd == -1) {
3026 prog = NULL;
3027 } else {
3028 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
3029 if (IS_ERR(prog))
3030 return PTR_ERR(prog);
3031 }
3032
Jason Wangcd5681d2018-01-16 16:31:01 +08003033 return __tun_set_ebpf(tun, prog_p, prog);
Jason Wang96f84062017-12-04 17:31:23 +08003034}
3035
Arnd Bergmann50857e22009-11-06 22:52:32 -08003036static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
3037 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00003039 struct tun_file *tfile = file->private_data;
Kirill Tkhaif6637062018-05-08 19:21:34 +03003040 struct net *net = sock_net(&tfile->sk);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003041 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 void __user* argp = (void __user*)arg;
Nicolas Dichtel26d31922018-11-28 19:12:56 +01003043 unsigned int ifindex, carrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08003045 kuid_t owner;
3046 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003047 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02003048 int vnet_hdr_sz;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02003049 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003050 int ret;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003051 bool do_notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Kirill Tkhaif2780d62018-02-14 16:40:14 +03003053 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
3054 (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08003055 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07003057 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00003058 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07003059 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003060 if (cmd == TUNGETFEATURES) {
3061 /* Currently this just means: "what IFF flags are valid?".
3062 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02003063 * TUNSETIFF.
3064 */
3065 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00003066 (unsigned int __user*)argp);
Kirill Tkhaif6637062018-05-08 19:21:34 +03003067 } else if (cmd == TUNSETQUEUE) {
Jason Wangcde8b152012-10-31 19:46:01 +00003068 return tun_set_queue(file, &ifr);
Kirill Tkhaif6637062018-05-08 19:21:34 +03003069 } else if (cmd == SIOCGSKNS) {
3070 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3071 return -EPERM;
3072 return open_related_ns(&net->ns, get_net_ns);
3073 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003074
Jason Wangc8d68e62012-10-31 19:46:00 +00003075 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00003076 rtnl_lock();
3077
yuan linyu9484dc72017-09-23 22:36:52 +08003078 tun = tun_get(tfile);
Gao Feng0f16bc12016-10-25 22:26:09 +08003079 if (cmd == TUNSETIFF) {
3080 ret = -EEXIST;
3081 if (tun)
3082 goto unlock;
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 ifr.ifr_name[IFNAMSIZ-1] = '\0';
3085
Kirill Tkhaif2780d62018-02-14 16:40:14 +03003086 ret = tun_set_iff(net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Herbert Xu876bfd42009-08-06 14:22:44 +00003088 if (ret)
3089 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Arnd Bergmann50857e22009-11-06 22:52:32 -08003091 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00003092 ret = -EFAULT;
3093 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04003095 if (cmd == TUNSETIFINDEX) {
3096 ret = -EPERM;
3097 if (tun)
3098 goto unlock;
3099
3100 ret = -EFAULT;
3101 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
3102 goto unlock;
3103
3104 ret = 0;
3105 tfile->ifindex = ifindex;
3106 goto unlock;
3107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Herbert Xu876bfd42009-08-06 14:22:44 +00003109 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00003111 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Jason Wang1e588332012-10-31 19:45:56 +00003113 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Kirill Tkhai0c3e0e32019-03-20 12:16:42 +03003115 net = dev_net(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003116 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07003118 case TUNGETIFF:
Kirill Tkhai12132762019-03-20 12:16:53 +03003119 tun_get_iff(tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07003120
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04003121 if (tfile->detached)
3122 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04003123 if (!tfile->socket.sk->sk_filter)
3124 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04003125
Arnd Bergmann50857e22009-11-06 22:52:32 -08003126 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00003127 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07003128 break;
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 case TUNSETNOCSUM:
3131 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Michał Mirosław88255372011-04-19 06:13:10 +00003133 /* [unimplemented] */
3134 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00003135 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 break;
3137
3138 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00003139 /* Disable/Enable persist mode. Keep an extra reference to the
3140 * module to prevent the module being unprobed.
3141 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003142 if (arg && !(tun->flags & IFF_PERSIST)) {
3143 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00003144 __module_get(THIS_MODULE);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003145 do_notify = true;
Jason Wangdd38bd82013-01-11 16:59:34 +00003146 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003147 if (!arg && (tun->flags & IFF_PERSIST)) {
3148 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00003149 module_put(THIS_MODULE);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003150 do_notify = true;
Jason Wang54f968d2012-10-31 19:45:57 +00003151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Joe Perches6b8a66e2011-03-02 07:18:10 +00003153 tun_debug(KERN_INFO, tun, "persist %s\n",
3154 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 break;
3156
3157 case TUNSETOWNER:
3158 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08003159 owner = make_kuid(current_user_ns(), arg);
3160 if (!uid_valid(owner)) {
3161 ret = -EINVAL;
3162 break;
3163 }
3164 tun->owner = owner;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003165 do_notify = true;
Jason Wang1e588332012-10-31 19:45:56 +00003166 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08003167 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 break;
3169
Guido Guenther8c644622007-07-02 22:50:25 -07003170 case TUNSETGROUP:
3171 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08003172 group = make_kgid(current_user_ns(), arg);
3173 if (!gid_valid(group)) {
3174 ret = -EINVAL;
3175 break;
3176 }
3177 tun->group = group;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003178 do_notify = true;
Jason Wang1e588332012-10-31 19:45:56 +00003179 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08003180 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07003181 break;
3182
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003183 case TUNSETLINK:
3184 /* Only allow setting the type when the interface is down */
3185 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003186 tun_debug(KERN_INFO, tun,
3187 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07003188 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003189 } else {
3190 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00003191 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
3192 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07003193 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003194 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003195 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197#ifdef TUN_DEBUG
3198 case TUNSETDEBUG:
3199 tun->debug = arg;
3200 break;
3201#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07003202 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00003203 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003204 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07003205
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003206 case TUNSETTXFILTER:
3207 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00003208 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003209 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00003210 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07003211 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003215 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003216 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
3217 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08003218 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00003219 ret = -EFAULT;
3220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003223 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00003224 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
3225 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08003226
Petr Machata3a37a962018-12-13 11:54:30 +00003227 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr, NULL);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003228 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003229
3230 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00003231 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003232 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
3233 ret = -EFAULT;
3234 break;
3235
3236 case TUNSETSNDBUF:
3237 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
3238 ret = -EFAULT;
3239 break;
3240 }
Craig Gallek931619222017-10-30 18:50:11 -04003241 if (sndbuf <= 0) {
3242 ret = -EINVAL;
3243 break;
3244 }
Herbert Xu33dccbb2009-02-05 21:25:32 -08003245
Jason Wangc8d68e62012-10-31 19:46:00 +00003246 tun->sndbuf = sndbuf;
3247 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08003248 break;
3249
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02003250 case TUNGETVNETHDRSZ:
3251 vnet_hdr_sz = tun->vnet_hdr_sz;
3252 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
3253 ret = -EFAULT;
3254 break;
3255
3256 case TUNSETVNETHDRSZ:
3257 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
3258 ret = -EFAULT;
3259 break;
3260 }
3261 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
3262 ret = -EINVAL;
3263 break;
3264 }
3265
3266 tun->vnet_hdr_sz = vnet_hdr_sz;
3267 break;
3268
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02003269 case TUNGETVNETLE:
3270 le = !!(tun->flags & TUN_VNET_LE);
3271 if (put_user(le, (int __user *)argp))
3272 ret = -EFAULT;
3273 break;
3274
3275 case TUNSETVNETLE:
3276 if (get_user(le, (int __user *)argp)) {
3277 ret = -EFAULT;
3278 break;
3279 }
3280 if (le)
3281 tun->flags |= TUN_VNET_LE;
3282 else
3283 tun->flags &= ~TUN_VNET_LE;
3284 break;
3285
Greg Kurz8b8e6582015-04-24 14:50:36 +02003286 case TUNGETVNETBE:
3287 ret = tun_get_vnet_be(tun, argp);
3288 break;
3289
3290 case TUNSETVNETBE:
3291 ret = tun_set_vnet_be(tun, argp);
3292 break;
3293
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003294 case TUNATTACHFILTER:
3295 /* Can be set only for TAPs */
3296 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003297 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003298 break;
3299 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00003300 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003301 break;
3302
Jason Wangc8d68e62012-10-31 19:46:00 +00003303 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003304 break;
3305
3306 case TUNDETACHFILTER:
3307 /* Can be set only for TAPs */
3308 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003309 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003310 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00003311 ret = 0;
3312 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003313 break;
3314
Pavel Emelyanov76975e92013-08-21 14:32:39 +04003315 case TUNGETFILTER:
3316 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003317 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04003318 break;
3319 ret = -EFAULT;
3320 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
3321 break;
3322 ret = 0;
3323 break;
3324
Jason Wang96f84062017-12-04 17:31:23 +08003325 case TUNSETSTEERINGEBPF:
Jason Wangcd5681d2018-01-16 16:31:01 +08003326 ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
Jason Wang96f84062017-12-04 17:31:23 +08003327 break;
3328
Jason Wangaff3d702018-01-16 16:31:02 +08003329 case TUNSETFILTEREBPF:
3330 ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
3331 break;
3332
Nicolas Dichtel26d31922018-11-28 19:12:56 +01003333 case TUNSETCARRIER:
3334 ret = -EFAULT;
3335 if (copy_from_user(&carrier, argp, sizeof(carrier)))
3336 goto unlock;
3337
3338 ret = tun_net_change_carrier(tun->dev, (bool)carrier);
3339 break;
3340
Kirill Tkhai0c3e0e32019-03-20 12:16:42 +03003341 case TUNGETDEVNETNS:
3342 ret = -EPERM;
3343 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3344 goto unlock;
3345 ret = open_related_ns(&net->ns, get_net_ns);
3346 break;
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00003349 ret = -EINVAL;
3350 break;
Joe Perchesee289b62010-05-17 22:47:34 -07003351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003353 if (do_notify)
3354 netdev_state_change(tun->dev);
3355
Herbert Xu876bfd42009-08-06 14:22:44 +00003356unlock:
3357 rtnl_unlock();
3358 if (tun)
3359 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Arnd Bergmann50857e22009-11-06 22:52:32 -08003363static long tun_chr_ioctl(struct file *file,
3364 unsigned int cmd, unsigned long arg)
3365{
3366 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
3367}
3368
3369#ifdef CONFIG_COMPAT
3370static long tun_chr_compat_ioctl(struct file *file,
3371 unsigned int cmd, unsigned long arg)
3372{
3373 switch (cmd) {
3374 case TUNSETIFF:
3375 case TUNGETIFF:
3376 case TUNSETTXFILTER:
3377 case TUNGETSNDBUF:
3378 case TUNSETSNDBUF:
3379 case SIOCGIFHWADDR:
3380 case SIOCSIFHWADDR:
3381 arg = (unsigned long)compat_ptr(arg);
3382 break;
3383 default:
3384 arg = (compat_ulong_t)arg;
3385 break;
3386 }
3387
3388 /*
3389 * compat_ifreq is shorter than ifreq, so we must not access beyond
3390 * the end of that structure. All fields that are used in this
3391 * driver are compatible though, we don't need to convert the
3392 * contents.
3393 */
3394 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
3395}
3396#endif /* CONFIG_COMPAT */
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398static int tun_chr_fasync(int fd, struct file *file, int on)
3399{
Jason Wang54f968d2012-10-31 19:45:57 +00003400 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 int ret;
3402
Jason Wang54f968d2012-10-31 19:45:57 +00003403 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06003404 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 if (on) {
Eric W. Biederman01919132017-07-16 22:05:57 -05003407 __f_setown(file, task_pid(current), PIDTYPE_TGID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00003408 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003409 } else
Jason Wang54f968d2012-10-31 19:45:57 +00003410 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06003411 ret = 0;
3412out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06003413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
3416static int tun_chr_open(struct inode *inode, struct file * file)
3417{
Eric W. Biederman140e807da2015-05-08 21:07:08 -05003418 struct net *net = current->nsproxy->net_ns;
Eric W. Biederman631ab462009-01-20 11:00:40 +00003419 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07003420
Joe Perches6b8a66e2011-03-02 07:18:10 +00003421 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00003422
Eric W. Biederman140e807da2015-05-08 21:07:08 -05003423 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003424 &tun_proto, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003425 if (!tfile)
3426 return -ENOMEM;
Jason Wangb196d882018-05-11 10:49:25 +08003427 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) {
3428 sk_free(&tfile->sk);
3429 return -ENOMEM;
3430 }
3431
Eric Dumazetc7256f52018-09-28 14:51:48 -07003432 mutex_init(&tfile->napi_mutex);
Monam Agarwalc9566742014-03-24 00:02:32 +05303433 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang54f968d2012-10-31 19:45:57 +00003434 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04003435 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00003436
Jason Wang54f968d2012-10-31 19:45:57 +00003437 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07003438 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00003439
3440 tfile->socket.file = file;
3441 tfile->socket.ops = &tun_socket_ops;
3442
3443 sock_init_data(&tfile->socket, &tfile->sk);
Jason Wang54f968d2012-10-31 19:45:57 +00003444
3445 tfile->sk.sk_write_space = tun_sock_write_space;
3446 tfile->sk.sk_sndbuf = INT_MAX;
3447
Eric W. Biederman631ab462009-01-20 11:00:40 +00003448 file->private_data = tfile;
Jason Wang4008e972012-12-13 23:53:30 +00003449 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00003450
Jason Wang19a6afb2013-06-08 14:17:41 +08003451 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return 0;
3454}
3455
3456static int tun_chr_close(struct inode *inode, struct file *file)
3457{
Eric W. Biederman631ab462009-01-20 11:00:40 +00003458 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Jason Wangc8d68e62012-10-31 19:46:00 +00003460 tun_detach(tfile, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 return 0;
3463}
3464
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003465#ifdef CONFIG_PROC_FS
yuan linyu9484dc72017-09-23 22:36:52 +08003466static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003467{
yuan linyu9484dc72017-09-23 22:36:52 +08003468 struct tun_file *tfile = file->private_data;
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003469 struct tun_struct *tun;
3470 struct ifreq ifr;
3471
3472 memset(&ifr, 0, sizeof(ifr));
3473
3474 rtnl_lock();
yuan linyu9484dc72017-09-23 22:36:52 +08003475 tun = tun_get(tfile);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003476 if (tun)
Kirill Tkhai12132762019-03-20 12:16:53 +03003477 tun_get_iff(tun, &ifr);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003478 rtnl_unlock();
3479
3480 if (tun)
3481 tun_put(tun);
3482
Joe Perchesa3816ab2014-09-29 16:08:25 -07003483 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003484}
3485#endif
3486
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003487static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003488 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05003490 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04003491 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08003493 .unlocked_ioctl = tun_chr_ioctl,
3494#ifdef CONFIG_COMPAT
3495 .compat_ioctl = tun_chr_compat_ioctl,
3496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 .open = tun_chr_open,
3498 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003499 .fasync = tun_chr_fasync,
3500#ifdef CONFIG_PROC_FS
3501 .show_fdinfo = tun_chr_show_fdinfo,
3502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503};
3504
3505static struct miscdevice tun_miscdev = {
3506 .minor = TUN_MINOR,
3507 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02003508 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510};
3511
3512/* ethtool interface */
3513
Chas Williams4e24f2d2018-06-02 17:49:53 -04003514static void tun_default_link_ksettings(struct net_device *dev,
3515 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Philippe Reynes29ccc492017-03-11 22:03:50 +01003517 ethtool_link_ksettings_zero_link_mode(cmd, supported);
3518 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
3519 cmd->base.speed = SPEED_10;
3520 cmd->base.duplex = DUPLEX_FULL;
3521 cmd->base.port = PORT_TP;
3522 cmd->base.phy_address = 0;
3523 cmd->base.autoneg = AUTONEG_DISABLE;
Chas Williams4e24f2d2018-06-02 17:49:53 -04003524}
3525
3526static int tun_get_link_ksettings(struct net_device *dev,
3527 struct ethtool_link_ksettings *cmd)
3528{
3529 struct tun_struct *tun = netdev_priv(dev);
3530
3531 memcpy(cmd, &tun->link_ksettings, sizeof(*cmd));
3532 return 0;
3533}
3534
3535static int tun_set_link_ksettings(struct net_device *dev,
3536 const struct ethtool_link_ksettings *cmd)
3537{
3538 struct tun_struct *tun = netdev_priv(dev);
3539
3540 memcpy(&tun->link_ksettings, cmd, sizeof(*cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return 0;
3542}
3543
3544static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3545{
3546 struct tun_struct *tun = netdev_priv(dev);
3547
Rick Jones33a5ba12011-11-15 14:59:53 +00003548 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
3549 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003552 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00003553 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003555 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00003556 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 break;
3558 }
3559}
3560
3561static u32 tun_get_msglevel(struct net_device *dev)
3562{
3563#ifdef TUN_DEBUG
3564 struct tun_struct *tun = netdev_priv(dev);
3565 return tun->debug;
3566#else
3567 return -EOPNOTSUPP;
3568#endif
3569}
3570
3571static void tun_set_msglevel(struct net_device *dev, u32 value)
3572{
3573#ifdef TUN_DEBUG
3574 struct tun_struct *tun = netdev_priv(dev);
3575 tun->debug = value;
3576#endif
3577}
3578
Jason Wang5503fce2017-01-18 15:02:03 +08003579static int tun_get_coalesce(struct net_device *dev,
3580 struct ethtool_coalesce *ec)
3581{
3582 struct tun_struct *tun = netdev_priv(dev);
3583
3584 ec->rx_max_coalesced_frames = tun->rx_batched;
3585
3586 return 0;
3587}
3588
3589static int tun_set_coalesce(struct net_device *dev,
3590 struct ethtool_coalesce *ec)
3591{
3592 struct tun_struct *tun = netdev_priv(dev);
3593
3594 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
3595 tun->rx_batched = NAPI_POLL_WEIGHT;
3596 else
3597 tun->rx_batched = ec->rx_max_coalesced_frames;
3598
3599 return 0;
3600}
3601
Jeff Garzik7282d492006-09-13 14:30:00 -04003602static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 .get_drvinfo = tun_get_drvinfo,
3604 .get_msglevel = tun_get_msglevel,
3605 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00003606 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02003607 .get_ts_info = ethtool_op_get_ts_info,
Jason Wang5503fce2017-01-18 15:02:03 +08003608 .get_coalesce = tun_get_coalesce,
3609 .set_coalesce = tun_set_coalesce,
Philippe Reynes29ccc492017-03-11 22:03:50 +01003610 .get_link_ksettings = tun_get_link_ksettings,
Chas Williams4e24f2d2018-06-02 17:49:53 -04003611 .set_link_ksettings = tun_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612};
3613
Jason Wang1576d982016-06-30 14:45:36 +08003614static int tun_queue_resize(struct tun_struct *tun)
3615{
3616 struct net_device *dev = tun->dev;
3617 struct tun_file *tfile;
Jason Wang5990a302018-01-04 11:14:27 +08003618 struct ptr_ring **rings;
Jason Wang1576d982016-06-30 14:45:36 +08003619 int n = tun->numqueues + tun->numdisabled;
3620 int ret, i;
3621
Jason Wang5990a302018-01-04 11:14:27 +08003622 rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
3623 if (!rings)
Jason Wang1576d982016-06-30 14:45:36 +08003624 return -ENOMEM;
3625
3626 for (i = 0; i < tun->numqueues; i++) {
3627 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wang5990a302018-01-04 11:14:27 +08003628 rings[i] = &tfile->tx_ring;
Jason Wang1576d982016-06-30 14:45:36 +08003629 }
3630 list_for_each_entry(tfile, &tun->disabled, next)
Jason Wang5990a302018-01-04 11:14:27 +08003631 rings[i++] = &tfile->tx_ring;
Jason Wang1576d982016-06-30 14:45:36 +08003632
Jason Wang5990a302018-01-04 11:14:27 +08003633 ret = ptr_ring_resize_multiple(rings, n,
3634 dev->tx_queue_len, GFP_KERNEL,
Jason Wangfc72d1d2018-01-04 11:14:28 +08003635 tun_ptr_free);
Jason Wang1576d982016-06-30 14:45:36 +08003636
Jason Wang5990a302018-01-04 11:14:27 +08003637 kfree(rings);
Jason Wang1576d982016-06-30 14:45:36 +08003638 return ret;
3639}
3640
3641static int tun_device_event(struct notifier_block *unused,
3642 unsigned long event, void *ptr)
3643{
3644 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3645 struct tun_struct *tun = netdev_priv(dev);
3646
Craig Gallek86dfb4ac2016-07-06 18:44:20 -04003647 if (dev->rtnl_link_ops != &tun_link_ops)
3648 return NOTIFY_DONE;
3649
Jason Wang1576d982016-06-30 14:45:36 +08003650 switch (event) {
3651 case NETDEV_CHANGE_TX_QUEUE_LEN:
3652 if (tun_queue_resize(tun))
3653 return NOTIFY_BAD;
3654 break;
3655 default:
3656 break;
3657 }
3658
3659 return NOTIFY_DONE;
3660}
3661
3662static struct notifier_block tun_notifier_block __read_mostly = {
3663 .notifier_call = tun_device_event,
3664};
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666static int __init tun_init(void)
3667{
3668 int ret = 0;
3669
Joe Perches6b8a66e2011-03-02 07:18:10 +00003670 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003672 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003673 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003674 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003675 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003676 }
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003679 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003680 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003681 goto err_misc;
3682 }
Jason Wang1576d982016-06-30 14:45:36 +08003683
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003684 ret = register_netdevice_notifier(&tun_notifier_block);
3685 if (ret) {
3686 pr_err("Can't register netdevice notifier\n");
3687 goto err_notifier;
3688 }
3689
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003690 return 0;
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003691
3692err_notifier:
3693 misc_deregister(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003694err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003695 rtnl_link_unregister(&tun_link_ops);
3696err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return ret;
3698}
3699
3700static void tun_cleanup(void)
3701{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003702 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003703 rtnl_link_unregister(&tun_link_ops);
Jason Wang1576d982016-06-30 14:45:36 +08003704 unregister_netdevice_notifier(&tun_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
3706
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003707/* Get an underlying socket object from tun file. Returns error unless file is
3708 * attached to a device. The returned object works like a packet socket, it
3709 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
3710 * holding a reference to the file for as long as the socket is in use. */
3711struct socket *tun_get_socket(struct file *file)
3712{
Jason Wang6e914fc2012-10-31 19:45:58 +00003713 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003714 if (file->f_op != &tun_fops)
3715 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00003716 tfile = file->private_data;
3717 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003718 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00003719 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003720}
3721EXPORT_SYMBOL_GPL(tun_get_socket);
3722
Jason Wang5990a302018-01-04 11:14:27 +08003723struct ptr_ring *tun_get_tx_ring(struct file *file)
Jason Wang83339c62017-05-17 12:14:41 +08003724{
3725 struct tun_file *tfile;
3726
3727 if (file->f_op != &tun_fops)
3728 return ERR_PTR(-EINVAL);
3729 tfile = file->private_data;
3730 if (!tfile)
3731 return ERR_PTR(-EBADFD);
Jason Wang5990a302018-01-04 11:14:27 +08003732 return &tfile->tx_ring;
Jason Wang83339c62017-05-17 12:14:41 +08003733}
Jason Wang5990a302018-01-04 11:14:27 +08003734EXPORT_SYMBOL_GPL(tun_get_tx_ring);
Jason Wang83339c62017-05-17 12:14:41 +08003735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736module_init(tun_init);
3737module_exit(tun_cleanup);
3738MODULE_DESCRIPTION(DRV_DESCRIPTION);
3739MODULE_AUTHOR(DRV_COPYRIGHT);
3740MODULE_LICENSE("GPL");
3741MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02003742MODULE_ALIAS("devname:net/tun");