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