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