blob: fed85447701a55c537c2ad58db3cf1b418c8ef5d [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * TUN - Universal TUN/TAP device driver.
4 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
7 */
8
9/*
10 * Changes:
11 *
Mike Kershawff4cc3a2005-09-01 17:40:05 -070012 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
13 * Add TUNSETLINK ioctl to set the link encapsulation
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Mark Smith <markzzzsmith@yahoo.com.au>
Joe Perches344dc8e2012-07-12 19:33:09 +000016 * Use eth_random_addr() for tap MAC address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
19 * Fixes in packet dropping, queue length setting and queue wakeup.
20 * Increased default tx queue length.
21 * Added ethtool API.
22 * Minor cleanups
23 *
24 * Daniel Podlejski <underley@underley.eu.org>
25 * Modifications for 2.3.99-pre5 kernel.
26 */
27
Joe Perches6b8a66e2011-03-02 07:18:10 +000028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define DRV_NAME "tun"
31#define DRV_VERSION "1.6"
32#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
33#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/errno.h>
37#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010038#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/major.h>
40#include <linux/slab.h>
41#include <linux/poll.h>
42#include <linux/fcntl.h>
43#include <linux/init.h>
44#include <linux/skbuff.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/miscdevice.h>
48#include <linux/ethtool.h>
49#include <linux/rtnetlink.h>
Arnd Bergmann50857e22009-11-06 22:52:32 -080050#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/if.h>
52#include <linux/if_arp.h>
53#include <linux/if_ether.h>
54#include <linux/if_tun.h>
Jason Wang6680ec62013-07-25 13:00:33 +080055#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070057#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070058#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000059#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070060#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070061#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080062#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080063#include <net/sock.h>
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +020064#include <net/xdp.h>
Jason A. Donenfeldb9815eb2020-06-29 19:06:22 -060065#include <net/ip_tunnels.h>
Masatake YAMATO93e14b62014-01-29 16:43:31 +090066#include <linux/seq_file.h>
Herbert Xue0b46d02014-11-07 21:22:23 +080067#include <linux/uio.h>
Jason Wang1576d982016-06-30 14:45:36 +080068#include <linux/skb_array.h>
Jason Wang761876c2017-08-11 19:41:18 +080069#include <linux/bpf.h>
70#include <linux/bpf_trace.h>
Petar Penkov90e33d42017-09-22 13:49:15 -070071#include <linux/mutex.h>
Phillip Pottercca8ea32021-04-06 18:45:54 +010072#include <linux/ieee802154.h>
73#include <linux/if_ltalk.h>
74#include <uapi/linux/if_fddi.h>
75#include <uapi/linux/if_hippi.h>
76#include <uapi/linux/if_fc.h>
77#include <net/ax25.h>
78#include <net/rose.h>
79#include <net/6lowpan.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
Jason Wang7df13212017-09-04 11:36:08 +080087#define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
Jason Wang66ccbc92017-08-11 19:41:16 +080088
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +020089/* TUN device flags */
90
91/* IFF_ATTACH_QUEUE is never stored in device flags,
92 * overload it to mean fasync when stored there.
93 */
94#define TUN_FASYNC IFF_ATTACH_QUEUE
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +020095/* High bits in flags field are unused. */
96#define TUN_VNET_LE 0x80000000
Greg Kurz8b8e6582015-04-24 14:50:36 +020097#define TUN_VNET_BE 0x40000000
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +020098
99#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Petar Penkov90e33d42017-09-22 13:49:15 -0700100 IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS)
101
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000102#define GOODCOPY_LEN 128
103
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700104#define FLT_EXACT_COUNT 8
105struct tap_filter {
106 unsigned int count; /* Number of addrs. Zero means disabled */
107 u32 mask[2]; /* Mask of the hashed addrs */
108 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
109};
110
Pankaj Guptabaf71c52015-01-12 11:41:29 +0530111/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
112 * to max number of VCPUs in guest. */
113#define MAX_TAP_QUEUES 256
Jason Wangb8732fb2013-01-23 03:59:13 +0000114#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000115
Jason Wang96442e422012-10-31 19:46:02 +0000116#define TUN_FLOW_EXPIRE (3 * HZ)
117
Jason Wang54f968d2012-10-31 19:45:57 +0000118/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800119 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000120 * to serve as one transmit queue for tuntap device. The sock_fprog and
121 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000122 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000123 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000124 *
125 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000126 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000127 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000128 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000129struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000130 struct sock sk;
131 struct socket socket;
Jason Wang6e914fc2012-10-31 19:45:58 +0000132 struct tun_struct __rcu *tun;
Jason Wang54f968d2012-10-31 19:45:57 +0000133 struct fasync_struct *fasync;
134 /* only used for fasnyc */
135 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400136 union {
137 u16 queue_index;
138 unsigned int ifindex;
139 };
Petar Penkov94317092017-09-22 13:49:14 -0700140 struct napi_struct napi;
Eric Dumazetaec72f32017-10-18 12:12:09 -0700141 bool napi_enabled;
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700142 bool napi_frags_enabled;
Petar Penkov90e33d42017-09-22 13:49:15 -0700143 struct mutex napi_mutex; /* Protects access to the above napi */
Jason Wang4008e972012-12-13 23:53:30 +0000144 struct list_head next;
145 struct tun_struct *detached;
Jason Wang5990a302018-01-04 11:14:27 +0800146 struct ptr_ring tx_ring;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100147 struct xdp_rxq_info xdp_rxq;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000148};
149
Jason Wangf9e06c42018-11-15 17:43:10 +0800150struct tun_page {
151 struct page *page;
152 int count;
153};
154
Jason Wang96442e422012-10-31 19:46:02 +0000155struct tun_flow_entry {
156 struct hlist_node hash_link;
157 struct rcu_head rcu;
158 struct tun_struct *tun;
159
160 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800161 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000162 int queue_index;
Li RongQing83b1bc12018-12-06 16:08:17 +0800163 unsigned long updated ____cacheline_aligned_in_smp;
Jason Wang96442e422012-10-31 19:46:02 +0000164};
165
166#define TUN_NUM_FLOW_ENTRIES 1024
Li RongQingf13b5462018-08-03 15:50:02 +0800167#define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1)
Jason Wang96442e422012-10-31 19:46:02 +0000168
Jason Wangcd5681d2018-01-16 16:31:01 +0800169struct tun_prog {
Jason Wang96f84062017-12-04 17:31:23 +0800170 struct rcu_head rcu;
171 struct bpf_prog *prog;
172};
173
Jason Wang54f968d2012-10-31 19:45:57 +0000174/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000175 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000176 * file were attached to a persist device.
177 */
Rusty Russell14daa022008-04-12 18:48:58 -0700178struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000179 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
180 unsigned int numqueues;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700181 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800182 kuid_t owner;
183 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700184
Rusty Russell14daa022008-04-12 18:48:58 -0700185 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000186 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000187#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
David S. Millerd591a1f2017-07-03 06:35:32 -0700188 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200189
Paolo Abenieaea34b2016-02-26 10:45:40 +0100190 int align;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200191 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000192 int sndbuf;
193 struct tap_filter txflt;
194 struct sock_fprog fprog;
195 /* protected by rtnl lock */
196 bool filter_attached;
Michal Kubecek34241702020-03-04 17:24:14 +0100197 u32 msg_enable;
Jason Wang96442e422012-10-31 19:46:02 +0000198 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000199 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
200 struct timer_list flow_gc_timer;
201 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000202 unsigned int numdisabled;
203 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000204 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000205 u32 flow_count;
Jason Wang5503fce2017-01-18 15:02:03 +0800206 u32 rx_batched;
Heiner Kallweit497a5752020-11-07 21:50:56 +0100207 atomic_long_t rx_frame_errors;
Jason Wang761876c2017-08-11 19:41:18 +0800208 struct bpf_prog __rcu *xdp_prog;
Jason Wangcd5681d2018-01-16 16:31:01 +0800209 struct tun_prog __rcu *steering_prog;
Jason Wangaff3d702018-01-16 16:31:02 +0800210 struct tun_prog __rcu *filter_prog;
Chas Williams4e24f2d2018-06-02 17:49:53 -0400211 struct ethtool_link_ksettings link_ksettings;
George Kennedy158b5152021-12-16 13:25:32 -0500212 /* init args */
213 struct file *file;
214 struct ifreq *ifr;
Rusty Russell14daa022008-04-12 18:48:58 -0700215};
216
Jason Wangaff3d702018-01-16 16:31:02 +0800217struct veth {
218 __be16 h_vlan_proto;
219 __be16 h_vlan_TCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
George Kennedy158b5152021-12-16 13:25:32 -0500222static void tun_flow_init(struct tun_struct *tun);
223static void tun_flow_uninit(struct tun_struct *tun);
224
Petar Penkov94317092017-09-22 13:49:14 -0700225static int tun_napi_receive(struct napi_struct *napi, int budget)
226{
227 struct tun_file *tfile = container_of(napi, struct tun_file, napi);
228 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
229 struct sk_buff_head process_queue;
230 struct sk_buff *skb;
231 int received = 0;
232
233 __skb_queue_head_init(&process_queue);
234
235 spin_lock(&queue->lock);
236 skb_queue_splice_tail_init(queue, &process_queue);
237 spin_unlock(&queue->lock);
238
239 while (received < budget && (skb = __skb_dequeue(&process_queue))) {
240 napi_gro_receive(napi, skb);
241 ++received;
242 }
243
244 if (!skb_queue_empty(&process_queue)) {
245 spin_lock(&queue->lock);
246 skb_queue_splice(&process_queue, queue);
247 spin_unlock(&queue->lock);
248 }
249
250 return received;
251}
252
253static int tun_napi_poll(struct napi_struct *napi, int budget)
254{
255 unsigned int received;
256
257 received = tun_napi_receive(napi, budget);
258
259 if (received < budget)
260 napi_complete_done(napi, received);
261
262 return received;
263}
264
265static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700266 bool napi_en, bool napi_frags)
Petar Penkov94317092017-09-22 13:49:14 -0700267{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700268 tfile->napi_enabled = napi_en;
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700269 tfile->napi_frags_enabled = napi_en && napi_frags;
Petar Penkov94317092017-09-22 13:49:14 -0700270 if (napi_en) {
Petar Penkovc39e3422019-11-14 09:52:09 -0800271 netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
272 NAPI_POLL_WEIGHT);
Petar Penkov94317092017-09-22 13:49:14 -0700273 napi_enable(&tfile->napi);
274 }
275}
276
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700277static void tun_napi_disable(struct tun_file *tfile)
Petar Penkov94317092017-09-22 13:49:14 -0700278{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700279 if (tfile->napi_enabled)
Petar Penkov94317092017-09-22 13:49:14 -0700280 napi_disable(&tfile->napi);
281}
282
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700283static void tun_napi_del(struct tun_file *tfile)
Petar Penkov94317092017-09-22 13:49:14 -0700284{
Eric Dumazetaec72f32017-10-18 12:12:09 -0700285 if (tfile->napi_enabled)
Petar Penkov94317092017-09-22 13:49:14 -0700286 netif_napi_del(&tfile->napi);
287}
288
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700289static bool tun_napi_frags_enabled(const struct tun_file *tfile)
Petar Penkov90e33d42017-09-22 13:49:15 -0700290{
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700291 return tfile->napi_frags_enabled;
Petar Penkov90e33d42017-09-22 13:49:15 -0700292}
293
Greg Kurz8b8e6582015-04-24 14:50:36 +0200294#ifdef CONFIG_TUN_VNET_CROSS_LE
295static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
296{
297 return tun->flags & TUN_VNET_BE ? false :
298 virtio_legacy_is_little_endian();
299}
300
301static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
302{
303 int be = !!(tun->flags & TUN_VNET_BE);
304
305 if (put_user(be, argp))
306 return -EFAULT;
307
308 return 0;
309}
310
311static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
312{
313 int be;
314
315 if (get_user(be, argp))
316 return -EFAULT;
317
318 if (be)
319 tun->flags |= TUN_VNET_BE;
320 else
321 tun->flags &= ~TUN_VNET_BE;
322
323 return 0;
324}
325#else
326static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
327{
328 return virtio_legacy_is_little_endian();
329}
330
331static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
332{
333 return -EINVAL;
334}
335
336static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
337{
338 return -EINVAL;
339}
340#endif /* CONFIG_TUN_VNET_CROSS_LE */
341
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200342static inline bool tun_is_little_endian(struct tun_struct *tun)
343{
Greg Kurz7d824102015-04-24 14:26:24 +0200344 return tun->flags & TUN_VNET_LE ||
Greg Kurz8b8e6582015-04-24 14:50:36 +0200345 tun_legacy_is_little_endian(tun);
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200346}
347
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300348static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
349{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200350 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300351}
352
353static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
354{
Greg Kurz25bd55bb2015-04-24 14:24:38 +0200355 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300356}
357
Jason Wang96442e422012-10-31 19:46:02 +0000358static inline u32 tun_hashfn(u32 rxhash)
359{
Li RongQingf13b5462018-08-03 15:50:02 +0800360 return rxhash & TUN_MASK_FLOW_ENTRIES;
Jason Wang96442e422012-10-31 19:46:02 +0000361}
362
363static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
364{
365 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000366
Sasha Levinb67bfe02013-02-27 17:06:00 -0800367 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000368 if (e->rxhash == rxhash)
369 return e;
370 }
371 return NULL;
372}
373
374static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
375 struct hlist_head *head,
376 u32 rxhash, u16 queue_index)
377{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000378 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
379
Jason Wang96442e422012-10-31 19:46:02 +0000380 if (e) {
Michal Kubecek34241702020-03-04 17:24:14 +0100381 netif_info(tun, tx_queued, tun->dev,
382 "create flow: hash %u index %u\n",
383 rxhash, queue_index);
Jason Wang96442e422012-10-31 19:46:02 +0000384 e->updated = jiffies;
385 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800386 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000387 e->queue_index = queue_index;
388 e->tun = tun;
389 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000390 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000391 }
392 return e;
393}
394
Jason Wang96442e422012-10-31 19:46:02 +0000395static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
396{
Michal Kubecek34241702020-03-04 17:24:14 +0100397 netif_info(tun, tx_queued, tun->dev, "delete flow: hash %u index %u\n",
398 e->rxhash, e->queue_index);
Jason Wang96442e422012-10-31 19:46:02 +0000399 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000400 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000401 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000402}
403
404static void tun_flow_flush(struct tun_struct *tun)
405{
406 int i;
407
408 spin_lock_bh(&tun->lock);
409 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
410 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800411 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000412
Sasha Levinb67bfe02013-02-27 17:06:00 -0800413 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000414 tun_flow_delete(tun, e);
415 }
416 spin_unlock_bh(&tun->lock);
417}
418
419static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
420{
421 int i;
422
423 spin_lock_bh(&tun->lock);
424 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
425 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800426 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000427
Sasha Levinb67bfe02013-02-27 17:06:00 -0800428 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000429 if (e->queue_index == queue_index)
430 tun_flow_delete(tun, e);
431 }
432 }
433 spin_unlock_bh(&tun->lock);
434}
435
Kees Cooke99e88a2017-10-16 14:43:17 -0700436static void tun_flow_cleanup(struct timer_list *t)
Jason Wang96442e422012-10-31 19:46:02 +0000437{
Kees Cooke99e88a2017-10-16 14:43:17 -0700438 struct tun_struct *tun = from_timer(tun, t, flow_gc_timer);
Jason Wang96442e422012-10-31 19:46:02 +0000439 unsigned long delay = tun->ageing_time;
440 unsigned long next_timer = jiffies + delay;
441 unsigned long count = 0;
442 int i;
443
Eric Dumazet7dbfb4e2017-10-20 11:29:55 -0700444 spin_lock(&tun->lock);
Jason Wang96442e422012-10-31 19:46:02 +0000445 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
446 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800447 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000448
Sasha Levinb67bfe02013-02-27 17:06:00 -0800449 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000450 unsigned long this_timer;
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700451
Jason Wang96442e422012-10-31 19:46:02 +0000452 this_timer = e->updated + delay;
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700453 if (time_before_eq(this_timer, jiffies)) {
Jason Wang96442e422012-10-31 19:46:02 +0000454 tun_flow_delete(tun, e);
Eric Dumazet81d98fa2017-10-20 11:29:56 -0700455 continue;
456 }
457 count++;
458 if (time_before(this_timer, next_timer))
Jason Wang96442e422012-10-31 19:46:02 +0000459 next_timer = this_timer;
460 }
461 }
462
463 if (count)
464 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
Eric Dumazet7dbfb4e2017-10-20 11:29:55 -0700465 spin_unlock(&tun->lock);
Jason Wang96442e422012-10-31 19:46:02 +0000466}
467
Eric Dumazet49974422012-12-12 19:22:57 +0000468static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000469 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000470{
471 struct hlist_head *head;
472 struct tun_flow_entry *e;
473 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000474 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000475
Li RongQing5c327f62018-12-06 16:28:11 +0800476 head = &tun->flows[tun_hashfn(rxhash)];
Jason Wang96442e422012-10-31 19:46:02 +0000477
478 rcu_read_lock();
479
Jason Wang96442e422012-10-31 19:46:02 +0000480 e = tun_flow_find(head, rxhash);
481 if (likely(e)) {
482 /* TODO: keep queueing to old queue until it's empty? */
Eric Dumazet4ffdd222019-10-09 09:20:02 -0700483 if (READ_ONCE(e->queue_index) != queue_index)
484 WRITE_ONCE(e->queue_index, queue_index);
Li RongQing83b1bc12018-12-06 16:08:17 +0800485 if (e->updated != jiffies)
486 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800487 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000488 } else {
489 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000490 if (!tun_flow_find(head, rxhash) &&
491 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000492 tun_flow_create(tun, head, rxhash, queue_index);
493
494 if (!timer_pending(&tun->flow_gc_timer))
495 mod_timer(&tun->flow_gc_timer,
496 round_jiffies_up(jiffies + delay));
497 spin_unlock_bh(&tun->lock);
498 }
499
Jason Wang96442e422012-10-31 19:46:02 +0000500 rcu_read_unlock();
501}
502
Michal Kubecek516c5122020-03-04 17:23:59 +0100503/* Save the hash received in the stack receive path and update the
Tom Herbert9bc88932013-12-22 18:54:32 +0800504 * flow_hash table accordingly.
505 */
506static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
507{
Eric Dumazet567e4b72015-02-06 12:59:01 -0800508 if (unlikely(e->rps_rxhash != hash))
Tom Herbert9bc88932013-12-22 18:54:32 +0800509 e->rps_rxhash = hash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800510}
511
Wang Li4b035272018-10-09 10:32:04 +0800512/* We try to identify a flow through its rxhash. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800513 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000514 * the rxq based on the txq where the last packet of the flow comes. As
515 * the userspace application move between processors, we may get a
Wang Li4b035272018-10-09 10:32:04 +0800516 * different rxq no. here.
Jason Wangc8d68e62012-10-31 19:46:00 +0000517 */
Jason Wang96f84062017-12-04 17:31:23 +0800518static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
Jason Wangc8d68e62012-10-31 19:46:00 +0000519{
Jason Wang96442e422012-10-31 19:46:02 +0000520 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000521 u32 txq = 0;
522 u32 numqueues = 0;
523
Mark Rutland6aa7de02017-10-23 14:07:29 -0700524 numqueues = READ_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000525
Jason Wangfeec0842017-06-06 14:09:49 +0800526 txq = __skb_get_hash_symmetric(skb);
Wang Li4b035272018-10-09 10:32:04 +0800527 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
528 if (e) {
529 tun_flow_save_rps_rxhash(e, txq);
530 txq = e->queue_index;
531 } else {
532 /* use multiply and shift instead of expensive divide */
533 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000534 }
535
Jason Wangc8d68e62012-10-31 19:46:00 +0000536 return txq;
537}
538
Jason Wang96f84062017-12-04 17:31:23 +0800539static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
540{
Jason Wangcd5681d2018-01-16 16:31:01 +0800541 struct tun_prog *prog;
Jason Wanga35d3102019-05-08 23:20:17 -0400542 u32 numqueues;
Jason Wang96f84062017-12-04 17:31:23 +0800543 u16 ret = 0;
544
Jason Wanga35d3102019-05-08 23:20:17 -0400545 numqueues = READ_ONCE(tun->numqueues);
546 if (!numqueues)
547 return 0;
548
Jason Wang96f84062017-12-04 17:31:23 +0800549 prog = rcu_dereference(tun->steering_prog);
550 if (prog)
551 ret = bpf_prog_run_clear_cb(prog->prog, skb);
552
Jason Wanga35d3102019-05-08 23:20:17 -0400553 return ret % numqueues;
Jason Wang96f84062017-12-04 17:31:23 +0800554}
555
556static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +0100557 struct net_device *sb_dev)
Jason Wang96f84062017-12-04 17:31:23 +0800558{
559 struct tun_struct *tun = netdev_priv(dev);
560 u16 ret;
561
562 rcu_read_lock();
563 if (rcu_dereference(tun->steering_prog))
564 ret = tun_ebpf_select_queue(tun, skb);
565 else
566 ret = tun_automq_select_queue(tun, skb);
567 rcu_read_unlock();
568
569 return ret;
570}
571
Jason Wangcde8b152012-10-31 19:46:01 +0000572static inline bool tun_not_capable(struct tun_struct *tun)
573{
574 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000575 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000576
577 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
578 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000579 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000580}
581
Jason Wangc8d68e62012-10-31 19:46:00 +0000582static void tun_set_real_num_queues(struct tun_struct *tun)
583{
584 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
585 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
586}
587
Jason Wang4008e972012-12-13 23:53:30 +0000588static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
589{
590 tfile->detached = tun;
591 list_add_tail(&tfile->next, &tun->disabled);
592 ++tun->numdisabled;
593}
594
Jason Wangd32649d2012-12-18 11:00:27 +0800595static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000596{
597 struct tun_struct *tun = tfile->detached;
598
599 tfile->detached = NULL;
600 list_del_init(&tfile->next);
601 --tun->numdisabled;
602 return tun;
603}
604
Jason Wang3a403072018-03-09 14:50:34 +0800605void tun_ptr_free(void *ptr)
Jason Wangfc72d1d2018-01-04 11:14:28 +0800606{
607 if (!ptr)
608 return;
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200609 if (tun_is_xdp_frame(ptr)) {
610 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800611
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +0200612 xdp_return_frame(xdpf);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800613 } else {
614 __skb_array_destroy_skb(ptr);
615 }
616}
Jason Wang3a403072018-03-09 14:50:34 +0800617EXPORT_SYMBOL_GPL(tun_ptr_free);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800618
Jason Wang4bfb0512013-09-05 17:53:59 +0800619static void tun_queue_purge(struct tun_file *tfile)
620{
Jason Wangfc72d1d2018-01-04 11:14:28 +0800621 void *ptr;
Jason Wang1576d982016-06-30 14:45:36 +0800622
Jason Wangfc72d1d2018-01-04 11:14:28 +0800623 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL)
624 tun_ptr_free(ptr);
Jason Wang1576d982016-06-30 14:45:36 +0800625
Jason Wang5503fce2017-01-18 15:02:03 +0800626 skb_queue_purge(&tfile->sk.sk_write_queue);
Jason Wang4bfb0512013-09-05 17:53:59 +0800627 skb_queue_purge(&tfile->sk.sk_error_queue);
628}
629
Jason Wangc8d68e62012-10-31 19:46:00 +0000630static void __tun_detach(struct tun_file *tfile, bool clean)
631{
632 struct tun_file *ntfile;
633 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000634
Jason Wangb8deabd2013-01-11 16:59:32 +0000635 tun = rtnl_dereference(tfile->tun);
636
Petar Penkov94317092017-09-22 13:49:14 -0700637 if (tun && clean) {
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700638 tun_napi_disable(tfile);
639 tun_napi_del(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700640 }
641
Jason Wang9e857222013-01-28 01:05:19 +0000642 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000643 u16 index = tfile->queue_index;
644 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000645
646 rcu_assign_pointer(tun->tfiles[index],
647 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000648 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000649 ntfile->queue_index = index;
Jason Wang9871a9e2019-05-08 23:20:18 -0400650 rcu_assign_pointer(tun->tfiles[tun->numqueues - 1],
651 NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000652
653 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000654 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530655 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000656 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000657 } else
Jason Wang4008e972012-12-13 23:53:30 +0000658 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000659
660 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000661 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000662 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800663 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000664 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000665 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000666 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000667 sock_put(&tfile->sk);
668 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000669
670 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000671 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
672 netif_carrier_off(tun->dev);
673
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200674 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000675 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000676 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000677 }
Jason Wangb196d882018-05-11 10:49:25 +0800678 if (tun)
679 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wang7063efd2018-05-16 20:39:33 +0800680 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free);
Eric W. Biederman140e807da2015-05-08 21:07:08 -0500681 sock_put(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000682 }
683}
684
685static void tun_detach(struct tun_file *tfile, bool clean)
686{
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200687 struct tun_struct *tun;
688 struct net_device *dev;
689
Jason Wangc8d68e62012-10-31 19:46:00 +0000690 rtnl_lock();
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200691 tun = rtnl_dereference(tfile->tun);
692 dev = tun ? tun->dev : NULL;
Jason Wangc8d68e62012-10-31 19:46:00 +0000693 __tun_detach(tfile, clean);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +0200694 if (dev)
695 netdev_state_change(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000696 rtnl_unlock();
697}
698
699static void tun_detach_all(struct net_device *dev)
700{
701 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000702 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000703 int i, n = tun->numqueues;
704
705 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000706 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000707 BUG_ON(!tfile);
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700708 tun_napi_disable(tfile);
Jason Wangaddf8fc2016-05-19 13:36:51 +0800709 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700710 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530711 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000712 --tun->numqueues;
713 }
Jason Wang9e857222013-01-28 01:05:19 +0000714 list_for_each_entry(tfile, &tun->disabled, next) {
Jason Wangaddf8fc2016-05-19 13:36:51 +0800715 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
Xi Wang9e641bd2014-05-16 15:11:48 -0700716 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530717 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000718 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000719 BUG_ON(tun->numqueues != 0);
720
721 synchronize_net();
722 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000723 tfile = rtnl_dereference(tun->tfiles[i]);
Eric Dumazet06e55ad2018-09-28 14:51:47 -0700724 tun_napi_del(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000725 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800726 tun_queue_purge(tfile);
Jason Wangb196d882018-05-11 10:49:25 +0800727 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wangc8d68e62012-10-31 19:46:00 +0000728 sock_put(&tfile->sk);
729 }
Jason Wang4008e972012-12-13 23:53:30 +0000730 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
731 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800732 tun_queue_purge(tfile);
Jason Wangb196d882018-05-11 10:49:25 +0800733 xdp_rxq_info_unreg(&tfile->xdp_rxq);
Jason Wang4008e972012-12-13 23:53:30 +0000734 sock_put(&tfile->sk);
735 }
736 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000737
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200738 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000739 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000740}
741
Petar Penkov94317092017-09-22 13:49:14 -0700742static int tun_attach(struct tun_struct *tun, struct file *file,
Yang Yingliang77f22f92019-09-10 18:56:57 +0800743 bool skip_filter, bool napi, bool napi_frags,
744 bool publish_tun)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000745{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000746 struct tun_file *tfile = file->private_data;
Jason Wang1576d982016-06-30 14:45:36 +0800747 struct net_device *dev = tun->dev;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000748 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000749
Paul Moore5dbbaf22013-01-14 07:12:19 +0000750 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
751 if (err < 0)
752 goto out;
753
Eric W. Biederman38231b72009-01-20 11:02:28 +0000754 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000755 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000756 goto out;
757
758 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200759 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000760 goto out;
761
762 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000763 if (!tfile->detached &&
764 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000765 goto out;
766
767 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000768
stephen hemminger92d4ea62013-12-05 20:42:58 -0800769 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400770 if (!skip_filter && (tun->filter_attached == true)) {
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +0200771 lock_sock(tfile->socket.sk);
772 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
773 release_sock(tfile->socket.sk);
Jason Wang54f968d2012-10-31 19:45:57 +0000774 if (!err)
775 goto out;
776 }
Jason Wang1576d982016-06-30 14:45:36 +0800777
778 if (!tfile->detached &&
Jason Wangb196d882018-05-11 10:49:25 +0800779 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len,
780 GFP_KERNEL, tun_ptr_free)) {
Jason Wang1576d982016-06-30 14:45:36 +0800781 err = -ENOMEM;
782 goto out;
783 }
784
Jason Wangc8d68e62012-10-31 19:46:00 +0000785 tfile->queue_index = tun->numqueues;
Jason Wangaddf8fc2016-05-19 13:36:51 +0800786 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100787
788 if (tfile->detached) {
789 /* Re-attach detached tfile, updating XDP queue_index */
790 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq));
791
792 if (tfile->xdp_rxq.queue_index != tfile->queue_index)
793 tfile->xdp_rxq.queue_index = tfile->queue_index;
794 } else {
795 /* Setup XDP RX-queue info, for new tfile getting attached */
796 err = xdp_rxq_info_reg(&tfile->xdp_rxq,
Björn Töpelb02e5a02020-11-30 19:52:01 +0100797 tun->dev, tfile->queue_index, 0);
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100798 if (err < 0)
799 goto out;
Jesper Dangaard Brouer8d5d8852018-04-17 16:46:12 +0200800 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq,
801 MEM_TYPE_PAGE_SHARED, NULL);
802 if (err < 0) {
803 xdp_rxq_info_unreg(&tfile->xdp_rxq);
804 goto out;
805 }
Jesper Dangaard Brouer8bf5c4e2018-01-03 11:25:59 +0100806 err = 0;
807 }
808
Petar Penkov94317092017-09-22 13:49:14 -0700809 if (tfile->detached) {
Jason Wang4008e972012-12-13 23:53:30 +0000810 tun_enable_queue(tfile);
Petar Penkov94317092017-09-22 13:49:14 -0700811 } else {
Jason Wang4008e972012-12-13 23:53:30 +0000812 sock_hold(&tfile->sk);
Eric Dumazetaf3fb242018-09-28 14:51:49 -0700813 tun_napi_init(tun, tfile, napi, napi_frags);
Petar Penkov94317092017-09-22 13:49:14 -0700814 }
Jason Wang4008e972012-12-13 23:53:30 +0000815
Jason Wange4a2a302018-09-12 11:16:59 +0800816 if (rtnl_dereference(tun->xdp_prog))
817 sock_set_flag(&tfile->sk, SOCK_XDP);
818
Jason Wangc8d68e62012-10-31 19:46:00 +0000819 /* device is allowed to go away first, so no need to hold extra
820 * refcnt.
821 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000822
Stanislav Fomichev0b7959b2019-01-07 13:38:38 -0800823 /* Publish tfile->tun and tun->tfiles only after we've fully
824 * initialized tfile; otherwise we risk using half-initialized
825 * object.
826 */
Yang Yingliang77f22f92019-09-10 18:56:57 +0800827 if (publish_tun)
828 rcu_assign_pointer(tfile->tun, tun);
Stanislav Fomichev0b7959b2019-01-07 13:38:38 -0800829 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
830 tun->numqueues++;
George Amanakis3a03cb82019-01-29 22:50:13 -0500831 tun_set_real_num_queues(tun);
Eric W. Biederman38231b72009-01-20 11:02:28 +0000832out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000833 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000834}
835
yuan linyu9484dc72017-09-23 22:36:52 +0800836static struct tun_struct *tun_get(struct tun_file *tfile)
Eric W. Biederman631ab462009-01-20 11:00:40 +0000837{
Jason Wang6e914fc2012-10-31 19:45:58 +0000838 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000839
Jason Wang6e914fc2012-10-31 19:45:58 +0000840 rcu_read_lock();
841 tun = rcu_dereference(tfile->tun);
842 if (tun)
843 dev_hold(tun->dev);
844 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000845
846 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000847}
848
Eric W. Biederman631ab462009-01-20 11:00:40 +0000849static void tun_put(struct tun_struct *tun)
850{
Jason Wang6e914fc2012-10-31 19:45:58 +0000851 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000852}
853
Joe Perches6b8a66e2011-03-02 07:18:10 +0000854/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700855static void addr_hash_set(u32 *mask, const u8 *addr)
856{
857 int n = ether_crc(ETH_ALEN, addr) >> 26;
858 mask[n >> 5] |= (1 << (n & 31));
859}
860
861static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
862{
863 int n = ether_crc(ETH_ALEN, addr) >> 26;
864 return mask[n >> 5] & (1 << (n & 31));
865}
866
867static int update_filter(struct tap_filter *filter, void __user *arg)
868{
869 struct { u8 u[ETH_ALEN]; } *addr;
870 struct tun_filter uf;
871 int err, alen, n, nexact;
872
873 if (copy_from_user(&uf, arg, sizeof(uf)))
874 return -EFAULT;
875
876 if (!uf.count) {
877 /* Disabled */
878 filter->count = 0;
879 return 0;
880 }
881
882 alen = ETH_ALEN * uf.count;
Markus Elfring28e81902016-08-20 08:54:15 +0200883 addr = memdup_user(arg + sizeof(uf), alen);
884 if (IS_ERR(addr))
885 return PTR_ERR(addr);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700886
887 /* The filter is updated without holding any locks. Which is
888 * perfectly safe. We disable it first and in the worst
889 * case we'll accept a few undesired packets. */
890 filter->count = 0;
891 wmb();
892
893 /* Use first set of addresses as an exact filter */
894 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
895 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
896
897 nexact = n;
898
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800899 /* Remaining multicast addresses are hashed,
900 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700901 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800902 for (; n < uf.count; n++) {
903 if (!is_multicast_ether_addr(addr[n].u)) {
904 err = 0; /* no filter */
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200905 goto free_addr;
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800906 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700907 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800908 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700909
910 /* For ALLMULTI just set the mask to all ones.
911 * This overrides the mask populated above. */
912 if ((uf.flags & TUN_FLT_ALLMULTI))
913 memset(filter->mask, ~0, sizeof(filter->mask));
914
915 /* Now enable the filter */
916 wmb();
917 filter->count = nexact;
918
919 /* Return the number of exact filters */
920 err = nexact;
Markus Elfring3b8d2a62016-08-20 09:00:34 +0200921free_addr:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700922 kfree(addr);
923 return err;
924}
925
926/* Returns: 0 - drop, !=0 - accept */
927static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
928{
929 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
930 * at this point. */
931 struct ethhdr *eh = (struct ethhdr *) skb->data;
932 int i;
933
934 /* Exact match */
935 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000936 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700937 return 1;
938
939 /* Inexact match (multicast only) */
940 if (is_multicast_ether_addr(eh->h_dest))
941 return addr_hash_test(filter->mask, eh->h_dest);
942
943 return 0;
944}
945
946/*
947 * Checks whether the packet is accepted or not.
948 * Returns: 0 - drop, !=0 - accept
949 */
950static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
951{
952 if (!filter->count)
953 return 1;
954
955 return run_filter(filter, skb);
956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/* Network device part of the driver */
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960static const struct ethtool_ops tun_ethtool_ops;
961
George Kennedy158b5152021-12-16 13:25:32 -0500962static int tun_net_init(struct net_device *dev)
963{
964 struct tun_struct *tun = netdev_priv(dev);
965 struct ifreq *ifr = tun->ifr;
966 int err;
967
968 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
969 if (!dev->tstats)
970 return -ENOMEM;
971
972 spin_lock_init(&tun->lock);
973
974 err = security_tun_dev_alloc_security(&tun->security);
975 if (err < 0) {
976 free_percpu(dev->tstats);
977 return err;
978 }
979
980 tun_flow_init(tun);
981
982 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
983 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
984 NETIF_F_HW_VLAN_STAG_TX;
985 dev->features = dev->hw_features | NETIF_F_LLTX;
986 dev->vlan_features = dev->features &
987 ~(NETIF_F_HW_VLAN_CTAG_TX |
988 NETIF_F_HW_VLAN_STAG_TX);
989
990 tun->flags = (tun->flags & ~TUN_FEATURES) |
991 (ifr->ifr_flags & TUN_FEATURES);
992
993 INIT_LIST_HEAD(&tun->disabled);
994 err = tun_attach(tun, tun->file, false, ifr->ifr_flags & IFF_NAPI,
995 ifr->ifr_flags & IFF_NAPI_FRAGS, false);
996 if (err < 0) {
997 tun_flow_uninit(tun);
998 security_tun_dev_free_security(tun->security);
999 free_percpu(dev->tstats);
1000 return err;
1001 }
1002 return 0;
1003}
1004
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001005/* Net device detach from fd. */
1006static void tun_net_uninit(struct net_device *dev)
1007{
Jason Wangc8d68e62012-10-31 19:46:00 +00001008 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/* Net device open. */
1012static int tun_net_open(struct net_device *dev)
1013{
Jason Wangc8d68e62012-10-31 19:46:00 +00001014 netif_tx_start_all_queues(dev);
Hannes Frederic Sowab20e2d52017-03-13 00:00:26 +01001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017}
1018
1019/* Net device close. */
1020static int tun_net_close(struct net_device *dev)
1021{
Jason Wangc8d68e62012-10-31 19:46:00 +00001022 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
1024}
1025
1026/* Net device start xmit */
Jason Wang96f84062017-12-04 17:31:23 +08001027static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Jason Wang3df97ba2016-04-25 23:13:42 -04001029#ifdef CONFIG_RPS
Eric Dumazetdc053602019-03-22 08:56:38 -07001030 if (tun->numqueues == 1 && static_branch_unlikely(&rps_needed)) {
Tom Herbert9bc88932013-12-22 18:54:32 +08001031 /* Select queue was not called for the skbuff, so we extract the
1032 * RPS hash and save it into the flow_table here.
1033 */
Wang Li4b035272018-10-09 10:32:04 +08001034 struct tun_flow_entry *e;
Tom Herbert9bc88932013-12-22 18:54:32 +08001035 __u32 rxhash;
1036
Jason Wangfeec0842017-06-06 14:09:49 +08001037 rxhash = __skb_get_hash_symmetric(skb);
Wang Li4b035272018-10-09 10:32:04 +08001038 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], rxhash);
1039 if (e)
1040 tun_flow_save_rps_rxhash(e, rxhash);
Tom Herbert9bc88932013-12-22 18:54:32 +08001041 }
Jason Wang3df97ba2016-04-25 23:13:42 -04001042#endif
Jason Wang96f84062017-12-04 17:31:23 +08001043}
1044
Jason Wangaff3d702018-01-16 16:31:02 +08001045static unsigned int run_ebpf_filter(struct tun_struct *tun,
1046 struct sk_buff *skb,
1047 int len)
1048{
1049 struct tun_prog *prog = rcu_dereference(tun->filter_prog);
1050
1051 if (prog)
1052 len = bpf_prog_run_clear_cb(prog->prog, skb);
1053
1054 return len;
1055}
1056
Jason Wang96f84062017-12-04 17:31:23 +08001057/* Net device start xmit */
1058static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1059{
1060 struct tun_struct *tun = netdev_priv(dev);
1061 int txq = skb->queue_mapping;
Nicolas Dichtela31d27f2021-11-12 08:56:03 +01001062 struct netdev_queue *queue;
Jason Wang96f84062017-12-04 17:31:23 +08001063 struct tun_file *tfile;
Jason Wangaff3d702018-01-16 16:31:02 +08001064 int len = skb->len;
Jason Wang96f84062017-12-04 17:31:23 +08001065
1066 rcu_read_lock();
1067 tfile = rcu_dereference(tun->tfiles[txq]);
Jason Wang96f84062017-12-04 17:31:23 +08001068
1069 /* Drop packet if interface is not attached */
Jason Wang9871a9e2019-05-08 23:20:18 -04001070 if (!tfile)
Jason Wang96f84062017-12-04 17:31:23 +08001071 goto drop;
1072
1073 if (!rcu_dereference(tun->steering_prog))
1074 tun_automq_xmit(tun, skb);
Tom Herbert9bc88932013-12-22 18:54:32 +08001075
Michal Kubecek34241702020-03-04 17:24:14 +01001076 netif_info(tun, tx_queued, tun->dev, "%s %d\n", __func__, skb->len);
Jason Wang6e914fc2012-10-31 19:45:58 +00001077
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001078 /* Drop if the filter does not like it.
1079 * This is a noop if the filter is disabled.
1080 * Filter can be enabled only for the TAP devices. */
1081 if (!check_filter(&tun->txflt, skb))
1082 goto drop;
1083
Jason Wang54f968d2012-10-31 19:45:57 +00001084 if (tfile->socket.sk->sk_filter &&
1085 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001086 goto drop;
1087
Jason Wangaff3d702018-01-16 16:31:02 +08001088 len = run_ebpf_filter(tun, skb, len);
Bjørn Mork81c89502018-04-17 22:46:38 +02001089 if (len == 0 || pskb_trim(skb, len))
Jason Wangaff3d702018-01-16 16:31:02 +08001090 goto drop;
1091
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001092 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
Jason Wang7bf66302013-09-05 17:54:00 +08001093 goto drop;
1094
Soheil Hassas Yeganeh7b996242016-08-23 18:22:33 -04001095 skb_tx_timestamp(skb);
Richard Cochraneda29772013-07-19 19:40:10 +02001096
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +00001097 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +08001098 * for indefinite time.
1099 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +00001100 skb_orphan(skb);
1101
Florian Westphal895b5c92019-09-29 20:54:03 +02001102 nf_reset_ct(skb);
Eric Dumazetf8af75f2013-03-06 11:02:37 +00001103
Jason Wang5990a302018-01-04 11:14:27 +08001104 if (ptr_ring_produce(&tfile->tx_ring, skb))
Jason Wang1576d982016-06-30 14:45:36 +08001105 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Nicolas Dichtela31d27f2021-11-12 08:56:03 +01001107 /* NETIF_F_LLTX requires to do our own update of trans_start */
1108 queue = netdev_get_tx_queue(dev, txq);
1109 queue->trans_start = jiffies;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +00001112 if (tfile->flags & TUN_FASYNC)
1113 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -07001114 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +00001115
1116 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +00001117 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119drop:
Heiner Kallweit497a5752020-11-07 21:50:56 +01001120 atomic_long_inc(&dev->tx_dropped);
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +00001121 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +00001123 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +08001124 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001127static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001129 /*
1130 * This callback is supposed to deal with mc filter in
1131 * _rx_ path and has nothing to do with the _tx_ path.
1132 * In rx path we always accept everything userspace gives us.
1133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001136static netdev_features_t tun_net_fix_features(struct net_device *dev,
1137 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +00001138{
1139 struct tun_struct *tun = netdev_priv(dev);
1140
1141 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1142}
Paolo Abenieaea34b2016-02-26 10:45:40 +01001143
1144static void tun_set_headroom(struct net_device *dev, int new_hr)
1145{
1146 struct tun_struct *tun = netdev_priv(dev);
1147
1148 if (new_hr < NET_SKB_PAD)
1149 new_hr = NET_SKB_PAD;
1150
1151 tun->align = new_hr;
1152}
1153
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001154static void
Paolo Abeni608b9972016-04-13 10:52:20 +02001155tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1156{
Paolo Abeni608b9972016-04-13 10:52:20 +02001157 struct tun_struct *tun = netdev_priv(dev);
Paolo Abeni608b9972016-04-13 10:52:20 +02001158
Heiner Kallweit497a5752020-11-07 21:50:56 +01001159 dev_get_tstats64(dev, stats);
Paolo Abeni608b9972016-04-13 10:52:20 +02001160
Heiner Kallweit497a5752020-11-07 21:50:56 +01001161 stats->rx_frame_errors +=
1162 (unsigned long)atomic_long_read(&tun->rx_frame_errors);
Paolo Abeni608b9972016-04-13 10:52:20 +02001163}
1164
Jason Wang761876c2017-08-11 19:41:18 +08001165static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1166 struct netlink_ext_ack *extack)
1167{
1168 struct tun_struct *tun = netdev_priv(dev);
Jason Wange4a2a302018-09-12 11:16:59 +08001169 struct tun_file *tfile;
Jason Wang761876c2017-08-11 19:41:18 +08001170 struct bpf_prog *old_prog;
Jason Wange4a2a302018-09-12 11:16:59 +08001171 int i;
Jason Wang761876c2017-08-11 19:41:18 +08001172
1173 old_prog = rtnl_dereference(tun->xdp_prog);
1174 rcu_assign_pointer(tun->xdp_prog, prog);
1175 if (old_prog)
1176 bpf_prog_put(old_prog);
1177
Jason Wange4a2a302018-09-12 11:16:59 +08001178 for (i = 0; i < tun->numqueues; i++) {
1179 tfile = rtnl_dereference(tun->tfiles[i]);
1180 if (prog)
1181 sock_set_flag(&tfile->sk, SOCK_XDP);
1182 else
1183 sock_reset_flag(&tfile->sk, SOCK_XDP);
1184 }
1185 list_for_each_entry(tfile, &tun->disabled, next) {
1186 if (prog)
1187 sock_set_flag(&tfile->sk, SOCK_XDP);
1188 else
1189 sock_reset_flag(&tfile->sk, SOCK_XDP);
1190 }
1191
Jason Wang761876c2017-08-11 19:41:18 +08001192 return 0;
1193}
1194
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001195static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
Jason Wang761876c2017-08-11 19:41:18 +08001196{
1197 switch (xdp->command) {
1198 case XDP_SETUP_PROG:
1199 return tun_xdp_set(dev, xdp->prog, xdp->extack);
Jason Wang761876c2017-08-11 19:41:18 +08001200 default:
1201 return -EINVAL;
1202 }
1203}
1204
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001205static int tun_net_change_carrier(struct net_device *dev, bool new_carrier)
1206{
1207 if (new_carrier) {
1208 struct tun_struct *tun = netdev_priv(dev);
1209
1210 if (!tun->numqueues)
1211 return -EPERM;
1212
1213 netif_carrier_on(dev);
1214 } else {
1215 netif_carrier_off(dev);
1216 }
1217 return 0;
1218}
1219
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001220static const struct net_device_ops tun_netdev_ops = {
George Kennedy158b5152021-12-16 13:25:32 -05001221 .ndo_init = tun_net_init,
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001222 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001223 .ndo_open = tun_net_open,
1224 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001225 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001226 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +00001227 .ndo_select_queue = tun_select_queue,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001228 .ndo_set_rx_headroom = tun_set_headroom,
Paolo Abeni608b9972016-04-13 10:52:20 +02001229 .ndo_get_stats64 = tun_net_get_stats64,
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001230 .ndo_change_carrier = tun_net_change_carrier,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001231};
1232
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001233static void __tun_xdp_flush_tfile(struct tun_file *tfile)
1234{
1235 /* Notify and wake up reader process */
1236 if (tfile->flags & TUN_FASYNC)
1237 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
1238 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
1239}
1240
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02001241static int tun_xdp_xmit(struct net_device *dev, int n,
1242 struct xdp_frame **frames, u32 flags)
Jason Wangfc72d1d2018-01-04 11:14:28 +08001243{
1244 struct tun_struct *tun = netdev_priv(dev);
Jason Wangfc72d1d2018-01-04 11:14:28 +08001245 struct tun_file *tfile;
1246 u32 numqueues;
Lorenzo Bianconifdc13972021-03-08 12:06:58 +01001247 int nxmit = 0;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001248 int i;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001249
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001250 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02001251 return -EINVAL;
1252
Jason Wangfc72d1d2018-01-04 11:14:28 +08001253 rcu_read_lock();
1254
Jason Wang9871a9e2019-05-08 23:20:18 -04001255resample:
Jason Wangfc72d1d2018-01-04 11:14:28 +08001256 numqueues = READ_ONCE(tun->numqueues);
1257 if (!numqueues) {
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001258 rcu_read_unlock();
1259 return -ENXIO; /* Caller will free/return all frames */
Jason Wangfc72d1d2018-01-04 11:14:28 +08001260 }
1261
1262 tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
1263 numqueues]);
Jason Wang9871a9e2019-05-08 23:20:18 -04001264 if (unlikely(!tfile))
1265 goto resample;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001266
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001267 spin_lock(&tfile->tx_ring.producer_lock);
1268 for (i = 0; i < n; i++) {
1269 struct xdp_frame *xdp = frames[i];
1270 /* Encode the XDP flag into lowest bit for consumer to differ
1271 * XDP buffer from sk_buff.
1272 */
1273 void *frame = tun_xdp_to_ptr(xdp);
1274
1275 if (__ptr_ring_produce(&tfile->tx_ring, frame)) {
Heiner Kallweit497a5752020-11-07 21:50:56 +01001276 atomic_long_inc(&dev->tx_dropped);
Lorenzo Bianconifdc13972021-03-08 12:06:58 +01001277 break;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001278 }
Lorenzo Bianconifdc13972021-03-08 12:06:58 +01001279 nxmit++;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02001280 }
1281 spin_unlock(&tfile->tx_ring.producer_lock);
1282
Jesper Dangaard Brouer0c9d9172018-05-31 11:00:03 +02001283 if (flags & XDP_XMIT_FLUSH)
1284 __tun_xdp_flush_tfile(tfile);
1285
Jason Wangfc72d1d2018-01-04 11:14:28 +08001286 rcu_read_unlock();
Lorenzo Bianconifdc13972021-03-08 12:06:58 +01001287 return nxmit;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001288}
1289
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02001290static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
1291{
Lorenzo Bianconi1b698fa2020-05-28 22:47:29 +02001292 struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
Lorenzo Bianconifdc13972021-03-08 12:06:58 +01001293 int nxmit;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02001294
1295 if (unlikely(!frame))
1296 return -EOVERFLOW;
1297
Lorenzo Bianconifdc13972021-03-08 12:06:58 +01001298 nxmit = tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH);
1299 if (!nxmit)
1300 xdp_return_frame_rx_napi(frame);
1301 return nxmit;
Jason Wangfc72d1d2018-01-04 11:14:28 +08001302}
1303
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001304static const struct net_device_ops tap_netdev_ops = {
George Kennedy158b5152021-12-16 13:25:32 -05001305 .ndo_init = tun_net_init,
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001306 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001307 .ndo_open = tun_net_open,
1308 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08001309 .ndo_start_xmit = tun_net_xmit,
Michał Mirosław88255372011-04-19 06:13:10 +00001310 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001311 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001312 .ndo_set_mac_address = eth_mac_addr,
1313 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +00001314 .ndo_select_queue = tun_select_queue,
Toshiaki Makita5e527962015-07-31 15:03:27 +09001315 .ndo_features_check = passthru_features_check,
Paolo Abenieaea34b2016-02-26 10:45:40 +01001316 .ndo_set_rx_headroom = tun_set_headroom,
Heiner Kallweit497a5752020-11-07 21:50:56 +01001317 .ndo_get_stats64 = dev_get_tstats64,
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001318 .ndo_bpf = tun_xdp,
Jason Wangfc72d1d2018-01-04 11:14:28 +08001319 .ndo_xdp_xmit = tun_xdp_xmit,
Nicolas Dichtel26d31922018-11-28 19:12:56 +01001320 .ndo_change_carrier = tun_net_change_carrier,
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001321};
1322
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001323static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +00001324{
1325 int i;
1326
Jason Wang96442e422012-10-31 19:46:02 +00001327 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1328 INIT_HLIST_HEAD(&tun->flows[i]);
1329
1330 tun->ageing_time = TUN_FLOW_EXPIRE;
Kees Cooke99e88a2017-10-16 14:43:17 -07001331 timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0);
1332 mod_timer(&tun->flow_gc_timer,
1333 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +00001334}
1335
1336static void tun_flow_uninit(struct tun_struct *tun)
1337{
1338 del_timer_sync(&tun->flow_gc_timer);
1339 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001340}
1341
Jarod Wilson91572082016-10-20 13:55:20 -04001342#define MIN_MTU 68
1343#define MAX_MTU 65535
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345/* Initialize net device. */
George Kennedy158b5152021-12-16 13:25:32 -05001346static void tun_net_initialize(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001351 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001352 dev->netdev_ops = &tun_netdev_ops;
Jason A. Donenfeldb9815eb2020-06-29 19:06:22 -06001353 dev->header_ops = &ip_tunnel_header_ops;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /* Point-to-Point TUN Device */
1356 dev->hard_header_len = 0;
1357 dev->addr_len = 0;
1358 dev->mtu = 1500;
1359
1360 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001361 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
1364
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001365 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -08001366 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001368 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +00001369 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +00001370 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001372 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 break;
1375 }
Jarod Wilson91572082016-10-20 13:55:20 -04001376
1377 dev->min_mtu = MIN_MTU;
1378 dev->max_mtu = MAX_MTU - dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Jason Wang2f3ab622018-05-22 14:21:04 +08001381static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile)
1382{
1383 struct sock *sk = tfile->socket.sk;
1384
1385 return (tun->dev->flags & IFF_UP) && sock_writeable(sk);
1386}
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/* Character device part */
1389
1390/* Poll */
Al Viroafc9a422017-07-03 06:39:46 -04001391static __poll_t tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001392{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +00001393 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001394 struct tun_struct *tun = tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001395 struct sock *sk;
Al Viroafc9a422017-07-03 06:39:46 -04001396 __poll_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 if (!tun)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001399 return EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Jason Wang54f968d2012-10-31 19:45:57 +00001401 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +00001402
Xi Wang9e641bd2014-05-16 15:11:48 -07001403 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001404
Jason Wang5990a302018-01-04 11:14:27 +08001405 if (!ptr_ring_empty(&tfile->tx_ring))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001406 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Jason Wang2f3ab622018-05-22 14:21:04 +08001408 /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to
1409 * guarantee EPOLLOUT to be raised by either here or
1410 * tun_sock_write_space(). Then process could get notification
1411 * after it writes to a down device and meets -EIO.
1412 */
1413 if (tun_sock_writeable(tun, tfile) ||
1414 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1415 tun_sock_writeable(tun, tfile)))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001416 mask |= EPOLLOUT | EPOLLWRNORM;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001417
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001418 if (tun->dev->reg_state != NETREG_REGISTERED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001419 mask = EPOLLERR;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001420
Eric W. Biederman631ab462009-01-20 11:00:40 +00001421 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return mask;
1423}
1424
Petar Penkov90e33d42017-09-22 13:49:15 -07001425static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
1426 size_t len,
1427 const struct iov_iter *it)
1428{
1429 struct sk_buff *skb;
1430 size_t linear;
1431 int err;
1432 int i;
1433
1434 if (it->nr_segs > MAX_SKB_FRAGS + 1)
Yunjian Wang950271d2020-12-25 10:52:16 +08001435 return ERR_PTR(-EMSGSIZE);
Petar Penkov90e33d42017-09-22 13:49:15 -07001436
1437 local_bh_disable();
1438 skb = napi_get_frags(&tfile->napi);
1439 local_bh_enable();
1440 if (!skb)
1441 return ERR_PTR(-ENOMEM);
1442
1443 linear = iov_iter_single_seg_count(it);
1444 err = __skb_grow(skb, linear);
1445 if (err)
1446 goto free;
1447
1448 skb->len = len;
1449 skb->data_len = len - linear;
1450 skb->truesize += skb->data_len;
1451
1452 for (i = 1; i < it->nr_segs; i++) {
1453 size_t fragsz = it->iov[i].iov_len;
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001454 struct page *page;
1455 void *frag;
Petar Penkov90e33d42017-09-22 13:49:15 -07001456
1457 if (fragsz == 0 || fragsz > PAGE_SIZE) {
1458 err = -EINVAL;
1459 goto free;
1460 }
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001461 frag = netdev_alloc_frag(fragsz);
1462 if (!frag) {
Petar Penkov90e33d42017-09-22 13:49:15 -07001463 err = -ENOMEM;
1464 goto free;
1465 }
Eric Dumazetaa6daac2018-11-18 07:37:33 -08001466 page = virt_to_head_page(frag);
1467 skb_fill_page_desc(skb, i - 1, page,
1468 frag - page_address(page), fragsz);
Petar Penkov90e33d42017-09-22 13:49:15 -07001469 }
1470
1471 return skb;
1472free:
1473 /* frees skb and all frags allocated with napi_alloc_frag() */
1474 napi_free_frags(&tfile->napi);
1475 return ERR_PTR(err);
1476}
1477
Rusty Russellf42157c2008-08-15 15:15:10 -07001478/* prepad is the amount to reserve at front. len is length after that.
1479 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001480static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001481 size_t prepad, size_t len,
1482 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001483{
Jason Wang54f968d2012-10-31 19:45:57 +00001484 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001485 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001486 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001487
1488 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001489 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001490 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001491
Herbert Xu33dccbb2009-02-05 21:25:32 -08001492 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001493 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001494 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001495 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001496
1497 skb_reserve(skb, prepad);
1498 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001499 skb->data_len = len - linear;
1500 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001501
1502 return skb;
1503}
1504
Jason Wang5503fce2017-01-18 15:02:03 +08001505static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1506 struct sk_buff *skb, int more)
1507{
1508 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1509 struct sk_buff_head process_queue;
1510 u32 rx_batched = tun->rx_batched;
1511 bool rcv = false;
1512
1513 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1514 local_bh_disable();
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001515 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001516 netif_receive_skb(skb);
1517 local_bh_enable();
1518 return;
1519 }
1520
1521 spin_lock(&queue->lock);
1522 if (!more || skb_queue_len(queue) == rx_batched) {
1523 __skb_queue_head_init(&process_queue);
1524 skb_queue_splice_tail_init(queue, &process_queue);
1525 rcv = true;
1526 } else {
1527 __skb_queue_tail(queue, skb);
1528 }
1529 spin_unlock(&queue->lock);
1530
1531 if (rcv) {
1532 struct sk_buff *nskb;
1533
1534 local_bh_disable();
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001535 while ((nskb = __skb_dequeue(&process_queue))) {
1536 skb_record_rx_queue(nskb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001537 netif_receive_skb(nskb);
Matthew Cover8ebebcb2018-11-18 00:46:00 -07001538 }
1539 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang5503fce2017-01-18 15:02:03 +08001540 netif_receive_skb(skb);
1541 local_bh_enable();
1542 }
1543}
1544
Jason Wang66ccbc92017-08-11 19:41:16 +08001545static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1546 int len, int noblock, bool zerocopy)
1547{
1548 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1549 return false;
1550
1551 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1552 return false;
1553
1554 if (!noblock)
1555 return false;
1556
1557 if (zerocopy)
1558 return false;
1559
1560 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1561 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1562 return false;
1563
1564 return true;
1565}
1566
Alexis Bauvin4b663362019-07-23 16:23:01 +02001567static struct sk_buff *__tun_build_skb(struct tun_file *tfile,
1568 struct page_frag *alloc_frag, char *buf,
Jason Wang8ae1aff2018-09-12 11:17:04 +08001569 int buflen, int len, int pad)
Jason Wangac1f1f62018-09-12 11:17:03 +08001570{
1571 struct sk_buff *skb = build_skb(buf, buflen);
1572
1573 if (!skb)
1574 return ERR_PTR(-ENOMEM);
1575
Jason Wang8ae1aff2018-09-12 11:17:04 +08001576 skb_reserve(skb, pad);
Jason Wangac1f1f62018-09-12 11:17:03 +08001577 skb_put(skb, len);
Alexis Bauvin4b663362019-07-23 16:23:01 +02001578 skb_set_owner_w(skb, tfile->socket.sk);
Jason Wangac1f1f62018-09-12 11:17:03 +08001579
1580 get_page(alloc_frag->page);
1581 alloc_frag->offset += buflen;
1582
1583 return skb;
1584}
1585
Jason Wang8ae1aff2018-09-12 11:17:04 +08001586static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
1587 struct xdp_buff *xdp, u32 act)
1588{
1589 int err;
1590
1591 switch (act) {
1592 case XDP_REDIRECT:
1593 err = xdp_do_redirect(tun->dev, xdp, xdp_prog);
Jason Wang8ae1aff2018-09-12 11:17:04 +08001594 if (err)
1595 return err;
1596 break;
1597 case XDP_TX:
1598 err = tun_xdp_tx(tun->dev, xdp);
1599 if (err < 0)
1600 return err;
1601 break;
1602 case XDP_PASS:
1603 break;
1604 default:
Paolo Abenic8064e52021-11-30 11:08:07 +01001605 bpf_warn_invalid_xdp_action(tun->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001606 fallthrough;
Jason Wang8ae1aff2018-09-12 11:17:04 +08001607 case XDP_ABORTED:
1608 trace_xdp_exception(tun->dev, xdp_prog, act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001609 fallthrough;
Jason Wang8ae1aff2018-09-12 11:17:04 +08001610 case XDP_DROP:
Heiner Kallweit497a5752020-11-07 21:50:56 +01001611 atomic_long_inc(&tun->dev->rx_dropped);
Jason Wang8ae1aff2018-09-12 11:17:04 +08001612 break;
1613 }
1614
1615 return act;
1616}
1617
Jason Wang761876c2017-08-11 19:41:18 +08001618static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1619 struct tun_file *tfile,
Jason Wang66ccbc92017-08-11 19:41:16 +08001620 struct iov_iter *from,
Jason Wang761876c2017-08-11 19:41:18 +08001621 struct virtio_net_hdr *hdr,
Jason Wang1cfe6e92017-09-04 11:36:09 +08001622 int len, int *skb_xdp)
Jason Wang66ccbc92017-08-11 19:41:16 +08001623{
Eric Dumazet0bbd7da2017-08-16 22:14:33 +08001624 struct page_frag *alloc_frag = &current->task_frag;
Jason Wang761876c2017-08-11 19:41:18 +08001625 struct bpf_prog *xdp_prog;
Jason Wang7df13212017-09-04 11:36:08 +08001626 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Jason Wang66ccbc92017-08-11 19:41:16 +08001627 char *buf;
1628 size_t copied;
Jason Wang8ae1aff2018-09-12 11:17:04 +08001629 int pad = TUN_RX_PAD;
1630 int err = 0;
Jason Wang7df13212017-09-04 11:36:08 +08001631
1632 rcu_read_lock();
1633 xdp_prog = rcu_dereference(tun->xdp_prog);
1634 if (xdp_prog)
Jason Wang4f23aff2018-09-12 11:17:00 +08001635 pad += XDP_PACKET_HEADROOM;
Jason Wang7df13212017-09-04 11:36:08 +08001636 buflen += SKB_DATA_ALIGN(len + pad);
1637 rcu_read_unlock();
Jason Wang66ccbc92017-08-11 19:41:16 +08001638
Jason Wang63b9ab62017-10-27 11:05:44 +08001639 alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
Jason Wang66ccbc92017-08-11 19:41:16 +08001640 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1641 return ERR_PTR(-ENOMEM);
1642
1643 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1644 copied = copy_page_from_iter(alloc_frag->page,
Jason Wang7df13212017-09-04 11:36:08 +08001645 alloc_frag->offset + pad,
Jason Wang66ccbc92017-08-11 19:41:16 +08001646 len, from);
1647 if (copied != len)
1648 return ERR_PTR(-EFAULT);
1649
Jason Wang7df13212017-09-04 11:36:08 +08001650 /* There's a small window that XDP may be set after the check
1651 * of xdp_prog above, this should be rare and for simplicity
1652 * we do XDP on skb in case the headroom is not enough.
1653 */
Jason Wangac1f1f62018-09-12 11:17:03 +08001654 if (hdr->gso_type || !xdp_prog) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001655 *skb_xdp = 1;
Alexis Bauvin4b663362019-07-23 16:23:01 +02001656 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len,
1657 pad);
Jason Wangac1f1f62018-09-12 11:17:03 +08001658 }
1659
1660 *skb_xdp = 0;
Jason Wang66ccbc92017-08-11 19:41:16 +08001661
Toshiaki Makita6547e382018-05-28 19:37:49 +09001662 local_bh_disable();
Jason Wang761876c2017-08-11 19:41:18 +08001663 rcu_read_lock();
1664 xdp_prog = rcu_dereference(tun->xdp_prog);
Jason Wang8ae1aff2018-09-12 11:17:04 +08001665 if (xdp_prog) {
Jason Wang761876c2017-08-11 19:41:18 +08001666 struct xdp_buff xdp;
Jason Wang761876c2017-08-11 19:41:18 +08001667 u32 act;
1668
Lorenzo Bianconi43b51692020-12-22 22:09:28 +01001669 xdp_init_buff(&xdp, buflen, &tfile->xdp_rxq);
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +01001670 xdp_prepare_buff(&xdp, buf, pad, len, false);
Jason Wang761876c2017-08-11 19:41:18 +08001671
Jason Wang8ae1aff2018-09-12 11:17:04 +08001672 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1673 if (act == XDP_REDIRECT || act == XDP_TX) {
Jason Wang761876c2017-08-11 19:41:18 +08001674 get_page(alloc_frag->page);
1675 alloc_frag->offset += buflen;
Jason Wang761876c2017-08-11 19:41:18 +08001676 }
Jason Wang8ae1aff2018-09-12 11:17:04 +08001677 err = tun_xdp_act(tun, xdp_prog, &xdp, act);
Will Deaconbee34892020-04-03 16:13:21 +01001678 if (err < 0) {
1679 if (act == XDP_REDIRECT || act == XDP_TX)
1680 put_page(alloc_frag->page);
1681 goto out;
1682 }
1683
Jason Wang1a097912018-09-12 11:17:05 +08001684 if (err == XDP_REDIRECT)
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01001685 xdp_do_flush();
Jason Wang8ae1aff2018-09-12 11:17:04 +08001686 if (err != XDP_PASS)
1687 goto out;
1688
1689 pad = xdp.data - xdp.data_hard_start;
1690 len = xdp.data_end - xdp.data;
Jason Wang761876c2017-08-11 19:41:18 +08001691 }
Jason Wang291aeb22018-09-12 11:17:01 +08001692 rcu_read_unlock();
1693 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001694
Alexis Bauvin4b663362019-07-23 16:23:01 +02001695 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad);
Jason Wang761876c2017-08-11 19:41:18 +08001696
Jason Wangf7053b62018-09-12 11:17:02 +08001697out:
Jason Wang761876c2017-08-11 19:41:18 +08001698 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001699 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001700 return NULL;
Jason Wang66ccbc92017-08-11 19:41:16 +08001701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001704static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001705 void *msg_control, struct iov_iter *from,
Jason Wang5503fce2017-01-18 15:02:03 +08001706 int noblock, bool more)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Harvey Harrison09640e632009-02-01 00:45:17 -08001708 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001710 size_t total_len = iov_iter_count(from);
Paolo Abenieaea34b2016-02-26 10:45:40 +01001711 size_t len = total_len, align = tun->align, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001712 struct virtio_net_hdr gso = { 0 };
Jason Wang96f8d9e2013-11-13 14:00:39 +08001713 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001714 int copylen;
1715 bool zerocopy = false;
1716 int err;
Jason Wang96f84062017-12-04 17:31:23 +08001717 u32 rxhash = 0;
Jason Wang1cfe6e92017-09-04 11:36:09 +08001718 int skb_xdp = 1;
Eric Dumazetaf3fb242018-09-28 14:51:49 -07001719 bool frags = tun_napi_frags_enabled(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001721 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001722 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001724 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Al Virocbbd26b2016-11-01 22:09:04 -04001726 if (!copy_from_iter_full(&pi, sizeof(pi), from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return -EFAULT;
1728 }
1729
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001730 if (tun->flags & IFF_VNET_HDR) {
Willem de Bruijne1edab82017-02-03 18:20:48 -05001731 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1732
1733 if (len < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001734 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001735 len -= vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001736
Al Virocbbd26b2016-11-01 22:09:04 -04001737 if (!copy_from_iter_full(&gso, sizeof(gso), from))
Rusty Russellf43798c2008-07-03 03:48:02 -07001738 return -EFAULT;
1739
Herbert Xu49091222009-06-08 00:20:01 -07001740 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001741 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1742 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 -07001743
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001744 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001745 return -EINVAL;
Willem de Bruijne1edab82017-02-03 18:20:48 -05001746 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001747 }
1748
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001749 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001750 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001751 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001752 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001753 return -EINVAL;
1754 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001755
Jason Wang96f8d9e2013-11-13 14:00:39 +08001756 good_linear = SKB_MAX_HEAD(align);
1757
Jason Wang88529172013-07-18 10:55:15 +08001758 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001759 struct iov_iter i = *from;
1760
Jason Wang88529172013-07-18 10:55:15 +08001761 /* There are 256 bytes to be copied in skb, so there is
1762 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001763 * The rest of the buffer is mapped from userspace.
1764 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001765 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001766 if (copylen > good_linear)
1767 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001768 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001769 iov_iter_advance(&i, copylen);
1770 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001771 zerocopy = true;
1772 }
1773
Petar Penkov90e33d42017-09-22 13:49:15 -07001774 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
Jason Wang1cfe6e92017-09-04 11:36:09 +08001775 /* For the packet that is not easy to be processed
1776 * (e.g gso or jumbo packet), we will do it at after
1777 * skb was created with generic XDP routine.
1778 */
1779 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
Jason Wang66ccbc92017-08-11 19:41:16 +08001780 if (IS_ERR(skb)) {
Heiner Kallweit497a5752020-11-07 21:50:56 +01001781 atomic_long_inc(&tun->dev->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001782 return PTR_ERR(skb);
1783 }
Jason Wang761876c2017-08-11 19:41:18 +08001784 if (!skb)
1785 return total_len;
Jason Wang66ccbc92017-08-11 19:41:16 +08001786 } else {
1787 if (!zerocopy) {
1788 copylen = len;
1789 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1790 linear = good_linear;
1791 else
1792 linear = tun16_to_cpu(tun, gso.hdr_len);
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Petar Penkov90e33d42017-09-22 13:49:15 -07001795 if (frags) {
1796 mutex_lock(&tfile->napi_mutex);
1797 skb = tun_napi_alloc_frags(tfile, copylen, from);
1798 /* tun_napi_alloc_frags() enforces a layout for the skb.
1799 * If zerocopy is enabled, then this layout will be
1800 * overwritten by zerocopy_sg_from_iter().
1801 */
1802 zerocopy = false;
1803 } else {
1804 skb = tun_alloc_skb(tfile, align, copylen, linear,
1805 noblock);
1806 }
1807
Jason Wang66ccbc92017-08-11 19:41:16 +08001808 if (IS_ERR(skb)) {
1809 if (PTR_ERR(skb) != -EAGAIN)
Heiner Kallweit497a5752020-11-07 21:50:56 +01001810 atomic_long_inc(&tun->dev->rx_dropped);
Petar Penkov90e33d42017-09-22 13:49:15 -07001811 if (frags)
1812 mutex_unlock(&tfile->napi_mutex);
Jason Wang66ccbc92017-08-11 19:41:16 +08001813 return PTR_ERR(skb);
1814 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001815
Jason Wang66ccbc92017-08-11 19:41:16 +08001816 if (zerocopy)
1817 err = zerocopy_sg_from_iter(skb, from);
1818 else
1819 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1820
1821 if (err) {
Eric Dumazet44771382019-03-14 20:19:47 -07001822 err = -EFAULT;
1823drop:
Heiner Kallweit497a5752020-11-07 21:50:56 +01001824 atomic_long_inc(&tun->dev->rx_dropped);
Jason Wang66ccbc92017-08-11 19:41:16 +08001825 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001826 if (frags) {
1827 tfile->napi.skb = NULL;
1828 mutex_unlock(&tfile->napi_mutex);
1829 }
1830
Eric Dumazet44771382019-03-14 20:19:47 -07001831 return err;
Jason Wang66ccbc92017-08-11 19:41:16 +08001832 }
Dave Jones8f227572006-03-11 18:49:13 -08001833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08001835 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
Heiner Kallweit497a5752020-11-07 21:50:56 +01001836 atomic_long_inc(&tun->rx_frame_errors);
Paolo Abenidf10db92016-06-14 00:00:04 +02001837 kfree_skb(skb);
Petar Penkov90e33d42017-09-22 13:49:15 -07001838 if (frags) {
1839 tfile->napi.skb = NULL;
1840 mutex_unlock(&tfile->napi_mutex);
1841 }
1842
Paolo Abenidf10db92016-06-14 00:00:04 +02001843 return -EINVAL;
1844 }
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001847 case IFF_TUN:
1848 if (tun->flags & IFF_NO_PI) {
Alexander Potapenko2580c4c2017-09-28 11:32:37 +02001849 u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
1850
1851 switch (ip_version) {
1852 case 4:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001853 pi.proto = htons(ETH_P_IP);
1854 break;
Alexander Potapenko2580c4c2017-09-28 11:32:37 +02001855 case 6:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001856 pi.proto = htons(ETH_P_IPV6);
1857 break;
1858 default:
Heiner Kallweit497a5752020-11-07 21:50:56 +01001859 atomic_long_inc(&tun->dev->rx_dropped);
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001860 kfree_skb(skb);
1861 return -EINVAL;
1862 }
1863 }
1864
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001865 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001867 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001869 case IFF_TAP:
Willem de Bruijn96aa1b22020-05-30 15:41:31 -04001870 if (frags && !pskb_may_pull(skb, ETH_HLEN)) {
1871 err = -ENOMEM;
1872 goto drop;
1873 }
1874 skb->protocol = eth_type_trans(skb, tun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001878 /* copy skb_ubuf_info for callback when skb has no error */
1879 if (zerocopy) {
Jonathan Lemon9ee5e5a2021-01-06 14:18:40 -08001880 skb_zcopy_init(skb, msg_control);
Jason Wangaf1cc7a2016-11-30 13:17:51 +08001881 } else if (msg_control) {
1882 struct ubuf_info *uarg = msg_control;
Jonathan Lemon36177832021-01-06 14:18:34 -08001883 uarg->callback(NULL, uarg, false);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001884 }
1885
Vlad Yasevich72f65102015-02-03 16:36:16 -05001886 skb_reset_network_header(skb);
Maxim Mikityanskiyd2aa1252019-02-21 12:39:57 +00001887 skb_probe_transport_header(skb);
Gilberto Bertin3fe260e2020-04-10 18:20:59 +02001888 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang38502af2013-03-25 20:19:56 +00001889
Jason Wang1cfe6e92017-09-04 11:36:09 +08001890 if (skb_xdp) {
Jason Wang761876c2017-08-11 19:41:18 +08001891 struct bpf_prog *xdp_prog;
1892 int ret;
1893
Toshiaki Makita6547e382018-05-28 19:37:49 +09001894 local_bh_disable();
Jason Wang761876c2017-08-11 19:41:18 +08001895 rcu_read_lock();
1896 xdp_prog = rcu_dereference(tun->xdp_prog);
1897 if (xdp_prog) {
1898 ret = do_xdp_generic(xdp_prog, skb);
1899 if (ret != XDP_PASS) {
1900 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001901 local_bh_enable();
Eric Dumazet1efba982020-01-22 09:07:35 -08001902 if (frags) {
1903 tfile->napi.skb = NULL;
1904 mutex_unlock(&tfile->napi_mutex);
1905 }
Jason Wang761876c2017-08-11 19:41:18 +08001906 return total_len;
1907 }
1908 }
1909 rcu_read_unlock();
Toshiaki Makita6547e382018-05-28 19:37:49 +09001910 local_bh_enable();
Jason Wang761876c2017-08-11 19:41:18 +08001911 }
1912
Paolo Abenicf1a1e02018-04-20 13:18:16 +02001913 /* Compute the costly rx hash only if needed for flow updates.
1914 * We may get a very small possibility of OOO during switching, not
1915 * worth to optimize.
1916 */
1917 if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 &&
1918 !tfile->detached)
Jason Wang96f84062017-12-04 17:31:23 +08001919 rxhash = __skb_get_hash_symmetric(skb);
Petar Penkov94317092017-09-22 13:49:14 -07001920
Eric Dumazet44771382019-03-14 20:19:47 -07001921 rcu_read_lock();
1922 if (unlikely(!(tun->dev->flags & IFF_UP))) {
1923 err = -EIO;
Eric Dumazet9180bb42019-03-16 13:09:53 -07001924 rcu_read_unlock();
Eric Dumazet44771382019-03-14 20:19:47 -07001925 goto drop;
1926 }
1927
Petar Penkov90e33d42017-09-22 13:49:15 -07001928 if (frags) {
Willem de Bruijn96aa1b22020-05-30 15:41:31 -04001929 u32 headlen;
1930
Petar Penkov90e33d42017-09-22 13:49:15 -07001931 /* Exercise flow dissector code path. */
Willem de Bruijn96aa1b22020-05-30 15:41:31 -04001932 skb_push(skb, ETH_HLEN);
1933 headlen = eth_get_headlen(tun->dev, skb->data,
1934 skb_headlen(skb));
Petar Penkov90e33d42017-09-22 13:49:15 -07001935
Eric Dumazet010f2452017-10-17 10:07:44 -07001936 if (unlikely(headlen > skb_headlen(skb))) {
Heiner Kallweit497a5752020-11-07 21:50:56 +01001937 atomic_long_inc(&tun->dev->rx_dropped);
Petar Penkov90e33d42017-09-22 13:49:15 -07001938 napi_free_frags(&tfile->napi);
Eric Dumazet44771382019-03-14 20:19:47 -07001939 rcu_read_unlock();
Petar Penkov90e33d42017-09-22 13:49:15 -07001940 mutex_unlock(&tfile->napi_mutex);
1941 WARN_ON(1);
1942 return -ENOMEM;
1943 }
1944
1945 local_bh_disable();
1946 napi_gro_frags(&tfile->napi);
1947 local_bh_enable();
1948 mutex_unlock(&tfile->napi_mutex);
Eric Dumazetaec72f32017-10-18 12:12:09 -07001949 } else if (tfile->napi_enabled) {
Petar Penkov94317092017-09-22 13:49:14 -07001950 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1951 int queue_len;
1952
1953 spin_lock_bh(&queue->lock);
1954 __skb_queue_tail(queue, skb);
1955 queue_len = skb_queue_len(queue);
1956 spin_unlock(&queue->lock);
1957
1958 if (!more || queue_len > NAPI_POLL_WEIGHT)
1959 napi_schedule(&tfile->napi);
1960
1961 local_bh_enable();
1962 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
1963 tun_rx_batched(tun, tfile, skb, more);
1964 } else {
1965 netif_rx_ni(skb);
1966 }
Eric Dumazet44771382019-03-14 20:19:47 -07001967 rcu_read_unlock();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001968
Heiner Kallweit497a5752020-11-07 21:50:56 +01001969 preempt_disable();
1970 dev_sw_netstats_rx_add(tun->dev, len);
1971 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Jason Wang96f84062017-12-04 17:31:23 +08001973 if (rxhash)
1974 tun_flow_update(tun, rxhash, tfile);
1975
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001976 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001977}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Al Virof5ff53b2014-06-19 15:36:49 -04001979static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001981 struct file *file = iocb->ki_filp;
Jason Wang54f968d2012-10-31 19:45:57 +00001982 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08001983 struct tun_struct *tun = tun_get(tfile);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001984 ssize_t result;
Jens Axboe5aac0392020-11-20 07:59:54 -07001985 int noblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 if (!tun)
1988 return -EBADFD;
1989
Jens Axboe5aac0392020-11-20 07:59:54 -07001990 if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
1991 noblock = 1;
1992
1993 result = tun_get_user(tun, tfile, NULL, from, noblock, false);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001994
1995 tun_put(tun);
1996 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Jason Wangfc72d1d2018-01-04 11:14:28 +08001999static ssize_t tun_put_user_xdp(struct tun_struct *tun,
2000 struct tun_file *tfile,
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002001 struct xdp_frame *xdp_frame,
Jason Wangfc72d1d2018-01-04 11:14:28 +08002002 struct iov_iter *iter)
2003{
2004 int vnet_hdr_sz = 0;
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002005 size_t size = xdp_frame->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002006 size_t ret;
2007
2008 if (tun->flags & IFF_VNET_HDR) {
2009 struct virtio_net_hdr gso = { 0 };
2010
2011 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
2012 if (unlikely(iov_iter_count(iter) < vnet_hdr_sz))
2013 return -EINVAL;
2014 if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) !=
2015 sizeof(gso)))
2016 return -EFAULT;
2017 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
2018 }
2019
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002020 ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002021
Heiner Kallweit497a5752020-11-07 21:50:56 +01002022 preempt_disable();
2023 dev_sw_netstats_tx_add(tun->dev, 1, ret);
2024 preempt_enable();
Jason Wangfc72d1d2018-01-04 11:14:28 +08002025
2026 return ret;
2027}
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00002030static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00002031 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00002032 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08002033 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 struct tun_pi pi = { 0, skb->protocol };
Herbert Xue0b46d02014-11-07 21:22:23 +08002036 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08002037 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002038 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08002039 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002040
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002041 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002042 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002044 if (tun->flags & IFF_VNET_HDR)
Willem de Bruijne1edab82017-02-03 18:20:48 -05002045 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Herbert Xue0b46d02014-11-07 21:22:23 +08002047 total = skb->len + vlan_hlen + vnet_hdr_sz;
2048
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002049 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08002050 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return -EINVAL;
2052
Herbert Xue0b46d02014-11-07 21:22:23 +08002053 total += sizeof(pi);
2054 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /* Packet will be striped */
2056 pi.flags |= TUN_PKT_STRIP;
2057 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002058
Herbert Xue0b46d02014-11-07 21:22:23 +08002059 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Herbert Xu2eb783c2014-11-03 04:30:14 +08002063 if (vnet_hdr_sz) {
Jarno Rajahalme9403cd72016-11-18 15:40:40 -08002064 struct virtio_net_hdr gso;
Mike Rapoport34166092016-06-08 16:09:20 +03002065
Herbert Xue0b46d02014-11-07 21:22:23 +08002066 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07002067 return -EINVAL;
2068
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -08002069 if (virtio_net_hdr_from_skb(skb, &gso,
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002070 tun_is_little_endian(tun), true,
2071 vlan_hlen)) {
Rusty Russellf43798c2008-07-03 03:48:02 -07002072 struct skb_shared_info *sinfo = skb_shinfo(skb);
Mike Rapoport34166092016-06-08 16:09:20 +03002073 pr_err("unexpected GSO type: "
2074 "0x%x, gso_size %d, hdr_len %d\n",
2075 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
2076 tun16_to_cpu(tun, gso.hdr_len));
2077 print_hex_dump(KERN_ERR, "tun: ",
2078 DUMP_PREFIX_NONE,
2079 16, 1, skb->head,
2080 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
2081 WARN_ON_ONCE(1);
2082 return -EINVAL;
2083 }
Rusty Russellf43798c2008-07-03 03:48:02 -07002084
Herbert Xue0b46d02014-11-07 21:22:23 +08002085 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07002086 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08002087
2088 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07002089 }
2090
Herbert Xua8f9bfd2014-11-03 04:30:13 +08002091 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08002092 int ret;
Jason Wangaff3d702018-01-16 16:31:02 +08002093 struct veth veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Jason Wang6680ec62013-07-25 13:00:33 +08002095 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002096 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Jason Wang6680ec62013-07-25 13:00:33 +08002098 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08002099
Herbert Xue0b46d02014-11-07 21:22:23 +08002100 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
2101 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08002102 goto done;
2103
Herbert Xue0b46d02014-11-07 21:22:23 +08002104 ret = copy_to_iter(&veth, sizeof(veth), iter);
2105 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08002106 goto done;
2107 }
2108
Herbert Xue0b46d02014-11-07 21:22:23 +08002109 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08002110
2111done:
Paolo Abeni608b9972016-04-13 10:52:20 +02002112 /* caller is in process context, */
Heiner Kallweit497a5752020-11-07 21:50:56 +01002113 preempt_disable();
2114 dev_sw_netstats_tx_add(tun->dev, 1, skb->len + vlan_hlen);
2115 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 return total;
2118}
2119
Jason Wangfc72d1d2018-01-04 11:14:28 +08002120static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err)
Jason Wang1576d982016-06-30 14:45:36 +08002121{
2122 DECLARE_WAITQUEUE(wait, current);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002123 void *ptr = NULL;
Jason Wangf48cc6b2016-07-04 13:53:38 +08002124 int error = 0;
Jason Wang1576d982016-06-30 14:45:36 +08002125
Jason Wangfc72d1d2018-01-04 11:14:28 +08002126 ptr = ptr_ring_consume(&tfile->tx_ring);
2127 if (ptr)
Jason Wang1576d982016-06-30 14:45:36 +08002128 goto out;
2129 if (noblock) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002130 error = -EAGAIN;
Jason Wang1576d982016-06-30 14:45:36 +08002131 goto out;
2132 }
2133
Al Viro333f7902019-07-05 20:14:16 +01002134 add_wait_queue(&tfile->socket.wq.wait, &wait);
Jason Wang1576d982016-06-30 14:45:36 +08002135
2136 while (1) {
Timur Celik71828b22019-02-23 12:53:13 +01002137 set_current_state(TASK_INTERRUPTIBLE);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002138 ptr = ptr_ring_consume(&tfile->tx_ring);
2139 if (ptr)
Jason Wang1576d982016-06-30 14:45:36 +08002140 break;
2141 if (signal_pending(current)) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002142 error = -ERESTARTSYS;
Jason Wang1576d982016-06-30 14:45:36 +08002143 break;
2144 }
2145 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
Jason Wangf48cc6b2016-07-04 13:53:38 +08002146 error = -EFAULT;
Jason Wang1576d982016-06-30 14:45:36 +08002147 break;
2148 }
2149
2150 schedule();
2151 }
2152
Timur Celikecef67c2019-02-25 21:13:13 +01002153 __set_current_state(TASK_RUNNING);
Al Viro333f7902019-07-05 20:14:16 +01002154 remove_wait_queue(&tfile->socket.wq.wait, &wait);
Jason Wang1576d982016-06-30 14:45:36 +08002155
2156out:
Jason Wangf48cc6b2016-07-04 13:53:38 +08002157 *err = error;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002158 return ptr;
Jason Wang1576d982016-06-30 14:45:36 +08002159}
2160
Jason Wang54f968d2012-10-31 19:45:57 +00002161static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05002162 struct iov_iter *to,
Jason Wangfc72d1d2018-01-04 11:14:28 +08002163 int noblock, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Al Viro9b067032014-11-07 13:52:07 -05002165 ssize_t ret;
Jason Wang1576d982016-06-30 14:45:36 +08002166 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Wei Xuc33ee152017-12-01 05:10:37 -05002168 if (!iov_iter_count(to)) {
Jason Wangfc72d1d2018-01-04 11:14:28 +08002169 tun_ptr_free(ptr);
Al Viro9b067032014-11-07 13:52:07 -05002170 return 0;
Wei Xuc33ee152017-12-01 05:10:37 -05002171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Jason Wangfc72d1d2018-01-04 11:14:28 +08002173 if (!ptr) {
Jason Wangac77cfd2017-05-17 12:14:43 +08002174 /* Read frames from ring */
Jason Wangfc72d1d2018-01-04 11:14:28 +08002175 ptr = tun_ring_recv(tfile, noblock, &err);
2176 if (!ptr)
Jason Wangac77cfd2017-05-17 12:14:43 +08002177 return err;
2178 }
Herbert Xue0b46d02014-11-07 21:22:23 +08002179
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002180 if (tun_is_xdp_frame(ptr)) {
2181 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002182
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002183 ret = tun_put_user_xdp(tun, tfile, xdpf, to);
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +02002184 xdp_return_frame(xdpf);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002185 } else {
2186 struct sk_buff *skb = ptr;
2187
2188 ret = tun_put_user(tun, tfile, skb, to);
2189 if (unlikely(ret < 0))
2190 kfree_skb(skb);
2191 else
2192 consume_skb(skb);
2193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002195 return ret;
2196}
2197
Al Viro9b067032014-11-07 13:52:07 -05002198static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002199{
2200 struct file *file = iocb->ki_filp;
2201 struct tun_file *tfile = file->private_data;
yuan linyu9484dc72017-09-23 22:36:52 +08002202 struct tun_struct *tun = tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05002203 ssize_t len = iov_iter_count(to), ret;
Jens Axboe5aac0392020-11-20 07:59:54 -07002204 int noblock = 0;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002205
2206 if (!tun)
2207 return -EBADFD;
Jens Axboe5aac0392020-11-20 07:59:54 -07002208
2209 if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
2210 noblock = 1;
2211
2212 ret = tun_do_read(tun, tfile, to, noblock, NULL);
David S. Miller42404c02013-12-10 22:05:45 -05002213 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08002214 if (ret > 0)
2215 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00002216 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return ret;
2218}
2219
Jason Wangcd5681d2018-01-16 16:31:01 +08002220static void tun_prog_free(struct rcu_head *rcu)
Jason Wang96f84062017-12-04 17:31:23 +08002221{
Jason Wangcd5681d2018-01-16 16:31:01 +08002222 struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu);
Jason Wang96f84062017-12-04 17:31:23 +08002223
2224 bpf_prog_destroy(prog->prog);
2225 kfree(prog);
2226}
2227
Jason Wang9d6474e2018-01-22 10:55:38 +08002228static int __tun_set_ebpf(struct tun_struct *tun,
2229 struct tun_prog __rcu **prog_p,
Jason Wangcd5681d2018-01-16 16:31:01 +08002230 struct bpf_prog *prog)
Jason Wang96f84062017-12-04 17:31:23 +08002231{
Jason Wangcd5681d2018-01-16 16:31:01 +08002232 struct tun_prog *old, *new = NULL;
Jason Wang96f84062017-12-04 17:31:23 +08002233
2234 if (prog) {
2235 new = kmalloc(sizeof(*new), GFP_KERNEL);
2236 if (!new)
2237 return -ENOMEM;
2238 new->prog = prog;
2239 }
2240
Jason Wang124da8f2017-12-08 12:02:30 +08002241 spin_lock_bh(&tun->lock);
Jason Wangcd5681d2018-01-16 16:31:01 +08002242 old = rcu_dereference_protected(*prog_p,
Jason Wang124da8f2017-12-08 12:02:30 +08002243 lockdep_is_held(&tun->lock));
Jason Wangcd5681d2018-01-16 16:31:01 +08002244 rcu_assign_pointer(*prog_p, new);
Jason Wang124da8f2017-12-08 12:02:30 +08002245 spin_unlock_bh(&tun->lock);
Jason Wang96f84062017-12-04 17:31:23 +08002246
2247 if (old)
Jason Wangcd5681d2018-01-16 16:31:01 +08002248 call_rcu(&old->rcu, tun_prog_free);
Jason Wang96f84062017-12-04 17:31:23 +08002249
2250 return 0;
2251}
2252
Jason Wang96442e422012-10-31 19:46:02 +00002253static void tun_free_netdev(struct net_device *dev)
2254{
2255 struct tun_struct *tun = netdev_priv(dev);
2256
Jason Wang4008e972012-12-13 23:53:30 +00002257 BUG_ON(!(list_empty(&tun->disabled)));
Eric Dumazet11fc7d52019-10-07 12:21:05 -07002258
Heiner Kallweit497a5752020-11-07 21:50:56 +01002259 free_percpu(dev->tstats);
Jason Wang96442e422012-10-31 19:46:02 +00002260 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00002261 security_tun_dev_free_security(tun->security);
Jason Wangcd5681d2018-01-16 16:31:01 +08002262 __tun_set_ebpf(tun, &tun->steering_prog, NULL);
Jason Wangaff3d702018-01-16 16:31:02 +08002263 __tun_set_ebpf(tun, &tun->filter_prog, NULL);
Jason Wang96442e422012-10-31 19:46:02 +00002264}
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266static void tun_setup(struct net_device *dev)
2267{
2268 struct tun_struct *tun = netdev_priv(dev);
2269
Eric W. Biederman0625c882012-02-07 16:48:55 -08002270 tun->owner = INVALID_UID;
2271 tun->group = INVALID_GID;
Chas Williams4e24f2d2018-06-02 17:49:53 -04002272 tun_default_link_ksettings(dev, &tun->link_ksettings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 dev->ethtool_ops = &tun_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04002275 dev->needs_free_netdev = true;
2276 dev->priv_destructor = tun_free_netdev;
Jason Wang016adb72016-04-08 13:26:48 +08002277 /* We prefer our own queue length */
2278 dev->tx_queue_len = TUN_READQ_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002281/* Trivial set of netlink ops to allow deleting tun or tap
2282 * device with netlink.
2283 */
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002284static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
2285 struct netlink_ext_ack *extack)
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002286{
Nicolas Dichtel35b827b2018-11-29 14:45:39 +01002287 NL_SET_ERR_MSG(extack,
2288 "tun/tap creation via rtnetlink is not supported.");
2289 return -EOPNOTSUPP;
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002290}
2291
Sabrina Dubroca1ec010e2018-02-16 11:03:07 +01002292static size_t tun_get_size(const struct net_device *dev)
2293{
2294 BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t));
2295 BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t));
2296
2297 return nla_total_size(sizeof(uid_t)) + /* OWNER */
2298 nla_total_size(sizeof(gid_t)) + /* GROUP */
2299 nla_total_size(sizeof(u8)) + /* TYPE */
2300 nla_total_size(sizeof(u8)) + /* PI */
2301 nla_total_size(sizeof(u8)) + /* VNET_HDR */
2302 nla_total_size(sizeof(u8)) + /* PERSIST */
2303 nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */
2304 nla_total_size(sizeof(u32)) + /* NUM_QUEUES */
2305 nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */
2306 0;
2307}
2308
2309static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
2310{
2311 struct tun_struct *tun = netdev_priv(dev);
2312
2313 if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK))
2314 goto nla_put_failure;
2315 if (uid_valid(tun->owner) &&
2316 nla_put_u32(skb, IFLA_TUN_OWNER,
2317 from_kuid_munged(current_user_ns(), tun->owner)))
2318 goto nla_put_failure;
2319 if (gid_valid(tun->group) &&
2320 nla_put_u32(skb, IFLA_TUN_GROUP,
2321 from_kgid_munged(current_user_ns(), tun->group)))
2322 goto nla_put_failure;
2323 if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI)))
2324 goto nla_put_failure;
2325 if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR)))
2326 goto nla_put_failure;
2327 if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST)))
2328 goto nla_put_failure;
2329 if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE,
2330 !!(tun->flags & IFF_MULTI_QUEUE)))
2331 goto nla_put_failure;
2332 if (tun->flags & IFF_MULTI_QUEUE) {
2333 if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues))
2334 goto nla_put_failure;
2335 if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES,
2336 tun->numdisabled))
2337 goto nla_put_failure;
2338 }
2339
2340 return 0;
2341
2342nla_put_failure:
2343 return -EMSGSIZE;
2344}
2345
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002346static struct rtnl_link_ops tun_link_ops __read_mostly = {
2347 .kind = DRV_NAME,
2348 .priv_size = sizeof(struct tun_struct),
2349 .setup = tun_setup,
2350 .validate = tun_validate,
Sabrina Dubroca1ec010e2018-02-16 11:03:07 +01002351 .get_size = tun_get_size,
2352 .fill_info = tun_fill_info,
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002353};
2354
Herbert Xu33dccbb2009-02-05 21:25:32 -08002355static void tun_sock_write_space(struct sock *sk)
2356{
Jason Wang54f968d2012-10-31 19:45:57 +00002357 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00002358 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002359
2360 if (!sock_writeable(sk))
2361 return;
2362
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002363 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Herbert Xu33dccbb2009-02-05 21:25:32 -08002364 return;
2365
Eric Dumazet43815482010-04-29 11:01:49 +00002366 wqueue = sk_sleep(sk);
2367 if (wqueue && waitqueue_active(wqueue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002368 wake_up_interruptible_sync_poll(wqueue, EPOLLOUT |
2369 EPOLLWRNORM | EPOLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07002370
Jason Wang54f968d2012-10-31 19:45:57 +00002371 tfile = container_of(sk, struct tun_file, sk);
2372 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002373}
2374
Jason Wangf9e06c42018-11-15 17:43:10 +08002375static void tun_put_page(struct tun_page *tpage)
2376{
2377 if (tpage->page)
2378 __page_frag_cache_drain(tpage->page, tpage->count);
2379}
2380
Jason Wang043d2222018-09-12 11:17:07 +08002381static int tun_xdp_one(struct tun_struct *tun,
2382 struct tun_file *tfile,
Jason Wangf9e06c42018-11-15 17:43:10 +08002383 struct xdp_buff *xdp, int *flush,
2384 struct tun_page *tpage)
Jason Wang043d2222018-09-12 11:17:07 +08002385{
Prashant Bhole4e4b08e2018-12-03 18:09:24 +09002386 unsigned int datasize = xdp->data_end - xdp->data;
Jason Wang043d2222018-09-12 11:17:07 +08002387 struct tun_xdp_hdr *hdr = xdp->data_hard_start;
2388 struct virtio_net_hdr *gso = &hdr->gso;
Jason Wang043d2222018-09-12 11:17:07 +08002389 struct bpf_prog *xdp_prog;
2390 struct sk_buff *skb = NULL;
2391 u32 rxhash = 0, act;
2392 int buflen = hdr->buflen;
2393 int err = 0;
2394 bool skb_xdp = false;
Jason Wangf9e06c42018-11-15 17:43:10 +08002395 struct page *page;
Jason Wang043d2222018-09-12 11:17:07 +08002396
2397 xdp_prog = rcu_dereference(tun->xdp_prog);
2398 if (xdp_prog) {
2399 if (gso->gso_type) {
2400 skb_xdp = true;
2401 goto build;
2402 }
Lorenzo Bianconi43b51692020-12-22 22:09:28 +01002403
2404 xdp_init_buff(xdp, buflen, &tfile->xdp_rxq);
Jason Wang043d2222018-09-12 11:17:07 +08002405 xdp_set_data_meta_invalid(xdp);
Jason Wang043d2222018-09-12 11:17:07 +08002406
2407 act = bpf_prog_run_xdp(xdp_prog, xdp);
2408 err = tun_xdp_act(tun, xdp_prog, xdp, act);
2409 if (err < 0) {
2410 put_page(virt_to_head_page(xdp->data));
2411 return err;
2412 }
2413
2414 switch (err) {
2415 case XDP_REDIRECT:
2416 *flush = true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002417 fallthrough;
Jason Wang043d2222018-09-12 11:17:07 +08002418 case XDP_TX:
2419 return 0;
2420 case XDP_PASS:
2421 break;
2422 default:
Jason Wangf9e06c42018-11-15 17:43:10 +08002423 page = virt_to_head_page(xdp->data);
2424 if (tpage->page == page) {
2425 ++tpage->count;
2426 } else {
2427 tun_put_page(tpage);
2428 tpage->page = page;
2429 tpage->count = 1;
2430 }
Jason Wang043d2222018-09-12 11:17:07 +08002431 return 0;
2432 }
2433 }
2434
2435build:
2436 skb = build_skb(xdp->data_hard_start, buflen);
2437 if (!skb) {
2438 err = -ENOMEM;
2439 goto out;
2440 }
2441
2442 skb_reserve(skb, xdp->data - xdp->data_hard_start);
2443 skb_put(skb, xdp->data_end - xdp->data);
2444
2445 if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) {
Heiner Kallweit497a5752020-11-07 21:50:56 +01002446 atomic_long_inc(&tun->rx_frame_errors);
Jason Wang043d2222018-09-12 11:17:07 +08002447 kfree_skb(skb);
2448 err = -EINVAL;
2449 goto out;
2450 }
2451
2452 skb->protocol = eth_type_trans(skb, tun->dev);
2453 skb_reset_network_header(skb);
Maxim Mikityanskiyd2aa1252019-02-21 12:39:57 +00002454 skb_probe_transport_header(skb);
Gilberto Bertin3fe260e2020-04-10 18:20:59 +02002455 skb_record_rx_queue(skb, tfile->queue_index);
Jason Wang043d2222018-09-12 11:17:07 +08002456
2457 if (skb_xdp) {
2458 err = do_xdp_generic(xdp_prog, skb);
2459 if (err != XDP_PASS)
2460 goto out;
2461 }
2462
Paolo Abenif29eb2a2018-11-07 10:34:36 +01002463 if (!rcu_dereference(tun->steering_prog) && tun->numqueues > 1 &&
2464 !tfile->detached)
Jason Wang043d2222018-09-12 11:17:07 +08002465 rxhash = __skb_get_hash_symmetric(skb);
2466
2467 netif_receive_skb(skb);
2468
Heiner Kallweit497a5752020-11-07 21:50:56 +01002469 /* No need to disable preemption here since this function is
Prashant Bhole6342ca62018-12-11 11:43:07 +09002470 * always called with bh disabled
2471 */
Heiner Kallweit497a5752020-11-07 21:50:56 +01002472 dev_sw_netstats_rx_add(tun->dev, datasize);
Jason Wang043d2222018-09-12 11:17:07 +08002473
2474 if (rxhash)
2475 tun_flow_update(tun, rxhash, tfile);
2476
2477out:
2478 return err;
2479}
2480
Ying Xue1b784142015-03-02 15:37:48 +08002481static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002482{
Jason Wang043d2222018-09-12 11:17:07 +08002483 int ret, i;
Jason Wang54f968d2012-10-31 19:45:57 +00002484 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002485 struct tun_struct *tun = tun_get(tfile);
Jason Wangfe8dd452018-09-12 11:17:06 +08002486 struct tun_msg_ctl *ctl = m->msg_control;
Jason Wang043d2222018-09-12 11:17:07 +08002487 struct xdp_buff *xdp;
Jason Wang54f968d2012-10-31 19:45:57 +00002488
2489 if (!tun)
2490 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04002491
Jason Wang043d2222018-09-12 11:17:07 +08002492 if (ctl && (ctl->type == TUN_MSG_PTR)) {
David S. Miller6f0271d2018-11-17 16:53:46 -08002493 struct tun_page tpage;
Jason Wang043d2222018-09-12 11:17:07 +08002494 int n = ctl->num;
2495 int flush = 0;
2496
David S. Miller6f0271d2018-11-17 16:53:46 -08002497 memset(&tpage, 0, sizeof(tpage));
2498
Jason Wang043d2222018-09-12 11:17:07 +08002499 local_bh_disable();
2500 rcu_read_lock();
2501
2502 for (i = 0; i < n; i++) {
2503 xdp = &((struct xdp_buff *)ctl->ptr)[i];
Jason Wangf9e06c42018-11-15 17:43:10 +08002504 tun_xdp_one(tun, tfile, xdp, &flush, &tpage);
Jason Wang043d2222018-09-12 11:17:07 +08002505 }
2506
2507 if (flush)
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +01002508 xdp_do_flush();
Jason Wang043d2222018-09-12 11:17:07 +08002509
2510 rcu_read_unlock();
2511 local_bh_enable();
2512
Jason Wangf9e06c42018-11-15 17:43:10 +08002513 tun_put_page(&tpage);
2514
Jason Wang043d2222018-09-12 11:17:07 +08002515 ret = total_len;
2516 goto out;
2517 }
Jason Wangfe8dd452018-09-12 11:17:06 +08002518
2519 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter,
Jason Wang5503fce2017-01-18 15:02:03 +08002520 m->msg_flags & MSG_DONTWAIT,
2521 m->msg_flags & MSG_MORE);
Jason Wang043d2222018-09-12 11:17:07 +08002522out:
Jason Wang54f968d2012-10-31 19:45:57 +00002523 tun_put(tun);
2524 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002525}
2526
Ying Xue1b784142015-03-02 15:37:48 +08002527static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002528 int flags)
2529{
Jason Wang54f968d2012-10-31 19:45:57 +00002530 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
yuan linyu9484dc72017-09-23 22:36:52 +08002531 struct tun_struct *tun = tun_get(tfile);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002532 void *ptr = m->msg_control;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002533 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00002534
Wei Xuc33ee152017-12-01 05:10:37 -05002535 if (!tun) {
2536 ret = -EBADFD;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002537 goto out_free;
Wei Xuc33ee152017-12-01 05:10:37 -05002538 }
Jason Wang54f968d2012-10-31 19:45:57 +00002539
Richard Cochraneda29772013-07-19 19:40:10 +02002540 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00002541 ret = -EINVAL;
Wei Xuc33ee152017-12-01 05:10:37 -05002542 goto out_put_tun;
Gao feng3811ae72013-04-24 21:59:23 +00002543 }
Richard Cochraneda29772013-07-19 19:40:10 +02002544 if (flags & MSG_ERRQUEUE) {
2545 ret = sock_recv_errqueue(sock->sk, m, total_len,
2546 SOL_PACKET, TUN_TX_TIMESTAMP);
2547 goto out;
2548 }
Jason Wangfc72d1d2018-01-04 11:14:28 +08002549 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr);
Alex Gartrell87897932014-12-25 23:05:03 -08002550 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05002551 m->msg_flags |= MSG_TRUNC;
2552 ret = flags & MSG_TRUNC ? ret : total_len;
2553 }
Gao feng3811ae72013-04-24 21:59:23 +00002554out:
Jason Wang54f968d2012-10-31 19:45:57 +00002555 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002556 return ret;
Wei Xuc33ee152017-12-01 05:10:37 -05002557
2558out_put_tun:
2559 tun_put(tun);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002560out_free:
2561 tun_ptr_free(ptr);
Wei Xuc33ee152017-12-01 05:10:37 -05002562 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002563}
2564
Jason Wangfc72d1d2018-01-04 11:14:28 +08002565static int tun_ptr_peek_len(void *ptr)
2566{
2567 if (likely(ptr)) {
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002568 if (tun_is_xdp_frame(ptr)) {
2569 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +08002570
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +02002571 return xdpf->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +08002572 }
2573 return __skb_array_len_with_tag(ptr);
2574 } else {
2575 return 0;
2576 }
2577}
2578
Jason Wang1576d982016-06-30 14:45:36 +08002579static int tun_peek_len(struct socket *sock)
2580{
2581 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
2582 struct tun_struct *tun;
2583 int ret = 0;
2584
yuan linyu9484dc72017-09-23 22:36:52 +08002585 tun = tun_get(tfile);
Jason Wang1576d982016-06-30 14:45:36 +08002586 if (!tun)
2587 return 0;
2588
Jason Wangfc72d1d2018-01-04 11:14:28 +08002589 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len);
Jason Wang1576d982016-06-30 14:45:36 +08002590 tun_put(tun);
2591
2592 return ret;
2593}
2594
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002595/* Ops structure to mimic raw sockets with tun */
2596static const struct proto_ops tun_socket_ops = {
Jason Wang1576d982016-06-30 14:45:36 +08002597 .peek_len = tun_peek_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002598 .sendmsg = tun_sendmsg,
2599 .recvmsg = tun_recvmsg,
2600};
2601
Herbert Xu33dccbb2009-02-05 21:25:32 -08002602static struct proto tun_proto = {
2603 .name = "tun",
2604 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00002605 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08002606};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002607
David Woodhouse980c9e82009-05-09 22:54:21 -07002608static int tun_flags(struct tun_struct *tun)
2609{
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02002610 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07002611}
2612
YueHaibingbc6d0762021-05-19 10:38:50 +08002613static ssize_t tun_flags_show(struct device *dev, struct device_attribute *attr,
David Woodhouse980c9e82009-05-09 22:54:21 -07002614 char *buf)
2615{
2616 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2617 return sprintf(buf, "0x%x\n", tun_flags(tun));
2618}
2619
YueHaibingbc6d0762021-05-19 10:38:50 +08002620static ssize_t owner_show(struct device *dev, struct device_attribute *attr,
2621 char *buf)
David Woodhouse980c9e82009-05-09 22:54:21 -07002622{
2623 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002624 return uid_valid(tun->owner)?
2625 sprintf(buf, "%u\n",
2626 from_kuid_munged(current_user_ns(), tun->owner)):
2627 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002628}
2629
YueHaibingbc6d0762021-05-19 10:38:50 +08002630static ssize_t group_show(struct device *dev, struct device_attribute *attr,
2631 char *buf)
David Woodhouse980c9e82009-05-09 22:54:21 -07002632{
2633 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08002634 return gid_valid(tun->group) ?
2635 sprintf(buf, "%u\n",
2636 from_kgid_munged(current_user_ns(), tun->group)):
2637 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07002638}
2639
YueHaibingbc6d0762021-05-19 10:38:50 +08002640static DEVICE_ATTR_RO(tun_flags);
2641static DEVICE_ATTR_RO(owner);
2642static DEVICE_ATTR_RO(group);
David Woodhouse980c9e82009-05-09 22:54:21 -07002643
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002644static struct attribute *tun_dev_attrs[] = {
2645 &dev_attr_tun_flags.attr,
2646 &dev_attr_owner.attr,
2647 &dev_attr_group.attr,
2648 NULL
2649};
2650
2651static const struct attribute_group tun_attr_group = {
2652 .attrs = tun_dev_attrs
2653};
2654
Pavel Emelyanovd647a592008-04-16 00:41:16 -07002655static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00002658 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 struct net_device *dev;
2660 int err;
2661
Jason Wang7c0c3b12013-01-11 16:59:33 +00002662 if (tfile->detached)
2663 return -EINVAL;
2664
Petar Penkov90e33d42017-09-22 13:49:15 -07002665 if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
2666 if (!capable(CAP_NET_ADMIN))
2667 return -EPERM;
2668
2669 if (!(ifr->ifr_flags & IFF_NAPI) ||
2670 (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
2671 return -EINVAL;
2672 }
2673
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002674 dev = __dev_get_by_name(net, ifr->ifr_name);
2675 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07002676 if (ifr->ifr_flags & IFF_TUN_EXCL)
2677 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00002678 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2679 tun = netdev_priv(dev);
2680 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2681 tun = netdev_priv(dev);
2682 else
2683 return -EINVAL;
2684
Jason Wang8e6d91a2013-05-28 18:32:11 +00002685 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002686 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00002687 return -EINVAL;
2688
Jason Wangcde8b152012-10-31 19:46:01 +00002689 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04002690 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002691 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04002692 if (err < 0)
2693 return err;
2694
Petar Penkov94317092017-09-22 13:49:14 -07002695 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002696 ifr->ifr_flags & IFF_NAPI,
Yang Yingliang77f22f92019-09-10 18:56:57 +08002697 ifr->ifr_flags & IFF_NAPI_FRAGS, true);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00002698 if (err < 0)
2699 return err;
Jason Wang4008e972012-12-13 23:53:30 +00002700
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002701 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00002702 (tun->numqueues + tun->numdisabled > 1)) {
2703 /* One or more queue has already been attached, no need
2704 * to initialize the device again.
2705 */
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002706 netdev_state_change(dev);
Jason Wange8dbad62013-04-22 20:40:39 +00002707 return 0;
2708 }
Sabrina Dubroca9fffc5c2018-04-10 16:28:55 +02002709
2710 tun->flags = (tun->flags & ~TUN_FEATURES) |
2711 (ifr->ifr_flags & TUN_FEATURES);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002712
2713 netdev_state_change(dev);
2714 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 char *name;
2716 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00002717 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2718 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Eric W. Biedermanc260b772012-11-18 21:34:11 +00002720 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002721 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04002722 err = security_tun_dev_create();
2723 if (err < 0)
2724 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /* Set dev type */
2727 if (ifr->ifr_flags & IFF_TUN) {
2728 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002729 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 name = "tun%d";
2731 } else if (ifr->ifr_flags & IFF_TAP) {
2732 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002733 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002735 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00002736 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (*ifr->ifr_name)
2739 name = ifr->ifr_name;
2740
Jason Wangc8d68e62012-10-31 19:46:00 +00002741 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02002742 NET_NAME_UNKNOWN, tun_setup, queues,
2743 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00002744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (!dev)
2746 return -ENOMEM;
2747
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07002748 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002749 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002750 dev->ifindex = tfile->ifindex;
Takashi Iwaic4d33e22015-02-04 14:37:34 +01002751 dev->sysfs_groups[0] = &tun_attr_group;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 tun = netdev_priv(dev);
2754 tun->dev = dev;
2755 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002756 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002757 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Paolo Abenieaea34b2016-02-26 10:45:40 +01002759 tun->align = NET_SKB_PAD;
Jason Wang54f968d2012-10-31 19:45:57 +00002760 tun->filter_attached = false;
2761 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Jason Wang5503fce2017-01-18 15:02:03 +08002762 tun->rx_batched = 0;
Jason Wang96f84062017-12-04 17:31:23 +08002763 RCU_INIT_POINTER(tun->steering_prog, NULL);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002764
George Kennedy158b5152021-12-16 13:25:32 -05002765 tun->ifr = ifr;
2766 tun->file = file;
Paolo Abeni608b9972016-04-13 10:52:20 +02002767
George Kennedy158b5152021-12-16 13:25:32 -05002768 tun_net_initialize(dev);
Jason Wangeb0fb362012-12-02 17:19:45 +00002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 err = register_netdevice(tun->dev);
George Kennedy158b5152021-12-16 13:25:32 -05002771 if (err < 0) {
2772 free_netdev(dev);
2773 return err;
2774 }
Menglong Dongc2e315b2021-01-18 03:15:39 -08002775 /* free_netdev() won't check refcnt, to avoid race
Yang Yingliang77f22f92019-09-10 18:56:57 +08002776 * with dev_put() we need publish tun after registration.
2777 */
2778 rcu_assign_pointer(tfile->tun, tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00002781 netif_carrier_on(tun->dev);
2782
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002783 /* Make sure persistent devices do not get stuck in
2784 * xoff state.
2785 */
2786 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00002787 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07002788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 strcpy(ifr->ifr_name, tun->dev->name);
2790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
Kirill Tkhai12132762019-03-20 12:16:53 +03002793static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07002794{
Mark McLoughline3b99552008-08-15 15:09:56 -07002795 strcpy(ifr->ifr_name, tun->dev->name);
2796
David Woodhouse980c9e82009-05-09 22:54:21 -07002797 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07002798
Mark McLoughline3b99552008-08-15 15:09:56 -07002799}
2800
Rusty Russell5228ddc2008-07-03 03:46:16 -07002801/* This is like a cut-down ethtool ops, except done via tun fd so no
2802 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00002803static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07002804{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002805 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002806
2807 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00002808 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002809 arg &= ~TUN_F_CSUM;
2810
2811 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2812 if (arg & TUN_F_TSO_ECN) {
2813 features |= NETIF_F_TSO_ECN;
2814 arg &= ~TUN_F_TSO_ECN;
2815 }
2816 if (arg & TUN_F_TSO4)
2817 features |= NETIF_F_TSO;
2818 if (arg & TUN_F_TSO6)
2819 features |= NETIF_F_TSO6;
2820 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2821 }
Willem de Bruijn0c19f8462017-11-21 10:22:25 -05002822
2823 arg &= ~TUN_F_UFO;
Rusty Russell5228ddc2008-07-03 03:46:16 -07002824 }
2825
2826 /* This gives the user a way to test for new features in future by
2827 * trying to set them. */
2828 if (arg)
2829 return -EINVAL;
2830
Michał Mirosław88255372011-04-19 06:13:10 +00002831 tun->set_features = features;
Yaroslav Isakov09050952017-03-16 22:44:10 +03002832 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2833 tun->dev->wanted_features |= features;
Michał Mirosław88255372011-04-19 06:13:10 +00002834 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07002835
2836 return 0;
2837}
2838
Jason Wangc8d68e62012-10-31 19:46:00 +00002839static void tun_detach_filter(struct tun_struct *tun, int n)
2840{
2841 int i;
2842 struct tun_file *tfile;
2843
2844 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002845 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002846 lock_sock(tfile->socket.sk);
2847 sk_detach_filter(tfile->socket.sk);
2848 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002849 }
2850
2851 tun->filter_attached = false;
2852}
2853
2854static int tun_attach_filter(struct tun_struct *tun)
2855{
2856 int i, ret = 0;
2857 struct tun_file *tfile;
2858
2859 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002860 tfile = rtnl_dereference(tun->tfiles[i]);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002861 lock_sock(tfile->socket.sk);
2862 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2863 release_sock(tfile->socket.sk);
Jason Wangc8d68e62012-10-31 19:46:00 +00002864 if (ret) {
2865 tun_detach_filter(tun, i);
2866 return ret;
2867 }
2868 }
2869
2870 tun->filter_attached = true;
2871 return ret;
2872}
2873
2874static void tun_set_sndbuf(struct tun_struct *tun)
2875{
2876 struct tun_file *tfile;
2877 int i;
2878
2879 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002880 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00002881 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2882 }
2883}
2884
Jason Wangcde8b152012-10-31 19:46:01 +00002885static int tun_set_queue(struct file *file, struct ifreq *ifr)
2886{
2887 struct tun_file *tfile = file->private_data;
2888 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00002889 int ret = 0;
2890
2891 rtnl_lock();
2892
2893 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00002894 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002895 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00002896 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00002897 goto unlock;
2898 }
2899 ret = security_tun_dev_attach_queue(tun->security);
2900 if (ret < 0)
2901 goto unlock;
Eric Dumazetaf3fb242018-09-28 14:51:49 -07002902 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI,
Yang Yingliang77f22f92019-09-10 18:56:57 +08002903 tun->flags & IFF_NAPI_FRAGS, true);
Jason Wang4008e972012-12-13 23:53:30 +00002904 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00002905 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002906 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00002907 ret = -EINVAL;
2908 else
2909 __tun_detach(tfile, false);
2910 } else
Jason Wangcde8b152012-10-31 19:46:01 +00002911 ret = -EINVAL;
2912
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002913 if (ret >= 0)
2914 netdev_state_change(tun->dev);
2915
Paul Moore5dbbaf22013-01-14 07:12:19 +00002916unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00002917 rtnl_unlock();
2918 return ret;
2919}
2920
Jason Wang8f3f3302020-07-31 00:17:20 -04002921static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
Jason Wangcd5681d2018-01-16 16:31:01 +08002922 void __user *data)
Jason Wang96f84062017-12-04 17:31:23 +08002923{
2924 struct bpf_prog *prog;
2925 int fd;
2926
2927 if (copy_from_user(&fd, data, sizeof(fd)))
2928 return -EFAULT;
2929
2930 if (fd == -1) {
2931 prog = NULL;
2932 } else {
2933 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
2934 if (IS_ERR(prog))
2935 return PTR_ERR(prog);
2936 }
2937
Jason Wangcd5681d2018-01-16 16:31:01 +08002938 return __tun_set_ebpf(tun, prog_p, prog);
Jason Wang96f84062017-12-04 17:31:23 +08002939}
2940
Phillip Pottercca8ea32021-04-06 18:45:54 +01002941/* Return correct value for tun->dev->addr_len based on tun->dev->type. */
2942static unsigned char tun_get_addr_len(unsigned short type)
2943{
2944 switch (type) {
2945 case ARPHRD_IP6GRE:
2946 case ARPHRD_TUNNEL6:
2947 return sizeof(struct in6_addr);
2948 case ARPHRD_IPGRE:
2949 case ARPHRD_TUNNEL:
2950 case ARPHRD_SIT:
2951 return 4;
2952 case ARPHRD_ETHER:
2953 return ETH_ALEN;
2954 case ARPHRD_IEEE802154:
2955 case ARPHRD_IEEE802154_MONITOR:
2956 return IEEE802154_EXTENDED_ADDR_LEN;
2957 case ARPHRD_PHONET_PIPE:
2958 case ARPHRD_PPP:
2959 case ARPHRD_NONE:
2960 return 0;
2961 case ARPHRD_6LOWPAN:
2962 return EUI64_ADDR_LEN;
2963 case ARPHRD_FDDI:
2964 return FDDI_K_ALEN;
2965 case ARPHRD_HIPPI:
2966 return HIPPI_ALEN;
2967 case ARPHRD_IEEE802:
2968 return FC_ALEN;
2969 case ARPHRD_ROSE:
2970 return ROSE_ADDR_LEN;
2971 case ARPHRD_NETROM:
2972 return AX25_ADDR_LEN;
2973 case ARPHRD_LOCALTLK:
2974 return LTALK_ALEN;
2975 default:
2976 return 0;
2977 }
2978}
2979
Arnd Bergmann50857e22009-11-06 22:52:32 -08002980static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2981 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002983 struct tun_file *tfile = file->private_data;
Kirill Tkhaif6637062018-05-08 19:21:34 +03002984 struct net *net = sock_net(&tfile->sk);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002985 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 void __user* argp = (void __user*)arg;
Nicolas Dichtel26d31922018-11-28 19:12:56 +01002987 unsigned int ifindex, carrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08002989 kuid_t owner;
2990 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002991 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002992 int vnet_hdr_sz;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002993 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002994 int ret;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02002995 bool do_notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Kirill Tkhaif2780d62018-02-14 16:40:14 +03002997 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
2998 (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08002999 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07003001 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00003002 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07003003 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003004 if (cmd == TUNGETFEATURES) {
3005 /* Currently this just means: "what IFF flags are valid?".
3006 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02003007 * TUNSETIFF.
3008 */
3009 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00003010 (unsigned int __user*)argp);
Kirill Tkhaif6637062018-05-08 19:21:34 +03003011 } else if (cmd == TUNSETQUEUE) {
Jason Wangcde8b152012-10-31 19:46:01 +00003012 return tun_set_queue(file, &ifr);
Kirill Tkhaif6637062018-05-08 19:21:34 +03003013 } else if (cmd == SIOCGSKNS) {
3014 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3015 return -EPERM;
3016 return open_related_ns(&net->ns, get_net_ns);
3017 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003018
Herbert Xu876bfd42009-08-06 14:22:44 +00003019 rtnl_lock();
3020
yuan linyu9484dc72017-09-23 22:36:52 +08003021 tun = tun_get(tfile);
Gao Feng0f16bc12016-10-25 22:26:09 +08003022 if (cmd == TUNSETIFF) {
3023 ret = -EEXIST;
3024 if (tun)
3025 goto unlock;
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 ifr.ifr_name[IFNAMSIZ-1] = '\0';
3028
Kirill Tkhaif2780d62018-02-14 16:40:14 +03003029 ret = tun_set_iff(net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Herbert Xu876bfd42009-08-06 14:22:44 +00003031 if (ret)
3032 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Arnd Bergmann50857e22009-11-06 22:52:32 -08003034 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00003035 ret = -EFAULT;
3036 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04003038 if (cmd == TUNSETIFINDEX) {
3039 ret = -EPERM;
3040 if (tun)
3041 goto unlock;
3042
3043 ret = -EFAULT;
3044 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
3045 goto unlock;
3046
3047 ret = 0;
3048 tfile->ifindex = ifindex;
3049 goto unlock;
3050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Herbert Xu876bfd42009-08-06 14:22:44 +00003052 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00003054 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Michal Kubecek34241702020-03-04 17:24:14 +01003056 netif_info(tun, drv, tun->dev, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Kirill Tkhai0c3e0e32019-03-20 12:16:42 +03003058 net = dev_net(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003059 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07003061 case TUNGETIFF:
Kirill Tkhai12132762019-03-20 12:16:53 +03003062 tun_get_iff(tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07003063
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04003064 if (tfile->detached)
3065 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04003066 if (!tfile->socket.sk->sk_filter)
3067 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04003068
Arnd Bergmann50857e22009-11-06 22:52:32 -08003069 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00003070 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07003071 break;
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 case TUNSETNOCSUM:
3074 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Michał Mirosław88255372011-04-19 06:13:10 +00003076 /* [unimplemented] */
Michal Kubecek34241702020-03-04 17:24:14 +01003077 netif_info(tun, drv, tun->dev, "ignored: set checksum %s\n",
3078 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 break;
3080
3081 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00003082 /* Disable/Enable persist mode. Keep an extra reference to the
3083 * module to prevent the module being unprobed.
3084 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003085 if (arg && !(tun->flags & IFF_PERSIST)) {
3086 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00003087 __module_get(THIS_MODULE);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003088 do_notify = true;
Jason Wangdd38bd82013-01-11 16:59:34 +00003089 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003090 if (!arg && (tun->flags & IFF_PERSIST)) {
3091 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00003092 module_put(THIS_MODULE);
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003093 do_notify = true;
Jason Wang54f968d2012-10-31 19:45:57 +00003094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Michal Kubecek34241702020-03-04 17:24:14 +01003096 netif_info(tun, drv, tun->dev, "persist %s\n",
3097 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 break;
3099
3100 case TUNSETOWNER:
3101 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08003102 owner = make_kuid(current_user_ns(), arg);
3103 if (!uid_valid(owner)) {
3104 ret = -EINVAL;
3105 break;
3106 }
3107 tun->owner = owner;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003108 do_notify = true;
Michal Kubecek34241702020-03-04 17:24:14 +01003109 netif_info(tun, drv, tun->dev, "owner set to %u\n",
3110 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 break;
3112
Guido Guenther8c644622007-07-02 22:50:25 -07003113 case TUNSETGROUP:
3114 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08003115 group = make_kgid(current_user_ns(), arg);
3116 if (!gid_valid(group)) {
3117 ret = -EINVAL;
3118 break;
3119 }
3120 tun->group = group;
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003121 do_notify = true;
Michal Kubecek34241702020-03-04 17:24:14 +01003122 netif_info(tun, drv, tun->dev, "group set to %u\n",
3123 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07003124 break;
3125
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003126 case TUNSETLINK:
3127 /* Only allow setting the type when the interface is down */
3128 if (tun->dev->flags & IFF_UP) {
Michal Kubecek34241702020-03-04 17:24:14 +01003129 netif_info(tun, drv, tun->dev,
3130 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07003131 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003132 } else {
Martin Schiller8e1e33f2020-11-18 07:39:19 +01003133 ret = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
3134 tun->dev);
3135 ret = notifier_to_errno(ret);
3136 if (ret) {
3137 netif_info(tun, drv, tun->dev,
3138 "Refused to change device type\n");
3139 break;
3140 }
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003141 tun->dev->type = (int) arg;
Phillip Pottercca8ea32021-04-06 18:45:54 +01003142 tun->dev->addr_len = tun_get_addr_len(tun->dev->type);
Michal Kubecek34241702020-03-04 17:24:14 +01003143 netif_info(tun, drv, tun->dev, "linktype set to %d\n",
3144 tun->dev->type);
Martin Schiller8e1e33f2020-11-18 07:39:19 +01003145 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
3146 tun->dev);
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003147 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00003148 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07003149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 case TUNSETDEBUG:
Michal Kubecek34241702020-03-04 17:24:14 +01003151 tun->msg_enable = (u32)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 break;
Michal Kubecek34241702020-03-04 17:24:14 +01003153
Rusty Russell5228ddc2008-07-03 03:46:16 -07003154 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00003155 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003156 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07003157
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003158 case TUNSETTXFILTER:
3159 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00003160 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003161 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00003162 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07003163 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003167 /* Get hw address */
Cong Wang3b23a322021-02-11 11:34:10 -08003168 dev_get_mac_address(&ifr.ifr_hwaddr, net, tun->dev->name);
Arnd Bergmann50857e22009-11-06 22:52:32 -08003169 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00003170 ret = -EFAULT;
3171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07003174 /* Set hw address */
Cong Wang3b23a322021-02-11 11:34:10 -08003175 ret = dev_set_mac_address_user(tun->dev, &ifr.ifr_hwaddr, NULL);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003176 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003177
3178 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00003179 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08003180 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
3181 ret = -EFAULT;
3182 break;
3183
3184 case TUNSETSNDBUF:
3185 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
3186 ret = -EFAULT;
3187 break;
3188 }
Craig Gallek931619222017-10-30 18:50:11 -04003189 if (sndbuf <= 0) {
3190 ret = -EINVAL;
3191 break;
3192 }
Herbert Xu33dccbb2009-02-05 21:25:32 -08003193
Jason Wangc8d68e62012-10-31 19:46:00 +00003194 tun->sndbuf = sndbuf;
3195 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08003196 break;
3197
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02003198 case TUNGETVNETHDRSZ:
3199 vnet_hdr_sz = tun->vnet_hdr_sz;
3200 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
3201 ret = -EFAULT;
3202 break;
3203
3204 case TUNSETVNETHDRSZ:
3205 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
3206 ret = -EFAULT;
3207 break;
3208 }
3209 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
3210 ret = -EINVAL;
3211 break;
3212 }
3213
3214 tun->vnet_hdr_sz = vnet_hdr_sz;
3215 break;
3216
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02003217 case TUNGETVNETLE:
3218 le = !!(tun->flags & TUN_VNET_LE);
3219 if (put_user(le, (int __user *)argp))
3220 ret = -EFAULT;
3221 break;
3222
3223 case TUNSETVNETLE:
3224 if (get_user(le, (int __user *)argp)) {
3225 ret = -EFAULT;
3226 break;
3227 }
3228 if (le)
3229 tun->flags |= TUN_VNET_LE;
3230 else
3231 tun->flags &= ~TUN_VNET_LE;
3232 break;
3233
Greg Kurz8b8e6582015-04-24 14:50:36 +02003234 case TUNGETVNETBE:
3235 ret = tun_get_vnet_be(tun, argp);
3236 break;
3237
3238 case TUNSETVNETBE:
3239 ret = tun_set_vnet_be(tun, argp);
3240 break;
3241
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003242 case TUNATTACHFILTER:
3243 /* Can be set only for TAPs */
3244 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003245 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003246 break;
3247 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00003248 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003249 break;
3250
Jason Wangc8d68e62012-10-31 19:46:00 +00003251 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003252 break;
3253
3254 case TUNDETACHFILTER:
3255 /* Can be set only for TAPs */
3256 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003257 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003258 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00003259 ret = 0;
3260 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00003261 break;
3262
Pavel Emelyanov76975e92013-08-21 14:32:39 +04003263 case TUNGETFILTER:
3264 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003265 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04003266 break;
3267 ret = -EFAULT;
3268 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
3269 break;
3270 ret = 0;
3271 break;
3272
Jason Wang96f84062017-12-04 17:31:23 +08003273 case TUNSETSTEERINGEBPF:
Jason Wangcd5681d2018-01-16 16:31:01 +08003274 ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
Jason Wang96f84062017-12-04 17:31:23 +08003275 break;
3276
Jason Wangaff3d702018-01-16 16:31:02 +08003277 case TUNSETFILTEREBPF:
3278 ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
3279 break;
3280
Nicolas Dichtel26d31922018-11-28 19:12:56 +01003281 case TUNSETCARRIER:
3282 ret = -EFAULT;
3283 if (copy_from_user(&carrier, argp, sizeof(carrier)))
3284 goto unlock;
3285
3286 ret = tun_net_change_carrier(tun->dev, (bool)carrier);
3287 break;
3288
Kirill Tkhai0c3e0e32019-03-20 12:16:42 +03003289 case TUNGETDEVNETNS:
3290 ret = -EPERM;
3291 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3292 goto unlock;
3293 ret = open_related_ns(&net->ns, get_net_ns);
3294 break;
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00003297 ret = -EINVAL;
3298 break;
Joe Perchesee289b62010-05-17 22:47:34 -07003299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Sabrina Dubroca83c1f362018-04-10 16:28:56 +02003301 if (do_notify)
3302 netdev_state_change(tun->dev);
3303
Herbert Xu876bfd42009-08-06 14:22:44 +00003304unlock:
3305 rtnl_unlock();
3306 if (tun)
3307 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003308 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Arnd Bergmann50857e22009-11-06 22:52:32 -08003311static long tun_chr_ioctl(struct file *file,
3312 unsigned int cmd, unsigned long arg)
3313{
3314 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
3315}
3316
3317#ifdef CONFIG_COMPAT
3318static long tun_chr_compat_ioctl(struct file *file,
3319 unsigned int cmd, unsigned long arg)
3320{
3321 switch (cmd) {
3322 case TUNSETIFF:
3323 case TUNGETIFF:
3324 case TUNSETTXFILTER:
3325 case TUNGETSNDBUF:
3326 case TUNSETSNDBUF:
3327 case SIOCGIFHWADDR:
3328 case SIOCSIFHWADDR:
3329 arg = (unsigned long)compat_ptr(arg);
3330 break;
3331 default:
3332 arg = (compat_ulong_t)arg;
3333 break;
3334 }
3335
3336 /*
3337 * compat_ifreq is shorter than ifreq, so we must not access beyond
3338 * the end of that structure. All fields that are used in this
3339 * driver are compatible though, we don't need to convert the
3340 * contents.
3341 */
3342 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
3343}
3344#endif /* CONFIG_COMPAT */
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static int tun_chr_fasync(int fd, struct file *file, int on)
3347{
Jason Wang54f968d2012-10-31 19:45:57 +00003348 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 int ret;
3350
Jason Wang54f968d2012-10-31 19:45:57 +00003351 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06003352 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (on) {
Eric W. Biederman01919132017-07-16 22:05:57 -05003355 __f_setown(file, task_pid(current), PIDTYPE_TGID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00003356 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003357 } else
Jason Wang54f968d2012-10-31 19:45:57 +00003358 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06003359 ret = 0;
3360out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06003361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362}
3363
3364static int tun_chr_open(struct inode *inode, struct file * file)
3365{
Eric W. Biederman140e807da2015-05-08 21:07:08 -05003366 struct net *net = current->nsproxy->net_ns;
Eric W. Biederman631ab462009-01-20 11:00:40 +00003367 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07003368
Eric W. Biederman140e807da2015-05-08 21:07:08 -05003369 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003370 &tun_proto, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00003371 if (!tfile)
3372 return -ENOMEM;
Jason Wangb196d882018-05-11 10:49:25 +08003373 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) {
3374 sk_free(&tfile->sk);
3375 return -ENOMEM;
3376 }
3377
Eric Dumazetc7256f52018-09-28 14:51:48 -07003378 mutex_init(&tfile->napi_mutex);
Monam Agarwalc9566742014-03-24 00:02:32 +05303379 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang54f968d2012-10-31 19:45:57 +00003380 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04003381 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00003382
Al Viro333f7902019-07-05 20:14:16 +01003383 init_waitqueue_head(&tfile->socket.wq.wait);
Jason Wang54f968d2012-10-31 19:45:57 +00003384
3385 tfile->socket.file = file;
3386 tfile->socket.ops = &tun_socket_ops;
3387
3388 sock_init_data(&tfile->socket, &tfile->sk);
Jason Wang54f968d2012-10-31 19:45:57 +00003389
3390 tfile->sk.sk_write_space = tun_sock_write_space;
3391 tfile->sk.sk_sndbuf = INT_MAX;
3392
Eric W. Biederman631ab462009-01-20 11:00:40 +00003393 file->private_data = tfile;
Jason Wang4008e972012-12-13 23:53:30 +00003394 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00003395
Jason Wang19a6afb2013-06-08 14:17:41 +08003396 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 return 0;
3399}
3400
3401static int tun_chr_close(struct inode *inode, struct file *file)
3402{
Eric W. Biederman631ab462009-01-20 11:00:40 +00003403 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Jason Wangc8d68e62012-10-31 19:46:00 +00003405 tun_detach(tfile, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407 return 0;
3408}
3409
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003410#ifdef CONFIG_PROC_FS
yuan linyu9484dc72017-09-23 22:36:52 +08003411static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003412{
yuan linyu9484dc72017-09-23 22:36:52 +08003413 struct tun_file *tfile = file->private_data;
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003414 struct tun_struct *tun;
3415 struct ifreq ifr;
3416
3417 memset(&ifr, 0, sizeof(ifr));
3418
3419 rtnl_lock();
yuan linyu9484dc72017-09-23 22:36:52 +08003420 tun = tun_get(tfile);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003421 if (tun)
Kirill Tkhai12132762019-03-20 12:16:53 +03003422 tun_get_iff(tun, &ifr);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003423 rtnl_unlock();
3424
3425 if (tun)
3426 tun_put(tun);
3427
Joe Perchesa3816ab2014-09-29 16:08:25 -07003428 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003429}
3430#endif
3431
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003432static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003433 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05003435 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04003436 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08003438 .unlocked_ioctl = tun_chr_ioctl,
3439#ifdef CONFIG_COMPAT
3440 .compat_ioctl = tun_chr_compat_ioctl,
3441#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 .open = tun_chr_open,
3443 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09003444 .fasync = tun_chr_fasync,
3445#ifdef CONFIG_PROC_FS
3446 .show_fdinfo = tun_chr_show_fdinfo,
3447#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448};
3449
3450static struct miscdevice tun_miscdev = {
3451 .minor = TUN_MINOR,
3452 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02003453 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455};
3456
3457/* ethtool interface */
3458
Chas Williams4e24f2d2018-06-02 17:49:53 -04003459static void tun_default_link_ksettings(struct net_device *dev,
3460 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
Philippe Reynes29ccc492017-03-11 22:03:50 +01003462 ethtool_link_ksettings_zero_link_mode(cmd, supported);
3463 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
3464 cmd->base.speed = SPEED_10;
3465 cmd->base.duplex = DUPLEX_FULL;
3466 cmd->base.port = PORT_TP;
3467 cmd->base.phy_address = 0;
3468 cmd->base.autoneg = AUTONEG_DISABLE;
Chas Williams4e24f2d2018-06-02 17:49:53 -04003469}
3470
3471static int tun_get_link_ksettings(struct net_device *dev,
3472 struct ethtool_link_ksettings *cmd)
3473{
3474 struct tun_struct *tun = netdev_priv(dev);
3475
3476 memcpy(cmd, &tun->link_ksettings, sizeof(*cmd));
3477 return 0;
3478}
3479
3480static int tun_set_link_ksettings(struct net_device *dev,
3481 const struct ethtool_link_ksettings *cmd)
3482{
3483 struct tun_struct *tun = netdev_priv(dev);
3484
3485 memcpy(&tun->link_ksettings, cmd, sizeof(*cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return 0;
3487}
3488
3489static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3490{
3491 struct tun_struct *tun = netdev_priv(dev);
3492
Rick Jones33a5ba12011-11-15 14:59:53 +00003493 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
3494 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003497 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00003498 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02003500 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00003501 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 break;
3503 }
3504}
3505
3506static u32 tun_get_msglevel(struct net_device *dev)
3507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 struct tun_struct *tun = netdev_priv(dev);
Michal Kubecek34241702020-03-04 17:24:14 +01003509
3510 return tun->msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
3513static void tun_set_msglevel(struct net_device *dev, u32 value)
3514{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 struct tun_struct *tun = netdev_priv(dev);
Michal Kubecek34241702020-03-04 17:24:14 +01003516
3517 tun->msg_enable = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Jason Wang5503fce2017-01-18 15:02:03 +08003520static int tun_get_coalesce(struct net_device *dev,
Yufeng Mof3ccfda12021-08-20 15:35:18 +08003521 struct ethtool_coalesce *ec,
3522 struct kernel_ethtool_coalesce *kernel_coal,
3523 struct netlink_ext_ack *extack)
Jason Wang5503fce2017-01-18 15:02:03 +08003524{
3525 struct tun_struct *tun = netdev_priv(dev);
3526
3527 ec->rx_max_coalesced_frames = tun->rx_batched;
3528
3529 return 0;
3530}
3531
3532static int tun_set_coalesce(struct net_device *dev,
Yufeng Mof3ccfda12021-08-20 15:35:18 +08003533 struct ethtool_coalesce *ec,
3534 struct kernel_ethtool_coalesce *kernel_coal,
3535 struct netlink_ext_ack *extack)
Jason Wang5503fce2017-01-18 15:02:03 +08003536{
3537 struct tun_struct *tun = netdev_priv(dev);
3538
3539 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
3540 tun->rx_batched = NAPI_POLL_WEIGHT;
3541 else
3542 tun->rx_batched = ec->rx_max_coalesced_frames;
3543
3544 return 0;
3545}
3546
Jeff Garzik7282d492006-09-13 14:30:00 -04003547static const struct ethtool_ops tun_ethtool_ops = {
Jakub Kicinskie5ad00b2020-03-05 17:05:58 -08003548 .supported_coalesce_params = ETHTOOL_COALESCE_RX_MAX_FRAMES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 .get_drvinfo = tun_get_drvinfo,
3550 .get_msglevel = tun_get_msglevel,
3551 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00003552 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02003553 .get_ts_info = ethtool_op_get_ts_info,
Jason Wang5503fce2017-01-18 15:02:03 +08003554 .get_coalesce = tun_get_coalesce,
3555 .set_coalesce = tun_set_coalesce,
Philippe Reynes29ccc492017-03-11 22:03:50 +01003556 .get_link_ksettings = tun_get_link_ksettings,
Chas Williams4e24f2d2018-06-02 17:49:53 -04003557 .set_link_ksettings = tun_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558};
3559
Jason Wang1576d982016-06-30 14:45:36 +08003560static int tun_queue_resize(struct tun_struct *tun)
3561{
3562 struct net_device *dev = tun->dev;
3563 struct tun_file *tfile;
Jason Wang5990a302018-01-04 11:14:27 +08003564 struct ptr_ring **rings;
Jason Wang1576d982016-06-30 14:45:36 +08003565 int n = tun->numqueues + tun->numdisabled;
3566 int ret, i;
3567
Jason Wang5990a302018-01-04 11:14:27 +08003568 rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
3569 if (!rings)
Jason Wang1576d982016-06-30 14:45:36 +08003570 return -ENOMEM;
3571
3572 for (i = 0; i < tun->numqueues; i++) {
3573 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wang5990a302018-01-04 11:14:27 +08003574 rings[i] = &tfile->tx_ring;
Jason Wang1576d982016-06-30 14:45:36 +08003575 }
3576 list_for_each_entry(tfile, &tun->disabled, next)
Jason Wang5990a302018-01-04 11:14:27 +08003577 rings[i++] = &tfile->tx_ring;
Jason Wang1576d982016-06-30 14:45:36 +08003578
Jason Wang5990a302018-01-04 11:14:27 +08003579 ret = ptr_ring_resize_multiple(rings, n,
3580 dev->tx_queue_len, GFP_KERNEL,
Jason Wangfc72d1d2018-01-04 11:14:28 +08003581 tun_ptr_free);
Jason Wang1576d982016-06-30 14:45:36 +08003582
Jason Wang5990a302018-01-04 11:14:27 +08003583 kfree(rings);
Jason Wang1576d982016-06-30 14:45:36 +08003584 return ret;
3585}
3586
3587static int tun_device_event(struct notifier_block *unused,
3588 unsigned long event, void *ptr)
3589{
3590 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3591 struct tun_struct *tun = netdev_priv(dev);
Fei Li72b319d2019-06-17 21:26:36 +08003592 int i;
Jason Wang1576d982016-06-30 14:45:36 +08003593
Craig Gallek86dfb4ac2016-07-06 18:44:20 -04003594 if (dev->rtnl_link_ops != &tun_link_ops)
3595 return NOTIFY_DONE;
3596
Jason Wang1576d982016-06-30 14:45:36 +08003597 switch (event) {
3598 case NETDEV_CHANGE_TX_QUEUE_LEN:
3599 if (tun_queue_resize(tun))
3600 return NOTIFY_BAD;
3601 break;
Fei Li72b319d2019-06-17 21:26:36 +08003602 case NETDEV_UP:
3603 for (i = 0; i < tun->numqueues; i++) {
3604 struct tun_file *tfile;
3605
3606 tfile = rtnl_dereference(tun->tfiles[i]);
3607 tfile->socket.sk->sk_write_space(tfile->socket.sk);
3608 }
3609 break;
Jason Wang1576d982016-06-30 14:45:36 +08003610 default:
3611 break;
3612 }
3613
3614 return NOTIFY_DONE;
3615}
3616
3617static struct notifier_block tun_notifier_block __read_mostly = {
3618 .notifier_call = tun_device_event,
3619};
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621static int __init tun_init(void)
3622{
3623 int ret = 0;
3624
Joe Perches6b8a66e2011-03-02 07:18:10 +00003625 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003627 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003628 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003629 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003630 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003631 }
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003634 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00003635 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003636 goto err_misc;
3637 }
Jason Wang1576d982016-06-30 14:45:36 +08003638
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003639 ret = register_netdevice_notifier(&tun_notifier_block);
3640 if (ret) {
3641 pr_err("Can't register netdevice notifier\n");
3642 goto err_notifier;
3643 }
3644
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003645 return 0;
Tonghao Zhang5edfbd32017-07-20 02:41:34 -07003646
3647err_notifier:
3648 misc_deregister(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07003649err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003650 rtnl_link_unregister(&tun_link_ops);
3651err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return ret;
3653}
3654
3655static void tun_cleanup(void)
3656{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003657 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08003658 rtnl_link_unregister(&tun_link_ops);
Jason Wang1576d982016-06-30 14:45:36 +08003659 unregister_netdevice_notifier(&tun_notifier_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660}
3661
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003662/* Get an underlying socket object from tun file. Returns error unless file is
3663 * attached to a device. The returned object works like a packet socket, it
3664 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
3665 * holding a reference to the file for as long as the socket is in use. */
3666struct socket *tun_get_socket(struct file *file)
3667{
Jason Wang6e914fc2012-10-31 19:45:58 +00003668 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003669 if (file->f_op != &tun_fops)
3670 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00003671 tfile = file->private_data;
3672 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003673 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00003674 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00003675}
3676EXPORT_SYMBOL_GPL(tun_get_socket);
3677
Jason Wang5990a302018-01-04 11:14:27 +08003678struct ptr_ring *tun_get_tx_ring(struct file *file)
Jason Wang83339c62017-05-17 12:14:41 +08003679{
3680 struct tun_file *tfile;
3681
3682 if (file->f_op != &tun_fops)
3683 return ERR_PTR(-EINVAL);
3684 tfile = file->private_data;
3685 if (!tfile)
3686 return ERR_PTR(-EBADFD);
Jason Wang5990a302018-01-04 11:14:27 +08003687 return &tfile->tx_ring;
Jason Wang83339c62017-05-17 12:14:41 +08003688}
Jason Wang5990a302018-01-04 11:14:27 +08003689EXPORT_SYMBOL_GPL(tun_get_tx_ring);
Jason Wang83339c62017-05-17 12:14:41 +08003690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691module_init(tun_init);
3692module_exit(tun_cleanup);
3693MODULE_DESCRIPTION(DRV_DESCRIPTION);
3694MODULE_AUTHOR(DRV_COPYRIGHT);
3695MODULE_LICENSE("GPL");
3696MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02003697MODULE_ALIAS("devname:net/tun");