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