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/tftp.h>
42 FEATURE ( FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1 );
47 * This data structure holds the state for an ongoing TFTP transfer.
50 /** Reference count */
52 /** Data transfer interface */
53 struct xfer_interface xfer;
55 /** URI being fetched */
57 /** Transport layer interface */
58 struct xfer_interface socket;
62 * This is the "blksize" option negotiated with the TFTP
63 * server. (If the TFTP server does not support TFTP options,
64 * this will default to 512).
69 * This is the value returned in the "tsize" option from the
70 * TFTP server. If the TFTP server does not support the
71 * "tsize" option, this value will be zero.
77 * This is the block number to be used in the next ACK sent
78 * back to the server, i.e. the number of the last received
79 * data block. The value zero indicates that the last
80 * received block was an OACK (i.e. that the next ACK will
81 * contain a block number of zero), and any value less than
82 * zero indicates that the connection has not yet been opened
83 * (i.e. that no blocks have yet been received).
88 * The peer address is determined by the first response
89 * received to the TFTP RRQ.
91 struct sockaddr_tcpip peer;
92 /** Retransmission timer */
93 struct retry_timer timer;
99 * @v refcnt Reference counter
101 static void tftp_free ( struct refcnt *refcnt ) {
102 struct tftp_request *tftp =
103 container_of ( refcnt, struct tftp_request, refcnt );
105 uri_put ( tftp->uri );
110 * Mark TFTP request as complete
112 * @v tftp TFTP connection
113 * @v rc Return status code
115 static void tftp_done ( struct tftp_request *tftp, int rc ) {
117 DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
118 tftp, rc, strerror ( rc ) );
120 /* Stop the retry timer */
121 stop_timer ( &tftp->timer );
123 /* Close all data transfer interfaces */
124 xfer_nullify ( &tftp->socket );
125 xfer_close ( &tftp->socket, rc );
126 xfer_nullify ( &tftp->xfer );
127 xfer_close ( &tftp->xfer, rc );
131 * TFTP requested blocksize
133 * This is treated as a global configuration parameter.
135 static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
138 * Set TFTP request blocksize
140 * @v blksize Requested block size
142 void tftp_set_request_blksize ( unsigned int blksize ) {
143 if ( blksize < TFTP_DEFAULT_BLKSIZE )
144 blksize = TFTP_DEFAULT_BLKSIZE;
145 tftp_request_blksize = blksize;
151 * @v tftp TFTP connection
152 * @ret rc Return status code
154 static int tftp_send_rrq ( struct tftp_request *tftp ) {
155 struct tftp_rrq *rrq;
156 const char *path = tftp->uri->path;
157 size_t len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
158 + 5 + 1 /* "octet" + NUL */
159 + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
160 + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */ );
161 struct io_buffer *iobuf;
163 DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
165 /* Allocate buffer */
166 iobuf = xfer_alloc_iob ( &tftp->socket, len );
171 rrq = iob_put ( iobuf, sizeof ( *rrq ) );
172 rrq->opcode = htons ( TFTP_RRQ );
174 snprintf ( rrq->data, iob_tailroom ( iobuf ),
175 "%s%coctet%cblksize%c%d%ctsize%c0", path, 0,
176 0, 0, tftp_request_blksize, 0, 0 ) + 1 );
178 /* RRQ always goes to the address specified in the initial
181 return xfer_deliver_iob ( &tftp->socket, iobuf );
187 * @v tftp TFTP connection
188 * @ret rc Return status code
190 static int tftp_send_ack ( struct tftp_request *tftp ) {
191 struct tftp_ack *ack;
192 struct io_buffer *iobuf;
193 struct xfer_metadata meta = {
194 .dest = ( struct sockaddr * ) &tftp->peer,
197 DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n",
200 /* Allocate buffer */
201 iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
206 ack = iob_put ( iobuf, sizeof ( *ack ) );
207 ack->opcode = htons ( TFTP_ACK );
208 ack->block = htons ( tftp->state );
210 /* ACK always goes to the peer recorded from the RRQ response */
211 return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
217 * @v tftp TFTP connection
218 * @ret rc Return status code
220 static int tftp_send_packet ( struct tftp_request *tftp ) {
222 /* Start retransmission timer */
223 start_timer ( &tftp->timer );
225 /* Send RRQ or ACK as appropriate */
226 if ( tftp->state < 0 ) {
227 return tftp_send_rrq ( tftp );
229 return tftp_send_ack ( tftp );
234 * Handle TFTP retransmission timer expiry
236 * @v timer Retry timer
237 * @v fail Failure indicator
239 static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
240 struct tftp_request *tftp =
241 container_of ( timer, struct tftp_request, timer );
244 tftp_done ( tftp, -ETIMEDOUT );
246 tftp_send_packet ( tftp );
251 * Mark TFTP block as received
253 * @v tftp TFTP connection
254 * @v block Block number
256 static void tftp_received ( struct tftp_request *tftp, unsigned int block ) {
258 /* Stop the retry timer */
259 stop_timer ( &tftp->timer );
261 /* Update state to indicate which block we're now waiting for */
264 /* Send next packet */
265 tftp_send_packet ( tftp );
269 * Process TFTP "blksize" option
271 * @v tftp TFTP connection
272 * @v value Option value
273 * @ret rc Return status code
275 static int tftp_process_blksize ( struct tftp_request *tftp,
276 const char *value ) {
279 tftp->blksize = strtoul ( value, &end, 10 );
281 DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
285 DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
291 * Process TFTP "tsize" option
293 * @v tftp TFTP connection
294 * @v value Option value
295 * @ret rc Return status code
297 static int tftp_process_tsize ( struct tftp_request *tftp,
298 const char *value ) {
301 tftp->tsize = strtoul ( value, &end, 10 );
303 DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
307 DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
309 /* Notify recipient of file size */
310 xfer_seek ( &tftp->xfer, tftp->tsize, SEEK_SET );
311 xfer_seek ( &tftp->xfer, 0, SEEK_SET );
322 * @v tftp TFTP connection
323 * @v value Option value
324 * @ret rc Return status code
326 int ( * process ) ( struct tftp_request *tftp, const char *value );
329 /** Recognised TFTP options */
330 static struct tftp_option tftp_options[] = {
331 { "blksize", tftp_process_blksize },
332 { "tsize", tftp_process_tsize },
337 * Process TFTP option
339 * @v tftp TFTP connection
340 * @v name Option name
341 * @v value Option value
342 * @ret rc Return status code
344 static int tftp_process_option ( struct tftp_request *tftp,
345 const char *name, const char *value ) {
346 struct tftp_option *option;
348 for ( option = tftp_options ; option->name ; option++ ) {
349 if ( strcasecmp ( name, option->name ) == 0 )
350 return option->process ( tftp, value );
353 DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
362 * @v tftp TFTP connection
363 * @v buf Temporary data buffer
364 * @v len Length of temporary data buffer
365 * @ret rc Return status code
367 static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
368 struct tftp_oack *oack = buf;
369 char *end = buf + len;
375 if ( len < sizeof ( *oack ) ) {
376 DBGC ( tftp, "TFTP %p received underlength OACK packet "
377 "length %d\n", tftp, len );
380 if ( end[-1] != '\0' ) {
381 DBGC ( tftp, "TFTP %p received OACK missing final NUL\n",
386 /* Process each option in turn */
388 while ( name < end ) {
389 value = ( name + strlen ( name ) + 1 );
390 if ( value == end ) {
391 DBGC ( tftp, "TFTP %p received OACK missing value "
392 "for option \"%s\"\n", tftp, name );
395 if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
397 name = ( value + strlen ( value ) + 1 );
400 /* Mark as received block 0 (the OACK) */
401 tftp_received ( tftp, 0 );
409 * @v tftp TFTP connection
410 * @v iobuf I/O buffer
411 * @ret rc Return status code
413 * Takes ownership of I/O buffer.
415 static int tftp_rx_data ( struct tftp_request *tftp,
416 struct io_buffer *iobuf ) {
417 struct tftp_data *data = iobuf->data;
423 if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
424 DBGC ( tftp, "TFTP %p received underlength DATA packet "
425 "length %d\n", tftp, iob_len ( iobuf ) );
431 block = ntohs ( data->block );
432 iob_pull ( iobuf, sizeof ( *data ) );
433 data_len = iob_len ( iobuf );
436 if ( ( rc = xfer_deliver_iob ( &tftp->xfer, iobuf ) ) != 0 ) {
437 DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
438 tftp, strerror ( rc ) );
439 tftp_done ( tftp, rc );
443 /* Mark block as received */
444 tftp_received ( tftp, block );
446 /* Finish when final block received */
447 if ( data_len < tftp->blksize )
448 tftp_done ( tftp, 0 );
453 /** Translation between TFTP errors and internal error numbers */
454 static const uint8_t tftp_errors[] = {
455 [TFTP_ERR_FILE_NOT_FOUND] = PXENV_STATUS_TFTP_FILE_NOT_FOUND,
456 [TFTP_ERR_ACCESS_DENIED] = PXENV_STATUS_TFTP_ACCESS_VIOLATION,
457 [TFTP_ERR_ILLEGAL_OP] = PXENV_STATUS_TFTP_UNKNOWN_OPCODE,
463 * @v tftp TFTP connection
464 * @v buf Temporary data buffer
465 * @v len Length of temporary data buffer
466 * @ret rc Return status code
468 static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
469 struct tftp_error *error = buf;
474 if ( len < sizeof ( *error ) ) {
475 DBGC ( tftp, "TFTP %p received underlength ERROR packet "
476 "length %d\n", tftp, len );
480 DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
481 "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
483 /* Determine final operation result */
484 err = ntohs ( error->errcode );
485 if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
486 rc = -tftp_errors[err];
488 rc = -PXENV_STATUS_TFTP_CANNOT_OPEN_CONNECTION;
490 /* Close TFTP request */
491 tftp_done ( tftp, rc );
499 * @v udp UDP connection
500 * @v data Received data
501 * @v len Length of received data
502 * @v st_src Partially-filled source address
503 * @v st_dest Partially-filled destination address
505 static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
506 struct io_buffer *iobuf,
507 struct xfer_metadata *meta ) {
508 struct tftp_request *tftp =
509 container_of ( socket, struct tftp_request, socket );
510 struct sockaddr_tcpip *st_src;
511 struct tftp_common *common = iobuf->data;
512 size_t len = iob_len ( iobuf );
516 if ( len < sizeof ( *common ) ) {
517 DBGC ( tftp, "TFTP %p received underlength packet length %d\n",
522 DBGC ( tftp, "TFTP %p received packet without metadata\n",
527 DBGC ( tftp, "TFTP %p received packet without source port\n",
532 /* Filter by TID. Set TID on first response received */
533 st_src = ( struct sockaddr_tcpip * ) meta->src;
534 if ( tftp->state < 0 ) {
535 memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
536 DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
537 ntohs ( tftp->peer.st_port ) );
538 } else if ( memcmp ( &tftp->peer, st_src,
539 sizeof ( tftp->peer ) ) != 0 ) {
540 DBGC ( tftp, "TFTP %p received packet from wrong source (got "
541 "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
542 ntohs ( tftp->peer.st_port ) );
546 switch ( common->opcode ) {
547 case htons ( TFTP_OACK ):
548 rc = tftp_rx_oack ( tftp, iobuf->data, len );
550 case htons ( TFTP_DATA ):
551 rc = tftp_rx_data ( tftp, iobuf );
554 case htons ( TFTP_ERROR ):
555 rc = tftp_rx_error ( tftp, iobuf->data, len );
558 DBGC ( tftp, "TFTP %p received strange packet type %d\n",
559 tftp, ntohs ( common->opcode ) );
569 * TFTP connection closed by network stack
571 * @v socket Transport layer interface
572 * @v rc Reason for close
574 static void tftp_socket_close ( struct xfer_interface *socket, int rc ) {
575 struct tftp_request *tftp =
576 container_of ( socket, struct tftp_request, socket );
578 DBGC ( tftp, "TFTP %p socket closed: %s\n",
579 tftp, strerror ( rc ) );
581 tftp_done ( tftp, rc );
584 /** TFTP socket operations */
585 static struct xfer_interface_operations tftp_socket_operations = {
586 .close = tftp_socket_close,
587 .vredirect = xfer_vopen,
588 .seek = ignore_xfer_seek,
589 .window = unlimited_xfer_window,
590 .alloc_iob = default_xfer_alloc_iob,
591 .deliver_iob = tftp_socket_deliver_iob,
592 .deliver_raw = xfer_deliver_as_iob,
596 * Close TFTP data transfer interface
598 * @v xfer Data transfer interface
599 * @v rc Reason for close
601 static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
602 struct tftp_request *tftp =
603 container_of ( xfer, struct tftp_request, xfer );
605 DBGC ( tftp, "TFTP %p interface closed: %s\n",
606 tftp, strerror ( rc ) );
608 tftp_done ( tftp, rc );
611 /** TFTP data transfer interface operations */
612 static struct xfer_interface_operations tftp_xfer_operations = {
613 .close = tftp_xfer_close,
614 .vredirect = ignore_xfer_vredirect,
615 .seek = ignore_xfer_seek,
616 .window = unlimited_xfer_window,
617 .alloc_iob = default_xfer_alloc_iob,
618 .deliver_iob = xfer_deliver_as_raw,
619 .deliver_raw = ignore_xfer_deliver_raw,
623 * Initiate TFTP download
625 * @v xfer Data transfer interface
626 * @v uri Uniform Resource Identifier
627 * @ret rc Return status code
629 int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
630 struct tftp_request *tftp;
631 struct sockaddr_tcpip server;
640 /* Allocate and populate TFTP structure */
641 tftp = zalloc ( sizeof ( *tftp ) );
644 tftp->refcnt.free = tftp_free;
645 xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
646 tftp->uri = uri_get ( uri );
647 xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
649 tftp->timer.expired = tftp_timer_expired;
652 memset ( &server, 0, sizeof ( server ) );
653 server.st_port = htons ( uri_port ( tftp->uri, TFTP_PORT ) );
654 if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
655 ( struct sockaddr * ) &server,
656 uri->host, NULL ) ) != 0 )
659 /* Start timer to initiate RRQ */
660 start_timer_nodelay ( &tftp->timer );
662 /* Attach to parent interface, mortalise self, and return */
663 xfer_plug_plug ( &tftp->xfer, xfer );
664 ref_put ( &tftp->refcnt );
668 DBGC ( tftp, "TFTP %p could not create request: %s\n",
669 tftp, strerror ( rc ) );
670 tftp_done ( tftp, rc );
671 ref_put ( &tftp->refcnt );
675 /** TFTP URI opener */
676 struct uri_opener tftp_uri_opener __uri_opener = {