xref: /illumos-gate/usr/src/boot/libsa/tftp.c (revision 22028508)
1199767f8SToomas Soome /*	$NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner Exp $	 */
2199767f8SToomas Soome 
3199767f8SToomas Soome /*
4199767f8SToomas Soome  * Copyright (c) 1996
5199767f8SToomas Soome  *	Matthias Drochner.  All rights reserved.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
8199767f8SToomas Soome  * modification, are permitted provided that the following conditions
9199767f8SToomas Soome  * are met:
10199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
12199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
13199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
14199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
15199767f8SToomas Soome  * 3. All advertising materials mentioning features or use of this software
16199767f8SToomas Soome  *    must display the following acknowledgement:
17199767f8SToomas Soome  *	This product includes software developed for the NetBSD Project
18199767f8SToomas Soome  *	by Matthias Drochner.
19199767f8SToomas Soome  * 4. The name of the author may not be used to endorse or promote products
20199767f8SToomas Soome  *    derived from this software without specific prior written permission.
21199767f8SToomas Soome  *
22199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23199767f8SToomas Soome  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24199767f8SToomas Soome  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25199767f8SToomas Soome  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26199767f8SToomas Soome  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27199767f8SToomas Soome  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28199767f8SToomas Soome  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29199767f8SToomas Soome  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30199767f8SToomas Soome  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31199767f8SToomas Soome  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32199767f8SToomas Soome  */
33199767f8SToomas Soome 
34199767f8SToomas Soome #include <sys/cdefs.h>
35199767f8SToomas Soome 
36199767f8SToomas Soome /*
37199767f8SToomas Soome  * Simple TFTP implementation for libsa.
38199767f8SToomas Soome  * Assumes:
396538c7b4SToomas Soome  *  - socket descriptor (int) at dev->d_opendata, dev stored at
406538c7b4SToomas Soome  *	open_file->f_devdata
4140218bdbSToomas Soome  *  - server host IP in global rootip
42199767f8SToomas Soome  * Restrictions:
43199767f8SToomas Soome  *  - read only
44199767f8SToomas Soome  *  - lseek only with SEEK_SET or SEEK_CUR
45199767f8SToomas Soome  *  - no big time differences between transfers (<tftp timeout)
46199767f8SToomas Soome  */
47199767f8SToomas Soome 
48199767f8SToomas Soome #include <sys/types.h>
49199767f8SToomas Soome #include <sys/stat.h>
50199767f8SToomas Soome #include <netinet/in.h>
51199767f8SToomas Soome #include <netinet/udp.h>
52199767f8SToomas Soome #include <netinet/in_systm.h>
53199767f8SToomas Soome #include <arpa/tftp.h>
54199767f8SToomas Soome 
55199767f8SToomas Soome #include <string.h>
56199767f8SToomas Soome 
57199767f8SToomas Soome #include "stand.h"
58199767f8SToomas Soome #include "net.h"
59199767f8SToomas Soome #include "netif.h"
60199767f8SToomas Soome 
61199767f8SToomas Soome #include "tftp.h"
62199767f8SToomas Soome 
63199767f8SToomas Soome struct tftp_handle;
64890c8671SToomas Soome struct tftprecv_extra;
65199767f8SToomas Soome 
66921f4108SToomas Soome static ssize_t recvtftp(struct iodesc *, void **, void **, time_t, void *);
67921f4108SToomas Soome static int tftp_open(const char *, struct open_file *);
68921f4108SToomas Soome static int tftp_close(struct open_file *);
69921f4108SToomas Soome static int tftp_parse_oack(struct tftp_handle *, char *, size_t);
70921f4108SToomas Soome static int tftp_read(struct open_file *, void *, size_t, size_t *);
71921f4108SToomas Soome static off_t tftp_seek(struct open_file *, off_t, int);
72921f4108SToomas Soome static int tftp_set_blksize(struct tftp_handle *, const char *);
73921f4108SToomas Soome static int tftp_stat(struct open_file *, struct stat *);
74199767f8SToomas Soome 
75199767f8SToomas Soome struct fs_ops tftp_fsops = {
76921f4108SToomas Soome 	.fs_name = "tftp",
77921f4108SToomas Soome 	.fo_open = tftp_open,
78921f4108SToomas Soome 	.fo_close = tftp_close,
79921f4108SToomas Soome 	.fo_read = tftp_read,
80921f4108SToomas Soome 	.fo_write = null_write,
81921f4108SToomas Soome 	.fo_seek = tftp_seek,
82921f4108SToomas Soome 	.fo_stat = tftp_stat,
83921f4108SToomas Soome 	.fo_readdir = null_readdir
84199767f8SToomas Soome };
85199767f8SToomas Soome 
86921f4108SToomas Soome static int	tftpport = 2000;
87199767f8SToomas Soome static int	is_open = 0;
88199767f8SToomas Soome 
89199767f8SToomas Soome /*
90199767f8SToomas Soome  * The legacy TFTP_BLKSIZE value was SEGSIZE(512).
91199767f8SToomas Soome  * TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and
92199767f8SToomas Soome  * IP header lengths).
93199767f8SToomas Soome  */
94921f4108SToomas Soome #define	TFTP_REQUESTED_BLKSIZE 1428
95199767f8SToomas Soome 
96199767f8SToomas Soome /*
97199767f8SToomas Soome  * Choose a blksize big enough so we can test with Ethernet
98199767f8SToomas Soome  * Jumbo frames in the future.
99199767f8SToomas Soome  */
100921f4108SToomas Soome #define	TFTP_MAX_BLKSIZE 9008
101199767f8SToomas Soome 
102199767f8SToomas Soome struct tftp_handle {
103199767f8SToomas Soome 	struct iodesc  *iodesc;
104921f4108SToomas Soome 	int		currblock;	/* contents of lastdata */
105921f4108SToomas Soome 	int		islastblock;	/* flag */
106921f4108SToomas Soome 	int		validsize;
107921f4108SToomas Soome 	int		off;
108921f4108SToomas Soome 	char		*path;	/* saved for re-requests */
109199767f8SToomas Soome 	unsigned int	tftp_blksize;
110199767f8SToomas Soome 	unsigned long	tftp_tsize;
111859472daSToomas Soome 	void		*pkt;
112859472daSToomas Soome 	struct tftphdr	*tftp_hdr;
113199767f8SToomas Soome };
114199767f8SToomas Soome 
115890c8671SToomas Soome struct tftprecv_extra {
116890c8671SToomas Soome 	struct tftp_handle	*tftp_handle;
117890c8671SToomas Soome 	unsigned short		rtype;		/* Received type */
118890c8671SToomas Soome };
119890c8671SToomas Soome 
120199767f8SToomas Soome #define	TFTP_MAX_ERRCODE EOPTNEG
121199767f8SToomas Soome static const int tftperrors[TFTP_MAX_ERRCODE + 1] = {
122199767f8SToomas Soome 	0,			/* ??? */
123199767f8SToomas Soome 	ENOENT,
124199767f8SToomas Soome 	EPERM,
125199767f8SToomas Soome 	ENOSPC,
126199767f8SToomas Soome 	EINVAL,			/* ??? */
127199767f8SToomas Soome 	EINVAL,			/* ??? */
128199767f8SToomas Soome 	EEXIST,
129199767f8SToomas Soome 	EINVAL,			/* ??? */
130199767f8SToomas Soome 	EINVAL,			/* Option negotiation failed. */
131199767f8SToomas Soome };
132199767f8SToomas Soome 
133199767f8SToomas Soome static int  tftp_getnextblock(struct tftp_handle *h);
134199767f8SToomas Soome 
135199767f8SToomas Soome /* send error message back. */
136199767f8SToomas Soome static void
tftp_senderr(struct tftp_handle * h,ushort_t errcode,const char * msg)137921f4108SToomas Soome tftp_senderr(struct tftp_handle *h, ushort_t errcode, const char *msg)
138199767f8SToomas Soome {
139199767f8SToomas Soome 	struct {
140921f4108SToomas Soome 		uchar_t header[HEADER_SIZE];
141921f4108SToomas Soome 		struct tftphdr t;
142921f4108SToomas Soome 		uchar_t space[63]; /* +1 from t */
143199767f8SToomas Soome 	} __packed __aligned(4) wbuf;
144921f4108SToomas Soome 	char *wtail;
145921f4108SToomas Soome 	int len;
146199767f8SToomas Soome 
147199767f8SToomas Soome 	len = strlen(msg);
148921f4108SToomas Soome 	if (len > sizeof (wbuf.space))
149921f4108SToomas Soome 		len = sizeof (wbuf.space);
150199767f8SToomas Soome 
151921f4108SToomas Soome 	wbuf.t.th_opcode = htons((ushort_t)ERROR);
152921f4108SToomas Soome 	wbuf.t.th_code = htons(errcode);
153199767f8SToomas Soome 
154199767f8SToomas Soome 	wtail = wbuf.t.th_msg;
155199767f8SToomas Soome 	bcopy(msg, wtail, len);
156199767f8SToomas Soome 	wtail[len] = '\0';
157199767f8SToomas Soome 	wtail += len + 1;
158199767f8SToomas Soome 
159921f4108SToomas Soome 	sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
160199767f8SToomas Soome }
161199767f8SToomas Soome 
162199767f8SToomas Soome static void
tftp_sendack(struct tftp_handle * h,ushort_t block)1631bb0ebc1SToomas Soome tftp_sendack(struct tftp_handle *h, ushort_t block)
164199767f8SToomas Soome {
165199767f8SToomas Soome 	struct {
166921f4108SToomas Soome 		uchar_t header[HEADER_SIZE];
167199767f8SToomas Soome 		struct tftphdr  t;
168199767f8SToomas Soome 	} __packed __aligned(4) wbuf;
169921f4108SToomas Soome 	char *wtail;
170199767f8SToomas Soome 
171921f4108SToomas Soome 	wbuf.t.th_opcode = htons((ushort_t)ACK);
172921f4108SToomas Soome 	wtail = (char *)&wbuf.t.th_block;
1731bb0ebc1SToomas Soome 	wbuf.t.th_block = htons(block);
174199767f8SToomas Soome 	wtail += 2;
175199767f8SToomas Soome 
176921f4108SToomas Soome 	sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
177199767f8SToomas Soome }
178199767f8SToomas Soome 
179199767f8SToomas Soome static ssize_t
recvtftp(struct iodesc * d,void ** pkt,void ** payload,time_t tleft,void * recv_extra)180890c8671SToomas Soome recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
181890c8671SToomas Soome     void *recv_extra)
182199767f8SToomas Soome {
183890c8671SToomas Soome 	struct tftprecv_extra *extra;
184890c8671SToomas Soome 	struct tftp_handle *h;
185199767f8SToomas Soome 	struct tftphdr *t;
186859472daSToomas Soome 	void *ptr = NULL;
187859472daSToomas Soome 	ssize_t len;
188199767f8SToomas Soome 
189199767f8SToomas Soome 	errno = 0;
190921f4108SToomas Soome 	extra = recv_extra;
191890c8671SToomas Soome 	h = extra->tftp_handle;
192199767f8SToomas Soome 
193859472daSToomas Soome 	len = readudp(d, &ptr, (void **)&t, tleft);
194199767f8SToomas Soome 
195859472daSToomas Soome 	if (len < 4) {
196859472daSToomas Soome 		free(ptr);
197199767f8SToomas Soome 		return (-1);
198859472daSToomas Soome 	}
199199767f8SToomas Soome 
200890c8671SToomas Soome 	extra->rtype = ntohs(t->th_opcode);
201199767f8SToomas Soome 	switch (ntohs(t->th_opcode)) {
202199767f8SToomas Soome 	case DATA: {
203199767f8SToomas Soome 		int got;
204199767f8SToomas Soome 
205921f4108SToomas Soome 		if (htons(t->th_block) < (ushort_t)d->xid) {
2061bb0ebc1SToomas Soome 			/*
2071bb0ebc1SToomas Soome 			 * Apparently our ACK was missed, re-send.
2081bb0ebc1SToomas Soome 			 */
2091bb0ebc1SToomas Soome 			tftp_sendack(h, htons(t->th_block));
2101bb0ebc1SToomas Soome 			free(ptr);
2111bb0ebc1SToomas Soome 			return (-1);
2121bb0ebc1SToomas Soome 		}
213921f4108SToomas Soome 		if (htons(t->th_block) != (ushort_t)d->xid) {
214199767f8SToomas Soome 			/*
2151bb0ebc1SToomas Soome 			 * Packet from the future, drop this.
216199767f8SToomas Soome 			 */
217859472daSToomas Soome 			free(ptr);
218199767f8SToomas Soome 			return (-1);
219199767f8SToomas Soome 		}
220199767f8SToomas Soome 		if (d->xid == 1) {
221199767f8SToomas Soome 			/*
222199767f8SToomas Soome 			 * First data packet from new port.
223199767f8SToomas Soome 			 */
224199767f8SToomas Soome 			struct udphdr *uh;
225921f4108SToomas Soome 			uh = (struct udphdr *)t - 1;
226199767f8SToomas Soome 			d->destport = uh->uh_sport;
2271bb0ebc1SToomas Soome 		}
228859472daSToomas Soome 		got = len - (t->th_data - (char *)t);
229859472daSToomas Soome 		*pkt = ptr;
230859472daSToomas Soome 		*payload = t;
231859472daSToomas Soome 		return (got);
232199767f8SToomas Soome 	}
233199767f8SToomas Soome 	case ERROR:
234921f4108SToomas Soome 		if ((unsigned)ntohs(t->th_code) > TFTP_MAX_ERRCODE) {
235199767f8SToomas Soome 			printf("illegal tftp error %d\n", ntohs(t->th_code));
236199767f8SToomas Soome 			errno = EIO;
237199767f8SToomas Soome 		} else {
238199767f8SToomas Soome #ifdef TFTP_DEBUG
239199767f8SToomas Soome 			printf("tftp-error %d\n", ntohs(t->th_code));
240199767f8SToomas Soome #endif
241199767f8SToomas Soome 			errno = tftperrors[ntohs(t->th_code)];
242199767f8SToomas Soome 		}
243859472daSToomas Soome 		free(ptr);
244199767f8SToomas Soome 		return (-1);
245199767f8SToomas Soome 	case OACK: {
246199767f8SToomas Soome 		struct udphdr *uh;
247199767f8SToomas Soome 		int tftp_oack_len;
248199767f8SToomas Soome 
249921f4108SToomas Soome 		/*
250921f4108SToomas Soome 		 * Unexpected OACK. TFTP transfer already in progress.
251199767f8SToomas Soome 		 * Drop the pkt.
252199767f8SToomas Soome 		 */
253199767f8SToomas Soome 		if (d->xid != 1) {
254859472daSToomas Soome 			free(ptr);
255199767f8SToomas Soome 			return (-1);
256199767f8SToomas Soome 		}
257199767f8SToomas Soome 
258199767f8SToomas Soome 		/*
259199767f8SToomas Soome 		 * Remember which port this OACK came from, because we need
260199767f8SToomas Soome 		 * to send the ACK or errors back to it.
261199767f8SToomas Soome 		 */
262921f4108SToomas Soome 		uh = (struct udphdr *)t - 1;
263199767f8SToomas Soome 		d->destport = uh->uh_sport;
264921f4108SToomas Soome 
265199767f8SToomas Soome 		/* Parse options ACK-ed by the server. */
266921f4108SToomas Soome 		tftp_oack_len = len - sizeof (t->th_opcode);
267199767f8SToomas Soome 		if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) {
268199767f8SToomas Soome 			tftp_senderr(h, EOPTNEG, "Malformed OACK");
269199767f8SToomas Soome 			errno = EIO;
270859472daSToomas Soome 			free(ptr);
271199767f8SToomas Soome 			return (-1);
272199767f8SToomas Soome 		}
273859472daSToomas Soome 		*pkt = ptr;
274859472daSToomas Soome 		*payload = t;
275199767f8SToomas Soome 		return (0);
276199767f8SToomas Soome 	}
277199767f8SToomas Soome 	default:
278199767f8SToomas Soome #ifdef TFTP_DEBUG
279199767f8SToomas Soome 		printf("tftp type %d not handled\n", ntohs(t->th_opcode));
280199767f8SToomas Soome #endif
281859472daSToomas Soome 		free(ptr);
282199767f8SToomas Soome 		return (-1);
283199767f8SToomas Soome 	}
284199767f8SToomas Soome }
285199767f8SToomas Soome 
286199767f8SToomas Soome /* send request, expect first block (or error) */
287199767f8SToomas Soome static int
tftp_makereq(struct tftp_handle * h)288199767f8SToomas Soome tftp_makereq(struct tftp_handle *h)
289199767f8SToomas Soome {
290199767f8SToomas Soome 	struct {
291921f4108SToomas Soome 		uchar_t header[HEADER_SIZE];
292199767f8SToomas Soome 		struct tftphdr  t;
293921f4108SToomas Soome 		uchar_t space[FNAME_SIZE + 6];
294199767f8SToomas Soome 	} __packed __aligned(4) wbuf;
295890c8671SToomas Soome 	struct tftprecv_extra recv_extra;
296921f4108SToomas Soome 	char *wtail;
297921f4108SToomas Soome 	int l;
298921f4108SToomas Soome 	ssize_t res;
299859472daSToomas Soome 	void *pkt;
300199767f8SToomas Soome 	struct tftphdr *t;
301199767f8SToomas Soome 	char *tftp_blksize = NULL;
302199767f8SToomas Soome 	int blksize_l;
303199767f8SToomas Soome 
304199767f8SToomas Soome 	/*
305199767f8SToomas Soome 	 * Allow overriding default TFTP block size by setting
306199767f8SToomas Soome 	 * a tftp.blksize environment variable.
307199767f8SToomas Soome 	 */
308199767f8SToomas Soome 	if ((tftp_blksize = getenv("tftp.blksize")) != NULL) {
309199767f8SToomas Soome 		tftp_set_blksize(h, tftp_blksize);
310199767f8SToomas Soome 	}
311199767f8SToomas Soome 
312921f4108SToomas Soome 	wbuf.t.th_opcode = htons((ushort_t)RRQ);
313199767f8SToomas Soome 	wtail = wbuf.t.th_stuff;
314199767f8SToomas Soome 	l = strlen(h->path);
315199767f8SToomas Soome #ifdef TFTP_PREPEND_PATH
316921f4108SToomas Soome 	if (l > FNAME_SIZE - (sizeof (TFTP_PREPEND_PATH) - 1))
317199767f8SToomas Soome 		return (ENAMETOOLONG);
318921f4108SToomas Soome 	bcopy(TFTP_PREPEND_PATH, wtail, sizeof (TFTP_PREPEND_PATH) - 1);
319921f4108SToomas Soome 	wtail += sizeof (TFTP_PREPEND_PATH) - 1;
320199767f8SToomas Soome #else
321199767f8SToomas Soome 	if (l > FNAME_SIZE)
322199767f8SToomas Soome 		return (ENAMETOOLONG);
323199767f8SToomas Soome #endif
324199767f8SToomas Soome 	bcopy(h->path, wtail, l + 1);
325199767f8SToomas Soome 	wtail += l + 1;
326199767f8SToomas Soome 	bcopy("octet", wtail, 6);
327199767f8SToomas Soome 	wtail += 6;
328199767f8SToomas Soome 	bcopy("blksize", wtail, 8);
329199767f8SToomas Soome 	wtail += 8;
330199767f8SToomas Soome 	blksize_l = sprintf(wtail, "%d", h->tftp_blksize);
331199767f8SToomas Soome 	wtail += blksize_l + 1;
332199767f8SToomas Soome 	bcopy("tsize", wtail, 6);
333199767f8SToomas Soome 	wtail += 6;
334199767f8SToomas Soome 	bcopy("0", wtail, 2);
335199767f8SToomas Soome 	wtail += 2;
336199767f8SToomas Soome 
337199767f8SToomas Soome 	h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
338199767f8SToomas Soome 	h->iodesc->destport = htons(IPPORT_TFTP);
339199767f8SToomas Soome 	h->iodesc->xid = 1;	/* expected block */
340199767f8SToomas Soome 
341199767f8SToomas Soome 	h->currblock = 0;
342199767f8SToomas Soome 	h->islastblock = 0;
343199767f8SToomas Soome 	h->validsize = 0;
344199767f8SToomas Soome 
345859472daSToomas Soome 	pkt = NULL;
346890c8671SToomas Soome 	recv_extra.tftp_handle = h;
347921f4108SToomas Soome 	res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t,
348921f4108SToomas Soome 	    &recvtftp, &pkt, (void **)&t, &recv_extra);
349859472daSToomas Soome 	if (res == -1) {
350859472daSToomas Soome 		free(pkt);
351859472daSToomas Soome 		return (errno);
352859472daSToomas Soome 	}
353859472daSToomas Soome 
354859472daSToomas Soome 	free(h->pkt);
355859472daSToomas Soome 	h->pkt = pkt;
356859472daSToomas Soome 	h->tftp_hdr = t;
357199767f8SToomas Soome 
358890c8671SToomas Soome 	if (recv_extra.rtype == OACK)
359199767f8SToomas Soome 		return (tftp_getnextblock(h));
360199767f8SToomas Soome 
361199767f8SToomas Soome 	/* Server ignored our blksize request, revert to TFTP default. */
362199767f8SToomas Soome 	h->tftp_blksize = SEGSIZE;
363199767f8SToomas Soome 
364890c8671SToomas Soome 	switch (recv_extra.rtype) {
365199767f8SToomas Soome 		case DATA: {
366199767f8SToomas Soome 			h->currblock = 1;
367199767f8SToomas Soome 			h->validsize = res;
368199767f8SToomas Soome 			h->islastblock = 0;
369199767f8SToomas Soome 			if (res < h->tftp_blksize) {
370199767f8SToomas Soome 				h->islastblock = 1;	/* very short file */
3711bb0ebc1SToomas Soome 				tftp_sendack(h, h->currblock);
372199767f8SToomas Soome 			}
373199767f8SToomas Soome 			return (0);
374199767f8SToomas Soome 		}
375199767f8SToomas Soome 		case ERROR:
376199767f8SToomas Soome 		default:
377199767f8SToomas Soome 			return (errno);
378199767f8SToomas Soome 	}
379199767f8SToomas Soome 
380199767f8SToomas Soome }
381199767f8SToomas Soome 
382199767f8SToomas Soome /* ack block, expect next */
383921f4108SToomas Soome static int
tftp_getnextblock(struct tftp_handle * h)384199767f8SToomas Soome tftp_getnextblock(struct tftp_handle *h)
385199767f8SToomas Soome {
386199767f8SToomas Soome 	struct {
387921f4108SToomas Soome 		uchar_t header[HEADER_SIZE];
388199767f8SToomas Soome 		struct tftphdr t;
389199767f8SToomas Soome 	} __packed __aligned(4) wbuf;
390890c8671SToomas Soome 	struct tftprecv_extra recv_extra;
391921f4108SToomas Soome 	char *wtail;
392921f4108SToomas Soome 	int res;
393859472daSToomas Soome 	void *pkt;
394199767f8SToomas Soome 	struct tftphdr *t;
395921f4108SToomas Soome 
396921f4108SToomas Soome 	wbuf.t.th_opcode = htons((ushort_t)ACK);
397921f4108SToomas Soome 	wtail = (char *)&wbuf.t.th_block;
398921f4108SToomas Soome 	wbuf.t.th_block = htons((ushort_t)h->currblock);
399199767f8SToomas Soome 	wtail += 2;
400199767f8SToomas Soome 
401199767f8SToomas Soome 	h->iodesc->xid = h->currblock + 1;	/* expected block */
402199767f8SToomas Soome 
403859472daSToomas Soome 	pkt = NULL;
404890c8671SToomas Soome 	recv_extra.tftp_handle = h;
405921f4108SToomas Soome 	res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t,
406921f4108SToomas Soome 	    &recvtftp, &pkt, (void **)&t, &recv_extra);
407199767f8SToomas Soome 
408859472daSToomas Soome 	if (res == -1) {		/* 0 is OK! */
409859472daSToomas Soome 		free(pkt);
410199767f8SToomas Soome 		return (errno);
411859472daSToomas Soome 	}
412199767f8SToomas Soome 
413859472daSToomas Soome 	free(h->pkt);
414859472daSToomas Soome 	h->pkt = pkt;
415859472daSToomas Soome 	h->tftp_hdr = t;
416199767f8SToomas Soome 	h->currblock++;
417199767f8SToomas Soome 	h->validsize = res;
418199767f8SToomas Soome 	if (res < h->tftp_blksize)
419199767f8SToomas Soome 		h->islastblock = 1;	/* EOF */
420199767f8SToomas Soome 
421199767f8SToomas Soome 	if (h->islastblock == 1) {
422921f4108SToomas Soome 		/* Send an ACK for the last block */
423921f4108SToomas Soome 		wbuf.t.th_block = htons((ushort_t)h->currblock);
424199767f8SToomas Soome 		sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
425199767f8SToomas Soome 	}
426199767f8SToomas Soome 
427199767f8SToomas Soome 	return (0);
428199767f8SToomas Soome }
429199767f8SToomas Soome 
430199767f8SToomas Soome static int
tftp_open(const char * path,struct open_file * f)431199767f8SToomas Soome tftp_open(const char *path, struct open_file *f)
432199767f8SToomas Soome {
4336538c7b4SToomas Soome 	struct devdesc *dev;
434199767f8SToomas Soome 	struct tftp_handle *tftpfile;
435921f4108SToomas Soome 	struct iodesc	*io;
436921f4108SToomas Soome 	int		res;
437921f4108SToomas Soome 	size_t		pathsize;
438921f4108SToomas Soome 	const char	*extraslash;
439199767f8SToomas Soome 
440aa61755eSToomas Soome 	if (netproto != NET_TFTP)
441aa61755eSToomas Soome 		return (EINVAL);
442aa61755eSToomas Soome 
443859472daSToomas Soome 	if (f->f_dev->dv_type != DEVT_NET)
444199767f8SToomas Soome 		return (EINVAL);
445199767f8SToomas Soome 
446199767f8SToomas Soome 	if (is_open)
447199767f8SToomas Soome 		return (EBUSY);
448199767f8SToomas Soome 
449921f4108SToomas Soome 	tftpfile = calloc(1, sizeof (*tftpfile));
450199767f8SToomas Soome 	if (!tftpfile)
451199767f8SToomas Soome 		return (ENOMEM);
452199767f8SToomas Soome 
453199767f8SToomas Soome 	tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE;
4546538c7b4SToomas Soome 	dev = f->f_devdata;
4556538c7b4SToomas Soome 	tftpfile->iodesc = io = socktodesc(*(int *)(dev->d_opendata));
4564667a9b1SToomas Soome 	if (io == NULL) {
4574667a9b1SToomas Soome 		free(tftpfile);
458199767f8SToomas Soome 		return (EINVAL);
4594667a9b1SToomas Soome 	}
460199767f8SToomas Soome 
46140218bdbSToomas Soome 	io->destip = rootip;
462199767f8SToomas Soome 	tftpfile->off = 0;
463921f4108SToomas Soome 	pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof (char);
464199767f8SToomas Soome 	tftpfile->path = malloc(pathsize);
465199767f8SToomas Soome 	if (tftpfile->path == NULL) {
466199767f8SToomas Soome 		free(tftpfile);
467921f4108SToomas Soome 		return (ENOMEM);
468199767f8SToomas Soome 	}
469199767f8SToomas Soome 	if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/')
470199767f8SToomas Soome 		extraslash = "";
471199767f8SToomas Soome 	else
472199767f8SToomas Soome 		extraslash = "/";
473199767f8SToomas Soome 	res = snprintf(tftpfile->path, pathsize, "%s%s%s",
474199767f8SToomas Soome 	    rootpath, extraslash, path);
475199767f8SToomas Soome 	if (res < 0 || res > pathsize) {
476199767f8SToomas Soome 		free(tftpfile->path);
477199767f8SToomas Soome 		free(tftpfile);
478921f4108SToomas Soome 		return (ENOMEM);
479199767f8SToomas Soome 	}
480199767f8SToomas Soome 
481199767f8SToomas Soome 	res = tftp_makereq(tftpfile);
482199767f8SToomas Soome 
483199767f8SToomas Soome 	if (res) {
484199767f8SToomas Soome 		free(tftpfile->path);
485859472daSToomas Soome 		free(tftpfile->pkt);
486199767f8SToomas Soome 		free(tftpfile);
487199767f8SToomas Soome 		return (res);
488199767f8SToomas Soome 	}
489921f4108SToomas Soome 	f->f_fsdata = tftpfile;
490199767f8SToomas Soome 	is_open = 1;
491199767f8SToomas Soome 	return (0);
492199767f8SToomas Soome }
493199767f8SToomas Soome 
494199767f8SToomas Soome static int
tftp_read(struct open_file * f,void * addr,size_t size,size_t * resid)495199767f8SToomas Soome tftp_read(struct open_file *f, void *addr, size_t size,
496199767f8SToomas Soome     size_t *resid /* out */)
497199767f8SToomas Soome {
498199767f8SToomas Soome 	struct tftp_handle *tftpfile;
4992017dcb0SToomas Soome 	size_t res;
50063e2133bSToomas Soome 	int rc;
50163e2133bSToomas Soome 
50263e2133bSToomas Soome 	rc = 0;
5032017dcb0SToomas Soome 	res = size;
504921f4108SToomas Soome 	tftpfile = f->f_fsdata;
505199767f8SToomas Soome 
5062017dcb0SToomas Soome 	/* Make sure we will not read past file end */
5072017dcb0SToomas Soome 	if (tftpfile->tftp_tsize > 0 &&
5082017dcb0SToomas Soome 	    tftpfile->off + size > tftpfile->tftp_tsize) {
5092017dcb0SToomas Soome 		size = tftpfile->tftp_tsize - tftpfile->off;
5102017dcb0SToomas Soome 	}
5112017dcb0SToomas Soome 
512199767f8SToomas Soome 	while (size > 0) {
513199767f8SToomas Soome 		int needblock, count;
514199767f8SToomas Soome 
515199767f8SToomas Soome 		twiddle(32);
516199767f8SToomas Soome 
517199767f8SToomas Soome 		needblock = tftpfile->off / tftpfile->tftp_blksize + 1;
518199767f8SToomas Soome 
519199767f8SToomas Soome 		if (tftpfile->currblock > needblock) {	/* seek backwards */
520199767f8SToomas Soome 			tftp_senderr(tftpfile, 0, "No error: read aborted");
52163e2133bSToomas Soome 			rc = tftp_makereq(tftpfile);
52263e2133bSToomas Soome 			if (rc != 0)
52363e2133bSToomas Soome 				break;
524199767f8SToomas Soome 		}
525199767f8SToomas Soome 
526199767f8SToomas Soome 		while (tftpfile->currblock < needblock) {
527199767f8SToomas Soome 
52863e2133bSToomas Soome 			rc = tftp_getnextblock(tftpfile);
52963e2133bSToomas Soome 			if (rc) {	/* no answer */
530199767f8SToomas Soome #ifdef TFTP_DEBUG
531199767f8SToomas Soome 				printf("tftp: read error\n");
532199767f8SToomas Soome #endif
53363e2133bSToomas Soome 				return (rc);
534199767f8SToomas Soome 			}
535199767f8SToomas Soome 			if (tftpfile->islastblock)
536199767f8SToomas Soome 				break;
537199767f8SToomas Soome 		}
538199767f8SToomas Soome 
539199767f8SToomas Soome 		if (tftpfile->currblock == needblock) {
540199767f8SToomas Soome 			int offinblock, inbuffer;
541199767f8SToomas Soome 
542199767f8SToomas Soome 			offinblock = tftpfile->off % tftpfile->tftp_blksize;
543199767f8SToomas Soome 
544199767f8SToomas Soome 			inbuffer = tftpfile->validsize - offinblock;
545199767f8SToomas Soome 			if (inbuffer < 0) {
546199767f8SToomas Soome #ifdef TFTP_DEBUG
547199767f8SToomas Soome 				printf("tftp: invalid offset %d\n",
548199767f8SToomas Soome 				    tftpfile->off);
549199767f8SToomas Soome #endif
550199767f8SToomas Soome 				return (EINVAL);
551199767f8SToomas Soome 			}
552199767f8SToomas Soome 			count = (size < inbuffer ? size : inbuffer);
553859472daSToomas Soome 			bcopy(tftpfile->tftp_hdr->th_data + offinblock,
554199767f8SToomas Soome 			    addr, count);
555199767f8SToomas Soome 
556199767f8SToomas Soome 			addr = (char *)addr + count;
557199767f8SToomas Soome 			tftpfile->off += count;
558199767f8SToomas Soome 			size -= count;
5592017dcb0SToomas Soome 			res -= count;
560199767f8SToomas Soome 
561199767f8SToomas Soome 			if ((tftpfile->islastblock) && (count == inbuffer))
562199767f8SToomas Soome 				break;	/* EOF */
563199767f8SToomas Soome 		} else {
564199767f8SToomas Soome #ifdef TFTP_DEBUG
565199767f8SToomas Soome 			printf("tftp: block %d not found\n", needblock);
566199767f8SToomas Soome #endif
567199767f8SToomas Soome 			return (EINVAL);
568199767f8SToomas Soome 		}
569199767f8SToomas Soome 
570199767f8SToomas Soome 	}
571199767f8SToomas Soome 
5722017dcb0SToomas Soome 	if (resid != NULL)
5732017dcb0SToomas Soome 		*resid = res;
57463e2133bSToomas Soome 	return (rc);
575199767f8SToomas Soome }
576199767f8SToomas Soome 
577921f4108SToomas Soome static int
tftp_close(struct open_file * f)578199767f8SToomas Soome tftp_close(struct open_file *f)
579199767f8SToomas Soome {
580199767f8SToomas Soome 	struct tftp_handle *tftpfile;
581921f4108SToomas Soome 	tftpfile = f->f_fsdata;
582199767f8SToomas Soome 
583199767f8SToomas Soome 	/* let it time out ... */
584199767f8SToomas Soome 
585199767f8SToomas Soome 	if (tftpfile) {
586199767f8SToomas Soome 		free(tftpfile->path);
587859472daSToomas Soome 		free(tftpfile->pkt);
588199767f8SToomas Soome 		free(tftpfile);
589199767f8SToomas Soome 	}
590199767f8SToomas Soome 	is_open = 0;
591199767f8SToomas Soome 	return (0);
592199767f8SToomas Soome }
593199767f8SToomas Soome 
594921f4108SToomas Soome static int
tftp_stat(struct open_file * f,struct stat * sb)595199767f8SToomas Soome tftp_stat(struct open_file *f, struct stat *sb)
596199767f8SToomas Soome {
597199767f8SToomas Soome 	struct tftp_handle *tftpfile;
598921f4108SToomas Soome 	tftpfile = f->f_fsdata;
599199767f8SToomas Soome 
600199767f8SToomas Soome 	sb->st_mode = 0444 | S_IFREG;
601199767f8SToomas Soome 	sb->st_nlink = 1;
602199767f8SToomas Soome 	sb->st_uid = 0;
603199767f8SToomas Soome 	sb->st_gid = 0;
604921f4108SToomas Soome 	sb->st_size = tftpfile->tftp_tsize;
605199767f8SToomas Soome 	return (0);
606199767f8SToomas Soome }
607199767f8SToomas Soome 
608199767f8SToomas Soome static off_t
tftp_seek(struct open_file * f,off_t offset,int where)609199767f8SToomas Soome tftp_seek(struct open_file *f, off_t offset, int where)
610199767f8SToomas Soome {
611199767f8SToomas Soome 	struct tftp_handle *tftpfile;
612921f4108SToomas Soome 	tftpfile = f->f_fsdata;
613199767f8SToomas Soome 
614199767f8SToomas Soome 	switch (where) {
615199767f8SToomas Soome 	case SEEK_SET:
616199767f8SToomas Soome 		tftpfile->off = offset;
617199767f8SToomas Soome 		break;
618199767f8SToomas Soome 	case SEEK_CUR:
619199767f8SToomas Soome 		tftpfile->off += offset;
620199767f8SToomas Soome 		break;
621199767f8SToomas Soome 	default:
622199767f8SToomas Soome 		errno = EOFFSET;
623199767f8SToomas Soome 		return (-1);
624199767f8SToomas Soome 	}
625199767f8SToomas Soome 	return (tftpfile->off);
626199767f8SToomas Soome }
627199767f8SToomas Soome 
628199767f8SToomas Soome static int
tftp_set_blksize(struct tftp_handle * h,const char * str)629199767f8SToomas Soome tftp_set_blksize(struct tftp_handle *h, const char *str)
630199767f8SToomas Soome {
631921f4108SToomas Soome 	char *endptr;
632199767f8SToomas Soome 	int new_blksize;
633199767f8SToomas Soome 	int ret = 0;
634199767f8SToomas Soome 
635199767f8SToomas Soome 	if (h == NULL || str == NULL)
636199767f8SToomas Soome 		return (ret);
637199767f8SToomas Soome 
638199767f8SToomas Soome 	new_blksize =
639199767f8SToomas Soome 	    (unsigned int)strtol(str, &endptr, 0);
640199767f8SToomas Soome 
641199767f8SToomas Soome 	/*
642199767f8SToomas Soome 	 * Only accept blksize value if it is numeric.
643199767f8SToomas Soome 	 * RFC2348 specifies that acceptable values are 8-65464.
644199767f8SToomas Soome 	 * Let's choose a limit less than MAXRSPACE.
645199767f8SToomas Soome 	 */
646921f4108SToomas Soome 	if (*endptr == '\0' && new_blksize >= 8 &&
647921f4108SToomas Soome 	    new_blksize <= TFTP_MAX_BLKSIZE) {
648199767f8SToomas Soome 		h->tftp_blksize = new_blksize;
649199767f8SToomas Soome 		ret = 1;
650199767f8SToomas Soome 	}
651199767f8SToomas Soome 
652199767f8SToomas Soome 	return (ret);
653199767f8SToomas Soome }
654199767f8SToomas Soome 
655199767f8SToomas Soome /*
656199767f8SToomas Soome  * In RFC2347, the TFTP Option Acknowledgement package (OACK)
657199767f8SToomas Soome  * is used to acknowledge a client's option negotiation request.
658199767f8SToomas Soome  * The format of an OACK packet is:
659199767f8SToomas Soome  *    +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
660199767f8SToomas Soome  *    |  opc  |  opt1  | 0 | value1 | 0 |  optN  | 0 | valueN | 0 |
661199767f8SToomas Soome  *    +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
662199767f8SToomas Soome  *
663199767f8SToomas Soome  *    opc
664199767f8SToomas Soome  *       The opcode field contains a 6, for Option Acknowledgment.
665199767f8SToomas Soome  *
666199767f8SToomas Soome  *    opt1
667199767f8SToomas Soome  *       The first option acknowledgment, copied from the original
668199767f8SToomas Soome  *       request.
669199767f8SToomas Soome  *
670199767f8SToomas Soome  *    value1
671199767f8SToomas Soome  *       The acknowledged value associated with the first option.  If
672199767f8SToomas Soome  *       and how this value may differ from the original request is
673199767f8SToomas Soome  *       detailed in the specification for the option.
674199767f8SToomas Soome  *
675199767f8SToomas Soome  *    optN, valueN
676199767f8SToomas Soome  *       The final option/value acknowledgment pair.
677199767f8SToomas Soome  */
678921f4108SToomas Soome static int
tftp_parse_oack(struct tftp_handle * h,char * buf,size_t len)679199767f8SToomas Soome tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
680199767f8SToomas Soome {
681921f4108SToomas Soome 	/*
682199767f8SToomas Soome 	 *  We parse the OACK strings into an array
683199767f8SToomas Soome 	 *  of name-value pairs.
684199767f8SToomas Soome 	 */
685199767f8SToomas Soome 	char *tftp_options[128] = { 0 };
686199767f8SToomas Soome 	char *val = buf;
687199767f8SToomas Soome 	int i = 0;
688199767f8SToomas Soome 	int option_idx = 0;
689199767f8SToomas Soome 	int blksize_is_set = 0;
690199767f8SToomas Soome 	int tsize = 0;
691921f4108SToomas Soome 
692199767f8SToomas Soome 	unsigned int orig_blksize;
693199767f8SToomas Soome 
694199767f8SToomas Soome 	while (option_idx < 128 && i < len) {
695199767f8SToomas Soome 		if (buf[i] == '\0') {
696199767f8SToomas Soome 			if (&buf[i] > val) {
697199767f8SToomas Soome 				tftp_options[option_idx] = val;
698199767f8SToomas Soome 				val = &buf[i] + 1;
699199767f8SToomas Soome 				++option_idx;
700199767f8SToomas Soome 			}
701199767f8SToomas Soome 		}
702199767f8SToomas Soome 		++i;
703199767f8SToomas Soome 	}
704199767f8SToomas Soome 
705199767f8SToomas Soome 	/* Save the block size we requested for sanity check later. */
706199767f8SToomas Soome 	orig_blksize = h->tftp_blksize;
707199767f8SToomas Soome 
708921f4108SToomas Soome 	/*
709199767f8SToomas Soome 	 * Parse individual TFTP options.
710199767f8SToomas Soome 	 *    * "blksize" is specified in RFC2348.
711199767f8SToomas Soome 	 *    * "tsize" is specified in RFC2349.
712921f4108SToomas Soome 	 */
713199767f8SToomas Soome 	for (i = 0; i < option_idx; i += 2) {
714921f4108SToomas Soome 		if (strcasecmp(tftp_options[i], "blksize") == 0) {
715921f4108SToomas Soome 			if (i + 1 < option_idx)
716921f4108SToomas Soome 				blksize_is_set =
717921f4108SToomas Soome 				    tftp_set_blksize(h, tftp_options[i + 1]);
718921f4108SToomas Soome 		} else if (strcasecmp(tftp_options[i], "tsize") == 0) {
719921f4108SToomas Soome 			if (i + 1 < option_idx)
720921f4108SToomas Soome 				tsize = strtol(tftp_options[i + 1], NULL, 10);
721921f4108SToomas Soome 			if (tsize != 0)
722921f4108SToomas Soome 				h->tftp_tsize = tsize;
723921f4108SToomas Soome 		} else {
724921f4108SToomas Soome 			/*
725921f4108SToomas Soome 			 * Do not allow any options we did not expect to be
726921f4108SToomas Soome 			 * ACKed.
727921f4108SToomas Soome 			 */
728921f4108SToomas Soome 			printf("unexpected tftp option '%s'\n",
729921f4108SToomas Soome 			    tftp_options[i]);
730921f4108SToomas Soome 			return (-1);
731921f4108SToomas Soome 		}
732199767f8SToomas Soome 	}
733199767f8SToomas Soome 
734199767f8SToomas Soome 	if (!blksize_is_set) {
735199767f8SToomas Soome 		/*
736199767f8SToomas Soome 		 * If TFTP blksize was not set, try defaulting
737199767f8SToomas Soome 		 * to the legacy TFTP blksize of SEGSIZE(512)
738199767f8SToomas Soome 		 */
739199767f8SToomas Soome 		h->tftp_blksize = SEGSIZE;
740199767f8SToomas Soome 	} else if (h->tftp_blksize > orig_blksize) {
741199767f8SToomas Soome 		/*
742199767f8SToomas Soome 		 * Server should not be proposing block sizes that
743199767f8SToomas Soome 		 * exceed what we said we can handle.
744199767f8SToomas Soome 		 */
745199767f8SToomas Soome 		printf("unexpected blksize %u\n", h->tftp_blksize);
746199767f8SToomas Soome 		return (-1);
747199767f8SToomas Soome 	}
748199767f8SToomas Soome 
749199767f8SToomas Soome #ifdef TFTP_DEBUG
750199767f8SToomas Soome 	printf("tftp_blksize: %u\n", h->tftp_blksize);
751199767f8SToomas Soome 	printf("tftp_tsize: %lu\n", h->tftp_tsize);
752199767f8SToomas Soome #endif
753921f4108SToomas Soome 	return (0);
754199767f8SToomas Soome }
755