Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.51.4.1
      1  1.51.4.1   gehenna /*	$NetBSD: if_tun.c,v 1.51.4.1 2002/05/16 03:57:33 gehenna Exp $	*/
      2      1.14       cgd 
      3       1.1       cgd /*
      4       1.8   deraadt  * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
      5       1.8   deraadt  * Nottingham University 1987.
      6       1.1       cgd  *
      7       1.1       cgd  * This source may be freely distributed, however I would be interested
      8       1.1       cgd  * in any changes that are made.
      9       1.6   deraadt  *
     10       1.1       cgd  * This driver takes packets off the IP i/f and hands them up to a
     11      1.21    scottr  * user process to have its wicked way with. This driver has its
     12       1.1       cgd  * roots in a similar driver written by Phil Cockcroft (formerly) at
     13      1.27   mycroft  * UCL. This driver is based much more on read/write/poll mode of
     14       1.1       cgd  * operation though.
     15       1.1       cgd  */
     16      1.48     lukem 
     17      1.48     lukem #include <sys/cdefs.h>
     18  1.51.4.1   gehenna __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.51.4.1 2002/05/16 03:57:33 gehenna Exp $");
     19       1.1       cgd 
     20       1.8   deraadt #include "tun.h"
     21      1.33  jonathan 
     22      1.33  jonathan #include "opt_inet.h"
     23      1.34  jonathan #include "opt_ns.h"
     24       1.8   deraadt 
     25       1.6   deraadt #include <sys/param.h>
     26       1.8   deraadt #include <sys/proc.h>
     27       1.6   deraadt #include <sys/systm.h>
     28       1.6   deraadt #include <sys/mbuf.h>
     29       1.6   deraadt #include <sys/buf.h>
     30       1.6   deraadt #include <sys/protosw.h>
     31       1.6   deraadt #include <sys/socket.h>
     32       1.6   deraadt #include <sys/ioctl.h>
     33       1.6   deraadt #include <sys/errno.h>
     34       1.6   deraadt #include <sys/syslog.h>
     35       1.6   deraadt #include <sys/select.h>
     36      1.27   mycroft #include <sys/poll.h>
     37       1.8   deraadt #include <sys/file.h>
     38      1.22  christos #include <sys/signalvar.h>
     39      1.23  christos #include <sys/conf.h>
     40       1.9   deraadt 
     41       1.9   deraadt #include <machine/cpu.h>
     42       1.6   deraadt 
     43       1.6   deraadt #include <net/if.h>
     44      1.30        is #include <net/if_ether.h>
     45       1.6   deraadt #include <net/netisr.h>
     46       1.6   deraadt #include <net/route.h>
     47       1.1       cgd 
     48      1.30        is 
     49       1.1       cgd #ifdef INET
     50       1.6   deraadt #include <netinet/in.h>
     51       1.6   deraadt #include <netinet/in_systm.h>
     52       1.6   deraadt #include <netinet/in_var.h>
     53       1.6   deraadt #include <netinet/ip.h>
     54      1.30        is #include <netinet/if_inarp.h>
     55       1.1       cgd #endif
     56       1.1       cgd 
     57       1.1       cgd #ifdef NS
     58       1.6   deraadt #include <netns/ns.h>
     59       1.6   deraadt #include <netns/ns_if.h>
     60       1.1       cgd #endif
     61       1.1       cgd 
     62       1.8   deraadt #include "bpfilter.h"
     63       1.8   deraadt #if NBPFILTER > 0
     64       1.8   deraadt #include <sys/time.h>
     65       1.8   deraadt #include <net/bpf.h>
     66       1.8   deraadt #endif
     67       1.8   deraadt 
     68       1.8   deraadt #include <net/if_tun.h>
     69       1.8   deraadt 
     70      1.29  christos #define TUNDEBUG	if (tundebug) printf
     71       1.6   deraadt int	tundebug = 0;
     72       1.1       cgd 
     73       1.6   deraadt extern int ifqmaxlen;
     74      1.22  christos void	tunattach __P((int));
     75      1.46    atatat LIST_HEAD(, tun_softc) tun_softc_list;
     76      1.46    atatat static struct simplelock tun_softc_lock;
     77       1.6   deraadt 
     78      1.22  christos int	tun_ioctl __P((struct ifnet *, u_long, caddr_t));
     79      1.22  christos int	tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
     80      1.22  christos 		       struct rtentry *rt));
     81      1.46    atatat int	tun_clone_create __P((struct if_clone *, int));
     82      1.46    atatat void	tun_clone_destroy __P((struct ifnet *));
     83       1.6   deraadt 
     84      1.46    atatat struct if_clone tun_cloner =
     85      1.46    atatat     IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
     86      1.46    atatat 
     87      1.46    atatat static void tunattach0 __P((struct tun_softc *));
     88      1.26        pk static void tuninit __P((struct tun_softc *));
     89      1.50    itojun #ifdef ALTQ
     90      1.50    itojun static void tunstart __P((struct ifnet *));
     91      1.50    itojun #endif
     92      1.46    atatat static struct tun_softc *tun_find_unit __P((dev_t));
     93  1.51.4.1   gehenna 
     94  1.51.4.1   gehenna dev_type_open(tunopen);
     95  1.51.4.1   gehenna dev_type_close(tunclose);
     96  1.51.4.1   gehenna dev_type_read(tunread);
     97  1.51.4.1   gehenna dev_type_write(tunwrite);
     98  1.51.4.1   gehenna dev_type_ioctl(tunioctl);
     99  1.51.4.1   gehenna dev_type_poll(tunpoll);
    100  1.51.4.1   gehenna 
    101  1.51.4.1   gehenna const struct cdevsw tun_cdevsw = {
    102  1.51.4.1   gehenna 	tunopen, tunclose, tunread, tunwrite, tunioctl,
    103  1.51.4.1   gehenna 	nostop, notty, tunpoll, nommap,
    104  1.51.4.1   gehenna };
    105       1.6   deraadt 
    106       1.8   deraadt void
    107       1.8   deraadt tunattach(unused)
    108       1.8   deraadt 	int unused;
    109       1.8   deraadt {
    110      1.46    atatat 
    111      1.46    atatat 	simple_lock_init(&tun_softc_lock);
    112      1.46    atatat 	LIST_INIT(&tun_softc_list);
    113      1.46    atatat 	if_clone_attach(&tun_cloner);
    114      1.46    atatat }
    115      1.46    atatat 
    116      1.46    atatat int
    117      1.46    atatat tun_clone_create(ifc, unit)
    118      1.46    atatat 	struct if_clone *ifc;
    119      1.46    atatat 	int unit;
    120      1.46    atatat {
    121      1.46    atatat 	struct tun_softc *sc;
    122      1.46    atatat 
    123      1.46    atatat 	sc = malloc(sizeof(struct tun_softc), M_DEVBUF, M_WAITOK);
    124      1.46    atatat 	(void)memset(sc, 0, sizeof(struct tun_softc));
    125      1.46    atatat 
    126      1.46    atatat 	(void)snprintf(sc->tun_if.if_xname, sizeof(sc->tun_if.if_xname),
    127      1.46    atatat 	    "%s%d", ifc->ifc_name, unit);
    128      1.46    atatat 	sc->tun_unit = unit;
    129      1.46    atatat 	simple_lock_init(&sc->tun_lock);
    130      1.46    atatat 
    131      1.46    atatat 	tunattach0(sc);
    132      1.46    atatat 
    133      1.46    atatat 	simple_lock(&tun_softc_lock);
    134      1.46    atatat 	LIST_INSERT_HEAD(&tun_softc_list, sc, tun_list);
    135      1.46    atatat 	simple_unlock(&tun_softc_lock);
    136      1.46    atatat 
    137      1.46    atatat 	return (0);
    138      1.46    atatat }
    139      1.46    atatat 
    140      1.46    atatat void
    141      1.46    atatat tunattach0(sc)
    142      1.46    atatat 	struct tun_softc *sc;
    143      1.46    atatat {
    144      1.46    atatat 	struct ifnet *ifp = (void *)sc;
    145      1.46    atatat 
    146      1.46    atatat 	sc->tun_flags = TUN_INITED;
    147      1.46    atatat 
    148      1.46    atatat 	ifp = &sc->tun_if;
    149      1.46    atatat 	ifp->if_softc = sc;
    150      1.46    atatat 	ifp->if_mtu = TUNMTU;
    151      1.46    atatat 	ifp->if_ioctl = tun_ioctl;
    152      1.46    atatat 	ifp->if_output = tun_output;
    153      1.50    itojun #ifdef ALTQ
    154      1.50    itojun 	ifp->if_start = tunstart;
    155      1.50    itojun #endif
    156      1.46    atatat 	ifp->if_flags = IFF_POINTOPOINT;
    157      1.46    atatat 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
    158      1.46    atatat 	ifp->if_collisions = 0;
    159      1.46    atatat 	ifp->if_ierrors = 0;
    160      1.46    atatat 	ifp->if_oerrors = 0;
    161      1.46    atatat 	ifp->if_ipackets = 0;
    162      1.46    atatat 	ifp->if_opackets = 0;
    163      1.46    atatat 	ifp->if_dlt = DLT_NULL;
    164      1.50    itojun 	IFQ_SET_READY(&ifp->if_snd);
    165      1.46    atatat 	if_attach(ifp);
    166      1.46    atatat 	if_alloc_sadl(ifp);
    167      1.46    atatat #if NBPFILTER > 0
    168      1.46    atatat 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
    169      1.46    atatat #endif
    170      1.46    atatat }
    171      1.46    atatat 
    172      1.46    atatat void
    173      1.46    atatat tun_clone_destroy(ifp)
    174       1.8   deraadt 	struct ifnet *ifp;
    175      1.46    atatat {
    176      1.46    atatat 	struct tun_softc *tp = (void *)ifp;
    177      1.46    atatat 	struct proc *p;
    178       1.8   deraadt 
    179      1.46    atatat 	simple_lock(&tun_softc_lock);
    180      1.46    atatat 	simple_lock(&tp->tun_lock);
    181      1.46    atatat 	LIST_REMOVE(tp, tun_list);
    182      1.46    atatat 	simple_unlock(&tp->tun_lock);
    183      1.46    atatat 	simple_unlock(&tun_softc_lock);
    184      1.46    atatat 
    185      1.46    atatat 	if (tp->tun_flags & TUN_RWAIT) {
    186      1.46    atatat 		tp->tun_flags &= ~TUN_RWAIT;
    187      1.46    atatat 		wakeup((caddr_t)tp);
    188      1.46    atatat 	}
    189      1.46    atatat 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    190      1.46    atatat 		if (tp->tun_pgrp > 0)
    191      1.46    atatat 			gsignal(tp->tun_pgrp, SIGIO);
    192      1.46    atatat 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    193      1.46    atatat 			psignal(p, SIGIO);
    194      1.46    atatat 	}
    195      1.46    atatat 	selwakeup(&tp->tun_rsel);
    196       1.8   deraadt 
    197       1.8   deraadt #if NBPFILTER > 0
    198      1.46    atatat 	bpfdetach(ifp);
    199       1.8   deraadt #endif
    200      1.46    atatat 	if_detach(ifp);
    201      1.46    atatat 
    202      1.46    atatat 	free(tp, M_DEVBUF);
    203      1.46    atatat }
    204      1.46    atatat 
    205      1.46    atatat static struct tun_softc *
    206      1.46    atatat tun_find_unit(dev)
    207      1.46    atatat 	dev_t dev;
    208      1.46    atatat {
    209      1.46    atatat 	struct tun_softc *tp;
    210      1.46    atatat 	int unit = minor(dev);
    211      1.46    atatat 
    212      1.46    atatat 	simple_lock(&tun_softc_lock);
    213      1.46    atatat 	LIST_FOREACH(tp, &tun_softc_list, tun_list)
    214      1.46    atatat 		if (unit == tp->tun_unit)
    215      1.46    atatat 			break;
    216      1.46    atatat 	if (tp)
    217      1.46    atatat 		simple_lock(&tp->tun_lock);
    218      1.46    atatat 	simple_unlock(&tun_softc_lock);
    219      1.46    atatat 
    220      1.46    atatat 	return (tp);
    221       1.8   deraadt }
    222       1.8   deraadt 
    223       1.6   deraadt /*
    224       1.6   deraadt  * tunnel open - must be superuser & the device must be
    225       1.6   deraadt  * configured in
    226       1.6   deraadt  */
    227       1.6   deraadt int
    228       1.6   deraadt tunopen(dev, flag, mode, p)
    229       1.6   deraadt 	dev_t	dev;
    230       1.6   deraadt 	int	flag, mode;
    231       1.6   deraadt 	struct proc *p;
    232       1.6   deraadt {
    233       1.6   deraadt 	struct ifnet	*ifp;
    234       1.8   deraadt 	struct tun_softc *tp;
    235      1.46    atatat 	int	error;
    236       1.1       cgd 
    237      1.22  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    238       1.5   deraadt 		return (error);
    239       1.5   deraadt 
    240      1.46    atatat 	if (NTUN < 1)
    241       1.6   deraadt 		return (ENXIO);
    242      1.46    atatat 
    243      1.46    atatat 	tp = tun_find_unit(dev);
    244      1.46    atatat 
    245      1.46    atatat 	if (!tp)
    246      1.46    atatat 		return (ENXIO);
    247      1.46    atatat 
    248      1.46    atatat 	if (tp->tun_flags & TUN_OPEN) {
    249      1.46    atatat 		simple_unlock(&tp->tun_lock);
    250      1.46    atatat 		return (EBUSY);
    251      1.46    atatat 	}
    252      1.46    atatat 
    253       1.6   deraadt 	ifp = &tp->tun_if;
    254       1.6   deraadt 	tp->tun_flags |= TUN_OPEN;
    255      1.24   thorpej 	TUNDEBUG("%s: open\n", ifp->if_xname);
    256      1.46    atatat 	simple_unlock(&tp->tun_lock);
    257       1.6   deraadt 	return (0);
    258       1.1       cgd }
    259       1.1       cgd 
    260       1.6   deraadt /*
    261       1.6   deraadt  * tunclose - close the device - mark i/f down & delete
    262       1.6   deraadt  * routing info
    263       1.6   deraadt  */
    264       1.6   deraadt int
    265      1.22  christos tunclose(dev, flag, mode, p)
    266       1.6   deraadt 	dev_t	dev;
    267       1.6   deraadt 	int	flag;
    268      1.22  christos 	int	mode;
    269      1.22  christos 	struct proc *p;
    270       1.6   deraadt {
    271      1.46    atatat 	int	s;
    272      1.46    atatat 	struct tun_softc *tp;
    273      1.46    atatat 	struct ifnet	*ifp;
    274       1.6   deraadt 
    275      1.46    atatat 	tp = tun_find_unit(dev);
    276      1.46    atatat 
    277      1.46    atatat 	/* interface was "destroyed" before the close */
    278      1.46    atatat 	if (tp == NULL)
    279      1.46    atatat 		return (0);
    280      1.46    atatat 
    281      1.46    atatat 	ifp = &tp->tun_if;
    282      1.46    atatat 
    283      1.10    andrew 	tp->tun_flags &= ~TUN_OPEN;
    284       1.6   deraadt 
    285       1.6   deraadt 	/*
    286       1.6   deraadt 	 * junk all pending output
    287       1.6   deraadt 	 */
    288      1.50    itojun 	s = splnet();
    289      1.50    itojun 	IFQ_PURGE(&ifp->if_snd);
    290      1.50    itojun 	splx(s);
    291       1.6   deraadt 
    292       1.6   deraadt 	if (ifp->if_flags & IFF_UP) {
    293      1.43   thorpej 		s = splnet();
    294       1.6   deraadt 		if_down(ifp);
    295       1.8   deraadt 		if (ifp->if_flags & IFF_RUNNING) {
    296      1.17   mycroft 			/* find internet addresses and delete routes */
    297      1.39  augustss 			struct ifaddr *ifa;
    298      1.47      matt 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    299      1.38    itojun #ifdef INET
    300      1.18   mycroft 				if (ifa->ifa_addr->sa_family == AF_INET) {
    301      1.18   mycroft 					rtinit(ifa, (int)RTM_DELETE,
    302      1.26        pk 					       tp->tun_flags & TUN_DSTADDR
    303      1.26        pk 							? RTF_HOST
    304      1.26        pk 							: 0);
    305      1.18   mycroft 				}
    306      1.38    itojun #endif
    307      1.11   deraadt 			}
    308       1.8   deraadt 		}
    309       1.6   deraadt 		splx(s);
    310       1.6   deraadt 	}
    311       1.6   deraadt 	tp->tun_pgrp = 0;
    312       1.7   deraadt 	selwakeup(&tp->tun_rsel);
    313       1.6   deraadt 
    314      1.24   thorpej 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
    315      1.46    atatat 	simple_unlock(&tp->tun_lock);
    316       1.6   deraadt 	return (0);
    317       1.1       cgd }
    318       1.1       cgd 
    319      1.26        pk static void
    320      1.24   thorpej tuninit(tp)
    321      1.24   thorpej 	struct tun_softc *tp;
    322       1.6   deraadt {
    323       1.6   deraadt 	struct ifnet	*ifp = &tp->tun_if;
    324      1.46    atatat 	struct ifaddr	*ifa;
    325       1.8   deraadt 
    326      1.24   thorpej 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
    327       1.6   deraadt 
    328       1.6   deraadt 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    329       1.8   deraadt 
    330      1.26        pk 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
    331      1.47      matt 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    332      1.38    itojun #ifdef INET
    333      1.11   deraadt 		if (ifa->ifa_addr->sa_family == AF_INET) {
    334      1.17   mycroft 			struct sockaddr_in *sin;
    335      1.11   deraadt 
    336      1.17   mycroft 			sin = satosin(ifa->ifa_addr);
    337      1.17   mycroft 			if (sin && sin->sin_addr.s_addr)
    338      1.17   mycroft 				tp->tun_flags |= TUN_IASET;
    339      1.17   mycroft 
    340      1.26        pk 			if (ifp->if_flags & IFF_POINTOPOINT) {
    341      1.26        pk 				sin = satosin(ifa->ifa_dstaddr);
    342      1.26        pk 				if (sin && sin->sin_addr.s_addr)
    343      1.26        pk 					tp->tun_flags |= TUN_DSTADDR;
    344      1.26        pk 			}
    345      1.11   deraadt 		}
    346      1.38    itojun #endif
    347      1.18   mycroft 	}
    348       1.8   deraadt 
    349      1.26        pk 	return;
    350       1.1       cgd }
    351       1.1       cgd 
    352       1.1       cgd /*
    353       1.1       cgd  * Process an ioctl request.
    354       1.1       cgd  */
    355       1.1       cgd int
    356      1.22  christos tun_ioctl(ifp, cmd, data)
    357       1.6   deraadt 	struct ifnet *ifp;
    358      1.25   mycroft 	u_long cmd;
    359       1.6   deraadt 	caddr_t	data;
    360       1.6   deraadt {
    361       1.6   deraadt 	int		error = 0, s;
    362      1.46    atatat 	struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
    363      1.46    atatat 
    364      1.46    atatat 	simple_lock(&tp->tun_lock);
    365       1.6   deraadt 
    366      1.43   thorpej 	s = splnet();
    367       1.6   deraadt 	switch(cmd) {
    368       1.6   deraadt 	case SIOCSIFADDR:
    369      1.24   thorpej 		tuninit((struct tun_softc *)(ifp->if_softc));
    370      1.24   thorpej 		TUNDEBUG("%s: address set\n", ifp->if_xname);
    371       1.6   deraadt 		break;
    372       1.6   deraadt 	case SIOCSIFDSTADDR:
    373      1.24   thorpej 		tuninit((struct tun_softc *)(ifp->if_softc));
    374      1.24   thorpej 		TUNDEBUG("%s: destination address set\n", ifp->if_xname);
    375       1.6   deraadt 		break;
    376      1.26        pk 	case SIOCSIFBRDADDR:
    377      1.26        pk 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
    378      1.26        pk 		break;
    379      1.31      matt 	case SIOCSIFMTU: {
    380      1.31      matt 		struct ifreq *ifr = (struct ifreq *) data;
    381      1.31      matt 		if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
    382      1.31      matt 		    error = EINVAL;
    383      1.31      matt 		    break;
    384      1.31      matt 		}
    385      1.31      matt 		TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
    386      1.31      matt 		ifp->if_mtu = ifr->ifr_mtu;
    387      1.32      matt 		break;
    388      1.32      matt 	}
    389      1.32      matt 	case SIOCADDMULTI:
    390      1.32      matt 	case SIOCDELMULTI: {
    391      1.32      matt 		struct ifreq *ifr = (struct ifreq *) data;
    392      1.32      matt 		if (ifr == 0) {
    393      1.32      matt 	        	error = EAFNOSUPPORT;           /* XXX */
    394      1.32      matt 			break;
    395      1.32      matt 		}
    396      1.32      matt 		switch (ifr->ifr_addr.sa_family) {
    397      1.32      matt 
    398      1.32      matt #ifdef INET
    399      1.32      matt 		case AF_INET:
    400      1.32      matt 			break;
    401      1.32      matt #endif
    402      1.32      matt 
    403      1.32      matt 		default:
    404      1.32      matt 			error = EAFNOSUPPORT;
    405      1.32      matt 			break;
    406      1.32      matt 		}
    407      1.31      matt 		break;
    408      1.31      matt 	}
    409      1.38    itojun 	case SIOCSIFFLAGS:
    410      1.38    itojun 		break;
    411       1.6   deraadt 	default:
    412       1.6   deraadt 		error = EINVAL;
    413       1.6   deraadt 	}
    414       1.6   deraadt 	splx(s);
    415      1.46    atatat 	simple_unlock(&tp->tun_lock);
    416       1.6   deraadt 	return (error);
    417       1.1       cgd }
    418       1.1       cgd 
    419       1.1       cgd /*
    420      1.22  christos  * tun_output - queue packets from higher level ready to put out.
    421       1.1       cgd  */
    422       1.6   deraadt int
    423      1.22  christos tun_output(ifp, m0, dst, rt)
    424       1.6   deraadt 	struct ifnet   *ifp;
    425       1.6   deraadt 	struct mbuf    *m0;
    426       1.6   deraadt 	struct sockaddr *dst;
    427      1.12   deraadt 	struct rtentry *rt;
    428       1.6   deraadt {
    429      1.24   thorpej 	struct tun_softc *tp = ifp->if_softc;
    430       1.6   deraadt 	struct proc	*p;
    431      1.38    itojun #ifdef INET
    432       1.6   deraadt 	int		s;
    433      1.51    itojun 	int		error;
    434      1.38    itojun #endif
    435      1.50    itojun 	ALTQ_DECL(struct altq_pktattr pktattr;)
    436       1.6   deraadt 
    437      1.46    atatat 	simple_lock(&tp->tun_lock);
    438      1.24   thorpej 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
    439       1.1       cgd 
    440       1.8   deraadt 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    441      1.24   thorpej 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
    442      1.24   thorpej 			  tp->tun_flags);
    443       1.8   deraadt 		m_freem (m0);
    444      1.46    atatat 		simple_unlock(&tp->tun_lock);
    445      1.26        pk 		return (EHOSTDOWN);
    446       1.8   deraadt 	}
    447       1.8   deraadt 
    448      1.50    itojun 	/*
    449      1.50    itojun 	 * if the queueing discipline needs packet classification,
    450      1.50    itojun 	 * do it before prepending link headers.
    451      1.50    itojun 	 */
    452      1.50    itojun 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
    453      1.50    itojun 
    454       1.8   deraadt #if NBPFILTER > 0
    455      1.40   thorpej 	if (ifp->if_bpf) {
    456       1.8   deraadt 		/*
    457       1.8   deraadt 		 * We need to prepend the address family as
    458       1.8   deraadt 		 * a four byte field.  Cons up a dummy header
    459       1.8   deraadt 		 * to pacify bpf.  This is safe because bpf
    460       1.8   deraadt 		 * will only read from the mbuf (i.e., it won't
    461       1.8   deraadt 		 * try to free it or keep a pointer to it).
    462       1.8   deraadt 		 */
    463       1.8   deraadt 		struct mbuf m;
    464      1.16       cgd 		u_int32_t af = dst->sa_family;
    465       1.8   deraadt 
    466       1.8   deraadt 		m.m_next = m0;
    467      1.16       cgd 		m.m_len = sizeof(af);
    468       1.8   deraadt 		m.m_data = (char *)&af;
    469       1.8   deraadt 
    470      1.40   thorpej 		bpf_mtap(ifp->if_bpf, &m);
    471       1.8   deraadt 	}
    472       1.8   deraadt #endif
    473       1.8   deraadt 
    474       1.6   deraadt 	switch(dst->sa_family) {
    475       1.1       cgd #ifdef INET
    476       1.6   deraadt 	case AF_INET:
    477      1.26        pk 		if (tp->tun_flags & TUN_PREPADDR) {
    478      1.26        pk 			/* Simple link-layer header */
    479      1.26        pk 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
    480      1.26        pk 			if (m0 == NULL) {
    481      1.26        pk 				IF_DROP(&ifp->if_snd);
    482      1.46    atatat 				simple_unlock(&tp->tun_lock);
    483      1.26        pk 				return (ENOBUFS);
    484      1.26        pk 			}
    485      1.26        pk 			bcopy(dst, mtod(m0, char *), dst->sa_len);
    486      1.26        pk 		}
    487      1.36  sommerfe 		/* FALLTHROUGH */
    488      1.36  sommerfe 	case AF_UNSPEC:
    489      1.43   thorpej 		s = splnet();
    490      1.50    itojun 		IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
    491      1.50    itojun 		if (error) {
    492       1.6   deraadt 			splx(s);
    493       1.6   deraadt 			ifp->if_collisions++;
    494      1.50    itojun 			return (error);
    495       1.6   deraadt 		}
    496       1.6   deraadt 		splx(s);
    497       1.6   deraadt 		ifp->if_opackets++;
    498       1.6   deraadt 		break;
    499       1.1       cgd #endif
    500       1.6   deraadt 	default:
    501       1.6   deraadt 		m_freem(m0);
    502      1.46    atatat 		simple_unlock(&tp->tun_lock);
    503      1.26        pk 		return (EAFNOSUPPORT);
    504       1.6   deraadt 	}
    505       1.6   deraadt 
    506       1.6   deraadt 	if (tp->tun_flags & TUN_RWAIT) {
    507       1.6   deraadt 		tp->tun_flags &= ~TUN_RWAIT;
    508       1.6   deraadt 		wakeup((caddr_t)tp);
    509       1.6   deraadt 	}
    510       1.6   deraadt 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    511       1.6   deraadt 		if (tp->tun_pgrp > 0)
    512       1.6   deraadt 			gsignal(tp->tun_pgrp, SIGIO);
    513      1.22  christos 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    514       1.6   deraadt 			psignal(p, SIGIO);
    515       1.6   deraadt 	}
    516       1.6   deraadt 	selwakeup(&tp->tun_rsel);
    517      1.46    atatat 	simple_unlock(&tp->tun_lock);
    518      1.26        pk 	return (0);
    519       1.1       cgd }
    520       1.1       cgd 
    521       1.1       cgd /*
    522       1.1       cgd  * the cdevsw interface is now pretty minimal.
    523       1.1       cgd  */
    524       1.6   deraadt int
    525      1.20   mycroft tunioctl(dev, cmd, data, flag, p)
    526       1.6   deraadt 	dev_t		dev;
    527      1.15       cgd 	u_long		cmd;
    528       1.6   deraadt 	caddr_t		data;
    529       1.6   deraadt 	int		flag;
    530      1.12   deraadt 	struct proc	*p;
    531       1.6   deraadt {
    532      1.46    atatat 	int		s;
    533      1.46    atatat 	struct tun_softc *tp;
    534      1.46    atatat 
    535      1.46    atatat 	tp = tun_find_unit(dev);
    536      1.46    atatat 
    537      1.46    atatat 	/* interface was "destroyed" already */
    538      1.46    atatat 	if (tp == NULL)
    539      1.46    atatat 		return (ENXIO);
    540       1.6   deraadt 
    541       1.6   deraadt 	switch (cmd) {
    542       1.6   deraadt 	case TUNSDEBUG:
    543       1.6   deraadt 		tundebug = *(int *)data;
    544       1.6   deraadt 		break;
    545      1.26        pk 
    546       1.6   deraadt 	case TUNGDEBUG:
    547       1.6   deraadt 		*(int *)data = tundebug;
    548       1.6   deraadt 		break;
    549      1.26        pk 
    550      1.26        pk 	case TUNSIFMODE:
    551      1.31      matt 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
    552      1.26        pk 		case IFF_POINTOPOINT:
    553      1.26        pk 		case IFF_BROADCAST:
    554      1.43   thorpej 			s = splnet();
    555      1.26        pk 			if (tp->tun_if.if_flags & IFF_UP) {
    556      1.26        pk 				splx(s);
    557      1.46    atatat 				simple_unlock(&tp->tun_lock);
    558      1.26        pk 				return (EBUSY);
    559      1.26        pk 			}
    560      1.26        pk 			tp->tun_if.if_flags &=
    561      1.31      matt 				~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
    562      1.26        pk 			tp->tun_if.if_flags |= *(int *)data;
    563      1.26        pk 			splx(s);
    564      1.26        pk 			break;
    565      1.26        pk 		default:
    566      1.46    atatat 			simple_unlock(&tp->tun_lock);
    567      1.26        pk 			return (EINVAL);
    568      1.26        pk 			break;
    569      1.26        pk 		}
    570      1.26        pk 		break;
    571      1.26        pk 
    572      1.26        pk 	case TUNSLMODE:
    573      1.26        pk 		if (*(int *)data)
    574      1.26        pk 			tp->tun_flags |= TUN_PREPADDR;
    575      1.26        pk 		else
    576      1.26        pk 			tp->tun_flags &= ~TUN_PREPADDR;
    577      1.26        pk 		break;
    578      1.26        pk 
    579       1.6   deraadt 	case FIONBIO:
    580       1.6   deraadt 		if (*(int *)data)
    581       1.6   deraadt 			tp->tun_flags |= TUN_NBIO;
    582       1.6   deraadt 		else
    583       1.6   deraadt 			tp->tun_flags &= ~TUN_NBIO;
    584       1.6   deraadt 		break;
    585      1.26        pk 
    586       1.6   deraadt 	case FIOASYNC:
    587       1.6   deraadt 		if (*(int *)data)
    588       1.6   deraadt 			tp->tun_flags |= TUN_ASYNC;
    589       1.6   deraadt 		else
    590       1.6   deraadt 			tp->tun_flags &= ~TUN_ASYNC;
    591       1.6   deraadt 		break;
    592      1.26        pk 
    593       1.6   deraadt 	case FIONREAD:
    594      1.43   thorpej 		s = splnet();
    595       1.6   deraadt 		if (tp->tun_if.if_snd.ifq_head)
    596      1.19        pk 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    597       1.6   deraadt 		else
    598       1.6   deraadt 			*(int *)data = 0;
    599       1.6   deraadt 		splx(s);
    600       1.6   deraadt 		break;
    601      1.26        pk 
    602       1.6   deraadt 	case TIOCSPGRP:
    603       1.6   deraadt 		tp->tun_pgrp = *(int *)data;
    604       1.6   deraadt 		break;
    605      1.26        pk 
    606       1.6   deraadt 	case TIOCGPGRP:
    607       1.6   deraadt 		*(int *)data = tp->tun_pgrp;
    608       1.6   deraadt 		break;
    609      1.26        pk 
    610       1.6   deraadt 	default:
    611      1.46    atatat 		simple_unlock(&tp->tun_lock);
    612       1.6   deraadt 		return (ENOTTY);
    613       1.6   deraadt 	}
    614      1.46    atatat 	simple_unlock(&tp->tun_lock);
    615       1.6   deraadt 	return (0);
    616       1.1       cgd }
    617       1.1       cgd 
    618       1.1       cgd /*
    619       1.6   deraadt  * The cdevsw read interface - reads a packet at a time, or at
    620       1.6   deraadt  * least as much of a packet as can be read.
    621       1.1       cgd  */
    622       1.6   deraadt int
    623      1.22  christos tunread(dev, uio, ioflag)
    624       1.6   deraadt 	dev_t		dev;
    625       1.6   deraadt 	struct uio	*uio;
    626      1.22  christos 	int		ioflag;
    627       1.6   deraadt {
    628      1.46    atatat 	struct tun_softc *tp;
    629      1.46    atatat 	struct ifnet	*ifp;
    630       1.6   deraadt 	struct mbuf	*m, *m0;
    631      1.46    atatat 	int		error=0, len, s, index;
    632      1.46    atatat 
    633      1.46    atatat 	tp = tun_find_unit(dev);
    634      1.46    atatat 
    635      1.46    atatat 	/* interface was "destroyed" already */
    636      1.46    atatat 	if (tp == NULL)
    637      1.46    atatat 		return (ENXIO);
    638      1.46    atatat 
    639      1.46    atatat 	index = tp->tun_if.if_index;
    640      1.46    atatat 	ifp = &tp->tun_if;
    641       1.6   deraadt 
    642      1.24   thorpej 	TUNDEBUG ("%s: read\n", ifp->if_xname);
    643       1.8   deraadt 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    644      1.26        pk 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
    645      1.46    atatat 		simple_unlock(&tp->tun_lock);
    646       1.8   deraadt 		return EHOSTDOWN;
    647       1.8   deraadt 	}
    648       1.8   deraadt 
    649       1.6   deraadt 	tp->tun_flags &= ~TUN_RWAIT;
    650       1.6   deraadt 
    651      1.43   thorpej 	s = splnet();
    652       1.6   deraadt 	do {
    653      1.50    itojun 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    654       1.6   deraadt 		if (m0 == 0) {
    655       1.6   deraadt 			if (tp->tun_flags & TUN_NBIO) {
    656       1.6   deraadt 				splx(s);
    657      1.46    atatat 				simple_unlock(&tp->tun_lock);
    658      1.26        pk 				return (EWOULDBLOCK);
    659       1.6   deraadt 			}
    660       1.6   deraadt 			tp->tun_flags |= TUN_RWAIT;
    661      1.46    atatat 			simple_unlock(&tp->tun_lock);
    662      1.26        pk 			if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
    663      1.26        pk 				splx(s);
    664      1.26        pk 				return (EINTR);
    665      1.46    atatat 			} else {
    666      1.46    atatat 				/*
    667      1.46    atatat 				 * Maybe the interface was destroyed while
    668      1.46    atatat 				 * we were sleeping, so let's ensure that
    669      1.46    atatat 				 * we're looking at the same (valid) tun
    670      1.46    atatat 				 * interface before looping.
    671      1.46    atatat 				 */
    672      1.46    atatat 				tp = tun_find_unit(dev);
    673      1.46    atatat 				if (tp == NULL ||
    674      1.46    atatat 				    tp->tun_if.if_index != index) {
    675      1.46    atatat 					splx(s);
    676      1.46    atatat 					if (tp)
    677      1.46    atatat 						simple_unlock(&tp->tun_lock);
    678      1.46    atatat 					return (ENXIO);
    679      1.46    atatat 				}
    680      1.26        pk 			}
    681       1.6   deraadt 		}
    682       1.6   deraadt 	} while (m0 == 0);
    683       1.6   deraadt 	splx(s);
    684       1.6   deraadt 
    685       1.6   deraadt 	while (m0 && uio->uio_resid > 0 && error == 0) {
    686      1.13   deraadt 		len = min(uio->uio_resid, m0->m_len);
    687      1.45    itojun 		if (len != 0)
    688      1.45    itojun 			error = uiomove(mtod(m0, caddr_t), len, uio);
    689       1.6   deraadt 		MFREE(m0, m);
    690       1.6   deraadt 		m0 = m;
    691       1.6   deraadt 	}
    692       1.6   deraadt 
    693       1.6   deraadt 	if (m0) {
    694       1.6   deraadt 		TUNDEBUG("Dropping mbuf\n");
    695       1.6   deraadt 		m_freem(m0);
    696       1.6   deraadt 	}
    697      1.19        pk 	if (error)
    698      1.19        pk 		ifp->if_ierrors++;
    699      1.46    atatat 	simple_unlock(&tp->tun_lock);
    700      1.26        pk 	return (error);
    701       1.1       cgd }
    702       1.1       cgd 
    703       1.1       cgd /*
    704       1.1       cgd  * the cdevsw write interface - an atomic write is a packet - or else!
    705       1.1       cgd  */
    706       1.6   deraadt int
    707      1.22  christos tunwrite(dev, uio, ioflag)
    708       1.8   deraadt 	dev_t		dev;
    709       1.6   deraadt 	struct uio	*uio;
    710      1.22  christos 	int		ioflag;
    711       1.6   deraadt {
    712      1.46    atatat 	struct tun_softc *tp;
    713      1.46    atatat 	struct ifnet	*ifp;
    714       1.6   deraadt 	struct mbuf	*top, **mp, *m;
    715      1.26        pk 	struct ifqueue	*ifq;
    716      1.26        pk 	struct sockaddr	dst;
    717      1.26        pk 	int		isr, error=0, s, tlen, mlen;
    718       1.6   deraadt 
    719      1.46    atatat 	tp = tun_find_unit(dev);
    720      1.46    atatat 
    721      1.46    atatat 	/* interface was "destroyed" already */
    722      1.46    atatat 	if (tp == NULL)
    723      1.46    atatat 		return (ENXIO);
    724      1.46    atatat 
    725      1.46    atatat 	ifp = &tp->tun_if;
    726      1.46    atatat 
    727      1.24   thorpej 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
    728       1.6   deraadt 
    729      1.26        pk 	if (tp->tun_flags & TUN_PREPADDR) {
    730      1.46    atatat 		if (uio->uio_resid < sizeof(dst)) {
    731      1.46    atatat 			simple_unlock(&tp->tun_lock);
    732      1.26        pk 			return (EIO);
    733      1.46    atatat 		}
    734      1.26        pk 		error = uiomove((caddr_t)&dst, sizeof(dst), uio);
    735      1.26        pk 		if (dst.sa_len > sizeof(dst)) {
    736      1.26        pk 			/* Duh.. */
    737      1.26        pk 			char discard;
    738      1.26        pk 			int n = dst.sa_len - sizeof(dst);
    739      1.26        pk 			while (n--)
    740      1.46    atatat 				if ((error = uiomove(&discard, 1, uio)) != 0) {
    741      1.46    atatat 					simple_unlock(&tp->tun_lock);
    742      1.26        pk 					return (error);
    743      1.46    atatat 				}
    744      1.26        pk 		}
    745      1.26        pk 	} else {
    746      1.26        pk #ifdef INET
    747      1.26        pk 		dst.sa_family = AF_INET;
    748      1.26        pk #endif
    749      1.26        pk 	}
    750      1.26        pk 
    751       1.6   deraadt 	if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
    752      1.37    mjacob 		TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
    753      1.37    mjacob 		    (unsigned long)uio->uio_resid);
    754      1.46    atatat 		simple_unlock(&tp->tun_lock);
    755      1.26        pk 		return (EIO);
    756       1.6   deraadt 	}
    757      1.26        pk 
    758      1.26        pk 	switch (dst.sa_family) {
    759      1.26        pk #ifdef INET
    760      1.26        pk 	case AF_INET:
    761      1.26        pk 		ifq = &ipintrq;
    762      1.26        pk 		isr = NETISR_IP;
    763      1.26        pk 		break;
    764      1.26        pk #endif
    765      1.26        pk 	default:
    766      1.46    atatat 		simple_unlock(&tp->tun_lock);
    767      1.26        pk 		return (EAFNOSUPPORT);
    768      1.26        pk 	}
    769      1.26        pk 
    770       1.8   deraadt 	tlen = uio->uio_resid;
    771       1.8   deraadt 
    772       1.8   deraadt 	/* get a header mbuf */
    773       1.8   deraadt 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    774      1.46    atatat 	if (m == NULL) {
    775      1.46    atatat 		simple_unlock(&tp->tun_lock);
    776      1.26        pk 		return (ENOBUFS);
    777      1.46    atatat 	}
    778       1.8   deraadt 	mlen = MHLEN;
    779       1.8   deraadt 
    780       1.6   deraadt 	top = 0;
    781       1.6   deraadt 	mp = &top;
    782       1.6   deraadt 	while (error == 0 && uio->uio_resid > 0) {
    783      1.13   deraadt 		m->m_len = min(mlen, uio->uio_resid);
    784       1.8   deraadt 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
    785       1.6   deraadt 		*mp = m;
    786       1.6   deraadt 		mp = &m->m_next;
    787       1.8   deraadt 		if (uio->uio_resid > 0) {
    788       1.8   deraadt 			MGET (m, M_DONTWAIT, MT_DATA);
    789       1.8   deraadt 			if (m == 0) {
    790       1.8   deraadt 				error = ENOBUFS;
    791       1.8   deraadt 				break;
    792       1.8   deraadt 			}
    793       1.8   deraadt 			mlen = MLEN;
    794       1.8   deraadt 		}
    795       1.6   deraadt 	}
    796       1.6   deraadt 	if (error) {
    797       1.6   deraadt 		if (top)
    798       1.8   deraadt 			m_freem (top);
    799      1.19        pk 		ifp->if_ierrors++;
    800      1.46    atatat 		simple_unlock(&tp->tun_lock);
    801      1.26        pk 		return (error);
    802       1.6   deraadt 	}
    803       1.6   deraadt 
    804       1.8   deraadt 	top->m_pkthdr.len = tlen;
    805       1.8   deraadt 	top->m_pkthdr.rcvif = ifp;
    806       1.8   deraadt 
    807       1.8   deraadt #if NBPFILTER > 0
    808      1.40   thorpej 	if (ifp->if_bpf) {
    809       1.8   deraadt 		/*
    810       1.8   deraadt 		 * We need to prepend the address family as
    811       1.8   deraadt 		 * a four byte field.  Cons up a dummy header
    812       1.8   deraadt 		 * to pacify bpf.  This is safe because bpf
    813       1.8   deraadt 		 * will only read from the mbuf (i.e., it won't
    814       1.8   deraadt 		 * try to free it or keep a pointer to it).
    815       1.8   deraadt 		 */
    816       1.8   deraadt 		struct mbuf m;
    817      1.16       cgd 		u_int32_t af = AF_INET;
    818       1.8   deraadt 
    819       1.8   deraadt 		m.m_next = top;
    820      1.16       cgd 		m.m_len = sizeof(af);
    821       1.8   deraadt 		m.m_data = (char *)&af;
    822       1.8   deraadt 
    823      1.40   thorpej 		bpf_mtap(ifp->if_bpf, &m);
    824       1.6   deraadt 	}
    825       1.8   deraadt #endif
    826       1.6   deraadt 
    827      1.43   thorpej 	s = splnet();
    828      1.26        pk 	if (IF_QFULL(ifq)) {
    829      1.26        pk 		IF_DROP(ifq);
    830       1.6   deraadt 		splx(s);
    831       1.6   deraadt 		ifp->if_collisions++;
    832       1.6   deraadt 		m_freem(top);
    833      1.46    atatat 		simple_unlock(&tp->tun_lock);
    834      1.26        pk 		return (ENOBUFS);
    835       1.6   deraadt 	}
    836      1.26        pk 	IF_ENQUEUE(ifq, top);
    837       1.6   deraadt 	splx(s);
    838       1.6   deraadt 	ifp->if_ipackets++;
    839      1.26        pk 	schednetisr(isr);
    840      1.46    atatat 	simple_unlock(&tp->tun_lock);
    841      1.26        pk 	return (error);
    842       1.1       cgd }
    843       1.1       cgd 
    844      1.50    itojun #ifdef ALTQ
    845      1.50    itojun /*
    846      1.50    itojun  * Start packet transmission on the interface.
    847      1.50    itojun  * when the interface queue is rate-limited by ALTQ or TBR,
    848      1.50    itojun  * if_start is needed to drain packets from the queue in order
    849      1.50    itojun  * to notify readers when outgoing packets become ready.
    850      1.50    itojun  */
    851      1.50    itojun static void
    852      1.50    itojun tunstart(ifp)
    853      1.50    itojun 	struct ifnet *ifp;
    854      1.50    itojun {
    855      1.50    itojun 	struct tun_softc *tp = ifp->if_softc;
    856      1.50    itojun 	struct mbuf *m;
    857      1.50    itojun 	struct proc	*p;
    858      1.50    itojun 
    859      1.50    itojun 	if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
    860      1.50    itojun 		return;
    861      1.50    itojun 
    862      1.50    itojun 	IFQ_POLL(&ifp->if_snd, m);
    863      1.50    itojun 	if (m != NULL) {
    864      1.50    itojun 		if (tp->tun_flags & TUN_RWAIT) {
    865      1.50    itojun 			tp->tun_flags &= ~TUN_RWAIT;
    866      1.50    itojun 			wakeup((caddr_t)tp);
    867      1.50    itojun 		}
    868      1.50    itojun 		if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    869      1.50    itojun 			if (tp->tun_pgrp > 0)
    870      1.50    itojun 				gsignal(tp->tun_pgrp, SIGIO);
    871      1.50    itojun 			else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    872      1.50    itojun 				psignal(p, SIGIO);
    873      1.50    itojun 		}
    874      1.50    itojun 		selwakeup(&tp->tun_rsel);
    875      1.50    itojun 	}
    876      1.50    itojun }
    877      1.50    itojun #endif /* ALTQ */
    878       1.1       cgd /*
    879      1.27   mycroft  * tunpoll - the poll interface, this is only useful on reads
    880       1.6   deraadt  * really. The write detect always returns true, write never blocks
    881       1.6   deraadt  * anyway, it either accepts the packet or drops it.
    882       1.6   deraadt  */
    883       1.6   deraadt int
    884      1.27   mycroft tunpoll(dev, events, p)
    885       1.6   deraadt 	dev_t		dev;
    886      1.27   mycroft 	int		events;
    887      1.22  christos 	struct proc	*p;
    888       1.6   deraadt {
    889      1.46    atatat 	struct tun_softc *tp;
    890      1.46    atatat 	struct ifnet	*ifp;
    891      1.46    atatat 	int		s, revents = 0;
    892      1.46    atatat 
    893      1.46    atatat 	tp = tun_find_unit(dev);
    894      1.46    atatat 
    895      1.46    atatat 	/* interface was "destroyed" already */
    896      1.46    atatat 	if (tp == NULL)
    897      1.46    atatat 		return (0);
    898      1.46    atatat 
    899      1.46    atatat 	ifp = &tp->tun_if;
    900       1.6   deraadt 
    901      1.43   thorpej 	s = splnet();
    902      1.27   mycroft 	TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
    903       1.6   deraadt 
    904      1.35     veego 	if (events & (POLLIN | POLLRDNORM)) {
    905      1.50    itojun 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
    906      1.27   mycroft 			TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
    907      1.24   thorpej 			    ifp->if_snd.ifq_len);
    908      1.27   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    909      1.27   mycroft 		} else {
    910      1.27   mycroft 			TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
    911      1.27   mycroft 			selrecord(p, &tp->tun_rsel);
    912       1.6   deraadt 		}
    913      1.35     veego 	}
    914      1.27   mycroft 
    915      1.27   mycroft 	if (events & (POLLOUT | POLLWRNORM))
    916      1.27   mycroft 		revents |= events & (POLLOUT | POLLWRNORM);
    917      1.27   mycroft 
    918       1.6   deraadt 	splx(s);
    919      1.46    atatat 	simple_unlock(&tp->tun_lock);
    920      1.27   mycroft 	return (revents);
    921       1.1       cgd }
    922