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>
26 #include <sys/systm.h>
29 #include <sys/module.h>
30 #include <sys/socket.h>
31 #include <sys/fcntl.h>
32 #include <sys/filio.h>
33 #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>
46 #include <sys/ctype.h>
49 #include <net/if_var.h>
50 #include <net/if_clone.h>
51 #include <net/if_types.h>
52 #include <net/netisr.h>
53 #include <net/route.h>
56 #include <netinet/in.h>
59 #include <net/if_tun.h>
61 #include <sys/queue.h>
62 #include <sys/condvar.h>
64 #include <security/mac/mac_framework.h>
67 * tun_list is protected by global tunmtx. Other mutable fields are
68 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
69 * static for the duration of a tunnel interface.
72 TAILQ_ENTRY(tun_softc) tun_list;
74 u_short tun_flags; /* misc flags */
75 #define TUN_OPEN 0x0001
76 #define TUN_INITED 0x0002
77 #define TUN_RCOLL 0x0004
78 #define TUN_IASET 0x0008
79 #define TUN_DSTADDR 0x0010
80 #define TUN_LMODE 0x0020
81 #define TUN_RWAIT 0x0040
82 #define TUN_ASYNC 0x0080
83 #define TUN_IFHEAD 0x0100
84 #define TUN_DYING 0x0200
86 #define TUN_READY (TUN_OPEN | TUN_INITED)
88 pid_t tun_pid; /* owning pid */
89 struct ifnet *tun_ifp; /* the interface */
90 struct sigio *tun_sigio; /* information for async I/O */
91 struct selinfo tun_rsel; /* read select */
92 struct mtx tun_mtx; /* protect mutable softc fields */
93 struct cv tun_cv; /* protect against ref'd dev destroy */
95 #define TUN2IFP(sc) ((sc)->tun_ifp)
97 #define TUNDEBUG if (tundebug) if_printf
100 * All mutable global variables in if_tun are locked using tunmtx, with
101 * the exception of tundebug, which is used unlocked, and tunclones,
102 * which is static after setup.
104 static struct mtx tunmtx;
105 static eventhandler_tag tag;
106 static const char tunname[] = "tun";
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 static struct sx tun_ioctl_sx;
115 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
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_RWTUN, &tundclone, 0,
121 "Enable legacy devfs interface creation.");
123 static void tunclone(void *arg, struct ucred *cred, char *name,
124 int namelen, struct cdev **dev);
125 static void tuncreate(const char *name, struct cdev *dev);
126 static int tunifioctl(struct ifnet *, u_long, caddr_t);
127 static void tuninit(struct ifnet *);
128 static int tunmodevent(module_t, int, void *);
129 static int tunoutput(struct ifnet *, struct mbuf *,
130 const struct sockaddr *, struct route *ro);
131 static void tunstart(struct ifnet *);
133 static int tun_clone_match(struct if_clone *ifc, const char *name);
134 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
135 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
136 static struct unrhdr *tun_unrhdr;
137 VNET_DEFINE_STATIC(struct if_clone *, tun_cloner);
138 #define V_tun_cloner VNET(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_match(struct if_clone *ifc, const char *name)
182 if (strncmp(tunname, name, 3) == 0 &&
183 (name[3] == '\0' || isdigit(name[3])))
190 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
195 err = ifc_name2unit(name, &unit);
200 /* If this unit number is still available that/s okay. */
201 if (alloc_unr_specific(tun_unrhdr, unit) == -1)
204 unit = alloc_unr(tun_unrhdr);
207 snprintf(name, IFNAMSIZ, "%s%d", tunname, unit);
209 /* find any existing device, or allocate new unit number */
210 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
212 /* No preexisting struct cdev *, create one */
213 dev = make_dev(&tun_cdevsw, unit,
214 UID_UUCP, GID_DIALER, 0600, "%s%d", tunname, unit);
216 tuncreate(tunname, dev);
222 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
225 char devname[SPECNAMELEN + 1];
226 int u, i, append_unit;
232 * If tun cloning is enabled, only the superuser can create an
235 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
238 if (strcmp(name, tunname) == 0) {
240 } else if (dev_stdclone(name, NULL, tunname, &u) != 1)
241 return; /* Don't recognise the name */
242 if (u != -1 && u > IF_MAXUNIT)
243 return; /* Unit number too high */
250 CURVNET_SET(CRED_TO_VNET(cred));
251 /* find any existing device, or allocate new unit number */
252 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
255 namelen = snprintf(devname, sizeof(devname), "%s%d",
259 /* No preexisting struct cdev *, create one */
260 *dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, u, cred,
261 UID_UUCP, GID_DIALER, 0600, "%s", name);
264 if_clone_create(name, namelen, NULL);
269 tun_destroy(struct tun_softc *tp)
273 mtx_lock(&tp->tun_mtx);
274 tp->tun_flags |= TUN_DYING;
275 if ((tp->tun_flags & TUN_OPEN) != 0)
276 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
278 mtx_unlock(&tp->tun_mtx);
280 CURVNET_SET(TUN2IFP(tp)->if_vnet);
281 sx_xlock(&tun_ioctl_sx);
282 TUN2IFP(tp)->if_softc = NULL;
283 sx_xunlock(&tun_ioctl_sx);
286 bpfdetach(TUN2IFP(tp));
287 if_detach(TUN2IFP(tp));
288 free_unr(tun_unrhdr, TUN2IFP(tp)->if_dunit);
289 if_free(TUN2IFP(tp));
291 seldrain(&tp->tun_rsel);
292 knlist_clear(&tp->tun_rsel.si_note, 0);
293 knlist_destroy(&tp->tun_rsel.si_note);
294 mtx_destroy(&tp->tun_mtx);
295 cv_destroy(&tp->tun_cv);
301 tun_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
303 struct tun_softc *tp = ifp->if_softc;
306 TAILQ_REMOVE(&tunhead, tp, tun_list);
314 vnet_tun_init(const void *unused __unused)
316 V_tun_cloner = if_clone_advanced(tunname, 0, tun_clone_match,
317 tun_clone_create, tun_clone_destroy);
319 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
320 vnet_tun_init, NULL);
323 vnet_tun_uninit(const void *unused __unused)
325 if_clone_detach(V_tun_cloner);
327 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
328 vnet_tun_uninit, NULL);
331 tun_uninit(const void *unused __unused)
333 struct tun_softc *tp;
335 EVENTHANDLER_DEREGISTER(dev_clone, tag);
336 drain_dev_clone_events();
339 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
340 TAILQ_REMOVE(&tunhead, tp, tun_list);
346 delete_unrhdr(tun_unrhdr);
347 clone_cleanup(&tunclones);
348 mtx_destroy(&tunmtx);
350 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
353 tunmodevent(module_t mod, int type, void *data)
358 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
359 clone_setup(&tunclones);
360 tun_unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
361 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
366 /* See tun_uninit, so it's done after the vnet_sysuninit() */
374 static moduledata_t tun_mod = {
380 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
381 MODULE_VERSION(if_tun, 1);
384 tunstart(struct ifnet *ifp)
386 struct tun_softc *tp = ifp->if_softc;
389 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
390 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
391 IFQ_LOCK(&ifp->if_snd);
392 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
394 IFQ_UNLOCK(&ifp->if_snd);
397 IFQ_UNLOCK(&ifp->if_snd);
400 mtx_lock(&tp->tun_mtx);
401 if (tp->tun_flags & TUN_RWAIT) {
402 tp->tun_flags &= ~TUN_RWAIT;
405 selwakeuppri(&tp->tun_rsel, PZERO + 1);
406 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
407 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
408 mtx_unlock(&tp->tun_mtx);
409 pgsigio(&tp->tun_sigio, SIGIO, 0);
411 mtx_unlock(&tp->tun_mtx);
414 /* XXX: should return an error code so it can fail. */
416 tuncreate(const char *name, struct cdev *dev)
418 struct tun_softc *sc;
421 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
422 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
423 cv_init(&sc->tun_cv, "tun_condvar");
424 sc->tun_flags = TUN_INITED;
427 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
430 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
432 panic("%s%d: failed to if_alloc() interface.\n",
433 name, dev2unit(dev));
434 if_initname(ifp, name, dev2unit(dev));
435 ifp->if_mtu = TUNMTU;
436 ifp->if_ioctl = tunifioctl;
437 ifp->if_output = tunoutput;
438 ifp->if_start = tunstart;
439 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
441 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
442 ifp->if_snd.ifq_drv_maxlen = 0;
443 IFQ_SET_READY(&ifp->if_snd);
444 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
445 ifp->if_capabilities |= IFCAP_LINKSTATE;
446 ifp->if_capenable |= IFCAP_LINKSTATE;
449 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
451 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
452 ifp->if_xname, dev2unit(dev));
456 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
459 struct tun_softc *tp;
462 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
467 tuncreate(tunname, dev);
471 mtx_lock(&tp->tun_mtx);
472 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
473 mtx_unlock(&tp->tun_mtx);
477 tp->tun_pid = td->td_proc->p_pid;
478 tp->tun_flags |= TUN_OPEN;
480 if_link_state_change(ifp, LINK_STATE_UP);
481 TUNDEBUG(ifp, "open\n");
482 mtx_unlock(&tp->tun_mtx);
488 * tunclose - close the device - mark i/f down & delete
492 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
494 struct tun_softc *tp;
500 mtx_lock(&tp->tun_mtx);
502 * Simply close the device if this isn't the controlling process. This
503 * may happen if, for instance, the tunnel has been handed off to
504 * another process. The original controller should be able to close it
505 * without putting us into an inconsistent state.
507 if (td->td_proc->p_pid != tp->tun_pid) {
508 mtx_unlock(&tp->tun_mtx);
513 * junk all pending output
515 CURVNET_SET(ifp->if_vnet);
516 IFQ_PURGE(&ifp->if_snd);
518 if (ifp->if_flags & IFF_UP) {
519 mtx_unlock(&tp->tun_mtx);
521 mtx_lock(&tp->tun_mtx);
524 /* Delete all addresses and routes which reference this interface. */
525 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
528 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
529 mtx_unlock(&tp->tun_mtx);
530 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
531 /* deal w/IPv4 PtP destination; unlocked read */
532 if (ifa->ifa_addr->sa_family == AF_INET) {
533 rtinit(ifa, (int)RTM_DELETE,
534 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
536 rtinit(ifa, (int)RTM_DELETE, 0);
540 mtx_lock(&tp->tun_mtx);
542 if_link_state_change(ifp, LINK_STATE_DOWN);
545 funsetown(&tp->tun_sigio);
546 selwakeuppri(&tp->tun_rsel, PZERO + 1);
547 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
548 TUNDEBUG (ifp, "closed\n");
549 tp->tun_flags &= ~TUN_OPEN;
552 cv_broadcast(&tp->tun_cv);
553 mtx_unlock(&tp->tun_mtx);
558 tuninit(struct ifnet *ifp)
560 struct tun_softc *tp = ifp->if_softc;
565 TUNDEBUG(ifp, "tuninit\n");
567 mtx_lock(&tp->tun_mtx);
568 ifp->if_flags |= IFF_UP;
569 ifp->if_drv_flags |= IFF_DRV_RUNNING;
570 getmicrotime(&ifp->if_lastchange);
574 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
575 if (ifa->ifa_addr->sa_family == AF_INET) {
576 struct sockaddr_in *si;
578 si = (struct sockaddr_in *)ifa->ifa_addr;
579 if (si->sin_addr.s_addr)
580 tp->tun_flags |= TUN_IASET;
582 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
583 if (si && si->sin_addr.s_addr)
584 tp->tun_flags |= TUN_DSTADDR;
587 if_addr_runlock(ifp);
589 mtx_unlock(&tp->tun_mtx);
593 * Process an ioctl request.
596 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
598 struct ifreq *ifr = (struct ifreq *)data;
599 struct tun_softc *tp;
603 sx_xlock(&tun_ioctl_sx);
611 ifs = (struct ifstat *)data;
612 mtx_lock(&tp->tun_mtx);
614 snprintf(ifs->ascii, sizeof(ifs->ascii),
615 "\tOpened by PID %d\n", tp->tun_pid);
617 ifs->ascii[0] = '\0';
618 mtx_unlock(&tp->tun_mtx);
622 TUNDEBUG(ifp, "address set\n");
625 ifp->if_mtu = ifr->ifr_mtu;
626 TUNDEBUG(ifp, "mtu set\n");
636 sx_xunlock(&tun_ioctl_sx);
641 * tunoutput - queue packets from higher level ready to put out.
644 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
647 struct tun_softc *tp = ifp->if_softc;
648 u_short cached_tun_flags;
652 TUNDEBUG (ifp, "tunoutput\n");
655 error = mac_ifnet_check_transmit(ifp, m0);
662 /* Could be unlocked read? */
663 mtx_lock(&tp->tun_mtx);
664 cached_tun_flags = tp->tun_flags;
665 mtx_unlock(&tp->tun_mtx);
666 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
667 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
672 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
677 /* BPF writes need to be handled specially. */
678 if (dst->sa_family == AF_UNSPEC)
679 bcopy(dst->sa_data, &af, sizeof(af));
683 if (bpf_peers_present(ifp->if_bpf))
684 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
686 /* prepend sockaddr? this may abort if the mbuf allocation fails */
687 if (cached_tun_flags & TUN_LMODE) {
688 /* allocate space for sockaddr */
689 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
691 /* if allocation failed drop packet */
693 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
694 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
697 bcopy(dst, m0->m_data, dst->sa_len);
701 if (cached_tun_flags & TUN_IFHEAD) {
702 /* Prepend the address family */
703 M_PREPEND(m0, 4, M_NOWAIT);
705 /* if allocation failed drop packet */
707 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
708 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
711 *(u_int32_t *)m0->m_data = htonl(af);
718 return (EAFNOSUPPORT);
722 error = (ifp->if_transmit)(ifp, m0);
725 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
730 * the cdevsw interface is now pretty minimal.
733 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
737 struct tun_softc *tp = dev->si_drv1;
738 struct tuninfo *tunp;
743 tunp = (struct tuninfo *)data;
744 if (TUN2IFP(tp)->if_type != tunp->type)
746 mtx_lock(&tp->tun_mtx);
747 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
748 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
749 ifr.ifr_mtu = tunp->mtu;
750 CURVNET_SET(TUN2IFP(tp)->if_vnet);
751 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
755 mtx_unlock(&tp->tun_mtx);
759 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
760 mtx_unlock(&tp->tun_mtx);
763 tunp = (struct tuninfo *)data;
764 mtx_lock(&tp->tun_mtx);
765 tunp->mtu = TUN2IFP(tp)->if_mtu;
766 tunp->type = TUN2IFP(tp)->if_type;
767 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
768 mtx_unlock(&tp->tun_mtx);
771 tundebug = *(int *)data;
774 *(int *)data = tundebug;
777 mtx_lock(&tp->tun_mtx);
779 tp->tun_flags |= TUN_LMODE;
780 tp->tun_flags &= ~TUN_IFHEAD;
782 tp->tun_flags &= ~TUN_LMODE;
783 mtx_unlock(&tp->tun_mtx);
786 mtx_lock(&tp->tun_mtx);
788 tp->tun_flags |= TUN_IFHEAD;
789 tp->tun_flags &= ~TUN_LMODE;
791 tp->tun_flags &= ~TUN_IFHEAD;
792 mtx_unlock(&tp->tun_mtx);
795 mtx_lock(&tp->tun_mtx);
796 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
797 mtx_unlock(&tp->tun_mtx);
800 /* deny this if UP */
801 if (TUN2IFP(tp)->if_flags & IFF_UP)
804 switch (*(int *)data & ~IFF_MULTICAST) {
805 case IFF_POINTOPOINT:
807 mtx_lock(&tp->tun_mtx);
808 TUN2IFP(tp)->if_flags &=
809 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
810 TUN2IFP(tp)->if_flags |= *(int *)data;
811 mtx_unlock(&tp->tun_mtx);
818 mtx_lock(&tp->tun_mtx);
819 tp->tun_pid = curthread->td_proc->p_pid;
820 mtx_unlock(&tp->tun_mtx);
825 mtx_lock(&tp->tun_mtx);
827 tp->tun_flags |= TUN_ASYNC;
829 tp->tun_flags &= ~TUN_ASYNC;
830 mtx_unlock(&tp->tun_mtx);
833 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
835 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
836 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
837 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
838 *(int *)data += mb->m_len;
839 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
844 return (fsetown(*(int *)data, &tp->tun_sigio));
847 *(int *)data = fgetown(&tp->tun_sigio);
850 /* This is deprecated, FIOSETOWN should be used instead. */
852 return (fsetown(-(*(int *)data), &tp->tun_sigio));
854 /* This is deprecated, FIOGETOWN should be used instead. */
856 *(int *)data = -fgetown(&tp->tun_sigio);
866 * The cdevsw read interface - reads a packet at a time, or at
867 * least as much of a packet as can be read.
870 tunread(struct cdev *dev, struct uio *uio, int flag)
872 struct tun_softc *tp = dev->si_drv1;
873 struct ifnet *ifp = TUN2IFP(tp);
877 TUNDEBUG (ifp, "read\n");
878 mtx_lock(&tp->tun_mtx);
879 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
880 mtx_unlock(&tp->tun_mtx);
881 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
885 tp->tun_flags &= ~TUN_RWAIT;
888 IFQ_DEQUEUE(&ifp->if_snd, m);
890 if (flag & O_NONBLOCK) {
891 mtx_unlock(&tp->tun_mtx);
892 return (EWOULDBLOCK);
894 tp->tun_flags |= TUN_RWAIT;
895 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
898 mtx_unlock(&tp->tun_mtx);
903 mtx_unlock(&tp->tun_mtx);
905 while (m && uio->uio_resid > 0 && error == 0) {
906 len = min(uio->uio_resid, m->m_len);
908 error = uiomove(mtod(m, void *), len, uio);
913 TUNDEBUG(ifp, "Dropping mbuf\n");
920 * the cdevsw write interface - an atomic write is a packet - or else!
923 tunwrite(struct cdev *dev, struct uio *uio, int flag)
925 struct tun_softc *tp = dev->si_drv1;
926 struct ifnet *ifp = TUN2IFP(tp);
928 uint32_t family, mru;
931 TUNDEBUG(ifp, "tunwrite\n");
933 if ((ifp->if_flags & IFF_UP) != IFF_UP)
934 /* ignore silently */
937 if (uio->uio_resid == 0)
941 if (tp->tun_flags & TUN_IFHEAD)
942 mru += sizeof(family);
943 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
944 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
948 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL) {
949 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
953 m->m_pkthdr.rcvif = ifp;
955 mac_ifnet_create_mbuf(ifp, m);
958 /* Could be unlocked read? */
959 mtx_lock(&tp->tun_mtx);
960 if (tp->tun_flags & TUN_IFHEAD) {
961 mtx_unlock(&tp->tun_mtx);
962 if (m->m_len < sizeof(family) &&
963 (m = m_pullup(m, sizeof(family))) == NULL)
965 family = ntohl(*mtod(m, u_int32_t *));
966 m_adj(m, sizeof(family));
968 mtx_unlock(&tp->tun_mtx);
972 BPF_MTAP2(ifp, &family, sizeof(family), m);
987 return (EAFNOSUPPORT);
989 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
990 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
991 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
992 CURVNET_SET(ifp->if_vnet);
993 M_SETFIB(m, ifp->if_fib);
994 netisr_dispatch(isr, m);
1000 * tunpoll - the poll interface, this is only useful on reads
1001 * really. The write detect always returns true, write never blocks
1002 * anyway, it either accepts the packet or drops it.
1005 tunpoll(struct cdev *dev, int events, struct thread *td)
1007 struct tun_softc *tp = dev->si_drv1;
1008 struct ifnet *ifp = TUN2IFP(tp);
1012 TUNDEBUG(ifp, "tunpoll\n");
1014 if (events & (POLLIN | POLLRDNORM)) {
1015 IFQ_LOCK(&ifp->if_snd);
1016 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
1018 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1019 revents |= events & (POLLIN | POLLRDNORM);
1021 TUNDEBUG(ifp, "tunpoll waiting\n");
1022 selrecord(td, &tp->tun_rsel);
1024 IFQ_UNLOCK(&ifp->if_snd);
1026 if (events & (POLLOUT | POLLWRNORM))
1027 revents |= events & (POLLOUT | POLLWRNORM);
1033 * tunkqfilter - support for the kevent() system call.
1036 tunkqfilter(struct cdev *dev, struct knote *kn)
1038 struct tun_softc *tp = dev->si_drv1;
1039 struct ifnet *ifp = TUN2IFP(tp);
1041 switch(kn->kn_filter) {
1043 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1044 ifp->if_xname, dev2unit(dev));
1045 kn->kn_fop = &tun_read_filterops;
1049 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1050 ifp->if_xname, dev2unit(dev));
1051 kn->kn_fop = &tun_write_filterops;
1055 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1056 ifp->if_xname, dev2unit(dev));
1061 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1067 * Return true of there is data in the interface queue.
1070 tunkqread(struct knote *kn, long hint)
1073 struct tun_softc *tp = kn->kn_hook;
1074 struct cdev *dev = tp->tun_dev;
1075 struct ifnet *ifp = TUN2IFP(tp);
1077 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1079 "%s have data in the queue. Len = %d, minor = %#x\n",
1080 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1084 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1093 * Always can write, always return MTU in kn->data.
1096 tunkqwrite(struct knote *kn, long hint)
1098 struct tun_softc *tp = kn->kn_hook;
1099 struct ifnet *ifp = TUN2IFP(tp);
1101 kn->kn_data = ifp->if_mtu;
1107 tunkqdetach(struct knote *kn)
1109 struct tun_softc *tp = kn->kn_hook;
1111 knlist_remove(&tp->tun_rsel.si_note, kn, 0);