2006-08-09 01:45:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2007-01-19 02:13:12 +01:00
|
|
|
#include <stdio.h>
|
2006-08-09 01:45:52 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2007-06-11 19:11:29 +02:00
|
|
|
#include <gpxe/refcnt.h>
|
|
|
|
#include <gpxe/xfer.h>
|
|
|
|
#include <gpxe/open.h>
|
2007-01-15 10:58:26 +01:00
|
|
|
#include <gpxe/uri.h>
|
2007-06-12 00:40:34 +02:00
|
|
|
#include <gpxe/tcpip.h>
|
|
|
|
#include <gpxe/retry.h>
|
2007-08-02 15:51:03 +02:00
|
|
|
#include <gpxe/features.h>
|
2007-11-28 11:57:26 +01:00
|
|
|
#include <gpxe/bitmap.h>
|
2007-06-11 19:11:29 +02:00
|
|
|
#include <gpxe/tftp.h>
|
2006-08-09 01:45:52 +02:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* TFTP protocol
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-08-02 15:51:03 +02:00
|
|
|
FEATURE ( FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1 );
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/**
|
|
|
|
* A TFTP request
|
|
|
|
*
|
|
|
|
* This data structure holds the state for an ongoing TFTP transfer.
|
|
|
|
*/
|
|
|
|
struct tftp_request {
|
|
|
|
/** Reference count */
|
|
|
|
struct refcnt refcnt;
|
|
|
|
/** Data transfer interface */
|
|
|
|
struct xfer_interface xfer;
|
|
|
|
|
|
|
|
/** URI being fetched */
|
|
|
|
struct uri *uri;
|
|
|
|
/** Transport layer interface */
|
|
|
|
struct xfer_interface socket;
|
2007-11-28 11:57:26 +01:00
|
|
|
/** Multicast transport layer interface */
|
|
|
|
struct xfer_interface mc_socket;
|
2007-06-11 19:11:29 +02:00
|
|
|
|
|
|
|
/** Data block size
|
2006-08-09 01:45:52 +02:00
|
|
|
*
|
2007-06-11 19:11:29 +02:00
|
|
|
* This is the "blksize" option negotiated with the TFTP
|
|
|
|
* server. (If the TFTP server does not support TFTP options,
|
|
|
|
* this will default to 512).
|
|
|
|
*/
|
|
|
|
unsigned int blksize;
|
|
|
|
/** File size
|
|
|
|
*
|
|
|
|
* This is the value returned in the "tsize" option from the
|
|
|
|
* TFTP server. If the TFTP server does not support the
|
|
|
|
* "tsize" option, this value will be zero.
|
|
|
|
*/
|
|
|
|
unsigned long tsize;
|
2007-11-28 11:57:26 +01:00
|
|
|
/** Multicast address
|
|
|
|
*
|
|
|
|
* This is the destination address for multicast data
|
|
|
|
* transmissions.
|
|
|
|
*/
|
|
|
|
struct sockaddr_tcpip multicast;
|
|
|
|
/** Master client
|
2007-06-11 19:11:29 +02:00
|
|
|
*
|
2007-11-28 11:57:26 +01:00
|
|
|
* True if this is the client responsible for sending ACKs.
|
2007-06-11 19:11:29 +02:00
|
|
|
*/
|
2007-11-28 11:57:26 +01:00
|
|
|
int master;
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/** Peer address
|
|
|
|
*
|
|
|
|
* The peer address is determined by the first response
|
|
|
|
* received to the TFTP RRQ.
|
2006-08-09 01:45:52 +02:00
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
struct sockaddr_tcpip peer;
|
2007-11-28 11:57:26 +01:00
|
|
|
/** Block bitmap */
|
|
|
|
struct bitmap bitmap;
|
|
|
|
/** Maximum known length
|
|
|
|
*
|
|
|
|
* We don't always know the file length in advance. In
|
|
|
|
* particular, if the TFTP server doesn't support the tsize
|
|
|
|
* option, or we are using MTFTP, then we don't know the file
|
|
|
|
* length until we see the end-of-file block (which, in the
|
|
|
|
* case of MTFTP, may not be the last block we see).
|
|
|
|
*
|
|
|
|
* This value is updated whenever we obtain information about
|
|
|
|
* the file length.
|
|
|
|
*/
|
|
|
|
size_t filesize;
|
2007-06-11 19:11:29 +02:00
|
|
|
/** Retransmission timer */
|
|
|
|
struct retry_timer timer;
|
2006-08-09 01:45:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2007-06-11 19:11:29 +02:00
|
|
|
* Free TFTP request
|
2006-08-09 01:45:52 +02:00
|
|
|
*
|
2007-06-11 19:11:29 +02:00
|
|
|
* @v refcnt Reference counter
|
2006-08-09 01:45:52 +02:00
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static void tftp_free ( struct refcnt *refcnt ) {
|
|
|
|
struct tftp_request *tftp =
|
|
|
|
container_of ( refcnt, struct tftp_request, refcnt );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
uri_put ( tftp->uri );
|
2007-11-28 11:57:26 +01:00
|
|
|
bitmap_free ( &tftp->bitmap );
|
2007-06-11 19:11:29 +02:00
|
|
|
free ( tftp );
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-06-11 19:11:29 +02:00
|
|
|
* Mark TFTP request as complete
|
2006-08-09 01:45:52 +02:00
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
2007-06-11 19:11:29 +02:00
|
|
|
* @v rc Return status code
|
2006-08-09 01:45:52 +02:00
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static void tftp_done ( struct tftp_request *tftp, int rc ) {
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-07-01 23:04:19 +02:00
|
|
|
DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
|
|
|
|
tftp, rc, strerror ( rc ) );
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Stop the retry timer */
|
|
|
|
stop_timer ( &tftp->timer );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Close all data transfer interfaces */
|
|
|
|
xfer_nullify ( &tftp->socket );
|
|
|
|
xfer_close ( &tftp->socket, rc );
|
2007-11-28 11:57:26 +01:00
|
|
|
xfer_nullify ( &tftp->mc_socket );
|
|
|
|
xfer_close ( &tftp->mc_socket, rc );
|
2007-06-11 19:11:29 +02:00
|
|
|
xfer_nullify ( &tftp->xfer );
|
|
|
|
xfer_close ( &tftp->xfer, rc );
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/**
|
|
|
|
* Presize TFTP receive buffers and block bitmap
|
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @v filesize Known minimum file size
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int tftp_presize ( struct tftp_request *tftp, size_t filesize ) {
|
|
|
|
unsigned int num_blocks;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Do nothing if we are already large enough */
|
|
|
|
if ( filesize <= tftp->filesize )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Record filesize */
|
|
|
|
tftp->filesize = filesize;
|
|
|
|
|
|
|
|
/* Notify recipient of file size */
|
|
|
|
xfer_seek ( &tftp->xfer, filesize, SEEK_SET );
|
|
|
|
xfer_seek ( &tftp->xfer, 0, SEEK_SET );
|
|
|
|
|
|
|
|
/* Calculate expected number of blocks. Note that files whose
|
|
|
|
* length is an exact multiple of the blocksize will have a
|
|
|
|
* trailing zero-length block, which must be included.
|
|
|
|
*/
|
|
|
|
num_blocks = ( ( filesize / tftp->blksize ) + 1 );
|
|
|
|
if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
|
|
|
|
DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
|
|
|
|
"%s\n", tftp, num_blocks, strerror ( rc ) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/**
|
|
|
|
* TFTP requested blocksize
|
|
|
|
*
|
|
|
|
* This is treated as a global configuration parameter.
|
|
|
|
*/
|
|
|
|
static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
|
|
|
/**
|
2007-06-11 19:11:29 +02:00
|
|
|
* Set TFTP request blocksize
|
|
|
|
*
|
|
|
|
* @v blksize Requested block size
|
|
|
|
*/
|
|
|
|
void tftp_set_request_blksize ( unsigned int blksize ) {
|
|
|
|
if ( blksize < TFTP_DEFAULT_BLKSIZE )
|
|
|
|
blksize = TFTP_DEFAULT_BLKSIZE;
|
|
|
|
tftp_request_blksize = blksize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transmit RRQ
|
2006-08-09 01:45:52 +02:00
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static int tftp_send_rrq ( struct tftp_request *tftp ) {
|
|
|
|
struct tftp_rrq *rrq;
|
|
|
|
const char *path = tftp->uri->path;
|
|
|
|
size_t len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
|
|
|
|
+ 5 + 1 /* "octet" + NUL */
|
|
|
|
+ 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
|
2007-11-28 11:57:26 +01:00
|
|
|
+ 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
|
|
|
|
+ 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
|
2007-06-11 19:11:29 +02:00
|
|
|
struct io_buffer *iobuf;
|
|
|
|
|
|
|
|
DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
|
|
|
|
|
|
|
|
/* Allocate buffer */
|
|
|
|
iobuf = xfer_alloc_iob ( &tftp->socket, len );
|
|
|
|
if ( ! iobuf )
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Build request */
|
|
|
|
rrq = iob_put ( iobuf, sizeof ( *rrq ) );
|
|
|
|
rrq->opcode = htons ( TFTP_RRQ );
|
|
|
|
iob_put ( iobuf,
|
2007-06-28 19:38:50 +02:00
|
|
|
snprintf ( rrq->data, iob_tailroom ( iobuf ),
|
2007-11-28 11:57:26 +01:00
|
|
|
"%s%coctet%cblksize%c%d%ctsize%c0%cmulticast%c",
|
|
|
|
path, 0, 0, 0, tftp_request_blksize, 0,
|
|
|
|
0, 0, 0 ) + 1 );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* RRQ always goes to the address specified in the initial
|
|
|
|
* xfer_open() call
|
|
|
|
*/
|
|
|
|
return xfer_deliver_iob ( &tftp->socket, iobuf );
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-06-11 19:11:29 +02:00
|
|
|
* Transmit ACK
|
2006-08-09 01:45:52 +02:00
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
2007-06-11 19:11:29 +02:00
|
|
|
* @ret rc Return status code
|
2006-08-09 01:45:52 +02:00
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static int tftp_send_ack ( struct tftp_request *tftp ) {
|
|
|
|
struct tftp_ack *ack;
|
|
|
|
struct io_buffer *iobuf;
|
|
|
|
struct xfer_metadata meta = {
|
|
|
|
.dest = ( struct sockaddr * ) &tftp->peer,
|
|
|
|
};
|
2007-11-28 11:57:26 +01:00
|
|
|
unsigned int block;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* Determine next required block number */
|
|
|
|
block = bitmap_first_gap ( &tftp->bitmap );
|
|
|
|
DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
|
2007-07-01 23:04:19 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Allocate buffer */
|
|
|
|
iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
|
|
|
|
if ( ! iobuf )
|
|
|
|
return -ENOMEM;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Build ACK */
|
|
|
|
ack = iob_put ( iobuf, sizeof ( *ack ) );
|
|
|
|
ack->opcode = htons ( TFTP_ACK );
|
2007-11-28 11:57:26 +01:00
|
|
|
ack->block = htons ( block );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* ACK always goes to the peer recorded from the RRQ response */
|
|
|
|
return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-28 11:57:26 +01:00
|
|
|
* Transmit next relevant packet
|
2006-08-09 01:45:52 +02:00
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
2007-06-11 19:11:29 +02:00
|
|
|
* @ret rc Return status code
|
2006-08-09 01:45:52 +02:00
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static int tftp_send_packet ( struct tftp_request *tftp ) {
|
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* Update retransmission timer */
|
|
|
|
stop_timer ( &tftp->timer );
|
2006-08-09 01:45:52 +02:00
|
|
|
start_timer ( &tftp->timer );
|
2007-06-11 19:11:29 +02:00
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* If we are the master client, send RRQ or ACK as appropriate */
|
|
|
|
if ( tftp->master ) {
|
|
|
|
if ( ! tftp->peer.st_family ) {
|
|
|
|
return tftp_send_rrq ( tftp );
|
|
|
|
} else {
|
|
|
|
return tftp_send_ack ( tftp );
|
|
|
|
}
|
2007-06-11 19:11:29 +02:00
|
|
|
} else {
|
2007-11-28 11:57:26 +01:00
|
|
|
/* Do nothing when not the master client */
|
|
|
|
return 0;
|
2007-06-11 19:11:29 +02:00
|
|
|
}
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle TFTP retransmission timer expiry
|
|
|
|
*
|
|
|
|
* @v timer Retry timer
|
|
|
|
* @v fail Failure indicator
|
|
|
|
*/
|
|
|
|
static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
|
2007-06-11 19:11:29 +02:00
|
|
|
struct tftp_request *tftp =
|
|
|
|
container_of ( timer, struct tftp_request, timer );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
|
|
|
if ( fail ) {
|
|
|
|
tftp_done ( tftp, -ETIMEDOUT );
|
|
|
|
} else {
|
|
|
|
tftp_send_packet ( tftp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-06-11 19:11:29 +02:00
|
|
|
* Process TFTP "blksize" option
|
2006-08-09 01:45:52 +02:00
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
2007-06-11 19:11:29 +02:00
|
|
|
* @v value Option value
|
2006-08-09 01:45:52 +02:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static int tftp_process_blksize ( struct tftp_request *tftp,
|
|
|
|
const char *value ) {
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
tftp->blksize = strtoul ( value, &end, 10 );
|
|
|
|
if ( *end ) {
|
|
|
|
DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
|
|
|
|
tftp, value );
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process TFTP "tsize" option
|
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @v value Option value
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int tftp_process_tsize ( struct tftp_request *tftp,
|
|
|
|
const char *value ) {
|
|
|
|
char *end;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
tftp->tsize = strtoul ( value, &end, 10 );
|
|
|
|
if ( *end ) {
|
|
|
|
DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
|
|
|
|
tftp, value );
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process TFTP "multicast" option
|
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @v value Option value
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int tftp_process_multicast ( struct tftp_request *tftp,
|
|
|
|
const char *value ) {
|
|
|
|
struct sockaddr_in *sin = ( struct sockaddr_in * ) &tftp->multicast;
|
|
|
|
char buf[ strlen ( value ) + 1 ];
|
|
|
|
char *addr;
|
|
|
|
char *port;
|
|
|
|
char *port_end;
|
|
|
|
char *mc;
|
|
|
|
char *mc_end;
|
|
|
|
struct sockaddr *mc_peer;
|
|
|
|
struct sockaddr *mc_local;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Split value into "addr,port,mc" fields */
|
|
|
|
memcpy ( buf, value, sizeof ( buf ) );
|
|
|
|
addr = buf;
|
|
|
|
port = strchr ( addr, ',' );
|
|
|
|
if ( ! port ) {
|
|
|
|
DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
*(port++) = '\0';
|
|
|
|
mc = strchr ( port, ',' );
|
|
|
|
if ( ! mc ) {
|
|
|
|
DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
*(mc++) = '\0';
|
|
|
|
|
|
|
|
/* Parse parameters */
|
|
|
|
if ( *addr ) {
|
|
|
|
if ( inet_aton ( addr, &sin->sin_addr ) == 0 ) {
|
|
|
|
DBGC ( tftp, "TFTP %p multicast invalid IP address "
|
|
|
|
"%s\n", tftp, addr );
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
DBGC ( tftp, "TFTP %p multicast IP address %s\n",
|
|
|
|
tftp, inet_ntoa ( sin->sin_addr ) );
|
|
|
|
}
|
|
|
|
if ( *port ) {
|
|
|
|
sin->sin_port = htons ( strtoul ( port, &port_end, 0 ) );
|
|
|
|
if ( *port_end ) {
|
|
|
|
DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
|
|
|
|
tftp, port );
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
DBGC ( tftp, "TFTP %p multicast port %d\n",
|
|
|
|
tftp, ntohs ( sin->sin_port ) );
|
|
|
|
}
|
|
|
|
tftp->master = strtoul ( mc, &mc_end, 0 );
|
|
|
|
if ( *mc_end ) {
|
|
|
|
DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
DBGC ( tftp, "TFTP %p is%s the master client\n",
|
|
|
|
tftp, ( tftp->master ? "" : " not" ) );
|
|
|
|
|
|
|
|
/* Open multicast socket, if new address specified */
|
|
|
|
if ( *addr || *port ) {
|
|
|
|
xfer_close ( &tftp->mc_socket, 0 );
|
|
|
|
mc_peer = ( ( struct sockaddr * ) &tftp->peer );
|
|
|
|
mc_local = ( ( struct sockaddr * ) &tftp->multicast );
|
|
|
|
mc_local->sa_family = mc_peer->sa_family;
|
|
|
|
if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
|
|
|
|
mc_peer, mc_local ) ) != 0 ) {
|
|
|
|
DBGC ( tftp, "TFTP %p could not open multicast "
|
|
|
|
"socket: %s\n", tftp, strerror ( rc ) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2007-06-11 19:11:29 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A TFTP option */
|
|
|
|
struct tftp_option {
|
|
|
|
/** Option name */
|
|
|
|
const char *name;
|
|
|
|
/** Option processor
|
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @v value Option value
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int ( * process ) ( struct tftp_request *tftp, const char *value );
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Recognised TFTP options */
|
|
|
|
static struct tftp_option tftp_options[] = {
|
|
|
|
{ "blksize", tftp_process_blksize },
|
|
|
|
{ "tsize", tftp_process_tsize },
|
2007-11-28 11:57:26 +01:00
|
|
|
{ "multicast", tftp_process_multicast },
|
2007-06-11 19:11:29 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process TFTP option
|
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @v name Option name
|
|
|
|
* @v value Option value
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int tftp_process_option ( struct tftp_request *tftp,
|
|
|
|
const char *name, const char *value ) {
|
|
|
|
struct tftp_option *option;
|
|
|
|
|
|
|
|
for ( option = tftp_options ; option->name ; option++ ) {
|
|
|
|
if ( strcasecmp ( name, option->name ) == 0 )
|
|
|
|
return option->process ( tftp, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
|
|
|
|
tftp, name, value );
|
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* Unknown options should be silently ignored */
|
|
|
|
return 0;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive OACK
|
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @v buf Temporary data buffer
|
|
|
|
* @v len Length of temporary data buffer
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
|
2006-08-09 01:45:52 +02:00
|
|
|
struct tftp_oack *oack = buf;
|
|
|
|
char *end = buf + len;
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if ( len < sizeof ( *oack ) ) {
|
2007-01-11 16:14:54 +01:00
|
|
|
DBGC ( tftp, "TFTP %p received underlength OACK packet "
|
|
|
|
"length %d\n", tftp, len );
|
2007-11-28 11:57:26 +01:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
if ( end[-1] != '\0' ) {
|
2007-01-11 16:14:54 +01:00
|
|
|
DBGC ( tftp, "TFTP %p received OACK missing final NUL\n",
|
|
|
|
tftp );
|
2007-11-28 11:57:26 +01:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process each option in turn */
|
|
|
|
name = oack->data;
|
|
|
|
while ( name < end ) {
|
|
|
|
value = ( name + strlen ( name ) + 1 );
|
|
|
|
if ( value == end ) {
|
2007-01-11 16:14:54 +01:00
|
|
|
DBGC ( tftp, "TFTP %p received OACK missing value "
|
|
|
|
"for option \"%s\"\n", tftp, name );
|
2007-11-28 11:57:26 +01:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
|
2007-11-28 11:57:26 +01:00
|
|
|
goto done;
|
2006-08-09 01:45:52 +02:00
|
|
|
name = ( value + strlen ( value ) + 1 );
|
|
|
|
}
|
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* Process tsize information, if available */
|
|
|
|
if ( tftp->tsize ) {
|
|
|
|
if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
|
|
|
|
goto done;
|
|
|
|
}
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* Request next data block */
|
|
|
|
tftp_send_packet ( tftp );
|
|
|
|
|
|
|
|
done:
|
|
|
|
if ( rc )
|
|
|
|
tftp_done ( tftp, rc );
|
|
|
|
return rc;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive DATA
|
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
2007-06-11 19:11:29 +02:00
|
|
|
* @v iobuf I/O buffer
|
2006-08-09 01:45:52 +02:00
|
|
|
* @ret rc Return status code
|
2007-06-11 19:11:29 +02:00
|
|
|
*
|
|
|
|
* Takes ownership of I/O buffer.
|
2006-08-09 01:45:52 +02:00
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static int tftp_rx_data ( struct tftp_request *tftp,
|
|
|
|
struct io_buffer *iobuf ) {
|
|
|
|
struct tftp_data *data = iobuf->data;
|
2007-09-08 20:30:25 +02:00
|
|
|
int block;
|
2007-11-28 11:57:26 +01:00
|
|
|
off_t offset;
|
2006-08-09 01:45:52 +02:00
|
|
|
size_t data_len;
|
2007-01-11 16:14:54 +01:00
|
|
|
int rc;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
|
|
|
/* Sanity check */
|
2007-06-11 19:11:29 +02:00
|
|
|
if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
|
2007-01-11 16:14:54 +01:00
|
|
|
DBGC ( tftp, "TFTP %p received underlength DATA packet "
|
2007-06-11 19:11:29 +02:00
|
|
|
"length %d\n", tftp, iob_len ( iobuf ) );
|
2007-11-28 11:57:26 +01:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Extract data */
|
2007-11-28 11:57:26 +01:00
|
|
|
block = ( ntohs ( data->block ) - 1 );
|
|
|
|
offset = ( block * tftp->blksize );
|
2007-06-11 19:11:29 +02:00
|
|
|
iob_pull ( iobuf, sizeof ( *data ) );
|
|
|
|
data_len = iob_len ( iobuf );
|
2007-11-28 11:57:26 +01:00
|
|
|
if ( data_len > tftp->blksize ) {
|
|
|
|
DBGC ( tftp, "TFTP %p received overlength DATA packet "
|
|
|
|
"length %d\n", tftp, data_len );
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2007-09-08 20:30:25 +02:00
|
|
|
}
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Deliver data */
|
2007-11-28 11:57:26 +01:00
|
|
|
xfer_seek ( &tftp->xfer, offset, SEEK_SET );
|
|
|
|
rc = xfer_deliver_iob ( &tftp->xfer, iobuf );
|
|
|
|
iobuf = NULL;
|
|
|
|
if ( rc != 0 ) {
|
2007-06-11 19:11:29 +02:00
|
|
|
DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
|
2007-01-11 16:14:54 +01:00
|
|
|
tftp, strerror ( rc ) );
|
2007-11-28 11:57:26 +01:00
|
|
|
goto done;
|
2007-01-11 16:14:54 +01:00
|
|
|
}
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* Ensure block bitmap is ready */
|
|
|
|
if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
|
|
|
|
goto done;
|
|
|
|
|
2006-08-09 01:45:52 +02:00
|
|
|
/* Mark block as received */
|
2007-11-28 11:57:26 +01:00
|
|
|
bitmap_set ( &tftp->bitmap, block );
|
|
|
|
|
|
|
|
/* Acknowledge block */
|
|
|
|
tftp_send_packet ( tftp );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* If all blocks have been received, finish. */
|
|
|
|
if ( bitmap_full ( &tftp->bitmap ) )
|
2006-08-09 01:45:52 +02:00
|
|
|
tftp_done ( tftp, 0 );
|
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
done:
|
|
|
|
free_iob ( iobuf );
|
|
|
|
if ( rc )
|
|
|
|
tftp_done ( tftp, rc );
|
|
|
|
return rc;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/** Translation between TFTP errors and internal error numbers */
|
|
|
|
static const uint8_t tftp_errors[] = {
|
|
|
|
[TFTP_ERR_FILE_NOT_FOUND] = PXENV_STATUS_TFTP_FILE_NOT_FOUND,
|
|
|
|
[TFTP_ERR_ACCESS_DENIED] = PXENV_STATUS_TFTP_ACCESS_VIOLATION,
|
|
|
|
[TFTP_ERR_ILLEGAL_OP] = PXENV_STATUS_TFTP_UNKNOWN_OPCODE,
|
|
|
|
};
|
2006-08-09 01:45:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive ERROR
|
|
|
|
*
|
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @v buf Temporary data buffer
|
|
|
|
* @v len Length of temporary data buffer
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
|
2006-08-09 01:45:52 +02:00
|
|
|
struct tftp_error *error = buf;
|
|
|
|
unsigned int err;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if ( len < sizeof ( *error ) ) {
|
2007-01-11 16:14:54 +01:00
|
|
|
DBGC ( tftp, "TFTP %p received underlength ERROR packet "
|
|
|
|
"length %d\n", tftp, len );
|
2006-08-09 01:45:52 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2007-01-11 16:14:54 +01:00
|
|
|
DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
|
|
|
|
"\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
|
|
|
/* Determine final operation result */
|
|
|
|
err = ntohs ( error->errcode );
|
|
|
|
if ( err < ( sizeof ( tftp_errors ) / sizeof ( tftp_errors[0] ) ) )
|
|
|
|
rc = -tftp_errors[err];
|
|
|
|
if ( ! rc )
|
|
|
|
rc = -PXENV_STATUS_TFTP_CANNOT_OPEN_CONNECTION;
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Close TFTP request */
|
2006-08-09 01:45:52 +02:00
|
|
|
tftp_done ( tftp, rc );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive new data
|
|
|
|
*
|
2007-11-28 11:57:26 +01:00
|
|
|
* @v tftp TFTP connection
|
|
|
|
* @v iobuf I/O buffer
|
|
|
|
* @v meta Transfer metadata, or NULL
|
|
|
|
* @ret rc Return status code
|
2006-08-09 01:45:52 +02:00
|
|
|
*/
|
2007-11-28 11:57:26 +01:00
|
|
|
static int tftp_rx ( struct tftp_request *tftp,
|
|
|
|
struct io_buffer *iobuf,
|
|
|
|
struct xfer_metadata *meta ) {
|
2007-06-11 19:11:29 +02:00
|
|
|
struct sockaddr_tcpip *st_src;
|
|
|
|
struct tftp_common *common = iobuf->data;
|
|
|
|
size_t len = iob_len ( iobuf );
|
|
|
|
int rc = -EINVAL;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Sanity checks */
|
2006-08-09 01:45:52 +02:00
|
|
|
if ( len < sizeof ( *common ) ) {
|
2007-01-11 16:14:54 +01:00
|
|
|
DBGC ( tftp, "TFTP %p received underlength packet length %d\n",
|
|
|
|
tftp, len );
|
2007-06-11 19:11:29 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ( ! meta ) {
|
|
|
|
DBGC ( tftp, "TFTP %p received packet without metadata\n",
|
|
|
|
tftp );
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ( ! meta->src ) {
|
|
|
|
DBGC ( tftp, "TFTP %p received packet without source port\n",
|
|
|
|
tftp );
|
|
|
|
goto done;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Filter by TID. Set TID on first response received */
|
2007-06-11 19:11:29 +02:00
|
|
|
st_src = ( struct sockaddr_tcpip * ) meta->src;
|
2007-11-28 11:57:26 +01:00
|
|
|
if ( ! tftp->peer.st_family ) {
|
2007-06-11 19:11:29 +02:00
|
|
|
memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
|
2007-01-11 16:14:54 +01:00
|
|
|
DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
|
2007-06-11 19:11:29 +02:00
|
|
|
ntohs ( tftp->peer.st_port ) );
|
|
|
|
} else if ( memcmp ( &tftp->peer, st_src,
|
|
|
|
sizeof ( tftp->peer ) ) != 0 ) {
|
|
|
|
DBGC ( tftp, "TFTP %p received packet from wrong source (got "
|
|
|
|
"%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
|
|
|
|
ntohs ( tftp->peer.st_port ) );
|
|
|
|
goto done;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ( common->opcode ) {
|
|
|
|
case htons ( TFTP_OACK ):
|
2007-06-11 19:11:29 +02:00
|
|
|
rc = tftp_rx_oack ( tftp, iobuf->data, len );
|
|
|
|
break;
|
2006-08-09 01:45:52 +02:00
|
|
|
case htons ( TFTP_DATA ):
|
2007-06-11 19:11:29 +02:00
|
|
|
rc = tftp_rx_data ( tftp, iobuf );
|
|
|
|
iobuf = NULL;
|
|
|
|
break;
|
2006-08-09 01:45:52 +02:00
|
|
|
case htons ( TFTP_ERROR ):
|
2007-06-11 19:11:29 +02:00
|
|
|
rc = tftp_rx_error ( tftp, iobuf->data, len );
|
|
|
|
break;
|
2006-08-09 01:45:52 +02:00
|
|
|
default:
|
2007-06-11 19:11:29 +02:00
|
|
|
DBGC ( tftp, "TFTP %p received strange packet type %d\n",
|
|
|
|
tftp, ntohs ( common->opcode ) );
|
|
|
|
break;
|
2006-08-09 01:45:52 +02:00
|
|
|
};
|
2007-06-11 19:11:29 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
free_iob ( iobuf );
|
|
|
|
return rc;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/**
|
|
|
|
* Receive new data via socket
|
|
|
|
*
|
|
|
|
* @v socket Transport layer interface
|
|
|
|
* @v iobuf I/O buffer
|
|
|
|
* @v meta Transfer metadata, or NULL
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
|
|
|
|
struct io_buffer *iobuf,
|
|
|
|
struct xfer_metadata *meta ) {
|
|
|
|
struct tftp_request *tftp =
|
|
|
|
container_of ( socket, struct tftp_request, socket );
|
|
|
|
|
|
|
|
return tftp_rx ( tftp, iobuf, meta );
|
|
|
|
}
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/**
|
|
|
|
* TFTP connection closed by network stack
|
|
|
|
*
|
|
|
|
* @v socket Transport layer interface
|
|
|
|
* @v rc Reason for close
|
|
|
|
*/
|
|
|
|
static void tftp_socket_close ( struct xfer_interface *socket, int rc ) {
|
|
|
|
struct tftp_request *tftp =
|
|
|
|
container_of ( socket, struct tftp_request, socket );
|
|
|
|
|
|
|
|
DBGC ( tftp, "TFTP %p socket closed: %s\n",
|
|
|
|
tftp, strerror ( rc ) );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/* Any close counts as an error */
|
|
|
|
if ( ! rc )
|
|
|
|
rc = -ECONNRESET;
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
tftp_done ( tftp, rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** TFTP socket operations */
|
|
|
|
static struct xfer_interface_operations tftp_socket_operations = {
|
|
|
|
.close = tftp_socket_close,
|
|
|
|
.vredirect = xfer_vopen,
|
|
|
|
.seek = ignore_xfer_seek,
|
2007-07-08 15:11:07 +02:00
|
|
|
.window = unlimited_xfer_window,
|
2007-06-11 19:11:29 +02:00
|
|
|
.alloc_iob = default_xfer_alloc_iob,
|
|
|
|
.deliver_iob = tftp_socket_deliver_iob,
|
|
|
|
.deliver_raw = xfer_deliver_as_iob,
|
|
|
|
};
|
2007-11-28 11:57:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive new data via multicast socket
|
|
|
|
*
|
|
|
|
* @v mc_socket Multicast transport layer interface
|
|
|
|
* @v iobuf I/O buffer
|
|
|
|
* @v meta Transfer metadata, or NULL
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int tftp_mc_socket_deliver_iob ( struct xfer_interface *mc_socket,
|
|
|
|
struct io_buffer *iobuf,
|
|
|
|
struct xfer_metadata *meta ) {
|
|
|
|
struct tftp_request *tftp =
|
|
|
|
container_of ( mc_socket, struct tftp_request, mc_socket );
|
|
|
|
|
|
|
|
return tftp_rx ( tftp, iobuf, meta );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TFTP multicast connection closed by network stack
|
|
|
|
*
|
|
|
|
* @v socket Multicast transport layer interface
|
|
|
|
* @v rc Reason for close
|
|
|
|
*/
|
|
|
|
static void tftp_mc_socket_close ( struct xfer_interface *mc_socket,
|
|
|
|
int rc ) {
|
|
|
|
struct tftp_request *tftp =
|
|
|
|
container_of ( mc_socket, struct tftp_request, mc_socket );
|
|
|
|
|
|
|
|
DBGC ( tftp, "TFTP %p multicast socket closed: %s\n",
|
|
|
|
tftp, strerror ( rc ) );
|
|
|
|
|
|
|
|
/* The multicast socket may be closed when we receive a new
|
|
|
|
* OACK and open/reopen the socket; we should not call
|
|
|
|
* tftp_done() at this point.
|
|
|
|
*/
|
|
|
|
}
|
2007-06-11 19:11:29 +02:00
|
|
|
|
2007-11-28 11:57:26 +01:00
|
|
|
/** TFTP multicast socket operations */
|
|
|
|
static struct xfer_interface_operations tftp_mc_socket_operations = {
|
|
|
|
.close = tftp_mc_socket_close,
|
|
|
|
.vredirect = xfer_vopen,
|
|
|
|
.seek = ignore_xfer_seek,
|
|
|
|
.window = unlimited_xfer_window,
|
|
|
|
.alloc_iob = default_xfer_alloc_iob,
|
|
|
|
.deliver_iob = tftp_mc_socket_deliver_iob,
|
|
|
|
.deliver_raw = xfer_deliver_as_iob,
|
|
|
|
};
|
|
|
|
|
2007-01-15 10:58:26 +01:00
|
|
|
/**
|
2007-06-11 19:11:29 +02:00
|
|
|
* Close TFTP data transfer interface
|
2007-01-15 10:58:26 +01:00
|
|
|
*
|
2007-06-11 19:11:29 +02:00
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v rc Reason for close
|
2007-01-15 10:58:26 +01:00
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
|
|
|
|
struct tftp_request *tftp =
|
|
|
|
container_of ( xfer, struct tftp_request, xfer );
|
2007-01-15 10:58:26 +01:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
DBGC ( tftp, "TFTP %p interface closed: %s\n",
|
|
|
|
tftp, strerror ( rc ) );
|
|
|
|
|
|
|
|
tftp_done ( tftp, rc );
|
2007-01-15 10:58:26 +01:00
|
|
|
}
|
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/** TFTP data transfer interface operations */
|
|
|
|
static struct xfer_interface_operations tftp_xfer_operations = {
|
|
|
|
.close = tftp_xfer_close,
|
|
|
|
.vredirect = ignore_xfer_vredirect,
|
|
|
|
.seek = ignore_xfer_seek,
|
2007-07-08 15:11:07 +02:00
|
|
|
.window = unlimited_xfer_window,
|
2007-06-11 19:11:29 +02:00
|
|
|
.alloc_iob = default_xfer_alloc_iob,
|
|
|
|
.deliver_iob = xfer_deliver_as_raw,
|
|
|
|
.deliver_raw = ignore_xfer_deliver_raw,
|
2007-01-15 10:58:26 +01:00
|
|
|
};
|
|
|
|
|
2006-08-09 01:45:52 +02:00
|
|
|
/**
|
|
|
|
* Initiate TFTP download
|
|
|
|
*
|
2007-06-11 19:11:29 +02:00
|
|
|
* @v xfer Data transfer interface
|
2007-01-15 10:58:26 +01:00
|
|
|
* @v uri Uniform Resource Identifier
|
|
|
|
* @ret rc Return status code
|
2006-08-09 01:45:52 +02:00
|
|
|
*/
|
2007-06-11 19:11:29 +02:00
|
|
|
int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
|
|
|
|
struct tftp_request *tftp;
|
|
|
|
struct sockaddr_tcpip server;
|
2006-08-09 01:45:52 +02:00
|
|
|
int rc;
|
|
|
|
|
2007-01-15 10:58:26 +01:00
|
|
|
/* Sanity checks */
|
2007-06-11 19:11:29 +02:00
|
|
|
if ( ! uri->host )
|
|
|
|
return -EINVAL;
|
|
|
|
if ( ! uri->path )
|
|
|
|
return -EINVAL;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-01-15 10:58:26 +01:00
|
|
|
/* Allocate and populate TFTP structure */
|
2007-06-18 01:25:21 +02:00
|
|
|
tftp = zalloc ( sizeof ( *tftp ) );
|
2007-06-11 19:11:29 +02:00
|
|
|
if ( ! tftp )
|
|
|
|
return -ENOMEM;
|
|
|
|
tftp->refcnt.free = tftp_free;
|
|
|
|
xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
|
|
|
|
tftp->uri = uri_get ( uri );
|
|
|
|
xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
|
2007-11-28 11:57:26 +01:00
|
|
|
xfer_init ( &tftp->mc_socket, &tftp_mc_socket_operations,
|
|
|
|
&tftp->refcnt );
|
2007-09-06 14:37:47 +02:00
|
|
|
tftp->blksize = TFTP_DEFAULT_BLKSIZE;
|
2007-11-28 11:57:26 +01:00
|
|
|
tftp->master = 1;
|
2007-01-11 16:14:54 +01:00
|
|
|
tftp->timer.expired = tftp_timer_expired;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Open socket */
|
|
|
|
memset ( &server, 0, sizeof ( server ) );
|
|
|
|
server.st_port = htons ( uri_port ( tftp->uri, TFTP_PORT ) );
|
|
|
|
if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
|
|
|
|
( struct sockaddr * ) &server,
|
|
|
|
uri->host, NULL ) ) != 0 )
|
2007-01-15 10:58:26 +01:00
|
|
|
goto err;
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Start timer to initiate RRQ */
|
2007-08-13 20:03:33 +02:00
|
|
|
start_timer_nodelay ( &tftp->timer );
|
2006-08-09 01:45:52 +02:00
|
|
|
|
2007-06-11 19:11:29 +02:00
|
|
|
/* Attach to parent interface, mortalise self, and return */
|
|
|
|
xfer_plug_plug ( &tftp->xfer, xfer );
|
|
|
|
ref_put ( &tftp->refcnt );
|
2007-01-15 10:58:26 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2007-06-11 19:11:29 +02:00
|
|
|
DBGC ( tftp, "TFTP %p could not create request: %s\n",
|
2007-01-15 10:58:26 +01:00
|
|
|
tftp, strerror ( rc ) );
|
2007-06-11 19:11:29 +02:00
|
|
|
tftp_done ( tftp, rc );
|
|
|
|
ref_put ( &tftp->refcnt );
|
2007-01-15 10:58:26 +01:00
|
|
|
return rc;
|
2006-08-09 01:45:52 +02:00
|
|
|
}
|
2007-06-11 19:11:29 +02:00
|
|
|
|
|
|
|
/** TFTP URI opener */
|
|
|
|
struct uri_opener tftp_uri_opener __uri_opener = {
|
|
|
|
.scheme = "tftp",
|
|
|
|
.open = tftp_open,
|
|
|
|
};
|