blob: b78765f3af1b0febd7feea6a98f422b6b9207b81 [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{
Anthony Liguori7267c092011-08-20 22:09:37 -050040 g_free(spt->filename);
Jan Kiszka460fec62009-06-24 14:42:31 +020041 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) {
Anthony Liguori7267c092011-08-20 22:09:37 -050057 g_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
Stefan Weilb6dce922010-07-22 22:15:23 +020095static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
96 uint8_t *buf, int len)
bellardc7f74642004-08-24 21:57:12 +000097{
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);
Stefan Weil89d2d3a2011-02-23 19:40:14 +0100139 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
blueswir1b55266b2008-09-20 08:07:15 +0000140 key) + 1;
Stefan Weil89d2d3a2011-02-23 19:40:14 +0100141 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
blueswir1b55266b2008-09-20 08:07:15 +0000142 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,
Stefan Weilb6dce922010-07-22 22:15:23 +0200158 uint16_t errorcode, const char *msg,
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200159 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;
bellardc7f74642004-08-24 21:57:12 +0000164
Jan Kiszka460fec62009-06-24 14:42:31 +0200165 m = m_get(spt->slirp);
bellardc7f74642004-08-24 21:57:12 +0000166
167 if (!m) {
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200168 goto out;
bellardc7f74642004-08-24 21:57:12 +0000169 }
170
171 memset(m->m_data, 0, m->m_size);
172
blueswir19634d902007-10-26 19:01:16 +0000173 m->m_data += IF_MAXLINKHDR;
bellardc7f74642004-08-24 21:57:12 +0000174 tp = (void *)m->m_data;
175 m->m_data += sizeof(struct udpiphdr);
ths3b46e622007-09-17 08:09:54 +0000176
bellardc7f74642004-08-24 21:57:12 +0000177 tp->tp_op = htons(TFTP_ERROR);
178 tp->x.tp_error.tp_error_code = htons(errorcode);
blueswir1b55266b2008-09-20 08:07:15 +0000179 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
bellardc7f74642004-08-24 21:57:12 +0000180
181 saddr.sin_addr = recv_tp->ip.ip_dst;
182 saddr.sin_port = recv_tp->udp.uh_dport;
183
184 daddr.sin_addr = spt->client_ip;
185 daddr.sin_port = spt->client_port;
186
ths5fafdf22007-09-16 21:08:06 +0000187 m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
bellardc7f74642004-08-24 21:57:12 +0000188 sizeof(struct ip) - sizeof(struct udphdr);
189
190 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
191
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200192out:
bellardc7f74642004-08-24 21:57:12 +0000193 tftp_session_terminate(spt);
bellardc7f74642004-08-24 21:57:12 +0000194}
195
ths5fafdf22007-09-16 21:08:06 +0000196static int tftp_send_data(struct tftp_session *spt,
Stefan Weilb6dce922010-07-22 22:15:23 +0200197 uint16_t block_nr,
bellardc7f74642004-08-24 21:57:12 +0000198 struct tftp_t *recv_tp)
199{
200 struct sockaddr_in saddr, daddr;
201 struct mbuf *m;
202 struct tftp_t *tp;
203 int nobytes;
204
205 if (block_nr < 1) {
206 return -1;
207 }
208
Jan Kiszka460fec62009-06-24 14:42:31 +0200209 m = m_get(spt->slirp);
bellardc7f74642004-08-24 21:57:12 +0000210
211 if (!m) {
212 return -1;
213 }
214
215 memset(m->m_data, 0, m->m_size);
216
blueswir19634d902007-10-26 19:01:16 +0000217 m->m_data += IF_MAXLINKHDR;
bellardc7f74642004-08-24 21:57:12 +0000218 tp = (void *)m->m_data;
219 m->m_data += sizeof(struct udpiphdr);
ths3b46e622007-09-17 08:09:54 +0000220
bellardc7f74642004-08-24 21:57:12 +0000221 tp->tp_op = htons(TFTP_DATA);
222 tp->x.tp_data.tp_block_nr = htons(block_nr);
223
224 saddr.sin_addr = recv_tp->ip.ip_dst;
225 saddr.sin_port = recv_tp->udp.uh_dport;
226
227 daddr.sin_addr = spt->client_ip;
228 daddr.sin_port = spt->client_port;
229
230 nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
231
232 if (nobytes < 0) {
233 m_free(m);
234
235 /* send "file not found" error back */
236
237 tftp_send_error(spt, 1, "File not found", tp);
238
239 return -1;
240 }
241
ths5fafdf22007-09-16 21:08:06 +0000242 m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
bellardc7f74642004-08-24 21:57:12 +0000243 sizeof(struct ip) - sizeof(struct udphdr);
244
245 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
246
247 if (nobytes == 512) {
248 tftp_session_update(spt);
249 }
250 else {
251 tftp_session_terminate(spt);
252 }
253
254 return 0;
255}
256
Jan Kiszka460fec62009-06-24 14:42:31 +0200257static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
bellardc7f74642004-08-24 21:57:12 +0000258{
259 struct tftp_session *spt;
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200260 int s, k;
Jan Kiszka93679642009-06-24 14:42:30 +0200261 size_t prefix_len;
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200262 char *req_fname;
bellardc7f74642004-08-24 21:57:12 +0000263
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000264 /* check if a session already exists and if so terminate it */
265 s = tftp_session_find(slirp, tp);
266 if (s >= 0) {
267 tftp_session_terminate(&slirp->tftp_sessions[s]);
268 }
269
Jan Kiszka460fec62009-06-24 14:42:31 +0200270 s = tftp_session_allocate(slirp, tp);
bellardc7f74642004-08-24 21:57:12 +0000271
272 if (s < 0) {
273 return;
274 }
275
Jan Kiszka460fec62009-06-24 14:42:31 +0200276 spt = &slirp->tftp_sessions[s];
bellardc7f74642004-08-24 21:57:12 +0000277
Jan Kiszkaf8e3cbd2009-06-24 14:42:29 +0200278 /* unspecifed prefix means service disabled */
Jan Kiszka460fec62009-06-24 14:42:31 +0200279 if (!slirp->tftp_prefix) {
Jan Kiszkaf8e3cbd2009-06-24 14:42:29 +0200280 tftp_send_error(spt, 2, "Access violation", tp);
281 return;
282 }
283
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200284 /* skip header fields */
285 k = 0;
Stefan Weil89d2d3a2011-02-23 19:40:14 +0100286 pktlen -= offsetof(struct tftp_t, x.tp_buf);
bellardc7f74642004-08-24 21:57:12 +0000287
Jan Kiszka93679642009-06-24 14:42:30 +0200288 /* prepend tftp_prefix */
Jan Kiszka460fec62009-06-24 14:42:31 +0200289 prefix_len = strlen(slirp->tftp_prefix);
Anthony Liguori7267c092011-08-20 22:09:37 -0500290 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
Jan Kiszka460fec62009-06-24 14:42:31 +0200291 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
Jan Kiszka74efd612009-06-29 08:47:30 +0200292 spt->filename[prefix_len] = '/';
Jan Kiszka93679642009-06-24 14:42:30 +0200293
bellardc7f74642004-08-24 21:57:12 +0000294 /* get name */
Jan Kiszka74efd612009-06-29 08:47:30 +0200295 req_fname = spt->filename + prefix_len + 1;
bellardc7f74642004-08-24 21:57:12 +0000296
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200297 while (1) {
298 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
299 tftp_send_error(spt, 2, "Access violation", tp);
bellardc7f74642004-08-24 21:57:12 +0000300 return;
301 }
Stefan Weil89d2d3a2011-02-23 19:40:14 +0100302 req_fname[k] = tp->x.tp_buf[k];
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200303 if (req_fname[k++] == '\0') {
bellardc7f74642004-08-24 21:57:12 +0000304 break;
305 }
306 }
ths3b46e622007-09-17 08:09:54 +0000307
bellardc7f74642004-08-24 21:57:12 +0000308 /* check mode */
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200309 if ((pktlen - k) < 6) {
310 tftp_send_error(spt, 2, "Access violation", tp);
bellardc7f74642004-08-24 21:57:12 +0000311 return;
312 }
ths3b46e622007-09-17 08:09:54 +0000313
Stefan Weil89d2d3a2011-02-23 19:40:14 +0100314 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
bellardc7f74642004-08-24 21:57:12 +0000315 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
316 return;
317 }
318
ths1f697db2007-02-20 00:07:50 +0000319 k += 6; /* skipping octet */
320
bellardc7f74642004-08-24 21:57:12 +0000321 /* do sanity checks on the filename */
Jan Kiszka74efd612009-06-29 08:47:30 +0200322 if (!strncmp(req_fname, "../", 3) ||
323 req_fname[strlen(req_fname) - 1] == '/' ||
Jan Kiszka93679642009-06-24 14:42:30 +0200324 strstr(req_fname, "/../")) {
bellardc7f74642004-08-24 21:57:12 +0000325 tftp_send_error(spt, 2, "Access violation", tp);
326 return;
327 }
328
bellardc7f74642004-08-24 21:57:12 +0000329 /* check if the file exists */
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200330 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
bellardc7f74642004-08-24 21:57:12 +0000331 tftp_send_error(spt, 1, "File not found", tp);
332 return;
333 }
334
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200335 if (tp->x.tp_buf[pktlen - 1] != 0) {
ths1f697db2007-02-20 00:07:50 +0000336 tftp_send_error(spt, 2, "Access violation", tp);
337 return;
338 }
339
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200340 while (k < pktlen) {
ths1f697db2007-02-20 00:07:50 +0000341 const char *key, *value;
342
Stefan Weil89d2d3a2011-02-23 19:40:14 +0100343 key = &tp->x.tp_buf[k];
ths1f697db2007-02-20 00:07:50 +0000344 k += strlen(key) + 1;
345
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200346 if (k >= pktlen) {
ths1f697db2007-02-20 00:07:50 +0000347 tftp_send_error(spt, 2, "Access violation", tp);
348 return;
349 }
350
Stefan Weil89d2d3a2011-02-23 19:40:14 +0100351 value = &tp->x.tp_buf[k];
ths1f697db2007-02-20 00:07:50 +0000352 k += strlen(value) + 1;
353
Sergei Gavrikovfacf1a62011-01-12 15:57:18 +0200354 if (strcasecmp(key, "tsize") == 0) {
ths1f697db2007-02-20 00:07:50 +0000355 int tsize = atoi(value);
356 struct stat stat_p;
357
Jan Kiszkaf8e3cbd2009-06-24 14:42:29 +0200358 if (tsize == 0) {
Jan Kiszka93679642009-06-24 14:42:30 +0200359 if (stat(spt->filename, &stat_p) == 0)
ths1f697db2007-02-20 00:07:50 +0000360 tsize = stat_p.st_size;
361 else {
362 tftp_send_error(spt, 1, "File not found", tp);
363 return;
364 }
365 }
366
367 tftp_send_oack(spt, "tsize", tsize, tp);
Milan Plzik1cb1a662010-01-07 13:39:43 +0100368 return;
ths1f697db2007-02-20 00:07:50 +0000369 }
370 }
371
bellardc7f74642004-08-24 21:57:12 +0000372 tftp_send_data(spt, 1, tp);
373}
374
Jan Kiszka460fec62009-06-24 14:42:31 +0200375static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
bellardc7f74642004-08-24 21:57:12 +0000376{
377 int s;
378
Jan Kiszka460fec62009-06-24 14:42:31 +0200379 s = tftp_session_find(slirp, tp);
bellardc7f74642004-08-24 21:57:12 +0000380
381 if (s < 0) {
382 return;
383 }
384
Jan Kiszka460fec62009-06-24 14:42:31 +0200385 if (tftp_send_data(&slirp->tftp_sessions[s],
ths5fafdf22007-09-16 21:08:06 +0000386 ntohs(tp->x.tp_data.tp_block_nr) + 1,
bellardc7f74642004-08-24 21:57:12 +0000387 tp) < 0) {
388 return;
389 }
390}
391
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000392static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
393{
394 int s;
395
396 s = tftp_session_find(slirp, tp);
397
398 if (s < 0) {
399 return;
400 }
401
402 tftp_session_terminate(&slirp->tftp_sessions[s]);
403}
404
bellardc7f74642004-08-24 21:57:12 +0000405void tftp_input(struct mbuf *m)
406{
407 struct tftp_t *tp = (struct tftp_t *)m->m_data;
408
409 switch(ntohs(tp->tp_op)) {
410 case TFTP_RRQ:
Jan Kiszka460fec62009-06-24 14:42:31 +0200411 tftp_handle_rrq(m->slirp, tp, m->m_len);
bellardc7f74642004-08-24 21:57:12 +0000412 break;
413
414 case TFTP_ACK:
Jan Kiszka460fec62009-06-24 14:42:31 +0200415 tftp_handle_ack(m->slirp, tp, m->m_len);
bellardc7f74642004-08-24 21:57:12 +0000416 break;
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000417
418 case TFTP_ERROR:
419 tftp_handle_error(m->slirp, tp, m->m_len);
420 break;
bellardc7f74642004-08-24 21:57:12 +0000421 }
422}