git: 655407eeea5b - main - netipsec: Apply the rest of a bundle when the PMTU check finds no SA

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Wed, 25 Nov 2026 13:55:24 UTC
The branch main has been updated by rcm:

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

commit 655407eeea5bf6ee1db75ee20496d3c57efcbf10
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-25 12:58:48 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-25 13:46:33 +0000

    netipsec: Apply the rest of a bundle when the PMTU check finds no SA
    
    ipsec{4,6}_check_pmtu() looks up the SA of the last request of a policy to
    learn the path MTU.  When that request is at level "use" and no SA is
    found for it, the lookup returns EJUSTRETURN, and ipsec4_common_output1()
    and ipsec6_common_output() took that to mean that the packet needs no
    IPsec processing.  That holds for a policy with one request.  For a bundle
    it also skipped the requests before the last one, for example the ESP
    tunnel request of "esp/tunnel/A-B/require ah/transport//use", and the
    packet was sent without their transforms.
    
    The lookup finds no SA when none is installed, and also when the last
    request is a transport request that follows a tunnel request, as in the
    example.  The lookup fills in the empty addresses of a transport request
    from the packet as it is before any transform, while the transform applies
    the request after the tunnel request, to the outer header, and the SA is
    installed for the addresses of the tunnel.  So the bundle above was sent
    without its transforms even with an AH SA from A to B in place.
    
    For IPv4 the check runs when net.inet.ipsec.dfbit is 1, or is above 1 and
    the packet has DF set, and not with the default of 0.  For IPv6 it runs for
    forwarded packets and not for locally originated ones.
    
    Return 0 from the check in this case and keep the reference to the SP, so
    that ipsec{4,6}_process_packet() goes through the requests as it does when
    the check does not run.  It returns EJUSTRETURN itself when no request
    applies, and such a packet is still sent without IPsec processing.
    
    When the check finds no SA it does not compare the packet with the path
    MTU.  With net.inet.ipsec.dfbit set the outer IPv4 header carries DF, so a
    packet that is too big once encapsulated is dropped without an ICMP
    needfrag reaching its sender.  ip6_output() fragments an outer IPv6
    packet instead.
    
    Reviewed by:            kib
    Fixes:                  6b66194bcb7e ("ipsec: Check PMTU before sending...")
    Fixes:                  9dfc8606eb80 ("ipsec: Add support for PMTUD...")
    MFC after:              2 weeks
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61645
---
 sys/netipsec/ipsec_output.c         | 38 +++++++++++++++++-----------
 tests/sys/netipsec/tunnel/bundle.sh | 49 +++++++++++++++++++++++++++++++++----
 2 files changed, 68 insertions(+), 19 deletions(-)

diff --git a/sys/netipsec/ipsec_output.c b/sys/netipsec/ipsec_output.c
index 218556aa016b..281d5233e220 100644
--- a/sys/netipsec/ipsec_output.c
+++ b/sys/netipsec/ipsec_output.c
@@ -374,6 +374,16 @@ setdf:
 	idx = sp->tcount - 1;
 	sav = ipsec4_allocsa(ifp, m, ip1, sp, &idx, &error);
 	if (sav == NULL) {
+		/*
+		 * No SA was found for the last request and it does not
+		 * require one.  The requests before it may still apply, and
+		 * a transport request after a tunnel request was looked up
+		 * with the addresses of this packet rather than those of the
+		 * tunnel, so it may have an SA after all.  Leave them to
+		 * ipsec4_process_packet() and skip the PMTU check.
+		 */
+		if (error == EJUSTRETURN)
+			return (0);
 		key_freesp(&sp);
 		/*
 		 * No matching SA was found and SADB_ACQUIRE message was generated.
@@ -381,8 +391,7 @@ setdf:
 		 */
 		if (error == 0)
 			error = EINPROGRESS;
-		if (error != EJUSTRETURN)
-			m_freem(m);
+		m_freem(m);
 
 		return (error);
 	}
