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