blob: c052bd6b2f234899aa98226308025d770dcece94 [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. Tsirkin031f5e032014-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. Tsirkin031f5e032014-11-19 14:44:40 +0200112
113#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300114 IFF_VNET_LE | IFF_MULTI_QUEUE)
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000115#define GOODCOPY_LEN 128
116
Max Krasnyanskyf271b2cc2008-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 Krasnyanskyf271b2cc2008-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
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +0300208static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
209{
210 return __virtio16_to_cpu(tun->flags & IFF_VNET_LE, val);
211}
212
213static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
214{
215 return __cpu_to_virtio16(tun->flags & IFF_VNET_LE, val);
216}
217
Jason Wang96442e422012-10-31 19:46:02 +0000218static inline u32 tun_hashfn(u32 rxhash)
219{
220 return rxhash & 0x3ff;
221}
222
223static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
224{
225 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000226
Sasha Levinb67bfe02013-02-27 17:06:00 -0800227 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000228 if (e->rxhash == rxhash)
229 return e;
230 }
231 return NULL;
232}
233
234static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
235 struct hlist_head *head,
236 u32 rxhash, u16 queue_index)
237{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000238 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
239
Jason Wang96442e422012-10-31 19:46:02 +0000240 if (e) {
241 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
242 rxhash, queue_index);
243 e->updated = jiffies;
244 e->rxhash = rxhash;
Tom Herbert9bc88932013-12-22 18:54:32 +0800245 e->rps_rxhash = 0;
Jason Wang96442e422012-10-31 19:46:02 +0000246 e->queue_index = queue_index;
247 e->tun = tun;
248 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000249 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000250 }
251 return e;
252}
253
Jason Wang96442e422012-10-31 19:46:02 +0000254static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
255{
256 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
257 e->rxhash, e->queue_index);
Tom Herbert9bc88932013-12-22 18:54:32 +0800258 sock_rps_reset_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000259 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000260 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000261 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000262}
263
264static void tun_flow_flush(struct tun_struct *tun)
265{
266 int i;
267
268 spin_lock_bh(&tun->lock);
269 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
270 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800271 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000272
Sasha Levinb67bfe02013-02-27 17:06:00 -0800273 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000274 tun_flow_delete(tun, e);
275 }
276 spin_unlock_bh(&tun->lock);
277}
278
279static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
280{
281 int i;
282
283 spin_lock_bh(&tun->lock);
284 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
285 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800286 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000287
Sasha Levinb67bfe02013-02-27 17:06:00 -0800288 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000289 if (e->queue_index == queue_index)
290 tun_flow_delete(tun, e);
291 }
292 }
293 spin_unlock_bh(&tun->lock);
294}
295
296static void tun_flow_cleanup(unsigned long data)
297{
298 struct tun_struct *tun = (struct tun_struct *)data;
299 unsigned long delay = tun->ageing_time;
300 unsigned long next_timer = jiffies + delay;
301 unsigned long count = 0;
302 int i;
303
304 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
305
306 spin_lock_bh(&tun->lock);
307 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
308 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800309 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000310
Sasha Levinb67bfe02013-02-27 17:06:00 -0800311 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000312 unsigned long this_timer;
313 count++;
314 this_timer = e->updated + delay;
315 if (time_before_eq(this_timer, jiffies))
316 tun_flow_delete(tun, e);
317 else if (time_before(this_timer, next_timer))
318 next_timer = this_timer;
319 }
320 }
321
322 if (count)
323 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
324 spin_unlock_bh(&tun->lock);
325}
326
Eric Dumazet49974422012-12-12 19:22:57 +0000327static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000328 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000329{
330 struct hlist_head *head;
331 struct tun_flow_entry *e;
332 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000333 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000334
335 if (!rxhash)
336 return;
337 else
338 head = &tun->flows[tun_hashfn(rxhash)];
339
340 rcu_read_lock();
341
Jason Wang9e857222013-01-28 01:05:19 +0000342 /* We may get a very small possibility of OOO during switching, not
343 * worth to optimize.*/
344 if (tun->numqueues == 1 || tfile->detached)
Jason Wang96442e422012-10-31 19:46:02 +0000345 goto unlock;
346
347 e = tun_flow_find(head, rxhash);
348 if (likely(e)) {
349 /* TODO: keep queueing to old queue until it's empty? */
350 e->queue_index = queue_index;
351 e->updated = jiffies;
Tom Herbert9bc88932013-12-22 18:54:32 +0800352 sock_rps_record_flow_hash(e->rps_rxhash);
Jason Wang96442e422012-10-31 19:46:02 +0000353 } else {
354 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000355 if (!tun_flow_find(head, rxhash) &&
356 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000357 tun_flow_create(tun, head, rxhash, queue_index);
358
359 if (!timer_pending(&tun->flow_gc_timer))
360 mod_timer(&tun->flow_gc_timer,
361 round_jiffies_up(jiffies + delay));
362 spin_unlock_bh(&tun->lock);
363 }
364
365unlock:
366 rcu_read_unlock();
367}
368
Tom Herbert9bc88932013-12-22 18:54:32 +0800369/**
370 * Save the hash received in the stack receive path and update the
371 * flow_hash table accordingly.
372 */
373static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
374{
375 if (unlikely(e->rps_rxhash != hash)) {
376 sock_rps_reset_flow_hash(e->rps_rxhash);
377 e->rps_rxhash = hash;
378 }
379}
380
Jason Wangc8d68e62012-10-31 19:46:00 +0000381/* We try to identify a flow through its rxhash first. The reason that
stephen hemminger92d4ea62013-12-05 20:42:58 -0800382 * we do not check rxq no. is because some cards(e.g 82599), chooses
Jason Wangc8d68e62012-10-31 19:46:00 +0000383 * the rxq based on the txq where the last packet of the flow comes. As
384 * the userspace application move between processors, we may get a
385 * different rxq no. here. If we could not get rxhash, then we would
386 * hope the rxq no. may help here.
387 */
Jason Wangf663dd92014-01-10 16:18:26 +0800388static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100389 void *accel_priv, select_queue_fallback_t fallback)
Jason Wangc8d68e62012-10-31 19:46:00 +0000390{
391 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000392 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000393 u32 txq = 0;
394 u32 numqueues = 0;
395
396 rcu_read_lock();
Jason Wang92bb73e2013-06-05 16:44:57 +0800397 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000398
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800399 txq = skb_get_hash(skb);
Jason Wangc8d68e62012-10-31 19:46:00 +0000400 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000401 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
Tom Herbert9bc88932013-12-22 18:54:32 +0800402 if (e) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800403 tun_flow_save_rps_rxhash(e, txq);
Zhi Yong Wufbe4d452014-01-02 13:24:28 +0800404 txq = e->queue_index;
Tom Herbert9bc88932013-12-22 18:54:32 +0800405 } else
Jason Wang96442e422012-10-31 19:46:02 +0000406 /* use multiply and shift instead of expensive divide */
407 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000408 } else if (likely(skb_rx_queue_recorded(skb))) {
409 txq = skb_get_rx_queue(skb);
410 while (unlikely(txq >= numqueues))
411 txq -= numqueues;
412 }
413
414 rcu_read_unlock();
415 return txq;
416}
417
Jason Wangcde8b152012-10-31 19:46:01 +0000418static inline bool tun_not_capable(struct tun_struct *tun)
419{
420 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000421 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000422
423 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
424 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000425 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000426}
427
Jason Wangc8d68e62012-10-31 19:46:00 +0000428static void tun_set_real_num_queues(struct tun_struct *tun)
429{
430 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
431 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
432}
433
Jason Wang4008e972012-12-13 23:53:30 +0000434static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
435{
436 tfile->detached = tun;
437 list_add_tail(&tfile->next, &tun->disabled);
438 ++tun->numdisabled;
439}
440
Jason Wangd32649d2012-12-18 11:00:27 +0800441static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000442{
443 struct tun_struct *tun = tfile->detached;
444
445 tfile->detached = NULL;
446 list_del_init(&tfile->next);
447 --tun->numdisabled;
448 return tun;
449}
450
Jason Wang4bfb0512013-09-05 17:53:59 +0800451static void tun_queue_purge(struct tun_file *tfile)
452{
453 skb_queue_purge(&tfile->sk.sk_receive_queue);
454 skb_queue_purge(&tfile->sk.sk_error_queue);
455}
456
Jason Wangc8d68e62012-10-31 19:46:00 +0000457static void __tun_detach(struct tun_file *tfile, bool clean)
458{
459 struct tun_file *ntfile;
460 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000461
Jason Wangb8deabd2013-01-11 16:59:32 +0000462 tun = rtnl_dereference(tfile->tun);
463
Jason Wang9e857222013-01-28 01:05:19 +0000464 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000465 u16 index = tfile->queue_index;
466 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000467
468 rcu_assign_pointer(tun->tfiles[index],
469 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000470 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000471 ntfile->queue_index = index;
472
473 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000474 if (clean) {
Monam Agarwalc9566742014-03-24 00:02:32 +0530475 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000476 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000477 } else
Jason Wang4008e972012-12-13 23:53:30 +0000478 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000479
480 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000481 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000482 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800483 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000484 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000485 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000486 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000487 sock_put(&tfile->sk);
488 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000489
490 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000491 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
492 netif_carrier_off(tun->dev);
493
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200494 if (!(tun->flags & IFF_PERSIST) &&
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000495 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000496 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000497 }
Jason Wang4008e972012-12-13 23:53:30 +0000498
Jason Wangc8d68e62012-10-31 19:46:00 +0000499 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
500 &tfile->socket.flags));
501 sk_release_kernel(&tfile->sk);
502 }
503}
504
505static void tun_detach(struct tun_file *tfile, bool clean)
506{
507 rtnl_lock();
508 __tun_detach(tfile, clean);
509 rtnl_unlock();
510}
511
512static void tun_detach_all(struct net_device *dev)
513{
514 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000515 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000516 int i, n = tun->numqueues;
517
518 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000519 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000520 BUG_ON(!tfile);
Xi Wang9e641bd2014-05-16 15:11:48 -0700521 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530522 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wangc8d68e62012-10-31 19:46:00 +0000523 --tun->numqueues;
524 }
Jason Wang9e857222013-01-28 01:05:19 +0000525 list_for_each_entry(tfile, &tun->disabled, next) {
Xi Wang9e641bd2014-05-16 15:11:48 -0700526 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Monam Agarwalc9566742014-03-24 00:02:32 +0530527 RCU_INIT_POINTER(tfile->tun, NULL);
Jason Wang9e857222013-01-28 01:05:19 +0000528 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000529 BUG_ON(tun->numqueues != 0);
530
531 synchronize_net();
532 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000533 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000534 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800535 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000536 sock_put(&tfile->sk);
537 }
Jason Wang4008e972012-12-13 23:53:30 +0000538 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
539 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800540 tun_queue_purge(tfile);
Jason Wang4008e972012-12-13 23:53:30 +0000541 sock_put(&tfile->sk);
542 }
543 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000544
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200545 if (tun->flags & IFF_PERSIST)
Jason Wangdd38bd82013-01-11 16:59:34 +0000546 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000547}
548
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400549static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000550{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000551 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000552 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000553
Paul Moore5dbbaf22013-01-14 07:12:19 +0000554 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
555 if (err < 0)
556 goto out;
557
Eric W. Biederman38231b72009-01-20 11:02:28 +0000558 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000559 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000560 goto out;
561
562 err = -EBUSY;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200563 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
Jason Wangc8d68e62012-10-31 19:46:00 +0000564 goto out;
565
566 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000567 if (!tfile->detached &&
568 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000569 goto out;
570
571 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000572
stephen hemminger92d4ea62013-12-05 20:42:58 -0800573 /* Re-attach the filter to persist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400574 if (!skip_filter && (tun->filter_attached == true)) {
Jason Wang54f968d2012-10-31 19:45:57 +0000575 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
576 if (!err)
577 goto out;
578 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000579 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000580 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000581 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000582 tun->numqueues++;
583
Jason Wang4008e972012-12-13 23:53:30 +0000584 if (tfile->detached)
585 tun_enable_queue(tfile);
586 else
587 sock_hold(&tfile->sk);
588
Jason Wangc8d68e62012-10-31 19:46:00 +0000589 tun_set_real_num_queues(tun);
590
Jason Wangc8d68e62012-10-31 19:46:00 +0000591 /* device is allowed to go away first, so no need to hold extra
592 * refcnt.
593 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000594
Eric W. Biederman38231b72009-01-20 11:02:28 +0000595out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000596 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000597}
598
Eric W. Biederman631ab462009-01-20 11:00:40 +0000599static struct tun_struct *__tun_get(struct tun_file *tfile)
600{
Jason Wang6e914fc2012-10-31 19:45:58 +0000601 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000602
Jason Wang6e914fc2012-10-31 19:45:58 +0000603 rcu_read_lock();
604 tun = rcu_dereference(tfile->tun);
605 if (tun)
606 dev_hold(tun->dev);
607 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000608
609 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000610}
611
612static struct tun_struct *tun_get(struct file *file)
613{
614 return __tun_get(file->private_data);
615}
616
617static void tun_put(struct tun_struct *tun)
618{
Jason Wang6e914fc2012-10-31 19:45:58 +0000619 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000620}
621
Joe Perches6b8a66e2011-03-02 07:18:10 +0000622/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700623static void addr_hash_set(u32 *mask, const u8 *addr)
624{
625 int n = ether_crc(ETH_ALEN, addr) >> 26;
626 mask[n >> 5] |= (1 << (n & 31));
627}
628
629static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
630{
631 int n = ether_crc(ETH_ALEN, addr) >> 26;
632 return mask[n >> 5] & (1 << (n & 31));
633}
634
635static int update_filter(struct tap_filter *filter, void __user *arg)
636{
637 struct { u8 u[ETH_ALEN]; } *addr;
638 struct tun_filter uf;
639 int err, alen, n, nexact;
640
641 if (copy_from_user(&uf, arg, sizeof(uf)))
642 return -EFAULT;
643
644 if (!uf.count) {
645 /* Disabled */
646 filter->count = 0;
647 return 0;
648 }
649
650 alen = ETH_ALEN * uf.count;
651 addr = kmalloc(alen, GFP_KERNEL);
652 if (!addr)
653 return -ENOMEM;
654
655 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
656 err = -EFAULT;
657 goto done;
658 }
659
660 /* The filter is updated without holding any locks. Which is
661 * perfectly safe. We disable it first and in the worst
662 * case we'll accept a few undesired packets. */
663 filter->count = 0;
664 wmb();
665
666 /* Use first set of addresses as an exact filter */
667 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
668 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
669
670 nexact = n;
671
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800672 /* Remaining multicast addresses are hashed,
673 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700674 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800675 for (; n < uf.count; n++) {
676 if (!is_multicast_ether_addr(addr[n].u)) {
677 err = 0; /* no filter */
678 goto done;
679 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700680 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800681 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700682
683 /* For ALLMULTI just set the mask to all ones.
684 * This overrides the mask populated above. */
685 if ((uf.flags & TUN_FLT_ALLMULTI))
686 memset(filter->mask, ~0, sizeof(filter->mask));
687
688 /* Now enable the filter */
689 wmb();
690 filter->count = nexact;
691
692 /* Return the number of exact filters */
693 err = nexact;
694
695done:
696 kfree(addr);
697 return err;
698}
699
700/* Returns: 0 - drop, !=0 - accept */
701static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
702{
703 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
704 * at this point. */
705 struct ethhdr *eh = (struct ethhdr *) skb->data;
706 int i;
707
708 /* Exact match */
709 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000710 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700711 return 1;
712
713 /* Inexact match (multicast only) */
714 if (is_multicast_ether_addr(eh->h_dest))
715 return addr_hash_test(filter->mask, eh->h_dest);
716
717 return 0;
718}
719
720/*
721 * Checks whether the packet is accepted or not.
722 * Returns: 0 - drop, !=0 - accept
723 */
724static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
725{
726 if (!filter->count)
727 return 1;
728
729 return run_filter(filter, skb);
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/* Network device part of the driver */
733
Jeff Garzik7282d492006-09-13 14:30:00 -0400734static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000736/* Net device detach from fd. */
737static void tun_net_uninit(struct net_device *dev)
738{
Jason Wangc8d68e62012-10-31 19:46:00 +0000739 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/* Net device open. */
743static int tun_net_open(struct net_device *dev)
744{
Jason Wangc8d68e62012-10-31 19:46:00 +0000745 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747}
748
749/* Net device close. */
750static int tun_net_close(struct net_device *dev)
751{
Jason Wangc8d68e62012-10-31 19:46:00 +0000752 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
754}
755
756/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000757static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000760 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000761 struct tun_file *tfile;
Dominic Curranfa358642014-01-22 03:03:23 +0000762 u32 numqueues = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Jason Wang6e914fc2012-10-31 19:45:58 +0000764 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000765 tfile = rcu_dereference(tun->tfiles[txq]);
Dominic Curranfa358642014-01-22 03:03:23 +0000766 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* Drop packet if interface is not attached */
Dominic Curranfa358642014-01-22 03:03:23 +0000769 if (txq >= numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto drop;
771
Dominic Curranfa358642014-01-22 03:03:23 +0000772 if (numqueues == 1) {
Tom Herbert9bc88932013-12-22 18:54:32 +0800773 /* Select queue was not called for the skbuff, so we extract the
774 * RPS hash and save it into the flow_table here.
775 */
776 __u32 rxhash;
777
778 rxhash = skb_get_hash(skb);
779 if (rxhash) {
780 struct tun_flow_entry *e;
781 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
782 rxhash);
783 if (e)
784 tun_flow_save_rps_rxhash(e, rxhash);
785 }
786 }
787
Jason Wang6e914fc2012-10-31 19:45:58 +0000788 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
789
Jason Wangc8d68e62012-10-31 19:46:00 +0000790 BUG_ON(!tfile);
791
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700792 /* Drop if the filter does not like it.
793 * This is a noop if the filter is disabled.
794 * Filter can be enabled only for the TAP devices. */
795 if (!check_filter(&tun->txflt, skb))
796 goto drop;
797
Jason Wang54f968d2012-10-31 19:45:57 +0000798 if (tfile->socket.sk->sk_filter &&
799 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000800 goto drop;
801
Rami Rosen36fe8c092012-11-25 22:07:40 +0000802 /* Limit the number of packets queued by dividing txq length with the
Jason Wangc8d68e62012-10-31 19:46:00 +0000803 * number of queues.
804 */
Dominic Curranfa358642014-01-22 03:03:23 +0000805 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
806 >= dev->tx_queue_len)
Michael S. Tsirkin5d097102012-12-03 10:07:14 +0000807 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Jason Wang7bf66302013-09-05 17:54:00 +0800809 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
810 goto drop;
811
Richard Cochraneda29772013-07-19 19:40:10 +0200812 if (skb->sk) {
813 sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
814 sw_tx_timestamp(skb);
815 }
816
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000817 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800818 * for indefinite time.
819 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000820 skb_orphan(skb);
821
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000822 nf_reset(skb);
823
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700824 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000825 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000828 if (tfile->flags & TUN_FASYNC)
829 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
Xi Wang9e641bd2014-05-16 15:11:48 -0700830 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
Jason Wang6e914fc2012-10-31 19:45:58 +0000831
832 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000833 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700836 dev->stats.tx_dropped++;
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000837 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000839 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000840 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700843static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700845 /*
846 * This callback is supposed to deal with mc filter in
847 * _rx_ path and has nothing to do with the _tx_ path.
848 * In rx path we always accept everything userspace gives us.
849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Ed Swierk4885a502007-09-16 12:21:38 -0700852#define MIN_MTU 68
853#define MAX_MTU 65535
854
855static int
856tun_net_change_mtu(struct net_device *dev, int new_mtu)
857{
858 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
859 return -EINVAL;
860 dev->mtu = new_mtu;
861 return 0;
862}
863
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000864static netdev_features_t tun_net_fix_features(struct net_device *dev,
865 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000866{
867 struct tun_struct *tun = netdev_priv(dev);
868
869 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
870}
Neil Hormanbebd0972011-06-15 05:25:01 +0000871#ifdef CONFIG_NET_POLL_CONTROLLER
872static void tun_poll_controller(struct net_device *dev)
873{
874 /*
875 * Tun only receives frames when:
876 * 1) the char device endpoint gets data from user space
877 * 2) the tun socket gets a sendmsg call from user space
stephen hemminger92d4ea62013-12-05 20:42:58 -0800878 * Since both of those are synchronous operations, we are guaranteed
Neil Hormanbebd0972011-06-15 05:25:01 +0000879 * never to have pending data when we poll for it
stephen hemminger92d4ea62013-12-05 20:42:58 -0800880 * so there is nothing to do here but return.
Neil Hormanbebd0972011-06-15 05:25:01 +0000881 * We need this though so netpoll recognizes us as an interface that
882 * supports polling, which enables bridge devices in virt setups to
883 * still use netconsole
884 */
885 return;
886}
887#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800888static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000889 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800890 .ndo_open = tun_net_open,
891 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800892 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800893 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000894 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000895 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000896#ifdef CONFIG_NET_POLL_CONTROLLER
897 .ndo_poll_controller = tun_poll_controller,
898#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800899};
900
901static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000902 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800903 .ndo_open = tun_net_open,
904 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800905 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800906 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000907 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000908 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800909 .ndo_set_mac_address = eth_mac_addr,
910 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000911 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000912#ifdef CONFIG_NET_POLL_CONTROLLER
913 .ndo_poll_controller = tun_poll_controller,
914#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800915};
916
Pavel Emelyanov944a1372013-06-11 17:01:08 +0400917static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +0000918{
919 int i;
920
Jason Wang96442e422012-10-31 19:46:02 +0000921 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
922 INIT_HLIST_HEAD(&tun->flows[i]);
923
924 tun->ageing_time = TUN_FLOW_EXPIRE;
925 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
926 mod_timer(&tun->flow_gc_timer,
927 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +0000928}
929
930static void tun_flow_uninit(struct tun_struct *tun)
931{
932 del_timer_sync(&tun->flow_gc_timer);
933 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +0000934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/* Initialize net device. */
937static void tun_net_init(struct net_device *dev)
938{
939 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200942 case IFF_TUN:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800943 dev->netdev_ops = &tun_netdev_ops;
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* Point-to-Point TUN Device */
946 dev->hard_header_len = 0;
947 dev->addr_len = 0;
948 dev->mtu = 1500;
949
950 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400951 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
953 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
954 break;
955
Michael S. Tsirkin40630b82014-11-19 15:17:31 +0200956 case IFF_TAP:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800957 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700959 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000960 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +0000961 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000963 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
966 break;
967 }
968}
969
970/* Character device part */
971
972/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000973static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400974{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000975 struct tun_file *tfile = file->private_data;
976 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000977 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800978 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000981 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jason Wang54f968d2012-10-31 19:45:57 +0000983 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000984
Joe Perches6b8a66e2011-03-02 07:18:10 +0000985 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Xi Wang9e641bd2014-05-16 15:11:48 -0700987 poll_wait(file, sk_sleep(sk), wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400988
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000989 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 mask |= POLLIN | POLLRDNORM;
991
Herbert Xu33dccbb2009-02-05 21:25:32 -0800992 if (sock_writeable(sk) ||
993 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
994 sock_writeable(sk)))
995 mask |= POLLOUT | POLLWRNORM;
996
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000997 if (tun->dev->reg_state != NETREG_REGISTERED)
998 mask = POLLERR;
999
Eric W. Biederman631ab462009-01-20 11:00:40 +00001000 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return mask;
1002}
1003
Rusty Russellf42157c2008-08-15 15:15:10 -07001004/* prepad is the amount to reserve at front. len is length after that.
1005 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +00001006static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001007 size_t prepad, size_t len,
1008 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -07001009{
Jason Wang54f968d2012-10-31 19:45:57 +00001010 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -07001011 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001012 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -07001013
1014 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -07001015 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001016 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -07001017
Herbert Xu33dccbb2009-02-05 21:25:32 -08001018 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07001019 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -07001020 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -08001021 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -07001022
1023 skb_reserve(skb, prepad);
1024 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001025 skb->data_len = len - linear;
1026 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -07001027
1028 return skb;
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +00001032static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1033 void *msg_control, const struct iovec *iv,
1034 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Harvey Harrison09640e632009-02-01 00:45:17 -08001036 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct sk_buff *skb;
Jason Wang3dd5c332013-07-10 13:43:27 +08001038 size_t len = total_len, align = NET_SKB_PAD, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -07001039 struct virtio_net_hdr gso = { 0 };
Jason Wang96f8d9e2013-11-13 14:00:39 +08001040 int good_linear;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001041 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001042 int copylen;
1043 bool zerocopy = false;
1044 int err;
Eric Dumazet49974422012-12-12 19:22:57 +00001045 u32 rxhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001047 if (!(tun->flags & IFF_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001048 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001050 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001052 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001054 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001057 if (tun->flags & IFF_VNET_HDR) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001058 if (len < tun->vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001059 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001060 len -= tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001061
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001062 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -07001063 return -EFAULT;
1064
Herbert Xu49091222009-06-08 00:20:01 -07001065 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001066 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1067 gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
Herbert Xu49091222009-06-08 00:20:01 -07001068
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001069 if (tun16_to_cpu(tun, gso.hdr_len) > len)
Rusty Russellf43798c2008-07-03 03:48:02 -07001070 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001071 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001072 }
1073
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001074 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
stephen hemmingera504b862011-06-08 14:33:07 +00001075 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001076 if (unlikely(len < ETH_HLEN ||
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001077 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001078 return -EINVAL;
1079 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001080
Jason Wang96f8d9e2013-11-13 14:00:39 +08001081 good_linear = SKB_MAX_HEAD(align);
1082
Jason Wang88529172013-07-18 10:55:15 +08001083 if (msg_control) {
1084 /* There are 256 bytes to be copied in skb, so there is
1085 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001086 * The rest of the buffer is mapped from userspace.
1087 */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001088 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
Jason Wang96f8d9e2013-11-13 14:00:39 +08001089 if (copylen > good_linear)
1090 copylen = good_linear;
Jason Wang3dd5c332013-07-10 13:43:27 +08001091 linear = copylen;
Jason Wang88529172013-07-18 10:55:15 +08001092 if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
1093 zerocopy = true;
1094 }
1095
1096 if (!zerocopy) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001097 copylen = len;
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001098 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
Jason Wang96f8d9e2013-11-13 14:00:39 +08001099 linear = good_linear;
1100 else
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001101 linear = tun16_to_cpu(tun, gso.hdr_len);
Jason Wang3dd5c332013-07-10 13:43:27 +08001102 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001103
Jason Wang3dd5c332013-07-10 13:43:27 +08001104 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001105 if (IS_ERR(skb)) {
1106 if (PTR_ERR(skb) != -EAGAIN)
1107 tun->dev->stats.rx_dropped++;
1108 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001111 if (zerocopy)
1112 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
Jason Wang88529172013-07-18 10:55:15 +08001113 else {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001114 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
Jason Wang88529172013-07-18 10:55:15 +08001115 if (!err && msg_control) {
1116 struct ubuf_info *uarg = msg_control;
1117 uarg->callback(uarg, false);
1118 }
1119 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001120
1121 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001122 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -08001123 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -08001125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Rusty Russellf43798c2008-07-03 03:48:02 -07001127 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001128 if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
1129 tun16_to_cpu(tun, gso.csum_offset))) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001130 tun->dev->stats.rx_frame_errors++;
1131 kfree_skb(skb);
1132 return -EINVAL;
1133 }
Michał Mirosław88255372011-04-19 06:13:10 +00001134 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001137 case IFF_TUN:
1138 if (tun->flags & IFF_NO_PI) {
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001139 switch (skb->data[0] & 0xf0) {
1140 case 0x40:
1141 pi.proto = htons(ETH_P_IP);
1142 break;
1143 case 0x60:
1144 pi.proto = htons(ETH_P_IPV6);
1145 break;
1146 default:
1147 tun->dev->stats.rx_dropped++;
1148 kfree_skb(skb);
1149 return -EINVAL;
1150 }
1151 }
1152
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001153 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001155 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001157 case IFF_TAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 skb->protocol = eth_type_trans(skb, tun->dev);
1159 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Ben Hutchings5188cd42014-10-30 18:27:17 +00001162 skb_reset_network_header(skb);
1163
Rusty Russellf43798c2008-07-03 03:48:02 -07001164 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1165 pr_debug("GSO!\n");
1166 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1167 case VIRTIO_NET_HDR_GSO_TCPV4:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001168 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Rusty Russellf43798c2008-07-03 03:48:02 -07001169 break;
1170 case VIRTIO_NET_HDR_GSO_TCPV6:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001171 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Rusty Russellf43798c2008-07-03 03:48:02 -07001172 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001173 case VIRTIO_NET_HDR_GSO_UDP:
Ben Hutchings3d0ad092014-10-30 18:27:12 +00001174 {
1175 static bool warned;
1176
1177 if (!warned) {
1178 warned = true;
1179 netdev_warn(tun->dev,
1180 "%s: using disabled UFO feature; please fix this program\n",
1181 current->comm);
1182 }
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001183 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Ben Hutchings5188cd42014-10-30 18:27:17 +00001184 if (skb->protocol == htons(ETH_P_IPV6))
1185 ipv6_proxy_select_ident(skb);
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001186 break;
Ben Hutchings3d0ad092014-10-30 18:27:12 +00001187 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001188 default:
1189 tun->dev->stats.rx_frame_errors++;
1190 kfree_skb(skb);
1191 return -EINVAL;
1192 }
1193
1194 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001195 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Rusty Russellf43798c2008-07-03 03:48:02 -07001196
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001197 skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
Rusty Russellf43798c2008-07-03 03:48:02 -07001198 if (skb_shinfo(skb)->gso_size == 0) {
1199 tun->dev->stats.rx_frame_errors++;
1200 kfree_skb(skb);
1201 return -EINVAL;
1202 }
1203
1204 /* Header must be checked, and gso_segs computed. */
1205 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1206 skb_shinfo(skb)->gso_segs = 0;
1207 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001208
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001209 /* copy skb_ubuf_info for callback when skb has no error */
1210 if (zerocopy) {
1211 skb_shinfo(skb)->destructor_arg = msg_control;
1212 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001213 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001214 }
1215
Jason Wang40893fd2013-03-26 23:11:22 +00001216 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001217
Tom Herbert3958afa1b2013-12-15 22:12:06 -08001218 rxhash = skb_get_hash(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001220
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001221 tun->dev->stats.rx_packets++;
1222 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Jason Wang9e857222013-01-28 01:05:19 +00001224 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001225 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001228static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1229 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001231 struct file *file = iocb->ki_filp;
Herbert Xuab46d7792009-02-14 20:46:39 -08001232 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001233 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001234 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (!tun)
1237 return -EBADFD;
1238
Joe Perches6b8a66e2011-03-02 07:18:10 +00001239 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Jason Wang54f968d2012-10-31 19:45:57 +00001241 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1242 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001243
1244 tun_put(tun);
1245 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001249static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001250 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001251 struct sk_buff *skb,
1252 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 struct tun_pi pi = { 0, skb->protocol };
1255 ssize_t total = 0;
Jason Wange6fd07c2013-12-11 13:08:33 +08001256 int vlan_offset = 0, copied;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001257 int vlan_hlen = 0;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001258 int vnet_hdr_sz = 0;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001259
1260 if (vlan_tx_tag_present(skb))
1261 vlan_hlen = VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001263 if (tun->flags & IFF_VNET_HDR)
Herbert Xu2eb783c2014-11-03 04:30:14 +08001264 vnet_hdr_sz = tun->vnet_hdr_sz;
1265
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001266 if (!(tun->flags & IFF_NO_PI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if ((len -= sizeof(pi)) < 0)
1268 return -EINVAL;
1269
Herbert Xu2eb783c2014-11-03 04:30:14 +08001270 if (len < skb->len + vlan_hlen + vnet_hdr_sz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /* Packet will be striped */
1272 pi.flags |= TUN_PKT_STRIP;
1273 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001274
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001275 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return -EFAULT;
1277 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Herbert Xu2eb783c2014-11-03 04:30:14 +08001280 if (vnet_hdr_sz) {
Rusty Russellf43798c2008-07-03 03:48:02 -07001281 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Herbert Xu2eb783c2014-11-03 04:30:14 +08001282 if ((len -= vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001283 return -EINVAL;
1284
1285 if (skb_is_gso(skb)) {
1286 struct skb_shared_info *sinfo = skb_shinfo(skb);
1287
1288 /* This is a hint as to how much should be linear. */
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001289 gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
1290 gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
Rusty Russellf43798c2008-07-03 03:48:02 -07001291 if (sinfo->gso_type & SKB_GSO_TCPV4)
1292 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1293 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1294 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001295 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001296 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001297 "0x%x, gso_size %d, hdr_len %d\n",
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001298 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1299 tun16_to_cpu(tun, gso.hdr_len));
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001300 print_hex_dump(KERN_ERR, "tun: ",
1301 DUMP_PREFIX_NONE,
1302 16, 1, skb->head,
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001303 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001304 WARN_ON_ONCE(1);
1305 return -EINVAL;
1306 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001307 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1308 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1309 } else
1310 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1311
1312 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1313 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michael S. Tsirkin56f0dcc2014-10-23 22:59:31 +03001314 gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
1315 vlan_hlen);
1316 gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
Jason Wang10a8d942011-06-10 00:56:17 +00001317 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1318 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001319 } /* else everything is zero */
1320
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001321 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1322 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001323 return -EFAULT;
Herbert Xu2eb783c2014-11-03 04:30:14 +08001324 total += vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001325 }
1326
Jason Wange6fd07c2013-12-11 13:08:33 +08001327 copied = total;
Herbert Xua8f9bfd2014-11-03 04:30:13 +08001328 len = min_t(int, skb->len + vlan_hlen, len);
1329 total += skb->len + vlan_hlen;
1330 if (vlan_hlen) {
Jason Wang6680ec62013-07-25 13:00:33 +08001331 int copy, ret;
1332 struct {
1333 __be16 h_vlan_proto;
1334 __be16 h_vlan_TCI;
1335 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Jason Wang6680ec62013-07-25 13:00:33 +08001337 veth.h_vlan_proto = skb->vlan_proto;
1338 veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Jason Wang6680ec62013-07-25 13:00:33 +08001340 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wang6680ec62013-07-25 13:00:33 +08001341
1342 copy = min_t(int, vlan_offset, len);
Jason Wange6fd07c2013-12-11 13:08:33 +08001343 ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
Jason Wang6680ec62013-07-25 13:00:33 +08001344 len -= copy;
Jason Wange6fd07c2013-12-11 13:08:33 +08001345 copied += copy;
Jason Wang6680ec62013-07-25 13:00:33 +08001346 if (ret || !len)
1347 goto done;
1348
1349 copy = min_t(int, sizeof(veth), len);
Jason Wange6fd07c2013-12-11 13:08:33 +08001350 ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
Jason Wang6680ec62013-07-25 13:00:33 +08001351 len -= copy;
Jason Wange6fd07c2013-12-11 13:08:33 +08001352 copied += copy;
Jason Wang6680ec62013-07-25 13:00:33 +08001353 if (ret || !len)
1354 goto done;
1355 }
1356
Jason Wange6fd07c2013-12-11 13:08:33 +08001357 skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
Jason Wang6680ec62013-07-25 13:00:33 +08001358
1359done:
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001360 tun->dev->stats.tx_packets++;
1361 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 return total;
1364}
1365
Jason Wang54f968d2012-10-31 19:45:57 +00001366static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001367 const struct iovec *iv, ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001370 ssize_t ret = 0;
Xi Wang9e641bd2014-05-16 15:11:48 -07001371 int peeked, err, off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Rami Rosen3872baf2012-11-25 22:07:41 +00001373 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Xi Wang9e641bd2014-05-16 15:11:48 -07001375 if (!len)
1376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Xi Wang9e641bd2014-05-16 15:11:48 -07001378 if (tun->dev->reg_state != NETREG_REGISTERED)
1379 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Xi Wang9e641bd2014-05-16 15:11:48 -07001381 /* Read frames from queue */
1382 skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
1383 &peeked, &off, &err);
1384 if (skb) {
Jason Wang54f968d2012-10-31 19:45:57 +00001385 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001386 kfree_skb(skb);
Xi Wang9e641bd2014-05-16 15:11:48 -07001387 } else
1388 ret = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001390 return ret;
1391}
1392
1393static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1394 unsigned long count, loff_t pos)
1395{
1396 struct file *file = iocb->ki_filp;
1397 struct tun_file *tfile = file->private_data;
1398 struct tun_struct *tun = __tun_get(tfile);
1399 ssize_t len, ret;
1400
1401 if (!tun)
1402 return -EBADFD;
1403 len = iov_length(iv, count);
1404 if (len < 0) {
1405 ret = -EINVAL;
1406 goto out;
1407 }
1408
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001409 ret = tun_do_read(tun, tfile, iv, len,
Jason Wang54f968d2012-10-31 19:45:57 +00001410 file->f_flags & O_NONBLOCK);
David S. Miller42404c02013-12-10 22:05:45 -05001411 ret = min_t(ssize_t, ret, len);
Zhi Yong Wud0b7da8a2013-12-06 14:16:51 +08001412 if (ret > 0)
1413 iocb->ki_pos = ret;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001414out:
1415 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return ret;
1417}
1418
Jason Wang96442e422012-10-31 19:46:02 +00001419static void tun_free_netdev(struct net_device *dev)
1420{
1421 struct tun_struct *tun = netdev_priv(dev);
1422
Jason Wang4008e972012-12-13 23:53:30 +00001423 BUG_ON(!(list_empty(&tun->disabled)));
Jason Wang96442e422012-10-31 19:46:02 +00001424 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001425 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001426 free_netdev(dev);
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429static void tun_setup(struct net_device *dev)
1430{
1431 struct tun_struct *tun = netdev_priv(dev);
1432
Eric W. Biederman0625c882012-02-07 16:48:55 -08001433 tun->owner = INVALID_UID;
1434 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang96442e422012-10-31 19:46:02 +00001437 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001440/* Trivial set of netlink ops to allow deleting tun or tap
1441 * device with netlink.
1442 */
1443static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1444{
1445 return -EINVAL;
1446}
1447
1448static struct rtnl_link_ops tun_link_ops __read_mostly = {
1449 .kind = DRV_NAME,
1450 .priv_size = sizeof(struct tun_struct),
1451 .setup = tun_setup,
1452 .validate = tun_validate,
1453};
1454
Herbert Xu33dccbb2009-02-05 21:25:32 -08001455static void tun_sock_write_space(struct sock *sk)
1456{
Jason Wang54f968d2012-10-31 19:45:57 +00001457 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001458 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001459
1460 if (!sock_writeable(sk))
1461 return;
1462
Herbert Xu33dccbb2009-02-05 21:25:32 -08001463 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1464 return;
1465
Eric Dumazet43815482010-04-29 11:01:49 +00001466 wqueue = sk_sleep(sk);
1467 if (wqueue && waitqueue_active(wqueue))
1468 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001469 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001470
Jason Wang54f968d2012-10-31 19:45:57 +00001471 tfile = container_of(sk, struct tun_file, sk);
1472 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001473}
1474
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001475static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1476 struct msghdr *m, size_t total_len)
1477{
Jason Wang54f968d2012-10-31 19:45:57 +00001478 int ret;
1479 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1480 struct tun_struct *tun = __tun_get(tfile);
1481
1482 if (!tun)
1483 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001484 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1485 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1486 tun_put(tun);
1487 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001488}
1489
1490static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1491 struct msghdr *m, size_t total_len,
1492 int flags)
1493{
Jason Wang54f968d2012-10-31 19:45:57 +00001494 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1495 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001496 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001497
1498 if (!tun)
1499 return -EBADFD;
1500
Richard Cochraneda29772013-07-19 19:40:10 +02001501 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00001502 ret = -EINVAL;
1503 goto out;
1504 }
Richard Cochraneda29772013-07-19 19:40:10 +02001505 if (flags & MSG_ERRQUEUE) {
1506 ret = sock_recv_errqueue(sock->sk, m, total_len,
1507 SOL_PACKET, TUN_TX_TIMESTAMP);
1508 goto out;
1509 }
Zhi Yong Wuf96eb742013-12-07 04:13:06 +08001510 ret = tun_do_read(tun, tfile, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001511 flags & MSG_DONTWAIT);
David S. Miller42404c02013-12-10 22:05:45 -05001512 if (ret > total_len) {
1513 m->msg_flags |= MSG_TRUNC;
1514 ret = flags & MSG_TRUNC ? ret : total_len;
1515 }
Gao feng3811ae72013-04-24 21:59:23 +00001516out:
Jason Wang54f968d2012-10-31 19:45:57 +00001517 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001518 return ret;
1519}
1520
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001521static int tun_release(struct socket *sock)
1522{
1523 if (sock->sk)
1524 sock_put(sock->sk);
1525 return 0;
1526}
1527
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001528/* Ops structure to mimic raw sockets with tun */
1529static const struct proto_ops tun_socket_ops = {
1530 .sendmsg = tun_sendmsg,
1531 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001532 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001533};
1534
Herbert Xu33dccbb2009-02-05 21:25:32 -08001535static struct proto tun_proto = {
1536 .name = "tun",
1537 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001538 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001539};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001540
David Woodhouse980c9e82009-05-09 22:54:21 -07001541static int tun_flags(struct tun_struct *tun)
1542{
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02001543 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
David Woodhouse980c9e82009-05-09 22:54:21 -07001544}
1545
1546static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1547 char *buf)
1548{
1549 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1550 return sprintf(buf, "0x%x\n", tun_flags(tun));
1551}
1552
1553static ssize_t tun_show_owner(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 uid_valid(tun->owner)?
1558 sprintf(buf, "%u\n",
1559 from_kuid_munged(current_user_ns(), tun->owner)):
1560 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001561}
1562
1563static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1564 char *buf)
1565{
1566 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001567 return gid_valid(tun->group) ?
1568 sprintf(buf, "%u\n",
1569 from_kgid_munged(current_user_ns(), tun->group)):
1570 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001571}
1572
1573static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1574static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1575static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1576
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001577static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001580 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct net_device *dev;
1582 int err;
1583
Jason Wang7c0c3b12013-01-11 16:59:33 +00001584 if (tfile->detached)
1585 return -EINVAL;
1586
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001587 dev = __dev_get_by_name(net, ifr->ifr_name);
1588 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001589 if (ifr->ifr_flags & IFF_TUN_EXCL)
1590 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001591 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1592 tun = netdev_priv(dev);
1593 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1594 tun = netdev_priv(dev);
1595 else
1596 return -EINVAL;
1597
Jason Wang8e6d91a2013-05-28 18:32:11 +00001598 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001599 !!(tun->flags & IFF_MULTI_QUEUE))
Jason Wang8e6d91a2013-05-28 18:32:11 +00001600 return -EINVAL;
1601
Jason Wangcde8b152012-10-31 19:46:01 +00001602 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001603 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001604 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001605 if (err < 0)
1606 return err;
1607
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001608 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001609 if (err < 0)
1610 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001611
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001612 if (tun->flags & IFF_MULTI_QUEUE &&
Jason Wange8dbad62013-04-22 20:40:39 +00001613 (tun->numqueues + tun->numdisabled > 1)) {
1614 /* One or more queue has already been attached, no need
1615 * to initialize the device again.
1616 */
1617 return 0;
1618 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 else {
1621 char *name;
1622 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001623 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1624 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001626 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001627 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001628 err = security_tun_dev_create();
1629 if (err < 0)
1630 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /* Set dev type */
1633 if (ifr->ifr_flags & IFF_TUN) {
1634 /* TUN device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001635 flags |= IFF_TUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 name = "tun%d";
1637 } else if (ifr->ifr_flags & IFF_TAP) {
1638 /* TAP device */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001639 flags |= IFF_TAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001641 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001642 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (*ifr->ifr_name)
1645 name = ifr->ifr_name;
1646
Jason Wangc8d68e62012-10-31 19:46:00 +00001647 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Tom Gundersenc835a672014-07-14 16:37:24 +02001648 NET_NAME_UNKNOWN, tun_setup, queues,
1649 queues);
Jason Wangedfb6a12013-01-23 03:59:12 +00001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (!dev)
1652 return -ENOMEM;
1653
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001654 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001655 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001656 dev->ifindex = tfile->ifindex;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 tun = netdev_priv(dev);
1659 tun->dev = dev;
1660 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001661 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001662 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Jason Wang54f968d2012-10-31 19:45:57 +00001664 tun->filter_attached = false;
1665 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001666
Jason Wang96442e422012-10-31 19:46:02 +00001667 spin_lock_init(&tun->lock);
1668
Paul Moore5dbbaf22013-01-14 07:12:19 +00001669 err = security_tun_dev_alloc_security(&tun->security);
1670 if (err < 0)
1671 goto err_free_dev;
Paul Moore2b980db2009-08-28 18:12:43 -04001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001674 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001675
Michał Mirosław88255372011-04-19 06:13:10 +00001676 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08001677 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
1678 NETIF_F_HW_VLAN_STAG_TX;
Michał Mirosław88255372011-04-19 06:13:10 +00001679 dev->features = dev->hw_features;
Fernando Luis Vazquez Cao6671b222014-02-18 21:20:09 +09001680 dev->vlan_features = dev->features &
1681 ~(NETIF_F_HW_VLAN_CTAG_TX |
1682 NETIF_F_HW_VLAN_STAG_TX);
Michał Mirosław88255372011-04-19 06:13:10 +00001683
Jason Wang4008e972012-12-13 23:53:30 +00001684 INIT_LIST_HEAD(&tun->disabled);
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001685 err = tun_attach(tun, file, false);
Jason Wangeb0fb362012-12-02 17:19:45 +00001686 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001687 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 err = register_netdevice(tun->dev);
1690 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001691 goto err_detach;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001692
David Woodhouse980c9e82009-05-09 22:54:21 -07001693 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1694 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1695 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001696 pr_err("Failed to create tun sysfs files\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00001699 netif_carrier_on(tun->dev);
1700
Joe Perches6b8a66e2011-03-02 07:18:10 +00001701 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02001703 tun->flags = (tun->flags & ~TUN_FEATURES) |
1704 (ifr->ifr_flags & TUN_FEATURES);
Jason Wangc8d68e62012-10-31 19:46:00 +00001705
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001706 /* Make sure persistent devices do not get stuck in
1707 * xoff state.
1708 */
1709 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001710 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 strcpy(ifr->ifr_name, tun->dev->name);
1713 return 0;
1714
Jason Wang662ca432013-09-11 18:09:48 +08001715err_detach:
1716 tun_detach_all(dev);
1717err_free_flow:
1718 tun_flow_uninit(tun);
1719 security_tun_dev_free_security(tun->security);
1720err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return err;
1723}
1724
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001725static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00001726 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001727{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001728 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001729
1730 strcpy(ifr->ifr_name, tun->dev->name);
1731
David Woodhouse980c9e82009-05-09 22:54:21 -07001732 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001733
Mark McLoughline3b99552008-08-15 15:09:56 -07001734}
1735
Rusty Russell5228ddc2008-07-03 03:46:16 -07001736/* This is like a cut-down ethtool ops, except done via tun fd so no
1737 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001738static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001739{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001740 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001741
1742 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001743 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001744 arg &= ~TUN_F_CSUM;
1745
1746 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1747 if (arg & TUN_F_TSO_ECN) {
1748 features |= NETIF_F_TSO_ECN;
1749 arg &= ~TUN_F_TSO_ECN;
1750 }
1751 if (arg & TUN_F_TSO4)
1752 features |= NETIF_F_TSO;
1753 if (arg & TUN_F_TSO6)
1754 features |= NETIF_F_TSO6;
1755 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1756 }
1757 }
1758
1759 /* This gives the user a way to test for new features in future by
1760 * trying to set them. */
1761 if (arg)
1762 return -EINVAL;
1763
Michał Mirosław88255372011-04-19 06:13:10 +00001764 tun->set_features = features;
1765 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001766
1767 return 0;
1768}
1769
Jason Wangc8d68e62012-10-31 19:46:00 +00001770static void tun_detach_filter(struct tun_struct *tun, int n)
1771{
1772 int i;
1773 struct tun_file *tfile;
1774
1775 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001776 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001777 sk_detach_filter(tfile->socket.sk);
1778 }
1779
1780 tun->filter_attached = false;
1781}
1782
1783static int tun_attach_filter(struct tun_struct *tun)
1784{
1785 int i, ret = 0;
1786 struct tun_file *tfile;
1787
1788 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001789 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001790 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1791 if (ret) {
1792 tun_detach_filter(tun, i);
1793 return ret;
1794 }
1795 }
1796
1797 tun->filter_attached = true;
1798 return ret;
1799}
1800
1801static void tun_set_sndbuf(struct tun_struct *tun)
1802{
1803 struct tun_file *tfile;
1804 int i;
1805
1806 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001807 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001808 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1809 }
1810}
1811
Jason Wangcde8b152012-10-31 19:46:01 +00001812static int tun_set_queue(struct file *file, struct ifreq *ifr)
1813{
1814 struct tun_file *tfile = file->private_data;
1815 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00001816 int ret = 0;
1817
1818 rtnl_lock();
1819
1820 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00001821 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001822 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00001823 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001824 goto unlock;
1825 }
1826 ret = security_tun_dev_attach_queue(tun->security);
1827 if (ret < 0)
1828 goto unlock;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001829 ret = tun_attach(tun, file, false);
Jason Wang4008e972012-12-13 23:53:30 +00001830 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001831 tun = rtnl_dereference(tfile->tun);
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001832 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00001833 ret = -EINVAL;
1834 else
1835 __tun_detach(tfile, false);
1836 } else
Jason Wangcde8b152012-10-31 19:46:01 +00001837 ret = -EINVAL;
1838
Paul Moore5dbbaf22013-01-14 07:12:19 +00001839unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00001840 rtnl_unlock();
1841 return ret;
1842}
1843
Arnd Bergmann50857e22009-11-06 22:52:32 -08001844static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1845 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001847 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001848 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 void __user* argp = (void __user*)arg;
1850 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001851 kuid_t owner;
1852 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001853 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001854 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001855 unsigned int ifindex;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001856 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Jason Wangcde8b152012-10-31 19:46:01 +00001858 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001859 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001861 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001862 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001863 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001864 if (cmd == TUNGETFEATURES) {
1865 /* Currently this just means: "what IFF flags are valid?".
1866 * This is needed because we never checked for invalid flags on
Michael S. Tsirkin031f5e032014-11-19 14:44:40 +02001867 * TUNSETIFF.
1868 */
1869 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001870 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001871 } else if (cmd == TUNSETQUEUE)
1872 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001873
Jason Wangc8d68e62012-10-31 19:46:00 +00001874 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001875 rtnl_lock();
1876
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001877 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1880
Herbert Xu876bfd42009-08-06 14:22:44 +00001881 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Herbert Xu876bfd42009-08-06 14:22:44 +00001883 if (ret)
1884 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Arnd Bergmann50857e22009-11-06 22:52:32 -08001886 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001887 ret = -EFAULT;
1888 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001890 if (cmd == TUNSETIFINDEX) {
1891 ret = -EPERM;
1892 if (tun)
1893 goto unlock;
1894
1895 ret = -EFAULT;
1896 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1897 goto unlock;
1898
1899 ret = 0;
1900 tfile->ifindex = ifindex;
1901 goto unlock;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Herbert Xu876bfd42009-08-06 14:22:44 +00001904 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001906 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Jason Wang1e588332012-10-31 19:45:56 +00001908 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Eric W. Biederman631ab462009-01-20 11:00:40 +00001910 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001912 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001913 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001914
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001915 if (tfile->detached)
1916 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001917 if (!tfile->socket.sk->sk_filter)
1918 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001919
Arnd Bergmann50857e22009-11-06 22:52:32 -08001920 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001921 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001922 break;
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 case TUNSETNOCSUM:
1925 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Michał Mirosław88255372011-04-19 06:13:10 +00001927 /* [unimplemented] */
1928 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001929 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931
1932 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001933 /* Disable/Enable persist mode. Keep an extra reference to the
1934 * module to prevent the module being unprobed.
1935 */
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001936 if (arg && !(tun->flags & IFF_PERSIST)) {
1937 tun->flags |= IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001938 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00001939 }
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001940 if (!arg && (tun->flags & IFF_PERSIST)) {
1941 tun->flags &= ~IFF_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001942 module_put(THIS_MODULE);
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Joe Perches6b8a66e2011-03-02 07:18:10 +00001945 tun_debug(KERN_INFO, tun, "persist %s\n",
1946 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 break;
1948
1949 case TUNSETOWNER:
1950 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001951 owner = make_kuid(current_user_ns(), arg);
1952 if (!uid_valid(owner)) {
1953 ret = -EINVAL;
1954 break;
1955 }
1956 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001957 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001958 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 break;
1960
Guido Guenther8c644622007-07-02 22:50:25 -07001961 case TUNSETGROUP:
1962 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001963 group = make_kgid(current_user_ns(), arg);
1964 if (!gid_valid(group)) {
1965 ret = -EINVAL;
1966 break;
1967 }
1968 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001969 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001970 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001971 break;
1972
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001973 case TUNSETLINK:
1974 /* Only allow setting the type when the interface is down */
1975 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001976 tun_debug(KERN_INFO, tun,
1977 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001978 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001979 } else {
1980 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001981 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1982 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001983 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001984 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001985 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987#ifdef TUN_DEBUG
1988 case TUNSETDEBUG:
1989 tun->debug = arg;
1990 break;
1991#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001992 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001993 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001994 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001995
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001996 case TUNSETTXFILTER:
1997 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001998 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02001999 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Eric W. Biederman631ab462009-01-20 11:00:40 +00002000 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07002001 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002002 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002005 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002006 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2007 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08002008 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00002009 ret = -EFAULT;
2010 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07002013 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00002014 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2015 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08002016
Kim B. Heino40102372008-02-29 12:26:21 -08002017 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002018 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002019
2020 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002021 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002022 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2023 ret = -EFAULT;
2024 break;
2025
2026 case TUNSETSNDBUF:
2027 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2028 ret = -EFAULT;
2029 break;
2030 }
2031
Jason Wangc8d68e62012-10-31 19:46:00 +00002032 tun->sndbuf = sndbuf;
2033 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002034 break;
2035
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002036 case TUNGETVNETHDRSZ:
2037 vnet_hdr_sz = tun->vnet_hdr_sz;
2038 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2039 ret = -EFAULT;
2040 break;
2041
2042 case TUNSETVNETHDRSZ:
2043 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2044 ret = -EFAULT;
2045 break;
2046 }
2047 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2048 ret = -EINVAL;
2049 break;
2050 }
2051
2052 tun->vnet_hdr_sz = vnet_hdr_sz;
2053 break;
2054
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002055 case TUNATTACHFILTER:
2056 /* Can be set only for TAPs */
2057 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002058 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002059 break;
2060 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002061 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002062 break;
2063
Jason Wangc8d68e62012-10-31 19:46:00 +00002064 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002065 break;
2066
2067 case TUNDETACHFILTER:
2068 /* Can be set only for TAPs */
2069 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002070 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002071 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002072 ret = 0;
2073 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002074 break;
2075
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002076 case TUNGETFILTER:
2077 ret = -EINVAL;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002078 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002079 break;
2080 ret = -EFAULT;
2081 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2082 break;
2083 ret = 0;
2084 break;
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002087 ret = -EINVAL;
2088 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Herbert Xu876bfd42009-08-06 14:22:44 +00002091unlock:
2092 rtnl_unlock();
2093 if (tun)
2094 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002095 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Arnd Bergmann50857e22009-11-06 22:52:32 -08002098static long tun_chr_ioctl(struct file *file,
2099 unsigned int cmd, unsigned long arg)
2100{
2101 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2102}
2103
2104#ifdef CONFIG_COMPAT
2105static long tun_chr_compat_ioctl(struct file *file,
2106 unsigned int cmd, unsigned long arg)
2107{
2108 switch (cmd) {
2109 case TUNSETIFF:
2110 case TUNGETIFF:
2111 case TUNSETTXFILTER:
2112 case TUNGETSNDBUF:
2113 case TUNSETSNDBUF:
2114 case SIOCGIFHWADDR:
2115 case SIOCSIFHWADDR:
2116 arg = (unsigned long)compat_ptr(arg);
2117 break;
2118 default:
2119 arg = (compat_ulong_t)arg;
2120 break;
2121 }
2122
2123 /*
2124 * compat_ifreq is shorter than ifreq, so we must not access beyond
2125 * the end of that structure. All fields that are used in this
2126 * driver are compatible though, we don't need to convert the
2127 * contents.
2128 */
2129 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2130}
2131#endif /* CONFIG_COMPAT */
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133static int tun_chr_fasync(int fd, struct file *file, int on)
2134{
Jason Wang54f968d2012-10-31 19:45:57 +00002135 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 int ret;
2137
Jason Wang54f968d2012-10-31 19:45:57 +00002138 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002139 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (on) {
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002142 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Jason Wang54f968d2012-10-31 19:45:57 +00002143 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002144 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002145 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002146 ret = 0;
2147out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002148 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151static int tun_chr_open(struct inode *inode, struct file * file)
2152{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002153 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002154
Joe Perches6b8a66e2011-03-02 07:18:10 +00002155 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002156
Jason Wang54f968d2012-10-31 19:45:57 +00002157 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2158 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002159 if (!tfile)
2160 return -ENOMEM;
Monam Agarwalc9566742014-03-24 00:02:32 +05302161 RCU_INIT_POINTER(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002162 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00002163 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002164 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002165
Jason Wang54f968d2012-10-31 19:45:57 +00002166 init_waitqueue_head(&tfile->wq.wait);
Xi Wang9e641bd2014-05-16 15:11:48 -07002167 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
Jason Wang54f968d2012-10-31 19:45:57 +00002168
2169 tfile->socket.file = file;
2170 tfile->socket.ops = &tun_socket_ops;
2171
2172 sock_init_data(&tfile->socket, &tfile->sk);
2173 sk_change_net(&tfile->sk, tfile->net);
2174
2175 tfile->sk.sk_write_space = tun_sock_write_space;
2176 tfile->sk.sk_sndbuf = INT_MAX;
2177
Eric W. Biederman631ab462009-01-20 11:00:40 +00002178 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00002179 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
Jason Wang4008e972012-12-13 23:53:30 +00002180 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002181
Jason Wang19a6afb2013-06-08 14:17:41 +08002182 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return 0;
2185}
2186
2187static int tun_chr_close(struct inode *inode, struct file *file)
2188{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002189 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00002190 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Jason Wangc8d68e62012-10-31 19:46:00 +00002192 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00002193 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 return 0;
2196}
2197
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002198#ifdef CONFIG_PROC_FS
2199static int tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
2200{
2201 struct tun_struct *tun;
2202 struct ifreq ifr;
2203
2204 memset(&ifr, 0, sizeof(ifr));
2205
2206 rtnl_lock();
2207 tun = tun_get(f);
2208 if (tun)
2209 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2210 rtnl_unlock();
2211
2212 if (tun)
2213 tun_put(tun);
2214
2215 return seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
2216}
2217#endif
2218
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002219static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002220 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002222 .read = do_sync_read,
2223 .aio_read = tun_chr_aio_read,
2224 .write = do_sync_write,
2225 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002227 .unlocked_ioctl = tun_chr_ioctl,
2228#ifdef CONFIG_COMPAT
2229 .compat_ioctl = tun_chr_compat_ioctl,
2230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 .open = tun_chr_open,
2232 .release = tun_chr_close,
Masatake YAMATO93e14b62014-01-29 16:43:31 +09002233 .fasync = tun_chr_fasync,
2234#ifdef CONFIG_PROC_FS
2235 .show_fdinfo = tun_chr_show_fdinfo,
2236#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237};
2238
2239static struct miscdevice tun_miscdev = {
2240 .minor = TUN_MINOR,
2241 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002242 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244};
2245
2246/* ethtool interface */
2247
2248static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2249{
2250 cmd->supported = 0;
2251 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00002252 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 cmd->duplex = DUPLEX_FULL;
2254 cmd->port = PORT_TP;
2255 cmd->phy_address = 0;
2256 cmd->transceiver = XCVR_INTERNAL;
2257 cmd->autoneg = AUTONEG_DISABLE;
2258 cmd->maxtxpkt = 0;
2259 cmd->maxrxpkt = 0;
2260 return 0;
2261}
2262
2263static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2264{
2265 struct tun_struct *tun = netdev_priv(dev);
2266
Rick Jones33a5ba12011-11-15 14:59:53 +00002267 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2268 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 switch (tun->flags & TUN_TYPE_MASK) {
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002271 case IFF_TUN:
Rick Jones33a5ba12011-11-15 14:59:53 +00002272 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 break;
Michael S. Tsirkin40630b82014-11-19 15:17:31 +02002274 case IFF_TAP:
Rick Jones33a5ba12011-11-15 14:59:53 +00002275 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 break;
2277 }
2278}
2279
2280static u32 tun_get_msglevel(struct net_device *dev)
2281{
2282#ifdef TUN_DEBUG
2283 struct tun_struct *tun = netdev_priv(dev);
2284 return tun->debug;
2285#else
2286 return -EOPNOTSUPP;
2287#endif
2288}
2289
2290static void tun_set_msglevel(struct net_device *dev, u32 value)
2291{
2292#ifdef TUN_DEBUG
2293 struct tun_struct *tun = netdev_priv(dev);
2294 tun->debug = value;
2295#endif
2296}
2297
Jeff Garzik7282d492006-09-13 14:30:00 -04002298static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 .get_settings = tun_get_settings,
2300 .get_drvinfo = tun_get_drvinfo,
2301 .get_msglevel = tun_get_msglevel,
2302 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002303 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002304 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305};
2306
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308static int __init tun_init(void)
2309{
2310 int ret = 0;
2311
Joe Perches6b8a66e2011-03-02 07:18:10 +00002312 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2313 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002315 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002316 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002317 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002318 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002319 }
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002322 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002323 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002324 goto err_misc;
2325 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002326 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002327err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002328 rtnl_link_unregister(&tun_link_ops);
2329err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return ret;
2331}
2332
2333static void tun_cleanup(void)
2334{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002335 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002336 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002339/* Get an underlying socket object from tun file. Returns error unless file is
2340 * attached to a device. The returned object works like a packet socket, it
2341 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2342 * holding a reference to the file for as long as the socket is in use. */
2343struct socket *tun_get_socket(struct file *file)
2344{
Jason Wang6e914fc2012-10-31 19:45:58 +00002345 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002346 if (file->f_op != &tun_fops)
2347 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002348 tfile = file->private_data;
2349 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002350 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002351 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002352}
2353EXPORT_SYMBOL_GPL(tun_get_socket);
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355module_init(tun_init);
2356module_exit(tun_cleanup);
2357MODULE_DESCRIPTION(DRV_DESCRIPTION);
2358MODULE_AUTHOR(DRV_COPYRIGHT);
2359MODULE_LICENSE("GPL");
2360MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002361MODULE_ALIAS("devname:net/tun");