Updated TFTP and PXE UDP API code to use not-yet-implemented data-xfer

UDP API.
pull/1/head
Michael Brown 2007-06-11 18:11:29 +01:00
parent 95adce0ce1
commit 01b4bde8a0
4 changed files with 478 additions and 411 deletions

View File

@ -80,69 +80,6 @@ union tftp_any {
struct tftp_oack oack; struct tftp_oack oack;
}; };
/** extern void tftp_set_request_blksize ( unsigned int blksize );
* A TFTP session
*
* This data structure holds the state for an ongoing TFTP transfer.
*/
struct tftp_session {
/** URI being fetched */
struct uri *uri;
/** Data buffer to fill */
struct buffer *buffer;
/** Asynchronous operation */
struct async async;
/** Requested data block size
*
* This is the "blksize" option requested from the TFTP
* server. It may or may not be honoured.
*/
unsigned int request_blksize;
/** Data block size
*
* 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;
/**
* Transfer ID
*
* This is the transfer ID allocated by the server, used as
* the server UDP port for all packets except the initial read
* request.
*/
uint16_t tid;
/** Session state
*
* This is the block number to be used in the next ACK sent
* back to the server, i.e. the number of the last received
* data block. The value zero indicates that the last
* received block was an OACK (i.e. that the next ACK will
* contain a block number of zero), and any value less than
* zero indicates that the connection has not yet been opened
* (i.e. that no blocks have yet been received).
*/
int state;
/** UDP connection */
struct udp_connection udp;
/** Retransmission timer */
struct retry_timer timer;
};
/* Function prototypes */
extern int tftp_get ( struct uri *uri, struct buffer *buffer,
struct async *parent );
#endif /* _GPXE_TFTP_H */ #endif /* _GPXE_TFTP_H */

View File

