>Synopsis:      rt_plen2mask() returns no netmask for AF_MPLS, so
                rtdeletemsg() looks an MPLS route up as a full 32-bit
                label.  An MPLS route with a shorter prefix (the routing
                socket accepts a netmask for it) can then never be removed
                by rt_ifa_purge() or "route flush": it stays
                in the table after its interface is destroyed, still
                pointing at the freed-up address (ifa_ifp == NULL), and
                the next route added through it panics in rtrequest().
>Category:      kernel
>Severity:      non-critical
>Class:         sw-bug
>Release:       OpenBSD 8.0-current (src as of 2026-11-03)
>Environment:
        System      : OpenBSD 8.0-current, GENERIC.MP as shipped (DIAGNOSTIC,
                      POOL_DEBUG)
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest), 4 CPUs
        Source      : sys/net/route.c rev 1.451, sys/net/rtsock.c rev 1.391
                      as of 2026-11-03, git d5b06e73 (line numbers below
                      are from these)

>Description:

        Prerequisite: on an unpatched kernel rt_ifa_purge() only walks
        the routing table of the address family of the address being
        removed (route.c:1463), so EVERY MPLS route with an IPv4 or
        IPv6 gateway survives the destruction of its interface.  That is
        a separate bug with its own fix, reported to bugs@ as "MPLS routes
        survive removal of their interface address or interface; after a
        destroy they can not be deleted" (rt_ifa_purge() walks all address
        families).  Everything below was seen on a
        kernel with that fix applied: it removes MPLS routes for a whole
        label, but not MPLS routes with a label prefix, and this report is
        about the latter.

        The bug itself does not depend on that fix.  On a stock kernel
        (master afb08c4d5, no fixes), with a label-prefix route added as
        in step 4 below and nothing destroyed, "route -T150 flush" fails
        with "route: write to routing socket: No such process", the route
        stays, and the flush stops there: a whole-label route added
        afterwards (route -T150 add -mpls -in 400 -swap -out 500 -inet
        192.0.2.2) survives the flush as well.  Deleting the prefix route
        from userland with the same netmask works; only the kernel's own
        delete paths fail.

        An MPLS route is normally a route for one label: route(8) sends
        "-mpls -in N" without a netmask and the kernel inserts it with the
        full prefix length of the MPLS table (dom_maxplen 32).  The routing
        socket also accepts a netmask for an AF_MPLS destination, though:
        rtable_satoplen() (rtable.c:924) handles any family with a
        dom_rtoffset, so

            route -T150 add -sa 210000001000000000000000000000 \
                -netmask 210000fff0 -inet 192.0.2.2

        (label 256, first 12 bits of the label word, i.e. labels
        256-511) inserts a /12 route into the MPLS table.  It is shown as
        label 256, and it can be deleted from userland if the same netmask
        is given.

        The kernel itself cannot delete it.  rtdeletemsg() (route.c:785),
        used by rt_ifa_purge(), and the sysctl dump
        that "route flush" works from (rtsock.c:1977) build the netmask
        with rt_plen2mask() -> rt_plentosa() (route.c:1972), which only
        knows AF_INET and AF_INET6 and returns NULL for AF_MPLS.  Without
        a netmask rtrequest_delete() calls rtable_delete() (route.c:920),
        which then looks for a /32 node, does not find the /12 one and
        returns ESRCH.  So:

        - "route flush" fails with "No such process" for such a route
          and leaves it in place.

        - When the address or interface goes away, rt_ifa_purge() finds
          the route (rt_ifa == ifa), rtdeletemsg() fails with ESRCH, and
          rt_ifa_purge() gives up (route.c:1487 "if (error) break").
          in_purgeaddr() then sets ia_ifp = NULL (in.c:762) and drops its
          reference, but the route keeps the address:

            MPLS:
            In label  Out label Op     Gateway      Flags ... Interface
            256       -         LOCAL  192.0.2.2    GS    ... (null)

        - Any later route lookup that ends on this route uses the stale
          address.  ifa_ifwithroute() resolves a gateway with
          rtalloc() (rtsock.c:1242) and takes rt->rt_ifa, so an RTM_ADD
          whose gateway is a label inside the prefix gets the old IPv4
          address with ifa_ifp == NULL and rtrequest() panics:

            panic: kernel diagnostic assertion "info->rti_ifa->ifa_ifp !=
                NULL" failed: file "sys/net/route.c", line 993
            rtrequest() at rtrequest+0x...
            rtm_output() at rtm_output+0x...
            route_output() at route_output+0x4ea
            route_send() at route_send+0x64
            sosend() at sosend+0x432
            dofilewritev() at dofilewritev+0x18f
            sys_write() at sys_write+0x55
            syscall() at syscall+0x60d
            Xsyscall() at Xsyscall+0x128

          When the destination family differs from that of the stale
          address, ifa_ifwithroute() instead calls
          ifaof_ifpforaddr(dst, ifa->ifa_ifp) with ifa_ifp == NULL and the
          kernel page-faults in ifaof_ifpforaddr() (seen once from the
          fuzzer, trace ifaof_ifpforaddr <- ifa_ifwithroute <- rtm_getifa
          <- rtm_output).

        It needs root (adding the route).  We found it with a routing
        socket fuzzer that sends random RTM_ADD messages, which hit
        both panics on a kernel that already had the "MPLS route left"
        fix and the fix for routes via an interface of another rdomain.

