git: c9f9c4fe47bf - main - ip6_forward: Do not reference the nexthop

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Sun, 22 Nov 2026 21:18:49 UTC
The branch main has been updated by rcm:

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

commit c9f9c4fe47bf17bb39d7ff3a200aadab6b69e60e
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-22 21:18:16 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-22 21:18:16 +0000

    ip6_forward: Do not reference the nexthop
    
    ip6_forward() looked the route up with NHR_REF and released the nexthop
    before returning.  It runs in the network epoch and uses the nexthop
    only until it returns: nd6_output_ifp() gets the interface, and
    icmp6_redirect_output() runs before the return.  The epoch keeps the
    nexthop valid that long, which ip6_tryforward() already relies on.
    
    Every packet that takes this path wrote twice to the nexthop reference
    count, which all CPUs forwarding to that nexthop share.  Forwarded
    packets take it whenever IPsec has a security policy, and today also
    whenever net.inet6.ip6.redirect is set, which is the default.
    
    Look the nexthop up without a reference, and assert that the caller is
    in the network epoch.
    
    No functional change intended.
    
    Reviewed by:            pouria
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61393
---
 sys/netinet6/ip6_forward.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/sys/netinet6/ip6_forward.c b/sys/netinet6/ip6_forward.c
index 4b263e65a637..81fa76e07e05 100644
--- a/sys/netinet6/ip6_forward.c
+++ b/sys/netinet6/ip6_forward.c
@@ -95,7 +95,7 @@ ip6_forward(struct mbuf *m, int srcrt)
 {
 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 	struct sockaddr_in6 dst;
-	struct nhop_object *nh = NULL;
+	struct nhop_object *nh;
 	int error, type = 0, code = 0;
 	struct mbuf *mcopy = NULL;
 	struct ifnet *origifp;	/* maybe unnecessary */
@@ -104,6 +104,8 @@ ip6_forward(struct mbuf *m, int srcrt)
 	struct m_tag *fwd_tag;
 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
 
+	NET_EPOCH_ASSERT();
+
 	/*
 	 * Do not forward packets to multicast destination (should be handled
 	 * by ip6_mforward().
@@ -186,8 +188,9 @@ ip6_forward(struct mbuf *m, int srcrt)
 	dst.sin6_addr = ip6->ip6_dst;
 	dst.sin6_scope_id = in6_get_unicast_scopeid(&ip6->ip6_dst, m->m_pkthdr.rcvif);
 again:
+	/* The network epoch keeps the nexthop valid until we return. */
 	nh = fib6_lookup(M_GETFIB(m), &dst.sin6_addr, dst.sin6_scope_id,
-	    NHR_REF, m->m_pkthdr.flowid);
+	    NHR_NONE, m->m_pkthdr.flowid);
 	if (nh == NULL) {
 		IP6STAT_INC(ip6s_noroute);
 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
@@ -339,8 +342,6 @@ again:
 		if (in6_localip(&ip6->ip6_dst))
 			m->m_flags |= M_FASTFWD_OURS;
 		else {
-			NH_FREE(nh);
-
 			/* Update address and scopeid. Assume scope is embedded */
 			dst.sin6_scope_id = ntohs(in6_getscope(&ip6->ip6_dst));
 			dst.sin6_addr = ip6->ip6_dst;
@@ -378,7 +379,6 @@ again:
 		m->m_flags |= M_SKIP_FIREWALL;
 		m->m_flags &= ~M_IP6_NEXTHOP;
 		m_tag_delete(m, fwd_tag);
-		NH_FREE(nh);
 		goto again;
 	}
 
@@ -440,12 +440,12 @@ pass:
 	}
 
 	if (mcopy == NULL)
-		goto out;
+		return;
 	switch (error) {
 	case 0:
 		if (type == ND_REDIRECT) {
 			icmp6_redirect_output(mcopy, nh);
-			goto out;
+			return;
 		}
 		goto freecopy;
 
@@ -467,14 +467,11 @@ pass:
 		break;
 	}
 	icmp6_error(mcopy, type, code, 0);
-	goto out;
+	return;
 
  freecopy:
 	m_freem(mcopy);
-	goto out;
+	return;
 bad:
 	m_freem(m);
-out:
-	if (nh != NULL)
-		NH_FREE(nh);
 }