blob: 2762c55aeb6692d5adb746b92beb0c160c19f37c [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>
63#include <linux/crc32.h>
Pavel Emelyanovd647a592008-04-16 00:41:16 -070064#include <linux/nsproxy.h>
Rusty Russellf43798c2008-07-03 03:48:02 -070065#include <linux/virtio_net.h>
Michael S. Tsirkin99405162010-02-14 01:01:10 +000066#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070067#include <net/net_namespace.h>
Pavel Emelyanov79d17602008-04-16 00:40:46 -070068#include <net/netns/generic.h>
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -080069#include <net/rtnetlink.h>
Herbert Xu33dccbb2009-02-05 21:25:32 -080070#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <asm/uaccess.h>
73
Rusty Russell14daa022008-04-12 18:48:58 -070074/* Uncomment to enable debugging */
75/* #define TUN_DEBUG 1 */
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#ifdef TUN_DEBUG
78static int debug;
Rusty Russell14daa022008-04-12 18:48:58 -070079
Joe Perches6b8a66e2011-03-02 07:18:10 +000080#define tun_debug(level, tun, fmt, args...) \
81do { \
82 if (tun->debug) \
83 netdev_printk(level, tun->dev, fmt, ##args); \
84} while (0)
85#define DBG1(level, fmt, args...) \
86do { \
87 if (debug == 2) \
88 printk(level fmt, ##args); \
89} while (0)
Rusty Russell14daa022008-04-12 18:48:58 -070090#else
Joe Perches6b8a66e2011-03-02 07:18:10 +000091#define tun_debug(level, tun, fmt, args...) \
92do { \
93 if (0) \
94 netdev_printk(level, tun->dev, fmt, ##args); \
95} while (0)
96#define DBG1(level, fmt, args...) \
97do { \
98 if (0) \
99 printk(level fmt, ##args); \
100} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
102
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000103#define GOODCOPY_LEN 128
104
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700105#define FLT_EXACT_COUNT 8
106struct tap_filter {
107 unsigned int count; /* Number of addrs. Zero means disabled */
108 u32 mask[2]; /* Mask of the hashed addrs */
109 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
110};
111
Jason Wangc8d68e62012-10-31 19:46:00 +0000112/* 1024 is probably a high enough limit: modern hypervisors seem to support on
113 * the order of 100-200 CPUs so this leaves us some breathing space if we want
114 * to match a queue per guest CPU.
115 */
116#define MAX_TAP_QUEUES 1024
117
Jason Wang54f968d2012-10-31 19:45:57 +0000118/* A tun_file connects an open character device to a tuntap netdevice. It
119 * also contains all socket related strctures (except sock_fprog and tap_filter)
120 * to serve as one transmit queue for tuntap device. The sock_fprog and
121 * tap_filter were kept in tun_struct since they were used for filtering for the
122 * netdevice not for a specific queue (at least I didn't see the reqirement for
123 * this).
Jason Wang6e914fc2012-10-31 19:45:58 +0000124 *
125 * RCU usage:
126 * The tun_file and tun_struct are loosely coupled, the pointer from on to the
127 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000128 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000129struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000130 struct sock sk;
131 struct socket socket;
132 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000133 struct tun_struct __rcu *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000134 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000135 struct fasync_struct *fasync;
136 /* only used for fasnyc */
137 unsigned int flags;
Jason Wangc8d68e62012-10-31 19:46:00 +0000138 u16 queue_index;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000139};
140
Jason Wang54f968d2012-10-31 19:45:57 +0000141/* Since the socket were moved to tun_file, to preserve the behavior of persist
142 * device, socket fileter, sndbuf and vnet header size were restore when the
143 * file were attached to a persist device.
144 */
Rusty Russell14daa022008-04-12 18:48:58 -0700145struct tun_struct {
Jason Wangc8d68e62012-10-31 19:46:00 +0000146 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
147 unsigned int numqueues;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700148 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800149 kuid_t owner;
150 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700151
Rusty Russell14daa022008-04-12 18:48:58 -0700152 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000153 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000154#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
155 NETIF_F_TSO6|NETIF_F_UFO)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200156
157 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000158 int sndbuf;
159 struct tap_filter txflt;
160 struct sock_fprog fprog;
161 /* protected by rtnl lock */
162 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700163#ifdef TUN_DEBUG
164 int debug;
165#endif
166};
167
Jason Wangc8d68e62012-10-31 19:46:00 +0000168/* We try to identify a flow through its rxhash first. The reason that
169 * we do not check rxq no. is becuase some cards(e.g 82599), chooses
170 * the rxq based on the txq where the last packet of the flow comes. As
171 * the userspace application move between processors, we may get a
172 * different rxq no. here. If we could not get rxhash, then we would
173 * hope the rxq no. may help here.
174 */
175static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
176{
177 struct tun_struct *tun = netdev_priv(dev);
178 u32 txq = 0;
179 u32 numqueues = 0;
180
181 rcu_read_lock();
182 numqueues = tun->numqueues;
183
184 txq = skb_get_rxhash(skb);
185 if (txq) {
186 /* use multiply and shift instead of expensive divide */
187 txq = ((u64)txq * numqueues) >> 32;
188 } else if (likely(skb_rx_queue_recorded(skb))) {
189 txq = skb_get_rx_queue(skb);
190 while (unlikely(txq >= numqueues))
191 txq -= numqueues;
192 }
193
194 rcu_read_unlock();
195 return txq;
196}
197
198static void tun_set_real_num_queues(struct tun_struct *tun)
199{
200 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
201 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
202}
203
204static void __tun_detach(struct tun_file *tfile, bool clean)
205{
206 struct tun_file *ntfile;
207 struct tun_struct *tun;
208 struct net_device *dev;
209
210 tun = rcu_dereference_protected(tfile->tun,
211 lockdep_rtnl_is_held());
212 if (tun) {
213 u16 index = tfile->queue_index;
214 BUG_ON(index >= tun->numqueues);
215 dev = tun->dev;
216
217 rcu_assign_pointer(tun->tfiles[index],
218 tun->tfiles[tun->numqueues - 1]);
219 rcu_assign_pointer(tfile->tun, NULL);
220 ntfile = rcu_dereference_protected(tun->tfiles[index],
221 lockdep_rtnl_is_held());
222 ntfile->queue_index = index;
223
224 --tun->numqueues;
225 sock_put(&tfile->sk);
226
227 synchronize_net();
228 /* Drop read queue */
229 skb_queue_purge(&tfile->sk.sk_receive_queue);
230 tun_set_real_num_queues(tun);
231
232 if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST))
233 if (dev->reg_state == NETREG_REGISTERED)
234 unregister_netdevice(dev);
235 }
236
237 if (clean) {
238 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
239 &tfile->socket.flags));
240 sk_release_kernel(&tfile->sk);
241 }
242}
243
244static void tun_detach(struct tun_file *tfile, bool clean)
245{
246 rtnl_lock();
247 __tun_detach(tfile, clean);
248 rtnl_unlock();
249}
250
251static void tun_detach_all(struct net_device *dev)
252{
253 struct tun_struct *tun = netdev_priv(dev);
254 struct tun_file *tfile;
255 int i, n = tun->numqueues;
256
257 for (i = 0; i < n; i++) {
258 tfile = rcu_dereference_protected(tun->tfiles[i],
259 lockdep_rtnl_is_held());
260 BUG_ON(!tfile);
261 wake_up_all(&tfile->wq.wait);
262 rcu_assign_pointer(tfile->tun, NULL);
263 --tun->numqueues;
264 }
265 BUG_ON(tun->numqueues != 0);
266
267 synchronize_net();
268 for (i = 0; i < n; i++) {
269 tfile = rcu_dereference_protected(tun->tfiles[i],
270 lockdep_rtnl_is_held());
271 /* Drop read queue */
272 skb_queue_purge(&tfile->sk.sk_receive_queue);
273 sock_put(&tfile->sk);
274 }
275}
276
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000277static int tun_attach(struct tun_struct *tun, struct file *file)
278{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000279 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000280 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000281
Eric W. Biederman38231b72009-01-20 11:02:28 +0000282 err = -EINVAL;
Jason Wangc8d68e62012-10-31 19:46:00 +0000283 if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
Eric W. Biederman38231b72009-01-20 11:02:28 +0000284 goto out;
285
286 err = -EBUSY;
Jason Wangc8d68e62012-10-31 19:46:00 +0000287 if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
288 goto out;
289
290 err = -E2BIG;
291 if (tun->numqueues == MAX_TAP_QUEUES)
Eric W. Biederman38231b72009-01-20 11:02:28 +0000292 goto out;
293
294 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000295
Jason Wangc8d68e62012-10-31 19:46:00 +0000296 /* Re-attach the filter to presist device */
Jason Wang54f968d2012-10-31 19:45:57 +0000297 if (tun->filter_attached == true) {
298 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
299 if (!err)
300 goto out;
301 }
Jason Wangc8d68e62012-10-31 19:46:00 +0000302 tfile->queue_index = tun->numqueues;
Jason Wang6e914fc2012-10-31 19:45:58 +0000303 rcu_assign_pointer(tfile->tun, tun);
Jason Wangc8d68e62012-10-31 19:46:00 +0000304 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
Jason Wang54f968d2012-10-31 19:45:57 +0000305 sock_hold(&tfile->sk);
Jason Wangc8d68e62012-10-31 19:46:00 +0000306 tun->numqueues++;
307
308 tun_set_real_num_queues(tun);
309
310 if (tun->numqueues == 1)
311 netif_carrier_on(tun->dev);
312
313 /* device is allowed to go away first, so no need to hold extra
314 * refcnt.
315 */
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000316
Eric W. Biederman38231b72009-01-20 11:02:28 +0000317out:
Eric W. Biederman38231b72009-01-20 11:02:28 +0000318 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000319}
320
Eric W. Biederman631ab462009-01-20 11:00:40 +0000321static struct tun_struct *__tun_get(struct tun_file *tfile)
322{
Jason Wang6e914fc2012-10-31 19:45:58 +0000323 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000324
Jason Wang6e914fc2012-10-31 19:45:58 +0000325 rcu_read_lock();
326 tun = rcu_dereference(tfile->tun);
327 if (tun)
328 dev_hold(tun->dev);
329 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000330
331 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000332}
333
334static struct tun_struct *tun_get(struct file *file)
335{
336 return __tun_get(file->private_data);
337}
338
339static void tun_put(struct tun_struct *tun)
340{
Jason Wang6e914fc2012-10-31 19:45:58 +0000341 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000342}
343
Joe Perches6b8a66e2011-03-02 07:18:10 +0000344/* TAP filtering */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700345static void addr_hash_set(u32 *mask, const u8 *addr)
346{
347 int n = ether_crc(ETH_ALEN, addr) >> 26;
348 mask[n >> 5] |= (1 << (n & 31));
349}
350
351static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
352{
353 int n = ether_crc(ETH_ALEN, addr) >> 26;
354 return mask[n >> 5] & (1 << (n & 31));
355}
356
357static int update_filter(struct tap_filter *filter, void __user *arg)
358{
359 struct { u8 u[ETH_ALEN]; } *addr;
360 struct tun_filter uf;
361 int err, alen, n, nexact;
362
363 if (copy_from_user(&uf, arg, sizeof(uf)))
364 return -EFAULT;
365
366 if (!uf.count) {
367 /* Disabled */
368 filter->count = 0;
369 return 0;
370 }
371
372 alen = ETH_ALEN * uf.count;
373 addr = kmalloc(alen, GFP_KERNEL);
374 if (!addr)
375 return -ENOMEM;
376
377 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
378 err = -EFAULT;
379 goto done;
380 }
381
382 /* The filter is updated without holding any locks. Which is
383 * perfectly safe. We disable it first and in the worst
384 * case we'll accept a few undesired packets. */
385 filter->count = 0;
386 wmb();
387
388 /* Use first set of addresses as an exact filter */
389 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
390 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
391
392 nexact = n;
393
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800394 /* Remaining multicast addresses are hashed,
395 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700396 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800397 for (; n < uf.count; n++) {
398 if (!is_multicast_ether_addr(addr[n].u)) {
399 err = 0; /* no filter */
400 goto done;
401 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700402 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800403 }
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700404
405 /* For ALLMULTI just set the mask to all ones.
406 * This overrides the mask populated above. */
407 if ((uf.flags & TUN_FLT_ALLMULTI))
408 memset(filter->mask, ~0, sizeof(filter->mask));
409
410 /* Now enable the filter */
411 wmb();
412 filter->count = nexact;
413
414 /* Return the number of exact filters */
415 err = nexact;
416
417done:
418 kfree(addr);
419 return err;
420}
421
422/* Returns: 0 - drop, !=0 - accept */
423static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
424{
425 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
426 * at this point. */
427 struct ethhdr *eh = (struct ethhdr *) skb->data;
428 int i;
429
430 /* Exact match */
431 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000432 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700433 return 1;
434
435 /* Inexact match (multicast only) */
436 if (is_multicast_ether_addr(eh->h_dest))
437 return addr_hash_test(filter->mask, eh->h_dest);
438
439 return 0;
440}
441
442/*
443 * Checks whether the packet is accepted or not.
444 * Returns: 0 - drop, !=0 - accept
445 */
446static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
447{
448 if (!filter->count)
449 return 1;
450
451 return run_filter(filter, skb);
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* Network device part of the driver */
455
Jeff Garzik7282d492006-09-13 14:30:00 -0400456static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000458/* Net device detach from fd. */
459static void tun_net_uninit(struct net_device *dev)
460{
Jason Wangc8d68e62012-10-31 19:46:00 +0000461 tun_detach_all(dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/* Net device open. */
465static int tun_net_open(struct net_device *dev)
466{
Jason Wangc8d68e62012-10-31 19:46:00 +0000467 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
469}
470
471/* Net device close. */
472static int tun_net_close(struct net_device *dev)
473{
Jason Wangc8d68e62012-10-31 19:46:00 +0000474 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
476}
477
478/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000479static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct tun_struct *tun = netdev_priv(dev);
Jason Wangc8d68e62012-10-31 19:46:00 +0000482 int txq = skb->queue_mapping;
Jason Wang6e914fc2012-10-31 19:45:58 +0000483 struct tun_file *tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jason Wang6e914fc2012-10-31 19:45:58 +0000485 rcu_read_lock();
Jason Wangc8d68e62012-10-31 19:46:00 +0000486 tfile = rcu_dereference(tun->tfiles[txq]);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* Drop packet if interface is not attached */
Jason Wangc8d68e62012-10-31 19:46:00 +0000489 if (txq >= tun->numqueues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto drop;
491
Jason Wang6e914fc2012-10-31 19:45:58 +0000492 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
493
Jason Wangc8d68e62012-10-31 19:46:00 +0000494 BUG_ON(!tfile);
495
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700496 /* Drop if the filter does not like it.
497 * This is a noop if the filter is disabled.
498 * Filter can be enabled only for the TAP devices. */
499 if (!check_filter(&tun->txflt, skb))
500 goto drop;
501
Jason Wang54f968d2012-10-31 19:45:57 +0000502 if (tfile->socket.sk->sk_filter &&
503 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000504 goto drop;
505
Jason Wangc8d68e62012-10-31 19:46:00 +0000506 /* Limit the number of packets queued by divining txq length with the
507 * number of queues.
508 */
Jason Wang54f968d2012-10-31 19:45:57 +0000509 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
Jason Wangc8d68e62012-10-31 19:46:00 +0000510 >= dev->tx_queue_len / tun->numqueues){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!(tun->flags & TUN_ONE_QUEUE)) {
512 /* Normal queueing mode. */
513 /* Packet scheduler handles dropping of further packets. */
Jason Wangc8d68e62012-10-31 19:46:00 +0000514 netif_stop_subqueue(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* We won't see all dropped packets individually, so overrun
517 * error is more appropriate. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700518 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else {
520 /* Single queue mode.
521 * Driver handles dropping of all packets itself. */
522 goto drop;
523 }
524 }
525
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000526 /* Orphan the skb - required as we might hang on to it
527 * for indefinite time. */
Michael S. Tsirkin868eefe2012-07-20 09:23:14 +0000528 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
529 goto drop;
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000530 skb_orphan(skb);
531
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700532 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000533 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000536 if (tfile->flags & TUN_FASYNC)
537 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
538 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000539 POLLRDNORM | POLLRDBAND);
Jason Wang6e914fc2012-10-31 19:45:58 +0000540
541 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000542 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700545 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000547 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000548 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700551static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700553 /*
554 * This callback is supposed to deal with mc filter in
555 * _rx_ path and has nothing to do with the _tx_ path.
556 * In rx path we always accept everything userspace gives us.
557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Ed Swierk4885a502007-09-16 12:21:38 -0700560#define MIN_MTU 68
561#define MAX_MTU 65535
562
563static int
564tun_net_change_mtu(struct net_device *dev, int new_mtu)
565{
566 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
567 return -EINVAL;
568 dev->mtu = new_mtu;
569 return 0;
570}
571
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000572static netdev_features_t tun_net_fix_features(struct net_device *dev,
573 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000574{
575 struct tun_struct *tun = netdev_priv(dev);
576
577 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
578}
Neil Hormanbebd0972011-06-15 05:25:01 +0000579#ifdef CONFIG_NET_POLL_CONTROLLER
580static void tun_poll_controller(struct net_device *dev)
581{
582 /*
583 * Tun only receives frames when:
584 * 1) the char device endpoint gets data from user space
585 * 2) the tun socket gets a sendmsg call from user space
586 * Since both of those are syncronous operations, we are guaranteed
587 * never to have pending data when we poll for it
588 * so theres nothing to do here but return.
589 * We need this though so netpoll recognizes us as an interface that
590 * supports polling, which enables bridge devices in virt setups to
591 * still use netconsole
592 */
593 return;
594}
595#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800596static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000597 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800598 .ndo_open = tun_net_open,
599 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800600 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800601 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000602 .ndo_fix_features = tun_net_fix_features,
Jason Wangc8d68e62012-10-31 19:46:00 +0000603 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000604#ifdef CONFIG_NET_POLL_CONTROLLER
605 .ndo_poll_controller = tun_poll_controller,
606#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800607};
608
609static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000610 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800611 .ndo_open = tun_net_open,
612 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800613 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800614 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000615 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000616 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800617 .ndo_set_mac_address = eth_mac_addr,
618 .ndo_validate_addr = eth_validate_addr,
Jason Wangc8d68e62012-10-31 19:46:00 +0000619 .ndo_select_queue = tun_select_queue,
Neil Hormanbebd0972011-06-15 05:25:01 +0000620#ifdef CONFIG_NET_POLL_CONTROLLER
621 .ndo_poll_controller = tun_poll_controller,
622#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800623};
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/* Initialize net device. */
626static void tun_net_init(struct net_device *dev)
627{
628 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 switch (tun->flags & TUN_TYPE_MASK) {
631 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800632 dev->netdev_ops = &tun_netdev_ops;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Point-to-Point TUN Device */
635 dev->hard_header_len = 0;
636 dev->addr_len = 0;
637 dev->mtu = 1500;
638
639 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400640 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
642 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
643 break;
644
645 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800646 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* Ethernet TAP Device */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -0700648 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000649 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000651 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
654 break;
655 }
656}
657
658/* Character device part */
659
660/* Poll */
Jason Wangc8d68e62012-10-31 19:46:00 +0000661static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400662{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000663 struct tun_file *tfile = file->private_data;
664 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000665 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800666 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000669 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jason Wang54f968d2012-10-31 19:45:57 +0000671 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000672
Joe Perches6b8a66e2011-03-02 07:18:10 +0000673 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jason Wang54f968d2012-10-31 19:45:57 +0000675 poll_wait(file, &tfile->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400676
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000677 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 mask |= POLLIN | POLLRDNORM;
679
Herbert Xu33dccbb2009-02-05 21:25:32 -0800680 if (sock_writeable(sk) ||
681 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
682 sock_writeable(sk)))
683 mask |= POLLOUT | POLLWRNORM;
684
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000685 if (tun->dev->reg_state != NETREG_REGISTERED)
686 mask = POLLERR;
687
Eric W. Biederman631ab462009-01-20 11:00:40 +0000688 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return mask;
690}
691
Rusty Russellf42157c2008-08-15 15:15:10 -0700692/* prepad is the amount to reserve at front. len is length after that.
693 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000694static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000695 size_t prepad, size_t len,
696 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700697{
Jason Wang54f968d2012-10-31 19:45:57 +0000698 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700699 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800700 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700701
702 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700703 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800704 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700705
Herbert Xu33dccbb2009-02-05 21:25:32 -0800706 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
707 &err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700708 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800709 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700710
711 skb_reserve(skb, prepad);
712 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800713 skb->data_len = len - linear;
714 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700715
716 return skb;
717}
718
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000719/* set skb frags from iovec, this can move to core network code for reuse */
720static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
721 int offset, size_t count)
722{
723 int len = iov_length(from, count) - offset;
724 int copy = skb_headlen(skb);
725 int size, offset1 = 0;
726 int i = 0;
727
728 /* Skip over from offset */
729 while (count && (offset >= from->iov_len)) {
730 offset -= from->iov_len;
731 ++from;
732 --count;
733 }
734
735 /* copy up to skb headlen */
736 while (count && (copy > 0)) {
737 size = min_t(unsigned int, copy, from->iov_len - offset);
738 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
739 size))
740 return -EFAULT;
741 if (copy > size) {
742 ++from;
743 --count;
744 offset = 0;
745 } else
746 offset += size;
747 copy -= size;
748 offset1 += size;
749 }
750
751 if (len == offset1)
752 return 0;
753
754 while (count--) {
755 struct page *page[MAX_SKB_FRAGS];
756 int num_pages;
757 unsigned long base;
758 unsigned long truesize;
759
760 len = from->iov_len - offset;
761 if (!len) {
762 offset = 0;
763 ++from;
764 continue;
765 }
766 base = (unsigned long)from->iov_base + offset;
767 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
768 if (i + size > MAX_SKB_FRAGS)
769 return -EMSGSIZE;
770 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
771 if (num_pages != size) {
772 for (i = 0; i < num_pages; i++)
773 put_page(page[i]);
774 return -EFAULT;
775 }
776 truesize = size * PAGE_SIZE;
777 skb->data_len += len;
778 skb->len += len;
779 skb->truesize += truesize;
780 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
781 while (len) {
782 int off = base & ~PAGE_MASK;
783 int size = min_t(int, len, PAGE_SIZE - off);
784 __skb_fill_page_desc(skb, i, page[i], off, size);
785 skb_shinfo(skb)->nr_frags++;
786 /* increase sk_wmem_alloc */
787 base += size;
788 len -= size;
789 i++;
790 }
791 offset = 0;
792 ++from;
793 }
794 return 0;
795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +0000798static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
799 void *msg_control, const struct iovec *iv,
800 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Harvey Harrison09640e62009-02-01 00:45:17 -0800802 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct sk_buff *skb;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000804 size_t len = total_len, align = NET_SKB_PAD;
Rusty Russellf43798c2008-07-03 03:48:02 -0700805 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000806 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000807 int copylen;
808 bool zerocopy = false;
809 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (!(tun->flags & TUN_NO_PI)) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000812 if ((len -= sizeof(pi)) > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return -EINVAL;
814
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000815 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000817 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
Rusty Russellf43798c2008-07-03 03:48:02 -0700820 if (tun->flags & TUN_VNET_HDR) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000821 if ((len -= tun->vnet_hdr_sz) > total_len)
Rusty Russellf43798c2008-07-03 03:48:02 -0700822 return -EINVAL;
823
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000824 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -0700825 return -EFAULT;
826
Herbert Xu49091222009-06-08 00:20:01 -0700827 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
828 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
829 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
830
Rusty Russellf43798c2008-07-03 03:48:02 -0700831 if (gso.hdr_len > len)
832 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200833 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700834 }
835
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700836 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +0000837 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -0700838 if (unlikely(len < ETH_HLEN ||
839 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700840 return -EINVAL;
841 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400842
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000843 if (msg_control)
844 zerocopy = true;
845
846 if (zerocopy) {
847 /* Userspace may produce vectors with count greater than
848 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
849 * to let the rest of data to be fit in the frags.
850 */
851 if (count > MAX_SKB_FRAGS) {
852 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
853 if (copylen < offset)
854 copylen = 0;
855 else
856 copylen -= offset;
857 } else
858 copylen = 0;
859 /* There are 256 bytes to be copied in skb, so there is enough
860 * room for skb expand head in case it is used.
861 * The rest of the buffer is mapped from userspace.
862 */
863 if (copylen < gso.hdr_len)
864 copylen = gso.hdr_len;
865 if (!copylen)
866 copylen = GOODCOPY_LEN;
867 } else
868 copylen = len;
869
Jason Wang54f968d2012-10-31 19:45:57 +0000870 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800871 if (IS_ERR(skb)) {
872 if (PTR_ERR(skb) != -EAGAIN)
873 tun->dev->stats.rx_dropped++;
874 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000877 if (zerocopy)
878 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
879 else
880 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
881
882 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700883 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -0800884 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -0800886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Rusty Russellf43798c2008-07-03 03:48:02 -0700888 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
889 if (!skb_partial_csum_set(skb, gso.csum_start,
890 gso.csum_offset)) {
891 tun->dev->stats.rx_frame_errors++;
892 kfree_skb(skb);
893 return -EINVAL;
894 }
Michał Mirosław88255372011-04-19 06:13:10 +0000895 }
Rusty Russellf43798c2008-07-03 03:48:02 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 switch (tun->flags & TUN_TYPE_MASK) {
898 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -0700899 if (tun->flags & TUN_NO_PI) {
900 switch (skb->data[0] & 0xf0) {
901 case 0x40:
902 pi.proto = htons(ETH_P_IP);
903 break;
904 case 0x60:
905 pi.proto = htons(ETH_P_IPV6);
906 break;
907 default:
908 tun->dev->stats.rx_dropped++;
909 kfree_skb(skb);
910 return -EINVAL;
911 }
912 }
913
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700914 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700916 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
918 case TUN_TAP_DEV:
919 skb->protocol = eth_type_trans(skb, tun->dev);
920 break;
Joe Perches6403eab2011-06-03 11:51:20 +0000921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Rusty Russellf43798c2008-07-03 03:48:02 -0700923 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
924 pr_debug("GSO!\n");
925 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
926 case VIRTIO_NET_HDR_GSO_TCPV4:
927 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
928 break;
929 case VIRTIO_NET_HDR_GSO_TCPV6:
930 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
931 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +0000932 case VIRTIO_NET_HDR_GSO_UDP:
933 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
934 break;
Rusty Russellf43798c2008-07-03 03:48:02 -0700935 default:
936 tun->dev->stats.rx_frame_errors++;
937 kfree_skb(skb);
938 return -EINVAL;
939 }
940
941 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
942 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
943
944 skb_shinfo(skb)->gso_size = gso.gso_size;
945 if (skb_shinfo(skb)->gso_size == 0) {
946 tun->dev->stats.rx_frame_errors++;
947 kfree_skb(skb);
948 return -EINVAL;
949 }
950
951 /* Header must be checked, and gso_segs computed. */
952 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
953 skb_shinfo(skb)->gso_segs = 0;
954 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400955
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000956 /* copy skb_ubuf_info for callback when skb has no error */
957 if (zerocopy) {
958 skb_shinfo(skb)->destructor_arg = msg_control;
959 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
960 }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400963
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700964 tun->dev->stats.rx_packets++;
965 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000967 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400968}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700970static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
971 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Herbert Xu33dccbb2009-02-05 21:25:32 -0800973 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -0800974 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +0000975 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000976 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 if (!tun)
979 return -EBADFD;
980
Joe Perches6b8a66e2011-03-02 07:18:10 +0000981 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jason Wang54f968d2012-10-31 19:45:57 +0000983 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
984 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000985
986 tun_put(tun);
987 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +0000991static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +0000992 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000993 struct sk_buff *skb,
994 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct tun_pi pi = { 0, skb->protocol };
997 ssize_t total = 0;
998
999 if (!(tun->flags & TUN_NO_PI)) {
1000 if ((len -= sizeof(pi)) < 0)
1001 return -EINVAL;
1002
1003 if (len < skb->len) {
1004 /* Packet will be striped */
1005 pi.flags |= TUN_PKT_STRIP;
1006 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001007
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001008 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return -EFAULT;
1010 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Rusty Russellf43798c2008-07-03 03:48:02 -07001013 if (tun->flags & TUN_VNET_HDR) {
1014 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001015 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -07001016 return -EINVAL;
1017
1018 if (skb_is_gso(skb)) {
1019 struct skb_shared_info *sinfo = skb_shinfo(skb);
1020
1021 /* This is a hint as to how much should be linear. */
1022 gso.hdr_len = skb_headlen(skb);
1023 gso.gso_size = sinfo->gso_size;
1024 if (sinfo->gso_type & SKB_GSO_TCPV4)
1025 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1026 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1027 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001028 else if (sinfo->gso_type & SKB_GSO_UDP)
1029 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001030 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001031 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +00001032 "0x%x, gso_size %d, hdr_len %d\n",
1033 sinfo->gso_type, gso.gso_size,
1034 gso.hdr_len);
1035 print_hex_dump(KERN_ERR, "tun: ",
1036 DUMP_PREFIX_NONE,
1037 16, 1, skb->head,
1038 min((int)gso.hdr_len, 64), true);
1039 WARN_ON_ONCE(1);
1040 return -EINVAL;
1041 }
Rusty Russellf43798c2008-07-03 03:48:02 -07001042 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1043 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1044 } else
1045 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1046
1047 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1048 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001049 gso.csum_start = skb_checksum_start_offset(skb);
Rusty Russellf43798c2008-07-03 03:48:02 -07001050 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001051 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1052 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -07001053 } /* else everything is zero */
1054
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001055 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
1056 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -07001057 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001058 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -07001059 }
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 len = min_t(int, skb->len, len);
1062
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +00001063 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001064 total += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001066 tun->dev->stats.tx_packets++;
1067 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 return total;
1070}
1071
Jason Wang54f968d2012-10-31 19:45:57 +00001072static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001073 struct kiocb *iocb, const struct iovec *iv,
1074 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 DECLARE_WAITQUEUE(wait, current);
1077 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001078 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Joe Perches6b8a66e2011-03-02 07:18:10 +00001080 tun_debug(KERN_INFO, tun, "tun_chr_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Amos Kong61a5ff12011-06-09 00:27:10 -07001082 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001083 add_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 current->state = TASK_INTERRUPTIBLE;
1086
1087 /* Read frames from the queue */
Jason Wang54f968d2012-10-31 19:45:57 +00001088 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001089 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 ret = -EAGAIN;
1091 break;
1092 }
1093 if (signal_pending(current)) {
1094 ret = -ERESTARTSYS;
1095 break;
1096 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001097 if (tun->dev->reg_state != NETREG_REGISTERED) {
1098 ret = -EIO;
1099 break;
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /* Nothing to read, let's sleep */
1103 schedule();
1104 continue;
1105 }
Jason Wangc8d68e62012-10-31 19:46:00 +00001106 netif_wake_subqueue(tun->dev, tfile->queue_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jason Wang54f968d2012-10-31 19:45:57 +00001108 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001109 kfree_skb(skb);
1110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
1113 current->state = TASK_RUNNING;
Amos Kong61a5ff12011-06-09 00:27:10 -07001114 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001115 remove_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001117 return ret;
1118}
1119
1120static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1121 unsigned long count, loff_t pos)
1122{
1123 struct file *file = iocb->ki_filp;
1124 struct tun_file *tfile = file->private_data;
1125 struct tun_struct *tun = __tun_get(tfile);
1126 ssize_t len, ret;
1127
1128 if (!tun)
1129 return -EBADFD;
1130 len = iov_length(iv, count);
1131 if (len < 0) {
1132 ret = -EINVAL;
1133 goto out;
1134 }
1135
Jason Wang54f968d2012-10-31 19:45:57 +00001136 ret = tun_do_read(tun, tfile, iocb, iv, len,
1137 file->f_flags & O_NONBLOCK);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001138 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001139out:
1140 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return ret;
1142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static void tun_setup(struct net_device *dev)
1145{
1146 struct tun_struct *tun = netdev_priv(dev);
1147
Eric W. Biederman0625c882012-02-07 16:48:55 -08001148 tun->owner = INVALID_UID;
1149 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang54f968d2012-10-31 19:45:57 +00001152 dev->destructor = free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001155/* Trivial set of netlink ops to allow deleting tun or tap
1156 * device with netlink.
1157 */
1158static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1159{
1160 return -EINVAL;
1161}
1162
1163static struct rtnl_link_ops tun_link_ops __read_mostly = {
1164 .kind = DRV_NAME,
1165 .priv_size = sizeof(struct tun_struct),
1166 .setup = tun_setup,
1167 .validate = tun_validate,
1168};
1169
Herbert Xu33dccbb2009-02-05 21:25:32 -08001170static void tun_sock_write_space(struct sock *sk)
1171{
Jason Wang54f968d2012-10-31 19:45:57 +00001172 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001173 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001174
1175 if (!sock_writeable(sk))
1176 return;
1177
Herbert Xu33dccbb2009-02-05 21:25:32 -08001178 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1179 return;
1180
Eric Dumazet43815482010-04-29 11:01:49 +00001181 wqueue = sk_sleep(sk);
1182 if (wqueue && waitqueue_active(wqueue))
1183 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001184 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001185
Jason Wang54f968d2012-10-31 19:45:57 +00001186 tfile = container_of(sk, struct tun_file, sk);
1187 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001188}
1189
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001190static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1191 struct msghdr *m, size_t total_len)
1192{
Jason Wang54f968d2012-10-31 19:45:57 +00001193 int ret;
1194 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1195 struct tun_struct *tun = __tun_get(tfile);
1196
1197 if (!tun)
1198 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001199 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1200 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1201 tun_put(tun);
1202 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001203}
1204
Jason Wang54f968d2012-10-31 19:45:57 +00001205
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001206static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1207 struct msghdr *m, size_t total_len,
1208 int flags)
1209{
Jason Wang54f968d2012-10-31 19:45:57 +00001210 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1211 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001212 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001213
1214 if (!tun)
1215 return -EBADFD;
1216
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001217 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1218 return -EINVAL;
Jason Wang54f968d2012-10-31 19:45:57 +00001219 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001220 flags & MSG_DONTWAIT);
1221 if (ret > total_len) {
1222 m->msg_flags |= MSG_TRUNC;
1223 ret = flags & MSG_TRUNC ? ret : total_len;
1224 }
Jason Wang54f968d2012-10-31 19:45:57 +00001225 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001226 return ret;
1227}
1228
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001229static int tun_release(struct socket *sock)
1230{
1231 if (sock->sk)
1232 sock_put(sock->sk);
1233 return 0;
1234}
1235
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001236/* Ops structure to mimic raw sockets with tun */
1237static const struct proto_ops tun_socket_ops = {
1238 .sendmsg = tun_sendmsg,
1239 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001240 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001241};
1242
Herbert Xu33dccbb2009-02-05 21:25:32 -08001243static struct proto tun_proto = {
1244 .name = "tun",
1245 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001246 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001247};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001248
David Woodhouse980c9e82009-05-09 22:54:21 -07001249static int tun_flags(struct tun_struct *tun)
1250{
1251 int flags = 0;
1252
1253 if (tun->flags & TUN_TUN_DEV)
1254 flags |= IFF_TUN;
1255 else
1256 flags |= IFF_TAP;
1257
1258 if (tun->flags & TUN_NO_PI)
1259 flags |= IFF_NO_PI;
1260
1261 if (tun->flags & TUN_ONE_QUEUE)
1262 flags |= IFF_ONE_QUEUE;
1263
1264 if (tun->flags & TUN_VNET_HDR)
1265 flags |= IFF_VNET_HDR;
1266
Jason Wangc8d68e62012-10-31 19:46:00 +00001267 if (tun->flags & TUN_TAP_MQ)
1268 flags |= IFF_MULTI_QUEUE;
1269
David Woodhouse980c9e82009-05-09 22:54:21 -07001270 return flags;
1271}
1272
1273static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1274 char *buf)
1275{
1276 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1277 return sprintf(buf, "0x%x\n", tun_flags(tun));
1278}
1279
1280static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1281 char *buf)
1282{
1283 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001284 return uid_valid(tun->owner)?
1285 sprintf(buf, "%u\n",
1286 from_kuid_munged(current_user_ns(), tun->owner)):
1287 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001288}
1289
1290static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1291 char *buf)
1292{
1293 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001294 return gid_valid(tun->group) ?
1295 sprintf(buf, "%u\n",
1296 from_kgid_munged(current_user_ns(), tun->group)):
1297 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001298}
1299
1300static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1301static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1302static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1303
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001304static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001307 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct net_device *dev;
1309 int err;
1310
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001311 dev = __dev_get_by_name(net, ifr->ifr_name);
1312 if (dev) {
Paul Moore2b980db2009-08-28 18:12:43 -04001313 const struct cred *cred = current_cred();
1314
David Woodhousef85ba782009-04-27 03:23:54 -07001315 if (ifr->ifr_flags & IFF_TUN_EXCL)
1316 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001317 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1318 tun = netdev_priv(dev);
1319 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1320 tun = netdev_priv(dev);
1321 else
1322 return -EINVAL;
1323
Eric W. Biederman0625c882012-02-07 16:48:55 -08001324 if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
1325 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Paul Moore2b980db2009-08-28 18:12:43 -04001326 !capable(CAP_NET_ADMIN))
1327 return -EPERM;
Jason Wang54f968d2012-10-31 19:45:57 +00001328 err = security_tun_dev_attach(tfile->socket.sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001329 if (err < 0)
1330 return err;
1331
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001332 err = tun_attach(tun, file);
1333 if (err < 0)
1334 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 else {
1337 char *name;
1338 unsigned long flags = 0;
1339
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001340 if (!capable(CAP_NET_ADMIN))
1341 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001342 err = security_tun_dev_create();
1343 if (err < 0)
1344 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 /* Set dev type */
1347 if (ifr->ifr_flags & IFF_TUN) {
1348 /* TUN device */
1349 flags |= TUN_TUN_DEV;
1350 name = "tun%d";
1351 } else if (ifr->ifr_flags & IFF_TAP) {
1352 /* TAP device */
1353 flags |= TUN_TAP_DEV;
1354 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001355 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001356 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (*ifr->ifr_name)
1359 name = ifr->ifr_name;
1360
Jason Wangc8d68e62012-10-31 19:46:00 +00001361 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1362 tun_setup,
1363 MAX_TAP_QUEUES, MAX_TAP_QUEUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (!dev)
1365 return -ENOMEM;
1366
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001367 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001368 dev->rtnl_link_ops = &tun_link_ops;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 tun = netdev_priv(dev);
1371 tun->dev = dev;
1372 tun->flags = flags;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001373 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001374 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Jason Wang54f968d2012-10-31 19:45:57 +00001376 tun->filter_attached = false;
1377 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001378
Jason Wang54f968d2012-10-31 19:45:57 +00001379 security_tun_dev_post_create(&tfile->sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 tun_net_init(dev);
1382
Michał Mirosław88255372011-04-19 06:13:10 +00001383 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1384 TUN_USER_FEATURES;
1385 dev->features = dev->hw_features;
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 err = register_netdevice(tun->dev);
1388 if (err < 0)
Jason Wang54f968d2012-10-31 19:45:57 +00001389 goto err_free_dev;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001390
David Woodhouse980c9e82009-05-09 22:54:21 -07001391 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1392 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1393 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001394 pr_err("Failed to create tun sysfs files\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001395
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001396 err = tun_attach(tun, file);
1397 if (err < 0)
Jason Wangc8d68e62012-10-31 19:46:00 +00001398 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Joe Perches6b8a66e2011-03-02 07:18:10 +00001401 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if (ifr->ifr_flags & IFF_NO_PI)
1404 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001405 else
1406 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1409 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001410 else
1411 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Rusty Russellf43798c2008-07-03 03:48:02 -07001413 if (ifr->ifr_flags & IFF_VNET_HDR)
1414 tun->flags |= TUN_VNET_HDR;
1415 else
1416 tun->flags &= ~TUN_VNET_HDR;
1417
Jason Wangc8d68e62012-10-31 19:46:00 +00001418 if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1419 tun->flags |= TUN_TAP_MQ;
1420 else
1421 tun->flags &= ~TUN_TAP_MQ;
1422
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001423 /* Make sure persistent devices do not get stuck in
1424 * xoff state.
1425 */
1426 if (netif_running(tun->dev))
Jason Wangc8d68e62012-10-31 19:46:00 +00001427 netif_tx_wake_all_queues(tun->dev);
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 strcpy(ifr->ifr_name, tun->dev->name);
1430 return 0;
1431
1432 err_free_dev:
1433 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return err;
1435}
1436
Herbert Xu876bfd42009-08-06 14:22:44 +00001437static int tun_get_iff(struct net *net, struct tun_struct *tun,
1438 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001439{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001440 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001441
1442 strcpy(ifr->ifr_name, tun->dev->name);
1443
David Woodhouse980c9e82009-05-09 22:54:21 -07001444 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001445
1446 return 0;
1447}
1448
Rusty Russell5228ddc2008-07-03 03:46:16 -07001449/* This is like a cut-down ethtool ops, except done via tun fd so no
1450 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001451static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001452{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001453 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001454
1455 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001456 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001457 arg &= ~TUN_F_CSUM;
1458
1459 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1460 if (arg & TUN_F_TSO_ECN) {
1461 features |= NETIF_F_TSO_ECN;
1462 arg &= ~TUN_F_TSO_ECN;
1463 }
1464 if (arg & TUN_F_TSO4)
1465 features |= NETIF_F_TSO;
1466 if (arg & TUN_F_TSO6)
1467 features |= NETIF_F_TSO6;
1468 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1469 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001470
1471 if (arg & TUN_F_UFO) {
1472 features |= NETIF_F_UFO;
1473 arg &= ~TUN_F_UFO;
1474 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001475 }
1476
1477 /* This gives the user a way to test for new features in future by
1478 * trying to set them. */
1479 if (arg)
1480 return -EINVAL;
1481
Michał Mirosław88255372011-04-19 06:13:10 +00001482 tun->set_features = features;
1483 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001484
1485 return 0;
1486}
1487
Jason Wangc8d68e62012-10-31 19:46:00 +00001488static void tun_detach_filter(struct tun_struct *tun, int n)
1489{
1490 int i;
1491 struct tun_file *tfile;
1492
1493 for (i = 0; i < n; i++) {
1494 tfile = rcu_dereference_protected(tun->tfiles[i],
1495 lockdep_rtnl_is_held());
1496 sk_detach_filter(tfile->socket.sk);
1497 }
1498
1499 tun->filter_attached = false;
1500}
1501
1502static int tun_attach_filter(struct tun_struct *tun)
1503{
1504 int i, ret = 0;
1505 struct tun_file *tfile;
1506
1507 for (i = 0; i < tun->numqueues; i++) {
1508 tfile = rcu_dereference_protected(tun->tfiles[i],
1509 lockdep_rtnl_is_held());
1510 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1511 if (ret) {
1512 tun_detach_filter(tun, i);
1513 return ret;
1514 }
1515 }
1516
1517 tun->filter_attached = true;
1518 return ret;
1519}
1520
1521static void tun_set_sndbuf(struct tun_struct *tun)
1522{
1523 struct tun_file *tfile;
1524 int i;
1525
1526 for (i = 0; i < tun->numqueues; i++) {
1527 tfile = rcu_dereference_protected(tun->tfiles[i],
1528 lockdep_rtnl_is_held());
1529 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1530 }
1531}
1532
Arnd Bergmann50857e22009-11-06 22:52:32 -08001533static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1534 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001536 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001537 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 void __user* argp = (void __user*)arg;
1539 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001540 kuid_t owner;
1541 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001542 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001543 int vnet_hdr_sz;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001544 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Mathias Krausea117dac2012-07-29 19:45:14 +00001546 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001547 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001549 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001550 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001551 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001552 if (cmd == TUNGETFEATURES) {
1553 /* Currently this just means: "what IFF flags are valid?".
1554 * This is needed because we never checked for invalid flags on
1555 * TUNSETIFF. */
1556 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1557 IFF_VNET_HDR,
1558 (unsigned int __user*)argp);
1559 }
1560
Jason Wangc8d68e62012-10-31 19:46:00 +00001561 ret = 0;
Herbert Xu876bfd42009-08-06 14:22:44 +00001562 rtnl_lock();
1563
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001564 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1567
Herbert Xu876bfd42009-08-06 14:22:44 +00001568 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Herbert Xu876bfd42009-08-06 14:22:44 +00001570 if (ret)
1571 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Arnd Bergmann50857e22009-11-06 22:52:32 -08001573 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001574 ret = -EFAULT;
1575 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Herbert Xu876bfd42009-08-06 14:22:44 +00001578 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001580 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Jason Wang1e588332012-10-31 19:45:56 +00001582 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Eric W. Biederman631ab462009-01-20 11:00:40 +00001584 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001586 case TUNGETIFF:
Herbert Xu876bfd42009-08-06 14:22:44 +00001587 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001588 if (ret)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001589 break;
Mark McLoughline3b99552008-08-15 15:09:56 -07001590
Arnd Bergmann50857e22009-11-06 22:52:32 -08001591 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001592 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001593 break;
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 case TUNSETNOCSUM:
1596 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Michał Mirosław88255372011-04-19 06:13:10 +00001598 /* [unimplemented] */
1599 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001600 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602
1603 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001604 /* Disable/Enable persist mode. Keep an extra reference to the
1605 * module to prevent the module being unprobed.
1606 */
1607 if (arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001609 __module_get(THIS_MODULE);
1610 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001612 module_put(THIS_MODULE);
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Joe Perches6b8a66e2011-03-02 07:18:10 +00001615 tun_debug(KERN_INFO, tun, "persist %s\n",
1616 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 break;
1618
1619 case TUNSETOWNER:
1620 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001621 owner = make_kuid(current_user_ns(), arg);
1622 if (!uid_valid(owner)) {
1623 ret = -EINVAL;
1624 break;
1625 }
1626 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001627 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001628 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 break;
1630
Guido Guenther8c644622007-07-02 22:50:25 -07001631 case TUNSETGROUP:
1632 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001633 group = make_kgid(current_user_ns(), arg);
1634 if (!gid_valid(group)) {
1635 ret = -EINVAL;
1636 break;
1637 }
1638 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001639 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001640 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001641 break;
1642
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001643 case TUNSETLINK:
1644 /* Only allow setting the type when the interface is down */
1645 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001646 tun_debug(KERN_INFO, tun,
1647 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001648 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001649 } else {
1650 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001651 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1652 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001653 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001654 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001655 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657#ifdef TUN_DEBUG
1658 case TUNSETDEBUG:
1659 tun->debug = arg;
1660 break;
1661#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001662 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001663 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001664 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001665
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001666 case TUNSETTXFILTER:
1667 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001668 ret = -EINVAL;
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001669 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001670 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001671 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001675 /* Get hw address */
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001676 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1677 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001678 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001679 ret = -EFAULT;
1680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2c2008-07-14 22:18:19 -07001683 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00001684 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1685 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001686
Kim B. Heino40102372008-02-29 12:26:21 -08001687 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001688 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001689
1690 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00001691 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001692 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1693 ret = -EFAULT;
1694 break;
1695
1696 case TUNSETSNDBUF:
1697 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1698 ret = -EFAULT;
1699 break;
1700 }
1701
Jason Wangc8d68e62012-10-31 19:46:00 +00001702 tun->sndbuf = sndbuf;
1703 tun_set_sndbuf(tun);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001704 break;
1705
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001706 case TUNGETVNETHDRSZ:
1707 vnet_hdr_sz = tun->vnet_hdr_sz;
1708 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1709 ret = -EFAULT;
1710 break;
1711
1712 case TUNSETVNETHDRSZ:
1713 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1714 ret = -EFAULT;
1715 break;
1716 }
1717 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1718 ret = -EINVAL;
1719 break;
1720 }
1721
1722 tun->vnet_hdr_sz = vnet_hdr_sz;
1723 break;
1724
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001725 case TUNATTACHFILTER:
1726 /* Can be set only for TAPs */
1727 ret = -EINVAL;
1728 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1729 break;
1730 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00001731 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001732 break;
1733
Jason Wangc8d68e62012-10-31 19:46:00 +00001734 ret = tun_attach_filter(tun);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001735 break;
1736
1737 case TUNDETACHFILTER:
1738 /* Can be set only for TAPs */
1739 ret = -EINVAL;
1740 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1741 break;
Jason Wangc8d68e62012-10-31 19:46:00 +00001742 ret = 0;
1743 tun_detach_filter(tun, tun->numqueues);
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001744 break;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00001747 ret = -EINVAL;
1748 break;
Joe Perchesee289b62010-05-17 22:47:34 -07001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Herbert Xu876bfd42009-08-06 14:22:44 +00001751unlock:
1752 rtnl_unlock();
1753 if (tun)
1754 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Arnd Bergmann50857e22009-11-06 22:52:32 -08001758static long tun_chr_ioctl(struct file *file,
1759 unsigned int cmd, unsigned long arg)
1760{
1761 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1762}
1763
1764#ifdef CONFIG_COMPAT
1765static long tun_chr_compat_ioctl(struct file *file,
1766 unsigned int cmd, unsigned long arg)
1767{
1768 switch (cmd) {
1769 case TUNSETIFF:
1770 case TUNGETIFF:
1771 case TUNSETTXFILTER:
1772 case TUNGETSNDBUF:
1773 case TUNSETSNDBUF:
1774 case SIOCGIFHWADDR:
1775 case SIOCSIFHWADDR:
1776 arg = (unsigned long)compat_ptr(arg);
1777 break;
1778 default:
1779 arg = (compat_ulong_t)arg;
1780 break;
1781 }
1782
1783 /*
1784 * compat_ifreq is shorter than ifreq, so we must not access beyond
1785 * the end of that structure. All fields that are used in this
1786 * driver are compatible though, we don't need to convert the
1787 * contents.
1788 */
1789 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1790}
1791#endif /* CONFIG_COMPAT */
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793static int tun_chr_fasync(int fd, struct file *file, int on)
1794{
Jason Wang54f968d2012-10-31 19:45:57 +00001795 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 int ret;
1797
Jason Wang54f968d2012-10-31 19:45:57 +00001798 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001799 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001802 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001804 goto out;
Jason Wang54f968d2012-10-31 19:45:57 +00001805 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001806 } else
Jason Wang54f968d2012-10-31 19:45:57 +00001807 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06001808 ret = 0;
1809out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06001810 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
1813static int tun_chr_open(struct inode *inode, struct file * file)
1814{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001815 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07001816
Joe Perches6b8a66e2011-03-02 07:18:10 +00001817 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00001818
Jason Wang54f968d2012-10-31 19:45:57 +00001819 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1820 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001821 if (!tfile)
1822 return -ENOMEM;
Jason Wang6e914fc2012-10-31 19:45:58 +00001823 rcu_assign_pointer(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001824 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00001825 tfile->flags = 0;
1826
1827 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1828 init_waitqueue_head(&tfile->wq.wait);
1829
1830 tfile->socket.file = file;
1831 tfile->socket.ops = &tun_socket_ops;
1832
1833 sock_init_data(&tfile->socket, &tfile->sk);
1834 sk_change_net(&tfile->sk, tfile->net);
1835
1836 tfile->sk.sk_write_space = tun_sock_write_space;
1837 tfile->sk.sk_sndbuf = INT_MAX;
1838
Eric W. Biederman631ab462009-01-20 11:00:40 +00001839 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00001840 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 0;
1843}
1844
1845static int tun_chr_close(struct inode *inode, struct file *file)
1846{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001847 struct tun_file *tfile = file->private_data;
Jason Wang54f968d2012-10-31 19:45:57 +00001848 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Jason Wangc8d68e62012-10-31 19:46:00 +00001850 tun_detach(tfile, true);
Jason Wang54f968d2012-10-31 19:45:57 +00001851 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 return 0;
1854}
1855
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001856static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001857 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001859 .read = do_sync_read,
1860 .aio_read = tun_chr_aio_read,
1861 .write = do_sync_write,
1862 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08001864 .unlocked_ioctl = tun_chr_ioctl,
1865#ifdef CONFIG_COMPAT
1866 .compat_ioctl = tun_chr_compat_ioctl,
1867#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 .open = tun_chr_open,
1869 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001870 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871};
1872
1873static struct miscdevice tun_miscdev = {
1874 .minor = TUN_MINOR,
1875 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02001876 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878};
1879
1880/* ethtool interface */
1881
1882static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1883{
1884 cmd->supported = 0;
1885 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00001886 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 cmd->duplex = DUPLEX_FULL;
1888 cmd->port = PORT_TP;
1889 cmd->phy_address = 0;
1890 cmd->transceiver = XCVR_INTERNAL;
1891 cmd->autoneg = AUTONEG_DISABLE;
1892 cmd->maxtxpkt = 0;
1893 cmd->maxrxpkt = 0;
1894 return 0;
1895}
1896
1897static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1898{
1899 struct tun_struct *tun = netdev_priv(dev);
1900
Rick Jones33a5ba12011-11-15 14:59:53 +00001901 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1902 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 switch (tun->flags & TUN_TYPE_MASK) {
1905 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00001906 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 break;
1908 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00001909 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 break;
1911 }
1912}
1913
1914static u32 tun_get_msglevel(struct net_device *dev)
1915{
1916#ifdef TUN_DEBUG
1917 struct tun_struct *tun = netdev_priv(dev);
1918 return tun->debug;
1919#else
1920 return -EOPNOTSUPP;
1921#endif
1922}
1923
1924static void tun_set_msglevel(struct net_device *dev, u32 value)
1925{
1926#ifdef TUN_DEBUG
1927 struct tun_struct *tun = netdev_priv(dev);
1928 tun->debug = value;
1929#endif
1930}
1931
Jeff Garzik7282d492006-09-13 14:30:00 -04001932static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 .get_settings = tun_get_settings,
1934 .get_drvinfo = tun_get_drvinfo,
1935 .get_msglevel = tun_get_msglevel,
1936 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00001937 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938};
1939
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941static int __init tun_init(void)
1942{
1943 int ret = 0;
1944
Joe Perches6b8a66e2011-03-02 07:18:10 +00001945 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1946 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001948 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001949 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001950 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001951 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001952 }
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001955 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001956 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001957 goto err_misc;
1958 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001959 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001960err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001961 rtnl_link_unregister(&tun_link_ops);
1962err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return ret;
1964}
1965
1966static void tun_cleanup(void)
1967{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001968 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001969 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001972/* Get an underlying socket object from tun file. Returns error unless file is
1973 * attached to a device. The returned object works like a packet socket, it
1974 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1975 * holding a reference to the file for as long as the socket is in use. */
1976struct socket *tun_get_socket(struct file *file)
1977{
Jason Wang6e914fc2012-10-31 19:45:58 +00001978 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001979 if (file->f_op != &tun_fops)
1980 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00001981 tfile = file->private_data;
1982 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001983 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00001984 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001985}
1986EXPORT_SYMBOL_GPL(tun_get_socket);
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988module_init(tun_init);
1989module_exit(tun_cleanup);
1990MODULE_DESCRIPTION(DRV_DESCRIPTION);
1991MODULE_AUTHOR(DRV_COPYRIGHT);
1992MODULE_LICENSE("GPL");
1993MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02001994MODULE_ALIAS("devname:net/tun");