blob: be196e89ab6cf0b54e26cfe3e03110e1cfe959c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16 */
17
18/*
19 * Changes:
20 *
Mike Kershawff4cc3a2005-09-01 17:40:05 -070021 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * Mark Smith <markzzzsmith@yahoo.com.au>
Joe Perches344dc8e2012-07-12 19:33:09 +000025 * Use eth_random_addr() for tap MAC address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
28 * Fixes in packet dropping, queue length setting and queue wakeup.
29 * Increased default tx queue length.
30 * Added ethtool API.
31 * Minor cleanups
32 *
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
35 */
36
Joe Perches6b8a66e2011-03-02 07:18:10 +000037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define DRV_NAME "tun"
40#define DRV_VERSION "1.6"
41#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
42#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/errno.h>
46#include <linux/kernel.h>
47#include <linux/major.h>
48#include <linux/slab.h>
49#include <linux/poll.h>
50#include <linux/fcntl.h>
51#include <linux/init.h>
52#include <linux/skbuff.h>
53#include <linux/netdevice.h>
54#include <linux/etherdevice.h>
55#include <linux/miscdevice.h>
56#include <linux/ethtool.h>
57#include <linux/rtnetlink.h>
Arnd Bergmann50857e22009-11-06 22:52:32 -080058#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/if.h>
60#include <linux/if_arp.h>
61#include <linux/if_ether.h>
62#include <linux/if_tun.h>
Jason Wang6680ec62013-07-25 13:00:33 +080063#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070065#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070066#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000067#include <linux/rcupdate.h>
Ben Hutchings5188cd42014-10-30 18:27:17 +000068#include <net/ipv6.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070069#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070070#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080071#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080072#include <net/sock.h>
Masatake YAMATO93e14b62014-01-29 16:43:31 +090073#include <linux/seq_file.h>
Herbert Xue0b46d02014-11-07 21:22:23 +080074#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/uaccess.h>
77
Rusty Russell14daa022008-04-12 18:48:58 -070078/* Uncomment to enable debugging */
79/* #define TUN_DEBUG 1 */
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#ifdef TUN_DEBUG
82static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070083
Joe Perches6b8a66e2011-03-02 07:18:10 +000084#define tun_debug(level, tun, fmt, args...) \
85do { \
86 if (tun->debug) \
87 netdev_printk(level, tun->dev, fmt, ##args); \
88} while (0)
89#define DBG1(level, fmt, args...) \
90do { \
91 if (debug == 2) \
92 printk(level fmt, ##args); \
93} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070094#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000095#define tun_debug(level, tun, fmt, args...) \
96do { \
97 if (0) \
98 netdev_printk(level, tun->dev, fmt, ##args); \
99} while (0)
100#define DBG1(level, fmt, args...) \
101do { \
102 if (0) \
103 printk(level fmt, ##args); \
104} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +0200107/* TUN device flags */
108
109/* IFF_ATTACH_QUEUE is never stored in device flags,
110 * overload it to mean fasync when stored there.
111 */
112#define TUN_FASYNC IFF_ATTACH_QUEUE
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200113/* High bits in flags field are unused. */
114#define TUN_VNET_LE 0x80000000
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +0200115
116#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200117 IFF_MULTI_QUEUE)
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000118#define GOODCOPY_LEN 128
119
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700120#define FLT_EXACT_COUNT 8
121struct tap_filter {
122 unsigned int count; /* Number of addrs. Zero means disabled */
123 u32 mask[2]; /* Mask of the hashed addrs */
124 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
125};
126
Pankaj Guptabaf71c52015-01-12 11:41:29 +0530127/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
128 * to max number of VCPUs in guest. */
129#define MAX_TAP_QUEUES 256
Jason Wangb8732fb2013-01-23 03:59:13 +0000130#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000131
Jason Wang96442e422012-10-31 19:46:02 +0000132#define TUN_FLOW_EXPIRE (3 * HZ)
133
Jason Wang54f968d2012-10-31 19:45:57 +0000134/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800135 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000136 * to serve as one transmit queue for tuntap device. The sock_fprog and
137 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000138 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000139 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000140 *
141 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000142 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000143 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000144 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000145struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000146 struct sock sk;
147 struct socket socket;
148 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000149 struct tun_struct __rcu *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000150 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000151 struct fasync_struct *fasync;
152 /* only used for fasnyc */
153 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400154 union {
155 u16 queue_index;
156 unsigned int ifindex;
157 };
Jason Wang4008e972012-12-13 23:53:30 +0000158 struct list_head next;
159 struct tun_struct *detached;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000160};
161
Jason Wang96442e422012-10-31 19:46:02 +0000162struct tun_flow_entry {
163 struct hlist_node hash_link;
164 struct rcu_head rcu;
165 struct tun_struct *tun;
166
167 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800168 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000169 int queue_index;
170 unsigned long updated;
171};
172
173#define TUN_NUM_FLOW_ENTRIES 1024
174
Jason Wang54f968d2012-10-31 19:45:57 +0000175/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000176 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000177 * file were attached to a persist device.
178 */
Rusty Russell14daa022008-04-12 18:48:58 -0700179struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000180 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
181 unsigned int numqueues;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700182 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800183 kuid_t owner;
184 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700185
Rusty Russell14daa022008-04-12 18:48:58 -0700186 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000187 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000188#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
Ben Hutchings3d0ad092014-10-30 18:27:12 +0000189 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200190
191 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;
Rusty Russell14daa022008-04-12 18:48:58 -0700197#ifdef TUN_DEBUG
198 int debug;
199#endif
Jason Wang96442e422012-10-31 19:46:02 +0000200 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000201 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
202 struct timer_list flow_gc_timer;
203 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000204 unsigned int numdisabled;
205 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000206 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000207 u32 flow_count;
Rusty Russell14daa022008-04-12 18:48:58 -0700208};
209
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300210static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
211{
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200212 return __virtio16_to_cpu(tun->flags & TUN_VNET_LE, val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300213}
214
215static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
216{
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +0200217 return __cpu_to_virtio16(tun->flags & TUN_VNET_LE, val);
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300218}
219
Jason Wang96442e422012-10-31 19:46:02 +0000220static inline u32 tun_hashfn(u32 rxhash)
221{
222 return rxhash & 0x3ff;
223}
224
225static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
226{
227 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000228
Sasha Levinb67bfe02013-02-27 17:06:00 -0800229 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000230 if (e->rxhash == rxhash)
231 return e;
232 }
233 return NULL;
234}
235
236static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
237 struct hlist_head *head,
238 u32 rxhash, u16 queue_index)
239{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000240 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
241
Jason Wang96442e422012-10-31 19:46:02 +0000242 if (e) {
243 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
244 rxhash, queue_index);
245 e->updated = jiffies;
246 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800247 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000248 e->queue_index = queue_index;
249 e->tun = tun;
250 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000251 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000252 }
253 return e;
254}
255
Jason Wang96442e422012-10-31 19:46:02 +0000256static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
257{
258 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
259 e->rxhash, e->queue_index);
Tom Herbert9bc88932013-12-22 18:54:32 +0800260 sock_rps_reset_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000261 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000262 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000263 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000264}
265
266static void tun_flow_flush(struct tun_struct *tun)
267{
268 int i;
269
270 spin_lock_bh(&tun->lock);
271 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
272 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800273 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000274
Sasha Levinb67bfe02013-02-27 17:06:00 -0800275 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000276 tun_flow_delete(tun, e);
277 }
278 spin_unlock_bh(&tun->lock);
279}
280
281static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
282{
283 int i;
284
285 spin_lock_bh(&tun->lock);
286 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
287 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800288 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000289
Sasha Levinb67bfe02013-02-27 17:06:00 -0800290 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000291 if (e->queue_index == queue_index)
292 tun_flow_delete(tun, e);
293 }
294 }
295 spin_unlock_bh(&tun->lock);
296}
297
298static void tun_flow_cleanup(unsigned long data)
299{
300 struct tun_struct *tun = (struct tun_struct *)data;
301 unsigned long delay = tun->ageing_time;
302 unsigned long next_timer = jiffies + delay;
303 unsigned long count = 0;
304 int i;
305
306 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
307
308 spin_lock_bh(&tun->lock);
309 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
310 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800311 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000312
Sasha Levinb67bfe02013-02-27 17:06:00 -0800313 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000314 unsigned long this_timer;
315 count++;
316 this_timer = e->updated + delay;
317 if (time_before_eq(this_timer, jiffies))
318 tun_flow_delete(tun, e);
319 else if (time_before(this_timer, next_timer))
320 next_timer = this_timer;
321 }
322 }
323
324 if (count)
325 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
326 spin_unlock_bh(&tun->lock);
327}
328
Eric Dumazet49974422012-12-12 19:22:57 +0000329static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000330 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000331{
332 struct hlist_head *head;
333 struct tun_flow_entry *e;
334 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000335 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000336
337 if (!rxhash)
338 return;
339 else
340 head = &tun->flows[tun_hashfn(rxhash)];
341
342 rcu_read_lock();
343
Jason Wang9e857222013-01-28 01:05:19 +0000344 /* We may get a very small possibility of OOO during switching, not
345 * worth to optimize.*/
346 if (tun->numqueues == 1 || tfile->detached)
Jason Wang96442e422012-10-31 19:46:02 +0000347 goto unlock;
348
349 e = tun_flow_find(head, rxhash);
350 if (likely(e)) {
351 /* TODO: keep queueing to old queue until it's empty? */
352 e->queue_index = queue_index;
353 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800354 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000355 } else {
356 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000357 if (!tun_flow_find(head, rxhash) &&
358 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000359 tun_flow_create(tun, head, rxhash, queue_index);
360
361 if (!timer_pending(&tun->flow_gc_timer))
362 mod_timer(&tun->flow_gc_timer,
363 round_jiffies_up(jiffies + delay));
364 spin_unlock_bh(&tun->lock);
365 }
366
367unlock:
368 rcu_read_unlock();
369}
370
Tom Herbert9bc88932013-12-22 18:54:32 +0800371/**
372 * Save the hash received in the stack receive path and update the
373 * flow_hash table accordingly.
374 */
375static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
376{
377 if (unlikely(e->rps_rxhash != hash)) {
378 sock_rps_reset_flow_hash(e->rps_rxhash);
379 e->rps_rxhash = hash;
380 }
381}
382
Jason Wangc8d68e62012-10-31 19:46:00 +0000383/* We try to identify a flow through its rxhash first. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800384 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000385 * the rxq based on the txq where the last packet of the flow comes. As
386 * the userspace application move between processors, we may get a
387 * different rxq no. here. If we could not get rxhash, then we would
388 * hope the rxq no. may help here.
389 */
Jason Wangf663dd92014-01-10 16:18:26 +0800390static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100391 void *accel_priv, select_queue_fallback_t fallback)
Jason Wangc8d68e62012-10-31 19:46:00 +0000392{
393 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000394 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000395 u32 txq = 0;
396 u32 numqueues = 0;
397
398 rcu_read_lock();
Jason Wang92bb73e2013-06-05 16:44:57 +0800399 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000400
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800401 txq = skb_get_hash(skb);
Jason Wangc8d68e62012-10-31 19:46:00 +0000402 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000403 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
Tom Herbert9bc88932013-12-22 18:54:32 +0800404 if (e) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800405 tun_flow_save_rps_rxhash(e, txq);
Zhi Yong Wufbe4d452014-01-02 13:24:28 +0800406 txq = e->queue_index;
Tom Herbert9bc88932013-12-22 18:54:32 +0800407 } else
Jason Wang96442e422012-10-31 19:46:02 +0000408 /* use multiply and shift instead of expensive divide */
409 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000410 } else if (likely(skb_rx_queue_recorded(skb))) {
411 txq = skb_get_rx_queue(skb);
412 while (unlikely(txq >= numqueues))
413 txq -= numqueues;
414 }
415
416 rcu_read_unlock();
417 return txq;
418}
419
Jason Wangcde8b152012-10-31 19:46:01 +0000420static inline bool tun_not_capable(struct tun_struct *tun)
421{
422 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000423 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000424
425 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
426 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000427 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000428}
429
Jason Wangc8d68e62012-10-31 19:46:00 +0000430static void tun_set_real_num_queues(struct tun_struct *tun)
431{
432 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
433 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
434}
435
Jason Wang4008e972012-12-13 23:53:30 +0000436static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
437{
438 tfile->detached = tun;
439 list_add_tail(&tfile->next, &tun->disabled);
440 ++tun->numdisabled;
441}
442
Jason Wangd32649d2012-12-18 11:00:27 +0800443static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000444{
445 struct tun_struct *tun = tfile->detached;
446
447 tfile->detached = NULL;
448 list_del_init(&tfile->next);
449 --tun->numdisabled;
450 return tun;
451}
452
Jason Wang4bfb0512013-09-05 17:53:59 +0800453static void tun_queue_purge(struct tun_file *tfile)
454{
455 skb_queue_purge(&tfile->sk.sk_receive_queue);
456 skb_queue_purge(&tfile->sk.sk_error_queue);
457}
458
Jason Wangc8d68e62012-10-31 19:46:00 +0000459static void __tun_detach(struct tun_file *tfile, bool clean)
460{
461 struct tun_file *ntfile;
462 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000463
Jason Wangb8deabd2013-01-11 16:59:32 +0000464 tun = rtnl_dereference(tfile->tun);
465
Jason Wang9e857222013-01-28 01:05:19 +0000466 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000467 u16 index = tfile->queue_index;
468 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000469
470 rcu_assign_pointer(tun->tfiles[index],
471 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000472 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000473 ntfile->queue_index = index;
474
475 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000476 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530477 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000478 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000479 } else
Jason Wang4008e972012-12-13 23:53:30 +0000480 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000481
482 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000483 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000484 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800485 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000486 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000487 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000488 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000489 sock_put(&tfile->sk);
490 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000491
492 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000493 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
494 netif_carrier_off(tun->dev);
495
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200496 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000497 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000498 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000499 }
Jason Wang4008e972012-12-13 23:53:30 +0000500
Jason Wangc8d68e62012-10-31 19:46:00 +0000501 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
502 &tfile->socket.flags));
503 sk_release_kernel(&tfile->sk);
504 }
505}
506
507static void tun_detach(struct tun_file *tfile, bool clean)
508{
509 rtnl_lock();
510 __tun_detach(tfile, clean);
511 rtnl_unlock();
512}
513
514static void tun_detach_all(struct net_device *dev)
515{
516 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000517 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000518 int i, n = tun->numqueues;
519
520 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000521 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000522 BUG_ON(!tfile);
Xi Wang9e641bd2014-05-16 15:11:48 -0700523 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530524 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000525 --tun->numqueues;
526 }
Jason Wang9e857222013-01-28 01:05:19 +0000527 list_for_each_entry(tfile, &tun->disabled, next) {
Xi Wang9e641bd2014-05-16 15:11:48 -0700528 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530529 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000530 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000531 BUG_ON(tun->numqueues != 0);
532
533 synchronize_net();
534 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000535 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000536 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800537 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000538 sock_put(&tfile->sk);
539 }
Jason Wang4008e972012-12-13 23:53:30 +0000540 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
541 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800542 tun_queue_purge(tfile);
Jason Wang4008e972012-12-13 23:53:30 +0000543 sock_put(&tfile->sk);
544 }
545 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000546
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200547 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000548 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000549}
550
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400551static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000552{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000553 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000554 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000555
Paul Moore5dbbaf22013-01-14 07:12:19 +0000556 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
557 if (err < 0)
558 goto out;
559
Eric W. Biederman38231b72009-01-20 11:02:28 +0000560 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000561 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000562 goto out;
563
564 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200565 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000566 goto out;
567
568 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000569 if (!tfile->detached &&
570 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000571 goto out;
572
573 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000574
stephen hemminger92d4ea62013-12-05 20:42:58 -0800575 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400576 if (!skip_filter && (tun->filter_attached == true)) {
Jason Wang54f968d2012-10-31 19:45:57 +0000577 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
578 if (!err)
579 goto out;
580 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000581 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000582 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000583 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000584 tun->numqueues++;
585
Jason Wang4008e972012-12-13 23:53:30 +0000586 if (tfile->detached)
587 tun_enable_queue(tfile);
588 else
589 sock_hold(&tfile->sk);
590
Jason Wangc8d68e62012-10-31 19:46:00 +0000591 tun_set_real_num_queues(tun);
592
Jason Wangc8d68e62012-10-31 19:46:00 +0000593 /* device is allowed to go away first, so no need to hold extra
594 * refcnt.
595 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000596
Eric W. Biederman38231b72009-01-20 11:02:28 +0000597out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000598 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000599}
600
Eric W. Biederman631ab462009-01-20 11:00:40 +0000601static struct tun_struct *__tun_get(struct tun_file *tfile)
602{
Jason Wang6e914fc2012-10-31 19:45:58 +0000603 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000604
Jason Wang6e914fc2012-10-31 19:45:58 +0000605 rcu_read_lock();
606 tun = rcu_dereference(tfile->tun);
607 if (tun)
608 dev_hold(tun->dev);
609 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000610
611 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000612}
613
614static struct tun_struct *tun_get(struct file *file)
615{
616 return __tun_get(file->private_data);
617}
618
619static void tun_put(struct tun_struct *tun)
620{
Jason Wang6e914fc2012-10-31 19:45:58 +0000621 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000622}
623
Joe Perches6b8a66e2011-03-02 07:18:10 +0000624/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700625static void addr_hash_set(u32 *mask, const u8 *addr)
626{
627 int n = ether_crc(ETH_ALEN, addr) >> 26;
628 mask[n >> 5] |= (1 << (n & 31));
629}
630
631static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
632{
633 int n = ether_crc(ETH_ALEN, addr) >> 26;
634 return mask[n >> 5] & (1 << (n & 31));
635}
636
637static int update_filter(struct tap_filter *filter, void __user *arg)
638{
639 struct { u8 u[ETH_ALEN]; } *addr;
640 struct tun_filter uf;
641 int err, alen, n, nexact;
642
643 if (copy_from_user(&uf, arg, sizeof(uf)))
644 return -EFAULT;
645
646 if (!uf.count) {
647 /* Disabled */
648 filter->count = 0;
649 return 0;
650 }
651
652 alen = ETH_ALEN * uf.count;
653 addr = kmalloc(alen, GFP_KERNEL);
654 if (!addr)
655 return -ENOMEM;
656
657 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
658 err = -EFAULT;
659 goto done;
660 }
661
662 /* The filter is updated without holding any locks. Which is
663 * perfectly safe. We disable it first and in the worst
664 * case we'll accept a few undesired packets. */
665 filter->count = 0;
666 wmb();
667
668 /* Use first set of addresses as an exact filter */
669 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
670 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
671
672 nexact = n;
673
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800674 /* Remaining multicast addresses are hashed,
675 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700676 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800677 for (; n < uf.count; n++) {
678 if (!is_multicast_ether_addr(addr[n].u)) {
679 err = 0; /* no filter */
680 goto done;
681 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700682 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800683 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700684
685 /* For ALLMULTI just set the mask to all ones.
686 * This overrides the mask populated above. */
687 if ((uf.flags & TUN_FLT_ALLMULTI))
688 memset(filter->mask, ~0, sizeof(filter->mask));
689
690 /* Now enable the filter */
691 wmb();
692 filter->count = nexact;
693
694 /* Return the number of exact filters */
695 err = nexact;
696
697done:
698 kfree(addr);
699 return err;
700}
701
702/* Returns: 0 - drop, !=0 - accept */
703static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
704{
705 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
706 * at this point. */
707 struct ethhdr *eh = (struct ethhdr *) skb->data;
708 int i;
709
710 /* Exact match */
711 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000712 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700713 return 1;
714
715 /* Inexact match (multicast only) */
716 if (is_multicast_ether_addr(eh->h_dest))
717 return addr_hash_test(filter->mask, eh->h_dest);
718
719 return 0;
720}
721
722/*
723 * Checks whether the packet is accepted or not.
724 * Returns: 0 - drop, !=0 - accept
725 */
726static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
727{
728 if (!filter->count)
729 return 1;
730
731 return run_filter(filter, skb);
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/* Network device part of the driver */
735
Jeff Garzik7282d492006-09-13 14:30:00 -0400736static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000738/* Net device detach from fd. */
739static void tun_net_uninit(struct net_device *dev)
740{
Jason Wangc8d68e62012-10-31 19:46:00 +0000741 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/* Net device open. */
745static int tun_net_open(struct net_device *dev)
746{
Jason Wangc8d68e62012-10-31 19:46:00 +0000747 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749}
750
751/* Net device close. */
752static int tun_net_close(struct net_device *dev)
753{
Jason Wangc8d68e62012-10-31 19:46:00 +0000754 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756}
757
758/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000759static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000762 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000763 struct tun_file *tfile;
Dominic Curranfa358642014-01-22 03:03:23 +0000764 u32 numqueues = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jason Wang6e914fc2012-10-31 19:45:58 +0000766 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000767 tfile = rcu_dereference(tun->tfiles[txq]);
Dominic Curranfa358642014-01-22 03:03:23 +0000768 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* Drop packet if interface is not attached */
Dominic Curranfa358642014-01-22 03:03:23 +0000771 if (txq >= numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto drop;
773
Dominic Curranfa358642014-01-22 03:03:23 +0000774 if (numqueues == 1) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800775 /* Select queue was not called for the skbuff, so we extract the
776 * RPS hash and save it into the flow_table here.
777 */
778 __u32 rxhash;
779
780 rxhash = skb_get_hash(skb);
781 if (rxhash) {
782 struct tun_flow_entry *e;
783 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
784 rxhash);
785 if (e)
786 tun_flow_save_rps_rxhash(e, rxhash);
787 }
788 }
789
Jason Wang6e914fc2012-10-31 19:45:58 +0000790 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
791
Jason Wangc8d68e62012-10-31 19:46:00 +0000792 BUG_ON(!tfile);
793
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700794 /* Drop if the filter does not like it.
795 * This is a noop if the filter is disabled.
796 * Filter can be enabled only for the TAP devices. */
797 if (!check_filter(&tun->txflt, skb))
798 goto drop;
799
Jason Wang54f968d2012-10-31 19:45:57 +0000800 if (tfile->socket.sk->sk_filter &&
801 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000802 goto drop;
803
Rami Rosen36fe8c092012-11-25 22:07:40 +0000804 /* Limit the number of packets queued by dividing txq length with the
Jason Wangc8d68e62012-10-31 19:46:00 +0000805 * number of queues.
806 */
Dominic Curranfa358642014-01-22 03:03:23 +0000807 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
808 >= dev->tx_queue_len)
Michael S. Tsirkin5d097102012-12-03 10:07:14 +0000809 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jason Wang7bf66302013-09-05 17:54:00 +0800811 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
812 goto drop;
813
Richard Cochraneda29772013-07-19 19:40:10 +0200814 if (skb->sk) {
815 sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
816 sw_tx_timestamp(skb);
817 }
818
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000819 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800820 * for indefinite time.
821 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000822 skb_orphan(skb);
823
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000824 nf_reset(skb);
825
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700826 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000827 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000830 if (tfile->flags & TUN_FASYNC)
831 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -0700832 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +0000833
834 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000835 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700838 dev->stats.tx_dropped++;
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000839 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000841 rcu_read_unlock();
Jason Wangbaeabab2014-11-18 13:20:41 +0800842 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700845static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700847 /*
848 * This callback is supposed to deal with mc filter in
849 * _rx_ path and has nothing to do with the _tx_ path.
850 * In rx path we always accept everything userspace gives us.
851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Ed Swierk4885a502007-09-16 12:21:38 -0700854#define MIN_MTU 68
855#define MAX_MTU 65535
856
857static int
858tun_net_change_mtu(struct net_device *dev, int new_mtu)
859{
860 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
861 return -EINVAL;
862 dev->mtu = new_mtu;
863 return 0;
864}
865
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000866static netdev_features_t tun_net_fix_features(struct net_device *dev,
867 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000868{
869 struct tun_struct *tun = netdev_priv(dev);
870
871 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
872}
Neil Hormanbebd0972011-06-15 05:25:01 +0000873#ifdef CONFIG_NET_POLL_CONTROLLER
874static void tun_poll_controller(struct net_device *dev)
875{
876 /*
877 * Tun only receives frames when:
878 * 1) the char device endpoint gets data from user space
879 * 2) the tun socket gets a sendmsg call from user space
stephen hemminger92d4ea62013-12-05 20:42:58 -0800880 * Since both of those are synchronous operations, we are guaranteed
Neil Hormanbebd0972011-06-15 05:25:01 +0000881 * never to have pending data when we poll for it
stephen hemminger92d4ea62013-12-05 20:42:58 -0800882 * so there is nothing to do here but return.
Neil Hormanbebd0972011-06-15 05:25:01 +0000883 * We need this though so netpoll recognizes us as an interface that
884 * supports polling, which enables bridge devices in virt setups to
885 * still use netconsole
886 */
887 return;
888}
889#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800890static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000891 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800892 .ndo_open = tun_net_open,
893 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800894 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800895 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000896 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000897 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000898#ifdef CONFIG_NET_POLL_CONTROLLER
899 .ndo_poll_controller = tun_poll_controller,
900#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800901};
902
903static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000904 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800905 .ndo_open = tun_net_open,
906 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800907 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800908 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000909 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000910 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800911 .ndo_set_mac_address = eth_mac_addr,
912 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000913 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000914#ifdef CONFIG_NET_POLL_CONTROLLER
915 .ndo_poll_controller = tun_poll_controller,
916#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800917};
918
Pavel Emelyanov944a1372013-06-11 17:01:08 +0400919static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +0000920{
921 int i;
922
Jason Wang96442e422012-10-31 19:46:02 +0000923 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
924 INIT_HLIST_HEAD(&tun->flows[i]);
925
926 tun->ageing_time = TUN_FLOW_EXPIRE;
927 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
928 mod_timer(&tun->flow_gc_timer,
929 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +0000930}
931
932static void tun_flow_uninit(struct tun_struct *tun)
933{
934 del_timer_sync(&tun->flow_gc_timer);
935 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +0000936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/* Initialize net device. */
939static void tun_net_init(struct net_device *dev)
940{
941 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200944 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800945 dev->netdev_ops = &tun_netdev_ops;
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /* Point-to-Point TUN Device */
948 dev->hard_header_len = 0;
949 dev->addr_len = 0;
950 dev->mtu = 1500;
951
952 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400953 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
955 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
956 break;
957
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200958 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800959 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700961 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000962 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +0000963 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000965 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
968 break;
969 }
970}
971
972/* Character device part */
973
974/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000975static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400976{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000977 struct tun_file *tfile = file->private_data;
978 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000979 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800980 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000983 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jason Wang54f968d2012-10-31 19:45:57 +0000985 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000986
Joe Perches6b8a66e2011-03-02 07:18:10 +0000987 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Xi Wang9e641bd2014-05-16 15:11:48 -0700989 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400990
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000991 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 mask |= POLLIN | POLLRDNORM;
993
Herbert Xu33dccbb2009-02-05 21:25:32 -0800994 if (sock_writeable(sk) ||
995 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
996 sock_writeable(sk)))
997 mask |= POLLOUT | POLLWRNORM;
998
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000999 if (tun->dev->reg_state != NETREG_REGISTERED)
1000 mask = POLLERR;
1001
Eric W. Biederman631ab462009-01-20 11:00:40 +00001002 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return mask;
1004}
1005
Rusty Russellf42157c2008-08-15 15:15:10 -07001006/* prepad is the amount to reserve at front. len is length after that.
1007 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001008static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001009 size_t prepad, size_t len,
1010 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001011{
Jason Wang54f968d2012-10-31 19:45:57 +00001012 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001013 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001014 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001015
1016 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001017 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001018 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001019
Herbert Xu33dccbb2009-02-05 21:25:32 -08001020 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001021 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001022 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001023 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001024
1025 skb_reserve(skb, prepad);
1026 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001027 skb->data_len = len - linear;
1028 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001029
1030 return skb;
1031}
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001034static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Al Virof5ff53b2014-06-19 15:36:49 -04001035 void *msg_control, struct iov_iter *from,
1036 int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Harvey Harrison09640e632009-02-01 00:45:17 -08001038 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct sk_buff *skb;
Al Virof5ff53b2014-06-19 15:36:49 -04001040 size_t total_len = iov_iter_count(from);
Jason Wang3dd5c332013-07-10 13:43:27 +08001041 size_t len = total_len, align = NET_SKB_PAD, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001042 struct virtio_net_hdr gso = { 0 };
Jason Wang96f8d9e2013-11-13 14:00:39 +08001043 int good_linear;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001044 int copylen;
1045 bool zerocopy = false;
1046 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001047 u32 rxhash;
Al Virof5ff53b2014-06-19 15:36:49 -04001048 ssize_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001050 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001051 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001053 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Al Virof5ff53b2014-06-19 15:36:49 -04001055 n = copy_from_iter(&pi, sizeof(pi), from);
1056 if (n != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return -EFAULT;
1058 }
1059
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001060 if (tun->flags & IFF_VNET_HDR) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001061 if (len < tun->vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001062 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001063 len -= tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001064
Al Virof5ff53b2014-06-19 15:36:49 -04001065 n = copy_from_iter(&gso, sizeof(gso), from);
1066 if (n != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07001067 return -EFAULT;
1068
Herbert Xu49091222009-06-08 00:20:01 -07001069 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001070 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1071 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 -07001072
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001073 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001074 return -EINVAL;
Herbert Xud8febb72014-11-30 18:03:31 +08001075 iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001076 }
1077
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001078 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001079 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001080 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001081 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001082 return -EINVAL;
1083 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001084
Jason Wang96f8d9e2013-11-13 14:00:39 +08001085 good_linear = SKB_MAX_HEAD(align);
1086
Jason Wang88529172013-07-18 10:55:15 +08001087 if (msg_control) {
Al Virof5ff53b2014-06-19 15:36:49 -04001088 struct iov_iter i = *from;
1089
Jason Wang88529172013-07-18 10:55:15 +08001090 /* There are 256 bytes to be copied in skb, so there is
1091 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001092 * The rest of the buffer is mapped from userspace.
1093 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001094 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001095 if (copylen > good_linear)
1096 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001097 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -04001098 iov_iter_advance(&i, copylen);
1099 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wang88529172013-07-18 10:55:15 +08001100 zerocopy = true;
1101 }
1102
1103 if (!zerocopy) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001104 copylen = len;
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001105 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
Jason Wang96f8d9e2013-11-13 14:00:39 +08001106 linear = good_linear;
1107 else
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001108 linear = tun16_to_cpu(tun, gso.hdr_len);
Jason Wang3dd5c332013-07-10 13:43:27 +08001109 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001110
Jason Wang3dd5c332013-07-10 13:43:27 +08001111 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001112 if (IS_ERR(skb)) {
1113 if (PTR_ERR(skb) != -EAGAIN)
1114 tun->dev->stats.rx_dropped++;
1115 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001118 if (zerocopy)
Al Virof5ff53b2014-06-19 15:36:49 -04001119 err = zerocopy_sg_from_iter(skb, from);
Jason Wang88529172013-07-18 10:55:15 +08001120 else {
Al Virof5ff53b2014-06-19 15:36:49 -04001121 err = skb_copy_datagram_from_iter(skb, 0, from, len);
Jason Wang88529172013-07-18 10:55:15 +08001122 if (!err && msg_control) {
1123 struct ubuf_info *uarg = msg_control;
1124 uarg->callback(uarg, false);
1125 }
1126 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001127
1128 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001129 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -08001130 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -08001132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Rusty Russellf43798c2008-07-03 03:48:02 -07001134 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001135 if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
1136 tun16_to_cpu(tun, gso.csum_offset))) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001137 tun->dev->stats.rx_frame_errors++;
1138 kfree_skb(skb);
1139 return -EINVAL;
1140 }
Michał Mirosław88255372011-04-19 06:13:10 +00001141 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001144 case IFF_TUN:
1145 if (tun->flags & IFF_NO_PI) {
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001146 switch (skb->data[0] & 0xf0) {
1147 case 0x40:
1148 pi.proto = htons(ETH_P_IP);
1149 break;
1150 case 0x60:
1151 pi.proto = htons(ETH_P_IPV6);
1152 break;
1153 default:
1154 tun->dev->stats.rx_dropped++;
1155 kfree_skb(skb);
1156 return -EINVAL;
1157 }
1158 }
1159
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001160 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001162 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001164 case IFF_TAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 skb->protocol = eth_type_trans(skb, tun->dev);
1166 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Ben Hutchings5188cd42014-10-30 18:27:17 +00001169 skb_reset_network_header(skb);
1170
Rusty Russellf43798c2008-07-03 03:48:02 -07001171 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1172 pr_debug("GSO!\n");
1173 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1174 case VIRTIO_NET_HDR_GSO_TCPV4:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001175 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Rusty Russellf43798c2008-07-03 03:48:02 -07001176 break;
1177 case VIRTIO_NET_HDR_GSO_TCPV6:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001178 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Rusty Russellf43798c2008-07-03 03:48:02 -07001179 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001180 case VIRTIO_NET_HDR_GSO_UDP:
Ben Hutchings3d0ad092014-10-30 18:27:12 +00001181 {
1182 static bool warned;
1183
1184 if (!warned) {
1185 warned = true;
1186 netdev_warn(tun->dev,
1187 "%s: using disabled UFO feature; please fix this program\n",
1188 current->comm);
1189 }
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001190 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Ben Hutchings5188cd42014-10-30 18:27:17 +00001191 if (skb->protocol == htons(ETH_P_IPV6))
1192 ipv6_proxy_select_ident(skb);
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001193 break;
Ben Hutchings3d0ad092014-10-30 18:27:12 +00001194 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001195 default:
1196 tun->dev->stats.rx_frame_errors++;
1197 kfree_skb(skb);
1198 return -EINVAL;
1199 }
1200
1201 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001202 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Rusty Russellf43798c2008-07-03 03:48:02 -07001203
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001204 skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
Rusty Russellf43798c2008-07-03 03:48:02 -07001205 if (skb_shinfo(skb)->gso_size == 0) {
1206 tun->dev->stats.rx_frame_errors++;
1207 kfree_skb(skb);
1208 return -EINVAL;
1209 }
1210
1211 /* Header must be checked, and gso_segs computed. */
1212 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1213 skb_shinfo(skb)->gso_segs = 0;
1214 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001215
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001216 /* copy skb_ubuf_info for callback when skb has no error */
1217 if (zerocopy) {
1218 skb_shinfo(skb)->destructor_arg = msg_control;
1219 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001220 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001221 }
1222
Jason Wang40893fd2013-03-26 23:11:22 +00001223 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001224
Tom Herbert3958afa1b2013-12-15 22:12:06 -08001225 rxhash = skb_get_hash(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001227
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001228 tun->dev->stats.rx_packets++;
1229 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Jason Wang9e857222013-01-28 01:05:19 +00001231 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001232 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Al Virof5ff53b2014-06-19 15:36:49 -04001235static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001237 struct file *file = iocb->ki_filp;
Herbert Xuab46d7792009-02-14 20:46:39 -08001238 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001239 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001240 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (!tun)
1243 return -EBADFD;
1244
Al Virof5ff53b2014-06-19 15:36:49 -04001245 result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001246
1247 tun_put(tun);
1248 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001252static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001253 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001254 struct sk_buff *skb,
Herbert Xue0b46d02014-11-07 21:22:23 +08001255 struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 struct tun_pi pi = { 0, skb->protocol };
Herbert Xue0b46d02014-11-07 21:22:23 +08001258 ssize_t total;
Jason Wang8c847d22014-11-13 16:54:14 +08001259 int vlan_offset = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001260 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001261 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001262
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001263 if (skb_vlan_tag_present(skb))
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001264 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001266 if (tun->flags & IFF_VNET_HDR)
Herbert Xu2eb783c2014-11-03 04:30:14 +08001267 vnet_hdr_sz = tun->vnet_hdr_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Herbert Xue0b46d02014-11-07 21:22:23 +08001269 total = skb->len + vlan_hlen + vnet_hdr_sz;
1270
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001271 if (!(tun->flags & IFF_NO_PI)) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001272 if (iov_iter_count(iter) < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -EINVAL;
1274
Herbert Xue0b46d02014-11-07 21:22:23 +08001275 total += sizeof(pi);
1276 if (iov_iter_count(iter) < total) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Packet will be striped */
1278 pi.flags |= TUN_PKT_STRIP;
1279 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001280
Herbert Xue0b46d02014-11-07 21:22:23 +08001281 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -EFAULT;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Herbert Xu2eb783c2014-11-03 04:30:14 +08001285 if (vnet_hdr_sz) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001286 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Herbert Xue0b46d02014-11-07 21:22:23 +08001287 if (iov_iter_count(iter) < vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001288 return -EINVAL;
1289
1290 if (skb_is_gso(skb)) {
1291 struct skb_shared_info *sinfo = skb_shinfo(skb);
1292
1293 /* This is a hint as to how much should be linear. */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001294 gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
1295 gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
Rusty Russellf43798c2008-07-03 03:48:02 -07001296 if (sinfo->gso_type & SKB_GSO_TCPV4)
1297 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1298 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1299 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001300 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001301 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001302 "0x%x, gso_size %d, hdr_len %d\n",
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001303 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1304 tun16_to_cpu(tun, gso.hdr_len));
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001305 print_hex_dump(KERN_ERR, "tun: ",
1306 DUMP_PREFIX_NONE,
1307 16, 1, skb->head,
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001308 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001309 WARN_ON_ONCE(1);
1310 return -EINVAL;
1311 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001312 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1313 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1314 } else
1315 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1316
1317 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1318 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001319 gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
1320 vlan_hlen);
1321 gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
Jason Wang10a8d942011-06-10 00:56:17 +00001322 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1323 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001324 } /* else everything is zero */
1325
Herbert Xue0b46d02014-11-07 21:22:23 +08001326 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
Rusty Russellf43798c2008-07-03 03:48:02 -07001327 return -EFAULT;
Jason Wang8c847d22014-11-13 16:54:14 +08001328
1329 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
Rusty Russellf43798c2008-07-03 03:48:02 -07001330 }
1331
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001332 if (vlan_hlen) {
Herbert Xue0b46d02014-11-07 21:22:23 +08001333 int ret;
Jason Wang6680ec62013-07-25 13:00:33 +08001334 struct {
1335 __be16 h_vlan_proto;
1336 __be16 h_vlan_TCI;
1337 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Jason Wang6680ec62013-07-25 13:00:33 +08001339 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001340 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Jason Wang6680ec62013-07-25 13:00:33 +08001342 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08001343
Herbert Xue0b46d02014-11-07 21:22:23 +08001344 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1345 if (ret || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001346 goto done;
1347
Herbert Xue0b46d02014-11-07 21:22:23 +08001348 ret = copy_to_iter(&veth, sizeof(veth), iter);
1349 if (ret != sizeof(veth) || !iov_iter_count(iter))
Jason Wang6680ec62013-07-25 13:00:33 +08001350 goto done;
1351 }
1352
Herbert Xue0b46d02014-11-07 21:22:23 +08001353 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
Jason Wang6680ec62013-07-25 13:00:33 +08001354
1355done:
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001356 tun->dev->stats.tx_packets++;
Herbert Xue0b46d02014-11-07 21:22:23 +08001357 tun->dev->stats.tx_bytes += skb->len + vlan_hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 return total;
1360}
1361
Jason Wang54f968d2012-10-31 19:45:57 +00001362static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Al Viro9b067032014-11-07 13:52:07 -05001363 struct iov_iter *to,
1364 int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct sk_buff *skb;
Al Viro9b067032014-11-07 13:52:07 -05001367 ssize_t ret;
Xi Wang9e641bd2014-05-16 15:11:48 -07001368 int peeked, err, off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Rami Rosen3872baf2012-11-25 22:07:41 +00001370 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Al Viro9b067032014-11-07 13:52:07 -05001372 if (!iov_iter_count(to))
1373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Xi Wang9e641bd2014-05-16 15:11:48 -07001375 if (tun->dev->reg_state != NETREG_REGISTERED)
1376 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Xi Wang9e641bd2014-05-16 15:11:48 -07001378 /* Read frames from queue */
1379 skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
1380 &peeked, &off, &err);
Herbert Xue0b46d02014-11-07 21:22:23 +08001381 if (!skb)
Alex Gartrell957f0942014-12-25 23:22:49 -08001382 return err;
Herbert Xue0b46d02014-11-07 21:22:23 +08001383
Al Viro9b067032014-11-07 13:52:07 -05001384 ret = tun_put_user(tun, tfile, skb, to);
Jason Wangf51a5e82014-12-01 16:53:15 +08001385 if (unlikely(ret < 0))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001386 kfree_skb(skb);
Jason Wangf51a5e82014-12-01 16:53:15 +08001387 else
1388 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001390 return ret;
1391}
1392
Al Viro9b067032014-11-07 13:52:07 -05001393static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001394{
1395 struct file *file = iocb->ki_filp;
1396 struct tun_file *tfile = file->private_data;
1397 struct tun_struct *tun = __tun_get(tfile);
Al Viro9b067032014-11-07 13:52:07 -05001398 ssize_t len = iov_iter_count(to), ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001399
1400 if (!tun)
1401 return -EBADFD;
Al Viro9b067032014-11-07 13:52:07 -05001402 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK);
David S. Miller42404c02013-12-10 22:05:45 -05001403 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08001404 if (ret > 0)
1405 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001406 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return ret;
1408}
1409
Jason Wang96442e422012-10-31 19:46:02 +00001410static void tun_free_netdev(struct net_device *dev)
1411{
1412 struct tun_struct *tun = netdev_priv(dev);
1413
Jason Wang4008e972012-12-13 23:53:30 +00001414 BUG_ON(!(list_empty(&tun->disabled)));
Jason Wang96442e422012-10-31 19:46:02 +00001415 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001416 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001417 free_netdev(dev);
1418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420static void tun_setup(struct net_device *dev)
1421{
1422 struct tun_struct *tun = netdev_priv(dev);
1423
Eric W. Biederman0625c882012-02-07 16:48:55 -08001424 tun->owner = INVALID_UID;
1425 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang96442e422012-10-31 19:46:02 +00001428 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001431/* Trivial set of netlink ops to allow deleting tun or tap
1432 * device with netlink.
1433 */
1434static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1435{
1436 return -EINVAL;
1437}
1438
1439static struct rtnl_link_ops tun_link_ops __read_mostly = {
1440 .kind = DRV_NAME,
1441 .priv_size = sizeof(struct tun_struct),
1442 .setup = tun_setup,
1443 .validate = tun_validate,
1444};
1445
Herbert Xu33dccbb2009-02-05 21:25:32 -08001446static void tun_sock_write_space(struct sock *sk)
1447{
Jason Wang54f968d2012-10-31 19:45:57 +00001448 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001449 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001450
1451 if (!sock_writeable(sk))
1452 return;
1453
Herbert Xu33dccbb2009-02-05 21:25:32 -08001454 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1455 return;
1456
Eric Dumazet43815482010-04-29 11:01:49 +00001457 wqueue = sk_sleep(sk);
1458 if (wqueue && waitqueue_active(wqueue))
1459 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001460 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001461
Jason Wang54f968d2012-10-31 19:45:57 +00001462 tfile = container_of(sk, struct tun_file, sk);
1463 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001464}
1465
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001466static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1467 struct msghdr *m, size_t total_len)
1468{
Jason Wang54f968d2012-10-31 19:45:57 +00001469 int ret;
1470 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1471 struct tun_struct *tun = __tun_get(tfile);
1472
1473 if (!tun)
1474 return -EBADFD;
Al Virof5ff53b2014-06-19 15:36:49 -04001475
Al Viroc0371da2014-11-24 10:42:55 -05001476 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04001477 m->msg_flags & MSG_DONTWAIT);
Jason Wang54f968d2012-10-31 19:45:57 +00001478 tun_put(tun);
1479 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001480}
1481
1482static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1483 struct msghdr *m, size_t total_len,
1484 int flags)
1485{
Jason Wang54f968d2012-10-31 19:45:57 +00001486 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1487 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001488 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001489
1490 if (!tun)
1491 return -EBADFD;
1492
Richard Cochraneda29772013-07-19 19:40:10 +02001493 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00001494 ret = -EINVAL;
1495 goto out;
1496 }
Richard Cochraneda29772013-07-19 19:40:10 +02001497 if (flags & MSG_ERRQUEUE) {
1498 ret = sock_recv_errqueue(sock->sk, m, total_len,
1499 SOL_PACKET, TUN_TX_TIMESTAMP);
1500 goto out;
1501 }
Al Viroc0371da2014-11-24 10:42:55 -05001502 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT);
Alex Gartrell87897932014-12-25 23:05:03 -08001503 if (ret > (ssize_t)total_len) {
David S. Miller42404c02013-12-10 22:05:45 -05001504 m->msg_flags |= MSG_TRUNC;
1505 ret = flags & MSG_TRUNC ? ret : total_len;
1506 }
Gao feng3811ae72013-04-24 21:59:23 +00001507out:
Jason Wang54f968d2012-10-31 19:45:57 +00001508 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001509 return ret;
1510}
1511
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001512static int tun_release(struct socket *sock)
1513{
1514 if (sock->sk)
1515 sock_put(sock->sk);
1516 return 0;
1517}
1518
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001519/* Ops structure to mimic raw sockets with tun */
1520static const struct proto_ops tun_socket_ops = {
1521 .sendmsg = tun_sendmsg,
1522 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001523 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001524};
1525
Herbert Xu33dccbb2009-02-05 21:25:32 -08001526static struct proto tun_proto = {
1527 .name = "tun",
1528 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001529 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001530};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001531
David Woodhouse980c9e82009-05-09 22:54:21 -07001532static int tun_flags(struct tun_struct *tun)
1533{
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02001534 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07001535}
1536
1537static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1538 char *buf)
1539{
1540 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1541 return sprintf(buf, "0x%x\n", tun_flags(tun));
1542}
1543
1544static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1545 char *buf)
1546{
1547 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001548 return uid_valid(tun->owner)?
1549 sprintf(buf, "%u\n",
1550 from_kuid_munged(current_user_ns(), tun->owner)):
1551 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001552}
1553
1554static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1555 char *buf)
1556{
1557 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001558 return gid_valid(tun->group) ?
1559 sprintf(buf, "%u\n",
1560 from_kgid_munged(current_user_ns(), tun->group)):
1561 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001562}
1563
1564static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1565static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1566static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1567
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001568static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001571 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 struct net_device *dev;
1573 int err;
1574
Jason Wang7c0c3b12013-01-11 16:59:33 +00001575 if (tfile->detached)
1576 return -EINVAL;
1577
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001578 dev = __dev_get_by_name(net, ifr->ifr_name);
1579 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001580 if (ifr->ifr_flags & IFF_TUN_EXCL)
1581 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001582 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1583 tun = netdev_priv(dev);
1584 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1585 tun = netdev_priv(dev);
1586 else
1587 return -EINVAL;
1588
Jason Wang8e6d91a2013-05-28 18:32:11 +00001589 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001590 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00001591 return -EINVAL;
1592
Jason Wangcde8b152012-10-31 19:46:01 +00001593 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001594 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001595 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001596 if (err < 0)
1597 return err;
1598
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001599 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001600 if (err < 0)
1601 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001602
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001603 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00001604 (tun->numqueues + tun->numdisabled > 1)) {
1605 /* One or more queue has already been attached, no need
1606 * to initialize the device again.
1607 */
1608 return 0;
1609 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 else {
1612 char *name;
1613 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001614 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1615 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001617 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001618 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001619 err = security_tun_dev_create();
1620 if (err < 0)
1621 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 /* Set dev type */
1624 if (ifr->ifr_flags & IFF_TUN) {
1625 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001626 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 name = "tun%d";
1628 } else if (ifr->ifr_flags & IFF_TAP) {
1629 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001630 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001632 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001633 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (*ifr->ifr_name)
1636 name = ifr->ifr_name;
1637
Jason Wangc8d68e62012-10-31 19:46:00 +00001638 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02001639 NET_NAME_UNKNOWN, tun_setup, queues,
1640 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (!dev)
1643 return -ENOMEM;
1644
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001645 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001646 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001647 dev->ifindex = tfile->ifindex;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 tun = netdev_priv(dev);
1650 tun->dev = dev;
1651 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001652 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001653 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Jason Wang54f968d2012-10-31 19:45:57 +00001655 tun->filter_attached = false;
1656 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001657
Jason Wang96442e422012-10-31 19:46:02 +00001658 spin_lock_init(&tun->lock);
1659
Paul Moore5dbbaf22013-01-14 07:12:19 +00001660 err = security_tun_dev_alloc_security(&tun->security);
1661 if (err < 0)
1662 goto err_free_dev;
Paul Moore2b980db2009-08-28 18:12:43 -04001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001665 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001666
Michał Mirosław88255372011-04-19 06:13:10 +00001667 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08001668 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
1669 NETIF_F_HW_VLAN_STAG_TX;
Michał Mirosław88255372011-04-19 06:13:10 +00001670 dev->features = dev->hw_features;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09001671 dev->vlan_features = dev->features &
1672 ~(NETIF_F_HW_VLAN_CTAG_TX |
1673 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00001674
Jason Wang4008e972012-12-13 23:53:30 +00001675 INIT_LIST_HEAD(&tun->disabled);
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001676 err = tun_attach(tun, file, false);
Jason Wangeb0fb362012-12-02 17:19:45 +00001677 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001678 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 err = register_netdevice(tun->dev);
1681 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001682 goto err_detach;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001683
David Woodhouse980c9e82009-05-09 22:54:21 -07001684 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1685 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1686 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001687 pr_err("Failed to create tun sysfs files\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00001690 netif_carrier_on(tun->dev);
1691
Joe Perches6b8a66e2011-03-02 07:18:10 +00001692 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02001694 tun->flags = (tun->flags & ~TUN_FEATURES) |
1695 (ifr->ifr_flags & TUN_FEATURES);
Jason Wangc8d68e62012-10-31 19:46:00 +00001696
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001697 /* Make sure persistent devices do not get stuck in
1698 * xoff state.
1699 */
1700 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001701 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 strcpy(ifr->ifr_name, tun->dev->name);
1704 return 0;
1705
Jason Wang662ca432013-09-11 18:09:48 +08001706err_detach:
1707 tun_detach_all(dev);
1708err_free_flow:
1709 tun_flow_uninit(tun);
1710 security_tun_dev_free_security(tun->security);
1711err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return err;
1714}
1715
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001716static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00001717 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001718{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001719 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001720
1721 strcpy(ifr->ifr_name, tun->dev->name);
1722
David Woodhouse980c9e82009-05-09 22:54:21 -07001723 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001724
Mark McLoughline3b99552008-08-15 15:09:56 -07001725}
1726
Rusty Russell5228ddc2008-07-03 03:46:16 -07001727/* This is like a cut-down ethtool ops, except done via tun fd so no
1728 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001729static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001730{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001731 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001732
1733 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001734 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001735 arg &= ~TUN_F_CSUM;
1736
1737 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1738 if (arg & TUN_F_TSO_ECN) {
1739 features |= NETIF_F_TSO_ECN;
1740 arg &= ~TUN_F_TSO_ECN;
1741 }
1742 if (arg & TUN_F_TSO4)
1743 features |= NETIF_F_TSO;
1744 if (arg & TUN_F_TSO6)
1745 features |= NETIF_F_TSO6;
1746 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1747 }
1748 }
1749
1750 /* This gives the user a way to test for new features in future by
1751 * trying to set them. */
1752 if (arg)
1753 return -EINVAL;
1754
Michał Mirosław88255372011-04-19 06:13:10 +00001755 tun->set_features = features;
1756 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001757
1758 return 0;
1759}
1760
Jason Wangc8d68e62012-10-31 19:46:00 +00001761static void tun_detach_filter(struct tun_struct *tun, int n)
1762{
1763 int i;
1764 struct tun_file *tfile;
1765
1766 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001767 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001768 sk_detach_filter(tfile->socket.sk);
1769 }
1770
1771 tun->filter_attached = false;
1772}
1773
1774static int tun_attach_filter(struct tun_struct *tun)
1775{
1776 int i, ret = 0;
1777 struct tun_file *tfile;
1778
1779 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001780 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001781 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1782 if (ret) {
1783 tun_detach_filter(tun, i);
1784 return ret;
1785 }
1786 }
1787
1788 tun->filter_attached = true;
1789 return ret;
1790}
1791
1792static void tun_set_sndbuf(struct tun_struct *tun)
1793{
1794 struct tun_file *tfile;
1795 int i;
1796
1797 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001798 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001799 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1800 }
1801}
1802
Jason Wangcde8b152012-10-31 19:46:01 +00001803static int tun_set_queue(struct file *file, struct ifreq *ifr)
1804{
1805 struct tun_file *tfile = file->private_data;
1806 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00001807 int ret = 0;
1808
1809 rtnl_lock();
1810
1811 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00001812 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001813 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00001814 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001815 goto unlock;
1816 }
1817 ret = security_tun_dev_attach_queue(tun->security);
1818 if (ret < 0)
1819 goto unlock;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001820 ret = tun_attach(tun, file, false);
Jason Wang4008e972012-12-13 23:53:30 +00001821 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001822 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001823 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00001824 ret = -EINVAL;
1825 else
1826 __tun_detach(tfile, false);
1827 } else
Jason Wangcde8b152012-10-31 19:46:01 +00001828 ret = -EINVAL;
1829
Paul Moore5dbbaf22013-01-14 07:12:19 +00001830unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00001831 rtnl_unlock();
1832 return ret;
1833}
1834
Arnd Bergmann50857e22009-11-06 22:52:32 -08001835static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1836 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001838 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001839 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 void __user* argp = (void __user*)arg;
1841 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001842 kuid_t owner;
1843 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001844 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001845 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001846 unsigned int ifindex;
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02001847 int le;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001848 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Jason Wangcde8b152012-10-31 19:46:01 +00001850 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001851 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001853 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001854 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001855 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001856 if (cmd == TUNGETFEATURES) {
1857 /* Currently this just means: "what IFF flags are valid?".
1858 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02001859 * TUNSETIFF.
1860 */
1861 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001862 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001863 } else if (cmd == TUNSETQUEUE)
1864 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001865
Jason Wangc8d68e62012-10-31 19:46:00 +00001866 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001867 rtnl_lock();
1868
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001869 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1872
Herbert Xu876bfd42009-08-06 14:22:44 +00001873 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Herbert Xu876bfd42009-08-06 14:22:44 +00001875 if (ret)
1876 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Arnd Bergmann50857e22009-11-06 22:52:32 -08001878 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001879 ret = -EFAULT;
1880 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001882 if (cmd == TUNSETIFINDEX) {
1883 ret = -EPERM;
1884 if (tun)
1885 goto unlock;
1886
1887 ret = -EFAULT;
1888 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1889 goto unlock;
1890
1891 ret = 0;
1892 tfile->ifindex = ifindex;
1893 goto unlock;
1894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Herbert Xu876bfd42009-08-06 14:22:44 +00001896 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001898 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Jason Wang1e588332012-10-31 19:45:56 +00001900 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Eric W. Biederman631ab462009-01-20 11:00:40 +00001902 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001904 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001905 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001906
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001907 if (tfile->detached)
1908 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001909 if (!tfile->socket.sk->sk_filter)
1910 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001911
Arnd Bergmann50857e22009-11-06 22:52:32 -08001912 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001913 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001914 break;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 case TUNSETNOCSUM:
1917 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Michał Mirosław88255372011-04-19 06:13:10 +00001919 /* [unimplemented] */
1920 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001921 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 break;
1923
1924 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001925 /* Disable/Enable persist mode. Keep an extra reference to the
1926 * module to prevent the module being unprobed.
1927 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001928 if (arg && !(tun->flags & IFF_PERSIST)) {
1929 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001930 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00001931 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001932 if (!arg && (tun->flags & IFF_PERSIST)) {
1933 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001934 module_put(THIS_MODULE);
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Joe Perches6b8a66e2011-03-02 07:18:10 +00001937 tun_debug(KERN_INFO, tun, "persist %s\n",
1938 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 break;
1940
1941 case TUNSETOWNER:
1942 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001943 owner = make_kuid(current_user_ns(), arg);
1944 if (!uid_valid(owner)) {
1945 ret = -EINVAL;
1946 break;
1947 }
1948 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001949 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001950 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 break;
1952
Guido Guenther8c644622007-07-02 22:50:25 -07001953 case TUNSETGROUP:
1954 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001955 group = make_kgid(current_user_ns(), arg);
1956 if (!gid_valid(group)) {
1957 ret = -EINVAL;
1958 break;
1959 }
1960 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001961 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001962 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001963 break;
1964
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001965 case TUNSETLINK:
1966 /* Only allow setting the type when the interface is down */
1967 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001968 tun_debug(KERN_INFO, tun,
1969 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001970 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001971 } else {
1972 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001973 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1974 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001975 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001976 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001977 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979#ifdef TUN_DEBUG
1980 case TUNSETDEBUG:
1981 tun->debug = arg;
1982 break;
1983#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001984 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001985 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001986 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001987
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001988 case TUNSETTXFILTER:
1989 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001990 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001991 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001992 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001993 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001994 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001997 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001998 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1999 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08002000 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002001 ret = -EFAULT;
2002 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002005 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00002006 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2007 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08002008
Kim B. Heino40102372008-02-29 12:26:21 -08002009 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002010 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002011
2012 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002013 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002014 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2015 ret = -EFAULT;
2016 break;
2017
2018 case TUNSETSNDBUF:
2019 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2020 ret = -EFAULT;
2021 break;
2022 }
2023
Jason Wangc8d68e62012-10-31 19:46:00 +00002024 tun->sndbuf = sndbuf;
2025 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002026 break;
2027
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002028 case TUNGETVNETHDRSZ:
2029 vnet_hdr_sz = tun->vnet_hdr_sz;
2030 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2031 ret = -EFAULT;
2032 break;
2033
2034 case TUNSETVNETHDRSZ:
2035 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2036 ret = -EFAULT;
2037 break;
2038 }
2039 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2040 ret = -EINVAL;
2041 break;
2042 }
2043
2044 tun->vnet_hdr_sz = vnet_hdr_sz;
2045 break;
2046
Michael S. Tsirkin1cf8e412014-12-16 15:05:06 +02002047 case TUNGETVNETLE:
2048 le = !!(tun->flags & TUN_VNET_LE);
2049 if (put_user(le, (int __user *)argp))
2050 ret = -EFAULT;
2051 break;
2052
2053 case TUNSETVNETLE:
2054 if (get_user(le, (int __user *)argp)) {
2055 ret = -EFAULT;
2056 break;
2057 }
2058 if (le)
2059 tun->flags |= TUN_VNET_LE;
2060 else
2061 tun->flags &= ~TUN_VNET_LE;
2062 break;
2063
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002064 case TUNATTACHFILTER:
2065 /* Can be set only for TAPs */
2066 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002067 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002068 break;
2069 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002070 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002071 break;
2072
Jason Wangc8d68e62012-10-31 19:46:00 +00002073 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002074 break;
2075
2076 case TUNDETACHFILTER:
2077 /* Can be set only for TAPs */
2078 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002079 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002080 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002081 ret = 0;
2082 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002083 break;
2084
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002085 case TUNGETFILTER:
2086 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002087 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002088 break;
2089 ret = -EFAULT;
2090 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2091 break;
2092 ret = 0;
2093 break;
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002096 ret = -EINVAL;
2097 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Herbert Xu876bfd42009-08-06 14:22:44 +00002100unlock:
2101 rtnl_unlock();
2102 if (tun)
2103 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Arnd Bergmann50857e22009-11-06 22:52:32 -08002107static long tun_chr_ioctl(struct file *file,
2108 unsigned int cmd, unsigned long arg)
2109{
2110 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2111}
2112
2113#ifdef CONFIG_COMPAT
2114static long tun_chr_compat_ioctl(struct file *file,
2115 unsigned int cmd, unsigned long arg)
2116{
2117 switch (cmd) {
2118 case TUNSETIFF:
2119 case TUNGETIFF:
2120 case TUNSETTXFILTER:
2121 case TUNGETSNDBUF:
2122 case TUNSETSNDBUF:
2123 case SIOCGIFHWADDR:
2124 case SIOCSIFHWADDR:
2125 arg = (unsigned long)compat_ptr(arg);
2126 break;
2127 default:
2128 arg = (compat_ulong_t)arg;
2129 break;
2130 }
2131
2132 /*
2133 * compat_ifreq is shorter than ifreq, so we must not access beyond
2134 * the end of that structure. All fields that are used in this
2135 * driver are compatible though, we don't need to convert the
2136 * contents.
2137 */
2138 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2139}
2140#endif /* CONFIG_COMPAT */
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142static int tun_chr_fasync(int fd, struct file *file, int on)
2143{
Jason Wang54f968d2012-10-31 19:45:57 +00002144 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 int ret;
2146
Jason Wang54f968d2012-10-31 19:45:57 +00002147 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002148 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (on) {
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002151 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00002152 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002153 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002154 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002155 ret = 0;
2156out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
2160static int tun_chr_open(struct inode *inode, struct file * file)
2161{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002162 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002163
Joe Perches6b8a66e2011-03-02 07:18:10 +00002164 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002165
Jason Wang54f968d2012-10-31 19:45:57 +00002166 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2167 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002168 if (!tfile)
2169 return -ENOMEM;
Monam Agarwalc9566742014-03-24 00:02:32 +05302170 RCU_INIT_POINTER(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002171 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00002172 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002173 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002174
Jason Wang54f968d2012-10-31 19:45:57 +00002175 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07002176 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00002177
2178 tfile->socket.file = file;
2179 tfile->socket.ops = &tun_socket_ops;
2180
2181 sock_init_data(&tfile->socket, &tfile->sk);
2182 sk_change_net(&tfile->sk, tfile->net);
2183
2184 tfile->sk.sk_write_space = tun_sock_write_space;
2185 tfile->sk.sk_sndbuf = INT_MAX;
2186
Eric W. Biederman631ab462009-01-20 11:00:40 +00002187 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00002188 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
Jason Wang4008e972012-12-13 23:53:30 +00002189 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002190
Jason Wang19a6afb2013-06-08 14:17:41 +08002191 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return 0;
2194}
2195
2196static int tun_chr_close(struct inode *inode, struct file *file)
2197{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002198 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00002199 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Jason Wangc8d68e62012-10-31 19:46:00 +00002201 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00002202 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 return 0;
2205}
2206
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002207#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -07002208static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002209{
2210 struct tun_struct *tun;
2211 struct ifreq ifr;
2212
2213 memset(&ifr, 0, sizeof(ifr));
2214
2215 rtnl_lock();
2216 tun = tun_get(f);
2217 if (tun)
2218 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2219 rtnl_unlock();
2220
2221 if (tun)
2222 tun_put(tun);
2223
Joe Perchesa3816ab2014-09-29 16:08:25 -07002224 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002225}
2226#endif
2227
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002228static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002229 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 .llseek = no_llseek,
Al Viro9b067032014-11-07 13:52:07 -05002231 .read = new_sync_read,
Al Virof5ff53b2014-06-19 15:36:49 -04002232 .write = new_sync_write,
Al Viro9b067032014-11-07 13:52:07 -05002233 .read_iter = tun_chr_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04002234 .write_iter = tun_chr_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002236 .unlocked_ioctl = tun_chr_ioctl,
2237#ifdef CONFIG_COMPAT
2238 .compat_ioctl = tun_chr_compat_ioctl,
2239#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 .open = tun_chr_open,
2241 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002242 .fasync = tun_chr_fasync,
2243#ifdef CONFIG_PROC_FS
2244 .show_fdinfo = tun_chr_show_fdinfo,
2245#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246};
2247
2248static struct miscdevice tun_miscdev = {
2249 .minor = TUN_MINOR,
2250 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002251 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253};
2254
2255/* ethtool interface */
2256
2257static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2258{
2259 cmd->supported = 0;
2260 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00002261 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 cmd->duplex = DUPLEX_FULL;
2263 cmd->port = PORT_TP;
2264 cmd->phy_address = 0;
2265 cmd->transceiver = XCVR_INTERNAL;
2266 cmd->autoneg = AUTONEG_DISABLE;
2267 cmd->maxtxpkt = 0;
2268 cmd->maxrxpkt = 0;
2269 return 0;
2270}
2271
2272static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2273{
2274 struct tun_struct *tun = netdev_priv(dev);
2275
Rick Jones33a5ba12011-11-15 14:59:53 +00002276 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2277 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002280 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00002281 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002283 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00002284 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 break;
2286 }
2287}
2288
2289static u32 tun_get_msglevel(struct net_device *dev)
2290{
2291#ifdef TUN_DEBUG
2292 struct tun_struct *tun = netdev_priv(dev);
2293 return tun->debug;
2294#else
2295 return -EOPNOTSUPP;
2296#endif
2297}
2298
2299static void tun_set_msglevel(struct net_device *dev, u32 value)
2300{
2301#ifdef TUN_DEBUG
2302 struct tun_struct *tun = netdev_priv(dev);
2303 tun->debug = value;
2304#endif
2305}
2306
Jeff Garzik7282d492006-09-13 14:30:00 -04002307static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 .get_settings = tun_get_settings,
2309 .get_drvinfo = tun_get_drvinfo,
2310 .get_msglevel = tun_get_msglevel,
2311 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002312 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002313 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314};
2315
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317static int __init tun_init(void)
2318{
2319 int ret = 0;
2320
Joe Perches6b8a66e2011-03-02 07:18:10 +00002321 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2322 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002324 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002325 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002326 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002327 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002328 }
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002331 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002332 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002333 goto err_misc;
2334 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002335 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002336err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002337 rtnl_link_unregister(&tun_link_ops);
2338err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return ret;
2340}
2341
2342static void tun_cleanup(void)
2343{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002344 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002345 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002348/* Get an underlying socket object from tun file. Returns error unless file is
2349 * attached to a device. The returned object works like a packet socket, it
2350 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2351 * holding a reference to the file for as long as the socket is in use. */
2352struct socket *tun_get_socket(struct file *file)
2353{
Jason Wang6e914fc2012-10-31 19:45:58 +00002354 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002355 if (file->f_op != &tun_fops)
2356 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002357 tfile = file->private_data;
2358 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002359 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002360 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002361}
2362EXPORT_SYMBOL_GPL(tun_get_socket);
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364module_init(tun_init);
2365module_exit(tun_cleanup);
2366MODULE_DESCRIPTION(DRV_DESCRIPTION);
2367MODULE_AUTHOR(DRV_COPYRIGHT);
2368MODULE_LICENSE("GPL");
2369MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002370MODULE_ALIAS("devname:net/tun");