git: 7c11494165fa - main - routing: Add RT_NH_NOREF for routes that do not reference the nexthop
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 26 Nov 2026 02:14:26 UTC
The branch main has been updated by rcm:
URL: https://cgit.FreeBSD.org/src/commit/?id=7c11494165fa89798167c1fb2d2b0235ed1b82ec
commit 7c11494165fa89798167c1fb2d2b0235ed1b82ec
Author: R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-26 02:09:51 +0000
Commit: R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-26 02:09:51 +0000
routing: Add RT_NH_NOREF for routes that do not reference the nexthop
A route that is used within one network epoch section does not need a
reference on its nexthop: nexthops are destroyed through
NET_EPOCH_CALL(), so a nexthop looked up in the section stays valid
until the section ends. ip_output() and ip6_output() have no way to
know that a route is used like this. When they drop the nexthop of the
route they are given, because it is no longer valid or a pfil hook
asked for a new lookup, they release it, and they store a referenced
nexthop in its place.
Add a route flag, RT_NH_NOREF, which the owner of a route sets to say
that the nexthop of the route is not to be referenced. The flag is set
before the route is first used and is never cleared. RO_NHFREE() and
RO_INVALIDATE_CACHE() drop the nexthop of such a route without
releasing it. ip_output(), and cache_route(), which looks up the
nexthop for ip6_output(), look up a new one without NHR_REF.
The flag must not be set on a route that outlives the epoch section it
is used in, such as the route of a PCB. ip_output() and ip6_output()
assert that a route with the flag is on the stack of the caller.
Reviewed by: pouria, zlei
MFC after: 2 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D61957
---
sys/net/route.h | 20 ++++++++++++++------
sys/netinet/ip_output.c | 5 +++++
sys/netinet6/in6_src.c | 4 +++-
sys/netinet6/ip6_output.c | 4 ++++
4 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/sys/net/route.h b/sys/net/route.h
index c2052dc9cde8..bd87f1c92a3b 100644
--- a/sys/net/route.h
+++ b/sys/net/route.h
@@ -72,6 +72,7 @@ struct route {
#define RT_MAY_LOOP (1 << RT_MAY_LOOP_BIT) /* 0x0008 */
#define RT_HAS_HEADER (1 << RT_HAS_HEADER_BIT) /* 0x0010 */
+#define RT_NH_NOREF 0x0002 /* Do not reference ro_nh */
#define RT_REJECT 0x0020 /* Destination is reject */
#define RT_BLACKHOLE 0x0040 /* Destination is blackhole */
#define RT_HAS_GW 0x0080 /* Destination has GW */
@@ -370,9 +371,19 @@ struct rt_addrinfo {
#ifdef _KERNEL
+/*
+ * RT_NH_NOREF is set by the owner of a route, before the route is first
+ * used, and is never cleared. The nexthop of such a route is not
+ * referenced: it is looked up without NHR_REF and is dropped without being
+ * released. This is for a route on the stack that is only used in one
+ * network epoch section, never for a route that is cached, such as the
+ * route of a PCB. ip_output() and ip6_output() honor the flag when they
+ * store a nexthop.
+ */
#define RO_NHFREE(_ro) do { \
- if ((_ro)->ro_nh) { \
- NH_FREE((_ro)->ro_nh); \
+ if ((_ro)->ro_nh != NULL) { \
+ if (((_ro)->ro_flags & RT_NH_NOREF) == 0) \
+ NH_FREE((_ro)->ro_nh); \
(_ro)->ro_nh = NULL; \
} \
} while (0)
@@ -382,10 +393,7 @@ struct rt_addrinfo {
LLE_FREE((ro)->ro_lle); \
(ro)->ro_lle = NULL; \
} \
- if ((ro)->ro_nh != NULL) { \
- NH_FREE((ro)->ro_nh); \
- (ro)->ro_nh = NULL; \
- } \
+ RO_NHFREE(ro); \
} while (0)
#define RO_GET_FAMILY(ro, dst) ((ro) != NULL && \
diff --git a/sys/netinet/ip_output.c b/sys/netinet/ip_output.c
index 3a9fc76666a7..d4cc23b3a57f 100644
--- a/sys/netinet/ip_output.c
+++ b/sys/netinet/ip_output.c
@@ -90,6 +90,7 @@
#include <netipsec/ipsec_support.h>
#include <machine/in_cksum.h>
+#include <machine/stack.h>
#include <security/mac/mac_framework.h>
@@ -338,6 +339,9 @@ ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
M_ASSERTPKTHDR(m);
NET_EPOCH_ASSERT();
+ /* A route with RT_NH_NOREF must be on the stack, see route.h. */
+ MPASS(ro == NULL || (ro->ro_flags & RT_NH_NOREF) == 0 ||
+ kstack_contains(curthread, (vm_offset_t)ro, sizeof(*ro)));
if (inp != NULL) {
INP_LOCK_ASSERT(inp);
@@ -476,6 +480,7 @@ again:
uint32_t flowid;
flowid = m->m_pkthdr.flowid;
ro->ro_nh = fib4_lookup(fibnum, dst->sin_addr, 0,
+ (ro->ro_flags & RT_NH_NOREF) != 0 ? NHR_NONE :
NHR_REF, flowid);
if (ro->ro_nh == NULL || (!NH_IS_VALID(ro->ro_nh))) {
diff --git a/sys/netinet6/in6_src.c b/sys/netinet6/in6_src.c
index f9e798c905a8..322aa8bd875e 100644
--- a/sys/netinet6/in6_src.c
+++ b/sys/netinet6/in6_src.c
@@ -660,7 +660,9 @@ cache_route(uint32_t fibnum, const struct sockaddr_in6 *dst, struct route_in6 *r
paddr = &unscoped_addr;
} else
paddr = &dst->sin6_addr;
- ro->ro_nh = fib6_lookup(fibnum, paddr, scopeid, NHR_REF, flowid);
+ ro->ro_nh = fib6_lookup(fibnum, paddr, scopeid,
+ (ro->ro_flags & RT_NH_NOREF) != 0 ? NHR_NONE : NHR_REF,
+ flowid);
}
return (ro->ro_nh);
}
diff --git a/sys/netinet6/ip6_output.c b/sys/netinet6/ip6_output.c
index 82e82bca6c73..0c259e9df0eb 100644
--- a/sys/netinet6/ip6_output.c
+++ b/sys/netinet6/ip6_output.c
@@ -83,6 +83,7 @@
#include <sys/ucred.h>
#include <machine/in_cksum.h>
+#include <machine/stack.h>
#include <net/if.h>
#include <net/if_var.h>
@@ -429,6 +430,9 @@ ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
uint32_t optvalid;
NET_EPOCH_ASSERT();
+ /* A route with RT_NH_NOREF must be on the stack, see route.h. */
+ MPASS(ro == NULL || (ro->ro_flags & RT_NH_NOREF) == 0 ||
+ kstack_contains(curthread, (vm_offset_t)ro, sizeof(*ro)));
if (inp != NULL) {
INP_LOCK_ASSERT(inp);