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,
136 /** Only get filesize and then abort the transfer */
137 TFTP_FL_SIZEONLY
= 0x0010,
140 /** Maximum number of MTFTP open requests before falling back to TFTP */
141 #define MTFTP_MAX_TIMEOUTS 3
146 * @v refcnt Reference counter
148 static void tftp_free ( struct refcnt
*refcnt
) {
149 struct tftp_request
*tftp
=
150 container_of ( refcnt
, struct tftp_request
, refcnt
);
152 uri_put ( tftp
->uri
);
153 bitmap_free ( &tftp
->bitmap
);
158 * Mark TFTP request as complete
160 * @v tftp TFTP connection
161 * @v rc Return status code
163 static void tftp_done ( struct tftp_request
*tftp
, int rc
) {
165 DBGC ( tftp
, "TFTP %p finished with status %d (%s)\n",
166 tftp
, rc
, strerror ( rc
) );
168 /* Stop the retry timer */
169 stop_timer ( &tftp
->timer
);
171 /* Close all data transfer interfaces */
172 xfer_nullify ( &tftp
->socket
);
173 xfer_close ( &tftp
->socket
, rc
);
174 xfer_nullify ( &tftp
->mc_socket
);
175 xfer_close ( &tftp
->mc_socket
, rc
);
176 xfer_nullify ( &tftp
->xfer
);
177 xfer_close ( &tftp
->xfer
, rc
);
183 * @v tftp TFTP connection
184 * @ret rc Return status code
186 static int tftp_reopen ( struct tftp_request
*tftp
) {
187 struct sockaddr_tcpip server
;
191 xfer_close ( &tftp
->socket
, 0 );
193 /* Disable ACK sending. */
194 tftp
->flags
&= ~TFTP_FL_SEND_ACK
;
196 /* Reset peer address */
197 memset ( &tftp
->peer
, 0, sizeof ( tftp
->peer
) );
200 memset ( &server
, 0, sizeof ( server
) );
201 server
.st_port
= htons ( tftp
->port
);
202 if ( ( rc
= xfer_open_named_socket ( &tftp
->socket
, SOCK_DGRAM
,
203 ( struct sockaddr
* ) &server
,
204 tftp
->uri
->host
, NULL
) ) != 0 ) {
205 DBGC ( tftp
, "TFTP %p could not open socket: %s\n",
206 tftp
, strerror ( rc
) );
214 * Reopen TFTP multicast socket
216 * @v tftp TFTP connection
217 * @v local Local socket address
218 * @ret rc Return status code
220 static int tftp_reopen_mc ( struct tftp_request
*tftp
,
221 struct sockaddr
*local
) {
224 /* Close multicast socket */
225 xfer_close ( &tftp
->mc_socket
, 0 );
227 /* Open multicast socket. We never send via this socket, so
228 * use the local address as the peer address (since the peer
229 * address cannot be NULL).
231 if ( ( rc
= xfer_open_socket ( &tftp
->mc_socket
, SOCK_DGRAM
,
232 local
, local
) ) != 0 ) {
233 DBGC ( tftp
, "TFTP %p could not open multicast "
234 "socket: %s\n", tftp
, strerror ( rc
) );
242 * Presize TFTP receive buffers and block bitmap
244 * @v tftp TFTP connection
245 * @v filesize Known minimum file size
246 * @ret rc Return status code
248 static int tftp_presize ( struct tftp_request
*tftp
, size_t filesize
) {
249 unsigned int num_blocks
;
252 /* Do nothing if we are already large enough */
253 if ( filesize
<= tftp
->filesize
)
256 /* Record filesize */
257 tftp
->filesize
= filesize
;
259 /* Notify recipient of file size */
260 xfer_seek ( &tftp
->xfer
, filesize
, SEEK_SET
);
261 xfer_seek ( &tftp
->xfer
, 0, SEEK_SET
);
263 /* Calculate expected number of blocks. Note that files whose
264 * length is an exact multiple of the blocksize will have a
265 * trailing zero-length block, which must be included.
267 num_blocks
= ( ( filesize
/ tftp
->blksize
) + 1 );
268 if ( ( rc
= bitmap_resize ( &tftp
->bitmap
, num_blocks
) ) != 0 ) {
269 DBGC ( tftp
, "TFTP %p could not resize bitmap to %d blocks: "
270 "%s\n", tftp
, num_blocks
, strerror ( rc
) );
278 * TFTP requested blocksize
280 * This is treated as a global configuration parameter.
282 static unsigned int tftp_request_blksize
= TFTP_MAX_BLKSIZE
;
285 * Set TFTP request blocksize
287 * @v blksize Requested block size
289 void tftp_set_request_blksize ( unsigned int blksize
) {
290 if ( blksize
< TFTP_DEFAULT_BLKSIZE
)
291 blksize
= TFTP_DEFAULT_BLKSIZE
;
292 tftp_request_blksize
= blksize
;
296 * MTFTP multicast receive address
298 * This is treated as a global configuration parameter.
300 static struct sockaddr_in tftp_mtftp_socket
= {
301 .sin_family
= AF_INET
,
302 .sin_addr
.s_addr
= htonl ( 0xefff0101 ),
303 .sin_port
= htons ( 3001 ),
307 * Set MTFTP multicast address
309 * @v address Multicast IPv4 address
311 void tftp_set_mtftp_address ( struct in_addr address
) {
312 tftp_mtftp_socket
.sin_addr
= address
;
316 * Set MTFTP multicast port
318 * @v port Multicast port
320 void tftp_set_mtftp_port ( unsigned int port
) {
321 tftp_mtftp_socket
.sin_port
= htons ( port
);
327 * @v tftp TFTP connection
328 * @ret rc Return status code
330 static int tftp_send_rrq ( struct tftp_request
*tftp
) {
331 struct tftp_rrq
*rrq
;
334 struct io_buffer
*iobuf
;
336 /* Strip initial '/' if present. If we were opened via the
337 * URI interface, then there will be an initial '/', since a
338 * full tftp:// URI provides no way to specify a non-absolute
339 * path. However, many TFTP servers (particularly Windows
340 * TFTP servers) complain about having an initial '/', and it
341 * violates user expectations to have a '/' silently added to
342 * the DHCP-specified filename.
344 path
= tftp
->uri
->path
;
348 DBGC ( tftp
, "TFTP %p requesting \"%s\"\n", tftp
, path
);
350 /* Allocate buffer */
351 len
= ( sizeof ( *rrq
) + strlen ( path
) + 1 /* NUL */
352 + 5 + 1 /* "octet" + NUL */
353 + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
354 + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
355 + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
356 iobuf
= xfer_alloc_iob ( &tftp
->socket
, len
);
361 rrq
= iob_put ( iobuf
, sizeof ( *rrq
) );
362 rrq
->opcode
= htons ( TFTP_RRQ
);
363 iob_put ( iobuf
, snprintf ( iobuf
->tail
, iob_tailroom ( iobuf
),
364 "%s%coctet", path
, 0 ) + 1 );
365 if ( tftp
->flags
& TFTP_FL_RRQ_SIZES
) {
366 iob_put ( iobuf
, snprintf ( iobuf
->tail
,
367 iob_tailroom ( iobuf
),
368 "blksize%c%d%ctsize%c0", 0,
369 tftp_request_blksize
, 0, 0 ) + 1 );
371 if ( tftp
->flags
& TFTP_FL_RRQ_MULTICAST
) {
372 iob_put ( iobuf
, snprintf ( iobuf
->tail
,
373 iob_tailroom ( iobuf
),
374 "multicast%c", 0 ) + 1 );
377 /* RRQ always goes to the address specified in the initial
380 return xfer_deliver_iob ( &tftp
->socket
, iobuf
);
386 * @v tftp TFTP connection
387 * @ret rc Return status code
389 static int tftp_send_ack ( struct tftp_request
*tftp
) {
390 struct tftp_ack
*ack
;
391 struct io_buffer
*iobuf
;
392 struct xfer_metadata meta
= {
393 .dest
= ( struct sockaddr
* ) &tftp
->peer
,
397 /* Determine next required block number */
398 block
= bitmap_first_gap ( &tftp
->bitmap
);
399 DBGC2 ( tftp
, "TFTP %p sending ACK for block %d\n", tftp
, block
);
401 /* Allocate buffer */
402 iobuf
= xfer_alloc_iob ( &tftp
->socket
, sizeof ( *ack
) );
407 ack
= iob_put ( iobuf
, sizeof ( *ack
) );
408 ack
->opcode
= htons ( TFTP_ACK
);
409 ack
->block
= htons ( block
);
411 /* ACK always goes to the peer recorded from the RRQ response */
412 return xfer_deliver_iob_meta ( &tftp
->socket
, iobuf
, &meta
);
416 * Transmit ERROR (Abort)
418 * @v tftp TFTP connection
419 * @v errcode TFTP error code
420 * @v errmsg Error message string
421 * @ret rc Return status code
423 static int tftp_send_error ( struct tftp_request
*tftp
, int errcode
,
424 const char *errmsg
) {
425 struct tftp_error
*err
;
426 struct io_buffer
*iobuf
;
427 struct xfer_metadata meta
= {
428 .dest
= ( struct sockaddr
* ) &tftp
->peer
,
432 DBGC2 ( tftp
, "TFTP %p sending ERROR %d: %s\n", tftp
, errcode
,
435 /* Allocate buffer */
436 msglen
= sizeof ( *err
) + strlen ( errmsg
) + 1 /* NUL */;
437 iobuf
= xfer_alloc_iob ( &tftp
->socket
, msglen
);
442 err
= iob_put ( iobuf
, msglen
);
443 err
->opcode
= htons ( TFTP_ERROR
);
444 err
->errcode
= htons ( errcode
);
445 strcpy ( err
->errmsg
, errmsg
);
447 /* ERR always goes to the peer recorded from the RRQ response */
448 return xfer_deliver_iob_meta ( &tftp
->socket
, iobuf
, &meta
);
452 * Transmit next relevant packet
454 * @v tftp TFTP connection
455 * @ret rc Return status code
457 static int tftp_send_packet ( struct tftp_request
*tftp
) {
459 /* Update retransmission timer. While name resolution takes place the
460 * window is zero. Avoid unnecessary delay after name resolution
461 * completes by retrying immediately.
463 stop_timer ( &tftp
->timer
);
464 if ( xfer_window ( &tftp
->socket
) ) {
465 start_timer ( &tftp
->timer
);
467 start_timer_nodelay ( &tftp
->timer
);
470 /* Send RRQ or ACK as appropriate */
471 if ( ! tftp
->peer
.st_family
) {
472 return tftp_send_rrq ( tftp
);
474 if ( tftp
->flags
& TFTP_FL_SEND_ACK
) {
475 return tftp_send_ack ( tftp
);
483 * Handle TFTP retransmission timer expiry
485 * @v timer Retry timer
486 * @v fail Failure indicator
488 static void tftp_timer_expired ( struct retry_timer
*timer
, int fail
) {
489 struct tftp_request
*tftp
=
490 container_of ( timer
, struct tftp_request
, timer
);
493 /* If we are doing MTFTP, attempt the various recovery strategies */
494 if ( tftp
->flags
& TFTP_FL_MTFTP_RECOVERY
) {
495 if ( tftp
->peer
.st_family
) {
496 /* If we have received any response from the server,
497 * try resending the RRQ to restart the download.
499 DBGC ( tftp
, "TFTP %p attempting reopen\n", tftp
);
500 if ( ( rc
= tftp_reopen ( tftp
) ) != 0 )
503 /* Fall back to plain TFTP after several attempts */
504 tftp
->mtftp_timeouts
++;
505 DBGC ( tftp
, "TFTP %p timeout %d waiting for MTFTP "
506 "open\n", tftp
, tftp
->mtftp_timeouts
);
508 if ( tftp
->mtftp_timeouts
> MTFTP_MAX_TIMEOUTS
) {
509 DBGC ( tftp
, "TFTP %p falling back to plain "
511 tftp
->flags
= TFTP_FL_RRQ_SIZES
;
513 /* Close multicast socket */
514 xfer_close ( &tftp
->mc_socket
, 0 );
516 /* Reset retry timer */
517 start_timer_nodelay ( &tftp
->timer
);
519 /* The blocksize may change: discard
522 bitmap_free ( &tftp
->bitmap
);
523 memset ( &tftp
->bitmap
, 0,
524 sizeof ( tftp
->bitmap
) );
526 /* Reopen on standard TFTP port */
527 tftp
->port
= TFTP_PORT
;
528 if ( ( rc
= tftp_reopen ( tftp
) ) != 0 )
533 /* Not doing MTFTP (or have fallen back to plain
534 * TFTP); fail as per normal.
541 tftp_send_packet ( tftp
);
545 tftp_done ( tftp
, rc
);
549 * Process TFTP "blksize" option
551 * @v tftp TFTP connection
552 * @v value Option value
553 * @ret rc Return status code
555 static int tftp_process_blksize ( struct tftp_request
*tftp
,
556 const char *value
) {
559 tftp
->blksize
= strtoul ( value
, &end
, 10 );
561 DBGC ( tftp
, "TFTP %p got invalid blksize \"%s\"\n",
563 return -( EINVAL
| ETFTP_INVALID_BLKSIZE
);
565 DBGC ( tftp
, "TFTP %p blksize=%d\n", tftp
, tftp
->blksize
);
571 * Process TFTP "tsize" option
573 * @v tftp TFTP connection
574 * @v value Option value
575 * @ret rc Return status code
577 static int tftp_process_tsize ( struct tftp_request
*tftp
,
578 const char *value
) {
581 tftp
->tsize
= strtoul ( value
, &end
, 10 );
583 DBGC ( tftp
, "TFTP %p got invalid tsize \"%s\"\n",
585 return -( EINVAL
| ETFTP_INVALID_TSIZE
);
587 DBGC ( tftp
, "TFTP %p tsize=%ld\n", tftp
, tftp
->tsize
);
593 * Process TFTP "multicast" option
595 * @v tftp TFTP connection
596 * @v value Option value
597 * @ret rc Return status code
599 static int tftp_process_multicast ( struct tftp_request
*tftp
,
600 const char *value
) {
603 struct sockaddr_in sin
;
605 char buf
[ strlen ( value
) + 1 ];
613 /* Split value into "addr,port,mc" fields */
614 memcpy ( buf
, value
, sizeof ( buf
) );
616 port
= strchr ( addr
, ',' );
618 DBGC ( tftp
, "TFTP %p multicast missing port,mc\n", tftp
);
619 return -( EINVAL
| ETFTP_MC_NO_PORT
);
622 mc
= strchr ( port
, ',' );
624 DBGC ( tftp
, "TFTP %p multicast missing mc\n", tftp
);
625 return -( EINVAL
| ETFTP_MC_NO_MC
);
629 /* Parse parameters */
630 if ( strtoul ( mc
, &mc_end
, 0 ) == 0 )
631 tftp
->flags
&= ~TFTP_FL_SEND_ACK
;
633 DBGC ( tftp
, "TFTP %p multicast invalid mc %s\n", tftp
, mc
);
634 return -( EINVAL
| ETFTP_MC_INVALID_MC
);
636 DBGC ( tftp
, "TFTP %p is%s the master client\n",
637 tftp
, ( ( tftp
->flags
& TFTP_FL_SEND_ACK
) ? "" : " not" ) );
638 if ( *addr
&& *port
) {
639 socket
.sin
.sin_family
= AF_INET
;
640 if ( inet_aton ( addr
, &socket
.sin
.sin_addr
) == 0 ) {
641 DBGC ( tftp
, "TFTP %p multicast invalid IP address "
642 "%s\n", tftp
, addr
);
643 return -( EINVAL
| ETFTP_MC_INVALID_IP
);
645 DBGC ( tftp
, "TFTP %p multicast IP address %s\n",
646 tftp
, inet_ntoa ( socket
.sin
.sin_addr
) );
647 socket
.sin
.sin_port
= htons ( strtoul ( port
, &port_end
, 0 ) );
649 DBGC ( tftp
, "TFTP %p multicast invalid port %s\n",
651 return -( EINVAL
| ETFTP_MC_INVALID_PORT
);
653 DBGC ( tftp
, "TFTP %p multicast port %d\n",
654 tftp
, ntohs ( socket
.sin
.sin_port
) );
655 if ( ( rc
= tftp_reopen_mc ( tftp
, &socket
.sa
) ) != 0 )
668 * @v tftp TFTP connection
669 * @v value Option value
670 * @ret rc Return status code
672 int ( * process
) ( struct tftp_request
*tftp
, const char *value
);
675 /** Recognised TFTP options */
676 static struct tftp_option tftp_options
[] = {
677 { "blksize", tftp_process_blksize
},
678 { "tsize", tftp_process_tsize
},
679 { "multicast", tftp_process_multicast
},
684 * Process TFTP option
686 * @v tftp TFTP connection
687 * @v name Option name
688 * @v value Option value
689 * @ret rc Return status code
691 static int tftp_process_option ( struct tftp_request
*tftp
,
692 const char *name
, const char *value
) {
693 struct tftp_option
*option
;
695 for ( option
= tftp_options
; option
->name
; option
++ ) {
696 if ( strcasecmp ( name
, option
->name
) == 0 )
697 return option
->process ( tftp
, value
);
700 DBGC ( tftp
, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
703 /* Unknown options should be silently ignored */
710 * @v tftp TFTP connection
711 * @v buf Temporary data buffer
712 * @v len Length of temporary data buffer
713 * @ret rc Return status code
715 static int tftp_rx_oack ( struct tftp_request
*tftp
, void *buf
, size_t len
) {
716 struct tftp_oack
*oack
= buf
;
717 char *end
= buf
+ len
;
724 if ( len
< sizeof ( *oack
) ) {
725 DBGC ( tftp
, "TFTP %p received underlength OACK packet "
726 "length %zd\n", tftp
, len
);
731 /* Process each option in turn */
732 for ( name
= oack
->data
; name
< end
; name
= next
) {
734 /* Parse option name and value
736 * We treat parsing errors as non-fatal, because there
737 * exists at least one TFTP server (IBM Tivoli PXE
738 * Server 5.1.0.3) that has been observed to send
739 * malformed OACKs containing trailing garbage bytes.
741 value
= ( name
+ strnlen ( name
, ( end
- name
) ) + 1 );
743 DBGC ( tftp
, "TFTP %p received OACK with malformed "
744 "option name:\n", tftp
);
745 DBGC_HD ( tftp
, oack
, len
);
748 if ( value
== end
) {
749 DBGC ( tftp
, "TFTP %p received OACK missing value "
750 "for option \"%s\"\n", tftp
, name
);
751 DBGC_HD ( tftp
, oack
, len
);
754 next
= ( value
+ strnlen ( value
, ( end
- value
) ) + 1 );
756 DBGC ( tftp
, "TFTP %p received OACK with malformed "
757 "value for option \"%s\":\n", tftp
, name
);
758 DBGC_HD ( tftp
, oack
, len
);
763 if ( ( rc
= tftp_process_option ( tftp
, name
, value
) ) != 0 )
767 /* Process tsize information, if available */
769 if ( ( rc
= tftp_presize ( tftp
, tftp
->tsize
) ) != 0 )
773 /* Abort request if only trying to determine file size */
774 if ( tftp
->flags
& TFTP_FL_SIZEONLY
) {
776 tftp_send_error ( tftp
, 0, "TFTP Aborted" );
777 tftp_done ( tftp
, rc
);
781 /* Request next data block */
782 tftp_send_packet ( tftp
);
786 tftp_done ( tftp
, rc
);
793 * @v tftp TFTP connection
794 * @v iobuf I/O buffer
795 * @ret rc Return status code
797 * Takes ownership of I/O buffer.
799 static int tftp_rx_data ( struct tftp_request
*tftp
,
800 struct io_buffer
*iobuf
) {
801 struct tftp_data
*data
= iobuf
->data
;
802 struct xfer_metadata meta
;
808 if ( tftp
->flags
& TFTP_FL_SIZEONLY
) {
809 /* If we get here then server doesn't support SIZE option */
811 tftp_send_error ( tftp
, 0, "TFTP Aborted" );
816 if ( iob_len ( iobuf
) < sizeof ( *data
) ) {
817 DBGC ( tftp
, "TFTP %p received underlength DATA packet "
818 "length %zd\n", tftp
, iob_len ( iobuf
) );
823 /* Calculate block number */
824 block
= ( ( bitmap_first_gap ( &tftp
->bitmap
) + 1 ) & ~0xffff );
825 if ( data
->block
== 0 && block
== 0 ) {
826 DBGC ( tftp
, "TFTP %p received data block 0\n", tftp
);
830 block
+= ( ntohs ( data
->block
) - 1 );
833 offset
= ( block
* tftp
->blksize
);
834 iob_pull ( iobuf
, sizeof ( *data
) );
835 data_len
= iob_len ( iobuf
);
836 if ( data_len
> tftp
->blksize
) {
837 DBGC ( tftp
, "TFTP %p received overlength DATA packet "
838 "length %zd\n", tftp
, data_len
);
844 memset ( &meta
, 0, sizeof ( meta
) );
845 meta
.whence
= SEEK_SET
;
846 meta
.offset
= offset
;
847 if ( ( rc
= xfer_deliver_iob_meta ( &tftp
->xfer
, iob_disown ( iobuf
),
849 DBGC ( tftp
, "TFTP %p could not deliver data: %s\n",
850 tftp
, strerror ( rc
) );
854 /* Ensure block bitmap is ready */
855 if ( ( rc
= tftp_presize ( tftp
, ( offset
+ data_len
) ) ) != 0 )
858 /* Mark block as received */
859 bitmap_set ( &tftp
->bitmap
, block
);
861 /* Acknowledge block */
862 tftp_send_packet ( tftp
);
864 /* If all blocks have been received, finish. */
865 if ( bitmap_full ( &tftp
->bitmap
) )
866 tftp_done ( tftp
, 0 );
871 tftp_done ( tftp
, rc
);
875 /** Translation between TFTP errors and internal error numbers */
876 static const int tftp_errors
[] = {
877 [TFTP_ERR_FILE_NOT_FOUND
] = ENOENT
,
878 [TFTP_ERR_ACCESS_DENIED
] = EACCES
,
879 [TFTP_ERR_ILLEGAL_OP
] = ENOTSUP
,
885 * @v tftp TFTP connection
886 * @v buf Temporary data buffer
887 * @v len Length of temporary data buffer
888 * @ret rc Return status code
890 static int tftp_rx_error ( struct tftp_request
*tftp
, void *buf
, size_t len
) {
891 struct tftp_error
*error
= buf
;
896 if ( len
< sizeof ( *error
) ) {
897 DBGC ( tftp
, "TFTP %p received underlength ERROR packet "
898 "length %zd\n", tftp
, len
);
902 DBGC ( tftp
, "TFTP %p received ERROR packet with code %d, message "
903 "\"%s\"\n", tftp
, ntohs ( error
->errcode
), error
->errmsg
);
905 /* Determine final operation result */
906 err
= ntohs ( error
->errcode
);
907 if ( err
< ( sizeof ( tftp_errors
) / sizeof ( tftp_errors
[0] ) ) )
908 rc
= -tftp_errors
[err
];
912 /* Close TFTP request */
913 tftp_done ( tftp
, rc
);
921 * @v tftp TFTP connection
922 * @v iobuf I/O buffer
923 * @v meta Transfer metadata
924 * @ret rc Return status code
926 static int tftp_rx ( struct tftp_request
*tftp
,
927 struct io_buffer
*iobuf
,
928 struct xfer_metadata
*meta
) {
929 struct sockaddr_tcpip
*st_src
;
930 struct tftp_common
*common
= iobuf
->data
;
931 size_t len
= iob_len ( iobuf
);
935 if ( len
< sizeof ( *common
) ) {
936 DBGC ( tftp
, "TFTP %p received underlength packet length "
937 "%zd\n", tftp
, len
);
941 DBGC ( tftp
, "TFTP %p received packet without source port\n",
946 /* Filter by TID. Set TID on first response received */
947 st_src
= ( struct sockaddr_tcpip
* ) meta
->src
;
948 if ( ! tftp
->peer
.st_family
) {
949 memcpy ( &tftp
->peer
, st_src
, sizeof ( tftp
->peer
) );
950 DBGC ( tftp
, "TFTP %p using remote port %d\n", tftp
,
951 ntohs ( tftp
->peer
.st_port
) );
952 } else if ( memcmp ( &tftp
->peer
, st_src
,
953 sizeof ( tftp
->peer
) ) != 0 ) {
954 DBGC ( tftp
, "TFTP %p received packet from wrong source (got "
955 "%d, wanted %d)\n", tftp
, ntohs ( st_src
->st_port
),
956 ntohs ( tftp
->peer
.st_port
) );
960 switch ( common
->opcode
) {
961 case htons ( TFTP_OACK
):
962 rc
= tftp_rx_oack ( tftp
, iobuf
->data
, len
);
964 case htons ( TFTP_DATA
):
965 rc
= tftp_rx_data ( tftp
, iob_disown ( iobuf
) );
967 case htons ( TFTP_ERROR
):
968 rc
= tftp_rx_error ( tftp
, iobuf
->data
, len
);
971 DBGC ( tftp
, "TFTP %p received strange packet type %d\n",
972 tftp
, ntohs ( common
->opcode
) );
982 * Receive new data via socket
984 * @v socket Transport layer interface
985 * @v iobuf I/O buffer
986 * @v meta Transfer metadata
987 * @ret rc Return status code
989 static int tftp_socket_deliver_iob ( struct xfer_interface
*socket
,
990 struct io_buffer
*iobuf
,
991 struct xfer_metadata
*meta
) {
992 struct tftp_request
*tftp
=
993 container_of ( socket
, struct tftp_request
, socket
);
995 /* Enable sending ACKs when we receive a unicast packet. This
996 * covers three cases:
998 * 1. Standard TFTP; we should always send ACKs, and will
999 * always receive a unicast packet before we need to send the
1002 * 2. RFC2090 multicast TFTP; the only unicast packets we will
1003 * receive are the OACKs; enable sending ACKs here (before
1004 * processing the OACK) and disable it when processing the
1005 * multicast option if we are not the master client.
1007 * 3. MTFTP; receiving a unicast datagram indicates that we
1008 * are the "master client" and should send ACKs.
1010 tftp
->flags
|= TFTP_FL_SEND_ACK
;
1012 return tftp_rx ( tftp
, iobuf
, meta
);
1015 /** TFTP socket operations */
1016 static struct xfer_interface_operations tftp_socket_operations
= {
1017 .close
= ignore_xfer_close
,
1018 .vredirect
= xfer_vreopen
,
1019 .window
= unlimited_xfer_window
,
1020 .alloc_iob
= default_xfer_alloc_iob
,
1021 .deliver_iob
= tftp_socket_deliver_iob
,
1022 .deliver_raw
= xfer_deliver_as_iob
,
1026 * Receive new data via multicast socket
1028 * @v mc_socket Multicast transport layer interface
1029 * @v iobuf I/O buffer
1030 * @v meta Transfer metadata
1031 * @ret rc Return status code
1033 static int tftp_mc_socket_deliver_iob ( struct xfer_interface
*mc_socket
,
1034 struct io_buffer
*iobuf
,
1035 struct xfer_metadata
*meta
) {
1036 struct tftp_request
*tftp
=
1037 container_of ( mc_socket
, struct tftp_request
, mc_socket
);
1039 return tftp_rx ( tftp
, iobuf
, meta
);
1042 /** TFTP multicast socket operations */
1043 static struct xfer_interface_operations tftp_mc_socket_operations
= {
1044 .close
= ignore_xfer_close
,
1045 .vredirect
= xfer_vreopen
,
1046 .window
= unlimited_xfer_window
,
1047 .alloc_iob
= default_xfer_alloc_iob
,
1048 .deliver_iob
= tftp_mc_socket_deliver_iob
,
1049 .deliver_raw
= xfer_deliver_as_iob
,
1053 * Close TFTP data transfer interface
1055 * @v xfer Data transfer interface
1056 * @v rc Reason for close
1058 static void tftp_xfer_close ( struct xfer_interface
*xfer
, int rc
) {
1059 struct tftp_request
*tftp
=
1060 container_of ( xfer
, struct tftp_request
, xfer
);
1062 DBGC ( tftp
, "TFTP %p interface closed: %s\n",
1063 tftp
, strerror ( rc
) );
1065 tftp_done ( tftp
, rc
);
1069 * Check flow control window
1071 * @v xfer Data transfer interface
1072 * @ret len Length of window
1074 static size_t tftp_xfer_window ( struct xfer_interface
*xfer
) {
1075 struct tftp_request
*tftp
=
1076 container_of ( xfer
, struct tftp_request
, xfer
);
1078 /* We abuse this data-xfer method to convey the blocksize to
1079 * the caller. This really should be done using some kind of
1080 * stat() method, but we don't yet have the facility to do
1083 return tftp
->blksize
;
1086 /** TFTP data transfer interface operations */
1087 static struct xfer_interface_operations tftp_xfer_operations
= {
1088 .close
= tftp_xfer_close
,
1089 .vredirect
= ignore_xfer_vredirect
,
1090 .window
= tftp_xfer_window
,
1091 .alloc_iob
= default_xfer_alloc_iob
,
1092 .deliver_iob
= xfer_deliver_as_raw
,
1093 .deliver_raw
= ignore_xfer_deliver_raw
,
1097 * Initiate TFTP/TFTM/MTFTP download
1099 * @v xfer Data transfer interface
1100 * @v uri Uniform Resource Identifier
1101 * @ret rc Return status code
1103 static int tftp_core_open ( struct xfer_interface
*xfer
, struct uri
*uri
,
1104 unsigned int default_port
,
1105 struct sockaddr
*multicast
,
1106 unsigned int flags
) {
1107 struct tftp_request
*tftp
;
1116 /* Allocate and populate TFTP structure */
1117 tftp
= zalloc ( sizeof ( *tftp
) );
1120 tftp
->refcnt
.free
= tftp_free
;
1121 xfer_init ( &tftp
->xfer
, &tftp_xfer_operations
, &tftp
->refcnt
);
1122 tftp
->uri
= uri_get ( uri
);
1123 xfer_init ( &tftp
->socket
, &tftp_socket_operations
, &tftp
->refcnt
);
1124 xfer_init ( &tftp
->mc_socket
, &tftp_mc_socket_operations
,
1126 tftp
->blksize
= TFTP_DEFAULT_BLKSIZE
;
1127 tftp
->flags
= flags
;
1128 tftp
->timer
.expired
= tftp_timer_expired
;
1131 tftp
->port
= uri_port ( tftp
->uri
, default_port
);
1132 if ( ( rc
= tftp_reopen ( tftp
) ) != 0 )
1135 /* Open multicast socket */
1137 if ( ( rc
= tftp_reopen_mc ( tftp
, multicast
) ) != 0 )
1141 /* Start timer to initiate RRQ */
1142 start_timer_nodelay ( &tftp
->timer
);
1144 /* Attach to parent interface, mortalise self, and return */
1145 xfer_plug_plug ( &tftp
->xfer
, xfer
);
1146 ref_put ( &tftp
->refcnt
);
1150 DBGC ( tftp
, "TFTP %p could not create request: %s\n",
1151 tftp
, strerror ( rc
) );
1152 tftp_done ( tftp
, rc
);
1153 ref_put ( &tftp
->refcnt
);
1158 * Initiate TFTP download
1160 * @v xfer Data transfer interface
1161 * @v uri Uniform Resource Identifier
1162 * @ret rc Return status code
1164 static int tftp_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
1165 return tftp_core_open ( xfer
, uri
, TFTP_PORT
, NULL
,
1166 TFTP_FL_RRQ_SIZES
);
1170 /** TFTP URI opener */
1171 struct uri_opener tftp_uri_opener __uri_opener
= {
1177 * Initiate TFTP-size request
1179 * @v xfer Data transfer interface
1180 * @v uri Uniform Resource Identifier
1181 * @ret rc Return status code
1183 static int tftpsize_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
1184 return tftp_core_open ( xfer
, uri
, TFTP_PORT
, NULL
,
1185 ( TFTP_FL_RRQ_SIZES
|
1186 TFTP_FL_SIZEONLY
) );
1190 /** TFTP URI opener */
1191 struct uri_opener tftpsize_uri_opener __uri_opener
= {
1192 .scheme
= "tftpsize",
1193 .open
= tftpsize_open
,
1197 * Initiate TFTM download
1199 * @v xfer Data transfer interface
1200 * @v uri Uniform Resource Identifier
1201 * @ret rc Return status code
1203 static int tftm_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
1204 return tftp_core_open ( xfer
, uri
, TFTP_PORT
, NULL
,
1205 ( TFTP_FL_RRQ_SIZES
|
1206 TFTP_FL_RRQ_MULTICAST
) );
1210 /** TFTM URI opener */
1211 struct uri_opener tftm_uri_opener __uri_opener
= {
1217 * Initiate MTFTP download
1219 * @v xfer Data transfer interface
1220 * @v uri Uniform Resource Identifier
1221 * @ret rc Return status code
1223 static int mtftp_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
1224 return tftp_core_open ( xfer
, uri
, MTFTP_PORT
,
1225 ( struct sockaddr
* ) &tftp_mtftp_socket
,
1226 TFTP_FL_MTFTP_RECOVERY
);
1229 /** MTFTP URI opener */
1230 struct uri_opener mtftp_uri_opener __uri_opener
= {
1235 /******************************************************************************
1239 ******************************************************************************
1242 /** TFTP server setting */
1243 struct setting next_server_setting __setting
= {
1244 .name
= "next-server",
1245 .description
= "TFTP server",
1246 .tag
= DHCP_EB_SIADDR
,
1247 .type
= &setting_type_ipv4
,
1251 * Apply TFTP configuration settings
1253 * @ret rc Return status code
1255 static int tftp_apply_settings ( void ) {
1256 static struct in_addr tftp_server
= { 0 };
1257 struct in_addr last_tftp_server
;
1258 char uri_string
[32];
1261 /* Retrieve TFTP server setting */
1262 last_tftp_server
= tftp_server
;
1263 fetch_ipv4_setting ( NULL
, &next_server_setting
, &tftp_server
);
1265 /* If TFTP server setting has changed, set the current working
1266 * URI to match. Do it only when the TFTP server has changed
1267 * to try to minimise surprises to the user, who probably
1268 * won't expect the CWURI to change just because they updated
1269 * an unrelated setting and triggered all the settings
1272 if ( tftp_server
.s_addr
!= last_tftp_server
.s_addr
) {
1273 snprintf ( uri_string
, sizeof ( uri_string
),
1274 "tftp://%s/", inet_ntoa ( tftp_server
) );
1275 uri
= parse_uri ( uri_string
);
1285 /** TFTP settings applicator */
1286 struct settings_applicator tftp_settings_applicator __settings_applicator
= {
1287 .apply
= tftp_apply_settings
,