2 * tftp.c - a simple, read-only tftp server for qemu
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
25 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "qemu/cutils.h"
30 static inline int tftp_session_in_use(struct tftp_session *spt)
32 return (spt->slirp != NULL);
35 static inline void tftp_session_update(struct tftp_session *spt)
37 spt->timestamp = curtime;
40 static void tftp_session_terminate(struct tftp_session *spt)
46 g_free(spt->filename);
50 static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
53 struct tftp_session *spt;
56 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
57 spt = &slirp->tftp_sessions[k];
59 if (!tftp_session_in_use(spt))
62 /* sessions time out after 5 inactive seconds */
63 if ((int)(curtime - spt->timestamp) > 5000) {
64 tftp_session_terminate(spt);
72 memset(spt, 0, sizeof(*spt));
73 spt->client_addr = *srcsas;
75 spt->client_port = tp->udp.uh_sport;
78 tftp_session_update(spt);
83 static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
86 struct tftp_session *spt;
89 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
90 spt = &slirp->tftp_sessions[k];
92 if (tftp_session_in_use(spt)) {
93 if (sockaddr_equal(&spt->client_addr, srcsas)) {
94 if (spt->client_port == tp->udp.uh_sport) {
104 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
105 uint8_t *buf, int len)
110 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
118 lseek(spt->fd, block_nr * 512, SEEK_SET);
120 bytes_read = read(spt->fd, buf, len);
126 static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
131 memset(m->m_data, 0, m->m_size);
133 m->m_data += IF_MAXLINKHDR;
134 if (spt->client_addr.ss_family == AF_INET6) {
135 m->m_data += sizeof(struct ip6);
137 m->m_data += sizeof(struct ip);
139 tp = (void *)m->m_data;
140 m->m_data += sizeof(struct udphdr);
145 static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
146 struct tftp_t *recv_tp)
148 if (spt->client_addr.ss_family == AF_INET6) {
149 struct sockaddr_in6 sa6, da6;
151 sa6.sin6_addr = spt->slirp->vhost_addr6;
152 sa6.sin6_port = recv_tp->udp.uh_dport;
153 da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
154 da6.sin6_port = spt->client_port;
156 udp6_output(NULL, m, &sa6, &da6);
158 struct sockaddr_in sa4, da4;
160 sa4.sin_addr = spt->slirp->vhost_addr;
161 sa4.sin_port = recv_tp->udp.uh_dport;
162 da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
163 da4.sin_port = spt->client_port;
165 udp_output(NULL, m, &sa4, &da4, IPTOS_LOWDELAY);
169 static int tftp_send_oack(struct tftp_session *spt,
170 const char *keys[], uint32_t values[], int nb,
171 struct tftp_t *recv_tp)
177 m = m_get(spt->slirp);
182 tp = tftp_prep_mbuf_data(spt, m);
184 tp->tp_op = htons(TFTP_OACK);
185 for (i = 0; i < nb; i++) {
186 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
188 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
192 m->m_len = sizeof(struct tftp_t) - 514 + n - sizeof(struct udphdr);
193 tftp_udp_output(spt, m, recv_tp);
198 static void tftp_send_error(struct tftp_session *spt,
199 uint16_t errorcode, const char *msg,
200 struct tftp_t *recv_tp)
205 m = m_get(spt->slirp);
211 tp = tftp_prep_mbuf_data(spt, m);
213 tp->tp_op = htons(TFTP_ERROR);
214 tp->x.tp_error.tp_error_code = htons(errorcode);
215 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
217 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg)
218 - sizeof(struct udphdr);
219 tftp_udp_output(spt, m, recv_tp);
222 tftp_session_terminate(spt);
225 static void tftp_send_next_block(struct tftp_session *spt,
226 struct tftp_t *recv_tp)
232 m = m_get(spt->slirp);
238 tp = tftp_prep_mbuf_data(spt, m);
240 tp->tp_op = htons(TFTP_DATA);
241 tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
243 nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
248 /* send "file not found" error back */
250 tftp_send_error(spt, 1, "File not found", tp);
255 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - sizeof(struct udphdr);
256 tftp_udp_output(spt, m, recv_tp);
258 if (nobytes == 512) {
259 tftp_session_update(spt);
262 tftp_session_terminate(spt);
268 static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
269 struct tftp_t *tp, int pktlen)
271 struct tftp_session *spt;
275 const char *option_name[2];
276 uint32_t option_value[2];
279 /* check if a session already exists and if so terminate it */
280 s = tftp_session_find(slirp, srcsas, tp);
282 tftp_session_terminate(&slirp->tftp_sessions[s]);
285 s = tftp_session_allocate(slirp, srcsas, tp);
291 spt = &slirp->tftp_sessions[s];
293 /* unspecified prefix means service disabled */
294 if (!slirp->tftp_prefix) {
295 tftp_send_error(spt, 2, "Access violation", tp);
299 /* skip header fields */
301 pktlen -= offsetof(struct tftp_t, x.tp_buf);
303 /* prepend tftp_prefix */
304 prefix_len = strlen(slirp->tftp_prefix);
305 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
306 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
307 spt->filename[prefix_len] = '/';
310 req_fname = spt->filename + prefix_len + 1;
313 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
314 tftp_send_error(spt, 2, "Access violation", tp);
317 req_fname[k] = tp->x.tp_buf[k];
318 if (req_fname[k++] == '\0') {
324 if ((pktlen - k) < 6) {
325 tftp_send_error(spt, 2, "Access violation", tp);
329 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
330 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
334 k += 6; /* skipping octet */
336 /* do sanity checks on the filename */
337 if (!strncmp(req_fname, "../", 3) ||
338 req_fname[strlen(req_fname) - 1] == '/' ||
339 strstr(req_fname, "/../")) {
340 tftp_send_error(spt, 2, "Access violation", tp);
344 /* check if the file exists */
345 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
346 tftp_send_error(spt, 1, "File not found", tp);
350 if (tp->x.tp_buf[pktlen - 1] != 0) {
351 tftp_send_error(spt, 2, "Access violation", tp);
355 while (k < pktlen && nb_options < ARRAY_SIZE(option_name)) {
356 const char *key, *value;
358 key = &tp->x.tp_buf[k];
359 k += strlen(key) + 1;
362 tftp_send_error(spt, 2, "Access violation", tp);
366 value = &tp->x.tp_buf[k];
367 k += strlen(value) + 1;
369 if (strcasecmp(key, "tsize") == 0) {
370 int tsize = atoi(value);
374 if (stat(spt->filename, &stat_p) == 0)
375 tsize = stat_p.st_size;
377 tftp_send_error(spt, 1, "File not found", tp);
382 option_name[nb_options] = "tsize";
383 option_value[nb_options] = tsize;
385 } else if (strcasecmp(key, "blksize") == 0) {
386 int blksize = atoi(value);
388 /* If blksize option is bigger than what we will
389 * emit, accept the option with our packet size.
390 * Otherwise, simply do as we didn't see the option.
392 if (blksize >= 512) {
393 option_name[nb_options] = "blksize";
394 option_value[nb_options] = 512;
400 if (nb_options > 0) {
401 assert(nb_options <= ARRAY_SIZE(option_name));
402 tftp_send_oack(spt, option_name, option_value, nb_options, tp);
407 tftp_send_next_block(spt, tp);
410 static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
411 struct tftp_t *tp, int pktlen)
415 s = tftp_session_find(slirp, srcsas, tp);
421 tftp_send_next_block(&slirp->tftp_sessions[s], tp);
424 static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
425 struct tftp_t *tp, int pktlen)
429 s = tftp_session_find(slirp, srcsas, tp);
435 tftp_session_terminate(&slirp->tftp_sessions[s]);
438 void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
440 struct tftp_t *tp = (struct tftp_t *)m->m_data;
442 switch(ntohs(tp->tp_op)) {
444 tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
448 tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
452 tftp_handle_error(m->slirp, srcsas, tp, m->m_len);