blob: d52ad2438e261c3351d8d0d5c81c7b4d84a78e6f [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 Krasnyanskyf271b2cc2008-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 Wang54f968d2012-10-31 19:45:57 +0000112/* A tun_file connects an open character device to a tuntap netdevice. It
113 * also contains all socket related strctures (except sock_fprog and tap_filter)
114 * to serve as one transmit queue for tuntap device. The sock_fprog and
115 * tap_filter were kept in tun_struct since they were used for filtering for the
116 * netdevice not for a specific queue (at least I didn't see the reqirement for
117 * this).
118 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000119struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000120 struct sock sk;
121 struct socket socket;
122 struct socket_wq wq;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000123 atomic_t count;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000124 struct tun_struct *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000125 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000126 struct fasync_struct *fasync;
127 /* only used for fasnyc */
128 unsigned int flags;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000129};
130
Jason Wang54f968d2012-10-31 19:45:57 +0000131/* Since the socket were moved to tun_file, to preserve the behavior of persist
132 * device, socket fileter, sndbuf and vnet header size were restore when the
133 * file were attached to a persist device.
134 */
Rusty Russell14daa022008-04-12 18:48:58 -0700135struct tun_struct {
Eric W. Biederman631ab462009-01-20 11:00:40 +0000136 struct tun_file *tfile;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700137 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800138 kuid_t owner;
139 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700140
Rusty Russell14daa022008-04-12 18:48:58 -0700141 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000142 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000143#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
144 NETIF_F_TSO6|NETIF_F_UFO)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200145
146 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000147 int sndbuf;
148 struct tap_filter txflt;
149 struct sock_fprog fprog;
150 /* protected by rtnl lock */
151 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700152#ifdef TUN_DEBUG
153 int debug;
154#endif
155};
156
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000157static int tun_attach(struct tun_struct *tun, struct file *file)
158{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000159 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000160 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000161
162 ASSERT_RTNL();
163
Eric W. Biederman38231b72009-01-20 11:02:28 +0000164 netif_tx_lock_bh(tun->dev);
165
166 err = -EINVAL;
167 if (tfile->tun)
168 goto out;
169
170 err = -EBUSY;
171 if (tun->tfile)
172 goto out;
173
174 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000175
176 /* Re-attach filter when attaching to a persist device */
177 if (tun->filter_attached == true) {
178 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
179 if (!err)
180 goto out;
181 }
Eric W. Biederman631ab462009-01-20 11:00:40 +0000182 tfile->tun = tun;
Jason Wang54f968d2012-10-31 19:45:57 +0000183 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000184 tun->tfile = tfile;
Nolan Leakebee31362010-07-27 13:53:43 +0000185 netif_carrier_on(tun->dev);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000186 dev_hold(tun->dev);
Jason Wang54f968d2012-10-31 19:45:57 +0000187 sock_hold(&tfile->sk);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000188 atomic_inc(&tfile->count);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000189
Eric W. Biederman38231b72009-01-20 11:02:28 +0000190out:
191 netif_tx_unlock_bh(tun->dev);
192 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000193}
194
Eric W. Biederman631ab462009-01-20 11:00:40 +0000195static void __tun_detach(struct tun_struct *tun)
196{
Jason Wang54f968d2012-10-31 19:45:57 +0000197 struct tun_file *tfile = tun->tfile;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000198 /* Detach from net device */
Eric W. Biederman38231b72009-01-20 11:02:28 +0000199 netif_tx_lock_bh(tun->dev);
Nolan Leakebee31362010-07-27 13:53:43 +0000200 netif_carrier_off(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000201 tun->tfile = NULL;
Jason Wang54f968d2012-10-31 19:45:57 +0000202 tfile->tun = NULL;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000203 netif_tx_unlock_bh(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000204
205 /* Drop read queue */
Jason Wang54f968d2012-10-31 19:45:57 +0000206 skb_queue_purge(&tfile->socket.sk->sk_receive_queue);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000207
208 /* Drop the extra count on the net device */
209 dev_put(tun->dev);
210}
211
212static void tun_detach(struct tun_struct *tun)
213{
214 rtnl_lock();
215 __tun_detach(tun);
216 rtnl_unlock();
Eric W. Biederman631ab462009-01-20 11:00:40 +0000217}
218
219static struct tun_struct *__tun_get(struct tun_file *tfile)
220{
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000221 struct tun_struct *tun = NULL;
222
223 if (atomic_inc_not_zero(&tfile->count))
224 tun = tfile->tun;
225
226 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000227}
228
229static struct tun_struct *tun_get(struct file *file)
230{
231 return __tun_get(file->private_data);
232}
233
234static void tun_put(struct tun_struct *tun)
235{
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000236 struct tun_file *tfile = tun->tfile;
237
238 if (atomic_dec_and_test(&tfile->count))
239 tun_detach(tfile->tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000240}
241
Joe Perches6b8a66e2011-03-02 07:18:10 +0000242/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700243static void addr_hash_set(u32 *mask, const u8 *addr)
244{
245 int n = ether_crc(ETH_ALEN, addr) >> 26;
246 mask[n >> 5] |= (1 << (n & 31));
247}
248
249static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
250{
251 int n = ether_crc(ETH_ALEN, addr) >> 26;
252 return mask[n >> 5] & (1 << (n & 31));
253}
254
255static int update_filter(struct tap_filter *filter, void __user *arg)
256{
257 struct { u8 u[ETH_ALEN]; } *addr;
258 struct tun_filter uf;
259 int err, alen, n, nexact;
260
261 if (copy_from_user(&uf, arg, sizeof(uf)))
262 return -EFAULT;
263
264 if (!uf.count) {
265 /* Disabled */
266 filter->count = 0;
267 return 0;
268 }
269
270 alen = ETH_ALEN * uf.count;
271 addr = kmalloc(alen, GFP_KERNEL);
272 if (!addr)
273 return -ENOMEM;
274
275 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
276 err = -EFAULT;
277 goto done;
278 }
279
280 /* The filter is updated without holding any locks. Which is
281 * perfectly safe. We disable it first and in the worst
282 * case we'll accept a few undesired packets. */
283 filter->count = 0;
284 wmb();
285
286 /* Use first set of addresses as an exact filter */
287 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
288 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
289
290 nexact = n;
291
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800292 /* Remaining multicast addresses are hashed,
293 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700294 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800295 for (; n < uf.count; n++) {
296 if (!is_multicast_ether_addr(addr[n].u)) {
297 err = 0; /* no filter */
298 goto done;
299 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700300 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800301 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700302
303 /* For ALLMULTI just set the mask to all ones.
304 * This overrides the mask populated above. */
305 if ((uf.flags & TUN_FLT_ALLMULTI))
306 memset(filter->mask, ~0, sizeof(filter->mask));
307
308 /* Now enable the filter */
309 wmb();
310 filter->count = nexact;
311
312 /* Return the number of exact filters */
313 err = nexact;
314
315done:
316 kfree(addr);
317 return err;
318}
319
320/* Returns: 0 - drop, !=0 - accept */
321static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
322{
323 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
324 * at this point. */
325 struct ethhdr *eh = (struct ethhdr *) skb->data;
326 int i;
327
328 /* Exact match */
329 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000330 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700331 return 1;
332
333 /* Inexact match (multicast only) */
334 if (is_multicast_ether_addr(eh->h_dest))
335 return addr_hash_test(filter->mask, eh->h_dest);
336
337 return 0;
338}
339
340/*
341 * Checks whether the packet is accepted or not.
342 * Returns: 0 - drop, !=0 - accept
343 */
344static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
345{
346 if (!filter->count)
347 return 1;
348
349 return run_filter(filter, skb);
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* Network device part of the driver */
353
Jeff Garzik7282d492006-09-13 14:30:00 -0400354static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000356/* Net device detach from fd. */
357static void tun_net_uninit(struct net_device *dev)
358{
359 struct tun_struct *tun = netdev_priv(dev);
360 struct tun_file *tfile = tun->tfile;
361
362 /* Inform the methods they need to stop using the dev.
363 */
364 if (tfile) {
Jason Wang54f968d2012-10-31 19:45:57 +0000365 wake_up_all(&tfile->wq.wait);
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000366 if (atomic_dec_and_test(&tfile->count))
367 __tun_detach(tun);
368 }
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/* Net device open. */
372static int tun_net_open(struct net_device *dev)
373{
374 netif_start_queue(dev);
375 return 0;
376}
377
378/* Net device close. */
379static int tun_net_close(struct net_device *dev)
380{
381 netif_stop_queue(dev);
382 return 0;
383}
384
385/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000386static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct tun_struct *tun = netdev_priv(dev);
Jason Wang54f968d2012-10-31 19:45:57 +0000389 struct tun_file *tfile = tun->tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Joe Perches6b8a66e2011-03-02 07:18:10 +0000391 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* Drop packet if interface is not attached */
Jason Wang54f968d2012-10-31 19:45:57 +0000394 if (!tfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto drop;
396
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700397 /* Drop if the filter does not like it.
398 * This is a noop if the filter is disabled.
399 * Filter can be enabled only for the TAP devices. */
400 if (!check_filter(&tun->txflt, skb))
401 goto drop;
402
Jason Wang54f968d2012-10-31 19:45:57 +0000403 if (tfile->socket.sk->sk_filter &&
404 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000405 goto drop;
406
Jason Wang54f968d2012-10-31 19:45:57 +0000407 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
408 >= dev->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!(tun->flags & TUN_ONE_QUEUE)) {
410 /* Normal queueing mode. */
411 /* Packet scheduler handles dropping of further packets. */
412 netif_stop_queue(dev);
413
414 /* We won't see all dropped packets individually, so overrun
415 * error is more appropriate. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700416 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 } else {
418 /* Single queue mode.
419 * Driver handles dropping of all packets itself. */
420 goto drop;
421 }
422 }
423
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000424 /* Orphan the skb - required as we might hang on to it
425 * for indefinite time. */
Michael S. Tsirkin868eefe2012-07-20 09:23:14 +0000426 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
427 goto drop;
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000428 skb_orphan(skb);
429
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700430 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000431 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000434 if (tfile->flags & TUN_FASYNC)
435 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
436 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000437 POLLRDNORM | POLLRDBAND);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000438 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700441 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000443 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700446static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700448 /*
449 * This callback is supposed to deal with mc filter in
450 * _rx_ path and has nothing to do with the _tx_ path.
451 * In rx path we always accept everything userspace gives us.
452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Ed Swierk4885a502007-09-16 12:21:38 -0700455#define MIN_MTU 68
456#define MAX_MTU 65535
457
458static int
459tun_net_change_mtu(struct net_device *dev, int new_mtu)
460{
461 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
462 return -EINVAL;
463 dev->mtu = new_mtu;
464 return 0;
465}
466
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000467static netdev_features_t tun_net_fix_features(struct net_device *dev,
468 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000469{
470 struct tun_struct *tun = netdev_priv(dev);
471
472 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
473}
Neil Hormanbebd0972011-06-15 05:25:01 +0000474#ifdef CONFIG_NET_POLL_CONTROLLER
475static void tun_poll_controller(struct net_device *dev)
476{
477 /*
478 * Tun only receives frames when:
479 * 1) the char device endpoint gets data from user space
480 * 2) the tun socket gets a sendmsg call from user space
481 * Since both of those are syncronous operations, we are guaranteed
482 * never to have pending data when we poll for it
483 * so theres nothing to do here but return.
484 * We need this though so netpoll recognizes us as an interface that
485 * supports polling, which enables bridge devices in virt setups to
486 * still use netconsole
487 */
488 return;
489}
490#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800491static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000492 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800493 .ndo_open = tun_net_open,
494 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800495 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800496 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000497 .ndo_fix_features = tun_net_fix_features,
Neil Hormanbebd0972011-06-15 05:25:01 +0000498#ifdef CONFIG_NET_POLL_CONTROLLER
499 .ndo_poll_controller = tun_poll_controller,
500#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800501};
502
503static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000504 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800505 .ndo_open = tun_net_open,
506 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800507 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800508 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000509 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000510 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800511 .ndo_set_mac_address = eth_mac_addr,
512 .ndo_validate_addr = eth_validate_addr,
Neil Hormanbebd0972011-06-15 05:25:01 +0000513#ifdef CONFIG_NET_POLL_CONTROLLER
514 .ndo_poll_controller = tun_poll_controller,
515#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800516};
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/* Initialize net device. */
519static void tun_net_init(struct net_device *dev)
520{
521 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 switch (tun->flags & TUN_TYPE_MASK) {
524 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800525 dev->netdev_ops = &tun_netdev_ops;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Point-to-Point TUN Device */
528 dev->hard_header_len = 0;
529 dev->addr_len = 0;
530 dev->mtu = 1500;
531
532 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400533 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
535 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
536 break;
537
538 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800539 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700541 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000542 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000544 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
547 break;
548 }
549}
550
551/* Character device part */
552
553/* Poll */
554static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400555{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000556 struct tun_file *tfile = file->private_data;
557 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000558 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800559 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000562 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jason Wang54f968d2012-10-31 19:45:57 +0000564 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000565
Joe Perches6b8a66e2011-03-02 07:18:10 +0000566 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jason Wang54f968d2012-10-31 19:45:57 +0000568 poll_wait(file, &tfile->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400569
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000570 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 mask |= POLLIN | POLLRDNORM;
572
Herbert Xu33dccbb2009-02-05 21:25:32 -0800573 if (sock_writeable(sk) ||
574 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
575 sock_writeable(sk)))
576 mask |= POLLOUT | POLLWRNORM;
577
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000578 if (tun->dev->reg_state != NETREG_REGISTERED)
579 mask = POLLERR;
580
Eric W. Biederman631ab462009-01-20 11:00:40 +0000581 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return mask;
583}
584
Rusty Russellf42157c2008-08-15 15:15:10 -0700585/* prepad is the amount to reserve at front. len is length after that.
586 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000587static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000588 size_t prepad, size_t len,
589 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700590{
Jason Wang54f968d2012-10-31 19:45:57 +0000591 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700592 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800593 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700594
595 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700596 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800597 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700598
Herbert Xu33dccbb2009-02-05 21:25:32 -0800599 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
600 &err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700601 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800602 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700603
604 skb_reserve(skb, prepad);
605 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800606 skb->data_len = len - linear;
607 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700608
609 return skb;
610}
611
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000612/* set skb frags from iovec, this can move to core network code for reuse */
613static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
614 int offset, size_t count)
615{
616 int len = iov_length(from, count) - offset;
617 int copy = skb_headlen(skb);
618 int size, offset1 = 0;
619 int i = 0;
620
621 /* Skip over from offset */
622 while (count && (offset >= from->iov_len)) {
623 offset -= from->iov_len;
624 ++from;
625 --count;
626 }
627
628 /* copy up to skb headlen */
629 while (count && (copy > 0)) {
630 size = min_t(unsigned int, copy, from->iov_len - offset);
631 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
632 size))
633 return -EFAULT;
634 if (copy > size) {
635 ++from;
636 --count;
637 offset = 0;
638 } else
639 offset += size;
640 copy -= size;
641 offset1 += size;
642 }
643
644 if (len == offset1)
645 return 0;
646
647 while (count--) {
648 struct page *page[MAX_SKB_FRAGS];
649 int num_pages;
650 unsigned long base;
651 unsigned long truesize;
652
653 len = from->iov_len - offset;
654 if (!len) {
655 offset = 0;
656 ++from;
657 continue;
658 }
659 base = (unsigned long)from->iov_base + offset;
660 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
661 if (i + size > MAX_SKB_FRAGS)
662 return -EMSGSIZE;
663 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
664 if (num_pages != size) {
665 for (i = 0; i < num_pages; i++)
666 put_page(page[i]);
667 return -EFAULT;
668 }
669 truesize = size * PAGE_SIZE;
670 skb->data_len += len;
671 skb->len += len;
672 skb->truesize += truesize;
673 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
674 while (len) {
675 int off = base & ~PAGE_MASK;
676 int size = min_t(int, len, PAGE_SIZE - off);
677 __skb_fill_page_desc(skb, i, page[i], off, size);
678 skb_shinfo(skb)->nr_frags++;
679 /* increase sk_wmem_alloc */
680 base += size;
681 len -= size;
682 i++;
683 }
684 offset = 0;
685 ++from;
686 }
687 return 0;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +0000691static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
692 void *msg_control, const struct iovec *iv,
693 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Harvey Harrison09640e632009-02-01 00:45:17 -0800695 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct sk_buff *skb;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000697 size_t len = total_len, align = NET_SKB_PAD;
Rusty Russellf43798c2008-07-03 03:48:02 -0700698 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000699 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000700 int copylen;
701 bool zerocopy = false;
702 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (!(tun->flags & TUN_NO_PI)) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000705 if ((len -= sizeof(pi)) > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -EINVAL;
707
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000708 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000710 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
Rusty Russellf43798c2008-07-03 03:48:02 -0700713 if (tun->flags & TUN_VNET_HDR) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000714 if ((len -= tun->vnet_hdr_sz) > total_len)
Rusty Russellf43798c2008-07-03 03:48:02 -0700715 return -EINVAL;
716
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000717 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -0700718 return -EFAULT;
719
Herbert Xu49091222009-06-08 00:20:01 -0700720 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
721 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
722 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
723
Rusty Russellf43798c2008-07-03 03:48:02 -0700724 if (gso.hdr_len > len)
725 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200726 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700727 }
728
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700729 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +0000730 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -0700731 if (unlikely(len < ETH_HLEN ||
732 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700733 return -EINVAL;
734 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400735
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000736 if (msg_control)
737 zerocopy = true;
738
739 if (zerocopy) {
740 /* Userspace may produce vectors with count greater than
741 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
742 * to let the rest of data to be fit in the frags.
743 */
744 if (count > MAX_SKB_FRAGS) {
745 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
746 if (copylen < offset)
747 copylen = 0;
748 else
749 copylen -= offset;
750 } else
751 copylen = 0;
752 /* There are 256 bytes to be copied in skb, so there is enough
753 * room for skb expand head in case it is used.
754 * The rest of the buffer is mapped from userspace.
755 */
756 if (copylen < gso.hdr_len)
757 copylen = gso.hdr_len;
758 if (!copylen)
759 copylen = GOODCOPY_LEN;
760 } else
761 copylen = len;
762
Jason Wang54f968d2012-10-31 19:45:57 +0000763 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800764 if (IS_ERR(skb)) {
765 if (PTR_ERR(skb) != -EAGAIN)
766 tun->dev->stats.rx_dropped++;
767 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000770 if (zerocopy)
771 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
772 else
773 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
774
775 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700776 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -0800777 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -0800779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Rusty Russellf43798c2008-07-03 03:48:02 -0700781 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
782 if (!skb_partial_csum_set(skb, gso.csum_start,
783 gso.csum_offset)) {
784 tun->dev->stats.rx_frame_errors++;
785 kfree_skb(skb);
786 return -EINVAL;
787 }
Michał Mirosław88255372011-04-19 06:13:10 +0000788 }
Rusty Russellf43798c2008-07-03 03:48:02 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 switch (tun->flags & TUN_TYPE_MASK) {
791 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -0700792 if (tun->flags & TUN_NO_PI) {
793 switch (skb->data[0] & 0xf0) {
794 case 0x40:
795 pi.proto = htons(ETH_P_IP);
796 break;
797 case 0x60:
798 pi.proto = htons(ETH_P_IPV6);
799 break;
800 default:
801 tun->dev->stats.rx_dropped++;
802 kfree_skb(skb);
803 return -EINVAL;
804 }
805 }
806
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700807 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700809 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
811 case TUN_TAP_DEV:
812 skb->protocol = eth_type_trans(skb, tun->dev);
813 break;
Joe Perches6403eab2011-06-03 11:51:20 +0000814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Rusty Russellf43798c2008-07-03 03:48:02 -0700816 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
817 pr_debug("GSO!\n");
818 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
819 case VIRTIO_NET_HDR_GSO_TCPV4:
820 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
821 break;
822 case VIRTIO_NET_HDR_GSO_TCPV6:
823 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
824 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +0000825 case VIRTIO_NET_HDR_GSO_UDP:
826 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
827 break;
Rusty Russellf43798c2008-07-03 03:48:02 -0700828 default:
829 tun->dev->stats.rx_frame_errors++;
830 kfree_skb(skb);
831 return -EINVAL;
832 }
833
834 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
835 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
836
837 skb_shinfo(skb)->gso_size = gso.gso_size;
838 if (skb_shinfo(skb)->gso_size == 0) {
839 tun->dev->stats.rx_frame_errors++;
840 kfree_skb(skb);
841 return -EINVAL;
842 }
843
844 /* Header must be checked, and gso_segs computed. */
845 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
846 skb_shinfo(skb)->gso_segs = 0;
847 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400848
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000849 /* copy skb_ubuf_info for callback when skb has no error */
850 if (zerocopy) {
851 skb_shinfo(skb)->destructor_arg = msg_control;
852 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
853 }
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400856
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700857 tun->dev->stats.rx_packets++;
858 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000860 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400861}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700863static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
864 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Herbert Xu33dccbb2009-02-05 21:25:32 -0800866 struct file *file = iocb->ki_filp;
Herbert Xuab46d7792009-02-14 20:46:39 -0800867 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +0000868 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000869 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 if (!tun)
872 return -EBADFD;
873
Joe Perches6b8a66e2011-03-02 07:18:10 +0000874 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Jason Wang54f968d2012-10-31 19:45:57 +0000876 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
877 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000878
879 tun_put(tun);
880 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +0000884static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +0000885 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000886 struct sk_buff *skb,
887 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct tun_pi pi = { 0, skb->protocol };
890 ssize_t total = 0;
891
892 if (!(tun->flags & TUN_NO_PI)) {
893 if ((len -= sizeof(pi)) < 0)
894 return -EINVAL;
895
896 if (len < skb->len) {
897 /* Packet will be striped */
898 pi.flags |= TUN_PKT_STRIP;
899 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400900
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000901 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -EFAULT;
903 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Rusty Russellf43798c2008-07-03 03:48:02 -0700906 if (tun->flags & TUN_VNET_HDR) {
907 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200908 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -0700909 return -EINVAL;
910
911 if (skb_is_gso(skb)) {
912 struct skb_shared_info *sinfo = skb_shinfo(skb);
913
914 /* This is a hint as to how much should be linear. */
915 gso.hdr_len = skb_headlen(skb);
916 gso.gso_size = sinfo->gso_size;
917 if (sinfo->gso_type & SKB_GSO_TCPV4)
918 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
919 else if (sinfo->gso_type & SKB_GSO_TCPV6)
920 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +0000921 else if (sinfo->gso_type & SKB_GSO_UDP)
922 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +0000923 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +0000924 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +0000925 "0x%x, gso_size %d, hdr_len %d\n",
926 sinfo->gso_type, gso.gso_size,
927 gso.hdr_len);
928 print_hex_dump(KERN_ERR, "tun: ",
929 DUMP_PREFIX_NONE,
930 16, 1, skb->head,
931 min((int)gso.hdr_len, 64), true);
932 WARN_ON_ONCE(1);
933 return -EINVAL;
934 }
Rusty Russellf43798c2008-07-03 03:48:02 -0700935 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
936 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
937 } else
938 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
939
940 if (skb->ip_summed == CHECKSUM_PARTIAL) {
941 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +0000942 gso.csum_start = skb_checksum_start_offset(skb);
Rusty Russellf43798c2008-07-03 03:48:02 -0700943 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +0000944 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
945 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -0700946 } /* else everything is zero */
947
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000948 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
949 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -0700950 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200951 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700952 }
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 len = min_t(int, skb->len, len);
955
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000956 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000957 total += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700959 tun->dev->stats.tx_packets++;
960 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 return total;
963}
964
Jason Wang54f968d2012-10-31 19:45:57 +0000965static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000966 struct kiocb *iocb, const struct iovec *iv,
967 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 DECLARE_WAITQUEUE(wait, current);
970 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000971 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Joe Perches6b8a66e2011-03-02 07:18:10 +0000973 tun_debug(KERN_INFO, tun, "tun_chr_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Amos Kong61a5ff12011-06-09 00:27:10 -0700975 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +0000976 add_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 current->state = TASK_INTERRUPTIBLE;
979
980 /* Read frames from the queue */
Jason Wang54f968d2012-10-31 19:45:57 +0000981 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000982 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ret = -EAGAIN;
984 break;
985 }
986 if (signal_pending(current)) {
987 ret = -ERESTARTSYS;
988 break;
989 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000990 if (tun->dev->reg_state != NETREG_REGISTERED) {
991 ret = -EIO;
992 break;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /* Nothing to read, let's sleep */
996 schedule();
997 continue;
998 }
999 netif_wake_queue(tun->dev);
1000
Jason Wang54f968d2012-10-31 19:45:57 +00001001 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001002 kfree_skb(skb);
1003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
1006 current->state = TASK_RUNNING;
Amos Kong61a5ff12011-06-09 00:27:10 -07001007 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001008 remove_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001010 return ret;
1011}
1012
1013static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1014 unsigned long count, loff_t pos)
1015{
1016 struct file *file = iocb->ki_filp;
1017 struct tun_file *tfile = file->private_data;
1018 struct tun_struct *tun = __tun_get(tfile);
1019 ssize_t len, ret;
1020
1021 if (!tun)
1022 return -EBADFD;
1023 len = iov_length(iv, count);
1024 if (len < 0) {
1025 ret = -EINVAL;
1026 goto out;
1027 }
1028
Jason Wang54f968d2012-10-31 19:45:57 +00001029 ret = tun_do_read(tun, tfile, iocb, iv, len,
1030 file->f_flags & O_NONBLOCK);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001031 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001032out:
1033 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return ret;
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static void tun_setup(struct net_device *dev)
1038{
1039 struct tun_struct *tun = netdev_priv(dev);
1040
Eric W. Biederman0625c882012-02-07 16:48:55 -08001041 tun->owner = INVALID_UID;
1042 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang54f968d2012-10-31 19:45:57 +00001045 dev->destructor = free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001048/* Trivial set of netlink ops to allow deleting tun or tap
1049 * device with netlink.
1050 */
1051static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1052{
1053 return -EINVAL;
1054}
1055
1056static struct rtnl_link_ops tun_link_ops __read_mostly = {
1057 .kind = DRV_NAME,
1058 .priv_size = sizeof(struct tun_struct),
1059 .setup = tun_setup,
1060 .validate = tun_validate,
1061};
1062
Herbert Xu33dccbb2009-02-05 21:25:32 -08001063static void tun_sock_write_space(struct sock *sk)
1064{
Jason Wang54f968d2012-10-31 19:45:57 +00001065 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001066 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001067
1068 if (!sock_writeable(sk))
1069 return;
1070
Herbert Xu33dccbb2009-02-05 21:25:32 -08001071 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1072 return;
1073
Eric Dumazet43815482010-04-29 11:01:49 +00001074 wqueue = sk_sleep(sk);
1075 if (wqueue && waitqueue_active(wqueue))
1076 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001077 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001078
Jason Wang54f968d2012-10-31 19:45:57 +00001079 tfile = container_of(sk, struct tun_file, sk);
1080 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001081}
1082
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001083static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1084 struct msghdr *m, size_t total_len)
1085{
Jason Wang54f968d2012-10-31 19:45:57 +00001086 int ret;
1087 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1088 struct tun_struct *tun = __tun_get(tfile);
1089
1090 if (!tun)
1091 return -EBADFD;
1092
1093 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1094 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1095 tun_put(tun);
1096 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001097}
1098
Jason Wang54f968d2012-10-31 19:45:57 +00001099
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001100static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1101 struct msghdr *m, size_t total_len,
1102 int flags)
1103{
Jason Wang54f968d2012-10-31 19:45:57 +00001104 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1105 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001106 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001107
1108 if (!tun)
1109 return -EBADFD;
1110
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001111 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1112 return -EINVAL;
Jason Wang54f968d2012-10-31 19:45:57 +00001113 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001114 flags & MSG_DONTWAIT);
1115 if (ret > total_len) {
1116 m->msg_flags |= MSG_TRUNC;
1117 ret = flags & MSG_TRUNC ? ret : total_len;
1118 }
Jason Wang54f968d2012-10-31 19:45:57 +00001119 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001120 return ret;
1121}
1122
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001123static int tun_release(struct socket *sock)
1124{
1125 if (sock->sk)
1126 sock_put(sock->sk);
1127 return 0;
1128}
1129
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001130/* Ops structure to mimic raw sockets with tun */
1131static const struct proto_ops tun_socket_ops = {
1132 .sendmsg = tun_sendmsg,
1133 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001134 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001135};
1136
Herbert Xu33dccbb2009-02-05 21:25:32 -08001137static struct proto tun_proto = {
1138 .name = "tun",
1139 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001140 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001141};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001142
David Woodhouse980c9e82009-05-09 22:54:21 -07001143static int tun_flags(struct tun_struct *tun)
1144{
1145 int flags = 0;
1146
1147 if (tun->flags & TUN_TUN_DEV)
1148 flags |= IFF_TUN;
1149 else
1150 flags |= IFF_TAP;
1151
1152 if (tun->flags & TUN_NO_PI)
1153 flags |= IFF_NO_PI;
1154
1155 if (tun->flags & TUN_ONE_QUEUE)
1156 flags |= IFF_ONE_QUEUE;
1157
1158 if (tun->flags & TUN_VNET_HDR)
1159 flags |= IFF_VNET_HDR;
1160
1161 return flags;
1162}
1163
1164static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1165 char *buf)
1166{
1167 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1168 return sprintf(buf, "0x%x\n", tun_flags(tun));
1169}
1170
1171static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1172 char *buf)
1173{
1174 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001175 return uid_valid(tun->owner)?
1176 sprintf(buf, "%u\n",
1177 from_kuid_munged(current_user_ns(), tun->owner)):
1178 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001179}
1180
1181static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1182 char *buf)
1183{
1184 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001185 return gid_valid(tun->group) ?
1186 sprintf(buf, "%u\n",
1187 from_kgid_munged(current_user_ns(), tun->group)):
1188 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001189}
1190
1191static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1192static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1193static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1194
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001195static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001198 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 struct net_device *dev;
1200 int err;
1201
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001202 dev = __dev_get_by_name(net, ifr->ifr_name);
1203 if (dev) {
Paul Moore2b980db2009-08-28 18:12:43 -04001204 const struct cred *cred = current_cred();
1205
David Woodhousef85ba782009-04-27 03:23:54 -07001206 if (ifr->ifr_flags & IFF_TUN_EXCL)
1207 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001208 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1209 tun = netdev_priv(dev);
1210 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1211 tun = netdev_priv(dev);
1212 else
1213 return -EINVAL;
1214
Eric W. Biederman0625c882012-02-07 16:48:55 -08001215 if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
1216 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Paul Moore2b980db2009-08-28 18:12:43 -04001217 !capable(CAP_NET_ADMIN))
1218 return -EPERM;
Jason Wang54f968d2012-10-31 19:45:57 +00001219 err = security_tun_dev_attach(tfile->socket.sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001220 if (err < 0)
1221 return err;
1222
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001223 err = tun_attach(tun, file);
1224 if (err < 0)
1225 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 else {
1228 char *name;
1229 unsigned long flags = 0;
1230
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001231 if (!capable(CAP_NET_ADMIN))
1232 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001233 err = security_tun_dev_create();
1234 if (err < 0)
1235 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* Set dev type */
1238 if (ifr->ifr_flags & IFF_TUN) {
1239 /* TUN device */
1240 flags |= TUN_TUN_DEV;
1241 name = "tun%d";
1242 } else if (ifr->ifr_flags & IFF_TAP) {
1243 /* TAP device */
1244 flags |= TUN_TAP_DEV;
1245 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001246 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001247 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (*ifr->ifr_name)
1250 name = ifr->ifr_name;
1251
1252 dev = alloc_netdev(sizeof(struct tun_struct), name,
1253 tun_setup);
1254 if (!dev)
1255 return -ENOMEM;
1256
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001257 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001258 dev->rtnl_link_ops = &tun_link_ops;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 tun = netdev_priv(dev);
1261 tun->dev = dev;
1262 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001263 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001264 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Jason Wang54f968d2012-10-31 19:45:57 +00001266 tun->filter_attached = false;
1267 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001268
Jason Wang54f968d2012-10-31 19:45:57 +00001269 security_tun_dev_post_create(&tfile->sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 tun_net_init(dev);
1272
Michał Mirosław88255372011-04-19 06:13:10 +00001273 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1274 TUN_USER_FEATURES;
1275 dev->features = dev->hw_features;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 err = register_netdevice(tun->dev);
1278 if (err < 0)
Jason Wang54f968d2012-10-31 19:45:57 +00001279 goto err_free_dev;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001280
David Woodhouse980c9e82009-05-09 22:54:21 -07001281 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1282 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1283 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001284 pr_err("Failed to create tun sysfs files\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001285
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001286 err = tun_attach(tun, file);
1287 if (err < 0)
Herbert Xu9c3fea62009-04-18 14:15:52 +00001288 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Joe Perches6b8a66e2011-03-02 07:18:10 +00001291 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (ifr->ifr_flags & IFF_NO_PI)
1294 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001295 else
1296 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1299 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001300 else
1301 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Rusty Russellf43798c2008-07-03 03:48:02 -07001303 if (ifr->ifr_flags & IFF_VNET_HDR)
1304 tun->flags |= TUN_VNET_HDR;
1305 else
1306 tun->flags &= ~TUN_VNET_HDR;
1307
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001308 /* Make sure persistent devices do not get stuck in
1309 * xoff state.
1310 */
1311 if (netif_running(tun->dev))
1312 netif_wake_queue(tun->dev);
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 strcpy(ifr->ifr_name, tun->dev->name);
1315 return 0;
1316
1317 err_free_dev:
1318 free_netdev(dev);
1319 failed:
1320 return err;
1321}
1322
Herbert Xu876bfd42009-08-06 14:22:44 +00001323static int tun_get_iff(struct net *net, struct tun_struct *tun,
1324 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001325{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001326 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001327
1328 strcpy(ifr->ifr_name, tun->dev->name);
1329
David Woodhouse980c9e82009-05-09 22:54:21 -07001330 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001331
1332 return 0;
1333}
1334
Rusty Russell5228ddc2008-07-03 03:46:16 -07001335/* This is like a cut-down ethtool ops, except done via tun fd so no
1336 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001337static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001338{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001339 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001340
1341 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001342 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001343 arg &= ~TUN_F_CSUM;
1344
1345 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1346 if (arg & TUN_F_TSO_ECN) {
1347 features |= NETIF_F_TSO_ECN;
1348 arg &= ~TUN_F_TSO_ECN;
1349 }
1350 if (arg & TUN_F_TSO4)
1351 features |= NETIF_F_TSO;
1352 if (arg & TUN_F_TSO6)
1353 features |= NETIF_F_TSO6;
1354 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1355 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001356
1357 if (arg & TUN_F_UFO) {
1358 features |= NETIF_F_UFO;
1359 arg &= ~TUN_F_UFO;
1360 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001361 }
1362
1363 /* This gives the user a way to test for new features in future by
1364 * trying to set them. */
1365 if (arg)
1366 return -EINVAL;
1367
Michał Mirosław88255372011-04-19 06:13:10 +00001368 tun->set_features = features;
1369 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001370
1371 return 0;
1372}
1373
Arnd Bergmann50857e22009-11-06 22:52:32 -08001374static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1375 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001377 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001378 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 void __user* argp = (void __user*)arg;
1380 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001381 kuid_t owner;
1382 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001383 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001384 int vnet_hdr_sz;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001385 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Mathias Krausea117dac2012-07-29 19:45:14 +00001387 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001388 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001390 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001391 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001392 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001393 if (cmd == TUNGETFEATURES) {
1394 /* Currently this just means: "what IFF flags are valid?".
1395 * This is needed because we never checked for invalid flags on
1396 * TUNSETIFF. */
1397 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1398 IFF_VNET_HDR,
1399 (unsigned int __user*)argp);
1400 }
1401
Herbert Xu876bfd42009-08-06 14:22:44 +00001402 rtnl_lock();
1403
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001404 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1407
Herbert Xu876bfd42009-08-06 14:22:44 +00001408 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Herbert Xu876bfd42009-08-06 14:22:44 +00001410 if (ret)
1411 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Arnd Bergmann50857e22009-11-06 22:52:32 -08001413 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001414 ret = -EFAULT;
1415 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
Herbert Xu876bfd42009-08-06 14:22:44 +00001418 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001420 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Jason Wang1e588332012-10-31 19:45:56 +00001422 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Eric W. Biederman631ab462009-01-20 11:00:40 +00001424 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001426 case TUNGETIFF:
Herbert Xu876bfd42009-08-06 14:22:44 +00001427 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001428 if (ret)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001429 break;
Mark McLoughline3b99552008-08-15 15:09:56 -07001430
Arnd Bergmann50857e22009-11-06 22:52:32 -08001431 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001432 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001433 break;
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 case TUNSETNOCSUM:
1436 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Michał Mirosław88255372011-04-19 06:13:10 +00001438 /* [unimplemented] */
1439 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001440 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 break;
1442
1443 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001444 /* Disable/Enable persist mode. Keep an extra reference to the
1445 * module to prevent the module being unprobed.
1446 */
1447 if (arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001449 __module_get(THIS_MODULE);
1450 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001452 module_put(THIS_MODULE);
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Joe Perches6b8a66e2011-03-02 07:18:10 +00001455 tun_debug(KERN_INFO, tun, "persist %s\n",
1456 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
1458
1459 case TUNSETOWNER:
1460 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001461 owner = make_kuid(current_user_ns(), arg);
1462 if (!uid_valid(owner)) {
1463 ret = -EINVAL;
1464 break;
1465 }
1466 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001467 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001468 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 break;
1470
Guido Guenther8c644622007-07-02 22:50:25 -07001471 case TUNSETGROUP:
1472 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001473 group = make_kgid(current_user_ns(), arg);
1474 if (!gid_valid(group)) {
1475 ret = -EINVAL;
1476 break;
1477 }
1478 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001479 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001480 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001481 break;
1482
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001483 case TUNSETLINK:
1484 /* Only allow setting the type when the interface is down */
1485 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001486 tun_debug(KERN_INFO, tun,
1487 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001488 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001489 } else {
1490 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001491 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1492 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001493 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001494 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001495 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497#ifdef TUN_DEBUG
1498 case TUNSETDEBUG:
1499 tun->debug = arg;
1500 break;
1501#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001502 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001503 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001504 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001505
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001506 case TUNSETTXFILTER:
1507 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001508 ret = -EINVAL;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001509 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001510 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001511 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001515 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001516 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1517 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001518 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001519 ret = -EFAULT;
1520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001523 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00001524 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1525 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001526
Kim B. Heino40102372008-02-29 12:26:21 -08001527 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001528 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001529
1530 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00001531 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001532 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1533 ret = -EFAULT;
1534 break;
1535
1536 case TUNSETSNDBUF:
1537 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1538 ret = -EFAULT;
1539 break;
1540 }
1541
Jason Wang54f968d2012-10-31 19:45:57 +00001542 tun->sndbuf = tfile->socket.sk->sk_sndbuf = sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001543 break;
1544
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001545 case TUNGETVNETHDRSZ:
1546 vnet_hdr_sz = tun->vnet_hdr_sz;
1547 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1548 ret = -EFAULT;
1549 break;
1550
1551 case TUNSETVNETHDRSZ:
1552 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1553 ret = -EFAULT;
1554 break;
1555 }
1556 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1557 ret = -EINVAL;
1558 break;
1559 }
1560
1561 tun->vnet_hdr_sz = vnet_hdr_sz;
1562 break;
1563
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001564 case TUNATTACHFILTER:
1565 /* Can be set only for TAPs */
1566 ret = -EINVAL;
1567 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1568 break;
1569 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00001570 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001571 break;
1572
Jason Wang54f968d2012-10-31 19:45:57 +00001573 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1574 if (!ret)
1575 tun->filter_attached = true;
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001576 break;
1577
1578 case TUNDETACHFILTER:
1579 /* Can be set only for TAPs */
1580 ret = -EINVAL;
1581 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1582 break;
Jason Wang54f968d2012-10-31 19:45:57 +00001583 ret = sk_detach_filter(tfile->socket.sk);
1584 if (!ret)
1585 tun->filter_attached = false;
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001586 break;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00001589 ret = -EINVAL;
1590 break;
Joe Perchesee289b62010-05-17 22:47:34 -07001591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Herbert Xu876bfd42009-08-06 14:22:44 +00001593unlock:
1594 rtnl_unlock();
1595 if (tun)
1596 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001597 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Arnd Bergmann50857e22009-11-06 22:52:32 -08001600static long tun_chr_ioctl(struct file *file,
1601 unsigned int cmd, unsigned long arg)
1602{
1603 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1604}
1605
1606#ifdef CONFIG_COMPAT
1607static long tun_chr_compat_ioctl(struct file *file,
1608 unsigned int cmd, unsigned long arg)
1609{
1610 switch (cmd) {
1611 case TUNSETIFF:
1612 case TUNGETIFF:
1613 case TUNSETTXFILTER:
1614 case TUNGETSNDBUF:
1615 case TUNSETSNDBUF:
1616 case SIOCGIFHWADDR:
1617 case SIOCSIFHWADDR:
1618 arg = (unsigned long)compat_ptr(arg);
1619 break;
1620 default:
1621 arg = (compat_ulong_t)arg;
1622 break;
1623 }
1624
1625 /*
1626 * compat_ifreq is shorter than ifreq, so we must not access beyond
1627 * the end of that structure. All fields that are used in this
1628 * driver are compatible though, we don't need to convert the
1629 * contents.
1630 */
1631 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1632}
1633#endif /* CONFIG_COMPAT */
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635static int tun_chr_fasync(int fd, struct file *file, int on)
1636{
Jason Wang54f968d2012-10-31 19:45:57 +00001637 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int ret;
1639
Jason Wang54f968d2012-10-31 19:45:57 +00001640 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001641 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001644 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001646 goto out;
Jason Wang54f968d2012-10-31 19:45:57 +00001647 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001648 } else
Jason Wang54f968d2012-10-31 19:45:57 +00001649 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06001650 ret = 0;
1651out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06001652 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655static int tun_chr_open(struct inode *inode, struct file * file)
1656{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001657 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07001658
Joe Perches6b8a66e2011-03-02 07:18:10 +00001659 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00001660
Jason Wang54f968d2012-10-31 19:45:57 +00001661 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1662 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001663 if (!tfile)
1664 return -ENOMEM;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +00001665 atomic_set(&tfile->count, 0);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001666 tfile->tun = NULL;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001667 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00001668 tfile->flags = 0;
1669
1670 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1671 init_waitqueue_head(&tfile->wq.wait);
1672
1673 tfile->socket.file = file;
1674 tfile->socket.ops = &tun_socket_ops;
1675
1676 sock_init_data(&tfile->socket, &tfile->sk);
1677 sk_change_net(&tfile->sk, tfile->net);
1678
1679 tfile->sk.sk_write_space = tun_sock_write_space;
1680 tfile->sk.sk_sndbuf = INT_MAX;
1681
Eric W. Biederman631ab462009-01-20 11:00:40 +00001682 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00001683 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return 0;
1686}
1687
1688static int tun_chr_close(struct inode *inode, struct file *file)
1689{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001690 struct tun_file *tfile = file->private_data;
Eric W. Biedermanf0a4d0e2009-06-08 00:44:31 -07001691 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001692 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Eric W. Biedermanf0a4d0e2009-06-08 00:44:31 -07001694 tun = __tun_get(tfile);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001695 if (tun) {
Herbert Xud23e4362009-07-02 23:03:55 +00001696 struct net_device *dev = tun->dev;
1697
Joe Perches6b8a66e2011-03-02 07:18:10 +00001698 tun_debug(KERN_INFO, tun, "tun_chr_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Eric W. Biederman631ab462009-01-20 11:00:40 +00001700 __tun_detach(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08001702 /* If desirable, unregister the netdevice. */
Herbert Xud23e4362009-07-02 23:03:55 +00001703 if (!(tun->flags & TUN_PERSIST)) {
1704 rtnl_lock();
1705 if (dev->reg_state == NETREG_REGISTERED)
1706 unregister_netdevice(dev);
1707 rtnl_unlock();
1708 }
Jason Wang54f968d2012-10-31 19:45:57 +00001709
1710 /* drop the reference that netdevice holds */
1711 sock_put(&tfile->sk);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Jason Wang54f968d2012-10-31 19:45:57 +00001714 /* drop the reference that file holds */
1715 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
1716 &tfile->socket.flags));
1717 sk_release_kernel(&tfile->sk);
1718 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 return 0;
1721}
1722
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001723static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001724 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001726 .read = do_sync_read,
1727 .aio_read = tun_chr_aio_read,
1728 .write = do_sync_write,
1729 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08001731 .unlocked_ioctl = tun_chr_ioctl,
1732#ifdef CONFIG_COMPAT
1733 .compat_ioctl = tun_chr_compat_ioctl,
1734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 .open = tun_chr_open,
1736 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001737 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738};
1739
1740static struct miscdevice tun_miscdev = {
1741 .minor = TUN_MINOR,
1742 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02001743 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745};
1746
1747/* ethtool interface */
1748
1749static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1750{
1751 cmd->supported = 0;
1752 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00001753 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 cmd->duplex = DUPLEX_FULL;
1755 cmd->port = PORT_TP;
1756 cmd->phy_address = 0;
1757 cmd->transceiver = XCVR_INTERNAL;
1758 cmd->autoneg = AUTONEG_DISABLE;
1759 cmd->maxtxpkt = 0;
1760 cmd->maxrxpkt = 0;
1761 return 0;
1762}
1763
1764static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1765{
1766 struct tun_struct *tun = netdev_priv(dev);
1767
Rick Jones33a5ba12011-11-15 14:59:53 +00001768 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1769 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 switch (tun->flags & TUN_TYPE_MASK) {
1772 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00001773 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 break;
1775 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00001776 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 break;
1778 }
1779}
1780
1781static u32 tun_get_msglevel(struct net_device *dev)
1782{
1783#ifdef TUN_DEBUG
1784 struct tun_struct *tun = netdev_priv(dev);
1785 return tun->debug;
1786#else
1787 return -EOPNOTSUPP;
1788#endif
1789}
1790
1791static void tun_set_msglevel(struct net_device *dev, u32 value)
1792{
1793#ifdef TUN_DEBUG
1794 struct tun_struct *tun = netdev_priv(dev);
1795 tun->debug = value;
1796#endif
1797}
1798
Jeff Garzik7282d492006-09-13 14:30:00 -04001799static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 .get_settings = tun_get_settings,
1801 .get_drvinfo = tun_get_drvinfo,
1802 .get_msglevel = tun_get_msglevel,
1803 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00001804 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805};
1806
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808static int __init tun_init(void)
1809{
1810 int ret = 0;
1811
Joe Perches6b8a66e2011-03-02 07:18:10 +00001812 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1813 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001815 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001816 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001817 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001818 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001819 }
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001822 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001823 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001824 goto err_misc;
1825 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001826 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001827err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001828 rtnl_link_unregister(&tun_link_ops);
1829err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return ret;
1831}
1832
1833static void tun_cleanup(void)
1834{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001835 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001836 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001839/* Get an underlying socket object from tun file. Returns error unless file is
1840 * attached to a device. The returned object works like a packet socket, it
1841 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1842 * holding a reference to the file for as long as the socket is in use. */
1843struct socket *tun_get_socket(struct file *file)
1844{
1845 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001846 struct tun_file *tfile = file->private_data;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001847 if (file->f_op != &tun_fops)
1848 return ERR_PTR(-EINVAL);
1849 tun = tun_get(file);
1850 if (!tun)
1851 return ERR_PTR(-EBADFD);
1852 tun_put(tun);
Jason Wang54f968d2012-10-31 19:45:57 +00001853 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001854}
1855EXPORT_SYMBOL_GPL(tun_get_socket);
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857module_init(tun_init);
1858module_exit(tun_cleanup);
1859MODULE_DESCRIPTION(DRV_DESCRIPTION);
1860MODULE_AUTHOR(DRV_COPYRIGHT);
1861MODULE_LICENSE("GPL");
1862MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02001863MODULE_ALIAS("devname:net/tun");