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: 40*98e805b4SToomas Soome * - socket descriptor (int) at dev->d_opendata, dev stored at 41*98e805b4SToomas 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 *); 75ca987d46SWarner Losh 76ca987d46SWarner Losh struct fs_ops tftp_fsops = { 773c3779dcSToomas Soome .fs_name = "tftp", 783c3779dcSToomas Soome .fo_open = tftp_open, 793c3779dcSToomas Soome .fo_close = tftp_close, 803c3779dcSToomas Soome .fo_read = tftp_read, 813c3779dcSToomas Soome .fo_write = null_write, 823c3779dcSToomas Soome .fo_seek = tftp_seek, 833c3779dcSToomas Soome .fo_stat = tftp_stat, 843c3779dcSToomas Soome .fo_readdir = null_readdir 85ca987d46SWarner Losh }; 86ca987d46SWarner Losh 87ca987d46SWarner Losh static int tftpport = 2000; 88ca987d46SWarner Losh static int is_open = 0; 89ca987d46SWarner Losh 90ca987d46SWarner Losh /* 91ca987d46SWarner Losh * The legacy TFTP_BLKSIZE value was SEGSIZE(512). 92ca987d46SWarner Losh * TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and 93ca987d46SWarner Losh * IP header lengths). 94ca987d46SWarner Losh */ 95ca987d46SWarner Losh #define TFTP_REQUESTED_BLKSIZE 1428 96ca987d46SWarner Losh 97ca987d46SWarner Losh /* 98ca987d46SWarner Losh * Choose a blksize big enough so we can test with Ethernet 99ca987d46SWarner Losh * Jumbo frames in the future. 100ca987d46SWarner Losh */ 101ca987d46SWarner Losh #define TFTP_MAX_BLKSIZE 9008 102723f9041SSimon J. Gerraty #define TFTP_TRIES 2 103ca987d46SWarner Losh 104ca987d46SWarner Losh struct tftp_handle { 105ca987d46SWarner Losh struct iodesc *iodesc; 106ca987d46SWarner Losh int currblock; /* contents of lastdata */ 107723f9041SSimon J. Gerraty int islastblock:1; /* flag */ 108723f9041SSimon J. Gerraty int tries:4; /* number of read attempts */ 109ca987d46SWarner Losh int validsize; 110ca987d46SWarner Losh int off; 111ca987d46SWarner Losh char *path; /* saved for re-requests */ 112ca987d46SWarner Losh unsigned int tftp_blksize; 113ca987d46SWarner Losh unsigned long tftp_tsize; 114ca987d46SWarner Losh void *pkt; 115ca987d46SWarner Losh struct tftphdr *tftp_hdr; 116ca987d46SWarner Losh }; 117ca987d46SWarner Losh 118c5b86c3bSKyle Evans struct tftprecv_extra { 119c5b86c3bSKyle Evans struct tftp_handle *tftp_handle; 120c5b86c3bSKyle Evans unsigned short rtype; /* Received type */ 121c5b86c3bSKyle Evans }; 122c5b86c3bSKyle Evans 123ca987d46SWarner Losh #define TFTP_MAX_ERRCODE EOPTNEG 124ca987d46SWarner Losh static const int tftperrors[TFTP_MAX_ERRCODE + 1] = { 125ca987d46SWarner Losh 0, /* ??? */ 126ca987d46SWarner Losh ENOENT, 127ca987d46SWarner Losh EPERM, 128ca987d46SWarner Losh ENOSPC, 129ca987d46SWarner Losh EINVAL, /* ??? */ 130ca987d46SWarner Losh EINVAL, /* ??? */ 131ca987d46SWarner Losh EEXIST, 132ca987d46SWarner Losh EINVAL, /* ??? */ 133ca987d46SWarner Losh EINVAL, /* Option negotiation failed. */ 134ca987d46SWarner Losh }; 135ca987d46SWarner Losh 136ca987d46SWarner Losh static int tftp_getnextblock(struct tftp_handle *h); 137ca987d46SWarner Losh 138ca987d46SWarner Losh /* send error message back. */ 139ca987d46SWarner Losh static void 140ca987d46SWarner Losh tftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg) 141ca987d46SWarner Losh { 142ca987d46SWarner Losh struct { 143ca987d46SWarner Losh u_char header[HEADER_SIZE]; 144ca987d46SWarner Losh struct tftphdr t; 145ca987d46SWarner Losh u_char space[63]; /* +1 from t */ 146ca987d46SWarner Losh } __packed __aligned(4) wbuf; 147ca987d46SWarner Losh char *wtail; 148ca987d46SWarner Losh int len; 149ca987d46SWarner Losh 150ca987d46SWarner Losh len = strlen(msg); 151ca987d46SWarner Losh if (len > sizeof(wbuf.space)) 152ca987d46SWarner Losh len = sizeof(wbuf.space); 153ca987d46SWarner Losh 154ca987d46SWarner Losh wbuf.t.th_opcode = htons((u_short)ERROR); 155ca987d46SWarner Losh wbuf.t.th_code = htons(errcode); 156ca987d46SWarner Losh 157ca987d46SWarner Losh wtail = wbuf.t.th_msg; 158ca987d46SWarner Losh bcopy(msg, wtail, len); 159ca987d46SWarner Losh wtail[len] = '\0'; 160ca987d46SWarner Losh wtail += len + 1; 161ca987d46SWarner Losh 162ca987d46SWarner Losh sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); 163ca987d46SWarner Losh } 164ca987d46SWarner Losh 165ca987d46SWarner Losh static void 166bb489cd7SToomas Soome tftp_sendack(struct tftp_handle *h, u_short block) 167ca987d46SWarner Losh { 168ca987d46SWarner Losh struct { 169ca987d46SWarner Losh u_char header[HEADER_SIZE]; 170ca987d46SWarner Losh struct tftphdr t; 171ca987d46SWarner Losh } __packed __aligned(4) wbuf; 172ca987d46SWarner Losh char *wtail; 173ca987d46SWarner Losh 174ca987d46SWarner Losh wbuf.t.th_opcode = htons((u_short)ACK); 175ca987d46SWarner Losh wtail = (char *)&wbuf.t.th_block; 176bb489cd7SToomas Soome wbuf.t.th_block = htons(block); 177ca987d46SWarner Losh wtail += 2; 178ca987d46SWarner Losh 179ca987d46SWarner Losh sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); 180ca987d46SWarner Losh } 181ca987d46SWarner Losh 182ca987d46SWarner Losh static ssize_t 183c5b86c3bSKyle Evans recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft, 184c5b86c3bSKyle Evans void *recv_extra) 185ca987d46SWarner Losh { 186c5b86c3bSKyle Evans struct tftprecv_extra *extra; 187c5b86c3bSKyle Evans struct tftp_handle *h; 188ca987d46SWarner Losh struct tftphdr *t; 189ca987d46SWarner Losh void *ptr = NULL; 190ca987d46SWarner Losh ssize_t len; 191ca987d46SWarner Losh 192ca987d46SWarner Losh errno = 0; 1933c3779dcSToomas Soome extra = recv_extra; 194c5b86c3bSKyle Evans h = extra->tftp_handle; 195ca987d46SWarner Losh 196ca987d46SWarner Losh len = readudp(d, &ptr, (void **)&t, tleft); 197ca987d46SWarner Losh 198ca987d46SWarner Losh if (len < 4) { 199ca987d46SWarner Losh free(ptr); 200ca987d46SWarner Losh return (-1); 201ca987d46SWarner Losh } 202ca987d46SWarner Losh 203c5b86c3bSKyle Evans extra->rtype = ntohs(t->th_opcode); 204ca987d46SWarner Losh switch (ntohs(t->th_opcode)) { 205ca987d46SWarner Losh case DATA: { 206ca987d46SWarner Losh int got; 207ca987d46SWarner Losh 208bb489cd7SToomas Soome if (htons(t->th_block) < (u_short)d->xid) { 209bb489cd7SToomas Soome /* 210bb489cd7SToomas Soome * Apparently our ACK was missed, re-send. 211bb489cd7SToomas Soome */ 212bb489cd7SToomas Soome tftp_sendack(h, htons(t->th_block)); 213bb489cd7SToomas Soome free(ptr); 214bb489cd7SToomas Soome return (-1); 215bb489cd7SToomas Soome } 216ca987d46SWarner Losh if (htons(t->th_block) != (u_short)d->xid) { 217ca987d46SWarner Losh /* 218bb489cd7SToomas Soome * Packet from the future, drop this. 219ca987d46SWarner Losh */ 220ca987d46SWarner Losh free(ptr); 221ca987d46SWarner Losh return (-1); 222ca987d46SWarner Losh } 223ca987d46SWarner Losh if (d->xid == 1) { 224ca987d46SWarner Losh /* 225ca987d46SWarner Losh * First data packet from new port. 226ca987d46SWarner Losh */ 227ca987d46SWarner Losh struct udphdr *uh; 228ca987d46SWarner Losh uh = (struct udphdr *)t - 1; 229ca987d46SWarner Losh d->destport = uh->uh_sport; 230bb489cd7SToomas Soome } 231ca987d46SWarner Losh got = len - (t->th_data - (char *)t); 232ca987d46SWarner Losh *pkt = ptr; 233ca987d46SWarner Losh *payload = t; 234ca987d46SWarner Losh return (got); 235ca987d46SWarner Losh } 236ca987d46SWarner Losh case ERROR: 237ca987d46SWarner Losh if ((unsigned)ntohs(t->th_code) > TFTP_MAX_ERRCODE) { 238ca987d46SWarner Losh printf("illegal tftp error %d\n", ntohs(t->th_code)); 239ca987d46SWarner Losh errno = EIO; 240ca987d46SWarner Losh } else { 241ca987d46SWarner Losh #ifdef TFTP_DEBUG 242ca987d46SWarner Losh printf("tftp-error %d\n", ntohs(t->th_code)); 243ca987d46SWarner Losh #endif 244ca987d46SWarner Losh errno = tftperrors[ntohs(t->th_code)]; 245ca987d46SWarner Losh } 246ca987d46SWarner Losh free(ptr); 247ca987d46SWarner Losh return (-1); 248ca987d46SWarner Losh case OACK: { 249ca987d46SWarner Losh struct udphdr *uh; 250ca987d46SWarner Losh int tftp_oack_len; 251ca987d46SWarner Losh 252ca987d46SWarner Losh /* 253ca987d46SWarner Losh * Unexpected OACK. TFTP transfer already in progress. 254ca987d46SWarner Losh * Drop the pkt. 255ca987d46SWarner Losh */ 256ca987d46SWarner Losh if (d->xid != 1) { 257ca987d46SWarner Losh free(ptr); 258ca987d46SWarner Losh return (-1); 259ca987d46SWarner Losh } 260ca987d46SWarner Losh 261ca987d46SWarner Losh /* 262ca987d46SWarner Losh * Remember which port this OACK came from, because we need 263ca987d46SWarner Losh * to send the ACK or errors back to it. 264ca987d46SWarner Losh */ 265ca987d46SWarner Losh uh = (struct udphdr *)t - 1; 266ca987d46SWarner Losh d->destport = uh->uh_sport; 267ca987d46SWarner Losh 268ca987d46SWarner Losh /* Parse options ACK-ed by the server. */ 269ca987d46SWarner Losh tftp_oack_len = len - sizeof(t->th_opcode); 270ca987d46SWarner Losh if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) { 271ca987d46SWarner Losh tftp_senderr(h, EOPTNEG, "Malformed OACK"); 272ca987d46SWarner Losh errno = EIO; 273ca987d46SWarner Losh free(ptr); 274ca987d46SWarner Losh return (-1); 275ca987d46SWarner Losh } 276ca987d46SWarner Losh *pkt = ptr; 277ca987d46SWarner Losh *payload = t; 278ca987d46SWarner Losh return (0); 279ca987d46SWarner Losh } 280ca987d46SWarner Losh default: 281ca987d46SWarner Losh #ifdef TFTP_DEBUG 282ca987d46SWarner Losh printf("tftp type %d not handled\n", ntohs(t->th_opcode)); 283ca987d46SWarner Losh #endif 284ca987d46SWarner Losh free(ptr); 285ca987d46SWarner Losh return (-1); 286ca987d46SWarner Losh } 287ca987d46SWarner Losh } 288ca987d46SWarner Losh 289ca987d46SWarner Losh /* send request, expect first block (or error) */ 290ca987d46SWarner Losh static int 291ca987d46SWarner Losh tftp_makereq(struct tftp_handle *h) 292ca987d46SWarner Losh { 293ca987d46SWarner Losh struct { 294ca987d46SWarner Losh u_char header[HEADER_SIZE]; 295ca987d46SWarner Losh struct tftphdr t; 296ca987d46SWarner Losh u_char space[FNAME_SIZE + 6]; 297ca987d46SWarner Losh } __packed __aligned(4) wbuf; 298c5b86c3bSKyle Evans struct tftprecv_extra recv_extra; 299ca987d46SWarner Losh char *wtail; 300ca987d46SWarner Losh int l; 301ca987d46SWarner Losh ssize_t res; 302ca987d46SWarner Losh void *pkt; 303ca987d46SWarner Losh struct tftphdr *t; 304ca987d46SWarner Losh char *tftp_blksize = NULL; 305ca987d46SWarner Losh int blksize_l; 306ca987d46SWarner Losh 307ca987d46SWarner Losh /* 308ca987d46SWarner Losh * Allow overriding default TFTP block size by setting 309ca987d46SWarner Losh * a tftp.blksize environment variable. 310ca987d46SWarner Losh */ 311ca987d46SWarner Losh if ((tftp_blksize = getenv("tftp.blksize")) != NULL) { 312ca987d46SWarner Losh tftp_set_blksize(h, tftp_blksize); 313ca987d46SWarner Losh } 314ca987d46SWarner Losh 315ca987d46SWarner Losh wbuf.t.th_opcode = htons((u_short)RRQ); 316ca987d46SWarner Losh wtail = wbuf.t.th_stuff; 317ca987d46SWarner Losh l = strlen(h->path); 318ca987d46SWarner Losh #ifdef TFTP_PREPEND_PATH 319ca987d46SWarner Losh if (l > FNAME_SIZE - (sizeof(TFTP_PREPEND_PATH) - 1)) 320ca987d46SWarner Losh return (ENAMETOOLONG); 321ca987d46SWarner Losh bcopy(TFTP_PREPEND_PATH, wtail, sizeof(TFTP_PREPEND_PATH) - 1); 322ca987d46SWarner Losh wtail += sizeof(TFTP_PREPEND_PATH) - 1; 323ca987d46SWarner Losh #else 324ca987d46SWarner Losh if (l > FNAME_SIZE) 325ca987d46SWarner Losh return (ENAMETOOLONG); 326ca987d46SWarner Losh #endif 327ca987d46SWarner Losh bcopy(h->path, wtail, l + 1); 328ca987d46SWarner Losh wtail += l + 1; 329ca987d46SWarner Losh bcopy("octet", wtail, 6); 330ca987d46SWarner Losh wtail += 6; 331ca987d46SWarner Losh bcopy("blksize", wtail, 8); 332ca987d46SWarner Losh wtail += 8; 333ca987d46SWarner Losh blksize_l = sprintf(wtail, "%d", h->tftp_blksize); 334ca987d46SWarner Losh wtail += blksize_l + 1; 335ca987d46SWarner Losh bcopy("tsize", wtail, 6); 336ca987d46SWarner Losh wtail += 6; 337ca987d46SWarner Losh bcopy("0", wtail, 2); 338ca987d46SWarner Losh wtail += 2; 339ca987d46SWarner Losh 340ca987d46SWarner Losh h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff)); 341ca987d46SWarner Losh h->iodesc->destport = htons(IPPORT_TFTP); 342ca987d46SWarner Losh h->iodesc->xid = 1; /* expected block */ 343ca987d46SWarner Losh 344ca987d46SWarner Losh h->currblock = 0; 345ca987d46SWarner Losh h->islastblock = 0; 346ca987d46SWarner Losh h->validsize = 0; 347ca987d46SWarner Losh 348ca987d46SWarner Losh pkt = NULL; 349c5b86c3bSKyle Evans recv_extra.tftp_handle = h; 350c5b86c3bSKyle Evans res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t, 3513c3779dcSToomas Soome &recvtftp, &pkt, (void **)&t, &recv_extra); 352ca987d46SWarner Losh if (res == -1) { 353ca987d46SWarner Losh free(pkt); 354ca987d46SWarner Losh return (errno); 355ca987d46SWarner Losh } 356ca987d46SWarner Losh 357ca987d46SWarner Losh free(h->pkt); 358ca987d46SWarner Losh h->pkt = pkt; 359ca987d46SWarner Losh h->tftp_hdr = t; 360ca987d46SWarner Losh 361c5b86c3bSKyle Evans if (recv_extra.rtype == OACK) 362ca987d46SWarner Losh return (tftp_getnextblock(h)); 363ca987d46SWarner Losh 364ca987d46SWarner Losh /* Server ignored our blksize request, revert to TFTP default. */ 365ca987d46SWarner Losh h->tftp_blksize = SEGSIZE; 366ca987d46SWarner Losh 367c5b86c3bSKyle Evans switch (recv_extra.rtype) { 368ca987d46SWarner Losh case DATA: { 369ca987d46SWarner Losh h->currblock = 1; 370ca987d46SWarner Losh h->validsize = res; 371ca987d46SWarner Losh h->islastblock = 0; 372ca987d46SWarner Losh if (res < h->tftp_blksize) { 373ca987d46SWarner Losh h->islastblock = 1; /* very short file */ 374bb489cd7SToomas Soome tftp_sendack(h, h->currblock); 375ca987d46SWarner Losh } 376ca987d46SWarner Losh return (0); 377ca987d46SWarner Losh } 378ca987d46SWarner Losh case ERROR: 379ca987d46SWarner Losh default: 380ca987d46SWarner Losh return (errno); 381ca987d46SWarner Losh } 382ca987d46SWarner Losh 383ca987d46SWarner Losh } 384ca987d46SWarner Losh 385ca987d46SWarner Losh /* ack block, expect next */ 386ca987d46SWarner Losh static int 387ca987d46SWarner Losh tftp_getnextblock(struct tftp_handle *h) 388ca987d46SWarner Losh { 389ca987d46SWarner Losh struct { 390ca987d46SWarner Losh u_char header[HEADER_SIZE]; 391ca987d46SWarner Losh struct tftphdr t; 392ca987d46SWarner Losh } __packed __aligned(4) wbuf; 393c5b86c3bSKyle Evans struct tftprecv_extra recv_extra; 394ca987d46SWarner Losh char *wtail; 395ca987d46SWarner Losh int res; 396ca987d46SWarner Losh void *pkt; 397ca987d46SWarner Losh struct tftphdr *t; 3983c3779dcSToomas Soome 399ca987d46SWarner Losh wbuf.t.th_opcode = htons((u_short)ACK); 400ca987d46SWarner Losh wtail = (char *)&wbuf.t.th_block; 401ca987d46SWarner Losh wbuf.t.th_block = htons((u_short)h->currblock); 402ca987d46SWarner Losh wtail += 2; 403ca987d46SWarner Losh 404ca987d46SWarner Losh h->iodesc->xid = h->currblock + 1; /* expected block */ 405ca987d46SWarner Losh 406ca987d46SWarner Losh pkt = NULL; 407c5b86c3bSKyle Evans recv_extra.tftp_handle = h; 408c5b86c3bSKyle Evans res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t, 4093c3779dcSToomas Soome &recvtftp, &pkt, (void **)&t, &recv_extra); 410ca987d46SWarner Losh 411ca987d46SWarner Losh if (res == -1) { /* 0 is OK! */ 412ca987d46SWarner Losh free(pkt); 413ca987d46SWarner Losh return (errno); 414ca987d46SWarner Losh } 415ca987d46SWarner Losh 416ca987d46SWarner Losh free(h->pkt); 417ca987d46SWarner Losh h->pkt = pkt; 418ca987d46SWarner Losh h->tftp_hdr = t; 419ca987d46SWarner Losh h->currblock++; 420ca987d46SWarner Losh h->validsize = res; 421ca987d46SWarner Losh if (res < h->tftp_blksize) 422ca987d46SWarner Losh h->islastblock = 1; /* EOF */ 423ca987d46SWarner Losh 424ca987d46SWarner Losh if (h->islastblock == 1) { 425ca987d46SWarner Losh /* Send an ACK for the last block */ 426ca987d46SWarner Losh wbuf.t.th_block = htons((u_short)h->currblock); 427ca987d46SWarner Losh sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); 428ca987d46SWarner Losh } 429ca987d46SWarner Losh 430ca987d46SWarner Losh return (0); 431ca987d46SWarner Losh } 432ca987d46SWarner Losh 433ca987d46SWarner Losh static int 434ca987d46SWarner Losh tftp_open(const char *path, struct open_file *f) 435ca987d46SWarner Losh { 436*98e805b4SToomas Soome struct devdesc *dev; 437ca987d46SWarner Losh struct tftp_handle *tftpfile; 438ca987d46SWarner Losh struct iodesc *io; 439ca987d46SWarner Losh int res; 440ca987d46SWarner Losh size_t pathsize; 441ca987d46SWarner Losh const char *extraslash; 442ca987d46SWarner Losh 443ca987d46SWarner Losh if (netproto != NET_TFTP) 444ca987d46SWarner Losh return (EINVAL); 445ca987d46SWarner Losh 446ca987d46SWarner Losh if (f->f_dev->dv_type != DEVT_NET) 447ca987d46SWarner Losh return (EINVAL); 448ca987d46SWarner Losh 449ca987d46SWarner Losh if (is_open) 450ca987d46SWarner Losh return (EBUSY); 451ca987d46SWarner Losh 452c6588669SToomas Soome tftpfile = calloc(1, sizeof(*tftpfile)); 453ca987d46SWarner Losh if (!tftpfile) 454ca987d46SWarner Losh return (ENOMEM); 455ca987d46SWarner Losh 456ca987d46SWarner Losh tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE; 457*98e805b4SToomas Soome dev = f->f_devdata; 458*98e805b4SToomas Soome tftpfile->iodesc = io = socktodesc(*(int *)(dev->d_opendata)); 4597ee96df3SToomas Soome if (io == NULL) { 4607ee96df3SToomas Soome free(tftpfile); 461ca987d46SWarner Losh return (EINVAL); 4627ee96df3SToomas Soome } 463ca987d46SWarner Losh 4641b1bb6f1SToomas Soome io->destip = rootip; 465ca987d46SWarner Losh tftpfile->off = 0; 466ca987d46SWarner Losh pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof(char); 467ca987d46SWarner Losh tftpfile->path = malloc(pathsize); 468ca987d46SWarner Losh if (tftpfile->path == NULL) { 469ca987d46SWarner Losh free(tftpfile); 470ca987d46SWarner Losh return (ENOMEM); 471ca987d46SWarner Losh } 472ca987d46SWarner Losh if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/') 473ca987d46SWarner Losh extraslash = ""; 474ca987d46SWarner Losh else 475ca987d46SWarner Losh extraslash = "/"; 476ca987d46SWarner Losh res = snprintf(tftpfile->path, pathsize, "%s%s%s", 477ca987d46SWarner Losh rootpath, extraslash, path); 478ca987d46SWarner Losh if (res < 0 || res > pathsize) { 479ca987d46SWarner Losh free(tftpfile->path); 480ca987d46SWarner Losh free(tftpfile); 481ca987d46SWarner Losh return (ENOMEM); 482ca987d46SWarner Losh } 483ca987d46SWarner Losh 484ca987d46SWarner Losh res = tftp_makereq(tftpfile); 485ca987d46SWarner Losh 486ca987d46SWarner Losh if (res) { 487ca987d46SWarner Losh free(tftpfile->path); 488ca987d46SWarner Losh free(tftpfile->pkt); 489ca987d46SWarner Losh free(tftpfile); 490ca987d46SWarner Losh return (res); 491ca987d46SWarner Losh } 4923c3779dcSToomas Soome f->f_fsdata = tftpfile; 493ca987d46SWarner Losh is_open = 1; 494ca987d46SWarner Losh return (0); 495ca987d46SWarner Losh } 496ca987d46SWarner Losh 497ca987d46SWarner Losh static int 498ca987d46SWarner Losh tftp_read(struct open_file *f, void *addr, size_t size, 499ca987d46SWarner Losh size_t *resid /* out */) 500ca987d46SWarner Losh { 501ca987d46SWarner Losh struct tftp_handle *tftpfile; 502f442898fSToomas Soome size_t res; 5037e63e808SToomas Soome int rc; 5047e63e808SToomas Soome 5057e63e808SToomas Soome rc = 0; 506f442898fSToomas Soome res = size; 5073c3779dcSToomas Soome tftpfile = f->f_fsdata; 508ca987d46SWarner Losh 509f442898fSToomas Soome /* Make sure we will not read past file end */ 510f442898fSToomas Soome if (tftpfile->tftp_tsize > 0 && 511f442898fSToomas Soome tftpfile->off + size > tftpfile->tftp_tsize) { 512f442898fSToomas Soome size = tftpfile->tftp_tsize - tftpfile->off; 513f442898fSToomas Soome } 514f442898fSToomas Soome 515ca987d46SWarner Losh while (size > 0) { 516ca987d46SWarner Losh int needblock, count; 517ca987d46SWarner Losh 518ca987d46SWarner Losh twiddle(32); 519ca987d46SWarner Losh 520ca987d46SWarner Losh needblock = tftpfile->off / tftpfile->tftp_blksize + 1; 521ca987d46SWarner Losh 522ca987d46SWarner Losh if (tftpfile->currblock > needblock) { /* seek backwards */ 523ca987d46SWarner Losh tftp_senderr(tftpfile, 0, "No error: read aborted"); 5247e63e808SToomas Soome rc = tftp_makereq(tftpfile); 5257e63e808SToomas Soome if (rc != 0) 5267e63e808SToomas Soome break; 527ca987d46SWarner Losh } 528ca987d46SWarner Losh 529ca987d46SWarner Losh while (tftpfile->currblock < needblock) { 530ca987d46SWarner Losh 5317e63e808SToomas Soome rc = tftp_getnextblock(tftpfile); 5327e63e808SToomas Soome if (rc) { /* no answer */ 533ca987d46SWarner Losh #ifdef TFTP_DEBUG 534ca987d46SWarner Losh printf("tftp: read error\n"); 535ca987d46SWarner Losh #endif 536723f9041SSimon J. Gerraty if (tftpfile->tries > TFTP_TRIES) { 5377e63e808SToomas Soome return (rc); 538723f9041SSimon J. Gerraty } else { 539723f9041SSimon J. Gerraty tftpfile->tries++; 540723f9041SSimon J. Gerraty tftp_makereq(tftpfile); 541723f9041SSimon J. Gerraty } 542ca987d46SWarner Losh } 543ca987d46SWarner Losh if (tftpfile->islastblock) 544ca987d46SWarner Losh break; 545ca987d46SWarner Losh } 546ca987d46SWarner Losh 547ca987d46SWarner Losh if (tftpfile->currblock == needblock) { 548ca987d46SWarner Losh int offinblock, inbuffer; 549ca987d46SWarner Losh 550ca987d46SWarner Losh offinblock = tftpfile->off % tftpfile->tftp_blksize; 551ca987d46SWarner Losh 552ca987d46SWarner Losh inbuffer = tftpfile->validsize - offinblock; 553ca987d46SWarner Losh if (inbuffer < 0) { 554ca987d46SWarner Losh #ifdef TFTP_DEBUG 555ca987d46SWarner Losh printf("tftp: invalid offset %d\n", 556ca987d46SWarner Losh tftpfile->off); 557ca987d46SWarner Losh #endif 558ca987d46SWarner Losh return (EINVAL); 559ca987d46SWarner Losh } 560ca987d46SWarner Losh count = (size < inbuffer ? size : inbuffer); 561ca987d46SWarner Losh bcopy(tftpfile->tftp_hdr->th_data + offinblock, 562ca987d46SWarner Losh addr, count); 563ca987d46SWarner Losh 564ca987d46SWarner Losh addr = (char *)addr + count; 565ca987d46SWarner Losh tftpfile->off += count; 566ca987d46SWarner Losh size -= count; 567f442898fSToomas Soome res -= count; 568ca987d46SWarner Losh 569ca987d46SWarner Losh if ((tftpfile->islastblock) && (count == inbuffer)) 570ca987d46SWarner Losh break; /* EOF */ 571ca987d46SWarner Losh } else { 572ca987d46SWarner Losh #ifdef TFTP_DEBUG 573ca987d46SWarner Losh printf("tftp: block %d not found\n", needblock); 574ca987d46SWarner Losh #endif 575ca987d46SWarner Losh return (EINVAL); 576ca987d46SWarner Losh } 577ca987d46SWarner Losh 578ca987d46SWarner Losh } 579ca987d46SWarner Losh 580f442898fSToomas Soome if (resid != NULL) 581f442898fSToomas Soome *resid = res; 5827e63e808SToomas Soome return (rc); 583ca987d46SWarner Losh } 584ca987d46SWarner Losh 585ca987d46SWarner Losh static int 586ca987d46SWarner Losh tftp_close(struct open_file *f) 587ca987d46SWarner Losh { 588ca987d46SWarner Losh struct tftp_handle *tftpfile; 5893c3779dcSToomas Soome tftpfile = f->f_fsdata; 590ca987d46SWarner Losh 591ca987d46SWarner Losh /* let it time out ... */ 592ca987d46SWarner Losh 593ca987d46SWarner Losh if (tftpfile) { 594ca987d46SWarner Losh free(tftpfile->path); 595ca987d46SWarner Losh free(tftpfile->pkt); 596ca987d46SWarner Losh free(tftpfile); 597ca987d46SWarner Losh } 598ca987d46SWarner Losh is_open = 0; 599ca987d46SWarner Losh return (0); 600ca987d46SWarner Losh } 601ca987d46SWarner Losh 602ca987d46SWarner Losh static int 603ca987d46SWarner Losh tftp_stat(struct open_file *f, struct stat *sb) 604ca987d46SWarner Losh { 605ca987d46SWarner Losh struct tftp_handle *tftpfile; 6063c3779dcSToomas Soome tftpfile = f->f_fsdata; 607ca987d46SWarner Losh 608ca987d46SWarner Losh sb->st_mode = 0444 | S_IFREG; 609ca987d46SWarner Losh sb->st_nlink = 1; 610ca987d46SWarner Losh sb->st_uid = 0; 611ca987d46SWarner Losh sb->st_gid = 0; 6123c3779dcSToomas Soome sb->st_size = tftpfile->tftp_tsize; 613ca987d46SWarner Losh return (0); 614ca987d46SWarner Losh } 615ca987d46SWarner Losh 616ca987d46SWarner Losh static off_t 617ca987d46SWarner Losh tftp_seek(struct open_file *f, off_t offset, int where) 618ca987d46SWarner Losh { 619ca987d46SWarner Losh struct tftp_handle *tftpfile; 6203c3779dcSToomas Soome tftpfile = f->f_fsdata; 621ca987d46SWarner Losh 622ca987d46SWarner Losh switch (where) { 623ca987d46SWarner Losh case SEEK_SET: 624ca987d46SWarner Losh tftpfile->off = offset; 625ca987d46SWarner Losh break; 626ca987d46SWarner Losh case SEEK_CUR: 627ca987d46SWarner Losh tftpfile->off += offset; 628ca987d46SWarner Losh break; 629ca987d46SWarner Losh default: 630ca987d46SWarner Losh errno = EOFFSET; 631ca987d46SWarner Losh return (-1); 632ca987d46SWarner Losh } 633ca987d46SWarner Losh return (tftpfile->off); 634ca987d46SWarner Losh } 635ca987d46SWarner Losh 636ca987d46SWarner Losh static int 637ca987d46SWarner Losh tftp_set_blksize(struct tftp_handle *h, const char *str) 638ca987d46SWarner Losh { 639ca987d46SWarner Losh char *endptr; 640ca987d46SWarner Losh int new_blksize; 641ca987d46SWarner Losh int ret = 0; 642ca987d46SWarner Losh 643ca987d46SWarner Losh if (h == NULL || str == NULL) 644ca987d46SWarner Losh return (ret); 645ca987d46SWarner Losh 646ca987d46SWarner Losh new_blksize = 647ca987d46SWarner Losh (unsigned int)strtol(str, &endptr, 0); 648ca987d46SWarner Losh 649ca987d46SWarner Losh /* 650ca987d46SWarner Losh * Only accept blksize value if it is numeric. 651ca987d46SWarner Losh * RFC2348 specifies that acceptable values are 8-65464. 652ca987d46SWarner Losh * Let's choose a limit less than MAXRSPACE. 653ca987d46SWarner Losh */ 6543c3779dcSToomas Soome if (*endptr == '\0' && new_blksize >= 8 && 6553c3779dcSToomas Soome new_blksize <= TFTP_MAX_BLKSIZE) { 656ca987d46SWarner Losh h->tftp_blksize = new_blksize; 657ca987d46SWarner Losh ret = 1; 658ca987d46SWarner Losh } 659ca987d46SWarner Losh 660ca987d46SWarner Losh return (ret); 661ca987d46SWarner Losh } 662ca987d46SWarner Losh 663ca987d46SWarner Losh /* 664ca987d46SWarner Losh * In RFC2347, the TFTP Option Acknowledgement package (OACK) 665ca987d46SWarner Losh * is used to acknowledge a client's option negotiation request. 666ca987d46SWarner Losh * The format of an OACK packet is: 667ca987d46SWarner Losh * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+ 668ca987d46SWarner Losh * | opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 | 669ca987d46SWarner Losh * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+ 670ca987d46SWarner Losh * 671ca987d46SWarner Losh * opc 672ca987d46SWarner Losh * The opcode field contains a 6, for Option Acknowledgment. 673ca987d46SWarner Losh * 674ca987d46SWarner Losh * opt1 675ca987d46SWarner Losh * The first option acknowledgment, copied from the original 676ca987d46SWarner Losh * request. 677ca987d46SWarner Losh * 678ca987d46SWarner Losh * value1 679ca987d46SWarner Losh * The acknowledged value associated with the first option. If 680ca987d46SWarner Losh * and how this value may differ from the original request is 681ca987d46SWarner Losh * detailed in the specification for the option. 682ca987d46SWarner Losh * 683ca987d46SWarner Losh * optN, valueN 684ca987d46SWarner Losh * The final option/value acknowledgment pair. 685ca987d46SWarner Losh */ 686ca987d46SWarner Losh static int 687ca987d46SWarner Losh tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len) 688ca987d46SWarner Losh { 689ca987d46SWarner Losh /* 690ca987d46SWarner Losh * We parse the OACK strings into an array 691ca987d46SWarner Losh * of name-value pairs. 692ca987d46SWarner Losh */ 693ca987d46SWarner Losh char *tftp_options[128] = { 0 }; 694ca987d46SWarner Losh char *val = buf; 695ca987d46SWarner Losh int i = 0; 696ca987d46SWarner Losh int option_idx = 0; 697ca987d46SWarner Losh int blksize_is_set = 0; 698ca987d46SWarner Losh int tsize = 0; 699ca987d46SWarner Losh 700ca987d46SWarner Losh unsigned int orig_blksize; 701ca987d46SWarner Losh 702ca987d46SWarner Losh while (option_idx < 128 && i < len) { 703ca987d46SWarner Losh if (buf[i] == '\0') { 704ca987d46SWarner Losh if (&buf[i] > val) { 705ca987d46SWarner Losh tftp_options[option_idx] = val; 706ca987d46SWarner Losh val = &buf[i] + 1; 707ca987d46SWarner Losh ++option_idx; 708ca987d46SWarner Losh } 709ca987d46SWarner Losh } 710ca987d46SWarner Losh ++i; 711ca987d46SWarner Losh } 712ca987d46SWarner Losh 713ca987d46SWarner Losh /* Save the block size we requested for sanity check later. */ 714ca987d46SWarner Losh orig_blksize = h->tftp_blksize; 715ca987d46SWarner Losh 716ca987d46SWarner Losh /* 717ca987d46SWarner Losh * Parse individual TFTP options. 718ca987d46SWarner Losh * * "blksize" is specified in RFC2348. 719ca987d46SWarner Losh * * "tsize" is specified in RFC2349. 720ca987d46SWarner Losh */ 721ca987d46SWarner Losh for (i = 0; i < option_idx; i += 2) { 722ca987d46SWarner Losh if (strcasecmp(tftp_options[i], "blksize") == 0) { 723ca987d46SWarner Losh if (i + 1 < option_idx) 724ca987d46SWarner Losh blksize_is_set = 725ca987d46SWarner Losh tftp_set_blksize(h, tftp_options[i + 1]); 726ca987d46SWarner Losh } else if (strcasecmp(tftp_options[i], "tsize") == 0) { 727ca987d46SWarner Losh if (i + 1 < option_idx) 7283c3779dcSToomas Soome tsize = strtol(tftp_options[i + 1], NULL, 10); 729ca987d46SWarner Losh if (tsize != 0) 730ca987d46SWarner Losh h->tftp_tsize = tsize; 731ca987d46SWarner Losh } else { 7323c3779dcSToomas Soome /* 7333c3779dcSToomas Soome * Do not allow any options we did not expect to be 7343c3779dcSToomas Soome * ACKed. 7353c3779dcSToomas Soome */ 7363c3779dcSToomas Soome printf("unexpected tftp option '%s'\n", 7373c3779dcSToomas Soome tftp_options[i]); 738ca987d46SWarner Losh return (-1); 739ca987d46SWarner Losh } 740ca987d46SWarner Losh } 741ca987d46SWarner Losh 742ca987d46SWarner Losh if (!blksize_is_set) { 743ca987d46SWarner Losh /* 744ca987d46SWarner Losh * If TFTP blksize was not set, try defaulting 745ca987d46SWarner Losh * to the legacy TFTP blksize of SEGSIZE(512) 746ca987d46SWarner Losh */ 747ca987d46SWarner Losh h->tftp_blksize = SEGSIZE; 748ca987d46SWarner Losh } else if (h->tftp_blksize > orig_blksize) { 749ca987d46SWarner Losh /* 750ca987d46SWarner Losh * Server should not be proposing block sizes that 751ca987d46SWarner Losh * exceed what we said we can handle. 752ca987d46SWarner Losh */ 753ca987d46SWarner Losh printf("unexpected blksize %u\n", h->tftp_blksize); 754ca987d46SWarner Losh return (-1); 755ca987d46SWarner Losh } 756ca987d46SWarner Losh 757ca987d46SWarner Losh #ifdef TFTP_DEBUG 758ca987d46SWarner Losh printf("tftp_blksize: %u\n", h->tftp_blksize); 759ca987d46SWarner Losh printf("tftp_tsize: %lu\n", h->tftp_tsize); 760ca987d46SWarner Losh #endif 7613c3779dcSToomas Soome return (0); 762ca987d46SWarner Losh } 763