Sub topic 3
What NAT changes in a packet
This chapter assumes you know IPv4 addresses and subnet masks, what a default gateway does, and how TCP and UDP use port numbers. It builds on the idea that private addresses cannot be routed on the internet.
The fields NAT rewrites
A NAT device is a router that edits packet headers as it forwards them. It changes only a few fields:
- IP addresses. Source NAT (SNAT) rewrites the source address. Destination NAT (DNAT) rewrites the destination address.
- Transport ports. The TCP or UDP source port (SNAT) or destination port (DNAT) may be rewritten. Port address translation (PAT) rewrites the source port whenever two private hosts would otherwise collide on the same public address and port.
- Checksums. The IP header checksum and the TCP or UDP checksum are recalculated. The TCP and UDP checksums cover a pseudo-header that includes both IP addresses, so changing an address invalidates them.
NAT does not change the payload, the sequence numbers, or the protocol. The TTL drops by one, but that happens because the device is routing, not because it is translating.
Tracing one connection
A client at 192.168.1.20 opens a connection to a web server at 198.51.100.7. The NAT device's public address is 203.0.113.5.
The outbound packet has its source rewritten. The reply arrives addressed to the public side, and the device rewrites its destination back to the private host. The server never sees 192.168.1.20.
The same logic runs in reverse for DNAT. An incoming packet to 203.0.113.5:8080 has its destination rewritten to something like 192.168.1.10:80, and the reply has its source rewritten back to 203.0.113.5:8080. That is how port forwarding looks from the packet's point of view.
Seeing the rewrite
Capture on both interfaces of the NAT device and compare. The interface names here are examples. eth1 faces the LAN and eth0 faces the internet.
# LAN side: original private source address
sudo tcpdump -ni eth1 tcp port 80
# WAN side: translated source address and port
sudo tcpdump -ni eth0 tcp port 80The same segment appears on both interfaces with a different source address, and possibly a different source port.
Common errors
- Addresses inside the payload are not rewritten. Basic NAT edits headers only. A protocol that carries an IP address in its data, such as FTP active mode or SIP, will advertise the private address unless a helper rewrites it. A later chapter covers these protocols.
- Replies that skip the NAT device. If the reply does not pass back through the device, the reverse rewrite never happens. The client then sees a packet from an address it never contacted and discards it.
- Translating only one direction. NAT works because the device remembers the mapping it created. A packet with no matching mapping and no DNAT rule is not translated, and it is usually dropped. The next chapter covers that memory.
Exercise
A client at 10.0.0.5 port 50000 connects to 198.51.100.7 port 443 through a NAT device with public address 203.0.113.5, which assigns public port 40002. Which source and destination appear on the reply as it arrives at the NAT device, and which after the device forwards it to the client?
Show answer
Arriving at the NAT device, the reply has source 198.51.100.7:443 and destination 203.0.113.5:40002. After translation, the source is still 198.51.100.7:443 and the destination is 10.0.0.5:50000. Only the destination is rewritten, and the IP and TCP checksums are recalculated.