xref: /openbmc/linux/drivers/net/tun.c (revision 1e5883382c3287a61aeebc33a301fd50f3a1005b)
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;
1230625c883SEric W. Biederman 	kuid_t			owner;
1240625c883SEric 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;
19038231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
191631ab46bSEric W. Biederman 
192631ab46bSEric W. Biederman 	/* Drop read queue */
19389f56d1eSMichael S. Tsirkin 	skb_queue_purge(&tun->socket.sk->sk_receive_queue);
194c70f1829SEric W. Biederman 
195c70f1829SEric W. Biederman 	/* Drop the extra count on the net device */
196c70f1829SEric W. Biederman 	dev_put(tun->dev);
197c70f1829SEric W. Biederman }
198c70f1829SEric W. Biederman 
199c70f1829SEric W. Biederman static void tun_detach(struct tun_struct *tun)
200c70f1829SEric W. Biederman {
201c70f1829SEric W. Biederman 	rtnl_lock();
202c70f1829SEric W. Biederman 	__tun_detach(tun);
203c70f1829SEric W. Biederman 	rtnl_unlock();
204631ab46bSEric W. Biederman }
205631ab46bSEric W. Biederman 
206631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
207631ab46bSEric W. Biederman {
208c70f1829SEric W. Biederman 	struct tun_struct *tun = NULL;
209c70f1829SEric W. Biederman 
210c70f1829SEric W. Biederman 	if (atomic_inc_not_zero(&tfile->count))
211c70f1829SEric W. Biederman 		tun = tfile->tun;
212c70f1829SEric W. Biederman 
213c70f1829SEric W. Biederman 	return tun;
214631ab46bSEric W. Biederman }
215631ab46bSEric W. Biederman 
216631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
217631ab46bSEric W. Biederman {
218631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
219631ab46bSEric W. Biederman }
220631ab46bSEric W. Biederman 
221631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
222631ab46bSEric W. Biederman {
223c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
224c70f1829SEric W. Biederman 
225c70f1829SEric W. Biederman 	if (atomic_dec_and_test(&tfile->count))
226c70f1829SEric W. Biederman 		tun_detach(tfile->tun);
227631ab46bSEric W. Biederman }
228631ab46bSEric W. Biederman 
2296b8a66eeSJoe Perches /* TAP filtering */
230f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
231f271b2ccSMax Krasnyansky {
232f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
233f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
234f271b2ccSMax Krasnyansky }
235f271b2ccSMax Krasnyansky 
236f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
237f271b2ccSMax Krasnyansky {
238f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
239f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
240f271b2ccSMax Krasnyansky }
241f271b2ccSMax Krasnyansky 
242f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
243f271b2ccSMax Krasnyansky {
244f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
245f271b2ccSMax Krasnyansky 	struct tun_filter uf;
246f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
247f271b2ccSMax Krasnyansky 
248f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
249f271b2ccSMax Krasnyansky 		return -EFAULT;
250f271b2ccSMax Krasnyansky 
251f271b2ccSMax Krasnyansky 	if (!uf.count) {
252f271b2ccSMax Krasnyansky 		/* Disabled */
253f271b2ccSMax Krasnyansky 		filter->count = 0;
254f271b2ccSMax Krasnyansky 		return 0;
255f271b2ccSMax Krasnyansky 	}
256f271b2ccSMax Krasnyansky 
257f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
258f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
259f271b2ccSMax Krasnyansky 	if (!addr)
260f271b2ccSMax Krasnyansky 		return -ENOMEM;
261f271b2ccSMax Krasnyansky 
262f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
263f271b2ccSMax Krasnyansky 		err = -EFAULT;
264f271b2ccSMax Krasnyansky 		goto done;
265f271b2ccSMax Krasnyansky 	}
266f271b2ccSMax Krasnyansky 
267f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
268f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
269f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
270f271b2ccSMax Krasnyansky 	filter->count = 0;
271f271b2ccSMax Krasnyansky 	wmb();
272f271b2ccSMax Krasnyansky 
273f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
274f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
275f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
276f271b2ccSMax Krasnyansky 
277f271b2ccSMax Krasnyansky 	nexact = n;
278f271b2ccSMax Krasnyansky 
279cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
280cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
281f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
282cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
283cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
284cfbf84fcSAlex Williamson 			err = 0; /* no filter */
285cfbf84fcSAlex Williamson 			goto done;
286cfbf84fcSAlex Williamson 		}
287f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
288cfbf84fcSAlex Williamson 	}
289f271b2ccSMax Krasnyansky 
290f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
291f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
292f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
293f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
294f271b2ccSMax Krasnyansky 
295f271b2ccSMax Krasnyansky 	/* Now enable the filter */
296f271b2ccSMax Krasnyansky 	wmb();
297f271b2ccSMax Krasnyansky 	filter->count = nexact;
298f271b2ccSMax Krasnyansky 
299f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
300f271b2ccSMax Krasnyansky 	err = nexact;
301f271b2ccSMax Krasnyansky 
302f271b2ccSMax Krasnyansky done:
303f271b2ccSMax Krasnyansky 	kfree(addr);
304f271b2ccSMax Krasnyansky 	return err;
305f271b2ccSMax Krasnyansky }
306f271b2ccSMax Krasnyansky 
307f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
308f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
309f271b2ccSMax Krasnyansky {
310f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
311f271b2ccSMax Krasnyansky 	 * at this point. */
312f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
313f271b2ccSMax Krasnyansky 	int i;
314f271b2ccSMax Krasnyansky 
315f271b2ccSMax Krasnyansky 	/* Exact match */
316f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
3172e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
318f271b2ccSMax Krasnyansky 			return 1;
319f271b2ccSMax Krasnyansky 
320f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
321f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
322f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
323f271b2ccSMax Krasnyansky 
324f271b2ccSMax Krasnyansky 	return 0;
325f271b2ccSMax Krasnyansky }
326f271b2ccSMax Krasnyansky 
327f271b2ccSMax Krasnyansky /*
328f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
329f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
330f271b2ccSMax Krasnyansky  */
331f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
332f271b2ccSMax Krasnyansky {
333f271b2ccSMax Krasnyansky 	if (!filter->count)
334f271b2ccSMax Krasnyansky 		return 1;
335f271b2ccSMax Krasnyansky 
336f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
337f271b2ccSMax Krasnyansky }
338f271b2ccSMax Krasnyansky 
3391da177e4SLinus Torvalds /* Network device part of the driver */
3401da177e4SLinus Torvalds 
3417282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
3421da177e4SLinus Torvalds 
343c70f1829SEric W. Biederman /* Net device detach from fd. */
344c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
345c70f1829SEric W. Biederman {
346c70f1829SEric W. Biederman 	struct tun_struct *tun = netdev_priv(dev);
347c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
348c70f1829SEric W. Biederman 
349c70f1829SEric W. Biederman 	/* Inform the methods they need to stop using the dev.
350c70f1829SEric W. Biederman 	 */
351c70f1829SEric W. Biederman 	if (tfile) {
35243815482SEric Dumazet 		wake_up_all(&tun->wq.wait);
353c70f1829SEric W. Biederman 		if (atomic_dec_and_test(&tfile->count))
354c70f1829SEric W. Biederman 			__tun_detach(tun);
355c70f1829SEric W. Biederman 	}
356c70f1829SEric W. Biederman }
357c70f1829SEric W. Biederman 
3589c3fea6aSHerbert Xu static void tun_free_netdev(struct net_device *dev)
3599c3fea6aSHerbert Xu {
3609c3fea6aSHerbert Xu 	struct tun_struct *tun = netdev_priv(dev);
3619c3fea6aSHerbert Xu 
362b09e786bSMikulas Patocka 	BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags));
363b09e786bSMikulas Patocka 
3641ab5ecb9SStanislav Kinsbursky 	sk_release_kernel(tun->socket.sk);
3659c3fea6aSHerbert Xu }
3669c3fea6aSHerbert Xu 
3671da177e4SLinus Torvalds /* Net device open. */
3681da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	netif_start_queue(dev);
3711da177e4SLinus Torvalds 	return 0;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /* Net device close. */
3751da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	netif_stop_queue(dev);
3781da177e4SLinus Torvalds 	return 0;
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds /* Net device start xmit */
382424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3851da177e4SLinus Torvalds 
3866b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
389631ab46bSEric W. Biederman 	if (!tun->tfile)
3901da177e4SLinus Torvalds 		goto drop;
3911da177e4SLinus Torvalds 
392f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
393f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
394f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
395f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
396f271b2ccSMax Krasnyansky 		goto drop;
397f271b2ccSMax Krasnyansky 
39899405162SMichael S. Tsirkin 	if (tun->socket.sk->sk_filter &&
39999405162SMichael S. Tsirkin 	    sk_filter(tun->socket.sk, skb))
40099405162SMichael S. Tsirkin 		goto drop;
40199405162SMichael S. Tsirkin 
40289f56d1eSMichael S. Tsirkin 	if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
4031da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
4041da177e4SLinus Torvalds 			/* Normal queueing mode. */
4051da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
4061da177e4SLinus Torvalds 			netif_stop_queue(dev);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
4091da177e4SLinus Torvalds 			 * error is more appropriate. */
41009f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
4111da177e4SLinus Torvalds 		} else {
4121da177e4SLinus Torvalds 			/* Single queue mode.
4131da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
4141da177e4SLinus Torvalds 			goto drop;
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
4180110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
4190110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
420868eefebSMichael S. Tsirkin 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
421868eefebSMichael S. Tsirkin 		goto drop;
4220110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
4230110d6f2SMichael S. Tsirkin 
424f271b2ccSMax Krasnyansky 	/* Enqueue packet */
42589f56d1eSMichael S. Tsirkin 	skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/* Notify and wake up reader process */
4281da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
4291da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
43043815482SEric Dumazet 	wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
43105c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
4326ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds drop:
43509f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
4361da177e4SLinus Torvalds 	kfree_skb(skb);
4376ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
440f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
4411da177e4SLinus Torvalds {
442f271b2ccSMax Krasnyansky 	/*
443f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
444f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
445f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
446f271b2ccSMax Krasnyansky 	 */
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4494885a504SEd Swierk #define MIN_MTU 68
4504885a504SEd Swierk #define MAX_MTU 65535
4514885a504SEd Swierk 
4524885a504SEd Swierk static int
4534885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
4544885a504SEd Swierk {
4554885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
4564885a504SEd Swierk 		return -EINVAL;
4574885a504SEd Swierk 	dev->mtu = new_mtu;
4584885a504SEd Swierk 	return 0;
4594885a504SEd Swierk }
4604885a504SEd Swierk 
461c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
462c8f44affSMichał Mirosław 	netdev_features_t features)
46388255375SMichał Mirosław {
46488255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
46588255375SMichał Mirosław 
46688255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
46788255375SMichał Mirosław }
468bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
469bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
470bebd097aSNeil Horman {
471bebd097aSNeil Horman 	/*
472bebd097aSNeil Horman 	 * Tun only receives frames when:
473bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
474bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
475bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
476bebd097aSNeil Horman 	 * never to have pending data when we poll for it
477bebd097aSNeil Horman 	 * so theres nothing to do here but return.
478bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
479bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
480bebd097aSNeil Horman 	 * still use netconsole
481bebd097aSNeil Horman 	 */
482bebd097aSNeil Horman 	return;
483bebd097aSNeil Horman }
484bebd097aSNeil Horman #endif
485758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
486c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
487758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
488758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
48900829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
490758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
49188255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
492bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
493bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
494bebd097aSNeil Horman #endif
495758e43b7SStephen Hemminger };
496758e43b7SStephen Hemminger 
497758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
498c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
499758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
500758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
50100829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
502758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
50388255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
504afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
505758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
506758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
507bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
508bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
509bebd097aSNeil Horman #endif
510758e43b7SStephen Hemminger };
511758e43b7SStephen Hemminger 
5121da177e4SLinus Torvalds /* Initialize net device. */
5131da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
5181da177e4SLinus Torvalds 	case TUN_TUN_DEV:
519758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
520758e43b7SStephen Hemminger 
5211da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
5221da177e4SLinus Torvalds 		dev->hard_header_len = 0;
5231da177e4SLinus Torvalds 		dev->addr_len = 0;
5241da177e4SLinus Torvalds 		dev->mtu = 1500;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 		/* Zero header length */
5271da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
5281da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
5291da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5301da177e4SLinus Torvalds 		break;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	case TUN_TAP_DEV:
5337a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
5341da177e4SLinus Torvalds 		/* Ethernet TAP Device */
5351da177e4SLinus Torvalds 		ether_setup(dev);
536550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
53736226a8dSBrian Braunstein 
538f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
53936226a8dSBrian Braunstein 
5401da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5411da177e4SLinus Torvalds 		break;
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds /* Character device part */
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /* Poll */
5481da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
5491da177e4SLinus Torvalds {
550b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
551b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
5523c8a9c63SMariusz Kozlowski 	struct sock *sk;
55333dccbb0SHerbert Xu 	unsigned int mask = 0;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	if (!tun)
556eac9e902SEric W. Biederman 		return POLLERR;
5571da177e4SLinus Torvalds 
55889f56d1eSMichael S. Tsirkin 	sk = tun->socket.sk;
5593c8a9c63SMariusz Kozlowski 
5606b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
5611da177e4SLinus Torvalds 
56243815482SEric Dumazet 	poll_wait(file, &tun->wq.wait, wait);
5631da177e4SLinus Torvalds 
56489f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
5651da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5661da177e4SLinus Torvalds 
56733dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
56833dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
56933dccbb0SHerbert Xu 	     sock_writeable(sk)))
57033dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
57133dccbb0SHerbert Xu 
572c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
573c70f1829SEric W. Biederman 		mask = POLLERR;
574c70f1829SEric W. Biederman 
575631ab46bSEric W. Biederman 	tun_put(tun);
5761da177e4SLinus Torvalds 	return mask;
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
579f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
580f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
5816f7c156cSstephen hemminger static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
58233dccbb0SHerbert Xu 				     size_t prepad, size_t len,
58333dccbb0SHerbert Xu 				     size_t linear, int noblock)
584f42157cbSRusty Russell {
58589f56d1eSMichael S. Tsirkin 	struct sock *sk = tun->socket.sk;
586f42157cbSRusty Russell 	struct sk_buff *skb;
58733dccbb0SHerbert Xu 	int err;
588f42157cbSRusty Russell 
589f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
5900eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
59133dccbb0SHerbert Xu 		linear = len;
592f42157cbSRusty Russell 
59333dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
59433dccbb0SHerbert Xu 				   &err);
595f42157cbSRusty Russell 	if (!skb)
59633dccbb0SHerbert Xu 		return ERR_PTR(err);
597f42157cbSRusty Russell 
598f42157cbSRusty Russell 	skb_reserve(skb, prepad);
599f42157cbSRusty Russell 	skb_put(skb, linear);
60033dccbb0SHerbert Xu 	skb->data_len = len - linear;
60133dccbb0SHerbert Xu 	skb->len += len - linear;
602f42157cbSRusty Russell 
603f42157cbSRusty Russell 	return skb;
604f42157cbSRusty Russell }
605f42157cbSRusty Russell 
6060690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */
6070690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
6080690899bSMichael S. Tsirkin 				  int offset, size_t count)
6090690899bSMichael S. Tsirkin {
6100690899bSMichael S. Tsirkin 	int len = iov_length(from, count) - offset;
6110690899bSMichael S. Tsirkin 	int copy = skb_headlen(skb);
6120690899bSMichael S. Tsirkin 	int size, offset1 = 0;
6130690899bSMichael S. Tsirkin 	int i = 0;
6140690899bSMichael S. Tsirkin 
6150690899bSMichael S. Tsirkin 	/* Skip over from offset */
6160690899bSMichael S. Tsirkin 	while (count && (offset >= from->iov_len)) {
6170690899bSMichael S. Tsirkin 		offset -= from->iov_len;
6180690899bSMichael S. Tsirkin 		++from;
6190690899bSMichael S. Tsirkin 		--count;
6200690899bSMichael S. Tsirkin 	}
6210690899bSMichael S. Tsirkin 
6220690899bSMichael S. Tsirkin 	/* copy up to skb headlen */
6230690899bSMichael S. Tsirkin 	while (count && (copy > 0)) {
6240690899bSMichael S. Tsirkin 		size = min_t(unsigned int, copy, from->iov_len - offset);
6250690899bSMichael S. Tsirkin 		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
6260690899bSMichael S. Tsirkin 				   size))
6270690899bSMichael S. Tsirkin 			return -EFAULT;
6280690899bSMichael S. Tsirkin 		if (copy > size) {
6290690899bSMichael S. Tsirkin 			++from;
6300690899bSMichael S. Tsirkin 			--count;
6310690899bSMichael S. Tsirkin 			offset = 0;
6320690899bSMichael S. Tsirkin 		} else
6330690899bSMichael S. Tsirkin 			offset += size;
6340690899bSMichael S. Tsirkin 		copy -= size;
6350690899bSMichael S. Tsirkin 		offset1 += size;
6360690899bSMichael S. Tsirkin 	}
6370690899bSMichael S. Tsirkin 
6380690899bSMichael S. Tsirkin 	if (len == offset1)
6390690899bSMichael S. Tsirkin 		return 0;
6400690899bSMichael S. Tsirkin 
6410690899bSMichael S. Tsirkin 	while (count--) {
6420690899bSMichael S. Tsirkin 		struct page *page[MAX_SKB_FRAGS];
6430690899bSMichael S. Tsirkin 		int num_pages;
6440690899bSMichael S. Tsirkin 		unsigned long base;
6450690899bSMichael S. Tsirkin 		unsigned long truesize;
6460690899bSMichael S. Tsirkin 
6470690899bSMichael S. Tsirkin 		len = from->iov_len - offset;
6480690899bSMichael S. Tsirkin 		if (!len) {
6490690899bSMichael S. Tsirkin 			offset = 0;
6500690899bSMichael S. Tsirkin 			++from;
6510690899bSMichael S. Tsirkin 			continue;
6520690899bSMichael S. Tsirkin 		}
6530690899bSMichael S. Tsirkin 		base = (unsigned long)from->iov_base + offset;
6540690899bSMichael S. Tsirkin 		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
6550690899bSMichael S. Tsirkin 		if (i + size > MAX_SKB_FRAGS)
6560690899bSMichael S. Tsirkin 			return -EMSGSIZE;
6570690899bSMichael S. Tsirkin 		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
6580690899bSMichael S. Tsirkin 		if (num_pages != size) {
6590690899bSMichael S. Tsirkin 			for (i = 0; i < num_pages; i++)
6600690899bSMichael S. Tsirkin 				put_page(page[i]);
6610690899bSMichael S. Tsirkin 			return -EFAULT;
6620690899bSMichael S. Tsirkin 		}
6630690899bSMichael S. Tsirkin 		truesize = size * PAGE_SIZE;
6640690899bSMichael S. Tsirkin 		skb->data_len += len;
6650690899bSMichael S. Tsirkin 		skb->len += len;
6660690899bSMichael S. Tsirkin 		skb->truesize += truesize;
6670690899bSMichael S. Tsirkin 		atomic_add(truesize, &skb->sk->sk_wmem_alloc);
6680690899bSMichael S. Tsirkin 		while (len) {
6690690899bSMichael S. Tsirkin 			int off = base & ~PAGE_MASK;
6700690899bSMichael S. Tsirkin 			int size = min_t(int, len, PAGE_SIZE - off);
6710690899bSMichael S. Tsirkin 			__skb_fill_page_desc(skb, i, page[i], off, size);
6720690899bSMichael S. Tsirkin 			skb_shinfo(skb)->nr_frags++;
6730690899bSMichael S. Tsirkin 			/* increase sk_wmem_alloc */
6740690899bSMichael S. Tsirkin 			base += size;
6750690899bSMichael S. Tsirkin 			len -= size;
6760690899bSMichael S. Tsirkin 			i++;
6770690899bSMichael S. Tsirkin 		}
6780690899bSMichael S. Tsirkin 		offset = 0;
6790690899bSMichael S. Tsirkin 		++from;
6800690899bSMichael S. Tsirkin 	}
6810690899bSMichael S. Tsirkin 	return 0;
6820690899bSMichael S. Tsirkin }
6830690899bSMichael S. Tsirkin 
6841da177e4SLinus Torvalds /* Get packet from user space buffer */
6850690899bSMichael S. Tsirkin static ssize_t tun_get_user(struct tun_struct *tun, void *msg_control,
6860690899bSMichael S. Tsirkin 			    const struct iovec *iv, size_t total_len,
6870690899bSMichael S. Tsirkin 			    size_t count, int noblock)
6881da177e4SLinus Torvalds {
68909640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
6901da177e4SLinus Torvalds 	struct sk_buff *skb;
6910690899bSMichael S. Tsirkin 	size_t len = total_len, align = NET_SKB_PAD;
692f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
6936f26c9a7SMichael S. Tsirkin 	int offset = 0;
6940690899bSMichael S. Tsirkin 	int copylen;
6950690899bSMichael S. Tsirkin 	bool zerocopy = false;
6960690899bSMichael S. Tsirkin 	int err;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
6990690899bSMichael S. Tsirkin 		if ((len -= sizeof(pi)) > total_len)
7001da177e4SLinus Torvalds 			return -EINVAL;
7011da177e4SLinus Torvalds 
7026f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
7031da177e4SLinus Torvalds 			return -EFAULT;
7046f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
7051da177e4SLinus Torvalds 	}
7061da177e4SLinus Torvalds 
707f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
7080690899bSMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) > total_len)
709f43798c2SRusty Russell 			return -EINVAL;
710f43798c2SRusty Russell 
7116f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
712f43798c2SRusty Russell 			return -EFAULT;
713f43798c2SRusty Russell 
7144909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
7154909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
7164909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
7174909122fSHerbert Xu 
718f43798c2SRusty Russell 		if (gso.hdr_len > len)
719f43798c2SRusty Russell 			return -EINVAL;
720d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
721f43798c2SRusty Russell 	}
722f43798c2SRusty Russell 
723e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
724a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
7250eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
7260eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
727e01bf1c8SRusty Russell 			return -EINVAL;
728e01bf1c8SRusty Russell 	}
7291da177e4SLinus Torvalds 
7300690899bSMichael S. Tsirkin 	if (msg_control)
7310690899bSMichael S. Tsirkin 		zerocopy = true;
7320690899bSMichael S. Tsirkin 
7330690899bSMichael S. Tsirkin 	if (zerocopy) {
7340690899bSMichael S. Tsirkin 		/* Userspace may produce vectors with count greater than
7350690899bSMichael S. Tsirkin 		 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
7360690899bSMichael S. Tsirkin 		 * to let the rest of data to be fit in the frags.
7370690899bSMichael S. Tsirkin 		 */
7380690899bSMichael S. Tsirkin 		if (count > MAX_SKB_FRAGS) {
7390690899bSMichael S. Tsirkin 			copylen = iov_length(iv, count - MAX_SKB_FRAGS);
7400690899bSMichael S. Tsirkin 			if (copylen < offset)
7410690899bSMichael S. Tsirkin 				copylen = 0;
7420690899bSMichael S. Tsirkin 			else
7430690899bSMichael S. Tsirkin 				copylen -= offset;
7440690899bSMichael S. Tsirkin 		} else
7450690899bSMichael S. Tsirkin 				copylen = 0;
7460690899bSMichael S. Tsirkin 		/* There are 256 bytes to be copied in skb, so there is enough
7470690899bSMichael S. Tsirkin 		 * room for skb expand head in case it is used.
7480690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
7490690899bSMichael S. Tsirkin 		 */
7500690899bSMichael S. Tsirkin 		if (copylen < gso.hdr_len)
7510690899bSMichael S. Tsirkin 			copylen = gso.hdr_len;
7520690899bSMichael S. Tsirkin 		if (!copylen)
7530690899bSMichael S. Tsirkin 			copylen = GOODCOPY_LEN;
7540690899bSMichael S. Tsirkin 	} else
7550690899bSMichael S. Tsirkin 		copylen = len;
7560690899bSMichael S. Tsirkin 
7570690899bSMichael S. Tsirkin 	skb = tun_alloc_skb(tun, align, copylen, gso.hdr_len, noblock);
75833dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
75933dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
76009f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
76133dccbb0SHerbert Xu 		return PTR_ERR(skb);
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 
7640690899bSMichael S. Tsirkin 	if (zerocopy)
7650690899bSMichael S. Tsirkin 		err = zerocopy_sg_from_iovec(skb, iv, offset, count);
7660690899bSMichael S. Tsirkin 	else
7670690899bSMichael S. Tsirkin 		err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
7680690899bSMichael S. Tsirkin 
7690690899bSMichael S. Tsirkin 	if (err) {
77009f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
7718f22757eSDave Jones 		kfree_skb(skb);
7721da177e4SLinus Torvalds 		return -EFAULT;
7738f22757eSDave Jones 	}
7741da177e4SLinus Torvalds 
775f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
776f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
777f43798c2SRusty Russell 					  gso.csum_offset)) {
778f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
779f43798c2SRusty Russell 			kfree_skb(skb);
780f43798c2SRusty Russell 			return -EINVAL;
781f43798c2SRusty Russell 		}
78288255375SMichał Mirosław 	}
783f43798c2SRusty Russell 
7841da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
7851da177e4SLinus Torvalds 	case TUN_TUN_DEV:
786f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
787f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
788f09f7ee2SAng Way Chuang 			case 0x40:
789f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
790f09f7ee2SAng Way Chuang 				break;
791f09f7ee2SAng Way Chuang 			case 0x60:
792f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
793f09f7ee2SAng Way Chuang 				break;
794f09f7ee2SAng Way Chuang 			default:
795f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
796f09f7ee2SAng Way Chuang 				kfree_skb(skb);
797f09f7ee2SAng Way Chuang 				return -EINVAL;
798f09f7ee2SAng Way Chuang 			}
799f09f7ee2SAng Way Chuang 		}
800f09f7ee2SAng Way Chuang 
801459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
8021da177e4SLinus Torvalds 		skb->protocol = pi.proto;
8034c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
8041da177e4SLinus Torvalds 		break;
8051da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8061da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
8071da177e4SLinus Torvalds 		break;
8086403eab1SJoe Perches 	}
8091da177e4SLinus Torvalds 
810f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
811f43798c2SRusty Russell 		pr_debug("GSO!\n");
812f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
813f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
814f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
815f43798c2SRusty Russell 			break;
816f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
817f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
818f43798c2SRusty Russell 			break;
819e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
820e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
821e36aa25aSSridhar Samudrala 			break;
822f43798c2SRusty Russell 		default:
823f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
824f43798c2SRusty Russell 			kfree_skb(skb);
825f43798c2SRusty Russell 			return -EINVAL;
826f43798c2SRusty Russell 		}
827f43798c2SRusty Russell 
828f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
829f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
830f43798c2SRusty Russell 
831f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
832f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
833f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
834f43798c2SRusty Russell 			kfree_skb(skb);
835f43798c2SRusty Russell 			return -EINVAL;
836f43798c2SRusty Russell 		}
837f43798c2SRusty Russell 
838f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
839f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
840f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
841f43798c2SRusty Russell 	}
8421da177e4SLinus Torvalds 
8430690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
8440690899bSMichael S. Tsirkin 	if (zerocopy) {
8450690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
8460690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
8470690899bSMichael S. Tsirkin 	}
8480690899bSMichael S. Tsirkin 
8491da177e4SLinus Torvalds 	netif_rx_ni(skb);
8501da177e4SLinus Torvalds 
85109f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
85209f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
8531da177e4SLinus Torvalds 
8540690899bSMichael S. Tsirkin 	return total_len;
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
857ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
858ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
8591da177e4SLinus Torvalds {
86033dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
861ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
862631ab46bSEric W. Biederman 	ssize_t result;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	if (!tun)
8651da177e4SLinus Torvalds 		return -EBADFD;
8661da177e4SLinus Torvalds 
8676b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
8681da177e4SLinus Torvalds 
8690690899bSMichael S. Tsirkin 	result = tun_get_user(tun, NULL, iv, iov_length(iv, count), count,
87033dccbb0SHerbert Xu 			      file->f_flags & O_NONBLOCK);
871631ab46bSEric W. Biederman 
872631ab46bSEric W. Biederman 	tun_put(tun);
873631ab46bSEric W. Biederman 	return result;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds /* Put packet to the user space buffer */
8776f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
8781da177e4SLinus Torvalds 			    struct sk_buff *skb,
87943b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
8821da177e4SLinus Torvalds 	ssize_t total = 0;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
8851da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
8861da177e4SLinus Torvalds 			return -EINVAL;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 		if (len < skb->len) {
8891da177e4SLinus Torvalds 			/* Packet will be striped */
8901da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
8911da177e4SLinus Torvalds 		}
8921da177e4SLinus Torvalds 
89343b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
8941da177e4SLinus Torvalds 			return -EFAULT;
8951da177e4SLinus Torvalds 		total += sizeof(pi);
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 
898f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
899f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
900d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
901f43798c2SRusty Russell 			return -EINVAL;
902f43798c2SRusty Russell 
903f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
904f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
905f43798c2SRusty Russell 
906f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
907f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
908f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
909f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
910f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
911f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
912f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
913e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
914e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
915ef3db4a5SMichael S. Tsirkin 			else {
9166b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
917ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
918ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
919ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
920ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
921ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
922ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
923ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
924ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
925ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
926ef3db4a5SMichael S. Tsirkin 			}
927f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
928f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
929f43798c2SRusty Russell 		} else
930f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
931f43798c2SRusty Russell 
932f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
933f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
93455508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
935f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
93610a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
93710a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
938f43798c2SRusty Russell 		} /* else everything is zero */
939f43798c2SRusty Russell 
94043b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
94143b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
942f43798c2SRusty Russell 			return -EFAULT;
943d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
944f43798c2SRusty Russell 	}
945f43798c2SRusty Russell 
9461da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
9471da177e4SLinus Torvalds 
94843b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
94905c2828cSMichael S. Tsirkin 	total += skb->len;
9501da177e4SLinus Torvalds 
95109f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
95209f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	return total;
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds 
95705c2828cSMichael S. Tsirkin static ssize_t tun_do_read(struct tun_struct *tun,
95805c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
95905c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
9621da177e4SLinus Torvalds 	struct sk_buff *skb;
96305c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
9641da177e4SLinus Torvalds 
9656b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_read\n");
9661da177e4SLinus Torvalds 
96761a5ff15SAmos Kong 	if (unlikely(!noblock))
96843815482SEric Dumazet 		add_wait_queue(&tun->wq.wait, &wait);
9691da177e4SLinus Torvalds 	while (len) {
9701da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 		/* Read frames from the queue */
97389f56d1eSMichael S. Tsirkin 		if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
97405c2828cSMichael S. Tsirkin 			if (noblock) {
9751da177e4SLinus Torvalds 				ret = -EAGAIN;
9761da177e4SLinus Torvalds 				break;
9771da177e4SLinus Torvalds 			}
9781da177e4SLinus Torvalds 			if (signal_pending(current)) {
9791da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
9801da177e4SLinus Torvalds 				break;
9811da177e4SLinus Torvalds 			}
982c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
983c70f1829SEric W. Biederman 				ret = -EIO;
984c70f1829SEric W. Biederman 				break;
985c70f1829SEric W. Biederman 			}
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
9881da177e4SLinus Torvalds 			schedule();
9891da177e4SLinus Torvalds 			continue;
9901da177e4SLinus Torvalds 		}
9911da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
9921da177e4SLinus Torvalds 
99343b39dcdSMichael S. Tsirkin 		ret = tun_put_user(tun, skb, iv, len);
9941da177e4SLinus Torvalds 		kfree_skb(skb);
9951da177e4SLinus Torvalds 		break;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
99961a5ff15SAmos Kong 	if (unlikely(!noblock))
100043815482SEric Dumazet 		remove_wait_queue(&tun->wq.wait, &wait);
10011da177e4SLinus Torvalds 
100205c2828cSMichael S. Tsirkin 	return ret;
100305c2828cSMichael S. Tsirkin }
100405c2828cSMichael S. Tsirkin 
100505c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
100605c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
100705c2828cSMichael S. Tsirkin {
100805c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
100905c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
101005c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
101105c2828cSMichael S. Tsirkin 	ssize_t len, ret;
101205c2828cSMichael S. Tsirkin 
101305c2828cSMichael S. Tsirkin 	if (!tun)
101405c2828cSMichael S. Tsirkin 		return -EBADFD;
101505c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
101605c2828cSMichael S. Tsirkin 	if (len < 0) {
101705c2828cSMichael S. Tsirkin 		ret = -EINVAL;
101805c2828cSMichael S. Tsirkin 		goto out;
101905c2828cSMichael S. Tsirkin 	}
102005c2828cSMichael S. Tsirkin 
102105c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
102205c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
1023631ab46bSEric W. Biederman out:
1024631ab46bSEric W. Biederman 	tun_put(tun);
10251da177e4SLinus Torvalds 	return ret;
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
10311da177e4SLinus Torvalds 
10320625c883SEric W. Biederman 	tun->owner = INVALID_UID;
10330625c883SEric W. Biederman 	tun->group = INVALID_GID;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
10369c3fea6aSHerbert Xu 	dev->destructor = tun_free_netdev;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
1039f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1040f019a7a5SEric W. Biederman  * device with netlink.
1041f019a7a5SEric W. Biederman  */
1042f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1043f019a7a5SEric W. Biederman {
1044f019a7a5SEric W. Biederman 	return -EINVAL;
1045f019a7a5SEric W. Biederman }
1046f019a7a5SEric W. Biederman 
1047f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1048f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1049f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1050f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1051f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1052f019a7a5SEric W. Biederman };
1053f019a7a5SEric W. Biederman 
105433dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
105533dccbb0SHerbert Xu {
105633dccbb0SHerbert Xu 	struct tun_struct *tun;
105743815482SEric Dumazet 	wait_queue_head_t *wqueue;
105833dccbb0SHerbert Xu 
105933dccbb0SHerbert Xu 	if (!sock_writeable(sk))
106033dccbb0SHerbert Xu 		return;
106133dccbb0SHerbert Xu 
106233dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
106333dccbb0SHerbert Xu 		return;
106433dccbb0SHerbert Xu 
106543815482SEric Dumazet 	wqueue = sk_sleep(sk);
106643815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
106743815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
106805c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1069c722c625SHerbert Xu 
107080924e5fSVitaliy Gusev 	tun = tun_sk(sk)->tun;
107133dccbb0SHerbert Xu 	kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
107233dccbb0SHerbert Xu }
107333dccbb0SHerbert Xu 
107433dccbb0SHerbert Xu static void tun_sock_destruct(struct sock *sk)
107533dccbb0SHerbert Xu {
107680924e5fSVitaliy Gusev 	free_netdev(tun_sk(sk)->tun->dev);
107733dccbb0SHerbert Xu }
107833dccbb0SHerbert Xu 
107905c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
108005c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
108105c2828cSMichael S. Tsirkin {
108205c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
10830690899bSMichael S. Tsirkin 	return tun_get_user(tun, m->msg_control, m->msg_iov, total_len,
10840690899bSMichael S. Tsirkin 			    m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
108505c2828cSMichael S. Tsirkin }
108605c2828cSMichael S. Tsirkin 
108705c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
108805c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
108905c2828cSMichael S. Tsirkin 		       int flags)
109005c2828cSMichael S. Tsirkin {
109105c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
109205c2828cSMichael S. Tsirkin 	int ret;
109305c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
109405c2828cSMichael S. Tsirkin 		return -EINVAL;
109505c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
109605c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
109705c2828cSMichael S. Tsirkin 	if (ret > total_len) {
109805c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
109905c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
110005c2828cSMichael S. Tsirkin 	}
110105c2828cSMichael S. Tsirkin 	return ret;
110205c2828cSMichael S. Tsirkin }
110305c2828cSMichael S. Tsirkin 
11041ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
11051ab5ecb9SStanislav Kinsbursky {
11061ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
11071ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
11081ab5ecb9SStanislav Kinsbursky 	return 0;
11091ab5ecb9SStanislav Kinsbursky }
11101ab5ecb9SStanislav Kinsbursky 
111105c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
111205c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
111305c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
111405c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
11151ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
111605c2828cSMichael S. Tsirkin };
111705c2828cSMichael S. Tsirkin 
111833dccbb0SHerbert Xu static struct proto tun_proto = {
111933dccbb0SHerbert Xu 	.name		= "tun",
112033dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
112133dccbb0SHerbert Xu 	.obj_size	= sizeof(struct tun_sock),
112233dccbb0SHerbert Xu };
1123f019a7a5SEric W. Biederman 
1124980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1125980c9e8cSDavid Woodhouse {
1126980c9e8cSDavid Woodhouse 	int flags = 0;
1127980c9e8cSDavid Woodhouse 
1128980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1129980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1130980c9e8cSDavid Woodhouse 	else
1131980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1132980c9e8cSDavid Woodhouse 
1133980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1134980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1135980c9e8cSDavid Woodhouse 
1136980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1137980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1138980c9e8cSDavid Woodhouse 
1139980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1140980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1141980c9e8cSDavid Woodhouse 
1142980c9e8cSDavid Woodhouse 	return flags;
1143980c9e8cSDavid Woodhouse }
1144980c9e8cSDavid Woodhouse 
1145980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1146980c9e8cSDavid Woodhouse 			      char *buf)
1147980c9e8cSDavid Woodhouse {
1148980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1149980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1150980c9e8cSDavid Woodhouse }
1151980c9e8cSDavid Woodhouse 
1152980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1153980c9e8cSDavid Woodhouse 			      char *buf)
1154980c9e8cSDavid Woodhouse {
1155980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
11560625c883SEric W. Biederman 	return uid_valid(tun->owner)?
11570625c883SEric W. Biederman 		sprintf(buf, "%u\n",
11580625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
11590625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1160980c9e8cSDavid Woodhouse }
1161980c9e8cSDavid Woodhouse 
1162980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1163980c9e8cSDavid Woodhouse 			      char *buf)
1164980c9e8cSDavid Woodhouse {
1165980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
11660625c883SEric W. Biederman 	return gid_valid(tun->group) ?
11670625c883SEric W. Biederman 		sprintf(buf, "%u\n",
11680625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
11690625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1170980c9e8cSDavid Woodhouse }
1171980c9e8cSDavid Woodhouse 
1172980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1173980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1174980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1175980c9e8cSDavid Woodhouse 
1176d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
11771da177e4SLinus Torvalds {
117833dccbb0SHerbert Xu 	struct sock *sk;
11791da177e4SLinus Torvalds 	struct tun_struct *tun;
11801da177e4SLinus Torvalds 	struct net_device *dev;
11811da177e4SLinus Torvalds 	int err;
11821da177e4SLinus Torvalds 
118374a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
118474a3e5a7SEric W. Biederman 	if (dev) {
11852b980dbdSPaul Moore 		const struct cred *cred = current_cred();
11862b980dbdSPaul Moore 
1187f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1188f85ba780SDavid Woodhouse 			return -EBUSY;
118974a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
119074a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
119174a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
119274a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
119374a3e5a7SEric W. Biederman 		else
119474a3e5a7SEric W. Biederman 			return -EINVAL;
119574a3e5a7SEric W. Biederman 
11960625c883SEric W. Biederman 		if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
11970625c883SEric W. Biederman 		     (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
11982b980dbdSPaul Moore 		    !capable(CAP_NET_ADMIN))
11992b980dbdSPaul Moore 			return -EPERM;
1200d7e9660aSLinus Torvalds 		err = security_tun_dev_attach(tun->socket.sk);
12012b980dbdSPaul Moore 		if (err < 0)
12022b980dbdSPaul Moore 			return err;
12032b980dbdSPaul Moore 
1204a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1205a7385ba2SEric W. Biederman 		if (err < 0)
1206a7385ba2SEric W. Biederman 			return err;
120786a264abSDavid Howells 	}
12081da177e4SLinus Torvalds 	else {
12091da177e4SLinus Torvalds 		char *name;
12101da177e4SLinus Torvalds 		unsigned long flags = 0;
12111da177e4SLinus Torvalds 
1212ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
1213ca6bb5d7SDavid Woodhouse 			return -EPERM;
12142b980dbdSPaul Moore 		err = security_tun_dev_create();
12152b980dbdSPaul Moore 		if (err < 0)
12162b980dbdSPaul Moore 			return err;
1217ca6bb5d7SDavid Woodhouse 
12181da177e4SLinus Torvalds 		/* Set dev type */
12191da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
12201da177e4SLinus Torvalds 			/* TUN device */
12211da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
12221da177e4SLinus Torvalds 			name = "tun%d";
12231da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
12241da177e4SLinus Torvalds 			/* TAP device */
12251da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
12261da177e4SLinus Torvalds 			name = "tap%d";
12271da177e4SLinus Torvalds 		} else
122836989b90SKusanagi Kouichi 			return -EINVAL;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 		if (*ifr->ifr_name)
12311da177e4SLinus Torvalds 			name = ifr->ifr_name;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
12341da177e4SLinus Torvalds 				   tun_setup);
12351da177e4SLinus Torvalds 		if (!dev)
12361da177e4SLinus Torvalds 			return -ENOMEM;
12371da177e4SLinus Torvalds 
1238fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1239f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1240758e43b7SStephen Hemminger 
12411da177e4SLinus Torvalds 		tun = netdev_priv(dev);
12421da177e4SLinus Torvalds 		tun->dev = dev;
12431da177e4SLinus Torvalds 		tun->flags = flags;
1244f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1245d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1246b09e786bSMikulas Patocka 		set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);
12471da177e4SLinus Torvalds 
124833dccbb0SHerbert Xu 		err = -ENOMEM;
12491ab5ecb9SStanislav Kinsbursky 		sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
125033dccbb0SHerbert Xu 		if (!sk)
125133dccbb0SHerbert Xu 			goto err_free_dev;
125233dccbb0SHerbert Xu 
12531ab5ecb9SStanislav Kinsbursky 		sk_change_net(sk, net);
125443815482SEric Dumazet 		tun->socket.wq = &tun->wq;
125543815482SEric Dumazet 		init_waitqueue_head(&tun->wq.wait);
125605c2828cSMichael S. Tsirkin 		tun->socket.ops = &tun_socket_ops;
125733dccbb0SHerbert Xu 		sock_init_data(&tun->socket, sk);
125833dccbb0SHerbert Xu 		sk->sk_write_space = tun_sock_write_space;
125933dccbb0SHerbert Xu 		sk->sk_sndbuf = INT_MAX;
12600690899bSMichael S. Tsirkin 		sock_set_flag(sk, SOCK_ZEROCOPY);
126133dccbb0SHerbert Xu 
126280924e5fSVitaliy Gusev 		tun_sk(sk)->tun = tun;
126333dccbb0SHerbert Xu 
12642b980dbdSPaul Moore 		security_tun_dev_post_create(sk);
12652b980dbdSPaul Moore 
12661da177e4SLinus Torvalds 		tun_net_init(dev);
12671da177e4SLinus Torvalds 
126888255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
126988255375SMichał Mirosław 			TUN_USER_FEATURES;
127088255375SMichał Mirosław 		dev->features = dev->hw_features;
127188255375SMichał Mirosław 
12721da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
12731da177e4SLinus Torvalds 		if (err < 0)
12749c3fea6aSHerbert Xu 			goto err_free_sk;
12759c3fea6aSHerbert Xu 
1276980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1277980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1278980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
12796b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1280980c9e8cSDavid Woodhouse 
12819c3fea6aSHerbert Xu 		sk->sk_destruct = tun_sock_destruct;
1282a7385ba2SEric W. Biederman 
1283a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1284a7385ba2SEric W. Biederman 		if (err < 0)
12859c3fea6aSHerbert Xu 			goto failed;
12861da177e4SLinus Torvalds 	}
12871da177e4SLinus Torvalds 
12886b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
12911da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1292a26af1e0SNathaniel Filardo 	else
1293a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
12961da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1297a26af1e0SNathaniel Filardo 	else
1298a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
12991da177e4SLinus Torvalds 
1300f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1301f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1302f43798c2SRusty Russell 	else
1303f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1304f43798c2SRusty Russell 
1305e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1306e35259a9SMax Krasnyansky 	 * xoff state.
1307e35259a9SMax Krasnyansky 	 */
1308e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1309e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
1310e35259a9SMax Krasnyansky 
13111da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
13121da177e4SLinus Torvalds 	return 0;
13131da177e4SLinus Torvalds 
131433dccbb0SHerbert Xu  err_free_sk:
13151ab5ecb9SStanislav Kinsbursky 	tun_free_netdev(dev);
13161da177e4SLinus Torvalds  err_free_dev:
13171da177e4SLinus Torvalds 	free_netdev(dev);
13181da177e4SLinus Torvalds  failed:
13191da177e4SLinus Torvalds 	return err;
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
1322876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun,
1323876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1324e3b99556SMark McLoughlin {
13256b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1326e3b99556SMark McLoughlin 
1327e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1328e3b99556SMark McLoughlin 
1329980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1330e3b99556SMark McLoughlin 
1331e3b99556SMark McLoughlin 	return 0;
1332e3b99556SMark McLoughlin }
1333e3b99556SMark McLoughlin 
13345228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
13355228ddc9SRusty Russell  * privs required. */
133688255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
13375228ddc9SRusty Russell {
1338c8f44affSMichał Mirosław 	netdev_features_t features = 0;
13395228ddc9SRusty Russell 
13405228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
134188255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
13425228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
13435228ddc9SRusty Russell 
13445228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
13455228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
13465228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
13475228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
13485228ddc9SRusty Russell 			}
13495228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
13505228ddc9SRusty Russell 				features |= NETIF_F_TSO;
13515228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
13525228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
13535228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
13545228ddc9SRusty Russell 		}
1355e36aa25aSSridhar Samudrala 
1356e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1357e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1358e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1359e36aa25aSSridhar Samudrala 		}
13605228ddc9SRusty Russell 	}
13615228ddc9SRusty Russell 
13625228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
13635228ddc9SRusty Russell 	 * trying to set them. */
13645228ddc9SRusty Russell 	if (arg)
13655228ddc9SRusty Russell 		return -EINVAL;
13665228ddc9SRusty Russell 
136788255375SMichał Mirosław 	tun->set_features = features;
136888255375SMichał Mirosław 	netdev_update_features(tun->dev);
13695228ddc9SRusty Russell 
13705228ddc9SRusty Russell 	return 0;
13715228ddc9SRusty Russell }
13725228ddc9SRusty Russell 
137350857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
137450857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
13751da177e4SLinus Torvalds {
137636b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1377631ab46bSEric W. Biederman 	struct tun_struct *tun;
13781da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
137999405162SMichael S. Tsirkin 	struct sock_fprog fprog;
13801da177e4SLinus Torvalds 	struct ifreq ifr;
13810625c883SEric W. Biederman 	kuid_t owner;
13820625c883SEric W. Biederman 	kgid_t group;
138333dccbb0SHerbert Xu 	int sndbuf;
1384d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1385f271b2ccSMax Krasnyansky 	int ret;
13861da177e4SLinus Torvalds 
1387a117dacdSMathias Krause 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
138850857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
13891da177e4SLinus Torvalds 			return -EFAULT;
13908bbb1813SDavid S. Miller 	} else {
1391a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
13928bbb1813SDavid S. Miller 	}
1393631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1394631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1395631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1396631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1397631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1398631ab46bSEric W. Biederman 				IFF_VNET_HDR,
1399631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1400631ab46bSEric W. Biederman 	}
1401631ab46bSEric W. Biederman 
1402876bfd4dSHerbert Xu 	rtnl_lock();
1403876bfd4dSHerbert Xu 
140436b50babSEric W. Biederman 	tun = __tun_get(tfile);
14051da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
14061da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
14071da177e4SLinus Torvalds 
1408876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
14091da177e4SLinus Torvalds 
1410876bfd4dSHerbert Xu 		if (ret)
1411876bfd4dSHerbert Xu 			goto unlock;
14121da177e4SLinus Torvalds 
141350857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1414876bfd4dSHerbert Xu 			ret = -EFAULT;
1415876bfd4dSHerbert Xu 		goto unlock;
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds 
1418876bfd4dSHerbert Xu 	ret = -EBADFD;
14191da177e4SLinus Torvalds 	if (!tun)
1420876bfd4dSHerbert Xu 		goto unlock;
14211da177e4SLinus Torvalds 
1422*1e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
14231da177e4SLinus Torvalds 
1424631ab46bSEric W. Biederman 	ret = 0;
14251da177e4SLinus Torvalds 	switch (cmd) {
1426e3b99556SMark McLoughlin 	case TUNGETIFF:
1427876bfd4dSHerbert Xu 		ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1428e3b99556SMark McLoughlin 		if (ret)
1429631ab46bSEric W. Biederman 			break;
1430e3b99556SMark McLoughlin 
143150857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1432631ab46bSEric W. Biederman 			ret = -EFAULT;
1433e3b99556SMark McLoughlin 		break;
1434e3b99556SMark McLoughlin 
14351da177e4SLinus Torvalds 	case TUNSETNOCSUM:
14361da177e4SLinus Torvalds 		/* Disable/Enable checksum */
14371da177e4SLinus Torvalds 
143888255375SMichał Mirosław 		/* [unimplemented] */
143988255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
14406b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
14411da177e4SLinus Torvalds 		break;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	case TUNSETPERSIST:
14441da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
14451da177e4SLinus Torvalds 		if (arg)
14461da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
14471da177e4SLinus Torvalds 		else
14481da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
14491da177e4SLinus Torvalds 
14506b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
14516b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
14521da177e4SLinus Torvalds 		break;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	case TUNSETOWNER:
14551da177e4SLinus Torvalds 		/* Set owner of the device */
14560625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
14570625c883SEric W. Biederman 		if (!uid_valid(owner)) {
14580625c883SEric W. Biederman 			ret = -EINVAL;
14590625c883SEric W. Biederman 			break;
14600625c883SEric W. Biederman 		}
14610625c883SEric W. Biederman 		tun->owner = owner;
1462*1e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
14630625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
14641da177e4SLinus Torvalds 		break;
14651da177e4SLinus Torvalds 
14668c644623SGuido Guenther 	case TUNSETGROUP:
14678c644623SGuido Guenther 		/* Set group of the device */
14680625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
14690625c883SEric W. Biederman 		if (!gid_valid(group)) {
14700625c883SEric W. Biederman 			ret = -EINVAL;
14710625c883SEric W. Biederman 			break;
14720625c883SEric W. Biederman 		}
14730625c883SEric W. Biederman 		tun->group = group;
1474*1e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
14750625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
14768c644623SGuido Guenther 		break;
14778c644623SGuido Guenther 
1478ff4cc3acSMike Kershaw 	case TUNSETLINK:
1479ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1480ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
14816b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
14826b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
148348abfe05SDavid S. Miller 			ret = -EBUSY;
1484ff4cc3acSMike Kershaw 		} else {
1485ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
14866b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
14876b8a66eeSJoe Perches 				  tun->dev->type);
148848abfe05SDavid S. Miller 			ret = 0;
1489ff4cc3acSMike Kershaw 		}
1490631ab46bSEric W. Biederman 		break;
1491ff4cc3acSMike Kershaw 
14921da177e4SLinus Torvalds #ifdef TUN_DEBUG
14931da177e4SLinus Torvalds 	case TUNSETDEBUG:
14941da177e4SLinus Torvalds 		tun->debug = arg;
14951da177e4SLinus Torvalds 		break;
14961da177e4SLinus Torvalds #endif
14975228ddc9SRusty Russell 	case TUNSETOFFLOAD:
149888255375SMichał Mirosław 		ret = set_offload(tun, arg);
1499631ab46bSEric W. Biederman 		break;
15005228ddc9SRusty Russell 
1501f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1502f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1503631ab46bSEric W. Biederman 		ret = -EINVAL;
1504f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1505631ab46bSEric W. Biederman 			break;
1506c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1507631ab46bSEric W. Biederman 		break;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1510b595076aSUwe Kleine-König 		/* Get hw address */
1511f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1512f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
151350857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1514631ab46bSEric W. Biederman 			ret = -EFAULT;
1515631ab46bSEric W. Biederman 		break;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1518f271b2ccSMax Krasnyansky 		/* Set hw address */
15196b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
15206b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
152140102371SKim B. Heino 
152240102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1523631ab46bSEric W. Biederman 		break;
152433dccbb0SHerbert Xu 
152533dccbb0SHerbert Xu 	case TUNGETSNDBUF:
152689f56d1eSMichael S. Tsirkin 		sndbuf = tun->socket.sk->sk_sndbuf;
152733dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
152833dccbb0SHerbert Xu 			ret = -EFAULT;
152933dccbb0SHerbert Xu 		break;
153033dccbb0SHerbert Xu 
153133dccbb0SHerbert Xu 	case TUNSETSNDBUF:
153233dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
153333dccbb0SHerbert Xu 			ret = -EFAULT;
153433dccbb0SHerbert Xu 			break;
153533dccbb0SHerbert Xu 		}
153633dccbb0SHerbert Xu 
153789f56d1eSMichael S. Tsirkin 		tun->socket.sk->sk_sndbuf = sndbuf;
153833dccbb0SHerbert Xu 		break;
153933dccbb0SHerbert Xu 
1540d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
1541d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
1542d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1543d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1544d9d52b51SMichael S. Tsirkin 		break;
1545d9d52b51SMichael S. Tsirkin 
1546d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
1547d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1548d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1549d9d52b51SMichael S. Tsirkin 			break;
1550d9d52b51SMichael S. Tsirkin 		}
1551d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1552d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
1553d9d52b51SMichael S. Tsirkin 			break;
1554d9d52b51SMichael S. Tsirkin 		}
1555d9d52b51SMichael S. Tsirkin 
1556d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
1557d9d52b51SMichael S. Tsirkin 		break;
1558d9d52b51SMichael S. Tsirkin 
155999405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
156099405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
156199405162SMichael S. Tsirkin 		ret = -EINVAL;
156299405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
156399405162SMichael S. Tsirkin 			break;
156499405162SMichael S. Tsirkin 		ret = -EFAULT;
156599405162SMichael S. Tsirkin 		if (copy_from_user(&fprog, argp, sizeof(fprog)))
156699405162SMichael S. Tsirkin 			break;
156799405162SMichael S. Tsirkin 
156899405162SMichael S. Tsirkin 		ret = sk_attach_filter(&fprog, tun->socket.sk);
156999405162SMichael S. Tsirkin 		break;
157099405162SMichael S. Tsirkin 
157199405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
157299405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
157399405162SMichael S. Tsirkin 		ret = -EINVAL;
157499405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
157599405162SMichael S. Tsirkin 			break;
157699405162SMichael S. Tsirkin 		ret = sk_detach_filter(tun->socket.sk);
157799405162SMichael S. Tsirkin 		break;
157899405162SMichael S. Tsirkin 
15791da177e4SLinus Torvalds 	default:
1580631ab46bSEric W. Biederman 		ret = -EINVAL;
1581631ab46bSEric W. Biederman 		break;
1582ee289b64SJoe Perches 	}
15831da177e4SLinus Torvalds 
1584876bfd4dSHerbert Xu unlock:
1585876bfd4dSHerbert Xu 	rtnl_unlock();
1586876bfd4dSHerbert Xu 	if (tun)
1587631ab46bSEric W. Biederman 		tun_put(tun);
1588631ab46bSEric W. Biederman 	return ret;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
159150857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
159250857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
159350857e2aSArnd Bergmann {
159450857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
159550857e2aSArnd Bergmann }
159650857e2aSArnd Bergmann 
159750857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
159850857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
159950857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
160050857e2aSArnd Bergmann {
160150857e2aSArnd Bergmann 	switch (cmd) {
160250857e2aSArnd Bergmann 	case TUNSETIFF:
160350857e2aSArnd Bergmann 	case TUNGETIFF:
160450857e2aSArnd Bergmann 	case TUNSETTXFILTER:
160550857e2aSArnd Bergmann 	case TUNGETSNDBUF:
160650857e2aSArnd Bergmann 	case TUNSETSNDBUF:
160750857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
160850857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
160950857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
161050857e2aSArnd Bergmann 		break;
161150857e2aSArnd Bergmann 	default:
161250857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
161350857e2aSArnd Bergmann 		break;
161450857e2aSArnd Bergmann 	}
161550857e2aSArnd Bergmann 
161650857e2aSArnd Bergmann 	/*
161750857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
161850857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
161950857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
162050857e2aSArnd Bergmann 	 * contents.
162150857e2aSArnd Bergmann 	 */
162250857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
162350857e2aSArnd Bergmann }
162450857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
162550857e2aSArnd Bergmann 
16261da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
16271da177e4SLinus Torvalds {
1628631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
16291da177e4SLinus Torvalds 	int ret;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	if (!tun)
16321da177e4SLinus Torvalds 		return -EBADFD;
16331da177e4SLinus Torvalds 
16346b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
16379d319522SJonathan Corbet 		goto out;
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	if (on) {
1640609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
16411da177e4SLinus Torvalds 		if (ret)
16429d319522SJonathan Corbet 			goto out;
16431da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
16441da177e4SLinus Torvalds 	} else
16451da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
16469d319522SJonathan Corbet 	ret = 0;
16479d319522SJonathan Corbet out:
1648631ab46bSEric W. Biederman 	tun_put(tun);
16499d319522SJonathan Corbet 	return ret;
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
16531da177e4SLinus Torvalds {
1654631ab46bSEric W. Biederman 	struct tun_file *tfile;
1655deed49fbSThomas Gleixner 
16566b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1657631ab46bSEric W. Biederman 
1658631ab46bSEric W. Biederman 	tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1659631ab46bSEric W. Biederman 	if (!tfile)
1660631ab46bSEric W. Biederman 		return -ENOMEM;
1661c70f1829SEric W. Biederman 	atomic_set(&tfile->count, 0);
1662631ab46bSEric W. Biederman 	tfile->tun = NULL;
166336b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1664631ab46bSEric W. Biederman 	file->private_data = tfile;
16651da177e4SLinus Torvalds 	return 0;
16661da177e4SLinus Torvalds }
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
16691da177e4SLinus Torvalds {
1670631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1671f0a4d0e5SEric W. Biederman 	struct tun_struct *tun;
16721da177e4SLinus Torvalds 
1673f0a4d0e5SEric W. Biederman 	tun = __tun_get(tfile);
1674631ab46bSEric W. Biederman 	if (tun) {
1675d23e4365SHerbert Xu 		struct net_device *dev = tun->dev;
1676d23e4365SHerbert Xu 
16776b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "tun_chr_close\n");
16781da177e4SLinus Torvalds 
1679631ab46bSEric W. Biederman 		__tun_detach(tun);
16801da177e4SLinus Torvalds 
16813ad2f3fbSDaniel Mack 		/* If desirable, unregister the netdevice. */
1682d23e4365SHerbert Xu 		if (!(tun->flags & TUN_PERSIST)) {
1683d23e4365SHerbert Xu 			rtnl_lock();
1684d23e4365SHerbert Xu 			if (dev->reg_state == NETREG_REGISTERED)
1685d23e4365SHerbert Xu 				unregister_netdevice(dev);
1686f0a4d0e5SEric W. Biederman 			rtnl_unlock();
1687d23e4365SHerbert Xu 		}
1688d23e4365SHerbert Xu 	}
1689631ab46bSEric W. Biederman 
16909c3fea6aSHerbert Xu 	tun = tfile->tun;
16919c3fea6aSHerbert Xu 	if (tun)
169289f56d1eSMichael S. Tsirkin 		sock_put(tun->socket.sk);
16939c3fea6aSHerbert Xu 
169436b50babSEric W. Biederman 	put_net(tfile->net);
1695631ab46bSEric W. Biederman 	kfree(tfile);
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	return 0;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
1700d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
17011da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
17021da177e4SLinus Torvalds 	.llseek = no_llseek,
1703ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1704ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1705ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1706ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
17071da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
1708876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
170950857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
171050857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
171150857e2aSArnd Bergmann #endif
17121da177e4SLinus Torvalds 	.open	= tun_chr_open,
17131da177e4SLinus Torvalds 	.release = tun_chr_close,
17141da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
17151da177e4SLinus Torvalds };
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
17181da177e4SLinus Torvalds 	.minor = TUN_MINOR,
17191da177e4SLinus Torvalds 	.name = "tun",
1720e454cea2SKay Sievers 	.nodename = "net/tun",
17211da177e4SLinus Torvalds 	.fops = &tun_fops,
17221da177e4SLinus Torvalds };
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds /* ethtool interface */
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
17271da177e4SLinus Torvalds {
17281da177e4SLinus Torvalds 	cmd->supported		= 0;
17291da177e4SLinus Torvalds 	cmd->advertising	= 0;
173070739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
17311da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
17321da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
17331da177e4SLinus Torvalds 	cmd->phy_address	= 0;
17341da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
17351da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
17361da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
17371da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
17381da177e4SLinus Torvalds 	return 0;
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
17421da177e4SLinus Torvalds {
17431da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17441da177e4SLinus Torvalds 
174533a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
174633a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
17491da177e4SLinus Torvalds 	case TUN_TUN_DEV:
175033a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
17511da177e4SLinus Torvalds 		break;
17521da177e4SLinus Torvalds 	case TUN_TAP_DEV:
175333a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
17541da177e4SLinus Torvalds 		break;
17551da177e4SLinus Torvalds 	}
17561da177e4SLinus Torvalds }
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
17591da177e4SLinus Torvalds {
17601da177e4SLinus Torvalds #ifdef TUN_DEBUG
17611da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17621da177e4SLinus Torvalds 	return tun->debug;
17631da177e4SLinus Torvalds #else
17641da177e4SLinus Torvalds 	return -EOPNOTSUPP;
17651da177e4SLinus Torvalds #endif
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
17691da177e4SLinus Torvalds {
17701da177e4SLinus Torvalds #ifdef TUN_DEBUG
17711da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17721da177e4SLinus Torvalds 	tun->debug = value;
17731da177e4SLinus Torvalds #endif
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
17767282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
17771da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
17781da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
17791da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
17801da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
1781bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
17821da177e4SLinus Torvalds };
17831da177e4SLinus Torvalds 
178479d17604SPavel Emelyanov 
17851da177e4SLinus Torvalds static int __init tun_init(void)
17861da177e4SLinus Torvalds {
17871da177e4SLinus Torvalds 	int ret = 0;
17881da177e4SLinus Torvalds 
17896b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
17906b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
17911da177e4SLinus Torvalds 
1792f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
179379d17604SPavel Emelyanov 	if (ret) {
17946b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
1795f019a7a5SEric W. Biederman 		goto err_linkops;
179679d17604SPavel Emelyanov 	}
179779d17604SPavel Emelyanov 
17981da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
179979d17604SPavel Emelyanov 	if (ret) {
18006b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
180179d17604SPavel Emelyanov 		goto err_misc;
180279d17604SPavel Emelyanov 	}
180379d17604SPavel Emelyanov 	return  0;
180479d17604SPavel Emelyanov err_misc:
1805f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
1806f019a7a5SEric W. Biederman err_linkops:
18071da177e4SLinus Torvalds 	return ret;
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds static void tun_cleanup(void)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
1813f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
18141da177e4SLinus Torvalds }
18151da177e4SLinus Torvalds 
181605c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
181705c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
181805c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
181905c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
182005c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
182105c2828cSMichael S. Tsirkin {
182205c2828cSMichael S. Tsirkin 	struct tun_struct *tun;
182305c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
182405c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
182505c2828cSMichael S. Tsirkin 	tun = tun_get(file);
182605c2828cSMichael S. Tsirkin 	if (!tun)
182705c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
182805c2828cSMichael S. Tsirkin 	tun_put(tun);
182905c2828cSMichael S. Tsirkin 	return &tun->socket;
183005c2828cSMichael S. Tsirkin }
183105c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
183205c2828cSMichael S. Tsirkin 
18331da177e4SLinus Torvalds module_init(tun_init);
18341da177e4SLinus Torvalds module_exit(tun_cleanup);
18351da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
18361da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
18371da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18381da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1839578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
1840