if_tun.c revision 131455
1/*	$NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $	*/
2
3/*
4 * Copyright (c) 1988, Julian Onions <jpo@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 it's
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 * $FreeBSD: head/sys/net/if_tun.c 131455 2004-07-02 12:16:02Z mlaier $
17 */
18
19#include "opt_atalk.h"
20#include "opt_inet.h"
21#include "opt_inet6.h"
22#include "opt_ipx.h"
23#include "opt_mac.h"
24
25#include <sys/param.h>
26#include <sys/proc.h>
27#include <sys/systm.h>
28#include <sys/mac.h>
29#include <sys/mbuf.h>
30#include <sys/module.h>
31#include <sys/socket.h>
32#include <sys/filio.h>
33#include <sys/sockio.h>
34#include <sys/ttycom.h>
35#include <sys/poll.h>
36#include <sys/signalvar.h>
37#include <sys/filedesc.h>
38#include <sys/kernel.h>
39#include <sys/sysctl.h>
40#include <sys/conf.h>
41#include <sys/uio.h>
42#include <sys/vnode.h>
43#include <sys/malloc.h>
44#include <sys/random.h>
45
46#include <net/if.h>
47#include <net/if_types.h>
48#include <net/netisr.h>
49#include <net/route.h>
50#ifdef INET
51#include <netinet/in.h>
52#endif
53#include <net/bpf.h>
54#include <net/if_tun.h>
55
56#include <sys/queue.h>
57
58/*
59 * tun_list is protected by global tunmtx.  Other mutable fields are
60 * protected by tun->tun_mtx, or by their owning subsystem.  tun_dev is
61 * static for the duration of a tunnel interface.
62 */
63struct tun_softc {
64	TAILQ_ENTRY(tun_softc)	tun_list;
65	struct cdev *tun_dev;
66	u_short	tun_flags;		/* misc flags */
67#define	TUN_OPEN	0x0001
68#define	TUN_INITED	0x0002
69#define	TUN_RCOLL	0x0004
70#define	TUN_IASET	0x0008
71#define	TUN_DSTADDR	0x0010
72#define	TUN_LMODE	0x0020
73#define	TUN_RWAIT	0x0040
74#define	TUN_ASYNC	0x0080
75#define	TUN_IFHEAD	0x0100
76
77#define TUN_READY       (TUN_OPEN | TUN_INITED)
78
79	/*
80	 * XXXRW: tun_pid is used to exclusively lock /dev/tun.  Is this
81	 * actually needed?  Can we just return EBUSY if already open?
82	 * Problem is that this involved inherent races when a tun device
83	 * is handed off from one process to another, as opposed to just
84	 * being slightly stale informationally.
85	 */
86	pid_t	tun_pid;		/* owning pid */
87	struct	ifnet tun_if;		/* the interface */
88	struct  sigio *tun_sigio;	/* information for async I/O */
89	struct	selinfo	tun_rsel;	/* read select */
90	struct mtx	tun_mtx;	/* protect mutable softc fields */
91};
92
93#define TUNDEBUG	if (tundebug) if_printf
94#define	TUNNAME		"tun"
95
96/*
97 * All mutable global variables in if_tun are locked using tunmtx, with
98 * the exception of tundebug, which is used unlocked, and tunclones,
99 * which is static after setup.
100 */
101static struct mtx tunmtx;
102static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
103static int tundebug = 0;
104static struct clonedevs *tunclones;
105static TAILQ_HEAD(,tun_softc)	tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
106SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
107
108static void	tunclone(void *arg, char *name, int namelen, struct cdev **dev);
109static void	tuncreate(struct cdev *dev);
110static int	tunifioctl(struct ifnet *, u_long, caddr_t);
111static int	tuninit(struct ifnet *);
112static int	tunmodevent(module_t, int, void *);
113static int	tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
114		    struct rtentry *rt);
115static void	tunstart(struct ifnet *);
116
117static d_open_t		tunopen;
118static d_close_t	tunclose;
119static d_read_t		tunread;
120static d_write_t	tunwrite;
121static d_ioctl_t	tunioctl;
122static d_poll_t		tunpoll;
123
124static struct cdevsw tun_cdevsw = {
125	.d_version =	D_VERSION,
126	.d_flags =	D_PSEUDO | D_NEEDGIANT,
127	.d_open =	tunopen,
128	.d_close =	tunclose,
129	.d_read =	tunread,
130	.d_write =	tunwrite,
131	.d_ioctl =	tunioctl,
132	.d_poll =	tunpoll,
133	.d_name =	TUNNAME,
134};
135
136static void
137tunclone(void *arg, char *name, int namelen, struct cdev **dev)
138{
139	int u, i;
140
141	if (*dev != NULL)
142		return;
143
144	if (strcmp(name, TUNNAME) == 0) {
145		u = -1;
146	} else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
147		return;	/* Don't recognise the name */
148	if (u != -1 && u > IF_MAXUNIT)
149		return;	/* Unit number too high */
150
151	/* find any existing device, or allocate new unit number */
152	i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
153	if (i) {
154		/* No preexisting struct cdev *, create one */
155		*dev = make_dev(&tun_cdevsw, unit2minor(u),
156		    UID_UUCP, GID_DIALER, 0600, "tun%d", u);
157		if (*dev != NULL)
158			(*dev)->si_flags |= SI_CHEAPCLONE;
159	}
160}
161
162static void
163tun_destroy(struct tun_softc *tp)
164{
165	struct cdev *dev;
166
167	/* Unlocked read. */
168	KASSERT((tp->tun_flags & TUN_OPEN) == 0,
169	    ("tununits is out of sync - unit %d", tp->tun_if.if_dunit));
170
171	dev = tp->tun_dev;
172	bpfdetach(&tp->tun_if);
173	if_detach(&tp->tun_if);
174	destroy_dev(dev);
175	mtx_destroy(&tp->tun_mtx);
176	free(tp, M_TUN);
177}
178
179static int
180tunmodevent(module_t mod, int type, void *data)
181{
182	static eventhandler_tag tag;
183	struct tun_softc *tp;
184
185	switch (type) {
186	case MOD_LOAD:
187		mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
188		clone_setup(&tunclones);
189		tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
190		if (tag == NULL)
191			return (ENOMEM);
192		break;
193	case MOD_UNLOAD:
194		EVENTHANDLER_DEREGISTER(dev_clone, tag);
195
196		mtx_lock(&tunmtx);
197		while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
198			TAILQ_REMOVE(&tunhead, tp, tun_list);
199			mtx_unlock(&tunmtx);
200			tun_destroy(tp);
201			mtx_lock(&tunmtx);
202		}
203		mtx_unlock(&tunmtx);
204		clone_cleanup(&tunclones);
205		mtx_destroy(&tunmtx);
206		break;
207	}
208	return 0;
209}
210
211static moduledata_t tun_mod = {
212	"if_tun",
213	tunmodevent,
214	0
215};
216
217DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
218
219static void
220tunstart(struct ifnet *ifp)
221{
222	struct tun_softc *tp = ifp->if_softc;
223	struct mbuf *m;
224
225	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
226		IFQ_LOCK(&ifp->if_snd);
227		IFQ_POLL_NOLOCK(&ifp->if_snd, m);
228		if (m == NULL) {
229			IFQ_UNLOCK(&ifp->if_snd);
230			return;
231		}
232		IFQ_UNLOCK(&ifp->if_snd);
233	}
234
235	mtx_lock(&tp->tun_mtx);
236	if (tp->tun_flags & TUN_RWAIT) {
237		tp->tun_flags &= ~TUN_RWAIT;
238		wakeup(tp);
239	}
240	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
241		mtx_unlock(&tp->tun_mtx);
242		pgsigio(&tp->tun_sigio, SIGIO, 0);
243	} else
244		mtx_unlock(&tp->tun_mtx);
245	selwakeuppri(&tp->tun_rsel, PZERO + 1);
246}
247
248static void
249tuncreate(struct cdev *dev)
250{
251	struct tun_softc *sc;
252	struct ifnet *ifp;
253
254	dev->si_flags &= ~SI_CHEAPCLONE;
255
256	MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
257	mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
258	sc->tun_flags = TUN_INITED;
259	sc->tun_dev = dev;
260	mtx_lock(&tunmtx);
261	TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
262	mtx_unlock(&tunmtx);
263
264	ifp = &sc->tun_if;
265	if_initname(ifp, TUNNAME, dev2unit(dev));
266	ifp->if_mtu = TUNMTU;
267	ifp->if_ioctl = tunifioctl;
268	ifp->if_output = tunoutput;
269	ifp->if_start = tunstart;
270	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
271	ifp->if_type = IFT_PPP;
272	ifp->if_softc = sc;
273	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
274	ifp->if_snd.ifq_drv_maxlen = 0;
275	IFQ_SET_READY(&ifp->if_snd);
276
277	if_attach(ifp);
278	bpfattach(ifp, DLT_NULL, sizeof(u_int));
279	dev->si_drv1 = sc;
280}
281
282static int
283tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
284{
285	struct ifnet	*ifp;
286	struct tun_softc *tp;
287
288	/*
289	 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
290	 * synchronization.
291	 */
292	tp = dev->si_drv1;
293	if (!tp) {
294		tuncreate(dev);
295		tp = dev->si_drv1;
296	}
297
298	/*
299	 * XXXRW: This use of tun_pid is subject to error due to the
300	 * fact that a reference to the tunnel can live beyond the
301	 * death of the process that created it.  Can we replace this
302	 * with a simple busy flag?
303	 */
304	mtx_lock(&tp->tun_mtx);
305	if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
306		mtx_unlock(&tp->tun_mtx);
307		return (EBUSY);
308	}
309	tp->tun_pid = td->td_proc->p_pid;
310
311	tp->tun_flags |= TUN_OPEN;
312	mtx_unlock(&tp->tun_mtx);
313	ifp = &tp->tun_if;
314	TUNDEBUG(ifp, "open\n");
315
316	return (0);
317}
318
319/*
320 * tunclose - close the device - mark i/f down & delete
321 * routing info
322 */
323static	int
324tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
325{
326	struct tun_softc *tp;
327	struct ifnet *ifp;
328	int s;
329
330	tp = dev->si_drv1;
331	ifp = &tp->tun_if;
332
333	mtx_lock(&tp->tun_mtx);
334	tp->tun_flags &= ~TUN_OPEN;
335	tp->tun_pid = 0;
336
337	/*
338	 * junk all pending output
339	 */
340	s = splimp();
341	IFQ_PURGE(&ifp->if_snd);
342	splx(s);
343	mtx_unlock(&tp->tun_mtx);
344
345	if (ifp->if_flags & IFF_UP) {
346		s = splimp();
347		if_down(ifp);
348		splx(s);
349	}
350
351	if (ifp->if_flags & IFF_RUNNING) {
352		struct ifaddr *ifa;
353
354		s = splimp();
355		/* find internet addresses and delete routes */
356		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
357			if (ifa->ifa_addr->sa_family == AF_INET)
358				/* Unlocked read. */
359				rtinit(ifa, (int)RTM_DELETE,
360				    tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
361		ifp->if_flags &= ~IFF_RUNNING;
362		splx(s);
363	}
364
365	funsetown(&tp->tun_sigio);
366	selwakeuppri(&tp->tun_rsel, PZERO + 1);
367	TUNDEBUG (ifp, "closed\n");
368	return (0);
369}
370
371static int
372tuninit(struct ifnet *ifp)
373{
374	struct tun_softc *tp = ifp->if_softc;
375	struct ifaddr *ifa;
376	int error = 0;
377
378	TUNDEBUG(ifp, "tuninit\n");
379
380	ifp->if_flags |= IFF_UP | IFF_RUNNING;
381	getmicrotime(&ifp->if_lastchange);
382
383	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
384	     ifa = TAILQ_NEXT(ifa, ifa_link)) {
385		if (ifa->ifa_addr == NULL)
386			error = EFAULT;
387			/* XXX: Should maybe return straight off? */
388		else {
389#ifdef INET
390			if (ifa->ifa_addr->sa_family == AF_INET) {
391			    struct sockaddr_in *si;
392
393			    si = (struct sockaddr_in *)ifa->ifa_addr;
394			    mtx_lock(&tp->tun_mtx);
395			    if (si->sin_addr.s_addr)
396				    tp->tun_flags |= TUN_IASET;
397
398			    si = (struct sockaddr_in *)ifa->ifa_dstaddr;
399			    if (si && si->sin_addr.s_addr)
400				    tp->tun_flags |= TUN_DSTADDR;
401			    mtx_unlock(&tp->tun_mtx);
402			}
403#endif
404		}
405	}
406	return (error);
407}
408
409/*
410 * Process an ioctl request.
411 */
412static int
413tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
414{
415	struct ifreq *ifr = (struct ifreq *)data;
416	struct tun_softc *tp = ifp->if_softc;
417	struct ifstat *ifs;
418	int		error = 0, s;
419
420	s = splimp();
421	switch(cmd) {
422	case SIOCGIFSTATUS:
423		ifs = (struct ifstat *)data;
424		mtx_lock(&tp->tun_mtx);
425		if (tp->tun_pid)
426			sprintf(ifs->ascii + strlen(ifs->ascii),
427			    "\tOpened by PID %d\n", tp->tun_pid);
428		mtx_unlock(&tp->tun_mtx);
429		break;
430	case SIOCSIFADDR:
431		error = tuninit(ifp);
432		TUNDEBUG(ifp, "address set, error=%d\n", error);
433		break;
434	case SIOCSIFDSTADDR:
435		error = tuninit(ifp);
436		TUNDEBUG(ifp, "destination address set, error=%d\n", error);
437		break;
438	case SIOCSIFMTU:
439		ifp->if_mtu = ifr->ifr_mtu;
440		TUNDEBUG(ifp, "mtu set\n");
441		break;
442	case SIOCSIFFLAGS:
443	case SIOCADDMULTI:
444	case SIOCDELMULTI:
445		break;
446	default:
447		error = EINVAL;
448	}
449	splx(s);
450	return (error);
451}
452
453/*
454 * tunoutput - queue packets from higher level ready to put out.
455 */
456static int
457tunoutput(
458	struct ifnet *ifp,
459	struct mbuf *m0,
460	struct sockaddr *dst,
461	struct rtentry *rt)
462{
463	struct tun_softc *tp = ifp->if_softc;
464	u_short cached_tun_flags;
465	int error;
466
467	TUNDEBUG (ifp, "tunoutput\n");
468
469#ifdef MAC
470	error = mac_check_ifnet_transmit(ifp, m0);
471	if (error) {
472		m_freem(m0);
473		return (error);
474	}
475#endif
476
477	/* Could be unlocked read? */
478	mtx_lock(&tp->tun_mtx);
479	cached_tun_flags = tp->tun_flags;
480	mtx_unlock(&tp->tun_mtx);
481	if ((cached_tun_flags & TUN_READY) != TUN_READY) {
482		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
483		m_freem (m0);
484		return (EHOSTDOWN);
485	}
486
487	if ((ifp->if_flags & IFF_UP) != IFF_UP) {
488		m_freem (m0);
489		return (EHOSTDOWN);
490	}
491
492	/* BPF write needs to be handled specially */
493	if (dst->sa_family == AF_UNSPEC) {
494		dst->sa_family = *(mtod(m0, int *));
495		m0->m_len -= sizeof(int);
496		m0->m_pkthdr.len -= sizeof(int);
497		m0->m_data += sizeof(int);
498	}
499
500	if (ifp->if_bpf) {
501		uint32_t af = dst->sa_family;
502		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
503	}
504
505	/* prepend sockaddr? this may abort if the mbuf allocation fails */
506	if (cached_tun_flags & TUN_LMODE) {
507		/* allocate space for sockaddr */
508		M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
509
510		/* if allocation failed drop packet */
511		if (m0 == NULL) {
512			ifp->if_iqdrops++;
513			ifp->if_oerrors++;
514			return (ENOBUFS);
515		} else {
516			bcopy(dst, m0->m_data, dst->sa_len);
517		}
518	}
519
520	if (cached_tun_flags & TUN_IFHEAD) {
521		/* Prepend the address family */
522		M_PREPEND(m0, 4, M_DONTWAIT);
523
524		/* if allocation failed drop packet */
525		if (m0 == NULL) {
526			ifp->if_iqdrops++;
527			ifp->if_oerrors++;
528			return (ENOBUFS);
529		} else
530			*(u_int32_t *)m0->m_data = htonl(dst->sa_family);
531	} else {
532#ifdef INET
533		if (dst->sa_family != AF_INET)
534#endif
535		{
536			m_freem(m0);
537			return (EAFNOSUPPORT);
538		}
539	}
540
541	IFQ_HANDOFF(ifp, m0, error);
542	if (error) {
543		ifp->if_collisions++;
544		return (ENOBUFS);
545	}
546	ifp->if_opackets++;
547	return (0);
548}
549
550/*
551 * the cdevsw interface is now pretty minimal.
552 */
553static	int
554tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
555{
556	int		s;
557	int		error;
558	struct tun_softc *tp = dev->si_drv1;
559	struct tuninfo *tunp;
560
561	switch (cmd) {
562	case TUNSIFINFO:
563		tunp = (struct tuninfo *)data;
564		if (tunp->mtu < IF_MINMTU)
565			return (EINVAL);
566		if (tp->tun_if.if_mtu != tunp->mtu
567		&& (error = suser(td)) != 0)
568			return (error);
569		tp->tun_if.if_mtu = tunp->mtu;
570		tp->tun_if.if_type = tunp->type;
571		tp->tun_if.if_baudrate = tunp->baudrate;
572		break;
573	case TUNGIFINFO:
574		tunp = (struct tuninfo *)data;
575		tunp->mtu = tp->tun_if.if_mtu;
576		tunp->type = tp->tun_if.if_type;
577		tunp->baudrate = tp->tun_if.if_baudrate;
578		break;
579	case TUNSDEBUG:
580		tundebug = *(int *)data;
581		break;
582	case TUNGDEBUG:
583		*(int *)data = tundebug;
584		break;
585	case TUNSLMODE:
586		mtx_lock(&tp->tun_mtx);
587		if (*(int *)data) {
588			tp->tun_flags |= TUN_LMODE;
589			tp->tun_flags &= ~TUN_IFHEAD;
590		} else
591			tp->tun_flags &= ~TUN_LMODE;
592		mtx_unlock(&tp->tun_mtx);
593		break;
594	case TUNSIFHEAD:
595		mtx_lock(&tp->tun_mtx);
596		if (*(int *)data) {
597			tp->tun_flags |= TUN_IFHEAD;
598			tp->tun_flags &= ~TUN_LMODE;
599		} else
600			tp->tun_flags &= ~TUN_IFHEAD;
601		mtx_unlock(&tp->tun_mtx);
602		break;
603	case TUNGIFHEAD:
604		/* Could be unlocked read? */
605		mtx_lock(&tp->tun_mtx);
606		*(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
607		mtx_unlock(&tp->tun_mtx);
608		break;
609	case TUNSIFMODE:
610		/* deny this if UP */
611		if (tp->tun_if.if_flags & IFF_UP)
612			return(EBUSY);
613
614		switch (*(int *)data & ~IFF_MULTICAST) {
615		case IFF_POINTOPOINT:
616		case IFF_BROADCAST:
617			tp->tun_if.if_flags &=
618			    ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
619			tp->tun_if.if_flags |= *(int *)data;
620			break;
621		default:
622			return(EINVAL);
623		}
624		break;
625	case TUNSIFPID:
626		mtx_lock(&tp->tun_mtx);
627		tp->tun_pid = curthread->td_proc->p_pid;
628		mtx_unlock(&tp->tun_mtx);
629		break;
630	case FIONBIO:
631		break;
632	case FIOASYNC:
633		mtx_lock(&tp->tun_mtx);
634		if (*(int *)data)
635			tp->tun_flags |= TUN_ASYNC;
636		else
637			tp->tun_flags &= ~TUN_ASYNC;
638		mtx_unlock(&tp->tun_mtx);
639		break;
640	case FIONREAD:
641		s = splimp();
642		if (!IFQ_IS_EMPTY(&tp->tun_if.if_snd)) {
643			struct mbuf *mb;
644			IFQ_LOCK(&tp->tun_if.if_snd);
645			IFQ_POLL_NOLOCK(&tp->tun_if.if_snd, mb);
646			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
647				*(int *)data += mb->m_len;
648			IFQ_UNLOCK(&tp->tun_if.if_snd);
649		} else
650			*(int *)data = 0;
651		splx(s);
652		break;
653	case FIOSETOWN:
654		return (fsetown(*(int *)data, &tp->tun_sigio));
655
656	case FIOGETOWN:
657		*(int *)data = fgetown(&tp->tun_sigio);
658		return (0);
659
660	/* This is deprecated, FIOSETOWN should be used instead. */
661	case TIOCSPGRP:
662		return (fsetown(-(*(int *)data), &tp->tun_sigio));
663
664	/* This is deprecated, FIOGETOWN should be used instead. */
665	case TIOCGPGRP:
666		*(int *)data = -fgetown(&tp->tun_sigio);
667		return (0);
668
669	default:
670		return (ENOTTY);
671	}
672	return (0);
673}
674
675/*
676 * The cdevsw read interface - reads a packet at a time, or at
677 * least as much of a packet as can be read.
678 */
679static	int
680tunread(struct cdev *dev, struct uio *uio, int flag)
681{
682	struct tun_softc *tp = dev->si_drv1;
683	struct ifnet	*ifp = &tp->tun_if;
684	struct mbuf	*m;
685	int		error=0, len, s;
686
687	TUNDEBUG (ifp, "read\n");
688	mtx_lock(&tp->tun_mtx);
689	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
690		mtx_unlock(&tp->tun_mtx);
691		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
692		return (EHOSTDOWN);
693	}
694
695	tp->tun_flags &= ~TUN_RWAIT;
696	mtx_unlock(&tp->tun_mtx);
697
698	s = splimp();
699	do {
700		IFQ_DEQUEUE(&ifp->if_snd, m);
701		if (m == NULL) {
702			if (flag & IO_NDELAY) {
703				splx(s);
704				return (EWOULDBLOCK);
705			}
706			mtx_lock(&tp->tun_mtx);
707			tp->tun_flags |= TUN_RWAIT;
708			mtx_unlock(&tp->tun_mtx);
709			if((error = tsleep(tp, PCATCH | (PZERO + 1),
710					"tunread", 0)) != 0) {
711				splx(s);
712				return (error);
713			}
714		}
715	} while (m == NULL);
716	splx(s);
717
718	while (m && uio->uio_resid > 0 && error == 0) {
719		len = min(uio->uio_resid, m->m_len);
720		if (len != 0)
721			error = uiomove(mtod(m, void *), len, uio);
722		m = m_free(m);
723	}
724
725	if (m) {
726		TUNDEBUG(ifp, "Dropping mbuf\n");
727		m_freem(m);
728	}
729	return (error);
730}
731
732/*
733 * the cdevsw write interface - an atomic write is a packet - or else!
734 */
735static	int
736tunwrite(struct cdev *dev, struct uio *uio, int flag)
737{
738	struct tun_softc *tp = dev->si_drv1;
739	struct ifnet	*ifp = &tp->tun_if;
740	struct mbuf	*top, **mp, *m;
741	int		error=0, tlen, mlen;
742	uint32_t	family;
743	int 		isr;
744
745	TUNDEBUG(ifp, "tunwrite\n");
746
747	if ((ifp->if_flags & IFF_UP) != IFF_UP)
748		/* ignore silently */
749		return (0);
750
751	if (uio->uio_resid == 0)
752		return (0);
753
754	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
755		TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
756		return (EIO);
757	}
758	tlen = uio->uio_resid;
759
760	/* get a header mbuf */
761	MGETHDR(m, M_DONTWAIT, MT_DATA);
762	if (m == NULL)
763		return (ENOBUFS);
764	mlen = MHLEN;
765
766	top = 0;
767	mp = &top;
768	while (error == 0 && uio->uio_resid > 0) {
769		m->m_len = min(mlen, uio->uio_resid);
770		error = uiomove(mtod(m, void *), m->m_len, uio);
771		*mp = m;
772		mp = &m->m_next;
773		if (uio->uio_resid > 0) {
774			MGET (m, M_DONTWAIT, MT_DATA);
775			if (m == 0) {
776				error = ENOBUFS;
777				break;
778			}
779			mlen = MLEN;
780		}
781	}
782	if (error) {
783		if (top)
784			m_freem (top);
785		ifp->if_ierrors++;
786		return (error);
787	}
788
789	top->m_pkthdr.len = tlen;
790	top->m_pkthdr.rcvif = ifp;
791#ifdef MAC
792	mac_create_mbuf_from_ifnet(ifp, top);
793#endif
794
795	/* Could be unlocked read? */
796	mtx_lock(&tp->tun_mtx);
797	if (tp->tun_flags & TUN_IFHEAD) {
798		mtx_unlock(&tp->tun_mtx);
799		if (top->m_len < sizeof(family) &&
800		    (top = m_pullup(top, sizeof(family))) == NULL)
801			return (ENOBUFS);
802		family = ntohl(*mtod(top, u_int32_t *));
803		m_adj(top, sizeof(family));
804	} else {
805		mtx_unlock(&tp->tun_mtx);
806		family = AF_INET;
807	}
808
809	BPF_MTAP2(ifp, &family, sizeof(family), top);
810
811	switch (family) {
812#ifdef INET
813	case AF_INET:
814		isr = NETISR_IP;
815		break;
816#endif
817#ifdef INET6
818	case AF_INET6:
819		isr = NETISR_IPV6;
820		break;
821#endif
822#ifdef IPX
823	case AF_IPX:
824		isr = NETISR_IPX;
825		break;
826#endif
827#ifdef NETATALK
828	case AF_APPLETALK:
829		isr = NETISR_ATALK2;
830		break;
831#endif
832	default:
833		m_freem(m);
834		return (EAFNOSUPPORT);
835	}
836	/* First chunk of an mbuf contains good junk */
837	if (harvest.point_to_point)
838		random_harvest(m, 16, 3, 0, RANDOM_NET);
839	ifp->if_ibytes += top->m_pkthdr.len;
840	ifp->if_ipackets++;
841	netisr_dispatch(isr, top);
842	return (0);
843}
844
845/*
846 * tunpoll - the poll interface, this is only useful on reads
847 * really. The write detect always returns true, write never blocks
848 * anyway, it either accepts the packet or drops it.
849 */
850static	int
851tunpoll(struct cdev *dev, int events, struct thread *td)
852{
853	int		s;
854	struct tun_softc *tp = dev->si_drv1;
855	struct ifnet	*ifp = &tp->tun_if;
856	int		revents = 0;
857	struct mbuf	*m;
858
859	s = splimp();
860	TUNDEBUG(ifp, "tunpoll\n");
861
862	if (events & (POLLIN | POLLRDNORM)) {
863		IFQ_LOCK(&ifp->if_snd);
864		IFQ_POLL_NOLOCK(&ifp->if_snd, m);
865		if (m != NULL) {
866			TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
867			revents |= events & (POLLIN | POLLRDNORM);
868		} else {
869			TUNDEBUG(ifp, "tunpoll waiting\n");
870			selrecord(td, &tp->tun_rsel);
871		}
872		IFQ_UNLOCK(&ifp->if_snd);
873	}
874	if (events & (POLLOUT | POLLWRNORM))
875		revents |= events & (POLLOUT | POLLWRNORM);
876
877	splx(s);
878	return (revents);
879}
880