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
;
679 if ( len
< sizeof ( *oack
) ) {
680 DBGC ( tftp
, "TFTP %p received underlength OACK packet "
681 "length %zd\n", tftp
, len
);
686 /* Process each option in turn */
687 for ( name
= oack
->data
; name
< end
; name
= next
) {
689 /* Parse option name and value
691 * We treat parsing errors as non-fatal, because there
692 * exists at least one TFTP server (IBM Tivoli PXE
693 * Server 5.1.0.3) that has been observed to send
694 * malformed OACKs containing trailing garbage bytes.
696 value
= ( name
+ strnlen ( name
, ( end
- name
) ) + 1 );
698 DBGC ( tftp
, "TFTP %p received OACK with malformed "
699 "option name:\n", tftp
);
700 DBGC_HD ( tftp
, oack
, len
);
703 if ( value
== end
) {
704 DBGC ( tftp
, "TFTP %p received OACK missing value "
705 "for option \"%s\"\n", tftp
, name
);
706 DBGC_HD ( tftp
, oack
, len
);
709 next
= ( value
+ strnlen ( value
, ( end
- value
) ) + 1 );
711 DBGC ( tftp
, "TFTP %p received OACK with malformed "
712 "value for option \"%s\":\n", tftp
, name
);
713 DBGC_HD ( tftp
, oack
, len
);
718 if ( ( rc
= tftp_process_option ( tftp
, name
, value
) ) != 0 )
722 /* Process tsize information, if available */
724 if ( ( rc
= tftp_presize ( tftp
, tftp
->tsize
) ) != 0 )
728 /* Request next data block */
729 tftp_send_packet ( tftp
);
733 tftp_done ( tftp
, rc
);
740 * @v tftp TFTP connection
741 * @v iobuf I/O buffer
742 * @ret rc Return status code
744 * Takes ownership of I/O buffer.
746 static int tftp_rx_data ( struct tftp_request
*tftp
,
747 struct io_buffer
*iobuf
) {
748 struct tftp_data
*data
= iobuf
->data
;
749 struct xfer_metadata meta
;
756 if ( iob_len ( iobuf
) < sizeof ( *data
) ) {
757 DBGC ( tftp
, "TFTP %p received underlength DATA packet "
758 "length %zd\n", tftp
, iob_len ( iobuf
) );
762 if ( data
->block
== 0 ) {
763 DBGC ( tftp
, "TFTP %p received data block 0\n", tftp
);
769 block
= ( ntohs ( data
->block
) - 1 );
770 offset
= ( block
* tftp
->blksize
);
771 iob_pull ( iobuf
, sizeof ( *data
) );
772 data_len
= iob_len ( iobuf
);
773 if ( data_len
> tftp
->blksize
) {
774 DBGC ( tftp
, "TFTP %p received overlength DATA packet "
775 "length %zd\n", tftp
, data_len
);
781 memset ( &meta
, 0, sizeof ( meta
) );
782 meta
.whence
= SEEK_SET
;
783 meta
.offset
= offset
;
784 if ( ( rc
= xfer_deliver_iob_meta ( &tftp
->xfer
, iob_disown ( iobuf
),
786 DBGC ( tftp
, "TFTP %p could not deliver data: %s\n",
787 tftp
, strerror ( rc
) );
791 /* Ensure block bitmap is ready */
792 if ( ( rc
= tftp_presize ( tftp
, ( offset
+ data_len
) ) ) != 0 )
795 /* Mark block as received */
796 bitmap_set ( &tftp
->bitmap
, block
);
798 /* Acknowledge block */
799 tftp_send_packet ( tftp
);
801 /* If all blocks have been received, finish. */
802 if ( bitmap_full ( &tftp
->bitmap
) )
803 tftp_done ( tftp
, 0 );
808 tftp_done ( tftp
, rc
);
812 /** Translation between TFTP errors and internal error numbers */
813 static const int tftp_errors
[] = {
814 [TFTP_ERR_FILE_NOT_FOUND
] = ENOENT
,
815 [TFTP_ERR_ACCESS_DENIED
] = EACCES
,
816 [TFTP_ERR_ILLEGAL_OP
] = ENOTSUP
,
822 * @v tftp TFTP connection
823 * @v buf Temporary data buffer
824 * @v len Length of temporary data buffer
825 * @ret rc Return status code
827 static int tftp_rx_error ( struct tftp_request
*tftp
, void *buf
, size_t len
) {
828 struct tftp_error
*error
= buf
;
833 if ( len
< sizeof ( *error
) ) {
834 DBGC ( tftp
, "TFTP %p received underlength ERROR packet "
835 "length %zd\n", tftp
, len
);
839 DBGC ( tftp
, "TFTP %p received ERROR packet with code %d, message "
840 "\"%s\"\n", tftp
, ntohs ( error
->errcode
), error
->errmsg
);
842 /* Determine final operation result */
843 err
= ntohs ( error
->errcode
);
844 if ( err
< ( sizeof ( tftp_errors
) / sizeof ( tftp_errors
[0] ) ) )
845 rc
= -tftp_errors
[err
];
849 /* Close TFTP request */
850 tftp_done ( tftp
, rc
);
858 * @v tftp TFTP connection
859 * @v iobuf I/O buffer
860 * @v meta Transfer metadata
861 * @ret rc Return status code
863 static int tftp_rx ( struct tftp_request
*tftp
,
864 struct io_buffer
*iobuf
,
865 struct xfer_metadata
*meta
) {
866 struct sockaddr_tcpip
*st_src
;
867 struct tftp_common
*common
= iobuf
->data
;
868 size_t len
= iob_len ( iobuf
);
872 if ( len
< sizeof ( *common
) ) {
873 DBGC ( tftp
, "TFTP %p received underlength packet length "
874 "%zd\n", tftp
, len
);
878 DBGC ( tftp
, "TFTP %p received packet without source port\n",
883 /* Filter by TID. Set TID on first response received */
884 st_src
= ( struct sockaddr_tcpip
* ) meta
->src
;
885 if ( ! tftp
->peer
.st_family
) {
886 memcpy ( &tftp
->peer
, st_src
, sizeof ( tftp
->peer
) );
887 DBGC ( tftp
, "TFTP %p using remote port %d\n", tftp
,
888 ntohs ( tftp
->peer
.st_port
) );
889 } else if ( memcmp ( &tftp
->peer
, st_src
,
890 sizeof ( tftp
->peer
) ) != 0 ) {
891 DBGC ( tftp
, "TFTP %p received packet from wrong source (got "
892 "%d, wanted %d)\n", tftp
, ntohs ( st_src
->st_port
),
893 ntohs ( tftp
->peer
.st_port
) );
897 switch ( common
->opcode
) {
898 case htons ( TFTP_OACK
):
899 rc
= tftp_rx_oack ( tftp
, iobuf
->data
, len
);
901 case htons ( TFTP_DATA
):
902 rc
= tftp_rx_data ( tftp
, iob_disown ( iobuf
) );
904 case htons ( TFTP_ERROR
):
905 rc
= tftp_rx_error ( tftp
, iobuf
->data
, len
);
908 DBGC ( tftp
, "TFTP %p received strange packet type %d\n",
909 tftp
, ntohs ( common
->opcode
) );
919 * Receive new data via socket
921 * @v socket Transport layer interface
922 * @v iobuf I/O buffer
923 * @v meta Transfer metadata
924 * @ret rc Return status code
926 static int tftp_socket_deliver_iob ( struct xfer_interface
*socket
,
927 struct io_buffer
*iobuf
,
928 struct xfer_metadata
*meta
) {
929 struct tftp_request
*tftp
=
930 container_of ( socket
, struct tftp_request
, socket
);
932 /* Enable sending ACKs when we receive a unicast packet. This
933 * covers three cases:
935 * 1. Standard TFTP; we should always send ACKs, and will
936 * always receive a unicast packet before we need to send the
939 * 2. RFC2090 multicast TFTP; the only unicast packets we will
940 * receive are the OACKs; enable sending ACKs here (before
941 * processing the OACK) and disable it when processing the
942 * multicast option if we are not the master client.
944 * 3. MTFTP; receiving a unicast datagram indicates that we
945 * are the "master client" and should send ACKs.
947 tftp
->flags
|= TFTP_FL_SEND_ACK
;
949 return tftp_rx ( tftp
, iobuf
, meta
);
952 /** TFTP socket operations */
953 static struct xfer_interface_operations tftp_socket_operations
= {
954 .close
= ignore_xfer_close
,
955 .vredirect
= xfer_vreopen
,
956 .window
= unlimited_xfer_window
,
957 .alloc_iob
= default_xfer_alloc_iob
,
958 .deliver_iob
= tftp_socket_deliver_iob
,
959 .deliver_raw
= xfer_deliver_as_iob
,
963 * Receive new data via multicast socket
965 * @v mc_socket Multicast transport layer interface
966 * @v iobuf I/O buffer
967 * @v meta Transfer metadata
968 * @ret rc Return status code
970 static int tftp_mc_socket_deliver_iob ( struct xfer_interface
*mc_socket
,
971 struct io_buffer
*iobuf
,
972 struct xfer_metadata
*meta
) {
973 struct tftp_request
*tftp
=
974 container_of ( mc_socket
, struct tftp_request
, mc_socket
);
976 return tftp_rx ( tftp
, iobuf
, meta
);
979 /** TFTP multicast socket operations */
980 static struct xfer_interface_operations tftp_mc_socket_operations
= {
981 .close
= ignore_xfer_close
,
982 .vredirect
= xfer_vreopen
,
983 .window
= unlimited_xfer_window
,
984 .alloc_iob
= default_xfer_alloc_iob
,
985 .deliver_iob
= tftp_mc_socket_deliver_iob
,
986 .deliver_raw
= xfer_deliver_as_iob
,
990 * Close TFTP data transfer interface
992 * @v xfer Data transfer interface
993 * @v rc Reason for close
995 static void tftp_xfer_close ( struct xfer_interface
*xfer
, int rc
) {
996 struct tftp_request
*tftp
=
997 container_of ( xfer
, struct tftp_request
, xfer
);
999 DBGC ( tftp
, "TFTP %p interface closed: %s\n",
1000 tftp
, strerror ( rc
) );
1002 tftp_done ( tftp
, rc
);
1006 * Check flow control window
1008 * @v xfer Data transfer interface
1009 * @ret len Length of window
1011 static size_t tftp_xfer_window ( struct xfer_interface
*xfer
) {
1012 struct tftp_request
*tftp
=
1013 container_of ( xfer
, struct tftp_request
, xfer
);
1015 /* We abuse this data-xfer method to convey the blocksize to
1016 * the caller. This really should be done using some kind of
1017 * stat() method, but we don't yet have the facility to do
1020 return tftp
->blksize
;
1023 /** TFTP data transfer interface operations */
1024 static struct xfer_interface_operations tftp_xfer_operations
= {
1025 .close
= tftp_xfer_close
,
1026 .vredirect
= ignore_xfer_vredirect
,
1027 .window
= tftp_xfer_window
,
1028 .alloc_iob
= default_xfer_alloc_iob
,
1029 .deliver_iob
= xfer_deliver_as_raw
,
1030 .deliver_raw
= ignore_xfer_deliver_raw
,
1034 * Initiate TFTP/TFTM/MTFTP download
1036 * @v xfer Data transfer interface
1037 * @v uri Uniform Resource Identifier
1038 * @ret rc Return status code
1040 static int tftp_core_open ( struct xfer_interface
*xfer
, struct uri
*uri
,
1041 unsigned int default_port
,
1042 struct sockaddr
*multicast
,
1043 unsigned int flags
) {
1044 struct tftp_request
*tftp
;
1053 /* Allocate and populate TFTP structure */
1054 tftp
= zalloc ( sizeof ( *tftp
) );
1057 tftp
->refcnt
.free
= tftp_free
;
1058 xfer_init ( &tftp
->xfer
, &tftp_xfer_operations
, &tftp
->refcnt
);
1059 tftp
->uri
= uri_get ( uri
);
1060 xfer_init ( &tftp
->socket
, &tftp_socket_operations
, &tftp
->refcnt
);
1061 xfer_init ( &tftp
->mc_socket
, &tftp_mc_socket_operations
,
1063 tftp
->blksize
= TFTP_DEFAULT_BLKSIZE
;
1064 tftp
->flags
= flags
;
1065 tftp
->timer
.expired
= tftp_timer_expired
;
1068 tftp
->port
= uri_port ( tftp
->uri
, default_port
);
1069 if ( ( rc
= tftp_reopen ( tftp
) ) != 0 )
1072 /* Open multicast socket */
1074 if ( ( rc
= tftp_reopen_mc ( tftp
, multicast
) ) != 0 )
1078 /* Start timer to initiate RRQ */
1079 start_timer_nodelay ( &tftp
->timer
);
1081 /* Attach to parent interface, mortalise self, and return */
1082 xfer_plug_plug ( &tftp
->xfer
, xfer
);
1083 ref_put ( &tftp
->refcnt
);
1087 DBGC ( tftp
, "TFTP %p could not create request: %s\n",
1088 tftp
, strerror ( rc
) );
1089 tftp_done ( tftp
, rc
);
1090 ref_put ( &tftp
->refcnt
);
1095 * Initiate TFTP download
1097 * @v xfer Data transfer interface
1098 * @v uri Uniform Resource Identifier
1099 * @ret rc Return status code
1101 static int tftp_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
1102 return tftp_core_open ( xfer
, uri
, TFTP_PORT
, NULL
,
1103 TFTP_FL_RRQ_SIZES
);
1107 /** TFTP URI opener */
1108 struct uri_opener tftp_uri_opener __uri_opener
= {
1114 * Initiate TFTM download
1116 * @v xfer Data transfer interface
1117 * @v uri Uniform Resource Identifier
1118 * @ret rc Return status code
1120 static int tftm_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
1121 return tftp_core_open ( xfer
, uri
, TFTP_PORT
, NULL
,
1122 ( TFTP_FL_RRQ_SIZES
|
1123 TFTP_FL_RRQ_MULTICAST
) );
1127 /** TFTM URI opener */
1128 struct uri_opener tftm_uri_opener __uri_opener
= {
1134 * Initiate MTFTP download
1136 * @v xfer Data transfer interface
1137 * @v uri Uniform Resource Identifier
1138 * @ret rc Return status code
1140 static int mtftp_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
1141 return tftp_core_open ( xfer
, uri
, MTFTP_PORT
,
1142 ( struct sockaddr
* ) &tftp_mtftp_socket
,
1143 TFTP_FL_MTFTP_RECOVERY
);
1146 /** MTFTP URI opener */
1147 struct uri_opener mtftp_uri_opener __uri_opener
= {
1152 /******************************************************************************
1156 ******************************************************************************
1159 /** TFTP server setting */
1160 struct setting next_server_setting __setting
= {
1161 .name
= "next-server",
1162 .description
= "TFTP server",
1163 .tag
= DHCP_EB_SIADDR
,
1164 .type
= &setting_type_ipv4
,
1168 * Apply TFTP configuration settings
1170 * @ret rc Return status code
1172 static int tftp_apply_settings ( void ) {
1173 static struct in_addr tftp_server
= { 0 };
1174 struct in_addr last_tftp_server
;
1175 char uri_string
[32];
1178 /* Retrieve TFTP server setting */
1179 last_tftp_server
= tftp_server
;
1180 fetch_ipv4_setting ( NULL
, &next_server_setting
, &tftp_server
);
1182 /* If TFTP server setting has changed, set the current working
1183 * URI to match. Do it only when the TFTP server has changed
1184 * to try to minimise surprises to the user, who probably
1185 * won't expect the CWURI to change just because they updated
1186 * an unrelated setting and triggered all the settings
1189 if ( tftp_server
.s_addr
!= last_tftp_server
.s_addr
) {
1190 snprintf ( uri_string
, sizeof ( uri_string
),
1191 "tftp://%s/", inet_ntoa ( tftp_server
) );
1192 uri
= parse_uri ( uri_string
);
1202 /** TFTP settings applicator */
1203 struct settings_applicator tftp_settings_applicator __settings_applicator
= {
1204 .apply
= tftp_apply_settings
,