From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796033696; b=oj/upP1NgYNMBCYaPXRqBK9H3MFg+GJ/bHOxKai0GFHUwF9QIYRFSRq87pXSG8bbr8lg LSY5Ttaow5zkZ6uc96fBfYT+JBS5ld3o5xqJcHx9GZ4DtWcPLrq4cbqhkftRnF7MSBJ4E NXQTBYlOtfY3MjTUrSmUTNDuQY8XDIJZJzXqK4Y15ZxMsG2jzYN0ma1c3gQsPW+rMH5ax IS9cYcIUaWQMUmcIOKm8VDvs0h2vUgWYVLJ6ZLhtTgmRTMATWnhCzmx9WyVNQxjhAvxXO pkuROk0aytAKz/NL/bz/RpcDl8ckDqJVZmN0sXHTlpNHSGclbA5VJaiEJdrWrsurlDg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796033696; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Reply-To:Subject:To:Cc: References:From:Content-Language:Organization:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=FEBRsU+BIWM57JcxOL5e2Sg3/DekNekqfPOdp+VohD0=; b=5B/VWHOde/NIvkh/NH3wjX5g5u6brphw1vk1+U6YHVirMLchI4ih/gSlx9GDAmZ/SExr 2TmNFtOeCljuS+jF5sPcwXNL7mLnBXytcxtPP7g09wU7pgD2PRtBwXtOw2GNSNa9JEd6+ bJgGWLT8e0ZrdluO3D9DBoPCbnqeIrYcxjOtnHxQbWFrhEqokTVKpVolqZrRQ17fVsh/L r69eHUAwEiC6PLtxi1CCWOi2aSdA6t4czRK90oGluaHxIHjc0EO23uzQazQEjKxBdOcmb MsLgFnEqt4luELThzC/PdzR/Y7bVFxndc3qbvyKykdj3rUx7NKP/5qcIC7nKcFOmoCg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=6wind.com; spf=pass smtp.mailfrom=6wind.com; dkim=pass header.d=6wind.com header.i=@6wind.com header.a=rsa-sha256 header.s=google header.b=YHZvfU6V; arc=none smtp.remote-ip=209.85.221.44 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3D79606A329 for ; Mon, 30 Nov 2026 10:14:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B3D79606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=6wind.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=6wind.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=6wind.com header.i=@6wind.com header.a=rsa-sha256 header.s=google header.b=YHZvfU6V Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-49d611f6938so414707f8f.3 for ; Mon, 30 Nov 2026 02:14:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind.com; s=google; t=1796033694; x=1796638494; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:organization :content-language:from:references:cc:to:subject:reply-to:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to:content-type; bh=FEBRsU+BIWM57JcxOL5e2Sg3/DekNekqfPOdp+VohD0=; b=YHZvfU6V4+pWQycIDjISm0RTlyhszEFZkzNPNXZrmql+Yf0bxvdfkFAjGjLp4DsBFc 8ofzCA5JSmFzSTKWohgd6ji3AOGHkdJkEy/r7IQ1OwZEuzo8s5dNWPhLCyRLOtzH/4lz oTWCRU2NB0QtHKqYLR2+3Xnb+jOf2gaasXqGRXLjOY63n6XXVFjcK3fL2OMOxKIicdv0 LzmCq3m9bMlTwiFg9h1fGHWmFQe1ZbUzre+aB6NV/rZ5GTkun+ZL4gf1SHx2atOFaDyl aQZKumRkrNLSwIdpWxnU418rv+wShQ0W2qBMEHc0hDYa4OaNpnfCAG8HyzQl5znPeZ/o hfQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796033694; x=1796638494; h=content-transfer-encoding:content-type:in-reply-to:organization :content-language:from:references:cc:to:subject:reply-to:user-agent :mime-version:date:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=FEBRsU+BIWM57JcxOL5e2Sg3/DekNekqfPOdp+VohD0=; b=vluAdvPe5kw5+cE5itaP/m4mheqANFN7oOfXxziU/kwM2eQVsPzzr4MvEMBSO+w4rj bCYg/9mJejY88YW5Ieh9KKThol70ybRu8SGjyCp0ANuESaSnBedU3dJoO+viVGOonMsE teCfXqK0bq+gi0KuRCkpTb0OppDUz2hcOhESVPloy7qnfWLF75AYoOnjWDUWOvXtA4Ll HrsXCy3KnN3L4eAjzlHttTmOPWrUFnAtURQOVxpDsLtucLS8zdlOtNiAO4NUmgeOXMwB jbAqQ3qMfpsutjO3h2lTmP27mO7ljFrVKSQUmuZEMY0HVx5aigdKaQP+8qNAc2rpjLxJ OzpQ== X-Forwarded-Encrypted: i=1; AL+hErFjO2eJZH10kWgRTIoG9tt5XarH3vlAJnFBA2e9gsk20BsbRnSaiobriWX15FWleCW1hCEN7rQ=@vger.kernel.org X-Gm-Message-State: APuU4Z+D/htvwHuBcbNRsykCYPtOhvRjZb0DRo43NHfpxtPkIyQzJ0po v7i18Tv4w1ba38XtJOHUVziwxAJkzMShUMSOHmwFTdIGX/iKtJbA4DfEtLmW6r6gmAE= X-Gm-Gg: AZFdTZfgLDs5nnhEoDhJvbL+54VVzdV7vMJ3gcO/P0ReE8gq05Haf/4EksurInOFAY7 HyPajQr1PS7I9PaOZ1cLC+G66KUfrTlEGw4m0iZGbsh+mVjIuZdv+ohe+eZAzwb17ArlZtflWmK GWKkIGuwCNizQOWd/2mBfkPCQUxUjFtAMdTG1W3k9VsCo7cFrz6sUgaT3vWrSI2jeJzeq75asnx bTddIKjMyhaeW0tL7qXK9UOao3YHLed2cUOZW797eQKsy8Zo5y7e/jICywgDasQXVqb0Gb9kM8m 0CvAgDFAUBjIlkPB3kyNHveTNWinHqTVkcAPhZ3YSP4NOkbZtAmiTpYR+9sR9WuN/DpJF42w54E pN8raxgEaZp6hMFEiZVB1BLxBxZ63jFQs0Tdek5EnjrnbOsv9CUUG/x4j1Xnei58BgsrFG+Q6pL HBobmeHGvO49L6xIMoFhGDtZ+9EGE+3uKQrjXNjs7h3ASunyQW07EuQeznHskDVzR/qI/vu3ULr EqjRjbsANoAUdJsSt8LO1NGmL+TG3KnNky1p5GvJu4EL3ugPGd4MZCfyJ9TXZh5dAInkkOtJoF3 gHj2xWvrc6sUfWgQi9PrJVBOzAv1Hg3jZExsv+uNCFrBhA2Ta1XBUa/BWDAeANAN8G6vwzuxkGF 1Ni/+1w== X-Received: by 2002:a05:6000:400d:b0:4ab:3a26:31b7 with SMTP id ffacd0b85a97d-4b826d20b62mr39839152f8f.6.1796033693921; Mon, 30 Nov 2026 02:14:53 -0800 (PST) Received: from ?IPV6:2a01:e0a:ab7:2110:6a1d:efff:fe52:1959? ([2a01:e0a:ab7:2110:6a1d:efff:fe52:1959]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab65cb42absm63326102f8f.1.2026.11.30.02.14.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 02:14:53 -0800 (PST) Message-ID: Date: Mon, 30 Nov 2026 11:14:53 +0100 X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Reply-To: nicolas.dichtel@6wind.com Subject: Re: [PATCH ipsec v2 2/2] selftests: net: add a test for xfrm policy removal To: Ralf Lici , Steffen Klassert , Herbert Xu Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Shuah Khan , netdev@vger.kernel.org, ipsec@lists.linux.dev, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org References: <467acfb25f93252b01f9f096037ae248b8826a56.1795797791.git.ralf@mandelbit.com> From: Nicolas Dichtel Content-Language: en-US Organization: 6WIND In-Reply-To: <467acfb25f93252b01f9f096037ae248b8826a56.1795797791.git.ralf@mandelbit.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le 27/11/2026 à 17:48, Ralf Lici a écrit :> 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 > --- > tools/testing/selftests/net/Makefile | 1 + > tools/testing/selftests/net/config | 1 + > .../selftests/net/xfrm_policy_removal.sh | 212 ++++++++++++++++++ > 3 files changed, 214 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..a5cd38091483 > --- /dev/null > +++ b/tools/testing/selftests/net/xfrm_policy_removal.sh > @@ -0,0 +1,212 @@ > +#!/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. > +setup() { > + local family=$1 > + local mode=$2 > + local plen=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 > + plen=64 > + 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 "$SRC/$plen" dev dummy0 nodad Putting nodad for IPv6 only would avoid a warning on IPv4. > + > + # 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 > + fi > + # The allow policy, which takes precedence for the test port. > + if [ "$mode" = expire ]; then > + limit=(limit time-hard 1) I wonder whether a 1-second delay could make the test flappy on a slow system. Maybe time-hard 3 with slowwait 10 would be more robust. Regards, Nicolas > + fi > + ip -n "$NS" xfrm policy add src "$SRC" dst "$DST" proto udp \ > + dport "$PORT" dir out priority 10 action allow "${limit[@]}" > +} > + > +# 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' > +} > + > +# 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 5 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 > + > + RET=0 > + > + if ! setup "$family" "$mode"; then > + log_test_skip "$name" > + cleanup_all_ns > + return > + fi > + > + start_sender > + if ! fails=$(send_datagrams 1) || [ "$fails" != 0 ]; then > + log_test_skip "$name" "connected UDP socket not usable" > + cleanup > + return > + fi > + esp=$(sa_packets) > + check_err "$esp" "the allow policy did not apply" > + > + remove_allow_policy "$mode" > + check_err $? "the allow policy was not removed" > + > + 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"