]>
Commit | Line | Data |
---|---|---|
c7f74643 FB |
1 | /* |
2 | * tftp.c - a simple, read-only tftp server for qemu | |
5fafdf24 | 3 | * |
c7f74643 | 4 | * Copyright (c) 2004 Magnus Damm <[email protected]> |
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 |
30 | static inline int tftp_session_in_use(struct tftp_session *spt) |
31 | { | |
32 | return (spt->slirp != NULL); | |
33 | } | |
c7f74643 | 34 | |
460fec67 | 35 | static inline void tftp_session_update(struct tftp_session *spt) |
c7f74643 | 36 | { |
a3504c87 | 37 | spt->timestamp = curtime; |
c7f74643 FB |
38 | } |
39 | ||
40 | static 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 |
50 | static 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; |
c7f74643 | 75 | spt->client_port = tp->udp.uh_sport; |
460fec67 | 76 | spt->slirp = slirp; |
c7f74643 FB |
77 | |
78 | tftp_session_update(spt); | |
79 | ||
80 | return k; | |
81 | } | |
82 | ||
fad7fb9c TH |
83 | static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas, |
84 | struct tftp_t *tp) | |
c7f74643 FB |
85 | { |
86 | struct tftp_session *spt; | |
87 | int k; | |
88 | ||
89 | for (k = 0; k < TFTP_SESSIONS_MAX; k++) { | |
460fec67 | 90 | spt = &slirp->tftp_sessions[k]; |
c7f74643 | 91 | |
460fec67 | 92 | if (tftp_session_in_use(spt)) { |
fad7fb9c | 93 | if (sockaddr_equal(&spt->client_addr, srcsas)) { |
c7f74643 FB |
94 | if (spt->client_port == tp->udp.uh_sport) { |
95 | return k; | |
96 | } | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | return -1; | |
102 | } | |
103 | ||
4aa401f3 | 104 | static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr, |
b6dce92e | 105 | uint8_t *buf, int len) |
c7f74643 | 106 | { |
78be0566 | 107 | int bytes_read = 0; |
c7f74643 | 108 | |
78be0566 HP |
109 | if (spt->fd < 0) { |
110 | spt->fd = open(spt->filename, O_RDONLY | O_BINARY); | |
111 | } | |
c7f74643 | 112 | |
78be0566 HP |
113 | if (spt->fd < 0) { |
114 | return -1; | |
115 | } | |
c7f74643 | 116 | |
78be0566 HP |
117 | if (len) { |
118 | lseek(spt->fd, block_nr * 512, SEEK_SET); | |
c7f74643 | 119 | |
78be0566 HP |
120 | bytes_read = read(spt->fd, buf, len); |
121 | } | |
c7f74643 | 122 | |
78be0566 | 123 | return bytes_read; |
c7f74643 FB |
124 | } |
125 | ||
fad7fb9c TH |
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 | ||
5fafdf24 | 169 | static int tftp_send_oack(struct tftp_session *spt, |
95b1ad7a | 170 | const char *keys[], uint32_t values[], int nb, |
1f697db9 TS |
171 | struct tftp_t *recv_tp) |
172 | { | |
1f697db9 TS |
173 | struct mbuf *m; |
174 | struct tftp_t *tp; | |
95b1ad7a | 175 | int i, n = 0; |
1f697db9 | 176 | |
460fec67 | 177 | m = m_get(spt->slirp); |
1f697db9 TS |
178 | |
179 | if (!m) | |
fad7fb9c | 180 | return -1; |
1f697db9 | 181 | |
fad7fb9c | 182 | tp = tftp_prep_mbuf_data(spt, m); |
3b46e624 | 183 | |
1f697db9 | 184 | tp->tp_op = htons(TFTP_OACK); |
95b1ad7a HP |
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 | } | |
1f697db9 | 191 | |
fad7fb9c TH |
192 | m->m_len = sizeof(struct tftp_t) - 514 + n - sizeof(struct udphdr); |
193 | tftp_udp_output(spt, m, recv_tp); | |
1f697db9 TS |
194 | |
195 | return 0; | |
196 | } | |
197 | ||
ef2d54d8 | 198 | static void tftp_send_error(struct tftp_session *spt, |
b6dce92e | 199 | uint16_t errorcode, const char *msg, |
ef2d54d8 | 200 | struct tftp_t *recv_tp) |
c7f74643 | 201 | { |
c7f74643 FB |
202 | struct mbuf *m; |
203 | struct tftp_t *tp; | |
c7f74643 | 204 | |
460fec67 | 205 | m = m_get(spt->slirp); |
c7f74643 FB |
206 | |
207 | if (!m) { | |
ef2d54d8 | 208 | goto out; |
c7f74643 FB |
209 | } |
210 | ||
fad7fb9c | 211 | tp = tftp_prep_mbuf_data(spt, m); |
3b46e624 | 212 | |
c7f74643 FB |
213 | tp->tp_op = htons(TFTP_ERROR); |
214 | tp->x.tp_error.tp_error_code = htons(errorcode); | |
b55266b5 | 215 | pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg); |
c7f74643 | 216 | |
fad7fb9c TH |
217 | m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) |
218 | - sizeof(struct udphdr); | |
219 | tftp_udp_output(spt, m, recv_tp); | |
c7f74643 | 220 | |
ef2d54d8 | 221 | out: |
c7f74643 | 222 | tftp_session_terminate(spt); |
c7f74643 FB |
223 | } |
224 | ||
eb7faf0e JK |
225 | static void tftp_send_next_block(struct tftp_session *spt, |
226 | struct tftp_t *recv_tp) | |
c7f74643 | 227 | { |
c7f74643 FB |
228 | struct mbuf *m; |
229 | struct tftp_t *tp; | |
230 | int nobytes; | |
231 | ||
460fec67 | 232 | m = m_get(spt->slirp); |
c7f74643 FB |
233 | |
234 | if (!m) { | |
eb7faf0e | 235 | return; |
c7f74643 FB |
236 | } |
237 | ||
fad7fb9c | 238 | tp = tftp_prep_mbuf_data(spt, m); |
3b46e624 | 239 | |
c7f74643 | 240 | tp->tp_op = htons(TFTP_DATA); |
4aa401f3 | 241 | tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff); |
c7f74643 | 242 | |
4aa401f3 | 243 | nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512); |
c7f74643 FB |
244 | |
245 | if (nobytes < 0) { | |
246 | m_free(m); | |
247 | ||
248 | /* send "file not found" error back */ | |
249 | ||
250 | tftp_send_error(spt, 1, "File not found", tp); | |
251 | ||
eb7faf0e | 252 | return; |
c7f74643 FB |
253 | } |
254 | ||
fad7fb9c TH |
255 | m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - sizeof(struct udphdr); |
256 | tftp_udp_output(spt, m, recv_tp); | |
c7f74643 FB |
257 | |
258 | if (nobytes == 512) { | |
259 | tftp_session_update(spt); | |
260 | } | |
261 | else { | |
262 | tftp_session_terminate(spt); | |
263 | } | |
264 | ||
4aa401f3 | 265 | spt->block_nr++; |
c7f74643 FB |
266 | } |
267 | ||
fad7fb9c TH |
268 | static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas, |
269 | struct tftp_t *tp, int pktlen) | |
c7f74643 FB |
270 | { |
271 | struct tftp_session *spt; | |
20c24bf2 | 272 | int s, k; |
9367964a | 273 | size_t prefix_len; |
20c24bf2 | 274 | char *req_fname; |
95b1ad7a HP |
275 | const char *option_name[2]; |
276 | uint32_t option_value[2]; | |
277 | int nb_options = 0; | |
c7f74643 | 278 | |
bfe4e172 | 279 | /* check if a session already exists and if so terminate it */ |
fad7fb9c | 280 | s = tftp_session_find(slirp, srcsas, tp); |
bfe4e172 TH |
281 | if (s >= 0) { |
282 | tftp_session_terminate(&slirp->tftp_sessions[s]); | |
283 | } | |
284 | ||
fad7fb9c | 285 | s = tftp_session_allocate(slirp, srcsas, tp); |
c7f74643 FB |
286 | |
287 | if (s < 0) { | |
288 | return; | |
289 | } | |
290 | ||
460fec67 | 291 | spt = &slirp->tftp_sessions[s]; |
c7f74643 | 292 | |
dc6fb73d | 293 | /* unspecified prefix means service disabled */ |
460fec67 | 294 | if (!slirp->tftp_prefix) { |
f8e3cbd3 JK |
295 | tftp_send_error(spt, 2, "Access violation", tp); |
296 | return; | |
297 | } | |
298 | ||
20c24bf2 JK |
299 | /* skip header fields */ |
300 | k = 0; | |
89d2d3af | 301 | pktlen -= offsetof(struct tftp_t, x.tp_buf); |
c7f74643 | 302 | |
9367964a | 303 | /* prepend tftp_prefix */ |
460fec67 | 304 | prefix_len = strlen(slirp->tftp_prefix); |
7267c094 | 305 | spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2); |
460fec67 | 306 | memcpy(spt->filename, slirp->tftp_prefix, prefix_len); |
74efd61a | 307 | spt->filename[prefix_len] = '/'; |
9367964a | 308 | |
c7f74643 | 309 | /* get name */ |
74efd61a | 310 | req_fname = spt->filename + prefix_len + 1; |
c7f74643 | 311 | |
20c24bf2 JK |
312 | while (1) { |
313 | if (k >= TFTP_FILENAME_MAX || k >= pktlen) { | |
314 | tftp_send_error(spt, 2, "Access violation", tp); | |
c7f74643 FB |
315 | return; |
316 | } | |
89d2d3af | 317 | req_fname[k] = tp->x.tp_buf[k]; |
20c24bf2 | 318 | if (req_fname[k++] == '\0') { |
c7f74643 FB |
319 | break; |
320 | } | |
321 | } | |
3b46e624 | 322 | |
c7f74643 | 323 | /* check mode */ |
20c24bf2 JK |
324 | if ((pktlen - k) < 6) { |
325 | tftp_send_error(spt, 2, "Access violation", tp); | |
c7f74643 FB |
326 | return; |
327 | } | |
3b46e624 | 328 | |
89d2d3af | 329 | if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) { |
c7f74643 FB |
330 | tftp_send_error(spt, 4, "Unsupported transfer mode", tp); |
331 | return; | |
332 | } | |
333 | ||
1f697db9 TS |
334 | k += 6; /* skipping octet */ |
335 | ||
c7f74643 | 336 | /* do sanity checks on the filename */ |
74efd61a JK |
337 | if (!strncmp(req_fname, "../", 3) || |
338 | req_fname[strlen(req_fname) - 1] == '/' || | |
9367964a | 339 | strstr(req_fname, "/../")) { |
c7f74643 FB |
340 | tftp_send_error(spt, 2, "Access violation", tp); |
341 | return; | |
342 | } | |
343 | ||
c7f74643 | 344 | /* check if the file exists */ |
20c24bf2 | 345 | if (tftp_read_data(spt, 0, NULL, 0) < 0) { |
c7f74643 FB |
346 | tftp_send_error(spt, 1, "File not found", tp); |
347 | return; | |
348 | } | |
349 | ||
20c24bf2 | 350 | if (tp->x.tp_buf[pktlen - 1] != 0) { |
1f697db9 TS |
351 | tftp_send_error(spt, 2, "Access violation", tp); |
352 | return; | |
353 | } | |
354 | ||
95b1ad7a | 355 | while (k < pktlen && nb_options < ARRAY_SIZE(option_name)) { |
1f697db9 TS |
356 | const char *key, *value; |
357 | ||
89d2d3af | 358 | key = &tp->x.tp_buf[k]; |
1f697db9 TS |
359 | k += strlen(key) + 1; |
360 | ||
20c24bf2 | 361 | if (k >= pktlen) { |
1f697db9 TS |
362 | tftp_send_error(spt, 2, "Access violation", tp); |
363 | return; | |
364 | } | |
365 | ||
89d2d3af | 366 | value = &tp->x.tp_buf[k]; |
1f697db9 TS |
367 | k += strlen(value) + 1; |
368 | ||
facf1a60 | 369 | if (strcasecmp(key, "tsize") == 0) { |
1f697db9 TS |
370 | int tsize = atoi(value); |
371 | struct stat stat_p; | |
372 | ||
f8e3cbd3 | 373 | if (tsize == 0) { |
9367964a | 374 | if (stat(spt->filename, &stat_p) == 0) |
1f697db9 TS |
375 | tsize = stat_p.st_size; |
376 | else { | |
377 | tftp_send_error(spt, 1, "File not found", tp); | |
378 | return; | |
379 | } | |
380 | } | |
381 | ||
95b1ad7a HP |
382 | option_name[nb_options] = "tsize"; |
383 | option_value[nb_options] = tsize; | |
384 | nb_options++; | |
385 | } else if (strcasecmp(key, "blksize") == 0) { | |
386 | int blksize = atoi(value); | |
387 | ||
388 | /* If blksize option is bigger than what we will | |
389 | * emit, accept the option with our packet size. | |
390 | * Otherwise, simply do as we didn't see the option. | |
391 | */ | |
392 | if (blksize >= 512) { | |
393 | option_name[nb_options] = "blksize"; | |
394 | option_value[nb_options] = 512; | |
395 | nb_options++; | |
396 | } | |
1f697db9 TS |
397 | } |
398 | } | |
399 | ||
95b1ad7a HP |
400 | if (nb_options > 0) { |
401 | assert(nb_options <= ARRAY_SIZE(option_name)); | |
402 | tftp_send_oack(spt, option_name, option_value, nb_options, tp); | |
403 | return; | |
404 | } | |
405 | ||
4aa401f3 HP |
406 | spt->block_nr = 0; |
407 | tftp_send_next_block(spt, tp); | |
c7f74643 FB |
408 | } |
409 | ||
fad7fb9c TH |
410 | static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas, |
411 | struct tftp_t *tp, int pktlen) | |
c7f74643 FB |
412 | { |
413 | int s; | |
414 | ||
fad7fb9c | 415 | s = tftp_session_find(slirp, srcsas, tp); |
c7f74643 FB |
416 | |
417 | if (s < 0) { | |
418 | return; | |
419 | } | |
420 | ||
eb7faf0e | 421 | tftp_send_next_block(&slirp->tftp_sessions[s], tp); |
c7f74643 FB |
422 | } |
423 | ||
fad7fb9c TH |
424 | static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas, |
425 | struct tftp_t *tp, int pktlen) | |
bfe4e172 TH |
426 | { |
427 | int s; | |
428 | ||
fad7fb9c | 429 | s = tftp_session_find(slirp, srcsas, tp); |
bfe4e172 TH |
430 | |
431 | if (s < 0) { | |
432 | return; | |
433 | } | |
434 | ||
435 | tftp_session_terminate(&slirp->tftp_sessions[s]); | |
436 | } | |
437 | ||
fad7fb9c | 438 | void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m) |
c7f74643 FB |
439 | { |
440 | struct tftp_t *tp = (struct tftp_t *)m->m_data; | |
441 | ||
442 | switch(ntohs(tp->tp_op)) { | |
443 | case TFTP_RRQ: | |
fad7fb9c | 444 | tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len); |
c7f74643 FB |
445 | break; |
446 | ||
447 | case TFTP_ACK: | |
fad7fb9c | 448 | tftp_handle_ack(m->slirp, srcsas, tp, m->m_len); |
c7f74643 | 449 | break; |
bfe4e172 TH |
450 | |
451 | case TFTP_ERROR: | |
fad7fb9c | 452 | tftp_handle_error(m->slirp, srcsas, tp, m->m_len); |
bfe4e172 | 453 | break; |
c7f74643 FB |
454 | } |
455 | } |