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