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" // for pstrcpy
30 unsigned char filename
[TFTP_FILENAME_MAX
];
32 struct in_addr client_ip
;
33 u_int16_t client_port
;
38 static struct tftp_session tftp_sessions
[TFTP_SESSIONS_MAX
];
40 const char *tftp_prefix
;
42 static void tftp_session_update(struct tftp_session
*spt
)
44 spt
->timestamp
= curtime
;
48 static void tftp_session_terminate(struct tftp_session
*spt
)
53 static int tftp_session_allocate(struct tftp_t
*tp
)
55 struct tftp_session
*spt
;
58 for (k
= 0; k
< TFTP_SESSIONS_MAX
; k
++) {
59 spt
= &tftp_sessions
[k
];
64 /* sessions time out after 5 inactive seconds */
65 if ((int)(curtime
- spt
->timestamp
) > 5000)
72 memset(spt
, 0, sizeof(*spt
));
73 memcpy(&spt
->client_ip
, &tp
->ip
.ip_src
, sizeof(spt
->client_ip
));
74 spt
->client_port
= tp
->udp
.uh_sport
;
76 tftp_session_update(spt
);
81 static int tftp_session_find(struct tftp_t
*tp
)
83 struct tftp_session
*spt
;
86 for (k
= 0; k
< TFTP_SESSIONS_MAX
; k
++) {
87 spt
= &tftp_sessions
[k
];
90 if (!memcmp(&spt
->client_ip
, &tp
->ip
.ip_src
, sizeof(spt
->client_ip
))) {
91 if (spt
->client_port
== tp
->udp
.uh_sport
) {
101 static int tftp_read_data(struct tftp_session
*spt
, u_int16_t block_nr
,
102 u_int8_t
*buf
, int len
)
109 n
= snprintf(buffer
, sizeof(buffer
), "%s/%s",
110 tftp_prefix
, spt
->filename
);
111 if (n
>= sizeof(buffer
))
114 fd
= open(buffer
, O_RDONLY
| O_BINARY
);
121 lseek(fd
, block_nr
* 512, SEEK_SET
);
123 bytes_read
= read(fd
, buf
, len
);
131 static int tftp_send_oack(struct tftp_session
*spt
,
132 const char *key
, uint32_t value
,
133 struct tftp_t
*recv_tp
)
135 struct sockaddr_in saddr
, daddr
;
145 memset(m
->m_data
, 0, m
->m_size
);
147 m
->m_data
+= IF_MAXLINKHDR
;
148 tp
= (void *)m
->m_data
;
149 m
->m_data
+= sizeof(struct udpiphdr
);
151 tp
->tp_op
= htons(TFTP_OACK
);
152 n
+= snprintf((char *)tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%s",
154 n
+= snprintf((char *)tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%u",
157 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
158 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
160 daddr
.sin_addr
= spt
->client_ip
;
161 daddr
.sin_port
= spt
->client_port
;
163 m
->m_len
= sizeof(struct tftp_t
) - 514 + n
-
164 sizeof(struct ip
) - sizeof(struct udphdr
);
165 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
172 static int tftp_send_error(struct tftp_session
*spt
,
173 u_int16_t errorcode
, const char *msg
,
174 struct tftp_t
*recv_tp
)
176 struct sockaddr_in saddr
, daddr
;
187 memset(m
->m_data
, 0, m
->m_size
);
189 m
->m_data
+= IF_MAXLINKHDR
;
190 tp
= (void *)m
->m_data
;
191 m
->m_data
+= sizeof(struct udpiphdr
);
193 tp
->tp_op
= htons(TFTP_ERROR
);
194 tp
->x
.tp_error
.tp_error_code
= htons(errorcode
);
195 pstrcpy((char *)tp
->x
.tp_error
.tp_msg
, sizeof(tp
->x
.tp_error
.tp_msg
), msg
);
197 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
198 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
200 daddr
.sin_addr
= spt
->client_ip
;
201 daddr
.sin_port
= spt
->client_port
;
205 m
->m_len
= sizeof(struct tftp_t
) - 514 + 3 + strlen(msg
) -
206 sizeof(struct ip
) - sizeof(struct udphdr
);
208 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
210 tftp_session_terminate(spt
);
215 static int tftp_send_data(struct tftp_session
*spt
,
217 struct tftp_t
*recv_tp
)
219 struct sockaddr_in saddr
, daddr
;
234 memset(m
->m_data
, 0, m
->m_size
);
236 m
->m_data
+= IF_MAXLINKHDR
;
237 tp
= (void *)m
->m_data
;
238 m
->m_data
+= sizeof(struct udpiphdr
);
240 tp
->tp_op
= htons(TFTP_DATA
);
241 tp
->x
.tp_data
.tp_block_nr
= htons(block_nr
);
243 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
244 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
246 daddr
.sin_addr
= spt
->client_ip
;
247 daddr
.sin_port
= spt
->client_port
;
249 nobytes
= tftp_read_data(spt
, block_nr
- 1, tp
->x
.tp_data
.tp_buf
, 512);
254 /* send "file not found" error back */
256 tftp_send_error(spt
, 1, "File not found", tp
);
261 m
->m_len
= sizeof(struct tftp_t
) - (512 - nobytes
) -
262 sizeof(struct ip
) - sizeof(struct udphdr
);
264 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
266 if (nobytes
== 512) {
267 tftp_session_update(spt
);
270 tftp_session_terminate(spt
);
276 static void tftp_handle_rrq(struct tftp_t
*tp
, int pktlen
)
278 struct tftp_session
*spt
;
282 s
= tftp_session_allocate(tp
);
288 spt
= &tftp_sessions
[s
];
292 n
= pktlen
- ((uint8_t *)&tp
->x
.tp_buf
[0] - (uint8_t *)tp
);
296 for (k
= 0; k
< n
; k
++) {
297 if (k
< TFTP_FILENAME_MAX
) {
304 if (src
[k
] == '\0') {
320 if (memcmp(&src
[k
], "octet\0", 6) != 0) {
321 tftp_send_error(spt
, 4, "Unsupported transfer mode", tp
);
325 k
+= 6; /* skipping octet */
327 /* do sanity checks on the filename */
329 if ((spt
->filename
[0] != '/')
330 || (spt
->filename
[strlen((char *)spt
->filename
) - 1] == '/')
331 || strstr((char *)spt
->filename
, "/../")) {
332 tftp_send_error(spt
, 2, "Access violation", tp
);
336 /* only allow exported prefixes */
339 tftp_send_error(spt
, 2, "Access violation", tp
);
343 /* check if the file exists */
345 if (tftp_read_data(spt
, 0, spt
->filename
, 0) < 0) {
346 tftp_send_error(spt
, 1, "File not found", tp
);
350 if (src
[n
- 1] != 0) {
351 tftp_send_error(spt
, 2, "Access violation", tp
);
356 const char *key
, *value
;
358 key
= (char *)src
+ k
;
359 k
+= strlen(key
) + 1;
362 tftp_send_error(spt
, 2, "Access violation", tp
);
366 value
= (char *)src
+ k
;
367 k
+= strlen(value
) + 1;
369 if (strcmp(key
, "tsize") == 0) {
370 int tsize
= atoi(value
);
373 if (tsize
== 0 && tftp_prefix
) {
377 len
= snprintf(buffer
, sizeof(buffer
), "%s/%s",
378 tftp_prefix
, spt
->filename
);
380 if (stat(buffer
, &stat_p
) == 0)
381 tsize
= stat_p
.st_size
;
383 tftp_send_error(spt
, 1, "File not found", tp
);
388 tftp_send_oack(spt
, "tsize", tsize
, tp
);
392 tftp_send_data(spt
, 1, tp
);
395 static void tftp_handle_ack(struct tftp_t
*tp
, int pktlen
)
399 s
= tftp_session_find(tp
);
405 if (tftp_send_data(&tftp_sessions
[s
],
406 ntohs(tp
->x
.tp_data
.tp_block_nr
) + 1,
412 void tftp_input(struct mbuf
*m
)
414 struct tftp_t
*tp
= (struct tftp_t
*)m
->m_data
;
416 switch(ntohs(tp
->tp_op
)) {
418 tftp_handle_rrq(tp
, m
->m_len
);
422 tftp_handle_ack(tp
, m
->m_len
);