xref: /openbsd-src/sys/net/if_tun.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: if_tun.c,v 1.48 2003/06/12 10:49:17 henning Exp $	*/
2 /*	$NetBSD: if_tun.c,v 1.24 1996/05/07 02:40:48 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, Julian Onions <Julian.Onions@nexor.co.uk>
6  * Nottingham University 1987.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * This driver takes packets off the IP i/f and hands them up to a
32  * user process to have its wicked way with. This driver has its
33  * roots in a similar driver written by Phil Cockcroft (formerly) at
34  * UCL. This driver is based much more on read/write/select mode of
35  * operation though.
36  */
37 
38 /* #define	TUN_DEBUG	9 */
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/select.h>
51 #include <sys/file.h>
52 #include <sys/time.h>
53 #include <sys/device.h>
54 #include <sys/vnode.h>
55 #include <sys/signalvar.h>
56 #include <sys/conf.h>
57 
58 #include <machine/cpu.h>
59 
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 /* #include <netinet/if_ether.h> */
71 #endif
72 
73 #ifdef NS
74 #include <netns/ns.h>
75 #include <netns/ns_if.h>
76 #endif
77 
78 #ifdef IPX
79 #include <netipx/ipx.h>
80 #include <netipx/ipx_if.h>
81 #endif
82 
83 #ifdef NETATALK
84 #include <netatalk/at.h>
85 #include <netatalk/at_var.h>
86 #endif
87 
88 #ifdef ISO
89 #include <netiso/iso.h>
90 #include <netiso/iso_var.h>
91 #endif
92 
93 #include "bpfilter.h"
94 #if NBPFILTER > 0
95 #include <net/bpf.h>
96 #endif
97 
98 #include <net/if_tun.h>
99 
100 struct tun_softc {
101 	struct	ifnet tun_if;		/* the interface */
102 	u_short	tun_flags;		/* misc flags */
103 	pid_t	tun_pgid;		/* the process group - if any */
104 	uid_t	tun_siguid;		/* uid for process that set tun_pgid */
105 	uid_t	tun_sigeuid;		/* euid for process that set tun_pgid */
106 	struct	selinfo	tun_rsel;	/* read select */
107 	struct	selinfo	tun_wsel;	/* write select (not used) */
108 };
109 
110 #ifdef	TUN_DEBUG
111 int	tundebug = TUN_DEBUG;
112 #define TUNDEBUG(a)	(tundebug? printf a : 0)
113 #else
114 #define TUNDEBUG(a)	/* (tundebug? printf a : 0) */
115 #endif
116 
117 struct tun_softc *tunctl;
118 int ntun;
119 
120 extern int ifqmaxlen;
121 
122 void	tunattach(int);
123 int	tunopen(dev_t, int, int, struct proc *);
124 int	tunclose(dev_t, int, int, struct proc *);
125 int	tun_ioctl(struct ifnet *, u_long, caddr_t);
126 int	tun_output(struct ifnet *, struct mbuf *, struct sockaddr *,
127 		        struct rtentry *rt);
128 int	tunioctl(dev_t, u_long, caddr_t, int, struct proc *);
129 int	tunread(dev_t, struct uio *, int);
130 int	tunwrite(dev_t, struct uio *, int);
131 int	tunselect(dev_t, int, struct proc *);
132 int	tunkqfilter(dev_t, struct knote *);
133 
134 
135 static int tuninit(struct tun_softc *);
136 #ifdef ALTQ
137 static void tunstart(struct ifnet *);
138 #endif
139 
140 void
141 tunattach(n)
142 	int n;
143 {
144 	register int i;
145 	struct ifnet *ifp;
146 
147 	ntun = n;
148 	tunctl = malloc(ntun * sizeof(*tunctl), M_DEVBUF, M_WAITOK);
149 	bzero(tunctl, ntun * sizeof(*tunctl));
150 	for (i = 0; i < ntun; i++) {
151 		tunctl[i].tun_flags = TUN_INITED;
152 
153 		ifp = &tunctl[i].tun_if;
154 		snprintf(ifp->if_xname, sizeof ifp->if_xname, "tun%d", i);
155 		ifp->if_softc = &tunctl[i];
156 		ifp->if_mtu = TUNMTU;
157 		ifp->if_ioctl = tun_ioctl;
158 		ifp->if_output = tun_output;
159 #ifdef ALTQ
160 		ifp->if_start = tunstart;
161 #endif
162 		ifp->if_flags = IFF_POINTOPOINT;
163 		ifp->if_type  = IFT_PROPVIRTUAL;
164 		IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
165 		IFQ_SET_READY(&ifp->if_snd);
166 		ifp->if_hdrlen = sizeof(u_int32_t);
167 		ifp->if_collisions = 0;
168 		ifp->if_ierrors = 0;
169 		ifp->if_oerrors = 0;
170 		ifp->if_ipackets = 0;
171 		ifp->if_opackets = 0;
172 		ifp->if_ibytes = 0;
173 		ifp->if_obytes = 0;
174 		if_attach(ifp);
175 		if_alloc_sadl(ifp);
176 #if NBPFILTER > 0
177 		bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
178 #endif
179 	}
180 }
181 
182 /*
183  * tunnel open - must be superuser & the device must be
184  * configured in
185  */
186 int
187 tunopen(dev, flag, mode, p)
188 	dev_t	dev;
189 	int	flag, mode;
190 	struct proc *p;
191 {
192 	struct tun_softc *tp;
193 	struct ifnet	*ifp;
194 	register int	unit, error;
195 
196 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
197 		return (error);
198 
199 	if ((unit = minor(dev)) >= ntun)
200 		return (ENXIO);
201 
202 	tp = &tunctl[unit];
203 	if (tp->tun_flags & TUN_OPEN)
204 		return EBUSY;
205 
206 	ifp = &tp->tun_if;
207 	tp->tun_flags |= TUN_OPEN;
208 	TUNDEBUG(("%s: open\n", ifp->if_xname));
209 	return (0);
210 }
211 
212 /*
213  * tunclose - close the device; if closing the real device, flush pending
214  *  output and (unless set STAYUP) bring down the interface.
215  */
216 int
217 tunclose(dev, flag, mode, p)
218 	dev_t	dev;
219 	int	flag;
220 	int	mode;
221 	struct proc *p;
222 {
223 	register int	unit, s;
224 	struct tun_softc *tp;
225 	struct ifnet	*ifp;
226 
227 	if ((unit = minor(dev)) >= ntun)
228 		return (ENXIO);
229 
230 	tp = &tunctl[unit];
231 	ifp = &tp->tun_if;
232 	tp->tun_flags &= ~TUN_OPEN;
233 
234 	/*
235 	 * junk all pending output
236 	 */
237 	s = splimp();
238 	IFQ_PURGE(&ifp->if_snd);
239 	splx(s);
240 
241 	if ((ifp->if_flags & IFF_UP) && !(tp->tun_flags & TUN_STAYUP)) {
242 		s = splimp();
243 		if_down(ifp);
244 		if (ifp->if_flags & IFF_RUNNING) {
245 			/* find internet addresses and delete routes */
246 			register struct ifaddr *ifa;
247 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
248 #ifdef INET
249 				if (ifa->ifa_addr->sa_family == AF_INET) {
250 					rtinit(ifa, (int)RTM_DELETE,
251 					       (tp->tun_flags & TUN_DSTADDR)?
252 							RTF_HOST : 0);
253 				}
254 #endif
255 			}
256 		}
257 		splx(s);
258 	}
259 	tp->tun_pgid = 0;
260 	selwakeup(&tp->tun_rsel);
261 
262 	TUNDEBUG(("%s: closed\n", ifp->if_xname));
263 	return (0);
264 }
265 
266 static int
267 tuninit(tp)
268 	struct tun_softc *tp;
269 {
270 	struct ifnet	*ifp = &tp->tun_if;
271 	register struct ifaddr *ifa;
272 
273 	TUNDEBUG(("%s: tuninit\n", ifp->if_xname));
274 
275 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
276 
277 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR|TUN_BRDADDR);
278 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
279 #ifdef INET
280 		if (ifa->ifa_addr->sa_family == AF_INET) {
281 			struct sockaddr_in *sin;
282 
283 			sin = satosin(ifa->ifa_addr);
284 			if (sin && sin->sin_addr.s_addr)
285 				tp->tun_flags |= TUN_IASET;
286 
287 			if (ifp->if_flags & IFF_POINTOPOINT) {
288 				sin = satosin(ifa->ifa_dstaddr);
289 				if (sin && sin->sin_addr.s_addr)
290 					tp->tun_flags |= TUN_DSTADDR;
291 			} else
292 				tp->tun_flags &= ~TUN_DSTADDR;
293 
294 			if (ifp->if_flags & IFF_BROADCAST) {
295 				sin = satosin(ifa->ifa_broadaddr);
296 				if (sin && sin->sin_addr.s_addr)
297 					tp->tun_flags |= TUN_BRDADDR;
298 			} else
299 				tp->tun_flags &= ~TUN_BRDADDR;
300 		}
301 #endif
302 #ifdef INET6
303 		if (ifa->ifa_addr->sa_family == AF_INET6) {
304 			struct sockaddr_in6 *sin;
305 
306 			sin = (struct sockaddr_in6 *)ifa->ifa_addr;
307 			if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
308 				tp->tun_flags |= TUN_IASET;
309 
310 			if (ifp->if_flags & IFF_POINTOPOINT) {
311 				sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
312 				if (sin &&
313 				    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
314 					tp->tun_flags |= TUN_DSTADDR;
315 			} else
316 				tp->tun_flags &= ~TUN_DSTADDR;
317 		}
318 #endif /* INET6 */
319 	}
320 
321 	return 0;
322 }
323 
324 /*
325  * Process an ioctl request.
326  */
327 int
328 tun_ioctl(ifp, cmd, data)
329 	struct ifnet *ifp;
330 	u_long	cmd;
331 	caddr_t	data;
332 {
333 	int	error = 0, s;
334 
335 	s = splimp();
336 	switch(cmd) {
337 	case SIOCSIFADDR:
338 		tuninit((struct tun_softc *)(ifp->if_softc));
339 		TUNDEBUG(("%s: address set\n", ifp->if_xname));
340 		break;
341 	case SIOCSIFDSTADDR:
342 		tuninit((struct tun_softc *)(ifp->if_softc));
343 		TUNDEBUG(("%s: destination address set\n", ifp->if_xname));
344 		break;
345 	case SIOCSIFBRDADDR:
346 		tuninit((struct tun_softc *)(ifp->if_softc));
347 		TUNDEBUG(("%s: broadcast address set\n", ifp->if_xname));
348 		break;
349 	case SIOCSIFMTU:
350 		ifp->if_mtu = ((struct ifreq *)data)->ifr_mtu;
351 		break;
352 	case SIOCADDMULTI:
353 	case SIOCDELMULTI: {
354 		struct ifreq *ifr = (struct ifreq *)data;
355 		if (ifr == 0) {
356 			error = EAFNOSUPPORT;	   /* XXX */
357 			break;
358 		}
359 		switch (ifr->ifr_addr.sa_family) {
360 #ifdef INET
361 		case AF_INET:
362 			break;
363 #endif
364 #ifdef INET6
365 		case AF_INET6:
366 			break;
367 #endif
368 		default:
369 			error = EAFNOSUPPORT;
370 			break;
371 		}
372 		break;
373 	}
374 
375 	case SIOCSIFFLAGS:
376 		break;
377 	default:
378 		error = EINVAL;
379 	}
380 	splx(s);
381 	return (error);
382 }
383 
384 /*
385  * tun_output - queue packets from higher level ready to put out.
386  */
387 int
388 tun_output(ifp, m0, dst, rt)
389 	struct ifnet   *ifp;
390 	struct mbuf    *m0;
391 	struct sockaddr *dst;
392 	struct rtentry *rt;
393 {
394 	struct tun_softc *tp = ifp->if_softc;
395 	int		s, len, error;
396 	u_int32_t	*af;
397 
398 	TUNDEBUG(("%s: tun_output\n", ifp->if_xname));
399 
400 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
401 		TUNDEBUG(("%s: not ready 0%o\n", ifp->if_xname,
402 			  tp->tun_flags));
403 		m_freem (m0);
404 		return EHOSTDOWN;
405 	}
406 
407 	M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
408 	af = mtod(m0, u_int32_t *);
409 	*af = htonl(dst->sa_family);
410 
411 #if NBPFILTER > 0
412 	if (ifp->if_bpf)
413 		bpf_mtap(ifp->if_bpf, m0);
414 #endif
415 
416 	len = m0->m_pkthdr.len + sizeof(*af);
417 	s = splimp();
418 	IFQ_ENQUEUE(&ifp->if_snd, m0, NULL, error);
419 	if (error) {
420 		splx(s);
421 		ifp->if_collisions++;
422 		return (error);
423 	}
424 	splx(s);
425 	ifp->if_opackets++;
426 	ifp->if_obytes += len;
427 
428 	if (tp->tun_flags & TUN_RWAIT) {
429 		tp->tun_flags &= ~TUN_RWAIT;
430 		wakeup((caddr_t)tp);
431 	}
432 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
433 		csignal(tp->tun_pgid, SIGIO,
434 		    tp->tun_siguid, tp->tun_sigeuid);
435 	selwakeup(&tp->tun_rsel);
436 	return 0;
437 }
438 
439 /*
440  * the cdevsw interface is now pretty minimal.
441  */
442 int
443 tunioctl(dev, cmd, data, flag, p)
444 	dev_t		dev;
445 	u_long		cmd;
446 	caddr_t		data;
447 	int		flag;
448 	struct proc	*p;
449 {
450 	int		unit, s;
451 	struct tun_softc *tp;
452 	struct tuninfo *tunp;
453 	struct mbuf *m;
454 
455 	if ((unit = minor(dev)) >= ntun)
456 		return (ENXIO);
457 
458 	tp = &tunctl[unit];
459 
460 	s = splimp();
461 	switch (cmd) {
462 	case TUNSIFINFO:
463 		tunp = (struct tuninfo *)data;
464 		tp->tun_if.if_mtu = tunp->mtu;
465 		tp->tun_if.if_type = tunp->type;
466 		tp->tun_if.if_flags = tunp->flags;
467 		tp->tun_if.if_baudrate = tunp->baudrate;
468 		break;
469 	case TUNGIFINFO:
470 		tunp = (struct tuninfo *)data;
471 		tunp->mtu = tp->tun_if.if_mtu;
472 		tunp->type = tp->tun_if.if_type;
473 		tunp->flags = tp->tun_if.if_flags;
474 		tunp->baudrate = tp->tun_if.if_baudrate;
475 		break;
476 #ifdef TUN_DEBUG
477 	case TUNSDEBUG:
478 		tundebug = *(int *)data;
479 		break;
480 	case TUNGDEBUG:
481 		*(int *)data = tundebug;
482 		break;
483 #endif
484         case TUNSIFMODE:
485 	        switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
486                 case IFF_POINTOPOINT:
487                 case IFF_BROADCAST:
488                         if (tp->tun_if.if_flags & IFF_UP) {
489                                 splx(s);
490                                 return (EBUSY);
491                         }
492                         tp->tun_if.if_flags &=
493                                 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
494                         tp->tun_if.if_flags |= *(int *)data;
495                         break;
496                 default:
497 		        splx(s);
498                         return (EINVAL);
499                 }
500                 break;
501 
502        	case FIONBIO:
503 		if (*(int *)data)
504 			tp->tun_flags |= TUN_NBIO;
505 		else
506 			tp->tun_flags &= ~TUN_NBIO;
507 		break;
508 	case FIOASYNC:
509 		if (*(int *)data)
510 			tp->tun_flags |= TUN_ASYNC;
511 		else
512 			tp->tun_flags &= ~TUN_ASYNC;
513 		break;
514 	case FIONREAD:
515 		IFQ_POLL(&tp->tun_if.if_snd, m);
516 		if (m != NULL)
517 			*(int *)data = m->m_pkthdr.len;
518 		else
519 			*(int *)data = 0;
520 		break;
521 	case TIOCSPGRP:
522 		tp->tun_pgid = *(int *)data;
523 		tp->tun_siguid = p->p_cred->p_ruid;
524 		tp->tun_sigeuid = p->p_ucred->cr_uid;
525 		break;
526 	case TIOCGPGRP:
527 		*(int *)data = tp->tun_pgid;
528 		break;
529 	default:
530 		splx(s);
531 		return (ENOTTY);
532 	}
533 	splx(s);
534 	return (0);
535 }
536 
537 /*
538  * The cdevsw read interface - reads a packet at a time, or at
539  * least as much of a packet as can be read.
540  */
541 int
542 tunread(dev, uio, ioflag)
543 	dev_t		dev;
544 	struct uio	*uio;
545 	int		ioflag;
546 {
547 	int		unit;
548 	struct tun_softc *tp;
549 	struct ifnet	*ifp;
550 	struct mbuf	*m, *m0;
551 	int		error = 0, len, s;
552 
553 	if ((unit = minor(dev)) >= ntun)
554 		return (ENXIO);
555 
556 	tp = &tunctl[unit];
557 	ifp = &tp->tun_if;
558 	TUNDEBUG(("%s: read\n", ifp->if_xname));
559 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
560 		TUNDEBUG(("%s: not ready 0%o\n", ifp->if_xname,
561 			  tp->tun_flags));
562 		return EHOSTDOWN;
563 	}
564 
565 	tp->tun_flags &= ~TUN_RWAIT;
566 
567 	s = splimp();
568 	do {
569 		while ((tp->tun_flags & TUN_READY) != TUN_READY)
570 			if ((error = tsleep((caddr_t)tp,
571 			    (PZERO+1)|PCATCH, "tunread", 0)) != 0) {
572 				splx(s);
573 				return (error);
574 			}
575 		IFQ_DEQUEUE(&ifp->if_snd, m0);
576 		if (m0 == 0) {
577 			if (tp->tun_flags & TUN_NBIO && ioflag & IO_NDELAY) {
578 				splx(s);
579 				return EWOULDBLOCK;
580 			}
581 			tp->tun_flags |= TUN_RWAIT;
582 			if ((error = tsleep((caddr_t)tp,
583 			    (PZERO + 1)|PCATCH, "tunread", 0)) != 0) {
584 				splx(s);
585 				return (error);
586 			}
587 		}
588 	} while (m0 == 0);
589 	splx(s);
590 
591 	while (m0 && uio->uio_resid > 0 && error == 0) {
592 		len = min(uio->uio_resid, m0->m_len);
593 		if (len != 0)
594 			error = uiomove(mtod(m0, caddr_t), len, uio);
595 		MFREE(m0, m);
596 		m0 = m;
597 	}
598 
599 	if (m0) {
600 		TUNDEBUG(("Dropping mbuf\n"));
601 		m_freem(m0);
602 	}
603 	if (error)
604 		ifp->if_ierrors++;
605 
606 	return error;
607 }
608 
609 /*
610  * the cdevsw write interface - an atomic write is a packet - or else!
611  */
612 int
613 tunwrite(dev, uio, ioflag)
614 	dev_t		dev;
615 	struct uio	*uio;
616 	int		ioflag;
617 {
618 	int		unit;
619 	struct ifnet	*ifp;
620 	struct ifqueue	*ifq;
621 	u_int32_t	*th;
622 	struct mbuf	*top, **mp, *m;
623 	int		isr;
624 	int		error=0, s, tlen, mlen;
625 
626 	if ((unit = minor(dev)) >= ntun)
627 		return (ENXIO);
628 
629 	ifp = &tunctl[unit].tun_if;
630 	TUNDEBUG(("%s: tunwrite\n", ifp->if_xname));
631 
632 	if (uio->uio_resid == 0 || uio->uio_resid > TUNMRU) {
633 		TUNDEBUG(("%s: len=%d!\n", ifp->if_xname, uio->uio_resid));
634 		return EMSGSIZE;
635 	}
636 	tlen = uio->uio_resid;
637 
638 	/* get a header mbuf */
639 	MGETHDR(m, M_DONTWAIT, MT_DATA);
640 	if (m == NULL)
641 		return ENOBUFS;
642 	mlen = MHLEN;
643 
644 	top = 0;
645 	mp = &top;
646 	while (error == 0 && uio->uio_resid > 0) {
647 		m->m_len = min(mlen, uio->uio_resid);
648 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
649 		*mp = m;
650 		mp = &m->m_next;
651 		if (uio->uio_resid > 0) {
652 			MGET (m, M_DONTWAIT, MT_DATA);
653 			if (m == 0) {
654 				error = ENOBUFS;
655 				break;
656 			}
657 			mlen = MLEN;
658 		}
659 	}
660 	if (error) {
661 		if (top)
662 			m_freem (top);
663 		ifp->if_ierrors++;
664 		return error;
665 	}
666 
667 	top->m_pkthdr.len = tlen;
668 	top->m_pkthdr.rcvif = ifp;
669 
670 #if NBPFILTER > 0
671 	if (ifp->if_bpf)
672 		bpf_mtap(ifp->if_bpf, top);
673 #endif
674 
675 	th = mtod(top, u_int32_t *);
676 	/* strip the tunnel header */
677 	top->m_data += sizeof(*th);
678 	top->m_len  -= sizeof(*th);
679 	top->m_pkthdr.len -= sizeof(*th);
680 
681 	switch (ntohl(*th)) {
682 #ifdef INET
683 	case AF_INET:
684 		ifq = &ipintrq;
685 		isr = NETISR_IP;
686 		break;
687 #endif
688 #ifdef INET6
689 	case AF_INET6:
690 		ifq = &ip6intrq;
691 		isr = NETISR_IPV6;
692 		break;
693 #endif
694 #ifdef NS
695 	case AF_NS:
696 		ifq = &nsintrq;
697 		isr = NETISR_NS;
698 		break;
699 #endif
700 #ifdef IPX
701 	case AF_IPX:
702 		ifq = &ipxintrq;
703 		isr = NETISR_IPX;
704 		break;
705 #endif
706 #ifdef NETATALK
707 	case AF_APPLETALK:
708 		ifq = &atintrq2;
709 		isr = NETISR_ATALK;
710 		break;
711 #endif
712 #ifdef ISO
713 	case AF_ISO:
714 		ifq = &clnlintrq;
715 		isr = NETISR_ISO;
716 		break;
717 #endif
718 	default:
719 		m_freem(top);
720 		return EAFNOSUPPORT;
721 	}
722 
723 	s = splimp();
724 	if (IF_QFULL(ifq)) {
725 		IF_DROP(ifq);
726 		splx(s);
727 		ifp->if_collisions++;
728 		m_freem(top);
729 		return ENOBUFS;
730 	}
731 	IF_ENQUEUE(ifq, top);
732 	schednetisr(isr);
733 	ifp->if_ipackets++;
734 	ifp->if_ibytes += top->m_pkthdr.len;
735 	splx(s);
736 	return error;
737 }
738 
739 /*
740  * tunselect - the select interface, this is only useful on reads
741  * really. The write detect always returns true, write never blocks
742  * anyway, it either accepts the packet or drops it.
743  */
744 int
745 tunselect(dev, rw, p)
746 	dev_t		dev;
747 	int		rw;
748 	struct proc	*p;
749 {
750 	int		unit, s;
751 	struct tun_softc *tp;
752 	struct ifnet	*ifp;
753 	struct mbuf	*m;
754 
755 	if ((unit = minor(dev)) >= ntun)
756 		return (ENXIO);
757 
758 	tp = &tunctl[unit];
759 	ifp = &tp->tun_if;
760 	s = splimp();
761 	TUNDEBUG(("%s: tunselect\n", ifp->if_xname));
762 
763 	switch (rw) {
764 	case FREAD:
765 		IFQ_POLL(&ifp->if_snd, m);
766 		if (m != NULL) {
767 			splx(s);
768 			TUNDEBUG(("%s: tunselect q=%d\n", ifp->if_xname,
769 				  ifp->if_snd.ifq_len));
770 			return 1;
771 		}
772 		selrecord(curproc, &tp->tun_rsel);
773 		break;
774 	case FWRITE:
775 		splx(s);
776 		return 1;
777 	}
778 	splx(s);
779 	TUNDEBUG(("%s: tunselect waiting\n", ifp->if_xname));
780 	return 0;
781 }
782 
783 /* Does not currently work */
784 
785 int
786 tunkqfilter(dev_t dev,struct knote *kn)
787 {
788 	return (1);
789 }
790 
791 #ifdef ALTQ
792 /*
793  * Start packet transmission on the interface.
794  * when the interface queue is rate-limited by ALTQ or TBR,
795  * if_start is needed to drain packets from the queue in order
796  * to notify readers when outgoing packets become ready.
797  */
798 static void
799 tunstart(ifp)
800 	struct ifnet *ifp;
801 {
802 	struct tun_softc *tp = ifp->if_softc;
803 	struct mbuf *m;
804 
805 	if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
806 		return;
807 
808 	IFQ_POLL(&ifp->if_snd, m);
809 	if (m != NULL) {
810 		if (tp->tun_flags & TUN_RWAIT) {
811 			tp->tun_flags &= ~TUN_RWAIT;
812 			wakeup((caddr_t)tp);
813 		}
814 		if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
815 			csignal(tp->tun_pgid, SIGIO,
816 			    tp->tun_siguid, tp->tun_sigeuid);
817 		selwakeup(&tp->tun_rsel);
818 	}
819 }
820 #endif
821