Sub topic 11
Why some protocols break behind NAT
This chapter assumes you know how NAT rewrites addresses and ports, how the translation table (connection tracking) works, and the basics of TCP, UDP, and IP headers. It explains why a few protocols fail through NAT, and how NAT helpers and protocol design work around that.
The core problem: addresses inside the payload
A NAT device rewrites the IP header and the TCP or UDP port fields. It does not normally touch the application payload. That works for protocols that carry no addresses in their data, such as HTTP or DNS over a single connection.
Some protocols copy an IP address or port into the payload. The peer then reads a private address like 192.168.1.10 that is meaningless outside the LAN. The headers are translated correctly, but the data inside is stale.
Protocols commonly affected:
- Active-mode FTP: the client sends a
PORTcommand containing its own IP and a port. The server then opens a new connection back to that address. - SIP and RTP: SIP messages carry addresses in the SDP body that tell the other side where to send audio. If those are private addresses, the media never arrives.
- IPsec ESP, GRE, and PPTP: these have no TCP or UDP ports, so PAT has nothing to use to tell flows apart.
Two failure modes
- Embedded address is private. The remote side tries to reach an address it cannot route to.
- Inbound connection with no mapping. Even with the correct public address, the server's new connection to the NAT device matches no entry in the translation table, so it is dropped.
Active FTP hits both. Passive FTP avoids them, because the client opens both connections outbound.
NAT helpers (ALGs)
An application-layer gateway (ALG), called a helper in Linux, inspects specific protocols. It rewrites the embedded addresses in the payload and creates an expected-connection entry so the inbound connection is allowed.
Linux ships helper modules such as nf_conntrack_ftp and nf_nat_ftp. Since kernel 4.x, automatic helper assignment is off by default for security reasons. You assign helpers explicitly with a rule in the raw table:
# Load the FTP helper modules
sudo modprobe nf_conntrack_ftp nf_nat_ftp
# Attach the FTP helper to control connections on port 21
sudo iptables -t raw -A PREROUTING -p tcp --dport 21 -j CT --helper ftpBetter fixes in the protocol
Modern designs avoid the problem instead of patching it:
| Approach | How it helps | Example |
|---|---|---|
| Client-initiated connections only | No inbound connection is needed | Passive FTP, HTTPS |
| Multiplexing on one port | Everything goes through one outbound flow | HTTP/2, QUIC |
| Discovering the public address | The client learns its mapped address and advertises that | STUN with ICE |
| Relaying | A public server forwards traffic | TURN, SIP media relays |
The last two rows lead into NAT traversal, which a later chapter covers.
Spotting the symptom
A protocol broken by NAT typically shows a control connection that works while the data or media channel fails. A packet capture on the server will show private addresses in the payload. You can check the translation table for tracked entries:
sudo conntrack -L -p tcp --dport 21Exercise
A client behind PAT uses active-mode FTP. Login succeeds, but listing a directory hangs. Why does this happen, and what are two ways to fix it?
Show answer
The PORT command carries the client's private address, so the server tries to connect back to an unroutable address. Even with the right address, the inbound connection has no matching translation entry. Fixes: switch the client to passive mode so it opens both connections outbound, or enable and assign an FTP conntrack/NAT helper on the gateway (this works only for unencrypted FTP).
Exercise
Why can PAT not handle ESP (IPsec) traffic from several hosts the way it handles TCP?
Show answer
ESP has no port numbers. PAT distinguishes flows by rewriting ports, so with no port field it cannot multiplex many hosts onto one public address. Solutions include NAT-T, which wraps ESP in UDP port 4500.