Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.52
      1 /*	$NetBSD: if_tun.c,v 1.52 2002/07/29 16:53:30 atatat 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.52 2002/07/29 16:53:30 atatat Exp $");
     19 
     20 #include "tun.h"
     21 
     22 #include "opt_inet.h"
     23 #include "opt_ns.h"
     24 
     25 #include <sys/param.h>
     26 #include <sys/proc.h>
     27 #include <sys/systm.h>
     28 #include <sys/mbuf.h>
     29 #include <sys/buf.h>
     30 #include <sys/protosw.h>
     31 #include <sys/socket.h>
     32 #include <sys/ioctl.h>
     33 #include <sys/errno.h>
     34 #include <sys/syslog.h>
     35 #include <sys/select.h>
     36 #include <sys/poll.h>
     37 #include <sys/file.h>
     38 #include <sys/signalvar.h>
     39 #include <sys/conf.h>
     40 
     41 #include <machine/cpu.h>
     42 
     43 #include <net/if.h>
     44 #include <net/if_ether.h>
     45 #include <net/netisr.h>
     46 #include <net/route.h>
     47 
     48 
     49 #ifdef INET
     50 #include <netinet/in.h>
     51 #include <netinet/in_systm.h>
     52 #include <netinet/in_var.h>
     53 #include <netinet/ip.h>
     54 #include <netinet/if_inarp.h>
     55 #endif
     56 
     57 #ifdef NS
     58 #include <netns/ns.h>
     59 #include <netns/ns_if.h>
     60 #endif
     61 
     62 #include "bpfilter.h"
     63 #if NBPFILTER > 0
     64 #include <sys/time.h>
     65 #include <net/bpf.h>
     66 #endif
     67 
     68 #include <net/if_tun.h>
     69 
     70 #define TUNDEBUG	if (tundebug) printf
     71 int	tundebug = 0;
     72 
     73 extern int ifqmaxlen;
     74 void	tunattach __P((int));
     75 LIST_HEAD(, tun_softc) tun_softc_list;
     76 static struct simplelock tun_softc_lock;
     77 
     78 int	tun_ioctl __P((struct ifnet *, u_long, caddr_t));
     79 int	tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
     80 		       struct rtentry *rt));
     81 int	tun_clone_create __P((struct if_clone *, int));
     82 void	tun_clone_destroy __P((struct ifnet *));
     83 
     84 struct if_clone tun_cloner =
     85     IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
     86 
     87 static void tunattach0 __P((struct tun_softc *));
     88 static void tuninit __P((struct tun_softc *));
     89 #ifdef ALTQ
     90 static void tunstart __P((struct ifnet *));
     91 #endif
     92 static struct tun_softc *tun_find_unit __P((dev_t));
     93 
     94 void
     95 tunattach(unused)
     96 	int unused;
     97 {
     98 
     99 	simple_lock_init(&tun_softc_lock);
    100 	LIST_INIT(&tun_softc_list);
    101 	if_clone_attach(&tun_cloner);
    102 }
    103 
    104 int
    105 tun_clone_create(ifc, unit)
    106 	struct if_clone *ifc;
    107 	int unit;
    108 {
    109 	struct tun_softc *sc;
    110 
    111 	sc = malloc(sizeof(struct tun_softc), M_DEVBUF, M_WAITOK);
    112 	(void)memset(sc, 0, sizeof(struct tun_softc));
    113 
    114 	(void)snprintf(sc->tun_if.if_xname, sizeof(sc->tun_if.if_xname),
    115 	    "%s%d", ifc->ifc_name, unit);
    116 	sc->tun_unit = unit;
    117 	simple_lock_init(&sc->tun_lock);
    118 
    119 	tunattach0(sc);
    120 
    121 	simple_lock(&tun_softc_lock);
    122 	LIST_INSERT_HEAD(&tun_softc_list, sc, tun_list);
    123 	simple_unlock(&tun_softc_lock);
    124 
    125 	return (0);
    126 }
    127 
    128 void
    129 tunattach0(sc)
    130 	struct tun_softc *sc;
    131 {
    132 	struct ifnet *ifp = (void *)sc;
    133 
    134 	sc->tun_flags = TUN_INITED;
    135 
    136 	ifp = &sc->tun_if;
    137 	ifp->if_softc = sc;
    138 	ifp->if_mtu = TUNMTU;
    139 	ifp->if_ioctl = tun_ioctl;
    140 	ifp->if_output = tun_output;
    141 #ifdef ALTQ
    142 	ifp->if_start = tunstart;
    143 #endif
    144 	ifp->if_flags = IFF_POINTOPOINT;
    145 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
    146 	ifp->if_collisions = 0;
    147 	ifp->if_ierrors = 0;
    148 	ifp->if_oerrors = 0;
    149 	ifp->if_ipackets = 0;
    150 	ifp->if_opackets = 0;
    151 	ifp->if_dlt = DLT_NULL;
    152 	IFQ_SET_READY(&ifp->if_snd);
    153 	if_attach(ifp);
    154 	if_alloc_sadl(ifp);
    155 #if NBPFILTER > 0
    156 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
    157 #endif
    158 }
    159 
    160 void
    161 tun_clone_destroy(ifp)
    162 	struct ifnet *ifp;
    163 {
    164 	struct tun_softc *tp = (void *)ifp;
    165 	struct proc *p;
    166 
    167 	simple_lock(&tun_softc_lock);
    168 	simple_lock(&tp->tun_lock);
    169 	LIST_REMOVE(tp, tun_list);
    170 	simple_unlock(&tp->tun_lock);
    171 	simple_unlock(&tun_softc_lock);
    172 
    173 	if (tp->tun_flags & TUN_RWAIT) {
    174 		tp->tun_flags &= ~TUN_RWAIT;
    175 		wakeup((caddr_t)tp);
    176 	}
    177 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    178 		if (tp->tun_pgrp > 0)
    179 			gsignal(tp->tun_pgrp, SIGIO);
    180 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    181 			psignal(p, SIGIO);
    182 	}
    183 	selwakeup(&tp->tun_rsel);
    184 
    185 #if NBPFILTER > 0
    186 	bpfdetach(ifp);
    187 #endif
    188 	if_detach(ifp);
    189 
    190 	free(tp, M_DEVBUF);
    191 }
    192 
    193 static struct tun_softc *
    194 tun_find_unit(dev)
    195 	dev_t dev;
    196 {
    197 	struct tun_softc *tp;
    198 	int unit = minor(dev);
    199 
    200 	simple_lock(&tun_softc_lock);
    201 	LIST_FOREACH(tp, &tun_softc_list, tun_list)
    202 		if (unit == tp->tun_unit)
    203 			break;
    204 	if (tp)
    205 		simple_lock(&tp->tun_lock);
    206 	simple_unlock(&tun_softc_lock);
    207 
    208 	return (tp);
    209 }
    210 
    211 /*
    212  * tunnel open - must be superuser & the device must be
    213  * configured in
    214  */
    215 int
    216 tunopen(dev, flag, mode, p)
    217 	dev_t	dev;
    218 	int	flag, mode;
    219 	struct proc *p;
    220 {
    221 	struct ifnet	*ifp;
    222 	struct tun_softc *tp;
    223 	int	error;
    224 
    225 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    226 		return (error);
    227 
    228 	if (NTUN < 1)
    229 		return (ENXIO);
    230 
    231 	tp = tun_find_unit(dev);
    232 
    233 	if (!tp) {
    234 		(void)tun_clone_create(&tun_cloner, minor(dev));
    235 		tp = tun_find_unit(dev);
    236 	}
    237 
    238 	if (!tp)
    239 		return (ENXIO);
    240 
    241 	if (tp->tun_flags & TUN_OPEN) {
    242 		simple_unlock(&tp->tun_lock);
    243 		return (EBUSY);
    244 	}
    245 
    246 	ifp = &tp->tun_if;
    247 	tp->tun_flags |= TUN_OPEN;
    248 	TUNDEBUG("%s: open\n", ifp->if_xname);
    249 	simple_unlock(&tp->tun_lock);
    250 	return (0);
    251 }
    252 
    253 /*
    254  * tunclose - close the device - mark i/f down & delete
    255  * routing info
    256  */
    257 int
    258 tunclose(dev, flag, mode, p)
    259 	dev_t	dev;
    260 	int	flag;
    261 	int	mode;
    262 	struct proc *p;
    263 {
    264 	int	s;
    265 	struct tun_softc *tp;
    266 	struct ifnet	*ifp;
    267 
    268 	tp = tun_find_unit(dev);
    269 
    270 	/* interface was "destroyed" before the close */
    271 	if (tp == NULL)
    272 		return (0);
    273 
    274 	ifp = &tp->tun_if;
    275 
    276 	tp->tun_flags &= ~TUN_OPEN;
    277 
    278 	/*
    279 	 * junk all pending output
    280 	 */
    281 	s = splnet();
    282 	IFQ_PURGE(&ifp->if_snd);
    283 	splx(s);
    284 
    285 	if (ifp->if_flags & IFF_UP) {
    286 		s = splnet();
    287 		if_down(ifp);
    288 		if (ifp->if_flags & IFF_RUNNING) {
    289 			/* find internet addresses and delete routes */
    290 			struct ifaddr *ifa;
    291 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    292 #ifdef INET
    293 				if (ifa->ifa_addr->sa_family == AF_INET) {
    294 					rtinit(ifa, (int)RTM_DELETE,
    295 					       tp->tun_flags & TUN_DSTADDR
    296 							? RTF_HOST
    297 							: 0);
    298 				}
    299 #endif
    300 			}
    301 		}
    302 		splx(s);
    303 	}
    304 	tp->tun_pgrp = 0;
    305 	selwakeup(&tp->tun_rsel);
    306 
    307 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
    308 	simple_unlock(&tp->tun_lock);
    309 	return (0);
    310 }
    311 
    312 static void
    313 tuninit(tp)
    314 	struct tun_softc *tp;
    315 {
    316 	struct ifnet	*ifp = &tp->tun_if;
    317 	struct ifaddr	*ifa;
    318 
    319 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
    320 
    321 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    322 
    323 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
    324 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    325 #ifdef INET
    326 		if (ifa->ifa_addr->sa_family == AF_INET) {
    327 			struct sockaddr_in *sin;
    328 
    329 			sin = satosin(ifa->ifa_addr);
    330 			if (sin && sin->sin_addr.s_addr)
    331 				tp->tun_flags |= TUN_IASET;
    332 
    333 			if (ifp->if_flags & IFF_POINTOPOINT) {
    334 				sin = satosin(ifa->ifa_dstaddr);
    335 				if (sin && sin->sin_addr.s_addr)
    336 					tp->tun_flags |= TUN_DSTADDR;
    337 			}
    338 		}
    339 #endif
    340 	}
    341 
    342 	return;
    343 }
    344 
    345 /*
    346  * Process an ioctl request.
    347  */
    348 int
    349 tun_ioctl(ifp, cmd, data)
    350 	struct ifnet *ifp;
    351 	u_long cmd;
    352 	caddr_t	data;
    353 {
    354 	int		error = 0, s;
    355 	struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
    356 
    357 	simple_lock(&tp->tun_lock);
    358 
    359 	s = splnet();
    360 	switch(cmd) {
    361 	case SIOCSIFADDR:
    362 		tuninit((struct tun_softc *)(ifp->if_softc));
    363 		TUNDEBUG("%s: address set\n", ifp->if_xname);
    364 		break;
    365 	case SIOCSIFDSTADDR:
    366 		tuninit((struct tun_softc *)(ifp->if_softc));
    367 		TUNDEBUG("%s: destination address set\n", ifp->if_xname);
    368 		break;
    369 	case SIOCSIFBRDADDR:
    370 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
    371 		break;
    372 	case SIOCSIFMTU: {
    373 		struct ifreq *ifr = (struct ifreq *) data;
    374 		if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
    375 		    error = EINVAL;
    376 		    break;
    377 		}
    378 		TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
    379 		ifp->if_mtu = ifr->ifr_mtu;
    380 		break;
    381 	}
    382 	case SIOCADDMULTI:
    383 	case SIOCDELMULTI: {
    384 		struct ifreq *ifr = (struct ifreq *) data;
    385 		if (ifr == 0) {
    386 	        	error = EAFNOSUPPORT;           /* XXX */
    387 			break;
    388 		}
    389 		switch (ifr->ifr_addr.sa_family) {
    390 
    391 #ifdef INET
    392 		case AF_INET:
    393 			break;
    394 #endif
    395 
    396 		default:
    397 			error = EAFNOSUPPORT;
    398 			break;
    399 		}
    400 		break;
    401 	}
    402 	case SIOCSIFFLAGS:
    403 		break;
    404 	default:
    405 		error = EINVAL;
    406 	}
    407 	splx(s);
    408 	simple_unlock(&tp->tun_lock);
    409 	return (error);
    410 }
    411 
    412 /*
    413  * tun_output - queue packets from higher level ready to put out.
    414  */
    415 int
    416 tun_output(ifp, m0, dst, rt)
    417 	struct ifnet   *ifp;
    418 	struct mbuf    *m0;
    419 	struct sockaddr *dst;
    420 	struct rtentry *rt;
    421 {
    422 	struct tun_softc *tp = ifp->if_softc;
    423 	struct proc	*p;
    424 #ifdef INET
    425 	int		s;
    426 	int		error;
    427 #endif
    428 	ALTQ_DECL(struct altq_pktattr pktattr;)
    429 
    430 	simple_lock(&tp->tun_lock);
    431 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
    432 
    433 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    434 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
    435 			  tp->tun_flags);
    436 		m_freem (m0);
    437 		simple_unlock(&tp->tun_lock);
    438 		return (EHOSTDOWN);
    439 	}
    440 
    441 	/*
    442 	 * if the queueing discipline needs packet classification,
    443 	 * do it before prepending link headers.
    444 	 */
    445 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
    446 
    447 #if NBPFILTER > 0
    448 	if (ifp->if_bpf) {
    449 		/*
    450 		 * We need to prepend the address family as
    451 		 * a four byte field.  Cons up a dummy header
    452 		 * to pacify bpf.  This is safe because bpf
    453 		 * will only read from the mbuf (i.e., it won't
    454 		 * try to free it or keep a pointer to it).
    455 		 */
    456 		struct mbuf m;
    457 		u_int32_t af = dst->sa_family;
    458 
    459 		m.m_next = m0;
    460 		m.m_len = sizeof(af);
    461 		m.m_data = (char *)&af;
    462 
    463 		bpf_mtap(ifp->if_bpf, &m);
    464 	}
    465 #endif
    466 
    467 	switch(dst->sa_family) {
    468 #ifdef INET
    469 	case AF_INET:
    470 		if (tp->tun_flags & TUN_PREPADDR) {
    471 			/* Simple link-layer header */
    472 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
    473 			if (m0 == NULL) {
    474 				IF_DROP(&ifp->if_snd);
    475 				simple_unlock(&tp->tun_lock);
    476 				return (ENOBUFS);
    477 			}
    478 			bcopy(dst, mtod(m0, char *), dst->sa_len);
    479 		}
    480 		/* FALLTHROUGH */
    481 	case AF_UNSPEC:
    482 		s = splnet();
    483 		IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
    484 		if (error) {
    485 			splx(s);
    486 			ifp->if_collisions++;
    487 			return (error);
    488 		}
    489 		splx(s);
    490 		ifp->if_opackets++;
    491 		break;
    492 #endif
    493 	default:
    494 		m_freem(m0);
    495 		simple_unlock(&tp->tun_lock);
    496 		return (EAFNOSUPPORT);
    497 	}
    498 
    499 	if (tp->tun_flags & TUN_RWAIT) {
    500 		tp->tun_flags &= ~TUN_RWAIT;
    501 		wakeup((caddr_t)tp);
    502 	}
    503 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    504 		if (tp->tun_pgrp > 0)
    505 			gsignal(tp->tun_pgrp, SIGIO);
    506 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    507 			psignal(p, SIGIO);
    508 	}
    509 	selwakeup(&tp->tun_rsel);
    510 	simple_unlock(&tp->tun_lock);
    511 	return (0);
    512 }
    513 
    514 /*
    515  * the cdevsw interface is now pretty minimal.
    516  */
    517 int
    518 tunioctl(dev, cmd, data, flag, p)
    519 	dev_t		dev;
    520 	u_long		cmd;
    521 	caddr_t		data;
    522 	int		flag;
    523 	struct proc	*p;
    524 {
    525 	int		s;
    526 	struct tun_softc *tp;
    527 
    528 	tp = tun_find_unit(dev);
    529 
    530 	/* interface was "destroyed" already */
    531 	if (tp == NULL)
    532 		return (ENXIO);
    533 
    534 	switch (cmd) {
    535 	case TUNSDEBUG:
    536 		tundebug = *(int *)data;
    537 		break;
    538 
    539 	case TUNGDEBUG:
    540 		*(int *)data = tundebug;
    541 		break;
    542 
    543 	case TUNSIFMODE:
    544 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
    545 		case IFF_POINTOPOINT:
    546 		case IFF_BROADCAST:
    547 			s = splnet();
    548 			if (tp->tun_if.if_flags & IFF_UP) {
    549 				splx(s);
    550 				simple_unlock(&tp->tun_lock);
    551 				return (EBUSY);
    552 			}
    553 			tp->tun_if.if_flags &=
    554 				~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
    555 			tp->tun_if.if_flags |= *(int *)data;
    556 			splx(s);
    557 			break;
    558 		default:
    559 			simple_unlock(&tp->tun_lock);
    560 			return (EINVAL);
    561 			break;
    562 		}
    563 		break;
    564 
    565 	case TUNSLMODE:
    566 		if (*(int *)data)
    567 			tp->tun_flags |= TUN_PREPADDR;
    568 		else
    569 			tp->tun_flags &= ~TUN_PREPADDR;
    570 		break;
    571 
    572 	case FIONBIO:
    573 		if (*(int *)data)
    574 			tp->tun_flags |= TUN_NBIO;
    575 		else
    576 			tp->tun_flags &= ~TUN_NBIO;
    577 		break;
    578 
    579 	case FIOASYNC:
    580 		if (*(int *)data)
    581 			tp->tun_flags |= TUN_ASYNC;
    582 		else
    583 			tp->tun_flags &= ~TUN_ASYNC;
    584 		break;
    585 
    586 	case FIONREAD:
    587 		s = splnet();
    588 		if (tp->tun_if.if_snd.ifq_head)
    589 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    590 		else
    591 			*(int *)data = 0;
    592 		splx(s);
    593 		break;
    594 
    595 	case TIOCSPGRP:
    596 		tp->tun_pgrp = *(int *)data;
    597 		break;
    598 
    599 	case TIOCGPGRP:
    600 		*(int *)data = tp->tun_pgrp;
    601 		break;
    602 
    603 	default:
    604 		simple_unlock(&tp->tun_lock);
    605 		return (ENOTTY);
    606 	}
    607 	simple_unlock(&tp->tun_lock);
    608 	return (0);
    609 }
    610 
    611 /*
    612  * The cdevsw read interface - reads a packet at a time, or at
    613  * least as much of a packet as can be read.
    614  */
    615 int
    616 tunread(dev, uio, ioflag)
    617 	dev_t		dev;
    618 	struct uio	*uio;
    619 	int		ioflag;
    620 {
    621 	struct tun_softc *tp;
    622 	struct ifnet	*ifp;
    623 	struct mbuf	*m, *m0;
    624 	int		error=0, len, s, index;
    625 
    626 	tp = tun_find_unit(dev);
    627 
    628 	/* interface was "destroyed" already */
    629 	if (tp == NULL)
    630 		return (ENXIO);
    631 
    632 	index = tp->tun_if.if_index;
    633 	ifp = &tp->tun_if;
    634 
    635 	TUNDEBUG ("%s: read\n", ifp->if_xname);
    636 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    637 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
    638 		simple_unlock(&tp->tun_lock);
    639 		return EHOSTDOWN;
    640 	}
    641 
    642 	tp->tun_flags &= ~TUN_RWAIT;
    643 
    644 	s = splnet();
    645 	do {
    646 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    647 		if (m0 == 0) {
    648 			if (tp->tun_flags & TUN_NBIO) {
    649 				splx(s);
    650 				simple_unlock(&tp->tun_lock);
    651 				return (EWOULDBLOCK);
    652 			}
    653 			tp->tun_flags |= TUN_RWAIT;
    654 			simple_unlock(&tp->tun_lock);
    655 			if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
    656 				splx(s);
    657 				return (EINTR);
    658 			} else {
    659 				/*
    660 				 * Maybe the interface was destroyed while
    661 				 * we were sleeping, so let's ensure that
    662 				 * we're looking at the same (valid) tun
    663 				 * interface before looping.
    664 				 */
    665 				tp = tun_find_unit(dev);
    666 				if (tp == NULL ||
    667 				    tp->tun_if.if_index != index) {
    668 					splx(s);
    669 					if (tp)
    670 						simple_unlock(&tp->tun_lock);
    671 					return (ENXIO);
    672 				}
    673 			}
    674 		}
    675 	} while (m0 == 0);
    676 	splx(s);
    677 
    678 	while (m0 && uio->uio_resid > 0 && error == 0) {
    679 		len = min(uio->uio_resid, m0->m_len);
    680 		if (len != 0)
    681 			error = uiomove(mtod(m0, caddr_t), len, uio);
    682 		MFREE(m0, m);
    683 		m0 = m;
    684 	}
    685 
    686 	if (m0) {
    687 		TUNDEBUG("Dropping mbuf\n");
    688 		m_freem(m0);
    689 	}
    690 	if (error)
    691 		ifp->if_ierrors++;
    692 	simple_unlock(&tp->tun_lock);
    693 	return (error);
    694 }
    695 
    696 /*
    697  * the cdevsw write interface - an atomic write is a packet - or else!
    698  */
    699 int
    700 tunwrite(dev, uio, ioflag)
    701 	dev_t		dev;
    702 	struct uio	*uio;
    703 	int		ioflag;
    704 {
    705 	struct tun_softc *tp;
    706 	struct ifnet	*ifp;
    707 	struct mbuf	*top, **mp, *m;
    708 	struct ifqueue	*ifq;
    709 	struct sockaddr	dst;
    710 	int		isr, error=0, s, tlen, mlen;
    711 
    712 	tp = tun_find_unit(dev);
    713 
    714 	/* interface was "destroyed" already */
    715 	if (tp == NULL)
    716 		return (ENXIO);
    717 
    718 	ifp = &tp->tun_if;
    719 
    720 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
    721 
    722 	if (tp->tun_flags & TUN_PREPADDR) {
    723 		if (uio->uio_resid < sizeof(dst)) {
    724 			simple_unlock(&tp->tun_lock);
    725 			return (EIO);
    726 		}
    727 		error = uiomove((caddr_t)&dst, sizeof(dst), uio);
    728 		if (dst.sa_len > sizeof(dst)) {
    729 			/* Duh.. */
    730 			char discard;
    731 			int n = dst.sa_len - sizeof(dst);
    732 			while (n--)
    733 				if ((error = uiomove(&discard, 1, uio)) != 0) {
    734 					simple_unlock(&tp->tun_lock);
    735 					return (error);
    736 				}
    737 		}
    738 	} else {
    739 #ifdef INET
    740 		dst.sa_family = AF_INET;
    741 #endif
    742 	}
    743 
    744 	if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
    745 		TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
    746 		    (unsigned long)uio->uio_resid);
    747 		simple_unlock(&tp->tun_lock);
    748 		return (EIO);
    749 	}
    750 
    751 	switch (dst.sa_family) {
    752 #ifdef INET
    753 	case AF_INET:
    754 		ifq = &ipintrq;
    755 		isr = NETISR_IP;
    756 		break;
    757 #endif
    758 	default:
    759 		simple_unlock(&tp->tun_lock);
    760 		return (EAFNOSUPPORT);
    761 	}
    762 
    763 	tlen = uio->uio_resid;
    764 
    765 	/* get a header mbuf */
    766 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    767 	if (m == NULL) {
    768 		simple_unlock(&tp->tun_lock);
    769 		return (ENOBUFS);
    770 	}
    771 	mlen = MHLEN;
    772 
    773 	top = 0;
    774 	mp = &top;
    775 	while (error == 0 && uio->uio_resid > 0) {
    776 		m->m_len = min(mlen, uio->uio_resid);
    777 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
    778 		*mp = m;
    779 		mp = &m->m_next;
    780 		if (uio->uio_resid > 0) {
    781 			MGET (m, M_DONTWAIT, MT_DATA);
    782 			if (m == 0) {
    783 				error = ENOBUFS;
    784 				break;
    785 			}
    786 			mlen = MLEN;
    787 		}
    788 	}
    789 	if (error) {
    790 		if (top)
    791 			m_freem (top);
    792 		ifp->if_ierrors++;
    793 		simple_unlock(&tp->tun_lock);
    794 		return (error);
    795 	}
    796 
    797 	top->m_pkthdr.len = tlen;
    798 	top->m_pkthdr.rcvif = ifp;
    799 
    800 #if NBPFILTER > 0
    801 	if (ifp->if_bpf) {
    802 		/*
    803 		 * We need to prepend the address family as
    804 		 * a four byte field.  Cons up a dummy header
    805 		 * to pacify bpf.  This is safe because bpf
    806 		 * will only read from the mbuf (i.e., it won't
    807 		 * try to free it or keep a pointer to it).
    808 		 */
    809 		struct mbuf m;
    810 		u_int32_t af = AF_INET;
    811 
    812 		m.m_next = top;
    813 		m.m_len = sizeof(af);
    814 		m.m_data = (char *)&af;
    815 
    816 		bpf_mtap(ifp->if_bpf, &m);
    817 	}
    818 #endif
    819 
    820 	s = splnet();
    821 	if (IF_QFULL(ifq)) {
    822 		IF_DROP(ifq);
    823 		splx(s);
    824 		ifp->if_collisions++;
    825 		m_freem(top);
    826 		simple_unlock(&tp->tun_lock);
    827 		return (ENOBUFS);
    828 	}
    829 	IF_ENQUEUE(ifq, top);
    830 	splx(s);
    831 	ifp->if_ipackets++;
    832 	schednetisr(isr);
    833 	simple_unlock(&tp->tun_lock);
    834 	return (error);
    835 }
    836 
    837 #ifdef ALTQ
    838 /*
    839  * Start packet transmission on the interface.
    840  * when the interface queue is rate-limited by ALTQ or TBR,
    841  * if_start is needed to drain packets from the queue in order
    842  * to notify readers when outgoing packets become ready.
    843  */
    844 static void
    845 tunstart(ifp)
    846 	struct ifnet *ifp;
    847 {
    848 	struct tun_softc *tp = ifp->if_softc;
    849 	struct mbuf *m;
    850 	struct proc	*p;
    851 
    852 	if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
    853 		return;
    854 
    855 	IFQ_POLL(&ifp->if_snd, m);
    856 	if (m != NULL) {
    857 		if (tp->tun_flags & TUN_RWAIT) {
    858 			tp->tun_flags &= ~TUN_RWAIT;
    859 			wakeup((caddr_t)tp);
    860 		}
    861 		if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    862 			if (tp->tun_pgrp > 0)
    863 				gsignal(tp->tun_pgrp, SIGIO);
    864 			else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    865 				psignal(p, SIGIO);
    866 		}
    867 		selwakeup(&tp->tun_rsel);
    868 	}
    869 }
    870 #endif /* ALTQ */
    871 /*
    872  * tunpoll - the poll interface, this is only useful on reads
    873  * really. The write detect always returns true, write never blocks
    874  * anyway, it either accepts the packet or drops it.
    875  */
    876 int
    877 tunpoll(dev, events, p)
    878 	dev_t		dev;
    879 	int		events;
    880 	struct proc	*p;
    881 {
    882 	struct tun_softc *tp;
    883 	struct ifnet	*ifp;
    884 	int		s, revents = 0;
    885 
    886 	tp = tun_find_unit(dev);
    887 
    888 	/* interface was "destroyed" already */
    889 	if (tp == NULL)
    890 		return (0);
    891 
    892 	ifp = &tp->tun_if;
    893 
    894 	s = splnet();
    895 	TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
    896 
    897 	if (events & (POLLIN | POLLRDNORM)) {
    898 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
    899 			TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
    900 			    ifp->if_snd.ifq_len);
    901 			revents |= events & (POLLIN | POLLRDNORM);
    902 		} else {
    903 			TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
    904 			selrecord(p, &tp->tun_rsel);
    905 		}
    906 	}
    907 
    908 	if (events & (POLLOUT | POLLWRNORM))
    909 		revents |= events & (POLLOUT | POLLWRNORM);
    910 
    911 	splx(s);
    912 	simple_unlock(&tp->tun_lock);
    913 	return (revents);
    914 }
    915