ea8ea4
From 6e0a50857ee0f03391c4505cb990763b382375f9 Mon Sep 17 00:00:00 2001
4f7c03
From: Eric Garver <eric@garver.life>
4f7c03
Date: Sun, 19 Jan 2020 14:37:31 -0500
ea8ea4
Subject: [PATCH 140/146] feat: nftables: support AllowZoneDrifting=yes
4f7c03
4f7c03
(cherry picked from commit 517a061c5886f2ebfb4aa7d73804aa7f3c5a3004)
4f7c03
(cherry picked from commit 618cd7e8612be216956aea278b798b32c067f933)
4f7c03
---
4f7c03
 src/firewall/core/nftables.py | 47 ++++++++++++++++++++++-------------
4f7c03
 1 file changed, 30 insertions(+), 17 deletions(-)
4f7c03
4f7c03
diff --git a/src/firewall/core/nftables.py b/src/firewall/core/nftables.py
4f7c03
index 0317d820389f..a559dc64c466 100644
4f7c03
--- a/src/firewall/core/nftables.py
4f7c03
+++ b/src/firewall/core/nftables.py
4f7c03
@@ -199,8 +199,11 @@ class nftables(object):
4f7c03
 
4f7c03
                 index = zone_source_index_cache[family].index(zone_source)
4f7c03
             else:
4f7c03
-                index = len(zone_source_index_cache[family])
4f7c03
-                
4f7c03
+                if self._fw._allow_zone_drifting:
4f7c03
+                    index = 0
4f7c03
+                else:
4f7c03
+                    index = len(zone_source_index_cache[family])
4f7c03
+
4f7c03
             if index == 0:
4f7c03
                 rule[0] = "insert"
4f7c03
             else:
4f7c03
@@ -411,9 +414,10 @@ class nftables(object):
4f7c03
                                   IPTABLES_TO_NFT_HOOK["raw"][chain][0],
4f7c03
                                   IPTABLES_TO_NFT_HOOK["raw"][chain][1]))
4f7c03
 
4f7c03
-            default_rules.append("add chain inet %s raw_%s_ZONES" % (TABLE_NAME, chain))
4f7c03
-            default_rules.append("add rule inet %s raw_%s jump raw_%s_ZONES" % (TABLE_NAME, chain, chain))
4f7c03
-            OUR_CHAINS["inet"]["raw"].update(set(["%s_ZONES" % chain]))
4f7c03
+            for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
4f7c03
+                default_rules.append("add chain inet %s raw_%s_%s" % (TABLE_NAME, chain, dispatch_suffix))
4f7c03
+                default_rules.append("add rule inet %s raw_%s jump raw_%s_%s" % (TABLE_NAME, chain, chain, dispatch_suffix))
4f7c03
+                OUR_CHAINS["inet"]["raw"].update(set(["%s_%s" % (chain, dispatch_suffix)]))
4f7c03
 
4f7c03
         OUR_CHAINS["inet"]["mangle"] = set()
4f7c03
         for chain in IPTABLES_TO_NFT_HOOK["mangle"].keys():
4f7c03
@@ -422,9 +426,10 @@ class nftables(object):
4f7c03
                                   IPTABLES_TO_NFT_HOOK["mangle"][chain][0],
4f7c03
                                   IPTABLES_TO_NFT_HOOK["mangle"][chain][1]))
4f7c03
 
4f7c03
-            default_rules.append("add chain inet %s mangle_%s_ZONES" % (TABLE_NAME, chain))
4f7c03
-            default_rules.append("add rule inet %s mangle_%s jump mangle_%s_ZONES" % (TABLE_NAME, chain, chain))
4f7c03
-            OUR_CHAINS["inet"]["mangle"].update(set(["%s_ZONES" % chain]))
4f7c03
+            for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
4f7c03
+                default_rules.append("add chain inet %s mangle_%s_%s" % (TABLE_NAME, chain, dispatch_suffix))
4f7c03
+                default_rules.append("add rule inet %s mangle_%s jump mangle_%s_%s" % (TABLE_NAME, chain, chain, dispatch_suffix))
4f7c03
+                OUR_CHAINS["inet"]["mangle"].update(set(["%s_%s" % (chain, dispatch_suffix)]))
4f7c03
 
4f7c03
         OUR_CHAINS["ip"]["nat"] = set()
4f7c03
         OUR_CHAINS["ip6"]["nat"] = set()
4f7c03
@@ -435,9 +440,10 @@ class nftables(object):
4f7c03
                                       IPTABLES_TO_NFT_HOOK["nat"][chain][0],
4f7c03
                                       IPTABLES_TO_NFT_HOOK["nat"][chain][1]))
4f7c03
 
4f7c03
-                default_rules.append("add chain %s %s nat_%s_ZONES" % (family, TABLE_NAME, chain))
4f7c03
-                default_rules.append("add rule %s %s nat_%s jump nat_%s_ZONES" % (family, TABLE_NAME, chain, chain))
4f7c03
-                OUR_CHAINS[family]["nat"].update(set(["%s_ZONES" % chain]))
4f7c03
+                for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
4f7c03
+                    default_rules.append("add chain %s %s nat_%s_%s" % (family, TABLE_NAME, chain, dispatch_suffix))
4f7c03
+                    default_rules.append("add rule %s %s nat_%s jump nat_%s_%s" % (family, TABLE_NAME, chain, chain, dispatch_suffix))
4f7c03
+                    OUR_CHAINS[family]["nat"].update(set(["%s_%s" % (chain, dispatch_suffix)]))
4f7c03
 
