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