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