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/settings.h>
36 #include <gpxe/dhcp.h>
38 #include <gpxe/tftp.h>
46 FEATURE ( FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1 );
48 /* TFTP-specific error codes */
49 #define ETFTP_INVALID_BLKSIZE EUNIQ_01
50 #define ETFTP_INVALID_TSIZE EUNIQ_02
51 #define ETFTP_MC_NO_PORT EUNIQ_03
52 #define ETFTP_MC_NO_MC EUNIQ_04
53 #define ETFTP_MC_INVALID_MC EUNIQ_05
54 #define ETFTP_MC_INVALID_IP EUNIQ_06
55 #define ETFTP_MC_INVALID_PORT EUNIQ_07
60 * This data structure holds the state for an ongoing TFTP transfer.
63 /** Reference count */
65 /** Data transfer interface */
66 struct xfer_interface xfer;
68 /** URI being fetched */
70 /** Transport layer interface */
71 struct xfer_interface socket;
72 /** Multicast transport layer interface */
73 struct xfer_interface mc_socket;
77 * This is the "blksize" option negotiated with the TFTP
78 * server. (If the TFTP server does not support TFTP options,
79 * this will default to 512).
84 * This is the value returned in the "tsize" option from the
85 * TFTP server. If the TFTP server does not support the
86 * "tsize" option, this value will be zero.
92 * This is the port to which RRQ packets are sent.
97 * The peer address is determined by the first response
98 * received to the TFTP RRQ.
100 struct sockaddr_tcpip peer;
103 /** MTFTP timeout count */
104 unsigned int mtftp_timeouts;
107 struct bitmap bitmap;
108 /** Maximum known length
110 * We don't always know the file length in advance. In
111 * particular, if the TFTP server doesn't support the tsize
112 * option, or we are using MTFTP, then we don't know the file
113 * length until we see the end-of-file block (which, in the
114 * case of MTFTP, may not be the last block we see).
116 * This value is updated whenever we obtain information about
120 /** Retransmission timer */
121 struct retry_timer timer;
124 /** TFTP request flags */
126 /** Send ACK packets */
127 TFTP_FL_SEND_ACK = 0x0001,
128 /** Request blksize and tsize options */
129 TFTP_FL_RRQ_SIZES = 0x0002,
130 /** Request multicast option */
131 TFTP_FL_RRQ_MULTICAST = 0x0004,
132 /** Perform MTFTP recovery on timeout */
133 TFTP_FL_MTFTP_RECOVERY = 0x0008,
136 /** Maximum number of MTFTP open requests before falling back to TFTP */
137 #define MTFTP_MAX_TIMEOUTS 3
142 * @v refcnt Reference counter
144 static void tftp_free ( struct refcnt *refcnt ) {
145 struct tftp_request *tftp =
146 container_of ( refcnt, struct tftp_request, refcnt );
148 uri_put ( tftp->uri );
149 bitmap_free ( &tftp->bitmap );
154 * Mark TFTP request as complete
156 * @v tftp TFTP connection
157 * @v rc Return status code
159 static void tftp_done ( struct tftp_request *tftp, int rc ) {
161 DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
162 tftp, rc, strerror ( rc ) );
164 /* Stop the retry timer */
165 stop_timer ( &tftp->timer );
167 /* Close all data transfer interfaces */
168 xfer_nullify ( &tftp->socket );
169 xfer_close ( &tftp->socket, rc );
170 xfer_nullify ( &tftp->mc_socket );
171 xfer_close ( &tftp->mc_socket, rc );
172 xfer_nullify ( &tftp->xfer );
173 xfer_close ( &tftp->xfer, rc );
179 * @v tftp TFTP connection
180 * @ret rc Return status code
182 static int tftp_reopen ( struct tftp_request *tftp ) {
183 struct sockaddr_tcpip server;
187 xfer_close ( &tftp->socket, 0 );
189 /* Disable ACK sending. */
190 tftp->flags &= ~TFTP_FL_SEND_ACK;
192 /* Reset peer address */
193 memset ( &tftp->peer, 0, sizeof ( tftp->peer ) );
196 memset ( &server, 0, sizeof ( server ) );
197 server.st_port = htons ( tftp->port );
198 if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
199 ( struct sockaddr * ) &server,
200 tftp->uri->host, NULL ) ) != 0 ) {
201 DBGC ( tftp, "TFTP %p could not open socket: %s\n",
202 tftp, strerror ( rc ) );
210 * Reopen TFTP multicast socket
212 * @v tftp TFTP connection
213 * @v local Local socket address
214 * @ret rc Return status code
216 static int tftp_reopen_mc ( struct tftp_request *tftp,
217 struct sockaddr *local ) {
220 /* Close multicast socket */
221 xfer_close ( &tftp->mc_socket, 0 );
223 /* Open multicast socket. We never send via this socket, so
224 * use the local address as the peer address (since the peer
225 * address cannot be NULL).
227 if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
228 local, local ) ) != 0 ) {
229 DBGC ( tftp, "TFTP %p could not open multicast "
230 "socket: %s\n", tftp, strerror ( rc ) );
238 * Presize TFTP receive buffers and block bitmap
240 * @v tftp TFTP connection
241 * @v filesize Known minimum file size
242 * @ret rc Return status code
244 static int tftp_presize ( struct tftp_request *tftp, size_t filesize ) {
245 unsigned int num_blocks;
248 /* Do nothing if we are already large enough */
249 if ( filesize <= tftp->filesize )
252 /* Record filesize */
253 tftp->filesize = filesize;
255 /* Notify recipient of file size */
256 xfer_seek ( &tftp->xfer, filesize, SEEK_SET );
257 xfer_seek ( &tftp->xfer, 0, SEEK_SET );
259 /* Calculate expected number of blocks. Note that files whose
260 * length is an exact multiple of the blocksize will have a
261 * trailing zero-length block, which must be included.
263 num_blocks = ( ( filesize / tftp->blksize ) + 1 );
264 if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
265 DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
266 "%s\n", tftp, num_blocks, strerror ( rc ) );
274 * TFTP requested blocksize
276 * This is treated as a global configuration parameter.
278 static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
281 * Set TFTP request blocksize
283 * @v blksize Requested block size
285 void tftp_set_request_blksize ( unsigned int blksize ) {
286 if ( blksize < TFTP_DEFAULT_BLKSIZE )
287 blksize = TFTP_DEFAULT_BLKSIZE;
288 tftp_request_blksize = blksize;
292 * MTFTP multicast receive address
294 * This is treated as a global configuration parameter.
296 static struct sockaddr_in tftp_mtftp_socket = {
297 .sin_family = AF_INET,
298 .sin_addr.s_addr = htonl ( 0xefff0101 ),
299 .sin_port = htons ( 3001 ),
303 * Set MTFTP multicast address
305 * @v address Multicast IPv4 address
307 void tftp_set_mtftp_address ( struct in_addr address ) {
308 tftp_mtftp_socket.sin_addr = address;
312 * Set MTFTP multicast port
314 * @v port Multicast port
316 void tftp_set_mtftp_port ( unsigned int port ) {
317 tftp_mtftp_socket.sin_port = htons ( port );
323 * @v tftp TFTP connection
324 * @ret rc Return status code
326 static int tftp_send_rrq ( struct tftp_request *tftp ) {
327 struct tftp_rrq *rrq;
330 struct io_buffer *iobuf;
332 /* Strip initial '/' if present. If we were opened via the
333 * URI interface, then there will be an initial '/', since a
334 * full tftp:// URI provides no way to specify a non-absolute
335 * path. However, many TFTP servers (particularly Windows
336 * TFTP servers) complain about having an initial '/', and it
337 * violates user expectations to have a '/' silently added to
338 * the DHCP-specified filename.
340 path = tftp->uri->path;
344 DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
346 /* Allocate buffer */
347 len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
348 + 5 + 1 /* "octet" + NUL */
349 + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
350 + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
351 + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
352 iobuf = xfer_alloc_iob ( &tftp->socket, len );
357 rrq = iob_put ( iobuf, sizeof ( *rrq ) );
358 rrq->opcode = htons ( TFTP_RRQ );
359 iob_put ( iobuf, snprintf ( iobuf->tail, iob_tailroom ( iobuf ),
360 "%s%coctet", path, 0 ) + 1 );
361 if ( tftp->flags & TFTP_FL_RRQ_SIZES ) {
362 iob_put ( iobuf, snprintf ( iobuf->tail,
363 iob_tailroom ( iobuf ),
364 "blksize%c%d%ctsize%c0", 0,
365 tftp_request_blksize, 0, 0 ) + 1 );
367 if ( tftp->flags & TFTP_FL_RRQ_MULTICAST ) {
368 iob_put ( iobuf, snprintf ( iobuf->tail,
369 iob_tailroom ( iobuf ),
370 "multicast%c", 0 ) + 1 );
373 /* RRQ always goes to the address specified in the initial
376 return xfer_deliver_iob ( &tftp->socket, iobuf );
382 * @v tftp TFTP connection
383 * @ret rc Return status code
385 static int tftp_send_ack ( struct tftp_request *tftp ) {
386 struct tftp_ack *ack;
387 struct io_buffer *iobuf;
388 struct xfer_metadata meta = {
389 .dest = ( struct sockaddr * ) &tftp->peer,
393 /* Determine next required block number */
394 block = bitmap_first_gap ( &tftp->bitmap );
395 DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
397 /* Allocate buffer */
398 iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
403 ack = iob_put ( iobuf, sizeof ( *ack ) );
404 ack->opcode = htons ( TFTP_ACK );
405 ack->block = htons ( block );
407 /* ACK always goes to the peer recorded from the RRQ response */
408 return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
412 * Transmit next relevant packet
414 * @v tftp TFTP connection
415 * @ret rc Return status code
417 static int tftp_send_packet ( struct tftp_request *tftp ) {
419 /* Update retransmission timer */
420 stop_timer ( &tftp->timer );
421 start_timer ( &tftp->timer );
423 /* Send RRQ or ACK as appropriate */
424 if ( ! tftp->peer.st_family ) {
425 return tftp_send_rrq ( tftp );
427 if ( tftp->flags & TFTP_FL_SEND_ACK ) {
428 return tftp_send_ack ( tftp );
436 * Handle TFTP retransmission timer expiry
438 * @v timer Retry timer
439 * @v fail Failure indicator
441 static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
442 struct tftp_request *tftp =
443 container_of ( timer, struct tftp_request, timer );
446 /* If we are doing MTFTP, attempt the various recovery strategies */
447 if ( tftp->flags & TFTP_FL_MTFTP_RECOVERY ) {
448 if ( tftp->peer.st_family ) {
449 /* If we have received any response from the server,
450 * try resending the RRQ to restart the download.
452 DBGC ( tftp, "TFTP %p attempting reopen\n", tftp );
453 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
456 /* Fall back to plain TFTP after several attempts */
457 tftp->mtftp_timeouts++;
458 DBGC ( tftp, "TFTP %p timeout %d waiting for MTFTP "
459 "open\n", tftp, tftp->mtftp_timeouts );
461 if ( tftp->mtftp_timeouts > MTFTP_MAX_TIMEOUTS ) {
462 DBGC ( tftp, "TFTP %p falling back to plain "
464 tftp->flags = TFTP_FL_RRQ_SIZES;
466 /* Close multicast socket */
467 xfer_close ( &tftp->mc_socket, 0 );
469 /* Reset retry timer */
470 start_timer_nodelay ( &tftp->timer );
472 /* The blocksize may change: discard
475 bitmap_free ( &tftp->bitmap );
476 memset ( &tftp->bitmap, 0,
477 sizeof ( tftp->bitmap ) );
479 /* Reopen on standard TFTP port */
480 tftp->port = TFTP_PORT;
481 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
486 /* Not doing MTFTP (or have fallen back to plain
487 * TFTP); fail as per normal.
494 tftp_send_packet ( tftp );
498 tftp_done ( tftp, rc );
502 * Process TFTP "blksize" option
504 * @v tftp TFTP connection
505 * @v value Option value
506 * @ret rc Return status code
508 static int tftp_process_blksize ( struct tftp_request *tftp,
509 const char *value ) {
512 tftp->blksize = strtoul ( value, &end, 10 );
514 DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
516 return -( EINVAL | ETFTP_INVALID_BLKSIZE );
518 DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
524 * Process TFTP "tsize" option
526 * @v tftp TFTP connection
527 * @v value Option value
528 * @ret rc Return status code
530 static int tftp_process_tsize ( struct tftp_request *tftp,
531 const char *value ) {
534 tftp->tsize = strtoul ( value, &end, 10 );
536 DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
538 return -( EINVAL | ETFTP_INVALID_TSIZE );
540 DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
546 * Process TFTP "multicast" option
548 * @v tftp TFTP connection
549 * @v value Option value
550 * @ret rc Return status code
552 static int tftp_process_multicast ( struct tftp_request *tftp,
553 const char *value ) {
556 struct sockaddr_in sin;
558 char buf[ strlen ( value ) + 1 ];
566 /* Split value into "addr,port,mc" fields */
567 memcpy ( buf, value, sizeof ( buf ) );
569 port = strchr ( addr, ',' );
571 DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
572 return -( EINVAL | ETFTP_MC_NO_PORT );
575 mc = strchr ( port, ',' );
577 DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
578 return -( EINVAL | ETFTP_MC_NO_MC );
582 /* Parse parameters */
583 if ( strtoul ( mc, &mc_end, 0 ) == 0 )
584 tftp->flags &= ~TFTP_FL_SEND_ACK;
586 DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
587 return -( EINVAL | ETFTP_MC_INVALID_MC );
589 DBGC ( tftp, "TFTP %p is%s the master client\n",
590 tftp, ( ( tftp->flags & TFTP_FL_SEND_ACK ) ? "" : " not" ) );
591 if ( *addr && *port ) {
592 socket.sin.sin_family = AF_INET;
593 if ( inet_aton ( addr, &socket.sin.sin_addr ) == 0 ) {
594 DBGC ( tftp, "TFTP %p multicast invalid IP address "
595 "%s\n", tftp, addr );
596 return -( EINVAL | ETFTP_MC_INVALID_IP );
598 DBGC ( tftp, "TFTP %p multicast IP address %s\n",
599 tftp, inet_ntoa ( socket.sin.sin_addr ) );
600 socket.sin.sin_port = htons ( strtoul ( port, &port_end, 0 ) );
602 DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
604 return -( EINVAL | ETFTP_MC_INVALID_PORT );
606 DBGC ( tftp, "TFTP %p multicast port %d\n",
607 tftp, ntohs ( socket.sin.sin_port ) );
608 if ( ( rc = tftp_reopen_mc ( tftp, &socket.sa ) ) != 0 )
621 * @v tftp TFTP connection
622 * @v value Option value
623 * @ret rc Return status code
625 int ( * process ) ( struct tftp_request *tftp, const char *value );
628 /** Recognised TFTP options */
629 static struct tftp_option tftp_options[] = {
630 { "blksize", tftp_process_blksize },
631 { "tsize", tftp_process_tsize },
632 { "multicast", tftp_process_multicast },
637 * Process TFTP option
639 * @v tftp TFTP connection
640 * @v name Option name
641 * @v value Option value
642 * @ret rc Return status code
644 static int tftp_process_option ( struct tftp_request *tftp,
645 const char *name, const char *value ) {
646 struct tftp_option *option;
648 for ( option = tftp_options ; option->name ; option++ ) {
649 if ( strcasecmp ( name, option->name ) == 0 )
650 return option->process ( tftp, value );
653 DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
656 /* Unknown options should be silently ignored */
663 * @v tftp TFTP connection
664 * @v buf Temporary data buffer
665 * @v len Length of temporary data buffer
666 * @ret rc Return status code
668 static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
669 struct tftp_oack *oack = buf;
670 char *end = buf + len;
676 if ( len < sizeof ( *oack ) ) {
677 DBGC ( tftp, "TFTP %p received underlength OACK packet "
678 "length %zd\n", tftp, len );
682 if ( end[-1] != '\0' ) {
683 DBGC ( tftp, "TFTP %p received OACK missing final NUL\n",
689 /* Process each option in turn */
691 while ( name < end ) {
692 value = ( name + strlen ( name ) + 1 );
693 if ( value == end ) {
694 DBGC ( tftp, "TFTP %p received OACK missing value "
695 "for option \"%s\"\n", tftp, name );
699 if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
701 name = ( value + strlen ( value ) + 1 );
704 /* Process tsize information, if available */
706 if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
710 /* Request next data block */
711 tftp_send_packet ( tftp );
715 tftp_done ( tftp, rc );
722 * @v tftp TFTP connection
723 * @v iobuf I/O buffer
724 * @ret rc Return status code
726 * Takes ownership of I/O buffer.
728 static int tftp_rx_data ( struct tftp_request *tftp,
729 struct io_buffer *iobuf ) {
730 struct tftp_data *data = iobuf->data;
731 struct xfer_metadata meta;
738 if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
739 DBGC ( tftp, "TFTP %p received underlength DATA packet "
740 "length %zd\n", tftp, iob_len ( iobuf ) );
744 if ( data->block == 0 ) {
745 DBGC ( tftp, "TFTP %p received data block 0\n", tftp );
751 block = ( ntohs ( data->block ) - 1 );
752 offset = ( block * tftp->blksize );
753 iob_pull ( iobuf, sizeof ( *data ) );
754 data_len = iob_len ( iobuf );
755 if ( data_len > tftp->blksize ) {
756 DBGC ( tftp, "TFTP %p received overlength DATA packet "
757 "length %zd\n", tftp, data_len );
763 memset ( &meta, 0, sizeof ( meta ) );
764 meta.whence = SEEK_SET;
765 meta.offset = offset;
766 if ( ( rc = xfer_deliver_iob_meta ( &tftp->xfer, iob_disown ( iobuf ),
768 DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
769 tftp, strerror ( rc ) );
773 /* Ensure block bitmap is ready */
774 if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
777 /* Mark block as received */
778 bitmap_set ( &tftp->bitmap, block );
780 /* Acknowledge block */
781 tftp_send_packet ( tftp );
783 /* If all blocks have been received, finish. */
784 if ( bitmap_full ( &tftp->bitmap ) )
785 tftp_done ( tftp, 0 );
790 tftp_done ( tftp, rc );
794 /** Translation between TFTP errors and internal error numbers */
795 static const int tftp_errors[] = {
796 [TFTP_ERR_FILE_NOT_FOUND] = ENOENT,
797 [TFTP_ERR_ACCESS_DENIED] = EACCES,
798 [TFTP_ERR_ILLEGAL_OP] = ENOTSUP,
804 * @v tftp TFTP connection
805 * @v buf Temporary data buffer
806 * @v len Length of temporary data buffer
807 * @ret rc Return status code
809 static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
810 struct tftp_error *error = buf;
815 if ( len < sizeof ( *error ) ) {
816 DBGC ( tftp, "TFTP %p received underlength ERROR packet "
817 "length %zd\n", tftp, len );
821 DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
822 "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
824 /* Determine final operation result */
825 err = ntohs ( error->errcode );
826 if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
827 rc = -tftp_errors[err];
831 /* Close TFTP request */
832 tftp_done ( tftp, rc );
840 * @v tftp TFTP connection
841 * @v iobuf I/O buffer
842 * @v meta Transfer metadata, or NULL
843 * @ret rc Return status code
845 static int tftp_rx ( struct tftp_request *tftp,
846 struct io_buffer *iobuf,
847 struct xfer_metadata *meta ) {
848 struct sockaddr_tcpip *st_src;
849 struct tftp_common *common = iobuf->data;
850 size_t len = iob_len ( iobuf );
854 if ( len < sizeof ( *common ) ) {
855 DBGC ( tftp, "TFTP %p received underlength packet length "
856 "%zd\n", tftp, len );
860 DBGC ( tftp, "TFTP %p received packet without metadata\n",
865 DBGC ( tftp, "TFTP %p received packet without source port\n",
870 /* Filter by TID. Set TID on first response received */
871 st_src = ( struct sockaddr_tcpip * ) meta->src;
872 if ( ! tftp->peer.st_family ) {
873 memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
874 DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
875 ntohs ( tftp->peer.st_port ) );
876 } else if ( memcmp ( &tftp->peer, st_src,
877 sizeof ( tftp->peer ) ) != 0 ) {
878 DBGC ( tftp, "TFTP %p received packet from wrong source (got "
879 "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
880 ntohs ( tftp->peer.st_port ) );
884 switch ( common->opcode ) {
885 case htons ( TFTP_OACK ):
886 rc = tftp_rx_oack ( tftp, iobuf->data, len );
888 case htons ( TFTP_DATA ):
889 rc = tftp_rx_data ( tftp, iob_disown ( iobuf ) );
891 case htons ( TFTP_ERROR ):
892 rc = tftp_rx_error ( tftp, iobuf->data, len );
895 DBGC ( tftp, "TFTP %p received strange packet type %d\n",
896 tftp, ntohs ( common->opcode ) );
906 * Receive new data via socket
908 * @v socket Transport layer interface
909 * @v iobuf I/O buffer
910 * @v meta Transfer metadata, or NULL
911 * @ret rc Return status code
913 static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
914 struct io_buffer *iobuf,
915 struct xfer_metadata *meta ) {
916 struct tftp_request *tftp =
917 container_of ( socket, struct tftp_request, socket );
919 /* Enable sending ACKs when we receive a unicast packet. This
920 * covers three cases:
922 * 1. Standard TFTP; we should always send ACKs, and will
923 * always receive a unicast packet before we need to send the
926 * 2. RFC2090 multicast TFTP; the only unicast packets we will
927 * receive are the OACKs; enable sending ACKs here (before
928 * processing the OACK) and disable it when processing the
929 * multicast option if we are not the master client.
931 * 3. MTFTP; receiving a unicast datagram indicates that we
932 * are the "master client" and should send ACKs.
934 tftp->flags |= TFTP_FL_SEND_ACK;
936 return tftp_rx ( tftp, iobuf, meta );
939 /** TFTP socket operations */
940 static struct xfer_interface_operations tftp_socket_operations = {
941 .close = ignore_xfer_close,
942 .vredirect = xfer_vopen,
943 .window = unlimited_xfer_window,
944 .alloc_iob = default_xfer_alloc_iob,
945 .deliver_iob = tftp_socket_deliver_iob,
946 .deliver_raw = xfer_deliver_as_iob,
950 * Receive new data via multicast socket
952 * @v mc_socket Multicast transport layer interface
953 * @v iobuf I/O buffer
954 * @v meta Transfer metadata, or NULL
955 * @ret rc Return status code
957 static int tftp_mc_socket_deliver_iob ( struct xfer_interface *mc_socket,
958 struct io_buffer *iobuf,
959 struct xfer_metadata *meta ) {
960 struct tftp_request *tftp =
961 container_of ( mc_socket, struct tftp_request, mc_socket );
963 return tftp_rx ( tftp, iobuf, meta );
966 /** TFTP multicast socket operations */
967 static struct xfer_interface_operations tftp_mc_socket_operations = {
968 .close = ignore_xfer_close,
969 .vredirect = xfer_vopen,
970 .window = unlimited_xfer_window,
971 .alloc_iob = default_xfer_alloc_iob,
972 .deliver_iob = tftp_mc_socket_deliver_iob,
973 .deliver_raw = xfer_deliver_as_iob,
977 * Close TFTP data transfer interface
979 * @v xfer Data transfer interface
980 * @v rc Reason for close
982 static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
983 struct tftp_request *tftp =
984 container_of ( xfer, struct tftp_request, xfer );
986 DBGC ( tftp, "TFTP %p interface closed: %s\n",
987 tftp, strerror ( rc ) );
989 tftp_done ( tftp, rc );
993 * Check flow control window
995 * @v xfer Data transfer interface
996 * @ret len Length of window
998 static size_t tftp_xfer_window ( struct xfer_interface *xfer ) {
999 struct tftp_request *tftp =
1000 container_of ( xfer, struct tftp_request, xfer );
1002 /* We abuse this data-xfer method to convey the blocksize to
1003 * the caller. This really should be done using some kind of
1004 * stat() method, but we don't yet have the facility to do
1007 return tftp->blksize;
1010 /** TFTP data transfer interface operations */
1011 static struct xfer_interface_operations tftp_xfer_operations = {
1012 .close = tftp_xfer_close,
1013 .vredirect = ignore_xfer_vredirect,
1014 .window = tftp_xfer_window,
1015 .alloc_iob = default_xfer_alloc_iob,
1016 .deliver_iob = xfer_deliver_as_raw,
1017 .deliver_raw = ignore_xfer_deliver_raw,
1021 * Initiate TFTP/TFTM/MTFTP download
1023 * @v xfer Data transfer interface
1024 * @v uri Uniform Resource Identifier
1025 * @ret rc Return status code
1027 static int tftp_core_open ( struct xfer_interface *xfer, struct uri *uri,
1028 unsigned int default_port,
1029 struct sockaddr *multicast,
1030 unsigned int flags ) {
1031 struct tftp_request *tftp;
1040 /* Allocate and populate TFTP structure */
1041 tftp = zalloc ( sizeof ( *tftp ) );
1044 tftp->refcnt.free = tftp_free;
1045 xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
1046 tftp->uri = uri_get ( uri );
1047 xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
1048 xfer_init ( &tftp->mc_socket, &tftp_mc_socket_operations,
1050 tftp->blksize = TFTP_DEFAULT_BLKSIZE;
1051 tftp->flags = flags;
1052 tftp->timer.expired = tftp_timer_expired;
1055 tftp->port = uri_port ( tftp->uri, default_port );
1056 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
1059 /* Open multicast socket */
1061 if ( ( rc = tftp_reopen_mc ( tftp, multicast ) ) != 0 )
1065 /* Start timer to initiate RRQ */
1066 start_timer_nodelay ( &tftp->timer );
1068 /* Attach to parent interface, mortalise self, and return */
1069 xfer_plug_plug ( &tftp->xfer, xfer );
1070 ref_put ( &tftp->refcnt );
1074 DBGC ( tftp, "TFTP %p could not create request: %s\n",
1075 tftp, strerror ( rc ) );
1076 tftp_done ( tftp, rc );
1077 ref_put ( &tftp->refcnt );
1082 * Initiate TFTP download
1084 * @v xfer Data transfer interface
1085 * @v uri Uniform Resource Identifier
1086 * @ret rc Return status code
1088 static int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
1089 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1090 TFTP_FL_RRQ_SIZES );
1094 /** TFTP URI opener */
1095 struct uri_opener tftp_uri_opener __uri_opener = {
1101 * Initiate TFTM download
1103 * @v xfer Data transfer interface
1104 * @v uri Uniform Resource Identifier
1105 * @ret rc Return status code
1107 static int tftm_open ( struct xfer_interface *xfer, struct uri *uri ) {
1108 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1109 ( TFTP_FL_RRQ_SIZES |
1110 TFTP_FL_RRQ_MULTICAST ) );
1114 /** TFTM URI opener */
1115 struct uri_opener tftm_uri_opener __uri_opener = {
1121 * Initiate MTFTP download
1123 * @v xfer Data transfer interface
1124 * @v uri Uniform Resource Identifier
1125 * @ret rc Return status code
1127 static int mtftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
1128 return tftp_core_open ( xfer, uri, MTFTP_PORT,
1129 ( struct sockaddr * ) &tftp_mtftp_socket,
1130 TFTP_FL_MTFTP_RECOVERY );
1133 /** MTFTP URI opener */
1134 struct uri_opener mtftp_uri_opener __uri_opener = {
1139 /******************************************************************************
1143 ******************************************************************************
1146 /** TFTP server setting */
1147 struct setting next_server_setting __setting = {
1148 .name = "next-server",
1149 .description = "TFTP server",
1150 .tag = DHCP_EB_SIADDR,
1151 .type = &setting_type_ipv4,
1155 * Apply TFTP configuration settings
1157 * @ret rc Return status code
1159 static int tftp_apply_settings ( void ) {
1160 static struct in_addr tftp_server = { 0 };
1161 struct in_addr last_tftp_server;
1162 char uri_string[32];
1165 /* Retrieve TFTP server setting */
1166 last_tftp_server = tftp_server;
1167 fetch_ipv4_setting ( NULL, &next_server_setting, &tftp_server );
1169 /* If TFTP server setting has changed, set the current working
1170 * URI to match. Do it only when the TFTP server has changed
1171 * to try to minimise surprises to the user, who probably
1172 * won't expect the CWURI to change just because they updated
1173 * an unrelated setting and triggered all the settings
1176 if ( tftp_server.s_addr != last_tftp_server.s_addr ) {
1177 snprintf ( uri_string, sizeof ( uri_string ),
1178 "tftp://%s/", inet_ntoa ( tftp_server ) );
1179 uri = parse_uri ( uri_string );
1189 /** TFTP settings applicator */
1190 struct settings_applicator tftp_settings_applicator __settings_applicator = {
1191 .apply = tftp_apply_settings,