Testing various option constraints.

-- Testcase --
{%
	include("./root/usr/share/firewall4/main.uc", {
		getenv: function(varname) {
			switch (varname) {
			case 'ACTION':
				return 'print';
			}
		}
	})
%}
-- End --

-- File uci/helpers.json --
{}
-- End --

-- File uci/firewall.json --
{
	"zone": [
		{
			".description": "A zone matching only IPv4 subnets is assumed to be an IPv4 only zone",
			"name": "ipv4only",
			"subnet": "192.168.1.0/24",
			"auto_helper": 0
		},

		{
			".description": "A zone with conflicting family and subnet settings should be skipped",
			"name": "afconflict",
			"subnet": "10.0.0.0/8",
			"family": "IPv6",
			"auto_helper": 0
		}
	],
	"ipset": [
		{
			"name": "ipv4set",
			"match": "src_ip",
			"entry": [
				"10.0.0.2",
				"10.0.0.3",
				"10.0.0.4"
			]
		}
	],
	"rule": [
		{
			".description": "Rules referencing an IPv4 only zone should be restricted to IPv4 themselves",
			"src": "ipv4only",
			"proto": "tcp",
			"dest_port": "22",
			"name": "Rule #1",
			"target": "accept"
		},

		{
			".description": "Rules whose family conflicts with their addresses should be skipped",
			"proto": "tcp",
			"src_ip": "10.0.0.1",
			"dest_port": "22",
			"name": "Rule #2",
			"target": "accept",
			"family": "IPv6"
		},

		{
			".description": "Rules whose family conflicts with the zone family should be skipped",
			"src": "ipv4only",
			"proto": "tcp",
			"dest_port": "22",
			"name": "Rule #3",
			"target": "accept",
			"family": "IPv6"
		},

		{
			".description": "Rules whose family conflicts with the referenced set family should be skipped",
			"src": "ipv4only",
			"proto": "tcp",
			"ipset": "ipv4set",
			"name": "Rule #4",
			"target": "accept",
			"family": "IPv6"
		}
	],
	"redirect": [
		{
			".description": "Redirects whose family conflicts with the referenced zone family should be skipped",
			"src": "ipv4only",
			"proto": "tcp",
			"src_dport": "22",
			"dest_ip": "fdca::1",
			"name": "Redirect #1",
			"target": "dnat"
		},
	],
	"nat": [
		{
			".description": "NAT rules whose family conflicts with the referenced zone family should be skipped",
			"name": "NAT #1",
			"family": "ipv6",
			"src": "ipv4only",
			"target": "masquerade"
		},

		{
			".description": "NAT rules whose family conflicts with their addresses should be skipped",
			"name": "NAT #2",
			"family": "ipv4",
			"src": "*",
			"src_ip": "fc00::/7",
			"target": "masquerade"
		},

		{
			".description": "NAT rules without any AF specific bits and unspecified family should default to IPv4 for backwards compatibility",
			"name": "NAT #3",
			"src": "*",
			"target": "masquerade"
		},

		{
			".description": "NAT rules without explicit family but IPv6 specific bits should be IPv6",
			"name": "NAT #4",
			"src": "*",
			"src_ip": "fc00::/7",
			"target": "masquerade"
		},


		{
			".description": "NAT rules with explicit family any should inherit zone restrictions",
			"name": "NAT #5",
			"src": "ipv4only",
			"target": "masquerade"
		},

		{
			".description": "NAT rules without any AF specific bits but explicit family any should be IPv4/IPv6",
			"name": "NAT #6",
			"family": "any",
			"src": "*",
			"target": "masquerade"
		}
	]
}
-- End --

-- Expect stderr --
[!] Section @zone[1] (afconflict) is restricted to IPv6 but referenced subnet list is IPv4 only, skipping
[!] Section @rule[1] (Rule #2) is restricted to IPv6 but referenced source IP is IPv4 only, skipping
[!] Section @rule[2] (Rule #3) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
[!] Section @rule[3] (Rule #4) is restricted to IPv6 but referenced set match is IPv4 only, skipping
[!] Section @redirect[0] (Redirect #1) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
[!] Section @nat[0] (NAT #1) is restricted to IPv6 but referenced source zone is IPv4 only, skipping
[!] Section @nat[1] (NAT #2) is restricted to IPv4 but referenced source IP is IPv6 only, skipping
-- End --

-- Expect stdout --
table inet fw4
flush table inet fw4

table inet fw4 {
	#
	# Set definitions
	#

	set ipv4set {
		type ipv4_addr
		elements = {
			10.0.0.2,
			10.0.0.3,
			10.0.0.4,
		}
	}


	#
	# Defines
	#

	define ipv4only_subnets = { 192.168.1.0/24 }

	#
	# 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"
		meta nfproto ipv4 ip saddr 192.168.1.0/24 jump input_ipv4only comment "!fw4: Handle ipv4only IPv4 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"
		meta nfproto ipv4 ip saddr 192.168.1.0/24 jump forward_ipv4only comment "!fw4: Handle ipv4only IPv4 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"
		meta nfproto ipv4 ip daddr 192.168.1.0/24 jump output_ipv4only comment "!fw4: Handle ipv4only IPv4 output traffic"
	}

	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_ipv4only {
		meta nfproto ipv4 tcp dport 22 counter accept comment "!fw4: Rule #1"
		ct status dnat accept comment "!fw4: Accept port redirections"
		jump drop_from_ipv4only
	}

	chain output_ipv4only {
		jump drop_to_ipv4only
	}

	chain forward_ipv4only {
		ct status dnat accept comment "!fw4: Accept port forwards"
		jump drop_to_ipv4only
	}

	chain drop_from_ipv4only {
		meta nfproto ipv4 ip saddr 192.168.1.0/24 counter drop comment "!fw4: drop ipv4only IPv4 traffic"
	}

	chain drop_to_ipv4only {
		meta nfproto ipv4 ip daddr 192.168.1.0/24 counter drop comment "!fw4: drop ipv4only IPv4 traffic"
	}


	#
	# NAT rules
	#

	chain dstnat {
		type nat hook prerouting priority dstnat; policy accept;
		meta nfproto ipv4 ip saddr 192.168.1.0/24 jump dstnat_ipv4only comment "!fw4: Handle ipv4only IPv4 dstnat traffic"
	}

	chain srcnat {
		type nat hook postrouting priority srcnat; policy accept;
		meta nfproto ipv4 masquerade comment "!fw4: NAT #3"
		ip6 saddr fc00::/7 masquerade comment "!fw4: NAT #4"
		masquerade comment "!fw4: NAT #6"
		meta nfproto ipv4 ip daddr 192.168.1.0/24 jump srcnat_ipv4only comment "!fw4: Handle ipv4only IPv4 srcnat traffic"
	}

	chain dstnat_ipv4only {
	}

	chain srcnat_ipv4only {
		meta nfproto ipv4 masquerade comment "!fw4: NAT #5"
	}


	#
	# Raw rules (notrack & helper)
	#

	chain raw_prerouting {
		type filter hook prerouting priority raw; policy accept;
	}

	chain raw_output {
		type filter hook output priority raw; policy accept;
	}


	#
	# 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 filter hook output priority mangle; policy accept;
	}

	chain mangle_forward {
		type filter hook forward priority mangle; policy accept;
	}
}
-- End --
