config defaults
	option synflood_protect	'1'
	option input		'ACCEPT'
	option output		'ACCEPT'
	option forward		'DROP'

config zone
	option name		'lan'
	list   network		'lan'
	option input		'ACCEPT'
	option output		'ACCEPT'
	option forward		'ACCEPT'
	option log		'0'

config zone
	option name		'wan'
	list   network		'wan'
	option input		'DROP'
	option output		'ACCEPT'
	option forward		'DROP'
	option masq		'1'
	option mtu_fix		'1'
	option log		'0'

config zone
	option name		'wwan'
	list   network		'wwan'
	list   network		'wwwan'
	option input		'DROP'
	option output		'ACCEPT'
	option forward		'DROP'
	option masq		'1'
	option mtu_fix		'1'

config zone
	option name		'vpn'
	option input		'ACCEPT'
	option output		'ACCEPT'
	option forward		'ACCEPT'
	option masq		'0'
	option network		'vpn'

config forwarding
	option src		'lan'
	option dest		'wan'

config forwarding
	option src		'vpn'
	option dest		'lan'

config forwarding
	option src		'vpn'
	option dest		'wwan'

config forwarding
	option src		'lan'
	option dest		'vpn'

# allow WAN to access ssh port
config rule
	option name		Allow-WAN-SSH
	option src		wan
	option dest_port	22
	option proto		tcp
	option target		ACCEPT

# allow WAN to access http port
config rule
	option name		Allow-WAN-HTTP
	option src		wan
	option dest_port	80
	option proto		tcp
	option target		ACCEPT

# allow WAN to access https port
config rule
	option name		Allow-WAN-HTTPS
	option src		wan
	option dest_port	443
	option proto		tcp
	option target		ACCEPT

# redirect wan interface SSH connections to Windows CNC PC
config redirect
	option src		wan
	option src_dport	9022
	option dest		lan
	option dest_ip		192.168.10.154
	option dest_port	22
	option proto		tcp
	option target		dnat

# redirect wan interface SSH connections to Linux CNC PC
config redirect
	option src		wan
	option src_dport	9122
	option dest		lan
	option dest_ip		192.168.10.155
	option dest_port	22
	option proto		tcp
	option target		dnat

# redirect wan interface RDP connections to Windows CNC PC
config redirect
	option src		wan
	option src_dport	3389
	option dest		lan
	option dest_ip		192.168.10.154
	option dest_port	3389
	option proto		tcp
	option target		dnat

# redirect wan interface RDP connections to Linux CNC PC
config redirect
	option src		wan
	option src_dport	3489
	option dest		lan
	option dest_ip		192.168.10.155
	option dest_port	3389
	option proto		tcp
	option target		dnat

# redirect wan interface MTConnect Adapter connections to Linux CNC PC
config redirect
	option src		wan
	option src_dport	11080
	option dest		lan
	option dest_ip		192.168.10.155
	option dest_port	5000
	option proto		tcp
	option target		dnat

# redirect wan interface MTConnect Adapter connections to Windows CNC PC
config redirect
	option src		wan
	option src_dport	12080
	option dest		lan
	option dest_ip		192.168.10.154
	option dest_port	5000
	option proto		tcp
	option target		dnat

# redirect wan interface HTTP connections to plasma web server
config redirect
	option src		wan
	option src_dport	8080
	option dest		lan
	option dest_ip		192.168.10.11
	option dest_port	80
	option proto		tcp
	option target		dnat

# redirect wan interface SSH connections to plasma sshd server
config redirect
	option src		wan
	option src_dport	8022
	option dest		lan
	option dest_ip		192.168.10.11
	option dest_port	22
	option proto		tcp
	option target		dnat

# redirect wan interface HTTPS connections to plasma web server
config redirect
	option src		wan
	option src_dport	8443
	option dest		lan
	option dest_ip		192.168.10.11
	option dest_port	443
	option proto		tcp
	option target		dnat

# redirect vpn interface SSH connections to Windows CNC PC
config redirect
	option src		vpn
	option src_dport	9022
	option dest		lan
	option dest_ip		192.168.10.154
	option dest_port	22
	option proto		tcp
	option target		dnat

# redirect vpn interface SSH connections to Linux CNC PC
config redirect
	option src		vpn
	option src_dport	9122
	option dest		lan
	option dest_ip		192.168.10.155
	option dest_port	22
	option proto		tcp
	option target		dnat

