xref: /openbmc/linux/drivers/net/tun.c (revision 54f968d6efdbf7dec36faa44fc11f01b0e4d1990)
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 
112*54f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It
113*54f968d6SJason Wang  * also contains all socket related strctures (except sock_fprog and tap_filter)
114*54f968d6SJason Wang  * to serve as one transmit queue for tuntap device. The sock_fprog and
115*54f968d6SJason Wang  * tap_filter were kept in tun_struct since they were used for filtering for the
116*54f968d6SJason Wang  * netdevice not for a specific queue (at least I didn't see the reqirement for
117*54f968d6SJason Wang  * this).
118*54f968d6SJason Wang  */
119631ab46bSEric W. Biederman struct tun_file {
120*54f968d6SJason Wang 	struct sock sk;
121*54f968d6SJason Wang 	struct socket socket;
122*54f968d6SJason Wang 	struct socket_wq wq;
123c70f1829SEric W. Biederman 	atomic_t count;
124631ab46bSEric W. Biederman 	struct tun_struct *tun;
12536b50babSEric W. Biederman 	struct net *net;
126*54f968d6SJason Wang 	struct fasync_struct *fasync;
127*54f968d6SJason Wang 	/* only used for fasnyc */
128*54f968d6SJason Wang 	unsigned int flags;
129631ab46bSEric W. Biederman };
130631ab46bSEric W. Biederman 
131*54f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
132*54f968d6SJason Wang  * device, socket fileter, sndbuf and vnet header size were restore when the
133*54f968d6SJason Wang  * file were attached to a persist device.
134*54f968d6SJason Wang  */
13514daa021SRusty Russell struct tun_struct {
136631ab46bSEric W. Biederman 	struct tun_file		*tfile;
137f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1380625c883SEric W. Biederman 	kuid_t			owner;
1390625c883SEric W. Biederman 	kgid_t			group;
14014daa021SRusty Russell 
14114daa021SRusty Russell 	struct net_device	*dev;
142c8f44affSMichał Mirosław 	netdev_features_t	set_features;
14388255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
14488255375SMichał Mirosław 			  NETIF_F_TSO6|NETIF_F_UFO)
145d9d52b51SMichael S. Tsirkin 
146d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
147*54f968d6SJason Wang 	int			sndbuf;
148*54f968d6SJason Wang 	struct tap_filter	txflt;
149*54f968d6SJason Wang 	struct sock_fprog	fprog;
150*54f968d6SJason Wang 	/* protected by rtnl lock */
151*54f968d6SJason Wang 	bool			filter_attached;
15214daa021SRusty Russell #ifdef TUN_DEBUG
15314daa021SRusty Russell 	int debug;
15414daa021SRusty Russell #endif
15514daa021SRusty Russell };
15614daa021SRusty Russell 
157a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
158a7385ba2SEric W. Biederman {
159631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
16038231b7aSEric W. Biederman 	int err;
161a7385ba2SEric W. Biederman 
162a7385ba2SEric W. Biederman 	ASSERT_RTNL();
163a7385ba2SEric W. Biederman 
16438231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
16538231b7aSEric W. Biederman 
16638231b7aSEric W. Biederman 	err = -EINVAL;
16738231b7aSEric W. Biederman 	if (tfile->tun)
16838231b7aSEric W. Biederman 		goto out;
16938231b7aSEric W. Biederman 
17038231b7aSEric W. Biederman 	err = -EBUSY;
17138231b7aSEric W. Biederman 	if (tun->tfile)
17238231b7aSEric W. Biederman 		goto out;
17338231b7aSEric W. Biederman 
17438231b7aSEric W. Biederman 	err = 0;
175*54f968d6SJason Wang 
176*54f968d6SJason Wang 	/* Re-attach filter when attaching to a persist device */
177*54f968d6SJason Wang 	if (tun->filter_attached == true) {
178*54f968d6SJason Wang 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
179*54f968d6SJason Wang 		if (!err)
180*54f968d6SJason Wang 			goto out;
181*54f968d6SJason Wang 	}
182631ab46bSEric W. Biederman 	tfile->tun = tun;
183*54f968d6SJason Wang 	tfile->socket.sk->sk_sndbuf = tun->sndbuf;
184631ab46bSEric W. Biederman 	tun->tfile = tfile;
185bee31369SNolan Leake 	netif_carrier_on(tun->dev);
186c70f1829SEric W. Biederman 	dev_hold(tun->dev);
187*54f968d6SJason Wang 	sock_hold(&tfile->sk);
188c70f1829SEric W. Biederman 	atomic_inc(&tfile->count);
189a7385ba2SEric W. Biederman 
19038231b7aSEric W. Biederman out:
19138231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
19238231b7aSEric W. Biederman 	return err;
193a7385ba2SEric W. Biederman }
194a7385ba2SEric W. Biederman 
195631ab46bSEric W. Biederman static void __tun_detach(struct tun_struct *tun)
196631ab46bSEric W. Biederman {
197*54f968d6SJason Wang 	struct tun_file *tfile = tun->tfile;
198631ab46bSEric W. Biederman 	/* Detach from net device */
19938231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
200bee31369SNolan Leake 	netif_carrier_off(tun->dev);
201631ab46bSEric W. Biederman 	tun->tfile = NULL;
202*54f968d6SJason Wang 	tfile->tun = NULL;
20338231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
204631ab46bSEric W. Biederman 
205631ab46bSEric W. Biederman 	/* Drop read queue */
206*54f968d6SJason Wang 	skb_queue_purge(&tfile->socket.sk->sk_receive_queue);
207c70f1829SEric W. Biederman 
208c70f1829SEric W. Biederman 	/* Drop the extra count on the net device */
209c70f1829SEric W. Biederman 	dev_put(tun->dev);
210c70f1829SEric W. Biederman }
211c70f1829SEric W. Biederman 
212c70f1829SEric W. Biederman static void tun_detach(struct tun_struct *tun)
213c70f1829SEric W. Biederman {
214c70f1829SEric W. Biederman 	rtnl_lock();
215c70f1829SEric W. Biederman 	__tun_detach(tun);
216c70f1829SEric W. Biederman 	rtnl_unlock();
217631ab46bSEric W. Biederman }
218631ab46bSEric W. Biederman 
219631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
220631ab46bSEric W. Biederman {
221c70f1829SEric W. Biederman 	struct tun_struct *tun = NULL;
222c70f1829SEric W. Biederman 
223c70f1829SEric W. Biederman 	if (atomic_inc_not_zero(&tfile->count))
224c70f1829SEric W. Biederman 		tun = tfile->tun;
225c70f1829SEric W. Biederman 
226c70f1829SEric W. Biederman 	return tun;
227631ab46bSEric W. Biederman }
228631ab46bSEric W. Biederman 
229631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
230631ab46bSEric W. Biederman {
231631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
232631ab46bSEric W. Biederman }
233631ab46bSEric W. Biederman 
234631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
235631ab46bSEric W. Biederman {
236c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
237c70f1829SEric W. Biederman 
238c70f1829SEric W. Biederman 	if (atomic_dec_and_test(&tfile->count))
239c70f1829SEric W. Biederman 		tun_detach(tfile->tun);
240631ab46bSEric W. Biederman }
241631ab46bSEric W. Biederman 
2426b8a66eeSJoe Perches /* TAP filtering */
243f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
244f271b2ccSMax Krasnyansky {
245f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
246f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
247f271b2ccSMax Krasnyansky }
248f271b2ccSMax Krasnyansky 
249f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
250f271b2ccSMax Krasnyansky {
251f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
252f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
253f271b2ccSMax Krasnyansky }
254f271b2ccSMax Krasnyansky 
255f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
256f271b2ccSMax Krasnyansky {
257f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
258f271b2ccSMax Krasnyansky 	struct tun_filter uf;
259f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
260f271b2ccSMax Krasnyansky 
261f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
262f271b2ccSMax Krasnyansky 		return -EFAULT;
263f271b2ccSMax Krasnyansky 
264f271b2ccSMax Krasnyansky 	if (!uf.count) {
265f271b2ccSMax Krasnyansky 		/* Disabled */
266f271b2ccSMax Krasnyansky 		filter->count = 0;
267f271b2ccSMax Krasnyansky 		return 0;
268f271b2ccSMax Krasnyansky 	}
269f271b2ccSMax Krasnyansky 
270f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
271f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
272f271b2ccSMax Krasnyansky 	if (!addr)
273f271b2ccSMax Krasnyansky 		return -ENOMEM;
274f271b2ccSMax Krasnyansky 
275f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
276f271b2ccSMax Krasnyansky 		err = -EFAULT;
277f271b2ccSMax Krasnyansky 		goto done;
278f271b2ccSMax Krasnyansky 	}
279f271b2ccSMax Krasnyansky 
280f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
281f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
282f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
283f271b2ccSMax Krasnyansky 	filter->count = 0;
284f271b2ccSMax Krasnyansky 	wmb();
285f271b2ccSMax Krasnyansky 
286f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
287f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
288f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
289f271b2ccSMax Krasnyansky 
290f271b2ccSMax Krasnyansky 	nexact = n;
291f271b2ccSMax Krasnyansky 
292cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
293cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
294f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
295cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
296cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
297cfbf84fcSAlex Williamson 			err = 0; /* no filter */
298cfbf84fcSAlex Williamson 			goto done;
299cfbf84fcSAlex Williamson 		}
300f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
301cfbf84fcSAlex Williamson 	}
302f271b2ccSMax Krasnyansky 
303f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
304f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
305f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
306f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
307f271b2ccSMax Krasnyansky 
308f271b2ccSMax Krasnyansky 	/* Now enable the filter */
309f271b2ccSMax Krasnyansky 	wmb();
310f271b2ccSMax Krasnyansky 	filter->count = nexact;
311f271b2ccSMax Krasnyansky 
312f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
313f271b2ccSMax Krasnyansky 	err = nexact;
314f271b2ccSMax Krasnyansky 
315f271b2ccSMax Krasnyansky done:
316f271b2ccSMax Krasnyansky 	kfree(addr);
317f271b2ccSMax Krasnyansky 	return err;
318f271b2ccSMax Krasnyansky }
319f271b2ccSMax Krasnyansky 
320f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
321f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
322f271b2ccSMax Krasnyansky {
323f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
324f271b2ccSMax Krasnyansky 	 * at this point. */
325f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
326f271b2ccSMax Krasnyansky 	int i;
327f271b2ccSMax Krasnyansky 
328f271b2ccSMax Krasnyansky 	/* Exact match */
329f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
3302e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
331f271b2ccSMax Krasnyansky 			return 1;
332f271b2ccSMax Krasnyansky 
333f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
334f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
335f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
336f271b2ccSMax Krasnyansky 
337f271b2ccSMax Krasnyansky 	return 0;
338f271b2ccSMax Krasnyansky }
339f271b2ccSMax Krasnyansky 
340f271b2ccSMax Krasnyansky /*
341f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
342f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
343f271b2ccSMax Krasnyansky  */
344f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
345f271b2ccSMax Krasnyansky {
346f271b2ccSMax Krasnyansky 	if (!filter->count)
347f271b2ccSMax Krasnyansky 		return 1;
348f271b2ccSMax Krasnyansky 
349f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
350f271b2ccSMax Krasnyansky }
351f271b2ccSMax Krasnyansky 
3521da177e4SLinus Torvalds /* Network device part of the driver */
3531da177e4SLinus Torvalds 
3547282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
3551da177e4SLinus Torvalds 
356c70f1829SEric W. Biederman /* Net device detach from fd. */
357c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
358c70f1829SEric W. Biederman {
359c70f1829SEric W. Biederman 	struct tun_struct *tun = netdev_priv(dev);
360c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
361c70f1829SEric W. Biederman 
362c70f1829SEric W. Biederman 	/* Inform the methods they need to stop using the dev.
363c70f1829SEric W. Biederman 	 */
364c70f1829SEric W. Biederman 	if (tfile) {
365*54f968d6SJason Wang 		wake_up_all(&tfile->wq.wait);
366c70f1829SEric W. Biederman 		if (atomic_dec_and_test(&tfile->count))
367c70f1829SEric W. Biederman 			__tun_detach(tun);
368c70f1829SEric W. Biederman 	}
369c70f1829SEric W. Biederman }
370c70f1829SEric W. Biederman 
3711da177e4SLinus Torvalds /* Net device open. */
3721da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds 	netif_start_queue(dev);
3751da177e4SLinus Torvalds 	return 0;
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds /* Net device close. */
3791da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	netif_stop_queue(dev);
3821da177e4SLinus Torvalds 	return 0;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /* Net device start xmit */
386424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
389*54f968d6SJason Wang 	struct tun_file *tfile = tun->tfile;
3901da177e4SLinus Torvalds 
3916b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
394*54f968d6SJason Wang 	if (!tfile)
3951da177e4SLinus Torvalds 		goto drop;
3961da177e4SLinus Torvalds 
397f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
398f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
399f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
400f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
401f271b2ccSMax Krasnyansky 		goto drop;
402f271b2ccSMax Krasnyansky 
403*54f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
404*54f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
40599405162SMichael S. Tsirkin 		goto drop;
40699405162SMichael S. Tsirkin 
407*54f968d6SJason Wang 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
408*54f968d6SJason Wang 	    >= dev->tx_queue_len) {
4091da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
4101da177e4SLinus Torvalds 			/* Normal queueing mode. */
4111da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
4121da177e4SLinus Torvalds 			netif_stop_queue(dev);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
4151da177e4SLinus Torvalds 			 * error is more appropriate. */
41609f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
4171da177e4SLinus Torvalds 		} else {
4181da177e4SLinus Torvalds 			/* Single queue mode.
4191da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
4201da177e4SLinus Torvalds 			goto drop;
4211da177e4SLinus Torvalds 		}
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4240110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
4250110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
426868eefebSMichael S. Tsirkin 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
427868eefebSMichael S. Tsirkin 		goto drop;
4280110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
4290110d6f2SMichael S. Tsirkin 
430f271b2ccSMax Krasnyansky 	/* Enqueue packet */
431*54f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	/* Notify and wake up reader process */
434*54f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
435*54f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
436*54f968d6SJason Wang 	wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
43705c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
4386ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds drop:
44109f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
4421da177e4SLinus Torvalds 	kfree_skb(skb);
4436ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
446f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
4471da177e4SLinus Torvalds {
448f271b2ccSMax Krasnyansky 	/*
449f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
450f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
451f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
452f271b2ccSMax Krasnyansky 	 */
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4554885a504SEd Swierk #define MIN_MTU 68
4564885a504SEd Swierk #define MAX_MTU 65535
4574885a504SEd Swierk 
4584885a504SEd Swierk static int
4594885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
4604885a504SEd Swierk {
4614885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
4624885a504SEd Swierk 		return -EINVAL;
4634885a504SEd Swierk 	dev->mtu = new_mtu;
4644885a504SEd Swierk 	return 0;
4654885a504SEd Swierk }
4664885a504SEd Swierk 
467c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
468c8f44affSMichał Mirosław 	netdev_features_t features)
46988255375SMichał Mirosław {
47088255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
47188255375SMichał Mirosław 
47288255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
47388255375SMichał Mirosław }
474bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
475bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
476bebd097aSNeil Horman {
477bebd097aSNeil Horman 	/*
478bebd097aSNeil Horman 	 * Tun only receives frames when:
479bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
480bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
481bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
482bebd097aSNeil Horman 	 * never to have pending data when we poll for it
483bebd097aSNeil Horman 	 * so theres nothing to do here but return.
484bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
485bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
486bebd097aSNeil Horman 	 * still use netconsole
487bebd097aSNeil Horman 	 */
488bebd097aSNeil Horman 	return;
489bebd097aSNeil Horman }
490bebd097aSNeil Horman #endif
491758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
492c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
493758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
494758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
49500829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
496758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
49788255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
498bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
499bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
500bebd097aSNeil Horman #endif
501758e43b7SStephen Hemminger };
502758e43b7SStephen Hemminger 
503758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
504c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
505758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
506758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
50700829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
508758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
50988255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
510afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
511758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
512758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
513bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
514bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
515bebd097aSNeil Horman #endif
516758e43b7SStephen Hemminger };
517758e43b7SStephen Hemminger 
5181da177e4SLinus Torvalds /* Initialize net device. */
5191da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
5241da177e4SLinus Torvalds 	case TUN_TUN_DEV:
525758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
526758e43b7SStephen Hemminger 
5271da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
5281da177e4SLinus Torvalds 		dev->hard_header_len = 0;
5291da177e4SLinus Torvalds 		dev->addr_len = 0;
5301da177e4SLinus Torvalds 		dev->mtu = 1500;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 		/* Zero header length */
5331da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
5341da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
5351da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5361da177e4SLinus Torvalds 		break;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	case TUN_TAP_DEV:
5397a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
5401da177e4SLinus Torvalds 		/* Ethernet TAP Device */
5411da177e4SLinus Torvalds 		ether_setup(dev);
542550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
54336226a8dSBrian Braunstein 
544f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
54536226a8dSBrian Braunstein 
5461da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5471da177e4SLinus Torvalds 		break;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /* Character device part */
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds /* Poll */
5541da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
5551da177e4SLinus Torvalds {
556b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
557b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
5583c8a9c63SMariusz Kozlowski 	struct sock *sk;
55933dccbb0SHerbert Xu 	unsigned int mask = 0;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	if (!tun)
562eac9e902SEric W. Biederman 		return POLLERR;
5631da177e4SLinus Torvalds 
564*54f968d6SJason Wang 	sk = tfile->socket.sk;
5653c8a9c63SMariusz Kozlowski 
5666b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
5671da177e4SLinus Torvalds 
568*54f968d6SJason Wang 	poll_wait(file, &tfile->wq.wait, wait);
5691da177e4SLinus Torvalds 
57089f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
5711da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5721da177e4SLinus Torvalds 
57333dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
57433dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
57533dccbb0SHerbert Xu 	     sock_writeable(sk)))
57633dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
57733dccbb0SHerbert Xu 
578c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
579c70f1829SEric W. Biederman 		mask = POLLERR;
580c70f1829SEric W. Biederman 
581631ab46bSEric W. Biederman 	tun_put(tun);
5821da177e4SLinus Torvalds 	return mask;
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
585f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
586f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
587*54f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
58833dccbb0SHerbert Xu 				     size_t prepad, size_t len,
58933dccbb0SHerbert Xu 				     size_t linear, int noblock)
590f42157cbSRusty Russell {
591*54f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
592f42157cbSRusty Russell 	struct sk_buff *skb;
59333dccbb0SHerbert Xu 	int err;
594f42157cbSRusty Russell 
595f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
5960eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
59733dccbb0SHerbert Xu 		linear = len;
598f42157cbSRusty Russell 
59933dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
60033dccbb0SHerbert Xu 				   &err);
601f42157cbSRusty Russell 	if (!skb)
60233dccbb0SHerbert Xu 		return ERR_PTR(err);
603f42157cbSRusty Russell 
604f42157cbSRusty Russell 	skb_reserve(skb, prepad);
605f42157cbSRusty Russell 	skb_put(skb, linear);
60633dccbb0SHerbert Xu 	skb->data_len = len - linear;
60733dccbb0SHerbert Xu 	skb->len += len - linear;
608f42157cbSRusty Russell 
609f42157cbSRusty Russell 	return skb;
610f42157cbSRusty Russell }
611f42157cbSRusty Russell 
6120690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */
6130690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
6140690899bSMichael S. Tsirkin 				  int offset, size_t count)
6150690899bSMichael S. Tsirkin {
6160690899bSMichael S. Tsirkin 	int len = iov_length(from, count) - offset;
6170690899bSMichael S. Tsirkin 	int copy = skb_headlen(skb);
6180690899bSMichael S. Tsirkin 	int size, offset1 = 0;
6190690899bSMichael S. Tsirkin 	int i = 0;
6200690899bSMichael S. Tsirkin 
6210690899bSMichael S. Tsirkin 	/* Skip over from offset */
6220690899bSMichael S. Tsirkin 	while (count && (offset >= from->iov_len)) {
6230690899bSMichael S. Tsirkin 		offset -= from->iov_len;
6240690899bSMichael S. Tsirkin 		++from;
6250690899bSMichael S. Tsirkin 		--count;
6260690899bSMichael S. Tsirkin 	}
6270690899bSMichael S. Tsirkin 
6280690899bSMichael S. Tsirkin 	/* copy up to skb headlen */
6290690899bSMichael S. Tsirkin 	while (count && (copy > 0)) {
6300690899bSMichael S. Tsirkin 		size = min_t(unsigned int, copy, from->iov_len - offset);
6310690899bSMichael S. Tsirkin 		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
6320690899bSMichael S. Tsirkin 				   size))
6330690899bSMichael S. Tsirkin 			return -EFAULT;
6340690899bSMichael S. Tsirkin 		if (copy > size) {
6350690899bSMichael S. Tsirkin 			++from;
6360690899bSMichael S. Tsirkin 			--count;
6370690899bSMichael S. Tsirkin 			offset = 0;
6380690899bSMichael S. Tsirkin 		} else
6390690899bSMichael S. Tsirkin 			offset += size;
6400690899bSMichael S. Tsirkin 		copy -= size;
6410690899bSMichael S. Tsirkin 		offset1 += size;
6420690899bSMichael S. Tsirkin 	}
6430690899bSMichael S. Tsirkin 
6440690899bSMichael S. Tsirkin 	if (len == offset1)
6450690899bSMichael S. Tsirkin 		return 0;
6460690899bSMichael S. Tsirkin 
6470690899bSMichael S. Tsirkin 	while (count--) {
6480690899bSMichael S. Tsirkin 		struct page *page[MAX_SKB_FRAGS];
6490690899bSMichael S. Tsirkin 		int num_pages;
6500690899bSMichael S. Tsirkin 		unsigned long base;
6510690899bSMichael S. Tsirkin 		unsigned long truesize;
6520690899bSMichael S. Tsirkin 
6530690899bSMichael S. Tsirkin 		len = from->iov_len - offset;
6540690899bSMichael S. Tsirkin 		if (!len) {
6550690899bSMichael S. Tsirkin 			offset = 0;
6560690899bSMichael S. Tsirkin 			++from;
6570690899bSMichael S. Tsirkin 			continue;
6580690899bSMichael S. Tsirkin 		}
6590690899bSMichael S. Tsirkin 		base = (unsigned long)from->iov_base + offset;
6600690899bSMichael S. Tsirkin 		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
6610690899bSMichael S. Tsirkin 		if (i + size > MAX_SKB_FRAGS)
6620690899bSMichael S. Tsirkin 			return -EMSGSIZE;
6630690899bSMichael S. Tsirkin 		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
6640690899bSMichael S. Tsirkin 		if (num_pages != size) {
6650690899bSMichael S. Tsirkin 			for (i = 0; i < num_pages; i++)
6660690899bSMichael S. Tsirkin 				put_page(page[i]);
6670690899bSMichael S. Tsirkin 			return -EFAULT;
6680690899bSMichael S. Tsirkin 		}
6690690899bSMichael S. Tsirkin 		truesize = size * PAGE_SIZE;
6700690899bSMichael S. Tsirkin 		skb->data_len += len;
6710690899bSMichael S. Tsirkin 		skb->len += len;
6720690899bSMichael S. Tsirkin 		skb->truesize += truesize;
6730690899bSMichael S. Tsirkin 		atomic_add(truesize, &skb->sk->sk_wmem_alloc);
6740690899bSMichael S. Tsirkin 		while (len) {
6750690899bSMichael S. Tsirkin 			int off = base & ~PAGE_MASK;
6760690899bSMichael S. Tsirkin 			int size = min_t(int, len, PAGE_SIZE - off);
6770690899bSMichael S. Tsirkin 			__skb_fill_page_desc(skb, i, page[i], off, size);
6780690899bSMichael S. Tsirkin 			skb_shinfo(skb)->nr_frags++;
6790690899bSMichael S. Tsirkin 			/* increase sk_wmem_alloc */
6800690899bSMichael S. Tsirkin 			base += size;
6810690899bSMichael S. Tsirkin 			len -= size;
6820690899bSMichael S. Tsirkin 			i++;
6830690899bSMichael S. Tsirkin 		}
6840690899bSMichael S. Tsirkin 		offset = 0;
6850690899bSMichael S. Tsirkin 		++from;
6860690899bSMichael S. Tsirkin 	}
6870690899bSMichael S. Tsirkin 	return 0;
6880690899bSMichael S. Tsirkin }
6890690899bSMichael S. Tsirkin 
6901da177e4SLinus Torvalds /* Get packet from user space buffer */
691*54f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
692*54f968d6SJason Wang 			    void *msg_control, const struct iovec *iv,
693*54f968d6SJason Wang 			    size_t total_len, size_t count, int noblock)
6941da177e4SLinus Torvalds {
69509640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
6961da177e4SLinus Torvalds 	struct sk_buff *skb;
6970690899bSMichael S. Tsirkin 	size_t len = total_len, align = NET_SKB_PAD;
698f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
6996f26c9a7SMichael S. Tsirkin 	int offset = 0;
7000690899bSMichael S. Tsirkin 	int copylen;
7010690899bSMichael S. Tsirkin 	bool zerocopy = false;
7020690899bSMichael S. Tsirkin 	int err;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
7050690899bSMichael S. Tsirkin 		if ((len -= sizeof(pi)) > total_len)
7061da177e4SLinus Torvalds 			return -EINVAL;
7071da177e4SLinus Torvalds 
7086f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
7091da177e4SLinus Torvalds 			return -EFAULT;
7106f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
7111da177e4SLinus Torvalds 	}
7121da177e4SLinus Torvalds 
713f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
7140690899bSMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) > total_len)
715f43798c2SRusty Russell 			return -EINVAL;
716f43798c2SRusty Russell 
7176f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
718f43798c2SRusty Russell 			return -EFAULT;
719f43798c2SRusty Russell 
7204909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
7214909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
7224909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
7234909122fSHerbert Xu 
724f43798c2SRusty Russell 		if (gso.hdr_len > len)
725f43798c2SRusty Russell 			return -EINVAL;
726d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
727f43798c2SRusty Russell 	}
728f43798c2SRusty Russell 
729e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
730a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
7310eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
7320eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
733e01bf1c8SRusty Russell 			return -EINVAL;
734e01bf1c8SRusty Russell 	}
7351da177e4SLinus Torvalds 
7360690899bSMichael S. Tsirkin 	if (msg_control)
7370690899bSMichael S. Tsirkin 		zerocopy = true;
7380690899bSMichael S. Tsirkin 
7390690899bSMichael S. Tsirkin 	if (zerocopy) {
7400690899bSMichael S. Tsirkin 		/* Userspace may produce vectors with count greater than
7410690899bSMichael S. Tsirkin 		 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
7420690899bSMichael S. Tsirkin 		 * to let the rest of data to be fit in the frags.
7430690899bSMichael S. Tsirkin 		 */
7440690899bSMichael S. Tsirkin 		if (count > MAX_SKB_FRAGS) {
7450690899bSMichael S. Tsirkin 			copylen = iov_length(iv, count - MAX_SKB_FRAGS);
7460690899bSMichael S. Tsirkin 			if (copylen < offset)
7470690899bSMichael S. Tsirkin 				copylen = 0;
7480690899bSMichael S. Tsirkin 			else
7490690899bSMichael S. Tsirkin 				copylen -= offset;
7500690899bSMichael S. Tsirkin 		} else
7510690899bSMichael S. Tsirkin 				copylen = 0;
7520690899bSMichael S. Tsirkin 		/* There are 256 bytes to be copied in skb, so there is enough
7530690899bSMichael S. Tsirkin 		 * room for skb expand head in case it is used.
7540690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
7550690899bSMichael S. Tsirkin 		 */
7560690899bSMichael S. Tsirkin 		if (copylen < gso.hdr_len)
7570690899bSMichael S. Tsirkin 			copylen = gso.hdr_len;
7580690899bSMichael S. Tsirkin 		if (!copylen)
7590690899bSMichael S. Tsirkin 			copylen = GOODCOPY_LEN;
7600690899bSMichael S. Tsirkin 	} else
7610690899bSMichael S. Tsirkin 		copylen = len;
7620690899bSMichael S. Tsirkin 
763*54f968d6SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
76433dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
76533dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
76609f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
76733dccbb0SHerbert Xu 		return PTR_ERR(skb);
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 
7700690899bSMichael S. Tsirkin 	if (zerocopy)
7710690899bSMichael S. Tsirkin 		err = zerocopy_sg_from_iovec(skb, iv, offset, count);
7720690899bSMichael S. Tsirkin 	else
7730690899bSMichael S. Tsirkin 		err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
7740690899bSMichael S. Tsirkin 
7750690899bSMichael S. Tsirkin 	if (err) {
77609f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
7778f22757eSDave Jones 		kfree_skb(skb);
7781da177e4SLinus Torvalds 		return -EFAULT;
7798f22757eSDave Jones 	}
7801da177e4SLinus Torvalds 
781f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
782f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
783f43798c2SRusty Russell 					  gso.csum_offset)) {
784f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
785f43798c2SRusty Russell 			kfree_skb(skb);
786f43798c2SRusty Russell 			return -EINVAL;
787f43798c2SRusty Russell 		}
78888255375SMichał Mirosław 	}
789f43798c2SRusty Russell 
7901da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
7911da177e4SLinus Torvalds 	case TUN_TUN_DEV:
792f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
793f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
794f09f7ee2SAng Way Chuang 			case 0x40:
795f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
796f09f7ee2SAng Way Chuang 				break;
797f09f7ee2SAng Way Chuang 			case 0x60:
798f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
799f09f7ee2SAng Way Chuang 				break;
800f09f7ee2SAng Way Chuang 			default:
801f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
802f09f7ee2SAng Way Chuang 				kfree_skb(skb);
803f09f7ee2SAng Way Chuang 				return -EINVAL;
804f09f7ee2SAng Way Chuang 			}
805f09f7ee2SAng Way Chuang 		}
806f09f7ee2SAng Way Chuang 
807459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
8081da177e4SLinus Torvalds 		skb->protocol = pi.proto;
8094c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
8101da177e4SLinus Torvalds 		break;
8111da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8121da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
8131da177e4SLinus Torvalds 		break;
8146403eab1SJoe Perches 	}
8151da177e4SLinus Torvalds 
816f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
817f43798c2SRusty Russell 		pr_debug("GSO!\n");
818f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
819f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
820f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
821f43798c2SRusty Russell 			break;
822f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
823f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
824f43798c2SRusty Russell 			break;
825e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
826e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
827e36aa25aSSridhar Samudrala 			break;
828f43798c2SRusty Russell 		default:
829f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
830f43798c2SRusty Russell 			kfree_skb(skb);
831f43798c2SRusty Russell 			return -EINVAL;
832f43798c2SRusty Russell 		}
833f43798c2SRusty Russell 
834f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
835f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
836f43798c2SRusty Russell 
837f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
838f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
839f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
840f43798c2SRusty Russell 			kfree_skb(skb);
841f43798c2SRusty Russell 			return -EINVAL;
842f43798c2SRusty Russell 		}
843f43798c2SRusty Russell 
844f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
845f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
846f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
847f43798c2SRusty Russell 	}
8481da177e4SLinus Torvalds 
8490690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
8500690899bSMichael S. Tsirkin 	if (zerocopy) {
8510690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
8520690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
8530690899bSMichael S. Tsirkin 	}
8540690899bSMichael S. Tsirkin 
8551da177e4SLinus Torvalds 	netif_rx_ni(skb);
8561da177e4SLinus Torvalds 
85709f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
85809f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
8591da177e4SLinus Torvalds 
8600690899bSMichael S. Tsirkin 	return total_len;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
863ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
864ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
8651da177e4SLinus Torvalds {
86633dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
867ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
868*54f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
869631ab46bSEric W. Biederman 	ssize_t result;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (!tun)
8721da177e4SLinus Torvalds 		return -EBADFD;
8731da177e4SLinus Torvalds 
8746b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
8751da177e4SLinus Torvalds 
876*54f968d6SJason Wang 	result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
877*54f968d6SJason Wang 			      count, file->f_flags & O_NONBLOCK);
878631ab46bSEric W. Biederman 
879631ab46bSEric W. Biederman 	tun_put(tun);
880631ab46bSEric W. Biederman 	return result;
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds /* Put packet to the user space buffer */
8846f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
885*54f968d6SJason Wang 			    struct tun_file *tfile,
8861da177e4SLinus Torvalds 			    struct sk_buff *skb,
88743b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
8901da177e4SLinus Torvalds 	ssize_t total = 0;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
8931da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
8941da177e4SLinus Torvalds 			return -EINVAL;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 		if (len < skb->len) {
8971da177e4SLinus Torvalds 			/* Packet will be striped */
8981da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
8991da177e4SLinus Torvalds 		}
9001da177e4SLinus Torvalds 
90143b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
9021da177e4SLinus Torvalds 			return -EFAULT;
9031da177e4SLinus Torvalds 		total += sizeof(pi);
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds 
906f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
907f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
908d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
909f43798c2SRusty Russell 			return -EINVAL;
910f43798c2SRusty Russell 
911f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
912f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
913f43798c2SRusty Russell 
914f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
915f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
916f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
917f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
918f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
919f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
920f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
921e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
922e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
923ef3db4a5SMichael S. Tsirkin 			else {
9246b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
925ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
926ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
927ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
928ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
929ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
930ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
931ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
932ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
933ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
934ef3db4a5SMichael S. Tsirkin 			}
935f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
936f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
937f43798c2SRusty Russell 		} else
938f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
939f43798c2SRusty Russell 
940f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
941f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
94255508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
943f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
94410a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
94510a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
946f43798c2SRusty Russell 		} /* else everything is zero */
947f43798c2SRusty Russell 
94843b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
94943b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
950f43798c2SRusty Russell 			return -EFAULT;
951d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
952f43798c2SRusty Russell 	}
953f43798c2SRusty Russell 
9541da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
9551da177e4SLinus Torvalds 
95643b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
95705c2828cSMichael S. Tsirkin 	total += skb->len;
9581da177e4SLinus Torvalds 
95909f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
96009f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	return total;
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds 
965*54f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
96605c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
96705c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
9681da177e4SLinus Torvalds {
9691da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
9701da177e4SLinus Torvalds 	struct sk_buff *skb;
97105c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
9721da177e4SLinus Torvalds 
9736b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_read\n");
9741da177e4SLinus Torvalds 
97561a5ff15SAmos Kong 	if (unlikely(!noblock))
976*54f968d6SJason Wang 		add_wait_queue(&tfile->wq.wait, &wait);
9771da177e4SLinus Torvalds 	while (len) {
9781da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 		/* Read frames from the queue */
981*54f968d6SJason Wang 		if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
98205c2828cSMichael S. Tsirkin 			if (noblock) {
9831da177e4SLinus Torvalds 				ret = -EAGAIN;
9841da177e4SLinus Torvalds 				break;
9851da177e4SLinus Torvalds 			}
9861da177e4SLinus Torvalds 			if (signal_pending(current)) {
9871da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
9881da177e4SLinus Torvalds 				break;
9891da177e4SLinus Torvalds 			}
990c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
991c70f1829SEric W. Biederman 				ret = -EIO;
992c70f1829SEric W. Biederman 				break;
993c70f1829SEric W. Biederman 			}
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
9961da177e4SLinus Torvalds 			schedule();
9971da177e4SLinus Torvalds 			continue;
9981da177e4SLinus Torvalds 		}
9991da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
10001da177e4SLinus Torvalds 
1001*54f968d6SJason Wang 		ret = tun_put_user(tun, tfile, skb, iv, len);
10021da177e4SLinus Torvalds 		kfree_skb(skb);
10031da177e4SLinus Torvalds 		break;
10041da177e4SLinus Torvalds 	}
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
100761a5ff15SAmos Kong 	if (unlikely(!noblock))
1008*54f968d6SJason Wang 		remove_wait_queue(&tfile->wq.wait, &wait);
10091da177e4SLinus Torvalds 
101005c2828cSMichael S. Tsirkin 	return ret;
101105c2828cSMichael S. Tsirkin }
101205c2828cSMichael S. Tsirkin 
101305c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
101405c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
101505c2828cSMichael S. Tsirkin {
101605c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
101705c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
101805c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
101905c2828cSMichael S. Tsirkin 	ssize_t len, ret;
102005c2828cSMichael S. Tsirkin 
102105c2828cSMichael S. Tsirkin 	if (!tun)
102205c2828cSMichael S. Tsirkin 		return -EBADFD;
102305c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
102405c2828cSMichael S. Tsirkin 	if (len < 0) {
102505c2828cSMichael S. Tsirkin 		ret = -EINVAL;
102605c2828cSMichael S. Tsirkin 		goto out;
102705c2828cSMichael S. Tsirkin 	}
102805c2828cSMichael S. Tsirkin 
1029*54f968d6SJason Wang 	ret = tun_do_read(tun, tfile, iocb, iv, len,
1030*54f968d6SJason Wang 			  file->f_flags & O_NONBLOCK);
103105c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
1032631ab46bSEric W. Biederman out:
1033631ab46bSEric W. Biederman 	tun_put(tun);
10341da177e4SLinus Torvalds 	return ret;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
10401da177e4SLinus Torvalds 
10410625c883SEric W. Biederman 	tun->owner = INVALID_UID;
10420625c883SEric W. Biederman 	tun->group = INVALID_GID;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
1045*54f968d6SJason Wang 	dev->destructor = free_netdev;
10461da177e4SLinus Torvalds }
10471da177e4SLinus Torvalds 
1048f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1049f019a7a5SEric W. Biederman  * device with netlink.
1050f019a7a5SEric W. Biederman  */
1051f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1052f019a7a5SEric W. Biederman {
1053f019a7a5SEric W. Biederman 	return -EINVAL;
1054f019a7a5SEric W. Biederman }
1055f019a7a5SEric W. Biederman 
1056f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1057f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1058f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1059f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1060f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1061f019a7a5SEric W. Biederman };
1062f019a7a5SEric W. Biederman 
106333dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
106433dccbb0SHerbert Xu {
1065*54f968d6SJason Wang 	struct tun_file *tfile;
106643815482SEric Dumazet 	wait_queue_head_t *wqueue;
106733dccbb0SHerbert Xu 
106833dccbb0SHerbert Xu 	if (!sock_writeable(sk))
106933dccbb0SHerbert Xu 		return;
107033dccbb0SHerbert Xu 
107133dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
107233dccbb0SHerbert Xu 		return;
107333dccbb0SHerbert Xu 
107443815482SEric Dumazet 	wqueue = sk_sleep(sk);
107543815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
107643815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
107705c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1078c722c625SHerbert Xu 
1079*54f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
1080*54f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
108133dccbb0SHerbert Xu }
108233dccbb0SHerbert Xu 
108305c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
108405c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
108505c2828cSMichael S. Tsirkin {
1086*54f968d6SJason Wang 	int ret;
1087*54f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1088*54f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
1089*54f968d6SJason Wang 
1090*54f968d6SJason Wang 	if (!tun)
1091*54f968d6SJason Wang 		return -EBADFD;
1092*54f968d6SJason Wang 
1093*54f968d6SJason Wang 	ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
10940690899bSMichael S. Tsirkin 			   m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1095*54f968d6SJason Wang 	tun_put(tun);
1096*54f968d6SJason Wang 	return ret;
109705c2828cSMichael S. Tsirkin }
109805c2828cSMichael S. Tsirkin 
1099*54f968d6SJason Wang 
110005c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
110105c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
110205c2828cSMichael S. Tsirkin 		       int flags)
110305c2828cSMichael S. Tsirkin {
1104*54f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1105*54f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
110605c2828cSMichael S. Tsirkin 	int ret;
1107*54f968d6SJason Wang 
1108*54f968d6SJason Wang 	if (!tun)
1109*54f968d6SJason Wang 		return -EBADFD;
1110*54f968d6SJason Wang 
111105c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
111205c2828cSMichael S. Tsirkin 		return -EINVAL;
1113*54f968d6SJason Wang 	ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
111405c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
111505c2828cSMichael S. Tsirkin 	if (ret > total_len) {
111605c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
111705c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
111805c2828cSMichael S. Tsirkin 	}
1119*54f968d6SJason Wang 	tun_put(tun);
112005c2828cSMichael S. Tsirkin 	return ret;
112105c2828cSMichael S. Tsirkin }
112205c2828cSMichael S. Tsirkin 
11231ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
11241ab5ecb9SStanislav Kinsbursky {
11251ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
11261ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
11271ab5ecb9SStanislav Kinsbursky 	return 0;
11281ab5ecb9SStanislav Kinsbursky }
11291ab5ecb9SStanislav Kinsbursky 
113005c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
113105c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
113205c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
113305c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
11341ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
113505c2828cSMichael S. Tsirkin };
113605c2828cSMichael S. Tsirkin 
113733dccbb0SHerbert Xu static struct proto tun_proto = {
113833dccbb0SHerbert Xu 	.name		= "tun",
113933dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
1140*54f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
114133dccbb0SHerbert Xu };
1142f019a7a5SEric W. Biederman 
1143980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1144980c9e8cSDavid Woodhouse {
1145980c9e8cSDavid Woodhouse 	int flags = 0;
1146980c9e8cSDavid Woodhouse 
1147980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1148980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1149980c9e8cSDavid Woodhouse 	else
1150980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1151980c9e8cSDavid Woodhouse 
1152980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1153980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1154980c9e8cSDavid Woodhouse 
1155980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1156980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1157980c9e8cSDavid Woodhouse 
1158980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1159980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1160980c9e8cSDavid Woodhouse 
1161980c9e8cSDavid Woodhouse 	return flags;
1162980c9e8cSDavid Woodhouse }
1163980c9e8cSDavid Woodhouse 
1164980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1165980c9e8cSDavid Woodhouse 			      char *buf)
1166980c9e8cSDavid Woodhouse {
1167980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1168980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1169980c9e8cSDavid Woodhouse }
1170980c9e8cSDavid Woodhouse 
1171980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1172980c9e8cSDavid Woodhouse 			      char *buf)
1173980c9e8cSDavid Woodhouse {
1174980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
11750625c883SEric W. Biederman 	return uid_valid(tun->owner)?
11760625c883SEric W. Biederman 		sprintf(buf, "%u\n",
11770625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
11780625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1179980c9e8cSDavid Woodhouse }
1180980c9e8cSDavid Woodhouse 
1181980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1182980c9e8cSDavid Woodhouse 			      char *buf)
1183980c9e8cSDavid Woodhouse {
1184980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
11850625c883SEric W. Biederman 	return gid_valid(tun->group) ?
11860625c883SEric W. Biederman 		sprintf(buf, "%u\n",
11870625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
11880625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1189980c9e8cSDavid Woodhouse }
1190980c9e8cSDavid Woodhouse 
1191980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1192980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1193980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1194980c9e8cSDavid Woodhouse 
1195d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
11961da177e4SLinus Torvalds {
11971da177e4SLinus Torvalds 	struct tun_struct *tun;
1198*54f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
11991da177e4SLinus Torvalds 	struct net_device *dev;
12001da177e4SLinus Torvalds 	int err;
12011da177e4SLinus Torvalds 
120274a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
120374a3e5a7SEric W. Biederman 	if (dev) {
12042b980dbdSPaul Moore 		const struct cred *cred = current_cred();
12052b980dbdSPaul Moore 
1206f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1207f85ba780SDavid Woodhouse 			return -EBUSY;
120874a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
120974a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
121074a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
121174a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
121274a3e5a7SEric W. Biederman 		else
121374a3e5a7SEric W. Biederman 			return -EINVAL;
121474a3e5a7SEric W. Biederman 
12150625c883SEric W. Biederman 		if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
12160625c883SEric W. Biederman 		     (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
12172b980dbdSPaul Moore 		    !capable(CAP_NET_ADMIN))
12182b980dbdSPaul Moore 			return -EPERM;
1219*54f968d6SJason Wang 		err = security_tun_dev_attach(tfile->socket.sk);
12202b980dbdSPaul Moore 		if (err < 0)
12212b980dbdSPaul Moore 			return err;
12222b980dbdSPaul Moore 
1223a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1224a7385ba2SEric W. Biederman 		if (err < 0)
1225a7385ba2SEric W. Biederman 			return err;
122686a264abSDavid Howells 	}
12271da177e4SLinus Torvalds 	else {
12281da177e4SLinus Torvalds 		char *name;
12291da177e4SLinus Torvalds 		unsigned long flags = 0;
12301da177e4SLinus Torvalds 
1231ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
1232ca6bb5d7SDavid Woodhouse 			return -EPERM;
12332b980dbdSPaul Moore 		err = security_tun_dev_create();
12342b980dbdSPaul Moore 		if (err < 0)
12352b980dbdSPaul Moore 			return err;
1236ca6bb5d7SDavid Woodhouse 
12371da177e4SLinus Torvalds 		/* Set dev type */
12381da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
12391da177e4SLinus Torvalds 			/* TUN device */
12401da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
12411da177e4SLinus Torvalds 			name = "tun%d";
12421da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
12431da177e4SLinus Torvalds 			/* TAP device */
12441da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
12451da177e4SLinus Torvalds 			name = "tap%d";
12461da177e4SLinus Torvalds 		} else
124736989b90SKusanagi Kouichi 			return -EINVAL;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 		if (*ifr->ifr_name)
12501da177e4SLinus Torvalds 			name = ifr->ifr_name;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
12531da177e4SLinus Torvalds 				   tun_setup);
12541da177e4SLinus Torvalds 		if (!dev)
12551da177e4SLinus Torvalds 			return -ENOMEM;
12561da177e4SLinus Torvalds 
1257fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1258f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1259758e43b7SStephen Hemminger 
12601da177e4SLinus Torvalds 		tun = netdev_priv(dev);
12611da177e4SLinus Torvalds 		tun->dev = dev;
12621da177e4SLinus Torvalds 		tun->flags = flags;
1263f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1264d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
12651da177e4SLinus Torvalds 
1266*54f968d6SJason Wang 		tun->filter_attached = false;
1267*54f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
126833dccbb0SHerbert Xu 
1269*54f968d6SJason Wang 		security_tun_dev_post_create(&tfile->sk);
12702b980dbdSPaul Moore 
12711da177e4SLinus Torvalds 		tun_net_init(dev);
12721da177e4SLinus Torvalds 
127388255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
127488255375SMichał Mirosław 			TUN_USER_FEATURES;
127588255375SMichał Mirosław 		dev->features = dev->hw_features;
127688255375SMichał Mirosław 
12771da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
12781da177e4SLinus Torvalds 		if (err < 0)
1279*54f968d6SJason Wang 			goto err_free_dev;
12809c3fea6aSHerbert Xu 
1281980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1282980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1283980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
12846b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1285980c9e8cSDavid Woodhouse 
1286a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1287a7385ba2SEric W. Biederman 		if (err < 0)
12889c3fea6aSHerbert Xu 			goto failed;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
12916b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
12941da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1295a26af1e0SNathaniel Filardo 	else
1296a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
12991da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1300a26af1e0SNathaniel Filardo 	else
1301a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
13021da177e4SLinus Torvalds 
1303f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1304f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1305f43798c2SRusty Russell 	else
1306f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1307f43798c2SRusty Russell 
1308e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1309e35259a9SMax Krasnyansky 	 * xoff state.
1310e35259a9SMax Krasnyansky 	 */
1311e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1312e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
1313e35259a9SMax Krasnyansky 
13141da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
13151da177e4SLinus Torvalds 	return 0;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds  err_free_dev:
13181da177e4SLinus Torvalds 	free_netdev(dev);
13191da177e4SLinus Torvalds  failed:
13201da177e4SLinus Torvalds 	return err;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
1323876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun,
1324876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1325e3b99556SMark McLoughlin {
13266b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1327e3b99556SMark McLoughlin 
1328e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1329e3b99556SMark McLoughlin 
1330980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1331e3b99556SMark McLoughlin 
1332e3b99556SMark McLoughlin 	return 0;
1333e3b99556SMark McLoughlin }
1334e3b99556SMark McLoughlin 
13355228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
13365228ddc9SRusty Russell  * privs required. */
133788255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
13385228ddc9SRusty Russell {
1339c8f44affSMichał Mirosław 	netdev_features_t features = 0;
13405228ddc9SRusty Russell 
13415228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
134288255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
13435228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
13445228ddc9SRusty Russell 
13455228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
13465228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
13475228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
13485228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
13495228ddc9SRusty Russell 			}
13505228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
13515228ddc9SRusty Russell 				features |= NETIF_F_TSO;
13525228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
13535228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
13545228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
13555228ddc9SRusty Russell 		}
1356e36aa25aSSridhar Samudrala 
1357e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1358e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1359e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1360e36aa25aSSridhar Samudrala 		}
13615228ddc9SRusty Russell 	}
13625228ddc9SRusty Russell 
13635228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
13645228ddc9SRusty Russell 	 * trying to set them. */
13655228ddc9SRusty Russell 	if (arg)
13665228ddc9SRusty Russell 		return -EINVAL;
13675228ddc9SRusty Russell 
136888255375SMichał Mirosław 	tun->set_features = features;
136988255375SMichał Mirosław 	netdev_update_features(tun->dev);
13705228ddc9SRusty Russell 
13715228ddc9SRusty Russell 	return 0;
13725228ddc9SRusty Russell }
13735228ddc9SRusty Russell 
137450857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
137550857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
13761da177e4SLinus Torvalds {
137736b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1378631ab46bSEric W. Biederman 	struct tun_struct *tun;
13791da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
13801da177e4SLinus Torvalds 	struct ifreq ifr;
13810625c883SEric W. Biederman 	kuid_t owner;
13820625c883SEric W. Biederman 	kgid_t group;
138333dccbb0SHerbert Xu 	int sndbuf;
1384d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1385f271b2ccSMax Krasnyansky 	int ret;
13861da177e4SLinus Torvalds 
1387a117dacdSMathias Krause 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
138850857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
13891da177e4SLinus Torvalds 			return -EFAULT;
13908bbb1813SDavid S. Miller 	} else {
1391a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
13928bbb1813SDavid S. Miller 	}
1393631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1394631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1395631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1396631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1397631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1398631ab46bSEric W. Biederman 				IFF_VNET_HDR,
1399631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1400631ab46bSEric W. Biederman 	}
1401631ab46bSEric W. Biederman 
1402876bfd4dSHerbert Xu 	rtnl_lock();
1403876bfd4dSHerbert Xu 
140436b50babSEric W. Biederman 	tun = __tun_get(tfile);
14051da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
14061da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
14071da177e4SLinus Torvalds 
1408876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
14091da177e4SLinus Torvalds 
1410876bfd4dSHerbert Xu 		if (ret)
1411876bfd4dSHerbert Xu 			goto unlock;
14121da177e4SLinus Torvalds 
141350857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1414876bfd4dSHerbert Xu 			ret = -EFAULT;
1415876bfd4dSHerbert Xu 		goto unlock;
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds 
1418876bfd4dSHerbert Xu 	ret = -EBADFD;
14191da177e4SLinus Torvalds 	if (!tun)
1420876bfd4dSHerbert Xu 		goto unlock;
14211da177e4SLinus Torvalds 
14221e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
14231da177e4SLinus Torvalds 
1424631ab46bSEric W. Biederman 	ret = 0;
14251da177e4SLinus Torvalds 	switch (cmd) {
1426e3b99556SMark McLoughlin 	case TUNGETIFF:
1427876bfd4dSHerbert Xu 		ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1428e3b99556SMark McLoughlin 		if (ret)
1429631ab46bSEric W. Biederman 			break;
1430e3b99556SMark McLoughlin 
143150857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1432631ab46bSEric W. Biederman 			ret = -EFAULT;
1433e3b99556SMark McLoughlin 		break;
1434e3b99556SMark McLoughlin 
14351da177e4SLinus Torvalds 	case TUNSETNOCSUM:
14361da177e4SLinus Torvalds 		/* Disable/Enable checksum */
14371da177e4SLinus Torvalds 
143888255375SMichał Mirosław 		/* [unimplemented] */
143988255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
14406b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
14411da177e4SLinus Torvalds 		break;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	case TUNSETPERSIST:
1444*54f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
1445*54f968d6SJason Wang 		 * module to prevent the module being unprobed.
1446*54f968d6SJason Wang 		 */
1447*54f968d6SJason Wang 		if (arg) {
14481da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
1449*54f968d6SJason Wang 			__module_get(THIS_MODULE);
1450*54f968d6SJason Wang 		} else {
14511da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
1452*54f968d6SJason Wang 			module_put(THIS_MODULE);
1453*54f968d6SJason Wang 		}
14541da177e4SLinus Torvalds 
14556b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
14566b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
14571da177e4SLinus Torvalds 		break;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	case TUNSETOWNER:
14601da177e4SLinus Torvalds 		/* Set owner of the device */
14610625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
14620625c883SEric W. Biederman 		if (!uid_valid(owner)) {
14630625c883SEric W. Biederman 			ret = -EINVAL;
14640625c883SEric W. Biederman 			break;
14650625c883SEric W. Biederman 		}
14660625c883SEric W. Biederman 		tun->owner = owner;
14671e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
14680625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
14691da177e4SLinus Torvalds 		break;
14701da177e4SLinus Torvalds 
14718c644623SGuido Guenther 	case TUNSETGROUP:
14728c644623SGuido Guenther 		/* Set group of the device */
14730625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
14740625c883SEric W. Biederman 		if (!gid_valid(group)) {
14750625c883SEric W. Biederman 			ret = -EINVAL;
14760625c883SEric W. Biederman 			break;
14770625c883SEric W. Biederman 		}
14780625c883SEric W. Biederman 		tun->group = group;
14791e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
14800625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
14818c644623SGuido Guenther 		break;
14828c644623SGuido Guenther 
1483ff4cc3acSMike Kershaw 	case TUNSETLINK:
1484ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1485ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
14866b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
14876b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
148848abfe05SDavid S. Miller 			ret = -EBUSY;
1489ff4cc3acSMike Kershaw 		} else {
1490ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
14916b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
14926b8a66eeSJoe Perches 				  tun->dev->type);
149348abfe05SDavid S. Miller 			ret = 0;
1494ff4cc3acSMike Kershaw 		}
1495631ab46bSEric W. Biederman 		break;
1496ff4cc3acSMike Kershaw 
14971da177e4SLinus Torvalds #ifdef TUN_DEBUG
14981da177e4SLinus Torvalds 	case TUNSETDEBUG:
14991da177e4SLinus Torvalds 		tun->debug = arg;
15001da177e4SLinus Torvalds 		break;
15011da177e4SLinus Torvalds #endif
15025228ddc9SRusty Russell 	case TUNSETOFFLOAD:
150388255375SMichał Mirosław 		ret = set_offload(tun, arg);
1504631ab46bSEric W. Biederman 		break;
15055228ddc9SRusty Russell 
1506f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1507f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1508631ab46bSEric W. Biederman 		ret = -EINVAL;
1509f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1510631ab46bSEric W. Biederman 			break;
1511c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1512631ab46bSEric W. Biederman 		break;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1515b595076aSUwe Kleine-König 		/* Get hw address */
1516f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1517f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
151850857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1519631ab46bSEric W. Biederman 			ret = -EFAULT;
1520631ab46bSEric W. Biederman 		break;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1523f271b2ccSMax Krasnyansky 		/* Set hw address */
15246b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
15256b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
152640102371SKim B. Heino 
152740102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1528631ab46bSEric W. Biederman 		break;
152933dccbb0SHerbert Xu 
153033dccbb0SHerbert Xu 	case TUNGETSNDBUF:
1531*54f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
153233dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
153333dccbb0SHerbert Xu 			ret = -EFAULT;
153433dccbb0SHerbert Xu 		break;
153533dccbb0SHerbert Xu 
153633dccbb0SHerbert Xu 	case TUNSETSNDBUF:
153733dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
153833dccbb0SHerbert Xu 			ret = -EFAULT;
153933dccbb0SHerbert Xu 			break;
154033dccbb0SHerbert Xu 		}
154133dccbb0SHerbert Xu 
1542*54f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf = sndbuf;
154333dccbb0SHerbert Xu 		break;
154433dccbb0SHerbert Xu 
1545d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
1546d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
1547d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1548d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1549d9d52b51SMichael S. Tsirkin 		break;
1550d9d52b51SMichael S. Tsirkin 
1551d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
1552d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1553d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1554d9d52b51SMichael S. Tsirkin 			break;
1555d9d52b51SMichael S. Tsirkin 		}
1556d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1557d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
1558d9d52b51SMichael S. Tsirkin 			break;
1559d9d52b51SMichael S. Tsirkin 		}
1560d9d52b51SMichael S. Tsirkin 
1561d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
1562d9d52b51SMichael S. Tsirkin 		break;
1563d9d52b51SMichael S. Tsirkin 
156499405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
156599405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
156699405162SMichael S. Tsirkin 		ret = -EINVAL;
156799405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
156899405162SMichael S. Tsirkin 			break;
156999405162SMichael S. Tsirkin 		ret = -EFAULT;
1570*54f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
157199405162SMichael S. Tsirkin 			break;
157299405162SMichael S. Tsirkin 
1573*54f968d6SJason Wang 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1574*54f968d6SJason Wang 		if (!ret)
1575*54f968d6SJason Wang 			tun->filter_attached = true;
157699405162SMichael S. Tsirkin 		break;
157799405162SMichael S. Tsirkin 
157899405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
157999405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
158099405162SMichael S. Tsirkin 		ret = -EINVAL;
158199405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
158299405162SMichael S. Tsirkin 			break;
1583*54f968d6SJason Wang 		ret = sk_detach_filter(tfile->socket.sk);
1584*54f968d6SJason Wang 		if (!ret)
1585*54f968d6SJason Wang 			tun->filter_attached = false;
158699405162SMichael S. Tsirkin 		break;
158799405162SMichael S. Tsirkin 
15881da177e4SLinus Torvalds 	default:
1589631ab46bSEric W. Biederman 		ret = -EINVAL;
1590631ab46bSEric W. Biederman 		break;
1591ee289b64SJoe Perches 	}
15921da177e4SLinus Torvalds 
1593876bfd4dSHerbert Xu unlock:
1594876bfd4dSHerbert Xu 	rtnl_unlock();
1595876bfd4dSHerbert Xu 	if (tun)
1596631ab46bSEric W. Biederman 		tun_put(tun);
1597631ab46bSEric W. Biederman 	return ret;
15981da177e4SLinus Torvalds }
15991da177e4SLinus Torvalds 
160050857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
160150857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
160250857e2aSArnd Bergmann {
160350857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
160450857e2aSArnd Bergmann }
160550857e2aSArnd Bergmann 
160650857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
160750857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
160850857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
160950857e2aSArnd Bergmann {
161050857e2aSArnd Bergmann 	switch (cmd) {
161150857e2aSArnd Bergmann 	case TUNSETIFF:
161250857e2aSArnd Bergmann 	case TUNGETIFF:
161350857e2aSArnd Bergmann 	case TUNSETTXFILTER:
161450857e2aSArnd Bergmann 	case TUNGETSNDBUF:
161550857e2aSArnd Bergmann 	case TUNSETSNDBUF:
161650857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
161750857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
161850857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
161950857e2aSArnd Bergmann 		break;
162050857e2aSArnd Bergmann 	default:
162150857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
162250857e2aSArnd Bergmann 		break;
162350857e2aSArnd Bergmann 	}
162450857e2aSArnd Bergmann 
162550857e2aSArnd Bergmann 	/*
162650857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
162750857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
162850857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
162950857e2aSArnd Bergmann 	 * contents.
163050857e2aSArnd Bergmann 	 */
163150857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
163250857e2aSArnd Bergmann }
163350857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
163450857e2aSArnd Bergmann 
16351da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
16361da177e4SLinus Torvalds {
1637*54f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
16381da177e4SLinus Torvalds 	int ret;
16391da177e4SLinus Torvalds 
1640*54f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
16419d319522SJonathan Corbet 		goto out;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	if (on) {
1644609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
16451da177e4SLinus Torvalds 		if (ret)
16469d319522SJonathan Corbet 			goto out;
1647*54f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
16481da177e4SLinus Torvalds 	} else
1649*54f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
16509d319522SJonathan Corbet 	ret = 0;
16519d319522SJonathan Corbet out:
16529d319522SJonathan Corbet 	return ret;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
16561da177e4SLinus Torvalds {
1657631ab46bSEric W. Biederman 	struct tun_file *tfile;
1658deed49fbSThomas Gleixner 
16596b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1660631ab46bSEric W. Biederman 
1661*54f968d6SJason Wang 	tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1662*54f968d6SJason Wang 					    &tun_proto);
1663631ab46bSEric W. Biederman 	if (!tfile)
1664631ab46bSEric W. Biederman 		return -ENOMEM;
1665c70f1829SEric W. Biederman 	atomic_set(&tfile->count, 0);
1666631ab46bSEric W. Biederman 	tfile->tun = NULL;
166736b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1668*54f968d6SJason Wang 	tfile->flags = 0;
1669*54f968d6SJason Wang 
1670*54f968d6SJason Wang 	rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1671*54f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
1672*54f968d6SJason Wang 
1673*54f968d6SJason Wang 	tfile->socket.file = file;
1674*54f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
1675*54f968d6SJason Wang 
1676*54f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
1677*54f968d6SJason Wang 	sk_change_net(&tfile->sk, tfile->net);
1678*54f968d6SJason Wang 
1679*54f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
1680*54f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
1681*54f968d6SJason Wang 
1682631ab46bSEric W. Biederman 	file->private_data = tfile;
1683*54f968d6SJason Wang 	set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1684*54f968d6SJason Wang 
16851da177e4SLinus Torvalds 	return 0;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
16891da177e4SLinus Torvalds {
1690631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1691f0a4d0e5SEric W. Biederman 	struct tun_struct *tun;
1692*54f968d6SJason Wang 	struct net *net = tfile->net;
16931da177e4SLinus Torvalds 
1694f0a4d0e5SEric W. Biederman 	tun = __tun_get(tfile);
1695631ab46bSEric W. Biederman 	if (tun) {
1696d23e4365SHerbert Xu 		struct net_device *dev = tun->dev;
1697d23e4365SHerbert Xu 
16986b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "tun_chr_close\n");
16991da177e4SLinus Torvalds 
1700631ab46bSEric W. Biederman 		__tun_detach(tun);
17011da177e4SLinus Torvalds 
17023ad2f3fbSDaniel Mack 		/* If desirable, unregister the netdevice. */
1703d23e4365SHerbert Xu 		if (!(tun->flags & TUN_PERSIST)) {
1704d23e4365SHerbert Xu 			rtnl_lock();
1705d23e4365SHerbert Xu 			if (dev->reg_state == NETREG_REGISTERED)
1706d23e4365SHerbert Xu 				unregister_netdevice(dev);
1707f0a4d0e5SEric W. Biederman 			rtnl_unlock();
1708d23e4365SHerbert Xu 		}
1709*54f968d6SJason Wang 
1710*54f968d6SJason Wang 		/* drop the reference that netdevice holds */
1711*54f968d6SJason Wang 		sock_put(&tfile->sk);
1712d23e4365SHerbert Xu 	}
1713631ab46bSEric W. Biederman 
1714*54f968d6SJason Wang 	/* drop the reference that file holds */
1715*54f968d6SJason Wang 	BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
1716*54f968d6SJason Wang 			 &tfile->socket.flags));
1717*54f968d6SJason Wang 	sk_release_kernel(&tfile->sk);
1718*54f968d6SJason Wang 	put_net(net);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	return 0;
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds 
1723d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
17241da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
17251da177e4SLinus Torvalds 	.llseek = no_llseek,
1726ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1727ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1728ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1729ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
17301da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
1731876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
173250857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
173350857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
173450857e2aSArnd Bergmann #endif
17351da177e4SLinus Torvalds 	.open	= tun_chr_open,
17361da177e4SLinus Torvalds 	.release = tun_chr_close,
17371da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
17381da177e4SLinus Torvalds };
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
17411da177e4SLinus Torvalds 	.minor = TUN_MINOR,
17421da177e4SLinus Torvalds 	.name = "tun",
1743e454cea2SKay Sievers 	.nodename = "net/tun",
17441da177e4SLinus Torvalds 	.fops = &tun_fops,
17451da177e4SLinus Torvalds };
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds /* ethtool interface */
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
17501da177e4SLinus Torvalds {
17511da177e4SLinus Torvalds 	cmd->supported		= 0;
17521da177e4SLinus Torvalds 	cmd->advertising	= 0;
175370739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
17541da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
17551da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
17561da177e4SLinus Torvalds 	cmd->phy_address	= 0;
17571da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
17581da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
17591da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
17601da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
17611da177e4SLinus Torvalds 	return 0;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
17651da177e4SLinus Torvalds {
17661da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17671da177e4SLinus Torvalds 
176833a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
176933a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
17721da177e4SLinus Torvalds 	case TUN_TUN_DEV:
177333a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
17741da177e4SLinus Torvalds 		break;
17751da177e4SLinus Torvalds 	case TUN_TAP_DEV:
177633a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
17771da177e4SLinus Torvalds 		break;
17781da177e4SLinus Torvalds 	}
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
17821da177e4SLinus Torvalds {
17831da177e4SLinus Torvalds #ifdef TUN_DEBUG
17841da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17851da177e4SLinus Torvalds 	return tun->debug;
17861da177e4SLinus Torvalds #else
17871da177e4SLinus Torvalds 	return -EOPNOTSUPP;
17881da177e4SLinus Torvalds #endif
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
17921da177e4SLinus Torvalds {
17931da177e4SLinus Torvalds #ifdef TUN_DEBUG
17941da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
17951da177e4SLinus Torvalds 	tun->debug = value;
17961da177e4SLinus Torvalds #endif
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
17997282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
18001da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
18011da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
18021da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
18031da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
1804bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
18051da177e4SLinus Torvalds };
18061da177e4SLinus Torvalds 
180779d17604SPavel Emelyanov 
18081da177e4SLinus Torvalds static int __init tun_init(void)
18091da177e4SLinus Torvalds {
18101da177e4SLinus Torvalds 	int ret = 0;
18111da177e4SLinus Torvalds 
18126b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
18136b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
18141da177e4SLinus Torvalds 
1815f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
181679d17604SPavel Emelyanov 	if (ret) {
18176b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
1818f019a7a5SEric W. Biederman 		goto err_linkops;
181979d17604SPavel Emelyanov 	}
182079d17604SPavel Emelyanov 
18211da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
182279d17604SPavel Emelyanov 	if (ret) {
18236b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
182479d17604SPavel Emelyanov 		goto err_misc;
182579d17604SPavel Emelyanov 	}
182679d17604SPavel Emelyanov 	return  0;
182779d17604SPavel Emelyanov err_misc:
1828f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
1829f019a7a5SEric W. Biederman err_linkops:
18301da177e4SLinus Torvalds 	return ret;
18311da177e4SLinus Torvalds }
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds static void tun_cleanup(void)
18341da177e4SLinus Torvalds {
18351da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
1836f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
18371da177e4SLinus Torvalds }
18381da177e4SLinus Torvalds 
183905c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
184005c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
184105c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
184205c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
184305c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
184405c2828cSMichael S. Tsirkin {
184505c2828cSMichael S. Tsirkin 	struct tun_struct *tun;
1846*54f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
184705c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
184805c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
184905c2828cSMichael S. Tsirkin 	tun = tun_get(file);
185005c2828cSMichael S. Tsirkin 	if (!tun)
185105c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
185205c2828cSMichael S. Tsirkin 	tun_put(tun);
1853*54f968d6SJason Wang 	return &tfile->socket;
185405c2828cSMichael S. Tsirkin }
185505c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
185605c2828cSMichael S. Tsirkin 
18571da177e4SLinus Torvalds module_init(tun_init);
18581da177e4SLinus Torvalds module_exit(tun_cleanup);
18591da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
18601da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
18611da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18621da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1863578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
1864