cpu: Add callback to check architectural watchpoint match
[qemu/ar7.git] / slirp / tftp.c
blobabb010621c4ad791a981a7277d3664064efcc6a2
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 "qemu/osdep.h"
26 #include <slirp.h>
27 #include "qemu-common.h"
29 static inline int tftp_session_in_use(struct tftp_session *spt)
31 return (spt->slirp != NULL);
34 static inline void tftp_session_update(struct tftp_session *spt)
36 spt->timestamp = curtime;
39 static void tftp_session_terminate(struct tftp_session *spt)
41 if (spt->fd >= 0) {
42 close(spt->fd);
43 spt->fd = -1;
45 g_free(spt->filename);
46 spt->slirp = NULL;
49 static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
51 struct tftp_session *spt;
52 int k;
54 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
55 spt = &slirp->tftp_sessions[k];
57 if (!tftp_session_in_use(spt))
58 goto found;
60 /* sessions time out after 5 inactive seconds */
61 if ((int)(curtime - spt->timestamp) > 5000) {
62 tftp_session_terminate(spt);
63 goto found;
67 return -1;
69 found:
70 memset(spt, 0, sizeof(*spt));
71 memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
72 spt->fd = -1;
73 spt->client_port = tp->udp.uh_sport;
74 spt->slirp = slirp;
76 tftp_session_update(spt);
78 return k;
81 static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
83 struct tftp_session *spt;
84 int k;
86 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
87 spt = &slirp->tftp_sessions[k];
89 if (tftp_session_in_use(spt)) {
90 if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
91 if (spt->client_port == tp->udp.uh_sport) {
92 return k;
98 return -1;
101 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
102 uint8_t *buf, int len)
104 int bytes_read = 0;
106 if (spt->fd < 0) {
107 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
110 if (spt->fd < 0) {
111 return -1;
114 if (len) {
115 lseek(spt->fd, block_nr * 512, SEEK_SET);
117 bytes_read = read(spt->fd, buf, len);
120 return bytes_read;
123 static int tftp_send_oack(struct tftp_session *spt,
124 const char *keys[], uint32_t values[], int nb,
125 struct tftp_t *recv_tp)
127 struct sockaddr_in saddr, daddr;
128 struct mbuf *m;
129 struct tftp_t *tp;
130 int i, n = 0;
132 m = m_get(spt->slirp);
134 if (!m)
135 return -1;
137 memset(m->m_data, 0, m->m_size);
139 m->m_data += IF_MAXLINKHDR;
140 tp = (void *)m->m_data;
141 m->m_data += sizeof(struct udpiphdr);
143 tp->tp_op = htons(TFTP_OACK);
144 for (i = 0; i < nb; i++) {
145 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
146 keys[i]) + 1;
147 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
148 values[i]) + 1;
151 saddr.sin_addr = recv_tp->ip.ip_dst;
152 saddr.sin_port = recv_tp->udp.uh_dport;
154 daddr.sin_addr = spt->client_ip;
155 daddr.sin_port = spt->client_port;
157 m->m_len = sizeof(struct tftp_t) - 514 + n -
158 sizeof(struct ip) - sizeof(struct udphdr);
159 udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
161 return 0;
164 static void tftp_send_error(struct tftp_session *spt,
165 uint16_t errorcode, const char *msg,
166 struct tftp_t *recv_tp)
168 struct sockaddr_in saddr, daddr;
169 struct mbuf *m;
170 struct tftp_t *tp;
172 m = m_get(spt->slirp);
174 if (!m) {
175 goto out;
178 memset(m->m_data, 0, m->m_size);
180 m->m_data += IF_MAXLINKHDR;
181 tp = (void *)m->m_data;
182 m->m_data += sizeof(struct udpiphdr);
184 tp->tp_op = htons(TFTP_ERROR);
185 tp->x.tp_error.tp_error_code = htons(errorcode);
186 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
188 saddr.sin_addr = recv_tp->ip.ip_dst;
189 saddr.sin_port = recv_tp->udp.uh_dport;
191 daddr.sin_addr = spt->client_ip;
192 daddr.sin_port = spt->client_port;
194 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
195 sizeof(struct ip) - sizeof(struct udphdr);
197 udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
199 out:
200 tftp_session_terminate(spt);
203 static void tftp_send_next_block(struct tftp_session *spt,
204 struct tftp_t *recv_tp)
206 struct sockaddr_in saddr, daddr;
207 struct mbuf *m;
208 struct tftp_t *tp;
209 int nobytes;
211 m = m_get(spt->slirp);
213 if (!m) {
214 return;
217 memset(m->m_data, 0, m->m_size);
219 m->m_data += IF_MAXLINKHDR;
220 tp = (void *)m->m_data;
221 m->m_data += sizeof(struct udpiphdr);
223 tp->tp_op = htons(TFTP_DATA);
224 tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
226 saddr.sin_addr = recv_tp->ip.ip_dst;
227 saddr.sin_port = recv_tp->udp.uh_dport;
229 daddr.sin_addr = spt->client_ip;
230 daddr.sin_port = spt->client_port;
232 nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
234 if (nobytes < 0) {
235 m_free(m);
237 /* send "file not found" error back */
239 tftp_send_error(spt, 1, "File not found", tp);
241 return;
244 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
245 sizeof(struct ip) - sizeof(struct udphdr);
247 udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
249 if (nobytes == 512) {
250 tftp_session_update(spt);
252 else {
253 tftp_session_terminate(spt);
256 spt->block_nr++;
259 static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
261 struct tftp_session *spt;
262 int s, k;
263 size_t prefix_len;
264 char *req_fname;
265 const char *option_name[2];
266 uint32_t option_value[2];
267 int nb_options = 0;
269 /* check if a session already exists and if so terminate it */
270 s = tftp_session_find(slirp, tp);
271 if (s >= 0) {
272 tftp_session_terminate(&slirp->tftp_sessions[s]);
275 s = tftp_session_allocate(slirp, tp);
277 if (s < 0) {
278 return;
281 spt = &slirp->tftp_sessions[s];
283 /* unspecified prefix means service disabled */
284 if (!slirp->tftp_prefix) {
285 tftp_send_error(spt, 2, "Access violation", tp);
286 return;
289 /* skip header fields */
290 k = 0;
291 pktlen -= offsetof(struct tftp_t, x.tp_buf);
293 /* prepend tftp_prefix */
294 prefix_len = strlen(slirp->tftp_prefix);
295 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
296 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
297 spt->filename[prefix_len] = '/';
299 /* get name */
300 req_fname = spt->filename + prefix_len + 1;
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] = 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 (strcasecmp(&tp->x.tp_buf[k], "octet") != 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 (!strncmp(req_fname, "../", 3) ||
328 req_fname[strlen(req_fname) - 1] == '/' ||
329 strstr(req_fname, "/../")) {
330 tftp_send_error(spt, 2, "Access violation", tp);
331 return;
334 /* check if the file exists */
335 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
336 tftp_send_error(spt, 1, "File not found", tp);
337 return;
340 if (tp->x.tp_buf[pktlen - 1] != 0) {
341 tftp_send_error(spt, 2, "Access violation", tp);
342 return;
345 while (k < pktlen && nb_options < ARRAY_SIZE(option_name)) {
346 const char *key, *value;
348 key = &tp->x.tp_buf[k];
349 k += strlen(key) + 1;
351 if (k >= pktlen) {
352 tftp_send_error(spt, 2, "Access violation", tp);
353 return;
356 value = &tp->x.tp_buf[k];
357 k += strlen(value) + 1;
359 if (strcasecmp(key, "tsize") == 0) {
360 int tsize = atoi(value);
361 struct stat stat_p;
363 if (tsize == 0) {
364 if (stat(spt->filename, &stat_p) == 0)
365 tsize = stat_p.st_size;
366 else {
367 tftp_send_error(spt, 1, "File not found", tp);
368 return;
372 option_name[nb_options] = "tsize";
373 option_value[nb_options] = tsize;
374 nb_options++;
375 } else if (strcasecmp(key, "blksize") == 0) {
376 int blksize = atoi(value);
378 /* If blksize option is bigger than what we will
379 * emit, accept the option with our packet size.
380 * Otherwise, simply do as we didn't see the option.
382 if (blksize >= 512) {
383 option_name[nb_options] = "blksize";
384 option_value[nb_options] = 512;
385 nb_options++;
390 if (nb_options > 0) {
391 assert(nb_options <= ARRAY_SIZE(option_name));
392 tftp_send_oack(spt, option_name, option_value, nb_options, tp);
393 return;
396 spt->block_nr = 0;
397 tftp_send_next_block(spt, tp);
400 static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
402 int s;
404 s = tftp_session_find(slirp, tp);
406 if (s < 0) {
407 return;
410 tftp_send_next_block(&slirp->tftp_sessions[s], tp);
413 static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
415 int s;
417 s = tftp_session_find(slirp, tp);
419 if (s < 0) {
420 return;
423 tftp_session_terminate(&slirp->tftp_sessions[s]);
426 void tftp_input(struct mbuf *m)
428 struct tftp_t *tp = (struct tftp_t *)m->m_data;
430 switch(ntohs(tp->tp_op)) {
431 case TFTP_RRQ:
432 tftp_handle_rrq(m->slirp, tp, m->m_len);
433 break;
435 case TFTP_ACK:
436 tftp_handle_ack(m->slirp, tp, m->m_len);
437 break;
439 case TFTP_ERROR:
440 tftp_handle_error(m->slirp, tp, m->m_len);
441 break;