>How-To-Repeat:

        As root, in a scratch routing domain 150 (vether150 must not exist
        yet), on a kernel with the "MPLS route left in the table after
        its interface is destroyed" fix:

        1.  ifconfig vether150 create
        2.  ifconfig vether150 rdomain 150
        3.  ifconfig vether150 inet 192.0.2.1/24 up
        4.  route -T150 add -sa 210000001000000000000000000000 \
                -netmask 210000fff0 -inet 192.0.2.2
        5.  ifconfig vether150 destroy
        6.  route -T150 -n show -mpls
        7.  route -T150 flush
        8.  route -T150 -n show -mpls
        9.  route -T150 add -inet 203.0.113.0/24 \
                -sa 210000001010000000000000000000

        ("-sa" passes a raw sockaddr in hex, starting at sa_family:
        21 = AF_MPLS, two pad bytes, then the label word; 00100000 is
        label 256 and 00101000 label 257, "ff f0" in the netmask selects
        the first 12 bits of the label word, i.e. labels 256-511.)

        Step 4 adds an MPLS route for labels 256-511 via 192.0.2.2.
        Unpatched, step 6 still lists it on interface "(null)", step 7
        fails with "route: write to routing socket: No such process",
        step 8 still lists it, and step 9 (an IPv4 route whose gateway is
        label 257) panics with the assertion above.  Patched, steps 6 and
        8 list no MPLS route, step 7 has nothing to flush, and step 9
        fails with "Network is unreachable".

        (On a kernel without the prerequisite fix the same steps panic
        the same way, because there rt_ifa_purge() does not look at the
        MPLS table at all.)

>Fix:

        Let rt_plentosa() build a netmask for MPLS routes shorter than a
        whole label, so that rtdeletemsg() and the sysctl dump describe
        them correctly.  A route for a whole label keeps getting no
        netmask, as before, so nothing changes for the MPLS routes route(8)
        and the daemons install.