# redirect vpn interface RDP connections to Windows CNC PC
config redirect
	option src		vpn
	option src_dport	3389
	option dest		lan
	option dest_ip		192.168.10.154
	option dest_port	3389
	option proto		tcp
	option target		dnat

# redirect vpn interface RDP connections to Linux CNC PC
config redirect
	option src		vpn
	option src_dport	3489
	option dest		lan
	option dest_ip		192.168.10.155
	option dest_port	3389
	option proto		tcp
	option target		dnat

# redirect vpn interface HTTP connections to plasma web server
config redirect
	option src		vpn
	option src_dport	8080
	option dest		lan
	option dest_ip		192.168.10.11
	option dest_port	80
	option proto		tcp
	option target		dnat

# redirect vpn interface SSH (port 8022) connections to plasma (port 22)
config redirect
	option src		vpn
	option src_dport	8022
	option dest		lan
	option dest_ip		192.168.10.11
	option dest_port	22
	option proto		tcp
	option target		dnat

# redirect vpn interface HTTPS connections to plasma web server
config redirect
	option src		vpn
	option src_dport	8443
	option dest		lan
	option dest_ip		192.168.10.11
	option dest_port	443
	option proto		tcp
	option target		dnat

# We need to accept udp packets on port 68,
# see https://dev.openwrt.org/ticket/4108
config rule
	option name		Allow-DHCP-Renew-wan
	option src		wan
	option proto		udp
	option dest_port	68
	option target		ACCEPT
	option family		ipv4

config rule
	option name		Allow-DHCP-Renew-wwan
	option src		wwan
	option proto		udp
	option dest_port	68
	option target		ACCEPT
	option family		ipv4

# Allow IPv4 ping
config rule
	option name		Allow-Ping-wan
	option src		wan
	option proto		icmp
	option icmp_type	echo-request
	option family		ipv4
	option target		ACCEPT

config rule
	option name		Allow-Ping-wwan
	option src		wwan
	option proto		icmp
	option icmp_type	echo-request
	option family		ipv4
	option target		ACCEPT

config rule
	option name		Allow-IGMP-wan
	option src		wan
	option proto		igmp
	option family		ipv4
	option target		ACCEPT

	option name		Allow-IGMP-wwan
	option src		wwan
	option proto		igmp
	option family		ipv4
	option target		ACCEPT

config rule
	option name		Allow-IPSec-ESP-wan
	option src		wan
	option dest		lan
	option proto		esp
	option target		ACCEPT

	option name		Allow-IPSec-ESP-wwan
	option src		wwan
	option dest		lan
	option proto		esp
	option target		ACCEPT

config rule
	option name		Allow-ISAKMP-wan
	option src		wan
	option dest		lan
	option dest_port	500
	option proto		udp
	option target		ACCEPT

config rule
	option name		Allow-ISAKMP-wwan
	option src		wwan
	option dest		lan
	option dest_port	500
	option proto		udp
	option target		ACCEPT

# # allow LAN to access wwan's modem 1
# config rule
# 	option name		Allow-LAN-modem1
# 	option src		lan
# 	option dest_ip		192.168.1.1
# 	option proto		tcpudp
# 	option target		ACCEPT

# # allow LAN to access wwan's modem 8
# config rule
# 	option name		Allow-LAN-modem8
# 	option src		lan
# 	option dest_ip		192.168.8.1
# 	option proto		tcpudp
# 	option target		ACCEPT

config ipset 'rg_cnc'
	option name		rg_cnc
	option family		ipv4
	option storage		hash
	list match		src_ip
	option hashsize		64
	option maxelem		256
	option loadfile		/etc/rg/firewall/sets/cnc4.list
	option reload_set	1

config ipset 'rg_cnc_windows'
	option name		rg_cnc_windows
	option family		ipv4
	option storage		hash
	list match		src_ip
	option hashsize		64
	option maxelem		256
	option loadfile		/etc/rg/firewall/sets/cnc-windows4.list
	option reload_set	1

config ipset 'rg_plasma'
	option name		rg_plasma
	option family		ipv4
	option storage		hash
	list match		src_ip
	option hashsize		64
	option maxelem		256
	option loadfile		/etc/rg/firewall/sets/plasma4.list
	option reload_set	1

config ipset 'rg_kontur'
	option name		rg_kontur
	option family		ipv4
	option storage		hash
	list match		dest_net
	option hashsize		64
	option maxelem		256
	option loadfile		/etc/rg/firewall/sets/kontur4.list
	option reload_set	1

