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