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