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