MikroTik Port Forwarding (DST-NAT) and Hairpin NAT Generator
Expose internal server/NVR ports to the public internet (DST-NAT) and eliminate local loopback timeouts using Hairpin NAT.
Input Parameters
Calculation Results
- wanIface
- ether1-ISP
- pubIp
- 203.142.77.10
- extPort
- 8000
- intIp
- 192.168.88.250
- intPort
- 8000
- proto
- tcp
- lanCidr
- 192.168.88.0/24
- desc
- NVR-CCTV-Server
- enableHairpin
- true
- enableFilterRule
- true
- dstNatRule
- add action=dst-nat chain=dstnat dst-address=203.142.77.10 dst-port=8000 protocol=tcp to-addresses=192.168.88.250 to-ports=8000 comment="Port Forward NVR-CCTV-Server (TCP 8000 -> 192.168.88.250:8000)"
- hairpinNatRule
- add action=masquerade chain=srcnat dst-address=192.168.88.250 dst-port=8000 out-interface-list=all protocol=tcp src-address=192.168.88.0/24 comment="Hairpin NAT Loopback NVR-CCTV-Server (TCP)"
- filterAcceptRule
- add action=accept chain=forward connection-nat-state=dstnat dst-address=192.168.88.250 dst-port=8000 protocol=tcp comment="Allow Forwarded Port NVR-CCTV-Server (TCP)"
# ===================================================================== # InfraCalc.id - Port Forwarding (DST-NAT) + Hairpin NAT (NVR-CCTV-Server) # Target: 203.142.77.10:8000 -> 192.168.88.250:8000 [TCP] # ===================================================================== /ip firewall nat # 1. DST-NAT: Arahkan trafik dari IP Publik / WAN ke IP Server Lokal add action=dst-nat chain=dstnat dst-address=203.142.77.10 dst-port=8000 protocol=tcp to-addresses=192.168.88.250 to-ports=8000 comment="Port Forward NVR-CCTV-Server (TCP 8000 -> 192.168.88.250:8000)" # 2. HAIRPIN NAT: Agar saat staf di jaringan lokal (192.168.88.0/24) mengakses IP Publik/Domain, tidak mental/timeout add action=masquerade chain=srcnat dst-address=192.168.88.250 dst-port=8000 out-interface-list=all protocol=tcp src-address=192.168.88.0/24 comment="Hairpin NAT Loopback NVR-CCTV-Server (TCP)" # 3. FIREWALL FILTER: Izinkan trafik forward hasil DST-NAT melewati firewall /ip firewall filter add action=accept chain=forward connection-nat-state=dstnat dst-address=192.168.88.250 dst-port=8000 protocol=tcp comment="Allow Forwarded Port NVR-CCTV-Server (TCP)"
How to Use This Tool
- Enter your public IP (or select your WAN interface), external internet-facing port, and the target internal server IP and port.
- Select the transport protocol (TCP, UDP, or Both), define your local LAN subnet CIDR (e.g. 192.168.88.0/24), and provide a descriptive label.
- Review the generated Destination NAT, Hairpin NAT loopback, and firewall filter rules, then copy the script into your MikroTik RouterOS terminal.
Worked Example: CCTV NVR Camera Port 8000
Computed example: Forwarding external port 8000 (TCP) at public IP 203.142.77.10 to internal host 192.168.88.250:8000 (NVR-CCTV-Server) alongside a Hairpin NAT loopback rule for LAN subnet 192.168.88.0/24.
{
"desc": "NVR-CCTV-Server",
"pubIp": "203.142.77.10",
"extPort": "8000",
"intIp": "192.168.88.250",
"intPort": "8000",
"proto": "tcp",
"lanCidr": "192.168.88.0/24",
"wanIface": "ether1-ISP"
}- wanIface:
- ether1-ISP
- pubIp:
- 203.142.77.10
- extPort:
- 8000
- intIp:
- 192.168.88.250
- intPort:
- 8000
- proto:
- tcp
- lanCidr:
- 192.168.88.0/24
- desc:
- NVR-CCTV-Server
- enableHairpin:
- true
- enableFilterRule:
- true
- dstNatRule:
- add action=dst-nat chain=dstnat dst-address=203.142.77.10 dst-port=8000 protocol=tcp to-addresses=192.168.88.250 to-ports=8000 comment="Port Forward NVR-CCTV-Server (TCP 8000 -> 192.168.88.250:8000)"
- hairpinNatRule:
- add action=masquerade chain=srcnat dst-address=192.168.88.250 dst-port=8000 out-interface-list=all protocol=tcp src-address=192.168.88.0/24 comment="Hairpin NAT Loopback NVR-CCTV-Server (TCP)"
- filterAcceptRule:
- add action=accept chain=forward connection-nat-state=dstnat dst-address=192.168.88.250 dst-port=8000 protocol=tcp comment="Allow Forwarded Port NVR-CCTV-Server (TCP)"
Core Concept & Engineering Notes
How It Was Calculated (Formulas & Steps)
Mathematical Formulas
- DST-NAT: chain=dstnat dst-address=Public_IP dst-port=External_Port action=dst-nat to-addresses=Internal_IP to-ports=Internal_Port
- Hairpin NAT: chain=srcnat src-address=LAN_Subnet dst-address=Internal_IP dst-port=Internal_Port action=masquerade
- Filter Forward Accept: chain=forward connection-nat-state=dstnat dst-address=Internal_IP dst-port=Internal_Port action=accept
Step-by-Step Calculation
- Destination NAT (DST-NAT): Forward incoming TCP port 8000 at 203.142.77.10 to internal host 192.168.88.250:8000
- Hairpin NAT: Masquerade source address from LAN 192.168.88.0/24 accessing 192.168.88.250 so replies return through the router instead of direct ARP
- Firewall Filter: Forward accept rule allows state=dstnat packets to reach 192.168.88.250:8000
Frequently Asked Questions (FAQ)
Why is my CCTV or internal server accessible from the internet but times out when connected to office Wi-Fi?
This is the classic symptom of missing Hairpin NAT. Internal clients reach out to the router's public IP; the local server replies directly over the local switch, which the client drops because the replying source IP does not match the public IP it contacted.
Do I need to specify a static Public IP if my router uses dynamic PPPoE or DHCP client?
If your public IP is dynamic, you can substitute the `dst-address` match condition with `in-interface=ether1-ISP` or `in-interface=pppoe-out1` in your RouterOS DST-NAT configuration rule.
Can the external public port differ from the internal server port?
Yes. Port translation allows you to expose public port 8080 on the WAN while mapping it to standard port 80 on your internal local web server.
Is port forwarding secure against internet attacks?
Any forwarded port is visible to public internet automated scanners. We strongly recommend changing well-known default ports (e.g. moving RDP 3389 to a high random port), using strong credentials, or accessing sensitive servers through a WireGuard VPN instead.
Related Calculators
Multi-WAN (2–5 ISP) proportional PCC load balancing and recursive failover configuration script generator for RouterOS v7.
Calculate Limit-At (CIR), Max-Limit (MIR), Burst, PCQ, and FQ-CoDel for RouterOS v7 with automated configuration script generation.
Calculate IPv4 network address, broadcast, usable host range, netmask, wildcard, binary mask, and RFC address type (RFC 1918, /31, CGNAT).
References & Notes
- MikroTik Documentation — Destination NAT (Port Forwarding)
- MikroTik Documentation — Hairpin NAT (NAT Loopback / Reflection)
Note: Results above are mathematical estimates based on standard formulas. Always verify against official vendor documentation and site conditions before production deployment.