Skip to content

Commit 3c3c324

Browse files
Florian Westphalummakynes
authored andcommitted
selftests: netfilter: nft_nat.sh: add test for reverse clash with nat
This will fail without the previous bug fix because we erronously believe that the clashing entry went way. However, the clash exists in the opposite direction due to an existing nat mapping: PASS: IP statless for ns2-LgTIuS ERROR: failed to test udp ns1-x4iyOW to ns2-LgTIuS with dnat rule step 2, result: "" This is partially adapted from test instructions from the below ubuntu tracker. Link: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/2109889 Signed-off-by: Florian Westphal <[email protected]> Tested-by: Shaun Brady <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 50d9ce9 commit 3c3c324

File tree

1 file changed

+76
-5
lines changed

1 file changed

+76
-5
lines changed

tools/testing/selftests/net/netfilter/nft_nat.sh

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,24 @@ EOF
866866
ip netns exec "$ns0" nft delete table $family nat
867867
}
868868

869+
file_cmp()
870+
{
871+
local infile="$1"
872+
local outfile="$2"
873+
874+
if ! cmp "$infile" "$outfile";then
875+
echo -n "Infile "
876+
ls -l "$infile"
877+
echo -n "Outfile "
878+
ls -l "$outfile"
879+
echo "ERROR: in and output file mismatch when checking $msg" 1>&1
880+
ret=1
881+
return 1
882+
fi
883+
884+
return 0
885+
}
886+
869887
test_stateless_nat_ip()
870888
{
871889
local lret=0
@@ -966,11 +984,7 @@ EOF
966984

967985
wait
968986

969-
if ! cmp "$INFILE" "$OUTFILE";then
970-
ls -l "$INFILE" "$OUTFILE"
971-
echo "ERROR: in and output file mismatch when checking udp with stateless nat" 1>&2
972-
lret=1
973-
fi
987+
file_cmp "$INFILE" "$OUTFILE" "udp with stateless nat" || lret=1
974988

975989
:> "$OUTFILE"
976990

@@ -991,6 +1005,62 @@ EOF
9911005
return $lret
9921006
}
9931007

1008+
test_dnat_clash()
1009+
{
1010+
local lret=0
1011+
1012+
if ! socat -h > /dev/null 2>&1;then
1013+
echo "SKIP: Could not run dnat clash test without socat tool"
1014+
[ $ret -eq 0 ] && ret=$ksft_skip
1015+
return $ksft_skip
1016+
fi
1017+
1018+
ip netns exec "$ns0" nft -f /dev/stdin <<EOF
1019+
flush ruleset
1020+
table ip dnat-test {
1021+
chain prerouting {
1022+
type nat hook prerouting priority dstnat; policy accept;
1023+
ip daddr 10.0.2.1 udp dport 1234 counter dnat to 10.0.1.1:1234
1024+
}
1025+
}
1026+
EOF
1027+
if [ $? -ne 0 ]; then
1028+
echo "SKIP: Could not add dnat rules"
1029+
[ $ret -eq 0 ] && ret=$ksft_skip
1030+
return $ksft_skip
1031+
fi
1032+
1033+
local udpdaddr="10.0.2.1"
1034+
for i in 1 2;do
1035+
echo "PING $udpdaddr" > "$INFILE"
1036+
echo "PONG 10.0.1.1 step $i" | ip netns exec "$ns0" timeout 3 socat STDIO UDP4-LISTEN:1234,bind=10.0.1.1 > "$OUTFILE" 2>/dev/null &
1037+
local lpid=$!
1038+
1039+
busywait $BUSYWAIT_TIMEOUT listener_ready "$ns0" 1234 "-u"
1040+
1041+
result=$(ip netns exec "$ns1" timeout 3 socat STDIO UDP4-SENDTO:"$udpdaddr:1234,sourceport=4321" < "$INFILE")
1042+
udpdaddr="10.0.1.1"
1043+
1044+
if [ "$result" != "PONG 10.0.1.1 step $i" ] ; then
1045+
echo "ERROR: failed to test udp $ns1 to $ns2 with dnat rule step $i, result: \"$result\"" 1>&2
1046+
lret=1
1047+
ret=1
1048+
fi
1049+
1050+
wait
1051+
1052+
file_cmp "$INFILE" "$OUTFILE" "udp dnat step $i" || lret=1
1053+
1054+
:> "$OUTFILE"
1055+
done
1056+
1057+
test $lret -eq 0 && echo "PASS: IP dnat clash $ns1:$ns2"
1058+
1059+
ip netns exec "$ns0" nft flush ruleset
1060+
1061+
return $lret
1062+
}
1063+
9941064
# ip netns exec "$ns0" ping -c 1 -q 10.0.$i.99
9951065
for i in "$ns0" "$ns1" "$ns2" ;do
9961066
ip netns exec "$i" nft -f /dev/stdin <<EOF
@@ -1147,6 +1217,7 @@ $test_inet_nat && test_redirect6 inet
11471217

11481218
test_port_shadowing
11491219
test_stateless_nat_ip
1220+
test_dnat_clash
11501221

11511222
if [ $ret -ne 0 ];then
11521223
echo -n "FAIL: "

0 commit comments

Comments
 (0)