blob: 807815fc996839d14efd18625fb300a2f48d2577 [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>
Eric W. Biederman881d9662007-09-17 11:56:21 -070068#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070069#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080070#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080071#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <asm/uaccess.h>
74
Rusty Russell14daa022008-04-12 18:48:58 -070075/* Uncomment to enable debugging */
76/* #define TUN_DEBUG 1 */
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#ifdef TUN_DEBUG
79static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070080
Joe Perches6b8a66e2011-03-02 07:18:10 +000081#define tun_debug(level, tun, fmt, args...) \
82do { \
83 if (tun->debug) \
84 netdev_printk(level, tun->dev, fmt, ##args); \
85} while (0)
86#define DBG1(level, fmt, args...) \
87do { \
88 if (debug == 2) \
89 printk(level fmt, ##args); \
90} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070091#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000092#define tun_debug(level, tun, fmt, args...) \
93do { \
94 if (0) \
95 netdev_printk(level, tun->dev, fmt, ##args); \
96} while (0)
97#define DBG1(level, fmt, args...) \
98do { \
99 if (0) \
100 printk(level fmt, ##args); \
101} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif
103
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000104#define GOODCOPY_LEN 128
105
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700106#define FLT_EXACT_COUNT 8
107struct tap_filter {
108 unsigned int count; /* Number of addrs. Zero means disabled */
109 u32 mask[2]; /* Mask of the hashed addrs */
110 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
111};
112
Jason Wangedfb6a12013-01-23 03:59:12 +0000113/* DEFAULT_MAX_NUM_RSS_QUEUES were choosed to let the rx/tx queues allocated for
114 * the netdevice to be fit in one page. So we can make sure the success of
115 * memory allocation. TODO: increase the limit. */
116#define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
Jason Wangb8732fb2013-01-23 03:59:13 +0000117#define MAX_TAP_FLOWS 4096
Jason Wangc8d68e62012-10-31 19:46:00 +0000118
Jason Wang96442e422012-10-31 19:46:02 +0000119#define TUN_FLOW_EXPIRE (3 * HZ)
120
Jason Wang54f968d2012-10-31 19:45:57 +0000121/* A tun_file connects an open character device to a tuntap netdevice. It
122 * also contains all socket related strctures (except sock_fprog and tap_filter)
123 * to serve as one transmit queue for tuntap device. The sock_fprog and
124 * tap_filter were kept in tun_struct since they were used for filtering for the
Rami Rosen36fe8c092012-11-25 22:07:40 +0000125 * netdevice not for a specific queue (at least I didn't see the requirement for
Jason Wang54f968d2012-10-31 19:45:57 +0000126 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000127 *
128 * RCU usage:
Rami Rosen36fe8c092012-11-25 22:07:40 +0000129 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
Jason Wang6e914fc2012-10-31 19:45:58 +0000130 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000131 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000132struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000133 struct sock sk;
134 struct socket socket;
135 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000136 struct tun_struct __rcu *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000137 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000138 struct fasync_struct *fasync;
139 /* only used for fasnyc */
140 unsigned int flags;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +0400141 union {
142 u16 queue_index;
143 unsigned int ifindex;
144 };
Jason Wang4008e972012-12-13 23:53:30 +0000145 struct list_head next;
146 struct tun_struct *detached;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000147};
148
Jason Wang96442e422012-10-31 19:46:02 +0000149struct tun_flow_entry {
150 struct hlist_node hash_link;
151 struct rcu_head rcu;
152 struct tun_struct *tun;
153
154 u32 rxhash;
155 int queue_index;
156 unsigned long updated;
157};
158
159#define TUN_NUM_FLOW_ENTRIES 1024
160
Jason Wang54f968d2012-10-31 19:45:57 +0000161/* Since the socket were moved to tun_file, to preserve the behavior of persist
Rami Rosen36fe8c092012-11-25 22:07:40 +0000162 * device, socket filter, sndbuf and vnet header size were restore when the
Jason Wang54f968d2012-10-31 19:45:57 +0000163 * file were attached to a persist device.
164 */
Rusty Russell14daa022008-04-12 18:48:58 -0700165struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000166 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
167 unsigned int numqueues;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700168 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800169 kuid_t owner;
170 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700171
Rusty Russell14daa022008-04-12 18:48:58 -0700172 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000173 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000174#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
175 NETIF_F_TSO6|NETIF_F_UFO)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200176
177 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000178 int sndbuf;
179 struct tap_filter txflt;
180 struct sock_fprog fprog;
181 /* protected by rtnl lock */
182 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700183#ifdef TUN_DEBUG
184 int debug;
185#endif
Jason Wang96442e422012-10-31 19:46:02 +0000186 spinlock_t lock;
Jason Wang96442e422012-10-31 19:46:02 +0000187 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
188 struct timer_list flow_gc_timer;
189 unsigned long ageing_time;
Jason Wang4008e972012-12-13 23:53:30 +0000190 unsigned int numdisabled;
191 struct list_head disabled;
Paul Moore5dbbaf22013-01-14 07:12:19 +0000192 void *security;
Jason Wangb8732fb2013-01-23 03:59:13 +0000193 u32 flow_count;
Rusty Russell14daa022008-04-12 18:48:58 -0700194};
195
Jason Wang96442e422012-10-31 19:46:02 +0000196static inline u32 tun_hashfn(u32 rxhash)
197{
198 return rxhash & 0x3ff;
199}
200
201static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
202{
203 struct tun_flow_entry *e;
Jason Wang96442e422012-10-31 19:46:02 +0000204
Sasha Levinb67bfe02013-02-27 17:06:00 -0800205 hlist_for_each_entry_rcu(e, head, hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000206 if (e->rxhash == rxhash)
207 return e;
208 }
209 return NULL;
210}
211
212static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
213 struct hlist_head *head,
214 u32 rxhash, u16 queue_index)
215{
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000216 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
217
Jason Wang96442e422012-10-31 19:46:02 +0000218 if (e) {
219 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
220 rxhash, queue_index);
221 e->updated = jiffies;
222 e->rxhash = rxhash;
223 e->queue_index = queue_index;
224 e->tun = tun;
225 hlist_add_head_rcu(&e->hash_link, head);
Jason Wangb8732fb2013-01-23 03:59:13 +0000226 ++tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000227 }
228 return e;
229}
230
Jason Wang96442e422012-10-31 19:46:02 +0000231static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
232{
233 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
234 e->rxhash, e->queue_index);
235 hlist_del_rcu(&e->hash_link);
Eric Dumazet9fdc6be2012-12-21 07:17:21 +0000236 kfree_rcu(e, rcu);
Jason Wangb8732fb2013-01-23 03:59:13 +0000237 --tun->flow_count;
Jason Wang96442e422012-10-31 19:46:02 +0000238}
239
240static void tun_flow_flush(struct tun_struct *tun)
241{
242 int i;
243
244 spin_lock_bh(&tun->lock);
245 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
246 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800247 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000248
Sasha Levinb67bfe02013-02-27 17:06:00 -0800249 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
Jason Wang96442e422012-10-31 19:46:02 +0000250 tun_flow_delete(tun, e);
251 }
252 spin_unlock_bh(&tun->lock);
253}
254
255static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
256{
257 int i;
258
259 spin_lock_bh(&tun->lock);
260 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
261 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800262 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000263
Sasha Levinb67bfe02013-02-27 17:06:00 -0800264 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000265 if (e->queue_index == queue_index)
266 tun_flow_delete(tun, e);
267 }
268 }
269 spin_unlock_bh(&tun->lock);
270}
271
272static void tun_flow_cleanup(unsigned long data)
273{
274 struct tun_struct *tun = (struct tun_struct *)data;
275 unsigned long delay = tun->ageing_time;
276 unsigned long next_timer = jiffies + delay;
277 unsigned long count = 0;
278 int i;
279
280 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
281
282 spin_lock_bh(&tun->lock);
283 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
284 struct tun_flow_entry *e;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800285 struct hlist_node *n;
Jason Wang96442e422012-10-31 19:46:02 +0000286
Sasha Levinb67bfe02013-02-27 17:06:00 -0800287 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
Jason Wang96442e422012-10-31 19:46:02 +0000288 unsigned long this_timer;
289 count++;
290 this_timer = e->updated + delay;
291 if (time_before_eq(this_timer, jiffies))
292 tun_flow_delete(tun, e);
293 else if (time_before(this_timer, next_timer))
294 next_timer = this_timer;
295 }
296 }
297
298 if (count)
299 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
300 spin_unlock_bh(&tun->lock);
301}
302
Eric Dumazet49974422012-12-12 19:22:57 +0000303static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
Jason Wang9e857222013-01-28 01:05:19 +0000304 struct tun_file *tfile)
Jason Wang96442e422012-10-31 19:46:02 +0000305{
306 struct hlist_head *head;
307 struct tun_flow_entry *e;
308 unsigned long delay = tun->ageing_time;
Jason Wang9e857222013-01-28 01:05:19 +0000309 u16 queue_index = tfile->queue_index;
Jason Wang96442e422012-10-31 19:46:02 +0000310
311 if (!rxhash)
312 return;
313 else
314 head = &tun->flows[tun_hashfn(rxhash)];
315
316 rcu_read_lock();
317
Jason Wang9e857222013-01-28 01:05:19 +0000318 /* We may get a very small possibility of OOO during switching, not
319 * worth to optimize.*/
320 if (tun->numqueues == 1 || tfile->detached)
Jason Wang96442e422012-10-31 19:46:02 +0000321 goto unlock;
322
323 e = tun_flow_find(head, rxhash);
324 if (likely(e)) {
325 /* TODO: keep queueing to old queue until it's empty? */
326 e->queue_index = queue_index;
327 e->updated = jiffies;
328 } else {
329 spin_lock_bh(&tun->lock);
Jason Wangb8732fb2013-01-23 03:59:13 +0000330 if (!tun_flow_find(head, rxhash) &&
331 tun->flow_count < MAX_TAP_FLOWS)
Jason Wang96442e422012-10-31 19:46:02 +0000332 tun_flow_create(tun, head, rxhash, queue_index);
333
334 if (!timer_pending(&tun->flow_gc_timer))
335 mod_timer(&tun->flow_gc_timer,
336 round_jiffies_up(jiffies + delay));
337 spin_unlock_bh(&tun->lock);
338 }
339
340unlock:
341 rcu_read_unlock();
342}
343
Jason Wangc8d68e62012-10-31 19:46:00 +0000344/* We try to identify a flow through its rxhash first. The reason that
345 * we do not check rxq no. is becuase some cards(e.g 82599), chooses
346 * the rxq based on the txq where the last packet of the flow comes. As
347 * the userspace application move between processors, we may get a
348 * different rxq no. here. If we could not get rxhash, then we would
349 * hope the rxq no. may help here.
350 */
351static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
352{
353 struct tun_struct *tun = netdev_priv(dev);
Jason Wang96442e422012-10-31 19:46:02 +0000354 struct tun_flow_entry *e;
Jason Wangc8d68e62012-10-31 19:46:00 +0000355 u32 txq = 0;
356 u32 numqueues = 0;
357
358 rcu_read_lock();
Jason Wang92bb73e2013-06-05 16:44:57 +0800359 numqueues = ACCESS_ONCE(tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000360
361 txq = skb_get_rxhash(skb);
362 if (txq) {
Jason Wang96442e422012-10-31 19:46:02 +0000363 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
364 if (e)
365 txq = e->queue_index;
366 else
367 /* use multiply and shift instead of expensive divide */
368 txq = ((u64)txq * numqueues) >> 32;
Jason Wangc8d68e62012-10-31 19:46:00 +0000369 } else if (likely(skb_rx_queue_recorded(skb))) {
370 txq = skb_get_rx_queue(skb);
371 while (unlikely(txq >= numqueues))
372 txq -= numqueues;
373 }
374
375 rcu_read_unlock();
376 return txq;
377}
378
Jason Wangcde8b152012-10-31 19:46:01 +0000379static inline bool tun_not_capable(struct tun_struct *tun)
380{
381 const struct cred *cred = current_cred();
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000382 struct net *net = dev_net(tun->dev);
Jason Wangcde8b152012-10-31 19:46:01 +0000383
384 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
385 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Eric W. Biedermanc260b772012-11-18 21:34:11 +0000386 !ns_capable(net->user_ns, CAP_NET_ADMIN);
Jason Wangcde8b152012-10-31 19:46:01 +0000387}
388
Jason Wangc8d68e62012-10-31 19:46:00 +0000389static void tun_set_real_num_queues(struct tun_struct *tun)
390{
391 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
392 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
393}
394
Jason Wang4008e972012-12-13 23:53:30 +0000395static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
396{
397 tfile->detached = tun;
398 list_add_tail(&tfile->next, &tun->disabled);
399 ++tun->numdisabled;
400}
401
Jason Wangd32649d2012-12-18 11:00:27 +0800402static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
Jason Wang4008e972012-12-13 23:53:30 +0000403{
404 struct tun_struct *tun = tfile->detached;
405
406 tfile->detached = NULL;
407 list_del_init(&tfile->next);
408 --tun->numdisabled;
409 return tun;
410}
411
Jason Wang4bfb0512013-09-05 17:53:59 +0800412static void tun_queue_purge(struct tun_file *tfile)
413{
414 skb_queue_purge(&tfile->sk.sk_receive_queue);
415 skb_queue_purge(&tfile->sk.sk_error_queue);
416}
417
Jason Wangc8d68e62012-10-31 19:46:00 +0000418static void __tun_detach(struct tun_file *tfile, bool clean)
419{
420 struct tun_file *ntfile;
421 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000422
Jason Wangb8deabd2013-01-11 16:59:32 +0000423 tun = rtnl_dereference(tfile->tun);
424
Jason Wang9e857222013-01-28 01:05:19 +0000425 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000426 u16 index = tfile->queue_index;
427 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000428
429 rcu_assign_pointer(tun->tfiles[index],
430 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000431 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000432 ntfile->queue_index = index;
433
434 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000435 if (clean) {
436 rcu_assign_pointer(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000437 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000438 } else
Jason Wang4008e972012-12-13 23:53:30 +0000439 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000440
441 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000442 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000443 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800444 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000445 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000446 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000447 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000448 sock_put(&tfile->sk);
449 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000450
451 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000452 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
453 netif_carrier_off(tun->dev);
454
455 if (!(tun->flags & TUN_PERSIST) &&
456 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000457 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000458 }
Jason Wang4008e972012-12-13 23:53:30 +0000459
Jason Wangc8d68e62012-10-31 19:46:00 +0000460 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
461 &tfile->socket.flags));
462 sk_release_kernel(&tfile->sk);
463 }
464}
465
466static void tun_detach(struct tun_file *tfile, bool clean)
467{
468 rtnl_lock();
469 __tun_detach(tfile, clean);
470 rtnl_unlock();
471}
472
473static void tun_detach_all(struct net_device *dev)
474{
475 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000476 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000477 int i, n = tun->numqueues;
478
479 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000480 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000481 BUG_ON(!tfile);
482 wake_up_all(&tfile->wq.wait);
483 rcu_assign_pointer(tfile->tun, NULL);
484 --tun->numqueues;
485 }
Jason Wang9e857222013-01-28 01:05:19 +0000486 list_for_each_entry(tfile, &tun->disabled, next) {
487 wake_up_all(&tfile->wq.wait);
488 rcu_assign_pointer(tfile->tun, NULL);
489 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000490 BUG_ON(tun->numqueues != 0);
491
492 synchronize_net();
493 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000494 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000495 /* Drop read queue */
Jason Wang4bfb0512013-09-05 17:53:59 +0800496 tun_queue_purge(tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000497 sock_put(&tfile->sk);
498 }
Jason Wang4008e972012-12-13 23:53:30 +0000499 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
500 tun_enable_queue(tfile);
Jason Wang4bfb0512013-09-05 17:53:59 +0800501 tun_queue_purge(tfile);
Jason Wang4008e972012-12-13 23:53:30 +0000502 sock_put(&tfile->sk);
503 }
504 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000505
506 if (tun->flags & TUN_PERSIST)
507 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000508}
509
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400510static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000511{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000512 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000513 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000514
Paul Moore5dbbaf22013-01-14 07:12:19 +0000515 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
516 if (err < 0)
517 goto out;
518
Eric W. Biederman38231b72009-01-20 11:02:28 +0000519 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000520 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000521 goto out;
522
523 err = -EBUSY;
Jason Wangc8d68e62012-10-31 19:46:00 +0000524 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
525 goto out;
526
527 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000528 if (!tfile->detached &&
529 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000530 goto out;
531
532 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000533
Jason Wangc8d68e62012-10-31 19:46:00 +0000534 /* Re-attach the filter to presist device */
Pavel Emelyanov849c9b62013-08-21 14:32:21 +0400535 if (!skip_filter && (tun->filter_attached == true)) {
Jason Wang54f968d2012-10-31 19:45:57 +0000536 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
537 if (!err)
538 goto out;
539 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000540 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000541 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000542 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000543 tun->numqueues++;
544
Jason Wang4008e972012-12-13 23:53:30 +0000545 if (tfile->detached)
546 tun_enable_queue(tfile);
547 else
548 sock_hold(&tfile->sk);
549
Jason Wangc8d68e62012-10-31 19:46:00 +0000550 tun_set_real_num_queues(tun);
551
Jason Wangc8d68e62012-10-31 19:46:00 +0000552 /* device is allowed to go away first, so no need to hold extra
553 * refcnt.
554 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000555
Eric W. Biederman38231b72009-01-20 11:02:28 +0000556out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000557 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000558}
559
Eric W. Biederman631ab462009-01-20 11:00:40 +0000560static struct tun_struct *__tun_get(struct tun_file *tfile)
561{
Jason Wang6e914fc2012-10-31 19:45:58 +0000562 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000563
Jason Wang6e914fc2012-10-31 19:45:58 +0000564 rcu_read_lock();
565 tun = rcu_dereference(tfile->tun);
566 if (tun)
567 dev_hold(tun->dev);
568 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000569
570 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000571}
572
573static struct tun_struct *tun_get(struct file *file)
574{
575 return __tun_get(file->private_data);
576}
577
578static void tun_put(struct tun_struct *tun)
579{
Jason Wang6e914fc2012-10-31 19:45:58 +0000580 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000581}
582
Joe Perches6b8a66e2011-03-02 07:18:10 +0000583/* TAP filtering */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700584static void addr_hash_set(u32 *mask, const u8 *addr)
585{
586 int n = ether_crc(ETH_ALEN, addr) >> 26;
587 mask[n >> 5] |= (1 << (n & 31));
588}
589
590static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
591{
592 int n = ether_crc(ETH_ALEN, addr) >> 26;
593 return mask[n >> 5] & (1 << (n & 31));
594}
595
596static int update_filter(struct tap_filter *filter, void __user *arg)
597{
598 struct { u8 u[ETH_ALEN]; } *addr;
599 struct tun_filter uf;
600 int err, alen, n, nexact;
601
602 if (copy_from_user(&uf, arg, sizeof(uf)))
603 return -EFAULT;
604
605 if (!uf.count) {
606 /* Disabled */
607 filter->count = 0;
608 return 0;
609 }
610
611 alen = ETH_ALEN * uf.count;
612 addr = kmalloc(alen, GFP_KERNEL);
613 if (!addr)
614 return -ENOMEM;
615
616 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
617 err = -EFAULT;
618 goto done;
619 }
620
621 /* The filter is updated without holding any locks. Which is
622 * perfectly safe. We disable it first and in the worst
623 * case we'll accept a few undesired packets. */
624 filter->count = 0;
625 wmb();
626
627 /* Use first set of addresses as an exact filter */
628 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
629 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
630
631 nexact = n;
632
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800633 /* Remaining multicast addresses are hashed,
634 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700635 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800636 for (; n < uf.count; n++) {
637 if (!is_multicast_ether_addr(addr[n].u)) {
638 err = 0; /* no filter */
639 goto done;
640 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700641 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800642 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700643
644 /* For ALLMULTI just set the mask to all ones.
645 * This overrides the mask populated above. */
646 if ((uf.flags & TUN_FLT_ALLMULTI))
647 memset(filter->mask, ~0, sizeof(filter->mask));
648
649 /* Now enable the filter */
650 wmb();
651 filter->count = nexact;
652
653 /* Return the number of exact filters */
654 err = nexact;
655
656done:
657 kfree(addr);
658 return err;
659}
660
661/* Returns: 0 - drop, !=0 - accept */
662static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
663{
664 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
665 * at this point. */
666 struct ethhdr *eh = (struct ethhdr *) skb->data;
667 int i;
668
669 /* Exact match */
670 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000671 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700672 return 1;
673
674 /* Inexact match (multicast only) */
675 if (is_multicast_ether_addr(eh->h_dest))
676 return addr_hash_test(filter->mask, eh->h_dest);
677
678 return 0;
679}
680
681/*
682 * Checks whether the packet is accepted or not.
683 * Returns: 0 - drop, !=0 - accept
684 */
685static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
686{
687 if (!filter->count)
688 return 1;
689
690 return run_filter(filter, skb);
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/* Network device part of the driver */
694
Jeff Garzik7282d492006-09-13 14:30:00 -0400695static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000697/* Net device detach from fd. */
698static void tun_net_uninit(struct net_device *dev)
699{
Jason Wangc8d68e62012-10-31 19:46:00 +0000700 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/* Net device open. */
704static int tun_net_open(struct net_device *dev)
705{
Jason Wangc8d68e62012-10-31 19:46:00 +0000706 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708}
709
710/* Net device close. */
711static int tun_net_close(struct net_device *dev)
712{
Jason Wangc8d68e62012-10-31 19:46:00 +0000713 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return 0;
715}
716
717/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000718static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000721 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000722 struct tun_file *tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Jason Wang6e914fc2012-10-31 19:45:58 +0000724 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000725 tfile = rcu_dereference(tun->tfiles[txq]);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* Drop packet if interface is not attached */
Jason Wangc8d68e62012-10-31 19:46:00 +0000728 if (txq >= tun->numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 goto drop;
730
Jason Wang6e914fc2012-10-31 19:45:58 +0000731 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
732
Jason Wangc8d68e62012-10-31 19:46:00 +0000733 BUG_ON(!tfile);
734
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700735 /* Drop if the filter does not like it.
736 * This is a noop if the filter is disabled.
737 * Filter can be enabled only for the TAP devices. */
738 if (!check_filter(&tun->txflt, skb))
739 goto drop;
740
Jason Wang54f968d2012-10-31 19:45:57 +0000741 if (tfile->socket.sk->sk_filter &&
742 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000743 goto drop;
744
Rami Rosen36fe8c092012-11-25 22:07:40 +0000745 /* Limit the number of packets queued by dividing txq length with the
Jason Wangc8d68e62012-10-31 19:46:00 +0000746 * number of queues.
747 */
Jason Wang54f968d2012-10-31 19:45:57 +0000748 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
Michael S. Tsirkin5d097102012-12-03 10:07:14 +0000749 >= dev->tx_queue_len / tun->numqueues)
750 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Jason Wang7bf66302013-09-05 17:54:00 +0800752 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
753 goto drop;
754
Richard Cochraneda29772013-07-19 19:40:10 +0200755 if (skb->sk) {
756 sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
757 sw_tx_timestamp(skb);
758 }
759
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000760 /* Orphan the skb - required as we might hang on to it
Jason Wang7bf66302013-09-05 17:54:00 +0800761 * for indefinite time.
762 */
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000763 skb_orphan(skb);
764
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000765 nf_reset(skb);
766
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700767 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000768 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000771 if (tfile->flags & TUN_FASYNC)
772 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
773 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000774 POLLRDNORM | POLLRDBAND);
Jason Wang6e914fc2012-10-31 19:45:58 +0000775
776 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000777 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700780 dev->stats.tx_dropped++;
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000781 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000783 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000784 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700787static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700789 /*
790 * This callback is supposed to deal with mc filter in
791 * _rx_ path and has nothing to do with the _tx_ path.
792 * In rx path we always accept everything userspace gives us.
793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Ed Swierk4885a502007-09-16 12:21:38 -0700796#define MIN_MTU 68
797#define MAX_MTU 65535
798
799static int
800tun_net_change_mtu(struct net_device *dev, int new_mtu)
801{
802 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
803 return -EINVAL;
804 dev->mtu = new_mtu;
805 return 0;
806}
807
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000808static netdev_features_t tun_net_fix_features(struct net_device *dev,
809 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000810{
811 struct tun_struct *tun = netdev_priv(dev);
812
813 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
814}
Neil Hormanbebd0972011-06-15 05:25:01 +0000815#ifdef CONFIG_NET_POLL_CONTROLLER
816static void tun_poll_controller(struct net_device *dev)
817{
818 /*
819 * Tun only receives frames when:
820 * 1) the char device endpoint gets data from user space
821 * 2) the tun socket gets a sendmsg call from user space
822 * Since both of those are syncronous operations, we are guaranteed
823 * never to have pending data when we poll for it
824 * so theres nothing to do here but return.
825 * We need this though so netpoll recognizes us as an interface that
826 * supports polling, which enables bridge devices in virt setups to
827 * still use netconsole
828 */
829 return;
830}
831#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800832static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000833 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800834 .ndo_open = tun_net_open,
835 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800836 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800837 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000838 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000839 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000840#ifdef CONFIG_NET_POLL_CONTROLLER
841 .ndo_poll_controller = tun_poll_controller,
842#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800843};
844
845static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000846 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800847 .ndo_open = tun_net_open,
848 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800849 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800850 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000851 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000852 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800853 .ndo_set_mac_address = eth_mac_addr,
854 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000855 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000856#ifdef CONFIG_NET_POLL_CONTROLLER
857 .ndo_poll_controller = tun_poll_controller,
858#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800859};
860
Pavel Emelyanov944a1372013-06-11 17:01:08 +0400861static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +0000862{
863 int i;
864
Jason Wang96442e422012-10-31 19:46:02 +0000865 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
866 INIT_HLIST_HEAD(&tun->flows[i]);
867
868 tun->ageing_time = TUN_FLOW_EXPIRE;
869 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
870 mod_timer(&tun->flow_gc_timer,
871 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +0000872}
873
874static void tun_flow_uninit(struct tun_struct *tun)
875{
876 del_timer_sync(&tun->flow_gc_timer);
877 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +0000878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/* Initialize net device. */
881static void tun_net_init(struct net_device *dev)
882{
883 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 switch (tun->flags & TUN_TYPE_MASK) {
886 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800887 dev->netdev_ops = &tun_netdev_ops;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* Point-to-Point TUN Device */
890 dev->hard_header_len = 0;
891 dev->addr_len = 0;
892 dev->mtu = 1500;
893
894 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400895 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
897 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
898 break;
899
900 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800901 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* Ethernet TAP Device */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700903 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000904 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +0000905 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000907 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
910 break;
911 }
912}
913
914/* Character device part */
915
916/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000917static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400918{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000919 struct tun_file *tfile = file->private_data;
920 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000921 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800922 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000925 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Jason Wang54f968d2012-10-31 19:45:57 +0000927 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000928
Joe Perches6b8a66e2011-03-02 07:18:10 +0000929 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Jason Wang54f968d2012-10-31 19:45:57 +0000931 poll_wait(file, &tfile->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400932
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000933 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 mask |= POLLIN | POLLRDNORM;
935
Herbert Xu33dccbb2009-02-05 21:25:32 -0800936 if (sock_writeable(sk) ||
937 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
938 sock_writeable(sk)))
939 mask |= POLLOUT | POLLWRNORM;
940
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000941 if (tun->dev->reg_state != NETREG_REGISTERED)
942 mask = POLLERR;
943
Eric W. Biederman631ab462009-01-20 11:00:40 +0000944 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return mask;
946}
947
Rusty Russellf42157c2008-08-15 15:15:10 -0700948/* prepad is the amount to reserve at front. len is length after that.
949 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000950static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000951 size_t prepad, size_t len,
952 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700953{
Jason Wang54f968d2012-10-31 19:45:57 +0000954 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700955 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800956 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700957
958 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700959 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800960 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700961
Herbert Xu33dccbb2009-02-05 21:25:32 -0800962 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -0700963 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -0700964 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800965 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700966
967 skb_reserve(skb, prepad);
968 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800969 skb->data_len = len - linear;
970 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700971
972 return skb;
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +0000976static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
977 void *msg_control, const struct iovec *iv,
978 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Harvey Harrison09640e62009-02-01 00:45:17 -0800980 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct sk_buff *skb;
Jason Wang3dd5c332013-07-10 13:43:27 +0800982 size_t len = total_len, align = NET_SKB_PAD, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -0700983 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000984 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000985 int copylen;
986 bool zerocopy = false;
987 int err;
Eric Dumazet49974422012-12-12 19:22:57 +0000988 u32 rxhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (!(tun->flags & TUN_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +0300991 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +0300993 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000995 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000997 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Rusty Russellf43798c2008-07-03 03:48:02 -07001000 if (tun->flags & TUN_VNET_HDR) {
Dan Carpenter15718ea2013-08-15 15:52:57 +03001001 if (len < tun->vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -07001002 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +03001003 len -= tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001004
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +00001005 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -07001006 return -EFAULT;
1007
Herbert Xu49091222009-06-08 00:20:01 -07001008 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1009 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
1010 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
1011
Rusty Russellf43798c2008-07-03 03:48:02 -07001012 if (gso.hdr_len > len)
1013 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001014 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001015 }
1016
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001017 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +00001018 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001019 if (unlikely(len < ETH_HLEN ||
1020 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001021 return -EINVAL;
1022 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001023
Jason Wang88529172013-07-18 10:55:15 +08001024 if (msg_control) {
1025 /* There are 256 bytes to be copied in skb, so there is
1026 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001027 * The rest of the buffer is mapped from userspace.
1028 */
Jason Wang88529172013-07-18 10:55:15 +08001029 copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN;
Jason Wang3dd5c332013-07-10 13:43:27 +08001030 linear = copylen;
Jason Wang88529172013-07-18 10:55:15 +08001031 if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
1032 zerocopy = true;
1033 }
1034
1035 if (!zerocopy) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001036 copylen = len;
Jason Wang3dd5c332013-07-10 13:43:27 +08001037 linear = gso.hdr_len;
1038 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001039
Jason Wang3dd5c332013-07-10 13:43:27 +08001040 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001041 if (IS_ERR(skb)) {
1042 if (PTR_ERR(skb) != -EAGAIN)
1043 tun->dev->stats.rx_dropped++;
1044 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001047 if (zerocopy)
1048 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
Jason Wang88529172013-07-18 10:55:15 +08001049 else {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001050 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
Jason Wang88529172013-07-18 10:55:15 +08001051 if (!err && msg_control) {
1052 struct ubuf_info *uarg = msg_control;
1053 uarg->callback(uarg, false);
1054 }
1055 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001056
1057 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001058 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -08001059 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -08001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Rusty Russellf43798c2008-07-03 03:48:02 -07001063 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1064 if (!skb_partial_csum_set(skb, gso.csum_start,
1065 gso.csum_offset)) {
1066 tun->dev->stats.rx_frame_errors++;
1067 kfree_skb(skb);
1068 return -EINVAL;
1069 }
Michał Mirosław88255372011-04-19 06:13:10 +00001070 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 switch (tun->flags & TUN_TYPE_MASK) {
1073 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001074 if (tun->flags & TUN_NO_PI) {
1075 switch (skb->data[0] & 0xf0) {
1076 case 0x40:
1077 pi.proto = htons(ETH_P_IP);
1078 break;
1079 case 0x60:
1080 pi.proto = htons(ETH_P_IPV6);
1081 break;
1082 default:
1083 tun->dev->stats.rx_dropped++;
1084 kfree_skb(skb);
1085 return -EINVAL;
1086 }
1087 }
1088
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001089 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001091 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
1093 case TUN_TAP_DEV:
1094 skb->protocol = eth_type_trans(skb, tun->dev);
1095 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Rusty Russellf43798c2008-07-03 03:48:02 -07001098 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1099 pr_debug("GSO!\n");
1100 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1101 case VIRTIO_NET_HDR_GSO_TCPV4:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001102 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Rusty Russellf43798c2008-07-03 03:48:02 -07001103 break;
1104 case VIRTIO_NET_HDR_GSO_TCPV6:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001105 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Rusty Russellf43798c2008-07-03 03:48:02 -07001106 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001107 case VIRTIO_NET_HDR_GSO_UDP:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001108 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001109 break;
Rusty Russellf43798c2008-07-03 03:48:02 -07001110 default:
1111 tun->dev->stats.rx_frame_errors++;
1112 kfree_skb(skb);
1113 return -EINVAL;
1114 }
1115
1116 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001117 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Rusty Russellf43798c2008-07-03 03:48:02 -07001118
1119 skb_shinfo(skb)->gso_size = gso.gso_size;
1120 if (skb_shinfo(skb)->gso_size == 0) {
1121 tun->dev->stats.rx_frame_errors++;
1122 kfree_skb(skb);
1123 return -EINVAL;
1124 }
1125
1126 /* Header must be checked, and gso_segs computed. */
1127 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1128 skb_shinfo(skb)->gso_segs = 0;
1129 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001130
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001131 /* copy skb_ubuf_info for callback when skb has no error */
1132 if (zerocopy) {
1133 skb_shinfo(skb)->destructor_arg = msg_control;
1134 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001135 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001136 }
1137
Eric Dumazet76fe4582012-12-17 04:39:20 +00001138 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001139 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001140
Eric Dumazet49974422012-12-12 19:22:57 +00001141 rxhash = skb_get_rxhash(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001143
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001144 tun->dev->stats.rx_packets++;
1145 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Jason Wang9e857222013-01-28 01:05:19 +00001147 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001148 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001151static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1152 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001154 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -08001155 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001156 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001157 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 if (!tun)
1160 return -EBADFD;
1161
Joe Perches6b8a66e2011-03-02 07:18:10 +00001162 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jason Wang54f968d2012-10-31 19:45:57 +00001164 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1165 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001166
1167 tun_put(tun);
1168 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001172static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001173 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001174 struct sk_buff *skb,
1175 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct tun_pi pi = { 0, skb->protocol };
1178 ssize_t total = 0;
Jason Wang6680ec62013-07-25 13:00:33 +08001179 int vlan_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 if (!(tun->flags & TUN_NO_PI)) {
1182 if ((len -= sizeof(pi)) < 0)
1183 return -EINVAL;
1184
1185 if (len < skb->len) {
1186 /* Packet will be striped */
1187 pi.flags |= TUN_PKT_STRIP;
1188 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001189
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001190 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return -EFAULT;
1192 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Rusty Russellf43798c2008-07-03 03:48:02 -07001195 if (tun->flags & TUN_VNET_HDR) {
1196 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001197 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001198 return -EINVAL;
1199
1200 if (skb_is_gso(skb)) {
1201 struct skb_shared_info *sinfo = skb_shinfo(skb);
1202
1203 /* This is a hint as to how much should be linear. */
1204 gso.hdr_len = skb_headlen(skb);
1205 gso.gso_size = sinfo->gso_size;
1206 if (sinfo->gso_type & SKB_GSO_TCPV4)
1207 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1208 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1209 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001210 else if (sinfo->gso_type & SKB_GSO_UDP)
1211 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001212 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001213 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001214 "0x%x, gso_size %d, hdr_len %d\n",
1215 sinfo->gso_type, gso.gso_size,
1216 gso.hdr_len);
1217 print_hex_dump(KERN_ERR, "tun: ",
1218 DUMP_PREFIX_NONE,
1219 16, 1, skb->head,
1220 min((int)gso.hdr_len, 64), true);
1221 WARN_ON_ONCE(1);
1222 return -EINVAL;
1223 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001224 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1225 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1226 } else
1227 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1228
1229 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1230 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001231 gso.csum_start = skb_checksum_start_offset(skb);
Rusty Russellf43798c2008-07-03 03:48:02 -07001232 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001233 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1234 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001235 } /* else everything is zero */
1236
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001237 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1238 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001239 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001240 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001241 }
1242
Jason Wang6680ec62013-07-25 13:00:33 +08001243 if (!vlan_tx_tag_present(skb)) {
1244 len = min_t(int, skb->len, len);
1245 } else {
1246 int copy, ret;
1247 struct {
1248 __be16 h_vlan_proto;
1249 __be16 h_vlan_TCI;
1250 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jason Wang6680ec62013-07-25 13:00:33 +08001252 veth.h_vlan_proto = skb->vlan_proto;
1253 veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jason Wang6680ec62013-07-25 13:00:33 +08001255 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
1256 len = min_t(int, skb->len + VLAN_HLEN, len);
1257
1258 copy = min_t(int, vlan_offset, len);
1259 ret = skb_copy_datagram_const_iovec(skb, 0, iv, total, copy);
1260 len -= copy;
1261 total += copy;
1262 if (ret || !len)
1263 goto done;
1264
1265 copy = min_t(int, sizeof(veth), len);
1266 ret = memcpy_toiovecend(iv, (void *)&veth, total, copy);
1267 len -= copy;
1268 total += copy;
1269 if (ret || !len)
1270 goto done;
1271 }
1272
1273 skb_copy_datagram_const_iovec(skb, vlan_offset, iv, total, len);
1274 total += len;
1275
1276done:
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001277 tun->dev->stats.tx_packets++;
1278 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 return total;
1281}
1282
Jason Wang54f968d2012-10-31 19:45:57 +00001283static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001284 struct kiocb *iocb, const struct iovec *iv,
1285 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 DECLARE_WAITQUEUE(wait, current);
1288 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001289 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Rami Rosen3872baf2012-11-25 22:07:41 +00001291 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Amos Kong61a5ff12011-06-09 00:27:10 -07001293 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001294 add_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 current->state = TASK_INTERRUPTIBLE;
1297
1298 /* Read frames from the queue */
Jason Wang54f968d2012-10-31 19:45:57 +00001299 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001300 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 ret = -EAGAIN;
1302 break;
1303 }
1304 if (signal_pending(current)) {
1305 ret = -ERESTARTSYS;
1306 break;
1307 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001308 if (tun->dev->reg_state != NETREG_REGISTERED) {
1309 ret = -EIO;
1310 break;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* Nothing to read, let's sleep */
1314 schedule();
1315 continue;
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Jason Wang54f968d2012-10-31 19:45:57 +00001318 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001319 kfree_skb(skb);
1320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
1323 current->state = TASK_RUNNING;
Amos Kong61a5ff12011-06-09 00:27:10 -07001324 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001325 remove_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001327 return ret;
1328}
1329
1330static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1331 unsigned long count, loff_t pos)
1332{
1333 struct file *file = iocb->ki_filp;
1334 struct tun_file *tfile = file->private_data;
1335 struct tun_struct *tun = __tun_get(tfile);
1336 ssize_t len, ret;
1337
1338 if (!tun)
1339 return -EBADFD;
1340 len = iov_length(iv, count);
1341 if (len < 0) {
1342 ret = -EINVAL;
1343 goto out;
1344 }
1345
Jason Wang54f968d2012-10-31 19:45:57 +00001346 ret = tun_do_read(tun, tfile, iocb, iv, len,
1347 file->f_flags & O_NONBLOCK);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001348 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001349out:
1350 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return ret;
1352}
1353
Jason Wang96442e422012-10-31 19:46:02 +00001354static void tun_free_netdev(struct net_device *dev)
1355{
1356 struct tun_struct *tun = netdev_priv(dev);
1357
Jason Wang4008e972012-12-13 23:53:30 +00001358 BUG_ON(!(list_empty(&tun->disabled)));
Jason Wang96442e422012-10-31 19:46:02 +00001359 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001360 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001361 free_netdev(dev);
1362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static void tun_setup(struct net_device *dev)
1365{
1366 struct tun_struct *tun = netdev_priv(dev);
1367
Eric W. Biederman0625c882012-02-07 16:48:55 -08001368 tun->owner = INVALID_UID;
1369 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang96442e422012-10-31 19:46:02 +00001372 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001375/* Trivial set of netlink ops to allow deleting tun or tap
1376 * device with netlink.
1377 */
1378static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1379{
1380 return -EINVAL;
1381}
1382
1383static struct rtnl_link_ops tun_link_ops __read_mostly = {
1384 .kind = DRV_NAME,
1385 .priv_size = sizeof(struct tun_struct),
1386 .setup = tun_setup,
1387 .validate = tun_validate,
1388};
1389
Herbert Xu33dccbb2009-02-05 21:25:32 -08001390static void tun_sock_write_space(struct sock *sk)
1391{
Jason Wang54f968d2012-10-31 19:45:57 +00001392 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001393 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001394
1395 if (!sock_writeable(sk))
1396 return;
1397
Herbert Xu33dccbb2009-02-05 21:25:32 -08001398 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1399 return;
1400
Eric Dumazet43815482010-04-29 11:01:49 +00001401 wqueue = sk_sleep(sk);
1402 if (wqueue && waitqueue_active(wqueue))
1403 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001404 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001405
Jason Wang54f968d2012-10-31 19:45:57 +00001406 tfile = container_of(sk, struct tun_file, sk);
1407 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001408}
1409
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001410static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1411 struct msghdr *m, size_t total_len)
1412{
Jason Wang54f968d2012-10-31 19:45:57 +00001413 int ret;
1414 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1415 struct tun_struct *tun = __tun_get(tfile);
1416
1417 if (!tun)
1418 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001419 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1420 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1421 tun_put(tun);
1422 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001423}
1424
1425static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1426 struct msghdr *m, size_t total_len,
1427 int flags)
1428{
Jason Wang54f968d2012-10-31 19:45:57 +00001429 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1430 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001431 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001432
1433 if (!tun)
1434 return -EBADFD;
1435
Richard Cochraneda29772013-07-19 19:40:10 +02001436 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00001437 ret = -EINVAL;
1438 goto out;
1439 }
Richard Cochraneda29772013-07-19 19:40:10 +02001440 if (flags & MSG_ERRQUEUE) {
1441 ret = sock_recv_errqueue(sock->sk, m, total_len,
1442 SOL_PACKET, TUN_TX_TIMESTAMP);
1443 goto out;
1444 }
Jason Wang54f968d2012-10-31 19:45:57 +00001445 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001446 flags & MSG_DONTWAIT);
1447 if (ret > total_len) {
1448 m->msg_flags |= MSG_TRUNC;
1449 ret = flags & MSG_TRUNC ? ret : total_len;
1450 }
Gao feng3811ae72013-04-24 21:59:23 +00001451out:
Jason Wang54f968d2012-10-31 19:45:57 +00001452 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001453 return ret;
1454}
1455
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001456static int tun_release(struct socket *sock)
1457{
1458 if (sock->sk)
1459 sock_put(sock->sk);
1460 return 0;
1461}
1462
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001463/* Ops structure to mimic raw sockets with tun */
1464static const struct proto_ops tun_socket_ops = {
1465 .sendmsg = tun_sendmsg,
1466 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001467 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001468};
1469
Herbert Xu33dccbb2009-02-05 21:25:32 -08001470static struct proto tun_proto = {
1471 .name = "tun",
1472 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001473 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001474};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001475
David Woodhouse980c9e82009-05-09 22:54:21 -07001476static int tun_flags(struct tun_struct *tun)
1477{
1478 int flags = 0;
1479
1480 if (tun->flags & TUN_TUN_DEV)
1481 flags |= IFF_TUN;
1482 else
1483 flags |= IFF_TAP;
1484
1485 if (tun->flags & TUN_NO_PI)
1486 flags |= IFF_NO_PI;
1487
Michael S. Tsirkin5d097102012-12-03 10:07:14 +00001488 /* This flag has no real effect. We track the value for backwards
1489 * compatibility.
1490 */
David Woodhouse980c9e82009-05-09 22:54:21 -07001491 if (tun->flags & TUN_ONE_QUEUE)
1492 flags |= IFF_ONE_QUEUE;
1493
1494 if (tun->flags & TUN_VNET_HDR)
1495 flags |= IFF_VNET_HDR;
1496
Jason Wangc8d68e62012-10-31 19:46:00 +00001497 if (tun->flags & TUN_TAP_MQ)
1498 flags |= IFF_MULTI_QUEUE;
1499
Pavel Emelyanov274038f2013-06-11 14:41:24 +04001500 if (tun->flags & TUN_PERSIST)
1501 flags |= IFF_PERSIST;
1502
David Woodhouse980c9e82009-05-09 22:54:21 -07001503 return flags;
1504}
1505
1506static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1507 char *buf)
1508{
1509 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1510 return sprintf(buf, "0x%x\n", tun_flags(tun));
1511}
1512
1513static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1514 char *buf)
1515{
1516 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001517 return uid_valid(tun->owner)?
1518 sprintf(buf, "%u\n",
1519 from_kuid_munged(current_user_ns(), tun->owner)):
1520 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001521}
1522
1523static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1524 char *buf)
1525{
1526 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001527 return gid_valid(tun->group) ?
1528 sprintf(buf, "%u\n",
1529 from_kgid_munged(current_user_ns(), tun->group)):
1530 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001531}
1532
1533static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1534static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1535static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1536
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001537static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
1539 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001540 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct net_device *dev;
1542 int err;
1543
Jason Wang7c0c3b12013-01-11 16:59:33 +00001544 if (tfile->detached)
1545 return -EINVAL;
1546
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001547 dev = __dev_get_by_name(net, ifr->ifr_name);
1548 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001549 if (ifr->ifr_flags & IFF_TUN_EXCL)
1550 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001551 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1552 tun = netdev_priv(dev);
1553 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1554 tun = netdev_priv(dev);
1555 else
1556 return -EINVAL;
1557
Jason Wang8e6d91a2013-05-28 18:32:11 +00001558 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
1559 !!(tun->flags & TUN_TAP_MQ))
1560 return -EINVAL;
1561
Jason Wangcde8b152012-10-31 19:46:01 +00001562 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001563 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001564 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001565 if (err < 0)
1566 return err;
1567
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001568 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001569 if (err < 0)
1570 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001571
1572 if (tun->flags & TUN_TAP_MQ &&
Jason Wange8dbad62013-04-22 20:40:39 +00001573 (tun->numqueues + tun->numdisabled > 1)) {
1574 /* One or more queue has already been attached, no need
1575 * to initialize the device again.
1576 */
1577 return 0;
1578 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 else {
1581 char *name;
1582 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001583 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1584 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001586 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001587 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001588 err = security_tun_dev_create();
1589 if (err < 0)
1590 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Set dev type */
1593 if (ifr->ifr_flags & IFF_TUN) {
1594 /* TUN device */
1595 flags |= TUN_TUN_DEV;
1596 name = "tun%d";
1597 } else if (ifr->ifr_flags & IFF_TAP) {
1598 /* TAP device */
1599 flags |= TUN_TAP_DEV;
1600 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001601 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001602 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (*ifr->ifr_name)
1605 name = ifr->ifr_name;
1606
Jason Wangc8d68e62012-10-31 19:46:00 +00001607 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Jason Wangedfb6a12013-01-23 03:59:12 +00001608 tun_setup, queues, queues);
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (!dev)
1611 return -ENOMEM;
1612
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001613 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001614 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001615 dev->ifindex = tfile->ifindex;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 tun = netdev_priv(dev);
1618 tun->dev = dev;
1619 tun->flags = flags;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001620 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001621 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Jason Wang54f968d2012-10-31 19:45:57 +00001623 tun->filter_attached = false;
1624 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001625
Jason Wang96442e422012-10-31 19:46:02 +00001626 spin_lock_init(&tun->lock);
1627
Paul Moore5dbbaf22013-01-14 07:12:19 +00001628 err = security_tun_dev_alloc_security(&tun->security);
1629 if (err < 0)
1630 goto err_free_dev;
Paul Moore2b980db2009-08-28 18:12:43 -04001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001633 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001634
Michał Mirosław88255372011-04-19 06:13:10 +00001635 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08001636 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
1637 NETIF_F_HW_VLAN_STAG_TX;
Michał Mirosław88255372011-04-19 06:13:10 +00001638 dev->features = dev->hw_features;
Jason Wangc0317992013-04-10 23:32:22 +00001639 dev->vlan_features = dev->features;
Michał Mirosław88255372011-04-19 06:13:10 +00001640
Jason Wang4008e972012-12-13 23:53:30 +00001641 INIT_LIST_HEAD(&tun->disabled);
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001642 err = tun_attach(tun, file, false);
Jason Wangeb0fb362012-12-02 17:19:45 +00001643 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001644 goto err_free_flow;
Jason Wangeb0fb362012-12-02 17:19:45 +00001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 err = register_netdevice(tun->dev);
1647 if (err < 0)
Jason Wang662ca432013-09-11 18:09:48 +08001648 goto err_detach;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001649
David Woodhouse980c9e82009-05-09 22:54:21 -07001650 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1651 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1652 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001653 pr_err("Failed to create tun sysfs files\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00001656 netif_carrier_on(tun->dev);
1657
Joe Perches6b8a66e2011-03-02 07:18:10 +00001658 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 if (ifr->ifr_flags & IFF_NO_PI)
1661 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001662 else
1663 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Michael S. Tsirkin5d097102012-12-03 10:07:14 +00001665 /* This flag has no real effect. We track the value for backwards
1666 * compatibility.
1667 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1669 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001670 else
1671 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Rusty Russellf43798c2008-07-03 03:48:02 -07001673 if (ifr->ifr_flags & IFF_VNET_HDR)
1674 tun->flags |= TUN_VNET_HDR;
1675 else
1676 tun->flags &= ~TUN_VNET_HDR;
1677
Jason Wangc8d68e62012-10-31 19:46:00 +00001678 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1679 tun->flags |= TUN_TAP_MQ;
1680 else
1681 tun->flags &= ~TUN_TAP_MQ;
1682
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001683 /* Make sure persistent devices do not get stuck in
1684 * xoff state.
1685 */
1686 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001687 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 strcpy(ifr->ifr_name, tun->dev->name);
1690 return 0;
1691
Jason Wang662ca432013-09-11 18:09:48 +08001692err_detach:
1693 tun_detach_all(dev);
1694err_free_flow:
1695 tun_flow_uninit(tun);
1696 security_tun_dev_free_security(tun->security);
1697err_free_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return err;
1700}
1701
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001702static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00001703 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001704{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001705 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001706
1707 strcpy(ifr->ifr_name, tun->dev->name);
1708
David Woodhouse980c9e82009-05-09 22:54:21 -07001709 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001710
Mark McLoughline3b99552008-08-15 15:09:56 -07001711}
1712
Rusty Russell5228ddc2008-07-03 03:46:16 -07001713/* This is like a cut-down ethtool ops, except done via tun fd so no
1714 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001715static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001716{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001717 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001718
1719 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001720 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001721 arg &= ~TUN_F_CSUM;
1722
1723 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1724 if (arg & TUN_F_TSO_ECN) {
1725 features |= NETIF_F_TSO_ECN;
1726 arg &= ~TUN_F_TSO_ECN;
1727 }
1728 if (arg & TUN_F_TSO4)
1729 features |= NETIF_F_TSO;
1730 if (arg & TUN_F_TSO6)
1731 features |= NETIF_F_TSO6;
1732 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1733 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001734
1735 if (arg & TUN_F_UFO) {
1736 features |= NETIF_F_UFO;
1737 arg &= ~TUN_F_UFO;
1738 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001739 }
1740
1741 /* This gives the user a way to test for new features in future by
1742 * trying to set them. */
1743 if (arg)
1744 return -EINVAL;
1745
Michał Mirosław88255372011-04-19 06:13:10 +00001746 tun->set_features = features;
1747 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001748
1749 return 0;
1750}
1751
Jason Wangc8d68e62012-10-31 19:46:00 +00001752static void tun_detach_filter(struct tun_struct *tun, int n)
1753{
1754 int i;
1755 struct tun_file *tfile;
1756
1757 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001758 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001759 sk_detach_filter(tfile->socket.sk);
1760 }
1761
1762 tun->filter_attached = false;
1763}
1764
1765static int tun_attach_filter(struct tun_struct *tun)
1766{
1767 int i, ret = 0;
1768 struct tun_file *tfile;
1769
1770 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001771 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001772 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1773 if (ret) {
1774 tun_detach_filter(tun, i);
1775 return ret;
1776 }
1777 }
1778
1779 tun->filter_attached = true;
1780 return ret;
1781}
1782
1783static void tun_set_sndbuf(struct tun_struct *tun)
1784{
1785 struct tun_file *tfile;
1786 int i;
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 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1791 }
1792}
1793
Jason Wangcde8b152012-10-31 19:46:01 +00001794static int tun_set_queue(struct file *file, struct ifreq *ifr)
1795{
1796 struct tun_file *tfile = file->private_data;
1797 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00001798 int ret = 0;
1799
1800 rtnl_lock();
1801
1802 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00001803 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001804 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00001805 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001806 goto unlock;
1807 }
1808 ret = security_tun_dev_attach_queue(tun->security);
1809 if (ret < 0)
1810 goto unlock;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001811 ret = tun_attach(tun, file, false);
Jason Wang4008e972012-12-13 23:53:30 +00001812 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001813 tun = rtnl_dereference(tfile->tun);
Jason Wang9e857222013-01-28 01:05:19 +00001814 if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00001815 ret = -EINVAL;
1816 else
1817 __tun_detach(tfile, false);
1818 } else
Jason Wangcde8b152012-10-31 19:46:01 +00001819 ret = -EINVAL;
1820
Paul Moore5dbbaf22013-01-14 07:12:19 +00001821unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00001822 rtnl_unlock();
1823 return ret;
1824}
1825
Arnd Bergmann50857e22009-11-06 22:52:32 -08001826static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1827 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001829 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001830 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 void __user* argp = (void __user*)arg;
1832 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001833 kuid_t owner;
1834 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001835 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001836 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001837 unsigned int ifindex;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001838 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Jason Wangcde8b152012-10-31 19:46:01 +00001840 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001841 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001843 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001844 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001845 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001846 if (cmd == TUNGETFEATURES) {
1847 /* Currently this just means: "what IFF flags are valid?".
1848 * This is needed because we never checked for invalid flags on
1849 * TUNSETIFF. */
1850 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
Jason Wangcde8b152012-10-31 19:46:01 +00001851 IFF_VNET_HDR | IFF_MULTI_QUEUE,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001852 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001853 } else if (cmd == TUNSETQUEUE)
1854 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001855
Jason Wangc8d68e62012-10-31 19:46:00 +00001856 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001857 rtnl_lock();
1858
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001859 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1862
Herbert Xu876bfd42009-08-06 14:22:44 +00001863 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Herbert Xu876bfd42009-08-06 14:22:44 +00001865 if (ret)
1866 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Arnd Bergmann50857e22009-11-06 22:52:32 -08001868 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001869 ret = -EFAULT;
1870 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001872 if (cmd == TUNSETIFINDEX) {
1873 ret = -EPERM;
1874 if (tun)
1875 goto unlock;
1876
1877 ret = -EFAULT;
1878 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1879 goto unlock;
1880
1881 ret = 0;
1882 tfile->ifindex = ifindex;
1883 goto unlock;
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Herbert Xu876bfd42009-08-06 14:22:44 +00001886 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001888 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Jason Wang1e588332012-10-31 19:45:56 +00001890 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Eric W. Biederman631ab462009-01-20 11:00:40 +00001892 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001894 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001895 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001896
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001897 if (tfile->detached)
1898 ifr.ifr_flags |= IFF_DETACH_QUEUE;
Pavel Emelyanov849c9b62013-08-21 14:32:21 +04001899 if (!tfile->socket.sk->sk_filter)
1900 ifr.ifr_flags |= IFF_NOFILTER;
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001901
Arnd Bergmann50857e22009-11-06 22:52:32 -08001902 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001903 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001904 break;
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 case TUNSETNOCSUM:
1907 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Michał Mirosław88255372011-04-19 06:13:10 +00001909 /* [unimplemented] */
1910 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001911 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 break;
1913
1914 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001915 /* Disable/Enable persist mode. Keep an extra reference to the
1916 * module to prevent the module being unprobed.
1917 */
Jason Wangdd38bd82013-01-11 16:59:34 +00001918 if (arg && !(tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001920 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00001921 }
1922 if (!arg && (tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001924 module_put(THIS_MODULE);
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Joe Perches6b8a66e2011-03-02 07:18:10 +00001927 tun_debug(KERN_INFO, tun, "persist %s\n",
1928 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 break;
1930
1931 case TUNSETOWNER:
1932 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001933 owner = make_kuid(current_user_ns(), arg);
1934 if (!uid_valid(owner)) {
1935 ret = -EINVAL;
1936 break;
1937 }
1938 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001939 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001940 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 break;
1942
Guido Guenther8c644622007-07-02 22:50:25 -07001943 case TUNSETGROUP:
1944 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001945 group = make_kgid(current_user_ns(), arg);
1946 if (!gid_valid(group)) {
1947 ret = -EINVAL;
1948 break;
1949 }
1950 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001951 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001952 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001953 break;
1954
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001955 case TUNSETLINK:
1956 /* Only allow setting the type when the interface is down */
1957 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001958 tun_debug(KERN_INFO, tun,
1959 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001960 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001961 } else {
1962 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001963 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1964 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001965 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001966 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001967 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969#ifdef TUN_DEBUG
1970 case TUNSETDEBUG:
1971 tun->debug = arg;
1972 break;
1973#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001974 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001975 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001976 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001977
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001978 case TUNSETTXFILTER:
1979 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001980 ret = -EINVAL;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001981 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001982 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001983 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001987 /* Get hw address */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001988 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1989 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001990 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001991 ret = -EFAULT;
1992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001995 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00001996 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1997 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001998
Kim B. Heino40102372008-02-29 12:26:21 -08001999 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002000 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002001
2002 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00002003 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08002004 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2005 ret = -EFAULT;
2006 break;
2007
2008 case TUNSETSNDBUF:
2009 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2010 ret = -EFAULT;
2011 break;
2012 }
2013
Jason Wangc8d68e62012-10-31 19:46:00 +00002014 tun->sndbuf = sndbuf;
2015 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002016 break;
2017
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002018 case TUNGETVNETHDRSZ:
2019 vnet_hdr_sz = tun->vnet_hdr_sz;
2020 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2021 ret = -EFAULT;
2022 break;
2023
2024 case TUNSETVNETHDRSZ:
2025 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2026 ret = -EFAULT;
2027 break;
2028 }
2029 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2030 ret = -EINVAL;
2031 break;
2032 }
2033
2034 tun->vnet_hdr_sz = vnet_hdr_sz;
2035 break;
2036
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002037 case TUNATTACHFILTER:
2038 /* Can be set only for TAPs */
2039 ret = -EINVAL;
2040 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2041 break;
2042 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002043 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002044 break;
2045
Jason Wangc8d68e62012-10-31 19:46:00 +00002046 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002047 break;
2048
2049 case TUNDETACHFILTER:
2050 /* Can be set only for TAPs */
2051 ret = -EINVAL;
2052 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2053 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002054 ret = 0;
2055 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002056 break;
2057
Pavel Emelyanov76975e92013-08-21 14:32:39 +04002058 case TUNGETFILTER:
2059 ret = -EINVAL;
2060 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2061 break;
2062 ret = -EFAULT;
2063 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2064 break;
2065 ret = 0;
2066 break;
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002069 ret = -EINVAL;
2070 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Herbert Xu876bfd42009-08-06 14:22:44 +00002073unlock:
2074 rtnl_unlock();
2075 if (tun)
2076 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002077 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Arnd Bergmann50857e22009-11-06 22:52:32 -08002080static long tun_chr_ioctl(struct file *file,
2081 unsigned int cmd, unsigned long arg)
2082{
2083 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2084}
2085
2086#ifdef CONFIG_COMPAT
2087static long tun_chr_compat_ioctl(struct file *file,
2088 unsigned int cmd, unsigned long arg)
2089{
2090 switch (cmd) {
2091 case TUNSETIFF:
2092 case TUNGETIFF:
2093 case TUNSETTXFILTER:
2094 case TUNGETSNDBUF:
2095 case TUNSETSNDBUF:
2096 case SIOCGIFHWADDR:
2097 case SIOCSIFHWADDR:
2098 arg = (unsigned long)compat_ptr(arg);
2099 break;
2100 default:
2101 arg = (compat_ulong_t)arg;
2102 break;
2103 }
2104
2105 /*
2106 * compat_ifreq is shorter than ifreq, so we must not access beyond
2107 * the end of that structure. All fields that are used in this
2108 * driver are compatible though, we don't need to convert the
2109 * contents.
2110 */
2111 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2112}
2113#endif /* CONFIG_COMPAT */
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115static int tun_chr_fasync(int fd, struct file *file, int on)
2116{
Jason Wang54f968d2012-10-31 19:45:57 +00002117 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int ret;
2119
Jason Wang54f968d2012-10-31 19:45:57 +00002120 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002121 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07002124 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002126 goto out;
Jason Wang54f968d2012-10-31 19:45:57 +00002127 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002128 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002129 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002130 ret = 0;
2131out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135static int tun_chr_open(struct inode *inode, struct file * file)
2136{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002137 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002138
Joe Perches6b8a66e2011-03-02 07:18:10 +00002139 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002140
Jason Wang54f968d2012-10-31 19:45:57 +00002141 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2142 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002143 if (!tfile)
2144 return -ENOMEM;
Jason Wang6e914fc2012-10-31 19:45:58 +00002145 rcu_assign_pointer(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002146 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00002147 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002148 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002149
2150 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
2151 init_waitqueue_head(&tfile->wq.wait);
2152
2153 tfile->socket.file = file;
2154 tfile->socket.ops = &tun_socket_ops;
2155
2156 sock_init_data(&tfile->socket, &tfile->sk);
2157 sk_change_net(&tfile->sk, tfile->net);
2158
2159 tfile->sk.sk_write_space = tun_sock_write_space;
2160 tfile->sk.sk_sndbuf = INT_MAX;
2161
Eric W. Biederman631ab462009-01-20 11:00:40 +00002162 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00002163 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
Jason Wang4008e972012-12-13 23:53:30 +00002164 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002165
Jason Wang19a6afb2013-06-08 14:17:41 +08002166 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return 0;
2169}
2170
2171static int tun_chr_close(struct inode *inode, struct file *file)
2172{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002173 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00002174 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Jason Wangc8d68e62012-10-31 19:46:00 +00002176 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00002177 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 return 0;
2180}
2181
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002182static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002183 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002185 .read = do_sync_read,
2186 .aio_read = tun_chr_aio_read,
2187 .write = do_sync_write,
2188 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002190 .unlocked_ioctl = tun_chr_ioctl,
2191#ifdef CONFIG_COMPAT
2192 .compat_ioctl = tun_chr_compat_ioctl,
2193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 .open = tun_chr_open,
2195 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002196 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197};
2198
2199static struct miscdevice tun_miscdev = {
2200 .minor = TUN_MINOR,
2201 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002202 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204};
2205
2206/* ethtool interface */
2207
2208static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2209{
2210 cmd->supported = 0;
2211 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00002212 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 cmd->duplex = DUPLEX_FULL;
2214 cmd->port = PORT_TP;
2215 cmd->phy_address = 0;
2216 cmd->transceiver = XCVR_INTERNAL;
2217 cmd->autoneg = AUTONEG_DISABLE;
2218 cmd->maxtxpkt = 0;
2219 cmd->maxrxpkt = 0;
2220 return 0;
2221}
2222
2223static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2224{
2225 struct tun_struct *tun = netdev_priv(dev);
2226
Rick Jones33a5ba12011-11-15 14:59:53 +00002227 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2228 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 switch (tun->flags & TUN_TYPE_MASK) {
2231 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002232 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 break;
2234 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002235 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 break;
2237 }
2238}
2239
2240static u32 tun_get_msglevel(struct net_device *dev)
2241{
2242#ifdef TUN_DEBUG
2243 struct tun_struct *tun = netdev_priv(dev);
2244 return tun->debug;
2245#else
2246 return -EOPNOTSUPP;
2247#endif
2248}
2249
2250static void tun_set_msglevel(struct net_device *dev, u32 value)
2251{
2252#ifdef TUN_DEBUG
2253 struct tun_struct *tun = netdev_priv(dev);
2254 tun->debug = value;
2255#endif
2256}
2257
Jeff Garzik7282d492006-09-13 14:30:00 -04002258static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .get_settings = tun_get_settings,
2260 .get_drvinfo = tun_get_drvinfo,
2261 .get_msglevel = tun_get_msglevel,
2262 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002263 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002264 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265};
2266
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268static int __init tun_init(void)
2269{
2270 int ret = 0;
2271
Joe Perches6b8a66e2011-03-02 07:18:10 +00002272 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2273 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002275 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002276 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002277 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002278 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002279 }
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002282 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002283 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002284 goto err_misc;
2285 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002286 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002287err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002288 rtnl_link_unregister(&tun_link_ops);
2289err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return ret;
2291}
2292
2293static void tun_cleanup(void)
2294{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002295 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002296 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002299/* Get an underlying socket object from tun file. Returns error unless file is
2300 * attached to a device. The returned object works like a packet socket, it
2301 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2302 * holding a reference to the file for as long as the socket is in use. */
2303struct socket *tun_get_socket(struct file *file)
2304{
Jason Wang6e914fc2012-10-31 19:45:58 +00002305 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002306 if (file->f_op != &tun_fops)
2307 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002308 tfile = file->private_data;
2309 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002310 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002311 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002312}
2313EXPORT_SYMBOL_GPL(tun_get_socket);
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315module_init(tun_init);
2316module_exit(tun_cleanup);
2317MODULE_DESCRIPTION(DRV_DESCRIPTION);
2318MODULE_AUTHOR(DRV_COPYRIGHT);
2319MODULE_LICENSE("GPL");
2320MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002321MODULE_ALIAS("devname:net/tun");