blob: 812dc3a65efbb9d1ee2724e73978dbc4803ec171 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * TUN - Universal TUN/TAP device driver.
4 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
7 */
8
9/*
10 * Changes:
11 *
Mike Kershawff4cc3a2005-09-01 17:40:05 -070012 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
13 * Add TUNSETLINK ioctl to set the link encapsulation
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Mark Smith <markzzzsmith@yahoo.com.au>
Joe Perches344dc8e2012-07-12 19:33:09 +000016 * Use eth_random_addr() for tap MAC address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
19 * Fixes in packet dropping, queue length setting and queue wakeup.
20 * Increased default tx queue length.
21 * Added ethtool API.
22 * Minor cleanups
23 *
24 * Daniel Podlejski <underley@underley.eu.org>
25 * Modifications for 2.3.99-pre5 kernel.
26 */
27
Joe Perches6b8a66e2011-03-02 07:18:10 +000028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define DRV_NAME "tun"
31#define DRV_VERSION "1.6"
32#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
33#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/errno.h>
37#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010038#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/major.h>
40#include <linux/slab.h>
41#include <linux/poll.h>
42#include <linux/fcntl.h>
43#include <linux/init.h>
44#include <linux/skbuff.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/miscdevice.h>
48#include <linux/ethtool.h>
49#include <linux/rtnetlink.h>
Arnd Bergmann50857e22009-11-06 22:52:32 -080050#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/if.h>
52#include <linux/if_arp.h>
53#include <linux/if_ether.h>
54#include <linux/if_tun.h>
Jason Wang6680ec62013-07-25 13:00:33 +080055#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070057#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070058#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000059#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070060#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070061#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080062#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080063#include <net/sock.h>
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +020064#include <net/xdp.h>
Masatake YAMATO93e14b62014-01-29 16:43:31 +090065#include <linux/seq_file.h>
Herbert Xue0b46d02014-11-07 21:22:23 +080066#include <linux/uio.h>
Jason Wang1576d982016-06-30 14:45:36 +080067#include <linux/skb_array.h>
Jason Wang761876c2017-08-11 19:41:18 +080068#include <linux/bpf.h>
69#include <linux/bpf_trace.h>
Petar Penkov90e33d42017-09-22 13:49:15 -070070#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080072#include <linux/uaccess.h>
Kirill Tkhaif2780d62018-02-14 16:40:14 +030073#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Chas Williams4e24f2d2018-06-02 17:49:53 -040075static void tun_default_link_ksettings(struct net_device *dev,
76 struct ethtool_link_ksettings *cmd);
77
Rusty Russell14daa022008-04-12 18:48:58 -070078/* Uncomment to enable debugging */
79/* #define TUN_DEBUG 1 */
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#ifdef TUN_DEBUG
82static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070083
Joe Perches6b8a66e2011-03-02 07:18:10 +000084#define tun_debug(level, tun, fmt, args...) \
85do { \
86 if (tun->debug) \
87 netdev_printk(level, tun->dev, fmt, ##args); \
88} while (0)
89#define DBG1(level, fmt, args...) \
90do { \
91 if (debug == 2) \
92 printk(level fmt, ##args); \
93} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070094#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000095#define tun_debug(level, tun, fmt, args...) \
96do { \
97 if (0) \
98 netdev_printk(level, tun->dev, fmt, ##args); \
99} while (0)
100#define DBG1(level, fmt, args...) \
101do { \
102 if (0) \
103 printk(level fmt, ##args); \
104} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106
Jason Wang7df13212017-09-04 11:36:08 +0800107#define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
Jason Wang66ccbc92017-08-11 19:41:16 +0800108
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +0200109/* TUN device flags */
110
111/* IFF_ATTACH_QUEUE is never stored in device flags,
112 * overload it to mean fasync when stored there.
113 */
114#define TUN_FASYNC IFF_ATTACH_QUEUE
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200115/* High bits in flags field are unused. */
116#define TUN_VNET_LE 0x80000000
Greg Kurz8b8e6582015-04-24 14:50:36 +0200117#define TUN_VNET_BE 0x40000000
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +0200118
119#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Petar Penkov90e33d42017-09-22 13:49:15 -0700120 IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS)
121
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000122#define GOODCOPY_LEN 128
123
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700124#define FLT_EXACT_COUNT 8
125struct tap_filter {
126 unsigned int count; /* Number of addrs. Zero means disabled */
127 u32 mask[2]; /* Mask of the hashed addrs */
128 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
129};
130
Pankaj Guptabaf71c52015-01-12 11:41:29 +0530131/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
132 * to max number of VCPUs in guest. */
133#define MAX_TAP_QUEUES 256
Jason Wangb8732fb2013-01-23 03:59:13 +0000134#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000135
Jason Wang96442e422012-10-31 19:46:02 +0000136#define TUN_FLOW_EXPIRE (3 * HZ)
137
Paolo Abeni608b9972016-04-13 10:52:20 +0200138struct tun_pcpu_stats {
139 u64 rx_packets;
140 u64 rx_bytes;
141 u64 tx_packets;
142 u64 tx_bytes;
143 struct u64_stats_sync syncp;
144 u32 rx_dropped;
145 u32 tx_dropped;
146 u32 rx_frame_errors;
147};
148
Jason Wang54f968d2012-10-31 19:45:57 +0000149/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800150 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000151 * to serve as one transmit queue for tuntap device. The sock_fprog and
152 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000153 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000154 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000155 *
156 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000157 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000158 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000159 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000160struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000161 struct sock sk;
162 struct socket socket;
Jason Wang6e914fc2012-10-31 19:45:58 +0000163 struct tun_struct __rcu *tun;
Jason Wang54f968d2012-10-31 19:45:57 +0000164 struct fasync_struct *fasync;
165 /* only used for fasnyc */
166 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400167 union {
168 u16 queue_index;
169 unsigned int ifindex;
170 };
Petar Penkov94317092017-09-22 13:49:14 -0700171 struct napi_struct napi;
Eric Dumazetaec72f32017-10-18 12:12:09 -0700172 bool napi_enabled;
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700173 bool napi_frags_enabled;
Petar Penkov90e33d42017-09-22 13:49:15 -0700174 struct mutex napi_mutex; /* Protects access to the above napi */
Jason Wang4008e972012-12-13 23:53:30 +0000175 struct list_head next;
176 struct tun_struct *detached;
Jason Wang5990a302018-01-04 11:14:27 +0800177 struct ptr_ring tx_ring;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100178 struct xdp_rxq_info xdp_rxq;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000179};
180
Jason Wangf9e06c42018-11-15 17:43:10 +0800181struct tun_page {
182 struct page *page;
183 int count;
184};
185
Jason Wang96442e422012-10-31 19:46:02 +0000186struct tun_flow_entry {
187 struct hlist_node hash_link;
188 struct rcu_head rcu;
189 struct tun_struct *tun;
190
191 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800192 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000193 int queue_index;
Li RongQing83b1bc12018-12-06 16:08:17 +0800194 unsigned long updated ____cacheline_aligned_in_smp;
Jason Wang96442e422012-10-31 19:46:02 +0000195};
196
197#define TUN_NUM_FLOW_ENTRIES 1024
Li RongQingf13b5462018-08-03 15:50:02 +0800198#define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1)
Jason Wang96442e422012-10-31 19:46:02 +0000199
Jason Wangcd5681d2018-01-16 16:31:01 +0800200struct tun_prog {
Jason Wang96f84062017-12-04 17:31:23 +0800201 struct rcu_head rcu;
202 struct bpf_prog *prog;
203};
204
Jason Wang54f968d2012-10-31 19:45:57 +0000205/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000206 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000207 * file were attached to a persist device.
208 */
Rusty Russell14daa022008-04-12 18:48:58 -0700209struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000210 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
211 unsigned int numqueues;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700212 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800213 kuid_t owner;
214 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700215
Rusty Russell14daa022008-04-12 18:48:58 -0700216 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000217 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000218#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
David S. Millerd591a1f2017-07-03 06:35:32 -0700219 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200220
Paolo Abenieaea34b2016-02-26 10:45:40 +0100221 int align;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200222 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000223 int sndbuf;
224 struct tap_filter txflt;
225 struct sock_fprog fprog;
226 /* protected by rtnl lock */
227 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700228#ifdef TUN_DEBUG
229 int debug;
230#endif
Jason Wang96442e422012-10-31 19:46:02 +0000231 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000232 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
233 struct timer_list flow_gc_timer;
234 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000235 unsigned int numdisabled;
236 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000237 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000238 u32 flow_count;
Jason Wang5503fce2017-01-18 15:02:03 +0800239 u32 rx_batched;
Paolo Abeni608b9972016-04-13 10:52:20 +0200240 struct tun_pcpu_stats __percpu *pcpu_stats;
Jason Wang761876c2017-08-11 19:41:18 +0800241 struct bpf_prog __rcu *xdp_prog;
Jason Wangcd5681d2018-01-16 16:31:01 +0800242 struct tun_prog __rcu *steering_prog;
Jason Wangaff3d702018-01-16 16:31:02 +0800243 struct tun_prog __rcu *filter_prog;
Chas Williams4e24f2d2018-06-02 17:49:53 -0400244 struct ethtool_link_ksettings link_ksettings;
Rusty Russell14daa022008-04-12 18:48:58 -0700245};
246
Jason Wangaff3d702018-01-16 16:31:02 +0800247struct veth {
248 __be16 h_vlan_proto;
249 __be16 h_vlan_TCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200252bool tun_is_xdp_frame(void *ptr)
Jason Wangfc72d1d2018-01-04 11:14:28 +0800253{
254 return (unsigned long)ptr & TUN_XDP_FLAG;
255}
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200256EXPORT_SYMBOL(tun_is_xdp_frame);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800257
258void *tun_xdp_to_ptr(void *ptr)
259{
260 return (void *)((unsigned long)ptr | TUN_XDP_FLAG);
261}
262EXPORT_SYMBOL(tun_xdp_to_ptr);
263
264void *tun_ptr_to_xdp(void *ptr)
265{
266 return (void *)((unsigned long)ptr & ~TUN_XDP_FLAG);
267}
268EXPORT_SYMBOL(tun_ptr_to_xdp);
269
Petar Penkov94317092017-09-22 13:49:14 -0700270static int tun_napi_receive(struct napi_struct *napi, int budget)
271{
272 struct tun_file *tfile = container_of(napi, struct tun_file, napi);
273 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
274 struct sk_buff_head process_queue;
275 struct sk_buff *skb;
276 int received = 0;
277
278 __skb_queue_head_init(&process_queue);
279
280 spin_lock(&queue->lock);
281 skb_queue_splice_tail_init(queue, &process_queue);
282 spin_unlock(&queue->lock);
283
284 while (received < budget && (skb = __skb_dequeue(&process_queue))) {
285 napi_gro_receive(napi, skb);
286 ++received;
287 }
288
289 if (!skb_queue_empty(&process_queue)) {
290 spin_lock(&queue->lock);
291 skb_queue_splice(&process_queue, queue);
292 spin_unlock(&queue->lock);
293 }
294
295 return received;
296}
297
298static int tun_napi_poll(struct napi_struct *napi, int budget)
299{
300 unsigned int received;
301
302 received = tun_napi_receive(napi, budget);
303
304 if (received < budget)
305 napi_complete_done(napi, received);
306
307 return received;
308}
309
310static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700311 bool napi_en, bool napi_frags)
Petar Penkov94317092017-09-22 13:49:14 -0700312{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700313 tfile->napi_enabled = napi_en;
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700314 tfile->napi_frags_enabled = napi_en && napi_frags;
Petar Penkov94317092017-09-22 13:49:14 -0700315 if (napi_en) {
316 netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
317 NAPI_POLL_WEIGHT);
318 napi_enable(&tfile->napi);
319 }
320}
321
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700322static void tun_napi_disable(struct tun_file *tfile)
Petar Penkov94317092017-09-22 13:49:14 -0700323{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700324 if (tfile->napi_enabled)
Petar Penkov94317092017-09-22 13:49:14 -0700325 napi_disable(&tfile->napi);
326}
327
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700328static void tun_napi_del(struct tun_file *tfile)
Petar Penkov94317092017-09-22 13:49:14 -0700329{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700330 if (tfile->napi_enabled)
Petar Penkov94317092017-09-22 13:49:14 -0700331 netif_napi_del(&tfile->napi);
332}
333
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700334static bool tun_napi_frags_enabled(const struct tun_file *tfile)
Petar Penkov90e33d42017-09-22 13:49:15 -0700335{
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700336 return tfile->napi_frags_enabled;
Petar Penkov90e33d42017-09-22 13:49:15 -0700337}
338
Greg Kurz8b8e6582015-04-24 14:50:36 +0200339#ifdef CONFIG_TUN_VNET_CROSS_LE
340static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
341{
342 return tun->flags & TUN_VNET_BE ? false :
343 virtio_legacy_is_little_endian();
344}
345
346static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
347{
348 int be = !!(tun->flags & TUN_VNET_BE);
349
350 if (put_user(be, argp))
351 return -EFAULT;
352
353 return 0;
354}
355
356static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
357{
358 int be;
359
360 if (get_user(be, argp))
361 return -EFAULT;
362
363 if (be)
364 tun->flags |= TUN_VNET_BE;
365 else
366 tun->flags &= ~TUN_VNET_BE;
367
368 return 0;
369}
370#else
371static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
372{
373 return virtio_legacy_is_little_endian();
374}
375
376static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
377{
378 return -EINVAL;
379}
380
381static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
382{
383 return -EINVAL;
384}
385#endif /* CONFIG_TUN_VNET_CROSS_LE */
386
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200387static inline bool tun_is_little_endian(struct tun_struct *tun)
388{
Greg Kurz7d824102015-04-24 14:26:24 +0200389 return tun->flags & TUN_VNET_LE ||
Greg Kurz8b8e6582015-04-24 14:50:36 +0200390 tun_legacy_is_little_endian(tun);
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200391}
392
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300393static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
394{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200395 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300396}
397
398static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
399{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200400 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300401}
402
Jason Wang96442e422012-10-31 19:46:02 +0000403static inline u32 tun_hashfn(u32 rxhash)
404{
Li RongQingf13b5462018-08-03 15:50:02 +0800405 return rxhash & TUN_MASK_FLOW_ENTRIES;
Jason Wang96442e422012-10-31 19:46:02 +0000406}
407
408static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
409{
410 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000411
Sasha Levinb67bfe02013-02-27 17:06:00 -0800412 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000413 if (e->rxhash == rxhash)
414 return e;
415 }
416 return NULL;
417}
418
419static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
420 struct hlist_head *head,
421 u32 rxhash, u16 queue_index)
422{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000423 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
424
Jason Wang96442e422012-10-31 19:46:02 +0000425 if (e) {
426 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
427 rxhash, queue_index);
428 e->updated = jiffies;
429 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800430 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000431 e->queue_index = queue_index;
432 e->tun = tun;
433 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000434 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000435 }
436 return e;
437}
438
Jason Wang96442e422012-10-31 19:46:02 +0000439static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
440{
441 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
442 e->rxhash, e->queue_index);
443 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000444 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000445 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000446}
447
448static void tun_flow_flush(struct tun_struct *tun)
449{
450 int i;
451
452 spin_lock_bh(&tun->lock);
453 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
454 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800455 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000456
Sasha Levinb67bfe02013-02-27 17:06:00 -0800457 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000458 tun_flow_delete(tun, e);
459 }
460 spin_unlock_bh(&tun->lock);
461}
462
463static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
464{
465 int i;
466
467 spin_lock_bh(&tun->lock);
468 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
469 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800470 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000471
Sasha Levinb67bfe02013-02-27 17:06:00 -0800472 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000473 if (e->queue_index == queue_index)
474 tun_flow_delete(tun, e);
475 }
476 }
477 spin_unlock_bh(&tun->lock);
478}
479
Kees Cooke99e88a2017-10-16 14:43:17 -0700480static void tun_flow_cleanup(struct timer_list *t)
Jason Wang96442e422012-10-31 19:46:02 +0000481{
Kees Cooke99e88a2017-10-16 14:43:17 -0700482 struct tun_struct *tun = from_timer(tun, t, flow_gc_timer);
Jason Wang96442e422012-10-31 19:46:02 +0000483 unsigned long delay = tun->ageing_time;
484 unsigned long next_timer = jiffies + delay;
485 unsigned long count = 0;
486 int i;
487
488 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
489
Eric Dumazet7dbfb4e2017-10-20 11:29:55 -0700490 spin_lock(&tun->lock);
Jason Wang96442e422012-10-31 19:46:02 +0000491 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
492 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800493 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000494
Sasha Levinb67bfe02013-02-27 17:06:00 -0800495 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000496 unsigned long this_timer;
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700497
Jason Wang96442e422012-10-31 19:46:02 +0000498 this_timer = e->updated + delay;
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700499 if (time_before_eq(this_timer, jiffies)) {
Jason Wang96442e422012-10-31 19:46:02 +0000500 tun_flow_delete(tun, e);
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700501 continue;
502 }
503 count++;
504 if (time_before(this_timer, next_timer))
Jason Wang96442e422012-10-31 19:46:02 +0000505 next_timer = this_timer;
506 }
507 }
508
509 if (count)
510 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
Eric Dumazet7dbfb4e2017-10-20 11:29:55 -0700511 spin_unlock(&tun->lock);
Jason Wang96442e422012-10-31 19:46:02 +0000512}
513
Eric Dumazet49974422012-12-12 19:22:57 +0000514static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000515 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000516{
517 struct hlist_head *head;
518 struct tun_flow_entry *e;
519 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000520 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000521
Li RongQing5c327f62018-12-06 16:28:11 +0800522 head = &tun->flows[tun_hashfn(rxhash)];
Jason Wang96442e422012-10-31 19:46:02 +0000523
524 rcu_read_lock();
525
Jason Wang96442e422012-10-31 19:46:02 +0000526 e = tun_flow_find(head, rxhash);
527 if (likely(e)) {
528 /* TODO: keep queueing to old queue until it's empty? */
Li RongQing83b1bc12018-12-06 16:08:17 +0800529 if (e->queue_index != queue_index)
530 e->queue_index = queue_index;
531 if (e->updated != jiffies)
532 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800533 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000534 } else {
535 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000536 if (!tun_flow_find(head, rxhash) &&
537 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000538 tun_flow_create(tun, head, rxhash, queue_index);
539
540 if (!timer_pending(&tun->flow_gc_timer))
541 mod_timer(&tun->flow_gc_timer,
542 round_jiffies_up(jiffies + delay));
543 spin_unlock_bh(&tun->lock);
544 }
545
Jason Wang96442e422012-10-31 19:46:02 +0000546 rcu_read_unlock();
547}
548
Tom Herbert9bc88932013-12-22 18:54:32 +0800549/**
550 * Save the hash received in the stack receive path and update the
551 * flow_hash table accordingly.
552 */
553static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
554{
Eric Dumazet567e4b72015-02-06 12:59:01 -0800555 if (unlikely(e->rps_rxhash != hash))
Tom Herbert9bc88932013-12-22 18:54:32 +0800556 e->rps_rxhash = hash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800557}
558
Wang Li4b035272018-10-09 10:32:04 +0800559/* We try to identify a flow through its rxhash. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800560 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000561 * the rxq based on the txq where the last packet of the flow comes. As
562 * the userspace application move between processors, we may get a
Wang Li4b035272018-10-09 10:32:04 +0800563 * different rxq no. here.
Jason Wangc8d68e62012-10-31 19:46:00 +0000564 */
Jason Wang96f84062017-12-04 17:31:23 +0800565static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
Jason Wangc8d68e62012-10-31 19:46:00 +0000566{
Jason Wang96442e422012-10-31 19:46:02 +0000567 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000568 u32 txq = 0;
569 u32 numqueues = 0;
570
Mark Rutland6aa7de02017-10-23 14:07:29 -0700571 numqueues = READ_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000572
Jason Wangfeec0842017-06-06 14:09:49 +0800573 txq = __skb_get_hash_symmetric(skb);
Wang Li4b035272018-10-09 10:32:04 +0800574 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
575 if (e) {
576 tun_flow_save_rps_rxhash(e, txq);
577 txq = e->queue_index;
578 } else {
579 /* use multiply and shift instead of expensive divide */
580 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000581 }
582
Jason Wangc8d68e62012-10-31 19:46:00 +0000583 return txq;
584}
585
Jason Wang96f84062017-12-04 17:31:23 +0800586static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
587{
Jason Wangcd5681d2018-01-16 16:31:01 +0800588 struct tun_prog *prog;
Jason Wanga35d3102019-05-08 23:20:17 -0400589 u32 numqueues;
Jason Wang96f84062017-12-04 17:31:23 +0800590 u16 ret = 0;
591
Jason Wanga35d3102019-05-08 23:20:17 -0400592 numqueues = READ_ONCE(tun->numqueues);
593 if (!numqueues)
594 return 0;
595
Jason Wang96f84062017-12-04 17:31:23 +0800596 prog = rcu_dereference(tun->steering_prog);
597 if (prog)
598 ret = bpf_prog_run_clear_cb(prog->prog, skb);
599
Jason Wanga35d3102019-05-08 23:20:17 -0400600 return ret % numqueues;
Jason Wang96f84062017-12-04 17:31:23 +0800601}
602
603static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +0100604 struct net_device *sb_dev)
Jason Wang96f84062017-12-04 17:31:23 +0800605{
606 struct tun_struct *tun = netdev_priv(dev);
607 u16 ret;
608
609 rcu_read_lock();
610 if (rcu_dereference(tun->steering_prog))
611 ret = tun_ebpf_select_queue(tun, skb);
612 else
613 ret = tun_automq_select_queue(tun, skb);
614 rcu_read_unlock();
615
616 return ret;
617}
618
Jason Wangcde8b152012-10-31 19:46:01 +0000619static inline bool tun_not_capable(struct tun_struct *tun)
620{
621 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000622 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000623
624 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
625 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000626 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000627}
628
Jason Wangc8d68e62012-10-31 19:46:00 +0000629static void tun_set_real_num_queues(struct tun_struct *tun)
630{
631 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
632 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
633}
634
Jason Wang4008e972012-12-13 23:53:30 +0000635static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
636{
637 tfile->detached = tun;
638 list_add_tail(&tfile->next, &tun->disabled);
639 ++tun->numdisabled;
640}
641
Jason Wangd32649d2012-12-18 11:00:27 +0800642static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000643{
644 struct tun_struct *tun = tfile->detached;
645
646 tfile->detached = NULL;
647 list_del_init(&tfile->next);
648 --tun->numdisabled;
649 return tun;
650}
651
Jason Wang3a403072018-03-09 14:50:34 +0800652void tun_ptr_free(void *ptr)
Jason Wangfc72d1d2018-01-04 11:14:28 +0800653{
654 if (!ptr)
655 return;
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200656 if (tun_is_xdp_frame(ptr)) {
657 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800658
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +0200659 xdp_return_frame(xdpf);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800660 } else {
661 __skb_array_destroy_skb(ptr);
662 }
663}
Jason Wang3a403072018-03-09 14:50:34 +0800664EXPORT_SYMBOL_GPL(tun_ptr_free);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800665
Jason Wang4bfb0512013-09-05 17:53:59 +0800666static void tun_queue_purge(struct tun_file *tfile)
667{
Jason Wangfc72d1d2018-01-04 11:14:28 +0800668 void *ptr;
Jason Wang1576d982016-06-30 14:45:36 +0800669
Jason Wangfc72d1d2018-01-04 11:14:28 +0800670 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL)
671 tun_ptr_free(ptr);
Jason Wang1576d982016-06-30 14:45:36 +0800672
Jason Wang5503fce2017-01-18 15:02:03 +0800673 skb_queue_purge(&tfile->sk.sk_write_queue);
Jason Wang4bfb0512013-09-05 17:53:59 +0800674 skb_queue_purge(&tfile->sk.sk_error_queue);
675}
676
Jason Wangc8d68e62012-10-31 19:46:00 +0000677static void __tun_detach(struct tun_file *tfile, bool clean)
678{
679 struct tun_file *ntfile;
680 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000681
Jason Wangb8deabd2013-01-11 16:59:32 +0000682 tun = rtnl_dereference(tfile->tun);
683
Petar Penkov94317092017-09-22 13:49:14 -0700684 if (tun && clean) {
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700685 tun_napi_disable(tfile);
686 tun_napi_del(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700687 }
688
Jason Wang9e857222013-01-28 01:05:19 +0000689 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000690 u16 index = tfile->queue_index;
691 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000692
693 rcu_assign_pointer(tun->tfiles[index],
694 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000695 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000696 ntfile->queue_index = index;
Jason Wang9871a9e2019-05-08 23:20:18 -0400697 rcu_assign_pointer(tun->tfiles[tun->numqueues - 1],
698 NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000699
700 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000701 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530702 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000703 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000704 } else
Jason Wang4008e972012-12-13 23:53:30 +0000705 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000706
707 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000708 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000709 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800710 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000711 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000712 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000713 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000714 sock_put(&tfile->sk);
715 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000716
717 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000718 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
719 netif_carrier_off(tun->dev);
720
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200721 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000722 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000723 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000724 }
Jason Wangb196d882018-05-11 10:49:25 +0800725 if (tun)
726 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wang7063efd2018-05-16 20:39:33 +0800727 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free);
Eric W. Biederman140e807da2015-05-08 21:07:08 -0500728 sock_put(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000729 }
730}
731
732static void tun_detach(struct tun_file *tfile, bool clean)
733{
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200734 struct tun_struct *tun;
735 struct net_device *dev;
736
Jason Wangc8d68e62012-10-31 19:46:00 +0000737 rtnl_lock();
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200738 tun = rtnl_dereference(tfile->tun);
739 dev = tun ? tun->dev : NULL;
Jason Wangc8d68e62012-10-31 19:46:00 +0000740 __tun_detach(tfile, clean);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200741 if (dev)
742 netdev_state_change(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000743 rtnl_unlock();
744}
745
746static void tun_detach_all(struct net_device *dev)
747{
748 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000749 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000750 int i, n = tun->numqueues;
751
752 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000753 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000754 BUG_ON(!tfile);
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700755 tun_napi_disable(tfile);
Jason Wangaddf8fc2016-05-19 13:36:51 +0800756 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700757 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530758 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000759 --tun->numqueues;
760 }
Jason Wang9e857222013-01-28 01:05:19 +0000761 list_for_each_entry(tfile, &tun->disabled, next) {
Jason Wangaddf8fc2016-05-19 13:36:51 +0800762 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700763 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530764 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000765 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000766 BUG_ON(tun->numqueues != 0);
767
768 synchronize_net();
769 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000770 tfile = rtnl_dereference(tun->tfiles[i]);
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700771 tun_napi_del(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000772 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800773 tun_queue_purge(tfile);
Jason Wangb196d882018-05-11 10:49:25 +0800774 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wangc8d68e62012-10-31 19:46:00 +0000775 sock_put(&tfile->sk);
776 }
Jason Wang4008e972012-12-13 23:53:30 +0000777 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
778 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800779 tun_queue_purge(tfile);
Jason Wangb196d882018-05-11 10:49:25 +0800780 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wang4008e972012-12-13 23:53:30 +0000781 sock_put(&tfile->sk);
782 }
783 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000784
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200785 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000786 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000787}
788
Petar Penkov94317092017-09-22 13:49:14 -0700789static int tun_attach(struct tun_struct *tun, struct file *file,
Yang Yingliang77f22f92019-09-10 18:56:57 +0800790 bool skip_filter, bool napi, bool napi_frags,
791 bool publish_tun)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000792{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000793 struct tun_file *tfile = file->private_data;
Jason Wang1576d982016-06-30 14:45:36 +0800794 struct net_device *dev = tun->dev;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000795 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000796
Paul Moore5dbbaf22013-01-14 07:12:19 +0000797 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
798 if (err < 0)
799 goto out;
800
Eric W. Biederman38231b72009-01-20 11:02:28 +0000801 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000802 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000803 goto out;
804
805 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200806 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000807 goto out;
808
809 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000810 if (!tfile->detached &&
811 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000812 goto out;
813
814 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000815
stephen hemminger92d4ea62013-12-05 20:42:58 -0800816 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400817 if (!skip_filter && (tun->filter_attached == true)) {
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +0200818 lock_sock(tfile->socket.sk);
819 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
820 release_sock(tfile->socket.sk);
Jason Wang54f968d2012-10-31 19:45:57 +0000821 if (!err)
822 goto out;
823 }
Jason Wang1576d982016-06-30 14:45:36 +0800824
825 if (!tfile->detached &&
Jason Wangb196d882018-05-11 10:49:25 +0800826 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len,
827 GFP_KERNEL, tun_ptr_free)) {
Jason Wang1576d982016-06-30 14:45:36 +0800828 err = -ENOMEM;
829 goto out;
830 }
831
Jason Wangc8d68e62012-10-31 19:46:00 +0000832 tfile->queue_index = tun->numqueues;
Jason Wangaddf8fc2016-05-19 13:36:51 +0800833 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100834
835 if (tfile->detached) {
836 /* Re-attach detached tfile, updating XDP queue_index */
837 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq));
838
839 if (tfile->xdp_rxq.queue_index != tfile->queue_index)
840 tfile->xdp_rxq.queue_index = tfile->queue_index;
841 } else {
842 /* Setup XDP RX-queue info, for new tfile getting attached */
843 err = xdp_rxq_info_reg(&tfile->xdp_rxq,
844 tun->dev, tfile->queue_index);
845 if (err < 0)
846 goto out;
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200847 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq,
848 MEM_TYPE_PAGE_SHARED, NULL);
849 if (err < 0) {
850 xdp_rxq_info_unreg(&tfile->xdp_rxq);
851 goto out;
852 }
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100853 err = 0;
854 }
855
Petar Penkov94317092017-09-22 13:49:14 -0700856 if (tfile->detached) {
Jason Wang4008e972012-12-13 23:53:30 +0000857 tun_enable_queue(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700858 } else {
Jason Wang4008e972012-12-13 23:53:30 +0000859 sock_hold(&tfile->sk);
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700860 tun_napi_init(tun, tfile, napi, napi_frags);
Petar Penkov94317092017-09-22 13:49:14 -0700861 }
Jason Wang4008e972012-12-13 23:53:30 +0000862
Jason Wange4a2a302018-09-12 11:16:59 +0800863 if (rtnl_dereference(tun->xdp_prog))
864 sock_set_flag(&tfile->sk, SOCK_XDP);
865
Jason Wangc8d68e62012-10-31 19:46:00 +0000866 /* device is allowed to go away first, so no need to hold extra
867 * refcnt.
868 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000869
Stanislav Fomichev0b7959b2019-01-07 13:38:38 -0800870 /* Publish tfile->tun and tun->tfiles only after we've fully
871 * initialized tfile; otherwise we risk using half-initialized
872 * object.
873 */
Yang Yingliang77f22f92019-09-10 18:56:57 +0800874 if (publish_tun)
875 rcu_assign_pointer(tfile->tun, tun);
Stanislav Fomichev0b7959b2019-01-07 13:38:38 -0800876 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
877 tun->numqueues++;
George Amanakis3a03cb82019-01-29 22:50:13 -0500878 tun_set_real_num_queues(tun);
Eric W. Biederman38231b72009-01-20 11:02:28 +0000879out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000880 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000881}
882
yuan linyu9484dc72017-09-23 22:36:52 +0800883static struct tun_struct *tun_get(struct tun_file *tfile)
Eric W. Biederman631ab462009-01-20 11:00:40 +0000884{
Jason Wang6e914fc2012-10-31 19:45:58 +0000885 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000886
Jason Wang6e914fc2012-10-31 19:45:58 +0000887 rcu_read_lock();
888 tun = rcu_dereference(tfile->tun);
889 if (tun)
890 dev_hold(tun->dev);
891 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000892
893 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000894}
895
Eric W. Biederman631ab462009-01-20 11:00:40 +0000896static void tun_put(struct tun_struct *tun)
897{
Jason Wang6e914fc2012-10-31 19:45:58 +0000898 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000899}
900
Joe Perches6b8a66e2011-03-02 07:18:10 +0000901/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700902static void addr_hash_set(u32 *mask, const u8 *addr)
903{
904 int n = ether_crc(ETH_ALEN, addr) >> 26;
905 mask[n >> 5] |= (1 << (n & 31));
906}
907
908static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
909{
910 int n = ether_crc(ETH_ALEN, addr) >> 26;
911 return mask[n >> 5] & (1 << (n & 31));
912}
913
914static int update_filter(struct tap_filter *filter, void __user *arg)
915{
916 struct { u8 u[ETH_ALEN]; } *addr;
917 struct tun_filter uf;
918 int err, alen, n, nexact;
919
920 if (copy_from_user(&uf, arg, sizeof(uf)))
921 return -EFAULT;
922
923 if (!uf.count) {
924 /* Disabled */
925 filter->count = 0;
926 return 0;
927 }
928
929 alen = ETH_ALEN * uf.count;
Markus Elfring28e81902016-08-20 08:54:15 +0200930 addr = memdup_user(arg + sizeof(uf), alen);
931 if (IS_ERR(addr))
932 return PTR_ERR(addr);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700933
934 /* The filter is updated without holding any locks. Which is
935 * perfectly safe. We disable it first and in the worst
936 * case we'll accept a few undesired packets. */
937 filter->count = 0;
938 wmb();
939
940 /* Use first set of addresses as an exact filter */
941 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
942 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
943
944 nexact = n;
945
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800946 /* Remaining multicast addresses are hashed,
947 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700948 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800949 for (; n < uf.count; n++) {
950 if (!is_multicast_ether_addr(addr[n].u)) {
951 err = 0; /* no filter */
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200952 goto free_addr;
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800953 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700954 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800955 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700956
957 /* For ALLMULTI just set the mask to all ones.
958 * This overrides the mask populated above. */
959 if ((uf.flags & TUN_FLT_ALLMULTI))
960 memset(filter->mask, ~0, sizeof(filter->mask));
961
962 /* Now enable the filter */
963 wmb();
964 filter->count = nexact;
965
966 /* Return the number of exact filters */
967 err = nexact;
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200968free_addr:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700969 kfree(addr);
970 return err;
971}
972
973/* Returns: 0 - drop, !=0 - accept */
974static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
975{
976 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
977 * at this point. */
978 struct ethhdr *eh = (struct ethhdr *) skb->data;
979 int i;
980
981 /* Exact match */
982 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000983 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700984 return 1;
985
986 /* Inexact match (multicast only) */
987 if (is_multicast_ether_addr(eh->h_dest))
988 return addr_hash_test(filter->mask, eh->h_dest);
989
990 return 0;
991}
992
993/*
994 * Checks whether the packet is accepted or not.
995 * Returns: 0 - drop, !=0 - accept
996 */
997static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
998{
999 if (!filter->count)
1000 return 1;
1001
1002 return run_filter(filter, skb);
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/* Network device part of the driver */
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007static const struct ethtool_ops tun_ethtool_ops;
1008
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001009/* Net device detach from fd. */
1010static void tun_net_uninit(struct net_device *dev)
1011{
Jason Wangc8d68e62012-10-31 19:46:00 +00001012 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/* Net device open. */
1016static int tun_net_open(struct net_device *dev)
1017{
Jason Wangc8d68e62012-10-31 19:46:00 +00001018 netif_tx_start_all_queues(dev);
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 0;
1021}
1022
1023/* Net device close. */
1024static int tun_net_close(struct net_device *dev)
1025{
Jason Wangc8d68e62012-10-31 19:46:00 +00001026 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 0;
1028}
1029
1030/* Net device start xmit */
Jason Wang96f84062017-12-04 17:31:23 +08001031static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Jason Wang3df97ba2016-04-25 23:13:42 -04001033#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07001034 if (tun->numqueues == 1 && static_branch_unlikely(&rps_needed)) {
Tom Herbert9bc88932013-12-22 18:54:32 +08001035 /* Select queue was not called for the skbuff, so we extract the
1036 * RPS hash and save it into the flow_table here.
1037 */
Wang Li4b035272018-10-09 10:32:04 +08001038 struct tun_flow_entry *e;
Tom Herbert9bc88932013-12-22 18:54:32 +08001039 __u32 rxhash;
1040
Jason Wangfeec0842017-06-06 14:09:49 +08001041 rxhash = __skb_get_hash_symmetric(skb);
Wang Li4b035272018-10-09 10:32:04 +08001042 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], rxhash);
1043 if (e)
1044 tun_flow_save_rps_rxhash(e, rxhash);
Tom Herbert9bc88932013-12-22 18:54:32 +08001045 }
Jason Wang3df97ba2016-04-25 23:13:42 -04001046#endif
Jason Wang96f84062017-12-04 17:31:23 +08001047}
1048
Jason Wangaff3d702018-01-16 16:31:02 +08001049static unsigned int run_ebpf_filter(struct tun_struct *tun,
1050 struct sk_buff *skb,
1051 int len)
1052{
1053 struct tun_prog *prog = rcu_dereference(tun->filter_prog);
1054
1055 if (prog)
1056 len = bpf_prog_run_clear_cb(prog->prog, skb);
1057
1058 return len;
1059}
1060
Jason Wang96f84062017-12-04 17:31:23 +08001061/* Net device start xmit */
1062static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1063{
1064 struct tun_struct *tun = netdev_priv(dev);
1065 int txq = skb->queue_mapping;
1066 struct tun_file *tfile;
Jason Wangaff3d702018-01-16 16:31:02 +08001067 int len = skb->len;
Jason Wang96f84062017-12-04 17:31:23 +08001068
1069 rcu_read_lock();
1070 tfile = rcu_dereference(tun->tfiles[txq]);
Jason Wang96f84062017-12-04 17:31:23 +08001071
1072 /* Drop packet if interface is not attached */
Jason Wang9871a9e2019-05-08 23:20:18 -04001073 if (!tfile)
Jason Wang96f84062017-12-04 17:31:23 +08001074 goto drop;
1075
1076 if (!rcu_dereference(tun->steering_prog))
1077 tun_automq_xmit(tun, skb);
Tom Herbert9bc88932013-12-22 18:54:32 +08001078
Jason Wang6e914fc2012-10-31 19:45:58 +00001079 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
1080
Jason Wangc8d68e62012-10-31 19:46:00 +00001081 BUG_ON(!tfile);
1082
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001083 /* Drop if the filter does not like it.
1084 * This is a noop if the filter is disabled.
1085 * Filter can be enabled only for the TAP devices. */
1086 if (!check_filter(&tun->txflt, skb))
1087 goto drop;
1088
Jason Wang54f968d2012-10-31 19:45:57 +00001089 if (tfile->socket.sk->sk_filter &&
1090 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001091 goto drop;
1092
Jason Wangaff3d702018-01-16 16:31:02 +08001093 len = run_ebpf_filter(tun, skb, len);
Bjørn Mork81c89502018-04-17 22:46:38 +02001094 if (len == 0 || pskb_trim(skb, len))
Jason Wangaff3d702018-01-16 16:31:02 +08001095 goto drop;
1096
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001097 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Jason Wang7bf66302013-09-05 17:54:00 +08001098 goto drop;
1099
Soheil Hassas Yeganeh7b996242016-08-23 18:22:33 -04001100 skb_tx_timestamp(skb);
Richard Cochraneda29772013-07-19 19:40:10 +02001101
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +00001102 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +08001103 * for indefinite time.
1104 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +00001105 skb_orphan(skb);
1106
Florian Westphal895b5c92019-09-29 20:54:03 +02001107 nf_reset_ct(skb);
Eric Dumazetf8af75f2013-03-06 11:02:37 +00001108
Jason Wang5990a302018-01-04 11:14:27 +08001109 if (ptr_ring_produce(&tfile->tx_ring, skb))
Jason Wang1576d982016-06-30 14:45:36 +08001110 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +00001113 if (tfile->flags & TUN_FASYNC)
1114 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -07001115 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +00001116
1117 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +00001118 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120drop:
Paolo Abeni608b9972016-04-13 10:52:20 +02001121 this_cpu_inc(tun->pcpu_stats->tx_dropped);
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +00001122 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +00001124 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +08001125 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001128static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001130 /*
1131 * This callback is supposed to deal with mc filter in
1132 * _rx_ path and has nothing to do with the _tx_ path.
1133 * In rx path we always accept everything userspace gives us.
1134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001137static netdev_features_t tun_net_fix_features(struct net_device *dev,
1138 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +00001139{
1140 struct tun_struct *tun = netdev_priv(dev);
1141
1142 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1143}
Paolo Abenieaea34b2016-02-26 10:45:40 +01001144
1145static void tun_set_headroom(struct net_device *dev, int new_hr)
1146{
1147 struct tun_struct *tun = netdev_priv(dev);
1148
1149 if (new_hr < NET_SKB_PAD)
1150 new_hr = NET_SKB_PAD;
1151
1152 tun->align = new_hr;
1153}
1154
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001155static void
Paolo Abeni608b9972016-04-13 10:52:20 +02001156tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1157{
1158 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
1159 struct tun_struct *tun = netdev_priv(dev);
1160 struct tun_pcpu_stats *p;
1161 int i;
1162
1163 for_each_possible_cpu(i) {
1164 u64 rxpackets, rxbytes, txpackets, txbytes;
1165 unsigned int start;
1166
1167 p = per_cpu_ptr(tun->pcpu_stats, i);
1168 do {
1169 start = u64_stats_fetch_begin(&p->syncp);
1170 rxpackets = p->rx_packets;
1171 rxbytes = p->rx_bytes;
1172 txpackets = p->tx_packets;
1173 txbytes = p->tx_bytes;
1174 } while (u64_stats_fetch_retry(&p->syncp, start));
1175
1176 stats->rx_packets += rxpackets;
1177 stats->rx_bytes += rxbytes;
1178 stats->tx_packets += txpackets;
1179 stats->tx_bytes += txbytes;
1180
1181 /* u32 counters */
1182 rx_dropped += p->rx_dropped;
1183 rx_frame_errors += p->rx_frame_errors;
1184 tx_dropped += p->tx_dropped;
1185 }
1186 stats->rx_dropped = rx_dropped;
1187 stats->rx_frame_errors = rx_frame_errors;
1188 stats->tx_dropped = tx_dropped;
Paolo Abeni608b9972016-04-13 10:52:20 +02001189}
1190
Jason Wang761876c2017-08-11 19:41:18 +08001191static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1192 struct netlink_ext_ack *extack)
1193{
1194 struct tun_struct *tun = netdev_priv(dev);
Jason Wange4a2a302018-09-12 11:16:59 +08001195 struct tun_file *tfile;
Jason Wang761876c2017-08-11 19:41:18 +08001196 struct bpf_prog *old_prog;
Jason Wange4a2a302018-09-12 11:16:59 +08001197 int i;
Jason Wang761876c2017-08-11 19:41:18 +08001198
1199 old_prog = rtnl_dereference(tun->xdp_prog);
1200 rcu_assign_pointer(tun->xdp_prog, prog);
1201 if (old_prog)
1202 bpf_prog_put(old_prog);
1203
Jason Wange4a2a302018-09-12 11:16:59 +08001204 for (i = 0; i < tun->numqueues; i++) {
1205 tfile = rtnl_dereference(tun->tfiles[i]);
1206 if (prog)
1207 sock_set_flag(&tfile->sk, SOCK_XDP);
1208 else
1209 sock_reset_flag(&tfile->sk, SOCK_XDP);
1210 }
1211 list_for_each_entry(tfile, &tun->disabled, next) {
1212 if (prog)
1213 sock_set_flag(&tfile->sk, SOCK_XDP);
1214 else
1215 sock_reset_flag(&tfile->sk, SOCK_XDP);
1216 }
1217
Jason Wang761876c2017-08-11 19:41:18 +08001218 return 0;
1219}
1220
1221static u32 tun_xdp_query(struct net_device *dev)
1222{
1223 struct tun_struct *tun = netdev_priv(dev);
1224 const struct bpf_prog *xdp_prog;
1225
1226 xdp_prog = rtnl_dereference(tun->xdp_prog);
1227 if (xdp_prog)
1228 return xdp_prog->aux->id;
1229
1230 return 0;
1231}
1232
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001233static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
Jason Wang761876c2017-08-11 19:41:18 +08001234{
1235 switch (xdp->command) {
1236 case XDP_SETUP_PROG:
1237 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1238 case XDP_QUERY_PROG:
1239 xdp->prog_id = tun_xdp_query(dev);
Jason Wang761876c2017-08-11 19:41:18 +08001240 return 0;
1241 default:
1242 return -EINVAL;
1243 }
1244}
1245
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001246static int tun_net_change_carrier(struct net_device *dev, bool new_carrier)
1247{
1248 if (new_carrier) {
1249 struct tun_struct *tun = netdev_priv(dev);
1250
1251 if (!tun->numqueues)
1252 return -EPERM;
1253
1254 netif_carrier_on(dev);
1255 } else {
1256 netif_carrier_off(dev);
1257 }
1258 return 0;
1259}
1260
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001261static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001262 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001263 .ndo_open = tun_net_open,
1264 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001265 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001266 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +00001267 .ndo_select_queue = tun_select_queue,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001268 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001269 .ndo_get_stats64 = tun_net_get_stats64,
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001270 .ndo_change_carrier = tun_net_change_carrier,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001271};
1272
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001273static void __tun_xdp_flush_tfile(struct tun_file *tfile)
1274{
1275 /* Notify and wake up reader process */
1276 if (tfile->flags & TUN_FASYNC)
1277 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
1278 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
1279}
1280
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02001281static int tun_xdp_xmit(struct net_device *dev, int n,
1282 struct xdp_frame **frames, u32 flags)
Jason Wangfc72d1d2018-01-04 11:14:28 +08001283{
1284 struct tun_struct *tun = netdev_priv(dev);
Jason Wangfc72d1d2018-01-04 11:14:28 +08001285 struct tun_file *tfile;
1286 u32 numqueues;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001287 int drops = 0;
1288 int cnt = n;
1289 int i;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001290
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001291 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02001292 return -EINVAL;
1293
Jason Wangfc72d1d2018-01-04 11:14:28 +08001294 rcu_read_lock();
1295
Jason Wang9871a9e2019-05-08 23:20:18 -04001296resample:
Jason Wangfc72d1d2018-01-04 11:14:28 +08001297 numqueues = READ_ONCE(tun->numqueues);
1298 if (!numqueues) {
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001299 rcu_read_unlock();
1300 return -ENXIO; /* Caller will free/return all frames */
Jason Wangfc72d1d2018-01-04 11:14:28 +08001301 }
1302
1303 tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
1304 numqueues]);
Jason Wang9871a9e2019-05-08 23:20:18 -04001305 if (unlikely(!tfile))
1306 goto resample;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001307
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001308 spin_lock(&tfile->tx_ring.producer_lock);
1309 for (i = 0; i < n; i++) {
1310 struct xdp_frame *xdp = frames[i];
1311 /* Encode the XDP flag into lowest bit for consumer to differ
1312 * XDP buffer from sk_buff.
1313 */
1314 void *frame = tun_xdp_to_ptr(xdp);
1315
1316 if (__ptr_ring_produce(&tfile->tx_ring, frame)) {
1317 this_cpu_inc(tun->pcpu_stats->tx_dropped);
1318 xdp_return_frame_rx_napi(xdp);
1319 drops++;
1320 }
1321 }
1322 spin_unlock(&tfile->tx_ring.producer_lock);
1323
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001324 if (flags & XDP_XMIT_FLUSH)
1325 __tun_xdp_flush_tfile(tfile);
1326
Jason Wangfc72d1d2018-01-04 11:14:28 +08001327 rcu_read_unlock();
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001328 return cnt - drops;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001329}
1330
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02001331static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
1332{
1333 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
1334
1335 if (unlikely(!frame))
1336 return -EOVERFLOW;
1337
Jesper Dangaard Brouer42421a52018-06-05 13:55:45 +02001338 return tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH);
Jason Wangfc72d1d2018-01-04 11:14:28 +08001339}
1340
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001341static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001342 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001343 .ndo_open = tun_net_open,
1344 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001345 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001346 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001347 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001348 .ndo_set_mac_address = eth_mac_addr,
1349 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +00001350 .ndo_select_queue = tun_select_queue,
Toshiaki Makita5e527962015-07-31 15:03:27 +09001351 .ndo_features_check = passthru_features_check,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001352 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001353 .ndo_get_stats64 = tun_net_get_stats64,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001354 .ndo_bpf = tun_xdp,
Jason Wangfc72d1d2018-01-04 11:14:28 +08001355 .ndo_xdp_xmit = tun_xdp_xmit,
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001356 .ndo_change_carrier = tun_net_change_carrier,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001357};
1358
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001359static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +00001360{
1361 int i;
1362
Jason Wang96442e422012-10-31 19:46:02 +00001363 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1364 INIT_HLIST_HEAD(&tun->flows[i]);
1365
1366 tun->ageing_time = TUN_FLOW_EXPIRE;
Kees Cooke99e88a2017-10-16 14:43:17 -07001367 timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0);
1368 mod_timer(&tun->flow_gc_timer,
1369 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +00001370}
1371
1372static void tun_flow_uninit(struct tun_struct *tun)
1373{
1374 del_timer_sync(&tun->flow_gc_timer);
1375 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001376}
1377
Jarod Wilson91572082016-10-20 13:55:20 -04001378#define MIN_MTU 68
1379#define MAX_MTU 65535
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/* Initialize net device. */
1382static void tun_net_init(struct net_device *dev)
1383{
1384 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001387 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001388 dev->netdev_ops = &tun_netdev_ops;
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Point-to-Point TUN Device */
1391 dev->hard_header_len = 0;
1392 dev->addr_len = 0;
1393 dev->mtu = 1500;
1394
1395 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001396 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001400 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -08001401 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001403 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +00001404 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +00001405 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001407 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410 }
Jarod Wilson91572082016-10-20 13:55:20 -04001411
1412 dev->min_mtu = MIN_MTU;
1413 dev->max_mtu = MAX_MTU - dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Jason Wang2f3ab622018-05-22 14:21:04 +08001416static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile)
1417{
1418 struct sock *sk = tfile->socket.sk;
1419
1420 return (tun->dev->flags & IFF_UP) && sock_writeable(sk);
1421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423/* Character device part */
1424
1425/* Poll */
Al Viroafc9a422017-07-03 06:39:46 -04001426static __poll_t tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001427{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +00001428 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001429 struct tun_struct *tun = tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001430 struct sock *sk;
Al Viroafc9a422017-07-03 06:39:46 -04001431 __poll_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (!tun)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001434 return EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Jason Wang54f968d2012-10-31 19:45:57 +00001436 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001437
Joe Perches6b8a66e2011-03-02 07:18:10 +00001438 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Xi Wang9e641bd2014-05-16 15:11:48 -07001440 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001441
Jason Wang5990a302018-01-04 11:14:27 +08001442 if (!ptr_ring_empty(&tfile->tx_ring))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001443 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Jason Wang2f3ab622018-05-22 14:21:04 +08001445 /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to
1446 * guarantee EPOLLOUT to be raised by either here or
1447 * tun_sock_write_space(). Then process could get notification
1448 * after it writes to a down device and meets -EIO.
1449 */
1450 if (tun_sock_writeable(tun, tfile) ||
1451 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1452 tun_sock_writeable(tun, tfile)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001453 mask |= EPOLLOUT | EPOLLWRNORM;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001454
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001455 if (tun->dev->reg_state != NETREG_REGISTERED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001456 mask = EPOLLERR;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001457
Eric W. Biederman631ab462009-01-20 11:00:40 +00001458 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return mask;
1460}
1461
Petar Penkov90e33d42017-09-22 13:49:15 -07001462static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
1463 size_t len,
1464 const struct iov_iter *it)
1465{
1466 struct sk_buff *skb;
1467 size_t linear;
1468 int err;
1469 int i;
1470
1471 if (it->nr_segs > MAX_SKB_FRAGS + 1)
1472 return ERR_PTR(-ENOMEM);
1473
1474 local_bh_disable();
1475 skb = napi_get_frags(&tfile->napi);
1476 local_bh_enable();
1477 if (!skb)
1478 return ERR_PTR(-ENOMEM);
1479
1480 linear = iov_iter_single_seg_count(it);
1481 err = __skb_grow(skb, linear);
1482 if (err)
1483 goto free;
1484
1485 skb->len = len;
1486 skb->data_len = len - linear;
1487 skb->truesize += skb->data_len;
1488
1489 for (i = 1; i < it->nr_segs; i++) {
1490 size_t fragsz = it->iov[i].iov_len;
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001491 struct page *page;
1492 void *frag;
Petar Penkov90e33d42017-09-22 13:49:15 -07001493
1494 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1495 err = -EINVAL;
1496 goto free;
1497 }
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001498 frag = netdev_alloc_frag(fragsz);
1499 if (!frag) {
Petar Penkov90e33d42017-09-22 13:49:15 -07001500 err = -ENOMEM;
1501 goto free;
1502 }
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001503 page = virt_to_head_page(frag);
1504 skb_fill_page_desc(skb, i - 1, page,
1505 frag - page_address(page), fragsz);
Petar Penkov90e33d42017-09-22 13:49:15 -07001506 }
1507
1508 return skb;
1509free:
1510 /* frees skb and all frags allocated with napi_alloc_frag() */
1511 napi_free_frags(&tfile->napi);
1512 return ERR_PTR(err);
1513}
1514
Rusty Russellf42157c2008-08-15 15:15:10 -07001515/* prepad is the amount to reserve at front. len is length after that.
1516 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001517static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001518 size_t prepad, size_t len,
1519 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001520{
Jason Wang54f968d2012-10-31 19:45:57 +00001521 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001522 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001523 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001524
1525 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001526 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001527 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001528
Herbert Xu33dccbb2009-02-05 21:25:32 -08001529 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001530 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001531 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001532 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001533
1534 skb_reserve(skb, prepad);
1535 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001536 skb->data_len = len - linear;
1537 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001538
1539 return skb;
1540}
1541
Jason Wang5503fce2017-01-18 15:02:03 +08001542static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1543 struct sk_buff *skb, int more)
1544{
1545 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1546 struct sk_buff_head process_queue;
1547 u32 rx_batched = tun->rx_batched;
1548 bool rcv = false;
1549
1550 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1551 local_bh_disable();
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001552 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001553 netif_receive_skb(skb);
1554 local_bh_enable();
1555 return;
1556 }
1557
1558 spin_lock(&queue->lock);
1559 if (!more || skb_queue_len(queue) == rx_batched) {
1560 __skb_queue_head_init(&process_queue);
1561 skb_queue_splice_tail_init(queue, &process_queue);
1562 rcv = true;
1563 } else {
1564 __skb_queue_tail(queue, skb);
1565 }
1566 spin_unlock(&queue->lock);
1567
1568 if (rcv) {
1569 struct sk_buff *nskb;
1570
1571 local_bh_disable();
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001572 while ((nskb = __skb_dequeue(&process_queue))) {
1573 skb_record_rx_queue(nskb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001574 netif_receive_skb(nskb);
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001575 }
1576 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001577 netif_receive_skb(skb);
1578 local_bh_enable();
1579 }
1580}
1581
Jason Wang66ccbc92017-08-11 19:41:16 +08001582static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1583 int len, int noblock, bool zerocopy)
1584{
1585 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1586 return false;
1587
1588 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1589 return false;
1590
1591 if (!noblock)
1592 return false;
1593
1594 if (zerocopy)
1595 return false;
1596
1597 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1598 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1599 return false;
1600
1601 return true;
1602}
1603
Alexis Bauvin4b663362019-07-23 16:23:01 +02001604static struct sk_buff *__tun_build_skb(struct tun_file *tfile,
1605 struct page_frag *alloc_frag, char *buf,
Jason Wang8ae1aff2018-09-12 11:17:04 +08001606 int buflen, int len, int pad)
Jason Wangac1f1f62018-09-12 11:17:03 +08001607{
1608 struct sk_buff *skb = build_skb(buf, buflen);
1609
1610 if (!skb)
1611 return ERR_PTR(-ENOMEM);
1612
Jason Wang8ae1aff2018-09-12 11:17:04 +08001613 skb_reserve(skb, pad);
Jason Wangac1f1f62018-09-12 11:17:03 +08001614 skb_put(skb, len);
Alexis Bauvin4b663362019-07-23 16:23:01 +02001615 skb_set_owner_w(skb, tfile->socket.sk);
Jason Wangac1f1f62018-09-12 11:17:03 +08001616
1617 get_page(alloc_frag->page);
1618 alloc_frag->offset += buflen;
1619
1620 return skb;
1621}
1622
Jason Wang8ae1aff2018-09-12 11:17:04 +08001623static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
1624 struct xdp_buff *xdp, u32 act)
1625{
1626 int err;
1627
1628 switch (act) {
1629 case XDP_REDIRECT:
1630 err = xdp_do_redirect(tun->dev, xdp, xdp_prog);
Jason Wang8ae1aff2018-09-12 11:17:04 +08001631 if (err)
1632 return err;
1633 break;
1634 case XDP_TX:
1635 err = tun_xdp_tx(tun->dev, xdp);
1636 if (err < 0)
1637 return err;
1638 break;
1639 case XDP_PASS:
1640 break;
1641 default:
1642 bpf_warn_invalid_xdp_action(act);
1643 /* fall through */
1644 case XDP_ABORTED:
1645 trace_xdp_exception(tun->dev, xdp_prog, act);
1646 /* fall through */
1647 case XDP_DROP:
1648 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1649 break;
1650 }
1651
1652 return act;
1653}
1654
Jason Wang761876c2017-08-11 19:41:18 +08001655static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1656 struct tun_file *tfile,
Jason Wang66ccbc92017-08-11 19:41:16 +08001657 struct iov_iter *from,
Jason Wang761876c2017-08-11 19:41:18 +08001658 struct virtio_net_hdr *hdr,
Jason Wang1cfe6e92017-09-04 11:36:09 +08001659 int len, int *skb_xdp)
Jason Wang66ccbc92017-08-11 19:41:16 +08001660{
Eric Dumazet0bbd7da2017-08-16 22:14:33 +08001661 struct page_frag *alloc_frag = &current->task_frag;
Jason Wang761876c2017-08-11 19:41:18 +08001662 struct bpf_prog *xdp_prog;
Jason Wang7df13212017-09-04 11:36:08 +08001663 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jason Wang66ccbc92017-08-11 19:41:16 +08001664 char *buf;
1665 size_t copied;
Jason Wang8ae1aff2018-09-12 11:17:04 +08001666 int pad = TUN_RX_PAD;
1667 int err = 0;
Jason Wang7df13212017-09-04 11:36:08 +08001668
1669 rcu_read_lock();
1670 xdp_prog = rcu_dereference(tun->xdp_prog);
1671 if (xdp_prog)
Jason Wang4f23aff2018-09-12 11:17:00 +08001672 pad += XDP_PACKET_HEADROOM;
Jason Wang7df13212017-09-04 11:36:08 +08001673 buflen += SKB_DATA_ALIGN(len + pad);
1674 rcu_read_unlock();
Jason Wang66ccbc92017-08-11 19:41:16 +08001675
Jason Wang63b9ab62017-10-27 11:05:44 +08001676 alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
Jason Wang66ccbc92017-08-11 19:41:16 +08001677 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1678 return ERR_PTR(-ENOMEM);
1679
1680 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1681 copied = copy_page_from_iter(alloc_frag->page,
Jason Wang7df13212017-09-04 11:36:08 +08001682 alloc_frag->offset + pad,
Jason Wang66ccbc92017-08-11 19:41:16 +08001683 len, from);
1684 if (copied != len)
1685 return ERR_PTR(-EFAULT);
1686
Jason Wang7df13212017-09-04 11:36:08 +08001687 /* There's a small window that XDP may be set after the check
1688 * of xdp_prog above, this should be rare and for simplicity
1689 * we do XDP on skb in case the headroom is not enough.
1690 */
Jason Wangac1f1f62018-09-12 11:17:03 +08001691 if (hdr->gso_type || !xdp_prog) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001692 *skb_xdp = 1;
Alexis Bauvin4b663362019-07-23 16:23:01 +02001693 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len,
1694 pad);
Jason Wangac1f1f62018-09-12 11:17:03 +08001695 }
1696
1697 *skb_xdp = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001698
Toshiaki Makita6547e382018-05-28 19:37:49 +09001699 local_bh_disable();
Jason Wang761876c2017-08-11 19:41:18 +08001700 rcu_read_lock();
1701 xdp_prog = rcu_dereference(tun->xdp_prog);
Jason Wang8ae1aff2018-09-12 11:17:04 +08001702 if (xdp_prog) {
Jason Wang761876c2017-08-11 19:41:18 +08001703 struct xdp_buff xdp;
Jason Wang761876c2017-08-11 19:41:18 +08001704 u32 act;
1705
1706 xdp.data_hard_start = buf;
Jason Wang7df13212017-09-04 11:36:08 +08001707 xdp.data = buf + pad;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001708 xdp_set_data_meta_invalid(&xdp);
Jason Wang761876c2017-08-11 19:41:18 +08001709 xdp.data_end = xdp.data + len;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +01001710 xdp.rxq = &tfile->xdp_rxq;
Jason Wang761876c2017-08-11 19:41:18 +08001711
Jason Wang8ae1aff2018-09-12 11:17:04 +08001712 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1713 if (act == XDP_REDIRECT || act == XDP_TX) {
Jason Wang761876c2017-08-11 19:41:18 +08001714 get_page(alloc_frag->page);
1715 alloc_frag->offset += buflen;
Jason Wang761876c2017-08-11 19:41:18 +08001716 }
Jason Wang8ae1aff2018-09-12 11:17:04 +08001717 err = tun_xdp_act(tun, xdp_prog, &xdp, act);
1718 if (err < 0)
1719 goto err_xdp;
Jason Wang1a097912018-09-12 11:17:05 +08001720 if (err == XDP_REDIRECT)
1721 xdp_do_flush_map();
Jason Wang8ae1aff2018-09-12 11:17:04 +08001722 if (err != XDP_PASS)
1723 goto out;
1724
1725 pad = xdp.data - xdp.data_hard_start;
1726 len = xdp.data_end - xdp.data;
Jason Wang761876c2017-08-11 19:41:18 +08001727 }
Jason Wang291aeb22018-09-12 11:17:01 +08001728 rcu_read_unlock();
1729 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001730
Alexis Bauvin4b663362019-07-23 16:23:01 +02001731 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad);
Jason Wang761876c2017-08-11 19:41:18 +08001732
Jason Wang8ae1aff2018-09-12 11:17:04 +08001733err_xdp:
Jason Wang761876c2017-08-11 19:41:18 +08001734 put_page(alloc_frag->page);
Jason Wangf7053b62018-09-12 11:17:02 +08001735out:
Jason Wang761876c2017-08-11 19:41:18 +08001736 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001737 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001738 return NULL;
Jason Wang66ccbc92017-08-11 19:41:16 +08001739}
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001742static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001743 void *msg_control, struct iov_iter *from,
Jason Wang5503fce2017-01-18 15:02:03 +08001744 int noblock, bool more)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Harvey Harrison09640e632009-02-01 00:45:17 -08001746 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001748 size_t total_len = iov_iter_count(from);
Paolo Abenieaea34b2016-02-26 10:45:40 +01001749 size_t len = total_len, align = tun->align, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001750 struct virtio_net_hdr gso = { 0 };
Paolo Abeni608b9972016-04-13 10:52:20 +02001751 struct tun_pcpu_stats *stats;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001752 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001753 int copylen;
1754 bool zerocopy = false;
1755 int err;
Jason Wang96f84062017-12-04 17:31:23 +08001756 u32 rxhash = 0;
Jason Wang1cfe6e92017-09-04 11:36:09 +08001757 int skb_xdp = 1;
Eric Dumazetaf3fb242018-09-28 14:51:49 -07001758 bool frags = tun_napi_frags_enabled(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001760 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001761 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001763 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Al Virocbbd26b2016-11-01 22:09:04 -04001765 if (!copy_from_iter_full(&pi, sizeof(pi), from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return -EFAULT;
1767 }
1768
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001769 if (tun->flags & IFF_VNET_HDR) {
Willem de Bruijne1edab82017-02-03 18:20:48 -05001770 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1771
1772 if (len < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001773 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001774 len -= vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001775
Al Virocbbd26b2016-11-01 22:09:04 -04001776 if (!copy_from_iter_full(&gso, sizeof(gso), from))
Rusty Russellf43798c2008-07-03 03:48:02 -07001777 return -EFAULT;
1778
Herbert Xu49091222009-06-08 00:20:01 -07001779 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001780 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1781 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 -07001782
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001783 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001784 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001785 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001786 }
1787
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001788 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001789 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001790 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001791 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001792 return -EINVAL;
1793 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001794
Jason Wang96f8d9e2013-11-13 14:00:39 +08001795 good_linear = SKB_MAX_HEAD(align);
1796
Jason Wang88529172013-07-18 10:55:15 +08001797 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001798 struct iov_iter i = *from;
1799
Jason Wang88529172013-07-18 10:55:15 +08001800 /* There are 256 bytes to be copied in skb, so there is
1801 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001802 * The rest of the buffer is mapped from userspace.
1803 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001804 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001805 if (copylen > good_linear)
1806 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001807 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001808 iov_iter_advance(&i, copylen);
1809 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001810 zerocopy = true;
1811 }
1812
Petar Penkov90e33d42017-09-22 13:49:15 -07001813 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001814 /* For the packet that is not easy to be processed
1815 * (e.g gso or jumbo packet), we will do it at after
1816 * skb was created with generic XDP routine.
1817 */
1818 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
Jason Wang66ccbc92017-08-11 19:41:16 +08001819 if (IS_ERR(skb)) {
Paolo Abeni608b9972016-04-13 10:52:20 +02001820 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001821 return PTR_ERR(skb);
1822 }
Jason Wang761876c2017-08-11 19:41:18 +08001823 if (!skb)
1824 return total_len;
Jason Wang66ccbc92017-08-11 19:41:16 +08001825 } else {
1826 if (!zerocopy) {
1827 copylen = len;
1828 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1829 linear = good_linear;
1830 else
1831 linear = tun16_to_cpu(tun, gso.hdr_len);
1832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Petar Penkov90e33d42017-09-22 13:49:15 -07001834 if (frags) {
1835 mutex_lock(&tfile->napi_mutex);
1836 skb = tun_napi_alloc_frags(tfile, copylen, from);
1837 /* tun_napi_alloc_frags() enforces a layout for the skb.
1838 * If zerocopy is enabled, then this layout will be
1839 * overwritten by zerocopy_sg_from_iter().
1840 */
1841 zerocopy = false;
1842 } else {
1843 skb = tun_alloc_skb(tfile, align, copylen, linear,
1844 noblock);
1845 }
1846
Jason Wang66ccbc92017-08-11 19:41:16 +08001847 if (IS_ERR(skb)) {
1848 if (PTR_ERR(skb) != -EAGAIN)
1849 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Petar Penkov90e33d42017-09-22 13:49:15 -07001850 if (frags)
1851 mutex_unlock(&tfile->napi_mutex);
Jason Wang66ccbc92017-08-11 19:41:16 +08001852 return PTR_ERR(skb);
1853 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001854
Jason Wang66ccbc92017-08-11 19:41:16 +08001855 if (zerocopy)
1856 err = zerocopy_sg_from_iter(skb, from);
1857 else
1858 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1859
1860 if (err) {
Eric Dumazet44771382019-03-14 20:19:47 -07001861 err = -EFAULT;
1862drop:
Jason Wang66ccbc92017-08-11 19:41:16 +08001863 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1864 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001865 if (frags) {
1866 tfile->napi.skb = NULL;
1867 mutex_unlock(&tfile->napi_mutex);
1868 }
1869
Eric Dumazet44771382019-03-14 20:19:47 -07001870 return err;
Jason Wang66ccbc92017-08-11 19:41:16 +08001871 }
Dave Jones8f227572006-03-11 18:49:13 -08001872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001874 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
Paolo Abenidf10db92016-06-14 00:00:04 +02001875 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1876 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001877 if (frags) {
1878 tfile->napi.skb = NULL;
1879 mutex_unlock(&tfile->napi_mutex);
1880 }
1881
Paolo Abenidf10db92016-06-14 00:00:04 +02001882 return -EINVAL;
1883 }
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001886 case IFF_TUN:
1887 if (tun->flags & IFF_NO_PI) {
Alexander Potapenko2580c4c2017-09-28 11:32:37 +02001888 u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
1889
1890 switch (ip_version) {
1891 case 4:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001892 pi.proto = htons(ETH_P_IP);
1893 break;
Alexander Potapenko2580c4c2017-09-28 11:32:37 +02001894 case 6:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001895 pi.proto = htons(ETH_P_IPV6);
1896 break;
1897 default:
Paolo Abeni608b9972016-04-13 10:52:20 +02001898 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001899 kfree_skb(skb);
1900 return -EINVAL;
1901 }
1902 }
1903
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001904 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001906 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001908 case IFF_TAP:
Petar Penkov90e33d42017-09-22 13:49:15 -07001909 if (!frags)
1910 skb->protocol = eth_type_trans(skb, tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001914 /* copy skb_ubuf_info for callback when skb has no error */
1915 if (zerocopy) {
1916 skb_shinfo(skb)->destructor_arg = msg_control;
1917 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001918 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangaf1cc7a2016-11-30 13:17:51 +08001919 } else if (msg_control) {
1920 struct ubuf_info *uarg = msg_control;
1921 uarg->callback(uarg, false);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001922 }
1923
Vlad Yasevich72f65102015-02-03 16:36:16 -05001924 skb_reset_network_header(skb);
Maxim Mikityanskiyd2aa1252019-02-21 12:39:57 +00001925 skb_probe_transport_header(skb);
Jason Wang38502af2013-03-25 20:19:56 +00001926
Jason Wang1cfe6e92017-09-04 11:36:09 +08001927 if (skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001928 struct bpf_prog *xdp_prog;
1929 int ret;
1930
Toshiaki Makita6547e382018-05-28 19:37:49 +09001931 local_bh_disable();
Jason Wang761876c2017-08-11 19:41:18 +08001932 rcu_read_lock();
1933 xdp_prog = rcu_dereference(tun->xdp_prog);
1934 if (xdp_prog) {
1935 ret = do_xdp_generic(xdp_prog, skb);
1936 if (ret != XDP_PASS) {
1937 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001938 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001939 return total_len;
1940 }
1941 }
1942 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001943 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001944 }
1945
Paolo Abenicf1a1e02018-04-20 13:18:16 +02001946 /* Compute the costly rx hash only if needed for flow updates.
1947 * We may get a very small possibility of OOO during switching, not
1948 * worth to optimize.
1949 */
1950 if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 &&
1951 !tfile->detached)
Jason Wang96f84062017-12-04 17:31:23 +08001952 rxhash = __skb_get_hash_symmetric(skb);
Petar Penkov94317092017-09-22 13:49:14 -07001953
Eric Dumazet44771382019-03-14 20:19:47 -07001954 rcu_read_lock();
1955 if (unlikely(!(tun->dev->flags & IFF_UP))) {
1956 err = -EIO;
Eric Dumazet9180bb42019-03-16 13:09:53 -07001957 rcu_read_unlock();
Eric Dumazet44771382019-03-14 20:19:47 -07001958 goto drop;
1959 }
1960
Petar Penkov90e33d42017-09-22 13:49:15 -07001961 if (frags) {
1962 /* Exercise flow dissector code path. */
Stanislav Fomichevc43f1252019-04-22 08:55:48 -07001963 u32 headlen = eth_get_headlen(tun->dev, skb->data,
1964 skb_headlen(skb));
Petar Penkov90e33d42017-09-22 13:49:15 -07001965
Eric Dumazet010f2452017-10-17 10:07:44 -07001966 if (unlikely(headlen > skb_headlen(skb))) {
Petar Penkov90e33d42017-09-22 13:49:15 -07001967 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1968 napi_free_frags(&tfile->napi);
Eric Dumazet44771382019-03-14 20:19:47 -07001969 rcu_read_unlock();
Petar Penkov90e33d42017-09-22 13:49:15 -07001970 mutex_unlock(&tfile->napi_mutex);
1971 WARN_ON(1);
1972 return -ENOMEM;
1973 }
1974
1975 local_bh_disable();
1976 napi_gro_frags(&tfile->napi);
1977 local_bh_enable();
1978 mutex_unlock(&tfile->napi_mutex);
Eric Dumazetaec72f32017-10-18 12:12:09 -07001979 } else if (tfile->napi_enabled) {
Petar Penkov94317092017-09-22 13:49:14 -07001980 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1981 int queue_len;
1982
1983 spin_lock_bh(&queue->lock);
1984 __skb_queue_tail(queue, skb);
1985 queue_len = skb_queue_len(queue);
1986 spin_unlock(&queue->lock);
1987
1988 if (!more || queue_len > NAPI_POLL_WEIGHT)
1989 napi_schedule(&tfile->napi);
1990
1991 local_bh_enable();
1992 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
1993 tun_rx_batched(tun, tfile, skb, more);
1994 } else {
1995 netif_rx_ni(skb);
1996 }
Eric Dumazet44771382019-03-14 20:19:47 -07001997 rcu_read_unlock();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001998
Paolo Abeni608b9972016-04-13 10:52:20 +02001999 stats = get_cpu_ptr(tun->pcpu_stats);
2000 u64_stats_update_begin(&stats->syncp);
2001 stats->rx_packets++;
2002 stats->rx_bytes += len;
2003 u64_stats_update_end(&stats->syncp);
2004 put_cpu_ptr(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Jason Wang96f84062017-12-04 17:31:23 +08002006 if (rxhash)
2007 tun_flow_update(tun, rxhash, tfile);
2008
Michael S. Tsirkin06908992012-07-20 09:23:23 +00002009 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002010}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Al Virof5ff53b2014-06-19 15:36:49 -04002012static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Herbert Xu33dccbb2009-02-05 21:25:32 -08002014 struct file *file = iocb->ki_filp;
Jason Wang54f968d2012-10-31 19:45:57 +00002015 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08002016 struct tun_struct *tun = tun_get(tfile);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002017 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 if (!tun)
2020 return -EBADFD;
2021
Jason Wang5503fce2017-01-18 15:02:03 +08002022 result = tun_get_user(tun, tfile, NULL, from,
2023 file->f_flags & O_NONBLOCK, false);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002024
2025 tun_put(tun);
2026 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Jason Wangfc72d1d2018-01-04 11:14:28 +08002029static ssize_t tun_put_user_xdp(struct tun_struct *tun,
2030 struct tun_file *tfile,
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002031 struct xdp_frame *xdp_frame,
Jason Wangfc72d1d2018-01-04 11:14:28 +08002032 struct iov_iter *iter)
2033{
2034 int vnet_hdr_sz = 0;
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002035 size_t size = xdp_frame->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002036 struct tun_pcpu_stats *stats;
2037 size_t ret;
2038
2039 if (tun->flags & IFF_VNET_HDR) {
2040 struct virtio_net_hdr gso = { 0 };
2041
2042 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
2043 if (unlikely(iov_iter_count(iter) < vnet_hdr_sz))
2044 return -EINVAL;
2045 if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) !=
2046 sizeof(gso)))
2047 return -EFAULT;
2048 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
2049 }
2050
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002051 ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002052
2053 stats = get_cpu_ptr(tun->pcpu_stats);
2054 u64_stats_update_begin(&stats->syncp);
2055 stats->tx_packets++;
2056 stats->tx_bytes += ret;
2057 u64_stats_update_end(&stats->syncp);
2058 put_cpu_ptr(tun->pcpu_stats);
2059
2060 return ret;
2061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00002064static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00002065 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00002066 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08002067 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 struct tun_pi pi = { 0, skb->protocol };
Paolo Abeni608b9972016-04-13 10:52:20 +02002070 struct tun_pcpu_stats *stats;
Herbert Xue0b46d02014-11-07 21:22:23 +08002071 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08002072 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002073 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08002074 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002075
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002076 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002077 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002079 if (tun->flags & IFF_VNET_HDR)
Willem de Bruijne1edab82017-02-03 18:20:48 -05002080 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Herbert Xue0b46d02014-11-07 21:22:23 +08002082 total = skb->len + vlan_hlen + vnet_hdr_sz;
2083
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002084 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08002085 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return -EINVAL;
2087
Herbert Xue0b46d02014-11-07 21:22:23 +08002088 total += sizeof(pi);
2089 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /* Packet will be striped */
2091 pi.flags |= TUN_PKT_STRIP;
2092 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002093
Herbert Xue0b46d02014-11-07 21:22:23 +08002094 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Herbert Xu2eb783c2014-11-03 04:30:14 +08002098 if (vnet_hdr_sz) {
Jarno Rajahalme9403cd72016-11-18 15:40:40 -08002099 struct virtio_net_hdr gso;
Mike Rapoport34166092016-06-08 16:09:20 +03002100
Herbert Xue0b46d02014-11-07 21:22:23 +08002101 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07002102 return -EINVAL;
2103
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08002104 if (virtio_net_hdr_from_skb(skb, &gso,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002105 tun_is_little_endian(tun), true,
2106 vlan_hlen)) {
Rusty Russellf43798c2008-07-03 03:48:02 -07002107 struct skb_shared_info *sinfo = skb_shinfo(skb);
Mike Rapoport34166092016-06-08 16:09:20 +03002108 pr_err("unexpected GSO type: "
2109 "0x%x, gso_size %d, hdr_len %d\n",
2110 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
2111 tun16_to_cpu(tun, gso.hdr_len));
2112 print_hex_dump(KERN_ERR, "tun: ",
2113 DUMP_PREFIX_NONE,
2114 16, 1, skb->head,
2115 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
2116 WARN_ON_ONCE(1);
2117 return -EINVAL;
2118 }
Rusty Russellf43798c2008-07-03 03:48:02 -07002119
Herbert Xue0b46d02014-11-07 21:22:23 +08002120 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07002121 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08002122
2123 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07002124 }
2125
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002126 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08002127 int ret;
Jason Wangaff3d702018-01-16 16:31:02 +08002128 struct veth veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Jason Wang6680ec62013-07-25 13:00:33 +08002130 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002131 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Jason Wang6680ec62013-07-25 13:00:33 +08002133 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08002134
Herbert Xue0b46d02014-11-07 21:22:23 +08002135 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
2136 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08002137 goto done;
2138
Herbert Xue0b46d02014-11-07 21:22:23 +08002139 ret = copy_to_iter(&veth, sizeof(veth), iter);
2140 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08002141 goto done;
2142 }
2143
Herbert Xue0b46d02014-11-07 21:22:23 +08002144 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08002145
2146done:
Paolo Abeni608b9972016-04-13 10:52:20 +02002147 /* caller is in process context, */
2148 stats = get_cpu_ptr(tun->pcpu_stats);
2149 u64_stats_update_begin(&stats->syncp);
2150 stats->tx_packets++;
2151 stats->tx_bytes += skb->len + vlan_hlen;
2152 u64_stats_update_end(&stats->syncp);
2153 put_cpu_ptr(tun->pcpu_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 return total;
2156}
2157
Jason Wangfc72d1d2018-01-04 11:14:28 +08002158static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err)
Jason Wang1576d982016-06-30 14:45:36 +08002159{
2160 DECLARE_WAITQUEUE(wait, current);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002161 void *ptr = NULL;
Jason Wangf48cc6b2016-07-04 13:53:38 +08002162 int error = 0;
Jason Wang1576d982016-06-30 14:45:36 +08002163
Jason Wangfc72d1d2018-01-04 11:14:28 +08002164 ptr = ptr_ring_consume(&tfile->tx_ring);
2165 if (ptr)
Jason Wang1576d982016-06-30 14:45:36 +08002166 goto out;
2167 if (noblock) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002168 error = -EAGAIN;
Jason Wang1576d982016-06-30 14:45:36 +08002169 goto out;
2170 }
2171
Al Viro333f7902019-07-05 20:14:16 +01002172 add_wait_queue(&tfile->socket.wq.wait, &wait);
Jason Wang1576d982016-06-30 14:45:36 +08002173
2174 while (1) {
Timur Celik71828b22019-02-23 12:53:13 +01002175 set_current_state(TASK_INTERRUPTIBLE);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002176 ptr = ptr_ring_consume(&tfile->tx_ring);
2177 if (ptr)
Jason Wang1576d982016-06-30 14:45:36 +08002178 break;
2179 if (signal_pending(current)) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002180 error = -ERESTARTSYS;
Jason Wang1576d982016-06-30 14:45:36 +08002181 break;
2182 }
2183 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002184 error = -EFAULT;
Jason Wang1576d982016-06-30 14:45:36 +08002185 break;
2186 }
2187
2188 schedule();
2189 }
2190
Timur Celikecef67c2019-02-25 21:13:13 +01002191 __set_current_state(TASK_RUNNING);
Al Viro333f7902019-07-05 20:14:16 +01002192 remove_wait_queue(&tfile->socket.wq.wait, &wait);
Jason Wang1576d982016-06-30 14:45:36 +08002193
2194out:
Jason Wangf48cc6b2016-07-04 13:53:38 +08002195 *err = error;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002196 return ptr;
Jason Wang1576d982016-06-30 14:45:36 +08002197}
2198
Jason Wang54f968d2012-10-31 19:45:57 +00002199static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05002200 struct iov_iter *to,
Jason Wangfc72d1d2018-01-04 11:14:28 +08002201 int noblock, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Al Viro9b067032014-11-07 13:52:07 -05002203 ssize_t ret;
Jason Wang1576d982016-06-30 14:45:36 +08002204 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Rami Rosen3872baf2012-11-25 22:07:41 +00002206 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Wei Xuc33ee152017-12-01 05:10:37 -05002208 if (!iov_iter_count(to)) {
Jason Wangfc72d1d2018-01-04 11:14:28 +08002209 tun_ptr_free(ptr);
Al Viro9b067032014-11-07 13:52:07 -05002210 return 0;
Wei Xuc33ee152017-12-01 05:10:37 -05002211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Jason Wangfc72d1d2018-01-04 11:14:28 +08002213 if (!ptr) {
Jason Wangac77cfd2017-05-17 12:14:43 +08002214 /* Read frames from ring */
Jason Wangfc72d1d2018-01-04 11:14:28 +08002215 ptr = tun_ring_recv(tfile, noblock, &err);
2216 if (!ptr)
Jason Wangac77cfd2017-05-17 12:14:43 +08002217 return err;
2218 }
Herbert Xue0b46d02014-11-07 21:22:23 +08002219
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002220 if (tun_is_xdp_frame(ptr)) {
2221 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002222
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002223 ret = tun_put_user_xdp(tun, tfile, xdpf, to);
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +02002224 xdp_return_frame(xdpf);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002225 } else {
2226 struct sk_buff *skb = ptr;
2227
2228 ret = tun_put_user(tun, tfile, skb, to);
2229 if (unlikely(ret < 0))
2230 kfree_skb(skb);
2231 else
2232 consume_skb(skb);
2233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002235 return ret;
2236}
2237
Al Viro9b067032014-11-07 13:52:07 -05002238static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002239{
2240 struct file *file = iocb->ki_filp;
2241 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08002242 struct tun_struct *tun = tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05002243 ssize_t len = iov_iter_count(to), ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002244
2245 if (!tun)
2246 return -EBADFD;
Jason Wangac77cfd2017-05-17 12:14:43 +08002247 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL);
David S. Miller42404c02013-12-10 22:05:45 -05002248 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08002249 if (ret > 0)
2250 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002251 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return ret;
2253}
2254
Jason Wangcd5681d2018-01-16 16:31:01 +08002255static void tun_prog_free(struct rcu_head *rcu)
Jason Wang96f84062017-12-04 17:31:23 +08002256{
Jason Wangcd5681d2018-01-16 16:31:01 +08002257 struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu);
Jason Wang96f84062017-12-04 17:31:23 +08002258
2259 bpf_prog_destroy(prog->prog);
2260 kfree(prog);
2261}
2262
Jason Wang9d6474e2018-01-22 10:55:38 +08002263static int __tun_set_ebpf(struct tun_struct *tun,
2264 struct tun_prog __rcu **prog_p,
Jason Wangcd5681d2018-01-16 16:31:01 +08002265 struct bpf_prog *prog)
Jason Wang96f84062017-12-04 17:31:23 +08002266{
Jason Wangcd5681d2018-01-16 16:31:01 +08002267 struct tun_prog *old, *new = NULL;
Jason Wang96f84062017-12-04 17:31:23 +08002268
2269 if (prog) {
2270 new = kmalloc(sizeof(*new), GFP_KERNEL);
2271 if (!new)
2272 return -ENOMEM;
2273 new->prog = prog;
2274 }
2275
Jason Wang124da8f2017-12-08 12:02:30 +08002276 spin_lock_bh(&tun->lock);
Jason Wangcd5681d2018-01-16 16:31:01 +08002277 old = rcu_dereference_protected(*prog_p,
Jason Wang124da8f2017-12-08 12:02:30 +08002278 lockdep_is_held(&tun->lock));
Jason Wangcd5681d2018-01-16 16:31:01 +08002279 rcu_assign_pointer(*prog_p, new);
Jason Wang124da8f2017-12-08 12:02:30 +08002280 spin_unlock_bh(&tun->lock);
Jason Wang96f84062017-12-04 17:31:23 +08002281
2282 if (old)
Jason Wangcd5681d2018-01-16 16:31:01 +08002283 call_rcu(&old->rcu, tun_prog_free);
Jason Wang96f84062017-12-04 17:31:23 +08002284
2285 return 0;
2286}
2287
Jason Wang96442e422012-10-31 19:46:02 +00002288static void tun_free_netdev(struct net_device *dev)
2289{
2290 struct tun_struct *tun = netdev_priv(dev);
2291
Jason Wang4008e972012-12-13 23:53:30 +00002292 BUG_ON(!(list_empty(&tun->disabled)));
Paolo Abeni608b9972016-04-13 10:52:20 +02002293 free_percpu(tun->pcpu_stats);
Jason Wang96442e422012-10-31 19:46:02 +00002294 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00002295 security_tun_dev_free_security(tun->security);
Jason Wangcd5681d2018-01-16 16:31:01 +08002296 __tun_set_ebpf(tun, &tun->steering_prog, NULL);
Jason Wangaff3d702018-01-16 16:31:02 +08002297 __tun_set_ebpf(tun, &tun->filter_prog, NULL);
Jason Wang96442e422012-10-31 19:46:02 +00002298}
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300static void tun_setup(struct net_device *dev)
2301{
2302 struct tun_struct *tun = netdev_priv(dev);
2303
Eric W. Biederman0625c882012-02-07 16:48:55 -08002304 tun->owner = INVALID_UID;
2305 tun->group = INVALID_GID;
Chas Williams4e24f2d2018-06-02 17:49:53 -04002306 tun_default_link_ksettings(dev, &tun->link_ksettings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 dev->ethtool_ops = &tun_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04002309 dev->needs_free_netdev = true;
2310 dev->priv_destructor = tun_free_netdev;
Jason Wang016adb72016-04-08 13:26:48 +08002311 /* We prefer our own queue length */
2312 dev->tx_queue_len = TUN_READQ_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002315/* Trivial set of netlink ops to allow deleting tun or tap
2316 * device with netlink.
2317 */
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002318static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2319 struct netlink_ext_ack *extack)
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002320{
Nicolas Dichtel35b827b2018-11-29 14:45:39 +01002321 NL_SET_ERR_MSG(extack,
2322 "tun/tap creation via rtnetlink is not supported.");
2323 return -EOPNOTSUPP;
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002324}
2325
Sabrina Dubroca1ec010e2018-02-16 11:03:07 +01002326static size_t tun_get_size(const struct net_device *dev)
2327{
2328 BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t));
2329 BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t));
2330
2331 return nla_total_size(sizeof(uid_t)) + /* OWNER */
2332 nla_total_size(sizeof(gid_t)) + /* GROUP */
2333 nla_total_size(sizeof(u8)) + /* TYPE */
2334 nla_total_size(sizeof(u8)) + /* PI */
2335 nla_total_size(sizeof(u8)) + /* VNET_HDR */
2336 nla_total_size(sizeof(u8)) + /* PERSIST */
2337 nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */
2338 nla_total_size(sizeof(u32)) + /* NUM_QUEUES */
2339 nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */
2340 0;
2341}
2342
2343static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
2344{
2345 struct tun_struct *tun = netdev_priv(dev);
2346
2347 if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK))
2348 goto nla_put_failure;
2349 if (uid_valid(tun->owner) &&
2350 nla_put_u32(skb, IFLA_TUN_OWNER,
2351 from_kuid_munged(current_user_ns(), tun->owner)))
2352 goto nla_put_failure;
2353 if (gid_valid(tun->group) &&
2354 nla_put_u32(skb, IFLA_TUN_GROUP,
2355 from_kgid_munged(current_user_ns(), tun->group)))
2356 goto nla_put_failure;
2357 if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI)))
2358 goto nla_put_failure;
2359 if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR)))
2360 goto nla_put_failure;
2361 if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST)))
2362 goto nla_put_failure;
2363 if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE,
2364 !!(tun->flags & IFF_MULTI_QUEUE)))
2365 goto nla_put_failure;
2366 if (tun->flags & IFF_MULTI_QUEUE) {
2367 if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues))
2368 goto nla_put_failure;
2369 if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES,
2370 tun->numdisabled))
2371 goto nla_put_failure;
2372 }
2373
2374 return 0;
2375
2376nla_put_failure:
2377 return -EMSGSIZE;
2378}
2379
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002380static struct rtnl_link_ops tun_link_ops __read_mostly = {
2381 .kind = DRV_NAME,
2382 .priv_size = sizeof(struct tun_struct),
2383 .setup = tun_setup,
2384 .validate = tun_validate,
Sabrina Dubroca1ec010e2018-02-16 11:03:07 +01002385 .get_size = tun_get_size,
2386 .fill_info = tun_fill_info,
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002387};
2388
Herbert Xu33dccbb2009-02-05 21:25:32 -08002389static void tun_sock_write_space(struct sock *sk)
2390{
Jason Wang54f968d2012-10-31 19:45:57 +00002391 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00002392 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002393
2394 if (!sock_writeable(sk))
2395 return;
2396
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002397 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Herbert Xu33dccbb2009-02-05 21:25:32 -08002398 return;
2399
Eric Dumazet43815482010-04-29 11:01:49 +00002400 wqueue = sk_sleep(sk);
2401 if (wqueue && waitqueue_active(wqueue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002402 wake_up_interruptible_sync_poll(wqueue, EPOLLOUT |
2403 EPOLLWRNORM | EPOLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07002404
Jason Wang54f968d2012-10-31 19:45:57 +00002405 tfile = container_of(sk, struct tun_file, sk);
2406 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002407}
2408
Jason Wangf9e06c42018-11-15 17:43:10 +08002409static void tun_put_page(struct tun_page *tpage)
2410{
2411 if (tpage->page)
2412 __page_frag_cache_drain(tpage->page, tpage->count);
2413}
2414
Jason Wang043d2222018-09-12 11:17:07 +08002415static int tun_xdp_one(struct tun_struct *tun,
2416 struct tun_file *tfile,
Jason Wangf9e06c42018-11-15 17:43:10 +08002417 struct xdp_buff *xdp, int *flush,
2418 struct tun_page *tpage)
Jason Wang043d2222018-09-12 11:17:07 +08002419{
Prashant Bhole4e4b08e2018-12-03 18:09:24 +09002420 unsigned int datasize = xdp->data_end - xdp->data;
Jason Wang043d2222018-09-12 11:17:07 +08002421 struct tun_xdp_hdr *hdr = xdp->data_hard_start;
2422 struct virtio_net_hdr *gso = &hdr->gso;
2423 struct tun_pcpu_stats *stats;
2424 struct bpf_prog *xdp_prog;
2425 struct sk_buff *skb = NULL;
2426 u32 rxhash = 0, act;
2427 int buflen = hdr->buflen;
2428 int err = 0;
2429 bool skb_xdp = false;
Jason Wangf9e06c42018-11-15 17:43:10 +08002430 struct page *page;
Jason Wang043d2222018-09-12 11:17:07 +08002431
2432 xdp_prog = rcu_dereference(tun->xdp_prog);
2433 if (xdp_prog) {
2434 if (gso->gso_type) {
2435 skb_xdp = true;
2436 goto build;
2437 }
2438 xdp_set_data_meta_invalid(xdp);
2439 xdp->rxq = &tfile->xdp_rxq;
2440
2441 act = bpf_prog_run_xdp(xdp_prog, xdp);
2442 err = tun_xdp_act(tun, xdp_prog, xdp, act);
2443 if (err < 0) {
2444 put_page(virt_to_head_page(xdp->data));
2445 return err;
2446 }
2447
2448 switch (err) {
2449 case XDP_REDIRECT:
2450 *flush = true;
2451 /* fall through */
2452 case XDP_TX:
2453 return 0;
2454 case XDP_PASS:
2455 break;
2456 default:
Jason Wangf9e06c42018-11-15 17:43:10 +08002457 page = virt_to_head_page(xdp->data);
2458 if (tpage->page == page) {
2459 ++tpage->count;
2460 } else {
2461 tun_put_page(tpage);
2462 tpage->page = page;
2463 tpage->count = 1;
2464 }
Jason Wang043d2222018-09-12 11:17:07 +08002465 return 0;
2466 }
2467 }
2468
2469build:
2470 skb = build_skb(xdp->data_hard_start, buflen);
2471 if (!skb) {
2472 err = -ENOMEM;
2473 goto out;
2474 }
2475
2476 skb_reserve(skb, xdp->data - xdp->data_hard_start);
2477 skb_put(skb, xdp->data_end - xdp->data);
2478
2479 if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) {
2480 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
2481 kfree_skb(skb);
2482 err = -EINVAL;
2483 goto out;
2484 }
2485
2486 skb->protocol = eth_type_trans(skb, tun->dev);
2487 skb_reset_network_header(skb);
Maxim Mikityanskiyd2aa1252019-02-21 12:39:57 +00002488 skb_probe_transport_header(skb);
Jason Wang043d2222018-09-12 11:17:07 +08002489
2490 if (skb_xdp) {
2491 err = do_xdp_generic(xdp_prog, skb);
2492 if (err != XDP_PASS)
2493 goto out;
2494 }
2495
Paolo Abenif29eb2a2018-11-07 10:34:36 +01002496 if (!rcu_dereference(tun->steering_prog) && tun->numqueues > 1 &&
2497 !tfile->detached)
Jason Wang043d2222018-09-12 11:17:07 +08002498 rxhash = __skb_get_hash_symmetric(skb);
2499
Matthew Cover8ebebcb2018-11-18 00:46:00 -07002500 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang043d2222018-09-12 11:17:07 +08002501 netif_receive_skb(skb);
2502
Prashant Bhole6342ca62018-12-11 11:43:07 +09002503 /* No need for get_cpu_ptr() here since this function is
2504 * always called with bh disabled
2505 */
2506 stats = this_cpu_ptr(tun->pcpu_stats);
Jason Wang043d2222018-09-12 11:17:07 +08002507 u64_stats_update_begin(&stats->syncp);
2508 stats->rx_packets++;
Prashant Bhole4e4b08e2018-12-03 18:09:24 +09002509 stats->rx_bytes += datasize;
Jason Wang043d2222018-09-12 11:17:07 +08002510 u64_stats_update_end(&stats->syncp);
Jason Wang043d2222018-09-12 11:17:07 +08002511
2512 if (rxhash)
2513 tun_flow_update(tun, rxhash, tfile);
2514
2515out:
2516 return err;
2517}
2518
Ying Xue1b784142015-03-02 15:37:48 +08002519static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002520{
Jason Wang043d2222018-09-12 11:17:07 +08002521 int ret, i;
Jason Wang54f968d2012-10-31 19:45:57 +00002522 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002523 struct tun_struct *tun = tun_get(tfile);
Jason Wangfe8dd452018-09-12 11:17:06 +08002524 struct tun_msg_ctl *ctl = m->msg_control;
Jason Wang043d2222018-09-12 11:17:07 +08002525 struct xdp_buff *xdp;
Jason Wang54f968d2012-10-31 19:45:57 +00002526
2527 if (!tun)
2528 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04002529
Jason Wang043d2222018-09-12 11:17:07 +08002530 if (ctl && (ctl->type == TUN_MSG_PTR)) {
David S. Miller6f0271d2018-11-17 16:53:46 -08002531 struct tun_page tpage;
Jason Wang043d2222018-09-12 11:17:07 +08002532 int n = ctl->num;
2533 int flush = 0;
2534
David S. Miller6f0271d2018-11-17 16:53:46 -08002535 memset(&tpage, 0, sizeof(tpage));
2536
Jason Wang043d2222018-09-12 11:17:07 +08002537 local_bh_disable();
2538 rcu_read_lock();
2539
2540 for (i = 0; i < n; i++) {
2541 xdp = &((struct xdp_buff *)ctl->ptr)[i];
Jason Wangf9e06c42018-11-15 17:43:10 +08002542 tun_xdp_one(tun, tfile, xdp, &flush, &tpage);
Jason Wang043d2222018-09-12 11:17:07 +08002543 }
2544
2545 if (flush)
2546 xdp_do_flush_map();
2547
2548 rcu_read_unlock();
2549 local_bh_enable();
2550
Jason Wangf9e06c42018-11-15 17:43:10 +08002551 tun_put_page(&tpage);
2552
Jason Wang043d2222018-09-12 11:17:07 +08002553 ret = total_len;
2554 goto out;
2555 }
Jason Wangfe8dd452018-09-12 11:17:06 +08002556
2557 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter,
Jason Wang5503fce2017-01-18 15:02:03 +08002558 m->msg_flags & MSG_DONTWAIT,
2559 m->msg_flags & MSG_MORE);
Jason Wang043d2222018-09-12 11:17:07 +08002560out:
Jason Wang54f968d2012-10-31 19:45:57 +00002561 tun_put(tun);
2562 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002563}
2564
Ying Xue1b784142015-03-02 15:37:48 +08002565static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002566 int flags)
2567{
Jason Wang54f968d2012-10-31 19:45:57 +00002568 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002569 struct tun_struct *tun = tun_get(tfile);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002570 void *ptr = m->msg_control;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002571 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00002572
Wei Xuc33ee152017-12-01 05:10:37 -05002573 if (!tun) {
2574 ret = -EBADFD;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002575 goto out_free;
Wei Xuc33ee152017-12-01 05:10:37 -05002576 }
Jason Wang54f968d2012-10-31 19:45:57 +00002577
Richard Cochraneda29772013-07-19 19:40:10 +02002578 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00002579 ret = -EINVAL;
Wei Xuc33ee152017-12-01 05:10:37 -05002580 goto out_put_tun;
Gao feng3811ae72013-04-24 21:59:23 +00002581 }
Richard Cochraneda29772013-07-19 19:40:10 +02002582 if (flags & MSG_ERRQUEUE) {
2583 ret = sock_recv_errqueue(sock->sk, m, total_len,
2584 SOL_PACKET, TUN_TX_TIMESTAMP);
2585 goto out;
2586 }
Jason Wangfc72d1d2018-01-04 11:14:28 +08002587 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr);
Alex Gartrell87897932014-12-25 23:05:03 -08002588 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05002589 m->msg_flags |= MSG_TRUNC;
2590 ret = flags & MSG_TRUNC ? ret : total_len;
2591 }
Gao feng3811ae72013-04-24 21:59:23 +00002592out:
Jason Wang54f968d2012-10-31 19:45:57 +00002593 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002594 return ret;
Wei Xuc33ee152017-12-01 05:10:37 -05002595
2596out_put_tun:
2597 tun_put(tun);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002598out_free:
2599 tun_ptr_free(ptr);
Wei Xuc33ee152017-12-01 05:10:37 -05002600 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002601}
2602
Jason Wangfc72d1d2018-01-04 11:14:28 +08002603static int tun_ptr_peek_len(void *ptr)
2604{
2605 if (likely(ptr)) {
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002606 if (tun_is_xdp_frame(ptr)) {
2607 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002608
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002609 return xdpf->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002610 }
2611 return __skb_array_len_with_tag(ptr);
2612 } else {
2613 return 0;
2614 }
2615}
2616
Jason Wang1576d982016-06-30 14:45:36 +08002617static int tun_peek_len(struct socket *sock)
2618{
2619 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2620 struct tun_struct *tun;
2621 int ret = 0;
2622
yuan linyu9484dc72017-09-23 22:36:52 +08002623 tun = tun_get(tfile);
Jason Wang1576d982016-06-30 14:45:36 +08002624 if (!tun)
2625 return 0;
2626
Jason Wangfc72d1d2018-01-04 11:14:28 +08002627 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len);
Jason Wang1576d982016-06-30 14:45:36 +08002628 tun_put(tun);
2629
2630 return ret;
2631}
2632
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002633/* Ops structure to mimic raw sockets with tun */
2634static const struct proto_ops tun_socket_ops = {
Jason Wang1576d982016-06-30 14:45:36 +08002635 .peek_len = tun_peek_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002636 .sendmsg = tun_sendmsg,
2637 .recvmsg = tun_recvmsg,
2638};
2639
Herbert Xu33dccbb2009-02-05 21:25:32 -08002640static struct proto tun_proto = {
2641 .name = "tun",
2642 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00002643 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08002644};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002645
David Woodhouse980c9e82009-05-09 22:54:21 -07002646static int tun_flags(struct tun_struct *tun)
2647{
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02002648 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07002649}
2650
2651static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
2652 char *buf)
2653{
2654 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2655 return sprintf(buf, "0x%x\n", tun_flags(tun));
2656}
2657
2658static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
2659 char *buf)
2660{
2661 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002662 return uid_valid(tun->owner)?
2663 sprintf(buf, "%u\n",
2664 from_kuid_munged(current_user_ns(), tun->owner)):
2665 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002666}
2667
2668static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
2669 char *buf)
2670{
2671 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002672 return gid_valid(tun->group) ?
2673 sprintf(buf, "%u\n",
2674 from_kgid_munged(current_user_ns(), tun->group)):
2675 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002676}
2677
2678static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
2679static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
2680static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
2681
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002682static struct attribute *tun_dev_attrs[] = {
2683 &dev_attr_tun_flags.attr,
2684 &dev_attr_owner.attr,
2685 &dev_attr_group.attr,
2686 NULL
2687};
2688
2689static const struct attribute_group tun_attr_group = {
2690 .attrs = tun_dev_attrs
2691};
2692
Pavel Emelyanovd647a592008-04-16 00:41:16 -07002693static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
2695 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00002696 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 struct net_device *dev;
2698 int err;
2699
Jason Wang7c0c3b12013-01-11 16:59:33 +00002700 if (tfile->detached)
2701 return -EINVAL;
2702
Petar Penkov90e33d42017-09-22 13:49:15 -07002703 if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
2704 if (!capable(CAP_NET_ADMIN))
2705 return -EPERM;
2706
2707 if (!(ifr->ifr_flags & IFF_NAPI) ||
2708 (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
2709 return -EINVAL;
2710 }
2711
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002712 dev = __dev_get_by_name(net, ifr->ifr_name);
2713 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07002714 if (ifr->ifr_flags & IFF_TUN_EXCL)
2715 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002716 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2717 tun = netdev_priv(dev);
2718 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2719 tun = netdev_priv(dev);
2720 else
2721 return -EINVAL;
2722
Jason Wang8e6d91a2013-05-28 18:32:11 +00002723 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002724 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00002725 return -EINVAL;
2726
Jason Wangcde8b152012-10-31 19:46:01 +00002727 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04002728 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002729 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04002730 if (err < 0)
2731 return err;
2732
Petar Penkov94317092017-09-22 13:49:14 -07002733 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002734 ifr->ifr_flags & IFF_NAPI,
Yang Yingliang77f22f92019-09-10 18:56:57 +08002735 ifr->ifr_flags & IFF_NAPI_FRAGS, true);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00002736 if (err < 0)
2737 return err;
Jason Wang4008e972012-12-13 23:53:30 +00002738
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002739 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00002740 (tun->numqueues + tun->numdisabled > 1)) {
2741 /* One or more queue has already been attached, no need
2742 * to initialize the device again.
2743 */
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002744 netdev_state_change(dev);
Jason Wange8dbad62013-04-22 20:40:39 +00002745 return 0;
2746 }
Sabrina Dubroca9fffc5c2018-04-10 16:28:55 +02002747
2748 tun->flags = (tun->flags & ~TUN_FEATURES) |
2749 (ifr->ifr_flags & TUN_FEATURES);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002750
2751 netdev_state_change(dev);
2752 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 char *name;
2754 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00002755 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2756 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Eric W. Biedermanc260b772012-11-18 21:34:11 +00002758 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002759 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04002760 err = security_tun_dev_create();
2761 if (err < 0)
2762 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 /* Set dev type */
2765 if (ifr->ifr_flags & IFF_TUN) {
2766 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002767 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 name = "tun%d";
2769 } else if (ifr->ifr_flags & IFF_TAP) {
2770 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002771 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002773 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00002774 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (*ifr->ifr_name)
2777 name = ifr->ifr_name;
2778
Jason Wangc8d68e62012-10-31 19:46:00 +00002779 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02002780 NET_NAME_UNKNOWN, tun_setup, queues,
2781 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00002782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (!dev)
2784 return -ENOMEM;
Cong Wang0ad646c2017-10-13 11:58:53 -07002785 err = dev_get_valid_name(net, dev, name);
Julien Gomes5c25f652017-10-25 11:50:50 -07002786 if (err < 0)
Cong Wang0ad646c2017-10-13 11:58:53 -07002787 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07002789 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002790 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002791 dev->ifindex = tfile->ifindex;
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002792 dev->sysfs_groups[0] = &tun_attr_group;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08002793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 tun = netdev_priv(dev);
2795 tun->dev = dev;
2796 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002797 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002798 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Paolo Abenieaea34b2016-02-26 10:45:40 +01002800 tun->align = NET_SKB_PAD;
Jason Wang54f968d2012-10-31 19:45:57 +00002801 tun->filter_attached = false;
2802 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Jason Wang5503fce2017-01-18 15:02:03 +08002803 tun->rx_batched = 0;
Jason Wang96f84062017-12-04 17:31:23 +08002804 RCU_INIT_POINTER(tun->steering_prog, NULL);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002805
Paolo Abeni608b9972016-04-13 10:52:20 +02002806 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2807 if (!tun->pcpu_stats) {
2808 err = -ENOMEM;
2809 goto err_free_dev;
2810 }
2811
Jason Wang96442e422012-10-31 19:46:02 +00002812 spin_lock_init(&tun->lock);
2813
Paul Moore5dbbaf22013-01-14 07:12:19 +00002814 err = security_tun_dev_alloc_security(&tun->security);
2815 if (err < 0)
Paolo Abeni608b9972016-04-13 10:52:20 +02002816 goto err_free_stat;
Paul Moore2b980db2009-08-28 18:12:43 -04002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04002819 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00002820
Michał Mirosław88255372011-04-19 06:13:10 +00002821 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08002822 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2823 NETIF_F_HW_VLAN_STAG_TX;
Paolo Abeni2a2bbf12016-04-14 18:39:39 +02002824 dev->features = dev->hw_features | NETIF_F_LLTX;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09002825 dev->vlan_features = dev->features &
2826 ~(NETIF_F_HW_VLAN_CTAG_TX |
2827 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00002828
Sabrina Dubroca9fffc5c2018-04-10 16:28:55 +02002829 tun->flags = (tun->flags & ~TUN_FEATURES) |
2830 (ifr->ifr_flags & TUN_FEATURES);
2831
Jason Wang4008e972012-12-13 23:53:30 +00002832 INIT_LIST_HEAD(&tun->disabled);
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002833 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI,
Yang Yingliang77f22f92019-09-10 18:56:57 +08002834 ifr->ifr_flags & IFF_NAPI_FRAGS, false);
Jason Wangeb0fb362012-12-02 17:19:45 +00002835 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002836 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00002837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 err = register_netdevice(tun->dev);
2839 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002840 goto err_detach;
Yang Yingliang77f22f92019-09-10 18:56:57 +08002841 /* free_netdev() won't check refcnt, to aovid race
2842 * with dev_put() we need publish tun after registration.
2843 */
2844 rcu_assign_pointer(tfile->tun, tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00002847 netif_carrier_on(tun->dev);
2848
Joe Perches6b8a66e2011-03-02 07:18:10 +00002849 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002851 /* Make sure persistent devices do not get stuck in
2852 * xoff state.
2853 */
2854 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00002855 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 strcpy(ifr->ifr_name, tun->dev->name);
2858 return 0;
2859
Jason Wang662ca432013-09-11 18:09:48 +08002860err_detach:
2861 tun_detach_all(dev);
Eric Dumazetff244c62017-08-18 13:39:56 -07002862 /* register_netdevice() already called tun_free_netdev() */
2863 goto err_free_dev;
2864
Jason Wang662ca432013-09-11 18:09:48 +08002865err_free_flow:
2866 tun_flow_uninit(tun);
2867 security_tun_dev_free_security(tun->security);
Paolo Abeni608b9972016-04-13 10:52:20 +02002868err_free_stat:
2869 free_percpu(tun->pcpu_stats);
Jason Wang662ca432013-09-11 18:09:48 +08002870err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return err;
2873}
2874
Kirill Tkhai12132762019-03-20 12:16:53 +03002875static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07002876{
Joe Perches6b8a66e2011-03-02 07:18:10 +00002877 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07002878
2879 strcpy(ifr->ifr_name, tun->dev->name);
2880
David Woodhouse980c9e82009-05-09 22:54:21 -07002881 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07002882
Mark McLoughline3b99552008-08-15 15:09:56 -07002883}
2884
Rusty Russell5228ddc2008-07-03 03:46:16 -07002885/* This is like a cut-down ethtool ops, except done via tun fd so no
2886 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00002887static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07002888{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002889 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002890
2891 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00002892 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002893 arg &= ~TUN_F_CSUM;
2894
2895 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2896 if (arg & TUN_F_TSO_ECN) {
2897 features |= NETIF_F_TSO_ECN;
2898 arg &= ~TUN_F_TSO_ECN;
2899 }
2900 if (arg & TUN_F_TSO4)
2901 features |= NETIF_F_TSO;
2902 if (arg & TUN_F_TSO6)
2903 features |= NETIF_F_TSO6;
2904 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2905 }
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05002906
2907 arg &= ~TUN_F_UFO;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002908 }
2909
2910 /* This gives the user a way to test for new features in future by
2911 * trying to set them. */
2912 if (arg)
2913 return -EINVAL;
2914
Michał Mirosław88255372011-04-19 06:13:10 +00002915 tun->set_features = features;
Yaroslav Isakov09050952017-03-16 22:44:10 +03002916 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2917 tun->dev->wanted_features |= features;
Michał Mirosław88255372011-04-19 06:13:10 +00002918 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07002919
2920 return 0;
2921}
2922
Jason Wangc8d68e62012-10-31 19:46:00 +00002923static void tun_detach_filter(struct tun_struct *tun, int n)
2924{
2925 int i;
2926 struct tun_file *tfile;
2927
2928 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002929 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002930 lock_sock(tfile->socket.sk);
2931 sk_detach_filter(tfile->socket.sk);
2932 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002933 }
2934
2935 tun->filter_attached = false;
2936}
2937
2938static int tun_attach_filter(struct tun_struct *tun)
2939{
2940 int i, ret = 0;
2941 struct tun_file *tfile;
2942
2943 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002944 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002945 lock_sock(tfile->socket.sk);
2946 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2947 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002948 if (ret) {
2949 tun_detach_filter(tun, i);
2950 return ret;
2951 }
2952 }
2953
2954 tun->filter_attached = true;
2955 return ret;
2956}
2957
2958static void tun_set_sndbuf(struct tun_struct *tun)
2959{
2960 struct tun_file *tfile;
2961 int i;
2962
2963 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002964 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00002965 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2966 }
2967}
2968
Jason Wangcde8b152012-10-31 19:46:01 +00002969static int tun_set_queue(struct file *file, struct ifreq *ifr)
2970{
2971 struct tun_file *tfile = file->private_data;
2972 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00002973 int ret = 0;
2974
2975 rtnl_lock();
2976
2977 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00002978 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002979 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00002980 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002981 goto unlock;
2982 }
2983 ret = security_tun_dev_attach_queue(tun->security);
2984 if (ret < 0)
2985 goto unlock;
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002986 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI,
Yang Yingliang77f22f92019-09-10 18:56:57 +08002987 tun->flags & IFF_NAPI_FRAGS, true);
Jason Wang4008e972012-12-13 23:53:30 +00002988 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002989 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002990 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00002991 ret = -EINVAL;
2992 else
2993 __tun_detach(tfile, false);
2994 } else
Jason Wangcde8b152012-10-31 19:46:01 +00002995 ret = -EINVAL;
2996
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002997 if (ret >= 0)
2998 netdev_state_change(tun->dev);
2999
Paul Moore5dbbaf22013-01-14 07:12:19 +00003000unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00003001 rtnl_unlock();
3002 return ret;
3003}
3004
Jason Wangcd5681d2018-01-16 16:31:01 +08003005static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog **prog_p,
3006 void __user *data)
Jason Wang96f84062017-12-04 17:31:23 +08003007{
3008 struct bpf_prog *prog;
3009 int fd;
3010
3011 if (copy_from_user(&fd, data, sizeof(fd)))
3012 return -EFAULT;
3013
3014 if (fd == -1) {
3015 prog = NULL;
3016 } else {
3017 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
3018 if (IS_ERR(prog))
3019 return PTR_ERR(prog);
3020 }
3021
Jason Wangcd5681d2018-01-16 16:31:01 +08003022 return __tun_set_ebpf(tun, prog_p, prog);
Jason Wang96f84062017-12-04 17:31:23 +08003023}
3024
Arnd Bergmann50857e22009-11-06 22:52:32 -08003025static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
3026 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00003028 struct tun_file *tfile = file->private_data;
Kirill Tkhaif6637062018-05-08 19:21:34 +03003029 struct net *net = sock_net(&tfile->sk);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003030 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 void __user* argp = (void __user*)arg;
Nicolas Dichtel26d31922018-11-28 19:12:56 +01003032 unsigned int ifindex, carrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08003034 kuid_t owner;
3035 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003036 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02003037 int vnet_hdr_sz;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02003038 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003039 int ret;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003040 bool do_notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Kirill Tkhaif2780d62018-02-14 16:40:14 +03003042 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
3043 (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08003044 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07003046 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00003047 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07003048 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003049 if (cmd == TUNGETFEATURES) {
3050 /* Currently this just means: "what IFF flags are valid?".
3051 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02003052 * TUNSETIFF.
3053 */
3054 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00003055 (unsigned int __user*)argp);
Kirill Tkhaif6637062018-05-08 19:21:34 +03003056 } else if (cmd == TUNSETQUEUE) {
Jason Wangcde8b152012-10-31 19:46:01 +00003057 return tun_set_queue(file, &ifr);
Kirill Tkhaif6637062018-05-08 19:21:34 +03003058 } else if (cmd == SIOCGSKNS) {
3059 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3060 return -EPERM;
3061 return open_related_ns(&net->ns, get_net_ns);
3062 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003063
Jason Wangc8d68e62012-10-31 19:46:00 +00003064 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00003065 rtnl_lock();
3066
yuan linyu9484dc72017-09-23 22:36:52 +08003067 tun = tun_get(tfile);
Gao Feng0f16bc12016-10-25 22:26:09 +08003068 if (cmd == TUNSETIFF) {
3069 ret = -EEXIST;
3070 if (tun)
3071 goto unlock;
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 ifr.ifr_name[IFNAMSIZ-1] = '\0';
3074
Kirill Tkhaif2780d62018-02-14 16:40:14 +03003075 ret = tun_set_iff(net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Herbert Xu876bfd42009-08-06 14:22:44 +00003077 if (ret)
3078 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Arnd Bergmann50857e22009-11-06 22:52:32 -08003080 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00003081 ret = -EFAULT;
3082 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04003084 if (cmd == TUNSETIFINDEX) {
3085 ret = -EPERM;
3086 if (tun)
3087 goto unlock;
3088
3089 ret = -EFAULT;
3090 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
3091 goto unlock;
3092
3093 ret = 0;
3094 tfile->ifindex = ifindex;
3095 goto unlock;
3096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Herbert Xu876bfd42009-08-06 14:22:44 +00003098 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00003100 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Jason Wang1e588332012-10-31 19:45:56 +00003102 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Kirill Tkhai0c3e0e32019-03-20 12:16:42 +03003104 net = dev_net(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003105 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07003107 case TUNGETIFF:
Kirill Tkhai12132762019-03-20 12:16:53 +03003108 tun_get_iff(tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07003109
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04003110 if (tfile->detached)
3111 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04003112 if (!tfile->socket.sk->sk_filter)
3113 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04003114
Arnd Bergmann50857e22009-11-06 22:52:32 -08003115 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00003116 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07003117 break;
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 case TUNSETNOCSUM:
3120 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Michał Mirosław88255372011-04-19 06:13:10 +00003122 /* [unimplemented] */
3123 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00003124 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 break;
3126
3127 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00003128 /* Disable/Enable persist mode. Keep an extra reference to the
3129 * module to prevent the module being unprobed.
3130 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003131 if (arg && !(tun->flags & IFF_PERSIST)) {
3132 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00003133 __module_get(THIS_MODULE);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003134 do_notify = true;
Jason Wangdd38bd82013-01-11 16:59:34 +00003135 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003136 if (!arg && (tun->flags & IFF_PERSIST)) {
3137 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00003138 module_put(THIS_MODULE);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003139 do_notify = true;
Jason Wang54f968d2012-10-31 19:45:57 +00003140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Joe Perches6b8a66e2011-03-02 07:18:10 +00003142 tun_debug(KERN_INFO, tun, "persist %s\n",
3143 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 break;
3145
3146 case TUNSETOWNER:
3147 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08003148 owner = make_kuid(current_user_ns(), arg);
3149 if (!uid_valid(owner)) {
3150 ret = -EINVAL;
3151 break;
3152 }
3153 tun->owner = owner;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003154 do_notify = true;
Jason Wang1e588332012-10-31 19:45:56 +00003155 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08003156 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 break;
3158
Guido Guenther8c644622007-07-02 22:50:25 -07003159 case TUNSETGROUP:
3160 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08003161 group = make_kgid(current_user_ns(), arg);
3162 if (!gid_valid(group)) {
3163 ret = -EINVAL;
3164 break;
3165 }
3166 tun->group = group;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003167 do_notify = true;
Jason Wang1e588332012-10-31 19:45:56 +00003168 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08003169 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07003170 break;
3171
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003172 case TUNSETLINK:
3173 /* Only allow setting the type when the interface is down */
3174 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003175 tun_debug(KERN_INFO, tun,
3176 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07003177 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003178 } else {
3179 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00003180 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
3181 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07003182 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003183 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003184 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186#ifdef TUN_DEBUG
3187 case TUNSETDEBUG:
3188 tun->debug = arg;
3189 break;
3190#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07003191 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00003192 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003193 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07003194
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003195 case TUNSETTXFILTER:
3196 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00003197 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003198 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00003199 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07003200 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003204 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003205 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
3206 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08003207 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00003208 ret = -EFAULT;
3209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003212 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00003213 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
3214 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08003215
Petr Machata3a37a962018-12-13 11:54:30 +00003216 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr, NULL);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003217 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003218
3219 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00003220 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003221 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
3222 ret = -EFAULT;
3223 break;
3224
3225 case TUNSETSNDBUF:
3226 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
3227 ret = -EFAULT;
3228 break;
3229 }
Craig Gallek931619222017-10-30 18:50:11 -04003230 if (sndbuf <= 0) {
3231 ret = -EINVAL;
3232 break;
3233 }
Herbert Xu33dccbb2009-02-05 21:25:32 -08003234
Jason Wangc8d68e62012-10-31 19:46:00 +00003235 tun->sndbuf = sndbuf;
3236 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08003237 break;
3238
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02003239 case TUNGETVNETHDRSZ:
3240 vnet_hdr_sz = tun->vnet_hdr_sz;
3241 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
3242 ret = -EFAULT;
3243 break;
3244
3245 case TUNSETVNETHDRSZ:
3246 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
3247 ret = -EFAULT;
3248 break;
3249 }
3250 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
3251 ret = -EINVAL;
3252 break;
3253 }
3254
3255 tun->vnet_hdr_sz = vnet_hdr_sz;
3256 break;
3257
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02003258 case TUNGETVNETLE:
3259 le = !!(tun->flags & TUN_VNET_LE);
3260 if (put_user(le, (int __user *)argp))
3261 ret = -EFAULT;
3262 break;
3263
3264 case TUNSETVNETLE:
3265 if (get_user(le, (int __user *)argp)) {
3266 ret = -EFAULT;
3267 break;
3268 }
3269 if (le)
3270 tun->flags |= TUN_VNET_LE;
3271 else
3272 tun->flags &= ~TUN_VNET_LE;
3273 break;
3274
Greg Kurz8b8e6582015-04-24 14:50:36 +02003275 case TUNGETVNETBE:
3276 ret = tun_get_vnet_be(tun, argp);
3277 break;
3278
3279 case TUNSETVNETBE:
3280 ret = tun_set_vnet_be(tun, argp);
3281 break;
3282
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003283 case TUNATTACHFILTER:
3284 /* Can be set only for TAPs */
3285 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003286 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003287 break;
3288 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00003289 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003290 break;
3291
Jason Wangc8d68e62012-10-31 19:46:00 +00003292 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003293 break;
3294
3295 case TUNDETACHFILTER:
3296 /* Can be set only for TAPs */
3297 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003298 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003299 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00003300 ret = 0;
3301 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003302 break;
3303
Pavel Emelyanov76975e92013-08-21 14:32:39 +04003304 case TUNGETFILTER:
3305 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003306 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04003307 break;
3308 ret = -EFAULT;
3309 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
3310 break;
3311 ret = 0;
3312 break;
3313
Jason Wang96f84062017-12-04 17:31:23 +08003314 case TUNSETSTEERINGEBPF:
Jason Wangcd5681d2018-01-16 16:31:01 +08003315 ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
Jason Wang96f84062017-12-04 17:31:23 +08003316 break;
3317
Jason Wangaff3d702018-01-16 16:31:02 +08003318 case TUNSETFILTEREBPF:
3319 ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
3320 break;
3321
Nicolas Dichtel26d31922018-11-28 19:12:56 +01003322 case TUNSETCARRIER:
3323 ret = -EFAULT;
3324 if (copy_from_user(&carrier, argp, sizeof(carrier)))
3325 goto unlock;
3326
3327 ret = tun_net_change_carrier(tun->dev, (bool)carrier);
3328 break;
3329
Kirill Tkhai0c3e0e32019-03-20 12:16:42 +03003330 case TUNGETDEVNETNS:
3331 ret = -EPERM;
3332 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3333 goto unlock;
3334 ret = open_related_ns(&net->ns, get_net_ns);
3335 break;
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00003338 ret = -EINVAL;
3339 break;
Joe Perchesee289b62010-05-17 22:47:34 -07003340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003342 if (do_notify)
3343 netdev_state_change(tun->dev);
3344
Herbert Xu876bfd42009-08-06 14:22:44 +00003345unlock:
3346 rtnl_unlock();
3347 if (tun)
3348 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
Arnd Bergmann50857e22009-11-06 22:52:32 -08003352static long tun_chr_ioctl(struct file *file,
3353 unsigned int cmd, unsigned long arg)
3354{
3355 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
3356}
3357
3358#ifdef CONFIG_COMPAT
3359static long tun_chr_compat_ioctl(struct file *file,
3360 unsigned int cmd, unsigned long arg)
3361{
3362 switch (cmd) {
3363 case TUNSETIFF:
3364 case TUNGETIFF:
3365 case TUNSETTXFILTER:
3366 case TUNGETSNDBUF:
3367 case TUNSETSNDBUF:
3368 case SIOCGIFHWADDR:
3369 case SIOCSIFHWADDR:
3370 arg = (unsigned long)compat_ptr(arg);
3371 break;
3372 default:
3373 arg = (compat_ulong_t)arg;
3374 break;
3375 }
3376
3377 /*
3378 * compat_ifreq is shorter than ifreq, so we must not access beyond
3379 * the end of that structure. All fields that are used in this
3380 * driver are compatible though, we don't need to convert the
3381 * contents.
3382 */
3383 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
3384}
3385#endif /* CONFIG_COMPAT */
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387static int tun_chr_fasync(int fd, struct file *file, int on)
3388{
Jason Wang54f968d2012-10-31 19:45:57 +00003389 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 int ret;
3391
Jason Wang54f968d2012-10-31 19:45:57 +00003392 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06003393 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 if (on) {
Eric W. Biederman01919132017-07-16 22:05:57 -05003396 __f_setown(file, task_pid(current), PIDTYPE_TGID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00003397 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003398 } else
Jason Wang54f968d2012-10-31 19:45:57 +00003399 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06003400 ret = 0;
3401out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06003402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
3405static int tun_chr_open(struct inode *inode, struct file * file)
3406{
Eric W. Biederman140e807da2015-05-08 21:07:08 -05003407 struct net *net = current->nsproxy->net_ns;
Eric W. Biederman631ab462009-01-20 11:00:40 +00003408 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07003409
Joe Perches6b8a66e2011-03-02 07:18:10 +00003410 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00003411
Eric W. Biederman140e807da2015-05-08 21:07:08 -05003412 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003413 &tun_proto, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003414 if (!tfile)
3415 return -ENOMEM;
Jason Wangb196d882018-05-11 10:49:25 +08003416 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) {
3417 sk_free(&tfile->sk);
3418 return -ENOMEM;
3419 }
3420
Eric Dumazetc7256f52018-09-28 14:51:48 -07003421 mutex_init(&tfile->napi_mutex);
Monam Agarwalc9566742014-03-24 00:02:32 +05303422 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang54f968d2012-10-31 19:45:57 +00003423 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04003424 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00003425
Al Viro333f7902019-07-05 20:14:16 +01003426 init_waitqueue_head(&tfile->socket.wq.wait);
Jason Wang54f968d2012-10-31 19:45:57 +00003427
3428 tfile->socket.file = file;
3429 tfile->socket.ops = &tun_socket_ops;
3430
3431 sock_init_data(&tfile->socket, &tfile->sk);
Jason Wang54f968d2012-10-31 19:45:57 +00003432
3433 tfile->sk.sk_write_space = tun_sock_write_space;
3434 tfile->sk.sk_sndbuf = INT_MAX;
3435
Eric W. Biederman631ab462009-01-20 11:00:40 +00003436 file->private_data = tfile;
Jason Wang4008e972012-12-13 23:53:30 +00003437 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00003438
Jason Wang19a6afb2013-06-08 14:17:41 +08003439 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return 0;
3442}
3443
3444static int tun_chr_close(struct inode *inode, struct file *file)
3445{
Eric W. Biederman631ab462009-01-20 11:00:40 +00003446 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Jason Wangc8d68e62012-10-31 19:46:00 +00003448 tun_detach(tfile, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 return 0;
3451}
3452
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003453#ifdef CONFIG_PROC_FS
yuan linyu9484dc72017-09-23 22:36:52 +08003454static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003455{
yuan linyu9484dc72017-09-23 22:36:52 +08003456 struct tun_file *tfile = file->private_data;
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003457 struct tun_struct *tun;
3458 struct ifreq ifr;
3459
3460 memset(&ifr, 0, sizeof(ifr));
3461
3462 rtnl_lock();
yuan linyu9484dc72017-09-23 22:36:52 +08003463 tun = tun_get(tfile);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003464 if (tun)
Kirill Tkhai12132762019-03-20 12:16:53 +03003465 tun_get_iff(tun, &ifr);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003466 rtnl_unlock();
3467
3468 if (tun)
3469 tun_put(tun);
3470
Joe Perchesa3816ab2014-09-29 16:08:25 -07003471 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003472}
3473#endif
3474
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003475static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003476 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05003478 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04003479 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08003481 .unlocked_ioctl = tun_chr_ioctl,
3482#ifdef CONFIG_COMPAT
3483 .compat_ioctl = tun_chr_compat_ioctl,
3484#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 .open = tun_chr_open,
3486 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003487 .fasync = tun_chr_fasync,
3488#ifdef CONFIG_PROC_FS
3489 .show_fdinfo = tun_chr_show_fdinfo,
3490#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491};
3492
3493static struct miscdevice tun_miscdev = {
3494 .minor = TUN_MINOR,
3495 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02003496 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498};
3499
3500/* ethtool interface */
3501
Chas Williams4e24f2d2018-06-02 17:49:53 -04003502static void tun_default_link_ksettings(struct net_device *dev,
3503 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
Philippe Reynes29ccc492017-03-11 22:03:50 +01003505 ethtool_link_ksettings_zero_link_mode(cmd, supported);
3506 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
3507 cmd->base.speed = SPEED_10;
3508 cmd->base.duplex = DUPLEX_FULL;
3509 cmd->base.port = PORT_TP;
3510 cmd->base.phy_address = 0;
3511 cmd->base.autoneg = AUTONEG_DISABLE;
Chas Williams4e24f2d2018-06-02 17:49:53 -04003512}
3513
3514static int tun_get_link_ksettings(struct net_device *dev,
3515 struct ethtool_link_ksettings *cmd)
3516{
3517 struct tun_struct *tun = netdev_priv(dev);
3518
3519 memcpy(cmd, &tun->link_ksettings, sizeof(*cmd));
3520 return 0;
3521}
3522
3523static int tun_set_link_ksettings(struct net_device *dev,
3524 const struct ethtool_link_ksettings *cmd)
3525{
3526 struct tun_struct *tun = netdev_priv(dev);
3527
3528 memcpy(&tun->link_ksettings, cmd, sizeof(*cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return 0;
3530}
3531
3532static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3533{
3534 struct tun_struct *tun = netdev_priv(dev);
3535
Rick Jones33a5ba12011-11-15 14:59:53 +00003536 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
3537 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003540 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00003541 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003543 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00003544 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 break;
3546 }
3547}
3548
3549static u32 tun_get_msglevel(struct net_device *dev)
3550{
3551#ifdef TUN_DEBUG
3552 struct tun_struct *tun = netdev_priv(dev);
3553 return tun->debug;
3554#else
3555 return -EOPNOTSUPP;
3556#endif
3557}
3558
3559static void tun_set_msglevel(struct net_device *dev, u32 value)
3560{
3561#ifdef TUN_DEBUG
3562 struct tun_struct *tun = netdev_priv(dev);
3563 tun->debug = value;
3564#endif
3565}
3566
Jason Wang5503fce2017-01-18 15:02:03 +08003567static int tun_get_coalesce(struct net_device *dev,
3568 struct ethtool_coalesce *ec)
3569{
3570 struct tun_struct *tun = netdev_priv(dev);
3571
3572 ec->rx_max_coalesced_frames = tun->rx_batched;
3573
3574 return 0;
3575}
3576
3577static int tun_set_coalesce(struct net_device *dev,
3578 struct ethtool_coalesce *ec)
3579{
3580 struct tun_struct *tun = netdev_priv(dev);
3581
3582 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
3583 tun->rx_batched = NAPI_POLL_WEIGHT;
3584 else
3585 tun->rx_batched = ec->rx_max_coalesced_frames;
3586
3587 return 0;
3588}
3589
Jeff Garzik7282d492006-09-13 14:30:00 -04003590static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 .get_drvinfo = tun_get_drvinfo,
3592 .get_msglevel = tun_get_msglevel,
3593 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00003594 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02003595 .get_ts_info = ethtool_op_get_ts_info,
Jason Wang5503fce2017-01-18 15:02:03 +08003596 .get_coalesce = tun_get_coalesce,
3597 .set_coalesce = tun_set_coalesce,
Philippe Reynes29ccc492017-03-11 22:03:50 +01003598 .get_link_ksettings = tun_get_link_ksettings,
Chas Williams4e24f2d2018-06-02 17:49:53 -04003599 .set_link_ksettings = tun_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600};
3601
Jason Wang1576d982016-06-30 14:45:36 +08003602static int tun_queue_resize(struct tun_struct *tun)
3603{
3604 struct net_device *dev = tun->dev;
3605 struct tun_file *tfile;
Jason Wang5990a302018-01-04 11:14:27 +08003606 struct ptr_ring **rings;
Jason Wang1576d982016-06-30 14:45:36 +08003607 int n = tun->numqueues + tun->numdisabled;
3608 int ret, i;
3609
Jason Wang5990a302018-01-04 11:14:27 +08003610 rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
3611 if (!rings)
Jason Wang1576d982016-06-30 14:45:36 +08003612 return -ENOMEM;
3613
3614 for (i = 0; i < tun->numqueues; i++) {
3615 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wang5990a302018-01-04 11:14:27 +08003616 rings[i] = &tfile->tx_ring;
Jason Wang1576d982016-06-30 14:45:36 +08003617 }
3618 list_for_each_entry(tfile, &tun->disabled, next)
Jason Wang5990a302018-01-04 11:14:27 +08003619 rings[i++] = &tfile->tx_ring;
Jason Wang1576d982016-06-30 14:45:36 +08003620
Jason Wang5990a302018-01-04 11:14:27 +08003621 ret = ptr_ring_resize_multiple(rings, n,
3622 dev->tx_queue_len, GFP_KERNEL,
Jason Wangfc72d1d2018-01-04 11:14:28 +08003623 tun_ptr_free);
Jason Wang1576d982016-06-30 14:45:36 +08003624
Jason Wang5990a302018-01-04 11:14:27 +08003625 kfree(rings);
Jason Wang1576d982016-06-30 14:45:36 +08003626 return ret;
3627}
3628
3629static int tun_device_event(struct notifier_block *unused,
3630 unsigned long event, void *ptr)
3631{
3632 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3633 struct tun_struct *tun = netdev_priv(dev);
Fei Li72b319d2019-06-17 21:26:36 +08003634 int i;
Jason Wang1576d982016-06-30 14:45:36 +08003635
Craig Gallek86dfb4ac2016-07-06 18:44:20 -04003636 if (dev->rtnl_link_ops != &tun_link_ops)
3637 return NOTIFY_DONE;
3638
Jason Wang1576d982016-06-30 14:45:36 +08003639 switch (event) {
3640 case NETDEV_CHANGE_TX_QUEUE_LEN:
3641 if (tun_queue_resize(tun))
3642 return NOTIFY_BAD;
3643 break;
Fei Li72b319d2019-06-17 21:26:36 +08003644 case NETDEV_UP:
3645 for (i = 0; i < tun->numqueues; i++) {
3646 struct tun_file *tfile;
3647
3648 tfile = rtnl_dereference(tun->tfiles[i]);
3649 tfile->socket.sk->sk_write_space(tfile->socket.sk);
3650 }
3651 break;
Jason Wang1576d982016-06-30 14:45:36 +08003652 default:
3653 break;
3654 }
3655
3656 return NOTIFY_DONE;
3657}
3658
3659static struct notifier_block tun_notifier_block __read_mostly = {
3660 .notifier_call = tun_device_event,
3661};
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663static int __init tun_init(void)
3664{
3665 int ret = 0;
3666
Joe Perches6b8a66e2011-03-02 07:18:10 +00003667 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003669 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003670 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003671 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003672 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003673 }
3674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003676 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003677 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003678 goto err_misc;
3679 }
Jason Wang1576d982016-06-30 14:45:36 +08003680
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003681 ret = register_netdevice_notifier(&tun_notifier_block);
3682 if (ret) {
3683 pr_err("Can't register netdevice notifier\n");
3684 goto err_notifier;
3685 }
3686
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003687 return 0;
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003688
3689err_notifier:
3690 misc_deregister(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003691err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003692 rtnl_link_unregister(&tun_link_ops);
3693err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return ret;
3695}
3696
3697static void tun_cleanup(void)
3698{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003699 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003700 rtnl_link_unregister(&tun_link_ops);
Jason Wang1576d982016-06-30 14:45:36 +08003701 unregister_netdevice_notifier(&tun_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003704/* Get an underlying socket object from tun file. Returns error unless file is
3705 * attached to a device. The returned object works like a packet socket, it
3706 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
3707 * holding a reference to the file for as long as the socket is in use. */
3708struct socket *tun_get_socket(struct file *file)
3709{
Jason Wang6e914fc2012-10-31 19:45:58 +00003710 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003711 if (file->f_op != &tun_fops)
3712 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00003713 tfile = file->private_data;
3714 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003715 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00003716 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003717}
3718EXPORT_SYMBOL_GPL(tun_get_socket);
3719
Jason Wang5990a302018-01-04 11:14:27 +08003720struct ptr_ring *tun_get_tx_ring(struct file *file)
Jason Wang83339c62017-05-17 12:14:41 +08003721{
3722 struct tun_file *tfile;
3723
3724 if (file->f_op != &tun_fops)
3725 return ERR_PTR(-EINVAL);
3726 tfile = file->private_data;
3727 if (!tfile)
3728 return ERR_PTR(-EBADFD);
Jason Wang5990a302018-01-04 11:14:27 +08003729 return &tfile->tx_ring;
Jason Wang83339c62017-05-17 12:14:41 +08003730}
Jason Wang5990a302018-01-04 11:14:27 +08003731EXPORT_SYMBOL_GPL(tun_get_tx_ring);
Jason Wang83339c62017-05-17 12:14:41 +08003732
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733module_init(tun_init);
3734module_exit(tun_cleanup);
3735MODULE_DESCRIPTION(DRV_DESCRIPTION);
3736MODULE_AUTHOR(DRV_COPYRIGHT);
3737MODULE_LICENSE("GPL");
3738MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02003739MODULE_ALIAS("devname:net/tun");