tcg/riscv: enable dynamic TLB sizing
[qemu/ar7.git] / slirp / tftp.c
bloba9ba1480db4e58c23cc2c33ceb5386b612989b1b
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"
28 #include "qemu/cutils.h"
29 #include "trace.h"
31 static inline int tftp_session_in_use(struct tftp_session *spt)
33 return (spt->slirp != NULL);
36 static inline void tftp_session_update(struct tftp_session *spt)
38 spt->timestamp = curtime;
41 static void tftp_session_terminate(struct tftp_session *spt)
43 if (spt->fd >= 0) {
44 close(spt->fd);
45 spt->fd = -1;
47 g_free(spt->filename);
48 spt->slirp = NULL;
51 static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
52 struct tftp_t *tp)
54 struct tftp_session *spt;
55 int k;
57 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
58 spt = &slirp->tftp_sessions[k];
60 if (!tftp_session_in_use(spt))
61 goto found;
63 /* sessions time out after 5 inactive seconds */
64 if ((int)(curtime - spt->timestamp) > 5000) {
65 tftp_session_terminate(spt);
66 goto found;
70 return -1;
72 found:
73 memset(spt, 0, sizeof(*spt));
74 memcpy(&spt->client_addr, srcsas, sockaddr_size(srcsas));
75 spt->fd = -1;
76 spt->block_size = 512;
77 spt->client_port = tp->udp.uh_sport;
78 spt->slirp = slirp;
80 tftp_session_update(spt);
82 return k;
85 static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
86 struct tftp_t *tp)
88 struct tftp_session *spt;
89 int k;
91 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
92 spt = &slirp->tftp_sessions[k];
94 if (tftp_session_in_use(spt)) {
95 if (sockaddr_equal(&spt->client_addr, srcsas)) {
96 if (spt->client_port == tp->udp.uh_sport) {
97 return k;
103 return -1;
106 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
107 uint8_t *buf, int len)
109 int bytes_read = 0;
111 if (spt->fd < 0) {
112 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
115 if (spt->fd < 0) {
116 return -1;
119 if (len) {
120 lseek(spt->fd, block_nr * spt->block_size, SEEK_SET);
122 bytes_read = read(spt->fd, buf, len);
125 return bytes_read;
128 static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
129 struct mbuf *m)
131 struct tftp_t *tp;
133 memset(m->m_data, 0, m->m_size);
135 m->m_data += IF_MAXLINKHDR;
136 if (spt->client_addr.ss_family == AF_INET6) {
137 m->m_data += sizeof(struct ip6);
138 } else {
139 m->m_data += sizeof(struct ip);
141 tp = (void *)m->m_data;
142 m->m_data += sizeof(struct udphdr);
144 return tp;
147 static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
148 struct tftp_t *recv_tp)
150 if (spt->client_addr.ss_family == AF_INET6) {
151 struct sockaddr_in6 sa6, da6;
153 sa6.sin6_addr = spt->slirp->vhost_addr6;
154 sa6.sin6_port = recv_tp->udp.uh_dport;
155 da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
156 da6.sin6_port = spt->client_port;
158 udp6_output(NULL, m, &sa6, &da6);
159 } else {
160 struct sockaddr_in sa4, da4;
162 sa4.sin_addr = spt->slirp->vhost_addr;
163 sa4.sin_port = recv_tp->udp.uh_dport;
164 da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
165 da4.sin_port = spt->client_port;
167 udp_output(NULL, m, &sa4, &da4, IPTOS_LOWDELAY);
171 static int tftp_send_oack(struct tftp_session *spt,
172 const char *keys[], uint32_t values[], int nb,
173 struct tftp_t *recv_tp)
175 struct mbuf *m;
176 struct tftp_t *tp;
177 int i, n = 0;
179 m = m_get(spt->slirp);
181 if (!m)
182 return -1;
184 tp = tftp_prep_mbuf_data(spt, m);
186 tp->tp_op = htons(TFTP_OACK);
187 for (i = 0; i < nb; i++) {
188 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
189 keys[i]) + 1;
190 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
191 values[i]) + 1;
194 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + n
195 - sizeof(struct udphdr);
196 tftp_udp_output(spt, m, recv_tp);
198 return 0;
201 static void tftp_send_error(struct tftp_session *spt,
202 uint16_t errorcode, const char *msg,
203 struct tftp_t *recv_tp)
205 struct mbuf *m;
206 struct tftp_t *tp;
208 trace_slirp_tftp_error(msg);
209 m = m_get(spt->slirp);
211 if (!m) {
212 goto out;
215 tp = tftp_prep_mbuf_data(spt, m);
217 tp->tp_op = htons(TFTP_ERROR);
218 tp->x.tp_error.tp_error_code = htons(errorcode);
219 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
221 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + 3 + strlen(msg)
222 - sizeof(struct udphdr);
223 tftp_udp_output(spt, m, recv_tp);
225 out:
226 tftp_session_terminate(spt);
229 static void tftp_send_next_block(struct tftp_session *spt,
230 struct tftp_t *recv_tp)
232 struct mbuf *m;
233 struct tftp_t *tp;
234 int nobytes;
236 m = m_get(spt->slirp);
238 if (!m) {
239 return;
242 tp = tftp_prep_mbuf_data(spt, m);
244 tp->tp_op = htons(TFTP_DATA);
245 tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
247 nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf,
248 spt->block_size);
250 if (nobytes < 0) {
251 m_free(m);
253 /* send "file not found" error back */
255 tftp_send_error(spt, 1, "File not found", tp);
257 return;
260 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX - nobytes)
261 - sizeof(struct udphdr);
262 tftp_udp_output(spt, m, recv_tp);
264 if (nobytes == spt->block_size) {
265 tftp_session_update(spt);
267 else {
268 tftp_session_terminate(spt);
271 spt->block_nr++;
274 static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
275 struct tftp_t *tp, int pktlen)
277 struct tftp_session *spt;
278 int s, k;
279 size_t prefix_len;
280 char *req_fname;
281 const char *option_name[2];
282 uint32_t option_value[2];
283 int nb_options = 0;
285 /* check if a session already exists and if so terminate it */
286 s = tftp_session_find(slirp, srcsas, tp);
287 if (s >= 0) {
288 tftp_session_terminate(&slirp->tftp_sessions[s]);
291 s = tftp_session_allocate(slirp, srcsas, tp);
293 if (s < 0) {
294 return;
297 spt = &slirp->tftp_sessions[s];
299 /* unspecified prefix means service disabled */
300 if (!slirp->tftp_prefix) {
301 tftp_send_error(spt, 2, "Access violation", tp);
302 return;
305 /* skip header fields */
306 k = 0;
307 pktlen -= offsetof(struct tftp_t, x.tp_buf);
309 /* prepend tftp_prefix */
310 prefix_len = strlen(slirp->tftp_prefix);
311 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
312 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
313 spt->filename[prefix_len] = '/';
315 /* get name */
316 req_fname = spt->filename + prefix_len + 1;
318 while (1) {
319 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
320 tftp_send_error(spt, 2, "Access violation", tp);
321 return;
323 req_fname[k] = tp->x.tp_buf[k];
324 if (req_fname[k++] == '\0') {
325 break;
328 trace_slirp_tftp_rrq(req_fname);
330 /* check mode */
331 if ((pktlen - k) < 6) {
332 tftp_send_error(spt, 2, "Access violation", tp);
333 return;
336 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
337 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
338 return;
341 k += 6; /* skipping octet */
343 /* do sanity checks on the filename */
344 if (!strncmp(req_fname, "../", 3) ||
345 req_fname[strlen(req_fname) - 1] == '/' ||
346 strstr(req_fname, "/../")) {
347 tftp_send_error(spt, 2, "Access violation", tp);
348 return;
351 /* check if the file exists */
352 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
353 tftp_send_error(spt, 1, "File not found", tp);
354 return;
357 if (tp->x.tp_buf[pktlen - 1] != 0) {
358 tftp_send_error(spt, 2, "Access violation", tp);
359 return;
362 while (k < pktlen && nb_options < G_N_ELEMENTS(option_name)) {
363 const char *key, *value;
365 key = &tp->x.tp_buf[k];
366 k += strlen(key) + 1;
368 if (k >= pktlen) {
369 tftp_send_error(spt, 2, "Access violation", tp);
370 return;
373 value = &tp->x.tp_buf[k];
374 k += strlen(value) + 1;
376 if (strcasecmp(key, "tsize") == 0) {
377 int tsize = atoi(value);
378 struct stat stat_p;
380 if (tsize == 0) {
381 if (stat(spt->filename, &stat_p) == 0)
382 tsize = stat_p.st_size;
383 else {
384 tftp_send_error(spt, 1, "File not found", tp);
385 return;
389 option_name[nb_options] = "tsize";
390 option_value[nb_options] = tsize;
391 nb_options++;
392 } else if (strcasecmp(key, "blksize") == 0) {
393 int blksize = atoi(value);
395 /* Accept blksize up to our maximum size */
396 if (blksize > 0) {
397 spt->block_size = MIN(blksize, TFTP_BLOCKSIZE_MAX);
398 option_name[nb_options] = "blksize";
399 option_value[nb_options] = spt->block_size;
400 nb_options++;
405 if (nb_options > 0) {
406 assert(nb_options <= G_N_ELEMENTS(option_name));
407 tftp_send_oack(spt, option_name, option_value, nb_options, tp);
408 return;
411 spt->block_nr = 0;
412 tftp_send_next_block(spt, tp);
415 static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
416 struct tftp_t *tp, int pktlen)
418 int s;
420 s = tftp_session_find(slirp, srcsas, tp);
422 if (s < 0) {
423 return;
426 tftp_send_next_block(&slirp->tftp_sessions[s], tp);
429 static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
430 struct tftp_t *tp, int pktlen)
432 int s;
434 s = tftp_session_find(slirp, srcsas, tp);
436 if (s < 0) {
437 return;
440 tftp_session_terminate(&slirp->tftp_sessions[s]);
443 void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
445 struct tftp_t *tp = (struct tftp_t *)m->m_data;
447 switch(ntohs(tp->tp_op)) {
448 case TFTP_RRQ:
449 tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
450 break;
452 case TFTP_ACK:
453 tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
454 break;
456 case TFTP_ERROR:
457 tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
458 break;