Skip to content

Commit aa8c63e

Browse files
committed
Add test cases that existed for iptables but not for nftables
Signed-off-by: Mario Loriedo <[email protected]>
1 parent 52aec26 commit aa8c63e

File tree

1 file changed

+127
-3
lines changed

1 file changed

+127
-3
lines changed

test/250-bridge-nftables.bats

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,15 @@ export NETAVARK_FW=nftables
147147
run_helper ps "$aardvark_pid"
148148
assert "${lines[1]}" =~ ".*aardvark-dns --config $NETAVARK_TMPDIR/config/aardvark-dns -p $dns_port run" "aardvark not running or bad options"
149149

150-
NETAVARK_DNS_PORT="$dns_port" run_netavark --file ${TESTSDIR}/testfiles/dualstack-bridge-network-container-dns-server.json \
150+
# Use run_helper instead of run_netavark here to check network namespace detection logic.
151+
# See https://github.com/containers/netavark/issues/911 for details.
152+
NETAVARK_DNS_PORT="$dns_port" run_helper $NETAVARK --config "$NETAVARK_TMPDIR/config" --rootless "$rootless" --file ${TESTSDIR}/testfiles/dualstack-bridge-network-container-dns-server.json \
151153
update podman1 --network-dns-servers 8.8.8.8
154+
assert "$output" = ""
155+
156+
# after update the pid should never change
157+
aardvark_pid2=$(cat "$NETAVARK_TMPDIR/config/aardvark-dns/aardvark.pid")
158+
assert "$aardvark_pid2" == "$aardvark_pid" "aardvark-dns pid after nv update"
152159

153160
# check aardvark config and running
154161
run_helper cat "$NETAVARK_TMPDIR/config/aardvark-dns/podman1"
@@ -271,7 +278,7 @@ export NETAVARK_FW=nftables
271278
}
272279

273280
@test "$fw_driver - bridge driver must generate config for aardvark with multiple custom dns server" {
274-
# get a random port directly to avoid low ports e.g. 53 would not create nftables
281+
# get a random port directly to avoid low ports e.g. 53 would not create nftables rules
275282
dns_port=$((RANDOM+10000))
276283

277284
NETAVARK_DNS_PORT="$dns_port" run_netavark --file ${TESTSDIR}/testfiles/dualstack-bridge-multiple-custom-dns-server.json \
@@ -351,6 +358,94 @@ export NETAVARK_FW=nftables
351358
expected_rc=1 run_helper ps "$aardvark_pid"
352359
}
353360

361+
@test "$fw_driver - dns with default drop policy" {
362+
run_netavark --file ${TESTSDIR}/testfiles/dualstack-bridge.json \
363+
setup $(get_container_netns_path)
364+
365+
run_in_host_netns nft add chain inet netavark INPUT \{ type filter hook input priority 0 \; policy drop \; \}
366+
run_in_host_netns nft add rule inet netavark INPUT ct state related,established accept
367+
run_in_host_netns nft add rule inet netavark INPUT meta l4proto ipv6-icmp accept # allow ICMPv6, required for DNS resolution
368+
369+
# check aardvark config and running
370+
run_helper cat "$NETAVARK_TMPDIR/config/aardvark-dns/podman1"
371+
assert "${lines[0]}" =~ "10.89.3.1,fd10:88:a::1" "aardvark set to listen to all IPs"
372+
assert "${lines[1]}" =~ "^[0-9a-f]{64} 10.89.3.2 fd10:88:a::2 somename$" "aardvark config's container"
373+
assert "${#lines[@]}" = 2 "too many lines in aardvark config"
374+
375+
aardvark_pid=$(cat "$NETAVARK_TMPDIR/config/aardvark-dns/aardvark.pid")
376+
assert "$ardvark_pid" =~ "[0-9]*" "aardvark pid not found"
377+
run_helper ps "$aardvark_pid"
378+
assert "${lines[1]}" =~ ".*aardvark-dns --config $NETAVARK_TMPDIR/config/aardvark-dns -p 53 run" "aardvark not running or bad options"
379+
380+
# test redirection actually works
381+
run_in_container_netns dig +short "somename.dns.podman" @10.89.3.1 A "somename.dns.podman" @10.89.3.1 AAAA
382+
assert "${lines[0]}" =~ "10.89.3.2" "ipv4 dns resolution works 1/2"
383+
assert "${lines[1]}" =~ "fd10:88:a::2" "ipv6 dns resolution works 2/2"
384+
385+
run_in_container_netns dig +short "somename.dns.podman" @fd10:88:a::1
386+
assert "${lines[0]}" =~ "10.89.3.2" "ipv6 dns resolution works"
387+
388+
run_netavark --file ${TESTSDIR}/testfiles/dualstack-bridge.json \
389+
teardown $(get_container_netns_path)
390+
391+
# check nftables rules were removed
392+
run_in_host_netns nft list chain inet netavark NETAVARK-HOSTPORT-DNAT
393+
assert "${#lines[@]}" = 4 "too many v4 NETAVARK_HOSTPORT-DNAT rules after teardown"
394+
395+
# check aardvark config got cleared, process killed
396+
expected_rc=2 run_helper ls "$NETAVARK_TMPDIR/config/aardvark-dns/podman1"
397+
expected_rc=1 run_helper ps "$aardvark_pid"
398+
}
399+
400+
@test "$fw_driver - dns with default drop policy with non-default dns port" {
401+
# get a random port
402+
dns_port=$((RANDOM+10000))
403+
404+
NETAVARK_DNS_PORT="$dns_port" run_netavark --file ${TESTSDIR}/testfiles/dualstack-bridge.json \
405+
setup $(get_container_netns_path)
406+
407+
# check that random DNS port was added to nftables rules
408+
run_in_host_netns nft list chain inet netavark NETAVARK-HOSTPORT-DNAT
409+
assert "${lines[2]}" =~ "ip6 daddr fd10:88:a::1 meta l4proto \{ tcp, udp \} th dport 53 dnat ip6 to \[fd10:88:a::1\]:$dns_port" "DNS forward rule ip6"
410+
assert "${lines[3]}" =~ "ip daddr 10.89.3.1 meta l4proto \{ tcp, udp \} th dport 53 dnat ip to 10.89.3.1:$dns_port" "DNS forward rule ip4"
411+
412+
run_in_host_netns nft add chain inet netavark INPUT \{ type filter hook input priority 0 \; policy drop \; \}
413+
run_in_host_netns nft add rule inet netavark INPUT ip saddr 10.89.3.0/24 meta l4proto \{ tcp, udp \} th dport $dns_port accept
414+
run_in_host_netns nft add rule inet netavark INPUT ip6 saddr fd10:88:a::/64 meta l4proto \{ tcp, udp \} th dport $dns_port accept
415+
run_in_host_netns nft add rule inet netavark INPUT ct state related,established accept
416+
run_in_host_netns nft add rule inet netavark INPUT meta l4proto ipv6-icmp accept # allow ICMPv6, required for DNS resolution
417+
418+
# check aardvark config and running
419+
run_helper cat "$NETAVARK_TMPDIR/config/aardvark-dns/podman1"
420+
assert "${lines[0]}" =~ "10.89.3.1,fd10:88:a::1" "aardvark set to listen to all IPs"
421+
assert "${lines[1]}" =~ "^[0-9a-f]{64} 10.89.3.2 fd10:88:a::2 somename$" "aardvark config's container"
422+
assert "${#lines[@]}" = 2 "too many lines in aardvark config"
423+
424+
aardvark_pid=$(cat "$NETAVARK_TMPDIR/config/aardvark-dns/aardvark.pid")
425+
assert "$ardvark_pid" =~ "[0-9]*" "aardvark pid not found"
426+
run_helper ps "$aardvark_pid"
427+
assert "${lines[1]}" =~ ".*aardvark-dns --config $NETAVARK_TMPDIR/config/aardvark-dns -p $dns_port run" "aardvark not running or bad options"
428+
429+
# test redirection actually works
430+
run_in_container_netns dig +short "somename.dns.podman" @10.89.3.1 A "somename.dns.podman" @10.89.3.1 AAAA
431+
assert "${lines[0]}" =~ "10.89.3.2" "ipv4 dns resolution works 1/2"
432+
assert "${lines[1]}" =~ "fd10:88:a::2" "ipv6 dns resolution works 2/2"
433+
434+
run_in_container_netns dig +short "somename.dns.podman" @fd10:88:a::1
435+
assert "${lines[0]}" =~ "10.89.3.2" "ipv6 dns resolution works"
436+
437+
NETAVARK_DNS_PORT="$dns_port" run_netavark --file ${TESTSDIR}/testfiles/dualstack-bridge.json \
438+
teardown $(get_container_netns_path)
439+
440+
# check nftables rules were removed
441+
run_in_host_netns nft list chain inet netavark NETAVARK-HOSTPORT-DNAT
442+
assert "${#lines[@]}" = 4 "too many v4 NETAVARK_HOSTPORT-DNAT rules after teardown"
443+
444+
# check aardvark config got cleared, process killed
445+
expected_rc=2 run_helper ls "$NETAVARK_TMPDIR/config/aardvark-dns/podman1"
446+
expected_rc=1 run_helper ps "$aardvark_pid"
447+
}
448+
354449
@test "$fw_driver - check error message from netns thread" {
355450
# create interface in netns to force error
356451
run_in_container_netns ip link add eth0 type dummy
@@ -477,6 +572,35 @@ export NETAVARK_FW=nftables
477572
test_port_fw hostip="127.0.0.1"
478573
}
479574

