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_private.h>
85 #include <net/if_types.h>
86 #include <net/if_vlan_var.h>
87 #include <net/netisr.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
92 #include <netinet/ip.h>
95 #include <netinet/ip6.h>
96 #include <netinet6/ip6_var.h>
98 #include <netinet/udp.h>
99 #include <netinet/tcp.h>
100 #include <netinet/tcp_lro.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_UNUSED2 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 */
148 struct lro_ctrl tun_lro; /* for TCP LRO */
149 bool tun_lro_ready; /* TCP LRO initialized */
151 #define TUN2IFP(sc) ((sc)->tun_ifp)
153 #define TUNDEBUG if (tundebug) if_printf
155 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
156 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
157 #define TUN_LOCK_ASSERT(tp) mtx_assert(&(tp)->tun_mtx, MA_OWNED);
159 #define TUN_VMIO_FLAG_MASK 0x0fff
162 * Interface capabilities of a tap device that supports the virtio-net
165 #define TAP_VNET_HDR_CAPS (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 \
166 | IFCAP_VLAN_HWCSUM \
167 | IFCAP_TSO | IFCAP_LRO \
170 #define TAP_ALL_OFFLOAD (CSUM_TSO | CSUM_TCP | CSUM_UDP |\
171 CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
174 * All mutable global variables in if_tun are locked using tunmtx, with
175 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
176 * which are static after setup.
178 static struct mtx tunmtx;
179 static eventhandler_tag arrival_tag;
180 static eventhandler_tag clone_tag;
181 static const char tunname[] = "tun";
182 static const char tapname[] = "tap";
183 static const char vmnetname[] = "vmnet";
184 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
185 static int tundebug = 0;
186 static int tundclone = 1;
187 static int tap_allow_uopen = 0; /* allow user devfs cloning */
188 static int tapuponopen = 0; /* IFF_UP on open() */
189 static int tapdclone = 1; /* enable devfs cloning */
191 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
192 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
194 static struct sx tun_ioctl_sx;
195 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
197 SYSCTL_DECL(_net_link);
199 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
200 "IP tunnel software network interface");
201 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
202 "Enable legacy devfs interface creation");
205 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
206 "Ethernet tunnel software network interface");
207 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
208 "Enable legacy devfs interface creation for all users");
209 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
210 "Bring interface up when /dev/tap is opened");
211 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
212 "Enable legacy devfs interface creation");
213 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
215 static int tun_create_device(struct tuntap_driver *drv, int unit,
216 struct ucred *cr, struct cdev **dev, const char *name);
217 static int tun_busy_locked(struct tuntap_softc *tp);
218 static void tun_unbusy_locked(struct tuntap_softc *tp);
219 static int tun_busy(struct tuntap_softc *tp);
220 static void tun_unbusy(struct tuntap_softc *tp);
222 static int tuntap_name2info(const char *name, int *unit, int *flags);
223 static void tunclone(void *arg, struct ucred *cred, char *name,
224 int namelen, struct cdev **dev);
225 static void tuncreate(struct cdev *dev);
226 static void tundtor(void *data);
227 static void tunrename(void *arg, struct ifnet *ifp);
228 static int tunifioctl(struct ifnet *, u_long, caddr_t);
229 static void tuninit(struct ifnet *);
230 static void tunifinit(void *xtp);
231 static int tuntapmodevent(module_t, int, void *);
232 static int tunoutput(struct ifnet *, struct mbuf *,
233 const struct sockaddr *, struct route *ro);
234 static void tunstart(struct ifnet *);
235 static void tunstart_l2(struct ifnet *);
237 static int tun_clone_match(struct if_clone *ifc, const char *name);
238 static int tap_clone_match(struct if_clone *ifc, const char *name);
239 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
240 static int tun_clone_create(struct if_clone *, char *, size_t,
241 struct ifc_data *, struct ifnet **);
242 static int tun_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
243 static void tun_vnethdr_set(struct ifnet *ifp, int vhdrlen);
245 static d_open_t tunopen;
246 static d_read_t tunread;
247 static d_write_t tunwrite;
248 static d_ioctl_t tunioctl;
249 static d_poll_t tunpoll;
250 static d_kqfilter_t tunkqfilter;
252 static int tunkqread(struct knote *, long);
253 static int tunkqwrite(struct knote *, long);
254 static void tunkqdetach(struct knote *);
256 static struct filterops tun_read_filterops = {
259 .f_detach = tunkqdetach,
260 .f_event = tunkqread,
263 static struct filterops tun_write_filterops = {
266 .f_detach = tunkqdetach,
267 .f_event = tunkqwrite,
270 static struct tuntap_driver {
271 struct cdevsw cdevsw;
273 struct unrhdr *unrhdr;
274 struct clonedevs *clones;
275 ifc_match_f *clone_match_fn;
276 ifc_create_f *clone_create_fn;
277 ifc_destroy_f *clone_destroy_fn;
278 } tuntap_drivers[] = {
282 .d_version = D_VERSION,
283 .d_flags = D_NEEDMINOR,
289 .d_kqfilter = tunkqfilter,
292 .clone_match_fn = tun_clone_match,
293 .clone_create_fn = tun_clone_create,
294 .clone_destroy_fn = tun_clone_destroy,
297 .ident_flags = TUN_L2,
299 .d_version = D_VERSION,
300 .d_flags = D_NEEDMINOR,
306 .d_kqfilter = tunkqfilter,
309 .clone_match_fn = tap_clone_match,
310 .clone_create_fn = tun_clone_create,
311 .clone_destroy_fn = tun_clone_destroy,
314 .ident_flags = TUN_L2 | TUN_VMNET,
316 .d_version = D_VERSION,
317 .d_flags = D_NEEDMINOR,
323 .d_kqfilter = tunkqfilter,
326 .clone_match_fn = vmnet_clone_match,
327 .clone_create_fn = tun_clone_create,
328 .clone_destroy_fn = tun_clone_destroy,
331 #define NDRV nitems(tuntap_drivers)
333 VNET_DEFINE_STATIC(struct if_clone *, tuntap_driver_cloners[NDRV]);
334 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
337 * Mechanism for marking a tunnel device as busy so that we can safely do some
338 * orthogonal operations (such as operations on devices) without racing against
339 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
340 * open, to be woken up when the condition is alleviated.
343 tun_busy_locked(struct tuntap_softc *tp)
347 if ((tp->tun_flags & TUN_DYING) != 0) {
349 * Perhaps unintuitive, but the device is busy going away.
350 * Other interpretations of EBUSY from tun_busy make little
351 * sense, since making a busy device even more busy doesn't
352 * sound like a problem.
362 tun_unbusy_locked(struct tuntap_softc *tp)
366 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
369 /* Wake up anything that may be waiting on our busy tunnel. */
370 if (tp->tun_busy == 0)
371 cv_broadcast(&tp->tun_cv);
375 tun_busy(struct tuntap_softc *tp)
380 ret = tun_busy_locked(tp);
386 tun_unbusy(struct tuntap_softc *tp)
390 tun_unbusy_locked(tp);
395 * Sets unit and/or flags given the device name. Must be called with correct
399 tuntap_name2info(const char *name, int *outunit, int *outflags)
401 struct tuntap_driver *drv;
410 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
411 * wants to be able to pass back a char * through the second param. We
412 * will always set that as NULL here, so we'll fake it.
414 dname = __DECONST(char *, name);
417 for (u_int i = 0; i < NDRV; i++) {
418 drv = &tuntap_drivers[i];
420 if (strcmp(name, drv->cdevsw.d_name) == 0) {
423 flags = drv->ident_flags;
427 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
429 flags = drv->ident_flags;
439 if (outflags != NULL)
445 * Get driver information from a set of flags specified. Masks the identifying
446 * part of the flags and compares it against all of the available
449 static struct tuntap_driver *
450 tuntap_driver_from_flags(int tun_flags)
453 for (u_int i = 0; i < NDRV; i++)
454 if ((tun_flags & TUN_DRIVER_IDENT_MASK) ==
455 tuntap_drivers[i].ident_flags)
456 return (&tuntap_drivers[i]);
462 tun_clone_match(struct if_clone *ifc, const char *name)
466 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
467 if ((tunflags & TUN_L2) == 0)
475 tap_clone_match(struct if_clone *ifc, const char *name)
479 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
480 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
488 vmnet_clone_match(struct if_clone *ifc, const char *name)
492 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
493 if ((tunflags & TUN_VMNET) != 0)
501 tun_clone_create(struct if_clone *ifc, char *name, size_t len,
502 struct ifc_data *ifd, struct ifnet **ifpp)
504 struct tuntap_driver *drv;
506 int err, i, tunflags, unit;
509 /* The name here tells us exactly what we're creating */
510 err = tuntap_name2info(name, &unit, &tunflags);
514 drv = tuntap_driver_from_flags(tunflags);
519 /* If this unit number is still available that's okay. */
520 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
523 unit = alloc_unr(drv->unrhdr);
526 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
528 /* find any existing device, or allocate new unit number */
530 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
531 /* No preexisting struct cdev *, create one */
533 i = tun_create_device(drv, unit, NULL, &dev, name);
537 struct tuntap_softc *tp = dev->si_drv1;
545 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
548 char devname[SPECNAMELEN + 1];
549 struct tuntap_driver *drv;
550 int append_unit, i, u, tunflags;
557 CURVNET_SET(CRED_TO_VNET(cred));
558 if (tuntap_name2info(name, &u, &tunflags) != 0)
559 goto out; /* Not recognized */
561 if (u != -1 && u > IF_MAXUNIT)
562 goto out; /* Unit number too high */
564 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
565 if ((tunflags & TUN_L2) != 0) {
566 /* tap/vmnet allow user open with a sysctl */
567 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
569 mayclone = mayclone && tundclone;
573 * If tun cloning is enabled, only the superuser can create an
584 drv = tuntap_driver_from_flags(tunflags);
588 /* find any existing device, or allocate new unit number */
589 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
592 namelen = snprintf(devname, sizeof(devname), "%s%d",
597 i = tun_create_device(drv, u, cred, dev, name);
601 if_clone_create(name, namelen, NULL);
608 tun_destroy(struct tuntap_softc *tp)
612 tp->tun_flags |= TUN_DYING;
613 if (tp->tun_busy != 0)
614 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
618 CURVNET_SET(TUN2IFP(tp)->if_vnet);
620 /* destroy_dev will take care of any alias. */
621 destroy_dev(tp->tun_dev);
622 seldrain(&tp->tun_rsel);
623 knlist_clear(&tp->tun_rsel.si_note, 0);
624 knlist_destroy(&tp->tun_rsel.si_note);
625 if ((tp->tun_flags & TUN_L2) != 0) {
626 ether_ifdetach(TUN2IFP(tp));
628 bpfdetach(TUN2IFP(tp));
629 if_detach(TUN2IFP(tp));
631 sx_xlock(&tun_ioctl_sx);
632 TUN2IFP(tp)->if_softc = NULL;
633 sx_xunlock(&tun_ioctl_sx);
634 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
635 if_free(TUN2IFP(tp));
636 mtx_destroy(&tp->tun_mtx);
637 cv_destroy(&tp->tun_cv);
643 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp, uint32_t flags)
645 struct tuntap_softc *tp = ifp->if_softc;
648 TAILQ_REMOVE(&tunhead, tp, tun_list);
656 vnet_tun_init(const void *unused __unused)
659 for (u_int i = 0; i < NDRV; ++i) {
660 struct if_clone_addreq req = {
661 .match_f = tuntap_drivers[i].clone_match_fn,
662 .create_f = tuntap_drivers[i].clone_create_fn,
663 .destroy_f = tuntap_drivers[i].clone_destroy_fn,
665 V_tuntap_driver_cloners[i] =
666 ifc_attach_cloner(tuntap_drivers[i].cdevsw.d_name, &req);
669 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
670 vnet_tun_init, NULL);
673 vnet_tun_uninit(const void *unused __unused)
676 for (u_int i = 0; i < NDRV; ++i)
677 if_clone_detach(V_tuntap_driver_cloners[i]);
679 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
680 vnet_tun_uninit, NULL);
683 tun_uninit(const void *unused __unused)
685 struct tuntap_driver *drv;
686 struct tuntap_softc *tp;
689 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
690 EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
693 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
694 TAILQ_REMOVE(&tunhead, tp, tun_list);
700 for (i = 0; i < nitems(tuntap_drivers); ++i) {
701 drv = &tuntap_drivers[i];
702 delete_unrhdr(drv->unrhdr);
703 clone_cleanup(&drv->clones);
705 mtx_destroy(&tunmtx);
707 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
709 static struct tuntap_driver *
710 tuntap_driver_from_ifnet(const struct ifnet *ifp)
712 struct tuntap_driver *drv;
718 for (i = 0; i < nitems(tuntap_drivers); ++i) {
719 drv = &tuntap_drivers[i];
720 if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
728 tuntapmodevent(module_t mod, int type, void *data)
730 struct tuntap_driver *drv;
735 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
736 for (i = 0; i < nitems(tuntap_drivers); ++i) {
737 drv = &tuntap_drivers[i];
738 clone_setup(&drv->clones);
739 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
741 arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
743 if (arrival_tag == NULL)
745 clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
746 if (clone_tag == NULL)
750 /* See tun_uninit, so it's done after the vnet_sysuninit() */
758 static moduledata_t tuntap_mod = {
764 /* We'll only ever have these two, so no need for a macro. */
765 static moduledata_t tun_mod = { "if_tun", NULL, 0 };
766 static moduledata_t tap_mod = { "if_tap", NULL, 0 };
768 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
769 MODULE_VERSION(if_tuntap, 1);
770 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
771 MODULE_VERSION(if_tun, 1);
772 DECLARE_MODULE(if_tap, tap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
773 MODULE_VERSION(if_tap, 1);
776 tun_create_device(struct tuntap_driver *drv, int unit, struct ucred *cr,
777 struct cdev **dev, const char *name)
779 struct make_dev_args args;
780 struct tuntap_softc *tp;
783 tp = malloc(sizeof(*tp), M_TUN, M_WAITOK | M_ZERO);
784 mtx_init(&tp->tun_mtx, "tun_mtx", NULL, MTX_DEF);
785 cv_init(&tp->tun_cv, "tun_condvar");
786 tp->tun_flags = drv->ident_flags;
789 make_dev_args_init(&args);
791 args.mda_flags = MAKEDEV_REF | MAKEDEV_CHECKNAME;
792 args.mda_devsw = &drv->cdevsw;
794 args.mda_uid = UID_UUCP;
795 args.mda_gid = GID_DIALER;
796 args.mda_mode = 0600;
797 args.mda_unit = unit;
798 args.mda_si_drv1 = tp;
799 error = make_dev_s(&args, dev, "%s", name);
805 KASSERT((*dev)->si_drv1 != NULL,
806 ("Failed to set si_drv1 at %s creation", name));
808 knlist_init_mtx(&tp->tun_rsel.si_note, &tp->tun_mtx);
810 TAILQ_INSERT_TAIL(&tunhead, tp, tun_list);
816 tunstart(struct ifnet *ifp)
818 struct tuntap_softc *tp = ifp->if_softc;
821 TUNDEBUG(ifp, "starting\n");
822 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
823 IFQ_LOCK(&ifp->if_snd);
824 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
826 IFQ_UNLOCK(&ifp->if_snd);
829 IFQ_UNLOCK(&ifp->if_snd);
833 if (tp->tun_flags & TUN_RWAIT) {
834 tp->tun_flags &= ~TUN_RWAIT;
837 selwakeuppri(&tp->tun_rsel, PZERO + 1);
838 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
839 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
841 pgsigio(&tp->tun_sigio, SIGIO, 0);
849 * queue packets from higher level ready to put out
852 tunstart_l2(struct ifnet *ifp)
854 struct tuntap_softc *tp = ifp->if_softc;
856 TUNDEBUG(ifp, "starting\n");
859 * do not junk pending output if we are in VMnet mode.
860 * XXX: can this do any harm because of queue overflow?
864 if (((tp->tun_flags & TUN_VMNET) == 0) &&
865 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
869 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
872 IF_DEQUEUE(&ifp->if_snd, m);
875 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
884 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
886 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
887 if (tp->tun_flags & TUN_RWAIT) {
888 tp->tun_flags &= ~TUN_RWAIT;
892 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
894 pgsigio(&tp->tun_sigio, SIGIO, 0);
898 selwakeuppri(&tp->tun_rsel, PZERO+1);
899 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
900 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
903 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
908 tap_transmit(struct ifnet *ifp, struct mbuf *m)
913 IFQ_HANDOFF(ifp, m, error);
917 /* XXX: should return an error code so it can fail. */
919 tuncreate(struct cdev *dev)
921 struct tuntap_driver *drv;
922 struct tuntap_softc *tp;
924 struct ether_addr eaddr;
930 ("si_drv1 should have been initialized at creation"));
933 iflags = IFF_MULTICAST;
934 if ((tp->tun_flags & TUN_L2) != 0) {
936 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
939 iflags |= IFF_POINTOPOINT;
941 ifp = tp->tun_ifp = if_alloc(type);
943 panic("%s%d: failed to if_alloc() interface.\n",
944 drv->cdevsw.d_name, dev2unit(dev));
946 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
947 ifp->if_ioctl = tunifioctl;
948 ifp->if_flags = iflags;
949 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
950 ifp->if_capabilities |= IFCAP_LINKSTATE;
951 if ((tp->tun_flags & TUN_L2) != 0)
952 ifp->if_capabilities |=
953 IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO;
954 ifp->if_capenable |= IFCAP_LINKSTATE;
956 if ((tp->tun_flags & TUN_L2) != 0) {
957 ifp->if_init = tunifinit;
958 ifp->if_start = tunstart_l2;
959 ifp->if_transmit = tap_transmit;
960 ifp->if_qflush = if_qflush;
962 ether_gen_addr(ifp, &eaddr);
963 ether_ifattach(ifp, eaddr.octet);
965 ifp->if_mtu = TUNMTU;
966 ifp->if_start = tunstart;
967 ifp->if_output = tunoutput;
969 ifp->if_snd.ifq_drv_maxlen = 0;
970 IFQ_SET_READY(&ifp->if_snd);
973 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
977 tp->tun_flags |= TUN_INITED;
980 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
981 ifp->if_xname, dev2unit(dev));
985 tunrename(void *arg __unused, struct ifnet *ifp)
987 struct tuntap_softc *tp;
990 if ((ifp->if_flags & IFF_RENAMING) == 0)
993 if (tuntap_driver_from_ifnet(ifp) == NULL)
997 * We need to grab the ioctl sx long enough to make sure the softc is
998 * still there. If it is, we can safely try to busy the tun device.
999 * The busy may fail if the device is currently dying, in which case
1000 * we do nothing. If it doesn't fail, the busy count stops the device
1001 * from dying until we've created the alias (that will then be
1002 * subsequently destroyed).
1004 sx_xlock(&tun_ioctl_sx);
1007 sx_xunlock(&tun_ioctl_sx);
1010 error = tun_busy(tp);
1011 sx_xunlock(&tun_ioctl_sx);
1014 if (tp->tun_alias != NULL) {
1015 destroy_dev(tp->tun_alias);
1016 tp->tun_alias = NULL;
1019 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
1023 * Failure's ok, aliases are created on a best effort basis. If a
1024 * tun user/consumer decides to rename the interface to conflict with
1025 * another device (non-ifnet) on the system, we will assume they know
1026 * what they are doing. make_dev_alias_p won't touch tun_alias on
1027 * failure, so we use it but ignore the return value.
1029 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1036 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1039 struct tuntap_softc *tp;
1040 int error __diagused, tunflags;
1043 CURVNET_SET(TD_TO_VNET(td));
1044 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1047 return (error); /* Shouldn't happen */
1052 ("si_drv1 should have been initialized at creation"));
1055 if ((tp->tun_flags & TUN_INITED) == 0) {
1060 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1066 error = tun_busy_locked(tp);
1067 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1070 if ((tp->tun_flags & TUN_L2) != 0) {
1071 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1072 sizeof(tp->tun_ether.octet));
1074 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1075 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1078 ifp->if_flags |= IFF_UP;
1081 tp->tun_pid = td->td_proc->p_pid;
1082 tp->tun_flags |= TUN_OPEN;
1084 if_link_state_change(ifp, LINK_STATE_UP);
1085 TUNDEBUG(ifp, "open\n");
1089 * This can fail with either ENOENT or EBUSY. This is in the middle of
1090 * d_open, so ENOENT should not be possible. EBUSY is possible, but
1091 * the only cdevpriv dtor being set will be tundtor and the softc being
1092 * passed is constant for a given cdev. We ignore the possible error
1093 * because of this as either "unlikely" or "not actually a problem."
1095 (void)devfs_set_cdevpriv(tp, tundtor);
1101 * tundtor - tear down the device - mark i/f down & delete
1108 struct tuntap_softc *tp;
1119 * Realistically, we can't be obstinate here. This only means that the
1120 * tuntap device was closed out of order, and the last closer wasn't the
1121 * controller. These are still good to know about, though, as software
1122 * should avoid multiple processes with a tuntap device open and
1123 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1126 if (p->p_pid != tp->tun_pid) {
1128 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1129 p->p_pid, p->p_comm, tp->tun_dev->si_name);
1133 * junk all pending output
1135 CURVNET_SET(ifp->if_vnet);
1138 if ((tp->tun_flags & TUN_L2) != 0) {
1140 IF_DRAIN(&ifp->if_snd);
1142 IFQ_PURGE(&ifp->if_snd);
1145 /* For vmnet, we won't do most of the address/route bits */
1146 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1147 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1149 #if defined(INET) || defined(INET6)
1150 if (l2tun && tp->tun_lro_ready) {
1151 TUNDEBUG (ifp, "LRO disabled\n");
1152 tcp_lro_free(&tp->tun_lro);
1153 tp->tun_lro_ready = false;
1156 if (ifp->if_flags & IFF_UP) {
1162 /* Delete all addresses and routes which reference this interface. */
1163 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1164 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1171 if_link_state_change(ifp, LINK_STATE_DOWN);
1174 funsetown(&tp->tun_sigio);
1175 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1176 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1177 TUNDEBUG (ifp, "closed\n");
1178 tp->tun_flags &= ~TUN_OPEN;
1180 tun_vnethdr_set(ifp, 0);
1182 tun_unbusy_locked(tp);
1187 tuninit(struct ifnet *ifp)
1189 struct tuntap_softc *tp = ifp->if_softc;
1191 TUNDEBUG(ifp, "tuninit\n");
1194 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1195 if ((tp->tun_flags & TUN_L2) == 0) {
1196 ifp->if_flags |= IFF_UP;
1197 getmicrotime(&ifp->if_lastchange);
1200 #if defined(INET) || defined(INET6)
1201 if (tcp_lro_init(&tp->tun_lro) == 0) {
1202 TUNDEBUG(ifp, "LRO enabled\n");
1203 tp->tun_lro.ifp = ifp;
1204 tp->tun_lro_ready = true;
1206 TUNDEBUG(ifp, "Could not enable LRO\n");
1207 tp->tun_lro_ready = false;
1210 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1212 /* attempt to start output */
1219 * Used only for l2 tunnel.
1222 tunifinit(void *xtp)
1224 struct tuntap_softc *tp;
1226 tp = (struct tuntap_softc *)xtp;
1227 tuninit(tp->tun_ifp);
1231 * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1232 * current value of ifp->if_capenable.
1235 tun_caps_changed(struct ifnet *ifp)
1237 uint64_t hwassist = 0;
1239 TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1240 if (ifp->if_capenable & IFCAP_TXCSUM)
1241 hwassist |= CSUM_TCP | CSUM_UDP;
1242 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1243 hwassist |= CSUM_TCP_IPV6
1245 if (ifp->if_capenable & IFCAP_TSO4)
1246 hwassist |= CSUM_IP_TSO;
1247 if (ifp->if_capenable & IFCAP_TSO6)
1248 hwassist |= CSUM_IP6_TSO;
1249 ifp->if_hwassist = hwassist;
1253 * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1254 * if_capabilities and if_capenable as needed.
1257 tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1259 struct tuntap_softc *tp = ifp->if_softc;
1261 TUN_LOCK_ASSERT(tp);
1263 if (tp->tun_vhdrlen == vhdrlen)
1267 * Update if_capabilities to reflect the
1268 * functionalities offered by the virtio-net
1272 ifp->if_capabilities |=
1275 ifp->if_capabilities &=
1278 * Disable any capabilities that we don't
1281 ifp->if_capenable &= ifp->if_capabilities;
1282 tun_caps_changed(ifp);
1283 tp->tun_vhdrlen = vhdrlen;
1285 TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1286 vhdrlen, ifp->if_capabilities);
1290 * Process an ioctl request.
1293 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1295 struct ifreq *ifr = (struct ifreq *)data;
1296 struct tuntap_softc *tp;
1298 struct ifmediareq *ifmr;
1299 int dummy, error = 0;
1303 sx_xlock(&tun_ioctl_sx);
1309 l2tun = (tp->tun_flags & TUN_L2) != 0;
1312 ifs = (struct ifstat *)data;
1315 snprintf(ifs->ascii, sizeof(ifs->ascii),
1316 "\tOpened by PID %d\n", tp->tun_pid);
1318 ifs->ascii[0] = '\0';
1323 error = ether_ioctl(ifp, cmd, data);
1327 TUNDEBUG(ifp, "address set\n");
1330 ifp->if_mtu = ifr->ifr_mtu;
1331 TUNDEBUG(ifp, "mtu set\n");
1343 ifmr = (struct ifmediareq *)data;
1344 dummy = ifmr->ifm_count;
1345 ifmr->ifm_count = 1;
1346 ifmr->ifm_status = IFM_AVALID;
1347 ifmr->ifm_active = IFM_ETHER | IFM_FDX | IFM_1000_T;
1348 if (tp->tun_flags & TUN_OPEN)
1349 ifmr->ifm_status |= IFM_ACTIVE;
1350 ifmr->ifm_current = ifmr->ifm_active;
1352 int media = IFM_ETHER;
1353 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1358 ifp->if_capenable = ifr->ifr_reqcap;
1359 tun_caps_changed(ifp);
1361 VLAN_CAPABILITIES(ifp);
1365 error = ether_ioctl(ifp, cmd, data);
1371 sx_xunlock(&tun_ioctl_sx);
1376 * tunoutput - queue packets from higher level ready to put out.
1379 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1382 struct tuntap_softc *tp = ifp->if_softc;
1383 u_short cached_tun_flags;
1387 TUNDEBUG (ifp, "tunoutput\n");
1390 error = mac_ifnet_check_transmit(ifp, m0);
1397 /* Could be unlocked read? */
1399 cached_tun_flags = tp->tun_flags;
1401 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1402 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1407 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1412 /* BPF writes need to be handled specially. */
1413 if (dst->sa_family == AF_UNSPEC)
1414 bcopy(dst->sa_data, &af, sizeof(af));
1416 af = RO_GET_FAMILY(ro, dst);
1418 BPF_MTAP2(ifp, &af, sizeof(af), m0);
1420 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1421 if (cached_tun_flags & TUN_LMODE) {
1422 /* allocate space for sockaddr */
1423 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1425 /* if allocation failed drop packet */
1427 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1428 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1431 bcopy(dst, m0->m_data, dst->sa_len);
1435 if (cached_tun_flags & TUN_IFHEAD) {
1436 /* Prepend the address family */
1437 M_PREPEND(m0, 4, M_NOWAIT);
1439 /* if allocation failed drop packet */
1441 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1442 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1445 *(u_int32_t *)m0->m_data = htonl(af);
1452 return (EAFNOSUPPORT);
1456 error = (ifp->if_transmit)(ifp, m0);
1459 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1464 * the cdevsw interface is now pretty minimal.
1467 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1470 struct ifreq ifr, *ifrp;
1471 struct tuntap_softc *tp = dev->si_drv1;
1472 struct ifnet *ifp = TUN2IFP(tp);
1473 struct tuninfo *tunp;
1474 int error, iflags, ival;
1477 l2tun = (tp->tun_flags & TUN_L2) != 0;
1479 /* tap specific ioctls */
1481 /* VMware/VMnet port ioctl's */
1482 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1483 defined(COMPAT_FREEBSD4)
1485 ival = IOCPARM_IVAL(data);
1486 data = (caddr_t)&ival;
1489 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1490 iflags = *(int *)data;
1491 iflags &= TUN_VMIO_FLAG_MASK;
1492 iflags &= ~IFF_CANTCHANGE;
1496 ifp->if_flags = iflags |
1497 (ifp->if_flags & IFF_CANTCHANGE);
1501 case SIOCGIFADDR: /* get MAC address of the remote side */
1503 bcopy(&tp->tun_ether.octet, data,
1504 sizeof(tp->tun_ether.octet));
1508 case SIOCSIFADDR: /* set MAC address of the remote side */
1510 bcopy(data, &tp->tun_ether.octet,
1511 sizeof(tp->tun_ether.octet));
1516 ival = *(int *)data;
1518 ival != sizeof(struct virtio_net_hdr) &&
1519 ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1523 tun_vnethdr_set(ifp, ival);
1529 *(int *)data = tp->tun_vhdrlen;
1535 /* Fall through to the common ioctls if unhandled */
1541 tp->tun_flags |= TUN_LMODE;
1542 tp->tun_flags &= ~TUN_IFHEAD;
1544 tp->tun_flags &= ~TUN_LMODE;
1551 tp->tun_flags |= TUN_IFHEAD;
1552 tp->tun_flags &= ~TUN_LMODE;
1554 tp->tun_flags &= ~TUN_IFHEAD;
1560 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1565 /* deny this if UP */
1566 if (TUN2IFP(tp)->if_flags & IFF_UP)
1569 switch (*(int *)data & ~IFF_MULTICAST) {
1570 case IFF_POINTOPOINT:
1573 TUN2IFP(tp)->if_flags &=
1574 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1575 TUN2IFP(tp)->if_flags |= *(int *)data;
1586 tp->tun_pid = curthread->td_proc->p_pid;
1591 /* Fall through to the common ioctls if unhandled */
1596 ifrp = (struct ifreq *)data;
1597 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1601 tunp = (struct tuninfo *)data;
1602 if (TUN2IFP(tp)->if_type != tunp->type)
1603 return (EPROTOTYPE);
1605 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1606 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1607 ifr.ifr_mtu = tunp->mtu;
1608 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1609 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1617 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1621 tunp = (struct tuninfo *)data;
1623 tunp->mtu = TUN2IFP(tp)->if_mtu;
1624 tunp->type = TUN2IFP(tp)->if_type;
1625 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1629 tundebug = *(int *)data;
1632 *(int *)data = tundebug;
1639 tp->tun_flags |= TUN_ASYNC;
1641 tp->tun_flags &= ~TUN_ASYNC;
1645 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1647 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1648 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1649 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1650 *(int *)data += mb->m_len;
1651 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1656 return (fsetown(*(int *)data, &tp->tun_sigio));
1659 *(int *)data = fgetown(&tp->tun_sigio);
1662 /* This is deprecated, FIOSETOWN should be used instead. */
1664 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1666 /* This is deprecated, FIOGETOWN should be used instead. */
1668 *(int *)data = -fgetown(&tp->tun_sigio);
1678 * The cdevsw read interface - reads a packet at a time, or at
1679 * least as much of a packet as can be read.
1682 tunread(struct cdev *dev, struct uio *uio, int flag)
1684 struct tuntap_softc *tp = dev->si_drv1;
1685 struct ifnet *ifp = TUN2IFP(tp);
1690 TUNDEBUG (ifp, "read\n");
1692 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1694 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1698 tp->tun_flags &= ~TUN_RWAIT;
1701 IFQ_DEQUEUE(&ifp->if_snd, m);
1704 if (flag & O_NONBLOCK) {
1706 return (EWOULDBLOCK);
1708 tp->tun_flags |= TUN_RWAIT;
1709 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1718 len = min(tp->tun_vhdrlen, uio->uio_resid);
1720 struct virtio_net_hdr_mrg_rxbuf vhdr;
1722 bzero(&vhdr, sizeof(vhdr));
1723 if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1724 m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1727 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1728 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1729 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1730 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1731 vhdr.hdr.csum_offset);
1732 error = uiomove(&vhdr, len, uio);
1735 while (m && uio->uio_resid > 0 && error == 0) {
1736 len = min(uio->uio_resid, m->m_len);
1738 error = uiomove(mtod(m, void *), len, uio);
1743 TUNDEBUG(ifp, "Dropping mbuf\n");
1750 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1751 struct virtio_net_hdr_mrg_rxbuf *vhdr)
1753 struct epoch_tracker et;
1754 struct ether_header *eh;
1760 * Only pass a unicast frame to ether_input(), if it would
1761 * actually have been received by non-virtual hardware.
1763 if (m->m_len < sizeof(struct ether_header)) {
1768 eh = mtod(m, struct ether_header *);
1770 if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1771 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1772 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1778 if (virtio_net_rx_csum(m, &vhdr->hdr)) {
1783 switch (ntohs(eh->ether_type)) {
1786 if (ifp->if_capenable & IFCAP_RXCSUM) {
1787 m->m_pkthdr.csum_flags |=
1788 CSUM_IP_CHECKED | CSUM_IP_VALID |
1789 CSUM_DATA_VALID | CSUM_SCTP_VALID |
1791 m->m_pkthdr.csum_data = 0xffff;
1796 case ETHERTYPE_IPV6:
1797 if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
1798 m->m_pkthdr.csum_flags |=
1799 CSUM_DATA_VALID_IPV6 | CSUM_SCTP_VALID |
1801 m->m_pkthdr.csum_data = 0xffff;
1808 /* Pass packet up to parent. */
1809 CURVNET_SET(ifp->if_vnet);
1810 NET_EPOCH_ENTER(et);
1811 #if defined(INET) || defined(INET6)
1812 if (tp->tun_lro_ready && ifp->if_capenable & IFCAP_LRO &&
1813 tcp_lro_rx(&tp->tun_lro, m, 0) == 0)
1814 tcp_lro_flush_all(&tp->tun_lro);
1817 (*ifp->if_input)(ifp, m);
1820 /* ibytes are counted in parent */
1821 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1826 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1828 struct epoch_tracker et;
1833 /* Could be unlocked read? */
1835 if (tp->tun_flags & TUN_IFHEAD) {
1837 if (m->m_len < sizeof(family) &&
1838 (m = m_pullup(m, sizeof(family))) == NULL)
1840 family = ntohl(*mtod(m, u_int32_t *));
1841 m_adj(m, sizeof(family));
1847 BPF_MTAP2(ifp, &family, sizeof(family), m);
1862 return (EAFNOSUPPORT);
1864 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1865 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1866 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1867 CURVNET_SET(ifp->if_vnet);
1868 M_SETFIB(m, ifp->if_fib);
1869 NET_EPOCH_ENTER(et);
1870 netisr_dispatch(isr, m);
1877 * the cdevsw write interface - an atomic write is a packet - or else!
1880 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1882 struct virtio_net_hdr_mrg_rxbuf vhdr;
1883 struct tuntap_softc *tp;
1887 int align, vhdrlen, error;
1892 TUNDEBUG(ifp, "tunwrite\n");
1893 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1894 /* ignore silently */
1897 if (uio->uio_resid == 0)
1900 l2tun = (tp->tun_flags & TUN_L2) != 0;
1901 mru = l2tun ? TAPMRU : TUNMRU;
1902 vhdrlen = tp->tun_vhdrlen;
1905 align = ETHER_ALIGN;
1907 } else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1908 mru += sizeof(uint32_t); /* family */
1909 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1910 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1915 error = uiomove(&vhdr, vhdrlen, uio);
1918 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1919 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1920 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1921 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1922 vhdr.hdr.csum_offset);
1925 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1926 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1930 m->m_pkthdr.rcvif = ifp;
1932 mac_ifnet_create_mbuf(ifp, m);
1936 return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1938 return (tunwrite_l3(tp, m));
1942 * tunpoll - the poll interface, this is only useful on reads
1943 * really. The write detect always returns true, write never blocks
1944 * anyway, it either accepts the packet or drops it.
1947 tunpoll(struct cdev *dev, int events, struct thread *td)
1949 struct tuntap_softc *tp = dev->si_drv1;
1950 struct ifnet *ifp = TUN2IFP(tp);
1953 TUNDEBUG(ifp, "tunpoll\n");
1955 if (events & (POLLIN | POLLRDNORM)) {
1956 IFQ_LOCK(&ifp->if_snd);
1957 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1958 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1959 revents |= events & (POLLIN | POLLRDNORM);
1961 TUNDEBUG(ifp, "tunpoll waiting\n");
1962 selrecord(td, &tp->tun_rsel);
1964 IFQ_UNLOCK(&ifp->if_snd);
1966 revents |= events & (POLLOUT | POLLWRNORM);
1972 * tunkqfilter - support for the kevent() system call.
1975 tunkqfilter(struct cdev *dev, struct knote *kn)
1977 struct tuntap_softc *tp = dev->si_drv1;
1978 struct ifnet *ifp = TUN2IFP(tp);
1980 switch(kn->kn_filter) {
1982 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1983 ifp->if_xname, dev2unit(dev));
1984 kn->kn_fop = &tun_read_filterops;
1988 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1989 ifp->if_xname, dev2unit(dev));
1990 kn->kn_fop = &tun_write_filterops;
1994 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1995 ifp->if_xname, dev2unit(dev));
2000 knlist_add(&tp->tun_rsel.si_note, kn, 0);
2006 * Return true of there is data in the interface queue.
2009 tunkqread(struct knote *kn, long hint)
2012 struct tuntap_softc *tp = kn->kn_hook;
2013 struct cdev *dev = tp->tun_dev;
2014 struct ifnet *ifp = TUN2IFP(tp);
2016 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
2018 "%s have data in the queue. Len = %d, minor = %#x\n",
2019 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
2023 "%s waiting for data, minor = %#x\n", ifp->if_xname,
2032 * Always can write, always return MTU in kn->data.
2035 tunkqwrite(struct knote *kn, long hint)
2037 struct tuntap_softc *tp = kn->kn_hook;
2038 struct ifnet *ifp = TUN2IFP(tp);
2040 kn->kn_data = ifp->if_mtu;
2046 tunkqdetach(struct knote *kn)
2048 struct tuntap_softc *tp = kn->kn_hook;
2050 knlist_remove(&tp->tun_rsel.si_note, kn, 0);