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