blob: db43a2409733f05f8703a599efbd2a79cdcb94d7 [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 Wangc8d68e62012-10-31 19:46:00 +0000412static void __tun_detach(struct tun_file *tfile, bool clean)
413{
414 struct tun_file *ntfile;
415 struct tun_struct *tun;
Jason Wangc8d68e62012-10-31 19:46:00 +0000416
Jason Wangb8deabd2013-01-11 16:59:32 +0000417 tun = rtnl_dereference(tfile->tun);
418
Jason Wang9e857222013-01-28 01:05:19 +0000419 if (tun && !tfile->detached) {
Jason Wangc8d68e62012-10-31 19:46:00 +0000420 u16 index = tfile->queue_index;
421 BUG_ON(index >= tun->numqueues);
Jason Wangc8d68e62012-10-31 19:46:00 +0000422
423 rcu_assign_pointer(tun->tfiles[index],
424 tun->tfiles[tun->numqueues - 1]);
Jason Wangb8deabd2013-01-11 16:59:32 +0000425 ntfile = rtnl_dereference(tun->tfiles[index]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000426 ntfile->queue_index = index;
427
428 --tun->numqueues;
Jason Wang9e857222013-01-28 01:05:19 +0000429 if (clean) {
430 rcu_assign_pointer(tfile->tun, NULL);
Jason Wang4008e972012-12-13 23:53:30 +0000431 sock_put(&tfile->sk);
Jason Wang9e857222013-01-28 01:05:19 +0000432 } else
Jason Wang4008e972012-12-13 23:53:30 +0000433 tun_disable_queue(tun, tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000434
435 synchronize_net();
Jason Wang96442e422012-10-31 19:46:02 +0000436 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
Jason Wangc8d68e62012-10-31 19:46:00 +0000437 /* Drop read queue */
438 skb_queue_purge(&tfile->sk.sk_receive_queue);
439 tun_set_real_num_queues(tun);
Jason Wangdd38bd82013-01-11 16:59:34 +0000440 } else if (tfile->detached && clean) {
Jason Wang4008e972012-12-13 23:53:30 +0000441 tun = tun_enable_queue(tfile);
Jason Wangdd38bd82013-01-11 16:59:34 +0000442 sock_put(&tfile->sk);
443 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000444
445 if (clean) {
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000446 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
447 netif_carrier_off(tun->dev);
448
449 if (!(tun->flags & TUN_PERSIST) &&
450 tun->dev->reg_state == NETREG_REGISTERED)
Jason Wang4008e972012-12-13 23:53:30 +0000451 unregister_netdevice(tun->dev);
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +0000452 }
Jason Wang4008e972012-12-13 23:53:30 +0000453
Jason Wangc8d68e62012-10-31 19:46:00 +0000454 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
455 &tfile->socket.flags));
456 sk_release_kernel(&tfile->sk);
457 }
458}
459
460static void tun_detach(struct tun_file *tfile, bool clean)
461{
462 rtnl_lock();
463 __tun_detach(tfile, clean);
464 rtnl_unlock();
465}
466
467static void tun_detach_all(struct net_device *dev)
468{
469 struct tun_struct *tun = netdev_priv(dev);
Jason Wang4008e972012-12-13 23:53:30 +0000470 struct tun_file *tfile, *tmp;
Jason Wangc8d68e62012-10-31 19:46:00 +0000471 int i, n = tun->numqueues;
472
473 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000474 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000475 BUG_ON(!tfile);
476 wake_up_all(&tfile->wq.wait);
477 rcu_assign_pointer(tfile->tun, NULL);
478 --tun->numqueues;
479 }
Jason Wang9e857222013-01-28 01:05:19 +0000480 list_for_each_entry(tfile, &tun->disabled, next) {
481 wake_up_all(&tfile->wq.wait);
482 rcu_assign_pointer(tfile->tun, NULL);
483 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000484 BUG_ON(tun->numqueues != 0);
485
486 synchronize_net();
487 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +0000488 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +0000489 /* Drop read queue */
490 skb_queue_purge(&tfile->sk.sk_receive_queue);
491 sock_put(&tfile->sk);
492 }
Jason Wang4008e972012-12-13 23:53:30 +0000493 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
494 tun_enable_queue(tfile);
495 skb_queue_purge(&tfile->sk.sk_receive_queue);
496 sock_put(&tfile->sk);
497 }
498 BUG_ON(tun->numdisabled != 0);
Jason Wangdd38bd82013-01-11 16:59:34 +0000499
500 if (tun->flags & TUN_PERSIST)
501 module_put(THIS_MODULE);
Jason Wangc8d68e62012-10-31 19:46:00 +0000502}
503
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000504static int tun_attach(struct tun_struct *tun, struct file *file)
505{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000506 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000507 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000508
Paul Moore5dbbaf22013-01-14 07:12:19 +0000509 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
510 if (err < 0)
511 goto out;
512
Eric W. Biederman38231b72009-01-20 11:02:28 +0000513 err = -EINVAL;
Jason Wang9e857222013-01-28 01:05:19 +0000514 if (rtnl_dereference(tfile->tun) && !tfile->detached)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000515 goto out;
516
517 err = -EBUSY;
Jason Wangc8d68e62012-10-31 19:46:00 +0000518 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
519 goto out;
520
521 err = -E2BIG;
Jason Wang4008e972012-12-13 23:53:30 +0000522 if (!tfile->detached &&
523 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000524 goto out;
525
526 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000527
Jason Wangc8d68e62012-10-31 19:46:00 +0000528 /* Re-attach the filter to presist device */
Jason Wang54f968d2012-10-31 19:45:57 +0000529 if (tun->filter_attached == true) {
530 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
531 if (!err)
532 goto out;
533 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000534 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000535 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000536 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wangc8d68e62012-10-31 19:46:00 +0000537 tun->numqueues++;
538
Jason Wang4008e972012-12-13 23:53:30 +0000539 if (tfile->detached)
540 tun_enable_queue(tfile);
541 else
542 sock_hold(&tfile->sk);
543
Jason Wangc8d68e62012-10-31 19:46:00 +0000544 tun_set_real_num_queues(tun);
545
Jason Wangc8d68e62012-10-31 19:46:00 +0000546 /* device is allowed to go away first, so no need to hold extra
547 * refcnt.
548 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000549
Eric W. Biederman38231b72009-01-20 11:02:28 +0000550out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000551 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000552}
553
Eric W. Biederman631ab462009-01-20 11:00:40 +0000554static struct tun_struct *__tun_get(struct tun_file *tfile)
555{
Jason Wang6e914fc2012-10-31 19:45:58 +0000556 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000557
Jason Wang6e914fc2012-10-31 19:45:58 +0000558 rcu_read_lock();
559 tun = rcu_dereference(tfile->tun);
560 if (tun)
561 dev_hold(tun->dev);
562 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000563
564 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000565}
566
567static struct tun_struct *tun_get(struct file *file)
568{
569 return __tun_get(file->private_data);
570}
571
572static void tun_put(struct tun_struct *tun)
573{
Jason Wang6e914fc2012-10-31 19:45:58 +0000574 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000575}
576
Joe Perches6b8a66e2011-03-02 07:18:10 +0000577/* TAP filtering */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700578static void addr_hash_set(u32 *mask, const u8 *addr)
579{
580 int n = ether_crc(ETH_ALEN, addr) >> 26;
581 mask[n >> 5] |= (1 << (n & 31));
582}
583
584static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
585{
586 int n = ether_crc(ETH_ALEN, addr) >> 26;
587 return mask[n >> 5] & (1 << (n & 31));
588}
589
590static int update_filter(struct tap_filter *filter, void __user *arg)
591{
592 struct { u8 u[ETH_ALEN]; } *addr;
593 struct tun_filter uf;
594 int err, alen, n, nexact;
595
596 if (copy_from_user(&uf, arg, sizeof(uf)))
597 return -EFAULT;
598
599 if (!uf.count) {
600 /* Disabled */
601 filter->count = 0;
602 return 0;
603 }
604
605 alen = ETH_ALEN * uf.count;
606 addr = kmalloc(alen, GFP_KERNEL);
607 if (!addr)
608 return -ENOMEM;
609
610 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
611 err = -EFAULT;
612 goto done;
613 }
614
615 /* The filter is updated without holding any locks. Which is
616 * perfectly safe. We disable it first and in the worst
617 * case we'll accept a few undesired packets. */
618 filter->count = 0;
619 wmb();
620
621 /* Use first set of addresses as an exact filter */
622 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
623 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
624
625 nexact = n;
626
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800627 /* Remaining multicast addresses are hashed,
628 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700629 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800630 for (; n < uf.count; n++) {
631 if (!is_multicast_ether_addr(addr[n].u)) {
632 err = 0; /* no filter */
633 goto done;
634 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700635 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800636 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700637
638 /* For ALLMULTI just set the mask to all ones.
639 * This overrides the mask populated above. */
640 if ((uf.flags & TUN_FLT_ALLMULTI))
641 memset(filter->mask, ~0, sizeof(filter->mask));
642
643 /* Now enable the filter */
644 wmb();
645 filter->count = nexact;
646
647 /* Return the number of exact filters */
648 err = nexact;
649
650done:
651 kfree(addr);
652 return err;
653}
654
655/* Returns: 0 - drop, !=0 - accept */
656static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
657{
658 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
659 * at this point. */
660 struct ethhdr *eh = (struct ethhdr *) skb->data;
661 int i;
662
663 /* Exact match */
664 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000665 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700666 return 1;
667
668 /* Inexact match (multicast only) */
669 if (is_multicast_ether_addr(eh->h_dest))
670 return addr_hash_test(filter->mask, eh->h_dest);
671
672 return 0;
673}
674
675/*
676 * Checks whether the packet is accepted or not.
677 * Returns: 0 - drop, !=0 - accept
678 */
679static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
680{
681 if (!filter->count)
682 return 1;
683
684 return run_filter(filter, skb);
685}
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/* Network device part of the driver */
688
Jeff Garzik7282d492006-09-13 14:30:00 -0400689static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000691/* Net device detach from fd. */
692static void tun_net_uninit(struct net_device *dev)
693{
Jason Wangc8d68e62012-10-31 19:46:00 +0000694 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000695}
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/* Net device open. */
698static int tun_net_open(struct net_device *dev)
699{
Jason Wangc8d68e62012-10-31 19:46:00 +0000700 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702}
703
704/* Net device close. */
705static int tun_net_close(struct net_device *dev)
706{
Jason Wangc8d68e62012-10-31 19:46:00 +0000707 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return 0;
709}
710
711/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000712static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000715 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000716 struct tun_file *tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jason Wang6e914fc2012-10-31 19:45:58 +0000718 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000719 tfile = rcu_dereference(tun->tfiles[txq]);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* Drop packet if interface is not attached */
Jason Wangc8d68e62012-10-31 19:46:00 +0000722 if (txq >= tun->numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto drop;
724
Jason Wang6e914fc2012-10-31 19:45:58 +0000725 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
726
Jason Wangc8d68e62012-10-31 19:46:00 +0000727 BUG_ON(!tfile);
728
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700729 /* Drop if the filter does not like it.
730 * This is a noop if the filter is disabled.
731 * Filter can be enabled only for the TAP devices. */
732 if (!check_filter(&tun->txflt, skb))
733 goto drop;
734
Jason Wang54f968d2012-10-31 19:45:57 +0000735 if (tfile->socket.sk->sk_filter &&
736 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000737 goto drop;
738
Rami Rosen36fe8c092012-11-25 22:07:40 +0000739 /* Limit the number of packets queued by dividing txq length with the
Jason Wangc8d68e62012-10-31 19:46:00 +0000740 * number of queues.
741 */
Jason Wang54f968d2012-10-31 19:45:57 +0000742 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
Michael S. Tsirkin5d097102012-12-03 10:07:14 +0000743 >= dev->tx_queue_len / tun->numqueues)
744 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Richard Cochraneda29772013-07-19 19:40:10 +0200746 if (skb->sk) {
747 sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
748 sw_tx_timestamp(skb);
749 }
750
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000751 /* Orphan the skb - required as we might hang on to it
752 * for indefinite time. */
Michael S. Tsirkin868eefe2012-07-20 09:23:14 +0000753 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
754 goto drop;
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000755 skb_orphan(skb);
756
Eric Dumazetf8af75f2013-03-06 11:02:37 +0000757 nf_reset(skb);
758
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700759 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000760 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000763 if (tfile->flags & TUN_FASYNC)
764 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
765 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000766 POLLRDNORM | POLLRDBAND);
Jason Wang6e914fc2012-10-31 19:45:58 +0000767
768 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000769 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700772 dev->stats.tx_dropped++;
Michael S. Tsirkin149d36f2012-11-01 09:16:32 +0000773 skb_tx_error(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000775 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000776 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700779static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700781 /*
782 * This callback is supposed to deal with mc filter in
783 * _rx_ path and has nothing to do with the _tx_ path.
784 * In rx path we always accept everything userspace gives us.
785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Ed Swierk4885a502007-09-16 12:21:38 -0700788#define MIN_MTU 68
789#define MAX_MTU 65535
790
791static int
792tun_net_change_mtu(struct net_device *dev, int new_mtu)
793{
794 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
795 return -EINVAL;
796 dev->mtu = new_mtu;
797 return 0;
798}
799
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000800static netdev_features_t tun_net_fix_features(struct net_device *dev,
801 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000802{
803 struct tun_struct *tun = netdev_priv(dev);
804
805 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
806}
Neil Hormanbebd0972011-06-15 05:25:01 +0000807#ifdef CONFIG_NET_POLL_CONTROLLER
808static void tun_poll_controller(struct net_device *dev)
809{
810 /*
811 * Tun only receives frames when:
812 * 1) the char device endpoint gets data from user space
813 * 2) the tun socket gets a sendmsg call from user space
814 * Since both of those are syncronous operations, we are guaranteed
815 * never to have pending data when we poll for it
816 * so theres nothing to do here but return.
817 * We need this though so netpoll recognizes us as an interface that
818 * supports polling, which enables bridge devices in virt setups to
819 * still use netconsole
820 */
821 return;
822}
823#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800824static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000825 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800826 .ndo_open = tun_net_open,
827 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800828 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800829 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000830 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000831 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000832#ifdef CONFIG_NET_POLL_CONTROLLER
833 .ndo_poll_controller = tun_poll_controller,
834#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800835};
836
837static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000838 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800839 .ndo_open = tun_net_open,
840 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800841 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800842 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000843 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000844 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800845 .ndo_set_mac_address = eth_mac_addr,
846 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000847 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000848#ifdef CONFIG_NET_POLL_CONTROLLER
849 .ndo_poll_controller = tun_poll_controller,
850#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800851};
852
Pavel Emelyanov944a1372013-06-11 17:01:08 +0400853static void tun_flow_init(struct tun_struct *tun)
Jason Wang96442e422012-10-31 19:46:02 +0000854{
855 int i;
856
Jason Wang96442e422012-10-31 19:46:02 +0000857 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
858 INIT_HLIST_HEAD(&tun->flows[i]);
859
860 tun->ageing_time = TUN_FLOW_EXPIRE;
861 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
862 mod_timer(&tun->flow_gc_timer,
863 round_jiffies_up(jiffies + tun->ageing_time));
Jason Wang96442e422012-10-31 19:46:02 +0000864}
865
866static void tun_flow_uninit(struct tun_struct *tun)
867{
868 del_timer_sync(&tun->flow_gc_timer);
869 tun_flow_flush(tun);
Jason Wang96442e422012-10-31 19:46:02 +0000870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/* Initialize net device. */
873static void tun_net_init(struct net_device *dev)
874{
875 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 switch (tun->flags & TUN_TYPE_MASK) {
878 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800879 dev->netdev_ops = &tun_netdev_ops;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Point-to-Point TUN Device */
882 dev->hard_header_len = 0;
883 dev->addr_len = 0;
884 dev->mtu = 1500;
885
886 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400887 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
889 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
890 break;
891
892 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800893 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* Ethernet TAP Device */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700895 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000896 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
stephen hemmingera6768472012-12-10 15:16:00 +0000897 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000899 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
902 break;
903 }
904}
905
906/* Character device part */
907
908/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000909static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400910{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000911 struct tun_file *tfile = file->private_data;
912 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000913 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800914 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000917 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Jason Wang54f968d2012-10-31 19:45:57 +0000919 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000920
Joe Perches6b8a66e2011-03-02 07:18:10 +0000921 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Jason Wang54f968d2012-10-31 19:45:57 +0000923 poll_wait(file, &tfile->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400924
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000925 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 mask |= POLLIN | POLLRDNORM;
927
Herbert Xu33dccbb2009-02-05 21:25:32 -0800928 if (sock_writeable(sk) ||
929 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
930 sock_writeable(sk)))
931 mask |= POLLOUT | POLLWRNORM;
932
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000933 if (tun->dev->reg_state != NETREG_REGISTERED)
934 mask = POLLERR;
935
Eric W. Biederman631ab462009-01-20 11:00:40 +0000936 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return mask;
938}
939
Rusty Russellf42157c2008-08-15 15:15:10 -0700940/* prepad is the amount to reserve at front. len is length after that.
941 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000942static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000943 size_t prepad, size_t len,
944 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700945{
Jason Wang54f968d2012-10-31 19:45:57 +0000946 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700947 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800948 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700949
950 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700951 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800952 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700953
Herbert Xu33dccbb2009-02-05 21:25:32 -0800954 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -0700955 &err, 0);
Rusty Russellf42157c2008-08-15 15:15:10 -0700956 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800957 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700958
959 skb_reserve(skb, prepad);
960 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800961 skb->data_len = len - linear;
962 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700963
964 return skb;
965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +0000968static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
969 void *msg_control, const struct iovec *iv,
970 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Harvey Harrison09640e62009-02-01 00:45:17 -0800972 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct sk_buff *skb;
Jason Wang3dd5c332013-07-10 13:43:27 +0800974 size_t len = total_len, align = NET_SKB_PAD, linear;
Rusty Russellf43798c2008-07-03 03:48:02 -0700975 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000976 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000977 int copylen;
978 bool zerocopy = false;
979 int err;
Eric Dumazet49974422012-12-12 19:22:57 +0000980 u32 rxhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (!(tun->flags & TUN_NO_PI)) {
Dan Carpenter15718ea2013-08-15 15:52:57 +0300983 if (len < sizeof(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +0300985 len -= sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000987 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000989 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
Rusty Russellf43798c2008-07-03 03:48:02 -0700992 if (tun->flags & TUN_VNET_HDR) {
Dan Carpenter15718ea2013-08-15 15:52:57 +0300993 if (len < tun->vnet_hdr_sz)
Rusty Russellf43798c2008-07-03 03:48:02 -0700994 return -EINVAL;
Dan Carpenter15718ea2013-08-15 15:52:57 +0300995 len -= tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700996
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000997 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -0700998 return -EFAULT;
999
Herbert Xu49091222009-06-08 00:20:01 -07001000 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1001 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
1002 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
1003
Rusty Russellf43798c2008-07-03 03:48:02 -07001004 if (gso.hdr_len > len)
1005 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001006 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001007 }
1008
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001009 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +00001010 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -07001011 if (unlikely(len < ETH_HLEN ||
1012 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -07001013 return -EINVAL;
1014 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001015
Jason Wang88529172013-07-18 10:55:15 +08001016 if (msg_control) {
1017 /* There are 256 bytes to be copied in skb, so there is
1018 * enough room for skb expand head in case it is used.
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001019 * The rest of the buffer is mapped from userspace.
1020 */
Jason Wang88529172013-07-18 10:55:15 +08001021 copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN;
Jason Wang3dd5c332013-07-10 13:43:27 +08001022 linear = copylen;
Jason Wang88529172013-07-18 10:55:15 +08001023 if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
1024 zerocopy = true;
1025 }
1026
1027 if (!zerocopy) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001028 copylen = len;
Jason Wang3dd5c332013-07-10 13:43:27 +08001029 linear = gso.hdr_len;
1030 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001031
Jason Wang3dd5c332013-07-10 13:43:27 +08001032 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001033 if (IS_ERR(skb)) {
1034 if (PTR_ERR(skb) != -EAGAIN)
1035 tun->dev->stats.rx_dropped++;
1036 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001039 if (zerocopy)
1040 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
Jason Wang88529172013-07-18 10:55:15 +08001041 else {
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001042 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
Jason Wang88529172013-07-18 10:55:15 +08001043 if (!err && msg_control) {
1044 struct ubuf_info *uarg = msg_control;
1045 uarg->callback(uarg, false);
1046 }
1047 }
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001048
1049 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001050 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -08001051 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -08001053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Rusty Russellf43798c2008-07-03 03:48:02 -07001055 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1056 if (!skb_partial_csum_set(skb, gso.csum_start,
1057 gso.csum_offset)) {
1058 tun->dev->stats.rx_frame_errors++;
1059 kfree_skb(skb);
1060 return -EINVAL;
1061 }
Michał Mirosław88255372011-04-19 06:13:10 +00001062 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 switch (tun->flags & TUN_TYPE_MASK) {
1065 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -07001066 if (tun->flags & TUN_NO_PI) {
1067 switch (skb->data[0] & 0xf0) {
1068 case 0x40:
1069 pi.proto = htons(ETH_P_IP);
1070 break;
1071 case 0x60:
1072 pi.proto = htons(ETH_P_IPV6);
1073 break;
1074 default:
1075 tun->dev->stats.rx_dropped++;
1076 kfree_skb(skb);
1077 return -EINVAL;
1078 }
1079 }
1080
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001081 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -07001083 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
1085 case TUN_TAP_DEV:
1086 skb->protocol = eth_type_trans(skb, tun->dev);
1087 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Rusty Russellf43798c2008-07-03 03:48:02 -07001090 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1091 pr_debug("GSO!\n");
1092 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1093 case VIRTIO_NET_HDR_GSO_TCPV4:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001094 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Rusty Russellf43798c2008-07-03 03:48:02 -07001095 break;
1096 case VIRTIO_NET_HDR_GSO_TCPV6:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001097 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Rusty Russellf43798c2008-07-03 03:48:02 -07001098 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001099 case VIRTIO_NET_HDR_GSO_UDP:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001100 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001101 break;
Rusty Russellf43798c2008-07-03 03:48:02 -07001102 default:
1103 tun->dev->stats.rx_frame_errors++;
1104 kfree_skb(skb);
1105 return -EINVAL;
1106 }
1107
1108 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001109 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Rusty Russellf43798c2008-07-03 03:48:02 -07001110
1111 skb_shinfo(skb)->gso_size = gso.gso_size;
1112 if (skb_shinfo(skb)->gso_size == 0) {
1113 tun->dev->stats.rx_frame_errors++;
1114 kfree_skb(skb);
1115 return -EINVAL;
1116 }
1117
1118 /* Header must be checked, and gso_segs computed. */
1119 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1120 skb_shinfo(skb)->gso_segs = 0;
1121 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001122
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001123 /* copy skb_ubuf_info for callback when skb has no error */
1124 if (zerocopy) {
1125 skb_shinfo(skb)->destructor_arg = msg_control;
1126 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00001127 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001128 }
1129
Eric Dumazet76fe4582012-12-17 04:39:20 +00001130 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001131 skb_probe_transport_header(skb, 0);
Jason Wang38502af2013-03-25 20:19:56 +00001132
Eric Dumazet49974422012-12-12 19:22:57 +00001133 rxhash = skb_get_rxhash(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001135
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001136 tun->dev->stats.rx_packets++;
1137 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jason Wang9e857222013-01-28 01:05:19 +00001139 tun_flow_update(tun, rxhash, tfile);
Michael S. Tsirkin06908992012-07-20 09:23:23 +00001140 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001141}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001143static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1144 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Herbert Xu33dccbb2009-02-05 21:25:32 -08001146 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -08001147 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +00001148 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001149 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 if (!tun)
1152 return -EBADFD;
1153
Joe Perches6b8a66e2011-03-02 07:18:10 +00001154 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Jason Wang54f968d2012-10-31 19:45:57 +00001156 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1157 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001158
1159 tun_put(tun);
1160 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +00001164static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +00001165 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +00001166 struct sk_buff *skb,
1167 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 struct tun_pi pi = { 0, skb->protocol };
1170 ssize_t total = 0;
Jason Wang6680ec62013-07-25 13:00:33 +08001171 int vlan_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if (!(tun->flags & TUN_NO_PI)) {
1174 if ((len -= sizeof(pi)) < 0)
1175 return -EINVAL;
1176
1177 if (len < skb->len) {
1178 /* Packet will be striped */
1179 pi.flags |= TUN_PKT_STRIP;
1180 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001181
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001182 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return -EFAULT;
1184 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Rusty Russellf43798c2008-07-03 03:48:02 -07001187 if (tun->flags & TUN_VNET_HDR) {
1188 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001189 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001190 return -EINVAL;
1191
1192 if (skb_is_gso(skb)) {
1193 struct skb_shared_info *sinfo = skb_shinfo(skb);
1194
1195 /* This is a hint as to how much should be linear. */
1196 gso.hdr_len = skb_headlen(skb);
1197 gso.gso_size = sinfo->gso_size;
1198 if (sinfo->gso_type & SKB_GSO_TCPV4)
1199 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1200 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1201 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001202 else if (sinfo->gso_type & SKB_GSO_UDP)
1203 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001204 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001205 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001206 "0x%x, gso_size %d, hdr_len %d\n",
1207 sinfo->gso_type, gso.gso_size,
1208 gso.hdr_len);
1209 print_hex_dump(KERN_ERR, "tun: ",
1210 DUMP_PREFIX_NONE,
1211 16, 1, skb->head,
1212 min((int)gso.hdr_len, 64), true);
1213 WARN_ON_ONCE(1);
1214 return -EINVAL;
1215 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001216 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1217 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1218 } else
1219 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1220
1221 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1222 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001223 gso.csum_start = skb_checksum_start_offset(skb);
Rusty Russellf43798c2008-07-03 03:48:02 -07001224 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001225 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1226 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001227 } /* else everything is zero */
1228
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001229 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1230 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001231 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001232 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001233 }
1234
Jason Wang6680ec62013-07-25 13:00:33 +08001235 if (!vlan_tx_tag_present(skb)) {
1236 len = min_t(int, skb->len, len);
1237 } else {
1238 int copy, ret;
1239 struct {
1240 __be16 h_vlan_proto;
1241 __be16 h_vlan_TCI;
1242 } veth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jason Wang6680ec62013-07-25 13:00:33 +08001244 veth.h_vlan_proto = skb->vlan_proto;
1245 veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Jason Wang6680ec62013-07-25 13:00:33 +08001247 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
1248 len = min_t(int, skb->len + VLAN_HLEN, len);
1249
1250 copy = min_t(int, vlan_offset, len);
1251 ret = skb_copy_datagram_const_iovec(skb, 0, iv, total, copy);
1252 len -= copy;
1253 total += copy;
1254 if (ret || !len)
1255 goto done;
1256
1257 copy = min_t(int, sizeof(veth), len);
1258 ret = memcpy_toiovecend(iv, (void *)&veth, total, copy);
1259 len -= copy;
1260 total += copy;
1261 if (ret || !len)
1262 goto done;
1263 }
1264
1265 skb_copy_datagram_const_iovec(skb, vlan_offset, iv, total, len);
1266 total += len;
1267
1268done:
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001269 tun->dev->stats.tx_packets++;
1270 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 return total;
1273}
1274
Jason Wang54f968d2012-10-31 19:45:57 +00001275static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001276 struct kiocb *iocb, const struct iovec *iv,
1277 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 DECLARE_WAITQUEUE(wait, current);
1280 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001281 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Rami Rosen3872baf2012-11-25 22:07:41 +00001283 tun_debug(KERN_INFO, tun, "tun_do_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Amos Kong61a5ff12011-06-09 00:27:10 -07001285 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001286 add_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 current->state = TASK_INTERRUPTIBLE;
1289
1290 /* Read frames from the queue */
Jason Wang54f968d2012-10-31 19:45:57 +00001291 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001292 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ret = -EAGAIN;
1294 break;
1295 }
1296 if (signal_pending(current)) {
1297 ret = -ERESTARTSYS;
1298 break;
1299 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001300 if (tun->dev->reg_state != NETREG_REGISTERED) {
1301 ret = -EIO;
1302 break;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /* Nothing to read, let's sleep */
1306 schedule();
1307 continue;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Jason Wang54f968d2012-10-31 19:45:57 +00001310 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001311 kfree_skb(skb);
1312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
1315 current->state = TASK_RUNNING;
Amos Kong61a5ff12011-06-09 00:27:10 -07001316 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001317 remove_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001319 return ret;
1320}
1321
1322static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1323 unsigned long count, loff_t pos)
1324{
1325 struct file *file = iocb->ki_filp;
1326 struct tun_file *tfile = file->private_data;
1327 struct tun_struct *tun = __tun_get(tfile);
1328 ssize_t len, ret;
1329
1330 if (!tun)
1331 return -EBADFD;
1332 len = iov_length(iv, count);
1333 if (len < 0) {
1334 ret = -EINVAL;
1335 goto out;
1336 }
1337
Jason Wang54f968d2012-10-31 19:45:57 +00001338 ret = tun_do_read(tun, tfile, iocb, iv, len,
1339 file->f_flags & O_NONBLOCK);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001340 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001341out:
1342 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return ret;
1344}
1345
Jason Wang96442e422012-10-31 19:46:02 +00001346static void tun_free_netdev(struct net_device *dev)
1347{
1348 struct tun_struct *tun = netdev_priv(dev);
1349
Jason Wang4008e972012-12-13 23:53:30 +00001350 BUG_ON(!(list_empty(&tun->disabled)));
Jason Wang96442e422012-10-31 19:46:02 +00001351 tun_flow_uninit(tun);
Paul Moore5dbbaf22013-01-14 07:12:19 +00001352 security_tun_dev_free_security(tun->security);
Jason Wang96442e422012-10-31 19:46:02 +00001353 free_netdev(dev);
1354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356static void tun_setup(struct net_device *dev)
1357{
1358 struct tun_struct *tun = netdev_priv(dev);
1359
Eric W. Biederman0625c882012-02-07 16:48:55 -08001360 tun->owner = INVALID_UID;
1361 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang96442e422012-10-31 19:46:02 +00001364 dev->destructor = tun_free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001367/* Trivial set of netlink ops to allow deleting tun or tap
1368 * device with netlink.
1369 */
1370static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1371{
1372 return -EINVAL;
1373}
1374
1375static struct rtnl_link_ops tun_link_ops __read_mostly = {
1376 .kind = DRV_NAME,
1377 .priv_size = sizeof(struct tun_struct),
1378 .setup = tun_setup,
1379 .validate = tun_validate,
1380};
1381
Herbert Xu33dccbb2009-02-05 21:25:32 -08001382static void tun_sock_write_space(struct sock *sk)
1383{
Jason Wang54f968d2012-10-31 19:45:57 +00001384 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001385 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001386
1387 if (!sock_writeable(sk))
1388 return;
1389
Herbert Xu33dccbb2009-02-05 21:25:32 -08001390 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1391 return;
1392
Eric Dumazet43815482010-04-29 11:01:49 +00001393 wqueue = sk_sleep(sk);
1394 if (wqueue && waitqueue_active(wqueue))
1395 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001396 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001397
Jason Wang54f968d2012-10-31 19:45:57 +00001398 tfile = container_of(sk, struct tun_file, sk);
1399 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001400}
1401
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001402static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1403 struct msghdr *m, size_t total_len)
1404{
Jason Wang54f968d2012-10-31 19:45:57 +00001405 int ret;
1406 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1407 struct tun_struct *tun = __tun_get(tfile);
1408
1409 if (!tun)
1410 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001411 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1412 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1413 tun_put(tun);
1414 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001415}
1416
1417static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1418 struct msghdr *m, size_t total_len,
1419 int flags)
1420{
Jason Wang54f968d2012-10-31 19:45:57 +00001421 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1422 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001423 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001424
1425 if (!tun)
1426 return -EBADFD;
1427
Richard Cochraneda29772013-07-19 19:40:10 +02001428 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
Gao feng3811ae72013-04-24 21:59:23 +00001429 ret = -EINVAL;
1430 goto out;
1431 }
Richard Cochraneda29772013-07-19 19:40:10 +02001432 if (flags & MSG_ERRQUEUE) {
1433 ret = sock_recv_errqueue(sock->sk, m, total_len,
1434 SOL_PACKET, TUN_TX_TIMESTAMP);
1435 goto out;
1436 }
Jason Wang54f968d2012-10-31 19:45:57 +00001437 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001438 flags & MSG_DONTWAIT);
1439 if (ret > total_len) {
1440 m->msg_flags |= MSG_TRUNC;
1441 ret = flags & MSG_TRUNC ? ret : total_len;
1442 }
Gao feng3811ae72013-04-24 21:59:23 +00001443out:
Jason Wang54f968d2012-10-31 19:45:57 +00001444 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001445 return ret;
1446}
1447
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001448static int tun_release(struct socket *sock)
1449{
1450 if (sock->sk)
1451 sock_put(sock->sk);
1452 return 0;
1453}
1454
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001455/* Ops structure to mimic raw sockets with tun */
1456static const struct proto_ops tun_socket_ops = {
1457 .sendmsg = tun_sendmsg,
1458 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001459 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001460};
1461
Herbert Xu33dccbb2009-02-05 21:25:32 -08001462static struct proto tun_proto = {
1463 .name = "tun",
1464 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001465 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001466};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001467
David Woodhouse980c9e82009-05-09 22:54:21 -07001468static int tun_flags(struct tun_struct *tun)
1469{
1470 int flags = 0;
1471
1472 if (tun->flags & TUN_TUN_DEV)
1473 flags |= IFF_TUN;
1474 else
1475 flags |= IFF_TAP;
1476
1477 if (tun->flags & TUN_NO_PI)
1478 flags |= IFF_NO_PI;
1479
Michael S. Tsirkin5d097102012-12-03 10:07:14 +00001480 /* This flag has no real effect. We track the value for backwards
1481 * compatibility.
1482 */
David Woodhouse980c9e82009-05-09 22:54:21 -07001483 if (tun->flags & TUN_ONE_QUEUE)
1484 flags |= IFF_ONE_QUEUE;
1485
1486 if (tun->flags & TUN_VNET_HDR)
1487 flags |= IFF_VNET_HDR;
1488
Jason Wangc8d68e62012-10-31 19:46:00 +00001489 if (tun->flags & TUN_TAP_MQ)
1490 flags |= IFF_MULTI_QUEUE;
1491
Pavel Emelyanov274038f2013-06-11 14:41:24 +04001492 if (tun->flags & TUN_PERSIST)
1493 flags |= IFF_PERSIST;
1494
David Woodhouse980c9e82009-05-09 22:54:21 -07001495 return flags;
1496}
1497
1498static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1499 char *buf)
1500{
1501 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1502 return sprintf(buf, "0x%x\n", tun_flags(tun));
1503}
1504
1505static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1506 char *buf)
1507{
1508 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001509 return uid_valid(tun->owner)?
1510 sprintf(buf, "%u\n",
1511 from_kuid_munged(current_user_ns(), tun->owner)):
1512 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001513}
1514
1515static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1516 char *buf)
1517{
1518 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001519 return gid_valid(tun->group) ?
1520 sprintf(buf, "%u\n",
1521 from_kgid_munged(current_user_ns(), tun->group)):
1522 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001523}
1524
1525static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1526static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1527static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1528
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001529static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001532 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct net_device *dev;
1534 int err;
1535
Jason Wang7c0c3b12013-01-11 16:59:33 +00001536 if (tfile->detached)
1537 return -EINVAL;
1538
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001539 dev = __dev_get_by_name(net, ifr->ifr_name);
1540 if (dev) {
David Woodhousef85ba782009-04-27 03:23:54 -07001541 if (ifr->ifr_flags & IFF_TUN_EXCL)
1542 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001543 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1544 tun = netdev_priv(dev);
1545 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1546 tun = netdev_priv(dev);
1547 else
1548 return -EINVAL;
1549
Jason Wang8e6d91a2013-05-28 18:32:11 +00001550 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
1551 !!(tun->flags & TUN_TAP_MQ))
1552 return -EINVAL;
1553
Jason Wangcde8b152012-10-31 19:46:01 +00001554 if (tun_not_capable(tun))
Paul Moore2b980db2009-08-28 18:12:43 -04001555 return -EPERM;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001556 err = security_tun_dev_open(tun->security);
Paul Moore2b980db2009-08-28 18:12:43 -04001557 if (err < 0)
1558 return err;
1559
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001560 err = tun_attach(tun, file);
1561 if (err < 0)
1562 return err;
Jason Wang4008e972012-12-13 23:53:30 +00001563
1564 if (tun->flags & TUN_TAP_MQ &&
Jason Wange8dbad62013-04-22 20:40:39 +00001565 (tun->numqueues + tun->numdisabled > 1)) {
1566 /* One or more queue has already been attached, no need
1567 * to initialize the device again.
1568 */
1569 return 0;
1570 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 else {
1573 char *name;
1574 unsigned long flags = 0;
Jason Wangedfb6a12013-01-23 03:59:12 +00001575 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1576 MAX_TAP_QUEUES : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Eric W. Biedermanc260b772012-11-18 21:34:11 +00001578 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001579 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001580 err = security_tun_dev_create();
1581 if (err < 0)
1582 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /* Set dev type */
1585 if (ifr->ifr_flags & IFF_TUN) {
1586 /* TUN device */
1587 flags |= TUN_TUN_DEV;
1588 name = "tun%d";
1589 } else if (ifr->ifr_flags & IFF_TAP) {
1590 /* TAP device */
1591 flags |= TUN_TAP_DEV;
1592 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001593 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001594 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (*ifr->ifr_name)
1597 name = ifr->ifr_name;
1598
Jason Wangc8d68e62012-10-31 19:46:00 +00001599 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
Jason Wangedfb6a12013-01-23 03:59:12 +00001600 tun_setup, queues, queues);
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (!dev)
1603 return -ENOMEM;
1604
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001605 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001606 dev->rtnl_link_ops = &tun_link_ops;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001607 dev->ifindex = tfile->ifindex;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 tun = netdev_priv(dev);
1610 tun->dev = dev;
1611 tun->flags = flags;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001612 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001613 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Jason Wang54f968d2012-10-31 19:45:57 +00001615 tun->filter_attached = false;
1616 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001617
Jason Wang96442e422012-10-31 19:46:02 +00001618 spin_lock_init(&tun->lock);
1619
Paul Moore5dbbaf22013-01-14 07:12:19 +00001620 err = security_tun_dev_alloc_security(&tun->security);
1621 if (err < 0)
1622 goto err_free_dev;
Paul Moore2b980db2009-08-28 18:12:43 -04001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 tun_net_init(dev);
Pavel Emelyanov944a1372013-06-11 17:01:08 +04001625 tun_flow_init(tun);
Jason Wang96442e422012-10-31 19:46:02 +00001626
Michał Mirosław88255372011-04-19 06:13:10 +00001627 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
Jason Wang6680ec62013-07-25 13:00:33 +08001628 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
1629 NETIF_F_HW_VLAN_STAG_TX;
Michał Mirosław88255372011-04-19 06:13:10 +00001630 dev->features = dev->hw_features;
Jason Wangc0317992013-04-10 23:32:22 +00001631 dev->vlan_features = dev->features;
Michał Mirosław88255372011-04-19 06:13:10 +00001632
Jason Wang4008e972012-12-13 23:53:30 +00001633 INIT_LIST_HEAD(&tun->disabled);
Jason Wangeb0fb362012-12-02 17:19:45 +00001634 err = tun_attach(tun, file);
1635 if (err < 0)
1636 goto err_free_dev;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 err = register_netdevice(tun->dev);
1639 if (err < 0)
Jason Wang54f968d2012-10-31 19:45:57 +00001640 goto err_free_dev;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001641
David Woodhouse980c9e82009-05-09 22:54:21 -07001642 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1643 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1644 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001645 pr_err("Failed to create tun sysfs files\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647
Michael S. Tsirkinaf668b32013-01-28 00:38:02 +00001648 netif_carrier_on(tun->dev);
1649
Joe Perches6b8a66e2011-03-02 07:18:10 +00001650 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (ifr->ifr_flags & IFF_NO_PI)
1653 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001654 else
1655 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Michael S. Tsirkin5d097102012-12-03 10:07:14 +00001657 /* This flag has no real effect. We track the value for backwards
1658 * compatibility.
1659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1661 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001662 else
1663 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Rusty Russellf43798c2008-07-03 03:48:02 -07001665 if (ifr->ifr_flags & IFF_VNET_HDR)
1666 tun->flags |= TUN_VNET_HDR;
1667 else
1668 tun->flags &= ~TUN_VNET_HDR;
1669
Jason Wangc8d68e62012-10-31 19:46:00 +00001670 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1671 tun->flags |= TUN_TAP_MQ;
1672 else
1673 tun->flags &= ~TUN_TAP_MQ;
1674
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001675 /* Make sure persistent devices do not get stuck in
1676 * xoff state.
1677 */
1678 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001679 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 strcpy(ifr->ifr_name, tun->dev->name);
1682 return 0;
1683
1684 err_free_dev:
1685 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return err;
1687}
1688
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001689static void tun_get_iff(struct net *net, struct tun_struct *tun,
Herbert Xu876bfd42009-08-06 14:22:44 +00001690 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001691{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001692 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001693
1694 strcpy(ifr->ifr_name, tun->dev->name);
1695
David Woodhouse980c9e82009-05-09 22:54:21 -07001696 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001697
Mark McLoughline3b99552008-08-15 15:09:56 -07001698}
1699
Rusty Russell5228ddc2008-07-03 03:46:16 -07001700/* This is like a cut-down ethtool ops, except done via tun fd so no
1701 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001702static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001703{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001704 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001705
1706 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001707 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001708 arg &= ~TUN_F_CSUM;
1709
1710 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1711 if (arg & TUN_F_TSO_ECN) {
1712 features |= NETIF_F_TSO_ECN;
1713 arg &= ~TUN_F_TSO_ECN;
1714 }
1715 if (arg & TUN_F_TSO4)
1716 features |= NETIF_F_TSO;
1717 if (arg & TUN_F_TSO6)
1718 features |= NETIF_F_TSO6;
1719 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1720 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001721
1722 if (arg & TUN_F_UFO) {
1723 features |= NETIF_F_UFO;
1724 arg &= ~TUN_F_UFO;
1725 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001726 }
1727
1728 /* This gives the user a way to test for new features in future by
1729 * trying to set them. */
1730 if (arg)
1731 return -EINVAL;
1732
Michał Mirosław88255372011-04-19 06:13:10 +00001733 tun->set_features = features;
1734 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001735
1736 return 0;
1737}
1738
Jason Wangc8d68e62012-10-31 19:46:00 +00001739static void tun_detach_filter(struct tun_struct *tun, int n)
1740{
1741 int i;
1742 struct tun_file *tfile;
1743
1744 for (i = 0; i < n; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001745 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001746 sk_detach_filter(tfile->socket.sk);
1747 }
1748
1749 tun->filter_attached = false;
1750}
1751
1752static int tun_attach_filter(struct tun_struct *tun)
1753{
1754 int i, ret = 0;
1755 struct tun_file *tfile;
1756
1757 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001758 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001759 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1760 if (ret) {
1761 tun_detach_filter(tun, i);
1762 return ret;
1763 }
1764 }
1765
1766 tun->filter_attached = true;
1767 return ret;
1768}
1769
1770static void tun_set_sndbuf(struct tun_struct *tun)
1771{
1772 struct tun_file *tfile;
1773 int i;
1774
1775 for (i = 0; i < tun->numqueues; i++) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001776 tfile = rtnl_dereference(tun->tfiles[i]);
Jason Wangc8d68e62012-10-31 19:46:00 +00001777 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1778 }
1779}
1780
Jason Wangcde8b152012-10-31 19:46:01 +00001781static int tun_set_queue(struct file *file, struct ifreq *ifr)
1782{
1783 struct tun_file *tfile = file->private_data;
1784 struct tun_struct *tun;
Jason Wangcde8b152012-10-31 19:46:01 +00001785 int ret = 0;
1786
1787 rtnl_lock();
1788
1789 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
Jason Wang4008e972012-12-13 23:53:30 +00001790 tun = tfile->detached;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001791 if (!tun) {
Jason Wangcde8b152012-10-31 19:46:01 +00001792 ret = -EINVAL;
Paul Moore5dbbaf22013-01-14 07:12:19 +00001793 goto unlock;
1794 }
1795 ret = security_tun_dev_attach_queue(tun->security);
1796 if (ret < 0)
1797 goto unlock;
1798 ret = tun_attach(tun, file);
Jason Wang4008e972012-12-13 23:53:30 +00001799 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
Jason Wangb8deabd2013-01-11 16:59:32 +00001800 tun = rtnl_dereference(tfile->tun);
Jason Wang9e857222013-01-28 01:05:19 +00001801 if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached)
Jason Wang4008e972012-12-13 23:53:30 +00001802 ret = -EINVAL;
1803 else
1804 __tun_detach(tfile, false);
1805 } else
Jason Wangcde8b152012-10-31 19:46:01 +00001806 ret = -EINVAL;
1807
Paul Moore5dbbaf22013-01-14 07:12:19 +00001808unlock:
Jason Wangcde8b152012-10-31 19:46:01 +00001809 rtnl_unlock();
1810 return ret;
1811}
1812
Arnd Bergmann50857e22009-11-06 22:52:32 -08001813static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1814 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001816 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001817 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 void __user* argp = (void __user*)arg;
1819 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001820 kuid_t owner;
1821 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001822 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001823 int vnet_hdr_sz;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001824 unsigned int ifindex;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001825 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Jason Wangcde8b152012-10-31 19:46:01 +00001827 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001828 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001830 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001831 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001832 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001833 if (cmd == TUNGETFEATURES) {
1834 /* Currently this just means: "what IFF flags are valid?".
1835 * This is needed because we never checked for invalid flags on
1836 * TUNSETIFF. */
1837 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
Jason Wangcde8b152012-10-31 19:46:01 +00001838 IFF_VNET_HDR | IFF_MULTI_QUEUE,
Eric W. Biederman631ab462009-01-20 11:00:40 +00001839 (unsigned int __user*)argp);
Jason Wangcde8b152012-10-31 19:46:01 +00001840 } else if (cmd == TUNSETQUEUE)
1841 return tun_set_queue(file, &ifr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001842
Jason Wangc8d68e62012-10-31 19:46:00 +00001843 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001844 rtnl_lock();
1845
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001846 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1849
Herbert Xu876bfd42009-08-06 14:22:44 +00001850 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Herbert Xu876bfd42009-08-06 14:22:44 +00001852 if (ret)
1853 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Arnd Bergmann50857e22009-11-06 22:52:32 -08001855 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001856 ret = -EFAULT;
1857 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04001859 if (cmd == TUNSETIFINDEX) {
1860 ret = -EPERM;
1861 if (tun)
1862 goto unlock;
1863
1864 ret = -EFAULT;
1865 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1866 goto unlock;
1867
1868 ret = 0;
1869 tfile->ifindex = ifindex;
1870 goto unlock;
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Herbert Xu876bfd42009-08-06 14:22:44 +00001873 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001875 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Jason Wang1e588332012-10-31 19:45:56 +00001877 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Eric W. Biederman631ab462009-01-20 11:00:40 +00001879 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001881 case TUNGETIFF:
Rami Rosen9ce99cf2012-11-23 03:58:10 +00001882 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001883
Pavel Emelyanov3d407a82013-08-21 14:32:00 +04001884 if (tfile->detached)
1885 ifr.ifr_flags |= IFF_DETACH_QUEUE;
1886
Arnd Bergmann50857e22009-11-06 22:52:32 -08001887 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001888 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001889 break;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 case TUNSETNOCSUM:
1892 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Michał Mirosław88255372011-04-19 06:13:10 +00001894 /* [unimplemented] */
1895 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001896 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 break;
1898
1899 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001900 /* Disable/Enable persist mode. Keep an extra reference to the
1901 * module to prevent the module being unprobed.
1902 */
Jason Wangdd38bd82013-01-11 16:59:34 +00001903 if (arg && !(tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001905 __module_get(THIS_MODULE);
Jason Wangdd38bd82013-01-11 16:59:34 +00001906 }
1907 if (!arg && (tun->flags & TUN_PERSIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001909 module_put(THIS_MODULE);
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Joe Perches6b8a66e2011-03-02 07:18:10 +00001912 tun_debug(KERN_INFO, tun, "persist %s\n",
1913 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
1915
1916 case TUNSETOWNER:
1917 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001918 owner = make_kuid(current_user_ns(), arg);
1919 if (!uid_valid(owner)) {
1920 ret = -EINVAL;
1921 break;
1922 }
1923 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001924 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001925 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
1927
Guido Guenther8c644622007-07-02 22:50:25 -07001928 case TUNSETGROUP:
1929 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001930 group = make_kgid(current_user_ns(), arg);
1931 if (!gid_valid(group)) {
1932 ret = -EINVAL;
1933 break;
1934 }
1935 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001936 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001937 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001938 break;
1939
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001940 case TUNSETLINK:
1941 /* Only allow setting the type when the interface is down */
1942 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001943 tun_debug(KERN_INFO, tun,
1944 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001945 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001946 } else {
1947 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001948 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1949 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001950 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001951 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001952 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954#ifdef TUN_DEBUG
1955 case TUNSETDEBUG:
1956 tun->debug = arg;
1957 break;
1958#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001959 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001960 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001961 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001962
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001963 case TUNSETTXFILTER:
1964 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001965 ret = -EINVAL;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001966 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001967 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001968 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001972 /* Get hw address */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001973 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1974 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001975 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001976 ret = -EFAULT;
1977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001980 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00001981 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1982 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001983
Kim B. Heino40102372008-02-29 12:26:21 -08001984 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001985 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001986
1987 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00001988 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001989 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1990 ret = -EFAULT;
1991 break;
1992
1993 case TUNSETSNDBUF:
1994 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1995 ret = -EFAULT;
1996 break;
1997 }
1998
Jason Wangc8d68e62012-10-31 19:46:00 +00001999 tun->sndbuf = sndbuf;
2000 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08002001 break;
2002
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02002003 case TUNGETVNETHDRSZ:
2004 vnet_hdr_sz = tun->vnet_hdr_sz;
2005 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2006 ret = -EFAULT;
2007 break;
2008
2009 case TUNSETVNETHDRSZ:
2010 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2011 ret = -EFAULT;
2012 break;
2013 }
2014 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2015 ret = -EINVAL;
2016 break;
2017 }
2018
2019 tun->vnet_hdr_sz = vnet_hdr_sz;
2020 break;
2021
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002022 case TUNATTACHFILTER:
2023 /* Can be set only for TAPs */
2024 ret = -EINVAL;
2025 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2026 break;
2027 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00002028 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002029 break;
2030
Jason Wangc8d68e62012-10-31 19:46:00 +00002031 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002032 break;
2033
2034 case TUNDETACHFILTER:
2035 /* Can be set only for TAPs */
2036 ret = -EINVAL;
2037 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2038 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00002039 ret = 0;
2040 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00002041 break;
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00002044 ret = -EINVAL;
2045 break;
Joe Perchesee289b62010-05-17 22:47:34 -07002046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Herbert Xu876bfd42009-08-06 14:22:44 +00002048unlock:
2049 rtnl_unlock();
2050 if (tun)
2051 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002052 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Arnd Bergmann50857e22009-11-06 22:52:32 -08002055static long tun_chr_ioctl(struct file *file,
2056 unsigned int cmd, unsigned long arg)
2057{
2058 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2059}
2060
2061#ifdef CONFIG_COMPAT
2062static long tun_chr_compat_ioctl(struct file *file,
2063 unsigned int cmd, unsigned long arg)
2064{
2065 switch (cmd) {
2066 case TUNSETIFF:
2067 case TUNGETIFF:
2068 case TUNSETTXFILTER:
2069 case TUNGETSNDBUF:
2070 case TUNSETSNDBUF:
2071 case SIOCGIFHWADDR:
2072 case SIOCSIFHWADDR:
2073 arg = (unsigned long)compat_ptr(arg);
2074 break;
2075 default:
2076 arg = (compat_ulong_t)arg;
2077 break;
2078 }
2079
2080 /*
2081 * compat_ifreq is shorter than ifreq, so we must not access beyond
2082 * the end of that structure. All fields that are used in this
2083 * driver are compatible though, we don't need to convert the
2084 * contents.
2085 */
2086 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2087}
2088#endif /* CONFIG_COMPAT */
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090static int tun_chr_fasync(int fd, struct file *file, int on)
2091{
Jason Wang54f968d2012-10-31 19:45:57 +00002092 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 int ret;
2094
Jason Wang54f968d2012-10-31 19:45:57 +00002095 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002096 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07002099 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06002101 goto out;
Jason Wang54f968d2012-10-31 19:45:57 +00002102 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002103 } else
Jason Wang54f968d2012-10-31 19:45:57 +00002104 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06002105 ret = 0;
2106out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06002107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110static int tun_chr_open(struct inode *inode, struct file * file)
2111{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002112 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07002113
Joe Perches6b8a66e2011-03-02 07:18:10 +00002114 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00002115
Jason Wang54f968d2012-10-31 19:45:57 +00002116 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
2117 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00002118 if (!tfile)
2119 return -ENOMEM;
Jason Wang6e914fc2012-10-31 19:45:58 +00002120 rcu_assign_pointer(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00002121 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00002122 tfile->flags = 0;
Pavel Emelyanovfb7589a2013-08-21 14:31:38 +04002123 tfile->ifindex = 0;
Jason Wang54f968d2012-10-31 19:45:57 +00002124
2125 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
2126 init_waitqueue_head(&tfile->wq.wait);
2127
2128 tfile->socket.file = file;
2129 tfile->socket.ops = &tun_socket_ops;
2130
2131 sock_init_data(&tfile->socket, &tfile->sk);
2132 sk_change_net(&tfile->sk, tfile->net);
2133
2134 tfile->sk.sk_write_space = tun_sock_write_space;
2135 tfile->sk.sk_sndbuf = INT_MAX;
2136
Eric W. Biederman631ab462009-01-20 11:00:40 +00002137 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00002138 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
Jason Wang4008e972012-12-13 23:53:30 +00002139 INIT_LIST_HEAD(&tfile->next);
Jason Wang54f968d2012-10-31 19:45:57 +00002140
Jason Wang19a6afb2013-06-08 14:17:41 +08002141 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144}
2145
2146static int tun_chr_close(struct inode *inode, struct file *file)
2147{
Eric W. Biederman631ab462009-01-20 11:00:40 +00002148 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00002149 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Jason Wangc8d68e62012-10-31 19:46:00 +00002151 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00002152 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 return 0;
2155}
2156
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08002157static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002158 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002160 .read = do_sync_read,
2161 .aio_read = tun_chr_aio_read,
2162 .write = do_sync_write,
2163 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08002165 .unlocked_ioctl = tun_chr_ioctl,
2166#ifdef CONFIG_COMPAT
2167 .compat_ioctl = tun_chr_compat_ioctl,
2168#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 .open = tun_chr_open,
2170 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002171 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172};
2173
2174static struct miscdevice tun_miscdev = {
2175 .minor = TUN_MINOR,
2176 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02002177 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179};
2180
2181/* ethtool interface */
2182
2183static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2184{
2185 cmd->supported = 0;
2186 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00002187 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 cmd->duplex = DUPLEX_FULL;
2189 cmd->port = PORT_TP;
2190 cmd->phy_address = 0;
2191 cmd->transceiver = XCVR_INTERNAL;
2192 cmd->autoneg = AUTONEG_DISABLE;
2193 cmd->maxtxpkt = 0;
2194 cmd->maxrxpkt = 0;
2195 return 0;
2196}
2197
2198static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2199{
2200 struct tun_struct *tun = netdev_priv(dev);
2201
Rick Jones33a5ba12011-11-15 14:59:53 +00002202 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2203 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 switch (tun->flags & TUN_TYPE_MASK) {
2206 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002207 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 break;
2209 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00002210 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 break;
2212 }
2213}
2214
2215static u32 tun_get_msglevel(struct net_device *dev)
2216{
2217#ifdef TUN_DEBUG
2218 struct tun_struct *tun = netdev_priv(dev);
2219 return tun->debug;
2220#else
2221 return -EOPNOTSUPP;
2222#endif
2223}
2224
2225static void tun_set_msglevel(struct net_device *dev, u32 value)
2226{
2227#ifdef TUN_DEBUG
2228 struct tun_struct *tun = netdev_priv(dev);
2229 tun->debug = value;
2230#endif
2231}
2232
Jeff Garzik7282d492006-09-13 14:30:00 -04002233static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 .get_settings = tun_get_settings,
2235 .get_drvinfo = tun_get_drvinfo,
2236 .get_msglevel = tun_get_msglevel,
2237 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00002238 .get_link = ethtool_op_get_link,
Richard Cochraneda29772013-07-19 19:40:10 +02002239 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240};
2241
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243static int __init tun_init(void)
2244{
2245 int ret = 0;
2246
Joe Perches6b8a66e2011-03-02 07:18:10 +00002247 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2248 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002250 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002251 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002252 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002253 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002254 }
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002257 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00002258 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002259 goto err_misc;
2260 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002261 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07002262err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002263 rtnl_link_unregister(&tun_link_ops);
2264err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return ret;
2266}
2267
2268static void tun_cleanup(void)
2269{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002270 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08002271 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002274/* Get an underlying socket object from tun file. Returns error unless file is
2275 * attached to a device. The returned object works like a packet socket, it
2276 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2277 * holding a reference to the file for as long as the socket is in use. */
2278struct socket *tun_get_socket(struct file *file)
2279{
Jason Wang6e914fc2012-10-31 19:45:58 +00002280 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002281 if (file->f_op != &tun_fops)
2282 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00002283 tfile = file->private_data;
2284 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002285 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00002286 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00002287}
2288EXPORT_SYMBOL_GPL(tun_get_socket);
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290module_init(tun_init);
2291module_exit(tun_cleanup);
2292MODULE_DESCRIPTION(DRV_DESCRIPTION);
2293MODULE_AUTHOR(DRV_COPYRIGHT);
2294MODULE_LICENSE("GPL");
2295MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02002296MODULE_ALIAS("devname:net/tun");