2 * tftp.c - a simple, read-only tftp server for qemu
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
25 #include "qemu/osdep.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)
45 g_free(spt->filename);
49 static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
51 struct tftp_session *spt;
54 for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
55 spt = &slirp->tftp_sessions[k];
57 if (!tftp_session_in_use(spt))
60 /* sessions time out after 5 inactive seconds */
61 if ((int)(curtime - spt->timestamp) > 5000) {
62 tftp_session_terminate(spt);
70 memset(spt, 0, sizeof(*spt));
71 memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
73 spt->client_port = tp->udp.uh_sport;
76 tftp_session_update(spt);
81 static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
83 struct tftp_session *spt;
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) {
101 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
102 uint8_t *buf, int len)
107 spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
115 lseek(spt->fd, block_nr * 512, SEEK_SET);
117 bytes_read = read(spt->fd, buf, len);
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;
132 m = m_get(spt->slirp);
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",
147 n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
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);
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;
172 m = m_get(spt->slirp);
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);
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;
211 m = m_get(spt->slirp);
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);
237 /* send "file not found" error back */
239 tftp_send_error(spt, 1, "File not found", tp);
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);
253 tftp_session_terminate(spt);
259 static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
261 struct tftp_session *spt;
265 const char *option_name[2];
266 uint32_t option_value[2];
269 /* check if a session already exists and if so terminate it */
270 s = tftp_session_find(slirp, tp);
272 tftp_session_terminate(&slirp->tftp_sessions[s]);
275 s = tftp_session_allocate(slirp, tp);
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);
289 /* skip header fields */
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] = '/';
300 req_fname = spt->filename + prefix_len + 1;
303 if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
304 tftp_send_error(spt, 2, "Access violation", tp);
307 req_fname[k] = tp->x.tp_buf[k];
308 if (req_fname[k++] == '\0') {
314 if ((pktlen - k) < 6) {
315 tftp_send_error(spt, 2, "Access violation", tp);
319 if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
320 tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
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);
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);
340 if (tp->x.tp_buf[pktlen - 1] != 0) {
341 tftp_send_error(spt, 2, "Access violation", tp);
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;
352 tftp_send_error(spt, 2, "Access violation", tp);
356 value = &tp->x.tp_buf[k];
357 k += strlen(value) + 1;
359 if (strcasecmp(key, "tsize") == 0) {
360 int tsize = atoi(value);
364 if (stat(spt->filename, &stat_p) == 0)
365 tsize = stat_p.st_size;
367 tftp_send_error(spt, 1, "File not found", tp);
372 option_name[nb_options] = "tsize";
373 option_value[nb_options] = tsize;
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;
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);
397 tftp_send_next_block(spt, tp);
400 static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
404 s = tftp_session_find(slirp, tp);
410 tftp_send_next_block(&slirp->tftp_sessions[s], tp);
413 static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
417 s = tftp_session_find(slirp, tp);
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)) {
432 tftp_handle_rrq(m->slirp, tp, m->m_len);
436 tftp_handle_ack(m->slirp, tp, m->m_len);
440 tftp_handle_error(m->slirp, tp, m->m_len);