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_clone.h>
49 #include <net/if_types.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
54 #include <netinet/in.h>
57 #include <net/if_tun.h>
59 #include <sys/queue.h>
60 #include <sys/condvar.h>
62 #include <security/mac/mac_framework.h>
65 * tun_list is protected by global tunmtx. Other mutable fields are
66 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
67 * static for the duration of a tunnel interface.
70 TAILQ_ENTRY(tun_softc) tun_list;
72 u_short tun_flags; /* misc flags */
73 #define TUN_OPEN 0x0001
74 #define TUN_INITED 0x0002
75 #define TUN_RCOLL 0x0004
76 #define TUN_IASET 0x0008
77 #define TUN_DSTADDR 0x0010
78 #define TUN_LMODE 0x0020
79 #define TUN_RWAIT 0x0040
80 #define TUN_ASYNC 0x0080
81 #define TUN_IFHEAD 0x0100
83 #define TUN_READY (TUN_OPEN | TUN_INITED)
86 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this
87 * actually needed? Can we just return EBUSY if already open?
88 * Problem is that this involved inherent races when a tun device
89 * is handed off from one process to another, as opposed to just
90 * being slightly stale informationally.
92 pid_t tun_pid; /* owning pid */
93 struct ifnet *tun_ifp; /* the interface */
94 struct sigio *tun_sigio; /* information for async I/O */
95 struct selinfo tun_rsel; /* read select */
96 struct mtx tun_mtx; /* protect mutable softc fields */
97 struct cv tun_cv; /* protect against ref'd dev destroy */
99 #define TUN2IFP(sc) ((sc)->tun_ifp)
101 #define TUNDEBUG if (tundebug) if_printf
102 #define TUNNAME "tun"
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 MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
111 static int tundebug = 0;
112 static int tundclone = 1;
113 static struct clonedevs *tunclones;
114 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
115 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
117 SYSCTL_DECL(_net_link);
118 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
119 "IP tunnel software network interface.");
120 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0,
121 "Enable legacy devfs interface creation.");
123 TUNABLE_INT("net.link.tun.devfs_cloning", &tundclone);
125 static void tunclone(void *arg, struct ucred *cred, char *name,
126 int namelen, struct cdev **dev);
127 static void tuncreate(const char *name, struct cdev *dev);
128 static int tunifioctl(struct ifnet *, u_long, caddr_t);
129 static void tuninit(struct ifnet *);
130 static int tunmodevent(module_t, int, void *);
131 static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
133 static void tunstart(struct ifnet *);
135 static int tun_clone_create(struct if_clone *, int, caddr_t);
136 static void tun_clone_destroy(struct ifnet *);
138 IFC_SIMPLE_DECLARE(tun, 0);
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_PSEUDO | 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", ifc->ifc_name, unit);
192 tuncreate(ifc->ifc_name, 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)
250 mtx_lock(&tp->tun_mtx);
251 if ((tp->tun_flags & TUN_OPEN) != 0)
252 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
254 mtx_unlock(&tp->tun_mtx);
256 CURVNET_SET(TUN2IFP(tp)->if_vnet);
258 bpfdetach(TUN2IFP(tp));
259 if_detach(TUN2IFP(tp));
260 if_free(TUN2IFP(tp));
262 seldrain(&tp->tun_rsel);
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 if_clone_attach(&tun_cloner);
297 if_clone_detach(&tun_cloner);
298 EVENTHANDLER_DEREGISTER(dev_clone, tag);
299 drain_dev_clone_events();
302 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
303 TAILQ_REMOVE(&tunhead, tp, tun_list);
309 clone_cleanup(&tunclones);
310 mtx_destroy(&tunmtx);
318 static moduledata_t tun_mod = {
324 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
325 MODULE_VERSION(if_tun, 1);
328 tunstart(struct ifnet *ifp)
330 struct tun_softc *tp = ifp->if_softc;
333 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
334 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
335 IFQ_LOCK(&ifp->if_snd);
336 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
338 IFQ_UNLOCK(&ifp->if_snd);
341 IFQ_UNLOCK(&ifp->if_snd);
344 mtx_lock(&tp->tun_mtx);
345 if (tp->tun_flags & TUN_RWAIT) {
346 tp->tun_flags &= ~TUN_RWAIT;
349 selwakeuppri(&tp->tun_rsel, PZERO + 1);
350 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
351 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
352 mtx_unlock(&tp->tun_mtx);
353 pgsigio(&tp->tun_sigio, SIGIO, 0);
355 mtx_unlock(&tp->tun_mtx);
358 /* XXX: should return an error code so it can fail. */
360 tuncreate(const char *name, struct cdev *dev)
362 struct tun_softc *sc;
365 dev->si_flags &= ~SI_CHEAPCLONE;
367 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
368 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
369 cv_init(&sc->tun_cv, "tun_condvar");
370 sc->tun_flags = TUN_INITED;
373 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
376 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
378 panic("%s%d: failed to if_alloc() interface.\n",
379 name, dev2unit(dev));
380 if_initname(ifp, name, dev2unit(dev));
381 ifp->if_mtu = TUNMTU;
382 ifp->if_ioctl = tunifioctl;
383 ifp->if_output = tunoutput;
384 ifp->if_start = tunstart;
385 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
387 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
388 ifp->if_snd.ifq_drv_maxlen = 0;
389 IFQ_SET_READY(&ifp->if_snd);
390 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
391 ifp->if_capabilities |= IFCAP_LINKSTATE;
392 ifp->if_capenable |= IFCAP_LINKSTATE;
395 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
397 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
398 ifp->if_xname, dev2unit(dev));
402 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
405 struct tun_softc *tp;
408 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
413 tuncreate(TUNNAME, dev);
418 * XXXRW: This use of tun_pid is subject to error due to the
419 * fact that a reference to the tunnel can live beyond the
420 * death of the process that created it. Can we replace this
421 * with a simple busy flag?
423 mtx_lock(&tp->tun_mtx);
424 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
425 mtx_unlock(&tp->tun_mtx);
428 tp->tun_pid = td->td_proc->p_pid;
430 tp->tun_flags |= TUN_OPEN;
432 if_link_state_change(ifp, LINK_STATE_UP);
433 TUNDEBUG(ifp, "open\n");
434 mtx_unlock(&tp->tun_mtx);
440 * tunclose - close the device - mark i/f down & delete
444 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
446 struct tun_softc *tp;
452 mtx_lock(&tp->tun_mtx);
453 tp->tun_flags &= ~TUN_OPEN;
457 * junk all pending output
459 CURVNET_SET(ifp->if_vnet);
460 IFQ_PURGE(&ifp->if_snd);
462 if (ifp->if_flags & IFF_UP) {
463 mtx_unlock(&tp->tun_mtx);
465 mtx_lock(&tp->tun_mtx);
468 /* Delete all addresses and routes which reference this interface. */
469 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
472 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
473 mtx_unlock(&tp->tun_mtx);
474 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
475 /* deal w/IPv4 PtP destination; unlocked read */
476 if (ifa->ifa_addr->sa_family == AF_INET) {
477 rtinit(ifa, (int)RTM_DELETE,
478 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
480 rtinit(ifa, (int)RTM_DELETE, 0);
484 mtx_lock(&tp->tun_mtx);
486 if_link_state_change(ifp, LINK_STATE_DOWN);
489 funsetown(&tp->tun_sigio);
490 selwakeuppri(&tp->tun_rsel, PZERO + 1);
491 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
492 TUNDEBUG (ifp, "closed\n");
494 cv_broadcast(&tp->tun_cv);
495 mtx_unlock(&tp->tun_mtx);
500 tuninit(struct ifnet *ifp)
502 struct tun_softc *tp = ifp->if_softc;
507 TUNDEBUG(ifp, "tuninit\n");
509 mtx_lock(&tp->tun_mtx);
510 ifp->if_flags |= IFF_UP;
511 ifp->if_drv_flags |= IFF_DRV_RUNNING;
512 getmicrotime(&ifp->if_lastchange);
516 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
517 if (ifa->ifa_addr->sa_family == AF_INET) {
518 struct sockaddr_in *si;
520 si = (struct sockaddr_in *)ifa->ifa_addr;
521 if (si->sin_addr.s_addr)
522 tp->tun_flags |= TUN_IASET;
524 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
525 if (si && si->sin_addr.s_addr)
526 tp->tun_flags |= TUN_DSTADDR;
529 if_addr_runlock(ifp);
531 mtx_unlock(&tp->tun_mtx);
535 * Process an ioctl request.
538 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
540 struct ifreq *ifr = (struct ifreq *)data;
541 struct tun_softc *tp = ifp->if_softc;
547 ifs = (struct ifstat *)data;
548 mtx_lock(&tp->tun_mtx);
550 sprintf(ifs->ascii + strlen(ifs->ascii),
551 "\tOpened by PID %d\n", tp->tun_pid);
552 mtx_unlock(&tp->tun_mtx);
556 TUNDEBUG(ifp, "address set\n");
560 TUNDEBUG(ifp, "destination address set\n");
563 ifp->if_mtu = ifr->ifr_mtu;
564 TUNDEBUG(ifp, "mtu set\n");
577 * tunoutput - queue packets from higher level ready to put out.
580 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
583 struct tun_softc *tp = ifp->if_softc;
584 u_short cached_tun_flags;
588 TUNDEBUG (ifp, "tunoutput\n");
591 error = mac_ifnet_check_transmit(ifp, m0);
598 /* Could be unlocked read? */
599 mtx_lock(&tp->tun_mtx);
600 cached_tun_flags = tp->tun_flags;
601 mtx_unlock(&tp->tun_mtx);
602 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
603 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
608 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
613 /* BPF writes need to be handled specially. */
614 if (dst->sa_family == AF_UNSPEC) {
615 bcopy(dst->sa_data, &af, sizeof(af));
619 if (bpf_peers_present(ifp->if_bpf)) {
621 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
624 /* prepend sockaddr? this may abort if the mbuf allocation fails */
625 if (cached_tun_flags & TUN_LMODE) {
626 /* allocate space for sockaddr */
627 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
629 /* if allocation failed drop packet */
635 bcopy(dst, m0->m_data, dst->sa_len);
639 if (cached_tun_flags & TUN_IFHEAD) {
640 /* Prepend the address family */
641 M_PREPEND(m0, 4, M_DONTWAIT);
643 /* if allocation failed drop packet */
649 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
652 if (dst->sa_family != AF_INET)
656 return (EAFNOSUPPORT);
660 error = (ifp->if_transmit)(ifp, m0);
668 * the cdevsw interface is now pretty minimal.
671 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
675 struct tun_softc *tp = dev->si_drv1;
676 struct tuninfo *tunp;
680 tunp = (struct tuninfo *)data;
681 if (tunp->mtu < IF_MINMTU)
683 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
684 error = priv_check(td, PRIV_NET_SETIFMTU);
688 mtx_lock(&tp->tun_mtx);
689 TUN2IFP(tp)->if_mtu = tunp->mtu;
690 TUN2IFP(tp)->if_type = tunp->type;
691 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
692 mtx_unlock(&tp->tun_mtx);
695 tunp = (struct tuninfo *)data;
696 mtx_lock(&tp->tun_mtx);
697 tunp->mtu = TUN2IFP(tp)->if_mtu;
698 tunp->type = TUN2IFP(tp)->if_type;
699 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
700 mtx_unlock(&tp->tun_mtx);
703 tundebug = *(int *)data;
706 *(int *)data = tundebug;
709 mtx_lock(&tp->tun_mtx);
711 tp->tun_flags |= TUN_LMODE;
712 tp->tun_flags &= ~TUN_IFHEAD;
714 tp->tun_flags &= ~TUN_LMODE;
715 mtx_unlock(&tp->tun_mtx);
718 mtx_lock(&tp->tun_mtx);
720 tp->tun_flags |= TUN_IFHEAD;
721 tp->tun_flags &= ~TUN_LMODE;
723 tp->tun_flags &= ~TUN_IFHEAD;
724 mtx_unlock(&tp->tun_mtx);
727 mtx_lock(&tp->tun_mtx);
728 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
729 mtx_unlock(&tp->tun_mtx);
732 /* deny this if UP */
733 if (TUN2IFP(tp)->if_flags & IFF_UP)
736 switch (*(int *)data & ~IFF_MULTICAST) {
737 case IFF_POINTOPOINT:
739 mtx_lock(&tp->tun_mtx);
740 TUN2IFP(tp)->if_flags &=
741 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
742 TUN2IFP(tp)->if_flags |= *(int *)data;
743 mtx_unlock(&tp->tun_mtx);
750 mtx_lock(&tp->tun_mtx);
751 tp->tun_pid = curthread->td_proc->p_pid;
752 mtx_unlock(&tp->tun_mtx);
757 mtx_lock(&tp->tun_mtx);
759 tp->tun_flags |= TUN_ASYNC;
761 tp->tun_flags &= ~TUN_ASYNC;
762 mtx_unlock(&tp->tun_mtx);
765 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
767 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
768 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
769 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
770 *(int *)data += mb->m_len;
771 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
776 return (fsetown(*(int *)data, &tp->tun_sigio));
779 *(int *)data = fgetown(&tp->tun_sigio);
782 /* This is deprecated, FIOSETOWN should be used instead. */
784 return (fsetown(-(*(int *)data), &tp->tun_sigio));
786 /* This is deprecated, FIOGETOWN should be used instead. */
788 *(int *)data = -fgetown(&tp->tun_sigio);
798 * The cdevsw read interface - reads a packet at a time, or at
799 * least as much of a packet as can be read.
802 tunread(struct cdev *dev, struct uio *uio, int flag)
804 struct tun_softc *tp = dev->si_drv1;
805 struct ifnet *ifp = TUN2IFP(tp);
809 TUNDEBUG (ifp, "read\n");
810 mtx_lock(&tp->tun_mtx);
811 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
812 mtx_unlock(&tp->tun_mtx);
813 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
817 tp->tun_flags &= ~TUN_RWAIT;
820 IFQ_DEQUEUE(&ifp->if_snd, m);
822 if (flag & O_NONBLOCK) {
823 mtx_unlock(&tp->tun_mtx);
824 return (EWOULDBLOCK);
826 tp->tun_flags |= TUN_RWAIT;
827 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
830 mtx_unlock(&tp->tun_mtx);
835 mtx_unlock(&tp->tun_mtx);
837 while (m && uio->uio_resid > 0 && error == 0) {
838 len = min(uio->uio_resid, m->m_len);
840 error = uiomove(mtod(m, void *), len, uio);
845 TUNDEBUG(ifp, "Dropping mbuf\n");
852 * the cdevsw write interface - an atomic write is a packet - or else!
855 tunwrite(struct cdev *dev, struct uio *uio, int flag)
857 struct tun_softc *tp = dev->si_drv1;
858 struct ifnet *ifp = TUN2IFP(tp);
863 TUNDEBUG(ifp, "tunwrite\n");
865 if ((ifp->if_flags & IFF_UP) != IFF_UP)
866 /* ignore silently */
869 if (uio->uio_resid == 0)
872 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
873 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
877 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0, M_PKTHDR)) == NULL) {
882 m->m_pkthdr.rcvif = ifp;
884 mac_ifnet_create_mbuf(ifp, m);
887 /* Could be unlocked read? */
888 mtx_lock(&tp->tun_mtx);
889 if (tp->tun_flags & TUN_IFHEAD) {
890 mtx_unlock(&tp->tun_mtx);
891 if (m->m_len < sizeof(family) &&
892 (m = m_pullup(m, sizeof(family))) == NULL)
894 family = ntohl(*mtod(m, u_int32_t *));
895 m_adj(m, sizeof(family));
897 mtx_unlock(&tp->tun_mtx);
901 BPF_MTAP2(ifp, &family, sizeof(family), m);
926 return (EAFNOSUPPORT);
928 /* First chunk of an mbuf contains good junk */
929 if (harvest.point_to_point)
930 random_harvest(m, 16, 3, 0, RANDOM_NET);
931 ifp->if_ibytes += m->m_pkthdr.len;
933 CURVNET_SET(ifp->if_vnet);
934 M_SETFIB(m, ifp->if_fib);
935 netisr_dispatch(isr, m);
941 * tunpoll - the poll interface, this is only useful on reads
942 * really. The write detect always returns true, write never blocks
943 * anyway, it either accepts the packet or drops it.
946 tunpoll(struct cdev *dev, int events, struct thread *td)
948 struct tun_softc *tp = dev->si_drv1;
949 struct ifnet *ifp = TUN2IFP(tp);
953 TUNDEBUG(ifp, "tunpoll\n");
955 if (events & (POLLIN | POLLRDNORM)) {
956 IFQ_LOCK(&ifp->if_snd);
957 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
959 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
960 revents |= events & (POLLIN | POLLRDNORM);
962 TUNDEBUG(ifp, "tunpoll waiting\n");
963 selrecord(td, &tp->tun_rsel);
965 IFQ_UNLOCK(&ifp->if_snd);
967 if (events & (POLLOUT | POLLWRNORM))
968 revents |= events & (POLLOUT | POLLWRNORM);
974 * tunkqfilter - support for the kevent() system call.
977 tunkqfilter(struct cdev *dev, struct knote *kn)
979 struct tun_softc *tp = dev->si_drv1;
980 struct ifnet *ifp = TUN2IFP(tp);
982 switch(kn->kn_filter) {
984 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
985 ifp->if_xname, dev2unit(dev));
986 kn->kn_fop = &tun_read_filterops;
990 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
991 ifp->if_xname, dev2unit(dev));
992 kn->kn_fop = &tun_write_filterops;
996 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
997 ifp->if_xname, dev2unit(dev));
1002 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1008 * Return true of there is data in the interface queue.
1011 tunkqread(struct knote *kn, long hint)
1014 struct tun_softc *tp = kn->kn_hook;
1015 struct cdev *dev = tp->tun_dev;
1016 struct ifnet *ifp = TUN2IFP(tp);
1018 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1020 "%s have data in the queue. Len = %d, minor = %#x\n",
1021 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1025 "%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)
1039 struct tun_softc *tp = kn->kn_hook;
1040 struct ifnet *ifp = TUN2IFP(tp);
1042 kn->kn_data = ifp->if_mtu;
1048 tunkqdetach(struct knote *kn)
1050 struct tun_softc *tp = kn->kn_hook;
1052 knlist_remove(&tp->tun_rsel.si_note, kn, 0);