xref: /openbmc/linux/drivers/net/tun.c (revision 0110d6f22f392f976e84ab49da1b42f85b64a3c5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  TUN - Universal TUN/TAP device driver.
31da177e4SLinus Torvalds  *  Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *  it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *  the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *  (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131da177e4SLinus Torvalds  *  GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /*
191da177e4SLinus Torvalds  *  Changes:
201da177e4SLinus Torvalds  *
21ff4cc3acSMike Kershaw  *  Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22ff4cc3acSMike Kershaw  *    Add TUNSETLINK ioctl to set the link encapsulation
23ff4cc3acSMike Kershaw  *
241da177e4SLinus Torvalds  *  Mark Smith <markzzzsmith@yahoo.com.au>
251da177e4SLinus Torvalds  *    Use random_ether_addr() for tap MAC address.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Harald Roelle <harald.roelle@ifi.lmu.de>  2004/04/20
281da177e4SLinus Torvalds  *    Fixes in packet dropping, queue length setting and queue wakeup.
291da177e4SLinus Torvalds  *    Increased default tx queue length.
301da177e4SLinus Torvalds  *    Added ethtool API.
311da177e4SLinus Torvalds  *    Minor cleanups
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  *  Daniel Podlejski <underley@underley.eu.org>
341da177e4SLinus Torvalds  *    Modifications for 2.3.99-pre5 kernel.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #define DRV_NAME	"tun"
381da177e4SLinus Torvalds #define DRV_VERSION	"1.6"
391da177e4SLinus Torvalds #define DRV_DESCRIPTION	"Universal TUN/TAP device driver"
401da177e4SLinus Torvalds #define DRV_COPYRIGHT	"(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/module.h>
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/kernel.h>
451da177e4SLinus Torvalds #include <linux/major.h>
461da177e4SLinus Torvalds #include <linux/slab.h>
471da177e4SLinus Torvalds #include <linux/poll.h>
481da177e4SLinus Torvalds #include <linux/fcntl.h>
491da177e4SLinus Torvalds #include <linux/init.h>
501da177e4SLinus Torvalds #include <linux/skbuff.h>
511da177e4SLinus Torvalds #include <linux/netdevice.h>
521da177e4SLinus Torvalds #include <linux/etherdevice.h>
531da177e4SLinus Torvalds #include <linux/miscdevice.h>
541da177e4SLinus Torvalds #include <linux/ethtool.h>
551da177e4SLinus Torvalds #include <linux/rtnetlink.h>
5650857e2aSArnd Bergmann #include <linux/compat.h>
571da177e4SLinus Torvalds #include <linux/if.h>
581da177e4SLinus Torvalds #include <linux/if_arp.h>
591da177e4SLinus Torvalds #include <linux/if_ether.h>
601da177e4SLinus Torvalds #include <linux/if_tun.h>
611da177e4SLinus Torvalds #include <linux/crc32.h>
62d647a591SPavel Emelyanov #include <linux/nsproxy.h>
63f43798c2SRusty Russell #include <linux/virtio_net.h>
6499405162SMichael S. Tsirkin #include <linux/rcupdate.h>
65881d966bSEric W. Biederman #include <net/net_namespace.h>
6679d17604SPavel Emelyanov #include <net/netns/generic.h>
67f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
6833dccbb0SHerbert Xu #include <net/sock.h>
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #include <asm/system.h>
711da177e4SLinus Torvalds #include <asm/uaccess.h>
721da177e4SLinus Torvalds 
7314daa021SRusty Russell /* Uncomment to enable debugging */
7414daa021SRusty Russell /* #define TUN_DEBUG 1 */
7514daa021SRusty Russell 
761da177e4SLinus Torvalds #ifdef TUN_DEBUG
771da177e4SLinus Torvalds static int debug;
7814daa021SRusty Russell 
7914daa021SRusty Russell #define DBG  if(tun->debug)printk
8014daa021SRusty Russell #define DBG1 if(debug==2)printk
8114daa021SRusty Russell #else
8214daa021SRusty Russell #define DBG( a... )
8314daa021SRusty Russell #define DBG1( a... )
841da177e4SLinus Torvalds #endif
851da177e4SLinus Torvalds 
86f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
87f271b2ccSMax Krasnyansky struct tap_filter {
88f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
89f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
90f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
91f271b2ccSMax Krasnyansky };
92f271b2ccSMax Krasnyansky 
93631ab46bSEric W. Biederman struct tun_file {
94c70f1829SEric W. Biederman 	atomic_t count;
95631ab46bSEric W. Biederman 	struct tun_struct *tun;
9636b50babSEric W. Biederman 	struct net *net;
97631ab46bSEric W. Biederman };
98631ab46bSEric W. Biederman 
9933dccbb0SHerbert Xu struct tun_sock;
10033dccbb0SHerbert Xu 
10114daa021SRusty Russell struct tun_struct {
102631ab46bSEric W. Biederman 	struct tun_file		*tfile;
103f271b2ccSMax Krasnyansky 	unsigned int 		flags;
10414daa021SRusty Russell 	uid_t			owner;
10514daa021SRusty Russell 	gid_t			group;
10614daa021SRusty Russell 
10714daa021SRusty Russell 	struct net_device	*dev;
10814daa021SRusty Russell 	struct fasync_struct	*fasync;
10914daa021SRusty Russell 
110f271b2ccSMax Krasnyansky 	struct tap_filter       txflt;
11133dccbb0SHerbert Xu 	struct socket		socket;
11214daa021SRusty Russell 
11314daa021SRusty Russell #ifdef TUN_DEBUG
11414daa021SRusty Russell 	int debug;
11514daa021SRusty Russell #endif
11614daa021SRusty Russell };
11714daa021SRusty Russell 
11833dccbb0SHerbert Xu struct tun_sock {
11933dccbb0SHerbert Xu 	struct sock		sk;
12033dccbb0SHerbert Xu 	struct tun_struct	*tun;
12133dccbb0SHerbert Xu };
12233dccbb0SHerbert Xu 
12333dccbb0SHerbert Xu static inline struct tun_sock *tun_sk(struct sock *sk)
12433dccbb0SHerbert Xu {
12533dccbb0SHerbert Xu 	return container_of(sk, struct tun_sock, sk);
12633dccbb0SHerbert Xu }
12733dccbb0SHerbert Xu 
128a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
129a7385ba2SEric W. Biederman {
130631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
13138231b7aSEric W. Biederman 	int err;
132a7385ba2SEric W. Biederman 
133a7385ba2SEric W. Biederman 	ASSERT_RTNL();
134a7385ba2SEric W. Biederman 
13538231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
13638231b7aSEric W. Biederman 
13738231b7aSEric W. Biederman 	err = -EINVAL;
13838231b7aSEric W. Biederman 	if (tfile->tun)
13938231b7aSEric W. Biederman 		goto out;
14038231b7aSEric W. Biederman 
14138231b7aSEric W. Biederman 	err = -EBUSY;
14238231b7aSEric W. Biederman 	if (tun->tfile)
14338231b7aSEric W. Biederman 		goto out;
14438231b7aSEric W. Biederman 
14538231b7aSEric W. Biederman 	err = 0;
146631ab46bSEric W. Biederman 	tfile->tun = tun;
147631ab46bSEric W. Biederman 	tun->tfile = tfile;
14805c2828cSMichael S. Tsirkin 	tun->socket.file = file;
149c70f1829SEric W. Biederman 	dev_hold(tun->dev);
15089f56d1eSMichael S. Tsirkin 	sock_hold(tun->socket.sk);
151c70f1829SEric W. Biederman 	atomic_inc(&tfile->count);
152a7385ba2SEric W. Biederman 
15338231b7aSEric W. Biederman out:
15438231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
15538231b7aSEric W. Biederman 	return err;
156a7385ba2SEric W. Biederman }
157a7385ba2SEric W. Biederman 
158631ab46bSEric W. Biederman static void __tun_detach(struct tun_struct *tun)
159631ab46bSEric W. Biederman {
160631ab46bSEric W. Biederman 	/* Detach from net device */
16138231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
162631ab46bSEric W. Biederman 	tun->tfile = NULL;
16305c2828cSMichael S. Tsirkin 	tun->socket.file = NULL;
16438231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
165631ab46bSEric W. Biederman 
166631ab46bSEric W. Biederman 	/* Drop read queue */
16789f56d1eSMichael S. Tsirkin 	skb_queue_purge(&tun->socket.sk->sk_receive_queue);
168c70f1829SEric W. Biederman 
169c70f1829SEric W. Biederman 	/* Drop the extra count on the net device */
170c70f1829SEric W. Biederman 	dev_put(tun->dev);
171c70f1829SEric W. Biederman }
172c70f1829SEric W. Biederman 
173c70f1829SEric W. Biederman static void tun_detach(struct tun_struct *tun)
174c70f1829SEric W. Biederman {
175c70f1829SEric W. Biederman 	rtnl_lock();
176c70f1829SEric W. Biederman 	__tun_detach(tun);
177c70f1829SEric W. Biederman 	rtnl_unlock();
178631ab46bSEric W. Biederman }
179631ab46bSEric W. Biederman 
180631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
181631ab46bSEric W. Biederman {
182c70f1829SEric W. Biederman 	struct tun_struct *tun = NULL;
183c70f1829SEric W. Biederman 
184c70f1829SEric W. Biederman 	if (atomic_inc_not_zero(&tfile->count))
185c70f1829SEric W. Biederman 		tun = tfile->tun;
186c70f1829SEric W. Biederman 
187c70f1829SEric W. Biederman 	return tun;
188631ab46bSEric W. Biederman }
189631ab46bSEric W. Biederman 
190631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
191631ab46bSEric W. Biederman {
192631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
193631ab46bSEric W. Biederman }
194631ab46bSEric W. Biederman 
195631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
196631ab46bSEric W. Biederman {
197c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
198c70f1829SEric W. Biederman 
199c70f1829SEric W. Biederman 	if (atomic_dec_and_test(&tfile->count))
200c70f1829SEric W. Biederman 		tun_detach(tfile->tun);
201631ab46bSEric W. Biederman }
202631ab46bSEric W. Biederman 
203f271b2ccSMax Krasnyansky /* TAP filterting */
204f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
205f271b2ccSMax Krasnyansky {
206f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
207f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
208f271b2ccSMax Krasnyansky }
209f271b2ccSMax Krasnyansky 
210f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
211f271b2ccSMax Krasnyansky {
212f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
213f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
214f271b2ccSMax Krasnyansky }
215f271b2ccSMax Krasnyansky 
216f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
217f271b2ccSMax Krasnyansky {
218f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
219f271b2ccSMax Krasnyansky 	struct tun_filter uf;
220f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
221f271b2ccSMax Krasnyansky 
222f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
223f271b2ccSMax Krasnyansky 		return -EFAULT;
224f271b2ccSMax Krasnyansky 
225f271b2ccSMax Krasnyansky 	if (!uf.count) {
226f271b2ccSMax Krasnyansky 		/* Disabled */
227f271b2ccSMax Krasnyansky 		filter->count = 0;
228f271b2ccSMax Krasnyansky 		return 0;
229f271b2ccSMax Krasnyansky 	}
230f271b2ccSMax Krasnyansky 
231f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
232f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
233f271b2ccSMax Krasnyansky 	if (!addr)
234f271b2ccSMax Krasnyansky 		return -ENOMEM;
235f271b2ccSMax Krasnyansky 
236f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
237f271b2ccSMax Krasnyansky 		err = -EFAULT;
238f271b2ccSMax Krasnyansky 		goto done;
239f271b2ccSMax Krasnyansky 	}
240f271b2ccSMax Krasnyansky 
241f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
242f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
243f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
244f271b2ccSMax Krasnyansky 	filter->count = 0;
245f271b2ccSMax Krasnyansky 	wmb();
246f271b2ccSMax Krasnyansky 
247f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
248f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
249f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
250f271b2ccSMax Krasnyansky 
251f271b2ccSMax Krasnyansky 	nexact = n;
252f271b2ccSMax Krasnyansky 
253cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
254cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
255f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
256cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
257cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
258cfbf84fcSAlex Williamson 			err = 0; /* no filter */
259cfbf84fcSAlex Williamson 			goto done;
260cfbf84fcSAlex Williamson 		}
261f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
262cfbf84fcSAlex Williamson 	}
263f271b2ccSMax Krasnyansky 
264f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
265f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
266f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
267f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
268f271b2ccSMax Krasnyansky 
269f271b2ccSMax Krasnyansky 	/* Now enable the filter */
270f271b2ccSMax Krasnyansky 	wmb();
271f271b2ccSMax Krasnyansky 	filter->count = nexact;
272f271b2ccSMax Krasnyansky 
273f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
274f271b2ccSMax Krasnyansky 	err = nexact;
275f271b2ccSMax Krasnyansky 
276f271b2ccSMax Krasnyansky done:
277f271b2ccSMax Krasnyansky 	kfree(addr);
278f271b2ccSMax Krasnyansky 	return err;
279f271b2ccSMax Krasnyansky }
280f271b2ccSMax Krasnyansky 
281f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
282f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
283f271b2ccSMax Krasnyansky {
284f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
285f271b2ccSMax Krasnyansky 	 * at this point. */
286f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
287f271b2ccSMax Krasnyansky 	int i;
288f271b2ccSMax Krasnyansky 
289f271b2ccSMax Krasnyansky 	/* Exact match */
290f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
291f271b2ccSMax Krasnyansky 		if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
292f271b2ccSMax Krasnyansky 			return 1;
293f271b2ccSMax Krasnyansky 
294f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
295f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
296f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
297f271b2ccSMax Krasnyansky 
298f271b2ccSMax Krasnyansky 	return 0;
299f271b2ccSMax Krasnyansky }
300f271b2ccSMax Krasnyansky 
301f271b2ccSMax Krasnyansky /*
302f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
303f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
304f271b2ccSMax Krasnyansky  */
305f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
306f271b2ccSMax Krasnyansky {
307f271b2ccSMax Krasnyansky 	if (!filter->count)
308f271b2ccSMax Krasnyansky 		return 1;
309f271b2ccSMax Krasnyansky 
310f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
311f271b2ccSMax Krasnyansky }
312f271b2ccSMax Krasnyansky 
3131da177e4SLinus Torvalds /* Network device part of the driver */
3141da177e4SLinus Torvalds 
3157282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
3161da177e4SLinus Torvalds 
317c70f1829SEric W. Biederman /* Net device detach from fd. */
318c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
319c70f1829SEric W. Biederman {
320c70f1829SEric W. Biederman 	struct tun_struct *tun = netdev_priv(dev);
321c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
322c70f1829SEric W. Biederman 
323c70f1829SEric W. Biederman 	/* Inform the methods they need to stop using the dev.
324c70f1829SEric W. Biederman 	 */
325c70f1829SEric W. Biederman 	if (tfile) {
326c40af84aSHerbert Xu 		wake_up_all(&tun->socket.wait);
327c70f1829SEric W. Biederman 		if (atomic_dec_and_test(&tfile->count))
328c70f1829SEric W. Biederman 			__tun_detach(tun);
329c70f1829SEric W. Biederman 	}
330c70f1829SEric W. Biederman }
331c70f1829SEric W. Biederman 
3329c3fea6aSHerbert Xu static void tun_free_netdev(struct net_device *dev)
3339c3fea6aSHerbert Xu {
3349c3fea6aSHerbert Xu 	struct tun_struct *tun = netdev_priv(dev);
3359c3fea6aSHerbert Xu 
33689f56d1eSMichael S. Tsirkin 	sock_put(tun->socket.sk);
3379c3fea6aSHerbert Xu }
3389c3fea6aSHerbert Xu 
3391da177e4SLinus Torvalds /* Net device open. */
3401da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	netif_start_queue(dev);
3431da177e4SLinus Torvalds 	return 0;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /* Net device close. */
3471da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	netif_stop_queue(dev);
3501da177e4SLinus Torvalds 	return 0;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /* Net device start xmit */
354424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
361631ab46bSEric W. Biederman 	if (!tun->tfile)
3621da177e4SLinus Torvalds 		goto drop;
3631da177e4SLinus Torvalds 
364f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
365f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
366f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
367f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
368f271b2ccSMax Krasnyansky 		goto drop;
369f271b2ccSMax Krasnyansky 
37099405162SMichael S. Tsirkin 	if (tun->socket.sk->sk_filter &&
37199405162SMichael S. Tsirkin 	    sk_filter(tun->socket.sk, skb))
37299405162SMichael S. Tsirkin 		goto drop;
37399405162SMichael S. Tsirkin 
37489f56d1eSMichael S. Tsirkin 	if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
3751da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
3761da177e4SLinus Torvalds 			/* Normal queueing mode. */
3771da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
3781da177e4SLinus Torvalds 			netif_stop_queue(dev);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
3811da177e4SLinus Torvalds 			 * error is more appropriate. */
38209f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
3831da177e4SLinus Torvalds 		} else {
3841da177e4SLinus Torvalds 			/* Single queue mode.
3851da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
3861da177e4SLinus Torvalds 			goto drop;
3871da177e4SLinus Torvalds 		}
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
390*0110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
391*0110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
392*0110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
393*0110d6f2SMichael S. Tsirkin 
394f271b2ccSMax Krasnyansky 	/* Enqueue packet */
39589f56d1eSMichael S. Tsirkin 	skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
3961da177e4SLinus Torvalds 	dev->trans_start = jiffies;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	/* Notify and wake up reader process */
3991da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
4001da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
40105c2828cSMichael S. Tsirkin 	wake_up_interruptible_poll(&tun->socket.wait, POLLIN |
40205c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
4036ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds drop:
40609f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
4071da177e4SLinus Torvalds 	kfree_skb(skb);
4086ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
411f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
4121da177e4SLinus Torvalds {
413f271b2ccSMax Krasnyansky 	/*
414f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
415f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
416f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
417f271b2ccSMax Krasnyansky 	 */
418f271b2ccSMax Krasnyansky 	return;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4214885a504SEd Swierk #define MIN_MTU 68
4224885a504SEd Swierk #define MAX_MTU 65535
4234885a504SEd Swierk 
4244885a504SEd Swierk static int
4254885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
4264885a504SEd Swierk {
4274885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
4284885a504SEd Swierk 		return -EINVAL;
4294885a504SEd Swierk 	dev->mtu = new_mtu;
4304885a504SEd Swierk 	return 0;
4314885a504SEd Swierk }
4324885a504SEd Swierk 
433758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
434c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
435758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
436758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
43700829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
438758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
439758e43b7SStephen Hemminger };
440758e43b7SStephen Hemminger 
441758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
442c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
443758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
444758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
44500829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
446758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
447758e43b7SStephen Hemminger 	.ndo_set_multicast_list	= tun_net_mclist,
448758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
449758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
450758e43b7SStephen Hemminger };
451758e43b7SStephen Hemminger 
4521da177e4SLinus Torvalds /* Initialize net device. */
4531da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
4581da177e4SLinus Torvalds 	case TUN_TUN_DEV:
459758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
460758e43b7SStephen Hemminger 
4611da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
4621da177e4SLinus Torvalds 		dev->hard_header_len = 0;
4631da177e4SLinus Torvalds 		dev->addr_len = 0;
4641da177e4SLinus Torvalds 		dev->mtu = 1500;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 		/* Zero header length */
4671da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
4681da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
4691da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
4701da177e4SLinus Torvalds 		break;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	case TUN_TAP_DEV:
4737a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
4741da177e4SLinus Torvalds 		/* Ethernet TAP Device */
4751da177e4SLinus Torvalds 		ether_setup(dev);
47636226a8dSBrian Braunstein 
477f271b2ccSMax Krasnyansky 		random_ether_addr(dev->dev_addr);
47836226a8dSBrian Braunstein 
4791da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
4801da177e4SLinus Torvalds 		break;
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds /* Character device part */
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /* Poll */
4871da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
4881da177e4SLinus Torvalds {
489b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
490b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
4913c8a9c63SMariusz Kozlowski 	struct sock *sk;
49233dccbb0SHerbert Xu 	unsigned int mask = 0;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	if (!tun)
495eac9e902SEric W. Biederman 		return POLLERR;
4961da177e4SLinus Torvalds 
49789f56d1eSMichael S. Tsirkin 	sk = tun->socket.sk;
4983c8a9c63SMariusz Kozlowski 
4991da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
5001da177e4SLinus Torvalds 
501c40af84aSHerbert Xu 	poll_wait(file, &tun->socket.wait, wait);
5021da177e4SLinus Torvalds 
50389f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
5041da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5051da177e4SLinus Torvalds 
50633dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
50733dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
50833dccbb0SHerbert Xu 	     sock_writeable(sk)))
50933dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
51033dccbb0SHerbert Xu 
511c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
512c70f1829SEric W. Biederman 		mask = POLLERR;
513c70f1829SEric W. Biederman 
514631ab46bSEric W. Biederman 	tun_put(tun);
5151da177e4SLinus Torvalds 	return mask;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
518f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
519f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
52033dccbb0SHerbert Xu static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
52133dccbb0SHerbert Xu 					    size_t prepad, size_t len,
52233dccbb0SHerbert Xu 					    size_t linear, int noblock)
523f42157cbSRusty Russell {
52489f56d1eSMichael S. Tsirkin 	struct sock *sk = tun->socket.sk;
525f42157cbSRusty Russell 	struct sk_buff *skb;
52633dccbb0SHerbert Xu 	int err;
527f42157cbSRusty Russell 
528f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
5290eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
53033dccbb0SHerbert Xu 		linear = len;
531f42157cbSRusty Russell 
53233dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
53333dccbb0SHerbert Xu 				   &err);
534f42157cbSRusty Russell 	if (!skb)
53533dccbb0SHerbert Xu 		return ERR_PTR(err);
536f42157cbSRusty Russell 
537f42157cbSRusty Russell 	skb_reserve(skb, prepad);
538f42157cbSRusty Russell 	skb_put(skb, linear);
53933dccbb0SHerbert Xu 	skb->data_len = len - linear;
54033dccbb0SHerbert Xu 	skb->len += len - linear;
541f42157cbSRusty Russell 
542f42157cbSRusty Russell 	return skb;
543f42157cbSRusty Russell }
544f42157cbSRusty Russell 
5451da177e4SLinus Torvalds /* Get packet from user space buffer */
54633dccbb0SHerbert Xu static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
5476f26c9a7SMichael S. Tsirkin 				       const struct iovec *iv, size_t count,
54833dccbb0SHerbert Xu 				       int noblock)
5491da177e4SLinus Torvalds {
55009640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
5511da177e4SLinus Torvalds 	struct sk_buff *skb;
5521da177e4SLinus Torvalds 	size_t len = count, align = 0;
553f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
5546f26c9a7SMichael S. Tsirkin 	int offset = 0;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
5571da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
5581da177e4SLinus Torvalds 			return -EINVAL;
5591da177e4SLinus Torvalds 
5606f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
5611da177e4SLinus Torvalds 			return -EFAULT;
5626f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 
565f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
566f43798c2SRusty Russell 		if ((len -= sizeof(gso)) > count)
567f43798c2SRusty Russell 			return -EINVAL;
568f43798c2SRusty Russell 
5696f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
570f43798c2SRusty Russell 			return -EFAULT;
571f43798c2SRusty Russell 
5724909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
5734909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
5744909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
5754909122fSHerbert Xu 
576f43798c2SRusty Russell 		if (gso.hdr_len > len)
577f43798c2SRusty Russell 			return -EINVAL;
5786f536f40SSridhar Samudrala 		offset += sizeof(gso);
579f43798c2SRusty Russell 	}
580f43798c2SRusty Russell 
581e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
5821da177e4SLinus Torvalds 		align = NET_IP_ALIGN;
5830eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
5840eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
585e01bf1c8SRusty Russell 			return -EINVAL;
586e01bf1c8SRusty Russell 	}
5871da177e4SLinus Torvalds 
58833dccbb0SHerbert Xu 	skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
58933dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
59033dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
59109f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
59233dccbb0SHerbert Xu 		return PTR_ERR(skb);
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 
5956f26c9a7SMichael S. Tsirkin 	if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
59609f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
5978f22757eSDave Jones 		kfree_skb(skb);
5981da177e4SLinus Torvalds 		return -EFAULT;
5998f22757eSDave Jones 	}
6001da177e4SLinus Torvalds 
601f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
602f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
603f43798c2SRusty Russell 					  gso.csum_offset)) {
604f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
605f43798c2SRusty Russell 			kfree_skb(skb);
606f43798c2SRusty Russell 			return -EINVAL;
607f43798c2SRusty Russell 		}
608f43798c2SRusty Russell 	} else if (tun->flags & TUN_NOCHECKSUM)
609f43798c2SRusty Russell 		skb->ip_summed = CHECKSUM_UNNECESSARY;
610f43798c2SRusty Russell 
6111da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
6121da177e4SLinus Torvalds 	case TUN_TUN_DEV:
613f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
614f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
615f09f7ee2SAng Way Chuang 			case 0x40:
616f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
617f09f7ee2SAng Way Chuang 				break;
618f09f7ee2SAng Way Chuang 			case 0x60:
619f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
620f09f7ee2SAng Way Chuang 				break;
621f09f7ee2SAng Way Chuang 			default:
622f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
623f09f7ee2SAng Way Chuang 				kfree_skb(skb);
624f09f7ee2SAng Way Chuang 				return -EINVAL;
625f09f7ee2SAng Way Chuang 			}
626f09f7ee2SAng Way Chuang 		}
627f09f7ee2SAng Way Chuang 
628459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
6291da177e4SLinus Torvalds 		skb->protocol = pi.proto;
6304c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
6311da177e4SLinus Torvalds 		break;
6321da177e4SLinus Torvalds 	case TUN_TAP_DEV:
6331da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
6341da177e4SLinus Torvalds 		break;
6351da177e4SLinus Torvalds 	};
6361da177e4SLinus Torvalds 
637f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
638f43798c2SRusty Russell 		pr_debug("GSO!\n");
639f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
640f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
641f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
642f43798c2SRusty Russell 			break;
643f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
644f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
645f43798c2SRusty Russell 			break;
646e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
647e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
648e36aa25aSSridhar Samudrala 			break;
649f43798c2SRusty Russell 		default:
650f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
651f43798c2SRusty Russell 			kfree_skb(skb);
652f43798c2SRusty Russell 			return -EINVAL;
653f43798c2SRusty Russell 		}
654f43798c2SRusty Russell 
655f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
656f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
657f43798c2SRusty Russell 
658f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
659f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
660f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
661f43798c2SRusty Russell 			kfree_skb(skb);
662f43798c2SRusty Russell 			return -EINVAL;
663f43798c2SRusty Russell 		}
664f43798c2SRusty Russell 
665f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
666f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
667f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
668f43798c2SRusty Russell 	}
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	netif_rx_ni(skb);
6711da177e4SLinus Torvalds 
67209f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
67309f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	return count;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
678ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
679ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
6801da177e4SLinus Torvalds {
68133dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
682ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
683631ab46bSEric W. Biederman 	ssize_t result;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	if (!tun)
6861da177e4SLinus Torvalds 		return -EBADFD;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
6891da177e4SLinus Torvalds 
6906f26c9a7SMichael S. Tsirkin 	result = tun_get_user(tun, iv, iov_length(iv, count),
69133dccbb0SHerbert Xu 			      file->f_flags & O_NONBLOCK);
692631ab46bSEric W. Biederman 
693631ab46bSEric W. Biederman 	tun_put(tun);
694631ab46bSEric W. Biederman 	return result;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds /* Put packet to the user space buffer */
6981da177e4SLinus Torvalds static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
6991da177e4SLinus Torvalds 				       struct sk_buff *skb,
70043b39dcdSMichael S. Tsirkin 				       const struct iovec *iv, int len)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
7031da177e4SLinus Torvalds 	ssize_t total = 0;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
7061da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
7071da177e4SLinus Torvalds 			return -EINVAL;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 		if (len < skb->len) {
7101da177e4SLinus Torvalds 			/* Packet will be striped */
7111da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
7121da177e4SLinus Torvalds 		}
7131da177e4SLinus Torvalds 
71443b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
7151da177e4SLinus Torvalds 			return -EFAULT;
7161da177e4SLinus Torvalds 		total += sizeof(pi);
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 
719f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
720f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
721f43798c2SRusty Russell 		if ((len -= sizeof(gso)) < 0)
722f43798c2SRusty Russell 			return -EINVAL;
723f43798c2SRusty Russell 
724f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
725f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
726f43798c2SRusty Russell 
727f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
728f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
729f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
730f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
731f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
732f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
733f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
734e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
735e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
736f43798c2SRusty Russell 			else
737f43798c2SRusty Russell 				BUG();
738f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
739f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
740f43798c2SRusty Russell 		} else
741f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
742f43798c2SRusty Russell 
743f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
744f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
745f43798c2SRusty Russell 			gso.csum_start = skb->csum_start - skb_headroom(skb);
746f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
747f43798c2SRusty Russell 		} /* else everything is zero */
748f43798c2SRusty Russell 
74943b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
75043b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
751f43798c2SRusty Russell 			return -EFAULT;
752f43798c2SRusty Russell 		total += sizeof(gso);
753f43798c2SRusty Russell 	}
754f43798c2SRusty Russell 
7551da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
7561da177e4SLinus Torvalds 
75743b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
75805c2828cSMichael S. Tsirkin 	total += skb->len;
7591da177e4SLinus Torvalds 
76009f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
76109f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	return total;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
76605c2828cSMichael S. Tsirkin static ssize_t tun_do_read(struct tun_struct *tun,
76705c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
76805c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
7711da177e4SLinus Torvalds 	struct sk_buff *skb;
77205c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
7751da177e4SLinus Torvalds 
776c40af84aSHerbert Xu 	add_wait_queue(&tun->socket.wait, &wait);
7771da177e4SLinus Torvalds 	while (len) {
7781da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 		/* Read frames from the queue */
78189f56d1eSMichael S. Tsirkin 		if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
78205c2828cSMichael S. Tsirkin 			if (noblock) {
7831da177e4SLinus Torvalds 				ret = -EAGAIN;
7841da177e4SLinus Torvalds 				break;
7851da177e4SLinus Torvalds 			}
7861da177e4SLinus Torvalds 			if (signal_pending(current)) {
7871da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
7881da177e4SLinus Torvalds 				break;
7891da177e4SLinus Torvalds 			}
790c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
791c70f1829SEric W. Biederman 				ret = -EIO;
792c70f1829SEric W. Biederman 				break;
793c70f1829SEric W. Biederman 			}
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
7961da177e4SLinus Torvalds 			schedule();
7971da177e4SLinus Torvalds 			continue;
7981da177e4SLinus Torvalds 		}
7991da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
8001da177e4SLinus Torvalds 
80143b39dcdSMichael S. Tsirkin 		ret = tun_put_user(tun, skb, iv, len);
8021da177e4SLinus Torvalds 		kfree_skb(skb);
8031da177e4SLinus Torvalds 		break;
8041da177e4SLinus Torvalds 	}
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
807c40af84aSHerbert Xu 	remove_wait_queue(&tun->socket.wait, &wait);
8081da177e4SLinus Torvalds 
80905c2828cSMichael S. Tsirkin 	return ret;
81005c2828cSMichael S. Tsirkin }
81105c2828cSMichael S. Tsirkin 
81205c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
81305c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
81405c2828cSMichael S. Tsirkin {
81505c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
81605c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
81705c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
81805c2828cSMichael S. Tsirkin 	ssize_t len, ret;
81905c2828cSMichael S. Tsirkin 
82005c2828cSMichael S. Tsirkin 	if (!tun)
82105c2828cSMichael S. Tsirkin 		return -EBADFD;
82205c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
82305c2828cSMichael S. Tsirkin 	if (len < 0) {
82405c2828cSMichael S. Tsirkin 		ret = -EINVAL;
82505c2828cSMichael S. Tsirkin 		goto out;
82605c2828cSMichael S. Tsirkin 	}
82705c2828cSMichael S. Tsirkin 
82805c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
82905c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
830631ab46bSEric W. Biederman out:
831631ab46bSEric W. Biederman 	tun_put(tun);
8321da177e4SLinus Torvalds 	return ret;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	tun->owner = -1;
8408c644623SGuido Guenther 	tun->group = -1;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
8439c3fea6aSHerbert Xu 	dev->destructor = tun_free_netdev;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
846f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
847f019a7a5SEric W. Biederman  * device with netlink.
848f019a7a5SEric W. Biederman  */
849f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
850f019a7a5SEric W. Biederman {
851f019a7a5SEric W. Biederman 	return -EINVAL;
852f019a7a5SEric W. Biederman }
853f019a7a5SEric W. Biederman 
854f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
855f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
856f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
857f019a7a5SEric W. Biederman 	.setup		= tun_setup,
858f019a7a5SEric W. Biederman 	.validate	= tun_validate,
859f019a7a5SEric W. Biederman };
860f019a7a5SEric W. Biederman 
86133dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
86233dccbb0SHerbert Xu {
86333dccbb0SHerbert Xu 	struct tun_struct *tun;
86433dccbb0SHerbert Xu 
86533dccbb0SHerbert Xu 	if (!sock_writeable(sk))
86633dccbb0SHerbert Xu 		return;
86733dccbb0SHerbert Xu 
86833dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
86933dccbb0SHerbert Xu 		return;
87033dccbb0SHerbert Xu 
871c722c625SHerbert Xu 	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
87205c2828cSMichael S. Tsirkin 		wake_up_interruptible_sync_poll(sk->sk_sleep, POLLOUT |
87305c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
874c722c625SHerbert Xu 
87580924e5fSVitaliy Gusev 	tun = tun_sk(sk)->tun;
87633dccbb0SHerbert Xu 	kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
87733dccbb0SHerbert Xu }
87833dccbb0SHerbert Xu 
87933dccbb0SHerbert Xu static void tun_sock_destruct(struct sock *sk)
88033dccbb0SHerbert Xu {
88180924e5fSVitaliy Gusev 	free_netdev(tun_sk(sk)->tun->dev);
88233dccbb0SHerbert Xu }
88333dccbb0SHerbert Xu 
88405c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
88505c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
88605c2828cSMichael S. Tsirkin {
88705c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
88805c2828cSMichael S. Tsirkin 	return tun_get_user(tun, m->msg_iov, total_len,
88905c2828cSMichael S. Tsirkin 			    m->msg_flags & MSG_DONTWAIT);
89005c2828cSMichael S. Tsirkin }
89105c2828cSMichael S. Tsirkin 
89205c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
89305c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
89405c2828cSMichael S. Tsirkin 		       int flags)
89505c2828cSMichael S. Tsirkin {
89605c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
89705c2828cSMichael S. Tsirkin 	int ret;
89805c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
89905c2828cSMichael S. Tsirkin 		return -EINVAL;
90005c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
90105c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
90205c2828cSMichael S. Tsirkin 	if (ret > total_len) {
90305c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
90405c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
90505c2828cSMichael S. Tsirkin 	}
90605c2828cSMichael S. Tsirkin 	return ret;
90705c2828cSMichael S. Tsirkin }
90805c2828cSMichael S. Tsirkin 
90905c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
91005c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
91105c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
91205c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
91305c2828cSMichael S. Tsirkin };
91405c2828cSMichael S. Tsirkin 
91533dccbb0SHerbert Xu static struct proto tun_proto = {
91633dccbb0SHerbert Xu 	.name		= "tun",
91733dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
91833dccbb0SHerbert Xu 	.obj_size	= sizeof(struct tun_sock),
91933dccbb0SHerbert Xu };
920f019a7a5SEric W. Biederman 
921980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
922980c9e8cSDavid Woodhouse {
923980c9e8cSDavid Woodhouse 	int flags = 0;
924980c9e8cSDavid Woodhouse 
925980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
926980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
927980c9e8cSDavid Woodhouse 	else
928980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
929980c9e8cSDavid Woodhouse 
930980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
931980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
932980c9e8cSDavid Woodhouse 
933980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
934980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
935980c9e8cSDavid Woodhouse 
936980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
937980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
938980c9e8cSDavid Woodhouse 
939980c9e8cSDavid Woodhouse 	return flags;
940980c9e8cSDavid Woodhouse }
941980c9e8cSDavid Woodhouse 
942980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
943980c9e8cSDavid Woodhouse 			      char *buf)
944980c9e8cSDavid Woodhouse {
945980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
946980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
947980c9e8cSDavid Woodhouse }
948980c9e8cSDavid Woodhouse 
949980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
950980c9e8cSDavid Woodhouse 			      char *buf)
951980c9e8cSDavid Woodhouse {
952980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
953980c9e8cSDavid Woodhouse 	return sprintf(buf, "%d\n", tun->owner);
954980c9e8cSDavid Woodhouse }
955980c9e8cSDavid Woodhouse 
956980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
957980c9e8cSDavid Woodhouse 			      char *buf)
958980c9e8cSDavid Woodhouse {
959980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
960980c9e8cSDavid Woodhouse 	return sprintf(buf, "%d\n", tun->group);
961980c9e8cSDavid Woodhouse }
962980c9e8cSDavid Woodhouse 
963980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
964980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
965980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
966980c9e8cSDavid Woodhouse 
967d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
9681da177e4SLinus Torvalds {
96933dccbb0SHerbert Xu 	struct sock *sk;
9701da177e4SLinus Torvalds 	struct tun_struct *tun;
9711da177e4SLinus Torvalds 	struct net_device *dev;
9721da177e4SLinus Torvalds 	int err;
9731da177e4SLinus Torvalds 
97474a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
97574a3e5a7SEric W. Biederman 	if (dev) {
9762b980dbdSPaul Moore 		const struct cred *cred = current_cred();
9772b980dbdSPaul Moore 
978f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
979f85ba780SDavid Woodhouse 			return -EBUSY;
98074a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
98174a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
98274a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
98374a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
98474a3e5a7SEric W. Biederman 		else
98574a3e5a7SEric W. Biederman 			return -EINVAL;
98674a3e5a7SEric W. Biederman 
9872b980dbdSPaul Moore 		if (((tun->owner != -1 && cred->euid != tun->owner) ||
9882b980dbdSPaul Moore 		     (tun->group != -1 && !in_egroup_p(tun->group))) &&
9892b980dbdSPaul Moore 		    !capable(CAP_NET_ADMIN))
9902b980dbdSPaul Moore 			return -EPERM;
991d7e9660aSLinus Torvalds 		err = security_tun_dev_attach(tun->socket.sk);
9922b980dbdSPaul Moore 		if (err < 0)
9932b980dbdSPaul Moore 			return err;
9942b980dbdSPaul Moore 
995a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
996a7385ba2SEric W. Biederman 		if (err < 0)
997a7385ba2SEric W. Biederman 			return err;
99886a264abSDavid Howells 	}
9991da177e4SLinus Torvalds 	else {
10001da177e4SLinus Torvalds 		char *name;
10011da177e4SLinus Torvalds 		unsigned long flags = 0;
10021da177e4SLinus Torvalds 
1003ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
1004ca6bb5d7SDavid Woodhouse 			return -EPERM;
10052b980dbdSPaul Moore 		err = security_tun_dev_create();
10062b980dbdSPaul Moore 		if (err < 0)
10072b980dbdSPaul Moore 			return err;
1008ca6bb5d7SDavid Woodhouse 
10091da177e4SLinus Torvalds 		/* Set dev type */
10101da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
10111da177e4SLinus Torvalds 			/* TUN device */
10121da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
10131da177e4SLinus Torvalds 			name = "tun%d";
10141da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
10151da177e4SLinus Torvalds 			/* TAP device */
10161da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
10171da177e4SLinus Torvalds 			name = "tap%d";
10181da177e4SLinus Torvalds 		} else
101936989b90SKusanagi Kouichi 			return -EINVAL;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 		if (*ifr->ifr_name)
10221da177e4SLinus Torvalds 			name = ifr->ifr_name;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
10251da177e4SLinus Torvalds 				   tun_setup);
10261da177e4SLinus Torvalds 		if (!dev)
10271da177e4SLinus Torvalds 			return -ENOMEM;
10281da177e4SLinus Torvalds 
1029fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1030f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1031758e43b7SStephen Hemminger 
10321da177e4SLinus Torvalds 		tun = netdev_priv(dev);
10331da177e4SLinus Torvalds 		tun->dev = dev;
10341da177e4SLinus Torvalds 		tun->flags = flags;
1035f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
10361da177e4SLinus Torvalds 
103733dccbb0SHerbert Xu 		err = -ENOMEM;
103833dccbb0SHerbert Xu 		sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
103933dccbb0SHerbert Xu 		if (!sk)
104033dccbb0SHerbert Xu 			goto err_free_dev;
104133dccbb0SHerbert Xu 
1042c40af84aSHerbert Xu 		init_waitqueue_head(&tun->socket.wait);
104305c2828cSMichael S. Tsirkin 		tun->socket.ops = &tun_socket_ops;
104433dccbb0SHerbert Xu 		sock_init_data(&tun->socket, sk);
104533dccbb0SHerbert Xu 		sk->sk_write_space = tun_sock_write_space;
104633dccbb0SHerbert Xu 		sk->sk_sndbuf = INT_MAX;
104733dccbb0SHerbert Xu 
104880924e5fSVitaliy Gusev 		tun_sk(sk)->tun = tun;
104933dccbb0SHerbert Xu 
10502b980dbdSPaul Moore 		security_tun_dev_post_create(sk);
10512b980dbdSPaul Moore 
10521da177e4SLinus Torvalds 		tun_net_init(dev);
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 		if (strchr(dev->name, '%')) {
10551da177e4SLinus Torvalds 			err = dev_alloc_name(dev, dev->name);
10561da177e4SLinus Torvalds 			if (err < 0)
105733dccbb0SHerbert Xu 				goto err_free_sk;
10581da177e4SLinus Torvalds 		}
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
10611da177e4SLinus Torvalds 		if (err < 0)
10629c3fea6aSHerbert Xu 			goto err_free_sk;
10639c3fea6aSHerbert Xu 
1064980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1065980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1066980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
1067980c9e8cSDavid Woodhouse 			printk(KERN_ERR "Failed to create tun sysfs files\n");
1068980c9e8cSDavid Woodhouse 
10699c3fea6aSHerbert Xu 		sk->sk_destruct = tun_sock_destruct;
1070a7385ba2SEric W. Biederman 
1071a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1072a7385ba2SEric W. Biederman 		if (err < 0)
10739c3fea6aSHerbert Xu 			goto failed;
10741da177e4SLinus Torvalds 	}
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
10791da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1080a26af1e0SNathaniel Filardo 	else
1081a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
10841da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1085a26af1e0SNathaniel Filardo 	else
1086a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
10871da177e4SLinus Torvalds 
1088f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1089f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1090f43798c2SRusty Russell 	else
1091f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1092f43798c2SRusty Russell 
1093e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1094e35259a9SMax Krasnyansky 	 * xoff state.
1095e35259a9SMax Krasnyansky 	 */
1096e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1097e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
1098e35259a9SMax Krasnyansky 
10991da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
11001da177e4SLinus Torvalds 	return 0;
11011da177e4SLinus Torvalds 
110233dccbb0SHerbert Xu  err_free_sk:
110333dccbb0SHerbert Xu 	sock_put(sk);
11041da177e4SLinus Torvalds  err_free_dev:
11051da177e4SLinus Torvalds 	free_netdev(dev);
11061da177e4SLinus Torvalds  failed:
11071da177e4SLinus Torvalds 	return err;
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds 
1110876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun,
1111876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1112e3b99556SMark McLoughlin {
1113e3b99556SMark McLoughlin 	DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
1114e3b99556SMark McLoughlin 
1115e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1116e3b99556SMark McLoughlin 
1117980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1118e3b99556SMark McLoughlin 
1119e3b99556SMark McLoughlin 	return 0;
1120e3b99556SMark McLoughlin }
1121e3b99556SMark McLoughlin 
11225228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
11235228ddc9SRusty Russell  * privs required. */
11245228ddc9SRusty Russell static int set_offload(struct net_device *dev, unsigned long arg)
11255228ddc9SRusty Russell {
11265228ddc9SRusty Russell 	unsigned int old_features, features;
11275228ddc9SRusty Russell 
11285228ddc9SRusty Russell 	old_features = dev->features;
11295228ddc9SRusty Russell 	/* Unset features, set them as we chew on the arg. */
11305228ddc9SRusty Russell 	features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
1131e36aa25aSSridhar Samudrala 				    |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6
1132e36aa25aSSridhar Samudrala 				    |NETIF_F_UFO));
11335228ddc9SRusty Russell 
11345228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
11355228ddc9SRusty Russell 		features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
11365228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
11375228ddc9SRusty Russell 
11385228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
11395228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
11405228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
11415228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
11425228ddc9SRusty Russell 			}
11435228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
11445228ddc9SRusty Russell 				features |= NETIF_F_TSO;
11455228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
11465228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
11475228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
11485228ddc9SRusty Russell 		}
1149e36aa25aSSridhar Samudrala 
1150e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1151e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1152e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1153e36aa25aSSridhar Samudrala 		}
11545228ddc9SRusty Russell 	}
11555228ddc9SRusty Russell 
11565228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
11575228ddc9SRusty Russell 	 * trying to set them. */
11585228ddc9SRusty Russell 	if (arg)
11595228ddc9SRusty Russell 		return -EINVAL;
11605228ddc9SRusty Russell 
11615228ddc9SRusty Russell 	dev->features = features;
11625228ddc9SRusty Russell 	if (old_features != dev->features)
11635228ddc9SRusty Russell 		netdev_features_change(dev);
11645228ddc9SRusty Russell 
11655228ddc9SRusty Russell 	return 0;
11665228ddc9SRusty Russell }
11675228ddc9SRusty Russell 
116850857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
116950857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
11701da177e4SLinus Torvalds {
117136b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1172631ab46bSEric W. Biederman 	struct tun_struct *tun;
11731da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
117499405162SMichael S. Tsirkin 	struct sock_fprog fprog;
11751da177e4SLinus Torvalds 	struct ifreq ifr;
117633dccbb0SHerbert Xu 	int sndbuf;
1177f271b2ccSMax Krasnyansky 	int ret;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
118050857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
11811da177e4SLinus Torvalds 			return -EFAULT;
11821da177e4SLinus Torvalds 
1183631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1184631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1185631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1186631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1187631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1188631ab46bSEric W. Biederman 				IFF_VNET_HDR,
1189631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1190631ab46bSEric W. Biederman 	}
1191631ab46bSEric W. Biederman 
1192876bfd4dSHerbert Xu 	rtnl_lock();
1193876bfd4dSHerbert Xu 
119436b50babSEric W. Biederman 	tun = __tun_get(tfile);
11951da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
11961da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
11971da177e4SLinus Torvalds 
1198876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
11991da177e4SLinus Torvalds 
1200876bfd4dSHerbert Xu 		if (ret)
1201876bfd4dSHerbert Xu 			goto unlock;
12021da177e4SLinus Torvalds 
120350857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1204876bfd4dSHerbert Xu 			ret = -EFAULT;
1205876bfd4dSHerbert Xu 		goto unlock;
12061da177e4SLinus Torvalds 	}
12071da177e4SLinus Torvalds 
1208876bfd4dSHerbert Xu 	ret = -EBADFD;
12091da177e4SLinus Torvalds 	if (!tun)
1210876bfd4dSHerbert Xu 		goto unlock;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
12131da177e4SLinus Torvalds 
1214631ab46bSEric W. Biederman 	ret = 0;
12151da177e4SLinus Torvalds 	switch (cmd) {
1216e3b99556SMark McLoughlin 	case TUNGETIFF:
1217876bfd4dSHerbert Xu 		ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1218e3b99556SMark McLoughlin 		if (ret)
1219631ab46bSEric W. Biederman 			break;
1220e3b99556SMark McLoughlin 
122150857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1222631ab46bSEric W. Biederman 			ret = -EFAULT;
1223e3b99556SMark McLoughlin 		break;
1224e3b99556SMark McLoughlin 
12251da177e4SLinus Torvalds 	case TUNSETNOCSUM:
12261da177e4SLinus Torvalds 		/* Disable/Enable checksum */
12271da177e4SLinus Torvalds 		if (arg)
12281da177e4SLinus Torvalds 			tun->flags |= TUN_NOCHECKSUM;
12291da177e4SLinus Torvalds 		else
12301da177e4SLinus Torvalds 			tun->flags &= ~TUN_NOCHECKSUM;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: checksum %s\n",
12331da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
12341da177e4SLinus Torvalds 		break;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	case TUNSETPERSIST:
12371da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
12381da177e4SLinus Torvalds 		if (arg)
12391da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
12401da177e4SLinus Torvalds 		else
12411da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: persist %s\n",
1244c6e991deSToyo Abe 		    tun->dev->name, arg ? "enabled" : "disabled");
12451da177e4SLinus Torvalds 		break;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	case TUNSETOWNER:
12481da177e4SLinus Torvalds 		/* Set owner of the device */
12491da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
12521da177e4SLinus Torvalds 		break;
12531da177e4SLinus Torvalds 
12548c644623SGuido Guenther 	case TUNSETGROUP:
12558c644623SGuido Guenther 		/* Set group of the device */
12568c644623SGuido Guenther 		tun->group= (gid_t) arg;
12578c644623SGuido Guenther 
12588c644623SGuido Guenther 		DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
12598c644623SGuido Guenther 		break;
12608c644623SGuido Guenther 
1261ff4cc3acSMike Kershaw 	case TUNSETLINK:
1262ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1263ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
1264ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
1265ff4cc3acSMike Kershaw 				tun->dev->name);
126648abfe05SDavid S. Miller 			ret = -EBUSY;
1267ff4cc3acSMike Kershaw 		} else {
1268ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
1269ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
127048abfe05SDavid S. Miller 			ret = 0;
1271ff4cc3acSMike Kershaw 		}
1272631ab46bSEric W. Biederman 		break;
1273ff4cc3acSMike Kershaw 
12741da177e4SLinus Torvalds #ifdef TUN_DEBUG
12751da177e4SLinus Torvalds 	case TUNSETDEBUG:
12761da177e4SLinus Torvalds 		tun->debug = arg;
12771da177e4SLinus Torvalds 		break;
12781da177e4SLinus Torvalds #endif
12795228ddc9SRusty Russell 	case TUNSETOFFLOAD:
12805228ddc9SRusty Russell 		ret = set_offload(tun->dev, arg);
1281631ab46bSEric W. Biederman 		break;
12825228ddc9SRusty Russell 
1283f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1284f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1285631ab46bSEric W. Biederman 		ret = -EINVAL;
1286f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1287631ab46bSEric W. Biederman 			break;
1288c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1289631ab46bSEric W. Biederman 		break;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1292f271b2ccSMax Krasnyansky 		/* Get hw addres */
1293f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1294f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
129550857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1296631ab46bSEric W. Biederman 			ret = -EFAULT;
1297631ab46bSEric W. Biederman 		break;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1300f271b2ccSMax Krasnyansky 		/* Set hw address */
1301e174961cSJohannes Berg 		DBG(KERN_DEBUG "%s: set hw address: %pM\n",
1302e174961cSJohannes Berg 			tun->dev->name, ifr.ifr_hwaddr.sa_data);
130340102371SKim B. Heino 
130440102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1305631ab46bSEric W. Biederman 		break;
130633dccbb0SHerbert Xu 
130733dccbb0SHerbert Xu 	case TUNGETSNDBUF:
130889f56d1eSMichael S. Tsirkin 		sndbuf = tun->socket.sk->sk_sndbuf;
130933dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
131033dccbb0SHerbert Xu 			ret = -EFAULT;
131133dccbb0SHerbert Xu 		break;
131233dccbb0SHerbert Xu 
131333dccbb0SHerbert Xu 	case TUNSETSNDBUF:
131433dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
131533dccbb0SHerbert Xu 			ret = -EFAULT;
131633dccbb0SHerbert Xu 			break;
131733dccbb0SHerbert Xu 		}
131833dccbb0SHerbert Xu 
131989f56d1eSMichael S. Tsirkin 		tun->socket.sk->sk_sndbuf = sndbuf;
132033dccbb0SHerbert Xu 		break;
132133dccbb0SHerbert Xu 
132299405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
132399405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
132499405162SMichael S. Tsirkin 		ret = -EINVAL;
132599405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
132699405162SMichael S. Tsirkin 			break;
132799405162SMichael S. Tsirkin 		ret = -EFAULT;
132899405162SMichael S. Tsirkin 		if (copy_from_user(&fprog, argp, sizeof(fprog)))
132999405162SMichael S. Tsirkin 			break;
133099405162SMichael S. Tsirkin 
133199405162SMichael S. Tsirkin 		ret = sk_attach_filter(&fprog, tun->socket.sk);
133299405162SMichael S. Tsirkin 		break;
133399405162SMichael S. Tsirkin 
133499405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
133599405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
133699405162SMichael S. Tsirkin 		ret = -EINVAL;
133799405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
133899405162SMichael S. Tsirkin 			break;
133999405162SMichael S. Tsirkin 		ret = sk_detach_filter(tun->socket.sk);
134099405162SMichael S. Tsirkin 		break;
134199405162SMichael S. Tsirkin 
13421da177e4SLinus Torvalds 	default:
1343631ab46bSEric W. Biederman 		ret = -EINVAL;
1344631ab46bSEric W. Biederman 		break;
13451da177e4SLinus Torvalds 	};
13461da177e4SLinus Torvalds 
1347876bfd4dSHerbert Xu unlock:
1348876bfd4dSHerbert Xu 	rtnl_unlock();
1349876bfd4dSHerbert Xu 	if (tun)
1350631ab46bSEric W. Biederman 		tun_put(tun);
1351631ab46bSEric W. Biederman 	return ret;
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
135450857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
135550857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
135650857e2aSArnd Bergmann {
135750857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
135850857e2aSArnd Bergmann }
135950857e2aSArnd Bergmann 
136050857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
136150857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
136250857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
136350857e2aSArnd Bergmann {
136450857e2aSArnd Bergmann 	switch (cmd) {
136550857e2aSArnd Bergmann 	case TUNSETIFF:
136650857e2aSArnd Bergmann 	case TUNGETIFF:
136750857e2aSArnd Bergmann 	case TUNSETTXFILTER:
136850857e2aSArnd Bergmann 	case TUNGETSNDBUF:
136950857e2aSArnd Bergmann 	case TUNSETSNDBUF:
137050857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
137150857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
137250857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
137350857e2aSArnd Bergmann 		break;
137450857e2aSArnd Bergmann 	default:
137550857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
137650857e2aSArnd Bergmann 		break;
137750857e2aSArnd Bergmann 	}
137850857e2aSArnd Bergmann 
137950857e2aSArnd Bergmann 	/*
138050857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
138150857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
138250857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
138350857e2aSArnd Bergmann 	 * contents.
138450857e2aSArnd Bergmann 	 */
138550857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
138650857e2aSArnd Bergmann }
138750857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
138850857e2aSArnd Bergmann 
13891da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
13901da177e4SLinus Torvalds {
1391631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
13921da177e4SLinus Torvalds 	int ret;
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	if (!tun)
13951da177e4SLinus Torvalds 		return -EBADFD;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
14009d319522SJonathan Corbet 		goto out;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	if (on) {
1403609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
14041da177e4SLinus Torvalds 		if (ret)
14059d319522SJonathan Corbet 			goto out;
14061da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
14071da177e4SLinus Torvalds 	} else
14081da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
14099d319522SJonathan Corbet 	ret = 0;
14109d319522SJonathan Corbet out:
1411631ab46bSEric W. Biederman 	tun_put(tun);
14129d319522SJonathan Corbet 	return ret;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
14161da177e4SLinus Torvalds {
1417631ab46bSEric W. Biederman 	struct tun_file *tfile;
1418deed49fbSThomas Gleixner 
14191da177e4SLinus Torvalds 	DBG1(KERN_INFO "tunX: tun_chr_open\n");
1420631ab46bSEric W. Biederman 
1421631ab46bSEric W. Biederman 	tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1422631ab46bSEric W. Biederman 	if (!tfile)
1423631ab46bSEric W. Biederman 		return -ENOMEM;
1424c70f1829SEric W. Biederman 	atomic_set(&tfile->count, 0);
1425631ab46bSEric W. Biederman 	tfile->tun = NULL;
142636b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1427631ab46bSEric W. Biederman 	file->private_data = tfile;
14281da177e4SLinus Torvalds 	return 0;
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
14321da177e4SLinus Torvalds {
1433631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1434f0a4d0e5SEric W. Biederman 	struct tun_struct *tun;
14351da177e4SLinus Torvalds 
1436f0a4d0e5SEric W. Biederman 	tun = __tun_get(tfile);
1437631ab46bSEric W. Biederman 	if (tun) {
1438d23e4365SHerbert Xu 		struct net_device *dev = tun->dev;
1439d23e4365SHerbert Xu 
1440d23e4365SHerbert Xu 		DBG(KERN_INFO "%s: tun_chr_close\n", dev->name);
14411da177e4SLinus Torvalds 
1442631ab46bSEric W. Biederman 		__tun_detach(tun);
14431da177e4SLinus Torvalds 
14443ad2f3fbSDaniel Mack 		/* If desirable, unregister the netdevice. */
1445d23e4365SHerbert Xu 		if (!(tun->flags & TUN_PERSIST)) {
1446d23e4365SHerbert Xu 			rtnl_lock();
1447d23e4365SHerbert Xu 			if (dev->reg_state == NETREG_REGISTERED)
1448d23e4365SHerbert Xu 				unregister_netdevice(dev);
1449f0a4d0e5SEric W. Biederman 			rtnl_unlock();
1450d23e4365SHerbert Xu 		}
1451d23e4365SHerbert Xu 	}
1452631ab46bSEric W. Biederman 
14539c3fea6aSHerbert Xu 	tun = tfile->tun;
14549c3fea6aSHerbert Xu 	if (tun)
145589f56d1eSMichael S. Tsirkin 		sock_put(tun->socket.sk);
14569c3fea6aSHerbert Xu 
145736b50babSEric W. Biederman 	put_net(tfile->net);
1458631ab46bSEric W. Biederman 	kfree(tfile);
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 	return 0;
14611da177e4SLinus Torvalds }
14621da177e4SLinus Torvalds 
1463d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
14641da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
14651da177e4SLinus Torvalds 	.llseek = no_llseek,
1466ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1467ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1468ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1469ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
14701da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
1471876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
147250857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
147350857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
147450857e2aSArnd Bergmann #endif
14751da177e4SLinus Torvalds 	.open	= tun_chr_open,
14761da177e4SLinus Torvalds 	.release = tun_chr_close,
14771da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
14781da177e4SLinus Torvalds };
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
14811da177e4SLinus Torvalds 	.minor = TUN_MINOR,
14821da177e4SLinus Torvalds 	.name = "tun",
1483e454cea2SKay Sievers 	.nodename = "net/tun",
14841da177e4SLinus Torvalds 	.fops = &tun_fops,
14851da177e4SLinus Torvalds };
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds /* ethtool interface */
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	cmd->supported		= 0;
14921da177e4SLinus Torvalds 	cmd->advertising	= 0;
14931da177e4SLinus Torvalds 	cmd->speed		= SPEED_10;
14941da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
14951da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
14961da177e4SLinus Torvalds 	cmd->phy_address	= 0;
14971da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
14981da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
14991da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
15001da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
15011da177e4SLinus Torvalds 	return 0;
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
15051da177e4SLinus Torvalds {
15061da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
15091da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
15101da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
15131da177e4SLinus Torvalds 	case TUN_TUN_DEV:
15141da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
15151da177e4SLinus Torvalds 		break;
15161da177e4SLinus Torvalds 	case TUN_TAP_DEV:
15171da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
15181da177e4SLinus Torvalds 		break;
15191da177e4SLinus Torvalds 	}
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
15231da177e4SLinus Torvalds {
15241da177e4SLinus Torvalds #ifdef TUN_DEBUG
15251da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
15261da177e4SLinus Torvalds 	return tun->debug;
15271da177e4SLinus Torvalds #else
15281da177e4SLinus Torvalds 	return -EOPNOTSUPP;
15291da177e4SLinus Torvalds #endif
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
15331da177e4SLinus Torvalds {
15341da177e4SLinus Torvalds #ifdef TUN_DEBUG
15351da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
15361da177e4SLinus Torvalds 	tun->debug = value;
15371da177e4SLinus Torvalds #endif
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds static u32 tun_get_link(struct net_device *dev)
15411da177e4SLinus Torvalds {
15421da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
1543631ab46bSEric W. Biederman 	return !!tun->tfile;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds static u32 tun_get_rx_csum(struct net_device *dev)
15471da177e4SLinus Torvalds {
15481da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
15491da177e4SLinus Torvalds 	return (tun->flags & TUN_NOCHECKSUM) == 0;
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds static int tun_set_rx_csum(struct net_device *dev, u32 data)
15531da177e4SLinus Torvalds {
15541da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
15551da177e4SLinus Torvalds 	if (data)
15561da177e4SLinus Torvalds 		tun->flags &= ~TUN_NOCHECKSUM;
15571da177e4SLinus Torvalds 	else
15581da177e4SLinus Torvalds 		tun->flags |= TUN_NOCHECKSUM;
15591da177e4SLinus Torvalds 	return 0;
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
15627282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
15631da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
15641da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
15651da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
15661da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
15671da177e4SLinus Torvalds 	.get_link	= tun_get_link,
15681da177e4SLinus Torvalds 	.get_rx_csum	= tun_get_rx_csum,
15691da177e4SLinus Torvalds 	.set_rx_csum	= tun_set_rx_csum
15701da177e4SLinus Torvalds };
15711da177e4SLinus Torvalds 
157279d17604SPavel Emelyanov 
15731da177e4SLinus Torvalds static int __init tun_init(void)
15741da177e4SLinus Torvalds {
15751da177e4SLinus Torvalds 	int ret = 0;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
15781da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
15791da177e4SLinus Torvalds 
1580f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
158179d17604SPavel Emelyanov 	if (ret) {
1582f019a7a5SEric W. Biederman 		printk(KERN_ERR "tun: Can't register link_ops\n");
1583f019a7a5SEric W. Biederman 		goto err_linkops;
158479d17604SPavel Emelyanov 	}
158579d17604SPavel Emelyanov 
15861da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
158779d17604SPavel Emelyanov 	if (ret) {
15881da177e4SLinus Torvalds 		printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
158979d17604SPavel Emelyanov 		goto err_misc;
159079d17604SPavel Emelyanov 	}
159179d17604SPavel Emelyanov 	return  0;
159279d17604SPavel Emelyanov err_misc:
1593f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
1594f019a7a5SEric W. Biederman err_linkops:
15951da177e4SLinus Torvalds 	return ret;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds static void tun_cleanup(void)
15991da177e4SLinus Torvalds {
16001da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
1601f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
160405c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
160505c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
160605c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
160705c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
160805c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
160905c2828cSMichael S. Tsirkin {
161005c2828cSMichael S. Tsirkin 	struct tun_struct *tun;
161105c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
161205c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
161305c2828cSMichael S. Tsirkin 	tun = tun_get(file);
161405c2828cSMichael S. Tsirkin 	if (!tun)
161505c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
161605c2828cSMichael S. Tsirkin 	tun_put(tun);
161705c2828cSMichael S. Tsirkin 	return &tun->socket;
161805c2828cSMichael S. Tsirkin }
161905c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
162005c2828cSMichael S. Tsirkin 
16211da177e4SLinus Torvalds module_init(tun_init);
16221da177e4SLinus Torvalds module_exit(tun_cleanup);
16231da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
16241da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
16251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
16261da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1627