git: d40d299bd1f5 - main - netipsec: Send the packet when the rest of a bundle is skipped
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 20 Nov 2026 20:27:36 UTC
The branch main has been updated by rcm:
URL: https://cgit.FreeBSD.org/src/commit/?id=d40d299bd1f54a8f5906a77209dfb62e9a9e74e4
commit d40d299bd1f54a8f5906a77209dfb62e9a9e74e4
Author: R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-20 13:50:24 +0000
Commit: R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-20 20:27:30 +0000
netipsec: Send the packet when the rest of a bundle is skipped
A policy can bundle several requests, for example an ESP tunnel and an AH
transport request. A request at level "use" that has no SA is skipped (as
is "default", which the net.inet.ipsec.*_deflev and
net.inet6.ipsec6.*_deflev sysctls make "use" by default), and the packet
is meant to leave with the transforms of the other requests.
ipsec_process_done() applies the next request of a bundle once a transform
completes. When all remaining requests are skipped,
ipsec{4,6}_perform_request() returns EJUSTRETURN and leaves the packet to
its caller, but ipsec_process_done() returned that to the transform, which
does not send the packet or free it. Keep the references to the SP and the
SA across the call, and on EJUSTRETURN send the packet with the transforms
applied so far, after the UDP encapsulation of the last SA applied.
ipcomp_output() calls ipsec_process_done() directly for packets below the
compression threshold and passed the EJUSTRETURN on to ip_output(), which
then went on with a packet that tunnel mode encapsulation had already
changed. It no longer sees one.
Reviewed by: kib, ae
Fixes: 9e3bdede0f3d ("Correct IPsec behaviour with a 'use'...")
MFC after: 2 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D61638
---
sys/netipsec/ipsec_output.c | 28 ++++++++++++-----
tests/sys/netipsec/tunnel/Makefile | 1 +
tests/sys/netipsec/tunnel/bundle.sh | 63 +++++++++++++++++++++++++++++++++++++
3 files changed, 84 insertions(+), 8 deletions(-)
diff --git a/sys/netipsec/ipsec_output.c b/sys/netipsec/ipsec_output.c
index 0a35a6edf0d9..218556aa016b 100644
--- a/sys/netipsec/ipsec_output.c
+++ b/sys/netipsec/ipsec_output.c
@@ -998,22 +998,29 @@ ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
* doing further processing.
*/
if (++idx < sp->tcount) {
+ /*
+ * The request consumes a reference to the SP. Keep ours and
+ * the SA: when none of the remaining requests has an SA and
+ * none requires one, the request returns EJUSTRETURN and
+ * leaves the packet to be sent here, with the transforms
+ * applied so far.
+ */
switch (saidx->dst.sa.sa_family) {
#ifdef INET
case AF_INET:
- key_freesav(&sav);
+ key_addref(sp);
IPSECSTAT_INC(ips_out_bundlesa);
- return (ipsec4_perform_request(NULL, m, ip, sp, NULL,
- idx, 0));
- /* NOTREACHED */
+ error = ipsec4_perform_request(NULL, m, ip, sp, NULL,
+ idx, 0);
+ break;
#endif
#ifdef INET6
case AF_INET6:
- key_freesav(&sav);
+ key_addref(sp);
IPSEC6STAT_INC(ips_out_bundlesa);
- return (ipsec6_perform_request(NULL, m, sp, NULL,
- idx, 0));
- /* NOTREACHED */
+ error = ipsec6_perform_request(NULL, m, sp, NULL,
+ idx, 0);
+ break;
#endif /* INET6 */
default:
DPRINTF(("%s: unknown protocol family %u\n", __func__,
@@ -1021,6 +1028,11 @@ ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
error = EPFNOSUPPORT;
goto bad;
}
+ if (error != EJUSTRETURN) {
+ key_freesav(&sav);
+ key_freesp(&sp);
+ return (error);
+ }
}
#ifdef INET6
/*
diff --git a/tests/sys/netipsec/tunnel/Makefile b/tests/sys/netipsec/tunnel/Makefile
index de1b56c4cc0b..b3946dc2e9d2 100644
--- a/tests/sys/netipsec/tunnel/Makefile
+++ b/tests/sys/netipsec/tunnel/Makefile
@@ -11,6 +11,7 @@ ATF_TESTS_SH+= empty \
aesni_aes_cbc_256_hmac_sha2_256 \
aesni_aes_gcm_128 \
aesni_aes_gcm_256 \
+ bundle \
chacha20_poly1305 \
esp_iv \
flowlabel \
diff --git a/tests/sys/netipsec/tunnel/bundle.sh b/tests/sys/netipsec/tunnel/bundle.sh
new file mode 100755
index 000000000000..f4865c6a4c8e
--- /dev/null
+++ b/tests/sys/netipsec/tunnel/bundle.sh
@@ -0,0 +1,63 @@
+
+. $(atf_get_srcdir)/utils.subr
+
+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()
+{
+ af=$1
+ 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
+ else
+ lan_a=2001:db8:1::/64; lan_b=2001:db8:45::/64; target=2001:db8:45::1
+ gw_a=2001:db8:23::2; gw_b=2001:db8:34::2
+ fi
+ sas="add ${gw_a} ${gw_b} esp 0x1000 -m tunnel -E aes-gcm-16 \"${KEY}\";
+add ${gw_b} ${gw_a} esp 0x1001 -m tunnel -E aes-gcm-16 \"${KEY}\";"
+
+ ist_init
+ ist_labsetup
+ ist_v${af}_setup
+ cat > ${TMPDIR}/ipsec.ipsecA.conf <<-EOF
+ flush;
+ spdflush;
+ spdadd ${lan_a} ${lan_b} any -P out ipsec esp/tunnel/${gw_a}-${gw_b}/require ah/transport//use;
+ spdadd ${lan_b} ${lan_a} any -P in ipsec esp/tunnel/${gw_b}-${gw_a}/require;
+ ${sas}
+ EOF
+ cat > ${TMPDIR}/ipsec.ipsecB.conf <<-EOF
+ flush;
+ spdflush;
+ spdadd ${lan_a} ${lan_b} any -P in ipsec esp/tunnel/${gw_a}-${gw_b}/require;
+ spdadd ${lan_b} ${lan_a} any -P out ipsec esp/tunnel/${gw_b}-${gw_a}/require;
+ ${sas}
+ EOF
+ atf_check -s exit:0 -o ignore jexec ipsecA setkey -f ${TMPDIR}/ipsec.ipsecA.conf
+ atf_check -s exit:0 -o ignore jexec ipsecB setkey -f ${TMPDIR}/ipsec.ipsecB.conf
+ atf_check -s exit:0 -o ignore jexec hostA ping -c 3 -i 0.2 ${target}
+}
+
+atf_test_case "use_v4" "cleanup"
+use_v4_head()
+{
+ atf_set descr 'A bundled request at level use without an SA is skipped (IPv4)'
+ atf_set require.user root
+}
+use_v4_body()
+{
+ bundle_use 4
+}
+use_v4_cleanup()
+{
+ ist_cleanup
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case "use_v4"
+}