From 7e7839dc4c76e53d22b5557f022fa87fb152c35a Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sat, 17 Oct 2026 21:52:56 +0200 Subject: nat: T9475: recover from stale nftables rule files Whether the NAT tables already exist was derived from the rendered rule files in /run. A failed commit could leave such a file behind without the table, every later NAT commit then tried to delete a missing table and failed with "Configuration file errors encountered!" until reboot. The NAT and static NAT tables are now declared before they are deleted, which succeeds whether or not they exist. --- data/templates/firewall/nftables-nat.j2 | 4 ++-- data/templates/firewall/nftables-static-nat.j2 | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'data') diff --git a/data/templates/firewall/nftables-nat.j2 b/data/templates/firewall/nftables-nat.j2 index 8c8dd3a8b..a1acbf741 100644 --- a/data/templates/firewall/nftables-nat.j2 +++ b/data/templates/firewall/nftables-nat.j2 @@ -2,9 +2,9 @@ {% import 'firewall/nftables-defines.j2' as group_tmpl %} -{% if first_install is not vyos_defined %} +# declaring the table first makes the delete succeed whether or not it exists +table ip vyos_nat delete table ip vyos_nat -{% endif %} {% if deleted is not vyos_defined %} table ip vyos_nat { # diff --git a/data/templates/firewall/nftables-static-nat.j2 b/data/templates/firewall/nftables-static-nat.j2 index e5e3da867..4f97270a5 100644 --- a/data/templates/firewall/nftables-static-nat.j2 +++ b/data/templates/firewall/nftables-static-nat.j2 @@ -1,8 +1,8 @@ #!/usr/sbin/nft -f -{% if first_install is not vyos_defined %} +# declaring the table first makes the delete succeed whether or not it exists +table ip vyos_static_nat delete table ip vyos_static_nat -{% endif %} {% if deleted is not vyos_defined %} table ip vyos_static_nat { # -- cgit v1.2.3 From 8dd4034d984414ab168a60681aa822703cdca033 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sat, 17 Oct 2026 21:53:02 +0200 Subject: nat: T9475: declare FQDN sets only once The FQDN sets used by NAT rules were declared twice in the NAT table. --- data/templates/firewall/nftables-nat.j2 | 6 ------ 1 file changed, 6 deletions(-) (limited to 'data') diff --git a/data/templates/firewall/nftables-nat.j2 b/data/templates/firewall/nftables-nat.j2 index a1acbf741..1ea761dea 100644 --- a/data/templates/firewall/nftables-nat.j2 +++ b/data/templates/firewall/nftables-nat.j2 @@ -39,12 +39,6 @@ table ip vyos_nat { {% endif %} } -{% for set_name in ip_fqdn %} - set FQDN_nat_{{ set_name }} { - type ipv4_addr - flags interval - } -{% endfor %} chain VYOS_PRE_DNAT_HOOK { return -- cgit v1.2.3 From c01cf102163c93891b17a663790db6c2b8433301 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sat, 17 Oct 2026 21:53:22 +0200 Subject: nat66: T9475: rebuild NAT66 table when rule file and table drift apart The NAT66 ruleset only deleted the existing table when its rule file in /run was present. A table removed while the file stayed made the delete, and with it every following commit, fail. The table is now declared right before it is deleted, so each commit rebuilds it regardless of what is currently loaded. --- data/templates/firewall/nftables-nat66.j2 | 4 ++-- smoketest/scripts/cli/test_nat66.py | 22 ++++++++++++++++++++++ src/conf_mode/nat66.py | 3 --- 3 files changed, 24 insertions(+), 5 deletions(-) (limited to 'data') diff --git a/data/templates/firewall/nftables-nat66.j2 b/data/templates/firewall/nftables-nat66.j2 index 09b5b6ac2..3f206ca4e 100644 --- a/data/templates/firewall/nftables-nat66.j2 +++ b/data/templates/firewall/nftables-nat66.j2 @@ -2,9 +2,9 @@ {% import 'firewall/nftables-defines.j2' as group_tmpl %} -{% if first_install is not vyos_defined %} +# declaring the table first makes the delete succeed whether or not it exists +table ip6 vyos_nat delete table ip6 vyos_nat -{% endif %} {% if deleted is not vyos_defined %} table ip6 vyos_nat { # diff --git a/smoketest/scripts/cli/test_nat66.py b/smoketest/scripts/cli/test_nat66.py index 9072e78dd..ee25edbc5 100755 --- a/smoketest/scripts/cli/test_nat66.py +++ b/smoketest/scripts/cli/test_nat66.py @@ -19,6 +19,7 @@ import unittest from base_vyostest_shim import VyOSUnitTestSHIM from vyos.configsession import ConfigSessionError +from vyos.utils.process import cmdl base_path = ['nat66'] src_path = base_path + ['source'] @@ -319,5 +320,26 @@ class TestNAT66(VyOSUnitTestSHIM.TestCase): self.cli_set(dst_path) self.cli_commit() + def test_nat66_table_recreated(self): + # the rule file in /run and the nftables table can drift apart, the + # next commit must rebuild the table instead of failing on it + self.cli_set(src_path + ['rule', '1', 'outbound-interface', 'name', 'eth1']) + self.cli_set(src_path + ['rule', '1', 'source', 'prefix', 'fc00::/64']) + self.cli_set(src_path + ['rule', '1', 'translation', 'address', 'masquerade']) + self.cli_commit() + + cmdl(['nft', 'delete', 'table', 'ip6', 'vyos_nat'], sudo=True) + + self.cli_set(src_path + ['rule', '2', 'outbound-interface', 'name', 'eth1']) + self.cli_set(src_path + ['rule', '2', 'source', 'prefix', 'fc02::/64']) + self.cli_set(src_path + ['rule', '2', 'exclude']) + self.cli_commit() + + nftables_search = [ + ['ip6 saddr fc00::/64', 'masquerade', 'comment "SRC-NAT66-1"'], + ['ip6 saddr fc02::/64', 'return', 'comment "SRC-NAT66-2"'], + ] + self.verify_nftables(nftables_search, 'ip6 vyos_nat') + if __name__ == '__main__': unittest.main(verbosity=2, failfast=VyOSUnitTestSHIM.TestCase.debug_on()) diff --git a/src/conf_mode/nat66.py b/src/conf_mode/nat66.py index 999e320eb..420f9143d 100755 --- a/src/conf_mode/nat66.py +++ b/src/conf_mode/nat66.py @@ -127,9 +127,6 @@ def verify(nat): return None def generate(nat): - if not os.path.exists(nftables_nat66_config): - nat['first_install'] = True - render(nftables_nat66_config, 'firewall/nftables-nat66.j2', nat) # dry-run newly generated configuration -- cgit v1.2.3