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