Skip to content

Commit 9a5e153

Browse files
committed
selftests: netfilter: test nat source port clash resolution interaction with tcp early demux
JIRA: https://issues.redhat.com/browse/RHEL-6151 Upstream Status: commit 117e149 commit 117e149 Author: Florian Westphal <fw@strlen.de> Date: Tue Aug 15 11:52:41 2023 +0200 selftests: netfilter: test nat source port clash resolution interaction with tcp early demux Test that nat engine resolves the source port clash and tcp packet is passed to the correct socket. While at it, get rid of the iperf3 dependency, just use socat for listener side too. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Florian Westphal <fwestpha@redhat.com>
1 parent 0d131ee commit 9a5e153

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

tools/testing/selftests/netfilter/nf_nat_edemux.sh

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ ret=0
1111
sfx=$(mktemp -u "XXXXXXXX")
1212
ns1="ns1-$sfx"
1313
ns2="ns2-$sfx"
14+
socatpid=0
1415

1516
cleanup()
1617
{
18+
[ $socatpid -gt 0 ] && kill $socatpid
1719
ip netns del $ns1
1820
ip netns del $ns2
1921
}
2022

21-
iperf3 -v > /dev/null 2>&1
23+
socat -h > /dev/null 2>&1
2224
if [ $? -ne 0 ];then
23-
echo "SKIP: Could not run test without iperf3"
25+
echo "SKIP: Could not run test without socat"
2426
exit $ksft_skip
2527
fi
2628

@@ -60,8 +62,8 @@ ip netns exec $ns2 ip link set up dev veth2
6062
ip netns exec $ns2 ip addr add 192.168.1.2/24 dev veth2
6163

6264
# Create a server in one namespace
63-
ip netns exec $ns1 iperf3 -s > /dev/null 2>&1 &
64-
iperfs=$!
65+
ip netns exec $ns1 socat -u TCP-LISTEN:5201,fork OPEN:/dev/null,wronly=1 &
66+
socatpid=$!
6567

6668
# Restrict source port to just one so we don't have to exhaust
6769
# all others.
@@ -83,17 +85,43 @@ sleep 1
8385
# ip daddr:dport will be rewritten to 192.168.1.1 5201
8486
# NAT must reallocate source port 10000 because
8587
# 192.168.1.2:10000 -> 192.168.1.1:5201 is already in use
86-
echo test | ip netns exec $ns2 socat -t 3 -u STDIN TCP:10.96.0.1:443 >/dev/null
88+
echo test | ip netns exec $ns2 socat -t 3 -u STDIN TCP:10.96.0.1:443,connect-timeout=3 >/dev/null
8789
ret=$?
8890

89-
kill $iperfs
90-
9191
# Check socat can connect to 10.96.0.1:443 (aka 192.168.1.1:5201).
9292
if [ $ret -eq 0 ]; then
9393
echo "PASS: socat can connect via NAT'd address"
9494
else
9595
echo "FAIL: socat cannot connect via NAT'd address"
96-
exit 1
9796
fi
9897

99-
exit 0
98+
# check sport clashres.
99+
ip netns exec $ns1 iptables -t nat -A PREROUTING -p tcp --dport 5202 -j REDIRECT --to-ports 5201
100+
ip netns exec $ns1 iptables -t nat -A PREROUTING -p tcp --dport 5203 -j REDIRECT --to-ports 5201
101+
102+
sleep 5 | ip netns exec $ns2 socat -t 5 -u STDIN TCP:192.168.1.1:5202,connect-timeout=5 >/dev/null &
103+
cpid1=$!
104+
sleep 1
105+
106+
# if connect succeeds, client closes instantly due to EOF on stdin.
107+
# if connect hangs, it will time out after 5s.
108+
echo | ip netns exec $ns2 socat -t 3 -u STDIN TCP:192.168.1.1:5203,connect-timeout=5 >/dev/null &
109+
cpid2=$!
110+
111+
time_then=$(date +%s)
112+
wait $cpid2
113+
rv=$?
114+
time_now=$(date +%s)
115+
116+
# Check how much time has elapsed, expectation is for
117+
# 'cpid2' to connect and then exit (and no connect delay).
118+
delta=$((time_now - time_then))
119+
120+
if [ $delta -lt 2 -a $rv -eq 0 ]; then
121+
echo "PASS: could connect to service via redirected ports"
122+
else
123+
echo "FAIL: socat cannot connect to service via redirect ($delta seconds elapsed, returned $rv)"
124+
ret=1
125+
fi
126+
127+
exit $ret

0 commit comments

Comments
 (0)