xref: /openbmc/linux/drivers/net/tun.c (revision bebd097a0af8bd6c51f50a65f3a435019b0e906a)
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 
376b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
386b8a66eeSJoe Perches 
391da177e4SLinus Torvalds #define DRV_NAME	"tun"
401da177e4SLinus Torvalds #define DRV_VERSION	"1.6"
411da177e4SLinus Torvalds #define DRV_DESCRIPTION	"Universal TUN/TAP device driver"
421da177e4SLinus Torvalds #define DRV_COPYRIGHT	"(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/module.h>
451da177e4SLinus Torvalds #include <linux/errno.h>
461da177e4SLinus Torvalds #include <linux/kernel.h>
471da177e4SLinus Torvalds #include <linux/major.h>
481da177e4SLinus Torvalds #include <linux/slab.h>
491da177e4SLinus Torvalds #include <linux/poll.h>
501da177e4SLinus Torvalds #include <linux/fcntl.h>
511da177e4SLinus Torvalds #include <linux/init.h>
521da177e4SLinus Torvalds #include <linux/skbuff.h>
531da177e4SLinus Torvalds #include <linux/netdevice.h>
541da177e4SLinus Torvalds #include <linux/etherdevice.h>
551da177e4SLinus Torvalds #include <linux/miscdevice.h>
561da177e4SLinus Torvalds #include <linux/ethtool.h>
571da177e4SLinus Torvalds #include <linux/rtnetlink.h>
5850857e2aSArnd Bergmann #include <linux/compat.h>
591da177e4SLinus Torvalds #include <linux/if.h>
601da177e4SLinus Torvalds #include <linux/if_arp.h>
611da177e4SLinus Torvalds #include <linux/if_ether.h>
621da177e4SLinus Torvalds #include <linux/if_tun.h>
631da177e4SLinus Torvalds #include <linux/crc32.h>
64d647a591SPavel Emelyanov #include <linux/nsproxy.h>
65f43798c2SRusty Russell #include <linux/virtio_net.h>
6699405162SMichael S. Tsirkin #include <linux/rcupdate.h>
67881d966bSEric W. Biederman #include <net/net_namespace.h>
6879d17604SPavel Emelyanov #include <net/netns/generic.h>
69f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
7033dccbb0SHerbert Xu #include <net/sock.h>
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #include <asm/system.h>
731da177e4SLinus Torvalds #include <asm/uaccess.h>
741da177e4SLinus Torvalds 
7514daa021SRusty Russell /* Uncomment to enable debugging */
7614daa021SRusty Russell /* #define TUN_DEBUG 1 */
7714daa021SRusty Russell 
781da177e4SLinus Torvalds #ifdef TUN_DEBUG
791da177e4SLinus Torvalds static int debug;
8014daa021SRusty Russell 
816b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
826b8a66eeSJoe Perches do {								\
836b8a66eeSJoe Perches 	if (tun->debug)						\
846b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
856b8a66eeSJoe Perches } while (0)
866b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
876b8a66eeSJoe Perches do {								\
886b8a66eeSJoe Perches 	if (debug == 2)						\
896b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
906b8a66eeSJoe Perches } while (0)
9114daa021SRusty Russell #else
926b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
936b8a66eeSJoe Perches do {								\
946b8a66eeSJoe Perches 	if (0)							\
956b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
966b8a66eeSJoe Perches } while (0)
976b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
986b8a66eeSJoe Perches do {								\
996b8a66eeSJoe Perches 	if (0)							\
1006b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
1016b8a66eeSJoe Perches } while (0)
1021da177e4SLinus Torvalds #endif
1031da177e4SLinus Torvalds 
104f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
105f271b2ccSMax Krasnyansky struct tap_filter {
106f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
107f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
108f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
109f271b2ccSMax Krasnyansky };
110f271b2ccSMax Krasnyansky 
111631ab46bSEric W. Biederman struct tun_file {
112c70f1829SEric W. Biederman 	atomic_t count;
113631ab46bSEric W. Biederman 	struct tun_struct *tun;
11436b50babSEric W. Biederman 	struct net *net;
115631ab46bSEric W. Biederman };
116631ab46bSEric W. Biederman 
11733dccbb0SHerbert Xu struct tun_sock;
11833dccbb0SHerbert Xu 
11914daa021SRusty Russell struct tun_struct {
120631ab46bSEric W. Biederman 	struct tun_file		*tfile;
121f271b2ccSMax Krasnyansky 	unsigned int 		flags;
12214daa021SRusty Russell 	uid_t			owner;
12314daa021SRusty Russell 	gid_t			group;
12414daa021SRusty Russell 
12514daa021SRusty Russell 	struct net_device	*dev;
12688255375SMichał Mirosław 	u32			set_features;
12788255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
12888255375SMichał Mirosław 			  NETIF_F_TSO6|NETIF_F_UFO)
12914daa021SRusty Russell 	struct fasync_struct	*fasync;
13014daa021SRusty Russell 
131f271b2ccSMax Krasnyansky 	struct tap_filter       txflt;
13233dccbb0SHerbert Xu 	struct socket		socket;
13343815482SEric Dumazet 	struct socket_wq	wq;
134d9d52b51SMichael S. Tsirkin 
135d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
136d9d52b51SMichael S. Tsirkin 
13714daa021SRusty Russell #ifdef TUN_DEBUG
13814daa021SRusty Russell 	int debug;
13914daa021SRusty Russell #endif
14014daa021SRusty Russell };
14114daa021SRusty Russell 
14233dccbb0SHerbert Xu struct tun_sock {
14333dccbb0SHerbert Xu 	struct sock		sk;
14433dccbb0SHerbert Xu 	struct tun_struct	*tun;
14533dccbb0SHerbert Xu };
14633dccbb0SHerbert Xu 
14733dccbb0SHerbert Xu static inline struct tun_sock *tun_sk(struct sock *sk)
14833dccbb0SHerbert Xu {
14933dccbb0SHerbert Xu 	return container_of(sk, struct tun_sock, sk);
15033dccbb0SHerbert Xu }
15133dccbb0SHerbert Xu 
152a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
153a7385ba2SEric W. Biederman {
154631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
15538231b7aSEric W. Biederman 	int err;
156a7385ba2SEric W. Biederman 
157a7385ba2SEric W. Biederman 	ASSERT_RTNL();
158a7385ba2SEric W. Biederman 
15938231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
16038231b7aSEric W. Biederman 
16138231b7aSEric W. Biederman 	err = -EINVAL;
16238231b7aSEric W. Biederman 	if (tfile->tun)
16338231b7aSEric W. Biederman 		goto out;
16438231b7aSEric W. Biederman 
16538231b7aSEric W. Biederman 	err = -EBUSY;
16638231b7aSEric W. Biederman 	if (tun->tfile)
16738231b7aSEric W. Biederman 		goto out;
16838231b7aSEric W. Biederman 
16938231b7aSEric W. Biederman 	err = 0;
170631ab46bSEric W. Biederman 	tfile->tun = tun;
171631ab46bSEric W. Biederman 	tun->tfile = tfile;
17205c2828cSMichael S. Tsirkin 	tun->socket.file = file;
173bee31369SNolan Leake 	netif_carrier_on(tun->dev);
174c70f1829SEric W. Biederman 	dev_hold(tun->dev);
17589f56d1eSMichael S. Tsirkin 	sock_hold(tun->socket.sk);
176c70f1829SEric W. Biederman 	atomic_inc(&tfile->count);
177a7385ba2SEric W. Biederman 
17838231b7aSEric W. Biederman out:
17938231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
18038231b7aSEric W. Biederman 	return err;
181a7385ba2SEric W. Biederman }
182a7385ba2SEric W. Biederman 
183631ab46bSEric W. Biederman static void __tun_detach(struct tun_struct *tun)
184631ab46bSEric W. Biederman {
185631ab46bSEric W. Biederman 	/* Detach from net device */
18638231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
187bee31369SNolan Leake 	netif_carrier_off(tun->dev);
188631ab46bSEric W. Biederman 	tun->tfile = NULL;
18905c2828cSMichael S. Tsirkin 	tun->socket.file = NULL;
19038231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
191631ab46bSEric W. Biederman 
192631ab46bSEric W. Biederman 	/* Drop read queue */
19389f56d1eSMichael S. Tsirkin 	skb_queue_purge(&tun->socket.sk->sk_receive_queue);
194c70f1829SEric W. Biederman 
195c70f1829SEric W. Biederman 	/* Drop the extra count on the net device */
196c70f1829SEric W. Biederman 	dev_put(tun->dev);
197c70f1829SEric W. Biederman }
198c70f1829SEric W. Biederman 
199c70f1829SEric W. Biederman static void tun_detach(struct tun_struct *tun)
200c70f1829SEric W. Biederman {
201c70f1829SEric W. Biederman 	rtnl_lock();
202c70f1829SEric W. Biederman 	__tun_detach(tun);
203c70f1829SEric W. Biederman 	rtnl_unlock();
204631ab46bSEric W. Biederman }
205631ab46bSEric W. Biederman 
206631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
207631ab46bSEric W. Biederman {
208c70f1829SEric W. Biederman 	struct tun_struct *tun = NULL;
209c70f1829SEric W. Biederman 
210c70f1829SEric W. Biederman 	if (atomic_inc_not_zero(&tfile->count))
211c70f1829SEric W. Biederman 		tun = tfile->tun;
212c70f1829SEric W. Biederman 
213c70f1829SEric W. Biederman 	return tun;
214631ab46bSEric W. Biederman }
215631ab46bSEric W. Biederman 
216631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
217631ab46bSEric W. Biederman {
218631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
219631ab46bSEric W. Biederman }
220631ab46bSEric W. Biederman 
221631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
222631ab46bSEric W. Biederman {
223c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
224c70f1829SEric W. Biederman 
225c70f1829SEric W. Biederman 	if (atomic_dec_and_test(&tfile->count))
226c70f1829SEric W. Biederman 		tun_detach(tfile->tun);
227631ab46bSEric W. Biederman }
228631ab46bSEric W. Biederman 
2296b8a66eeSJoe Perches /* TAP filtering */
230f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
231f271b2ccSMax Krasnyansky {
232f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
233f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
234f271b2ccSMax Krasnyansky }
235f271b2ccSMax Krasnyansky 
236f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
237f271b2ccSMax Krasnyansky {
238f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
239f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
240f271b2ccSMax Krasnyansky }
241f271b2ccSMax Krasnyansky 
242f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
243f271b2ccSMax Krasnyansky {
244f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
245f271b2ccSMax Krasnyansky 	struct tun_filter uf;
246f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
247f271b2ccSMax Krasnyansky 
248f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
249f271b2ccSMax Krasnyansky 		return -EFAULT;
250f271b2ccSMax Krasnyansky 
251f271b2ccSMax Krasnyansky 	if (!uf.count) {
252f271b2ccSMax Krasnyansky 		/* Disabled */
253f271b2ccSMax Krasnyansky 		filter->count = 0;
254f271b2ccSMax Krasnyansky 		return 0;
255f271b2ccSMax Krasnyansky 	}
256f271b2ccSMax Krasnyansky 
257f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
258f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
259f271b2ccSMax Krasnyansky 	if (!addr)
260f271b2ccSMax Krasnyansky 		return -ENOMEM;
261f271b2ccSMax Krasnyansky 
262f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
263f271b2ccSMax Krasnyansky 		err = -EFAULT;
264f271b2ccSMax Krasnyansky 		goto done;
265f271b2ccSMax Krasnyansky 	}
266f271b2ccSMax Krasnyansky 
267f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
268f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
269f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
270f271b2ccSMax Krasnyansky 	filter->count = 0;
271f271b2ccSMax Krasnyansky 	wmb();
272f271b2ccSMax Krasnyansky 
273f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
274f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
275f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
276f271b2ccSMax Krasnyansky 
277f271b2ccSMax Krasnyansky 	nexact = n;
278f271b2ccSMax Krasnyansky 
279cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
280cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
281f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
282cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
283cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
284cfbf84fcSAlex Williamson 			err = 0; /* no filter */
285cfbf84fcSAlex Williamson 			goto done;
286cfbf84fcSAlex Williamson 		}
287f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
288cfbf84fcSAlex Williamson 	}
289f271b2ccSMax Krasnyansky 
290f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
291f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
292f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
293f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
294f271b2ccSMax Krasnyansky 
295f271b2ccSMax Krasnyansky 	/* Now enable the filter */
296f271b2ccSMax Krasnyansky 	wmb();
297f271b2ccSMax Krasnyansky 	filter->count = nexact;
298f271b2ccSMax Krasnyansky 
299f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
300f271b2ccSMax Krasnyansky 	err = nexact;
301f271b2ccSMax Krasnyansky 
302f271b2ccSMax Krasnyansky done:
303f271b2ccSMax Krasnyansky 	kfree(addr);
304f271b2ccSMax Krasnyansky 	return err;
305f271b2ccSMax Krasnyansky }
306f271b2ccSMax Krasnyansky 
307f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
308f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
309f271b2ccSMax Krasnyansky {
310f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
311f271b2ccSMax Krasnyansky 	 * at this point. */
312f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
313f271b2ccSMax Krasnyansky 	int i;
314f271b2ccSMax Krasnyansky 
315f271b2ccSMax Krasnyansky 	/* Exact match */
316f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
317f271b2ccSMax Krasnyansky 		if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
318f271b2ccSMax Krasnyansky 			return 1;
319f271b2ccSMax Krasnyansky 
320f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
321f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
322f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
323f271b2ccSMax Krasnyansky 
324f271b2ccSMax Krasnyansky 	return 0;
325f271b2ccSMax Krasnyansky }
326f271b2ccSMax Krasnyansky 
327f271b2ccSMax Krasnyansky /*
328f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
329f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
330f271b2ccSMax Krasnyansky  */
331f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
332f271b2ccSMax Krasnyansky {
333f271b2ccSMax Krasnyansky 	if (!filter->count)
334f271b2ccSMax Krasnyansky 		return 1;
335f271b2ccSMax Krasnyansky 
336f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
337f271b2ccSMax Krasnyansky }
338f271b2ccSMax Krasnyansky 
3391da177e4SLinus Torvalds /* Network device part of the driver */
3401da177e4SLinus Torvalds 
3417282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
3421da177e4SLinus Torvalds 
343c70f1829SEric W. Biederman /* Net device detach from fd. */
344c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
345c70f1829SEric W. Biederman {
346c70f1829SEric W. Biederman 	struct tun_struct *tun = netdev_priv(dev);
347c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
348c70f1829SEric W. Biederman 
349c70f1829SEric W. Biederman 	/* Inform the methods they need to stop using the dev.
350c70f1829SEric W. Biederman 	 */
351c70f1829SEric W. Biederman 	if (tfile) {
35243815482SEric Dumazet 		wake_up_all(&tun->wq.wait);
353c70f1829SEric W. Biederman 		if (atomic_dec_and_test(&tfile->count))
354c70f1829SEric W. Biederman 			__tun_detach(tun);
355c70f1829SEric W. Biederman 	}
356c70f1829SEric W. Biederman }
357c70f1829SEric W. Biederman 
3589c3fea6aSHerbert Xu static void tun_free_netdev(struct net_device *dev)
3599c3fea6aSHerbert Xu {
3609c3fea6aSHerbert Xu 	struct tun_struct *tun = netdev_priv(dev);
3619c3fea6aSHerbert Xu 
36289f56d1eSMichael S. Tsirkin 	sock_put(tun->socket.sk);
3639c3fea6aSHerbert Xu }
3649c3fea6aSHerbert Xu 
3651da177e4SLinus Torvalds /* Net device open. */
3661da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	netif_start_queue(dev);
3691da177e4SLinus Torvalds 	return 0;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /* Net device close. */
3731da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	netif_stop_queue(dev);
3761da177e4SLinus Torvalds 	return 0;
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds /* Net device start xmit */
380424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3831da177e4SLinus Torvalds 
3846b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
387631ab46bSEric W. Biederman 	if (!tun->tfile)
3881da177e4SLinus Torvalds 		goto drop;
3891da177e4SLinus Torvalds 
390f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
391f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
392f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
393f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
394f271b2ccSMax Krasnyansky 		goto drop;
395f271b2ccSMax Krasnyansky 
39699405162SMichael S. Tsirkin 	if (tun->socket.sk->sk_filter &&
39799405162SMichael S. Tsirkin 	    sk_filter(tun->socket.sk, skb))
39899405162SMichael S. Tsirkin 		goto drop;
39999405162SMichael S. Tsirkin 
40089f56d1eSMichael S. Tsirkin 	if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
4011da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
4021da177e4SLinus Torvalds 			/* Normal queueing mode. */
4031da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
4041da177e4SLinus Torvalds 			netif_stop_queue(dev);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
4071da177e4SLinus Torvalds 			 * error is more appropriate. */
40809f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
4091da177e4SLinus Torvalds 		} else {
4101da177e4SLinus Torvalds 			/* Single queue mode.
4111da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
4121da177e4SLinus Torvalds 			goto drop;
4131da177e4SLinus Torvalds 		}
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
4160110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
4170110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
4180110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
4190110d6f2SMichael S. Tsirkin 
420f271b2ccSMax Krasnyansky 	/* Enqueue packet */
42189f56d1eSMichael S. Tsirkin 	skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	/* Notify and wake up reader process */
4241da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
4251da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
42643815482SEric Dumazet 	wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
42705c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
4286ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds drop:
43109f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
4321da177e4SLinus Torvalds 	kfree_skb(skb);
4336ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
436f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
4371da177e4SLinus Torvalds {
438f271b2ccSMax Krasnyansky 	/*
439f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
440f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
441f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
442f271b2ccSMax Krasnyansky 	 */
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4454885a504SEd Swierk #define MIN_MTU 68
4464885a504SEd Swierk #define MAX_MTU 65535
4474885a504SEd Swierk 
4484885a504SEd Swierk static int
4494885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
4504885a504SEd Swierk {
4514885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
4524885a504SEd Swierk 		return -EINVAL;
4534885a504SEd Swierk 	dev->mtu = new_mtu;
4544885a504SEd Swierk 	return 0;
4554885a504SEd Swierk }
4564885a504SEd Swierk 
45788255375SMichał Mirosław static u32 tun_net_fix_features(struct net_device *dev, u32 features)
45888255375SMichał Mirosław {
45988255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
46088255375SMichał Mirosław 
46188255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
46288255375SMichał Mirosław }
463*bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
464*bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
465*bebd097aSNeil Horman {
466*bebd097aSNeil Horman 	/*
467*bebd097aSNeil Horman 	 * Tun only receives frames when:
468*bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
469*bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
470*bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
471*bebd097aSNeil Horman 	 * never to have pending data when we poll for it
472*bebd097aSNeil Horman 	 * so theres nothing to do here but return.
473*bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
474*bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
475*bebd097aSNeil Horman 	 * still use netconsole
476*bebd097aSNeil Horman 	 */
477*bebd097aSNeil Horman 	return;
478*bebd097aSNeil Horman }
479*bebd097aSNeil Horman #endif
480758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
481c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
482758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
483758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
48400829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
485758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
48688255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
487*bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
488*bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
489*bebd097aSNeil Horman #endif
490758e43b7SStephen Hemminger };
491758e43b7SStephen Hemminger 
492758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
493c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
494758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
495758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
49600829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
497758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
49888255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
499758e43b7SStephen Hemminger 	.ndo_set_multicast_list	= tun_net_mclist,
500758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
501758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
502*bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
503*bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
504*bebd097aSNeil Horman #endif
505758e43b7SStephen Hemminger };
506758e43b7SStephen Hemminger 
5071da177e4SLinus Torvalds /* Initialize net device. */
5081da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
5131da177e4SLinus Torvalds 	case TUN_TUN_DEV:
514758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
515758e43b7SStephen Hemminger 
5161da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
5171da177e4SLinus Torvalds 		dev->hard_header_len = 0;
5181da177e4SLinus Torvalds 		dev->addr_len = 0;
5191da177e4SLinus Torvalds 		dev->mtu = 1500;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 		/* Zero header length */
5221da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
5231da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
5241da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5251da177e4SLinus Torvalds 		break;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	case TUN_TAP_DEV:
5287a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
5291da177e4SLinus Torvalds 		/* Ethernet TAP Device */
5301da177e4SLinus Torvalds 		ether_setup(dev);
53136226a8dSBrian Braunstein 
532f271b2ccSMax Krasnyansky 		random_ether_addr(dev->dev_addr);
53336226a8dSBrian Braunstein 
5341da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5351da177e4SLinus Torvalds 		break;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds /* Character device part */
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds /* Poll */
5421da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
5431da177e4SLinus Torvalds {
544b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
545b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
5463c8a9c63SMariusz Kozlowski 	struct sock *sk;
54733dccbb0SHerbert Xu 	unsigned int mask = 0;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	if (!tun)
550eac9e902SEric W. Biederman 		return POLLERR;
5511da177e4SLinus Torvalds 
55289f56d1eSMichael S. Tsirkin 	sk = tun->socket.sk;
5533c8a9c63SMariusz Kozlowski 
5546b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
5551da177e4SLinus Torvalds 
55643815482SEric Dumazet 	poll_wait(file, &tun->wq.wait, wait);
5571da177e4SLinus Torvalds 
55889f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
5591da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5601da177e4SLinus Torvalds 
56133dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
56233dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
56333dccbb0SHerbert Xu 	     sock_writeable(sk)))
56433dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
56533dccbb0SHerbert Xu 
566c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
567c70f1829SEric W. Biederman 		mask = POLLERR;
568c70f1829SEric W. Biederman 
569631ab46bSEric W. Biederman 	tun_put(tun);
5701da177e4SLinus Torvalds 	return mask;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
573f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
574f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
57533dccbb0SHerbert Xu static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
57633dccbb0SHerbert Xu 					    size_t prepad, size_t len,
57733dccbb0SHerbert Xu 					    size_t linear, int noblock)
578f42157cbSRusty Russell {
57989f56d1eSMichael S. Tsirkin 	struct sock *sk = tun->socket.sk;
580f42157cbSRusty Russell 	struct sk_buff *skb;
58133dccbb0SHerbert Xu 	int err;
582f42157cbSRusty Russell 
58382862742SHerbert Xu 	sock_update_classid(sk);
58482862742SHerbert Xu 
585f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
5860eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
58733dccbb0SHerbert Xu 		linear = len;
588f42157cbSRusty Russell 
58933dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
59033dccbb0SHerbert Xu 				   &err);
591f42157cbSRusty Russell 	if (!skb)
59233dccbb0SHerbert Xu 		return ERR_PTR(err);
593f42157cbSRusty Russell 
594f42157cbSRusty Russell 	skb_reserve(skb, prepad);
595f42157cbSRusty Russell 	skb_put(skb, linear);
59633dccbb0SHerbert Xu 	skb->data_len = len - linear;
59733dccbb0SHerbert Xu 	skb->len += len - linear;
598f42157cbSRusty Russell 
599f42157cbSRusty Russell 	return skb;
600f42157cbSRusty Russell }
601f42157cbSRusty Russell 
6021da177e4SLinus Torvalds /* Get packet from user space buffer */
60333dccbb0SHerbert Xu static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
6046f26c9a7SMichael S. Tsirkin 				       const struct iovec *iv, size_t count,
60533dccbb0SHerbert Xu 				       int noblock)
6061da177e4SLinus Torvalds {
60709640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
6081da177e4SLinus Torvalds 	struct sk_buff *skb;
6091da177e4SLinus Torvalds 	size_t len = count, align = 0;
610f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
6116f26c9a7SMichael S. Tsirkin 	int offset = 0;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
6141da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
6151da177e4SLinus Torvalds 			return -EINVAL;
6161da177e4SLinus Torvalds 
6176f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
6181da177e4SLinus Torvalds 			return -EFAULT;
6196f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 
622f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
623d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) > count)
624f43798c2SRusty Russell 			return -EINVAL;
625f43798c2SRusty Russell 
6266f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
627f43798c2SRusty Russell 			return -EFAULT;
628f43798c2SRusty Russell 
6294909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
6304909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
6314909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
6324909122fSHerbert Xu 
633f43798c2SRusty Russell 		if (gso.hdr_len > len)
634f43798c2SRusty Russell 			return -EINVAL;
635d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
636f43798c2SRusty Russell 	}
637f43798c2SRusty Russell 
638e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
6391da177e4SLinus Torvalds 		align = NET_IP_ALIGN;
6400eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
6410eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
642e01bf1c8SRusty Russell 			return -EINVAL;
643e01bf1c8SRusty Russell 	}
6441da177e4SLinus Torvalds 
64533dccbb0SHerbert Xu 	skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
64633dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
64733dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
64809f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
64933dccbb0SHerbert Xu 		return PTR_ERR(skb);
6501da177e4SLinus Torvalds 	}
6511da177e4SLinus Torvalds 
6526f26c9a7SMichael S. Tsirkin 	if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
65309f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
6548f22757eSDave Jones 		kfree_skb(skb);
6551da177e4SLinus Torvalds 		return -EFAULT;
6568f22757eSDave Jones 	}
6571da177e4SLinus Torvalds 
658f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
659f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
660f43798c2SRusty Russell 					  gso.csum_offset)) {
661f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
662f43798c2SRusty Russell 			kfree_skb(skb);
663f43798c2SRusty Russell 			return -EINVAL;
664f43798c2SRusty Russell 		}
66588255375SMichał Mirosław 	}
666f43798c2SRusty Russell 
6671da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
6681da177e4SLinus Torvalds 	case TUN_TUN_DEV:
669f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
670f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
671f09f7ee2SAng Way Chuang 			case 0x40:
672f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
673f09f7ee2SAng Way Chuang 				break;
674f09f7ee2SAng Way Chuang 			case 0x60:
675f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
676f09f7ee2SAng Way Chuang 				break;
677f09f7ee2SAng Way Chuang 			default:
678f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
679f09f7ee2SAng Way Chuang 				kfree_skb(skb);
680f09f7ee2SAng Way Chuang 				return -EINVAL;
681f09f7ee2SAng Way Chuang 			}
682f09f7ee2SAng Way Chuang 		}
683f09f7ee2SAng Way Chuang 
684459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
6851da177e4SLinus Torvalds 		skb->protocol = pi.proto;
6864c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
6871da177e4SLinus Torvalds 		break;
6881da177e4SLinus Torvalds 	case TUN_TAP_DEV:
6891da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
6901da177e4SLinus Torvalds 		break;
6911da177e4SLinus Torvalds 	};
6921da177e4SLinus Torvalds 
693f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
694f43798c2SRusty Russell 		pr_debug("GSO!\n");
695f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
696f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
697f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
698f43798c2SRusty Russell 			break;
699f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
700f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
701f43798c2SRusty Russell 			break;
702e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
703e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
704e36aa25aSSridhar Samudrala 			break;
705f43798c2SRusty Russell 		default:
706f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
707f43798c2SRusty Russell 			kfree_skb(skb);
708f43798c2SRusty Russell 			return -EINVAL;
709f43798c2SRusty Russell 		}
710f43798c2SRusty Russell 
711f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
712f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
713f43798c2SRusty Russell 
714f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
715f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
716f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
717f43798c2SRusty Russell 			kfree_skb(skb);
718f43798c2SRusty Russell 			return -EINVAL;
719f43798c2SRusty Russell 		}
720f43798c2SRusty Russell 
721f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
722f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
723f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
724f43798c2SRusty Russell 	}
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	netif_rx_ni(skb);
7271da177e4SLinus Torvalds 
72809f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
72909f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	return count;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
735ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
7361da177e4SLinus Torvalds {
73733dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
738ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
739631ab46bSEric W. Biederman 	ssize_t result;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	if (!tun)
7421da177e4SLinus Torvalds 		return -EBADFD;
7431da177e4SLinus Torvalds 
7446b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
7451da177e4SLinus Torvalds 
7466f26c9a7SMichael S. Tsirkin 	result = tun_get_user(tun, iv, iov_length(iv, count),
74733dccbb0SHerbert Xu 			      file->f_flags & O_NONBLOCK);
748631ab46bSEric W. Biederman 
749631ab46bSEric W. Biederman 	tun_put(tun);
750631ab46bSEric W. Biederman 	return result;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds /* Put packet to the user space buffer */
7541da177e4SLinus Torvalds static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
7551da177e4SLinus Torvalds 				       struct sk_buff *skb,
75643b39dcdSMichael S. Tsirkin 				       const struct iovec *iv, int len)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
7591da177e4SLinus Torvalds 	ssize_t total = 0;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
7621da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
7631da177e4SLinus Torvalds 			return -EINVAL;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 		if (len < skb->len) {
7661da177e4SLinus Torvalds 			/* Packet will be striped */
7671da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
7681da177e4SLinus Torvalds 		}
7691da177e4SLinus Torvalds 
77043b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
7711da177e4SLinus Torvalds 			return -EFAULT;
7721da177e4SLinus Torvalds 		total += sizeof(pi);
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
775f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
776f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
777d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
778f43798c2SRusty Russell 			return -EINVAL;
779f43798c2SRusty Russell 
780f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
781f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
782f43798c2SRusty Russell 
783f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
784f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
785f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
786f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
787f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
788f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
789f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
790e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
791e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
792ef3db4a5SMichael S. Tsirkin 			else {
7936b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
794ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
795ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
796ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
797ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
798ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
799ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
800ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
801ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
802ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
803ef3db4a5SMichael S. Tsirkin 			}
804f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
805f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
806f43798c2SRusty Russell 		} else
807f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
808f43798c2SRusty Russell 
809f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
810f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
81155508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
812f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
813f43798c2SRusty Russell 		} /* else everything is zero */
814f43798c2SRusty Russell 
81543b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
81643b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
817f43798c2SRusty Russell 			return -EFAULT;
818d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
819f43798c2SRusty Russell 	}
820f43798c2SRusty Russell 
8211da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
8221da177e4SLinus Torvalds 
82343b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
82405c2828cSMichael S. Tsirkin 	total += skb->len;
8251da177e4SLinus Torvalds 
82609f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
82709f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	return total;
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
83205c2828cSMichael S. Tsirkin static ssize_t tun_do_read(struct tun_struct *tun,
83305c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
83405c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
8371da177e4SLinus Torvalds 	struct sk_buff *skb;
83805c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
8391da177e4SLinus Torvalds 
8406b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_read\n");
8411da177e4SLinus Torvalds 
84243815482SEric Dumazet 	add_wait_queue(&tun->wq.wait, &wait);
8431da177e4SLinus Torvalds 	while (len) {
8441da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 		/* Read frames from the queue */
84789f56d1eSMichael S. Tsirkin 		if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
84805c2828cSMichael S. Tsirkin 			if (noblock) {
8491da177e4SLinus Torvalds 				ret = -EAGAIN;
8501da177e4SLinus Torvalds 				break;
8511da177e4SLinus Torvalds 			}
8521da177e4SLinus Torvalds 			if (signal_pending(current)) {
8531da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
8541da177e4SLinus Torvalds 				break;
8551da177e4SLinus Torvalds 			}
856c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
857c70f1829SEric W. Biederman 				ret = -EIO;
858c70f1829SEric W. Biederman 				break;
859c70f1829SEric W. Biederman 			}
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
8621da177e4SLinus Torvalds 			schedule();
8631da177e4SLinus Torvalds 			continue;
8641da177e4SLinus Torvalds 		}
8651da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
8661da177e4SLinus Torvalds 
86743b39dcdSMichael S. Tsirkin 		ret = tun_put_user(tun, skb, iv, len);
8681da177e4SLinus Torvalds 		kfree_skb(skb);
8691da177e4SLinus Torvalds 		break;
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
87343815482SEric Dumazet 	remove_wait_queue(&tun->wq.wait, &wait);
8741da177e4SLinus Torvalds 
87505c2828cSMichael S. Tsirkin 	return ret;
87605c2828cSMichael S. Tsirkin }
87705c2828cSMichael S. Tsirkin 
87805c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
87905c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
88005c2828cSMichael S. Tsirkin {
88105c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
88205c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
88305c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
88405c2828cSMichael S. Tsirkin 	ssize_t len, ret;
88505c2828cSMichael S. Tsirkin 
88605c2828cSMichael S. Tsirkin 	if (!tun)
88705c2828cSMichael S. Tsirkin 		return -EBADFD;
88805c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
88905c2828cSMichael S. Tsirkin 	if (len < 0) {
89005c2828cSMichael S. Tsirkin 		ret = -EINVAL;
89105c2828cSMichael S. Tsirkin 		goto out;
89205c2828cSMichael S. Tsirkin 	}
89305c2828cSMichael S. Tsirkin 
89405c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
89505c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
896631ab46bSEric W. Biederman out:
897631ab46bSEric W. Biederman 	tun_put(tun);
8981da177e4SLinus Torvalds 	return ret;
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	tun->owner = -1;
9068c644623SGuido Guenther 	tun->group = -1;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
9099c3fea6aSHerbert Xu 	dev->destructor = tun_free_netdev;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
912f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
913f019a7a5SEric W. Biederman  * device with netlink.
914f019a7a5SEric W. Biederman  */
915f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
916f019a7a5SEric W. Biederman {
917f019a7a5SEric W. Biederman 	return -EINVAL;
918f019a7a5SEric W. Biederman }
919f019a7a5SEric W. Biederman 
920f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
921f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
922f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
923f019a7a5SEric W. Biederman 	.setup		= tun_setup,
924f019a7a5SEric W. Biederman 	.validate	= tun_validate,
925f019a7a5SEric W. Biederman };
926f019a7a5SEric W. Biederman 
92733dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
92833dccbb0SHerbert Xu {
92933dccbb0SHerbert Xu 	struct tun_struct *tun;
93043815482SEric Dumazet 	wait_queue_head_t *wqueue;
93133dccbb0SHerbert Xu 
93233dccbb0SHerbert Xu 	if (!sock_writeable(sk))
93333dccbb0SHerbert Xu 		return;
93433dccbb0SHerbert Xu 
93533dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
93633dccbb0SHerbert Xu 		return;
93733dccbb0SHerbert Xu 
93843815482SEric Dumazet 	wqueue = sk_sleep(sk);
93943815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
94043815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
94105c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
942c722c625SHerbert Xu 
94380924e5fSVitaliy Gusev 	tun = tun_sk(sk)->tun;
94433dccbb0SHerbert Xu 	kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
94533dccbb0SHerbert Xu }
94633dccbb0SHerbert Xu 
94733dccbb0SHerbert Xu static void tun_sock_destruct(struct sock *sk)
94833dccbb0SHerbert Xu {
94980924e5fSVitaliy Gusev 	free_netdev(tun_sk(sk)->tun->dev);
95033dccbb0SHerbert Xu }
95133dccbb0SHerbert Xu 
95205c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
95305c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
95405c2828cSMichael S. Tsirkin {
95505c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
95605c2828cSMichael S. Tsirkin 	return tun_get_user(tun, m->msg_iov, total_len,
95705c2828cSMichael S. Tsirkin 			    m->msg_flags & MSG_DONTWAIT);
95805c2828cSMichael S. Tsirkin }
95905c2828cSMichael S. Tsirkin 
96005c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
96105c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
96205c2828cSMichael S. Tsirkin 		       int flags)
96305c2828cSMichael S. Tsirkin {
96405c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
96505c2828cSMichael S. Tsirkin 	int ret;
96605c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
96705c2828cSMichael S. Tsirkin 		return -EINVAL;
96805c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
96905c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
97005c2828cSMichael S. Tsirkin 	if (ret > total_len) {
97105c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
97205c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
97305c2828cSMichael S. Tsirkin 	}
97405c2828cSMichael S. Tsirkin 	return ret;
97505c2828cSMichael S. Tsirkin }
97605c2828cSMichael S. Tsirkin 
97705c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
97805c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
97905c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
98005c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
98105c2828cSMichael S. Tsirkin };
98205c2828cSMichael S. Tsirkin 
98333dccbb0SHerbert Xu static struct proto tun_proto = {
98433dccbb0SHerbert Xu 	.name		= "tun",
98533dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
98633dccbb0SHerbert Xu 	.obj_size	= sizeof(struct tun_sock),
98733dccbb0SHerbert Xu };
988f019a7a5SEric W. Biederman 
989980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
990980c9e8cSDavid Woodhouse {
991980c9e8cSDavid Woodhouse 	int flags = 0;
992980c9e8cSDavid Woodhouse 
993980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
994980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
995980c9e8cSDavid Woodhouse 	else
996980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
997980c9e8cSDavid Woodhouse 
998980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
999980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1000980c9e8cSDavid Woodhouse 
1001980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1002980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1003980c9e8cSDavid Woodhouse 
1004980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1005980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1006980c9e8cSDavid Woodhouse 
1007980c9e8cSDavid Woodhouse 	return flags;
1008980c9e8cSDavid Woodhouse }
1009980c9e8cSDavid Woodhouse 
1010980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1011980c9e8cSDavid Woodhouse 			      char *buf)
1012980c9e8cSDavid Woodhouse {
1013980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1014980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1015980c9e8cSDavid Woodhouse }
1016980c9e8cSDavid Woodhouse 
1017980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1018980c9e8cSDavid Woodhouse 			      char *buf)
1019980c9e8cSDavid Woodhouse {
1020980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1021980c9e8cSDavid Woodhouse 	return sprintf(buf, "%d\n", tun->owner);
1022980c9e8cSDavid Woodhouse }
1023980c9e8cSDavid Woodhouse 
1024980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1025980c9e8cSDavid Woodhouse 			      char *buf)
1026980c9e8cSDavid Woodhouse {
1027980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1028980c9e8cSDavid Woodhouse 	return sprintf(buf, "%d\n", tun->group);
1029980c9e8cSDavid Woodhouse }
1030980c9e8cSDavid Woodhouse 
1031980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1032980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1033980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1034980c9e8cSDavid Woodhouse 
1035d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
10361da177e4SLinus Torvalds {
103733dccbb0SHerbert Xu 	struct sock *sk;
10381da177e4SLinus Torvalds 	struct tun_struct *tun;
10391da177e4SLinus Torvalds 	struct net_device *dev;
10401da177e4SLinus Torvalds 	int err;
10411da177e4SLinus Torvalds 
104274a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
104374a3e5a7SEric W. Biederman 	if (dev) {
10442b980dbdSPaul Moore 		const struct cred *cred = current_cred();
10452b980dbdSPaul Moore 
1046f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1047f85ba780SDavid Woodhouse 			return -EBUSY;
104874a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
104974a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
105074a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
105174a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
105274a3e5a7SEric W. Biederman 		else
105374a3e5a7SEric W. Biederman 			return -EINVAL;
105474a3e5a7SEric W. Biederman 
10552b980dbdSPaul Moore 		if (((tun->owner != -1 && cred->euid != tun->owner) ||
10562b980dbdSPaul Moore 		     (tun->group != -1 && !in_egroup_p(tun->group))) &&
10572b980dbdSPaul Moore 		    !capable(CAP_NET_ADMIN))
10582b980dbdSPaul Moore 			return -EPERM;
1059d7e9660aSLinus Torvalds 		err = security_tun_dev_attach(tun->socket.sk);
10602b980dbdSPaul Moore 		if (err < 0)
10612b980dbdSPaul Moore 			return err;
10622b980dbdSPaul Moore 
1063a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1064a7385ba2SEric W. Biederman 		if (err < 0)
1065a7385ba2SEric W. Biederman 			return err;
106686a264abSDavid Howells 	}
10671da177e4SLinus Torvalds 	else {
10681da177e4SLinus Torvalds 		char *name;
10691da177e4SLinus Torvalds 		unsigned long flags = 0;
10701da177e4SLinus Torvalds 
1071ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
1072ca6bb5d7SDavid Woodhouse 			return -EPERM;
10732b980dbdSPaul Moore 		err = security_tun_dev_create();
10742b980dbdSPaul Moore 		if (err < 0)
10752b980dbdSPaul Moore 			return err;
1076ca6bb5d7SDavid Woodhouse 
10771da177e4SLinus Torvalds 		/* Set dev type */
10781da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
10791da177e4SLinus Torvalds 			/* TUN device */
10801da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
10811da177e4SLinus Torvalds 			name = "tun%d";
10821da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
10831da177e4SLinus Torvalds 			/* TAP device */
10841da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
10851da177e4SLinus Torvalds 			name = "tap%d";
10861da177e4SLinus Torvalds 		} else
108736989b90SKusanagi Kouichi 			return -EINVAL;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 		if (*ifr->ifr_name)
10901da177e4SLinus Torvalds 			name = ifr->ifr_name;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
10931da177e4SLinus Torvalds 				   tun_setup);
10941da177e4SLinus Torvalds 		if (!dev)
10951da177e4SLinus Torvalds 			return -ENOMEM;
10961da177e4SLinus Torvalds 
1097fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1098f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1099758e43b7SStephen Hemminger 
11001da177e4SLinus Torvalds 		tun = netdev_priv(dev);
11011da177e4SLinus Torvalds 		tun->dev = dev;
11021da177e4SLinus Torvalds 		tun->flags = flags;
1103f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1104d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
11051da177e4SLinus Torvalds 
110633dccbb0SHerbert Xu 		err = -ENOMEM;
110733dccbb0SHerbert Xu 		sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
110833dccbb0SHerbert Xu 		if (!sk)
110933dccbb0SHerbert Xu 			goto err_free_dev;
111033dccbb0SHerbert Xu 
111143815482SEric Dumazet 		tun->socket.wq = &tun->wq;
111243815482SEric Dumazet 		init_waitqueue_head(&tun->wq.wait);
111305c2828cSMichael S. Tsirkin 		tun->socket.ops = &tun_socket_ops;
111433dccbb0SHerbert Xu 		sock_init_data(&tun->socket, sk);
111533dccbb0SHerbert Xu 		sk->sk_write_space = tun_sock_write_space;
111633dccbb0SHerbert Xu 		sk->sk_sndbuf = INT_MAX;
111733dccbb0SHerbert Xu 
111880924e5fSVitaliy Gusev 		tun_sk(sk)->tun = tun;
111933dccbb0SHerbert Xu 
11202b980dbdSPaul Moore 		security_tun_dev_post_create(sk);
11212b980dbdSPaul Moore 
11221da177e4SLinus Torvalds 		tun_net_init(dev);
11231da177e4SLinus Torvalds 
112488255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
112588255375SMichał Mirosław 			TUN_USER_FEATURES;
112688255375SMichał Mirosław 		dev->features = dev->hw_features;
112788255375SMichał Mirosław 
11281da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
11291da177e4SLinus Torvalds 		if (err < 0)
11309c3fea6aSHerbert Xu 			goto err_free_sk;
11319c3fea6aSHerbert Xu 
1132980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1133980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1134980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
11356b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1136980c9e8cSDavid Woodhouse 
11379c3fea6aSHerbert Xu 		sk->sk_destruct = tun_sock_destruct;
1138a7385ba2SEric W. Biederman 
1139a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1140a7385ba2SEric W. Biederman 		if (err < 0)
11419c3fea6aSHerbert Xu 			goto failed;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 
11446b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
11471da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1148a26af1e0SNathaniel Filardo 	else
1149a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
11521da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1153a26af1e0SNathaniel Filardo 	else
1154a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
11551da177e4SLinus Torvalds 
1156f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1157f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1158f43798c2SRusty Russell 	else
1159f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1160f43798c2SRusty Russell 
1161e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1162e35259a9SMax Krasnyansky 	 * xoff state.
1163e35259a9SMax Krasnyansky 	 */
1164e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1165e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
1166e35259a9SMax Krasnyansky 
11671da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
11681da177e4SLinus Torvalds 	return 0;
11691da177e4SLinus Torvalds 
117033dccbb0SHerbert Xu  err_free_sk:
117133dccbb0SHerbert Xu 	sock_put(sk);
11721da177e4SLinus Torvalds  err_free_dev:
11731da177e4SLinus Torvalds 	free_netdev(dev);
11741da177e4SLinus Torvalds  failed:
11751da177e4SLinus Torvalds 	return err;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
1178876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun,
1179876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1180e3b99556SMark McLoughlin {
11816b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1182e3b99556SMark McLoughlin 
1183e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1184e3b99556SMark McLoughlin 
1185980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1186e3b99556SMark McLoughlin 
1187e3b99556SMark McLoughlin 	return 0;
1188e3b99556SMark McLoughlin }
1189e3b99556SMark McLoughlin 
11905228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
11915228ddc9SRusty Russell  * privs required. */
119288255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
11935228ddc9SRusty Russell {
119488255375SMichał Mirosław 	u32 features = 0;
11955228ddc9SRusty Russell 
11965228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
119788255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
11985228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
11995228ddc9SRusty Russell 
12005228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
12015228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
12025228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
12035228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
12045228ddc9SRusty Russell 			}
12055228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
12065228ddc9SRusty Russell 				features |= NETIF_F_TSO;
12075228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
12085228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
12095228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
12105228ddc9SRusty Russell 		}
1211e36aa25aSSridhar Samudrala 
1212e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1213e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1214e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1215e36aa25aSSridhar Samudrala 		}
12165228ddc9SRusty Russell 	}
12175228ddc9SRusty Russell 
12185228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
12195228ddc9SRusty Russell 	 * trying to set them. */
12205228ddc9SRusty Russell 	if (arg)
12215228ddc9SRusty Russell 		return -EINVAL;
12225228ddc9SRusty Russell 
122388255375SMichał Mirosław 	tun->set_features = features;
122488255375SMichał Mirosław 	netdev_update_features(tun->dev);
12255228ddc9SRusty Russell 
12265228ddc9SRusty Russell 	return 0;
12275228ddc9SRusty Russell }
12285228ddc9SRusty Russell 
122950857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
123050857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
12311da177e4SLinus Torvalds {
123236b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1233631ab46bSEric W. Biederman 	struct tun_struct *tun;
12341da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
123599405162SMichael S. Tsirkin 	struct sock_fprog fprog;
12361da177e4SLinus Torvalds 	struct ifreq ifr;
123733dccbb0SHerbert Xu 	int sndbuf;
1238d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1239f271b2ccSMax Krasnyansky 	int ret;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
124250857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
12431da177e4SLinus Torvalds 			return -EFAULT;
12441da177e4SLinus Torvalds 
1245631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1246631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1247631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1248631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1249631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1250631ab46bSEric W. Biederman 				IFF_VNET_HDR,
1251631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1252631ab46bSEric W. Biederman 	}
1253631ab46bSEric W. Biederman 
1254876bfd4dSHerbert Xu 	rtnl_lock();
1255876bfd4dSHerbert Xu 
125636b50babSEric W. Biederman 	tun = __tun_get(tfile);
12571da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
12581da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
12591da177e4SLinus Torvalds 
1260876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
12611da177e4SLinus Torvalds 
1262876bfd4dSHerbert Xu 		if (ret)
1263876bfd4dSHerbert Xu 			goto unlock;
12641da177e4SLinus Torvalds 
126550857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1266876bfd4dSHerbert Xu 			ret = -EFAULT;
1267876bfd4dSHerbert Xu 		goto unlock;
12681da177e4SLinus Torvalds 	}
12691da177e4SLinus Torvalds 
1270876bfd4dSHerbert Xu 	ret = -EBADFD;
12711da177e4SLinus Torvalds 	if (!tun)
1272876bfd4dSHerbert Xu 		goto unlock;
12731da177e4SLinus Torvalds 
12746b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
12751da177e4SLinus Torvalds 
1276631ab46bSEric W. Biederman 	ret = 0;
12771da177e4SLinus Torvalds 	switch (cmd) {
1278e3b99556SMark McLoughlin 	case TUNGETIFF:
1279876bfd4dSHerbert Xu 		ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1280e3b99556SMark McLoughlin 		if (ret)
1281631ab46bSEric W. Biederman 			break;
1282e3b99556SMark McLoughlin 
128350857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1284631ab46bSEric W. Biederman 			ret = -EFAULT;
1285e3b99556SMark McLoughlin 		break;
1286e3b99556SMark McLoughlin 
12871da177e4SLinus Torvalds 	case TUNSETNOCSUM:
12881da177e4SLinus Torvalds 		/* Disable/Enable checksum */
12891da177e4SLinus Torvalds 
129088255375SMichał Mirosław 		/* [unimplemented] */
129188255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
12926b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
12931da177e4SLinus Torvalds 		break;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	case TUNSETPERSIST:
12961da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
12971da177e4SLinus Torvalds 		if (arg)
12981da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
12991da177e4SLinus Torvalds 		else
13001da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
13011da177e4SLinus Torvalds 
13026b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
13036b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
13041da177e4SLinus Torvalds 		break;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	case TUNSETOWNER:
13071da177e4SLinus Torvalds 		/* Set owner of the device */
13081da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
13091da177e4SLinus Torvalds 
13106b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
13111da177e4SLinus Torvalds 		break;
13121da177e4SLinus Torvalds 
13138c644623SGuido Guenther 	case TUNSETGROUP:
13148c644623SGuido Guenther 		/* Set group of the device */
13158c644623SGuido Guenther 		tun->group= (gid_t) arg;
13168c644623SGuido Guenther 
13176b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
13188c644623SGuido Guenther 		break;
13198c644623SGuido Guenther 
1320ff4cc3acSMike Kershaw 	case TUNSETLINK:
1321ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1322ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
13236b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
13246b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
132548abfe05SDavid S. Miller 			ret = -EBUSY;
1326ff4cc3acSMike Kershaw 		} else {
1327ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
13286b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
13296b8a66eeSJoe Perches 				  tun->dev->type);
133048abfe05SDavid S. Miller 			ret = 0;
1331ff4cc3acSMike Kershaw 		}
1332631ab46bSEric W. Biederman 		break;
1333ff4cc3acSMike Kershaw 
13341da177e4SLinus Torvalds #ifdef TUN_DEBUG
13351da177e4SLinus Torvalds 	case TUNSETDEBUG:
13361da177e4SLinus Torvalds 		tun->debug = arg;
13371da177e4SLinus Torvalds 		break;
13381da177e4SLinus Torvalds #endif
13395228ddc9SRusty Russell 	case TUNSETOFFLOAD:
134088255375SMichał Mirosław 		ret = set_offload(tun, arg);
1341631ab46bSEric W. Biederman 		break;
13425228ddc9SRusty Russell 
1343f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1344f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1345631ab46bSEric W. Biederman 		ret = -EINVAL;
1346f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1347631ab46bSEric W. Biederman 			break;
1348c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1349631ab46bSEric W. Biederman 		break;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1352b595076aSUwe Kleine-König 		/* Get hw address */
1353f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1354f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
135550857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1356631ab46bSEric W. Biederman 			ret = -EFAULT;
1357631ab46bSEric W. Biederman 		break;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1360f271b2ccSMax Krasnyansky 		/* Set hw address */
13616b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
13626b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
136340102371SKim B. Heino 
136440102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1365631ab46bSEric W. Biederman 		break;
136633dccbb0SHerbert Xu 
136733dccbb0SHerbert Xu 	case TUNGETSNDBUF:
136889f56d1eSMichael S. Tsirkin 		sndbuf = tun->socket.sk->sk_sndbuf;
136933dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
137033dccbb0SHerbert Xu 			ret = -EFAULT;
137133dccbb0SHerbert Xu 		break;
137233dccbb0SHerbert Xu 
137333dccbb0SHerbert Xu 	case TUNSETSNDBUF:
137433dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
137533dccbb0SHerbert Xu 			ret = -EFAULT;
137633dccbb0SHerbert Xu 			break;
137733dccbb0SHerbert Xu 		}
137833dccbb0SHerbert Xu 
137989f56d1eSMichael S. Tsirkin 		tun->socket.sk->sk_sndbuf = sndbuf;
138033dccbb0SHerbert Xu 		break;
138133dccbb0SHerbert Xu 
1382d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
1383d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
1384d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1385d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1386d9d52b51SMichael S. Tsirkin 		break;
1387d9d52b51SMichael S. Tsirkin 
1388d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
1389d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1390d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1391d9d52b51SMichael S. Tsirkin 			break;
1392d9d52b51SMichael S. Tsirkin 		}
1393d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1394d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
1395d9d52b51SMichael S. Tsirkin 			break;
1396d9d52b51SMichael S. Tsirkin 		}
1397d9d52b51SMichael S. Tsirkin 
1398d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
1399d9d52b51SMichael S. Tsirkin 		break;
1400d9d52b51SMichael S. Tsirkin 
140199405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
140299405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
140399405162SMichael S. Tsirkin 		ret = -EINVAL;
140499405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
140599405162SMichael S. Tsirkin 			break;
140699405162SMichael S. Tsirkin 		ret = -EFAULT;
140799405162SMichael S. Tsirkin 		if (copy_from_user(&fprog, argp, sizeof(fprog)))
140899405162SMichael S. Tsirkin 			break;
140999405162SMichael S. Tsirkin 
141099405162SMichael S. Tsirkin 		ret = sk_attach_filter(&fprog, tun->socket.sk);
141199405162SMichael S. Tsirkin 		break;
141299405162SMichael S. Tsirkin 
141399405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
141499405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
141599405162SMichael S. Tsirkin 		ret = -EINVAL;
141699405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
141799405162SMichael S. Tsirkin 			break;
141899405162SMichael S. Tsirkin 		ret = sk_detach_filter(tun->socket.sk);
141999405162SMichael S. Tsirkin 		break;
142099405162SMichael S. Tsirkin 
14211da177e4SLinus Torvalds 	default:
1422631ab46bSEric W. Biederman 		ret = -EINVAL;
1423631ab46bSEric W. Biederman 		break;
1424ee289b64SJoe Perches 	}
14251da177e4SLinus Torvalds 
1426876bfd4dSHerbert Xu unlock:
1427876bfd4dSHerbert Xu 	rtnl_unlock();
1428876bfd4dSHerbert Xu 	if (tun)
1429631ab46bSEric W. Biederman 		tun_put(tun);
1430631ab46bSEric W. Biederman 	return ret;
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds 
143350857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
143450857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
143550857e2aSArnd Bergmann {
143650857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
143750857e2aSArnd Bergmann }
143850857e2aSArnd Bergmann 
143950857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
144050857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
144150857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
144250857e2aSArnd Bergmann {
144350857e2aSArnd Bergmann 	switch (cmd) {
144450857e2aSArnd Bergmann 	case TUNSETIFF:
144550857e2aSArnd Bergmann 	case TUNGETIFF:
144650857e2aSArnd Bergmann 	case TUNSETTXFILTER:
144750857e2aSArnd Bergmann 	case TUNGETSNDBUF:
144850857e2aSArnd Bergmann 	case TUNSETSNDBUF:
144950857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
145050857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
145150857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
145250857e2aSArnd Bergmann 		break;
145350857e2aSArnd Bergmann 	default:
145450857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
145550857e2aSArnd Bergmann 		break;
145650857e2aSArnd Bergmann 	}
145750857e2aSArnd Bergmann 
145850857e2aSArnd Bergmann 	/*
145950857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
146050857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
146150857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
146250857e2aSArnd Bergmann 	 * contents.
146350857e2aSArnd Bergmann 	 */
146450857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
146550857e2aSArnd Bergmann }
146650857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
146750857e2aSArnd Bergmann 
14681da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
14691da177e4SLinus Torvalds {
1470631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
14711da177e4SLinus Torvalds 	int ret;
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	if (!tun)
14741da177e4SLinus Torvalds 		return -EBADFD;
14751da177e4SLinus Torvalds 
14766b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
14799d319522SJonathan Corbet 		goto out;
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	if (on) {
1482609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
14831da177e4SLinus Torvalds 		if (ret)
14849d319522SJonathan Corbet 			goto out;
14851da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
14861da177e4SLinus Torvalds 	} else
14871da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
14889d319522SJonathan Corbet 	ret = 0;
14899d319522SJonathan Corbet out:
1490631ab46bSEric W. Biederman 	tun_put(tun);
14919d319522SJonathan Corbet 	return ret;
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
14951da177e4SLinus Torvalds {
1496631ab46bSEric W. Biederman 	struct tun_file *tfile;
1497deed49fbSThomas Gleixner 
14986b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1499631ab46bSEric W. Biederman 
1500631ab46bSEric W. Biederman 	tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1501631ab46bSEric W. Biederman 	if (!tfile)
1502631ab46bSEric W. Biederman 		return -ENOMEM;
1503c70f1829SEric W. Biederman 	atomic_set(&tfile->count, 0);
1504631ab46bSEric W. Biederman 	tfile->tun = NULL;
150536b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1506631ab46bSEric W. Biederman 	file->private_data = tfile;
15071da177e4SLinus Torvalds 	return 0;
15081da177e4SLinus Torvalds }
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
15111da177e4SLinus Torvalds {
1512631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1513f0a4d0e5SEric W. Biederman 	struct tun_struct *tun;
15141da177e4SLinus Torvalds 
1515f0a4d0e5SEric W. Biederman 	tun = __tun_get(tfile);
1516631ab46bSEric W. Biederman 	if (tun) {
1517d23e4365SHerbert Xu 		struct net_device *dev = tun->dev;
1518d23e4365SHerbert Xu 
15196b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "tun_chr_close\n");
15201da177e4SLinus Torvalds 
1521631ab46bSEric W. Biederman 		__tun_detach(tun);
15221da177e4SLinus Torvalds 
15233ad2f3fbSDaniel Mack 		/* If desirable, unregister the netdevice. */
1524d23e4365SHerbert Xu 		if (!(tun->flags & TUN_PERSIST)) {
1525d23e4365SHerbert Xu 			rtnl_lock();
1526d23e4365SHerbert Xu 			if (dev->reg_state == NETREG_REGISTERED)
1527d23e4365SHerbert Xu 				unregister_netdevice(dev);
1528f0a4d0e5SEric W. Biederman 			rtnl_unlock();
1529d23e4365SHerbert Xu 		}
1530d23e4365SHerbert Xu 	}
1531631ab46bSEric W. Biederman 
15329c3fea6aSHerbert Xu 	tun = tfile->tun;
15339c3fea6aSHerbert Xu 	if (tun)
153489f56d1eSMichael S. Tsirkin 		sock_put(tun->socket.sk);
15359c3fea6aSHerbert Xu 
153636b50babSEric W. Biederman 	put_net(tfile->net);
1537631ab46bSEric W. Biederman 	kfree(tfile);
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 	return 0;
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds 
1542d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
15431da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
15441da177e4SLinus Torvalds 	.llseek = no_llseek,
1545ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1546ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1547ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1548ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
15491da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
1550876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
155150857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
155250857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
155350857e2aSArnd Bergmann #endif
15541da177e4SLinus Torvalds 	.open	= tun_chr_open,
15551da177e4SLinus Torvalds 	.release = tun_chr_close,
15561da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
15571da177e4SLinus Torvalds };
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
15601da177e4SLinus Torvalds 	.minor = TUN_MINOR,
15611da177e4SLinus Torvalds 	.name = "tun",
1562e454cea2SKay Sievers 	.nodename = "net/tun",
15631da177e4SLinus Torvalds 	.fops = &tun_fops,
15641da177e4SLinus Torvalds };
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds /* ethtool interface */
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
15691da177e4SLinus Torvalds {
15701da177e4SLinus Torvalds 	cmd->supported		= 0;
15711da177e4SLinus Torvalds 	cmd->advertising	= 0;
157270739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
15731da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
15741da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
15751da177e4SLinus Torvalds 	cmd->phy_address	= 0;
15761da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
15771da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
15781da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
15791da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
15801da177e4SLinus Torvalds 	return 0;
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
15841da177e4SLinus Torvalds {
15851da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
15881da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
15891da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
15921da177e4SLinus Torvalds 	case TUN_TUN_DEV:
15931da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
15941da177e4SLinus Torvalds 		break;
15951da177e4SLinus Torvalds 	case TUN_TAP_DEV:
15961da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
15971da177e4SLinus Torvalds 		break;
15981da177e4SLinus Torvalds 	}
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
16021da177e4SLinus Torvalds {
16031da177e4SLinus Torvalds #ifdef TUN_DEBUG
16041da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
16051da177e4SLinus Torvalds 	return tun->debug;
16061da177e4SLinus Torvalds #else
16071da177e4SLinus Torvalds 	return -EOPNOTSUPP;
16081da177e4SLinus Torvalds #endif
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
16121da177e4SLinus Torvalds {
16131da177e4SLinus Torvalds #ifdef TUN_DEBUG
16141da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
16151da177e4SLinus Torvalds 	tun->debug = value;
16161da177e4SLinus Torvalds #endif
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
16197282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
16201da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
16211da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
16221da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
16231da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
1624bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
16251da177e4SLinus Torvalds };
16261da177e4SLinus Torvalds 
162779d17604SPavel Emelyanov 
16281da177e4SLinus Torvalds static int __init tun_init(void)
16291da177e4SLinus Torvalds {
16301da177e4SLinus Torvalds 	int ret = 0;
16311da177e4SLinus Torvalds 
16326b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
16336b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
16341da177e4SLinus Torvalds 
1635f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
163679d17604SPavel Emelyanov 	if (ret) {
16376b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
1638f019a7a5SEric W. Biederman 		goto err_linkops;
163979d17604SPavel Emelyanov 	}
164079d17604SPavel Emelyanov 
16411da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
164279d17604SPavel Emelyanov 	if (ret) {
16436b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
164479d17604SPavel Emelyanov 		goto err_misc;
164579d17604SPavel Emelyanov 	}
164679d17604SPavel Emelyanov 	return  0;
164779d17604SPavel Emelyanov err_misc:
1648f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
1649f019a7a5SEric W. Biederman err_linkops:
16501da177e4SLinus Torvalds 	return ret;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds static void tun_cleanup(void)
16541da177e4SLinus Torvalds {
16551da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
1656f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds 
165905c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
166005c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
166105c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
166205c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
166305c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
166405c2828cSMichael S. Tsirkin {
166505c2828cSMichael S. Tsirkin 	struct tun_struct *tun;
166605c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
166705c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
166805c2828cSMichael S. Tsirkin 	tun = tun_get(file);
166905c2828cSMichael S. Tsirkin 	if (!tun)
167005c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
167105c2828cSMichael S. Tsirkin 	tun_put(tun);
167205c2828cSMichael S. Tsirkin 	return &tun->socket;
167305c2828cSMichael S. Tsirkin }
167405c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
167505c2828cSMichael S. Tsirkin 
16761da177e4SLinus Torvalds module_init(tun_init);
16771da177e4SLinus Torvalds module_exit(tun_cleanup);
16781da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
16791da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
16801da177e4SLinus Torvalds MODULE_LICENSE("GPL");
16811da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1682578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
1683