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