VirtualBox

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

Last change on this file since 9144 was 5401, checked in by vboxsync, 17 years ago

gcc warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 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
27
28static void tftp_session_update(PNATState pData, struct tftp_session *spt)
29{
30 spt->timestamp = curtime;
31 spt->in_use = 1;
32}
33
34static void tftp_session_terminate(struct tftp_session *spt)
35{
36 spt->in_use = 0;
37}
38
39static int tftp_session_allocate(PNATState pData, struct tftp_t *tp)
40{
41 struct tftp_session *spt;
42 int k;
43
44 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
45 spt = &tftp_sessions[k];
46
47 if (!spt->in_use)
48 goto found;
49
50 /* sessions time out after 5 inactive seconds */
51 if ((int)(curtime - spt->timestamp) > 5000)
52 goto found;
53 }
54
55 return -1;
56
57 found:
58 memset(spt, 0, sizeof(*spt));
59 memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
60 spt->client_port = tp->udp.uh_sport;
61
62 tftp_session_update(pData, spt);
63
64 return k;
65}
66
67static int tftp_session_find(PNATState pData, struct tftp_t *tp)
68{
69 struct tftp_session *spt;
70 int k;
71
72 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
73 spt = &tftp_sessions[k];
74
75 if (spt->in_use) {
76 if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
77 if (spt->client_port == tp->udp.uh_sport) {
78 return k;
79 }
80 }
81 }
82 }
83
84 return -1;
85}
86
87static int tftp_read_data(PNATState pData, struct tftp_session *spt, u_int16_t block_nr,
88 u_int8_t *buf, int len)
89{
90 int fd;
91 int bytes_read = 0;
92 char buffer[1024];
93 int n;
94
95 n = RTStrPrintf(buffer, sizeof(buffer), "%s/%s",
96 tftp_prefix, spt->filename);
97 if (n >= sizeof(buffer))
98 return -1;
99
100 fd = open(buffer, O_RDONLY | O_BINARY);
101
102 if (fd < 0) {
103 return -1;
104 }
105
106 if (len) {
107 lseek(fd, block_nr * 512, SEEK_SET);
108
109 bytes_read = read(fd, buf, len);
110 }
111
112 close(fd);
113
114 return bytes_read;
115}
116
117static int tftp_send_oack(PNATState pData,
118 struct tftp_session *spt,
119 const char *key, uint32_t value,
120 struct tftp_t *recv_tp)
121{
122 struct sockaddr_in saddr, daddr;
123 struct mbuf *m;
124 struct tftp_t *tp;
125 int n = 0;
126
127 m = m_get(pData);
128
129 if (!m)
130 return -1;
131
132 memset(m->m_data, 0, m->m_size);
133
134 m->m_data += if_maxlinkhdr;
135 tp = (void *)m->m_data;
136 m->m_data += sizeof(struct udpiphdr);
137
138 tp->tp_op = htons(TFTP_OACK);
139 n += sprintf((char *)tp->x.tp_buf + n, "%s", key) + 1;
140 n += sprintf((char *)tp->x.tp_buf + n, "%u", value) + 1;
141
142 saddr.sin_addr = recv_tp->ip.ip_dst;
143 saddr.sin_port = recv_tp->udp.uh_dport;
144
145 daddr.sin_addr = spt->client_ip;
146 daddr.sin_port = spt->client_port;
147
148 m->m_len = sizeof(struct tftp_t) - 514 + n -
149 sizeof(struct ip) - sizeof(struct udphdr);
150 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
151
152 return 0;
153}
154
155
156
157static int tftp_send_error(PNATState pData,
158 struct tftp_session *spt,
159 u_int16_t errorcode, const char *msg,
160 struct tftp_t *recv_tp)
161{
162 struct sockaddr_in saddr, daddr;
163 struct mbuf *m;
164 struct tftp_t *tp;
165 int nobytes;
166
167 m = m_get(pData);
168
169 if (!m) {
170 return -1;
171 }
172
173 memset(m->m_data, 0, m->m_size);
174
175 m->m_data += if_maxlinkhdr;
176 tp = (void *)m->m_data;
177 m->m_data += sizeof(struct udpiphdr);
178
179 tp->tp_op = htons(TFTP_ERROR);
180 tp->x.tp_error.tp_error_code = htons(errorcode);
181 strcpy((char *)tp->x.tp_error.tp_msg, msg);
182
183 saddr.sin_addr = recv_tp->ip.ip_dst;
184 saddr.sin_port = recv_tp->udp.uh_dport;
185
186 daddr.sin_addr = spt->client_ip;
187 daddr.sin_port = spt->client_port;
188
189 nobytes = 2;
190
191 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
192 sizeof(struct ip) - sizeof(struct udphdr);
193
194 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
195
196 tftp_session_terminate(spt);
197
198 return 0;
199}
200
201static int tftp_send_data(PNATState pData,
202 struct tftp_session *spt,
203 u_int16_t block_nr,
204 struct tftp_t *recv_tp)
205{
206 struct sockaddr_in saddr, daddr;
207 struct mbuf *m;
208 struct tftp_t *tp;
209 int nobytes;
210
211 if (block_nr < 1) {
212 return -1;
213 }
214
215 m = m_get(pData);
216
217 if (!m) {
218 return -1;
219 }
220
221 memset(m->m_data, 0, m->m_size);
222
223 m->m_data += if_maxlinkhdr;
224 tp = (void *)m->m_data;
225 m->m_data += sizeof(struct udpiphdr);
226
227 tp->tp_op = htons(TFTP_DATA);
228 tp->x.tp_data.tp_block_nr = htons(block_nr);
229
230 saddr.sin_addr = recv_tp->ip.ip_dst;
231 saddr.sin_port = recv_tp->udp.uh_dport;
232
233 daddr.sin_addr = spt->client_ip;
234 daddr.sin_port = spt->client_port;
235
236 nobytes = tftp_read_data(pData, spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
237
238 if (nobytes < 0) {
239 m_free(pData, m);
240
241 /* send "file not found" error back */
242
243 tftp_send_error(pData, spt, 1, "File not found", tp);
244
245 return -1;
246 }
247
248 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
249 sizeof(struct ip) - sizeof(struct udphdr);
250
251 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
252
253 if (nobytes == 512) {
254 tftp_session_update(pData, spt);
255 }
256 else {
257 tftp_session_terminate(spt);
258 }
259
260 return 0;
261}
262
263static void tftp_handle_rrq(PNATState pData, struct tftp_t *tp, int pktlen)
264{
265 struct tftp_session *spt;
266 int s, k, n;
267 u_int8_t *src, *dst;
268
269 s = tftp_session_allocate(pData, tp);
270
271 if (s < 0) {
272 return;
273 }
274
275 spt = &tftp_sessions[s];
276
277 src = tp->x.tp_buf;
278 dst = spt->filename;
279 n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
280
281 /* get name */
282
283 for (k = 0; k < n; k++) {
284 if (k < TFTP_FILENAME_MAX) {
285 dst[k] = src[k];
286 }
287 else {
288 return;
289 }
290
291 if (src[k] == '\0') {
292 break;
293 }
294 }
295
296 if (k >= n) {
297 return;
298 }
299
300 k++;
301
302 /* check mode */
303 if ((n - k) < 6) {
304 return;
305 }
306
307 if (memcmp(&src[k], "octet\0", 6) != 0) {
308 tftp_send_error(pData, spt, 4, "Unsupported transfer mode", tp);
309 return;
310 }
311
312 k += 6; /* skipping octet */
313
314 /* do sanity checks on the filename */
315
316 if ( !strncmp((const char*)spt->filename, "../", 3)
317 || (spt->filename[strlen((const char *)spt->filename) - 1] == '/')
318 || strstr((const char *)spt->filename, "/../")) {
319 tftp_send_error(pData, spt, 2, "Access violation", tp);
320 return;
321 }
322
323 /* only allow exported prefixes */
324
325 if (!tftp_prefix) {
326 tftp_send_error(pData, spt, 2, "Access violation", tp);
327 return;
328 }
329
330 /* check if the file exists */
331
332 if (tftp_read_data(pData, spt, 0, spt->filename, 0) < 0) {
333 tftp_send_error(pData, spt, 1, "File not found", tp);
334 return;
335 }
336
337 if (src[n - 1] != 0) {
338 tftp_send_error(pData, spt, 2, "Access violation", tp);
339 return;
340 }
341
342 while (k < n) {
343 const char *key, *value;
344
345 key = (const char *)src + k;
346 k += strlen(key) + 1;
347
348 if (k >= n) {
349 tftp_send_error(pData, spt, 2, "Access violation", tp);
350 return;
351 }
352
353 value = (const char *)src + k;
354 k += strlen(value) + 1;
355
356 if (strcmp(key, "tsize") == 0) {
357 int tsize = atoi(value);
358 struct stat stat_p;
359
360 if (tsize == 0 && tftp_prefix) {
361 char buffer[1024];
362 int len;
363
364 len = RTStrPrintf(buffer, sizeof(buffer), "%s/%s",
365 tftp_prefix, spt->filename);
366
367 if (stat(buffer, &stat_p) == 0)
368 tsize = stat_p.st_size;
369 else {
370 tftp_send_error(pData, spt, 1, "File not found", tp);
371 return;
372 }
373 }
374
375 tftp_send_oack(pData, spt, "tsize", tsize, tp);
376 }
377 }
378
379 tftp_send_data(pData, spt, 1, tp);
380}
381
382static void tftp_handle_ack(PNATState pData, struct tftp_t *tp, int pktlen)
383{
384 int s;
385
386 s = tftp_session_find(pData, tp);
387
388 if (s < 0) {
389 return;
390 }
391
392 if (tftp_send_data(pData, &tftp_sessions[s],
393 ntohs(tp->x.tp_data.tp_block_nr) + 1,
394 tp) < 0) {
395 return;
396 }
397}
398
399void tftp_input(PNATState pData, struct mbuf *m)
400{
401 struct tftp_t *tp = (struct tftp_t *)m->m_data;
402
403 switch(ntohs(tp->tp_op)) {
404 case TFTP_RRQ:
405 tftp_handle_rrq(pData, tp, m->m_len);
406 break;
407
408 case TFTP_ACK:
409 tftp_handle_ack(pData, tp, m->m_len);
410 break;
411 }
412}
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