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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
30 #include <ipxe/refcnt.h>
31 #include <ipxe/iobuf.h>
32 #include <ipxe/xfer.h>
33 #include <ipxe/open.h>
35 #include <ipxe/tcpip.h>
36 #include <ipxe/retry.h>
37 #include <ipxe/features.h>
38 #include <ipxe/bitmap.h>
39 #include <ipxe/settings.h>
40 #include <ipxe/dhcp.h>
42 #include <ipxe/tftp.h>
50 FEATURE ( FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1 );
52 /* TFTP-specific error codes */
53 #define EINVAL_BLKSIZE __einfo_error ( EINFO_EINVAL_BLKSIZE )
54 #define EINFO_EINVAL_BLKSIZE __einfo_uniqify \
55 ( EINFO_EINVAL, 0x01, "Invalid blksize" )
56 #define EINVAL_TSIZE __einfo_error ( EINFO_EINVAL_TSIZE )
57 #define EINFO_EINVAL_TSIZE __einfo_uniqify \
58 ( EINFO_EINVAL, 0x02, "Invalid tsize" )
59 #define EINVAL_MC_NO_PORT __einfo_error ( EINFO_EINVAL_MC_NO_PORT )
60 #define EINFO_EINVAL_MC_NO_PORT __einfo_uniqify \
61 ( EINFO_EINVAL, 0x03, "Missing multicast port" )
62 #define EINVAL_MC_NO_MC __einfo_error ( EINFO_EINVAL_MC_NO_MC )
63 #define EINFO_EINVAL_MC_NO_MC __einfo_uniqify \
64 ( EINFO_EINVAL, 0x04, "Missing multicast mc" )
65 #define EINVAL_MC_INVALID_MC __einfo_error ( EINFO_EINVAL_MC_INVALID_MC )
66 #define EINFO_EINVAL_MC_INVALID_MC __einfo_uniqify \
67 ( EINFO_EINVAL, 0x05, "Missing multicast IP" )
68 #define EINVAL_MC_INVALID_IP __einfo_error ( EINFO_EINVAL_MC_INVALID_IP )
69 #define EINFO_EINVAL_MC_INVALID_IP __einfo_uniqify \
70 ( EINFO_EINVAL, 0x06, "Invalid multicast IP" )
71 #define EINVAL_MC_INVALID_PORT __einfo_error ( EINFO_EINVAL_MC_INVALID_PORT )
72 #define EINFO_EINVAL_MC_INVALID_PORT __einfo_uniqify \
73 ( EINFO_EINVAL, 0x07, "Invalid multicast port" )
78 * This data structure holds the state for an ongoing TFTP transfer.
81 /** Reference count */
83 /** Data transfer interface */
84 struct interface xfer;
86 /** URI being fetched */
88 /** Transport layer interface */
89 struct interface socket;
90 /** Multicast transport layer interface */
91 struct interface mc_socket;
95 * This is the "blksize" option negotiated with the TFTP
96 * server. (If the TFTP server does not support TFTP options,
97 * this will default to 512).
102 * This is the value returned in the "tsize" option from the
103 * TFTP server. If the TFTP server does not support the
104 * "tsize" option, this value will be zero.
110 * This is the port to which RRQ packets are sent.
115 * The peer address is determined by the first response
116 * received to the TFTP RRQ.
118 struct sockaddr_tcpip peer;
121 /** MTFTP timeout count */
122 unsigned int mtftp_timeouts;
125 struct bitmap bitmap;
126 /** Maximum known length
128 * We don't always know the file length in advance. In
129 * particular, if the TFTP server doesn't support the tsize
130 * option, or we are using MTFTP, then we don't know the file
131 * length until we see the end-of-file block (which, in the
132 * case of MTFTP, may not be the last block we see).
134 * This value is updated whenever we obtain information about
138 /** Retransmission timer */
139 struct retry_timer timer;
142 /** TFTP request flags */
144 /** Send ACK packets */
145 TFTP_FL_SEND_ACK = 0x0001,
146 /** Request blksize and tsize options */
147 TFTP_FL_RRQ_SIZES = 0x0002,
148 /** Request multicast option */
149 TFTP_FL_RRQ_MULTICAST = 0x0004,
150 /** Perform MTFTP recovery on timeout */
151 TFTP_FL_MTFTP_RECOVERY = 0x0008,
152 /** Only get filesize and then abort the transfer */
153 TFTP_FL_SIZEONLY = 0x0010,
156 /** Maximum number of MTFTP open requests before falling back to TFTP */
157 #define MTFTP_MAX_TIMEOUTS 3
162 * @v refcnt Reference counter
164 static void tftp_free ( struct refcnt *refcnt ) {
165 struct tftp_request *tftp =
166 container_of ( refcnt, struct tftp_request, refcnt );
168 uri_put ( tftp->uri );
169 bitmap_free ( &tftp->bitmap );
174 * Mark TFTP request as complete
176 * @v tftp TFTP connection
177 * @v rc Return status code
179 static void tftp_done ( struct tftp_request *tftp, int rc ) {
181 DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
182 tftp, rc, strerror ( rc ) );
184 /* Stop the retry timer */
185 stop_timer ( &tftp->timer );
187 /* Close all data transfer interfaces */
188 intf_shutdown ( &tftp->socket, rc );
189 intf_shutdown ( &tftp->mc_socket, rc );
190 intf_shutdown ( &tftp->xfer, rc );
196 * @v tftp TFTP connection
197 * @ret rc Return status code
199 static int tftp_reopen ( struct tftp_request *tftp ) {
200 struct sockaddr_tcpip server;
204 intf_restart ( &tftp->socket, 0 );
206 /* Disable ACK sending. */
207 tftp->flags &= ~TFTP_FL_SEND_ACK;
209 /* Reset peer address */
210 memset ( &tftp->peer, 0, sizeof ( tftp->peer ) );
213 memset ( &server, 0, sizeof ( server ) );
214 server.st_port = htons ( tftp->port );
215 if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
216 ( struct sockaddr * ) &server,
217 tftp->uri->host, NULL ) ) != 0 ) {
218 DBGC ( tftp, "TFTP %p could not open socket: %s\n",
219 tftp, strerror ( rc ) );
227 * Reopen TFTP multicast socket
229 * @v tftp TFTP connection
230 * @v local Local socket address
231 * @ret rc Return status code
233 static int tftp_reopen_mc ( struct tftp_request *tftp,
234 struct sockaddr *local ) {
237 /* Close multicast socket */
238 intf_restart ( &tftp->mc_socket, 0 );
240 /* Open multicast socket. We never send via this socket, so
241 * use the local address as the peer address (since the peer
242 * address cannot be NULL).
244 if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
245 local, local ) ) != 0 ) {
246 DBGC ( tftp, "TFTP %p could not open multicast "
247 "socket: %s\n", tftp, strerror ( rc ) );
255 * Presize TFTP receive buffers and block bitmap
257 * @v tftp TFTP connection
258 * @v filesize Known minimum file size
259 * @ret rc Return status code
261 static int tftp_presize ( struct tftp_request *tftp, size_t filesize ) {
262 unsigned int num_blocks;
265 /* Do nothing if we are already large enough */
266 if ( filesize <= tftp->filesize )
269 /* Record filesize */
270 tftp->filesize = filesize;
272 /* Notify recipient of file size */
273 xfer_seek ( &tftp->xfer, filesize );
274 xfer_seek ( &tftp->xfer, 0 );
276 /* Calculate expected number of blocks. Note that files whose
277 * length is an exact multiple of the blocksize will have a
278 * trailing zero-length block, which must be included.
280 num_blocks = ( ( filesize / tftp->blksize ) + 1 );
281 if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
282 DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
283 "%s\n", tftp, num_blocks, strerror ( rc ) );
291 * MTFTP multicast receive address
293 * This is treated as a global configuration parameter.
295 static struct sockaddr_in tftp_mtftp_socket = {
296 .sin_family = AF_INET,
297 .sin_addr.s_addr = htonl ( 0xefff0101 ),
298 .sin_port = htons ( 3001 ),
302 * Set MTFTP multicast address
304 * @v address Multicast IPv4 address
306 void tftp_set_mtftp_address ( struct in_addr address ) {
307 tftp_mtftp_socket.sin_addr = address;
311 * Set MTFTP multicast port
313 * @v port Multicast port
315 void tftp_set_mtftp_port ( unsigned int port ) {
316 tftp_mtftp_socket.sin_port = htons ( port );
322 * @v tftp TFTP connection
323 * @ret rc Return status code
325 static int tftp_send_rrq ( struct tftp_request *tftp ) {
326 const char *path = tftp->uri->path;
327 struct tftp_rrq *rrq;
329 struct io_buffer *iobuf;
332 DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
334 /* Allocate buffer */
335 len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
336 + 5 + 1 /* "octet" + NUL */
337 + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
338 + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
339 + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
340 iobuf = xfer_alloc_iob ( &tftp->socket, len );
344 /* Determine block size */
345 blksize = xfer_window ( &tftp->xfer );
346 if ( blksize > TFTP_MAX_BLKSIZE )
347 blksize = TFTP_MAX_BLKSIZE;
350 rrq = iob_put ( iobuf, sizeof ( *rrq ) );
351 rrq->opcode = htons ( TFTP_RRQ );
352 iob_put ( iobuf, snprintf ( iobuf->tail, iob_tailroom ( iobuf ),
353 "%s%coctet", path, 0 ) + 1 );
354 if ( tftp->flags & TFTP_FL_RRQ_SIZES ) {
355 iob_put ( iobuf, snprintf ( iobuf->tail,
356 iob_tailroom ( iobuf ),
357 "blksize%c%zd%ctsize%c0",
358 0, blksize, 0, 0 ) + 1 );
360 if ( tftp->flags & TFTP_FL_RRQ_MULTICAST ) {
361 iob_put ( iobuf, snprintf ( iobuf->tail,
362 iob_tailroom ( iobuf ),
363 "multicast%c", 0 ) + 1 );
366 /* RRQ always goes to the address specified in the initial
369 return xfer_deliver_iob ( &tftp->socket, iobuf );
375 * @v tftp TFTP connection
376 * @ret rc Return status code
378 static int tftp_send_ack ( struct tftp_request *tftp ) {
379 struct tftp_ack *ack;
380 struct io_buffer *iobuf;
381 struct xfer_metadata meta = {
382 .dest = ( struct sockaddr * ) &tftp->peer,
386 /* Determine next required block number */
387 block = bitmap_first_gap ( &tftp->bitmap );
388 DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
390 /* Allocate buffer */
391 iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
396 ack = iob_put ( iobuf, sizeof ( *ack ) );
397 ack->opcode = htons ( TFTP_ACK );
398 ack->block = htons ( block );
400 /* ACK always goes to the peer recorded from the RRQ response */
401 return xfer_deliver ( &tftp->socket, iobuf, &meta );
405 * Transmit ERROR (Abort)
407 * @v tftp TFTP connection
408 * @v errcode TFTP error code
409 * @v errmsg Error message string
410 * @ret rc Return status code
412 static int tftp_send_error ( struct tftp_request *tftp, int errcode,
413 const char *errmsg ) {
414 struct tftp_error *err;
415 struct io_buffer *iobuf;
416 struct xfer_metadata meta = {
417 .dest = ( struct sockaddr * ) &tftp->peer,
421 DBGC2 ( tftp, "TFTP %p sending ERROR %d: %s\n", tftp, errcode,
424 /* Allocate buffer */
425 msglen = sizeof ( *err ) + strlen ( errmsg ) + 1 /* NUL */;
426 iobuf = xfer_alloc_iob ( &tftp->socket, msglen );
431 err = iob_put ( iobuf, msglen );
432 err->opcode = htons ( TFTP_ERROR );
433 err->errcode = htons ( errcode );
434 strcpy ( err->errmsg, errmsg );
436 /* ERR always goes to the peer recorded from the RRQ response */
437 return xfer_deliver ( &tftp->socket, iobuf, &meta );
441 * Transmit next relevant packet
443 * @v tftp TFTP connection
444 * @ret rc Return status code
446 static int tftp_send_packet ( struct tftp_request *tftp ) {
448 /* Update retransmission timer. While name resolution takes place the
449 * window is zero. Avoid unnecessary delay after name resolution
450 * completes by retrying immediately.
452 stop_timer ( &tftp->timer );
453 if ( xfer_window ( &tftp->socket ) ) {
454 start_timer ( &tftp->timer );
456 start_timer_nodelay ( &tftp->timer );
459 /* Send RRQ or ACK as appropriate */
460 if ( ! tftp->peer.st_family ) {
461 return tftp_send_rrq ( tftp );
463 if ( tftp->flags & TFTP_FL_SEND_ACK ) {
464 return tftp_send_ack ( tftp );
472 * Handle TFTP retransmission timer expiry
474 * @v timer Retry timer
475 * @v fail Failure indicator
477 static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
478 struct tftp_request *tftp =
479 container_of ( timer, struct tftp_request, timer );
482 /* If we are doing MTFTP, attempt the various recovery strategies */
483 if ( tftp->flags & TFTP_FL_MTFTP_RECOVERY ) {
484 if ( tftp->peer.st_family ) {
485 /* If we have received any response from the server,
486 * try resending the RRQ to restart the download.
488 DBGC ( tftp, "TFTP %p attempting reopen\n", tftp );
489 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
492 /* Fall back to plain TFTP after several attempts */
493 tftp->mtftp_timeouts++;
494 DBGC ( tftp, "TFTP %p timeout %d waiting for MTFTP "
495 "open\n", tftp, tftp->mtftp_timeouts );
497 if ( tftp->mtftp_timeouts > MTFTP_MAX_TIMEOUTS ) {
498 DBGC ( tftp, "TFTP %p falling back to plain "
500 tftp->flags = TFTP_FL_RRQ_SIZES;
502 /* Close multicast socket */
503 intf_restart ( &tftp->mc_socket, 0 );
505 /* Reset retry timer */
506 start_timer_nodelay ( &tftp->timer );
508 /* The blocksize may change: discard
511 bitmap_free ( &tftp->bitmap );
512 memset ( &tftp->bitmap, 0,
513 sizeof ( tftp->bitmap ) );
515 /* Reopen on standard TFTP port */
516 tftp->port = TFTP_PORT;
517 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
522 /* Not doing MTFTP (or have fallen back to plain
523 * TFTP); fail as per normal.
530 tftp_send_packet ( tftp );
534 tftp_done ( tftp, rc );
538 * Process TFTP "blksize" option
540 * @v tftp TFTP connection
541 * @v value Option value
542 * @ret rc Return status code
544 static int tftp_process_blksize ( struct tftp_request *tftp,
545 const char *value ) {
548 tftp->blksize = strtoul ( value, &end, 10 );
550 DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
552 return -EINVAL_BLKSIZE;
554 DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
560 * Process TFTP "tsize" option
562 * @v tftp TFTP connection
563 * @v value Option value
564 * @ret rc Return status code
566 static int tftp_process_tsize ( struct tftp_request *tftp,
567 const char *value ) {
570 tftp->tsize = strtoul ( value, &end, 10 );
572 DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
574 return -EINVAL_TSIZE;
576 DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
582 * Process TFTP "multicast" option
584 * @v tftp TFTP connection
585 * @v value Option value
586 * @ret rc Return status code
588 static int tftp_process_multicast ( struct tftp_request *tftp,
589 const char *value ) {
592 struct sockaddr_in sin;
594 char buf[ strlen ( value ) + 1 ];
602 /* Split value into "addr,port,mc" fields */
603 memcpy ( buf, value, sizeof ( buf ) );
605 port = strchr ( addr, ',' );
607 DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
608 return -EINVAL_MC_NO_PORT;
611 mc = strchr ( port, ',' );
613 DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
614 return -EINVAL_MC_NO_MC;
618 /* Parse parameters */
619 if ( strtoul ( mc, &mc_end, 0 ) == 0 )
620 tftp->flags &= ~TFTP_FL_SEND_ACK;
622 DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
623 return -EINVAL_MC_INVALID_MC;
625 DBGC ( tftp, "TFTP %p is%s the master client\n",
626 tftp, ( ( tftp->flags & TFTP_FL_SEND_ACK ) ? "" : " not" ) );
627 if ( *addr && *port ) {
628 socket.sin.sin_family = AF_INET;
629 if ( inet_aton ( addr, &socket.sin.sin_addr ) == 0 ) {
630 DBGC ( tftp, "TFTP %p multicast invalid IP address "
631 "%s\n", tftp, addr );
632 return -EINVAL_MC_INVALID_IP;
634 DBGC ( tftp, "TFTP %p multicast IP address %s\n",
635 tftp, inet_ntoa ( socket.sin.sin_addr ) );
636 socket.sin.sin_port = htons ( strtoul ( port, &port_end, 0 ) );
638 DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
640 return -EINVAL_MC_INVALID_PORT;
642 DBGC ( tftp, "TFTP %p multicast port %d\n",
643 tftp, ntohs ( socket.sin.sin_port ) );
644 if ( ( rc = tftp_reopen_mc ( tftp, &socket.sa ) ) != 0 )
657 * @v tftp TFTP connection
658 * @v value Option value
659 * @ret rc Return status code
661 int ( * process ) ( struct tftp_request *tftp, const char *value );
664 /** Recognised TFTP options */
665 static struct tftp_option tftp_options[] = {
666 { "blksize", tftp_process_blksize },
667 { "tsize", tftp_process_tsize },
668 { "multicast", tftp_process_multicast },
673 * Process TFTP option
675 * @v tftp TFTP connection
676 * @v name Option name
677 * @v value Option value
678 * @ret rc Return status code
680 static int tftp_process_option ( struct tftp_request *tftp,
681 const char *name, const char *value ) {
682 struct tftp_option *option;
684 for ( option = tftp_options ; option->name ; option++ ) {
685 if ( strcasecmp ( name, option->name ) == 0 )
686 return option->process ( tftp, value );
689 DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
692 /* Unknown options should be silently ignored */
699 * @v tftp TFTP connection
700 * @v buf Temporary data buffer
701 * @v len Length of temporary data buffer
702 * @ret rc Return status code
704 static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
705 struct tftp_oack *oack = buf;
706 char *end = buf + len;
713 if ( len < sizeof ( *oack ) ) {
714 DBGC ( tftp, "TFTP %p received underlength OACK packet "
715 "length %zd\n", tftp, len );
720 /* Process each option in turn */
721 for ( name = oack->data ; name < end ; name = next ) {
723 /* Parse option name and value
725 * We treat parsing errors as non-fatal, because there
726 * exists at least one TFTP server (IBM Tivoli PXE
727 * Server 5.1.0.3) that has been observed to send
728 * malformed OACKs containing trailing garbage bytes.
730 value = ( name + strnlen ( name, ( end - name ) ) + 1 );
732 DBGC ( tftp, "TFTP %p received OACK with malformed "
733 "option name:\n", tftp );
734 DBGC_HD ( tftp, oack, len );
737 if ( value == end ) {
738 DBGC ( tftp, "TFTP %p received OACK missing value "
739 "for option \"%s\"\n", tftp, name );
740 DBGC_HD ( tftp, oack, len );
743 next = ( value + strnlen ( value, ( end - value ) ) + 1 );
745 DBGC ( tftp, "TFTP %p received OACK with malformed "
746 "value for option \"%s\":\n", tftp, name );
747 DBGC_HD ( tftp, oack, len );
752 if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
756 /* Process tsize information, if available */
758 if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
762 /* Abort request if only trying to determine file size */
763 if ( tftp->flags & TFTP_FL_SIZEONLY ) {
765 tftp_send_error ( tftp, 0, "TFTP Aborted" );
766 tftp_done ( tftp, rc );
770 /* Request next data block */
771 tftp_send_packet ( tftp );
775 tftp_done ( tftp, rc );
782 * @v tftp TFTP connection
783 * @v iobuf I/O buffer
784 * @ret rc Return status code
786 * Takes ownership of I/O buffer.
788 static int tftp_rx_data ( struct tftp_request *tftp,
789 struct io_buffer *iobuf ) {
790 struct tftp_data *data = iobuf->data;
791 struct xfer_metadata meta;
797 if ( tftp->flags & TFTP_FL_SIZEONLY ) {
798 /* If we get here then server doesn't support SIZE option */
800 tftp_send_error ( tftp, 0, "TFTP Aborted" );
805 if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
806 DBGC ( tftp, "TFTP %p received underlength DATA packet "
807 "length %zd\n", tftp, iob_len ( iobuf ) );
812 /* Calculate block number */
813 block = ( ( bitmap_first_gap ( &tftp->bitmap ) + 1 ) & ~0xffff );
814 if ( data->block == 0 && block == 0 ) {
815 DBGC ( tftp, "TFTP %p received data block 0\n", tftp );
819 block += ( ntohs ( data->block ) - 1 );
822 offset = ( block * tftp->blksize );
823 iob_pull ( iobuf, sizeof ( *data ) );
824 data_len = iob_len ( iobuf );
825 if ( data_len > tftp->blksize ) {
826 DBGC ( tftp, "TFTP %p received overlength DATA packet "
827 "length %zd\n", tftp, data_len );
833 memset ( &meta, 0, sizeof ( meta ) );
834 meta.flags = XFER_FL_ABS_OFFSET;
835 meta.offset = offset;
836 if ( ( rc = xfer_deliver ( &tftp->xfer, iob_disown ( iobuf ),
838 DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
839 tftp, strerror ( rc ) );
843 /* Ensure block bitmap is ready */
844 if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
847 /* Mark block as received */
848 bitmap_set ( &tftp->bitmap, block );
850 /* Acknowledge block */
851 tftp_send_packet ( tftp );
853 /* If all blocks have been received, finish. */
854 if ( bitmap_full ( &tftp->bitmap ) )
855 tftp_done ( tftp, 0 );
860 tftp_done ( tftp, rc );
865 * Convert TFTP error code to return status code
867 * @v errcode TFTP error code
868 * @ret rc Return status code
870 static int tftp_errcode_to_rc ( unsigned int errcode ) {
872 case TFTP_ERR_FILE_NOT_FOUND: return -ENOENT;
873 case TFTP_ERR_ACCESS_DENIED: return -EACCES;
874 case TFTP_ERR_ILLEGAL_OP: return -ENOTTY;
875 default: return -ENOTSUP;
882 * @v tftp TFTP connection
883 * @v buf Temporary data buffer
884 * @v len Length of temporary data buffer
885 * @ret rc Return status code
887 static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
888 struct tftp_error *error = buf;
892 if ( len < sizeof ( *error ) ) {
893 DBGC ( tftp, "TFTP %p received underlength ERROR packet "
894 "length %zd\n", tftp, len );
898 DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
899 "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
901 /* Determine final operation result */
902 rc = tftp_errcode_to_rc ( ntohs ( error->errcode ) );
904 /* Close TFTP request */
905 tftp_done ( tftp, rc );
913 * @v tftp TFTP connection
914 * @v iobuf I/O buffer
915 * @v meta Transfer metadata
916 * @ret rc Return status code
918 static int tftp_rx ( struct tftp_request *tftp,
919 struct io_buffer *iobuf,
920 struct xfer_metadata *meta ) {
921 struct sockaddr_tcpip *st_src;
922 struct tftp_common *common = iobuf->data;
923 size_t len = iob_len ( iobuf );
927 if ( len < sizeof ( *common ) ) {
928 DBGC ( tftp, "TFTP %p received underlength packet length "
929 "%zd\n", tftp, len );
933 DBGC ( tftp, "TFTP %p received packet without source port\n",
938 /* Filter by TID. Set TID on first response received */
939 st_src = ( struct sockaddr_tcpip * ) meta->src;
940 if ( ! tftp->peer.st_family ) {
941 memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
942 DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
943 ntohs ( tftp->peer.st_port ) );
944 } else if ( memcmp ( &tftp->peer, st_src,
945 sizeof ( tftp->peer ) ) != 0 ) {
946 DBGC ( tftp, "TFTP %p received packet from wrong source (got "
947 "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
948 ntohs ( tftp->peer.st_port ) );
952 switch ( common->opcode ) {
953 case htons ( TFTP_OACK ):
954 rc = tftp_rx_oack ( tftp, iobuf->data, len );
956 case htons ( TFTP_DATA ):
957 rc = tftp_rx_data ( tftp, iob_disown ( iobuf ) );
959 case htons ( TFTP_ERROR ):
960 rc = tftp_rx_error ( tftp, iobuf->data, len );
963 DBGC ( tftp, "TFTP %p received strange packet type %d\n",
964 tftp, ntohs ( common->opcode ) );
974 * Receive new data via socket
976 * @v tftp TFTP connection
977 * @v iobuf I/O buffer
978 * @v meta Transfer metadata
979 * @ret rc Return status code
981 static int tftp_socket_deliver ( struct tftp_request *tftp,
982 struct io_buffer *iobuf,
983 struct xfer_metadata *meta ) {
985 /* Enable sending ACKs when we receive a unicast packet. This
986 * covers three cases:
988 * 1. Standard TFTP; we should always send ACKs, and will
989 * always receive a unicast packet before we need to send the
992 * 2. RFC2090 multicast TFTP; the only unicast packets we will
993 * receive are the OACKs; enable sending ACKs here (before
994 * processing the OACK) and disable it when processing the
995 * multicast option if we are not the master client.
997 * 3. MTFTP; receiving a unicast datagram indicates that we
998 * are the "master client" and should send ACKs.
1000 tftp->flags |= TFTP_FL_SEND_ACK;
1002 return tftp_rx ( tftp, iobuf, meta );
1005 /** TFTP socket operations */
1006 static struct interface_operation tftp_socket_operations[] = {
1007 INTF_OP ( xfer_deliver, struct tftp_request *, tftp_socket_deliver ),
1010 /** TFTP socket interface descriptor */
1011 static struct interface_descriptor tftp_socket_desc =
1012 INTF_DESC ( struct tftp_request, socket, tftp_socket_operations );
1014 /** TFTP multicast socket operations */
1015 static struct interface_operation tftp_mc_socket_operations[] = {
1016 INTF_OP ( xfer_deliver, struct tftp_request *, tftp_rx ),
1019 /** TFTP multicast socket interface descriptor */
1020 static struct interface_descriptor tftp_mc_socket_desc =
1021 INTF_DESC ( struct tftp_request, mc_socket, tftp_mc_socket_operations );
1024 * Check flow control window
1026 * @v tftp TFTP connection
1027 * @ret len Length of window
1029 static size_t tftp_xfer_window ( struct tftp_request *tftp ) {
1031 /* We abuse this data-xfer method to convey the blocksize to
1032 * the caller. This really should be done using some kind of
1033 * stat() method, but we don't yet have the facility to do
1036 return tftp->blksize;
1039 /** TFTP data transfer interface operations */
1040 static struct interface_operation tftp_xfer_operations[] = {
1041 INTF_OP ( xfer_window, struct tftp_request *, tftp_xfer_window ),
1042 INTF_OP ( intf_close, struct tftp_request *, tftp_done ),
1045 /** TFTP data transfer interface descriptor */
1046 static struct interface_descriptor tftp_xfer_desc =
1047 INTF_DESC ( struct tftp_request, xfer, tftp_xfer_operations );
1050 * Initiate TFTP/TFTM/MTFTP download
1052 * @v xfer Data transfer interface
1053 * @v uri Uniform Resource Identifier
1054 * @ret rc Return status code
1056 static int tftp_core_open ( struct interface *xfer, struct uri *uri,
1057 unsigned int default_port,
1058 struct sockaddr *multicast,
1059 unsigned int flags ) {
1060 struct tftp_request *tftp;
1069 /* Allocate and populate TFTP structure */
1070 tftp = zalloc ( sizeof ( *tftp ) );
1073 ref_init ( &tftp->refcnt, tftp_free );
1074 intf_init ( &tftp->xfer, &tftp_xfer_desc, &tftp->refcnt );
1075 intf_init ( &tftp->socket, &tftp_socket_desc, &tftp->refcnt );
1076 intf_init ( &tftp->mc_socket, &tftp_mc_socket_desc, &tftp->refcnt );
1077 timer_init ( &tftp->timer, tftp_timer_expired, &tftp->refcnt );
1078 tftp->uri = uri_get ( uri );
1079 tftp->blksize = TFTP_DEFAULT_BLKSIZE;
1080 tftp->flags = flags;
1083 tftp->port = uri_port ( tftp->uri, default_port );
1084 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
1087 /* Open multicast socket */
1089 if ( ( rc = tftp_reopen_mc ( tftp, multicast ) ) != 0 )
1093 /* Start timer to initiate RRQ */
1094 start_timer_nodelay ( &tftp->timer );
1096 /* Attach to parent interface, mortalise self, and return */
1097 intf_plug_plug ( &tftp->xfer, xfer );
1098 ref_put ( &tftp->refcnt );
1102 DBGC ( tftp, "TFTP %p could not create request: %s\n",
1103 tftp, strerror ( rc ) );
1104 tftp_done ( tftp, rc );
1105 ref_put ( &tftp->refcnt );
1110 * Initiate TFTP download
1112 * @v xfer Data transfer interface
1113 * @v uri Uniform Resource Identifier
1114 * @ret rc Return status code
1116 static int tftp_open ( struct interface *xfer, struct uri *uri ) {
1117 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1118 TFTP_FL_RRQ_SIZES );
1122 /** TFTP URI opener */
1123 struct uri_opener tftp_uri_opener __uri_opener = {
1129 * Initiate TFTP-size request
1131 * @v xfer Data transfer interface
1132 * @v uri Uniform Resource Identifier
1133 * @ret rc Return status code
1135 static int tftpsize_open ( struct interface *xfer, struct uri *uri ) {
1136 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1137 ( TFTP_FL_RRQ_SIZES |
1138 TFTP_FL_SIZEONLY ) );
1142 /** TFTP URI opener */
1143 struct uri_opener tftpsize_uri_opener __uri_opener = {
1144 .scheme = "tftpsize",
1145 .open = tftpsize_open,
1149 * Initiate TFTM download
1151 * @v xfer Data transfer interface
1152 * @v uri Uniform Resource Identifier
1153 * @ret rc Return status code
1155 static int tftm_open ( struct interface *xfer, struct uri *uri ) {
1156 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1157 ( TFTP_FL_RRQ_SIZES |
1158 TFTP_FL_RRQ_MULTICAST ) );
1162 /** TFTM URI opener */
1163 struct uri_opener tftm_uri_opener __uri_opener = {
1169 * Initiate MTFTP download
1171 * @v xfer Data transfer interface
1172 * @v uri Uniform Resource Identifier
1173 * @ret rc Return status code
1175 static int mtftp_open ( struct interface *xfer, struct uri *uri ) {
1176 return tftp_core_open ( xfer, uri, MTFTP_PORT,
1177 ( struct sockaddr * ) &tftp_mtftp_socket,
1178 TFTP_FL_MTFTP_RECOVERY );
1181 /** MTFTP URI opener */
1182 struct uri_opener mtftp_uri_opener __uri_opener = {
1187 /******************************************************************************
1191 ******************************************************************************
1195 * Apply TFTP configuration settings
1197 * @ret rc Return status code
1199 static int tftp_apply_settings ( void ) {
1200 static struct in_addr tftp_server = { 0 };
1201 struct in_addr last_tftp_server;
1202 char uri_string[32];
1205 /* Retrieve TFTP server setting */
1206 last_tftp_server = tftp_server;
1207 fetch_ipv4_setting ( NULL, &next_server_setting, &tftp_server );
1209 /* If TFTP server setting has changed, set the current working
1210 * URI to match. Do it only when the TFTP server has changed
1211 * to try to minimise surprises to the user, who probably
1212 * won't expect the CWURI to change just because they updated
1213 * an unrelated setting and triggered all the settings
1216 if ( tftp_server.s_addr != last_tftp_server.s_addr ) {
1217 if ( tftp_server.s_addr ) {
1218 snprintf ( uri_string, sizeof ( uri_string ),
1219 "tftp://%s/", inet_ntoa ( tftp_server ) );
1220 uri = parse_uri ( uri_string );
1233 /** TFTP settings applicator */
1234 struct settings_applicator tftp_settings_applicator __settings_applicator = {
1235 .apply = tftp_apply_settings,