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