On Sun, Nov 08, 2026 at 08:57:11PM +0000, [email protected] wrote:
> >Synopsis:    nvgre_down() clears IFF_RUNNING, releases NET_LOCK for
>               ifq_barrier()/taskq_del_barrier(), and only after re-taking it
>               undoes the configuration-dependent state: the parent's detach 
> and
>               link-state hooks, the multicast group, the tunnel trees.  The
>               nvgre reconfiguration ioctls are refused only while IFF_RUNNING
>               is set, so during that window "-tunnel", "tunnel" (other address
>               family), "parent"/"-parent" and "vnetid" can change the state
>               nvgre_down() is about to use.  Seen: panic "unhandled af 0",
>               the multicast membership freed by the other family's function
>               ("free: size too large"/"too small ... type in_multi"), and a
>               page fault in if_detachhook_del() on the wrong parent.
> >Category:    kernel
> >Severity:    serious
> >Priority:    medium
> >Class:               sw-bug
> >Release:     OpenBSD 8.0-current (source as of 2026-11-08)
> >Environment:
>       System      : OpenBSD 8.0-current
>       Details     : OpenBSD 8.0-current (GENERIC.MP as shipped: DIAGNOSTIC,
>                     POOL_DEBUG)
>       Architecture: OpenBSD.amd64
>       Machine     : amd64 (QEMU/KVM guest, 2 vcpu)
>       Source      : sys/net/if_gre.c rev 1.193, openbsd/src master as of
>                     2026-11-08, git afb08c4d5 (the same code is in
>                     d5b06e73, r1.192)
> 
> >Description:
> 
>       nvgre_down() (sys/net/if_gre.c):
> 
>           CLR(ifp->if_flags, IFF_RUNNING);
> 
>           NET_UNLOCK();
>           ifq_barrier(&ifp->if_snd);
>           taskq_del_barrier(softnet, &sc->sc_send_task);
>           NET_LOCK();
> 
>           mq_purge(&sc->sc_send_list);
> 
>           ifp0 = if_get(sc->sc_ifp0);
>           if (ifp0 != NULL) {
>                   if_detachhook_del(ifp0, &sc->sc_dtask);
>                   if_linkstatehook_del(ifp0, &sc->sc_ltask);
>           }
>           if_put(ifp0);
> 
>           switch (tunnel->t_af) {
>           case AF_INET:   in_delmulti(sc->sc_inm); break;
>           case AF_INET6:  in6_delmulti(sc->sc_inm); break;
>           default:        unhandled_af(tunnel->t_af);
>           }
> 
>           RBT_REMOVE(nvgre_ucast_tree, &nvgre_ucast_tree, sc);
>           RBT_REMOVE(nvgre_mcast_tree, &nvgre_mcast_tree, sc);
> 
>       The reconfiguration ioctls in nvgre_ioctl() (SIOCSLIFPHYADDR,
>       SIOCDIFPHYADDR, SIOCSIFPARENT, SIOCDIFPARENT, SIOCSVNETID, ...) return
>       EBUSY only while IFF_RUNNING is set.  It is already clear while
>       nvgre_down() sleeps in the barriers, so they run and change
>       sc_ifp0, tunnel->t_af, the tunnel addresses or the vnetid.  After
>       NET_LOCK is re-taken nvgre_down() then
>         - panics in unhandled_af() if "-tunnel" set t_af to AF_UNSPEC:
> 
>           panic: unhandled af 0
>           unhandled_af(0) at unhandled_af+0x1a
>           nvgre_down() at nvgre_down+0x1a9
>           nvgre_ioctl(..., SIOCSIFFLAGS, ...) at nvgre_ioctl+0x3c2
> 
>         - frees sc_inm with the wrong family's function if "tunnel"
>           switched between IPv4 and IPv6 (in either direction):
> 
>           panic: free: size too large 80 > 64 (0xffff800000e04b00) type 
> in_multi
>           free() at free+0x3f2
>           in6_delmulti() at in6_delmulti+0x16c
>           nvgre_down() at nvgre_down+0x16a
> 
>         - removes the hooks from whatever sc_ifp0 now names, after
>           "-parent"/"parent", and faults (seen with an earlier, partial
>           fix that only snapshotted t_af):
> 
>           uvm_fault(..., 0x7, 0, 2) -> e
>           if_detachhook_del() at if_detachhook_del+0x35
>           nvgre_down() at nvgre_down+0x155
> 
>       and RBT_REMOVE() runs with keys that no longer match how the softc
>       was inserted.
> 
> >How-To-Repeat:
> 
>       As root, pasted into one shell (vether200, nvgre200 and rdomain 200
>       unused).  The four loops run in the background with their output
>       discarded, for three minutes, then are stopped:
> 
>           ifconfig vether200 create rdomain 200 192.0.2.1/24 up
>           ifconfig nvgre200 create rdomain 200 tunneldomain 200 \
>               tunnel 192.0.2.1 239.0.2.1 parent vether200 vnetid 200
>           (while :; do
>               ifconfig nvgre200 -tunnel
>               ifconfig nvgre200 tunnel 192.0.2.1 239.0.2.1
>           done) >/dev/null 2>&1 &
>           (while :; do
>               ifconfig nvgre200 tunnel 2001:db8:200::1 ff12::200
>               ifconfig nvgre200 tunnel 192.0.2.1 239.0.2.1
>               ifconfig nvgre200 vnetid $(( (RANDOM % 1000) + 256 ))
>               ifconfig nvgre200 -parent
>               ifconfig nvgre200 parent vether200
>           done) >/dev/null 2>&1 &
>           (while :; do
>               ifconfig nvgre200 up
>               ifconfig nvgre200 down
>           done) >/dev/null 2>&1 &
>           (while :; do
>               ifconfig nvgre200 down
>               ifconfig nvgre200 up
>           done) >/dev/null 2>&1 &
>           sleep 180
>           kill $(jobs -p)
> 
>       The machine panics in nvgre_down(), usually within a minute.
> 
> >Fix:
> 
>       Please consider this diff for discussion.  It undoes everything
>       that depends on the configuration before NET_LOCK is released,
>       while the ioctls still cannot change it; only the queue barriers
>       and the purge of the send list remain after the sleep.  A
>       concurrent "up" during the sleep sets everything up again from
>       scratch through nvgre_up(), which is consistent.
> 
>       Locking: this takes no new lock and does not make nvgre_down()
>       hold NET_LOCK any longer.  The moved operations (two hook
>       deletions, the multicast leave, two RBT_REMOVEs) already ran
>       under NET_LOCK, after the barriers; they now run before the
>       unlock instead.  The barriers still run without NET_LOCK.  A
>       larger alternative that keeps the current order -- a per-softc
>       lock serialising up/down plus EBUSY on reconfiguration while
>       down is in progress, like vxlan(4) -- was also built and passed
>       the same A/B (3/3 crash -> 3/3 survive, also with WITNESS, no
>       lock order reversals); available on request.
> 
> Index: sys/net/if_gre.c
> ===================================================================
> --- sys/net/if_gre.c
> +++ sys/net/if_gre.c
> @@ -3564,13 +3564,12 @@ nvgre_down(struct nvgre_softc *sc)
>  
>       CLR(ifp->if_flags, IFF_RUNNING);
>  
> -     NET_UNLOCK();
> -     ifq_barrier(&ifp->if_snd);
> -     taskq_del_barrier(softnet, &sc->sc_send_task);
> -     NET_LOCK();
> -
> -     mq_purge(&sc->sc_send_list);
> -
> +     /*
> +      * Undo everything that depends on the configuration (parent, tunnel
> +      * address family, multicast group) before NET_LOCK is released below:
> +      * the reconfiguration ioctls only check IFF_RUNNING, which is now
> +      * clear, so they can change the configuration while we sleep.
> +      */
>       ifp0 = if_get(sc->sc_ifp0);
>       if (ifp0 != NULL) {
>               if_detachhook_del(ifp0, &sc->sc_dtask);
> @@ -3595,6 +3594,13 @@ nvgre_down(struct nvgre_softc *sc)
>       RBT_REMOVE(nvgre_ucast_tree, &nvgre_ucast_tree, sc);
>       RBT_REMOVE(nvgre_mcast_tree, &nvgre_mcast_tree, sc);
>  
> +     NET_UNLOCK();
> +     ifq_barrier(&ifp->if_snd);
> +     taskq_del_barrier(softnet, &sc->sc_send_task);
> +     NET_LOCK();
> +
> +     mq_purge(&sc->sc_send_list);
> +
>       return (0);
>  }
>  
> 
> >Verification:
> 
>       The diff above (md5 eed528bfd6698ab73142953af21a784f) applies with
>       patch -p0 -F0 to sys/net/if_gre.c r1.193 (master afb08c4d5) without
>       offset, and to r1.192 (d5b06e73) with offset 3, no fuzz.  Kernels:
>       master afb08c4d5 GENERIC.MP as shipped, amd64, no warnings; patched
>       = the same tree + the diff.  Every run on a freshly booted kernel
>       (2-vcpu guest), the four loops above run for 180 s by a script
>       (errors discarded):
> 
>         unpatched  6/6 crashed: "unhandled af 0" in nvgre_down() (1),
>                    "free: size too large 80 > 64 ... in_multi" and
>                    "free: size too small 64 <= 128 / 2 ... in_multi"
>                    (in6_delmulti() on an IPv4 membership and in_delmulti()
>                    on an IPv6 one, from nvgre_down()) (2), and a stop in ddb
>                    with an unusable trace ("SPL NOT LOWERED ON TRAP EXIT",
>                    stopped in proc_trampoline / savectx) (3)
>         patched    3/3 survived the full 180 s, no kernel messages
> 
>       The How-To-Repeat exactly as printed (one shell, 180 s), on master
>       afb08c4d5 GENERIC.MP (unpatched md5 acbccac0, patched md5 debcea40),
>       fresh boot per run, VM 142:
>         unpatched  3/3 panic: "free: size too large 80 > 64 ... in_multi",
>                    "free: size too small 64 <= 128 / 2 ... in_multi",
>                    "unhandled af 0"
>         patched    3/3 survived
>       The same with the loops' output going to the console instead of
>       /dev/null: unpatched 1/1 panic (in_multi free), so the output does
>       not hide the race.  (The block above is laid out one command per line 
> for
>       mail; the tested script had the same commands in the same loops.
>       An earlier four-shell version of the loops:
>       unpatched 1/1 panic, patched 1/1 survived.)
> 
>       (An earlier, partial fix that only snapshotted t_af and moved the
>       RBT_REMOVEs before the sleep still crashed 3/3, in
>       if_detachhook_del() as shown above, which is why the whole
>       configuration-dependent teardown is moved.)
> 
>       Normal use on the patched kernel, 3/3, 7 checks each, no kernel
>       messages (net.inet.gre.allow=1): two nvgre(4) over a pair(4) link
>       between rdomains, ping across, down/up and ping again, "-tunnel" and
>       "tunnel" while down then up and ping again, destroy both.  The same
>       test passes on the unpatched kernel.