config ipset 'rg_dns_mesh'
	option name		rg_dns_mesh
	option family		ipv4
	option storage		hash
	list match		dest_ip
	option hashsize		64
	option maxelem		256
	option reload_set	0

config ipset 'rg_dns_git'
	option name		rg_dns_git
	option family		ipv4
	option storage		hash
	list match		dest_ip
	option hashsize		64
	option maxelem		256
	option reload_set	0

config ipset 'rg_repo_clients'
	option name		rg_repo_clients
	option family		ipv4
	option storage		hash
	list match		src_net
	option hashsize		64
	option maxelem		256
	option loadfile		/etc/rg/firewall/sets/repository-clients4.list
	option reload_set	1

config ipset 'rg_dns_repositories'
	option name		rg_dns_repositories
	option family		ipv4
	option storage		hash
	list match		dest_ip
	option hashsize		64
	option maxelem		256
	option reload_set	0

# allow PL (Plasma Source) to access wwan
config rule
	option name		Allow-Plasma-PL-wwan
	option src		lan
	option dest		wwan
	option family		ipv4
	option ipset		'rg_plasma src'
	option proto		tcpudp
	option target		ACCEPT

# shared CNC ports: TeamViewer, AnyDesk, Ammyy Admin and Kontur.Dostup
config rule
	option name		Allow-CNC-Remote-Ports-wwan
	option src		lan
	option dest		wwan
	option family		ipv4
	option ipset		'rg_cnc src'
	option proto		tcpudp
	# Kontur.Dostup: VNC
	list dest_port		3478
	# TeamViewer; Kontur.Dostup: HTTPS
	list dest_port		5938
	# Kontur.Dostup: WebSockets
	list dest_port		5939
	# AnyDesk
	list dest_port		6568
	# Ammyy Admin
	list dest_port		8080
	# Kontur.Dostup: HTTPS
	list dest_port		8443
	# Kontur.Dostup: WebSockets
	list dest_port		8444
	option target		ACCEPT

# allow CNC PCs to access MeshCentral (RG) over wwan
config rule
	option name		Allow-CNC-Mesh-443-wwan
	option src		lan
	option dest		wwan
	option family		ipv4
	option ipset		'rg_dns_mesh dest'
	option extra		'-m set --match-set rg_cnc src'
	option proto		tcpudp
	option dest_port	443
	option target		ACCEPT

# allow CNC PCs to access Kontur IPv4 networks over wwan
config rule
	option name		Allow-CNC-Kontur-ipv4-wwan
	option src		lan
	option dest		wwan
	option family		ipv4
	option ipset		'rg_kontur dest'
	option extra		'-m set --match-set rg_cnc src'
	option proto		all
	option target		ACCEPT

# allow Win to access wwan for Git repo over wwan
config rule
	option name		Allow-CNC-Git-wwan
	option src		lan
	option dest		wwan
	option family		ipv4
	option ipset		'rg_dns_git dest'
	option extra		'-m set --match-set rg_cnc_windows src'
	option proto		tcpudp
	list dest_port		22
	list dest_port		8022
	option target		ACCEPT

# public repositories and CRL: preserve the former proxy's LAN client scope
config rule
	option name		Allow-LAN-Public-Repositories-wwan
	option src		lan
	option dest		wwan
	option family		ipv4
	option ipset		'rg_dns_repositories dest'
	option extra		'-m set --match-set rg_repo_clients src'
	option proto		tcp
	# HTTP repositories and certificate revocation lists
	list dest_port		80
	# Direct HTTPS, with TLS terminated by the CNC client and repository server
	list dest_port		443
	option target		ACCEPT

# allow any hosts to ping wwan
config rule
	option name		Enable-ICMP-wwan
	option src		lan
	option dest		wwan
	option proto		icmp
	list icmp_type		echo-request
	list icmp_type		echo-reply
	list icmp_type		destination-unreachable
	list icmp_type		packet-too-big
	list icmp_type		time-exceeded
	list icmp_type		bad-header
	list icmp_type		unknown-header-type
	option limit		1000/sec
	option target		ACCEPT

config include 'rg_firewall_dns'
	option type		script
	option path		/lib/rg/firewall-dns/firewall.include
	option family		ipv4
	option reload		1

# include a file with users custom iptables rules
config include
	option fw4_compatible	1
	option path 		/etc/firewall.user
