Blame SOURCES/0031-feat-nftables-support-AllowZoneDrifting-yes.patch

087194
From 3c2ca67f86de7cd490ae25333e330b4aea0447f1 Mon Sep 17 00:00:00 2001
087194
From: Eric Garver <eric@garver.life>
087194
Date: Sun, 19 Jan 2020 14:37:31 -0500
087194
Subject: [PATCH 31/35] feat: nftables: support AllowZoneDrifting=yes
087194
087194
(cherry picked from commit 517a061c5886f2ebfb4aa7d73804aa7f3c5a3004)
087194
(cherry picked from commit d15fb2911a89477f26a800d498fa47d7c2e5ec5f)
087194
---
087194
 src/firewall/core/nftables.py | 44 +++++++++++++++++++++++------------
087194
 1 file changed, 29 insertions(+), 15 deletions(-)
087194
087194
diff --git a/src/firewall/core/nftables.py b/src/firewall/core/nftables.py
087194
index 33a170a76a98..79799388a923 100644
087194
--- a/src/firewall/core/nftables.py
087194
+++ b/src/firewall/core/nftables.py
087194
@@ -204,8 +204,11 @@ class nftables(object):
087194
 
087194
                 index = zone_source_index_cache[family].index(zone_source)
087194
             else:
087194
-                index = len(zone_source_index_cache[family])
087194
-                
087194
+                if self._fw._allow_zone_drifting:
087194
+                    index = 0
087194
+                else:
087194
+                    index = len(zone_source_index_cache[family])
087194
+
087194
             if index == 0:
087194
                 rule[0] = "insert"
087194
             else:
087194
@@ -488,8 +491,9 @@ class nftables(object):
087194
                                   IPTABLES_TO_NFT_HOOK["raw"][chain][1]))
087194
 
087194
         for chain in ["PREROUTING"]:
087194
-            default_rules.append("add chain inet %s raw_%s_ZONES" % (TABLE_NAME, chain))
087194
-            default_rules.append("add rule inet %s raw_%s jump raw_%s_ZONES" % (TABLE_NAME, chain, chain))
087194
+            for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
087194
+                default_rules.append("add chain inet %s raw_%s_%s" % (TABLE_NAME, chain, dispatch_suffix))
087194
+                default_rules.append("add rule inet %s raw_%s jump raw_%s_%s" % (TABLE_NAME, chain, chain, dispatch_suffix))
087194
 
087194
         for chain in IPTABLES_TO_NFT_HOOK["mangle"].keys():
087194
             default_rules.append("add chain inet %s mangle_%s '{ type filter hook %s priority %d ; }'" %
087194
@@ -497,8 +501,9 @@ class nftables(object):
087194
                                   IPTABLES_TO_NFT_HOOK["mangle"][chain][0],
087194
                                   IPTABLES_TO_NFT_HOOK["mangle"][chain][1]))
087194
 
087194
-            default_rules.append("add chain inet %s mangle_%s_ZONES" % (TABLE_NAME, chain))
087194
-            default_rules.append("add rule inet %s mangle_%s jump mangle_%s_ZONES" % (TABLE_NAME, chain, chain))
087194
+            for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
087194
+                default_rules.append("add chain inet %s mangle_%s_%s" % (TABLE_NAME, chain, dispatch_suffix))
087194
+                default_rules.append("add rule inet %s mangle_%s jump mangle_%s_%s" % (TABLE_NAME, chain, chain, dispatch_suffix))
087194
 
087194
         for family in ["ip", "ip6"]:
087194
             for chain in IPTABLES_TO_NFT_HOOK["nat"].keys():
087194
@@ -507,8 +512,9 @@ class nftables(object):
087194
                                       IPTABLES_TO_NFT_HOOK["nat"][chain][0],
087194
                                       IPTABLES_TO_NFT_HOOK["nat"][chain][1]))
087194
 
087194
-                default_rules.append("add chain %s %s nat_%s_ZONES" % (family, TABLE_NAME, chain))
087194
-                default_rules.append("add rule %s %s nat_%s jump nat_%s_ZONES" % (family, TABLE_NAME, chain, chain))
087194
+                for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
087194
+                    default_rules.append("add chain %s %s nat_%s_%s" % (family, TABLE_NAME, chain, dispatch_suffix))
087194
+                    default_rules.append("add rule %s %s nat_%s jump nat_%s_%s" % (family, TABLE_NAME, chain, chain, dispatch_suffix))
087194
 
087194
         for chain in IPTABLES_TO_NFT_HOOK["filter"].keys():
087194
             default_rules.append("add chain inet %s filter_%s '{ type filter hook %s priority %d ; }'" %
087194
@@ -517,11 +523,12 @@ class nftables(object):
087194
                                   IPTABLES_TO_NFT_HOOK["filter"][chain][1]))
087194
 
087194
         # filter, INPUT
