199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1996
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Matthias Drochner. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * are met:
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 *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Simple TFTP implementation for libsa.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Assumes:
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/types.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stat.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/udp.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in_systm.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <arpa/tftp.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "net.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "netif.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "tftp.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct tftp_handle;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_open(const char *path, struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_close(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 void *sbuf, size_t ssize,
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
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct fs_ops tftp_fsops = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "tftp",
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_read,
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_write,
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_seek,
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_stat,
199767f8919635c4928607450d9e0abb932109ceToomas Soome null_readdir
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern struct in_addr servip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftpport = 2000;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int is_open = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define TFTP_REQUESTED_BLKSIZE 1428
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Choose a blksize big enough so we can test with Ethernet
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Jumbo frames in the future.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define TFTP_MAX_BLKSIZE 9008
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct tftp_handle {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int currblock; /* contents of lastdata */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int islastblock; /* flag */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int validsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path; /* saved for re-requests */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int tftp_blksize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long tftp_tsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char header[HEADER_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftphdr t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char space[TFTP_MAX_BLKSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } __packed __aligned(4) lastdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define TFTP_MAX_ERRCODE EOPTNEG
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const int tftperrors[TFTP_MAX_ERRCODE + 1] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0, /* ??? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ENOENT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EPERM,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ENOSPC,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EINVAL, /* ??? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome EINVAL, /* ??? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome EEXIST,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EINVAL, /* ??? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome EINVAL, /* Option negotiation failed. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int tftp_getnextblock(struct tftp_handle *h);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* send error message back. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char header[HEADER_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftphdr t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char space[63]; /* +1 from t */
199767f8919635c4928607450d9e0abb932109ceToomas Soome } __packed __aligned(4) wbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *wtail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(msg);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > sizeof(wbuf.space))
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = sizeof(wbuf.space);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_opcode = htons((u_short) ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_code = htons(errcode);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail = wbuf.t.th_msg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(msg, wtail, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail[len] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += len + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_sendack(struct tftp_handle *h)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char header[HEADER_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftphdr t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } __packed __aligned(4) wbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *wtail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_opcode = htons((u_short) ACK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail = (char *) &wbuf.t.th_block;
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_block = htons((u_short) h->currblock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ssize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomerecvtftp(struct tftp_handle *h, void *pkt, ssize_t len, time_t tleft,
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned short *rtype)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *d = h->iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftphdr *t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = readudp(d, pkt, len, tleft);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len < 4)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = (struct tftphdr *) pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *rtype = ntohs(t->th_opcode);
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (ntohs(t->th_opcode)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DATA: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int got;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (htons(t->th_block) != (u_short) d->xid) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Expected block?
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (d->xid == 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * First data packet from new port.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct udphdr *uh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uh = (struct udphdr *) pkt - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->destport = uh->uh_sport;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } /* else check uh_sport has not changed??? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome got = len - (t->th_data - (char *) t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return got;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome case ERROR:
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 errno = EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TFTP_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp-error %d\n", ntohs(t->th_code));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = tftperrors[ntohs(t->th_code)];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome case OACK: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct udphdr *uh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int tftp_oack_len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Unexpected OACK. TFTP transfer already in progress.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Drop the pkt.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (d->xid != 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uh = (struct udphdr *) pkt - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->destport = uh->uh_sport;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Parse options ACK-ed by the server. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_oack_len = len - sizeof(t->th_opcode);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_senderr(h, EOPTNEG, "Malformed OACK");
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TFTP_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp type %d not handled\n", ntohs(t->th_opcode));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* send request, expect first block (or error) */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_makereq(struct tftp_handle *h)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char header[HEADER_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftphdr t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char space[FNAME_SIZE + 6];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } __packed __aligned(4) wbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *wtail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftphdr *t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *tftp_blksize = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int blksize_l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned short rtype = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Allow overriding default TFTP block size by setting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a tftp.blksize environment variable.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((tftp_blksize = getenv("tftp.blksize")) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_set_blksize(h, tftp_blksize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_opcode = htons((u_short) RRQ);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail = wbuf.t.th_stuff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l = strlen(h->path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TFTP_PREPEND_PATH
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l > FNAME_SIZE - (sizeof(TFTP_PREPEND_PATH) - 1))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENAMETOOLONG);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(TFTP_PREPEND_PATH, wtail, sizeof(TFTP_PREPEND_PATH) - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += sizeof(TFTP_PREPEND_PATH) - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l > FNAME_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENAMETOOLONG);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(h->path, wtail, l + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += l + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy("octet", wtail, 6);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += 6;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy("blksize", wtail, 8);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += 8;
199767f8919635c4928607450d9e0abb932109ceToomas Soome blksize_l = sprintf(wtail, "%d", h->tftp_blksize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += blksize_l + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy("tsize", wtail, 6);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += 6;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy("0", wtail, 2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = &h->lastdata.t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* h->iodesc->myport = htons(--tftpport); */
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->iodesc->destport = htons(IPPORT_TFTP);
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->iodesc->xid = 1; /* expected block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->currblock = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->islastblock = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->validsize = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rtype == OACK)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (tftp_getnextblock(h));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Server ignored our blksize request, revert to TFTP default. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->tftp_blksize = SEGSIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (rtype) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DATA: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->currblock = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->validsize = res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->islastblock = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res < h->tftp_blksize) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->islastblock = 1; /* very short file */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_sendack(h);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome case ERROR:
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ack block, expect next */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_getnextblock(struct tftp_handle *h)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char header[HEADER_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftphdr t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } __packed __aligned(4) wbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *wtail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftphdr *t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned short rtype = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_opcode = htons((u_short) ACK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail = (char *) &wbuf.t.th_block;
199767f8919635c4928607450d9e0abb932109ceToomas Soome wbuf.t.th_block = htons((u_short) h->currblock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome wtail += 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = &h->lastdata.t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->iodesc->xid = h->currblock + 1; /* expected block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res == -1) /* 0 is OK! */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->currblock++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->validsize = res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res < h->tftp_blksize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->islastblock = 1; /* EOF */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (h->islastblock == 1) {
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 Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_open(const char *path, struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftp_handle *tftpfile;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *io;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t pathsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *extraslash;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(f->f_dev->dv_name, "net") != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef __i386__
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(f->f_dev->dv_name, "pxe") != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (is_open)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EBUSY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!tftpfile)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome memset(tftpfile, 0, sizeof(*tftpfile));
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (io == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome io->destip = servip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->off = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof(char);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->path = malloc(pathsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tftpfile->path == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tftpfile);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome extraslash = "";
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome extraslash = "/";
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = snprintf(tftpfile->path, pathsize, "%s%s%s",
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootpath, extraslash, path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res < 0 || res > pathsize) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tftpfile->path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tftpfile);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = tftp_makereq(tftpfile);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tftpfile->path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tftpfile);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (res);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_fsdata = (void *) tftpfile;
199767f8919635c4928607450d9e0abb932109ceToomas Soome is_open = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_read(struct open_file *f, void *addr, size_t size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t *resid /* out */)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftp_handle *tftpfile;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile = (struct tftp_handle *) f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (size > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int needblock, count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(32);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome needblock = tftpfile->off / tftpfile->tftp_blksize + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (tftpfile->currblock < needblock) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = tftp_getnextblock(tftpfile);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res) { /* no answer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TFTP_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp: read error\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (res);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tftpfile->islastblock)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tftpfile->currblock == needblock) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int offinblock, inbuffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome offinblock = tftpfile->off % tftpfile->tftp_blksize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome inbuffer = tftpfile->validsize - offinblock;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (inbuffer < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TFTP_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp: invalid offset %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome count = (size < inbuffer ? size : inbuffer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(tftpfile->lastdata.t.th_data + offinblock,
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr, count);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = (char *)addr + count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->off += count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size -= count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((tftpfile->islastblock) && (count == inbuffer))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break; /* EOF */
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TFTP_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp: block %d not found\n", needblock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *resid = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftp_handle *tftpfile;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile = (struct tftp_handle *) f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* let it time out ... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tftpfile) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tftpfile->path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tftpfile);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome is_open = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_write(struct open_file *f __unused, void *start __unused, size_t size __unused,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t *resid __unused /* out */)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EROFS);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_stat(struct open_file *f, struct stat *sb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftp_handle *tftpfile;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile = (struct tftp_handle *) f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_mode = 0444 | S_IFREG;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_nlink = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_uid = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_gid = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = (off_t) tftpfile->tftp_tsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic off_t
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_seek(struct open_file *f, off_t offset, int where)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct tftp_handle *tftpfile;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile = (struct tftp_handle *) f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (where) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_SET:
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->off = offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_CUR:
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftpfile->off += offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EOFFSET;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (tftpfile->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ssize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomesendrecv_tftp(struct tftp_handle *h,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t (*sproc)(struct iodesc *, void *, size_t),
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *sbuf, size_t ssize,
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{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *d = h->iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome time_t t, t1, tleft;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TFTP_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("sendrecv: called\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome tleft = MINTMO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = t1 = getsecs();
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((getsecs() - t) > MAXTMO) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = ETIMEDOUT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = (*sproc)(d, sbuf, ssize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc != -1 && cc < ssize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("sendrecv: short write! (%zd < %zu)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc, ssize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Error on transmit; wait before retrying */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((getsecs() - t1) < tleft);
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomerecvnext:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to get a packet and process it. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = (*rproc)(h, rbuf, rsize, tleft, rtype);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Return on data, EOF or real error. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc != -1 || errno != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (cc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((getsecs() - t1) < tleft) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto recvnext;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Timed out or didn't get the packet we're waiting for */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tleft += MINTMO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tleft > (2 * MINTMO)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tleft = (2 * MINTMO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome t1 = getsecs();
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_set_blksize(struct tftp_handle *h, const char *str)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *endptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int new_blksize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ret = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (h == NULL || str == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome new_blksize =
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned int)strtol(str, &endptr, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*endptr == '\0' && new_blksize >= 8
199767f8919635c4928607450d9e0abb932109ceToomas Soome && new_blksize <= TFTP_MAX_BLKSIZE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->tftp_blksize = new_blksize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * opc
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The opcode field contains a 6, for Option Acknowledgment.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * opt1
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The first option acknowledgment, copied from the original
199767f8919635c4928607450d9e0abb932109ceToomas Soome * request.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * value1
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * optN, valueN
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The final option/value acknowledgment pair.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soometftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We parse the OACK strings into an array
199767f8919635c4928607450d9e0abb932109ceToomas Soome * of name-value pairs.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *tftp_options[128] = { 0 };
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *val = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int option_idx = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int blksize_is_set = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int tsize = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int orig_blksize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (option_idx < 128 && i < len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf[i] == '\0') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (&buf[i] > val) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_options[option_idx] = val;
199767f8919635c4928607450d9e0abb932109ceToomas Soome val = &buf[i] + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ++option_idx;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ++i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Save the block size we requested for sanity check later. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome orig_blksize = h->tftp_blksize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse individual TFTP options.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * * "blksize" is specified in RFC2348.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * * "tsize" is specified in RFC2349.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < option_idx; i += 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcasecmp(tftp_options[i], "blksize") == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i + 1 < option_idx)
199767f8919635c4928607450d9e0abb932109ceToomas Soome blksize_is_set =
199767f8919635c4928607450d9e0abb932109ceToomas Soome tftp_set_blksize(h, tftp_options[i + 1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (strcasecmp(tftp_options[i], "tsize") == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i + 1 < option_idx)
199767f8919635c4928607450d9e0abb932109ceToomas Soome tsize = strtol(tftp_options[i + 1], (char **)NULL, 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tsize != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->tftp_tsize = tsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
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 }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!blksize_is_set) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If TFTP blksize was not set, try defaulting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to the legacy TFTP blksize of SEGSIZE(512)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome h->tftp_blksize = SEGSIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (h->tftp_blksize > orig_blksize) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Server should not be proposing block sizes that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * exceed what we said we can handle.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("unexpected blksize %u\n", h->tftp_blksize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TFTP_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp_blksize: %u\n", h->tftp_blksize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("tftp_tsize: %lu\n", h->tftp_tsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}