31a1e258cd307bf405fa2461b2078b29f49457a2
[qemu.git] / slirp / tftp.c
blob31a1e258cd307bf405fa2461b2078b29f49457a2
1 /*
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
22 * THE SOFTWARE.
25 #include <slirp.h>
26 #include "qemu-common.h"
28 struct tftp_session {
29 int in_use;
30 char *filename;
32 struct in_addr client_ip;
33 u_int16_t client_port;
35 int timestamp;
38 static struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
40 char *tftp_prefix;
42 static void tftp_session_update(struct tftp_session *spt)
44 spt->timestamp = curtime;
45 spt->in_use = 1;
48 static void tftp_session_terminate(struct tftp_session *spt)
50 qemu_free(spt->filename);
51 spt->in_use = 0;
54 static int tftp_session_allocate(struct tftp_t *tp)
56 struct tftp_session *spt;
57 int k;
59 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
60 spt = &tftp_sessions[k];
62 if (!spt->in_use)
63 goto found;
65 /* sessions time out after 5 inactive seconds */
66 if ((int)(curtime - spt->timestamp) > 5000) {
67 qemu_free(spt->filename);
68 goto found;
72 return -1;
74 found:
75 memset(spt, 0, sizeof(*spt));
76 memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
77 spt->client_port = tp->udp.uh_sport;
79 tftp_session_update(spt);
81 return k;
84 static int tftp_session_find(struct tftp_t *tp)
86 struct tftp_session *spt;
87 int k;
89 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
90 spt = &tftp_sessions[k];
92 if (spt->in_use) {
93 if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
94 if (spt->client_port == tp->udp.uh_sport) {
95 return k;
101 return -1;
104 static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr,
105 u_int8_t *buf, int len)
107 int fd;
108 int bytes_read = 0;
110 fd = open(spt->filename, O_RDONLY | O_BINARY);
112 if (fd < 0) {
113 return -1;
116 if (len) {
117 lseek(fd, block_nr * 512, SEEK_SET);
119 bytes_read = read(fd, buf, len);
122 close(fd);
124 return bytes_read;
127 static int tftp_send_oack(struct tftp_session *spt,
128 const char *key, uint32_t value,
129 struct tftp_t *recv_tp)
131 struct sockaddr_in saddr, daddr;
132 struct mbuf *m;
133 struct tftp_t *tp;
134 int n = 0;
136 m = m_get();
138 if (!m)
139 return -1;
141 memset(m->m_data, 0, m->m_size);
143 m->m_data += IF_MAXLINKHDR;
144 tp = (void *)m->m_data;
145 m->m_data += sizeof(struct udpiphdr);
147 tp->tp_op = htons(TFTP_OACK);
148 n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
149 key) + 1;
150 n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
151 value) + 1;
153 saddr.sin_addr = recv_tp->ip.ip_dst;
154 saddr.sin_port = recv_tp->udp.uh_dport;
156 daddr.sin_addr = spt->client_ip;
157 daddr.sin_port = spt->client_port;
159 m->m_len = sizeof(struct tftp_t) - 514 + n -
160 sizeof(struct ip) - sizeof(struct udphdr);
161 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
163 return 0;
166 static void tftp_send_error(struct tftp_session *spt,
167 u_int16_t errorcode, const char *msg,
168 struct tftp_t *recv_tp)
170 struct sockaddr_in saddr, daddr;
171 struct mbuf *m;
172 struct tftp_t *tp;
173 int nobytes;
175 m = m_get();
177 if (!m) {
178 goto out;
181 memset(m->m_data, 0, m->m_size);
183 m->m_data += IF_MAXLINKHDR;
184 tp = (void *)m->m_data;
185 m->m_data += sizeof(struct udpiphdr);
187 tp->tp_op = htons(TFTP_ERROR);
188 tp->x.tp_error.tp_error_code = htons(errorcode);
189 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
191 saddr.sin_addr = recv_tp->ip.ip_dst;
192 saddr.sin_port = recv_tp->udp.uh_dport;
194 daddr.sin_addr = spt->client_ip;
195 daddr.sin_port = spt->client_port;
197 nobytes = 2;
199 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
200 sizeof(struct ip) - sizeof(struct udphdr);
202 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
204 out:
205 tftp_session_terminate(spt);
208 static int tftp_send_data(struct tftp_session *spt,
209 u_int16_t block_nr,
210 struct tftp_t *recv_tp)
212 struct sockaddr_in saddr, daddr;
213 struct mbuf *m;
214 struct tftp_t *tp;
215 int nobytes;
217 if (block_nr < 1) {
218 return -1;
221 m = m_get();
223 if (!m) {
224 return -1;
227 memset(m->m_data, 0, m->m_size);
229 m->m_data += IF_MAXLINKHDR;
230 tp = (void *)m->m_data;
231 m->m_data += sizeof(struct udpiphdr);
233 tp->tp_op = htons(TFTP_DATA);
234 tp->x.tp_data.tp_block_nr = htons(block_nr);
236 saddr.sin_addr = recv_tp->ip.ip_dst;
237 saddr.sin_port = recv_tp->udp.uh_dport;
239 daddr.sin_addr = spt->client_ip;
240 daddr.sin_port = spt->client_port;
242 nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
244 if (nobytes < 0) {
245 m_free(m);
247 /* send "file not found" error back */
249 tftp_send_error(spt, 1, "File not found", tp);
251 return -1;
254 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
255 sizeof(struct ip) - sizeof(struct udphdr);
257 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
259 if (nobytes == 512) {
260 tftp_session_update(spt);
262 else {
263 tftp_session_terminate(spt);
266 return 0;
269 static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
271 struct tftp_session *spt;
272 int s, k;
273 size_t prefix_len;
274 char *req_fname;
276 s = tftp_session_allocate(tp);
278 if (s < 0) {
279 return;
282 spt = &tftp_sessions[s];
284 /* unspecifed prefix means service disabled */
285 if (!tftp_prefix) {
286 tftp_send_error(spt, 2, "Access violation", tp);
287 return;
290 /* skip header fields */
291 k = 0;
292 pktlen -= ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
294 /* prepend tftp_prefix */
295 prefix_len = strlen(tftp_prefix);
296 spt->filename = qemu_malloc(prefix_len + TFTP_FILENAME_MAX + 1);
297 memcpy(spt->filename, tftp_prefix, prefix_len);
299 /* get name */
300 req_fname = spt->filename + prefix_len;
302 while (1) {
303 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
304 tftp_send_error(spt, 2, "Access violation", tp);
305 return;
307 req_fname[k] = (char)tp->x.tp_buf[k];
308 if (req_fname[k++] == '\0') {
309 break;
313 /* check mode */
314 if ((pktlen - k) < 6) {
315 tftp_send_error(spt, 2, "Access violation", tp);
316 return;
319 if (memcmp(&tp->x.tp_buf[k], "octet\0", 6) != 0) {
320 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
321 return;
324 k += 6; /* skipping octet */
326 /* do sanity checks on the filename */
327 if (req_fname[0] != '/' || req_fname[strlen(req_fname) - 1] == '/' ||
328 strstr(req_fname, "/../")) {
329 tftp_send_error(spt, 2, "Access violation", tp);
330 return;
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);
336 return;
339 if (tp->x.tp_buf[pktlen - 1] != 0) {
340 tftp_send_error(spt, 2, "Access violation", tp);
341 return;
344 while (k < pktlen) {
345 const char *key, *value;
347 key = (const char *)&tp->x.tp_buf[k];
348 k += strlen(key) + 1;
350 if (k >= pktlen) {
351 tftp_send_error(spt, 2, "Access violation", tp);
352 return;
355 value = (const char *)&tp->x.tp_buf[k];
356 k += strlen(value) + 1;
358 if (strcmp(key, "tsize") == 0) {
359 int tsize = atoi(value);
360 struct stat stat_p;
362 if (tsize == 0) {
363 if (stat(spt->filename, &stat_p) == 0)
364 tsize = stat_p.st_size;
365 else {
366 tftp_send_error(spt, 1, "File not found", tp);
367 return;
371 tftp_send_oack(spt, "tsize", tsize, tp);
375 tftp_send_data(spt, 1, tp);
378 static void tftp_handle_ack(struct tftp_t *tp, int pktlen)
380 int s;
382 s = tftp_session_find(tp);
384 if (s < 0) {
385 return;
388 if (tftp_send_data(&tftp_sessions[s],
389 ntohs(tp->x.tp_data.tp_block_nr) + 1,
390 tp) < 0) {
391 return;
395 void tftp_input(struct mbuf *m)
397 struct tftp_t *tp = (struct tftp_t *)m->m_data;
399 switch(ntohs(tp->tp_op)) {
400 case TFTP_RRQ:
401 tftp_handle_rrq(tp, m->m_len);
402 break;
404 case TFTP_ACK:
405 tftp_handle_ack(tp, m->m_len);
406 break;