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