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
19 #include "opt_atalk.h"
21 #include "opt_inet6.h"
24 #include <sys/param.h>
27 #include <sys/systm.h>
30 #include <sys/module.h>
31 #include <sys/socket.h>
32 #include <sys/fcntl.h>
33 #include <sys/filio.h>
34 #include <sys/sockio.h>
35 #include <sys/ttycom.h>
37 #include <sys/selinfo.h>
38 #include <sys/signalvar.h>
39 #include <sys/filedesc.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
45 #include <sys/random.h>
48 #include <net/if_var.h>
49 #include <net/if_clone.h>
50 #include <net/if_types.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
55 #include <netinet/in.h>
58 #include <net/if_tun.h>
60 #include <sys/queue.h>
61 #include <sys/condvar.h>
63 #include <security/mac/mac_framework.h>
66 * tun_list is protected by global tunmtx. Other mutable fields are
67 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
68 * static for the duration of a tunnel interface.
71 TAILQ_ENTRY(tun_softc) tun_list;
73 u_short tun_flags; /* misc flags */
74 #define TUN_OPEN 0x0001
75 #define TUN_INITED 0x0002
76 #define TUN_RCOLL 0x0004
77 #define TUN_IASET 0x0008
78 #define TUN_DSTADDR 0x0010
79 #define TUN_LMODE 0x0020
80 #define TUN_RWAIT 0x0040
81 #define TUN_ASYNC 0x0080
82 #define TUN_IFHEAD 0x0100
84 #define TUN_READY (TUN_OPEN | TUN_INITED)
87 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this
88 * actually needed? Can we just return EBUSY if already open?
89 * Problem is that this involved inherent races when a tun device
90 * is handed off from one process to another, as opposed to just
91 * being slightly stale informationally.
93 pid_t tun_pid; /* owning pid */
94 struct ifnet *tun_ifp; /* the interface */
95 struct sigio *tun_sigio; /* information for async I/O */
96 struct selinfo tun_rsel; /* read select */
97 struct mtx tun_mtx; /* protect mutable softc fields */
98 struct cv tun_cv; /* protect against ref'd dev destroy */
100 #define TUN2IFP(sc) ((sc)->tun_ifp)
102 #define TUNDEBUG if (tundebug) if_printf
105 * All mutable global variables in if_tun are locked using tunmtx, with
106 * the exception of tundebug, which is used unlocked, and tunclones,
107 * which is static after setup.
109 static struct mtx tunmtx;
110 static const char tunname[] = "tun";
111 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
112 static int tundebug = 0;
113 static int tundclone = 1;
114 static struct clonedevs *tunclones;
115 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
116 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
118 SYSCTL_DECL(_net_link);
119 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
120 "IP tunnel software network interface.");
121 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0,
122 "Enable legacy devfs interface creation.");
124 TUNABLE_INT("net.link.tun.devfs_cloning", &tundclone);
126 static void tunclone(void *arg, struct ucred *cred, char *name,
127 int namelen, struct cdev **dev);
128 static void tuncreate(const char *name, struct cdev *dev);
129 static int tunifioctl(struct ifnet *, u_long, caddr_t);
130 static void tuninit(struct ifnet *);
131 static int tunmodevent(module_t, int, void *);
132 static int tunoutput(struct ifnet *, struct mbuf *,
133 const struct sockaddr *, struct route *ro);
134 static void tunstart(struct ifnet *);
136 static int tun_clone_create(struct if_clone *, int, caddr_t);
137 static void tun_clone_destroy(struct ifnet *);
138 static struct if_clone *tun_cloner;
140 static d_open_t tunopen;
141 static d_close_t tunclose;
142 static d_read_t tunread;
143 static d_write_t tunwrite;
144 static d_ioctl_t tunioctl;
145 static d_poll_t tunpoll;
146 static d_kqfilter_t tunkqfilter;
148 static int tunkqread(struct knote *, long);
149 static int tunkqwrite(struct knote *, long);
150 static void tunkqdetach(struct knote *);
152 static struct filterops tun_read_filterops = {
155 .f_detach = tunkqdetach,
156 .f_event = tunkqread,
159 static struct filterops tun_write_filterops = {
162 .f_detach = tunkqdetach,
163 .f_event = tunkqwrite,
166 static struct cdevsw tun_cdevsw = {
167 .d_version = D_VERSION,
168 .d_flags = D_NEEDMINOR,
175 .d_kqfilter = tunkqfilter,
180 tun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
185 /* find any existing device, or allocate new unit number */
186 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
188 /* No preexisting struct cdev *, create one */
189 dev = make_dev(&tun_cdevsw, unit,
190 UID_UUCP, GID_DIALER, 0600, "%s%d", tunname, unit);
192 tuncreate(tunname, dev);
198 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
201 char devname[SPECNAMELEN + 1];
202 int u, i, append_unit;
208 * If tun cloning is enabled, only the superuser can create an
211 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
214 if (strcmp(name, tunname) == 0) {
216 } else if (dev_stdclone(name, NULL, tunname, &u) != 1)
217 return; /* Don't recognise the name */
218 if (u != -1 && u > IF_MAXUNIT)
219 return; /* Unit number too high */
226 CURVNET_SET(CRED_TO_VNET(cred));
227 /* find any existing device, or allocate new unit number */
228 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
231 namelen = snprintf(devname, sizeof(devname), "%s%d",
235 /* No preexisting struct cdev *, create one */
236 *dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, u, cred,
237 UID_UUCP, GID_DIALER, 0600, "%s", name);
240 if_clone_create(name, namelen, NULL);
245 tun_destroy(struct tun_softc *tp)
249 mtx_lock(&tp->tun_mtx);
250 if ((tp->tun_flags & TUN_OPEN) != 0)
251 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
253 mtx_unlock(&tp->tun_mtx);
255 CURVNET_SET(TUN2IFP(tp)->if_vnet);
257 bpfdetach(TUN2IFP(tp));
258 if_detach(TUN2IFP(tp));
259 if_free(TUN2IFP(tp));
261 seldrain(&tp->tun_rsel);
262 knlist_clear(&tp->tun_rsel.si_note, 0);
263 knlist_destroy(&tp->tun_rsel.si_note);
264 mtx_destroy(&tp->tun_mtx);
265 cv_destroy(&tp->tun_cv);
271 tun_clone_destroy(struct ifnet *ifp)
273 struct tun_softc *tp = ifp->if_softc;
276 TAILQ_REMOVE(&tunhead, tp, tun_list);
282 tunmodevent(module_t mod, int type, void *data)
284 static eventhandler_tag tag;
285 struct tun_softc *tp;
289 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
290 clone_setup(&tunclones);
291 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
294 tun_cloner = if_clone_simple(tunname, tun_clone_create,
295 tun_clone_destroy, 0);
298 if_clone_detach(tun_cloner);
299 EVENTHANDLER_DEREGISTER(dev_clone, tag);
300 drain_dev_clone_events();
303 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
304 TAILQ_REMOVE(&tunhead, tp, tun_list);
310 clone_cleanup(&tunclones);
311 mtx_destroy(&tunmtx);
319 static moduledata_t tun_mod = {
325 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
326 MODULE_VERSION(if_tun, 1);
329 tunstart(struct ifnet *ifp)
331 struct tun_softc *tp = ifp->if_softc;
334 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
335 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
336 IFQ_LOCK(&ifp->if_snd);
337 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
339 IFQ_UNLOCK(&ifp->if_snd);
342 IFQ_UNLOCK(&ifp->if_snd);
345 mtx_lock(&tp->tun_mtx);
346 if (tp->tun_flags & TUN_RWAIT) {
347 tp->tun_flags &= ~TUN_RWAIT;
350 selwakeuppri(&tp->tun_rsel, PZERO + 1);
351 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
352 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
353 mtx_unlock(&tp->tun_mtx);
354 pgsigio(&tp->tun_sigio, SIGIO, 0);
356 mtx_unlock(&tp->tun_mtx);
359 /* XXX: should return an error code so it can fail. */
361 tuncreate(const char *name, struct cdev *dev)
363 struct tun_softc *sc;
366 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
367 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
368 cv_init(&sc->tun_cv, "tun_condvar");
369 sc->tun_flags = TUN_INITED;
372 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
375 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
377 panic("%s%d: failed to if_alloc() interface.\n",
378 name, dev2unit(dev));
379 if_initname(ifp, name, dev2unit(dev));
380 ifp->if_mtu = TUNMTU;
381 ifp->if_ioctl = tunifioctl;
382 ifp->if_output = tunoutput;
383 ifp->if_start = tunstart;
384 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
386 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
387 ifp->if_snd.ifq_drv_maxlen = 0;
388 IFQ_SET_READY(&ifp->if_snd);
389 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
390 ifp->if_capabilities |= IFCAP_LINKSTATE;
391 ifp->if_capenable |= IFCAP_LINKSTATE;
394 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
396 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
397 ifp->if_xname, dev2unit(dev));
401 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
404 struct tun_softc *tp;
407 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
412 tuncreate(tunname, dev);
417 * XXXRW: This use of tun_pid is subject to error due to the
418 * fact that a reference to the tunnel can live beyond the
419 * death of the process that created it. Can we replace this
420 * with a simple busy flag?
422 mtx_lock(&tp->tun_mtx);
423 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
424 mtx_unlock(&tp->tun_mtx);
427 tp->tun_pid = td->td_proc->p_pid;
429 tp->tun_flags |= TUN_OPEN;
431 if_link_state_change(ifp, LINK_STATE_UP);
432 TUNDEBUG(ifp, "open\n");
433 mtx_unlock(&tp->tun_mtx);
439 * tunclose - close the device - mark i/f down & delete
443 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
445 struct tun_softc *tp;
451 mtx_lock(&tp->tun_mtx);
452 tp->tun_flags &= ~TUN_OPEN;
456 * junk all pending output
458 CURVNET_SET(ifp->if_vnet);
459 IFQ_PURGE(&ifp->if_snd);
461 if (ifp->if_flags & IFF_UP) {
462 mtx_unlock(&tp->tun_mtx);
464 mtx_lock(&tp->tun_mtx);
467 /* Delete all addresses and routes which reference this interface. */
468 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
471 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
472 mtx_unlock(&tp->tun_mtx);
473 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
474 /* deal w/IPv4 PtP destination; unlocked read */
475 if (ifa->ifa_addr->sa_family == AF_INET) {
476 rtinit(ifa, (int)RTM_DELETE,
477 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
479 rtinit(ifa, (int)RTM_DELETE, 0);
483 mtx_lock(&tp->tun_mtx);
485 if_link_state_change(ifp, LINK_STATE_DOWN);
488 funsetown(&tp->tun_sigio);
489 selwakeuppri(&tp->tun_rsel, PZERO + 1);
490 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
491 TUNDEBUG (ifp, "closed\n");
493 cv_broadcast(&tp->tun_cv);
494 mtx_unlock(&tp->tun_mtx);
499 tuninit(struct ifnet *ifp)
501 struct tun_softc *tp = ifp->if_softc;
506 TUNDEBUG(ifp, "tuninit\n");
508 mtx_lock(&tp->tun_mtx);
509 ifp->if_flags |= IFF_UP;
510 ifp->if_drv_flags |= IFF_DRV_RUNNING;
511 getmicrotime(&ifp->if_lastchange);
515 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
516 if (ifa->ifa_addr->sa_family == AF_INET) {
517 struct sockaddr_in *si;
519 si = (struct sockaddr_in *)ifa->ifa_addr;
520 if (si->sin_addr.s_addr)
521 tp->tun_flags |= TUN_IASET;
523 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
524 if (si && si->sin_addr.s_addr)
525 tp->tun_flags |= TUN_DSTADDR;
528 if_addr_runlock(ifp);
530 mtx_unlock(&tp->tun_mtx);
534 * Process an ioctl request.
537 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
539 struct ifreq *ifr = (struct ifreq *)data;
540 struct tun_softc *tp = ifp->if_softc;
546 ifs = (struct ifstat *)data;
547 mtx_lock(&tp->tun_mtx);
549 snprintf(ifs->ascii, sizeof(ifs->ascii),
550 "\tOpened by PID %d\n", tp->tun_pid);
552 ifs->ascii[0] = '\0';
553 mtx_unlock(&tp->tun_mtx);
557 TUNDEBUG(ifp, "address set\n");
560 ifp->if_mtu = ifr->ifr_mtu;
561 TUNDEBUG(ifp, "mtu set\n");
574 * tunoutput - queue packets from higher level ready to put out.
577 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
580 struct tun_softc *tp = ifp->if_softc;
581 u_short cached_tun_flags;
585 TUNDEBUG (ifp, "tunoutput\n");
588 error = mac_ifnet_check_transmit(ifp, m0);
595 /* Could be unlocked read? */
596 mtx_lock(&tp->tun_mtx);
597 cached_tun_flags = tp->tun_flags;
598 mtx_unlock(&tp->tun_mtx);
599 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
600 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
605 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
610 /* BPF writes need to be handled specially. */
611 if (dst->sa_family == AF_UNSPEC)
612 bcopy(dst->sa_data, &af, sizeof(af));
616 if (bpf_peers_present(ifp->if_bpf))
617 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
619 /* prepend sockaddr? this may abort if the mbuf allocation fails */
620 if (cached_tun_flags & TUN_LMODE) {
621 /* allocate space for sockaddr */
622 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
624 /* if allocation failed drop packet */
630 bcopy(dst, m0->m_data, dst->sa_len);
634 if (cached_tun_flags & TUN_IFHEAD) {
635 /* Prepend the address family */
636 M_PREPEND(m0, 4, M_NOWAIT);
638 /* if allocation failed drop packet */
644 *(u_int32_t *)m0->m_data = htonl(af);
651 return (EAFNOSUPPORT);
655 error = (ifp->if_transmit)(ifp, m0);
663 * the cdevsw interface is now pretty minimal.
666 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
670 struct tun_softc *tp = dev->si_drv1;
671 struct tuninfo *tunp;
675 tunp = (struct tuninfo *)data;
676 if (tunp->mtu < IF_MINMTU)
678 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
679 error = priv_check(td, PRIV_NET_SETIFMTU);
683 mtx_lock(&tp->tun_mtx);
684 TUN2IFP(tp)->if_mtu = tunp->mtu;
685 TUN2IFP(tp)->if_type = tunp->type;
686 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
687 mtx_unlock(&tp->tun_mtx);
690 tunp = (struct tuninfo *)data;
691 mtx_lock(&tp->tun_mtx);
692 tunp->mtu = TUN2IFP(tp)->if_mtu;
693 tunp->type = TUN2IFP(tp)->if_type;
694 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
695 mtx_unlock(&tp->tun_mtx);
698 tundebug = *(int *)data;
701 *(int *)data = tundebug;
704 mtx_lock(&tp->tun_mtx);
706 tp->tun_flags |= TUN_LMODE;
707 tp->tun_flags &= ~TUN_IFHEAD;
709 tp->tun_flags &= ~TUN_LMODE;
710 mtx_unlock(&tp->tun_mtx);
713 mtx_lock(&tp->tun_mtx);
715 tp->tun_flags |= TUN_IFHEAD;
716 tp->tun_flags &= ~TUN_LMODE;
718 tp->tun_flags &= ~TUN_IFHEAD;
719 mtx_unlock(&tp->tun_mtx);
722 mtx_lock(&tp->tun_mtx);
723 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
724 mtx_unlock(&tp->tun_mtx);
727 /* deny this if UP */
728 if (TUN2IFP(tp)->if_flags & IFF_UP)
731 switch (*(int *)data & ~IFF_MULTICAST) {
732 case IFF_POINTOPOINT:
734 mtx_lock(&tp->tun_mtx);
735 TUN2IFP(tp)->if_flags &=
736 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
737 TUN2IFP(tp)->if_flags |= *(int *)data;
738 mtx_unlock(&tp->tun_mtx);
745 mtx_lock(&tp->tun_mtx);
746 tp->tun_pid = curthread->td_proc->p_pid;
747 mtx_unlock(&tp->tun_mtx);
752 mtx_lock(&tp->tun_mtx);
754 tp->tun_flags |= TUN_ASYNC;
756 tp->tun_flags &= ~TUN_ASYNC;
757 mtx_unlock(&tp->tun_mtx);
760 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
762 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
763 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
764 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
765 *(int *)data += mb->m_len;
766 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
771 return (fsetown(*(int *)data, &tp->tun_sigio));
774 *(int *)data = fgetown(&tp->tun_sigio);
777 /* This is deprecated, FIOSETOWN should be used instead. */
779 return (fsetown(-(*(int *)data), &tp->tun_sigio));
781 /* This is deprecated, FIOGETOWN should be used instead. */
783 *(int *)data = -fgetown(&tp->tun_sigio);
793 * The cdevsw read interface - reads a packet at a time, or at
794 * least as much of a packet as can be read.
797 tunread(struct cdev *dev, struct uio *uio, int flag)
799 struct tun_softc *tp = dev->si_drv1;
800 struct ifnet *ifp = TUN2IFP(tp);
804 TUNDEBUG (ifp, "read\n");
805 mtx_lock(&tp->tun_mtx);
806 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
807 mtx_unlock(&tp->tun_mtx);
808 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
812 tp->tun_flags &= ~TUN_RWAIT;
815 IFQ_DEQUEUE(&ifp->if_snd, m);
817 if (flag & O_NONBLOCK) {
818 mtx_unlock(&tp->tun_mtx);
819 return (EWOULDBLOCK);
821 tp->tun_flags |= TUN_RWAIT;
822 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
825 mtx_unlock(&tp->tun_mtx);
830 mtx_unlock(&tp->tun_mtx);
832 while (m && uio->uio_resid > 0 && error == 0) {
833 len = min(uio->uio_resid, m->m_len);
835 error = uiomove(mtod(m, void *), len, uio);
840 TUNDEBUG(ifp, "Dropping mbuf\n");
847 * the cdevsw write interface - an atomic write is a packet - or else!
850 tunwrite(struct cdev *dev, struct uio *uio, int flag)
852 struct tun_softc *tp = dev->si_drv1;
853 struct ifnet *ifp = TUN2IFP(tp);
858 TUNDEBUG(ifp, "tunwrite\n");
860 if ((ifp->if_flags & IFF_UP) != IFF_UP)
861 /* ignore silently */
864 if (uio->uio_resid == 0)
867 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
868 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
872 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL) {
877 m->m_pkthdr.rcvif = ifp;
879 mac_ifnet_create_mbuf(ifp, m);
882 /* Could be unlocked read? */
883 mtx_lock(&tp->tun_mtx);
884 if (tp->tun_flags & TUN_IFHEAD) {
885 mtx_unlock(&tp->tun_mtx);
886 if (m->m_len < sizeof(family) &&
887 (m = m_pullup(m, sizeof(family))) == NULL)
889 family = ntohl(*mtod(m, u_int32_t *));
890 m_adj(m, sizeof(family));
892 mtx_unlock(&tp->tun_mtx);
896 BPF_MTAP2(ifp, &family, sizeof(family), m);
921 return (EAFNOSUPPORT);
923 if (harvest.point_to_point)
924 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_TUN);
925 ifp->if_ibytes += m->m_pkthdr.len;
927 CURVNET_SET(ifp->if_vnet);
928 M_SETFIB(m, ifp->if_fib);
929 netisr_dispatch(isr, m);
935 * tunpoll - the poll interface, this is only useful on reads
936 * really. The write detect always returns true, write never blocks
937 * anyway, it either accepts the packet or drops it.
940 tunpoll(struct cdev *dev, int events, struct thread *td)
942 struct tun_softc *tp = dev->si_drv1;
943 struct ifnet *ifp = TUN2IFP(tp);
947 TUNDEBUG(ifp, "tunpoll\n");
949 if (events & (POLLIN | POLLRDNORM)) {
950 IFQ_LOCK(&ifp->if_snd);
951 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
953 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
954 revents |= events & (POLLIN | POLLRDNORM);
956 TUNDEBUG(ifp, "tunpoll waiting\n");
957 selrecord(td, &tp->tun_rsel);
959 IFQ_UNLOCK(&ifp->if_snd);
961 if (events & (POLLOUT | POLLWRNORM))
962 revents |= events & (POLLOUT | POLLWRNORM);
968 * tunkqfilter - support for the kevent() system call.
971 tunkqfilter(struct cdev *dev, struct knote *kn)
973 struct tun_softc *tp = dev->si_drv1;
974 struct ifnet *ifp = TUN2IFP(tp);
976 switch(kn->kn_filter) {
978 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
979 ifp->if_xname, dev2unit(dev));
980 kn->kn_fop = &tun_read_filterops;
984 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
985 ifp->if_xname, dev2unit(dev));
986 kn->kn_fop = &tun_write_filterops;
990 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
991 ifp->if_xname, dev2unit(dev));
996 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1002 * Return true of there is data in the interface queue.
1005 tunkqread(struct knote *kn, long hint)
1008 struct tun_softc *tp = kn->kn_hook;
1009 struct cdev *dev = tp->tun_dev;
1010 struct ifnet *ifp = TUN2IFP(tp);
1012 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1014 "%s have data in the queue. Len = %d, minor = %#x\n",
1015 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1019 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1028 * Always can write, always return MTU in kn->data.
1031 tunkqwrite(struct knote *kn, long hint)
1033 struct tun_softc *tp = kn->kn_hook;
1034 struct ifnet *ifp = TUN2IFP(tp);
1036 kn->kn_data = ifp->if_mtu;
1042 tunkqdetach(struct knote *kn)
1044 struct tun_softc *tp = kn->kn_hook;
1046 knlist_remove(&tp->tun_rsel.si_note, kn, 0);