xref: /dragonfly/sys/net/tap/if_tap.c (revision 1cef5f30)
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 ifstat *ifs = NULL;
567 	struct ifmediareq *ifmr = NULL;
568 	int error = 0;
569 	int dummy;
570 
571 	switch (cmd) {
572 	case SIOCADDMULTI:
573 	case SIOCDELMULTI:
574 		break;
575 
576 	case SIOCSIFADDR:
577 	case SIOCGIFADDR:
578 	case SIOCSIFMTU:
579 		error = ether_ioctl(ifp, cmd, data);
580 		break;
581 
582 	case SIOCSIFFLAGS:
583 		tapifflags(sc);
584 		break;
585 
586 	case SIOCGIFMEDIA:
587 		/*
588 		 * The bridge code needs this when running the
589 		 * spanning tree protocol.
590 		 */
591 		ifmr = (struct ifmediareq *)data;
592 		dummy = ifmr->ifm_count;
593 		ifmr->ifm_count = 1;
594 		ifmr->ifm_status = IFM_AVALID;
595 		ifmr->ifm_active = IFM_ETHER;
596 		if (sc->tap_flags & TAP_OPEN)
597 			ifmr->ifm_status |= IFM_ACTIVE;
598 		ifmr->ifm_current = ifmr->ifm_active;
599 		if (dummy >= 1) {
600 			int media = IFM_ETHER;
601 			error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
602 		}
603 		break;
604 
605 	case SIOCGIFSTATUS:
606 		ifs = (struct ifstat *)data;
607 		dummy = strlen(ifs->ascii);
608 		if ((sc->tap_flags & TAP_OPEN) && dummy < sizeof(ifs->ascii)) {
609 			if (sc->tap_sigtd && sc->tap_sigtd->sio_proc) {
610 				ksnprintf(ifs->ascii + dummy,
611 				    sizeof(ifs->ascii) - dummy,
612 				    "\tOpened by pid %d\n",
613 				    (int)sc->tap_sigtd->sio_proc->p_pid);
614 			} else {
615 				ksnprintf(ifs->ascii + dummy,
616 				    sizeof(ifs->ascii) - dummy,
617 				    "\tOpened by <unknown>\n");
618 			}
619 		}
620 		break;
621 
622 	default:
623 		error = EINVAL;
624 		break;
625 	}
626 
627 	return (error);
628 }
629 
630 /*
631  * tapifstart
632  *
633  * Queue packets from higher level ready to put out (called with if serializer
634  * held)
635  *
636  * MPSAFE
637  */
638 static void
639 tapifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
640 {
641 	struct tap_softc *sc = ifp->if_softc;
642 	struct ifqueue *ifq;
643 	struct mbuf *m;
644 	int has_data = 0;
645 
646 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
647 	TAPDEBUG(ifp, "starting, minor = %#x\n", minor(sc->tap_dev));
648 
649 	if ((sc->tap_flags & TAP_READY) != TAP_READY) {
650 		TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
651 			 minor(sc->tap_dev), sc->tap_flags);
652 		ifsq_purge(ifsq);
653 		return;
654 	}
655 
656 	ifsq_set_oactive(ifsq);
657 
658 	ifq = &sc->tap_devq;
659 	while ((m = ifsq_dequeue(ifsq)) != NULL) {
660 		if (IF_QFULL(ifq)) {
661 			IF_DROP(ifq);
662 			IFNET_STAT_INC(ifp, oerrors, 1);
663 			m_freem(m);
664 		} else {
665 			IF_ENQUEUE(ifq, m);
666 			IFNET_STAT_INC(ifp, opackets, 1);
667 			has_data = 1;
668 		}
669 	}
670 
671 	if (has_data) {
672 		if (sc->tap_flags & TAP_RWAIT) {
673 			sc->tap_flags &= ~TAP_RWAIT;
674 			wakeup((caddr_t)sc);
675 		}
676 
677 		KNOTE(&sc->tap_rkq.ki_note, 0);
678 
679 		if ((sc->tap_flags & TAP_ASYNC) && (sc->tap_sigio != NULL)) {
680 			get_mplock();
681 			pgsigio(sc->tap_sigio, SIGIO, 0);
682 			rel_mplock();
683 		}
684 	}
685 
686 	ifsq_clr_oactive(ifsq);
687 }
688 
689 static void
690 tapifstop(struct tap_softc *sc, int clear_flags)
691 {
692 	struct ifnet *ifp = &sc->tap_if;
693 
694 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
695 	IF_DRAIN(&sc->tap_devq);
696 	sc->tap_flags &= ~TAP_CLOSEDOWN;
697 	if (clear_flags) {
698 		ifp->if_flags &= ~IFF_RUNNING;
699 		ifsq_clr_oactive(ifq_get_subq_default(&ifp->if_snd));
700 	}
701 }
702 
703 /*
704  * tapioctl
705  *
706  * The ops interface is now pretty minimal.  Called via fileops with nothing
707  * held.
708  *
709  * MPSAFE
710  */
711 static int
712 tapioctl(struct dev_ioctl_args *ap)
713 {
714 	cdev_t dev = ap->a_head.a_dev;
715 	caddr_t data = ap->a_data;
716 	struct tap_softc *sc = dev->si_drv1;
717 	struct ifnet *ifp = &sc->tap_if;
718 	struct ifreq *ifr;
719 	struct tapinfo *tapp = NULL;
720 	struct mbuf *mb;
721 	int error;
722 
723 	ifnet_serialize_all(ifp);
724 	error = 0;
725 
726 	switch (ap->a_cmd) {
727 	case TAPSIFINFO:
728 		tapp = (struct tapinfo *)data;
729 		if (ifp->if_type != tapp->type)
730 			return (EPROTOTYPE);
731 		ifp->if_mtu = tapp->mtu;
732 		ifp->if_baudrate = tapp->baudrate;
733 		break;
734 
735 	case TAPGIFINFO:
736 		tapp = (struct tapinfo *)data;
737 		tapp->mtu = ifp->if_mtu;
738 		tapp->type = ifp->if_type;
739 		tapp->baudrate = ifp->if_baudrate;
740 		break;
741 
742 	case TAPGIFNAME:
743 		ifr = (struct ifreq *)data;
744 		strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
745 		break;
746 
747 	case TAPSDEBUG:
748 		tapdebug = *(int *)data;
749 		break;
750 
751 	case TAPGDEBUG:
752 		*(int *)data = tapdebug;
753 		break;
754 
755 	case FIOASYNC:
756 		if (*(int *)data)
757 			sc->tap_flags |= TAP_ASYNC;
758 		else
759 			sc->tap_flags &= ~TAP_ASYNC;
760 		break;
761 
762 	case FIONREAD:
763 		/* Take a look at devq first */
764 		IF_POLL(&sc->tap_devq, mb);
765 		if (mb != NULL) {
766 			*(int *)data = 0;
767 			for(; mb != NULL; mb = mb->m_next)
768 				*(int *)data += mb->m_len;
769 		} else {
770 			*(int *)data = ifsq_poll_pktlen(
771 			    ifq_get_subq_default(&ifp->if_snd));
772 		}
773 		break;
774 
775 	case FIOSETOWN:
776 		error = fsetown(*(int *)data, &sc->tap_sigio);
777 		if (error == 0)
778 			error = fsetown(*(int *)data, &sc->tap_sigtd);
779 		break;
780 
781 	case FIOGETOWN:
782 		*(int *)data = fgetown(&sc->tap_sigio);
783 		break;
784 
785 	/* this is deprecated, FIOSETOWN should be used instead */
786 	case TIOCSPGRP:
787 		error = fsetown(-(*(int *)data), &sc->tap_sigio);
788 		break;
789 
790 	/* this is deprecated, FIOGETOWN should be used instead */
791 	case TIOCGPGRP:
792 		*(int *)data = -fgetown(&sc->tap_sigio);
793 		break;
794 
795 	/*
796 	 * Support basic control of the network interface via the device file.
797 	 * e.g., vke(4) currently uses the 'SIOCGIFADDR' ioctl.
798 	 */
799 
800 	case SIOCGIFFLAGS:
801 		bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
802 		break;
803 
804 	case SIOCGIFADDR:
805 		bcopy(sc->ether_addr, data, sizeof(sc->ether_addr));
806 		break;
807 
808 	case SIOCSIFADDR:
809 		bcopy(data, sc->ether_addr, sizeof(sc->ether_addr));
810 		break;
811 
812 	default:
813 		error = ENOTTY;
814 		break;
815 	}
816 
817 	ifnet_deserialize_all(ifp);
818 	return (error);
819 }
820 
821 
822 /*
823  * tapread
824  *
825  * The ops read interface - reads a packet at a time, or at
826  * least as much of a packet as can be read.
827  *
828  * Called from the fileops interface with nothing held.
829  *
830  * MPSAFE
831  */
832 static int
833 tapread(struct dev_read_args *ap)
834 {
835 	cdev_t dev = ap->a_head.a_dev;
836 	struct uio *uio = ap->a_uio;
837 	struct tap_softc *sc = dev->si_drv1;
838 	struct ifnet *ifp = &sc->tap_if;
839 	struct mbuf *m0 = NULL;
840 	int error = 0, len;
841 
842 	TAPDEBUG(ifp, "reading, minor = %#x\n", minor(dev));
843 
844 	if ((sc->tap_flags & TAP_READY) != TAP_READY) {
845 		TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
846 			 minor(dev), sc->tap_flags);
847 
848 		return (EHOSTDOWN);
849 	}
850 
851 	sc->tap_flags &= ~TAP_RWAIT;
852 
853 	/* sleep until we get a packet */
854 	do {
855 		ifnet_serialize_all(ifp);
856 		IF_DEQUEUE(&sc->tap_devq, m0);
857 		if (m0 == NULL) {
858 			if (ap->a_ioflag & IO_NDELAY) {
859 				ifnet_deserialize_all(ifp);
860 				return (EWOULDBLOCK);
861 			}
862 			sc->tap_flags |= TAP_RWAIT;
863 			tsleep_interlock(sc, PCATCH);
864 			ifnet_deserialize_all(ifp);
865 			error = tsleep(sc, PCATCH | PINTERLOCKED, "taprd", 0);
866 			if (error)
867 				return (error);
868 		} else {
869 			ifnet_deserialize_all(ifp);
870 		}
871 	} while (m0 == NULL);
872 
873 	BPF_MTAP(ifp, m0);
874 
875 	/* xfer packet to user space */
876 	while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
877 		len = (int)szmin(uio->uio_resid, m0->m_len);
878 		if (len == 0)
879 			break;
880 
881 		error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
882 		m0 = m_free(m0);
883 	}
884 
885 	if (m0 != NULL) {
886 		TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n", minor(dev));
887 		m_freem(m0);
888 	}
889 
890 	return (error);
891 }
892 
893 /*
894  * tapwrite
895  *
896  * The ops write interface - an atomic write is a packet - or else!
897  *
898  * Called from the fileops interface with nothing held.
899  *
900  * MPSAFE
901  */
902 static int
903 tapwrite(struct dev_write_args *ap)
904 {
905 	cdev_t dev = ap->a_head.a_dev;
906 	struct uio *uio = ap->a_uio;
907 	struct tap_softc *sc = dev->si_drv1;
908 	struct ifnet *ifp = &sc->tap_if;
909 	struct mbuf *top = NULL, **mp = NULL, *m = NULL;
910 	int error;
911 	size_t tlen, mlen;
912 
913 	TAPDEBUG(ifp, "writing, minor = %#x\n", minor(dev));
914 
915 	if ((sc->tap_flags & TAP_READY) != TAP_READY) {
916 		TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
917 			 minor(dev), sc->tap_flags);
918 		return (EHOSTDOWN);
919 	}
920 
921 	if (uio->uio_resid == 0)
922 		return (0);
923 
924 	if (uio->uio_resid > TAPMRU) {
925 		TAPDEBUG(ifp, "invalid packet len = %zu, minor = %#x\n",
926 			 uio->uio_resid, minor(dev));
927 
928 		return (EIO);
929 	}
930 	tlen = uio->uio_resid;
931 
932 	/* get a header mbuf */
933 	MGETHDR(m, M_WAITOK, MT_DATA);
934 	if (m == NULL)
935 		return (ENOBUFS);
936 	mlen = MHLEN;
937 
938 	top = NULL;
939 	mp = &top;
940 	error = 0;
941 
942 	while (error == 0 && uio->uio_resid > 0) {
943 		m->m_len = (int)szmin(mlen, uio->uio_resid);
944 		error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
945 		*mp = m;
946 		mp = &m->m_next;
947 		if (uio->uio_resid > 0) {
948 			MGET(m, M_WAITOK, MT_DATA);
949 			if (m == NULL) {
950 				error = ENOBUFS;
951 				break;
952 			}
953 			mlen = MLEN;
954 		}
955 	}
956 	if (error) {
957 		IFNET_STAT_INC(ifp, ierrors, 1);
958 		if (top)
959 			m_freem(top);
960 		return (error);
961 	}
962 
963 	top->m_pkthdr.len = (int)tlen;
964 	top->m_pkthdr.rcvif = ifp;
965 
966 	/*
967 	 * Ethernet bridge and bpf are handled in ether_input
968 	 *
969 	 * adjust mbuf and give packet to the ether_input
970 	 */
971 	ifnet_serialize_all(ifp);
972 	ifp->if_input(ifp, top, NULL, -1);
973 	IFNET_STAT_INC(ifp, ipackets, 1); /* ibytes are counted in ether_input */
974 	ifnet_deserialize_all(ifp);
975 
976 	return (0);
977 }
978 
979 
980 /*
981  * tapkqfilter - called from the fileops interface with nothing held
982  *
983  * MPSAFE
984  */
985 static int
986 tapkqfilter(struct dev_kqfilter_args *ap)
987 {
988 	cdev_t dev = ap->a_head.a_dev;
989 	struct knote *kn = ap->a_kn;
990 	struct tap_softc *sc = dev->si_drv1;
991 	struct klist *list;
992 
993 	list = &sc->tap_rkq.ki_note;
994 	ap->a_result =0;
995 
996 	switch (kn->kn_filter) {
997 	case EVFILT_READ:
998 		kn->kn_fop = &tapread_filtops;
999 		kn->kn_hook = (void *)sc;
1000 		break;
1001 	case EVFILT_WRITE:
1002 		kn->kn_fop = &tapwrite_filtops;
1003 		kn->kn_hook = (void *)sc;
1004 		break;
1005 	default:
1006 		ap->a_result = EOPNOTSUPP;
1007 		return (0);
1008 	}
1009 
1010 	knote_insert(list, kn);
1011 	return (0);
1012 }
1013 
1014 static int
1015 tap_filter_read(struct knote *kn, long hint)
1016 {
1017 	struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1018 
1019 	if (IF_QEMPTY(&sc->tap_devq) == 0)	/* XXX serializer */
1020 		return (1);
1021 	else
1022 		return (0);
1023 }
1024 
1025 static int
1026 tap_filter_write(struct knote *kn, long hint)
1027 {
1028 	/* Always ready for a write */
1029 	return (1);
1030 }
1031 
1032 static void
1033 tap_filter_detach(struct knote *kn)
1034 {
1035 	struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1036 
1037 	knote_remove(&sc->tap_rkq.ki_note, kn);
1038 }
1039