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