could you try this?

the tl;dr is that it moves use of the nvgre tunnel configuration
in the packet processing paths under NET_LOCK, so it will be
serialised with nvgre_up and nvgre_down. nvgre rx runs as part of the ip
stack, so it already has the net lock. nvgre tx needs the net lock to
call ip_output anyway.

a big part of this change is to force nvgre_start to always run
from the ifq bundle task on a softnet thread so it can be sure about
the state of NET_LOCK, which seems to work well in rport(4).

using the ifq and the bundle task ends up simplifying things anyway.

Index: if_gre.c
===================================================================
RCS file: /cvs/src/sys/net/if_gre.c,v
diff -u -p -r1.196 if_gre.c
--- if_gre.c    10 Nov 2026 05:51:16 -0000      1.196
+++ if_gre.c    11 Nov 2026 03:36:44 -0000
@@ -405,9 +405,6 @@ struct nvgre_softc {
 
        struct arpcom            sc_ac;
 
-       struct mbuf_queue        sc_send_list;
-       struct task              sc_send_task;
-
        void                    *sc_inm;
        struct task              sc_ltask;
        struct task              sc_dtask;
@@ -436,7 +433,8 @@ RBT_PROTOTYPE(nvgre_mcast_tree, nvgre_so
 static int     nvgre_clone_create(struct if_clone *, int);
 static int     nvgre_clone_destroy(struct ifnet *);
 
-static void    nvgre_start(struct ifnet *);
+static int     nvgre_enqueue(struct ifnet *, struct mbuf *m);
+static void    nvgre_start(struct ifqueue *);
 static int     nvgre_ioctl(struct ifnet *, u_long, caddr_t);
 
 static int     nvgre_up(struct nvgre_softc *);
@@ -447,7 +445,6 @@ static void nvgre_detach(void *);
 
 static int     nvgre_input(const struct gre_tunnel *, struct mbuf *, int,
                    uint8_t, struct netstack *);
-static void    nvgre_send(void *);
 
 static int     nvgre_add_addr(struct nvgre_softc *, const struct ifbareq *);
 static int     nvgre_del_addr(struct nvgre_softc *, const struct ifbareq *);
@@ -836,8 +833,9 @@ nvgre_clone_create(struct if_clone *ifc,
        ifp->if_softc = sc;
        ifp->if_hardmtu = ETHER_MAX_HARDMTU_LEN;
        ifp->if_ioctl = nvgre_ioctl;
-       ifp->if_start = nvgre_start;
-       ifp->if_xflags = IFXF_CLONED;
+       ifp->if_enqueue = nvgre_enqueue;
+       ifp->if_qstart = nvgre_start;
+       ifp->if_xflags = IFXF_CLONED | IFXF_MPSAFE;
        ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
        ether_fakeaddr(ifp);
 
@@ -850,8 +848,6 @@ nvgre_clone_create(struct if_clone *ifc,
        tunnel->t_key = htonl((NVGRE_VSID_RES_MAX + 1) <<
            GRE_KEY_ENTROPY_SHIFT);
 
-       mq_init(&sc->sc_send_list, IFQ_MAXLEN * 2, IPL_SOFTNET);
-       task_set(&sc->sc_send_task, nvgre_send, sc);
        task_set(&sc->sc_ltask, nvgre_link_change, sc);
        task_set(&sc->sc_dtask, nvgre_detach, sc);
 
@@ -3472,7 +3468,7 @@ nvgre_up(struct nvgre_softc *sc)
                goto put;
        }
 
-       NET_ASSERT_LOCKED();
+       NET_ASSERT_LOCKED_EXCLUSIVE();
 
        if (RBT_INSERT(nvgre_mcast_tree, &nvgre_mcast_tree, sc) != NULL) {
                error = EADDRINUSE;
@@ -3534,20 +3530,11 @@ nvgre_down(struct nvgre_softc *sc)
 {
        struct gre_tunnel *tunnel = &sc->sc_tunnel;
        struct ifnet *ifp = &sc->sc_ac.ac_if;
-       struct taskq *softnet = net_tq(ifp->if_index);
        struct ifnet *ifp0;
 
-       NET_ASSERT_LOCKED();
-
+       NET_ASSERT_LOCKED_EXCLUSIVE();
        CLR(ifp->if_flags, IFF_RUNNING);
 
-       NET_UNLOCK();
-       ifq_barrier(&ifp->if_snd);
-       taskq_del_barrier(softnet, &sc->sc_send_task);
-       NET_LOCK();
-
-       mq_purge(&sc->sc_send_list);
-
        ifp0 = if_get(sc->sc_ifp0);
        if (ifp0 != NULL) {
                if_detachhook_del(ifp0, &sc->sc_dtask);
@@ -3696,9 +3683,71 @@ nvgre_del_addr(struct nvgre_softc *sc, c
        return (etherbridge_del_addr(&sc->sc_eb, 0, &ifba->ifba_dst));
 }
 
+static int
+nvgre_enqueue(struct ifnet *ifp, struct mbuf *m)
+{
+       struct ifqueue *ifq = &ifp->if_snd;
+       int error;
+
+       if (!atomic_load_int(&gre_allow)) {
+               m_freem(m);
+               return (EACCES);
+       }
+
+       error = ifq_enqueue(ifq, m);
+       if (error != 0)
+               return (error);
+
+       /* always defer to a softnet thread to control NET_LOCK scope */
+       task_add(ifq->ifq_softnet, &ifq->ifq_bundle);
+
+       return (0);
+}
+
+static uint64_t
+nvgre_send4(struct nvgre_softc *sc, struct mbuf_list *ml)
+{
+       struct ip_moptions imo;
+       struct mbuf *m;
+       uint64_t oerrors = 0;
+
+       imo.imo_ifidx = sc->sc_ifp0;
+       imo.imo_ttl = sc->sc_tunnel.t_ttl;
+       imo.imo_loop = 0;
+
+       while ((m = ml_dequeue(ml)) != NULL) {
+               if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &imo, NULL, 0) != 0)
+                       oerrors++;
+       }
+
+       return (oerrors);
+}
+
+#ifdef INET6
+static uint64_t
+nvgre_send6(struct nvgre_softc *sc, struct mbuf_list *ml)
+{
+       struct ip6_moptions im6o;
+       struct mbuf *m;
+       uint64_t oerrors = 0;
+
+       im6o.im6o_ifidx = sc->sc_ifp0;
+       im6o.im6o_hlim = sc->sc_tunnel.t_ttl;
+       im6o.im6o_loop = 0;
+
+       while ((m = ml_dequeue(ml)) != NULL) {
+               if (ip6_output(m, NULL, NULL, 0, &im6o, NULL) != 0)
+                       oerrors++;
+       }
+
+       return (oerrors);
+}
+#endif /* INET6 */
+
 static void
-nvgre_start(struct ifnet *ifp)
+nvgre_start(struct ifqueue *ifq)
 {
+       struct ifnet *ifp = ifq->ifq_if;
        struct nvgre_softc *sc = ifp->if_softc;
        const struct gre_tunnel *tunnel = &sc->sc_tunnel;
        union gre_addr gateway;
@@ -3709,12 +3758,15 @@ nvgre_start(struct ifnet *ifp)
        caddr_t if_bpf;
 #endif
 
-       if (!atomic_load_int(&gre_allow)) {
-               ifq_purge(&ifp->if_snd);
+       NET_LOCK_SHARED();
+
+       if (!ISSET(ifp->if_flags, IFF_RUNNING)) {
+               ifq_purge(ifq);
+               NET_UNLOCK_SHARED();
                return;
        }
 
-       while ((m0 = ifq_dequeue(&ifp->if_snd)) != NULL) {
+       while ((m0 = ifq_dequeue(ifq)) != NULL) {
 #if NBPFILTER > 0
                if_bpf = ifp->if_bpf;
                if (if_bpf)
@@ -3768,87 +3820,27 @@ nvgre_start(struct ifnet *ifp)
        }
 
        if (!ml_empty(&ml)) {
-               if (mq_enlist(&sc->sc_send_list, &ml) == 0)
-                       task_add(net_tq(ifp->if_index), &sc->sc_send_task);
-               /* else set OACTIVE? */
-       }
-}
-
-static uint64_t
-nvgre_send4(struct nvgre_softc *sc, struct mbuf_list *ml)
-{
-       struct ip_moptions imo;
-       struct mbuf *m;
-       uint64_t oerrors = 0;
-
-       imo.imo_ifidx = sc->sc_ifp0;
-       imo.imo_ttl = sc->sc_tunnel.t_ttl;
-       imo.imo_loop = 0;
-
-       NET_LOCK();
-       while ((m = ml_dequeue(ml)) != NULL) {
-               if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &imo, NULL, 0) != 0)
-                       oerrors++;
-       }
-       NET_UNLOCK();
-
-       return (oerrors);
-}
-
-#ifdef INET6
-static uint64_t
-nvgre_send6(struct nvgre_softc *sc, struct mbuf_list *ml)
-{
-       struct ip6_moptions im6o;
-       struct mbuf *m;
-       uint64_t oerrors = 0;
-
-       im6o.im6o_ifidx = sc->sc_ifp0;
-       im6o.im6o_hlim = sc->sc_tunnel.t_ttl;
-       im6o.im6o_loop = 0;
-
-       NET_LOCK();
-       while ((m = ml_dequeue(ml)) != NULL) {
-               if (ip6_output(m, NULL, NULL, 0, &im6o, NULL) != 0)
-                       oerrors++;
-       }
-       NET_UNLOCK();
-
-       return (oerrors);
-}
-#endif /* INET6 */
-
-static void
-nvgre_send(void *arg)
-{
-       struct nvgre_softc *sc = arg;
-       struct ifnet *ifp = &sc->sc_ac.ac_if;
-       sa_family_t af = sc->sc_tunnel.t_af;
-       struct mbuf_list ml;
-       uint64_t oerrors;
-
-       if (!ISSET(ifp->if_flags, IFF_RUNNING))
-               return;
+               sa_family_t af = sc->sc_tunnel.t_af;
+               uint64_t oerrors;
 
-       mq_delist(&sc->sc_send_list, &ml);
-       if (ml_empty(&ml))
-               return;
+               switch (af) {
+               case AF_INET:
+                       oerrors = nvgre_send4(sc, &ml);
+                       break;
+       #ifdef INET6
+               case AF_INET6:
+                       oerrors = nvgre_send6(sc, &ml);
+                       break;
+       #endif
+               default:
+                       unhandled_af(af);
+                       /* NOTREACHED */
+               }
 
-       switch (af) {
-       case AF_INET:
-               oerrors = nvgre_send4(sc, &ml);
-               break;
-#ifdef INET6
-       case AF_INET6:
-               oerrors = nvgre_send6(sc, &ml);
-               break;
-#endif
-       default:
-               unhandled_af(af);
-               /* NOTREACHED */
+               ifq->ifq_errors += oerrors;
        }
 
-       ifp->if_oerrors += oerrors; /* XXX should be ifq_oerrors */
+       NET_UNLOCK_SHARED();
 }
 
 static int

Reply via email to