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