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