Commit 954398b4 authored by Florian Westphal's avatar Florian Westphal Committed by Jakub Kicinski

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

swap test for "ip" with "conntrack", former is already accounted for
via setup_ns helper.  Also switch to bash.
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
Link: https://lore.kernel.org/r/20240411233624.8129-8-fw@strlen.deSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 9785517a
#!/bin/sh #!/bin/bash
# This script demonstrates interaction of conntrack and vrf. # This script demonstrates interaction of conntrack and vrf.
# The vrf driver calls the netfilter hooks again, with oif/iif # The vrf driver calls the netfilter hooks again, with oif/iif
...@@ -28,84 +28,65 @@ ...@@ -28,84 +28,65 @@
# that was supposed to be fixed by the commit mentioned above to make sure # that was supposed to be fixed by the commit mentioned above to make sure
# that any fix to test case 1 won't break masquerade again. # that any fix to test case 1 won't break masquerade again.
ksft_skip=4 source lib.sh
IP0=172.30.30.1 IP0=172.30.30.1
IP1=172.30.30.2 IP1=172.30.30.2
PFXL=30 PFXL=30
ret=0 ret=0
sfx=$(mktemp -u "XXXXXXXX")
ns0="ns0-$sfx"
ns1="ns1-$sfx"
cleanup() cleanup()
{ {
ip netns pids $ns0 | xargs kill 2>/dev/null ip netns pids $ns0 | xargs kill 2>/dev/null
ip netns pids $ns1 | xargs kill 2>/dev/null ip netns pids $ns1 | xargs kill 2>/dev/null
ip netns del $ns0 $ns1 cleanup_all_ns
} }
nft --version > /dev/null 2>&1 if ! nft --version > /dev/null 2>&1;then
if [ $? -ne 0 ];then
echo "SKIP: Could not run test without nft tool" echo "SKIP: Could not run test without nft tool"
exit $ksft_skip exit $ksft_skip
fi fi
ip -Version > /dev/null 2>&1 if ! conntrack --version > /dev/null 2>&1;then
if [ $? -ne 0 ];then echo "SKIP: Could not run test without conntrack tool"
echo "SKIP: Could not run test without ip tool"
exit $ksft_skip
fi
ip netns add "$ns0"
if [ $? -ne 0 ];then
echo "SKIP: Could not create net namespace $ns0"
exit $ksft_skip exit $ksft_skip
fi fi
ip netns add "$ns1"
trap cleanup EXIT trap cleanup EXIT
ip netns exec $ns0 sysctl -q -w net.ipv4.conf.default.rp_filter=0 setup_ns ns0 ns1
ip netns exec $ns0 sysctl -q -w net.ipv4.conf.all.rp_filter=0
ip netns exec $ns0 sysctl -q -w net.ipv4.conf.all.rp_filter=0
ip link add veth0 netns "$ns0" type veth peer name veth0 netns "$ns1" > /dev/null 2>&1 ip netns exec "$ns0" sysctl -q -w net.ipv4.conf.default.rp_filter=0
if [ $? -ne 0 ];then ip netns exec "$ns0" sysctl -q -w net.ipv4.conf.all.rp_filter=0
ip netns exec "$ns0" sysctl -q -w net.ipv4.conf.all.rp_filter=0
if ! ip link add veth0 netns "$ns0" type veth peer name veth0 netns "$ns1" > /dev/null 2>&1; then
echo "SKIP: Could not add veth device" echo "SKIP: Could not add veth device"
exit $ksft_skip exit $ksft_skip
fi fi
ip -net $ns0 li add tvrf type vrf table 9876 if ! ip -net "$ns0" li add tvrf type vrf table 9876; then
if [ $? -ne 0 ];then
echo "SKIP: Could not add vrf device" echo "SKIP: Could not add vrf device"
exit $ksft_skip exit $ksft_skip
fi fi
ip -net $ns0 li set lo up ip -net "$ns0" li set veth0 master tvrf
ip -net "$ns0" li set tvrf up
ip -net $ns0 li set veth0 master tvrf ip -net "$ns0" li set veth0 up
ip -net $ns0 li set tvrf up ip -net "$ns1" li set veth0 up
ip -net $ns0 li set veth0 up
ip -net $ns1 li set veth0 up
ip -net $ns0 addr add $IP0/$PFXL dev veth0 ip -net "$ns0" addr add $IP0/$PFXL dev veth0
ip -net $ns1 addr add $IP1/$PFXL dev veth0 ip -net "$ns1" addr add $IP1/$PFXL dev veth0
ip netns exec $ns1 iperf3 -s > /dev/null 2>&1& ip netns exec "$ns1" iperf3 -s > /dev/null 2>&1 &
if [ $? -ne 0 ];then
echo "SKIP: Could not start iperf3"
exit $ksft_skip
fi
# test vrf ingress handling. # test vrf ingress handling.
# The incoming connection should be placed in conntrack zone 1, # The incoming connection should be placed in conntrack zone 1,
# as decided by the first iteration of the ruleset. # as decided by the first iteration of the ruleset.
test_ct_zone_in() test_ct_zone_in()
{ {
ip netns exec $ns0 nft -f - <<EOF ip netns exec "$ns0" nft -f - <<EOF
table testct { table testct {
chain rawpre { chain rawpre {
type filter hook prerouting priority raw; type filter hook prerouting priority raw;
...@@ -126,21 +107,21 @@ table testct { ...@@ -126,21 +107,21 @@ table testct {
} }
} }
EOF EOF
ip netns exec $ns1 ping -W 1 -c 1 -I veth0 $IP0 > /dev/null ip netns exec "$ns1" ping -W 1 -c 1 -I veth0 "$IP0" > /dev/null
# should be in zone 1, not zone 2 # should be in zone 1, not zone 2
count=$(ip netns exec $ns0 conntrack -L -s $IP1 -d $IP0 -p icmp --zone 1 2>/dev/null | wc -l) count=$(ip netns exec "$ns0" conntrack -L -s $IP1 -d $IP0 -p icmp --zone 1 2>/dev/null | wc -l)
if [ $count -eq 1 ]; then if [ "$count" -eq 1 ]; then
echo "PASS: entry found in conntrack zone 1" echo "PASS: entry found in conntrack zone 1"
else else
echo "FAIL: entry not found in conntrack zone 1" echo "FAIL: entry not found in conntrack zone 1"
count=$(ip netns exec $ns0 conntrack -L -s $IP1 -d $IP0 -p icmp --zone 2 2> /dev/null | wc -l) count=$(ip netns exec "$ns0" conntrack -L -s $IP1 -d $IP0 -p icmp --zone 2 2> /dev/null | wc -l)
if [ $count -eq 1 ]; then if [ "$count" -eq 1 ]; then
echo "FAIL: entry found in zone 2 instead" echo "FAIL: entry found in zone 2 instead"
else else
echo "FAIL: entry not in zone 1 or 2, dumping table" echo "FAIL: entry not in zone 1 or 2, dumping table"
ip netns exec $ns0 conntrack -L ip netns exec "$ns0" conntrack -L
ip netns exec $ns0 nft list ruleset ip netns exec "$ns0" nft list ruleset
fi fi
fi fi
} }
...@@ -153,12 +134,12 @@ test_masquerade_vrf() ...@@ -153,12 +134,12 @@ test_masquerade_vrf()
local qdisc=$1 local qdisc=$1
if [ "$qdisc" != "default" ]; then if [ "$qdisc" != "default" ]; then
tc -net $ns0 qdisc add dev tvrf root $qdisc tc -net "$ns0" qdisc add dev tvrf root "$qdisc"
fi fi
ip netns exec $ns0 conntrack -F 2>/dev/null ip netns exec "$ns0" conntrack -F 2>/dev/null
ip netns exec $ns0 nft -f - <<EOF ip netns exec "$ns0" nft -f - <<EOF
flush ruleset flush ruleset
table ip nat { table ip nat {
chain rawout { chain rawout {
...@@ -179,17 +160,15 @@ table ip nat { ...@@ -179,17 +160,15 @@ table ip nat {
} }
} }
EOF EOF
ip netns exec $ns0 ip vrf exec tvrf iperf3 -t 1 -c $IP1 >/dev/null if ! ip netns exec "$ns0" ip vrf exec tvrf iperf3 -t 1 -c $IP1 >/dev/null; then
if [ $? -ne 0 ]; then
echo "FAIL: iperf3 connect failure with masquerade + sport rewrite on vrf device" echo "FAIL: iperf3 connect failure with masquerade + sport rewrite on vrf device"
ret=1 ret=1
return return
fi fi
# must also check that nat table was evaluated on second (lower device) iteration. # must also check that nat table was evaluated on second (lower device) iteration.
ip netns exec $ns0 nft list table ip nat |grep -q 'counter packets 2' && ip netns exec "$ns0" nft list table ip nat |grep -q 'counter packets 2' &&
ip netns exec $ns0 nft list table ip nat |grep -q 'untracked counter packets [1-9]' if ip netns exec "$ns0" nft list table ip nat |grep -q 'untracked counter packets [1-9]'; then
if [ $? -eq 0 ]; then
echo "PASS: iperf3 connect with masquerade + sport rewrite on vrf device ($qdisc qdisc)" echo "PASS: iperf3 connect with masquerade + sport rewrite on vrf device ($qdisc qdisc)"
else else
echo "FAIL: vrf rules have unexpected counter value" echo "FAIL: vrf rules have unexpected counter value"
...@@ -197,7 +176,7 @@ EOF ...@@ -197,7 +176,7 @@ EOF
fi fi
if [ "$qdisc" != "default" ]; then if [ "$qdisc" != "default" ]; then
tc -net $ns0 qdisc del dev tvrf root tc -net "$ns0" qdisc del dev tvrf root
fi fi
} }
...@@ -206,8 +185,8 @@ EOF ...@@ -206,8 +185,8 @@ EOF
# oifname is the lower device (veth0 in this case). # oifname is the lower device (veth0 in this case).
test_masquerade_veth() test_masquerade_veth()
{ {
ip netns exec $ns0 conntrack -F 2>/dev/null ip netns exec "$ns0" conntrack -F 2>/dev/null
ip netns exec $ns0 nft -f - <<EOF ip netns exec "$ns0" nft -f - <<EOF
flush ruleset flush ruleset
table ip nat { table ip nat {
chain postrouting { chain postrouting {
...@@ -216,16 +195,14 @@ table ip nat { ...@@ -216,16 +195,14 @@ table ip nat {
} }
} }
EOF EOF
ip netns exec $ns0 ip vrf exec tvrf iperf3 -t 1 -c $IP1 > /dev/null if ! ip netns exec "$ns0" ip vrf exec tvrf iperf3 -t 1 -c $IP1 > /dev/null; then
if [ $? -ne 0 ]; then
echo "FAIL: iperf3 connect failure with masquerade + sport rewrite on veth device" echo "FAIL: iperf3 connect failure with masquerade + sport rewrite on veth device"
ret=1 ret=1
return return
fi fi
# must also check that nat table was evaluated on second (lower device) iteration. # must also check that nat table was evaluated on second (lower device) iteration.
ip netns exec $ns0 nft list table ip nat |grep -q 'counter packets 2' if ip netns exec "$ns0" nft list table ip nat |grep -q 'counter packets 2'; then
if [ $? -eq 0 ]; then
echo "PASS: iperf3 connect with masquerade + sport rewrite on veth device" echo "PASS: iperf3 connect with masquerade + sport rewrite on veth device"
else else
echo "FAIL: vrf masq rule has unexpected counter value" echo "FAIL: vrf masq rule has unexpected counter value"
......
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