git: 6c41d928bcd7 - main - routing: Fix table sizes

Dag-Erling Smørgrav <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a7e676a.26c82.53f80b95__45269.9992036045$1786668932$gmane$org@gitrepo.freebsd.org>
The branch main has been updated by des:

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

commit 6c41d928bcd763ec60d55bec2886c05b03cf9e6a
Author:     Dag-Erling Smørgrav <[email protected]>
AuthorDate: 2026-08-13 21:45:53 +0000
Commit:     Dag-Erling Smørgrav <[email protected]>
CommitDate: 2026-08-14 00:54:27 +0000

    routing: Fix table sizes
    
    Tables that have one element per protocol or address family were
    previously sized by AF_MAX + 1 since AF_MAX was off by one.  Now that
    AF_MAX has been corrected, we need to apply the opposite correction to
    these tables.
    
    Fixes:          ddd850aa7720 ("sys/socket.h: Fix AF_MAX")
    MFC after:      3 days
    Sponsored by:   Klara, Inc.
    Sponsored by:   NetApp, Inc.
    Reviewed by:    pouria, kevans, glebius
    Differential Revision:  https://reviews.freebsd.org/D58826
---
 sys/net/route/route_tables.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/sys/net/route/route_tables.c b/sys/net/route/route_tables.c
index 59c7b6c61a93..8b123b0fbd3a 100644
--- a/sys/net/route/route_tables.c
+++ b/sys/net/route/route_tables.c
@@ -204,7 +204,7 @@ static void
 populate_kernel_routes(struct rib_head **new_rt_tables, struct rib_head *rh)
 {
 	for (int i = 0; i < V_rt_numfibs; i++) {
-		struct rib_head *rh_src = new_rt_tables[i * (AF_MAX + 1) + rh->rib_family];
+		struct rib_head *rh_src = new_rt_tables[i * AF_MAX + rh->rib_family];
 		if ((rh_src != NULL) && (rh_src != rh))
 			rib_copy_kernel_routes(rh_src, rh);
 	}
@@ -228,7 +228,7 @@ grow_rtables(uint32_t num_tables)
 	KASSERT(num_tables >= V_rt_numfibs, ("num_tables(%u) < rt_numfibs(%u)\n",
 				num_tables, V_rt_numfibs));
 
-	new_rt_tables = mallocarray(num_tables * (AF_MAX + 1), sizeof(void *),
+	new_rt_tables = mallocarray(num_tables * AF_MAX, sizeof(void *),
 	    M_RTABLE, M_WAITOK | M_ZERO);
 
 #ifdef FIB_ALGO
@@ -237,12 +237,12 @@ grow_rtables(uint32_t num_tables)
 
 	/*
 	 * Current rt_tables layout:
-	 * fib0[af0, af1, af2, .., AF_MAX]fib1[af0, af1, af2, .., Af_MAX]..
+	 * fib0[af0, af1, af2, .., AF_MAX-1]fib1[af0, af1, af2, .., AF_MAX-1]..
 	 * this allows to copy existing tables data by using memcpy()
 	 */
 	if (V_rt_tables != NULL)
 		memcpy(new_rt_tables, V_rt_tables,
-		    V_rt_numfibs * (AF_MAX + 1) * sizeof(void *));
+		    V_rt_numfibs * AF_MAX * sizeof(void *));
 
 	/* Populate the remainders */
 	SLIST_FOREACH(dom, &domains, dom_next) {
@@ -250,7 +250,7 @@ grow_rtables(uint32_t num_tables)
 			continue;
 		family = dom->dom_family;
 		for (int i = 0; i < num_tables; i++) {
-			prnh = &new_rt_tables[i * (AF_MAX + 1) + family];
+			prnh = &new_rt_tables[i * AF_MAX + family];
 			if (*prnh != NULL)
 				continue;
 			rh = dom->dom_rtattach(i);
@@ -370,13 +370,13 @@ rt_tables_get_rnh_ptr(uint32_t table, sa_family_t family)
 	KASSERT(table < V_rt_numfibs,
 	    ("%s: table out of bounds (%d < %d)", __func__, table,
 	     V_rt_numfibs));
-	KASSERT(family < (AF_MAX + 1),
-	    ("%s: fam out of bounds (%d < %d)", __func__, family, AF_MAX + 1));
+	KASSERT(family < AF_MAX,
+	    ("%s: fam out of bounds (%d < %d)", __func__, family, AF_MAX));
 
 	/* rnh is [fib=0][af=0]. */
 	prnh = V_rt_tables;
 	/* Get the offset to the requested table and fam. */
-	prnh += table * (AF_MAX + 1) + family;
+	prnh += table * AF_MAX + family;
 
 	return (*prnh);
 }
@@ -393,7 +393,7 @@ rt_tables_get_rnh_safe(uint32_t table, sa_family_t family)
 {
 	if (__predict_false(table >= V_rt_numfibs))
 		return (NULL);
-	if (__predict_false(family >= (AF_MAX + 1)))
+	if (__predict_false(family >= AF_MAX))
 		return (NULL);
 	return (rt_tables_get_rnh_ptr(table, family));
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.