xref: /openbsd-src/sys/net/if_tun.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: if_tun.c,v 1.168 2016/04/13 11:41:15 mpi Exp $	*/
2 /*	$NetBSD: if_tun.c,v 1.24 1996/05/07 02:40:48 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, Julian Onions <Julian.Onions@nexor.co.uk>
6  * Nottingham University 1987.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * This driver takes packets off the IP i/f and hands them up to a
32  * user process to have its wicked way with. This driver has its
33  * roots in a similar driver written by Phil Cockcroft (formerly) at
34  * UCL. This driver is based much more on read/write/select mode of
35  * operation though.
36  */
37 
38 /* #define	TUN_DEBUG	9 */
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/selinfo.h>
51 #include <sys/file.h>
52 #include <sys/time.h>
53 #include <sys/device.h>
54 #include <sys/vnode.h>
55 #include <sys/signalvar.h>
56 #include <sys/poll.h>
57 #include <sys/conf.h>
58 
59 
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 
67 #ifdef PIPEX
68 #include <net/pipex.h>
69 #endif
70 
71 #include "bpfilter.h"
72 #if NBPFILTER > 0
73 #include <net/bpf.h>
74 #endif
75 
76 #include <net/if_tun.h>
77 
78 struct tun_softc {
79 	struct arpcom	arpcom;		/* ethernet common data */
80 	struct selinfo	tun_rsel;	/* read select */
81 	struct selinfo	tun_wsel;	/* write select (not used) */
82 	LIST_ENTRY(tun_softc) entry;	/* all tunnel interfaces */
83 	int		tun_unit;
84 	uid_t		tun_siguid;	/* uid for process that set tun_pgid */
85 	uid_t		tun_sigeuid;	/* euid for process that set tun_pgid */
86 	pid_t		tun_pgid;	/* the process group - if any */
87 	u_short		tun_flags;	/* misc flags */
88 #define tun_if	arpcom.ac_if
89 #ifdef PIPEX
90 	struct pipex_iface_context pipex_iface; /* pipex context */
91 #endif
92 };
93 
94 #ifdef	TUN_DEBUG
95 int	tundebug = TUN_DEBUG;
96 #define TUNDEBUG(a)	(tundebug? printf a : 0)
97 #else
98 #define TUNDEBUG(a)	/* (tundebug? printf a : 0) */
99 #endif
100 
101 /* Only these IFF flags are changeable by TUNSIFINFO */
102 #define TUN_IFF_FLAGS (IFF_UP|IFF_POINTOPOINT|IFF_MULTICAST|IFF_BROADCAST)
103 
104 void	tunattach(int);
105 
106 /* cdev functions */
107 int	tunopen(dev_t, int, int, struct proc *);
108 int	tunclose(dev_t, int, int, struct proc *);
109 int	tunioctl(dev_t, u_long, caddr_t, int, struct proc *);
110 int	tunread(dev_t, struct uio *, int);
111 int	tunwrite(dev_t, struct uio *, int);
112 int	tunpoll(dev_t, int, struct proc *);
113 int	tunkqfilter(dev_t, struct knote *);
114 
115 int	tapopen(dev_t, int, int, struct proc *);
116 int	tapclose(dev_t, int, int, struct proc *);
117 int	tapioctl(dev_t, u_long, caddr_t, int, struct proc *);
118 int	tapread(dev_t, struct uio *, int);
119 int	tapwrite(dev_t, struct uio *, int);
120 int	tappoll(dev_t, int, struct proc *);
121 int	tapkqfilter(dev_t, struct knote *);
122 
123 int	tun_dev_open(struct tun_softc *, int, int, struct proc *);
124 int	tun_dev_close(struct tun_softc *, int, int, struct proc *);
125 int	tun_dev_ioctl(struct tun_softc *, u_long, caddr_t, int, struct proc *);
126 int	tun_dev_read(struct tun_softc *, struct uio *, int);
127 int	tun_dev_write(struct tun_softc *, struct uio *, int);
128 int	tun_dev_poll(struct tun_softc *, int, struct proc *);
129 int	tun_dev_kqfilter(struct tun_softc *, struct knote *);
130 
131 
132 int	tun_ioctl(struct ifnet *, u_long, caddr_t);
133 int	tun_output(struct ifnet *, struct mbuf *, struct sockaddr *,
134 	    struct rtentry *);
135 int	tun_clone_create(struct if_clone *, int);
136 int	tap_clone_create(struct if_clone *, int);
137 int	tun_create(struct if_clone *, int, int);
138 int	tun_clone_destroy(struct ifnet *);
139 static inline struct	tun_softc *tun_lookup(int);
140 static inline struct	tun_softc *tap_lookup(int);
141 void	tun_wakeup(struct tun_softc *);
142 int	tun_init(struct tun_softc *);
143 void	tun_start(struct ifnet *);
144 int	filt_tunread(struct knote *, long);
145 int	filt_tunwrite(struct knote *, long);
146 void	filt_tunrdetach(struct knote *);
147 void	filt_tunwdetach(struct knote *);
148 void	tun_link_state(struct tun_softc *);
149 
150 struct filterops tunread_filtops =
151 	{ 1, NULL, filt_tunrdetach, filt_tunread};
152 
153 struct filterops tunwrite_filtops =
154 	{ 1, NULL, filt_tunwdetach, filt_tunwrite};
155 
156 LIST_HEAD(, tun_softc) tun_softc_list;
157 LIST_HEAD(, tun_softc) tap_softc_list;
158 
159 struct if_clone tun_cloner =
160     IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
161 
162 struct if_clone tap_cloner =
163     IF_CLONE_INITIALIZER("tap", tap_clone_create, tun_clone_destroy);
164 
165 void
166 tunattach(int n)
167 {
168 	LIST_INIT(&tun_softc_list);
169 	LIST_INIT(&tap_softc_list);
170 	if_clone_attach(&tun_cloner);
171 	if_clone_attach(&tap_cloner);
172 #ifdef PIPEX
173 	pipex_init();
174 #endif
175 }
176 
177 int
178 tun_clone_create(struct if_clone *ifc, int unit)
179 {
180 	return (tun_create(ifc, unit, 0));
181 }
182 
183 int
184 tap_clone_create(struct if_clone *ifc, int unit)
185 {
186 	return (tun_create(ifc, unit, TUN_LAYER2));
187 }
188 
189 int
190 tun_create(struct if_clone *ifc, int unit, int flags)
191 {
192 	struct tun_softc	*tp;
193 	struct ifnet		*ifp;
194 	int			 s;
195 
196 	tp = malloc(sizeof(*tp), M_DEVBUF, M_NOWAIT|M_ZERO);
197 	if (tp == NULL)
198 		return (ENOMEM);
199 
200 	tp->tun_unit = unit;
201 	tp->tun_flags = TUN_INITED|TUN_STAYUP;
202 
203 	ifp = &tp->tun_if;
204 	snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d", ifc->ifc_name,
205 	    unit);
206 	ifp->if_softc = tp;
207 
208 	ifp->if_ioctl = tun_ioctl;
209 	ifp->if_output = tun_output;
210 	ifp->if_start = tun_start;
211 	ifp->if_hardmtu = TUNMRU;
212 	ifp->if_link_state = LINK_STATE_DOWN;
213 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
214 
215 	if ((flags & TUN_LAYER2) == 0) {
216 		tp->tun_flags &= ~TUN_LAYER2;
217 		ifp->if_mtu = ETHERMTU;
218 		ifp->if_flags = (IFF_POINTOPOINT|IFF_MULTICAST);
219 		ifp->if_type = IFT_TUNNEL;
220 		ifp->if_hdrlen = sizeof(u_int32_t);
221 		ifp->if_rtrequest = p2p_rtrequest;
222 
223 		if_attach(ifp);
224 		if_alloc_sadl(ifp);
225 #if NBPFILTER > 0
226 		bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
227 #endif
228 		s = splnet();
229 		LIST_INSERT_HEAD(&tun_softc_list, tp, entry);
230 		splx(s);
231 	} else {
232 		tp->tun_flags |= TUN_LAYER2;
233 		ether_fakeaddr(ifp);
234 		ifp->if_flags =
235 		    (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
236 		ifp->if_capabilities = IFCAP_VLAN_MTU;
237 
238 		if_attach(ifp);
239 		ether_ifattach(ifp);
240 
241 		s = splnet();
242 		LIST_INSERT_HEAD(&tap_softc_list, tp, entry);
243 		splx(s);
244 	}
245 
246 #ifdef PIPEX
247 	if ((tp->tun_flags & TUN_LAYER2) == 0)
248 		pipex_iface_init(&tp->pipex_iface, ifp);
249 #endif
250 
251 	return (0);
252 }
253 
254 int
255 tun_clone_destroy(struct ifnet *ifp)
256 {
257 	struct tun_softc	*tp = ifp->if_softc;
258 	int			 s;
259 
260 #ifdef PIPEX
261 	if ((tp->tun_flags & TUN_LAYER2) == 0)
262 		pipex_iface_fini(&tp->pipex_iface);
263 #endif
264 	tun_wakeup(tp);
265 
266 	s = splhigh();
267 	klist_invalidate(&tp->tun_rsel.si_note);
268 	klist_invalidate(&tp->tun_wsel.si_note);
269 	splx(s);
270 
271 	s = splnet();
272 	LIST_REMOVE(tp, entry);
273 	splx(s);
274 
275 	if (tp->tun_flags & TUN_LAYER2)
276 		ether_ifdetach(ifp);
277 
278 	if_detach(ifp);
279 
280 	free(tp, M_DEVBUF, 0);
281 	return (0);
282 }
283 
284 static inline struct tun_softc *
285 tun_lookup(int unit)
286 {
287 	struct tun_softc *tp;
288 
289 	LIST_FOREACH(tp, &tun_softc_list, entry)
290 		if (tp->tun_unit == unit)
291 			return (tp);
292 	return (NULL);
293 }
294 
295 static inline struct tun_softc *
296 tap_lookup(int unit)
297 {
298 	struct tun_softc *tp;
299 
300 	LIST_FOREACH(tp, &tap_softc_list, entry)
301 		if (tp->tun_unit == unit)
302 			return (tp);
303 	return (NULL);
304 }
305 
306 /*
307  * tunnel open - must be superuser & the device must be
308  * configured in
309  */
310 int
311 tunopen(dev_t dev, int flag, int mode, struct proc *p)
312 {
313 	struct tun_softc *tp;
314 	int error;
315 
316 	if ((tp = tun_lookup(minor(dev))) == NULL) {	/* create on demand */
317 		char	xname[IFNAMSIZ];
318 
319 		snprintf(xname, sizeof(xname), "%s%d", "tun", minor(dev));
320 		if ((error = if_clone_create(xname)) != 0)
321 			return (error);
322 
323 		if ((tp = tun_lookup(minor(dev))) == NULL)
324 			return (ENXIO);
325 		tp->tun_flags &= ~TUN_STAYUP;
326 	}
327 
328 	return (tun_dev_open(tp, flag, mode, p));
329 }
330 
331 int
332 tapopen(dev_t dev, int flag, int mode, struct proc *p)
333 {
334 	struct tun_softc *tp;
335 	int error;
336 
337 	if ((tp = tap_lookup(minor(dev))) == NULL) {	/* create on demand */
338 		char	xname[IFNAMSIZ];
339 
340 		snprintf(xname, sizeof(xname), "%s%d", "tap", minor(dev));
341 		if ((error = if_clone_create(xname)) != 0)
342 			return (error);
343 
344 		if ((tp = tap_lookup(minor(dev))) == NULL)
345 			return (ENXIO);
346 		tp->tun_flags &= ~TUN_STAYUP;
347 	}
348 
349 	return (tun_dev_open(tp, flag, mode, p));
350 }
351 
352 int
353 tun_dev_open(struct tun_softc *tp, int flag, int mode, struct proc *p)
354 {
355 	struct ifnet *ifp;
356 	int s;
357 
358 	if (tp->tun_flags & TUN_OPEN)
359 		return (EBUSY);
360 
361 	ifp = &tp->tun_if;
362 	tp->tun_flags |= TUN_OPEN;
363 	if (flag & FNONBLOCK)
364 		tp->tun_flags |= TUN_NBIO;
365 
366 	/* automatically mark the interface running on open */
367 	s = splnet();
368 	ifp->if_flags |= IFF_RUNNING;
369 	tun_link_state(tp);
370 	splx(s);
371 
372 	TUNDEBUG(("%s: open\n", ifp->if_xname));
373 	return (0);
374 }
375 
376 /*
377  * tunclose - close the device; if closing the real device, flush pending
378  *  output and unless STAYUP bring down and destroy the interface.
379  */
380 int
381 tunclose(dev_t dev, int flag, int mode, struct proc *p)
382 {
383 	struct tun_softc	*tp;
384 
385 	if ((tp = tun_lookup(minor(dev))) == NULL)
386 		return (ENXIO);
387 	return (tun_dev_close(tp, flag, mode, p));
388 }
389 
390 int
391 tapclose(dev_t dev, int flag, int mode, struct proc *p)
392 {
393 	struct tun_softc	*tp;
394 
395 	if ((tp = tap_lookup(minor(dev))) == NULL)
396 		return (ENXIO);
397 	return (tun_dev_close(tp, flag, mode, p));
398 }
399 
400 int
401 tun_dev_close(struct tun_softc *tp, int flag, int mode, struct proc *p)
402 {
403 	int			 s;
404 	struct ifnet		*ifp;
405 
406 	ifp = &tp->tun_if;
407 	tp->tun_flags &= ~(TUN_OPEN|TUN_NBIO|TUN_ASYNC);
408 
409 	/*
410 	 * junk all pending output
411 	 */
412 	s = splnet();
413 	ifp->if_flags &= ~IFF_RUNNING;
414 	tun_link_state(tp);
415 	IFQ_PURGE(&ifp->if_snd);
416 	splx(s);
417 
418 	TUNDEBUG(("%s: closed\n", ifp->if_xname));
419 
420 	if (!(tp->tun_flags & TUN_STAYUP))
421 		return (if_clone_destroy(ifp->if_xname));
422 	else {
423 		tp->tun_pgid = 0;
424 		selwakeup(&tp->tun_rsel);
425 	}
426 
427 	return (0);
428 }
429 
430 int
431 tun_init(struct tun_softc *tp)
432 {
433 	struct ifnet	*ifp = &tp->tun_if;
434 	struct ifaddr	*ifa;
435 
436 	TUNDEBUG(("%s: tun_init\n", ifp->if_xname));
437 
438 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
439 
440 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR|TUN_BRDADDR);
441 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
442 		if (ifa->ifa_addr->sa_family == AF_INET) {
443 			struct sockaddr_in *sin;
444 
445 			sin = satosin(ifa->ifa_addr);
446 			if (sin && sin->sin_addr.s_addr)
447 				tp->tun_flags |= TUN_IASET;
448 
449 			if (ifp->if_flags & IFF_POINTOPOINT) {
450 				sin = satosin(ifa->ifa_dstaddr);
451 				if (sin && sin->sin_addr.s_addr)
452 					tp->tun_flags |= TUN_DSTADDR;
453 			} else
454 				tp->tun_flags &= ~TUN_DSTADDR;
455 
456 			if (ifp->if_flags & IFF_BROADCAST) {
457 				sin = satosin(ifa->ifa_broadaddr);
458 				if (sin && sin->sin_addr.s_addr)
459 					tp->tun_flags |= TUN_BRDADDR;
460 			} else
461 				tp->tun_flags &= ~TUN_BRDADDR;
462 		}
463 #ifdef INET6
464 		if (ifa->ifa_addr->sa_family == AF_INET6) {
465 			struct sockaddr_in6 *sin6;
466 
467 			sin6 = satosin6(ifa->ifa_addr);
468 			if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
469 				tp->tun_flags |= TUN_IASET;
470 
471 			if (ifp->if_flags & IFF_POINTOPOINT) {
472 				sin6 = satosin6(ifa->ifa_dstaddr);
473 				if (sin6 &&
474 				    !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
475 					tp->tun_flags |= TUN_DSTADDR;
476 			} else
477 				tp->tun_flags &= ~TUN_DSTADDR;
478 		}
479 #endif /* INET6 */
480 	}
481 
482 	return (0);
483 }
484 
485 /*
486  * Process an ioctl request.
487  */
488 int
489 tun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
490 {
491 	struct tun_softc	*tp = (struct tun_softc *)(ifp->if_softc);
492 	struct ifreq		*ifr = (struct ifreq *)data;
493 	int			 error = 0, s;
494 
495 	s = splnet();
496 
497 	switch (cmd) {
498 	case SIOCSIFADDR:
499 		tun_init(tp);
500 		break;
501 	case SIOCSIFDSTADDR:
502 		tun_init(tp);
503 		TUNDEBUG(("%s: destination address set\n", ifp->if_xname));
504 		break;
505 	case SIOCSIFMTU:
506 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > TUNMRU)
507 			error = EINVAL;
508 		else
509 			ifp->if_mtu = ifr->ifr_mtu;
510 		break;
511 	case SIOCADDMULTI:
512 	case SIOCDELMULTI:
513 		break;
514 	case SIOCSIFFLAGS:
515 		break;
516 	default:
517 		if (tp->tun_flags & TUN_LAYER2)
518 			error = ether_ioctl(ifp, &tp->arpcom, cmd, data);
519 		else
520 			error = ENOTTY;
521 	}
522 
523 	splx(s);
524 	return (error);
525 }
526 
527 /*
528  * tun_output - queue packets from higher level ready to put out.
529  */
530 int
531 tun_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
532     struct rtentry *rt)
533 {
534 	struct tun_softc	*tp = ifp->if_softc;
535 	int			 s, error;
536 	u_int32_t		*af;
537 
538 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
539 		m_freem(m0);
540 		return (EHOSTDOWN);
541 	}
542 
543 	TUNDEBUG(("%s: tun_output\n", ifp->if_xname));
544 
545 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
546 		TUNDEBUG(("%s: not ready %#x\n", ifp->if_xname,
547 		     tp->tun_flags));
548 		m_freem(m0);
549 		return (EHOSTDOWN);
550 	}
551 
552 	if (tp->tun_flags & TUN_LAYER2)
553 		return (ether_output(ifp, m0, dst, rt));
554 
555 	M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
556 	if (m0 == NULL)
557 		return (ENOBUFS);
558 	af = mtod(m0, u_int32_t *);
559 	*af = htonl(dst->sa_family);
560 
561 	s = splnet();
562 
563 #if NBPFILTER > 0
564 	if (ifp->if_bpf)
565 		bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
566 #endif
567 #ifdef PIPEX
568 	if (pipex_enable && (m0 = pipex_output(m0, dst->sa_family,
569 	    sizeof(u_int32_t), &tp->pipex_iface)) == NULL) {
570 		splx(s);
571 		return (0);
572 	}
573 #endif
574 
575 	error = if_enqueue(ifp, m0);
576 	splx(s);
577 
578 	if (error) {
579 		ifp->if_collisions++;
580 		return (error);
581 	}
582 	ifp->if_opackets++;
583 
584 	tun_wakeup(tp);
585 	return (0);
586 }
587 
588 void
589 tun_wakeup(struct tun_softc *tp)
590 {
591 	if (tp->tun_flags & TUN_RWAIT) {
592 		tp->tun_flags &= ~TUN_RWAIT;
593 		wakeup((caddr_t)tp);
594 	}
595 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
596 		csignal(tp->tun_pgid, SIGIO,
597 		    tp->tun_siguid, tp->tun_sigeuid);
598 	selwakeup(&tp->tun_rsel);
599 }
600 
601 /*
602  * the cdevsw interface is now pretty minimal.
603  */
604 int
605 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
606 {
607 	struct tun_softc *tp;
608 
609 	if ((tp = tun_lookup(minor(dev))) == NULL)
610 		return (ENXIO);
611 	return (tun_dev_ioctl(tp, cmd, data, flag, p));
612 }
613 
614 int
615 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
616 {
617 	struct tun_softc *tp;
618 
619 	if ((tp = tap_lookup(minor(dev))) == NULL)
620 		return (ENXIO);
621 	return (tun_dev_ioctl(tp, cmd, data, flag, p));
622 }
623 
624 int
625 tun_dev_ioctl(struct tun_softc *tp, u_long cmd, caddr_t data, int flag,
626     struct proc *p)
627 {
628 	int			 s;
629 	struct tuninfo		*tunp;
630 	struct mbuf		*m;
631 
632 	s = splnet();
633 	switch (cmd) {
634 	case TUNSIFINFO:
635 		tunp = (struct tuninfo *)data;
636 		if (tunp->mtu < ETHERMIN || tunp->mtu > TUNMRU) {
637 			splx(s);
638 			return (EINVAL);
639 		}
640 		tp->tun_if.if_mtu = tunp->mtu;
641 		tp->tun_if.if_type = tunp->type;
642 		tp->tun_if.if_flags =
643 		    (tunp->flags & TUN_IFF_FLAGS) |
644 		    (tp->tun_if.if_flags & ~TUN_IFF_FLAGS);
645 		tp->tun_if.if_baudrate = tunp->baudrate;
646 		break;
647 	case TUNGIFINFO:
648 		tunp = (struct tuninfo *)data;
649 		tunp->mtu = tp->tun_if.if_mtu;
650 		tunp->type = tp->tun_if.if_type;
651 		tunp->flags = tp->tun_if.if_flags;
652 		tunp->baudrate = tp->tun_if.if_baudrate;
653 		break;
654 #ifdef TUN_DEBUG
655 	case TUNSDEBUG:
656 		tundebug = *(int *)data;
657 		break;
658 	case TUNGDEBUG:
659 		*(int *)data = tundebug;
660 		break;
661 #endif
662 	case TUNSIFMODE:
663 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
664 		case IFF_POINTOPOINT:
665 		case IFF_BROADCAST:
666 			tp->tun_if.if_flags &= ~TUN_IFF_FLAGS;
667 			tp->tun_if.if_flags |= *(int *)data & TUN_IFF_FLAGS;
668 			break;
669 		default:
670 			splx(s);
671 			return (EINVAL);
672 		}
673 		break;
674 
675 	case FIONBIO:
676 		if (*(int *)data)
677 			tp->tun_flags |= TUN_NBIO;
678 		else
679 			tp->tun_flags &= ~TUN_NBIO;
680 		break;
681 	case FIOASYNC:
682 		if (*(int *)data)
683 			tp->tun_flags |= TUN_ASYNC;
684 		else
685 			tp->tun_flags &= ~TUN_ASYNC;
686 		break;
687 	case FIONREAD:
688 		m = ifq_deq_begin(&tp->tun_if.if_snd);
689 		if (m != NULL) {
690 			*(int *)data = m->m_pkthdr.len;
691 			ifq_deq_rollback(&tp->tun_if.if_snd, m);
692 		} else
693 			*(int *)data = 0;
694 		break;
695 	case TIOCSPGRP:
696 		tp->tun_pgid = *(int *)data;
697 		tp->tun_siguid = p->p_ucred->cr_ruid;
698 		tp->tun_sigeuid = p->p_ucred->cr_uid;
699 		break;
700 	case TIOCGPGRP:
701 		*(int *)data = tp->tun_pgid;
702 		break;
703 	case SIOCGIFADDR:
704 		if (!(tp->tun_flags & TUN_LAYER2)) {
705 			splx(s);
706 			return (EINVAL);
707 		}
708 		bcopy(tp->arpcom.ac_enaddr, data,
709 		    sizeof(tp->arpcom.ac_enaddr));
710 		break;
711 
712 	case SIOCSIFADDR:
713 		if (!(tp->tun_flags & TUN_LAYER2)) {
714 			splx(s);
715 			return (EINVAL);
716 		}
717 		bcopy(data, tp->arpcom.ac_enaddr,
718 		    sizeof(tp->arpcom.ac_enaddr));
719 		break;
720 	default:
721 #ifdef PIPEX
722 	    	if (!(tp->tun_flags & TUN_LAYER2)) {
723 			int ret;
724 			ret = pipex_ioctl(&tp->pipex_iface, cmd, data);
725 			splx(s);
726 			return (ret);
727 		}
728 #endif
729 		splx(s);
730 		return (ENOTTY);
731 	}
732 	splx(s);
733 	return (0);
734 }
735 
736 /*
737  * The cdevsw read interface - reads a packet at a time, or at
738  * least as much of a packet as can be read.
739  */
740 int
741 tunread(dev_t dev, struct uio *uio, int ioflag)
742 {
743 	struct tun_softc *tp;
744 
745 	if ((tp = tun_lookup(minor(dev))) == NULL)
746 		return (ENXIO);
747 	return (tun_dev_read(tp, uio, ioflag));
748 }
749 
750 int
751 tapread(dev_t dev, struct uio *uio, int ioflag)
752 {
753 	struct tun_softc *tp;
754 
755 	if ((tp = tap_lookup(minor(dev))) == NULL)
756 		return (ENXIO);
757 	return (tun_dev_read(tp, uio, ioflag));
758 }
759 
760 int
761 tun_dev_read(struct tun_softc *tp, struct uio *uio, int ioflag)
762 {
763 	struct ifnet		*ifp = &tp->tun_if;
764 	struct mbuf		*m, *m0;
765 	unsigned int		 ifidx;
766 	int			 error = 0, s;
767 	size_t			 len;
768 
769 	if ((tp->tun_flags & TUN_READY) != TUN_READY)
770 		return (EHOSTDOWN);
771 
772 	ifidx = ifp->if_index;
773 	tp->tun_flags &= ~TUN_RWAIT;
774 
775 	s = splnet();
776 	do {
777 		struct ifnet *ifp1;
778 		int destroyed;
779 
780 		while ((tp->tun_flags & TUN_READY) != TUN_READY) {
781 			if ((error = tsleep((caddr_t)tp,
782 			    (PZERO + 1)|PCATCH, "tunread", 0)) != 0) {
783 				splx(s);
784 				return (error);
785 			}
786 			/* Make sure the interface still exists. */
787 			ifp1 = if_get(ifidx);
788 			destroyed = (ifp1 == NULL);
789 			if_put(ifp1);
790 			if (destroyed) {
791 				splx(s);
792 				return (ENXIO);
793 			}
794 		}
795 		IFQ_DEQUEUE(&ifp->if_snd, m0);
796 		if (m0 == NULL) {
797 			if (tp->tun_flags & TUN_NBIO && ioflag & IO_NDELAY) {
798 				splx(s);
799 				return (EWOULDBLOCK);
800 			}
801 			tp->tun_flags |= TUN_RWAIT;
802 			if ((error = tsleep((caddr_t)tp,
803 			    (PZERO + 1)|PCATCH, "tunread", 0)) != 0) {
804 				splx(s);
805 				return (error);
806 			}
807 			/* Make sure the interface still exists. */
808 			ifp1 = if_get(ifidx);
809 			destroyed = (ifp1 == NULL);
810 			if_put(ifp1);
811 			if (destroyed) {
812 				splx(s);
813 				return (ENXIO);
814 			}
815 		}
816 	} while (m0 == NULL);
817 	splx(s);
818 
819 	if (tp->tun_flags & TUN_LAYER2) {
820 #if NBPFILTER > 0
821 		if (ifp->if_bpf)
822 			bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
823 #endif
824 		ifp->if_opackets++;
825 	}
826 
827 	while (m0 != NULL && uio->uio_resid > 0 && error == 0) {
828 		len = ulmin(uio->uio_resid, m0->m_len);
829 		if (len != 0)
830 			error = uiomove(mtod(m0, caddr_t), len, uio);
831 		m = m_free(m0);
832 		m0 = m;
833 	}
834 
835 	if (m0 != NULL) {
836 		TUNDEBUG(("Dropping mbuf\n"));
837 		m_freem(m0);
838 	}
839 	if (error)
840 		ifp->if_oerrors++;
841 
842 	return (error);
843 }
844 
845 /*
846  * the cdevsw write interface - an atomic write is a packet - or else!
847  */
848 int
849 tunwrite(dev_t dev, struct uio *uio, int ioflag)
850 {
851 	struct tun_softc *tp;
852 
853 	if ((tp = tun_lookup(minor(dev))) == NULL)
854 		return (ENXIO);
855 	return (tun_dev_write(tp, uio, ioflag));
856 }
857 
858 int
859 tapwrite(dev_t dev, struct uio *uio, int ioflag)
860 {
861 	struct tun_softc *tp;
862 
863 	if ((tp = tap_lookup(minor(dev))) == NULL)
864 		return (ENXIO);
865 	return (tun_dev_write(tp, uio, ioflag));
866 }
867 
868 int
869 tun_dev_write(struct tun_softc *tp, struct uio *uio, int ioflag)
870 {
871 	struct ifnet		*ifp;
872 	struct niqueue		*ifq;
873 	u_int32_t		*th;
874 	struct mbuf		*top, **mp, *m;
875 	int			error = 0, tlen;
876 	size_t			mlen;
877 #if NBPFILTER > 0
878 	int			 s;
879 #endif
880 	ifp = &tp->tun_if;
881 	TUNDEBUG(("%s: tunwrite\n", ifp->if_xname));
882 
883 	if (uio->uio_resid == 0 || uio->uio_resid > ifp->if_mtu +
884 	    (tp->tun_flags & TUN_LAYER2 ? ETHER_HDR_LEN : sizeof(*th))) {
885 		TUNDEBUG(("%s: len=%d!\n", ifp->if_xname, uio->uio_resid));
886 		return (EMSGSIZE);
887 	}
888 	tlen = uio->uio_resid;
889 
890 	/* get a header mbuf */
891 	MGETHDR(m, M_DONTWAIT, MT_DATA);
892 	if (m == NULL)
893 		return (ENOBUFS);
894 	mlen = MHLEN;
895 	if (uio->uio_resid >= MINCLSIZE) {
896 		MCLGET(m, M_DONTWAIT);
897 		if (!(m->m_flags & M_EXT)) {
898 			m_free(m);
899 			return (ENOBUFS);
900 		}
901 		mlen = MCLBYTES;
902 	}
903 
904 	top = NULL;
905 	mp = &top;
906 	if (tp->tun_flags & TUN_LAYER2) {
907 		/*
908 		 * Pad so that IP header is correctly aligned
909 		 * this is necessary for all strict aligned architectures.
910 		 */
911 		mlen -= ETHER_ALIGN;
912 		m->m_data += ETHER_ALIGN;
913 	}
914 	while (error == 0 && uio->uio_resid > 0) {
915 		m->m_len = ulmin(mlen, uio->uio_resid);
916 		error = uiomove(mtod (m, caddr_t), 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 			if (uio->uio_resid >= MINCLSIZE) {
927 				MCLGET(m, M_DONTWAIT);
928 				if (!(m->m_flags & M_EXT)) {
929 					error = ENOBUFS;
930 					m_free(m);
931 					break;
932 				}
933 				mlen = MCLBYTES;
934 			}
935 		}
936 	}
937 	if (error) {
938 		m_freem(top);
939 		ifp->if_ierrors++;
940 		return (error);
941 	}
942 
943 	top->m_pkthdr.len = tlen;
944 
945 	if (tp->tun_flags & TUN_LAYER2) {
946 		struct mbuf_list ml = MBUF_LIST_INITIALIZER();
947 
948 		ml_enqueue(&ml, top);
949 		if_input(ifp, &ml);
950 		return (0);
951 	}
952 
953 #if NBPFILTER > 0
954 	if (ifp->if_bpf) {
955 		s = splnet();
956 		bpf_mtap(ifp->if_bpf, top, BPF_DIRECTION_IN);
957 		splx(s);
958 	}
959 #endif
960 
961 	th = mtod(top, u_int32_t *);
962 	/* strip the tunnel header */
963 	top->m_data += sizeof(*th);
964 	top->m_len  -= sizeof(*th);
965 	top->m_pkthdr.len -= sizeof(*th);
966 	top->m_pkthdr.ph_rtableid = ifp->if_rdomain;
967 	top->m_pkthdr.ph_ifidx = ifp->if_index;
968 
969 	switch (ntohl(*th)) {
970 	case AF_INET:
971 		ifq = &ipintrq;
972 		break;
973 #ifdef INET6
974 	case AF_INET6:
975 		ifq = &ip6intrq;
976 		break;
977 #endif
978 	default:
979 		m_freem(top);
980 		return (EAFNOSUPPORT);
981 	}
982 
983 	if (niq_enqueue(ifq, top) != 0) {
984 		ifp->if_collisions++;
985 		return (ENOBUFS);
986 	}
987 
988 	ifp->if_ipackets++;
989 	ifp->if_ibytes += top->m_pkthdr.len;
990 
991 	return (error);
992 }
993 
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 proc *p)
1001 {
1002 	struct tun_softc *tp;
1003 
1004 	if ((tp = tun_lookup(minor(dev))) == NULL)
1005 		return (POLLERR);
1006 	return (tun_dev_poll(tp, events, p));
1007 }
1008 
1009 int
1010 tappoll(dev_t dev, int events, struct proc *p)
1011 {
1012 	struct tun_softc *tp;
1013 
1014 	if ((tp = tap_lookup(minor(dev))) == NULL)
1015 		return (POLLERR);
1016 	return (tun_dev_poll(tp, events, p));
1017 }
1018 
1019 int
1020 tun_dev_poll(struct tun_softc *tp, int events, struct proc *p)
1021 {
1022 	int			 revents, s;
1023 	struct ifnet		*ifp;
1024 	unsigned int		 len;
1025 
1026 	ifp = &tp->tun_if;
1027 	revents = 0;
1028 	s = splnet();
1029 	TUNDEBUG(("%s: tunpoll\n", ifp->if_xname));
1030 
1031 	if (events & (POLLIN | POLLRDNORM)) {
1032 		len = IFQ_LEN(&ifp->if_snd);
1033 		if (len > 0) {
1034 			TUNDEBUG(("%s: tunselect q=%d\n", ifp->if_xname, len));
1035 			revents |= events & (POLLIN | POLLRDNORM);
1036 		} else {
1037 			TUNDEBUG(("%s: tunpoll waiting\n", ifp->if_xname));
1038 			selrecord(p, &tp->tun_rsel);
1039 		}
1040 	}
1041 	if (events & (POLLOUT | POLLWRNORM))
1042 		revents |= events & (POLLOUT | POLLWRNORM);
1043 	splx(s);
1044 	return (revents);
1045 }
1046 
1047 /*
1048  * kqueue(2) support.
1049  *
1050  * The tun driver uses an array of tun_softc's based on the minor number
1051  * of the device.  kn->kn_hook gets set to the specific tun_softc.
1052  *
1053  * filt_tunread() sets kn->kn_data to the iface qsize
1054  * filt_tunwrite() sets kn->kn_data to the MTU size
1055  */
1056 int
1057 tunkqfilter(dev_t dev, struct knote *kn)
1058 {
1059 	struct tun_softc *tp;
1060 
1061 	if ((tp = tun_lookup(minor(dev))) == NULL)
1062 		return (ENXIO);
1063 	return (tun_dev_kqfilter(tp, kn));
1064 }
1065 
1066 int
1067 tapkqfilter(dev_t dev, struct knote *kn)
1068 {
1069 	struct tun_softc *tp;
1070 
1071 	if ((tp = tap_lookup(minor(dev))) == NULL)
1072 		return (ENXIO);
1073 	return (tun_dev_kqfilter(tp, kn));
1074 }
1075 
1076 int
1077 tun_dev_kqfilter(struct tun_softc *tp, struct knote *kn)
1078 {
1079 	int			 s;
1080 	struct klist		*klist;
1081 	struct ifnet		*ifp;
1082 
1083 	ifp = &tp->tun_if;
1084 	TUNDEBUG(("%s: tunkqfilter\n", ifp->if_xname));
1085 
1086 	switch (kn->kn_filter) {
1087 		case EVFILT_READ:
1088 			klist = &tp->tun_rsel.si_note;
1089 			kn->kn_fop = &tunread_filtops;
1090 			break;
1091 		case EVFILT_WRITE:
1092 			klist = &tp->tun_wsel.si_note;
1093 			kn->kn_fop = &tunwrite_filtops;
1094 			break;
1095 		default:
1096 			return (EINVAL);
1097 	}
1098 
1099 	kn->kn_hook = (caddr_t)tp;
1100 
1101 	s = splhigh();
1102 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1103 	splx(s);
1104 
1105 	return (0);
1106 }
1107 
1108 void
1109 filt_tunrdetach(struct knote *kn)
1110 {
1111 	int			 s;
1112 	struct tun_softc	*tp;
1113 
1114 	tp = (struct tun_softc *)kn->kn_hook;
1115 	s = splhigh();
1116 	if (!(kn->kn_status & KN_DETACHED))
1117 		SLIST_REMOVE(&tp->tun_rsel.si_note, kn, knote, kn_selnext);
1118 	splx(s);
1119 }
1120 
1121 int
1122 filt_tunread(struct knote *kn, long hint)
1123 {
1124 	int			 s;
1125 	struct tun_softc	*tp;
1126 	struct ifnet		*ifp;
1127 	unsigned int		 len;
1128 
1129 	if (kn->kn_status & KN_DETACHED) {
1130 		kn->kn_data = 0;
1131 		return (1);
1132 	}
1133 
1134 	tp = (struct tun_softc *)kn->kn_hook;
1135 	ifp = &tp->tun_if;
1136 
1137 	s = splnet();
1138 	len = IFQ_LEN(&ifp->if_snd);
1139 	if (len > 0) {
1140 		splx(s);
1141 		kn->kn_data = len;
1142 
1143 		TUNDEBUG(("%s: tunkqread q=%d\n", ifp->if_xname,
1144 		    IFQ_LEN(&ifp->if_snd)));
1145 		return (1);
1146 	}
1147 	splx(s);
1148 	TUNDEBUG(("%s: tunkqread waiting\n", ifp->if_xname));
1149 	return (0);
1150 }
1151 
1152 void
1153 filt_tunwdetach(struct knote *kn)
1154 {
1155 	int			 s;
1156 	struct tun_softc	*tp;
1157 
1158 	tp = (struct tun_softc *)kn->kn_hook;
1159 	s = splhigh();
1160 	if (!(kn->kn_status & KN_DETACHED))
1161 		SLIST_REMOVE(&tp->tun_wsel.si_note, kn, knote, kn_selnext);
1162 	splx(s);
1163 }
1164 
1165 int
1166 filt_tunwrite(struct knote *kn, long hint)
1167 {
1168 	struct tun_softc	*tp;
1169 	struct ifnet		*ifp;
1170 
1171 	if (kn->kn_status & KN_DETACHED) {
1172 		kn->kn_data = 0;
1173 		return (1);
1174 	}
1175 
1176 	tp = (struct tun_softc *)kn->kn_hook;
1177 	ifp = &tp->tun_if;
1178 
1179 	kn->kn_data = ifp->if_mtu;
1180 
1181 	return (1);
1182 }
1183 
1184 void
1185 tun_start(struct ifnet *ifp)
1186 {
1187 	struct tun_softc	*tp = ifp->if_softc;
1188 
1189 	splassert(IPL_NET);
1190 
1191 	if (IFQ_LEN(&ifp->if_snd))
1192 		tun_wakeup(tp);
1193 }
1194 
1195 void
1196 tun_link_state(struct tun_softc *tp)
1197 {
1198 	struct ifnet *ifp = &tp->tun_if;
1199 	int link_state = LINK_STATE_DOWN;
1200 
1201 	if (tp->tun_flags & TUN_OPEN) {
1202 		if (tp->tun_flags & TUN_LAYER2)
1203 			link_state = LINK_STATE_FULL_DUPLEX;
1204 		else
1205 			link_state = LINK_STATE_UP;
1206 	}
1207 	if (ifp->if_link_state != link_state) {
1208 		ifp->if_link_state = link_state;
1209 		if_link_state_change(ifp);
1210 	}
1211 }
1212