blob: 082f5d0409f43b15bf3c644318ca1d5019e9d8b7 [file] [log] [blame]
bellardc7f74642004-08-24 21:57:12 +00001/*
2 * tftp.c - a simple, read-only tftp server for qemu
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardc7f74642004-08-24 21:57:12 +00004 * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardc7f74642004-08-24 21:57:12 +00006 * 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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25#include <slirp.h>
Jan Kiszka0d62c4c2009-06-24 14:42:29 +020026#include "qemu-common.h"
bellardc7f74642004-08-24 21:57:12 +000027
Jan Kiszka460fec62009-06-24 14:42:31 +020028static inline int tftp_session_in_use(struct tftp_session *spt)
29{
30 return (spt->slirp != NULL);
31}
ths3b46e622007-09-17 08:09:54 +000032
Jan Kiszka460fec62009-06-24 14:42:31 +020033static inline void tftp_session_update(struct tftp_session *spt)
bellardc7f74642004-08-24 21:57:12 +000034{
bellarda3504c82004-08-25 20:55:44 +000035 spt->timestamp = curtime;
bellardc7f74642004-08-24 21:57:12 +000036}
37
38static void tftp_session_terminate(struct tftp_session *spt)
39{
Jan Kiszka460fec62009-06-24 14:42:31 +020040 qemu_free(spt->filename);
41 spt->slirp = NULL;
bellardc7f74642004-08-24 21:57:12 +000042}
43
Jan Kiszka460fec62009-06-24 14:42:31 +020044static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
bellardc7f74642004-08-24 21:57:12 +000045{
46 struct tftp_session *spt;
bellardc7f74642004-08-24 21:57:12 +000047 int k;
48
bellardc7f74642004-08-24 21:57:12 +000049 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
Jan Kiszka460fec62009-06-24 14:42:31 +020050 spt = &slirp->tftp_sessions[k];
bellardc7f74642004-08-24 21:57:12 +000051
Jan Kiszka460fec62009-06-24 14:42:31 +020052 if (!tftp_session_in_use(spt))
bellarda3504c82004-08-25 20:55:44 +000053 goto found;
bellardc7f74642004-08-24 21:57:12 +000054
55 /* sessions time out after 5 inactive seconds */
Jan Kiszka93679642009-06-24 14:42:30 +020056 if ((int)(curtime - spt->timestamp) > 5000) {
57 qemu_free(spt->filename);
bellarda3504c82004-08-25 20:55:44 +000058 goto found;
Jan Kiszka93679642009-06-24 14:42:30 +020059 }
bellardc7f74642004-08-24 21:57:12 +000060 }
61
62 return -1;
63
64 found:
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;
Jan Kiszka460fec62009-06-24 14:42:31 +020068 spt->slirp = slirp;
bellardc7f74642004-08-24 21:57:12 +000069
70 tftp_session_update(spt);
71
72 return k;
73}
74
Jan Kiszka460fec62009-06-24 14:42:31 +020075static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
bellardc7f74642004-08-24 21:57:12 +000076{
77 struct tftp_session *spt;
78 int k;
79
80 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
Jan Kiszka460fec62009-06-24 14:42:31 +020081 spt = &slirp->tftp_sessions[k];
bellardc7f74642004-08-24 21:57:12 +000082
Jan Kiszka460fec62009-06-24 14:42:31 +020083 if (tftp_session_in_use(spt)) {
bellardc7f74642004-08-24 21:57:12 +000084 if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
85 if (spt->client_port == tp->udp.uh_sport) {
86 return k;
87 }
88 }
89 }
90 }
91
92 return -1;
93}
94
95static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr,
96 u_int8_t *buf, int len)
97{
98 int fd;
99 int bytes_read = 0;
100
Jan Kiszka93679642009-06-24 14:42:30 +0200101 fd = open(spt->filename, O_RDONLY | O_BINARY);
bellardc7f74642004-08-24 21:57:12 +0000102
103 if (fd < 0) {
104 return -1;
105 }
106
107 if (len) {
108 lseek(fd, block_nr * 512, SEEK_SET);
109
110 bytes_read = read(fd, buf, len);
111 }
112
113 close(fd);
114
115 return bytes_read;
116}
117
ths5fafdf22007-09-16 21:08:06 +0000118static int tftp_send_oack(struct tftp_session *spt,
ths1f697db2007-02-20 00:07:50 +0000119 const char *key, uint32_t value,
120 struct tftp_t *recv_tp)
121{
122 struct sockaddr_in saddr, daddr;
123 struct mbuf *m;
124 struct tftp_t *tp;
125 int n = 0;
126
Jan Kiszka460fec62009-06-24 14:42:31 +0200127 m = m_get(spt->slirp);
ths1f697db2007-02-20 00:07:50 +0000128
129 if (!m)
130 return -1;
131
132 memset(m->m_data, 0, m->m_size);
133
blueswir19634d902007-10-26 19:01:16 +0000134 m->m_data += IF_MAXLINKHDR;
ths1f697db2007-02-20 00:07:50 +0000135 tp = (void *)m->m_data;
136 m->m_data += sizeof(struct udpiphdr);
ths3b46e622007-09-17 08:09:54 +0000137
ths1f697db2007-02-20 00:07:50 +0000138 tp->tp_op = htons(TFTP_OACK);
blueswir1b55266b2008-09-20 08:07:15 +0000139 n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
140 key) + 1;
141 n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
142 value) + 1;
ths1f697db2007-02-20 00:07:50 +0000143
144 saddr.sin_addr = recv_tp->ip.ip_dst;
145 saddr.sin_port = recv_tp->udp.uh_dport;
ths3b46e622007-09-17 08:09:54 +0000146
ths1f697db2007-02-20 00:07:50 +0000147 daddr.sin_addr = spt->client_ip;
148 daddr.sin_port = spt->client_port;
149
ths5fafdf22007-09-16 21:08:06 +0000150 m->m_len = sizeof(struct tftp_t) - 514 + n -
ths1f697db2007-02-20 00:07:50 +0000151 sizeof(struct ip) - sizeof(struct udphdr);
152 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
153
154 return 0;
155}
156
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200157static void tftp_send_error(struct tftp_session *spt,
158 u_int16_t errorcode, const char *msg,
159 struct tftp_t *recv_tp)
bellardc7f74642004-08-24 21:57:12 +0000160{
161 struct sockaddr_in saddr, daddr;
162 struct mbuf *m;
163 struct tftp_t *tp;
164 int nobytes;
165
Jan Kiszka460fec62009-06-24 14:42:31 +0200166 m = m_get(spt->slirp);
bellardc7f74642004-08-24 21:57:12 +0000167
168 if (!m) {
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200169 goto out;
bellardc7f74642004-08-24 21:57:12 +0000170 }
171
172 memset(m->m_data, 0, m->m_size);
173
blueswir19634d902007-10-26 19:01:16 +0000174 m->m_data += IF_MAXLINKHDR;
bellardc7f74642004-08-24 21:57:12 +0000175 tp = (void *)m->m_data;
176 m->m_data += sizeof(struct udpiphdr);
ths3b46e622007-09-17 08:09:54 +0000177
bellardc7f74642004-08-24 21:57:12 +0000178 tp->tp_op = htons(TFTP_ERROR);
179 tp->x.tp_error.tp_error_code = htons(errorcode);
blueswir1b55266b2008-09-20 08:07:15 +0000180 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
bellardc7f74642004-08-24 21:57:12 +0000181
182 saddr.sin_addr = recv_tp->ip.ip_dst;
183 saddr.sin_port = recv_tp->udp.uh_dport;
184
185 daddr.sin_addr = spt->client_ip;
186 daddr.sin_port = spt->client_port;
187
188 nobytes = 2;
189
ths5fafdf22007-09-16 21:08:06 +0000190 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
bellardc7f74642004-08-24 21:57:12 +0000191 sizeof(struct ip) - sizeof(struct udphdr);
192
193 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
194
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200195out:
bellardc7f74642004-08-24 21:57:12 +0000196 tftp_session_terminate(spt);
bellardc7f74642004-08-24 21:57:12 +0000197}
198
ths5fafdf22007-09-16 21:08:06 +0000199static int tftp_send_data(struct tftp_session *spt,
bellardc7f74642004-08-24 21:57:12 +0000200 u_int16_t block_nr,
201 struct tftp_t *recv_tp)
202{
203 struct sockaddr_in saddr, daddr;
204 struct mbuf *m;
205 struct tftp_t *tp;
206 int nobytes;
207
208 if (block_nr < 1) {
209 return -1;
210 }
211
Jan Kiszka460fec62009-06-24 14:42:31 +0200212 m = m_get(spt->slirp);
bellardc7f74642004-08-24 21:57:12 +0000213
214 if (!m) {
215 return -1;
216 }
217
218 memset(m->m_data, 0, m->m_size);
219
blueswir19634d902007-10-26 19:01:16 +0000220 m->m_data += IF_MAXLINKHDR;
bellardc7f74642004-08-24 21:57:12 +0000221 tp = (void *)m->m_data;
222 m->m_data += sizeof(struct udpiphdr);
ths3b46e622007-09-17 08:09:54 +0000223
bellardc7f74642004-08-24 21:57:12 +0000224 tp->tp_op = htons(TFTP_DATA);
225 tp->x.tp_data.tp_block_nr = htons(block_nr);
226
227 saddr.sin_addr = recv_tp->ip.ip_dst;
228 saddr.sin_port = recv_tp->udp.uh_dport;
229
230 daddr.sin_addr = spt->client_ip;
231 daddr.sin_port = spt->client_port;
232
233 nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
234
235 if (nobytes < 0) {
236 m_free(m);
237
238 /* send "file not found" error back */
239
240 tftp_send_error(spt, 1, "File not found", tp);
241
242 return -1;
243 }
244
ths5fafdf22007-09-16 21:08:06 +0000245 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
bellardc7f74642004-08-24 21:57:12 +0000246 sizeof(struct ip) - sizeof(struct udphdr);
247
248 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
249
250 if (nobytes == 512) {
251 tftp_session_update(spt);
252 }
253 else {
254 tftp_session_terminate(spt);
255 }
256
257 return 0;
258}
259
Jan Kiszka460fec62009-06-24 14:42:31 +0200260static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
bellardc7f74642004-08-24 21:57:12 +0000261{
262 struct tftp_session *spt;
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200263 int s, k;
Jan Kiszka93679642009-06-24 14:42:30 +0200264 size_t prefix_len;
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200265 char *req_fname;
bellardc7f74642004-08-24 21:57:12 +0000266
Jan Kiszka460fec62009-06-24 14:42:31 +0200267 s = tftp_session_allocate(slirp, tp);
bellardc7f74642004-08-24 21:57:12 +0000268
269 if (s < 0) {
270 return;
271 }
272
Jan Kiszka460fec62009-06-24 14:42:31 +0200273 spt = &slirp->tftp_sessions[s];
bellardc7f74642004-08-24 21:57:12 +0000274
Jan Kiszkaf8e3cbd2009-06-24 14:42:29 +0200275 /* unspecifed prefix means service disabled */
Jan Kiszka460fec62009-06-24 14:42:31 +0200276 if (!slirp->tftp_prefix) {
Jan Kiszkaf8e3cbd2009-06-24 14:42:29 +0200277 tftp_send_error(spt, 2, "Access violation", tp);
278 return;
279 }
280
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200281 /* skip header fields */
282 k = 0;
283 pktlen -= ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
bellardc7f74642004-08-24 21:57:12 +0000284
Jan Kiszka93679642009-06-24 14:42:30 +0200285 /* prepend tftp_prefix */
Jan Kiszka460fec62009-06-24 14:42:31 +0200286 prefix_len = strlen(slirp->tftp_prefix);
Jan Kiszka74efd612009-06-29 08:47:30 +0200287 spt->filename = qemu_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
Jan Kiszka460fec62009-06-24 14:42:31 +0200288 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
Jan Kiszka74efd612009-06-29 08:47:30 +0200289 spt->filename[prefix_len] = '/';
Jan Kiszka93679642009-06-24 14:42:30 +0200290
bellardc7f74642004-08-24 21:57:12 +0000291 /* get name */
Jan Kiszka74efd612009-06-29 08:47:30 +0200292 req_fname = spt->filename + prefix_len + 1;
bellardc7f74642004-08-24 21:57:12 +0000293
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200294 while (1) {
295 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
296 tftp_send_error(spt, 2, "Access violation", tp);
bellardc7f74642004-08-24 21:57:12 +0000297 return;
298 }
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200299 req_fname[k] = (char)tp->x.tp_buf[k];
300 if (req_fname[k++] == '\0') {
bellardc7f74642004-08-24 21:57:12 +0000301 break;
302 }
303 }
ths3b46e622007-09-17 08:09:54 +0000304
bellardc7f74642004-08-24 21:57:12 +0000305 /* check mode */
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200306 if ((pktlen - k) < 6) {
307 tftp_send_error(spt, 2, "Access violation", tp);
bellardc7f74642004-08-24 21:57:12 +0000308 return;
309 }
ths3b46e622007-09-17 08:09:54 +0000310
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200311 if (memcmp(&tp->x.tp_buf[k], "octet\0", 6) != 0) {
bellardc7f74642004-08-24 21:57:12 +0000312 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
313 return;
314 }
315
ths1f697db2007-02-20 00:07:50 +0000316 k += 6; /* skipping octet */
317
bellardc7f74642004-08-24 21:57:12 +0000318 /* do sanity checks on the filename */
Jan Kiszka74efd612009-06-29 08:47:30 +0200319 if (!strncmp(req_fname, "../", 3) ||
320 req_fname[strlen(req_fname) - 1] == '/' ||
Jan Kiszka93679642009-06-24 14:42:30 +0200321 strstr(req_fname, "/../")) {
bellardc7f74642004-08-24 21:57:12 +0000322 tftp_send_error(spt, 2, "Access violation", tp);
323 return;
324 }
325
bellardc7f74642004-08-24 21:57:12 +0000326 /* check if the file exists */
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200327 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
bellardc7f74642004-08-24 21:57:12 +0000328 tftp_send_error(spt, 1, "File not found", tp);
329 return;
330 }
331
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200332 if (tp->x.tp_buf[pktlen - 1] != 0) {
ths1f697db2007-02-20 00:07:50 +0000333 tftp_send_error(spt, 2, "Access violation", tp);
334 return;
335 }
336
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200337 while (k < pktlen) {
ths1f697db2007-02-20 00:07:50 +0000338 const char *key, *value;
339
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200340 key = (const char *)&tp->x.tp_buf[k];
ths1f697db2007-02-20 00:07:50 +0000341 k += strlen(key) + 1;
342
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200343 if (k >= pktlen) {
ths1f697db2007-02-20 00:07:50 +0000344 tftp_send_error(spt, 2, "Access violation", tp);
345 return;
346 }
347
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200348 value = (const char *)&tp->x.tp_buf[k];
ths1f697db2007-02-20 00:07:50 +0000349 k += strlen(value) + 1;
350
351 if (strcmp(key, "tsize") == 0) {
352 int tsize = atoi(value);
353 struct stat stat_p;
354
Jan Kiszkaf8e3cbd2009-06-24 14:42:29 +0200355 if (tsize == 0) {
Jan Kiszka93679642009-06-24 14:42:30 +0200356 if (stat(spt->filename, &stat_p) == 0)
ths1f697db2007-02-20 00:07:50 +0000357 tsize = stat_p.st_size;
358 else {
359 tftp_send_error(spt, 1, "File not found", tp);
360 return;
361 }
362 }
363
364 tftp_send_oack(spt, "tsize", tsize, tp);
365 }
366 }
367
bellardc7f74642004-08-24 21:57:12 +0000368 tftp_send_data(spt, 1, tp);
369}
370
Jan Kiszka460fec62009-06-24 14:42:31 +0200371static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
bellardc7f74642004-08-24 21:57:12 +0000372{
373 int s;
374
Jan Kiszka460fec62009-06-24 14:42:31 +0200375 s = tftp_session_find(slirp, tp);
bellardc7f74642004-08-24 21:57:12 +0000376
377 if (s < 0) {
378 return;
379 }
380
Jan Kiszka460fec62009-06-24 14:42:31 +0200381 if (tftp_send_data(&slirp->tftp_sessions[s],
ths5fafdf22007-09-16 21:08:06 +0000382 ntohs(tp->x.tp_data.tp_block_nr) + 1,
bellardc7f74642004-08-24 21:57:12 +0000383 tp) < 0) {
384 return;
385 }
386}
387
388void tftp_input(struct mbuf *m)
389{
390 struct tftp_t *tp = (struct tftp_t *)m->m_data;
391
392 switch(ntohs(tp->tp_op)) {
393 case TFTP_RRQ:
Jan Kiszka460fec62009-06-24 14:42:31 +0200394 tftp_handle_rrq(m->slirp, tp, m->m_len);
bellardc7f74642004-08-24 21:57:12 +0000395 break;
396
397 case TFTP_ACK:
Jan Kiszka460fec62009-06-24 14:42:31 +0200398 tftp_handle_ack(m->slirp, tp, m->m_len);
bellardc7f74642004-08-24 21:57:12 +0000399 break;
400 }
401}