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