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