blob: a9bc4bb1b65819f10cb5f3029b9c6839e4a840cc [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
Peter Maydell7df74822016-01-29 17:49:59 +000025#include "qemu/osdep.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020026#include "slirp.h"
Jan Kiszka0d62c4c2009-06-24 14:42:29 +020027#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020028#include "qemu/cutils.h"
bellardc7f74642004-08-24 21:57:12 +000029
Jan Kiszka460fec62009-06-24 14:42:31 +020030static inline int tftp_session_in_use(struct tftp_session *spt)
31{
32 return (spt->slirp != NULL);
33}
ths3b46e622007-09-17 08:09:54 +000034
Jan Kiszka460fec62009-06-24 14:42:31 +020035static inline void tftp_session_update(struct tftp_session *spt)
bellardc7f74642004-08-24 21:57:12 +000036{
bellarda3504c82004-08-25 20:55:44 +000037 spt->timestamp = curtime;
bellardc7f74642004-08-24 21:57:12 +000038}
39
40static void tftp_session_terminate(struct tftp_session *spt)
41{
Hervé Poussineau78be0562012-09-10 20:52:25 +020042 if (spt->fd >= 0) {
43 close(spt->fd);
44 spt->fd = -1;
45 }
Anthony Liguori7267c092011-08-20 22:09:37 -050046 g_free(spt->filename);
Jan Kiszka460fec62009-06-24 14:42:31 +020047 spt->slirp = NULL;
bellardc7f74642004-08-24 21:57:12 +000048}
49
Thomas Huthfad7fb92016-03-15 10:31:23 +010050static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
51 struct tftp_t *tp)
bellardc7f74642004-08-24 21:57:12 +000052{
53 struct tftp_session *spt;
bellardc7f74642004-08-24 21:57:12 +000054 int k;
55
bellardc7f74642004-08-24 21:57:12 +000056 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
Jan Kiszka460fec62009-06-24 14:42:31 +020057 spt = &slirp->tftp_sessions[k];
bellardc7f74642004-08-24 21:57:12 +000058
Jan Kiszka460fec62009-06-24 14:42:31 +020059 if (!tftp_session_in_use(spt))
bellarda3504c82004-08-25 20:55:44 +000060 goto found;
bellardc7f74642004-08-24 21:57:12 +000061
62 /* sessions time out after 5 inactive seconds */
Jan Kiszka93679642009-06-24 14:42:30 +020063 if ((int)(curtime - spt->timestamp) > 5000) {
Hervé Poussineau78be0562012-09-10 20:52:25 +020064 tftp_session_terminate(spt);
bellarda3504c82004-08-25 20:55:44 +000065 goto found;
Jan Kiszka93679642009-06-24 14:42:30 +020066 }
bellardc7f74642004-08-24 21:57:12 +000067 }
68
69 return -1;
70
71 found:
72 memset(spt, 0, sizeof(*spt));
Marc-André Lureau17eb5872017-03-23 15:31:56 +040073 memcpy(&spt->client_addr, srcsas, sockaddr_size(srcsas));
Hervé Poussineau78be0562012-09-10 20:52:25 +020074 spt->fd = -1;
Hervé Poussineau94435982016-11-21 20:45:49 +010075 spt->block_size = 512;
bellardc7f74642004-08-24 21:57:12 +000076 spt->client_port = tp->udp.uh_sport;
Jan Kiszka460fec62009-06-24 14:42:31 +020077 spt->slirp = slirp;
bellardc7f74642004-08-24 21:57:12 +000078
79 tftp_session_update(spt);
80
81 return k;
82}
83
Thomas Huthfad7fb92016-03-15 10:31:23 +010084static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
85 struct tftp_t *tp)
bellardc7f74642004-08-24 21:57:12 +000086{
87 struct tftp_session *spt;
88 int k;
89
90 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
Jan Kiszka460fec62009-06-24 14:42:31 +020091 spt = &slirp->tftp_sessions[k];
bellardc7f74642004-08-24 21:57:12 +000092
Jan Kiszka460fec62009-06-24 14:42:31 +020093 if (tftp_session_in_use(spt)) {
Thomas Huthfad7fb92016-03-15 10:31:23 +010094 if (sockaddr_equal(&spt->client_addr, srcsas)) {
bellardc7f74642004-08-24 21:57:12 +000095 if (spt->client_port == tp->udp.uh_sport) {
96 return k;
97 }
98 }
99 }
100 }
101
102 return -1;
103}
104
Hervé Poussineau4aa401f2012-09-13 12:39:36 +0200105static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
Stefan Weilb6dce922010-07-22 22:15:23 +0200106 uint8_t *buf, int len)
bellardc7f74642004-08-24 21:57:12 +0000107{
Hervé Poussineau78be0562012-09-10 20:52:25 +0200108 int bytes_read = 0;
bellardc7f74642004-08-24 21:57:12 +0000109
Hervé Poussineau78be0562012-09-10 20:52:25 +0200110 if (spt->fd < 0) {
111 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
112 }
bellardc7f74642004-08-24 21:57:12 +0000113
Hervé Poussineau78be0562012-09-10 20:52:25 +0200114 if (spt->fd < 0) {
115 return -1;
116 }
bellardc7f74642004-08-24 21:57:12 +0000117
Hervé Poussineau78be0562012-09-10 20:52:25 +0200118 if (len) {
Hervé Poussineau94435982016-11-21 20:45:49 +0100119 lseek(spt->fd, block_nr * spt->block_size, SEEK_SET);
bellardc7f74642004-08-24 21:57:12 +0000120
Hervé Poussineau78be0562012-09-10 20:52:25 +0200121 bytes_read = read(spt->fd, buf, len);
122 }
bellardc7f74642004-08-24 21:57:12 +0000123
Hervé Poussineau78be0562012-09-10 20:52:25 +0200124 return bytes_read;
bellardc7f74642004-08-24 21:57:12 +0000125}
126
Thomas Huthfad7fb92016-03-15 10:31:23 +0100127static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
128 struct mbuf *m)
129{
130 struct tftp_t *tp;
131
132 memset(m->m_data, 0, m->m_size);
133
134 m->m_data += IF_MAXLINKHDR;
135 if (spt->client_addr.ss_family == AF_INET6) {
136 m->m_data += sizeof(struct ip6);
137 } else {
138 m->m_data += sizeof(struct ip);
139 }
140 tp = (void *)m->m_data;
141 m->m_data += sizeof(struct udphdr);
142
143 return tp;
144}
145
146static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
147 struct tftp_t *recv_tp)
148{
149 if (spt->client_addr.ss_family == AF_INET6) {
150 struct sockaddr_in6 sa6, da6;
151
152 sa6.sin6_addr = spt->slirp->vhost_addr6;
153 sa6.sin6_port = recv_tp->udp.uh_dport;
154 da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
155 da6.sin6_port = spt->client_port;
156
157 udp6_output(NULL, m, &sa6, &da6);
158 } else {
159 struct sockaddr_in sa4, da4;
160
161 sa4.sin_addr = spt->slirp->vhost_addr;
162 sa4.sin_port = recv_tp->udp.uh_dport;
163 da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
164 da4.sin_port = spt->client_port;
165
166 udp_output(NULL, m, &sa4, &da4, IPTOS_LOWDELAY);
167 }
168}
169
ths5fafdf22007-09-16 21:08:06 +0000170static int tftp_send_oack(struct tftp_session *spt,
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200171 const char *keys[], uint32_t values[], int nb,
ths1f697db2007-02-20 00:07:50 +0000172 struct tftp_t *recv_tp)
173{
ths1f697db2007-02-20 00:07:50 +0000174 struct mbuf *m;
175 struct tftp_t *tp;
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200176 int i, n = 0;
ths1f697db2007-02-20 00:07:50 +0000177
Jan Kiszka460fec62009-06-24 14:42:31 +0200178 m = m_get(spt->slirp);
ths1f697db2007-02-20 00:07:50 +0000179
180 if (!m)
Thomas Huthfad7fb92016-03-15 10:31:23 +0100181 return -1;
ths1f697db2007-02-20 00:07:50 +0000182
Thomas Huthfad7fb92016-03-15 10:31:23 +0100183 tp = tftp_prep_mbuf_data(spt, m);
ths3b46e622007-09-17 08:09:54 +0000184
ths1f697db2007-02-20 00:07:50 +0000185 tp->tp_op = htons(TFTP_OACK);
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200186 for (i = 0; i < nb; i++) {
187 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
188 keys[i]) + 1;
189 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
190 values[i]) + 1;
191 }
ths1f697db2007-02-20 00:07:50 +0000192
Hervé Poussineau94435982016-11-21 20:45:49 +0100193 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + n
194 - sizeof(struct udphdr);
Thomas Huthfad7fb92016-03-15 10:31:23 +0100195 tftp_udp_output(spt, m, recv_tp);
ths1f697db2007-02-20 00:07:50 +0000196
197 return 0;
198}
199
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200200static void tftp_send_error(struct tftp_session *spt,
Stefan Weilb6dce922010-07-22 22:15:23 +0200201 uint16_t errorcode, const char *msg,
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200202 struct tftp_t *recv_tp)
bellardc7f74642004-08-24 21:57:12 +0000203{
bellardc7f74642004-08-24 21:57:12 +0000204 struct mbuf *m;
205 struct tftp_t *tp;
bellardc7f74642004-08-24 21:57:12 +0000206
Jan Kiszka460fec62009-06-24 14:42:31 +0200207 m = m_get(spt->slirp);
bellardc7f74642004-08-24 21:57:12 +0000208
209 if (!m) {
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200210 goto out;
bellardc7f74642004-08-24 21:57:12 +0000211 }
212
Thomas Huthfad7fb92016-03-15 10:31:23 +0100213 tp = tftp_prep_mbuf_data(spt, m);
ths3b46e622007-09-17 08:09:54 +0000214
bellardc7f74642004-08-24 21:57:12 +0000215 tp->tp_op = htons(TFTP_ERROR);
216 tp->x.tp_error.tp_error_code = htons(errorcode);
blueswir1b55266b2008-09-20 08:07:15 +0000217 pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
bellardc7f74642004-08-24 21:57:12 +0000218
Hervé Poussineau94435982016-11-21 20:45:49 +0100219 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + 3 + strlen(msg)
Thomas Huthfad7fb92016-03-15 10:31:23 +0100220 - sizeof(struct udphdr);
221 tftp_udp_output(spt, m, recv_tp);
bellardc7f74642004-08-24 21:57:12 +0000222
Jan Kiszkaef2d54d2009-06-24 14:42:30 +0200223out:
bellardc7f74642004-08-24 21:57:12 +0000224 tftp_session_terminate(spt);
bellardc7f74642004-08-24 21:57:12 +0000225}
226
Jan Kiszkaeb7faf02012-09-13 12:44:27 +0200227static void tftp_send_next_block(struct tftp_session *spt,
228 struct tftp_t *recv_tp)
bellardc7f74642004-08-24 21:57:12 +0000229{
bellardc7f74642004-08-24 21:57:12 +0000230 struct mbuf *m;
231 struct tftp_t *tp;
232 int nobytes;
233
Jan Kiszka460fec62009-06-24 14:42:31 +0200234 m = m_get(spt->slirp);
bellardc7f74642004-08-24 21:57:12 +0000235
236 if (!m) {
Jan Kiszkaeb7faf02012-09-13 12:44:27 +0200237 return;
bellardc7f74642004-08-24 21:57:12 +0000238 }
239
Thomas Huthfad7fb92016-03-15 10:31:23 +0100240 tp = tftp_prep_mbuf_data(spt, m);
ths3b46e622007-09-17 08:09:54 +0000241
bellardc7f74642004-08-24 21:57:12 +0000242 tp->tp_op = htons(TFTP_DATA);
Hervé Poussineau4aa401f2012-09-13 12:39:36 +0200243 tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
bellardc7f74642004-08-24 21:57:12 +0000244
Hervé Poussineau94435982016-11-21 20:45:49 +0100245 nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf,
246 spt->block_size);
bellardc7f74642004-08-24 21:57:12 +0000247
248 if (nobytes < 0) {
249 m_free(m);
250
251 /* send "file not found" error back */
252
253 tftp_send_error(spt, 1, "File not found", tp);
254
Jan Kiszkaeb7faf02012-09-13 12:44:27 +0200255 return;
bellardc7f74642004-08-24 21:57:12 +0000256 }
257
Hervé Poussineau94435982016-11-21 20:45:49 +0100258 m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX - nobytes)
259 - sizeof(struct udphdr);
Thomas Huthfad7fb92016-03-15 10:31:23 +0100260 tftp_udp_output(spt, m, recv_tp);
bellardc7f74642004-08-24 21:57:12 +0000261
Hervé Poussineau94435982016-11-21 20:45:49 +0100262 if (nobytes == spt->block_size) {
bellardc7f74642004-08-24 21:57:12 +0000263 tftp_session_update(spt);
264 }
265 else {
266 tftp_session_terminate(spt);
267 }
268
Hervé Poussineau4aa401f2012-09-13 12:39:36 +0200269 spt->block_nr++;
bellardc7f74642004-08-24 21:57:12 +0000270}
271
Thomas Huthfad7fb92016-03-15 10:31:23 +0100272static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
273 struct tftp_t *tp, int pktlen)
bellardc7f74642004-08-24 21:57:12 +0000274{
275 struct tftp_session *spt;
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200276 int s, k;
Jan Kiszka93679642009-06-24 14:42:30 +0200277 size_t prefix_len;
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200278 char *req_fname;
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200279 const char *option_name[2];
280 uint32_t option_value[2];
281 int nb_options = 0;
bellardc7f74642004-08-24 21:57:12 +0000282
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000283 /* check if a session already exists and if so terminate it */
Thomas Huthfad7fb92016-03-15 10:31:23 +0100284 s = tftp_session_find(slirp, srcsas, tp);
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000285 if (s >= 0) {
286 tftp_session_terminate(&slirp->tftp_sessions[s]);
287 }
288
Thomas Huthfad7fb92016-03-15 10:31:23 +0100289 s = tftp_session_allocate(slirp, srcsas, tp);
bellardc7f74642004-08-24 21:57:12 +0000290
291 if (s < 0) {
292 return;
293 }
294
Jan Kiszka460fec62009-06-24 14:42:31 +0200295 spt = &slirp->tftp_sessions[s];
bellardc7f74642004-08-24 21:57:12 +0000296
Deepak Kathayatdc6fb732014-03-24 16:30:17 +0800297 /* unspecified prefix means service disabled */
Jan Kiszka460fec62009-06-24 14:42:31 +0200298 if (!slirp->tftp_prefix) {
Jan Kiszkaf8e3cbd2009-06-24 14:42:29 +0200299 tftp_send_error(spt, 2, "Access violation", tp);
300 return;
301 }
302
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200303 /* skip header fields */
304 k = 0;
Stefan Weil89d2d3af2011-02-23 19:40:14 +0100305 pktlen -= offsetof(struct tftp_t, x.tp_buf);
bellardc7f74642004-08-24 21:57:12 +0000306
Jan Kiszka93679642009-06-24 14:42:30 +0200307 /* prepend tftp_prefix */
Jan Kiszka460fec62009-06-24 14:42:31 +0200308 prefix_len = strlen(slirp->tftp_prefix);
Anthony Liguori7267c092011-08-20 22:09:37 -0500309 spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
Jan Kiszka460fec62009-06-24 14:42:31 +0200310 memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
Jan Kiszka74efd612009-06-29 08:47:30 +0200311 spt->filename[prefix_len] = '/';
Jan Kiszka93679642009-06-24 14:42:30 +0200312
bellardc7f74642004-08-24 21:57:12 +0000313 /* get name */
Jan Kiszka74efd612009-06-29 08:47:30 +0200314 req_fname = spt->filename + prefix_len + 1;
bellardc7f74642004-08-24 21:57:12 +0000315
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200316 while (1) {
317 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
318 tftp_send_error(spt, 2, "Access violation", tp);
bellardc7f74642004-08-24 21:57:12 +0000319 return;
320 }
Stefan Weil89d2d3af2011-02-23 19:40:14 +0100321 req_fname[k] = tp->x.tp_buf[k];
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200322 if (req_fname[k++] == '\0') {
bellardc7f74642004-08-24 21:57:12 +0000323 break;
324 }
325 }
ths3b46e622007-09-17 08:09:54 +0000326
bellardc7f74642004-08-24 21:57:12 +0000327 /* check mode */
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200328 if ((pktlen - k) < 6) {
329 tftp_send_error(spt, 2, "Access violation", tp);
bellardc7f74642004-08-24 21:57:12 +0000330 return;
331 }
ths3b46e622007-09-17 08:09:54 +0000332
Stefan Weil89d2d3af2011-02-23 19:40:14 +0100333 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
bellardc7f74642004-08-24 21:57:12 +0000334 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
335 return;
336 }
337
ths1f697db2007-02-20 00:07:50 +0000338 k += 6; /* skipping octet */
339
bellardc7f74642004-08-24 21:57:12 +0000340 /* do sanity checks on the filename */
Jan Kiszka74efd612009-06-29 08:47:30 +0200341 if (!strncmp(req_fname, "../", 3) ||
342 req_fname[strlen(req_fname) - 1] == '/' ||
Jan Kiszka93679642009-06-24 14:42:30 +0200343 strstr(req_fname, "/../")) {
bellardc7f74642004-08-24 21:57:12 +0000344 tftp_send_error(spt, 2, "Access violation", tp);
345 return;
346 }
347
bellardc7f74642004-08-24 21:57:12 +0000348 /* check if the file exists */
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200349 if (tftp_read_data(spt, 0, NULL, 0) < 0) {
bellardc7f74642004-08-24 21:57:12 +0000350 tftp_send_error(spt, 1, "File not found", tp);
351 return;
352 }
353
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200354 if (tp->x.tp_buf[pktlen - 1] != 0) {
ths1f697db2007-02-20 00:07:50 +0000355 tftp_send_error(spt, 2, "Access violation", tp);
356 return;
357 }
358
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200359 while (k < pktlen && nb_options < ARRAY_SIZE(option_name)) {
ths1f697db2007-02-20 00:07:50 +0000360 const char *key, *value;
361
Stefan Weil89d2d3af2011-02-23 19:40:14 +0100362 key = &tp->x.tp_buf[k];
ths1f697db2007-02-20 00:07:50 +0000363 k += strlen(key) + 1;
364
Jan Kiszka20c24bf2009-06-24 14:42:30 +0200365 if (k >= pktlen) {
ths1f697db2007-02-20 00:07:50 +0000366 tftp_send_error(spt, 2, "Access violation", tp);
367 return;
368 }
369
Stefan Weil89d2d3af2011-02-23 19:40:14 +0100370 value = &tp->x.tp_buf[k];
ths1f697db2007-02-20 00:07:50 +0000371 k += strlen(value) + 1;
372
Sergei Gavrikovfacf1a62011-01-12 15:57:18 +0200373 if (strcasecmp(key, "tsize") == 0) {
ths1f697db2007-02-20 00:07:50 +0000374 int tsize = atoi(value);
375 struct stat stat_p;
376
Jan Kiszkaf8e3cbd2009-06-24 14:42:29 +0200377 if (tsize == 0) {
Jan Kiszka93679642009-06-24 14:42:30 +0200378 if (stat(spt->filename, &stat_p) == 0)
ths1f697db2007-02-20 00:07:50 +0000379 tsize = stat_p.st_size;
380 else {
381 tftp_send_error(spt, 1, "File not found", tp);
382 return;
383 }
384 }
385
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200386 option_name[nb_options] = "tsize";
387 option_value[nb_options] = tsize;
388 nb_options++;
389 } else if (strcasecmp(key, "blksize") == 0) {
390 int blksize = atoi(value);
391
Hervé Poussineau94435982016-11-21 20:45:49 +0100392 /* Accept blksize up to our maximum size */
393 if (blksize > 0) {
394 spt->block_size = MIN(blksize, TFTP_BLOCKSIZE_MAX);
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200395 option_name[nb_options] = "blksize";
Hervé Poussineau94435982016-11-21 20:45:49 +0100396 option_value[nb_options] = spt->block_size;
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200397 nb_options++;
398 }
ths1f697db2007-02-20 00:07:50 +0000399 }
400 }
401
Hervé Poussineau95b1ad72012-09-13 07:55:01 +0200402 if (nb_options > 0) {
403 assert(nb_options <= ARRAY_SIZE(option_name));
404 tftp_send_oack(spt, option_name, option_value, nb_options, tp);
405 return;
406 }
407
Hervé Poussineau4aa401f2012-09-13 12:39:36 +0200408 spt->block_nr = 0;
409 tftp_send_next_block(spt, tp);
bellardc7f74642004-08-24 21:57:12 +0000410}
411
Thomas Huthfad7fb92016-03-15 10:31:23 +0100412static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
413 struct tftp_t *tp, int pktlen)
bellardc7f74642004-08-24 21:57:12 +0000414{
415 int s;
416
Thomas Huthfad7fb92016-03-15 10:31:23 +0100417 s = tftp_session_find(slirp, srcsas, tp);
bellardc7f74642004-08-24 21:57:12 +0000418
419 if (s < 0) {
420 return;
421 }
422
Jan Kiszkaeb7faf02012-09-13 12:44:27 +0200423 tftp_send_next_block(&slirp->tftp_sessions[s], tp);
bellardc7f74642004-08-24 21:57:12 +0000424}
425
Thomas Huthfad7fb92016-03-15 10:31:23 +0100426static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
427 struct tftp_t *tp, int pktlen)
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000428{
429 int s;
430
Thomas Huthfad7fb92016-03-15 10:31:23 +0100431 s = tftp_session_find(slirp, srcsas, tp);
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000432
433 if (s < 0) {
434 return;
435 }
436
437 tftp_session_terminate(&slirp->tftp_sessions[s]);
438}
439
Thomas Huthfad7fb92016-03-15 10:31:23 +0100440void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
bellardc7f74642004-08-24 21:57:12 +0000441{
442 struct tftp_t *tp = (struct tftp_t *)m->m_data;
443
444 switch(ntohs(tp->tp_op)) {
445 case TFTP_RRQ:
Thomas Huthfad7fb92016-03-15 10:31:23 +0100446 tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
bellardc7f74642004-08-24 21:57:12 +0000447 break;
448
449 case TFTP_ACK:
Thomas Huthfad7fb92016-03-15 10:31:23 +0100450 tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
bellardc7f74642004-08-24 21:57:12 +0000451 break;
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000452
453 case TFTP_ERROR:
Thomas Huthfad7fb92016-03-15 10:31:23 +0100454 tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
Thomas Horstenbfe4e172010-01-07 17:01:28 +0000455 break;
bellardc7f74642004-08-24 21:57:12 +0000456 }
457}