Sub topic 3
What NAT changes in a packet
This chapter assumes you know IPv4 addressing and subnet masks, what a router and default gateway do, and how TCP and UDP use port numbers. You should also be comfortable running commands in a Linux shell. The chapter covers which fields a NAT device rewrites, in which direction, and what it has to fix up afterwards so the packet stays valid.
What NAT rewrites
Network Address Translation (NAT) is a function on a router or firewall. It rewrites address fields in packets that pass through it. A packet carries two relevant pairs of values:
- The IPv4 header holds a source address and a destination address.
- The TCP or UDP header holds a source port and a destination port.
Together with the protocol number, these five values form the 5-tuple that identifies a flow. NAT changes one or more of these values on the way through and changes them back on the reply.
NAT does not change the payload, the TTL logic, or the routing decision itself. The device still routes the packet normally. It just edits some header fields before or after that decision.
There are two basic kinds of rewrite:
| Kind | Field rewritten | Typical use | Hook point on Linux |
|---|---|---|---|
| Source NAT (SNAT) | Source address, often the source port too | Private hosts reaching the Internet | POSTROUTING, after the routing decision |
| Destination NAT (DNAT) | Destination address, often the destination port too | Port forwarding to a private server | PREROUTING, before the routing decision |
The ordering matters. DNAT happens before routing, because the new destination decides which interface the packet leaves through. SNAT happens after routing, because the new source address usually depends on the outgoing interface.
A packet going out
Suppose a LAN host 192.168.1.20 opens a web connection to 198.51.100.10. The NAT gateway has the LAN address 192.168.1.1 and the public address 203.0.113.5.
The host builds this packet:
| Field | Value |
|---|---|
| Source address | 192.168.1.20 |
| Source port | 51000 |
| Destination address | 198.51.100.10 |
| Destination port | 443 |
The gateway applies source NAT as the packet leaves the public interface:
| Field | Before | After |
|---|---|---|
| Source address | 192.168.1.20 | 203.0.113.5 |
| Source port | 51000 | 51000 (or another free port) |
| Destination address | 198.51.100.10 | 198.51.100.10 |
| Destination port | 443 | 443 |
The source address changes because 192.168.1.20 is a private address. Internet routers do not route private ranges, so a reply to it would never come back. The source port stays the same when it is free. If another LAN host already uses public port 51000 toward the same destination, the gateway picks a different port. That case is covered in the chapter on source NAT and PAT.
A reply coming back
The server at 198.51.100.10 sees a connection from 203.0.113.5:51000. It answers to that address:
| Field | Value on the Internet |
|---|---|
| Source address | 198.51.100.10 |
| Source port | 443 |
| Destination address | 203.0.113.5 |
| Destination port | 51000 |
When the reply reaches the gateway, the gateway reverses the translation. It rewrites the destination to 192.168.1.20:51000 and forwards the packet into the LAN. The gateway does this without any routing rule or configuration for this specific reply. It looks up the connection in its translation table, which the next chapter covers in detail.
A packet coming in: destination NAT
Destination NAT works the same way in the other direction. An outside client connects to the gateway's public address, and the gateway rewrites the destination to a private server.
An outside client 198.51.100.77 sends a packet to 203.0.113.5:8080. A DNAT rule sends port 8080 to 192.168.1.10:80:
| Field | Before | After |
|---|---|---|
| Source address | 198.51.100.77 | 198.51.100.77 |
| Source port | 40000 | 40000 |
| Destination address | 203.0.113.5 | 192.168.1.10 |
| Destination port | 8080 | 80 |
The server replies from 192.168.1.10:80 to 198.51.100.77:40000. The gateway reverses the translation and rewrites the reply's source to 203.0.113.5:8080. The client then sees the reply coming from the address it contacted. This is required: a TCP client discards a segment whose source does not match any connection it has open.
Checksums must be fixed too
IPv4 and TCP and UDP all carry checksums that cover the fields NAT edits:
- The IPv4 header checksum covers the source and destination addresses.
- The TCP and UDP checksums cover a pseudo-header that includes both addresses, plus the ports and the payload.
After rewriting any of these fields, the NAT device must recompute the checksums, or the receiver drops the packet as corrupt. Implementations do this incrementally. They adjust the old checksum by the difference between the old and new values instead of reading the whole packet again, which is cheap. This is also why NAT cannot work on traffic where it cannot see or edit these headers, such as an IPsec ESP payload that authenticates the addresses. That case is covered in the chapter on protocols that break behind NAT.
Seeing the rewrite
You can observe the translation on a Linux gateway with the connection tracking tool. The first line below checks that forwarding and masquerading are set up, and the second lists tracked connections.
# Allow the Linux host to forward packets between interfaces
sudo sysctl -w net.ipv4.ip_forward=1
# Inspect the kernel's connection tracking table (NAT mappings)
sudo conntrack -L -p tcpA line in the output has two tuples. For the outgoing example above it looks like this:
tcp 6 431999 ESTABLISHED src=192.168.1.20 dst=198.51.100.10 sport=51000 dport=443 src=198.51.100.10 dst=203.0.113.5 sport=443 dport=51000 [ASSURED] mark=0 use=1Read it as two halves:
- The first
src= dst= sport= dport=group is the original direction, exactly as the LAN host sent it. - The second group is the reply direction, as the server will send it. Its destination is
203.0.113.5:51000, the translated source.
The difference between the original destination and the reply source (or the original source and the reply destination) is the translation. If the two halves mirror each other exactly, no NAT applies to that connection.
What NAT does not change
- It does not change the transport protocol, the payload bytes, or the TCP sequence numbers in ordinary cases.
- It does not hide that a connection exists. The public address and port are visible to the remote end.
- It does not act as a security policy. NAT only translates. A gateway drops unsolicited inbound packets because no mapping exists for them. A firewall rule is what states that policy explicitly.
Some protocols put addresses inside the payload, such as FTP in active mode and SIP. A plain NAT device does not touch those, and the connection breaks unless a helper rewrites them. That is a later chapter.
Common mistakes
- Expecting the destination to be rewritten after routing. Linux does DNAT in PREROUTING because the routing decision must see the new destination.
- Forgetting that a reply needs the reverse rewrite. If replies bypass the NAT device, the client sees a source address it never contacted and drops the packet.
- Treating the source port as fixed. A NAT device may change the port as well as the address.
- Reading
conntrackoutput as one tuple. The second tuple is the reply, so the translated address appears there.
Exercise
A LAN host 10.0.0.5 sends a TCP packet from port 40100 to 192.0.2.8 port 80 through a NAT gateway whose public address is 203.0.113.9. Assuming the source port is not changed, give the source address, source port, destination address and destination port of the packet on the Internet side, and of the server's reply when it arrives at the gateway.
Show answer
Outgoing on the Internet side: source 203.0.113.9:40100, destination 192.0.2.8:80. The reply arriving at the gateway has source 192.0.2.8:80 and destination 203.0.113.9:40100. The gateway then rewrites the destination to 10.0.0.5:40100 before delivering it to the LAN.
Exercise
Why does Linux apply destination NAT in PREROUTING and source NAT in POSTROUTING, and not the other way around?
Show answer
DNAT changes the destination, and the routing decision depends on the destination, so the rewrite must happen before routing. SNAT usually chooses the new source from the outgoing interface, which is only known after routing, so it happens in POSTROUTING.
Exercise
A conntrack entry shows original src=192.168.1.20 dst=198.51.100.10 sport=51000 dport=443 and reply src=198.51.100.10 dst=203.0.113.5 sport=443 dport=51000. What translation is in effect, and which address does the server see as the client?
Show answer
Source NAT is in effect. The LAN address 192.168.1.20 is replaced by 203.0.113.5 and the port stays 51000. The server sees the client as 203.0.113.5:51000.