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