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
26 #include "qemu-common.h"
28 static inline int tftp_session_in_use(struct tftp_session
*spt
)
30 return (spt
->slirp
!= NULL
);
33 static inline void tftp_session_update(struct tftp_session
*spt
)
35 spt
->timestamp
= curtime
;
38 static void tftp_session_terminate(struct tftp_session
*spt
)
40 qemu_free(spt
->filename
);
44 static int tftp_session_allocate(Slirp
*slirp
, struct tftp_t
*tp
)
46 struct tftp_session
*spt
;
49 for (k
= 0; k
< TFTP_SESSIONS_MAX
; k
++) {
50 spt
= &slirp
->tftp_sessions
[k
];
52 if (!tftp_session_in_use(spt
))
55 /* sessions time out after 5 inactive seconds */
56 if ((int)(curtime
- spt
->timestamp
) > 5000) {
57 qemu_free(spt
->filename
);
65 memset(spt
, 0, sizeof(*spt
));
66 memcpy(&spt
->client_ip
, &tp
->ip
.ip_src
, sizeof(spt
->client_ip
));
67 spt
->client_port
= tp
->udp
.uh_sport
;
70 tftp_session_update(spt
);
75 static int tftp_session_find(Slirp
*slirp
, struct tftp_t
*tp
)
77 struct tftp_session
*spt
;
80 for (k
= 0; k
< TFTP_SESSIONS_MAX
; k
++) {
81 spt
= &slirp
->tftp_sessions
[k
];
83 if (tftp_session_in_use(spt
)) {
84 if (!memcmp(&spt
->client_ip
, &tp
->ip
.ip_src
, sizeof(spt
->client_ip
))) {
85 if (spt
->client_port
== tp
->udp
.uh_sport
) {
95 static int tftp_read_data(struct tftp_session
*spt
, u_int16_t block_nr
,
96 u_int8_t
*buf
, int len
)
101 fd
= open(spt
->filename
, O_RDONLY
| O_BINARY
);
108 lseek(fd
, block_nr
* 512, SEEK_SET
);
110 bytes_read
= read(fd
, buf
, len
);
118 static int tftp_send_oack(struct tftp_session
*spt
,
119 const char *key
, uint32_t value
,
120 struct tftp_t
*recv_tp
)
122 struct sockaddr_in saddr
, daddr
;
127 m
= m_get(spt
->slirp
);
132 memset(m
->m_data
, 0, m
->m_size
);
134 m
->m_data
+= IF_MAXLINKHDR
;
135 tp
= (void *)m
->m_data
;
136 m
->m_data
+= sizeof(struct udpiphdr
);
138 tp
->tp_op
= htons(TFTP_OACK
);
139 n
+= snprintf((char *)tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%s",
141 n
+= snprintf((char *)tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%u",
144 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
145 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
147 daddr
.sin_addr
= spt
->client_ip
;
148 daddr
.sin_port
= spt
->client_port
;
150 m
->m_len
= sizeof(struct tftp_t
) - 514 + n
-
151 sizeof(struct ip
) - sizeof(struct udphdr
);
152 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
157 static void tftp_send_error(struct tftp_session
*spt
,
158 u_int16_t errorcode
, const char *msg
,
159 struct tftp_t
*recv_tp
)
161 struct sockaddr_in saddr
, daddr
;
166 m
= m_get(spt
->slirp
);
172 memset(m
->m_data
, 0, m
->m_size
);
174 m
->m_data
+= IF_MAXLINKHDR
;
175 tp
= (void *)m
->m_data
;
176 m
->m_data
+= sizeof(struct udpiphdr
);
178 tp
->tp_op
= htons(TFTP_ERROR
);
179 tp
->x
.tp_error
.tp_error_code
= htons(errorcode
);
180 pstrcpy((char *)tp
->x
.tp_error
.tp_msg
, sizeof(tp
->x
.tp_error
.tp_msg
), msg
);
182 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
183 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
185 daddr
.sin_addr
= spt
->client_ip
;
186 daddr
.sin_port
= spt
->client_port
;
190 m
->m_len
= sizeof(struct tftp_t
) - 514 + 3 + strlen(msg
) -
191 sizeof(struct ip
) - sizeof(struct udphdr
);
193 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
196 tftp_session_terminate(spt
);
199 static int tftp_send_data(struct tftp_session
*spt
,
201 struct tftp_t
*recv_tp
)
203 struct sockaddr_in saddr
, daddr
;
212 m
= m_get(spt
->slirp
);
218 memset(m
->m_data
, 0, m
->m_size
);
220 m
->m_data
+= IF_MAXLINKHDR
;
221 tp
= (void *)m
->m_data
;
222 m
->m_data
+= sizeof(struct udpiphdr
);
224 tp
->tp_op
= htons(TFTP_DATA
);
225 tp
->x
.tp_data
.tp_block_nr
= htons(block_nr
);
227 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
228 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
230 daddr
.sin_addr
= spt
->client_ip
;
231 daddr
.sin_port
= spt
->client_port
;
233 nobytes
= tftp_read_data(spt
, block_nr
- 1, tp
->x
.tp_data
.tp_buf
, 512);
238 /* send "file not found" error back */
240 tftp_send_error(spt
, 1, "File not found", tp
);
245 m
->m_len
= sizeof(struct tftp_t
) - (512 - nobytes
) -
246 sizeof(struct ip
) - sizeof(struct udphdr
);
248 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
250 if (nobytes
== 512) {
251 tftp_session_update(spt
);
254 tftp_session_terminate(spt
);
260 static void tftp_handle_rrq(Slirp
*slirp
, struct tftp_t
*tp
, int pktlen
)
262 struct tftp_session
*spt
;
267 /* check if a session already exists and if so terminate it */
268 s
= tftp_session_find(slirp
, tp
);
270 tftp_session_terminate(&slirp
->tftp_sessions
[s
]);
273 s
= tftp_session_allocate(slirp
, tp
);
279 spt
= &slirp
->tftp_sessions
[s
];
281 /* unspecifed prefix means service disabled */
282 if (!slirp
->tftp_prefix
) {
283 tftp_send_error(spt
, 2, "Access violation", tp
);
287 /* skip header fields */
289 pktlen
-= ((uint8_t *)&tp
->x
.tp_buf
[0] - (uint8_t *)tp
);
291 /* prepend tftp_prefix */
292 prefix_len
= strlen(slirp
->tftp_prefix
);
293 spt
->filename
= qemu_malloc(prefix_len
+ TFTP_FILENAME_MAX
+ 2);
294 memcpy(spt
->filename
, slirp
->tftp_prefix
, prefix_len
);
295 spt
->filename
[prefix_len
] = '/';
298 req_fname
= spt
->filename
+ prefix_len
+ 1;
301 if (k
>= TFTP_FILENAME_MAX
|| k
>= pktlen
) {
302 tftp_send_error(spt
, 2, "Access violation", tp
);
305 req_fname
[k
] = (char)tp
->x
.tp_buf
[k
];
306 if (req_fname
[k
++] == '\0') {
312 if ((pktlen
- k
) < 6) {
313 tftp_send_error(spt
, 2, "Access violation", tp
);
317 if (memcmp(&tp
->x
.tp_buf
[k
], "octet\0", 6) != 0) {
318 tftp_send_error(spt
, 4, "Unsupported transfer mode", tp
);
322 k
+= 6; /* skipping octet */
324 /* do sanity checks on the filename */
325 if (!strncmp(req_fname
, "../", 3) ||
326 req_fname
[strlen(req_fname
) - 1] == '/' ||
327 strstr(req_fname
, "/../")) {
328 tftp_send_error(spt
, 2, "Access violation", tp
);
332 /* check if the file exists */
333 if (tftp_read_data(spt
, 0, NULL
, 0) < 0) {
334 tftp_send_error(spt
, 1, "File not found", tp
);
338 if (tp
->x
.tp_buf
[pktlen
- 1] != 0) {
339 tftp_send_error(spt
, 2, "Access violation", tp
);
344 const char *key
, *value
;
346 key
= (const char *)&tp
->x
.tp_buf
[k
];
347 k
+= strlen(key
) + 1;
350 tftp_send_error(spt
, 2, "Access violation", tp
);
354 value
= (const char *)&tp
->x
.tp_buf
[k
];
355 k
+= strlen(value
) + 1;
357 if (strcmp(key
, "tsize") == 0) {
358 int tsize
= atoi(value
);
362 if (stat(spt
->filename
, &stat_p
) == 0)
363 tsize
= stat_p
.st_size
;
365 tftp_send_error(spt
, 1, "File not found", tp
);
370 tftp_send_oack(spt
, "tsize", tsize
, tp
);
375 tftp_send_data(spt
, 1, tp
);
378 static void tftp_handle_ack(Slirp
*slirp
, struct tftp_t
*tp
, int pktlen
)
382 s
= tftp_session_find(slirp
, tp
);
388 if (tftp_send_data(&slirp
->tftp_sessions
[s
],
389 ntohs(tp
->x
.tp_data
.tp_block_nr
) + 1,
395 static void tftp_handle_error(Slirp
*slirp
, struct tftp_t
*tp
, int pktlen
)
399 s
= tftp_session_find(slirp
, tp
);
405 tftp_session_terminate(&slirp
->tftp_sessions
[s
]);
408 void tftp_input(struct mbuf
*m
)
410 struct tftp_t
*tp
= (struct tftp_t
*)m
->m_data
;
412 switch(ntohs(tp
->tp_op
)) {
414 tftp_handle_rrq(m
->slirp
, tp
, m
->m_len
);
418 tftp_handle_ack(m
->slirp
, tp
, m
->m_len
);
422 tftp_handle_error(m
->slirp
, tp
, m
->m_len
);