575+
# Test that port forwarding works with strict Reverse Path Forwarding enabled on the host
576+
@test "$fw_driver - port forwarding with two networks and RPF - tcp" {
577+
# First, enable strict RPF on host/container ns.
578+
run_in_host_netns sysctl -w net.ipv4.conf.all.rp_filter=1
579+
run_in_host_netns sysctl -w net.ipv4.conf.default.rp_filter=1
580+
run_in_container_netns sysctl -w net.ipv4.conf.all.rp_filter=1
581+
run_in_container_netns sysctl -w net.ipv4.conf.default.rp_filter=1
582+
583+
# We need a dummy interface with a host ip,
584+
# if we connect directly to the bridge ip it doesn't reproduce.
585+
add_dummy_interface_on_host dummy0 "10.0.0.1/24"
586+
587+
run_netavark --file ${TESTSDIR}/testfiles/two-networks.json setup $(get_container_netns_path)
588+
result="$output"
589+
590+
run_in_host_netns cat /proc/sys/net/ipv4/conf/podman2/rp_filter
591+
assert "2" "rp_filter podman2 bridge"
592+
run_in_host_netns cat /proc/sys/net/ipv4/conf/podman3/rp_filter
593+
assert "2" "rp_filter podman3 bridge"
594+
595+
run_in_container_netns cat /proc/sys/net/ipv4/conf/eth0/rp_filter
596+
assert "2" "rp_filter eth0 interface"
597+
run_in_container_netns cat /proc/sys/net/ipv4/conf/eth1/rp_filter
598+
assert "2" "rp_filter eth1 interface"
599+
600+
# Important: Use the "host" ip here and not localhost or bridge ip.
601+
run_connection_test "0" "tcp" 8080 "10.0.0.1" 8080
602+
}
603+
480604
@test "bridge ipam none" {
481605
read -r -d '\0' config <<EOF
482606
{
@@ -985,6 +1109,7 @@ net/ipv4/conf/podman1/rp_filter = 2"
9851109
@test "$fw_driver - port forwarding ipv4 - tcp with firewalld reload" {
9861110
test_port_fw firewalld_reload=true
9871111
}
1112+
9881113
@test "$fw_driver - test firewall-reload" {
9891114
# setup a simple bridge network
9901115
run_netavark --file ${TESTSDIR}/testfiles/simplebridge.json setup $(get_container_netns_path)
@@ -1015,7 +1140,6 @@ net/ipv4/conf/podman1/rp_filter = 2"
10151140
run_netavark --file ${TESTSDIR}/testfiles/simplebridge.json teardown $(get_container_netns_path)
10161141
}
10171142

1018-
10191143
function check_simple_bridge_nftables() {
10201144
# check nftables POSTROUTING chain
10211145
run_in_host_netns nft list chain inet netavark POSTROUTING

0 commit comments

Comments
 (0)