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.
48 /** Reference counter */
50 /** Data transfer interface */
51 struct xfer_interface xfer;
53 /** URI being fetched */
55 /** FTP control channel interface */
56 struct xfer_interface control;
57 /** FTP data channel interface */
58 struct xfer_interface data;
62 /** Buffer to be filled with data received via the control channel */
64 /** Remaining size of recvbuf */
66 /** FTP status code, as text */
68 /** Passive-mode parameters, as text */
69 char passive_text[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
75 * @v refcnt Reference counter
77 static void ftp_free ( struct refcnt *refcnt ) {
78 struct ftp_request *ftp =
79 container_of ( refcnt, struct ftp_request, refcnt );
81 DBGC ( ftp, "FTP %p freed\n", ftp );
88 * Mark FTP operation as complete
91 * @v rc Return status code
93 static void ftp_done ( struct ftp_request *ftp, int rc ) {
95 DBGC ( ftp, "FTP %p completed (%s)\n", ftp, strerror ( rc ) );
97 /* Close all data transfer interfaces */
98 xfer_nullify ( &ftp->xfer );
99 xfer_close ( &ftp->xfer, rc );
100 xfer_nullify ( &ftp->control );
101 xfer_close ( &ftp->control, rc );
102 xfer_nullify ( &ftp->data );
103 xfer_close ( &ftp->data, rc );
106 /*****************************************************************************
108 * FTP control channel
113 * FTP control channel strings
115 * These are used as printf() format strings. Since only one of them
116 * (RETR) takes an argument, we always supply that argument to the
119 static const char * ftp_strings[] = {
120 [FTP_CONNECT] = NULL,
121 [FTP_USER] = "USER anonymous\r\n",
122 [FTP_PASS] = "PASS etherboot@etherboot.org\r\n",
123 [FTP_TYPE] = "TYPE I\r\n",
124 [FTP_PASV] = "PASV\r\n",
125 [FTP_RETR] = "RETR %s\r\n",
127 [FTP_QUIT] = "QUIT\r\n",
132 * Handle control channel being closed
134 * @v control FTP control channel interface
135 * @v rc Reason for close
137 * When the control channel is closed, the data channel must also be
138 * closed, if it is currently open.
140 static void ftp_control_close ( struct xfer_interface *control, int rc ) {
141 struct ftp_request *ftp =
142 container_of ( control, struct ftp_request, control );
144 DBGC ( ftp, "FTP %p control connection closed: %s\n",
145 ftp, strerror ( rc ) );
147 /* Complete FTP operation */
148 ftp_done ( ftp, rc );
152 * Parse FTP byte sequence value
154 * @v text Text string
155 * @v value Value buffer
156 * @v len Length of value buffer
158 * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
159 * form for IP addresses in PORT commands) into a byte sequence. @c
160 * *text will be updated to point beyond the end of the parsed byte
163 * This function is safe in the presence of malformed data, though the
164 * output is undefined.
166 static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
168 *(value++) = strtoul ( *text, text, 10 );
175 * Move to next state and send the appropriate FTP control string
180 static void ftp_next_state ( struct ftp_request *ftp ) {
182 /* Move to next state */
183 if ( ftp->state < FTP_DONE )
186 /* Send control string if needed */
187 if ( ftp_strings[ftp->state] != NULL ) {
188 DBGC ( ftp, "FTP %p sending ", ftp );
189 DBGC ( ftp, ftp_strings[ftp->state], ftp->uri->path );
190 xfer_printf ( &ftp->control, ftp_strings[ftp->state],
196 * Handle an FTP control channel response
200 * This is called once we have received a complete response line.
202 static void ftp_reply ( struct ftp_request *ftp ) {
203 char status_major = ftp->status_text[0];
204 char separator = ftp->status_text[3];
206 DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
208 /* Ignore malformed lines */
209 if ( separator != ' ' )
212 /* Ignore "intermediate" responses (1xx codes) */
213 if ( status_major == '1' )
216 /* Anything other than success (2xx) or, in the case of a
217 * repsonse to a "USER" command, a password prompt (3xx), is a
220 if ( ! ( ( status_major == '2' ) ||
221 ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
222 /* Flag protocol error and close connections */
223 ftp_done ( ftp, -EPROTO );
227 /* Open passive connection when we get "PASV" response */
228 if ( ftp->state == FTP_PASV ) {
229 char *ptr = ftp->passive_text;
231 struct sockaddr_in sin;
236 sa.sin.sin_family = AF_INET;
237 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
238 sizeof ( sa.sin.sin_addr ) );
239 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
240 sizeof ( sa.sin.sin_port ) );
241 if ( ( rc = xfer_open_socket ( &ftp->data, SOCK_STREAM,
242 &sa.sa, NULL ) ) != 0 ) {
243 DBGC ( ftp, "FTP %p could not open data connection\n",
245 ftp_done ( ftp, rc );
250 /* Move to next state and send control string */
251 ftp_next_state ( ftp );
256 * Handle new data arriving on FTP control channel
258 * @v control FTP control channel interface
260 * @v len Length of new data
262 * Data is collected until a complete line is received, at which point
263 * its information is passed to ftp_reply().
265 static int ftp_control_deliver_raw ( struct xfer_interface *control,
266 const void *data, size_t len ) {
267 struct ftp_request *ftp =
268 container_of ( control, struct ftp_request, control );
269 char *recvbuf = ftp->recvbuf;
270 size_t recvsize = ftp->recvsize;
274 c = * ( ( char * ) data++ );
278 /* End of line: call ftp_reply() to handle
279 * completed reply. Avoid calling ftp_reply()
280 * twice if we receive both \r and \n.
284 /* Start filling up the status code buffer */
285 recvbuf = ftp->status_text;
286 recvsize = sizeof ( ftp->status_text ) - 1;
289 /* Start filling up the passive parameter buffer */
290 recvbuf = ftp->passive_text;
291 recvsize = sizeof ( ftp->passive_text ) - 1;
294 /* Stop filling the passive parameter buffer */
298 /* Fill up buffer if applicable */
299 if ( recvsize > 0 ) {
307 /* Store for next invocation */
308 ftp->recvbuf = recvbuf;
309 ftp->recvsize = recvsize;
314 /** FTP control channel operations */
315 static struct xfer_interface_operations ftp_control_operations = {
316 .close = ftp_control_close,
317 .vredirect = xfer_vopen,
318 .window = unlimited_xfer_window,
319 .alloc_iob = default_xfer_alloc_iob,
320 .deliver_iob = xfer_deliver_as_raw,
321 .deliver_raw = ftp_control_deliver_raw,
324 /*****************************************************************************
331 * Handle FTP data channel being closed
333 * @v data FTP data channel interface
334 * @v rc Reason for closure
336 * When the data channel is closed, the control channel should be left
337 * alone; the server will send a completion message via the control
338 * channel which we'll pick up.
340 * If the data channel is closed due to an error, we abort the request.
342 static void ftp_data_closed ( struct xfer_interface *data, int rc ) {
343 struct ftp_request *ftp =
344 container_of ( data, struct ftp_request, data );
346 DBGC ( ftp, "FTP %p data connection closed: %s\n",
347 ftp, strerror ( rc ) );
349 /* If there was an error, close control channel and record status */
351 ftp_done ( ftp, rc );
353 ftp_next_state ( ftp );
358 * Handle data delivery via FTP data channel
360 * @v xfer FTP data channel interface
361 * @v iobuf I/O buffer
362 * @v meta Data transfer metadata, or NULL
363 * @ret rc Return status code
365 static int ftp_data_deliver_iob ( struct xfer_interface *data,
366 struct io_buffer *iobuf,
367 struct xfer_metadata *meta __unused ) {
368 struct ftp_request *ftp =
369 container_of ( data, struct ftp_request, data );
372 if ( ( rc = xfer_deliver_iob ( &ftp->xfer, iobuf ) ) != 0 ) {
373 DBGC ( ftp, "FTP %p failed to deliver data: %s\n",
374 ftp, strerror ( rc ) );
381 /** FTP data channel operations */
382 static struct xfer_interface_operations ftp_data_operations = {
383 .close = ftp_data_closed,
384 .vredirect = xfer_vopen,
385 .window = unlimited_xfer_window,
386 .alloc_iob = default_xfer_alloc_iob,
387 .deliver_iob = ftp_data_deliver_iob,
388 .deliver_raw = xfer_deliver_as_iob,
391 /*****************************************************************************
393 * Data transfer interface
398 * Close FTP data transfer interface
400 * @v xfer FTP data transfer interface
401 * @v rc Reason for close
403 static void ftp_xfer_closed ( struct xfer_interface *xfer, int rc ) {
404 struct ftp_request *ftp =
405 container_of ( xfer, struct ftp_request, xfer );
407 DBGC ( ftp, "FTP %p data transfer interface closed: %s\n",
408 ftp, strerror ( rc ) );
410 ftp_done ( ftp, rc );
413 /** FTP data transfer interface operations */
414 static struct xfer_interface_operations ftp_xfer_operations = {
415 .close = ftp_xfer_closed,
416 .vredirect = ignore_xfer_vredirect,
417 .window = unlimited_xfer_window,
418 .alloc_iob = default_xfer_alloc_iob,
419 .deliver_iob = xfer_deliver_as_raw,
420 .deliver_raw = ignore_xfer_deliver_raw,
423 /*****************************************************************************
430 * Initiate an FTP connection
432 * @v xfer Data transfer interface
433 * @v uri Uniform Resource Identifier
434 * @ret rc Return status code
436 static int ftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
437 struct ftp_request *ftp;
438 struct sockaddr_tcpip server;
447 /* Allocate and populate structure */
448 ftp = zalloc ( sizeof ( *ftp ) );
451 ftp->refcnt.free = ftp_free;
452 xfer_init ( &ftp->xfer, &ftp_xfer_operations, &ftp->refcnt );
453 ftp->uri = uri_get ( uri );
454 xfer_init ( &ftp->control, &ftp_control_operations, &ftp->refcnt );
455 xfer_init ( &ftp->data, &ftp_data_operations, &ftp->refcnt );
456 ftp->recvbuf = ftp->status_text;
457 ftp->recvsize = sizeof ( ftp->status_text ) - 1;
459 DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
461 /* Open control connection */
462 memset ( &server, 0, sizeof ( server ) );
463 server.st_port = htons ( uri_port ( uri, FTP_PORT ) );
464 if ( ( rc = xfer_open_named_socket ( &ftp->control, SOCK_STREAM,
465 ( struct sockaddr * ) &server,
466 uri->host, NULL ) ) != 0 )
469 /* Attach to parent interface, mortalise self, and return */
470 xfer_plug_plug ( &ftp->xfer, xfer );
471 ref_put ( &ftp->refcnt );
475 DBGC ( ftp, "FTP %p could not create request: %s\n",
476 ftp, strerror ( rc ) );
477 ftp_done ( ftp, rc );
478 ref_put ( &ftp->refcnt );
482 /** FTP URI opener */
483 struct uri_opener ftp_uri_opener __uri_opener = {