git: f18c1ca514e2 - stable/14 - route/fib_algo: Fix nexthop reference leak on FLM_BATCH

From: Pouria Mousavizadeh Tehrani <pouria_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 09:07:34 UTC
The branch stable/14 has been updated by pouria:

URL: https://cgit.FreeBSD.org/src/commit/?id=f18c1ca514e21e4e1e2769a6ddd2c827628b6b03

commit f18c1ca514e21e4e1e2769a6ddd2c827628b6b03
Author:     Pouria Mousavizadeh Tehrani <pouria@FreeBSD.org>
AuthorDate: 2026-11-18 19:00:11 +0000
Commit:     Pouria Mousavizadeh Tehrani <pouria@FreeBSD.org>
CommitDate: 2026-11-27 06:36:40 +0000

    route/fib_algo: Fix nexthop reference leak on FLM_BATCH
    
    handle_rtable_change_cb() references the new nexthop before calling
    the algorithm.  If the algorithm asks to batch the change by
    returning FLM_BATCH, queue_rtable_change() references the same
    nexthop again for the queued entry, while apply_rtable_changes()
    only unreferences the old nexthop of each entry.
    
    Reference new nhop once in handle_rtable_change_cb(), before
    deciding whether the change is queued or passed to the algorithm,
    and do not reference it again in fill_change_entry().  This also
    leaves the queue reallocation as the only way for
    queue_rtable_change() to fail.
    
    Currently, dxr is the only algorithm returning FLM_BATCH.
    
    Fixes:          6b8ef0d428c9 ("Add batched update support for the fib algo.")
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D61577
    
    (cherry picked from commit c15a606d2f4443e3f3ba775532f0f499efaff3b7)
---
 sys/net/route/fib_algo.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/sys/net/route/fib_algo.c b/sys/net/route/fib_algo.c
index 25370f18934f..aa1bee8ee44b 100644
--- a/sys/net/route/fib_algo.c
+++ b/sys/net/route/fib_algo.c
@@ -707,7 +707,11 @@ apply_rtable_changes(struct fib_data *fd)
 	return (result == FLM_SUCCESS);
 }
 
-static bool
+/*
+ * Fills in queue entry @ce with the change described by @rc.
+ * The caller holds the reference to the new nexthop.
+ */
+static void
 fill_change_entry(struct fib_data *fd, struct fib_change_entry *ce, struct rib_cmd_info *rc)
 {
 	int plen = 0;
@@ -728,12 +732,6 @@ fill_change_entry(struct fib_data *fd, struct fib_change_entry *ce, struct rib_c
 	ce->plen = plen;
 	ce->nh_old = rc->rc_nh_old;
 	ce->nh_new = rc->rc_nh_new;
-	if (ce->nh_new != NULL) {
-		if (fib_ref_nhop(fd, ce->nh_new) == 0)
-			return (false);
-	}
-
-	return (true);
 }
 
 static bool
@@ -760,7 +758,9 @@ queue_rtable_change(struct fib_data *fd, struct rib_cmd_info *rc)
 		q->size = q_size;
 	}
 
-	return (fill_change_entry(fd, &q->entries[q->count++], rc));
+	fill_change_entry(fd, &q->entries[q->count++], rc);
+
+	return (true);
 }
 
 /*
@@ -800,6 +800,19 @@ handle_rtable_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc,
 		return;
 	}
 
+	/*
+	 * Maintain guarantee that every nexthop returned by the dataplane
+	 *  lookup has > 0 refcount, so can be safely referenced within current
+	 *  epoch.
+	 */
+	if (rc->rc_nh_new != NULL) {
+		if (fib_ref_nhop(fd, rc->rc_nh_new) == 0) {
+			/* ran out of indexes */
+			schedule_fd_rebuild(fd, "ran out of nhop indexes");
+			return;
+		}
+	}
+
 	/*
 	 * Algo requested updates to be delivered in batches.
 	 * Add the current change to the queue and return.
@@ -815,19 +828,6 @@ handle_rtable_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc,
 		return;
 	}
 
-	/*
-	 * Maintain guarantee that every nexthop returned by the dataplane
-	 *  lookup has > 0 refcount, so can be safely referenced within current
-	 *  epoch.
-	 */
-	if (rc->rc_nh_new != NULL) {
-		if (fib_ref_nhop(fd, rc->rc_nh_new) == 0) {
-			/* ran out of indexes */
-			schedule_fd_rebuild(fd, "ran out of nhop indexes");
-			return;
-		}
-	}
-
 	result = fd->fd_flm->flm_change_rib_item_cb(rnh, rc, fd->fd_algo_data);
 
 	switch (result) {