As we mentioned in the 1st part of this series, FTP is a more complex protocol than many, using one control connection and one data connection.
In typical Stream Mode operation, a new data connection is opened and closed for each data transfer, whether thatâs an upload, a download, or a directory listing. To avoid confusion between different data connections, and as a recognition of the fact that networks may have old packets shuttling around for some time, these connections need to be distinguishable from one another.
In the previous article, we noted that two network sockets are distinguished by the five elements of âLocal Addressâ, âLocal Portâ, âProtocolâ, âRemote Addressâ, and âRemote Portâ. For a data connection associated with any particular request, the local and remote addresses are fixed, as the addresses of the client and server. The protocol is TCP, and only the two ports are variable.
For a PASV, or passive data connection, the client-side port is chosen randomly by the client, and the server-side port is similarly chosen randomly by the server. The client connects to the server.
For a PORT, or active data connection, the client-side port is chosen randomly by the client, and the server-side port is set to port 20. The server connects to the client.
All of these work through firewalls and NAT routers, because firewalls and NAT routers contain an Application Layer Gateway (ALG) that watches for PORT and PASV commands, and modifies the control (in the case of a NAT) and/or uses the values provided to open up a firewall hole.
For the default data connection (what happens if no PORT or PASV command is sent before the first data transfer command), the client-side port is predictable (itâs the same as the source port the client used when connecting the control channel), and the server-side port is 20. Again, the server connects to the client.
Because firewalls and NATs open up a âreverseâ hole for TCP sockets, the default data port works with firewalls and NATs that arenât running an ALG, or whose ALG cannot scan for PORT and PASV commands.
There are a couple of reasons â the first is that it doesnât know that the service connected to is running the FTP protocol. This is common if the server is running on a port other than the usual port 21.
The second reason is that the FTP control connection doesnât look like it contains FTP commands â usually because the connection is encrypted. This can happen because youâre tunneling the FTP control connection through an encrypted tunnel such as SSH (donât laugh â it does happen!), or hopefully itâs because youâre running FTP over SSL, so that the control and data connections can be encrypted, and you can authenticate the identity of the FTP server.
In the words of Deep Thought: âHmm⊠trickyâ.
There are a couple of classic solutions:
The astute reader can probably see where Iâm going with this.
The default data port is predictable â if the client connects from port U to port L at the server (L is usually 21), then the default data port will be opened from port L-1 at the server to port U at the client.
The default data port doesnât need the firewall to do anything other than allow reverse connections back along the port that initiated the connection. You donât need to open huge ranges at the serverâs firewall (in fact you should be able to simply open port 21 inbound to your server).
The default data port is required to be supported by FTP servers going back a long way- at least a couple of decades. Yes, really, that long.
Good point, that, and a great sentence to use whenever you wish to halt innovation in its tracks.
Okay, itâs obvious that there are some drawbacks:
Even with those drawbacks, there are still further solutions to apply â the first being to use Block-mode instead of Stream-mode. In Stream-mode, each data transfer requires opening and closing the data connection; in Block-mode, which is a little like HTTPâs chunked mode, blocks of data are sent, and followed by an âEOFâ marker (End of File), so that the data connection doesnât need to be closed. If you can convince your FTP client to request Block-mode with the default data connection, and your FTP server supports it (WFTPD Pro has done so for several years), you can achieve FTP over SSL through NATs and firewalls simply by opening port 21.
For the second problem, itâs worth noting that many FTP client authors implemented default data connections out of a sense of robustness, so default data connections will often work if you can convince the PORT and PASV commands to fail â by, for instance, putting restrictive firewalls or NATs in the way, or perhaps by preventing the FTP server from accepting PORT or PASV commands in some way.
Clearly, since Microsoftâs IIS 7.5 downloadable FTP Server supports FTPS in block mode with the default data port, there has been some consideration given to my whispers to them that this could solve the FTP over SSL through firewall problem.
Other than my own WFTPD Explorer, I am not aware of any particular clients that support the explicit use of FTP over SSL with Block-mode on the default data connection â Iâd love to hear of your experiments with this mode of operation, to see if it works as well for you as it does for me.
One Response to How FTP Data Connections Work Part 2 (OR: Fun With Port 20)