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