xref: /openbmc/linux/drivers/net/tun.c (revision a117dacde0288f3ec60b6e5bcedae8fa37ee0dfc)
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;
12314daa021SRusty Russell 	uid_t			owner;
12414daa021SRusty Russell 	gid_t			group;
12514daa021SRusty Russell 
12614daa021SRusty Russell 	struct net_device	*dev;
127c8f44affSMichał Mirosław 	netdev_features_t	set_features;
12888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
12988255375SMichał Mirosław 			  NETIF_F_TSO6|NETIF_F_UFO)
13014daa021SRusty Russell 	struct fasync_struct	*fasync;
13114daa021SRusty Russell 
132f271b2ccSMax Krasnyansky 	struct tap_filter       txflt;
13333dccbb0SHerbert Xu 	struct socket		socket;
13443815482SEric Dumazet 	struct socket_wq	wq;
135d9d52b51SMichael S. Tsirkin 
136d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
137d9d52b51SMichael S. Tsirkin 
13814daa021SRusty Russell #ifdef TUN_DEBUG
13914daa021SRusty Russell 	int debug;
14014daa021SRusty Russell #endif
14114daa021SRusty Russell };
14214daa021SRusty Russell 
14333dccbb0SHerbert Xu struct tun_sock {
14433dccbb0SHerbert Xu 	struct sock		sk;
14533dccbb0SHerbert Xu 	struct tun_struct	*tun;
14633dccbb0SHerbert Xu };
14733dccbb0SHerbert Xu 
14833dccbb0SHerbert Xu static inline struct tun_sock *tun_sk(struct sock *sk)
14933dccbb0SHerbert Xu {
15033dccbb0SHerbert Xu 	return container_of(sk, struct tun_sock, sk);
15133dccbb0SHerbert Xu }
15233dccbb0SHerbert Xu 
153a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
154a7385ba2SEric W. Biederman {
155631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
15638231b7aSEric W. Biederman 	int err;
157a7385ba2SEric W. Biederman 
158a7385ba2SEric W. Biederman 	ASSERT_RTNL();
159a7385ba2SEric W. Biederman 
16038231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
16138231b7aSEric W. Biederman 
16238231b7aSEric W. Biederman 	err = -EINVAL;
16338231b7aSEric W. Biederman 	if (tfile->tun)
16438231b7aSEric W. Biederman 		goto out;
16538231b7aSEric W. Biederman 
16638231b7aSEric W. Biederman 	err = -EBUSY;
16738231b7aSEric W. Biederman 	if (tun->tfile)
16838231b7aSEric W. Biederman 		goto out;
16938231b7aSEric W. Biederman 
17038231b7aSEric W. Biederman 	err = 0;
171631ab46bSEric W. Biederman 	tfile->tun = tun;
172631ab46bSEric W. Biederman 	tun->tfile = tfile;
17305c2828cSMichael S. Tsirkin 	tun->socket.file = file;
174bee31369SNolan Leake 	netif_carrier_on(tun->dev);
175c70f1829SEric W. Biederman 	dev_hold(tun->dev);
17689f56d1eSMichael S. Tsirkin 	sock_hold(tun->socket.sk);
177c70f1829SEric W. Biederman 	atomic_inc(&tfile->count);
178a7385ba2SEric W. Biederman 
17938231b7aSEric W. Biederman out:
18038231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
18138231b7aSEric W. Biederman 	return err;
182a7385ba2SEric W. Biederman }
183a7385ba2SEric W. Biederman 
184631ab46bSEric W. Biederman static void __tun_detach(struct tun_struct *tun)
185631ab46bSEric W. Biederman {
186631ab46bSEric W. Biederman 	/* Detach from net device */
18738231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
188bee31369SNolan Leake 	netif_carrier_off(tun->dev);
189631ab46bSEric W. Biederman 	tun->tfile = NULL;
19005c2828cSMichael S. Tsirkin 	tun->socket.file = NULL;
19138231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
192631ab46bSEric W. Biederman 
193631ab46bSEric W. Biederman 	/* Drop read queue */
19489f56d1eSMichael S. Tsirkin 	skb_queue_purge(&tun->socket.sk->sk_receive_queue);
195c70f1829SEric W. Biederman 
196c70f1829SEric W. Biederman 	/* Drop the extra count on the net device */
197c70f1829SEric W. Biederman 	dev_put(tun->dev);
198c70f1829SEric W. Biederman }
199c70f1829SEric W. Biederman 
200c70f1829SEric W. Biederman static void tun_detach(struct tun_struct *tun)
201c70f1829SEric W. Biederman {
202c70f1829SEric W. Biederman 	rtnl_lock();
203c70f1829SEric W. Biederman 	__tun_detach(tun);
204c70f1829SEric W. Biederman 	rtnl_unlock();
205631ab46bSEric W. Biederman }
206631ab46bSEric W. Biederman 
207631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
208631ab46bSEric W. Biederman {
209c70f1829SEric W. Biederman 	struct tun_struct *tun = NULL;
210c70f1829SEric W. Biederman 
211c70f1829SEric W. Biederman 	if (atomic_inc_not_zero(&tfile->count))
212c70f1829SEric W. Biederman 		tun = tfile->tun;
213c70f1829SEric W. Biederman 
214c70f1829SEric W. Biederman 	return tun;
215631ab46bSEric W. Biederman }
216631ab46bSEric W. Biederman 
217631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
218631ab46bSEric W. Biederman {
219631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
220631ab46bSEric W. Biederman }
221631ab46bSEric W. Biederman 
222631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
223631ab46bSEric W. Biederman {
224c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
225c70f1829SEric W. Biederman 
226c70f1829SEric W. Biederman 	if (atomic_dec_and_test(&tfile->count))
227c70f1829SEric W. Biederman 		tun_detach(tfile->tun);
228631ab46bSEric W. Biederman }
229631ab46bSEric W. Biederman 
2306b8a66eeSJoe Perches /* TAP filtering */
231f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
232f271b2ccSMax Krasnyansky {
233f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
234f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
235f271b2ccSMax Krasnyansky }
236f271b2ccSMax Krasnyansky 
237f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
238f271b2ccSMax Krasnyansky {
239f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
240f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
241f271b2ccSMax Krasnyansky }
242f271b2ccSMax Krasnyansky 
243f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
244f271b2ccSMax Krasnyansky {
245f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
246f271b2ccSMax Krasnyansky 	struct tun_filter uf;
247f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
248f271b2ccSMax Krasnyansky 
249f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
250f271b2ccSMax Krasnyansky 		return -EFAULT;
251f271b2ccSMax Krasnyansky 
252f271b2ccSMax Krasnyansky 	if (!uf.count) {
253f271b2ccSMax Krasnyansky 		/* Disabled */
254f271b2ccSMax Krasnyansky 		filter->count = 0;
255f271b2ccSMax Krasnyansky 		return 0;
256f271b2ccSMax Krasnyansky 	}
257f271b2ccSMax Krasnyansky 
258f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
259f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
260f271b2ccSMax Krasnyansky 	if (!addr)
261f271b2ccSMax Krasnyansky 		return -ENOMEM;
262f271b2ccSMax Krasnyansky 
263f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
264f271b2ccSMax Krasnyansky 		err = -EFAULT;
265f271b2ccSMax Krasnyansky 		goto done;
266f271b2ccSMax Krasnyansky 	}
267f271b2ccSMax Krasnyansky 
268f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
269f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
270f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
271f271b2ccSMax Krasnyansky 	filter->count = 0;
272f271b2ccSMax Krasnyansky 	wmb();
273f271b2ccSMax Krasnyansky 
274f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
275f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
276f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
277f271b2ccSMax Krasnyansky 
278f271b2ccSMax Krasnyansky 	nexact = n;
279f271b2ccSMax Krasnyansky 
280cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
281cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
282f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
283cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
284cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
285cfbf84fcSAlex Williamson 			err = 0; /* no filter */
286cfbf84fcSAlex Williamson 			goto done;
287cfbf84fcSAlex Williamson 		}
288f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
289cfbf84fcSAlex Williamson 	}
290f271b2ccSMax Krasnyansky 
291f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
292f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
293f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
294f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
295f271b2ccSMax Krasnyansky 
296f271b2ccSMax Krasnyansky 	/* Now enable the filter */
297f271b2ccSMax Krasnyansky 	wmb();
298f271b2ccSMax Krasnyansky 	filter->count = nexact;
299f271b2ccSMax Krasnyansky 
300f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
301f271b2ccSMax Krasnyansky 	err = nexact;
302f271b2ccSMax Krasnyansky 
303f271b2ccSMax Krasnyansky done:
304f271b2ccSMax Krasnyansky 	kfree(addr);
305f271b2ccSMax Krasnyansky 	return err;
306f271b2ccSMax Krasnyansky }
307f271b2ccSMax Krasnyansky 
308f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
309f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
310f271b2ccSMax Krasnyansky {
311f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
312f271b2ccSMax Krasnyansky 	 * at this point. */
313f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
314f271b2ccSMax Krasnyansky 	int i;
315f271b2ccSMax Krasnyansky 
316f271b2ccSMax Krasnyansky 	/* Exact match */
317f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
3182e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
319f271b2ccSMax Krasnyansky 			return 1;
320f271b2ccSMax Krasnyansky 
321f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
322f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
323f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
324f271b2ccSMax Krasnyansky 
325f271b2ccSMax Krasnyansky 	return 0;
326f271b2ccSMax Krasnyansky }
327f271b2ccSMax Krasnyansky 
328f271b2ccSMax Krasnyansky /*
329f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
330f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
331f271b2ccSMax Krasnyansky  */
332f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
333f271b2ccSMax Krasnyansky {
334f271b2ccSMax Krasnyansky 	if (!filter->count)
335f271b2ccSMax Krasnyansky 		return 1;
336f271b2ccSMax Krasnyansky 
337f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
338f271b2ccSMax Krasnyansky }
339f271b2ccSMax Krasnyansky 
3401da177e4SLinus Torvalds /* Network device part of the driver */
3411da177e4SLinus Torvalds 
3427282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
3431da177e4SLinus Torvalds 
344c70f1829SEric W. Biederman /* Net device detach from fd. */
345c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
346c70f1829SEric W. Biederman {
347c70f1829SEric W. Biederman 	struct tun_struct *tun = netdev_priv(dev);
348c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
349c70f1829SEric W. Biederman 
350c70f1829SEric W. Biederman 	/* Inform the methods they need to stop using the dev.
351c70f1829SEric W. Biederman 	 */
352c70f1829SEric W. Biederman 	if (tfile) {
35343815482SEric Dumazet 		wake_up_all(&tun->wq.wait);
354c70f1829SEric W. Biederman 		if (atomic_dec_and_test(&tfile->count))
355c70f1829SEric W. Biederman 			__tun_detach(tun);
356c70f1829SEric W. Biederman 	}
357c70f1829SEric W. Biederman }
358c70f1829SEric W. Biederman 
3599c3fea6aSHerbert Xu static void tun_free_netdev(struct net_device *dev)
3609c3fea6aSHerbert Xu {
3619c3fea6aSHerbert Xu 	struct tun_struct *tun = netdev_priv(dev);
3629c3fea6aSHerbert Xu 
363b09e786bSMikulas Patocka 	BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags));
364b09e786bSMikulas Patocka 
3651ab5ecb9SStanislav Kinsbursky 	sk_release_kernel(tun->socket.sk);
3669c3fea6aSHerbert Xu }
3679c3fea6aSHerbert Xu 
3681da177e4SLinus Torvalds /* Net device open. */
3691da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	netif_start_queue(dev);
3721da177e4SLinus Torvalds 	return 0;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds /* Net device close. */
3761da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	netif_stop_queue(dev);
3791da177e4SLinus Torvalds 	return 0;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds /* Net device start xmit */
383424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3861da177e4SLinus Torvalds 
3876b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
390631ab46bSEric W. Biederman 	if (!tun->tfile)
3911da177e4SLinus Torvalds 		goto drop;
3921da177e4SLinus Torvalds 
393f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
394f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
395f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
396f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
397f271b2ccSMax Krasnyansky 		goto drop;
398f271b2ccSMax Krasnyansky 
39999405162SMichael S. Tsirkin 	if (tun->socket.sk->sk_filter &&
40099405162SMichael S. Tsirkin 	    sk_filter(tun->socket.sk, skb))
40199405162SMichael S. Tsirkin 		goto drop;
40299405162SMichael S. Tsirkin 
40389f56d1eSMichael S. Tsirkin 	if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
4041da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
4051da177e4SLinus Torvalds 			/* Normal queueing mode. */
4061da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
4071da177e4SLinus Torvalds 			netif_stop_queue(dev);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
4101da177e4SLinus Torvalds 			 * error is more appropriate. */
41109f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
4121da177e4SLinus Torvalds 		} else {
4131da177e4SLinus Torvalds 			/* Single queue mode.
4141da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
4151da177e4SLinus Torvalds 			goto drop;
4161da177e4SLinus Torvalds 		}
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 
4190110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
4200110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
421868eefebSMichael S. Tsirkin 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
422868eefebSMichael S. Tsirkin 		goto drop;
4230110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
4240110d6f2SMichael S. Tsirkin 
425f271b2ccSMax Krasnyansky 	/* Enqueue packet */
42689f56d1eSMichael S. Tsirkin 	skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/* Notify and wake up reader process */
4291da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
4301da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
43143815482SEric Dumazet 	wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
43205c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
4336ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds drop:
43609f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
4371da177e4SLinus Torvalds 	kfree_skb(skb);
4386ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
441f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
4421da177e4SLinus Torvalds {
443f271b2ccSMax Krasnyansky 	/*
444f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
445f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
446f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
447f271b2ccSMax Krasnyansky 	 */
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4504885a504SEd Swierk #define MIN_MTU 68
4514885a504SEd Swierk #define MAX_MTU 65535
4524885a504SEd Swierk 
4534885a504SEd Swierk static int
4544885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
4554885a504SEd Swierk {
4564885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
4574885a504SEd Swierk 		return -EINVAL;
4584885a504SEd Swierk 	dev->mtu = new_mtu;
4594885a504SEd Swierk 	return 0;
4604885a504SEd Swierk }
4614885a504SEd Swierk 
462c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
463c8f44affSMichał Mirosław 	netdev_features_t features)
46488255375SMichał Mirosław {
46588255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
46688255375SMichał Mirosław 
46788255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
46888255375SMichał Mirosław }
469bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
470bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
471bebd097aSNeil Horman {
472bebd097aSNeil Horman 	/*
473bebd097aSNeil Horman 	 * Tun only receives frames when:
474bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
475bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
476bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
477bebd097aSNeil Horman 	 * never to have pending data when we poll for it
478bebd097aSNeil Horman 	 * so theres nothing to do here but return.
479bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
480bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
481bebd097aSNeil Horman 	 * still use netconsole
482bebd097aSNeil Horman 	 */
483bebd097aSNeil Horman 	return;
484bebd097aSNeil Horman }
485bebd097aSNeil Horman #endif
486758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
487c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
488758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
489758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
49000829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
491758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
49288255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
493bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
494bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
495bebd097aSNeil Horman #endif
496758e43b7SStephen Hemminger };
497758e43b7SStephen Hemminger 
498758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
499c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
500758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
501758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
50200829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
503758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
50488255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
505afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
506758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
507758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
508bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
509bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
510bebd097aSNeil Horman #endif
511758e43b7SStephen Hemminger };
512758e43b7SStephen Hemminger 
5131da177e4SLinus Torvalds /* Initialize net device. */
5141da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
5191da177e4SLinus Torvalds 	case TUN_TUN_DEV:
520758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
521758e43b7SStephen Hemminger 
5221da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
5231da177e4SLinus Torvalds 		dev->hard_header_len = 0;
5241da177e4SLinus Torvalds 		dev->addr_len = 0;
5251da177e4SLinus Torvalds 		dev->mtu = 1500;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		/* Zero header length */
5281da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
5291da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
5301da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5311da177e4SLinus Torvalds 		break;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	case TUN_TAP_DEV:
5347a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
5351da177e4SLinus Torvalds 		/* Ethernet TAP Device */
5361da177e4SLinus Torvalds 		ether_setup(dev);
537550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
53836226a8dSBrian Braunstein 
539f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
54036226a8dSBrian Braunstein 
5411da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5421da177e4SLinus Torvalds 		break;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds /* Character device part */
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds /* Poll */
5491da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
5501da177e4SLinus Torvalds {
551b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
552b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
5533c8a9c63SMariusz Kozlowski 	struct sock *sk;
55433dccbb0SHerbert Xu 	unsigned int mask = 0;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (!tun)
557eac9e902SEric W. Biederman 		return POLLERR;
5581da177e4SLinus Torvalds 
55989f56d1eSMichael S. Tsirkin 	sk = tun->socket.sk;
5603c8a9c63SMariusz Kozlowski 
5616b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
5621da177e4SLinus Torvalds 
56343815482SEric Dumazet 	poll_wait(file, &tun->wq.wait, wait);
5641da177e4SLinus Torvalds 
56589f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
5661da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5671da177e4SLinus Torvalds 
56833dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
56933dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
57033dccbb0SHerbert Xu 	     sock_writeable(sk)))
57133dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
57233dccbb0SHerbert Xu 
573c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
574c70f1829SEric W. Biederman 		mask = POLLERR;
575c70f1829SEric W. Biederman 
576631ab46bSEric W. Biederman 	tun_put(tun);
5771da177e4SLinus Torvalds 	return mask;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
580f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
581f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
5826f7c156cSstephen hemminger static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
58333dccbb0SHerbert Xu 				     size_t prepad, size_t len,
58433dccbb0SHerbert Xu 				     size_t linear, int noblock)
585f42157cbSRusty Russell {
58689f56d1eSMichael S. Tsirkin 	struct sock *sk = tun->socket.sk;
587f42157cbSRusty Russell 	struct sk_buff *skb;
58833dccbb0SHerbert Xu 	int err;
589f42157cbSRusty Russell 
59082862742SHerbert Xu 	sock_update_classid(sk);
59182862742SHerbert Xu 
592f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
5930eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
59433dccbb0SHerbert Xu 		linear = len;
595f42157cbSRusty Russell 
59633dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
59733dccbb0SHerbert Xu 				   &err);
598f42157cbSRusty Russell 	if (!skb)
59933dccbb0SHerbert Xu 		return ERR_PTR(err);
600f42157cbSRusty Russell 
601f42157cbSRusty Russell 	skb_reserve(skb, prepad);
602f42157cbSRusty Russell 	skb_put(skb, linear);
60333dccbb0SHerbert Xu 	skb->data_len = len - linear;
60433dccbb0SHerbert Xu 	skb->len += len - linear;
605f42157cbSRusty Russell 
606f42157cbSRusty Russell 	return skb;
607f42157cbSRusty Russell }
608f42157cbSRusty Russell 
6090690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */
6100690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
6110690899bSMichael S. Tsirkin 				  int offset, size_t count)
6120690899bSMichael S. Tsirkin {
6130690899bSMichael S. Tsirkin 	int len = iov_length(from, count) - offset;
6140690899bSMichael S. Tsirkin 	int copy = skb_headlen(skb);
6150690899bSMichael S. Tsirkin 	int size, offset1 = 0;
6160690899bSMichael S. Tsirkin 	int i = 0;
6170690899bSMichael S. Tsirkin 
6180690899bSMichael S. Tsirkin 	/* Skip over from offset */
6190690899bSMichael S. Tsirkin 	while (count && (offset >= from->iov_len)) {
6200690899bSMichael S. Tsirkin 		offset -= from->iov_len;
6210690899bSMichael S. Tsirkin 		++from;
6220690899bSMichael S. Tsirkin 		--count;
6230690899bSMichael S. Tsirkin 	}
6240690899bSMichael S. Tsirkin 
6250690899bSMichael S. Tsirkin 	/* copy up to skb headlen */
6260690899bSMichael S. Tsirkin 	while (count && (copy > 0)) {
6270690899bSMichael S. Tsirkin 		size = min_t(unsigned int, copy, from->iov_len - offset);
6280690899bSMichael S. Tsirkin 		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
6290690899bSMichael S. Tsirkin 				   size))
6300690899bSMichael S. Tsirkin 			return -EFAULT;
6310690899bSMichael S. Tsirkin 		if (copy > size) {
6320690899bSMichael S. Tsirkin 			++from;
6330690899bSMichael S. Tsirkin 			--count;
6340690899bSMichael S. Tsirkin 			offset = 0;
6350690899bSMichael S. Tsirkin 		} else
6360690899bSMichael S. Tsirkin 			offset += size;
6370690899bSMichael S. Tsirkin 		copy -= size;
6380690899bSMichael S. Tsirkin 		offset1 += size;
6390690899bSMichael S. Tsirkin 	}
6400690899bSMichael S. Tsirkin 
6410690899bSMichael S. Tsirkin 	if (len == offset1)
6420690899bSMichael S. Tsirkin 		return 0;
6430690899bSMichael S. Tsirkin 
6440690899bSMichael S. Tsirkin 	while (count--) {
6450690899bSMichael S. Tsirkin 		struct page *page[MAX_SKB_FRAGS];
6460690899bSMichael S. Tsirkin 		int num_pages;
6470690899bSMichael S. Tsirkin 		unsigned long base;
6480690899bSMichael S. Tsirkin 		unsigned long truesize;
6490690899bSMichael S. Tsirkin 
6500690899bSMichael S. Tsirkin 		len = from->iov_len - offset;
6510690899bSMichael S. Tsirkin 		if (!len) {
6520690899bSMichael S. Tsirkin 			offset = 0;
6530690899bSMichael S. Tsirkin 			++from;
6540690899bSMichael S. Tsirkin 			continue;
6550690899bSMichael S. Tsirkin 		}
6560690899bSMichael S. Tsirkin 		base = (unsigned long)from->iov_base + offset;
6570690899bSMichael S. Tsirkin 		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
6580690899bSMichael S. Tsirkin 		if (i + size > MAX_SKB_FRAGS)
6590690899bSMichael S. Tsirkin 			return -EMSGSIZE;
6600690899bSMichael S. Tsirkin 		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
6610690899bSMichael S. Tsirkin 		if (num_pages != size) {
6620690899bSMichael S. Tsirkin 			for (i = 0; i < num_pages; i++)
6630690899bSMichael S. Tsirkin 				put_page(page[i]);
6640690899bSMichael S. Tsirkin 			return -EFAULT;
6650690899bSMichael S. Tsirkin 		}
6660690899bSMichael S. Tsirkin 		truesize = size * PAGE_SIZE;
6670690899bSMichael S. Tsirkin 		skb->data_len += len;
6680690899bSMichael S. Tsirkin 		skb->len += len;
6690690899bSMichael S. Tsirkin 		skb->truesize += truesize;
6700690899bSMichael S. Tsirkin 		atomic_add(truesize, &skb->sk->sk_wmem_alloc);
6710690899bSMichael S. Tsirkin 		while (len) {
6720690899bSMichael S. Tsirkin 			int off = base & ~PAGE_MASK;
6730690899bSMichael S. Tsirkin 			int size = min_t(int, len, PAGE_SIZE - off);
6740690899bSMichael S. Tsirkin 			__skb_fill_page_desc(skb, i, page[i], off, size);
6750690899bSMichael S. Tsirkin 			skb_shinfo(skb)->nr_frags++;
6760690899bSMichael S. Tsirkin 			/* increase sk_wmem_alloc */
6770690899bSMichael S. Tsirkin 			base += size;
6780690899bSMichael S. Tsirkin 			len -= size;
6790690899bSMichael S. Tsirkin 			i++;
6800690899bSMichael S. Tsirkin 		}
6810690899bSMichael S. Tsirkin 		offset = 0;
6820690899bSMichael S. Tsirkin 		++from;
6830690899bSMichael S. Tsirkin 	}
6840690899bSMichael S. Tsirkin 	return 0;
6850690899bSMichael S. Tsirkin }
6860690899bSMichael S. Tsirkin 
6871da177e4SLinus Torvalds /* Get packet from user space buffer */
6880690899bSMichael S. Tsirkin static ssize_t tun_get_user(struct tun_struct *tun, void *msg_control,
6890690899bSMichael S. Tsirkin 			    const struct iovec *iv, size_t total_len,
6900690899bSMichael S. Tsirkin 			    size_t count, int noblock)
6911da177e4SLinus Torvalds {
69209640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
6931da177e4SLinus Torvalds 	struct sk_buff *skb;
6940690899bSMichael S. Tsirkin 	size_t len = total_len, align = NET_SKB_PAD;
695f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
6966f26c9a7SMichael S. Tsirkin 	int offset = 0;
6970690899bSMichael S. Tsirkin 	int copylen;
6980690899bSMichael S. Tsirkin 	bool zerocopy = false;
6990690899bSMichael S. Tsirkin 	int err;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
7020690899bSMichael S. Tsirkin 		if ((len -= sizeof(pi)) > total_len)
7031da177e4SLinus Torvalds 			return -EINVAL;
7041da177e4SLinus Torvalds 
7056f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
7061da177e4SLinus Torvalds 			return -EFAULT;
7076f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
710f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
7110690899bSMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) > total_len)
712f43798c2SRusty Russell 			return -EINVAL;
713f43798c2SRusty Russell 
7146f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
715f43798c2SRusty Russell 			return -EFAULT;
716f43798c2SRusty Russell 
7174909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
7184909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
7194909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
7204909122fSHerbert Xu 
721f43798c2SRusty Russell 		if (gso.hdr_len > len)
722f43798c2SRusty Russell 			return -EINVAL;
723d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
724f43798c2SRusty Russell 	}
725f43798c2SRusty Russell 
726e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
727a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
7280eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
7290eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
730e01bf1c8SRusty Russell 			return -EINVAL;
731e01bf1c8SRusty Russell 	}
7321da177e4SLinus Torvalds 
7330690899bSMichael S. Tsirkin 	if (msg_control)
7340690899bSMichael S. Tsirkin 		zerocopy = true;
7350690899bSMichael S. Tsirkin 
7360690899bSMichael S. Tsirkin 	if (zerocopy) {
7370690899bSMichael S. Tsirkin 		/* Userspace may produce vectors with count greater than
7380690899bSMichael S. Tsirkin 		 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
7390690899bSMichael S. Tsirkin 		 * to let the rest of data to be fit in the frags.
7400690899bSMichael S. Tsirkin 		 */
7410690899bSMichael S. Tsirkin 		if (count > MAX_SKB_FRAGS) {
7420690899bSMichael S. Tsirkin 			copylen = iov_length(iv, count - MAX_SKB_FRAGS);
7430690899bSMichael S. Tsirkin 			if (copylen < offset)
7440690899bSMichael S. Tsirkin 				copylen = 0;
7450690899bSMichael S. Tsirkin 			else
7460690899bSMichael S. Tsirkin 				copylen -= offset;
7470690899bSMichael S. Tsirkin 		} else
7480690899bSMichael S. Tsirkin 				copylen = 0;
7490690899bSMichael S. Tsirkin 		/* There are 256 bytes to be copied in skb, so there is enough
7500690899bSMichael S. Tsirkin 		 * room for skb expand head in case it is used.
7510690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
7520690899bSMichael S. Tsirkin 		 */
7530690899bSMichael S. Tsirkin 		if (copylen < gso.hdr_len)
7540690899bSMichael S. Tsirkin 			copylen = gso.hdr_len;
7550690899bSMichael S. Tsirkin 		if (!copylen)
7560690899bSMichael S. Tsirkin 			copylen = GOODCOPY_LEN;
7570690899bSMichael S. Tsirkin 	} else
7580690899bSMichael S. Tsirkin 		copylen = len;
7590690899bSMichael S. Tsirkin 
7600690899bSMichael S. Tsirkin 	skb = tun_alloc_skb(tun, align, copylen, gso.hdr_len, noblock);
76133dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
76233dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
76309f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
76433dccbb0SHerbert Xu 		return PTR_ERR(skb);
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 
7670690899bSMichael S. Tsirkin 	if (zerocopy)
7680690899bSMichael S. Tsirkin 		err = zerocopy_sg_from_iovec(skb, iv, offset, count);
7690690899bSMichael S. Tsirkin 	else
7700690899bSMichael S. Tsirkin 		err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
7710690899bSMichael S. Tsirkin 
7720690899bSMichael S. Tsirkin 	if (err) {
77309f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
7748f22757eSDave Jones 		kfree_skb(skb);
7751da177e4SLinus Torvalds 		return -EFAULT;
7768f22757eSDave Jones 	}
7771da177e4SLinus Torvalds 
778f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
779f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
780f43798c2SRusty Russell 					  gso.csum_offset)) {
781f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
782f43798c2SRusty Russell 			kfree_skb(skb);
783f43798c2SRusty Russell 			return -EINVAL;
784f43798c2SRusty Russell 		}
78588255375SMichał Mirosław 	}
786f43798c2SRusty Russell 
7871da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
7881da177e4SLinus Torvalds 	case TUN_TUN_DEV:
789f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
790f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
791f09f7ee2SAng Way Chuang 			case 0x40:
792f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
793f09f7ee2SAng Way Chuang 				break;
794f09f7ee2SAng Way Chuang 			case 0x60:
795f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
796f09f7ee2SAng Way Chuang 				break;
797f09f7ee2SAng Way Chuang 			default:
798f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
799f09f7ee2SAng Way Chuang 				kfree_skb(skb);
800f09f7ee2SAng Way Chuang 				return -EINVAL;
801f09f7ee2SAng Way Chuang 			}
802f09f7ee2SAng Way Chuang 		}
803f09f7ee2SAng Way Chuang 
804459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
8051da177e4SLinus Torvalds 		skb->protocol = pi.proto;
8064c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
8071da177e4SLinus Torvalds 		break;
8081da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8091da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
8101da177e4SLinus Torvalds 		break;
8116403eab1SJoe Perches 	}
8121da177e4SLinus Torvalds 
813f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
814f43798c2SRusty Russell 		pr_debug("GSO!\n");
815f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
816f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
817f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
818f43798c2SRusty Russell 			break;
819f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
820f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
821f43798c2SRusty Russell 			break;
822e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
823e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
824e36aa25aSSridhar Samudrala 			break;
825f43798c2SRusty Russell 		default:
826f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
827f43798c2SRusty Russell 			kfree_skb(skb);
828f43798c2SRusty Russell 			return -EINVAL;
829f43798c2SRusty Russell 		}
830f43798c2SRusty Russell 
831f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
832f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
833f43798c2SRusty Russell 
834f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
835f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
836f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
837f43798c2SRusty Russell 			kfree_skb(skb);
838f43798c2SRusty Russell 			return -EINVAL;
839f43798c2SRusty Russell 		}
840f43798c2SRusty Russell 
841f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
842f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
843f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
844f43798c2SRusty Russell 	}
8451da177e4SLinus Torvalds 
8460690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
8470690899bSMichael S. Tsirkin 	if (zerocopy) {
8480690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
8490690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
8500690899bSMichael S. Tsirkin 	}
8510690899bSMichael S. Tsirkin 
8521da177e4SLinus Torvalds 	netif_rx_ni(skb);
8531da177e4SLinus Torvalds 
85409f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
85509f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
8561da177e4SLinus Torvalds 
8570690899bSMichael S. Tsirkin 	return total_len;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
860ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
861ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
8621da177e4SLinus Torvalds {
86333dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
864ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
865631ab46bSEric W. Biederman 	ssize_t result;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	if (!tun)
8681da177e4SLinus Torvalds 		return -EBADFD;
8691da177e4SLinus Torvalds 
8706b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
8711da177e4SLinus Torvalds 
8720690899bSMichael S. Tsirkin 	result = tun_get_user(tun, NULL, iv, iov_length(iv, count), count,
87333dccbb0SHerbert Xu 			      file->f_flags & O_NONBLOCK);
874631ab46bSEric W. Biederman 
875631ab46bSEric W. Biederman 	tun_put(tun);
876631ab46bSEric W. Biederman 	return result;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds /* Put packet to the user space buffer */
8806f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
8811da177e4SLinus Torvalds 			    struct sk_buff *skb,
88243b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
8851da177e4SLinus Torvalds 	ssize_t total = 0;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
8881da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
8891da177e4SLinus Torvalds 			return -EINVAL;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 		if (len < skb->len) {
8921da177e4SLinus Torvalds 			/* Packet will be striped */
8931da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
8941da177e4SLinus Torvalds 		}
8951da177e4SLinus Torvalds 
89643b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
8971da177e4SLinus Torvalds 			return -EFAULT;
8981da177e4SLinus Torvalds 		total += sizeof(pi);
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
901f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
902f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
903d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
904f43798c2SRusty Russell 			return -EINVAL;
905f43798c2SRusty Russell 
906f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
907f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
908f43798c2SRusty Russell 
909f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
910f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
911f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
912f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
913f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
914f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
915f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
916e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
917e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
918ef3db4a5SMichael S. Tsirkin 			else {
9196b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
920ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
921ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
922ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
923ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
924ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
925ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
926ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
927ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
928ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
929ef3db4a5SMichael S. Tsirkin 			}
930f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
931f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
932f43798c2SRusty Russell 		} else
933f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
934f43798c2SRusty Russell 
935f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
936f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
93755508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
938f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
93910a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
94010a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
941f43798c2SRusty Russell 		} /* else everything is zero */
942f43798c2SRusty Russell 
94343b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
94443b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
945f43798c2SRusty Russell 			return -EFAULT;
946d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
947f43798c2SRusty Russell 	}
948f43798c2SRusty Russell 
9491da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
9501da177e4SLinus Torvalds 
95143b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
95205c2828cSMichael S. Tsirkin 	total += skb->len;
9531da177e4SLinus Torvalds 
95409f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
95509f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	return total;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
96005c2828cSMichael S. Tsirkin static ssize_t tun_do_read(struct tun_struct *tun,
96105c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
96205c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
9651da177e4SLinus Torvalds 	struct sk_buff *skb;
96605c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
9671da177e4SLinus Torvalds 
9686b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_read\n");
9691da177e4SLinus Torvalds 
97061a5ff15SAmos Kong 	if (unlikely(!noblock))
97143815482SEric Dumazet 		add_wait_queue(&tun->wq.wait, &wait);
9721da177e4SLinus Torvalds 	while (len) {
9731da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 		/* Read frames from the queue */
97689f56d1eSMichael S. Tsirkin 		if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
97705c2828cSMichael S. Tsirkin 			if (noblock) {
9781da177e4SLinus Torvalds 				ret = -EAGAIN;
9791da177e4SLinus Torvalds 				break;
9801da177e4SLinus Torvalds 			}
9811da177e4SLinus Torvalds 			if (signal_pending(current)) {
9821da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
9831da177e4SLinus Torvalds 				break;
9841da177e4SLinus Torvalds 			}
985c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
986c70f1829SEric W. Biederman 				ret = -EIO;
987c70f1829SEric W. Biederman 				break;
988c70f1829SEric W. Biederman 			}
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
9911da177e4SLinus Torvalds 			schedule();
9921da177e4SLinus Torvalds 			continue;
9931da177e4SLinus Torvalds 		}
9941da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
9951da177e4SLinus Torvalds 
99643b39dcdSMichael S. Tsirkin 		ret = tun_put_user(tun, skb, iv, len);
9971da177e4SLinus Torvalds 		kfree_skb(skb);
9981da177e4SLinus Torvalds 		break;
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
100261a5ff15SAmos Kong 	if (unlikely(!noblock))
100343815482SEric Dumazet 		remove_wait_queue(&tun->wq.wait, &wait);
10041da177e4SLinus Torvalds 
100505c2828cSMichael S. Tsirkin 	return ret;
100605c2828cSMichael S. Tsirkin }
100705c2828cSMichael S. Tsirkin 
100805c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
100905c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
101005c2828cSMichael S. Tsirkin {
101105c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
101205c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
101305c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
101405c2828cSMichael S. Tsirkin 	ssize_t len, ret;
101505c2828cSMichael S. Tsirkin 
101605c2828cSMichael S. Tsirkin 	if (!tun)
101705c2828cSMichael S. Tsirkin 		return -EBADFD;
101805c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
101905c2828cSMichael S. Tsirkin 	if (len < 0) {
102005c2828cSMichael S. Tsirkin 		ret = -EINVAL;
102105c2828cSMichael S. Tsirkin 		goto out;
102205c2828cSMichael S. Tsirkin 	}
102305c2828cSMichael S. Tsirkin 
102405c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
102505c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
1026631ab46bSEric W. Biederman out:
1027631ab46bSEric W. Biederman 	tun_put(tun);
10281da177e4SLinus Torvalds 	return ret;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	tun->owner = -1;
10368c644623SGuido Guenther 	tun->group = -1;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
10399c3fea6aSHerbert Xu 	dev->destructor = tun_free_netdev;
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
1042f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1043f019a7a5SEric W. Biederman  * device with netlink.
1044f019a7a5SEric W. Biederman  */
1045f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1046f019a7a5SEric W. Biederman {
1047f019a7a5SEric W. Biederman 	return -EINVAL;
1048f019a7a5SEric W. Biederman }
1049f019a7a5SEric W. Biederman 
1050f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1051f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1052f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1053f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1054f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1055f019a7a5SEric W. Biederman };
1056f019a7a5SEric W. Biederman 
105733dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
105833dccbb0SHerbert Xu {
105933dccbb0SHerbert Xu 	struct tun_struct *tun;
106043815482SEric Dumazet 	wait_queue_head_t *wqueue;
106133dccbb0SHerbert Xu 
106233dccbb0SHerbert Xu 	if (!sock_writeable(sk))
106333dccbb0SHerbert Xu 		return;
106433dccbb0SHerbert Xu 
106533dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
106633dccbb0SHerbert Xu 		return;
106733dccbb0SHerbert Xu 
106843815482SEric Dumazet 	wqueue = sk_sleep(sk);
106943815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
107043815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
107105c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1072c722c625SHerbert Xu 
107380924e5fSVitaliy Gusev 	tun = tun_sk(sk)->tun;
107433dccbb0SHerbert Xu 	kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
107533dccbb0SHerbert Xu }
107633dccbb0SHerbert Xu 
107733dccbb0SHerbert Xu static void tun_sock_destruct(struct sock *sk)
107833dccbb0SHerbert Xu {
107980924e5fSVitaliy Gusev 	free_netdev(tun_sk(sk)->tun->dev);
108033dccbb0SHerbert Xu }
108133dccbb0SHerbert Xu 
108205c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
108305c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
108405c2828cSMichael S. Tsirkin {
108505c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
10860690899bSMichael S. Tsirkin 	return tun_get_user(tun, m->msg_control, m->msg_iov, total_len,
10870690899bSMichael S. Tsirkin 			    m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
108805c2828cSMichael S. Tsirkin }
108905c2828cSMichael S. Tsirkin 
109005c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
109105c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
109205c2828cSMichael S. Tsirkin 		       int flags)
109305c2828cSMichael S. Tsirkin {
109405c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
109505c2828cSMichael S. Tsirkin 	int ret;
109605c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
109705c2828cSMichael S. Tsirkin 		return -EINVAL;
109805c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
109905c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
110005c2828cSMichael S. Tsirkin 	if (ret > total_len) {
110105c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
110205c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
110305c2828cSMichael S. Tsirkin 	}
110405c2828cSMichael S. Tsirkin 	return ret;
110505c2828cSMichael S. Tsirkin }
110605c2828cSMichael S. Tsirkin 
11071ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
11081ab5ecb9SStanislav Kinsbursky {
11091ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
11101ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
11111ab5ecb9SStanislav Kinsbursky 	return 0;
11121ab5ecb9SStanislav Kinsbursky }
11131ab5ecb9SStanislav Kinsbursky 
111405c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
111505c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
111605c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
111705c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
11181ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
111905c2828cSMichael S. Tsirkin };
112005c2828cSMichael S. Tsirkin 
112133dccbb0SHerbert Xu static struct proto tun_proto = {
112233dccbb0SHerbert Xu 	.name		= "tun",
112333dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
112433dccbb0SHerbert Xu 	.obj_size	= sizeof(struct tun_sock),
112533dccbb0SHerbert Xu };
1126f019a7a5SEric W. Biederman 
1127980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1128980c9e8cSDavid Woodhouse {
1129980c9e8cSDavid Woodhouse 	int flags = 0;
1130980c9e8cSDavid Woodhouse 
1131980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1132980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1133980c9e8cSDavid Woodhouse 	else
1134980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1135980c9e8cSDavid Woodhouse 
1136980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1137980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1138980c9e8cSDavid Woodhouse 
1139980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1140980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1141980c9e8cSDavid Woodhouse 
1142980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1143980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1144980c9e8cSDavid Woodhouse 
1145980c9e8cSDavid Woodhouse 	return flags;
1146980c9e8cSDavid Woodhouse }
1147980c9e8cSDavid Woodhouse 
1148980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1149980c9e8cSDavid Woodhouse 			      char *buf)
1150980c9e8cSDavid Woodhouse {
1151980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1152980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1153980c9e8cSDavid Woodhouse }
1154980c9e8cSDavid Woodhouse 
1155980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1156980c9e8cSDavid Woodhouse 			      char *buf)
1157980c9e8cSDavid Woodhouse {
1158980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1159980c9e8cSDavid Woodhouse 	return sprintf(buf, "%d\n", tun->owner);
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));
1166980c9e8cSDavid Woodhouse 	return sprintf(buf, "%d\n", tun->group);
1167980c9e8cSDavid Woodhouse }
1168980c9e8cSDavid Woodhouse 
1169980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1170980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1171980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1172980c9e8cSDavid Woodhouse 
1173d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
11741da177e4SLinus Torvalds {
117533dccbb0SHerbert Xu 	struct sock *sk;
11761da177e4SLinus Torvalds 	struct tun_struct *tun;
11771da177e4SLinus Torvalds 	struct net_device *dev;
11781da177e4SLinus Torvalds 	int err;
11791da177e4SLinus Torvalds 
118074a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
118174a3e5a7SEric W. Biederman 	if (dev) {
11822b980dbdSPaul Moore 		const struct cred *cred = current_cred();
11832b980dbdSPaul Moore 
1184f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1185f85ba780SDavid Woodhouse 			return -EBUSY;
118674a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
118774a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
118874a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
118974a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
119074a3e5a7SEric W. Biederman 		else
119174a3e5a7SEric W. Biederman 			return -EINVAL;
119274a3e5a7SEric W. Biederman 
11932b980dbdSPaul Moore 		if (((tun->owner != -1 && cred->euid != tun->owner) ||
11942b980dbdSPaul Moore 		     (tun->group != -1 && !in_egroup_p(tun->group))) &&
11952b980dbdSPaul Moore 		    !capable(CAP_NET_ADMIN))
11962b980dbdSPaul Moore 			return -EPERM;
1197d7e9660aSLinus Torvalds 		err = security_tun_dev_attach(tun->socket.sk);
11982b980dbdSPaul Moore 		if (err < 0)
11992b980dbdSPaul Moore 			return err;
12002b980dbdSPaul Moore 
1201a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1202a7385ba2SEric W. Biederman 		if (err < 0)
1203a7385ba2SEric W. Biederman 			return err;
120486a264abSDavid Howells 	}
12051da177e4SLinus Torvalds 	else {
12061da177e4SLinus Torvalds 		char *name;
12071da177e4SLinus Torvalds 		unsigned long flags = 0;
12081da177e4SLinus Torvalds 
1209ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
1210ca6bb5d7SDavid Woodhouse 			return -EPERM;
12112b980dbdSPaul Moore 		err = security_tun_dev_create();
12122b980dbdSPaul Moore 		if (err < 0)
12132b980dbdSPaul Moore 			return err;
1214ca6bb5d7SDavid Woodhouse 
12151da177e4SLinus Torvalds 		/* Set dev type */
12161da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
12171da177e4SLinus Torvalds 			/* TUN device */
12181da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
12191da177e4SLinus Torvalds 			name = "tun%d";
12201da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
12211da177e4SLinus Torvalds 			/* TAP device */
12221da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
12231da177e4SLinus Torvalds 			name = "tap%d";
12241da177e4SLinus Torvalds 		} else
122536989b90SKusanagi Kouichi 			return -EINVAL;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 		if (*ifr->ifr_name)
12281da177e4SLinus Torvalds 			name = ifr->ifr_name;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
12311da177e4SLinus Torvalds 				   tun_setup);
12321da177e4SLinus Torvalds 		if (!dev)
12331da177e4SLinus Torvalds 			return -ENOMEM;
12341da177e4SLinus Torvalds 
1235fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1236f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1237758e43b7SStephen Hemminger 
12381da177e4SLinus Torvalds 		tun = netdev_priv(dev);
12391da177e4SLinus Torvalds 		tun->dev = dev;
12401da177e4SLinus Torvalds 		tun->flags = flags;
1241f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1242d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1243b09e786bSMikulas Patocka 		set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);
12441da177e4SLinus Torvalds 
124533dccbb0SHerbert Xu 		err = -ENOMEM;
12461ab5ecb9SStanislav Kinsbursky 		sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
124733dccbb0SHerbert Xu 		if (!sk)
124833dccbb0SHerbert Xu 			goto err_free_dev;
124933dccbb0SHerbert Xu 
12501ab5ecb9SStanislav Kinsbursky 		sk_change_net(sk, net);
125143815482SEric Dumazet 		tun->socket.wq = &tun->wq;
125243815482SEric Dumazet 		init_waitqueue_head(&tun->wq.wait);
125305c2828cSMichael S. Tsirkin 		tun->socket.ops = &tun_socket_ops;
125433dccbb0SHerbert Xu 		sock_init_data(&tun->socket, sk);
125533dccbb0SHerbert Xu 		sk->sk_write_space = tun_sock_write_space;
125633dccbb0SHerbert Xu 		sk->sk_sndbuf = INT_MAX;
12570690899bSMichael S. Tsirkin 		sock_set_flag(sk, SOCK_ZEROCOPY);
125833dccbb0SHerbert Xu 
125980924e5fSVitaliy Gusev 		tun_sk(sk)->tun = tun;
126033dccbb0SHerbert Xu 
12612b980dbdSPaul Moore 		security_tun_dev_post_create(sk);
12622b980dbdSPaul Moore 
12631da177e4SLinus Torvalds 		tun_net_init(dev);
12641da177e4SLinus Torvalds 
126588255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
126688255375SMichał Mirosław 			TUN_USER_FEATURES;
126788255375SMichał Mirosław 		dev->features = dev->hw_features;
126888255375SMichał Mirosław 
12691da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
12701da177e4SLinus Torvalds 		if (err < 0)
12719c3fea6aSHerbert Xu 			goto err_free_sk;
12729c3fea6aSHerbert Xu 
1273980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1274980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1275980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
12766b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1277980c9e8cSDavid Woodhouse 
12789c3fea6aSHerbert Xu 		sk->sk_destruct = tun_sock_destruct;
1279a7385ba2SEric W. Biederman 
1280a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1281a7385ba2SEric W. Biederman 		if (err < 0)
12829c3fea6aSHerbert Xu 			goto failed;
12831da177e4SLinus Torvalds 	}
12841da177e4SLinus Torvalds 
12856b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
12881da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1289a26af1e0SNathaniel Filardo 	else
1290a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
12931da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1294a26af1e0SNathaniel Filardo 	else
1295a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
12961da177e4SLinus Torvalds 
1297f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1298f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1299f43798c2SRusty Russell 	else
1300f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1301f43798c2SRusty Russell 
1302e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1303e35259a9SMax Krasnyansky 	 * xoff state.
1304e35259a9SMax Krasnyansky 	 */
1305e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1306e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
1307e35259a9SMax Krasnyansky 
13081da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
13091da177e4SLinus Torvalds 	return 0;
13101da177e4SLinus Torvalds 
131133dccbb0SHerbert Xu  err_free_sk:
13121ab5ecb9SStanislav Kinsbursky 	tun_free_netdev(dev);
13131da177e4SLinus Torvalds  err_free_dev:
13141da177e4SLinus Torvalds 	free_netdev(dev);
13151da177e4SLinus Torvalds  failed:
13161da177e4SLinus Torvalds 	return err;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
1319876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun,
1320876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1321e3b99556SMark McLoughlin {
13226b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1323e3b99556SMark McLoughlin 
1324e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1325e3b99556SMark McLoughlin 
1326980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1327e3b99556SMark McLoughlin 
1328e3b99556SMark McLoughlin 	return 0;
1329e3b99556SMark McLoughlin }
1330e3b99556SMark McLoughlin 
13315228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
13325228ddc9SRusty Russell  * privs required. */
133388255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
13345228ddc9SRusty Russell {
1335c8f44affSMichał Mirosław 	netdev_features_t features = 0;
13365228ddc9SRusty Russell 
13375228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
133888255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
13395228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
13405228ddc9SRusty Russell 
13415228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
13425228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
13435228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
13445228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
13455228ddc9SRusty Russell 			}
13465228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
13475228ddc9SRusty Russell 				features |= NETIF_F_TSO;
13485228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
13495228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
13505228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
13515228ddc9SRusty Russell 		}
1352e36aa25aSSridhar Samudrala 
1353e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1354e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1355e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1356e36aa25aSSridhar Samudrala 		}
13575228ddc9SRusty Russell 	}
13585228ddc9SRusty Russell 
13595228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
13605228ddc9SRusty Russell 	 * trying to set them. */
13615228ddc9SRusty Russell 	if (arg)
13625228ddc9SRusty Russell 		return -EINVAL;
13635228ddc9SRusty Russell 
136488255375SMichał Mirosław 	tun->set_features = features;
136588255375SMichał Mirosław 	netdev_update_features(tun->dev);
13665228ddc9SRusty Russell 
13675228ddc9SRusty Russell 	return 0;
13685228ddc9SRusty Russell }
13695228ddc9SRusty Russell 
137050857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
137150857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
13721da177e4SLinus Torvalds {
137336b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1374631ab46bSEric W. Biederman 	struct tun_struct *tun;
13751da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
137699405162SMichael S. Tsirkin 	struct sock_fprog fprog;
13771da177e4SLinus Torvalds 	struct ifreq ifr;
137833dccbb0SHerbert Xu 	int sndbuf;
1379d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1380f271b2ccSMax Krasnyansky 	int ret;
13811da177e4SLinus Torvalds 
1382*a117dacdSMathias Krause 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
138350857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
13841da177e4SLinus Torvalds 			return -EFAULT;
1385*a117dacdSMathias Krause 	} else
1386*a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
13871da177e4SLinus Torvalds 
1388631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1389631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1390631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1391631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1392631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1393631ab46bSEric W. Biederman 				IFF_VNET_HDR,
1394631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1395631ab46bSEric W. Biederman 	}
1396631ab46bSEric W. Biederman 
1397876bfd4dSHerbert Xu 	rtnl_lock();
1398876bfd4dSHerbert Xu 
139936b50babSEric W. Biederman 	tun = __tun_get(tfile);
14001da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
14011da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
14021da177e4SLinus Torvalds 
1403876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
14041da177e4SLinus Torvalds 
1405876bfd4dSHerbert Xu 		if (ret)
1406876bfd4dSHerbert Xu 			goto unlock;
14071da177e4SLinus Torvalds 
140850857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1409876bfd4dSHerbert Xu 			ret = -EFAULT;
1410876bfd4dSHerbert Xu 		goto unlock;
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds 
1413876bfd4dSHerbert Xu 	ret = -EBADFD;
14141da177e4SLinus Torvalds 	if (!tun)
1415876bfd4dSHerbert Xu 		goto unlock;
14161da177e4SLinus Torvalds 
14176b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
14181da177e4SLinus Torvalds 
1419631ab46bSEric W. Biederman 	ret = 0;
14201da177e4SLinus Torvalds 	switch (cmd) {
1421e3b99556SMark McLoughlin 	case TUNGETIFF:
1422876bfd4dSHerbert Xu 		ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1423e3b99556SMark McLoughlin 		if (ret)
1424631ab46bSEric W. Biederman 			break;
1425e3b99556SMark McLoughlin 
142650857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1427631ab46bSEric W. Biederman 			ret = -EFAULT;
1428e3b99556SMark McLoughlin 		break;
1429e3b99556SMark McLoughlin 
14301da177e4SLinus Torvalds 	case TUNSETNOCSUM:
14311da177e4SLinus Torvalds 		/* Disable/Enable checksum */
14321da177e4SLinus Torvalds 
143388255375SMichał Mirosław 		/* [unimplemented] */
143488255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
14356b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
14361da177e4SLinus Torvalds 		break;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	case TUNSETPERSIST:
14391da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
14401da177e4SLinus Torvalds 		if (arg)
14411da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
14421da177e4SLinus Torvalds 		else
14431da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
14441da177e4SLinus Torvalds 
14456b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
14466b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
14471da177e4SLinus Torvalds 		break;
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	case TUNSETOWNER:
14501da177e4SLinus Torvalds 		/* Set owner of the device */
14511da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
14521da177e4SLinus Torvalds 
14536b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
14541da177e4SLinus Torvalds 		break;
14551da177e4SLinus Torvalds 
14568c644623SGuido Guenther 	case TUNSETGROUP:
14578c644623SGuido Guenther 		/* Set group of the device */
14588c644623SGuido Guenther 		tun->group= (gid_t) arg;
14598c644623SGuido Guenther 
14606b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
14618c644623SGuido Guenther 		break;
14628c644623SGuido Guenther 
1463ff4cc3acSMike Kershaw 	case TUNSETLINK:
1464ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1465ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
14666b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
14676b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
146848abfe05SDavid S. Miller 			ret = -EBUSY;
1469ff4cc3acSMike Kershaw 		} else {
1470ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
14716b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
14726b8a66eeSJoe Perches 				  tun->dev->type);
147348abfe05SDavid S. Miller 			ret = 0;
1474ff4cc3acSMike Kershaw 		}
1475631ab46bSEric W. Biederman 		break;
1476ff4cc3acSMike Kershaw 
14771da177e4SLinus Torvalds #ifdef TUN_DEBUG
14781da177e4SLinus Torvalds 	case TUNSETDEBUG:
14791da177e4SLinus Torvalds 		tun->debug = arg;
14801da177e4SLinus Torvalds 		break;
14811da177e4SLinus Torvalds #endif
14825228ddc9SRusty Russell 	case TUNSETOFFLOAD:
148388255375SMichał Mirosław 		ret = set_offload(tun, arg);
1484631ab46bSEric W. Biederman 		break;
14855228ddc9SRusty Russell 
1486f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1487f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1488631ab46bSEric W. Biederman 		ret = -EINVAL;
1489f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1490631ab46bSEric W. Biederman 			break;
1491c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1492631ab46bSEric W. Biederman 		break;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1495b595076aSUwe Kleine-König 		/* Get hw address */
1496f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1497f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
149850857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1499631ab46bSEric W. Biederman 			ret = -EFAULT;
1500631ab46bSEric W. Biederman 		break;
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1503f271b2ccSMax Krasnyansky 		/* Set hw address */
15046b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
15056b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
150640102371SKim B. Heino 
150740102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1508631ab46bSEric W. Biederman 		break;
150933dccbb0SHerbert Xu 
151033dccbb0SHerbert Xu 	case TUNGETSNDBUF:
151189f56d1eSMichael S. Tsirkin 		sndbuf = tun->socket.sk->sk_sndbuf;
151233dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
151333dccbb0SHerbert Xu 			ret = -EFAULT;
151433dccbb0SHerbert Xu 		break;
151533dccbb0SHerbert Xu 
151633dccbb0SHerbert Xu 	case TUNSETSNDBUF:
151733dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
151833dccbb0SHerbert Xu 			ret = -EFAULT;
151933dccbb0SHerbert Xu 			break;
152033dccbb0SHerbert Xu 		}
152133dccbb0SHerbert Xu 
152289f56d1eSMichael S. Tsirkin 		tun->socket.sk->sk_sndbuf = sndbuf;
152333dccbb0SHerbert Xu 		break;
152433dccbb0SHerbert Xu 
1525d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
1526d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
1527d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1528d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1529d9d52b51SMichael S. Tsirkin 		break;
1530d9d52b51SMichael S. Tsirkin 
1531d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
1532d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1533d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1534d9d52b51SMichael S. Tsirkin 			break;
1535d9d52b51SMichael S. Tsirkin 		}
1536d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1537d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
1538d9d52b51SMichael S. Tsirkin 			break;
1539d9d52b51SMichael S. Tsirkin 		}
1540d9d52b51SMichael S. Tsirkin 
1541d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
1542d9d52b51SMichael S. Tsirkin 		break;
1543d9d52b51SMichael S. Tsirkin 
154499405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
154599405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
154699405162SMichael S. Tsirkin 		ret = -EINVAL;
154799405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
154899405162SMichael S. Tsirkin 			break;
154999405162SMichael S. Tsirkin 		ret = -EFAULT;
155099405162SMichael S. Tsirkin 		if (copy_from_user(&fprog, argp, sizeof(fprog)))
155199405162SMichael S. Tsirkin 			break;
155299405162SMichael S. Tsirkin 
155399405162SMichael S. Tsirkin 		ret = sk_attach_filter(&fprog, tun->socket.sk);
155499405162SMichael S. Tsirkin 		break;
155599405162SMichael S. Tsirkin 
155699405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
155799405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
155899405162SMichael S. Tsirkin 		ret = -EINVAL;
155999405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
156099405162SMichael S. Tsirkin 			break;
156199405162SMichael S. Tsirkin 		ret = sk_detach_filter(tun->socket.sk);
156299405162SMichael S. Tsirkin 		break;
156399405162SMichael S. Tsirkin 
15641da177e4SLinus Torvalds 	default:
1565631ab46bSEric W. Biederman 		ret = -EINVAL;
1566631ab46bSEric W. Biederman 		break;
1567ee289b64SJoe Perches 	}
15681da177e4SLinus Torvalds 
1569876bfd4dSHerbert Xu unlock:
1570876bfd4dSHerbert Xu 	rtnl_unlock();
1571876bfd4dSHerbert Xu 	if (tun)
1572631ab46bSEric W. Biederman 		tun_put(tun);
1573631ab46bSEric W. Biederman 	return ret;
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
157650857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
157750857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
157850857e2aSArnd Bergmann {
157950857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
158050857e2aSArnd Bergmann }
158150857e2aSArnd Bergmann 
158250857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
158350857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
158450857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
158550857e2aSArnd Bergmann {
158650857e2aSArnd Bergmann 	switch (cmd) {
158750857e2aSArnd Bergmann 	case TUNSETIFF:
158850857e2aSArnd Bergmann 	case TUNGETIFF:
158950857e2aSArnd Bergmann 	case TUNSETTXFILTER:
159050857e2aSArnd Bergmann 	case TUNGETSNDBUF:
159150857e2aSArnd Bergmann 	case TUNSETSNDBUF:
159250857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
159350857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
159450857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
159550857e2aSArnd Bergmann 		break;
159650857e2aSArnd Bergmann 	default:
159750857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
159850857e2aSArnd Bergmann 		break;
159950857e2aSArnd Bergmann 	}
160050857e2aSArnd Bergmann 
160150857e2aSArnd Bergmann 	/*
160250857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
160350857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
160450857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
160550857e2aSArnd Bergmann 	 * contents.
160650857e2aSArnd Bergmann 	 */
160750857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
160850857e2aSArnd Bergmann }
160950857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
161050857e2aSArnd Bergmann 
16111da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
16121da177e4SLinus Torvalds {
1613631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
16141da177e4SLinus Torvalds 	int ret;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	if (!tun)
16171da177e4SLinus Torvalds 		return -EBADFD;
16181da177e4SLinus Torvalds 
16196b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
16229d319522SJonathan Corbet 		goto out;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	if (on) {
1625609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
16261da177e4SLinus Torvalds 		if (ret)
16279d319522SJonathan Corbet 			goto out;
16281da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
16291da177e4SLinus Torvalds 	} else
16301da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
16319d319522SJonathan Corbet 	ret = 0;
16329d319522SJonathan Corbet out:
1633631ab46bSEric W. Biederman 	tun_put(tun);
16349d319522SJonathan Corbet 	return ret;
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
16381da177e4SLinus Torvalds {
1639631ab46bSEric W. Biederman 	struct tun_file *tfile;
1640deed49fbSThomas Gleixner 
16416b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1642631ab46bSEric W. Biederman 
1643631ab46bSEric W. Biederman 	tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1644631ab46bSEric W. Biederman 	if (!tfile)
1645631ab46bSEric W. Biederman 		return -ENOMEM;
1646c70f1829SEric W. Biederman 	atomic_set(&tfile->count, 0);
1647631ab46bSEric W. Biederman 	tfile->tun = NULL;
164836b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1649631ab46bSEric W. Biederman 	file->private_data = tfile;
16501da177e4SLinus Torvalds 	return 0;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
16541da177e4SLinus Torvalds {
1655631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1656f0a4d0e5SEric W. Biederman 	struct tun_struct *tun;
16571da177e4SLinus Torvalds 
1658f0a4d0e5SEric W. Biederman 	tun = __tun_get(tfile);
1659631ab46bSEric W. Biederman 	if (tun) {
1660d23e4365SHerbert Xu 		struct net_device *dev = tun->dev;
1661d23e4365SHerbert Xu 
16626b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "tun_chr_close\n");
16631da177e4SLinus Torvalds 
1664631ab46bSEric W. Biederman 		__tun_detach(tun);
16651da177e4SLinus Torvalds 
16663ad2f3fbSDaniel Mack 		/* If desirable, unregister the netdevice. */
1667d23e4365SHerbert Xu 		if (!(tun->flags & TUN_PERSIST)) {
1668d23e4365SHerbert Xu 			rtnl_lock();
1669d23e4365SHerbert Xu 			if (dev->reg_state == NETREG_REGISTERED)
1670d23e4365SHerbert Xu 				unregister_netdevice(dev);
1671f0a4d0e5SEric W. Biederman 			rtnl_unlock();
1672d23e4365SHerbert Xu 		}
1673d23e4365SHerbert Xu 	}
1674631ab46bSEric W. Biederman 
16759c3fea6aSHerbert Xu 	tun = tfile->tun;
16769c3fea6aSHerbert Xu 	if (tun)
167789f56d1eSMichael S. Tsirkin 		sock_put(tun->socket.sk);
16789c3fea6aSHerbert Xu 
167936b50babSEric W. Biederman 	put_net(tfile->net);
1680631ab46bSEric W. Biederman 	kfree(tfile);
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 	return 0;
16831da177e4SLinus Torvalds }
16841da177e4SLinus Torvalds 
1685d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
16861da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
16871da177e4SLinus Torvalds 	.llseek = no_llseek,
1688ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1689ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1690ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1691ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
16921da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
1693876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
169450857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
169550857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
169650857e2aSArnd Bergmann #endif
16971da177e4SLinus Torvalds 	.open	= tun_chr_open,
16981da177e4SLinus Torvalds 	.release = tun_chr_close,
16991da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
17001da177e4SLinus Torvalds };
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
17031da177e4SLinus Torvalds 	.minor = TUN_MINOR,
17041da177e4SLinus Torvalds 	.name = "tun",
1705e454cea2SKay Sievers 	.nodename = "net/tun",
17061da177e4SLinus Torvalds 	.fops = &tun_fops,
17071da177e4SLinus Torvalds };
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds /* ethtool interface */
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
17121da177e4SLinus Torvalds {
17131da177e4SLinus Torvalds 	cmd->supported		= 0;
17141da177e4SLinus Torvalds 	cmd->advertising	= 0;
171570739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
17161da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
17171da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
17181da177e4SLinus Torvalds 	cmd->phy_address	= 0;
17191da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
17201da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
17211da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
17221da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
17231da177e4SLinus Torvalds 	return 0;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
17271da177e4SLinus Torvalds {
17281da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17291da177e4SLinus Torvalds 
173033a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
173133a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
17341da177e4SLinus Torvalds 	case TUN_TUN_DEV:
173533a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
17361da177e4SLinus Torvalds 		break;
17371da177e4SLinus Torvalds 	case TUN_TAP_DEV:
173833a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
17391da177e4SLinus Torvalds 		break;
17401da177e4SLinus Torvalds 	}
17411da177e4SLinus Torvalds }
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
17441da177e4SLinus Torvalds {
17451da177e4SLinus Torvalds #ifdef TUN_DEBUG
17461da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17471da177e4SLinus Torvalds 	return tun->debug;
17481da177e4SLinus Torvalds #else
17491da177e4SLinus Torvalds 	return -EOPNOTSUPP;
17501da177e4SLinus Torvalds #endif
17511da177e4SLinus Torvalds }
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
17541da177e4SLinus Torvalds {
17551da177e4SLinus Torvalds #ifdef TUN_DEBUG
17561da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17571da177e4SLinus Torvalds 	tun->debug = value;
17581da177e4SLinus Torvalds #endif
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17617282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
17621da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
17631da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
17641da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
17651da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
1766bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
17671da177e4SLinus Torvalds };
17681da177e4SLinus Torvalds 
176979d17604SPavel Emelyanov 
17701da177e4SLinus Torvalds static int __init tun_init(void)
17711da177e4SLinus Torvalds {
17721da177e4SLinus Torvalds 	int ret = 0;
17731da177e4SLinus Torvalds 
17746b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
17756b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
17761da177e4SLinus Torvalds 
1777f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
177879d17604SPavel Emelyanov 	if (ret) {
17796b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
1780f019a7a5SEric W. Biederman 		goto err_linkops;
178179d17604SPavel Emelyanov 	}
178279d17604SPavel Emelyanov 
17831da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
178479d17604SPavel Emelyanov 	if (ret) {
17856b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
178679d17604SPavel Emelyanov 		goto err_misc;
178779d17604SPavel Emelyanov 	}
178879d17604SPavel Emelyanov 	return  0;
178979d17604SPavel Emelyanov err_misc:
1790f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
1791f019a7a5SEric W. Biederman err_linkops:
17921da177e4SLinus Torvalds 	return ret;
17931da177e4SLinus Torvalds }
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds static void tun_cleanup(void)
17961da177e4SLinus Torvalds {
17971da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
1798f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds 
180105c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
180205c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
180305c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
180405c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
180505c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
180605c2828cSMichael S. Tsirkin {
180705c2828cSMichael S. Tsirkin 	struct tun_struct *tun;
180805c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
180905c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
181005c2828cSMichael S. Tsirkin 	tun = tun_get(file);
181105c2828cSMichael S. Tsirkin 	if (!tun)
181205c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
181305c2828cSMichael S. Tsirkin 	tun_put(tun);
181405c2828cSMichael S. Tsirkin 	return &tun->socket;
181505c2828cSMichael S. Tsirkin }
181605c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
181705c2828cSMichael S. Tsirkin 
18181da177e4SLinus Torvalds module_init(tun_init);
18191da177e4SLinus Torvalds module_exit(tun_cleanup);
18201da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
18211da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
18221da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18231da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1824578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
1825