xref: /dragonfly/sys/net/tun/if_tun.c (revision 73b5ca6b)
1 /*	$NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd 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 it's
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  * $FreeBSD: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $
17  */
18 
19 #include "use_tun.h"
20 #include "opt_inet.h"
21 #include "opt_inet6.h"
22 
23 #include <sys/param.h>
24 #include <sys/proc.h>
25 #include <sys/priv.h>
26 #include <sys/systm.h>
27 #include <sys/mbuf.h>
28 #include <sys/socket.h>
29 #include <sys/conf.h>
30 #include <sys/device.h>
31 #include <sys/filio.h>
32 #include <sys/sockio.h>
33 #include <sys/ttycom.h>
34 #include <sys/signalvar.h>
35 #include <sys/filedesc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38 #include <sys/uio.h>
39 #include <sys/vnode.h>
40 #include <sys/malloc.h>
41 #include <sys/mplock2.h>
42 #include <sys/devfs.h>
43 #include <sys/queue.h>
44 
45 #include <net/bpf.h>
46 #include <net/if.h>
47 #include <net/if_types.h>
48 #include <net/if_clone.h>
49 #include <net/ifq_var.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
52 
53 #ifdef INET
54 #include <netinet/in.h>
55 #endif
56 
57 #include "if_tunvar.h"
58 #include "if_tun.h"
59 
60 #if NTUN <= 1
61 #define TUN_PREALLOCATED_UNITS	4
62 #else
63 #define TUN_PREALLOCATED_UNITS	NTUN
64 #endif
65 
66 #define TUN		"tun"
67 #define TUNDEBUG	if (tundebug) if_printf
68 
69 /* module */
70 static int		tunmodevent(module_t, int, void *);
71 
72 /* device */
73 static struct tun_softc *tuncreate(cdev_t, int);
74 static void		tundestroy(struct tun_softc *sc);
75 
76 /* clone */
77 static int		tun_clone_create(struct if_clone *, int, caddr_t);
78 static int		tun_clone_destroy(struct ifnet *);
79 
80 /* network interface */
81 static int		tunifinit(struct ifnet *);
82 static void		tunifstart(struct ifnet *, struct ifaltq_subque *);
83 static int		tunifoutput(struct ifnet *, struct mbuf *,
84 				    struct sockaddr *, struct rtentry *rt);
85 static int		tunifioctl(struct ifnet *, u_long,
86 				   caddr_t, struct ucred *);
87 
88 /* character device */
89 static d_open_t		tunopen;
90 static d_close_t	tunclose;
91 static d_read_t		tunread;
92 static d_write_t	tunwrite;
93 static d_ioctl_t	tunioctl;
94 static d_kqfilter_t	tunkqfilter;
95 static d_clone_t	tunclone;
96 
97 static struct dev_ops	tun_ops = {
98 	{ TUN, 0, 0 },
99 	.d_open =	tunopen,
100 	.d_close =	tunclose,
101 	.d_read =	tunread,
102 	.d_write =	tunwrite,
103 	.d_ioctl =	tunioctl,
104 	.d_kqfilter =	tunkqfilter
105 };
106 
107 /* kqueue support */
108 static void		tun_filter_detach(struct knote *);
109 static int		tun_filter_read(struct knote *, long);
110 static int		tun_filter_write(struct knote *, long);
111 
112 static struct filterops tun_read_filtops = {
113 	FILTEROP_ISFD,
114 	NULL,
115 	tun_filter_detach,
116 	tun_filter_read
117 };
118 static struct filterops tun_write_filtops = {
119 	FILTEROP_ISFD,
120 	NULL,
121 	tun_filter_detach,
122 	tun_filter_write
123 };
124 
125 static int		tundebug = 0;	/* debug flag */
126 static int		tunrefcnt = 0;	/* module reference counter */
127 
128 static MALLOC_DEFINE(M_TUN, TUN, "Tunnel Interface");
129 
130 static DEVFS_DEFINE_CLONE_BITMAP(tun);
131 
132 struct if_clone tun_cloner = IF_CLONE_INITIALIZER(
133 	TUN, tun_clone_create, tun_clone_destroy, 0, IF_MAXUNIT);
134 
135 static SLIST_HEAD(,tun_softc) tun_listhead =
136 	SLIST_HEAD_INITIALIZER(&tun_listhead);
137 
138 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0,
139 	   "Enable debug output");
140 SYSCTL_DECL(_net_link);
141 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
142 	    "IP tunnel software network interface");
143 SYSCTL_INT(_net_link_tun, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0,
144 	   "Enable debug output");
145 SYSCTL_INT(_net_link_tun, OID_AUTO, refcnt, CTLFLAG_RD, &tunrefcnt, 0,
146 	   "Number of opened devices");
147 
148 DEV_MODULE(if_tun, tunmodevent, NULL);
149 
150 /*
151  * tunmodevent - module event handler
152  */
153 static int
154 tunmodevent(module_t mod, int type, void *data)
155 {
156 	static cdev_t dev = NULL;
157 	struct tun_softc *sc, *sc_tmp;
158 	int i;
159 
160 	switch (type) {
161 	case MOD_LOAD:
162 		dev = make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
163 					 tunclone, UID_UUCP, GID_DIALER,
164 					 0600, TUN);
165 
166 		SLIST_INIT(&tun_listhead);
167 		if_clone_attach(&tun_cloner);
168 
169 		for (i = 0; i < TUN_PREALLOCATED_UNITS; ++i) {
170 			make_dev(&tun_ops, i, UID_UUCP, GID_DIALER,
171 				 0600, "%s%d", TUN, i);
172 			devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i);
173 		}
174 		break;
175 
176 	case MOD_UNLOAD:
177 		if (tunrefcnt > 0)
178 			return (EBUSY);
179 
180 		if_clone_detach(&tun_cloner);
181 
182 		SLIST_FOREACH_MUTABLE(sc, &tun_listhead, tun_link, sc_tmp)
183 			tundestroy(sc);
184 
185 		dev_ops_remove_all(&tun_ops);
186 		destroy_autoclone_dev(dev, &DEVFS_CLONE_BITMAP(tun));
187 		break;
188 
189 	default:
190 		return (EOPNOTSUPP);
191 	}
192 
193 	return (0);
194 }
195 
196 static int
197 tunclone(struct dev_clone_args *ap)
198 {
199 	int unit;
200 
201 	unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
202 	ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER,
203 				  0600, "%s%d", TUN, unit);
204 
205 	if (tuncreate(ap->a_dev, 0) == NULL)
206 		return (ENOMEM);
207 	else
208 		return (0);
209 }
210 
211 static struct tun_softc *
212 tuncreate(cdev_t dev, int flags)
213 {
214 	struct tun_softc *sc;
215 	struct ifnet *ifp;
216 	int unit = minor(dev);
217 
218 	sc = kmalloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
219 	dev->si_drv1 = sc;
220 	sc->tun_dev = dev;
221 	sc->tun_flags = TUN_INITED;
222 	sc->tun_flags |= flags;
223 
224 	reference_dev(dev);  /* device association */
225 
226 	ifp = sc->tun_ifp = if_alloc(IFT_PPP);
227 	if (ifp == NULL) {
228 		kprintf("%s: failed to if_alloc() interface for %s%d",
229 			__func__, TUN, unit);
230 		return (NULL);
231 	}
232 
233 	if_initname(ifp, TUN, unit);
234 	ifp->if_mtu = TUNMTU;
235 	ifp->if_ioctl = tunifioctl;
236 	ifp->if_output = tunifoutput;
237 	ifp->if_start = tunifstart;
238 	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
239 	ifp->if_type = IFT_PPP;
240 	ifp->if_softc = sc;
241 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
242 	ifq_set_ready(&ifp->if_snd);
243 
244 	if_attach(ifp, NULL);
245 	bpfattach(ifp, DLT_NULL, sizeof(u_int));
246 
247 	SLIST_INSERT_HEAD(&tun_listhead, sc, tun_link);
248 	TUNDEBUG(ifp, "created, minor = %#x, flags = 0x%x\n",
249 		 unit, sc->tun_flags);
250 	return (sc);
251 }
252 
253 static void
254 tundestroy(struct tun_softc *sc)
255 {
256 	cdev_t dev = sc->tun_dev;
257 	struct ifnet *ifp = sc->tun_ifp;
258 	int unit = minor(dev);
259 
260 	TUNDEBUG(ifp, "destroyed, minor = %#x. Module refcnt = %d\n",
261 		 unit, tunrefcnt);
262 
263 	bpfdetach(ifp);
264 	if_detach(ifp);
265 	if_free(ifp);
266 
267 	sc->tun_dev = NULL;
268 	dev->si_drv1 = NULL;
269 	release_dev(dev);  /* device disassociation */
270 
271 	/* Also destroy the cloned device */
272 	if (unit >= TUN_PREALLOCATED_UNITS) {
273 		destroy_dev(dev);
274 		devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), unit);
275 	}
276 
277 	SLIST_REMOVE(&tun_listhead, sc, tun_softc, tun_link);
278 	kfree(sc, M_TUN);
279 }
280 
281 /*
282  * tunnel open - must be superuser & the device must be configured in
283  */
284 static int
285 tunopen(struct dev_open_args *ap)
286 {
287 	cdev_t dev = ap->a_head.a_dev;
288 	struct ifnet *ifp;
289 	struct tun_softc *sc;
290 	int error;
291 
292 	if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
293 		return (error);
294 
295 	sc = dev->si_drv1;
296 	if (sc == NULL && (sc = tuncreate(dev, TUN_MANUALMAKE)) == NULL)
297 		return (ENOMEM);
298 	if (sc->tun_flags & TUN_OPEN)
299 		return (EBUSY);
300 
301 	ifp = sc->tun_ifp;
302 	if ((sc->tun_flags & TUN_CLONE) == 0) {
303 		EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
304 
305 		/* Announce the return of the interface. */
306 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
307 	}
308 
309 	sc->tun_pid = curproc->p_pid;
310 	sc->tun_flags |= TUN_OPEN;
311 	tunrefcnt++;
312 
313 	TUNDEBUG(ifp, "opened, minor = %#x. Module refcnt = %d\n",
314 		 minor(dev), tunrefcnt);
315 	return (0);
316 }
317 
318 /*
319  * close the device - mark interface down & delete routing info
320  */
321 static int
322 tunclose(struct dev_close_args *ap)
323 {
324 	cdev_t dev = ap->a_head.a_dev;
325 	struct tun_softc *sc = dev->si_drv1;
326 	struct ifnet *ifp;
327 	int unit = minor(dev);
328 
329 	KASSERT(sc != NULL,
330 		("try closing the already destroyed %s%d", TUN, unit));
331 	ifp = sc->tun_ifp;
332 
333 	sc->tun_flags &= ~TUN_OPEN;
334 	sc->tun_pid = 0;
335 
336 	/* Junk all pending output. */
337 	ifq_purge_all(&ifp->if_snd);
338 
339 	if (ifp->if_flags & IFF_UP)
340 		if_down(ifp);
341 	ifp->if_flags &= ~IFF_RUNNING;
342 
343 	if ((sc->tun_flags & TUN_CLONE) == 0) {
344 		if_purgeaddrs_nolink(ifp);
345 
346 		EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
347 
348 		/* Announce the departure of the interface. */
349 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
350 	}
351 
352 	funsetown(&sc->tun_sigio);
353 	KNOTE(&sc->tun_rkq.ki_note, 0);
354 
355 	tunrefcnt--;
356 	if (tunrefcnt < 0) {
357 		tunrefcnt = 0;
358 		if_printf(ifp, ". Module refcnt = %d is out of sync! "
359 			  "Force refcnt to be 0.\n", tunrefcnt);
360 	}
361 
362 	TUNDEBUG(ifp, "closed, minor = %#x. Module refcnt = %d\n",
363 		 unit, tunrefcnt);
364 
365 	/* Only auto-destroy if the interface was not manually created. */
366 	if ((sc->tun_flags & TUN_MANUALMAKE) == 0 &&
367 	    unit >= TUN_PREALLOCATED_UNITS) {
368 		tundestroy(sc);
369 		dev->si_drv1 = NULL;
370 	}
371 
372 	return (0);
373 }
374 
375 
376 /*
377  * Interface clone support
378  *
379  * Create and destroy tun device/interface via ifconfig(8).
380  */
381 
382 static struct tun_softc *
383 tunfind(int unit)
384 {
385 	struct tun_softc *sc;
386 
387 	SLIST_FOREACH(sc, &tun_listhead, tun_link) {
388 		if (minor(sc->tun_dev) == unit)
389 			return (sc);
390 	}
391 	return (NULL);
392 }
393 
394 static int
395 tun_clone_create(struct if_clone *ifc __unused, int unit,
396 		 caddr_t param __unused)
397 {
398 	struct tun_softc *sc;
399 	cdev_t dev;
400 
401 	sc = tunfind(unit);
402 	if (sc == NULL) {
403 		if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tun), unit)) {
404 			devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), unit);
405 			dev = make_dev(&tun_ops, unit, UID_UUCP, GID_DIALER,
406 				       0600, "%s%d", TUN, unit);
407 		} else {
408 			dev = devfs_find_device_by_name("%s%d", TUN, unit);
409 		}
410 
411 		if (dev == NULL)
412 			return (ENODEV);
413 		if ((sc = tuncreate(dev, TUN_MANUALMAKE)) == NULL)
414 			return (ENOMEM);
415 	}
416 
417 	sc->tun_flags |= TUN_CLONE;
418 	TUNDEBUG(sc->tun_ifp, "clone created, minor = %#x, flags = 0x%x\n",
419 		 minor(sc->tun_dev), sc->tun_flags);
420 
421 	return (0);
422 }
423 
424 static int
425 tun_clone_destroy(struct ifnet * ifp)
426 {
427 	struct tun_softc *sc = ifp->if_softc;
428 
429 	if (sc->tun_flags & TUN_OPEN)
430 		return (EBUSY);
431 	if ((sc->tun_flags & TUN_CLONE) == 0)
432 		return (EINVAL);
433 
434 	TUNDEBUG(ifp, "clone destroyed, minor = %#x, flags = 0x%x\n",
435 		 minor(sc->tun_dev), sc->tun_flags);
436 	tundestroy(sc);
437 
438 	return (0);
439 }
440 
441 
442 /*
443  * Network interface functions
444  */
445 
446 static int
447 tunifinit(struct ifnet *ifp)
448 {
449 #ifdef INET
450 	struct tun_softc *sc = ifp->if_softc;
451 #endif
452 	struct ifaddr_container *ifac;
453 	int error = 0;
454 
455 	TUNDEBUG(ifp, "initialize\n");
456 
457 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
458 	getmicrotime(&ifp->if_lastchange);
459 
460 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
461 		struct ifaddr *ifa = ifac->ifa;
462 
463 		if (ifa->ifa_addr == NULL) {
464 			error = EFAULT;
465 			/* XXX: Should maybe return straight off? */
466 		} else {
467 #ifdef INET
468 			if (ifa->ifa_addr->sa_family == AF_INET) {
469 				struct sockaddr_in *si;
470 
471 				si = (struct sockaddr_in *)ifa->ifa_addr;
472 				if (si->sin_addr.s_addr)
473 					sc->tun_flags |= TUN_IASET;
474 			}
475 #endif
476 		}
477 	}
478 	return (error);
479 }
480 
481 /*
482  * Process an ioctl request.
483  *
484  * MPSAFE
485  */
486 static int
487 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
488 {
489 	struct ifreq *ifr = (struct ifreq *)data;
490 	struct tun_softc *sc = ifp->if_softc;
491 	struct ifstat *ifs;
492 	int error = 0;
493 
494 	switch (cmd) {
495 	case SIOCGIFSTATUS:
496 		ifs = (struct ifstat *)data;
497 		if (sc->tun_pid)
498 			ksprintf(ifs->ascii + strlen(ifs->ascii),
499 			    "\tOpened by PID %d\n", sc->tun_pid);
500 		break;
501 	case SIOCSIFADDR:
502 		error = tunifinit(ifp);
503 		TUNDEBUG(ifp, "address set, error=%d\n", error);
504 		break;
505 	case SIOCSIFDSTADDR:
506 		error = tunifinit(ifp);
507 		TUNDEBUG(ifp, "destination address set, error=%d\n", error);
508 		break;
509 	case SIOCSIFMTU:
510 		ifp->if_mtu = ifr->ifr_mtu;
511 		TUNDEBUG(ifp, "mtu set\n");
512 		break;
513 	case SIOCSIFFLAGS:
514 	case SIOCADDMULTI:
515 	case SIOCDELMULTI:
516 		break;
517 	default:
518 		error = EINVAL;
519 	}
520 	return (error);
521 }
522 
523 /*
524  * Start packet transmission on the interface.
525  * when the interface queue is rate-limited by ALTQ,
526  * if_start is needed to drain packets from the queue in order
527  * to notify readers when outgoing packets become ready.
528  */
529 static void
530 tunifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
531 {
532 	struct tun_softc *sc = ifp->if_softc;
533 	struct mbuf *m;
534 
535 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
536 
537 	if (!ifq_is_enabled(&ifp->if_snd))
538 		return;
539 
540 	m = ifsq_poll(ifsq);
541 	if (m != NULL) {
542 		if (sc->tun_flags & TUN_RWAIT) {
543 			sc->tun_flags &= ~TUN_RWAIT;
544 			wakeup((caddr_t)sc);
545 		}
546 		if (sc->tun_flags & TUN_ASYNC && sc->tun_sigio)
547 			pgsigio(sc->tun_sigio, SIGIO, 0);
548 		ifsq_deserialize_hw(ifsq);
549 		KNOTE(&sc->tun_rkq.ki_note, 0);
550 		ifsq_serialize_hw(ifsq);
551 	}
552 }
553 
554 /*
555  * tunifoutput - queue packets from higher level ready to put out.
556  *
557  * MPSAFE
558  */
559 static int
560 tunifoutput_serialized(struct ifnet *ifp, struct mbuf *m0,
561 		       struct sockaddr *dst, struct rtentry *rt)
562 {
563 	struct tun_softc *sc = ifp->if_softc;
564 	int error;
565 	struct altq_pktattr pktattr;
566 
567 	TUNDEBUG(ifp, "output\n");
568 
569 	if ((sc->tun_flags & TUN_READY) != TUN_READY) {
570 		TUNDEBUG(ifp, "not ready, flags = 0x%x\n", sc->tun_flags);
571 		m_freem (m0);
572 		return (EHOSTDOWN);
573 	}
574 
575 	/*
576 	 * if the queueing discipline needs packet classification,
577 	 * do it before prepending link headers.
578 	 */
579 	ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
580 
581 	/* BPF write needs to be handled specially */
582 	if (dst->sa_family == AF_UNSPEC) {
583 		dst->sa_family = *(mtod(m0, int *));
584 		m0->m_len -= sizeof(int);
585 		m0->m_pkthdr.len -= sizeof(int);
586 		m0->m_data += sizeof(int);
587 	}
588 
589 	if (ifp->if_bpf) {
590 		bpf_gettoken();
591 		if (ifp->if_bpf) {
592 			/*
593 			 * We need to prepend the address family as
594 			 * a four byte field.
595 			 */
596 			uint32_t af = dst->sa_family;
597 
598 			bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
599 		}
600 		bpf_reltoken();
601 	}
602 
603 	/* prepend sockaddr? this may abort if the mbuf allocation fails */
604 	if (sc->tun_flags & TUN_LMODE) {
605 		/* allocate space for sockaddr */
606 		M_PREPEND(m0, dst->sa_len, M_NOWAIT);
607 
608 		/* if allocation failed drop packet */
609 		if (m0 == NULL){
610 			IFNET_STAT_INC(ifp, oerrors, 1);
611 			return (ENOBUFS);
612 		} else {
613 			bcopy(dst, m0->m_data, dst->sa_len);
614 		}
615 	}
616 
617 	if (sc->tun_flags & TUN_IFHEAD) {
618 		/* Prepend the address family */
619 		M_PREPEND(m0, 4, M_NOWAIT);
620 
621 		/* if allocation failed drop packet */
622 		if (m0 == NULL){
623 			IFNET_STAT_INC(ifp, oerrors, 1);
624 			return (ENOBUFS);
625 		} else {
626 			*(u_int32_t *)m0->m_data = htonl(dst->sa_family);
627 		}
628 	} else {
629 #ifdef INET
630 		if (dst->sa_family != AF_INET)
631 #endif
632 		{
633 			m_freem(m0);
634 			return (EAFNOSUPPORT);
635 		}
636 	}
637 
638 	error = ifq_handoff(ifp, m0, &pktattr);
639 	if (error) {
640 		IFNET_STAT_INC(ifp, collisions, 1);
641 	} else {
642 		IFNET_STAT_INC(ifp, opackets, 1);
643 		if (sc->tun_flags & TUN_RWAIT) {
644 			sc->tun_flags &= ~TUN_RWAIT;
645 			wakeup((caddr_t)sc);
646 		}
647 		get_mplock();
648 		if (sc->tun_flags & TUN_ASYNC && sc->tun_sigio)
649 			pgsigio(sc->tun_sigio, SIGIO, 0);
650 		rel_mplock();
651 		ifnet_deserialize_all(ifp);
652 		KNOTE(&sc->tun_rkq.ki_note, 0);
653 		ifnet_serialize_all(ifp);
654 	}
655 	return (error);
656 }
657 
658 static int
659 tunifoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
660 	    struct rtentry *rt)
661 {
662 	int error;
663 
664 	ifnet_serialize_all(ifp);
665 	error = tunifoutput_serialized(ifp, m0, dst, rt);
666 	ifnet_deserialize_all(ifp);
667 
668 	return (error);
669 }
670 
671 
672 /*
673  * the ops interface is now pretty minimal.
674  */
675 static int
676 tunioctl(struct dev_ioctl_args *ap)
677 {
678 	cdev_t dev = ap->a_head.a_dev;
679 	caddr_t data = ap->a_data;
680 	struct tun_softc *sc = dev->si_drv1;
681 	struct ifnet *ifp = sc->tun_ifp;
682 	struct ifreq *ifr;
683 	struct tuninfo *tunp;
684 	int error = 0;
685 
686 	switch (ap->a_cmd) {
687 	case TUNSIFINFO:
688 		tunp = (struct tuninfo *)data;
689 		if (ifp->if_type != tunp->type)
690 			return (EPROTOTYPE);
691 		if (tunp->mtu < IF_MINMTU)
692 			return (EINVAL);
693 		ifp->if_mtu = tunp->mtu;
694 		ifp->if_baudrate = tunp->baudrate;
695 		break;
696 
697 	case TUNGIFINFO:
698 		tunp = (struct tuninfo *)data;
699 		tunp->mtu = ifp->if_mtu;
700 		tunp->type = ifp->if_type;
701 		tunp->baudrate = ifp->if_baudrate;
702 		break;
703 
704 	case TUNGIFNAME:
705 		ifr = (struct ifreq *)data;
706 		strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
707 		break;
708 
709 	case TUNSDEBUG:
710 		tundebug = *(int *)data;
711 		break;
712 
713 	case TUNGDEBUG:
714 		*(int *)data = tundebug;
715 		break;
716 
717 	case TUNSLMODE:
718 		if (*(int *)data) {
719 			sc->tun_flags |= TUN_LMODE;
720 			sc->tun_flags &= ~TUN_IFHEAD;
721 		} else {
722 			sc->tun_flags &= ~TUN_LMODE;
723 		}
724 		break;
725 
726 	case TUNSIFHEAD:
727 		if (*(int *)data) {
728 			sc->tun_flags |= TUN_IFHEAD;
729 			sc->tun_flags &= ~TUN_LMODE;
730 		} else {
731 			sc->tun_flags &= ~TUN_IFHEAD;
732 		}
733 		break;
734 
735 	case TUNGIFHEAD:
736 		*(int *)data = (sc->tun_flags & TUN_IFHEAD) ? 1 : 0;
737 		break;
738 
739 	case TUNSIFMODE:
740 		/* deny this if UP */
741 		if (ifp->if_flags & IFF_UP)
742 			return (EBUSY);
743 
744 		switch (*(int *)data & ~IFF_MULTICAST) {
745 		case IFF_POINTOPOINT:
746 		case IFF_BROADCAST:
747 			ifp->if_flags &= ~(IFF_BROADCAST | IFF_POINTOPOINT);
748 			ifp->if_flags |= *(int *)data;
749 			break;
750 		default:
751 			return (EINVAL);
752 		}
753 		break;
754 
755 	case TUNSIFPID:
756 		sc->tun_pid = curproc->p_pid;
757 		break;
758 
759 	case FIOASYNC:
760 		if (*(int *)data)
761 			sc->tun_flags |= TUN_ASYNC;
762 		else
763 			sc->tun_flags &= ~TUN_ASYNC;
764 		break;
765 
766 	case FIONREAD:
767 		*(int *)data = ifsq_poll_pktlen(
768 		    ifq_get_subq_default(&ifp->if_snd));
769 		break;
770 
771 	case FIOSETOWN:
772 		error = fsetown(*(int *)data, &sc->tun_sigio);
773 		break;
774 
775 	case FIOGETOWN:
776 		*(int *)data = fgetown(&sc->tun_sigio);
777 		break;
778 
779 	/* This is deprecated, FIOSETOWN should be used instead. */
780 	case TIOCSPGRP:
781 		error = fsetown(-(*(int *)data), &sc->tun_sigio);
782 		break;
783 
784 	/* This is deprecated, FIOGETOWN should be used instead. */
785 	case TIOCGPGRP:
786 		*(int *)data = -fgetown(&sc->tun_sigio);
787 		break;
788 
789 	default:
790 		error = ENOTTY;
791 		break;
792 	}
793 
794 	return (error);
795 }
796 
797 /*
798  * The ops read interface - reads a packet at a time, or at
799  * least as much of a packet as can be read.
800  */
801 static	int
802 tunread(struct dev_read_args *ap)
803 {
804 	cdev_t dev = ap->a_head.a_dev;
805 	struct uio *uio = ap->a_uio;
806 	struct tun_softc *sc = dev->si_drv1;
807 	struct ifnet *ifp = sc->tun_ifp;
808 	struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
809 	struct mbuf *m0;
810 	int error=0, len;
811 
812 	TUNDEBUG(ifp, "read\n");
813 	if ((sc->tun_flags & TUN_READY) != TUN_READY) {
814 		TUNDEBUG(ifp, "not ready, flags = 0x%x\n", sc->tun_flags);
815 		return (EHOSTDOWN);
816 	}
817 
818 	sc->tun_flags &= ~TUN_RWAIT;
819 
820 	ifnet_serialize_all(ifp);
821 
822 	while ((m0 = ifsq_dequeue(ifsq)) == NULL) {
823 		if (ap->a_ioflag & IO_NDELAY) {
824 			ifnet_deserialize_all(ifp);
825 			return (EWOULDBLOCK);
826 		}
827 		sc->tun_flags |= TUN_RWAIT;
828 		ifnet_deserialize_all(ifp);
829 		if ((error = tsleep(sc, PCATCH, "tunread", 0)) != 0)
830 			return (error);
831 		ifnet_serialize_all(ifp);
832 	}
833 
834 	ifnet_deserialize_all(ifp);
835 
836 	while (m0 && uio->uio_resid > 0 && error == 0) {
837 		len = (int)szmin(uio->uio_resid, m0->m_len);
838 		if (len != 0)
839 			error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
840 		m0 = m_free(m0);
841 	}
842 
843 	if (m0) {
844 		TUNDEBUG(ifp, "dropping mbuf\n");
845 		m_freem(m0);
846 	}
847 	return (error);
848 }
849 
850 /*
851  * the ops write interface - an atomic write is a packet - or else!
852  */
853 static	int
854 tunwrite(struct dev_write_args *ap)
855 {
856 	cdev_t dev = ap->a_head.a_dev;
857 	struct uio *uio = ap->a_uio;
858 	struct tun_softc *sc = dev->si_drv1;
859 	struct ifnet *ifp = sc->tun_ifp;
860 	struct mbuf *top, **mp, *m;
861 	size_t tlen;
862 	uint32_t family, mru;
863 	int error = 0;
864 	int isr;
865 
866 	TUNDEBUG(ifp, "tunwrite\n");
867 
868 	if (uio->uio_resid == 0)
869 		return (0);
870 
871 	mru = TUNMRU;
872 	if (sc->tun_flags & TUN_IFHEAD)
873 		mru += sizeof(family);
874 	if (uio->uio_resid > mru) {
875 		TUNDEBUG(ifp, "len = %zd!\n", uio->uio_resid);
876 		return (EIO);
877 	}
878 
879 	/* get a header mbuf */
880 	MGETHDR(m, M_WAITOK, MT_DATA);
881 	if (m == NULL)
882 		return (ENOBUFS);
883 
884 	tlen = uio->uio_resid;
885 	top = NULL;
886 	mp = &top;
887 	while (error == 0 && uio->uio_resid > 0) {
888 		m->m_len = (int)szmin(MHLEN, uio->uio_resid);
889 		error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
890 		*mp = m;
891 		mp = &m->m_next;
892 		if (uio->uio_resid > 0) {
893 			MGET(m, M_WAITOK, MT_DATA);
894 			if (m == NULL) {
895 				error = ENOBUFS;
896 				break;
897 			}
898 		}
899 	}
900 	if (error) {
901 		if (top)
902 			m_freem(top);
903 		IFNET_STAT_INC(ifp, ierrors, 1);
904 		return (error);
905 	}
906 
907 	top->m_pkthdr.len = (int)tlen;
908 	top->m_pkthdr.rcvif = ifp;
909 
910 	if (ifp->if_bpf) {
911 		bpf_gettoken();
912 
913 		if (ifp->if_bpf) {
914 			if (sc->tun_flags & TUN_IFHEAD) {
915 				/*
916 				 * Conveniently, we already have a 4-byte
917 				 * address family prepended to our packet !
918 				 * Inconveniently, it's in the wrong byte
919 				 * order !
920 				 */
921 				if ((top = m_pullup(top, sizeof(family)))
922 				    == NULL) {
923 					bpf_reltoken();
924 					return (ENOBUFS);
925 				}
926 				*mtod(top, u_int32_t *) =
927 				    ntohl(*mtod(top, u_int32_t *));
928 				bpf_mtap(ifp->if_bpf, top);
929 				*mtod(top, u_int32_t *) =
930 				    htonl(*mtod(top, u_int32_t *));
931 			} else {
932 				/*
933 				 * We need to prepend the address family as
934 				 * a four byte field.
935 				 */
936 				static const uint32_t af = AF_INET;
937 
938 				bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
939 			}
940 		}
941 
942 		bpf_reltoken();
943 	}
944 
945 	if (sc->tun_flags & TUN_IFHEAD) {
946 		if (top->m_len < sizeof(family) &&
947 		    (top = m_pullup(top, sizeof(family))) == NULL)
948 				return (ENOBUFS);
949 		family = ntohl(*mtod(top, u_int32_t *));
950 		m_adj(top, sizeof(family));
951 	} else {
952 		family = AF_INET;
953 	}
954 
955 	IFNET_STAT_INC(ifp, ibytes, top->m_pkthdr.len);
956 	IFNET_STAT_INC(ifp, ipackets, 1);
957 
958 	switch (family) {
959 #ifdef INET
960 	case AF_INET:
961 		isr = NETISR_IP;
962 		break;
963 #endif
964 #ifdef INET6
965 	case AF_INET6:
966 		isr = NETISR_IPV6;
967 		break;
968 #endif
969 	default:
970 		m_freem(m);
971 		return (EAFNOSUPPORT);
972 	}
973 
974 	netisr_queue(isr, top);
975 	return (0);
976 }
977 
978 
979 /*
980  * tunkqfilter - support for the kevent() system call.
981  */
982 static int
983 tunkqfilter(struct dev_kqfilter_args *ap)
984 {
985 	cdev_t dev = ap->a_head.a_dev;
986 	struct tun_softc *sc = dev->si_drv1;
987 	struct ifnet *ifp = sc->tun_ifp;
988 	struct knote *kn = ap->a_kn;
989 	struct klist *klist;
990 
991 	ap->a_result = 0;
992 	ifnet_serialize_all(ifp);
993 
994 	switch (kn->kn_filter) {
995 	case EVFILT_READ:
996 		kn->kn_fop = &tun_read_filtops;
997 		kn->kn_hook = (caddr_t)sc;
998 		break;
999 	case EVFILT_WRITE:
1000 		kn->kn_fop = &tun_write_filtops;
1001 		kn->kn_hook = (caddr_t)sc;
1002 		break;
1003 	default:
1004 		ifnet_deserialize_all(ifp);
1005 		ap->a_result = EOPNOTSUPP;
1006 		return (0);
1007 	}
1008 
1009 	klist = &sc->tun_rkq.ki_note;
1010 	knote_insert(klist, kn);
1011 	ifnet_deserialize_all(ifp);
1012 
1013 	return (0);
1014 }
1015 
1016 static void
1017 tun_filter_detach(struct knote *kn)
1018 {
1019 	struct tun_softc *sc = (struct tun_softc *)kn->kn_hook;
1020 	struct klist *klist = &sc->tun_rkq.ki_note;
1021 
1022 	knote_remove(klist, kn);
1023 }
1024 
1025 static int
1026 tun_filter_write(struct knote *kn, long hint)
1027 {
1028 	/* Always ready for a write */
1029 	return (1);
1030 }
1031 
1032 static int
1033 tun_filter_read(struct knote *kn, long hint)
1034 {
1035 	struct tun_softc *sc = (struct tun_softc *)kn->kn_hook;
1036 	struct ifnet *ifp = sc->tun_ifp;
1037 	int ready = 0;
1038 
1039 	ifnet_serialize_all(ifp);
1040 	if (!ifsq_is_empty(ifq_get_subq_default(&ifp->if_snd)))
1041 		ready = 1;
1042 	ifnet_deserialize_all(ifp);
1043 
1044 	return (ready);
1045 }
1046