>Synopsis:      rt_ifa_purge() only walks the routing table of the
                address family of the address being removed.  MPLS
                routes use an IPv4 or IPv6 address of the outgoing
                interface as rt_ifa, so they are never purged: after
                "ifconfig ... destroy" they stay in the table with no
                interface, can not be deleted, block their label and
                leak memory until reboot
>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
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest), 2 CPUs
        Source      : sys/net/route.c rev 1.451, sys/net/rtsock.c,
                      sys/netinet/in.c, sys/netinet6/in6.c as of
                      2026-11-03, git d5b06e73 (line numbers below are
                      from these)

>Description:

        An MPLS label switching route, e.g.

            route add -mpls -in 400 -swap -out 500 -inet 192.168.0.1

        is an AF_MPLS route whose gateway is an IPv4 (or IPv6) address.
        rtm_getifa() (rtsock.c:952) picks the interface address that
        reaches the gateway, so rtrequest() stores an AF_INET (or
        AF_INET6) ifaddr in the AF_MPLS route (route.c:1047-1048,
        rt_ifa = ifaref(ifa), rt_ifidx = its interface).

        When that address goes away, in_purgeaddr() (in.c:754) and
        in6_purgeaddr() (in6.c:935) call rt_ifa_purge() to remove all
        routes using it.  rt_ifa_purge() (route.c:1458) walks only one
        table per rtable:

            int error, af = ifa->ifa_addr->sa_family;      /* :1463 */
            ...
            error = rtable_walk(rtableid, af, &rt,         /* :1473 */
                rt_ifa_purge_walker, ifa);

        so the AF_MPLS table is never looked at and every MPLS route
        that uses the address is left behind.  if_detach() has no other
        route flush; it removes routes only through in_ifdetach() and
        in6_ifdetach(), i.e. through rt_ifa_purge().  rt_if_track()
        (route.c:1893) already walks every family from 1 to AF_MAX, so
        the routes only lose RTF_UP when the interface goes down.

        Consequences, all seen on GENERIC.MP as shipped:

        - After "ifconfig <if> destroy" the route stays in the table
          with no interface ("(null)" in route show).  It can not be
          deleted: RTM_DELETE (rtsock.c:981-987) and RTM_CHANGE
          (rtsock.c:1062-1068) return ESRCH when if_get(rt_ifidx)
          fails, so route(8) says "not in table", and "route flush" fails
          on it with "No such process".

        - The label can not be used again in that rtable until reboot:
          adding a route for the same label, after recreating the
          interface, fails with EEXIST ("File exists").

        - Memory leaks: each stale route keeps its rtentry and the
          removed in_ifaddr/in6_ifaddr (rt_ifa reference) forever.  100
          create/add/destroy cycles grew the rtentry pool by 200 items
          and M_IFADDR by 100 allocations (the second rtentry per cycle
          is presumably the gateway route the MPLS route has cached in
          rt_gwroute; not checked further).  From reading the code, the
          leaked ifaddr still points (ifa_ifp) to the freed ifnet.

        - Without a destroy, "ifconfig <if> inet <addr> delete" leaves
          the MPLS route UP on the interface with an rt_ifa that is no
          longer on it, and moving the interface to another rdomain
          leaves the route in the old rtable pointing to an interface
          that is no longer in that rdomain; once that interface is
          destroyed, the route is stuck as above.

        All of this needs root.  No crash was seen; the leaked ifaddr's
        dangling ifa_ifp would only matter if something dereferenced it
        through the stale route (not found, not tested).

>How-To-Repeat:

        As root, in a scratch routing domain 158 (vether158 must not
        exist yet):

        1.  ifconfig vether158 create rdomain 158 192.168.0.2/24 up
        2.  route -T158 add -mpls -in 400 -swap -out 500 -inet 192.168.0.1
        3.  ifconfig vether158 destroy
        4.  route -T158 -n show -mpls
        5.  route -T158 delete -mpls -in 400 -inet 192.168.0.1
        6.  ifconfig vether158 create rdomain 158 192.168.0.2/24 up
        7.  route -T158 add -mpls -in 400 -swap -out 500 -inet 192.168.0.1
        8.  ifconfig vether158 destroy
        9.  vmstat -m | grep -e "^rtentry" -e " ifaddr "
        10. for i in $(jot 100 1000); do
                ifconfig vether158 create rdomain 158 192.168.0.2/24 up
                route -T158 add -mpls -in $i -swap -out 500 \
                    -inet 192.168.0.1 >/dev/null
                ifconfig vether158 destroy
            done
        11. route -T158 -n show -mpls | grep -c "(null)"
        12. vmstat -m | grep -e "^rtentry" -e " ifaddr "

        Unpatched, step 4 still lists the route, without an interface:

            400       500       SWAP   192.168.0.1        GST   ... (null)

        step 5 fails with "delete net : gateway 192.168.0.1: not in
        table", step 7 fails with "add net : gateway 192.168.0.1: File
        exists", step 11 prints 101, and from step 9 to step 12 the
        InUse column of rtentry grows by 200 and that of ifaddr by 100.

        Patched, step 4 lists no route, step 5 fails with "not in
        table" because there is nothing left to delete, step 7
        succeeds, step 11 prints 0, and steps 9 and 12 print the same
        InUse values.

        Not covered here: an MPLS route with a label prefix (only possible
        through the routing socket with a netmask) is not removed by this
        fix either, because the kernel cannot delete such a route at all
        (rt_plentosa() has no AF_MPLS case, so rtdeletemsg() gets ESRCH;
        "route flush" fails the same way on a stock kernel).  That is a
        separate bug with its own fix, reported separately as "route: an
        MPLS route with a label prefix cannot be deleted by the kernel; it
        outlives its interface and panics "ifa_ifp != NULL"".  With such a
        route present, rt_ifa_purge() stops at that ESRCH, as it already
        does today for any delete error.

