tests: shell: Add a test case for FTP helper combined with NAT.
PATCH 1/1 tests: shell: Add a test case for FTP helper combined with NAT.
This test verifies functionality of the FTP helper, for both passive, active FTP modes,
and the functionality of the nf_nat_ftp module.
--- tests/shell/features/curl.sh | 4 + tests/shell/features/tcpdump.sh | 4 + tests/shell/features/vsftpd.sh | 4 + tests/shell/testcases/packetpath/ftp_nat | 164 +++++++++++++++++++++++ tests/shell/testcases/packetpath/nat_ftp | 164 +++++++++++++++++++++++ 5 files changed, 340 insertions(+) create mode 100755 tests/shell/features/curl.sh create mode 100755 tests/shell/features/tcpdump.sh create mode 100755 tests/shell/features/vsftpd.sh create mode 100755 tests/shell/testcases/packetpath/ftp_nat create mode 100755 tests/shell/testcases/packetpath/nat_ftp
tests/shell/features/curl.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether curl is installed +curl -h >/dev/null 2>&1
tests/shell/features/tcpdump.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether tcpdump is installed +tcpdump -h >/dev/null 2>&1
tests/shell/features/vsftpd.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether vsftpd is installed +which vsftpd >/dev/null 2>&1
tests/shell/testcases/packetpath/ftp_nat new file +164 -0
164 lines not on this page · raw message
tests/shell/testcases/packetpath/nat_ftp new file +164 -0
164 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v2 1/1 tests: shell: Add a test case for FTP helper combined with NAT.
This test verifies functionality of the FTP helper, for both passive, active FTP modes,
and the functionality of the nf_nat_ftp module.
--- tests/shell/features/curl.sh | 4 + tests/shell/features/tcpdump.sh | 4 + tests/shell/features/vsftpd.sh | 4 + tests/shell/testcases/packetpath/nat_ftp | 164 +++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100755 tests/shell/features/curl.sh create mode 100755 tests/shell/features/tcpdump.sh create mode 100755 tests/shell/features/vsftpd.sh create mode 100755 tests/shell/testcases/packetpath/nat_ftp
tests/shell/features/curl.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether curl is installed +curl -h >/dev/null 2>&1
tests/shell/features/tcpdump.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether tcpdump is installed +tcpdump -h >/dev/null 2>&1
tests/shell/features/vsftpd.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether vsftpd is installed +which vsftpd >/dev/null 2>&1
tests/shell/testcases/packetpath/nat_ftp new file +164 -0
164 lines not on this page · raw message
Signature · 1 line
2.49.0
Florian Westphal
maintainerQuoted 3 lines · Yi Chen
This test verifies functionality of the FTP helper, for both passive, active FTP modes,
and the functionality of thenf_nat_ftpmodule.
Thanks for this test case.
Some minor comments below.
tests/shell/features/tcpdump.sh line 4
Quoted 10 lines
tests/shell/features/tcpdump.sh new file +4
-0@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether tcpdump is installed +tcpdump -h >/dev/null 2>&1
Is tcpdump a requirement? AFAICS the dumps are only used as a debug aid when something goes wrong?
Quoted 1 line
+INFILE=$(mktemp -p
/var/ftp/pub/)
This directory might not be writeable.
Can you use a /tmp/ directory?
I suggest to do:
WORKDIR=$(mktemp -d)
mkdir "$WORKDIR/pub"
... and then place all files there.
tests/shell/features/tcpdump.sh
Quoted 3 lines
tests/shell/features/tcpdump.sh new file +3
-0+dd if=/dev/urandom of="$INFILE" bs=4096 count=1 2>/dev/null +chmod 755 $INFILE +assert_pass "Prepare the file for FTP transmission"
Including this one
... and this config:
tests/shell/features/tcpdump.sh
Quoted 13 lines
tests/shell/features/tcpdump.sh new file +13
-0+cat > ./vsftpd.conf <<-EOF +anonymous_enable=YES +local_enable=YES +connect_from_port_20=YES +listen=NO +listen_ipv6=YES +pam_service_name=vsftpd +background=YES +EOF +ip netns exec $S vsftpd ./vsftpd.conf +sleep 1 +ip netns exec $S ss -6ltnp | grep -q '*:21' +assert_pass "start vsftpd server"
So no files are created outside of /tmp.
tests/shell/features/tcpdump.sh
Quoted 7 lines
tests/shell/features/tcpdump.sh new file +7
-0+# test passive mode +reload_ruleset +ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${0##*/}.pcap 2> /dev/null & sleep 2 +ip netns exec $C curl -s --connect-timeout 5 ftp://[${ip_rc}]:2121${INFILE#/var/ftp} -o $OUTFILE +assert_pass "curl ftp passive mode " + +pkill tcpdump
Can you do this instead?:
Quoted 1 line
+ip netns exec $S tcpdump -q --immediate-mode -Ui
s_r-w ${0##*/}.pcap 2>/dev/null&
tcpdump_pid=$!
sleep 2
...
kill "$tcpdump_pid"
?
pkill will zap all tcpdump instances.
Since tests are executed in parallel, it might zap other tcpdump instances as well and not just the one spawned by this script.
Quoted 1 line
+tcpdump -nnr ${0##*/}.pcap src ${ip_rs} and dst ${ip_sr} 2>&1 |grep -q FTP
Not sure why the above is needed. Isn't the 'cmp' enough to see if the ftp xfer worked?
tests/shell/features/tcpdump.sh
Quoted 3 lines
tests/shell/features/tcpdump.sh new file +3
-0+assert_pass "assert FTP traffic NATed" + +cmp "$INFILE" "$OUTFILE"
... because if there is a problem with the helper, then the cmp ought to fail?
Quoted 1 line
+tcpdump -nnr ${0##*/}.pcap src ${ip_rs} and dst ${ip_sr} 2>&1 |grep -q FTP
Why tcpdump check is needed:
If the snat rule doesn't work, does this test still pass? "cmp file" pass doesn't mean the snat really happened.
Add tcpdump check to make sure NAT really happened.
Quoted 4 lines
tcpdump_pid=$!
sleep 2
...
kill "$tcpdump_pid"
Sure, will do.
Quoted 1 line
So no files are created outside of /tmp.
Sure, got this principal
Quoted 2 lines
+INFILE=$(mktemp -p
/var/ftp/pub/)
This directory might not be writeable.
Sure, vsftpd must support a custom file path. I will find the configuration.
Quoted 2 lines
Is tcpdump a requirement? AFAICS the dumps are only used as a debug aid when something goes wrong?
tcpdump is widely used in our LNST test cases. for example check if one packet got modified.
Is it bad to use in upstream tests? If you still feel strange, I can remove the tcpdump check.
What I care about most is whether the ruleset in the test is configured correctly.
One only needs to NAT the control connection — the data connection will be NATed automatically.
Of course, if there's a better ruleset that can exercise more kernel code, that would be even better.
Will send out the next version.
Quoted 95 lines · Florian Westphal
Quoted 3 lines · Yi Chen
This test verifies functionality of the FTP helper, for both passive, active FTP modes,
and the functionality of thenf_nat_ftpmodule.Thanks for this test case.
Some minor comments below.
tests/shell/features/tcpdump.sh line 4
Quoted 10 lines
tests/shell/features/tcpdump.sh new file +4
-0@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether tcpdump is installed +tcpdump -h >/dev/null 2>&1Is tcpdump a requirement? AFAICS the dumps are only used as a debug aid when something goes wrong?
Quoted 1 line
+INFILE=$(mktemp -p
/var/ftp/pub/)This directory might not be writeable.
Can you use a
/tmp/directory?I suggest to do:
WORKDIR=$(mktemp -d)
mkdir "$WORKDIR/pub"... and then place all files there.
tests/shell/features/tcpdump.sh
Quoted 3 lines
tests/shell/features/tcpdump.sh new file +3
-0+dd if=/dev/urandom of="$INFILE" bs=4096 count=1 2>/dev/null +chmod 755 $INFILE +assert_pass "Prepare the file for FTP transmission"Including this one
... and this config:
tests/shell/features/tcpdump.sh
Quoted 13 lines
tests/shell/features/tcpdump.sh new file +13
-0+cat > ./vsftpd.conf <<-EOF +anonymous_enable=YES +local_enable=YES +connect_from_port_20=YES +listen=NO +listen_ipv6=YES +pam_service_name=vsftpd +background=YES +EOF +ip netns exec $S vsftpd ./vsftpd.conf +sleep 1 +ip netns exec $S ss -6ltnp | grep -q '*:21' +assert_pass "start vsftpd server"So no files are created outside of /tmp.
tests/shell/features/tcpdump.sh
Quoted 7 lines
tests/shell/features/tcpdump.sh new file +7
-0+# test passive mode +reload_ruleset +ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${0##*/}.pcap 2> /dev/null & sleep 2 +ip netns exec $C curl -s --connect-timeout 5 ftp://[${ip_rc}]:2121${INFILE#/var/ftp} -o $OUTFILE +assert_pass "curl ftp passive mode " + +pkill tcpdumpCan you do this instead?:
Quoted 1 line
+ip netns exec $S tcpdump -q --immediate-mode -Ui
s_r-w ${0##*/}.pcap 2>/dev/null&tcpdump_pid=$!
sleep 2
...
kill "$tcpdump_pid"?
pkill will zap all tcpdump instances.
Since tests are executed in parallel, it might zap other tcpdump instances as well and not just the one spawned by this script.Quoted 1 line
+tcpdump -nnr ${0##*/}.pcap src ${ip_rs} and dst ${ip_sr} 2>&1 |grep -q FTP
Not sure why the above is needed. Isn't the 'cmp' enough to see if the ftp xfer worked?
tests/shell/features/tcpdump.sh
Quoted 3 lines
tests/shell/features/tcpdump.sh new file +3
-0+assert_pass "assert FTP traffic NATed" + +cmp "$INFILE" "$OUTFILE"... because if there is a problem with the helper, then the cmp ought to fail?
Florian Westphal
maintainerQuoted 6 lines · Yi Chen
Quoted 2 lines
Is tcpdump a requirement? AFAICS the dumps are only used as a debug aid when something goes wrong?
tcpdump is widely used in our LNST test cases. for example check if one packet got modified.
Is it bad to use in upstream tests? If you still feel strange, I can remove the tcpdump check.
No need, you can keep tcpdump around.
Quoted 4 lines
What I care about most is whether the ruleset in the test is configured correctly.
One only needs to NAT the control connection — the data connection will be NATed automatically.
I'd have expected that the ftp connection would fail in case there is a problem with NAT.
But if you prefer to also validate via tcpdump thats fine.
Thanks for v2, I will have a look.
PATCH v2 1/1 tests: shell: Add a test case for FTP helper combined with NAT.
In reply to Yi ChenThis test verifies functionality of the FTP helper, for both passive, active FTP modes,
and the functionality of the nf_nat_ftp module.
--- tests/shell/features/curl.sh | 4 + tests/shell/features/tcpdump.sh | 4 + tests/shell/features/vsftpd.sh | 4 + tests/shell/testcases/packetpath/nat_ftp | 174 +++++++++++++++++++++++ 4 files changed, 186 insertions(+) create mode 100755 tests/shell/features/curl.sh create mode 100755 tests/shell/features/tcpdump.sh create mode 100755 tests/shell/features/vsftpd.sh create mode 100755 tests/shell/testcases/packetpath/nat_ftp
tests/shell/features/curl.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether curl is installed +curl -h >/dev/null 2>&1
tests/shell/features/tcpdump.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether tcpdump is installed +tcpdump -h >/dev/null 2>&1
tests/shell/features/vsftpd.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh + +# check whether vsftpd is installed +which vsftpd >/dev/null 2>&1
tests/shell/testcases/packetpath/nat_ftp new file +174 -0
174 lines not on this page · raw message
Signature · 1 line
2.49.0
Florian Westphal
maintainerQuoted 3 lines · Yi Chen
This test verifies functionality of the FTP helper, for both passive, active FTP modes,
and the functionality of thenf_nat_ftpmodule.
Thanks, I had to apply this delta to make this work for me, can you check that it still passes on your end?
I guess nf_nat_ftp module is already loaded on
your system, its needed for all tests as the FTP server is on a different address than what the client connectects to.
The important changes are:
- load nf_nat_ftp early
- use ${PCAP} for last tcpdump too, local dir isn't writeable in my virtme-ng setup.
Rest is debugging aid/cosmetic. The curl feature check gets extended to skip in case curl exists but was built with no ftp support.
I removed -s flag from curl, this also removes the error messages, if any, which makes it harder to debug. Its fine to have more information available in case something goes wrong.
I now get:
I: [OK] 1/1 tests/shell/testcases/packetpath/nat_ftp
No need to resend unless you want to make further enhancements.
tests/shell/features/curl.sh +2 -2
@@ -1,4 +1,4 @@ #!/bin/sh -# check whether curl is installed -curl -h >/dev/null 2>&1 +# check whether curl is installed and supports ftp +curl --version | grep "^Protocols: "| grep -q " ftp"
tests/shell/testcases/packetpath/nat_ftp +13 -9
@@ -22,7 +22,10 @@ assert_pass() echo "FAIL: ${@}" ip netns exec $R nft list ruleset tcpdump -nnr ${PCAP} - ip netns exec $R cat /proc/net/nf_conntrack + test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack + ip netns exec $R conntrack -S + ip netns exec $R conntrack -L + ip netns exec $S ss -nitepal exit 1 else echo "PASS: ${@}"
@@ -43,6 +46,9 @@ PCAP="$WORKDIR/tcpdump.pcap" mkdir -p $WORKDIR assert_pass "mkdir $WORKDIR" +modprobe nf_nat_ftp +assert_pass "modprobe nf_nat_ftp. Needed for DNAT of data connection and active mode PORT change with SNAT" + ip_sr=2001:db8:ffff:22::1 ip_cr=2001:db8:ffff:21::2 ip_rs=2001:db8:ffff:22::fffe
@@ -86,7 +92,7 @@ reload_ruleset() chain PRE-dnat { type nat hook prerouting priority dstnat; policy accept; # Dnat the control connection, data connection will be automaticly NATed. - ip6 daddr ${ip_rc} ip6 nexthdr tcp tcp dport 2121 counter dnat ip6 to [${ip_sr}]:21 + ip6 daddr ${ip_rc} counter ip6 nexthdr tcp tcp dport 2121 counter dnat ip6 to [${ip_sr}]:21 } chain PRE-aftnat {
@@ -103,7 +109,7 @@ reload_ruleset() chain forward { type filter hook forward priority filter; policy drop; - ip6 daddr ${ip_sr} tcp dport 21 ct state new counter accept + ip6 daddr ${ip_sr} counter tcp dport 21 ct state new counter accept ip6 nexthdr tcp ct state established counter accept ip6 nexthdr tcp ct state related counter log accept }
@@ -142,7 +148,7 @@ reload_ruleset ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! sleep 1 -ip netns exec $C curl -s --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE +ip netns exec $C curl --no-progress-meter --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp passive mode " cmp "$INFILE" "$OUTFILE"
@@ -155,19 +161,17 @@ assert_pass "assert FTP traffic NATed" # test active mode reload_ruleset -modprobe nf_nat_ftp -assert_pass "modprobe nf_nat_ftp. Active mode need it to modify the client ip in PORT command under SNAT" -ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${0##*/}.pcap 2> /dev/null & +ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! -ip netns exec $C curl -s -P - --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE +ip netns exec $C curl --no-progress-meter -P - --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp active mode " cmp "$INFILE" "$OUTFILE" assert_pass "FTP Active mode: in and output files remain the same when FTP traffic passes through NAT." kill $pid; sync -tcpdump -nnr ${0##*/}.pcap src ${ip_rs} and dst ${ip_sr} 2>&1 |grep -q FTP +tcpdump -nnr ${PCAP} src ${ip_rs} and dst ${ip_sr} 2>&1 |grep -q FTP assert_pass "assert FTP traffic NATed" # trap calls cleanup
Indeed, both passive and active mode need to preload the nf_nat_ftp module. The patched script passed on my side too.
Thanks for fixing mistakes in the script!
Quoted 116 lines · Florian Westphal
Quoted 3 lines · Yi Chen
This test verifies functionality of the FTP helper, for both passive, active FTP modes,
and the functionality of thenf_nat_ftpmodule.Thanks, I had to apply this delta to make this work for me, can you check that it still passes on your end?
I guess
nf_nat_ftpmodule is already loaded on
your system, its needed for all tests as the FTP server is on a different address than what the client connectects to.The important changes are:
- loadnf_nat_ftpearly
- use ${PCAP} for last tcpdump too, local dir isn't writeable in my virtme-ng setup.Rest is debugging aid/cosmetic. The curl feature check gets extended to skip in case curl exists but was built with no ftp support.
I removed -s flag from curl, this also removes the error messages, if any, which makes it harder to debug. Its fine to have more information available in case something goes wrong.
I now get:
I: [OK] 1/1 tests/shell/testcases/packetpath/nat_ftpNo need to resend unless you want to make further enhancements.
tests/shell/features/curl.sh +2
-2@@ -1,4 +1,4 @@ #!/bin/sh -# check whether curl is installed -curl -h >/dev/null 2>&1 +# check whether curl is installed and supports ftp +curl --version | grep "^Protocols: "| grep -q " ftp"tests/shell/testcases/packetpath/nat_ftp +13
-9@@ -22,7 +22,10 @@ assert_pass() echo "FAIL: ${@}" ip netns exec $R nft list ruleset tcpdump -nnr ${PCAP} - ip netns exec $R cat /proc/net/nf_conntrack + test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack + ip netns exec $R conntrack -S + ip netns exec $R conntrack -L + ip netns exec $S ss -nitepal exit 1 else echo "PASS: ${@}"@@ -43,6 +46,9 @@ PCAP="$WORKDIR/tcpdump.pcap" mkdir -p $WORKDIR assert_pass "mkdir $WORKDIR" +modprobe nf_nat_ftp +assert_pass "modprobe nf_nat_ftp. Needed for DNAT of data connection and active mode PORT change with SNAT" + ip_sr=2001:db8:ffff:22::1 ip_cr=2001:db8:ffff:21::2 ip_rs=2001:db8:ffff:22::fffe@@ -86,7 +92,7 @@ reload_ruleset() chain PRE-dnat { type nat hook prerouting priority dstnat; policy accept; # Dnat the control connection, data connection will be automaticly NATed. - ip6 daddr ${ip_rc} ip6 nexthdr tcp tcp dport 2121 counter dnat ip6 to [${ip_sr}]:21 + ip6 daddr ${ip_rc} counter ip6 nexthdr tcp tcp dport 2121 counter dnat ip6 to [${ip_sr}]:21 } chain PRE-aftnat {@@ -103,7 +109,7 @@ reload_ruleset() chain forward { type filter hook forward priority filter; policy drop; - ip6 daddr ${ip_sr} tcp dport 21 ct state new counter accept + ip6 daddr ${ip_sr} counter tcp dport 21 ct state new counter accept ip6 nexthdr tcp ct state established counter accept ip6 nexthdr tcp ct state related counter log accept }@@ -142,7 +148,7 @@ reload_ruleset ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! sleep 1 -ip netns exec $C curl -s --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE +ip netns exec $C curl --no-progress-meter --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp passive mode " cmp "$INFILE" "$OUTFILE"@@ -155,19 +161,17 @@ assert_pass "assert FTP traffic NATed" # test active mode reload_ruleset -modprobe nf_nat_ftp -assert_pass "modprobe nf_nat_ftp. Active mode need it to modify the client ip in PORT command under SNAT" -ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${0##*/}.pcap 2> /dev/null & +ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! -ip netns exec $C curl -s -P - --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE +ip netns exec $C curl --no-progress-meter -P - --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp active mode " cmp "$INFILE" "$OUTFILE" assert_pass "FTP Active mode: in and output files remain the same when FTP traffic passes through NAT." kill $pid; sync -tcpdump -nnr ${0##*/}.pcap src ${ip_rs} and dst ${ip_sr} 2>&1 |grep -q FTP +tcpdump -nnr ${PCAP} src ${ip_rs} and dst ${ip_sr} 2>&1 |grep -q FTP assert_pass "assert FTP traffic NATed" # trap calls cleanup
Florian Westphal
maintainerQuoted 2 lines · Yi Chen
Indeed, both passive and active mode need to preload the
nf_nat_ftpmodule. The patched script passed on my side too.
Thanks for double-checking, I've pushed the updated script out.