1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
4 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
5 * Nottingham University 1987.
7 * This source may be freely distributed, however I would be interested
8 * in any changes that are made.
10 * This driver takes packets off the IP i/f and hands them up to a
11 * user process to have its wicked way with. This driver has it's
12 * roots in a similar driver written by Phil Cockcroft (formerly) at
13 * UCL. This driver is based much more on read/write/poll mode of
20 #include "opt_inet6.h"
22 #include <sys/param.h>
25 #include <sys/systm.h>
28 #include <sys/module.h>
29 #include <sys/socket.h>
30 #include <sys/fcntl.h>
31 #include <sys/filio.h>
32 #include <sys/sockio.h>
33 #include <sys/ttycom.h>
35 #include <sys/selinfo.h>
36 #include <sys/signalvar.h>
37 #include <sys/filedesc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/random.h>
46 #include <net/if_var.h>
47 #include <net/if_clone.h>
48 #include <net/if_types.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
53 #include <netinet/in.h>
56 #include <net/if_tun.h>
58 #include <sys/queue.h>
59 #include <sys/condvar.h>
61 #include <security/mac/mac_framework.h>
64 * tun_list is protected by global tunmtx. Other mutable fields are
65 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
66 * static for the duration of a tunnel interface.
69 TAILQ_ENTRY(tun_softc) tun_list;
71 u_short tun_flags; /* misc flags */
72 #define TUN_OPEN 0x0001
73 #define TUN_INITED 0x0002
74 #define TUN_RCOLL 0x0004
75 #define TUN_IASET 0x0008
76 #define TUN_DSTADDR 0x0010
77 #define TUN_LMODE 0x0020
78 #define TUN_RWAIT 0x0040
79 #define TUN_ASYNC 0x0080
80 #define TUN_IFHEAD 0x0100
82 #define TUN_READY (TUN_OPEN | TUN_INITED)
85 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this
86 * actually needed? Can we just return EBUSY if already open?
87 * Problem is that this involved inherent races when a tun device
88 * is handed off from one process to another, as opposed to just
89 * being slightly stale informationally.
91 pid_t tun_pid; /* owning pid */
92 struct ifnet *tun_ifp; /* the interface */
93 struct sigio *tun_sigio; /* information for async I/O */
94 struct selinfo tun_rsel; /* read select */
95 struct mtx tun_mtx; /* protect mutable softc fields */
96 struct cv tun_cv; /* protect against ref'd dev destroy */
98 #define TUN2IFP(sc) ((sc)->tun_ifp)
100 #define TUNDEBUG if (tundebug) if_printf
103 * All mutable global variables in if_tun are locked using tunmtx, with
104 * the exception of tundebug, which is used unlocked, and tunclones,
105 * which is static after setup.
107 static struct mtx tunmtx;
108 static const char tunname[] = "tun";
109 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
110 static int tundebug = 0;
111 static int tundclone = 1;
112 static struct clonedevs *tunclones;
113 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
114 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
116 SYSCTL_DECL(_net_link);
117 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
118 "IP tunnel software network interface.");
119 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
120 "Enable legacy devfs interface creation.");
122 static void tunclone(void *arg, struct ucred *cred, char *name,
123 int namelen, struct cdev **dev);
124 static void tuncreate(const char *name, struct cdev *dev);
125 static int tunifioctl(struct ifnet *, u_long, caddr_t);
126 static void tuninit(struct ifnet *);
127 static int tunmodevent(module_t, int, void *);
128 static int tunoutput(struct ifnet *, struct mbuf *,
129 const struct sockaddr *, struct route *ro);
130 static void tunstart(struct ifnet *);
132 static int tun_clone_create(struct if_clone *, int, caddr_t);
133 static void tun_clone_destroy(struct ifnet *);
134 static struct if_clone *tun_cloner;
136 static d_open_t tunopen;
137 static d_close_t tunclose;
138 static d_read_t tunread;
139 static d_write_t tunwrite;
140 static d_ioctl_t tunioctl;
141 static d_poll_t tunpoll;
142 static d_kqfilter_t tunkqfilter;
144 static int tunkqread(struct knote *, long);
145 static int tunkqwrite(struct knote *, long);
146 static void tunkqdetach(struct knote *);
148 static struct filterops tun_read_filterops = {
151 .f_detach = tunkqdetach,
152 .f_event = tunkqread,
155 static struct filterops tun_write_filterops = {
158 .f_detach = tunkqdetach,
159 .f_event = tunkqwrite,
162 static struct cdevsw tun_cdevsw = {
163 .d_version = D_VERSION,
164 .d_flags = D_NEEDMINOR,
171 .d_kqfilter = tunkqfilter,
176 tun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
181 /* find any existing device, or allocate new unit number */
182 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
184 /* No preexisting struct cdev *, create one */
185 dev = make_dev(&tun_cdevsw, unit,
186 UID_UUCP, GID_DIALER, 0600, "%s%d", tunname, unit);
188 tuncreate(tunname, dev);
194 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
197 char devname[SPECNAMELEN + 1];
198 int u, i, append_unit;
204 * If tun cloning is enabled, only the superuser can create an
207 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
210 if (strcmp(name, tunname) == 0) {
212 } else if (dev_stdclone(name, NULL, tunname, &u) != 1)
213 return; /* Don't recognise the name */
214 if (u != -1 && u > IF_MAXUNIT)
215 return; /* Unit number too high */
222 CURVNET_SET(CRED_TO_VNET(cred));
223 /* find any existing device, or allocate new unit number */
224 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
227 namelen = snprintf(devname, sizeof(devname), "%s%d",
231 /* No preexisting struct cdev *, create one */
232 *dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, u, cred,
233 UID_UUCP, GID_DIALER, 0600, "%s", name);
236 if_clone_create(name, namelen, NULL);
241 tun_destroy(struct tun_softc *tp)
245 mtx_lock(&tp->tun_mtx);
246 if ((tp->tun_flags & TUN_OPEN) != 0)
247 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
249 mtx_unlock(&tp->tun_mtx);
251 CURVNET_SET(TUN2IFP(tp)->if_vnet);
253 bpfdetach(TUN2IFP(tp));
254 if_detach(TUN2IFP(tp));
255 if_free(TUN2IFP(tp));
257 seldrain(&tp->tun_rsel);
258 knlist_clear(&tp->tun_rsel.si_note, 0);
259 knlist_destroy(&tp->tun_rsel.si_note);
260 mtx_destroy(&tp->tun_mtx);
261 cv_destroy(&tp->tun_cv);
267 tun_clone_destroy(struct ifnet *ifp)
269 struct tun_softc *tp = ifp->if_softc;
272 TAILQ_REMOVE(&tunhead, tp, tun_list);
278 tunmodevent(module_t mod, int type, void *data)
280 static eventhandler_tag tag;
281 struct tun_softc *tp;
285 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
286 clone_setup(&tunclones);
287 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
290 tun_cloner = if_clone_simple(tunname, tun_clone_create,
291 tun_clone_destroy, 0);
294 if_clone_detach(tun_cloner);
295 EVENTHANDLER_DEREGISTER(dev_clone, tag);
296 drain_dev_clone_events();
299 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
300 TAILQ_REMOVE(&tunhead, tp, tun_list);
306 clone_cleanup(&tunclones);
307 mtx_destroy(&tunmtx);
315 static moduledata_t tun_mod = {
321 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
322 MODULE_VERSION(if_tun, 1);
325 tunstart(struct ifnet *ifp)
327 struct tun_softc *tp = ifp->if_softc;
330 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
331 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
332 IFQ_LOCK(&ifp->if_snd);
333 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
335 IFQ_UNLOCK(&ifp->if_snd);
338 IFQ_UNLOCK(&ifp->if_snd);
341 mtx_lock(&tp->tun_mtx);
342 if (tp->tun_flags & TUN_RWAIT) {
343 tp->tun_flags &= ~TUN_RWAIT;
346 selwakeuppri(&tp->tun_rsel, PZERO + 1);
347 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
348 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
349 mtx_unlock(&tp->tun_mtx);
350 pgsigio(&tp->tun_sigio, SIGIO, 0);
352 mtx_unlock(&tp->tun_mtx);
355 /* XXX: should return an error code so it can fail. */
357 tuncreate(const char *name, struct cdev *dev)
359 struct tun_softc *sc;
362 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
363 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
364 cv_init(&sc->tun_cv, "tun_condvar");
365 sc->tun_flags = TUN_INITED;
368 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
371 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
373 panic("%s%d: failed to if_alloc() interface.\n",
374 name, dev2unit(dev));
375 if_initname(ifp, name, dev2unit(dev));
376 ifp->if_mtu = TUNMTU;
377 ifp->if_ioctl = tunifioctl;
378 ifp->if_output = tunoutput;
379 ifp->if_start = tunstart;
380 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
382 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
383 ifp->if_snd.ifq_drv_maxlen = 0;
384 IFQ_SET_READY(&ifp->if_snd);
385 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
386 ifp->if_capabilities |= IFCAP_LINKSTATE;
387 ifp->if_capenable |= IFCAP_LINKSTATE;
390 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
392 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
393 ifp->if_xname, dev2unit(dev));
397 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
400 struct tun_softc *tp;
403 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
408 tuncreate(tunname, dev);
413 * XXXRW: This use of tun_pid is subject to error due to the
414 * fact that a reference to the tunnel can live beyond the
415 * death of the process that created it. Can we replace this
416 * with a simple busy flag?
418 mtx_lock(&tp->tun_mtx);
419 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
420 mtx_unlock(&tp->tun_mtx);
423 tp->tun_pid = td->td_proc->p_pid;
425 tp->tun_flags |= TUN_OPEN;
427 if_link_state_change(ifp, LINK_STATE_UP);
428 TUNDEBUG(ifp, "open\n");
429 mtx_unlock(&tp->tun_mtx);
435 * tunclose - close the device - mark i/f down & delete
439 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
441 struct tun_softc *tp;
447 mtx_lock(&tp->tun_mtx);
448 tp->tun_flags &= ~TUN_OPEN;
452 * junk all pending output
454 CURVNET_SET(ifp->if_vnet);
455 IFQ_PURGE(&ifp->if_snd);
457 if (ifp->if_flags & IFF_UP) {
458 mtx_unlock(&tp->tun_mtx);
460 mtx_lock(&tp->tun_mtx);
463 /* Delete all addresses and routes which reference this interface. */
464 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
467 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
468 mtx_unlock(&tp->tun_mtx);
469 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
470 /* deal w/IPv4 PtP destination; unlocked read */
471 if (ifa->ifa_addr->sa_family == AF_INET) {
472 rtinit(ifa, (int)RTM_DELETE,
473 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
475 rtinit(ifa, (int)RTM_DELETE, 0);
479 mtx_lock(&tp->tun_mtx);
481 if_link_state_change(ifp, LINK_STATE_DOWN);
484 funsetown(&tp->tun_sigio);
485 selwakeuppri(&tp->tun_rsel, PZERO + 1);
486 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
487 TUNDEBUG (ifp, "closed\n");
489 cv_broadcast(&tp->tun_cv);
490 mtx_unlock(&tp->tun_mtx);
495 tuninit(struct ifnet *ifp)
497 struct tun_softc *tp = ifp->if_softc;
502 TUNDEBUG(ifp, "tuninit\n");
504 mtx_lock(&tp->tun_mtx);
505 ifp->if_flags |= IFF_UP;
506 ifp->if_drv_flags |= IFF_DRV_RUNNING;
507 getmicrotime(&ifp->if_lastchange);
511 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
512 if (ifa->ifa_addr->sa_family == AF_INET) {
513 struct sockaddr_in *si;
515 si = (struct sockaddr_in *)ifa->ifa_addr;
516 if (si->sin_addr.s_addr)
517 tp->tun_flags |= TUN_IASET;
519 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
520 if (si && si->sin_addr.s_addr)
521 tp->tun_flags |= TUN_DSTADDR;
524 if_addr_runlock(ifp);
526 mtx_unlock(&tp->tun_mtx);
530 * Process an ioctl request.
533 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
535 struct ifreq *ifr = (struct ifreq *)data;
536 struct tun_softc *tp = ifp->if_softc;
542 ifs = (struct ifstat *)data;
543 mtx_lock(&tp->tun_mtx);
545 snprintf(ifs->ascii, sizeof(ifs->ascii),
546 "\tOpened by PID %d\n", tp->tun_pid);
548 ifs->ascii[0] = '\0';
549 mtx_unlock(&tp->tun_mtx);
553 TUNDEBUG(ifp, "address set\n");
556 ifp->if_mtu = ifr->ifr_mtu;
557 TUNDEBUG(ifp, "mtu set\n");
570 * tunoutput - queue packets from higher level ready to put out.
573 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
576 struct tun_softc *tp = ifp->if_softc;
577 u_short cached_tun_flags;
581 TUNDEBUG (ifp, "tunoutput\n");
584 error = mac_ifnet_check_transmit(ifp, m0);
591 /* Could be unlocked read? */
592 mtx_lock(&tp->tun_mtx);
593 cached_tun_flags = tp->tun_flags;
594 mtx_unlock(&tp->tun_mtx);
595 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
596 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
601 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
606 /* BPF writes need to be handled specially. */
607 if (dst->sa_family == AF_UNSPEC)
608 bcopy(dst->sa_data, &af, sizeof(af));
612 if (bpf_peers_present(ifp->if_bpf))
613 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
615 /* prepend sockaddr? this may abort if the mbuf allocation fails */
616 if (cached_tun_flags & TUN_LMODE) {
617 /* allocate space for sockaddr */
618 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
620 /* if allocation failed drop packet */
622 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
623 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
626 bcopy(dst, m0->m_data, dst->sa_len);
630 if (cached_tun_flags & TUN_IFHEAD) {
631 /* Prepend the address family */
632 M_PREPEND(m0, 4, M_NOWAIT);
634 /* if allocation failed drop packet */
636 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
637 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
640 *(u_int32_t *)m0->m_data = htonl(af);
647 return (EAFNOSUPPORT);
651 error = (ifp->if_transmit)(ifp, m0);
654 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
659 * the cdevsw interface is now pretty minimal.
662 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
666 struct tun_softc *tp = dev->si_drv1;
667 struct tuninfo *tunp;
671 tunp = (struct tuninfo *)data;
672 if (tunp->mtu < IF_MINMTU)
674 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
675 error = priv_check(td, PRIV_NET_SETIFMTU);
679 mtx_lock(&tp->tun_mtx);
680 TUN2IFP(tp)->if_mtu = tunp->mtu;
681 TUN2IFP(tp)->if_type = tunp->type;
682 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
683 mtx_unlock(&tp->tun_mtx);
686 tunp = (struct tuninfo *)data;
687 mtx_lock(&tp->tun_mtx);
688 tunp->mtu = TUN2IFP(tp)->if_mtu;
689 tunp->type = TUN2IFP(tp)->if_type;
690 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
691 mtx_unlock(&tp->tun_mtx);
694 tundebug = *(int *)data;
697 *(int *)data = tundebug;
700 mtx_lock(&tp->tun_mtx);
702 tp->tun_flags |= TUN_LMODE;
703 tp->tun_flags &= ~TUN_IFHEAD;
705 tp->tun_flags &= ~TUN_LMODE;
706 mtx_unlock(&tp->tun_mtx);
709 mtx_lock(&tp->tun_mtx);
711 tp->tun_flags |= TUN_IFHEAD;
712 tp->tun_flags &= ~TUN_LMODE;
714 tp->tun_flags &= ~TUN_IFHEAD;
715 mtx_unlock(&tp->tun_mtx);
718 mtx_lock(&tp->tun_mtx);
719 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
720 mtx_unlock(&tp->tun_mtx);
723 /* deny this if UP */
724 if (TUN2IFP(tp)->if_flags & IFF_UP)
727 switch (*(int *)data & ~IFF_MULTICAST) {
728 case IFF_POINTOPOINT:
730 mtx_lock(&tp->tun_mtx);
731 TUN2IFP(tp)->if_flags &=
732 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
733 TUN2IFP(tp)->if_flags |= *(int *)data;
734 mtx_unlock(&tp->tun_mtx);
741 mtx_lock(&tp->tun_mtx);
742 tp->tun_pid = curthread->td_proc->p_pid;
743 mtx_unlock(&tp->tun_mtx);
748 mtx_lock(&tp->tun_mtx);
750 tp->tun_flags |= TUN_ASYNC;
752 tp->tun_flags &= ~TUN_ASYNC;
753 mtx_unlock(&tp->tun_mtx);
756 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
758 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
759 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
760 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
761 *(int *)data += mb->m_len;
762 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
767 return (fsetown(*(int *)data, &tp->tun_sigio));
770 *(int *)data = fgetown(&tp->tun_sigio);
773 /* This is deprecated, FIOSETOWN should be used instead. */
775 return (fsetown(-(*(int *)data), &tp->tun_sigio));
777 /* This is deprecated, FIOGETOWN should be used instead. */
779 *(int *)data = -fgetown(&tp->tun_sigio);
789 * The cdevsw read interface - reads a packet at a time, or at
790 * least as much of a packet as can be read.
793 tunread(struct cdev *dev, struct uio *uio, int flag)
795 struct tun_softc *tp = dev->si_drv1;
796 struct ifnet *ifp = TUN2IFP(tp);
800 TUNDEBUG (ifp, "read\n");
801 mtx_lock(&tp->tun_mtx);
802 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
803 mtx_unlock(&tp->tun_mtx);
804 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
808 tp->tun_flags &= ~TUN_RWAIT;
811 IFQ_DEQUEUE(&ifp->if_snd, m);
813 if (flag & O_NONBLOCK) {
814 mtx_unlock(&tp->tun_mtx);
815 return (EWOULDBLOCK);
817 tp->tun_flags |= TUN_RWAIT;
818 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
821 mtx_unlock(&tp->tun_mtx);
826 mtx_unlock(&tp->tun_mtx);
828 while (m && uio->uio_resid > 0 && error == 0) {
829 len = min(uio->uio_resid, m->m_len);
831 error = uiomove(mtod(m, void *), len, uio);
836 TUNDEBUG(ifp, "Dropping mbuf\n");
843 * the cdevsw write interface - an atomic write is a packet - or else!
846 tunwrite(struct cdev *dev, struct uio *uio, int flag)
848 struct tun_softc *tp = dev->si_drv1;
849 struct ifnet *ifp = TUN2IFP(tp);
851 uint32_t family, mru;
854 TUNDEBUG(ifp, "tunwrite\n");
856 if ((ifp->if_flags & IFF_UP) != IFF_UP)
857 /* ignore silently */
860 if (uio->uio_resid == 0)
864 if (tp->tun_flags & TUN_IFHEAD)
865 mru += sizeof(family);
866 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
867 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
871 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL) {
872 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
876 m->m_pkthdr.rcvif = ifp;
878 mac_ifnet_create_mbuf(ifp, m);
881 /* Could be unlocked read? */
882 mtx_lock(&tp->tun_mtx);
883 if (tp->tun_flags & TUN_IFHEAD) {
884 mtx_unlock(&tp->tun_mtx);
885 if (m->m_len < sizeof(family) &&
886 (m = m_pullup(m, sizeof(family))) == NULL)
888 family = ntohl(*mtod(m, u_int32_t *));
889 m_adj(m, sizeof(family));
891 mtx_unlock(&tp->tun_mtx);
895 BPF_MTAP2(ifp, &family, sizeof(family), m);
910 return (EAFNOSUPPORT);
912 random_harvest_queue(m, sizeof(*m), 2, RANDOM_NET_TUN);
913 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
914 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
915 CURVNET_SET(ifp->if_vnet);
916 M_SETFIB(m, ifp->if_fib);
917 netisr_dispatch(isr, m);
923 * tunpoll - the poll interface, this is only useful on reads
924 * really. The write detect always returns true, write never blocks
925 * anyway, it either accepts the packet or drops it.
928 tunpoll(struct cdev *dev, int events, struct thread *td)
930 struct tun_softc *tp = dev->si_drv1;
931 struct ifnet *ifp = TUN2IFP(tp);
935 TUNDEBUG(ifp, "tunpoll\n");
937 if (events & (POLLIN | POLLRDNORM)) {
938 IFQ_LOCK(&ifp->if_snd);
939 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
941 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
942 revents |= events & (POLLIN | POLLRDNORM);
944 TUNDEBUG(ifp, "tunpoll waiting\n");
945 selrecord(td, &tp->tun_rsel);
947 IFQ_UNLOCK(&ifp->if_snd);
949 if (events & (POLLOUT | POLLWRNORM))
950 revents |= events & (POLLOUT | POLLWRNORM);
956 * tunkqfilter - support for the kevent() system call.
959 tunkqfilter(struct cdev *dev, struct knote *kn)
961 struct tun_softc *tp = dev->si_drv1;
962 struct ifnet *ifp = TUN2IFP(tp);
964 switch(kn->kn_filter) {
966 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
967 ifp->if_xname, dev2unit(dev));
968 kn->kn_fop = &tun_read_filterops;
972 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
973 ifp->if_xname, dev2unit(dev));
974 kn->kn_fop = &tun_write_filterops;
978 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
979 ifp->if_xname, dev2unit(dev));
984 knlist_add(&tp->tun_rsel.si_note, kn, 0);
990 * Return true of there is data in the interface queue.
993 tunkqread(struct knote *kn, long hint)
996 struct tun_softc *tp = kn->kn_hook;
997 struct cdev *dev = tp->tun_dev;
998 struct ifnet *ifp = TUN2IFP(tp);
1000 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1002 "%s have data in the queue. Len = %d, minor = %#x\n",
1003 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1007 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1016 * Always can write, always return MTU in kn->data.
1019 tunkqwrite(struct knote *kn, long hint)
1021 struct tun_softc *tp = kn->kn_hook;
1022 struct ifnet *ifp = TUN2IFP(tp);
1024 kn->kn_data = ifp->if_mtu;
1030 tunkqdetach(struct knote *kn)
1032 struct tun_softc *tp = kn->kn_hook;
1034 knlist_remove(&tp->tun_rsel.si_note, kn, 0);