1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/* $OpenBSD: tftp.c,v 1.6 2014/07/13 15:31:20 mpi Exp $ */
/* $NetBSD: tftp.c,v 1.15 2003/08/18 15:45:29 dsl Exp $ */
/*
* Copyright (c) 1996
* Matthias Drochner. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Simple TFTP implementation for libsa.
* Assumes:
* - socket descriptor (int) at open_file->f_devdata
* - server host IP in global servip
* Restrictions:
* - read only
* - lseek only with SEEK_SET or SEEK_CUR
* - no big time differences between transfers (<tftp timeout)
*/
/*
* XXX Does not currently implement:
* XXX
* XXX LIBSA_NO_FS_CLOSE
* XXX LIBSA_NO_FS_SEEK
* XXX LIBSA_NO_FS_WRITE
* XXX LIBSA_NO_FS_SYMLINK (does this even make sense?)
* XXX LIBSA_FS_SINGLECOMPONENT (does this even make sense?)
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <lib/libkern/libkern.h>
#include "stand.h"
#include "net.h"
#include "netif.h"
#include "tftp.h"
extern struct in_addr servip;
static int tftpport = 2000;
#define RSPACE 520 /* max data packet, rounded up */
struct tftp_handle {
struct iodesc *iodesc;
int currblock; /* contents of lastdata */
int islastblock; /* flag */
int validsize;
int off;
const char *path; /* saved for re-requests */
struct {
struct packet_header header;
struct tftphdr t;
u_char space[RSPACE];
} lastdata;
};
static const int tftperrors[8] = {
0, /* ??? */
ENOENT,
EPERM,
ENOSPC,
EINVAL, /* ??? */
EINVAL, /* ??? */
EEXIST,
EINVAL /* ??? */
};
ssize_t recvtftp(struct iodesc *, void *, size_t, time_t);
int tftp_makereq(struct tftp_handle *);
int tftp_getnextblock(struct tftp_handle *);
#ifndef TFTP_NOTERMINATE
void tftp_terminate(struct tftp_handle *);
#endif
ssize_t
recvtftp(struct iodesc *d, void *pkt, size_t len, time_t tleft)
{
ssize_t n;
struct tftphdr *t;
errno = 0;
n = readudp(d, pkt, len, tleft);
if (n < 4)
return -1;
t = (struct tftphdr *) pkt;
switch (ntohs(t->th_opcode)) {
case DATA:
if (htons(t->th_block) != d->xid) {
/*
* Expected block?
*/
return -1;
}
if (d->xid == 1) {
/*
* First data packet from new port.
*/
struct udphdr *uh;
uh = (struct udphdr *) pkt - 1;
d->destport = uh->uh_sport;
} /* else check uh_sport has not changed??? */
return (n - (t->th_data - (char *)t));
case ERROR:
if ((unsigned) ntohs(t->th_code) >= 8) {
printf("illegal tftp error %d\n", ntohs(t->th_code));
errno = EIO;
} else {
#ifdef DEBUG
printf("tftp-error %d\n", ntohs(t->th_code));
#endif
errno = tftperrors[ntohs(t->th_code)];
}
return -1;
default:
#ifdef DEBUG
printf("tftp type %d not handled\n", ntohs(t->th_opcode));
#endif
return -1;
}
}
/* send request, expect first block (or error) */
int
tftp_makereq(struct tftp_handle *h)
{
struct {
struct packet_header header;
struct tftphdr t;
u_char space[FNAME_SIZE + 6];
} wbuf;
char *wtail;
int l;
ssize_t res;
struct tftphdr *t;
bzero(&wbuf, sizeof(wbuf));
wbuf.t.th_opcode = htons((u_short) RRQ);
wtail = wbuf.t.th_stuff;
l = strlen(h->path);
bcopy(h->path, wtail, l + 1);
wtail += l + 1;
bcopy("octet", wtail, 6);
wtail += 6;
t = &h->lastdata.t;
/* h->iodesc->myport = htons(--tftpport); */
h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
h->iodesc->destport = htons(IPPORT_TFTP);
h->iodesc->xid = 1; /* expected block */
res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
recvtftp, t, sizeof(*t) + RSPACE);
if (res == -1)
return errno;
h->currblock = 1;
h->validsize = res;
h->islastblock = 0;
if (res < SEGSIZE)
h->islastblock = 1; /* very short file */
return 0;
}
/* ack block, expect next */
int
tftp_getnextblock(struct tftp_handle *h)
{
struct {
struct packet_header header;
struct tftphdr t;
} wbuf;
char *wtail;
int res;
struct tftphdr *t;
bzero(&wbuf, sizeof(wbuf));
wbuf.t.th_opcode = htons((u_short) ACK);
wbuf.t.th_block = htons((u_short) h->currblock);
wtail = (char *) &wbuf.t.th_data;
t = &h->lastdata.t;
h->iodesc->xid = h->currblock + 1; /* expected block */
res = sendrecv(h->iodesc, sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
recvtftp, t, sizeof(*t) + RSPACE);
if (res == -1) /* 0 is OK! */
return errno;
h->currblock++;
h->validsize = res;
if (res < SEGSIZE)
h->islastblock = 1; /* EOF */
return 0;
}
#ifndef TFTP_NOTERMINATE
void
tftp_terminate(struct tftp_handle *h)
{
struct {
struct packet_header header;
struct tftphdr t;
} wbuf;
char *wtail;
bzero(&wbuf, sizeof(wbuf));
wtail = (char *) &wbuf.t.th_data;
if (h->islastblock) {
wbuf.t.th_opcode = htons((u_short) ACK);
wbuf.t.th_block = htons((u_short) h->currblock);
} else {
wbuf.t.th_opcode = htons((u_short) ERROR);
wbuf.t.th_code = htons((u_short) ENOSPACE); /* ??? */
wtail++; /* ERROR data is a string, thus needs NUL. */
}
(void) sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
}
#endif
int
tftp_open(char *path, struct open_file *f)
{
struct tftp_handle *tftpfile;
struct iodesc *io;
int res;
tftpfile = (struct tftp_handle *) alloc(sizeof(*tftpfile));
if (tftpfile == NULL)
return ENOMEM;
tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
io->destip = servip;
tftpfile->off = 0;
tftpfile->path = path; /* XXXXXXX we hope it's static */
res = tftp_makereq(tftpfile);
if (res) {
free(tftpfile, sizeof(*tftpfile));
return res;
}
f->f_fsdata = (void *) tftpfile;
return 0;
}
int
tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid)
{
struct tftp_handle *tftpfile;
#if !defined(LIBSA_NO_TWIDDLE)
static int tc = 0;
#endif
tftpfile = (struct tftp_handle *) f->f_fsdata;
while (size > 0) {
int needblock;
size_t count;
needblock = tftpfile->off / SEGSIZE + 1;
if (tftpfile->currblock > needblock) { /* seek backwards */
#ifndef TFTP_NOTERMINATE
tftp_terminate(tftpfile);
#endif
/* Don't bother to check retval: it worked for open() */
tftp_makereq(tftpfile);
}
while (tftpfile->currblock < needblock) {
int res;
#if !defined(LIBSA_NO_TWIDDLE)
if ((tc++ % 16) == 0)
twiddle();
#endif
res = tftp_getnextblock(tftpfile);
if (res) { /* no answer */
#ifdef DEBUG
printf("tftp: read error (block %d->%d)\n",
tftpfile->currblock, needblock);
#endif
return res;
}
if (tftpfile->islastblock)
break;
}
if (tftpfile->currblock == needblock) {
size_t offinblock, inbuffer;
offinblock = tftpfile->off % SEGSIZE;
if (offinblock > tftpfile->validsize) {
#ifdef DEBUG
printf("tftp: invalid offset %d\n",
tftpfile->off);
#endif
return EINVAL;
}
inbuffer = tftpfile->validsize - offinblock;
count = (size < inbuffer ? size : inbuffer);
bcopy(tftpfile->lastdata.t.th_data + offinblock,
addr, count);
addr = (caddr_t)addr + count;
tftpfile->off += count;
size -= count;
if ((tftpfile->islastblock) && (count == inbuffer))
break; /* EOF */
} else {
#ifdef DEBUG
printf("tftp: block %d not found\n", needblock);
#endif
return EINVAL;
}
}
if (resid != NULL)
*resid = size;
return 0;
}
int
tftp_close(struct open_file *f)
{
struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata;
#ifdef TFTP_NOTERMINATE
/* let it time out ... */
#else
tftp_terminate(tftpfile);
#endif
free(tftpfile, sizeof(*tftpfile));
return 0;
}
int
tftp_write(struct open_file *f, void *start, size_t size, size_t *resid)
{
return EROFS;
}
int
tftp_stat(struct open_file *f, struct stat *sb)
{
struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata;
sb->st_mode = 0444;
sb->st_nlink = 1;
sb->st_uid = 0;
sb->st_gid = 0;
sb->st_size = -1;
return 0;
}
off_t
tftp_seek(struct open_file *f, off_t offset, int where)
{
struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata;
switch (where) {
case SEEK_SET:
tftpfile->off = offset;
break;
case SEEK_CUR:
tftpfile->off += offset;
break;
default:
errno = EOFFSET;
return -1;
}
return (tftpfile->off);
}
/*
* Not implemented.
*/
#ifndef NO_READDIR
int
tftp_readdir(struct open_file *f, char *name)
{
return EROFS;
}
#endif
|