From 252be4c402afa5c4e35b827f272e3c52e8dbe46f Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Thu, 30 Aug 2018 21:36:07 +0800 Subject: [PATCH] if_tun: Disallow to destroy an opened device When a tun device (created via 'ifconfig') is opened, disallow to destroy it (e.g., ifconfig destroy). Otherwise, a panic occurs when the caller closes the device, since it has been already destroyed. Following the previous fix to if_tap. --- sys/net/tun/if_tun.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sys/net/tun/if_tun.c b/sys/net/tun/if_tun.c index 73a581295d..793c917c8f 100644 --- a/sys/net/tun/if_tun.c +++ b/sys/net/tun/if_tun.c @@ -324,9 +324,13 @@ tunclose(struct dev_close_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tun_softc *sc = dev->si_drv1; - struct ifnet *ifp = sc->tun_ifp; + struct ifnet *ifp; int unit = minor(dev); + KASSERT(sc != NULL, + ("try closing the already destroyed %s%d", TUN, unit)); + ifp = sc->tun_ifp; + sc->tun_flags &= ~TUN_OPEN; sc->tun_pid = 0; @@ -423,8 +427,10 @@ tun_clone_destroy(struct ifnet * ifp) { struct tun_softc *sc = ifp->if_softc; + if (sc->tun_flags & TUN_OPEN) + return (EBUSY); if ((sc->tun_flags & TUN_CLONE) == 0) - return (ENXIO); + return (EINVAL); TUNDEBUG(ifp, "clone destroyed, minor = %#x, flags = 0x%x\n", minor(sc->tun_dev), sc->tun_flags); -- 2.41.0