123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
-
-
- #include <stdint.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
- #include <byteswap.h>
- #include <errno.h>
- #include <assert.h>
- #include <gpxe/refcnt.h>
- #include <gpxe/xfer.h>
- #include <gpxe/open.h>
- #include <gpxe/uri.h>
- #include <gpxe/tcpip.h>
- #include <gpxe/retry.h>
- #include <gpxe/tftp.h>
-
-
-
-
- struct tftp_request {
-
- struct refcnt refcnt;
-
- struct xfer_interface xfer;
-
-
- struct uri *uri;
-
- struct xfer_interface socket;
-
-
-
- unsigned int blksize;
-
-
- unsigned long tsize;
-
-
-
- int state;
-
-
- struct sockaddr_tcpip peer;
-
- struct retry_timer timer;
- };
-
-
- static void tftp_free ( struct refcnt *refcnt ) {
- struct tftp_request *tftp =
- container_of ( refcnt, struct tftp_request, refcnt );
-
- uri_put ( tftp->uri );
- free ( tftp );
- }
-
-
- static void tftp_done ( struct tftp_request *tftp, int rc ) {
-
- DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
- tftp, rc, strerror ( rc ) );
-
-
- stop_timer ( &tftp->timer );
-
-
- xfer_nullify ( &tftp->socket );
- xfer_close ( &tftp->socket, rc );
- xfer_nullify ( &tftp->xfer );
- xfer_close ( &tftp->xfer, rc );
- }
-
-
- static unsigned int tftp_request_blksize = TFTP_MAX_BLKSIZE;
-
-
- void tftp_set_request_blksize ( unsigned int blksize ) {
- if ( blksize < TFTP_DEFAULT_BLKSIZE )
- blksize = TFTP_DEFAULT_BLKSIZE;
- tftp_request_blksize = blksize;
- }
-
-
- 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
- + 5 + 1
- + 7 + 1 + 5 + 1
- + 5 + 1 + 1 + 1 );
- struct io_buffer *iobuf;
-
- DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
-
-
- iobuf = xfer_alloc_iob ( &tftp->socket, len );
- if ( ! iobuf )
- return -ENOMEM;
-
-
- rrq = iob_put ( iobuf, sizeof ( *rrq ) );
- rrq->opcode = htons ( TFTP_RRQ );
- iob_put ( iobuf,
- snprintf ( rrq->data, iob_tailroom ( iobuf ),
- "%s%coctet%cblksize%c%d%ctsize%c0", path, 0,
- 0, 0, tftp_request_blksize, 0, 0 ) + 1 );
-
-
-
- return xfer_deliver_iob ( &tftp->socket, iobuf );
- }
-
-
- 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,
- };
-
- DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n",
- tftp, tftp->state );
-
-
- iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
- if ( ! iobuf )
- return -ENOMEM;
-
-
- ack = iob_put ( iobuf, sizeof ( *ack ) );
- ack->opcode = htons ( TFTP_ACK );
- ack->block = htons ( tftp->state );
-
-
- return xfer_deliver_iob_meta ( &tftp->socket, iobuf, &meta );
- }
-
-
- static int tftp_send_packet ( struct tftp_request *tftp ) {
-
-
- start_timer ( &tftp->timer );
-
-
- if ( tftp->state < 0 ) {
- return tftp_send_rrq ( tftp );
- } else {
- return tftp_send_ack ( tftp );
- }
- }
-
-
- 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 );
- }
- }
-
-
- static void tftp_received ( struct tftp_request *tftp, unsigned int block ) {
-
-
- stop_timer ( &tftp->timer );
-
-
- tftp->state = block;
-
-
- tftp_send_packet ( tftp );
- }
-
-
- 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;
- }
-
-
- static int tftp_process_tsize ( struct tftp_request *tftp,
- const char *value ) {
- char *end;
-
- 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 );
-
-
- xfer_seek ( &tftp->xfer, tftp->tsize, SEEK_SET );
- xfer_seek ( &tftp->xfer, 0, SEEK_SET );
-
- return 0;
- }
-
-
- struct tftp_option {
-
- const char *name;
-
-
- int ( * process ) ( struct tftp_request *tftp, const char *value );
- };
-
-
- static struct tftp_option tftp_options[] = {
- { "blksize", tftp_process_blksize },
- { "tsize", tftp_process_tsize },
- { NULL, NULL }
- };
-
-
- 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 );
-
- return -EINVAL;
- }
-
-
- static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
- struct tftp_oack *oack = buf;
- char *end = buf + len;
- char *name;
- char *value;
- int rc;
-
-
- if ( len < sizeof ( *oack ) ) {
- DBGC ( tftp, "TFTP %p received underlength OACK packet "
- "length %d\n", tftp, len );
- return -EINVAL;
- }
- if ( end[-1] != '\0' ) {
- DBGC ( tftp, "TFTP %p received OACK missing final NUL\n",
- tftp );
- return -EINVAL;
- }
-
-
- name = oack->data;
- while ( name < end ) {
- value = ( name + strlen ( name ) + 1 );
- if ( value == end ) {
- DBGC ( tftp, "TFTP %p received OACK missing value "
- "for option \"%s\"\n", tftp, name );
- return -EINVAL;
- }
- if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
- return rc;
- name = ( value + strlen ( value ) + 1 );
- }
-
-
- tftp_received ( tftp, 0 );
-
- return 0;
- }
-
-
- static int tftp_rx_data ( struct tftp_request *tftp,
- struct io_buffer *iobuf ) {
- struct tftp_data *data = iobuf->data;
- unsigned int block;
- size_t data_len;
- int rc;
-
-
- if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
- DBGC ( tftp, "TFTP %p received underlength DATA packet "
- "length %d\n", tftp, iob_len ( iobuf ) );
- free_iob ( iobuf );
- return -EINVAL;
- }
-
-
- block = ntohs ( data->block );
- iob_pull ( iobuf, sizeof ( *data ) );
- data_len = iob_len ( iobuf );
-
-
- if ( ( rc = xfer_deliver_iob ( &tftp->xfer, iobuf ) ) != 0 ) {
- DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
- tftp, strerror ( rc ) );
- tftp_done ( tftp, rc );
- return rc;
- }
-
-
- tftp_received ( tftp, block );
-
-
- if ( data_len < tftp->blksize )
- tftp_done ( tftp, 0 );
-
- return 0;
- }
-
-
- 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,
- };
-
-
- static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
- struct tftp_error *error = buf;
- unsigned int err;
- int rc = 0;
-
-
- if ( len < sizeof ( *error ) ) {
- DBGC ( tftp, "TFTP %p received underlength ERROR packet "
- "length %d\n", tftp, len );
- return -EINVAL;
- }
-
- DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
- "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
-
-
- 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;
-
-
- tftp_done ( tftp, rc );
-
- return 0;
- }
-
-
- 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 );
- struct sockaddr_tcpip *st_src;
- struct tftp_common *common = iobuf->data;
- size_t len = iob_len ( iobuf );
- int rc = -EINVAL;
-
-
- if ( len < sizeof ( *common ) ) {
- DBGC ( tftp, "TFTP %p received underlength packet length %d\n",
- tftp, len );
- 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;
- }
-
-
- st_src = ( struct sockaddr_tcpip * ) meta->src;
- if ( tftp->state < 0 ) {
- memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
- DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
- 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;
- }
-
- switch ( common->opcode ) {
- case htons ( TFTP_OACK ):
- rc = tftp_rx_oack ( tftp, iobuf->data, len );
- break;
- case htons ( TFTP_DATA ):
- rc = tftp_rx_data ( tftp, iobuf );
- iobuf = NULL;
- break;
- case htons ( TFTP_ERROR ):
- rc = tftp_rx_error ( tftp, iobuf->data, len );
- break;
- default:
- DBGC ( tftp, "TFTP %p received strange packet type %d\n",
- tftp, ntohs ( common->opcode ) );
- break;
- };
-
- done:
- free_iob ( iobuf );
- return rc;
- }
-
-
- 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 );
- }
-
-
- 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,
- };
-
-
- static void tftp_xfer_close ( struct xfer_interface *xfer, int rc ) {
- struct tftp_request *tftp =
- container_of ( xfer, struct tftp_request, xfer );
-
- DBGC ( tftp, "TFTP %p interface closed: %s\n",
- tftp, strerror ( rc ) );
-
- tftp_done ( tftp, rc );
- }
-
-
- static struct xfer_interface_operations tftp_xfer_operations = {
- .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,
- };
-
-
- int tftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
- struct tftp_request *tftp;
- struct sockaddr_tcpip server;
- int rc;
-
-
- if ( ! uri->host )
- return -EINVAL;
- if ( ! uri->path )
- return -EINVAL;
-
-
- tftp = zalloc ( sizeof ( *tftp ) );
- 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 );
- tftp->state = -1;
- tftp->timer.expired = tftp_timer_expired;
-
-
- 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 )
- goto err;
-
-
- start_timer ( &tftp->timer );
-
-
- xfer_plug_plug ( &tftp->xfer, xfer );
- ref_put ( &tftp->refcnt );
- return 0;
-
- err:
- DBGC ( tftp, "TFTP %p could not create request: %s\n",
- tftp, strerror ( rc ) );
- tftp_done ( tftp, rc );
- ref_put ( &tftp->refcnt );
- return rc;
- }
-
-
- struct uri_opener tftp_uri_opener __uri_opener = {
- .scheme = "tftp",
- .open = tftp_open,
- };
|