>Fix:

        Let rt_ifa_purge() walk the tables of all address families, as
        rt_if_track() does, so routes of another family that use the
        address (MPLS routes) are removed with it.  rtable_walk()
        returns EAFNOSUPPORT for families without a table, which the
        loop already ignores.

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     6 Nov 2026 06:00:00 -0000
@@ -1452,7 +1452,9 @@ rt_ifa_dellocal(struct ifaddr *ifa)
 }
 
 /*
- * Remove all addresses attached to ``ifa''.
+ * Remove all routes attached to ``ifa''.  Not only routes of the address
+ * family of ``ifa'' use it: an MPLS route with an IPv4 or IPv6 gateway
+ * holds the address of the outgoing interface as well.
  */
 void
 rt_ifa_purge(struct ifaddr *ifa)
@@ -1460,7 +1462,7 @@ rt_ifa_purge(struct ifaddr *ifa)
        struct ifnet            *ifp = ifa->ifa_ifp;
        struct rtentry          *rt = NULL;
        unsigned int             rtableid;
-       int                      error, af = ifa->ifa_addr->sa_family;
+       int                      error, af;
 
        KASSERT(ifp != NULL);
 
@@ -1469,20 +1471,25 @@ rt_ifa_purge(struct ifaddr *ifa)
                if (rtable_l2(rtableid) != ifp->if_rdomain)
                        continue;
 
-               do {
-                       error = rtable_walk(rtableid, af, &rt,
-                           rt_ifa_purge_walker, ifa);
-                       if (rt != NULL && error == EEXIST) {
-                               error = rtdeletemsg(rt, ifp, rtableid);
-                               if (error == 0)
-                                       error = EAGAIN;
-                       }
-                       rtfree(rt);
-                       rt = NULL;
-               } while (error == EAGAIN);
-
-               if (error == EAFNOSUPPORT)
-                       error = 0;
+               for (af = 1; af <= AF_MAX; af++) {
+                       do {
+                               error = rtable_walk(rtableid, af, &rt,
+                                   rt_ifa_purge_walker, ifa);
+                               if (rt != NULL && error == EEXIST) {
+                                       error = rtdeletemsg(rt, ifp, rtableid);
+                                       if (error == 0)
+                                               error = EAGAIN;
+                               }
+                               rtfree(rt);
+                               rt = NULL;
+                       } while (error == EAGAIN);
+
+                       if (error == EAFNOSUPPORT)
+                               error = 0;
+
+                       if (error)
+                               break;
+               }
 
                if (error)
                        break;

