xref: /dragonfly/sys/net/tap/if_tap.c (revision 335b9e93)
1 /*
2  * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * BASED ON:
27  * -------------------------------------------------------------------------
28  *
29  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
30  * Nottingham University 1987.
31  */
32 
33 /*
34  * $FreeBSD: src/sys/net/if_tap.c,v 1.3.2.3 2002/04/14 21:41:48 luigi Exp $
35  * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
36  */
37 
38 #include "opt_inet.h"
39 
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/filedesc.h>
44 #include <sys/filio.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/proc.h>
49 #include <sys/priv.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 #include <sys/ttycom.h>
56 #include <sys/uio.h>
57 #include <sys/vnode.h>
58 #include <sys/serialize.h>
59 #include <sys/thread2.h>
60 #include <sys/mplock2.h>
61 #include <sys/devfs.h>
62 #include <sys/queue.h>
63 
64 #include <net/bpf.h>
65 #include <net/ethernet.h>
66 #include <net/if.h>
67 #include <net/if_types.h>
68 #include <net/if_arp.h>
69 #include <net/if_clone.h>
70 #include <net/if_media.h>
71 #include <net/ifq_var.h>
72 #include <net/route.h>
73 
74 #include <netinet/in.h>
75 
76 #include "if_tapvar.h"
77 #include "if_tap.h"
78 
79 #define TAP_IFFLAGS	(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST)
80 
81 #define TAP		"tap"
82 #define TAPDEBUG	if (tapdebug) if_printf
83 
84 /* module */
85 static int		tapmodevent(module_t, int, void *);
86 
87 /* device */
88 static struct tap_softc *tapcreate(cdev_t, int);
89 static void		tapdestroy(struct tap_softc *);
90 
91 /* clone */
92 static int		tap_clone_create(struct if_clone *, int, caddr_t);
93 static int		tap_clone_destroy(struct ifnet *);
94 
95 /* network interface */
96 static void		tapifinit(void *);
97 static void		tapifstart(struct ifnet *, struct ifaltq_subque *);
98 static int		tapifioctl(struct ifnet *, u_long, caddr_t,
99 				   struct ucred *);
100 static void		tapifstop(struct tap_softc *, int);
101 static void		tapifflags(struct tap_softc *);
102 
103 /* character device */
104 static d_open_t		tapopen;
105 static d_clone_t	tapclone;
106 static d_close_t	tapclose;
107 static d_read_t		tapread;
108 static d_write_t	tapwrite;
109 static d_ioctl_t	tapioctl;
110 static d_kqfilter_t	tapkqfilter;
111 
112 static struct dev_ops	tap_ops = {
113 	{ TAP, 0, 0 },
114 	.d_open =	tapopen,
115 	.d_close =	tapclose,
116 	.d_read =	tapread,
117 	.d_write =	tapwrite,
118 	.d_ioctl =	tapioctl,
119 	.d_kqfilter =	tapkqfilter
120 };
121 
122 /* kqueue support */
123 static void		tap_filter_detach(struct knote *);
124 static int		tap_filter_read(struct knote *, long);
125 static int		tap_filter_write(struct knote *, long);
126 
127 static struct filterops tapread_filtops = {
128 	FILTEROP_ISFD,
129 	NULL,
130 	tap_filter_detach,
131 	tap_filter_read
132 };
133 static struct filterops tapwrite_filtops = {
134 	FILTEROP_ISFD,
135 	NULL,
136 	tap_filter_detach,
137 	tap_filter_write
138 };
139 
140 static int		tapdebug = 0;		/* debug flag */
141 static int		taprefcnt = 0;		/* module ref. counter */
142 static int		tapuopen = 0;		/* allow user open() */
143 static int		tapuponopen = 0;	/* IFF_UP when opened */
144 
145 static MALLOC_DEFINE(M_TAP, TAP, "Ethernet tunnel interface");
146 
147 static DEVFS_DEFINE_CLONE_BITMAP(tap);
148 
149 struct if_clone tap_cloner = IF_CLONE_INITIALIZER(
150 	TAP, tap_clone_create, tap_clone_destroy, 0, IF_MAXUNIT);
151 
152 static SLIST_HEAD(,tap_softc) tap_listhead =
153 	SLIST_HEAD_INITIALIZER(&tap_listhead);
154 
155 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
156 SYSCTL_DECL(_net_link);
157 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
158 	    "Ethernet tunnel software network interface");
159 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
160 	   "Allow user to open /dev/tap (based on node permissions)");
161 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
162 	   "Bring interface up when /dev/tap is opened");
163 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
164 SYSCTL_INT(_net_link_tap, OID_AUTO, refcnt, CTLFLAG_RD, &taprefcnt, 0,
165 	   "Number of opened devices");
166 
167 DEV_MODULE(if_tap, tapmodevent, NULL);
168 
169 /*
170  * tapmodevent
171  *
172  * module event handler
173  */
174 static int
175 tapmodevent(module_t mod, int type, void *data)
176 {
177 	static cdev_t dev = NULL;
178 	struct tap_softc *sc, *sc_tmp;
179 
180 	switch (type) {
181 	case MOD_LOAD:
182 		dev = make_autoclone_dev(&tap_ops, &DEVFS_CLONE_BITMAP(tap),
183 					 tapclone, UID_ROOT, GID_WHEEL,
184 					 0600, TAP);
185 		SLIST_INIT(&tap_listhead);
186 		if_clone_attach(&tap_cloner);
187 		break;
188 
189 	case MOD_UNLOAD:
190 		if (taprefcnt > 0)
191 			return (EBUSY);
192 
193 		if_clone_detach(&tap_cloner);
194 
195 		SLIST_FOREACH_MUTABLE(sc, &tap_listhead, tap_link, sc_tmp)
196 			tapdestroy(sc);
197 
198 		dev_ops_remove_all(&tap_ops);
199 		destroy_autoclone_dev(dev, &DEVFS_CLONE_BITMAP(tap));
200 		break;
201 
202 	default:
203 		return (EOPNOTSUPP);
204 	}
205 
206 	return (0);
207 }
208 
209 
210 /*
211  * Create or clone an interface
212  */
213 static struct tap_softc *
214 tapcreate(cdev_t dev, int flags)
215 {
216 	struct tap_softc *sc;
217 	struct ifnet *ifp;
218 	uint8_t ether_addr[ETHER_ADDR_LEN];
219 	int unit = minor(dev);
220 
221 	sc = kmalloc(sizeof(*sc), M_TAP, M_WAITOK | M_ZERO);
222 	dev->si_drv1 = sc;
223 	sc->tap_dev = dev;
224 	sc->tap_flags |= flags;
225 
226 	reference_dev(dev); /* device association */
227 
228 	/* generate fake MAC address: 00 bd xx xx xx unit_no */
229 	ether_addr[0] = 0x00;
230 	ether_addr[1] = 0xbd;
231 	bcopy(&ticks, &ether_addr[2], 3);
232 	ether_addr[5] = (u_char)unit;
233 
234 	/* fill the rest and attach interface */
235 	ifp = &sc->tap_if;
236 	if_initname(ifp, TAP, unit);
237 	ifp->if_type = IFT_ETHER;
238 	ifp->if_init = tapifinit;
239 	ifp->if_start = tapifstart;
240 	ifp->if_ioctl = tapifioctl;
241 	ifp->if_mtu = ETHERMTU;
242 	ifp->if_flags = TAP_IFFLAGS;
243 	ifp->if_softc = sc;
244 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
245 	ifq_set_ready(&ifp->if_snd);
246 
247 	ether_ifattach(ifp, ether_addr, NULL);
248 
249 	sc->tap_flags |= TAP_INITED;
250 	sc->tap_devq.ifq_maxlen = ifqmaxlen;
251 
252 	SLIST_INSERT_HEAD(&tap_listhead, sc, tap_link);
253 
254 	TAPDEBUG(ifp, "created, minor = %#x, flags = 0x%x\n",
255 		 unit, sc->tap_flags);
256 	return (sc);
257 }
258 
259 static struct tap_softc *
260 tapfind(int unit)
261 {
262 	struct tap_softc *sc;
263 
264 	SLIST_FOREACH(sc, &tap_listhead, tap_link) {
265 		if (minor(sc->tap_dev) == unit)
266 			return (sc);
267 	}
268 	return (NULL);
269 }
270 
271 /*
272  * Create a new tap instance via ifconfig.
273  */
274 static int
275 tap_clone_create(struct if_clone *ifc __unused, int unit,
276 		 caddr_t param __unused)
277 {
278 	struct tap_softc *sc;
279 	cdev_t dev;
280 
281 	sc = tapfind(unit);
282 	if (sc == NULL) {
283 		if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tap), unit)) {
284 			devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), unit);
285 			dev = make_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
286 				       0600, "%s%d", TAP, unit);
287 		} else {
288 			dev = devfs_find_device_by_name("%s%d", TAP, unit);
289 		}
290 
291 		if (dev == NULL)
292 			return (ENODEV);
293 		if ((sc = tapcreate(dev, TAP_MANUALMAKE)) == NULL)
294 			return (ENOMEM);
295 	} else {
296 		dev = sc->tap_dev;
297 	}
298 
299 	sc->tap_flags |= TAP_CLONE;
300 	TAPDEBUG(&sc->tap_if, "clone created, minor = %#x, flags = 0x%x\n",
301 		 minor(dev), sc->tap_flags);
302 
303 	return (0);
304 }
305 
306 /*
307  * Open the tap device.
308  */
309 static int
310 tapopen(struct dev_open_args *ap)
311 {
312 	cdev_t dev = NULL;
313 	struct tap_softc *sc = NULL;
314 	struct ifnet *ifp = NULL;
315 	int error;
316 
317 	if (tapuopen == 0 &&
318 	    (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
319 		return (error);
320 
321 	get_mplock();
322 	dev = ap->a_head.a_dev;
323 	sc = dev->si_drv1;
324 	if (sc == NULL && (sc = tapcreate(dev, TAP_MANUALMAKE)) == NULL) {
325 		rel_mplock();
326 		return (ENOMEM);
327 	}
328 	if (sc->tap_flags & TAP_OPEN) {
329 		rel_mplock();
330 		return (EBUSY);
331 	}
332 	ifp = &sc->tap_if;
333 
334 	if ((sc->tap_flags & TAP_CLONE) == 0) {
335 		EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
336 
337 		/* Announce the return of the interface. */
338 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
339 	}
340 
341 	bcopy(sc->arpcom.ac_enaddr, sc->ether_addr, sizeof(sc->ether_addr));
342 
343 	if (curthread->td_proc)
344 		fsetown(curthread->td_proc->p_pid, &sc->tap_sigtd);
345 	sc->tap_flags |= TAP_OPEN;
346 	taprefcnt++;
347 
348 	if (tapuponopen && (ifp->if_flags & IFF_UP) == 0) {
349 		crit_enter();
350 		if_up(ifp);
351 		crit_exit();
352 
353 		ifnet_serialize_all(ifp);
354 		tapifflags(sc);
355 		ifnet_deserialize_all(ifp);
356 
357 		sc->tap_flags |= TAP_CLOSEDOWN;
358 	}
359 
360 	TAPDEBUG(ifp, "opened, minor = %#x. Module refcnt = %d\n",
361 		 minor(dev), taprefcnt);
362 
363 	rel_mplock();
364 	return (0);
365 }
366 
367 static int
368 tapclone(struct dev_clone_args *ap)
369 {
370 	int unit;
371 
372 	unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tap), 0);
373 	ap->a_dev = make_only_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
374 				  0600, "%s%d", TAP, unit);
375 	if (tapcreate(ap->a_dev, 0) == NULL)
376 		return (ENOMEM);
377 	else
378 		return (0);
379 }
380 
381 /*
382  * Close the tap device: bring the interface down & delete routing info.
383  */
384 static int
385 tapclose(struct dev_close_args *ap)
386 {
387 	cdev_t dev = ap->a_head.a_dev;
388 	struct tap_softc *sc = dev->si_drv1;
389 	struct ifnet *ifp;
390 	int unit = minor(dev);
391 	int clear_flags = 0;
392 
393 	KASSERT(sc != NULL,
394 		("try closing the already destroyed %s%d", TAP, unit));
395 	ifp = &sc->tap_if;
396 
397 	get_mplock();
398 
399 	/* Junk all pending output */
400 	ifq_purge_all(&ifp->if_snd);
401 
402 	/*
403 	 * If the interface is not cloned, we always bring it down.
404 	 *
405 	 * If the interface is cloned, then we bring it down during
406 	 * closing only if it was brought up during opening.
407 	 */
408 	if ((sc->tap_flags & TAP_CLONE) == 0 ||
409 	    (sc->tap_flags & TAP_CLOSEDOWN)) {
410 		if (ifp->if_flags & IFF_UP)
411 			if_down(ifp);
412 		clear_flags = 1;
413 	}
414 	ifnet_serialize_all(ifp);
415 	tapifstop(sc, clear_flags);
416 	ifnet_deserialize_all(ifp);
417 
418 	if ((sc->tap_flags & TAP_CLONE) == 0) {
419 		if_purgeaddrs_nolink(ifp);
420 
421 		EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
422 
423 		/* Announce the departure of the interface. */
424 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
425 	}
426 
427 	funsetown(&sc->tap_sigio);
428 	sc->tap_sigio = NULL;
429 	KNOTE(&sc->tap_rkq.ki_note, 0);
430 
431 	sc->tap_flags &= ~TAP_OPEN;
432 	funsetown(&sc->tap_sigtd);
433 	sc->tap_sigtd = NULL;
434 
435 	taprefcnt--;
436 	if (taprefcnt < 0) {
437 		taprefcnt = 0;
438 		if_printf(ifp, ". Module refcnt = %d is out of sync! "
439 			  "Force refcnt to be 0.\n", taprefcnt);
440 	}
441 
442 	TAPDEBUG(ifp, "closed, minor = %#x. Module refcnt = %d\n",
443 		 unit, taprefcnt);
444 
445 	/* Only auto-destroy if the interface was not manually created. */
446 	if ((sc->tap_flags & TAP_MANUALMAKE) == 0) {
447 		tapdestroy(sc);
448 		dev->si_drv1 = NULL;
449 	}
450 
451 	rel_mplock();
452 	return (0);
453 }
454 
455 
456 /*
457  * Destroy a tap instance: the interface and the associated device.
458  */
459 static void
460 tapdestroy(struct tap_softc *sc)
461 {
462 	struct ifnet *ifp = &sc->tap_if;
463 	cdev_t dev = sc->tap_dev;
464 	int unit = minor(dev);
465 
466 	TAPDEBUG(ifp, "destroyed, minor = %#x. Module refcnt = %d\n",
467 		 unit, taprefcnt);
468 
469 	ifnet_serialize_all(ifp);
470 	tapifstop(sc, 1);
471 	ifnet_deserialize_all(ifp);
472 
473 	ether_ifdetach(ifp);
474 
475 	kprintf("TAPDESTROY UNIT %d\n", unit);
476 
477 	sc->tap_dev = NULL;
478 	dev->si_drv1 = NULL;
479 	release_dev(dev); /* device disassociation */
480 
481 	/* Also destroy the cloned device */
482 	destroy_dev(dev);
483 	devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tap), unit);
484 
485 	SLIST_REMOVE(&tap_listhead, sc, tap_softc, tap_link);
486 	kfree(sc, M_TAP);
487 }
488 
489 
490 /*
491  * Destroy a tap instance that is created via ifconfig.
492  */
493 static int
494 tap_clone_destroy(struct ifnet *ifp)
495 {
496 	struct tap_softc *sc = ifp->if_softc;
497 
498 	if (sc->tap_flags & TAP_OPEN)
499 		return (EBUSY);
500 	if ((sc->tap_flags & TAP_CLONE) == 0)
501 		return (EINVAL);
502 
503 	TAPDEBUG(ifp, "clone destroyed, minor = %#x, flags = 0x%x\n",
504 		 minor(sc->tap_dev), sc->tap_flags);
505 	tapdestroy(sc);
506 
507 	return (0);
508 }
509 
510 
511 /*
512  * tapifinit
513  *
514  * Network interface initialization function (called with if serializer held)
515  *
516  * MPSAFE
517  */
518 static void
519 tapifinit(void *xtp)
520 {
521 	struct tap_softc *sc = xtp;
522 	struct ifnet *ifp = &sc->tap_if;
523 	struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
524 
525 	TAPDEBUG(ifp, "initializing, minor = %#x, flags = 0x%x\n",
526 		 minor(sc->tap_dev), sc->tap_flags);
527 
528 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
529 
530 	tapifstop(sc, 1);
531 
532 	ifp->if_flags |= IFF_RUNNING;
533 	ifsq_clr_oactive(ifsq);
534 
535 	/* attempt to start output */
536 	tapifstart(ifp, ifsq);
537 }
538 
539 static void
540 tapifflags(struct tap_softc *sc)
541 {
542 	struct ifnet *ifp = &sc->tap_if;
543 
544 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
545 
546 	if (ifp->if_flags & IFF_UP) {
547 		if ((ifp->if_flags & IFF_RUNNING) == 0)
548 			tapifinit(sc);
549 	} else {
550 		tapifstop(sc, 1);
551 	}
552 }
553 
554 /*
555  * tapifioctl
556  *
557  * Process an ioctl request on network interface (called with if serializer
558  * held).
559  *
560  * MPSAFE
561  */
562 static int
563 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
564 {
565 	struct tap_softc *sc = ifp->if_softc;
566 	struct ifreq *ifr = NULL;
567 	struct ifstat *ifs = NULL;
568 	struct ifmediareq *ifmr = NULL;
569 	int error = 0;
570 	int dummy;
571 
572 	switch (cmd) {
573 	case SIOCADDMULTI:
574 	case SIOCDELMULTI:
575 		break;
576 
577 	case SIOCSIFMTU:
578 		ifr = (struct ifreq *)data;
579 		ifp->if_mtu = ifr->ifr_mtu;
580 		TAPDEBUG(ifp, "mtu set\n");
581 		break;
582 
583 	case SIOCSIFADDR:
584 	case SIOCGIFADDR:
585 		error = ether_ioctl(ifp, cmd, data);
586 		break;
587 
588 	case SIOCSIFFLAGS:
589 		tapifflags(sc);
590 		break;
591 
592 	case SIOCGIFMEDIA:
593 		/*
594 		 * The bridge code needs this when running the
595 		 * spanning tree protocol.
596 		 */
597 		ifmr = (struct ifmediareq *)data;
598 		dummy = ifmr->ifm_count;
599 		ifmr->ifm_count = 1;
600 		ifmr->ifm_status = IFM_AVALID;
601 		ifmr->ifm_active = IFM_ETHER;
602 		if (sc->tap_flags & TAP_OPEN)
603 			ifmr->ifm_status |= IFM_ACTIVE;
604 		ifmr->ifm_current = ifmr->ifm_active;
605 		if (dummy >= 1) {
606 			int media = IFM_ETHER;
607 			error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
608 		}
609 		break;
610 
611 	case SIOCGIFSTATUS:
612 		ifs = (struct ifstat *)data;
613 		dummy = strlen(ifs->ascii);
614 		if ((sc->tap_flags & TAP_OPEN) && dummy < sizeof(ifs->ascii)) {
615 			if (sc->tap_sigtd && sc->tap_sigtd->sio_proc) {
616 				ksnprintf(ifs->ascii + dummy,
617 				    sizeof(ifs->ascii) - dummy,
618 				    "\tOpened by pid %d\n",
619 				    (int)sc->tap_sigtd->sio_proc->p_pid);
620 			} else {
621 				ksnprintf(ifs->ascii + dummy,
622 				    sizeof(ifs->ascii) - dummy,
623 				    "\tOpened by <unknown>\n");
624 			}
625 		}
626 		break;
627 
628 	default:
629 		error = EINVAL;
630 		break;
631 	}
632 
633 	return (error);
634 }
635 
636 /*
637  * tapifstart
638  *
639  * Queue packets from higher level ready to put out (called with if serializer
640  * held)
641  *
642  * MPSAFE
643  */
644 static void
645 tapifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
646 {
647 	struct tap_softc *sc = ifp->if_softc;
648 	struct ifqueue *ifq;
649 	struct mbuf *m;
650 	int has_data = 0;
651 
652 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
653 	TAPDEBUG(ifp, "starting, minor = %#x\n", minor(sc->tap_dev));
654 
655 	if ((sc->tap_flags & TAP_READY) != TAP_READY) {
656 		TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
657 			 minor(sc->tap_dev), sc->tap_flags);
658 		ifsq_purge(ifsq);
659 		return;
660 	}
661 
662 	ifsq_set_oactive(ifsq);
663 
664 	ifq = &sc->tap_devq;
665 	while ((m = ifsq_dequeue(ifsq)) != NULL) {
666 		if (IF_QFULL(ifq)) {
667 			IF_DROP(ifq);
668 			IFNET_STAT_INC(ifp, oerrors, 1);
669 			m_freem(m);
670 		} else {
671 			IF_ENQUEUE(ifq, m);
672 			IFNET_STAT_INC(ifp, opackets, 1);
673 			has_data = 1;
674 		}
675 	}
676 
677 	if (has_data) {
678 		if (sc->tap_flags & TAP_RWAIT) {
679 			sc->tap_flags &= ~TAP_RWAIT;
680 			wakeup((caddr_t)sc);
681 		}
682 
683 		KNOTE(&sc->tap_rkq.ki_note, 0);
684 
685 		if ((sc->tap_flags & TAP_ASYNC) && (sc->tap_sigio != NULL)) {
686 			get_mplock();
687 			pgsigio(sc->tap_sigio, SIGIO, 0);
688 			rel_mplock();
689 		}
690 	}
691 
692 	ifsq_clr_oactive(ifsq);
693 }
694 
695 static void
696 tapifstop(struct tap_softc *sc, int clear_flags)
697 {
698 	struct ifnet *ifp = &sc->tap_if;
699 
700 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
701 	IF_DRAIN(&sc->tap_devq);
702 	sc->tap_flags &= ~TAP_CLOSEDOWN;
703 	if (clear_flags) {
704 		ifp->if_flags &= ~IFF_RUNNING;
705 		ifsq_clr_oactive(ifq_get_subq_default(&ifp->if_snd));
706 	}
707 }
708 
709 /*
710  * tapioctl
711  *
712  * The ops interface is now pretty minimal.  Called via fileops with nothing
713  * held.
714  *
715  * MPSAFE
716  */
717 static int
718 tapioctl(struct dev_ioctl_args *ap)
719 {
720 	cdev_t dev = ap->a_head.a_dev;
721 	caddr_t data = ap->a_data;
722 	struct tap_softc *sc = dev->si_drv1;
723 	struct ifnet *ifp = &sc->tap_if;
724 	struct ifreq *ifr;
725 	struct tapinfo *tapp = NULL;
726 	struct mbuf *mb;
727 	int error;
728 
729 	ifnet_serialize_all(ifp);
730 	error = 0;
731 
732 	switch (ap->a_cmd) {
733 	case TAPSIFINFO:
734 		tapp = (struct tapinfo *)data;
735 		if (ifp->if_type != tapp->type)
736 			return (EPROTOTYPE);
737 		ifp->if_mtu = tapp->mtu;
738 		ifp->if_baudrate = tapp->baudrate;
739 		break;
740 
741 	case TAPGIFINFO:
742 		tapp = (struct tapinfo *)data;
743 		tapp->mtu = ifp->if_mtu;
744 		tapp->type = ifp->if_type;
745 		tapp->baudrate = ifp->if_baudrate;
746 		break;
747 
748 	case TAPGIFNAME:
749 		ifr = (struct ifreq *)data;
750 		strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
751 		break;
752 
753 	case TAPSDEBUG:
754 		tapdebug = *(int *)data;
755 		break;
756 
757 	case TAPGDEBUG:
758 		*(int *)data = tapdebug;
759 		break;
760 
761 	case FIOASYNC:
762 		if (*(int *)data)
763 			sc->tap_flags |= TAP_ASYNC;
764 		else
765 			sc->tap_flags &= ~TAP_ASYNC;
766 		break;
767 
768 	case FIONREAD:
769 		/* Take a look at devq first */
770 		IF_POLL(&sc->tap_devq, mb);
771 		if (mb != NULL) {
772 			*(int *)data = 0;
773 			for(; mb != NULL; mb = mb->m_next)
774 				*(int *)data += mb->m_len;
775 		} else {
776 			*(int *)data = ifsq_poll_pktlen(
777 			    ifq_get_subq_default(&ifp->if_snd));
778 		}
779 		break;
780 
781 	case FIOSETOWN:
782 		error = fsetown(*(int *)data, &sc->tap_sigio);
783 		if (error == 0)
784 			error = fsetown(*(int *)data, &sc->tap_sigtd);
785 		break;
786 
787 	case FIOGETOWN:
788 		*(int *)data = fgetown(&sc->tap_sigio);
789 		break;
790 
791 	/* this is deprecated, FIOSETOWN should be used instead */
792 	case TIOCSPGRP:
793 		error = fsetown(-(*(int *)data), &sc->tap_sigio);
794 		break;
795 
796 	/* this is deprecated, FIOGETOWN should be used instead */
797 	case TIOCGPGRP:
798 		*(int *)data = -fgetown(&sc->tap_sigio);
799 		break;
800 
801 	/*
802 	 * Support basic control of the network interface via the device file.
803 	 * e.g., vke(4) currently uses the 'SIOCGIFADDR' ioctl.
804 	 */
805 
806 	case SIOCGIFFLAGS:
807 		bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
808 		break;
809 
810 	case SIOCGIFADDR:
811 		bcopy(sc->ether_addr, data, sizeof(sc->ether_addr));
812 		break;
813 
814 	case SIOCSIFADDR:
815 		bcopy(data, sc->ether_addr, sizeof(sc->ether_addr));
816 		break;
817 
818 	default:
819 		error = ENOTTY;
820 		break;
821 	}
822 
823 	ifnet_deserialize_all(ifp);
824 	return (error);
825 }
826 
827 
828 /*
829  * tapread
830  *
831  * The ops read interface - reads a packet at a time, or at
832  * least as much of a packet as can be read.
833  *
834  * Called from the fileops interface with nothing held.
835  *
836  * MPSAFE
837  */
838 static int
839 tapread(struct dev_read_args *ap)
840 {
841 	cdev_t dev = ap->a_head.a_dev;
842 	struct uio *uio = ap->a_uio;
843 	struct tap_softc *sc = dev->si_drv1;
844 	struct ifnet *ifp = &sc->tap_if;
845 	struct mbuf *m0 = NULL;
846 	int error = 0, len;
847 
848 	TAPDEBUG(ifp, "reading, minor = %#x\n", minor(dev));
849 
850 	if ((sc->tap_flags & TAP_READY) != TAP_READY) {
851 		TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
852 			 minor(dev), sc->tap_flags);
853 
854 		return (EHOSTDOWN);
855 	}
856 
857 	sc->tap_flags &= ~TAP_RWAIT;
858 
859 	/* sleep until we get a packet */
860 	do {
861 		ifnet_serialize_all(ifp);
862 		IF_DEQUEUE(&sc->tap_devq, m0);
863 		if (m0 == NULL) {
864 			if (ap->a_ioflag & IO_NDELAY) {
865 				ifnet_deserialize_all(ifp);
866 				return (EWOULDBLOCK);
867 			}
868 			sc->tap_flags |= TAP_RWAIT;
869 			tsleep_interlock(sc, PCATCH);
870 			ifnet_deserialize_all(ifp);
871 			error = tsleep(sc, PCATCH | PINTERLOCKED, "taprd", 0);
872 			if (error)
873 				return (error);
874 		} else {
875 			ifnet_deserialize_all(ifp);
876 		}
877 	} while (m0 == NULL);
878 
879 	BPF_MTAP(ifp, m0);
880 
881 	/* xfer packet to user space */
882 	while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
883 		len = (int)szmin(uio->uio_resid, m0->m_len);
884 		if (len == 0)
885 			break;
886 
887 		error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
888 		m0 = m_free(m0);
889 	}
890 
891 	if (m0 != NULL) {
892 		TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n", minor(dev));
893 		m_freem(m0);
894 	}
895 
896 	return (error);
897 }
898 
899 /*
900  * tapwrite
901  *
902  * The ops write interface - an atomic write is a packet - or else!
903  *
904  * Called from the fileops interface with nothing held.
905  *
906  * MPSAFE
907  */
908 static int
909 tapwrite(struct dev_write_args *ap)
910 {
911 	cdev_t dev = ap->a_head.a_dev;
912 	struct uio *uio = ap->a_uio;
913 	struct tap_softc *sc = dev->si_drv1;
914 	struct ifnet *ifp = &sc->tap_if;
915 	struct mbuf *top = NULL, **mp = NULL, *m = NULL;
916 	int error;
917 	size_t tlen, mlen;
918 
919 	TAPDEBUG(ifp, "writing, minor = %#x\n", minor(dev));
920 
921 	if ((sc->tap_flags & TAP_READY) != TAP_READY) {
922 		TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
923 			 minor(dev), sc->tap_flags);
924 		return (EHOSTDOWN);
925 	}
926 
927 	if (uio->uio_resid == 0)
928 		return (0);
929 
930 	if (uio->uio_resid > TAPMRU) {
931 		TAPDEBUG(ifp, "invalid packet len = %zu, minor = %#x\n",
932 			 uio->uio_resid, minor(dev));
933 
934 		return (EIO);
935 	}
936 	tlen = uio->uio_resid;
937 
938 	/* get a header mbuf */
939 	MGETHDR(m, M_WAITOK, MT_DATA);
940 	if (m == NULL)
941 		return (ENOBUFS);
942 	mlen = MHLEN;
943 
944 	top = NULL;
945 	mp = &top;
946 	error = 0;
947 
948 	while (error == 0 && uio->uio_resid > 0) {
949 		m->m_len = (int)szmin(mlen, uio->uio_resid);
950 		error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
951 		*mp = m;
952 		mp = &m->m_next;
953 		if (uio->uio_resid > 0) {
954 			MGET(m, M_WAITOK, MT_DATA);
955 			if (m == NULL) {
956 				error = ENOBUFS;
957 				break;
958 			}
959 			mlen = MLEN;
960 		}
961 	}
962 	if (error) {
963 		IFNET_STAT_INC(ifp, ierrors, 1);
964 		if (top)
965 			m_freem(top);
966 		return (error);
967 	}
968 
969 	top->m_pkthdr.len = (int)tlen;
970 	top->m_pkthdr.rcvif = ifp;
971 
972 	/*
973 	 * Ethernet bridge and bpf are handled in ether_input
974 	 *
975 	 * adjust mbuf and give packet to the ether_input
976 	 */
977 	ifnet_serialize_all(ifp);
978 	ifp->if_input(ifp, top, NULL, -1);
979 	IFNET_STAT_INC(ifp, ipackets, 1); /* ibytes are counted in ether_input */
980 	ifnet_deserialize_all(ifp);
981 
982 	return (0);
983 }
984 
985 
986 /*
987  * tapkqfilter - called from the fileops interface with nothing held
988  *
989  * MPSAFE
990  */
991 static int
992 tapkqfilter(struct dev_kqfilter_args *ap)
993 {
994 	cdev_t dev = ap->a_head.a_dev;
995 	struct knote *kn = ap->a_kn;
996 	struct tap_softc *sc = dev->si_drv1;
997 	struct klist *list;
998 
999 	list = &sc->tap_rkq.ki_note;
1000 	ap->a_result =0;
1001 
1002 	switch (kn->kn_filter) {
1003 	case EVFILT_READ:
1004 		kn->kn_fop = &tapread_filtops;
1005 		kn->kn_hook = (void *)sc;
1006 		break;
1007 	case EVFILT_WRITE:
1008 		kn->kn_fop = &tapwrite_filtops;
1009 		kn->kn_hook = (void *)sc;
1010 		break;
1011 	default:
1012 		ap->a_result = EOPNOTSUPP;
1013 		return (0);
1014 	}
1015 
1016 	knote_insert(list, kn);
1017 	return (0);
1018 }
1019 
1020 static int
1021 tap_filter_read(struct knote *kn, long hint)
1022 {
1023 	struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1024 
1025 	if (IF_QEMPTY(&sc->tap_devq) == 0)	/* XXX serializer */
1026 		return (1);
1027 	else
1028 		return (0);
1029 }
1030 
1031 static int
1032 tap_filter_write(struct knote *kn, long hint)
1033 {
1034 	/* Always ready for a write */
1035 	return (1);
1036 }
1037 
1038 static void
1039 tap_filter_detach(struct knote *kn)
1040 {
1041 	struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1042 
1043 	knote_remove(&sc->tap_rkq.ki_note, kn);
1044 }
1045