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_init = tunifinit;
969 ifp->if_start = tunstart_l2;
971 ether_gen_addr(ifp, &eaddr);
972 ether_ifattach(ifp, eaddr.octet);
974 ifp->if_mtu = TUNMTU;
975 ifp->if_start = tunstart;
976 ifp->if_output = tunoutput;
978 ifp->if_snd.ifq_drv_maxlen = 0;
979 IFQ_SET_READY(&ifp->if_snd);
982 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
986 tp->tun_flags |= TUN_INITED;
989 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
990 ifp->if_xname, dev2unit(dev));
994 tunrename(void *arg __unused, struct ifnet *ifp)
996 struct tuntap_softc *tp;
999 if ((ifp->if_flags & IFF_RENAMING) == 0)
1002 if (tuntap_driver_from_ifnet(ifp) == NULL)
1006 * We need to grab the ioctl sx long enough to make sure the softc is
1007 * still there. If it is, we can safely try to busy the tun device.
1008 * The busy may fail if the device is currently dying, in which case
1009 * we do nothing. If it doesn't fail, the busy count stops the device
1010 * from dying until we've created the alias (that will then be
1011 * subsequently destroyed).
1013 sx_xlock(&tun_ioctl_sx);
1016 sx_xunlock(&tun_ioctl_sx);
1019 error = tun_busy(tp);
1020 sx_xunlock(&tun_ioctl_sx);
1023 if (tp->tun_alias != NULL) {
1024 destroy_dev(tp->tun_alias);
1025 tp->tun_alias = NULL;
1028 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
1032 * Failure's ok, aliases are created on a best effort basis. If a
1033 * tun user/consumer decides to rename the interface to conflict with
1034 * another device (non-ifnet) on the system, we will assume they know
1035 * what they are doing. make_dev_alias_p won't touch tun_alias on
1036 * failure, so we use it but ignore the return value.
1038 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1045 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1048 struct tuntap_softc *tp;
1049 int error, tunflags;
1052 CURVNET_SET(TD_TO_VNET(td));
1053 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1056 return (error); /* Shouldn't happen */
1061 ("si_drv1 should have been initialized at creation"));
1064 if ((tp->tun_flags & TUN_INITED) == 0) {
1069 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1075 error = tun_busy_locked(tp);
1076 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1079 if ((tp->tun_flags & TUN_L2) != 0) {
1080 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1081 sizeof(tp->tun_ether.octet));
1083 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1084 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1087 ifp->if_flags |= IFF_UP;
1090 tp->tun_pid = td->td_proc->p_pid;
1091 tp->tun_flags |= TUN_OPEN;
1093 if_link_state_change(ifp, LINK_STATE_UP);
1094 TUNDEBUG(ifp, "open\n");
1098 * This can fail with either ENOENT or EBUSY. This is in the middle of
1099 * d_open, so ENOENT should not be possible. EBUSY is possible, but
1100 * the only cdevpriv dtor being set will be tundtor and the softc being
1101 * passed is constant for a given cdev. We ignore the possible error
1102 * because of this as either "unlikely" or "not actually a problem."
1104 (void)devfs_set_cdevpriv(tp, tundtor);
1110 * tundtor - tear down the device - mark i/f down & delete
1117 struct tuntap_softc *tp;
1128 * Realistically, we can't be obstinate here. This only means that the
1129 * tuntap device was closed out of order, and the last closer wasn't the
1130 * controller. These are still good to know about, though, as software
1131 * should avoid multiple processes with a tuntap device open and
1132 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1135 if (p->p_pid != tp->tun_pid) {
1137 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1138 p->p_pid, p->p_comm, tp->tun_dev->si_name);
1142 * junk all pending output
1144 CURVNET_SET(ifp->if_vnet);
1147 if ((tp->tun_flags & TUN_L2) != 0) {
1149 IF_DRAIN(&ifp->if_snd);
1151 IFQ_PURGE(&ifp->if_snd);
1154 /* For vmnet, we won't do most of the address/route bits */
1155 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1156 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1159 if (ifp->if_flags & IFF_UP) {
1165 /* Delete all addresses and routes which reference this interface. */
1166 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1169 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1171 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1172 /* deal w/IPv4 PtP destination; unlocked read */
1173 if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1174 rtinit(ifa, (int)RTM_DELETE,
1175 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1177 rtinit(ifa, (int)RTM_DELETE, 0);
1185 if_link_state_change(ifp, LINK_STATE_DOWN);
1188 funsetown(&tp->tun_sigio);
1189 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1190 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1191 TUNDEBUG (ifp, "closed\n");
1192 tp->tun_flags &= ~TUN_OPEN;
1194 tun_vnethdr_set(ifp, 0);
1196 tun_unbusy_locked(tp);
1201 tuninit(struct ifnet *ifp)
1203 struct tuntap_softc *tp = ifp->if_softc;
1205 struct epoch_tracker et;
1209 TUNDEBUG(ifp, "tuninit\n");
1212 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1213 if ((tp->tun_flags & TUN_L2) == 0) {
1214 ifp->if_flags |= IFF_UP;
1215 getmicrotime(&ifp->if_lastchange);
1217 NET_EPOCH_ENTER(et);
1218 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1219 if (ifa->ifa_addr->sa_family == AF_INET) {
1220 struct sockaddr_in *si;
1222 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1223 if (si && si->sin_addr.s_addr) {
1224 tp->tun_flags |= TUN_DSTADDR;
1233 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1235 /* attempt to start output */
1242 * Used only for l2 tunnel.
1245 tunifinit(void *xtp)
1247 struct tuntap_softc *tp;
1249 tp = (struct tuntap_softc *)xtp;
1250 tuninit(tp->tun_ifp);
1254 * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1255 * current value of ifp->if_capenable.
1258 tun_caps_changed(struct ifnet *ifp)
1260 uint64_t hwassist = 0;
1262 TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1263 if (ifp->if_capenable & IFCAP_TXCSUM)
1264 hwassist |= CSUM_TCP | CSUM_UDP;
1265 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1266 hwassist |= CSUM_TCP_IPV6
1268 if (ifp->if_capenable & IFCAP_TSO4)
1269 hwassist |= CSUM_IP_TSO;
1270 if (ifp->if_capenable & IFCAP_TSO6)
1271 hwassist |= CSUM_IP6_TSO;
1272 ifp->if_hwassist = hwassist;
1276 * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1277 * if_capabilities and if_capenable as needed.
1280 tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1282 struct tuntap_softc *tp = ifp->if_softc;
1284 TUN_LOCK_ASSERT(tp);
1286 if (tp->tun_vhdrlen == vhdrlen)
1290 * Update if_capabilities to reflect the
1291 * functionalities offered by the virtio-net
1295 ifp->if_capabilities |=
1298 ifp->if_capabilities &=
1301 * Disable any capabilities that we don't
1304 ifp->if_capenable &= ifp->if_capabilities;
1305 tun_caps_changed(ifp);
1306 tp->tun_vhdrlen = vhdrlen;
1308 TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1309 vhdrlen, ifp->if_capabilities);
1313 * Process an ioctl request.
1316 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1318 struct ifreq *ifr = (struct ifreq *)data;
1319 struct tuntap_softc *tp;
1321 struct ifmediareq *ifmr;
1322 int dummy, error = 0;
1326 sx_xlock(&tun_ioctl_sx);
1332 l2tun = (tp->tun_flags & TUN_L2) != 0;
1335 ifs = (struct ifstat *)data;
1338 snprintf(ifs->ascii, sizeof(ifs->ascii),
1339 "\tOpened by PID %d\n", tp->tun_pid);
1341 ifs->ascii[0] = '\0';
1346 error = ether_ioctl(ifp, cmd, data);
1350 TUNDEBUG(ifp, "address set\n");
1353 ifp->if_mtu = ifr->ifr_mtu;
1354 TUNDEBUG(ifp, "mtu set\n");
1366 ifmr = (struct ifmediareq *)data;
1367 dummy = ifmr->ifm_count;
1368 ifmr->ifm_count = 1;
1369 ifmr->ifm_status = IFM_AVALID;
1370 ifmr->ifm_active = IFM_ETHER;
1371 if (tp->tun_flags & TUN_OPEN)
1372 ifmr->ifm_status |= IFM_ACTIVE;
1373 ifmr->ifm_current = ifmr->ifm_active;
1375 int media = IFM_ETHER;
1376 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1381 ifp->if_capenable = ifr->ifr_reqcap;
1382 tun_caps_changed(ifp);
1384 VLAN_CAPABILITIES(ifp);
1388 error = ether_ioctl(ifp, cmd, data);
1394 sx_xunlock(&tun_ioctl_sx);
1399 * tunoutput - queue packets from higher level ready to put out.
1402 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1405 struct tuntap_softc *tp = ifp->if_softc;
1406 u_short cached_tun_flags;
1410 TUNDEBUG (ifp, "tunoutput\n");
1413 error = mac_ifnet_check_transmit(ifp, m0);
1420 /* Could be unlocked read? */
1422 cached_tun_flags = tp->tun_flags;
1424 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1425 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1430 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1435 /* BPF writes need to be handled specially. */
1436 if (dst->sa_family == AF_UNSPEC)
1437 bcopy(dst->sa_data, &af, sizeof(af));
1439 af = dst->sa_family;
1441 if (bpf_peers_present(ifp->if_bpf))
1442 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1444 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1445 if (cached_tun_flags & TUN_LMODE) {
1446 /* allocate space for sockaddr */
1447 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1449 /* if allocation failed drop packet */
1451 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1452 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1455 bcopy(dst, m0->m_data, dst->sa_len);
1459 if (cached_tun_flags & TUN_IFHEAD) {
1460 /* Prepend the address family */
1461 M_PREPEND(m0, 4, M_NOWAIT);
1463 /* if allocation failed drop packet */
1465 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1466 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1469 *(u_int32_t *)m0->m_data = htonl(af);
1476 return (EAFNOSUPPORT);
1480 error = (ifp->if_transmit)(ifp, m0);
1483 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1488 * the cdevsw interface is now pretty minimal.
1491 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1494 struct ifreq ifr, *ifrp;
1495 struct tuntap_softc *tp = dev->si_drv1;
1496 struct ifnet *ifp = TUN2IFP(tp);
1497 struct tuninfo *tunp;
1498 int error, iflags, ival;
1501 l2tun = (tp->tun_flags & TUN_L2) != 0;
1503 /* tap specific ioctls */
1505 /* VMware/VMnet port ioctl's */
1506 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1507 defined(COMPAT_FREEBSD4)
1509 ival = IOCPARM_IVAL(data);
1510 data = (caddr_t)&ival;
1513 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1514 iflags = *(int *)data;
1515 iflags &= TUN_VMIO_FLAG_MASK;
1516 iflags &= ~IFF_CANTCHANGE;
1520 ifp->if_flags = iflags |
1521 (ifp->if_flags & IFF_CANTCHANGE);
1525 case SIOCGIFADDR: /* get MAC address of the remote side */
1527 bcopy(&tp->tun_ether.octet, data,
1528 sizeof(tp->tun_ether.octet));
1532 case SIOCSIFADDR: /* set MAC address of the remote side */
1534 bcopy(data, &tp->tun_ether.octet,
1535 sizeof(tp->tun_ether.octet));
1540 ival = *(int *)data;
1542 ival != sizeof(struct virtio_net_hdr) &&
1543 ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1547 tun_vnethdr_set(ifp, ival);
1553 *(int *)data = tp->tun_vhdrlen;
1559 /* Fall through to the common ioctls if unhandled */
1565 tp->tun_flags |= TUN_LMODE;
1566 tp->tun_flags &= ~TUN_IFHEAD;
1568 tp->tun_flags &= ~TUN_LMODE;
1575 tp->tun_flags |= TUN_IFHEAD;
1576 tp->tun_flags &= ~TUN_LMODE;
1578 tp->tun_flags &= ~TUN_IFHEAD;
1584 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1589 /* deny this if UP */
1590 if (TUN2IFP(tp)->if_flags & IFF_UP)
1593 switch (*(int *)data & ~IFF_MULTICAST) {
1594 case IFF_POINTOPOINT:
1597 TUN2IFP(tp)->if_flags &=
1598 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1599 TUN2IFP(tp)->if_flags |= *(int *)data;
1610 tp->tun_pid = curthread->td_proc->p_pid;
1615 /* Fall through to the common ioctls if unhandled */
1620 ifrp = (struct ifreq *)data;
1621 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1625 tunp = (struct tuninfo *)data;
1626 if (TUN2IFP(tp)->if_type != tunp->type)
1627 return (EPROTOTYPE);
1629 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1630 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1631 ifr.ifr_mtu = tunp->mtu;
1632 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1633 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1641 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1645 tunp = (struct tuninfo *)data;
1647 tunp->mtu = TUN2IFP(tp)->if_mtu;
1648 tunp->type = TUN2IFP(tp)->if_type;
1649 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1653 tundebug = *(int *)data;
1656 *(int *)data = tundebug;
1663 tp->tun_flags |= TUN_ASYNC;
1665 tp->tun_flags &= ~TUN_ASYNC;
1669 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1671 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1672 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1673 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1674 *(int *)data += mb->m_len;
1675 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1680 return (fsetown(*(int *)data, &tp->tun_sigio));
1683 *(int *)data = fgetown(&tp->tun_sigio);
1686 /* This is deprecated, FIOSETOWN should be used instead. */
1688 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1690 /* This is deprecated, FIOGETOWN should be used instead. */
1692 *(int *)data = -fgetown(&tp->tun_sigio);
1702 * The cdevsw read interface - reads a packet at a time, or at
1703 * least as much of a packet as can be read.
1706 tunread(struct cdev *dev, struct uio *uio, int flag)
1708 struct tuntap_softc *tp = dev->si_drv1;
1709 struct ifnet *ifp = TUN2IFP(tp);
1714 TUNDEBUG (ifp, "read\n");
1716 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1718 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1722 tp->tun_flags &= ~TUN_RWAIT;
1725 IFQ_DEQUEUE(&ifp->if_snd, m);
1728 if (flag & O_NONBLOCK) {
1730 return (EWOULDBLOCK);
1732 tp->tun_flags |= TUN_RWAIT;
1733 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1742 if ((tp->tun_flags & TUN_L2) != 0)
1745 len = min(tp->tun_vhdrlen, uio->uio_resid);
1747 struct virtio_net_hdr_mrg_rxbuf vhdr;
1749 bzero(&vhdr, sizeof(vhdr));
1750 if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1751 m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1754 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1755 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1756 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1757 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1758 vhdr.hdr.csum_offset);
1759 error = uiomove(&vhdr, len, uio);
1762 while (m && uio->uio_resid > 0 && error == 0) {
1763 len = min(uio->uio_resid, m->m_len);
1765 error = uiomove(mtod(m, void *), len, uio);
1770 TUNDEBUG(ifp, "Dropping mbuf\n");
1777 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1778 struct virtio_net_hdr_mrg_rxbuf *vhdr)
1780 struct epoch_tracker et;
1781 struct ether_header *eh;
1787 * Only pass a unicast frame to ether_input(), if it would
1788 * actually have been received by non-virtual hardware.
1790 if (m->m_len < sizeof(struct ether_header)) {
1795 eh = mtod(m, struct ether_header *);
1797 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1798 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1799 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1804 if (vhdr != NULL && virtio_net_rx_csum(m, &vhdr->hdr)) {
1809 /* Pass packet up to parent. */
1810 CURVNET_SET(ifp->if_vnet);
1811 NET_EPOCH_ENTER(et);
1812 (*ifp->if_input)(ifp, m);
1815 /* ibytes are counted in parent */
1816 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1821 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1823 struct epoch_tracker et;
1828 /* Could be unlocked read? */
1830 if (tp->tun_flags & TUN_IFHEAD) {
1832 if (m->m_len < sizeof(family) &&
1833 (m = m_pullup(m, sizeof(family))) == NULL)
1835 family = ntohl(*mtod(m, u_int32_t *));
1836 m_adj(m, sizeof(family));
1842 BPF_MTAP2(ifp, &family, sizeof(family), m);
1857 return (EAFNOSUPPORT);
1859 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1860 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1861 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1862 CURVNET_SET(ifp->if_vnet);
1863 M_SETFIB(m, ifp->if_fib);
1864 NET_EPOCH_ENTER(et);
1865 netisr_dispatch(isr, m);
1872 * the cdevsw write interface - an atomic write is a packet - or else!
1875 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1877 struct virtio_net_hdr_mrg_rxbuf vhdr;
1878 struct tuntap_softc *tp;
1882 int align, vhdrlen, error;
1887 TUNDEBUG(ifp, "tunwrite\n");
1888 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1889 /* ignore silently */
1892 if (uio->uio_resid == 0)
1895 l2tun = (tp->tun_flags & TUN_L2) != 0;
1896 mru = l2tun ? TAPMRU : TUNMRU;
1897 vhdrlen = tp->tun_vhdrlen;
1900 align = ETHER_ALIGN;
1902 } else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1903 mru += sizeof(uint32_t); /* family */
1904 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1905 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1910 error = uiomove(&vhdr, vhdrlen, uio);
1913 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1914 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1915 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1916 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1917 vhdr.hdr.csum_offset);
1920 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1921 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1925 m->m_pkthdr.rcvif = ifp;
1927 mac_ifnet_create_mbuf(ifp, m);
1931 return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1933 return (tunwrite_l3(tp, m));
1937 * tunpoll - the poll interface, this is only useful on reads
1938 * really. The write detect always returns true, write never blocks
1939 * anyway, it either accepts the packet or drops it.
1942 tunpoll(struct cdev *dev, int events, struct thread *td)
1944 struct tuntap_softc *tp = dev->si_drv1;
1945 struct ifnet *ifp = TUN2IFP(tp);
1948 TUNDEBUG(ifp, "tunpoll\n");
1950 if (events & (POLLIN | POLLRDNORM)) {
1951 IFQ_LOCK(&ifp->if_snd);
1952 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1953 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1954 revents |= events & (POLLIN | POLLRDNORM);
1956 TUNDEBUG(ifp, "tunpoll waiting\n");
1957 selrecord(td, &tp->tun_rsel);
1959 IFQ_UNLOCK(&ifp->if_snd);
1961 revents |= events & (POLLOUT | POLLWRNORM);
1967 * tunkqfilter - support for the kevent() system call.
1970 tunkqfilter(struct cdev *dev, struct knote *kn)
1972 struct tuntap_softc *tp = dev->si_drv1;
1973 struct ifnet *ifp = TUN2IFP(tp);
1975 switch(kn->kn_filter) {
1977 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1978 ifp->if_xname, dev2unit(dev));
1979 kn->kn_fop = &tun_read_filterops;
1983 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1984 ifp->if_xname, dev2unit(dev));
1985 kn->kn_fop = &tun_write_filterops;
1989 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1990 ifp->if_xname, dev2unit(dev));
1995 knlist_add(&tp->tun_rsel.si_note, kn, 0);
2001 * Return true of there is data in the interface queue.
2004 tunkqread(struct knote *kn, long hint)
2007 struct tuntap_softc *tp = kn->kn_hook;
2008 struct cdev *dev = tp->tun_dev;
2009 struct ifnet *ifp = TUN2IFP(tp);
2011 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
2013 "%s have data in the queue. Len = %d, minor = %#x\n",
2014 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
2018 "%s waiting for data, minor = %#x\n", ifp->if_xname,
2027 * Always can write, always return MTU in kn->data.
2030 tunkqwrite(struct knote *kn, long hint)
2032 struct tuntap_softc *tp = kn->kn_hook;
2033 struct ifnet *ifp = TUN2IFP(tp);
2035 kn->kn_data = ifp->if_mtu;
2041 tunkqdetach(struct knote *kn)
2043 struct tuntap_softc *tp = kn->kn_hook;
2045 knlist_remove(&tp->tun_rsel.si_note, kn, 0);