Index: sys/net/route.c
===================================================================
RCS file: /cvs/src/sys/net/route.c,v
diff -u -p -r1.451 route.c
--- sys/net/route.c     22 Apr 2026 15:17:43 -0000      1.451
+++ sys/net/route.c     8 Nov 2026 06:00:00 -0000
@@ -1975,6 +1975,9 @@ rt_plentosa(sa_family_t af, int plen, st
 #ifdef INET6
        struct sockaddr_in6     *sin6 = (struct sockaddr_in6 *)sa_mask;
 #endif
+#ifdef MPLS
+       struct sockaddr_mpls    *smpls = (struct sockaddr_mpls *)sa_mask;
+#endif
 
        KASSERT(plen >= 0 || plen == -1);
 
@@ -1996,6 +1999,20 @@ rt_plentosa(sa_family_t af, int plen, st
                in6_prefixlen2mask(&sin6->sin6_addr, plen);
                break;
 #endif /* INET6 */
+#ifdef MPLS
+       case AF_MPLS:
+               /*
+                * A route for a whole label has no netmask.  Shorter ones
+                * need it, without it rtdeletemsg() cannot remove them.
+                */
+               if (plen >= 32)
+                       return (NULL);
+               smpls->smpls_family = AF_MPLS;
+               smpls->smpls_len = sizeof(struct sockaddr_mpls);
+               if (plen > 0)
+                       smpls->smpls_label = htonl(0xffffffffU << (32 - plen));
+               break;
+#endif /* MPLS */
        default:
                return (NULL);
        }

>Verification:

        amd64 KVM guest, 4 CPUs, GENERIC.MP as shipped.  Source: a fresh
        export of openbsd/src d5b06e73 (2026-11-03, route.c 1.451).
        Unpatched = that tree + the prerequisite fix (the diff of "MPLS
        routes survive removal of their interface address or interface;
        after a destroy they can not be deleted",
        applied with patch -p0, no fuzz or offset).  Patched = the same
        tree + the diff above, applied with patch -p0 on top of it (no
        fuzz; offset 7 lines because the prerequisite adds lines earlier
        in route.c); only route.c changed between the two builds, no
        warnings.  The diff also applies to stock d5b06e73 and to the
        current tree (git afb08c4d5) without fuzz or offset.

        How-To-Repeat steps 1-9 exactly as above (step 4 and 9 each on
        one line), a fresh boot before every run:

          unpatched   3 of 3 runs panic in step 9: "info->rti_ifa->ifa_ifp
                      != NULL" failed, route.c line 993, trace rtrequest
                      <- rtm_output <- route_output <- route_send <- sosend
                      <- dofilewritev <- sys_write
                      (a separate run with steps 1-8 only: step 6 and 8
                      list "256 - LOCAL 192.0.2.2 GS ... (null)", step 7
                      "route: write to routing socket: No such process")
          patched     3 of 3 runs: steps 6 and 8 list no MPLS route, step
                      7 has nothing to flush, step 9 "add net
                      203.0.113.0/24: gateway ...: Network is unreachable",
                      no panic or other kernel message
          stock       1 run on d5b06e73 without the prerequisite: panics in
                      step 9 with the same assertion

        Normal use on the patched kernel, 3 fresh boots, all checks
        passing each time:
          - MPLS routes for whole labels as route(8) installs them: swap,
            push and pop with IPv4 and IPv6 gateways added, shown, looked
            up and deleted, an IPv4 route with -mplslabel, address delete
            and interface destroy removing exactly their routes, mpe(4)
            with mplslabel/relabel, rdomain move and back, labelled
            traffic between two pair(4) interfaces, rtentry and ifaddr
            counts unchanged (36 of 36 checks; the unpatched kernel also
            passes 36 of 36, and its "route show -mpls" lines for these
            routes are identical to the patched kernel's);
          - label-prefix routes: added, shown, deleted from userland with
            the netmask, removed by "route flush", by deleting the
            address and by destroying the interface, next to a whole-label
            route, an IPv4 route via a label inside the prefix while the
            interface exists, and no "(null)" route and no panic after
            the destroy (14 of 14 checks);
          - the route(8) regression script of our CI (flags, metrics,
            priorities, multipath, labels, -sa, MPLS error cases,
            sourceaddr, exec, monitor, flush): 95 pass, 0 fail, 1 known
            xfail ("route flush" of an empty table exits 1).

        Unpatched, the claim that such a route can be deleted from
        userland when the netmask is given was checked once: "route
        -T151 delete -sa 2100...  -netmask 210000fff0 -inet 192.0.2.2"
        removes it.  Only the kernel's own deletions fail.

        How it was found: a routing socket fuzzer (random RTM_ADD/
        RTM_CHANGE/RTM_DELETE messages with random sockaddrs, plus
        route(8) and interface create/destroy/rdomain moves in rtables
        100-103) kept hitting "info->rti_ifa->ifa_ifp != NULL" on a
        kernel that already had the prerequisite fix and the fix for
        routes via an interface of another rdomain.  With a debug printf
        in rt_ifa_purge() the next hit showed rtdeletemsg() failing with
        ESRCH for an AF_MPLS route of prefix length 0, which stayed in
        the table with the purged address, followed by the
        ifaof_ifpforaddr() page fault described above.

Reply via email to