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