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
)
44 g_free(spt
->filename
);
48 static int tftp_session_allocate(Slirp
*slirp
, struct tftp_t
*tp
)
50 struct tftp_session
*spt
;
53 for (k
= 0; k
< TFTP_SESSIONS_MAX
; k
++) {
54 spt
= &slirp
->tftp_sessions
[k
];
56 if (!tftp_session_in_use(spt
))
59 /* sessions time out after 5 inactive seconds */
60 if ((int)(curtime
- spt
->timestamp
) > 5000) {
61 tftp_session_terminate(spt
);
69 memset(spt
, 0, sizeof(*spt
));
70 memcpy(&spt
->client_ip
, &tp
->ip
.ip_src
, sizeof(spt
->client_ip
));
72 spt
->client_port
= tp
->udp
.uh_sport
;
75 tftp_session_update(spt
);
80 static int tftp_session_find(Slirp
*slirp
, struct tftp_t
*tp
)
82 struct tftp_session
*spt
;
85 for (k
= 0; k
< TFTP_SESSIONS_MAX
; k
++) {
86 spt
= &slirp
->tftp_sessions
[k
];
88 if (tftp_session_in_use(spt
)) {
89 if (!memcmp(&spt
->client_ip
, &tp
->ip
.ip_src
, sizeof(spt
->client_ip
))) {
90 if (spt
->client_port
== tp
->udp
.uh_sport
) {
100 static int tftp_read_data(struct tftp_session
*spt
, uint32_t block_nr
,
101 uint8_t *buf
, int len
)
106 spt
->fd
= open(spt
->filename
, O_RDONLY
| O_BINARY
);
114 lseek(spt
->fd
, block_nr
* 512, SEEK_SET
);
116 bytes_read
= read(spt
->fd
, buf
, len
);
122 static int tftp_send_oack(struct tftp_session
*spt
,
123 const char *keys
[], uint32_t values
[], int nb
,
124 struct tftp_t
*recv_tp
)
126 struct sockaddr_in saddr
, daddr
;
131 m
= m_get(spt
->slirp
);
136 memset(m
->m_data
, 0, m
->m_size
);
138 m
->m_data
+= IF_MAXLINKHDR
;
139 tp
= (void *)m
->m_data
;
140 m
->m_data
+= sizeof(struct udpiphdr
);
142 tp
->tp_op
= htons(TFTP_OACK
);
143 for (i
= 0; i
< nb
; i
++) {
144 n
+= snprintf(tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%s",
146 n
+= snprintf(tp
->x
.tp_buf
+ n
, sizeof(tp
->x
.tp_buf
) - n
, "%u",
150 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
151 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
153 daddr
.sin_addr
= spt
->client_ip
;
154 daddr
.sin_port
= spt
->client_port
;
156 m
->m_len
= sizeof(struct tftp_t
) - 514 + n
-
157 sizeof(struct ip
) - sizeof(struct udphdr
);
158 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
163 static void tftp_send_error(struct tftp_session
*spt
,
164 uint16_t errorcode
, const char *msg
,
165 struct tftp_t
*recv_tp
)
167 struct sockaddr_in saddr
, daddr
;
171 m
= m_get(spt
->slirp
);
177 memset(m
->m_data
, 0, m
->m_size
);
179 m
->m_data
+= IF_MAXLINKHDR
;
180 tp
= (void *)m
->m_data
;
181 m
->m_data
+= sizeof(struct udpiphdr
);
183 tp
->tp_op
= htons(TFTP_ERROR
);
184 tp
->x
.tp_error
.tp_error_code
= htons(errorcode
);
185 pstrcpy((char *)tp
->x
.tp_error
.tp_msg
, sizeof(tp
->x
.tp_error
.tp_msg
), msg
);
187 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
188 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
190 daddr
.sin_addr
= spt
->client_ip
;
191 daddr
.sin_port
= spt
->client_port
;
193 m
->m_len
= sizeof(struct tftp_t
) - 514 + 3 + strlen(msg
) -
194 sizeof(struct ip
) - sizeof(struct udphdr
);
196 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
199 tftp_session_terminate(spt
);
202 static void tftp_send_next_block(struct tftp_session
*spt
,
203 struct tftp_t
*recv_tp
)
205 struct sockaddr_in saddr
, daddr
;
210 m
= m_get(spt
->slirp
);
216 memset(m
->m_data
, 0, m
->m_size
);
218 m
->m_data
+= IF_MAXLINKHDR
;
219 tp
= (void *)m
->m_data
;
220 m
->m_data
+= sizeof(struct udpiphdr
);
222 tp
->tp_op
= htons(TFTP_DATA
);
223 tp
->x
.tp_data
.tp_block_nr
= htons((spt
->block_nr
+ 1) & 0xffff);
225 saddr
.sin_addr
= recv_tp
->ip
.ip_dst
;
226 saddr
.sin_port
= recv_tp
->udp
.uh_dport
;
228 daddr
.sin_addr
= spt
->client_ip
;
229 daddr
.sin_port
= spt
->client_port
;
231 nobytes
= tftp_read_data(spt
, spt
->block_nr
, tp
->x
.tp_data
.tp_buf
, 512);
236 /* send "file not found" error back */
238 tftp_send_error(spt
, 1, "File not found", tp
);
243 m
->m_len
= sizeof(struct tftp_t
) - (512 - nobytes
) -
244 sizeof(struct ip
) - sizeof(struct udphdr
);
246 udp_output2(NULL
, m
, &saddr
, &daddr
, IPTOS_LOWDELAY
);
248 if (nobytes
== 512) {
249 tftp_session_update(spt
);
252 tftp_session_terminate(spt
);
258 static void tftp_handle_rrq(Slirp
*slirp
, struct tftp_t
*tp
, int pktlen
)
260 struct tftp_session
*spt
;
264 const char *option_name
[2];
265 uint32_t option_value
[2];
268 /* check if a session already exists and if so terminate it */
269 s
= tftp_session_find(slirp
, tp
);
271 tftp_session_terminate(&slirp
->tftp_sessions
[s
]);
274 s
= tftp_session_allocate(slirp
, tp
);
280 spt
= &slirp
->tftp_sessions
[s
];
282 /* unspecifed prefix means service disabled */
283 if (!slirp
->tftp_prefix
) {
284 tftp_send_error(spt
, 2, "Access violation", tp
);
288 /* skip header fields */
290 pktlen
-= offsetof(struct tftp_t
, x
.tp_buf
);
292 /* prepend tftp_prefix */
293 prefix_len
= strlen(slirp
->tftp_prefix
);
294 spt
->filename
= g_malloc(prefix_len
+ TFTP_FILENAME_MAX
+ 2);
295 memcpy(spt
->filename
, slirp
->tftp_prefix
, prefix_len
);
296 spt
->filename
[prefix_len
] = '/';
299 req_fname
= spt
->filename
+ prefix_len
+ 1;
302 if (k
>= TFTP_FILENAME_MAX
|| k
>= pktlen
) {
303 tftp_send_error(spt
, 2, "Access violation", tp
);
306 req_fname
[k
] = tp
->x
.tp_buf
[k
];
307 if (req_fname
[k
++] == '\0') {
313 if ((pktlen
- k
) < 6) {
314 tftp_send_error(spt
, 2, "Access violation", tp
);
318 if (strcasecmp(&tp
->x
.tp_buf
[k
], "octet") != 0) {
319 tftp_send_error(spt
, 4, "Unsupported transfer mode", tp
);
323 k
+= 6; /* skipping octet */
325 /* do sanity checks on the filename */
326 if (!strncmp(req_fname
, "../", 3) ||
327 req_fname
[strlen(req_fname
) - 1] == '/' ||
328 strstr(req_fname
, "/../")) {
329 tftp_send_error(spt
, 2, "Access violation", tp
);
333 /* check if the file exists */
334 if (tftp_read_data(spt
, 0, NULL
, 0) < 0) {
335 tftp_send_error(spt
, 1, "File not found", tp
);
339 if (tp
->x
.tp_buf
[pktlen
- 1] != 0) {
340 tftp_send_error(spt
, 2, "Access violation", tp
);
344 while (k
< pktlen
&& nb_options
< ARRAY_SIZE(option_name
)) {
345 const char *key
, *value
;
347 key
= &tp
->x
.tp_buf
[k
];
348 k
+= strlen(key
) + 1;
351 tftp_send_error(spt
, 2, "Access violation", tp
);
355 value
= &tp
->x
.tp_buf
[k
];
356 k
+= strlen(value
) + 1;
358 if (strcasecmp(key
, "tsize") == 0) {
359 int tsize
= atoi(value
);
363 if (stat(spt
->filename
, &stat_p
) == 0)
364 tsize
= stat_p
.st_size
;
366 tftp_send_error(spt
, 1, "File not found", tp
);
371 option_name
[nb_options
] = "tsize";
372 option_value
[nb_options
] = tsize
;
374 } else if (strcasecmp(key
, "blksize") == 0) {
375 int blksize
= atoi(value
);
377 /* If blksize option is bigger than what we will
378 * emit, accept the option with our packet size.
379 * Otherwise, simply do as we didn't see the option.
381 if (blksize
>= 512) {
382 option_name
[nb_options
] = "blksize";
383 option_value
[nb_options
] = 512;
389 if (nb_options
> 0) {
390 assert(nb_options
<= ARRAY_SIZE(option_name
));
391 tftp_send_oack(spt
, option_name
, option_value
, nb_options
, tp
);
396 tftp_send_next_block(spt
, tp
);
399 static void tftp_handle_ack(Slirp
*slirp
, struct tftp_t
*tp
, int pktlen
)
403 s
= tftp_session_find(slirp
, tp
);
409 tftp_send_next_block(&slirp
->tftp_sessions
[s
], tp
);
412 static void tftp_handle_error(Slirp
*slirp
, struct tftp_t
*tp
, int pktlen
)
416 s
= tftp_session_find(slirp
, tp
);
422 tftp_session_terminate(&slirp
->tftp_sessions
[s
]);
425 void tftp_input(struct mbuf
*m
)
427 struct tftp_t
*tp
= (struct tftp_t
*)m
->m_data
;
429 switch(ntohs(tp
->tp_op
)) {
431 tftp_handle_rrq(m
->slirp
, tp
, m
->m_len
);
435 tftp_handle_ack(m
->slirp
, tp
, m
->m_len
);
439 tftp_handle_error(m
->slirp
, tp
, m
->m_len
);