1 1.38 rin /* $NetBSD: tftp.c,v 1.38 2022/08/07 05:51:55 rin Exp $ */ 2 1.1 drochner 3 1.1 drochner /* 4 1.1 drochner * Copyright (c) 1996 5 1.1 drochner * Matthias Drochner. All rights reserved. 6 1.1 drochner * 7 1.1 drochner * Redistribution and use in source and binary forms, with or without 8 1.1 drochner * modification, are permitted provided that the following conditions 9 1.1 drochner * are met: 10 1.1 drochner * 1. Redistributions of source code must retain the above copyright 11 1.1 drochner * notice, this list of conditions and the following disclaimer. 12 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 drochner * notice, this list of conditions and the following disclaimer in the 14 1.1 drochner * documentation and/or other materials provided with the distribution. 15 1.1 drochner * 16 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 drochner * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 drochner * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 drochner * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 drochner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 drochner * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 drochner * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 drochner * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 drochner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 drochner * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 drochner * 27 1.1 drochner */ 28 1.1 drochner 29 1.1 drochner /* 30 1.1 drochner * Simple TFTP implementation for libsa. 31 1.1 drochner * Assumes: 32 1.1 drochner * - socket descriptor (int) at open_file->f_devdata 33 1.1 drochner * - server host IP in global servip 34 1.1 drochner * Restrictions: 35 1.1 drochner * - read only 36 1.1 drochner * - lseek only with SEEK_SET or SEEK_CUR 37 1.1 drochner * - no big time differences between transfers (<tftp timeout) 38 1.1 drochner */ 39 1.1 drochner 40 1.6 cgd /* 41 1.6 cgd * XXX Does not currently implement: 42 1.6 cgd * XXX 43 1.6 cgd * XXX LIBSA_NO_FS_CLOSE 44 1.6 cgd * XXX LIBSA_NO_FS_SEEK 45 1.6 cgd * XXX LIBSA_NO_FS_WRITE 46 1.6 cgd * XXX LIBSA_NO_FS_SYMLINK (does this even make sense?) 47 1.6 cgd * XXX LIBSA_FS_SINGLECOMPONENT (does this even make sense?) 48 1.6 cgd */ 49 1.6 cgd 50 1.1 drochner #include <sys/types.h> 51 1.1 drochner #include <sys/stat.h> 52 1.1 drochner #include <netinet/in.h> 53 1.1 drochner #include <netinet/udp.h> 54 1.1 drochner #include <netinet/in_systm.h> 55 1.9 thorpej #include <lib/libkern/libkern.h> 56 1.1 drochner 57 1.1 drochner #include "stand.h" 58 1.1 drochner #include "net.h" 59 1.1 drochner 60 1.1 drochner #include "tftp.h" 61 1.1 drochner 62 1.1 drochner extern struct in_addr servip; 63 1.1 drochner 64 1.1 drochner static int tftpport = 2000; 65 1.1 drochner 66 1.1 drochner #define RSPACE 520 /* max data packet, rounded up */ 67 1.1 drochner 68 1.1 drochner struct tftp_handle { 69 1.1 drochner struct iodesc *iodesc; 70 1.1 drochner int currblock; /* contents of lastdata */ 71 1.1 drochner int islastblock; /* flag */ 72 1.1 drochner int validsize; 73 1.1 drochner int off; 74 1.15 dsl const char *path; /* saved for re-requests */ 75 1.1 drochner struct { 76 1.31 zoltan u_char header[UDP_TOTAL_HEADER_SIZE]; 77 1.1 drochner struct tftphdr t; 78 1.1 drochner u_char space[RSPACE]; 79 1.1 drochner } lastdata; 80 1.1 drochner }; 81 1.1 drochner 82 1.14 mycroft static const int tftperrors[8] = { 83 1.1 drochner 0, /* ??? */ 84 1.1 drochner ENOENT, 85 1.1 drochner EPERM, 86 1.1 drochner ENOSPC, 87 1.1 drochner EINVAL, /* ??? */ 88 1.1 drochner EINVAL, /* ??? */ 89 1.1 drochner EEXIST, 90 1.23 isaki EINVAL, /* ??? */ 91 1.1 drochner }; 92 1.3 christos 93 1.29 tsutsui static ssize_t recvtftp(struct iodesc *, void *, size_t, saseconds_t); 94 1.29 tsutsui static int tftp_makereq(struct tftp_handle *); 95 1.29 tsutsui static int tftp_getnextblock(struct tftp_handle *); 96 1.3 christos #ifndef TFTP_NOTERMINATE 97 1.29 tsutsui static void tftp_terminate(struct tftp_handle *); 98 1.3 christos #endif 99 1.29 tsutsui static ssize_t tftp_size_of_file(struct tftp_handle *tftpfile); 100 1.1 drochner 101 1.18 perry static ssize_t 102 1.28 tsutsui recvtftp(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft) 103 1.1 drochner { 104 1.7 drochner ssize_t n; 105 1.1 drochner struct tftphdr *t; 106 1.1 drochner 107 1.1 drochner errno = 0; 108 1.1 drochner 109 1.7 drochner n = readudp(d, pkt, len, tleft); 110 1.1 drochner 111 1.7 drochner if (n < 4) 112 1.23 isaki return -1; 113 1.1 drochner 114 1.23 isaki t = (struct tftphdr *)pkt; 115 1.1 drochner switch (ntohs(t->th_opcode)) { 116 1.7 drochner case DATA: 117 1.38 rin if (ntohs(t->th_block) != d->xid) { 118 1.1 drochner /* 119 1.1 drochner * Expected block? 120 1.1 drochner */ 121 1.23 isaki return -1; 122 1.1 drochner } 123 1.1 drochner if (d->xid == 1) { 124 1.1 drochner /* 125 1.1 drochner * First data packet from new port. 126 1.1 drochner */ 127 1.10 augustss struct udphdr *uh; 128 1.23 isaki uh = (struct udphdr *)pkt - 1; 129 1.1 drochner d->destport = uh->uh_sport; 130 1.1 drochner } /* else check uh_sport has not changed??? */ 131 1.7 drochner return (n - (t->th_data - (char *)t)); 132 1.1 drochner case ERROR: 133 1.24 tsutsui if ((unsigned int)ntohs(t->th_code) >= 8) { 134 1.1 drochner printf("illegal tftp error %d\n", ntohs(t->th_code)); 135 1.1 drochner errno = EIO; 136 1.1 drochner } else { 137 1.1 drochner #ifdef DEBUG 138 1.1 drochner printf("tftp-error %d\n", ntohs(t->th_code)); 139 1.1 drochner #endif 140 1.1 drochner errno = tftperrors[ntohs(t->th_code)]; 141 1.1 drochner } 142 1.23 isaki return -1; 143 1.1 drochner default: 144 1.1 drochner #ifdef DEBUG 145 1.1 drochner printf("tftp type %d not handled\n", ntohs(t->th_opcode)); 146 1.1 drochner #endif 147 1.23 isaki return -1; 148 1.1 drochner } 149 1.1 drochner } 150 1.1 drochner 151 1.1 drochner /* send request, expect first block (or error) */ 152 1.18 perry static int 153 1.23 isaki tftp_makereq(struct tftp_handle *h) 154 1.1 drochner { 155 1.1 drochner struct { 156 1.31 zoltan u_char header[UDP_TOTAL_HEADER_SIZE]; 157 1.23 isaki struct tftphdr t; 158 1.1 drochner u_char space[FNAME_SIZE + 6]; 159 1.1 drochner } wbuf; 160 1.1 drochner char *wtail; 161 1.1 drochner int l; 162 1.1 drochner ssize_t res; 163 1.1 drochner struct tftphdr *t; 164 1.1 drochner 165 1.23 isaki wbuf.t.th_opcode = htons((u_short)RRQ); 166 1.1 drochner wtail = wbuf.t.th_stuff; 167 1.1 drochner l = strlen(h->path); 168 1.25 christos (void)memcpy(wtail, h->path, l + 1); 169 1.1 drochner wtail += l + 1; 170 1.25 christos (void)memcpy(wtail, "octet", 6); 171 1.1 drochner wtail += 6; 172 1.1 drochner 173 1.1 drochner t = &h->lastdata.t; 174 1.1 drochner 175 1.1 drochner /* h->iodesc->myport = htons(--tftpport); */ 176 1.1 drochner h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff)); 177 1.1 drochner h->iodesc->destport = htons(IPPORT_TFTP); 178 1.1 drochner h->iodesc->xid = 1; /* expected block */ 179 1.1 drochner 180 1.23 isaki res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *)&wbuf.t, 181 1.1 drochner recvtftp, t, sizeof(*t) + RSPACE); 182 1.1 drochner 183 1.1 drochner if (res == -1) 184 1.23 isaki return errno; 185 1.1 drochner 186 1.1 drochner h->currblock = 1; 187 1.1 drochner h->validsize = res; 188 1.1 drochner h->islastblock = 0; 189 1.1 drochner if (res < SEGSIZE) 190 1.1 drochner h->islastblock = 1; /* very short file */ 191 1.23 isaki return 0; 192 1.1 drochner } 193 1.1 drochner 194 1.1 drochner /* ack block, expect next */ 195 1.18 perry static int 196 1.23 isaki tftp_getnextblock(struct tftp_handle *h) 197 1.1 drochner { 198 1.1 drochner struct { 199 1.31 zoltan u_char header[UDP_TOTAL_HEADER_SIZE]; 200 1.1 drochner struct tftphdr t; 201 1.1 drochner } wbuf; 202 1.1 drochner char *wtail; 203 1.1 drochner int res; 204 1.1 drochner struct tftphdr *t; 205 1.1 drochner 206 1.23 isaki wbuf.t.th_opcode = htons((u_short)ACK); 207 1.23 isaki wbuf.t.th_block = htons((u_short)h->currblock); 208 1.23 isaki wtail = (char *)&wbuf.t.th_data; 209 1.1 drochner 210 1.1 drochner t = &h->lastdata.t; 211 1.1 drochner 212 1.1 drochner h->iodesc->xid = h->currblock + 1; /* expected block */ 213 1.1 drochner 214 1.23 isaki res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *)&wbuf.t, 215 1.1 drochner recvtftp, t, sizeof(*t) + RSPACE); 216 1.1 drochner 217 1.1 drochner if (res == -1) /* 0 is OK! */ 218 1.23 isaki return errno; 219 1.1 drochner 220 1.1 drochner h->currblock++; 221 1.1 drochner h->validsize = res; 222 1.1 drochner if (res < SEGSIZE) 223 1.1 drochner h->islastblock = 1; /* EOF */ 224 1.23 isaki return 0; 225 1.1 drochner } 226 1.1 drochner 227 1.1 drochner #ifndef TFTP_NOTERMINATE 228 1.1 drochner static void 229 1.23 isaki tftp_terminate(struct tftp_handle *h) 230 1.1 drochner { 231 1.1 drochner struct { 232 1.31 zoltan u_char header[UDP_TOTAL_HEADER_SIZE]; 233 1.1 drochner struct tftphdr t; 234 1.1 drochner } wbuf; 235 1.1 drochner char *wtail; 236 1.1 drochner 237 1.30 drochner wtail = (char *)&wbuf.t.th_data; 238 1.1 drochner if (h->islastblock) { 239 1.23 isaki wbuf.t.th_opcode = htons((u_short)ACK); 240 1.23 isaki wbuf.t.th_block = htons((u_short)h->currblock); 241 1.1 drochner } else { 242 1.23 isaki wbuf.t.th_opcode = htons((u_short)ERROR); 243 1.23 isaki wbuf.t.th_code = htons((u_short)ENOSPACE); /* ??? */ 244 1.30 drochner *wtail++ = '\0'; /* empty error string */ 245 1.1 drochner } 246 1.1 drochner 247 1.23 isaki (void)sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); 248 1.1 drochner } 249 1.1 drochner #endif 250 1.1 drochner 251 1.32 joerg __compactcall int 252 1.23 isaki tftp_open(const char *path, struct open_file *f) 253 1.1 drochner { 254 1.1 drochner struct tftp_handle *tftpfile; 255 1.1 drochner struct iodesc *io; 256 1.1 drochner int res; 257 1.1 drochner 258 1.23 isaki tftpfile = (struct tftp_handle *)alloc(sizeof(*tftpfile)); 259 1.1 drochner if (!tftpfile) 260 1.23 isaki return ENOMEM; 261 1.1 drochner 262 1.23 isaki tftpfile->iodesc = io = socktodesc(*(int *)(f->f_devdata)); 263 1.1 drochner io->destip = servip; 264 1.1 drochner tftpfile->off = 0; 265 1.1 drochner tftpfile->path = path; /* XXXXXXX we hope it's static */ 266 1.1 drochner 267 1.4 kim res = tftp_makereq(tftpfile); 268 1.1 drochner 269 1.1 drochner if (res) { 270 1.20 christos dealloc(tftpfile, sizeof(*tftpfile)); 271 1.23 isaki return res; 272 1.1 drochner } 273 1.23 isaki f->f_fsdata = (void *)tftpfile; 274 1.27 ad fsmod = "nfs"; 275 1.23 isaki return 0; 276 1.1 drochner } 277 1.1 drochner 278 1.32 joerg __compactcall int 279 1.23 isaki tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid) 280 1.1 drochner { 281 1.1 drochner struct tftp_handle *tftpfile; 282 1.6 cgd #if !defined(LIBSA_NO_TWIDDLE) 283 1.1 drochner static int tc = 0; 284 1.6 cgd #endif 285 1.23 isaki tftpfile = (struct tftp_handle *)f->f_fsdata; 286 1.1 drochner 287 1.1 drochner while (size > 0) { 288 1.16 fvdl int needblock; 289 1.16 fvdl size_t count; 290 1.1 drochner 291 1.1 drochner needblock = tftpfile->off / SEGSIZE + 1; 292 1.1 drochner 293 1.1 drochner if (tftpfile->currblock > needblock) { /* seek backwards */ 294 1.1 drochner #ifndef TFTP_NOTERMINATE 295 1.1 drochner tftp_terminate(tftpfile); 296 1.1 drochner #endif 297 1.1 drochner tftp_makereq(tftpfile); /* no error check, it worked 298 1.23 isaki * for open */ 299 1.1 drochner } 300 1.1 drochner 301 1.1 drochner while (tftpfile->currblock < needblock) { 302 1.1 drochner int res; 303 1.1 drochner 304 1.37 rin #if !defined(LIBSA_NO_TWIDDLE) 305 1.37 rin if (!(tc++ % 16)) 306 1.37 rin twiddle(); 307 1.37 rin #endif 308 1.37 rin 309 1.1 drochner res = tftp_getnextblock(tftpfile); 310 1.1 drochner if (res) { /* no answer */ 311 1.1 drochner #ifdef DEBUG 312 1.36 christos printf("%s: read error (block %d->%d)\n", 313 1.36 christos __func__, tftpfile->currblock, needblock); 314 1.1 drochner #endif 315 1.23 isaki return res; 316 1.1 drochner } 317 1.1 drochner if (tftpfile->islastblock) 318 1.1 drochner break; 319 1.1 drochner } 320 1.1 drochner 321 1.1 drochner if (tftpfile->currblock == needblock) { 322 1.16 fvdl size_t offinblock, inbuffer; 323 1.1 drochner 324 1.1 drochner offinblock = tftpfile->off % SEGSIZE; 325 1.1 drochner 326 1.36 christos if ((int)offinblock > tftpfile->validsize) { 327 1.1 drochner #ifdef DEBUG 328 1.36 christos printf("%s: invalid offset %d\n", __func__, 329 1.1 drochner tftpfile->off); 330 1.1 drochner #endif 331 1.23 isaki return EINVAL; 332 1.1 drochner } 333 1.28 tsutsui inbuffer = tftpfile->validsize - offinblock; 334 1.1 drochner count = (size < inbuffer ? size : inbuffer); 335 1.25 christos (void)memcpy(addr, 336 1.25 christos tftpfile->lastdata.t.th_data + offinblock, 337 1.25 christos count); 338 1.1 drochner 339 1.22 he addr = (char *)addr + count; 340 1.1 drochner tftpfile->off += count; 341 1.1 drochner size -= count; 342 1.1 drochner 343 1.1 drochner if ((tftpfile->islastblock) && (count == inbuffer)) 344 1.1 drochner break; /* EOF */ 345 1.1 drochner } else { 346 1.1 drochner #ifdef DEBUG 347 1.36 christos printf("%s: block %d not found\n", 348 1.36 christos __func__, needblock); 349 1.1 drochner #endif 350 1.23 isaki return EINVAL; 351 1.1 drochner } 352 1.1 drochner 353 1.1 drochner } 354 1.1 drochner 355 1.1 drochner if (resid) 356 1.1 drochner *resid = size; 357 1.23 isaki return 0; 358 1.1 drochner } 359 1.1 drochner 360 1.32 joerg __compactcall int 361 1.23 isaki tftp_close(struct open_file *f) 362 1.1 drochner { 363 1.1 drochner struct tftp_handle *tftpfile; 364 1.23 isaki tftpfile = (struct tftp_handle *)f->f_fsdata; 365 1.1 drochner 366 1.1 drochner #ifdef TFTP_NOTERMINATE 367 1.1 drochner /* let it time out ... */ 368 1.1 drochner #else 369 1.1 drochner tftp_terminate(tftpfile); 370 1.1 drochner #endif 371 1.1 drochner 372 1.20 christos dealloc(tftpfile, sizeof(*tftpfile)); 373 1.23 isaki return 0; 374 1.1 drochner } 375 1.1 drochner 376 1.32 joerg __compactcall int 377 1.23 isaki tftp_write(struct open_file *f, void *start, size_t size, size_t *resid) 378 1.1 drochner { 379 1.23 isaki 380 1.23 isaki return EROFS; 381 1.1 drochner } 382 1.1 drochner 383 1.26 chris static ssize_t 384 1.26 chris tftp_size_of_file(struct tftp_handle *tftpfile) 385 1.26 chris { 386 1.26 chris ssize_t filesize; 387 1.26 chris 388 1.26 chris if (tftpfile->currblock > 1) { /* move to start of file */ 389 1.26 chris #ifndef TFTP_NOTERMINATE 390 1.26 chris tftp_terminate(tftpfile); 391 1.26 chris #endif 392 1.26 chris tftp_makereq(tftpfile); /* no error check, it worked 393 1.26 chris * for open */ 394 1.26 chris } 395 1.26 chris 396 1.26 chris /* start with the size of block 1 */ 397 1.26 chris filesize = tftpfile->validsize; 398 1.26 chris 399 1.26 chris /* and keep adding the sizes till we hit the last block */ 400 1.26 chris while (!tftpfile->islastblock) { 401 1.26 chris int res; 402 1.26 chris 403 1.26 chris res = tftp_getnextblock(tftpfile); 404 1.26 chris if (res) { /* no answer */ 405 1.26 chris #ifdef DEBUG 406 1.36 christos printf("%s: read error (block %d)\n", 407 1.36 christos __func__, tftpfile->currblock); 408 1.26 chris #endif 409 1.26 chris return -1; 410 1.26 chris } 411 1.26 chris filesize += tftpfile->validsize; 412 1.26 chris } 413 1.26 chris #ifdef DEBUG 414 1.36 christos printf("%s: file is %zu bytes\n", __func__, filesize); 415 1.26 chris #endif 416 1.26 chris return filesize; 417 1.26 chris } 418 1.26 chris 419 1.32 joerg __compactcall int 420 1.23 isaki tftp_stat(struct open_file *f, struct stat *sb) 421 1.1 drochner { 422 1.1 drochner struct tftp_handle *tftpfile; 423 1.23 isaki tftpfile = (struct tftp_handle *)f->f_fsdata; 424 1.1 drochner 425 1.1 drochner sb->st_mode = 0444; 426 1.1 drochner sb->st_nlink = 1; 427 1.1 drochner sb->st_uid = 0; 428 1.1 drochner sb->st_gid = 0; 429 1.26 chris sb->st_size = tftp_size_of_file(tftpfile); 430 1.23 isaki return 0; 431 1.1 drochner } 432 1.1 drochner 433 1.34 tsutsui #if defined(LIBSA_ENABLE_LS_OP) 434 1.35 christos #include "ls.h" 435 1.34 tsutsui __compactcall void 436 1.34 tsutsui tftp_ls(struct open_file *f, const char *pattern) 437 1.34 tsutsui { 438 1.35 christos lsunsup("tftp"); 439 1.34 tsutsui } 440 1.34 tsutsui #endif 441 1.34 tsutsui 442 1.32 joerg __compactcall off_t 443 1.23 isaki tftp_seek(struct open_file *f, off_t offset, int where) 444 1.1 drochner { 445 1.1 drochner struct tftp_handle *tftpfile; 446 1.23 isaki tftpfile = (struct tftp_handle *)f->f_fsdata; 447 1.1 drochner 448 1.1 drochner switch (where) { 449 1.1 drochner case SEEK_SET: 450 1.1 drochner tftpfile->off = offset; 451 1.1 drochner break; 452 1.1 drochner case SEEK_CUR: 453 1.1 drochner tftpfile->off += offset; 454 1.1 drochner break; 455 1.1 drochner default: 456 1.1 drochner errno = EOFFSET; 457 1.23 isaki return -1; 458 1.1 drochner } 459 1.23 isaki return tftpfile->off; 460 1.1 drochner } 461