xref: /freebsd/sys/net/if_tuntap.c (revision 4ad8cb68134a393f1080757aabaf959391a3a238)
1 /*	$NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $	*/
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
6  * All rights reserved.
7  * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * BASED ON:
32  * -------------------------------------------------------------------------
33  *
34  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
35  * Nottingham University 1987.
36  *
37  * This source may be freely distributed, however I would be interested
38  * in any changes that are made.
39  *
40  * This driver takes packets off the IP i/f and hands them up to a
41  * user process to have its wicked way with. This driver has it's
42  * roots in a similar driver written by Phil Cockcroft (formerly) at
43  * UCL. This driver is based much more on read/write/poll mode of
44  * operation though.
45  *
46  * $FreeBSD$
47  */
48 
49 #include "opt_inet.h"
50 #include "opt_inet6.h"
51 
52 #include <sys/param.h>
53 #include <sys/lock.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/systm.h>
57 #include <sys/jail.h>
58 #include <sys/mbuf.h>
59 #include <sys/module.h>
60 #include <sys/socket.h>
61 #include <sys/eventhandler.h>
62 #include <sys/fcntl.h>
63 #include <sys/filio.h>
64 #include <sys/sockio.h>
65 #include <sys/sx.h>
66 #include <sys/syslog.h>
67 #include <sys/ttycom.h>
68 #include <sys/poll.h>
69 #include <sys/selinfo.h>
70 #include <sys/signalvar.h>
71 #include <sys/filedesc.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
74 #include <sys/conf.h>
75 #include <sys/uio.h>
76 #include <sys/malloc.h>
77 #include <sys/random.h>
78 #include <sys/ctype.h>
79 
80 #include <net/ethernet.h>
81 #include <net/if.h>
82 #include <net/if_var.h>
83 #include <net/if_clone.h>
84 #include <net/if_dl.h>
85 #include <net/if_media.h>
86 #include <net/if_types.h>
87 #include <net/if_vlan_var.h>
88 #include <net/netisr.h>
89 #include <net/route.h>
90 #include <net/vnet.h>
91 #ifdef INET
92 #include <netinet/in.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet/udp.h>
97 #include <netinet/tcp.h>
98 #endif
99 #include <net/bpf.h>
100 #include <net/if_tap.h>
101 #include <net/if_tun.h>
102 
103 #include <dev/virtio/network/virtio_net.h>
104 
105 #include <sys/queue.h>
106 #include <sys/condvar.h>
107 #include <security/mac/mac_framework.h>
108 
109 struct tuntap_driver;
110 
111 /*
112  * tun_list is protected by global tunmtx.  Other mutable fields are
113  * protected by tun->tun_mtx, or by their owning subsystem.  tun_dev is
114  * static for the duration of a tunnel interface.
115  */
116 struct tuntap_softc {
117 	TAILQ_ENTRY(tuntap_softc)	 tun_list;
118 	struct cdev			*tun_alias;
119 	struct cdev			*tun_dev;
120 	u_short				 tun_flags;	/* misc flags */
121 #define	TUN_OPEN	0x0001
122 #define	TUN_INITED	0x0002
123 #define	TUN_IASET	0x0008
124 #define	TUN_DSTADDR	0x0010
125 #define	TUN_LMODE	0x0020
126 #define	TUN_RWAIT	0x0040
127 #define	TUN_ASYNC	0x0080
128 #define	TUN_IFHEAD	0x0100
129 #define	TUN_DYING	0x0200
130 #define	TUN_L2		0x0400
131 #define	TUN_VMNET	0x0800
132 
133 #define	TUN_DRIVER_IDENT_MASK	(TUN_L2 | TUN_VMNET)
134 #define	TUN_READY		(TUN_OPEN | TUN_INITED)
135 
136 	pid_t			 tun_pid;	/* owning pid */
137 	struct ifnet		*tun_ifp;	/* the interface */
138 	struct sigio		*tun_sigio;	/* async I/O info */
139 	struct tuntap_driver	*tun_drv;	/* appropriate driver */
140 	struct selinfo		 tun_rsel;	/* read select */
141 	struct mtx		 tun_mtx;	/* softc field mutex */
142 	struct cv		 tun_cv;	/* for ref'd dev destroy */
143 	struct ether_addr	 tun_ether;	/* remote address */
144 	int			 tun_busy;	/* busy count */
145 	int			 tun_vhdrlen;	/* virtio-net header length */
146 };
147 #define	TUN2IFP(sc)	((sc)->tun_ifp)
148 
149 #define	TUNDEBUG	if (tundebug) if_printf
150 
151 #define	TUN_LOCK(tp)		mtx_lock(&(tp)->tun_mtx)
152 #define	TUN_UNLOCK(tp)		mtx_unlock(&(tp)->tun_mtx)
153 #define	TUN_LOCK_ASSERT(tp)	mtx_assert(&(tp)->tun_mtx, MA_OWNED);
154 
155 #define	TUN_VMIO_FLAG_MASK	0x0fff
156 
157 /*
158  * Interface capabilities of a tap device that supports the virtio-net
159  * header.
160  */
161 #define TAP_VNET_HDR_CAPS	(IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6	\
162 				| IFCAP_VLAN_HWCSUM			\
163 				| IFCAP_TSO | IFCAP_LRO			\
164 				| IFCAP_VLAN_HWTSO)
165 
166 #define TAP_ALL_OFFLOAD		(CSUM_TSO | CSUM_TCP | CSUM_UDP |\
167 				    CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
168 
169 
170 /*
171  * All mutable global variables in if_tun are locked using tunmtx, with
172  * the exception of tundebug, which is used unlocked, and the drivers' *clones,
173  * which are static after setup.
174  */
175 static struct mtx tunmtx;
176 static eventhandler_tag arrival_tag;
177 static eventhandler_tag clone_tag;
178 static const char tunname[] = "tun";
179 static const char tapname[] = "tap";
180 static const char vmnetname[] = "vmnet";
181 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
182 static int tundebug = 0;
183 static int tundclone = 1;
184 static int tap_allow_uopen = 0;	/* allow user open() */
185 static int tapuponopen = 0;	/* IFF_UP on open() */
186 static int tapdclone = 1;	/* enable devfs cloning */
187 
188 static TAILQ_HEAD(,tuntap_softc)	tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
189 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
190 
191 static struct sx tun_ioctl_sx;
192 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
193 
194 SYSCTL_DECL(_net_link);
195 /* tun */
196 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
197     "IP tunnel software network interface");
198 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
199     "Enable legacy devfs interface creation");
200 
201 /* tap */
202 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
203     "Ethernet tunnel software network interface");
204 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
205     "Allow user to open /dev/tap (based on node permissions)");
206 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
207     "Bring interface up when /dev/tap is opened");
208 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
209     "Enable legacy devfs interface creation");
210 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
211 
212 static int	tun_busy_locked(struct tuntap_softc *tp);
213 static void	tun_unbusy_locked(struct tuntap_softc *tp);
214 static int	tun_busy(struct tuntap_softc *tp);
215 static void	tun_unbusy(struct tuntap_softc *tp);
216 
217 static int	tuntap_name2info(const char *name, int *unit, int *flags);
218 static void	tunclone(void *arg, struct ucred *cred, char *name,
219 		    int namelen, struct cdev **dev);
220 static void	tuncreate(struct cdev *dev, struct tuntap_driver *);
221 static void	tunrename(void *arg, struct ifnet *ifp);
222 static int	tunifioctl(struct ifnet *, u_long, caddr_t);
223 static void	tuninit(struct ifnet *);
224 static void	tunifinit(void *xtp);
225 static int	tuntapmodevent(module_t, int, void *);
226 static int	tunoutput(struct ifnet *, struct mbuf *,
227 		    const struct sockaddr *, struct route *ro);
228 static void	tunstart(struct ifnet *);
229 static void	tunstart_l2(struct ifnet *);
230 
231 static int	tun_clone_match(struct if_clone *ifc, const char *name);
232 static int	tap_clone_match(struct if_clone *ifc, const char *name);
233 static int	vmnet_clone_match(struct if_clone *ifc, const char *name);
234 static int	tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
235 static int	tun_clone_destroy(struct if_clone *, struct ifnet *);
236 static void	tun_vnethdr_set(struct ifnet *ifp, int vhdrlen);
237 
238 static d_open_t		tunopen;
239 static d_close_t	tunclose;
240 static d_read_t		tunread;
241 static d_write_t	tunwrite;
242 static d_ioctl_t	tunioctl;
243 static d_poll_t		tunpoll;
244 static d_kqfilter_t	tunkqfilter;
245 
246 static int		tunkqread(struct knote *, long);
247 static int		tunkqwrite(struct knote *, long);
248 static void		tunkqdetach(struct knote *);
249 
250 static struct filterops tun_read_filterops = {
251 	.f_isfd =	1,
252 	.f_attach =	NULL,
253 	.f_detach =	tunkqdetach,
254 	.f_event =	tunkqread,
255 };
256 
257 static struct filterops tun_write_filterops = {
258 	.f_isfd =	1,
259 	.f_attach =	NULL,
260 	.f_detach =	tunkqdetach,
261 	.f_event =	tunkqwrite,
262 };
263 
264 static struct tuntap_driver {
265 	struct cdevsw		 cdevsw;
266 	int			 ident_flags;
267 	struct unrhdr		*unrhdr;
268 	struct clonedevs	*clones;
269 	ifc_match_t		*clone_match_fn;
270 	ifc_create_t		*clone_create_fn;
271 	ifc_destroy_t		*clone_destroy_fn;
272 } tuntap_drivers[] = {
273 	{
274 		.ident_flags =	0,
275 		.cdevsw =	{
276 		    .d_version =	D_VERSION,
277 		    .d_flags =		D_NEEDMINOR,
278 		    .d_open =		tunopen,
279 		    .d_close =		tunclose,
280 		    .d_read =		tunread,
281 		    .d_write =		tunwrite,
282 		    .d_ioctl =		tunioctl,
283 		    .d_poll =		tunpoll,
284 		    .d_kqfilter =	tunkqfilter,
285 		    .d_name =		tunname,
286 		},
287 		.clone_match_fn =	tun_clone_match,
288 		.clone_create_fn =	tun_clone_create,
289 		.clone_destroy_fn =	tun_clone_destroy,
290 	},
291 	{
292 		.ident_flags =	TUN_L2,
293 		.cdevsw =	{
294 		    .d_version =	D_VERSION,
295 		    .d_flags =		D_NEEDMINOR,
296 		    .d_open =		tunopen,
297 		    .d_close =		tunclose,
298 		    .d_read =		tunread,
299 		    .d_write =		tunwrite,
300 		    .d_ioctl =		tunioctl,
301 		    .d_poll =		tunpoll,
302 		    .d_kqfilter =	tunkqfilter,
303 		    .d_name =		tapname,
304 		},
305 		.clone_match_fn =	tap_clone_match,
306 		.clone_create_fn =	tun_clone_create,
307 		.clone_destroy_fn =	tun_clone_destroy,
308 	},
309 	{
310 		.ident_flags =	TUN_L2 | TUN_VMNET,
311 		.cdevsw =	{
312 		    .d_version =	D_VERSION,
313 		    .d_flags =		D_NEEDMINOR,
314 		    .d_open =		tunopen,
315 		    .d_close =		tunclose,
316 		    .d_read =		tunread,
317 		    .d_write =		tunwrite,
318 		    .d_ioctl =		tunioctl,
319 		    .d_poll =		tunpoll,
320 		    .d_kqfilter =	tunkqfilter,
321 		    .d_name =		vmnetname,
322 		},
323 		.clone_match_fn =	vmnet_clone_match,
324 		.clone_create_fn =	tun_clone_create,
325 		.clone_destroy_fn =	tun_clone_destroy,
326 	},
327 };
328 
329 struct tuntap_driver_cloner {
330 	SLIST_ENTRY(tuntap_driver_cloner)	 link;
331 	struct tuntap_driver			*drv;
332 	struct if_clone				*cloner;
333 };
334 
335 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
336     SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
337 
338 #define	V_tuntap_driver_cloners	VNET(tuntap_driver_cloners)
339 
340 /*
341  * Mechanism for marking a tunnel device as busy so that we can safely do some
342  * orthogonal operations (such as operations on devices) without racing against
343  * tun_destroy.  tun_destroy will wait on the condvar if we're at all busy or
344  * open, to be woken up when the condition is alleviated.
345  */
346 static int
347 tun_busy_locked(struct tuntap_softc *tp)
348 {
349 
350 	TUN_LOCK_ASSERT(tp);
351 	if ((tp->tun_flags & TUN_DYING) != 0) {
352 		/*
353 		 * Perhaps unintuitive, but the device is busy going away.
354 		 * Other interpretations of EBUSY from tun_busy make little
355 		 * sense, since making a busy device even more busy doesn't
356 		 * sound like a problem.
357 		 */
358 		return (EBUSY);
359 	}
360 
361 	++tp->tun_busy;
362 	return (0);
363 }
364 
365 static void
366 tun_unbusy_locked(struct tuntap_softc *tp)
367 {
368 
369 	TUN_LOCK_ASSERT(tp);
370 	KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
371 
372 	--tp->tun_busy;
373 	/* Wake up anything that may be waiting on our busy tunnel. */
374 	if (tp->tun_busy == 0)
375 		cv_broadcast(&tp->tun_cv);
376 }
377 
378 static int
379 tun_busy(struct tuntap_softc *tp)
380 {
381 	int ret;
382 
383 	TUN_LOCK(tp);
384 	ret = tun_busy_locked(tp);
385 	TUN_UNLOCK(tp);
386 	return (ret);
387 }
388 
389 
390 static void
391 tun_unbusy(struct tuntap_softc *tp)
392 {
393 
394 	TUN_LOCK(tp);
395 	tun_unbusy_locked(tp);
396 	TUN_UNLOCK(tp);
397 }
398 
399 /*
400  * Sets unit and/or flags given the device name.  Must be called with correct
401  * vnet context.
402  */
403 static int
404 tuntap_name2info(const char *name, int *outunit, int *outflags)
405 {
406 	struct tuntap_driver *drv;
407 	struct tuntap_driver_cloner *drvc;
408 	char *dname;
409 	int flags, unit;
410 	bool found;
411 
412 	if (name == NULL)
413 		return (EINVAL);
414 
415 	/*
416 	 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
417 	 * wants to be able to pass back a char * through the second param. We
418 	 * will always set that as NULL here, so we'll fake it.
419 	 */
420 	dname = __DECONST(char *, name);
421 	found = false;
422 
423 	KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
424 	    ("tuntap_driver_cloners failed to initialize"));
425 	SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
426 		KASSERT(drvc->drv != NULL,
427 		    ("tuntap_driver_cloners entry not properly initialized"));
428 		drv = drvc->drv;
429 
430 		if (strcmp(name, drv->cdevsw.d_name) == 0) {
431 			found = true;
432 			unit = -1;
433 			flags = drv->ident_flags;
434 			break;
435 		}
436 
437 		if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
438 			found = true;
439 			flags = drv->ident_flags;
440 			break;
441 		}
442 	}
443 
444 	if (!found)
445 		return (ENXIO);
446 
447 	if (outunit != NULL)
448 		*outunit = unit;
449 	if (outflags != NULL)
450 		*outflags = flags;
451 	return (0);
452 }
453 
454 /*
455  * Get driver information from a set of flags specified.  Masks the identifying
456  * part of the flags and compares it against all of the available
457  * tuntap_drivers. Must be called with correct vnet context.
458  */
459 static struct tuntap_driver *
460 tuntap_driver_from_flags(int tun_flags)
461 {
462 	struct tuntap_driver *drv;
463 	struct tuntap_driver_cloner *drvc;
464 
465 	KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
466 	    ("tuntap_driver_cloners failed to initialize"));
467 	SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
468 		KASSERT(drvc->drv != NULL,
469 		    ("tuntap_driver_cloners entry not properly initialized"));
470 		drv = drvc->drv;
471 		if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->ident_flags)
472 			return (drv);
473 	}
474 
475 	return (NULL);
476 }
477 
478 
479 
480 static int
481 tun_clone_match(struct if_clone *ifc, const char *name)
482 {
483 	int tunflags;
484 
485 	if (tuntap_name2info(name, NULL, &tunflags) == 0) {
486 		if ((tunflags & TUN_L2) == 0)
487 			return (1);
488 	}
489 
490 	return (0);
491 }
492 
493 static int
494 tap_clone_match(struct if_clone *ifc, const char *name)
495 {
496 	int tunflags;
497 
498 	if (tuntap_name2info(name, NULL, &tunflags) == 0) {
499 		if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
500 			return (1);
501 	}
502 
503 	return (0);
504 }
505 
506 static int
507 vmnet_clone_match(struct if_clone *ifc, const char *name)
508 {
509 	int tunflags;
510 
511 	if (tuntap_name2info(name, NULL, &tunflags) == 0) {
512 		if ((tunflags & TUN_VMNET) != 0)
513 			return (1);
514 	}
515 
516 	return (0);
517 }
518 
519 static int
520 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
521 {
522 	struct tuntap_driver *drv;
523 	struct cdev *dev;
524 	int err, i, tunflags, unit;
525 
526 	tunflags = 0;
527 	/* The name here tells us exactly what we're creating */
528 	err = tuntap_name2info(name, &unit, &tunflags);
529 	if (err != 0)
530 		return (err);
531 
532 	drv = tuntap_driver_from_flags(tunflags);
533 	if (drv == NULL)
534 		return (ENXIO);
535 
536 	if (unit != -1) {
537 		/* If this unit number is still available that's okay. */
538 		if (alloc_unr_specific(drv->unrhdr, unit) == -1)
539 			return (EEXIST);
540 	} else {
541 		unit = alloc_unr(drv->unrhdr);
542 	}
543 
544 	snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
545 
546 	/* find any existing device, or allocate new unit number */
547 	i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
548 	if (i) {
549 		/* No preexisting struct cdev *, create one */
550 		dev = make_dev(&drv->cdevsw, unit, UID_UUCP, GID_DIALER, 0600,
551 		    "%s%d", drv->cdevsw.d_name, unit);
552 	}
553 
554 	tuncreate(dev, drv);
555 
556 	return (0);
557 }
558 
559 static void
560 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
561     struct cdev **dev)
562 {
563 	char devname[SPECNAMELEN + 1];
564 	struct tuntap_driver *drv;
565 	int append_unit, i, u, tunflags;
566 	bool mayclone;
567 
568 	if (*dev != NULL)
569 		return;
570 
571 	tunflags = 0;
572 	CURVNET_SET(CRED_TO_VNET(cred));
573 	if (tuntap_name2info(name, &u, &tunflags) != 0)
574 		goto out;	/* Not recognized */
575 
576 	if (u != -1 && u > IF_MAXUNIT)
577 		goto out;	/* Unit number too high */
578 
579 	mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
580 	if ((tunflags & TUN_L2) != 0) {
581 		/* tap/vmnet allow user open with a sysctl */
582 		mayclone = (mayclone || tap_allow_uopen) && tapdclone;
583 	} else {
584 		mayclone = mayclone && tundclone;
585 	}
586 
587 	/*
588 	 * If tun cloning is enabled, only the superuser can create an
589 	 * interface.
590 	 */
591 	if (!mayclone)
592 		goto out;
593 
594 	if (u == -1)
595 		append_unit = 1;
596 	else
597 		append_unit = 0;
598 
599 	drv = tuntap_driver_from_flags(tunflags);
600 	if (drv == NULL)
601 		goto out;
602 
603 	/* find any existing device, or allocate new unit number */
604 	i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
605 	if (i) {
606 		if (append_unit) {
607 			namelen = snprintf(devname, sizeof(devname), "%s%d",
608 			    name, u);
609 			name = devname;
610 		}
611 		/* No preexisting struct cdev *, create one */
612 		*dev = make_dev_credf(MAKEDEV_REF, &drv->cdevsw, u, cred,
613 		    UID_UUCP, GID_DIALER, 0600, "%s", name);
614 	}
615 
616 	if_clone_create(name, namelen, NULL);
617 out:
618 	CURVNET_RESTORE();
619 }
620 
621 static void
622 tun_destroy(struct tuntap_softc *tp)
623 {
624 
625 	TUN_LOCK(tp);
626 	tp->tun_flags |= TUN_DYING;
627 	if (tp->tun_busy != 0)
628 		cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
629 	else
630 		TUN_UNLOCK(tp);
631 
632 	CURVNET_SET(TUN2IFP(tp)->if_vnet);
633 
634 	/* destroy_dev will take care of any alias. */
635 	destroy_dev(tp->tun_dev);
636 	seldrain(&tp->tun_rsel);
637 	knlist_clear(&tp->tun_rsel.si_note, 0);
638 	knlist_destroy(&tp->tun_rsel.si_note);
639 	if ((tp->tun_flags & TUN_L2) != 0) {
640 		ether_ifdetach(TUN2IFP(tp));
641 	} else {
642 		bpfdetach(TUN2IFP(tp));
643 		if_detach(TUN2IFP(tp));
644 	}
645 	sx_xlock(&tun_ioctl_sx);
646 	TUN2IFP(tp)->if_softc = NULL;
647 	sx_xunlock(&tun_ioctl_sx);
648 	free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
649 	if_free(TUN2IFP(tp));
650 	mtx_destroy(&tp->tun_mtx);
651 	cv_destroy(&tp->tun_cv);
652 	free(tp, M_TUN);
653 	CURVNET_RESTORE();
654 }
655 
656 static int
657 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
658 {
659 	struct tuntap_softc *tp = ifp->if_softc;
660 
661 	mtx_lock(&tunmtx);
662 	TAILQ_REMOVE(&tunhead, tp, tun_list);
663 	mtx_unlock(&tunmtx);
664 	tun_destroy(tp);
665 
666 	return (0);
667 }
668 
669 static void
670 vnet_tun_init(const void *unused __unused)
671 {
672 	struct tuntap_driver *drv;
673 	struct tuntap_driver_cloner *drvc;
674 	int i;
675 
676 	for (i = 0; i < nitems(tuntap_drivers); ++i) {
677 		drv = &tuntap_drivers[i];
678 		drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
679 
680 		drvc->drv = drv;
681 		drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
682 		    drv->clone_match_fn, drv->clone_create_fn,
683 		    drv->clone_destroy_fn);
684 		SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
685 	};
686 }
687 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
688 		vnet_tun_init, NULL);
689 
690 static void
691 vnet_tun_uninit(const void *unused __unused)
692 {
693 	struct tuntap_driver_cloner *drvc;
694 
695 	while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
696 		drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
697 		SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
698 
699 		if_clone_detach(drvc->cloner);
700 		free(drvc, M_TUN);
701 	}
702 }
703 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
704     vnet_tun_uninit, NULL);
705 
706 static void
707 tun_uninit(const void *unused __unused)
708 {
709 	struct tuntap_driver *drv;
710 	struct tuntap_softc *tp;
711 	int i;
712 
713 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
714 	EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
715 	drain_dev_clone_events();
716 
717 	mtx_lock(&tunmtx);
718 	while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
719 		TAILQ_REMOVE(&tunhead, tp, tun_list);
720 		mtx_unlock(&tunmtx);
721 		tun_destroy(tp);
722 		mtx_lock(&tunmtx);
723 	}
724 	mtx_unlock(&tunmtx);
725 	for (i = 0; i < nitems(tuntap_drivers); ++i) {
726 		drv = &tuntap_drivers[i];
727 		delete_unrhdr(drv->unrhdr);
728 		clone_cleanup(&drv->clones);
729 	}
730 	mtx_destroy(&tunmtx);
731 }
732 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
733 
734 static struct tuntap_driver *
735 tuntap_driver_from_ifnet(const struct ifnet *ifp)
736 {
737 	struct tuntap_driver *drv;
738 	int i;
739 
740 	if (ifp == NULL)
741 		return (NULL);
742 
743 	for (i = 0; i < nitems(tuntap_drivers); ++i) {
744 		drv = &tuntap_drivers[i];
745 		if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
746 			return (drv);
747 	}
748 
749 	return (NULL);
750 }
751 
752 static int
753 tuntapmodevent(module_t mod, int type, void *data)
754 {
755 	struct tuntap_driver *drv;
756 	int i;
757 
758 	switch (type) {
759 	case MOD_LOAD:
760 		mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
761 		for (i = 0; i < nitems(tuntap_drivers); ++i) {
762 			drv = &tuntap_drivers[i];
763 			clone_setup(&drv->clones);
764 			drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
765 		}
766 		arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
767 		   tunrename, 0, 1000);
768 		if (arrival_tag == NULL)
769 			return (ENOMEM);
770 		clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
771 		if (clone_tag == NULL)
772 			return (ENOMEM);
773 		break;
774 	case MOD_UNLOAD:
775 		/* See tun_uninit, so it's done after the vnet_sysuninit() */
776 		break;
777 	default:
778 		return EOPNOTSUPP;
779 	}
780 	return 0;
781 }
782 
783 static moduledata_t tuntap_mod = {
784 	"if_tuntap",
785 	tuntapmodevent,
786 	0
787 };
788 
789 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
790 MODULE_VERSION(if_tuntap, 1);
791 MODULE_VERSION(if_tun, 1);
792 MODULE_VERSION(if_tap, 1);
793 
794 static void
795 tunstart(struct ifnet *ifp)
796 {
797 	struct tuntap_softc *tp = ifp->if_softc;
798 	struct mbuf *m;
799 
800 	TUNDEBUG(ifp, "starting\n");
801 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
802 		IFQ_LOCK(&ifp->if_snd);
803 		IFQ_POLL_NOLOCK(&ifp->if_snd, m);
804 		if (m == NULL) {
805 			IFQ_UNLOCK(&ifp->if_snd);
806 			return;
807 		}
808 		IFQ_UNLOCK(&ifp->if_snd);
809 	}
810 
811 	TUN_LOCK(tp);
812 	if (tp->tun_flags & TUN_RWAIT) {
813 		tp->tun_flags &= ~TUN_RWAIT;
814 		wakeup(tp);
815 	}
816 	selwakeuppri(&tp->tun_rsel, PZERO + 1);
817 	KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
818 	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
819 		TUN_UNLOCK(tp);
820 		pgsigio(&tp->tun_sigio, SIGIO, 0);
821 	} else
822 		TUN_UNLOCK(tp);
823 }
824 
825 /*
826  * tunstart_l2
827  *
828  * queue packets from higher level ready to put out
829  */
830 static void
831 tunstart_l2(struct ifnet *ifp)
832 {
833 	struct tuntap_softc	*tp = ifp->if_softc;
834 
835 	TUNDEBUG(ifp, "starting\n");
836 
837 	/*
838 	 * do not junk pending output if we are in VMnet mode.
839 	 * XXX: can this do any harm because of queue overflow?
840 	 */
841 
842 	TUN_LOCK(tp);
843 	if (((tp->tun_flags & TUN_VMNET) == 0) &&
844 	    ((tp->tun_flags & TUN_READY) != TUN_READY)) {
845 		struct mbuf *m;
846 
847 		/* Unlocked read. */
848 		TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
849 
850 		for (;;) {
851 			IF_DEQUEUE(&ifp->if_snd, m);
852 			if (m != NULL) {
853 				m_freem(m);
854 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
855 			} else
856 				break;
857 		}
858 		TUN_UNLOCK(tp);
859 
860 		return;
861 	}
862 
863 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
864 
865 	if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
866 		if (tp->tun_flags & TUN_RWAIT) {
867 			tp->tun_flags &= ~TUN_RWAIT;
868 			wakeup(tp);
869 		}
870 
871 		if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
872 			TUN_UNLOCK(tp);
873 			pgsigio(&tp->tun_sigio, SIGIO, 0);
874 			TUN_LOCK(tp);
875 		}
876 
877 		selwakeuppri(&tp->tun_rsel, PZERO+1);
878 		KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
879 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
880 	}
881 
882 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
883 	TUN_UNLOCK(tp);
884 } /* tunstart_l2 */
885 
886 
887 /* XXX: should return an error code so it can fail. */
888 static void
889 tuncreate(struct cdev *dev, struct tuntap_driver *drv)
890 {
891 	struct tuntap_softc *sc;
892 	struct ifnet *ifp;
893 	struct ether_addr eaddr;
894 	int iflags;
895 	u_char type;
896 
897 	sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
898 	mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
899 	cv_init(&sc->tun_cv, "tun_condvar");
900 	sc->tun_flags = drv->ident_flags;
901 	sc->tun_dev = dev;
902 	sc->tun_drv = drv;
903 	mtx_lock(&tunmtx);
904 	TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
905 	mtx_unlock(&tunmtx);
906 
907 	iflags = IFF_MULTICAST;
908 	if ((sc->tun_flags & TUN_L2) != 0) {
909 		type = IFT_ETHER;
910 		iflags |= IFF_BROADCAST | IFF_SIMPLEX;
911 	} else {
912 		type = IFT_PPP;
913 		iflags |= IFF_POINTOPOINT;
914 	}
915 	ifp = sc->tun_ifp = if_alloc(type);
916 	if (ifp == NULL)
917 		panic("%s%d: failed to if_alloc() interface.\n",
918 		    drv->cdevsw.d_name, dev2unit(dev));
919 	ifp->if_softc = sc;
920 	if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
921 	ifp->if_ioctl = tunifioctl;
922 	ifp->if_flags = iflags;
923 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
924 	knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
925 	ifp->if_capabilities |= IFCAP_LINKSTATE;
926 	ifp->if_capenable |= IFCAP_LINKSTATE;
927 
928 	if ((sc->tun_flags & TUN_L2) != 0) {
929 		ifp->if_mtu = ETHERMTU;
930 		ifp->if_init = tunifinit;
931 		ifp->if_start = tunstart_l2;
932 
933 		ether_gen_addr(ifp, &eaddr);
934 		ether_ifattach(ifp, eaddr.octet);
935 	} else {
936 		ifp->if_mtu = TUNMTU;
937 		ifp->if_start = tunstart;
938 		ifp->if_output = tunoutput;
939 
940 		ifp->if_snd.ifq_drv_maxlen = 0;
941 		IFQ_SET_READY(&ifp->if_snd);
942 
943 		if_attach(ifp);
944 		bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
945 	}
946 	dev->si_drv1 = sc;
947 
948 	TUN_LOCK(sc);
949 	sc->tun_flags |= TUN_INITED;
950 	TUN_UNLOCK(sc);
951 
952 	TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
953 	    ifp->if_xname, dev2unit(dev));
954 }
955 
956 static void
957 tunrename(void *arg __unused, struct ifnet *ifp)
958 {
959 	struct tuntap_softc *tp;
960 	int error;
961 
962 	if ((ifp->if_flags & IFF_RENAMING) == 0)
963 		return;
964 
965 	if (tuntap_driver_from_ifnet(ifp) == NULL)
966 		return;
967 
968 	/*
969 	 * We need to grab the ioctl sx long enough to make sure the softc is
970 	 * still there.  If it is, we can safely try to busy the tun device.
971 	 * The busy may fail if the device is currently dying, in which case
972 	 * we do nothing.  If it doesn't fail, the busy count stops the device
973 	 * from dying until we've created the alias (that will then be
974 	 * subsequently destroyed).
975 	 */
976 	sx_xlock(&tun_ioctl_sx);
977 	tp = ifp->if_softc;
978 	if (tp == NULL) {
979 		sx_xunlock(&tun_ioctl_sx);
980 		return;
981 	}
982 	error = tun_busy(tp);
983 	sx_xunlock(&tun_ioctl_sx);
984 	if (error != 0)
985 		return;
986 	if (tp->tun_alias != NULL) {
987 		destroy_dev(tp->tun_alias);
988 		tp->tun_alias = NULL;
989 	}
990 
991 	if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
992 		goto out;
993 
994 	/*
995 	 * Failure's ok, aliases are created on a best effort basis.  If a
996 	 * tun user/consumer decides to rename the interface to conflict with
997 	 * another device (non-ifnet) on the system, we will assume they know
998 	 * what they are doing.  make_dev_alias_p won't touch tun_alias on
999 	 * failure, so we use it but ignore the return value.
1000 	 */
1001 	make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1002 	    ifp->if_xname);
1003 out:
1004 	tun_unbusy(tp);
1005 }
1006 
1007 static int
1008 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1009 {
1010 	struct ifnet	*ifp;
1011 	struct tuntap_driver *drv;
1012 	struct tuntap_softc *tp;
1013 	int error, tunflags;
1014 
1015 	tunflags = 0;
1016 	CURVNET_SET(TD_TO_VNET(td));
1017 	error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1018 	if (error != 0) {
1019 		CURVNET_RESTORE();
1020 		return (error);	/* Shouldn't happen */
1021 	}
1022 
1023 	if ((tunflags & TUN_L2) != 0) {
1024 		/* Restrict? */
1025 		if (tap_allow_uopen == 0) {
1026 			error = priv_check(td, PRIV_NET_TAP);
1027 			if (error != 0) {
1028 				CURVNET_RESTORE();
1029 				return (error);
1030 			}
1031 		}
1032 	}
1033 
1034 	/*
1035 	 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
1036 	 * synchronization.
1037 	 */
1038 	tp = dev->si_drv1;
1039 	if (!tp) {
1040 		drv = tuntap_driver_from_flags(tunflags);
1041 		if (drv == NULL) {
1042 			CURVNET_RESTORE();
1043 			return (ENXIO);
1044 		}
1045 		tuncreate(dev, drv);
1046 		tp = dev->si_drv1;
1047 	}
1048 
1049 	TUN_LOCK(tp);
1050 	if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1051 		TUN_UNLOCK(tp);
1052 		CURVNET_RESTORE();
1053 		return (EBUSY);
1054 	}
1055 
1056 	error = tun_busy_locked(tp);
1057 	KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1058 	ifp = TUN2IFP(tp);
1059 
1060 	if ((tp->tun_flags & TUN_L2) != 0) {
1061 		bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1062 		    sizeof(tp->tun_ether.octet));
1063 
1064 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1065 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1066 
1067 		if (tapuponopen)
1068 			ifp->if_flags |= IFF_UP;
1069 	}
1070 
1071 	tp->tun_pid = td->td_proc->p_pid;
1072 	tp->tun_flags |= TUN_OPEN;
1073 
1074 	if_link_state_change(ifp, LINK_STATE_UP);
1075 	TUNDEBUG(ifp, "open\n");
1076 	TUN_UNLOCK(tp);
1077 	CURVNET_RESTORE();
1078 	return (0);
1079 }
1080 
1081 /*
1082  * tunclose - close the device - mark i/f down & delete
1083  * routing info
1084  */
1085 static	int
1086 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
1087 {
1088 	struct proc *p;
1089 	struct tuntap_softc *tp;
1090 	struct ifnet *ifp;
1091 	bool l2tun;
1092 
1093 	p = td->td_proc;
1094 	tp = dev->si_drv1;
1095 	ifp = TUN2IFP(tp);
1096 
1097 	TUN_LOCK(tp);
1098 
1099 	/*
1100 	 * Realistically, we can't be obstinate here.  This only means that the
1101 	 * tuntap device was closed out of order, and the last closer wasn't the
1102 	 * controller.  These are still good to know about, though, as software
1103 	 * should avoid multiple processes with a tuntap device open and
1104 	 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1105 	 * parent).
1106 	 */
1107 	if (p->p_pid != tp->tun_pid) {
1108 		log(LOG_INFO,
1109 		    "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1110 		    p->p_pid, p->p_comm, dev->si_name);
1111 	}
1112 
1113 	/*
1114 	 * junk all pending output
1115 	 */
1116 	CURVNET_SET(ifp->if_vnet);
1117 
1118 	l2tun = false;
1119 	if ((tp->tun_flags & TUN_L2) != 0) {
1120 		l2tun = true;
1121 		IF_DRAIN(&ifp->if_snd);
1122 	} else {
1123 		IFQ_PURGE(&ifp->if_snd);
1124 	}
1125 
1126 	/* For vmnet, we won't do most of the address/route bits */
1127 	if ((tp->tun_flags & TUN_VMNET) != 0 ||
1128 	    (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1129 		goto out;
1130 
1131 	if (ifp->if_flags & IFF_UP) {
1132 		TUN_UNLOCK(tp);
1133 		if_down(ifp);
1134 		TUN_LOCK(tp);
1135 	}
1136 
1137 	/* Delete all addresses and routes which reference this interface. */
1138 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1139 		struct ifaddr *ifa;
1140 
1141 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1142 		TUN_UNLOCK(tp);
1143 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1144 			/* deal w/IPv4 PtP destination; unlocked read */
1145 			if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1146 				rtinit(ifa, (int)RTM_DELETE,
1147 				    tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1148 			} else {
1149 				rtinit(ifa, (int)RTM_DELETE, 0);
1150 			}
1151 		}
1152 		if_purgeaddrs(ifp);
1153 		TUN_LOCK(tp);
1154 	}
1155 
1156 out:
1157 	if_link_state_change(ifp, LINK_STATE_DOWN);
1158 	CURVNET_RESTORE();
1159 
1160 	funsetown(&tp->tun_sigio);
1161 	selwakeuppri(&tp->tun_rsel, PZERO + 1);
1162 	KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1163 	TUNDEBUG (ifp, "closed\n");
1164 	tp->tun_flags &= ~TUN_OPEN;
1165 	tp->tun_pid = 0;
1166 	tun_vnethdr_set(ifp, 0);
1167 
1168 	tun_unbusy_locked(tp);
1169 	TUN_UNLOCK(tp);
1170 	return (0);
1171 }
1172 
1173 static void
1174 tuninit(struct ifnet *ifp)
1175 {
1176 	struct tuntap_softc *tp = ifp->if_softc;
1177 #ifdef INET
1178 	struct epoch_tracker et;
1179 	struct ifaddr *ifa;
1180 #endif
1181 
1182 	TUNDEBUG(ifp, "tuninit\n");
1183 
1184 	TUN_LOCK(tp);
1185 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1186 	if ((tp->tun_flags & TUN_L2) == 0) {
1187 		ifp->if_flags |= IFF_UP;
1188 		getmicrotime(&ifp->if_lastchange);
1189 #ifdef INET
1190 		NET_EPOCH_ENTER(et);
1191 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1192 			if (ifa->ifa_addr->sa_family == AF_INET) {
1193 				struct sockaddr_in *si;
1194 
1195 				si = (struct sockaddr_in *)ifa->ifa_addr;
1196 				if (si->sin_addr.s_addr)
1197 					tp->tun_flags |= TUN_IASET;
1198 
1199 				si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1200 				if (si && si->sin_addr.s_addr)
1201 					tp->tun_flags |= TUN_DSTADDR;
1202 			}
1203 		}
1204 		NET_EPOCH_EXIT(et);
1205 #endif
1206 		TUN_UNLOCK(tp);
1207 	} else {
1208 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1209 		TUN_UNLOCK(tp);
1210 		/* attempt to start output */
1211 		tunstart_l2(ifp);
1212 	}
1213 
1214 }
1215 
1216 /*
1217  * Used only for l2 tunnel.
1218  */
1219 static void
1220 tunifinit(void *xtp)
1221 {
1222 	struct tuntap_softc *tp;
1223 
1224 	tp = (struct tuntap_softc *)xtp;
1225 	tuninit(tp->tun_ifp);
1226 }
1227 
1228 /*
1229  * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1230  * current value of ifp->if_capenable.
1231  */
1232 static void
1233 tun_caps_changed(struct ifnet *ifp)
1234 {
1235 	uint64_t hwassist = 0;
1236 
1237 	TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1238 	if (ifp->if_capenable & IFCAP_TXCSUM)
1239 		hwassist |= CSUM_TCP | CSUM_UDP;
1240 	if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1241 		hwassist |= CSUM_TCP_IPV6
1242 		    | CSUM_UDP_IPV6;
1243 	if (ifp->if_capenable & IFCAP_TSO4)
1244 		hwassist |= CSUM_IP_TSO;
1245 	if (ifp->if_capenable & IFCAP_TSO6)
1246 		hwassist |= CSUM_IP6_TSO;
1247 	ifp->if_hwassist = hwassist;
1248 }
1249 
1250 /*
1251  * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1252  * if_capabilities and if_capenable as needed.
1253  */
1254 static void
1255 tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1256 {
1257 	struct tuntap_softc *tp = ifp->if_softc;
1258 
1259 	TUN_LOCK_ASSERT(tp);
1260 
1261 	if (tp->tun_vhdrlen == vhdrlen)
1262 		return;
1263 
1264 	/*
1265 	 * Update if_capabilities to reflect the
1266 	 * functionalities offered by the virtio-net
1267 	 * header.
1268 	 */
1269 	if (vhdrlen != 0)
1270 		ifp->if_capabilities |=
1271 			TAP_VNET_HDR_CAPS;
1272 	else
1273 		ifp->if_capabilities &=
1274 			~TAP_VNET_HDR_CAPS;
1275 	/*
1276 	 * Disable any capabilities that we don't
1277 	 * support anymore.
1278 	 */
1279 	ifp->if_capenable &= ifp->if_capabilities;
1280 	tun_caps_changed(ifp);
1281 	tp->tun_vhdrlen = vhdrlen;
1282 
1283 	TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1284 	    vhdrlen, ifp->if_capabilities);
1285 }
1286 
1287 /*
1288  * Process an ioctl request.
1289  */
1290 static int
1291 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1292 {
1293 	struct ifreq *ifr = (struct ifreq *)data;
1294 	struct tuntap_softc *tp;
1295 	struct ifstat *ifs;
1296 	struct ifmediareq	*ifmr;
1297 	int		dummy, error = 0;
1298 	bool		l2tun;
1299 
1300 	ifmr = NULL;
1301 	sx_xlock(&tun_ioctl_sx);
1302 	tp = ifp->if_softc;
1303 	if (tp == NULL) {
1304 		error = ENXIO;
1305 		goto bad;
1306 	}
1307 	l2tun = (tp->tun_flags & TUN_L2) != 0;
1308 	switch(cmd) {
1309 	case SIOCGIFSTATUS:
1310 		ifs = (struct ifstat *)data;
1311 		TUN_LOCK(tp);
1312 		if (tp->tun_pid)
1313 			snprintf(ifs->ascii, sizeof(ifs->ascii),
1314 			    "\tOpened by PID %d\n", tp->tun_pid);
1315 		else
1316 			ifs->ascii[0] = '\0';
1317 		TUN_UNLOCK(tp);
1318 		break;
1319 	case SIOCSIFADDR:
1320 		if (l2tun)
1321 			error = ether_ioctl(ifp, cmd, data);
1322 		else
1323 			tuninit(ifp);
1324 		if (error == 0)
1325 		    TUNDEBUG(ifp, "address set\n");
1326 		break;
1327 	case SIOCSIFMTU:
1328 		ifp->if_mtu = ifr->ifr_mtu;
1329 		TUNDEBUG(ifp, "mtu set\n");
1330 		break;
1331 	case SIOCSIFFLAGS:
1332 	case SIOCADDMULTI:
1333 	case SIOCDELMULTI:
1334 		break;
1335 	case SIOCGIFMEDIA:
1336 		if (!l2tun) {
1337 			error = EINVAL;
1338 			break;
1339 		}
1340 
1341 		ifmr = (struct ifmediareq *)data;
1342 		dummy = ifmr->ifm_count;
1343 		ifmr->ifm_count = 1;
1344 		ifmr->ifm_status = IFM_AVALID;
1345 		ifmr->ifm_active = IFM_ETHER;
1346 		if (tp->tun_flags & TUN_OPEN)
1347 			ifmr->ifm_status |= IFM_ACTIVE;
1348 		ifmr->ifm_current = ifmr->ifm_active;
1349 		if (dummy >= 1) {
1350 			int media = IFM_ETHER;
1351 			error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1352 		}
1353 		break;
1354 	case SIOCSIFCAP:
1355 		TUN_LOCK(tp);
1356 		ifp->if_capenable = ifr->ifr_reqcap;
1357 		tun_caps_changed(ifp);
1358 		TUN_UNLOCK(tp);
1359 		VLAN_CAPABILITIES(ifp);
1360 		break;
1361 	default:
1362 		if (l2tun) {
1363 			error = ether_ioctl(ifp, cmd, data);
1364 		} else {
1365 			error = EINVAL;
1366 		}
1367 	}
1368 bad:
1369 	sx_xunlock(&tun_ioctl_sx);
1370 	return (error);
1371 }
1372 
1373 /*
1374  * tunoutput - queue packets from higher level ready to put out.
1375  */
1376 static int
1377 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1378     struct route *ro)
1379 {
1380 	struct tuntap_softc *tp = ifp->if_softc;
1381 	u_short cached_tun_flags;
1382 	int error;
1383 	u_int32_t af;
1384 
1385 	TUNDEBUG (ifp, "tunoutput\n");
1386 
1387 #ifdef MAC
1388 	error = mac_ifnet_check_transmit(ifp, m0);
1389 	if (error) {
1390 		m_freem(m0);
1391 		return (error);
1392 	}
1393 #endif
1394 
1395 	/* Could be unlocked read? */
1396 	TUN_LOCK(tp);
1397 	cached_tun_flags = tp->tun_flags;
1398 	TUN_UNLOCK(tp);
1399 	if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1400 		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1401 		m_freem (m0);
1402 		return (EHOSTDOWN);
1403 	}
1404 
1405 	if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1406 		m_freem (m0);
1407 		return (EHOSTDOWN);
1408 	}
1409 
1410 	/* BPF writes need to be handled specially. */
1411 	if (dst->sa_family == AF_UNSPEC)
1412 		bcopy(dst->sa_data, &af, sizeof(af));
1413 	else
1414 		af = dst->sa_family;
1415 
1416 	if (bpf_peers_present(ifp->if_bpf))
1417 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1418 
1419 	/* prepend sockaddr? this may abort if the mbuf allocation fails */
1420 	if (cached_tun_flags & TUN_LMODE) {
1421 		/* allocate space for sockaddr */
1422 		M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1423 
1424 		/* if allocation failed drop packet */
1425 		if (m0 == NULL) {
1426 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1427 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1428 			return (ENOBUFS);
1429 		} else {
1430 			bcopy(dst, m0->m_data, dst->sa_len);
1431 		}
1432 	}
1433 
1434 	if (cached_tun_flags & TUN_IFHEAD) {
1435 		/* Prepend the address family */
1436 		M_PREPEND(m0, 4, M_NOWAIT);
1437 
1438 		/* if allocation failed drop packet */
1439 		if (m0 == NULL) {
1440 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1441 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1442 			return (ENOBUFS);
1443 		} else
1444 			*(u_int32_t *)m0->m_data = htonl(af);
1445 	} else {
1446 #ifdef INET
1447 		if (af != AF_INET)
1448 #endif
1449 		{
1450 			m_freem(m0);
1451 			return (EAFNOSUPPORT);
1452 		}
1453 	}
1454 
1455 	error = (ifp->if_transmit)(ifp, m0);
1456 	if (error)
1457 		return (ENOBUFS);
1458 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1459 	return (0);
1460 }
1461 
1462 /*
1463  * the cdevsw interface is now pretty minimal.
1464  */
1465 static	int
1466 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1467     struct thread *td)
1468 {
1469 	struct ifreq ifr, *ifrp;
1470 	struct tuntap_softc *tp = dev->si_drv1;
1471 	struct ifnet *ifp = TUN2IFP(tp);
1472 	struct tuninfo *tunp;
1473 	int error, iflags, ival;
1474 	bool	l2tun;
1475 
1476 	l2tun = (tp->tun_flags & TUN_L2) != 0;
1477 	if (l2tun) {
1478 		/* tap specific ioctls */
1479 		switch(cmd) {
1480 		/* VMware/VMnet port ioctl's */
1481 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1482     defined(COMPAT_FREEBSD4)
1483 		case _IO('V', 0):
1484 			ival = IOCPARM_IVAL(data);
1485 			data = (caddr_t)&ival;
1486 			/* FALLTHROUGH */
1487 #endif
1488 		case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1489 			iflags = *(int *)data;
1490 			iflags &= TUN_VMIO_FLAG_MASK;
1491 			iflags &= ~IFF_CANTCHANGE;
1492 			iflags |= IFF_UP;
1493 
1494 			TUN_LOCK(tp);
1495 			ifp->if_flags = iflags |
1496 			    (ifp->if_flags & IFF_CANTCHANGE);
1497 			TUN_UNLOCK(tp);
1498 
1499 			return (0);
1500 		case SIOCGIFADDR:	/* get MAC address of the remote side */
1501 			TUN_LOCK(tp);
1502 			bcopy(&tp->tun_ether.octet, data,
1503 			    sizeof(tp->tun_ether.octet));
1504 			TUN_UNLOCK(tp);
1505 
1506 			return (0);
1507 		case SIOCSIFADDR:	/* set MAC address of the remote side */
1508 			TUN_LOCK(tp);
1509 			bcopy(data, &tp->tun_ether.octet,
1510 			    sizeof(tp->tun_ether.octet));
1511 			TUN_UNLOCK(tp);
1512 
1513 			return (0);
1514 		case TAPSVNETHDR:
1515 			ival = *(int *)data;
1516 			if (ival != 0 &&
1517 			    ival != sizeof(struct virtio_net_hdr) &&
1518 			    ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1519 				return (EINVAL);
1520 			}
1521 			TUN_LOCK(tp);
1522 			tun_vnethdr_set(ifp, ival);
1523 			TUN_UNLOCK(tp);
1524 
1525 			return (0);
1526 		case TAPGVNETHDR:
1527 			TUN_LOCK(tp);
1528 			*(int *)data = tp->tun_vhdrlen;
1529 			TUN_UNLOCK(tp);
1530 
1531 			return (0);
1532 		}
1533 
1534 		/* Fall through to the common ioctls if unhandled */
1535 	} else {
1536 		switch (cmd) {
1537 		case TUNSLMODE:
1538 			TUN_LOCK(tp);
1539 			if (*(int *)data) {
1540 				tp->tun_flags |= TUN_LMODE;
1541 				tp->tun_flags &= ~TUN_IFHEAD;
1542 			} else
1543 				tp->tun_flags &= ~TUN_LMODE;
1544 			TUN_UNLOCK(tp);
1545 
1546 			return (0);
1547 		case TUNSIFHEAD:
1548 			TUN_LOCK(tp);
1549 			if (*(int *)data) {
1550 				tp->tun_flags |= TUN_IFHEAD;
1551 				tp->tun_flags &= ~TUN_LMODE;
1552 			} else
1553 				tp->tun_flags &= ~TUN_IFHEAD;
1554 			TUN_UNLOCK(tp);
1555 
1556 			return (0);
1557 		case TUNGIFHEAD:
1558 			TUN_LOCK(tp);
1559 			*(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1560 			TUN_UNLOCK(tp);
1561 
1562 			return (0);
1563 		case TUNSIFMODE:
1564 			/* deny this if UP */
1565 			if (TUN2IFP(tp)->if_flags & IFF_UP)
1566 				return (EBUSY);
1567 
1568 			switch (*(int *)data & ~IFF_MULTICAST) {
1569 			case IFF_POINTOPOINT:
1570 			case IFF_BROADCAST:
1571 				TUN_LOCK(tp);
1572 				TUN2IFP(tp)->if_flags &=
1573 				    ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1574 				TUN2IFP(tp)->if_flags |= *(int *)data;
1575 				TUN_UNLOCK(tp);
1576 
1577 				break;
1578 			default:
1579 				return (EINVAL);
1580 			}
1581 
1582 			return (0);
1583 		case TUNSIFPID:
1584 			TUN_LOCK(tp);
1585 			tp->tun_pid = curthread->td_proc->p_pid;
1586 			TUN_UNLOCK(tp);
1587 
1588 			return (0);
1589 		}
1590 		/* Fall through to the common ioctls if unhandled */
1591 	}
1592 
1593 	switch (cmd) {
1594 	case TUNGIFNAME:
1595 		ifrp = (struct ifreq *)data;
1596 		strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1597 
1598 		return (0);
1599 	case TUNSIFINFO:
1600 		tunp = (struct tuninfo *)data;
1601 		if (TUN2IFP(tp)->if_type != tunp->type)
1602 			return (EPROTOTYPE);
1603 		TUN_LOCK(tp);
1604 		if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1605 			strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1606 			ifr.ifr_mtu = tunp->mtu;
1607 			CURVNET_SET(TUN2IFP(tp)->if_vnet);
1608 			error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1609 			    (caddr_t)&ifr, td);
1610 			CURVNET_RESTORE();
1611 			if (error) {
1612 				TUN_UNLOCK(tp);
1613 				return (error);
1614 			}
1615 		}
1616 		TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1617 		TUN_UNLOCK(tp);
1618 		break;
1619 	case TUNGIFINFO:
1620 		tunp = (struct tuninfo *)data;
1621 		TUN_LOCK(tp);
1622 		tunp->mtu = TUN2IFP(tp)->if_mtu;
1623 		tunp->type = TUN2IFP(tp)->if_type;
1624 		tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1625 		TUN_UNLOCK(tp);
1626 		break;
1627 	case TUNSDEBUG:
1628 		tundebug = *(int *)data;
1629 		break;
1630 	case TUNGDEBUG:
1631 		*(int *)data = tundebug;
1632 		break;
1633 	case FIONBIO:
1634 		break;
1635 	case FIOASYNC:
1636 		TUN_LOCK(tp);
1637 		if (*(int *)data)
1638 			tp->tun_flags |= TUN_ASYNC;
1639 		else
1640 			tp->tun_flags &= ~TUN_ASYNC;
1641 		TUN_UNLOCK(tp);
1642 		break;
1643 	case FIONREAD:
1644 		if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1645 			struct mbuf *mb;
1646 			IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1647 			IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1648 			for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1649 				*(int *)data += mb->m_len;
1650 			IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1651 		} else
1652 			*(int *)data = 0;
1653 		break;
1654 	case FIOSETOWN:
1655 		return (fsetown(*(int *)data, &tp->tun_sigio));
1656 
1657 	case FIOGETOWN:
1658 		*(int *)data = fgetown(&tp->tun_sigio);
1659 		return (0);
1660 
1661 	/* This is deprecated, FIOSETOWN should be used instead. */
1662 	case TIOCSPGRP:
1663 		return (fsetown(-(*(int *)data), &tp->tun_sigio));
1664 
1665 	/* This is deprecated, FIOGETOWN should be used instead. */
1666 	case TIOCGPGRP:
1667 		*(int *)data = -fgetown(&tp->tun_sigio);
1668 		return (0);
1669 
1670 	default:
1671 		return (ENOTTY);
1672 	}
1673 	return (0);
1674 }
1675 
1676 /*
1677  * The cdevsw read interface - reads a packet at a time, or at
1678  * least as much of a packet as can be read.
1679  */
1680 static	int
1681 tunread(struct cdev *dev, struct uio *uio, int flag)
1682 {
1683 	struct tuntap_softc *tp = dev->si_drv1;
1684 	struct ifnet	*ifp = TUN2IFP(tp);
1685 	struct mbuf	*m;
1686 	size_t		len;
1687 	int		error = 0;
1688 
1689 	TUNDEBUG (ifp, "read\n");
1690 	TUN_LOCK(tp);
1691 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1692 		TUN_UNLOCK(tp);
1693 		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1694 		return (EHOSTDOWN);
1695 	}
1696 
1697 	tp->tun_flags &= ~TUN_RWAIT;
1698 
1699 	for (;;) {
1700 		IFQ_DEQUEUE(&ifp->if_snd, m);
1701 		if (m != NULL)
1702 			break;
1703 		if (flag & O_NONBLOCK) {
1704 			TUN_UNLOCK(tp);
1705 			return (EWOULDBLOCK);
1706 		}
1707 		tp->tun_flags |= TUN_RWAIT;
1708 		error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1709 		    "tunread", 0);
1710 		if (error != 0) {
1711 			TUN_UNLOCK(tp);
1712 			return (error);
1713 		}
1714 	}
1715 	TUN_UNLOCK(tp);
1716 
1717 	if ((tp->tun_flags & TUN_L2) != 0)
1718 		BPF_MTAP(ifp, m);
1719 
1720 	len = min(tp->tun_vhdrlen, uio->uio_resid);
1721 	if (len > 0) {
1722 		struct virtio_net_hdr_mrg_rxbuf vhdr;
1723 
1724 		bzero(&vhdr, sizeof(vhdr));
1725 		if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1726 			m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1727 		}
1728 
1729 		TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1730 		    "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1731 		    vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1732 		    vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1733 		    vhdr.hdr.csum_offset);
1734 		error = uiomove(&vhdr, len, uio);
1735 	}
1736 
1737 	while (m && uio->uio_resid > 0 && error == 0) {
1738 		len = min(uio->uio_resid, m->m_len);
1739 		if (len != 0)
1740 			error = uiomove(mtod(m, void *), len, uio);
1741 		m = m_free(m);
1742 	}
1743 
1744 	if (m) {
1745 		TUNDEBUG(ifp, "Dropping mbuf\n");
1746 		m_freem(m);
1747 	}
1748 	return (error);
1749 }
1750 
1751 static int
1752 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1753 	    struct virtio_net_hdr_mrg_rxbuf *vhdr)
1754 {
1755 	struct ether_header *eh;
1756 	struct ifnet *ifp;
1757 
1758 	ifp = TUN2IFP(tp);
1759 
1760 	/*
1761 	 * Only pass a unicast frame to ether_input(), if it would
1762 	 * actually have been received by non-virtual hardware.
1763 	 */
1764 	if (m->m_len < sizeof(struct ether_header)) {
1765 		m_freem(m);
1766 		return (0);
1767 	}
1768 
1769 	eh = mtod(m, struct ether_header *);
1770 
1771 	if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1772 	    !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1773 	    bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1774 		m_freem(m);
1775 		return (0);
1776 	}
1777 
1778 	if (vhdr != NULL && virtio_net_rx_csum(m, &vhdr->hdr)) {
1779 		m_freem(m);
1780 		return (0);
1781 	}
1782 
1783 	/* Pass packet up to parent. */
1784 	CURVNET_SET(ifp->if_vnet);
1785 	(*ifp->if_input)(ifp, m);
1786 	CURVNET_RESTORE();
1787 	/* ibytes are counted in parent */
1788 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1789 	return (0);
1790 }
1791 
1792 static int
1793 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1794 {
1795 	struct epoch_tracker et;
1796 	struct ifnet *ifp;
1797 	int family, isr;
1798 
1799 	ifp = TUN2IFP(tp);
1800 	/* Could be unlocked read? */
1801 	TUN_LOCK(tp);
1802 	if (tp->tun_flags & TUN_IFHEAD) {
1803 		TUN_UNLOCK(tp);
1804 		if (m->m_len < sizeof(family) &&
1805 		(m = m_pullup(m, sizeof(family))) == NULL)
1806 			return (ENOBUFS);
1807 		family = ntohl(*mtod(m, u_int32_t *));
1808 		m_adj(m, sizeof(family));
1809 	} else {
1810 		TUN_UNLOCK(tp);
1811 		family = AF_INET;
1812 	}
1813 
1814 	BPF_MTAP2(ifp, &family, sizeof(family), m);
1815 
1816 	switch (family) {
1817 #ifdef INET
1818 	case AF_INET:
1819 		isr = NETISR_IP;
1820 		break;
1821 #endif
1822 #ifdef INET6
1823 	case AF_INET6:
1824 		isr = NETISR_IPV6;
1825 		break;
1826 #endif
1827 	default:
1828 		m_freem(m);
1829 		return (EAFNOSUPPORT);
1830 	}
1831 	random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1832 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1833 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1834 	CURVNET_SET(ifp->if_vnet);
1835 	M_SETFIB(m, ifp->if_fib);
1836 	NET_EPOCH_ENTER(et);
1837 	netisr_dispatch(isr, m);
1838 	NET_EPOCH_EXIT(et);
1839 	CURVNET_RESTORE();
1840 	return (0);
1841 }
1842 
1843 /*
1844  * the cdevsw write interface - an atomic write is a packet - or else!
1845  */
1846 static	int
1847 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1848 {
1849 	struct virtio_net_hdr_mrg_rxbuf vhdr;
1850 	struct tuntap_softc *tp;
1851 	struct ifnet	*ifp;
1852 	struct mbuf	*m;
1853 	uint32_t	mru;
1854 	int		align, vhdrlen, error;
1855 	bool		l2tun;
1856 
1857 	tp = dev->si_drv1;
1858 	ifp = TUN2IFP(tp);
1859 	TUNDEBUG(ifp, "tunwrite\n");
1860 	if ((ifp->if_flags & IFF_UP) != IFF_UP)
1861 		/* ignore silently */
1862 		return (0);
1863 
1864 	if (uio->uio_resid == 0)
1865 		return (0);
1866 
1867 	l2tun = (tp->tun_flags & TUN_L2) != 0;
1868 	mru = l2tun ? TAPMRU : TUNMRU;
1869 	vhdrlen = tp->tun_vhdrlen;
1870 	align = 0;
1871 	if (l2tun) {
1872 		align = ETHER_ALIGN;
1873 		mru += vhdrlen;
1874 	} else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1875 		mru += sizeof(uint32_t);	/* family */
1876 	if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1877 		TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1878 		return (EIO);
1879 	}
1880 
1881 	if (vhdrlen > 0) {
1882 		error = uiomove(&vhdr, vhdrlen, uio);
1883 		if (error != 0)
1884 			return (error);
1885 		TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1886 		    "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1887 		    vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1888 		    vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1889 		    vhdr.hdr.csum_offset);
1890 	}
1891 
1892 	if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1893 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1894 		return (ENOBUFS);
1895 	}
1896 
1897 	m->m_pkthdr.rcvif = ifp;
1898 #ifdef MAC
1899 	mac_ifnet_create_mbuf(ifp, m);
1900 #endif
1901 
1902 	if (l2tun)
1903 		return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1904 
1905 	return (tunwrite_l3(tp, m));
1906 }
1907 
1908 /*
1909  * tunpoll - the poll interface, this is only useful on reads
1910  * really. The write detect always returns true, write never blocks
1911  * anyway, it either accepts the packet or drops it.
1912  */
1913 static	int
1914 tunpoll(struct cdev *dev, int events, struct thread *td)
1915 {
1916 	struct tuntap_softc *tp = dev->si_drv1;
1917 	struct ifnet	*ifp = TUN2IFP(tp);
1918 	int		revents = 0;
1919 
1920 	TUNDEBUG(ifp, "tunpoll\n");
1921 
1922 	if (events & (POLLIN | POLLRDNORM)) {
1923 		IFQ_LOCK(&ifp->if_snd);
1924 		if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1925 			TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1926 			revents |= events & (POLLIN | POLLRDNORM);
1927 		} else {
1928 			TUNDEBUG(ifp, "tunpoll waiting\n");
1929 			selrecord(td, &tp->tun_rsel);
1930 		}
1931 		IFQ_UNLOCK(&ifp->if_snd);
1932 	}
1933 	revents |= events & (POLLOUT | POLLWRNORM);
1934 
1935 	return (revents);
1936 }
1937 
1938 /*
1939  * tunkqfilter - support for the kevent() system call.
1940  */
1941 static int
1942 tunkqfilter(struct cdev *dev, struct knote *kn)
1943 {
1944 	struct tuntap_softc	*tp = dev->si_drv1;
1945 	struct ifnet	*ifp = TUN2IFP(tp);
1946 
1947 	switch(kn->kn_filter) {
1948 	case EVFILT_READ:
1949 		TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1950 		    ifp->if_xname, dev2unit(dev));
1951 		kn->kn_fop = &tun_read_filterops;
1952 		break;
1953 
1954 	case EVFILT_WRITE:
1955 		TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1956 		    ifp->if_xname, dev2unit(dev));
1957 		kn->kn_fop = &tun_write_filterops;
1958 		break;
1959 
1960 	default:
1961 		TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1962 		    ifp->if_xname, dev2unit(dev));
1963 		return(EINVAL);
1964 	}
1965 
1966 	kn->kn_hook = tp;
1967 	knlist_add(&tp->tun_rsel.si_note, kn, 0);
1968 
1969 	return (0);
1970 }
1971 
1972 /*
1973  * Return true of there is data in the interface queue.
1974  */
1975 static int
1976 tunkqread(struct knote *kn, long hint)
1977 {
1978 	int			ret;
1979 	struct tuntap_softc	*tp = kn->kn_hook;
1980 	struct cdev		*dev = tp->tun_dev;
1981 	struct ifnet	*ifp = TUN2IFP(tp);
1982 
1983 	if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1984 		TUNDEBUG(ifp,
1985 		    "%s have data in the queue.  Len = %d, minor = %#x\n",
1986 		    ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1987 		ret = 1;
1988 	} else {
1989 		TUNDEBUG(ifp,
1990 		    "%s waiting for data, minor = %#x\n", ifp->if_xname,
1991 		    dev2unit(dev));
1992 		ret = 0;
1993 	}
1994 
1995 	return (ret);
1996 }
1997 
1998 /*
1999  * Always can write, always return MTU in kn->data.
2000  */
2001 static int
2002 tunkqwrite(struct knote *kn, long hint)
2003 {
2004 	struct tuntap_softc	*tp = kn->kn_hook;
2005 	struct ifnet	*ifp = TUN2IFP(tp);
2006 
2007 	kn->kn_data = ifp->if_mtu;
2008 
2009 	return (1);
2010 }
2011 
2012 static void
2013 tunkqdetach(struct knote *kn)
2014 {
2015 	struct tuntap_softc	*tp = kn->kn_hook;
2016 
2017 	knlist_remove(&tp->tun_rsel.si_note, kn, 0);
2018 }
2019