git: 3d88ee6af296 - main - ip6_forward: Free the ICMPv6 copy on all error paths

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Fri, 13 Nov 2026 17:40:03 UTC
The branch main has been updated by rcm:

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

commit 3d88ee6af2961deefe7412b2e393aa0c05d4102c
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-13 17:37:42 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-13 17:37:42 +0000

    ip6_forward: Free the ICMPv6 copy on all error paths
    
    ip6_forward() keeps a copy of the start of the packet in case it has
    to send an ICMPv6 error.  Every exit must either pass the copy to
    icmp6_error() or icmp6_redirect_output(), or free it.  Three exits did
    neither and leaked the copy:
    
     - the destination scope check, when a packet would leave the zone of
       its destination address;
     - a packet that a pfil hook redirected to a local address, which is
       queued for local input;
     - a packet whose checksum has to be computed here and whose last
       header offset is not valid.
    
    Free the copy in these three cases.
    
    Reviewed by:            tuexen, pouria
    Fixes:                  8a006adb2441 ("Add support for IPv6 to ipfw fwd...")
    Fixes:                  bcb298fa9e23 ("sctp, tcp, udp: improve deferred computation...")
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61509
---
 sys/netinet6/ip6_forward.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/sys/netinet6/ip6_forward.c b/sys/netinet6/ip6_forward.c
index 0ebb51fd80f0..4b263e65a637 100644
--- a/sys/netinet6/ip6_forward.c
+++ b/sys/netinet6/ip6_forward.c
@@ -253,6 +253,7 @@ again:
 	if (inzone != outzone) {
 		IP6STAT_INC(ip6s_cantforward);
 		IP6STAT_INC(ip6s_badscope);
+		m_freem(mcopy);
 		goto bad;
 	}
 
@@ -362,7 +363,7 @@ again:
 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
 #endif
 		error = netisr_queue(NETISR_IPV6, m);
-		goto out;
+		goto freecopy;
 	}
 	/* Or forward to some other address? */
 	if ((m->m_flags & M_IP6_NEXTHOP) &&
@@ -400,8 +401,11 @@ pass:
 	    ~nh->nh_ifp->if_hwassist)) {
 		int offset = ip6_lasthdr(m, 0, IPPROTO_IPV6, NULL);
 
-		if (offset < sizeof(struct ip6_hdr) || offset > m->m_pkthdr.len)
+		if (offset < sizeof(struct ip6_hdr) ||
+		    offset > m->m_pkthdr.len) {
+			m_freem(mcopy);
 			goto bad;
+		}
 		in6_delayed_cksum(m, m->m_pkthdr.len - offset, offset);
 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
 	}