xref: /freebsd/stand/libsa/tftp.c (revision 3e15b01d)
1ca987d46SWarner Losh /*	$NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner Exp $	 */
2ca987d46SWarner Losh 
3ca987d46SWarner Losh /*
4ca987d46SWarner Losh  * Copyright (c) 1996
5ca987d46SWarner Losh  *	Matthias Drochner.  All rights reserved.
6ca987d46SWarner Losh  *
7ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
8ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
9ca987d46SWarner Losh  * are met:
10ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
12ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
13ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
14ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
15ca987d46SWarner Losh  * 3. All advertising materials mentioning features or use of this software
16ca987d46SWarner Losh  *    must display the following acknowledgement:
17ca987d46SWarner Losh  *	This product includes software developed for the NetBSD Project
18ca987d46SWarner Losh  *	by Matthias Drochner.
19ca987d46SWarner Losh  * 4. The name of the author may not be used to endorse or promote products
20ca987d46SWarner Losh  *    derived from this software without specific prior written permission.
21ca987d46SWarner Losh  *
22ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23ca987d46SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24ca987d46SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25ca987d46SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26ca987d46SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27ca987d46SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28ca987d46SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29ca987d46SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30ca987d46SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31ca987d46SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32ca987d46SWarner Losh  */
33ca987d46SWarner Losh 
34ca987d46SWarner Losh /*
35ca987d46SWarner Losh  * Simple TFTP implementation for libsa.
36ca987d46SWarner Losh  * Assumes:
3798e805b4SToomas Soome  *  - socket descriptor (int) at dev->d_opendata, dev stored at
3898e805b4SToomas Soome  *	open_file->f_devdata
391b1bb6f1SToomas Soome  *  - server host IP in global rootip
40ca987d46SWarner Losh  * Restrictions:
41ca987d46SWarner Losh  *  - read only
42ca987d46SWarner Losh  *  - lseek only with SEEK_SET or SEEK_CUR
43ca987d46SWarner Losh  *  - no big time differences between transfers (<tftp timeout)
44ca987d46SWarner Losh  */
45ca987d46SWarner Losh 
46ca987d46SWarner Losh #include <sys/types.h>
47ca987d46SWarner Losh #include <sys/stat.h>
48ca987d46SWarner Losh #include <netinet/in.h>
49ca987d46SWarner Losh #include <netinet/udp.h>
50ca987d46SWarner Losh #include <netinet/in_systm.h>
51ca987d46SWarner Losh #include <arpa/tftp.h>
52ca987d46SWarner Losh 
53ca987d46SWarner Losh #include <string.h>
54ca987d46SWarner Losh 
55ca987d46SWarner Losh #include "stand.h"
56ca987d46SWarner Losh #include "net.h"
57ca987d46SWarner Losh #include "netif.h"
58ca987d46SWarner Losh 
59ca987d46SWarner Losh #include "tftp.h"
60ca987d46SWarner Losh 
61ca987d46SWarner Losh struct tftp_handle;
62c5b86c3bSKyle Evans struct tftprecv_extra;
63ca987d46SWarner Losh 
643c3779dcSToomas Soome static ssize_t recvtftp(struct iodesc *, void **, void **, time_t, void *);
653c3779dcSToomas Soome static int tftp_open(const char *, struct open_file *);
663c3779dcSToomas Soome static int tftp_close(struct open_file *);
673c3779dcSToomas Soome static int tftp_parse_oack(struct tftp_handle *, char *, size_t);
683c3779dcSToomas Soome static int tftp_read(struct open_file *, void *, size_t, size_t *);
693c3779dcSToomas Soome static off_t tftp_seek(struct open_file *, off_t, int);
703c3779dcSToomas Soome static int tftp_set_blksize(struct tftp_handle *, const char *);
713c3779dcSToomas Soome static int tftp_stat(struct open_file *, struct stat *);
723eb01900SEmmanuel Vadot static int tftp_preload(struct open_file *);
73ca987d46SWarner Losh 
74ca987d46SWarner Losh struct fs_ops tftp_fsops = {
753c3779dcSToomas Soome 	.fs_name = "tftp",
763c3779dcSToomas Soome 	.fo_open = tftp_open,
773c3779dcSToomas Soome 	.fo_close = tftp_close,
783c3779dcSToomas Soome 	.fo_read = tftp_read,
793c3779dcSToomas Soome 	.fo_write = null_write,
803c3779dcSToomas Soome 	.fo_seek = tftp_seek,
813c3779dcSToomas Soome 	.fo_stat = tftp_stat,
823eb01900SEmmanuel Vadot 	.fo_preload = tftp_preload,
833c3779dcSToomas Soome 	.fo_readdir = null_readdir
84ca987d46SWarner Losh };
85ca987d46SWarner Losh 
86ca987d46SWarner Losh static int	tftpport = 2000;
87ca987d46SWarner Losh static int	is_open = 0;
88ca987d46SWarner Losh 
89ca987d46SWarner Losh /*
90ca987d46SWarner Losh  * The legacy TFTP_BLKSIZE value was SEGSIZE(512).
91ca987d46SWarner Losh  * TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and
92ca987d46SWarner Losh  * IP header lengths).
93ca987d46SWarner Losh  */
94ca987d46SWarner Losh #define	TFTP_REQUESTED_BLKSIZE 1428
95ca987d46SWarner Losh 
96ca987d46SWarner Losh /*
97ca987d46SWarner Losh  * Choose a blksize big enough so we can test with Ethernet
98ca987d46SWarner Losh  * Jumbo frames in the future.
99ca987d46SWarner Losh  */
100ca987d46SWarner Losh #define	TFTP_MAX_BLKSIZE 9008
101723f9041SSimon J. Gerraty #define TFTP_TRIES 2
102ca987d46SWarner Losh 
103ca987d46SWarner Losh struct tftp_handle {
104ca987d46SWarner Losh 	struct iodesc  *iodesc;
105ca987d46SWarner Losh 	int		currblock;	/* contents of lastdata */
1061a3ccb8fSDimitry Andric 	unsigned int	islastblock:1;	/* flag */
1071a3ccb8fSDimitry Andric 	unsigned int	tries:4;	/* number of read attempts */
108ca987d46SWarner Losh 	int		validsize;
109ca987d46SWarner Losh 	int		off;
110ca987d46SWarner Losh 	char		*path;	/* saved for re-requests */
111ca987d46SWarner Losh 	unsigned int	tftp_blksize;
112ca987d46SWarner Losh 	unsigned long	tftp_tsize;
113ca987d46SWarner Losh 	void		*pkt;
114ca987d46SWarner Losh 	struct tftphdr	*tftp_hdr;
1154f36ed51SEmmanuel Vadot 	char		*tftp_cache;
1164f36ed51SEmmanuel Vadot 	bool		lastacksent;
117ca987d46SWarner Losh };
118ca987d46SWarner Losh 
119c5b86c3bSKyle Evans struct tftprecv_extra {
120c5b86c3bSKyle Evans 	struct tftp_handle	*tftp_handle;
121c5b86c3bSKyle Evans 	unsigned short		rtype;		/* Received type */
122c5b86c3bSKyle Evans };
123c5b86c3bSKyle Evans 
124ca987d46SWarner Losh #define	TFTP_MAX_ERRCODE EOPTNEG
125ca987d46SWarner Losh static const int tftperrors[TFTP_MAX_ERRCODE + 1] = {
126bf07f2f8SEmmanuel Vadot 	0,			/* NAK */
127ca987d46SWarner Losh 	ENOENT,
128ca987d46SWarner Losh 	EPERM,
129ca987d46SWarner Losh 	ENOSPC,
130ca987d46SWarner Losh 	EINVAL,			/* ??? */
131ca987d46SWarner Losh 	EINVAL,			/* ??? */
132ca987d46SWarner Losh 	EEXIST,
133ca987d46SWarner Losh 	EINVAL,			/* ??? */
134ca987d46SWarner Losh 	EINVAL,			/* Option negotiation failed. */
135ca987d46SWarner Losh };
136ca987d46SWarner Losh 
137ca987d46SWarner Losh static int  tftp_getnextblock(struct tftp_handle *h);
138ca987d46SWarner Losh 
139ca987d46SWarner Losh /* send error message back. */
140ca987d46SWarner Losh static void
tftp_senderr(struct tftp_handle * h,u_short errcode,const char * msg)141ca987d46SWarner Losh tftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg)
142ca987d46SWarner Losh {
143ca987d46SWarner Losh 	struct {
144ca987d46SWarner Losh 		u_char header[HEADER_SIZE];
145ca987d46SWarner Losh 		struct tftphdr t;
146ca987d46SWarner Losh 		u_char space[63]; /* +1 from t */
147ca987d46SWarner Losh 	} __packed __aligned(4) wbuf;
148ca987d46SWarner Losh 	char *wtail;
149ca987d46SWarner Losh 	int len;
150ca987d46SWarner Losh 
151ca987d46SWarner Losh 	len = strlen(msg);
152ca987d46SWarner Losh 	if (len > sizeof(wbuf.space))
153ca987d46SWarner Losh 		len = sizeof(wbuf.space);
154ca987d46SWarner Losh 
155ca987d46SWarner Losh 	wbuf.t.th_opcode = htons((u_short)ERROR);
156ca987d46SWarner Losh 	wbuf.t.th_code = htons(errcode);
157ca987d46SWarner Losh 
158ca987d46SWarner Losh 	wtail = wbuf.t.th_msg;
159ca987d46SWarner Losh 	bcopy(msg, wtail, len);
160ca987d46SWarner Losh 	wtail[len] = '\0';
161ca987d46SWarner Losh 	wtail += len + 1;
162ca987d46SWarner Losh 
163ca987d46SWarner Losh 	sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
164ca987d46SWarner Losh }
165ca987d46SWarner Losh 
166ca987d46SWarner Losh static void
tftp_sendack(struct tftp_handle * h,u_short block)167bb489cd7SToomas Soome tftp_sendack(struct tftp_handle *h, u_short block)
168ca987d46SWarner Losh {
169ca987d46SWarner Losh 	struct {
170ca987d46SWarner Losh 		u_char header[HEADER_SIZE];
171ca987d46SWarner Losh 		struct tftphdr  t;
172ca987d46SWarner Losh 	} __packed __aligned(4) wbuf;
173ca987d46SWarner Losh 	char *wtail;
174ca987d46SWarner Losh 
175ca987d46SWarner Losh 	wbuf.t.th_opcode = htons((u_short)ACK);
176ca987d46SWarner Losh 	wtail = (char *)&wbuf.t.th_block;
177bb489cd7SToomas Soome 	wbuf.t.th_block = htons(block);
178ca987d46SWarner Losh 	wtail += 2;
179ca987d46SWarner Losh 
180ca987d46SWarner Losh 	sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
181ca987d46SWarner Losh }
182ca987d46SWarner Losh 
183ca987d46SWarner Losh static ssize_t
recvtftp(struct iodesc * d,void ** pkt,void ** payload,time_t tleft,void * recv_extra)184c5b86c3bSKyle Evans recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
185c5b86c3bSKyle Evans     void *recv_extra)
186ca987d46SWarner Losh {
187c5b86c3bSKyle Evans 	struct tftprecv_extra *extra;
188c5b86c3bSKyle Evans 	struct tftp_handle *h;
189ca987d46SWarner Losh 	struct tftphdr *t;
190ca987d46SWarner Losh 	void *ptr = NULL;
191ca987d46SWarner Losh 	ssize_t len;
192bf07f2f8SEmmanuel Vadot 	int tftp_error;
193ca987d46SWarner Losh 
194ca987d46SWarner Losh 	errno = 0;
1953c3779dcSToomas Soome 	extra = recv_extra;
196c5b86c3bSKyle Evans 	h = extra->tftp_handle;
197ca987d46SWarner Losh 
198ca987d46SWarner Losh 	len = readudp(d, &ptr, (void **)&t, tleft);
199ca987d46SWarner Losh 
200ca987d46SWarner Losh 	if (len < 4) {
201ca987d46SWarner Losh 		free(ptr);
202ca987d46SWarner Losh 		return (-1);
203ca987d46SWarner Losh 	}
204ca987d46SWarner Losh 
205c5b86c3bSKyle Evans 	extra->rtype = ntohs(t->th_opcode);
206ca987d46SWarner Losh 	switch (ntohs(t->th_opcode)) {
207ca987d46SWarner Losh 	case DATA: {
208ca987d46SWarner Losh 		int got;
209ca987d46SWarner Losh 
210bb489cd7SToomas Soome 		if (htons(t->th_block) < (u_short)d->xid) {
211bb489cd7SToomas Soome 			/*
212bb489cd7SToomas Soome 			 * Apparently our ACK was missed, re-send.
213bb489cd7SToomas Soome 			 */
214bb489cd7SToomas Soome 			tftp_sendack(h, htons(t->th_block));
215bb489cd7SToomas Soome 			free(ptr);
216bb489cd7SToomas Soome 			return (-1);
217bb489cd7SToomas Soome 		}
218ca987d46SWarner Losh 		if (htons(t->th_block) != (u_short)d->xid) {
219ca987d46SWarner Losh 			/*
220bb489cd7SToomas Soome 			 * Packet from the future, drop this.
221ca987d46SWarner Losh 			 */
222ca987d46SWarner Losh 			free(ptr);
223ca987d46SWarner Losh 			return (-1);
224ca987d46SWarner Losh 		}
225ca987d46SWarner Losh 		if (d->xid == 1) {
226ca987d46SWarner Losh 			/*
227ca987d46SWarner Losh 			 * First data packet from new port.
228ca987d46SWarner Losh 			 */
229ca987d46SWarner Losh 			struct udphdr *uh;
230ca987d46SWarner Losh 			uh = (struct udphdr *)t - 1;
231ca987d46SWarner Losh 			d->destport = uh->uh_sport;
232bb489cd7SToomas Soome 		}
233ca987d46SWarner Losh 		got = len - (t->th_data - (char *)t);
234ca987d46SWarner Losh 		*pkt = ptr;
235ca987d46SWarner Losh 		*payload = t;
236ca987d46SWarner Losh 		return (got);
237ca987d46SWarner Losh 	}
238ca987d46SWarner Losh 	case ERROR:
239bf07f2f8SEmmanuel Vadot 		tftp_error = ntohs(t->th_code);
240bf07f2f8SEmmanuel Vadot 		if ((unsigned)tftp_error > TFTP_MAX_ERRCODE) {
241bf07f2f8SEmmanuel Vadot 			printf("illegal tftp error %d\n", tftp_error);
242ca987d46SWarner Losh 			errno = EIO;
243ca987d46SWarner Losh 		} else {
244ca987d46SWarner Losh #ifdef TFTP_DEBUG
245bf07f2f8SEmmanuel Vadot 			printf("tftp-error %d\n", tftp_error);
246ca987d46SWarner Losh #endif
247bf07f2f8SEmmanuel Vadot 			errno = tftperrors[tftp_error];
248ca987d46SWarner Losh 		}
249ca987d46SWarner Losh 		free(ptr);
250bf07f2f8SEmmanuel Vadot 		/* If we got a NAK return 0, it's usually a directory */
251bf07f2f8SEmmanuel Vadot 		if (tftp_error == 0)
252bf07f2f8SEmmanuel Vadot 			return (0);
253ca987d46SWarner Losh 		return (-1);
254ca987d46SWarner Losh 	case OACK: {
255ca987d46SWarner Losh 		struct udphdr *uh;
256ca987d46SWarner Losh 		int tftp_oack_len;
257ca987d46SWarner Losh 
258ca987d46SWarner Losh 		/*
259ca987d46SWarner Losh 		 * Unexpected OACK. TFTP transfer already in progress.
260ca987d46SWarner Losh 		 * Drop the pkt.
261ca987d46SWarner Losh 		 */
262ca987d46SWarner Losh 		if (d->xid != 1) {
263ca987d46SWarner Losh 			free(ptr);
264ca987d46SWarner Losh 			return (-1);
265ca987d46SWarner Losh 		}
266ca987d46SWarner Losh 
267ca987d46SWarner Losh 		/*
268ca987d46SWarner Losh 		 * Remember which port this OACK came from, because we need
269ca987d46SWarner Losh 		 * to send the ACK or errors back to it.
270ca987d46SWarner Losh 		 */
271ca987d46SWarner Losh 		uh = (struct udphdr *)t - 1;
272ca987d46SWarner Losh 		d->destport = uh->uh_sport;
273ca987d46SWarner Losh 
274ca987d46SWarner Losh 		/* Parse options ACK-ed by the server. */
275ca987d46SWarner Losh 		tftp_oack_len = len - sizeof(t->th_opcode);
276ca987d46SWarner Losh 		if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) {
277ca987d46SWarner Losh 			tftp_senderr(h, EOPTNEG, "Malformed OACK");
278ca987d46SWarner Losh 			errno = EIO;
279ca987d46SWarner Losh 			free(ptr);
280ca987d46SWarner Losh 			return (-1);
281ca987d46SWarner Losh 		}
282ca987d46SWarner Losh 		*pkt = ptr;
283ca987d46SWarner Losh 		*payload = t;
284ca987d46SWarner Losh 		return (0);
285ca987d46SWarner Losh 	}
286ca987d46SWarner Losh 	default:
287ca987d46SWarner Losh #ifdef TFTP_DEBUG
288ca987d46SWarner Losh 		printf("tftp type %d not handled\n", ntohs(t->th_opcode));
289ca987d46SWarner Losh #endif
290ca987d46SWarner Losh 		free(ptr);
291ca987d46SWarner Losh 		return (-1);
292ca987d46SWarner Losh 	}
293ca987d46SWarner Losh }
294ca987d46SWarner Losh 
295ca987d46SWarner Losh /* send request, expect first block (or error) */
296ca987d46SWarner Losh static int
tftp_makereq(struct tftp_handle * h)297ca987d46SWarner Losh tftp_makereq(struct tftp_handle *h)
298ca987d46SWarner Losh {
299ca987d46SWarner Losh 	struct {
300ca987d46SWarner Losh 		u_char header[HEADER_SIZE];
301ca987d46SWarner Losh 		struct tftphdr  t;
302ca987d46SWarner Losh 		u_char space[FNAME_SIZE + 6];
303ca987d46SWarner Losh 	} __packed __aligned(4) wbuf;
304c5b86c3bSKyle Evans 	struct tftprecv_extra recv_extra;
305ca987d46SWarner Losh 	char *wtail;
306ca987d46SWarner Losh 	int l;
307ca987d46SWarner Losh 	ssize_t res;
308ca987d46SWarner Losh 	void *pkt;
309ca987d46SWarner Losh 	struct tftphdr *t;
310ca987d46SWarner Losh 	char *tftp_blksize = NULL;
311ca987d46SWarner Losh 	int blksize_l;
312ca987d46SWarner Losh 
313ca987d46SWarner Losh 	/*
314ca987d46SWarner Losh 	 * Allow overriding default TFTP block size by setting
315ca987d46SWarner Losh 	 * a tftp.blksize environment variable.
316ca987d46SWarner Losh 	 */
317ca987d46SWarner Losh 	if ((tftp_blksize = getenv("tftp.blksize")) != NULL) {
318ca987d46SWarner Losh 		tftp_set_blksize(h, tftp_blksize);
319ca987d46SWarner Losh 	}
320ca987d46SWarner Losh 
321ca987d46SWarner Losh 	wbuf.t.th_opcode = htons((u_short)RRQ);
322ca987d46SWarner Losh 	wtail = wbuf.t.th_stuff;
323ca987d46SWarner Losh 	l = strlen(h->path);
324ca987d46SWarner Losh #ifdef TFTP_PREPEND_PATH
325ca987d46SWarner Losh 	if (l > FNAME_SIZE - (sizeof(TFTP_PREPEND_PATH) - 1))
326ca987d46SWarner Losh 		return (ENAMETOOLONG);
327ca987d46SWarner Losh 	bcopy(TFTP_PREPEND_PATH, wtail, sizeof(TFTP_PREPEND_PATH) - 1);
328ca987d46SWarner Losh 	wtail += sizeof(TFTP_PREPEND_PATH) - 1;
329ca987d46SWarner Losh #else
330ca987d46SWarner Losh 	if (l > FNAME_SIZE)
331ca987d46SWarner Losh 		return (ENAMETOOLONG);
332ca987d46SWarner Losh #endif
333ca987d46SWarner Losh 	bcopy(h->path, wtail, l + 1);
334ca987d46SWarner Losh 	wtail += l + 1;
335ca987d46SWarner Losh 	bcopy("octet", wtail, 6);
336ca987d46SWarner Losh 	wtail += 6;
337ca987d46SWarner Losh 	bcopy("blksize", wtail, 8);
338ca987d46SWarner Losh 	wtail += 8;
339ca987d46SWarner Losh 	blksize_l = sprintf(wtail, "%d", h->tftp_blksize);
340ca987d46SWarner Losh 	wtail += blksize_l + 1;
341ca987d46SWarner Losh 	bcopy("tsize", wtail, 6);
342ca987d46SWarner Losh 	wtail += 6;
343ca987d46SWarner Losh 	bcopy("0", wtail, 2);
344ca987d46SWarner Losh 	wtail += 2;
345ca987d46SWarner Losh 
346ca987d46SWarner Losh 	h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
347ca987d46SWarner Losh 	h->iodesc->destport = htons(IPPORT_TFTP);
348ca987d46SWarner Losh 	h->iodesc->xid = 1;	/* expected block */
349ca987d46SWarner Losh 
350ca987d46SWarner Losh 	h->currblock = 0;
351ca987d46SWarner Losh 	h->islastblock = 0;
352ca987d46SWarner Losh 	h->validsize = 0;
353ca987d46SWarner Losh 
354ca987d46SWarner Losh 	pkt = NULL;
355c5b86c3bSKyle Evans 	recv_extra.tftp_handle = h;
356c5b86c3bSKyle Evans 	res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t,
3573c3779dcSToomas Soome 	    &recvtftp, &pkt, (void **)&t, &recv_extra);
358ca987d46SWarner Losh 	if (res == -1) {
359ca987d46SWarner Losh 		free(pkt);
360ca987d46SWarner Losh 		return (errno);
361ca987d46SWarner Losh 	}
362ca987d46SWarner Losh 
363ca987d46SWarner Losh 	free(h->pkt);
364ca987d46SWarner Losh 	h->pkt = pkt;
365ca987d46SWarner Losh 	h->tftp_hdr = t;
366ca987d46SWarner Losh 
367c5b86c3bSKyle Evans 	if (recv_extra.rtype == OACK)
368ca987d46SWarner Losh 		return (tftp_getnextblock(h));
369ca987d46SWarner Losh 
370ca987d46SWarner Losh 	/* Server ignored our blksize request, revert to TFTP default. */
371ca987d46SWarner Losh 	h->tftp_blksize = SEGSIZE;
372ca987d46SWarner Losh 
373c5b86c3bSKyle Evans 	switch (recv_extra.rtype) {
374ca987d46SWarner Losh 		case DATA: {
375ca987d46SWarner Losh 			h->currblock = 1;
376ca987d46SWarner Losh 			h->validsize = res;
377ca987d46SWarner Losh 			h->islastblock = 0;
378ca987d46SWarner Losh 			if (res < h->tftp_blksize) {
379ca987d46SWarner Losh 				h->islastblock = 1;	/* very short file */
380bb489cd7SToomas Soome 				tftp_sendack(h, h->currblock);
3814f36ed51SEmmanuel Vadot 				h->lastacksent = true;
382ca987d46SWarner Losh 			}
383ca987d46SWarner Losh 			return (0);
384ca987d46SWarner Losh 		}
385ca987d46SWarner Losh 		case ERROR:
386ca987d46SWarner Losh 		default:
387ca987d46SWarner Losh 			return (errno);
388ca987d46SWarner Losh 	}
389ca987d46SWarner Losh 
390ca987d46SWarner Losh }
391ca987d46SWarner Losh 
392ca987d46SWarner Losh /* ack block, expect next */
393ca987d46SWarner Losh static int
tftp_getnextblock(struct tftp_handle * h)394ca987d46SWarner Losh tftp_getnextblock(struct tftp_handle *h)
395ca987d46SWarner Losh {
396ca987d46SWarner Losh 	struct {
397ca987d46SWarner Losh 		u_char header[HEADER_SIZE];
398ca987d46SWarner Losh 		struct tftphdr t;
399ca987d46SWarner Losh 	} __packed __aligned(4) wbuf;
400c5b86c3bSKyle Evans 	struct tftprecv_extra recv_extra;
401ca987d46SWarner Losh 	char *wtail;
402ca987d46SWarner Losh 	int res;
403ca987d46SWarner Losh 	void *pkt;
404ca987d46SWarner Losh 	struct tftphdr *t;
4053c3779dcSToomas Soome 
406ca987d46SWarner Losh 	wbuf.t.th_opcode = htons((u_short)ACK);
407ca987d46SWarner Losh 	wtail = (char *)&wbuf.t.th_block;
408ca987d46SWarner Losh 	wbuf.t.th_block = htons((u_short)h->currblock);
409ca987d46SWarner Losh 	wtail += 2;
410ca987d46SWarner Losh 
411ca987d46SWarner Losh 	h->iodesc->xid = h->currblock + 1;	/* expected block */
412ca987d46SWarner Losh 
413ca987d46SWarner Losh 	pkt = NULL;
414c5b86c3bSKyle Evans 	recv_extra.tftp_handle = h;
415c5b86c3bSKyle Evans 	res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t,
4163c3779dcSToomas Soome 	    &recvtftp, &pkt, (void **)&t, &recv_extra);
417ca987d46SWarner Losh 
418ca987d46SWarner Losh 	if (res == -1) {		/* 0 is OK! */
419ca987d46SWarner Losh 		free(pkt);
420ca987d46SWarner Losh 		return (errno);
421ca987d46SWarner Losh 	}
422ca987d46SWarner Losh 
423ca987d46SWarner Losh 	free(h->pkt);
424ca987d46SWarner Losh 	h->pkt = pkt;
425ca987d46SWarner Losh 	h->tftp_hdr = t;
426ca987d46SWarner Losh 	h->currblock++;
427ca987d46SWarner Losh 	h->validsize = res;
428ca987d46SWarner Losh 	if (res < h->tftp_blksize)
429ca987d46SWarner Losh 		h->islastblock = 1;	/* EOF */
430ca987d46SWarner Losh 
431ca987d46SWarner Losh 	if (h->islastblock == 1) {
432ca987d46SWarner Losh 		/* Send an ACK for the last block */
433ca987d46SWarner Losh 		wbuf.t.th_block = htons((u_short)h->currblock);
434ca987d46SWarner Losh 		sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
435ca987d46SWarner Losh 	}
436ca987d46SWarner Losh 
437ca987d46SWarner Losh 	return (0);
438ca987d46SWarner Losh }
439ca987d46SWarner Losh 
440ca987d46SWarner Losh static int
tftp_open(const char * path,struct open_file * f)441ca987d46SWarner Losh tftp_open(const char *path, struct open_file *f)
442ca987d46SWarner Losh {
44398e805b4SToomas Soome 	struct devdesc *dev;
444ca987d46SWarner Losh 	struct tftp_handle *tftpfile;
445ca987d46SWarner Losh 	struct iodesc	*io;
446ca987d46SWarner Losh 	int		res;
447ca987d46SWarner Losh 	size_t		pathsize;
448ca987d46SWarner Losh 	const char	*extraslash;
449ca987d46SWarner Losh 
450ca987d46SWarner Losh 	if (netproto != NET_TFTP)
451ca987d46SWarner Losh 		return (EINVAL);
452ca987d46SWarner Losh 
453ca987d46SWarner Losh 	if (f->f_dev->dv_type != DEVT_NET)
454ca987d46SWarner Losh 		return (EINVAL);
455ca987d46SWarner Losh 
456ca987d46SWarner Losh 	if (is_open)
457ca987d46SWarner Losh 		return (EBUSY);
458ca987d46SWarner Losh 
459c6588669SToomas Soome 	tftpfile = calloc(1, sizeof(*tftpfile));
460ca987d46SWarner Losh 	if (!tftpfile)
461ca987d46SWarner Losh 		return (ENOMEM);
462ca987d46SWarner Losh 
463ca987d46SWarner Losh 	tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE;
46498e805b4SToomas Soome 	dev = f->f_devdata;
46598e805b4SToomas Soome 	tftpfile->iodesc = io = socktodesc(*(int *)(dev->d_opendata));
4667ee96df3SToomas Soome 	if (io == NULL) {
4677ee96df3SToomas Soome 		free(tftpfile);
468ca987d46SWarner Losh 		return (EINVAL);
4697ee96df3SToomas Soome 	}
470ca987d46SWarner Losh 
4711b1bb6f1SToomas Soome 	io->destip = rootip;
472ca987d46SWarner Losh 	tftpfile->off = 0;
473ca987d46SWarner Losh 	pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof(char);
474ca987d46SWarner Losh 	tftpfile->path = malloc(pathsize);
475ca987d46SWarner Losh 	if (tftpfile->path == NULL) {
476ca987d46SWarner Losh 		free(tftpfile);
477ca987d46SWarner Losh 		return (ENOMEM);
478ca987d46SWarner Losh 	}
479ca987d46SWarner Losh 	if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/')
480ca987d46SWarner Losh 		extraslash = "";
481ca987d46SWarner Losh 	else
482ca987d46SWarner Losh 		extraslash = "/";
483ca987d46SWarner Losh 	res = snprintf(tftpfile->path, pathsize, "%s%s%s",
484ca987d46SWarner Losh 	    rootpath, extraslash, path);
485ca987d46SWarner Losh 	if (res < 0 || res > pathsize) {
486ca987d46SWarner Losh 		free(tftpfile->path);
487ca987d46SWarner Losh 		free(tftpfile);
488ca987d46SWarner Losh 		return (ENOMEM);
489ca987d46SWarner Losh 	}
490ca987d46SWarner Losh 
491ca987d46SWarner Losh 	res = tftp_makereq(tftpfile);
492ca987d46SWarner Losh 
493ca987d46SWarner Losh 	if (res) {
494ca987d46SWarner Losh 		free(tftpfile->path);
495ca987d46SWarner Losh 		free(tftpfile->pkt);
496ca987d46SWarner Losh 		free(tftpfile);
497ca987d46SWarner Losh 		return (res);
498ca987d46SWarner Losh 	}
4993c3779dcSToomas Soome 	f->f_fsdata = tftpfile;
500ca987d46SWarner Losh 	is_open = 1;
501ca987d46SWarner Losh 	return (0);
502ca987d46SWarner Losh }
503ca987d46SWarner Losh 
504ca987d46SWarner Losh static int
tftp_read(struct open_file * f,void * addr,size_t size,size_t * resid)505ca987d46SWarner Losh tftp_read(struct open_file *f, void *addr, size_t size,
506ca987d46SWarner Losh     size_t *resid /* out */)
507ca987d46SWarner Losh {
508ca987d46SWarner Losh 	struct tftp_handle *tftpfile;
509f442898fSToomas Soome 	size_t res;
5107e63e808SToomas Soome 	int rc;
5117e63e808SToomas Soome 
5127e63e808SToomas Soome 	rc = 0;
513f442898fSToomas Soome 	res = size;
5143c3779dcSToomas Soome 	tftpfile = f->f_fsdata;
515ca987d46SWarner Losh 
516f442898fSToomas Soome 	/* Make sure we will not read past file end */
517f442898fSToomas Soome 	if (tftpfile->tftp_tsize > 0 &&
518f442898fSToomas Soome 	    tftpfile->off + size > tftpfile->tftp_tsize) {
519f442898fSToomas Soome 		size = tftpfile->tftp_tsize - tftpfile->off;
520f442898fSToomas Soome 	}
521f442898fSToomas Soome 
5223eb01900SEmmanuel Vadot 	if (tftpfile->tftp_cache != NULL) {
5233eb01900SEmmanuel Vadot 		bcopy(tftpfile->tftp_cache + tftpfile->off,
5243eb01900SEmmanuel Vadot 		    addr, size);
5253eb01900SEmmanuel Vadot 
5263eb01900SEmmanuel Vadot 		addr = (char *)addr + size;
5273eb01900SEmmanuel Vadot 		tftpfile->off += size;
5283eb01900SEmmanuel Vadot 		res -= size;
5293eb01900SEmmanuel Vadot 		goto out;
5303eb01900SEmmanuel Vadot 	}
5313eb01900SEmmanuel Vadot 
532ca987d46SWarner Losh 	while (size > 0) {
533ca987d46SWarner Losh 		int needblock, count;
534ca987d46SWarner Losh 
535ca987d46SWarner Losh 		twiddle(32);
536ca987d46SWarner Losh 
537ca987d46SWarner Losh 		needblock = tftpfile->off / tftpfile->tftp_blksize + 1;
538ca987d46SWarner Losh 
539ca987d46SWarner Losh 		if (tftpfile->currblock > needblock) {	/* seek backwards */
540ca987d46SWarner Losh 			tftp_senderr(tftpfile, 0, "No error: read aborted");
5417e63e808SToomas Soome 			rc = tftp_makereq(tftpfile);
5427e63e808SToomas Soome 			if (rc != 0)
5437e63e808SToomas Soome 				break;
544ca987d46SWarner Losh 		}
545ca987d46SWarner Losh 
546ca987d46SWarner Losh 		while (tftpfile->currblock < needblock) {
547ca987d46SWarner Losh 
5487e63e808SToomas Soome 			rc = tftp_getnextblock(tftpfile);
5497e63e808SToomas Soome 			if (rc) {	/* no answer */
550ca987d46SWarner Losh #ifdef TFTP_DEBUG
551ca987d46SWarner Losh 				printf("tftp: read error\n");
552ca987d46SWarner Losh #endif
553723f9041SSimon J. Gerraty 				if (tftpfile->tries > TFTP_TRIES) {
5547e63e808SToomas Soome 					return (rc);
555723f9041SSimon J. Gerraty 				} else {
556723f9041SSimon J. Gerraty 					tftpfile->tries++;
557723f9041SSimon J. Gerraty 					tftp_makereq(tftpfile);
558723f9041SSimon J. Gerraty 				}
559ca987d46SWarner Losh 			}
560ca987d46SWarner Losh 			if (tftpfile->islastblock)
561ca987d46SWarner Losh 				break;
562ca987d46SWarner Losh 		}
563ca987d46SWarner Losh 
564ca987d46SWarner Losh 		if (tftpfile->currblock == needblock) {
565ca987d46SWarner Losh 			int offinblock, inbuffer;
566ca987d46SWarner Losh 
567ca987d46SWarner Losh 			offinblock = tftpfile->off % tftpfile->tftp_blksize;
568ca987d46SWarner Losh 
569ca987d46SWarner Losh 			inbuffer = tftpfile->validsize - offinblock;
570ca987d46SWarner Losh 			if (inbuffer < 0) {
571ca987d46SWarner Losh #ifdef TFTP_DEBUG
572ca987d46SWarner Losh 				printf("tftp: invalid offset %d\n",
573ca987d46SWarner Losh 				    tftpfile->off);
574ca987d46SWarner Losh #endif
575ca987d46SWarner Losh 				return (EINVAL);
576ca987d46SWarner Losh 			}
577ca987d46SWarner Losh 			count = (size < inbuffer ? size : inbuffer);
578ca987d46SWarner Losh 			bcopy(tftpfile->tftp_hdr->th_data + offinblock,
579ca987d46SWarner Losh 			    addr, count);
580ca987d46SWarner Losh 
581ca987d46SWarner Losh 			addr = (char *)addr + count;
582ca987d46SWarner Losh 			tftpfile->off += count;
583ca987d46SWarner Losh 			size -= count;
584f442898fSToomas Soome 			res -= count;
585ca987d46SWarner Losh 
586ca987d46SWarner Losh 			if ((tftpfile->islastblock) && (count == inbuffer))
587ca987d46SWarner Losh 				break;	/* EOF */
588ca987d46SWarner Losh 		} else {
589ca987d46SWarner Losh #ifdef TFTP_DEBUG
590ca987d46SWarner Losh 			printf("tftp: block %d not found\n", needblock);
591ca987d46SWarner Losh #endif
592ca987d46SWarner Losh 			return (EINVAL);
593ca987d46SWarner Losh 		}
594ca987d46SWarner Losh 
595ca987d46SWarner Losh 	}
596ca987d46SWarner Losh 
5973eb01900SEmmanuel Vadot out:
598f442898fSToomas Soome 	if (resid != NULL)
599f442898fSToomas Soome 		*resid = res;
6007e63e808SToomas Soome 	return (rc);
601ca987d46SWarner Losh }
602ca987d46SWarner Losh 
603ca987d46SWarner Losh static int
tftp_close(struct open_file * f)604ca987d46SWarner Losh tftp_close(struct open_file *f)
605ca987d46SWarner Losh {
606ca987d46SWarner Losh 	struct tftp_handle *tftpfile;
6073c3779dcSToomas Soome 	tftpfile = f->f_fsdata;
608ca987d46SWarner Losh 
6094f36ed51SEmmanuel Vadot 	if (tftpfile->lastacksent == false)
6104f36ed51SEmmanuel Vadot 		tftp_senderr(tftpfile, 0, "No error: file closed");
611ca987d46SWarner Losh 
612ca987d46SWarner Losh 	if (tftpfile) {
613ca987d46SWarner Losh 		free(tftpfile->path);
614ca987d46SWarner Losh 		free(tftpfile->pkt);
6153eb01900SEmmanuel Vadot 		free(tftpfile->tftp_cache);
616ca987d46SWarner Losh 		free(tftpfile);
617ca987d46SWarner Losh 	}
618ca987d46SWarner Losh 	is_open = 0;
619ca987d46SWarner Losh 	return (0);
620ca987d46SWarner Losh }
621ca987d46SWarner Losh 
622ca987d46SWarner Losh static int
tftp_stat(struct open_file * f,struct stat * sb)623ca987d46SWarner Losh tftp_stat(struct open_file *f, struct stat *sb)
624ca987d46SWarner Losh {
625ca987d46SWarner Losh 	struct tftp_handle *tftpfile;
6263c3779dcSToomas Soome 	tftpfile = f->f_fsdata;
627ca987d46SWarner Losh 
628ca987d46SWarner Losh 	sb->st_mode = 0444 | S_IFREG;
629ca987d46SWarner Losh 	sb->st_nlink = 1;
630ca987d46SWarner Losh 	sb->st_uid = 0;
631ca987d46SWarner Losh 	sb->st_gid = 0;
6323c3779dcSToomas Soome 	sb->st_size = tftpfile->tftp_tsize;
633ca987d46SWarner Losh 	return (0);
634ca987d46SWarner Losh }
635ca987d46SWarner Losh 
636ca987d46SWarner Losh static off_t
tftp_seek(struct open_file * f,off_t offset,int where)637ca987d46SWarner Losh tftp_seek(struct open_file *f, off_t offset, int where)
638ca987d46SWarner Losh {
639ca987d46SWarner Losh 	struct tftp_handle *tftpfile;
6403c3779dcSToomas Soome 	tftpfile = f->f_fsdata;
641ca987d46SWarner Losh 
642ca987d46SWarner Losh 	switch (where) {
643ca987d46SWarner Losh 	case SEEK_SET:
644ca987d46SWarner Losh 		tftpfile->off = offset;
645ca987d46SWarner Losh 		break;
646ca987d46SWarner Losh 	case SEEK_CUR:
647ca987d46SWarner Losh 		tftpfile->off += offset;
648ca987d46SWarner Losh 		break;
649ca987d46SWarner Losh 	default:
650ca987d46SWarner Losh 		errno = EOFFSET;
651ca987d46SWarner Losh 		return (-1);
652ca987d46SWarner Losh 	}
653ca987d46SWarner Losh 	return (tftpfile->off);
654ca987d46SWarner Losh }
655ca987d46SWarner Losh 
656ca987d46SWarner Losh static int
tftp_preload(struct open_file * f)6573eb01900SEmmanuel Vadot tftp_preload(struct open_file *f)
6583eb01900SEmmanuel Vadot {
6593eb01900SEmmanuel Vadot 	struct tftp_handle *tftpfile;
6603eb01900SEmmanuel Vadot 	char *cache;
6613eb01900SEmmanuel Vadot 	int rc;
6623eb01900SEmmanuel Vadot #ifdef TFTP_DEBUG
6633eb01900SEmmanuel Vadot 	time_t start, end;
6643eb01900SEmmanuel Vadot #endif
6653eb01900SEmmanuel Vadot 
6663eb01900SEmmanuel Vadot 	tftpfile = f->f_fsdata;
6673eb01900SEmmanuel Vadot 	cache = malloc(sizeof(char) * tftpfile->tftp_tsize);
6683eb01900SEmmanuel Vadot 	if (cache == NULL) {
6693eb01900SEmmanuel Vadot 		printf("Couldn't allocate %ju bytes for preload caching"
6703eb01900SEmmanuel Vadot 		    ", disabling caching\n",
6713eb01900SEmmanuel Vadot 		    (uintmax_t)sizeof(char) * tftpfile->tftp_tsize);
6723eb01900SEmmanuel Vadot 		return (-1);
6733eb01900SEmmanuel Vadot 	}
6743eb01900SEmmanuel Vadot 
6753eb01900SEmmanuel Vadot #ifdef TFTP_DEBUG
6763eb01900SEmmanuel Vadot 	start = getsecs();
6773eb01900SEmmanuel Vadot 	printf("Preloading %s ", tftpfile->path);
6783eb01900SEmmanuel Vadot #endif
679dfc9c1d4SEmmanuel Vadot 	if (tftpfile->currblock == 1)
680dfc9c1d4SEmmanuel Vadot 		bcopy(tftpfile->tftp_hdr->th_data,
681dfc9c1d4SEmmanuel Vadot 		    cache,
682dfc9c1d4SEmmanuel Vadot 		    tftpfile->validsize);
683dfc9c1d4SEmmanuel Vadot 	else
684dfc9c1d4SEmmanuel Vadot 		tftpfile->currblock = 0;
685dfc9c1d4SEmmanuel Vadot 
6863eb01900SEmmanuel Vadot 	while (tftpfile->islastblock == 0) {
6873eb01900SEmmanuel Vadot 		twiddle(32);
6883eb01900SEmmanuel Vadot 		rc = tftp_getnextblock(tftpfile);
6893eb01900SEmmanuel Vadot 		if (rc) {
6903eb01900SEmmanuel Vadot 			free(cache);
6913eb01900SEmmanuel Vadot 			printf("Got TFTP error %d, disabling caching\n", rc);
6923eb01900SEmmanuel Vadot 			return (rc);
6933eb01900SEmmanuel Vadot 		}
6943eb01900SEmmanuel Vadot 		bcopy(tftpfile->tftp_hdr->th_data,
6953eb01900SEmmanuel Vadot 		    cache + (tftpfile->tftp_blksize * (tftpfile->currblock - 1)),
6963eb01900SEmmanuel Vadot 		    tftpfile->validsize);
6973eb01900SEmmanuel Vadot 	}
6983eb01900SEmmanuel Vadot #ifdef TFTP_DEBUG
6993eb01900SEmmanuel Vadot 	end = getsecs();
7003eb01900SEmmanuel Vadot 	printf("\nPreloaded %s (%ju bytes) during %jd seconds\n",
7013eb01900SEmmanuel Vadot 	    tftpfile->path, (intmax_t)tftpfile->tftp_tsize,
7023eb01900SEmmanuel Vadot 	    (intmax_t)end - start);
7033eb01900SEmmanuel Vadot #endif
7043eb01900SEmmanuel Vadot 
7053eb01900SEmmanuel Vadot 	tftpfile->tftp_cache = cache;
7063eb01900SEmmanuel Vadot 	return (0);
7073eb01900SEmmanuel Vadot }
7083eb01900SEmmanuel Vadot 
7093eb01900SEmmanuel Vadot static int
tftp_set_blksize(struct tftp_handle * h,const char * str)710ca987d46SWarner Losh tftp_set_blksize(struct tftp_handle *h, const char *str)
711ca987d46SWarner Losh {
712ca987d46SWarner Losh 	char *endptr;
713ca987d46SWarner Losh 	int new_blksize;
714ca987d46SWarner Losh 	int ret = 0;
715ca987d46SWarner Losh 
716ca987d46SWarner Losh 	if (h == NULL || str == NULL)
717ca987d46SWarner Losh 		return (ret);
718ca987d46SWarner Losh 
719ca987d46SWarner Losh 	new_blksize =
720ca987d46SWarner Losh 	    (unsigned int)strtol(str, &endptr, 0);
721ca987d46SWarner Losh 
722ca987d46SWarner Losh 	/*
723ca987d46SWarner Losh 	 * Only accept blksize value if it is numeric.
724ca987d46SWarner Losh 	 * RFC2348 specifies that acceptable values are 8-65464.
725ca987d46SWarner Losh 	 * Let's choose a limit less than MAXRSPACE.
726ca987d46SWarner Losh 	 */
7273c3779dcSToomas Soome 	if (*endptr == '\0' && new_blksize >= 8 &&
7283c3779dcSToomas Soome 	    new_blksize <= TFTP_MAX_BLKSIZE) {
729ca987d46SWarner Losh 		h->tftp_blksize = new_blksize;
730ca987d46SWarner Losh 		ret = 1;
731ca987d46SWarner Losh 	}
732ca987d46SWarner Losh 
733ca987d46SWarner Losh 	return (ret);
734ca987d46SWarner Losh }
735ca987d46SWarner Losh 
736ca987d46SWarner Losh /*
737ca987d46SWarner Losh  * In RFC2347, the TFTP Option Acknowledgement package (OACK)
738ca987d46SWarner Losh  * is used to acknowledge a client's option negotiation request.
739ca987d46SWarner Losh  * The format of an OACK packet is:
740ca987d46SWarner Losh  *    +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
741ca987d46SWarner Losh  *    |  opc  |  opt1  | 0 | value1 | 0 |  optN  | 0 | valueN | 0 |
742ca987d46SWarner Losh  *    +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
743ca987d46SWarner Losh  *
744ca987d46SWarner Losh  *    opc
745ca987d46SWarner Losh  *       The opcode field contains a 6, for Option Acknowledgment.
746ca987d46SWarner Losh  *
747ca987d46SWarner Losh  *    opt1
748ca987d46SWarner Losh  *       The first option acknowledgment, copied from the original
749ca987d46SWarner Losh  *       request.
750ca987d46SWarner Losh  *
751ca987d46SWarner Losh  *    value1
752ca987d46SWarner Losh  *       The acknowledged value associated with the first option.  If
753ca987d46SWarner Losh  *       and how this value may differ from the original request is
754ca987d46SWarner Losh  *       detailed in the specification for the option.
755ca987d46SWarner Losh  *
756ca987d46SWarner Losh  *    optN, valueN
757ca987d46SWarner Losh  *       The final option/value acknowledgment pair.
758ca987d46SWarner Losh  */
759ca987d46SWarner Losh static int
tftp_parse_oack(struct tftp_handle * h,char * buf,size_t len)760ca987d46SWarner Losh tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
761ca987d46SWarner Losh {
762ca987d46SWarner Losh 	/*
763ca987d46SWarner Losh 	 *  We parse the OACK strings into an array
764ca987d46SWarner Losh 	 *  of name-value pairs.
765ca987d46SWarner Losh 	 */
766ca987d46SWarner Losh 	char *tftp_options[128] = { 0 };
767ca987d46SWarner Losh 	char *val = buf;
768ca987d46SWarner Losh 	int i = 0;
769ca987d46SWarner Losh 	int option_idx = 0;
770ca987d46SWarner Losh 	int blksize_is_set = 0;
771ca987d46SWarner Losh 	int tsize = 0;
772ca987d46SWarner Losh 
773ca987d46SWarner Losh 	unsigned int orig_blksize;
774ca987d46SWarner Losh 
775ca987d46SWarner Losh 	while (option_idx < 128 && i < len) {
776ca987d46SWarner Losh 		if (buf[i] == '\0') {
777ca987d46SWarner Losh 			if (&buf[i] > val) {
778ca987d46SWarner Losh 				tftp_options[option_idx] = val;
779ca987d46SWarner Losh 				val = &buf[i] + 1;
780ca987d46SWarner Losh 				++option_idx;
781ca987d46SWarner Losh 			}
782ca987d46SWarner Losh 		}
783ca987d46SWarner Losh 		++i;
784ca987d46SWarner Losh 	}
785ca987d46SWarner Losh 
786ca987d46SWarner Losh 	/* Save the block size we requested for sanity check later. */
787ca987d46SWarner Losh 	orig_blksize = h->tftp_blksize;
788ca987d46SWarner Losh 
789ca987d46SWarner Losh 	/*
790ca987d46SWarner Losh 	 * Parse individual TFTP options.
791ca987d46SWarner Losh 	 *    * "blksize" is specified in RFC2348.
792ca987d46SWarner Losh 	 *    * "tsize" is specified in RFC2349.
793ca987d46SWarner Losh 	 */
794ca987d46SWarner Losh 	for (i = 0; i < option_idx; i += 2) {
795ca987d46SWarner Losh 		if (strcasecmp(tftp_options[i], "blksize") == 0) {
796ca987d46SWarner Losh 			if (i + 1 < option_idx)
797ca987d46SWarner Losh 				blksize_is_set =
798ca987d46SWarner Losh 				    tftp_set_blksize(h, tftp_options[i + 1]);
799ca987d46SWarner Losh 		} else if (strcasecmp(tftp_options[i], "tsize") == 0) {
800ca987d46SWarner Losh 			if (i + 1 < option_idx)
8013c3779dcSToomas Soome 				tsize = strtol(tftp_options[i + 1], NULL, 10);
802ca987d46SWarner Losh 			if (tsize != 0)
803ca987d46SWarner Losh 				h->tftp_tsize = tsize;
804ca987d46SWarner Losh 		} else {
8053c3779dcSToomas Soome 			/*
8063c3779dcSToomas Soome 			 * Do not allow any options we did not expect to be
8073c3779dcSToomas Soome 			 * ACKed.
8083c3779dcSToomas Soome 			 */
8093c3779dcSToomas Soome 			printf("unexpected tftp option '%s'\n",
8103c3779dcSToomas Soome 			    tftp_options[i]);
811ca987d46SWarner Losh 			return (-1);
812ca987d46SWarner Losh 		}
813ca987d46SWarner Losh 	}
814ca987d46SWarner Losh 
815ca987d46SWarner Losh 	if (!blksize_is_set) {
816ca987d46SWarner Losh 		/*
817ca987d46SWarner Losh 		 * If TFTP blksize was not set, try defaulting
818ca987d46SWarner Losh 		 * to the legacy TFTP blksize of SEGSIZE(512)
819ca987d46SWarner Losh 		 */
820ca987d46SWarner Losh 		h->tftp_blksize = SEGSIZE;
821ca987d46SWarner Losh 	} else if (h->tftp_blksize > orig_blksize) {
822ca987d46SWarner Losh 		/*
823ca987d46SWarner Losh 		 * Server should not be proposing block sizes that
824ca987d46SWarner Losh 		 * exceed what we said we can handle.
825ca987d46SWarner Losh 		 */
826ca987d46SWarner Losh 		printf("unexpected blksize %u\n", h->tftp_blksize);
827ca987d46SWarner Losh 		return (-1);
828ca987d46SWarner Losh 	}
829ca987d46SWarner Losh 
830ca987d46SWarner Losh #ifdef TFTP_DEBUG
831ca987d46SWarner Losh 	printf("tftp_blksize: %u\n", h->tftp_blksize);
832ca987d46SWarner Losh 	printf("tftp_tsize: %lu\n", h->tftp_tsize);
833ca987d46SWarner Losh #endif
8343c3779dcSToomas Soome 	return (0);
835ca987d46SWarner Losh }
836