1ca987d46SWarner Losh /* $NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner Exp $ */ 2ca987d46SWarner Losh 3ca987d46SWarner Losh /* 4ca987d46SWarner Losh * Copyright (c) 1996 5ca987d46SWarner Losh * Matthias Drochner. All rights reserved. 6ca987d46SWarner Losh * 7ca987d46SWarner Losh * Redistribution and use in source and binary forms, with or without 8ca987d46SWarner Losh * modification, are permitted provided that the following conditions 9ca987d46SWarner Losh * are met: 10ca987d46SWarner Losh * 1. Redistributions of source code must retain the above copyright 11ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer. 12ca987d46SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 13ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer in the 14ca987d46SWarner Losh * documentation and/or other materials provided with the distribution. 15ca987d46SWarner Losh * 3. All advertising materials mentioning features or use of this software 16ca987d46SWarner Losh * must display the following acknowledgement: 17ca987d46SWarner Losh * This product includes software developed for the NetBSD Project 18ca987d46SWarner Losh * by Matthias Drochner. 19ca987d46SWarner Losh * 4. The name of the author may not be used to endorse or promote products 20ca987d46SWarner Losh * derived from this software without specific prior written permission. 21ca987d46SWarner Losh * 22ca987d46SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23ca987d46SWarner Losh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24ca987d46SWarner Losh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25ca987d46SWarner Losh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26ca987d46SWarner Losh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27ca987d46SWarner Losh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28ca987d46SWarner Losh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29ca987d46SWarner Losh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30ca987d46SWarner Losh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31ca987d46SWarner Losh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32ca987d46SWarner Losh */ 33ca987d46SWarner Losh 34ca987d46SWarner Losh #include <sys/cdefs.h> 35ca987d46SWarner Losh __FBSDID("$FreeBSD$"); 36ca987d46SWarner Losh 37ca987d46SWarner Losh /* 38ca987d46SWarner Losh * Simple TFTP implementation for libsa. 39ca987d46SWarner Losh * Assumes: 4098e805b4SToomas Soome * - socket descriptor (int) at dev->d_opendata, dev stored at 4198e805b4SToomas Soome * open_file->f_devdata 421b1bb6f1SToomas Soome * - server host IP in global rootip 43ca987d46SWarner Losh * Restrictions: 44ca987d46SWarner Losh * - read only 45ca987d46SWarner Losh * - lseek only with SEEK_SET or SEEK_CUR 46ca987d46SWarner Losh * - no big time differences between transfers (<tftp timeout) 47ca987d46SWarner Losh */ 48ca987d46SWarner Losh 49ca987d46SWarner Losh #include <sys/types.h> 50ca987d46SWarner Losh #include <sys/stat.h> 51ca987d46SWarner Losh #include <netinet/in.h> 52ca987d46SWarner Losh #include <netinet/udp.h> 53ca987d46SWarner Losh #include <netinet/in_systm.h> 54ca987d46SWarner Losh #include <arpa/tftp.h> 55ca987d46SWarner Losh 56ca987d46SWarner Losh #include <string.h> 57ca987d46SWarner Losh 58ca987d46SWarner Losh #include "stand.h" 59ca987d46SWarner Losh #include "net.h" 60ca987d46SWarner Losh #include "netif.h" 61ca987d46SWarner Losh 62ca987d46SWarner Losh #include "tftp.h" 63ca987d46SWarner Losh 64ca987d46SWarner Losh struct tftp_handle; 65c5b86c3bSKyle Evans struct tftprecv_extra; 66ca987d46SWarner Losh 673c3779dcSToomas Soome static ssize_t recvtftp(struct iodesc *, void **, void **, time_t, void *); 683c3779dcSToomas Soome static int tftp_open(const char *, struct open_file *); 693c3779dcSToomas Soome static int tftp_close(struct open_file *); 703c3779dcSToomas Soome static int tftp_parse_oack(struct tftp_handle *, char *, size_t); 713c3779dcSToomas Soome static int tftp_read(struct open_file *, void *, size_t, size_t *); 723c3779dcSToomas Soome static off_t tftp_seek(struct open_file *, off_t, int); 733c3779dcSToomas Soome static int tftp_set_blksize(struct tftp_handle *, const char *); 743c3779dcSToomas Soome static int tftp_stat(struct open_file *, struct stat *); 753eb01900SEmmanuel Vadot static int tftp_preload(struct open_file *); 76ca987d46SWarner Losh 77ca987d46SWarner Losh struct fs_ops tftp_fsops = { 783c3779dcSToomas Soome .fs_name = "tftp", 793c3779dcSToomas Soome .fo_open = tftp_open, 803c3779dcSToomas Soome .fo_close = tftp_close, 813c3779dcSToomas Soome .fo_read = tftp_read, 823c3779dcSToomas Soome .fo_write = null_write, 833c3779dcSToomas Soome .fo_seek = tftp_seek, 843c3779dcSToomas Soome .fo_stat = tftp_stat, 853eb01900SEmmanuel Vadot .fo_preload = tftp_preload, 863c3779dcSToomas Soome .fo_readdir = null_readdir 87ca987d46SWarner Losh }; 88ca987d46SWarner Losh 89ca987d46SWarner Losh static int tftpport = 2000; 90ca987d46SWarner Losh static int is_open = 0; 91ca987d46SWarner Losh 92ca987d46SWarner Losh /* 93ca987d46SWarner Losh * The legacy TFTP_BLKSIZE value was SEGSIZE(512). 94ca987d46SWarner Losh * TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and 95ca987d46SWarner Losh * IP header lengths). 96ca987d46SWarner Losh */ 97ca987d46SWarner Losh #define TFTP_REQUESTED_BLKSIZE 1428 98ca987d46SWarner Losh 99ca987d46SWarner Losh /* 100ca987d46SWarner Losh * Choose a blksize big enough so we can test with Ethernet 101ca987d46SWarner Losh * Jumbo frames in the future. 102ca987d46SWarner Losh */ 103ca987d46SWarner Losh #define TFTP_MAX_BLKSIZE 9008 104723f9041SSimon J. Gerraty #define TFTP_TRIES 2 105ca987d46SWarner Losh 106ca987d46SWarner Losh struct tftp_handle { 107ca987d46SWarner Losh struct iodesc *iodesc; 108ca987d46SWarner Losh int currblock; /* contents of lastdata */ 109*1a3ccb8fSDimitry Andric unsigned int islastblock:1; /* flag */ 110*1a3ccb8fSDimitry Andric unsigned int tries:4; /* number of read attempts */ 111ca987d46SWarner Losh int validsize; 112ca987d46SWarner Losh int off; 113ca987d46SWarner Losh char *path; /* saved for re-requests */ 114ca987d46SWarner Losh unsigned int tftp_blksize; 115ca987d46SWarner Losh unsigned long tftp_tsize; 116ca987d46SWarner Losh void *pkt; 117ca987d46SWarner Losh struct tftphdr *tftp_hdr; 1184f36ed51SEmmanuel Vadot char *tftp_cache; 1194f36ed51SEmmanuel Vadot bool lastacksent; 120ca987d46SWarner Losh }; 121ca987d46SWarner Losh 122c5b86c3bSKyle Evans struct tftprecv_extra { 123c5b86c3bSKyle Evans struct tftp_handle *tftp_handle; 124c5b86c3bSKyle Evans unsigned short rtype; /* Received type */ 125c5b86c3bSKyle Evans }; 126c5b86c3bSKyle Evans 127ca987d46SWarner Losh #define TFTP_MAX_ERRCODE EOPTNEG 128ca987d46SWarner Losh static const int tftperrors[TFTP_MAX_ERRCODE + 1] = { 129bf07f2f8SEmmanuel Vadot 0, /* NAK */ 130ca987d46SWarner Losh ENOENT, 131ca987d46SWarner Losh EPERM, 132ca987d46SWarner Losh ENOSPC, 133ca987d46SWarner Losh EINVAL, /* ??? */ 134ca987d46SWarner Losh EINVAL, /* ??? */ 135ca987d46SWarner Losh EEXIST, 136ca987d46SWarner Losh EINVAL, /* ??? */ 137ca987d46SWarner Losh EINVAL, /* Option negotiation failed. */ 138ca987d46SWarner Losh }; 139ca987d46SWarner Losh 140ca987d46SWarner Losh static int tftp_getnextblock(struct tftp_handle *h); 141ca987d46SWarner Losh 142ca987d46SWarner Losh /* send error message back. */ 143ca987d46SWarner Losh static void 144ca987d46SWarner Losh tftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg) 145ca987d46SWarner Losh { 146ca987d46SWarner Losh struct { 147ca987d46SWarner Losh u_char header[HEADER_SIZE]; 148ca987d46SWarner Losh struct tftphdr t; 149ca987d46SWarner Losh u_char space[63]; /* +1 from t */ 150ca987d46SWarner Losh } __packed __aligned(4) wbuf; 151ca987d46SWarner Losh char *wtail; 152ca987d46SWarner Losh int len; 153ca987d46SWarner Losh 154ca987d46SWarner Losh len = strlen(msg); 155ca987d46SWarner Losh if (len > sizeof(wbuf.space)) 156ca987d46SWarner Losh len = sizeof(wbuf.space); 157ca987d46SWarner Losh 158ca987d46SWarner Losh wbuf.t.th_opcode = htons((u_short)ERROR); 159ca987d46SWarner Losh wbuf.t.th_code = htons(errcode); 160ca987d46SWarner Losh 161ca987d46SWarner Losh wtail = wbuf.t.th_msg; 162ca987d46SWarner Losh bcopy(msg, wtail, len); 163ca987d46SWarner Losh wtail[len] = '\0'; 164ca987d46SWarner Losh wtail += len + 1; 165ca987d46SWarner Losh 166ca987d46SWarner Losh sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); 167ca987d46SWarner Losh } 168ca987d46SWarner Losh 169ca987d46SWarner Losh static void 170bb489cd7SToomas Soome tftp_sendack(struct tftp_handle *h, u_short block) 171ca987d46SWarner Losh { 172ca987d46SWarner Losh struct { 173ca987d46SWarner Losh u_char header[HEADER_SIZE]; 174ca987d46SWarner Losh struct tftphdr t; 175ca987d46SWarner Losh } __packed __aligned(4) wbuf; 176ca987d46SWarner Losh char *wtail; 177ca987d46SWarner Losh 178ca987d46SWarner Losh wbuf.t.th_opcode = htons((u_short)ACK); 179ca987d46SWarner Losh wtail = (char *)&wbuf.t.th_block; 180bb489cd7SToomas Soome wbuf.t.th_block = htons(block); 181ca987d46SWarner Losh wtail += 2; 182ca987d46SWarner Losh 183ca987d46SWarner Losh sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); 184ca987d46SWarner Losh } 185ca987d46SWarner Losh 186ca987d46SWarner Losh static ssize_t 187c5b86c3bSKyle Evans recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft, 188c5b86c3bSKyle Evans void *recv_extra) 189ca987d46SWarner Losh { 190c5b86c3bSKyle Evans struct tftprecv_extra *extra; 191c5b86c3bSKyle Evans struct tftp_handle *h; 192ca987d46SWarner Losh struct tftphdr *t; 193ca987d46SWarner Losh void *ptr = NULL; 194ca987d46SWarner Losh ssize_t len; 195bf07f2f8SEmmanuel Vadot int tftp_error; 196ca987d46SWarner Losh 197ca987d46SWarner Losh errno = 0; 1983c3779dcSToomas Soome extra = recv_extra; 199c5b86c3bSKyle Evans h = extra->tftp_handle; 200ca987d46SWarner Losh 201ca987d46SWarner Losh len = readudp(d, &ptr, (void **)&t, tleft); 202ca987d46SWarner Losh 203ca987d46SWarner Losh if (len < 4) { 204ca987d46SWarner Losh free(ptr); 205ca987d46SWarner Losh return (-1); 206ca987d46SWarner Losh } 207ca987d46SWarner Losh 208c5b86c3bSKyle Evans extra->rtype = ntohs(t->th_opcode); 209ca987d46SWarner Losh switch (ntohs(t->th_opcode)) { 210ca987d46SWarner Losh case DATA: { 211ca987d46SWarner Losh int got; 212ca987d46SWarner Losh 213bb489cd7SToomas Soome if (htons(t->th_block) < (u_short)d->xid) { 214bb489cd7SToomas Soome /* 215bb489cd7SToomas Soome * Apparently our ACK was missed, re-send. 216bb489cd7SToomas Soome */ 217bb489cd7SToomas Soome tftp_sendack(h, htons(t->th_block)); 218bb489cd7SToomas Soome free(ptr); 219bb489cd7SToomas Soome return (-1); 220bb489cd7SToomas Soome } 221ca987d46SWarner Losh if (htons(t->th_block) != (u_short)d->xid) { 222ca987d46SWarner Losh /* 223bb489cd7SToomas Soome * Packet from the future, drop this. 224ca987d46SWarner Losh */ 225ca987d46SWarner Losh free(ptr); 226ca987d46SWarner Losh return (-1); 227ca987d46SWarner Losh } 228ca987d46SWarner Losh if (d->xid == 1) { 229ca987d46SWarner Losh /* 230ca987d46SWarner Losh * First data packet from new port. 231ca987d46SWarner Losh */ 232ca987d46SWarner Losh struct udphdr *uh; 233ca987d46SWarner Losh uh = (struct udphdr *)t - 1; 234ca987d46SWarner Losh d->destport = uh->uh_sport; 235bb489cd7SToomas Soome } 236ca987d46SWarner Losh got = len - (t->th_data - (char *)t); 237ca987d46SWarner Losh *pkt = ptr; 238ca987d46SWarner Losh *payload = t; 239ca987d46SWarner Losh return (got); 240ca987d46SWarner Losh } 241ca987d46SWarner Losh case ERROR: 242bf07f2f8SEmmanuel Vadot tftp_error = ntohs(t->th_code); 243bf07f2f8SEmmanuel Vadot if ((unsigned)tftp_error > TFTP_MAX_ERRCODE) { 244bf07f2f8SEmmanuel Vadot printf("illegal tftp error %d\n", tftp_error); 245ca987d46SWarner Losh errno = EIO; 246ca987d46SWarner Losh } else { 247ca987d46SWarner Losh #ifdef TFTP_DEBUG 248bf07f2f8SEmmanuel Vadot printf("tftp-error %d\n", tftp_error); 249ca987d46SWarner Losh #endif 250bf07f2f8SEmmanuel Vadot errno = tftperrors[tftp_error]; 251ca987d46SWarner Losh } 252ca987d46SWarner Losh free(ptr); 253bf07f2f8SEmmanuel Vadot /* If we got a NAK return 0, it's usually a directory */ 254bf07f2f8SEmmanuel Vadot if (tftp_error == 0) 255bf07f2f8SEmmanuel Vadot return (0); 256ca987d46SWarner Losh return (-1); 257ca987d46SWarner Losh case OACK: { 258ca987d46SWarner Losh struct udphdr *uh; 259ca987d46SWarner Losh int tftp_oack_len; 260ca987d46SWarner Losh 261ca987d46SWarner Losh /* 262ca987d46SWarner Losh * Unexpected OACK. TFTP transfer already in progress. 263ca987d46SWarner Losh * Drop the pkt. 264ca987d46SWarner Losh */ 265ca987d46SWarner Losh if (d->xid != 1) { 266ca987d46SWarner Losh free(ptr); 267ca987d46SWarner Losh return (-1); 268ca987d46SWarner Losh } 269ca987d46SWarner Losh 270ca987d46SWarner Losh /* 271ca987d46SWarner Losh * Remember which port this OACK came from, because we need 272ca987d46SWarner Losh * to send the ACK or errors back to it. 273ca987d46SWarner Losh */ 274ca987d46SWarner Losh uh = (struct udphdr *)t - 1; 275ca987d46SWarner Losh d->destport = uh->uh_sport; 276ca987d46SWarner Losh 277ca987d46SWarner Losh /* Parse options ACK-ed by the server. */ 278ca987d46SWarner Losh tftp_oack_len = len - sizeof(t->th_opcode); 279ca987d46SWarner Losh if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) { 280ca987d46SWarner Losh tftp_senderr(h, EOPTNEG, "Malformed OACK"); 281ca987d46SWarner Losh errno = EIO; 282ca987d46SWarner Losh free(ptr); 283ca987d46SWarner Losh return (-1); 284ca987d46SWarner Losh } 285ca987d46SWarner Losh *pkt = ptr; 286ca987d46SWarner Losh *payload = t; 287ca987d46SWarner Losh return (0); 288ca987d46SWarner Losh } 289ca987d46SWarner Losh default: 290ca987d46SWarner Losh #ifdef TFTP_DEBUG 291ca987d46SWarner Losh printf("tftp type %d not handled\n", ntohs(t->th_opcode)); 292ca987d46SWarner Losh #endif 293ca987d46SWarner Losh free(ptr); 294ca987d46SWarner Losh return (-1); 295ca987d46SWarner Losh } 296ca987d46SWarner Losh } 297ca987d46SWarner Losh 298ca987d46SWarner Losh /* send request, expect first block (or error) */ 299ca987d46SWarner Losh static int 300ca987d46SWarner Losh tftp_makereq(struct tftp_handle *h) 301ca987d46SWarner Losh { 302ca987d46SWarner Losh struct { 303ca987d46SWarner Losh u_char header[HEADER_SIZE]; 304ca987d46SWarner Losh struct tftphdr t; 305ca987d46SWarner Losh u_char space[FNAME_SIZE + 6]; 306ca987d46SWarner Losh } __packed __aligned(4) wbuf; 307c5b86c3bSKyle Evans struct tftprecv_extra recv_extra; 308ca987d46SWarner Losh char *wtail; 309ca987d46SWarner Losh int l; 310ca987d46SWarner Losh ssize_t res; 311ca987d46SWarner Losh void *pkt; 312ca987d46SWarner Losh struct tftphdr *t; 313ca987d46SWarner Losh char *tftp_blksize = NULL; 314ca987d46SWarner Losh int blksize_l; 315ca987d46SWarner Losh 316ca987d46SWarner Losh /* 317ca987d46SWarner Losh * Allow overriding default TFTP block size by setting 318ca987d46SWarner Losh * a tftp.blksize environment variable. 319ca987d46SWarner Losh */ 320ca987d46SWarner Losh if ((tftp_blksize = getenv("tftp.blksize")) != NULL) { 321ca987d46SWarner Losh tftp_set_blksize(h, tftp_blksize); 322ca987d46SWarner Losh } 323ca987d46SWarner Losh 324ca987d46SWarner Losh wbuf.t.th_opcode = htons((u_short)RRQ); 325ca987d46SWarner Losh wtail = wbuf.t.th_stuff; 326ca987d46SWarner Losh l = strlen(h->path); 327ca987d46SWarner Losh #ifdef TFTP_PREPEND_PATH 328ca987d46SWarner Losh if (l > FNAME_SIZE - (sizeof(TFTP_PREPEND_PATH) - 1)) 329ca987d46SWarner Losh return (ENAMETOOLONG); 330ca987d46SWarner Losh bcopy(TFTP_PREPEND_PATH, wtail, sizeof(TFTP_PREPEND_PATH) - 1); 331ca987d46SWarner Losh wtail += sizeof(TFTP_PREPEND_PATH) - 1; 332ca987d46SWarner Losh #else 333ca987d46SWarner Losh if (l > FNAME_SIZE) 334ca987d46SWarner Losh return (ENAMETOOLONG); 335ca987d46SWarner Losh #endif 336ca987d46SWarner Losh bcopy(h->path, wtail, l + 1); 337ca987d46SWarner Losh wtail += l + 1; 338ca987d46SWarner Losh bcopy("octet", wtail, 6); 339ca987d46SWarner Losh wtail += 6; 340ca987d46SWarner Losh bcopy("blksize", wtail, 8); 341ca987d46SWarner Losh wtail += 8; 342ca987d46SWarner Losh blksize_l = sprintf(wtail, "%d", h->tftp_blksize); 343ca987d46SWarner Losh wtail += blksize_l + 1; 344ca987d46SWarner Losh bcopy("tsize", wtail, 6); 345ca987d46SWarner Losh wtail += 6; 346ca987d46SWarner Losh bcopy("0", wtail, 2); 347ca987d46SWarner Losh wtail += 2; 348ca987d46SWarner Losh 349ca987d46SWarner Losh h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff)); 350ca987d46SWarner Losh h->iodesc->destport = htons(IPPORT_TFTP); 351ca987d46SWarner Losh h->iodesc->xid = 1; /* expected block */ 352ca987d46SWarner Losh 353ca987d46SWarner Losh h->currblock = 0; 354ca987d46SWarner Losh h->islastblock = 0; 355ca987d46SWarner Losh h->validsize = 0; 356ca987d46SWarner Losh 357ca987d46SWarner Losh pkt = NULL; 358c5b86c3bSKyle Evans recv_extra.tftp_handle = h; 359c5b86c3bSKyle Evans res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t, 3603c3779dcSToomas Soome &recvtftp, &pkt, (void **)&t, &recv_extra); 361ca987d46SWarner Losh if (res == -1) { 362ca987d46SWarner Losh free(pkt); 363ca987d46SWarner Losh return (errno); 364ca987d46SWarner Losh } 365ca987d46SWarner Losh 366ca987d46SWarner Losh free(h->pkt); 367ca987d46SWarner Losh h->pkt = pkt; 368ca987d46SWarner Losh h->tftp_hdr = t; 369ca987d46SWarner Losh 370c5b86c3bSKyle Evans if (recv_extra.rtype == OACK) 371ca987d46SWarner Losh return (tftp_getnextblock(h)); 372ca987d46SWarner Losh 373ca987d46SWarner Losh /* Server ignored our blksize request, revert to TFTP default. */ 374ca987d46SWarner Losh h->tftp_blksize = SEGSIZE; 375ca987d46SWarner Losh 376c5b86c3bSKyle Evans switch (recv_extra.rtype) { 377ca987d46SWarner Losh case DATA: { 378ca987d46SWarner Losh h->currblock = 1; 379ca987d46SWarner Losh h->validsize = res; 380ca987d46SWarner Losh h->islastblock = 0; 381ca987d46SWarner Losh if (res < h->tftp_blksize) { 382ca987d46SWarner Losh h->islastblock = 1; /* very short file */ 383bb489cd7SToomas Soome tftp_sendack(h, h->currblock); 3844f36ed51SEmmanuel Vadot h->lastacksent = true; 385ca987d46SWarner Losh } 386ca987d46SWarner Losh return (0); 387ca987d46SWarner Losh } 388ca987d46SWarner Losh case ERROR: 389ca987d46SWarner Losh default: 390ca987d46SWarner Losh return (errno); 391ca987d46SWarner Losh } 392ca987d46SWarner Losh 393ca987d46SWarner Losh } 394ca987d46SWarner Losh 395ca987d46SWarner Losh /* ack block, expect next */ 396ca987d46SWarner Losh static int 397ca987d46SWarner Losh tftp_getnextblock(struct tftp_handle *h) 398ca987d46SWarner Losh { 399ca987d46SWarner Losh struct { 400ca987d46SWarner Losh u_char header[HEADER_SIZE]; 401ca987d46SWarner Losh struct tftphdr t; 402ca987d46SWarner Losh } __packed __aligned(4) wbuf; 403c5b86c3bSKyle Evans struct tftprecv_extra recv_extra; 404ca987d46SWarner Losh char *wtail; 405ca987d46SWarner Losh int res; 406ca987d46SWarner Losh void *pkt; 407ca987d46SWarner Losh struct tftphdr *t; 4083c3779dcSToomas Soome 409ca987d46SWarner Losh wbuf.t.th_opcode = htons((u_short)ACK); 410ca987d46SWarner Losh wtail = (char *)&wbuf.t.th_block; 411ca987d46SWarner Losh wbuf.t.th_block = htons((u_short)h->currblock); 412ca987d46SWarner Losh wtail += 2; 413ca987d46SWarner Losh 414ca987d46SWarner Losh h->iodesc->xid = h->currblock + 1; /* expected block */ 415ca987d46SWarner Losh 416ca987d46SWarner Losh pkt = NULL; 417c5b86c3bSKyle Evans recv_extra.tftp_handle = h; 418c5b86c3bSKyle Evans res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t, 4193c3779dcSToomas Soome &recvtftp, &pkt, (void **)&t, &recv_extra); 420ca987d46SWarner Losh 421ca987d46SWarner Losh if (res == -1) { /* 0 is OK! */ 422ca987d46SWarner Losh free(pkt); 423ca987d46SWarner Losh return (errno); 424ca987d46SWarner Losh } 425ca987d46SWarner Losh 426ca987d46SWarner Losh free(h->pkt); 427ca987d46SWarner Losh h->pkt = pkt; 428ca987d46SWarner Losh h->tftp_hdr = t; 429ca987d46SWarner Losh h->currblock++; 430ca987d46SWarner Losh h->validsize = res; 431ca987d46SWarner Losh if (res < h->tftp_blksize) 432ca987d46SWarner Losh h->islastblock = 1; /* EOF */ 433ca987d46SWarner Losh 434ca987d46SWarner Losh if (h->islastblock == 1) { 435ca987d46SWarner Losh /* Send an ACK for the last block */ 436ca987d46SWarner Losh wbuf.t.th_block = htons((u_short)h->currblock); 437ca987d46SWarner Losh sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); 438ca987d46SWarner Losh } 439ca987d46SWarner Losh 440ca987d46SWarner Losh return (0); 441ca987d46SWarner Losh } 442ca987d46SWarner Losh 443ca987d46SWarner Losh static int 444ca987d46SWarner Losh tftp_open(const char *path, struct open_file *f) 445ca987d46SWarner Losh { 44698e805b4SToomas Soome struct devdesc *dev; 447ca987d46SWarner Losh struct tftp_handle *tftpfile; 448ca987d46SWarner Losh struct iodesc *io; 449ca987d46SWarner Losh int res; 450ca987d46SWarner Losh size_t pathsize; 451ca987d46SWarner Losh const char *extraslash; 452ca987d46SWarner Losh 453ca987d46SWarner Losh if (netproto != NET_TFTP) 454ca987d46SWarner Losh return (EINVAL); 455ca987d46SWarner Losh 456ca987d46SWarner Losh if (f->f_dev->dv_type != DEVT_NET) 457ca987d46SWarner Losh return (EINVAL); 458ca987d46SWarner Losh 459ca987d46SWarner Losh if (is_open) 460ca987d46SWarner Losh return (EBUSY); 461ca987d46SWarner Losh 462c6588669SToomas Soome tftpfile = calloc(1, sizeof(*tftpfile)); 463ca987d46SWarner Losh if (!tftpfile) 464ca987d46SWarner Losh return (ENOMEM); 465ca987d46SWarner Losh 466ca987d46SWarner Losh tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE; 46798e805b4SToomas Soome dev = f->f_devdata; 46898e805b4SToomas Soome tftpfile->iodesc = io = socktodesc(*(int *)(dev->d_opendata)); 4697ee96df3SToomas Soome if (io == NULL) { 4707ee96df3SToomas Soome free(tftpfile); 471ca987d46SWarner Losh return (EINVAL); 4727ee96df3SToomas Soome } 473ca987d46SWarner Losh 4741b1bb6f1SToomas Soome io->destip = rootip; 475ca987d46SWarner Losh tftpfile->off = 0; 476ca987d46SWarner Losh pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof(char); 477ca987d46SWarner Losh tftpfile->path = malloc(pathsize); 478ca987d46SWarner Losh if (tftpfile->path == NULL) { 479ca987d46SWarner Losh free(tftpfile); 480ca987d46SWarner Losh return (ENOMEM); 481ca987d46SWarner Losh } 482ca987d46SWarner Losh if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/') 483ca987d46SWarner Losh extraslash = ""; 484ca987d46SWarner Losh else 485ca987d46SWarner Losh extraslash = "/"; 486ca987d46SWarner Losh res = snprintf(tftpfile->path, pathsize, "%s%s%s", 487ca987d46SWarner Losh rootpath, extraslash, path); 488ca987d46SWarner Losh if (res < 0 || res > pathsize) { 489ca987d46SWarner Losh free(tftpfile->path); 490ca987d46SWarner Losh free(tftpfile); 491ca987d46SWarner Losh return (ENOMEM); 492ca987d46SWarner Losh } 493ca987d46SWarner Losh 494ca987d46SWarner Losh res = tftp_makereq(tftpfile); 495ca987d46SWarner Losh 496ca987d46SWarner Losh if (res) { 497ca987d46SWarner Losh free(tftpfile->path); 498ca987d46SWarner Losh free(tftpfile->pkt); 499ca987d46SWarner Losh free(tftpfile); 500ca987d46SWarner Losh return (res); 501ca987d46SWarner Losh } 5023c3779dcSToomas Soome f->f_fsdata = tftpfile; 503ca987d46SWarner Losh is_open = 1; 504ca987d46SWarner Losh return (0); 505ca987d46SWarner Losh } 506ca987d46SWarner Losh 507ca987d46SWarner Losh static int 508ca987d46SWarner Losh tftp_read(struct open_file *f, void *addr, size_t size, 509ca987d46SWarner Losh size_t *resid /* out */) 510ca987d46SWarner Losh { 511ca987d46SWarner Losh struct tftp_handle *tftpfile; 512f442898fSToomas Soome size_t res; 5137e63e808SToomas Soome int rc; 5147e63e808SToomas Soome 5157e63e808SToomas Soome rc = 0; 516f442898fSToomas Soome res = size; 5173c3779dcSToomas Soome tftpfile = f->f_fsdata; 518ca987d46SWarner Losh 519f442898fSToomas Soome /* Make sure we will not read past file end */ 520f442898fSToomas Soome if (tftpfile->tftp_tsize > 0 && 521f442898fSToomas Soome tftpfile->off + size > tftpfile->tftp_tsize) { 522f442898fSToomas Soome size = tftpfile->tftp_tsize - tftpfile->off; 523f442898fSToomas Soome } 524f442898fSToomas Soome 5253eb01900SEmmanuel Vadot if (tftpfile->tftp_cache != NULL) { 5263eb01900SEmmanuel Vadot bcopy(tftpfile->tftp_cache + tftpfile->off, 5273eb01900SEmmanuel Vadot addr, size); 5283eb01900SEmmanuel Vadot 5293eb01900SEmmanuel Vadot addr = (char *)addr + size; 5303eb01900SEmmanuel Vadot tftpfile->off += size; 5313eb01900SEmmanuel Vadot res -= size; 5323eb01900SEmmanuel Vadot goto out; 5333eb01900SEmmanuel Vadot } 5343eb01900SEmmanuel Vadot 535ca987d46SWarner Losh while (size > 0) { 536ca987d46SWarner Losh int needblock, count; 537ca987d46SWarner Losh 538ca987d46SWarner Losh twiddle(32); 539ca987d46SWarner Losh 540ca987d46SWarner Losh needblock = tftpfile->off / tftpfile->tftp_blksize + 1; 541ca987d46SWarner Losh 542ca987d46SWarner Losh if (tftpfile->currblock > needblock) { /* seek backwards */ 543ca987d46SWarner Losh tftp_senderr(tftpfile, 0, "No error: read aborted"); 5447e63e808SToomas Soome rc = tftp_makereq(tftpfile); 5457e63e808SToomas Soome if (rc != 0) 5467e63e808SToomas Soome break; 547ca987d46SWarner Losh } 548ca987d46SWarner Losh 549ca987d46SWarner Losh while (tftpfile->currblock < needblock) { 550ca987d46SWarner Losh 5517e63e808SToomas Soome rc = tftp_getnextblock(tftpfile); 5527e63e808SToomas Soome if (rc) { /* no answer */ 553ca987d46SWarner Losh #ifdef TFTP_DEBUG 554ca987d46SWarner Losh printf("tftp: read error\n"); 555ca987d46SWarner Losh #endif 556723f9041SSimon J. Gerraty if (tftpfile->tries > TFTP_TRIES) { 5577e63e808SToomas Soome return (rc); 558723f9041SSimon J. Gerraty } else { 559723f9041SSimon J. Gerraty tftpfile->tries++; 560723f9041SSimon J. Gerraty tftp_makereq(tftpfile); 561723f9041SSimon J. Gerraty } 562ca987d46SWarner Losh } 563ca987d46SWarner Losh if (tftpfile->islastblock) 564ca987d46SWarner Losh break; 565ca987d46SWarner Losh } 566ca987d46SWarner Losh 567ca987d46SWarner Losh if (tftpfile->currblock == needblock) { 568ca987d46SWarner Losh int offinblock, inbuffer; 569ca987d46SWarner Losh 570ca987d46SWarner Losh offinblock = tftpfile->off % tftpfile->tftp_blksize; 571ca987d46SWarner Losh 572ca987d46SWarner Losh inbuffer = tftpfile->validsize - offinblock; 573ca987d46SWarner Losh if (inbuffer < 0) { 574ca987d46SWarner Losh #ifdef TFTP_DEBUG 575ca987d46SWarner Losh printf("tftp: invalid offset %d\n", 576ca987d46SWarner Losh tftpfile->off); 577ca987d46SWarner Losh #endif 578ca987d46SWarner Losh return (EINVAL); 579ca987d46SWarner Losh } 580ca987d46SWarner Losh count = (size < inbuffer ? size : inbuffer); 581ca987d46SWarner Losh bcopy(tftpfile->tftp_hdr->th_data + offinblock, 582ca987d46SWarner Losh addr, count); 583ca987d46SWarner Losh 584ca987d46SWarner Losh addr = (char *)addr + count; 585ca987d46SWarner Losh tftpfile->off += count; 586ca987d46SWarner Losh size -= count; 587f442898fSToomas Soome res -= count; 588ca987d46SWarner Losh 589ca987d46SWarner Losh if ((tftpfile->islastblock) && (count == inbuffer)) 590ca987d46SWarner Losh break; /* EOF */ 591ca987d46SWarner Losh } else { 592ca987d46SWarner Losh #ifdef TFTP_DEBUG 593ca987d46SWarner Losh printf("tftp: block %d not found\n", needblock); 594ca987d46SWarner Losh #endif 595ca987d46SWarner Losh return (EINVAL); 596ca987d46SWarner Losh } 597ca987d46SWarner Losh 598ca987d46SWarner Losh } 599ca987d46SWarner Losh 6003eb01900SEmmanuel Vadot out: 601f442898fSToomas Soome if (resid != NULL) 602f442898fSToomas Soome *resid = res; 6037e63e808SToomas Soome return (rc); 604ca987d46SWarner Losh } 605ca987d46SWarner Losh 606ca987d46SWarner Losh static int 607ca987d46SWarner Losh tftp_close(struct open_file *f) 608ca987d46SWarner Losh { 609ca987d46SWarner Losh struct tftp_handle *tftpfile; 6103c3779dcSToomas Soome tftpfile = f->f_fsdata; 611ca987d46SWarner Losh 6124f36ed51SEmmanuel Vadot if (tftpfile->lastacksent == false) 6134f36ed51SEmmanuel Vadot tftp_senderr(tftpfile, 0, "No error: file closed"); 614ca987d46SWarner Losh 615ca987d46SWarner Losh if (tftpfile) { 616ca987d46SWarner Losh free(tftpfile->path); 617ca987d46SWarner Losh free(tftpfile->pkt); 6183eb01900SEmmanuel Vadot free(tftpfile->tftp_cache); 619ca987d46SWarner Losh free(tftpfile); 620ca987d46SWarner Losh } 621ca987d46SWarner Losh is_open = 0; 622ca987d46SWarner Losh return (0); 623ca987d46SWarner Losh } 624ca987d46SWarner Losh 625ca987d46SWarner Losh static int 626ca987d46SWarner Losh tftp_stat(struct open_file *f, struct stat *sb) 627ca987d46SWarner Losh { 628ca987d46SWarner Losh struct tftp_handle *tftpfile; 6293c3779dcSToomas Soome tftpfile = f->f_fsdata; 630ca987d46SWarner Losh 631ca987d46SWarner Losh sb->st_mode = 0444 | S_IFREG; 632ca987d46SWarner Losh sb->st_nlink = 1; 633ca987d46SWarner Losh sb->st_uid = 0; 634ca987d46SWarner Losh sb->st_gid = 0; 6353c3779dcSToomas Soome sb->st_size = tftpfile->tftp_tsize; 636ca987d46SWarner Losh return (0); 637ca987d46SWarner Losh } 638ca987d46SWarner Losh 639ca987d46SWarner Losh static off_t 640ca987d46SWarner Losh tftp_seek(struct open_file *f, off_t offset, int where) 641ca987d46SWarner Losh { 642ca987d46SWarner Losh struct tftp_handle *tftpfile; 6433c3779dcSToomas Soome tftpfile = f->f_fsdata; 644ca987d46SWarner Losh 645ca987d46SWarner Losh switch (where) { 646ca987d46SWarner Losh case SEEK_SET: 647ca987d46SWarner Losh tftpfile->off = offset; 648ca987d46SWarner Losh break; 649ca987d46SWarner Losh case SEEK_CUR: 650ca987d46SWarner Losh tftpfile->off += offset; 651ca987d46SWarner Losh break; 652ca987d46SWarner Losh default: 653ca987d46SWarner Losh errno = EOFFSET; 654ca987d46SWarner Losh return (-1); 655ca987d46SWarner Losh } 656ca987d46SWarner Losh return (tftpfile->off); 657ca987d46SWarner Losh } 658ca987d46SWarner Losh 659ca987d46SWarner Losh static int 6603eb01900SEmmanuel Vadot tftp_preload(struct open_file *f) 6613eb01900SEmmanuel Vadot { 6623eb01900SEmmanuel Vadot struct tftp_handle *tftpfile; 6633eb01900SEmmanuel Vadot char *cache; 6643eb01900SEmmanuel Vadot int rc; 6653eb01900SEmmanuel Vadot #ifdef TFTP_DEBUG 6663eb01900SEmmanuel Vadot time_t start, end; 6673eb01900SEmmanuel Vadot #endif 6683eb01900SEmmanuel Vadot 6693eb01900SEmmanuel Vadot tftpfile = f->f_fsdata; 6703eb01900SEmmanuel Vadot cache = malloc(sizeof(char) * tftpfile->tftp_tsize); 6713eb01900SEmmanuel Vadot if (cache == NULL) { 6723eb01900SEmmanuel Vadot printf("Couldn't allocate %ju bytes for preload caching" 6733eb01900SEmmanuel Vadot ", disabling caching\n", 6743eb01900SEmmanuel Vadot (uintmax_t)sizeof(char) * tftpfile->tftp_tsize); 6753eb01900SEmmanuel Vadot return (-1); 6763eb01900SEmmanuel Vadot } 6773eb01900SEmmanuel Vadot 6783eb01900SEmmanuel Vadot #ifdef TFTP_DEBUG 6793eb01900SEmmanuel Vadot start = getsecs(); 6803eb01900SEmmanuel Vadot printf("Preloading %s ", tftpfile->path); 6813eb01900SEmmanuel Vadot #endif 682dfc9c1d4SEmmanuel Vadot if (tftpfile->currblock == 1) 683dfc9c1d4SEmmanuel Vadot bcopy(tftpfile->tftp_hdr->th_data, 684dfc9c1d4SEmmanuel Vadot cache, 685dfc9c1d4SEmmanuel Vadot tftpfile->validsize); 686dfc9c1d4SEmmanuel Vadot else 687dfc9c1d4SEmmanuel Vadot tftpfile->currblock = 0; 688dfc9c1d4SEmmanuel Vadot 6893eb01900SEmmanuel Vadot while (tftpfile->islastblock == 0) { 6903eb01900SEmmanuel Vadot twiddle(32); 6913eb01900SEmmanuel Vadot rc = tftp_getnextblock(tftpfile); 6923eb01900SEmmanuel Vadot if (rc) { 6933eb01900SEmmanuel Vadot free(cache); 6943eb01900SEmmanuel Vadot printf("Got TFTP error %d, disabling caching\n", rc); 6953eb01900SEmmanuel Vadot return (rc); 6963eb01900SEmmanuel Vadot } 6973eb01900SEmmanuel Vadot bcopy(tftpfile->tftp_hdr->th_data, 6983eb01900SEmmanuel Vadot cache + (tftpfile->tftp_blksize * (tftpfile->currblock - 1)), 6993eb01900SEmmanuel Vadot tftpfile->validsize); 7003eb01900SEmmanuel Vadot } 7013eb01900SEmmanuel Vadot #ifdef TFTP_DEBUG 7023eb01900SEmmanuel Vadot end = getsecs(); 7033eb01900SEmmanuel Vadot printf("\nPreloaded %s (%ju bytes) during %jd seconds\n", 7043eb01900SEmmanuel Vadot tftpfile->path, (intmax_t)tftpfile->tftp_tsize, 7053eb01900SEmmanuel Vadot (intmax_t)end - start); 7063eb01900SEmmanuel Vadot #endif 7073eb01900SEmmanuel Vadot 7083eb01900SEmmanuel Vadot tftpfile->tftp_cache = cache; 7093eb01900SEmmanuel Vadot return (0); 7103eb01900SEmmanuel Vadot } 7113eb01900SEmmanuel Vadot 7123eb01900SEmmanuel Vadot static int 713ca987d46SWarner Losh tftp_set_blksize(struct tftp_handle *h, const char *str) 714ca987d46SWarner Losh { 715ca987d46SWarner Losh char *endptr; 716ca987d46SWarner Losh int new_blksize; 717ca987d46SWarner Losh int ret = 0; 718ca987d46SWarner Losh 719ca987d46SWarner Losh if (h == NULL || str == NULL) 720ca987d46SWarner Losh return (ret); 721ca987d46SWarner Losh 722ca987d46SWarner Losh new_blksize = 723ca987d46SWarner Losh (unsigned int)strtol(str, &endptr, 0); 724ca987d46SWarner Losh 725ca987d46SWarner Losh /* 726ca987d46SWarner Losh * Only accept blksize value if it is numeric. 727ca987d46SWarner Losh * RFC2348 specifies that acceptable values are 8-65464. 728ca987d46SWarner Losh * Let's choose a limit less than MAXRSPACE. 729ca987d46SWarner Losh */ 7303c3779dcSToomas Soome if (*endptr == '\0' && new_blksize >= 8 && 7313c3779dcSToomas Soome new_blksize <= TFTP_MAX_BLKSIZE) { 732ca987d46SWarner Losh h->tftp_blksize = new_blksize; 733ca987d46SWarner Losh ret = 1; 734ca987d46SWarner Losh } 735ca987d46SWarner Losh 736ca987d46SWarner Losh return (ret); 737ca987d46SWarner Losh } 738ca987d46SWarner Losh 739ca987d46SWarner Losh /* 740ca987d46SWarner Losh * In RFC2347, the TFTP Option Acknowledgement package (OACK) 741ca987d46SWarner Losh * is used to acknowledge a client's option negotiation request. 742ca987d46SWarner Losh * The format of an OACK packet is: 743ca987d46SWarner Losh * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+ 744ca987d46SWarner Losh * | opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 | 745ca987d46SWarner Losh * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+ 746ca987d46SWarner Losh * 747ca987d46SWarner Losh * opc 748ca987d46SWarner Losh * The opcode field contains a 6, for Option Acknowledgment. 749ca987d46SWarner Losh * 750ca987d46SWarner Losh * opt1 751ca987d46SWarner Losh * The first option acknowledgment, copied from the original 752ca987d46SWarner Losh * request. 753ca987d46SWarner Losh * 754ca987d46SWarner Losh * value1 755ca987d46SWarner Losh * The acknowledged value associated with the first option. If 756ca987d46SWarner Losh * and how this value may differ from the original request is 757ca987d46SWarner Losh * detailed in the specification for the option. 758ca987d46SWarner Losh * 759ca987d46SWarner Losh * optN, valueN 760ca987d46SWarner Losh * The final option/value acknowledgment pair. 761ca987d46SWarner Losh */ 762ca987d46SWarner Losh static int 763ca987d46SWarner Losh tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len) 764ca987d46SWarner Losh { 765ca987d46SWarner Losh /* 766ca987d46SWarner Losh * We parse the OACK strings into an array 767ca987d46SWarner Losh * of name-value pairs. 768ca987d46SWarner Losh */ 769ca987d46SWarner Losh char *tftp_options[128] = { 0 }; 770ca987d46SWarner Losh char *val = buf; 771ca987d46SWarner Losh int i = 0; 772ca987d46SWarner Losh int option_idx = 0; 773ca987d46SWarner Losh int blksize_is_set = 0; 774ca987d46SWarner Losh int tsize = 0; 775ca987d46SWarner Losh 776ca987d46SWarner Losh unsigned int orig_blksize; 777ca987d46SWarner Losh 778ca987d46SWarner Losh while (option_idx < 128 && i < len) { 779ca987d46SWarner Losh if (buf[i] == '\0') { 780ca987d46SWarner Losh if (&buf[i] > val) { 781ca987d46SWarner Losh tftp_options[option_idx] = val; 782ca987d46SWarner Losh val = &buf[i] + 1; 783ca987d46SWarner Losh ++option_idx; 784ca987d46SWarner Losh } 785ca987d46SWarner Losh } 786ca987d46SWarner Losh ++i; 787ca987d46SWarner Losh } 788ca987d46SWarner Losh 789ca987d46SWarner Losh /* Save the block size we requested for sanity check later. */ 790ca987d46SWarner Losh orig_blksize = h->tftp_blksize; 791ca987d46SWarner Losh 792ca987d46SWarner Losh /* 793ca987d46SWarner Losh * Parse individual TFTP options. 794ca987d46SWarner Losh * * "blksize" is specified in RFC2348. 795ca987d46SWarner Losh * * "tsize" is specified in RFC2349. 796ca987d46SWarner Losh */ 797ca987d46SWarner Losh for (i = 0; i < option_idx; i += 2) { 798ca987d46SWarner Losh if (strcasecmp(tftp_options[i], "blksize") == 0) { 799ca987d46SWarner Losh if (i + 1 < option_idx) 800ca987d46SWarner Losh blksize_is_set = 801ca987d46SWarner Losh tftp_set_blksize(h, tftp_options[i + 1]); 802ca987d46SWarner Losh } else if (strcasecmp(tftp_options[i], "tsize") == 0) { 803ca987d46SWarner Losh if (i + 1 < option_idx) 8043c3779dcSToomas Soome tsize = strtol(tftp_options[i + 1], NULL, 10); 805ca987d46SWarner Losh if (tsize != 0) 806ca987d46SWarner Losh h->tftp_tsize = tsize; 807ca987d46SWarner Losh } else { 8083c3779dcSToomas Soome /* 8093c3779dcSToomas Soome * Do not allow any options we did not expect to be 8103c3779dcSToomas Soome * ACKed. 8113c3779dcSToomas Soome */ 8123c3779dcSToomas Soome printf("unexpected tftp option '%s'\n", 8133c3779dcSToomas Soome tftp_options[i]); 814ca987d46SWarner Losh return (-1); 815ca987d46SWarner Losh } 816ca987d46SWarner Losh } 817ca987d46SWarner Losh 818ca987d46SWarner Losh if (!blksize_is_set) { 819ca987d46SWarner Losh /* 820ca987d46SWarner Losh * If TFTP blksize was not set, try defaulting 821ca987d46SWarner Losh * to the legacy TFTP blksize of SEGSIZE(512) 822ca987d46SWarner Losh */ 823ca987d46SWarner Losh h->tftp_blksize = SEGSIZE; 824ca987d46SWarner Losh } else if (h->tftp_blksize > orig_blksize) { 825ca987d46SWarner Losh /* 826ca987d46SWarner Losh * Server should not be proposing block sizes that 827ca987d46SWarner Losh * exceed what we said we can handle. 828ca987d46SWarner Losh */ 829ca987d46SWarner Losh printf("unexpected blksize %u\n", h->tftp_blksize); 830ca987d46SWarner Losh return (-1); 831ca987d46SWarner Losh } 832ca987d46SWarner Losh 833ca987d46SWarner Losh #ifdef TFTP_DEBUG 834ca987d46SWarner Losh printf("tftp_blksize: %u\n", h->tftp_blksize); 835ca987d46SWarner Losh printf("tftp_tsize: %lu\n", h->tftp_tsize); 836ca987d46SWarner Losh #endif 8373c3779dcSToomas Soome return (0); 838ca987d46SWarner Losh } 839