xref: /openbmc/linux/drivers/net/tun.c (revision 0625c883bc4b3eba6f93f268cf67b5664244c0fe)
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>
25344dc8edSJoe Perches  *    Use eth_random_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/uaccess.h>
731da177e4SLinus Torvalds 
7414daa021SRusty Russell /* Uncomment to enable debugging */
7514daa021SRusty Russell /* #define TUN_DEBUG 1 */
7614daa021SRusty Russell 
771da177e4SLinus Torvalds #ifdef TUN_DEBUG
781da177e4SLinus Torvalds static int debug;
7914daa021SRusty Russell 
806b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
816b8a66eeSJoe Perches do {								\
826b8a66eeSJoe Perches 	if (tun->debug)						\
836b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
846b8a66eeSJoe Perches } while (0)
856b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
866b8a66eeSJoe Perches do {								\
876b8a66eeSJoe Perches 	if (debug == 2)						\
886b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
896b8a66eeSJoe Perches } while (0)
9014daa021SRusty Russell #else
916b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
926b8a66eeSJoe Perches do {								\
936b8a66eeSJoe Perches 	if (0)							\
946b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
956b8a66eeSJoe Perches } while (0)
966b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
976b8a66eeSJoe Perches do {								\
986b8a66eeSJoe Perches 	if (0)							\
996b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
1006b8a66eeSJoe Perches } while (0)
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
1030690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128
1040690899bSMichael S. Tsirkin 
105f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
106f271b2ccSMax Krasnyansky struct tap_filter {
107f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
108f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
109f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
110f271b2ccSMax Krasnyansky };
111f271b2ccSMax Krasnyansky 
112631ab46bSEric W. Biederman struct tun_file {
113c70f1829SEric W. Biederman 	atomic_t count;
114631ab46bSEric W. Biederman 	struct tun_struct *tun;
11536b50babSEric W. Biederman 	struct net *net;
116631ab46bSEric W. Biederman };
117631ab46bSEric W. Biederman 
11833dccbb0SHerbert Xu struct tun_sock;
11933dccbb0SHerbert Xu 
12014daa021SRusty Russell struct tun_struct {
121631ab46bSEric W. Biederman 	struct tun_file		*tfile;
122f271b2ccSMax Krasnyansky 	unsigned int 		flags;
123*0625c883SEric W. Biederman 	kuid_t			owner;
124*0625c883SEric W. Biederman 	kgid_t			group;
12514daa021SRusty Russell 
12614daa021SRusty Russell 	struct net_device	*dev;
127c8f44affSMichał Mirosław 	netdev_features_t	set_features;
12888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
12988255375SMichał Mirosław 			  NETIF_F_TSO6|NETIF_F_UFO)
13014daa021SRusty Russell 	struct fasync_struct	*fasync;
13114daa021SRusty Russell 
132f271b2ccSMax Krasnyansky 	struct tap_filter       txflt;
13333dccbb0SHerbert Xu 	struct socket		socket;
13443815482SEric Dumazet 	struct socket_wq	wq;
135d9d52b51SMichael S. Tsirkin 
136d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
137d9d52b51SMichael S. Tsirkin 
13814daa021SRusty Russell #ifdef TUN_DEBUG
13914daa021SRusty Russell 	int debug;
14014daa021SRusty Russell #endif
14114daa021SRusty Russell };
14214daa021SRusty Russell 
14333dccbb0SHerbert Xu struct tun_sock {
14433dccbb0SHerbert Xu 	struct sock		sk;
14533dccbb0SHerbert Xu 	struct tun_struct	*tun;
14633dccbb0SHerbert Xu };
14733dccbb0SHerbert Xu 
14833dccbb0SHerbert Xu static inline struct tun_sock *tun_sk(struct sock *sk)
14933dccbb0SHerbert Xu {
15033dccbb0SHerbert Xu 	return container_of(sk, struct tun_sock, sk);
15133dccbb0SHerbert Xu }
15233dccbb0SHerbert Xu 
153a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
154a7385ba2SEric W. Biederman {
155631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
15638231b7aSEric W. Biederman 	int err;
157a7385ba2SEric W. Biederman 
158a7385ba2SEric W. Biederman 	ASSERT_RTNL();
159a7385ba2SEric W. Biederman 
16038231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
16138231b7aSEric W. Biederman 
16238231b7aSEric W. Biederman 	err = -EINVAL;
16338231b7aSEric W. Biederman 	if (tfile->tun)
16438231b7aSEric W. Biederman 		goto out;
16538231b7aSEric W. Biederman 
16638231b7aSEric W. Biederman 	err = -EBUSY;
16738231b7aSEric W. Biederman 	if (tun->tfile)
16838231b7aSEric W. Biederman 		goto out;
16938231b7aSEric W. Biederman 
17038231b7aSEric W. Biederman 	err = 0;
171631ab46bSEric W. Biederman 	tfile->tun = tun;
172631ab46bSEric W. Biederman 	tun->tfile = tfile;
17305c2828cSMichael S. Tsirkin 	tun->socket.file = file;
174bee31369SNolan Leake 	netif_carrier_on(tun->dev);
175c70f1829SEric W. Biederman 	dev_hold(tun->dev);
17689f56d1eSMichael S. Tsirkin 	sock_hold(tun->socket.sk);
177c70f1829SEric W. Biederman 	atomic_inc(&tfile->count);
178a7385ba2SEric W. Biederman 
17938231b7aSEric W. Biederman out:
18038231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
18138231b7aSEric W. Biederman 	return err;
182a7385ba2SEric W. Biederman }
183a7385ba2SEric W. Biederman 
184631ab46bSEric W. Biederman static void __tun_detach(struct tun_struct *tun)
185631ab46bSEric W. Biederman {
186631ab46bSEric W. Biederman 	/* Detach from net device */
18738231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
188bee31369SNolan Leake 	netif_carrier_off(tun->dev);
189631ab46bSEric W. Biederman 	tun->tfile = NULL;
19005c2828cSMichael S. Tsirkin 	tun->socket.file = NULL;
19138231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
192631ab46bSEric W. Biederman 
193631ab46bSEric W. Biederman 	/* Drop read queue */
19489f56d1eSMichael S. Tsirkin 	skb_queue_purge(&tun->socket.sk->sk_receive_queue);
195c70f1829SEric W. Biederman 
196c70f1829SEric W. Biederman 	/* Drop the extra count on the net device */
197c70f1829SEric W. Biederman 	dev_put(tun->dev);
198c70f1829SEric W. Biederman }
199c70f1829SEric W. Biederman 
200c70f1829SEric W. Biederman static void tun_detach(struct tun_struct *tun)
201c70f1829SEric W. Biederman {
202c70f1829SEric W. Biederman 	rtnl_lock();
203c70f1829SEric W. Biederman 	__tun_detach(tun);
204c70f1829SEric W. Biederman 	rtnl_unlock();
205631ab46bSEric W. Biederman }
206631ab46bSEric W. Biederman 
207631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
208631ab46bSEric W. Biederman {
209c70f1829SEric W. Biederman 	struct tun_struct *tun = NULL;
210c70f1829SEric W. Biederman 
211c70f1829SEric W. Biederman 	if (atomic_inc_not_zero(&tfile->count))
212c70f1829SEric W. Biederman 		tun = tfile->tun;
213c70f1829SEric W. Biederman 
214c70f1829SEric W. Biederman 	return tun;
215631ab46bSEric W. Biederman }
216631ab46bSEric W. Biederman 
217631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
218631ab46bSEric W. Biederman {
219631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
220631ab46bSEric W. Biederman }
221631ab46bSEric W. Biederman 
222631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
223631ab46bSEric W. Biederman {
224c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
225c70f1829SEric W. Biederman 
226c70f1829SEric W. Biederman 	if (atomic_dec_and_test(&tfile->count))
227c70f1829SEric W. Biederman 		tun_detach(tfile->tun);
228631ab46bSEric W. Biederman }
229631ab46bSEric W. Biederman 
2306b8a66eeSJoe Perches /* TAP filtering */
231f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
232f271b2ccSMax Krasnyansky {
233f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
234f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
235f271b2ccSMax Krasnyansky }
236f271b2ccSMax Krasnyansky 
237f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
238f271b2ccSMax Krasnyansky {
239f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
240f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
241f271b2ccSMax Krasnyansky }
242f271b2ccSMax Krasnyansky 
243f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
244f271b2ccSMax Krasnyansky {
245f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
246f271b2ccSMax Krasnyansky 	struct tun_filter uf;
247f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
248f271b2ccSMax Krasnyansky 
249f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
250f271b2ccSMax Krasnyansky 		return -EFAULT;
251f271b2ccSMax Krasnyansky 
252f271b2ccSMax Krasnyansky 	if (!uf.count) {
253f271b2ccSMax Krasnyansky 		/* Disabled */
254f271b2ccSMax Krasnyansky 		filter->count = 0;
255f271b2ccSMax Krasnyansky 		return 0;
256f271b2ccSMax Krasnyansky 	}
257f271b2ccSMax Krasnyansky 
258f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
259f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
260f271b2ccSMax Krasnyansky 	if (!addr)
261f271b2ccSMax Krasnyansky 		return -ENOMEM;
262f271b2ccSMax Krasnyansky 
263f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
264f271b2ccSMax Krasnyansky 		err = -EFAULT;
265f271b2ccSMax Krasnyansky 		goto done;
266f271b2ccSMax Krasnyansky 	}
267f271b2ccSMax Krasnyansky 
268f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
269f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
270f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
271f271b2ccSMax Krasnyansky 	filter->count = 0;
272f271b2ccSMax Krasnyansky 	wmb();
273f271b2ccSMax Krasnyansky 
274f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
275f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
276f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
277f271b2ccSMax Krasnyansky 
278f271b2ccSMax Krasnyansky 	nexact = n;
279f271b2ccSMax Krasnyansky 
280cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
281cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
282f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
283cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
284cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
285cfbf84fcSAlex Williamson 			err = 0; /* no filter */
286cfbf84fcSAlex Williamson 			goto done;
287cfbf84fcSAlex Williamson 		}
288f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
289cfbf84fcSAlex Williamson 	}
290f271b2ccSMax Krasnyansky 
291f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
292f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
293f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
294f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
295f271b2ccSMax Krasnyansky 
296f271b2ccSMax Krasnyansky 	/* Now enable the filter */
297f271b2ccSMax Krasnyansky 	wmb();
298f271b2ccSMax Krasnyansky 	filter->count = nexact;
299f271b2ccSMax Krasnyansky 
300f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
301f271b2ccSMax Krasnyansky 	err = nexact;
302f271b2ccSMax Krasnyansky 
303f271b2ccSMax Krasnyansky done:
304f271b2ccSMax Krasnyansky 	kfree(addr);
305f271b2ccSMax Krasnyansky 	return err;
306f271b2ccSMax Krasnyansky }
307f271b2ccSMax Krasnyansky 
308f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
309f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
310f271b2ccSMax Krasnyansky {
311f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
312f271b2ccSMax Krasnyansky 	 * at this point. */
313f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
314f271b2ccSMax Krasnyansky 	int i;
315f271b2ccSMax Krasnyansky 
316f271b2ccSMax Krasnyansky 	/* Exact match */
317f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
3182e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
319f271b2ccSMax Krasnyansky 			return 1;
320f271b2ccSMax Krasnyansky 
321f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
322f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
323f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
324f271b2ccSMax Krasnyansky 
325f271b2ccSMax Krasnyansky 	return 0;
326f271b2ccSMax Krasnyansky }
327f271b2ccSMax Krasnyansky 
328f271b2ccSMax Krasnyansky /*
329f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
330f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
331f271b2ccSMax Krasnyansky  */
332f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
333f271b2ccSMax Krasnyansky {
334f271b2ccSMax Krasnyansky 	if (!filter->count)
335f271b2ccSMax Krasnyansky 		return 1;
336f271b2ccSMax Krasnyansky 
337f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
338f271b2ccSMax Krasnyansky }
339f271b2ccSMax Krasnyansky 
3401da177e4SLinus Torvalds /* Network device part of the driver */
3411da177e4SLinus Torvalds 
3427282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
3431da177e4SLinus Torvalds 
344c70f1829SEric W. Biederman /* Net device detach from fd. */
345c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
346c70f1829SEric W. Biederman {
347c70f1829SEric W. Biederman 	struct tun_struct *tun = netdev_priv(dev);
348c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
349c70f1829SEric W. Biederman 
350c70f1829SEric W. Biederman 	/* Inform the methods they need to stop using the dev.
351c70f1829SEric W. Biederman 	 */
352c70f1829SEric W. Biederman 	if (tfile) {
35343815482SEric Dumazet 		wake_up_all(&tun->wq.wait);
354c70f1829SEric W. Biederman 		if (atomic_dec_and_test(&tfile->count))
355c70f1829SEric W. Biederman 			__tun_detach(tun);
356c70f1829SEric W. Biederman 	}
357c70f1829SEric W. Biederman }
358c70f1829SEric W. Biederman 
3599c3fea6aSHerbert Xu static void tun_free_netdev(struct net_device *dev)
3609c3fea6aSHerbert Xu {
3619c3fea6aSHerbert Xu 	struct tun_struct *tun = netdev_priv(dev);
3629c3fea6aSHerbert Xu 
363b09e786bSMikulas Patocka 	BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags));
364b09e786bSMikulas Patocka 
3651ab5ecb9SStanislav Kinsbursky 	sk_release_kernel(tun->socket.sk);
3669c3fea6aSHerbert Xu }
3679c3fea6aSHerbert Xu 
3681da177e4SLinus Torvalds /* Net device open. */
3691da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	netif_start_queue(dev);
3721da177e4SLinus Torvalds 	return 0;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds /* Net device close. */
3761da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	netif_stop_queue(dev);
3791da177e4SLinus Torvalds 	return 0;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds /* Net device start xmit */
383424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3861da177e4SLinus Torvalds 
3876b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
390631ab46bSEric W. Biederman 	if (!tun->tfile)
3911da177e4SLinus Torvalds 		goto drop;
3921da177e4SLinus Torvalds 
393f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
394f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
395f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
396f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
397f271b2ccSMax Krasnyansky 		goto drop;
398f271b2ccSMax Krasnyansky 
39999405162SMichael S. Tsirkin 	if (tun->socket.sk->sk_filter &&
40099405162SMichael S. Tsirkin 	    sk_filter(tun->socket.sk, skb))
40199405162SMichael S. Tsirkin 		goto drop;
40299405162SMichael S. Tsirkin 
40389f56d1eSMichael S. Tsirkin 	if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
4041da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
4051da177e4SLinus Torvalds 			/* Normal queueing mode. */
4061da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
4071da177e4SLinus Torvalds 			netif_stop_queue(dev);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
4101da177e4SLinus Torvalds 			 * error is more appropriate. */
41109f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
4121da177e4SLinus Torvalds 		} else {
4131da177e4SLinus Torvalds 			/* Single queue mode.
4141da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
4151da177e4SLinus Torvalds 			goto drop;
4161da177e4SLinus Torvalds 		}
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 
4190110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
4200110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
421868eefebSMichael S. Tsirkin 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
422868eefebSMichael S. Tsirkin 		goto drop;
4230110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
4240110d6f2SMichael S. Tsirkin 
425f271b2ccSMax Krasnyansky 	/* Enqueue packet */
42689f56d1eSMichael S. Tsirkin 	skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/* Notify and wake up reader process */
4291da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
4301da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
43143815482SEric Dumazet 	wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
43205c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
4336ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds drop:
43609f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
4371da177e4SLinus Torvalds 	kfree_skb(skb);
4386ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
441f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
4421da177e4SLinus Torvalds {
443f271b2ccSMax Krasnyansky 	/*
444f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
445f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
446f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
447f271b2ccSMax Krasnyansky 	 */
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4504885a504SEd Swierk #define MIN_MTU 68
4514885a504SEd Swierk #define MAX_MTU 65535
4524885a504SEd Swierk 
4534885a504SEd Swierk static int
4544885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
4554885a504SEd Swierk {
4564885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
4574885a504SEd Swierk 		return -EINVAL;
4584885a504SEd Swierk 	dev->mtu = new_mtu;
4594885a504SEd Swierk 	return 0;
4604885a504SEd Swierk }
4614885a504SEd Swierk 
462c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
463c8f44affSMichał Mirosław 	netdev_features_t features)
46488255375SMichał Mirosław {
46588255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
46688255375SMichał Mirosław 
46788255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
46888255375SMichał Mirosław }
469bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
470bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
471bebd097aSNeil Horman {
472bebd097aSNeil Horman 	/*
473bebd097aSNeil Horman 	 * Tun only receives frames when:
474bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
475bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
476bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
477bebd097aSNeil Horman 	 * never to have pending data when we poll for it
478bebd097aSNeil Horman 	 * so theres nothing to do here but return.
479bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
480bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
481bebd097aSNeil Horman 	 * still use netconsole
482bebd097aSNeil Horman 	 */
483bebd097aSNeil Horman 	return;
484bebd097aSNeil Horman }
485bebd097aSNeil Horman #endif
486758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
487c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
488758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
489758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
49000829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
491758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
49288255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
493bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
494bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
495bebd097aSNeil Horman #endif
496758e43b7SStephen Hemminger };
497758e43b7SStephen Hemminger 
498758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
499c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
500758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
501758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
50200829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
503758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
50488255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
505afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
506758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
507758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
508bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
509bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
510bebd097aSNeil Horman #endif
511758e43b7SStephen Hemminger };
512758e43b7SStephen Hemminger 
5131da177e4SLinus Torvalds /* Initialize net device. */
5141da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
5191da177e4SLinus Torvalds 	case TUN_TUN_DEV:
520758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
521758e43b7SStephen Hemminger 
5221da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
5231da177e4SLinus Torvalds 		dev->hard_header_len = 0;
5241da177e4SLinus Torvalds 		dev->addr_len = 0;
5251da177e4SLinus Torvalds 		dev->mtu = 1500;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		/* Zero header length */
5281da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
5291da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
5301da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5311da177e4SLinus Torvalds 		break;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	case TUN_TAP_DEV:
5347a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
5351da177e4SLinus Torvalds 		/* Ethernet TAP Device */
5361da177e4SLinus Torvalds 		ether_setup(dev);
537550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
53836226a8dSBrian Braunstein 
539f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
54036226a8dSBrian Braunstein 
5411da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5421da177e4SLinus Torvalds 		break;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds /* Character device part */
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds /* Poll */
5491da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
5501da177e4SLinus Torvalds {
551b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
552b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
5533c8a9c63SMariusz Kozlowski 	struct sock *sk;
55433dccbb0SHerbert Xu 	unsigned int mask = 0;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (!tun)
557eac9e902SEric W. Biederman 		return POLLERR;
5581da177e4SLinus Torvalds 
55989f56d1eSMichael S. Tsirkin 	sk = tun->socket.sk;
5603c8a9c63SMariusz Kozlowski 
5616b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
5621da177e4SLinus Torvalds 
56343815482SEric Dumazet 	poll_wait(file, &tun->wq.wait, wait);
5641da177e4SLinus Torvalds 
56589f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
5661da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5671da177e4SLinus Torvalds 
56833dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
56933dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
57033dccbb0SHerbert Xu 	     sock_writeable(sk)))
57133dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
57233dccbb0SHerbert Xu 
573c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
574c70f1829SEric W. Biederman 		mask = POLLERR;
575c70f1829SEric W. Biederman 
576631ab46bSEric W. Biederman 	tun_put(tun);
5771da177e4SLinus Torvalds 	return mask;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
580f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
581f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
5826f7c156cSstephen hemminger static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
58333dccbb0SHerbert Xu 				     size_t prepad, size_t len,
58433dccbb0SHerbert Xu 				     size_t linear, int noblock)
585f42157cbSRusty Russell {
58689f56d1eSMichael S. Tsirkin 	struct sock *sk = tun->socket.sk;
587f42157cbSRusty Russell 	struct sk_buff *skb;
58833dccbb0SHerbert Xu 	int err;
589f42157cbSRusty Russell 
59082862742SHerbert Xu 	sock_update_classid(sk);
59182862742SHerbert Xu 
592f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
5930eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
59433dccbb0SHerbert Xu 		linear = len;
595f42157cbSRusty Russell 
59633dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
59733dccbb0SHerbert Xu 				   &err);
598f42157cbSRusty Russell 	if (!skb)
59933dccbb0SHerbert Xu 		return ERR_PTR(err);
600f42157cbSRusty Russell 
601f42157cbSRusty Russell 	skb_reserve(skb, prepad);
602f42157cbSRusty Russell 	skb_put(skb, linear);
60333dccbb0SHerbert Xu 	skb->data_len = len - linear;
60433dccbb0SHerbert Xu 	skb->len += len - linear;
605f42157cbSRusty Russell 
606f42157cbSRusty Russell 	return skb;
607f42157cbSRusty Russell }
608f42157cbSRusty Russell 
6090690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */
6100690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
6110690899bSMichael S. Tsirkin 				  int offset, size_t count)
6120690899bSMichael S. Tsirkin {
6130690899bSMichael S. Tsirkin 	int len = iov_length(from, count) - offset;
6140690899bSMichael S. Tsirkin 	int copy = skb_headlen(skb);
6150690899bSMichael S. Tsirkin 	int size, offset1 = 0;
6160690899bSMichael S. Tsirkin 	int i = 0;
6170690899bSMichael S. Tsirkin 
6180690899bSMichael S. Tsirkin 	/* Skip over from offset */
6190690899bSMichael S. Tsirkin 	while (count && (offset >= from->iov_len)) {
6200690899bSMichael S. Tsirkin 		offset -= from->iov_len;
6210690899bSMichael S. Tsirkin 		++from;
6220690899bSMichael S. Tsirkin 		--count;
6230690899bSMichael S. Tsirkin 	}
6240690899bSMichael S. Tsirkin 
6250690899bSMichael S. Tsirkin 	/* copy up to skb headlen */
6260690899bSMichael S. Tsirkin 	while (count && (copy > 0)) {
6270690899bSMichael S. Tsirkin 		size = min_t(unsigned int, copy, from->iov_len - offset);
6280690899bSMichael S. Tsirkin 		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
6290690899bSMichael S. Tsirkin 				   size))
6300690899bSMichael S. Tsirkin 			return -EFAULT;
6310690899bSMichael S. Tsirkin 		if (copy > size) {
6320690899bSMichael S. Tsirkin 			++from;
6330690899bSMichael S. Tsirkin 			--count;
6340690899bSMichael S. Tsirkin 			offset = 0;
6350690899bSMichael S. Tsirkin 		} else
6360690899bSMichael S. Tsirkin 			offset += size;
6370690899bSMichael S. Tsirkin 		copy -= size;
6380690899bSMichael S. Tsirkin 		offset1 += size;
6390690899bSMichael S. Tsirkin 	}
6400690899bSMichael S. Tsirkin 
6410690899bSMichael S. Tsirkin 	if (len == offset1)
6420690899bSMichael S. Tsirkin 		return 0;
6430690899bSMichael S. Tsirkin 
6440690899bSMichael S. Tsirkin 	while (count--) {
6450690899bSMichael S. Tsirkin 		struct page *page[MAX_SKB_FRAGS];
6460690899bSMichael S. Tsirkin 		int num_pages;
6470690899bSMichael S. Tsirkin 		unsigned long base;
6480690899bSMichael S. Tsirkin 		unsigned long truesize;
6490690899bSMichael S. Tsirkin 
6500690899bSMichael S. Tsirkin 		len = from->iov_len - offset;
6510690899bSMichael S. Tsirkin 		if (!len) {
6520690899bSMichael S. Tsirkin 			offset = 0;
6530690899bSMichael S. Tsirkin 			++from;
6540690899bSMichael S. Tsirkin 			continue;
6550690899bSMichael S. Tsirkin 		}
6560690899bSMichael S. Tsirkin 		base = (unsigned long)from->iov_base + offset;
6570690899bSMichael S. Tsirkin 		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
6580690899bSMichael S. Tsirkin 		if (i + size > MAX_SKB_FRAGS)
6590690899bSMichael S. Tsirkin 			return -EMSGSIZE;
6600690899bSMichael S. Tsirkin 		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
6610690899bSMichael S. Tsirkin 		if (num_pages != size) {
6620690899bSMichael S. Tsirkin 			for (i = 0; i < num_pages; i++)
6630690899bSMichael S. Tsirkin 				put_page(page[i]);
6640690899bSMichael S. Tsirkin 			return -EFAULT;
6650690899bSMichael S. Tsirkin 		}
6660690899bSMichael S. Tsirkin 		truesize = size * PAGE_SIZE;
6670690899bSMichael S. Tsirkin 		skb->data_len += len;
6680690899bSMichael S. Tsirkin 		skb->len += len;
6690690899bSMichael S. Tsirkin 		skb->truesize += truesize;
6700690899bSMichael S. Tsirkin 		atomic_add(truesize, &skb->sk->sk_wmem_alloc);
6710690899bSMichael S. Tsirkin 		while (len) {
6720690899bSMichael S. Tsirkin 			int off = base & ~PAGE_MASK;
6730690899bSMichael S. Tsirkin 			int size = min_t(int, len, PAGE_SIZE - off);
6740690899bSMichael S. Tsirkin 			__skb_fill_page_desc(skb, i, page[i], off, size);
6750690899bSMichael S. Tsirkin 			skb_shinfo(skb)->nr_frags++;
6760690899bSMichael S. Tsirkin 			/* increase sk_wmem_alloc */
6770690899bSMichael S. Tsirkin 			base += size;
6780690899bSMichael S. Tsirkin 			len -= size;
6790690899bSMichael S. Tsirkin 			i++;
6800690899bSMichael S. Tsirkin 		}
6810690899bSMichael S. Tsirkin 		offset = 0;
6820690899bSMichael S. Tsirkin 		++from;
6830690899bSMichael S. Tsirkin 	}
6840690899bSMichael S. Tsirkin 	return 0;
6850690899bSMichael S. Tsirkin }
6860690899bSMichael S. Tsirkin 
6871da177e4SLinus Torvalds /* Get packet from user space buffer */
6880690899bSMichael S. Tsirkin static ssize_t tun_get_user(struct tun_struct *tun, void *msg_control,
6890690899bSMichael S. Tsirkin 			    const struct iovec *iv, size_t total_len,
6900690899bSMichael S. Tsirkin 			    size_t count, int noblock)
6911da177e4SLinus Torvalds {
69209640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
6931da177e4SLinus Torvalds 	struct sk_buff *skb;
6940690899bSMichael S. Tsirkin 	size_t len = total_len, align = NET_SKB_PAD;
695f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
6966f26c9a7SMichael S. Tsirkin 	int offset = 0;
6970690899bSMichael S. Tsirkin 	int copylen;
6980690899bSMichael S. Tsirkin 	bool zerocopy = false;
6990690899bSMichael S. Tsirkin 	int err;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
7020690899bSMichael S. Tsirkin 		if ((len -= sizeof(pi)) > total_len)
7031da177e4SLinus Torvalds 			return -EINVAL;
7041da177e4SLinus Torvalds 
7056f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
7061da177e4SLinus Torvalds 			return -EFAULT;
7076f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
710f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
7110690899bSMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) > total_len)
712f43798c2SRusty Russell 			return -EINVAL;
713f43798c2SRusty Russell 
7146f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
715f43798c2SRusty Russell 			return -EFAULT;
716f43798c2SRusty Russell 
7174909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
7184909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
7194909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
7204909122fSHerbert Xu 
721f43798c2SRusty Russell 		if (gso.hdr_len > len)
722f43798c2SRusty Russell 			return -EINVAL;
723d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
724f43798c2SRusty Russell 	}
725f43798c2SRusty Russell 
726e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
727a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
7280eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
7290eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
730e01bf1c8SRusty Russell 			return -EINVAL;
731e01bf1c8SRusty Russell 	}
7321da177e4SLinus Torvalds 
7330690899bSMichael S. Tsirkin 	if (msg_control)
7340690899bSMichael S. Tsirkin 		zerocopy = true;
7350690899bSMichael S. Tsirkin 
7360690899bSMichael S. Tsirkin 	if (zerocopy) {
7370690899bSMichael S. Tsirkin 		/* Userspace may produce vectors with count greater than
7380690899bSMichael S. Tsirkin 		 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
7390690899bSMichael S. Tsirkin 		 * to let the rest of data to be fit in the frags.
7400690899bSMichael S. Tsirkin 		 */
7410690899bSMichael S. Tsirkin 		if (count > MAX_SKB_FRAGS) {
7420690899bSMichael S. Tsirkin 			copylen = iov_length(iv, count - MAX_SKB_FRAGS);
7430690899bSMichael S. Tsirkin 			if (copylen < offset)
7440690899bSMichael S. Tsirkin 				copylen = 0;
7450690899bSMichael S. Tsirkin 			else
7460690899bSMichael S. Tsirkin 				copylen -= offset;
7470690899bSMichael S. Tsirkin 		} else
7480690899bSMichael S. Tsirkin 				copylen = 0;
7490690899bSMichael S. Tsirkin 		/* There are 256 bytes to be copied in skb, so there is enough
7500690899bSMichael S. Tsirkin 		 * room for skb expand head in case it is used.
7510690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
7520690899bSMichael S. Tsirkin 		 */
7530690899bSMichael S. Tsirkin 		if (copylen < gso.hdr_len)
7540690899bSMichael S. Tsirkin 			copylen = gso.hdr_len;
7550690899bSMichael S. Tsirkin 		if (!copylen)
7560690899bSMichael S. Tsirkin 			copylen = GOODCOPY_LEN;
7570690899bSMichael S. Tsirkin 	} else
7580690899bSMichael S. Tsirkin 		copylen = len;
7590690899bSMichael S. Tsirkin 
7600690899bSMichael S. Tsirkin 	skb = tun_alloc_skb(tun, align, copylen, gso.hdr_len, noblock);
76133dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
76233dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
76309f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
76433dccbb0SHerbert Xu 		return PTR_ERR(skb);
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 
7670690899bSMichael S. Tsirkin 	if (zerocopy)
7680690899bSMichael S. Tsirkin 		err = zerocopy_sg_from_iovec(skb, iv, offset, count);
7690690899bSMichael S. Tsirkin 	else
7700690899bSMichael S. Tsirkin 		err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
7710690899bSMichael S. Tsirkin 
7720690899bSMichael S. Tsirkin 	if (err) {
77309f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
7748f22757eSDave Jones 		kfree_skb(skb);
7751da177e4SLinus Torvalds 		return -EFAULT;
7768f22757eSDave Jones 	}
7771da177e4SLinus Torvalds 
778f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
779f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
780f43798c2SRusty Russell 					  gso.csum_offset)) {
781f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
782f43798c2SRusty Russell 			kfree_skb(skb);
783f43798c2SRusty Russell 			return -EINVAL;
784f43798c2SRusty Russell 		}
78588255375SMichał Mirosław 	}
786f43798c2SRusty Russell 
7871da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
7881da177e4SLinus Torvalds 	case TUN_TUN_DEV:
789f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
790f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
791f09f7ee2SAng Way Chuang 			case 0x40:
792f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
793f09f7ee2SAng Way Chuang 				break;
794f09f7ee2SAng Way Chuang 			case 0x60:
795f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
796f09f7ee2SAng Way Chuang 				break;
797f09f7ee2SAng Way Chuang 			default:
798f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
799f09f7ee2SAng Way Chuang 				kfree_skb(skb);
800f09f7ee2SAng Way Chuang 				return -EINVAL;
801f09f7ee2SAng Way Chuang 			}
802f09f7ee2SAng Way Chuang 		}
803f09f7ee2SAng Way Chuang 
804459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
8051da177e4SLinus Torvalds 		skb->protocol = pi.proto;
8064c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
8071da177e4SLinus Torvalds 		break;
8081da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8091da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
8101da177e4SLinus Torvalds 		break;
8116403eab1SJoe Perches 	}
8121da177e4SLinus Torvalds 
813f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
814f43798c2SRusty Russell 		pr_debug("GSO!\n");
815f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
816f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
817f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
818f43798c2SRusty Russell 			break;
819f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
820f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
821f43798c2SRusty Russell 			break;
822e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
823e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
824e36aa25aSSridhar Samudrala 			break;
825f43798c2SRusty Russell 		default:
826f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
827f43798c2SRusty Russell 			kfree_skb(skb);
828f43798c2SRusty Russell 			return -EINVAL;
829f43798c2SRusty Russell 		}
830f43798c2SRusty Russell 
831f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
832f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
833f43798c2SRusty Russell 
834f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
835f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
836f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
837f43798c2SRusty Russell 			kfree_skb(skb);
838f43798c2SRusty Russell 			return -EINVAL;
839f43798c2SRusty Russell 		}
840f43798c2SRusty Russell 
841f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
842f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
843f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
844f43798c2SRusty Russell 	}
8451da177e4SLinus Torvalds 
8460690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
8470690899bSMichael S. Tsirkin 	if (zerocopy) {
8480690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
8490690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
8500690899bSMichael S. Tsirkin 	}
8510690899bSMichael S. Tsirkin 
8521da177e4SLinus Torvalds 	netif_rx_ni(skb);
8531da177e4SLinus Torvalds 
85409f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
85509f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
8561da177e4SLinus Torvalds 
8570690899bSMichael S. Tsirkin 	return total_len;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
860ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
861ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
8621da177e4SLinus Torvalds {
86333dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
864ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
865631ab46bSEric W. Biederman 	ssize_t result;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	if (!tun)
8681da177e4SLinus Torvalds 		return -EBADFD;
8691da177e4SLinus Torvalds 
8706b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
8711da177e4SLinus Torvalds 
8720690899bSMichael S. Tsirkin 	result = tun_get_user(tun, NULL, iv, iov_length(iv, count), count,
87333dccbb0SHerbert Xu 			      file->f_flags & O_NONBLOCK);
874631ab46bSEric W. Biederman 
875631ab46bSEric W. Biederman 	tun_put(tun);
876631ab46bSEric W. Biederman 	return result;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds /* Put packet to the user space buffer */
8806f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
8811da177e4SLinus Torvalds 			    struct sk_buff *skb,
88243b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
8851da177e4SLinus Torvalds 	ssize_t total = 0;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
8881da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
8891da177e4SLinus Torvalds 			return -EINVAL;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 		if (len < skb->len) {
8921da177e4SLinus Torvalds 			/* Packet will be striped */
8931da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
8941da177e4SLinus Torvalds 		}
8951da177e4SLinus Torvalds 
89643b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
8971da177e4SLinus Torvalds 			return -EFAULT;
8981da177e4SLinus Torvalds 		total += sizeof(pi);
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
901f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
902f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
903d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
904f43798c2SRusty Russell 			return -EINVAL;
905f43798c2SRusty Russell 
906f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
907f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
908f43798c2SRusty Russell 
909f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
910f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
911f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
912f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
913f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
914f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
915f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
916e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
917e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
918ef3db4a5SMichael S. Tsirkin 			else {
9196b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
920ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
921ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
922ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
923ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
924ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
925ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
926ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
927ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
928ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
929ef3db4a5SMichael S. Tsirkin 			}
930f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
931f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
932f43798c2SRusty Russell 		} else
933f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
934f43798c2SRusty Russell 
935f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
936f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
93755508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
938f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
93910a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
94010a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
941f43798c2SRusty Russell 		} /* else everything is zero */
942f43798c2SRusty Russell 
94343b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
94443b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
945f43798c2SRusty Russell 			return -EFAULT;
946d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
947f43798c2SRusty Russell 	}
948f43798c2SRusty Russell 
9491da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
9501da177e4SLinus Torvalds 
95143b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
95205c2828cSMichael S. Tsirkin 	total += skb->len;
9531da177e4SLinus Torvalds 
95409f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
95509f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	return total;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
96005c2828cSMichael S. Tsirkin static ssize_t tun_do_read(struct tun_struct *tun,
96105c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
96205c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
9651da177e4SLinus Torvalds 	struct sk_buff *skb;
96605c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
9671da177e4SLinus Torvalds 
9686b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_read\n");
9691da177e4SLinus Torvalds 
97061a5ff15SAmos Kong 	if (unlikely(!noblock))
97143815482SEric Dumazet 		add_wait_queue(&tun->wq.wait, &wait);
9721da177e4SLinus Torvalds 	while (len) {
9731da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 		/* Read frames from the queue */
97689f56d1eSMichael S. Tsirkin 		if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
97705c2828cSMichael S. Tsirkin 			if (noblock) {
9781da177e4SLinus Torvalds 				ret = -EAGAIN;
9791da177e4SLinus Torvalds 				break;
9801da177e4SLinus Torvalds 			}
9811da177e4SLinus Torvalds 			if (signal_pending(current)) {
9821da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
9831da177e4SLinus Torvalds 				break;
9841da177e4SLinus Torvalds 			}
985c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
986c70f1829SEric W. Biederman 				ret = -EIO;
987c70f1829SEric W. Biederman 				break;
988c70f1829SEric W. Biederman 			}
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
9911da177e4SLinus Torvalds 			schedule();
9921da177e4SLinus Torvalds 			continue;
9931da177e4SLinus Torvalds 		}
9941da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
9951da177e4SLinus Torvalds 
99643b39dcdSMichael S. Tsirkin 		ret = tun_put_user(tun, skb, iv, len);
9971da177e4SLinus Torvalds 		kfree_skb(skb);
9981da177e4SLinus Torvalds 		break;
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
100261a5ff15SAmos Kong 	if (unlikely(!noblock))
100343815482SEric Dumazet 		remove_wait_queue(&tun->wq.wait, &wait);
10041da177e4SLinus Torvalds 
100505c2828cSMichael S. Tsirkin 	return ret;
100605c2828cSMichael S. Tsirkin }
100705c2828cSMichael S. Tsirkin 
100805c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
100905c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
101005c2828cSMichael S. Tsirkin {
101105c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
101205c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
101305c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
101405c2828cSMichael S. Tsirkin 	ssize_t len, ret;
101505c2828cSMichael S. Tsirkin 
101605c2828cSMichael S. Tsirkin 	if (!tun)
101705c2828cSMichael S. Tsirkin 		return -EBADFD;
101805c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
101905c2828cSMichael S. Tsirkin 	if (len < 0) {
102005c2828cSMichael S. Tsirkin 		ret = -EINVAL;
102105c2828cSMichael S. Tsirkin 		goto out;
102205c2828cSMichael S. Tsirkin 	}
102305c2828cSMichael S. Tsirkin 
102405c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
102505c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
1026631ab46bSEric W. Biederman out:
1027631ab46bSEric W. Biederman 	tun_put(tun);
10281da177e4SLinus Torvalds 	return ret;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
10341da177e4SLinus Torvalds 
1035*0625c883SEric W. Biederman 	tun->owner = INVALID_UID;
1036*0625c883SEric W. Biederman 	tun->group = INVALID_GID;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
10399c3fea6aSHerbert Xu 	dev->destructor = tun_free_netdev;
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
1042f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1043f019a7a5SEric W. Biederman  * device with netlink.
1044f019a7a5SEric W. Biederman  */
1045f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1046f019a7a5SEric W. Biederman {
1047f019a7a5SEric W. Biederman 	return -EINVAL;
1048f019a7a5SEric W. Biederman }
1049f019a7a5SEric W. Biederman 
1050f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1051f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1052f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1053f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1054f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1055f019a7a5SEric W. Biederman };
1056f019a7a5SEric W. Biederman 
105733dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
105833dccbb0SHerbert Xu {
105933dccbb0SHerbert Xu 	struct tun_struct *tun;
106043815482SEric Dumazet 	wait_queue_head_t *wqueue;
106133dccbb0SHerbert Xu 
106233dccbb0SHerbert Xu 	if (!sock_writeable(sk))
106333dccbb0SHerbert Xu 		return;
106433dccbb0SHerbert Xu 
106533dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
106633dccbb0SHerbert Xu 		return;
106733dccbb0SHerbert Xu 
106843815482SEric Dumazet 	wqueue = sk_sleep(sk);
106943815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
107043815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
107105c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1072c722c625SHerbert Xu 
107380924e5fSVitaliy Gusev 	tun = tun_sk(sk)->tun;
107433dccbb0SHerbert Xu 	kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
107533dccbb0SHerbert Xu }
107633dccbb0SHerbert Xu 
107733dccbb0SHerbert Xu static void tun_sock_destruct(struct sock *sk)
107833dccbb0SHerbert Xu {
107980924e5fSVitaliy Gusev 	free_netdev(tun_sk(sk)->tun->dev);
108033dccbb0SHerbert Xu }
108133dccbb0SHerbert Xu 
108205c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
108305c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
108405c2828cSMichael S. Tsirkin {
108505c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
10860690899bSMichael S. Tsirkin 	return tun_get_user(tun, m->msg_control, m->msg_iov, total_len,
10870690899bSMichael S. Tsirkin 			    m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
108805c2828cSMichael S. Tsirkin }
108905c2828cSMichael S. Tsirkin 
109005c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
109105c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
109205c2828cSMichael S. Tsirkin 		       int flags)
109305c2828cSMichael S. Tsirkin {
109405c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
109505c2828cSMichael S. Tsirkin 	int ret;
109605c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
109705c2828cSMichael S. Tsirkin 		return -EINVAL;
109805c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
109905c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
110005c2828cSMichael S. Tsirkin 	if (ret > total_len) {
110105c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
110205c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
110305c2828cSMichael S. Tsirkin 	}
110405c2828cSMichael S. Tsirkin 	return ret;
110505c2828cSMichael S. Tsirkin }
110605c2828cSMichael S. Tsirkin 
11071ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
11081ab5ecb9SStanislav Kinsbursky {
11091ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
11101ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
11111ab5ecb9SStanislav Kinsbursky 	return 0;
11121ab5ecb9SStanislav Kinsbursky }
11131ab5ecb9SStanislav Kinsbursky 
111405c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
111505c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
111605c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
111705c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
11181ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
111905c2828cSMichael S. Tsirkin };
112005c2828cSMichael S. Tsirkin 
112133dccbb0SHerbert Xu static struct proto tun_proto = {
112233dccbb0SHerbert Xu 	.name		= "tun",
112333dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
112433dccbb0SHerbert Xu 	.obj_size	= sizeof(struct tun_sock),
112533dccbb0SHerbert Xu };
1126f019a7a5SEric W. Biederman 
1127980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1128980c9e8cSDavid Woodhouse {
1129980c9e8cSDavid Woodhouse 	int flags = 0;
1130980c9e8cSDavid Woodhouse 
1131980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1132980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1133980c9e8cSDavid Woodhouse 	else
1134980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1135980c9e8cSDavid Woodhouse 
1136980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1137980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1138980c9e8cSDavid Woodhouse 
1139980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1140980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1141980c9e8cSDavid Woodhouse 
1142980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1143980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1144980c9e8cSDavid Woodhouse 
1145980c9e8cSDavid Woodhouse 	return flags;
1146980c9e8cSDavid Woodhouse }
1147980c9e8cSDavid Woodhouse 
1148980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1149980c9e8cSDavid Woodhouse 			      char *buf)
1150980c9e8cSDavid Woodhouse {
1151980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1152980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1153980c9e8cSDavid Woodhouse }
1154980c9e8cSDavid Woodhouse 
1155980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1156980c9e8cSDavid Woodhouse 			      char *buf)
1157980c9e8cSDavid Woodhouse {
1158980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1159*0625c883SEric W. Biederman 	return uid_valid(tun->owner)?
1160*0625c883SEric W. Biederman 		sprintf(buf, "%u\n",
1161*0625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
1162*0625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1163980c9e8cSDavid Woodhouse }
1164980c9e8cSDavid Woodhouse 
1165980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1166980c9e8cSDavid Woodhouse 			      char *buf)
1167980c9e8cSDavid Woodhouse {
1168980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1169*0625c883SEric W. Biederman 	return gid_valid(tun->group) ?
1170*0625c883SEric W. Biederman 		sprintf(buf, "%u\n",
1171*0625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
1172*0625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1173980c9e8cSDavid Woodhouse }
1174980c9e8cSDavid Woodhouse 
1175980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1176980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1177980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1178980c9e8cSDavid Woodhouse 
1179d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
11801da177e4SLinus Torvalds {
118133dccbb0SHerbert Xu 	struct sock *sk;
11821da177e4SLinus Torvalds 	struct tun_struct *tun;
11831da177e4SLinus Torvalds 	struct net_device *dev;
11841da177e4SLinus Torvalds 	int err;
11851da177e4SLinus Torvalds 
118674a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
118774a3e5a7SEric W. Biederman 	if (dev) {
11882b980dbdSPaul Moore 		const struct cred *cred = current_cred();
11892b980dbdSPaul Moore 
1190f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1191f85ba780SDavid Woodhouse 			return -EBUSY;
119274a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
119374a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
119474a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
119574a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
119674a3e5a7SEric W. Biederman 		else
119774a3e5a7SEric W. Biederman 			return -EINVAL;
119874a3e5a7SEric W. Biederman 
1199*0625c883SEric W. Biederman 		if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
1200*0625c883SEric W. Biederman 		     (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
12012b980dbdSPaul Moore 		    !capable(CAP_NET_ADMIN))
12022b980dbdSPaul Moore 			return -EPERM;
1203d7e9660aSLinus Torvalds 		err = security_tun_dev_attach(tun->socket.sk);
12042b980dbdSPaul Moore 		if (err < 0)
12052b980dbdSPaul Moore 			return err;
12062b980dbdSPaul Moore 
1207a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1208a7385ba2SEric W. Biederman 		if (err < 0)
1209a7385ba2SEric W. Biederman 			return err;
121086a264abSDavid Howells 	}
12111da177e4SLinus Torvalds 	else {
12121da177e4SLinus Torvalds 		char *name;
12131da177e4SLinus Torvalds 		unsigned long flags = 0;
12141da177e4SLinus Torvalds 
1215ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
1216ca6bb5d7SDavid Woodhouse 			return -EPERM;
12172b980dbdSPaul Moore 		err = security_tun_dev_create();
12182b980dbdSPaul Moore 		if (err < 0)
12192b980dbdSPaul Moore 			return err;
1220ca6bb5d7SDavid Woodhouse 
12211da177e4SLinus Torvalds 		/* Set dev type */
12221da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
12231da177e4SLinus Torvalds 			/* TUN device */
12241da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
12251da177e4SLinus Torvalds 			name = "tun%d";
12261da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
12271da177e4SLinus Torvalds 			/* TAP device */
12281da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
12291da177e4SLinus Torvalds 			name = "tap%d";
12301da177e4SLinus Torvalds 		} else
123136989b90SKusanagi Kouichi 			return -EINVAL;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 		if (*ifr->ifr_name)
12341da177e4SLinus Torvalds 			name = ifr->ifr_name;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
12371da177e4SLinus Torvalds 				   tun_setup);
12381da177e4SLinus Torvalds 		if (!dev)
12391da177e4SLinus Torvalds 			return -ENOMEM;
12401da177e4SLinus Torvalds 
1241fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1242f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1243758e43b7SStephen Hemminger 
12441da177e4SLinus Torvalds 		tun = netdev_priv(dev);
12451da177e4SLinus Torvalds 		tun->dev = dev;
12461da177e4SLinus Torvalds 		tun->flags = flags;
1247f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1248d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1249b09e786bSMikulas Patocka 		set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);
12501da177e4SLinus Torvalds 
125133dccbb0SHerbert Xu 		err = -ENOMEM;
12521ab5ecb9SStanislav Kinsbursky 		sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
125333dccbb0SHerbert Xu 		if (!sk)
125433dccbb0SHerbert Xu 			goto err_free_dev;
125533dccbb0SHerbert Xu 
12561ab5ecb9SStanislav Kinsbursky 		sk_change_net(sk, net);
125743815482SEric Dumazet 		tun->socket.wq = &tun->wq;
125843815482SEric Dumazet 		init_waitqueue_head(&tun->wq.wait);
125905c2828cSMichael S. Tsirkin 		tun->socket.ops = &tun_socket_ops;
126033dccbb0SHerbert Xu 		sock_init_data(&tun->socket, sk);
126133dccbb0SHerbert Xu 		sk->sk_write_space = tun_sock_write_space;
126233dccbb0SHerbert Xu 		sk->sk_sndbuf = INT_MAX;
12630690899bSMichael S. Tsirkin 		sock_set_flag(sk, SOCK_ZEROCOPY);
126433dccbb0SHerbert Xu 
126580924e5fSVitaliy Gusev 		tun_sk(sk)->tun = tun;
126633dccbb0SHerbert Xu 
12672b980dbdSPaul Moore 		security_tun_dev_post_create(sk);
12682b980dbdSPaul Moore 
12691da177e4SLinus Torvalds 		tun_net_init(dev);
12701da177e4SLinus Torvalds 
127188255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
127288255375SMichał Mirosław 			TUN_USER_FEATURES;
127388255375SMichał Mirosław 		dev->features = dev->hw_features;
127488255375SMichał Mirosław 
12751da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
12761da177e4SLinus Torvalds 		if (err < 0)
12779c3fea6aSHerbert Xu 			goto err_free_sk;
12789c3fea6aSHerbert Xu 
1279980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1280980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1281980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
12826b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1283980c9e8cSDavid Woodhouse 
12849c3fea6aSHerbert Xu 		sk->sk_destruct = tun_sock_destruct;
1285a7385ba2SEric W. Biederman 
1286a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1287a7385ba2SEric W. Biederman 		if (err < 0)
12889c3fea6aSHerbert Xu 			goto failed;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
12916b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
12941da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1295a26af1e0SNathaniel Filardo 	else
1296a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
12991da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1300a26af1e0SNathaniel Filardo 	else
1301a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
13021da177e4SLinus Torvalds 
1303f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1304f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1305f43798c2SRusty Russell 	else
1306f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1307f43798c2SRusty Russell 
1308e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1309e35259a9SMax Krasnyansky 	 * xoff state.
1310e35259a9SMax Krasnyansky 	 */
1311e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1312e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
1313e35259a9SMax Krasnyansky 
13141da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
13151da177e4SLinus Torvalds 	return 0;
13161da177e4SLinus Torvalds 
131733dccbb0SHerbert Xu  err_free_sk:
13181ab5ecb9SStanislav Kinsbursky 	tun_free_netdev(dev);
13191da177e4SLinus Torvalds  err_free_dev:
13201da177e4SLinus Torvalds 	free_netdev(dev);
13211da177e4SLinus Torvalds  failed:
13221da177e4SLinus Torvalds 	return err;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
1325876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun,
1326876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1327e3b99556SMark McLoughlin {
13286b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1329e3b99556SMark McLoughlin 
1330e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1331e3b99556SMark McLoughlin 
1332980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1333e3b99556SMark McLoughlin 
1334e3b99556SMark McLoughlin 	return 0;
1335e3b99556SMark McLoughlin }
1336e3b99556SMark McLoughlin 
13375228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
13385228ddc9SRusty Russell  * privs required. */
133988255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
13405228ddc9SRusty Russell {
1341c8f44affSMichał Mirosław 	netdev_features_t features = 0;
13425228ddc9SRusty Russell 
13435228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
134488255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
13455228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
13465228ddc9SRusty Russell 
13475228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
13485228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
13495228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
13505228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
13515228ddc9SRusty Russell 			}
13525228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
13535228ddc9SRusty Russell 				features |= NETIF_F_TSO;
13545228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
13555228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
13565228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
13575228ddc9SRusty Russell 		}
1358e36aa25aSSridhar Samudrala 
1359e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1360e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1361e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1362e36aa25aSSridhar Samudrala 		}
13635228ddc9SRusty Russell 	}
13645228ddc9SRusty Russell 
13655228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
13665228ddc9SRusty Russell 	 * trying to set them. */
13675228ddc9SRusty Russell 	if (arg)
13685228ddc9SRusty Russell 		return -EINVAL;
13695228ddc9SRusty Russell 
137088255375SMichał Mirosław 	tun->set_features = features;
137188255375SMichał Mirosław 	netdev_update_features(tun->dev);
13725228ddc9SRusty Russell 
13735228ddc9SRusty Russell 	return 0;
13745228ddc9SRusty Russell }
13755228ddc9SRusty Russell 
137650857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
137750857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
13781da177e4SLinus Torvalds {
137936b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1380631ab46bSEric W. Biederman 	struct tun_struct *tun;
13811da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
138299405162SMichael S. Tsirkin 	struct sock_fprog fprog;
13831da177e4SLinus Torvalds 	struct ifreq ifr;
1384*0625c883SEric W. Biederman 	kuid_t owner;
1385*0625c883SEric W. Biederman 	kgid_t group;
138633dccbb0SHerbert Xu 	int sndbuf;
1387d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1388f271b2ccSMax Krasnyansky 	int ret;
13891da177e4SLinus Torvalds 
1390a117dacdSMathias Krause 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
139150857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
13921da177e4SLinus Torvalds 			return -EFAULT;
13938bbb1813SDavid S. Miller 	} else {
1394a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
13958bbb1813SDavid S. Miller 	}
1396631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1397631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1398631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1399631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1400631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1401631ab46bSEric W. Biederman 				IFF_VNET_HDR,
1402631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1403631ab46bSEric W. Biederman 	}
1404631ab46bSEric W. Biederman 
1405876bfd4dSHerbert Xu 	rtnl_lock();
1406876bfd4dSHerbert Xu 
140736b50babSEric W. Biederman 	tun = __tun_get(tfile);
14081da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
14091da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
14101da177e4SLinus Torvalds 
1411876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
14121da177e4SLinus Torvalds 
1413876bfd4dSHerbert Xu 		if (ret)
1414876bfd4dSHerbert Xu 			goto unlock;
14151da177e4SLinus Torvalds 
141650857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1417876bfd4dSHerbert Xu 			ret = -EFAULT;
1418876bfd4dSHerbert Xu 		goto unlock;
14191da177e4SLinus Torvalds 	}
14201da177e4SLinus Torvalds 
1421876bfd4dSHerbert Xu 	ret = -EBADFD;
14221da177e4SLinus Torvalds 	if (!tun)
1423876bfd4dSHerbert Xu 		goto unlock;
14241da177e4SLinus Torvalds 
14256b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
14261da177e4SLinus Torvalds 
1427631ab46bSEric W. Biederman 	ret = 0;
14281da177e4SLinus Torvalds 	switch (cmd) {
1429e3b99556SMark McLoughlin 	case TUNGETIFF:
1430876bfd4dSHerbert Xu 		ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1431e3b99556SMark McLoughlin 		if (ret)
1432631ab46bSEric W. Biederman 			break;
1433e3b99556SMark McLoughlin 
143450857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1435631ab46bSEric W. Biederman 			ret = -EFAULT;
1436e3b99556SMark McLoughlin 		break;
1437e3b99556SMark McLoughlin 
14381da177e4SLinus Torvalds 	case TUNSETNOCSUM:
14391da177e4SLinus Torvalds 		/* Disable/Enable checksum */
14401da177e4SLinus Torvalds 
144188255375SMichał Mirosław 		/* [unimplemented] */
144288255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
14436b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
14441da177e4SLinus Torvalds 		break;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	case TUNSETPERSIST:
14471da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
14481da177e4SLinus Torvalds 		if (arg)
14491da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
14501da177e4SLinus Torvalds 		else
14511da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
14521da177e4SLinus Torvalds 
14536b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
14546b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
14551da177e4SLinus Torvalds 		break;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	case TUNSETOWNER:
14581da177e4SLinus Torvalds 		/* Set owner of the device */
1459*0625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
1460*0625c883SEric W. Biederman 		if (!uid_valid(owner)) {
1461*0625c883SEric W. Biederman 			ret = -EINVAL;
1462*0625c883SEric W. Biederman 			break;
1463*0625c883SEric W. Biederman 		}
1464*0625c883SEric W. Biederman 		tun->owner = owner;
1465*0625c883SEric W. Biederman 		tun_debug(KERN_INFO, tun, "owner set to %d\n",
1466*0625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
14671da177e4SLinus Torvalds 		break;
14681da177e4SLinus Torvalds 
14698c644623SGuido Guenther 	case TUNSETGROUP:
14708c644623SGuido Guenther 		/* Set group of the device */
1471*0625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
1472*0625c883SEric W. Biederman 		if (!gid_valid(group)) {
1473*0625c883SEric W. Biederman 			ret = -EINVAL;
1474*0625c883SEric W. Biederman 			break;
1475*0625c883SEric W. Biederman 		}
1476*0625c883SEric W. Biederman 		tun->group = group;
1477*0625c883SEric W. Biederman 		tun_debug(KERN_INFO, tun, "group set to %d\n",
1478*0625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
14798c644623SGuido Guenther 		break;
14808c644623SGuido Guenther 
1481ff4cc3acSMike Kershaw 	case TUNSETLINK:
1482ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1483ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
14846b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
14856b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
148648abfe05SDavid S. Miller 			ret = -EBUSY;
1487ff4cc3acSMike Kershaw 		} else {
1488ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
14896b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
14906b8a66eeSJoe Perches 				  tun->dev->type);
149148abfe05SDavid S. Miller 			ret = 0;
1492ff4cc3acSMike Kershaw 		}
1493631ab46bSEric W. Biederman 		break;
1494ff4cc3acSMike Kershaw 
14951da177e4SLinus Torvalds #ifdef TUN_DEBUG
14961da177e4SLinus Torvalds 	case TUNSETDEBUG:
14971da177e4SLinus Torvalds 		tun->debug = arg;
14981da177e4SLinus Torvalds 		break;
14991da177e4SLinus Torvalds #endif
15005228ddc9SRusty Russell 	case TUNSETOFFLOAD:
150188255375SMichał Mirosław 		ret = set_offload(tun, arg);
1502631ab46bSEric W. Biederman 		break;
15035228ddc9SRusty Russell 
1504f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1505f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1506631ab46bSEric W. Biederman 		ret = -EINVAL;
1507f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1508631ab46bSEric W. Biederman 			break;
1509c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1510631ab46bSEric W. Biederman 		break;
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1513b595076aSUwe Kleine-König 		/* Get hw address */
1514f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1515f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
151650857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1517631ab46bSEric W. Biederman 			ret = -EFAULT;
1518631ab46bSEric W. Biederman 		break;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1521f271b2ccSMax Krasnyansky 		/* Set hw address */
15226b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
15236b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
152440102371SKim B. Heino 
152540102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1526631ab46bSEric W. Biederman 		break;
152733dccbb0SHerbert Xu 
152833dccbb0SHerbert Xu 	case TUNGETSNDBUF:
152989f56d1eSMichael S. Tsirkin 		sndbuf = tun->socket.sk->sk_sndbuf;
153033dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
153133dccbb0SHerbert Xu 			ret = -EFAULT;
153233dccbb0SHerbert Xu 		break;
153333dccbb0SHerbert Xu 
153433dccbb0SHerbert Xu 	case TUNSETSNDBUF:
153533dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
153633dccbb0SHerbert Xu 			ret = -EFAULT;
153733dccbb0SHerbert Xu 			break;
153833dccbb0SHerbert Xu 		}
153933dccbb0SHerbert Xu 
154089f56d1eSMichael S. Tsirkin 		tun->socket.sk->sk_sndbuf = sndbuf;
154133dccbb0SHerbert Xu 		break;
154233dccbb0SHerbert Xu 
1543d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
1544d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
1545d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1546d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1547d9d52b51SMichael S. Tsirkin 		break;
1548d9d52b51SMichael S. Tsirkin 
1549d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
1550d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1551d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1552d9d52b51SMichael S. Tsirkin 			break;
1553d9d52b51SMichael S. Tsirkin 		}
1554d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1555d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
1556d9d52b51SMichael S. Tsirkin 			break;
1557d9d52b51SMichael S. Tsirkin 		}
1558d9d52b51SMichael S. Tsirkin 
1559d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
1560d9d52b51SMichael S. Tsirkin 		break;
1561d9d52b51SMichael S. Tsirkin 
156299405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
156399405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
156499405162SMichael S. Tsirkin 		ret = -EINVAL;
156599405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
156699405162SMichael S. Tsirkin 			break;
156799405162SMichael S. Tsirkin 		ret = -EFAULT;
156899405162SMichael S. Tsirkin 		if (copy_from_user(&fprog, argp, sizeof(fprog)))
156999405162SMichael S. Tsirkin 			break;
157099405162SMichael S. Tsirkin 
157199405162SMichael S. Tsirkin 		ret = sk_attach_filter(&fprog, tun->socket.sk);
157299405162SMichael S. Tsirkin 		break;
157399405162SMichael S. Tsirkin 
157499405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
157599405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
157699405162SMichael S. Tsirkin 		ret = -EINVAL;
157799405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
157899405162SMichael S. Tsirkin 			break;
157999405162SMichael S. Tsirkin 		ret = sk_detach_filter(tun->socket.sk);
158099405162SMichael S. Tsirkin 		break;
158199405162SMichael S. Tsirkin 
15821da177e4SLinus Torvalds 	default:
1583631ab46bSEric W. Biederman 		ret = -EINVAL;
1584631ab46bSEric W. Biederman 		break;
1585ee289b64SJoe Perches 	}
15861da177e4SLinus Torvalds 
1587876bfd4dSHerbert Xu unlock:
1588876bfd4dSHerbert Xu 	rtnl_unlock();
1589876bfd4dSHerbert Xu 	if (tun)
1590631ab46bSEric W. Biederman 		tun_put(tun);
1591631ab46bSEric W. Biederman 	return ret;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
159450857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
159550857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
159650857e2aSArnd Bergmann {
159750857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
159850857e2aSArnd Bergmann }
159950857e2aSArnd Bergmann 
160050857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
160150857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
160250857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
160350857e2aSArnd Bergmann {
160450857e2aSArnd Bergmann 	switch (cmd) {
160550857e2aSArnd Bergmann 	case TUNSETIFF:
160650857e2aSArnd Bergmann 	case TUNGETIFF:
160750857e2aSArnd Bergmann 	case TUNSETTXFILTER:
160850857e2aSArnd Bergmann 	case TUNGETSNDBUF:
160950857e2aSArnd Bergmann 	case TUNSETSNDBUF:
161050857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
161150857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
161250857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
161350857e2aSArnd Bergmann 		break;
161450857e2aSArnd Bergmann 	default:
161550857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
161650857e2aSArnd Bergmann 		break;
161750857e2aSArnd Bergmann 	}
161850857e2aSArnd Bergmann 
161950857e2aSArnd Bergmann 	/*
162050857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
162150857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
162250857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
162350857e2aSArnd Bergmann 	 * contents.
162450857e2aSArnd Bergmann 	 */
162550857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
162650857e2aSArnd Bergmann }
162750857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
162850857e2aSArnd Bergmann 
16291da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
16301da177e4SLinus Torvalds {
1631631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
16321da177e4SLinus Torvalds 	int ret;
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	if (!tun)
16351da177e4SLinus Torvalds 		return -EBADFD;
16361da177e4SLinus Torvalds 
16376b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
16409d319522SJonathan Corbet 		goto out;
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 	if (on) {
1643609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
16441da177e4SLinus Torvalds 		if (ret)
16459d319522SJonathan Corbet 			goto out;
16461da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
16471da177e4SLinus Torvalds 	} else
16481da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
16499d319522SJonathan Corbet 	ret = 0;
16509d319522SJonathan Corbet out:
1651631ab46bSEric W. Biederman 	tun_put(tun);
16529d319522SJonathan Corbet 	return ret;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
16561da177e4SLinus Torvalds {
1657631ab46bSEric W. Biederman 	struct tun_file *tfile;
1658deed49fbSThomas Gleixner 
16596b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1660631ab46bSEric W. Biederman 
1661631ab46bSEric W. Biederman 	tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1662631ab46bSEric W. Biederman 	if (!tfile)
1663631ab46bSEric W. Biederman 		return -ENOMEM;
1664c70f1829SEric W. Biederman 	atomic_set(&tfile->count, 0);
1665631ab46bSEric W. Biederman 	tfile->tun = NULL;
166636b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1667631ab46bSEric W. Biederman 	file->private_data = tfile;
16681da177e4SLinus Torvalds 	return 0;
16691da177e4SLinus Torvalds }
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
16721da177e4SLinus Torvalds {
1673631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1674f0a4d0e5SEric W. Biederman 	struct tun_struct *tun;
16751da177e4SLinus Torvalds 
1676f0a4d0e5SEric W. Biederman 	tun = __tun_get(tfile);
1677631ab46bSEric W. Biederman 	if (tun) {
1678d23e4365SHerbert Xu 		struct net_device *dev = tun->dev;
1679d23e4365SHerbert Xu 
16806b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "tun_chr_close\n");
16811da177e4SLinus Torvalds 
1682631ab46bSEric W. Biederman 		__tun_detach(tun);
16831da177e4SLinus Torvalds 
16843ad2f3fbSDaniel Mack 		/* If desirable, unregister the netdevice. */
1685d23e4365SHerbert Xu 		if (!(tun->flags & TUN_PERSIST)) {
1686d23e4365SHerbert Xu 			rtnl_lock();
1687d23e4365SHerbert Xu 			if (dev->reg_state == NETREG_REGISTERED)
1688d23e4365SHerbert Xu 				unregister_netdevice(dev);
1689f0a4d0e5SEric W. Biederman 			rtnl_unlock();
1690d23e4365SHerbert Xu 		}
1691d23e4365SHerbert Xu 	}
1692631ab46bSEric W. Biederman 
16939c3fea6aSHerbert Xu 	tun = tfile->tun;
16949c3fea6aSHerbert Xu 	if (tun)
169589f56d1eSMichael S. Tsirkin 		sock_put(tun->socket.sk);
16969c3fea6aSHerbert Xu 
169736b50babSEric W. Biederman 	put_net(tfile->net);
1698631ab46bSEric W. Biederman 	kfree(tfile);
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	return 0;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds 
1703d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
17041da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
17051da177e4SLinus Torvalds 	.llseek = no_llseek,
1706ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1707ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1708ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1709ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
17101da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
1711876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
171250857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
171350857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
171450857e2aSArnd Bergmann #endif
17151da177e4SLinus Torvalds 	.open	= tun_chr_open,
17161da177e4SLinus Torvalds 	.release = tun_chr_close,
17171da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
17181da177e4SLinus Torvalds };
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
17211da177e4SLinus Torvalds 	.minor = TUN_MINOR,
17221da177e4SLinus Torvalds 	.name = "tun",
1723e454cea2SKay Sievers 	.nodename = "net/tun",
17241da177e4SLinus Torvalds 	.fops = &tun_fops,
17251da177e4SLinus Torvalds };
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds /* ethtool interface */
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
17301da177e4SLinus Torvalds {
17311da177e4SLinus Torvalds 	cmd->supported		= 0;
17321da177e4SLinus Torvalds 	cmd->advertising	= 0;
173370739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
17341da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
17351da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
17361da177e4SLinus Torvalds 	cmd->phy_address	= 0;
17371da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
17381da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
17391da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
17401da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
17411da177e4SLinus Torvalds 	return 0;
17421da177e4SLinus Torvalds }
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
17451da177e4SLinus Torvalds {
17461da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17471da177e4SLinus Torvalds 
174833a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
174933a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
17521da177e4SLinus Torvalds 	case TUN_TUN_DEV:
175333a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
17541da177e4SLinus Torvalds 		break;
17551da177e4SLinus Torvalds 	case TUN_TAP_DEV:
175633a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
17571da177e4SLinus Torvalds 		break;
17581da177e4SLinus Torvalds 	}
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
17621da177e4SLinus Torvalds {
17631da177e4SLinus Torvalds #ifdef TUN_DEBUG
17641da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17651da177e4SLinus Torvalds 	return tun->debug;
17661da177e4SLinus Torvalds #else
17671da177e4SLinus Torvalds 	return -EOPNOTSUPP;
17681da177e4SLinus Torvalds #endif
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
17721da177e4SLinus Torvalds {
17731da177e4SLinus Torvalds #ifdef TUN_DEBUG
17741da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17751da177e4SLinus Torvalds 	tun->debug = value;
17761da177e4SLinus Torvalds #endif
17771da177e4SLinus Torvalds }
17781da177e4SLinus Torvalds 
17797282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
17801da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
17811da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
17821da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
17831da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
1784bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
17851da177e4SLinus Torvalds };
17861da177e4SLinus Torvalds 
178779d17604SPavel Emelyanov 
17881da177e4SLinus Torvalds static int __init tun_init(void)
17891da177e4SLinus Torvalds {
17901da177e4SLinus Torvalds 	int ret = 0;
17911da177e4SLinus Torvalds 
17926b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
17936b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
17941da177e4SLinus Torvalds 
1795f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
179679d17604SPavel Emelyanov 	if (ret) {
17976b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
1798f019a7a5SEric W. Biederman 		goto err_linkops;
179979d17604SPavel Emelyanov 	}
180079d17604SPavel Emelyanov 
18011da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
180279d17604SPavel Emelyanov 	if (ret) {
18036b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
180479d17604SPavel Emelyanov 		goto err_misc;
180579d17604SPavel Emelyanov 	}
180679d17604SPavel Emelyanov 	return  0;
180779d17604SPavel Emelyanov err_misc:
1808f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
1809f019a7a5SEric W. Biederman err_linkops:
18101da177e4SLinus Torvalds 	return ret;
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds static void tun_cleanup(void)
18141da177e4SLinus Torvalds {
18151da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
1816f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
181905c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
182005c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
182105c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
182205c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
182305c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
182405c2828cSMichael S. Tsirkin {
182505c2828cSMichael S. Tsirkin 	struct tun_struct *tun;
182605c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
182705c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
182805c2828cSMichael S. Tsirkin 	tun = tun_get(file);
182905c2828cSMichael S. Tsirkin 	if (!tun)
183005c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
183105c2828cSMichael S. Tsirkin 	tun_put(tun);
183205c2828cSMichael S. Tsirkin 	return &tun->socket;
183305c2828cSMichael S. Tsirkin }
183405c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
183505c2828cSMichael S. Tsirkin 
18361da177e4SLinus Torvalds module_init(tun_init);
18371da177e4SLinus Torvalds module_exit(tun_cleanup);
18381da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
18391da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
18401da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18411da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1842578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
1843