2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <gpxe/refcnt.h>
28 #include <gpxe/xfer.h>
29 #include <gpxe/open.h>
31 #include <gpxe/tcpip.h>
32 #include <gpxe/retry.h>
33 #include <gpxe/features.h>
34 #include <gpxe/bitmap.h>
35 #include <gpxe/tftp.h>
43 FEATURE ( FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1 );
48 * This data structure holds the state for an ongoing TFTP transfer.
51 /** Reference count */
53 /** Data transfer interface */
54 struct xfer_interface xfer;
56 /** URI being fetched */
58 /** Transport layer interface */
59 struct xfer_interface socket;
60 /** Multicast transport layer interface */
61 struct xfer_interface mc_socket;
65 * This is the "blksize" option negotiated with the TFTP
66 * server. (If the TFTP server does not support TFTP options,
67 * this will default to 512).
72 * This is the value returned in the "tsize" option from the
73 * TFTP server. If the TFTP server does not support the
74 * "tsize" option, this value will be zero.
79 * This is the destination address for multicast data
82 struct sockaddr_tcpip multicast;
85 * True if this is the client responsible for sending ACKs.
91 * The peer address is determined by the first response
92 * received to the TFTP RRQ.
94 struct sockaddr_tcpip peer;
97 /** Maximum known length
99 * We don't always know the file length in advance. In
100 * particular, if the TFTP server doesn't support the tsize
101 * option, or we are using MTFTP, then we don't know the file
102 * length until we see the end-of-file block (which, in the
103 * case of MTFTP, may not be the last block we see).
105 * This value is updated whenever we obtain information about
109 /** Retransmission timer */
110 struct retry_timer timer;
116 * @v refcnt Reference counter
118 static void tftp_free ( struct refcnt *refcnt ) {
119 struct tftp_request *tftp =
120 container_of ( refcnt, struct tftp_request, refcnt );
122 uri_put ( tftp->uri );
123 bitmap_free ( &tftp->bitmap );
128 * Mark TFTP request as complete
130 * @v tftp TFTP connection
131 * @v rc Return status code
133 static void tftp_done ( struct tftp_request *tftp, int rc ) {
135 DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
136 tftp, rc, strerror ( rc ) );
138 /* Stop the retry timer */
139 stop_timer ( &tftp->timer );
141 /* Close all data transfer interfaces */
142 xfer_nullify ( &tftp->socket );
143 xfer_close ( &tftp->socket, rc );
144 xfer_nullify ( &tftp->mc_socket );
145 xfer_close ( &tftp->mc_socket, rc );
146 xfer_nullify ( &tftp->xfer );
147 xfer_close ( &tftp->xfer, rc );
151 * Presize TFTP receive buffers and block bitmap
153 * @v tftp TFTP connection
154 * @v filesize Known minimum file size
155 * @ret rc Return status code
157 static int tftp_presize ( struct tftp_request *tftp, size_t filesize ) {
158 unsigned int num_blocks;
161 /* Do nothing if we are already large enough */
162 if ( filesize <= tftp->filesize )
165 /* Record filesize */
166 tftp->filesize = filesize;
168 /* Notify recipient of file size */
169 xfer_seek ( &tftp->xfer, filesize, SEEK_SET );
170 xfer_seek ( &tftp->xfer, 0, SEEK_SET );
172 /* Calculate expected number of blocks. Note that files whose
173 * length is an exact multiple of the blocksize will have a
174 * trailing zero-length block, which must be included.
176 num_blocks = ( ( filesize / tftp->blksize ) + 1 );
177 if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
178 DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
179 "%s\n", tftp, num_blocks, strerror ( rc ) );
187 * TFTP requested blocksize
189 * This is treated as a global configuration parameter.
191 static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
194 * Set TFTP request blocksize
196 * @v blksize Requested block size
198 void tftp_set_request_blksize ( unsigned int blksize ) {
199 if ( blksize < TFTP_DEFAULT_BLKSIZE )
200 blksize = TFTP_DEFAULT_BLKSIZE;
201 tftp_request_blksize = blksize;
207 * @v tftp TFTP connection
208 * @ret rc Return status code
210 static int tftp_send_rrq ( struct tftp_request *tftp ) {
211 struct tftp_rrq *rrq;
212 const char *path = tftp->uri->path;
213 size_t len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
214 + 5 + 1 /* "octet" + NUL */
215 + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
216 + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
217 + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
218 struct io_buffer *iobuf;
220 DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
222 /* Allocate buffer */
223 iobuf = xfer_alloc_iob ( &tftp->socket, len );
228 rrq = iob_put ( iobuf, sizeof ( *rrq ) );
229 rrq->opcode = htons ( TFTP_RRQ );
231 snprintf ( rrq->data, iob_tailroom ( iobuf ),
232 "%s%coctet%cblksize%c%d%ctsize%c0%cmulticast%c",
233 path, 0, 0, 0, tftp_request_blksize, 0,
236 /* RRQ always goes to the address specified in the initial
239 return xfer_deliver_iob ( &tftp->socket, iobuf );
245 * @v tftp TFTP connection
246 * @ret rc Return status code
248 static int tftp_send_ack ( struct tftp_request *tftp ) {
249 struct tftp_ack *ack;
250 struct io_buffer *iobuf;
251 struct xfer_metadata meta = {
252 .dest = ( struct sockaddr * ) &tftp->peer,
256 /* Determine next required block number */
257 block = bitmap_first_gap ( &tftp->bitmap );
258 DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
260 /* Allocate buffer */
261 iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
266 ack = iob_put ( iobuf, sizeof ( *ack ) );
267 ack->opcode = htons ( TFTP_ACK );
268 ack->block = htons ( block );
270 /* ACK always goes to the peer recorded from the RRQ response */
271 return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
275 * Transmit next relevant packet
277 * @v tftp TFTP connection
278 * @ret rc Return status code
280 static int tftp_send_packet ( struct tftp_request *tftp ) {
282 /* Update retransmission timer */
283 stop_timer ( &tftp->timer );
284 start_timer ( &tftp->timer );
286 /* If we are the master client, send RRQ or ACK as appropriate */
287 if ( tftp->master ) {
288 if ( ! tftp->peer.st_family ) {
289 return tftp_send_rrq ( tftp );
291 return tftp_send_ack ( tftp );
294 /* Do nothing when not the master client */
300 * Handle TFTP retransmission timer expiry
302 * @v timer Retry timer
303 * @v fail Failure indicator
305 static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
306 struct tftp_request *tftp =
307 container_of ( timer, struct tftp_request, timer );
310 tftp_done ( tftp, -ETIMEDOUT );
312 tftp_send_packet ( tftp );
317 * Process TFTP "blksize" option
319 * @v tftp TFTP connection
320 * @v value Option value
321 * @ret rc Return status code
323 static int tftp_process_blksize ( struct tftp_request *tftp,
324 const char *value ) {
327 tftp->blksize = strtoul ( value, &end, 10 );
329 DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
333 DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
339 * Process TFTP "tsize" option
341 * @v tftp TFTP connection
342 * @v value Option value
343 * @ret rc Return status code
345 static int tftp_process_tsize ( struct tftp_request *tftp,
346 const char *value ) {
349 tftp->tsize = strtoul ( value, &end, 10 );
351 DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
355 DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
361 * Process TFTP "multicast" option
363 * @v tftp TFTP connection
364 * @v value Option value
365 * @ret rc Return status code
367 static int tftp_process_multicast ( struct tftp_request *tftp,
368 const char *value ) {
369 struct sockaddr_in *sin = ( struct sockaddr_in * ) &tftp->multicast;
370 char buf[ strlen ( value ) + 1 ];
376 struct sockaddr *mc_peer;
377 struct sockaddr *mc_local;
380 /* Split value into "addr,port,mc" fields */
381 memcpy ( buf, value, sizeof ( buf ) );
383 port = strchr ( addr, ',' );
385 DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
389 mc = strchr ( port, ',' );
391 DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
396 /* Parse parameters */
398 if ( inet_aton ( addr, &sin->sin_addr ) == 0 ) {
399 DBGC ( tftp, "TFTP %p multicast invalid IP address "
400 "%s\n", tftp, addr );
403 DBGC ( tftp, "TFTP %p multicast IP address %s\n",
404 tftp, inet_ntoa ( sin->sin_addr ) );
407 sin->sin_port = htons ( strtoul ( port, &port_end, 0 ) );
409 DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
413 DBGC ( tftp, "TFTP %p multicast port %d\n",
414 tftp, ntohs ( sin->sin_port ) );
416 tftp->master = strtoul ( mc, &mc_end, 0 );
418 DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
421 DBGC ( tftp, "TFTP %p is%s the master client\n",
422 tftp, ( tftp->master ? "" : " not" ) );
424 /* Open multicast socket, if new address specified */
425 if ( *addr || *port ) {
426 xfer_close ( &tftp->mc_socket, 0 );
427 mc_peer = ( ( struct sockaddr * ) &tftp->peer );
428 mc_local = ( ( struct sockaddr * ) &tftp->multicast );
429 mc_local->sa_family = mc_peer->sa_family;
430 if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
431 mc_peer, mc_local ) ) != 0 ) {
432 DBGC ( tftp, "TFTP %p could not open multicast "
433 "socket: %s\n", tftp, strerror ( rc ) );
447 * @v tftp TFTP connection
448 * @v value Option value
449 * @ret rc Return status code
451 int ( * process ) ( struct tftp_request *tftp, const char *value );
454 /** Recognised TFTP options */
455 static struct tftp_option tftp_options[] = {
456 { "blksize", tftp_process_blksize },
457 { "tsize", tftp_process_tsize },
458 { "multicast", tftp_process_multicast },
463 * Process TFTP option
465 * @v tftp TFTP connection
466 * @v name Option name
467 * @v value Option value
468 * @ret rc Return status code
470 static int tftp_process_option ( struct tftp_request *tftp,
471 const char *name, const char *value ) {
472 struct tftp_option *option;
474 for ( option = tftp_options ; option->name ; option++ ) {
475 if ( strcasecmp ( name, option->name ) == 0 )
476 return option->process ( tftp, value );
479 DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
482 /* Unknown options should be silently ignored */
489 * @v tftp TFTP connection
490 * @v buf Temporary data buffer
491 * @v len Length of temporary data buffer
492 * @ret rc Return status code
494 static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
495 struct tftp_oack *oack = buf;
496 char *end = buf + len;
502 if ( len < sizeof ( *oack ) ) {
503 DBGC ( tftp, "TFTP %p received underlength OACK packet "
504 "length %zd\n", tftp, len );
508 if ( end[-1] != '\0' ) {
509 DBGC ( tftp, "TFTP %p received OACK missing final NUL\n",
515 /* Process each option in turn */
517 while ( name < end ) {
518 value = ( name + strlen ( name ) + 1 );
519 if ( value == end ) {
520 DBGC ( tftp, "TFTP %p received OACK missing value "
521 "for option \"%s\"\n", tftp, name );
525 if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
527 name = ( value + strlen ( value ) + 1 );
530 /* Process tsize information, if available */
532 if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
536 /* Request next data block */
537 tftp_send_packet ( tftp );
541 tftp_done ( tftp, rc );
548 * @v tftp TFTP connection
549 * @v iobuf I/O buffer
550 * @ret rc Return status code
552 * Takes ownership of I/O buffer.
554 static int tftp_rx_data ( struct tftp_request *tftp,
555 struct io_buffer *iobuf ) {
556 struct tftp_data *data = iobuf->data;
557 struct xfer_metadata meta;
564 if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
565 DBGC ( tftp, "TFTP %p received underlength DATA packet "
566 "length %zd\n", tftp, iob_len ( iobuf ) );
572 block = ( ntohs ( data->block ) - 1 );
573 offset = ( block * tftp->blksize );
574 iob_pull ( iobuf, sizeof ( *data ) );
575 data_len = iob_len ( iobuf );
576 if ( data_len > tftp->blksize ) {
577 DBGC ( tftp, "TFTP %p received overlength DATA packet "
578 "length %zd\n", tftp, data_len );
584 memset ( &meta, 0, sizeof ( meta ) );
585 meta.whence = SEEK_SET;
586 meta.offset = offset;
587 rc = xfer_deliver_iob_meta ( &tftp->xfer, iobuf, &meta );
590 DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
591 tftp, strerror ( rc ) );
595 /* Ensure block bitmap is ready */
596 if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
599 /* Mark block as received */
600 bitmap_set ( &tftp->bitmap, block );
602 /* Acknowledge block */
603 tftp_send_packet ( tftp );
605 /* If all blocks have been received, finish. */
606 if ( bitmap_full ( &tftp->bitmap ) )
607 tftp_done ( tftp, 0 );
612 tftp_done ( tftp, rc );
616 /** Translation between TFTP errors and internal error numbers */
617 static const uint8_t tftp_errors[] = {
618 [TFTP_ERR_FILE_NOT_FOUND] = PXENV_STATUS_TFTP_FILE_NOT_FOUND,
619 [TFTP_ERR_ACCESS_DENIED] = PXENV_STATUS_TFTP_ACCESS_VIOLATION,
620 [TFTP_ERR_ILLEGAL_OP] = PXENV_STATUS_TFTP_UNKNOWN_OPCODE,
626 * @v tftp TFTP connection
627 * @v buf Temporary data buffer
628 * @v len Length of temporary data buffer
629 * @ret rc Return status code
631 static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
632 struct tftp_error *error = buf;
637 if ( len < sizeof ( *error ) ) {
638 DBGC ( tftp, "TFTP %p received underlength ERROR packet "
639 "length %zd\n", tftp, len );
643 DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
644 "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
646 /* Determine final operation result */
647 err = ntohs ( error->errcode );
648 if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
649 rc = -tftp_errors[err];
651 rc = -PXENV_STATUS_TFTP_CANNOT_OPEN_CONNECTION;
653 /* Close TFTP request */
654 tftp_done ( tftp, rc );
662 * @v tftp TFTP connection
663 * @v iobuf I/O buffer
664 * @v meta Transfer metadata, or NULL
665 * @ret rc Return status code
667 static int tftp_rx ( struct tftp_request *tftp,
668 struct io_buffer *iobuf,
669 struct xfer_metadata *meta ) {
670 struct sockaddr_tcpip *st_src;
671 struct tftp_common *common = iobuf->data;
672 size_t len = iob_len ( iobuf );
676 if ( len < sizeof ( *common ) ) {
677 DBGC ( tftp, "TFTP %p received underlength packet length "
678 "%zd\n", tftp, len );
682 DBGC ( tftp, "TFTP %p received packet without metadata\n",
687 DBGC ( tftp, "TFTP %p received packet without source port\n",
692 /* Filter by TID. Set TID on first response received */
693 st_src = ( struct sockaddr_tcpip * ) meta->src;
694 if ( ! tftp->peer.st_family ) {
695 memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
696 DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
697 ntohs ( tftp->peer.st_port ) );
698 } else if ( memcmp ( &tftp->peer, st_src,
699 sizeof ( tftp->peer ) ) != 0 ) {
700 DBGC ( tftp, "TFTP %p received packet from wrong source (got "
701 "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
702 ntohs ( tftp->peer.st_port ) );
706 switch ( common->opcode ) {
707 case htons ( TFTP_OACK ):
708 rc = tftp_rx_oack ( tftp, iobuf->data, len );
710 case htons ( TFTP_DATA ):
711 rc = tftp_rx_data ( tftp, iobuf );
714 case htons ( TFTP_ERROR ):
715 rc = tftp_rx_error ( tftp, iobuf->data, len );
718 DBGC ( tftp, "TFTP %p received strange packet type %d\n",
719 tftp, ntohs ( common->opcode ) );
729 * Receive new data via socket
731 * @v socket Transport layer interface
732 * @v iobuf I/O buffer
733 * @v meta Transfer metadata, or NULL
734 * @ret rc Return status code
736 static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
737 struct io_buffer *iobuf,
738 struct xfer_metadata *meta ) {
739 struct tftp_request *tftp =
740 container_of ( socket, struct tftp_request, socket );
742 return tftp_rx ( tftp, iobuf, meta );
746 * TFTP connection closed by network stack
748 * @v socket Transport layer interface
749 * @v rc Reason for close
751 static void tftp_socket_close ( struct xfer_interface *socket, int rc ) {
752 struct tftp_request *tftp =
753 container_of ( socket, struct tftp_request, socket );
755 DBGC ( tftp, "TFTP %p socket closed: %s\n",
756 tftp, strerror ( rc ) );
758 /* Any close counts as an error */
762 tftp_done ( tftp, rc );
765 /** TFTP socket operations */
766 static struct xfer_interface_operations tftp_socket_operations = {
767 .close = tftp_socket_close,
768 .vredirect = xfer_vopen,
769 .window = unlimited_xfer_window,
770 .alloc_iob = default_xfer_alloc_iob,
771 .deliver_iob = tftp_socket_deliver_iob,
772 .deliver_raw = xfer_deliver_as_iob,
776 * Receive new data via multicast socket
778 * @v mc_socket Multicast transport layer interface
779 * @v iobuf I/O buffer
780 * @v meta Transfer metadata, or NULL
781 * @ret rc Return status code
783 static int tftp_mc_socket_deliver_iob ( struct xfer_interface *mc_socket,
784 struct io_buffer *iobuf,
785 struct xfer_metadata *meta ) {
786 struct tftp_request *tftp =
787 container_of ( mc_socket, struct tftp_request, mc_socket );
789 return tftp_rx ( tftp, iobuf, meta );
793 * TFTP multicast connection closed by network stack
795 * @v socket Multicast transport layer interface
796 * @v rc Reason for close
798 static void tftp_mc_socket_close ( struct xfer_interface *mc_socket,
800 struct tftp_request *tftp =
801 container_of ( mc_socket, struct tftp_request, mc_socket );
803 DBGC ( tftp, "TFTP %p multicast socket closed: %s\n",
804 tftp, strerror ( rc ) );
806 /* The multicast socket may be closed when we receive a new
807 * OACK and open/reopen the socket; we should not call
808 * tftp_done() at this point.
812 /** TFTP multicast socket operations */
813 static struct xfer_interface_operations tftp_mc_socket_operations = {
814 .close = tftp_mc_socket_close,
815 .vredirect = xfer_vopen,
816 .window = unlimited_xfer_window,
817 .alloc_iob = default_xfer_alloc_iob,
818 .deliver_iob = tftp_mc_socket_deliver_iob,
819 .deliver_raw = xfer_deliver_as_iob,
823 * Close TFTP data transfer interface
825 * @v xfer Data transfer interface
826 * @v rc Reason for close
828 static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
829 struct tftp_request *tftp =
830 container_of ( xfer, struct tftp_request, xfer );
832 DBGC ( tftp, "TFTP %p interface closed: %s\n",
833 tftp, strerror ( rc ) );
835 tftp_done ( tftp, rc );
838 /** TFTP data transfer interface operations */
839 static struct xfer_interface_operations tftp_xfer_operations = {
840 .close = tftp_xfer_close,
841 .vredirect = ignore_xfer_vredirect,
842 .window = unlimited_xfer_window,
843 .alloc_iob = default_xfer_alloc_iob,
844 .deliver_iob = xfer_deliver_as_raw,
845 .deliver_raw = ignore_xfer_deliver_raw,
849 * Initiate TFTP download
851 * @v xfer Data transfer interface
852 * @v uri Uniform Resource Identifier
853 * @ret rc Return status code
855 int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
856 struct tftp_request *tftp;
857 struct sockaddr_tcpip server;
866 /* Allocate and populate TFTP structure */
867 tftp = zalloc ( sizeof ( *tftp ) );
870 tftp->refcnt.free = tftp_free;
871 xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
872 tftp->uri = uri_get ( uri );
873 xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
874 xfer_init ( &tftp->mc_socket, &tftp_mc_socket_operations,
876 tftp->blksize = TFTP_DEFAULT_BLKSIZE;
878 tftp->timer.expired = tftp_timer_expired;
881 memset ( &server, 0, sizeof ( server ) );
882 server.st_port = htons ( uri_port ( tftp->uri, TFTP_PORT ) );
883 if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
884 ( struct sockaddr * ) &server,
885 uri->host, NULL ) ) != 0 )
888 /* Start timer to initiate RRQ */
889 start_timer_nodelay ( &tftp->timer );
891 /* Attach to parent interface, mortalise self, and return */
892 xfer_plug_plug ( &tftp->xfer, xfer );
893 ref_put ( &tftp->refcnt );
897 DBGC ( tftp, "TFTP %p could not create request: %s\n",
898 tftp, strerror ( rc ) );
899 tftp_done ( tftp, rc );
900 ref_put ( &tftp->refcnt );
904 /** TFTP URI opener */
905 struct uri_opener tftp_uri_opener __uri_opener = {