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