xref: /openbsd-src/sys/net/if_tun.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: if_tun.c,v 1.126 2014/07/12 18:44:22 tedu 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 #include <net/route.h>
64 
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/if_ether.h>
70 #endif
71 
72 #ifdef PIPEX
73 #include <net/pipex.h>
74 #endif
75 
76 #include "bpfilter.h"
77 #if NBPFILTER > 0
78 #include <net/bpf.h>
79 #endif
80 
81 #include <net/if_tun.h>
82 
83 struct tun_softc {
84 	struct arpcom	arpcom;		/* ethernet common data */
85 	struct selinfo	tun_rsel;	/* read select */
86 	struct selinfo	tun_wsel;	/* write select (not used) */
87 	LIST_ENTRY(tun_softc) tun_list;	/* all tunnel interfaces */
88 	int		tun_unit;
89 	uid_t		tun_siguid;	/* uid for process that set tun_pgid */
90 	uid_t		tun_sigeuid;	/* euid for process that set tun_pgid */
91 	pid_t		tun_pgid;	/* the process group - if any */
92 	u_short		tun_flags;	/* misc flags */
93 #define tun_if	arpcom.ac_if
94 #ifdef PIPEX
95 	struct pipex_iface_context pipex_iface; /* pipex context */
96 #endif
97 };
98 
99 #ifdef	TUN_DEBUG
100 int	tundebug = TUN_DEBUG;
101 #define TUNDEBUG(a)	(tundebug? printf a : 0)
102 #else
103 #define TUNDEBUG(a)	/* (tundebug? printf a : 0) */
104 #endif
105 
106 /* Only these IFF flags are changeable by TUNSIFINFO */
107 #define TUN_IFF_FLAGS (IFF_UP|IFF_POINTOPOINT|IFF_MULTICAST|IFF_BROADCAST)
108 
109 void	tunattach(int);
110 int	tunopen(dev_t, int, int, struct proc *);
111 int	tunclose(dev_t, int, int, struct proc *);
112 int	tun_ioctl(struct ifnet *, u_long, caddr_t);
113 int	tun_output(struct ifnet *, struct mbuf *, struct sockaddr *,
114 	    struct rtentry *);
115 int	tunioctl(dev_t, u_long, caddr_t, int, struct proc *);
116 int	tunread(dev_t, struct uio *, int);
117 int	tunwrite(dev_t, struct uio *, int);
118 int	tunpoll(dev_t, int, struct proc *);
119 int	tunkqfilter(dev_t, struct knote *);
120 int	tun_clone_create(struct if_clone *, int);
121 int	tun_create(struct if_clone *, int, int);
122 int	tun_clone_destroy(struct ifnet *);
123 struct	tun_softc *tun_lookup(int);
124 void	tun_wakeup(struct tun_softc *);
125 int	tun_switch(struct tun_softc *, int);
126 
127 int	tuninit(struct tun_softc *);
128 int	filt_tunread(struct knote *, long);
129 int	filt_tunwrite(struct knote *, long);
130 void	filt_tunrdetach(struct knote *);
131 void	filt_tunwdetach(struct knote *);
132 void	tunstart(struct ifnet *);
133 void	tun_link_state(struct tun_softc *);
134 
135 struct filterops tunread_filtops =
136 	{ 1, NULL, filt_tunrdetach, filt_tunread};
137 
138 struct filterops tunwrite_filtops =
139 	{ 1, NULL, filt_tunwdetach, filt_tunwrite};
140 
141 LIST_HEAD(, tun_softc) tun_softc_list;
142 
143 struct if_clone tun_cloner =
144     IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
145 
146 void
147 tunattach(int n)
148 {
149 	LIST_INIT(&tun_softc_list);
150 	if_clone_attach(&tun_cloner);
151 #ifdef PIPEX
152 	pipex_init();
153 #endif
154 }
155 
156 int
157 tun_clone_create(struct if_clone *ifc, int unit)
158 {
159 	return (tun_create(ifc, unit, 0));
160 }
161 
162 int
163 tun_create(struct if_clone *ifc, int unit, int flags)
164 {
165 	struct tun_softc	*tp;
166 	struct ifnet		*ifp;
167 	int			 s;
168 
169 	tp = malloc(sizeof(*tp), M_DEVBUF, M_NOWAIT|M_ZERO);
170 	if (!tp)
171 		return (ENOMEM);
172 
173 	tp->tun_unit = unit;
174 	tp->tun_flags = TUN_INITED|TUN_STAYUP;
175 
176 	ifp = &tp->tun_if;
177 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "%s%d", ifc->ifc_name,
178 	    unit);
179 	ether_fakeaddr(ifp);
180 
181 	ifp->if_softc = tp;
182 	ifp->if_ioctl = tun_ioctl;
183 	ifp->if_output = tun_output;
184 	ifp->if_start = tunstart;
185 	ifp->if_hardmtu = TUNMRU;
186 	ifp->if_link_state = LINK_STATE_DOWN;
187 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
188 	IFQ_SET_READY(&ifp->if_snd);
189 
190 	if ((flags & TUN_LAYER2) == 0) {
191 		tp->tun_flags &= ~TUN_LAYER2;
192 		ifp->if_mtu = ETHERMTU;
193 		ifp->if_flags = IFF_POINTOPOINT;
194 		ifp->if_type = IFT_TUNNEL;
195 		ifp->if_hdrlen = sizeof(u_int32_t);
196 
197 		if_attach(ifp);
198 		if_alloc_sadl(ifp);
199 #if NBPFILTER > 0
200 		bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
201 #endif
202 	} else {
203 		tp->tun_flags |= TUN_LAYER2;
204 		ifp->if_flags =
205 		    (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|IFF_LINK0);
206 		ifp->if_capabilities = IFCAP_VLAN_MTU;
207 
208 		if_attach(ifp);
209 		ether_ifattach(ifp);
210 	}
211 	/* force output function to our function */
212 	ifp->if_output = tun_output;
213 
214 	s = splnet();
215 	LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list);
216 	splx(s);
217 #ifdef PIPEX
218 	pipex_iface_init(&tp->pipex_iface, ifp);
219 #endif
220 
221 	return (0);
222 }
223 
224 int
225 tun_clone_destroy(struct ifnet *ifp)
226 {
227 	struct tun_softc	*tp = ifp->if_softc;
228 	int			 s;
229 
230 #ifdef PIPEX
231 	pipex_iface_stop(&tp->pipex_iface);
232 #endif
233 	tun_wakeup(tp);
234 
235 	s = splhigh();
236 	klist_invalidate(&tp->tun_rsel.si_note);
237 	klist_invalidate(&tp->tun_wsel.si_note);
238 	splx(s);
239 
240 	s = splnet();
241 	LIST_REMOVE(tp, tun_list);
242 	splx(s);
243 
244 	if (tp->tun_flags & TUN_LAYER2)
245 		ether_ifdetach(ifp);
246 
247 	if_detach(ifp);
248 
249 	free(tp, M_DEVBUF, 0);
250 	return (0);
251 }
252 
253 struct tun_softc *
254 tun_lookup(int unit)
255 {
256 	struct tun_softc *tp;
257 
258 	LIST_FOREACH(tp, &tun_softc_list, tun_list)
259 		if (tp->tun_unit == unit)
260 			return (tp);
261 	return (NULL);
262 }
263 
264 int
265 tun_switch(struct tun_softc *tp, int flags)
266 {
267 	struct ifnet		*ifp = &tp->tun_if;
268 	int			 unit, open, r, s;
269 	struct ifg_list		*ifgl;
270 	u_int			ifgr_len;
271 	char			*ifgrpnames, *p;
272 
273 	if ((tp->tun_flags & TUN_LAYER2) == (flags & TUN_LAYER2))
274 		return (0);
275 
276 	/* tp will be removed so store unit number */
277 	unit = tp->tun_unit;
278 	open = tp->tun_flags & (TUN_OPEN|TUN_NBIO|TUN_ASYNC);
279 	TUNDEBUG(("%s: switching to layer %d\n", ifp->if_xname,
280 		    flags & TUN_LAYER2 ? 2 : 3));
281 
282 	/* remember joined groups */
283 	ifgr_len = 0;
284 	ifgrpnames = NULL;
285 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
286 		ifgr_len += IFNAMSIZ;
287 	if (ifgr_len)
288 		ifgrpnames = malloc(ifgr_len + 1, M_TEMP, M_NOWAIT|M_ZERO);
289 	if (ifgrpnames) {
290 		p = ifgrpnames;
291 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
292 			strlcpy(p, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
293 			p += IFNAMSIZ;
294 		}
295 	}
296 
297 	/* remove old device and ... */
298 	tun_clone_destroy(ifp);
299 	/* attach new interface */
300 	r = tun_create(&tun_cloner, unit, flags);
301 
302 	if (r == 0) {
303 		if ((tp = tun_lookup(unit)) == NULL) {
304 			/* this should never fail */
305 			r = ENXIO;
306 			goto abort;
307 		}
308 
309 		/* rejoin groups */
310 		ifp = &tp->tun_if;
311 		for (p = ifgrpnames; p && *p; p += IFNAMSIZ)
312 			if_addgroup(ifp, p);
313 	}
314 	if (open && r == 0) {
315 		/* already opened before ifconfig tunX link0 */
316 		s = splnet();
317 		tp->tun_flags |= open;
318 		ifp->if_flags |= IFF_RUNNING;
319 		tun_link_state(tp);
320 		splx(s);
321 		TUNDEBUG(("%s: already open\n", tp->tun_if.if_xname));
322 	}
323  abort:
324 	if (ifgrpnames)
325 		free(ifgrpnames, M_TEMP, 0);
326 	return (r);
327 }
328 
329 /*
330  * tunnel open - must be superuser & the device must be
331  * configured in
332  */
333 int
334 tunopen(dev_t dev, int flag, int mode, struct proc *p)
335 {
336 	struct tun_softc	*tp;
337 	struct ifnet		*ifp;
338 	int			 error, s;
339 
340 	if ((tp = tun_lookup(minor(dev))) == NULL) {	/* create on demand */
341 		char	xname[IFNAMSIZ];
342 
343 		snprintf(xname, sizeof(xname), "%s%d", "tun", minor(dev));
344 		if ((error = if_clone_create(xname)) != 0)
345 			return (error);
346 
347 		if ((tp = tun_lookup(minor(dev))) == NULL)
348 			return (ENXIO);
349 		tp->tun_flags &= ~TUN_STAYUP;
350 	}
351 
352 	if (tp->tun_flags & TUN_OPEN)
353 		return (EBUSY);
354 
355 	ifp = &tp->tun_if;
356 	tp->tun_flags |= TUN_OPEN;
357 
358 	/* automatically mark the interface running on open */
359 	s = splnet();
360 	ifp->if_flags |= IFF_RUNNING;
361 	tun_link_state(tp);
362 	splx(s);
363 
364 	TUNDEBUG(("%s: open\n", ifp->if_xname));
365 	return (0);
366 }
367 
368 /*
369  * tunclose - close the device; if closing the real device, flush pending
370  *  output and unless STAYUP bring down and destroy the interface.
371  */
372 int
373 tunclose(dev_t dev, int flag, int mode, struct proc *p)
374 {
375 	int			 s;
376 	struct tun_softc	*tp;
377 	struct ifnet		*ifp;
378 
379 	if ((tp = tun_lookup(minor(dev))) == NULL)
380 		return (ENXIO);
381 
382 	ifp = &tp->tun_if;
383 	tp->tun_flags &= ~(TUN_OPEN|TUN_NBIO|TUN_ASYNC);
384 
385 	/*
386 	 * junk all pending output
387 	 */
388 	s = splnet();
389 	ifp->if_flags &= ~IFF_RUNNING;
390 	tun_link_state(tp);
391 	IFQ_PURGE(&ifp->if_snd);
392 	splx(s);
393 
394 	TUNDEBUG(("%s: closed\n", ifp->if_xname));
395 
396 	if (!(tp->tun_flags & TUN_STAYUP))
397 		return (if_clone_destroy(ifp->if_xname));
398 	else {
399 		tp->tun_pgid = 0;
400 		selwakeup(&tp->tun_rsel);
401 	}
402 
403 	return (0);
404 }
405 
406 int
407 tuninit(struct tun_softc *tp)
408 {
409 	struct ifnet	*ifp = &tp->tun_if;
410 	struct ifaddr	*ifa;
411 
412 	TUNDEBUG(("%s: tuninit\n", ifp->if_xname));
413 
414 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
415 	ifp->if_flags &= ~IFF_OACTIVE; /* we are never active */
416 
417 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR|TUN_BRDADDR);
418 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
419 #ifdef INET
420 		if (ifa->ifa_addr->sa_family == AF_INET) {
421 			struct sockaddr_in *sin;
422 
423 			sin = satosin(ifa->ifa_addr);
424 			if (sin && sin->sin_addr.s_addr)
425 				tp->tun_flags |= TUN_IASET;
426 
427 			if (ifp->if_flags & IFF_POINTOPOINT) {
428 				sin = satosin(ifa->ifa_dstaddr);
429 				if (sin && sin->sin_addr.s_addr)
430 					tp->tun_flags |= TUN_DSTADDR;
431 			} else
432 				tp->tun_flags &= ~TUN_DSTADDR;
433 
434 			if (ifp->if_flags & IFF_BROADCAST) {
435 				sin = satosin(ifa->ifa_broadaddr);
436 				if (sin && sin->sin_addr.s_addr)
437 					tp->tun_flags |= TUN_BRDADDR;
438 			} else
439 				tp->tun_flags &= ~TUN_BRDADDR;
440 		}
441 #endif
442 #ifdef INET6
443 		if (ifa->ifa_addr->sa_family == AF_INET6) {
444 			struct sockaddr_in6 *sin;
445 
446 			sin = (struct sockaddr_in6 *)ifa->ifa_addr;
447 			if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
448 				tp->tun_flags |= TUN_IASET;
449 
450 			if (ifp->if_flags & IFF_POINTOPOINT) {
451 				sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
452 				if (sin &&
453 				    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
454 					tp->tun_flags |= TUN_DSTADDR;
455 			} else
456 				tp->tun_flags &= ~TUN_DSTADDR;
457 		}
458 #endif /* INET6 */
459 	}
460 
461 	return (0);
462 }
463 
464 /*
465  * Process an ioctl request.
466  */
467 int
468 tun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
469 {
470 	struct tun_softc	*tp = (struct tun_softc *)(ifp->if_softc);
471 	struct ifreq		*ifr = (struct ifreq *)data;
472 	struct ifaddr		*ifa = (struct ifaddr *)data;
473 	int			 error = 0, s;
474 
475 	s = splnet();
476 
477 	switch (cmd) {
478 	case SIOCSIFADDR:
479 		tuninit(tp);
480 		TUNDEBUG(("%s: address set\n", ifp->if_xname));
481 		if (tp->tun_flags & TUN_LAYER2) {
482 			switch (ifa->ifa_addr->sa_family) {
483 #ifdef INET
484 			case AF_INET:
485 				arp_ifinit(&tp->arpcom, ifa);
486 				break;
487 #endif
488 			default:
489 				break;
490 			}
491 		} else {
492 			ifa->ifa_rtrequest = p2p_rtrequest;
493 		}
494 		break;
495 	case SIOCSIFDSTADDR:
496 		tuninit(tp);
497 		TUNDEBUG(("%s: destination address set\n", ifp->if_xname));
498 		break;
499 	case SIOCSIFMTU:
500 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > TUNMRU)
501 			error = EINVAL;
502 		else
503 			ifp->if_mtu = ifr->ifr_mtu;
504 		break;
505 	case SIOCADDMULTI:
506 	case SIOCDELMULTI:
507 		break;
508 	case SIOCSIFFLAGS:
509 		error = tun_switch(tp,
510 		    ifp->if_flags & IFF_LINK0 ? TUN_LAYER2 : 0);
511 		break;
512 	default:
513 		if (tp->tun_flags & TUN_LAYER2)
514 			error = ether_ioctl(ifp, &tp->arpcom, cmd, data);
515 		else
516 			error = ENOTTY;
517 	}
518 
519 	splx(s);
520 	return (error);
521 }
522 
523 /*
524  * tun_output - queue packets from higher level ready to put out.
525  */
526 int
527 tun_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
528     struct rtentry *rt)
529 {
530 	struct tun_softc	*tp = ifp->if_softc;
531 	int			 s, len, error;
532 	u_int32_t		*af;
533 
534 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
535 		m_freem(m0);
536 		return (EHOSTDOWN);
537 	}
538 
539 	TUNDEBUG(("%s: tun_output\n", ifp->if_xname));
540 
541 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
542 		TUNDEBUG(("%s: not ready %#x\n", ifp->if_xname,
543 		     tp->tun_flags));
544 		m_freem(m0);
545 		return (EHOSTDOWN);
546 	}
547 
548 	if (tp->tun_flags & TUN_LAYER2)
549 		/* call ether_output and that will call tunstart at the end */
550 		return (ether_output(ifp, m0, dst, rt));
551 
552 	M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
553 	if (m0 == NULL)
554 		return (ENOBUFS);
555 	af = mtod(m0, u_int32_t *);
556 	*af = htonl(dst->sa_family);
557 
558 	s = splnet();
559 
560 #if NBPFILTER > 0
561 	if (ifp->if_bpf)
562 		bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
563 #endif
564 #ifdef PIPEX
565 	if ((m0 = pipex_output(m0, dst->sa_family, sizeof(u_int32_t),
566 	    &tp->pipex_iface)) == NULL) {
567 		splx(s);
568 		return (0);
569 	}
570 #endif
571 
572 	len = m0->m_pkthdr.len;
573 	IFQ_ENQUEUE(&ifp->if_snd, m0, NULL, error);
574 	if (error) {
575 		splx(s);
576 		ifp->if_collisions++;
577 		return (error);
578 	}
579 	splx(s);
580 	ifp->if_opackets++;
581 	ifp->if_obytes += len;
582 
583 	tun_wakeup(tp);
584 	return (0);
585 }
586 
587 void
588 tun_wakeup(struct tun_softc *tp)
589 {
590 	if (tp->tun_flags & TUN_RWAIT) {
591 		tp->tun_flags &= ~TUN_RWAIT;
592 		wakeup((caddr_t)tp);
593 	}
594 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
595 		csignal(tp->tun_pgid, SIGIO,
596 		    tp->tun_siguid, tp->tun_sigeuid);
597 	selwakeup(&tp->tun_rsel);
598 }
599 
600 /*
601  * the cdevsw interface is now pretty minimal.
602  */
603 int
604 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
605 {
606 	int			 s;
607 	struct tun_softc	*tp;
608 	struct tuninfo		*tunp;
609 	struct mbuf		*m;
610 
611 	if ((tp = tun_lookup(minor(dev))) == NULL)
612 		return (ENXIO);
613 
614 	s = splnet();
615 	switch (cmd) {
616 	case TUNSIFINFO:
617 		tunp = (struct tuninfo *)data;
618 		if (tunp->mtu < ETHERMIN || tunp->mtu > TUNMRU) {
619 			splx(s);
620 			return (EINVAL);
621 		}
622 		tp->tun_if.if_mtu = tunp->mtu;
623 		tp->tun_if.if_type = tunp->type;
624 		tp->tun_if.if_flags =
625 		    (tunp->flags & TUN_IFF_FLAGS) |
626 		    (tp->tun_if.if_flags & ~TUN_IFF_FLAGS);
627 		tp->tun_if.if_baudrate = tunp->baudrate;
628 		break;
629 	case TUNGIFINFO:
630 		tunp = (struct tuninfo *)data;
631 		tunp->mtu = tp->tun_if.if_mtu;
632 		tunp->type = tp->tun_if.if_type;
633 		tunp->flags = tp->tun_if.if_flags;
634 		tunp->baudrate = tp->tun_if.if_baudrate;
635 		break;
636 #ifdef TUN_DEBUG
637 	case TUNSDEBUG:
638 		tundebug = *(int *)data;
639 		break;
640 	case TUNGDEBUG:
641 		*(int *)data = tundebug;
642 		break;
643 #endif
644 	case TUNSIFMODE:
645 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
646 		case IFF_POINTOPOINT:
647 		case IFF_BROADCAST:
648 			tp->tun_if.if_flags &= ~TUN_IFF_FLAGS;
649 			tp->tun_if.if_flags |= *(int *)data & TUN_IFF_FLAGS;
650 			break;
651 		default:
652 			splx(s);
653 			return (EINVAL);
654 		}
655 		break;
656 
657 	case FIONBIO:
658 		if (*(int *)data)
659 			tp->tun_flags |= TUN_NBIO;
660 		else
661 			tp->tun_flags &= ~TUN_NBIO;
662 		break;
663 	case FIOASYNC:
664 		if (*(int *)data)
665 			tp->tun_flags |= TUN_ASYNC;
666 		else
667 			tp->tun_flags &= ~TUN_ASYNC;
668 		break;
669 	case FIONREAD:
670 		IFQ_POLL(&tp->tun_if.if_snd, m);
671 		if (m != NULL)
672 			*(int *)data = m->m_pkthdr.len;
673 		else
674 			*(int *)data = 0;
675 		break;
676 	case TIOCSPGRP:
677 		tp->tun_pgid = *(int *)data;
678 		tp->tun_siguid = p->p_ucred->cr_ruid;
679 		tp->tun_sigeuid = p->p_ucred->cr_uid;
680 		break;
681 	case TIOCGPGRP:
682 		*(int *)data = tp->tun_pgid;
683 		break;
684 	case OSIOCGIFADDR:
685 	case SIOCGIFADDR:
686 		if (!(tp->tun_flags & TUN_LAYER2)) {
687 			splx(s);
688 			return (EINVAL);
689 		}
690 		bcopy(tp->arpcom.ac_enaddr, data,
691 		    sizeof(tp->arpcom.ac_enaddr));
692 		break;
693 
694 	case SIOCSIFADDR:
695 		if (!(tp->tun_flags & TUN_LAYER2)) {
696 			splx(s);
697 			return (EINVAL);
698 		}
699 		bcopy(data, tp->arpcom.ac_enaddr,
700 		    sizeof(tp->arpcom.ac_enaddr));
701 		break;
702 	default:
703 #ifdef PIPEX
704 	    {
705 		int ret;
706 		ret = pipex_ioctl(&tp->pipex_iface, cmd, data);
707 		splx(s);
708 		return (ret);
709 	    }
710 #else
711 		splx(s);
712 		return (ENOTTY);
713 #endif
714 	}
715 	splx(s);
716 	return (0);
717 }
718 
719 /*
720  * The cdevsw read interface - reads a packet at a time, or at
721  * least as much of a packet as can be read.
722  */
723 int
724 tunread(dev_t dev, struct uio *uio, int ioflag)
725 {
726 	struct tun_softc	*tp;
727 	struct ifnet		*ifp;
728 	struct mbuf		*m, *m0;
729 	int			 error = 0, len, s;
730 
731 	if ((tp = tun_lookup(minor(dev))) == NULL)
732 		return (ENXIO);
733 
734 	ifp = &tp->tun_if;
735 	TUNDEBUG(("%s: read\n", ifp->if_xname));
736 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
737 		TUNDEBUG(("%s: not ready %#x\n", ifp->if_xname, tp->tun_flags));
738 		return (EHOSTDOWN);
739 	}
740 
741 	tp->tun_flags &= ~TUN_RWAIT;
742 
743 	s = splnet();
744 	do {
745 		while ((tp->tun_flags & TUN_READY) != TUN_READY)
746 			if ((error = tsleep((caddr_t)tp,
747 			    (PZERO + 1)|PCATCH, "tunread", 0)) != 0) {
748 				splx(s);
749 				return (error);
750 			}
751 		IFQ_DEQUEUE(&ifp->if_snd, m0);
752 		if (m0 == NULL) {
753 			if (tp->tun_flags & TUN_NBIO && ioflag & IO_NDELAY) {
754 				splx(s);
755 				return (EWOULDBLOCK);
756 			}
757 			tp->tun_flags |= TUN_RWAIT;
758 			if ((error = tsleep((caddr_t)tp,
759 			    (PZERO + 1)|PCATCH, "tunread", 0)) != 0) {
760 				splx(s);
761 				return (error);
762 			}
763 		}
764 	} while (m0 == NULL);
765 	splx(s);
766 
767 	while (m0 != NULL && uio->uio_resid > 0 && error == 0) {
768 		len = min(uio->uio_resid, m0->m_len);
769 		if (len != 0)
770 			error = uiomove(mtod(m0, caddr_t), len, uio);
771 		MFREE(m0, m);
772 		m0 = m;
773 	}
774 
775 	if (m0 != NULL) {
776 		TUNDEBUG(("Dropping mbuf\n"));
777 		m_freem(m0);
778 	}
779 	if (error)
780 		ifp->if_oerrors++;
781 
782 	return (error);
783 }
784 
785 /*
786  * the cdevsw write interface - an atomic write is a packet - or else!
787  */
788 int
789 tunwrite(dev_t dev, struct uio *uio, int ioflag)
790 {
791 	struct tun_softc	*tp;
792 	struct ifnet		*ifp;
793 	struct ifqueue		*ifq;
794 	u_int32_t		*th;
795 	struct mbuf		*top, **mp, *m;
796 	int			 isr;
797 	int			 error=0, s, tlen, mlen;
798 
799 	if ((tp = tun_lookup(minor(dev))) == NULL)
800 		return (ENXIO);
801 
802 	ifp = &tp->tun_if;
803 	TUNDEBUG(("%s: tunwrite\n", ifp->if_xname));
804 
805 	if (uio->uio_resid == 0 || uio->uio_resid > ifp->if_mtu +
806 	    (tp->tun_flags & TUN_LAYER2 ? ETHER_HDR_LEN : sizeof(*th))) {
807 		TUNDEBUG(("%s: len=%d!\n", ifp->if_xname, uio->uio_resid));
808 		return (EMSGSIZE);
809 	}
810 	tlen = uio->uio_resid;
811 
812 	/* get a header mbuf */
813 	MGETHDR(m, M_DONTWAIT, MT_DATA);
814 	if (m == NULL)
815 		return (ENOBUFS);
816 	mlen = MHLEN;
817 	if (uio->uio_resid >= MINCLSIZE) {
818 		MCLGET(m, M_DONTWAIT);
819 		if (!(m->m_flags & M_EXT)) {
820 			m_free(m);
821 			return (ENOBUFS);
822 		}
823 		mlen = MCLBYTES;
824 	}
825 
826 	top = NULL;
827 	mp = &top;
828 	if (tp->tun_flags & TUN_LAYER2) {
829 		/*
830 		 * Pad so that IP header is correctly aligned
831 		 * this is necessary for all strict aligned architectures.
832 		 */
833 		mlen -= ETHER_ALIGN;
834 		m->m_data += ETHER_ALIGN;
835 	}
836 	while (error == 0 && uio->uio_resid > 0) {
837 		m->m_len = min(mlen, uio->uio_resid);
838 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
839 		*mp = m;
840 		mp = &m->m_next;
841 		if (error == 0 && uio->uio_resid > 0) {
842 			MGET(m, M_DONTWAIT, MT_DATA);
843 			if (m == NULL) {
844 				error = ENOBUFS;
845 				break;
846 			}
847 			mlen = MLEN;
848 			if (uio->uio_resid >= MINCLSIZE) {
849 				MCLGET(m, M_DONTWAIT);
850 				if (!(m->m_flags & M_EXT)) {
851 					error = ENOBUFS;
852 					m_free(m);
853 					break;
854 				}
855 				mlen = MCLBYTES;
856 			}
857 		}
858 	}
859 	if (error) {
860 		if (top != NULL)
861 			m_freem(top);
862 		ifp->if_ierrors++;
863 		return (error);
864 	}
865 
866 	top->m_pkthdr.len = tlen;
867 	top->m_pkthdr.rcvif = ifp;
868 
869 #if NBPFILTER > 0
870 	if (ifp->if_bpf) {
871 		s = splnet();
872 		bpf_mtap(ifp->if_bpf, top, BPF_DIRECTION_IN);
873 		splx(s);
874 	}
875 #endif
876 
877 	if (tp->tun_flags & TUN_LAYER2) {
878 		/* quirk to not add randomness from a virtual device */
879 		atomic_setbits_int(&netisr, (1 << NETISR_RND_DONE));
880 
881 		s = splnet();
882 		ether_input_mbuf(ifp, top);
883 		splx(s);
884 
885 		ifp->if_ipackets++; /* ibytes are counted in ether_input */
886 
887 		return (0);
888 	}
889 
890 	th = mtod(top, u_int32_t *);
891 	/* strip the tunnel header */
892 	top->m_data += sizeof(*th);
893 	top->m_len  -= sizeof(*th);
894 	top->m_pkthdr.len -= sizeof(*th);
895 	top->m_pkthdr.ph_rtableid = ifp->if_rdomain;
896 
897 	switch (ntohl(*th)) {
898 #ifdef INET
899 	case AF_INET:
900 		ifq = &ipintrq;
901 		isr = NETISR_IP;
902 		break;
903 #endif
904 #ifdef INET6
905 	case AF_INET6:
906 		ifq = &ip6intrq;
907 		isr = NETISR_IPV6;
908 		break;
909 #endif
910 	default:
911 		m_freem(top);
912 		return (EAFNOSUPPORT);
913 	}
914 
915 	s = splnet();
916 	if (IF_QFULL(ifq)) {
917 		IF_DROP(ifq);
918 		splx(s);
919 		ifp->if_collisions++;
920 		m_freem(top);
921 		if (!ifq->ifq_congestion)
922 			if_congestion(ifq);
923 		return (ENOBUFS);
924 	}
925 	IF_ENQUEUE(ifq, top);
926 	schednetisr(isr);
927 	ifp->if_ipackets++;
928 	ifp->if_ibytes += top->m_pkthdr.len;
929 	splx(s);
930 	return (error);
931 }
932 
933 /*
934  * tunpoll - the poll interface, this is only useful on reads
935  * really. The write detect always returns true, write never blocks
936  * anyway, it either accepts the packet or drops it.
937  */
938 int
939 tunpoll(dev_t dev, int events, struct proc *p)
940 {
941 	int			 revents, s;
942 	struct tun_softc	*tp;
943 	struct ifnet		*ifp;
944 	struct mbuf		*m;
945 
946 	if ((tp = tun_lookup(minor(dev))) == NULL)
947 		return (POLLERR);
948 
949 	ifp = &tp->tun_if;
950 	revents = 0;
951 	s = splnet();
952 	TUNDEBUG(("%s: tunpoll\n", ifp->if_xname));
953 
954 	if (events & (POLLIN | POLLRDNORM)) {
955 		IFQ_POLL(&ifp->if_snd, m);
956 		if (m != NULL) {
957 			TUNDEBUG(("%s: tunselect q=%d\n", ifp->if_xname,
958 			    IFQ_LEN(ifp->if_snd)));
959 			revents |= events & (POLLIN | POLLRDNORM);
960 		} else {
961 			TUNDEBUG(("%s: tunpoll waiting\n", ifp->if_xname));
962 			selrecord(p, &tp->tun_rsel);
963 		}
964 	}
965 	if (events & (POLLOUT | POLLWRNORM))
966 		revents |= events & (POLLOUT | POLLWRNORM);
967 	splx(s);
968 	return (revents);
969 }
970 
971 /*
972  * kqueue(2) support.
973  *
974  * The tun driver uses an array of tun_softc's based on the minor number
975  * of the device.  kn->kn_hook gets set to the specific tun_softc.
976  *
977  * filt_tunread() sets kn->kn_data to the iface qsize
978  * filt_tunwrite() sets kn->kn_data to the MTU size
979  */
980 int
981 tunkqfilter(dev_t dev, struct knote *kn)
982 {
983 	int			 s;
984 	struct klist		*klist;
985 	struct tun_softc	*tp;
986 	struct ifnet		*ifp;
987 
988 	if ((tp = tun_lookup(minor(dev))) == NULL)
989 		return (ENXIO);
990 
991 	ifp = &tp->tun_if;
992 
993 	s = splnet();
994 	TUNDEBUG(("%s: tunkqfilter\n", ifp->if_xname));
995 	splx(s);
996 
997 	switch (kn->kn_filter) {
998 		case EVFILT_READ:
999 			klist = &tp->tun_rsel.si_note;
1000 			kn->kn_fop = &tunread_filtops;
1001 			break;
1002 		case EVFILT_WRITE:
1003 			klist = &tp->tun_wsel.si_note;
1004 			kn->kn_fop = &tunwrite_filtops;
1005 			break;
1006 		default:
1007 			return (EINVAL);
1008 	}
1009 
1010 	kn->kn_hook = (caddr_t)tp;
1011 
1012 	s = splhigh();
1013 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1014 	splx(s);
1015 
1016 	return (0);
1017 }
1018 
1019 void
1020 filt_tunrdetach(struct knote *kn)
1021 {
1022 	int			 s;
1023 	struct tun_softc	*tp;
1024 
1025 	tp = (struct tun_softc *)kn->kn_hook;
1026 	s = splhigh();
1027 	if (!(kn->kn_status & KN_DETACHED))
1028 		SLIST_REMOVE(&tp->tun_rsel.si_note, kn, knote, kn_selnext);
1029 	splx(s);
1030 }
1031 
1032 int
1033 filt_tunread(struct knote *kn, long hint)
1034 {
1035 	int			 s;
1036 	struct tun_softc	*tp;
1037 	struct ifnet		*ifp;
1038 	struct mbuf		*m;
1039 
1040 	if (kn->kn_status & KN_DETACHED) {
1041 		kn->kn_data = 0;
1042 		return (1);
1043 	}
1044 
1045 	tp = (struct tun_softc *)kn->kn_hook;
1046 	ifp = &tp->tun_if;
1047 
1048 	s = splnet();
1049 	IFQ_POLL(&ifp->if_snd, m);
1050 	if (m != NULL) {
1051 		splx(s);
1052 		kn->kn_data = IFQ_LEN(&ifp->if_snd);
1053 
1054 		TUNDEBUG(("%s: tunkqread q=%d\n", ifp->if_xname,
1055 		    IFQ_LEN(&ifp->if_snd)));
1056 		return (1);
1057 	}
1058 	splx(s);
1059 	TUNDEBUG(("%s: tunkqread waiting\n", ifp->if_xname));
1060 	return (0);
1061 }
1062 
1063 void
1064 filt_tunwdetach(struct knote *kn)
1065 {
1066 	int			 s;
1067 	struct tun_softc	*tp;
1068 
1069 	tp = (struct tun_softc *)kn->kn_hook;
1070 	s = splhigh();
1071 	if (!(kn->kn_status & KN_DETACHED))
1072 		SLIST_REMOVE(&tp->tun_wsel.si_note, kn, knote, kn_selnext);
1073 	splx(s);
1074 }
1075 
1076 int
1077 filt_tunwrite(struct knote *kn, long hint)
1078 {
1079 	struct tun_softc	*tp;
1080 	struct ifnet		*ifp;
1081 
1082 	if (kn->kn_status & KN_DETACHED) {
1083 		kn->kn_data = 0;
1084 		return (1);
1085 	}
1086 
1087 	tp = (struct tun_softc *)kn->kn_hook;
1088 	ifp = &tp->tun_if;
1089 
1090 	kn->kn_data = ifp->if_mtu;
1091 
1092 	return (1);
1093 }
1094 
1095 /*
1096  * Start packet transmission on the interface.
1097  * In layer 2 mode this function is called from ether_output.
1098  */
1099 void
1100 tunstart(struct ifnet *ifp)
1101 {
1102 	struct tun_softc	*tp = ifp->if_softc;
1103 	struct mbuf		*m;
1104 
1105 	splassert(IPL_NET);
1106 
1107 	IFQ_POLL(&ifp->if_snd, m);
1108 	if (m != NULL) {
1109 		if (tp->tun_flags & TUN_LAYER2) {
1110 #if NBPFILTER > 0
1111 			if (ifp->if_bpf)
1112 				bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
1113 #endif
1114 			ifp->if_opackets++;
1115 		}
1116 		tun_wakeup(tp);
1117 	}
1118 }
1119 
1120 void
1121 tun_link_state(struct tun_softc *tp)
1122 {
1123 	struct ifnet *ifp = &tp->tun_if;
1124 	int link_state = LINK_STATE_DOWN;
1125 
1126 	if (tp->tun_flags & TUN_OPEN) {
1127 		if (tp->tun_flags & TUN_LAYER2)
1128 			link_state = LINK_STATE_FULL_DUPLEX;
1129 		else
1130 			link_state = LINK_STATE_UP;
1131 	}
1132 	if (ifp->if_link_state != link_state) {
1133 		ifp->if_link_state = link_state;
1134 		if_link_state_change(ifp);
1135 	}
1136 }
1137