Firewalls & Network SecurityLAN Networks

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.

  • Category: Firewalls & Network Security · LAN Networks · Tutorials
  • Published:
  • Editorial team: Alba Technology Academy
  • Reading time: about 9 minutes

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.

Outbound NAT with port translation The PC 192.168.1.10 sends a packet from port 51514 to the server 198.51.100.80 on port 443. As it leaves through the WAN, the firewall replaces the source with its own public address 203.0.113.25 and port 61033 and records the translation in the state table. The server's reply arrives at 203.0.113.25 port 61033 and the firewall turns it back into 192.168.1.10 port 51514. Outbound NAT: the firewall rewrites the source address and port PC192.168.1.10LAN OPNsenseoutbound NATLANWAN203.0.113.25 Server198.51.100.80Internet src 192.168.1.10:51514dst 198.51.100.80:443 src 203.0.113.25:61033dst 198.51.100.80:443 src 198.51.100.80:443dst 203.0.113.25:61033 src 198.51.100.80:443dst 192.168.1.10:51514 outgoingoutgoing, translated replyreply, translated back Firewall state table192.168.1.10:51514 ⇄ 203.0.113.25:61033 ⇄ 198.51.100.80:443 (TCP) Example addresses: private RFC 1918, public reserved for documentation RFC 5737
Figure 1 — The firewall replaces the source address and port on the way out and, thanks to the state table, restores the original ones on the reply.

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.

ProtocolInternal (address:port)Translated on the WANRemote
TCP192.168.1.10:51514203.0.113.25:61033198.51.100.80:443
TCP192.168.1.11:51514203.0.113.25:57210198.51.100.80:443
UDP192.168.1.12:50877203.0.113.25:64415198.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.

Inbound port forwarding The Internet client 198.51.100.44 connects to 203.0.113.25 on port 443. The firewall's port forwarding rule replaces the destination with 192.168.20.10 port 443, and the associated rule on the WAN interface lets the packet through to the server in the DMZ. The reply goes back with source 203.0.113.25. A connection to port 22, for which there is no forwarding, is blocked. Port forwarding: the firewall rewrites the destination Server192.168.20.10DMZ OPNsenseWAN203.0.113.25DMZ 1 · Port Forward→ 192.168.20.10:443 2 · WAN rulePass TCP → .20.10:443 Client198.51.100.44Internet src 198.51.100.44:40112dst 203.0.113.25:443 src 198.51.100.44:40112dst 192.168.20.10:443 src 192.168.20.10:443dst 198.51.100.44:40112 src 203.0.113.25:443dst 198.51.100.44:40112 requestrequest, forwarded replyreply, translated ✕ 198.51.100.44 → 203.0.113.25:22no port forwarding and no rule: blocked on the WAN The firewall translates first and filters afterwards: the WAN rule uses the internal address Example addresses: private RFC 1918, public reserved for documentation RFC 5737
Figure 2 — With port forwarding the firewall replaces the public destination with that of the internal server; ports without forwarding stay closed.

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.

Public address, double NAT and CGNAT Three cases. With a public address on the firewall's WAN, port forwarding works. With double NAT the firewall's WAN has a private address behind the provider's router and two forwardings are needed. With CGNAT the WAN has a 100.64.0.0/10 address and the provider's NAT, which the customer cannot configure, stops incoming connections. Where the translation happens Public OPNsenseWAN 203.0.113.25 Internet ✓ forwarding Double NAT OPNsenseWAN 192.168.100.2 Provider's router192.168.100.1 · 203.0.113.25 Internet twoforwardings CGNAT OPNsenseWAN 100.64.12.34 Provider's NATshared, not configurable Internet ✕ forwarding 100.64.0.0/10: shared address space for CGNAT (RFC 6598). Example addresses.
Figure 3 — Port forwarding only works if incoming connections reach your own firewall: with double NAT two forwardings are needed, with CGNAT no forwarding is possible.

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

NAT and Port Forwarding: How a Private Network Talks to the Internet — Animation without voice-over, with on-screen text in English and sound effects.

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.

OPNsense® is a registered trademark. Alba Technology does not represent the OPNsense project.