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