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