@@ -451,12 +460,8 @@ ipsec4_common_output1(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp,
 
 	/* NB: callee frees mbuf and releases reference to SP */
 	error = ipsec4_check_pmtu(ifp, m, ip1, sp, forwarding);
-	if (error != 0) {
-		if (error == EJUSTRETURN)
-			return (0);
-
+	if (error != 0)
 		return (error);
-	}
 
 	error = ipsec4_process_packet(ifp, m, ip1, sp, inp, mtu);
 	if (error == EJUSTRETURN) {
@@ -793,6 +798,16 @@ ipsec6_check_pmtu(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
 	idx = sp->tcount - 1;
 	sav = ipsec6_allocsa(ifp, m, sp, &idx, &error);
 	if (sav == NULL) {
+		/*
+		 * No SA was found for the last request and it does not
+		 * require one.  The requests before it may still apply, and
+		 * a transport request after a tunnel request was looked up
+		 * with the addresses of this packet rather than those of the
+		 * tunnel, so it may have an SA after all.  Leave them to
+		 * ipsec6_process_packet() and skip the PMTU check.
+		 */
+		if (error == EJUSTRETURN)
+			return (0);
 		key_freesp(&sp);
 		/*
 		 * No matching SA was found and SADB_ACQUIRE message was generated.
@@ -800,8 +815,7 @@ ipsec6_check_pmtu(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
 		 */
 		if (error == 0)
 			error = EINPROGRESS;
-		if (error != EJUSTRETURN)
-			m_freem(m);
+		m_freem(m);
 
 		return (error);
 	}
@@ -855,12 +869,8 @@ ipsec6_common_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp,
 	}
 
 	error = ipsec6_check_pmtu(ifp, m, sp, forwarding);
-	if (error != 0) {
-		if (error == EJUSTRETURN)
-			return (0);
-
+	if (error != 0)
 		return (error);
-	}
 
 	/* NB: callee frees mbuf and releases reference to SP */
 	error = ipsec6_process_packet(ifp, m, sp, inp, mtu);
diff --git a/tests/sys/netipsec/tunnel/bundle.sh b/tests/sys/netipsec/tunnel/bundle.sh
index f4865c6a4c8e..629ccb1e8c08 100755
--- a/tests/sys/netipsec/tunnel/bundle.sh
+++ b/tests/sys/netipsec/tunnel/bundle.sh
@@ -3,13 +3,16 @@
 
 KEY="12345678901234567890"
 
-# bundle_use 4|6: ipsecA's outbound policy bundles a required ESP tunnel
-# with an AH transport request at level "use", and only the ESP SAs exist.
-# The AH request is skipped, so the packets must leave with ESP alone;
-# ipsecB has a plain ESP tunnel and drops anything else.
+# bundle_use 4|6 [DFBIT]: ipsecA's outbound policy bundles a required ESP
+# tunnel with an AH transport request at level "use", and only the ESP SAs
+# exist.  The AH request is skipped, so the packets must leave with ESP
+# alone: ipsecB has a plain ESP tunnel and drops anything else, and the
+# router has no route to lan_b for a packet sent without the tunnel.
+# DFBIT sets net.inet.ipsec.dfbit on ipsecA, which makes IPv4 run the PMTU
+# check, as forwarded IPv6 always does.
 bundle_use()
 {
-	af=$1
+	af=$1; dfbit=${2:-}
 	if [ ${af} = 4 ]; then
 		lan_a=192.0.2.0/30; lan_b=203.0.113.0/30; target=203.0.113.1
 		gw_a=198.51.100.2; gw_b=198.51.100.6
@@ -23,6 +26,10 @@ add ${gw_b} ${gw_a} esp 0x1001 -m tunnel -E aes-gcm-16 \"${KEY}\";"
 	ist_init
 	ist_labsetup
 	ist_v${af}_setup
+	if [ -n "${dfbit}" ]; then
+		atf_check -s exit:0 -o ignore \
+		    jexec ipsecA sysctl net.inet.ipsec.dfbit=${dfbit}
+	fi
 	cat > ${TMPDIR}/ipsec.ipsecA.conf <<-EOF
 	flush;
 	spdflush;
@@ -57,7 +64,39 @@ use_v4_cleanup()
 	ist_cleanup
 }
 
+atf_test_case "use_v4_dfbit" "cleanup"
+use_v4_dfbit_head()
+{
+	atf_set descr 'A bundled request at level use without an SA is skipped (IPv4, PMTU check)'
+	atf_set require.user root
+}
+use_v4_dfbit_body()
+{
+	bundle_use 4 1
+}
+use_v4_dfbit_cleanup()
+{
+	ist_cleanup
+}
+
+atf_test_case "use_v6" "cleanup"
+use_v6_head()
+{
+	atf_set descr 'A bundled request at level use without an SA is skipped (IPv6)'
+	atf_set require.user root
+}
+use_v6_body()
+{
+	bundle_use 6
+}
+use_v6_cleanup()
+{
+	ist_cleanup
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case "use_v4"
+	atf_add_test_case "use_v4_dfbit"
+	atf_add_test_case "use_v6"
 }