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