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