Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.78.2.1
      1 /*	$NetBSD: if_tun.c,v 1.78.2.1 2006/02/18 15:39:23 yamt 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.78.2.1 2006/02/18 15:39:23 yamt 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 #ifdef INET
    349 				if (ifa->ifa_addr->sa_family == AF_INET) {
    350 					rtinit(ifa, (int)RTM_DELETE,
    351 					       tp->tun_flags & TUN_DSTADDR
    352 							? RTF_HOST
    353 							: 0);
    354 				}
    355 #endif
    356 			}
    357 		}
    358 	}
    359 	tp->tun_pgid = 0;
    360 	selnotify(&tp->tun_rsel, 0);
    361 
    362 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
    363 	simple_unlock(&tp->tun_lock);
    364 out_nolock:
    365 	splx(s);
    366 	return (0);
    367 }
    368 
    369 /*
    370  * Call at splnet() with tp locked.
    371  */
    372 static void
    373 tuninit(struct tun_softc *tp)
    374 {
    375 	struct ifnet	*ifp = &tp->tun_if;
    376 	struct ifaddr	*ifa;
    377 
    378 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
    379 
    380 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    381 
    382 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
    383 	IFADDR_FOREACH(ifa, ifp) {
    384 #ifdef INET
    385 		if (ifa->ifa_addr->sa_family == AF_INET) {
    386 			struct sockaddr_in *sin;
    387 
    388 			sin = satosin(ifa->ifa_addr);
    389 			if (sin && sin->sin_addr.s_addr)
    390 				tp->tun_flags |= TUN_IASET;
    391 
    392 			if (ifp->if_flags & IFF_POINTOPOINT) {
    393 				sin = satosin(ifa->ifa_dstaddr);
    394 				if (sin && sin->sin_addr.s_addr)
    395 					tp->tun_flags |= TUN_DSTADDR;
    396 			}
    397 		}
    398 #endif
    399 	}
    400 
    401 	return;
    402 }
    403 
    404 /*
    405  * Process an ioctl request.
    406  */
    407 static int
    408 tun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    409 {
    410 	int		error = 0, s;
    411 	struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
    412 
    413 	s = splnet();
    414 	simple_lock(&tp->tun_lock);
    415 
    416 	switch (cmd) {
    417 	case SIOCSIFADDR:
    418 		tuninit(tp);
    419 		TUNDEBUG("%s: address set\n", ifp->if_xname);
    420 		break;
    421 	case SIOCSIFDSTADDR:
    422 		tuninit(tp);
    423 		TUNDEBUG("%s: destination address set\n", ifp->if_xname);
    424 		break;
    425 	case SIOCSIFBRDADDR:
    426 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
    427 		break;
    428 	case SIOCSIFMTU: {
    429 		struct ifreq *ifr = (struct ifreq *) data;
    430 		if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
    431 		    error = EINVAL;
    432 		    break;
    433 		}
    434 		TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
    435 		ifp->if_mtu = ifr->ifr_mtu;
    436 		break;
    437 	}
    438 	case SIOCADDMULTI:
    439 	case SIOCDELMULTI: {
    440 		struct ifreq *ifr = (struct ifreq *) data;
    441 		if (ifr == 0) {
    442 	        	error = EAFNOSUPPORT;           /* XXX */
    443 			break;
    444 		}
    445 		switch (ifr->ifr_addr.sa_family) {
    446 #ifdef INET
    447 		case AF_INET:
    448 			break;
    449 #endif
    450 		default:
    451 			error = EAFNOSUPPORT;
    452 			break;
    453 		}
    454 		break;
    455 	}
    456 	case SIOCSIFFLAGS:
    457 		break;
    458 	default:
    459 		error = EINVAL;
    460 	}
    461 
    462 	simple_unlock(&tp->tun_lock);
    463 	splx(s);
    464 	return (error);
    465 }
    466 
    467 /*
    468  * tun_output - queue packets from higher level ready to put out.
    469  */
    470 static int
    471 tun_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
    472     struct rtentry *rt)
    473 {
    474 	struct tun_softc *tp = ifp->if_softc;
    475 	int		s;
    476 	int		error;
    477 #ifdef INET
    478 	int		mlen;
    479 #endif
    480 	ALTQ_DECL(struct altq_pktattr pktattr;)
    481 
    482 	s = splnet();
    483 	simple_lock(&tp->tun_lock);
    484 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
    485 
    486 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    487 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
    488 			  tp->tun_flags);
    489 		m_freem (m0);
    490 		error = EHOSTDOWN;
    491 		goto out;
    492 	}
    493 
    494 	/*
    495 	 * if the queueing discipline needs packet classification,
    496 	 * do it before prepending link headers.
    497 	 */
    498 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
    499 
    500 #if NBPFILTER > 0
    501 	if (ifp->if_bpf)
    502 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m0);
    503 #endif
    504 
    505 	switch(dst->sa_family) {
    506 #ifdef INET6
    507 	case AF_INET6:
    508 #endif
    509 #ifdef INET
    510 	case AF_INET:
    511 #endif
    512 #if defined(INET) || defined(INET6)
    513 		if (tp->tun_flags & TUN_PREPADDR) {
    514 			/* Simple link-layer header */
    515 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
    516 			if (m0 == NULL) {
    517 				IF_DROP(&ifp->if_snd);
    518 				error = ENOBUFS;
    519 				goto out;
    520 			}
    521 			bcopy(dst, mtod(m0, char *), dst->sa_len);
    522 		}
    523 		/* FALLTHROUGH */
    524 	case AF_UNSPEC:
    525 		IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
    526 		if (error) {
    527 			ifp->if_collisions++;
    528 			error = EAFNOSUPPORT;
    529 			goto out;
    530 		}
    531 		mlen = m0->m_pkthdr.len;
    532 		ifp->if_opackets++;
    533 		ifp->if_obytes += mlen;
    534 		break;
    535 #endif
    536 	default:
    537 		m_freem(m0);
    538 		error = EAFNOSUPPORT;
    539 		goto out;
    540 	}
    541 
    542 	if (tp->tun_flags & TUN_RWAIT) {
    543 		tp->tun_flags &= ~TUN_RWAIT;
    544 		wakeup((caddr_t)tp);
    545 	}
    546 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
    547 		fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
    548 		    NULL);
    549 
    550 	selnotify(&tp->tun_rsel, 0);
    551 out:
    552 	simple_unlock(&tp->tun_lock);
    553 	splx(s);
    554 	return (0);
    555 }
    556 
    557 /*
    558  * the cdevsw interface is now pretty minimal.
    559  */
    560 int
    561 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    562 {
    563 	struct tun_softc *tp;
    564 	int s, error = 0;
    565 
    566 	s = splnet();
    567 	tp = tun_find_unit(dev);
    568 
    569 	/* interface was "destroyed" already */
    570 	if (tp == NULL) {
    571 		error = ENXIO;
    572 		goto out_nolock;
    573 	}
    574 
    575 	switch (cmd) {
    576 	case TUNSDEBUG:
    577 		tundebug = *(int *)data;
    578 		break;
    579 
    580 	case TUNGDEBUG:
    581 		*(int *)data = tundebug;
    582 		break;
    583 
    584 	case TUNSIFMODE:
    585 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
    586 		case IFF_POINTOPOINT:
    587 		case IFF_BROADCAST:
    588 			if (tp->tun_if.if_flags & IFF_UP) {
    589 				error = EBUSY;
    590 				goto out;
    591 			}
    592 			tp->tun_if.if_flags &=
    593 				~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
    594 			tp->tun_if.if_flags |= *(int *)data;
    595 			break;
    596 		default:
    597 			error = EINVAL;
    598 			goto out;
    599 		}
    600 		break;
    601 
    602 	case TUNSLMODE:
    603 		if (*(int *)data)
    604 			tp->tun_flags |= TUN_PREPADDR;
    605 		else
    606 			tp->tun_flags &= ~TUN_PREPADDR;
    607 		break;
    608 
    609 	case FIONBIO:
    610 		if (*(int *)data)
    611 			tp->tun_flags |= TUN_NBIO;
    612 		else
    613 			tp->tun_flags &= ~TUN_NBIO;
    614 		break;
    615 
    616 	case FIOASYNC:
    617 		if (*(int *)data)
    618 			tp->tun_flags |= TUN_ASYNC;
    619 		else
    620 			tp->tun_flags &= ~TUN_ASYNC;
    621 		break;
    622 
    623 	case FIONREAD:
    624 		if (tp->tun_if.if_snd.ifq_head)
    625 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    626 		else
    627 			*(int *)data = 0;
    628 		break;
    629 
    630 	case TIOCSPGRP:
    631 	case FIOSETOWN:
    632 		error = fsetown(l->l_proc, &tp->tun_pgid, cmd, data);
    633 		break;
    634 
    635 	case TIOCGPGRP:
    636 	case FIOGETOWN:
    637 		error = fgetown(l->l_proc, tp->tun_pgid, cmd, data);
    638 		break;
    639 
    640 	default:
    641 		error = ENOTTY;
    642 	}
    643 
    644 out:
    645 	simple_unlock(&tp->tun_lock);
    646 out_nolock:
    647 	splx(s);
    648 	return (error);
    649 }
    650 
    651 /*
    652  * The cdevsw read interface - reads a packet at a time, or at
    653  * least as much of a packet as can be read.
    654  */
    655 int
    656 tunread(dev_t dev, struct uio *uio, int ioflag)
    657 {
    658 	struct tun_softc *tp;
    659 	struct ifnet	*ifp;
    660 	struct mbuf	*m, *m0;
    661 	int		error = 0, len, s, index;
    662 
    663 	s = splnet();
    664 	tp = tun_find_unit(dev);
    665 
    666 	/* interface was "destroyed" already */
    667 	if (tp == NULL) {
    668 		error = ENXIO;
    669 		goto out_nolock;
    670 	}
    671 
    672 	index = tp->tun_if.if_index;
    673 	ifp = &tp->tun_if;
    674 
    675 	TUNDEBUG ("%s: read\n", ifp->if_xname);
    676 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    677 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
    678 		error = EHOSTDOWN;
    679 		goto out;
    680 	}
    681 
    682 	tp->tun_flags &= ~TUN_RWAIT;
    683 
    684 	do {
    685 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    686 		if (m0 == 0) {
    687 			if (tp->tun_flags & TUN_NBIO) {
    688 				error = EWOULDBLOCK;
    689 				goto out;
    690 			}
    691 			tp->tun_flags |= TUN_RWAIT;
    692 			if (ltsleep((caddr_t)tp, PZERO|PCATCH|PNORELOCK,
    693 					"tunread", 0, &tp->tun_lock) != 0) {
    694 				error = EINTR;
    695 				goto out_nolock;
    696 			} else {
    697 				/*
    698 				 * Maybe the interface was destroyed while
    699 				 * we were sleeping, so let's ensure that
    700 				 * we're looking at the same (valid) tun
    701 				 * interface before looping.
    702 				 */
    703 				tp = tun_find_unit(dev);
    704 				if (tp == NULL) {
    705 					error = ENXIO;
    706 					goto out_nolock;
    707 				}
    708 				if (tp->tun_if.if_index != index) {
    709 					error = ENXIO;
    710 					goto out;
    711 				}
    712 			}
    713 		}
    714 	} while (m0 == 0);
    715 
    716 	simple_unlock(&tp->tun_lock);
    717 	splx(s);
    718 
    719 	/* Copy the mbuf chain */
    720 	while (m0 && uio->uio_resid > 0 && error == 0) {
    721 		len = min(uio->uio_resid, m0->m_len);
    722 		if (len != 0)
    723 			error = uiomove(mtod(m0, caddr_t), len, uio);
    724 		MFREE(m0, m);
    725 		m0 = m;
    726 	}
    727 
    728 	if (m0) {
    729 		TUNDEBUG("Dropping mbuf\n");
    730 		m_freem(m0);
    731 	}
    732 	if (error)
    733 		ifp->if_ierrors++;
    734 
    735 	return (error);
    736 
    737 out:
    738 	simple_unlock(&tp->tun_lock);
    739 out_nolock:
    740 	splx(s);
    741 	return (error);
    742 }
    743 
    744 /*
    745  * the cdevsw write interface - an atomic write is a packet - or else!
    746  */
    747 int
    748 tunwrite(dev_t dev, struct uio *uio, int ioflag)
    749 {
    750 	struct tun_softc *tp;
    751 	struct ifnet	*ifp;
    752 	struct mbuf	*top, **mp, *m;
    753 	struct ifqueue	*ifq;
    754 	struct sockaddr	dst;
    755 	int		isr, error = 0, s, tlen, mlen;
    756 
    757 	s = splnet();
    758 	tp = tun_find_unit(dev);
    759 
    760 	/* interface was "destroyed" already */
    761 	if (tp == NULL) {
    762 		error = ENXIO;
    763 		goto out_nolock;
    764 	}
    765 
    766 	/* Unlock until we've got the data */
    767 	simple_unlock(&tp->tun_lock);
    768 	splx(s);
    769 
    770 	ifp = &tp->tun_if;
    771 
    772 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
    773 
    774 	if (tp->tun_flags & TUN_PREPADDR) {
    775 		if (uio->uio_resid < sizeof(dst)) {
    776 			error = EIO;
    777 			goto out0;
    778 		}
    779 		error = uiomove((caddr_t)&dst, sizeof(dst), uio);
    780 		if (dst.sa_len > sizeof(dst)) {
    781 			/* Duh.. */
    782 			char discard;
    783 			int n = dst.sa_len - sizeof(dst);
    784 			while (n--)
    785 				if ((error = uiomove(&discard, 1, uio)) != 0) {
    786 					goto out0;
    787 				}
    788 		}
    789 	} else {
    790 #ifdef INET
    791 		dst.sa_family = AF_INET;
    792 #endif
    793 	}
    794 
    795 	if (uio->uio_resid > TUNMTU) {
    796 		TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
    797 		    (unsigned long)uio->uio_resid);
    798 		error = EIO;
    799 		goto out0;
    800 	}
    801 
    802 	switch (dst.sa_family) {
    803 #ifdef INET
    804 	case AF_INET:
    805 		ifq = &ipintrq;
    806 		isr = NETISR_IP;
    807 		break;
    808 #endif
    809 #ifdef INET6
    810 	case AF_INET6:
    811 		ifq = &ip6intrq;
    812 		isr = NETISR_IPV6;
    813 #endif
    814 	default:
    815 		error = EAFNOSUPPORT;
    816 		goto out0;
    817 	}
    818 
    819 	tlen = uio->uio_resid;
    820 
    821 	/* get a header mbuf */
    822 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    823 	if (m == NULL) {
    824 		error = ENOBUFS;
    825 		goto out0;
    826 	}
    827 	mlen = MHLEN;
    828 
    829 	top = NULL;
    830 	mp = &top;
    831 	while (error == 0 && uio->uio_resid > 0) {
    832 		m->m_len = min(mlen, uio->uio_resid);
    833 		error = uiomove(mtod(m, caddr_t), m->m_len, uio);
    834 		*mp = m;
    835 		mp = &m->m_next;
    836 		if (error == 0 && uio->uio_resid > 0) {
    837 			MGET(m, M_DONTWAIT, MT_DATA);
    838 			if (m == NULL) {
    839 				error = ENOBUFS;
    840 				break;
    841 			}
    842 			mlen = MLEN;
    843 		}
    844 	}
    845 	if (error) {
    846 		if (top != NULL)
    847 			m_freem (top);
    848 		ifp->if_ierrors++;
    849 		goto out0;
    850 	}
    851 
    852 	top->m_pkthdr.len = tlen;
    853 	top->m_pkthdr.rcvif = ifp;
    854 
    855 #if NBPFILTER > 0
    856 	if (ifp->if_bpf)
    857 		bpf_mtap_af(ifp->if_bpf, AF_INET, top);
    858 #endif
    859 
    860 	s = splnet();
    861 	simple_lock(&tp->tun_lock);
    862 	if ((tp->tun_flags & TUN_INITED) == 0) {
    863 		/* Interface was destroyed */
    864 		error = ENXIO;
    865 		goto out;
    866 	}
    867 	if (IF_QFULL(ifq)) {
    868 		IF_DROP(ifq);
    869 		ifp->if_collisions++;
    870 		m_freem(top);
    871 		error = ENOBUFS;
    872 		goto out;
    873 	}
    874 
    875 	IF_ENQUEUE(ifq, top);
    876 	ifp->if_ipackets++;
    877 	ifp->if_ibytes += tlen;
    878 	schednetisr(isr);
    879 out:
    880 	simple_unlock(&tp->tun_lock);
    881 out_nolock:
    882 	splx(s);
    883 out0:
    884 	return (error);
    885 }
    886 
    887 #ifdef ALTQ
    888 /*
    889  * Start packet transmission on the interface.
    890  * when the interface queue is rate-limited by ALTQ or TBR,
    891  * if_start is needed to drain packets from the queue in order
    892  * to notify readers when outgoing packets become ready.
    893  *
    894  * Should be called at splnet.
    895  */
    896 static void
    897 tunstart(struct ifnet *ifp)
    898 {
    899 	struct tun_softc *tp = ifp->if_softc;
    900 
    901 	if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
    902 		return;
    903 
    904 	simple_lock(&tp->tun_lock);
    905 	if (!IF_IS_EMPTY(&ifp->if_snd)) {
    906 		if (tp->tun_flags & TUN_RWAIT) {
    907 			tp->tun_flags &= ~TUN_RWAIT;
    908 			wakeup((caddr_t)tp);
    909 		}
    910 		if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
    911 			fownsignal(tp->tun_pgid, SIGIO, POLL_OUT,
    912 				POLLOUT|POLLWRNORM, NULL);
    913 
    914 		selwakeup(&tp->tun_rsel);
    915 	}
    916 	simple_unlock(&tp->tun_lock);
    917 }
    918 #endif /* ALTQ */
    919 /*
    920  * tunpoll - the poll interface, this is only useful on reads
    921  * really. The write detect always returns true, write never blocks
    922  * anyway, it either accepts the packet or drops it.
    923  */
    924 int
    925 tunpoll(dev_t dev, int events, struct lwp *l)
    926 {
    927 	struct tun_softc *tp;
    928 	struct ifnet	*ifp;
    929 	int		s, revents = 0;
    930 
    931 	s = splnet();
    932 	tp = tun_find_unit(dev);
    933 
    934 	/* interface was "destroyed" already */
    935 	if (tp == NULL)
    936 		goto out_nolock;
    937 
    938 	ifp = &tp->tun_if;
    939 
    940 	TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
    941 
    942 	if (events & (POLLIN | POLLRDNORM)) {
    943 		if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
    944 			TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
    945 			    ifp->if_snd.ifq_len);
    946 			revents |= events & (POLLIN | POLLRDNORM);
    947 		} else {
    948 			TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
    949 			selrecord(l, &tp->tun_rsel);
    950 		}
    951 	}
    952 
    953 	if (events & (POLLOUT | POLLWRNORM))
    954 		revents |= events & (POLLOUT | POLLWRNORM);
    955 
    956 	simple_unlock(&tp->tun_lock);
    957 out_nolock:
    958 	splx(s);
    959 	return (revents);
    960 }
    961 
    962 static void
    963 filt_tunrdetach(struct knote *kn)
    964 {
    965 	struct tun_softc *tp = kn->kn_hook;
    966 	int s;
    967 
    968 	s = splnet();
    969 	SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
    970 	splx(s);
    971 }
    972 
    973 static int
    974 filt_tunread(struct knote *kn, long hint)
    975 {
    976 	struct tun_softc *tp = kn->kn_hook;
    977 	struct ifnet *ifp = &tp->tun_if;
    978 	struct mbuf *m;
    979 	int s;
    980 
    981 	s = splnet();
    982 	IF_POLL(&ifp->if_snd, m);
    983 	if (m == NULL) {
    984 		splx(s);
    985 		return (0);
    986 	}
    987 
    988 	for (kn->kn_data = 0; m != NULL; m = m->m_next)
    989 		kn->kn_data += m->m_len;
    990 
    991 	splx(s);
    992 	return (1);
    993 }
    994 
    995 static const struct filterops tunread_filtops =
    996 	{ 1, NULL, filt_tunrdetach, filt_tunread };
    997 
    998 static const struct filterops tun_seltrue_filtops =
    999 	{ 1, NULL, filt_tunrdetach, filt_seltrue };
   1000 
   1001 int
   1002 tunkqfilter(dev_t dev, struct knote *kn)
   1003 {
   1004 	struct tun_softc *tp;
   1005 	struct klist *klist;
   1006 	int rv = 0, s;
   1007 
   1008 	s = splnet();
   1009 	tp = tun_find_unit(dev);
   1010 	if (tp == NULL)
   1011 		goto out_nolock;
   1012 
   1013 	switch (kn->kn_filter) {
   1014 	case EVFILT_READ:
   1015 		klist = &tp->tun_rsel.sel_klist;
   1016 		kn->kn_fop = &tunread_filtops;
   1017 		break;
   1018 
   1019 	case EVFILT_WRITE:
   1020 		klist = &tp->tun_rsel.sel_klist;
   1021 		kn->kn_fop = &tun_seltrue_filtops;
   1022 		break;
   1023 
   1024 	default:
   1025 		rv = 1;
   1026 		goto out;
   1027 	}
   1028 
   1029 	kn->kn_hook = tp;
   1030 
   1031 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1032 
   1033 out:
   1034 	simple_unlock(&tp->tun_lock);
   1035 out_nolock:
   1036 	splx(s);
   1037 	return (rv);
   1038 }
   1039