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
25 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "qemu/cutils.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
)
47 g_free(spt
->filename
);
51 static int tftp_session_allocate(Slirp
*slirp
, struct sockaddr_storage
*srcsas
,
54 struct tftp_session
*spt
;
57 for (k
= 0; k
< TFTP_SESSIONS_MAX
; k
++) {
58 spt
= &slirp
->tftp_sessions
[k
];
60 if (!tftp_session_in_use(spt
))
63 /* sessions time out after 5 inactive seconds */
64 if ((int)(curtime
- spt
->timestamp
) > 5000) {
65 tftp_session_terminate(spt
);
73 memset(spt
, 0, sizeof(*spt
));
74 memcpy(&spt
->client_addr
, srcsas
, sockaddr_size(srcsas
));
76 spt
->block_size
= 512;
77 spt
->client_port
= tp
->udp
.uh_sport
;
80 tftp_session_update(spt
);
85 static int tftp_session_find(Slirp
*slirp
, struct sockaddr_storage
*srcsas
,
88 struct tftp_session
*spt
;
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
) {
106 static int tftp_read_data(struct tftp_session
*spt
, uint32_t block_nr
,
107 uint8_t *buf
, int len
)
112 spt
->fd
= open(spt
->filename
, O_RDONLY
| O_BINARY
);
120 lseek(spt
->fd
, block_nr
* spt
->block_size
, SEEK_SET
);
122 bytes_read
= read(spt
->fd
, buf
, len
);
128 static struct tftp_t
*tftp_prep_mbuf_data(struct tftp_session
*spt
,
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
);
139 m
->m_data
+= sizeof(struct ip
);
141 tp
= (void *)m
->m_data
;
142 m
->m_data
+= sizeof(struct udphdr
);
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
);
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
)
179 m
= m_get(spt
->slirp
);
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",
190 n
+= snprintf(tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%u",
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
);
201 static void tftp_send_error(struct tftp_session
*spt
,
202 uint16_t errorcode
, const char *msg
,
203 struct tftp_t
*recv_tp
)
208 trace_slirp_tftp_error(msg
);
209 m
= m_get(spt
->slirp
);
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
);
226 tftp_session_terminate(spt
);
229 static void tftp_send_next_block(struct tftp_session
*spt
,
230 struct tftp_t
*recv_tp
)
236 m
= m_get(spt
->slirp
);
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
,
253 /* send "file not found" error back */
255 tftp_send_error(spt
, 1, "File not found", tp
);
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
);
268 tftp_session_terminate(spt
);
274 static void tftp_handle_rrq(Slirp
*slirp
, struct sockaddr_storage
*srcsas
,
275 struct tftp_t
*tp
, int pktlen
)
277 struct tftp_session
*spt
;
281 const char *option_name
[2];
282 uint32_t option_value
[2];
285 /* check if a session already exists and if so terminate it */
286 s
= tftp_session_find(slirp
, srcsas
, tp
);
288 tftp_session_terminate(&slirp
->tftp_sessions
[s
]);
291 s
= tftp_session_allocate(slirp
, srcsas
, tp
);
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
);
305 /* skip header fields */
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
] = '/';
316 req_fname
= spt
->filename
+ prefix_len
+ 1;
319 if (k
>= TFTP_FILENAME_MAX
|| k
>= pktlen
) {
320 tftp_send_error(spt
, 2, "Access violation", tp
);
323 req_fname
[k
] = tp
->x
.tp_buf
[k
];
324 if (req_fname
[k
++] == '\0') {
328 trace_slirp_tftp_rrq(req_fname
);
331 if ((pktlen
- k
) < 6) {
332 tftp_send_error(spt
, 2, "Access violation", tp
);
336 if (strcasecmp(&tp
->x
.tp_buf
[k
], "octet") != 0) {
337 tftp_send_error(spt
, 4, "Unsupported transfer mode", tp
);
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
);
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
);
357 if (tp
->x
.tp_buf
[pktlen
- 1] != 0) {
358 tftp_send_error(spt
, 2, "Access violation", tp
);
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;
369 tftp_send_error(spt
, 2, "Access violation", tp
);
373 value
= &tp
->x
.tp_buf
[k
];
374 k
+= strlen(value
) + 1;
376 if (strcasecmp(key
, "tsize") == 0) {
377 int tsize
= atoi(value
);
381 if (stat(spt
->filename
, &stat_p
) == 0)
382 tsize
= stat_p
.st_size
;
384 tftp_send_error(spt
, 1, "File not found", tp
);
389 option_name
[nb_options
] = "tsize";
390 option_value
[nb_options
] = tsize
;
392 } else if (strcasecmp(key
, "blksize") == 0) {
393 int blksize
= atoi(value
);
395 /* Accept blksize up to our maximum size */
397 spt
->block_size
= MIN(blksize
, TFTP_BLOCKSIZE_MAX
);
398 option_name
[nb_options
] = "blksize";
399 option_value
[nb_options
] = spt
->block_size
;
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
);
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
)
420 s
= tftp_session_find(slirp
, srcsas
, tp
);
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
)
434 s
= tftp_session_find(slirp
, srcsas
, tp
);
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
)) {
449 tftp_handle_rrq(m
->slirp
, srcsas
, tp
, m
->m_len
);
453 tftp_handle_ack(m
->slirp
, srcsas
, tp
, m
->m_len
);
457 tftp_handle_error(m
->slirp
, srcsas
, tp
, m
->m_len
);