1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
3 * SPDX-License-Identifier: BSD-2-Clause
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
48 #include "opt_inet6.h"
50 #include <sys/param.h>
54 #include <sys/systm.h>
57 #include <sys/module.h>
58 #include <sys/socket.h>
59 #include <sys/eventhandler.h>
60 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/sockio.h>
64 #include <sys/syslog.h>
65 #include <sys/ttycom.h>
67 #include <sys/selinfo.h>
68 #include <sys/signalvar.h>
69 #include <sys/filedesc.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
74 #include <sys/malloc.h>
75 #include <sys/random.h>
76 #include <sys/ctype.h>
78 #include <net/ethernet.h>
80 #include <net/if_var.h>
81 #include <net/if_clone.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84 #include <net/if_types.h>
85 #include <net/if_vlan_var.h>
86 #include <net/netisr.h>
87 #include <net/route.h>
89 #include <netinet/in.h>
91 #include <netinet/ip.h>
94 #include <netinet/ip6.h>
95 #include <netinet6/ip6_var.h>
97 #include <netinet/udp.h>
98 #include <netinet/tcp.h>
100 #include <net/if_tap.h>
101 #include <net/if_tun.h>
103 #include <dev/virtio/network/virtio_net.h>
105 #include <sys/queue.h>
106 #include <sys/condvar.h>
107 #include <security/mac/mac_framework.h>
109 struct tuntap_driver;
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.
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_UNUSED1 0x0008
124 #define TUN_UNUSED2 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
133 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
134 #define TUN_READY (TUN_OPEN | TUN_INITED)
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 */
147 #define TUN2IFP(sc) ((sc)->tun_ifp)
149 #define TUNDEBUG if (tundebug) if_printf
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);
155 #define TUN_VMIO_FLAG_MASK 0x0fff
158 * Interface capabilities of a tap device that supports the virtio-net
161 #define TAP_VNET_HDR_CAPS (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 \
162 | IFCAP_VLAN_HWCSUM \
163 | IFCAP_TSO | IFCAP_LRO \
166 #define TAP_ALL_OFFLOAD (CSUM_TSO | CSUM_TCP | CSUM_UDP |\
167 CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
170 * All mutable global variables in if_tun are locked using tunmtx, with
171 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
172 * which are static after setup.
174 static struct mtx tunmtx;
175 static eventhandler_tag arrival_tag;
176 static eventhandler_tag clone_tag;
177 static const char tunname[] = "tun";
178 static const char tapname[] = "tap";
179 static const char vmnetname[] = "vmnet";
180 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
181 static int tundebug = 0;
182 static int tundclone = 1;
183 static int tap_allow_uopen = 0; /* allow user devfs cloning */
184 static int tapuponopen = 0; /* IFF_UP on open() */
185 static int tapdclone = 1; /* enable devfs cloning */
187 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
188 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
190 static struct sx tun_ioctl_sx;
191 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
193 SYSCTL_DECL(_net_link);
195 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
196 "IP tunnel software network interface");
197 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
198 "Enable legacy devfs interface creation");
201 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
202 "Ethernet tunnel software network interface");
203 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
204 "Enable legacy devfs interface creation for all users");
205 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
206 "Bring interface up when /dev/tap is opened");
207 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
208 "Enable legacy devfs interface creation");
209 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
211 static int tun_create_device(struct tuntap_driver *drv, int unit,
212 struct ucred *cr, struct cdev **dev, const char *name);
213 static int tun_busy_locked(struct tuntap_softc *tp);
214 static void tun_unbusy_locked(struct tuntap_softc *tp);
215 static int tun_busy(struct tuntap_softc *tp);
216 static void tun_unbusy(struct tuntap_softc *tp);
218 static int tuntap_name2info(const char *name, int *unit, int *flags);
219 static void tunclone(void *arg, struct ucred *cred, char *name,
220 int namelen, struct cdev **dev);
221 static void tuncreate(struct cdev *dev);
222 static void tundtor(void *data);
223 static void tunrename(void *arg, struct ifnet *ifp);
224 static int tunifioctl(struct ifnet *, u_long, caddr_t);
225 static void tuninit(struct ifnet *);
226 static void tunifinit(void *xtp);
227 static int tuntapmodevent(module_t, int, void *);
228 static int tunoutput(struct ifnet *, struct mbuf *,
229 const struct sockaddr *, struct route *ro);
230 static void tunstart(struct ifnet *);
231 static void tunstart_l2(struct ifnet *);
233 static int tun_clone_match(struct if_clone *ifc, const char *name);
234 static int tap_clone_match(struct if_clone *ifc, const char *name);
235 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
236 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
237 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
238 static void tun_vnethdr_set(struct ifnet *ifp, int vhdrlen);
240 static d_open_t tunopen;
241 static d_read_t tunread;
242 static d_write_t tunwrite;
243 static d_ioctl_t tunioctl;
244 static d_poll_t tunpoll;
245 static d_kqfilter_t tunkqfilter;
247 static int tunkqread(struct knote *, long);
248 static int tunkqwrite(struct knote *, long);
249 static void tunkqdetach(struct knote *);
251 static struct filterops tun_read_filterops = {
254 .f_detach = tunkqdetach,
255 .f_event = tunkqread,
258 static struct filterops tun_write_filterops = {
261 .f_detach = tunkqdetach,
262 .f_event = tunkqwrite,
265 static struct tuntap_driver {
266 struct cdevsw cdevsw;
268 struct unrhdr *unrhdr;
269 struct clonedevs *clones;
270 ifc_match_t *clone_match_fn;
271 ifc_create_t *clone_create_fn;
272 ifc_destroy_t *clone_destroy_fn;
273 } tuntap_drivers[] = {
277 .d_version = D_VERSION,
278 .d_flags = D_NEEDMINOR,
284 .d_kqfilter = tunkqfilter,
287 .clone_match_fn = tun_clone_match,
288 .clone_create_fn = tun_clone_create,
289 .clone_destroy_fn = tun_clone_destroy,
292 .ident_flags = TUN_L2,
294 .d_version = D_VERSION,
295 .d_flags = D_NEEDMINOR,
301 .d_kqfilter = tunkqfilter,
304 .clone_match_fn = tap_clone_match,
305 .clone_create_fn = tun_clone_create,
306 .clone_destroy_fn = tun_clone_destroy,
309 .ident_flags = TUN_L2 | TUN_VMNET,
311 .d_version = D_VERSION,
312 .d_flags = D_NEEDMINOR,
318 .d_kqfilter = tunkqfilter,
321 .clone_match_fn = vmnet_clone_match,
322 .clone_create_fn = tun_clone_create,
323 .clone_destroy_fn = tun_clone_destroy,
327 struct tuntap_driver_cloner {
328 SLIST_ENTRY(tuntap_driver_cloner) link;
329 struct tuntap_driver *drv;
330 struct if_clone *cloner;
333 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
334 SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
336 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
339 * Mechanism for marking a tunnel device as busy so that we can safely do some
340 * orthogonal operations (such as operations on devices) without racing against
341 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
342 * open, to be woken up when the condition is alleviated.
345 tun_busy_locked(struct tuntap_softc *tp)
349 if ((tp->tun_flags & TUN_DYING) != 0) {
351 * Perhaps unintuitive, but the device is busy going away.
352 * Other interpretations of EBUSY from tun_busy make little
353 * sense, since making a busy device even more busy doesn't
354 * sound like a problem.
364 tun_unbusy_locked(struct tuntap_softc *tp)
368 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
371 /* Wake up anything that may be waiting on our busy tunnel. */
372 if (tp->tun_busy == 0)
373 cv_broadcast(&tp->tun_cv);
377 tun_busy(struct tuntap_softc *tp)
382 ret = tun_busy_locked(tp);
388 tun_unbusy(struct tuntap_softc *tp)
392 tun_unbusy_locked(tp);
397 * Sets unit and/or flags given the device name. Must be called with correct
401 tuntap_name2info(const char *name, int *outunit, int *outflags)
403 struct tuntap_driver *drv;
404 struct tuntap_driver_cloner *drvc;
413 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
414 * wants to be able to pass back a char * through the second param. We
415 * will always set that as NULL here, so we'll fake it.
417 dname = __DECONST(char *, name);
420 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
421 ("tuntap_driver_cloners failed to initialize"));
422 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
423 KASSERT(drvc->drv != NULL,
424 ("tuntap_driver_cloners entry not properly initialized"));
427 if (strcmp(name, drv->cdevsw.d_name) == 0) {
430 flags = drv->ident_flags;
434 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
436 flags = drv->ident_flags;
446 if (outflags != NULL)
452 * Get driver information from a set of flags specified. Masks the identifying
453 * part of the flags and compares it against all of the available
454 * tuntap_drivers. Must be called with correct vnet context.
456 static struct tuntap_driver *
457 tuntap_driver_from_flags(int tun_flags)
459 struct tuntap_driver *drv;
460 struct tuntap_driver_cloner *drvc;
462 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
463 ("tuntap_driver_cloners failed to initialize"));
464 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
465 KASSERT(drvc->drv != NULL,
466 ("tuntap_driver_cloners entry not properly initialized"));
468 if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->ident_flags)
476 tun_clone_match(struct if_clone *ifc, const char *name)
480 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
481 if ((tunflags & TUN_L2) == 0)
489 tap_clone_match(struct if_clone *ifc, const char *name)
493 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
494 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
502 vmnet_clone_match(struct if_clone *ifc, const char *name)
506 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
507 if ((tunflags & TUN_VMNET) != 0)
515 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
517 struct tuntap_driver *drv;
519 int err, i, tunflags, unit;
522 /* The name here tells us exactly what we're creating */
523 err = tuntap_name2info(name, &unit, &tunflags);
527 drv = tuntap_driver_from_flags(tunflags);
532 /* If this unit number is still available that's okay. */
533 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
536 unit = alloc_unr(drv->unrhdr);
539 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
541 /* find any existing device, or allocate new unit number */
543 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
546 /* No preexisting struct cdev *, create one */
548 i = tun_create_device(drv, unit, NULL, &dev, name);
556 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
559 char devname[SPECNAMELEN + 1];
560 struct tuntap_driver *drv;
561 int append_unit, i, u, tunflags;
568 CURVNET_SET(CRED_TO_VNET(cred));
569 if (tuntap_name2info(name, &u, &tunflags) != 0)
570 goto out; /* Not recognized */
572 if (u != -1 && u > IF_MAXUNIT)
573 goto out; /* Unit number too high */
575 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
576 if ((tunflags & TUN_L2) != 0) {
577 /* tap/vmnet allow user open with a sysctl */
578 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
580 mayclone = mayclone && tundclone;
584 * If tun cloning is enabled, only the superuser can create an
595 drv = tuntap_driver_from_flags(tunflags);
599 /* find any existing device, or allocate new unit number */
600 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
605 namelen = snprintf(devname, sizeof(devname), "%s%d",
610 i = tun_create_device(drv, u, cred, dev, name);
613 if_clone_create(name, namelen, NULL);
619 tun_destroy(struct tuntap_softc *tp)
623 tp->tun_flags |= TUN_DYING;
624 if (tp->tun_busy != 0)
625 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
629 CURVNET_SET(TUN2IFP(tp)->if_vnet);
631 /* destroy_dev will take care of any alias. */
632 destroy_dev(tp->tun_dev);
633 seldrain(&tp->tun_rsel);
634 knlist_clear(&tp->tun_rsel.si_note, 0);
635 knlist_destroy(&tp->tun_rsel.si_note);
636 if ((tp->tun_flags & TUN_L2) != 0) {
637 ether_ifdetach(TUN2IFP(tp));
639 bpfdetach(TUN2IFP(tp));
640 if_detach(TUN2IFP(tp));
642 sx_xlock(&tun_ioctl_sx);
643 TUN2IFP(tp)->if_softc = NULL;
644 sx_xunlock(&tun_ioctl_sx);
645 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
646 if_free(TUN2IFP(tp));
647 mtx_destroy(&tp->tun_mtx);
648 cv_destroy(&tp->tun_cv);
654 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
656 struct tuntap_softc *tp = ifp->if_softc;
659 TAILQ_REMOVE(&tunhead, tp, tun_list);
667 vnet_tun_init(const void *unused __unused)
669 struct tuntap_driver *drv;
670 struct tuntap_driver_cloner *drvc;
673 for (i = 0; i < nitems(tuntap_drivers); ++i) {
674 drv = &tuntap_drivers[i];
675 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
678 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
679 drv->clone_match_fn, drv->clone_create_fn,
680 drv->clone_destroy_fn);
681 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
684 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
685 vnet_tun_init, NULL);
688 vnet_tun_uninit(const void *unused __unused)
690 struct tuntap_driver_cloner *drvc;
692 while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
693 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
694 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
696 if_clone_detach(drvc->cloner);
700 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
701 vnet_tun_uninit, NULL);
704 tun_uninit(const void *unused __unused)
706 struct tuntap_driver *drv;
707 struct tuntap_softc *tp;
710 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
711 EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
712 drain_dev_clone_events();
715 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
716 TAILQ_REMOVE(&tunhead, tp, tun_list);
722 for (i = 0; i < nitems(tuntap_drivers); ++i) {
723 drv = &tuntap_drivers[i];
724 delete_unrhdr(drv->unrhdr);
725 clone_cleanup(&drv->clones);
727 mtx_destroy(&tunmtx);
729 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
731 static struct tuntap_driver *
732 tuntap_driver_from_ifnet(const struct ifnet *ifp)
734 struct tuntap_driver *drv;
740 for (i = 0; i < nitems(tuntap_drivers); ++i) {
741 drv = &tuntap_drivers[i];
742 if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
750 tuntapmodevent(module_t mod, int type, void *data)
752 struct tuntap_driver *drv;
757 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
758 for (i = 0; i < nitems(tuntap_drivers); ++i) {
759 drv = &tuntap_drivers[i];
760 clone_setup(&drv->clones);
761 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
763 arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
765 if (arrival_tag == NULL)
767 clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
768 if (clone_tag == NULL)
772 /* See tun_uninit, so it's done after the vnet_sysuninit() */
780 static moduledata_t tuntap_mod = {
786 /* We'll only ever have these two, so no need for a macro. */
787 static moduledata_t tun_mod = { "if_tun", NULL, 0 };
788 static moduledata_t tap_mod = { "if_tap", NULL, 0 };
790 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
791 MODULE_VERSION(if_tuntap, 1);
792 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
793 MODULE_VERSION(if_tun, 1);
794 DECLARE_MODULE(if_tap, tap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
795 MODULE_VERSION(if_tap, 1);
798 tun_create_device(struct tuntap_driver *drv, int unit, struct ucred *cr,
799 struct cdev **dev, const char *name)
801 struct make_dev_args args;
802 struct tuntap_softc *tp;
805 tp = malloc(sizeof(*tp), M_TUN, M_WAITOK | M_ZERO);
806 mtx_init(&tp->tun_mtx, "tun_mtx", NULL, MTX_DEF);
807 cv_init(&tp->tun_cv, "tun_condvar");
808 tp->tun_flags = drv->ident_flags;
811 make_dev_args_init(&args);
813 args.mda_flags = MAKEDEV_REF | MAKEDEV_CHECKNAME;
814 args.mda_devsw = &drv->cdevsw;
816 args.mda_uid = UID_UUCP;
817 args.mda_gid = GID_DIALER;
818 args.mda_mode = 0600;
819 args.mda_unit = unit;
820 args.mda_si_drv1 = tp;
821 error = make_dev_s(&args, dev, "%s", name);
827 KASSERT((*dev)->si_drv1 != NULL,
828 ("Failed to set si_drv1 at %s creation", name));
830 knlist_init_mtx(&tp->tun_rsel.si_note, &tp->tun_mtx);
832 TAILQ_INSERT_TAIL(&tunhead, tp, tun_list);
838 tunstart(struct ifnet *ifp)
840 struct tuntap_softc *tp = ifp->if_softc;
843 TUNDEBUG(ifp, "starting\n");
844 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
845 IFQ_LOCK(&ifp->if_snd);
846 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
848 IFQ_UNLOCK(&ifp->if_snd);
851 IFQ_UNLOCK(&ifp->if_snd);
855 if (tp->tun_flags & TUN_RWAIT) {
856 tp->tun_flags &= ~TUN_RWAIT;
859 selwakeuppri(&tp->tun_rsel, PZERO + 1);
860 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
861 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
863 pgsigio(&tp->tun_sigio, SIGIO, 0);
871 * queue packets from higher level ready to put out
874 tunstart_l2(struct ifnet *ifp)
876 struct tuntap_softc *tp = ifp->if_softc;
878 TUNDEBUG(ifp, "starting\n");
881 * do not junk pending output if we are in VMnet mode.
882 * XXX: can this do any harm because of queue overflow?
886 if (((tp->tun_flags & TUN_VMNET) == 0) &&
887 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
891 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
894 IF_DEQUEUE(&ifp->if_snd, m);
897 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
906 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
908 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
909 if (tp->tun_flags & TUN_RWAIT) {
910 tp->tun_flags &= ~TUN_RWAIT;
914 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
916 pgsigio(&tp->tun_sigio, SIGIO, 0);
920 selwakeuppri(&tp->tun_rsel, PZERO+1);
921 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
922 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
925 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
930 tap_transmit(struct ifnet *ifp, struct mbuf *m)
935 IFQ_HANDOFF(ifp, m, error);
939 /* XXX: should return an error code so it can fail. */
941 tuncreate(struct cdev *dev)
943 struct tuntap_driver *drv;
944 struct tuntap_softc *tp;
946 struct ether_addr eaddr;
952 ("si_drv1 should have been initialized at creation"));
955 iflags = IFF_MULTICAST;
956 if ((tp->tun_flags & TUN_L2) != 0) {
958 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
961 iflags |= IFF_POINTOPOINT;
963 ifp = tp->tun_ifp = if_alloc(type);
965 panic("%s%d: failed to if_alloc() interface.\n",
966 drv->cdevsw.d_name, dev2unit(dev));
968 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
969 ifp->if_ioctl = tunifioctl;
970 ifp->if_flags = iflags;
971 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
972 ifp->if_capabilities |= IFCAP_LINKSTATE;
973 ifp->if_capenable |= IFCAP_LINKSTATE;
975 if ((tp->tun_flags & TUN_L2) != 0) {
976 ifp->if_init = tunifinit;
977 ifp->if_start = tunstart_l2;
978 ifp->if_transmit = tap_transmit;
979 ifp->if_qflush = if_qflush;
981 ether_gen_addr(ifp, &eaddr);
982 ether_ifattach(ifp, eaddr.octet);
984 ifp->if_mtu = TUNMTU;
985 ifp->if_start = tunstart;
986 ifp->if_output = tunoutput;
988 ifp->if_snd.ifq_drv_maxlen = 0;
989 IFQ_SET_READY(&ifp->if_snd);
992 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
996 tp->tun_flags |= TUN_INITED;
999 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
1000 ifp->if_xname, dev2unit(dev));
1004 tunrename(void *arg __unused, struct ifnet *ifp)
1006 struct tuntap_softc *tp;
1009 if ((ifp->if_flags & IFF_RENAMING) == 0)
1012 if (tuntap_driver_from_ifnet(ifp) == NULL)
1016 * We need to grab the ioctl sx long enough to make sure the softc is
1017 * still there. If it is, we can safely try to busy the tun device.
1018 * The busy may fail if the device is currently dying, in which case
1019 * we do nothing. If it doesn't fail, the busy count stops the device
1020 * from dying until we've created the alias (that will then be
1021 * subsequently destroyed).
1023 sx_xlock(&tun_ioctl_sx);
1026 sx_xunlock(&tun_ioctl_sx);
1029 error = tun_busy(tp);
1030 sx_xunlock(&tun_ioctl_sx);
1033 if (tp->tun_alias != NULL) {
1034 destroy_dev(tp->tun_alias);
1035 tp->tun_alias = NULL;
1038 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
1042 * Failure's ok, aliases are created on a best effort basis. If a
1043 * tun user/consumer decides to rename the interface to conflict with
1044 * another device (non-ifnet) on the system, we will assume they know
1045 * what they are doing. make_dev_alias_p won't touch tun_alias on
1046 * failure, so we use it but ignore the return value.
1048 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1055 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1058 struct tuntap_softc *tp;
1059 int error, tunflags;
1062 CURVNET_SET(TD_TO_VNET(td));
1063 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1066 return (error); /* Shouldn't happen */
1071 ("si_drv1 should have been initialized at creation"));
1074 if ((tp->tun_flags & TUN_INITED) == 0) {
1079 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1085 error = tun_busy_locked(tp);
1086 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1089 if ((tp->tun_flags & TUN_L2) != 0) {
1090 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1091 sizeof(tp->tun_ether.octet));
1093 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1094 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1097 ifp->if_flags |= IFF_UP;
1100 tp->tun_pid = td->td_proc->p_pid;
1101 tp->tun_flags |= TUN_OPEN;
1103 if_link_state_change(ifp, LINK_STATE_UP);
1104 TUNDEBUG(ifp, "open\n");
1108 * This can fail with either ENOENT or EBUSY. This is in the middle of
1109 * d_open, so ENOENT should not be possible. EBUSY is possible, but
1110 * the only cdevpriv dtor being set will be tundtor and the softc being
1111 * passed is constant for a given cdev. We ignore the possible error
1112 * because of this as either "unlikely" or "not actually a problem."
1114 (void)devfs_set_cdevpriv(tp, tundtor);
1120 * tundtor - tear down the device - mark i/f down & delete
1127 struct tuntap_softc *tp;
1138 * Realistically, we can't be obstinate here. This only means that the
1139 * tuntap device was closed out of order, and the last closer wasn't the
1140 * controller. These are still good to know about, though, as software
1141 * should avoid multiple processes with a tuntap device open and
1142 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1145 if (p->p_pid != tp->tun_pid) {
1147 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1148 p->p_pid, p->p_comm, tp->tun_dev->si_name);
1152 * junk all pending output
1154 CURVNET_SET(ifp->if_vnet);
1157 if ((tp->tun_flags & TUN_L2) != 0) {
1159 IF_DRAIN(&ifp->if_snd);
1161 IFQ_PURGE(&ifp->if_snd);
1164 /* For vmnet, we won't do most of the address/route bits */
1165 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1166 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1169 if (ifp->if_flags & IFF_UP) {
1175 /* Delete all addresses and routes which reference this interface. */
1176 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1177 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1184 if_link_state_change(ifp, LINK_STATE_DOWN);
1187 funsetown(&tp->tun_sigio);
1188 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1189 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1190 TUNDEBUG (ifp, "closed\n");
1191 tp->tun_flags &= ~TUN_OPEN;
1193 tun_vnethdr_set(ifp, 0);
1195 tun_unbusy_locked(tp);
1200 tuninit(struct ifnet *ifp)
1202 struct tuntap_softc *tp = ifp->if_softc;
1204 TUNDEBUG(ifp, "tuninit\n");
1207 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1208 if ((tp->tun_flags & TUN_L2) == 0) {
1209 ifp->if_flags |= IFF_UP;
1210 getmicrotime(&ifp->if_lastchange);
1213 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1215 /* attempt to start output */
1222 * Used only for l2 tunnel.
1225 tunifinit(void *xtp)
1227 struct tuntap_softc *tp;
1229 tp = (struct tuntap_softc *)xtp;
1230 tuninit(tp->tun_ifp);
1234 * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1235 * current value of ifp->if_capenable.
1238 tun_caps_changed(struct ifnet *ifp)
1240 uint64_t hwassist = 0;
1242 TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1243 if (ifp->if_capenable & IFCAP_TXCSUM)
1244 hwassist |= CSUM_TCP | CSUM_UDP;
1245 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1246 hwassist |= CSUM_TCP_IPV6
1248 if (ifp->if_capenable & IFCAP_TSO4)
1249 hwassist |= CSUM_IP_TSO;
1250 if (ifp->if_capenable & IFCAP_TSO6)
1251 hwassist |= CSUM_IP6_TSO;
1252 ifp->if_hwassist = hwassist;
1256 * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1257 * if_capabilities and if_capenable as needed.
1260 tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1262 struct tuntap_softc *tp = ifp->if_softc;
1264 TUN_LOCK_ASSERT(tp);
1266 if (tp->tun_vhdrlen == vhdrlen)
1270 * Update if_capabilities to reflect the
1271 * functionalities offered by the virtio-net
1275 ifp->if_capabilities |=
1278 ifp->if_capabilities &=
1281 * Disable any capabilities that we don't
1284 ifp->if_capenable &= ifp->if_capabilities;
1285 tun_caps_changed(ifp);
1286 tp->tun_vhdrlen = vhdrlen;
1288 TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1289 vhdrlen, ifp->if_capabilities);
1293 * Process an ioctl request.
1296 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1298 struct ifreq *ifr = (struct ifreq *)data;
1299 struct tuntap_softc *tp;
1301 struct ifmediareq *ifmr;
1302 int dummy, error = 0;
1306 sx_xlock(&tun_ioctl_sx);
1312 l2tun = (tp->tun_flags & TUN_L2) != 0;
1315 ifs = (struct ifstat *)data;
1318 snprintf(ifs->ascii, sizeof(ifs->ascii),
1319 "\tOpened by PID %d\n", tp->tun_pid);
1321 ifs->ascii[0] = '\0';
1326 error = ether_ioctl(ifp, cmd, data);
1330 TUNDEBUG(ifp, "address set\n");
1333 ifp->if_mtu = ifr->ifr_mtu;
1334 TUNDEBUG(ifp, "mtu set\n");
1346 ifmr = (struct ifmediareq *)data;
1347 dummy = ifmr->ifm_count;
1348 ifmr->ifm_count = 1;
1349 ifmr->ifm_status = IFM_AVALID;
1350 ifmr->ifm_active = IFM_ETHER;
1351 if (tp->tun_flags & TUN_OPEN)
1352 ifmr->ifm_status |= IFM_ACTIVE;
1353 ifmr->ifm_current = ifmr->ifm_active;
1355 int media = IFM_ETHER;
1356 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1361 ifp->if_capenable = ifr->ifr_reqcap;
1362 tun_caps_changed(ifp);
1364 VLAN_CAPABILITIES(ifp);
1368 error = ether_ioctl(ifp, cmd, data);
1374 sx_xunlock(&tun_ioctl_sx);
1379 * tunoutput - queue packets from higher level ready to put out.
1382 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1385 struct tuntap_softc *tp = ifp->if_softc;
1386 u_short cached_tun_flags;
1390 TUNDEBUG (ifp, "tunoutput\n");
1393 error = mac_ifnet_check_transmit(ifp, m0);
1400 /* Could be unlocked read? */
1402 cached_tun_flags = tp->tun_flags;
1404 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1405 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1410 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1415 /* BPF writes need to be handled specially. */
1416 if (dst->sa_family == AF_UNSPEC)
1417 bcopy(dst->sa_data, &af, sizeof(af));
1419 af = RO_GET_FAMILY(ro, dst);
1421 BPF_MTAP2(ifp, &af, sizeof(af), m0);
1423 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1424 if (cached_tun_flags & TUN_LMODE) {
1425 /* allocate space for sockaddr */
1426 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1428 /* if allocation failed drop packet */
1430 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1431 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1434 bcopy(dst, m0->m_data, dst->sa_len);
1438 if (cached_tun_flags & TUN_IFHEAD) {
1439 /* Prepend the address family */
1440 M_PREPEND(m0, 4, M_NOWAIT);
1442 /* if allocation failed drop packet */
1444 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1445 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1448 *(u_int32_t *)m0->m_data = htonl(af);
1455 return (EAFNOSUPPORT);
1459 error = (ifp->if_transmit)(ifp, m0);
1462 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1467 * the cdevsw interface is now pretty minimal.
1470 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1473 struct ifreq ifr, *ifrp;
1474 struct tuntap_softc *tp = dev->si_drv1;
1475 struct ifnet *ifp = TUN2IFP(tp);
1476 struct tuninfo *tunp;
1477 int error, iflags, ival;
1480 l2tun = (tp->tun_flags & TUN_L2) != 0;
1482 /* tap specific ioctls */
1484 /* VMware/VMnet port ioctl's */
1485 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1486 defined(COMPAT_FREEBSD4)
1488 ival = IOCPARM_IVAL(data);
1489 data = (caddr_t)&ival;
1492 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1493 iflags = *(int *)data;
1494 iflags &= TUN_VMIO_FLAG_MASK;
1495 iflags &= ~IFF_CANTCHANGE;
1499 ifp->if_flags = iflags |
1500 (ifp->if_flags & IFF_CANTCHANGE);
1504 case SIOCGIFADDR: /* get MAC address of the remote side */
1506 bcopy(&tp->tun_ether.octet, data,
1507 sizeof(tp->tun_ether.octet));
1511 case SIOCSIFADDR: /* set MAC address of the remote side */
1513 bcopy(data, &tp->tun_ether.octet,
1514 sizeof(tp->tun_ether.octet));
1519 ival = *(int *)data;
1521 ival != sizeof(struct virtio_net_hdr) &&
1522 ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1526 tun_vnethdr_set(ifp, ival);
1532 *(int *)data = tp->tun_vhdrlen;
1538 /* Fall through to the common ioctls if unhandled */
1544 tp->tun_flags |= TUN_LMODE;
1545 tp->tun_flags &= ~TUN_IFHEAD;
1547 tp->tun_flags &= ~TUN_LMODE;
1554 tp->tun_flags |= TUN_IFHEAD;
1555 tp->tun_flags &= ~TUN_LMODE;
1557 tp->tun_flags &= ~TUN_IFHEAD;
1563 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1568 /* deny this if UP */
1569 if (TUN2IFP(tp)->if_flags & IFF_UP)
1572 switch (*(int *)data & ~IFF_MULTICAST) {
1573 case IFF_POINTOPOINT:
1576 TUN2IFP(tp)->if_flags &=
1577 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1578 TUN2IFP(tp)->if_flags |= *(int *)data;
1589 tp->tun_pid = curthread->td_proc->p_pid;
1594 /* Fall through to the common ioctls if unhandled */
1599 ifrp = (struct ifreq *)data;
1600 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1604 tunp = (struct tuninfo *)data;
1605 if (TUN2IFP(tp)->if_type != tunp->type)
1606 return (EPROTOTYPE);
1608 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1609 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1610 ifr.ifr_mtu = tunp->mtu;
1611 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1612 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1620 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1624 tunp = (struct tuninfo *)data;
1626 tunp->mtu = TUN2IFP(tp)->if_mtu;
1627 tunp->type = TUN2IFP(tp)->if_type;
1628 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1632 tundebug = *(int *)data;
1635 *(int *)data = tundebug;
1642 tp->tun_flags |= TUN_ASYNC;
1644 tp->tun_flags &= ~TUN_ASYNC;
1648 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1650 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1651 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1652 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1653 *(int *)data += mb->m_len;
1654 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1659 return (fsetown(*(int *)data, &tp->tun_sigio));
1662 *(int *)data = fgetown(&tp->tun_sigio);
1665 /* This is deprecated, FIOSETOWN should be used instead. */
1667 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1669 /* This is deprecated, FIOGETOWN should be used instead. */
1671 *(int *)data = -fgetown(&tp->tun_sigio);
1681 * The cdevsw read interface - reads a packet at a time, or at
1682 * least as much of a packet as can be read.
1685 tunread(struct cdev *dev, struct uio *uio, int flag)
1687 struct tuntap_softc *tp = dev->si_drv1;
1688 struct ifnet *ifp = TUN2IFP(tp);
1693 TUNDEBUG (ifp, "read\n");
1695 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1697 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1701 tp->tun_flags &= ~TUN_RWAIT;
1704 IFQ_DEQUEUE(&ifp->if_snd, m);
1707 if (flag & O_NONBLOCK) {
1709 return (EWOULDBLOCK);
1711 tp->tun_flags |= TUN_RWAIT;
1712 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1721 len = min(tp->tun_vhdrlen, uio->uio_resid);
1723 struct virtio_net_hdr_mrg_rxbuf vhdr;
1725 bzero(&vhdr, sizeof(vhdr));
1726 if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1727 m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1730 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1731 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1732 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1733 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1734 vhdr.hdr.csum_offset);
1735 error = uiomove(&vhdr, len, uio);
1738 while (m && uio->uio_resid > 0 && error == 0) {
1739 len = min(uio->uio_resid, m->m_len);
1741 error = uiomove(mtod(m, void *), len, uio);
1746 TUNDEBUG(ifp, "Dropping mbuf\n");
1753 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1754 struct virtio_net_hdr_mrg_rxbuf *vhdr)
1756 struct epoch_tracker et;
1757 struct ether_header *eh;
1763 * Only pass a unicast frame to ether_input(), if it would
1764 * actually have been received by non-virtual hardware.
1766 if (m->m_len < sizeof(struct ether_header)) {
1771 eh = mtod(m, struct ether_header *);
1773 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1774 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1775 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1780 if (vhdr != NULL && virtio_net_rx_csum(m, &vhdr->hdr)) {
1785 /* Pass packet up to parent. */
1786 CURVNET_SET(ifp->if_vnet);
1787 NET_EPOCH_ENTER(et);
1788 (*ifp->if_input)(ifp, m);
1791 /* ibytes are counted in parent */
1792 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1797 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1799 struct epoch_tracker et;
1804 /* Could be unlocked read? */
1806 if (tp->tun_flags & TUN_IFHEAD) {
1808 if (m->m_len < sizeof(family) &&
1809 (m = m_pullup(m, sizeof(family))) == NULL)
1811 family = ntohl(*mtod(m, u_int32_t *));
1812 m_adj(m, sizeof(family));
1818 BPF_MTAP2(ifp, &family, sizeof(family), m);
1833 return (EAFNOSUPPORT);
1835 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1836 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1837 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1838 CURVNET_SET(ifp->if_vnet);
1839 M_SETFIB(m, ifp->if_fib);
1840 NET_EPOCH_ENTER(et);
1841 netisr_dispatch(isr, m);
1848 * the cdevsw write interface - an atomic write is a packet - or else!
1851 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1853 struct virtio_net_hdr_mrg_rxbuf vhdr;
1854 struct tuntap_softc *tp;
1858 int align, vhdrlen, error;
1863 TUNDEBUG(ifp, "tunwrite\n");
1864 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1865 /* ignore silently */
1868 if (uio->uio_resid == 0)
1871 l2tun = (tp->tun_flags & TUN_L2) != 0;
1872 mru = l2tun ? TAPMRU : TUNMRU;
1873 vhdrlen = tp->tun_vhdrlen;
1876 align = ETHER_ALIGN;
1878 } else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1879 mru += sizeof(uint32_t); /* family */
1880 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1881 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1886 error = uiomove(&vhdr, vhdrlen, uio);
1889 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1890 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1891 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1892 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1893 vhdr.hdr.csum_offset);
1896 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1897 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1901 m->m_pkthdr.rcvif = ifp;
1903 mac_ifnet_create_mbuf(ifp, m);
1907 return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1909 return (tunwrite_l3(tp, m));
1913 * tunpoll - the poll interface, this is only useful on reads
1914 * really. The write detect always returns true, write never blocks
1915 * anyway, it either accepts the packet or drops it.
1918 tunpoll(struct cdev *dev, int events, struct thread *td)
1920 struct tuntap_softc *tp = dev->si_drv1;
1921 struct ifnet *ifp = TUN2IFP(tp);
1924 TUNDEBUG(ifp, "tunpoll\n");
1926 if (events & (POLLIN | POLLRDNORM)) {
1927 IFQ_LOCK(&ifp->if_snd);
1928 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1929 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1930 revents |= events & (POLLIN | POLLRDNORM);
1932 TUNDEBUG(ifp, "tunpoll waiting\n");
1933 selrecord(td, &tp->tun_rsel);
1935 IFQ_UNLOCK(&ifp->if_snd);
1937 revents |= events & (POLLOUT | POLLWRNORM);
1943 * tunkqfilter - support for the kevent() system call.
1946 tunkqfilter(struct cdev *dev, struct knote *kn)
1948 struct tuntap_softc *tp = dev->si_drv1;
1949 struct ifnet *ifp = TUN2IFP(tp);
1951 switch(kn->kn_filter) {
1953 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1954 ifp->if_xname, dev2unit(dev));
1955 kn->kn_fop = &tun_read_filterops;
1959 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1960 ifp->if_xname, dev2unit(dev));
1961 kn->kn_fop = &tun_write_filterops;
1965 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1966 ifp->if_xname, dev2unit(dev));
1971 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1977 * Return true of there is data in the interface queue.
1980 tunkqread(struct knote *kn, long hint)
1983 struct tuntap_softc *tp = kn->kn_hook;
1984 struct cdev *dev = tp->tun_dev;
1985 struct ifnet *ifp = TUN2IFP(tp);
1987 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1989 "%s have data in the queue. Len = %d, minor = %#x\n",
1990 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1994 "%s waiting for data, minor = %#x\n", ifp->if_xname,
2003 * Always can write, always return MTU in kn->data.
2006 tunkqwrite(struct knote *kn, long hint)
2008 struct tuntap_softc *tp = kn->kn_hook;
2009 struct ifnet *ifp = TUN2IFP(tp);
2011 kn->kn_data = ifp->if_mtu;
2017 tunkqdetach(struct knote *kn)
2019 struct tuntap_softc *tp = kn->kn_hook;
2021 knlist_remove(&tp->tun_rsel.si_note, kn, 0);