xref: /dflybsd-src/sys/net/tun/if_tun.c (revision 163625b9bf04f32ec3cbbaef7e6b242aeb8a152c)
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: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $
17  * $DragonFly: src/sys/net/tun/if_tun.c,v 1.37 2008/06/05 18:06:32 swildner Exp $
18  */
19 
20 #include "use_tun.h"
21 #include "opt_atalk.h"
22 #include "opt_inet.h"
23 #include "opt_inet6.h"
24 #include "opt_ipx.h"
25 
26 #include <sys/param.h>
27 #include <sys/proc.h>
28 #include <sys/priv.h>
29 #include <sys/systm.h>
30 #include <sys/mbuf.h>
31 #include <sys/socket.h>
32 #include <sys/conf.h>
33 #include <sys/device.h>
34 #include <sys/filio.h>
35 #include <sys/sockio.h>
36 #include <sys/thread2.h>
37 #include <sys/ttycom.h>
38 #include <sys/signalvar.h>
39 #include <sys/filedesc.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/uio.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 
46 #include <sys/mplock2.h>
47 
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/ifq_var.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
53 #include <sys/devfs.h>
54 
55 #ifdef INET
56 #include <netinet/in.h>
57 #endif
58 
59 #include <net/bpf.h>
60 
61 #include "if_tunvar.h"
62 #include "if_tun.h"
63 
64 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
65 
66 static void tunattach (void *);
67 PSEUDO_SET(tunattach, if_tun);
68 
69 static void tuncreate (cdev_t dev);
70 
71 #define TUNDEBUG	if (tundebug) if_printf
72 static int tundebug = 0;
73 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
74 
75 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
76 	    struct rtentry *rt);
77 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
78 static int tuninit (struct ifnet *);
79 static void tunstart(struct ifnet *);
80 static void tun_filter_detach(struct knote *);
81 static int tun_filter_read(struct knote *, long);
82 static int tun_filter_write(struct knote *, long);
83 
84 static	d_open_t	tunopen;
85 static	d_close_t	tunclose;
86 static	d_read_t	tunread;
87 static	d_write_t	tunwrite;
88 static	d_ioctl_t	tunioctl;
89 static	d_kqfilter_t	tunkqfilter;
90 
91 static d_clone_t tunclone;
92 DEVFS_DECLARE_CLONE_BITMAP(tun);
93 
94 #if NTUN <= 1
95 #define TUN_PREALLOCATED_UNITS	4
96 #else
97 #define TUN_PREALLOCATED_UNITS	NTUN
98 #endif
99 
100 #define CDEV_MAJOR 52
101 static struct dev_ops tun_ops = {
102 	{ "tun", CDEV_MAJOR, D_KQFILTER },
103 	.d_open =	tunopen,
104 	.d_close =	tunclose,
105 	.d_read =	tunread,
106 	.d_write =	tunwrite,
107 	.d_ioctl =	tunioctl,
108 	.d_kqfilter =	tunkqfilter
109 };
110 
111 static void
112 tunattach(void *dummy)
113 {
114 	int i;
115 	make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
116 		tunclone, UID_UUCP, GID_DIALER, 0600, "tun");
117 	for (i = 0; i < TUN_PREALLOCATED_UNITS; i++) {
118 		make_dev(&tun_ops, i, UID_UUCP, GID_DIALER, 0600, "tun%d", i);
119 		devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i);
120 	}
121 	/* Doesn't need uninit because unloading is not possible, see PSEUDO_SET */
122 }
123 
124 static int
125 tunclone(struct dev_clone_args *ap)
126 {
127 	int unit;
128 
129 	unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
130 	ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 0600,
131 								"tun%d", unit);
132 
133 	return 0;
134 }
135 
136 static void
137 tuncreate(cdev_t dev)
138 {
139 	struct tun_softc *sc;
140 	struct ifnet *ifp;
141 
142 #if 0
143 	dev = make_dev(&tun_ops, minor(dev),
144 	    UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
145 #endif
146 
147 	MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
148 	sc->tun_flags = TUN_INITED;
149 
150 	ifp = &sc->tun_if;
151 	if_initname(ifp, "tun", lminor(dev));
152 	ifp->if_mtu = TUNMTU;
153 	ifp->if_ioctl = tunifioctl;
154 	ifp->if_output = tunoutput;
155 	ifp->if_start = tunstart;
156 	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
157 	ifp->if_type = IFT_PPP;
158 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
159 	ifq_set_ready(&ifp->if_snd);
160 	ifp->if_softc = sc;
161 	if_attach(ifp, NULL);
162 	bpfattach(ifp, DLT_NULL, sizeof(u_int));
163 	dev->si_drv1 = sc;
164 }
165 
166 /*
167  * tunnel open - must be superuser & the device must be
168  * configured in
169  */
170 static	int
171 tunopen(struct dev_open_args *ap)
172 {
173 	cdev_t dev = ap->a_head.a_dev;
174 	struct ifnet	*ifp;
175 	struct tun_softc *tp;
176 	int	error;
177 
178 	if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
179 		return (error);
180 
181 	tp = dev->si_drv1;
182 	if (!tp) {
183 		tuncreate(dev);
184 		tp = dev->si_drv1;
185 	}
186 	if (tp->tun_flags & TUN_OPEN)
187 		return EBUSY;
188 	tp->tun_pid = curproc->p_pid;
189 	ifp = &tp->tun_if;
190 	tp->tun_flags |= TUN_OPEN;
191 	TUNDEBUG(ifp, "open\n");
192 	return (0);
193 }
194 
195 /*
196  * tunclose - close the device - mark i/f down & delete
197  * routing info
198  */
199 static	int
200 tunclose(struct dev_close_args *ap)
201 {
202 	cdev_t dev = ap->a_head.a_dev;
203 	struct tun_softc *tp;
204 	struct ifnet	*ifp;
205 
206 	tp = dev->si_drv1;
207 	ifp = &tp->tun_if;
208 
209 	tp->tun_flags &= ~TUN_OPEN;
210 	tp->tun_pid = 0;
211 
212 	/* Junk all pending output. */
213 	ifq_purge(&ifp->if_snd);
214 
215 	if (ifp->if_flags & IFF_UP)
216 		if_down(ifp);
217 	ifp->if_flags &= ~IFF_RUNNING;
218 	if_purgeaddrs_nolink(ifp);
219 
220 	funsetown(tp->tun_sigio);
221 	selwakeup(&tp->tun_rsel);
222 
223 	TUNDEBUG(ifp, "closed\n");
224 #if 0
225 	if (dev->si_uminor >= TUN_PREALLOCATED_UNITS) {
226 		devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), dev->si_uminor);
227 	}
228 #endif
229 	return (0);
230 }
231 
232 static int
233 tuninit(struct ifnet *ifp)
234 {
235 	struct tun_softc *tp = ifp->if_softc;
236 	struct ifaddr_container *ifac;
237 	int error = 0;
238 
239 	TUNDEBUG(ifp, "tuninit\n");
240 
241 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
242 	getmicrotime(&ifp->if_lastchange);
243 
244 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
245 		struct ifaddr *ifa = ifac->ifa;
246 
247 		if (ifa->ifa_addr == NULL) {
248 			error = EFAULT;
249 			/* XXX: Should maybe return straight off? */
250 		} else {
251 #ifdef INET
252 			if (ifa->ifa_addr->sa_family == AF_INET) {
253 			    struct sockaddr_in *si;
254 
255 			    si = (struct sockaddr_in *)ifa->ifa_addr;
256 			    if (si->sin_addr.s_addr)
257 				    tp->tun_flags |= TUN_IASET;
258 			}
259 #endif
260 		}
261 	}
262 	return (error);
263 }
264 
265 /*
266  * Process an ioctl request.
267  *
268  * MPSAFE
269  */
270 int
271 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
272 {
273 	struct ifreq *ifr = (struct ifreq *)data;
274 	struct tun_softc *tp = ifp->if_softc;
275 	struct ifstat *ifs;
276 	int error = 0;
277 
278 	switch(cmd) {
279 	case SIOCGIFSTATUS:
280 		ifs = (struct ifstat *)data;
281 		if (tp->tun_pid)
282 			ksprintf(ifs->ascii + strlen(ifs->ascii),
283 			    "\tOpened by PID %d\n", tp->tun_pid);
284 		break;
285 	case SIOCSIFADDR:
286 		error = tuninit(ifp);
287 		TUNDEBUG(ifp, "address set, error=%d\n", error);
288 		break;
289 	case SIOCSIFDSTADDR:
290 		error = tuninit(ifp);
291 		TUNDEBUG(ifp, "destination address set, error=%d\n", error);
292 		break;
293 	case SIOCSIFMTU:
294 		ifp->if_mtu = ifr->ifr_mtu;
295 		TUNDEBUG(ifp, "mtu set\n");
296 		break;
297 	case SIOCSIFFLAGS:
298 	case SIOCADDMULTI:
299 	case SIOCDELMULTI:
300 		break;
301 	default:
302 		error = EINVAL;
303 	}
304 	return (error);
305 }
306 
307 /*
308  * tunoutput - queue packets from higher level ready to put out.
309  *
310  * MPSAFE
311  */
312 static int
313 tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
314 		     struct rtentry *rt)
315 {
316 	struct tun_softc *tp = ifp->if_softc;
317 	int error;
318 	struct altq_pktattr pktattr;
319 
320 	TUNDEBUG(ifp, "tunoutput\n");
321 
322 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
323 		TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
324 		m_freem (m0);
325 		return EHOSTDOWN;
326 	}
327 
328 	/*
329 	 * if the queueing discipline needs packet classification,
330 	 * do it before prepending link headers.
331 	 */
332 	ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
333 
334 	/* BPF write needs to be handled specially */
335 	if (dst->sa_family == AF_UNSPEC) {
336 		dst->sa_family = *(mtod(m0, int *));
337 		m0->m_len -= sizeof(int);
338 		m0->m_pkthdr.len -= sizeof(int);
339 		m0->m_data += sizeof(int);
340 	}
341 
342 	if (ifp->if_bpf) {
343 		/*
344 		 * We need to prepend the address family as
345 		 * a four byte field.
346 		 */
347 		uint32_t af = dst->sa_family;
348 
349 		bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
350 	}
351 
352 	/* prepend sockaddr? this may abort if the mbuf allocation fails */
353 	if (tp->tun_flags & TUN_LMODE) {
354 		/* allocate space for sockaddr */
355 		M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
356 
357 		/* if allocation failed drop packet */
358 		if (m0 == NULL){
359 			IF_DROP(&ifp->if_snd);
360 			ifp->if_oerrors++;
361 			return (ENOBUFS);
362 		} else {
363 			bcopy(dst, m0->m_data, dst->sa_len);
364 		}
365 	}
366 
367 	if (tp->tun_flags & TUN_IFHEAD) {
368 		/* Prepend the address family */
369 		M_PREPEND(m0, 4, MB_DONTWAIT);
370 
371 		/* if allocation failed drop packet */
372 		if (m0 == NULL){
373 			IF_DROP(&ifp->if_snd);
374 			ifp->if_oerrors++;
375 			return ENOBUFS;
376 		} else
377 			*(u_int32_t *)m0->m_data = htonl(dst->sa_family);
378 	} else {
379 #ifdef INET
380 		if (dst->sa_family != AF_INET)
381 #endif
382 		{
383 			m_freem(m0);
384 			return EAFNOSUPPORT;
385 		}
386 	}
387 
388 	error = ifq_handoff(ifp, m0, &pktattr);
389 	if (error) {
390 		ifp->if_collisions++;
391 	} else {
392 		ifp->if_opackets++;
393 		if (tp->tun_flags & TUN_RWAIT) {
394 			tp->tun_flags &= ~TUN_RWAIT;
395 			wakeup((caddr_t)tp);
396 		}
397 		get_mplock();
398 		if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
399 			pgsigio(tp->tun_sigio, SIGIO, 0);
400 		selwakeup(&tp->tun_rsel);
401 		rel_mplock();
402 	}
403 	return (error);
404 }
405 
406 static int
407 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
408 	  struct rtentry *rt)
409 {
410 	int error;
411 
412 	ifnet_serialize_all(ifp);
413 	error = tunoutput_serialized(ifp, m0, dst, rt);
414 	ifnet_deserialize_all(ifp);
415 
416 	return error;
417 }
418 
419 /*
420  * the ops interface is now pretty minimal.
421  */
422 static	int
423 tunioctl(struct dev_ioctl_args *ap)
424 {
425 	cdev_t dev = ap->a_head.a_dev;
426 	struct tun_softc *tp = dev->si_drv1;
427  	struct tuninfo *tunp;
428 
429 	switch (ap->a_cmd) {
430  	case TUNSIFINFO:
431  		tunp = (struct tuninfo *)ap->a_data;
432 		if (tunp->mtu < IF_MINMTU)
433 			return (EINVAL);
434  		tp->tun_if.if_mtu = tunp->mtu;
435  		tp->tun_if.if_type = tunp->type;
436  		tp->tun_if.if_baudrate = tunp->baudrate;
437  		break;
438  	case TUNGIFINFO:
439  		tunp = (struct tuninfo *)ap->a_data;
440  		tunp->mtu = tp->tun_if.if_mtu;
441  		tunp->type = tp->tun_if.if_type;
442  		tunp->baudrate = tp->tun_if.if_baudrate;
443  		break;
444 	case TUNSDEBUG:
445 		tundebug = *(int *)ap->a_data;
446 		break;
447 	case TUNGDEBUG:
448 		*(int *)ap->a_data = tundebug;
449 		break;
450 	case TUNSLMODE:
451 		if (*(int *)ap->a_data) {
452 			tp->tun_flags |= TUN_LMODE;
453 			tp->tun_flags &= ~TUN_IFHEAD;
454 		} else
455 			tp->tun_flags &= ~TUN_LMODE;
456 		break;
457 	case TUNSIFHEAD:
458 		if (*(int *)ap->a_data) {
459 			tp->tun_flags |= TUN_IFHEAD;
460 			tp->tun_flags &= ~TUN_LMODE;
461 		} else
462 			tp->tun_flags &= ~TUN_IFHEAD;
463 		break;
464 	case TUNGIFHEAD:
465 		*(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
466 		break;
467 	case TUNSIFMODE:
468 		/* deny this if UP */
469 		if (tp->tun_if.if_flags & IFF_UP)
470 			return(EBUSY);
471 
472 		switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
473 		case IFF_POINTOPOINT:
474 		case IFF_BROADCAST:
475 			tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
476 			tp->tun_if.if_flags |= *(int *)ap->a_data;
477 			break;
478 		default:
479 			return(EINVAL);
480 		}
481 		break;
482 	case TUNSIFPID:
483 		tp->tun_pid = curproc->p_pid;
484 		break;
485 	case FIOASYNC:
486 		if (*(int *)ap->a_data)
487 			tp->tun_flags |= TUN_ASYNC;
488 		else
489 			tp->tun_flags &= ~TUN_ASYNC;
490 		break;
491 	case FIONREAD:
492 		if (!ifq_is_empty(&tp->tun_if.if_snd)) {
493 			struct mbuf *mb;
494 
495 			mb = ifq_poll(&tp->tun_if.if_snd);
496 			for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
497 				*(int *)ap->a_data += mb->m_len;
498 		} else {
499 			*(int *)ap->a_data = 0;
500 		}
501 		break;
502 	case FIOSETOWN:
503 		return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
504 
505 	case FIOGETOWN:
506 		*(int *)ap->a_data = fgetown(tp->tun_sigio);
507 		return (0);
508 
509 	/* This is deprecated, FIOSETOWN should be used instead. */
510 	case TIOCSPGRP:
511 		return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
512 
513 	/* This is deprecated, FIOGETOWN should be used instead. */
514 	case TIOCGPGRP:
515 		*(int *)ap->a_data = -fgetown(tp->tun_sigio);
516 		return (0);
517 
518 	default:
519 		return (ENOTTY);
520 	}
521 	return (0);
522 }
523 
524 /*
525  * The ops read interface - reads a packet at a time, or at
526  * least as much of a packet as can be read.
527  */
528 static	int
529 tunread(struct dev_read_args *ap)
530 {
531 	cdev_t dev = ap->a_head.a_dev;
532 	struct uio *uio = ap->a_uio;
533 	struct tun_softc *tp = dev->si_drv1;
534 	struct ifnet	*ifp = &tp->tun_if;
535 	struct mbuf	*m0;
536 	int		error=0, len;
537 
538 	TUNDEBUG(ifp, "read\n");
539 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
540 		TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
541 		return EHOSTDOWN;
542 	}
543 
544 	tp->tun_flags &= ~TUN_RWAIT;
545 
546 	ifnet_serialize_all(ifp);
547 
548 	while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
549 		if (ap->a_ioflag & IO_NDELAY) {
550 			ifnet_deserialize_all(ifp);
551 			return EWOULDBLOCK;
552 		}
553 		tp->tun_flags |= TUN_RWAIT;
554 		ifnet_deserialize_all(ifp);
555 		if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
556 			return error;
557 		ifnet_serialize_all(ifp);
558 	}
559 
560 	ifnet_deserialize_all(ifp);
561 
562 	while (m0 && uio->uio_resid > 0 && error == 0) {
563 		len = (int)szmin(uio->uio_resid, m0->m_len);
564 		if (len != 0)
565 			error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
566 		m0 = m_free(m0);
567 	}
568 
569 	if (m0) {
570 		TUNDEBUG(ifp, "Dropping mbuf\n");
571 		m_freem(m0);
572 	}
573 	return error;
574 }
575 
576 /*
577  * the ops write interface - an atomic write is a packet - or else!
578  */
579 static	int
580 tunwrite(struct dev_write_args *ap)
581 {
582 	cdev_t dev = ap->a_head.a_dev;
583 	struct uio *uio = ap->a_uio;
584 	struct tun_softc *tp = dev->si_drv1;
585 	struct ifnet	*ifp = &tp->tun_if;
586 	struct mbuf	*top, **mp, *m;
587 	int		error=0;
588 	size_t		tlen, mlen;
589 	uint32_t	family;
590 	int		isr;
591 
592 	TUNDEBUG(ifp, "tunwrite\n");
593 
594 	if (uio->uio_resid == 0)
595 		return 0;
596 
597 	if (uio->uio_resid > TUNMRU) {
598 		TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
599 		return EIO;
600 	}
601 	tlen = uio->uio_resid;
602 
603 	/* get a header mbuf */
604 	MGETHDR(m, MB_DONTWAIT, MT_DATA);
605 	if (m == NULL)
606 		return ENOBUFS;
607 	mlen = MHLEN;
608 
609 	top = 0;
610 	mp = &top;
611 	while (error == 0 && uio->uio_resid > 0) {
612 		m->m_len = (int)szmin(mlen, uio->uio_resid);
613 		error = uiomove(mtod (m, caddr_t), (size_t)m->m_len, uio);
614 		*mp = m;
615 		mp = &m->m_next;
616 		if (uio->uio_resid > 0) {
617 			MGET (m, MB_DONTWAIT, MT_DATA);
618 			if (m == 0) {
619 				error = ENOBUFS;
620 				break;
621 			}
622 			mlen = MLEN;
623 		}
624 	}
625 	if (error) {
626 		if (top)
627 			m_freem (top);
628 		ifp->if_ierrors++;
629 		return error;
630 	}
631 
632 	top->m_pkthdr.len = (int)tlen;
633 	top->m_pkthdr.rcvif = ifp;
634 
635 	if (ifp->if_bpf) {
636 		if (tp->tun_flags & TUN_IFHEAD) {
637 			/*
638 			 * Conveniently, we already have a 4-byte address
639 			 * family prepended to our packet !
640 			 * Inconveniently, it's in the wrong byte order !
641 			 */
642 			if ((top = m_pullup(top, sizeof(family))) == NULL)
643 				return ENOBUFS;
644 			*mtod(top, u_int32_t *) =
645 			    ntohl(*mtod(top, u_int32_t *));
646 			bpf_mtap(ifp->if_bpf, top);
647 			*mtod(top, u_int32_t *) =
648 			    htonl(*mtod(top, u_int32_t *));
649 		} else {
650 			/*
651 			 * We need to prepend the address family as
652 			 * a four byte field.
653 			 */
654 			static const uint32_t af = AF_INET;
655 
656 			bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
657 		}
658 	}
659 
660 	if (tp->tun_flags & TUN_IFHEAD) {
661 		if (top->m_len < sizeof(family) &&
662 		    (top = m_pullup(top, sizeof(family))) == NULL)
663 				return ENOBUFS;
664 		family = ntohl(*mtod(top, u_int32_t *));
665 		m_adj(top, sizeof(family));
666 	} else
667 		family = AF_INET;
668 
669 	ifp->if_ibytes += top->m_pkthdr.len;
670 	ifp->if_ipackets++;
671 
672 	switch (family) {
673 #ifdef INET
674 	case AF_INET:
675 		isr = NETISR_IP;
676 		break;
677 #endif
678 #ifdef INET6
679 	case AF_INET6:
680 		isr = NETISR_IPV6;
681 		break;
682 #endif
683 #ifdef IPX
684 	case AF_IPX:
685 		isr = NETISR_IPX;
686 		break;
687 #endif
688 #ifdef NETATALK
689 	case AF_APPLETALK:
690 		isr = NETISR_ATALK2;
691 		break;
692 #endif
693 	default:
694 		m_freem(m);
695 		return (EAFNOSUPPORT);
696 	}
697 
698 	netisr_dispatch(isr, top);
699 	return (0);
700 }
701 
702 static struct filterops tun_read_filtops =
703 	{ 1, NULL, tun_filter_detach, tun_filter_read };
704 static struct filterops tun_write_filtops =
705 	{ 1, NULL, tun_filter_detach, tun_filter_write };
706 
707 static int
708 tunkqfilter(struct dev_kqfilter_args *ap)
709 {
710 	cdev_t dev = ap->a_head.a_dev;
711 	struct tun_softc *tp = dev->si_drv1;
712 	struct knote *kn = ap->a_kn;
713 	struct klist *klist;
714 
715 	ap->a_result = 0;
716 	ifnet_serialize_all(&tp->tun_if);
717 
718 	switch (kn->kn_filter) {
719 	case EVFILT_READ:
720 		kn->kn_fop = &tun_read_filtops;
721 		kn->kn_hook = (caddr_t)tp;
722 		break;
723 	case EVFILT_WRITE:
724 		kn->kn_fop = &tun_write_filtops;
725 		kn->kn_hook = (caddr_t)tp;
726 		break;
727 	default:
728 		ifnet_deserialize_all(&tp->tun_if);
729 		ap->a_result = EOPNOTSUPP;
730 		return (0);
731 	}
732 
733 	klist = &tp->tun_rsel.si_note;
734 	crit_enter();
735 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
736 	crit_exit();
737 
738 	ifnet_deserialize_all(&tp->tun_if);
739 
740 	return (0);
741 }
742 
743 static void
744 tun_filter_detach(struct knote *kn)
745 {
746 	struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
747 	struct klist *klist;
748 
749 	klist = &tp->tun_rsel.si_note;
750 	crit_enter();
751 	SLIST_REMOVE(klist, kn, knote, kn_selnext);
752 	crit_exit();
753 }
754 
755 static int
756 tun_filter_write(struct knote *kn, long hint)
757 {
758 	/* Always ready for a write */
759 	return (1);
760 }
761 
762 static int
763 tun_filter_read(struct knote *kn, long hint)
764 {
765 	struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
766 	int ready = 0;
767 
768 	ifnet_serialize_all(&tp->tun_if);
769 	if (!ifq_is_empty(&tp->tun_if.if_snd))
770 		ready = 1;
771 	ifnet_deserialize_all(&tp->tun_if);
772 
773 	return (ready);
774 }
775 
776 /*
777  * Start packet transmission on the interface.
778  * when the interface queue is rate-limited by ALTQ,
779  * if_start is needed to drain packets from the queue in order
780  * to notify readers when outgoing packets become ready.
781  */
782 static void
783 tunstart(struct ifnet *ifp)
784 {
785 	struct tun_softc *tp = ifp->if_softc;
786 	struct mbuf *m;
787 
788 	if (!ifq_is_enabled(&ifp->if_snd))
789 		return;
790 
791 	m = ifq_poll(&ifp->if_snd);
792 	if (m != NULL) {
793 		if (tp->tun_flags & TUN_RWAIT) {
794 			tp->tun_flags &= ~TUN_RWAIT;
795 			wakeup((caddr_t)tp);
796 		}
797 		if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
798 			pgsigio(tp->tun_sigio, SIGIO, 0);
799 		selwakeup(&tp->tun_rsel);
800 	}
801 }
802