blob: 36be4fd0cd2324af4d42c8f28ec9fbe6b64fb5b8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/uaccess.h>
76
Rusty Russell14daa022008-04-12 18:48:58 -070077/* Uncomment to enable debugging */
78/* #define TUN_DEBUG 1 */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#ifdef TUN_DEBUG
81static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070082
Joe Perches6b8a66e2011-03-02 07:18:10 +000083#define tun_debug(level, tun, fmt, args...) \
84do { \
85 if (tun->debug) \
86 netdev_printk(level, tun->dev, fmt, ##args); \
87} while (0)
88#define DBG1(level, fmt, args...) \
89do { \
90 if (debug == 2) \
91 printk(level fmt, ##args); \
92} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070093#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000094#define tun_debug(level, tun, fmt, args...) \
95do { \
96 if (0) \
97 netdev_printk(level, tun->dev, fmt, ##args); \
98} while (0)
99#define DBG1(level, fmt, args...) \
100do { \
101 if (0) \
102 printk(level fmt, ##args); \
103} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
105
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +0200106/* TUN device flags */
107
108/* IFF_ATTACH_QUEUE is never stored in device flags,
109 * overload it to mean fasync when stored there.
110 */
111#define TUN_FASYNC IFF_ATTACH_QUEUE
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +0200112
113#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
114 IFF_MULTI_QUEUE)
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000115#define GOODCOPY_LEN 128
116
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700117#define FLT_EXACT_COUNT 8
118struct tap_filter {
119 unsigned int count; /* Number of addrs. Zero means disabled */
120 u32 mask[2]; /* Mask of the hashed addrs */
121 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
122};
123
stephen hemminger92d4ea62013-12-05 20:42:58 -0800124/* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for
Jason Wangedfb6a12013-01-23 03:59:12 +0000125 * the netdevice to be fit in one page. So we can make sure the success of
126 * memory allocation. TODO: increase the limit. */
127#define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
Jason Wangb8732fb2013-01-23 03:59:13 +0000128#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000129
Jason Wang96442e422012-10-31 19:46:02 +0000130#define TUN_FLOW_EXPIRE (3 * HZ)
131
Jason Wang54f968d2012-10-31 19:45:57 +0000132/* A tun_file connects an open character device to a tuntap netdevice. It
stephen hemminger92d4ea62013-12-05 20:42:58 -0800133 * also contains all socket related structures (except sock_fprog and tap_filter)
Jason Wang54f968d2012-10-31 19:45:57 +0000134 * to serve as one transmit queue for tuntap device. The sock_fprog and
135 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000136 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000137 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000138 *
139 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000140 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000141 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000142 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000143struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000144 struct sock sk;
145 struct socket socket;
146 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000147 struct tun_struct __rcu *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000148 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000149 struct fasync_struct *fasync;
150 /* only used for fasnyc */
151 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400152 union {
153 u16 queue_index;
154 unsigned int ifindex;
155 };
Jason Wang4008e972012-12-13 23:53:30 +0000156 struct list_head next;
157 struct tun_struct *detached;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000158};
159
Jason Wang96442e422012-10-31 19:46:02 +0000160struct tun_flow_entry {
161 struct hlist_node hash_link;
162 struct rcu_head rcu;
163 struct tun_struct *tun;
164
165 u32 rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800166 u32 rps_rxhash;
Jason Wang96442e422012-10-31 19:46:02 +0000167 int queue_index;
168 unsigned long updated;
169};
170
171#define TUN_NUM_FLOW_ENTRIES 1024
172
Jason Wang54f968d2012-10-31 19:45:57 +0000173/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000174 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000175 * file were attached to a persist device.
176 */
Rusty Russell14daa022008-04-12 18:48:58 -0700177struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000178 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
179 unsigned int numqueues;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700180 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800181 kuid_t owner;
182 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700183
Rusty Russell14daa022008-04-12 18:48:58 -0700184 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000185 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000186#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
Ben Hutchings3d0ad092014-10-30 18:27:12 +0000187 NETIF_F_TSO6)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200188
189 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000190 int sndbuf;
191 struct tap_filter txflt;
192 struct sock_fprog fprog;
193 /* protected by rtnl lock */
194 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700195#ifdef TUN_DEBUG
196 int debug;
197#endif
Jason Wang96442e422012-10-31 19:46:02 +0000198 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000199 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
200 struct timer_list flow_gc_timer;
201 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000202 unsigned int numdisabled;
203 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000204 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000205 u32 flow_count;
Rusty Russell14daa022008-04-12 18:48:58 -0700206};
207
Jason Wang96442e422012-10-31 19:46:02 +0000208static inline u32 tun_hashfn(u32 rxhash)
209{
210 return rxhash & 0x3ff;
211}
212
213static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
214{
215 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000216
Sasha Levinb67bfe02013-02-27 17:06:00 -0800217 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000218 if (e->rxhash == rxhash)
219 return e;
220 }
221 return NULL;
222}
223
224static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
225 struct hlist_head *head,
226 u32 rxhash, u16 queue_index)
227{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000228 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
229
Jason Wang96442e422012-10-31 19:46:02 +0000230 if (e) {
231 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
232 rxhash, queue_index);
233 e->updated = jiffies;
234 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800235 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000236 e->queue_index = queue_index;
237 e->tun = tun;
238 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000239 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000240 }
241 return e;
242}
243
Jason Wang96442e422012-10-31 19:46:02 +0000244static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
245{
246 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
247 e->rxhash, e->queue_index);
Tom Herbert9bc88932013-12-22 18:54:32 +0800248 sock_rps_reset_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000249 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000250 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000251 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000252}
253
254static void tun_flow_flush(struct tun_struct *tun)
255{
256 int i;
257
258 spin_lock_bh(&tun->lock);
259 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
260 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800261 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000262
Sasha Levinb67bfe02013-02-27 17:06:00 -0800263 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000264 tun_flow_delete(tun, e);
265 }
266 spin_unlock_bh(&tun->lock);
267}
268
269static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
270{
271 int i;
272
273 spin_lock_bh(&tun->lock);
274 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
275 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800276 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000277
Sasha Levinb67bfe02013-02-27 17:06:00 -0800278 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000279 if (e->queue_index == queue_index)
280 tun_flow_delete(tun, e);
281 }
282 }
283 spin_unlock_bh(&tun->lock);
284}
285
286static void tun_flow_cleanup(unsigned long data)
287{
288 struct tun_struct *tun = (struct tun_struct *)data;
289 unsigned long delay = tun->ageing_time;
290 unsigned long next_timer = jiffies + delay;
291 unsigned long count = 0;
292 int i;
293
294 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
295
296 spin_lock_bh(&tun->lock);
297 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
298 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800299 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000300
Sasha Levinb67bfe02013-02-27 17:06:00 -0800301 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000302 unsigned long this_timer;
303 count++;
304 this_timer = e->updated + delay;
305 if (time_before_eq(this_timer, jiffies))
306 tun_flow_delete(tun, e);
307 else if (time_before(this_timer, next_timer))
308 next_timer = this_timer;
309 }
310 }
311
312 if (count)
313 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
314 spin_unlock_bh(&tun->lock);
315}
316
Eric Dumazet49974422012-12-12 19:22:57 +0000317static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000318 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000319{
320 struct hlist_head *head;
321 struct tun_flow_entry *e;
322 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000323 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000324
325 if (!rxhash)
326 return;
327 else
328 head = &tun->flows[tun_hashfn(rxhash)];
329
330 rcu_read_lock();
331
Jason Wang9e857222013-01-28 01:05:19 +0000332 /* We may get a very small possibility of OOO during switching, not
333 * worth to optimize.*/
334 if (tun->numqueues == 1 || tfile->detached)
Jason Wang96442e422012-10-31 19:46:02 +0000335 goto unlock;
336
337 e = tun_flow_find(head, rxhash);
338 if (likely(e)) {
339 /* TODO: keep queueing to old queue until it's empty? */
340 e->queue_index = queue_index;
341 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800342 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000343 } else {
344 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000345 if (!tun_flow_find(head, rxhash) &&
346 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000347 tun_flow_create(tun, head, rxhash, queue_index);
348
349 if (!timer_pending(&tun->flow_gc_timer))
350 mod_timer(&tun->flow_gc_timer,
351 round_jiffies_up(jiffies + delay));
352 spin_unlock_bh(&tun->lock);
353 }
354
355unlock:
356 rcu_read_unlock();
357}
358
Tom Herbert9bc88932013-12-22 18:54:32 +0800359/**
360 * Save the hash received in the stack receive path and update the
361 * flow_hash table accordingly.
362 */
363static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
364{
365 if (unlikely(e->rps_rxhash != hash)) {
366 sock_rps_reset_flow_hash(e->rps_rxhash);
367 e->rps_rxhash = hash;
368 }
369}
370
Jason Wangc8d68e62012-10-31 19:46:00 +0000371/* We try to identify a flow through its rxhash first. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800372 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000373 * the rxq based on the txq where the last packet of the flow comes. As
374 * the userspace application move between processors, we may get a
375 * different rxq no. here. If we could not get rxhash, then we would
376 * hope the rxq no. may help here.
377 */
Jason Wangf663dd92014-01-10 16:18:26 +0800378static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100379 void *accel_priv, select_queue_fallback_t fallback)
Jason Wangc8d68e62012-10-31 19:46:00 +0000380{
381 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000382 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000383 u32 txq = 0;
384 u32 numqueues = 0;
385
386 rcu_read_lock();
Jason Wang92bb73e2013-06-05 16:44:57 +0800387 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000388
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800389 txq = skb_get_hash(skb);
Jason Wangc8d68e62012-10-31 19:46:00 +0000390 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000391 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
Tom Herbert9bc88932013-12-22 18:54:32 +0800392 if (e) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800393 tun_flow_save_rps_rxhash(e, txq);
Zhi Yong Wufbe4d452014-01-02 13:24:28 +0800394 txq = e->queue_index;
Tom Herbert9bc88932013-12-22 18:54:32 +0800395 } else
Jason Wang96442e422012-10-31 19:46:02 +0000396 /* use multiply and shift instead of expensive divide */
397 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000398 } else if (likely(skb_rx_queue_recorded(skb))) {
399 txq = skb_get_rx_queue(skb);
400 while (unlikely(txq >= numqueues))
401 txq -= numqueues;
402 }
403
404 rcu_read_unlock();
405 return txq;
406}
407
Jason Wangcde8b152012-10-31 19:46:01 +0000408static inline bool tun_not_capable(struct tun_struct *tun)
409{
410 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000411 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000412
413 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
414 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000415 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000416}
417
Jason Wangc8d68e62012-10-31 19:46:00 +0000418static void tun_set_real_num_queues(struct tun_struct *tun)
419{
420 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
421 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
422}
423
Jason Wang4008e972012-12-13 23:53:30 +0000424static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
425{
426 tfile->detached = tun;
427 list_add_tail(&tfile->next, &tun->disabled);
428 ++tun->numdisabled;
429}
430
Jason Wangd32649d2012-12-18 11:00:27 +0800431static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000432{
433 struct tun_struct *tun = tfile->detached;
434
435 tfile->detached = NULL;
436 list_del_init(&tfile->next);
437 --tun->numdisabled;
438 return tun;
439}
440
Jason Wang4bfb0512013-09-05 17:53:59 +0800441static void tun_queue_purge(struct tun_file *tfile)
442{
443 skb_queue_purge(&tfile->sk.sk_receive_queue);
444 skb_queue_purge(&tfile->sk.sk_error_queue);
445}
446
Jason Wangc8d68e62012-10-31 19:46:00 +0000447static void __tun_detach(struct tun_file *tfile, bool clean)
448{
449 struct tun_file *ntfile;
450 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000451
Jason Wangb8deabd2013-01-11 16:59:32 +0000452 tun = rtnl_dereference(tfile->tun);
453
Jason Wang9e857222013-01-28 01:05:19 +0000454 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000455 u16 index = tfile->queue_index;
456 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000457
458 rcu_assign_pointer(tun->tfiles[index],
459 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000460 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000461 ntfile->queue_index = index;
462
463 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000464 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530465 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000466 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000467 } else
Jason Wang4008e972012-12-13 23:53:30 +0000468 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000469
470 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000471 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000472 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800473 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000474 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000475 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000476 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000477 sock_put(&tfile->sk);
478 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000479
480 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000481 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
482 netif_carrier_off(tun->dev);
483
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200484 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000485 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000486 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000487 }
Jason Wang4008e972012-12-13 23:53:30 +0000488
Jason Wangc8d68e62012-10-31 19:46:00 +0000489 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
490 &tfile->socket.flags));
491 sk_release_kernel(&tfile->sk);
492 }
493}
494
495static void tun_detach(struct tun_file *tfile, bool clean)
496{
497 rtnl_lock();
498 __tun_detach(tfile, clean);
499 rtnl_unlock();
500}
501
502static void tun_detach_all(struct net_device *dev)
503{
504 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000505 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000506 int i, n = tun->numqueues;
507
508 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000509 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000510 BUG_ON(!tfile);
Xi Wang9e641bd2014-05-16 15:11:48 -0700511 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530512 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000513 --tun->numqueues;
514 }
Jason Wang9e857222013-01-28 01:05:19 +0000515 list_for_each_entry(tfile, &tun->disabled, next) {
Xi Wang9e641bd2014-05-16 15:11:48 -0700516 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530517 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000518 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000519 BUG_ON(tun->numqueues != 0);
520
521 synchronize_net();
522 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000523 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000524 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800525 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000526 sock_put(&tfile->sk);
527 }
Jason Wang4008e972012-12-13 23:53:30 +0000528 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
529 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800530 tun_queue_purge(tfile);
Jason Wang4008e972012-12-13 23:53:30 +0000531 sock_put(&tfile->sk);
532 }
533 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000534
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200535 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000536 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000537}
538
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400539static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000540{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000541 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000542 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000543
Paul Moore5dbbaf22013-01-14 07:12:19 +0000544 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
545 if (err < 0)
546 goto out;
547
Eric W. Biederman38231b72009-01-20 11:02:28 +0000548 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000549 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000550 goto out;
551
552 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200553 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000554 goto out;
555
556 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000557 if (!tfile->detached &&
558 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000559 goto out;
560
561 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000562
stephen hemminger92d4ea62013-12-05 20:42:58 -0800563 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400564 if (!skip_filter && (tun->filter_attached == true)) {
Jason Wang54f968d2012-10-31 19:45:57 +0000565 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
566 if (!err)
567 goto out;
568 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000569 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000570 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000571 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000572 tun->numqueues++;
573
Jason Wang4008e972012-12-13 23:53:30 +0000574 if (tfile->detached)
575 tun_enable_queue(tfile);
576 else
577 sock_hold(&tfile->sk);
578
Jason Wangc8d68e62012-10-31 19:46:00 +0000579 tun_set_real_num_queues(tun);
580
Jason Wangc8d68e62012-10-31 19:46:00 +0000581 /* device is allowed to go away first, so no need to hold extra
582 * refcnt.
583 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000584
Eric W. Biederman38231b72009-01-20 11:02:28 +0000585out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000586 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000587}
588
Eric W. Biederman631ab462009-01-20 11:00:40 +0000589static struct tun_struct *__tun_get(struct tun_file *tfile)
590{
Jason Wang6e914fc2012-10-31 19:45:58 +0000591 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000592
Jason Wang6e914fc2012-10-31 19:45:58 +0000593 rcu_read_lock();
594 tun = rcu_dereference(tfile->tun);
595 if (tun)
596 dev_hold(tun->dev);
597 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000598
599 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000600}
601
602static struct tun_struct *tun_get(struct file *file)
603{
604 return __tun_get(file->private_data);
605}
606
607static void tun_put(struct tun_struct *tun)
608{
Jason Wang6e914fc2012-10-31 19:45:58 +0000609 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000610}
611
Joe Perches6b8a66e2011-03-02 07:18:10 +0000612/* TAP filtering */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700613static void addr_hash_set(u32 *mask, const u8 *addr)
614{
615 int n = ether_crc(ETH_ALEN, addr) >> 26;
616 mask[n >> 5] |= (1 << (n & 31));
617}
618
619static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
620{
621 int n = ether_crc(ETH_ALEN, addr) >> 26;
622 return mask[n >> 5] & (1 << (n & 31));
623}
624
625static int update_filter(struct tap_filter *filter, void __user *arg)
626{
627 struct { u8 u[ETH_ALEN]; } *addr;
628 struct tun_filter uf;
629 int err, alen, n, nexact;
630
631 if (copy_from_user(&uf, arg, sizeof(uf)))
632 return -EFAULT;
633
634 if (!uf.count) {
635 /* Disabled */
636 filter->count = 0;
637 return 0;
638 }
639
640 alen = ETH_ALEN * uf.count;
641 addr = kmalloc(alen, GFP_KERNEL);
642 if (!addr)
643 return -ENOMEM;
644
645 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
646 err = -EFAULT;
647 goto done;
648 }
649
650 /* The filter is updated without holding any locks. Which is
651 * perfectly safe. We disable it first and in the worst
652 * case we'll accept a few undesired packets. */
653 filter->count = 0;
654 wmb();
655
656 /* Use first set of addresses as an exact filter */
657 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
658 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
659
660 nexact = n;
661
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800662 /* Remaining multicast addresses are hashed,
663 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700664 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800665 for (; n < uf.count; n++) {
666 if (!is_multicast_ether_addr(addr[n].u)) {
667 err = 0; /* no filter */
668 goto done;
669 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700670 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800671 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700672
673 /* For ALLMULTI just set the mask to all ones.
674 * This overrides the mask populated above. */
675 if ((uf.flags & TUN_FLT_ALLMULTI))
676 memset(filter->mask, ~0, sizeof(filter->mask));
677
678 /* Now enable the filter */
679 wmb();
680 filter->count = nexact;
681
682 /* Return the number of exact filters */
683 err = nexact;
684
685done:
686 kfree(addr);
687 return err;
688}
689
690/* Returns: 0 - drop, !=0 - accept */
691static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
692{
693 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
694 * at this point. */
695 struct ethhdr *eh = (struct ethhdr *) skb->data;
696 int i;
697
698 /* Exact match */
699 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000700 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700701 return 1;
702
703 /* Inexact match (multicast only) */
704 if (is_multicast_ether_addr(eh->h_dest))
705 return addr_hash_test(filter->mask, eh->h_dest);
706
707 return 0;
708}
709
710/*
711 * Checks whether the packet is accepted or not.
712 * Returns: 0 - drop, !=0 - accept
713 */
714static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
715{
716 if (!filter->count)
717 return 1;
718
719 return run_filter(filter, skb);
720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/* Network device part of the driver */
723
Jeff Garzik7282d492006-09-13 14:30:00 -0400724static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000726/* Net device detach from fd. */
727static void tun_net_uninit(struct net_device *dev)
728{
Jason Wangc8d68e62012-10-31 19:46:00 +0000729 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/* Net device open. */
733static int tun_net_open(struct net_device *dev)
734{
Jason Wangc8d68e62012-10-31 19:46:00 +0000735 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 0;
737}
738
739/* Net device close. */
740static int tun_net_close(struct net_device *dev)
741{
Jason Wangc8d68e62012-10-31 19:46:00 +0000742 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return 0;
744}
745
746/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000747static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000750 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000751 struct tun_file *tfile;
Dominic Curranfa358642014-01-22 03:03:23 +0000752 u32 numqueues = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Jason Wang6e914fc2012-10-31 19:45:58 +0000754 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000755 tfile = rcu_dereference(tun->tfiles[txq]);
Dominic Curranfa358642014-01-22 03:03:23 +0000756 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* Drop packet if interface is not attached */
Dominic Curranfa358642014-01-22 03:03:23 +0000759 if (txq >= numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto drop;
761
Dominic Curranfa358642014-01-22 03:03:23 +0000762 if (numqueues == 1) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800763 /* Select queue was not called for the skbuff, so we extract the
764 * RPS hash and save it into the flow_table here.
765 */
766 __u32 rxhash;
767
768 rxhash = skb_get_hash(skb);
769 if (rxhash) {
770 struct tun_flow_entry *e;
771 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
772 rxhash);
773 if (e)
774 tun_flow_save_rps_rxhash(e, rxhash);
775 }
776 }
777
Jason Wang6e914fc2012-10-31 19:45:58 +0000778 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
779
Jason Wangc8d68e62012-10-31 19:46:00 +0000780 BUG_ON(!tfile);
781
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700782 /* Drop if the filter does not like it.
783 * This is a noop if the filter is disabled.
784 * Filter can be enabled only for the TAP devices. */
785 if (!check_filter(&tun->txflt, skb))
786 goto drop;
787
Jason Wang54f968d2012-10-31 19:45:57 +0000788 if (tfile->socket.sk->sk_filter &&
789 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000790 goto drop;
791
Rami Rosen36fe8c092012-11-25 22:07:40 +0000792 /* Limit the number of packets queued by dividing txq length with the
Jason Wangc8d68e62012-10-31 19:46:00 +0000793 * number of queues.
794 */
Dominic Curranfa358642014-01-22 03:03:23 +0000795 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
796 >= dev->tx_queue_len)
Michael S. Tsirkin5d097102012-12-03 10:07:14 +0000797 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jason Wang7bf66302013-09-05 17:54:00 +0800799 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
800 goto drop;
801
Richard Cochraneda29772013-07-19 19:40:10 +0200802 if (skb->sk) {
803 sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
804 sw_tx_timestamp(skb);
805 }
806
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000807 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800808 * for indefinite time.
809 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000810 skb_orphan(skb);
811
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000812 nf_reset(skb);
813
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700814 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000815 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000818 if (tfile->flags & TUN_FASYNC)
819 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -0700820 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +0000821
822 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000823 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700826 dev->stats.tx_dropped++;
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000827 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000829 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000830 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700833static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700835 /*
836 * This callback is supposed to deal with mc filter in
837 * _rx_ path and has nothing to do with the _tx_ path.
838 * In rx path we always accept everything userspace gives us.
839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Ed Swierk4885a502007-09-16 12:21:38 -0700842#define MIN_MTU 68
843#define MAX_MTU 65535
844
845static int
846tun_net_change_mtu(struct net_device *dev, int new_mtu)
847{
848 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
849 return -EINVAL;
850 dev->mtu = new_mtu;
851 return 0;
852}
853
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000854static netdev_features_t tun_net_fix_features(struct net_device *dev,
855 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000856{
857 struct tun_struct *tun = netdev_priv(dev);
858
859 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
860}
Neil Hormanbebd0972011-06-15 05:25:01 +0000861#ifdef CONFIG_NET_POLL_CONTROLLER
862static void tun_poll_controller(struct net_device *dev)
863{
864 /*
865 * Tun only receives frames when:
866 * 1) the char device endpoint gets data from user space
867 * 2) the tun socket gets a sendmsg call from user space
stephen hemminger92d4ea62013-12-05 20:42:58 -0800868 * Since both of those are synchronous operations, we are guaranteed
Neil Hormanbebd0972011-06-15 05:25:01 +0000869 * never to have pending data when we poll for it
stephen hemminger92d4ea62013-12-05 20:42:58 -0800870 * so there is nothing to do here but return.
Neil Hormanbebd0972011-06-15 05:25:01 +0000871 * We need this though so netpoll recognizes us as an interface that
872 * supports polling, which enables bridge devices in virt setups to
873 * still use netconsole
874 */
875 return;
876}
877#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800878static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000879 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800880 .ndo_open = tun_net_open,
881 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800882 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800883 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000884 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000885 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000886#ifdef CONFIG_NET_POLL_CONTROLLER
887 .ndo_poll_controller = tun_poll_controller,
888#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800889};
890
891static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000892 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800893 .ndo_open = tun_net_open,
894 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800895 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800896 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000897 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000898 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800899 .ndo_set_mac_address = eth_mac_addr,
900 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000901 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000902#ifdef CONFIG_NET_POLL_CONTROLLER
903 .ndo_poll_controller = tun_poll_controller,
904#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800905};
906
Pavel Emelyanov944a1372013-06-11 17:01:08 +0400907static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +0000908{
909 int i;
910
Jason Wang96442e422012-10-31 19:46:02 +0000911 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
912 INIT_HLIST_HEAD(&tun->flows[i]);
913
914 tun->ageing_time = TUN_FLOW_EXPIRE;
915 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
916 mod_timer(&tun->flow_gc_timer,
917 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +0000918}
919
920static void tun_flow_uninit(struct tun_struct *tun)
921{
922 del_timer_sync(&tun->flow_gc_timer);
923 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +0000924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/* Initialize net device. */
927static void tun_net_init(struct net_device *dev)
928{
929 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200932 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800933 dev->netdev_ops = &tun_netdev_ops;
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* Point-to-Point TUN Device */
936 dev->hard_header_len = 0;
937 dev->addr_len = 0;
938 dev->mtu = 1500;
939
940 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400941 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
943 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
944 break;
945
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200946 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800947 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* Ethernet TAP Device */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700949 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000950 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +0000951 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000953 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
956 break;
957 }
958}
959
960/* Character device part */
961
962/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000963static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400964{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000965 struct tun_file *tfile = file->private_data;
966 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000967 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800968 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000971 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Jason Wang54f968d2012-10-31 19:45:57 +0000973 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000974
Joe Perches6b8a66e2011-03-02 07:18:10 +0000975 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Xi Wang9e641bd2014-05-16 15:11:48 -0700977 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400978
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000979 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 mask |= POLLIN | POLLRDNORM;
981
Herbert Xu33dccbb2009-02-05 21:25:32 -0800982 if (sock_writeable(sk) ||
983 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
984 sock_writeable(sk)))
985 mask |= POLLOUT | POLLWRNORM;
986
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000987 if (tun->dev->reg_state != NETREG_REGISTERED)
988 mask = POLLERR;
989
Eric W. Biederman631ab462009-01-20 11:00:40 +0000990 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return mask;
992}
993
Rusty Russellf42157c2008-08-15 15:15:10 -0700994/* prepad is the amount to reserve at front. len is length after that.
995 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000996static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000997 size_t prepad, size_t len,
998 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700999{
Jason Wang54f968d2012-10-31 19:45:57 +00001000 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001001 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001002 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001003
1004 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001005 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001006 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001007
Herbert Xu33dccbb2009-02-05 21:25:32 -08001008 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001009 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001010 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001011 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001012
1013 skb_reserve(skb, prepad);
1014 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001015 skb->data_len = len - linear;
1016 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001017
1018 return skb;
1019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001022static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1023 void *msg_control, const struct iovec *iv,
1024 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Harvey Harrison09640e62009-02-01 00:45:17 -08001026 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct sk_buff *skb;
Jason Wang3dd5c332013-07-10 13:43:27 +08001028 size_t len = total_len, align = NET_SKB_PAD, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001029 struct virtio_net_hdr gso = { 0 };
Jason Wang96f8d9e2013-11-13 14:00:39 +08001030 int good_linear;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001031 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001032 int copylen;
1033 bool zerocopy = false;
1034 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001035 u32 rxhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001037 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001038 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001040 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001042 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001044 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001047 if (tun->flags & IFF_VNET_HDR) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001048 if (len < tun->vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001049 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001050 len -= tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001051
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001052 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -07001053 return -EFAULT;
1054
Herbert Xu49091222009-06-08 00:20:01 -07001055 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1056 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
1057 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
1058
Rusty Russellf43798c2008-07-03 03:48:02 -07001059 if (gso.hdr_len > len)
1060 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001061 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001062 }
1063
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001064 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001065 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001066 if (unlikely(len < ETH_HLEN ||
1067 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001068 return -EINVAL;
1069 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001070
Jason Wang96f8d9e2013-11-13 14:00:39 +08001071 good_linear = SKB_MAX_HEAD(align);
1072
Jason Wang88529172013-07-18 10:55:15 +08001073 if (msg_control) {
1074 /* There are 256 bytes to be copied in skb, so there is
1075 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001076 * The rest of the buffer is mapped from userspace.
1077 */
Jason Wang88529172013-07-18 10:55:15 +08001078 copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001079 if (copylen > good_linear)
1080 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001081 linear = copylen;
Jason Wang88529172013-07-18 10:55:15 +08001082 if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
1083 zerocopy = true;
1084 }
1085
1086 if (!zerocopy) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001087 copylen = len;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001088 if (gso.hdr_len > good_linear)
1089 linear = good_linear;
1090 else
1091 linear = gso.hdr_len;
Jason Wang3dd5c332013-07-10 13:43:27 +08001092 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001093
Jason Wang3dd5c332013-07-10 13:43:27 +08001094 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001095 if (IS_ERR(skb)) {
1096 if (PTR_ERR(skb) != -EAGAIN)
1097 tun->dev->stats.rx_dropped++;
1098 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001101 if (zerocopy)
1102 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
Jason Wang88529172013-07-18 10:55:15 +08001103 else {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001104 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
Jason Wang88529172013-07-18 10:55:15 +08001105 if (!err && msg_control) {
1106 struct ubuf_info *uarg = msg_control;
1107 uarg->callback(uarg, false);
1108 }
1109 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001110
1111 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001112 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -08001113 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -08001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Rusty Russellf43798c2008-07-03 03:48:02 -07001117 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1118 if (!skb_partial_csum_set(skb, gso.csum_start,
1119 gso.csum_offset)) {
1120 tun->dev->stats.rx_frame_errors++;
1121 kfree_skb(skb);
1122 return -EINVAL;
1123 }
Michał Mirosław88255372011-04-19 06:13:10 +00001124 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001127 case IFF_TUN:
1128 if (tun->flags & IFF_NO_PI) {
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001129 switch (skb->data[0] & 0xf0) {
1130 case 0x40:
1131 pi.proto = htons(ETH_P_IP);
1132 break;
1133 case 0x60:
1134 pi.proto = htons(ETH_P_IPV6);
1135 break;
1136 default:
1137 tun->dev->stats.rx_dropped++;
1138 kfree_skb(skb);
1139 return -EINVAL;
1140 }
1141 }
1142
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001143 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001145 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001147 case IFF_TAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 skb->protocol = eth_type_trans(skb, tun->dev);
1149 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Ben Hutchings5188cd42014-10-30 18:27:17 +00001152 skb_reset_network_header(skb);
1153
Rusty Russellf43798c2008-07-03 03:48:02 -07001154 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1155 pr_debug("GSO!\n");
1156 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1157 case VIRTIO_NET_HDR_GSO_TCPV4:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001158 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Rusty Russellf43798c2008-07-03 03:48:02 -07001159 break;
1160 case VIRTIO_NET_HDR_GSO_TCPV6:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001161 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Rusty Russellf43798c2008-07-03 03:48:02 -07001162 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001163 case VIRTIO_NET_HDR_GSO_UDP:
Ben Hutchings3d0ad092014-10-30 18:27:12 +00001164 {
1165 static bool warned;
1166
1167 if (!warned) {
1168 warned = true;
1169 netdev_warn(tun->dev,
1170 "%s: using disabled UFO feature; please fix this program\n",
1171 current->comm);
1172 }
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001173 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Ben Hutchings5188cd42014-10-30 18:27:17 +00001174 if (skb->protocol == htons(ETH_P_IPV6))
1175 ipv6_proxy_select_ident(skb);
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001176 break;
Ben Hutchings3d0ad092014-10-30 18:27:12 +00001177 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001178 default:
1179 tun->dev->stats.rx_frame_errors++;
1180 kfree_skb(skb);
1181 return -EINVAL;
1182 }
1183
1184 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001185 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Rusty Russellf43798c2008-07-03 03:48:02 -07001186
1187 skb_shinfo(skb)->gso_size = gso.gso_size;
1188 if (skb_shinfo(skb)->gso_size == 0) {
1189 tun->dev->stats.rx_frame_errors++;
1190 kfree_skb(skb);
1191 return -EINVAL;
1192 }
1193
1194 /* Header must be checked, and gso_segs computed. */
1195 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1196 skb_shinfo(skb)->gso_segs = 0;
1197 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001198
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001199 /* copy skb_ubuf_info for callback when skb has no error */
1200 if (zerocopy) {
1201 skb_shinfo(skb)->destructor_arg = msg_control;
1202 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001203 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001204 }
1205
Jason Wang40893fd2013-03-26 23:11:22 +00001206 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001207
Tom Herbert3958afa1b2013-12-15 22:12:06 -08001208 rxhash = skb_get_hash(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001210
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001211 tun->dev->stats.rx_packets++;
1212 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jason Wang9e857222013-01-28 01:05:19 +00001214 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001215 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001218static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1219 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001221 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -08001222 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001223 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001224 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (!tun)
1227 return -EBADFD;
1228
Joe Perches6b8a66e2011-03-02 07:18:10 +00001229 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Jason Wang54f968d2012-10-31 19:45:57 +00001231 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1232 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001233
1234 tun_put(tun);
1235 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001239static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001240 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001241 struct sk_buff *skb,
1242 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct tun_pi pi = { 0, skb->protocol };
1245 ssize_t total = 0;
Jason Wange6fd07c2013-12-11 13:08:33 +08001246 int vlan_offset = 0, copied;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001247 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001248 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001249
1250 if (vlan_tx_tag_present(skb))
1251 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001253 if (tun->flags & IFF_VNET_HDR)
Herbert Xu2eb783c2014-11-03 04:30:14 +08001254 vnet_hdr_sz = tun->vnet_hdr_sz;
1255
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001256 if (!(tun->flags & IFF_NO_PI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if ((len -= sizeof(pi)) < 0)
1258 return -EINVAL;
1259
Herbert Xu2eb783c2014-11-03 04:30:14 +08001260 if (len < skb->len + vlan_hlen + vnet_hdr_sz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /* Packet will be striped */
1262 pi.flags |= TUN_PKT_STRIP;
1263 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001264
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001265 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return -EFAULT;
1267 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Herbert Xu2eb783c2014-11-03 04:30:14 +08001270 if (vnet_hdr_sz) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001271 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Herbert Xu2eb783c2014-11-03 04:30:14 +08001272 if ((len -= vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001273 return -EINVAL;
1274
1275 if (skb_is_gso(skb)) {
1276 struct skb_shared_info *sinfo = skb_shinfo(skb);
1277
1278 /* This is a hint as to how much should be linear. */
1279 gso.hdr_len = skb_headlen(skb);
1280 gso.gso_size = sinfo->gso_size;
1281 if (sinfo->gso_type & SKB_GSO_TCPV4)
1282 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1283 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1284 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001285 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001286 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001287 "0x%x, gso_size %d, hdr_len %d\n",
1288 sinfo->gso_type, gso.gso_size,
1289 gso.hdr_len);
1290 print_hex_dump(KERN_ERR, "tun: ",
1291 DUMP_PREFIX_NONE,
1292 16, 1, skb->head,
1293 min((int)gso.hdr_len, 64), true);
1294 WARN_ON_ONCE(1);
1295 return -EINVAL;
1296 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001297 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1298 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1299 } else
1300 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1301
1302 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1303 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001304 gso.csum_start = skb_checksum_start_offset(skb) +
1305 vlan_hlen;
Rusty Russellf43798c2008-07-03 03:48:02 -07001306 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001307 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1308 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001309 } /* else everything is zero */
1310
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001311 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1312 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001313 return -EFAULT;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001314 total += vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001315 }
1316
Jason Wange6fd07c2013-12-11 13:08:33 +08001317 copied = total;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001318 len = min_t(int, skb->len + vlan_hlen, len);
1319 total += skb->len + vlan_hlen;
1320 if (vlan_hlen) {
Jason Wang6680ec62013-07-25 13:00:33 +08001321 int copy, ret;
1322 struct {
1323 __be16 h_vlan_proto;
1324 __be16 h_vlan_TCI;
1325 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Jason Wang6680ec62013-07-25 13:00:33 +08001327 veth.h_vlan_proto = skb->vlan_proto;
1328 veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Jason Wang6680ec62013-07-25 13:00:33 +08001330 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08001331
1332 copy = min_t(int, vlan_offset, len);
Jason Wange6fd07c2013-12-11 13:08:33 +08001333 ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
Jason Wang6680ec62013-07-25 13:00:33 +08001334 len -= copy;
Jason Wange6fd07c2013-12-11 13:08:33 +08001335 copied += copy;
Jason Wang6680ec62013-07-25 13:00:33 +08001336 if (ret || !len)
1337 goto done;
1338
1339 copy = min_t(int, sizeof(veth), len);
Jason Wange6fd07c2013-12-11 13:08:33 +08001340 ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
Jason Wang6680ec62013-07-25 13:00:33 +08001341 len -= copy;
Jason Wange6fd07c2013-12-11 13:08:33 +08001342 copied += copy;
Jason Wang6680ec62013-07-25 13:00:33 +08001343 if (ret || !len)
1344 goto done;
1345 }
1346
Jason Wange6fd07c2013-12-11 13:08:33 +08001347 skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
Jason Wang6680ec62013-07-25 13:00:33 +08001348
1349done:
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001350 tun->dev->stats.tx_packets++;
1351 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 return total;
1354}
1355
Jason Wang54f968d2012-10-31 19:45:57 +00001356static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001357 const struct iovec *iv, ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001360 ssize_t ret = 0;
Xi Wang9e641bd2014-05-16 15:11:48 -07001361 int peeked, err, off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Rami Rosen3872baf2012-11-25 22:07:41 +00001363 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Xi Wang9e641bd2014-05-16 15:11:48 -07001365 if (!len)
1366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Xi Wang9e641bd2014-05-16 15:11:48 -07001368 if (tun->dev->reg_state != NETREG_REGISTERED)
1369 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Xi Wang9e641bd2014-05-16 15:11:48 -07001371 /* Read frames from queue */
1372 skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
1373 &peeked, &off, &err);
1374 if (skb) {
Jason Wang54f968d2012-10-31 19:45:57 +00001375 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001376 kfree_skb(skb);
Xi Wang9e641bd2014-05-16 15:11:48 -07001377 } else
1378 ret = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001380 return ret;
1381}
1382
1383static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1384 unsigned long count, loff_t pos)
1385{
1386 struct file *file = iocb->ki_filp;
1387 struct tun_file *tfile = file->private_data;
1388 struct tun_struct *tun = __tun_get(tfile);
1389 ssize_t len, ret;
1390
1391 if (!tun)
1392 return -EBADFD;
1393 len = iov_length(iv, count);
1394 if (len < 0) {
1395 ret = -EINVAL;
1396 goto out;
1397 }
1398
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001399 ret = tun_do_read(tun, tfile, iv, len,
Jason Wang54f968d2012-10-31 19:45:57 +00001400 file->f_flags & O_NONBLOCK);
David S. Miller42404c02013-12-10 22:05:45 -05001401 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08001402 if (ret > 0)
1403 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001404out:
1405 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return ret;
1407}
1408
Jason Wang96442e422012-10-31 19:46:02 +00001409static void tun_free_netdev(struct net_device *dev)
1410{
1411 struct tun_struct *tun = netdev_priv(dev);
1412
Jason Wang4008e972012-12-13 23:53:30 +00001413 BUG_ON(!(list_empty(&tun->disabled)));
Jason Wang96442e422012-10-31 19:46:02 +00001414 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001415 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001416 free_netdev(dev);
1417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419static void tun_setup(struct net_device *dev)
1420{
1421 struct tun_struct *tun = netdev_priv(dev);
1422
Eric W. Biederman0625c882012-02-07 16:48:55 -08001423 tun->owner = INVALID_UID;
1424 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang96442e422012-10-31 19:46:02 +00001427 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001430/* Trivial set of netlink ops to allow deleting tun or tap
1431 * device with netlink.
1432 */
1433static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1434{
1435 return -EINVAL;
1436}
1437
1438static struct rtnl_link_ops tun_link_ops __read_mostly = {
1439 .kind = DRV_NAME,
1440 .priv_size = sizeof(struct tun_struct),
1441 .setup = tun_setup,
1442 .validate = tun_validate,
1443};
1444
Herbert Xu33dccbb2009-02-05 21:25:32 -08001445static void tun_sock_write_space(struct sock *sk)
1446{
Jason Wang54f968d2012-10-31 19:45:57 +00001447 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001448 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001449
1450 if (!sock_writeable(sk))
1451 return;
1452
Herbert Xu33dccbb2009-02-05 21:25:32 -08001453 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1454 return;
1455
Eric Dumazet43815482010-04-29 11:01:49 +00001456 wqueue = sk_sleep(sk);
1457 if (wqueue && waitqueue_active(wqueue))
1458 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001459 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001460
Jason Wang54f968d2012-10-31 19:45:57 +00001461 tfile = container_of(sk, struct tun_file, sk);
1462 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001463}
1464
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001465static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1466 struct msghdr *m, size_t total_len)
1467{
Jason Wang54f968d2012-10-31 19:45:57 +00001468 int ret;
1469 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1470 struct tun_struct *tun = __tun_get(tfile);
1471
1472 if (!tun)
1473 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001474 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1475 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1476 tun_put(tun);
1477 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001478}
1479
1480static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1481 struct msghdr *m, size_t total_len,
1482 int flags)
1483{
Jason Wang54f968d2012-10-31 19:45:57 +00001484 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1485 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001486 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001487
1488 if (!tun)
1489 return -EBADFD;
1490
Richard Cochraneda29772013-07-19 19:40:10 +02001491 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00001492 ret = -EINVAL;
1493 goto out;
1494 }
Richard Cochraneda29772013-07-19 19:40:10 +02001495 if (flags & MSG_ERRQUEUE) {
1496 ret = sock_recv_errqueue(sock->sk, m, total_len,
1497 SOL_PACKET, TUN_TX_TIMESTAMP);
1498 goto out;
1499 }
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001500 ret = tun_do_read(tun, tfile, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001501 flags & MSG_DONTWAIT);
David S. Miller42404c02013-12-10 22:05:45 -05001502 if (ret > total_len) {
1503 m->msg_flags |= MSG_TRUNC;
1504 ret = flags & MSG_TRUNC ? ret : total_len;
1505 }
Gao feng3811ae72013-04-24 21:59:23 +00001506out:
Jason Wang54f968d2012-10-31 19:45:57 +00001507 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001508 return ret;
1509}
1510
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001511static int tun_release(struct socket *sock)
1512{
1513 if (sock->sk)
1514 sock_put(sock->sk);
1515 return 0;
1516}
1517
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001518/* Ops structure to mimic raw sockets with tun */
1519static const struct proto_ops tun_socket_ops = {
1520 .sendmsg = tun_sendmsg,
1521 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001522 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001523};
1524
Herbert Xu33dccbb2009-02-05 21:25:32 -08001525static struct proto tun_proto = {
1526 .name = "tun",
1527 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001528 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001529};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001530
David Woodhouse980c9e82009-05-09 22:54:21 -07001531static int tun_flags(struct tun_struct *tun)
1532{
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02001533 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07001534}
1535
1536static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1537 char *buf)
1538{
1539 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1540 return sprintf(buf, "0x%x\n", tun_flags(tun));
1541}
1542
1543static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1544 char *buf)
1545{
1546 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001547 return uid_valid(tun->owner)?
1548 sprintf(buf, "%u\n",
1549 from_kuid_munged(current_user_ns(), tun->owner)):
1550 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001551}
1552
1553static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1554 char *buf)
1555{
1556 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001557 return gid_valid(tun->group) ?
1558 sprintf(buf, "%u\n",
1559 from_kgid_munged(current_user_ns(), tun->group)):
1560 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001561}
1562
1563static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1564static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1565static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1566
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001567static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001570 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 struct net_device *dev;
1572 int err;
1573
Jason Wang7c0c3b12013-01-11 16:59:33 +00001574 if (tfile->detached)
1575 return -EINVAL;
1576
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001577 dev = __dev_get_by_name(net, ifr->ifr_name);
1578 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001579 if (ifr->ifr_flags & IFF_TUN_EXCL)
1580 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001581 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1582 tun = netdev_priv(dev);
1583 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1584 tun = netdev_priv(dev);
1585 else
1586 return -EINVAL;
1587
Jason Wang8e6d91a2013-05-28 18:32:11 +00001588 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001589 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00001590 return -EINVAL;
1591
Jason Wangcde8b152012-10-31 19:46:01 +00001592 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001593 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001594 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001595 if (err < 0)
1596 return err;
1597
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001598 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001599 if (err < 0)
1600 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001601
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001602 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00001603 (tun->numqueues + tun->numdisabled > 1)) {
1604 /* One or more queue has already been attached, no need
1605 * to initialize the device again.
1606 */
1607 return 0;
1608 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 else {
1611 char *name;
1612 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001613 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1614 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001616 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001617 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001618 err = security_tun_dev_create();
1619 if (err < 0)
1620 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 /* Set dev type */
1623 if (ifr->ifr_flags & IFF_TUN) {
1624 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001625 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 name = "tun%d";
1627 } else if (ifr->ifr_flags & IFF_TAP) {
1628 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001629 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001631 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001632 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (*ifr->ifr_name)
1635 name = ifr->ifr_name;
1636
Jason Wangc8d68e62012-10-31 19:46:00 +00001637 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02001638 NET_NAME_UNKNOWN, tun_setup, queues,
1639 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (!dev)
1642 return -ENOMEM;
1643
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001644 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001645 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001646 dev->ifindex = tfile->ifindex;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 tun = netdev_priv(dev);
1649 tun->dev = dev;
1650 tun->flags = flags;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001651 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001652 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Jason Wang54f968d2012-10-31 19:45:57 +00001654 tun->filter_attached = false;
1655 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001656
Jason Wang96442e422012-10-31 19:46:02 +00001657 spin_lock_init(&tun->lock);
1658
Paul Moore5dbbaf22013-01-14 07:12:19 +00001659 err = security_tun_dev_alloc_security(&tun->security);
1660 if (err < 0)
1661 goto err_free_dev;
Paul Moore2b980db2009-08-28 18:12:43 -04001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001664 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001665
Michał Mirosław88255372011-04-19 06:13:10 +00001666 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08001667 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
1668 NETIF_F_HW_VLAN_STAG_TX;
Michał Mirosław88255372011-04-19 06:13:10 +00001669 dev->features = dev->hw_features;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09001670 dev->vlan_features = dev->features &
1671 ~(NETIF_F_HW_VLAN_CTAG_TX |
1672 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00001673
Jason Wang4008e972012-12-13 23:53:30 +00001674 INIT_LIST_HEAD(&tun->disabled);
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001675 err = tun_attach(tun, file, false);
Jason Wangeb0fb362012-12-02 17:19:45 +00001676 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001677 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 err = register_netdevice(tun->dev);
1680 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001681 goto err_detach;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001682
David Woodhouse980c9e82009-05-09 22:54:21 -07001683 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1684 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1685 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001686 pr_err("Failed to create tun sysfs files\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00001689 netif_carrier_on(tun->dev);
1690
Joe Perches6b8a66e2011-03-02 07:18:10 +00001691 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02001693 tun->flags = (tun->flags & ~TUN_FEATURES) |
1694 (ifr->ifr_flags & TUN_FEATURES);
Jason Wangc8d68e62012-10-31 19:46:00 +00001695
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001696 /* Make sure persistent devices do not get stuck in
1697 * xoff state.
1698 */
1699 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001700 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 strcpy(ifr->ifr_name, tun->dev->name);
1703 return 0;
1704
Jason Wang662ca432013-09-11 18:09:48 +08001705err_detach:
1706 tun_detach_all(dev);
1707err_free_flow:
1708 tun_flow_uninit(tun);
1709 security_tun_dev_free_security(tun->security);
1710err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return err;
1713}
1714
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001715static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00001716 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001717{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001718 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001719
1720 strcpy(ifr->ifr_name, tun->dev->name);
1721
David Woodhouse980c9e82009-05-09 22:54:21 -07001722 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001723
Mark McLoughline3b99552008-08-15 15:09:56 -07001724}
1725
Rusty Russell5228ddc2008-07-03 03:46:16 -07001726/* This is like a cut-down ethtool ops, except done via tun fd so no
1727 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001728static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001729{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001730 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001731
1732 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001733 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001734 arg &= ~TUN_F_CSUM;
1735
1736 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1737 if (arg & TUN_F_TSO_ECN) {
1738 features |= NETIF_F_TSO_ECN;
1739 arg &= ~TUN_F_TSO_ECN;
1740 }
1741 if (arg & TUN_F_TSO4)
1742 features |= NETIF_F_TSO;
1743 if (arg & TUN_F_TSO6)
1744 features |= NETIF_F_TSO6;
1745 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1746 }
1747 }
1748
1749 /* This gives the user a way to test for new features in future by
1750 * trying to set them. */
1751 if (arg)
1752 return -EINVAL;
1753
Michał Mirosław88255372011-04-19 06:13:10 +00001754 tun->set_features = features;
1755 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001756
1757 return 0;
1758}
1759
Jason Wangc8d68e62012-10-31 19:46:00 +00001760static void tun_detach_filter(struct tun_struct *tun, int n)
1761{
1762 int i;
1763 struct tun_file *tfile;
1764
1765 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001766 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001767 sk_detach_filter(tfile->socket.sk);
1768 }
1769
1770 tun->filter_attached = false;
1771}
1772
1773static int tun_attach_filter(struct tun_struct *tun)
1774{
1775 int i, ret = 0;
1776 struct tun_file *tfile;
1777
1778 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001779 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001780 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1781 if (ret) {
1782 tun_detach_filter(tun, i);
1783 return ret;
1784 }
1785 }
1786
1787 tun->filter_attached = true;
1788 return ret;
1789}
1790
1791static void tun_set_sndbuf(struct tun_struct *tun)
1792{
1793 struct tun_file *tfile;
1794 int i;
1795
1796 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001797 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001798 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1799 }
1800}
1801
Jason Wangcde8b152012-10-31 19:46:01 +00001802static int tun_set_queue(struct file *file, struct ifreq *ifr)
1803{
1804 struct tun_file *tfile = file->private_data;
1805 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00001806 int ret = 0;
1807
1808 rtnl_lock();
1809
1810 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00001811 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001812 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00001813 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001814 goto unlock;
1815 }
1816 ret = security_tun_dev_attach_queue(tun->security);
1817 if (ret < 0)
1818 goto unlock;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001819 ret = tun_attach(tun, file, false);
Jason Wang4008e972012-12-13 23:53:30 +00001820 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001821 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001822 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00001823 ret = -EINVAL;
1824 else
1825 __tun_detach(tfile, false);
1826 } else
Jason Wangcde8b152012-10-31 19:46:01 +00001827 ret = -EINVAL;
1828
Paul Moore5dbbaf22013-01-14 07:12:19 +00001829unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00001830 rtnl_unlock();
1831 return ret;
1832}
1833
Arnd Bergmann50857e22009-11-06 22:52:32 -08001834static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1835 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001837 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001838 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 void __user* argp = (void __user*)arg;
1840 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001841 kuid_t owner;
1842 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001843 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001844 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001845 unsigned int ifindex;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001846 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Jason Wangcde8b152012-10-31 19:46:01 +00001848 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001849 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001851 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001852 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001853 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001854 if (cmd == TUNGETFEATURES) {
1855 /* Currently this just means: "what IFF flags are valid?".
1856 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e02014-11-19 14:44:40 +02001857 * TUNSETIFF.
1858 */
1859 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001860 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001861 } else if (cmd == TUNSETQUEUE)
1862 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001863
Jason Wangc8d68e62012-10-31 19:46:00 +00001864 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001865 rtnl_lock();
1866
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001867 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1870
Herbert Xu876bfd42009-08-06 14:22:44 +00001871 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Herbert Xu876bfd42009-08-06 14:22:44 +00001873 if (ret)
1874 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Arnd Bergmann50857e22009-11-06 22:52:32 -08001876 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001877 ret = -EFAULT;
1878 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001880 if (cmd == TUNSETIFINDEX) {
1881 ret = -EPERM;
1882 if (tun)
1883 goto unlock;
1884
1885 ret = -EFAULT;
1886 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1887 goto unlock;
1888
1889 ret = 0;
1890 tfile->ifindex = ifindex;
1891 goto unlock;
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Herbert Xu876bfd42009-08-06 14:22:44 +00001894 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001896 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Jason Wang1e588332012-10-31 19:45:56 +00001898 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Eric W. Biederman631ab462009-01-20 11:00:40 +00001900 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001902 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001903 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001904
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001905 if (tfile->detached)
1906 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001907 if (!tfile->socket.sk->sk_filter)
1908 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001909
Arnd Bergmann50857e22009-11-06 22:52:32 -08001910 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001911 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001912 break;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 case TUNSETNOCSUM:
1915 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Michał Mirosław88255372011-04-19 06:13:10 +00001917 /* [unimplemented] */
1918 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001919 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 break;
1921
1922 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001923 /* Disable/Enable persist mode. Keep an extra reference to the
1924 * module to prevent the module being unprobed.
1925 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001926 if (arg && !(tun->flags & IFF_PERSIST)) {
1927 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001928 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00001929 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001930 if (!arg && (tun->flags & IFF_PERSIST)) {
1931 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001932 module_put(THIS_MODULE);
1933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Joe Perches6b8a66e2011-03-02 07:18:10 +00001935 tun_debug(KERN_INFO, tun, "persist %s\n",
1936 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
1938
1939 case TUNSETOWNER:
1940 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001941 owner = make_kuid(current_user_ns(), arg);
1942 if (!uid_valid(owner)) {
1943 ret = -EINVAL;
1944 break;
1945 }
1946 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001947 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001948 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 break;
1950
Guido Guenther8c644622007-07-02 22:50:25 -07001951 case TUNSETGROUP:
1952 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001953 group = make_kgid(current_user_ns(), arg);
1954 if (!gid_valid(group)) {
1955 ret = -EINVAL;
1956 break;
1957 }
1958 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001959 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001960 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001961 break;
1962
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001963 case TUNSETLINK:
1964 /* Only allow setting the type when the interface is down */
1965 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001966 tun_debug(KERN_INFO, tun,
1967 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001968 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001969 } else {
1970 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001971 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1972 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001973 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001974 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001975 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977#ifdef TUN_DEBUG
1978 case TUNSETDEBUG:
1979 tun->debug = arg;
1980 break;
1981#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001982 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001983 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001984 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001985
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001986 case TUNSETTXFILTER:
1987 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001988 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001989 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001990 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001991 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001995 /* Get hw address */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001996 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1997 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001998 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001999 ret = -EFAULT;
2000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07002003 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00002004 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2005 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08002006
Kim B. Heino40102372008-02-29 12:26:21 -08002007 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002008 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002009
2010 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002011 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002012 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2013 ret = -EFAULT;
2014 break;
2015
2016 case TUNSETSNDBUF:
2017 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2018 ret = -EFAULT;
2019 break;
2020 }
2021
Jason Wangc8d68e62012-10-31 19:46:00 +00002022 tun->sndbuf = sndbuf;
2023 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002024 break;
2025
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002026 case TUNGETVNETHDRSZ:
2027 vnet_hdr_sz = tun->vnet_hdr_sz;
2028 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2029 ret = -EFAULT;
2030 break;
2031
2032 case TUNSETVNETHDRSZ:
2033 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2034 ret = -EFAULT;
2035 break;
2036 }
2037 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2038 ret = -EINVAL;
2039 break;
2040 }
2041
2042 tun->vnet_hdr_sz = vnet_hdr_sz;
2043 break;
2044
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002045 case TUNATTACHFILTER:
2046 /* Can be set only for TAPs */
2047 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002048 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002049 break;
2050 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002051 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002052 break;
2053
Jason Wangc8d68e62012-10-31 19:46:00 +00002054 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002055 break;
2056
2057 case TUNDETACHFILTER:
2058 /* Can be set only for TAPs */
2059 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002060 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002061 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002062 ret = 0;
2063 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002064 break;
2065
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002066 case TUNGETFILTER:
2067 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002068 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002069 break;
2070 ret = -EFAULT;
2071 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2072 break;
2073 ret = 0;
2074 break;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002077 ret = -EINVAL;
2078 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Herbert Xu876bfd42009-08-06 14:22:44 +00002081unlock:
2082 rtnl_unlock();
2083 if (tun)
2084 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002085 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Arnd Bergmann50857e22009-11-06 22:52:32 -08002088static long tun_chr_ioctl(struct file *file,
2089 unsigned int cmd, unsigned long arg)
2090{
2091 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2092}
2093
2094#ifdef CONFIG_COMPAT
2095static long tun_chr_compat_ioctl(struct file *file,
2096 unsigned int cmd, unsigned long arg)
2097{
2098 switch (cmd) {
2099 case TUNSETIFF:
2100 case TUNGETIFF:
2101 case TUNSETTXFILTER:
2102 case TUNGETSNDBUF:
2103 case TUNSETSNDBUF:
2104 case SIOCGIFHWADDR:
2105 case SIOCSIFHWADDR:
2106 arg = (unsigned long)compat_ptr(arg);
2107 break;
2108 default:
2109 arg = (compat_ulong_t)arg;
2110 break;
2111 }
2112
2113 /*
2114 * compat_ifreq is shorter than ifreq, so we must not access beyond
2115 * the end of that structure. All fields that are used in this
2116 * driver are compatible though, we don't need to convert the
2117 * contents.
2118 */
2119 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2120}
2121#endif /* CONFIG_COMPAT */
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123static int tun_chr_fasync(int fd, struct file *file, int on)
2124{
Jason Wang54f968d2012-10-31 19:45:57 +00002125 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 int ret;
2127
Jason Wang54f968d2012-10-31 19:45:57 +00002128 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002129 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (on) {
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002132 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00002133 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002134 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002135 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002136 ret = 0;
2137out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002138 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141static int tun_chr_open(struct inode *inode, struct file * file)
2142{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002143 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002144
Joe Perches6b8a66e2011-03-02 07:18:10 +00002145 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002146
Jason Wang54f968d2012-10-31 19:45:57 +00002147 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2148 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002149 if (!tfile)
2150 return -ENOMEM;
Monam Agarwalc9566742014-03-24 00:02:32 +05302151 RCU_INIT_POINTER(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002152 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00002153 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002154 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002155
Jason Wang54f968d2012-10-31 19:45:57 +00002156 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07002157 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00002158
2159 tfile->socket.file = file;
2160 tfile->socket.ops = &tun_socket_ops;
2161
2162 sock_init_data(&tfile->socket, &tfile->sk);
2163 sk_change_net(&tfile->sk, tfile->net);
2164
2165 tfile->sk.sk_write_space = tun_sock_write_space;
2166 tfile->sk.sk_sndbuf = INT_MAX;
2167
Eric W. Biederman631ab462009-01-20 11:00:40 +00002168 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00002169 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
Jason Wang4008e972012-12-13 23:53:30 +00002170 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002171
Jason Wang19a6afb2013-06-08 14:17:41 +08002172 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return 0;
2175}
2176
2177static int tun_chr_close(struct inode *inode, struct file *file)
2178{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002179 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00002180 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Jason Wangc8d68e62012-10-31 19:46:00 +00002182 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00002183 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 return 0;
2186}
2187
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002188#ifdef CONFIG_PROC_FS
2189static int tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
2190{
2191 struct tun_struct *tun;
2192 struct ifreq ifr;
2193
2194 memset(&ifr, 0, sizeof(ifr));
2195
2196 rtnl_lock();
2197 tun = tun_get(f);
2198 if (tun)
2199 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2200 rtnl_unlock();
2201
2202 if (tun)
2203 tun_put(tun);
2204
2205 return seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
2206}
2207#endif
2208
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002209static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002210 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002212 .read = do_sync_read,
2213 .aio_read = tun_chr_aio_read,
2214 .write = do_sync_write,
2215 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002217 .unlocked_ioctl = tun_chr_ioctl,
2218#ifdef CONFIG_COMPAT
2219 .compat_ioctl = tun_chr_compat_ioctl,
2220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 .open = tun_chr_open,
2222 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002223 .fasync = tun_chr_fasync,
2224#ifdef CONFIG_PROC_FS
2225 .show_fdinfo = tun_chr_show_fdinfo,
2226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227};
2228
2229static struct miscdevice tun_miscdev = {
2230 .minor = TUN_MINOR,
2231 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002232 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234};
2235
2236/* ethtool interface */
2237
2238static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2239{
2240 cmd->supported = 0;
2241 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00002242 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 cmd->duplex = DUPLEX_FULL;
2244 cmd->port = PORT_TP;
2245 cmd->phy_address = 0;
2246 cmd->transceiver = XCVR_INTERNAL;
2247 cmd->autoneg = AUTONEG_DISABLE;
2248 cmd->maxtxpkt = 0;
2249 cmd->maxrxpkt = 0;
2250 return 0;
2251}
2252
2253static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2254{
2255 struct tun_struct *tun = netdev_priv(dev);
2256
Rick Jones33a5ba12011-11-15 14:59:53 +00002257 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2258 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002261 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00002262 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002264 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00002265 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 break;
2267 }
2268}
2269
2270static u32 tun_get_msglevel(struct net_device *dev)
2271{
2272#ifdef TUN_DEBUG
2273 struct tun_struct *tun = netdev_priv(dev);
2274 return tun->debug;
2275#else
2276 return -EOPNOTSUPP;
2277#endif
2278}
2279
2280static void tun_set_msglevel(struct net_device *dev, u32 value)
2281{
2282#ifdef TUN_DEBUG
2283 struct tun_struct *tun = netdev_priv(dev);
2284 tun->debug = value;
2285#endif
2286}
2287
Jeff Garzik7282d492006-09-13 14:30:00 -04002288static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 .get_settings = tun_get_settings,
2290 .get_drvinfo = tun_get_drvinfo,
2291 .get_msglevel = tun_get_msglevel,
2292 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002293 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002294 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295};
2296
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298static int __init tun_init(void)
2299{
2300 int ret = 0;
2301
Joe Perches6b8a66e2011-03-02 07:18:10 +00002302 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2303 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002305 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002306 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002307 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002308 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002309 }
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002312 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002313 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002314 goto err_misc;
2315 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002316 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002317err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002318 rtnl_link_unregister(&tun_link_ops);
2319err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return ret;
2321}
2322
2323static void tun_cleanup(void)
2324{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002325 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002326 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002329/* Get an underlying socket object from tun file. Returns error unless file is
2330 * attached to a device. The returned object works like a packet socket, it
2331 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2332 * holding a reference to the file for as long as the socket is in use. */
2333struct socket *tun_get_socket(struct file *file)
2334{
Jason Wang6e914fc2012-10-31 19:45:58 +00002335 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002336 if (file->f_op != &tun_fops)
2337 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002338 tfile = file->private_data;
2339 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002340 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002341 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002342}
2343EXPORT_SYMBOL_GPL(tun_get_socket);
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345module_init(tun_init);
2346module_exit(tun_cleanup);
2347MODULE_DESCRIPTION(DRV_DESCRIPTION);
2348MODULE_AUTHOR(DRV_COPYRIGHT);
2349MODULE_LICENSE("GPL");
2350MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002351MODULE_ALIAS("devname:net/tun");