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