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