--- /dev/null
+#!/bin/sh
+#set -x
+
+
+REAL_PATH=$(realpath $(dirname $0))
+
+################################
+# definitions
+################################
+source $REAL_PATH/definitions
+
+
+################################
+# nat PREROUTING rules
+# dnat openvpn packets
+# from the internet
+# to the internal vpn host
+$IPTABLES --table nat --append PREROUTING \
+ --match comment \
+ --comment "openvpn -> internal vpn host" \
+ --protocol udp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --destination $PUBLIC_IP_202 \
+ --destination-port 1194 \
+ --jump DNAT --to-destination $VPN_INTERNAL_IP
+# dnat smtp packets
+# from the internet
+# to the perimeter svc host
+$IPTABLES --table nat --append PREROUTING \
+ --match comment \
+ --comment "smtp -> perimeter service host" \
+ --protocol tcp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --destination $PUBLIC_IP_202 \
+ --destination-port 25 \
+ --jump DNAT --to-destination $SVC_PERIMETER_IP
+# dnat dns packets
+# from the internet
+# to the perimeter external name server host
+$IPTABLES --table nat --append PREROUTING \
+ --match comment \
+ --comment "dns (udp) -> perimeter name server host" \
+ --protocol udp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --destination $PUBLIC_IP_202 \
+ --destination-port 53 \
+ --jump DNAT --to-destination $ENS_PERIMETER_IP
+# dnat dns packets
+# from the internet
+# to the perimeter external name server host
+$IPTABLES --table nat --append PREROUTING \
+ --match comment \
+ --comment "dns (tcp) -> perimeter name server host" \
+ --protocol tcp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --destination $PUBLIC_IP_202 \
+ --destination-port 53 \
+ --jump DNAT --to-destination $ENS_PERIMETER_IP
+# dnat http and https packets
+# from the internet
+# to the perimeter web server host
+$IPTABLES --table nat --append PREROUTING \
+ --match comment \
+ --comment "http, https -> perimeter www host" \
+ --protocol tcp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --destination $PUBLIC_IP_202 \
+ --match multiport \
+ --destination-ports 80,443 \
+ --jump DNAT --to-destination $WS_PERIMETER_IP
+## dnat http and https packets
+## from the internet
+## to the perimeter whmcs host
+#$IPTABLES --table nat --append PREROUTING \
+# --match comment \
+# --comment "http, https -> perimeter whmcs host" \
+# --protocol tcp \
+# --in-interface $EXTERNAL_IF_194 \
+# --destination $PUBLIC_IP \
+# --match multiport \
+# --destination-ports 80,443 \
+# --jump DNAT --to-destination $WHMCS_PERIMETER_IP
+
+################################
+# filter INPUT rules
+# allow dns replies
+# from the perimeter ns host
+$IPTABLES --table filter --append INPUT \
+ --match comment \
+ --comment "dns replies <- pns.pm" \
+ --match conntrack \
+ --ctstate ESTABLISHED \
+ --protocol udp \
+ --in-interface $PERIMETER_IF \
+ --source $PNS_PERIMETER_IP \
+ --source-port 53 \
+ --destination $EFG_PERIMETER_IP \
+ --destination-port 1024:65535 \
+ --jump ACCEPT
+# allow ssh packets
+# from the usr, sr and in networks
+$IPTABLES --table filter --append INPUT \
+ --match comment \
+ --comment "ssh from the usr, sr and in networks" \
+ --protocol tcp \
+ --in-interface $PERIMETER_IF \
+ --source ${USR_NET},${SR_NET},$IN_NET \
+ --source-port 1024:65535 \
+ --destination $EFG_PERIMETER_IP \
+ --destination-port 22 \
+ --jump ACCEPT
+# allow icmp packets
+# from anywhere
+$IPTABLES --table filter --append INPUT \
+ --protocol icmp \
+ --jump ACCEPT
+# allow packets
+# from the loopback address
+# to the loopback address
+$IPTABLES --table filter --append INPUT \
+ --in-interface $LOOPBACK_IF \
+ --source $LOOPBACK_IP \
+ --destination $LOOPBACK_IP \
+ --jump ACCEPT
+
+################################
+# filter FORWARD rules
+# forward packets
+# of established sessions
+# from the internet
+# to the usr, sr and in networks
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "established sessions to the usr, sr and in networks" \
+ --match conntrack \
+ --ctstate ESTABLISHED,RELATED \
+ --in-interface ${EXTERNAL_IF_PREFIX}+ \
+ --out-interface $PERIMETER_IF \
+ --destination ${USR_NET},${SR_NET},$IN_NET \
+ --jump ACCEPT
+# forward packets
+# from the usr, sr and in networks
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "from the usr, sr and in networks" \
+ --in-interface $PERIMETER_IF \
+ --source ${USR_NET},${SR_NET},$IN_NET \
+ --out-interface ${EXTERNAL_IF_PREFIX}+ \
+ --jump ACCEPT
+# forward openvpn packets
+# from the internet
+# to the internal vpn host
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "openvpn -> internal openvpn host" \
+ --protocol udp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --out-interface $PERIMETER_IF \
+ --destination $VPN_INTERNAL_IP \
+ --destination-port 1194 \
+ --jump ACCEPT
+# forward smtp and dns requests
+# from the internet
+# to the perimeter svc host
+#$IPTABLES --table filter --append FORWARD \
+# --match comment \
+# --comment "smtp, dns -> perimeter service host" \
+# --protocol tcp \
+# --in-interface $EXTERNAL_IF_STATIC \
+# --out-interface $PERIMETER_IF \
+# --destination $SVC_PERIMETER_IP \
+# --match multiport \
+# --destination-ports 25,53 \
+# --jump ACCEPT
+# forward smtp and dns replies
+# of established sessions
+# from the perimeter svc host
+# to the internet
+#$IPTABLES --table filter --append FORWARD \
+# --match comment \
+# --comment "perimeter service host smtp, dns replies" \
+# --protocol tcp \
+# --match conntrack \
+# --ctstate ESTABLISHED \
+# --in-interface $PERIMETER_IF \
+# --source $SVC_PERIMETER_IP \
+# --match multiport \
+# --source-ports 25,53 \
+# --out-interface $EXTERNAL_IF_STATIC \
+# --jump ACCEPT
+# forward http and https requests
+# from the internet
+# to the perimeter web server host
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "http, https -> perimeter web server host" \
+ --protocol tcp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --out-interface $PERIMETER_IF \
+ --destination $WS_PERIMETER_IP \
+ --match multiport \
+ --destination-ports 80,443 \
+ --jump ACCEPT
+# forward http and https replies
+# of established sessions
+# from the perimeter web server host
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "perimeter web server host http, https replies" \
+ --protocol tcp \
+ --match conntrack \
+ --ctstate ESTABLISHED \
+ --in-interface $PERIMETER_IF \
+ --source $WS_PERIMETER_IP \
+ --match multiport \
+ --source-ports 80,443 \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --jump ACCEPT
+## forward http and https requests
+## from the internet
+## to the perimeter whmcs host
+#$IPTABLES --table filter --append FORWARD \
+# --match comment \
+# --comment "http, https -> perimeter whmcs host" \
+# --protocol tcp \
+# --in-interface $EXTERNAL_IF_194 \
+# --out-interface $PERIMETER_IF \
+# --destination $WHMCS_PERIMETER_IP \
+# --match multiport \
+# --destination-ports 80,443 \
+# --jump ACCEPT
+## forward http and https replies
+## of established sessions
+## from the perimeter whmcs host
+## to the internet
+#$IPTABLES --table filter --append FORWARD \
+# --match comment \
+# --comment "perimeter whmcs host http, https replies" \
+# --protocol tcp \
+# --match conntrack \
+# --ctstate ESTABLISHED \
+# --in-interface $PERIMETER_IF \
+# --source $WHMCS_PERIMETER_IP \
+# --match multiport \
+# --source-ports 80,443 \
+# --out-interface $EXTERNAL_IF_194 \
+# --jump ACCEPT
+# forward dns requests and notifications
+# from the internet
+# to the external name server host
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "dns requests from internet (udp) -> ens.pm" \
+ --protocol udp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --source-port 1024:65535 \
+ --out-interface $PERIMETER_IF \
+ --destination $ENS_PERIMETER_IP \
+ --destination-port 53 \
+ --jump ACCEPT
+# forward dns replies
+# from the external name server host
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "dns replies from ens.pm (udp) -> internet" \
+ --match conntrack \
+ --ctstate ESTABLISHED,RELATED \
+ --protocol udp \
+ --in-interface $PERIMETER_IF \
+ --source $ENS_PERIMETER_IP \
+ --source-port 53 \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --destination-port 1024:65535 \
+ --jump ACCEPT
+# forward dns requests
+# from the external and perimeter name server hosts
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "dns requests from (e|p)ns.pm (udp) -> internet" \
+ --protocol udp \
+ --in-interface $PERIMETER_IF \
+ --source ${ENS_PERIMETER_IP},$PNS_PERIMETER_IP \
+ --source-port 1024:65535 \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --destination-port 53 \
+ --jump ACCEPT
+# forward dns replies
+# from the internet
+# to the external and perimeter name server hosts
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "dns replies (udp) -> (e|p)ns.pm" \
+ --match conntrack \
+ --ctstate ESTABLISHED,RELATED \
+ --protocol udp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --source-port 53 \
+ --out-interface $PERIMETER_IF \
+ --destination ${ENS_PERIMETER_IP},$PNS_PERIMETER_IP \
+ --destination-port 1024:65535 \
+ --jump ACCEPT
+# forward dns requests
+# from the internet
+# to the external name server host
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "dns requests (tcp) -> ens.pm" \
+ --protocol tcp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --source-port 1024:65535 \
+ --out-interface $PERIMETER_IF \
+ --destination $ENS_PERIMETER_IP \
+ --destination-port 53 \
+ --jump ACCEPT
+# forward dns replies
+# of established sessions
+# from the external name server host
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "dns replies from ens.pm (tcp) -> internet" \
+ --match conntrack \
+ --ctstate ESTABLISHED \
+ --protocol tcp \
+ --in-interface $PERIMETER_IF \
+ --source $ENS_PERIMETER_IP \
+ --source-port 53 \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --destination-port 1024:65535 \
+ --jump ACCEPT
+# forward dns requests
+# from the external and perimeter name server hosts
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "dns requests from (e|p)ns.pm -> internet" \
+ --protocol tcp \
+ --in-interface $PERIMETER_IF \
+ --source ${ENS_PERIMETER_IP},$PNS_PERIMETER_IP \
+ --source-port 1024:65535 \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --destination-port 53 \
+ --jump ACCEPT
+# forward dns replies
+# of established sessions
+# from the internet
+# to the external and perimeter name server hosts
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "dns replies (tcp) -> (e|p)ns.pm" \
+ --match conntrack \
+ --ctstate ESTABLISHED \
+ --protocol tcp \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --source-port 53 \
+ --out-interface $PERIMETER_IF \
+ --destination ${ENS_PERIMETER_IP},$PNS_PERIMETER_IP \
+ --destination-port 1024:65535 \
+ --jump ACCEPT
+# forward smtp, dns, http and https requests
+# from the perimeter svc host
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --protocol tcp \
+ --in-interface $PERIMETER_IF \
+ --source $SVC_PERIMETER_IP \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --match multiport \
+ --destination-ports 25,53,80,443 \
+ --jump ACCEPT
+# forward smtp, dns, http and https replies
+# of established sessions
+# from the internet
+# to the perimeter svc host
+$IPTABLES --table filter --append FORWARD \
+ --protocol tcp \
+ --match conntrack \
+ --ctstate ESTABLISHED \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --match multiport \
+ --source-ports 25,53,80,443 \
+ --out-interface $PERIMETER_IF \
+ --destination $SVC_PERIMETER_IP \
+ --jump ACCEPT
+# forward http and https requests
+# from the perimeter web server host
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "perimeter ws host http, https requests (let's encrypt)" \
+ --protocol tcp \
+ --in-interface $PERIMETER_IF \
+ --source $WS_PERIMETER_IP \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --match multiport \
+ --destination-ports 80,443 \
+ --jump ACCEPT
+# forward http and https replies
+# of established sessions
+# from the internet
+# to the perimeter web server host
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "perimeter ws host http, https replies (let's encrypt)" \
+ --protocol tcp \
+ --match conntrack \
+ --ctstate ESTABLISHED \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --match multiport \
+ --source-ports 80,443 \
+ --out-interface $PERIMETER_IF \
+ --destination $WS_PERIMETER_IP \
+ --jump ACCEPT
+# forward http and https requests
+# from the perimeter whmcs host
+# to the internet
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "perimeter whmcs host http, https requests (let's encrypt)" \
+ --protocol tcp \
+ --in-interface $PERIMETER_IF \
+ --source $WHMCS_PERIMETER_IP \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --match multiport \
+ --destination-ports 80,443 \
+ --jump ACCEPT
+# forward http and https replies
+# of established sessions
+# from the internet
+# to the perimeter whmcs host
+$IPTABLES --table filter --append FORWARD \
+ --match comment \
+ --comment "perimeter whmcs host http, https replies (let's encrypt)" \
+ --protocol tcp \
+ --match conntrack \
+ --ctstate ESTABLISHED \
+ --in-interface $EXTERNAL_IF_STATIC \
+ --match multiport \
+ --source-ports 80,443 \
+ --out-interface $PERIMETER_IF \
+ --destination $WHMCS_PERIMETER_IP \
+ --jump ACCEPT
+# forward icmp packets
+# from anywhere
+# to anywhere
+$IPTABLES --table filter --append FORWARD \
+ --protocol icmp \
+ --jump ACCEPT
+
+################################
+# filter OUTPUT rules
+# allow dns requests
+# to the perimeter name server host
+$IPTABLES --table filter --append OUTPUT \
+ --match comment \
+ --comment "dns requests -> pns.pm" \
+ --match conntrack \
+ --ctstate NEW \
+ --protocol udp \
+ --source $EFG_PERIMETER_IP \
+ --source-port 1024:65535 \
+ --out-interface $PERIMETER_IF \
+ --destination $PNS_PERIMETER_IP \
+ --destination-port 53 \
+ --jump ACCEPT
+# allow ssh packets
+# of established sessions
+# to the usr, sr and in networks
+$IPTABLES --table filter --append OUTPUT \
+ --protocol tcp \
+ --match conntrack \
+ --ctstate ESTABLISHED \
+ --source $EFG_PERIMETER_IP \
+ --source-port 22 \
+ --out-interface $PERIMETER_IF \
+ --destination ${USR_NET},${SR_NET},$IN_NET \
+ --jump ACCEPT
+# allow icmp packets
+# to anywhere
+$IPTABLES --table filter --append OUTPUT \
+ --protocol icmp \
+ --jump ACCEPT
+# allow packets
+# from the loopback address
+# to the loopback address
+$IPTABLES --table filter --append OUTPUT \
+ --source $LOOPBACK_IP \
+ --out-interface $LOOPBACK_IF \
+ --destination $LOOPBACK_IP \
+ --jump ACCEPT
+
+################################
+# nat POSTROUTING rules
+# snat packets
+# from the usr, sr and in networks
+$IPTABLES --table nat --append POSTROUTING \
+ --source ${USR_NET},${SR_NET},$IN_NET \
+ --out-interface $EXTERNAL_IF_DYNAMIC \
+ --jump SNAT --to-source $DYNAMIC_MANAGEMENT_IP
+# snat packets
+# from the usr, sr and in networks
+$IPTABLES --table nat --append POSTROUTING \
+ --source ${USR_NET},${SR_NET},$IN_NET \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --jump SNAT --to-source $PUBLIC_IP_202
+# snat packets
+# from the perimeter svc host
+$IPTABLES --table nat --append POSTROUTING \
+ --source $SVC_PERIMETER_IP \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --jump SNAT --to-source $PUBLIC_IP_202
+# snat packets
+# from the perimeter web server host
+$IPTABLES --table nat --append POSTROUTING \
+ --source $WS_PERIMETER_IP \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --jump SNAT --to-source $PUBLIC_IP_202
+# snat packets
+# from the perimeter whmcs host
+$IPTABLES --table nat --append POSTROUTING \
+ --source $WHMCS_PERIMETER_IP \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --jump SNAT --to-source $PUBLIC_IP_202
+# snat packets
+# from the perimeter name server host (perimeter network)
+$IPTABLES --table nat --append POSTROUTING \
+ --source $PNS_PERIMETER_IP \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --jump SNAT --to-source $PUBLIC_IP_202
+# snat packets
+# from the external name server host (perimeter network)
+$IPTABLES --table nat --append POSTROUTING \
+ --source $ENS_PERIMETER_IP \
+ --out-interface $EXTERNAL_IF_STATIC \
+ --jump SNAT --to-source $PUBLIC_IP_202