Article View: linux.kernel.netdev
Article #25679[PATCH ipsec v3 2/2] selftests: net: add a test for xfrm policy removal
From: Ralf Lici
Date: Wed, 02 Dec 2026 08:52
Date: Wed, 02 Dec 2026 08:52
281 lines
17383 bytes
17383 bytes
Check that a connected UDP socket that cached its route through an allow
policy stops sending in plaintext once that policy is deleted, deleted
by index, expires or is flushed.
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
tools/testing/selftests/net/Makefile | 1 +
tools/testing/selftests/net/config | 1 +
.../selftests/net/xfrm_policy_removal.sh | 236 ++++++++++++++++++
3 files changed, 238 insertions(+)
create mode 100755 tools/testing/selftests/net/xfrm_policy_removal.sh
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 9180b9bec515..bdc31252a6fc 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -136,6 +136,7 @@ TEST_PROGS := \
vrf_route_leaking.sh \
vrf_strict_mode_test.sh \
xfrm_policy.sh \
+ xfrm_policy_removal.sh \
xfrm_state.sh \
# end of TEST_PROGS
diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config
index d4416f8bdcfd..d5d289089059 100644
--- a/tools/testing/selftests/net/config
+++ b/tools/testing/selftests/net/config
@@ -18,6 +18,7 @@ CONFIG_DUMMY=y
CONFIG_GENEVE=m
CONFIG_GTP=m
CONFIG_IFB=y
+CONFIG_INET6_ESP=y
CONFIG_INET_DIAG=y
CONFIG_INET_ESP=y
CONFIG_INET_ESP_OFFLOAD=y
diff --git a/tools/testing/selftests/net/xfrm_policy_removal.sh b/tools/testing/selftests/net/xfrm_policy_removal.sh
new file mode 100755
index 000000000000..bbd8e67d57ff
--- /dev/null
+++ b/tools/testing/selftests/net/xfrm_policy_removal.sh
@@ -0,0 +1,236 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Test that removing an xfrm policy invalidates the routes that sockets
+# cached while it was in place.
+#
+# An allow policy without templates exempts UDP to one port from an ESP
+# policy with a lower priority. A connected UDP socket sends a datagram
+# through the allow policy, caching a plain route, then the allow policy is
+# deleted, deleted by index, expires or is flushed, and the socket sends
+# again. The datagrams sent after the removal must be encrypted, as counted
+# by the SA. For the flush, the output default is block instead of the ESP
+# policy, so these sends must fail.
+
+source lib.sh
+
+NS=""
+SRC=""
+DST=""
+PORT=5002
+COUNT=10
+KEY=0x0102030405060708090a0b0c0d0e0f1011121314
+
+cleanup() {
+ stop_sender
+ cleanup_all_ns
+}
+
+trap cleanup EXIT
+
+# setup creates the namespace, a dummy device with the IPv$1 sender
+# address and the state and policies for removal $2. It returns
+# $ksft_skip if the system lacks something the test needs, and fails if
+# a policy is rejected.
+setup() {
+ local family=$1
+ local mode=$2
+ local addr=(10.0.0.1/24)
+ local limit=()
+
+ SRC=10.0.0.1
+ DST=10.0.0.2
+ if [ "$family" = 6 ]; then
+ SRC=2001:db8::1
+ DST=2001:db8::2
+ addr=(2001:db8::1/64 nodad)
+ fi
+
+ setup_ns NS || return "$ksft_skip"
+ # The datagrams only need to leave the sender: a dummy device drops
+ # them and needs no neighbour resolution for the peer.
+ ip -n "$NS" link add dummy0 type dummy || return "$ksft_skip"
+ # No link-local address: adding its routes would advance the route
+ # generation and hide a missing invalidation.
+ ip -n "$NS" link set dummy0 addrgenmode none
+ ip -n "$NS" link set dummy0 up
+ ip -n "$NS" addr add "${addr[@]}" dev dummy0
+
+ # The SA that the ESP policy requires. Its packet counter tells
+ # which datagrams were encrypted.
+ ip -n "$NS" xfrm state add src "$SRC" dst "$DST" proto esp spi 0x1000 \
+ reqid 1 mode transport aead 'rfc4106(gcm(aes))' "$KEY" 128 ||
+ return "$ksft_skip"
+ if [ "$mode" = flush ]; then
+ # A flush also removes the ESP policy, so rely on the output
+ # default instead.
+ ip -n "$NS" xfrm policy setdefault in accept fwd accept \
+ out block || return "$ksft_skip"
+ else
+ ip -n "$NS" xfrm policy add src "$SRC" dst "$DST" dir out \
+ priority 100 tmpl src "$SRC" dst "$DST" proto esp \
+ reqid 1 mode transport || return 1
+ fi
+ # The allow policy, which takes precedence for the test port.
+ if [ "$mode" = expire ]; then
+ limit=(limit time-hard 3)
+ fi
+ ip -n "$NS" xfrm policy add src "$SRC" dst "$DST" proto udp \
+ dport "$PORT" dir out priority 10 action allow "${limit[@]}" ||
+ return 1
+}
+
+# sender runs in the namespace and keeps one UDP socket connected to
+# $1 port $2, so that the route it caches survives across the removal.
+# For each count it reads, it sends that many datagrams and prints how
+# many sends failed.
+# shellcheck disable=SC2329 # run in the namespace through declare -f
+sender() {
+ local fails
+ local n
+ local i
+
+ exec 3>"/dev/udp/$1/$2" || exit 1
+ while read -r n; do
+ fails=0
+ for ((i = 0; i < n; i++)); do
+ echo >&3 2>/dev/null || fails=$((fails + 1))
+ done
+ echo "$fails"
+ done
+}
+
+start_sender() {
+ coproc SENDER {
+ ip netns exec "$NS" bash -c \
+ "$(declare -f sender); sender $DST $PORT"
+ }
+}
+
+stop_sender() {
+ if [ -n "$SENDER_PID" ]; then
+ kill "$SENDER_PID" 2>/dev/null
+ wait "$SENDER_PID" 2>/dev/null
+ fi
+}
+
+send_datagrams() {
+ local fails
+
+ # Ask the sender for $1 datagrams
+ echo "$1" >&"${SENDER[1]}" || return 1
+ # and print how many of its sends failed.
+ read -r -t 5 fails <&"${SENDER[0]}" || return 1
+ echo "$fails"
+}
+
+# sa_packets prints the number of packets that the SA has encrypted.
+sa_packets() {
+ ip -n "$NS" -s xfrm state |
+ sed -nE 's/^\s*[0-9]+\(bytes\), ([0-9]+)\(packets\)$/\1/p'
+}
+
+# sent_encrypted sends one datagram and succeeds if the SA encrypted it.
+# shellcheck disable=SC2329 # called through slowwait
+sent_encrypted() {
+ local esp
+
+ esp=$(sa_packets)
+ send_datagrams 1 >/dev/null
+ [ "$(sa_packets)" -gt "$esp" ]
+}
+
+# allow_policy_gone succeeds once the allow policy is no longer installed.
+allow_policy_gone() {
+ ! ip -n "$NS" xfrm policy list dir out | grep -q "dport $PORT"
+}
+
+remove_allow_policy() {
+ local mode=$1
+ local index
+
+ case $mode in
+ delete)
+ ip -n "$NS" xfrm policy delete src "$SRC" dst "$DST" \
+ proto udp dport "$PORT" dir out
+ ;;
+ index)
+ index=$(ip -n "$NS" -s xfrm policy list dir out |
+ grep -A1 "dport $PORT" |
+ sed -nE 's/.* index ([0-9]+) .*/\1/p')
+ ip -n "$NS" xfrm policy delete dir out index "$index"
+ ;;
+ expire)
+ slowwait 10 allow_policy_gone
+ ;;
+ flush)
+ ip -n "$NS" xfrm policy flush
+ ;;
+ esac
+ allow_policy_gone
+}
+
+run_test() {
+ local family=$1
+ local mode=$2
+ local name="IPv$family: send after $3"
+ local plain
+ local fails
+ local sent
+ local esp
+ local ret
+
+ RET=0
+
+ setup "$family" "$mode"
+ ret=$?
+ if [ "$ret" = "$ksft_skip" ]; then
+ log_test_skip "$name"
+ cleanup_all_ns
+ return
+ elif [ "$ret" != 0 ]; then
+ check_err 1 "a policy was rejected"
+ log_test "$name"
+ cleanup_all_ns
+ return
+ fi
+
+ start_sender
+ fails=$(send_datagrams 1)
+ check_err $? "the sender did not answer"
+ check_err "$fails" "the datagram through the allow policy failed"
+ esp=$(sa_packets)
+ check_err "$esp" "the allow policy did not apply"
+
+ remove_allow_policy "$mode"
+ check_err $? "the allow policy was not removed"
+ if [ "$mode" = expire ]; then
+ # An IPv6 expiry invalidates the routes from a work item
+ slowwait 5 sent_encrypted
+ esp=$(sa_packets)
+ fi
+
+ fails=$(send_datagrams "$COUNT")
+ check_err $? "the sender did not answer"
+ if [ "$mode" = flush ]; then
+ sent=$((COUNT - fails))
+ check_err "$sent" "$sent of $COUNT datagrams not blocked"
+ else
+ plain=$((COUNT - fails - $(sa_packets) + esp))
+ check_err "$fails" "$fails of $COUNT sends failed"
+ check_err "$plain" \
+ "$plain of $COUNT datagrams sent in plaintext"
+ fi
+
+ log_test "$name"
+ cleanup
+}
+
+for family in 4 6; do
+ run_test "$family" delete "policy deletion"
+ run_test "$family" index "policy deletion by index"
+ run_test "$family" expire "policy expiry"
+ run_test "$family" flush "policy flush"
+done
+
+exit "$EXIT_STATUS"
--
2.56.1
Technical Details
Message-ID:
<1894c17e6f839d233000b58186131d442d3e9672.1796200887.git.ralf@mandelbit.com>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375653-linux-kernel-netdev=dewy.ruReferences:
<cover.1796200887.git.ralf@mandelbit.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375653-linux-kernel-netdev=dewy.ru From: ralf@mandelbit.com (Ralf Lici) Newsgroups: linux.kernel.netdev Subject: [PATCH ipsec v3 2/2] selftests: net: add a test for xfrm policy removal Date: Wed, 2 Dec 2026 09:52:03 +0100 Organization: Usenet Network Message-ID: <1894c17e6f839d233000b58186131d442d3e9672.1796200887.git.ralf@mandelbit.com> References: <cover.1796200887.git.ralf@mandelbit.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1927780"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:xsfWYBF/l0O4+fyKGY0CGC+pHBQ= sha256:by2IX/vEWC0o9jYxdghyMAo1K/e7LPHTb3gfBdZRWLE= sha1:kFNtAGrKPdYVZZXVZlr1NuIsKy4= sha256:5HD0NC38LkcENAY+Y96Hllhx8T/voXpAQaTsZWgdnGE= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 64CF61175B9; Wed, 2 Dec 2026 11:52:56 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.05 X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=mandelbit.com X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GnwSztprCa_6; Wed, 2 Dec 2026 11:52:53 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375653-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=mandelbit.com X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 01E271175B8; Wed, 2 Dec 2026 11:52:44 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 7AE9B2B8DE; Wed, 2 Dec 2026 08:52:36 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796201556; b=1kc4Ij4DKOFNkSIK8avcEFFWgPjSnVOLPkmOXtIciLCTLDFXrPtEfHE2J0pf1BT2q/IZ tRdtrCjjrejI+aWo7vNG+LZU8TkqKG1rwzUSpjuim+mDgOLMwU5Jy+CF4LUEMopKKiIIr 1nhK9hPDvjsrhu8zUd2yEAy+WLEE4JfWZECjmxiCJc5TDgoZiScLDt25qT0Obq5N4IQ/e HzqLJhWivgxivrYq1yhbuOzSG/V/0sOJvwAL4aRsPELW9RonDC9sqaCp8FF3jS9anSQKQ PXkV9Da5MVI0wyTZf357m9KBlHoStTbuC84+YKOpE1Iw05bXG+4I/wLVZSyvyUc3ZrQ== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796201556; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To: Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding:X-Rspamd-Queue-Id; bh=E02FqgGFQzlTXLyU1LvAbsbmdDW/d41UicUcYv3N/fQ=; b=FGo2p99hpDXRvqQAYiTxiGM6Wd/aH0ZFJ7zsLOIIAP5F3IDXVVTl3Ph6aToMA6oX5VoY x7YlftT/RCeBPc/Xr+nWaHf/SB7zWHDzrY05NkU3JfvSnbeYbarhMdySgnXIzO5aeb5n7 exnY44jSymdswYdxRnfO81yYo6Ojp8Ac7ULa9mkxEfRL4vuod4Ql7OYh5kDZ5vGuXAFK0 dNBEUUV3mYHzbOfPyFlZMT2Q7lJeOkJhm5PiJhjWeal/WCU3GV3vFxzhQu2GpGLDztycN mT2EDasLWT4rdkyOSefUU4m3sNgSPKN2RD21z2bKL/iZe+QQTtEVSlFKC1xb4KygxKg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b="oS77oK/1"; dmarc=none header.from=mandelbit.com; spf=pass smtp.mailfrom=mandelbit.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4D210606A68E; Wed, 2 Dec 2026 08:52:36 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=oS77oK/1 X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.51 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796201550; b=AXKurNuwnAmPBelTXeLG8x8f4CJ4ZnSI8j4qo7AxG1gy1G395MMxDraYwe15ldA2WsJw /Pql8WGaWppaDY+/PVoi1eeycVh7N2HeCGACuGGrUBD8EW+Z3Na/RxB+p6a3bYaDCi7bS BYhjZl++WXa8S3keG14+fWxiusrifUfOs1y1WY+9PAU70Y3pEPBq55hLz7RhHwFIV5lq1 RZef5TcByqDKz2HTf9cyAdFLvc/J1+shJLl/zVUUnrDG2MGUjyC4ccjVX7Y4b1iSey8ji JLem1gTiGERWMNWs85hIDC7o8jNVELIX0lSU1ISMOcb/gCwkdIZcVWnTW8ktiAWuCFQ== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796201550; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Rspamd-Queue-Id; bh=E02FqgGFQzlTXLyU1LvAbsbmdDW/d41UicUcYv3N/fQ=; b=MKZFoYNjOFoZXrgNyqJ0oUsSSRqwdcOZ9ulU0lln74tPcsnkwBbVZ9FgHv8o6c18rrO1 KRIiuo642tui8SNrHbjIycik9GS+86VnMZrdT6jh5vpSbfQQ5V/KpQua8pTHPwxfiKfmg IWumxciVzP4RfCBFvNZh9BDMuJPYvHpb3XZou1bpmW/OwesRs5f0GIpbZWFphwk6CwaJO QD7PkarGqKs2evYv0Y7GRbEmIIyPvJGYE75pBp3cOV0akb07161rX4t6Ifuc2EXesZ4JH cEQZbAhU/0incPi7ABV3ay3fM/Mbz/Q5YKeBOY+asj6ZPoZur9PryWHKhNsQJLZ6TxA== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=mandelbit.com; spf=pass smtp.mailfrom=mandelbit.com; dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b="oS77oK/1"; arc=none smtp.remote-ip=195.10.208.51 X-Received: from mout-b-206.mailbox.org (mout-b-206.mailbox.org [195.10.208.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8FD31606A338; Wed, 2 Dec 2026 08:52:27 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8FD31606A338 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com X-Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-b-206.mailbox.org (Postfix) with ESMTPS id 4jfWjG3f8gzB9; Wed, 02 Dec 2026 09:52:18 +0100 (CET) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com; s=MBO0001; t=1796201538; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E02FqgGFQzlTXLyU1LvAbsbmdDW/d41UicUcYv3N/fQ=; b=oS77oK/1u4p9Wn5N4RxXjJh2VBroXoUxeT4Xonp4GNJo9ouMh4UztQHEQ5bsSd13QKDtFx wYu0LuLLIDjr0ppEvkK+qP7td82A0RwUMbfRprybN8O+JRjscTfVzFeUzqocLOqBAgGHH2 FGSGgqPvK3rkHbN905jAiE3xuNE/t0lRossPy5HbnKfBfbJdYA9PHnwvwAXOQnejJjkLxU 5PBiGTawgAyVzIaC7BWHc666vTS1Gx7Av7FrQTIpmTzeVRMxCtU2Sjc/Z906zLsSGq0Jq5 S/xxRXvrjuAt+n8wgc3VAxmjeDMuHDnEG/7l/TBRJzPfFCj6pGej6u5Q9Vow8Q== X-Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of ralf@mandelbit.com designates 2001:67c:2050:b231:465::102 as permitted sender) smtp.mailfrom=ralf@mandelbit.com X-Cc: "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Willem de Bruijn <willemb@google.com>, Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>, Nicolas Dichtel <nicolas.dichtel@6wind.com>, Sabrina Dubroca <sd@queasysnail.net>, netdev@vger.kernel.org, ipsec@lists.linux.dev, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailer: git-send-email 2.56.1 X-In-Reply-To: <cover.1796200887.git.ralf@mandelbit.com> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-Rspamd-Queue-Id: 4jfWjG3f8gzB9