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 open_file->f_devdata 41 * - server host IP in global servip 42 * Restrictions: 43 * - read only 44 * - lseek only with SEEK_SET or SEEK_CUR 45 * - no big time differences between transfers (<tftp timeout) 46 */ 47 48 #include <sys/types.h> 49 #include <sys/stat.h> 50 #include <netinet/in.h> 51 #include <netinet/udp.h> 52 #include <netinet/in_systm.h> 53 #include <arpa/tftp.h> 54 55 #include <string.h> 56 57 #include "stand.h" 58 #include "net.h" 59 #include "netif.h" 60 61 #include "tftp.h" 62 63 struct tftp_handle; 64 65 static int tftp_open(const char *path, struct open_file *f); 66 static int tftp_close(struct open_file *f); 67 static int tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len); 68 static int tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid); 69 static int tftp_write(struct open_file *f, void *buf, size_t size, size_t *resid); 70 static off_t tftp_seek(struct open_file *f, off_t offset, int where); 71 static int tftp_set_blksize(struct tftp_handle *h, const char *str); 72 static int tftp_stat(struct open_file *f, struct stat *sb); 73 static ssize_t sendrecv_tftp(struct tftp_handle *h, 74 ssize_t (*sproc)(struct iodesc *, void *, size_t), 75 void *sbuf, size_t ssize, 76 ssize_t (*rproc)(struct tftp_handle *h, void **, void **, time_t, unsigned short *), 77 void **, void **, unsigned short *rtype); 78 79 struct fs_ops tftp_fsops = { 80 "tftp", 81 tftp_open, 82 tftp_close, 83 tftp_read, 84 tftp_write, 85 tftp_seek, 86 tftp_stat, 87 null_readdir 88 }; 89 90 extern struct in_addr servip; 91 92 static int tftpport = 2000; 93 static int is_open = 0; 94 95 /* 96 * The legacy TFTP_BLKSIZE value was SEGSIZE(512). 97 * TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and 98 * IP header lengths). 99 */ 100 #define TFTP_REQUESTED_BLKSIZE 1428 101 102 /* 103 * Choose a blksize big enough so we can test with Ethernet 104 * Jumbo frames in the future. 105 */ 106 #define TFTP_MAX_BLKSIZE 9008 107 108 struct tftp_handle { 109 struct iodesc *iodesc; 110 int currblock; /* contents of lastdata */ 111 int islastblock; /* flag */ 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 }; 120 121 #define TFTP_MAX_ERRCODE EOPTNEG 122 static const int tftperrors[TFTP_MAX_ERRCODE + 1] = { 123 0, /* ??? */ 124 ENOENT, 125 EPERM, 126 ENOSPC, 127 EINVAL, /* ??? */ 128 EINVAL, /* ??? */ 129 EEXIST, 130 EINVAL, /* ??? */ 131 EINVAL, /* Option negotiation failed. */ 132 }; 133 134 static int tftp_getnextblock(struct tftp_handle *h); 135 136 /* send error message back. */ 137 static void 138 tftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg) 139 { 140 struct { 141 u_char header[HEADER_SIZE]; 142 struct tftphdr t; 143 u_char space[63]; /* +1 from t */ 144 } __packed __aligned(4) wbuf; 145 char *wtail; 146 int len; 147 148 len = strlen(msg); 149 if (len > sizeof(wbuf.space)) 150 len = sizeof(wbuf.space); 151 152 wbuf.t.th_opcode = htons((u_short) ERROR); 153 wbuf.t.th_code = htons(errcode); 154 155 wtail = wbuf.t.th_msg; 156 bcopy(msg, wtail, len); 157 wtail[len] = '\0'; 158 wtail += len + 1; 159 160 sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t); 161 } 162 163 static void 164 tftp_sendack(struct tftp_handle *h) 165 { 166 struct { 167 u_char header[HEADER_SIZE]; 168 struct tftphdr t; 169 } __packed __aligned(4) wbuf; 170 char *wtail; 171 172 wbuf.t.th_opcode = htons((u_short) ACK); 173 wtail = (char *) &wbuf.t.th_block; 174 wbuf.t.th_block = htons((u_short) h->currblock); 175 wtail += 2; 176 177 sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t); 178 } 179 180 static ssize_t 181 recvtftp(struct tftp_handle *h, void **pkt, void **payload, time_t tleft, 182 unsigned short *rtype) 183 { 184 struct iodesc *d = h->iodesc; 185 struct tftphdr *t; 186 void *ptr = NULL; 187 ssize_t len; 188 189 errno = 0; 190 191 len = readudp(d, &ptr, (void **)&t, tleft); 192 193 if (len < 4) { 194 free(ptr); 195 return (-1); 196 } 197 198 *rtype = ntohs(t->th_opcode); 199 switch (ntohs(t->th_opcode)) { 200 case DATA: { 201 int got; 202 203 if (htons(t->th_block) != (u_short) d->xid) { 204 /* 205 * Expected block? 206 */ 207 free(ptr); 208 return (-1); 209 } 210 if (d->xid == 1) { 211 /* 212 * First data packet from new port. 213 */ 214 struct udphdr *uh; 215 uh = (struct udphdr *) t - 1; 216 d->destport = uh->uh_sport; 217 } /* else check uh_sport has not changed??? */ 218 got = len - (t->th_data - (char *)t); 219 *pkt = ptr; 220 *payload = t; 221 return (got); 222 } 223 case ERROR: 224 if ((unsigned) ntohs(t->th_code) > TFTP_MAX_ERRCODE) { 225 printf("illegal tftp error %d\n", ntohs(t->th_code)); 226 errno = EIO; 227 } else { 228 #ifdef TFTP_DEBUG 229 printf("tftp-error %d\n", ntohs(t->th_code)); 230 #endif 231 errno = tftperrors[ntohs(t->th_code)]; 232 } 233 free(ptr); 234 return (-1); 235 case OACK: { 236 struct udphdr *uh; 237 int tftp_oack_len; 238 239 /* 240 * Unexpected OACK. TFTP transfer already in progress. 241 * Drop the pkt. 242 */ 243 if (d->xid != 1) { 244 free(ptr); 245 return (-1); 246 } 247 248 /* 249 * Remember which port this OACK came from, because we need 250 * to send the ACK or errors back to it. 251 */ 252 uh = (struct udphdr *) t - 1; 253 d->destport = uh->uh_sport; 254 255 /* Parse options ACK-ed by the server. */ 256 tftp_oack_len = len - sizeof(t->th_opcode); 257 if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) { 258 tftp_senderr(h, EOPTNEG, "Malformed OACK"); 259 errno = EIO; 260 free(ptr); 261 return (-1); 262 } 263 *pkt = ptr; 264 *payload = t; 265 return (0); 266 } 267 default: 268 #ifdef TFTP_DEBUG 269 printf("tftp type %d not handled\n", ntohs(t->th_opcode)); 270 #endif 271 free(ptr); 272 return (-1); 273 } 274 } 275 276 /* send request, expect first block (or error) */ 277 static int 278 tftp_makereq(struct tftp_handle *h) 279 { 280 struct { 281 u_char header[HEADER_SIZE]; 282 struct tftphdr t; 283 u_char space[FNAME_SIZE + 6]; 284 } __packed __aligned(4) wbuf; 285 char *wtail; 286 int l; 287 ssize_t res; 288 void *pkt; 289 struct tftphdr *t; 290 char *tftp_blksize = NULL; 291 int blksize_l; 292 unsigned short rtype = 0; 293 294 /* 295 * Allow overriding default TFTP block size by setting 296 * a tftp.blksize environment variable. 297 */ 298 if ((tftp_blksize = getenv("tftp.blksize")) != NULL) { 299 tftp_set_blksize(h, tftp_blksize); 300 } 301 302 wbuf.t.th_opcode = htons((u_short) RRQ); 303 wtail = wbuf.t.th_stuff; 304 l = strlen(h->path); 305 #ifdef TFTP_PREPEND_PATH 306 if (l > FNAME_SIZE - (sizeof(TFTP_PREPEND_PATH) - 1)) 307 return (ENAMETOOLONG); 308 bcopy(TFTP_PREPEND_PATH, wtail, sizeof(TFTP_PREPEND_PATH) - 1); 309 wtail += sizeof(TFTP_PREPEND_PATH) - 1; 310 #else 311 if (l > FNAME_SIZE) 312 return (ENAMETOOLONG); 313 #endif 314 bcopy(h->path, wtail, l + 1); 315 wtail += l + 1; 316 bcopy("octet", wtail, 6); 317 wtail += 6; 318 bcopy("blksize", wtail, 8); 319 wtail += 8; 320 blksize_l = sprintf(wtail, "%d", h->tftp_blksize); 321 wtail += blksize_l + 1; 322 bcopy("tsize", wtail, 6); 323 wtail += 6; 324 bcopy("0", wtail, 2); 325 wtail += 2; 326 327 /* h->iodesc->myport = htons(--tftpport); */ 328 h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff)); 329 h->iodesc->destport = htons(IPPORT_TFTP); 330 h->iodesc->xid = 1; /* expected block */ 331 332 h->currblock = 0; 333 h->islastblock = 0; 334 h->validsize = 0; 335 336 pkt = NULL; 337 res = sendrecv_tftp(h, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t, 338 &recvtftp, &pkt, (void **)&t, &rtype); 339 if (res == -1) { 340 free(pkt); 341 return (errno); 342 } 343 344 free(h->pkt); 345 h->pkt = pkt; 346 h->tftp_hdr = t; 347 348 if (rtype == OACK) 349 return (tftp_getnextblock(h)); 350 351 /* Server ignored our blksize request, revert to TFTP default. */ 352 h->tftp_blksize = SEGSIZE; 353 354 switch (rtype) { 355 case DATA: { 356 h->currblock = 1; 357 h->validsize = res; 358 h->islastblock = 0; 359 if (res < h->tftp_blksize) { 360 h->islastblock = 1; /* very short file */ 361 tftp_sendack(h); 362 } 363 return (0); 364 } 365 case ERROR: 366 default: 367 return (errno); 368 } 369 370 } 371 372 /* ack block, expect next */ 373 static int 374 tftp_getnextblock(struct tftp_handle *h) 375 { 376 struct { 377 u_char header[HEADER_SIZE]; 378 struct tftphdr t; 379 } __packed __aligned(4) wbuf; 380 char *wtail; 381 int res; 382 void *pkt; 383 struct tftphdr *t; 384 unsigned short rtype = 0; 385 wbuf.t.th_opcode = htons((u_short) ACK); 386 wtail = (char *) &wbuf.t.th_block; 387 wbuf.t.th_block = htons((u_short) h->currblock); 388 wtail += 2; 389 390 h->iodesc->xid = h->currblock + 1; /* expected block */ 391 392 pkt = NULL; 393 res = sendrecv_tftp(h, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t, 394 &recvtftp, &pkt, (void **)&t, &rtype); 395 396 if (res == -1) { /* 0 is OK! */ 397 free(pkt); 398 return (errno); 399 } 400 401 free(h->pkt); 402 h->pkt = pkt; 403 h->tftp_hdr = t; 404 h->currblock++; 405 h->validsize = res; 406 if (res < h->tftp_blksize) 407 h->islastblock = 1; /* EOF */ 408 409 if (h->islastblock == 1) { 410 /* Send an ACK for the last block */ 411 wbuf.t.th_block = htons((u_short) h->currblock); 412 sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); 413 } 414 415 return (0); 416 } 417 418 static int 419 tftp_open(const char *path, struct open_file *f) 420 { 421 struct tftp_handle *tftpfile; 422 struct iodesc *io; 423 int res; 424 size_t pathsize; 425 const char *extraslash; 426 427 if (netproto != NET_TFTP) 428 return (EINVAL); 429 430 if (f->f_dev->dv_type != DEVT_NET) 431 return (EINVAL); 432 433 if (is_open) 434 return (EBUSY); 435 436 tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile)); 437 if (!tftpfile) 438 return (ENOMEM); 439 440 memset(tftpfile, 0, sizeof(*tftpfile)); 441 tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE; 442 tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata)); 443 if (io == NULL) 444 return (EINVAL); 445 446 io->destip = servip; 447 tftpfile->off = 0; 448 pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof(char); 449 tftpfile->path = malloc(pathsize); 450 if (tftpfile->path == NULL) { 451 free(tftpfile); 452 return(ENOMEM); 453 } 454 if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/') 455 extraslash = ""; 456 else 457 extraslash = "/"; 458 res = snprintf(tftpfile->path, pathsize, "%s%s%s", 459 rootpath, extraslash, path); 460 if (res < 0 || res > pathsize) { 461 free(tftpfile->path); 462 free(tftpfile); 463 return(ENOMEM); 464 } 465 466 res = tftp_makereq(tftpfile); 467 468 if (res) { 469 free(tftpfile->path); 470 free(tftpfile->pkt); 471 free(tftpfile); 472 return (res); 473 } 474 f->f_fsdata = (void *) tftpfile; 475 is_open = 1; 476 return (0); 477 } 478 479 static int 480 tftp_read(struct open_file *f, void *addr, size_t size, 481 size_t *resid /* out */) 482 { 483 struct tftp_handle *tftpfile; 484 tftpfile = (struct tftp_handle *) f->f_fsdata; 485 486 while (size > 0) { 487 int needblock, count; 488 489 twiddle(32); 490 491 needblock = tftpfile->off / tftpfile->tftp_blksize + 1; 492 493 if (tftpfile->currblock > needblock) { /* seek backwards */ 494 tftp_senderr(tftpfile, 0, "No error: read aborted"); 495 tftp_makereq(tftpfile); /* no error check, it worked 496 * for open */ 497 } 498 499 while (tftpfile->currblock < needblock) { 500 int res; 501 502 res = tftp_getnextblock(tftpfile); 503 if (res) { /* no answer */ 504 #ifdef TFTP_DEBUG 505 printf("tftp: read error\n"); 506 #endif 507 return (res); 508 } 509 if (tftpfile->islastblock) 510 break; 511 } 512 513 if (tftpfile->currblock == needblock) { 514 int offinblock, inbuffer; 515 516 offinblock = tftpfile->off % tftpfile->tftp_blksize; 517 518 inbuffer = tftpfile->validsize - offinblock; 519 if (inbuffer < 0) { 520 #ifdef TFTP_DEBUG 521 printf("tftp: invalid offset %d\n", 522 tftpfile->off); 523 #endif 524 return (EINVAL); 525 } 526 count = (size < inbuffer ? size : inbuffer); 527 bcopy(tftpfile->tftp_hdr->th_data + offinblock, 528 addr, count); 529 530 addr = (char *)addr + count; 531 tftpfile->off += count; 532 size -= count; 533 534 if ((tftpfile->islastblock) && (count == inbuffer)) 535 break; /* EOF */ 536 } else { 537 #ifdef TFTP_DEBUG 538 printf("tftp: block %d not found\n", needblock); 539 #endif 540 return (EINVAL); 541 } 542 543 } 544 545 if (resid) 546 *resid = size; 547 return (0); 548 } 549 550 static int 551 tftp_close(struct open_file *f) 552 { 553 struct tftp_handle *tftpfile; 554 tftpfile = (struct tftp_handle *) f->f_fsdata; 555 556 /* let it time out ... */ 557 558 if (tftpfile) { 559 free(tftpfile->path); 560 free(tftpfile->pkt); 561 free(tftpfile); 562 } 563 is_open = 0; 564 return (0); 565 } 566 567 static int 568 tftp_write(struct open_file *f __unused, void *start __unused, size_t size __unused, 569 size_t *resid __unused /* out */) 570 { 571 return (EROFS); 572 } 573 574 static int 575 tftp_stat(struct open_file *f, struct stat *sb) 576 { 577 struct tftp_handle *tftpfile; 578 tftpfile = (struct tftp_handle *) f->f_fsdata; 579 580 sb->st_mode = 0444 | S_IFREG; 581 sb->st_nlink = 1; 582 sb->st_uid = 0; 583 sb->st_gid = 0; 584 sb->st_size = (off_t) tftpfile->tftp_tsize; 585 return (0); 586 } 587 588 static off_t 589 tftp_seek(struct open_file *f, off_t offset, int where) 590 { 591 struct tftp_handle *tftpfile; 592 tftpfile = (struct tftp_handle *) f->f_fsdata; 593 594 switch (where) { 595 case SEEK_SET: 596 tftpfile->off = offset; 597 break; 598 case SEEK_CUR: 599 tftpfile->off += offset; 600 break; 601 default: 602 errno = EOFFSET; 603 return (-1); 604 } 605 return (tftpfile->off); 606 } 607 608 static ssize_t 609 sendrecv_tftp(struct tftp_handle *h, 610 ssize_t (*sproc)(struct iodesc *, void *, size_t), 611 void *sbuf, size_t ssize, 612 ssize_t (*rproc)(struct tftp_handle *, void **, void **, time_t, 613 unsigned short *), 614 void **pkt, void **payload, unsigned short *rtype) 615 { 616 struct iodesc *d = h->iodesc; 617 ssize_t cc; 618 time_t t, t1, tleft; 619 620 #ifdef TFTP_DEBUG 621 if (debug) 622 printf("sendrecv: called\n"); 623 #endif 624 625 tleft = MINTMO; 626 t = t1 = getsecs(); 627 for (;;) { 628 if ((getsecs() - t) > MAXTMO) { 629 errno = ETIMEDOUT; 630 return -1; 631 } 632 633 cc = (*sproc)(d, sbuf, ssize); 634 if (cc != -1 && cc < ssize) 635 panic("sendrecv: short write! (%zd < %zu)", 636 cc, ssize); 637 638 if (cc == -1) { 639 /* Error on transmit; wait before retrying */ 640 while ((getsecs() - t1) < tleft); 641 continue; 642 } 643 644 recvnext: 645 /* Try to get a packet and process it. */ 646 cc = (*rproc)(h, pkt, payload, tleft, rtype); 647 /* Return on data, EOF or real error. */ 648 if (cc != -1 || errno != 0) 649 return (cc); 650 if ((getsecs() - t1) < tleft) { 651 goto recvnext; 652 } 653 654 /* Timed out or didn't get the packet we're waiting for */ 655 tleft += MINTMO; 656 if (tleft > (2 * MINTMO)) { 657 tleft = (2 * MINTMO); 658 } 659 t1 = getsecs(); 660 } 661 } 662 663 static int 664 tftp_set_blksize(struct tftp_handle *h, const char *str) 665 { 666 char *endptr; 667 int new_blksize; 668 int ret = 0; 669 670 if (h == NULL || str == NULL) 671 return (ret); 672 673 new_blksize = 674 (unsigned int)strtol(str, &endptr, 0); 675 676 /* 677 * Only accept blksize value if it is numeric. 678 * RFC2348 specifies that acceptable values are 8-65464. 679 * Let's choose a limit less than MAXRSPACE. 680 */ 681 if (*endptr == '\0' && new_blksize >= 8 682 && new_blksize <= TFTP_MAX_BLKSIZE) { 683 h->tftp_blksize = new_blksize; 684 ret = 1; 685 } 686 687 return (ret); 688 } 689 690 /* 691 * In RFC2347, the TFTP Option Acknowledgement package (OACK) 692 * is used to acknowledge a client's option negotiation request. 693 * The format of an OACK packet is: 694 * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+ 695 * | opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 | 696 * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+ 697 * 698 * opc 699 * The opcode field contains a 6, for Option Acknowledgment. 700 * 701 * opt1 702 * The first option acknowledgment, copied from the original 703 * request. 704 * 705 * value1 706 * The acknowledged value associated with the first option. If 707 * and how this value may differ from the original request is 708 * detailed in the specification for the option. 709 * 710 * optN, valueN 711 * The final option/value acknowledgment pair. 712 */ 713 static int 714 tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len) 715 { 716 /* 717 * We parse the OACK strings into an array 718 * of name-value pairs. 719 */ 720 char *tftp_options[128] = { 0 }; 721 char *val = buf; 722 int i = 0; 723 int option_idx = 0; 724 int blksize_is_set = 0; 725 int tsize = 0; 726 727 unsigned int orig_blksize; 728 729 while (option_idx < 128 && i < len) { 730 if (buf[i] == '\0') { 731 if (&buf[i] > val) { 732 tftp_options[option_idx] = val; 733 val = &buf[i] + 1; 734 ++option_idx; 735 } 736 } 737 ++i; 738 } 739 740 /* Save the block size we requested for sanity check later. */ 741 orig_blksize = h->tftp_blksize; 742 743 /* 744 * Parse individual TFTP options. 745 * * "blksize" is specified in RFC2348. 746 * * "tsize" is specified in RFC2349. 747 */ 748 for (i = 0; i < option_idx; i += 2) { 749 if (strcasecmp(tftp_options[i], "blksize") == 0) { 750 if (i + 1 < option_idx) 751 blksize_is_set = 752 tftp_set_blksize(h, tftp_options[i + 1]); 753 } else if (strcasecmp(tftp_options[i], "tsize") == 0) { 754 if (i + 1 < option_idx) 755 tsize = strtol(tftp_options[i + 1], (char **)NULL, 10); 756 if (tsize != 0) 757 h->tftp_tsize = tsize; 758 } else { 759 /* Do not allow any options we did not expect to be ACKed. */ 760 printf("unexpected tftp option '%s'\n", tftp_options[i]); 761 return (-1); 762 } 763 } 764 765 if (!blksize_is_set) { 766 /* 767 * If TFTP blksize was not set, try defaulting 768 * to the legacy TFTP blksize of SEGSIZE(512) 769 */ 770 h->tftp_blksize = SEGSIZE; 771 } else if (h->tftp_blksize > orig_blksize) { 772 /* 773 * Server should not be proposing block sizes that 774 * exceed what we said we can handle. 775 */ 776 printf("unexpected blksize %u\n", h->tftp_blksize); 777 return (-1); 778 } 779 780 #ifdef TFTP_DEBUG 781 printf("tftp_blksize: %u\n", h->tftp_blksize); 782 printf("tftp_tsize: %lu\n", h->tftp_tsize); 783 #endif 784 return 0; 785 } 786