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