xref: /freebsd/sys/net/if_tuntap.c (revision ef9ffb8594eee294334ced627755bf5b46b48f9f)
1251a32b5SKyle Evans /*	$NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $	*/
2251a32b5SKyle Evans /*-
34d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
4251a32b5SKyle Evans  *
5251a32b5SKyle Evans  * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
6251a32b5SKyle Evans  * All rights reserved.
7251a32b5SKyle Evans  * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
8251a32b5SKyle Evans  *
9251a32b5SKyle Evans  * Redistribution and use in source and binary forms, with or without
10251a32b5SKyle Evans  * modification, are permitted provided that the following conditions
11251a32b5SKyle Evans  * are met:
12251a32b5SKyle Evans  *
13251a32b5SKyle Evans  * 1. Redistributions of source code must retain the above copyright
14251a32b5SKyle Evans  *    notice, this list of conditions and the following disclaimer.
15251a32b5SKyle Evans  * 2. Redistributions in binary form must reproduce the above copyright
16251a32b5SKyle Evans  *    notice, this list of conditions and the following disclaimer in the
17251a32b5SKyle Evans  *    documentation and/or other materials provided with the distribution.
18251a32b5SKyle Evans  *
19251a32b5SKyle Evans  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20251a32b5SKyle Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21251a32b5SKyle Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22251a32b5SKyle Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23251a32b5SKyle Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24251a32b5SKyle Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25251a32b5SKyle Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26251a32b5SKyle Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27251a32b5SKyle Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28251a32b5SKyle Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29251a32b5SKyle Evans  * SUCH DAMAGE.
30251a32b5SKyle Evans  *
31251a32b5SKyle Evans  * BASED ON:
32251a32b5SKyle Evans  * -------------------------------------------------------------------------
33251a32b5SKyle Evans  *
34251a32b5SKyle Evans  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
35251a32b5SKyle Evans  * Nottingham University 1987.
36251a32b5SKyle Evans  *
37251a32b5SKyle Evans  * This source may be freely distributed, however I would be interested
38251a32b5SKyle Evans  * in any changes that are made.
39251a32b5SKyle Evans  *
40251a32b5SKyle Evans  * This driver takes packets off the IP i/f and hands them up to a
41251a32b5SKyle Evans  * user process to have its wicked way with. This driver has it's
42251a32b5SKyle Evans  * roots in a similar driver written by Phil Cockcroft (formerly) at
43251a32b5SKyle Evans  * UCL. This driver is based much more on read/write/poll mode of
44251a32b5SKyle Evans  * operation though.
45251a32b5SKyle Evans  */
46251a32b5SKyle Evans 
47251a32b5SKyle Evans #include "opt_inet.h"
48251a32b5SKyle Evans #include "opt_inet6.h"
49251a32b5SKyle Evans 
50251a32b5SKyle Evans #include <sys/param.h>
51251a32b5SKyle Evans #include <sys/lock.h>
52251a32b5SKyle Evans #include <sys/priv.h>
53251a32b5SKyle Evans #include <sys/proc.h>
54251a32b5SKyle Evans #include <sys/systm.h>
55251a32b5SKyle Evans #include <sys/jail.h>
56251a32b5SKyle Evans #include <sys/mbuf.h>
57251a32b5SKyle Evans #include <sys/module.h>
58251a32b5SKyle Evans #include <sys/socket.h>
59e2e050c8SConrad Meyer #include <sys/eventhandler.h>
60251a32b5SKyle Evans #include <sys/fcntl.h>
61251a32b5SKyle Evans #include <sys/filio.h>
62251a32b5SKyle Evans #include <sys/sockio.h>
63251a32b5SKyle Evans #include <sys/sx.h>
6429128766SKyle Evans #include <sys/syslog.h>
65251a32b5SKyle Evans #include <sys/ttycom.h>
66251a32b5SKyle Evans #include <sys/poll.h>
67251a32b5SKyle Evans #include <sys/selinfo.h>
68251a32b5SKyle Evans #include <sys/signalvar.h>
69251a32b5SKyle Evans #include <sys/filedesc.h>
70251a32b5SKyle Evans #include <sys/kernel.h>
71251a32b5SKyle Evans #include <sys/sysctl.h>
72251a32b5SKyle Evans #include <sys/conf.h>
73251a32b5SKyle Evans #include <sys/uio.h>
74251a32b5SKyle Evans #include <sys/malloc.h>
75251a32b5SKyle Evans #include <sys/random.h>
76251a32b5SKyle Evans #include <sys/ctype.h>
77251a32b5SKyle Evans 
78251a32b5SKyle Evans #include <net/ethernet.h>
79251a32b5SKyle Evans #include <net/if.h>
80251a32b5SKyle Evans #include <net/if_var.h>
81251a32b5SKyle Evans #include <net/if_clone.h>
82251a32b5SKyle Evans #include <net/if_dl.h>
83251a32b5SKyle Evans #include <net/if_media.h>
842c2b37adSJustin Hibbits #include <net/if_private.h>
85251a32b5SKyle Evans #include <net/if_types.h>
86f8bc74e2SVincenzo Maffione #include <net/if_vlan_var.h>
87251a32b5SKyle Evans #include <net/netisr.h>
88251a32b5SKyle Evans #include <net/route.h>
89251a32b5SKyle Evans #include <net/vnet.h>
90251a32b5SKyle Evans #include <netinet/in.h>
91f7810883SKyle Evans #ifdef INET
92f8bc74e2SVincenzo Maffione #include <netinet/ip.h>
93f7810883SKyle Evans #endif
94f7810883SKyle Evans #ifdef INET6
95f8bc74e2SVincenzo Maffione #include <netinet/ip6.h>
96f8bc74e2SVincenzo Maffione #include <netinet6/ip6_var.h>
97f7810883SKyle Evans #endif
98f8bc74e2SVincenzo Maffione #include <netinet/udp.h>
99f8bc74e2SVincenzo Maffione #include <netinet/tcp.h>
10099c79cabSMichael Tuexen #include <netinet/tcp_lro.h>
101251a32b5SKyle Evans #include <net/bpf.h>
102251a32b5SKyle Evans #include <net/if_tap.h>
103251a32b5SKyle Evans #include <net/if_tun.h>
104251a32b5SKyle Evans 
105f8bc74e2SVincenzo Maffione #include <dev/virtio/network/virtio_net.h>
106f8bc74e2SVincenzo Maffione 
107251a32b5SKyle Evans #include <sys/queue.h>
108251a32b5SKyle Evans #include <sys/condvar.h>
109251a32b5SKyle Evans #include <security/mac/mac_framework.h>
110251a32b5SKyle Evans 
111251a32b5SKyle Evans struct tuntap_driver;
112251a32b5SKyle Evans 
113251a32b5SKyle Evans /*
114251a32b5SKyle Evans  * tun_list is protected by global tunmtx.  Other mutable fields are
115251a32b5SKyle Evans  * protected by tun->tun_mtx, or by their owning subsystem.  tun_dev is
116251a32b5SKyle Evans  * static for the duration of a tunnel interface.
117251a32b5SKyle Evans  */
118251a32b5SKyle Evans struct tuntap_softc {
119251a32b5SKyle Evans 	TAILQ_ENTRY(tuntap_softc)	 tun_list;
12059997c3cSKyle Evans 	struct cdev			*tun_alias;
121251a32b5SKyle Evans 	struct cdev			*tun_dev;
122251a32b5SKyle Evans 	u_short				 tun_flags;	/* misc flags */
123251a32b5SKyle Evans #define	TUN_OPEN	0x0001
124251a32b5SKyle Evans #define	TUN_INITED	0x0002
1256041d76eSKyle Evans #define	TUN_UNUSED1	0x0008
126a6b76897SAlexander V. Chernikov #define	TUN_UNUSED2	0x0010
127251a32b5SKyle Evans #define	TUN_LMODE	0x0020
128251a32b5SKyle Evans #define	TUN_RWAIT	0x0040
129251a32b5SKyle Evans #define	TUN_ASYNC	0x0080
130251a32b5SKyle Evans #define	TUN_IFHEAD	0x0100
131251a32b5SKyle Evans #define	TUN_DYING	0x0200
132251a32b5SKyle Evans #define	TUN_L2		0x0400
133251a32b5SKyle Evans #define	TUN_VMNET	0x0800
134251a32b5SKyle Evans 
13581b3b91eSKyle Evans #define	TUN_DRIVER_IDENT_MASK	(TUN_L2 | TUN_VMNET)
136251a32b5SKyle Evans #define	TUN_READY		(TUN_OPEN | TUN_INITED)
137251a32b5SKyle Evans 
138251a32b5SKyle Evans 	pid_t			 tun_pid;	/* owning pid */
139251a32b5SKyle Evans 	struct ifnet		*tun_ifp;	/* the interface */
14081b3b91eSKyle Evans 	struct sigio		*tun_sigio;	/* async I/O info */
141251a32b5SKyle Evans 	struct tuntap_driver	*tun_drv;	/* appropriate driver */
142251a32b5SKyle Evans 	struct selinfo		 tun_rsel;	/* read select */
14381b3b91eSKyle Evans 	struct mtx		 tun_mtx;	/* softc field mutex */
14481b3b91eSKyle Evans 	struct cv		 tun_cv;	/* for ref'd dev destroy */
145251a32b5SKyle Evans 	struct ether_addr	 tun_ether;	/* remote address */
146c4cad154SKyle Evans 	int			 tun_busy;	/* busy count */
147f8bc74e2SVincenzo Maffione 	int			 tun_vhdrlen;	/* virtio-net header length */
14899c79cabSMichael Tuexen 	struct lro_ctrl		 tun_lro;	/* for TCP LRO */
14999c79cabSMichael Tuexen 	bool			 tun_lro_ready;	/* TCP LRO initialized */
150251a32b5SKyle Evans };
151251a32b5SKyle Evans #define	TUN2IFP(sc)	((sc)->tun_ifp)
152251a32b5SKyle Evans 
153251a32b5SKyle Evans #define	TUNDEBUG	if (tundebug) if_printf
154251a32b5SKyle Evans 
155251a32b5SKyle Evans #define	TUN_LOCK(tp)		mtx_lock(&(tp)->tun_mtx)
156251a32b5SKyle Evans #define	TUN_UNLOCK(tp)		mtx_unlock(&(tp)->tun_mtx)
157c4cad154SKyle Evans #define	TUN_LOCK_ASSERT(tp)	mtx_assert(&(tp)->tun_mtx, MA_OWNED);
158251a32b5SKyle Evans 
159251a32b5SKyle Evans #define	TUN_VMIO_FLAG_MASK	0x0fff
160251a32b5SKyle Evans 
161251a32b5SKyle Evans /*
162f8bc74e2SVincenzo Maffione  * Interface capabilities of a tap device that supports the virtio-net
163f8bc74e2SVincenzo Maffione  * header.
164f8bc74e2SVincenzo Maffione  */
165f8bc74e2SVincenzo Maffione #define TAP_VNET_HDR_CAPS	(IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6	\
166f8bc74e2SVincenzo Maffione 				| IFCAP_VLAN_HWCSUM			\
167f8bc74e2SVincenzo Maffione 				| IFCAP_TSO | IFCAP_LRO			\
168f8bc74e2SVincenzo Maffione 				| IFCAP_VLAN_HWTSO)
169f8bc74e2SVincenzo Maffione 
170f8bc74e2SVincenzo Maffione #define TAP_ALL_OFFLOAD		(CSUM_TSO | CSUM_TCP | CSUM_UDP |\
171f8bc74e2SVincenzo Maffione 				    CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
172f8bc74e2SVincenzo Maffione 
173f8bc74e2SVincenzo Maffione /*
174251a32b5SKyle Evans  * All mutable global variables in if_tun are locked using tunmtx, with
175251a32b5SKyle Evans  * the exception of tundebug, which is used unlocked, and the drivers' *clones,
176251a32b5SKyle Evans  * which are static after setup.
177251a32b5SKyle Evans  */
178251a32b5SKyle Evans static struct mtx tunmtx;
17959997c3cSKyle Evans static eventhandler_tag arrival_tag;
18059997c3cSKyle Evans static eventhandler_tag clone_tag;
181251a32b5SKyle Evans static const char tunname[] = "tun";
182251a32b5SKyle Evans static const char tapname[] = "tap";
183251a32b5SKyle Evans static const char vmnetname[] = "vmnet";
184251a32b5SKyle Evans static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
185251a32b5SKyle Evans static int tundebug = 0;
186251a32b5SKyle Evans static int tundclone = 1;
1873d501333SKyle Evans static int tap_allow_uopen = 0;	/* allow user devfs cloning */
188251a32b5SKyle Evans static int tapuponopen = 0;	/* IFF_UP on open() */
189251a32b5SKyle Evans static int tapdclone = 1;	/* enable devfs cloning */
190251a32b5SKyle Evans 
191251a32b5SKyle Evans static TAILQ_HEAD(,tuntap_softc)	tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
192251a32b5SKyle Evans SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
193251a32b5SKyle Evans 
194251a32b5SKyle Evans static struct sx tun_ioctl_sx;
195251a32b5SKyle Evans SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
196251a32b5SKyle Evans 
197251a32b5SKyle Evans SYSCTL_DECL(_net_link);
198251a32b5SKyle Evans /* tun */
1997029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
200b5b83671SVincenzo Maffione     "IP tunnel software network interface");
201251a32b5SKyle Evans SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
202b5b83671SVincenzo Maffione     "Enable legacy devfs interface creation");
203251a32b5SKyle Evans 
204251a32b5SKyle Evans /* tap */
2057029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
206251a32b5SKyle Evans     "Ethernet tunnel software network interface");
207251a32b5SKyle Evans SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
2083d501333SKyle Evans     "Enable legacy devfs interface creation for all users");
209251a32b5SKyle Evans SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
210251a32b5SKyle Evans     "Bring interface up when /dev/tap is opened");
211251a32b5SKyle Evans SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
212251a32b5SKyle Evans     "Enable legacy devfs interface creation");
213251a32b5SKyle Evans SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
214251a32b5SKyle Evans 
2156869d530SKyle Evans static int	tun_create_device(struct tuntap_driver *drv, int unit,
2166869d530SKyle Evans     struct ucred *cr, struct cdev **dev, const char *name);
217c4cad154SKyle Evans static int	tun_busy_locked(struct tuntap_softc *tp);
218c4cad154SKyle Evans static void	tun_unbusy_locked(struct tuntap_softc *tp);
219c4cad154SKyle Evans static int	tun_busy(struct tuntap_softc *tp);
220c4cad154SKyle Evans static void	tun_unbusy(struct tuntap_softc *tp);
221c4cad154SKyle Evans 
222251a32b5SKyle Evans static int	tuntap_name2info(const char *name, int *unit, int *flags);
223251a32b5SKyle Evans static void	tunclone(void *arg, struct ucred *cred, char *name,
224251a32b5SKyle Evans 		    int namelen, struct cdev **dev);
2256869d530SKyle Evans static void	tuncreate(struct cdev *dev);
22660250777SKyle Evans static void	tundtor(void *data);
22759997c3cSKyle Evans static void	tunrename(void *arg, struct ifnet *ifp);
228251a32b5SKyle Evans static int	tunifioctl(struct ifnet *, u_long, caddr_t);
229251a32b5SKyle Evans static void	tuninit(struct ifnet *);
230251a32b5SKyle Evans static void	tunifinit(void *xtp);
231251a32b5SKyle Evans static int	tuntapmodevent(module_t, int, void *);
232251a32b5SKyle Evans static int	tunoutput(struct ifnet *, struct mbuf *,
233251a32b5SKyle Evans 		    const struct sockaddr *, struct route *ro);
234251a32b5SKyle Evans static void	tunstart(struct ifnet *);
235251a32b5SKyle Evans static void	tunstart_l2(struct ifnet *);
236251a32b5SKyle Evans 
237251a32b5SKyle Evans static int	tun_clone_match(struct if_clone *ifc, const char *name);
238251a32b5SKyle Evans static int	tap_clone_match(struct if_clone *ifc, const char *name);
239251a32b5SKyle Evans static int	vmnet_clone_match(struct if_clone *ifc, const char *name);
24091ebcbe0SAlexander V. Chernikov static int	tun_clone_create(struct if_clone *, char *, size_t,
24191ebcbe0SAlexander V. Chernikov 		    struct ifc_data *, struct ifnet **);
24291ebcbe0SAlexander V. Chernikov static int	tun_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
243f8bc74e2SVincenzo Maffione static void	tun_vnethdr_set(struct ifnet *ifp, int vhdrlen);
244251a32b5SKyle Evans 
245251a32b5SKyle Evans static d_open_t		tunopen;
246251a32b5SKyle Evans static d_read_t		tunread;
247251a32b5SKyle Evans static d_write_t	tunwrite;
248251a32b5SKyle Evans static d_ioctl_t	tunioctl;
249251a32b5SKyle Evans static d_poll_t		tunpoll;
250251a32b5SKyle Evans static d_kqfilter_t	tunkqfilter;
251251a32b5SKyle Evans 
252251a32b5SKyle Evans static int		tunkqread(struct knote *, long);
253251a32b5SKyle Evans static int		tunkqwrite(struct knote *, long);
254251a32b5SKyle Evans static void		tunkqdetach(struct knote *);
255251a32b5SKyle Evans 
256*ef9ffb85SMark Johnston static const struct filterops tun_read_filterops = {
257251a32b5SKyle Evans 	.f_isfd =	1,
258251a32b5SKyle Evans 	.f_attach =	NULL,
259251a32b5SKyle Evans 	.f_detach =	tunkqdetach,
260251a32b5SKyle Evans 	.f_event =	tunkqread,
261251a32b5SKyle Evans };
262251a32b5SKyle Evans 
263*ef9ffb85SMark Johnston static const struct filterops tun_write_filterops = {
264251a32b5SKyle Evans 	.f_isfd =	1,
265251a32b5SKyle Evans 	.f_attach =	NULL,
266251a32b5SKyle Evans 	.f_detach =	tunkqdetach,
267251a32b5SKyle Evans 	.f_event =	tunkqwrite,
268251a32b5SKyle Evans };
269251a32b5SKyle Evans 
270251a32b5SKyle Evans static struct tuntap_driver {
271251a32b5SKyle Evans 	struct cdevsw		 cdevsw;
27281b3b91eSKyle Evans 	int			 ident_flags;
27381b3b91eSKyle Evans 	struct unrhdr		*unrhdr;
274251a32b5SKyle Evans 	struct clonedevs	*clones;
27591ebcbe0SAlexander V. Chernikov 	ifc_match_f		*clone_match_fn;
27691ebcbe0SAlexander V. Chernikov 	ifc_create_f		*clone_create_fn;
27791ebcbe0SAlexander V. Chernikov 	ifc_destroy_f		*clone_destroy_fn;
278251a32b5SKyle Evans } tuntap_drivers[] = {
279251a32b5SKyle Evans 	{
28081b3b91eSKyle Evans 		.ident_flags =	0,
281251a32b5SKyle Evans 		.cdevsw =	{
282251a32b5SKyle Evans 		    .d_version =	D_VERSION,
283251a32b5SKyle Evans 		    .d_flags =		D_NEEDMINOR,
284251a32b5SKyle Evans 		    .d_open =		tunopen,
285251a32b5SKyle Evans 		    .d_read =		tunread,
286251a32b5SKyle Evans 		    .d_write =		tunwrite,
287251a32b5SKyle Evans 		    .d_ioctl =		tunioctl,
288251a32b5SKyle Evans 		    .d_poll =		tunpoll,
289251a32b5SKyle Evans 		    .d_kqfilter =	tunkqfilter,
290251a32b5SKyle Evans 		    .d_name =		tunname,
291251a32b5SKyle Evans 		},
292251a32b5SKyle Evans 		.clone_match_fn =	tun_clone_match,
293251a32b5SKyle Evans 		.clone_create_fn =	tun_clone_create,
294251a32b5SKyle Evans 		.clone_destroy_fn =	tun_clone_destroy,
295251a32b5SKyle Evans 	},
296251a32b5SKyle Evans 	{
29781b3b91eSKyle Evans 		.ident_flags =	TUN_L2,
298251a32b5SKyle Evans 		.cdevsw =	{
299251a32b5SKyle Evans 		    .d_version =	D_VERSION,
300251a32b5SKyle Evans 		    .d_flags =		D_NEEDMINOR,
301251a32b5SKyle Evans 		    .d_open =		tunopen,
302251a32b5SKyle Evans 		    .d_read =		tunread,
303251a32b5SKyle Evans 		    .d_write =		tunwrite,
304251a32b5SKyle Evans 		    .d_ioctl =		tunioctl,
305251a32b5SKyle Evans 		    .d_poll =		tunpoll,
306251a32b5SKyle Evans 		    .d_kqfilter =	tunkqfilter,
307251a32b5SKyle Evans 		    .d_name =		tapname,
308251a32b5SKyle Evans 		},
309251a32b5SKyle Evans 		.clone_match_fn =	tap_clone_match,
310251a32b5SKyle Evans 		.clone_create_fn =	tun_clone_create,
311251a32b5SKyle Evans 		.clone_destroy_fn =	tun_clone_destroy,
312251a32b5SKyle Evans 	},
313251a32b5SKyle Evans 	{
31481b3b91eSKyle Evans 		.ident_flags =	TUN_L2 | TUN_VMNET,
315251a32b5SKyle Evans 		.cdevsw =	{
316251a32b5SKyle Evans 		    .d_version =	D_VERSION,
317251a32b5SKyle Evans 		    .d_flags =		D_NEEDMINOR,
318251a32b5SKyle Evans 		    .d_open =		tunopen,
319251a32b5SKyle Evans 		    .d_read =		tunread,
320251a32b5SKyle Evans 		    .d_write =		tunwrite,
321251a32b5SKyle Evans 		    .d_ioctl =		tunioctl,
322251a32b5SKyle Evans 		    .d_poll =		tunpoll,
323251a32b5SKyle Evans 		    .d_kqfilter =	tunkqfilter,
324251a32b5SKyle Evans 		    .d_name =		vmnetname,
325251a32b5SKyle Evans 		},
326251a32b5SKyle Evans 		.clone_match_fn =	vmnet_clone_match,
327251a32b5SKyle Evans 		.clone_create_fn =	tun_clone_create,
328251a32b5SKyle Evans 		.clone_destroy_fn =	tun_clone_destroy,
329251a32b5SKyle Evans 	},
330251a32b5SKyle Evans };
331fa93ba40SGleb Smirnoff #define	NDRV	nitems(tuntap_drivers)
332251a32b5SKyle Evans 
333fa93ba40SGleb Smirnoff VNET_DEFINE_STATIC(struct if_clone *, tuntap_driver_cloners[NDRV]);
334251a32b5SKyle Evans #define	V_tuntap_driver_cloners	VNET(tuntap_driver_cloners)
335251a32b5SKyle Evans 
336251a32b5SKyle Evans /*
337c4cad154SKyle Evans  * Mechanism for marking a tunnel device as busy so that we can safely do some
338c4cad154SKyle Evans  * orthogonal operations (such as operations on devices) without racing against
339c4cad154SKyle Evans  * tun_destroy.  tun_destroy will wait on the condvar if we're at all busy or
340c4cad154SKyle Evans  * open, to be woken up when the condition is alleviated.
341c4cad154SKyle Evans  */
342c4cad154SKyle Evans static int
tun_busy_locked(struct tuntap_softc * tp)343c4cad154SKyle Evans tun_busy_locked(struct tuntap_softc *tp)
344c4cad154SKyle Evans {
345c4cad154SKyle Evans 
346c4cad154SKyle Evans 	TUN_LOCK_ASSERT(tp);
347c4cad154SKyle Evans 	if ((tp->tun_flags & TUN_DYING) != 0) {
348c4cad154SKyle Evans 		/*
349c4cad154SKyle Evans 		 * Perhaps unintuitive, but the device is busy going away.
350c4cad154SKyle Evans 		 * Other interpretations of EBUSY from tun_busy make little
351c4cad154SKyle Evans 		 * sense, since making a busy device even more busy doesn't
352c4cad154SKyle Evans 		 * sound like a problem.
353c4cad154SKyle Evans 		 */
354c4cad154SKyle Evans 		return (EBUSY);
355c4cad154SKyle Evans 	}
356c4cad154SKyle Evans 
357c4cad154SKyle Evans 	++tp->tun_busy;
358c4cad154SKyle Evans 	return (0);
359c4cad154SKyle Evans }
360c4cad154SKyle Evans 
361c4cad154SKyle Evans static void
tun_unbusy_locked(struct tuntap_softc * tp)362c4cad154SKyle Evans tun_unbusy_locked(struct tuntap_softc *tp)
363c4cad154SKyle Evans {
364c4cad154SKyle Evans 
365c4cad154SKyle Evans 	TUN_LOCK_ASSERT(tp);
366c4cad154SKyle Evans 	KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
367c4cad154SKyle Evans 
368c4cad154SKyle Evans 	--tp->tun_busy;
369c4cad154SKyle Evans 	/* Wake up anything that may be waiting on our busy tunnel. */
370c4cad154SKyle Evans 	if (tp->tun_busy == 0)
371c4cad154SKyle Evans 		cv_broadcast(&tp->tun_cv);
372c4cad154SKyle Evans }
373c4cad154SKyle Evans 
374c4cad154SKyle Evans static int
tun_busy(struct tuntap_softc * tp)375c4cad154SKyle Evans tun_busy(struct tuntap_softc *tp)
376c4cad154SKyle Evans {
377c4cad154SKyle Evans 	int ret;
378c4cad154SKyle Evans 
379c4cad154SKyle Evans 	TUN_LOCK(tp);
380c4cad154SKyle Evans 	ret = tun_busy_locked(tp);
381c4cad154SKyle Evans 	TUN_UNLOCK(tp);
382c4cad154SKyle Evans 	return (ret);
383c4cad154SKyle Evans }
384c4cad154SKyle Evans 
385c4cad154SKyle Evans static void
tun_unbusy(struct tuntap_softc * tp)386c4cad154SKyle Evans tun_unbusy(struct tuntap_softc *tp)
387c4cad154SKyle Evans {
388c4cad154SKyle Evans 
389c4cad154SKyle Evans 	TUN_LOCK(tp);
390c4cad154SKyle Evans 	tun_unbusy_locked(tp);
391c4cad154SKyle Evans 	TUN_UNLOCK(tp);
392c4cad154SKyle Evans }
393c4cad154SKyle Evans 
394c4cad154SKyle Evans /*
395251a32b5SKyle Evans  * Sets unit and/or flags given the device name.  Must be called with correct
396251a32b5SKyle Evans  * vnet context.
397251a32b5SKyle Evans  */
398251a32b5SKyle Evans static int
tuntap_name2info(const char * name,int * outunit,int * outflags)399251a32b5SKyle Evans tuntap_name2info(const char *name, int *outunit, int *outflags)
400251a32b5SKyle Evans {
401251a32b5SKyle Evans 	struct tuntap_driver *drv;
402251a32b5SKyle Evans 	char *dname;
403251a32b5SKyle Evans 	int flags, unit;
404251a32b5SKyle Evans 	bool found;
405251a32b5SKyle Evans 
406251a32b5SKyle Evans 	if (name == NULL)
407251a32b5SKyle Evans 		return (EINVAL);
408251a32b5SKyle Evans 
409251a32b5SKyle Evans 	/*
410251a32b5SKyle Evans 	 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
411251a32b5SKyle Evans 	 * wants to be able to pass back a char * through the second param. We
412251a32b5SKyle Evans 	 * will always set that as NULL here, so we'll fake it.
413251a32b5SKyle Evans 	 */
414251a32b5SKyle Evans 	dname = __DECONST(char *, name);
415251a32b5SKyle Evans 	found = false;
416251a32b5SKyle Evans 
417fa93ba40SGleb Smirnoff 	for (u_int i = 0; i < NDRV; i++) {
418fa93ba40SGleb Smirnoff 		drv = &tuntap_drivers[i];
419251a32b5SKyle Evans 
420251a32b5SKyle Evans 		if (strcmp(name, drv->cdevsw.d_name) == 0) {
421251a32b5SKyle Evans 			found = true;
422251a32b5SKyle Evans 			unit = -1;
42381b3b91eSKyle Evans 			flags = drv->ident_flags;
424251a32b5SKyle Evans 			break;
425251a32b5SKyle Evans 		}
426251a32b5SKyle Evans 
427251a32b5SKyle Evans 		if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
428251a32b5SKyle Evans 			found = true;
42981b3b91eSKyle Evans 			flags = drv->ident_flags;
430251a32b5SKyle Evans 			break;
431251a32b5SKyle Evans 		}
432251a32b5SKyle Evans 	}
433251a32b5SKyle Evans 
434251a32b5SKyle Evans 	if (!found)
435251a32b5SKyle Evans 		return (ENXIO);
436251a32b5SKyle Evans 
437251a32b5SKyle Evans 	if (outunit != NULL)
438251a32b5SKyle Evans 		*outunit = unit;
439251a32b5SKyle Evans 	if (outflags != NULL)
440251a32b5SKyle Evans 		*outflags = flags;
441251a32b5SKyle Evans 	return (0);
442251a32b5SKyle Evans }
443251a32b5SKyle Evans 
444251a32b5SKyle Evans /*
445251a32b5SKyle Evans  * Get driver information from a set of flags specified.  Masks the identifying
446251a32b5SKyle Evans  * part of the flags and compares it against all of the available
447fa93ba40SGleb Smirnoff  * tuntap_drivers.
448251a32b5SKyle Evans  */
449251a32b5SKyle Evans static struct tuntap_driver *
tuntap_driver_from_flags(int tun_flags)450251a32b5SKyle Evans tuntap_driver_from_flags(int tun_flags)
451251a32b5SKyle Evans {
452251a32b5SKyle Evans 
453fa93ba40SGleb Smirnoff 	for (u_int i = 0; i < NDRV; i++)
454fa93ba40SGleb Smirnoff 		if ((tun_flags & TUN_DRIVER_IDENT_MASK) ==
455fa93ba40SGleb Smirnoff 		    tuntap_drivers[i].ident_flags)
456fa93ba40SGleb Smirnoff 			return (&tuntap_drivers[i]);
457251a32b5SKyle Evans 
458251a32b5SKyle Evans 	return (NULL);
459251a32b5SKyle Evans }
460251a32b5SKyle Evans 
461251a32b5SKyle Evans static int
tun_clone_match(struct if_clone * ifc,const char * name)462251a32b5SKyle Evans tun_clone_match(struct if_clone *ifc, const char *name)
463251a32b5SKyle Evans {
464251a32b5SKyle Evans 	int tunflags;
465251a32b5SKyle Evans 
466251a32b5SKyle Evans 	if (tuntap_name2info(name, NULL, &tunflags) == 0) {
467251a32b5SKyle Evans 		if ((tunflags & TUN_L2) == 0)
468251a32b5SKyle Evans 			return (1);
469251a32b5SKyle Evans 	}
470251a32b5SKyle Evans 
471251a32b5SKyle Evans 	return (0);
472251a32b5SKyle Evans }
473251a32b5SKyle Evans 
474251a32b5SKyle Evans static int
tap_clone_match(struct if_clone * ifc,const char * name)475251a32b5SKyle Evans tap_clone_match(struct if_clone *ifc, const char *name)
476251a32b5SKyle Evans {
477251a32b5SKyle Evans 	int tunflags;
478251a32b5SKyle Evans 
479251a32b5SKyle Evans 	if (tuntap_name2info(name, NULL, &tunflags) == 0) {
480251a32b5SKyle Evans 		if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
481251a32b5SKyle Evans 			return (1);
482251a32b5SKyle Evans 	}
483251a32b5SKyle Evans 
484251a32b5SKyle Evans 	return (0);
485251a32b5SKyle Evans }
486251a32b5SKyle Evans 
487251a32b5SKyle Evans static int
vmnet_clone_match(struct if_clone * ifc,const char * name)488251a32b5SKyle Evans vmnet_clone_match(struct if_clone *ifc, const char *name)
489251a32b5SKyle Evans {
490251a32b5SKyle Evans 	int tunflags;
491251a32b5SKyle Evans 
492251a32b5SKyle Evans 	if (tuntap_name2info(name, NULL, &tunflags) == 0) {
493251a32b5SKyle Evans 		if ((tunflags & TUN_VMNET) != 0)
494251a32b5SKyle Evans 			return (1);
495251a32b5SKyle Evans 	}
496251a32b5SKyle Evans 
497251a32b5SKyle Evans 	return (0);
498251a32b5SKyle Evans }
499251a32b5SKyle Evans 
500251a32b5SKyle Evans static int
tun_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,struct ifnet ** ifpp)50191ebcbe0SAlexander V. Chernikov tun_clone_create(struct if_clone *ifc, char *name, size_t len,
50291ebcbe0SAlexander V. Chernikov     struct ifc_data *ifd, struct ifnet **ifpp)
503251a32b5SKyle Evans {
504251a32b5SKyle Evans 	struct tuntap_driver *drv;
505251a32b5SKyle Evans 	struct cdev *dev;
506251a32b5SKyle Evans 	int err, i, tunflags, unit;
507251a32b5SKyle Evans 
508251a32b5SKyle Evans 	tunflags = 0;
509251a32b5SKyle Evans 	/* The name here tells us exactly what we're creating */
510251a32b5SKyle Evans 	err = tuntap_name2info(name, &unit, &tunflags);
511251a32b5SKyle Evans 	if (err != 0)
512251a32b5SKyle Evans 		return (err);
513251a32b5SKyle Evans 
514251a32b5SKyle Evans 	drv = tuntap_driver_from_flags(tunflags);
515251a32b5SKyle Evans 	if (drv == NULL)
516251a32b5SKyle Evans 		return (ENXIO);
517251a32b5SKyle Evans 
518251a32b5SKyle Evans 	if (unit != -1) {
519b5b83671SVincenzo Maffione 		/* If this unit number is still available that's okay. */
520251a32b5SKyle Evans 		if (alloc_unr_specific(drv->unrhdr, unit) == -1)
521251a32b5SKyle Evans 			return (EEXIST);
522251a32b5SKyle Evans 	} else {
523251a32b5SKyle Evans 		unit = alloc_unr(drv->unrhdr);
524251a32b5SKyle Evans 	}
525251a32b5SKyle Evans 
526251a32b5SKyle Evans 	snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
527251a32b5SKyle Evans 
528251a32b5SKyle Evans 	/* find any existing device, or allocate new unit number */
5296869d530SKyle Evans 	dev = NULL;
530251a32b5SKyle Evans 	i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
531251a32b5SKyle Evans 	/* No preexisting struct cdev *, create one */
5326869d530SKyle Evans 	if (i != 0)
5336869d530SKyle Evans 		i = tun_create_device(drv, unit, NULL, &dev, name);
53491ebcbe0SAlexander V. Chernikov 	if (i == 0) {
53527f1ec0bSKonstantin Belousov 		dev_ref(dev);
5366869d530SKyle Evans 		tuncreate(dev);
53791ebcbe0SAlexander V. Chernikov 		struct tuntap_softc *tp = dev->si_drv1;
53891ebcbe0SAlexander V. Chernikov 		*ifpp = tp->tun_ifp;
53991ebcbe0SAlexander V. Chernikov 	}
540251a32b5SKyle Evans 
5416869d530SKyle Evans 	return (i);
542251a32b5SKyle Evans }
543251a32b5SKyle Evans 
544251a32b5SKyle Evans static void
tunclone(void * arg,struct ucred * cred,char * name,int namelen,struct cdev ** dev)545251a32b5SKyle Evans tunclone(void *arg, struct ucred *cred, char *name, int namelen,
546251a32b5SKyle Evans     struct cdev **dev)
547251a32b5SKyle Evans {
548251a32b5SKyle Evans 	char devname[SPECNAMELEN + 1];
549251a32b5SKyle Evans 	struct tuntap_driver *drv;
550251a32b5SKyle Evans 	int append_unit, i, u, tunflags;
551251a32b5SKyle Evans 	bool mayclone;
552251a32b5SKyle Evans 
553251a32b5SKyle Evans 	if (*dev != NULL)
554251a32b5SKyle Evans 		return;
555251a32b5SKyle Evans 
556251a32b5SKyle Evans 	tunflags = 0;
557251a32b5SKyle Evans 	CURVNET_SET(CRED_TO_VNET(cred));
558251a32b5SKyle Evans 	if (tuntap_name2info(name, &u, &tunflags) != 0)
559251a32b5SKyle Evans 		goto out;	/* Not recognized */
560251a32b5SKyle Evans 
561251a32b5SKyle Evans 	if (u != -1 && u > IF_MAXUNIT)
562251a32b5SKyle Evans 		goto out;	/* Unit number too high */
563251a32b5SKyle Evans 
564251a32b5SKyle Evans 	mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
565251a32b5SKyle Evans 	if ((tunflags & TUN_L2) != 0) {
566251a32b5SKyle Evans 		/* tap/vmnet allow user open with a sysctl */
567251a32b5SKyle Evans 		mayclone = (mayclone || tap_allow_uopen) && tapdclone;
568251a32b5SKyle Evans 	} else {
569251a32b5SKyle Evans 		mayclone = mayclone && tundclone;
570251a32b5SKyle Evans 	}
571251a32b5SKyle Evans 
572251a32b5SKyle Evans 	/*
573251a32b5SKyle Evans 	 * If tun cloning is enabled, only the superuser can create an
574251a32b5SKyle Evans 	 * interface.
575251a32b5SKyle Evans 	 */
576251a32b5SKyle Evans 	if (!mayclone)
577251a32b5SKyle Evans 		goto out;
578251a32b5SKyle Evans 
579251a32b5SKyle Evans 	if (u == -1)
580251a32b5SKyle Evans 		append_unit = 1;
581251a32b5SKyle Evans 	else
582251a32b5SKyle Evans 		append_unit = 0;
583251a32b5SKyle Evans 
584251a32b5SKyle Evans 	drv = tuntap_driver_from_flags(tunflags);
585251a32b5SKyle Evans 	if (drv == NULL)
586251a32b5SKyle Evans 		goto out;
587251a32b5SKyle Evans 
588251a32b5SKyle Evans 	/* find any existing device, or allocate new unit number */
589251a32b5SKyle Evans 	i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
590251a32b5SKyle Evans 	if (i) {
591251a32b5SKyle Evans 		if (append_unit) {
592251a32b5SKyle Evans 			namelen = snprintf(devname, sizeof(devname), "%s%d",
593251a32b5SKyle Evans 			    name, u);
594251a32b5SKyle Evans 			name = devname;
595251a32b5SKyle Evans 		}
596251a32b5SKyle Evans 
5976869d530SKyle Evans 		i = tun_create_device(drv, u, cred, dev, name);
5986869d530SKyle Evans 	}
59927f1ec0bSKonstantin Belousov 	if (i == 0) {
60027f1ec0bSKonstantin Belousov 		dev_ref(*dev);
601251a32b5SKyle Evans 		if_clone_create(name, namelen, NULL);
60227f1ec0bSKonstantin Belousov 	}
603251a32b5SKyle Evans out:
604251a32b5SKyle Evans 	CURVNET_RESTORE();
605251a32b5SKyle Evans }
606251a32b5SKyle Evans 
607251a32b5SKyle Evans static void
tun_destroy(struct tuntap_softc * tp)608251a32b5SKyle Evans tun_destroy(struct tuntap_softc *tp)
609251a32b5SKyle Evans {
610251a32b5SKyle Evans 
611251a32b5SKyle Evans 	TUN_LOCK(tp);
612251a32b5SKyle Evans 	tp->tun_flags |= TUN_DYING;
613c4cad154SKyle Evans 	if (tp->tun_busy != 0)
614251a32b5SKyle Evans 		cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
615251a32b5SKyle Evans 	else
616251a32b5SKyle Evans 		TUN_UNLOCK(tp);
617251a32b5SKyle Evans 
618251a32b5SKyle Evans 	CURVNET_SET(TUN2IFP(tp)->if_vnet);
619251a32b5SKyle Evans 
62059997c3cSKyle Evans 	/* destroy_dev will take care of any alias. */
621a6fa0495SKyle Evans 	destroy_dev(tp->tun_dev);
622251a32b5SKyle Evans 	seldrain(&tp->tun_rsel);
623251a32b5SKyle Evans 	knlist_clear(&tp->tun_rsel.si_note, 0);
624251a32b5SKyle Evans 	knlist_destroy(&tp->tun_rsel.si_note);
625a6fa0495SKyle Evans 	if ((tp->tun_flags & TUN_L2) != 0) {
626a6fa0495SKyle Evans 		ether_ifdetach(TUN2IFP(tp));
627a6fa0495SKyle Evans 	} else {
628a6fa0495SKyle Evans 		bpfdetach(TUN2IFP(tp));
629a6fa0495SKyle Evans 		if_detach(TUN2IFP(tp));
630a6fa0495SKyle Evans 	}
631db226f0dSKyle Evans 	sx_xlock(&tun_ioctl_sx);
632db226f0dSKyle Evans 	TUN2IFP(tp)->if_softc = NULL;
633db226f0dSKyle Evans 	sx_xunlock(&tun_ioctl_sx);
634a6fa0495SKyle Evans 	free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
635a6fa0495SKyle Evans 	if_free(TUN2IFP(tp));
636251a32b5SKyle Evans 	mtx_destroy(&tp->tun_mtx);
637251a32b5SKyle Evans 	cv_destroy(&tp->tun_cv);
638251a32b5SKyle Evans 	free(tp, M_TUN);
639251a32b5SKyle Evans 	CURVNET_RESTORE();
640251a32b5SKyle Evans }
641251a32b5SKyle Evans 
642251a32b5SKyle Evans static int
tun_clone_destroy(struct if_clone * ifc __unused,struct ifnet * ifp,uint32_t flags)64391ebcbe0SAlexander V. Chernikov tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp, uint32_t flags)
644251a32b5SKyle Evans {
645251a32b5SKyle Evans 	struct tuntap_softc *tp = ifp->if_softc;
646251a32b5SKyle Evans 
647251a32b5SKyle Evans 	mtx_lock(&tunmtx);
648251a32b5SKyle Evans 	TAILQ_REMOVE(&tunhead, tp, tun_list);
649251a32b5SKyle Evans 	mtx_unlock(&tunmtx);
650251a32b5SKyle Evans 	tun_destroy(tp);
651251a32b5SKyle Evans 
652251a32b5SKyle Evans 	return (0);
653251a32b5SKyle Evans }
654251a32b5SKyle Evans 
655251a32b5SKyle Evans static void
vnet_tun_init(const void * unused __unused)656251a32b5SKyle Evans vnet_tun_init(const void *unused __unused)
657251a32b5SKyle Evans {
658251a32b5SKyle Evans 
659fa93ba40SGleb Smirnoff 	for (u_int i = 0; i < NDRV; ++i) {
66091ebcbe0SAlexander V. Chernikov 		struct if_clone_addreq req = {
661fa93ba40SGleb Smirnoff 			.match_f = tuntap_drivers[i].clone_match_fn,
662fa93ba40SGleb Smirnoff 			.create_f = tuntap_drivers[i].clone_create_fn,
663fa93ba40SGleb Smirnoff 			.destroy_f = tuntap_drivers[i].clone_destroy_fn,
66491ebcbe0SAlexander V. Chernikov 		};
665fa93ba40SGleb Smirnoff 		V_tuntap_driver_cloners[i] =
666fa93ba40SGleb Smirnoff 		    ifc_attach_cloner(tuntap_drivers[i].cdevsw.d_name, &req);
667251a32b5SKyle Evans 	};
668251a32b5SKyle Evans }
669251a32b5SKyle Evans VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
670251a32b5SKyle Evans 		vnet_tun_init, NULL);
671251a32b5SKyle Evans 
672251a32b5SKyle Evans static void
vnet_tun_uninit(const void * unused __unused)673251a32b5SKyle Evans vnet_tun_uninit(const void *unused __unused)
674251a32b5SKyle Evans {
675251a32b5SKyle Evans 
676fa93ba40SGleb Smirnoff 	for (u_int i = 0; i < NDRV; ++i)
677fa93ba40SGleb Smirnoff 		if_clone_detach(V_tuntap_driver_cloners[i]);
678251a32b5SKyle Evans }
679251a32b5SKyle Evans VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
680251a32b5SKyle Evans     vnet_tun_uninit, NULL);
681251a32b5SKyle Evans 
682251a32b5SKyle Evans static void
tun_uninit(const void * unused __unused)683251a32b5SKyle Evans tun_uninit(const void *unused __unused)
684251a32b5SKyle Evans {
685251a32b5SKyle Evans 	struct tuntap_driver *drv;
686251a32b5SKyle Evans 	struct tuntap_softc *tp;
687251a32b5SKyle Evans 	int i;
688251a32b5SKyle Evans 
68959997c3cSKyle Evans 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
69059997c3cSKyle Evans 	EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
691251a32b5SKyle Evans 
692251a32b5SKyle Evans 	mtx_lock(&tunmtx);
693251a32b5SKyle Evans 	while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
694251a32b5SKyle Evans 		TAILQ_REMOVE(&tunhead, tp, tun_list);
695251a32b5SKyle Evans 		mtx_unlock(&tunmtx);
696251a32b5SKyle Evans 		tun_destroy(tp);
697251a32b5SKyle Evans 		mtx_lock(&tunmtx);
698251a32b5SKyle Evans 	}
699251a32b5SKyle Evans 	mtx_unlock(&tunmtx);
700251a32b5SKyle Evans 	for (i = 0; i < nitems(tuntap_drivers); ++i) {
701251a32b5SKyle Evans 		drv = &tuntap_drivers[i];
702251a32b5SKyle Evans 		delete_unrhdr(drv->unrhdr);
703251a32b5SKyle Evans 		clone_cleanup(&drv->clones);
704251a32b5SKyle Evans 	}
705251a32b5SKyle Evans 	mtx_destroy(&tunmtx);
706251a32b5SKyle Evans }
707251a32b5SKyle Evans SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
708251a32b5SKyle Evans 
70959997c3cSKyle Evans static struct tuntap_driver *
tuntap_driver_from_ifnet(const struct ifnet * ifp)71059997c3cSKyle Evans tuntap_driver_from_ifnet(const struct ifnet *ifp)
71159997c3cSKyle Evans {
71259997c3cSKyle Evans 	struct tuntap_driver *drv;
71359997c3cSKyle Evans 	int i;
71459997c3cSKyle Evans 
71559997c3cSKyle Evans 	if (ifp == NULL)
71659997c3cSKyle Evans 		return (NULL);
71759997c3cSKyle Evans 
71859997c3cSKyle Evans 	for (i = 0; i < nitems(tuntap_drivers); ++i) {
71959997c3cSKyle Evans 		drv = &tuntap_drivers[i];
72059997c3cSKyle Evans 		if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
72159997c3cSKyle Evans 			return (drv);
72259997c3cSKyle Evans 	}
72359997c3cSKyle Evans 
72459997c3cSKyle Evans 	return (NULL);
72559997c3cSKyle Evans }
72659997c3cSKyle Evans 
727251a32b5SKyle Evans static int
tuntapmodevent(module_t mod,int type,void * data)728251a32b5SKyle Evans tuntapmodevent(module_t mod, int type, void *data)
729251a32b5SKyle Evans {
730251a32b5SKyle Evans 	struct tuntap_driver *drv;
731251a32b5SKyle Evans 	int i;
732251a32b5SKyle Evans 
733251a32b5SKyle Evans 	switch (type) {
734251a32b5SKyle Evans 	case MOD_LOAD:
735251a32b5SKyle Evans 		mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
736251a32b5SKyle Evans 		for (i = 0; i < nitems(tuntap_drivers); ++i) {
737251a32b5SKyle Evans 			drv = &tuntap_drivers[i];
738251a32b5SKyle Evans 			clone_setup(&drv->clones);
739251a32b5SKyle Evans 			drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
740251a32b5SKyle Evans 		}
74159997c3cSKyle Evans 		arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
74259997c3cSKyle Evans 		   tunrename, 0, 1000);
74359997c3cSKyle Evans 		if (arrival_tag == NULL)
74459997c3cSKyle Evans 			return (ENOMEM);
74559997c3cSKyle Evans 		clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
74659997c3cSKyle Evans 		if (clone_tag == NULL)
747251a32b5SKyle Evans 			return (ENOMEM);
748251a32b5SKyle Evans 		break;
749251a32b5SKyle Evans 	case MOD_UNLOAD:
750251a32b5SKyle Evans 		/* See tun_uninit, so it's done after the vnet_sysuninit() */
751251a32b5SKyle Evans 		break;
752251a32b5SKyle Evans 	default:
753251a32b5SKyle Evans 		return EOPNOTSUPP;
754251a32b5SKyle Evans 	}
755251a32b5SKyle Evans 	return 0;
756251a32b5SKyle Evans }
757251a32b5SKyle Evans 
758251a32b5SKyle Evans static moduledata_t tuntap_mod = {
759251a32b5SKyle Evans 	"if_tuntap",
760251a32b5SKyle Evans 	tuntapmodevent,
761251a32b5SKyle Evans 	0
762251a32b5SKyle Evans };
763251a32b5SKyle Evans 
764200abb43SKyle Evans /* We'll only ever have these two, so no need for a macro. */
765200abb43SKyle Evans static moduledata_t tun_mod = { "if_tun", NULL, 0 };
766200abb43SKyle Evans static moduledata_t tap_mod = { "if_tap", NULL, 0 };
767200abb43SKyle Evans 
768251a32b5SKyle Evans DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
769251a32b5SKyle Evans MODULE_VERSION(if_tuntap, 1);
770200abb43SKyle Evans DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
7715c4eed86SKyle Evans MODULE_VERSION(if_tun, 1);
772200abb43SKyle Evans DECLARE_MODULE(if_tap, tap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
7735c4eed86SKyle Evans MODULE_VERSION(if_tap, 1);
774251a32b5SKyle Evans 
7756869d530SKyle Evans static int
tun_create_device(struct tuntap_driver * drv,int unit,struct ucred * cr,struct cdev ** dev,const char * name)7766869d530SKyle Evans tun_create_device(struct tuntap_driver *drv, int unit, struct ucred *cr,
7776869d530SKyle Evans     struct cdev **dev, const char *name)
7786869d530SKyle Evans {
7796869d530SKyle Evans 	struct make_dev_args args;
7806869d530SKyle Evans 	struct tuntap_softc *tp;
7816869d530SKyle Evans 	int error;
7826869d530SKyle Evans 
7836869d530SKyle Evans 	tp = malloc(sizeof(*tp), M_TUN, M_WAITOK | M_ZERO);
7846869d530SKyle Evans 	mtx_init(&tp->tun_mtx, "tun_mtx", NULL, MTX_DEF);
7856869d530SKyle Evans 	cv_init(&tp->tun_cv, "tun_condvar");
7866869d530SKyle Evans 	tp->tun_flags = drv->ident_flags;
7876869d530SKyle Evans 	tp->tun_drv = drv;
7886869d530SKyle Evans 
7896869d530SKyle Evans 	make_dev_args_init(&args);
7906869d530SKyle Evans 	if (cr != NULL)
7910365e5fcSKonstantin Belousov 		args.mda_flags = MAKEDEV_REF | MAKEDEV_CHECKNAME;
7926869d530SKyle Evans 	args.mda_devsw = &drv->cdevsw;
7936869d530SKyle Evans 	args.mda_cr = cr;
7946869d530SKyle Evans 	args.mda_uid = UID_UUCP;
7956869d530SKyle Evans 	args.mda_gid = GID_DIALER;
7966869d530SKyle Evans 	args.mda_mode = 0600;
7976869d530SKyle Evans 	args.mda_unit = unit;
7986869d530SKyle Evans 	args.mda_si_drv1 = tp;
7996869d530SKyle Evans 	error = make_dev_s(&args, dev, "%s", name);
8006869d530SKyle Evans 	if (error != 0) {
8016869d530SKyle Evans 		free(tp, M_TUN);
8026869d530SKyle Evans 		return (error);
8036869d530SKyle Evans 	}
8046869d530SKyle Evans 
8056869d530SKyle Evans 	KASSERT((*dev)->si_drv1 != NULL,
8066869d530SKyle Evans 	    ("Failed to set si_drv1 at %s creation", name));
8076869d530SKyle Evans 	tp->tun_dev = *dev;
8086869d530SKyle Evans 	knlist_init_mtx(&tp->tun_rsel.si_note, &tp->tun_mtx);
8096869d530SKyle Evans 	mtx_lock(&tunmtx);
8106869d530SKyle Evans 	TAILQ_INSERT_TAIL(&tunhead, tp, tun_list);
8116869d530SKyle Evans 	mtx_unlock(&tunmtx);
8126869d530SKyle Evans 	return (0);
8136869d530SKyle Evans }
8146869d530SKyle Evans 
815251a32b5SKyle Evans static void
tunstart(struct ifnet * ifp)816251a32b5SKyle Evans tunstart(struct ifnet *ifp)
817251a32b5SKyle Evans {
818251a32b5SKyle Evans 	struct tuntap_softc *tp = ifp->if_softc;
819251a32b5SKyle Evans 	struct mbuf *m;
820251a32b5SKyle Evans 
821251a32b5SKyle Evans 	TUNDEBUG(ifp, "starting\n");
822251a32b5SKyle Evans 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
823251a32b5SKyle Evans 		IFQ_LOCK(&ifp->if_snd);
824251a32b5SKyle Evans 		IFQ_POLL_NOLOCK(&ifp->if_snd, m);
825251a32b5SKyle Evans 		if (m == NULL) {
826251a32b5SKyle Evans 			IFQ_UNLOCK(&ifp->if_snd);
827251a32b5SKyle Evans 			return;
828251a32b5SKyle Evans 		}
829251a32b5SKyle Evans 		IFQ_UNLOCK(&ifp->if_snd);
830251a32b5SKyle Evans 	}
831251a32b5SKyle Evans 
832251a32b5SKyle Evans 	TUN_LOCK(tp);
833251a32b5SKyle Evans 	if (tp->tun_flags & TUN_RWAIT) {
834251a32b5SKyle Evans 		tp->tun_flags &= ~TUN_RWAIT;
835251a32b5SKyle Evans 		wakeup(tp);
836251a32b5SKyle Evans 	}
837251a32b5SKyle Evans 	selwakeuppri(&tp->tun_rsel, PZERO + 1);
838251a32b5SKyle Evans 	KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
839251a32b5SKyle Evans 	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
840251a32b5SKyle Evans 		TUN_UNLOCK(tp);
841251a32b5SKyle Evans 		pgsigio(&tp->tun_sigio, SIGIO, 0);
842251a32b5SKyle Evans 	} else
843251a32b5SKyle Evans 		TUN_UNLOCK(tp);
844251a32b5SKyle Evans }
845251a32b5SKyle Evans 
846251a32b5SKyle Evans /*
847251a32b5SKyle Evans  * tunstart_l2
848251a32b5SKyle Evans  *
849251a32b5SKyle Evans  * queue packets from higher level ready to put out
850251a32b5SKyle Evans  */
851251a32b5SKyle Evans static void
tunstart_l2(struct ifnet * ifp)852251a32b5SKyle Evans tunstart_l2(struct ifnet *ifp)
853251a32b5SKyle Evans {
854251a32b5SKyle Evans 	struct tuntap_softc	*tp = ifp->if_softc;
855251a32b5SKyle Evans 
856251a32b5SKyle Evans 	TUNDEBUG(ifp, "starting\n");
857251a32b5SKyle Evans 
858251a32b5SKyle Evans 	/*
859251a32b5SKyle Evans 	 * do not junk pending output if we are in VMnet mode.
860251a32b5SKyle Evans 	 * XXX: can this do any harm because of queue overflow?
861251a32b5SKyle Evans 	 */
862251a32b5SKyle Evans 
863251a32b5SKyle Evans 	TUN_LOCK(tp);
864251a32b5SKyle Evans 	if (((tp->tun_flags & TUN_VMNET) == 0) &&
865251a32b5SKyle Evans 	    ((tp->tun_flags & TUN_READY) != TUN_READY)) {
866251a32b5SKyle Evans 		struct mbuf *m;
867251a32b5SKyle Evans 
868251a32b5SKyle Evans 		/* Unlocked read. */
869251a32b5SKyle Evans 		TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
870251a32b5SKyle Evans 
871251a32b5SKyle Evans 		for (;;) {
872251a32b5SKyle Evans 			IF_DEQUEUE(&ifp->if_snd, m);
873251a32b5SKyle Evans 			if (m != NULL) {
874251a32b5SKyle Evans 				m_freem(m);
875251a32b5SKyle Evans 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
876251a32b5SKyle Evans 			} else
877251a32b5SKyle Evans 				break;
878251a32b5SKyle Evans 		}
879251a32b5SKyle Evans 		TUN_UNLOCK(tp);
880251a32b5SKyle Evans 
881251a32b5SKyle Evans 		return;
882251a32b5SKyle Evans 	}
883251a32b5SKyle Evans 
884251a32b5SKyle Evans 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
885251a32b5SKyle Evans 
886251a32b5SKyle Evans 	if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
887251a32b5SKyle Evans 		if (tp->tun_flags & TUN_RWAIT) {
888251a32b5SKyle Evans 			tp->tun_flags &= ~TUN_RWAIT;
889251a32b5SKyle Evans 			wakeup(tp);
890251a32b5SKyle Evans 		}
891251a32b5SKyle Evans 
892251a32b5SKyle Evans 		if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
893251a32b5SKyle Evans 			TUN_UNLOCK(tp);
894251a32b5SKyle Evans 			pgsigio(&tp->tun_sigio, SIGIO, 0);
895251a32b5SKyle Evans 			TUN_LOCK(tp);
896251a32b5SKyle Evans 		}
897251a32b5SKyle Evans 
898251a32b5SKyle Evans 		selwakeuppri(&tp->tun_rsel, PZERO+1);
899251a32b5SKyle Evans 		KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
900251a32b5SKyle Evans 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
901251a32b5SKyle Evans 	}
902251a32b5SKyle Evans 
903251a32b5SKyle Evans 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
904251a32b5SKyle Evans 	TUN_UNLOCK(tp);
905251a32b5SKyle Evans } /* tunstart_l2 */
906251a32b5SKyle Evans 
90735af22acSMichael Tuexen static int
tap_transmit(struct ifnet * ifp,struct mbuf * m)90835af22acSMichael Tuexen tap_transmit(struct ifnet *ifp, struct mbuf *m)
90935af22acSMichael Tuexen {
91035af22acSMichael Tuexen 	int error;
91135af22acSMichael Tuexen 
91235af22acSMichael Tuexen 	BPF_MTAP(ifp, m);
91335af22acSMichael Tuexen 	IFQ_HANDOFF(ifp, m, error);
91435af22acSMichael Tuexen 	return (error);
91535af22acSMichael Tuexen }
91635af22acSMichael Tuexen 
917251a32b5SKyle Evans /* XXX: should return an error code so it can fail. */
918251a32b5SKyle Evans static void
tuncreate(struct cdev * dev)9196869d530SKyle Evans tuncreate(struct cdev *dev)
920251a32b5SKyle Evans {
9216869d530SKyle Evans 	struct tuntap_driver *drv;
9226869d530SKyle Evans 	struct tuntap_softc *tp;
923251a32b5SKyle Evans 	struct ifnet *ifp;
924251a32b5SKyle Evans 	struct ether_addr eaddr;
925251a32b5SKyle Evans 	int iflags;
926251a32b5SKyle Evans 	u_char type;
927251a32b5SKyle Evans 
9286869d530SKyle Evans 	tp = dev->si_drv1;
9296869d530SKyle Evans 	KASSERT(tp != NULL,
9306869d530SKyle Evans 	    ("si_drv1 should have been initialized at creation"));
931251a32b5SKyle Evans 
9326869d530SKyle Evans 	drv = tp->tun_drv;
933251a32b5SKyle Evans 	iflags = IFF_MULTICAST;
9346869d530SKyle Evans 	if ((tp->tun_flags & TUN_L2) != 0) {
935251a32b5SKyle Evans 		type = IFT_ETHER;
936251a32b5SKyle Evans 		iflags |= IFF_BROADCAST | IFF_SIMPLEX;
937251a32b5SKyle Evans 	} else {
938251a32b5SKyle Evans 		type = IFT_PPP;
939251a32b5SKyle Evans 		iflags |= IFF_POINTOPOINT;
940251a32b5SKyle Evans 	}
9416869d530SKyle Evans 	ifp = tp->tun_ifp = if_alloc(type);
9426869d530SKyle Evans 	ifp->if_softc = tp;
943251a32b5SKyle Evans 	if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
944251a32b5SKyle Evans 	ifp->if_ioctl = tunifioctl;
945251a32b5SKyle Evans 	ifp->if_flags = iflags;
946251a32b5SKyle Evans 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
94701c738cdSMark Johnston 	ifp->if_capabilities |= IFCAP_LINKSTATE | IFCAP_MEXTPG;
948ff69d13aSMichael Tuexen 	if ((tp->tun_flags & TUN_L2) != 0)
94999c79cabSMichael Tuexen 		ifp->if_capabilities |=
95099c79cabSMichael Tuexen 		    IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO;
95101c738cdSMark Johnston 	ifp->if_capenable |= IFCAP_LINKSTATE | IFCAP_MEXTPG;
952251a32b5SKyle Evans 
9536869d530SKyle Evans 	if ((tp->tun_flags & TUN_L2) != 0) {
954251a32b5SKyle Evans 		ifp->if_init = tunifinit;
955251a32b5SKyle Evans 		ifp->if_start = tunstart_l2;
95635af22acSMichael Tuexen 		ifp->if_transmit = tap_transmit;
95735af22acSMichael Tuexen 		ifp->if_qflush = if_qflush;
958251a32b5SKyle Evans 
959251a32b5SKyle Evans 		ether_gen_addr(ifp, &eaddr);
960251a32b5SKyle Evans 		ether_ifattach(ifp, eaddr.octet);
961251a32b5SKyle Evans 	} else {
962251a32b5SKyle Evans 		ifp->if_mtu = TUNMTU;
963251a32b5SKyle Evans 		ifp->if_start = tunstart;
964251a32b5SKyle Evans 		ifp->if_output = tunoutput;
965251a32b5SKyle Evans 
966251a32b5SKyle Evans 		ifp->if_snd.ifq_drv_maxlen = 0;
967251a32b5SKyle Evans 		IFQ_SET_READY(&ifp->if_snd);
968251a32b5SKyle Evans 
969251a32b5SKyle Evans 		if_attach(ifp);
970251a32b5SKyle Evans 		bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
971251a32b5SKyle Evans 	}
972251a32b5SKyle Evans 
9736869d530SKyle Evans 	TUN_LOCK(tp);
9746869d530SKyle Evans 	tp->tun_flags |= TUN_INITED;
9756869d530SKyle Evans 	TUN_UNLOCK(tp);
976251a32b5SKyle Evans 
977251a32b5SKyle Evans 	TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
978251a32b5SKyle Evans 	    ifp->if_xname, dev2unit(dev));
979251a32b5SKyle Evans }
980251a32b5SKyle Evans 
98159997c3cSKyle Evans static void
tunrename(void * arg __unused,struct ifnet * ifp)98259997c3cSKyle Evans tunrename(void *arg __unused, struct ifnet *ifp)
98359997c3cSKyle Evans {
98459997c3cSKyle Evans 	struct tuntap_softc *tp;
98559997c3cSKyle Evans 	int error;
98659997c3cSKyle Evans 
98759997c3cSKyle Evans 	if ((ifp->if_flags & IFF_RENAMING) == 0)
98859997c3cSKyle Evans 		return;
98959997c3cSKyle Evans 
99059997c3cSKyle Evans 	if (tuntap_driver_from_ifnet(ifp) == NULL)
99159997c3cSKyle Evans 		return;
99259997c3cSKyle Evans 
99359997c3cSKyle Evans 	/*
99459997c3cSKyle Evans 	 * We need to grab the ioctl sx long enough to make sure the softc is
99559997c3cSKyle Evans 	 * still there.  If it is, we can safely try to busy the tun device.
99659997c3cSKyle Evans 	 * The busy may fail if the device is currently dying, in which case
99759997c3cSKyle Evans 	 * we do nothing.  If it doesn't fail, the busy count stops the device
99859997c3cSKyle Evans 	 * from dying until we've created the alias (that will then be
99959997c3cSKyle Evans 	 * subsequently destroyed).
100059997c3cSKyle Evans 	 */
100159997c3cSKyle Evans 	sx_xlock(&tun_ioctl_sx);
100259997c3cSKyle Evans 	tp = ifp->if_softc;
100359997c3cSKyle Evans 	if (tp == NULL) {
100459997c3cSKyle Evans 		sx_xunlock(&tun_ioctl_sx);
100559997c3cSKyle Evans 		return;
100659997c3cSKyle Evans 	}
100759997c3cSKyle Evans 	error = tun_busy(tp);
100859997c3cSKyle Evans 	sx_xunlock(&tun_ioctl_sx);
100959997c3cSKyle Evans 	if (error != 0)
101059997c3cSKyle Evans 		return;
101159997c3cSKyle Evans 	if (tp->tun_alias != NULL) {
101259997c3cSKyle Evans 		destroy_dev(tp->tun_alias);
101359997c3cSKyle Evans 		tp->tun_alias = NULL;
101459997c3cSKyle Evans 	}
101559997c3cSKyle Evans 
101659997c3cSKyle Evans 	if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
101759997c3cSKyle Evans 		goto out;
101859997c3cSKyle Evans 
101959997c3cSKyle Evans 	/*
102059997c3cSKyle Evans 	 * Failure's ok, aliases are created on a best effort basis.  If a
102159997c3cSKyle Evans 	 * tun user/consumer decides to rename the interface to conflict with
102259997c3cSKyle Evans 	 * another device (non-ifnet) on the system, we will assume they know
102359997c3cSKyle Evans 	 * what they are doing.  make_dev_alias_p won't touch tun_alias on
102459997c3cSKyle Evans 	 * failure, so we use it but ignore the return value.
102559997c3cSKyle Evans 	 */
102659997c3cSKyle Evans 	make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
102759997c3cSKyle Evans 	    ifp->if_xname);
102859997c3cSKyle Evans out:
102959997c3cSKyle Evans 	tun_unbusy(tp);
103059997c3cSKyle Evans }
103159997c3cSKyle Evans 
1032251a32b5SKyle Evans static int
tunopen(struct cdev * dev,int flag,int mode,struct thread * td)1033251a32b5SKyle Evans tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1034251a32b5SKyle Evans {
1035251a32b5SKyle Evans 	struct ifnet	*ifp;
1036251a32b5SKyle Evans 	struct tuntap_softc *tp;
103751221b68SKyle Evans 	int error __diagused, tunflags;
1038251a32b5SKyle Evans 
1039251a32b5SKyle Evans 	tunflags = 0;
1040251a32b5SKyle Evans 	CURVNET_SET(TD_TO_VNET(td));
1041251a32b5SKyle Evans 	error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1042251a32b5SKyle Evans 	if (error != 0) {
1043251a32b5SKyle Evans 		CURVNET_RESTORE();
1044251a32b5SKyle Evans 		return (error);	/* Shouldn't happen */
1045251a32b5SKyle Evans 	}
1046251a32b5SKyle Evans 
1047251a32b5SKyle Evans 	tp = dev->si_drv1;
10486869d530SKyle Evans 	KASSERT(tp != NULL,
10496869d530SKyle Evans 	    ("si_drv1 should have been initialized at creation"));
10506869d530SKyle Evans 
10516869d530SKyle Evans 	TUN_LOCK(tp);
10526869d530SKyle Evans 	if ((tp->tun_flags & TUN_INITED) == 0) {
10536869d530SKyle Evans 		TUN_UNLOCK(tp);
1054251a32b5SKyle Evans 		CURVNET_RESTORE();
1055251a32b5SKyle Evans 		return (ENXIO);
1056251a32b5SKyle Evans 	}
1057251a32b5SKyle Evans 	if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1058251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1059251a32b5SKyle Evans 		CURVNET_RESTORE();
1060251a32b5SKyle Evans 		return (EBUSY);
1061251a32b5SKyle Evans 	}
1062251a32b5SKyle Evans 
1063c4cad154SKyle Evans 	error = tun_busy_locked(tp);
1064c4cad154SKyle Evans 	KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1065251a32b5SKyle Evans 	ifp = TUN2IFP(tp);
1066251a32b5SKyle Evans 
1067251a32b5SKyle Evans 	if ((tp->tun_flags & TUN_L2) != 0) {
1068251a32b5SKyle Evans 		bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1069251a32b5SKyle Evans 		    sizeof(tp->tun_ether.octet));
1070251a32b5SKyle Evans 
1071251a32b5SKyle Evans 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1072251a32b5SKyle Evans 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1073251a32b5SKyle Evans 
1074251a32b5SKyle Evans 		if (tapuponopen)
1075251a32b5SKyle Evans 			ifp->if_flags |= IFF_UP;
1076251a32b5SKyle Evans 	}
1077251a32b5SKyle Evans 
1078251a32b5SKyle Evans 	tp->tun_pid = td->td_proc->p_pid;
1079251a32b5SKyle Evans 	tp->tun_flags |= TUN_OPEN;
1080251a32b5SKyle Evans 
1081251a32b5SKyle Evans 	if_link_state_change(ifp, LINK_STATE_UP);
1082251a32b5SKyle Evans 	TUNDEBUG(ifp, "open\n");
1083251a32b5SKyle Evans 	TUN_UNLOCK(tp);
108460250777SKyle Evans 
108560250777SKyle Evans 	/*
108660250777SKyle Evans 	 * This can fail with either ENOENT or EBUSY.  This is in the middle of
108760250777SKyle Evans 	 * d_open, so ENOENT should not be possible.  EBUSY is possible, but
108860250777SKyle Evans 	 * the only cdevpriv dtor being set will be tundtor and the softc being
108960250777SKyle Evans 	 * passed is constant for a given cdev.  We ignore the possible error
109060250777SKyle Evans 	 * because of this as either "unlikely" or "not actually a problem."
109160250777SKyle Evans 	 */
109260250777SKyle Evans 	(void)devfs_set_cdevpriv(tp, tundtor);
1093251a32b5SKyle Evans 	CURVNET_RESTORE();
1094251a32b5SKyle Evans 	return (0);
1095251a32b5SKyle Evans }
1096251a32b5SKyle Evans 
1097251a32b5SKyle Evans /*
109860250777SKyle Evans  * tundtor - tear down the device - mark i/f down & delete
1099251a32b5SKyle Evans  * routing info
1100251a32b5SKyle Evans  */
110160250777SKyle Evans static void
tundtor(void * data)110260250777SKyle Evans tundtor(void *data)
1103251a32b5SKyle Evans {
110429128766SKyle Evans 	struct proc *p;
1105251a32b5SKyle Evans 	struct tuntap_softc *tp;
1106251a32b5SKyle Evans 	struct ifnet *ifp;
1107251a32b5SKyle Evans 	bool l2tun;
1108251a32b5SKyle Evans 
110960250777SKyle Evans 	tp = data;
111060250777SKyle Evans 	p = curproc;
1111251a32b5SKyle Evans 	ifp = TUN2IFP(tp);
1112251a32b5SKyle Evans 
1113251a32b5SKyle Evans 	TUN_LOCK(tp);
111429128766SKyle Evans 
1115251a32b5SKyle Evans 	/*
111629128766SKyle Evans 	 * Realistically, we can't be obstinate here.  This only means that the
111729128766SKyle Evans 	 * tuntap device was closed out of order, and the last closer wasn't the
111829128766SKyle Evans 	 * controller.  These are still good to know about, though, as software
111929128766SKyle Evans 	 * should avoid multiple processes with a tuntap device open and
112029128766SKyle Evans 	 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
112129128766SKyle Evans 	 * parent).
1122251a32b5SKyle Evans 	 */
112329128766SKyle Evans 	if (p->p_pid != tp->tun_pid) {
112429128766SKyle Evans 		log(LOG_INFO,
112529128766SKyle Evans 		    "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
112660250777SKyle Evans 		    p->p_pid, p->p_comm, tp->tun_dev->si_name);
1127251a32b5SKyle Evans 	}
1128251a32b5SKyle Evans 
1129251a32b5SKyle Evans 	/*
1130251a32b5SKyle Evans 	 * junk all pending output
1131251a32b5SKyle Evans 	 */
1132251a32b5SKyle Evans 	CURVNET_SET(ifp->if_vnet);
1133251a32b5SKyle Evans 
1134251a32b5SKyle Evans 	l2tun = false;
1135251a32b5SKyle Evans 	if ((tp->tun_flags & TUN_L2) != 0) {
1136251a32b5SKyle Evans 		l2tun = true;
1137251a32b5SKyle Evans 		IF_DRAIN(&ifp->if_snd);
1138251a32b5SKyle Evans 	} else {
1139251a32b5SKyle Evans 		IFQ_PURGE(&ifp->if_snd);
1140251a32b5SKyle Evans 	}
1141251a32b5SKyle Evans 
1142251a32b5SKyle Evans 	/* For vmnet, we won't do most of the address/route bits */
114316760d8eSKyle Evans 	if ((tp->tun_flags & TUN_VMNET) != 0 ||
114416760d8eSKyle Evans 	    (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1145251a32b5SKyle Evans 		goto out;
11465b0010b4SGleb Smirnoff #if defined(INET) || defined(INET6)
114799c79cabSMichael Tuexen 	if (l2tun && tp->tun_lro_ready) {
114899c79cabSMichael Tuexen 		TUNDEBUG (ifp, "LRO disabled\n");
114999c79cabSMichael Tuexen 		tcp_lro_free(&tp->tun_lro);
115099c79cabSMichael Tuexen 		tp->tun_lro_ready = false;
115199c79cabSMichael Tuexen 	}
11525b0010b4SGleb Smirnoff #endif
1153251a32b5SKyle Evans 	if (ifp->if_flags & IFF_UP) {
1154251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1155251a32b5SKyle Evans 		if_down(ifp);
1156251a32b5SKyle Evans 		TUN_LOCK(tp);
1157251a32b5SKyle Evans 	}
1158251a32b5SKyle Evans 
1159251a32b5SKyle Evans 	/* Delete all addresses and routes which reference this interface. */
1160251a32b5SKyle Evans 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1161251a32b5SKyle Evans 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1162251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1163251a32b5SKyle Evans 		if_purgeaddrs(ifp);
1164251a32b5SKyle Evans 		TUN_LOCK(tp);
1165251a32b5SKyle Evans 	}
1166251a32b5SKyle Evans 
1167251a32b5SKyle Evans out:
1168251a32b5SKyle Evans 	if_link_state_change(ifp, LINK_STATE_DOWN);
1169251a32b5SKyle Evans 	CURVNET_RESTORE();
1170251a32b5SKyle Evans 
1171251a32b5SKyle Evans 	funsetown(&tp->tun_sigio);
1172251a32b5SKyle Evans 	selwakeuppri(&tp->tun_rsel, PZERO + 1);
1173251a32b5SKyle Evans 	KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1174251a32b5SKyle Evans 	TUNDEBUG (ifp, "closed\n");
1175251a32b5SKyle Evans 	tp->tun_flags &= ~TUN_OPEN;
1176251a32b5SKyle Evans 	tp->tun_pid = 0;
1177f8bc74e2SVincenzo Maffione 	tun_vnethdr_set(ifp, 0);
1178251a32b5SKyle Evans 
1179c4cad154SKyle Evans 	tun_unbusy_locked(tp);
1180251a32b5SKyle Evans 	TUN_UNLOCK(tp);
1181251a32b5SKyle Evans }
1182251a32b5SKyle Evans 
1183251a32b5SKyle Evans static void
tuninit(struct ifnet * ifp)1184251a32b5SKyle Evans tuninit(struct ifnet *ifp)
1185251a32b5SKyle Evans {
1186251a32b5SKyle Evans 	struct tuntap_softc *tp = ifp->if_softc;
1187251a32b5SKyle Evans 
1188251a32b5SKyle Evans 	TUNDEBUG(ifp, "tuninit\n");
1189251a32b5SKyle Evans 
1190251a32b5SKyle Evans 	TUN_LOCK(tp);
1191251a32b5SKyle Evans 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1192251a32b5SKyle Evans 	if ((tp->tun_flags & TUN_L2) == 0) {
1193251a32b5SKyle Evans 		ifp->if_flags |= IFF_UP;
1194251a32b5SKyle Evans 		getmicrotime(&ifp->if_lastchange);
1195251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1196251a32b5SKyle Evans 	} else {
11975b0010b4SGleb Smirnoff #if defined(INET) || defined(INET6)
119899c79cabSMichael Tuexen 		if (tcp_lro_init(&tp->tun_lro) == 0) {
119999c79cabSMichael Tuexen 			TUNDEBUG(ifp, "LRO enabled\n");
120099c79cabSMichael Tuexen 			tp->tun_lro.ifp = ifp;
120199c79cabSMichael Tuexen 			tp->tun_lro_ready = true;
120299c79cabSMichael Tuexen 		} else {
120399c79cabSMichael Tuexen 			TUNDEBUG(ifp, "Could not enable LRO\n");
120499c79cabSMichael Tuexen 			tp->tun_lro_ready = false;
120599c79cabSMichael Tuexen 		}
12065b0010b4SGleb Smirnoff #endif
1207251a32b5SKyle Evans 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1208251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1209251a32b5SKyle Evans 		/* attempt to start output */
1210251a32b5SKyle Evans 		tunstart_l2(ifp);
1211251a32b5SKyle Evans 	}
1212251a32b5SKyle Evans 
1213251a32b5SKyle Evans }
1214251a32b5SKyle Evans 
1215251a32b5SKyle Evans /*
1216251a32b5SKyle Evans  * Used only for l2 tunnel.
1217251a32b5SKyle Evans  */
1218251a32b5SKyle Evans static void
tunifinit(void * xtp)1219251a32b5SKyle Evans tunifinit(void *xtp)
1220251a32b5SKyle Evans {
1221251a32b5SKyle Evans 	struct tuntap_softc *tp;
1222251a32b5SKyle Evans 
1223251a32b5SKyle Evans 	tp = (struct tuntap_softc *)xtp;
1224251a32b5SKyle Evans 	tuninit(tp->tun_ifp);
1225251a32b5SKyle Evans }
1226251a32b5SKyle Evans 
1227251a32b5SKyle Evans /*
1228f8bc74e2SVincenzo Maffione  * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1229f8bc74e2SVincenzo Maffione  * current value of ifp->if_capenable.
1230f8bc74e2SVincenzo Maffione  */
1231f8bc74e2SVincenzo Maffione static void
tun_caps_changed(struct ifnet * ifp)1232f8bc74e2SVincenzo Maffione tun_caps_changed(struct ifnet *ifp)
1233f8bc74e2SVincenzo Maffione {
1234f8bc74e2SVincenzo Maffione 	uint64_t hwassist = 0;
1235f8bc74e2SVincenzo Maffione 
1236f8bc74e2SVincenzo Maffione 	TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1237f8bc74e2SVincenzo Maffione 	if (ifp->if_capenable & IFCAP_TXCSUM)
1238f8bc74e2SVincenzo Maffione 		hwassist |= CSUM_TCP | CSUM_UDP;
1239f8bc74e2SVincenzo Maffione 	if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1240f8bc74e2SVincenzo Maffione 		hwassist |= CSUM_TCP_IPV6
1241f8bc74e2SVincenzo Maffione 		    | CSUM_UDP_IPV6;
1242f8bc74e2SVincenzo Maffione 	if (ifp->if_capenable & IFCAP_TSO4)
1243f8bc74e2SVincenzo Maffione 		hwassist |= CSUM_IP_TSO;
1244f8bc74e2SVincenzo Maffione 	if (ifp->if_capenable & IFCAP_TSO6)
1245f8bc74e2SVincenzo Maffione 		hwassist |= CSUM_IP6_TSO;
1246f8bc74e2SVincenzo Maffione 	ifp->if_hwassist = hwassist;
1247f8bc74e2SVincenzo Maffione }
1248f8bc74e2SVincenzo Maffione 
1249f8bc74e2SVincenzo Maffione /*
1250f8bc74e2SVincenzo Maffione  * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1251f8bc74e2SVincenzo Maffione  * if_capabilities and if_capenable as needed.
1252f8bc74e2SVincenzo Maffione  */
1253f8bc74e2SVincenzo Maffione static void
tun_vnethdr_set(struct ifnet * ifp,int vhdrlen)1254f8bc74e2SVincenzo Maffione tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1255f8bc74e2SVincenzo Maffione {
1256f8bc74e2SVincenzo Maffione 	struct tuntap_softc *tp = ifp->if_softc;
1257f8bc74e2SVincenzo Maffione 
1258f8bc74e2SVincenzo Maffione 	TUN_LOCK_ASSERT(tp);
1259f8bc74e2SVincenzo Maffione 
1260f8bc74e2SVincenzo Maffione 	if (tp->tun_vhdrlen == vhdrlen)
1261f8bc74e2SVincenzo Maffione 		return;
1262f8bc74e2SVincenzo Maffione 
1263f8bc74e2SVincenzo Maffione 	/*
1264f8bc74e2SVincenzo Maffione 	 * Update if_capabilities to reflect the
1265f8bc74e2SVincenzo Maffione 	 * functionalities offered by the virtio-net
1266f8bc74e2SVincenzo Maffione 	 * header.
1267f8bc74e2SVincenzo Maffione 	 */
1268f8bc74e2SVincenzo Maffione 	if (vhdrlen != 0)
1269f8bc74e2SVincenzo Maffione 		ifp->if_capabilities |=
1270f8bc74e2SVincenzo Maffione 			TAP_VNET_HDR_CAPS;
1271f8bc74e2SVincenzo Maffione 	else
1272f8bc74e2SVincenzo Maffione 		ifp->if_capabilities &=
1273f8bc74e2SVincenzo Maffione 			~TAP_VNET_HDR_CAPS;
1274f8bc74e2SVincenzo Maffione 	/*
1275f8bc74e2SVincenzo Maffione 	 * Disable any capabilities that we don't
1276f8bc74e2SVincenzo Maffione 	 * support anymore.
1277f8bc74e2SVincenzo Maffione 	 */
1278f8bc74e2SVincenzo Maffione 	ifp->if_capenable &= ifp->if_capabilities;
1279f8bc74e2SVincenzo Maffione 	tun_caps_changed(ifp);
1280f8bc74e2SVincenzo Maffione 	tp->tun_vhdrlen = vhdrlen;
1281f8bc74e2SVincenzo Maffione 
1282f8bc74e2SVincenzo Maffione 	TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1283f8bc74e2SVincenzo Maffione 	    vhdrlen, ifp->if_capabilities);
1284f8bc74e2SVincenzo Maffione }
1285f8bc74e2SVincenzo Maffione 
1286f8bc74e2SVincenzo Maffione /*
1287251a32b5SKyle Evans  * Process an ioctl request.
1288251a32b5SKyle Evans  */
1289251a32b5SKyle Evans static int
tunifioctl(struct ifnet * ifp,u_long cmd,caddr_t data)1290251a32b5SKyle Evans tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1291251a32b5SKyle Evans {
1292251a32b5SKyle Evans 	struct ifreq *ifr = (struct ifreq *)data;
1293251a32b5SKyle Evans 	struct tuntap_softc *tp;
1294251a32b5SKyle Evans 	struct ifstat *ifs;
1295251a32b5SKyle Evans 	struct ifmediareq	*ifmr;
1296251a32b5SKyle Evans 	int		dummy, error = 0;
1297251a32b5SKyle Evans 	bool		l2tun;
1298251a32b5SKyle Evans 
1299251a32b5SKyle Evans 	ifmr = NULL;
1300251a32b5SKyle Evans 	sx_xlock(&tun_ioctl_sx);
1301251a32b5SKyle Evans 	tp = ifp->if_softc;
1302251a32b5SKyle Evans 	if (tp == NULL) {
1303251a32b5SKyle Evans 		error = ENXIO;
1304251a32b5SKyle Evans 		goto bad;
1305251a32b5SKyle Evans 	}
1306251a32b5SKyle Evans 	l2tun = (tp->tun_flags & TUN_L2) != 0;
1307251a32b5SKyle Evans 	switch(cmd) {
1308251a32b5SKyle Evans 	case SIOCGIFSTATUS:
1309251a32b5SKyle Evans 		ifs = (struct ifstat *)data;
1310251a32b5SKyle Evans 		TUN_LOCK(tp);
1311251a32b5SKyle Evans 		if (tp->tun_pid)
1312251a32b5SKyle Evans 			snprintf(ifs->ascii, sizeof(ifs->ascii),
1313251a32b5SKyle Evans 			    "\tOpened by PID %d\n", tp->tun_pid);
1314251a32b5SKyle Evans 		else
1315251a32b5SKyle Evans 			ifs->ascii[0] = '\0';
1316251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1317251a32b5SKyle Evans 		break;
1318251a32b5SKyle Evans 	case SIOCSIFADDR:
1319251a32b5SKyle Evans 		if (l2tun)
1320251a32b5SKyle Evans 			error = ether_ioctl(ifp, cmd, data);
1321251a32b5SKyle Evans 		else
1322251a32b5SKyle Evans 			tuninit(ifp);
1323251a32b5SKyle Evans 		if (error == 0)
1324251a32b5SKyle Evans 		    TUNDEBUG(ifp, "address set\n");
1325251a32b5SKyle Evans 		break;
1326251a32b5SKyle Evans 	case SIOCSIFMTU:
1327251a32b5SKyle Evans 		ifp->if_mtu = ifr->ifr_mtu;
1328251a32b5SKyle Evans 		TUNDEBUG(ifp, "mtu set\n");
1329251a32b5SKyle Evans 		break;
1330251a32b5SKyle Evans 	case SIOCSIFFLAGS:
1331251a32b5SKyle Evans 	case SIOCADDMULTI:
1332251a32b5SKyle Evans 	case SIOCDELMULTI:
1333251a32b5SKyle Evans 		break;
1334251a32b5SKyle Evans 	case SIOCGIFMEDIA:
1335251a32b5SKyle Evans 		if (!l2tun) {
1336251a32b5SKyle Evans 			error = EINVAL;
1337251a32b5SKyle Evans 			break;
1338251a32b5SKyle Evans 		}
1339251a32b5SKyle Evans 
1340251a32b5SKyle Evans 		ifmr = (struct ifmediareq *)data;
1341251a32b5SKyle Evans 		dummy = ifmr->ifm_count;
1342251a32b5SKyle Evans 		ifmr->ifm_count = 1;
1343251a32b5SKyle Evans 		ifmr->ifm_status = IFM_AVALID;
13440ec220dfSAlexandre Snarskii 		ifmr->ifm_active = IFM_ETHER | IFM_FDX | IFM_1000_T;
1345251a32b5SKyle Evans 		if (tp->tun_flags & TUN_OPEN)
1346251a32b5SKyle Evans 			ifmr->ifm_status |= IFM_ACTIVE;
1347251a32b5SKyle Evans 		ifmr->ifm_current = ifmr->ifm_active;
1348251a32b5SKyle Evans 		if (dummy >= 1) {
1349251a32b5SKyle Evans 			int media = IFM_ETHER;
1350251a32b5SKyle Evans 			error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1351251a32b5SKyle Evans 		}
1352251a32b5SKyle Evans 		break;
1353f8bc74e2SVincenzo Maffione 	case SIOCSIFCAP:
1354f8bc74e2SVincenzo Maffione 		TUN_LOCK(tp);
1355f8bc74e2SVincenzo Maffione 		ifp->if_capenable = ifr->ifr_reqcap;
1356f8bc74e2SVincenzo Maffione 		tun_caps_changed(ifp);
1357f8bc74e2SVincenzo Maffione 		TUN_UNLOCK(tp);
1358f8bc74e2SVincenzo Maffione 		VLAN_CAPABILITIES(ifp);
1359f8bc74e2SVincenzo Maffione 		break;
1360251a32b5SKyle Evans 	default:
1361251a32b5SKyle Evans 		if (l2tun) {
1362251a32b5SKyle Evans 			error = ether_ioctl(ifp, cmd, data);
1363251a32b5SKyle Evans 		} else {
1364251a32b5SKyle Evans 			error = EINVAL;
1365251a32b5SKyle Evans 		}
1366251a32b5SKyle Evans 	}
1367251a32b5SKyle Evans bad:
1368251a32b5SKyle Evans 	sx_xunlock(&tun_ioctl_sx);
1369251a32b5SKyle Evans 	return (error);
1370251a32b5SKyle Evans }
1371251a32b5SKyle Evans 
1372251a32b5SKyle Evans /*
1373251a32b5SKyle Evans  * tunoutput - queue packets from higher level ready to put out.
1374251a32b5SKyle Evans  */
1375251a32b5SKyle Evans static int
tunoutput(struct ifnet * ifp,struct mbuf * m0,const struct sockaddr * dst,struct route * ro)1376251a32b5SKyle Evans tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1377251a32b5SKyle Evans     struct route *ro)
1378251a32b5SKyle Evans {
1379251a32b5SKyle Evans 	struct tuntap_softc *tp = ifp->if_softc;
1380251a32b5SKyle Evans 	u_short cached_tun_flags;
1381251a32b5SKyle Evans 	int error;
1382251a32b5SKyle Evans 	u_int32_t af;
1383251a32b5SKyle Evans 
1384251a32b5SKyle Evans 	TUNDEBUG (ifp, "tunoutput\n");
1385251a32b5SKyle Evans 
1386251a32b5SKyle Evans #ifdef MAC
1387251a32b5SKyle Evans 	error = mac_ifnet_check_transmit(ifp, m0);
1388251a32b5SKyle Evans 	if (error) {
1389251a32b5SKyle Evans 		m_freem(m0);
1390251a32b5SKyle Evans 		return (error);
1391251a32b5SKyle Evans 	}
1392251a32b5SKyle Evans #endif
1393251a32b5SKyle Evans 
1394251a32b5SKyle Evans 	/* Could be unlocked read? */
1395251a32b5SKyle Evans 	TUN_LOCK(tp);
1396251a32b5SKyle Evans 	cached_tun_flags = tp->tun_flags;
1397251a32b5SKyle Evans 	TUN_UNLOCK(tp);
1398251a32b5SKyle Evans 	if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1399251a32b5SKyle Evans 		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1400251a32b5SKyle Evans 		m_freem (m0);
1401251a32b5SKyle Evans 		return (EHOSTDOWN);
1402251a32b5SKyle Evans 	}
1403251a32b5SKyle Evans 
1404251a32b5SKyle Evans 	if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1405251a32b5SKyle Evans 		m_freem (m0);
1406251a32b5SKyle Evans 		return (EHOSTDOWN);
1407251a32b5SKyle Evans 	}
1408251a32b5SKyle Evans 
1409251a32b5SKyle Evans 	/* BPF writes need to be handled specially. */
14102cb0fce2SSeth Hoffert 	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
1411251a32b5SKyle Evans 		bcopy(dst->sa_data, &af, sizeof(af));
1412251a32b5SKyle Evans 	else
141362e1a437SZhenlei Huang 		af = RO_GET_FAMILY(ro, dst);
1414251a32b5SKyle Evans 
14154ffe410eSMichael Tuexen 	BPF_MTAP2(ifp, &af, sizeof(af), m0);
1416251a32b5SKyle Evans 
1417251a32b5SKyle Evans 	/* prepend sockaddr? this may abort if the mbuf allocation fails */
1418251a32b5SKyle Evans 	if (cached_tun_flags & TUN_LMODE) {
1419251a32b5SKyle Evans 		/* allocate space for sockaddr */
1420251a32b5SKyle Evans 		M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1421251a32b5SKyle Evans 
1422251a32b5SKyle Evans 		/* if allocation failed drop packet */
1423251a32b5SKyle Evans 		if (m0 == NULL) {
1424251a32b5SKyle Evans 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1425251a32b5SKyle Evans 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1426251a32b5SKyle Evans 			return (ENOBUFS);
1427251a32b5SKyle Evans 		} else {
1428251a32b5SKyle Evans 			bcopy(dst, m0->m_data, dst->sa_len);
1429251a32b5SKyle Evans 		}
1430251a32b5SKyle Evans 	}
1431251a32b5SKyle Evans 
1432251a32b5SKyle Evans 	if (cached_tun_flags & TUN_IFHEAD) {
1433251a32b5SKyle Evans 		/* Prepend the address family */
1434251a32b5SKyle Evans 		M_PREPEND(m0, 4, M_NOWAIT);
1435251a32b5SKyle Evans 
1436251a32b5SKyle Evans 		/* if allocation failed drop packet */
1437251a32b5SKyle Evans 		if (m0 == NULL) {
1438251a32b5SKyle Evans 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1439251a32b5SKyle Evans 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1440251a32b5SKyle Evans 			return (ENOBUFS);
1441251a32b5SKyle Evans 		} else
1442251a32b5SKyle Evans 			*(u_int32_t *)m0->m_data = htonl(af);
1443251a32b5SKyle Evans 	} else {
1444251a32b5SKyle Evans #ifdef INET
1445251a32b5SKyle Evans 		if (af != AF_INET)
1446251a32b5SKyle Evans #endif
1447251a32b5SKyle Evans 		{
1448251a32b5SKyle Evans 			m_freem(m0);
1449251a32b5SKyle Evans 			return (EAFNOSUPPORT);
1450251a32b5SKyle Evans 		}
1451251a32b5SKyle Evans 	}
1452251a32b5SKyle Evans 
1453251a32b5SKyle Evans 	error = (ifp->if_transmit)(ifp, m0);
1454251a32b5SKyle Evans 	if (error)
1455251a32b5SKyle Evans 		return (ENOBUFS);
1456251a32b5SKyle Evans 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1457251a32b5SKyle Evans 	return (0);
1458251a32b5SKyle Evans }
1459251a32b5SKyle Evans 
1460251a32b5SKyle Evans /*
1461251a32b5SKyle Evans  * the cdevsw interface is now pretty minimal.
1462251a32b5SKyle Evans  */
1463251a32b5SKyle Evans static	int
tunioctl(struct cdev * dev,u_long cmd,caddr_t data,int flag,struct thread * td)1464251a32b5SKyle Evans tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1465251a32b5SKyle Evans     struct thread *td)
1466251a32b5SKyle Evans {
1467251a32b5SKyle Evans 	struct ifreq ifr, *ifrp;
1468251a32b5SKyle Evans 	struct tuntap_softc *tp = dev->si_drv1;
1469f8bc74e2SVincenzo Maffione 	struct ifnet *ifp = TUN2IFP(tp);
1470251a32b5SKyle Evans 	struct tuninfo *tunp;
1471f8bc74e2SVincenzo Maffione 	int error, iflags, ival;
1472251a32b5SKyle Evans 	bool	l2tun;
1473251a32b5SKyle Evans 
1474251a32b5SKyle Evans 	l2tun = (tp->tun_flags & TUN_L2) != 0;
1475251a32b5SKyle Evans 	if (l2tun) {
1476251a32b5SKyle Evans 		/* tap specific ioctls */
1477251a32b5SKyle Evans 		switch(cmd) {
1478251a32b5SKyle Evans 		/* VMware/VMnet port ioctl's */
1479251a32b5SKyle Evans #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1480251a32b5SKyle Evans     defined(COMPAT_FREEBSD4)
1481251a32b5SKyle Evans 		case _IO('V', 0):
1482251a32b5SKyle Evans 			ival = IOCPARM_IVAL(data);
1483251a32b5SKyle Evans 			data = (caddr_t)&ival;
1484251a32b5SKyle Evans 			/* FALLTHROUGH */
1485251a32b5SKyle Evans #endif
1486251a32b5SKyle Evans 		case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1487251a32b5SKyle Evans 			iflags = *(int *)data;
1488251a32b5SKyle Evans 			iflags &= TUN_VMIO_FLAG_MASK;
1489251a32b5SKyle Evans 			iflags &= ~IFF_CANTCHANGE;
1490251a32b5SKyle Evans 			iflags |= IFF_UP;
1491251a32b5SKyle Evans 
1492251a32b5SKyle Evans 			TUN_LOCK(tp);
1493f8bc74e2SVincenzo Maffione 			ifp->if_flags = iflags |
1494f8bc74e2SVincenzo Maffione 			    (ifp->if_flags & IFF_CANTCHANGE);
1495251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1496251a32b5SKyle Evans 
1497251a32b5SKyle Evans 			return (0);
1498251a32b5SKyle Evans 		case SIOCGIFADDR:	/* get MAC address of the remote side */
1499251a32b5SKyle Evans 			TUN_LOCK(tp);
1500251a32b5SKyle Evans 			bcopy(&tp->tun_ether.octet, data,
1501251a32b5SKyle Evans 			    sizeof(tp->tun_ether.octet));
1502251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1503251a32b5SKyle Evans 
1504251a32b5SKyle Evans 			return (0);
1505251a32b5SKyle Evans 		case SIOCSIFADDR:	/* set MAC address of the remote side */
1506251a32b5SKyle Evans 			TUN_LOCK(tp);
1507251a32b5SKyle Evans 			bcopy(data, &tp->tun_ether.octet,
1508251a32b5SKyle Evans 			    sizeof(tp->tun_ether.octet));
1509251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1510251a32b5SKyle Evans 
1511251a32b5SKyle Evans 			return (0);
1512f8bc74e2SVincenzo Maffione 		case TAPSVNETHDR:
1513f8bc74e2SVincenzo Maffione 			ival = *(int *)data;
1514f8bc74e2SVincenzo Maffione 			if (ival != 0 &&
1515f8bc74e2SVincenzo Maffione 			    ival != sizeof(struct virtio_net_hdr) &&
1516f8bc74e2SVincenzo Maffione 			    ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1517f8bc74e2SVincenzo Maffione 				return (EINVAL);
1518f8bc74e2SVincenzo Maffione 			}
1519f8bc74e2SVincenzo Maffione 			TUN_LOCK(tp);
1520f8bc74e2SVincenzo Maffione 			tun_vnethdr_set(ifp, ival);
1521f8bc74e2SVincenzo Maffione 			TUN_UNLOCK(tp);
1522f8bc74e2SVincenzo Maffione 
1523f8bc74e2SVincenzo Maffione 			return (0);
1524f8bc74e2SVincenzo Maffione 		case TAPGVNETHDR:
1525f8bc74e2SVincenzo Maffione 			TUN_LOCK(tp);
1526f8bc74e2SVincenzo Maffione 			*(int *)data = tp->tun_vhdrlen;
1527f8bc74e2SVincenzo Maffione 			TUN_UNLOCK(tp);
1528f8bc74e2SVincenzo Maffione 
1529f8bc74e2SVincenzo Maffione 			return (0);
1530251a32b5SKyle Evans 		}
1531251a32b5SKyle Evans 
1532251a32b5SKyle Evans 		/* Fall through to the common ioctls if unhandled */
1533251a32b5SKyle Evans 	} else {
1534251a32b5SKyle Evans 		switch (cmd) {
1535251a32b5SKyle Evans 		case TUNSLMODE:
1536251a32b5SKyle Evans 			TUN_LOCK(tp);
1537251a32b5SKyle Evans 			if (*(int *)data) {
1538251a32b5SKyle Evans 				tp->tun_flags |= TUN_LMODE;
1539251a32b5SKyle Evans 				tp->tun_flags &= ~TUN_IFHEAD;
1540251a32b5SKyle Evans 			} else
1541251a32b5SKyle Evans 				tp->tun_flags &= ~TUN_LMODE;
1542251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1543251a32b5SKyle Evans 
1544251a32b5SKyle Evans 			return (0);
1545251a32b5SKyle Evans 		case TUNSIFHEAD:
1546251a32b5SKyle Evans 			TUN_LOCK(tp);
1547251a32b5SKyle Evans 			if (*(int *)data) {
1548251a32b5SKyle Evans 				tp->tun_flags |= TUN_IFHEAD;
1549251a32b5SKyle Evans 				tp->tun_flags &= ~TUN_LMODE;
1550251a32b5SKyle Evans 			} else
1551251a32b5SKyle Evans 				tp->tun_flags &= ~TUN_IFHEAD;
1552251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1553251a32b5SKyle Evans 
1554251a32b5SKyle Evans 			return (0);
1555251a32b5SKyle Evans 		case TUNGIFHEAD:
1556251a32b5SKyle Evans 			TUN_LOCK(tp);
1557251a32b5SKyle Evans 			*(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1558251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1559251a32b5SKyle Evans 
1560251a32b5SKyle Evans 			return (0);
1561251a32b5SKyle Evans 		case TUNSIFMODE:
1562251a32b5SKyle Evans 			/* deny this if UP */
1563251a32b5SKyle Evans 			if (TUN2IFP(tp)->if_flags & IFF_UP)
1564251a32b5SKyle Evans 				return (EBUSY);
1565251a32b5SKyle Evans 
1566251a32b5SKyle Evans 			switch (*(int *)data & ~IFF_MULTICAST) {
1567251a32b5SKyle Evans 			case IFF_POINTOPOINT:
1568251a32b5SKyle Evans 			case IFF_BROADCAST:
1569251a32b5SKyle Evans 				TUN_LOCK(tp);
1570251a32b5SKyle Evans 				TUN2IFP(tp)->if_flags &=
1571251a32b5SKyle Evans 				    ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1572251a32b5SKyle Evans 				TUN2IFP(tp)->if_flags |= *(int *)data;
1573251a32b5SKyle Evans 				TUN_UNLOCK(tp);
1574251a32b5SKyle Evans 
1575251a32b5SKyle Evans 				break;
1576251a32b5SKyle Evans 			default:
1577251a32b5SKyle Evans 				return (EINVAL);
1578251a32b5SKyle Evans 			}
1579251a32b5SKyle Evans 
1580251a32b5SKyle Evans 			return (0);
1581251a32b5SKyle Evans 		case TUNSIFPID:
1582251a32b5SKyle Evans 			TUN_LOCK(tp);
1583251a32b5SKyle Evans 			tp->tun_pid = curthread->td_proc->p_pid;
1584251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1585251a32b5SKyle Evans 
1586251a32b5SKyle Evans 			return (0);
1587251a32b5SKyle Evans 		}
1588251a32b5SKyle Evans 		/* Fall through to the common ioctls if unhandled */
1589251a32b5SKyle Evans 	}
1590251a32b5SKyle Evans 
1591251a32b5SKyle Evans 	switch (cmd) {
15920dbac71fSKyle Evans 	case TUNGIFNAME:
15930dbac71fSKyle Evans 		ifrp = (struct ifreq *)data;
15940dbac71fSKyle Evans 		strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
15950dbac71fSKyle Evans 
15960dbac71fSKyle Evans 		return (0);
1597251a32b5SKyle Evans 	case TUNSIFINFO:
1598251a32b5SKyle Evans 		tunp = (struct tuninfo *)data;
1599251a32b5SKyle Evans 		if (TUN2IFP(tp)->if_type != tunp->type)
1600251a32b5SKyle Evans 			return (EPROTOTYPE);
1601251a32b5SKyle Evans 		TUN_LOCK(tp);
1602251a32b5SKyle Evans 		if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1603251a32b5SKyle Evans 			strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1604251a32b5SKyle Evans 			ifr.ifr_mtu = tunp->mtu;
1605251a32b5SKyle Evans 			CURVNET_SET(TUN2IFP(tp)->if_vnet);
1606251a32b5SKyle Evans 			error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1607251a32b5SKyle Evans 			    (caddr_t)&ifr, td);
1608251a32b5SKyle Evans 			CURVNET_RESTORE();
1609251a32b5SKyle Evans 			if (error) {
1610251a32b5SKyle Evans 				TUN_UNLOCK(tp);
1611251a32b5SKyle Evans 				return (error);
1612251a32b5SKyle Evans 			}
1613251a32b5SKyle Evans 		}
1614251a32b5SKyle Evans 		TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1615251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1616251a32b5SKyle Evans 		break;
1617251a32b5SKyle Evans 	case TUNGIFINFO:
1618251a32b5SKyle Evans 		tunp = (struct tuninfo *)data;
1619251a32b5SKyle Evans 		TUN_LOCK(tp);
1620251a32b5SKyle Evans 		tunp->mtu = TUN2IFP(tp)->if_mtu;
1621251a32b5SKyle Evans 		tunp->type = TUN2IFP(tp)->if_type;
1622251a32b5SKyle Evans 		tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1623251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1624251a32b5SKyle Evans 		break;
1625251a32b5SKyle Evans 	case TUNSDEBUG:
1626251a32b5SKyle Evans 		tundebug = *(int *)data;
1627251a32b5SKyle Evans 		break;
1628251a32b5SKyle Evans 	case TUNGDEBUG:
1629251a32b5SKyle Evans 		*(int *)data = tundebug;
1630251a32b5SKyle Evans 		break;
1631251a32b5SKyle Evans 	case FIONBIO:
1632251a32b5SKyle Evans 		break;
1633251a32b5SKyle Evans 	case FIOASYNC:
1634251a32b5SKyle Evans 		TUN_LOCK(tp);
1635251a32b5SKyle Evans 		if (*(int *)data)
1636251a32b5SKyle Evans 			tp->tun_flags |= TUN_ASYNC;
1637251a32b5SKyle Evans 		else
1638251a32b5SKyle Evans 			tp->tun_flags &= ~TUN_ASYNC;
1639251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1640251a32b5SKyle Evans 		break;
1641251a32b5SKyle Evans 	case FIONREAD:
1642251a32b5SKyle Evans 		if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1643251a32b5SKyle Evans 			struct mbuf *mb;
1644251a32b5SKyle Evans 			IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1645251a32b5SKyle Evans 			IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1646251a32b5SKyle Evans 			for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1647251a32b5SKyle Evans 				*(int *)data += mb->m_len;
1648251a32b5SKyle Evans 			IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1649251a32b5SKyle Evans 		} else
1650251a32b5SKyle Evans 			*(int *)data = 0;
1651251a32b5SKyle Evans 		break;
1652251a32b5SKyle Evans 	case FIOSETOWN:
1653251a32b5SKyle Evans 		return (fsetown(*(int *)data, &tp->tun_sigio));
1654251a32b5SKyle Evans 
1655251a32b5SKyle Evans 	case FIOGETOWN:
1656251a32b5SKyle Evans 		*(int *)data = fgetown(&tp->tun_sigio);
1657251a32b5SKyle Evans 		return (0);
1658251a32b5SKyle Evans 
1659251a32b5SKyle Evans 	/* This is deprecated, FIOSETOWN should be used instead. */
1660251a32b5SKyle Evans 	case TIOCSPGRP:
1661251a32b5SKyle Evans 		return (fsetown(-(*(int *)data), &tp->tun_sigio));
1662251a32b5SKyle Evans 
1663251a32b5SKyle Evans 	/* This is deprecated, FIOGETOWN should be used instead. */
1664251a32b5SKyle Evans 	case TIOCGPGRP:
1665251a32b5SKyle Evans 		*(int *)data = -fgetown(&tp->tun_sigio);
1666251a32b5SKyle Evans 		return (0);
1667251a32b5SKyle Evans 
1668251a32b5SKyle Evans 	default:
1669251a32b5SKyle Evans 		return (ENOTTY);
1670251a32b5SKyle Evans 	}
1671251a32b5SKyle Evans 	return (0);
1672251a32b5SKyle Evans }
1673251a32b5SKyle Evans 
1674251a32b5SKyle Evans /*
1675251a32b5SKyle Evans  * The cdevsw read interface - reads a packet at a time, or at
1676251a32b5SKyle Evans  * least as much of a packet as can be read.
1677251a32b5SKyle Evans  */
1678251a32b5SKyle Evans static	int
tunread(struct cdev * dev,struct uio * uio,int flag)1679251a32b5SKyle Evans tunread(struct cdev *dev, struct uio *uio, int flag)
1680251a32b5SKyle Evans {
1681251a32b5SKyle Evans 	struct tuntap_softc *tp = dev->si_drv1;
1682251a32b5SKyle Evans 	struct ifnet	*ifp = TUN2IFP(tp);
1683251a32b5SKyle Evans 	struct mbuf	*m;
1684f8bc74e2SVincenzo Maffione 	size_t		len;
1685f8bc74e2SVincenzo Maffione 	int		error = 0;
1686251a32b5SKyle Evans 
1687251a32b5SKyle Evans 	TUNDEBUG (ifp, "read\n");
1688251a32b5SKyle Evans 	TUN_LOCK(tp);
1689251a32b5SKyle Evans 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1690251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1691251a32b5SKyle Evans 		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1692251a32b5SKyle Evans 		return (EHOSTDOWN);
1693251a32b5SKyle Evans 	}
1694251a32b5SKyle Evans 
1695251a32b5SKyle Evans 	tp->tun_flags &= ~TUN_RWAIT;
1696251a32b5SKyle Evans 
1697b5b83671SVincenzo Maffione 	for (;;) {
1698251a32b5SKyle Evans 		IFQ_DEQUEUE(&ifp->if_snd, m);
1699b5b83671SVincenzo Maffione 		if (m != NULL)
1700b5b83671SVincenzo Maffione 			break;
1701251a32b5SKyle Evans 		if (flag & O_NONBLOCK) {
1702251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1703251a32b5SKyle Evans 			return (EWOULDBLOCK);
1704251a32b5SKyle Evans 		}
1705251a32b5SKyle Evans 		tp->tun_flags |= TUN_RWAIT;
1706251a32b5SKyle Evans 		error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1707251a32b5SKyle Evans 		    "tunread", 0);
1708251a32b5SKyle Evans 		if (error != 0) {
1709251a32b5SKyle Evans 			TUN_UNLOCK(tp);
1710251a32b5SKyle Evans 			return (error);
1711251a32b5SKyle Evans 		}
1712251a32b5SKyle Evans 	}
1713251a32b5SKyle Evans 	TUN_UNLOCK(tp);
1714251a32b5SKyle Evans 
1715f8bc74e2SVincenzo Maffione 	len = min(tp->tun_vhdrlen, uio->uio_resid);
1716f8bc74e2SVincenzo Maffione 	if (len > 0) {
1717f8bc74e2SVincenzo Maffione 		struct virtio_net_hdr_mrg_rxbuf vhdr;
1718f8bc74e2SVincenzo Maffione 
1719f8bc74e2SVincenzo Maffione 		bzero(&vhdr, sizeof(vhdr));
1720f8bc74e2SVincenzo Maffione 		if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1721f8bc74e2SVincenzo Maffione 			m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1722f8bc74e2SVincenzo Maffione 		}
1723f8bc74e2SVincenzo Maffione 
1724f8bc74e2SVincenzo Maffione 		TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1725f8bc74e2SVincenzo Maffione 		    "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1726f8bc74e2SVincenzo Maffione 		    vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1727f8bc74e2SVincenzo Maffione 		    vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1728f8bc74e2SVincenzo Maffione 		    vhdr.hdr.csum_offset);
1729f8bc74e2SVincenzo Maffione 		error = uiomove(&vhdr, len, uio);
1730f8bc74e2SVincenzo Maffione 	}
173101c738cdSMark Johnston 	if (error == 0)
173201c738cdSMark Johnston 		error = m_mbuftouio(uio, m, 0);
1733251a32b5SKyle Evans 	m_freem(m);
1734251a32b5SKyle Evans 	return (error);
1735251a32b5SKyle Evans }
1736251a32b5SKyle Evans 
1737251a32b5SKyle Evans static int
tunwrite_l2(struct tuntap_softc * tp,struct mbuf * m,struct virtio_net_hdr_mrg_rxbuf * vhdr)1738f8bc74e2SVincenzo Maffione tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1739f8bc74e2SVincenzo Maffione 	    struct virtio_net_hdr_mrg_rxbuf *vhdr)
1740251a32b5SKyle Evans {
1741af614b8eSGleb Smirnoff 	struct epoch_tracker et;
1742251a32b5SKyle Evans 	struct ether_header *eh;
1743251a32b5SKyle Evans 	struct ifnet *ifp;
1744251a32b5SKyle Evans 
1745251a32b5SKyle Evans 	ifp = TUN2IFP(tp);
1746251a32b5SKyle Evans 
1747251a32b5SKyle Evans 	/*
1748251a32b5SKyle Evans 	 * Only pass a unicast frame to ether_input(), if it would
1749251a32b5SKyle Evans 	 * actually have been received by non-virtual hardware.
1750251a32b5SKyle Evans 	 */
1751251a32b5SKyle Evans 	if (m->m_len < sizeof(struct ether_header)) {
1752251a32b5SKyle Evans 		m_freem(m);
1753251a32b5SKyle Evans 		return (0);
1754251a32b5SKyle Evans 	}
1755251a32b5SKyle Evans 
1756251a32b5SKyle Evans 	eh = mtod(m, struct ether_header *);
1757251a32b5SKyle Evans 
175844669b76SMichael Tuexen 	if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1759251a32b5SKyle Evans 	    !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1760251a32b5SKyle Evans 	    bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1761251a32b5SKyle Evans 		m_freem(m);
1762251a32b5SKyle Evans 		return (0);
1763251a32b5SKyle Evans 	}
1764251a32b5SKyle Evans 
1765ff69d13aSMichael Tuexen 	if (vhdr != NULL) {
1766ff69d13aSMichael Tuexen 		if (virtio_net_rx_csum(m, &vhdr->hdr)) {
1767f8bc74e2SVincenzo Maffione 			m_freem(m);
1768f8bc74e2SVincenzo Maffione 			return (0);
1769f8bc74e2SVincenzo Maffione 		}
1770ff69d13aSMichael Tuexen 	} else {
1771ff69d13aSMichael Tuexen 		switch (ntohs(eh->ether_type)) {
1772ff69d13aSMichael Tuexen #ifdef INET
1773ff69d13aSMichael Tuexen 		case ETHERTYPE_IP:
1774ff69d13aSMichael Tuexen 			if (ifp->if_capenable & IFCAP_RXCSUM) {
1775ff69d13aSMichael Tuexen 				m->m_pkthdr.csum_flags |=
1776ff69d13aSMichael Tuexen 				    CSUM_IP_CHECKED | CSUM_IP_VALID |
1777ff69d13aSMichael Tuexen 				    CSUM_DATA_VALID | CSUM_SCTP_VALID |
1778ff69d13aSMichael Tuexen 				    CSUM_PSEUDO_HDR;
1779ff69d13aSMichael Tuexen 				m->m_pkthdr.csum_data = 0xffff;
1780ff69d13aSMichael Tuexen 			}
1781ff69d13aSMichael Tuexen 			break;
1782ff69d13aSMichael Tuexen #endif
1783ff69d13aSMichael Tuexen #ifdef INET6
1784ff69d13aSMichael Tuexen 		case ETHERTYPE_IPV6:
1785ff69d13aSMichael Tuexen 			if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
1786ff69d13aSMichael Tuexen 				m->m_pkthdr.csum_flags |=
1787ff69d13aSMichael Tuexen 				    CSUM_DATA_VALID_IPV6 | CSUM_SCTP_VALID |
1788ff69d13aSMichael Tuexen 				    CSUM_PSEUDO_HDR;
1789ff69d13aSMichael Tuexen 				m->m_pkthdr.csum_data = 0xffff;
1790ff69d13aSMichael Tuexen 			}
1791ff69d13aSMichael Tuexen 			break;
1792ff69d13aSMichael Tuexen #endif
1793ff69d13aSMichael Tuexen 		}
1794ff69d13aSMichael Tuexen 	}
1795f8bc74e2SVincenzo Maffione 
1796251a32b5SKyle Evans 	/* Pass packet up to parent. */
1797251a32b5SKyle Evans 	CURVNET_SET(ifp->if_vnet);
1798af614b8eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
17995b0010b4SGleb Smirnoff #if defined(INET) || defined(INET6)
18005b0010b4SGleb Smirnoff 	if (tp->tun_lro_ready && ifp->if_capenable & IFCAP_LRO &&
18015b0010b4SGleb Smirnoff 	    tcp_lro_rx(&tp->tun_lro, m, 0) == 0)
180299c79cabSMichael Tuexen 		tcp_lro_flush_all(&tp->tun_lro);
180399c79cabSMichael Tuexen 	else
18045b0010b4SGleb Smirnoff #endif
1805251a32b5SKyle Evans 		(*ifp->if_input)(ifp, m);
1806af614b8eSGleb Smirnoff 	NET_EPOCH_EXIT(et);
1807251a32b5SKyle Evans 	CURVNET_RESTORE();
1808251a32b5SKyle Evans 	/* ibytes are counted in parent */
1809251a32b5SKyle Evans 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1810251a32b5SKyle Evans 	return (0);
1811251a32b5SKyle Evans }
1812251a32b5SKyle Evans 
1813251a32b5SKyle Evans static int
tunwrite_l3(struct tuntap_softc * tp,struct mbuf * m)1814251a32b5SKyle Evans tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1815251a32b5SKyle Evans {
1816b8a6e03fSGleb Smirnoff 	struct epoch_tracker et;
1817251a32b5SKyle Evans 	struct ifnet *ifp;
1818251a32b5SKyle Evans 	int family, isr;
1819251a32b5SKyle Evans 
1820251a32b5SKyle Evans 	ifp = TUN2IFP(tp);
1821251a32b5SKyle Evans 	/* Could be unlocked read? */
1822251a32b5SKyle Evans 	TUN_LOCK(tp);
1823251a32b5SKyle Evans 	if (tp->tun_flags & TUN_IFHEAD) {
1824251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1825251a32b5SKyle Evans 		if (m->m_len < sizeof(family) &&
1826251a32b5SKyle Evans 		(m = m_pullup(m, sizeof(family))) == NULL)
1827251a32b5SKyle Evans 			return (ENOBUFS);
1828251a32b5SKyle Evans 		family = ntohl(*mtod(m, u_int32_t *));
1829251a32b5SKyle Evans 		m_adj(m, sizeof(family));
1830251a32b5SKyle Evans 	} else {
1831251a32b5SKyle Evans 		TUN_UNLOCK(tp);
1832251a32b5SKyle Evans 		family = AF_INET;
1833251a32b5SKyle Evans 	}
1834251a32b5SKyle Evans 
1835251a32b5SKyle Evans 	BPF_MTAP2(ifp, &family, sizeof(family), m);
1836251a32b5SKyle Evans 
1837251a32b5SKyle Evans 	switch (family) {
1838251a32b5SKyle Evans #ifdef INET
1839251a32b5SKyle Evans 	case AF_INET:
1840251a32b5SKyle Evans 		isr = NETISR_IP;
1841251a32b5SKyle Evans 		break;
1842251a32b5SKyle Evans #endif
1843251a32b5SKyle Evans #ifdef INET6
1844251a32b5SKyle Evans 	case AF_INET6:
1845251a32b5SKyle Evans 		isr = NETISR_IPV6;
1846251a32b5SKyle Evans 		break;
1847251a32b5SKyle Evans #endif
1848251a32b5SKyle Evans 	default:
1849251a32b5SKyle Evans 		m_freem(m);
1850251a32b5SKyle Evans 		return (EAFNOSUPPORT);
1851251a32b5SKyle Evans 	}
1852251a32b5SKyle Evans 	random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1853251a32b5SKyle Evans 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1854251a32b5SKyle Evans 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1855251a32b5SKyle Evans 	CURVNET_SET(ifp->if_vnet);
1856251a32b5SKyle Evans 	M_SETFIB(m, ifp->if_fib);
1857b8a6e03fSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1858251a32b5SKyle Evans 	netisr_dispatch(isr, m);
1859b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
1860251a32b5SKyle Evans 	CURVNET_RESTORE();
1861251a32b5SKyle Evans 	return (0);
1862251a32b5SKyle Evans }
1863251a32b5SKyle Evans 
1864251a32b5SKyle Evans /*
1865251a32b5SKyle Evans  * the cdevsw write interface - an atomic write is a packet - or else!
1866251a32b5SKyle Evans  */
1867251a32b5SKyle Evans static	int
tunwrite(struct cdev * dev,struct uio * uio,int flag)1868251a32b5SKyle Evans tunwrite(struct cdev *dev, struct uio *uio, int flag)
1869251a32b5SKyle Evans {
1870f8bc74e2SVincenzo Maffione 	struct virtio_net_hdr_mrg_rxbuf vhdr;
1871251a32b5SKyle Evans 	struct tuntap_softc *tp;
1872251a32b5SKyle Evans 	struct ifnet	*ifp;
1873251a32b5SKyle Evans 	struct mbuf	*m;
1874251a32b5SKyle Evans 	uint32_t	mru;
1875f8bc74e2SVincenzo Maffione 	int		align, vhdrlen, error;
1876251a32b5SKyle Evans 	bool		l2tun;
1877251a32b5SKyle Evans 
1878251a32b5SKyle Evans 	tp = dev->si_drv1;
1879251a32b5SKyle Evans 	ifp = TUN2IFP(tp);
1880251a32b5SKyle Evans 	TUNDEBUG(ifp, "tunwrite\n");
1881251a32b5SKyle Evans 	if ((ifp->if_flags & IFF_UP) != IFF_UP)
1882251a32b5SKyle Evans 		/* ignore silently */
1883251a32b5SKyle Evans 		return (0);
1884251a32b5SKyle Evans 
1885251a32b5SKyle Evans 	if (uio->uio_resid == 0)
1886251a32b5SKyle Evans 		return (0);
1887251a32b5SKyle Evans 
1888251a32b5SKyle Evans 	l2tun = (tp->tun_flags & TUN_L2) != 0;
1889251a32b5SKyle Evans 	mru = l2tun ? TAPMRU : TUNMRU;
1890f8bc74e2SVincenzo Maffione 	vhdrlen = tp->tun_vhdrlen;
1891f8bc74e2SVincenzo Maffione 	align = 0;
1892f8bc74e2SVincenzo Maffione 	if (l2tun) {
1893251a32b5SKyle Evans 		align = ETHER_ALIGN;
1894f8bc74e2SVincenzo Maffione 		mru += vhdrlen;
1895f8bc74e2SVincenzo Maffione 	} else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1896251a32b5SKyle Evans 		mru += sizeof(uint32_t);	/* family */
1897251a32b5SKyle Evans 	if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1898251a32b5SKyle Evans 		TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1899251a32b5SKyle Evans 		return (EIO);
1900251a32b5SKyle Evans 	}
1901251a32b5SKyle Evans 
1902f8bc74e2SVincenzo Maffione 	if (vhdrlen > 0) {
1903f8bc74e2SVincenzo Maffione 		error = uiomove(&vhdr, vhdrlen, uio);
1904f8bc74e2SVincenzo Maffione 		if (error != 0)
1905f8bc74e2SVincenzo Maffione 			return (error);
1906f8bc74e2SVincenzo Maffione 		TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1907f8bc74e2SVincenzo Maffione 		    "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1908f8bc74e2SVincenzo Maffione 		    vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1909f8bc74e2SVincenzo Maffione 		    vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1910f8bc74e2SVincenzo Maffione 		    vhdr.hdr.csum_offset);
1911f8bc74e2SVincenzo Maffione 	}
1912f8bc74e2SVincenzo Maffione 
1913251a32b5SKyle Evans 	if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1914251a32b5SKyle Evans 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1915251a32b5SKyle Evans 		return (ENOBUFS);
1916251a32b5SKyle Evans 	}
1917251a32b5SKyle Evans 
1918251a32b5SKyle Evans 	m->m_pkthdr.rcvif = ifp;
1919251a32b5SKyle Evans #ifdef MAC
1920251a32b5SKyle Evans 	mac_ifnet_create_mbuf(ifp, m);
1921251a32b5SKyle Evans #endif
1922251a32b5SKyle Evans 
1923251a32b5SKyle Evans 	if (l2tun)
1924f8bc74e2SVincenzo Maffione 		return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1925251a32b5SKyle Evans 
1926251a32b5SKyle Evans 	return (tunwrite_l3(tp, m));
1927251a32b5SKyle Evans }
1928251a32b5SKyle Evans 
1929251a32b5SKyle Evans /*
1930251a32b5SKyle Evans  * tunpoll - the poll interface, this is only useful on reads
1931251a32b5SKyle Evans  * really. The write detect always returns true, write never blocks
1932251a32b5SKyle Evans  * anyway, it either accepts the packet or drops it.
1933251a32b5SKyle Evans  */
1934251a32b5SKyle Evans static	int
tunpoll(struct cdev * dev,int events,struct thread * td)1935251a32b5SKyle Evans tunpoll(struct cdev *dev, int events, struct thread *td)
1936251a32b5SKyle Evans {
1937251a32b5SKyle Evans 	struct tuntap_softc *tp = dev->si_drv1;
1938251a32b5SKyle Evans 	struct ifnet	*ifp = TUN2IFP(tp);
1939251a32b5SKyle Evans 	int		revents = 0;
1940251a32b5SKyle Evans 
1941251a32b5SKyle Evans 	TUNDEBUG(ifp, "tunpoll\n");
1942251a32b5SKyle Evans 
1943251a32b5SKyle Evans 	if (events & (POLLIN | POLLRDNORM)) {
1944251a32b5SKyle Evans 		IFQ_LOCK(&ifp->if_snd);
1945251a32b5SKyle Evans 		if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1946251a32b5SKyle Evans 			TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1947251a32b5SKyle Evans 			revents |= events & (POLLIN | POLLRDNORM);
1948251a32b5SKyle Evans 		} else {
1949251a32b5SKyle Evans 			TUNDEBUG(ifp, "tunpoll waiting\n");
1950251a32b5SKyle Evans 			selrecord(td, &tp->tun_rsel);
1951251a32b5SKyle Evans 		}
1952251a32b5SKyle Evans 		IFQ_UNLOCK(&ifp->if_snd);
1953251a32b5SKyle Evans 	}
1954251a32b5SKyle Evans 	revents |= events & (POLLOUT | POLLWRNORM);
1955251a32b5SKyle Evans 
1956251a32b5SKyle Evans 	return (revents);
1957251a32b5SKyle Evans }
1958251a32b5SKyle Evans 
1959251a32b5SKyle Evans /*
1960251a32b5SKyle Evans  * tunkqfilter - support for the kevent() system call.
1961251a32b5SKyle Evans  */
1962251a32b5SKyle Evans static int
tunkqfilter(struct cdev * dev,struct knote * kn)1963251a32b5SKyle Evans tunkqfilter(struct cdev *dev, struct knote *kn)
1964251a32b5SKyle Evans {
1965251a32b5SKyle Evans 	struct tuntap_softc	*tp = dev->si_drv1;
1966251a32b5SKyle Evans 	struct ifnet	*ifp = TUN2IFP(tp);
1967251a32b5SKyle Evans 
1968251a32b5SKyle Evans 	switch(kn->kn_filter) {
1969251a32b5SKyle Evans 	case EVFILT_READ:
1970251a32b5SKyle Evans 		TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1971251a32b5SKyle Evans 		    ifp->if_xname, dev2unit(dev));
1972251a32b5SKyle Evans 		kn->kn_fop = &tun_read_filterops;
1973251a32b5SKyle Evans 		break;
1974251a32b5SKyle Evans 
1975251a32b5SKyle Evans 	case EVFILT_WRITE:
1976251a32b5SKyle Evans 		TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1977251a32b5SKyle Evans 		    ifp->if_xname, dev2unit(dev));
1978251a32b5SKyle Evans 		kn->kn_fop = &tun_write_filterops;
1979251a32b5SKyle Evans 		break;
1980251a32b5SKyle Evans 
1981251a32b5SKyle Evans 	default:
1982251a32b5SKyle Evans 		TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1983251a32b5SKyle Evans 		    ifp->if_xname, dev2unit(dev));
1984251a32b5SKyle Evans 		return(EINVAL);
1985251a32b5SKyle Evans 	}
1986251a32b5SKyle Evans 
1987251a32b5SKyle Evans 	kn->kn_hook = tp;
1988251a32b5SKyle Evans 	knlist_add(&tp->tun_rsel.si_note, kn, 0);
1989251a32b5SKyle Evans 
1990251a32b5SKyle Evans 	return (0);
1991251a32b5SKyle Evans }
1992251a32b5SKyle Evans 
1993251a32b5SKyle Evans /*
1994251a32b5SKyle Evans  * Return true of there is data in the interface queue.
1995251a32b5SKyle Evans  */
1996251a32b5SKyle Evans static int
tunkqread(struct knote * kn,long hint)1997251a32b5SKyle Evans tunkqread(struct knote *kn, long hint)
1998251a32b5SKyle Evans {
1999251a32b5SKyle Evans 	int			ret;
2000251a32b5SKyle Evans 	struct tuntap_softc	*tp = kn->kn_hook;
2001251a32b5SKyle Evans 	struct cdev		*dev = tp->tun_dev;
2002251a32b5SKyle Evans 	struct ifnet	*ifp = TUN2IFP(tp);
2003251a32b5SKyle Evans 
2004251a32b5SKyle Evans 	if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
2005251a32b5SKyle Evans 		TUNDEBUG(ifp,
2006251a32b5SKyle Evans 		    "%s have data in the queue.  Len = %d, minor = %#x\n",
2007251a32b5SKyle Evans 		    ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
2008251a32b5SKyle Evans 		ret = 1;
2009251a32b5SKyle Evans 	} else {
2010251a32b5SKyle Evans 		TUNDEBUG(ifp,
2011251a32b5SKyle Evans 		    "%s waiting for data, minor = %#x\n", ifp->if_xname,
2012251a32b5SKyle Evans 		    dev2unit(dev));
2013251a32b5SKyle Evans 		ret = 0;
2014251a32b5SKyle Evans 	}
2015251a32b5SKyle Evans 
2016251a32b5SKyle Evans 	return (ret);
2017251a32b5SKyle Evans }
2018251a32b5SKyle Evans 
2019251a32b5SKyle Evans /*
2020251a32b5SKyle Evans  * Always can write, always return MTU in kn->data.
2021251a32b5SKyle Evans  */
2022251a32b5SKyle Evans static int
tunkqwrite(struct knote * kn,long hint)2023251a32b5SKyle Evans tunkqwrite(struct knote *kn, long hint)
2024251a32b5SKyle Evans {
2025251a32b5SKyle Evans 	struct tuntap_softc	*tp = kn->kn_hook;
2026251a32b5SKyle Evans 	struct ifnet	*ifp = TUN2IFP(tp);
2027251a32b5SKyle Evans 
2028251a32b5SKyle Evans 	kn->kn_data = ifp->if_mtu;
2029251a32b5SKyle Evans 
2030251a32b5SKyle Evans 	return (1);
2031251a32b5SKyle Evans }
2032251a32b5SKyle Evans 
2033251a32b5SKyle Evans static void
tunkqdetach(struct knote * kn)2034251a32b5SKyle Evans tunkqdetach(struct knote *kn)
2035251a32b5SKyle Evans {
2036251a32b5SKyle Evans 	struct tuntap_softc	*tp = kn->kn_hook;
2037251a32b5SKyle Evans 
2038251a32b5SKyle Evans 	knlist_remove(&tp->tun_rsel.si_note, kn, 0);
2039251a32b5SKyle Evans }
2040