Commit a849e06c authored by Florian Westphal's avatar Florian Westphal Committed by Jakub Kicinski

selftests: netfilter: nft_synproxy.sh: move to lib.sh infra

use checktool helper where applicable.
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
Link: https://lore.kernel.org/r/20240418152744.15105-4-fw@strlen.deSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent cebb3522
#!/bin/bash #!/bin/bash
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
#
# Kselftest framework requirement - SKIP code is 4. source lib.sh
ksft_skip=4
ret=0
rnd=$(mktemp -u XXXXXXXX)
nsr="nsr-$rnd" # synproxy machine
ns1="ns1-$rnd" # iperf client
ns2="ns2-$rnd" # iperf server
checktool (){ ret=0
if ! $1 > /dev/null 2>&1; then
echo "SKIP: Could not $2"
exit $ksft_skip
fi
}
checktool "nft --version" "run test without nft tool" checktool "nft --version" "run test without nft tool"
checktool "ip -Version" "run test without ip tool"
checktool "iperf3 --version" "run test without iperf3" checktool "iperf3 --version" "run test without iperf3"
checktool "ip netns add $nsr" "create net namespace"
modprobe -q nf_conntrack setup_ns nsr ns1 ns2
ip netns add $ns1 modprobe -q nf_conntrack
ip netns add $ns2
cleanup() { cleanup() {
ip netns pids $ns1 | xargs kill 2>/dev/null ip netns pids "$ns1" | xargs kill 2>/dev/null
ip netns pids $ns2 | xargs kill 2>/dev/null ip netns pids "$ns2" | xargs kill 2>/dev/null
ip netns del $ns1
ip netns del $ns2
ip netns del $nsr cleanup_all_ns
} }
trap cleanup EXIT trap cleanup EXIT
ip link add veth0 netns $nsr type veth peer name eth0 netns $ns1 ip link add veth0 netns "$nsr" type veth peer name eth0 netns "$ns1"
ip link add veth1 netns $nsr type veth peer name eth0 netns $ns2 ip link add veth1 netns "$nsr" type veth peer name eth0 netns "$ns2"
for dev in lo veth0 veth1; do for dev in veth0 veth1; do
ip -net $nsr link set $dev up ip -net "$nsr" link set "$dev" up
done done
ip -net $nsr addr add 10.0.1.1/24 dev veth0 ip -net "$nsr" addr add 10.0.1.1/24 dev veth0
ip -net $nsr addr add 10.0.2.1/24 dev veth1 ip -net "$nsr" addr add 10.0.2.1/24 dev veth1
ip netns exec $nsr sysctl -q net.ipv4.conf.veth0.forwarding=1 ip netns exec "$nsr" sysctl -q net.ipv4.conf.veth0.forwarding=1
ip netns exec $nsr sysctl -q net.ipv4.conf.veth1.forwarding=1 ip netns exec "$nsr" sysctl -q net.ipv4.conf.veth1.forwarding=1
ip netns exec $nsr sysctl -q net.netfilter.nf_conntrack_tcp_loose=0 ip netns exec "$nsr" sysctl -q net.netfilter.nf_conntrack_tcp_loose=0
for n in $ns1 $ns2; do for n in $ns1 $ns2; do
ip -net $n link set lo up ip -net "$n" link set eth0 up
ip -net $n link set eth0 up
done done
ip -net $ns1 addr add 10.0.1.99/24 dev eth0 ip -net "$ns1" addr add 10.0.1.99/24 dev eth0
ip -net $ns2 addr add 10.0.2.99/24 dev eth0 ip -net "$ns2" addr add 10.0.2.99/24 dev eth0
ip -net $ns1 route add default via 10.0.1.1 ip -net "$ns1" route add default via 10.0.1.1
ip -net $ns2 route add default via 10.0.2.1 ip -net "$ns2" route add default via 10.0.2.1
# test basic connectivity # test basic connectivity
if ! ip netns exec $ns1 ping -c 1 -q 10.0.2.99 > /dev/null; then if ! ip netns exec "$ns1" ping -c 1 -q 10.0.2.99 > /dev/null; then
echo "ERROR: $ns1 cannot reach $ns2" 1>&2 echo "ERROR: $ns1 cannot reach $ns2" 1>&2
exit 1 exit 1
fi fi
if ! ip netns exec $ns2 ping -c 1 -q 10.0.1.99 > /dev/null; then if ! ip netns exec "$ns2" ping -c 1 -q 10.0.1.99 > /dev/null; then
echo "ERROR: $ns2 cannot reach $ns1" 1>&2 echo "ERROR: $ns2 cannot reach $ns1" 1>&2
exit 1 exit 1
fi fi
ip netns exec $ns2 iperf3 -s > /dev/null 2>&1 & ip netns exec "$ns2" iperf3 -s > /dev/null 2>&1 &
# ip netns exec $nsr tcpdump -vvv -n -i veth1 tcp | head -n 10 & # ip netns exec $nsr tcpdump -vvv -n -i veth1 tcp | head -n 10 &
sleep 1 sleep 1
ip netns exec $nsr nft -f - <<EOF ip netns exec "$nsr" nft -f - <<EOF
table inet filter { table inet filter {
chain prerouting { chain prerouting {
type filter hook prerouting priority -300; policy accept; type filter hook prerouting priority -300; policy accept;
...@@ -104,12 +85,10 @@ if [ $? -ne 0 ]; then ...@@ -104,12 +85,10 @@ if [ $? -ne 0 ]; then
exit $ksft_skip exit $ksft_skip
fi fi
ip netns exec $ns1 timeout 5 iperf3 -c 10.0.2.99 -n $((1 * 1024 * 1024)) > /dev/null if ! ip netns exec "$ns1" timeout 5 iperf3 -c 10.0.2.99 -n $((1 * 1024 * 1024)) > /dev/null; then
if [ $? -ne 0 ]; then
echo "FAIL: iperf3 returned an error" 1>&2 echo "FAIL: iperf3 returned an error" 1>&2
ret=$? ret=1
ip netns exec $nsr nft list ruleset ip netns exec "$nsr" nft list ruleset
else else
echo "PASS: synproxy connection successful" echo "PASS: synproxy connection successful"
fi fi
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment