VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/tftp.c@ 1016

Last change on this file since 1016 was 1016, checked in by vboxsync, 18 years ago

slirp: resync with qemu: added OACK support to tftp server; changed tftp_prefix to take a root directory

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1/*
2 * tftp.c - a simple, read-only tftp server for qemu
3 *
4 * Copyright (c) 2004 Magnus Damm <[email protected]>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include <slirp.h>
26
27struct tftp_session {
28 int in_use;
29 unsigned char filename[TFTP_FILENAME_MAX];
30
31 struct in_addr client_ip;
32 u_int16_t client_port;
33
34 int timestamp;
35};
36
37struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
38
39const char *tftp_prefix;
40
41static void tftp_session_update(struct tftp_session *spt)
42{
43 spt->timestamp = curtime;
44 spt->in_use = 1;
45}
46
47static void tftp_session_terminate(struct tftp_session *spt)
48{
49 spt->in_use = 0;
50}
51
52static int tftp_session_allocate(struct tftp_t *tp)
53{
54 struct tftp_session *spt;
55 int k;
56
57 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
58 spt = &tftp_sessions[k];
59
60 if (!spt->in_use)
61 goto found;
62
63 /* sessions time out after 5 inactive seconds */
64 if ((int)(curtime - spt->timestamp) > 5000)
65 goto found;
66 }
67
68 return -1;
69
70 found:
71 memset(spt, 0, sizeof(*spt));
72 memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
73 spt->client_port = tp->udp.uh_sport;
74
75 tftp_session_update(spt);
76
77 return k;
78}
79
80static int tftp_session_find(struct tftp_t *tp)
81{
82 struct tftp_session *spt;
83 int k;
84
85 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
86 spt = &tftp_sessions[k];
87
88 if (spt->in_use) {
89 if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
90 if (spt->client_port == tp->udp.uh_sport) {
91 return k;
92 }
93 }
94 }
95 }
96
97 return -1;
98}
99
100static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr,
101 u_int8_t *buf, int len)
102{
103 int fd;
104 int bytes_read = 0;
105 char buffer[1024];
106 int n;
107
108 n = snprintf(buffer, sizeof(buffer), "%s/%s",
109 tftp_prefix, spt->filename);
110 if (n >= sizeof(buffer))
111 return -1;
112
113 fd = open(buffer, O_RDONLY | O_BINARY);
114
115 if (fd < 0) {
116 return -1;
117 }
118
119 if (len) {
120 lseek(fd, block_nr * 512, SEEK_SET);
121
122 bytes_read = read(fd, buf, len);
123 }
124
125 close(fd);
126
127 return bytes_read;
128}
129
130static int tftp_send_oack(struct tftp_session *spt,
131 const char *key, uint32_t value,
132 struct tftp_t *recv_tp)
133{
134 struct sockaddr_in saddr, daddr;
135 struct mbuf *m;
136 struct tftp_t *tp;
137 int n = 0;
138
139 m = m_get();
140
141 if (!m)
142 return -1;
143
144 memset(m->m_data, 0, m->m_size);
145
146 m->m_data += if_maxlinkhdr;
147 tp = (void *)m->m_data;
148 m->m_data += sizeof(struct udpiphdr);
149
150 tp->tp_op = htons(TFTP_OACK);
151 n += sprintf(tp->x.tp_buf + n, "%s", key) + 1;
152 n += sprintf(tp->x.tp_buf + n, "%u", value) + 1;
153
154 saddr.sin_addr = recv_tp->ip.ip_dst;
155 saddr.sin_port = recv_tp->udp.uh_dport;
156
157 daddr.sin_addr = spt->client_ip;
158 daddr.sin_port = spt->client_port;
159
160 m->m_len = sizeof(struct tftp_t) - 514 + n -
161 sizeof(struct ip) - sizeof(struct udphdr);
162 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
163
164 return 0;
165}
166
167
168
169static int tftp_send_error(struct tftp_session *spt,
170 u_int16_t errorcode, const char *msg,
171 struct tftp_t *recv_tp)
172{
173 struct sockaddr_in saddr, daddr;
174 struct mbuf *m;
175 struct tftp_t *tp;
176 int nobytes;
177
178 m = m_get();
179
180 if (!m) {
181 return -1;
182 }
183
184 memset(m->m_data, 0, m->m_size);
185
186 m->m_data += if_maxlinkhdr;
187 tp = (void *)m->m_data;
188 m->m_data += sizeof(struct udpiphdr);
189
190 tp->tp_op = htons(TFTP_ERROR);
191 tp->x.tp_error.tp_error_code = htons(errorcode);
192#ifndef VBOX
193 strcpy(tp->x.tp_error.tp_msg, msg);
194#else /* VBOX */
195 strcpy((char *)tp->x.tp_error.tp_msg, msg);
196#endif /* VBOX */
197
198 saddr.sin_addr = recv_tp->ip.ip_dst;
199 saddr.sin_port = recv_tp->udp.uh_dport;
200
201 daddr.sin_addr = spt->client_ip;
202 daddr.sin_port = spt->client_port;
203
204 nobytes = 2;
205
206 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
207 sizeof(struct ip) - sizeof(struct udphdr);
208
209 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
210
211 tftp_session_terminate(spt);
212
213 return 0;
214}
215
216static int tftp_send_data(struct tftp_session *spt,
217 u_int16_t block_nr,
218 struct tftp_t *recv_tp)
219{
220 struct sockaddr_in saddr, daddr;
221 struct mbuf *m;
222 struct tftp_t *tp;
223 int nobytes;
224
225 if (block_nr < 1) {
226 return -1;
227 }
228
229 m = m_get();
230
231 if (!m) {
232 return -1;
233 }
234
235 memset(m->m_data, 0, m->m_size);
236
237 m->m_data += if_maxlinkhdr;
238 tp = (void *)m->m_data;
239 m->m_data += sizeof(struct udpiphdr);
240
241 tp->tp_op = htons(TFTP_DATA);
242 tp->x.tp_data.tp_block_nr = htons(block_nr);
243
244 saddr.sin_addr = recv_tp->ip.ip_dst;
245 saddr.sin_port = recv_tp->udp.uh_dport;
246
247 daddr.sin_addr = spt->client_ip;
248 daddr.sin_port = spt->client_port;
249
250 nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
251
252 if (nobytes < 0) {
253 m_free(m);
254
255 /* send "file not found" error back */
256
257 tftp_send_error(spt, 1, "File not found", tp);
258
259 return -1;
260 }
261
262 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
263 sizeof(struct ip) - sizeof(struct udphdr);
264
265 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
266
267 if (nobytes == 512) {
268 tftp_session_update(spt);
269 }
270 else {
271 tftp_session_terminate(spt);
272 }
273
274 return 0;
275}
276
277static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
278{
279 struct tftp_session *spt;
280 int s, k, n;
281 u_int8_t *src, *dst;
282
283 s = tftp_session_allocate(tp);
284
285 if (s < 0) {
286 return;
287 }
288
289 spt = &tftp_sessions[s];
290
291 src = tp->x.tp_buf;
292 dst = spt->filename;
293 n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
294
295 /* get name */
296
297 for (k = 0; k < n; k++) {
298 if (k < TFTP_FILENAME_MAX) {
299 dst[k] = src[k];
300 }
301 else {
302 return;
303 }
304
305 if (src[k] == '\0') {
306 break;
307 }
308 }
309
310 if (k >= n) {
311 return;
312 }
313
314 k++;
315
316 /* check mode */
317 if ((n - k) < 6) {
318 return;
319 }
320
321 if (memcmp(&src[k], "octet\0", 6) != 0) {
322 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
323 return;
324 }
325
326 k += 6; /* skipping octet */
327
328 /* do sanity checks on the filename */
329
330#ifndef VBOX
331 if ((spt->filename[0] != '/')
332 || (spt->filename[strlen(spt->filename) - 1] == '/')
333 || strstr(spt->filename, "/../")) {
334#else /* VBOX */
335 if ((spt->filename[0] != '/')
336 || (spt->filename[strlen((const char *)spt->filename) - 1] == '/')
337 || strstr((char *)spt->filename, "/../")) {
338#endif /* VBOX */
339 tftp_send_error(spt, 2, "Access violation", tp);
340 return;
341 }
342
343 /* only allow exported prefixes */
344
345 if (!tftp_prefix) {
346 tftp_send_error(spt, 2, "Access violation", tp);
347 return;
348 }
349
350 /* check if the file exists */
351
352 if (tftp_read_data(spt, 0, spt->filename, 0) < 0) {
353 tftp_send_error(spt, 1, "File not found", tp);
354 return;
355 }
356
357 if (src[n - 1] != 0) {
358 tftp_send_error(spt, 2, "Access violation", tp);
359 return;
360 }
361
362 while (k < n) {
363 const char *key, *value;
364
365 key = src + k;
366 k += strlen(key) + 1;
367
368 if (k >= n) {
369 tftp_send_error(spt, 2, "Access violation", tp);
370 return;
371 }
372
373 value = src + k;
374 k += strlen(value) + 1;
375
376 if (strcmp(key, "tsize") == 0) {
377 int tsize = atoi(value);
378 struct stat stat_p;
379
380 if (tsize == 0 && tftp_prefix) {
381 char buffer[1024];
382 int len;
383
384 len = snprintf(buffer, sizeof(buffer), "%s/%s",
385 tftp_prefix, spt->filename);
386
387 if (stat(buffer, &stat_p) == 0)
388 tsize = stat_p.st_size;
389 else {
390 tftp_send_error(spt, 1, "File not found", tp);
391 return;
392 }
393 }
394
395 tftp_send_oack(spt, "tsize", tsize, tp);
396 }
397 }
398
399 tftp_send_data(spt, 1, tp);
400}
401
402static void tftp_handle_ack(struct tftp_t *tp, int pktlen)
403{
404 int s;
405
406 s = tftp_session_find(tp);
407
408 if (s < 0) {
409 return;
410 }
411
412 if (tftp_send_data(&tftp_sessions[s],
413 ntohs(tp->x.tp_data.tp_block_nr) + 1,
414 tp) < 0) {
415 return;
416 }
417}
418
419void tftp_input(struct mbuf *m)
420{
421 struct tftp_t *tp = (struct tftp_t *)m->m_data;
422
423 switch(ntohs(tp->tp_op)) {
424 case TFTP_RRQ:
425 tftp_handle_rrq(tp, m->m_len);
426 break;
427
428 case TFTP_ACK:
429 tftp_handle_ack(tp, m->m_len);
430 break;
431 }
432}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette