199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1996
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Matthias Drochner. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3. All advertising materials mentioning features or use of this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * must display the following acknowledgement:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This product includes software developed for the NetBSD Project
199767f8919635c4928607450d9e0abb932109ceToomas Soome * by Matthias Drochner.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. The name of the author may not be used to endorse or promote products
199767f8919635c4928607450d9e0abb932109ceToomas Soome * derived from this software without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Simple TFTP implementation for libsa.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - socket descriptor (int) at open_file->f_devdata
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - server host IP in global servip
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Restrictions:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - read only
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - lseek only with SEEK_SET or SEEK_CUR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - no big time differences between transfers (<tftp timeout)
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_open(const char *path, struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_write(struct open_file *f, void *buf, size_t size, size_t *resid);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic off_t tftp_seek(struct open_file *f, off_t offset, int where);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_set_blksize(struct tftp_handle *h, const char *str);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_stat(struct open_file *f, struct stat *sb);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ssize_t sendrecv_tftp(struct tftp_handle *h,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t (*sproc)(struct iodesc *, void *, size_t),
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t (*rproc)(struct tftp_handle *h, void *, ssize_t, time_t, unsigned short *),
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *rbuf, size_t rsize, unsigned short *rtype);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The legacy TFTP_BLKSIZE value was SEGSIZE(512).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IP header lengths).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Choose a blksize big enough so we can test with Ethernet
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Jumbo frames in the future.
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const int tftperrors[TFTP_MAX_ERRCODE + 1] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0, /* ??? */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_getnextblock(struct tftp_handle *h);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* send error message back. */
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_block = htons((u_short) h->currblock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomerecvtftp(struct tftp_handle *h, void *pkt, ssize_t len, time_t tleft,
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned short *rtype)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Expected block?
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * First data packet from new port.
199767f8919635c4928607450d9e0abb932109ceToomas Soome } /* else check uh_sport has not changed??? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((unsigned) ntohs(t->th_code) > TFTP_MAX_ERRCODE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("illegal tftp error %d\n", ntohs(t->th_code));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Unexpected OACK. TFTP transfer already in progress.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Drop the pkt.
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Remember which port this OACK came from, because we need
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to send the ACK or errors back to it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Parse options ACK-ed by the server. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp type %d not handled\n", ntohs(t->th_opcode));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* send request, expect first block (or error) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Allow overriding default TFTP block size by setting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a tftp.blksize environment variable.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((tftp_blksize = getenv("tftp.blksize")) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l > FNAME_SIZE - (sizeof(TFTP_PREPEND_PATH) - 1))
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(TFTP_PREPEND_PATH, wtail, sizeof(TFTP_PREPEND_PATH) - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome blksize_l = sprintf(wtail, "%d", h->tftp_blksize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* h->iodesc->myport = htons(--tftpport); */
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = sendrecv_tftp(h, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &recvtftp, t, sizeof(*t) + h->tftp_blksize, &rtype);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Server ignored our blksize request, revert to TFTP default. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ack block, expect next */
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_block = htons((u_short) h->currblock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->iodesc->xid = h->currblock + 1; /* expected block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = sendrecv_tftp(h, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &recvtftp, t, sizeof(*t) + h->tftp_blksize, &rtype);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Send an ACK for the last block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_block = htons((u_short) h->currblock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_open(const char *path, struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile));
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof(char);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = snprintf(tftpfile->path, pathsize, "%s%s%s",
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_read(struct open_file *f, void *addr, size_t size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (size > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome needblock = tftpfile->off / tftpfile->tftp_blksize + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tftpfile->currblock > needblock) { /* seek backwards */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_senderr(tftpfile, 0, "No error: read aborted");
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_makereq(tftpfile); /* no error check, it worked
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for open */
199767f8919635c4928607450d9e0abb932109ceToomas Soome offinblock = tftpfile->off % tftpfile->tftp_blksize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(tftpfile->lastdata.t.th_data + offinblock,
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((tftpfile->islastblock) && (count == inbuffer))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break; /* EOF */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp: block %d not found\n", needblock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* let it time out ... */
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_write(struct open_file *f __unused, void *start __unused, size_t size __unused,
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_seek(struct open_file *f, off_t offset, int where)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t (*sproc)(struct iodesc *, void *, size_t),
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t (*rproc)(struct tftp_handle *, void *, ssize_t, time_t, unsigned short *),
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *rbuf, size_t rsize, unsigned short *rtype)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Error on transmit; wait before retrying */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to get a packet and process it. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Return on data, EOF or real error. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Timed out or didn't get the packet we're waiting for */
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_set_blksize(struct tftp_handle *h, const char *str)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Only accept blksize value if it is numeric.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RFC2348 specifies that acceptable values are 8-65464.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Let's choose a limit less than MAXRSPACE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * In RFC2347, the TFTP Option Acknowledgement package (OACK)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is used to acknowledge a client's option negotiation request.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The format of an OACK packet is:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
199767f8919635c4928607450d9e0abb932109ceToomas Soome * | opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The opcode field contains a 6, for Option Acknowledgment.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The first option acknowledgment, copied from the original
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The acknowledged value associated with the first option. If
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and how this value may differ from the original request is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * detailed in the specification for the option.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * optN, valueN
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The final option/value acknowledgment pair.
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We parse the OACK strings into an array
199767f8919635c4928607450d9e0abb932109ceToomas Soome * of name-value pairs.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Save the block size we requested for sanity check later. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse individual TFTP options.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * * "blksize" is specified in RFC2348.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * * "tsize" is specified in RFC2349.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcasecmp(tftp_options[i], "blksize") == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (strcasecmp(tftp_options[i], "tsize") == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tsize = strtol(tftp_options[i + 1], (char **)NULL, 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Do not allow any options we did not expect to be ACKed. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("unexpected tftp option '%s'\n", tftp_options[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If TFTP blksize was not set, try defaulting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to the legacy TFTP blksize of SEGSIZE(512)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Server should not be proposing block sizes that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * exceed what we said we can handle.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("unexpected blksize %u\n", h->tftp_blksize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);