xref: /netbsd-src/sys/net/if_tun.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: if_tun.c,v 1.110 2008/11/20 21:55:15 dyoung 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 its
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 
17 #include <sys/cdefs.h>
18 __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.110 2008/11/20 21:55:15 dyoung Exp $");
19 
20 #include "opt_inet.h"
21 
22 #include <sys/param.h>
23 #include <sys/proc.h>
24 #include <sys/systm.h>
25 #include <sys/mbuf.h>
26 #include <sys/buf.h>
27 #include <sys/protosw.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
30 #include <sys/errno.h>
31 #include <sys/syslog.h>
32 #include <sys/select.h>
33 #include <sys/poll.h>
34 #include <sys/file.h>
35 #include <sys/signalvar.h>
36 #include <sys/conf.h>
37 #include <sys/kauth.h>
38 #include <sys/simplelock.h>
39 #include <sys/cpu.h>
40 
41 #include <net/if.h>
42 #include <net/if_types.h>
43 #include <net/netisr.h>
44 #include <net/route.h>
45 
46 
47 #ifdef INET
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/in_var.h>
51 #include <netinet/ip.h>
52 #include <netinet/if_inarp.h>
53 #endif
54 
55 
56 #include "bpfilter.h"
57 #if NBPFILTER > 0
58 #include <sys/time.h>
59 #include <net/bpf.h>
60 #endif
61 
62 #include <net/if_tun.h>
63 
64 #define TUNDEBUG	if (tundebug) printf
65 int	tundebug = 0;
66 
67 extern int ifqmaxlen;
68 void	tunattach(int);
69 
70 static LIST_HEAD(, tun_softc) tun_softc_list;
71 static LIST_HEAD(, tun_softc) tunz_softc_list;
72 static struct simplelock tun_softc_lock;
73 
74 static int	tun_ioctl(struct ifnet *, u_long, void *);
75 static int	tun_output(struct ifnet *, struct mbuf *,
76 			const struct sockaddr *, struct rtentry *rt);
77 static int	tun_clone_create(struct if_clone *, int);
78 static int	tun_clone_destroy(struct ifnet *);
79 
80 static struct if_clone tun_cloner =
81     IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
82 
83 static void tunattach0(struct tun_softc *);
84 static void tuninit(struct tun_softc *);
85 static void tun_i_softintr(void *);
86 static void tun_o_softintr(void *);
87 #ifdef ALTQ
88 static void tunstart(struct ifnet *);
89 #endif
90 static struct tun_softc *tun_find_unit(dev_t);
91 static struct tun_softc *tun_find_zunit(int);
92 
93 static dev_type_open(tunopen);
94 static dev_type_close(tunclose);
95 static dev_type_read(tunread);
96 static dev_type_write(tunwrite);
97 static dev_type_ioctl(tunioctl);
98 static dev_type_poll(tunpoll);
99 static dev_type_kqfilter(tunkqfilter);
100 
101 const struct cdevsw tun_cdevsw = {
102 	tunopen, tunclose, tunread, tunwrite, tunioctl,
103 	nostop, notty, tunpoll, nommap, tunkqfilter, D_OTHER,
104 };
105 
106 void
107 tunattach(int unused)
108 {
109 
110 	simple_lock_init(&tun_softc_lock);
111 	LIST_INIT(&tun_softc_list);
112 	LIST_INIT(&tunz_softc_list);
113 	if_clone_attach(&tun_cloner);
114 }
115 
116 /*
117  * Find driver instance from dev_t.
118  * Call at splnet().
119  * Returns with tp locked (if found).
120  */
121 static struct tun_softc *
122 tun_find_unit(dev_t dev)
123 {
124 	struct tun_softc *tp;
125 	int unit = minor(dev);
126 
127 	simple_lock(&tun_softc_lock);
128 	LIST_FOREACH(tp, &tun_softc_list, tun_list)
129 		if (unit == tp->tun_unit)
130 			break;
131 	if (tp)
132 		simple_lock(&tp->tun_lock);
133 	simple_unlock(&tun_softc_lock);
134 
135 	return (tp);
136 }
137 
138 /*
139  * Find zombie driver instance by unit number.
140  * Call at splnet().
141  * Remove tp from list and return it unlocked (if found).
142  */
143 static struct tun_softc *
144 tun_find_zunit(int unit)
145 {
146 	struct tun_softc *tp;
147 
148 	simple_lock(&tun_softc_lock);
149 	LIST_FOREACH(tp, &tunz_softc_list, tun_list)
150 		if (unit == tp->tun_unit)
151 			break;
152 	if (tp)
153 		LIST_REMOVE(tp, tun_list);
154 	simple_unlock(&tun_softc_lock);
155 #ifdef DIAGNOSTIC
156 	if (tp != NULL && (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN)
157 		printf("tun%d: inconsistent flags: %x\n", unit, tp->tun_flags);
158 #endif
159 
160 	return (tp);
161 }
162 
163 static int
164 tun_clone_create(struct if_clone *ifc, int unit)
165 {
166 	struct tun_softc *tp;
167 
168 	if ((tp = tun_find_zunit(unit)) == NULL) {
169 		/* Allocate a new instance */
170 		tp = malloc(sizeof(*tp), M_DEVBUF, M_WAITOK|M_ZERO);
171 
172 		tp->tun_unit = unit;
173 		simple_lock_init(&tp->tun_lock);
174 		selinit(&tp->tun_rsel);
175 		selinit(&tp->tun_wsel);
176 	} else {
177 		/* Revive tunnel instance; clear ifp part */
178 		(void)memset(&tp->tun_if, 0, sizeof(struct ifnet));
179 	}
180 
181 	if_initname(&tp->tun_if, ifc->ifc_name, unit);
182 	tunattach0(tp);
183 	tp->tun_flags |= TUN_INITED;
184 	tp->tun_osih = softint_establish(SOFTINT_CLOCK, tun_o_softintr, tp);
185 	tp->tun_isih = softint_establish(SOFTINT_CLOCK, tun_i_softintr, tp);
186 
187 	simple_lock(&tun_softc_lock);
188 	LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list);
189 	simple_unlock(&tun_softc_lock);
190 
191 	return (0);
192 }
193 
194 static void
195 tunattach0(struct tun_softc *tp)
196 {
197 	struct ifnet *ifp;
198 
199 	ifp = &tp->tun_if;
200 	ifp->if_softc = tp;
201 	ifp->if_mtu = TUNMTU;
202 	ifp->if_ioctl = tun_ioctl;
203 	ifp->if_output = tun_output;
204 #ifdef ALTQ
205 	ifp->if_start = tunstart;
206 #endif
207 	ifp->if_flags = IFF_POINTOPOINT;
208 	ifp->if_type = IFT_TUNNEL;
209 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
210 	ifp->if_collisions = 0;
211 	ifp->if_ierrors = 0;
212 	ifp->if_oerrors = 0;
213 	ifp->if_ipackets = 0;
214 	ifp->if_opackets = 0;
215 	ifp->if_ibytes   = 0;
216 	ifp->if_obytes   = 0;
217 	ifp->if_dlt = DLT_NULL;
218 	IFQ_SET_READY(&ifp->if_snd);
219 	if_attach(ifp);
220 	if_alloc_sadl(ifp);
221 #if NBPFILTER > 0
222 	bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
223 #endif
224 }
225 
226 static int
227 tun_clone_destroy(struct ifnet *ifp)
228 {
229 	struct tun_softc *tp = (void *)ifp;
230 	int s, zombie = 0;
231 
232 	s = splnet();
233 	simple_lock(&tun_softc_lock);
234 	simple_lock(&tp->tun_lock);
235 	LIST_REMOVE(tp, tun_list);
236 	if (tp->tun_flags & TUN_OPEN) {
237 		/* Hang on to storage until last close */
238 		zombie = 1;
239 		tp->tun_flags &= ~TUN_INITED;
240 		LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list);
241 	}
242 	simple_unlock(&tun_softc_lock);
243 
244 	IF_PURGE(&ifp->if_snd);
245 	ifp->if_flags &= ~IFF_RUNNING;
246 
247 	if (tp->tun_flags & TUN_RWAIT) {
248 		tp->tun_flags &= ~TUN_RWAIT;
249 		wakeup((void *)tp);
250 	}
251 	selnotify(&tp->tun_rsel, 0, 0);
252 
253 	simple_unlock(&tp->tun_lock);
254 	splx(s);
255 
256 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
257 		fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL);
258 
259 #if NBPFILTER > 0
260 	bpfdetach(ifp);
261 #endif
262 	if_detach(ifp);
263 
264 	if (!zombie) {
265 		seldestroy(&tp->tun_rsel);
266 		seldestroy(&tp->tun_wsel);
267 		softint_disestablish(tp->tun_osih);
268 		softint_disestablish(tp->tun_isih);
269 		free(tp, M_DEVBUF);
270 	}
271 
272 	return (0);
273 }
274 
275 /*
276  * tunnel open - must be superuser & the device must be
277  * configured in
278  */
279 static int
280 tunopen(dev_t dev, int flag, int mode, struct lwp *l)
281 {
282 	struct ifnet	*ifp;
283 	struct tun_softc *tp;
284 	int	s, error;
285 
286 	if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
287 	    NULL)) != 0)
288 		return (error);
289 
290 	s = splnet();
291 	tp = tun_find_unit(dev);
292 
293 	if (tp == NULL) {
294 		(void)tun_clone_create(&tun_cloner, minor(dev));
295 		tp = tun_find_unit(dev);
296 		if (tp == NULL) {
297 			error = ENXIO;
298 			goto out_nolock;
299 		}
300 	}
301 
302 	if (tp->tun_flags & TUN_OPEN) {
303 		error = EBUSY;
304 		goto out;
305 	}
306 
307 	ifp = &tp->tun_if;
308 	tp->tun_flags |= TUN_OPEN;
309 	TUNDEBUG("%s: open\n", ifp->if_xname);
310 out:
311 	simple_unlock(&tp->tun_lock);
312 out_nolock:
313 	splx(s);
314 	return (error);
315 }
316 
317 /*
318  * tunclose - close the device - mark i/f down & delete
319  * routing info
320  */
321 int
322 tunclose(dev_t dev, int flag, int mode,
323     struct lwp *l)
324 {
325 	int	s;
326 	struct tun_softc *tp;
327 	struct ifnet	*ifp;
328 
329 	s = splnet();
330 	if ((tp = tun_find_zunit(minor(dev))) != NULL) {
331 		/* interface was "destroyed" before the close */
332 		seldestroy(&tp->tun_rsel);
333 		seldestroy(&tp->tun_wsel);
334 		softint_disestablish(tp->tun_osih);
335 		softint_disestablish(tp->tun_isih);
336 		free(tp, M_DEVBUF);
337 		goto out_nolock;
338 	}
339 
340 	if ((tp = tun_find_unit(dev)) == NULL)
341 		goto out_nolock;
342 
343 	ifp = &tp->tun_if;
344 
345 	tp->tun_flags &= ~TUN_OPEN;
346 
347 	/*
348 	 * junk all pending output
349 	 */
350 	IFQ_PURGE(&ifp->if_snd);
351 
352 	if (ifp->if_flags & IFF_UP) {
353 		if_down(ifp);
354 		if (ifp->if_flags & IFF_RUNNING) {
355 			/* find internet addresses and delete routes */
356 			struct ifaddr *ifa;
357 			IFADDR_FOREACH(ifa, ifp) {
358 #if defined(INET) || defined(INET6)
359 				if (ifa->ifa_addr->sa_family == AF_INET ||
360 				    ifa->ifa_addr->sa_family == AF_INET6) {
361 					rtinit(ifa, (int)RTM_DELETE,
362 					       tp->tun_flags & TUN_DSTADDR
363 							? RTF_HOST
364 							: 0);
365 				}
366 #endif
367 			}
368 		}
369 	}
370 	tp->tun_pgid = 0;
371 	selnotify(&tp->tun_rsel, 0, 0);
372 
373 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
374 	simple_unlock(&tp->tun_lock);
375 out_nolock:
376 	splx(s);
377 	return (0);
378 }
379 
380 /*
381  * Call at splnet().
382  */
383 static void
384 tuninit(struct tun_softc *tp)
385 {
386 	struct ifnet	*ifp = &tp->tun_if;
387 	struct ifaddr	*ifa;
388 
389 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
390 
391 	simple_lock(&tp->tun_lock);
392 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
393 
394 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
395 	IFADDR_FOREACH(ifa, ifp) {
396 #ifdef INET
397 		if (ifa->ifa_addr->sa_family == AF_INET) {
398 			struct sockaddr_in *sin;
399 
400 			sin = satosin(ifa->ifa_addr);
401 			if (sin && sin->sin_addr.s_addr)
402 				tp->tun_flags |= TUN_IASET;
403 
404 			if (ifp->if_flags & IFF_POINTOPOINT) {
405 				sin = satosin(ifa->ifa_dstaddr);
406 				if (sin && sin->sin_addr.s_addr)
407 					tp->tun_flags |= TUN_DSTADDR;
408 			}
409 		}
410 #endif
411 #ifdef INET6
412 		if (ifa->ifa_addr->sa_family == AF_INET6) {
413 			struct sockaddr_in6 *sin;
414 
415 			sin = (struct sockaddr_in6 *)ifa->ifa_addr;
416 			if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
417 				tp->tun_flags |= TUN_IASET;
418 
419 			if (ifp->if_flags & IFF_POINTOPOINT) {
420 				sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
421 				if (sin &&
422 				    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
423 					tp->tun_flags |= TUN_DSTADDR;
424 			} else
425 				tp->tun_flags &= ~TUN_DSTADDR;
426 		}
427 #endif /* INET6 */
428 	}
429 
430 	simple_unlock(&tp->tun_lock);
431 	return;
432 }
433 
434 /*
435  * Process an ioctl request.
436  */
437 static int
438 tun_ioctl(struct ifnet *ifp, u_long cmd, void *data)
439 {
440 	int		error = 0, s;
441 	struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
442 	struct ifreq *ifr = data;
443 
444 	s = splnet();
445 
446 	switch (cmd) {
447 	case SIOCINITIFADDR:
448 		tuninit(tp);
449 		TUNDEBUG("%s: address set\n", ifp->if_xname);
450 		break;
451 	case SIOCSIFDSTADDR:
452 		tuninit(tp);
453 		TUNDEBUG("%s: destination address set\n", ifp->if_xname);
454 		break;
455 	case SIOCSIFBRDADDR:
456 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
457 		break;
458 	case SIOCSIFMTU:
459 		if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
460 			error = EINVAL;
461 			break;
462 		}
463 		TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
464 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
465 			error = 0;
466 		break;
467 	case SIOCADDMULTI:
468 	case SIOCDELMULTI:
469 		if (ifr == NULL) {
470 	        	error = EAFNOSUPPORT;           /* XXX */
471 			break;
472 		}
473 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
474 #ifdef INET
475 		case AF_INET:
476 			break;
477 #endif
478 #ifdef INET6
479 		case AF_INET6:
480 			break;
481 #endif
482 		default:
483 			error = EAFNOSUPPORT;
484 			break;
485 		}
486 		break;
487 	default:
488 		error = ifioctl_common(ifp, cmd, data);
489 	}
490 
491 	splx(s);
492 	return (error);
493 }
494 
495 /*
496  * tun_output - queue packets from higher level ready to put out.
497  */
498 static int
499 tun_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
500     struct rtentry *rt)
501 {
502 	struct tun_softc *tp = ifp->if_softc;
503 	int		s;
504 	int		error;
505 #if defined(INET) || defined(INET6)
506 	int		mlen;
507 	uint32_t	*af;
508 #endif
509 	ALTQ_DECL(struct altq_pktattr pktattr;)
510 
511 	s = splnet();
512 	simple_lock(&tp->tun_lock);
513 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
514 
515 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
516 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
517 			  tp->tun_flags);
518 		m_freem (m0);
519 		error = EHOSTDOWN;
520 		goto out;
521 	}
522 
523 	/*
524 	 * if the queueing discipline needs packet classification,
525 	 * do it before prepending link headers.
526 	 */
527 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
528 
529 #if NBPFILTER > 0
530 	if (ifp->if_bpf)
531 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m0);
532 #endif
533 
534 	switch(dst->sa_family) {
535 #ifdef INET6
536 	case AF_INET6:
537 #endif
538 #ifdef INET
539 	case AF_INET:
540 #endif
541 #if defined(INET) || defined(INET6)
542 		if (tp->tun_flags & TUN_PREPADDR) {
543 			/* Simple link-layer header */
544 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
545 			if (m0 == NULL) {
546 				IF_DROP(&ifp->if_snd);
547 				error = ENOBUFS;
548 				goto out;
549 			}
550 			bcopy(dst, mtod(m0, char *), dst->sa_len);
551 		}
552 
553 		if (tp->tun_flags & TUN_IFHEAD) {
554 			/* Prepend the address family */
555 			M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
556 			if (m0 == NULL) {
557 				IF_DROP(&ifp->if_snd);
558 				error = ENOBUFS;
559 				goto out;
560 			}
561 			af = mtod(m0,uint32_t *);
562 			*af = htonl(dst->sa_family);
563 		} else {
564 #ifdef INET
565 			if (dst->sa_family != AF_INET)
566 #endif
567 			{
568 				m_freem(m0);
569 				error = EAFNOSUPPORT;
570 				goto out;
571 			}
572 		}
573 		/* FALLTHROUGH */
574 	case AF_UNSPEC:
575 		IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
576 		if (error) {
577 			ifp->if_collisions++;
578 			error = EAFNOSUPPORT;
579 			goto out;
580 		}
581 		mlen = m0->m_pkthdr.len;
582 		ifp->if_opackets++;
583 		ifp->if_obytes += mlen;
584 		break;
585 #endif
586 	default:
587 		m_freem(m0);
588 		error = EAFNOSUPPORT;
589 		goto out;
590 	}
591 
592 	if (tp->tun_flags & TUN_RWAIT) {
593 		tp->tun_flags &= ~TUN_RWAIT;
594 		wakeup((void *)tp);
595 	}
596 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
597 		softint_schedule(tp->tun_isih);
598 
599 	selnotify(&tp->tun_rsel, 0, 0);
600 out:
601 	simple_unlock(&tp->tun_lock);
602 	splx(s);
603 	return (0);
604 }
605 
606 static void
607 tun_i_softintr(void *cookie)
608 {
609 	struct tun_softc *tp = cookie;
610 
611 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
612 		fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
613 		    NULL);
614 }
615 
616 static void
617 tun_o_softintr(void *cookie)
618 {
619 	struct tun_softc *tp = cookie;
620 
621 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
622 		fownsignal(tp->tun_pgid, SIGIO, POLL_OUT, POLLOUT|POLLWRNORM,
623 		    NULL);
624 }
625 
626 /*
627  * the cdevsw interface is now pretty minimal.
628  */
629 int
630 tunioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
631 {
632 	struct tun_softc *tp;
633 	int s, error = 0;
634 
635 	s = splnet();
636 	tp = tun_find_unit(dev);
637 
638 	/* interface was "destroyed" already */
639 	if (tp == NULL) {
640 		error = ENXIO;
641 		goto out_nolock;
642 	}
643 
644 	switch (cmd) {
645 	case TUNSDEBUG:
646 		tundebug = *(int *)data;
647 		break;
648 
649 	case TUNGDEBUG:
650 		*(int *)data = tundebug;
651 		break;
652 
653 	case TUNSIFMODE:
654 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
655 		case IFF_POINTOPOINT:
656 		case IFF_BROADCAST:
657 			if (tp->tun_if.if_flags & IFF_UP) {
658 				error = EBUSY;
659 				goto out;
660 			}
661 			tp->tun_if.if_flags &=
662 				~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
663 			tp->tun_if.if_flags |= *(int *)data;
664 			break;
665 		default:
666 			error = EINVAL;
667 			goto out;
668 		}
669 		break;
670 
671 	case TUNSLMODE:
672 		if (*(int *)data) {
673 			tp->tun_flags |= TUN_PREPADDR;
674 			tp->tun_flags &= ~TUN_IFHEAD;
675 		} else
676 			tp->tun_flags &= ~TUN_PREPADDR;
677 		break;
678 
679 	case TUNSIFHEAD:
680 		if (*(int *)data) {
681 			tp->tun_flags |= TUN_IFHEAD;
682 			tp->tun_flags &= ~TUN_PREPADDR;
683 		} else
684 			tp->tun_flags &= ~TUN_IFHEAD;
685 		break;
686 
687 	case TUNGIFHEAD:
688 		*(int *)data = (tp->tun_flags & TUN_IFHEAD);
689 		break;
690 
691 	case FIONBIO:
692 		if (*(int *)data)
693 			tp->tun_flags |= TUN_NBIO;
694 		else
695 			tp->tun_flags &= ~TUN_NBIO;
696 		break;
697 
698 	case FIOASYNC:
699 		if (*(int *)data)
700 			tp->tun_flags |= TUN_ASYNC;
701 		else
702 			tp->tun_flags &= ~TUN_ASYNC;
703 		break;
704 
705 	case FIONREAD:
706 		if (tp->tun_if.if_snd.ifq_head)
707 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
708 		else
709 			*(int *)data = 0;
710 		break;
711 
712 	case TIOCSPGRP:
713 	case FIOSETOWN:
714 		error = fsetown(&tp->tun_pgid, cmd, data);
715 		break;
716 
717 	case TIOCGPGRP:
718 	case FIOGETOWN:
719 		error = fgetown(tp->tun_pgid, cmd, data);
720 		break;
721 
722 	default:
723 		error = ENOTTY;
724 	}
725 
726 out:
727 	simple_unlock(&tp->tun_lock);
728 out_nolock:
729 	splx(s);
730 	return (error);
731 }
732 
733 /*
734  * The cdevsw read interface - reads a packet at a time, or at
735  * least as much of a packet as can be read.
736  */
737 int
738 tunread(dev_t dev, struct uio *uio, int ioflag)
739 {
740 	struct tun_softc *tp;
741 	struct ifnet	*ifp;
742 	struct mbuf	*m, *m0;
743 	int		error = 0, len, s, index;
744 
745 	s = splnet();
746 	tp = tun_find_unit(dev);
747 
748 	/* interface was "destroyed" already */
749 	if (tp == NULL) {
750 		error = ENXIO;
751 		goto out_nolock;
752 	}
753 
754 	index = tp->tun_if.if_index;
755 	ifp = &tp->tun_if;
756 
757 	TUNDEBUG ("%s: read\n", ifp->if_xname);
758 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
759 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
760 		error = EHOSTDOWN;
761 		goto out;
762 	}
763 
764 	tp->tun_flags &= ~TUN_RWAIT;
765 
766 	do {
767 		IFQ_DEQUEUE(&ifp->if_snd, m0);
768 		if (m0 == 0) {
769 			if (tp->tun_flags & TUN_NBIO) {
770 				error = EWOULDBLOCK;
771 				goto out;
772 			}
773 			tp->tun_flags |= TUN_RWAIT;
774 			if (ltsleep((void *)tp, PZERO|PCATCH|PNORELOCK,
775 					"tunread", 0, &tp->tun_lock) != 0) {
776 				error = EINTR;
777 				goto out_nolock;
778 			} else {
779 				/*
780 				 * Maybe the interface was destroyed while
781 				 * we were sleeping, so let's ensure that
782 				 * we're looking at the same (valid) tun
783 				 * interface before looping.
784 				 */
785 				tp = tun_find_unit(dev);
786 				if (tp == NULL) {
787 					error = ENXIO;
788 					goto out_nolock;
789 				}
790 				if (tp->tun_if.if_index != index) {
791 					error = ENXIO;
792 					goto out;
793 				}
794 			}
795 		}
796 	} while (m0 == 0);
797 
798 	simple_unlock(&tp->tun_lock);
799 	splx(s);
800 
801 	/* Copy the mbuf chain */
802 	while (m0 && uio->uio_resid > 0 && error == 0) {
803 		len = min(uio->uio_resid, m0->m_len);
804 		if (len != 0)
805 			error = uiomove(mtod(m0, void *), len, uio);
806 		MFREE(m0, m);
807 		m0 = m;
808 	}
809 
810 	if (m0) {
811 		TUNDEBUG("Dropping mbuf\n");
812 		m_freem(m0);
813 	}
814 	if (error)
815 		ifp->if_ierrors++;
816 
817 	return (error);
818 
819 out:
820 	simple_unlock(&tp->tun_lock);
821 out_nolock:
822 	splx(s);
823 	return (error);
824 }
825 
826 /*
827  * the cdevsw write interface - an atomic write is a packet - or else!
828  */
829 int
830 tunwrite(dev_t dev, struct uio *uio, int ioflag)
831 {
832 	struct tun_softc *tp;
833 	struct ifnet	*ifp;
834 	struct mbuf	*top, **mp, *m;
835 	struct ifqueue	*ifq;
836 	struct sockaddr	dst;
837 	int		isr, error = 0, s, tlen, mlen;
838 	uint32_t	family;
839 
840 	s = splnet();
841 	tp = tun_find_unit(dev);
842 
843 	/* interface was "destroyed" already */
844 	if (tp == NULL) {
845 		error = ENXIO;
846 		goto out_nolock;
847 	}
848 
849 	/* Unlock until we've got the data */
850 	simple_unlock(&tp->tun_lock);
851 	splx(s);
852 
853 	ifp = &tp->tun_if;
854 
855 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
856 
857 	if (tp->tun_flags & TUN_PREPADDR) {
858 		if (uio->uio_resid < sizeof(dst)) {
859 			error = EIO;
860 			goto out0;
861 		}
862 		error = uiomove((void *)&dst, sizeof(dst), uio);
863 		if (dst.sa_len > sizeof(dst)) {
864 			/* Duh.. */
865 			char discard;
866 			int n = dst.sa_len - sizeof(dst);
867 			while (n--)
868 				if ((error = uiomove(&discard, 1, uio)) != 0) {
869 					goto out0;
870 				}
871 		}
872 	} else if (tp->tun_flags & TUN_IFHEAD) {
873 		if (uio->uio_resid < sizeof(family)){
874 			error = EIO;
875 			goto out0;
876 		}
877 		error = uiomove((void *)&family, sizeof(family), uio);
878 		dst.sa_family = ntohl(family);
879 	} else {
880 #ifdef INET
881 		dst.sa_family = AF_INET;
882 #endif
883 	}
884 
885 	if (uio->uio_resid > TUNMTU) {
886 		TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
887 		    (unsigned long)uio->uio_resid);
888 		error = EIO;
889 		goto out0;
890 	}
891 
892 	switch (dst.sa_family) {
893 #ifdef INET
894 	case AF_INET:
895 		ifq = &ipintrq;
896 		isr = NETISR_IP;
897 		break;
898 #endif
899 #ifdef INET6
900 	case AF_INET6:
901 		ifq = &ip6intrq;
902 		isr = NETISR_IPV6;
903 		break;
904 #endif
905 	default:
906 		error = EAFNOSUPPORT;
907 		goto out0;
908 	}
909 
910 	tlen = uio->uio_resid;
911 
912 	/* get a header mbuf */
913 	MGETHDR(m, M_DONTWAIT, MT_DATA);
914 	if (m == NULL) {
915 		error = ENOBUFS;
916 		goto out0;
917 	}
918 	mlen = MHLEN;
919 
920 	top = NULL;
921 	mp = &top;
922 	while (error == 0 && uio->uio_resid > 0) {
923 		m->m_len = min(mlen, uio->uio_resid);
924 		error = uiomove(mtod(m, void *), m->m_len, uio);
925 		*mp = m;
926 		mp = &m->m_next;
927 		if (error == 0 && uio->uio_resid > 0) {
928 			MGET(m, M_DONTWAIT, MT_DATA);
929 			if (m == NULL) {
930 				error = ENOBUFS;
931 				break;
932 			}
933 			mlen = MLEN;
934 		}
935 	}
936 	if (error) {
937 		if (top != NULL)
938 			m_freem (top);
939 		ifp->if_ierrors++;
940 		goto out0;
941 	}
942 
943 	top->m_pkthdr.len = tlen;
944 	top->m_pkthdr.rcvif = ifp;
945 
946 #if NBPFILTER > 0
947 	if (ifp->if_bpf)
948 		bpf_mtap_af(ifp->if_bpf, dst.sa_family, top);
949 #endif
950 
951 	s = splnet();
952 	simple_lock(&tp->tun_lock);
953 	if ((tp->tun_flags & TUN_INITED) == 0) {
954 		/* Interface was destroyed */
955 		error = ENXIO;
956 		goto out;
957 	}
958 	if (IF_QFULL(ifq)) {
959 		IF_DROP(ifq);
960 		ifp->if_collisions++;
961 		m_freem(top);
962 		error = ENOBUFS;
963 		goto out;
964 	}
965 
966 	IF_ENQUEUE(ifq, top);
967 	ifp->if_ipackets++;
968 	ifp->if_ibytes += tlen;
969 	schednetisr(isr);
970 out:
971 	simple_unlock(&tp->tun_lock);
972 out_nolock:
973 	splx(s);
974 out0:
975 	return (error);
976 }
977 
978 #ifdef ALTQ
979 /*
980  * Start packet transmission on the interface.
981  * when the interface queue is rate-limited by ALTQ or TBR,
982  * if_start is needed to drain packets from the queue in order
983  * to notify readers when outgoing packets become ready.
984  *
985  * Should be called at splnet.
986  */
987 static void
988 tunstart(struct ifnet *ifp)
989 {
990 	struct tun_softc *tp = ifp->if_softc;
991 
992 	if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
993 		return;
994 
995 	simple_lock(&tp->tun_lock);
996 	if (!IF_IS_EMPTY(&ifp->if_snd)) {
997 		if (tp->tun_flags & TUN_RWAIT) {
998 			tp->tun_flags &= ~TUN_RWAIT;
999 			wakeup((void *)tp);
1000 		}
1001 		if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
1002 			softint_schedule(tp->tun_osih);
1003 
1004 		selnotify(&tp->tun_rsel, 0, 0);
1005 	}
1006 	simple_unlock(&tp->tun_lock);
1007 }
1008 #endif /* ALTQ */
1009 /*
1010  * tunpoll - the poll interface, this is only useful on reads
1011  * really. The write detect always returns true, write never blocks
1012  * anyway, it either accepts the packet or drops it.
1013  */
1014 int
1015 tunpoll(dev_t dev, int events, struct lwp *l)
1016 {
1017 	struct tun_softc *tp;
1018 	struct ifnet	*ifp;
1019 	int		s, revents = 0;
1020 
1021 	s = splnet();
1022 	tp = tun_find_unit(dev);
1023 
1024 	/* interface was "destroyed" already */
1025 	if (tp == NULL)
1026 		goto out_nolock;
1027 
1028 	ifp = &tp->tun_if;
1029 
1030 	TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
1031 
1032 	if (events & (POLLIN | POLLRDNORM)) {
1033 		if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1034 			TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
1035 			    ifp->if_snd.ifq_len);
1036 			revents |= events & (POLLIN | POLLRDNORM);
1037 		} else {
1038 			TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
1039 			selrecord(l, &tp->tun_rsel);
1040 		}
1041 	}
1042 
1043 	if (events & (POLLOUT | POLLWRNORM))
1044 		revents |= events & (POLLOUT | POLLWRNORM);
1045 
1046 	simple_unlock(&tp->tun_lock);
1047 out_nolock:
1048 	splx(s);
1049 	return (revents);
1050 }
1051 
1052 static void
1053 filt_tunrdetach(struct knote *kn)
1054 {
1055 	struct tun_softc *tp = kn->kn_hook;
1056 	int s;
1057 
1058 	s = splnet();
1059 	SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
1060 	splx(s);
1061 }
1062 
1063 static int
1064 filt_tunread(struct knote *kn, long hint)
1065 {
1066 	struct tun_softc *tp = kn->kn_hook;
1067 	struct ifnet *ifp = &tp->tun_if;
1068 	struct mbuf *m;
1069 	int s;
1070 
1071 	s = splnet();
1072 	IF_POLL(&ifp->if_snd, m);
1073 	if (m == NULL) {
1074 		splx(s);
1075 		return (0);
1076 	}
1077 
1078 	for (kn->kn_data = 0; m != NULL; m = m->m_next)
1079 		kn->kn_data += m->m_len;
1080 
1081 	splx(s);
1082 	return (1);
1083 }
1084 
1085 static const struct filterops tunread_filtops =
1086 	{ 1, NULL, filt_tunrdetach, filt_tunread };
1087 
1088 static const struct filterops tun_seltrue_filtops =
1089 	{ 1, NULL, filt_tunrdetach, filt_seltrue };
1090 
1091 int
1092 tunkqfilter(dev_t dev, struct knote *kn)
1093 {
1094 	struct tun_softc *tp;
1095 	struct klist *klist;
1096 	int rv = 0, s;
1097 
1098 	s = splnet();
1099 	tp = tun_find_unit(dev);
1100 	if (tp == NULL)
1101 		goto out_nolock;
1102 
1103 	switch (kn->kn_filter) {
1104 	case EVFILT_READ:
1105 		klist = &tp->tun_rsel.sel_klist;
1106 		kn->kn_fop = &tunread_filtops;
1107 		break;
1108 
1109 	case EVFILT_WRITE:
1110 		klist = &tp->tun_rsel.sel_klist;
1111 		kn->kn_fop = &tun_seltrue_filtops;
1112 		break;
1113 
1114 	default:
1115 		rv = EINVAL;
1116 		goto out;
1117 	}
1118 
1119 	kn->kn_hook = tp;
1120 
1121 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1122 
1123 out:
1124 	simple_unlock(&tp->tun_lock);
1125 out_nolock:
1126 	splx(s);
1127 	return (rv);
1128 }
1129