8 #include <gpxe/socket.h>
9 #include <gpxe/tcpip.h>
11 #include <gpxe/xfer.h>
12 #include <gpxe/open.h>
14 #include <gpxe/features.h>
19 * File transfer protocol
23 FEATURE ( FEATURE_PROTOCOL
, "FTP", DHCP_EB_FEATURE_FTP
, 1 );
28 * These @b must be sequential, i.e. a successful FTP session must
29 * pass through each of these states in order.
47 /** Reference counter */
49 /** Data transfer interface */
50 struct xfer_interface xfer
;
52 /** URI being fetched */
54 /** FTP control channel interface */
55 struct xfer_interface control
;
56 /** FTP data channel interface */
57 struct xfer_interface data
;
61 /** Buffer to be filled with data received via the control channel */
63 /** Remaining size of recvbuf */
65 /** FTP status code, as text */
67 /** Passive-mode parameters, as text */
68 char passive_text
[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
74 * @v refcnt Reference counter
76 static void ftp_free ( struct refcnt
*refcnt
) {
77 struct ftp_request
*ftp
=
78 container_of ( refcnt
, struct ftp_request
, refcnt
);
80 DBGC ( ftp
, "FTP %p freed\n", ftp
);
87 * Mark FTP operation as complete
90 * @v rc Return status code
92 static void ftp_done ( struct ftp_request
*ftp
, int rc
) {
94 DBGC ( ftp
, "FTP %p completed (%s)\n", ftp
, strerror ( rc
) );
96 /* Close all data transfer interfaces */
97 xfer_nullify ( &ftp
->xfer
);
98 xfer_close ( &ftp
->xfer
, rc
);
99 xfer_nullify ( &ftp
->control
);
100 xfer_close ( &ftp
->control
, rc
);
101 xfer_nullify ( &ftp
->data
);
102 xfer_close ( &ftp
->data
, rc
);
105 /*****************************************************************************
107 * FTP control channel
112 * FTP control channel strings
114 * These are used as printf() format strings. Since only one of them
115 * (RETR) takes an argument, we always supply that argument to the
118 static const char * ftp_strings
[] = {
120 [FTP_USER
] = "USER anonymous\r\n",
121 [FTP_PASS
] = "PASS etherboot@etherboot.org\r\n",
122 [FTP_TYPE
] = "TYPE I\r\n",
123 [FTP_PASV
] = "PASV\r\n",
124 [FTP_RETR
] = "RETR %s\r\n",
125 [FTP_QUIT
] = "QUIT\r\n",
130 * Handle control channel being closed
132 * @v control FTP control channel interface
133 * @v rc Reason for close
135 * When the control channel is closed, the data channel must also be
136 * closed, if it is currently open.
138 static void ftp_control_close ( struct xfer_interface
*control
, int rc
) {
139 struct ftp_request
*ftp
=
140 container_of ( control
, struct ftp_request
, control
);
142 DBGC ( ftp
, "FTP %p control connection closed: %s\n",
143 ftp
, strerror ( rc
) );
145 /* Complete FTP operation */
146 ftp_done ( ftp
, rc
);
150 * Parse FTP byte sequence value
152 * @v text Text string
153 * @v value Value buffer
154 * @v len Length of value buffer
156 * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
157 * form for IP addresses in PORT commands) into a byte sequence. @c
158 * *text will be updated to point beyond the end of the parsed byte
161 * This function is safe in the presence of malformed data, though the
162 * output is undefined.
164 static void ftp_parse_value ( char **text
, uint8_t *value
, size_t len
) {
166 *(value
++) = strtoul ( *text
, text
, 10 );
173 * Handle an FTP control channel response
177 * This is called once we have received a complete response line.
179 static void ftp_reply ( struct ftp_request
*ftp
) {
180 char status_major
= ftp
->status_text
[0];
181 char separator
= ftp
->status_text
[3];
183 DBGC ( ftp
, "FTP %p received status %s\n", ftp
, ftp
->status_text
);
185 /* Ignore malformed lines */
186 if ( separator
!= ' ' )
189 /* Ignore "intermediate" responses (1xx codes) */
190 if ( status_major
== '1' )
193 /* Anything other than success (2xx) or, in the case of a
194 * repsonse to a "USER" command, a password prompt (3xx), is a
197 if ( ! ( ( status_major
== '2' ) ||
198 ( ( status_major
== '3' ) && ( ftp
->state
== FTP_USER
) ) ) ){
199 /* Flag protocol error and close connections */
200 ftp_done ( ftp
, -EPROTO
);
203 /* Open passive connection when we get "PASV" response */
204 if ( ftp
->state
== FTP_PASV
) {
205 char *ptr
= ftp
->passive_text
;
207 struct sockaddr_in sin
;
212 sa
.sin
.sin_family
= AF_INET
;
213 ftp_parse_value ( &ptr
, ( uint8_t * ) &sa
.sin
.sin_addr
,
214 sizeof ( sa
.sin
.sin_addr
) );
215 ftp_parse_value ( &ptr
, ( uint8_t * ) &sa
.sin
.sin_port
,
216 sizeof ( sa
.sin
.sin_port
) );
217 if ( ( rc
= xfer_open_socket ( &ftp
->data
, SOCK_STREAM
,
218 &sa
.sa
, NULL
) ) != 0 ) {
219 DBGC ( ftp
, "FTP %p could not open data connection\n",
221 ftp_done ( ftp
, rc
);
226 /* Move to next state */
227 if ( ftp
->state
< FTP_DONE
)
230 /* Send control string */
231 if ( ftp
->state
< FTP_DONE
) {
232 DBGC ( ftp
, "FTP %p sending ", ftp
);
233 DBGC ( ftp
, ftp_strings
[ftp
->state
], ftp
->uri
->path
);
234 xfer_printf ( &ftp
->control
, ftp_strings
[ftp
->state
],
240 * Handle new data arriving on FTP control channel
242 * @v control FTP control channel interface
244 * @v len Length of new data
246 * Data is collected until a complete line is received, at which point
247 * its information is passed to ftp_reply().
249 static int ftp_control_deliver_raw ( struct xfer_interface
*control
,
250 const void *data
, size_t len
) {
251 struct ftp_request
*ftp
=
252 container_of ( control
, struct ftp_request
, control
);
253 char *recvbuf
= ftp
->recvbuf
;
254 size_t recvsize
= ftp
->recvsize
;
258 c
= * ( ( char * ) data
++ );
262 /* End of line: call ftp_reply() to handle
263 * completed reply. Avoid calling ftp_reply()
264 * twice if we receive both \r and \n.
268 /* Start filling up the status code buffer */
269 recvbuf
= ftp
->status_text
;
270 recvsize
= sizeof ( ftp
->status_text
) - 1;
273 /* Start filling up the passive parameter buffer */
274 recvbuf
= ftp
->passive_text
;
275 recvsize
= sizeof ( ftp
->passive_text
) - 1;
278 /* Stop filling the passive parameter buffer */
282 /* Fill up buffer if applicable */
283 if ( recvsize
> 0 ) {
291 /* Store for next invocation */
292 ftp
->recvbuf
= recvbuf
;
293 ftp
->recvsize
= recvsize
;
298 /** FTP control channel operations */
299 static struct xfer_interface_operations ftp_control_operations
= {
300 .close
= ftp_control_close
,
301 .vredirect
= xfer_vopen
,
302 .window
= unlimited_xfer_window
,
303 .alloc_iob
= default_xfer_alloc_iob
,
304 .deliver_iob
= xfer_deliver_as_raw
,
305 .deliver_raw
= ftp_control_deliver_raw
,
308 /*****************************************************************************
315 * Handle FTP data channel being closed
317 * @v data FTP data channel interface
318 * @v rc Reason for closure
320 * When the data channel is closed, the control channel should be left
321 * alone; the server will send a completion message via the control
322 * channel which we'll pick up.
324 * If the data channel is closed due to an error, we abort the request.
326 static void ftp_data_closed ( struct xfer_interface
*data
, int rc
) {
327 struct ftp_request
*ftp
=
328 container_of ( data
, struct ftp_request
, data
);
330 DBGC ( ftp
, "FTP %p data connection closed: %s\n",
331 ftp
, strerror ( rc
) );
333 /* If there was an error, close control channel and record status */
335 ftp_done ( ftp
, rc
);
339 * Handle data delivery via FTP data channel
341 * @v xfer FTP data channel interface
342 * @v iobuf I/O buffer
343 * @v meta Data transfer metadata, or NULL
344 * @ret rc Return status code
346 static int ftp_data_deliver_iob ( struct xfer_interface
*data
,
347 struct io_buffer
*iobuf
,
348 struct xfer_metadata
*meta __unused
) {
349 struct ftp_request
*ftp
=
350 container_of ( data
, struct ftp_request
, data
);
353 if ( ( rc
= xfer_deliver_iob ( &ftp
->xfer
, iobuf
) ) != 0 ) {
354 DBGC ( ftp
, "FTP %p failed to deliver data: %s\n",
355 ftp
, strerror ( rc
) );
362 /** FTP data channel operations */
363 static struct xfer_interface_operations ftp_data_operations
= {
364 .close
= ftp_data_closed
,
365 .vredirect
= xfer_vopen
,
366 .window
= unlimited_xfer_window
,
367 .alloc_iob
= default_xfer_alloc_iob
,
368 .deliver_iob
= ftp_data_deliver_iob
,
369 .deliver_raw
= xfer_deliver_as_iob
,
372 /*****************************************************************************
374 * Data transfer interface
379 * Close FTP data transfer interface
381 * @v xfer FTP data transfer interface
382 * @v rc Reason for close
384 static void ftp_xfer_closed ( struct xfer_interface
*xfer
, int rc
) {
385 struct ftp_request
*ftp
=
386 container_of ( xfer
, struct ftp_request
, xfer
);
388 DBGC ( ftp
, "FTP %p data transfer interface closed: %s\n",
389 ftp
, strerror ( rc
) );
391 ftp_done ( ftp
, rc
);
394 /** FTP data transfer interface operations */
395 static struct xfer_interface_operations ftp_xfer_operations
= {
396 .close
= ftp_xfer_closed
,
397 .vredirect
= ignore_xfer_vredirect
,
398 .window
= unlimited_xfer_window
,
399 .alloc_iob
= default_xfer_alloc_iob
,
400 .deliver_iob
= xfer_deliver_as_raw
,
401 .deliver_raw
= ignore_xfer_deliver_raw
,
404 /*****************************************************************************
411 * Initiate an FTP connection
413 * @v xfer Data transfer interface
414 * @v uri Uniform Resource Identifier
415 * @ret rc Return status code
417 static int ftp_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
418 struct ftp_request
*ftp
;
419 struct sockaddr_tcpip server
;
428 /* Allocate and populate structure */
429 ftp
= zalloc ( sizeof ( *ftp
) );
432 ftp
->refcnt
.free
= ftp_free
;
433 xfer_init ( &ftp
->xfer
, &ftp_xfer_operations
, &ftp
->refcnt
);
434 ftp
->uri
= uri_get ( uri
);
435 xfer_init ( &ftp
->control
, &ftp_control_operations
, &ftp
->refcnt
);
436 xfer_init ( &ftp
->data
, &ftp_data_operations
, &ftp
->refcnt
);
437 ftp
->recvbuf
= ftp
->status_text
;
438 ftp
->recvsize
= sizeof ( ftp
->status_text
) - 1;
440 DBGC ( ftp
, "FTP %p fetching %s\n", ftp
, ftp
->uri
->path
);
442 /* Open control connection */
443 memset ( &server
, 0, sizeof ( server
) );
444 server
.st_port
= htons ( uri_port ( uri
, FTP_PORT
) );
445 if ( ( rc
= xfer_open_named_socket ( &ftp
->control
, SOCK_STREAM
,
446 ( struct sockaddr
* ) &server
,
447 uri
->host
, NULL
) ) != 0 )
450 /* Attach to parent interface, mortalise self, and return */
451 xfer_plug_plug ( &ftp
->xfer
, xfer
);
452 ref_put ( &ftp
->refcnt
);
456 DBGC ( ftp
, "FTP %p could not create request: %s\n",
457 ftp
, strerror ( rc
) );
458 ftp_done ( ftp
, rc
);
459 ref_put ( &ftp
->refcnt
);
463 /** FTP URI opener */
464 struct uri_opener ftp_uri_opener __uri_opener
= {