/* $NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner Exp $ */
/*
* Copyright (c) 1996
* Matthias Drochner. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project
* by Matthias Drochner.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
__FBSDID("$FreeBSD$");
/*
* Simple TFTP implementation for libsa.
* Assumes:
* - socket descriptor (int) at open_file->f_devdata
* - server host IP in global servip
* Restrictions:
* - read only
* - lseek only with SEEK_SET or SEEK_CUR
* - no big time differences between transfers (<tftp timeout)
*/
#include <netinet/in_systm.h>
#include <string.h>
#include "stand.h"
#include "net.h"
#include "netif.h"
#include "tftp.h"
struct tftp_handle;
static int tftp_close(struct open_file *f);
"tftp",
};
static int is_open = 0;
/*
* The legacy TFTP_BLKSIZE value was SEGSIZE(512).
* TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and
* IP header lengths).
*/
/*
* Choose a blksize big enough so we can test with Ethernet
* Jumbo frames in the future.
*/
struct tftp_handle {
int validsize;
int off;
unsigned int tftp_blksize;
unsigned long tftp_tsize;
struct {
struct tftphdr t;
};
0, /* ??? */
EINVAL, /* ??? */
EINVAL, /* ??? */
EINVAL, /* ??? */
EINVAL, /* Option negotiation failed. */
};
static int tftp_getnextblock(struct tftp_handle *h);
/* send error message back. */
static void
{
struct {
struct tftphdr t;
char *wtail;
int len;
}
static void
{
struct {
struct tftphdr t;
char *wtail;
wtail += 2;
}
static ssize_t
unsigned short *rtype)
{
struct tftphdr *t;
errno = 0;
if (len < 4)
return (-1);
case DATA: {
int got;
/*
* Expected block?
*/
return (-1);
}
if (d->xid == 1) {
/*
* First data packet from new port.
*/
} /* else check uh_sport has not changed??? */
return got;
}
case ERROR:
} else {
#ifdef TFTP_DEBUG
#endif
}
return (-1);
case OACK: {
int tftp_oack_len;
/*
* Unexpected OACK. TFTP transfer already in progress.
* Drop the pkt.
*/
if (d->xid != 1) {
return (-1);
}
/*
* Remember which port this OACK came from, because we need
* to send the ACK or errors back to it.
*/
/* Parse options ACK-ed by the server. */
return (-1);
}
return (0);
}
default:
#ifdef TFTP_DEBUG
#endif
return (-1);
}
}
/* send request, expect first block (or error) */
static int
{
struct {
struct tftphdr t;
char *wtail;
int l;
struct tftphdr *t;
int blksize_l;
unsigned short rtype = 0;
/*
* Allow overriding default TFTP block size by setting
* a tftp.blksize environment variable.
*/
}
#ifdef TFTP_PREPEND_PATH
return (ENAMETOOLONG);
#else
if (l > FNAME_SIZE)
return (ENAMETOOLONG);
#endif
wtail += l + 1;
wtail += 6;
wtail += 8;
wtail += 6;
wtail += 2;
t = &h->lastdata.t;
/* h->iodesc->myport = htons(--tftpport); */
h->currblock = 0;
h->islastblock = 0;
h->validsize = 0;
return (tftp_getnextblock(h));
/* Server ignored our blksize request, revert to TFTP default. */
h->tftp_blksize = SEGSIZE;
switch (rtype) {
case DATA: {
h->currblock = 1;
h->islastblock = 0;
if (res < h->tftp_blksize) {
tftp_sendack(h);
}
return (0);
}
case ERROR:
default:
return (errno);
}
}
/* ack block, expect next */
static int
{
struct {
struct tftphdr t;
char *wtail;
int res;
struct tftphdr *t;
unsigned short rtype = 0;
wtail += 2;
t = &h->lastdata.t;
return (errno);
h->currblock++;
if (res < h->tftp_blksize)
if (h->islastblock == 1) {
/* Send an ACK for the last block */
}
return (0);
}
static int
{
int res;
const char *extraslash;
#ifdef __i386__
return (EINVAL);
#else
return (EINVAL);
#endif
}
if (is_open)
return (EBUSY);
if (!tftpfile)
return (ENOMEM);
return (EINVAL);
return(ENOMEM);
}
extraslash = "";
else
extraslash = "/";
return(ENOMEM);
}
if (res) {
return (res);
}
is_open = 1;
return (0);
}
static int
{
while (size > 0) {
twiddle(32);
* for open */
}
int res;
if (res) { /* no answer */
#ifdef TFTP_DEBUG
printf("tftp: read error\n");
#endif
return (res);
}
if (tftpfile->islastblock)
break;
}
if (inbuffer < 0) {
#ifdef TFTP_DEBUG
printf("tftp: invalid offset %d\n",
#endif
return (EINVAL);
}
break; /* EOF */
} else {
#ifdef TFTP_DEBUG
#endif
return (EINVAL);
}
}
if (resid)
return (0);
}
static int
{
/* let it time out ... */
if (tftpfile) {
}
is_open = 0;
return (0);
}
static int
{
return (EROFS);
}
static int
{
return (0);
}
static off_t
{
switch (where) {
case SEEK_SET:
break;
case SEEK_CUR:
break;
default:
return (-1);
}
}
static ssize_t
{
#ifdef TFTP_DEBUG
if (debug)
printf("sendrecv: called\n");
#endif
for (;;) {
return -1;
}
panic("sendrecv: short write! (%zd < %zu)",
if (cc == -1) {
/* Error on transmit; wait before retrying */
continue;
}
/* Try to get a packet and process it. */
/* Return on data, EOF or real error. */
return (cc);
goto recvnext;
}
/* Timed out or didn't get the packet we're waiting for */
}
}
}
static int
{
char *endptr;
int new_blksize;
int ret = 0;
return (ret);
/*
* Only accept blksize value if it is numeric.
* RFC2348 specifies that acceptable values are 8-65464.
* Let's choose a limit less than MAXRSPACE.
*/
&& new_blksize <= TFTP_MAX_BLKSIZE) {
h->tftp_blksize = new_blksize;
ret = 1;
}
return (ret);
}
/*
* In RFC2347, the TFTP Option Acknowledgement package (OACK)
* is used to acknowledge a client's option negotiation request.
* The format of an OACK packet is:
* +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
* | opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 |
* +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
*
* opc
* The opcode field contains a 6, for Option Acknowledgment.
*
* opt1
* The first option acknowledgment, copied from the original
* request.
*
* value1
* The acknowledged value associated with the first option. If
* and how this value may differ from the original request is
* detailed in the specification for the option.
*
* optN, valueN
*/
static int
{
/*
* We parse the OACK strings into an array
* of name-value pairs.
*/
int i = 0;
int option_idx = 0;
int blksize_is_set = 0;
int tsize = 0;
unsigned int orig_blksize;
if (buf[i] == '\0') {
++option_idx;
}
}
++i;
}
/* Save the block size we requested for sanity check later. */
orig_blksize = h->tftp_blksize;
/*
* Parse individual TFTP options.
* * "blksize" is specified in RFC2348.
* * "tsize" is specified in RFC2349.
*/
for (i = 0; i < option_idx; i += 2) {
if (i + 1 < option_idx)
if (i + 1 < option_idx)
if (tsize != 0)
h->tftp_tsize = tsize;
} else {
/* Do not allow any options we did not expect to be ACKed. */
return (-1);
}
}
if (!blksize_is_set) {
/*
* If TFTP blksize was not set, try defaulting
* to the legacy TFTP blksize of SEGSIZE(512)
*/
h->tftp_blksize = SEGSIZE;
} else if (h->tftp_blksize > orig_blksize) {
/*
* Server should not be proposing block sizes that
* exceed what we said we can handle.
*/
return (-1);
}
#ifdef TFTP_DEBUG
#endif
return 0;
}