NAT Rules
Source and destination Network Address Translation.
Open Policies > NAT to translate the source address, the destination address, or both, of IPv4 sessions. Each rule matches a session by its original (pre-NAT) packet and defines what the translated packet looks like.
A rule has two parts. Conditions select the sessions the rule applies to. NAT Configuration defines the translation.
How NAT rules apply
- The firewall checks NAT rules from top to bottom and applies the first rule whose conditions match. At most one NAT rule applies to a session. Put specific rules above broader ones.
- The first packet of a session decides its translation, which then stays fixed for the session's lifetime. Changing a rule does not affect existing sessions.
- A rule applies only while it is enabled. To limit it to certain hours, give it a schedule.
NAT is endpoint-independent: for a given internal source IP and port, the firewall reuses the same external IP and port whatever destination the client talks to. This suits NAT traversal techniques such as STUN, and peer-to-peer, VoIP, and gaming applications.
Conditions
A condition names a field and the objects to match it against. Within a rule:
- Multiple objects in one condition are combined with a logical OR.
- Different conditions are combined with a logical AND.
- A field without a condition matches any value.
- A condition can only match; negation is not supported.
Supported conditions
| Condition | Required | Objects |
|---|---|---|
| Source Zone | No | One or more |
| Destination Zone | Yes | Exactly one |
| Source IP Address | Only with Static IP SNAT | One or more. With Static IP SNAT, exactly one IP Address object. |
| Destination IP Address | Only with Static IP DNAT | One or more. With Static IP DNAT, exactly one IP Address object. |
| Source Port | No | One or more |
| Destination Port | No | One or more |
| IP Protocol | No | One or more |
| Destination Interface | No | Exactly one |
NAT is IPv4-only. IP Address objects and groups used in a NAT rule must contain only IPv4 values, and a group must not contain excluded objects.
Fields NAT cannot match
The first packet decides the translation, so a condition can use only fields found in that packet's L3 and L4 headers, or derived directly from them, such as the egress zone from a route lookup. The addresses a rule matches must also be fixed when the configuration is committed. As a result, NAT conditions do not accept:
- Application. Identifying an application requires the packet payload, which arrives after the translation is fixed.
- FQDN and other domain-based fields. Domain matching relies on payload fields such as SNI or the HTTP Host header, which the first packet does not carry.
- Country and ASN. A GeoIP database update would change which addresses match without a commit.
- IP feeds. A feed's members are fetched later and change without a commit.
Destination zone and destination interface
When a packet arrives, the firewall looks up the route for its original destination IP address, before any translation, to find the egress interface and its zone. The Destination Zone and Destination Interface conditions match the result of this lookup.
After DNAT rewrites the destination IP address, the firewall looks up the route again to find the actual egress interface and zone. Write conditions against the first lookup, not the second.
NAT configuration
A rule can perform:
- Source NAT (SNAT) only.
- Destination NAT (DNAT) only.
- Both, typically for hairpin NAT.
- Neither. Set both SNAT and DNAT to None. The rule still matches and counts hits but rewrites nothing, which is useful for checking that a rule matches as expected before putting it into service.
Translated addresses come from NAT Pool objects, which must exist before you create the rule. A NAT Pool is its own object type, separate from IP Address objects. Its items can be single IP addresses, CIDRs, or ranges.
Source NAT (SNAT)
SNAT rewrites the source IP address of a session. Its most common use is letting internal hosts share one or more public IP addresses when they access external networks. Choose one of the types below, or None to leave the source unchanged.
| Type | Rewrites | Translated address |
|---|---|---|
| Dynamic IP and Port | Source IP and source port | A NAT Pool address or an interface address, shared by many hosts |
| Dynamic IP | Source IP | A NAT Pool address, held by one host at a time |
| Static IP | Source IP | A fixed NAT Pool address for each host |
SNAT: Dynamic IP and Port
The firewall rewrites both the source IP and the source port, so many internal hosts can share one public IP address while their sessions stay distinct. Ports are allocated from the range 1024-65535 on the translated address.
Choose where the translated address comes from:
- Translated Address > NAT Pool: the firewall selects an address from the NAT Pool. While the NAT Pool is unchanged, an internal IP address always maps to the same translated IP address.
- Interface: the firewall uses the first IP address configured on the interface. If the interface uses DHCP or PPPoE and has no address yet, traffic matching the rule is dropped, and translation resumes once the interface obtains an address. An interface kept by the operating system, such as the management port, cannot be selected.
SNAT: Dynamic IP
The firewall rewrites only the source IP and leaves the source port unchanged. It picks the translated address from the NAT Pool. Once an address is assigned to an internal IP address, no other internal IP address can use it until all sessions of the first one have aged out.
SNAT: Static IP
The firewall rewrites only the source IP and leaves the source port unchanged, using a fixed one-to-one mapping between the Source IP Address condition and the NAT Pool. Select exactly one NAT Pool. See Static IP pairing.
Destination NAT (DNAT)
DNAT rewrites the destination IP address of a session, and optionally the destination port. Its most common use is publishing internal servers on a public IP address. Choose one of the types below, or None to leave the destination unchanged.
DNAT: Dynamic IP
The firewall selects the translated address from one or more NAT Pools, which distributes sessions across several backend servers. Choose a Session Distribution Method:
| Session Distribution Method | How the address is selected |
|---|---|
| Round Robin | In turn, one address after another. |
| Source IP Hash | A hash of the original source IP, modulo the number of pool addresses. |
| IP Modulo | The bitwise XOR of the original source IP and destination IP, modulo the number of pool addresses. |
| IP Hash | A hash of the original source IP and destination IP together, modulo the number of pool addresses. |
DNAT: Static IP
The firewall maps the Destination IP Address condition one to one onto the NAT Pool. Select exactly one NAT Pool. See Static IP pairing.
Translated port
With Dynamic IP or Static IP, you can also set a Translated Port to replace the original destination port of TCP and UDP sessions. For example, a request to public port 8080 can be translated to port 80 on the internal server. Without a Translated Port, the destination port is unchanged. ICMP is not affected.
Static IP pairing
With Static IP SNAT or DNAT, the addresses in the condition map one to one onto the addresses in the NAT Pool. For SNAT the condition is Source IP Address; for DNAT it is Destination IP Address.
- The condition must reference exactly one IP Address object, not an IP Address Group. The object can contain several items.
- The items of the object pair with the items of the NAT Pool in configured order.
- Both items of a pair must have the same form (single IP address, CIDR, or range) and cover the same number of addresses. A single IP address and a /32 are different forms.
- Within a pair, addresses map by position.
| Pair | Condition | NAT Pool | Why the pair is valid |
|---|---|---|---|
| 1 | 10.0.0.0/24 | 198.51.100.0/24 | Both are CIDRs covering the same number of addresses. |
| 2 | 192.168.0.1-192.168.0.10 | 203.0.113.101-203.0.113.110 | Both are ranges covering the same number of addresses. |
| 3 | 192.168.100.1 | 192.0.2.1 | Both are single IP addresses. |
With this configuration, 10.0.0.5 maps to 198.51.100.5 and 192.168.0.10 maps to 203.0.113.110.
Address reachability
The upstream network must deliver packets for every translated or published address to the firewall. An interface address is already reachable. For any other NAT Pool address, make sure the upstream router has a route for it that points to the firewall.
If return packets never reach the firewall, check upstream routing and neighbor resolution as well as the NAT rule's hit count.
Examples
In these examples the firewall's internal interface is lan, in the trust zone, and its external interface is wan, in the untrust zone. The wan interface already has an IP address, and the default route points at it.
Share the interface address
The most common SNAT scenario: internal hosts use the public IP address of the wan interface to access external networks.
trust (LAN) untrust (WAN)
10.0.0.2 ------[ lan | firewall | wan ]------ Internet
10.0.0.1 203.0.113.10- Conditions
- Destination Zone: untrust
- NAT Configuration
- Source NAT (SNAT): Dynamic IP and Port
- Interface: wan
- Destination NAT (DNAT): None
- Source NAT (SNAT): Dynamic IP and Port
Give each host a fixed public address
Static IP SNAT maps each internal host to its own public IP address.
trust (LAN) untrust (WAN)
10.0.0.2 ----|
10.0.0.3 ----|
10.0.0.4 ----|----[ lan | firewall | wan ]------ Internet
10.0.0.10 ---| 10.0.0.1 203.0.113.10
Static mapping: 203.0.113.2-203.0.113.4, 203.0.113.5- Conditions
- Source IP Address: 10.0.0.2-10.0.0.4, 10.0.0.10
- Destination Zone: untrust
- NAT Configuration
- Source NAT (SNAT): Static IP
- NAT Pool: 203.0.113.2-203.0.113.4, 203.0.113.5
- Destination NAT (DNAT): None
- Source NAT (SNAT): Static IP
The items pair in order, giving this mapping:
- 10.0.0.2 → 203.0.113.2
- 10.0.0.3 → 203.0.113.3
- 10.0.0.4 → 203.0.113.4
- 10.0.0.10 → 203.0.113.5
Before testing, make the translated addresses reachable from upstream. See Address reachability.
Publish a service on several servers
Dynamic IP DNAT maps port 8080 on the public IP address to port 80 on three internal servers and distributes sessions across them.
trust (LAN) untrust (WAN)
10.0.0.2:80 ----|
10.0.0.3:80 ----|----[ lan | firewall | wan ]------ Internet
10.0.0.4:80 ----| 10.0.0.1 203.0.113.10:8080- Conditions
- Source Zone: untrust
- Destination Zone: untrust
- Destination IP Address: 203.0.113.10
- Destination Port: 8080
- NAT Configuration
- Source NAT (SNAT): None
- Destination NAT (DNAT): Dynamic IP
- Session Distribution Method: Source IP Hash
- NAT Pool: 10.0.0.2-10.0.0.4
- Translated Port: 80
Destination Zone is untrust because the original destination, 203.0.113.10, is the wan interface's address. See Destination zone and destination interface.
Publish servers one to one
Static IP DNAT maps a block of public IP addresses one to one onto internal servers and translates port 8080 to port 80.
trust (LAN) untrust (WAN)
10.0.0.2:80 ----|
10.0.0.3:80 ----|----[ lan | firewall | wan ]------ Internet
10.0.0.4:80 ----| 10.0.0.1 203.0.113.10
Static mapping: 203.0.113.2-203.0.113.4- Conditions
- Source Zone: untrust
- Destination Zone: untrust
- Destination IP Address: 203.0.113.2-203.0.113.4
- Destination Port: 8080
- NAT Configuration
- Source NAT (SNAT): None
- Destination NAT (DNAT): Static IP
- NAT Pool: 10.0.0.2-10.0.0.4
- Translated Port: 80
The items pair in order, giving this mapping:
- 203.0.113.2 → 10.0.0.2
- 203.0.113.3 → 10.0.0.3
- 203.0.113.4 → 10.0.0.4
Before testing, make the published addresses reachable from upstream. See Address reachability.
Hairpin NAT
Hairpin NAT lets an internal host reach an internal server through its public IP address. This is usually needed when the same domain name is used inside and outside: an internal client resolves the name to the public IP address, so its traffic goes to the firewall, which translates it and sends it back to the internal server.
Here the internal host 10.0.0.2 reaches the internal server 10.0.0.4:80 through the public address 203.0.113.10:8080.
trust (LAN) untrust (WAN)
Client 10.0.0.2 ------|
|----[ lan | firewall | wan ]------ Internet
Server 10.0.0.4:80 ---| 10.0.0.1 203.0.113.10:8080- Conditions
- Source Zone: trust
- Destination Zone: untrust
- Destination IP Address: 203.0.113.10
- Destination Port: 8080
- NAT Configuration
- Source NAT (SNAT): Dynamic IP and Port
- Interface: wan
- Destination NAT (DNAT): Static IP
- NAT Pool: 10.0.0.4
- Translated Port: 80
- Source NAT (SNAT): Dynamic IP and Port
Three details matter in this rule:
- Destination Zone is untrust. The packet enters through
lan, but its original destination, 203.0.113.10, belongs to thewaninterface, so the first route lookup returnswanand itsuntrustzone. - Source Zone must be trust. Hairpin traffic and traffic from the external network can have the same destination IP address. Without Source Zone, this rule would also match external traffic and apply SNAT to it.
- SNAT cannot be omitted. With DNAT alone, the server sees the client's internal IP address as the source and replies to it directly over the internal network, bypassing the firewall. The client expects a reply from the public IP address, receives one from the server's internal address instead, and the connection fails.
Apply and verify
Save and publish the rule and the objects it references. Check its position among the other NAT rules, and make sure a security rule allows the traffic.
Use Policy Verification to check the conditions. Then open a new session to check routing and translation, because existing sessions keep the translation they already have.