Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.62
      1 /*	$NetBSD: if_tun.c,v 1.62 2003/06/28 14:22:07 darrenr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
      5  * Nottingham University 1987.
      6  *
      7  * This source may be freely distributed, however I would be interested
      8  * in any changes that are made.
      9  *
     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 its
     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
     14  * operation though.
     15  */
     16 
     17 #include <sys/cdefs.h>
     18 __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.62 2003/06/28 14:22:07 darrenr Exp $");
     19 
     20 #include "tun.h"
     21 
     22 #include "opt_inet.h"
     23 #include "opt_ns.h"
     24 
     25 #include <sys/param.h>
     26 #include <sys/proc.h>
     27 #include <sys/systm.h>
     28 #include <sys/mbuf.h>
     29 #include <sys/buf.h>
     30 #include <sys/protosw.h>
     31 #include <sys/socket.h>
     32 #include <sys/ioctl.h>
     33 #include <sys/errno.h>
     34 #include <sys/syslog.h>
     35 #include <sys/select.h>
     36 #include <sys/poll.h>
     37 #include <sys/file.h>
     38 #include <sys/signalvar.h>
     39 #include <sys/conf.h>
     40 
     41 #include <machine/cpu.h>
     42 
     43 #include <net/if.h>
     44 #include <net/if_ether.h>
     45 #include <net/netisr.h>
     46 #include <net/route.h>
     47 
     48 
     49 #ifdef INET
     50 #include <netinet/in.h>
     51 #include <netinet/in_systm.h>
     52 #include <netinet/in_var.h>
     53 #include <netinet/ip.h>
     54 #include <netinet/if_inarp.h>
     55 #endif
     56 
     57 #ifdef NS
     58 #include <netns/ns.h>
     59 #include <netns/ns_if.h>
     60 #endif
     61 
     62 #include "bpfilter.h"
     63 #if NBPFILTER > 0
     64 #include <sys/time.h>
     65 #include <net/bpf.h>
     66 #endif
     67 
     68 #include <net/if_tun.h>
     69 
     70 #define TUNDEBUG	if (tundebug) printf
     71 int	tundebug = 0;
     72 
     73 extern int ifqmaxlen;
     74 void	tunattach __P((int));
     75 LIST_HEAD(, tun_softc) tun_softc_list;
     76 static struct simplelock tun_softc_lock;
     77 
     78 int	tun_ioctl __P((struct ifnet *, u_long, caddr_t));
     79 int	tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
     80 		       struct rtentry *rt));
     81 int	tun_clone_create __P((struct if_clone *, int));
     82 void	tun_clone_destroy __P((struct ifnet *));
     83 
     84 struct if_clone tun_cloner =
     85     IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
     86 
     87 static void tunattach0 __P((struct tun_softc *));
     88 static void tuninit __P((struct tun_softc *));
     89 #ifdef ALTQ
     90 static void tunstart __P((struct ifnet *));
     91 #endif
     92 static struct tun_softc *tun_find_unit __P((dev_t));
     93 
     94 dev_type_open(tunopen);
     95 dev_type_close(tunclose);
     96 dev_type_read(tunread);
     97 dev_type_write(tunwrite);
     98 dev_type_ioctl(tunioctl);
     99 dev_type_poll(tunpoll);
    100 dev_type_kqfilter(tunkqfilter);
    101 
    102 const struct cdevsw tun_cdevsw = {
    103 	tunopen, tunclose, tunread, tunwrite, tunioctl,
    104 	nostop, notty, tunpoll, nommap, tunkqfilter,
    105 };
    106 
    107 void
    108 tunattach(unused)
    109 	int unused;
    110 {
    111 
    112 	simple_lock_init(&tun_softc_lock);
    113 	LIST_INIT(&tun_softc_list);
    114 	if_clone_attach(&tun_cloner);
    115 }
    116 
    117 int
    118 tun_clone_create(ifc, unit)
    119 	struct if_clone *ifc;
    120 	int unit;
    121 {
    122 	struct tun_softc *sc;
    123 
    124 	sc = malloc(sizeof(struct tun_softc), M_DEVBUF, M_WAITOK);
    125 	(void)memset(sc, 0, sizeof(struct tun_softc));
    126 
    127 	(void)snprintf(sc->tun_if.if_xname, sizeof(sc->tun_if.if_xname),
    128 	    "%s%d", ifc->ifc_name, unit);
    129 	sc->tun_unit = unit;
    130 	simple_lock_init(&sc->tun_lock);
    131 
    132 	tunattach0(sc);
    133 
    134 	simple_lock(&tun_softc_lock);
    135 	LIST_INSERT_HEAD(&tun_softc_list, sc, tun_list);
    136 	simple_unlock(&tun_softc_lock);
    137 
    138 	return (0);
    139 }
    140 
    141 void
    142 tunattach0(sc)
    143 	struct tun_softc *sc;
    144 {
    145 	struct ifnet *ifp = (void *)sc;
    146 
    147 	sc->tun_flags = TUN_INITED;
    148 
    149 	ifp = &sc->tun_if;
    150 	ifp->if_softc = sc;
    151 	ifp->if_mtu = TUNMTU;
    152 	ifp->if_ioctl = tun_ioctl;
    153 	ifp->if_output = tun_output;
    154 #ifdef ALTQ
    155 	ifp->if_start = tunstart;
    156 #endif
    157 	ifp->if_flags = IFF_POINTOPOINT;
    158 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
    159 	ifp->if_collisions = 0;
    160 	ifp->if_ierrors = 0;
    161 	ifp->if_oerrors = 0;
    162 	ifp->if_ipackets = 0;
    163 	ifp->if_opackets = 0;
    164 	ifp->if_ibytes   = 0;
    165 	ifp->if_obytes   = 0;
    166 	ifp->if_dlt = DLT_NULL;
    167 	IFQ_SET_READY(&ifp->if_snd);
    168 	if_attach(ifp);
    169 	if_alloc_sadl(ifp);
    170 #if NBPFILTER > 0
    171 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
    172 #endif
    173 }
    174 
    175 void
    176 tun_clone_destroy(ifp)
    177 	struct ifnet *ifp;
    178 {
    179 	struct tun_softc *tp = (void *)ifp;
    180 	struct proc *p;
    181 
    182 	simple_lock(&tun_softc_lock);
    183 	simple_lock(&tp->tun_lock);
    184 	LIST_REMOVE(tp, tun_list);
    185 	simple_unlock(&tp->tun_lock);
    186 	simple_unlock(&tun_softc_lock);
    187 
    188 	if (tp->tun_flags & TUN_RWAIT) {
    189 		tp->tun_flags &= ~TUN_RWAIT;
    190 		wakeup((caddr_t)tp);
    191 	}
    192 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    193 		if (tp->tun_pgrp > 0)
    194 			gsignal(tp->tun_pgrp, SIGIO);
    195 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    196 			psignal(p, SIGIO);
    197 	}
    198 	selwakeup(&tp->tun_rsel);
    199 
    200 #if NBPFILTER > 0
    201 	bpfdetach(ifp);
    202 #endif
    203 	if_detach(ifp);
    204 
    205 	free(tp, M_DEVBUF);
    206 }
    207 
    208 static struct tun_softc *
    209 tun_find_unit(dev)
    210 	dev_t dev;
    211 {
    212 	struct tun_softc *tp;
    213 	int unit = minor(dev);
    214 
    215 	simple_lock(&tun_softc_lock);
    216 	LIST_FOREACH(tp, &tun_softc_list, tun_list)
    217 		if (unit == tp->tun_unit)
    218 			break;
    219 	if (tp)
    220 		simple_lock(&tp->tun_lock);
    221 	simple_unlock(&tun_softc_lock);
    222 
    223 	return (tp);
    224 }
    225 
    226 /*
    227  * tunnel open - must be superuser & the device must be
    228  * configured in
    229  */
    230 int
    231 tunopen(dev, flag, mode, l)
    232 	dev_t	dev;
    233 	int	flag, mode;
    234 	struct lwp *l;
    235 {
    236 	struct proc 	*p = l->l_proc;
    237 	struct ifnet	*ifp;
    238 	struct tun_softc *tp;
    239 	int	error;
    240 
    241 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    242 		return (error);
    243 
    244 	if (NTUN < 1)
    245 		return (ENXIO);
    246 
    247 	tp = tun_find_unit(dev);
    248 
    249 	if (!tp) {
    250 		(void)tun_clone_create(&tun_cloner, minor(dev));
    251 		tp = tun_find_unit(dev);
    252 	}
    253 
    254 	if (!tp)
    255 		return (ENXIO);
    256 
    257 	if (tp->tun_flags & TUN_OPEN) {
    258 		simple_unlock(&tp->tun_lock);
    259 		return (EBUSY);
    260 	}
    261 
    262 	ifp = &tp->tun_if;
    263 	tp->tun_flags |= TUN_OPEN;
    264 	TUNDEBUG("%s: open\n", ifp->if_xname);
    265 	simple_unlock(&tp->tun_lock);
    266 	return (0);
    267 }
    268 
    269 /*
    270  * tunclose - close the device - mark i/f down & delete
    271  * routing info
    272  */
    273 int
    274 tunclose(dev, flag, mode, l)
    275 	dev_t	dev;
    276 	int	flag;
    277 	int	mode;
    278 	struct lwp *l;
    279 {
    280 	int	s;
    281 	struct tun_softc *tp;
    282 	struct ifnet	*ifp;
    283 
    284 	tp = tun_find_unit(dev);
    285 
    286 	/* interface was "destroyed" before the close */
    287 	if (tp == NULL)
    288 		return (0);
    289 
    290 	ifp = &tp->tun_if;
    291 
    292 	tp->tun_flags &= ~TUN_OPEN;
    293 
    294 	/*
    295 	 * junk all pending output
    296 	 */
    297 	s = splnet();
    298 	IFQ_PURGE(&ifp->if_snd);
    299 	splx(s);
    300 
    301 	if (ifp->if_flags & IFF_UP) {
    302 		s = splnet();
    303 		if_down(ifp);
    304 		if (ifp->if_flags & IFF_RUNNING) {
    305 			/* find internet addresses and delete routes */
    306 			struct ifaddr *ifa;
    307 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    308 #ifdef INET
    309 				if (ifa->ifa_addr->sa_family == AF_INET) {
    310 					rtinit(ifa, (int)RTM_DELETE,
    311 					       tp->tun_flags & TUN_DSTADDR
    312 							? RTF_HOST
    313 							: 0);
    314 				}
    315 #endif
    316 			}
    317 		}
    318 		splx(s);
    319 	}
    320 	tp->tun_pgrp = 0;
    321 	selnotify(&tp->tun_rsel, 0);
    322 
    323 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
    324 	simple_unlock(&tp->tun_lock);
    325 	return (0);
    326 }
    327 
    328 static void
    329 tuninit(tp)
    330 	struct tun_softc *tp;
    331 {
    332 	struct ifnet	*ifp = &tp->tun_if;
    333 	struct ifaddr	*ifa;
    334 
    335 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
    336 
    337 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    338 
    339 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
    340 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    341 #ifdef INET
    342 		if (ifa->ifa_addr->sa_family == AF_INET) {
    343 			struct sockaddr_in *sin;
    344 
    345 			sin = satosin(ifa->ifa_addr);
    346 			if (sin && sin->sin_addr.s_addr)
    347 				tp->tun_flags |= TUN_IASET;
    348 
    349 			if (ifp->if_flags & IFF_POINTOPOINT) {
    350 				sin = satosin(ifa->ifa_dstaddr);
    351 				if (sin && sin->sin_addr.s_addr)
    352 					tp->tun_flags |= TUN_DSTADDR;
    353 			}
    354 		}
    355 #endif
    356 	}
    357 
    358 	return;
    359 }
    360 
    361 /*
    362  * Process an ioctl request.
    363  */
    364 int
    365 tun_ioctl(ifp, cmd, data)
    366 	struct ifnet *ifp;
    367 	u_long cmd;
    368 	caddr_t	data;
    369 {
    370 	int		error = 0, s;
    371 	struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
    372 
    373 	simple_lock(&tp->tun_lock);
    374 
    375 	s = splnet();
    376 	switch (cmd) {
    377 	case SIOCSIFADDR:
    378 		tuninit((struct tun_softc *)(ifp->if_softc));
    379 		TUNDEBUG("%s: address set\n", ifp->if_xname);
    380 		break;
    381 	case SIOCSIFDSTADDR:
    382 		tuninit((struct tun_softc *)(ifp->if_softc));
    383 		TUNDEBUG("%s: destination address set\n", ifp->if_xname);
    384 		break;
    385 	case SIOCSIFBRDADDR:
    386 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
    387 		break;
    388 	case SIOCSIFMTU: {
    389 		struct ifreq *ifr = (struct ifreq *) data;
    390 		if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
    391 		    error = EINVAL;
    392 		    break;
    393 		}
    394 		TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
    395 		ifp->if_mtu = ifr->ifr_mtu;
    396 		break;
    397 	}
    398 	case SIOCADDMULTI:
    399 	case SIOCDELMULTI: {
    400 		struct ifreq *ifr = (struct ifreq *) data;
    401 		if (ifr == 0) {
    402 	        	error = EAFNOSUPPORT;           /* XXX */
    403 			break;
    404 		}
    405 		switch (ifr->ifr_addr.sa_family) {
    406 
    407 #ifdef INET
    408 		case AF_INET:
    409 			break;
    410 #endif
    411 
    412 		default:
    413 			error = EAFNOSUPPORT;
    414 			break;
    415 		}
    416 		break;
    417 	}
    418 	case SIOCSIFFLAGS:
    419 		break;
    420 	default:
    421 		error = EINVAL;
    422 	}
    423 	splx(s);
    424 	simple_unlock(&tp->tun_lock);
    425 	return (error);
    426 }
    427 
    428 /*
    429  * tun_output - queue packets from higher level ready to put out.
    430  */
    431 int
    432 tun_output(ifp, m0, dst, rt)
    433 	struct ifnet   *ifp;
    434 	struct mbuf    *m0;
    435 	struct sockaddr *dst;
    436 	struct rtentry *rt;
    437 {
    438 	struct tun_softc *tp = ifp->if_softc;
    439 	struct proc	*p;
    440 #ifdef INET
    441 	int		s;
    442 	int		error;
    443 #endif
    444 	int		mlen;
    445 	ALTQ_DECL(struct altq_pktattr pktattr;)
    446 
    447 	simple_lock(&tp->tun_lock);
    448 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
    449 
    450 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    451 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
    452 			  tp->tun_flags);
    453 		m_freem (m0);
    454 		simple_unlock(&tp->tun_lock);
    455 		return (EHOSTDOWN);
    456 	}
    457 
    458 	/*
    459 	 * if the queueing discipline needs packet classification,
    460 	 * do it before prepending link headers.
    461 	 */
    462 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
    463 
    464 #if NBPFILTER > 0
    465 	if (ifp->if_bpf) {
    466 		/*
    467 		 * We need to prepend the address family as
    468 		 * a four byte field.  Cons up a dummy header
    469 		 * to pacify bpf.  This is safe because bpf
    470 		 * will only read from the mbuf (i.e., it won't
    471 		 * try to free it or keep a pointer to it).
    472 		 */
    473 		struct mbuf m;
    474 		u_int32_t af = dst->sa_family;
    475 
    476 		m.m_flags = 0;
    477 		m.m_next = m0;
    478 		m.m_len = sizeof(af);
    479 		m.m_data = (char *)&af;
    480 
    481 		bpf_mtap(ifp->if_bpf, &m);
    482 	}
    483 #endif
    484 
    485 	switch(dst->sa_family) {
    486 #ifdef INET
    487 	case AF_INET:
    488 		if (tp->tun_flags & TUN_PREPADDR) {
    489 			/* Simple link-layer header */
    490 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
    491 			if (m0 == NULL) {
    492 				IF_DROP(&ifp->if_snd);
    493 				simple_unlock(&tp->tun_lock);
    494 				return (ENOBUFS);
    495 			}
    496 			bcopy(dst, mtod(m0, char *), dst->sa_len);
    497 		}
    498 		/* FALLTHROUGH */
    499 	case AF_UNSPEC:
    500 		s = splnet();
    501 		IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
    502 		if (error) {
    503 			splx(s);
    504 			ifp->if_collisions++;
    505 			return (error);
    506 		}
    507 		mlen = m0->m_pkthdr.len;
    508 		splx(s);
    509 		ifp->if_opackets++;
    510 		ifp->if_obytes += mlen;
    511 		break;
    512 #endif
    513 	default:
    514 		m_freem(m0);
    515 		simple_unlock(&tp->tun_lock);
    516 		return (EAFNOSUPPORT);
    517 	}
    518 
    519 	if (tp->tun_flags & TUN_RWAIT) {
    520 		tp->tun_flags &= ~TUN_RWAIT;
    521 		wakeup((caddr_t)tp);
    522 	}
    523 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    524 		if (tp->tun_pgrp > 0)
    525 			gsignal(tp->tun_pgrp, SIGIO);
    526 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    527 			psignal(p, SIGIO);
    528 	}
    529 	selnotify(&tp->tun_rsel, 0);
    530 	simple_unlock(&tp->tun_lock);
    531 	return (0);
    532 }
    533 
    534 /*
    535  * the cdevsw interface is now pretty minimal.
    536  */
    537 int
    538 tunioctl(dev, cmd, data, flag, l)
    539 	dev_t		dev;
    540 	u_long		cmd;
    541 	caddr_t		data;
    542 	int		flag;
    543 	struct lwp	*l;
    544 {
    545 	int		s;
    546 	struct tun_softc *tp;
    547 	pid_t pgid;
    548 	int error;
    549 
    550 	tp = tun_find_unit(dev);
    551 
    552 	/* interface was "destroyed" already */
    553 	if (tp == NULL)
    554 		return (ENXIO);
    555 
    556 	switch (cmd) {
    557 	case TUNSDEBUG:
    558 		tundebug = *(int *)data;
    559 		break;
    560 
    561 	case TUNGDEBUG:
    562 		*(int *)data = tundebug;
    563 		break;
    564 
    565 	case TUNSIFMODE:
    566 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
    567 		case IFF_POINTOPOINT:
    568 		case IFF_BROADCAST:
    569 			s = splnet();
    570 			if (tp->tun_if.if_flags & IFF_UP) {
    571 				splx(s);
    572 				simple_unlock(&tp->tun_lock);
    573 				return (EBUSY);
    574 			}
    575 			tp->tun_if.if_flags &=
    576 				~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
    577 			tp->tun_if.if_flags |= *(int *)data;
    578 			splx(s);
    579 			break;
    580 		default:
    581 			simple_unlock(&tp->tun_lock);
    582 			return (EINVAL);
    583 		}
    584 		break;
    585 
    586 	case TUNSLMODE:
    587 		if (*(int *)data)
    588 			tp->tun_flags |= TUN_PREPADDR;
    589 		else
    590 			tp->tun_flags &= ~TUN_PREPADDR;
    591 		break;
    592 
    593 	case FIONBIO:
    594 		if (*(int *)data)
    595 			tp->tun_flags |= TUN_NBIO;
    596 		else
    597 			tp->tun_flags &= ~TUN_NBIO;
    598 		break;
    599 
    600 	case FIOASYNC:
    601 		if (*(int *)data)
    602 			tp->tun_flags |= TUN_ASYNC;
    603 		else
    604 			tp->tun_flags &= ~TUN_ASYNC;
    605 		break;
    606 
    607 	case FIONREAD:
    608 		s = splnet();
    609 		if (tp->tun_if.if_snd.ifq_head)
    610 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    611 		else
    612 			*(int *)data = 0;
    613 		splx(s);
    614 		break;
    615 
    616 	case TIOCSPGRP:
    617 		pgid = *(int *)data;
    618 		if (pgid != 0) {
    619 			error = pgid_in_session(l->l_proc, pgid);
    620 			if (error != 0)
    621 				return error;
    622 		}
    623 		tp->tun_pgrp = pgid;
    624 		break;
    625 
    626 	case TIOCGPGRP:
    627 		*(int *)data = tp->tun_pgrp;
    628 		break;
    629 
    630 	default:
    631 		simple_unlock(&tp->tun_lock);
    632 		return (ENOTTY);
    633 	}
    634 	simple_unlock(&tp->tun_lock);
    635 	return (0);
    636 }
    637 
    638 /*
    639  * The cdevsw read interface - reads a packet at a time, or at
    640  * least as much of a packet as can be read.
    641  */
    642 int
    643 tunread(dev, uio, ioflag)
    644 	dev_t		dev;
    645 	struct uio	*uio;
    646 	int		ioflag;
    647 {
    648 	struct tun_softc *tp;
    649 	struct ifnet	*ifp;
    650 	struct mbuf	*m, *m0;
    651 	int		error=0, len, s, index;
    652 
    653 	tp = tun_find_unit(dev);
    654 
    655 	/* interface was "destroyed" already */
    656 	if (tp == NULL)
    657 		return (ENXIO);
    658 
    659 	index = tp->tun_if.if_index;
    660 	ifp = &tp->tun_if;
    661 
    662 	TUNDEBUG ("%s: read\n", ifp->if_xname);
    663 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    664 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
    665 		simple_unlock(&tp->tun_lock);
    666 		return EHOSTDOWN;
    667 	}
    668 
    669 	tp->tun_flags &= ~TUN_RWAIT;
    670 
    671 	s = splnet();
    672 	do {
    673 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    674 		if (m0 == 0) {
    675 			if (tp->tun_flags & TUN_NBIO) {
    676 				splx(s);
    677 				simple_unlock(&tp->tun_lock);
    678 				return (EWOULDBLOCK);
    679 			}
    680 			tp->tun_flags |= TUN_RWAIT;
    681 			simple_unlock(&tp->tun_lock);
    682 			if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
    683 				splx(s);
    684 				return (EINTR);
    685 			} else {
    686 				/*
    687 				 * Maybe the interface was destroyed while
    688 				 * we were sleeping, so let's ensure that
    689 				 * we're looking at the same (valid) tun
    690 				 * interface before looping.
    691 				 */
    692 				tp = tun_find_unit(dev);
    693 				if (tp == NULL ||
    694 				    tp->tun_if.if_index != index) {
    695 					splx(s);
    696 					if (tp)
    697 						simple_unlock(&tp->tun_lock);
    698 					return (ENXIO);
    699 				}
    700 			}
    701 		}
    702 	} while (m0 == 0);
    703 	splx(s);
    704 
    705 	while (m0 && uio->uio_resid > 0 && error == 0) {
    706 		len = min(uio->uio_resid, m0->m_len);
    707 		if (len != 0)
    708 			error = uiomove(mtod(m0, caddr_t), len, uio);
    709 		MFREE(m0, m);
    710 		m0 = m;
    711 	}
    712 
    713 	if (m0) {
    714 		TUNDEBUG("Dropping mbuf\n");
    715 		m_freem(m0);
    716 	}
    717 	if (error)
    718 		ifp->if_ierrors++;
    719 	simple_unlock(&tp->tun_lock);
    720 	return (error);
    721 }
    722 
    723 /*
    724  * the cdevsw write interface - an atomic write is a packet - or else!
    725  */
    726 int
    727 tunwrite(dev, uio, ioflag)
    728 	dev_t		dev;
    729 	struct uio	*uio;
    730 	int		ioflag;
    731 {
    732 	struct tun_softc *tp;
    733 	struct ifnet	*ifp;
    734 	struct mbuf	*top, **mp, *m;
    735 	struct ifqueue	*ifq;
    736 	struct sockaddr	dst;
    737 	int		isr, error=0, s, tlen, mlen;
    738 
    739 	tp = tun_find_unit(dev);
    740 
    741 	/* interface was "destroyed" already */
    742 	if (tp == NULL)
    743 		return (ENXIO);
    744 
    745 	ifp = &tp->tun_if;
    746 
    747 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
    748 
    749 	if (tp->tun_flags & TUN_PREPADDR) {
    750 		if (uio->uio_resid < sizeof(dst)) {
    751 			simple_unlock(&tp->tun_lock);
    752 			return (EIO);
    753 		}
    754 		error = uiomove((caddr_t)&dst, sizeof(dst), uio);
    755 		if (dst.sa_len > sizeof(dst)) {
    756 			/* Duh.. */
    757 			char discard;
    758 			int n = dst.sa_len - sizeof(dst);
    759 			while (n--)
    760 				if ((error = uiomove(&discard, 1, uio)) != 0) {
    761 					simple_unlock(&tp->tun_lock);
    762 					return (error);
    763 				}
    764 		}
    765 	} else {
    766 #ifdef INET
    767 		dst.sa_family = AF_INET;
    768 #endif
    769 	}
    770 
    771 	if (uio->uio_resid > TUNMTU) {
    772 		TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
    773 		    (unsigned long)uio->uio_resid);
    774 		simple_unlock(&tp->tun_lock);
    775 		return (EIO);
    776 	}
    777 
    778 	switch (dst.sa_family) {
    779 #ifdef INET
    780 	case AF_INET:
    781 		ifq = &ipintrq;
    782 		isr = NETISR_IP;
    783 		break;
    784 #endif
    785 	default:
    786 		simple_unlock(&tp->tun_lock);
    787 		return (EAFNOSUPPORT);
    788 	}
    789 
    790 	tlen = uio->uio_resid;
    791 
    792 	/* get a header mbuf */
    793 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    794 	if (m == NULL) {
    795 		simple_unlock(&tp->tun_lock);
    796 		return (ENOBUFS);
    797 	}
    798 	mlen = MHLEN;
    799 
    800 	top = 0;
    801 	mp = &top;
    802 	while (error == 0 && uio->uio_resid > 0) {
    803 		m->m_len = min(mlen, uio->uio_resid);
    804 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
    805 		*mp = m;
    806 		mp = &m->m_next;
    807 		if (uio->uio_resid > 0) {
    808 			MGET (m, M_DONTWAIT, MT_DATA);
    809 			if (m == 0) {
    810 				error = ENOBUFS;
    811 				break;
    812 			}
    813 			mlen = MLEN;
    814 		}
    815 	}
    816 	if (error) {
    817 		if (top)
    818 			m_freem (top);
    819 		ifp->if_ierrors++;
    820 		simple_unlock(&tp->tun_lock);
    821 		return (error);
    822 	}
    823 
    824 	top->m_pkthdr.len = tlen;
    825 	top->m_pkthdr.rcvif = ifp;
    826 
    827 #if NBPFILTER > 0
    828 	if (ifp->if_bpf) {
    829 		/*
    830 		 * We need to prepend the address family as
    831 		 * a four byte field.  Cons up a dummy header
    832 		 * to pacify bpf.  This is safe because bpf
    833 		 * will only read from the mbuf (i.e., it won't
    834 		 * try to free it or keep a pointer to it).
    835 		 */
    836 		struct mbuf m;
    837 		u_int32_t af = AF_INET;
    838 
    839 		m.m_flags = 0;
    840 		m.m_next = top;
    841 		m.m_len = sizeof(af);
    842 		m.m_data = (char *)&af;
    843 
    844 		bpf_mtap(ifp->if_bpf, &m);
    845 	}
    846 #endif
    847 
    848 	s = splnet();
    849 	if (IF_QFULL(ifq)) {
    850 		IF_DROP(ifq);
    851 		splx(s);
    852 		ifp->if_collisions++;
    853 		m_freem(top);
    854 		simple_unlock(&tp->tun_lock);
    855 		return (ENOBUFS);
    856 	}
    857 	IF_ENQUEUE(ifq, top);
    858 	splx(s);
    859 	ifp->if_ipackets++;
    860 	ifp->if_ibytes += tlen;
    861 	schednetisr(isr);
    862 	simple_unlock(&tp->tun_lock);
    863 	return (error);
    864 }
    865 
    866 #ifdef ALTQ
    867 /*
    868  * Start packet transmission on the interface.
    869  * when the interface queue is rate-limited by ALTQ or TBR,
    870  * if_start is needed to drain packets from the queue in order
    871  * to notify readers when outgoing packets become ready.
    872  */
    873 static void
    874 tunstart(ifp)
    875 	struct ifnet *ifp;
    876 {
    877 	struct tun_softc *tp = ifp->if_softc;
    878 	struct mbuf *m;
    879 	struct proc	*p;
    880 
    881 	if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
    882 		return;
    883 
    884 	IFQ_POLL(&ifp->if_snd, m);
    885 	if (m != NULL) {
    886 		if (tp->tun_flags & TUN_RWAIT) {
    887 			tp->tun_flags &= ~TUN_RWAIT;
    888 			wakeup((caddr_t)tp);
    889 		}
    890 		if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    891 			if (tp->tun_pgrp > 0)
    892 				gsignal(tp->tun_pgrp, SIGIO);
    893 			else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    894 				psignal(p, SIGIO);
    895 		}
    896 		selwakeup(&tp->tun_rsel);
    897 	}
    898 }
    899 #endif /* ALTQ */
    900 /*
    901  * tunpoll - the poll interface, this is only useful on reads
    902  * really. The write detect always returns true, write never blocks
    903  * anyway, it either accepts the packet or drops it.
    904  */
    905 int
    906 tunpoll(dev, events, l)
    907 	dev_t		dev;
    908 	int		events;
    909 	struct lwp	*l;
    910 {
    911 	struct tun_softc *tp;
    912 	struct ifnet	*ifp;
    913 	int		s, revents = 0;
    914 
    915 	tp = tun_find_unit(dev);
    916 
    917 	/* interface was "destroyed" already */
    918 	if (tp == NULL)
    919 		return (0);
    920 
    921 	ifp = &tp->tun_if;
    922 
    923 	s = splnet();
    924 	TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
    925 
    926 	if (events & (POLLIN | POLLRDNORM)) {
    927 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
    928 			TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
    929 			    ifp->if_snd.ifq_len);
    930 			revents |= events & (POLLIN | POLLRDNORM);
    931 		} else {
    932 			TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
    933 			selrecord(l, &tp->tun_rsel);
    934 		}
    935 	}
    936 
    937 	if (events & (POLLOUT | POLLWRNORM))
    938 		revents |= events & (POLLOUT | POLLWRNORM);
    939 
    940 	splx(s);
    941 	simple_unlock(&tp->tun_lock);
    942 	return (revents);
    943 }
    944 
    945 static void
    946 filt_tunrdetach(struct knote *kn)
    947 {
    948 	struct tun_softc *tp = kn->kn_hook;
    949 	int s;
    950 
    951 	s = splnet();
    952 	SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
    953 	splx(s);
    954 }
    955 
    956 static int
    957 filt_tunread(struct knote *kn, long hint)
    958 {
    959 	struct tun_softc *tp = kn->kn_hook;
    960 	struct ifnet *ifp = &tp->tun_if;
    961 	struct mbuf *m;
    962 	int s;
    963 
    964 	s = splnet();
    965 	IF_POLL(&ifp->if_snd, m);
    966 	if (m == NULL) {
    967 		splx(s);
    968 		return (0);
    969 	}
    970 
    971 	for (kn->kn_data = 0; m != NULL; m = m->m_next)
    972 		kn->kn_data += m->m_len;
    973 
    974 	splx(s);
    975 	return (1);
    976 }
    977 
    978 static const struct filterops tunread_filtops =
    979 	{ 1, NULL, filt_tunrdetach, filt_tunread };
    980 
    981 static const struct filterops tun_seltrue_filtops =
    982 	{ 1, NULL, filt_tunrdetach, filt_seltrue };
    983 
    984 int
    985 tunkqfilter(dev_t dev, struct knote *kn)
    986 {
    987 	struct tun_softc *tp = tun_find_unit(dev);
    988 	struct klist *klist;
    989 	int s;
    990 
    991 	switch (kn->kn_filter) {
    992 	case EVFILT_READ:
    993 		klist = &tp->tun_rsel.sel_klist;
    994 		kn->kn_fop = &tunread_filtops;
    995 		break;
    996 
    997 	case EVFILT_WRITE:
    998 		klist = &tp->tun_rsel.sel_klist;
    999 		kn->kn_fop = &tun_seltrue_filtops;
   1000 		break;
   1001 
   1002 	default:
   1003 		return (1);
   1004 	}
   1005 
   1006 	kn->kn_hook = tp;
   1007 
   1008 	s = splnet();
   1009 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1010 	splx(s);
   1011 
   1012 	return (0);
   1013 }
   1014