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