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"
25 #include <sys/param.h>
28 #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_clone.h>
49 #include <net/if_types.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
53 #include <netinet/in.h>
56 #include <net/if_tun.h>
58 #include <sys/queue.h>
60 #include <security/mac/mac_framework.h>
63 * tun_list is protected by global tunmtx. Other mutable fields are
64 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
65 * static for the duration of a tunnel interface.
68 TAILQ_ENTRY(tun_softc) tun_list;
70 u_short tun_flags; /* misc flags */
71 #define TUN_OPEN 0x0001
72 #define TUN_INITED 0x0002
73 #define TUN_RCOLL 0x0004
74 #define TUN_IASET 0x0008
75 #define TUN_DSTADDR 0x0010
76 #define TUN_LMODE 0x0020
77 #define TUN_RWAIT 0x0040
78 #define TUN_ASYNC 0x0080
79 #define TUN_IFHEAD 0x0100
81 #define TUN_READY (TUN_OPEN | TUN_INITED)
84 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this
85 * actually needed? Can we just return EBUSY if already open?
86 * Problem is that this involved inherent races when a tun device
87 * is handed off from one process to another, as opposed to just
88 * being slightly stale informationally.
90 pid_t tun_pid; /* owning pid */
91 struct ifnet *tun_ifp; /* the interface */
92 struct sigio *tun_sigio; /* information for async I/O */
93 struct selinfo tun_rsel; /* read select */
94 struct mtx tun_mtx; /* protect mutable softc fields */
96 #define TUN2IFP(sc) ((sc)->tun_ifp)
98 #define TUNDEBUG if (tundebug) if_printf
102 * All mutable global variables in if_tun are locked using tunmtx, with
103 * the exception of tundebug, which is used unlocked, and tunclones,
104 * which is static after setup.
106 static struct mtx tunmtx;
107 static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
108 static int tundebug = 0;
109 static int tundclone = 1;
110 static struct clonedevs *tunclones;
111 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
112 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
114 SYSCTL_DECL(_net_link);
115 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
116 "IP tunnel software network interface.");
117 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0,
118 "Enable legacy devfs interface creation.");
120 TUNABLE_INT("net.link.tun.devfs_cloning", &tundclone);
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 int tuninit(struct ifnet *);
127 static int tunmodevent(module_t, int, void *);
128 static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
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 *);
135 IFC_SIMPLE_DECLARE(tun, 0);
137 static d_open_t tunopen;
138 static d_close_t tunclose;
139 static d_read_t tunread;
140 static d_write_t tunwrite;
141 static d_ioctl_t tunioctl;
142 static d_poll_t tunpoll;
143 static d_kqfilter_t tunkqfilter;
145 static int tunkqread(struct knote *, long);
146 static int tunkqwrite(struct knote *, long);
147 static void tunkqdetach(struct knote *);
149 static struct filterops tun_read_filterops = {
152 .f_detach = tunkqdetach,
153 .f_event = tunkqread,
156 static struct filterops tun_write_filterops = {
159 .f_detach = tunkqdetach,
160 .f_event = tunkqwrite,
163 static struct cdevsw tun_cdevsw = {
164 .d_version = D_VERSION,
165 .d_flags = D_PSEUDO | D_NEEDGIANT,
172 .d_kqfilter = tunkqfilter,
177 tun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
182 /* find any existing device, or allocate new unit number */
183 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
185 /* No preexisting struct cdev *, create one */
186 dev = make_dev(&tun_cdevsw, unit2minor(unit),
187 UID_UUCP, GID_DIALER, 0600, "%s%d", ifc->ifc_name, unit);
190 dev->si_flags |= SI_CHEAPCLONE;
193 tuncreate(ifc->ifc_name, dev);
199 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
202 char devname[SPECNAMELEN + 1];
203 int u, i, append_unit;
209 * If tun cloning is enabled, only the superuser can create an
212 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
215 if (strcmp(name, TUNNAME) == 0) {
217 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
218 return; /* Don't recognise the name */
219 if (u != -1 && u > IF_MAXUNIT)
220 return; /* Unit number too high */
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", name,
235 /* No preexisting struct cdev *, create one */
236 *dev = make_dev(&tun_cdevsw, unit2minor(u),
237 UID_UUCP, GID_DIALER, 0600, "%s", name);
240 (*dev)->si_flags |= SI_CHEAPCLONE;
244 if_clone_create(name, namelen, NULL);
248 tun_destroy(struct tun_softc *tp)
253 KASSERT((tp->tun_flags & TUN_OPEN) == 0,
254 ("tununits is out of sync - unit %d", TUN2IFP(tp)->if_dunit));
257 bpfdetach(TUN2IFP(tp));
258 if_detach(TUN2IFP(tp));
259 if_free(TUN2IFP(tp));
261 knlist_destroy(&tp->tun_rsel.si_note);
262 mtx_destroy(&tp->tun_mtx);
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 if_clone_attach(&tun_cloner);
293 if_clone_detach(&tun_cloner);
294 EVENTHANDLER_DEREGISTER(dev_clone, tag);
297 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
298 TAILQ_REMOVE(&tunhead, tp, tun_list);
304 clone_cleanup(&tunclones);
305 mtx_destroy(&tunmtx);
313 static moduledata_t tun_mod = {
319 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
322 tunstart(struct ifnet *ifp)
324 struct tun_softc *tp = ifp->if_softc;
327 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
328 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
329 IFQ_LOCK(&ifp->if_snd);
330 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
332 IFQ_UNLOCK(&ifp->if_snd);
335 IFQ_UNLOCK(&ifp->if_snd);
338 mtx_lock(&tp->tun_mtx);
339 if (tp->tun_flags & TUN_RWAIT) {
340 tp->tun_flags &= ~TUN_RWAIT;
343 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
344 mtx_unlock(&tp->tun_mtx);
345 pgsigio(&tp->tun_sigio, SIGIO, 0);
347 mtx_unlock(&tp->tun_mtx);
348 selwakeuppri(&tp->tun_rsel, PZERO + 1);
349 KNOTE_UNLOCKED(&tp->tun_rsel.si_note, 0);
352 /* XXX: should return an error code so it can fail. */
354 tuncreate(const char *name, struct cdev *dev)
356 struct tun_softc *sc;
359 dev->si_flags &= ~SI_CHEAPCLONE;
361 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
362 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
363 sc->tun_flags = TUN_INITED;
366 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
369 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
371 panic("%s%d: failed to if_alloc() interface.\n",
372 name, dev2unit(dev));
373 if_initname(ifp, name, dev2unit(dev));
374 ifp->if_mtu = TUNMTU;
375 ifp->if_ioctl = tunifioctl;
376 ifp->if_output = tunoutput;
377 ifp->if_start = tunstart;
378 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
380 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
381 ifp->if_snd.ifq_drv_maxlen = 0;
382 IFQ_SET_READY(&ifp->if_snd);
383 knlist_init(&sc->tun_rsel.si_note, NULL, NULL, NULL, NULL);
386 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
388 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
389 ifp->if_xname, minor(dev));
393 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
396 struct tun_softc *tp;
399 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
404 tuncreate(TUNNAME, dev);
409 * XXXRW: This use of tun_pid is subject to error due to the
410 * fact that a reference to the tunnel can live beyond the
411 * death of the process that created it. Can we replace this
412 * with a simple busy flag?
414 mtx_lock(&tp->tun_mtx);
415 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
416 mtx_unlock(&tp->tun_mtx);
419 tp->tun_pid = td->td_proc->p_pid;
421 tp->tun_flags |= TUN_OPEN;
422 mtx_unlock(&tp->tun_mtx);
424 TUNDEBUG(ifp, "open\n");
430 * tunclose - close the device - mark i/f down & delete
434 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
436 struct tun_softc *tp;
443 mtx_lock(&tp->tun_mtx);
444 tp->tun_flags &= ~TUN_OPEN;
448 * junk all pending output
451 IFQ_PURGE(&ifp->if_snd);
453 mtx_unlock(&tp->tun_mtx);
455 if (ifp->if_flags & IFF_UP) {
461 /* Delete all addresses and routes which reference this interface. */
462 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
466 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
467 /* deal w/IPv4 PtP destination; unlocked read */
468 if (ifa->ifa_addr->sa_family == AF_INET) {
469 rtinit(ifa, (int)RTM_DELETE,
470 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
472 rtinit(ifa, (int)RTM_DELETE, 0);
476 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
480 funsetown(&tp->tun_sigio);
481 selwakeuppri(&tp->tun_rsel, PZERO + 1);
482 KNOTE_UNLOCKED(&tp->tun_rsel.si_note, 0);
483 TUNDEBUG (ifp, "closed\n");
488 tuninit(struct ifnet *ifp)
490 struct tun_softc *tp = ifp->if_softc;
494 TUNDEBUG(ifp, "tuninit\n");
496 ifp->if_flags |= IFF_UP;
497 ifp->if_drv_flags |= IFF_DRV_RUNNING;
498 getmicrotime(&ifp->if_lastchange);
501 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
502 if (ifa->ifa_addr->sa_family == AF_INET) {
503 struct sockaddr_in *si;
505 si = (struct sockaddr_in *)ifa->ifa_addr;
506 mtx_lock(&tp->tun_mtx);
507 if (si->sin_addr.s_addr)
508 tp->tun_flags |= TUN_IASET;
510 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
511 if (si && si->sin_addr.s_addr)
512 tp->tun_flags |= TUN_DSTADDR;
513 mtx_unlock(&tp->tun_mtx);
521 * Process an ioctl request.
524 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
526 struct ifreq *ifr = (struct ifreq *)data;
527 struct tun_softc *tp = ifp->if_softc;
534 ifs = (struct ifstat *)data;
535 mtx_lock(&tp->tun_mtx);
537 sprintf(ifs->ascii + strlen(ifs->ascii),
538 "\tOpened by PID %d\n", tp->tun_pid);
539 mtx_unlock(&tp->tun_mtx);
542 error = tuninit(ifp);
543 TUNDEBUG(ifp, "address set, error=%d\n", error);
546 error = tuninit(ifp);
547 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
550 ifp->if_mtu = ifr->ifr_mtu;
551 TUNDEBUG(ifp, "mtu set\n");
565 * tunoutput - queue packets from higher level ready to put out.
571 struct sockaddr *dst,
574 struct tun_softc *tp = ifp->if_softc;
575 u_short cached_tun_flags;
579 TUNDEBUG (ifp, "tunoutput\n");
582 error = mac_check_ifnet_transmit(ifp, m0);
589 /* Could be unlocked read? */
590 mtx_lock(&tp->tun_mtx);
591 cached_tun_flags = tp->tun_flags;
592 mtx_unlock(&tp->tun_mtx);
593 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
594 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
599 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
604 /* BPF writes need to be handled specially. */
605 if (dst->sa_family == AF_UNSPEC) {
606 bcopy(dst->sa_data, &af, sizeof(af));
610 if (bpf_peers_present(ifp->if_bpf)) {
612 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_DONTWAIT);
620 /* if allocation failed drop packet */
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_DONTWAIT);
634 /* if allocation failed drop packet */
640 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
643 if (dst->sa_family != AF_INET)
647 return (EAFNOSUPPORT);
651 IFQ_HANDOFF(ifp, m0, error);
653 ifp->if_collisions++;
661 * the cdevsw interface is now pretty minimal.
664 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
668 struct tun_softc *tp = dev->si_drv1;
669 struct tuninfo *tunp;
673 tunp = (struct tuninfo *)data;
674 if (tunp->mtu < IF_MINMTU)
676 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
677 error = priv_check(td, PRIV_NET_SETIFMTU);
681 TUN2IFP(tp)->if_mtu = tunp->mtu;
682 TUN2IFP(tp)->if_type = tunp->type;
683 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
686 tunp = (struct tuninfo *)data;
687 tunp->mtu = TUN2IFP(tp)->if_mtu;
688 tunp->type = TUN2IFP(tp)->if_type;
689 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
692 tundebug = *(int *)data;
695 *(int *)data = tundebug;
698 mtx_lock(&tp->tun_mtx);
700 tp->tun_flags |= TUN_LMODE;
701 tp->tun_flags &= ~TUN_IFHEAD;
703 tp->tun_flags &= ~TUN_LMODE;
704 mtx_unlock(&tp->tun_mtx);
707 mtx_lock(&tp->tun_mtx);
709 tp->tun_flags |= TUN_IFHEAD;
710 tp->tun_flags &= ~TUN_LMODE;
712 tp->tun_flags &= ~TUN_IFHEAD;
713 mtx_unlock(&tp->tun_mtx);
716 /* Could be unlocked read? */
717 mtx_lock(&tp->tun_mtx);
718 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
719 mtx_unlock(&tp->tun_mtx);
722 /* deny this if UP */
723 if (TUN2IFP(tp)->if_flags & IFF_UP)
726 switch (*(int *)data & ~IFF_MULTICAST) {
727 case IFF_POINTOPOINT:
729 TUN2IFP(tp)->if_flags &=
730 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
731 TUN2IFP(tp)->if_flags |= *(int *)data;
738 mtx_lock(&tp->tun_mtx);
739 tp->tun_pid = curthread->td_proc->p_pid;
740 mtx_unlock(&tp->tun_mtx);
745 mtx_lock(&tp->tun_mtx);
747 tp->tun_flags |= TUN_ASYNC;
749 tp->tun_flags &= ~TUN_ASYNC;
750 mtx_unlock(&tp->tun_mtx);
754 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
756 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
757 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
758 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
759 *(int *)data += mb->m_len;
760 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
766 return (fsetown(*(int *)data, &tp->tun_sigio));
769 *(int *)data = fgetown(&tp->tun_sigio);
772 /* This is deprecated, FIOSETOWN should be used instead. */
774 return (fsetown(-(*(int *)data), &tp->tun_sigio));
776 /* This is deprecated, FIOGETOWN should be used instead. */
778 *(int *)data = -fgetown(&tp->tun_sigio);
788 * The cdevsw read interface - reads a packet at a time, or at
789 * least as much of a packet as can be read.
792 tunread(struct cdev *dev, struct uio *uio, int flag)
794 struct tun_softc *tp = dev->si_drv1;
795 struct ifnet *ifp = TUN2IFP(tp);
799 TUNDEBUG (ifp, "read\n");
800 mtx_lock(&tp->tun_mtx);
801 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
802 mtx_unlock(&tp->tun_mtx);
803 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
807 tp->tun_flags &= ~TUN_RWAIT;
808 mtx_unlock(&tp->tun_mtx);
812 IFQ_DEQUEUE(&ifp->if_snd, m);
814 if (flag & O_NONBLOCK) {
816 return (EWOULDBLOCK);
818 mtx_lock(&tp->tun_mtx);
819 tp->tun_flags |= TUN_RWAIT;
820 mtx_unlock(&tp->tun_mtx);
821 if ((error = tsleep(tp, PCATCH | (PZERO + 1),
822 "tunread", 0)) != 0) {
830 while (m && uio->uio_resid > 0 && error == 0) {
831 len = min(uio->uio_resid, m->m_len);
833 error = uiomove(mtod(m, void *), len, uio);
838 TUNDEBUG(ifp, "Dropping mbuf\n");
845 * the cdevsw write interface - an atomic write is a packet - or else!
848 tunwrite(struct cdev *dev, struct uio *uio, int flag)
850 struct tun_softc *tp = dev->si_drv1;
851 struct ifnet *ifp = TUN2IFP(tp);
857 TUNDEBUG(ifp, "tunwrite\n");
859 if ((ifp->if_flags & IFF_UP) != IFF_UP)
860 /* ignore silently */
863 if (uio->uio_resid == 0)
866 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
867 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
871 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0, M_PKTHDR)) == NULL) {
876 m->m_pkthdr.rcvif = ifp;
878 mac_create_mbuf_from_ifnet(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);
920 return (EAFNOSUPPORT);
922 /* First chunk of an mbuf contains good junk */
923 if (harvest.point_to_point)
924 random_harvest(m, 16, 3, 0, RANDOM_NET);
925 ifp->if_ibytes += m->m_pkthdr.len;
927 netisr_dispatch(isr, m);
932 * tunpoll - the poll interface, this is only useful on reads
933 * really. The write detect always returns true, write never blocks
934 * anyway, it either accepts the packet or drops it.
937 tunpoll(struct cdev *dev, int events, struct thread *td)
940 struct tun_softc *tp = dev->si_drv1;
941 struct ifnet *ifp = TUN2IFP(tp);
946 TUNDEBUG(ifp, "tunpoll\n");
948 if (events & (POLLIN | POLLRDNORM)) {
949 IFQ_LOCK(&ifp->if_snd);
950 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
952 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
953 revents |= events & (POLLIN | POLLRDNORM);
955 TUNDEBUG(ifp, "tunpoll waiting\n");
956 selrecord(td, &tp->tun_rsel);
958 IFQ_UNLOCK(&ifp->if_snd);
960 if (events & (POLLOUT | POLLWRNORM))
961 revents |= events & (POLLOUT | POLLWRNORM);
968 * tunkqfilter - support for the kevent() system call.
971 tunkqfilter(struct cdev *dev, struct knote *kn)
974 struct tun_softc *tp = dev->si_drv1;
975 struct ifnet *ifp = TUN2IFP(tp);
978 switch(kn->kn_filter) {
980 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
981 ifp->if_xname, minor(dev));
982 kn->kn_fop = &tun_read_filterops;
986 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
987 ifp->if_xname, minor(dev));
988 kn->kn_fop = &tun_write_filterops;
992 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
993 ifp->if_xname, minor(dev));
999 kn->kn_hook = (caddr_t) dev;
1000 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1006 * Return true of there is data in the interface queue.
1009 tunkqread(struct knote *kn, long hint)
1012 struct cdev *dev = (struct cdev *)(kn->kn_hook);
1013 struct tun_softc *tp = dev->si_drv1;
1014 struct ifnet *ifp = TUN2IFP(tp);
1017 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1019 "%s have data in the queue. Len = %d, minor = %#x\n",
1020 ifp->if_xname, ifp->if_snd.ifq_len, minor(dev));
1024 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1034 * Always can write, always return MTU in kn->data.
1037 tunkqwrite(struct knote *kn, long hint)
1040 struct tun_softc *tp = ((struct cdev *)kn->kn_hook)->si_drv1;
1041 struct ifnet *ifp = TUN2IFP(tp);
1044 kn->kn_data = ifp->if_mtu;
1051 tunkqdetach(struct knote *kn)
1053 struct tun_softc *tp = ((struct cdev *)kn->kn_hook)->si_drv1;
1055 knlist_remove(&tp->tun_rsel.si_note, kn, 0);