public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Sichert <l.sichert@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Lukas Sichert <l.sichert@proxmox.com>
Subject: [PATCH network v3 2/7] fix #5066: zones: factor out SNAT rule generation
Date: Thu, 26 Nov 2026 16:29:00 +0100	[thread overview]
Message-ID: <20261126153925.54600-3-l.sichert@proxmox.com> (raw)
In-Reply-To: <20261126153925.54600-1-l.sichert@proxmox.com>

Both the Simple and EVPN zone plugins need to generate the same set of
SNAT and conntrack rules. Move their generation into a common helper so
that both plugins use the same setup sequence.

Create a temporary PROXMOX-SDN-NEW chain and add a jump to it from
POSTROUTING. Check whether the temporary chain and its POSTROUTING jump
already exist before creating them. The temporary chain allows the
complete set of newly generated rules to be prepared before replacing
the currently active PROXMOX-SDN chain.

Zones with a dedicated VRF pass their uplinks to the helper, which then
masquerades on each of them. Without uplinks, the SNAT rule is built
from the route to the outside, as before.

Only add the conntrack zone rule for the firewall bridges if it does not
exist yet. Reloading runs post-up without post-down, so the plain insert
added another copy on each run. Drop the post-down rule as well: the
ifreload path does not use it, and a manual post-down could remove the
rule that is shared by all SNAT subnets.

Signed-off-by: Lukas Sichert <l.sichert@proxmox.com>
Link: https://bugzilla.proxmox.com/show_bug.cgi?id=5066
---

Notes:
    changes v2 -> v3:
    - rebased on top of master
    - replaces "snat: add config generation helpers": the helper now
      prepares the rules in a temporary chain and takes optional uplinks
      for the masquerade rules of dedicated-VRF zones
    - add the conntrack zone rule only once
    
    changes v1 -> v2:
    - rebased on top of master

 src/PVE/Network/SDN/Zones/Plugin.pm | 47 +++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/src/PVE/Network/SDN/Zones/Plugin.pm b/src/PVE/Network/SDN/Zones/Plugin.pm
index 795ec9f..93ed142 100644
--- a/src/PVE/Network/SDN/Zones/Plugin.pm
+++ b/src/PVE/Network/SDN/Zones/Plugin.pm
@@ -8,6 +8,7 @@ use PVE::Cluster;
 use PVE::JSONSchema;
 use PVE::Network;
 use PVE::Network::SDN::Formats;
+use PVE::RESTEnvironment qw(log_warn);
 use PVE::RS::SDN;
 use PVE::Tools qw(run_command);
 
@@ -431,6 +432,52 @@ sub get_vrf_member_interfaces {
     return sort grep { ($ifaces->{$_}->{vrf} // '') eq $vrf } keys %$ifaces;
 }
 
+sub generate_new_snat_rules {
+    my ($checkrouteip, $iptables, $cidr, $uplinks) = @_;
+
+    my @snat_rules = ();
+
+    # Create the temporary chain only if it does not exist.
+    push @snat_rules,
+        "post-up $iptables -t nat -L PROXMOX-SDN-NEW 2>/dev/null"
+        . " || $iptables -t nat -N PROXMOX-SDN-NEW";
+
+    # Add the jump only if the exact jump does not already exist.
+    push @snat_rules,
+        "post-up $iptables -t nat -C POSTROUTING -j PROXMOX-SDN-NEW 2>/dev/null"
+        . " || $iptables -t nat -A POSTROUTING -j PROXMOX-SDN-NEW";
+
+    if ($uplinks && @$uplinks) {
+        # the routing table picks the uplink per connection (e.g. with ECMP), and its address may
+        # be dynamic, so let each uplink masquerade with its own address
+        for my $uplink (@$uplinks) {
+            push @snat_rules,
+                "post-up $iptables -t nat -A PROXMOX-SDN-NEW -s '$cidr' -o $uplink -j MASQUERADE";
+        }
+    } else {
+        #find outgoing interface
+        my ($outip, $outiface);
+        eval { ($outip, $outiface) = get_local_route_ip($checkrouteip); };
+        if ($@) {
+            log_warn("interface for SNAT could not be resolved: $@");
+        }
+
+        if ($outip && $outiface) {
+            #use snat, faster than masquerade
+            push @snat_rules,
+                "post-up $iptables -t nat -A PROXMOX-SDN-NEW -s '$cidr' -o $outiface -j SNAT --to-source $outip";
+        }
+    }
+
+    # Add the conntrack zone rule only once. Reloading re-runs post-up without post-down, and
+    # the rule is shared by all SNAT subnets, so it is not removed together with a single one.
+    push @snat_rules,
+        "post-up $iptables -t raw -C PREROUTING -i fwbr+ -j CT --zone 1 2>/dev/null"
+        . " || $iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1";
+
+    return @snat_rules;
+}
+
 sub find_local_ip_interface_peers {
     my ($peers, $iface) = @_;
 
-- 
2.47.3





  parent reply	other threads:[~2026-11-26 15:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 15:28 [PATCH manager/network v3 0/7] fix #5066: make generated snat rules flushable Lukas Sichert
2026-11-26 15:28 ` [PATCH network v3 1/7] style: zones: use fully qualified JSONSchema calls for standard options Lukas Sichert
2026-11-26 15:29 ` Lukas Sichert [this message]
2026-11-26 15:29 ` [PATCH network v3 3/7] fix #5066: zones: add helper to activate prepared SNAT rules Lukas Sichert
2026-11-26 15:29 ` [PATCH network v3 4/7] fix #5066: evpn: prepare SNAT rules in temporary chain Lukas Sichert
2026-11-26 15:29 ` [PATCH network v3 5/7] fix #5066: simple zone: " Lukas Sichert
2026-11-26 15:29 ` [PATCH network v3 6/7] fix #5066: zones: add test for generating and applying SNAT rules Lukas Sichert
2026-11-26 15:29 ` [PATCH manager v3 7/7] fix #5066: network: activate prepared SDN " Lukas Sichert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126153925.54600-3-l.sichert@proxmox.com \
    --to=l.sichert@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal