NAT and Port Forwarding: How a Private Network Talks to the Internet
Private and public addresses, port translation, the state table and port forwarding, with the OPNsense configuration, CGNAT and double NAT.
In a home or business network, dozens of devices browse the Internet, yet from the outside there is almost always a single public address. That is the work of NAT, the address translation that the router or firewall performs on every packet. The same mechanism explains why an internal server cannot be reached from the outside until port forwarding is configured. We will look at private and public addresses, outbound NAT, port forwarding and the OPNsense configuration, then NAT reflection, CGNAT, double NAT and IPv6.
Private and public addresses
On the Internet every IPv4 address must be unique, and IPv4 addresses are scarce: about 4.3 billion in total. For internal networks, RFC 1918 reserves three blocks of private addresses that anyone may use freely: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. The same addresses are repeated in millions of networks and are not routed on the Internet: a packet leaving with source 192.168.1.10 would have no way to receive the reply.
The Internet connection, on the other hand, receives a public address from the provider, unique and reachable from anywhere in the world. NAT (Network Address Translation) lets the traffic of the whole private network out through that single address. In the examples we will use private addresses for the internal network and, for the public side, the 203.0.113.0/24 and 198.51.100.0/24 blocks, which RFC 5737 reserves for documentation.
Outbound NAT: addresses and ports
Take a small network: the LAN 192.168.1.0/24, a PC with address 192.168.1.10 and an OPNsense firewall whose WAN interface has the public address 203.0.113.25. The PC opens an HTTPS page on the server 198.51.100.80. The first packet leaves with source 192.168.1.10, port 51514 (chosen at random by the operating system), and destination 198.51.100.80, port 443.
When the packet leaves through the WAN, the firewall replaces the source address with its own public address and, if needed, the source port too: the packet reaches the server with source 203.0.113.25, port 61033. The server replies to that address and port; the firewall recognises the reply, restores the original destination, 192.168.1.10 port 51514, and delivers it to the PC. Neither the PC nor the server notices.
Using ports as well allows many devices to share a single public address: this technique is called PAT (Port Address Translation), NAPT in the RFCs or, in the Linux world, masquerading. The pf packet filter, on which OPNsense is based, normally changes the source port even when it would not be necessary, making translated ports less predictable.
The translation table
To restore the original addresses, the firewall remembers every translation. In OPNsense, as in many stateful firewalls, this information is part of the state table: for each connection it holds the protocol, the internal address and port, the translated ones and the remote ones. Here are three simultaneous connections; two PCs happened to choose the same source port, but on the outside they get different ports and the firewall tells the replies apart without ambiguity.
| Protocol | Internal (address:port) | Translated on the WAN | Remote |
|---|---|---|---|
| TCP | 192.168.1.10:51514 | 203.0.113.25:61033 | 198.51.100.80:443 |
| TCP | 192.168.1.11:51514 | 203.0.113.25:57210 | 198.51.100.80:443 |
| UDP | 192.168.1.12:50877 | 203.0.113.25:64415 | 198.51.100.53:53 |
Entries stay while the connection is active, then expire after a period of inactivity. In OPNsense they can be viewed in Firewall › Diagnostics › States.
Why incoming connections do not get through
Now an Internet client, 198.51.100.44, tries to connect to 203.0.113.25 on port 443. There is no matching entry in the table, because no internal device opened that connection: the firewall does not know which private address should receive the packet. Moreover, there is no Pass rule on the OPNsense WAN interface, so the packet is dropped by the default block.
As a side effect, NAT provides some protection: internal devices cannot be reached directly. But the real defence is still the firewall. NAT was created to save addresses, not as a security measure.
Port forwarding: destination NAT
To publish an internal service you need a rule that tells the firewall what to do with incoming connections: this is port forwarding, or destination NAT (DNAT). Suppose we have a web server, 192.168.20.10, in a DMZ network connected to a firewall interface other than the LAN. The rule says: "TCP traffic arriving on the WAN, addressed to 203.0.113.25 port 443, is forwarded to 192.168.20.10 port 443".
When the request from 198.51.100.44 arrives, the firewall replaces the destination with 192.168.20.10 and delivers the packet to the server, which sees the client's real address. The server's reply goes back through the firewall, which uses the state table entry to put 203.0.113.25 back as the source. The external and internal ports can also differ: for example, public port 8443 forwarded to port 443 on the server.
In OPNsense: outbound NAT
Outbound NAT is managed in Firewall › NAT › Outbound. At the top you choose the mode:
- Automatic (the default): OPNsense generates the rules that translate the internal networks to the WAN interface address by itself.
- Hybrid: manual rules are added to the automatic ones and evaluated first. This is useful, for example, to make a server go out with a different public address or to keep the source port unchanged (Static port) for an application that needs it.
- Manual: only the rules written by the administrator apply. It offers complete control, but every new internal network needs its own rule, otherwise it does not reach the Internet.
- There is also a Disable mode, which turns outbound NAT off, useful when translation is done by another device or is not needed.
In OPNsense: port forwarding and the associated rule
Port forwarding is configured in Firewall › NAT › Port Forward. For the web server, create a rule with these fields:
- Interface: WAN, the interface where the connections arrive.
- Protocol: TCP.
- Destination: WAN address, that is 203.0.113.25; Destination port range: HTTPS (443).
- Redirect target IP: 192.168.20.10; Redirect target port: HTTPS (443).
- Filter rule association: Add associated filter rule.
Translation alone is not enough: the packet must also be allowed by the rules of the WAN interface, which would otherwise block it. With the association, OPNsense creates a Pass rule in Firewall › Rules › WAN linked to the forwarding and updates it when the forwarding changes. Note the destination of that rule: not the public address but 192.168.20.10, port 443. The firewall applies the translation before filtering, so the rules already see the internal address. If the service should only be reachable from certain sites or partners, restrict the Source field to the authorised addresses.
NAT reflection, or hairpin
From the outside the site works, but a LAN PC that opens the same public address, 203.0.113.25, gets nothing. The packet does not come in through the WAN, so the forwarding does not apply. The solution is called NAT reflection (or hairpin NAT): the firewall applies the forwarding to internal traffic addressed to its own public address as well. OPNsense offers it as an option of the individual rule and in the firewall's advanced settings. When client and server are on the same network, the source must be translated too, otherwise the server replies directly to the client and the connection is not established. Split DNS is often cleaner: the internal resolver answers the site's name with the server's private address, and internal traffic does not go through NAT at all.
When port forwarding cannot work: CGNAT and double NAT
Because of the shortage of IPv4 addresses, many providers use CGNAT (Carrier-Grade NAT): the customer's WAN receives an address from the shared address space 100.64.0.0/10, defined by RFC 6598, and the public address is shared with other customers on the provider's NAT. In this case port forwarding on your own firewall is useless: connections from the outside stop at the provider's NAT, which does not know where to forward them and which the customer cannot configure. To recognise it, compare the WAN address with the public address you appear with on the Internet: if they differ, there is a NAT upstream. The possible ways forward are asking the provider for a public address, where offered, using IPv6, or an outbound tunnel to a server with a public address.
Double NAT, instead, occurs when the firewall sits behind the provider's router and its WAN receives a private address, for example 192.168.100.2. There are two translations, so there must be two forwardings as well: on the provider's router towards the firewall's WAN, then on the firewall towards the server. When the router allows it, it is better to set it to bridge mode, so the firewall receives the public address directly and the chain becomes simpler.
The risks of exposing a service, and the alternatives
Every forwarded port can be reached from the entire Internet, where automated scans constantly look for vulnerable services. A few rules of caution:
- never expose management interfaces: the firewall's web interface, remote desktop, NAS and camera administration;
- place published servers in a DMZ, separated from the LAN by firewall rules, so that a compromised server cannot reach the PCs;
- keep the service updated, restrict the sources where possible and enable logging on the associated rule.
For staff or administrator access, the best alternative is almost always a VPN: only one port is exposed, the VPN's, with strong authentication, and internal services stay closed to the Internet. OPNsense supports WireGuard, OpenVPN and IPsec. A VPN too, however, requires incoming connections to reach the firewall: behind CGNAT one of the solutions above is still needed.
IPv6: addresses for everyone, without NAT
With IPv6 the original problem disappears: addresses are so plentiful that every device gets a global address and NAT is not needed. That does not mean devices are exposed: the stateful firewall still blocks unsolicited incoming connections. To publish a service you do not set up port forwarding; instead you write a Pass rule on the WAN towards the server's IPv6 address and the service port.
Conclusions
Outbound NAT lets an entire private network use a single public address, translating addresses and ports and remembering each translation in the state table. For the same reason, connections from the outside cannot find their way in until a port forwarding rule names the destination server and a firewall rule allows them. In OPNsense the automatic mode in Firewall › NAT › Outbound and a rule in Firewall › NAT › Port Forward with its associated rule are usually enough. Before opening a port, however, check that the WAN really has a public address and consider a VPN.
The video
Further reading
- Book: LOCAL AREA NETWORKS (LAN) — Volume 4: Routing, NAT, and Internet Access (English edition)
- Book: OPNsense® in Practice — Volume 1: The Foundations of a Professional Journey (English edition)
- Video course: Capire i Firewall con OPNsense (video course in Italian)
- Video course: Reti LAN da Zero: Switch, IP, Router, Wi-Fi e Internet (video course in Italian)
- Related article: How a Firewall Rule Works
- Related article: VLANs: Why Separating Networks Improves Security and Organisation
- Specialization: Firewalls & Network Security
Technical sources
- RFC 1918 — Private addresses for internal networks (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
- RFC 3022 — Traditional NAT: basic address translation and network address and port translation (NAPT).
- RFC 6598 — Shared address space 100.64.0.0/10 for carrier-grade NAT (CGNAT).
- RFC 5737 — IPv4 address blocks reserved for documentation (203.0.113.0/24 and 198.51.100.0/24, used in the examples).
- Documentazione ufficiale di OPNsense — Firewall › NAT — Port Forward (destination, redirect target, filter rule association, NAT reflection) and outbound NAT modes (automatic, hybrid, manual, disabled).
- FreeBSD — pagina di manuale pf.conf(5) — nat and rdr translation rules evaluated before filter rules; the static-port option for the source port.
- IANA — Service Name and Transport Protocol Port Number Registry — Ports of the services mentioned: https 443, domain 53, ssh 22.
Standards and official documentation available from the respective organisations.