qapi: Fix command with named empty argument type
[qemu/ar7.git] / slirp / tftp.c
blob25ad6efdf82779c8456a159ee2a865ec5ebf6eba
1 /*
2 * tftp.c - a simple, read-only tftp server for qemu
4 * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include <slirp.h>
27 #include "qemu-common.h"
29 static inline int tftp_session_in_use(struct tftp_session *spt)
31 return (spt->slirp != NULL);
34 static inline void tftp_session_update(struct tftp_session *spt)
36 spt->timestamp = curtime;
39 static void tftp_session_terminate(struct tftp_session *spt)
41 if (spt->fd >= 0) {
42 close(spt->fd);
43 spt->fd = -1;
45 g_free(spt->filename);
46 spt->slirp = NULL;
49 static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
50 struct tftp_t *tp)
52 struct tftp_session *spt;
53 int k;
55 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
56 spt = &slirp->tftp_sessions[k];
58 if (!tftp_session_in_use(spt))
59 goto found;
61 /* sessions time out after 5 inactive seconds */
62 if ((int)(curtime - spt->timestamp) > 5000) {
63 tftp_session_terminate(spt);
64 goto found;
68 return -1;
70 found:
71 memset(spt, 0, sizeof(*spt));
72 spt->client_addr = *srcsas;
73 spt->fd = -1;
74 spt->client_port = tp->udp.uh_sport;
75 spt->slirp = slirp;
77 tftp_session_update(spt);
79 return k;
82 static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
83 struct tftp_t *tp)
85 struct tftp_session *spt;
86 int k;
88 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
89 spt = &slirp->tftp_sessions[k];
91 if (tftp_session_in_use(spt)) {
92 if (sockaddr_equal(&spt->client_addr, srcsas)) {
93 if (spt->client_port == tp->udp.uh_sport) {
94 return k;
100 return -1;
103 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
104 uint8_t *buf, int len)
106 int bytes_read = 0;
108 if (spt->fd < 0) {
109 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
112 if (spt->fd < 0) {
113 return -1;
116 if (len) {
117 lseek(spt->fd, block_nr * 512, SEEK_SET);
119 bytes_read = read(spt->fd, buf, len);
122 return bytes_read;
125 static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
126 struct mbuf *m)
128 struct tftp_t *tp;
130 memset(m->m_data, 0, m->m_size);
132 m->m_data += IF_MAXLINKHDR;
133 if (spt->client_addr.ss_family == AF_INET6) {
134 m->m_data += sizeof(struct ip6);
135 } else {
136 m->m_data += sizeof(struct ip);
138 tp = (void *)m->m_data;
139 m->m_data += sizeof(struct udphdr);
141 return tp;
144 static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
145 struct tftp_t *recv_tp)
147 if (spt->client_addr.ss_family == AF_INET6) {
148 struct sockaddr_in6 sa6, da6;
150 sa6.sin6_addr = spt->slirp->vhost_addr6;
151 sa6.sin6_port = recv_tp->udp.uh_dport;
152 da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
153 da6.sin6_port = spt->client_port;
155 udp6_output(NULL, m, &sa6, &da6);
156 } else {
157 struct sockaddr_in sa4, da4;
159 sa4.sin_addr = spt->slirp->vhost_addr;
160 sa4.sin_port = recv_tp->udp.uh_dport;
161 da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
162 da4.sin_port = spt->client_port;
164 udp_output(NULL, m, &sa4, &da4, IPTOS_LOWDELAY);
168 static int tftp_send_oack(struct tftp_session *spt,
169 const char *keys[], uint32_t values[], int nb,
170 struct tftp_t *recv_tp)
172 struct mbuf *m;
173 struct tftp_t *tp;
174 int i, n = 0;
176 m = m_get(spt->slirp);
178 if (!m)
179 return -1;
181 tp = tftp_prep_mbuf_data(spt, m);
183 tp->tp_op = htons(TFTP_OACK);
184 for (i = 0; i < nb; i++) {
185 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
186 keys[i]) + 1;
187 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
188 values[i]) + 1;
191 m->m_len = sizeof(struct tftp_t) - 514 + n - sizeof(struct udphdr);
192 tftp_udp_output(spt, m, recv_tp);
194 return 0;
197 static void tftp_send_error(struct tftp_session *spt,
198 uint16_t errorcode, const char *msg,
199 struct tftp_t *recv_tp)
201 struct mbuf *m;
202 struct tftp_t *tp;
204 m = m_get(spt->slirp);
206 if (!m) {
207 goto out;
210 memset(m->m_data, 0, m->m_size);
212 tp = tftp_prep_mbuf_data(spt, m);
214 tp->tp_op = htons(TFTP_ERROR);
215 tp->x.tp_error.tp_error_code = htons(errorcode);
216 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
218 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg)
219 - sizeof(struct udphdr);
220 tftp_udp_output(spt, m, recv_tp);
222 out:
223 tftp_session_terminate(spt);
226 static void tftp_send_next_block(struct tftp_session *spt,
227 struct tftp_t *recv_tp)
229 struct mbuf *m;
230 struct tftp_t *tp;
231 int nobytes;
233 m = m_get(spt->slirp);
235 if (!m) {
236 return;
239 memset(m->m_data, 0, m->m_size);
241 tp = tftp_prep_mbuf_data(spt, m);
243 tp->tp_op = htons(TFTP_DATA);
244 tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
246 nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
248 if (nobytes < 0) {
249 m_free(m);
251 /* send "file not found" error back */
253 tftp_send_error(spt, 1, "File not found", tp);
255 return;
258 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - sizeof(struct udphdr);
259 tftp_udp_output(spt, m, recv_tp);
261 if (nobytes == 512) {
262 tftp_session_update(spt);
264 else {
265 tftp_session_terminate(spt);
268 spt->block_nr++;
271 static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
272 struct tftp_t *tp, int pktlen)
274 struct tftp_session *spt;
275 int s, k;
276 size_t prefix_len;
277 char *req_fname;
278 const char *option_name[2];
279 uint32_t option_value[2];
280 int nb_options = 0;
282 /* check if a session already exists and if so terminate it */
283 s = tftp_session_find(slirp, srcsas, tp);
284 if (s >= 0) {
285 tftp_session_terminate(&slirp->tftp_sessions[s]);
288 s = tftp_session_allocate(slirp, srcsas, tp);
290 if (s < 0) {
291 return;
294 spt = &slirp->tftp_sessions[s];
296 /* unspecified prefix means service disabled */
297 if (!slirp->tftp_prefix) {
298 tftp_send_error(spt, 2, "Access violation", tp);
299 return;
302 /* skip header fields */
303 k = 0;
304 pktlen -= offsetof(struct tftp_t, x.tp_buf);
306 /* prepend tftp_prefix */
307 prefix_len = strlen(slirp->tftp_prefix);
308 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
309 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
310 spt->filename[prefix_len] = '/';
312 /* get name */
313 req_fname = spt->filename + prefix_len + 1;
315 while (1) {
316 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
317 tftp_send_error(spt, 2, "Access violation", tp);
318 return;
320 req_fname[k] = tp->x.tp_buf[k];
321 if (req_fname[k++] == '\0') {
322 break;
326 /* check mode */
327 if ((pktlen - k) < 6) {
328 tftp_send_error(spt, 2, "Access violation", tp);
329 return;
332 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
333 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
334 return;
337 k += 6; /* skipping octet */
339 /* do sanity checks on the filename */
340 if (!strncmp(req_fname, "../", 3) ||
341 req_fname[strlen(req_fname) - 1] == '/' ||
342 strstr(req_fname, "/../")) {
343 tftp_send_error(spt, 2, "Access violation", tp);
344 return;
347 /* check if the file exists */
348 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
349 tftp_send_error(spt, 1, "File not found", tp);
350 return;
353 if (tp->x.tp_buf[pktlen - 1] != 0) {
354 tftp_send_error(spt, 2, "Access violation", tp);
355 return;
358 while (k < pktlen && nb_options < ARRAY_SIZE(option_name)) {
359 const char *key, *value;
361 key = &tp->x.tp_buf[k];
362 k += strlen(key) + 1;
364 if (k >= pktlen) {
365 tftp_send_error(spt, 2, "Access violation", tp);
366 return;
369 value = &tp->x.tp_buf[k];
370 k += strlen(value) + 1;
372 if (strcasecmp(key, "tsize") == 0) {
373 int tsize = atoi(value);
374 struct stat stat_p;
376 if (tsize == 0) {
377 if (stat(spt->filename, &stat_p) == 0)
378 tsize = stat_p.st_size;
379 else {
380 tftp_send_error(spt, 1, "File not found", tp);
381 return;
385 option_name[nb_options] = "tsize";
386 option_value[nb_options] = tsize;
387 nb_options++;
388 } else if (strcasecmp(key, "blksize") == 0) {
389 int blksize = atoi(value);
391 /* If blksize option is bigger than what we will
392 * emit, accept the option with our packet size.
393 * Otherwise, simply do as we didn't see the option.
395 if (blksize >= 512) {
396 option_name[nb_options] = "blksize";
397 option_value[nb_options] = 512;
398 nb_options++;
403 if (nb_options > 0) {
404 assert(nb_options <= ARRAY_SIZE(option_name));
405 tftp_send_oack(spt, option_name, option_value, nb_options, tp);
406 return;
409 spt->block_nr = 0;
410 tftp_send_next_block(spt, tp);
413 static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
414 struct tftp_t *tp, int pktlen)
416 int s;
418 s = tftp_session_find(slirp, srcsas, tp);
420 if (s < 0) {
421 return;
424 tftp_send_next_block(&slirp->tftp_sessions[s], tp);
427 static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
428 struct tftp_t *tp, int pktlen)
430 int s;
432 s = tftp_session_find(slirp, srcsas, tp);
434 if (s < 0) {
435 return;
438 tftp_session_terminate(&slirp->tftp_sessions[s]);
441 void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
443 struct tftp_t *tp = (struct tftp_t *)m->m_data;
445 switch(ntohs(tp->tp_op)) {
446 case TFTP_RRQ:
447 tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
448 break;
450 case TFTP_ACK:
451 tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
452 break;
454 case TFTP_ERROR:
455 tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
456 break;