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
112 /** An FTP control channel string */
113 struct ftp_control_string {
114 /** Literal portion */
119 * @ret string Variable portion of string
121 const char * ( *variable ) ( struct ftp_request *ftp );
125 * Retrieve FTP pathname
128 * @ret path FTP pathname
130 static const char * ftp_uri_path ( struct ftp_request *ftp ) {
131 return ftp->uri->path;
134 /** FTP control channel strings */
135 static struct ftp_control_string ftp_strings[] = {
136 [FTP_CONNECT] = { NULL, NULL },
137 [FTP_USER] = { "USER anonymous", NULL },
138 [FTP_PASS] = { "PASS etherboot@etherboot.org", NULL },
139 [FTP_TYPE] = { "TYPE I", NULL },
140 [FTP_PASV] = { "PASV", NULL },
141 [FTP_RETR] = { "RETR ", ftp_uri_path },
142 [FTP_WAIT] = { NULL, NULL },
143 [FTP_QUIT] = { "QUIT", NULL },
144 [FTP_DONE] = { NULL, NULL },
148 * Handle control channel being closed
150 * @v control FTP control channel interface
151 * @v rc Reason for close
153 * When the control channel is closed, the data channel must also be
154 * closed, if it is currently open.
156 static void ftp_control_close ( struct xfer_interface *control, int rc ) {
157 struct ftp_request *ftp =
158 container_of ( control, struct ftp_request, control );
160 DBGC ( ftp, "FTP %p control connection closed: %s\n",
161 ftp, strerror ( rc ) );
163 /* Complete FTP operation */
164 ftp_done ( ftp, rc );
168 * Parse FTP byte sequence value
170 * @v text Text string
171 * @v value Value buffer
172 * @v len Length of value buffer
174 * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
175 * form for IP addresses in PORT commands) into a byte sequence. @c
176 * *text will be updated to point beyond the end of the parsed byte
179 * This function is safe in the presence of malformed data, though the
180 * output is undefined.
182 static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
184 *(value++) = strtoul ( *text, text, 10 );
191 * Move to next state and send the appropriate FTP control string
196 static void ftp_next_state ( struct ftp_request *ftp ) {
197 struct ftp_control_string *ftp_string;
199 const char *variable;
201 /* Move to next state */
202 if ( ftp->state < FTP_DONE )
205 /* Send control string if needed */
206 ftp_string = &ftp_strings[ftp->state];
207 literal = ftp_string->literal;
208 variable = ( ftp_string->variable ?
209 ftp_string->variable ( ftp ) : "" );
211 DBGC ( ftp, "FTP %p sending %s%s\n", ftp, literal, variable );
212 xfer_printf ( &ftp->control, "%s%s\r\n", literal, variable );
217 * Handle an FTP control channel response
221 * This is called once we have received a complete response line.
223 static void ftp_reply ( struct ftp_request *ftp ) {
224 char status_major = ftp->status_text[0];
225 char separator = ftp->status_text[3];
227 DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
229 /* Ignore malformed lines */
230 if ( separator != ' ' )
233 /* Ignore "intermediate" responses (1xx codes) */
234 if ( status_major == '1' )
237 /* Anything other than success (2xx) or, in the case of a
238 * repsonse to a "USER" command, a password prompt (3xx), is a
241 if ( ! ( ( status_major == '2' ) ||
242 ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
243 /* Flag protocol error and close connections */
244 ftp_done ( ftp, -EPROTO );
248 /* Open passive connection when we get "PASV" response */
249 if ( ftp->state == FTP_PASV ) {
250 char *ptr = ftp->passive_text;
252 struct sockaddr_in sin;
257 sa.sin.sin_family = AF_INET;
258 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
259 sizeof ( sa.sin.sin_addr ) );
260 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
261 sizeof ( sa.sin.sin_port ) );
262 if ( ( rc = xfer_open_socket ( &ftp->data, SOCK_STREAM,
263 &sa.sa, NULL ) ) != 0 ) {
264 DBGC ( ftp, "FTP %p could not open data connection\n",
266 ftp_done ( ftp, rc );
271 /* Move to next state and send control string */
272 ftp_next_state ( ftp );
277 * Handle new data arriving on FTP control channel
279 * @v control FTP control channel interface
281 * @v len Length of new data
283 * Data is collected until a complete line is received, at which point
284 * its information is passed to ftp_reply().
286 static int ftp_control_deliver_raw ( struct xfer_interface *control,
287 const void *data, size_t len ) {
288 struct ftp_request *ftp =
289 container_of ( control, struct ftp_request, control );
290 char *recvbuf = ftp->recvbuf;
291 size_t recvsize = ftp->recvsize;
295 c = * ( ( char * ) data++ );
299 /* End of line: call ftp_reply() to handle
300 * completed reply. Avoid calling ftp_reply()
301 * twice if we receive both \r and \n.
305 /* Start filling up the status code buffer */
306 recvbuf = ftp->status_text;
307 recvsize = sizeof ( ftp->status_text ) - 1;
310 /* Start filling up the passive parameter buffer */
311 recvbuf = ftp->passive_text;
312 recvsize = sizeof ( ftp->passive_text ) - 1;
315 /* Stop filling the passive parameter buffer */
319 /* Fill up buffer if applicable */
320 if ( recvsize > 0 ) {
328 /* Store for next invocation */
329 ftp->recvbuf = recvbuf;
330 ftp->recvsize = recvsize;
335 /** FTP control channel operations */
336 static struct xfer_interface_operations ftp_control_operations = {
337 .close = ftp_control_close,
338 .vredirect = xfer_vreopen,
339 .window = unlimited_xfer_window,
340 .alloc_iob = default_xfer_alloc_iob,
341 .deliver_iob = xfer_deliver_as_raw,
342 .deliver_raw = ftp_control_deliver_raw,
345 /*****************************************************************************
352 * Handle FTP data channel being closed
354 * @v data FTP data channel interface
355 * @v rc Reason for closure
357 * When the data channel is closed, the control channel should be left
358 * alone; the server will send a completion message via the control
359 * channel which we'll pick up.
361 * If the data channel is closed due to an error, we abort the request.
363 static void ftp_data_closed ( struct xfer_interface *data, int rc ) {
364 struct ftp_request *ftp =
365 container_of ( data, struct ftp_request, data );
367 DBGC ( ftp, "FTP %p data connection closed: %s\n",
368 ftp, strerror ( rc ) );
370 /* If there was an error, close control channel and record status */
372 ftp_done ( ftp, rc );
374 ftp_next_state ( ftp );
379 * Handle data delivery via FTP data channel
381 * @v xfer FTP data channel interface
382 * @v iobuf I/O buffer
383 * @v meta Data transfer metadata
384 * @ret rc Return status code
386 static int ftp_data_deliver_iob ( struct xfer_interface *data,
387 struct io_buffer *iobuf,
388 struct xfer_metadata *meta __unused ) {
389 struct ftp_request *ftp =
390 container_of ( data, struct ftp_request, data );
393 if ( ( rc = xfer_deliver_iob ( &ftp->xfer, iobuf ) ) != 0 ) {
394 DBGC ( ftp, "FTP %p failed to deliver data: %s\n",
395 ftp, strerror ( rc ) );
402 /** FTP data channel operations */
403 static struct xfer_interface_operations ftp_data_operations = {
404 .close = ftp_data_closed,
405 .vredirect = xfer_vreopen,
406 .window = unlimited_xfer_window,
407 .alloc_iob = default_xfer_alloc_iob,
408 .deliver_iob = ftp_data_deliver_iob,
409 .deliver_raw = xfer_deliver_as_iob,
412 /*****************************************************************************
414 * Data transfer interface
419 * Close FTP data transfer interface
421 * @v xfer FTP data transfer interface
422 * @v rc Reason for close
424 static void ftp_xfer_closed ( struct xfer_interface *xfer, int rc ) {
425 struct ftp_request *ftp =
426 container_of ( xfer, struct ftp_request, xfer );
428 DBGC ( ftp, "FTP %p data transfer interface closed: %s\n",
429 ftp, strerror ( rc ) );
431 ftp_done ( ftp, rc );
434 /** FTP data transfer interface operations */
435 static struct xfer_interface_operations ftp_xfer_operations = {
436 .close = ftp_xfer_closed,
437 .vredirect = ignore_xfer_vredirect,
438 .window = unlimited_xfer_window,
439 .alloc_iob = default_xfer_alloc_iob,
440 .deliver_iob = xfer_deliver_as_raw,
441 .deliver_raw = ignore_xfer_deliver_raw,
444 /*****************************************************************************
451 * Initiate an FTP connection
453 * @v xfer Data transfer interface
454 * @v uri Uniform Resource Identifier
455 * @ret rc Return status code
457 static int ftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
458 struct ftp_request *ftp;
459 struct sockaddr_tcpip server;
468 /* Allocate and populate structure */
469 ftp = zalloc ( sizeof ( *ftp ) );
472 ftp->refcnt.free = ftp_free;
473 xfer_init ( &ftp->xfer, &ftp_xfer_operations, &ftp->refcnt );
474 ftp->uri = uri_get ( uri );
475 xfer_init ( &ftp->control, &ftp_control_operations, &ftp->refcnt );
476 xfer_init ( &ftp->data, &ftp_data_operations, &ftp->refcnt );
477 ftp->recvbuf = ftp->status_text;
478 ftp->recvsize = sizeof ( ftp->status_text ) - 1;
480 DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
482 /* Open control connection */
483 memset ( &server, 0, sizeof ( server ) );
484 server.st_port = htons ( uri_port ( uri, FTP_PORT ) );
485 if ( ( rc = xfer_open_named_socket ( &ftp->control, SOCK_STREAM,
486 ( struct sockaddr * ) &server,
487 uri->host, NULL ) ) != 0 )
490 /* Attach to parent interface, mortalise self, and return */
491 xfer_plug_plug ( &ftp->xfer, xfer );
492 ref_put ( &ftp->refcnt );
496 DBGC ( ftp, "FTP %p could not create request: %s\n",
497 ftp, strerror ( rc ) );
498 ftp_done ( ftp, rc );
499 ref_put ( &ftp->refcnt );
503 /** FTP URI opener */
504 struct uri_opener ftp_uri_opener __uri_opener = {