]> git.proxmox.com Git - qemu.git/blob - slirp/tftp.c
slirp: Handle more than 65535 blocks in TFTP transfers
[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 <slirp.h>
26 #include "qemu-common.h"
27
28 static inline int tftp_session_in_use(struct tftp_session *spt)
29 {
30 return (spt->slirp != NULL);
31 }
32
33 static inline void tftp_session_update(struct tftp_session *spt)
34 {
35 spt->timestamp = curtime;
36 }
37
38 static void tftp_session_terminate(struct tftp_session *spt)
39 {
40 if (spt->fd >= 0) {
41 close(spt->fd);
42 spt->fd = -1;
43 }
44 g_free(spt->filename);
45 spt->slirp = NULL;
46 }
47
48 static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
49 {
50 struct tftp_session *spt;
51 int k;
52
53 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
54 spt = &slirp->tftp_sessions[k];
55
56 if (!tftp_session_in_use(spt))
57 goto found;
58
59 /* sessions time out after 5 inactive seconds */
60 if ((int)(curtime - spt->timestamp) > 5000) {
61 tftp_session_terminate(spt);
62 goto found;
63 }
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));
71 spt->fd = -1;
72 spt->client_port = tp->udp.uh_sport;
73 spt->slirp = slirp;
74
75 tftp_session_update(spt);
76
77 return k;
78 }
79
80 static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
81 {
82 struct tftp_session *spt;
83 int k;
84
85 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
86 spt = &slirp->tftp_sessions[k];
87
88 if (tftp_session_in_use(spt)) {
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
100 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
101 uint8_t *buf, int len)
102 {
103 int bytes_read = 0;
104
105 if (spt->fd < 0) {
106 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
107 }
108
109 if (spt->fd < 0) {
110 return -1;
111 }
112
113 if (len) {
114 lseek(spt->fd, block_nr * 512, SEEK_SET);
115
116 bytes_read = read(spt->fd, buf, len);
117 }
118
119 return bytes_read;
120 }
121
122 static int tftp_send_oack(struct tftp_session *spt,
123 const char *key, uint32_t value,
124 struct tftp_t *recv_tp)
125 {
126 struct sockaddr_in saddr, daddr;
127 struct mbuf *m;
128 struct tftp_t *tp;
129 int n = 0;
130
131 m = m_get(spt->slirp);
132
133 if (!m)
134 return -1;
135
136 memset(m->m_data, 0, m->m_size);
137
138 m->m_data += IF_MAXLINKHDR;
139 tp = (void *)m->m_data;
140 m->m_data += sizeof(struct udpiphdr);
141
142 tp->tp_op = htons(TFTP_OACK);
143 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
144 key) + 1;
145 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
146 value) + 1;
147
148 saddr.sin_addr = recv_tp->ip.ip_dst;
149 saddr.sin_port = recv_tp->udp.uh_dport;
150
151 daddr.sin_addr = spt->client_ip;
152 daddr.sin_port = spt->client_port;
153
154 m->m_len = sizeof(struct tftp_t) - 514 + n -
155 sizeof(struct ip) - sizeof(struct udphdr);
156 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
157
158 return 0;
159 }
160
161 static void tftp_send_error(struct tftp_session *spt,
162 uint16_t errorcode, const char *msg,
163 struct tftp_t *recv_tp)
164 {
165 struct sockaddr_in saddr, daddr;
166 struct mbuf *m;
167 struct tftp_t *tp;
168
169 m = m_get(spt->slirp);
170
171 if (!m) {
172 goto out;
173 }
174
175 memset(m->m_data, 0, m->m_size);
176
177 m->m_data += IF_MAXLINKHDR;
178 tp = (void *)m->m_data;
179 m->m_data += sizeof(struct udpiphdr);
180
181 tp->tp_op = htons(TFTP_ERROR);
182 tp->x.tp_error.tp_error_code = htons(errorcode);
183 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
184
185 saddr.sin_addr = recv_tp->ip.ip_dst;
186 saddr.sin_port = recv_tp->udp.uh_dport;
187
188 daddr.sin_addr = spt->client_ip;
189 daddr.sin_port = spt->client_port;
190
191 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
192 sizeof(struct ip) - sizeof(struct udphdr);
193
194 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
195
196 out:
197 tftp_session_terminate(spt);
198 }
199
200 static int tftp_send_next_block(struct tftp_session *spt,
201 struct tftp_t *recv_tp)
202 {
203 struct sockaddr_in saddr, daddr;
204 struct mbuf *m;
205 struct tftp_t *tp;
206 int nobytes;
207
208 m = m_get(spt->slirp);
209
210 if (!m) {
211 return -1;
212 }
213
214 memset(m->m_data, 0, m->m_size);
215
216 m->m_data += IF_MAXLINKHDR;
217 tp = (void *)m->m_data;
218 m->m_data += sizeof(struct udpiphdr);
219
220 tp->tp_op = htons(TFTP_DATA);
221 tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
222
223 saddr.sin_addr = recv_tp->ip.ip_dst;
224 saddr.sin_port = recv_tp->udp.uh_dport;
225
226 daddr.sin_addr = spt->client_ip;
227 daddr.sin_port = spt->client_port;
228
229 nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
230
231 if (nobytes < 0) {
232 m_free(m);
233
234 /* send "file not found" error back */
235
236 tftp_send_error(spt, 1, "File not found", tp);
237
238 return -1;
239 }
240
241 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
242 sizeof(struct ip) - sizeof(struct udphdr);
243
244 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
245
246 if (nobytes == 512) {
247 tftp_session_update(spt);
248 }
249 else {
250 tftp_session_terminate(spt);
251 }
252
253 spt->block_nr++;
254 return 0;
255 }
256
257 static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
258 {
259 struct tftp_session *spt;
260 int s, k;
261 size_t prefix_len;
262 char *req_fname;
263
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
270 s = tftp_session_allocate(slirp, tp);
271
272 if (s < 0) {
273 return;
274 }
275
276 spt = &slirp->tftp_sessions[s];
277
278 /* unspecifed prefix means service disabled */
279 if (!slirp->tftp_prefix) {
280 tftp_send_error(spt, 2, "Access violation", tp);
281 return;
282 }
283
284 /* skip header fields */
285 k = 0;
286 pktlen -= offsetof(struct tftp_t, x.tp_buf);
287
288 /* prepend tftp_prefix */
289 prefix_len = strlen(slirp->tftp_prefix);
290 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
291 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
292 spt->filename[prefix_len] = '/';
293
294 /* get name */
295 req_fname = spt->filename + prefix_len + 1;
296
297 while (1) {
298 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
299 tftp_send_error(spt, 2, "Access violation", tp);
300 return;
301 }
302 req_fname[k] = tp->x.tp_buf[k];
303 if (req_fname[k++] == '\0') {
304 break;
305 }
306 }
307
308 /* check mode */
309 if ((pktlen - k) < 6) {
310 tftp_send_error(spt, 2, "Access violation", tp);
311 return;
312 }
313
314 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
315 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
316 return;
317 }
318
319 k += 6; /* skipping octet */
320
321 /* do sanity checks on the filename */
322 if (!strncmp(req_fname, "../", 3) ||
323 req_fname[strlen(req_fname) - 1] == '/' ||
324 strstr(req_fname, "/../")) {
325 tftp_send_error(spt, 2, "Access violation", tp);
326 return;
327 }
328
329 /* check if the file exists */
330 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
331 tftp_send_error(spt, 1, "File not found", tp);
332 return;
333 }
334
335 if (tp->x.tp_buf[pktlen - 1] != 0) {
336 tftp_send_error(spt, 2, "Access violation", tp);
337 return;
338 }
339
340 while (k < pktlen) {
341 const char *key, *value;
342
343 key = &tp->x.tp_buf[k];
344 k += strlen(key) + 1;
345
346 if (k >= pktlen) {
347 tftp_send_error(spt, 2, "Access violation", tp);
348 return;
349 }
350
351 value = &tp->x.tp_buf[k];
352 k += strlen(value) + 1;
353
354 if (strcasecmp(key, "tsize") == 0) {
355 int tsize = atoi(value);
356 struct stat stat_p;
357
358 if (tsize == 0) {
359 if (stat(spt->filename, &stat_p) == 0)
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);
368 return;
369 }
370 }
371
372 spt->block_nr = 0;
373 tftp_send_next_block(spt, tp);
374 }
375
376 static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
377 {
378 int s;
379
380 s = tftp_session_find(slirp, tp);
381
382 if (s < 0) {
383 return;
384 }
385
386 if (tftp_send_next_block(&slirp->tftp_sessions[s],
387 tp) < 0) {
388 return;
389 }
390 }
391
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
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:
411 tftp_handle_rrq(m->slirp, tp, m->m_len);
412 break;
413
414 case TFTP_ACK:
415 tftp_handle_ack(m->slirp, tp, m->m_len);
416 break;
417
418 case TFTP_ERROR:
419 tftp_handle_error(m->slirp, tp, m->m_len);
420 break;
421 }
422 }