]> git.proxmox.com Git - mirror_qemu.git/blob - slirp/tftp.c
slirp: remove now useless QEMU headers inclusions
[mirror_qemu.git] / slirp / tftp.c
1 /*
2 * tftp.c - a simple, read-only tftp server for qemu
3 *
4 * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
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 "qemu/osdep.h"
26 #include "slirp.h"
27 #include "qemu-common.h"
28
29 static inline int tftp_session_in_use(struct tftp_session *spt)
30 {
31 return (spt->slirp != NULL);
32 }
33
34 static inline void tftp_session_update(struct tftp_session *spt)
35 {
36 spt->timestamp = curtime;
37 }
38
39 static void tftp_session_terminate(struct tftp_session *spt)
40 {
41 if (spt->fd >= 0) {
42 close(spt->fd);
43 spt->fd = -1;
44 }
45 g_free(spt->filename);
46 spt->slirp = NULL;
47 }
48
49 static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
50 struct tftp_t *tp)
51 {
52 struct tftp_session *spt;
53 int k;
54
55 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
56 spt = &slirp->tftp_sessions[k];
57
58 if (!tftp_session_in_use(spt))
59 goto found;
60
61 /* sessions time out after 5 inactive seconds */
62 if ((int)(curtime - spt->timestamp) > 5000) {
63 tftp_session_terminate(spt);
64 goto found;
65 }
66 }
67
68 return -1;
69
70 found:
71 memset(spt, 0, sizeof(*spt));
72 memcpy(&spt->client_addr, srcsas, sockaddr_size(srcsas));
73 spt->fd = -1;
74 spt->block_size = 512;
75 spt->client_port = tp->udp.uh_sport;
76 spt->slirp = slirp;
77
78 tftp_session_update(spt);
79
80 return k;
81 }
82
83 static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
84 struct tftp_t *tp)
85 {
86 struct tftp_session *spt;
87 int k;
88
89 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
90 spt = &slirp->tftp_sessions[k];
91
92 if (tftp_session_in_use(spt)) {
93 if (sockaddr_equal(&spt->client_addr, srcsas)) {
94 if (spt->client_port == tp->udp.uh_sport) {
95 return k;
96 }
97 }
98 }
99 }
100
101 return -1;
102 }
103
104 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
105 uint8_t *buf, int len)
106 {
107 int bytes_read = 0;
108
109 if (spt->fd < 0) {
110 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
111 }
112
113 if (spt->fd < 0) {
114 return -1;
115 }
116
117 if (len) {
118 lseek(spt->fd, block_nr * spt->block_size, SEEK_SET);
119
120 bytes_read = read(spt->fd, buf, len);
121 }
122
123 return bytes_read;
124 }
125
126 static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
127 struct mbuf *m)
128 {
129 struct tftp_t *tp;
130
131 memset(m->m_data, 0, m->m_size);
132
133 m->m_data += IF_MAXLINKHDR;
134 if (spt->client_addr.ss_family == AF_INET6) {
135 m->m_data += sizeof(struct ip6);
136 } else {
137 m->m_data += sizeof(struct ip);
138 }
139 tp = (void *)m->m_data;
140 m->m_data += sizeof(struct udphdr);
141
142 return tp;
143 }
144
145 static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
146 struct tftp_t *recv_tp)
147 {
148 if (spt->client_addr.ss_family == AF_INET6) {
149 struct sockaddr_in6 sa6, da6;
150
151 sa6.sin6_addr = spt->slirp->vhost_addr6;
152 sa6.sin6_port = recv_tp->udp.uh_dport;
153 da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
154 da6.sin6_port = spt->client_port;
155
156 udp6_output(NULL, m, &sa6, &da6);
157 } else {
158 struct sockaddr_in sa4, da4;
159
160 sa4.sin_addr = spt->slirp->vhost_addr;
161 sa4.sin_port = recv_tp->udp.uh_dport;
162 da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
163 da4.sin_port = spt->client_port;
164
165 udp_output(NULL, m, &sa4, &da4, IPTOS_LOWDELAY);
166 }
167 }
168
169 static int tftp_send_oack(struct tftp_session *spt,
170 const char *keys[], uint32_t values[], int nb,
171 struct tftp_t *recv_tp)
172 {
173 struct mbuf *m;
174 struct tftp_t *tp;
175 int i, n = 0;
176
177 m = m_get(spt->slirp);
178
179 if (!m)
180 return -1;
181
182 tp = tftp_prep_mbuf_data(spt, m);
183
184 tp->tp_op = htons(TFTP_OACK);
185 for (i = 0; i < nb; i++) {
186 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
187 keys[i]) + 1;
188 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
189 values[i]) + 1;
190 }
191
192 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + n
193 - sizeof(struct udphdr);
194 tftp_udp_output(spt, m, recv_tp);
195
196 return 0;
197 }
198
199 static void tftp_send_error(struct tftp_session *spt,
200 uint16_t errorcode, const char *msg,
201 struct tftp_t *recv_tp)
202 {
203 struct mbuf *m;
204 struct tftp_t *tp;
205
206 DEBUG_TFTP("tftp error msg: %s", msg);
207
208 m = m_get(spt->slirp);
209
210 if (!m) {
211 goto out;
212 }
213
214 tp = tftp_prep_mbuf_data(spt, m);
215
216 tp->tp_op = htons(TFTP_ERROR);
217 tp->x.tp_error.tp_error_code = htons(errorcode);
218 slirp_pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
219
220 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + 3 + strlen(msg)
221 - sizeof(struct udphdr);
222 tftp_udp_output(spt, m, recv_tp);
223
224 out:
225 tftp_session_terminate(spt);
226 }
227
228 static void tftp_send_next_block(struct tftp_session *spt,
229 struct tftp_t *recv_tp)
230 {
231 struct mbuf *m;
232 struct tftp_t *tp;
233 int nobytes;
234
235 m = m_get(spt->slirp);
236
237 if (!m) {
238 return;
239 }
240
241 tp = tftp_prep_mbuf_data(spt, m);
242
243 tp->tp_op = htons(TFTP_DATA);
244 tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
245
246 nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf,
247 spt->block_size);
248
249 if (nobytes < 0) {
250 m_free(m);
251
252 /* send "file not found" error back */
253
254 tftp_send_error(spt, 1, "File not found", tp);
255
256 return;
257 }
258
259 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX - nobytes)
260 - sizeof(struct udphdr);
261 tftp_udp_output(spt, m, recv_tp);
262
263 if (nobytes == spt->block_size) {
264 tftp_session_update(spt);
265 }
266 else {
267 tftp_session_terminate(spt);
268 }
269
270 spt->block_nr++;
271 }
272
273 static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
274 struct tftp_t *tp, int pktlen)
275 {
276 struct tftp_session *spt;
277 int s, k;
278 size_t prefix_len;
279 char *req_fname;
280 const char *option_name[2];
281 uint32_t option_value[2];
282 int nb_options = 0;
283
284 /* check if a session already exists and if so terminate it */
285 s = tftp_session_find(slirp, srcsas, tp);
286 if (s >= 0) {
287 tftp_session_terminate(&slirp->tftp_sessions[s]);
288 }
289
290 s = tftp_session_allocate(slirp, srcsas, tp);
291
292 if (s < 0) {
293 return;
294 }
295
296 spt = &slirp->tftp_sessions[s];
297
298 /* unspecified prefix means service disabled */
299 if (!slirp->tftp_prefix) {
300 tftp_send_error(spt, 2, "Access violation", tp);
301 return;
302 }
303
304 /* skip header fields */
305 k = 0;
306 pktlen -= offsetof(struct tftp_t, x.tp_buf);
307
308 /* prepend tftp_prefix */
309 prefix_len = strlen(slirp->tftp_prefix);
310 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
311 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
312 spt->filename[prefix_len] = '/';
313
314 /* get name */
315 req_fname = spt->filename + prefix_len + 1;
316
317 while (1) {
318 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
319 tftp_send_error(spt, 2, "Access violation", tp);
320 return;
321 }
322 req_fname[k] = tp->x.tp_buf[k];
323 if (req_fname[k++] == '\0') {
324 break;
325 }
326 }
327
328 DEBUG_TFTP("tftp rrq file: %s", req_fname);
329
330 /* check mode */
331 if ((pktlen - k) < 6) {
332 tftp_send_error(spt, 2, "Access violation", tp);
333 return;
334 }
335
336 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
337 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
338 return;
339 }
340
341 k += 6; /* skipping octet */
342
343 /* do sanity checks on the filename */
344 if (!strncmp(req_fname, "../", 3) ||
345 req_fname[strlen(req_fname) - 1] == '/' ||
346 strstr(req_fname, "/../")) {
347 tftp_send_error(spt, 2, "Access violation", tp);
348 return;
349 }
350
351 /* check if the file exists */
352 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
353 tftp_send_error(spt, 1, "File not found", tp);
354 return;
355 }
356
357 if (tp->x.tp_buf[pktlen - 1] != 0) {
358 tftp_send_error(spt, 2, "Access violation", tp);
359 return;
360 }
361
362 while (k < pktlen && nb_options < G_N_ELEMENTS(option_name)) {
363 const char *key, *value;
364
365 key = &tp->x.tp_buf[k];
366 k += strlen(key) + 1;
367
368 if (k >= pktlen) {
369 tftp_send_error(spt, 2, "Access violation", tp);
370 return;
371 }
372
373 value = &tp->x.tp_buf[k];
374 k += strlen(value) + 1;
375
376 if (strcasecmp(key, "tsize") == 0) {
377 int tsize = atoi(value);
378 struct stat stat_p;
379
380 if (tsize == 0) {
381 if (stat(spt->filename, &stat_p) == 0)
382 tsize = stat_p.st_size;
383 else {
384 tftp_send_error(spt, 1, "File not found", tp);
385 return;
386 }
387 }
388
389 option_name[nb_options] = "tsize";
390 option_value[nb_options] = tsize;
391 nb_options++;
392 } else if (strcasecmp(key, "blksize") == 0) {
393 int blksize = atoi(value);
394
395 /* Accept blksize up to our maximum size */
396 if (blksize > 0) {
397 spt->block_size = MIN(blksize, TFTP_BLOCKSIZE_MAX);
398 option_name[nb_options] = "blksize";
399 option_value[nb_options] = spt->block_size;
400 nb_options++;
401 }
402 }
403 }
404
405 if (nb_options > 0) {
406 assert(nb_options <= G_N_ELEMENTS(option_name));
407 tftp_send_oack(spt, option_name, option_value, nb_options, tp);
408 return;
409 }
410
411 spt->block_nr = 0;
412 tftp_send_next_block(spt, tp);
413 }
414
415 static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
416 struct tftp_t *tp, int pktlen)
417 {
418 int s;
419
420 s = tftp_session_find(slirp, srcsas, tp);
421
422 if (s < 0) {
423 return;
424 }
425
426 tftp_send_next_block(&slirp->tftp_sessions[s], tp);
427 }
428
429 static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
430 struct tftp_t *tp, int pktlen)
431 {
432 int s;
433
434 s = tftp_session_find(slirp, srcsas, tp);
435
436 if (s < 0) {
437 return;
438 }
439
440 tftp_session_terminate(&slirp->tftp_sessions[s]);
441 }
442
443 void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
444 {
445 struct tftp_t *tp = (struct tftp_t *)m->m_data;
446
447 switch(ntohs(tp->tp_op)) {
448 case TFTP_RRQ:
449 tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
450 break;
451
452 case TFTP_ACK:
453 tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
454 break;
455
456 case TFTP_ERROR:
457 tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
458 break;
459 }
460 }