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