ruleset: reuse zone-jump.uc template for notrack and helper chain jumps
authorJo-Philipp Wich <jo@mein.io>
Tue, 14 Jun 2022 12:59:58 +0000 (14:59 +0200)
committerJo-Philipp Wich <jo@mein.io>
Tue, 14 Jun 2022 14:27:26 +0000 (16:27 +0200)
Avoid some code-duplication by reusing the zone-jump.uc partial template
to emit the helper_* chain jump rules.

Also add some test coverage for notrack rules.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
root/usr/share/firewall4/templates/ruleset.uc
root/usr/share/firewall4/templates/zone-jump.uc
tests/01_configuration/01_ruleset
tests/02_zones/01_policies
tests/02_zones/04_wildcard_devices
tests/02_zones/05_subnet_mask_matches
tests/02_zones/07_helpers
tests/03_rules/05_mangle
tests/03_rules/10_notrack [new file with mode: 0644]

index 9d3be7f1f038c27de93ee08ff44de8a9e6102d66..d37498447a99a368370cb8cfcfff68e1685564e0 100644 (file)
@@ -147,10 +147,7 @@ table inet fw4 {
 {%    let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
 {%    let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
 {%    if (devices_pos || subnets_pos): %}
-               {%+ if (rule.family): -%}
-                       meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
-               {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
-               jump helper_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} CT helper assignment"
+               {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
 {%    endif %}
 {%   endif %}
                {%+ include("zone-jump.uc", { fw4, zone, rule, direction: "output" }) %}
@@ -169,10 +166,7 @@ table inet fw4 {
 {%    let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
 {%    let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
 {%    if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
-               {%+ if (rule.family): -%}
-                       meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
-               {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
-               jump helper_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} CT helper assignment"
+               {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "helper" }) %}
 {%    endif %}
 {%   endfor %}
 {%  endif %}
@@ -333,10 +327,7 @@ table inet fw4 {
 {%    let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, false); %}
 {%    let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, false); %}
 {%    if (rule.devices_neg || rule.subnets_neg || devices_pos || subnets_pos): %}
-               {%+ if (rule.family): -%}
-                       meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
-               {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
-               jump notrack_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} CT bypass"
+               {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
 {%    endif %}
 {%   endfor %}
 {%  endif %}
@@ -351,10 +342,7 @@ table inet fw4 {
 {%    let devices_pos = fw4.filter_loopback_devs(rule.devices_pos, true); %}
 {%    let subnets_pos = fw4.filter_loopback_addrs(rule.subnets_pos, true); %}
 {%    if (devices_pos || subnets_pos): %}
-               {%+ if (rule.family): -%}
-                       meta nfproto {{ fw4.nfproto(rule.family) }} {%+ endif -%}
-               {%+ include("zone-match.uc", { fw4, egress: false, rule: { ...rule, devices_pos, subnets_pos } }) -%}
-               jump notrack_{{ zone.name }} comment "!fw4: {{ zone.name }} {{ fw4.nfproto(rule.family, true) }} CT bypass"
+               {%+ include("zone-jump.uc", { fw4, zone, rule: { ...rule, devices_pos, subnets_pos }, direction: "notrack" }) %}
 {%    endif %}
 {%   endfor %}
 {%  endif %}
index 902321f4b3e3c865053b938d2b5e8a92f0f2193b..e31b0bc5debb6a3ba31de5242c0deb3af2f25ae4 100644 (file)
@@ -3,4 +3,4 @@
 {%+ include("zone-match.uc", { egress: (direction in ["output", "srcnat"]), rule }) -%}
 jump {{ direction }}_{{ zone.name }} comment "!fw4: Handle {{ zone.name }} {{
        fw4.nfproto(rule.family, true)
-}} {{ direction }} traffic"
+}} {{ direction }} {{ (direction == 'helper') ? "assignment" : "traffic" }}"
index cd32edceca5cc584b191758bb43f3b50dd0b16f2..9acb429c50b94ad72fab8f07e171afb966037600 100644 (file)
@@ -142,7 +142,7 @@ table inet fw4 {
 
        chain prerouting {
                type filter hook prerouting priority filter; policy accept;
-               iifname "br-lan" jump helper_lan comment "!fw4: lan IPv4/IPv6 CT helper assignment"
+               iifname "br-lan" jump helper_lan comment "!fw4: Handle lan IPv4/IPv6 helper assignment"
        }
 
        chain handle_reject {
index 86c2910be465de7a0e937898c78667024c00853a..5a2eeac873284fa5b4bc8a9774abc110e15c944a 100644 (file)
@@ -122,9 +122,9 @@ table inet fw4 {
 
        chain prerouting {
                type filter hook prerouting priority filter; policy accept;
-               iifname "zone1" jump helper_test1 comment "!fw4: test1 IPv4/IPv6 CT helper assignment"
-               iifname "zone2" jump helper_test2 comment "!fw4: test2 IPv4/IPv6 CT helper assignment"
-               iifname "zone3" jump helper_test3 comment "!fw4: test3 IPv4/IPv6 CT helper assignment"
+               iifname "zone1" jump helper_test1 comment "!fw4: Handle test1 IPv4/IPv6 helper assignment"
+               iifname "zone2" jump helper_test2 comment "!fw4: Handle test2 IPv4/IPv6 helper assignment"
+               iifname "zone3" jump helper_test3 comment "!fw4: Handle test3 IPv4/IPv6 helper assignment"
        }
 
        chain handle_reject {
index 86955941d46fe2ce16202fae87246e60fb8411e0..b7e01e1ce28a44385e8604372abfd510af52d575 100644 (file)
@@ -163,14 +163,14 @@ table inet fw4 {
 
        chain prerouting {
                type filter hook prerouting priority filter; policy accept;
-               iifname "/never/" jump helper_test2 comment "!fw4: test2 IPv4/IPv6 CT helper assignment"
-               iifname "test*" jump helper_test3 comment "!fw4: test3 IPv4/IPv6 CT helper assignment"
-               iifname "foo*" jump helper_test4 comment "!fw4: test4 IPv4/IPv6 CT helper assignment"
-               iifname "bar*" jump helper_test4 comment "!fw4: test4 IPv4/IPv6 CT helper assignment"
-               iifname { "test1", "test2" } jump helper_test4 comment "!fw4: test4 IPv4/IPv6 CT helper assignment"
-               iifname "foo*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: test5 IPv4/IPv6 CT helper assignment"
-               iifname "bar*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: test5 IPv4/IPv6 CT helper assignment"
-               iifname { "test1", "test2" } iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: test5 IPv4/IPv6 CT helper assignment"
+               iifname "/never/" jump helper_test2 comment "!fw4: Handle test2 IPv4/IPv6 helper assignment"
+               iifname "test*" jump helper_test3 comment "!fw4: Handle test3 IPv4/IPv6 helper assignment"
+               iifname "foo*" jump helper_test4 comment "!fw4: Handle test4 IPv4/IPv6 helper assignment"
+               iifname "bar*" jump helper_test4 comment "!fw4: Handle test4 IPv4/IPv6 helper assignment"
+               iifname { "test1", "test2" } jump helper_test4 comment "!fw4: Handle test4 IPv4/IPv6 helper assignment"
+               iifname "foo*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: Handle test5 IPv4/IPv6 helper assignment"
+               iifname "bar*" iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: Handle test5 IPv4/IPv6 helper assignment"
+               iifname { "test1", "test2" } iifname != { "test3", "test4" } iifname != "baz*" iifname != "qrx*" jump helper_test5 comment "!fw4: Handle test5 IPv4/IPv6 helper assignment"
        }
 
        chain handle_reject {
index 29108008f379be8cc6719313a06351aa011bce4c..27f9dbc8b6031acd6df296567443f2b73e3cf00d 100644 (file)
@@ -112,9 +112,9 @@ table inet fw4 {
 
        chain prerouting {
                type filter hook prerouting priority filter; policy accept;
-               meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: test2 IPv6 CT helper assignment"
-               meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: test2 IPv6 CT helper assignment"
-               meta nfproto ipv6 ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: test2 IPv6 CT helper assignment"
+               meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::1 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: Handle test2 IPv6 helper assignment"
+               meta nfproto ipv6 ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff == ::2 ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: Handle test2 IPv6 helper assignment"
+               meta nfproto ipv6 ip6 saddr { ::3, ::4 } ip6 saddr != { ::7, ::8 } ip6 saddr & ::ffff != ::5 ip6 saddr & ::ffff != ::6 jump helper_test2 comment "!fw4: Handle test2 IPv6 helper assignment"
        }
 
        chain handle_reject {
index c6f68de40adba6d052f8cfdda1296446ab69b793..ceef65aa749047a3facf14c56b18b6dc31f033a7 100644 (file)
@@ -196,10 +196,10 @@ table inet fw4 {
 
        chain prerouting {
                type filter hook prerouting priority filter; policy accept;
-               iifname "zone1" jump helper_test1 comment "!fw4: test1 IPv4/IPv6 CT helper assignment"
-               iifname "zone2" jump helper_test2 comment "!fw4: test2 IPv4/IPv6 CT helper assignment"
-               iifname "zone3" jump helper_test3 comment "!fw4: test3 IPv4/IPv6 CT helper assignment"
-               iifname "zone4" jump helper_test4 comment "!fw4: test4 IPv4/IPv6 CT helper assignment"
+               iifname "zone1" jump helper_test1 comment "!fw4: Handle test1 IPv4/IPv6 helper assignment"
+               iifname "zone2" jump helper_test2 comment "!fw4: Handle test2 IPv4/IPv6 helper assignment"
+               iifname "zone3" jump helper_test3 comment "!fw4: Handle test3 IPv4/IPv6 helper assignment"
+               iifname "zone4" jump helper_test4 comment "!fw4: Handle test4 IPv4/IPv6 helper assignment"
        }
 
        chain handle_reject {
index 79ae8ec5698026a987d0d8a0b58430469d1e6ae6..04ae461e2fabdcca354376419e2c95a8fef1c9f3 100644 (file)
@@ -204,8 +204,8 @@ table inet fw4 {
 
        chain prerouting {
                type filter hook prerouting priority filter; policy accept;
-               iifname { "eth0", "eth1" } jump helper_lan comment "!fw4: lan IPv4/IPv6 CT helper assignment"
-               iifname { "eth2", "eth3" } jump helper_wan comment "!fw4: wan IPv4/IPv6 CT helper assignment"
+               iifname { "eth0", "eth1" } jump helper_lan comment "!fw4: Handle lan IPv4/IPv6 helper assignment"
+               iifname { "eth2", "eth3" } jump helper_wan comment "!fw4: Handle wan IPv4/IPv6 helper assignment"
        }
 
        chain handle_reject {
diff --git a/tests/03_rules/10_notrack b/tests/03_rules/10_notrack
new file mode 100644 (file)
index 0000000..717894b
--- /dev/null
@@ -0,0 +1,275 @@
+Ensure that NOTRACK rules end up in the appropriate chains, depending on
+the src and dest options.
+
+-- Testcase --
+{%
+       include("./root/usr/share/firewall4/main.uc", {
+               getenv: function(varname) {
+                       switch (varname) {
+                       case 'ACTION':
+                               return 'print';
+                       }
+               }
+       })
+%}
+-- End --
+
+-- File uci/helpers.json --
+{}
+-- End --
+
+-- File fs/open~_sys_class_net_eth0_flags.txt --
+0x1103
+-- End --
+
+-- File fs/open~_sys_class_net_lo_flags.txt --
+0x9
+-- End --
+
+-- File uci/firewall.json --
+{
+       "zone": [
+               {
+                       "name": "zone1",
+                       "device": [ "eth0" ],
+                       "auto_helper": 0
+               },
+               {
+                       "name": "zone2",
+                       "device": [ "lo" ],
+                       "auto_helper": 0
+               },
+               {
+                       "name": "zone3",
+                       "subnet": [ "127.0.0.1/8", "::1/128" ],
+                       "auto_helper": 0
+               }
+       ],
+       "rule": [
+               {
+                       ".description": "An ordinary notrack rule should end up in the raw_prerouting chain",
+                       "name": "Notrack rule #1",
+                       "src": "zone1",
+                       "target": "NOTRACK"
+               },
+               {
+                       ".description": "A notrack rule with loopback source device should end up in the raw_output chain",
+                       "name": "Notrack rule #2",
+                       "src": "zone2",
+                       "target": "NOTRACK"
+               },
+               {
+                       ".description": "A notrack rule with loopback source address should end up in the raw_output chain",
+                       "name": "Notrack rule #3",
+                       "src": "zone3",
+                       "target": "NOTRACK"
+               }
+       ]
+}
+-- End --
+
+-- Expect stdout --
+table inet fw4
+flush table inet fw4
+
+table inet fw4 {
+       #
+       # Set definitions
+       #
+
+
+       #
+       # Defines
+       #
+
+       define zone1_devices = { "eth0" }
+       define zone2_devices = { "lo" }
+       define zone3_subnets = { 127.0.0.0/8, ::1 }
+
+       #
+       # User includes
+       #
+
+       include "/etc/nftables.d/*.nft"
+
+
+       #
+       # Filter rules
+       #
+
+       chain input {
+               type filter hook input priority filter; policy drop;
+
+               iifname "lo" accept comment "!fw4: Accept traffic from loopback"
+
+               ct state established,related accept comment "!fw4: Allow inbound established and related flows"
+               iifname "eth0" jump input_zone1 comment "!fw4: Handle zone1 IPv4/IPv6 input traffic"
+               iifname "lo" jump input_zone2 comment "!fw4: Handle zone2 IPv4/IPv6 input traffic"
+               meta nfproto ipv4 ip saddr 127.0.0.0/8 jump input_zone3 comment "!fw4: Handle zone3 IPv4 input traffic"
+               meta nfproto ipv6 ip6 saddr ::1 jump input_zone3 comment "!fw4: Handle zone3 IPv6 input traffic"
+       }
+
+       chain forward {
+               type filter hook forward priority filter; policy drop;
+
+               ct state established,related accept comment "!fw4: Allow forwarded established and related flows"
+               iifname "eth0" jump forward_zone1 comment "!fw4: Handle zone1 IPv4/IPv6 forward traffic"
+               iifname "lo" jump forward_zone2 comment "!fw4: Handle zone2 IPv4/IPv6 forward traffic"
+               meta nfproto ipv4 ip saddr 127.0.0.0/8 jump forward_zone3 comment "!fw4: Handle zone3 IPv4 forward traffic"
+               meta nfproto ipv6 ip6 saddr ::1 jump forward_zone3 comment "!fw4: Handle zone3 IPv6 forward traffic"
+       }
+
+       chain output {
+               type filter hook output priority filter; policy drop;
+
+               oifname "lo" accept comment "!fw4: Accept traffic towards loopback"
+
+               ct state established,related accept comment "!fw4: Allow outbound established and related flows"
+               oifname "eth0" jump output_zone1 comment "!fw4: Handle zone1 IPv4/IPv6 output traffic"
+               oifname "lo" jump output_zone2 comment "!fw4: Handle zone2 IPv4/IPv6 output traffic"
+               meta nfproto ipv4 ip daddr 127.0.0.0/8 jump output_zone3 comment "!fw4: Handle zone3 IPv4 output traffic"
+               meta nfproto ipv6 ip6 daddr ::1 jump output_zone3 comment "!fw4: Handle zone3 IPv6 output traffic"
+       }
+
+       chain prerouting {
+               type filter hook prerouting priority filter; policy accept;
+       }
+
+       chain handle_reject {
+               meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
+               reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
+       }
+
+       chain input_zone1 {
+               jump drop_from_zone1
+       }
+
+       chain output_zone1 {
+               jump drop_to_zone1
+       }
+
+       chain forward_zone1 {
+               jump drop_to_zone1
+       }
+
+       chain drop_from_zone1 {
+               iifname "eth0" counter drop comment "!fw4: drop zone1 IPv4/IPv6 traffic"
+       }
+
+       chain drop_to_zone1 {
+               oifname "eth0" counter drop comment "!fw4: drop zone1 IPv4/IPv6 traffic"
+       }
+
+       chain input_zone2 {
+               jump drop_from_zone2
+       }
+
+       chain output_zone2 {
+               jump drop_to_zone2
+       }
+
+       chain forward_zone2 {
+               jump drop_to_zone2
+       }
+
+       chain drop_from_zone2 {
+               iifname "lo" counter drop comment "!fw4: drop zone2 IPv4/IPv6 traffic"
+       }
+
+       chain drop_to_zone2 {
+               oifname "lo" counter drop comment "!fw4: drop zone2 IPv4/IPv6 traffic"
+       }
+
+       chain input_zone3 {
+               jump drop_from_zone3
+       }
+
+       chain output_zone3 {
+               jump drop_to_zone3
+       }
+
+       chain forward_zone3 {
+               jump drop_to_zone3
+       }
+
+       chain drop_from_zone3 {
+               meta nfproto ipv4 ip saddr 127.0.0.0/8 counter drop comment "!fw4: drop zone3 IPv4 traffic"
+               meta nfproto ipv6 ip6 saddr ::1 counter drop comment "!fw4: drop zone3 IPv6 traffic"
+       }
+
+       chain drop_to_zone3 {
+               meta nfproto ipv4 ip daddr 127.0.0.0/8 counter drop comment "!fw4: drop zone3 IPv4 traffic"
+               meta nfproto ipv6 ip6 daddr ::1 counter drop comment "!fw4: drop zone3 IPv6 traffic"
+       }
+
+
+       #
+       # NAT rules
+       #
+
+       chain dstnat {
+               type nat hook prerouting priority dstnat; policy accept;
+       }
+
+       chain srcnat {
+               type nat hook postrouting priority srcnat; policy accept;
+       }
+
+
+       #
+       # Raw rules (notrack)
+       #
+
+       chain raw_prerouting {
+               type filter hook prerouting priority raw; policy accept;
+               iifname "eth0" jump notrack_zone1 comment "!fw4: Handle zone1 IPv4/IPv6 notrack traffic"
+       }
+
+       chain raw_output {
+               type filter hook output priority raw; policy accept;
+               iifname "lo" jump notrack_zone2 comment "!fw4: Handle zone2 IPv4/IPv6 notrack traffic"
+               meta nfproto ipv4 ip saddr 127.0.0.0/8 jump notrack_zone3 comment "!fw4: Handle zone3 IPv4 notrack traffic"
+               meta nfproto ipv6 ip6 saddr ::1 jump notrack_zone3 comment "!fw4: Handle zone3 IPv6 notrack traffic"
+       }
+
+       chain notrack_zone1 {
+               meta l4proto tcp counter notrack comment "!fw4: Notrack rule #1"
+               meta l4proto udp counter notrack comment "!fw4: Notrack rule #1"
+       }
+
+       chain notrack_zone2 {
+               meta l4proto tcp counter notrack comment "!fw4: Notrack rule #2"
+               meta l4proto udp counter notrack comment "!fw4: Notrack rule #2"
+       }
+
+       chain notrack_zone3 {
+               meta l4proto tcp counter notrack comment "!fw4: Notrack rule #3"
+               meta l4proto udp counter notrack comment "!fw4: Notrack rule #3"
+       }
+
+
+       #
+       # Mangle rules
+       #
+
+       chain mangle_prerouting {
+               type filter hook prerouting priority mangle; policy accept;
+       }
+
+       chain mangle_postrouting {
+               type filter hook postrouting priority mangle; policy accept;
+       }
+
+       chain mangle_input {
+               type filter hook input priority mangle; policy accept;
+       }
+
+       chain mangle_output {
+               type route hook output priority mangle; policy accept;
+       }
+
+       chain mangle_forward {
+               type filter hook forward priority mangle; policy accept;
+       }
+}
+-- End --