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