087194
-        default_rules.append("add chain inet %s filter_%s_ZONES" % (TABLE_NAME, "INPUT"))
087194
         default_rules.append("add rule inet %s filter_%s ct state established,related accept" % (TABLE_NAME, "INPUT"))
087194
         default_rules.append("add rule inet %s filter_%s ct status dnat accept" % (TABLE_NAME, "INPUT"))
087194
         default_rules.append("add rule inet %s filter_%s iifname lo accept" % (TABLE_NAME, "INPUT"))
087194
-        default_rules.append("add rule inet %s filter_%s jump filter_%s_ZONES" % (TABLE_NAME, "INPUT", "INPUT"))
087194
+        for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
087194
+            default_rules.append("add chain inet %s filter_%s_%s" % (TABLE_NAME, "INPUT", dispatch_suffix))
087194
+            default_rules.append("add rule inet %s filter_%s jump filter_%s_%s" % (TABLE_NAME, "INPUT", "INPUT", dispatch_suffix))
087194
         if log_denied != "off":
087194
             default_rules.append("add rule inet %s filter_%s ct state invalid %%%%LOGTYPE%%%% log prefix '\"STATE_INVALID_DROP: \"'" % (TABLE_NAME, "INPUT"))
087194
         default_rules.append("add rule inet %s filter_%s ct state invalid drop" % (TABLE_NAME, "INPUT"))
087194
@@ -530,13 +537,15 @@ class nftables(object):
087194
         default_rules.append("add rule inet %s filter_%s reject with icmpx type admin-prohibited" % (TABLE_NAME, "INPUT"))
087194
 
087194
         # filter, FORWARD
087194
-        default_rules.append("add chain inet %s filter_%s_IN_ZONES" % (TABLE_NAME, "FORWARD"))
087194
-        default_rules.append("add chain inet %s filter_%s_OUT_ZONES" % (TABLE_NAME, "FORWARD"))
087194
         default_rules.append("add rule inet %s filter_%s ct state established,related accept" % (TABLE_NAME, "FORWARD"))
087194
         default_rules.append("add rule inet %s filter_%s ct status dnat accept" % (TABLE_NAME, "FORWARD"))
087194
         default_rules.append("add rule inet %s filter_%s iifname lo accept" % (TABLE_NAME, "FORWARD"))
087194
-        default_rules.append("add rule inet %s filter_%s jump filter_%s_IN_ZONES" % (TABLE_NAME, "FORWARD", "FORWARD"))
087194
-        default_rules.append("add rule inet %s filter_%s jump filter_%s_OUT_ZONES" % (TABLE_NAME, "FORWARD", "FORWARD"))
087194
+        for direction in ["IN", "OUT"]:
087194
+            for dispatch_suffix in ["ZONES_SOURCE", "ZONES"] if self._fw._allow_zone_drifting else ["ZONES"]:
087194
+                default_rules.append("add chain inet %s filter_%s_%s_%s" % (TABLE_NAME, "FORWARD", direction, dispatch_suffix))
087194
+                default_rules.append("add chain inet %s filter_%s_%s_%s" % (TABLE_NAME, "FORWARD", direction, dispatch_suffix))
087194
+                default_rules.append("add rule inet %s filter_%s jump filter_%s_%s_%s" % (TABLE_NAME, "FORWARD", "FORWARD", direction, dispatch_suffix))
087194
+                default_rules.append("add rule inet %s filter_%s jump filter_%s_%s_%s" % (TABLE_NAME, "FORWARD", "FORWARD", direction, dispatch_suffix))
087194
         if log_denied != "off":
087194
             default_rules.append("add rule inet %s filter_%s ct state invalid %%%%LOGTYPE%%%% log prefix '\"STATE_INVALID_DROP: \"'" % (TABLE_NAME, "FORWARD"))
087194
         default_rules.append("add rule inet %s filter_%s ct state invalid drop" % (TABLE_NAME, "FORWARD"))
087194
@@ -634,6 +643,11 @@ class nftables(object):
087194
             "OUTPUT": "daddr",
087194
         }[chain]
087194
 
087194
+        if self._fw._allow_zone_drifting:
087194
+            zone_dispatch_chain = "%s_%s_ZONES_SOURCE" % (table, chain)
087194
+        else:
087194
+            zone_dispatch_chain = "%s_%s_ZONES" % (table, chain)
087194
+
087194
         target = DEFAULT_ZONE_TARGET.format(chain=SHORTCUTS[chain], zone=zone)
087194
         action = "goto"
087194
 
087194
@@ -653,7 +667,7 @@ class nftables(object):
087194
                 rule_family = "ip6"
087194
 
087194
         rule = [add_del, "rule", family, "%s" % TABLE_NAME,
087194
-                "%s_%s_ZONES" % (table, chain),
087194
+                zone_dispatch_chain,
087194
                 "%%ZONE_SOURCE%%", zone,
087194
                 rule_family, opt, address, action, "%s_%s" % (table, target)]
087194
         return [rule]
087194
-- 
087194
2.23.0
087194