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