@ -71,10 +71,11 @@ static void pxe_tftp_build_uri ( char uri_string[PXE_URI_LEN],
port = htons ( TFTP_PORT ); port = htons ( TFTP_PORT );
if ( ! blksize ) if ( ! blksize )
blksize = TFTP_MAX_BLKSIZE; blksize = TFTP_MAX_BLKSIZE;
snprintf ( uri_string, sizeof ( uri_string ), tftp_set_request_blksize ( blksize );
"tftp://%s:%d%s%s?blksize=%d", inet_ntoa ( address ),
ntohs ( port ), ( ( filename[0] == '/' ) ? "" : "/" ), snprintf ( uri_string, sizeof ( uri_string ), "tftp://%s:%d%s%s",
filename, blksize ); inet_ntoa ( address ), ntohs ( port ),
( ( filename[0] == '/' ) ? "" : "/" ), filename );
} }
/** /**

View File

@ -6,6 +6,7 @@
#include <string.h> #include <string.h>
#include <byteswap.h> #include <byteswap.h>
#include <gpxe/xfer.h>
#include <gpxe/udp.h> #include <gpxe/udp.h>
#include <gpxe/uaccess.h> #include <gpxe/uaccess.h>
#include <gpxe/process.h> #include <gpxe/process.h>
@ -31,85 +32,63 @@
/** A PXE UDP connection */ /** A PXE UDP connection */
struct pxe_udp_connection { struct pxe_udp_connection {
/** Etherboot UDP connection */ /** Data transfer interface to UDP stack */
struct udp_connection udp; struct xfer_interface xfer;
/** "Connection is open" flag */ /** "Connection is open" flag */
int open; int open;
/** Current pxenv_udp_read() operation, if any */ /** Local address */
struct sockaddr_in local;
/** Current PXENV_UDP_READ parameter block */
struct s_PXENV_UDP_READ *pxenv_udp_read; struct s_PXENV_UDP_READ *pxenv_udp_read;
/** Current pxenv_udp_write() operation, if any */
struct s_PXENV_UDP_WRITE *pxenv_udp_write;
}; };
static inline struct pxe_udp_connection *
udp_to_pxe ( struct udp_connection *conn ) {
return container_of ( conn, struct pxe_udp_connection, udp );
}
/**
* Send PXE UDP data
*
* @v conn UDP connection
* @v data Temporary data buffer
* @v len Size of temporary data buffer
* @ret rc Return status code
*
* Sends the packet belonging to the current pxenv_udp_write()
* operation.
*/
static int pxe_udp_senddata ( struct udp_connection *conn, void *data,
size_t len ) {
struct pxe_udp_connection *pxe_udp = udp_to_pxe ( conn );
struct s_PXENV_UDP_WRITE *pxenv_udp_write = pxe_udp->pxenv_udp_write;
userptr_t buffer;
/* Transmit packet */
buffer = real_to_user ( pxenv_udp_write->buffer.segment,
pxenv_udp_write->buffer.offset );
if ( len > pxenv_udp_write->buffer_size )
len = pxenv_udp_write->buffer_size;
copy_from_user ( data, buffer, 0, len );
return udp_send ( conn, data, len );
}
/** /**
* Receive PXE UDP data * Receive PXE UDP data
* *
* @v conn UDP connection * @v xfer Data transfer interface
* @v data Received data * @v iobuf I/O buffer
* @v len Length of received data * @v meta Data transfer metadata
* @v st_src Source address * @ret rc Return status code
* @v st_dest Destination address
* *
* Receives a packet as part of the current pxenv_udp_read() * Receives a packet as part of the current pxenv_udp_read()
* operation. * operation.
*/ */
static int pxe_udp_newdata ( struct udp_connection *conn, void *data, static int pxe_udp_deliver_iob ( struct xfer_interface *xfer,
size_t len, struct sockaddr_tcpip *st_src, struct io_buffer *iobuf,
struct sockaddr_tcpip *st_dest ) { struct xfer_metadata *meta ) {
struct pxe_udp_connection *pxe_udp = udp_to_pxe ( conn ); struct pxe_udp_connection *pxe_udp =
container_of ( xfer, struct pxe_udp_connection, xfer );
struct s_PXENV_UDP_READ *pxenv_udp_read = pxe_udp->pxenv_udp_read; struct s_PXENV_UDP_READ *pxenv_udp_read = pxe_udp->pxenv_udp_read;
struct sockaddr_in *sin_src = ( ( struct sockaddr_in * ) st_src ); struct sockaddr_in *sin_src;
struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest ); struct sockaddr_in *sin_dest;
userptr_t buffer; userptr_t buffer;
size_t len;
int rc = 0;
if ( ! pxenv_udp_read ) { if ( ! pxenv_udp_read ) {
DBG ( "PXE discarded UDP packet\n" ); DBG ( "PXE discarded UDP packet\n" );
return -ENOBUFS; rc = -ENOBUFS;
goto done;
} }
/* Copy packet to buffer and record length */ /* Copy packet to buffer and record length */
buffer = real_to_user ( pxenv_udp_read->buffer.segment, buffer = real_to_user ( pxenv_udp_read->buffer.segment,
pxenv_udp_read->buffer.offset ); pxenv_udp_read->buffer.offset );
len = iob_len ( iobuf );
if ( len > pxenv_udp_read->buffer_size ) if ( len > pxenv_udp_read->buffer_size )
len = pxenv_udp_read->buffer_size; len = pxenv_udp_read->buffer_size;
copy_to_user ( buffer, 0, data, len ); copy_to_user ( buffer, 0, iobuf->data, len );
pxenv_udp_read->buffer_size = len; pxenv_udp_read->buffer_size = len;
/* Fill in source/dest information */ /* Fill in source/dest information */
assert ( meta );
sin_src = ( struct sockaddr_in * ) meta->src;
assert ( sin_src );
assert ( sin_src->sin_family == AF_INET ); assert ( sin_src->sin_family == AF_INET );
pxenv_udp_read->src_ip = sin_src->sin_addr.s_addr; pxenv_udp_read->src_ip = sin_src->sin_addr.s_addr;
pxenv_udp_read->s_port = sin_src->sin_port; pxenv_udp_read->s_port = sin_src->sin_port;
sin_dest = ( struct sockaddr_in * ) meta->dest;
assert ( sin_dest );
assert ( sin_dest->sin_family == AF_INET ); assert ( sin_dest->sin_family == AF_INET );
pxenv_udp_read->dest_ip = sin_dest->sin_addr.s_addr; pxenv_udp_read->dest_ip = sin_dest->sin_addr.s_addr;
pxenv_udp_read->d_port = sin_dest->sin_port; pxenv_udp_read->d_port = sin_dest->sin_port;
@ -117,18 +96,34 @@ static int pxe_udp_newdata ( struct udp_connection *conn, void *data,
/* Mark as received */ /* Mark as received */
pxe_udp->pxenv_udp_read = NULL; pxe_udp->pxenv_udp_read = NULL;
return 0; done:
free_iob ( iobuf );
return rc;
} }
/** PXE UDP operations */ /** PXE UDP data transfer interface operations */
static struct udp_operations pxe_udp_operations = { static struct xfer_interface_operations pxe_udp_xfer_operations = {
.senddata = pxe_udp_senddata, .close = ignore_xfer_close,
.newdata = pxe_udp_newdata, .vredirect = ignore_xfer_vredirect,
.request = ignore_xfer_request,
.seek = ignore_xfer_seek,
.alloc_iob = default_xfer_alloc_iob,
.deliver_iob = pxe_udp_deliver_iob,
.deliver_raw = xfer_deliver_as_iob,
}; };
/** The PXE UDP connection */ /** The PXE UDP connection */
static struct pxe_udp_connection pxe_udp = { static struct pxe_udp_connection pxe_udp = {
.udp.udp_op = &pxe_udp_operations, .xfer = {
.intf = {
.dest = &null_xfer.intf,
.refcnt = NULL,
},
.op = &pxe_udp_xfer_operations,
},
.local = {
.sin_family = AF_INET,
},
}; };
/** /**
@ -174,7 +169,6 @@ static struct pxe_udp_connection pxe_udp = {
* *
*/ */
PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) { PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
struct in_addr new_ip = { .s_addr = pxenv_udp_open->src_ip };
DBG ( "PXENV_UDP_OPEN" ); DBG ( "PXENV_UDP_OPEN" );
@ -184,17 +178,11 @@ PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
return PXENV_EXIT_FAILURE; return PXENV_EXIT_FAILURE;
} }
/* Set IP address if specified */ /* Record source IP address */
if ( new_ip.s_addr ) { pxe_udp.local.sin_addr.s_addr = pxenv_udp_open->src_ip;
/* FIXME: actually do something here */
DBG ( " with new IP address %s", inet_ntoa ( new_ip ) );
}
/* Open UDP connection */ /* Open promiscuous UDP connection */
if ( udp_open ( &pxe_udp.udp, 0 ) != 0 ) { udp_open_promisc ( &pxe_udp.xfer );
pxenv_udp_open->Status = PXENV_STATUS_OUT_OF_RESOURCES;
return PXENV_EXIT_FAILURE;
}
pxe_udp.open = 1; pxe_udp.open = 1;
pxenv_udp_open->Status = PXENV_STATUS_SUCCESS; pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
@ -232,7 +220,7 @@ PXENV_EXIT_t pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
} }
/* Close UDP connection */ /* Close UDP connection */
udp_close ( &pxe_udp.udp ); udp_close_promisc ( &pxe_udp.xfer );
pxe_udp.open = 0; pxe_udp.open = 0;
pxenv_udp_close->Status = PXENV_STATUS_SUCCESS; pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
@ -281,10 +269,14 @@ PXENV_EXIT_t pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
* *
*/ */
PXENV_EXIT_t pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) { PXENV_EXIT_t pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
union { struct sockaddr_in dest;
struct sockaddr_in sin; struct xfer_metadata meta = {
struct sockaddr_tcpip st; .src = ( struct sockaddr * ) &pxe_udp.local,
} dest; .dest = ( struct sockaddr * ) &dest,
};
size_t len;
struct io_buffer *iobuf;
userptr_t buffer;
int rc; int rc;
DBG ( "PXENV_UDP_WRITE" ); DBG ( "PXENV_UDP_WRITE" );
@ -297,36 +289,44 @@ PXENV_EXIT_t pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
/* Construct destination socket address */ /* Construct destination socket address */
memset ( &dest, 0, sizeof ( dest ) ); memset ( &dest, 0, sizeof ( dest ) );
dest.sin.sin_family = AF_INET; dest.sin_family = AF_INET;
dest.sin.sin_addr.s_addr = pxenv_udp_write->ip; dest.sin_addr.s_addr = pxenv_udp_write->ip;
dest.sin.sin_port = pxenv_udp_write->dst_port; dest.sin_port = pxenv_udp_write->dst_port;
udp_connect ( &pxe_udp.udp, &dest.st );
/* Set local (source) port. PXE spec says source port is 2069 /* Set local (source) port. PXE spec says source port is 2069
* if not specified. Really, this ought to be set at UDP open * if not specified. Really, this ought to be set at UDP open
* time but hey, we didn't design this API. * time but hey, we didn't design this API.
*/ */
if ( ! pxenv_udp_write->src_port ) pxe_udp.local.sin_port = pxenv_udp_write->src_port;
pxenv_udp_write->src_port = htons ( 2069 ); if ( ! pxe_udp.local.sin_port )
udp_bind ( &pxe_udp.udp, pxenv_udp_write->src_port ); pxe_udp.local.sin_port = htons ( 2069 );
/* FIXME: we ignore the gateway specified, since we're /* FIXME: we ignore the gateway specified, since we're
* confident of being able to do our own routing. We should * confident of being able to do our own routing. We should
* probably allow for multiple gateways. * probably allow for multiple gateways.
*/ */
/* Allocate and fill data buffer */
len = pxenv_udp_write->buffer_size;
iobuf = xfer_alloc_iob ( &pxe_udp.xfer, len );
if ( ! iobuf ) {
pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
return PXENV_EXIT_FAILURE;
}
buffer = real_to_user ( pxenv_udp_write->buffer.segment,
pxenv_udp_write->buffer.offset );
copy_from_user ( iob_put ( iobuf, len ), buffer, 0, len );
DBG ( " %04x:%04x+%x %d->%s:%d", pxenv_udp_write->buffer.segment, DBG ( " %04x:%04x+%x %d->%s:%d", pxenv_udp_write->buffer.segment,
pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size, pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
ntohs ( pxenv_udp_write->src_port ), ntohs ( pxenv_udp_write->src_port ),
inet_ntoa ( dest.sin.sin_addr ), inet_ntoa ( dest.sin_addr ),
ntohs ( pxenv_udp_write->dst_port ) ); ntohs ( pxenv_udp_write->dst_port ) );
/* Transmit packet */ /* Transmit packet */
pxe_udp.pxenv_udp_write = pxenv_udp_write; if ( ( rc = xfer_deliver_iob_meta ( &pxe_udp.xfer, iobuf,
rc = udp_senddata ( &pxe_udp.udp ); &meta ) ) != 0 ) {
pxe_udp.pxenv_udp_write = NULL; pxenv_udp_write->Status = PXENV_STATUS ( rc );
if ( rc != 0 ) {
pxenv_udp_write->Status = PXENV_STATUS_UNDI_TRANSMIT_ERROR;
return PXENV_EXIT_FAILURE; return PXENV_EXIT_FAILURE;
} }
@ -389,9 +389,6 @@ PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
return PXENV_EXIT_FAILURE; return PXENV_EXIT_FAILURE;
} }
/* Bind promiscuously; we will do our own filtering */
udp_bind_promisc ( &pxe_udp.udp );
/* Try receiving a packet */ /* Try receiving a packet */
pxe_udp.pxenv_udp_read = pxenv_udp_read; pxe_udp.pxenv_udp_read = pxenv_udp_read;
step(); step();

