blob: bdbb526eca7b201fbd9927f14883b179ee03143a [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).
Jason Wang6e914fc2012-10-31 19:45:58 +0000118 *
119 * RCU usage:
120 * The tun_file and tun_struct are loosely coupled, the pointer from on to the
121 * other can only be read while rcu_read_lock or rtnl_lock is held.
Jason Wang54f968d2012-10-31 19:45:57 +0000122 */
Eric W. Biederman631ab462009-01-20 11:00:40 +0000123struct tun_file {
Jason Wang54f968d2012-10-31 19:45:57 +0000124 struct sock sk;
125 struct socket socket;
126 struct socket_wq wq;
Jason Wang6e914fc2012-10-31 19:45:58 +0000127 struct tun_struct __rcu *tun;
Eric W. Biederman36b50ba2009-01-20 11:01:48 +0000128 struct net *net;
Jason Wang54f968d2012-10-31 19:45:57 +0000129 struct fasync_struct *fasync;
130 /* only used for fasnyc */
131 unsigned int flags;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000132};
133
Jason Wang54f968d2012-10-31 19:45:57 +0000134/* Since the socket were moved to tun_file, to preserve the behavior of persist
135 * device, socket fileter, sndbuf and vnet header size were restore when the
136 * file were attached to a persist device.
137 */
Rusty Russell14daa022008-04-12 18:48:58 -0700138struct tun_struct {
Jason Wang6e914fc2012-10-31 19:45:58 +0000139 struct tun_file __rcu *tfile;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700140 unsigned int flags;
Eric W. Biederman0625c882012-02-07 16:48:55 -0800141 kuid_t owner;
142 kgid_t group;
Rusty Russell14daa022008-04-12 18:48:58 -0700143
Rusty Russell14daa022008-04-12 18:48:58 -0700144 struct net_device *dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000145 netdev_features_t set_features;
Michał Mirosław88255372011-04-19 06:13:10 +0000146#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
147 NETIF_F_TSO6|NETIF_F_UFO)
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200148
149 int vnet_hdr_sz;
Jason Wang54f968d2012-10-31 19:45:57 +0000150 int sndbuf;
151 struct tap_filter txflt;
152 struct sock_fprog fprog;
153 /* protected by rtnl lock */
154 bool filter_attached;
Rusty Russell14daa022008-04-12 18:48:58 -0700155#ifdef TUN_DEBUG
156 int debug;
157#endif
158};
159
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000160static int tun_attach(struct tun_struct *tun, struct file *file)
161{
Eric W. Biederman631ab462009-01-20 11:00:40 +0000162 struct tun_file *tfile = file->private_data;
Eric W. Biederman38231b72009-01-20 11:02:28 +0000163 int err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000164
165 ASSERT_RTNL();
166
Eric W. Biederman38231b72009-01-20 11:02:28 +0000167 netif_tx_lock_bh(tun->dev);
168
169 err = -EINVAL;
170 if (tfile->tun)
171 goto out;
172
173 err = -EBUSY;
174 if (tun->tfile)
175 goto out;
176
177 err = 0;
Jason Wang54f968d2012-10-31 19:45:57 +0000178
179 /* Re-attach filter when attaching to a persist device */
180 if (tun->filter_attached == true) {
181 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
182 if (!err)
183 goto out;
184 }
Jason Wang6e914fc2012-10-31 19:45:58 +0000185 rcu_assign_pointer(tfile->tun, tun);
Jason Wang54f968d2012-10-31 19:45:57 +0000186 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
Jason Wang6e914fc2012-10-31 19:45:58 +0000187 rcu_assign_pointer(tun->tfile, tfile);
Nolan Leakebee31362010-07-27 13:53:43 +0000188 netif_carrier_on(tun->dev);
Jason Wang54f968d2012-10-31 19:45:57 +0000189 sock_hold(&tfile->sk);
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000190
Eric W. Biederman38231b72009-01-20 11:02:28 +0000191out:
192 netif_tx_unlock_bh(tun->dev);
193 return err;
Eric W. Biedermana7385ba2009-01-20 10:57:48 +0000194}
195
Eric W. Biederman631ab462009-01-20 11:00:40 +0000196static void __tun_detach(struct tun_struct *tun)
197{
Jason Wang6e914fc2012-10-31 19:45:58 +0000198 struct tun_file *tfile = rcu_dereference_protected(tun->tfile,
199 lockdep_rtnl_is_held());
Eric W. Biederman631ab462009-01-20 11:00:40 +0000200 /* Detach from net device */
Nolan Leakebee31362010-07-27 13:53:43 +0000201 netif_carrier_off(tun->dev);
Jason Wang6e914fc2012-10-31 19:45:58 +0000202 rcu_assign_pointer(tun->tfile, NULL);
203 if (tfile) {
204 rcu_assign_pointer(tfile->tun, NULL);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000205
Jason Wang6e914fc2012-10-31 19:45:58 +0000206 synchronize_net();
207 /* Drop read queue */
208 skb_queue_purge(&tfile->socket.sk->sk_receive_queue);
209 }
Eric W. Biederman631ab462009-01-20 11:00:40 +0000210}
211
212static struct tun_struct *__tun_get(struct tun_file *tfile)
213{
Jason Wang6e914fc2012-10-31 19:45:58 +0000214 struct tun_struct *tun;
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000215
Jason Wang6e914fc2012-10-31 19:45:58 +0000216 rcu_read_lock();
217 tun = rcu_dereference(tfile->tun);
218 if (tun)
219 dev_hold(tun->dev);
220 rcu_read_unlock();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000221
222 return tun;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000223}
224
225static struct tun_struct *tun_get(struct file *file)
226{
227 return __tun_get(file->private_data);
228}
229
230static void tun_put(struct tun_struct *tun)
231{
Jason Wang6e914fc2012-10-31 19:45:58 +0000232 dev_put(tun->dev);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000233}
234
Joe Perches6b8a66e2011-03-02 07:18:10 +0000235/* TAP filtering */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700236static void addr_hash_set(u32 *mask, const u8 *addr)
237{
238 int n = ether_crc(ETH_ALEN, addr) >> 26;
239 mask[n >> 5] |= (1 << (n & 31));
240}
241
242static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
243{
244 int n = ether_crc(ETH_ALEN, addr) >> 26;
245 return mask[n >> 5] & (1 << (n & 31));
246}
247
248static int update_filter(struct tap_filter *filter, void __user *arg)
249{
250 struct { u8 u[ETH_ALEN]; } *addr;
251 struct tun_filter uf;
252 int err, alen, n, nexact;
253
254 if (copy_from_user(&uf, arg, sizeof(uf)))
255 return -EFAULT;
256
257 if (!uf.count) {
258 /* Disabled */
259 filter->count = 0;
260 return 0;
261 }
262
263 alen = ETH_ALEN * uf.count;
264 addr = kmalloc(alen, GFP_KERNEL);
265 if (!addr)
266 return -ENOMEM;
267
268 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
269 err = -EFAULT;
270 goto done;
271 }
272
273 /* The filter is updated without holding any locks. Which is
274 * perfectly safe. We disable it first and in the worst
275 * case we'll accept a few undesired packets. */
276 filter->count = 0;
277 wmb();
278
279 /* Use first set of addresses as an exact filter */
280 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
281 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
282
283 nexact = n;
284
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800285 /* Remaining multicast addresses are hashed,
286 * unicast will leave the filter disabled. */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700287 memset(filter->mask, 0, sizeof(filter->mask));
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800288 for (; n < uf.count; n++) {
289 if (!is_multicast_ether_addr(addr[n].u)) {
290 err = 0; /* no filter */
291 goto done;
292 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700293 addr_hash_set(filter->mask, addr[n].u);
Alex Williamsoncfbf84f2009-02-08 17:49:17 -0800294 }
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700295
296 /* For ALLMULTI just set the mask to all ones.
297 * This overrides the mask populated above. */
298 if ((uf.flags & TUN_FLT_ALLMULTI))
299 memset(filter->mask, ~0, sizeof(filter->mask));
300
301 /* Now enable the filter */
302 wmb();
303 filter->count = nexact;
304
305 /* Return the number of exact filters */
306 err = nexact;
307
308done:
309 kfree(addr);
310 return err;
311}
312
313/* Returns: 0 - drop, !=0 - accept */
314static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
315{
316 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
317 * at this point. */
318 struct ethhdr *eh = (struct ethhdr *) skb->data;
319 int i;
320
321 /* Exact match */
322 for (i = 0; i < filter->count; i++)
Joe Perches2e42e472012-05-09 17:17:46 +0000323 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700324 return 1;
325
326 /* Inexact match (multicast only) */
327 if (is_multicast_ether_addr(eh->h_dest))
328 return addr_hash_test(filter->mask, eh->h_dest);
329
330 return 0;
331}
332
333/*
334 * Checks whether the packet is accepted or not.
335 * Returns: 0 - drop, !=0 - accept
336 */
337static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
338{
339 if (!filter->count)
340 return 1;
341
342 return run_filter(filter, skb);
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/* Network device part of the driver */
346
Jeff Garzik7282d492006-09-13 14:30:00 -0400347static const struct ethtool_ops tun_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000349/* Net device detach from fd. */
350static void tun_net_uninit(struct net_device *dev)
351{
352 struct tun_struct *tun = netdev_priv(dev);
Jason Wang6e914fc2012-10-31 19:45:58 +0000353 struct tun_file *tfile = rcu_dereference_protected(tun->tfile,
354 lockdep_rtnl_is_held());
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000355
356 /* Inform the methods they need to stop using the dev.
357 */
358 if (tfile) {
Jason Wang54f968d2012-10-31 19:45:57 +0000359 wake_up_all(&tfile->wq.wait);
Jason Wang6e914fc2012-10-31 19:45:58 +0000360 __tun_detach(tun);
361 synchronize_net();
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000362 }
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/* Net device open. */
366static int tun_net_open(struct net_device *dev)
367{
368 netif_start_queue(dev);
369 return 0;
370}
371
372/* Net device close. */
373static int tun_net_close(struct net_device *dev)
374{
375 netif_stop_queue(dev);
376 return 0;
377}
378
379/* Net device start xmit */
Stephen Hemminger424efe92009-08-31 19:50:51 +0000380static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct tun_struct *tun = netdev_priv(dev);
Jason Wang6e914fc2012-10-31 19:45:58 +0000383 struct tun_file *tfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Jason Wang6e914fc2012-10-31 19:45:58 +0000385 rcu_read_lock();
386 tfile = rcu_dereference(tun->tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* Drop packet if interface is not attached */
Jason Wang54f968d2012-10-31 19:45:57 +0000388 if (!tfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 goto drop;
390
Jason Wang6e914fc2012-10-31 19:45:58 +0000391 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
392
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700393 /* Drop if the filter does not like it.
394 * This is a noop if the filter is disabled.
395 * Filter can be enabled only for the TAP devices. */
396 if (!check_filter(&tun->txflt, skb))
397 goto drop;
398
Jason Wang54f968d2012-10-31 19:45:57 +0000399 if (tfile->socket.sk->sk_filter &&
400 sk_filter(tfile->socket.sk, skb))
Michael S. Tsirkin99405162010-02-14 01:01:10 +0000401 goto drop;
402
Jason Wang54f968d2012-10-31 19:45:57 +0000403 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
404 >= dev->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (!(tun->flags & TUN_ONE_QUEUE)) {
406 /* Normal queueing mode. */
407 /* Packet scheduler handles dropping of further packets. */
408 netif_stop_queue(dev);
409
410 /* We won't see all dropped packets individually, so overrun
411 * error is more appropriate. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700412 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 } else {
414 /* Single queue mode.
415 * Driver handles dropping of all packets itself. */
416 goto drop;
417 }
418 }
419
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000420 /* Orphan the skb - required as we might hang on to it
421 * for indefinite time. */
Michael S. Tsirkin868eefe2012-07-20 09:23:14 +0000422 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
423 goto drop;
Michael S. Tsirkin0110d6f2010-04-13 04:59:44 +0000424 skb_orphan(skb);
425
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700426 /* Enqueue packet */
Jason Wang54f968d2012-10-31 19:45:57 +0000427 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* Notify and wake up reader process */
Jason Wang54f968d2012-10-31 19:45:57 +0000430 if (tfile->flags & TUN_FASYNC)
431 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
432 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000433 POLLRDNORM | POLLRDBAND);
Jason Wang6e914fc2012-10-31 19:45:58 +0000434
435 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000436 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438drop:
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700439 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 kfree_skb(skb);
Jason Wang6e914fc2012-10-31 19:45:58 +0000441 rcu_read_unlock();
Patrick McHardy6ed10652009-06-23 06:03:08 +0000442 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700445static void tun_net_mclist(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700447 /*
448 * This callback is supposed to deal with mc filter in
449 * _rx_ path and has nothing to do with the _tx_ path.
450 * In rx path we always accept everything userspace gives us.
451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Ed Swierk4885a502007-09-16 12:21:38 -0700454#define MIN_MTU 68
455#define MAX_MTU 65535
456
457static int
458tun_net_change_mtu(struct net_device *dev, int new_mtu)
459{
460 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
461 return -EINVAL;
462 dev->mtu = new_mtu;
463 return 0;
464}
465
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000466static netdev_features_t tun_net_fix_features(struct net_device *dev,
467 netdev_features_t features)
Michał Mirosław88255372011-04-19 06:13:10 +0000468{
469 struct tun_struct *tun = netdev_priv(dev);
470
471 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
472}
Neil Hormanbebd0972011-06-15 05:25:01 +0000473#ifdef CONFIG_NET_POLL_CONTROLLER
474static void tun_poll_controller(struct net_device *dev)
475{
476 /*
477 * Tun only receives frames when:
478 * 1) the char device endpoint gets data from user space
479 * 2) the tun socket gets a sendmsg call from user space
480 * Since both of those are syncronous operations, we are guaranteed
481 * never to have pending data when we poll for it
482 * so theres nothing to do here but return.
483 * We need this though so netpoll recognizes us as an interface that
484 * supports polling, which enables bridge devices in virt setups to
485 * still use netconsole
486 */
487 return;
488}
489#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800490static const struct net_device_ops tun_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000491 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800492 .ndo_open = tun_net_open,
493 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800494 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800495 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000496 .ndo_fix_features = tun_net_fix_features,
Neil Hormanbebd0972011-06-15 05:25:01 +0000497#ifdef CONFIG_NET_POLL_CONTROLLER
498 .ndo_poll_controller = tun_poll_controller,
499#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800500};
501
502static const struct net_device_ops tap_netdev_ops = {
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000503 .ndo_uninit = tun_net_uninit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800504 .ndo_open = tun_net_open,
505 .ndo_stop = tun_net_close,
Stephen Hemminger00829822008-11-20 20:14:53 -0800506 .ndo_start_xmit = tun_net_xmit,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800507 .ndo_change_mtu = tun_net_change_mtu,
Michał Mirosław88255372011-04-19 06:13:10 +0000508 .ndo_fix_features = tun_net_fix_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000509 .ndo_set_rx_mode = tun_net_mclist,
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800510 .ndo_set_mac_address = eth_mac_addr,
511 .ndo_validate_addr = eth_validate_addr,
Neil Hormanbebd0972011-06-15 05:25:01 +0000512#ifdef CONFIG_NET_POLL_CONTROLLER
513 .ndo_poll_controller = tun_poll_controller,
514#endif
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800515};
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/* Initialize net device. */
518static void tun_net_init(struct net_device *dev)
519{
520 struct tun_struct *tun = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 switch (tun->flags & TUN_TYPE_MASK) {
523 case TUN_TUN_DEV:
Stephen Hemminger758e43b2008-11-19 22:10:37 -0800524 dev->netdev_ops = &tun_netdev_ops;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Point-to-Point TUN Device */
527 dev->hard_header_len = 0;
528 dev->addr_len = 0;
529 dev->mtu = 1500;
530
531 /* Zero header length */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400532 dev->type = ARPHRD_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
534 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
535 break;
536
537 case TUN_TAP_DEV:
Kusanagi Kouichi7a0a9602008-12-29 18:23:28 -0800538 dev->netdev_ops = &tap_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* Ethernet TAP Device */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -0700540 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000541 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000543 eth_hw_addr_random(dev);
Brian Braunstein36226a82007-04-26 01:00:55 -0700544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
546 break;
547 }
548}
549
550/* Character device part */
551
552/* Poll */
553static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400554{
Eric W. Biedermanb2430de2009-01-20 11:03:21 +0000555 struct tun_file *tfile = file->private_data;
556 struct tun_struct *tun = __tun_get(tfile);
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000557 struct sock *sk;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800558 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (!tun)
Eric W. Biedermaneac9e902009-01-20 10:59:05 +0000561 return POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jason Wang54f968d2012-10-31 19:45:57 +0000563 sk = tfile->socket.sk;
Mariusz Kozlowski3c8a9c62009-07-05 19:48:35 +0000564
Joe Perches6b8a66e2011-03-02 07:18:10 +0000565 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jason Wang54f968d2012-10-31 19:45:57 +0000567 poll_wait(file, &tfile->wq.wait, wait);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400568
Michael S. Tsirkin89f56d12009-08-30 07:04:42 +0000569 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 mask |= POLLIN | POLLRDNORM;
571
Herbert Xu33dccbb2009-02-05 21:25:32 -0800572 if (sock_writeable(sk) ||
573 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
574 sock_writeable(sk)))
575 mask |= POLLOUT | POLLWRNORM;
576
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000577 if (tun->dev->reg_state != NETREG_REGISTERED)
578 mask = POLLERR;
579
Eric W. Biederman631ab462009-01-20 11:00:40 +0000580 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return mask;
582}
583
Rusty Russellf42157c2008-08-15 15:15:10 -0700584/* prepad is the amount to reserve at front. len is length after that.
585 * linear is a hint as to how much to copy (usually headers). */
Jason Wang54f968d2012-10-31 19:45:57 +0000586static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000587 size_t prepad, size_t len,
588 size_t linear, int noblock)
Rusty Russellf42157c2008-08-15 15:15:10 -0700589{
Jason Wang54f968d2012-10-31 19:45:57 +0000590 struct sock *sk = tfile->socket.sk;
Rusty Russellf42157c2008-08-15 15:15:10 -0700591 struct sk_buff *skb;
Herbert Xu33dccbb2009-02-05 21:25:32 -0800592 int err;
Rusty Russellf42157c2008-08-15 15:15:10 -0700593
594 /* Under a page? Don't bother with paged skb. */
Herbert Xu0eca93b2009-04-14 02:09:43 -0700595 if (prepad + len < PAGE_SIZE || !linear)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800596 linear = len;
Rusty Russellf42157c2008-08-15 15:15:10 -0700597
Herbert Xu33dccbb2009-02-05 21:25:32 -0800598 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
599 &err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700600 if (!skb)
Herbert Xu33dccbb2009-02-05 21:25:32 -0800601 return ERR_PTR(err);
Rusty Russellf42157c2008-08-15 15:15:10 -0700602
603 skb_reserve(skb, prepad);
604 skb_put(skb, linear);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800605 skb->data_len = len - linear;
606 skb->len += len - linear;
Rusty Russellf42157c2008-08-15 15:15:10 -0700607
608 return skb;
609}
610
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000611/* set skb frags from iovec, this can move to core network code for reuse */
612static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
613 int offset, size_t count)
614{
615 int len = iov_length(from, count) - offset;
616 int copy = skb_headlen(skb);
617 int size, offset1 = 0;
618 int i = 0;
619
620 /* Skip over from offset */
621 while (count && (offset >= from->iov_len)) {
622 offset -= from->iov_len;
623 ++from;
624 --count;
625 }
626
627 /* copy up to skb headlen */
628 while (count && (copy > 0)) {
629 size = min_t(unsigned int, copy, from->iov_len - offset);
630 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
631 size))
632 return -EFAULT;
633 if (copy > size) {
634 ++from;
635 --count;
636 offset = 0;
637 } else
638 offset += size;
639 copy -= size;
640 offset1 += size;
641 }
642
643 if (len == offset1)
644 return 0;
645
646 while (count--) {
647 struct page *page[MAX_SKB_FRAGS];
648 int num_pages;
649 unsigned long base;
650 unsigned long truesize;
651
652 len = from->iov_len - offset;
653 if (!len) {
654 offset = 0;
655 ++from;
656 continue;
657 }
658 base = (unsigned long)from->iov_base + offset;
659 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
660 if (i + size > MAX_SKB_FRAGS)
661 return -EMSGSIZE;
662 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
663 if (num_pages != size) {
664 for (i = 0; i < num_pages; i++)
665 put_page(page[i]);
666 return -EFAULT;
667 }
668 truesize = size * PAGE_SIZE;
669 skb->data_len += len;
670 skb->len += len;
671 skb->truesize += truesize;
672 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
673 while (len) {
674 int off = base & ~PAGE_MASK;
675 int size = min_t(int, len, PAGE_SIZE - off);
676 __skb_fill_page_desc(skb, i, page[i], off, size);
677 skb_shinfo(skb)->nr_frags++;
678 /* increase sk_wmem_alloc */
679 base += size;
680 len -= size;
681 i++;
682 }
683 offset = 0;
684 ++from;
685 }
686 return 0;
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/* Get packet from user space buffer */
Jason Wang54f968d2012-10-31 19:45:57 +0000690static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
691 void *msg_control, const struct iovec *iv,
692 size_t total_len, size_t count, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Harvey Harrison09640e62009-02-01 00:45:17 -0800694 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct sk_buff *skb;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000696 size_t len = total_len, align = NET_SKB_PAD;
Rusty Russellf43798c2008-07-03 03:48:02 -0700697 struct virtio_net_hdr gso = { 0 };
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000698 int offset = 0;
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000699 int copylen;
700 bool zerocopy = false;
701 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (!(tun->flags & TUN_NO_PI)) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000704 if ((len -= sizeof(pi)) > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -EINVAL;
706
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000707 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return -EFAULT;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000709 offset += sizeof(pi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Rusty Russellf43798c2008-07-03 03:48:02 -0700712 if (tun->flags & TUN_VNET_HDR) {
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000713 if ((len -= tun->vnet_hdr_sz) > total_len)
Rusty Russellf43798c2008-07-03 03:48:02 -0700714 return -EINVAL;
715
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000716 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
Rusty Russellf43798c2008-07-03 03:48:02 -0700717 return -EFAULT;
718
Herbert Xu49091222009-06-08 00:20:01 -0700719 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
720 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
721 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
722
Rusty Russellf43798c2008-07-03 03:48:02 -0700723 if (gso.hdr_len > len)
724 return -EINVAL;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200725 offset += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700726 }
727
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700728 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
stephen hemmingera504b862011-06-08 14:33:07 +0000729 align += NET_IP_ALIGN;
Herbert Xu0eca93b2009-04-14 02:09:43 -0700730 if (unlikely(len < ETH_HLEN ||
731 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
Rusty Russelle01bf1c2008-04-12 18:49:30 -0700732 return -EINVAL;
733 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400734
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000735 if (msg_control)
736 zerocopy = true;
737
738 if (zerocopy) {
739 /* Userspace may produce vectors with count greater than
740 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
741 * to let the rest of data to be fit in the frags.
742 */
743 if (count > MAX_SKB_FRAGS) {
744 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
745 if (copylen < offset)
746 copylen = 0;
747 else
748 copylen -= offset;
749 } else
750 copylen = 0;
751 /* There are 256 bytes to be copied in skb, so there is enough
752 * room for skb expand head in case it is used.
753 * The rest of the buffer is mapped from userspace.
754 */
755 if (copylen < gso.hdr_len)
756 copylen = gso.hdr_len;
757 if (!copylen)
758 copylen = GOODCOPY_LEN;
759 } else
760 copylen = len;
761
Jason Wang54f968d2012-10-31 19:45:57 +0000762 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
Herbert Xu33dccbb2009-02-05 21:25:32 -0800763 if (IS_ERR(skb)) {
764 if (PTR_ERR(skb) != -EAGAIN)
765 tun->dev->stats.rx_dropped++;
766 return PTR_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000769 if (zerocopy)
770 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
771 else
772 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
773
774 if (err) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700775 tun->dev->stats.rx_dropped++;
Dave Jones8f227572006-03-11 18:49:13 -0800776 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -EFAULT;
Dave Jones8f227572006-03-11 18:49:13 -0800778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Rusty Russellf43798c2008-07-03 03:48:02 -0700780 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
781 if (!skb_partial_csum_set(skb, gso.csum_start,
782 gso.csum_offset)) {
783 tun->dev->stats.rx_frame_errors++;
784 kfree_skb(skb);
785 return -EINVAL;
786 }
Michał Mirosław88255372011-04-19 06:13:10 +0000787 }
Rusty Russellf43798c2008-07-03 03:48:02 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 switch (tun->flags & TUN_TYPE_MASK) {
790 case TUN_TUN_DEV:
Ang Way Chuangf09f7ee2008-06-17 21:10:33 -0700791 if (tun->flags & TUN_NO_PI) {
792 switch (skb->data[0] & 0xf0) {
793 case 0x40:
794 pi.proto = htons(ETH_P_IP);
795 break;
796 case 0x60:
797 pi.proto = htons(ETH_P_IPV6);
798 break;
799 default:
800 tun->dev->stats.rx_dropped++;
801 kfree_skb(skb);
802 return -EINVAL;
803 }
804 }
805
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700806 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 skb->protocol = pi.proto;
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700808 skb->dev = tun->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
810 case TUN_TAP_DEV:
811 skb->protocol = eth_type_trans(skb, tun->dev);
812 break;
Joe Perches6403eab2011-06-03 11:51:20 +0000813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Rusty Russellf43798c2008-07-03 03:48:02 -0700815 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
816 pr_debug("GSO!\n");
817 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
818 case VIRTIO_NET_HDR_GSO_TCPV4:
819 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
820 break;
821 case VIRTIO_NET_HDR_GSO_TCPV6:
822 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
823 break;
Sridhar Samudralae36aa252009-07-14 14:21:04 +0000824 case VIRTIO_NET_HDR_GSO_UDP:
825 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
826 break;
Rusty Russellf43798c2008-07-03 03:48:02 -0700827 default:
828 tun->dev->stats.rx_frame_errors++;
829 kfree_skb(skb);
830 return -EINVAL;
831 }
832
833 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
834 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
835
836 skb_shinfo(skb)->gso_size = gso.gso_size;
837 if (skb_shinfo(skb)->gso_size == 0) {
838 tun->dev->stats.rx_frame_errors++;
839 kfree_skb(skb);
840 return -EINVAL;
841 }
842
843 /* Header must be checked, and gso_segs computed. */
844 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
845 skb_shinfo(skb)->gso_segs = 0;
846 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400847
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000848 /* copy skb_ubuf_info for callback when skb has no error */
849 if (zerocopy) {
850 skb_shinfo(skb)->destructor_arg = msg_control;
851 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
852 }
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 netif_rx_ni(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400855
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700856 tun->dev->stats.rx_packets++;
857 tun->dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Michael S. Tsirkin06908992012-07-20 09:23:23 +0000859 return total_len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400860}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700862static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
863 unsigned long count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Herbert Xu33dccbb2009-02-05 21:25:32 -0800865 struct file *file = iocb->ki_filp;
Herbert Xuab46d772009-02-14 20:46:39 -0800866 struct tun_struct *tun = tun_get(file);
Jason Wang54f968d2012-10-31 19:45:57 +0000867 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +0000868 ssize_t result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (!tun)
871 return -EBADFD;
872
Joe Perches6b8a66e2011-03-02 07:18:10 +0000873 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Jason Wang54f968d2012-10-31 19:45:57 +0000875 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
876 count, file->f_flags & O_NONBLOCK);
Eric W. Biederman631ab462009-01-20 11:00:40 +0000877
878 tun_put(tun);
879 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882/* Put packet to the user space buffer */
stephen hemminger6f7c1562011-06-08 14:33:08 +0000883static ssize_t tun_put_user(struct tun_struct *tun,
Jason Wang54f968d2012-10-31 19:45:57 +0000884 struct tun_file *tfile,
stephen hemminger6f7c1562011-06-08 14:33:08 +0000885 struct sk_buff *skb,
886 const struct iovec *iv, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct tun_pi pi = { 0, skb->protocol };
889 ssize_t total = 0;
890
891 if (!(tun->flags & TUN_NO_PI)) {
892 if ((len -= sizeof(pi)) < 0)
893 return -EINVAL;
894
895 if (len < skb->len) {
896 /* Packet will be striped */
897 pi.flags |= TUN_PKT_STRIP;
898 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400899
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000900 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EFAULT;
902 total += sizeof(pi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Rusty Russellf43798c2008-07-03 03:48:02 -0700905 if (tun->flags & TUN_VNET_HDR) {
906 struct virtio_net_hdr gso = { 0 }; /* no info leak */
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200907 if ((len -= tun->vnet_hdr_sz) < 0)
Rusty Russellf43798c2008-07-03 03:48:02 -0700908 return -EINVAL;
909
910 if (skb_is_gso(skb)) {
911 struct skb_shared_info *sinfo = skb_shinfo(skb);
912
913 /* This is a hint as to how much should be linear. */
914 gso.hdr_len = skb_headlen(skb);
915 gso.gso_size = sinfo->gso_size;
916 if (sinfo->gso_type & SKB_GSO_TCPV4)
917 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
918 else if (sinfo->gso_type & SKB_GSO_TCPV6)
919 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Sridhar Samudralae36aa252009-07-14 14:21:04 +0000920 else if (sinfo->gso_type & SKB_GSO_UDP)
921 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +0000922 else {
Joe Perches6b8a66e2011-03-02 07:18:10 +0000923 pr_err("unexpected GSO type: "
Michael S. Tsirkinef3db4a2010-07-21 04:32:45 +0000924 "0x%x, gso_size %d, hdr_len %d\n",
925 sinfo->gso_type, gso.gso_size,
926 gso.hdr_len);
927 print_hex_dump(KERN_ERR, "tun: ",
928 DUMP_PREFIX_NONE,
929 16, 1, skb->head,
930 min((int)gso.hdr_len, 64), true);
931 WARN_ON_ONCE(1);
932 return -EINVAL;
933 }
Rusty Russellf43798c2008-07-03 03:48:02 -0700934 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
935 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
936 } else
937 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
938
939 if (skb->ip_summed == CHECKSUM_PARTIAL) {
940 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +0000941 gso.csum_start = skb_checksum_start_offset(skb);
Rusty Russellf43798c2008-07-03 03:48:02 -0700942 gso.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +0000943 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
944 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Rusty Russellf43798c2008-07-03 03:48:02 -0700945 } /* else everything is zero */
946
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000947 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
948 sizeof(gso))))
Rusty Russellf43798c2008-07-03 03:48:02 -0700949 return -EFAULT;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +0200950 total += tun->vnet_hdr_sz;
Rusty Russellf43798c2008-07-03 03:48:02 -0700951 }
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 len = min_t(int, skb->len, len);
954
Michael S. Tsirkin43b39dc2009-04-20 01:25:59 +0000955 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000956 total += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700958 tun->dev->stats.tx_packets++;
959 tun->dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 return total;
962}
963
Jason Wang54f968d2012-10-31 19:45:57 +0000964static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000965 struct kiocb *iocb, const struct iovec *iv,
966 ssize_t len, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 DECLARE_WAITQUEUE(wait, current);
969 struct sk_buff *skb;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000970 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Joe Perches6b8a66e2011-03-02 07:18:10 +0000972 tun_debug(KERN_INFO, tun, "tun_chr_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Amos Kong61a5ff12011-06-09 00:27:10 -0700974 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +0000975 add_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 while (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 current->state = TASK_INTERRUPTIBLE;
978
979 /* Read frames from the queue */
Jason Wang54f968d2012-10-31 19:45:57 +0000980 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
Michael S. Tsirkin05c28282010-01-14 06:17:09 +0000981 if (noblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ret = -EAGAIN;
983 break;
984 }
985 if (signal_pending(current)) {
986 ret = -ERESTARTSYS;
987 break;
988 }
Eric W. Biedermanc70f1822009-01-20 11:07:17 +0000989 if (tun->dev->reg_state != NETREG_REGISTERED) {
990 ret = -EIO;
991 break;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /* Nothing to read, let's sleep */
995 schedule();
996 continue;
997 }
998 netif_wake_queue(tun->dev);
999
Jason Wang54f968d2012-10-31 19:45:57 +00001000 ret = tun_put_user(tun, tfile, skb, iv, len);
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001001 kfree_skb(skb);
1002 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
1005 current->state = TASK_RUNNING;
Amos Kong61a5ff12011-06-09 00:27:10 -07001006 if (unlikely(!noblock))
Jason Wang54f968d2012-10-31 19:45:57 +00001007 remove_wait_queue(&tfile->wq.wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001009 return ret;
1010}
1011
1012static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1013 unsigned long count, loff_t pos)
1014{
1015 struct file *file = iocb->ki_filp;
1016 struct tun_file *tfile = file->private_data;
1017 struct tun_struct *tun = __tun_get(tfile);
1018 ssize_t len, ret;
1019
1020 if (!tun)
1021 return -EBADFD;
1022 len = iov_length(iv, count);
1023 if (len < 0) {
1024 ret = -EINVAL;
1025 goto out;
1026 }
1027
Jason Wang54f968d2012-10-31 19:45:57 +00001028 ret = tun_do_read(tun, tfile, iocb, iv, len,
1029 file->f_flags & O_NONBLOCK);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001030 ret = min_t(ssize_t, ret, len);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001031out:
1032 tun_put(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return ret;
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static void tun_setup(struct net_device *dev)
1037{
1038 struct tun_struct *tun = netdev_priv(dev);
1039
Eric W. Biederman0625c882012-02-07 16:48:55 -08001040 tun->owner = INVALID_UID;
1041 tun->group = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 dev->ethtool_ops = &tun_ethtool_ops;
Jason Wang54f968d2012-10-31 19:45:57 +00001044 dev->destructor = free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001047/* Trivial set of netlink ops to allow deleting tun or tap
1048 * device with netlink.
1049 */
1050static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1051{
1052 return -EINVAL;
1053}
1054
1055static struct rtnl_link_ops tun_link_ops __read_mostly = {
1056 .kind = DRV_NAME,
1057 .priv_size = sizeof(struct tun_struct),
1058 .setup = tun_setup,
1059 .validate = tun_validate,
1060};
1061
Herbert Xu33dccbb2009-02-05 21:25:32 -08001062static void tun_sock_write_space(struct sock *sk)
1063{
Jason Wang54f968d2012-10-31 19:45:57 +00001064 struct tun_file *tfile;
Eric Dumazet43815482010-04-29 11:01:49 +00001065 wait_queue_head_t *wqueue;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001066
1067 if (!sock_writeable(sk))
1068 return;
1069
Herbert Xu33dccbb2009-02-05 21:25:32 -08001070 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1071 return;
1072
Eric Dumazet43815482010-04-29 11:01:49 +00001073 wqueue = sk_sleep(sk);
1074 if (wqueue && waitqueue_active(wqueue))
1075 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001076 POLLWRNORM | POLLWRBAND);
Herbert Xuc722c622009-06-03 21:45:55 -07001077
Jason Wang54f968d2012-10-31 19:45:57 +00001078 tfile = container_of(sk, struct tun_file, sk);
1079 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
Herbert Xu33dccbb2009-02-05 21:25:32 -08001080}
1081
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001082static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1083 struct msghdr *m, size_t total_len)
1084{
Jason Wang54f968d2012-10-31 19:45:57 +00001085 int ret;
1086 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1087 struct tun_struct *tun = __tun_get(tfile);
1088
1089 if (!tun)
1090 return -EBADFD;
Jason Wang54f968d2012-10-31 19:45:57 +00001091 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1092 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1093 tun_put(tun);
1094 return ret;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001095}
1096
Jason Wang54f968d2012-10-31 19:45:57 +00001097
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001098static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1099 struct msghdr *m, size_t total_len,
1100 int flags)
1101{
Jason Wang54f968d2012-10-31 19:45:57 +00001102 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1103 struct tun_struct *tun = __tun_get(tfile);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001104 int ret;
Jason Wang54f968d2012-10-31 19:45:57 +00001105
1106 if (!tun)
1107 return -EBADFD;
1108
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001109 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1110 return -EINVAL;
Jason Wang54f968d2012-10-31 19:45:57 +00001111 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001112 flags & MSG_DONTWAIT);
1113 if (ret > total_len) {
1114 m->msg_flags |= MSG_TRUNC;
1115 ret = flags & MSG_TRUNC ? ret : total_len;
1116 }
Jason Wang54f968d2012-10-31 19:45:57 +00001117 tun_put(tun);
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001118 return ret;
1119}
1120
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001121static int tun_release(struct socket *sock)
1122{
1123 if (sock->sk)
1124 sock_put(sock->sk);
1125 return 0;
1126}
1127
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001128/* Ops structure to mimic raw sockets with tun */
1129static const struct proto_ops tun_socket_ops = {
1130 .sendmsg = tun_sendmsg,
1131 .recvmsg = tun_recvmsg,
Stanislav Kinsbursky1ab5ecb2012-03-12 02:59:41 +00001132 .release = tun_release,
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001133};
1134
Herbert Xu33dccbb2009-02-05 21:25:32 -08001135static struct proto tun_proto = {
1136 .name = "tun",
1137 .owner = THIS_MODULE,
Jason Wang54f968d2012-10-31 19:45:57 +00001138 .obj_size = sizeof(struct tun_file),
Herbert Xu33dccbb2009-02-05 21:25:32 -08001139};
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001140
David Woodhouse980c9e82009-05-09 22:54:21 -07001141static int tun_flags(struct tun_struct *tun)
1142{
1143 int flags = 0;
1144
1145 if (tun->flags & TUN_TUN_DEV)
1146 flags |= IFF_TUN;
1147 else
1148 flags |= IFF_TAP;
1149
1150 if (tun->flags & TUN_NO_PI)
1151 flags |= IFF_NO_PI;
1152
1153 if (tun->flags & TUN_ONE_QUEUE)
1154 flags |= IFF_ONE_QUEUE;
1155
1156 if (tun->flags & TUN_VNET_HDR)
1157 flags |= IFF_VNET_HDR;
1158
1159 return flags;
1160}
1161
1162static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1163 char *buf)
1164{
1165 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1166 return sprintf(buf, "0x%x\n", tun_flags(tun));
1167}
1168
1169static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1170 char *buf)
1171{
1172 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001173 return uid_valid(tun->owner)?
1174 sprintf(buf, "%u\n",
1175 from_kuid_munged(current_user_ns(), tun->owner)):
1176 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001177}
1178
1179static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1180 char *buf)
1181{
1182 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
Eric W. Biederman0625c882012-02-07 16:48:55 -08001183 return gid_valid(tun->group) ?
1184 sprintf(buf, "%u\n",
1185 from_kgid_munged(current_user_ns(), tun->group)):
1186 sprintf(buf, "-1\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001187}
1188
1189static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1190static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1191static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1192
Pavel Emelyanovd647a592008-04-16 00:41:16 -07001193static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001196 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct net_device *dev;
1198 int err;
1199
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001200 dev = __dev_get_by_name(net, ifr->ifr_name);
1201 if (dev) {
Paul Moore2b980db2009-08-28 18:12:43 -04001202 const struct cred *cred = current_cred();
1203
David Woodhousef85ba782009-04-27 03:23:54 -07001204 if (ifr->ifr_flags & IFF_TUN_EXCL)
1205 return -EBUSY;
Eric W. Biederman74a3e5a2009-01-20 10:56:20 +00001206 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1207 tun = netdev_priv(dev);
1208 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1209 tun = netdev_priv(dev);
1210 else
1211 return -EINVAL;
1212
Eric W. Biederman0625c882012-02-07 16:48:55 -08001213 if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
1214 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
Paul Moore2b980db2009-08-28 18:12:43 -04001215 !capable(CAP_NET_ADMIN))
1216 return -EPERM;
Jason Wang54f968d2012-10-31 19:45:57 +00001217 err = security_tun_dev_attach(tfile->socket.sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001218 if (err < 0)
1219 return err;
1220
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001221 err = tun_attach(tun, file);
1222 if (err < 0)
1223 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 else {
1226 char *name;
1227 unsigned long flags = 0;
1228
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001229 if (!capable(CAP_NET_ADMIN))
1230 return -EPERM;
Paul Moore2b980db2009-08-28 18:12:43 -04001231 err = security_tun_dev_create();
1232 if (err < 0)
1233 return err;
David Woodhouseca6bb5d2006-06-22 16:07:52 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /* Set dev type */
1236 if (ifr->ifr_flags & IFF_TUN) {
1237 /* TUN device */
1238 flags |= TUN_TUN_DEV;
1239 name = "tun%d";
1240 } else if (ifr->ifr_flags & IFF_TAP) {
1241 /* TAP device */
1242 flags |= TUN_TAP_DEV;
1243 name = "tap%d";
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001244 } else
Kusanagi Kouichi36989b92009-09-16 21:36:13 +00001245 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (*ifr->ifr_name)
1248 name = ifr->ifr_name;
1249
1250 dev = alloc_netdev(sizeof(struct tun_struct), name,
1251 tun_setup);
1252 if (!dev)
1253 return -ENOMEM;
1254
Pavel Emelyanovfc54c652008-04-16 00:41:53 -07001255 dev_net_set(dev, net);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001256 dev->rtnl_link_ops = &tun_link_ops;
Stephen Hemminger758e43b2008-11-19 22:10:37 -08001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 tun = netdev_priv(dev);
1259 tun->dev = dev;
1260 tun->flags = flags;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001261 tun->txflt.count = 0;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001262 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Jason Wang54f968d2012-10-31 19:45:57 +00001264 tun->filter_attached = false;
1265 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001266
Jason Wang54f968d2012-10-31 19:45:57 +00001267 security_tun_dev_post_create(&tfile->sk);
Paul Moore2b980db2009-08-28 18:12:43 -04001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 tun_net_init(dev);
1270
Michał Mirosław88255372011-04-19 06:13:10 +00001271 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1272 TUN_USER_FEATURES;
1273 dev->features = dev->hw_features;
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 err = register_netdevice(tun->dev);
1276 if (err < 0)
Jason Wang54f968d2012-10-31 19:45:57 +00001277 goto err_free_dev;
Herbert Xu9c3fea62009-04-18 14:15:52 +00001278
David Woodhouse980c9e82009-05-09 22:54:21 -07001279 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1280 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1281 device_create_file(&tun->dev->dev, &dev_attr_group))
Joe Perches6b8a66e2011-03-02 07:18:10 +00001282 pr_err("Failed to create tun sysfs files\n");
David Woodhouse980c9e82009-05-09 22:54:21 -07001283
Eric W. Biedermana7385ba2009-01-20 10:57:48 +00001284 err = tun_attach(tun, file);
1285 if (err < 0)
Herbert Xu9c3fea62009-04-18 14:15:52 +00001286 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
Joe Perches6b8a66e2011-03-02 07:18:10 +00001289 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 if (ifr->ifr_flags & IFF_NO_PI)
1292 tun->flags |= TUN_NO_PI;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001293 else
1294 tun->flags &= ~TUN_NO_PI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1297 tun->flags |= TUN_ONE_QUEUE;
Nathaniel Filardoa26af1e2008-02-05 03:05:07 -08001298 else
1299 tun->flags &= ~TUN_ONE_QUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Rusty Russellf43798c2008-07-03 03:48:02 -07001301 if (ifr->ifr_flags & IFF_VNET_HDR)
1302 tun->flags |= TUN_VNET_HDR;
1303 else
1304 tun->flags &= ~TUN_VNET_HDR;
1305
Max Krasnyanskye35259a2008-07-10 16:59:11 -07001306 /* Make sure persistent devices do not get stuck in
1307 * xoff state.
1308 */
1309 if (netif_running(tun->dev))
1310 netif_wake_queue(tun->dev);
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 strcpy(ifr->ifr_name, tun->dev->name);
1313 return 0;
1314
1315 err_free_dev:
1316 free_netdev(dev);
1317 failed:
1318 return err;
1319}
1320
Herbert Xu876bfd42009-08-06 14:22:44 +00001321static int tun_get_iff(struct net *net, struct tun_struct *tun,
1322 struct ifreq *ifr)
Mark McLoughline3b99552008-08-15 15:09:56 -07001323{
Joe Perches6b8a66e2011-03-02 07:18:10 +00001324 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
Mark McLoughline3b99552008-08-15 15:09:56 -07001325
1326 strcpy(ifr->ifr_name, tun->dev->name);
1327
David Woodhouse980c9e82009-05-09 22:54:21 -07001328 ifr->ifr_flags = tun_flags(tun);
Mark McLoughline3b99552008-08-15 15:09:56 -07001329
1330 return 0;
1331}
1332
Rusty Russell5228ddc2008-07-03 03:46:16 -07001333/* This is like a cut-down ethtool ops, except done via tun fd so no
1334 * privs required. */
Michał Mirosław88255372011-04-19 06:13:10 +00001335static int set_offload(struct tun_struct *tun, unsigned long arg)
Rusty Russell5228ddc2008-07-03 03:46:16 -07001336{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001337 netdev_features_t features = 0;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001338
1339 if (arg & TUN_F_CSUM) {
Michał Mirosław88255372011-04-19 06:13:10 +00001340 features |= NETIF_F_HW_CSUM;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001341 arg &= ~TUN_F_CSUM;
1342
1343 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1344 if (arg & TUN_F_TSO_ECN) {
1345 features |= NETIF_F_TSO_ECN;
1346 arg &= ~TUN_F_TSO_ECN;
1347 }
1348 if (arg & TUN_F_TSO4)
1349 features |= NETIF_F_TSO;
1350 if (arg & TUN_F_TSO6)
1351 features |= NETIF_F_TSO6;
1352 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1353 }
Sridhar Samudralae36aa252009-07-14 14:21:04 +00001354
1355 if (arg & TUN_F_UFO) {
1356 features |= NETIF_F_UFO;
1357 arg &= ~TUN_F_UFO;
1358 }
Rusty Russell5228ddc2008-07-03 03:46:16 -07001359 }
1360
1361 /* This gives the user a way to test for new features in future by
1362 * trying to set them. */
1363 if (arg)
1364 return -EINVAL;
1365
Michał Mirosław88255372011-04-19 06:13:10 +00001366 tun->set_features = features;
1367 netdev_update_features(tun->dev);
Rusty Russell5228ddc2008-07-03 03:46:16 -07001368
1369 return 0;
1370}
1371
Arnd Bergmann50857e22009-11-06 22:52:32 -08001372static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1373 unsigned long arg, int ifreq_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001375 struct tun_file *tfile = file->private_data;
Eric W. Biederman631ab462009-01-20 11:00:40 +00001376 struct tun_struct *tun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 void __user* argp = (void __user*)arg;
1378 struct ifreq ifr;
Eric W. Biederman0625c882012-02-07 16:48:55 -08001379 kuid_t owner;
1380 kgid_t group;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001381 int sndbuf;
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001382 int vnet_hdr_sz;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001383 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Mathias Krausea117dac2012-07-29 19:45:14 +00001385 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
Arnd Bergmann50857e22009-11-06 22:52:32 -08001386 if (copy_from_user(&ifr, argp, ifreq_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EFAULT;
David S. Miller8bbb1812012-07-30 14:52:48 -07001388 } else {
Mathias Krausea117dac2012-07-29 19:45:14 +00001389 memset(&ifr, 0, sizeof(ifr));
David S. Miller8bbb1812012-07-30 14:52:48 -07001390 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001391 if (cmd == TUNGETFEATURES) {
1392 /* Currently this just means: "what IFF flags are valid?".
1393 * This is needed because we never checked for invalid flags on
1394 * TUNSETIFF. */
1395 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1396 IFF_VNET_HDR,
1397 (unsigned int __user*)argp);
1398 }
1399
Herbert Xu876bfd42009-08-06 14:22:44 +00001400 rtnl_lock();
1401
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001402 tun = __tun_get(tfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (cmd == TUNSETIFF && !tun) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1405
Herbert Xu876bfd42009-08-06 14:22:44 +00001406 ret = tun_set_iff(tfile->net, file, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Herbert Xu876bfd42009-08-06 14:22:44 +00001408 if (ret)
1409 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Arnd Bergmann50857e22009-11-06 22:52:32 -08001411 if (copy_to_user(argp, &ifr, ifreq_len))
Herbert Xu876bfd42009-08-06 14:22:44 +00001412 ret = -EFAULT;
1413 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
Herbert Xu876bfd42009-08-06 14:22:44 +00001416 ret = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (!tun)
Herbert Xu876bfd42009-08-06 14:22:44 +00001418 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Jason Wang1e588332012-10-31 19:45:56 +00001420 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Eric W. Biederman631ab462009-01-20 11:00:40 +00001422 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 switch (cmd) {
Mark McLoughline3b99552008-08-15 15:09:56 -07001424 case TUNGETIFF:
Herbert Xu876bfd42009-08-06 14:22:44 +00001425 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
Mark McLoughline3b99552008-08-15 15:09:56 -07001426 if (ret)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001427 break;
Mark McLoughline3b99552008-08-15 15:09:56 -07001428
Arnd Bergmann50857e22009-11-06 22:52:32 -08001429 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001430 ret = -EFAULT;
Mark McLoughline3b99552008-08-15 15:09:56 -07001431 break;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 case TUNSETNOCSUM:
1434 /* Disable/Enable checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Michał Mirosław88255372011-04-19 06:13:10 +00001436 /* [unimplemented] */
1437 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
Joe Perches6b8a66e2011-03-02 07:18:10 +00001438 arg ? "disabled" : "enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 break;
1440
1441 case TUNSETPERSIST:
Jason Wang54f968d2012-10-31 19:45:57 +00001442 /* Disable/Enable persist mode. Keep an extra reference to the
1443 * module to prevent the module being unprobed.
1444 */
1445 if (arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 tun->flags |= TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001447 __module_get(THIS_MODULE);
1448 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 tun->flags &= ~TUN_PERSIST;
Jason Wang54f968d2012-10-31 19:45:57 +00001450 module_put(THIS_MODULE);
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Joe Perches6b8a66e2011-03-02 07:18:10 +00001453 tun_debug(KERN_INFO, tun, "persist %s\n",
1454 arg ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
1456
1457 case TUNSETOWNER:
1458 /* Set owner of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001459 owner = make_kuid(current_user_ns(), arg);
1460 if (!uid_valid(owner)) {
1461 ret = -EINVAL;
1462 break;
1463 }
1464 tun->owner = owner;
Jason Wang1e588332012-10-31 19:45:56 +00001465 tun_debug(KERN_INFO, tun, "owner set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001466 from_kuid(&init_user_ns, tun->owner));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 break;
1468
Guido Guenther8c644622007-07-02 22:50:25 -07001469 case TUNSETGROUP:
1470 /* Set group of the device */
Eric W. Biederman0625c882012-02-07 16:48:55 -08001471 group = make_kgid(current_user_ns(), arg);
1472 if (!gid_valid(group)) {
1473 ret = -EINVAL;
1474 break;
1475 }
1476 tun->group = group;
Jason Wang1e588332012-10-31 19:45:56 +00001477 tun_debug(KERN_INFO, tun, "group set to %u\n",
Eric W. Biederman0625c882012-02-07 16:48:55 -08001478 from_kgid(&init_user_ns, tun->group));
Guido Guenther8c644622007-07-02 22:50:25 -07001479 break;
1480
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001481 case TUNSETLINK:
1482 /* Only allow setting the type when the interface is down */
1483 if (tun->dev->flags & IFF_UP) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001484 tun_debug(KERN_INFO, tun,
1485 "Linktype set failed because interface is up\n");
David S. Miller48abfe02008-04-23 19:37:58 -07001486 ret = -EBUSY;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001487 } else {
1488 tun->dev->type = (int) arg;
Joe Perches6b8a66e2011-03-02 07:18:10 +00001489 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1490 tun->dev->type);
David S. Miller48abfe02008-04-23 19:37:58 -07001491 ret = 0;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001492 }
Eric W. Biederman631ab462009-01-20 11:00:40 +00001493 break;
Mike Kershawff4cc3a2005-09-01 17:40:05 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495#ifdef TUN_DEBUG
1496 case TUNSETDEBUG:
1497 tun->debug = arg;
1498 break;
1499#endif
Rusty Russell5228ddc2008-07-03 03:46:16 -07001500 case TUNSETOFFLOAD:
Michał Mirosław88255372011-04-19 06:13:10 +00001501 ret = set_offload(tun, arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001502 break;
Rusty Russell5228ddc2008-07-03 03:46:16 -07001503
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001504 case TUNSETTXFILTER:
1505 /* Can be set only for TAPs */
Eric W. Biederman631ab462009-01-20 11:00:40 +00001506 ret = -EINVAL;
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001507 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
Eric W. Biederman631ab462009-01-20 11:00:40 +00001508 break;
Harvey Harrisonc0e5a8c2008-07-16 12:45:34 -07001509 ret = update_filter(&tun->txflt, (void __user *)arg);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 case SIOCGIFHWADDR:
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001513 /* Get hw address */
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001514 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1515 ifr.ifr_hwaddr.sa_family = tun->dev->type;
Arnd Bergmann50857e22009-11-06 22:52:32 -08001516 if (copy_to_user(argp, &ifr, ifreq_len))
Eric W. Biederman631ab462009-01-20 11:00:40 +00001517 ret = -EFAULT;
1518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 case SIOCSIFHWADDR:
Max Krasnyanskyf271b2cc2008-07-14 22:18:19 -07001521 /* Set hw address */
Joe Perches6b8a66e2011-03-02 07:18:10 +00001522 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1523 ifr.ifr_hwaddr.sa_data);
Kim B. Heino40102372008-02-29 12:26:21 -08001524
Kim B. Heino40102372008-02-29 12:26:21 -08001525 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001526 break;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001527
1528 case TUNGETSNDBUF:
Jason Wang54f968d2012-10-31 19:45:57 +00001529 sndbuf = tfile->socket.sk->sk_sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001530 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1531 ret = -EFAULT;
1532 break;
1533
1534 case TUNSETSNDBUF:
1535 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1536 ret = -EFAULT;
1537 break;
1538 }
1539
Jason Wang54f968d2012-10-31 19:45:57 +00001540 tun->sndbuf = tfile->socket.sk->sk_sndbuf = sndbuf;
Herbert Xu33dccbb2009-02-05 21:25:32 -08001541 break;
1542
Michael S. Tsirkind9d52b52010-03-17 17:45:01 +02001543 case TUNGETVNETHDRSZ:
1544 vnet_hdr_sz = tun->vnet_hdr_sz;
1545 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1546 ret = -EFAULT;
1547 break;
1548
1549 case TUNSETVNETHDRSZ:
1550 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1551 ret = -EFAULT;
1552 break;
1553 }
1554 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1555 ret = -EINVAL;
1556 break;
1557 }
1558
1559 tun->vnet_hdr_sz = vnet_hdr_sz;
1560 break;
1561
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001562 case TUNATTACHFILTER:
1563 /* Can be set only for TAPs */
1564 ret = -EINVAL;
1565 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1566 break;
1567 ret = -EFAULT;
Jason Wang54f968d2012-10-31 19:45:57 +00001568 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001569 break;
1570
Jason Wang54f968d2012-10-31 19:45:57 +00001571 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1572 if (!ret)
1573 tun->filter_attached = true;
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001574 break;
1575
1576 case TUNDETACHFILTER:
1577 /* Can be set only for TAPs */
1578 ret = -EINVAL;
1579 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1580 break;
Jason Wang54f968d2012-10-31 19:45:57 +00001581 ret = sk_detach_filter(tfile->socket.sk);
1582 if (!ret)
1583 tun->filter_attached = false;
Michael S. Tsirkin99405162010-02-14 01:01:10 +00001584 break;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 default:
Eric W. Biederman631ab462009-01-20 11:00:40 +00001587 ret = -EINVAL;
1588 break;
Joe Perchesee289b62010-05-17 22:47:34 -07001589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Herbert Xu876bfd42009-08-06 14:22:44 +00001591unlock:
1592 rtnl_unlock();
1593 if (tun)
1594 tun_put(tun);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001595 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Arnd Bergmann50857e22009-11-06 22:52:32 -08001598static long tun_chr_ioctl(struct file *file,
1599 unsigned int cmd, unsigned long arg)
1600{
1601 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1602}
1603
1604#ifdef CONFIG_COMPAT
1605static long tun_chr_compat_ioctl(struct file *file,
1606 unsigned int cmd, unsigned long arg)
1607{
1608 switch (cmd) {
1609 case TUNSETIFF:
1610 case TUNGETIFF:
1611 case TUNSETTXFILTER:
1612 case TUNGETSNDBUF:
1613 case TUNSETSNDBUF:
1614 case SIOCGIFHWADDR:
1615 case SIOCSIFHWADDR:
1616 arg = (unsigned long)compat_ptr(arg);
1617 break;
1618 default:
1619 arg = (compat_ulong_t)arg;
1620 break;
1621 }
1622
1623 /*
1624 * compat_ifreq is shorter than ifreq, so we must not access beyond
1625 * the end of that structure. All fields that are used in this
1626 * driver are compatible though, we don't need to convert the
1627 * contents.
1628 */
1629 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1630}
1631#endif /* CONFIG_COMPAT */
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633static int tun_chr_fasync(int fd, struct file *file, int on)
1634{
Jason Wang54f968d2012-10-31 19:45:57 +00001635 struct tun_file *tfile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int ret;
1637
Jason Wang54f968d2012-10-31 19:45:57 +00001638 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001639 goto out;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (on) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001642 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (ret)
Jonathan Corbet9d319522008-06-19 15:50:37 -06001644 goto out;
Jason Wang54f968d2012-10-31 19:45:57 +00001645 tfile->flags |= TUN_FASYNC;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001646 } else
Jason Wang54f968d2012-10-31 19:45:57 +00001647 tfile->flags &= ~TUN_FASYNC;
Jonathan Corbet9d319522008-06-19 15:50:37 -06001648 ret = 0;
1649out:
Jonathan Corbet9d319522008-06-19 15:50:37 -06001650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
1653static int tun_chr_open(struct inode *inode, struct file * file)
1654{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001655 struct tun_file *tfile;
Thomas Gleixnerdeed49f2009-10-14 01:19:46 -07001656
Joe Perches6b8a66e2011-03-02 07:18:10 +00001657 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
Eric W. Biederman631ab462009-01-20 11:00:40 +00001658
Jason Wang54f968d2012-10-31 19:45:57 +00001659 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1660 &tun_proto);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001661 if (!tfile)
1662 return -ENOMEM;
Jason Wang6e914fc2012-10-31 19:45:58 +00001663 rcu_assign_pointer(tfile->tun, NULL);
Eric W. Biederman36b50ba2009-01-20 11:01:48 +00001664 tfile->net = get_net(current->nsproxy->net_ns);
Jason Wang54f968d2012-10-31 19:45:57 +00001665 tfile->flags = 0;
1666
1667 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1668 init_waitqueue_head(&tfile->wq.wait);
1669
1670 tfile->socket.file = file;
1671 tfile->socket.ops = &tun_socket_ops;
1672
1673 sock_init_data(&tfile->socket, &tfile->sk);
1674 sk_change_net(&tfile->sk, tfile->net);
1675
1676 tfile->sk.sk_write_space = tun_sock_write_space;
1677 tfile->sk.sk_sndbuf = INT_MAX;
1678
Eric W. Biederman631ab462009-01-20 11:00:40 +00001679 file->private_data = tfile;
Jason Wang54f968d2012-10-31 19:45:57 +00001680 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return 0;
1683}
1684
1685static int tun_chr_close(struct inode *inode, struct file *file)
1686{
Eric W. Biederman631ab462009-01-20 11:00:40 +00001687 struct tun_file *tfile = file->private_data;
Eric W. Biedermanf0a4d0e2009-06-08 00:44:31 -07001688 struct tun_struct *tun;
Jason Wang54f968d2012-10-31 19:45:57 +00001689 struct net *net = tfile->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Jason Wang6e914fc2012-10-31 19:45:58 +00001691 rtnl_lock();
1692
1693 tun = rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held());
Eric W. Biederman631ab462009-01-20 11:00:40 +00001694 if (tun) {
Herbert Xud23e4362009-07-02 23:03:55 +00001695 struct net_device *dev = tun->dev;
1696
Joe Perches6b8a66e2011-03-02 07:18:10 +00001697 tun_debug(KERN_INFO, tun, "tun_chr_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Eric W. Biederman631ab462009-01-20 11:00:40 +00001699 __tun_detach(tun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Jason Wang6e914fc2012-10-31 19:45:58 +00001701 synchronize_net();
1702
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001703 /* If desirable, unregister the netdevice. */
Herbert Xud23e4362009-07-02 23:03:55 +00001704 if (!(tun->flags & TUN_PERSIST)) {
Herbert Xud23e4362009-07-02 23:03:55 +00001705 if (dev->reg_state == NETREG_REGISTERED)
1706 unregister_netdevice(dev);
Herbert Xud23e4362009-07-02 23:03:55 +00001707 }
Jason Wang54f968d2012-10-31 19:45:57 +00001708
1709 /* drop the reference that netdevice holds */
1710 sock_put(&tfile->sk);
Eric W. Biederman631ab462009-01-20 11:00:40 +00001711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Jason Wang6e914fc2012-10-31 19:45:58 +00001713 rtnl_unlock();
1714
Jason Wang54f968d2012-10-31 19:45:57 +00001715 /* drop the reference that file holds */
1716 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
1717 &tfile->socket.flags));
1718 sk_release_kernel(&tfile->sk);
1719 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 return 0;
1722}
1723
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001724static const struct file_operations tun_fops = {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001725 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001727 .read = do_sync_read,
1728 .aio_read = tun_chr_aio_read,
1729 .write = do_sync_write,
1730 .aio_write = tun_chr_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 .poll = tun_chr_poll,
Arnd Bergmann50857e22009-11-06 22:52:32 -08001732 .unlocked_ioctl = tun_chr_ioctl,
1733#ifdef CONFIG_COMPAT
1734 .compat_ioctl = tun_chr_compat_ioctl,
1735#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 .open = tun_chr_open,
1737 .release = tun_chr_close,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001738 .fasync = tun_chr_fasync
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739};
1740
1741static struct miscdevice tun_miscdev = {
1742 .minor = TUN_MINOR,
1743 .name = "tun",
Kay Sieverse454cea2009-09-18 23:01:12 +02001744 .nodename = "net/tun",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .fops = &tun_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746};
1747
1748/* ethtool interface */
1749
1750static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1751{
1752 cmd->supported = 0;
1753 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +00001754 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 cmd->duplex = DUPLEX_FULL;
1756 cmd->port = PORT_TP;
1757 cmd->phy_address = 0;
1758 cmd->transceiver = XCVR_INTERNAL;
1759 cmd->autoneg = AUTONEG_DISABLE;
1760 cmd->maxtxpkt = 0;
1761 cmd->maxrxpkt = 0;
1762 return 0;
1763}
1764
1765static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1766{
1767 struct tun_struct *tun = netdev_priv(dev);
1768
Rick Jones33a5ba12011-11-15 14:59:53 +00001769 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1770 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 switch (tun->flags & TUN_TYPE_MASK) {
1773 case TUN_TUN_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00001774 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 break;
1776 case TUN_TAP_DEV:
Rick Jones33a5ba12011-11-15 14:59:53 +00001777 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 break;
1779 }
1780}
1781
1782static u32 tun_get_msglevel(struct net_device *dev)
1783{
1784#ifdef TUN_DEBUG
1785 struct tun_struct *tun = netdev_priv(dev);
1786 return tun->debug;
1787#else
1788 return -EOPNOTSUPP;
1789#endif
1790}
1791
1792static void tun_set_msglevel(struct net_device *dev, u32 value)
1793{
1794#ifdef TUN_DEBUG
1795 struct tun_struct *tun = netdev_priv(dev);
1796 tun->debug = value;
1797#endif
1798}
1799
Jeff Garzik7282d492006-09-13 14:30:00 -04001800static const struct ethtool_ops tun_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 .get_settings = tun_get_settings,
1802 .get_drvinfo = tun_get_drvinfo,
1803 .get_msglevel = tun_get_msglevel,
1804 .set_msglevel = tun_set_msglevel,
Nolan Leakebee31362010-07-27 13:53:43 +00001805 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806};
1807
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809static int __init tun_init(void)
1810{
1811 int ret = 0;
1812
Joe Perches6b8a66e2011-03-02 07:18:10 +00001813 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1814 pr_info("%s\n", DRV_COPYRIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001816 ret = rtnl_link_register(&tun_link_ops);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001817 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001818 pr_err("Can't register link_ops\n");
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001819 goto err_linkops;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001820 }
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 ret = misc_register(&tun_miscdev);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001823 if (ret) {
Joe Perches6b8a66e2011-03-02 07:18:10 +00001824 pr_err("Can't register misc device %d\n", TUN_MINOR);
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001825 goto err_misc;
1826 }
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001827 return 0;
Pavel Emelyanov79d17602008-04-16 00:40:46 -07001828err_misc:
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001829 rtnl_link_unregister(&tun_link_ops);
1830err_linkops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return ret;
1832}
1833
1834static void tun_cleanup(void)
1835{
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001836 misc_deregister(&tun_miscdev);
Eric W. Biedermanf019a7a2009-01-21 16:02:16 -08001837 rtnl_link_unregister(&tun_link_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001840/* Get an underlying socket object from tun file. Returns error unless file is
1841 * attached to a device. The returned object works like a packet socket, it
1842 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1843 * holding a reference to the file for as long as the socket is in use. */
1844struct socket *tun_get_socket(struct file *file)
1845{
Jason Wang6e914fc2012-10-31 19:45:58 +00001846 struct tun_file *tfile;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001847 if (file->f_op != &tun_fops)
1848 return ERR_PTR(-EINVAL);
Jason Wang6e914fc2012-10-31 19:45:58 +00001849 tfile = file->private_data;
1850 if (!tfile)
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001851 return ERR_PTR(-EBADFD);
Jason Wang54f968d2012-10-31 19:45:57 +00001852 return &tfile->socket;
Michael S. Tsirkin05c28282010-01-14 06:17:09 +00001853}
1854EXPORT_SYMBOL_GPL(tun_get_socket);
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856module_init(tun_init);
1857module_exit(tun_cleanup);
1858MODULE_DESCRIPTION(DRV_DESCRIPTION);
1859MODULE_AUTHOR(DRV_COPYRIGHT);
1860MODULE_LICENSE("GPL");
1861MODULE_ALIAS_MISCDEV(TUN_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +02001862MODULE_ALIAS("devname:net/tun");