>Verification:

        amd64 KVM guest, 2 CPUs.  Pristine -current src (2026-11-03, git
        d5b06e73, route.c 1.451).  Kernels: GENERIC.MP as shipped and
        GENERIC.MP + WITNESS, without the diff and with exactly the diff
        above applied with patch -p0 to a fresh export of the same tree
        (it applies without fuzz or offset; the patched tree differs from
        the pristine one only in route.c, checked file by file; the
        builds have no warnings).

        How-To-Repeat as shown above, after a fresh boot each (the route
        command of step 10 on one line; the text exactly as printed
        above was run once more on both GENERIC.MP kernels, same
        results):

          unpatched GENERIC.MP           3 of 3 runs: route left as
                                         "(null)", delete "not in
                                         table", re-add "File exists",
                                         101 stale routes, rtentry InUse
                                         28 -> 228, ifaddr 44 -> 144
          unpatched GENERIC.MP + WITNESS 3 of 3 runs: the same
          patched GENERIC.MP             3 of 3 runs: no route left,
                                         re-add succeeds, 0 stale
                                         routes, rtentry InUse 26 -> 26,
                                         ifaddr 43 -> 43
          patched GENERIC.MP + WITNESS   3 of 3 runs: the same, no
                                         WITNESS or other kernel
                                         messages

        Also checked on both kernels: "route flush" of the rtable, an
        address delete and an rdomain move with an MPLS route on the
        interface.  Unpatched: flush fails with "No such process" and
        the route stays; after the address delete the route stays UP
        on the interface; after the move it stays in the old rtable.
        Patched: none of these leave an MPLS route behind.

        Normal use on the patched kernels (GENERIC.MP and + WITNESS, 3
        fresh boots each, 36 of 36 checks every time): MPLS swap, push
        and pop routes with IPv4 and IPv6 gateways added, shown, looked
        up and deleted; an IPv4 route with -mplslabel; deleting an
        unrelated alias keeps the MPLS routes, deleting the IPv4
        address removes only the routes using it, deleting the IPv6
        address removes the other; destroying an interface with IPv4,
        IPv6 and MPLS routes on it removes all of them; mpe(4) with
        mplslabel and relabel, local label route removed on destroy;
        rdomain move and back; labelled traffic between two pair(4)
        interfaces in two rdomains (push in one, swap in the other,
        label 301 seen coming back); rtentry pool and M_IFADDR InUse
        equal before and after.  No panic, fault or WITNESS message.
        The unpatched kernel fails 9 of these checks, all of them
        MPLS routes left behind and the resulting leak.

        Current -current: commit 37be935f0 (the rtables rework) changed
        the context of the third hunk in rt_ifa_purge() after this
        report's base, so the diff above no longer applies to the tip.
        The same change rebased onto origin/master afb08c4d5 (the added
        and removed lines are identical; only the surrounding context
        moved) is appended below.  With it, the How-To-Repeat behaves on
        master exactly as above (GENERIC.MP, 0 warnings, fresh reboot per
        run, 4-vcpu guest; step 10 on one line):

          unpatched master  3/3: re-add "File exists", 101 stale routes,
                            rtentry InUse 28 -> 228, ifaddr 44 -> 144
          patched master    3/3: re-add succeeds, 0 stale routes,
                            rtentry InUse 26 -> 26, ifaddr 43 -> 43
          normal use, patched master: 36/36 checks, 3 fresh boots

----------------------------------------------------------------------
Rebased for -current after commit 37be935f0 (origin/master afb08c4d5,
sys/net/route.c).  Apply this instead of the diff above on a tree that
includes the rtables rework.

Index: sys/net/route.c
===================================================================
--- sys/net/route.c
+++ sys/net/route.c
@@ -1450,7 +1450,9 @@ rt_ifa_dellocal(struct ifaddr *ifa)
 }
 
 /*
- * Remove all addresses attached to ``ifa''.
+ * Remove all routes attached to ``ifa''.  Not only routes of the address
+ * family of ``ifa'' use it: an MPLS route with an IPv4 or IPv6 gateway
+ * holds the address of the outgoing interface as well.
  */
 void
 rt_ifa_purge(struct ifaddr *ifa)
@@ -1458,7 +1460,7 @@ rt_ifa_purge(struct ifaddr *ifa)
        struct ifnet            *ifp = ifa->ifa_ifp;
        struct rtentry          *rt = NULL;
        unsigned int             rtableid;
-       int                      error, af = ifa->ifa_addr->sa_family;
+       int                      error, af;
 
        KASSERT(ifp != NULL);
 
@@ -1468,20 +1470,25 @@ rt_ifa_purge(struct ifaddr *ifa)
                    rtable_l2(rtableid) != ifp->if_rdomain)
                        continue;
 
-               do {
-                       error = rtable_walk(rtableid, af, &rt,
-                           rt_ifa_purge_walker, ifa);
-                       if (rt != NULL && error == EEXIST) {
-                               error = rtdeletemsg(rt, ifp, rtableid);
-                               if (error == 0)
-                                       error = EAGAIN;
-                       }
-                       rtfree(rt);
-                       rt = NULL;
-               } while (error == EAGAIN);
-
-               if (error == EAFNOSUPPORT)
-                       error = 0;
+               for (af = 1; af <= AF_MAX; af++) {
+                       do {
+                               error = rtable_walk(rtableid, af, &rt,
+                                   rt_ifa_purge_walker, ifa);
+                               if (rt != NULL && error == EEXIST) {
+                                       error = rtdeletemsg(rt, ifp, rtableid);
+                                       if (error == 0)
+                                               error = EAGAIN;
+                               }
+                               rtfree(rt);
+                               rt = NULL;
+                       } while (error == EAGAIN);
+
+                       if (error == EAFNOSUPPORT)
+                               error = 0;
+
+                       if (error)
+                               break;
+               }
 
                if (error)
                        break;

Reply via email to