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