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