skip to content

When an FTP client behind a NAT sends PORT with its private address, what must the NAT's FTP helper change, and why does the edit reach beyond the PORT line?

level: seniorimportance: nice to knowfreq 9%

answer

  1. ASCII address, variable length
  2. a mapping for the inbound dial
  3. bytes added shift the stream
  4. sequence numbers until the end

basics

~20 s

A NAT's FTP helper rewrites PORT's private address and port to a public mapping and expects the server's inbound connection. Because the ASCII text may change length, it must also shift TCP sequence and acknowledgement numbers for the rest of the connection.

solid answer

~50 s

In active mode the client announces where the server should dial, for example `PORT 10,0,0,5,195,80`. Behind a NAT that private address is useless to the server, and some servers refuse a `PORT` naming any address other than the client's own. An FTP helper on the NAT therefore rewrites the line to its public address and a port it maps back to the client, and creates an expected entry so the server's connection, arriving from its port 20, is translated in. Because the address is decimal text, the new line can be longer or shorter: `203,0,113,45,195,80` is four characters longer than `10,0,0,5,195,80`. RFC 2663 says that once the length changes, the helper must adjust TCP sequence and acknowledgement numbers on every later packet of that connection, besides checksums and lengths. A `PORT` line split across segments or encrypted by TLS defeats the rewrite, which is why clients behind NAT should use passive mode.

go deeper

for a junior

Recall that PORT carries the client's own address as text, and that behind a NAT this is a private address the server cannot reach.

for a middle

Explain the helper's steps: rewrite PORT, map a public port, expect the server's inbound connection, and why servers may reject a mismatched address.

for a senior

Explain why a length change forces sequence and acknowledgement fix-ups for the rest of the connection, and the cases, split segments and TLS, where the helper fails.

for a principal

Weigh keeping active-mode support through NAT helpers against mandating passive mode, considering helper state on failover and the inspection that TLS removes.

## The problem the helper solves In **active mode** an FTP client tells the server where to open the data connection by sending `PORT h1,h2,h3,h4,p1,p2`: its own IPv4 address and a listening port, written as decimal text. The server then connects from its port 20 to that address. When the client sits behind a **NAT** on a private network, two things go wrong at once: - the address in `PORT` is a private one, for example `10.0.0.5`, which the server cannot route to; - even with a reachable address, the NAT has no mapping for an unsolicited inbound connection, so it would drop the server's attempt. The client cannot fix this on its own: it knows only its private address, and nothing in the FTP exchange tells it which public address and port the NAT would present. Servers add a third obstacle. Many refuse a `PORT` whose address differs from the address the control connection came from, as a defence against the bounce attack. ProFTPD logs this as `Refused PORT 192,168,1,2,193,116 (address mismatch)` unless `AllowForeignAddress` permits it; vsftpd applies the same check unless `port_promiscuous` is enabled. Seeing the client's control connection arrive from the NAT's public address, such a server rejects a `PORT` naming the private one. ## What the helper changes An FTP **application-level gateway** (ALG) on the NAT watches the client's control connection to port 21. When it sees `PORT`, it: 1. allocates a public port on the NAT that maps back to the client's private address and listening port; 2. rewrites the text, so `PORT 10,0,0,5,195,80` becomes, say, `PORT 203,0,113,45,195,80`; 3. records an expected inbound connection from the server to that public port, so the server's dial from port 20 is translated to the client instead of dropped; 4. fixes the packet: TCP checksum, IP total length and IP header checksum. The same helper handles `EPRT`, which also carries the client's address, and, on the server side of a NAT, `227` replies. ## Why the edit reaches beyond one line The address and port are **ASCII**, so their length depends on the digits: | Text | Characters | |---|---| | `10,0,0,5,195,80` | 15 | | `203,0,113,45,195,80` | 19 | TCP numbers every byte of the stream. If the helper inserts four bytes, every byte the client sends afterwards sits four positions later than the client believes, and the server's acknowledgements refer to positions the client never sent. RFC 2663 and RFC 3027 spell out the consequence: - the helper must add the difference to the **sequence numbers** of every later client-to-server packet on that connection; - it must subtract it from the **acknowledgement numbers** of every server-to-client packet; - it keeps this delta for the life of the connection, and every further `PORT` that changes length adds to it; - if the rewritten length is identical, only the checksum needs fixing. A longer packet can also exceed the path MTU, forcing fragmentation or a Path MTU Discovery round. ## Where the rewrite fails The helper can only rewrite what it can see whole and in clear text: - **Split commands.** RFC 2663 calls fragmented `PORT` packets a pathological case the NAT must reassemble first; a `PORT` sent one character per segment simply cannot be translated. - **Encryption.** After `AUTH TLS` the `PORT` line is inside TLS, so the helper neither rewrites the address nor creates the mapping, and active mode fails outright. - **No helper at all.** Many NAT paths have none, or have it disabled. - **Lost state.** The running offset lives in the NAT's connection state. If that state disappears, through a reboot or a failover to a peer that does not share it, the byte numbering on the control connection no longer lines up and the session stalls even though nothing in FTP changed. ## The practical conclusion Every failure above disappears in passive mode, where the client opens the data connection outbound and its NAT treats it as one more ordinary outgoing connection. RFC 2663 makes the same observation: an FTP session from a private host keeps working through a traditional NAT if it uses `PASV`, even when the helper cannot translate anything. The helper's rewrite is a compatibility measure for active mode; knowing its sequence-number bookkeeping explains why it is fragile, and why network engineers treat "use passive" as the default answer for clients behind NAT.

  • Why would a server refuse the PORT command even if the client's NAT could forward the connection?
    Many servers refuse a `PORT` naming an address other than the one the control connection came from, to block bounce-style misuse. Behind a NAT the control connection arrives from the public address while `PORT` names the private one, so the server reports an address mismatch. ProFTPD's `AllowForeignAddress` and vsftpd's `port_promiscuous` relax the check, at a security cost.
  • What happens to the helper's sequence-number bookkeeping if a second PORT in the same session changes length again?
    The deltas accumulate. The helper keeps a running offset for the connection and applies the total to every later sequence number in one direction and acknowledgement number in the other. Losing that state, for example on a NAT failover without state synchronisation, desynchronises the control connection.

  • The NAT only needs to rewrite the IP header, as for any other connection
  • Changing digits in PORT never changes the packet's length
  • The server's 227 reply must be rewritten to fix an active-mode client
  • An FTP helper can rewrite PORT inside an FTPS control connection
  • Servers always accept a PORT that names a different address from the control connection