Skip to content

Commit 7152303

Browse files
Florian Westphalummakynes
authored andcommitted
selftests: netfilter: add synproxy test
Simple test for synproxy feature, iperf3 should be intercepted by synproxy netns, but connection should still succeed. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 7db788a commit 7152303

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

tools/testing/selftests/netfilter/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TEST_PROGS := nft_trans_stress.sh nft_fib.sh nft_nat.sh bridge_brouter.sh \
66
nft_concat_range.sh nft_conntrack_helper.sh \
77
nft_queue.sh nft_meta.sh nf_nat_edemux.sh \
88
ipip-conntrack-mtu.sh conntrack_tcp_unreplied.sh \
9-
conntrack_vrf.sh
9+
conntrack_vrf.sh nft_synproxy.sh
1010

1111
LDLIBS = -lmnl
1212
TEST_GEN_FILES = nf-queue
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
5+
# Kselftest framework requirement - SKIP code is 4.
6+
ksft_skip=4
7+
ret=0
8+
9+
rnd=$(mktemp -u XXXXXXXX)
10+
nsr="nsr-$rnd" # synproxy machine
11+
ns1="ns1-$rnd" # iperf client
12+
ns2="ns2-$rnd" # iperf server
13+
14+
checktool (){
15+
if ! $1 > /dev/null 2>&1; then
16+
echo "SKIP: Could not $2"
17+
exit $ksft_skip
18+
fi
19+
}
20+
21+
checktool "nft --version" "run test without nft tool"
22+
checktool "ip -Version" "run test without ip tool"
23+
checktool "iperf3 --version" "run test without iperf3"
24+
checktool "ip netns add $nsr" "create net namespace"
25+
26+
ip netns add $ns1
27+
ip netns add $ns2
28+
29+
cleanup() {
30+
ip netns pids $ns1 | xargs kill 2>/dev/null
31+
ip netns pids $ns2 | xargs kill 2>/dev/null
32+
ip netns del $ns1
33+
ip netns del $ns2
34+
35+
ip netns del $nsr
36+
}
37+
38+
trap cleanup EXIT
39+
40+
ip link add veth0 netns $nsr type veth peer name eth0 netns $ns1
41+
ip link add veth1 netns $nsr type veth peer name eth0 netns $ns2
42+
43+
for dev in lo veth0 veth1; do
44+
ip -net $nsr link set $dev up
45+
done
46+
47+
ip -net $nsr addr add 10.0.1.1/24 dev veth0
48+
ip -net $nsr addr add 10.0.2.1/24 dev veth1
49+
50+
ip netns exec $nsr sysctl -q net.ipv4.conf.veth0.forwarding=1
51+
ip netns exec $nsr sysctl -q net.ipv4.conf.veth1.forwarding=1
52+
ip netns exec $nsr sysctl -q net.netfilter.nf_conntrack_tcp_loose=0
53+
54+
for n in $ns1 $ns2; do
55+
ip -net $n link set lo up
56+
ip -net $n link set eth0 up
57+
done
58+
ip -net $ns1 addr add 10.0.1.99/24 dev eth0
59+
ip -net $ns2 addr add 10.0.2.99/24 dev eth0
60+
ip -net $ns1 route add default via 10.0.1.1
61+
ip -net $ns2 route add default via 10.0.2.1
62+
63+
# test basic connectivity
64+
if ! ip netns exec $ns1 ping -c 1 -q 10.0.2.99 > /dev/null; then
65+
echo "ERROR: $ns1 cannot reach $ns2" 1>&2
66+
exit 1
67+
fi
68+
69+
if ! ip netns exec $ns2 ping -c 1 -q 10.0.1.99 > /dev/null; then
70+
echo "ERROR: $ns2 cannot reach $ns1" 1>&2
71+
exit 1
72+
fi
73+
74+
ip netns exec $ns2 iperf3 -s > /dev/null 2>&1 &
75+
# ip netns exec $nsr tcpdump -vvv -n -i veth1 tcp | head -n 10 &
76+
77+
sleep 1
78+
79+
ip netns exec $nsr nft -f - <<EOF
80+
table inet filter {
81+
chain prerouting {
82+
type filter hook prerouting priority -300; policy accept;
83+
meta iif veth0 tcp flags syn counter notrack
84+
}
85+
86+
chain forward {
87+
type filter hook forward priority 0; policy accept;
88+
89+
ct state new,established counter accept
90+
91+
meta iif veth0 meta l4proto tcp ct state untracked,invalid synproxy mss 1460 sack-perm timestamp
92+
93+
ct state invalid counter drop
94+
95+
# make ns2 unreachable w.o. tcp synproxy
96+
tcp flags syn counter drop
97+
}
98+
}
99+
EOF
100+
if [ $? -ne 0 ]; then
101+
echo "SKIP: Cannot add nft synproxy"
102+
exit $ksft_skip
103+
fi
104+
105+
ip netns exec $ns1 timeout 5 iperf3 -c 10.0.2.99 -n $((1 * 1024 * 1024)) > /dev/null
106+
107+
if [ $? -ne 0 ]; then
108+
echo "FAIL: iperf3 returned an error" 1>&2
109+
ret=$?
110+
ip netns exec $nsr nft list ruleset
111+
else
112+
echo "PASS: synproxy connection successful"
113+
fi
114+
115+
exit $ret

0 commit comments

Comments
 (0)