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.
19 FILE_LICENCE ( GPL2_OR_LATER );
29 #include <gpxe/refcnt.h>
30 #include <gpxe/xfer.h>
31 #include <gpxe/open.h>
33 #include <gpxe/tcpip.h>
34 #include <gpxe/retry.h>
35 #include <gpxe/features.h>
36 #include <gpxe/bitmap.h>
37 #include <gpxe/settings.h>
38 #include <gpxe/dhcp.h>
40 #include <gpxe/tftp.h>
48 FEATURE ( FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1 );
50 /* TFTP-specific error codes */
51 #define ETFTP_INVALID_BLKSIZE EUNIQ_01
52 #define ETFTP_INVALID_TSIZE EUNIQ_02
53 #define ETFTP_MC_NO_PORT EUNIQ_03
54 #define ETFTP_MC_NO_MC EUNIQ_04
55 #define ETFTP_MC_INVALID_MC EUNIQ_05
56 #define ETFTP_MC_INVALID_IP EUNIQ_06
57 #define ETFTP_MC_INVALID_PORT EUNIQ_07
62 * This data structure holds the state for an ongoing TFTP transfer.
65 /** Reference count */
67 /** Data transfer interface */
68 struct xfer_interface xfer;
70 /** URI being fetched */
72 /** Transport layer interface */
73 struct xfer_interface socket;
74 /** Multicast transport layer interface */
75 struct xfer_interface mc_socket;
79 * This is the "blksize" option negotiated with the TFTP
80 * server. (If the TFTP server does not support TFTP options,
81 * this will default to 512).
86 * This is the value returned in the "tsize" option from the
87 * TFTP server. If the TFTP server does not support the
88 * "tsize" option, this value will be zero.
94 * This is the port to which RRQ packets are sent.
99 * The peer address is determined by the first response
100 * received to the TFTP RRQ.
102 struct sockaddr_tcpip peer;
105 /** MTFTP timeout count */
106 unsigned int mtftp_timeouts;
109 struct bitmap bitmap;
110 /** Maximum known length
112 * We don't always know the file length in advance. In
113 * particular, if the TFTP server doesn't support the tsize
114 * option, or we are using MTFTP, then we don't know the file
115 * length until we see the end-of-file block (which, in the
116 * case of MTFTP, may not be the last block we see).
118 * This value is updated whenever we obtain information about
122 /** Retransmission timer */
123 struct retry_timer timer;
126 /** TFTP request flags */
128 /** Send ACK packets */
129 TFTP_FL_SEND_ACK = 0x0001,
130 /** Request blksize and tsize options */
131 TFTP_FL_RRQ_SIZES = 0x0002,
132 /** Request multicast option */
133 TFTP_FL_RRQ_MULTICAST = 0x0004,
134 /** Perform MTFTP recovery on timeout */
135 TFTP_FL_MTFTP_RECOVERY = 0x0008,
138 /** Maximum number of MTFTP open requests before falling back to TFTP */
139 #define MTFTP_MAX_TIMEOUTS 3
144 * @v refcnt Reference counter
146 static void tftp_free ( struct refcnt *refcnt ) {
147 struct tftp_request *tftp =
148 container_of ( refcnt, struct tftp_request, refcnt );
150 uri_put ( tftp->uri );
151 bitmap_free ( &tftp->bitmap );
156 * Mark TFTP request as complete
158 * @v tftp TFTP connection
159 * @v rc Return status code
161 static void tftp_done ( struct tftp_request *tftp, int rc ) {
163 DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
164 tftp, rc, strerror ( rc ) );
166 /* Stop the retry timer */
167 stop_timer ( &tftp->timer );
169 /* Close all data transfer interfaces */
170 xfer_nullify ( &tftp->socket );
171 xfer_close ( &tftp->socket, rc );
172 xfer_nullify ( &tftp->mc_socket );
173 xfer_close ( &tftp->mc_socket, rc );
174 xfer_nullify ( &tftp->xfer );
175 xfer_close ( &tftp->xfer, rc );
181 * @v tftp TFTP connection
182 * @ret rc Return status code
184 static int tftp_reopen ( struct tftp_request *tftp ) {
185 struct sockaddr_tcpip server;
189 xfer_close ( &tftp->socket, 0 );
191 /* Disable ACK sending. */
192 tftp->flags &= ~TFTP_FL_SEND_ACK;
194 /* Reset peer address */
195 memset ( &tftp->peer, 0, sizeof ( tftp->peer ) );
198 memset ( &server, 0, sizeof ( server ) );
199 server.st_port = htons ( tftp->port );
200 if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
201 ( struct sockaddr * ) &server,
202 tftp->uri->host, NULL ) ) != 0 ) {
203 DBGC ( tftp, "TFTP %p could not open socket: %s\n",
204 tftp, strerror ( rc ) );
212 * Reopen TFTP multicast socket
214 * @v tftp TFTP connection
215 * @v local Local socket address
216 * @ret rc Return status code
218 static int tftp_reopen_mc ( struct tftp_request *tftp,
219 struct sockaddr *local ) {
222 /* Close multicast socket */
223 xfer_close ( &tftp->mc_socket, 0 );
225 /* Open multicast socket. We never send via this socket, so
226 * use the local address as the peer address (since the peer
227 * address cannot be NULL).
229 if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
230 local, local ) ) != 0 ) {
231 DBGC ( tftp, "TFTP %p could not open multicast "
232 "socket: %s\n", tftp, strerror ( rc ) );
240 * Presize TFTP receive buffers and block bitmap
242 * @v tftp TFTP connection
243 * @v filesize Known minimum file size
244 * @ret rc Return status code
246 static int tftp_presize ( struct tftp_request *tftp, size_t filesize ) {
247 unsigned int num_blocks;
250 /* Do nothing if we are already large enough */
251 if ( filesize <= tftp->filesize )
254 /* Record filesize */
255 tftp->filesize = filesize;
257 /* Notify recipient of file size */
258 xfer_seek ( &tftp->xfer, filesize, SEEK_SET );
259 xfer_seek ( &tftp->xfer, 0, SEEK_SET );
261 /* Calculate expected number of blocks. Note that files whose
262 * length is an exact multiple of the blocksize will have a
263 * trailing zero-length block, which must be included.
265 num_blocks = ( ( filesize / tftp->blksize ) + 1 );
266 if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
267 DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
268 "%s\n", tftp, num_blocks, strerror ( rc ) );
276 * TFTP requested blocksize
278 * This is treated as a global configuration parameter.
280 static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
283 * Set TFTP request blocksize
285 * @v blksize Requested block size
287 void tftp_set_request_blksize ( unsigned int blksize ) {
288 if ( blksize < TFTP_DEFAULT_BLKSIZE )
289 blksize = TFTP_DEFAULT_BLKSIZE;
290 tftp_request_blksize = blksize;
294 * MTFTP multicast receive address
296 * This is treated as a global configuration parameter.
298 static struct sockaddr_in tftp_mtftp_socket = {
299 .sin_family = AF_INET,
300 .sin_addr.s_addr = htonl ( 0xefff0101 ),
301 .sin_port = htons ( 3001 ),
305 * Set MTFTP multicast address
307 * @v address Multicast IPv4 address
309 void tftp_set_mtftp_address ( struct in_addr address ) {
310 tftp_mtftp_socket.sin_addr = address;
314 * Set MTFTP multicast port
316 * @v port Multicast port
318 void tftp_set_mtftp_port ( unsigned int port ) {
319 tftp_mtftp_socket.sin_port = htons ( port );
325 * @v tftp TFTP connection
326 * @ret rc Return status code
328 static int tftp_send_rrq ( struct tftp_request *tftp ) {
329 struct tftp_rrq *rrq;
332 struct io_buffer *iobuf;
334 /* Strip initial '/' if present. If we were opened via the
335 * URI interface, then there will be an initial '/', since a
336 * full tftp:// URI provides no way to specify a non-absolute
337 * path. However, many TFTP servers (particularly Windows
338 * TFTP servers) complain about having an initial '/', and it
339 * violates user expectations to have a '/' silently added to
340 * the DHCP-specified filename.
342 path = tftp->uri->path;
346 DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
348 /* Allocate buffer */
349 len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
350 + 5 + 1 /* "octet" + NUL */
351 + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
352 + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
353 + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
354 iobuf = xfer_alloc_iob ( &tftp->socket, len );
359 rrq = iob_put ( iobuf, sizeof ( *rrq ) );
360 rrq->opcode = htons ( TFTP_RRQ );
361 iob_put ( iobuf, snprintf ( iobuf->tail, iob_tailroom ( iobuf ),
362 "%s%coctet", path, 0 ) + 1 );
363 if ( tftp->flags & TFTP_FL_RRQ_SIZES ) {
364 iob_put ( iobuf, snprintf ( iobuf->tail,
365 iob_tailroom ( iobuf ),
366 "blksize%c%d%ctsize%c0", 0,
367 tftp_request_blksize, 0, 0 ) + 1 );
369 if ( tftp->flags & TFTP_FL_RRQ_MULTICAST ) {
370 iob_put ( iobuf, snprintf ( iobuf->tail,
371 iob_tailroom ( iobuf ),
372 "multicast%c", 0 ) + 1 );
375 /* RRQ always goes to the address specified in the initial
378 return xfer_deliver_iob ( &tftp->socket, iobuf );
384 * @v tftp TFTP connection
385 * @ret rc Return status code
387 static int tftp_send_ack ( struct tftp_request *tftp ) {
388 struct tftp_ack *ack;
389 struct io_buffer *iobuf;
390 struct xfer_metadata meta = {
391 .dest = ( struct sockaddr * ) &tftp->peer,
395 /* Determine next required block number */
396 block = bitmap_first_gap ( &tftp->bitmap );
397 DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
399 /* Allocate buffer */
400 iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
405 ack = iob_put ( iobuf, sizeof ( *ack ) );
406 ack->opcode = htons ( TFTP_ACK );
407 ack->block = htons ( block );
409 /* ACK always goes to the peer recorded from the RRQ response */
410 return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
414 * Transmit next relevant packet
416 * @v tftp TFTP connection
417 * @ret rc Return status code
419 static int tftp_send_packet ( struct tftp_request *tftp ) {
421 /* Update retransmission timer */
422 stop_timer ( &tftp->timer );
423 start_timer ( &tftp->timer );
425 /* Send RRQ or ACK as appropriate */
426 if ( ! tftp->peer.st_family ) {
427 return tftp_send_rrq ( tftp );
429 if ( tftp->flags & TFTP_FL_SEND_ACK ) {
430 return tftp_send_ack ( tftp );
438 * Handle TFTP retransmission timer expiry
440 * @v timer Retry timer
441 * @v fail Failure indicator
443 static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
444 struct tftp_request *tftp =
445 container_of ( timer, struct tftp_request, timer );
448 /* If we are doing MTFTP, attempt the various recovery strategies */
449 if ( tftp->flags & TFTP_FL_MTFTP_RECOVERY ) {
450 if ( tftp->peer.st_family ) {
451 /* If we have received any response from the server,
452 * try resending the RRQ to restart the download.
454 DBGC ( tftp, "TFTP %p attempting reopen\n", tftp );
455 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
458 /* Fall back to plain TFTP after several attempts */
459 tftp->mtftp_timeouts++;
460 DBGC ( tftp, "TFTP %p timeout %d waiting for MTFTP "
461 "open\n", tftp, tftp->mtftp_timeouts );
463 if ( tftp->mtftp_timeouts > MTFTP_MAX_TIMEOUTS ) {
464 DBGC ( tftp, "TFTP %p falling back to plain "
466 tftp->flags = TFTP_FL_RRQ_SIZES;
468 /* Close multicast socket */
469 xfer_close ( &tftp->mc_socket, 0 );
471 /* Reset retry timer */
472 start_timer_nodelay ( &tftp->timer );
474 /* The blocksize may change: discard
477 bitmap_free ( &tftp->bitmap );
478 memset ( &tftp->bitmap, 0,
479 sizeof ( tftp->bitmap ) );
481 /* Reopen on standard TFTP port */
482 tftp->port = TFTP_PORT;
483 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
488 /* Not doing MTFTP (or have fallen back to plain
489 * TFTP); fail as per normal.
496 tftp_send_packet ( tftp );
500 tftp_done ( tftp, rc );
504 * Process TFTP "blksize" option
506 * @v tftp TFTP connection
507 * @v value Option value
508 * @ret rc Return status code
510 static int tftp_process_blksize ( struct tftp_request *tftp,
511 const char *value ) {
514 tftp->blksize = strtoul ( value, &end, 10 );
516 DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
518 return -( EINVAL | ETFTP_INVALID_BLKSIZE );
520 DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
526 * Process TFTP "tsize" option
528 * @v tftp TFTP connection
529 * @v value Option value
530 * @ret rc Return status code
532 static int tftp_process_tsize ( struct tftp_request *tftp,
533 const char *value ) {
536 tftp->tsize = strtoul ( value, &end, 10 );
538 DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
540 return -( EINVAL | ETFTP_INVALID_TSIZE );
542 DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
548 * Process TFTP "multicast" option
550 * @v tftp TFTP connection
551 * @v value Option value
552 * @ret rc Return status code
554 static int tftp_process_multicast ( struct tftp_request *tftp,
555 const char *value ) {
558 struct sockaddr_in sin;
560 char buf[ strlen ( value ) + 1 ];
568 /* Split value into "addr,port,mc" fields */
569 memcpy ( buf, value, sizeof ( buf ) );
571 port = strchr ( addr, ',' );
573 DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
574 return -( EINVAL | ETFTP_MC_NO_PORT );
577 mc = strchr ( port, ',' );
579 DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
580 return -( EINVAL | ETFTP_MC_NO_MC );
584 /* Parse parameters */
585 if ( strtoul ( mc, &mc_end, 0 ) == 0 )
586 tftp->flags &= ~TFTP_FL_SEND_ACK;
588 DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
589 return -( EINVAL | ETFTP_MC_INVALID_MC );
591 DBGC ( tftp, "TFTP %p is%s the master client\n",
592 tftp, ( ( tftp->flags & TFTP_FL_SEND_ACK ) ? "" : " not" ) );
593 if ( *addr && *port ) {
594 socket.sin.sin_family = AF_INET;
595 if ( inet_aton ( addr, &socket.sin.sin_addr ) == 0 ) {
596 DBGC ( tftp, "TFTP %p multicast invalid IP address "
597 "%s\n", tftp, addr );
598 return -( EINVAL | ETFTP_MC_INVALID_IP );
600 DBGC ( tftp, "TFTP %p multicast IP address %s\n",
601 tftp, inet_ntoa ( socket.sin.sin_addr ) );
602 socket.sin.sin_port = htons ( strtoul ( port, &port_end, 0 ) );
604 DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
606 return -( EINVAL | ETFTP_MC_INVALID_PORT );
608 DBGC ( tftp, "TFTP %p multicast port %d\n",
609 tftp, ntohs ( socket.sin.sin_port ) );
610 if ( ( rc = tftp_reopen_mc ( tftp, &socket.sa ) ) != 0 )
623 * @v tftp TFTP connection
624 * @v value Option value
625 * @ret rc Return status code
627 int ( * process ) ( struct tftp_request *tftp, const char *value );
630 /** Recognised TFTP options */
631 static struct tftp_option tftp_options[] = {
632 { "blksize", tftp_process_blksize },
633 { "tsize", tftp_process_tsize },
634 { "multicast", tftp_process_multicast },
639 * Process TFTP option
641 * @v tftp TFTP connection
642 * @v name Option name
643 * @v value Option value
644 * @ret rc Return status code
646 static int tftp_process_option ( struct tftp_request *tftp,
647 const char *name, const char *value ) {
648 struct tftp_option *option;
650 for ( option = tftp_options ; option->name ; option++ ) {
651 if ( strcasecmp ( name, option->name ) == 0 )
652 return option->process ( tftp, value );
655 DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
658 /* Unknown options should be silently ignored */
665 * @v tftp TFTP connection
666 * @v buf Temporary data buffer
667 * @v len Length of temporary data buffer
668 * @ret rc Return status code
670 static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
671 struct tftp_oack *oack = buf;
672 char *end = buf + len;
678 if ( len < sizeof ( *oack ) ) {
679 DBGC ( tftp, "TFTP %p received underlength OACK packet "
680 "length %zd\n", tftp, len );
684 if ( end[-1] != '\0' ) {
685 DBGC ( tftp, "TFTP %p received OACK missing final NUL\n",
691 /* Process each option in turn */
693 while ( name < end ) {
694 value = ( name + strlen ( name ) + 1 );
695 if ( value == end ) {
696 DBGC ( tftp, "TFTP %p received OACK missing value "
697 "for option \"%s\"\n", tftp, name );
701 if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
703 name = ( value + strlen ( value ) + 1 );
706 /* Process tsize information, if available */
708 if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
712 /* Request next data block */
713 tftp_send_packet ( tftp );
717 tftp_done ( tftp, rc );
724 * @v tftp TFTP connection
725 * @v iobuf I/O buffer
726 * @ret rc Return status code
728 * Takes ownership of I/O buffer.
730 static int tftp_rx_data ( struct tftp_request *tftp,
731 struct io_buffer *iobuf ) {
732 struct tftp_data *data = iobuf->data;
733 struct xfer_metadata meta;
740 if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
741 DBGC ( tftp, "TFTP %p received underlength DATA packet "
742 "length %zd\n", tftp, iob_len ( iobuf ) );
746 if ( data->block == 0 ) {
747 DBGC ( tftp, "TFTP %p received data block 0\n", tftp );
753 block = ( ntohs ( data->block ) - 1 );
754 offset = ( block * tftp->blksize );
755 iob_pull ( iobuf, sizeof ( *data ) );
756 data_len = iob_len ( iobuf );
757 if ( data_len > tftp->blksize ) {
758 DBGC ( tftp, "TFTP %p received overlength DATA packet "
759 "length %zd\n", tftp, data_len );
765 memset ( &meta, 0, sizeof ( meta ) );
766 meta.whence = SEEK_SET;
767 meta.offset = offset;
768 if ( ( rc = xfer_deliver_iob_meta ( &tftp->xfer, iob_disown ( iobuf ),
770 DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
771 tftp, strerror ( rc ) );
775 /* Ensure block bitmap is ready */
776 if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
779 /* Mark block as received */
780 bitmap_set ( &tftp->bitmap, block );
782 /* Acknowledge block */
783 tftp_send_packet ( tftp );
785 /* If all blocks have been received, finish. */
786 if ( bitmap_full ( &tftp->bitmap ) )
787 tftp_done ( tftp, 0 );
792 tftp_done ( tftp, rc );
796 /** Translation between TFTP errors and internal error numbers */
797 static const int tftp_errors[] = {
798 [TFTP_ERR_FILE_NOT_FOUND] = ENOENT,
799 [TFTP_ERR_ACCESS_DENIED] = EACCES,
800 [TFTP_ERR_ILLEGAL_OP] = ENOTSUP,
806 * @v tftp TFTP connection
807 * @v buf Temporary data buffer
808 * @v len Length of temporary data buffer
809 * @ret rc Return status code
811 static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
812 struct tftp_error *error = buf;
817 if ( len < sizeof ( *error ) ) {
818 DBGC ( tftp, "TFTP %p received underlength ERROR packet "
819 "length %zd\n", tftp, len );
823 DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
824 "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
826 /* Determine final operation result */
827 err = ntohs ( error->errcode );
828 if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
829 rc = -tftp_errors[err];
833 /* Close TFTP request */
834 tftp_done ( tftp, rc );
842 * @v tftp TFTP connection
843 * @v iobuf I/O buffer
844 * @v meta Transfer metadata
845 * @ret rc Return status code
847 static int tftp_rx ( struct tftp_request *tftp,
848 struct io_buffer *iobuf,
849 struct xfer_metadata *meta ) {
850 struct sockaddr_tcpip *st_src;
851 struct tftp_common *common = iobuf->data;
852 size_t len = iob_len ( iobuf );
856 if ( len < sizeof ( *common ) ) {
857 DBGC ( tftp, "TFTP %p received underlength packet length "
858 "%zd\n", tftp, len );
862 DBGC ( tftp, "TFTP %p received packet without source port\n",
867 /* Filter by TID. Set TID on first response received */
868 st_src = ( struct sockaddr_tcpip * ) meta->src;
869 if ( ! tftp->peer.st_family ) {
870 memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
871 DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
872 ntohs ( tftp->peer.st_port ) );
873 } else if ( memcmp ( &tftp->peer, st_src,
874 sizeof ( tftp->peer ) ) != 0 ) {
875 DBGC ( tftp, "TFTP %p received packet from wrong source (got "
876 "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
877 ntohs ( tftp->peer.st_port ) );
881 switch ( common->opcode ) {
882 case htons ( TFTP_OACK ):
883 rc = tftp_rx_oack ( tftp, iobuf->data, len );
885 case htons ( TFTP_DATA ):
886 rc = tftp_rx_data ( tftp, iob_disown ( iobuf ) );
888 case htons ( TFTP_ERROR ):
889 rc = tftp_rx_error ( tftp, iobuf->data, len );
892 DBGC ( tftp, "TFTP %p received strange packet type %d\n",
893 tftp, ntohs ( common->opcode ) );
903 * Receive new data via socket
905 * @v socket Transport layer interface
906 * @v iobuf I/O buffer
907 * @v meta Transfer metadata
908 * @ret rc Return status code
910 static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
911 struct io_buffer *iobuf,
912 struct xfer_metadata *meta ) {
913 struct tftp_request *tftp =
914 container_of ( socket, struct tftp_request, socket );
916 /* Enable sending ACKs when we receive a unicast packet. This
917 * covers three cases:
919 * 1. Standard TFTP; we should always send ACKs, and will
920 * always receive a unicast packet before we need to send the
923 * 2. RFC2090 multicast TFTP; the only unicast packets we will
924 * receive are the OACKs; enable sending ACKs here (before
925 * processing the OACK) and disable it when processing the
926 * multicast option if we are not the master client.
928 * 3. MTFTP; receiving a unicast datagram indicates that we
929 * are the "master client" and should send ACKs.
931 tftp->flags |= TFTP_FL_SEND_ACK;
933 return tftp_rx ( tftp, iobuf, meta );
936 /** TFTP socket operations */
937 static struct xfer_interface_operations tftp_socket_operations = {
938 .close = ignore_xfer_close,
939 .vredirect = xfer_vreopen,
940 .window = unlimited_xfer_window,
941 .alloc_iob = default_xfer_alloc_iob,
942 .deliver_iob = tftp_socket_deliver_iob,
943 .deliver_raw = xfer_deliver_as_iob,
947 * Receive new data via multicast socket
949 * @v mc_socket Multicast transport layer interface
950 * @v iobuf I/O buffer
951 * @v meta Transfer metadata
952 * @ret rc Return status code
954 static int tftp_mc_socket_deliver_iob ( struct xfer_interface *mc_socket,
955 struct io_buffer *iobuf,
956 struct xfer_metadata *meta ) {
957 struct tftp_request *tftp =
958 container_of ( mc_socket, struct tftp_request, mc_socket );
960 return tftp_rx ( tftp, iobuf, meta );
963 /** TFTP multicast socket operations */
964 static struct xfer_interface_operations tftp_mc_socket_operations = {
965 .close = ignore_xfer_close,
966 .vredirect = xfer_vreopen,
967 .window = unlimited_xfer_window,
968 .alloc_iob = default_xfer_alloc_iob,
969 .deliver_iob = tftp_mc_socket_deliver_iob,
970 .deliver_raw = xfer_deliver_as_iob,
974 * Close TFTP data transfer interface
976 * @v xfer Data transfer interface
977 * @v rc Reason for close
979 static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
980 struct tftp_request *tftp =
981 container_of ( xfer, struct tftp_request, xfer );
983 DBGC ( tftp, "TFTP %p interface closed: %s\n",
984 tftp, strerror ( rc ) );
986 tftp_done ( tftp, rc );
990 * Check flow control window
992 * @v xfer Data transfer interface
993 * @ret len Length of window
995 static size_t tftp_xfer_window ( struct xfer_interface *xfer ) {
996 struct tftp_request *tftp =
997 container_of ( xfer, struct tftp_request, xfer );
999 /* We abuse this data-xfer method to convey the blocksize to
1000 * the caller. This really should be done using some kind of
1001 * stat() method, but we don't yet have the facility to do
1004 return tftp->blksize;
1007 /** TFTP data transfer interface operations */
1008 static struct xfer_interface_operations tftp_xfer_operations = {
1009 .close = tftp_xfer_close,
1010 .vredirect = ignore_xfer_vredirect,
1011 .window = tftp_xfer_window,
1012 .alloc_iob = default_xfer_alloc_iob,
1013 .deliver_iob = xfer_deliver_as_raw,
1014 .deliver_raw = ignore_xfer_deliver_raw,
1018 * Initiate TFTP/TFTM/MTFTP download
1020 * @v xfer Data transfer interface
1021 * @v uri Uniform Resource Identifier
1022 * @ret rc Return status code
1024 static int tftp_core_open ( struct xfer_interface *xfer, struct uri *uri,
1025 unsigned int default_port,
1026 struct sockaddr *multicast,
1027 unsigned int flags ) {
1028 struct tftp_request *tftp;
1037 /* Allocate and populate TFTP structure */
1038 tftp = zalloc ( sizeof ( *tftp ) );
1041 tftp->refcnt.free = tftp_free;
1042 xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
1043 tftp->uri = uri_get ( uri );
1044 xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
1045 xfer_init ( &tftp->mc_socket, &tftp_mc_socket_operations,
1047 tftp->blksize = TFTP_DEFAULT_BLKSIZE;
1048 tftp->flags = flags;
1049 tftp->timer.expired = tftp_timer_expired;
1052 tftp->port = uri_port ( tftp->uri, default_port );
1053 if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
1056 /* Open multicast socket */
1058 if ( ( rc = tftp_reopen_mc ( tftp, multicast ) ) != 0 )
1062 /* Start timer to initiate RRQ */
1063 start_timer_nodelay ( &tftp->timer );
1065 /* Attach to parent interface, mortalise self, and return */
1066 xfer_plug_plug ( &tftp->xfer, xfer );
1067 ref_put ( &tftp->refcnt );
1071 DBGC ( tftp, "TFTP %p could not create request: %s\n",
1072 tftp, strerror ( rc ) );
1073 tftp_done ( tftp, rc );
1074 ref_put ( &tftp->refcnt );
1079 * Initiate TFTP download
1081 * @v xfer Data transfer interface
1082 * @v uri Uniform Resource Identifier
1083 * @ret rc Return status code
1085 static int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
1086 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1087 TFTP_FL_RRQ_SIZES );
1091 /** TFTP URI opener */
1092 struct uri_opener tftp_uri_opener __uri_opener = {
1098 * Initiate TFTM download
1100 * @v xfer Data transfer interface
1101 * @v uri Uniform Resource Identifier
1102 * @ret rc Return status code
1104 static int tftm_open ( struct xfer_interface *xfer, struct uri *uri ) {
1105 return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1106 ( TFTP_FL_RRQ_SIZES |
1107 TFTP_FL_RRQ_MULTICAST ) );
1111 /** TFTM URI opener */
1112 struct uri_opener tftm_uri_opener __uri_opener = {
1118 * Initiate MTFTP download
1120 * @v xfer Data transfer interface
1121 * @v uri Uniform Resource Identifier
1122 * @ret rc Return status code
1124 static int mtftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
1125 return tftp_core_open ( xfer, uri, MTFTP_PORT,
1126 ( struct sockaddr * ) &tftp_mtftp_socket,
1127 TFTP_FL_MTFTP_RECOVERY );
1130 /** MTFTP URI opener */
1131 struct uri_opener mtftp_uri_opener __uri_opener = {
1136 /******************************************************************************
1140 ******************************************************************************
1143 /** TFTP server setting */
1144 struct setting next_server_setting __setting = {
1145 .name = "next-server",
1146 .description = "TFTP server",
1147 .tag = DHCP_EB_SIADDR,
1148 .type = &setting_type_ipv4,
1152 * Apply TFTP configuration settings
1154 * @ret rc Return status code
1156 static int tftp_apply_settings ( void ) {
1157 static struct in_addr tftp_server = { 0 };
1158 struct in_addr last_tftp_server;
1159 char uri_string[32];
1162 /* Retrieve TFTP server setting */
1163 last_tftp_server = tftp_server;
1164 fetch_ipv4_setting ( NULL, &next_server_setting, &tftp_server );
1166 /* If TFTP server setting has changed, set the current working
1167 * URI to match. Do it only when the TFTP server has changed
1168 * to try to minimise surprises to the user, who probably
1169 * won't expect the CWURI to change just because they updated
1170 * an unrelated setting and triggered all the settings
1173 if ( tftp_server.s_addr != last_tftp_server.s_addr ) {
1174 snprintf ( uri_string, sizeof ( uri_string ),
1175 "tftp://%s/", inet_ntoa ( tftp_server ) );
1176 uri = parse_uri ( uri_string );
1186 /** TFTP settings applicator */
1187 struct settings_applicator tftp_settings_applicator __settings_applicator = {
1188 .apply = tftp_apply_settings,