if_tun.c revision 123922
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 123922 2003-12-28 03:56:00Z sam $
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 <machine/bus.h>	/* XXX Shouldn't really be required ! */
45#include <sys/random.h>
46#include <sys/rman.h>
47
48#include <net/if.h>
49#include <net/if_types.h>
50#include <net/netisr.h>
51#include <net/route.h>
52#ifdef INET
53#include <netinet/in.h>
54#endif
55#include <net/bpf.h>
56#include <net/if_tunvar.h>
57#include <net/if_tun.h>
58
59#define TUNDEBUG	if (tundebug) if_printf
60#define	TUNNAME		"tun"
61
62static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
63static int tundebug = 0;
64static struct tun_softc *tunhead = NULL;
65static struct rman tununits;
66static udev_t tunbasedev = NOUDEV;
67SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
68
69static void	tunclone(void *arg, char *name, int namelen, dev_t *dev);
70static void	tuncreate(dev_t dev);
71static int	tunifioctl(struct ifnet *, u_long, caddr_t);
72static int	tuninit(struct ifnet *);
73static int	tunmodevent(module_t, int, void *);
74static int	tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
75		    struct rtentry *rt);
76static void	tunstart(struct ifnet *);
77
78static d_open_t		tunopen;
79static d_close_t	tunclose;
80static d_read_t		tunread;
81static d_write_t	tunwrite;
82static d_ioctl_t	tunioctl;
83static d_poll_t		tunpoll;
84
85#define CDEV_MAJOR 52
86static struct cdevsw tun_cdevsw = {
87	.d_open =	tunopen,
88	.d_close =	tunclose,
89	.d_read =	tunread,
90	.d_write =	tunwrite,
91	.d_ioctl =	tunioctl,
92	.d_poll =	tunpoll,
93	.d_name =	TUNNAME,
94	.d_maj =	CDEV_MAJOR,
95};
96
97static void
98tunclone(void *arg, char *name, int namelen, dev_t *dev)
99{
100	struct resource *r;
101	int err;
102	int u;
103
104	if (*dev != NODEV)
105		return;
106
107	if (strcmp(name, TUNNAME) == 0) {
108		r = rman_reserve_resource(&tununits, 0, IF_MAXUNIT, 1,
109		    RF_ALLOCATED | RF_ACTIVE, NULL);
110		u = rman_get_start(r);
111		err = rman_release_resource(r);
112		KASSERT(err == 0, ("Unexpected failure releasing resource"));
113		*dev = makedev(CDEV_MAJOR, unit2minor(u));
114		if ((*dev)->si_flags & SI_NAMED)
115			return;	/* Already make_dev()d */
116	} else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
117		return;	/* Don't recognise the name */
118
119	*dev = make_dev(&tun_cdevsw, unit2minor(u),
120	    UID_ROOT, GID_WHEEL, 0600, "tun%d", u);
121
122	/*
123	 * All devices depend on tunbasedev so that we can simply
124	 * destroy_dev() this device at module unload time to get
125	 * rid of all our make_dev()d resources.
126	 */
127	if (tunbasedev == NOUDEV)
128		tunbasedev = (*dev)->si_udev;
129	else {
130		(*dev)->si_flags |= SI_CHEAPCLONE;
131		dev_depends(udev2dev(tunbasedev, 0), *dev);
132	}
133}
134
135static int
136tunmodevent(module_t mod, int type, void *data)
137{
138	static eventhandler_tag tag;
139	struct tun_softc *tp;
140	dev_t dev;
141	int err;
142
143	switch (type) {
144	case MOD_LOAD:
145		tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
146		if (tag == NULL)
147			return (ENOMEM);
148		tununits.rm_type = RMAN_ARRAY;
149		tununits.rm_descr = "open if_tun units";
150		err = rman_init(&tununits);
151		if (err != 0) {
152			EVENTHANDLER_DEREGISTER(dev_clone, tag);
153			return (err);
154		}
155		err = rman_manage_region(&tununits, 0, IF_MAXUNIT);
156		if (err != 0) {
157			printf("%s: tununits: rman_manage_region: Failed %d\n",
158			    TUNNAME, err);
159			rman_fini(&tununits);
160			EVENTHANDLER_DEREGISTER(dev_clone, tag);
161			return (err);
162		}
163		break;
164	case MOD_UNLOAD:
165		err = rman_fini(&tununits);
166		if (err != 0)
167			return (err);
168		EVENTHANDLER_DEREGISTER(dev_clone, tag);
169
170		while (tunhead != NULL) {
171			KASSERT((tunhead->tun_flags & TUN_OPEN) == 0,
172			    ("tununits is out of sync - unit %d",
173			    tunhead->tun_if.if_dunit));
174			tp = tunhead;
175			dev = makedev(tun_cdevsw.d_maj,
176			    unit2minor(tp->tun_if.if_dunit));
177			KASSERT(dev->si_drv1 == tp, ("Bad makedev result"));
178			tunhead = tp->next;
179			bpfdetach(&tp->tun_if);
180			if_detach(&tp->tun_if);
181			KASSERT(dev->si_flags & SI_NAMED, ("Missing make_dev"));
182			free(tp, M_TUN);
183		}
184
185		/*
186		 * Destroying tunbasedev results in all of our make_dev()s
187		 * conveniently going away.
188		 */
189		if (tunbasedev != NOUDEV)
190			destroy_dev(udev2dev(tunbasedev, 0));
191
192		break;
193	}
194	return 0;
195}
196
197static moduledata_t tun_mod = {
198	"if_tun",
199	tunmodevent,
200	0
201};
202
203DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
204
205static void
206tunstart(struct ifnet *ifp)
207{
208	struct tun_softc *tp = ifp->if_softc;
209
210	if (tp->tun_flags & TUN_RWAIT) {
211		tp->tun_flags &= ~TUN_RWAIT;
212		wakeup(tp);
213	}
214	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
215		pgsigio(&tp->tun_sigio, SIGIO, 0);
216	selwakeuppri(&tp->tun_rsel, PZERO + 1);
217}
218
219static void
220tuncreate(dev_t dev)
221{
222	struct tun_softc *sc;
223	struct ifnet *ifp;
224
225	if (!(dev->si_flags & SI_NAMED))
226		dev = make_dev(&tun_cdevsw, minor(dev),
227		    UID_UUCP, GID_DIALER, 0600, "tun%d", dev2unit(dev));
228
229	MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
230	sc->tun_flags = TUN_INITED;
231	sc->next = tunhead;
232	tunhead = sc;
233
234	ifp = &sc->tun_if;
235	if_initname(ifp, TUNNAME, dev2unit(dev));
236	ifp->if_mtu = TUNMTU;
237	ifp->if_ioctl = tunifioctl;
238	ifp->if_output = tunoutput;
239	ifp->if_start = tunstart;
240	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
241	ifp->if_type = IFT_PPP;
242	ifp->if_snd.ifq_maxlen = ifqmaxlen;
243	ifp->if_softc = sc;
244	if_attach(ifp);
245	bpfattach(ifp, DLT_NULL, sizeof(u_int));
246	dev->si_drv1 = sc;
247}
248
249static int
250tunopen(dev_t dev, int flag, int mode, struct thread *td)
251{
252	struct resource *r;
253	struct ifnet	*ifp;
254	struct tun_softc *tp;
255	int unit;
256
257	unit = dev2unit(dev);
258	if (unit > IF_MAXUNIT)
259		return (ENXIO);
260
261	r = rman_reserve_resource(&tununits, unit, unit, 1,
262	    RF_ALLOCATED | RF_ACTIVE, NULL);
263	if (r == NULL)
264		return (EBUSY);
265
266	dev->si_flags &= ~SI_CHEAPCLONE;
267
268	tp = dev->si_drv1;
269	if (!tp) {
270		tuncreate(dev);
271		tp = dev->si_drv1;
272	}
273	KASSERT(!(tp->tun_flags & TUN_OPEN), ("Resource & flags out-of-sync"));
274	tp->tun_unit = r;
275	tp->tun_pid = td->td_proc->p_pid;
276	ifp = &tp->tun_if;
277	tp->tun_flags |= TUN_OPEN;
278	TUNDEBUG(ifp, "open\n");
279
280	return (0);
281}
282
283/*
284 * tunclose - close the device - mark i/f down & delete
285 * routing info
286 */
287static	int
288tunclose(dev_t dev, int foo, int bar, struct thread *td)
289{
290	struct tun_softc *tp;
291	struct ifnet *ifp;
292	int s;
293	int err;
294
295	tp = dev->si_drv1;
296	ifp = &tp->tun_if;
297
298	KASSERT(tp->tun_unit, ("Unit %d not marked open", tp->tun_if.if_dunit));
299	tp->tun_flags &= ~TUN_OPEN;
300	tp->tun_pid = 0;
301
302	/*
303	 * junk all pending output
304	 */
305	IF_DRAIN(&ifp->if_snd);
306
307	if (ifp->if_flags & IFF_UP) {
308		s = splimp();
309		if_down(ifp);
310		splx(s);
311	}
312
313	if (ifp->if_flags & IFF_RUNNING) {
314		struct ifaddr *ifa;
315
316		s = splimp();
317		/* find internet addresses and delete routes */
318		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
319			if (ifa->ifa_addr->sa_family == AF_INET)
320				rtinit(ifa, (int)RTM_DELETE,
321				    tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
322		ifp->if_flags &= ~IFF_RUNNING;
323		splx(s);
324	}
325
326	funsetown(&tp->tun_sigio);
327	selwakeuppri(&tp->tun_rsel, PZERO + 1);
328
329	TUNDEBUG (ifp, "closed\n");
330	err = rman_release_resource(tp->tun_unit);
331	KASSERT(err == 0, ("Unit %d failed to release", tp->tun_if.if_dunit));
332
333	return (0);
334}
335
336static int
337tuninit(struct ifnet *ifp)
338{
339	struct tun_softc *tp = ifp->if_softc;
340	struct ifaddr *ifa;
341	int error = 0;
342
343	TUNDEBUG(ifp, "tuninit\n");
344
345	ifp->if_flags |= IFF_UP | IFF_RUNNING;
346	getmicrotime(&ifp->if_lastchange);
347
348	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
349	     ifa = TAILQ_NEXT(ifa, ifa_link)) {
350		if (ifa->ifa_addr == NULL)
351			error = EFAULT;
352			/* XXX: Should maybe return straight off? */
353		else {
354#ifdef INET
355			if (ifa->ifa_addr->sa_family == AF_INET) {
356			    struct sockaddr_in *si;
357
358			    si = (struct sockaddr_in *)ifa->ifa_addr;
359			    if (si->sin_addr.s_addr)
360				    tp->tun_flags |= TUN_IASET;
361
362			    si = (struct sockaddr_in *)ifa->ifa_dstaddr;
363			    if (si && si->sin_addr.s_addr)
364				    tp->tun_flags |= TUN_DSTADDR;
365			}
366#endif
367		}
368	}
369	return (error);
370}
371
372/*
373 * Process an ioctl request.
374 */
375static int
376tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
377{
378	struct ifreq *ifr = (struct ifreq *)data;
379	struct tun_softc *tp = ifp->if_softc;
380	struct ifstat *ifs;
381	int		error = 0, s;
382
383	s = splimp();
384	switch(cmd) {
385	case SIOCGIFSTATUS:
386		ifs = (struct ifstat *)data;
387		if (tp->tun_pid)
388			sprintf(ifs->ascii + strlen(ifs->ascii),
389			    "\tOpened by PID %d\n", tp->tun_pid);
390		break;
391	case SIOCSIFADDR:
392		error = tuninit(ifp);
393		TUNDEBUG(ifp, "address set, error=%d\n", error);
394		break;
395	case SIOCSIFDSTADDR:
396		error = tuninit(ifp);
397		TUNDEBUG(ifp, "destination address set, error=%d\n", error);
398		break;
399	case SIOCSIFMTU:
400		ifp->if_mtu = ifr->ifr_mtu;
401		TUNDEBUG(ifp, "mtu set\n");
402		break;
403	case SIOCSIFFLAGS:
404	case SIOCADDMULTI:
405	case SIOCDELMULTI:
406		break;
407	default:
408		error = EINVAL;
409	}
410	splx(s);
411	return (error);
412}
413
414/*
415 * tunoutput - queue packets from higher level ready to put out.
416 */
417static int
418tunoutput(
419	struct ifnet *ifp,
420	struct mbuf *m0,
421	struct sockaddr *dst,
422	struct rtentry *rt)
423{
424	struct tun_softc *tp = ifp->if_softc;
425#ifdef MAC
426	int error;
427#endif
428
429	TUNDEBUG (ifp, "tunoutput\n");
430
431#ifdef MAC
432	error = mac_check_ifnet_transmit(ifp, m0);
433	if (error) {
434		m_freem(m0);
435		return (error);
436	}
437#endif
438
439	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
440		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
441		m_freem (m0);
442		return (EHOSTDOWN);
443	}
444
445	if ((ifp->if_flags & IFF_UP) != IFF_UP) {
446		m_freem (m0);
447		return (EHOSTDOWN);
448	}
449
450	/* BPF write needs to be handled specially */
451	if (dst->sa_family == AF_UNSPEC) {
452		dst->sa_family = *(mtod(m0, int *));
453		m0->m_len -= sizeof(int);
454		m0->m_pkthdr.len -= sizeof(int);
455		m0->m_data += sizeof(int);
456	}
457
458	if (ifp->if_bpf) {
459		uint32_t af = dst->sa_family;
460		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
461	}
462
463	/* prepend sockaddr? this may abort if the mbuf allocation fails */
464	if (tp->tun_flags & TUN_LMODE) {
465		/* allocate space for sockaddr */
466		M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
467
468		/* if allocation failed drop packet */
469		if (m0 == NULL) {
470			ifp->if_iqdrops++;
471			ifp->if_oerrors++;
472			return (ENOBUFS);
473		} else {
474			bcopy(dst, m0->m_data, dst->sa_len);
475		}
476	}
477
478	if (tp->tun_flags & TUN_IFHEAD) {
479		/* Prepend the address family */
480		M_PREPEND(m0, 4, M_DONTWAIT);
481
482		/* if allocation failed drop packet */
483		if (m0 == NULL) {
484			ifp->if_iqdrops++;
485			ifp->if_oerrors++;
486			return (ENOBUFS);
487		} else
488			*(u_int32_t *)m0->m_data = htonl(dst->sa_family);
489	} else {
490#ifdef INET
491		if (dst->sa_family != AF_INET)
492#endif
493		{
494			m_freem(m0);
495			return (EAFNOSUPPORT);
496		}
497	}
498
499	if (! IF_HANDOFF(&ifp->if_snd, m0, ifp)) {
500		ifp->if_collisions++;
501		return (ENOBUFS);
502	}
503	ifp->if_opackets++;
504	return (0);
505}
506
507/*
508 * the cdevsw interface is now pretty minimal.
509 */
510static	int
511tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
512{
513	int		s;
514	int		error;
515	struct tun_softc *tp = dev->si_drv1;
516	struct tuninfo *tunp;
517
518	switch (cmd) {
519	case TUNSIFINFO:
520		tunp = (struct tuninfo *)data;
521		if (tunp->mtu < IF_MINMTU)
522			return (EINVAL);
523		if (tp->tun_if.if_mtu != tunp->mtu
524		&& (error = suser(td)) != 0)
525			return (error);
526		tp->tun_if.if_mtu = tunp->mtu;
527		tp->tun_if.if_type = tunp->type;
528		tp->tun_if.if_baudrate = tunp->baudrate;
529		break;
530	case TUNGIFINFO:
531		tunp = (struct tuninfo *)data;
532		tunp->mtu = tp->tun_if.if_mtu;
533		tunp->type = tp->tun_if.if_type;
534		tunp->baudrate = tp->tun_if.if_baudrate;
535		break;
536	case TUNSDEBUG:
537		tundebug = *(int *)data;
538		break;
539	case TUNGDEBUG:
540		*(int *)data = tundebug;
541		break;
542	case TUNSLMODE:
543		if (*(int *)data) {
544			tp->tun_flags |= TUN_LMODE;
545			tp->tun_flags &= ~TUN_IFHEAD;
546		} else
547			tp->tun_flags &= ~TUN_LMODE;
548		break;
549	case TUNSIFHEAD:
550		if (*(int *)data) {
551			tp->tun_flags |= TUN_IFHEAD;
552			tp->tun_flags &= ~TUN_LMODE;
553		} else
554			tp->tun_flags &= ~TUN_IFHEAD;
555		break;
556	case TUNGIFHEAD:
557		*(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
558		break;
559	case TUNSIFMODE:
560		/* deny this if UP */
561		if (tp->tun_if.if_flags & IFF_UP)
562			return(EBUSY);
563
564		switch (*(int *)data & ~IFF_MULTICAST) {
565		case IFF_POINTOPOINT:
566		case IFF_BROADCAST:
567			tp->tun_if.if_flags &=
568			    ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
569			tp->tun_if.if_flags |= *(int *)data;
570			break;
571		default:
572			return(EINVAL);
573		}
574		break;
575	case TUNSIFPID:
576		tp->tun_pid = curthread->td_proc->p_pid;
577		break;
578	case FIONBIO:
579		break;
580	case FIOASYNC:
581		if (*(int *)data)
582			tp->tun_flags |= TUN_ASYNC;
583		else
584			tp->tun_flags &= ~TUN_ASYNC;
585		break;
586	case FIONREAD:
587		s = splimp();
588		if (tp->tun_if.if_snd.ifq_head) {
589			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
590			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
591				*(int *)data += mb->m_len;
592		} else
593			*(int *)data = 0;
594		splx(s);
595		break;
596	case FIOSETOWN:
597		return (fsetown(*(int *)data, &tp->tun_sigio));
598
599	case FIOGETOWN:
600		*(int *)data = fgetown(&tp->tun_sigio);
601		return (0);
602
603	/* This is deprecated, FIOSETOWN should be used instead. */
604	case TIOCSPGRP:
605		return (fsetown(-(*(int *)data), &tp->tun_sigio));
606
607	/* This is deprecated, FIOGETOWN should be used instead. */
608	case TIOCGPGRP:
609		*(int *)data = -fgetown(&tp->tun_sigio);
610		return (0);
611
612	default:
613		return (ENOTTY);
614	}
615	return (0);
616}
617
618/*
619 * The cdevsw read interface - reads a packet at a time, or at
620 * least as much of a packet as can be read.
621 */
622static	int
623tunread(dev_t dev, struct uio *uio, int flag)
624{
625	struct tun_softc *tp = dev->si_drv1;
626	struct ifnet	*ifp = &tp->tun_if;
627	struct mbuf	*m;
628	int		error=0, len, s;
629
630	TUNDEBUG (ifp, "read\n");
631	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
632		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
633		return (EHOSTDOWN);
634	}
635
636	tp->tun_flags &= ~TUN_RWAIT;
637
638	s = splimp();
639	do {
640		IF_DEQUEUE(&ifp->if_snd, m);
641		if (m == NULL) {
642			if (flag & IO_NDELAY) {
643				splx(s);
644				return (EWOULDBLOCK);
645			}
646			tp->tun_flags |= TUN_RWAIT;
647			if((error = tsleep(tp, PCATCH | (PZERO + 1),
648					"tunread", 0)) != 0) {
649				splx(s);
650				return (error);
651			}
652		}
653	} while (m == NULL);
654	splx(s);
655
656	while (m && uio->uio_resid > 0 && error == 0) {
657		len = min(uio->uio_resid, m->m_len);
658		if (len != 0)
659			error = uiomove(mtod(m, void *), len, uio);
660		m = m_free(m);
661	}
662
663	if (m) {
664		TUNDEBUG(ifp, "Dropping mbuf\n");
665		m_freem(m);
666	}
667	return (error);
668}
669
670/*
671 * the cdevsw write interface - an atomic write is a packet - or else!
672 */
673static	int
674tunwrite(dev_t dev, struct uio *uio, int flag)
675{
676	struct tun_softc *tp = dev->si_drv1;
677	struct ifnet	*ifp = &tp->tun_if;
678	struct mbuf	*top, **mp, *m;
679	int		error=0, tlen, mlen;
680	uint32_t	family;
681	int 		isr;
682
683	TUNDEBUG(ifp, "tunwrite\n");
684
685	if ((ifp->if_flags & IFF_UP) != IFF_UP)
686		/* ignore silently */
687		return (0);
688
689	if (uio->uio_resid == 0)
690		return (0);
691
692	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
693		TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
694		return (EIO);
695	}
696	tlen = uio->uio_resid;
697
698	/* get a header mbuf */
699	MGETHDR(m, M_DONTWAIT, MT_DATA);
700	if (m == NULL)
701		return (ENOBUFS);
702	mlen = MHLEN;
703
704	top = 0;
705	mp = &top;
706	while (error == 0 && uio->uio_resid > 0) {
707		m->m_len = min(mlen, uio->uio_resid);
708		error = uiomove(mtod(m, void *), m->m_len, uio);
709		*mp = m;
710		mp = &m->m_next;
711		if (uio->uio_resid > 0) {
712			MGET (m, M_DONTWAIT, MT_DATA);
713			if (m == 0) {
714				error = ENOBUFS;
715				break;
716			}
717			mlen = MLEN;
718		}
719	}
720	if (error) {
721		if (top)
722			m_freem (top);
723		ifp->if_ierrors++;
724		return (error);
725	}
726
727	top->m_pkthdr.len = tlen;
728	top->m_pkthdr.rcvif = ifp;
729#ifdef MAC
730	mac_create_mbuf_from_ifnet(ifp, top);
731#endif
732
733	if (tp->tun_flags & TUN_IFHEAD) {
734		if (top->m_len < sizeof(family) &&
735		    (top = m_pullup(top, sizeof(family))) == NULL)
736			return (ENOBUFS);
737		family = ntohl(*mtod(top, u_int32_t *));
738		m_adj(top, sizeof(family));
739	} else
740		family = AF_INET;
741
742	BPF_MTAP2(ifp, &family, sizeof(family), top);
743
744	switch (family) {
745#ifdef INET
746	case AF_INET:
747		isr = NETISR_IP;
748		break;
749#endif
750#ifdef INET6
751	case AF_INET6:
752		isr = NETISR_IPV6;
753		break;
754#endif
755#ifdef IPX
756	case AF_IPX:
757		isr = NETISR_IPX;
758		break;
759#endif
760#ifdef NETATALK
761	case AF_APPLETALK:
762		isr = NETISR_ATALK2;
763		break;
764#endif
765	default:
766		m_freem(m);
767		return (EAFNOSUPPORT);
768	}
769	/* First chunk of an mbuf contains good junk */
770	if (harvest.point_to_point)
771		random_harvest(m, 16, 3, 0, RANDOM_NET);
772	ifp->if_ibytes += top->m_pkthdr.len;
773	ifp->if_ipackets++;
774	netisr_dispatch(isr, top);
775	return (0);
776}
777
778/*
779 * tunpoll - the poll interface, this is only useful on reads
780 * really. The write detect always returns true, write never blocks
781 * anyway, it either accepts the packet or drops it.
782 */
783static	int
784tunpoll(dev_t dev, int events, struct thread *td)
785{
786	int		s;
787	struct tun_softc *tp = dev->si_drv1;
788	struct ifnet	*ifp = &tp->tun_if;
789	int		revents = 0;
790
791	s = splimp();
792	TUNDEBUG(ifp, "tunpoll\n");
793
794	if (events & (POLLIN | POLLRDNORM)) {
795		if (ifp->if_snd.ifq_len > 0) {
796			TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
797			revents |= events & (POLLIN | POLLRDNORM);
798		} else {
799			TUNDEBUG(ifp, "tunpoll waiting\n");
800			selrecord(td, &tp->tun_rsel);
801		}
802	}
803	if (events & (POLLOUT | POLLWRNORM))
804		revents |= events & (POLLOUT | POLLWRNORM);
805
806	splx(s);
807	return (revents);
808}
809