1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 * -------------------------------------------------------------------------
34 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
35 * Nottingham University 1987.
37 * This source may be freely distributed, however I would be interested
38 * in any changes that are made.
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
50 #include "opt_inet6.h"
52 #include <sys/param.h>
56 #include <sys/systm.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>
66 #include <sys/syslog.h>
67 #include <sys/ttycom.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>
76 #include <sys/malloc.h>
77 #include <sys/random.h>
78 #include <sys/ctype.h>
80 #include <net/ethernet.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>
91 #include <netinet/in.h>
93 #include <netinet/ip.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
99 #include <netinet/udp.h>
100 #include <netinet/tcp.h>
102 #include <net/if_tap.h>
103 #include <net/if_tun.h>
105 #include <dev/virtio/network/virtio_net.h>
107 #include <sys/queue.h>
108 #include <sys/condvar.h>
109 #include <security/mac/mac_framework.h>
111 struct tuntap_driver;
114 * tun_list is protected by global tunmtx. Other mutable fields are
115 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
116 * static for the duration of a tunnel interface.
118 struct tuntap_softc {
119 TAILQ_ENTRY(tuntap_softc) tun_list;
120 struct cdev *tun_alias;
121 struct cdev *tun_dev;
122 u_short tun_flags; /* misc flags */
123 #define TUN_OPEN 0x0001
124 #define TUN_INITED 0x0002
125 #define TUN_UNUSED1 0x0008
126 #define TUN_DSTADDR 0x0010
127 #define TUN_LMODE 0x0020
128 #define TUN_RWAIT 0x0040
129 #define TUN_ASYNC 0x0080
130 #define TUN_IFHEAD 0x0100
131 #define TUN_DYING 0x0200
132 #define TUN_L2 0x0400
133 #define TUN_VMNET 0x0800
135 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
136 #define TUN_READY (TUN_OPEN | TUN_INITED)
138 pid_t tun_pid; /* owning pid */
139 struct ifnet *tun_ifp; /* the interface */
140 struct sigio *tun_sigio; /* async I/O info */
141 struct tuntap_driver *tun_drv; /* appropriate driver */
142 struct selinfo tun_rsel; /* read select */
143 struct mtx tun_mtx; /* softc field mutex */
144 struct cv tun_cv; /* for ref'd dev destroy */
145 struct ether_addr tun_ether; /* remote address */
146 int tun_busy; /* busy count */
147 int tun_vhdrlen; /* virtio-net header length */
149 #define TUN2IFP(sc) ((sc)->tun_ifp)
151 #define TUNDEBUG if (tundebug) if_printf
153 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
154 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
155 #define TUN_LOCK_ASSERT(tp) mtx_assert(&(tp)->tun_mtx, MA_OWNED);
157 #define TUN_VMIO_FLAG_MASK 0x0fff
160 * Interface capabilities of a tap device that supports the virtio-net
163 #define TAP_VNET_HDR_CAPS (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 \
164 | IFCAP_VLAN_HWCSUM \
165 | IFCAP_TSO | IFCAP_LRO \
168 #define TAP_ALL_OFFLOAD (CSUM_TSO | CSUM_TCP | CSUM_UDP |\
169 CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
173 * All mutable global variables in if_tun are locked using tunmtx, with
174 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
175 * which are static after setup.
177 static struct mtx tunmtx;
178 static eventhandler_tag arrival_tag;
179 static eventhandler_tag clone_tag;
180 static const char tunname[] = "tun";
181 static const char tapname[] = "tap";
182 static const char vmnetname[] = "vmnet";
183 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
184 static int tundebug = 0;
185 static int tundclone = 1;
186 static int tap_allow_uopen = 0; /* allow user devfs cloning */
187 static int tapuponopen = 0; /* IFF_UP on open() */
188 static int tapdclone = 1; /* enable devfs cloning */
190 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
191 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
193 static struct sx tun_ioctl_sx;
194 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
196 SYSCTL_DECL(_net_link);
198 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
199 "IP tunnel software network interface");
200 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
201 "Enable legacy devfs interface creation");
204 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
205 "Ethernet tunnel software network interface");
206 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
207 "Enable legacy devfs interface creation for all users");
208 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
209 "Bring interface up when /dev/tap is opened");
210 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
211 "Enable legacy devfs interface creation");
212 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
214 static int tun_create_device(struct tuntap_driver *drv, int unit,
215 struct ucred *cr, struct cdev **dev, const char *name);
216 static int tun_busy_locked(struct tuntap_softc *tp);
217 static void tun_unbusy_locked(struct tuntap_softc *tp);
218 static int tun_busy(struct tuntap_softc *tp);
219 static void tun_unbusy(struct tuntap_softc *tp);
221 static int tuntap_name2info(const char *name, int *unit, int *flags);
222 static void tunclone(void *arg, struct ucred *cred, char *name,
223 int namelen, struct cdev **dev);
224 static void tuncreate(struct cdev *dev);
225 static void tundtor(void *data);
226 static void tunrename(void *arg, struct ifnet *ifp);
227 static int tunifioctl(struct ifnet *, u_long, caddr_t);
228 static void tuninit(struct ifnet *);
229 static void tunifinit(void *xtp);
230 static int tuntapmodevent(module_t, int, void *);
231 static int tunoutput(struct ifnet *, struct mbuf *,
232 const struct sockaddr *, struct route *ro);
233 static void tunstart(struct ifnet *);
234 static void tunstart_l2(struct ifnet *);
236 static int tun_clone_match(struct if_clone *ifc, const char *name);
237 static int tap_clone_match(struct if_clone *ifc, const char *name);
238 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
239 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
240 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
241 static void tun_vnethdr_set(struct ifnet *ifp, int vhdrlen);
243 static d_open_t tunopen;
244 static d_read_t tunread;
245 static d_write_t tunwrite;
246 static d_ioctl_t tunioctl;
247 static d_poll_t tunpoll;
248 static d_kqfilter_t tunkqfilter;
250 static int tunkqread(struct knote *, long);
251 static int tunkqwrite(struct knote *, long);
252 static void tunkqdetach(struct knote *);
254 static struct filterops tun_read_filterops = {
257 .f_detach = tunkqdetach,
258 .f_event = tunkqread,
261 static struct filterops tun_write_filterops = {
264 .f_detach = tunkqdetach,
265 .f_event = tunkqwrite,
268 static struct tuntap_driver {
269 struct cdevsw cdevsw;
271 struct unrhdr *unrhdr;
272 struct clonedevs *clones;
273 ifc_match_t *clone_match_fn;
274 ifc_create_t *clone_create_fn;
275 ifc_destroy_t *clone_destroy_fn;
276 } tuntap_drivers[] = {
280 .d_version = D_VERSION,
281 .d_flags = D_NEEDMINOR,
287 .d_kqfilter = tunkqfilter,
290 .clone_match_fn = tun_clone_match,
291 .clone_create_fn = tun_clone_create,
292 .clone_destroy_fn = tun_clone_destroy,
295 .ident_flags = TUN_L2,
297 .d_version = D_VERSION,
298 .d_flags = D_NEEDMINOR,
304 .d_kqfilter = tunkqfilter,
307 .clone_match_fn = tap_clone_match,
308 .clone_create_fn = tun_clone_create,
309 .clone_destroy_fn = tun_clone_destroy,
312 .ident_flags = TUN_L2 | TUN_VMNET,
314 .d_version = D_VERSION,
315 .d_flags = D_NEEDMINOR,
321 .d_kqfilter = tunkqfilter,
324 .clone_match_fn = vmnet_clone_match,
325 .clone_create_fn = tun_clone_create,
326 .clone_destroy_fn = tun_clone_destroy,
330 struct tuntap_driver_cloner {
331 SLIST_ENTRY(tuntap_driver_cloner) link;
332 struct tuntap_driver *drv;
333 struct if_clone *cloner;
336 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
337 SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
339 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
342 * Mechanism for marking a tunnel device as busy so that we can safely do some
343 * orthogonal operations (such as operations on devices) without racing against
344 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
345 * open, to be woken up when the condition is alleviated.
348 tun_busy_locked(struct tuntap_softc *tp)
352 if ((tp->tun_flags & TUN_DYING) != 0) {
354 * Perhaps unintuitive, but the device is busy going away.
355 * Other interpretations of EBUSY from tun_busy make little
356 * sense, since making a busy device even more busy doesn't
357 * sound like a problem.
367 tun_unbusy_locked(struct tuntap_softc *tp)
371 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
374 /* Wake up anything that may be waiting on our busy tunnel. */
375 if (tp->tun_busy == 0)
376 cv_broadcast(&tp->tun_cv);
380 tun_busy(struct tuntap_softc *tp)
385 ret = tun_busy_locked(tp);
392 tun_unbusy(struct tuntap_softc *tp)
396 tun_unbusy_locked(tp);
401 * Sets unit and/or flags given the device name. Must be called with correct
405 tuntap_name2info(const char *name, int *outunit, int *outflags)
407 struct tuntap_driver *drv;
408 struct tuntap_driver_cloner *drvc;
417 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
418 * wants to be able to pass back a char * through the second param. We
419 * will always set that as NULL here, so we'll fake it.
421 dname = __DECONST(char *, name);
424 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
425 ("tuntap_driver_cloners failed to initialize"));
426 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
427 KASSERT(drvc->drv != NULL,
428 ("tuntap_driver_cloners entry not properly initialized"));
431 if (strcmp(name, drv->cdevsw.d_name) == 0) {
434 flags = drv->ident_flags;
438 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
440 flags = drv->ident_flags;
450 if (outflags != NULL)
456 * Get driver information from a set of flags specified. Masks the identifying
457 * part of the flags and compares it against all of the available
458 * tuntap_drivers. Must be called with correct vnet context.
460 static struct tuntap_driver *
461 tuntap_driver_from_flags(int tun_flags)
463 struct tuntap_driver *drv;
464 struct tuntap_driver_cloner *drvc;
466 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
467 ("tuntap_driver_cloners failed to initialize"));
468 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
469 KASSERT(drvc->drv != NULL,
470 ("tuntap_driver_cloners entry not properly initialized"));
472 if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->ident_flags)
482 tun_clone_match(struct if_clone *ifc, const char *name)
486 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
487 if ((tunflags & TUN_L2) == 0)
495 tap_clone_match(struct if_clone *ifc, const char *name)
499 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
500 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
508 vmnet_clone_match(struct if_clone *ifc, const char *name)
512 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
513 if ((tunflags & TUN_VMNET) != 0)
521 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
523 struct tuntap_driver *drv;
525 int err, i, tunflags, unit;
528 /* The name here tells us exactly what we're creating */
529 err = tuntap_name2info(name, &unit, &tunflags);
533 drv = tuntap_driver_from_flags(tunflags);
538 /* If this unit number is still available that's okay. */
539 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
542 unit = alloc_unr(drv->unrhdr);
545 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
547 /* find any existing device, or allocate new unit number */
549 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
550 /* No preexisting struct cdev *, create one */
552 i = tun_create_device(drv, unit, NULL, &dev, name);
560 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
563 char devname[SPECNAMELEN + 1];
564 struct tuntap_driver *drv;
565 int append_unit, i, u, tunflags;
572 CURVNET_SET(CRED_TO_VNET(cred));
573 if (tuntap_name2info(name, &u, &tunflags) != 0)
574 goto out; /* Not recognized */
576 if (u != -1 && u > IF_MAXUNIT)
577 goto out; /* Unit number too high */
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;
584 mayclone = mayclone && tundclone;
588 * If tun cloning is enabled, only the superuser can create an
599 drv = tuntap_driver_from_flags(tunflags);
603 /* find any existing device, or allocate new unit number */
604 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
607 namelen = snprintf(devname, sizeof(devname), "%s%d",
612 i = tun_create_device(drv, u, cred, dev, name);
615 if_clone_create(name, namelen, NULL);
621 tun_destroy(struct tuntap_softc *tp)
625 tp->tun_flags |= TUN_DYING;
626 if (tp->tun_busy != 0)
627 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
631 CURVNET_SET(TUN2IFP(tp)->if_vnet);
633 /* destroy_dev will take care of any alias. */
634 destroy_dev(tp->tun_dev);
635 seldrain(&tp->tun_rsel);
636 knlist_clear(&tp->tun_rsel.si_note, 0);
637 knlist_destroy(&tp->tun_rsel.si_note);
638 if ((tp->tun_flags & TUN_L2) != 0) {
639 ether_ifdetach(TUN2IFP(tp));
641 bpfdetach(TUN2IFP(tp));
642 if_detach(TUN2IFP(tp));
644 sx_xlock(&tun_ioctl_sx);
645 TUN2IFP(tp)->if_softc = NULL;
646 sx_xunlock(&tun_ioctl_sx);
647 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
648 if_free(TUN2IFP(tp));
649 mtx_destroy(&tp->tun_mtx);
650 cv_destroy(&tp->tun_cv);
656 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
658 struct tuntap_softc *tp = ifp->if_softc;
661 TAILQ_REMOVE(&tunhead, tp, tun_list);
669 vnet_tun_init(const void *unused __unused)
671 struct tuntap_driver *drv;
672 struct tuntap_driver_cloner *drvc;
675 for (i = 0; i < nitems(tuntap_drivers); ++i) {
676 drv = &tuntap_drivers[i];
677 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
680 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
681 drv->clone_match_fn, drv->clone_create_fn,
682 drv->clone_destroy_fn);
683 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
686 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
687 vnet_tun_init, NULL);
690 vnet_tun_uninit(const void *unused __unused)
692 struct tuntap_driver_cloner *drvc;
694 while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
695 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
696 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
698 if_clone_detach(drvc->cloner);
702 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
703 vnet_tun_uninit, NULL);
706 tun_uninit(const void *unused __unused)
708 struct tuntap_driver *drv;
709 struct tuntap_softc *tp;
712 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
713 EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
714 drain_dev_clone_events();
717 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
718 TAILQ_REMOVE(&tunhead, tp, tun_list);
724 for (i = 0; i < nitems(tuntap_drivers); ++i) {
725 drv = &tuntap_drivers[i];
726 delete_unrhdr(drv->unrhdr);
727 clone_cleanup(&drv->clones);
729 mtx_destroy(&tunmtx);
731 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
733 static struct tuntap_driver *
734 tuntap_driver_from_ifnet(const struct ifnet *ifp)
736 struct tuntap_driver *drv;
742 for (i = 0; i < nitems(tuntap_drivers); ++i) {
743 drv = &tuntap_drivers[i];
744 if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
752 tuntapmodevent(module_t mod, int type, void *data)
754 struct tuntap_driver *drv;
759 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
760 for (i = 0; i < nitems(tuntap_drivers); ++i) {
761 drv = &tuntap_drivers[i];
762 clone_setup(&drv->clones);
763 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
765 arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
767 if (arrival_tag == NULL)
769 clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
770 if (clone_tag == NULL)
774 /* See tun_uninit, so it's done after the vnet_sysuninit() */
782 static moduledata_t tuntap_mod = {
788 /* We'll only ever have these two, so no need for a macro. */
789 static moduledata_t tun_mod = { "if_tun", NULL, 0 };
790 static moduledata_t tap_mod = { "if_tap", NULL, 0 };
792 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
793 MODULE_VERSION(if_tuntap, 1);
794 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
795 MODULE_VERSION(if_tun, 1);
796 DECLARE_MODULE(if_tap, tap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
797 MODULE_VERSION(if_tap, 1);
800 tun_create_device(struct tuntap_driver *drv, int unit, struct ucred *cr,
801 struct cdev **dev, const char *name)
803 struct make_dev_args args;
804 struct tuntap_softc *tp;
807 tp = malloc(sizeof(*tp), M_TUN, M_WAITOK | M_ZERO);
808 mtx_init(&tp->tun_mtx, "tun_mtx", NULL, MTX_DEF);
809 cv_init(&tp->tun_cv, "tun_condvar");
810 tp->tun_flags = drv->ident_flags;
813 make_dev_args_init(&args);
815 args.mda_flags = MAKEDEV_REF;
816 args.mda_devsw = &drv->cdevsw;
818 args.mda_uid = UID_UUCP;
819 args.mda_gid = GID_DIALER;
820 args.mda_mode = 0600;
821 args.mda_unit = unit;
822 args.mda_si_drv1 = tp;
823 error = make_dev_s(&args, dev, "%s", name);
829 KASSERT((*dev)->si_drv1 != NULL,
830 ("Failed to set si_drv1 at %s creation", name));
832 knlist_init_mtx(&tp->tun_rsel.si_note, &tp->tun_mtx);
834 TAILQ_INSERT_TAIL(&tunhead, tp, tun_list);
840 tunstart(struct ifnet *ifp)
842 struct tuntap_softc *tp = ifp->if_softc;
845 TUNDEBUG(ifp, "starting\n");
846 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
847 IFQ_LOCK(&ifp->if_snd);
848 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
850 IFQ_UNLOCK(&ifp->if_snd);
853 IFQ_UNLOCK(&ifp->if_snd);
857 if (tp->tun_flags & TUN_RWAIT) {
858 tp->tun_flags &= ~TUN_RWAIT;
861 selwakeuppri(&tp->tun_rsel, PZERO + 1);
862 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
863 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
865 pgsigio(&tp->tun_sigio, SIGIO, 0);
873 * queue packets from higher level ready to put out
876 tunstart_l2(struct ifnet *ifp)
878 struct tuntap_softc *tp = ifp->if_softc;
880 TUNDEBUG(ifp, "starting\n");
883 * do not junk pending output if we are in VMnet mode.
884 * XXX: can this do any harm because of queue overflow?
888 if (((tp->tun_flags & TUN_VMNET) == 0) &&
889 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
893 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
896 IF_DEQUEUE(&ifp->if_snd, m);
899 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
908 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
910 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
911 if (tp->tun_flags & TUN_RWAIT) {
912 tp->tun_flags &= ~TUN_RWAIT;
916 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
918 pgsigio(&tp->tun_sigio, SIGIO, 0);
922 selwakeuppri(&tp->tun_rsel, PZERO+1);
923 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
924 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
927 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
931 /* XXX: should return an error code so it can fail. */
933 tuncreate(struct cdev *dev)
935 struct tuntap_driver *drv;
936 struct tuntap_softc *tp;
938 struct ether_addr eaddr;
944 ("si_drv1 should have been initialized at creation"));
947 iflags = IFF_MULTICAST;
948 if ((tp->tun_flags & TUN_L2) != 0) {
950 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
953 iflags |= IFF_POINTOPOINT;
955 ifp = tp->tun_ifp = if_alloc(type);
957 panic("%s%d: failed to if_alloc() interface.\n",
958 drv->cdevsw.d_name, dev2unit(dev));
960 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
961 ifp->if_ioctl = tunifioctl;
962 ifp->if_flags = iflags;
963 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
964 ifp->if_capabilities |= IFCAP_LINKSTATE;
965 ifp->if_capenable |= IFCAP_LINKSTATE;
967 if ((tp->tun_flags & TUN_L2) != 0) {
968 ifp->if_mtu = ETHERMTU;
969 ifp->if_init = tunifinit;
970 ifp->if_start = tunstart_l2;
972 ether_gen_addr(ifp, &eaddr);
973 ether_ifattach(ifp, eaddr.octet);
975 ifp->if_mtu = TUNMTU;
976 ifp->if_start = tunstart;
977 ifp->if_output = tunoutput;
979 ifp->if_snd.ifq_drv_maxlen = 0;
980 IFQ_SET_READY(&ifp->if_snd);
983 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
987 tp->tun_flags |= TUN_INITED;
990 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
991 ifp->if_xname, dev2unit(dev));
995 tunrename(void *arg __unused, struct ifnet *ifp)
997 struct tuntap_softc *tp;
1000 if ((ifp->if_flags & IFF_RENAMING) == 0)
1003 if (tuntap_driver_from_ifnet(ifp) == NULL)
1007 * We need to grab the ioctl sx long enough to make sure the softc is
1008 * still there. If it is, we can safely try to busy the tun device.
1009 * The busy may fail if the device is currently dying, in which case
1010 * we do nothing. If it doesn't fail, the busy count stops the device
1011 * from dying until we've created the alias (that will then be
1012 * subsequently destroyed).
1014 sx_xlock(&tun_ioctl_sx);
1017 sx_xunlock(&tun_ioctl_sx);
1020 error = tun_busy(tp);
1021 sx_xunlock(&tun_ioctl_sx);
1024 if (tp->tun_alias != NULL) {
1025 destroy_dev(tp->tun_alias);
1026 tp->tun_alias = NULL;
1029 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
1033 * Failure's ok, aliases are created on a best effort basis. If a
1034 * tun user/consumer decides to rename the interface to conflict with
1035 * another device (non-ifnet) on the system, we will assume they know
1036 * what they are doing. make_dev_alias_p won't touch tun_alias on
1037 * failure, so we use it but ignore the return value.
1039 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1046 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1049 struct tuntap_softc *tp;
1050 int error, tunflags;
1053 CURVNET_SET(TD_TO_VNET(td));
1054 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1057 return (error); /* Shouldn't happen */
1062 ("si_drv1 should have been initialized at creation"));
1065 if ((tp->tun_flags & TUN_INITED) == 0) {
1070 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1076 error = tun_busy_locked(tp);
1077 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1080 if ((tp->tun_flags & TUN_L2) != 0) {
1081 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1082 sizeof(tp->tun_ether.octet));
1084 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1085 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1088 ifp->if_flags |= IFF_UP;
1091 tp->tun_pid = td->td_proc->p_pid;
1092 tp->tun_flags |= TUN_OPEN;
1094 if_link_state_change(ifp, LINK_STATE_UP);
1095 TUNDEBUG(ifp, "open\n");
1099 * This can fail with either ENOENT or EBUSY. This is in the middle of
1100 * d_open, so ENOENT should not be possible. EBUSY is possible, but
1101 * the only cdevpriv dtor being set will be tundtor and the softc being
1102 * passed is constant for a given cdev. We ignore the possible error
1103 * because of this as either "unlikely" or "not actually a problem."
1105 (void)devfs_set_cdevpriv(tp, tundtor);
1111 * tundtor - tear down the device - mark i/f down & delete
1118 struct tuntap_softc *tp;
1129 * Realistically, we can't be obstinate here. This only means that the
1130 * tuntap device was closed out of order, and the last closer wasn't the
1131 * controller. These are still good to know about, though, as software
1132 * should avoid multiple processes with a tuntap device open and
1133 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1136 if (p->p_pid != tp->tun_pid) {
1138 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1139 p->p_pid, p->p_comm, tp->tun_dev->si_name);
1143 * junk all pending output
1145 CURVNET_SET(ifp->if_vnet);
1148 if ((tp->tun_flags & TUN_L2) != 0) {
1150 IF_DRAIN(&ifp->if_snd);
1152 IFQ_PURGE(&ifp->if_snd);
1155 /* For vmnet, we won't do most of the address/route bits */
1156 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1157 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1160 if (ifp->if_flags & IFF_UP) {
1166 /* Delete all addresses and routes which reference this interface. */
1167 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1170 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1172 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1173 /* deal w/IPv4 PtP destination; unlocked read */
1174 if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1175 rtinit(ifa, (int)RTM_DELETE,
1176 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1178 rtinit(ifa, (int)RTM_DELETE, 0);
1186 if_link_state_change(ifp, LINK_STATE_DOWN);
1189 funsetown(&tp->tun_sigio);
1190 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1191 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1192 TUNDEBUG (ifp, "closed\n");
1193 tp->tun_flags &= ~TUN_OPEN;
1195 tun_vnethdr_set(ifp, 0);
1197 tun_unbusy_locked(tp);
1202 tuninit(struct ifnet *ifp)
1204 struct tuntap_softc *tp = ifp->if_softc;
1206 struct epoch_tracker et;
1210 TUNDEBUG(ifp, "tuninit\n");
1213 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1214 if ((tp->tun_flags & TUN_L2) == 0) {
1215 ifp->if_flags |= IFF_UP;
1216 getmicrotime(&ifp->if_lastchange);
1218 NET_EPOCH_ENTER(et);
1219 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1220 if (ifa->ifa_addr->sa_family == AF_INET) {
1221 struct sockaddr_in *si;
1223 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1224 if (si && si->sin_addr.s_addr) {
1225 tp->tun_flags |= TUN_DSTADDR;
1234 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1236 /* attempt to start output */
1243 * Used only for l2 tunnel.
1246 tunifinit(void *xtp)
1248 struct tuntap_softc *tp;
1250 tp = (struct tuntap_softc *)xtp;
1251 tuninit(tp->tun_ifp);
1255 * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1256 * current value of ifp->if_capenable.
1259 tun_caps_changed(struct ifnet *ifp)
1261 uint64_t hwassist = 0;
1263 TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1264 if (ifp->if_capenable & IFCAP_TXCSUM)
1265 hwassist |= CSUM_TCP | CSUM_UDP;
1266 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1267 hwassist |= CSUM_TCP_IPV6
1269 if (ifp->if_capenable & IFCAP_TSO4)
1270 hwassist |= CSUM_IP_TSO;
1271 if (ifp->if_capenable & IFCAP_TSO6)
1272 hwassist |= CSUM_IP6_TSO;
1273 ifp->if_hwassist = hwassist;
1277 * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1278 * if_capabilities and if_capenable as needed.
1281 tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1283 struct tuntap_softc *tp = ifp->if_softc;
1285 TUN_LOCK_ASSERT(tp);
1287 if (tp->tun_vhdrlen == vhdrlen)
1291 * Update if_capabilities to reflect the
1292 * functionalities offered by the virtio-net
1296 ifp->if_capabilities |=
1299 ifp->if_capabilities &=
1302 * Disable any capabilities that we don't
1305 ifp->if_capenable &= ifp->if_capabilities;
1306 tun_caps_changed(ifp);
1307 tp->tun_vhdrlen = vhdrlen;
1309 TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1310 vhdrlen, ifp->if_capabilities);
1314 * Process an ioctl request.
1317 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1319 struct ifreq *ifr = (struct ifreq *)data;
1320 struct tuntap_softc *tp;
1322 struct ifmediareq *ifmr;
1323 int dummy, error = 0;
1327 sx_xlock(&tun_ioctl_sx);
1333 l2tun = (tp->tun_flags & TUN_L2) != 0;
1336 ifs = (struct ifstat *)data;
1339 snprintf(ifs->ascii, sizeof(ifs->ascii),
1340 "\tOpened by PID %d\n", tp->tun_pid);
1342 ifs->ascii[0] = '\0';
1347 error = ether_ioctl(ifp, cmd, data);
1351 TUNDEBUG(ifp, "address set\n");
1354 ifp->if_mtu = ifr->ifr_mtu;
1355 TUNDEBUG(ifp, "mtu set\n");
1367 ifmr = (struct ifmediareq *)data;
1368 dummy = ifmr->ifm_count;
1369 ifmr->ifm_count = 1;
1370 ifmr->ifm_status = IFM_AVALID;
1371 ifmr->ifm_active = IFM_ETHER;
1372 if (tp->tun_flags & TUN_OPEN)
1373 ifmr->ifm_status |= IFM_ACTIVE;
1374 ifmr->ifm_current = ifmr->ifm_active;
1376 int media = IFM_ETHER;
1377 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1382 ifp->if_capenable = ifr->ifr_reqcap;
1383 tun_caps_changed(ifp);
1385 VLAN_CAPABILITIES(ifp);
1389 error = ether_ioctl(ifp, cmd, data);
1395 sx_xunlock(&tun_ioctl_sx);
1400 * tunoutput - queue packets from higher level ready to put out.
1403 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1406 struct tuntap_softc *tp = ifp->if_softc;
1407 u_short cached_tun_flags;
1411 TUNDEBUG (ifp, "tunoutput\n");
1414 error = mac_ifnet_check_transmit(ifp, m0);
1421 /* Could be unlocked read? */
1423 cached_tun_flags = tp->tun_flags;
1425 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1426 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1431 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1436 /* BPF writes need to be handled specially. */
1437 if (dst->sa_family == AF_UNSPEC)
1438 bcopy(dst->sa_data, &af, sizeof(af));
1440 af = dst->sa_family;
1442 if (bpf_peers_present(ifp->if_bpf))
1443 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1445 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1446 if (cached_tun_flags & TUN_LMODE) {
1447 /* allocate space for sockaddr */
1448 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1450 /* if allocation failed drop packet */
1452 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1453 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1456 bcopy(dst, m0->m_data, dst->sa_len);
1460 if (cached_tun_flags & TUN_IFHEAD) {
1461 /* Prepend the address family */
1462 M_PREPEND(m0, 4, M_NOWAIT);
1464 /* if allocation failed drop packet */
1466 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1467 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1470 *(u_int32_t *)m0->m_data = htonl(af);
1477 return (EAFNOSUPPORT);
1481 error = (ifp->if_transmit)(ifp, m0);
1484 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1489 * the cdevsw interface is now pretty minimal.
1492 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1495 struct ifreq ifr, *ifrp;
1496 struct tuntap_softc *tp = dev->si_drv1;
1497 struct ifnet *ifp = TUN2IFP(tp);
1498 struct tuninfo *tunp;
1499 int error, iflags, ival;
1502 l2tun = (tp->tun_flags & TUN_L2) != 0;
1504 /* tap specific ioctls */
1506 /* VMware/VMnet port ioctl's */
1507 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1508 defined(COMPAT_FREEBSD4)
1510 ival = IOCPARM_IVAL(data);
1511 data = (caddr_t)&ival;
1514 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1515 iflags = *(int *)data;
1516 iflags &= TUN_VMIO_FLAG_MASK;
1517 iflags &= ~IFF_CANTCHANGE;
1521 ifp->if_flags = iflags |
1522 (ifp->if_flags & IFF_CANTCHANGE);
1526 case SIOCGIFADDR: /* get MAC address of the remote side */
1528 bcopy(&tp->tun_ether.octet, data,
1529 sizeof(tp->tun_ether.octet));
1533 case SIOCSIFADDR: /* set MAC address of the remote side */
1535 bcopy(data, &tp->tun_ether.octet,
1536 sizeof(tp->tun_ether.octet));
1541 ival = *(int *)data;
1543 ival != sizeof(struct virtio_net_hdr) &&
1544 ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1548 tun_vnethdr_set(ifp, ival);
1554 *(int *)data = tp->tun_vhdrlen;
1560 /* Fall through to the common ioctls if unhandled */
1566 tp->tun_flags |= TUN_LMODE;
1567 tp->tun_flags &= ~TUN_IFHEAD;
1569 tp->tun_flags &= ~TUN_LMODE;
1576 tp->tun_flags |= TUN_IFHEAD;
1577 tp->tun_flags &= ~TUN_LMODE;
1579 tp->tun_flags &= ~TUN_IFHEAD;
1585 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1590 /* deny this if UP */
1591 if (TUN2IFP(tp)->if_flags & IFF_UP)
1594 switch (*(int *)data & ~IFF_MULTICAST) {
1595 case IFF_POINTOPOINT:
1598 TUN2IFP(tp)->if_flags &=
1599 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1600 TUN2IFP(tp)->if_flags |= *(int *)data;
1611 tp->tun_pid = curthread->td_proc->p_pid;
1616 /* Fall through to the common ioctls if unhandled */
1621 ifrp = (struct ifreq *)data;
1622 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1626 tunp = (struct tuninfo *)data;
1627 if (TUN2IFP(tp)->if_type != tunp->type)
1628 return (EPROTOTYPE);
1630 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1631 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1632 ifr.ifr_mtu = tunp->mtu;
1633 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1634 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1642 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1646 tunp = (struct tuninfo *)data;
1648 tunp->mtu = TUN2IFP(tp)->if_mtu;
1649 tunp->type = TUN2IFP(tp)->if_type;
1650 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1654 tundebug = *(int *)data;
1657 *(int *)data = tundebug;
1664 tp->tun_flags |= TUN_ASYNC;
1666 tp->tun_flags &= ~TUN_ASYNC;
1670 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1672 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1673 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1674 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1675 *(int *)data += mb->m_len;
1676 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1681 return (fsetown(*(int *)data, &tp->tun_sigio));
1684 *(int *)data = fgetown(&tp->tun_sigio);
1687 /* This is deprecated, FIOSETOWN should be used instead. */
1689 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1691 /* This is deprecated, FIOGETOWN should be used instead. */
1693 *(int *)data = -fgetown(&tp->tun_sigio);
1703 * The cdevsw read interface - reads a packet at a time, or at
1704 * least as much of a packet as can be read.
1707 tunread(struct cdev *dev, struct uio *uio, int flag)
1709 struct tuntap_softc *tp = dev->si_drv1;
1710 struct ifnet *ifp = TUN2IFP(tp);
1715 TUNDEBUG (ifp, "read\n");
1717 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1719 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1723 tp->tun_flags &= ~TUN_RWAIT;
1726 IFQ_DEQUEUE(&ifp->if_snd, m);
1729 if (flag & O_NONBLOCK) {
1731 return (EWOULDBLOCK);
1733 tp->tun_flags |= TUN_RWAIT;
1734 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1743 if ((tp->tun_flags & TUN_L2) != 0)
1746 len = min(tp->tun_vhdrlen, uio->uio_resid);
1748 struct virtio_net_hdr_mrg_rxbuf vhdr;
1750 bzero(&vhdr, sizeof(vhdr));
1751 if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1752 m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1755 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1756 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1757 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1758 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1759 vhdr.hdr.csum_offset);
1760 error = uiomove(&vhdr, len, uio);
1763 while (m && uio->uio_resid > 0 && error == 0) {
1764 len = min(uio->uio_resid, m->m_len);
1766 error = uiomove(mtod(m, void *), len, uio);
1771 TUNDEBUG(ifp, "Dropping mbuf\n");
1778 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1779 struct virtio_net_hdr_mrg_rxbuf *vhdr)
1781 struct epoch_tracker et;
1782 struct ether_header *eh;
1788 * Only pass a unicast frame to ether_input(), if it would
1789 * actually have been received by non-virtual hardware.
1791 if (m->m_len < sizeof(struct ether_header)) {
1796 eh = mtod(m, struct ether_header *);
1798 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1799 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1800 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1805 if (vhdr != NULL && virtio_net_rx_csum(m, &vhdr->hdr)) {
1810 /* Pass packet up to parent. */
1811 CURVNET_SET(ifp->if_vnet);
1812 NET_EPOCH_ENTER(et);
1813 (*ifp->if_input)(ifp, m);
1816 /* ibytes are counted in parent */
1817 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1822 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1824 struct epoch_tracker et;
1829 /* Could be unlocked read? */
1831 if (tp->tun_flags & TUN_IFHEAD) {
1833 if (m->m_len < sizeof(family) &&
1834 (m = m_pullup(m, sizeof(family))) == NULL)
1836 family = ntohl(*mtod(m, u_int32_t *));
1837 m_adj(m, sizeof(family));
1843 BPF_MTAP2(ifp, &family, sizeof(family), m);
1858 return (EAFNOSUPPORT);
1860 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1861 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1862 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1863 CURVNET_SET(ifp->if_vnet);
1864 M_SETFIB(m, ifp->if_fib);
1865 NET_EPOCH_ENTER(et);
1866 netisr_dispatch(isr, m);
1873 * the cdevsw write interface - an atomic write is a packet - or else!
1876 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1878 struct virtio_net_hdr_mrg_rxbuf vhdr;
1879 struct tuntap_softc *tp;
1883 int align, vhdrlen, error;
1888 TUNDEBUG(ifp, "tunwrite\n");
1889 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1890 /* ignore silently */
1893 if (uio->uio_resid == 0)
1896 l2tun = (tp->tun_flags & TUN_L2) != 0;
1897 mru = l2tun ? TAPMRU : TUNMRU;
1898 vhdrlen = tp->tun_vhdrlen;
1901 align = ETHER_ALIGN;
1903 } else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1904 mru += sizeof(uint32_t); /* family */
1905 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1906 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1911 error = uiomove(&vhdr, vhdrlen, uio);
1914 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1915 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1916 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1917 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1918 vhdr.hdr.csum_offset);
1921 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1922 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1926 m->m_pkthdr.rcvif = ifp;
1928 mac_ifnet_create_mbuf(ifp, m);
1932 return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1934 return (tunwrite_l3(tp, m));
1938 * tunpoll - the poll interface, this is only useful on reads
1939 * really. The write detect always returns true, write never blocks
1940 * anyway, it either accepts the packet or drops it.
1943 tunpoll(struct cdev *dev, int events, struct thread *td)
1945 struct tuntap_softc *tp = dev->si_drv1;
1946 struct ifnet *ifp = TUN2IFP(tp);
1949 TUNDEBUG(ifp, "tunpoll\n");
1951 if (events & (POLLIN | POLLRDNORM)) {
1952 IFQ_LOCK(&ifp->if_snd);
1953 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1954 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1955 revents |= events & (POLLIN | POLLRDNORM);
1957 TUNDEBUG(ifp, "tunpoll waiting\n");
1958 selrecord(td, &tp->tun_rsel);
1960 IFQ_UNLOCK(&ifp->if_snd);
1962 revents |= events & (POLLOUT | POLLWRNORM);
1968 * tunkqfilter - support for the kevent() system call.
1971 tunkqfilter(struct cdev *dev, struct knote *kn)
1973 struct tuntap_softc *tp = dev->si_drv1;
1974 struct ifnet *ifp = TUN2IFP(tp);
1976 switch(kn->kn_filter) {
1978 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1979 ifp->if_xname, dev2unit(dev));
1980 kn->kn_fop = &tun_read_filterops;
1984 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1985 ifp->if_xname, dev2unit(dev));
1986 kn->kn_fop = &tun_write_filterops;
1990 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1991 ifp->if_xname, dev2unit(dev));
1996 knlist_add(&tp->tun_rsel.si_note, kn, 0);
2002 * Return true of there is data in the interface queue.
2005 tunkqread(struct knote *kn, long hint)
2008 struct tuntap_softc *tp = kn->kn_hook;
2009 struct cdev *dev = tp->tun_dev;
2010 struct ifnet *ifp = TUN2IFP(tp);
2012 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
2014 "%s have data in the queue. Len = %d, minor = %#x\n",
2015 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
2019 "%s waiting for data, minor = %#x\n", ifp->if_xname,
2028 * Always can write, always return MTU in kn->data.
2031 tunkqwrite(struct knote *kn, long hint)
2033 struct tuntap_softc *tp = kn->kn_hook;
2034 struct ifnet *ifp = TUN2IFP(tp);
2036 kn->kn_data = ifp->if_mtu;
2042 tunkqdetach(struct knote *kn)
2044 struct tuntap_softc *tp = kn->kn_hook;
2046 knlist_remove(&tp->tun_rsel.si_note, kn, 0);