Connecting to FTP server behind NAT router with private IPv4 address using FileZilla

Original post to the FileZilla bug tracker: https://trac.filezilla-project.org/ticket/12723

The client is connecting to a server behind an NAT router, whose IP address facing us is also a private address, as illustrated below.

+-----------+   +-------------|------+   +--------------+
| my client |   |     NAT router     |   | their server |
| 172.x.x.x +---+ 192.168.x.x | 10.x +---+   10.x.x.x   |
+-----------+   +-------------|------+   +--------------+

I left the setting for “Connection / FTP / Passive mode” by default, to “Use the server’s external IP address instead”. However, it did not work for me.

The message log indicates that the server replied with its private address, and a netstat command on the client at the moment showed it was actually trying to connect to exactly the server’s private address.

By turning Debug level to 3 (Verbose), I noticed a trace message:

Destination IP of data connection does not match peer IP of control connection. Not binding source address of data connection.

Having searched this message in the source code, I arrived at function CTransferSocket :: SetupPassiveTransfer, and found that its host argument seemed to be assigned in the function CFtpRawTransferOpData :: ParsePasvResponse.

Inside ParsePasvResponse, the condition for assigning the peerIP from the control socket to the host_ of the passive data connection is an un-routable data peer address as well as a routable control peer address. Unfortunately, with all un-routable, internal addresses for both data and control, my scenario effectively bypasses this mechanism.

At the end of the ParsePasvResponse function, I noticed that I can instruct the client to always use server address by setting the option OPTION_PASVREPLYFALLBACKMODE to 2, which is set by an option named Pasv reply fallback mode, stored in %appdata%\FileZilla\filezilla.xml. I wonder the author’s intention on omitting this option in the settings UI, although it indeed provides a workaround for my situation.

Another workaround is to connect through a proxy, for instance, by setting a SOCKS4 proxy in “Connection / Generic proxy”. This will force the client to prefer the EPSV command for passive mode, which gets a response from the server providing only the port, circumventing the IPv4 NAT issue.