From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 409AE1FF0B3 for ; Thu, 26 Nov 2026 16:40:37 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 4240721838; Thu, 26 Nov 2026 16:39:43 +0100 (CET) From: Lukas Sichert To: pve-devel@lists.proxmox.com Subject: [PATCH network v3 6/7] fix #5066: zones: add test for generating and applying SNAT rules Date: Thu, 26 Nov 2026 16:29:04 +0100 Message-ID: <20261126153925.54600-7-l.sichert@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261126153925.54600-1-l.sichert@proxmox.com> References: <20261126153925.54600-1-l.sichert@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795707577426 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.409 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 5CZ4ZLBXE43V6DWHHZFMP5VBAQSIGB7A X-Message-ID-Hash: 5CZ4ZLBXE43V6DWHHZFMP5VBAQSIGB7A X-MailFrom: l.sichert@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Lukas Sichert X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The zone tests only compare the generated interfaces file, so nothing checks how the post-up hooks and the activation of the prepared rules work together. Run both against a fake iptables and check the rules after the first apply, after applying again, and after SNAT was disabled for all subnets. This includes zones with a dedicated VRF, which masquerade on each of their uplinks. Signed-off-by: Lukas Sichert Link: https://bugzilla.proxmox.com/show_bug.cgi?id=5066 --- Notes: new in v3 src/test/Makefile | 5 +- src/test/run_test_apply_snat.pl | 209 ++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+), 1 deletion(-) create mode 100755 src/test/run_test_apply_snat.pl diff --git a/src/test/Makefile b/src/test/Makefile index d9160f3..adb8866 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -1,6 +1,6 @@ all: test -test: test_zones test_zone_host_vrf test_static_routes test_ct_zones test_fabric_host_vrf test_fabric_zone_wireguard test_ipams test_dns test_subnets test_vnets_blackbox test_running_config test_api_index +test: test_zones test_zone_host_vrf test_static_routes test_ct_zones test_fabric_host_vrf test_fabric_zone_wireguard test_ipams test_dns test_subnets test_vnets_blackbox test_running_config test_api_index test_apply_snat test_zones: run_test_zones.pl ./run_test_zones.pl @@ -38,4 +38,7 @@ test_running_config: run_test_running_config.pl test_api_index: run_test_api_index.pl ./run_test_api_index.pl +test_apply_snat: run_test_apply_snat.pl + ./run_test_apply_snat.pl + clean: diff --git a/src/test/run_test_apply_snat.pl b/src/test/run_test_apply_snat.pl new file mode 100755 index 0000000..d69c4fd --- /dev/null +++ b/src/test/run_test_apply_snat.pl @@ -0,0 +1,209 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use lib qw(..); + +use Net::IP; +use Test::More; +use Test::MockModule; +use Test::Differences; +use Text::ParseWords qw(shellwords); + +use PVE::Network::SDN; +use PVE::Network::SDN::Zones; +use PVE::INotify; + +sub read_sdn_config { + my ($file) = @_; + + # Read structure back in again + open my $in, '<', $file or die $!; + my $sdn_config; + { + local $/; # slurp mode + $sdn_config = eval <$in>; + } + close $in; + + return $sdn_config; +} + +# state of the mocked iptables: $tables->{$bin}->{$table}->{$chain} = [rules] +my $tables; + +sub mock_run_command { + my ($cmd) = @_; + + # hooks run through a shell, which handles the redirection + my @args = grep { $_ ne '2>/dev/null' } @$cmd; + + my ($bin, undef, $table, $op, $chain, @rule) = @args; + my $chains = $tables->{$bin}->{$table}; + my $rules = $chains->{$chain}; + my $rule = "@rule"; + + die "no chain/target/match by that name\n" if !$rules && $op ne '-N'; + + if ($op eq '-N') { + die "chain already exists\n" if $rules; + $chains->{$chain} = []; + } elsif ($op eq '-A') { + push @$rules, $rule; + } elsif ($op eq '-I') { + unshift @$rules, $rule; + } elsif ($op eq '-C' || $op eq '-D') { + my ($idx) = grep { $rules->[$_] eq $rule } 0 .. $#$rules; + die "no matching rule\n" if !defined($idx); + splice(@$rules, $idx, 1) if $op eq '-D'; + } elsif ($op eq '-F') { + @$rules = (); + } elsif ($op eq '-X') { + die "chain is not empty or still in use\n" + if @$rules || grep { $_ eq "-j $chain" } map { @$_ } values %$chains; + delete $chains->{$chain}; + } elsif ($op eq '-E') { + $chains->{$rule} = delete $chains->{$chain}; + for my $list (values %$chains) { + s/^-j \Q$chain\E$/-j $rule/ for @$list; + } + } elsif ($op ne '-L' && $op ne '-S') { + die "unsupported operation $op\n"; + } +} + +# runs the iptables post-up hooks like ifupdown2 does, 'a || b' only runs b if a failed +sub split_post_up_hooks { + my ($interfaces) = @_; + + for my $line (split(/\n/, $interfaces)) { + next if $line !~ m/^\s*post-up (ip6?tables .*)/; + + my ($cmd_a, $cmd_b) = split(/ \|\| /, $1, 2); + eval { mock_run_command([shellwords($cmd_a)]) }; + mock_run_command([shellwords($cmd_b)]) if $@ && defined($cmd_b); + die $@ if $@ && !defined($cmd_b); + } +} + +sub get_iptables { + my %state; + + foreach my $bin (qw(iptables ip6tables)) { + my $nat = $tables->{$bin}->{nat}; + $state{$bin} = { + chains => [sort grep { /^PROXMOX/ } keys %$nat], + jumps => $nat->{POSTROUTING}, + snat => [sort map { s/^-s (\S+) .*/$1/r } @{ $nat->{'PROXMOX-SDN'} // [] }], + ct_rules => scalar(@{ $tables->{$bin}->{raw}->{PREROUTING} }), + }; + } + + return \%state; +} + +my $mocked_zones; +$mocked_zones = Test::MockModule->new('PVE::Network::SDN::Zones'); +$mocked_zones->mock(run_command => \&mock_run_command); + +my @tests = sort glob('./zones/*/*snat'); + +foreach my $name (@tests) { + + my $sdn_config = read_sdn_config("./$name/sdn_config"); + + open(my $fh1, '<', "./$name/interfaces") or die "can't read interfaces file - $!"; + my $interfaces_config = PVE::INotify::__read_etc_network_interfaces($fh1, undef, undef); + close $fh1; + + my $pve_common_inotify; + $pve_common_inotify = Test::MockModule->new('PVE::INotify'); + $pve_common_inotify->mock( + nodename => sub { + return 'localhost'; + }, + read_file => sub { + # HACK this assumes we are always calling PVE::INotify::read_file('interfaces'); + return $interfaces_config; + }, + ); + + my $pve_sdn_zones_plugin; + $pve_sdn_zones_plugin = Test::MockModule->new('PVE::Network::SDN::Zones::Plugin'); + $pve_sdn_zones_plugin->mock( + get_local_route_ip => sub { + my ($targetip) = @_; + my $outiface = "vmbr0"; + my $outip = $interfaces_config->{ifaces}->{$outiface}->{address}; + if (Net::IP::ip_is_ipv6($targetip)) { + $outip = $interfaces_config->{ifaces}->{$outiface}->{address6}; + } + return ($outip, $outiface); + }, + ); + + my $sdn_module = Test::MockModule->new("PVE::Network::SDN"); + $sdn_module->mock( + running_config => sub { + return $sdn_config; + }, + ); + + foreach my $bin (qw(iptables ip6tables)) { + $tables->{$bin} = { nat => { POSTROUTING => [] }, raw => { PREROUTING => [] } }; + } + + my $bring_up_interfaces = sub { + split_post_up_hooks(PVE::Network::SDN::generate_raw_etc_network_config($sdn_config)); + }; + + my $apply_sdn = sub { + $bring_up_interfaces->(); + PVE::Network::SDN::Zones::apply_new_snat_rules(); + + return get_iptables(); + }; + + # every SNAT subnet must show up in the chain of the matching binary (iptables/ip6tables) + my $expected = { iptables => [], ip6tables => [] }; + for my $id (sort keys $sdn_config->{subnets}->{ids}->%*) { + next if !$sdn_config->{subnets}->{ids}->{$id}->{snat}; + + my ($zone, $network, $mask) = split(/-/, $id); + my $bin = Net::IP::ip_is_ipv6($network) ? 'ip6tables' : 'iptables'; + + # zones with a dedicated VRF masquerade on each uplink, others use a single SNAT rule + my $rules = 1; + if ($sdn_config->{zones}->{ids}->{$zone}->{'dedicated-vrf'}) { + my $ifaces = $interfaces_config->{ifaces}; + $rules = grep { ($ifaces->{$_}->{vrf} // '') eq "vrf_$zone" } keys $ifaces->%*; + } + + push $expected->{$bin}->@*, ("$network/$mask") x $rules; + } + + my $active = {}; + foreach my $bin (qw(iptables ip6tables)) { + my @snat = sort $expected->{$bin}->@*; + $active->{$bin} = { + chains => @snat ? ['PROXMOX-SDN'] : [], + jumps => @snat ? ['-j PROXMOX-SDN'] : [], + snat => \@snat, + ct_rules => @snat ? 1 : 0, + }; + } + + eq_or_diff($apply_sdn->(), $active, "$name: SNAT rules are active after the first apply"); + eq_or_diff($apply_sdn->(), $active, "$name: applying again does not duplicate any rule"); + + # check that no new chain is prepared if no subnet with SNAT exists anymore + delete $_->{snat} for values $sdn_config->{subnets}->{ids}->%*; + my $disabled = {}; + foreach my $bin (qw(iptables ip6tables)) { + $disabled->{$bin} = { %{ $active->{$bin} }, chains => [], jumps => [], snat => [] }; + } + eq_or_diff($apply_sdn->(), $disabled, "$name: SNAT chain is removed if SNAT is disabled"); +} + +done_testing(); -- 2.47.3