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"
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 memcpy(&spt
->client_addr
, srcsas
, sockaddr_size(srcsas
));
75 spt
->block_size
= 512;
76 spt
->client_port
= tp
->udp
.uh_sport
;
79 tftp_session_update(spt
);
84 static int tftp_session_find(Slirp
*slirp
, struct sockaddr_storage
*srcsas
,
87 struct tftp_session
*spt
;
90 for (k
= 0; k
< TFTP_SESSIONS_MAX
; k
++) {
91 spt
= &slirp
->tftp_sessions
[k
];
93 if (tftp_session_in_use(spt
)) {
94 if (sockaddr_equal(&spt
->client_addr
, srcsas
)) {
95 if (spt
->client_port
== tp
->udp
.uh_sport
) {
105 static int tftp_read_data(struct tftp_session
*spt
, uint32_t block_nr
,
106 uint8_t *buf
, int len
)
111 spt
->fd
= open(spt
->filename
, O_RDONLY
| O_BINARY
);
119 lseek(spt
->fd
, block_nr
* spt
->block_size
, SEEK_SET
);
121 bytes_read
= read(spt
->fd
, buf
, len
);
127 static struct tftp_t
*tftp_prep_mbuf_data(struct tftp_session
*spt
,
132 memset(m
->m_data
, 0, m
->m_size
);
134 m
->m_data
+= IF_MAXLINKHDR
;
135 if (spt
->client_addr
.ss_family
== AF_INET6
) {
136 m
->m_data
+= sizeof(struct ip6
);
138 m
->m_data
+= sizeof(struct ip
);
140 tp
= (void *)m
->m_data
;
141 m
->m_data
+= sizeof(struct udphdr
);
146 static void tftp_udp_output(struct tftp_session
*spt
, struct mbuf
*m
,
147 struct tftp_t
*recv_tp
)
149 if (spt
->client_addr
.ss_family
== AF_INET6
) {
150 struct sockaddr_in6 sa6
, da6
;
152 sa6
.sin6_addr
= spt
->slirp
->vhost_addr6
;
153 sa6
.sin6_port
= recv_tp
->udp
.uh_dport
;
154 da6
.sin6_addr
= ((struct sockaddr_in6
*)&spt
->client_addr
)->sin6_addr
;
155 da6
.sin6_port
= spt
->client_port
;
157 udp6_output(NULL
, m
, &sa6
, &da6
);
159 struct sockaddr_in sa4
, da4
;
161 sa4
.sin_addr
= spt
->slirp
->vhost_addr
;
162 sa4
.sin_port
= recv_tp
->udp
.uh_dport
;
163 da4
.sin_addr
= ((struct sockaddr_in
*)&spt
->client_addr
)->sin_addr
;
164 da4
.sin_port
= spt
->client_port
;
166 udp_output(NULL
, m
, &sa4
, &da4
, IPTOS_LOWDELAY
);
170 static int tftp_send_oack(struct tftp_session
*spt
,
171 const char *keys
[], uint32_t values
[], int nb
,
172 struct tftp_t
*recv_tp
)
178 m
= m_get(spt
->slirp
);
183 tp
= tftp_prep_mbuf_data(spt
, m
);
185 tp
->tp_op
= htons(TFTP_OACK
);
186 for (i
= 0; i
< nb
; i
++) {
187 n
+= snprintf(tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%s",
189 n
+= snprintf(tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%u",
193 m
->m_len
= sizeof(struct tftp_t
) - (TFTP_BLOCKSIZE_MAX
+ 2) + n
194 - sizeof(struct udphdr
);
195 tftp_udp_output(spt
, m
, recv_tp
);
200 static void tftp_send_error(struct tftp_session
*spt
,
201 uint16_t errorcode
, const char *msg
,
202 struct tftp_t
*recv_tp
)
207 m
= m_get(spt
->slirp
);
213 tp
= tftp_prep_mbuf_data(spt
, m
);
215 tp
->tp_op
= htons(TFTP_ERROR
);
216 tp
->x
.tp_error
.tp_error_code
= htons(errorcode
);
217 pstrcpy((char *)tp
->x
.tp_error
.tp_msg
, sizeof(tp
->x
.tp_error
.tp_msg
), msg
);
219 m
->m_len
= sizeof(struct tftp_t
) - (TFTP_BLOCKSIZE_MAX
+ 2) + 3 + strlen(msg
)
220 - sizeof(struct udphdr
);
221 tftp_udp_output(spt
, m
, recv_tp
);
224 tftp_session_terminate(spt
);
227 static void tftp_send_next_block(struct tftp_session
*spt
,
228 struct tftp_t
*recv_tp
)
234 m
= m_get(spt
->slirp
);
240 tp
= tftp_prep_mbuf_data(spt
, m
);
242 tp
->tp_op
= htons(TFTP_DATA
);
243 tp
->x
.tp_data
.tp_block_nr
= htons((spt
->block_nr
+ 1) & 0xffff);
245 nobytes
= tftp_read_data(spt
, spt
->block_nr
, tp
->x
.tp_data
.tp_buf
,
251 /* send "file not found" error back */
253 tftp_send_error(spt
, 1, "File not found", tp
);
258 m
->m_len
= sizeof(struct tftp_t
) - (TFTP_BLOCKSIZE_MAX
- nobytes
)
259 - sizeof(struct udphdr
);
260 tftp_udp_output(spt
, m
, recv_tp
);
262 if (nobytes
== spt
->block_size
) {
263 tftp_session_update(spt
);
266 tftp_session_terminate(spt
);
272 static void tftp_handle_rrq(Slirp
*slirp
, struct sockaddr_storage
*srcsas
,
273 struct tftp_t
*tp
, int pktlen
)
275 struct tftp_session
*spt
;
279 const char *option_name
[2];
280 uint32_t option_value
[2];
283 /* check if a session already exists and if so terminate it */
284 s
= tftp_session_find(slirp
, srcsas
, tp
);
286 tftp_session_terminate(&slirp
->tftp_sessions
[s
]);
289 s
= tftp_session_allocate(slirp
, srcsas
, tp
);
295 spt
= &slirp
->tftp_sessions
[s
];
297 /* unspecified prefix means service disabled */
298 if (!slirp
->tftp_prefix
) {
299 tftp_send_error(spt
, 2, "Access violation", tp
);
303 /* skip header fields */
305 pktlen
-= offsetof(struct tftp_t
, x
.tp_buf
);
307 /* prepend tftp_prefix */
308 prefix_len
= strlen(slirp
->tftp_prefix
);
309 spt
->filename
= g_malloc(prefix_len
+ TFTP_FILENAME_MAX
+ 2);
310 memcpy(spt
->filename
, slirp
->tftp_prefix
, prefix_len
);
311 spt
->filename
[prefix_len
] = '/';
314 req_fname
= spt
->filename
+ prefix_len
+ 1;
317 if (k
>= TFTP_FILENAME_MAX
|| k
>= pktlen
) {
318 tftp_send_error(spt
, 2, "Access violation", tp
);
321 req_fname
[k
] = tp
->x
.tp_buf
[k
];
322 if (req_fname
[k
++] == '\0') {
328 if ((pktlen
- k
) < 6) {
329 tftp_send_error(spt
, 2, "Access violation", tp
);
333 if (strcasecmp(&tp
->x
.tp_buf
[k
], "octet") != 0) {
334 tftp_send_error(spt
, 4, "Unsupported transfer mode", tp
);
338 k
+= 6; /* skipping octet */
340 /* do sanity checks on the filename */
341 if (!strncmp(req_fname
, "../", 3) ||
342 req_fname
[strlen(req_fname
) - 1] == '/' ||
343 strstr(req_fname
, "/../")) {
344 tftp_send_error(spt
, 2, "Access violation", tp
);
348 /* check if the file exists */
349 if (tftp_read_data(spt
, 0, NULL
, 0) < 0) {
350 tftp_send_error(spt
, 1, "File not found", tp
);
354 if (tp
->x
.tp_buf
[pktlen
- 1] != 0) {
355 tftp_send_error(spt
, 2, "Access violation", tp
);
359 while (k
< pktlen
&& nb_options
< ARRAY_SIZE(option_name
)) {
360 const char *key
, *value
;
362 key
= &tp
->x
.tp_buf
[k
];
363 k
+= strlen(key
) + 1;
366 tftp_send_error(spt
, 2, "Access violation", tp
);
370 value
= &tp
->x
.tp_buf
[k
];
371 k
+= strlen(value
) + 1;
373 if (strcasecmp(key
, "tsize") == 0) {
374 int tsize
= atoi(value
);
378 if (stat(spt
->filename
, &stat_p
) == 0)
379 tsize
= stat_p
.st_size
;
381 tftp_send_error(spt
, 1, "File not found", tp
);
386 option_name
[nb_options
] = "tsize";
387 option_value
[nb_options
] = tsize
;
389 } else if (strcasecmp(key
, "blksize") == 0) {
390 int blksize
= atoi(value
);
392 /* Accept blksize up to our maximum size */
394 spt
->block_size
= MIN(blksize
, TFTP_BLOCKSIZE_MAX
);
395 option_name
[nb_options
] = "blksize";
396 option_value
[nb_options
] = spt
->block_size
;
402 if (nb_options
> 0) {
403 assert(nb_options
<= ARRAY_SIZE(option_name
));
404 tftp_send_oack(spt
, option_name
, option_value
, nb_options
, tp
);
409 tftp_send_next_block(spt
, tp
);
412 static void tftp_handle_ack(Slirp
*slirp
, struct sockaddr_storage
*srcsas
,
413 struct tftp_t
*tp
, int pktlen
)
417 s
= tftp_session_find(slirp
, srcsas
, tp
);
423 tftp_send_next_block(&slirp
->tftp_sessions
[s
], tp
);
426 static void tftp_handle_error(Slirp
*slirp
, struct sockaddr_storage
*srcsas
,
427 struct tftp_t
*tp
, int pktlen
)
431 s
= tftp_session_find(slirp
, srcsas
, tp
);
437 tftp_session_terminate(&slirp
->tftp_sessions
[s
]);
440 void tftp_input(struct sockaddr_storage
*srcsas
, struct mbuf
*m
)
442 struct tftp_t
*tp
= (struct tftp_t
*)m
->m_data
;
444 switch(ntohs(tp
->tp_op
)) {
446 tftp_handle_rrq(m
->slirp
, srcsas
, tp
, m
->m_len
);
450 tftp_handle_ack(m
->slirp
, srcsas
, tp
, m
->m_len
);
454 tftp_handle_error(m
->slirp
, srcsas
, tp
, m
->m_len
);