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