blob: 50e9cc19023a701bad861ac117665a024ba776b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16 */
17
18/*
19 * Changes:
20 *
Mike Kershawff4cc3a2005-09-01 17:40:05 -070021 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * Mark Smith <markzzzsmith@yahoo.com.au>
Joe Perches344dc8e2012-07-12 19:33:09 +000025 * Use eth_random_addr() for tap MAC address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
28 * Fixes in packet dropping, queue length setting and queue wakeup.
29 * Increased default tx queue length.
30 * Added ethtool API.
31 * Minor cleanups
32 *
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
35 */
36
Joe Perches6b8a66e2011-03-02 07:18:10 +000037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define DRV_NAME "tun"
40#define DRV_VERSION "1.6"
41#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
42#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/errno.h>
46#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010047#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/major.h>
49#include <linux/slab.h>
50#include <linux/poll.h>
51#include <linux/fcntl.h>
52#include <linux/init.h>
53#include <linux/skbuff.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/miscdevice.h>
57#include <linux/ethtool.h>
58#include <linux/rtnetlink.h>
Arnd Bergmann50857e22009-11-06 22:52:32 -080059#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/if.h>
61#include <linux/if_arp.h>
62#include <linux/if_ether.h>
63#include <linux/if_tun.h>
Jason Wang6680ec62013-07-25 13:00:33 +080064#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070066#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070067#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000068#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070069#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070070#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080071#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080072#include <net/sock.h>
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +020073#include <net/xdp.h>
Masatake YAMATO93e14b62014-01-29 16:43:31 +090074#include <linux/seq_file.h>
Herbert Xue0b46d02014-11-07 21:22:23 +080075#include <linux/uio.h>
Jason Wang1576d982016-06-30 14:45:36 +080076#include <linux/skb_array.h>
Jason Wang761876c2017-08-11 19:41:18 +080077#include <linux/bpf.h>
78#include <linux/bpf_trace.h>
Petar Penkov90e33d42017-09-22 13:49:15 -070079#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080081#include <linux/uaccess.h>
Kirill Tkhaif2780d62018-02-14 16:40:14 +030082#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Chas Williams4e24f2d2018-06-02 17:49:53 -040084static void tun_default_link_ksettings(struct net_device *dev,
85 struct ethtool_link_ksettings *cmd);
86
Rusty Russell14daa022008-04-12 18:48:58 -070087/* Uncomment to enable debugging */
88/* #define TUN_DEBUG 1 */
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#ifdef TUN_DEBUG
91static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070092
Joe Perches6b8a66e2011-03-02 07:18:10 +000093#define tun_debug(level, tun, fmt, args...) \
94do { \
95 if (tun->debug) \
96 netdev_printk(level, tun->dev, fmt, ##args); \
97} while (0)
98#define DBG1(level, fmt, args...) \
99do { \
100 if (debug == 2) \
101 printk(level fmt, ##args); \
102} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -0700103#else
Joe Perches6b8a66e2011-03-02 07:18:10 +0000104#define tun_debug(level, tun, fmt, args...) \
105do { \
106 if (0) \
107 netdev_printk(level, tun->dev, fmt, ##args); \
108} while (0)
109#define DBG1(level, fmt, args...) \
110do { \
111 if (0) \
112 printk(level fmt, ##args); \
113} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115
Jason Wang761876c2017-08-11 19:41:18 +0800116#define TUN_HEADROOM 256
Jason Wang7df13212017-09-04 11:36:08 +0800117#define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
Jason Wang66ccbc92017-08-11 19:41:16 +0800118
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +0200119/* TUN device flags */
120
121/* IFF_ATTACH_QUEUE is never stored in device flags,
122 * overload it to mean fasync when stored there.
123 */
124#define TUN_FASYNC IFF_ATTACH_QUEUE
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200125/* High bits in flags field are unused. */
126#define TUN_VNET_LE 0x80000000
Greg Kurz8b8e6582015-04-24 14:50:36 +0200127#define TUN_VNET_BE 0x40000000
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +0200128
129#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Petar Penkov90e33d42017-09-22 13:49:15 -0700130 IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS)
131
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000132#define GOODCOPY_LEN 128
133
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700134#define FLT_EXACT_COUNT 8
135struct tap_filter {
136 unsigned int count; /* Number of addrs. Zero means disabled */
137 u32 mask[2]; /* Mask of the hashed addrs */
138 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
139};
140
Pankaj Guptabaf71c52015-01-12 11:41:29 +0530141/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
142 * to max number of VCPUs in guest. */
143#define MAX_TAP_QUEUES 256
Jason Wangb8732fb2013-01-23 03:59:13 +0000144#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000145
Jason Wang96442e422012-10-31 19:46:02 +0000146#define TUN_FLOW_EXPIRE (3 * HZ)
147
Paolo Abeni608b9972016-04-13 10:52:20 +0200148struct tun_pcpu_stats {
149 u64 rx_packets;
150 u64 rx_bytes;
151 u64 tx_packets;
152 u64 tx_bytes;
153 struct u64_stats_sync syncp;
154 u32 rx_dropped;
155 u32 tx_dropped;
156 u32 rx_frame_errors;
157};
158
Jason Wang54f968d2012-10-31 19:45:57 +0000159/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800160 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000161 * to serve as one transmit queue for tuntap device. The sock_fprog and
162 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000163 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000164 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000165 *
166 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000167 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000168 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000169 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000170struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000171 struct sock sk;
172 struct socket socket;
173 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000174 struct tun_struct __rcu *tun;
Jason Wang54f968d2012-10-31 19:45:57 +0000175 struct fasync_struct *fasync;
176 /* only used for fasnyc */
177 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400178 union {
179 u16 queue_index;
180 unsigned int ifindex;
181 };
Petar Penkov94317092017-09-22 13:49:14 -0700182 struct napi_struct napi;
Eric Dumazetaec72f32017-10-18 12:12:09 -0700183 bool napi_enabled;
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700184 bool napi_frags_enabled;
Petar Penkov90e33d42017-09-22 13:49:15 -0700185 struct mutex napi_mutex; /* Protects access to the above napi */
Jason Wang4008e972012-12-13 23:53:30 +0000186 struct list_head next;
187 struct tun_struct *detached;
Jason Wang5990a302018-01-04 11:14:27 +0800188 struct ptr_ring tx_ring;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100189 struct xdp_rxq_info xdp_rxq;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000190};
191
Jason Wang96442e422012-10-31 19:46:02 +0000192struct tun_flow_entry {
193 struct hlist_node hash_link;
194 struct rcu_head rcu;
195 struct tun_struct *tun;
196
197 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800198 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000199 int queue_index;
200 unsigned long updated;
201};
202
203#define TUN_NUM_FLOW_ENTRIES 1024
Li RongQingf13b5462018-08-03 15:50:02 +0800204#define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1)
Jason Wang96442e422012-10-31 19:46:02 +0000205
Jason Wangcd5681d2018-01-16 16:31:01 +0800206struct tun_prog {
Jason Wang96f84062017-12-04 17:31:23 +0800207 struct rcu_head rcu;
208 struct bpf_prog *prog;
209};
210
Jason Wang54f968d2012-10-31 19:45:57 +0000211/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000212 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000213 * file were attached to a persist device.
214 */
Rusty Russell14daa022008-04-12 18:48:58 -0700215struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000216 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
217 unsigned int numqueues;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700218 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800219 kuid_t owner;
220 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700221
Rusty Russell14daa022008-04-12 18:48:58 -0700222 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000223 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000224#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
David S. Millerd591a1f2017-07-03 06:35:32 -0700225 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200226
Paolo Abenieaea34b2016-02-26 10:45:40 +0100227 int align;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200228 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000229 int sndbuf;
230 struct tap_filter txflt;
231 struct sock_fprog fprog;
232 /* protected by rtnl lock */
233 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700234#ifdef TUN_DEBUG
235 int debug;
236#endif
Jason Wang96442e422012-10-31 19:46:02 +0000237 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000238 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
239 struct timer_list flow_gc_timer;
240 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000241 unsigned int numdisabled;
242 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000243 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000244 u32 flow_count;
Jason Wang5503fce2017-01-18 15:02:03 +0800245 u32 rx_batched;
Paolo Abeni608b9972016-04-13 10:52:20 +0200246 struct tun_pcpu_stats __percpu *pcpu_stats;
Jason Wang761876c2017-08-11 19:41:18 +0800247 struct bpf_prog __rcu *xdp_prog;
Jason Wangcd5681d2018-01-16 16:31:01 +0800248 struct tun_prog __rcu *steering_prog;
Jason Wangaff3d702018-01-16 16:31:02 +0800249 struct tun_prog __rcu *filter_prog;
Chas Williams4e24f2d2018-06-02 17:49:53 -0400250 struct ethtool_link_ksettings link_ksettings;
Rusty Russell14daa022008-04-12 18:48:58 -0700251};
252
Jason Wangaff3d702018-01-16 16:31:02 +0800253struct veth {
254 __be16 h_vlan_proto;
255 __be16 h_vlan_TCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200258bool tun_is_xdp_frame(void *ptr)
Jason Wangfc72d1d2018-01-04 11:14:28 +0800259{
260 return (unsigned long)ptr & TUN_XDP_FLAG;
261}
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200262EXPORT_SYMBOL(tun_is_xdp_frame);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800263
264void *tun_xdp_to_ptr(void *ptr)
265{
266 return (void *)((unsigned long)ptr | TUN_XDP_FLAG);
267}
268EXPORT_SYMBOL(tun_xdp_to_ptr);
269
270void *tun_ptr_to_xdp(void *ptr)
271{
272 return (void *)((unsigned long)ptr & ~TUN_XDP_FLAG);
273}
274EXPORT_SYMBOL(tun_ptr_to_xdp);
275
Petar Penkov94317092017-09-22 13:49:14 -0700276static int tun_napi_receive(struct napi_struct *napi, int budget)
277{
278 struct tun_file *tfile = container_of(napi, struct tun_file, napi);
279 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
280 struct sk_buff_head process_queue;
281 struct sk_buff *skb;
282 int received = 0;
283
284 __skb_queue_head_init(&process_queue);
285
286 spin_lock(&queue->lock);
287 skb_queue_splice_tail_init(queue, &process_queue);
288 spin_unlock(&queue->lock);
289
290 while (received < budget && (skb = __skb_dequeue(&process_queue))) {
291 napi_gro_receive(napi, skb);
292 ++received;
293 }
294
295 if (!skb_queue_empty(&process_queue)) {
296 spin_lock(&queue->lock);
297 skb_queue_splice(&process_queue, queue);
298 spin_unlock(&queue->lock);
299 }
300
301 return received;
302}
303
304static int tun_napi_poll(struct napi_struct *napi, int budget)
305{
306 unsigned int received;
307
308 received = tun_napi_receive(napi, budget);
309
310 if (received < budget)
311 napi_complete_done(napi, received);
312
313 return received;
314}
315
316static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700317 bool napi_en, bool napi_frags)
Petar Penkov94317092017-09-22 13:49:14 -0700318{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700319 tfile->napi_enabled = napi_en;
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700320 tfile->napi_frags_enabled = napi_en && napi_frags;
Petar Penkov94317092017-09-22 13:49:14 -0700321 if (napi_en) {
322 netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
323 NAPI_POLL_WEIGHT);
324 napi_enable(&tfile->napi);
325 }
326}
327
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700328static void tun_napi_disable(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 napi_disable(&tfile->napi);
332}
333
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700334static void tun_napi_del(struct tun_file *tfile)
Petar Penkov94317092017-09-22 13:49:14 -0700335{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700336 if (tfile->napi_enabled)
Petar Penkov94317092017-09-22 13:49:14 -0700337 netif_napi_del(&tfile->napi);
338}
339
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700340static bool tun_napi_frags_enabled(const struct tun_file *tfile)
Petar Penkov90e33d42017-09-22 13:49:15 -0700341{
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700342 return tfile->napi_frags_enabled;
Petar Penkov90e33d42017-09-22 13:49:15 -0700343}
344
Greg Kurz8b8e6582015-04-24 14:50:36 +0200345#ifdef CONFIG_TUN_VNET_CROSS_LE
346static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
347{
348 return tun->flags & TUN_VNET_BE ? false :
349 virtio_legacy_is_little_endian();
350}
351
352static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
353{
354 int be = !!(tun->flags & TUN_VNET_BE);
355
356 if (put_user(be, argp))
357 return -EFAULT;
358
359 return 0;
360}
361
362static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
363{
364 int be;
365
366 if (get_user(be, argp))
367 return -EFAULT;
368
369 if (be)
370 tun->flags |= TUN_VNET_BE;
371 else
372 tun->flags &= ~TUN_VNET_BE;
373
374 return 0;
375}
376#else
377static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
378{
379 return virtio_legacy_is_little_endian();
380}
381
382static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
383{
384 return -EINVAL;
385}
386
387static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
388{
389 return -EINVAL;
390}
391#endif /* CONFIG_TUN_VNET_CROSS_LE */
392
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200393static inline bool tun_is_little_endian(struct tun_struct *tun)
394{
Greg Kurz7d824102015-04-24 14:26:24 +0200395 return tun->flags & TUN_VNET_LE ||
Greg Kurz8b8e6582015-04-24 14:50:36 +0200396 tun_legacy_is_little_endian(tun);
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200397}
398
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300399static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
400{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200401 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300402}
403
404static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
405{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200406 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300407}
408
Jason Wang96442e422012-10-31 19:46:02 +0000409static inline u32 tun_hashfn(u32 rxhash)
410{
Li RongQingf13b5462018-08-03 15:50:02 +0800411 return rxhash & TUN_MASK_FLOW_ENTRIES;
Jason Wang96442e422012-10-31 19:46:02 +0000412}
413
414static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
415{
416 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000417
Sasha Levinb67bfe02013-02-27 17:06:00 -0800418 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000419 if (e->rxhash == rxhash)
420 return e;
421 }
422 return NULL;
423}
424
425static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
426 struct hlist_head *head,
427 u32 rxhash, u16 queue_index)
428{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000429 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
430
Jason Wang96442e422012-10-31 19:46:02 +0000431 if (e) {
432 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
433 rxhash, queue_index);
434 e->updated = jiffies;
435 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800436 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000437 e->queue_index = queue_index;
438 e->tun = tun;
439 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000440 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000441 }
442 return e;
443}
444
Jason Wang96442e422012-10-31 19:46:02 +0000445static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
446{
447 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
448 e->rxhash, e->queue_index);
449 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000450 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000451 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000452}
453
454static void tun_flow_flush(struct tun_struct *tun)
455{
456 int i;
457
458 spin_lock_bh(&tun->lock);
459 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
460 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800461 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000462
Sasha Levinb67bfe02013-02-27 17:06:00 -0800463 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000464 tun_flow_delete(tun, e);
465 }
466 spin_unlock_bh(&tun->lock);
467}
468
469static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
470{
471 int i;
472
473 spin_lock_bh(&tun->lock);
474 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
475 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800476 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000477
Sasha Levinb67bfe02013-02-27 17:06:00 -0800478 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000479 if (e->queue_index == queue_index)
480 tun_flow_delete(tun, e);
481 }
482 }
483 spin_unlock_bh(&tun->lock);
484}
485
Kees Cooke99e88a2017-10-16 14:43:17 -0700486static void tun_flow_cleanup(struct timer_list *t)
Jason Wang96442e422012-10-31 19:46:02 +0000487{
Kees Cooke99e88a2017-10-16 14:43:17 -0700488 struct tun_struct *tun = from_timer(tun, t, flow_gc_timer);
Jason Wang96442e422012-10-31 19:46:02 +0000489 unsigned long delay = tun->ageing_time;
490 unsigned long next_timer = jiffies + delay;
491 unsigned long count = 0;
492 int i;
493
494 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
495
Eric Dumazet7dbfb4e2017-10-20 11:29:55 -0700496 spin_lock(&tun->lock);
Jason Wang96442e422012-10-31 19:46:02 +0000497 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
498 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800499 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000500
Sasha Levinb67bfe02013-02-27 17:06:00 -0800501 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000502 unsigned long this_timer;
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700503
Jason Wang96442e422012-10-31 19:46:02 +0000504 this_timer = e->updated + delay;
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700505 if (time_before_eq(this_timer, jiffies)) {
Jason Wang96442e422012-10-31 19:46:02 +0000506 tun_flow_delete(tun, e);
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700507 continue;
508 }
509 count++;
510 if (time_before(this_timer, next_timer))
Jason Wang96442e422012-10-31 19:46:02 +0000511 next_timer = this_timer;
512 }
513 }
514
515 if (count)
516 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
Eric Dumazet7dbfb4e2017-10-20 11:29:55 -0700517 spin_unlock(&tun->lock);
Jason Wang96442e422012-10-31 19:46:02 +0000518}
519
Eric Dumazet49974422012-12-12 19:22:57 +0000520static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000521 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000522{
523 struct hlist_head *head;
524 struct tun_flow_entry *e;
525 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000526 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000527
528 if (!rxhash)
529 return;
530 else
531 head = &tun->flows[tun_hashfn(rxhash)];
532
533 rcu_read_lock();
534
Jason Wang96442e422012-10-31 19:46:02 +0000535 e = tun_flow_find(head, rxhash);
536 if (likely(e)) {
537 /* TODO: keep queueing to old queue until it's empty? */
538 e->queue_index = queue_index;
539 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800540 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000541 } else {
542 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000543 if (!tun_flow_find(head, rxhash) &&
544 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000545 tun_flow_create(tun, head, rxhash, queue_index);
546
547 if (!timer_pending(&tun->flow_gc_timer))
548 mod_timer(&tun->flow_gc_timer,
549 round_jiffies_up(jiffies + delay));
550 spin_unlock_bh(&tun->lock);
551 }
552
Jason Wang96442e422012-10-31 19:46:02 +0000553 rcu_read_unlock();
554}
555
Tom Herbert9bc88932013-12-22 18:54:32 +0800556/**
557 * Save the hash received in the stack receive path and update the
558 * flow_hash table accordingly.
559 */
560static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
561{
Eric Dumazet567e4b72015-02-06 12:59:01 -0800562 if (unlikely(e->rps_rxhash != hash))
Tom Herbert9bc88932013-12-22 18:54:32 +0800563 e->rps_rxhash = hash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800564}
565
Jason Wangc8d68e62012-10-31 19:46:00 +0000566/* We try to identify a flow through its rxhash first. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800567 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000568 * the rxq based on the txq where the last packet of the flow comes. As
569 * the userspace application move between processors, we may get a
570 * different rxq no. here. If we could not get rxhash, then we would
571 * hope the rxq no. may help here.
572 */
Jason Wang96f84062017-12-04 17:31:23 +0800573static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
Jason Wangc8d68e62012-10-31 19:46:00 +0000574{
Jason Wang96442e422012-10-31 19:46:02 +0000575 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000576 u32 txq = 0;
577 u32 numqueues = 0;
578
Mark Rutland6aa7de02017-10-23 14:07:29 -0700579 numqueues = READ_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000580
Jason Wangfeec0842017-06-06 14:09:49 +0800581 txq = __skb_get_hash_symmetric(skb);
Jason Wangc8d68e62012-10-31 19:46:00 +0000582 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000583 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
Tom Herbert9bc88932013-12-22 18:54:32 +0800584 if (e) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800585 tun_flow_save_rps_rxhash(e, txq);
Zhi Yong Wufbe4d452014-01-02 13:24:28 +0800586 txq = e->queue_index;
Tom Herbert9bc88932013-12-22 18:54:32 +0800587 } else
Jason Wang96442e422012-10-31 19:46:02 +0000588 /* use multiply and shift instead of expensive divide */
589 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000590 } else if (likely(skb_rx_queue_recorded(skb))) {
591 txq = skb_get_rx_queue(skb);
592 while (unlikely(txq >= numqueues))
593 txq -= numqueues;
594 }
595
Jason Wangc8d68e62012-10-31 19:46:00 +0000596 return txq;
597}
598
Jason Wang96f84062017-12-04 17:31:23 +0800599static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
600{
Jason Wangcd5681d2018-01-16 16:31:01 +0800601 struct tun_prog *prog;
Jason Wang96f84062017-12-04 17:31:23 +0800602 u16 ret = 0;
603
604 prog = rcu_dereference(tun->steering_prog);
605 if (prog)
606 ret = bpf_prog_run_clear_cb(prog->prog, skb);
607
608 return ret % tun->numqueues;
609}
610
611static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Alexander Duyck4f49dec2018-07-09 12:19:59 -0400612 struct net_device *sb_dev,
613 select_queue_fallback_t fallback)
Jason Wang96f84062017-12-04 17:31:23 +0800614{
615 struct tun_struct *tun = netdev_priv(dev);
616 u16 ret;
617
618 rcu_read_lock();
619 if (rcu_dereference(tun->steering_prog))
620 ret = tun_ebpf_select_queue(tun, skb);
621 else
622 ret = tun_automq_select_queue(tun, skb);
623 rcu_read_unlock();
624
625 return ret;
626}
627
Jason Wangcde8b152012-10-31 19:46:01 +0000628static inline bool tun_not_capable(struct tun_struct *tun)
629{
630 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000631 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000632
633 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
634 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000635 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000636}
637
Jason Wangc8d68e62012-10-31 19:46:00 +0000638static void tun_set_real_num_queues(struct tun_struct *tun)
639{
640 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
641 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
642}
643
Jason Wang4008e972012-12-13 23:53:30 +0000644static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
645{
646 tfile->detached = tun;
647 list_add_tail(&tfile->next, &tun->disabled);
648 ++tun->numdisabled;
649}
650
Jason Wangd32649d2012-12-18 11:00:27 +0800651static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000652{
653 struct tun_struct *tun = tfile->detached;
654
655 tfile->detached = NULL;
656 list_del_init(&tfile->next);
657 --tun->numdisabled;
658 return tun;
659}
660
Jason Wang3a403072018-03-09 14:50:34 +0800661void tun_ptr_free(void *ptr)
Jason Wangfc72d1d2018-01-04 11:14:28 +0800662{
663 if (!ptr)
664 return;
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200665 if (tun_is_xdp_frame(ptr)) {
666 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800667
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +0200668 xdp_return_frame(xdpf);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800669 } else {
670 __skb_array_destroy_skb(ptr);
671 }
672}
Jason Wang3a403072018-03-09 14:50:34 +0800673EXPORT_SYMBOL_GPL(tun_ptr_free);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800674
Jason Wang4bfb0512013-09-05 17:53:59 +0800675static void tun_queue_purge(struct tun_file *tfile)
676{
Jason Wangfc72d1d2018-01-04 11:14:28 +0800677 void *ptr;
Jason Wang1576d982016-06-30 14:45:36 +0800678
Jason Wangfc72d1d2018-01-04 11:14:28 +0800679 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL)
680 tun_ptr_free(ptr);
Jason Wang1576d982016-06-30 14:45:36 +0800681
Jason Wang5503fce2017-01-18 15:02:03 +0800682 skb_queue_purge(&tfile->sk.sk_write_queue);
Jason Wang4bfb0512013-09-05 17:53:59 +0800683 skb_queue_purge(&tfile->sk.sk_error_queue);
684}
685
Jason Wangc8d68e62012-10-31 19:46:00 +0000686static void __tun_detach(struct tun_file *tfile, bool clean)
687{
688 struct tun_file *ntfile;
689 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000690
Jason Wangb8deabd2013-01-11 16:59:32 +0000691 tun = rtnl_dereference(tfile->tun);
692
Petar Penkov94317092017-09-22 13:49:14 -0700693 if (tun && clean) {
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700694 tun_napi_disable(tfile);
695 tun_napi_del(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700696 }
697
Jason Wang9e857222013-01-28 01:05:19 +0000698 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000699 u16 index = tfile->queue_index;
700 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000701
702 rcu_assign_pointer(tun->tfiles[index],
703 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000704 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000705 ntfile->queue_index = index;
706
707 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000708 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530709 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000710 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000711 } else
Jason Wang4008e972012-12-13 23:53:30 +0000712 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000713
714 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000715 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000716 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800717 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000718 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000719 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000720 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000721 sock_put(&tfile->sk);
722 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000723
724 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000725 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
726 netif_carrier_off(tun->dev);
727
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200728 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000729 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000730 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000731 }
Jason Wangb196d882018-05-11 10:49:25 +0800732 if (tun)
733 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wang7063efd2018-05-16 20:39:33 +0800734 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free);
Eric W. Biederman140e807da2015-05-08 21:07:08 -0500735 sock_put(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000736 }
737}
738
739static void tun_detach(struct tun_file *tfile, bool clean)
740{
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200741 struct tun_struct *tun;
742 struct net_device *dev;
743
Jason Wangc8d68e62012-10-31 19:46:00 +0000744 rtnl_lock();
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200745 tun = rtnl_dereference(tfile->tun);
746 dev = tun ? tun->dev : NULL;
Jason Wangc8d68e62012-10-31 19:46:00 +0000747 __tun_detach(tfile, clean);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200748 if (dev)
749 netdev_state_change(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000750 rtnl_unlock();
751}
752
753static void tun_detach_all(struct net_device *dev)
754{
755 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000756 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000757 int i, n = tun->numqueues;
758
759 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000760 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000761 BUG_ON(!tfile);
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700762 tun_napi_disable(tfile);
Jason Wangaddf8fc2016-05-19 13:36:51 +0800763 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700764 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530765 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000766 --tun->numqueues;
767 }
Jason Wang9e857222013-01-28 01:05:19 +0000768 list_for_each_entry(tfile, &tun->disabled, next) {
Jason Wangaddf8fc2016-05-19 13:36:51 +0800769 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700770 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530771 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000772 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000773 BUG_ON(tun->numqueues != 0);
774
775 synchronize_net();
776 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000777 tfile = rtnl_dereference(tun->tfiles[i]);
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700778 tun_napi_del(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000779 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800780 tun_queue_purge(tfile);
Jason Wangb196d882018-05-11 10:49:25 +0800781 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wangc8d68e62012-10-31 19:46:00 +0000782 sock_put(&tfile->sk);
783 }
Jason Wang4008e972012-12-13 23:53:30 +0000784 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
785 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800786 tun_queue_purge(tfile);
Jason Wangb196d882018-05-11 10:49:25 +0800787 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wang4008e972012-12-13 23:53:30 +0000788 sock_put(&tfile->sk);
789 }
790 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000791
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200792 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000793 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000794}
795
Petar Penkov94317092017-09-22 13:49:14 -0700796static int tun_attach(struct tun_struct *tun, struct file *file,
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700797 bool skip_filter, bool napi, bool napi_frags)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000798{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000799 struct tun_file *tfile = file->private_data;
Jason Wang1576d982016-06-30 14:45:36 +0800800 struct net_device *dev = tun->dev;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000801 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000802
Paul Moore5dbbaf22013-01-14 07:12:19 +0000803 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
804 if (err < 0)
805 goto out;
806
Eric W. Biederman38231b72009-01-20 11:02:28 +0000807 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000808 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000809 goto out;
810
811 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200812 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000813 goto out;
814
815 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000816 if (!tfile->detached &&
817 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000818 goto out;
819
820 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000821
stephen hemminger92d4ea62013-12-05 20:42:58 -0800822 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400823 if (!skip_filter && (tun->filter_attached == true)) {
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +0200824 lock_sock(tfile->socket.sk);
825 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
826 release_sock(tfile->socket.sk);
Jason Wang54f968d2012-10-31 19:45:57 +0000827 if (!err)
828 goto out;
829 }
Jason Wang1576d982016-06-30 14:45:36 +0800830
831 if (!tfile->detached &&
Jason Wangb196d882018-05-11 10:49:25 +0800832 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len,
833 GFP_KERNEL, tun_ptr_free)) {
Jason Wang1576d982016-06-30 14:45:36 +0800834 err = -ENOMEM;
835 goto out;
836 }
837
Jason Wangc8d68e62012-10-31 19:46:00 +0000838 tfile->queue_index = tun->numqueues;
Jason Wangaddf8fc2016-05-19 13:36:51 +0800839 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100840
841 if (tfile->detached) {
842 /* Re-attach detached tfile, updating XDP queue_index */
843 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq));
844
845 if (tfile->xdp_rxq.queue_index != tfile->queue_index)
846 tfile->xdp_rxq.queue_index = tfile->queue_index;
847 } else {
848 /* Setup XDP RX-queue info, for new tfile getting attached */
849 err = xdp_rxq_info_reg(&tfile->xdp_rxq,
850 tun->dev, tfile->queue_index);
851 if (err < 0)
852 goto out;
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200853 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq,
854 MEM_TYPE_PAGE_SHARED, NULL);
855 if (err < 0) {
856 xdp_rxq_info_unreg(&tfile->xdp_rxq);
857 goto out;
858 }
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100859 err = 0;
860 }
861
Jason Wang6e914fc2012-10-31 19:45:58 +0000862 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000863 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000864 tun->numqueues++;
865
Petar Penkov94317092017-09-22 13:49:14 -0700866 if (tfile->detached) {
Jason Wang4008e972012-12-13 23:53:30 +0000867 tun_enable_queue(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700868 } else {
Jason Wang4008e972012-12-13 23:53:30 +0000869 sock_hold(&tfile->sk);
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700870 tun_napi_init(tun, tfile, napi, napi_frags);
Petar Penkov94317092017-09-22 13:49:14 -0700871 }
Jason Wang4008e972012-12-13 23:53:30 +0000872
Jason Wangc8d68e62012-10-31 19:46:00 +0000873 tun_set_real_num_queues(tun);
874
Jason Wangc8d68e62012-10-31 19:46:00 +0000875 /* device is allowed to go away first, so no need to hold extra
876 * refcnt.
877 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000878
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{
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001018 struct tun_struct *tun = netdev_priv(dev);
1019 int i;
1020
Jason Wangc8d68e62012-10-31 19:46:00 +00001021 netif_tx_start_all_queues(dev);
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001022
1023 for (i = 0; i < tun->numqueues; i++) {
1024 struct tun_file *tfile;
1025
1026 tfile = rtnl_dereference(tun->tfiles[i]);
1027 tfile->socket.sk->sk_write_space(tfile->socket.sk);
1028 }
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return 0;
1031}
1032
1033/* Net device close. */
1034static int tun_net_close(struct net_device *dev)
1035{
Jason Wangc8d68e62012-10-31 19:46:00 +00001036 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return 0;
1038}
1039
1040/* Net device start xmit */
Jason Wang96f84062017-12-04 17:31:23 +08001041static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Jason Wang3df97ba2016-04-25 23:13:42 -04001043#ifdef CONFIG_RPS
Jason Wang96f84062017-12-04 17:31:23 +08001044 if (tun->numqueues == 1 && static_key_false(&rps_needed)) {
Tom Herbert9bc88932013-12-22 18:54:32 +08001045 /* Select queue was not called for the skbuff, so we extract the
1046 * RPS hash and save it into the flow_table here.
1047 */
1048 __u32 rxhash;
1049
Jason Wangfeec0842017-06-06 14:09:49 +08001050 rxhash = __skb_get_hash_symmetric(skb);
Tom Herbert9bc88932013-12-22 18:54:32 +08001051 if (rxhash) {
1052 struct tun_flow_entry *e;
1053 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
1054 rxhash);
1055 if (e)
1056 tun_flow_save_rps_rxhash(e, rxhash);
1057 }
1058 }
Jason Wang3df97ba2016-04-25 23:13:42 -04001059#endif
Jason Wang96f84062017-12-04 17:31:23 +08001060}
1061
Jason Wangaff3d702018-01-16 16:31:02 +08001062static unsigned int run_ebpf_filter(struct tun_struct *tun,
1063 struct sk_buff *skb,
1064 int len)
1065{
1066 struct tun_prog *prog = rcu_dereference(tun->filter_prog);
1067
1068 if (prog)
1069 len = bpf_prog_run_clear_cb(prog->prog, skb);
1070
1071 return len;
1072}
1073
Jason Wang96f84062017-12-04 17:31:23 +08001074/* Net device start xmit */
1075static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1076{
1077 struct tun_struct *tun = netdev_priv(dev);
1078 int txq = skb->queue_mapping;
1079 struct tun_file *tfile;
Jason Wangaff3d702018-01-16 16:31:02 +08001080 int len = skb->len;
Jason Wang96f84062017-12-04 17:31:23 +08001081
1082 rcu_read_lock();
1083 tfile = rcu_dereference(tun->tfiles[txq]);
Jason Wang96f84062017-12-04 17:31:23 +08001084
1085 /* Drop packet if interface is not attached */
Willem de Bruijncc166422017-12-05 22:11:17 -05001086 if (txq >= tun->numqueues)
Jason Wang96f84062017-12-04 17:31:23 +08001087 goto drop;
1088
1089 if (!rcu_dereference(tun->steering_prog))
1090 tun_automq_xmit(tun, skb);
Tom Herbert9bc88932013-12-22 18:54:32 +08001091
Jason Wang6e914fc2012-10-31 19:45:58 +00001092 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
1093
Jason Wangc8d68e62012-10-31 19:46:00 +00001094 BUG_ON(!tfile);
1095
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001096 /* Drop if the filter does not like it.
1097 * This is a noop if the filter is disabled.
1098 * Filter can be enabled only for the TAP devices. */
1099 if (!check_filter(&tun->txflt, skb))
1100 goto drop;
1101
Jason Wang54f968d2012-10-31 19:45:57 +00001102 if (tfile->socket.sk->sk_filter &&
1103 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001104 goto drop;
1105
Jason Wangaff3d702018-01-16 16:31:02 +08001106 len = run_ebpf_filter(tun, skb, len);
Bjørn Mork81c89502018-04-17 22:46:38 +02001107 if (len == 0 || pskb_trim(skb, len))
Jason Wangaff3d702018-01-16 16:31:02 +08001108 goto drop;
1109
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001110 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Jason Wang7bf66302013-09-05 17:54:00 +08001111 goto drop;
1112
Soheil Hassas Yeganeh7b996242016-08-23 18:22:33 -04001113 skb_tx_timestamp(skb);
Richard Cochraneda29772013-07-19 19:40:10 +02001114
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +00001115 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +08001116 * for indefinite time.
1117 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +00001118 skb_orphan(skb);
1119
Eric Dumazetf8af75f2013-03-06 11:02:37 +00001120 nf_reset(skb);
1121
Jason Wang5990a302018-01-04 11:14:27 +08001122 if (ptr_ring_produce(&tfile->tx_ring, skb))
Jason Wang1576d982016-06-30 14:45:36 +08001123 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +00001126 if (tfile->flags & TUN_FASYNC)
1127 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -07001128 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +00001129
1130 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +00001131 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133drop:
Paolo Abeni608b9972016-04-13 10:52:20 +02001134 this_cpu_inc(tun->pcpu_stats->tx_dropped);
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +00001135 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +00001137 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +08001138 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001141static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001143 /*
1144 * This callback is supposed to deal with mc filter in
1145 * _rx_ path and has nothing to do with the _tx_ path.
1146 * In rx path we always accept everything userspace gives us.
1147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001150static netdev_features_t tun_net_fix_features(struct net_device *dev,
1151 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +00001152{
1153 struct tun_struct *tun = netdev_priv(dev);
1154
1155 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1156}
Paolo Abenieaea34b2016-02-26 10:45:40 +01001157
1158static void tun_set_headroom(struct net_device *dev, int new_hr)
1159{
1160 struct tun_struct *tun = netdev_priv(dev);
1161
1162 if (new_hr < NET_SKB_PAD)
1163 new_hr = NET_SKB_PAD;
1164
1165 tun->align = new_hr;
1166}
1167
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001168static void
Paolo Abeni608b9972016-04-13 10:52:20 +02001169tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1170{
1171 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
1172 struct tun_struct *tun = netdev_priv(dev);
1173 struct tun_pcpu_stats *p;
1174 int i;
1175
1176 for_each_possible_cpu(i) {
1177 u64 rxpackets, rxbytes, txpackets, txbytes;
1178 unsigned int start;
1179
1180 p = per_cpu_ptr(tun->pcpu_stats, i);
1181 do {
1182 start = u64_stats_fetch_begin(&p->syncp);
1183 rxpackets = p->rx_packets;
1184 rxbytes = p->rx_bytes;
1185 txpackets = p->tx_packets;
1186 txbytes = p->tx_bytes;
1187 } while (u64_stats_fetch_retry(&p->syncp, start));
1188
1189 stats->rx_packets += rxpackets;
1190 stats->rx_bytes += rxbytes;
1191 stats->tx_packets += txpackets;
1192 stats->tx_bytes += txbytes;
1193
1194 /* u32 counters */
1195 rx_dropped += p->rx_dropped;
1196 rx_frame_errors += p->rx_frame_errors;
1197 tx_dropped += p->tx_dropped;
1198 }
1199 stats->rx_dropped = rx_dropped;
1200 stats->rx_frame_errors = rx_frame_errors;
1201 stats->tx_dropped = tx_dropped;
Paolo Abeni608b9972016-04-13 10:52:20 +02001202}
1203
Jason Wang761876c2017-08-11 19:41:18 +08001204static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1205 struct netlink_ext_ack *extack)
1206{
1207 struct tun_struct *tun = netdev_priv(dev);
1208 struct bpf_prog *old_prog;
1209
1210 old_prog = rtnl_dereference(tun->xdp_prog);
1211 rcu_assign_pointer(tun->xdp_prog, prog);
1212 if (old_prog)
1213 bpf_prog_put(old_prog);
1214
1215 return 0;
1216}
1217
1218static u32 tun_xdp_query(struct net_device *dev)
1219{
1220 struct tun_struct *tun = netdev_priv(dev);
1221 const struct bpf_prog *xdp_prog;
1222
1223 xdp_prog = rtnl_dereference(tun->xdp_prog);
1224 if (xdp_prog)
1225 return xdp_prog->aux->id;
1226
1227 return 0;
1228}
1229
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001230static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
Jason Wang761876c2017-08-11 19:41:18 +08001231{
1232 switch (xdp->command) {
1233 case XDP_SETUP_PROG:
1234 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1235 case XDP_QUERY_PROG:
1236 xdp->prog_id = tun_xdp_query(dev);
Jason Wang761876c2017-08-11 19:41:18 +08001237 return 0;
1238 default:
1239 return -EINVAL;
1240 }
1241}
1242
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001243static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001244 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001245 .ndo_open = tun_net_open,
1246 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001247 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001248 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +00001249 .ndo_select_queue = tun_select_queue,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001250 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001251 .ndo_get_stats64 = tun_net_get_stats64,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001252};
1253
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001254static void __tun_xdp_flush_tfile(struct tun_file *tfile)
1255{
1256 /* Notify and wake up reader process */
1257 if (tfile->flags & TUN_FASYNC)
1258 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
1259 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
1260}
1261
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02001262static int tun_xdp_xmit(struct net_device *dev, int n,
1263 struct xdp_frame **frames, u32 flags)
Jason Wangfc72d1d2018-01-04 11:14:28 +08001264{
1265 struct tun_struct *tun = netdev_priv(dev);
Jason Wangfc72d1d2018-01-04 11:14:28 +08001266 struct tun_file *tfile;
1267 u32 numqueues;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001268 int drops = 0;
1269 int cnt = n;
1270 int i;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001271
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001272 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02001273 return -EINVAL;
1274
Jason Wangfc72d1d2018-01-04 11:14:28 +08001275 rcu_read_lock();
1276
1277 numqueues = READ_ONCE(tun->numqueues);
1278 if (!numqueues) {
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001279 rcu_read_unlock();
1280 return -ENXIO; /* Caller will free/return all frames */
Jason Wangfc72d1d2018-01-04 11:14:28 +08001281 }
1282
1283 tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
1284 numqueues]);
Jason Wangfc72d1d2018-01-04 11:14:28 +08001285
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001286 spin_lock(&tfile->tx_ring.producer_lock);
1287 for (i = 0; i < n; i++) {
1288 struct xdp_frame *xdp = frames[i];
1289 /* Encode the XDP flag into lowest bit for consumer to differ
1290 * XDP buffer from sk_buff.
1291 */
1292 void *frame = tun_xdp_to_ptr(xdp);
1293
1294 if (__ptr_ring_produce(&tfile->tx_ring, frame)) {
1295 this_cpu_inc(tun->pcpu_stats->tx_dropped);
1296 xdp_return_frame_rx_napi(xdp);
1297 drops++;
1298 }
1299 }
1300 spin_unlock(&tfile->tx_ring.producer_lock);
1301
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001302 if (flags & XDP_XMIT_FLUSH)
1303 __tun_xdp_flush_tfile(tfile);
1304
Jason Wangfc72d1d2018-01-04 11:14:28 +08001305 rcu_read_unlock();
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001306 return cnt - drops;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001307}
1308
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02001309static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
1310{
1311 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
1312
1313 if (unlikely(!frame))
1314 return -EOVERFLOW;
1315
Jesper Dangaard Brouer42421a52018-06-05 13:55:45 +02001316 return tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH);
Jason Wangfc72d1d2018-01-04 11:14:28 +08001317}
1318
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001319static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001320 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001321 .ndo_open = tun_net_open,
1322 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001323 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001324 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001325 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001326 .ndo_set_mac_address = eth_mac_addr,
1327 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +00001328 .ndo_select_queue = tun_select_queue,
Toshiaki Makita5e527962015-07-31 15:03:27 +09001329 .ndo_features_check = passthru_features_check,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001330 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001331 .ndo_get_stats64 = tun_net_get_stats64,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001332 .ndo_bpf = tun_xdp,
Jason Wangfc72d1d2018-01-04 11:14:28 +08001333 .ndo_xdp_xmit = tun_xdp_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001334};
1335
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001336static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +00001337{
1338 int i;
1339
Jason Wang96442e422012-10-31 19:46:02 +00001340 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1341 INIT_HLIST_HEAD(&tun->flows[i]);
1342
1343 tun->ageing_time = TUN_FLOW_EXPIRE;
Kees Cooke99e88a2017-10-16 14:43:17 -07001344 timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0);
1345 mod_timer(&tun->flow_gc_timer,
1346 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +00001347}
1348
1349static void tun_flow_uninit(struct tun_struct *tun)
1350{
1351 del_timer_sync(&tun->flow_gc_timer);
1352 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001353}
1354
Jarod Wilson91572082016-10-20 13:55:20 -04001355#define MIN_MTU 68
1356#define MAX_MTU 65535
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358/* Initialize net device. */
1359static void tun_net_init(struct net_device *dev)
1360{
1361 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001364 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001365 dev->netdev_ops = &tun_netdev_ops;
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Point-to-Point TUN Device */
1368 dev->hard_header_len = 0;
1369 dev->addr_len = 0;
1370 dev->mtu = 1500;
1371
1372 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001373 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
1376
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001377 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -08001378 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001380 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +00001381 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +00001382 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001384 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 break;
1387 }
Jarod Wilson91572082016-10-20 13:55:20 -04001388
1389 dev->min_mtu = MIN_MTU;
1390 dev->max_mtu = MAX_MTU - dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Jason Wang2f3ab622018-05-22 14:21:04 +08001393static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile)
1394{
1395 struct sock *sk = tfile->socket.sk;
1396
1397 return (tun->dev->flags & IFF_UP) && sock_writeable(sk);
1398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/* Character device part */
1401
1402/* Poll */
Al Viroafc9a422017-07-03 06:39:46 -04001403static __poll_t tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001404{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +00001405 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001406 struct tun_struct *tun = tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001407 struct sock *sk;
Al Viroafc9a422017-07-03 06:39:46 -04001408 __poll_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 if (!tun)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001411 return EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Jason Wang54f968d2012-10-31 19:45:57 +00001413 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001414
Joe Perches6b8a66e2011-03-02 07:18:10 +00001415 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Xi Wang9e641bd2014-05-16 15:11:48 -07001417 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001418
Jason Wang5990a302018-01-04 11:14:27 +08001419 if (!ptr_ring_empty(&tfile->tx_ring))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001420 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Jason Wang2f3ab622018-05-22 14:21:04 +08001422 /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to
1423 * guarantee EPOLLOUT to be raised by either here or
1424 * tun_sock_write_space(). Then process could get notification
1425 * after it writes to a down device and meets -EIO.
1426 */
1427 if (tun_sock_writeable(tun, tfile) ||
1428 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1429 tun_sock_writeable(tun, tfile)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001430 mask |= EPOLLOUT | EPOLLWRNORM;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001431
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001432 if (tun->dev->reg_state != NETREG_REGISTERED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001433 mask = EPOLLERR;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001434
Eric W. Biederman631ab462009-01-20 11:00:40 +00001435 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return mask;
1437}
1438
Petar Penkov90e33d42017-09-22 13:49:15 -07001439static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
1440 size_t len,
1441 const struct iov_iter *it)
1442{
1443 struct sk_buff *skb;
1444 size_t linear;
1445 int err;
1446 int i;
1447
1448 if (it->nr_segs > MAX_SKB_FRAGS + 1)
1449 return ERR_PTR(-ENOMEM);
1450
1451 local_bh_disable();
1452 skb = napi_get_frags(&tfile->napi);
1453 local_bh_enable();
1454 if (!skb)
1455 return ERR_PTR(-ENOMEM);
1456
1457 linear = iov_iter_single_seg_count(it);
1458 err = __skb_grow(skb, linear);
1459 if (err)
1460 goto free;
1461
1462 skb->len = len;
1463 skb->data_len = len - linear;
1464 skb->truesize += skb->data_len;
1465
1466 for (i = 1; i < it->nr_segs; i++) {
Eric Dumazet43a08e02018-02-15 14:47:15 -08001467 struct page_frag *pfrag = &current->task_frag;
Petar Penkov90e33d42017-09-22 13:49:15 -07001468 size_t fragsz = it->iov[i].iov_len;
Petar Penkov90e33d42017-09-22 13:49:15 -07001469
1470 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1471 err = -EINVAL;
1472 goto free;
1473 }
1474
Eric Dumazet43a08e02018-02-15 14:47:15 -08001475 if (!skb_page_frag_refill(fragsz, pfrag, GFP_KERNEL)) {
Petar Penkov90e33d42017-09-22 13:49:15 -07001476 err = -ENOMEM;
1477 goto free;
1478 }
1479
Eric Dumazet43a08e02018-02-15 14:47:15 -08001480 skb_fill_page_desc(skb, i - 1, pfrag->page,
1481 pfrag->offset, fragsz);
1482 page_ref_inc(pfrag->page);
1483 pfrag->offset += fragsz;
Petar Penkov90e33d42017-09-22 13:49:15 -07001484 }
1485
1486 return skb;
1487free:
1488 /* frees skb and all frags allocated with napi_alloc_frag() */
1489 napi_free_frags(&tfile->napi);
1490 return ERR_PTR(err);
1491}
1492
Rusty Russellf42157c2008-08-15 15:15:10 -07001493/* prepad is the amount to reserve at front. len is length after that.
1494 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001495static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001496 size_t prepad, size_t len,
1497 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001498{
Jason Wang54f968d2012-10-31 19:45:57 +00001499 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001500 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001501 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001502
1503 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001504 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001505 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001506
Herbert Xu33dccbb2009-02-05 21:25:32 -08001507 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001508 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001509 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001510 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001511
1512 skb_reserve(skb, prepad);
1513 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001514 skb->data_len = len - linear;
1515 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001516
1517 return skb;
1518}
1519
Jason Wang5503fce2017-01-18 15:02:03 +08001520static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1521 struct sk_buff *skb, int more)
1522{
1523 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1524 struct sk_buff_head process_queue;
1525 u32 rx_batched = tun->rx_batched;
1526 bool rcv = false;
1527
1528 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1529 local_bh_disable();
1530 netif_receive_skb(skb);
1531 local_bh_enable();
1532 return;
1533 }
1534
1535 spin_lock(&queue->lock);
1536 if (!more || skb_queue_len(queue) == rx_batched) {
1537 __skb_queue_head_init(&process_queue);
1538 skb_queue_splice_tail_init(queue, &process_queue);
1539 rcv = true;
1540 } else {
1541 __skb_queue_tail(queue, skb);
1542 }
1543 spin_unlock(&queue->lock);
1544
1545 if (rcv) {
1546 struct sk_buff *nskb;
1547
1548 local_bh_disable();
1549 while ((nskb = __skb_dequeue(&process_queue)))
1550 netif_receive_skb(nskb);
1551 netif_receive_skb(skb);
1552 local_bh_enable();
1553 }
1554}
1555
Jason Wang66ccbc92017-08-11 19:41:16 +08001556static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1557 int len, int noblock, bool zerocopy)
1558{
1559 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1560 return false;
1561
1562 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1563 return false;
1564
1565 if (!noblock)
1566 return false;
1567
1568 if (zerocopy)
1569 return false;
1570
1571 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1572 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1573 return false;
1574
1575 return true;
1576}
1577
Jason Wang761876c2017-08-11 19:41:18 +08001578static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1579 struct tun_file *tfile,
Jason Wang66ccbc92017-08-11 19:41:16 +08001580 struct iov_iter *from,
Jason Wang761876c2017-08-11 19:41:18 +08001581 struct virtio_net_hdr *hdr,
Jason Wang1cfe6e92017-09-04 11:36:09 +08001582 int len, int *skb_xdp)
Jason Wang66ccbc92017-08-11 19:41:16 +08001583{
Eric Dumazet0bbd7da2017-08-16 22:14:33 +08001584 struct page_frag *alloc_frag = &current->task_frag;
Jason Wang66ccbc92017-08-11 19:41:16 +08001585 struct sk_buff *skb;
Jason Wang761876c2017-08-11 19:41:18 +08001586 struct bpf_prog *xdp_prog;
Jason Wang7df13212017-09-04 11:36:08 +08001587 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jason Wang761876c2017-08-11 19:41:18 +08001588 unsigned int delta = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001589 char *buf;
1590 size_t copied;
Jason Wang7df13212017-09-04 11:36:08 +08001591 int err, pad = TUN_RX_PAD;
1592
1593 rcu_read_lock();
1594 xdp_prog = rcu_dereference(tun->xdp_prog);
1595 if (xdp_prog)
1596 pad += TUN_HEADROOM;
1597 buflen += SKB_DATA_ALIGN(len + pad);
1598 rcu_read_unlock();
Jason Wang66ccbc92017-08-11 19:41:16 +08001599
Jason Wang63b9ab62017-10-27 11:05:44 +08001600 alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
Jason Wang66ccbc92017-08-11 19:41:16 +08001601 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1602 return ERR_PTR(-ENOMEM);
1603
1604 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1605 copied = copy_page_from_iter(alloc_frag->page,
Jason Wang7df13212017-09-04 11:36:08 +08001606 alloc_frag->offset + pad,
Jason Wang66ccbc92017-08-11 19:41:16 +08001607 len, from);
1608 if (copied != len)
1609 return ERR_PTR(-EFAULT);
1610
Jason Wang7df13212017-09-04 11:36:08 +08001611 /* There's a small window that XDP may be set after the check
1612 * of xdp_prog above, this should be rare and for simplicity
1613 * we do XDP on skb in case the headroom is not enough.
1614 */
1615 if (hdr->gso_type || !xdp_prog)
Jason Wang1cfe6e92017-09-04 11:36:09 +08001616 *skb_xdp = 1;
Jason Wang761876c2017-08-11 19:41:18 +08001617 else
Jason Wang1cfe6e92017-09-04 11:36:09 +08001618 *skb_xdp = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001619
Toshiaki Makita6547e382018-05-28 19:37:49 +09001620 local_bh_disable();
Jason Wang761876c2017-08-11 19:41:18 +08001621 rcu_read_lock();
1622 xdp_prog = rcu_dereference(tun->xdp_prog);
Jason Wang1cfe6e92017-09-04 11:36:09 +08001623 if (xdp_prog && !*skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001624 struct xdp_buff xdp;
1625 void *orig_data;
1626 u32 act;
1627
1628 xdp.data_hard_start = buf;
Jason Wang7df13212017-09-04 11:36:08 +08001629 xdp.data = buf + pad;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001630 xdp_set_data_meta_invalid(&xdp);
Jason Wang761876c2017-08-11 19:41:18 +08001631 xdp.data_end = xdp.data + len;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +01001632 xdp.rxq = &tfile->xdp_rxq;
Jason Wang761876c2017-08-11 19:41:18 +08001633 orig_data = xdp.data;
1634 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1635
1636 switch (act) {
1637 case XDP_REDIRECT:
1638 get_page(alloc_frag->page);
1639 alloc_frag->offset += buflen;
1640 err = xdp_do_redirect(tun->dev, &xdp, xdp_prog);
Jason Wang1bb4f2e2018-02-24 11:32:26 +08001641 xdp_do_flush_map();
Jason Wang761876c2017-08-11 19:41:18 +08001642 if (err)
1643 goto err_redirect;
Xin Long654d5732017-11-19 19:31:04 +08001644 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001645 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001646 return NULL;
1647 case XDP_TX:
Jason Wang59655a52018-03-14 11:23:40 +08001648 get_page(alloc_frag->page);
1649 alloc_frag->offset += buflen;
Toshiaki Makita6e8cfd62018-07-13 13:24:38 +09001650 if (tun_xdp_tx(tun->dev, &xdp) < 0)
Jason Wang59655a52018-03-14 11:23:40 +08001651 goto err_redirect;
Jason Wang59655a52018-03-14 11:23:40 +08001652 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001653 local_bh_enable();
Jason Wang59655a52018-03-14 11:23:40 +08001654 return NULL;
Jason Wang761876c2017-08-11 19:41:18 +08001655 case XDP_PASS:
1656 delta = orig_data - xdp.data;
Nikita V. Shirokov8fb58f12018-04-17 21:42:19 -07001657 len = xdp.data_end - xdp.data;
Jason Wang761876c2017-08-11 19:41:18 +08001658 break;
1659 default:
1660 bpf_warn_invalid_xdp_action(act);
1661 /* fall through */
1662 case XDP_ABORTED:
1663 trace_xdp_exception(tun->dev, xdp_prog, act);
1664 /* fall through */
1665 case XDP_DROP:
1666 goto err_xdp;
1667 }
1668 }
1669
1670 skb = build_skb(buf, buflen);
1671 if (!skb) {
1672 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001673 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001674 return ERR_PTR(-ENOMEM);
1675 }
1676
Jason Wang7df13212017-09-04 11:36:08 +08001677 skb_reserve(skb, pad - delta);
Nikita V. Shirokov8fb58f12018-04-17 21:42:19 -07001678 skb_put(skb, len);
Jason Wang66ccbc92017-08-11 19:41:16 +08001679 get_page(alloc_frag->page);
1680 alloc_frag->offset += buflen;
1681
Jason Wang761876c2017-08-11 19:41:18 +08001682 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001683 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001684
Jason Wang66ccbc92017-08-11 19:41:16 +08001685 return skb;
Jason Wang761876c2017-08-11 19:41:18 +08001686
1687err_redirect:
1688 put_page(alloc_frag->page);
1689err_xdp:
1690 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001691 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001692 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1693 return NULL;
Jason Wang66ccbc92017-08-11 19:41:16 +08001694}
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001697static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001698 void *msg_control, struct iov_iter *from,
Jason Wang5503fce2017-01-18 15:02:03 +08001699 int noblock, bool more)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Harvey Harrison09640e632009-02-01 00:45:17 -08001701 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001703 size_t total_len = iov_iter_count(from);
Paolo Abenieaea34b2016-02-26 10:45:40 +01001704 size_t len = total_len, align = tun->align, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001705 struct virtio_net_hdr gso = { 0 };
Paolo Abeni608b9972016-04-13 10:52:20 +02001706 struct tun_pcpu_stats *stats;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001707 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001708 int copylen;
1709 bool zerocopy = false;
1710 int err;
Jason Wang96f84062017-12-04 17:31:23 +08001711 u32 rxhash = 0;
Jason Wang1cfe6e92017-09-04 11:36:09 +08001712 int skb_xdp = 1;
Eric Dumazetaf3fb242018-09-28 14:51:49 -07001713 bool frags = tun_napi_frags_enabled(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Eric Dumazet1bd4978a2015-12-16 08:57:37 -08001715 if (!(tun->dev->flags & IFF_UP))
1716 return -EIO;
1717
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001718 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001719 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001721 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Al Virocbbd26b2016-11-01 22:09:04 -04001723 if (!copy_from_iter_full(&pi, sizeof(pi), from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return -EFAULT;
1725 }
1726
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001727 if (tun->flags & IFF_VNET_HDR) {
Willem de Bruijne1edab82017-02-03 18:20:48 -05001728 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1729
1730 if (len < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001731 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001732 len -= vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001733
Al Virocbbd26b2016-11-01 22:09:04 -04001734 if (!copy_from_iter_full(&gso, sizeof(gso), from))
Rusty Russellf43798c2008-07-03 03:48:02 -07001735 return -EFAULT;
1736
Herbert Xu49091222009-06-08 00:20:01 -07001737 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001738 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1739 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 -07001740
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001741 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001742 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001743 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001744 }
1745
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001746 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001747 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001748 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001749 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001750 return -EINVAL;
1751 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001752
Jason Wang96f8d9e2013-11-13 14:00:39 +08001753 good_linear = SKB_MAX_HEAD(align);
1754
Jason Wang88529172013-07-18 10:55:15 +08001755 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001756 struct iov_iter i = *from;
1757
Jason Wang88529172013-07-18 10:55:15 +08001758 /* There are 256 bytes to be copied in skb, so there is
1759 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001760 * The rest of the buffer is mapped from userspace.
1761 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001762 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001763 if (copylen > good_linear)
1764 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001765 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001766 iov_iter_advance(&i, copylen);
1767 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001768 zerocopy = true;
1769 }
1770
Petar Penkov90e33d42017-09-22 13:49:15 -07001771 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001772 /* For the packet that is not easy to be processed
1773 * (e.g gso or jumbo packet), we will do it at after
1774 * skb was created with generic XDP routine.
1775 */
1776 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
Jason Wang66ccbc92017-08-11 19:41:16 +08001777 if (IS_ERR(skb)) {
Paolo Abeni608b9972016-04-13 10:52:20 +02001778 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001779 return PTR_ERR(skb);
1780 }
Jason Wang761876c2017-08-11 19:41:18 +08001781 if (!skb)
1782 return total_len;
Jason Wang66ccbc92017-08-11 19:41:16 +08001783 } else {
1784 if (!zerocopy) {
1785 copylen = len;
1786 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1787 linear = good_linear;
1788 else
1789 linear = tun16_to_cpu(tun, gso.hdr_len);
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Petar Penkov90e33d42017-09-22 13:49:15 -07001792 if (frags) {
1793 mutex_lock(&tfile->napi_mutex);
1794 skb = tun_napi_alloc_frags(tfile, copylen, from);
1795 /* tun_napi_alloc_frags() enforces a layout for the skb.
1796 * If zerocopy is enabled, then this layout will be
1797 * overwritten by zerocopy_sg_from_iter().
1798 */
1799 zerocopy = false;
1800 } else {
1801 skb = tun_alloc_skb(tfile, align, copylen, linear,
1802 noblock);
1803 }
1804
Jason Wang66ccbc92017-08-11 19:41:16 +08001805 if (IS_ERR(skb)) {
1806 if (PTR_ERR(skb) != -EAGAIN)
1807 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Petar Penkov90e33d42017-09-22 13:49:15 -07001808 if (frags)
1809 mutex_unlock(&tfile->napi_mutex);
Jason Wang66ccbc92017-08-11 19:41:16 +08001810 return PTR_ERR(skb);
1811 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001812
Jason Wang66ccbc92017-08-11 19:41:16 +08001813 if (zerocopy)
1814 err = zerocopy_sg_from_iter(skb, from);
1815 else
1816 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1817
1818 if (err) {
1819 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1820 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001821 if (frags) {
1822 tfile->napi.skb = NULL;
1823 mutex_unlock(&tfile->napi_mutex);
1824 }
1825
Jason Wang66ccbc92017-08-11 19:41:16 +08001826 return -EFAULT;
1827 }
Dave Jones8f227572006-03-11 18:49:13 -08001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001830 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
Paolo Abenidf10db92016-06-14 00:00:04 +02001831 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1832 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001833 if (frags) {
1834 tfile->napi.skb = NULL;
1835 mutex_unlock(&tfile->napi_mutex);
1836 }
1837
Paolo Abenidf10db92016-06-14 00:00:04 +02001838 return -EINVAL;
1839 }
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001842 case IFF_TUN:
1843 if (tun->flags & IFF_NO_PI) {
Alexander Potapenko2580c4c2017-09-28 11:32:37 +02001844 u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
1845
1846 switch (ip_version) {
1847 case 4:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001848 pi.proto = htons(ETH_P_IP);
1849 break;
Alexander Potapenko2580c4c2017-09-28 11:32:37 +02001850 case 6:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001851 pi.proto = htons(ETH_P_IPV6);
1852 break;
1853 default:
Paolo Abeni608b9972016-04-13 10:52:20 +02001854 this_cpu_inc(tun->pcpu_stats->rx_dropped);
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001855 kfree_skb(skb);
1856 return -EINVAL;
1857 }
1858 }
1859
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001860 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001862 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001864 case IFF_TAP:
Petar Penkov90e33d42017-09-22 13:49:15 -07001865 if (!frags)
1866 skb->protocol = eth_type_trans(skb, tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001870 /* copy skb_ubuf_info for callback when skb has no error */
1871 if (zerocopy) {
1872 skb_shinfo(skb)->destructor_arg = msg_control;
1873 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001874 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangaf1cc7a2016-11-30 13:17:51 +08001875 } else if (msg_control) {
1876 struct ubuf_info *uarg = msg_control;
1877 uarg->callback(uarg, false);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001878 }
1879
Vlad Yasevich72f65102015-02-03 16:36:16 -05001880 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001881 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001882
Jason Wang1cfe6e92017-09-04 11:36:09 +08001883 if (skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001884 struct bpf_prog *xdp_prog;
1885 int ret;
1886
Toshiaki Makita6547e382018-05-28 19:37:49 +09001887 local_bh_disable();
Jason Wang761876c2017-08-11 19:41:18 +08001888 rcu_read_lock();
1889 xdp_prog = rcu_dereference(tun->xdp_prog);
1890 if (xdp_prog) {
1891 ret = do_xdp_generic(xdp_prog, skb);
1892 if (ret != XDP_PASS) {
1893 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001894 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001895 return total_len;
1896 }
1897 }
1898 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001899 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001900 }
1901
Paolo Abenicf1a1e02018-04-20 13:18:16 +02001902 /* Compute the costly rx hash only if needed for flow updates.
1903 * We may get a very small possibility of OOO during switching, not
1904 * worth to optimize.
1905 */
1906 if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 &&
1907 !tfile->detached)
Jason Wang96f84062017-12-04 17:31:23 +08001908 rxhash = __skb_get_hash_symmetric(skb);
Petar Penkov94317092017-09-22 13:49:14 -07001909
Petar Penkov90e33d42017-09-22 13:49:15 -07001910 if (frags) {
1911 /* Exercise flow dissector code path. */
1912 u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb));
1913
Eric Dumazet010f2452017-10-17 10:07:44 -07001914 if (unlikely(headlen > skb_headlen(skb))) {
Petar Penkov90e33d42017-09-22 13:49:15 -07001915 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1916 napi_free_frags(&tfile->napi);
1917 mutex_unlock(&tfile->napi_mutex);
1918 WARN_ON(1);
1919 return -ENOMEM;
1920 }
1921
1922 local_bh_disable();
1923 napi_gro_frags(&tfile->napi);
1924 local_bh_enable();
1925 mutex_unlock(&tfile->napi_mutex);
Eric Dumazetaec72f32017-10-18 12:12:09 -07001926 } else if (tfile->napi_enabled) {
Petar Penkov94317092017-09-22 13:49:14 -07001927 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1928 int queue_len;
1929
1930 spin_lock_bh(&queue->lock);
1931 __skb_queue_tail(queue, skb);
1932 queue_len = skb_queue_len(queue);
1933 spin_unlock(&queue->lock);
1934
1935 if (!more || queue_len > NAPI_POLL_WEIGHT)
1936 napi_schedule(&tfile->napi);
1937
1938 local_bh_enable();
1939 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
1940 tun_rx_batched(tun, tfile, skb, more);
1941 } else {
1942 netif_rx_ni(skb);
1943 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001944
Paolo Abeni608b9972016-04-13 10:52:20 +02001945 stats = get_cpu_ptr(tun->pcpu_stats);
1946 u64_stats_update_begin(&stats->syncp);
1947 stats->rx_packets++;
1948 stats->rx_bytes += len;
1949 u64_stats_update_end(&stats->syncp);
1950 put_cpu_ptr(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Jason Wang96f84062017-12-04 17:31:23 +08001952 if (rxhash)
1953 tun_flow_update(tun, rxhash, tfile);
1954
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001955 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001956}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Al Virof5ff53b2014-06-19 15:36:49 -04001958static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001960 struct file *file = iocb->ki_filp;
Jason Wang54f968d2012-10-31 19:45:57 +00001961 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001962 struct tun_struct *tun = tun_get(tfile);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001963 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 if (!tun)
1966 return -EBADFD;
1967
Jason Wang5503fce2017-01-18 15:02:03 +08001968 result = tun_get_user(tun, tfile, NULL, from,
1969 file->f_flags & O_NONBLOCK, false);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001970
1971 tun_put(tun);
1972 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Jason Wangfc72d1d2018-01-04 11:14:28 +08001975static ssize_t tun_put_user_xdp(struct tun_struct *tun,
1976 struct tun_file *tfile,
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02001977 struct xdp_frame *xdp_frame,
Jason Wangfc72d1d2018-01-04 11:14:28 +08001978 struct iov_iter *iter)
1979{
1980 int vnet_hdr_sz = 0;
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02001981 size_t size = xdp_frame->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001982 struct tun_pcpu_stats *stats;
1983 size_t ret;
1984
1985 if (tun->flags & IFF_VNET_HDR) {
1986 struct virtio_net_hdr gso = { 0 };
1987
1988 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1989 if (unlikely(iov_iter_count(iter) < vnet_hdr_sz))
1990 return -EINVAL;
1991 if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) !=
1992 sizeof(gso)))
1993 return -EFAULT;
1994 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
1995 }
1996
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02001997 ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001998
1999 stats = get_cpu_ptr(tun->pcpu_stats);
2000 u64_stats_update_begin(&stats->syncp);
2001 stats->tx_packets++;
2002 stats->tx_bytes += ret;
2003 u64_stats_update_end(&stats->syncp);
2004 put_cpu_ptr(tun->pcpu_stats);
2005
2006 return ret;
2007}
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00002010static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00002011 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00002012 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08002013 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 struct tun_pi pi = { 0, skb->protocol };
Paolo Abeni608b9972016-04-13 10:52:20 +02002016 struct tun_pcpu_stats *stats;
Herbert Xue0b46d02014-11-07 21:22:23 +08002017 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08002018 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002019 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08002020 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002021
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002022 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002023 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002025 if (tun->flags & IFF_VNET_HDR)
Willem de Bruijne1edab82017-02-03 18:20:48 -05002026 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Herbert Xue0b46d02014-11-07 21:22:23 +08002028 total = skb->len + vlan_hlen + vnet_hdr_sz;
2029
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002030 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08002031 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return -EINVAL;
2033
Herbert Xue0b46d02014-11-07 21:22:23 +08002034 total += sizeof(pi);
2035 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* Packet will be striped */
2037 pi.flags |= TUN_PKT_STRIP;
2038 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002039
Herbert Xue0b46d02014-11-07 21:22:23 +08002040 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Herbert Xu2eb783c2014-11-03 04:30:14 +08002044 if (vnet_hdr_sz) {
Jarno Rajahalme9403cd72016-11-18 15:40:40 -08002045 struct virtio_net_hdr gso;
Mike Rapoport34166092016-06-08 16:09:20 +03002046
Herbert Xue0b46d02014-11-07 21:22:23 +08002047 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07002048 return -EINVAL;
2049
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08002050 if (virtio_net_hdr_from_skb(skb, &gso,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002051 tun_is_little_endian(tun), true,
2052 vlan_hlen)) {
Rusty Russellf43798c2008-07-03 03:48:02 -07002053 struct skb_shared_info *sinfo = skb_shinfo(skb);
Mike Rapoport34166092016-06-08 16:09:20 +03002054 pr_err("unexpected GSO type: "
2055 "0x%x, gso_size %d, hdr_len %d\n",
2056 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
2057 tun16_to_cpu(tun, gso.hdr_len));
2058 print_hex_dump(KERN_ERR, "tun: ",
2059 DUMP_PREFIX_NONE,
2060 16, 1, skb->head,
2061 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
2062 WARN_ON_ONCE(1);
2063 return -EINVAL;
2064 }
Rusty Russellf43798c2008-07-03 03:48:02 -07002065
Herbert Xue0b46d02014-11-07 21:22:23 +08002066 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07002067 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08002068
2069 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07002070 }
2071
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002072 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08002073 int ret;
Jason Wangaff3d702018-01-16 16:31:02 +08002074 struct veth veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Jason Wang6680ec62013-07-25 13:00:33 +08002076 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002077 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Jason Wang6680ec62013-07-25 13:00:33 +08002079 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08002080
Herbert Xue0b46d02014-11-07 21:22:23 +08002081 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
2082 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08002083 goto done;
2084
Herbert Xue0b46d02014-11-07 21:22:23 +08002085 ret = copy_to_iter(&veth, sizeof(veth), iter);
2086 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08002087 goto done;
2088 }
2089
Herbert Xue0b46d02014-11-07 21:22:23 +08002090 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08002091
2092done:
Paolo Abeni608b9972016-04-13 10:52:20 +02002093 /* caller is in process context, */
2094 stats = get_cpu_ptr(tun->pcpu_stats);
2095 u64_stats_update_begin(&stats->syncp);
2096 stats->tx_packets++;
2097 stats->tx_bytes += skb->len + vlan_hlen;
2098 u64_stats_update_end(&stats->syncp);
2099 put_cpu_ptr(tun->pcpu_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 return total;
2102}
2103
Jason Wangfc72d1d2018-01-04 11:14:28 +08002104static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err)
Jason Wang1576d982016-06-30 14:45:36 +08002105{
2106 DECLARE_WAITQUEUE(wait, current);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002107 void *ptr = NULL;
Jason Wangf48cc6b2016-07-04 13:53:38 +08002108 int error = 0;
Jason Wang1576d982016-06-30 14:45:36 +08002109
Jason Wangfc72d1d2018-01-04 11:14:28 +08002110 ptr = ptr_ring_consume(&tfile->tx_ring);
2111 if (ptr)
Jason Wang1576d982016-06-30 14:45:36 +08002112 goto out;
2113 if (noblock) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002114 error = -EAGAIN;
Jason Wang1576d982016-06-30 14:45:36 +08002115 goto out;
2116 }
2117
2118 add_wait_queue(&tfile->wq.wait, &wait);
2119 current->state = TASK_INTERRUPTIBLE;
2120
2121 while (1) {
Jason Wangfc72d1d2018-01-04 11:14:28 +08002122 ptr = ptr_ring_consume(&tfile->tx_ring);
2123 if (ptr)
Jason Wang1576d982016-06-30 14:45:36 +08002124 break;
2125 if (signal_pending(current)) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002126 error = -ERESTARTSYS;
Jason Wang1576d982016-06-30 14:45:36 +08002127 break;
2128 }
2129 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002130 error = -EFAULT;
Jason Wang1576d982016-06-30 14:45:36 +08002131 break;
2132 }
2133
2134 schedule();
2135 }
2136
2137 current->state = TASK_RUNNING;
2138 remove_wait_queue(&tfile->wq.wait, &wait);
2139
2140out:
Jason Wangf48cc6b2016-07-04 13:53:38 +08002141 *err = error;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002142 return ptr;
Jason Wang1576d982016-06-30 14:45:36 +08002143}
2144
Jason Wang54f968d2012-10-31 19:45:57 +00002145static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05002146 struct iov_iter *to,
Jason Wangfc72d1d2018-01-04 11:14:28 +08002147 int noblock, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Al Viro9b067032014-11-07 13:52:07 -05002149 ssize_t ret;
Jason Wang1576d982016-06-30 14:45:36 +08002150 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Rami Rosen3872baf2012-11-25 22:07:41 +00002152 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Wei Xuc33ee152017-12-01 05:10:37 -05002154 if (!iov_iter_count(to)) {
Jason Wangfc72d1d2018-01-04 11:14:28 +08002155 tun_ptr_free(ptr);
Al Viro9b067032014-11-07 13:52:07 -05002156 return 0;
Wei Xuc33ee152017-12-01 05:10:37 -05002157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Jason Wangfc72d1d2018-01-04 11:14:28 +08002159 if (!ptr) {
Jason Wangac77cfd2017-05-17 12:14:43 +08002160 /* Read frames from ring */
Jason Wangfc72d1d2018-01-04 11:14:28 +08002161 ptr = tun_ring_recv(tfile, noblock, &err);
2162 if (!ptr)
Jason Wangac77cfd2017-05-17 12:14:43 +08002163 return err;
2164 }
Herbert Xue0b46d02014-11-07 21:22:23 +08002165
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002166 if (tun_is_xdp_frame(ptr)) {
2167 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002168
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002169 ret = tun_put_user_xdp(tun, tfile, xdpf, to);
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +02002170 xdp_return_frame(xdpf);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002171 } else {
2172 struct sk_buff *skb = ptr;
2173
2174 ret = tun_put_user(tun, tfile, skb, to);
2175 if (unlikely(ret < 0))
2176 kfree_skb(skb);
2177 else
2178 consume_skb(skb);
2179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002181 return ret;
2182}
2183
Al Viro9b067032014-11-07 13:52:07 -05002184static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002185{
2186 struct file *file = iocb->ki_filp;
2187 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08002188 struct tun_struct *tun = tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05002189 ssize_t len = iov_iter_count(to), ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002190
2191 if (!tun)
2192 return -EBADFD;
Jason Wangac77cfd2017-05-17 12:14:43 +08002193 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL);
David S. Miller42404c02013-12-10 22:05:45 -05002194 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08002195 if (ret > 0)
2196 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002197 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return ret;
2199}
2200
Jason Wangcd5681d2018-01-16 16:31:01 +08002201static void tun_prog_free(struct rcu_head *rcu)
Jason Wang96f84062017-12-04 17:31:23 +08002202{
Jason Wangcd5681d2018-01-16 16:31:01 +08002203 struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu);
Jason Wang96f84062017-12-04 17:31:23 +08002204
2205 bpf_prog_destroy(prog->prog);
2206 kfree(prog);
2207}
2208
Jason Wang9d6474e2018-01-22 10:55:38 +08002209static int __tun_set_ebpf(struct tun_struct *tun,
2210 struct tun_prog __rcu **prog_p,
Jason Wangcd5681d2018-01-16 16:31:01 +08002211 struct bpf_prog *prog)
Jason Wang96f84062017-12-04 17:31:23 +08002212{
Jason Wangcd5681d2018-01-16 16:31:01 +08002213 struct tun_prog *old, *new = NULL;
Jason Wang96f84062017-12-04 17:31:23 +08002214
2215 if (prog) {
2216 new = kmalloc(sizeof(*new), GFP_KERNEL);
2217 if (!new)
2218 return -ENOMEM;
2219 new->prog = prog;
2220 }
2221
Jason Wang124da8f2017-12-08 12:02:30 +08002222 spin_lock_bh(&tun->lock);
Jason Wangcd5681d2018-01-16 16:31:01 +08002223 old = rcu_dereference_protected(*prog_p,
Jason Wang124da8f2017-12-08 12:02:30 +08002224 lockdep_is_held(&tun->lock));
Jason Wangcd5681d2018-01-16 16:31:01 +08002225 rcu_assign_pointer(*prog_p, new);
Jason Wang124da8f2017-12-08 12:02:30 +08002226 spin_unlock_bh(&tun->lock);
Jason Wang96f84062017-12-04 17:31:23 +08002227
2228 if (old)
Jason Wangcd5681d2018-01-16 16:31:01 +08002229 call_rcu(&old->rcu, tun_prog_free);
Jason Wang96f84062017-12-04 17:31:23 +08002230
2231 return 0;
2232}
2233
Jason Wang96442e422012-10-31 19:46:02 +00002234static void tun_free_netdev(struct net_device *dev)
2235{
2236 struct tun_struct *tun = netdev_priv(dev);
2237
Jason Wang4008e972012-12-13 23:53:30 +00002238 BUG_ON(!(list_empty(&tun->disabled)));
Paolo Abeni608b9972016-04-13 10:52:20 +02002239 free_percpu(tun->pcpu_stats);
Jason Wang96442e422012-10-31 19:46:02 +00002240 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00002241 security_tun_dev_free_security(tun->security);
Jason Wangcd5681d2018-01-16 16:31:01 +08002242 __tun_set_ebpf(tun, &tun->steering_prog, NULL);
Jason Wangaff3d702018-01-16 16:31:02 +08002243 __tun_set_ebpf(tun, &tun->filter_prog, NULL);
Jason Wang96442e422012-10-31 19:46:02 +00002244}
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246static void tun_setup(struct net_device *dev)
2247{
2248 struct tun_struct *tun = netdev_priv(dev);
2249
Eric W. Biederman0625c882012-02-07 16:48:55 -08002250 tun->owner = INVALID_UID;
2251 tun->group = INVALID_GID;
Chas Williams4e24f2d2018-06-02 17:49:53 -04002252 tun_default_link_ksettings(dev, &tun->link_ksettings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 dev->ethtool_ops = &tun_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04002255 dev->needs_free_netdev = true;
2256 dev->priv_destructor = tun_free_netdev;
Jason Wang016adb72016-04-08 13:26:48 +08002257 /* We prefer our own queue length */
2258 dev->tx_queue_len = TUN_READQ_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002261/* Trivial set of netlink ops to allow deleting tun or tap
2262 * device with netlink.
2263 */
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002264static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2265 struct netlink_ext_ack *extack)
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002266{
2267 return -EINVAL;
2268}
2269
Sabrina Dubroca1ec010e2018-02-16 11:03:07 +01002270static size_t tun_get_size(const struct net_device *dev)
2271{
2272 BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t));
2273 BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t));
2274
2275 return nla_total_size(sizeof(uid_t)) + /* OWNER */
2276 nla_total_size(sizeof(gid_t)) + /* GROUP */
2277 nla_total_size(sizeof(u8)) + /* TYPE */
2278 nla_total_size(sizeof(u8)) + /* PI */
2279 nla_total_size(sizeof(u8)) + /* VNET_HDR */
2280 nla_total_size(sizeof(u8)) + /* PERSIST */
2281 nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */
2282 nla_total_size(sizeof(u32)) + /* NUM_QUEUES */
2283 nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */
2284 0;
2285}
2286
2287static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
2288{
2289 struct tun_struct *tun = netdev_priv(dev);
2290
2291 if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK))
2292 goto nla_put_failure;
2293 if (uid_valid(tun->owner) &&
2294 nla_put_u32(skb, IFLA_TUN_OWNER,
2295 from_kuid_munged(current_user_ns(), tun->owner)))
2296 goto nla_put_failure;
2297 if (gid_valid(tun->group) &&
2298 nla_put_u32(skb, IFLA_TUN_GROUP,
2299 from_kgid_munged(current_user_ns(), tun->group)))
2300 goto nla_put_failure;
2301 if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI)))
2302 goto nla_put_failure;
2303 if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR)))
2304 goto nla_put_failure;
2305 if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST)))
2306 goto nla_put_failure;
2307 if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE,
2308 !!(tun->flags & IFF_MULTI_QUEUE)))
2309 goto nla_put_failure;
2310 if (tun->flags & IFF_MULTI_QUEUE) {
2311 if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues))
2312 goto nla_put_failure;
2313 if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES,
2314 tun->numdisabled))
2315 goto nla_put_failure;
2316 }
2317
2318 return 0;
2319
2320nla_put_failure:
2321 return -EMSGSIZE;
2322}
2323
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002324static struct rtnl_link_ops tun_link_ops __read_mostly = {
2325 .kind = DRV_NAME,
2326 .priv_size = sizeof(struct tun_struct),
2327 .setup = tun_setup,
2328 .validate = tun_validate,
Sabrina Dubroca1ec010e2018-02-16 11:03:07 +01002329 .get_size = tun_get_size,
2330 .fill_info = tun_fill_info,
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002331};
2332
Herbert Xu33dccbb2009-02-05 21:25:32 -08002333static void tun_sock_write_space(struct sock *sk)
2334{
Jason Wang54f968d2012-10-31 19:45:57 +00002335 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00002336 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002337
2338 if (!sock_writeable(sk))
2339 return;
2340
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002341 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Herbert Xu33dccbb2009-02-05 21:25:32 -08002342 return;
2343
Eric Dumazet43815482010-04-29 11:01:49 +00002344 wqueue = sk_sleep(sk);
2345 if (wqueue && waitqueue_active(wqueue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002346 wake_up_interruptible_sync_poll(wqueue, EPOLLOUT |
2347 EPOLLWRNORM | EPOLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07002348
Jason Wang54f968d2012-10-31 19:45:57 +00002349 tfile = container_of(sk, struct tun_file, sk);
2350 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002351}
2352
Ying Xue1b784142015-03-02 15:37:48 +08002353static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002354{
Jason Wang54f968d2012-10-31 19:45:57 +00002355 int ret;
2356 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002357 struct tun_struct *tun = tun_get(tfile);
Jason Wang54f968d2012-10-31 19:45:57 +00002358
2359 if (!tun)
2360 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04002361
Al Viroc0371da2014-11-24 10:42:55 -05002362 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
Jason Wang5503fce2017-01-18 15:02:03 +08002363 m->msg_flags & MSG_DONTWAIT,
2364 m->msg_flags & MSG_MORE);
Jason Wang54f968d2012-10-31 19:45:57 +00002365 tun_put(tun);
2366 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002367}
2368
Ying Xue1b784142015-03-02 15:37:48 +08002369static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002370 int flags)
2371{
Jason Wang54f968d2012-10-31 19:45:57 +00002372 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002373 struct tun_struct *tun = tun_get(tfile);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002374 void *ptr = m->msg_control;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002375 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00002376
Wei Xuc33ee152017-12-01 05:10:37 -05002377 if (!tun) {
2378 ret = -EBADFD;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002379 goto out_free;
Wei Xuc33ee152017-12-01 05:10:37 -05002380 }
Jason Wang54f968d2012-10-31 19:45:57 +00002381
Richard Cochraneda29772013-07-19 19:40:10 +02002382 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00002383 ret = -EINVAL;
Wei Xuc33ee152017-12-01 05:10:37 -05002384 goto out_put_tun;
Gao feng3811ae72013-04-24 21:59:23 +00002385 }
Richard Cochraneda29772013-07-19 19:40:10 +02002386 if (flags & MSG_ERRQUEUE) {
2387 ret = sock_recv_errqueue(sock->sk, m, total_len,
2388 SOL_PACKET, TUN_TX_TIMESTAMP);
2389 goto out;
2390 }
Jason Wangfc72d1d2018-01-04 11:14:28 +08002391 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr);
Alex Gartrell87897932014-12-25 23:05:03 -08002392 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05002393 m->msg_flags |= MSG_TRUNC;
2394 ret = flags & MSG_TRUNC ? ret : total_len;
2395 }
Gao feng3811ae72013-04-24 21:59:23 +00002396out:
Jason Wang54f968d2012-10-31 19:45:57 +00002397 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002398 return ret;
Wei Xuc33ee152017-12-01 05:10:37 -05002399
2400out_put_tun:
2401 tun_put(tun);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002402out_free:
2403 tun_ptr_free(ptr);
Wei Xuc33ee152017-12-01 05:10:37 -05002404 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002405}
2406
Jason Wangfc72d1d2018-01-04 11:14:28 +08002407static int tun_ptr_peek_len(void *ptr)
2408{
2409 if (likely(ptr)) {
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002410 if (tun_is_xdp_frame(ptr)) {
2411 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002412
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002413 return xdpf->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002414 }
2415 return __skb_array_len_with_tag(ptr);
2416 } else {
2417 return 0;
2418 }
2419}
2420
Jason Wang1576d982016-06-30 14:45:36 +08002421static int tun_peek_len(struct socket *sock)
2422{
2423 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2424 struct tun_struct *tun;
2425 int ret = 0;
2426
yuan linyu9484dc72017-09-23 22:36:52 +08002427 tun = tun_get(tfile);
Jason Wang1576d982016-06-30 14:45:36 +08002428 if (!tun)
2429 return 0;
2430
Jason Wangfc72d1d2018-01-04 11:14:28 +08002431 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len);
Jason Wang1576d982016-06-30 14:45:36 +08002432 tun_put(tun);
2433
2434 return ret;
2435}
2436
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002437/* Ops structure to mimic raw sockets with tun */
2438static const struct proto_ops tun_socket_ops = {
Jason Wang1576d982016-06-30 14:45:36 +08002439 .peek_len = tun_peek_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002440 .sendmsg = tun_sendmsg,
2441 .recvmsg = tun_recvmsg,
2442};
2443
Herbert Xu33dccbb2009-02-05 21:25:32 -08002444static struct proto tun_proto = {
2445 .name = "tun",
2446 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00002447 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08002448};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002449
David Woodhouse980c9e82009-05-09 22:54:21 -07002450static int tun_flags(struct tun_struct *tun)
2451{
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02002452 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07002453}
2454
2455static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
2456 char *buf)
2457{
2458 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2459 return sprintf(buf, "0x%x\n", tun_flags(tun));
2460}
2461
2462static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
2463 char *buf)
2464{
2465 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002466 return uid_valid(tun->owner)?
2467 sprintf(buf, "%u\n",
2468 from_kuid_munged(current_user_ns(), tun->owner)):
2469 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002470}
2471
2472static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
2473 char *buf)
2474{
2475 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002476 return gid_valid(tun->group) ?
2477 sprintf(buf, "%u\n",
2478 from_kgid_munged(current_user_ns(), tun->group)):
2479 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002480}
2481
2482static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
2483static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
2484static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
2485
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002486static struct attribute *tun_dev_attrs[] = {
2487 &dev_attr_tun_flags.attr,
2488 &dev_attr_owner.attr,
2489 &dev_attr_group.attr,
2490 NULL
2491};
2492
2493static const struct attribute_group tun_attr_group = {
2494 .attrs = tun_dev_attrs
2495};
2496
Pavel Emelyanovd647a592008-04-16 00:41:16 -07002497static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00002500 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 struct net_device *dev;
2502 int err;
2503
Jason Wang7c0c3b12013-01-11 16:59:33 +00002504 if (tfile->detached)
2505 return -EINVAL;
2506
Petar Penkov90e33d42017-09-22 13:49:15 -07002507 if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
2508 if (!capable(CAP_NET_ADMIN))
2509 return -EPERM;
2510
2511 if (!(ifr->ifr_flags & IFF_NAPI) ||
2512 (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
2513 return -EINVAL;
2514 }
2515
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002516 dev = __dev_get_by_name(net, ifr->ifr_name);
2517 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07002518 if (ifr->ifr_flags & IFF_TUN_EXCL)
2519 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002520 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2521 tun = netdev_priv(dev);
2522 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2523 tun = netdev_priv(dev);
2524 else
2525 return -EINVAL;
2526
Jason Wang8e6d91a2013-05-28 18:32:11 +00002527 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002528 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00002529 return -EINVAL;
2530
Jason Wangcde8b152012-10-31 19:46:01 +00002531 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04002532 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002533 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04002534 if (err < 0)
2535 return err;
2536
Petar Penkov94317092017-09-22 13:49:14 -07002537 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002538 ifr->ifr_flags & IFF_NAPI,
2539 ifr->ifr_flags & IFF_NAPI_FRAGS);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00002540 if (err < 0)
2541 return err;
Jason Wang4008e972012-12-13 23:53:30 +00002542
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002543 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00002544 (tun->numqueues + tun->numdisabled > 1)) {
2545 /* One or more queue has already been attached, no need
2546 * to initialize the device again.
2547 */
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002548 netdev_state_change(dev);
Jason Wange8dbad62013-04-22 20:40:39 +00002549 return 0;
2550 }
Sabrina Dubroca9fffc5c2018-04-10 16:28:55 +02002551
2552 tun->flags = (tun->flags & ~TUN_FEATURES) |
2553 (ifr->ifr_flags & TUN_FEATURES);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002554
2555 netdev_state_change(dev);
2556 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 char *name;
2558 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00002559 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2560 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Eric W. Biedermanc260b772012-11-18 21:34:11 +00002562 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002563 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04002564 err = security_tun_dev_create();
2565 if (err < 0)
2566 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 /* Set dev type */
2569 if (ifr->ifr_flags & IFF_TUN) {
2570 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002571 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 name = "tun%d";
2573 } else if (ifr->ifr_flags & IFF_TAP) {
2574 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002575 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002577 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00002578 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (*ifr->ifr_name)
2581 name = ifr->ifr_name;
2582
Jason Wangc8d68e62012-10-31 19:46:00 +00002583 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02002584 NET_NAME_UNKNOWN, tun_setup, queues,
2585 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00002586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (!dev)
2588 return -ENOMEM;
Cong Wang0ad646c2017-10-13 11:58:53 -07002589 err = dev_get_valid_name(net, dev, name);
Julien Gomes5c25f652017-10-25 11:50:50 -07002590 if (err < 0)
Cong Wang0ad646c2017-10-13 11:58:53 -07002591 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07002593 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002594 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002595 dev->ifindex = tfile->ifindex;
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002596 dev->sysfs_groups[0] = &tun_attr_group;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 tun = netdev_priv(dev);
2599 tun->dev = dev;
2600 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002601 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002602 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Paolo Abenieaea34b2016-02-26 10:45:40 +01002604 tun->align = NET_SKB_PAD;
Jason Wang54f968d2012-10-31 19:45:57 +00002605 tun->filter_attached = false;
2606 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Jason Wang5503fce2017-01-18 15:02:03 +08002607 tun->rx_batched = 0;
Jason Wang96f84062017-12-04 17:31:23 +08002608 RCU_INIT_POINTER(tun->steering_prog, NULL);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002609
Paolo Abeni608b9972016-04-13 10:52:20 +02002610 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2611 if (!tun->pcpu_stats) {
2612 err = -ENOMEM;
2613 goto err_free_dev;
2614 }
2615
Jason Wang96442e422012-10-31 19:46:02 +00002616 spin_lock_init(&tun->lock);
2617
Paul Moore5dbbaf22013-01-14 07:12:19 +00002618 err = security_tun_dev_alloc_security(&tun->security);
2619 if (err < 0)
Paolo Abeni608b9972016-04-13 10:52:20 +02002620 goto err_free_stat;
Paul Moore2b980db2009-08-28 18:12:43 -04002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04002623 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00002624
Michał Mirosław88255372011-04-19 06:13:10 +00002625 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08002626 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2627 NETIF_F_HW_VLAN_STAG_TX;
Paolo Abeni2a2bbf12016-04-14 18:39:39 +02002628 dev->features = dev->hw_features | NETIF_F_LLTX;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09002629 dev->vlan_features = dev->features &
2630 ~(NETIF_F_HW_VLAN_CTAG_TX |
2631 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00002632
Sabrina Dubroca9fffc5c2018-04-10 16:28:55 +02002633 tun->flags = (tun->flags & ~TUN_FEATURES) |
2634 (ifr->ifr_flags & TUN_FEATURES);
2635
Jason Wang4008e972012-12-13 23:53:30 +00002636 INIT_LIST_HEAD(&tun->disabled);
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002637 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI,
2638 ifr->ifr_flags & IFF_NAPI_FRAGS);
Jason Wangeb0fb362012-12-02 17:19:45 +00002639 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002640 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 err = register_netdevice(tun->dev);
2643 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08002644 goto err_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
2646
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00002647 netif_carrier_on(tun->dev);
2648
Joe Perches6b8a66e2011-03-02 07:18:10 +00002649 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002651 /* Make sure persistent devices do not get stuck in
2652 * xoff state.
2653 */
2654 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00002655 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 strcpy(ifr->ifr_name, tun->dev->name);
2658 return 0;
2659
Jason Wang662ca432013-09-11 18:09:48 +08002660err_detach:
2661 tun_detach_all(dev);
Eric Dumazetff244c62017-08-18 13:39:56 -07002662 /* register_netdevice() already called tun_free_netdev() */
2663 goto err_free_dev;
2664
Jason Wang662ca432013-09-11 18:09:48 +08002665err_free_flow:
2666 tun_flow_uninit(tun);
2667 security_tun_dev_free_security(tun->security);
Paolo Abeni608b9972016-04-13 10:52:20 +02002668err_free_stat:
2669 free_percpu(tun->pcpu_stats);
Jason Wang662ca432013-09-11 18:09:48 +08002670err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return err;
2673}
2674
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002675static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00002676 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07002677{
Joe Perches6b8a66e2011-03-02 07:18:10 +00002678 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07002679
2680 strcpy(ifr->ifr_name, tun->dev->name);
2681
David Woodhouse980c9e82009-05-09 22:54:21 -07002682 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07002683
Mark McLoughline3b99552008-08-15 15:09:56 -07002684}
2685
Rusty Russell5228ddc2008-07-03 03:46:16 -07002686/* This is like a cut-down ethtool ops, except done via tun fd so no
2687 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00002688static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07002689{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002690 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002691
2692 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00002693 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002694 arg &= ~TUN_F_CSUM;
2695
2696 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2697 if (arg & TUN_F_TSO_ECN) {
2698 features |= NETIF_F_TSO_ECN;
2699 arg &= ~TUN_F_TSO_ECN;
2700 }
2701 if (arg & TUN_F_TSO4)
2702 features |= NETIF_F_TSO;
2703 if (arg & TUN_F_TSO6)
2704 features |= NETIF_F_TSO6;
2705 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2706 }
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05002707
2708 arg &= ~TUN_F_UFO;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002709 }
2710
2711 /* This gives the user a way to test for new features in future by
2712 * trying to set them. */
2713 if (arg)
2714 return -EINVAL;
2715
Michał Mirosław88255372011-04-19 06:13:10 +00002716 tun->set_features = features;
Yaroslav Isakov09050952017-03-16 22:44:10 +03002717 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2718 tun->dev->wanted_features |= features;
Michał Mirosław88255372011-04-19 06:13:10 +00002719 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07002720
2721 return 0;
2722}
2723
Jason Wangc8d68e62012-10-31 19:46:00 +00002724static void tun_detach_filter(struct tun_struct *tun, int n)
2725{
2726 int i;
2727 struct tun_file *tfile;
2728
2729 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002730 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002731 lock_sock(tfile->socket.sk);
2732 sk_detach_filter(tfile->socket.sk);
2733 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002734 }
2735
2736 tun->filter_attached = false;
2737}
2738
2739static int tun_attach_filter(struct tun_struct *tun)
2740{
2741 int i, ret = 0;
2742 struct tun_file *tfile;
2743
2744 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002745 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002746 lock_sock(tfile->socket.sk);
2747 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2748 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002749 if (ret) {
2750 tun_detach_filter(tun, i);
2751 return ret;
2752 }
2753 }
2754
2755 tun->filter_attached = true;
2756 return ret;
2757}
2758
2759static void tun_set_sndbuf(struct tun_struct *tun)
2760{
2761 struct tun_file *tfile;
2762 int i;
2763
2764 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002765 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00002766 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2767 }
2768}
2769
Jason Wangcde8b152012-10-31 19:46:01 +00002770static int tun_set_queue(struct file *file, struct ifreq *ifr)
2771{
2772 struct tun_file *tfile = file->private_data;
2773 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00002774 int ret = 0;
2775
2776 rtnl_lock();
2777
2778 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00002779 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002780 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00002781 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002782 goto unlock;
2783 }
2784 ret = security_tun_dev_attach_queue(tun->security);
2785 if (ret < 0)
2786 goto unlock;
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002787 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI,
2788 tun->flags & IFF_NAPI_FRAGS);
Jason Wang4008e972012-12-13 23:53:30 +00002789 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002790 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002791 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00002792 ret = -EINVAL;
2793 else
2794 __tun_detach(tfile, false);
2795 } else
Jason Wangcde8b152012-10-31 19:46:01 +00002796 ret = -EINVAL;
2797
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002798 if (ret >= 0)
2799 netdev_state_change(tun->dev);
2800
Paul Moore5dbbaf22013-01-14 07:12:19 +00002801unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00002802 rtnl_unlock();
2803 return ret;
2804}
2805
Jason Wangcd5681d2018-01-16 16:31:01 +08002806static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog **prog_p,
2807 void __user *data)
Jason Wang96f84062017-12-04 17:31:23 +08002808{
2809 struct bpf_prog *prog;
2810 int fd;
2811
2812 if (copy_from_user(&fd, data, sizeof(fd)))
2813 return -EFAULT;
2814
2815 if (fd == -1) {
2816 prog = NULL;
2817 } else {
2818 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
2819 if (IS_ERR(prog))
2820 return PTR_ERR(prog);
2821 }
2822
Jason Wangcd5681d2018-01-16 16:31:01 +08002823 return __tun_set_ebpf(tun, prog_p, prog);
Jason Wang96f84062017-12-04 17:31:23 +08002824}
2825
Arnd Bergmann50857e22009-11-06 22:52:32 -08002826static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2827 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002829 struct tun_file *tfile = file->private_data;
Kirill Tkhaif6637062018-05-08 19:21:34 +03002830 struct net *net = sock_net(&tfile->sk);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002831 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 void __user* argp = (void __user*)arg;
2833 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08002834 kuid_t owner;
2835 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002836 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002837 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002838 unsigned int ifindex;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002839 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002840 int ret;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002841 bool do_notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Kirill Tkhaif2780d62018-02-14 16:40:14 +03002843 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
2844 (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08002845 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07002847 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00002848 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07002849 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002850 if (cmd == TUNGETFEATURES) {
2851 /* Currently this just means: "what IFF flags are valid?".
2852 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02002853 * TUNSETIFF.
2854 */
2855 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00002856 (unsigned int __user*)argp);
Kirill Tkhaif6637062018-05-08 19:21:34 +03002857 } else if (cmd == TUNSETQUEUE) {
Jason Wangcde8b152012-10-31 19:46:01 +00002858 return tun_set_queue(file, &ifr);
Kirill Tkhaif6637062018-05-08 19:21:34 +03002859 } else if (cmd == SIOCGSKNS) {
2860 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2861 return -EPERM;
2862 return open_related_ns(&net->ns, get_net_ns);
2863 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002864
Jason Wangc8d68e62012-10-31 19:46:00 +00002865 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00002866 rtnl_lock();
2867
yuan linyu9484dc72017-09-23 22:36:52 +08002868 tun = tun_get(tfile);
Gao Feng0f16bc12016-10-25 22:26:09 +08002869 if (cmd == TUNSETIFF) {
2870 ret = -EEXIST;
2871 if (tun)
2872 goto unlock;
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2875
Kirill Tkhaif2780d62018-02-14 16:40:14 +03002876 ret = tun_set_iff(net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Herbert Xu876bfd42009-08-06 14:22:44 +00002878 if (ret)
2879 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Arnd Bergmann50857e22009-11-06 22:52:32 -08002881 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00002882 ret = -EFAULT;
2883 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002885 if (cmd == TUNSETIFINDEX) {
2886 ret = -EPERM;
2887 if (tun)
2888 goto unlock;
2889
2890 ret = -EFAULT;
2891 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2892 goto unlock;
2893
2894 ret = 0;
2895 tfile->ifindex = ifindex;
2896 goto unlock;
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Herbert Xu876bfd42009-08-06 14:22:44 +00002899 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00002901 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Jason Wang1e588332012-10-31 19:45:56 +00002903 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Eric W. Biederman631ab462009-01-20 11:00:40 +00002905 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07002907 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00002908 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07002909
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002910 if (tfile->detached)
2911 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04002912 if (!tfile->socket.sk->sk_filter)
2913 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04002914
Arnd Bergmann50857e22009-11-06 22:52:32 -08002915 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002916 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07002917 break;
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 case TUNSETNOCSUM:
2920 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Michał Mirosław88255372011-04-19 06:13:10 +00002922 /* [unimplemented] */
2923 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00002924 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 break;
2926
2927 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00002928 /* Disable/Enable persist mode. Keep an extra reference to the
2929 * module to prevent the module being unprobed.
2930 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002931 if (arg && !(tun->flags & IFF_PERSIST)) {
2932 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002933 __module_get(THIS_MODULE);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002934 do_notify = true;
Jason Wangdd38bd82013-01-11 16:59:34 +00002935 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002936 if (!arg && (tun->flags & IFF_PERSIST)) {
2937 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00002938 module_put(THIS_MODULE);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002939 do_notify = true;
Jason Wang54f968d2012-10-31 19:45:57 +00002940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Joe Perches6b8a66e2011-03-02 07:18:10 +00002942 tun_debug(KERN_INFO, tun, "persist %s\n",
2943 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 break;
2945
2946 case TUNSETOWNER:
2947 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002948 owner = make_kuid(current_user_ns(), arg);
2949 if (!uid_valid(owner)) {
2950 ret = -EINVAL;
2951 break;
2952 }
2953 tun->owner = owner;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002954 do_notify = true;
Jason Wang1e588332012-10-31 19:45:56 +00002955 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002956 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 break;
2958
Guido Guenther8c644622007-07-02 22:50:25 -07002959 case TUNSETGROUP:
2960 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08002961 group = make_kgid(current_user_ns(), arg);
2962 if (!gid_valid(group)) {
2963 ret = -EINVAL;
2964 break;
2965 }
2966 tun->group = group;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002967 do_notify = true;
Jason Wang1e588332012-10-31 19:45:56 +00002968 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08002969 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07002970 break;
2971
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002972 case TUNSETLINK:
2973 /* Only allow setting the type when the interface is down */
2974 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002975 tun_debug(KERN_INFO, tun,
2976 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07002977 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002978 } else {
2979 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00002980 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
2981 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07002982 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002983 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00002984 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07002985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986#ifdef TUN_DEBUG
2987 case TUNSETDEBUG:
2988 tun->debug = arg;
2989 break;
2990#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07002991 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00002992 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002993 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002994
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002995 case TUNSETTXFILTER:
2996 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00002997 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002998 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00002999 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07003000 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003004 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003005 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
3006 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08003007 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00003008 ret = -EFAULT;
3009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003012 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00003013 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
3014 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08003015
Kim B. Heino40102372008-02-29 12:26:21 -08003016 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003017 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003018
3019 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00003020 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003021 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
3022 ret = -EFAULT;
3023 break;
3024
3025 case TUNSETSNDBUF:
3026 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
3027 ret = -EFAULT;
3028 break;
3029 }
Craig Gallek931619222017-10-30 18:50:11 -04003030 if (sndbuf <= 0) {
3031 ret = -EINVAL;
3032 break;
3033 }
Herbert Xu33dccbb2009-02-05 21:25:32 -08003034
Jason Wangc8d68e62012-10-31 19:46:00 +00003035 tun->sndbuf = sndbuf;
3036 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08003037 break;
3038
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02003039 case TUNGETVNETHDRSZ:
3040 vnet_hdr_sz = tun->vnet_hdr_sz;
3041 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
3042 ret = -EFAULT;
3043 break;
3044
3045 case TUNSETVNETHDRSZ:
3046 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
3047 ret = -EFAULT;
3048 break;
3049 }
3050 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
3051 ret = -EINVAL;
3052 break;
3053 }
3054
3055 tun->vnet_hdr_sz = vnet_hdr_sz;
3056 break;
3057
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02003058 case TUNGETVNETLE:
3059 le = !!(tun->flags & TUN_VNET_LE);
3060 if (put_user(le, (int __user *)argp))
3061 ret = -EFAULT;
3062 break;
3063
3064 case TUNSETVNETLE:
3065 if (get_user(le, (int __user *)argp)) {
3066 ret = -EFAULT;
3067 break;
3068 }
3069 if (le)
3070 tun->flags |= TUN_VNET_LE;
3071 else
3072 tun->flags &= ~TUN_VNET_LE;
3073 break;
3074
Greg Kurz8b8e6582015-04-24 14:50:36 +02003075 case TUNGETVNETBE:
3076 ret = tun_get_vnet_be(tun, argp);
3077 break;
3078
3079 case TUNSETVNETBE:
3080 ret = tun_set_vnet_be(tun, argp);
3081 break;
3082
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003083 case TUNATTACHFILTER:
3084 /* Can be set only for TAPs */
3085 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003086 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003087 break;
3088 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00003089 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003090 break;
3091
Jason Wangc8d68e62012-10-31 19:46:00 +00003092 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003093 break;
3094
3095 case TUNDETACHFILTER:
3096 /* Can be set only for TAPs */
3097 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003098 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003099 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00003100 ret = 0;
3101 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003102 break;
3103
Pavel Emelyanov76975e92013-08-21 14:32:39 +04003104 case TUNGETFILTER:
3105 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003106 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04003107 break;
3108 ret = -EFAULT;
3109 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
3110 break;
3111 ret = 0;
3112 break;
3113
Jason Wang96f84062017-12-04 17:31:23 +08003114 case TUNSETSTEERINGEBPF:
Jason Wangcd5681d2018-01-16 16:31:01 +08003115 ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
Jason Wang96f84062017-12-04 17:31:23 +08003116 break;
3117
Jason Wangaff3d702018-01-16 16:31:02 +08003118 case TUNSETFILTEREBPF:
3119 ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
3120 break;
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00003123 ret = -EINVAL;
3124 break;
Joe Perchesee289b62010-05-17 22:47:34 -07003125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003127 if (do_notify)
3128 netdev_state_change(tun->dev);
3129
Herbert Xu876bfd42009-08-06 14:22:44 +00003130unlock:
3131 rtnl_unlock();
3132 if (tun)
3133 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003134 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Arnd Bergmann50857e22009-11-06 22:52:32 -08003137static long tun_chr_ioctl(struct file *file,
3138 unsigned int cmd, unsigned long arg)
3139{
3140 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
3141}
3142
3143#ifdef CONFIG_COMPAT
3144static long tun_chr_compat_ioctl(struct file *file,
3145 unsigned int cmd, unsigned long arg)
3146{
3147 switch (cmd) {
3148 case TUNSETIFF:
3149 case TUNGETIFF:
3150 case TUNSETTXFILTER:
3151 case TUNGETSNDBUF:
3152 case TUNSETSNDBUF:
3153 case SIOCGIFHWADDR:
3154 case SIOCSIFHWADDR:
3155 arg = (unsigned long)compat_ptr(arg);
3156 break;
3157 default:
3158 arg = (compat_ulong_t)arg;
3159 break;
3160 }
3161
3162 /*
3163 * compat_ifreq is shorter than ifreq, so we must not access beyond
3164 * the end of that structure. All fields that are used in this
3165 * driver are compatible though, we don't need to convert the
3166 * contents.
3167 */
3168 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
3169}
3170#endif /* CONFIG_COMPAT */
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172static int tun_chr_fasync(int fd, struct file *file, int on)
3173{
Jason Wang54f968d2012-10-31 19:45:57 +00003174 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 int ret;
3176
Jason Wang54f968d2012-10-31 19:45:57 +00003177 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06003178 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if (on) {
Eric W. Biederman01919132017-07-16 22:05:57 -05003181 __f_setown(file, task_pid(current), PIDTYPE_TGID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00003182 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003183 } else
Jason Wang54f968d2012-10-31 19:45:57 +00003184 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06003185 ret = 0;
3186out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06003187 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
3190static int tun_chr_open(struct inode *inode, struct file * file)
3191{
Eric W. Biederman140e807da2015-05-08 21:07:08 -05003192 struct net *net = current->nsproxy->net_ns;
Eric W. Biederman631ab462009-01-20 11:00:40 +00003193 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07003194
Joe Perches6b8a66e2011-03-02 07:18:10 +00003195 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00003196
Eric W. Biederman140e807da2015-05-08 21:07:08 -05003197 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003198 &tun_proto, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003199 if (!tfile)
3200 return -ENOMEM;
Jason Wangb196d882018-05-11 10:49:25 +08003201 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) {
3202 sk_free(&tfile->sk);
3203 return -ENOMEM;
3204 }
3205
Eric Dumazetc7256f52018-09-28 14:51:48 -07003206 mutex_init(&tfile->napi_mutex);
Monam Agarwalc9566742014-03-24 00:02:32 +05303207 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang54f968d2012-10-31 19:45:57 +00003208 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04003209 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00003210
Jason Wang54f968d2012-10-31 19:45:57 +00003211 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07003212 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00003213
3214 tfile->socket.file = file;
3215 tfile->socket.ops = &tun_socket_ops;
3216
3217 sock_init_data(&tfile->socket, &tfile->sk);
Jason Wang54f968d2012-10-31 19:45:57 +00003218
3219 tfile->sk.sk_write_space = tun_sock_write_space;
3220 tfile->sk.sk_sndbuf = INT_MAX;
3221
Eric W. Biederman631ab462009-01-20 11:00:40 +00003222 file->private_data = tfile;
Jason Wang4008e972012-12-13 23:53:30 +00003223 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00003224
Jason Wang19a6afb2013-06-08 14:17:41 +08003225 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return 0;
3228}
3229
3230static int tun_chr_close(struct inode *inode, struct file *file)
3231{
Eric W. Biederman631ab462009-01-20 11:00:40 +00003232 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Jason Wangc8d68e62012-10-31 19:46:00 +00003234 tun_detach(tfile, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 return 0;
3237}
3238
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003239#ifdef CONFIG_PROC_FS
yuan linyu9484dc72017-09-23 22:36:52 +08003240static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003241{
yuan linyu9484dc72017-09-23 22:36:52 +08003242 struct tun_file *tfile = file->private_data;
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003243 struct tun_struct *tun;
3244 struct ifreq ifr;
3245
3246 memset(&ifr, 0, sizeof(ifr));
3247
3248 rtnl_lock();
yuan linyu9484dc72017-09-23 22:36:52 +08003249 tun = tun_get(tfile);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003250 if (tun)
3251 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
3252 rtnl_unlock();
3253
3254 if (tun)
3255 tun_put(tun);
3256
Joe Perchesa3816ab2014-09-29 16:08:25 -07003257 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003258}
3259#endif
3260
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003261static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003262 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05003264 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04003265 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08003267 .unlocked_ioctl = tun_chr_ioctl,
3268#ifdef CONFIG_COMPAT
3269 .compat_ioctl = tun_chr_compat_ioctl,
3270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 .open = tun_chr_open,
3272 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003273 .fasync = tun_chr_fasync,
3274#ifdef CONFIG_PROC_FS
3275 .show_fdinfo = tun_chr_show_fdinfo,
3276#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277};
3278
3279static struct miscdevice tun_miscdev = {
3280 .minor = TUN_MINOR,
3281 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02003282 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284};
3285
3286/* ethtool interface */
3287
Chas Williams4e24f2d2018-06-02 17:49:53 -04003288static void tun_default_link_ksettings(struct net_device *dev,
3289 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
Philippe Reynes29ccc492017-03-11 22:03:50 +01003291 ethtool_link_ksettings_zero_link_mode(cmd, supported);
3292 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
3293 cmd->base.speed = SPEED_10;
3294 cmd->base.duplex = DUPLEX_FULL;
3295 cmd->base.port = PORT_TP;
3296 cmd->base.phy_address = 0;
3297 cmd->base.autoneg = AUTONEG_DISABLE;
Chas Williams4e24f2d2018-06-02 17:49:53 -04003298}
3299
3300static int tun_get_link_ksettings(struct net_device *dev,
3301 struct ethtool_link_ksettings *cmd)
3302{
3303 struct tun_struct *tun = netdev_priv(dev);
3304
3305 memcpy(cmd, &tun->link_ksettings, sizeof(*cmd));
3306 return 0;
3307}
3308
3309static int tun_set_link_ksettings(struct net_device *dev,
3310 const struct ethtool_link_ksettings *cmd)
3311{
3312 struct tun_struct *tun = netdev_priv(dev);
3313
3314 memcpy(&tun->link_ksettings, cmd, sizeof(*cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return 0;
3316}
3317
3318static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3319{
3320 struct tun_struct *tun = netdev_priv(dev);
3321
Rick Jones33a5ba12011-11-15 14:59:53 +00003322 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
3323 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
3325 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003326 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00003327 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003329 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00003330 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 break;
3332 }
3333}
3334
3335static u32 tun_get_msglevel(struct net_device *dev)
3336{
3337#ifdef TUN_DEBUG
3338 struct tun_struct *tun = netdev_priv(dev);
3339 return tun->debug;
3340#else
3341 return -EOPNOTSUPP;
3342#endif
3343}
3344
3345static void tun_set_msglevel(struct net_device *dev, u32 value)
3346{
3347#ifdef TUN_DEBUG
3348 struct tun_struct *tun = netdev_priv(dev);
3349 tun->debug = value;
3350#endif
3351}
3352
Jason Wang5503fce2017-01-18 15:02:03 +08003353static int tun_get_coalesce(struct net_device *dev,
3354 struct ethtool_coalesce *ec)
3355{
3356 struct tun_struct *tun = netdev_priv(dev);
3357
3358 ec->rx_max_coalesced_frames = tun->rx_batched;
3359
3360 return 0;
3361}
3362
3363static int tun_set_coalesce(struct net_device *dev,
3364 struct ethtool_coalesce *ec)
3365{
3366 struct tun_struct *tun = netdev_priv(dev);
3367
3368 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
3369 tun->rx_batched = NAPI_POLL_WEIGHT;
3370 else
3371 tun->rx_batched = ec->rx_max_coalesced_frames;
3372
3373 return 0;
3374}
3375
Jeff Garzik7282d492006-09-13 14:30:00 -04003376static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 .get_drvinfo = tun_get_drvinfo,
3378 .get_msglevel = tun_get_msglevel,
3379 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00003380 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02003381 .get_ts_info = ethtool_op_get_ts_info,
Jason Wang5503fce2017-01-18 15:02:03 +08003382 .get_coalesce = tun_get_coalesce,
3383 .set_coalesce = tun_set_coalesce,
Philippe Reynes29ccc492017-03-11 22:03:50 +01003384 .get_link_ksettings = tun_get_link_ksettings,
Chas Williams4e24f2d2018-06-02 17:49:53 -04003385 .set_link_ksettings = tun_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386};
3387
Jason Wang1576d982016-06-30 14:45:36 +08003388static int tun_queue_resize(struct tun_struct *tun)
3389{
3390 struct net_device *dev = tun->dev;
3391 struct tun_file *tfile;
Jason Wang5990a302018-01-04 11:14:27 +08003392 struct ptr_ring **rings;
Jason Wang1576d982016-06-30 14:45:36 +08003393 int n = tun->numqueues + tun->numdisabled;
3394 int ret, i;
3395
Jason Wang5990a302018-01-04 11:14:27 +08003396 rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
3397 if (!rings)
Jason Wang1576d982016-06-30 14:45:36 +08003398 return -ENOMEM;
3399
3400 for (i = 0; i < tun->numqueues; i++) {
3401 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wang5990a302018-01-04 11:14:27 +08003402 rings[i] = &tfile->tx_ring;
Jason Wang1576d982016-06-30 14:45:36 +08003403 }
3404 list_for_each_entry(tfile, &tun->disabled, next)
Jason Wang5990a302018-01-04 11:14:27 +08003405 rings[i++] = &tfile->tx_ring;
Jason Wang1576d982016-06-30 14:45:36 +08003406
Jason Wang5990a302018-01-04 11:14:27 +08003407 ret = ptr_ring_resize_multiple(rings, n,
3408 dev->tx_queue_len, GFP_KERNEL,
Jason Wangfc72d1d2018-01-04 11:14:28 +08003409 tun_ptr_free);
Jason Wang1576d982016-06-30 14:45:36 +08003410
Jason Wang5990a302018-01-04 11:14:27 +08003411 kfree(rings);
Jason Wang1576d982016-06-30 14:45:36 +08003412 return ret;
3413}
3414
3415static int tun_device_event(struct notifier_block *unused,
3416 unsigned long event, void *ptr)
3417{
3418 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3419 struct tun_struct *tun = netdev_priv(dev);
3420
Craig Gallek86dfb4ac2016-07-06 18:44:20 -04003421 if (dev->rtnl_link_ops != &tun_link_ops)
3422 return NOTIFY_DONE;
3423
Jason Wang1576d982016-06-30 14:45:36 +08003424 switch (event) {
3425 case NETDEV_CHANGE_TX_QUEUE_LEN:
3426 if (tun_queue_resize(tun))
3427 return NOTIFY_BAD;
3428 break;
3429 default:
3430 break;
3431 }
3432
3433 return NOTIFY_DONE;
3434}
3435
3436static struct notifier_block tun_notifier_block __read_mostly = {
3437 .notifier_call = tun_device_event,
3438};
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440static int __init tun_init(void)
3441{
3442 int ret = 0;
3443
Joe Perches6b8a66e2011-03-02 07:18:10 +00003444 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003446 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003447 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003448 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003449 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003450 }
3451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003453 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003454 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003455 goto err_misc;
3456 }
Jason Wang1576d982016-06-30 14:45:36 +08003457
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003458 ret = register_netdevice_notifier(&tun_notifier_block);
3459 if (ret) {
3460 pr_err("Can't register netdevice notifier\n");
3461 goto err_notifier;
3462 }
3463
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003464 return 0;
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003465
3466err_notifier:
3467 misc_deregister(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003468err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003469 rtnl_link_unregister(&tun_link_ops);
3470err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return ret;
3472}
3473
3474static void tun_cleanup(void)
3475{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003476 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003477 rtnl_link_unregister(&tun_link_ops);
Jason Wang1576d982016-06-30 14:45:36 +08003478 unregister_netdevice_notifier(&tun_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003481/* Get an underlying socket object from tun file. Returns error unless file is
3482 * attached to a device. The returned object works like a packet socket, it
3483 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
3484 * holding a reference to the file for as long as the socket is in use. */
3485struct socket *tun_get_socket(struct file *file)
3486{
Jason Wang6e914fc2012-10-31 19:45:58 +00003487 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003488 if (file->f_op != &tun_fops)
3489 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00003490 tfile = file->private_data;
3491 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003492 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00003493 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003494}
3495EXPORT_SYMBOL_GPL(tun_get_socket);
3496
Jason Wang5990a302018-01-04 11:14:27 +08003497struct ptr_ring *tun_get_tx_ring(struct file *file)
Jason Wang83339c62017-05-17 12:14:41 +08003498{
3499 struct tun_file *tfile;
3500
3501 if (file->f_op != &tun_fops)
3502 return ERR_PTR(-EINVAL);
3503 tfile = file->private_data;
3504 if (!tfile)
3505 return ERR_PTR(-EBADFD);
Jason Wang5990a302018-01-04 11:14:27 +08003506 return &tfile->tx_ring;
Jason Wang83339c62017-05-17 12:14:41 +08003507}
Jason Wang5990a302018-01-04 11:14:27 +08003508EXPORT_SYMBOL_GPL(tun_get_tx_ring);
Jason Wang83339c62017-05-17 12:14:41 +08003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510module_init(tun_init);
3511module_exit(tun_cleanup);
3512MODULE_DESCRIPTION(DRV_DESCRIPTION);
3513MODULE_AUTHOR(DRV_COPYRIGHT);
3514MODULE_LICENSE("GPL");
3515MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02003516MODULE_ALIAS("devname:net/tun");