4f7c03
         OUR_CHAINS["inet"]["filter"] = set()
4f7c03
         for chain in IPTABLES_TO_NFT_HOOK["filter"].keys():
4f7c03
@@ -447,10 +453,11 @@ class nftables(object):
4f7c03
                                   IPTABLES_TO_NFT_HOOK["filter"][chain][1]))
4f7c03
 
4f7c03
         # filter, INPUT
4f7c03
-        default_rules.append("add chain inet %s filter_%s_ZONES" % (TABLE_NAME, "INPUT"))
4f7c03
         default_rules.append("add rule inet %s filter_%s ct state established,related accept" % (TABLE_NAME, "INPUT"))
4f7c03
         default_rules.append("add rule inet %s filter_%s iifname lo accept" % (TABLE_NAME, "INPUT"))
4f7c03
-        default_rules.append("add rule inet %s filter_%s jump filter_%s_ZONES" % (TABLE_NAME, "INPUT", "INPUT"))
4f7c03
+        for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
4f7c03
+            default_rules.append("add chain inet %s filter_%s_%s" % (TABLE_NAME, "INPUT", dispatch_suffix))
4f7c03
+            default_rules.append("add rule inet %s filter_%s jump filter_%s_%s" % (TABLE_NAME, "INPUT", "INPUT", dispatch_suffix))
4f7c03
         if log_denied != "off":
4f7c03
             default_rules.append("add rule inet %s filter_%s ct state invalid %%%%LOGTYPE%%%% log prefix '\"STATE_INVALID_DROP: \"'" % (TABLE_NAME, "INPUT"))
4f7c03
         default_rules.append("add rule inet %s filter_%s ct state invalid drop" % (TABLE_NAME, "INPUT"))
4f7c03
@@ -460,11 +467,12 @@ class nftables(object):
4f7c03
 
4f7c03
         # filter, FORWARD
4f7c03
         default_rules.append("add chain inet %s filter_%s_IN_ZONES" % (TABLE_NAME, "FORWARD"))
4f7c03
-        default_rules.append("add chain inet %s filter_%s_OUT_ZONES" % (TABLE_NAME, "FORWARD"))
4f7c03
         default_rules.append("add rule inet %s filter_%s ct state established,related accept" % (TABLE_NAME, "FORWARD"))
4f7c03
         default_rules.append("add rule inet %s filter_%s iifname lo accept" % (TABLE_NAME, "FORWARD"))
4f7c03
-        default_rules.append("add rule inet %s filter_%s jump filter_%s_IN_ZONES" % (TABLE_NAME, "FORWARD", "FORWARD"))
4f7c03
-        default_rules.append("add rule inet %s filter_%s jump filter_%s_OUT_ZONES" % (TABLE_NAME, "FORWARD", "FORWARD"))
4f7c03
+        for direction in ["IN", "OUT"]:
4f7c03
+            for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
4f7c03
+                default_rules.append("add chain inet %s filter_%s_%s_%s" % (TABLE_NAME, "FORWARD", direction, dispatch_suffix))
4f7c03
+                default_rules.append("add rule inet %s filter_%s jump filter_%s_%s_%s" % (TABLE_NAME, "FORWARD", "FORWARD", direction, dispatch_suffix))
4f7c03
         if log_denied != "off":
4f7c03
             default_rules.append("add rule inet %s filter_%s ct state invalid %%%%LOGTYPE%%%% log prefix '\"STATE_INVALID_DROP: \"'" % (TABLE_NAME, "FORWARD"))
4f7c03
         default_rules.append("add rule inet %s filter_%s ct state invalid drop" % (TABLE_NAME, "FORWARD"))
4f7c03
@@ -566,6 +574,11 @@ class nftables(object):
4f7c03
             "OUTPUT": "daddr",
4f7c03
         }[chain]
4f7c03
 
4f7c03
+        if self._fw._allow_zone_drifting:
4f7c03
+            zone_dispatch_chain = "%s_%s_ZONES_SOURCE" % (table, chain)
4f7c03
+        else:
4f7c03
+            zone_dispatch_chain = "%s_%s_ZONES" % (table, chain)
4f7c03
+
4f7c03
         target = DEFAULT_ZONE_TARGET.format(chain=SHORTCUTS[chain], zone=zone)
4f7c03
         action = "goto"
4f7c03
 
4f7c03
@@ -585,7 +598,7 @@ class nftables(object):
4f7c03
                 rule_family = "ip6"
4f7c03
 
4f7c03
         rule = [add_del, "rule", family, "%s" % TABLE_NAME,
4f7c03
-                "%s_%s_ZONES" % (table, chain),
4f7c03
+                zone_dispatch_chain,
4f7c03
                 "%%ZONE_SOURCE%%", zone,
4f7c03
                 rule_family, opt, address, action, "%s_%s" % (table, target)]
4f7c03
         return [rule]
4f7c03
-- 
4f7c03
2.23.0
4f7c03