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