View File

@ -24,9 +24,11 @@
#include <byteswap.h> #include <byteswap.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <gpxe/async.h> #include <gpxe/refcnt.h>
#include <gpxe/tftp.h> #include <gpxe/xfer.h>
#include <gpxe/open.h>
#include <gpxe/uri.h> #include <gpxe/uri.h>
#include <gpxe/tftp.h>
/** @file /** @file
* *
@ -34,19 +36,224 @@
* *
*/ */
/** A TFTP option */ /**
struct tftp_option { * A TFTP request
/** Option name */ *
const char *name; * This data structure holds the state for an ongoing TFTP transfer.
/** Option processor */
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;
/** Data block size
*
* 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;
/** Request state
*
* This is the block number to be used in the next ACK sent
* back to the server, i.e. the number of the last received
* data block. The value zero indicates that the last
* received block was an OACK (i.e. that the next ACK will
* contain a block number of zero), and any value less than
* zero indicates that the connection has not yet been opened
* (i.e. that no blocks have yet been received).
*/
int state;
/** Peer address
*
* The peer address is determined by the first response
* received to the TFTP RRQ.
*/
struct sockaddr_tcpip peer;
/** Retransmission timer */
struct retry_timer timer;
};
/**
* Free TFTP request
*
* @v refcnt Reference counter
*/
static void tftp_free ( struct refcnt *refcnt ) {
struct tftp_request *tftp =
container_of ( refcnt, struct tftp_request, refcnt );
uri_put ( tftp->uri );
free ( tftp );
}
/**
* Mark TFTP request as complete
*
* @v tftp TFTP connection
* @v rc Return status code
*/
static void tftp_done ( struct tftp_request *tftp, int rc ) {
/* Stop the retry timer */
stop_timer ( &tftp->timer );
/* Close all data transfer interfaces */
xfer_nullify ( &tftp->socket );
xfer_close ( &tftp->socket, rc );
xfer_nullify ( &tftp->xfer );
xfer_close ( &tftp->xfer, rc );
}
/**
* TFTP requested blocksize
*
* This is treated as a global configuration parameter.
*/
static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
/**
* 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
* *
* @v tftp TFTP connection * @v tftp TFTP connection
* @v value Option value
* @ret rc Return status code * @ret rc Return status code
*/ */
int ( * process ) ( struct tftp_session *tftp, const char *value ); 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 */
+ 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */ );
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,
snprintf ( iobuf->data, iob_tailroom ( iobuf ),
"%s%coctet%cblksize%c%d%ctsize%c0", path, 0,
0, 0, tftp_request_blksize, 0, 0 ) + 1 );
/* RRQ always goes to the address specified in the initial
* xfer_open() call
*/
return xfer_deliver_iob ( &tftp->socket, iobuf );
}
/**
* Transmit ACK
*
* @v tftp TFTP connection
* @ret rc Return status code
*/
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,
}; };
/* Allocate buffer */
iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
if ( ! iobuf )
return -ENOMEM;
/* Build ACK */
ack = iob_put ( iobuf, sizeof ( *ack ) );
ack->opcode = htons ( TFTP_ACK );
ack->block = htons ( tftp->state );
/* ACK always goes to the peer recorded from the RRQ response */
return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
}
/**
* Transmit data
*
* @v tftp TFTP connection
* @ret rc Return status code
*/
static int tftp_send_packet ( struct tftp_request *tftp ) {
/* Start retransmission timer */
start_timer ( &tftp->timer );
/* Send RRQ or ACK as appropriate */
if ( tftp->state < 0 ) {
return tftp_send_rrq ( tftp );
} else {
return tftp_send_ack ( tftp );
}
}
/**
* Handle TFTP retransmission timer expiry
*
* @v timer Retry timer
* @v fail Failure indicator
*/
static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
struct tftp_request *tftp =
container_of ( timer, struct tftp_request, timer );
if ( fail ) {
tftp_done ( tftp, -ETIMEDOUT );
} else {
tftp_send_packet ( tftp );
}
}
/**
* Mark TFTP block as received
*
* @v tftp TFTP connection
* @v block Block number
*/
static void tftp_received ( struct tftp_request *tftp, unsigned int block ) {
/* Stop the retry timer */
stop_timer ( &tftp->timer );
/* Update state to indicate which block we're now waiting for */
tftp->state = block;
/* Send next packet */
tftp_send_packet ( tftp );
}
/** /**
* Process TFTP "blksize" option * Process TFTP "blksize" option
* *
@ -54,7 +261,7 @@ struct tftp_option {
* @v value Option value * @v value Option value
* @ret rc Return status code * @ret rc Return status code
*/ */
static int tftp_process_blksize ( struct tftp_session *tftp, static int tftp_process_blksize ( struct tftp_request *tftp,
const char *value ) { const char *value ) {
char *end; char *end;
@ -76,7 +283,7 @@ static int tftp_process_blksize ( struct tftp_session *tftp,
* @v value Option value * @v value Option value
* @ret rc Return status code * @ret rc Return status code
*/ */
static int tftp_process_tsize ( struct tftp_session *tftp, static int tftp_process_tsize ( struct tftp_request *tftp,
const char *value ) { const char *value ) {
char *end; char *end;
@ -88,9 +295,26 @@ static int tftp_process_tsize ( struct tftp_session *tftp,
} }
DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize ); DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
/* Notify recipient of file size */
xfer_seek ( &tftp->xfer, tftp->tsize, SEEK_SET );
xfer_seek ( &tftp->xfer, 0, SEEK_SET );
return 0; 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 */ /** Recognised TFTP options */
static struct tftp_option tftp_options[] = { static struct tftp_option tftp_options[] = {
{ "blksize", tftp_process_blksize }, { "blksize", tftp_process_blksize },
@ -106,7 +330,7 @@ static struct tftp_option tftp_options[] = {
* @v value Option value * @v value Option value
* @ret rc Return status code * @ret rc Return status code
*/ */
static int tftp_process_option ( struct tftp_session *tftp, static int tftp_process_option ( struct tftp_request *tftp,
const char *name, const char *value ) { const char *name, const char *value ) {
struct tftp_option *option; struct tftp_option *option;
@ -121,110 +345,6 @@ static int tftp_process_option ( struct tftp_session *tftp,
return -EINVAL; return -EINVAL;
} }
/** 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,
};
/**
* Mark TFTP session as complete
*
* @v tftp TFTP connection
* @v rc Return status code
*/
static void tftp_done ( struct tftp_session *tftp, int rc ) {
/* Stop the retry timer */
stop_timer ( &tftp->timer );
/* Close UDP connection */
udp_close ( &tftp->udp );
/* Mark async operation as complete */
async_done ( &tftp->async, rc );
}
/**
* Send next packet in TFTP session
*
* @v tftp TFTP connection
*/
static void tftp_send_packet ( struct tftp_session *tftp ) {
start_timer ( &tftp->timer );
udp_senddata ( &tftp->udp );
}
/**
* Handle TFTP retransmission timer expiry
*
* @v timer Retry timer
* @v fail Failure indicator
*/
static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
struct tftp_session *tftp =
container_of ( timer, struct tftp_session, timer );
if ( fail ) {
tftp_done ( tftp, -ETIMEDOUT );
} else {
tftp_send_packet ( tftp );
}
}
/**
* Mark TFTP block as received
*
* @v tftp TFTP connection
* @v block Block number
*/
static void tftp_received ( struct tftp_session *tftp, unsigned int block ) {
/* Stop the retry timer */
stop_timer ( &tftp->timer );
/* Update state to indicate which block we're now waiting for */
tftp->state = block;
/* Send next packet */
tftp_send_packet ( tftp );
}
/**
* Transmit RRQ
*
* @v tftp TFTP connection
* @v buf Temporary data buffer
* @v len Length of temporary data buffer
* @ret rc Return status code
*/
static int tftp_send_rrq ( struct tftp_session *tftp, void *buf, size_t len ) {
struct tftp_rrq *rrq = buf;
void *data;
void *end;
DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, tftp->uri->path );
data = rrq->data;
end = ( buf + len );
if ( data > end )
goto overflow;
data += ( snprintf ( data, ( end - data ),
"%s%coctet%cblksize%c%d%ctsize%c0",
tftp->uri->path, 0, 0, 0,
tftp->request_blksize, 0, 0 ) + 1 );
if ( data > end )
goto overflow;
rrq->opcode = htons ( TFTP_RRQ );
return udp_send ( &tftp->udp, buf, ( data - buf ) );
overflow:
DBGC ( tftp, "TFTP %p RRQ out of space\n", tftp );
return -ENOBUFS;
}
/** /**
* Receive OACK * Receive OACK
* *
@ -233,7 +353,7 @@ static int tftp_send_rrq ( struct tftp_session *tftp, void *buf, size_t len ) {
* @v len Length of temporary data buffer * @v len Length of temporary data buffer
* @ret rc Return status code * @ret rc Return status code
*/ */
static int tftp_rx_oack ( struct tftp_session *tftp, void *buf, size_t len ) { static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
struct tftp_oack *oack = buf; struct tftp_oack *oack = buf;
char *end = buf + len; char *end = buf + len;
char *name; char *name;
@ -276,31 +396,34 @@ static int tftp_rx_oack ( struct tftp_session *tftp, void *buf, size_t len ) {
* Receive DATA * Receive DATA
* *
* @v tftp TFTP connection * @v tftp TFTP connection
* @v buf Temporary data buffer * @v iobuf I/O buffer
* @v len Length of temporary data buffer
* @ret rc Return status code * @ret rc Return status code
*
* Takes ownership of I/O buffer.
*/ */
static int tftp_rx_data ( struct tftp_session *tftp, void *buf, size_t len ) { static int tftp_rx_data ( struct tftp_request *tftp,
struct tftp_data *data = buf; struct io_buffer *iobuf ) {
struct tftp_data *data = iobuf->data;
unsigned int block; unsigned int block;
size_t data_offset;
size_t data_len; size_t data_len;
int rc; int rc;
/* Sanity check */ /* Sanity check */
if ( len < sizeof ( *data ) ) { if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
DBGC ( tftp, "TFTP %p received underlength DATA packet " DBGC ( tftp, "TFTP %p received underlength DATA packet "
"length %d\n", tftp, len ); "length %d\n", tftp, iob_len ( iobuf ) );
free_iob ( iobuf );
return -EINVAL; return -EINVAL;
} }
/* Fill data buffer */ /* Extract data */
block = ntohs ( data->block ); block = ntohs ( data->block );
data_offset = ( ( block - 1 ) * tftp->blksize ); iob_pull ( iobuf, sizeof ( *data ) );
data_len = ( len - offsetof ( typeof ( *data ), data ) ); data_len = iob_len ( iobuf );
if ( ( rc = fill_buffer ( tftp->buffer, data->data, data_offset,
data_len ) ) != 0 ) { /* Deliver data */
DBGC ( tftp, "TFTP %p could not fill data buffer: %s\n", if ( ( rc = xfer_deliver_iob ( &tftp->xfer, iobuf ) ) != 0 ) {
DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
tftp, strerror ( rc ) ); tftp, strerror ( rc ) );
tftp_done ( tftp, rc ); tftp_done ( tftp, rc );
return rc; return rc;
@ -316,21 +439,12 @@ static int tftp_rx_data ( struct tftp_session *tftp, void *buf, size_t len ) {
return 0; return 0;
} }
/** /** Translation between TFTP errors and internal error numbers */
* Transmit ACK static const uint8_t tftp_errors[] = {
* [TFTP_ERR_FILE_NOT_FOUND] = PXENV_STATUS_TFTP_FILE_NOT_FOUND,
* @v tftp TFTP connection [TFTP_ERR_ACCESS_DENIED] = PXENV_STATUS_TFTP_ACCESS_VIOLATION,
* @v buf Temporary data buffer [TFTP_ERR_ILLEGAL_OP] = PXENV_STATUS_TFTP_UNKNOWN_OPCODE,
* @v len Length of temporary data buffer };
* @ret rc Return status code
*/
static int tftp_send_ack ( struct tftp_session *tftp ) {
struct tftp_ack ack;
ack.opcode = htons ( TFTP_ACK );
ack.block = htons ( tftp->state );
return udp_send ( &tftp->udp, &ack, sizeof ( ack ) );
}
/** /**
* Receive ERROR * Receive ERROR
@ -340,7 +454,7 @@ static int tftp_send_ack ( struct tftp_session *tftp ) {
* @v len Length of temporary data buffer * @v len Length of temporary data buffer
* @ret rc Return status code * @ret rc Return status code
*/ */
static int tftp_rx_error ( struct tftp_session *tftp, void *buf, size_t len ) { static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
struct tftp_error *error = buf; struct tftp_error *error = buf;
unsigned int err; unsigned int err;
int rc = 0; int rc = 0;
@ -362,32 +476,12 @@ static int tftp_rx_error ( struct tftp_session *tftp, void *buf, size_t len ) {
if ( ! rc ) if ( ! rc )
rc = -PXENV_STATUS_TFTP_CANNOT_OPEN_CONNECTION; rc = -PXENV_STATUS_TFTP_CANNOT_OPEN_CONNECTION;
/* Close TFTP session */ /* Close TFTP request */
tftp_done ( tftp, rc ); tftp_done ( tftp, rc );
return 0; return 0;
} }
/**
* Transmit data
*
* @v conn UDP connection
* @v buf Temporary data buffer
* @v len Length of temporary data buffer
* @ret rc Return status code
*/
static int tftp_senddata ( struct udp_connection *conn,
void *buf, size_t len ) {
struct tftp_session *tftp =
container_of ( conn, struct tftp_session, udp );
if ( tftp->state < 0 ) {
return tftp_send_rrq ( tftp, buf, len );
} else {
return tftp_send_ack ( tftp );
}
}
/** /**
* Receive new data * Receive new data
* *
@ -397,141 +491,179 @@ static int tftp_senddata ( struct udp_connection *conn,
* @v st_src Partially-filled source address * @v st_src Partially-filled source address
* @v st_dest Partially-filled destination address * @v st_dest Partially-filled destination address
*/ */
static int tftp_newdata ( struct udp_connection *conn, void *data, size_t len, static int tftp_socket_deliver_iob ( struct xfer_interface *socket,
struct sockaddr_tcpip *st_src __unused, struct io_buffer *iobuf,
struct sockaddr_tcpip *st_dest __unused ) { struct xfer_metadata *meta ) {
struct tftp_session *tftp = struct tftp_request *tftp =
container_of ( conn, struct tftp_session, udp ); container_of ( socket, struct tftp_request, socket );
struct tftp_common *common = data; struct sockaddr_tcpip *st_src;
struct tftp_common *common = iobuf->data;
size_t len = iob_len ( iobuf );
int rc = -EINVAL;
/* Sanity checks */
if ( len < sizeof ( *common ) ) { if ( len < sizeof ( *common ) ) {
DBGC ( tftp, "TFTP %p received underlength packet length %d\n", DBGC ( tftp, "TFTP %p received underlength packet length %d\n",
tftp, len ); tftp, len );
return -EINVAL; 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;
} }
/* Filter by TID. Set TID on first response received */ /* Filter by TID. Set TID on first response received */
if ( tftp->tid ) { st_src = ( struct sockaddr_tcpip * ) meta->src;
if ( tftp->tid != st_src->st_port ) { if ( tftp->state < 0 ) {
DBGC ( tftp, "TFTP %p received packet from wrong port " memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
"(got %d, wanted %d)\n", tftp,
ntohs ( st_src->st_port ), ntohs ( tftp->tid ));
return -EINVAL;
}
} else {
tftp->tid = st_src->st_port;
DBGC ( tftp, "TFTP %p using remote port %d\n", tftp, DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
ntohs ( tftp->tid ) ); ntohs ( tftp->peer.st_port ) );
udp_connect_port ( &tftp->udp, tftp->tid ); } else if ( memcmp ( &tftp->peer, st_src,
} sizeof ( tftp->peer ) ) != 0 ) {
DBGC ( tftp, "TFTP %p received packet from wrong source (got "
/* Filter by source address */ "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
if ( memcmp ( st_src, udp_peer ( &tftp->udp ), ntohs ( tftp->peer.st_port ) );
sizeof ( *st_src ) ) != 0 ) { goto done;
DBGC ( tftp, "TFTP %p received packet from foreign source\n",
tftp );
return -EINVAL;
} }
switch ( common->opcode ) { switch ( common->opcode ) {
case htons ( TFTP_OACK ): case htons ( TFTP_OACK ):
return tftp_rx_oack ( tftp, data, len ); rc = tftp_rx_oack ( tftp, iobuf->data, len );
break;
case htons ( TFTP_DATA ): case htons ( TFTP_DATA ):
return tftp_rx_data ( tftp, data, len ); rc = tftp_rx_data ( tftp, iobuf );
iobuf = NULL;
break;
case htons ( TFTP_ERROR ): case htons ( TFTP_ERROR ):
return tftp_rx_error ( tftp, data, len ); rc = tftp_rx_error ( tftp, iobuf->data, len );
break;
default: default:
DBGC ( tftp, "TFTP %p received strange packet type %d\n", tftp, DBGC ( tftp, "TFTP %p received strange packet type %d\n",
ntohs ( common->opcode ) ); tftp, ntohs ( common->opcode ) );
return -EINVAL; break;
}; };
done:
free_iob ( iobuf );
return rc;
} }
/** TFTP UDP operations */ /**
static struct udp_operations tftp_udp_operations = { * TFTP connection closed by network stack
.senddata = tftp_senddata, *
.newdata = tftp_newdata, * @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 ) );
tftp_done ( tftp, rc );
}
/** TFTP socket operations */
static struct xfer_interface_operations tftp_socket_operations = {
.close = tftp_socket_close,
.vredirect = xfer_vopen,
.request = ignore_xfer_request,
.seek = ignore_xfer_seek,
.alloc_iob = default_xfer_alloc_iob,
.deliver_iob = tftp_socket_deliver_iob,
.deliver_raw = xfer_deliver_as_iob,
}; };
/** /**
* Reap asynchronous operation * Close TFTP data transfer interface
* *
* @v async Asynchronous operation * @v xfer Data transfer interface
* @v rc Reason for close
*/ */
static void tftp_reap ( struct async *async ) { static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
struct tftp_session *tftp = struct tftp_request *tftp =
container_of ( async, struct tftp_session, async ); container_of ( xfer, struct tftp_request, xfer );
free ( tftp ); DBGC ( tftp, "TFTP %p interface closed: %s\n",
tftp, strerror ( rc ) );
tftp_done ( tftp, rc );
} }
/** TFTP asynchronous operations */ /** TFTP data transfer interface operations */
static struct async_operations tftp_async_operations = { static struct xfer_interface_operations tftp_xfer_operations = {
.reap = tftp_reap, .close = tftp_xfer_close,
.vredirect = ignore_xfer_vredirect,
.request = ignore_xfer_request,
.seek = ignore_xfer_seek,
.alloc_iob = default_xfer_alloc_iob,
.deliver_iob = xfer_deliver_as_raw,
.deliver_raw = ignore_xfer_deliver_raw,
}; };
/** /**
* Initiate TFTP download * Initiate TFTP download
* *
* @v xfer Data transfer interface
* @v uri Uniform Resource Identifier * @v uri Uniform Resource Identifier
* @v buffer Buffer into which to download file
* @v parent Parent asynchronous operation
* @ret rc Return status code * @ret rc Return status code
*/ */
int tftp_get ( struct uri *uri, struct buffer *buffer, struct async *parent ) { int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
struct tftp_session *tftp = NULL; struct tftp_request *tftp;
struct sockaddr_tcpip server;
int rc; int rc;
/* Sanity checks */ /* Sanity checks */
if ( ! uri->path ) { if ( ! uri->host )
rc = -EINVAL; return -EINVAL;
goto err; if ( ! uri->path )
} return -EINVAL;
/* Allocate and populate TFTP structure */ /* Allocate and populate TFTP structure */
tftp = malloc ( sizeof ( *tftp ) ); tftp = malloc ( sizeof ( *tftp ) );
if ( ! tftp ) { if ( ! tftp )
rc = -ENOMEM; return -ENOMEM;
goto err;
}
memset ( tftp, 0, sizeof ( *tftp ) ); memset ( tftp, 0, sizeof ( *tftp ) );
tftp->uri = uri; tftp->refcnt.free = tftp_free;
tftp->buffer = buffer; xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
if ( ! tftp->request_blksize ) tftp->uri = uri_get ( uri );
tftp->request_blksize = TFTP_MAX_BLKSIZE; xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
tftp->blksize = TFTP_DEFAULT_BLKSIZE;
tftp->state = -1; tftp->state = -1;
tftp->udp.udp_op = &tftp_udp_operations;
tftp->timer.expired = tftp_timer_expired; tftp->timer.expired = tftp_timer_expired;
/* Open socket */
#warning "Quick name resolution hack" memset ( &server, 0, sizeof ( server ) );
union { server.st_port = htons ( uri_port ( tftp->uri, TFTP_PORT ) );
struct sockaddr_tcpip st; if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
struct sockaddr_in sin; ( struct sockaddr * ) &server,
} server; uri->host, NULL ) ) != 0 )
server.sin.sin_port = htons ( TFTP_PORT );
server.sin.sin_family = AF_INET;
if ( inet_aton ( uri->host, &server.sin.sin_addr ) == 0 ) {
rc = -EINVAL;
goto err;
}
udp_connect ( &tftp->udp, &server.st );
/* Open UDP connection */
if ( ( rc = udp_open ( &tftp->udp, 0 ) ) != 0 )
goto err; goto err;
/* Transmit initial RRQ */ /* Start timer to initiate RRQ */
tftp_send_packet ( tftp ); start_timer ( &tftp->timer );
async_init ( &tftp->async, &tftp_async_operations, parent ); /* Attach to parent interface, mortalise self, and return */
xfer_plug_plug ( &tftp->xfer, xfer );
ref_put ( &tftp->refcnt );
return 0; return 0;
err: err:
DBGC ( tftp, "TFTP %p could not create session: %s\n", DBGC ( tftp, "TFTP %p could not create request: %s\n",
tftp, strerror ( rc ) ); tftp, strerror ( rc ) );
free ( tftp ); tftp_done ( tftp, rc );
ref_put ( &tftp->refcnt );
return rc; return rc;
} }
/** TFTP URI opener */
struct uri_opener tftp_uri_opener __uri_opener = {
.scheme = "tftp",
.open = tftp_open,
};