@@ -11,16 +11,18 @@ ret=0
1111sfx=$( mktemp -u " XXXXXXXX" )
1212ns1=" ns1-$sfx "
1313ns2=" ns2-$sfx "
14+ socatpid=0
1415
1516cleanup ()
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
2224if [ $? -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
2527fi
2628
@@ -60,8 +62,8 @@ ip netns exec $ns2 ip link set up dev veth2
6062ip 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
8789ret=$?
8890
89- kill $iperfs
90-
9191# Check socat can connect to 10.96.0.1:443 (aka 192.168.1.1:5201).
9292if [ $ret -eq 0 ]; then
9393 echo " PASS: socat can connect via NAT'd address"
9494else
9595 echo " FAIL: socat cannot connect via NAT'd address"
96- exit 1
9796fi
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