FTP は、制御用の control connection, Data 転送用の data connection という 2 つの connection を張ります。 さらに data connection には active mode, passive mode という 2 つの mode があります。
# active mode
port command を使用。
client が待ち受け port を server に送信。
server から connection を張る。
# passive mode
pasv command を使用。
server が待ち受け port を client に返信。
client から connection を張る。
active/passive mode の大きな違いは上記になります。
実際に file を download した場合の capture を用い、動作の違いについて確認してみます。
# 構成

# client log
client:/# ftp 192.168.71.254 Connected to 192.168.71.254. 220 (vsFTPd 2.0.7) Name (192.168.71.254:root): anonymous 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> ftp> get welcome.msg !___ active mode で file を download local: welcome.msg remote: welcome.msg 200 PORT command successful. Consider using PASV. 150 Opening BINARY mode data connection for welcome.msg (166 bytes). 226 File send OK. 166 bytes received in 0.00 secs (267.5 kB/s) ftp> passive Passive mode on. ftp> ftp> get welcome.msg !___ passive mode で file を download local: welcome.msg remote: welcome.msg 227 Entering Passive Mode (192,168,71,254,190,46) 150 Opening BINARY mode data connection for welcome.msg (166 bytes). 226 File send OK. 166 bytes received in 0.00 secs (343.5 kB/s) ftp> quit 221 Goodbye. |
#capture (active mode)
active mode の場合、上記 capture のように、PORT command 中に client が待ち受ける ip address, port# の情報が入っています。 (ip address:192.168.71.11, port#:130x256+39=33319)
その情報を用い、22 行目のように、server(192.168.71.254:20) -> client(192.168.71.11:33319) に対して connection を張ります。
#capture (passive mode)

passive mode の場合、上記 capture 33 行目のように、PASV command を送信します。 active mode と異なり、この packet 中に ip address や port# の情報は含まれず、その次 (34行目) の、server からの 227 response 中に、server が待ち受ける ip address, port# の情報が入っています。 (ip address:192.168.71.254, port#:21x256+109=5485)
その情報を用い、35 行目のように、client(192.168.71.11:48365) -> server(192.168.71.254:5485) に対して connection を張ります。 client port#48365 は任意の番号です。
FTP に関する Troubleshooting を行う場合、
- Control Connection 自体が張れないのか、Data Connection 側の問題なのか
- Data Connection の問題の場合、active/passive どちらかのみで発生するのか、active/passive 両方で発生するのか
といった切り分けを行うことで、原因が特定しやすくなり、問題解決までの時間が短くなります。