2004-08-24 23:57:12 +02:00
|
|
|
/*
|
|
|
|
* tftp.c - a simple, read-only tftp server for qemu
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-08-24 23:57:12 +02:00
|
|
|
* Copyright (c) 2004 Magnus Damm <damm@opensource.se>
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-08-24 23:57:12 +02:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 18:49:59 +01:00
|
|
|
#include "qemu/osdep.h"
|
2016-06-22 19:11:19 +02:00
|
|
|
#include "slirp.h"
|
2009-06-24 14:42:29 +02:00
|
|
|
#include "qemu-common.h"
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/cutils.h"
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
static inline int tftp_session_in_use(struct tftp_session *spt)
|
|
|
|
{
|
|
|
|
return (spt->slirp != NULL);
|
|
|
|
}
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
static inline void tftp_session_update(struct tftp_session *spt)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
2004-08-25 22:55:44 +02:00
|
|
|
spt->timestamp = curtime;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tftp_session_terminate(struct tftp_session *spt)
|
|
|
|
{
|
2012-09-10 20:52:25 +02:00
|
|
|
if (spt->fd >= 0) {
|
|
|
|
close(spt->fd);
|
|
|
|
spt->fd = -1;
|
|
|
|
}
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(spt->filename);
|
2009-06-24 14:42:31 +02:00
|
|
|
spt->slirp = NULL;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
|
|
|
|
struct tftp_t *tp)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
|
2009-06-24 14:42:31 +02:00
|
|
|
spt = &slirp->tftp_sessions[k];
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
if (!tftp_session_in_use(spt))
|
2004-08-25 22:55:44 +02:00
|
|
|
goto found;
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
/* sessions time out after 5 inactive seconds */
|
2009-06-24 14:42:30 +02:00
|
|
|
if ((int)(curtime - spt->timestamp) > 5000) {
|
2012-09-10 20:52:25 +02:00
|
|
|
tftp_session_terminate(spt);
|
2004-08-25 22:55:44 +02:00
|
|
|
goto found;
|
2009-06-24 14:42:30 +02:00
|
|
|
}
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
found:
|
|
|
|
memset(spt, 0, sizeof(*spt));
|
2017-03-23 12:31:56 +01:00
|
|
|
memcpy(&spt->client_addr, srcsas, sockaddr_size(srcsas));
|
2012-09-10 20:52:25 +02:00
|
|
|
spt->fd = -1;
|
2016-11-21 20:45:49 +01:00
|
|
|
spt->block_size = 512;
|
2004-08-24 23:57:12 +02:00
|
|
|
spt->client_port = tp->udp.uh_sport;
|
2009-06-24 14:42:31 +02:00
|
|
|
spt->slirp = slirp;
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
tftp_session_update(spt);
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
|
|
|
|
struct tftp_t *tp)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
|
2009-06-24 14:42:31 +02:00
|
|
|
spt = &slirp->tftp_sessions[k];
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
if (tftp_session_in_use(spt)) {
|
2016-03-15 10:31:23 +01:00
|
|
|
if (sockaddr_equal(&spt->client_addr, srcsas)) {
|
2004-08-24 23:57:12 +02:00
|
|
|
if (spt->client_port == tp->udp.uh_sport) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-13 12:39:36 +02:00
|
|
|
static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
|
2010-07-22 22:15:23 +02:00
|
|
|
uint8_t *buf, int len)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
2012-09-10 20:52:25 +02:00
|
|
|
int bytes_read = 0;
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2012-09-10 20:52:25 +02:00
|
|
|
if (spt->fd < 0) {
|
|
|
|
spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
|
|
|
|
}
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2012-09-10 20:52:25 +02:00
|
|
|
if (spt->fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2012-09-10 20:52:25 +02:00
|
|
|
if (len) {
|
2016-11-21 20:45:49 +01:00
|
|
|
lseek(spt->fd, block_nr * spt->block_size, SEEK_SET);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2012-09-10 20:52:25 +02:00
|
|
|
bytes_read = read(spt->fd, buf, len);
|
|
|
|
}
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2012-09-10 20:52:25 +02:00
|
|
|
return bytes_read;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
|
|
|
|
struct mbuf *m)
|
|
|
|
{
|
|
|
|
struct tftp_t *tp;
|
|
|
|
|
|
|
|
memset(m->m_data, 0, m->m_size);
|
|
|
|
|
|
|
|
m->m_data += IF_MAXLINKHDR;
|
|
|
|
if (spt->client_addr.ss_family == AF_INET6) {
|
|
|
|
m->m_data += sizeof(struct ip6);
|
|
|
|
} else {
|
|
|
|
m->m_data += sizeof(struct ip);
|
|
|
|
}
|
|
|
|
tp = (void *)m->m_data;
|
|
|
|
m->m_data += sizeof(struct udphdr);
|
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
|
|
|
|
struct tftp_t *recv_tp)
|
|
|
|
{
|
|
|
|
if (spt->client_addr.ss_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 sa6, da6;
|
|
|
|
|
|
|
|
sa6.sin6_addr = spt->slirp->vhost_addr6;
|
|
|
|
sa6.sin6_port = recv_tp->udp.uh_dport;
|
|
|
|
da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
|
|
|
|
da6.sin6_port = spt->client_port;
|
|
|
|
|
|
|
|
udp6_output(NULL, m, &sa6, &da6);
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in sa4, da4;
|
|
|
|
|
|
|
|
sa4.sin_addr = spt->slirp->vhost_addr;
|
|
|
|
sa4.sin_port = recv_tp->udp.uh_dport;
|
|
|
|
da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
|
|
|
|
da4.sin_port = spt->client_port;
|
|
|
|
|
|
|
|
udp_output(NULL, m, &sa4, &da4, IPTOS_LOWDELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
static int tftp_send_oack(struct tftp_session *spt,
|
2012-09-13 07:55:01 +02:00
|
|
|
const char *keys[], uint32_t values[], int nb,
|
2007-02-20 01:07:50 +01:00
|
|
|
struct tftp_t *recv_tp)
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
2012-09-13 07:55:01 +02:00
|
|
|
int i, n = 0;
|
2007-02-20 01:07:50 +01:00
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
m = m_get(spt->slirp);
|
2007-02-20 01:07:50 +01:00
|
|
|
|
|
|
|
if (!m)
|
2016-03-15 10:31:23 +01:00
|
|
|
return -1;
|
2007-02-20 01:07:50 +01:00
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
tp = tftp_prep_mbuf_data(spt, m);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2007-02-20 01:07:50 +01:00
|
|
|
tp->tp_op = htons(TFTP_OACK);
|
2012-09-13 07:55:01 +02:00
|
|
|
for (i = 0; i < nb; i++) {
|
|
|
|
n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
|
|
|
|
keys[i]) + 1;
|
|
|
|
n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
|
|
|
|
values[i]) + 1;
|
|
|
|
}
|
2007-02-20 01:07:50 +01:00
|
|
|
|
2016-11-21 20:45:49 +01:00
|
|
|
m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + n
|
|
|
|
- sizeof(struct udphdr);
|
2016-03-15 10:31:23 +01:00
|
|
|
tftp_udp_output(spt, m, recv_tp);
|
2007-02-20 01:07:50 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-24 14:42:30 +02:00
|
|
|
static void tftp_send_error(struct tftp_session *spt,
|
2010-07-22 22:15:23 +02:00
|
|
|
uint16_t errorcode, const char *msg,
|
2009-06-24 14:42:30 +02:00
|
|
|
struct tftp_t *recv_tp)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
m = m_get(spt->slirp);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
if (!m) {
|
2009-06-24 14:42:30 +02:00
|
|
|
goto out;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
tp = tftp_prep_mbuf_data(spt, m);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
tp->tp_op = htons(TFTP_ERROR);
|
|
|
|
tp->x.tp_error.tp_error_code = htons(errorcode);
|
2008-09-20 10:07:15 +02:00
|
|
|
pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2016-11-21 20:45:49 +01:00
|
|
|
m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX + 2) + 3 + strlen(msg)
|
2016-03-15 10:31:23 +01:00
|
|
|
- sizeof(struct udphdr);
|
|
|
|
tftp_udp_output(spt, m, recv_tp);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2009-06-24 14:42:30 +02:00
|
|
|
out:
|
2004-08-24 23:57:12 +02:00
|
|
|
tftp_session_terminate(spt);
|
|
|
|
}
|
|
|
|
|
2012-09-13 12:44:27 +02:00
|
|
|
static void tftp_send_next_block(struct tftp_session *spt,
|
|
|
|
struct tftp_t *recv_tp)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
|
|
|
int nobytes;
|
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
m = m_get(spt->slirp);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
if (!m) {
|
2012-09-13 12:44:27 +02:00
|
|
|
return;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
tp = tftp_prep_mbuf_data(spt, m);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
tp->tp_op = htons(TFTP_DATA);
|
2012-09-13 12:39:36 +02:00
|
|
|
tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2016-11-21 20:45:49 +01:00
|
|
|
nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf,
|
|
|
|
spt->block_size);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
if (nobytes < 0) {
|
|
|
|
m_free(m);
|
|
|
|
|
|
|
|
/* send "file not found" error back */
|
|
|
|
|
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
|
2012-09-13 12:44:27 +02:00
|
|
|
return;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-21 20:45:49 +01:00
|
|
|
m->m_len = sizeof(struct tftp_t) - (TFTP_BLOCKSIZE_MAX - nobytes)
|
|
|
|
- sizeof(struct udphdr);
|
2016-03-15 10:31:23 +01:00
|
|
|
tftp_udp_output(spt, m, recv_tp);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2016-11-21 20:45:49 +01:00
|
|
|
if (nobytes == spt->block_size) {
|
2004-08-24 23:57:12 +02:00
|
|
|
tftp_session_update(spt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tftp_session_terminate(spt);
|
|
|
|
}
|
|
|
|
|
2012-09-13 12:39:36 +02:00
|
|
|
spt->block_nr++;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
|
|
|
|
struct tftp_t *tp, int pktlen)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
2009-06-24 14:42:30 +02:00
|
|
|
int s, k;
|
2009-06-24 14:42:30 +02:00
|
|
|
size_t prefix_len;
|
2009-06-24 14:42:30 +02:00
|
|
|
char *req_fname;
|
2012-09-13 07:55:01 +02:00
|
|
|
const char *option_name[2];
|
|
|
|
uint32_t option_value[2];
|
|
|
|
int nb_options = 0;
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2010-01-07 18:01:28 +01:00
|
|
|
/* check if a session already exists and if so terminate it */
|
2016-03-15 10:31:23 +01:00
|
|
|
s = tftp_session_find(slirp, srcsas, tp);
|
2010-01-07 18:01:28 +01:00
|
|
|
if (s >= 0) {
|
|
|
|
tftp_session_terminate(&slirp->tftp_sessions[s]);
|
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
s = tftp_session_allocate(slirp, srcsas, tp);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
spt = &slirp->tftp_sessions[s];
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2014-03-24 09:30:17 +01:00
|
|
|
/* unspecified prefix means service disabled */
|
2009-06-24 14:42:31 +02:00
|
|
|
if (!slirp->tftp_prefix) {
|
2009-06-24 14:42:29 +02:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 14:42:30 +02:00
|
|
|
/* skip header fields */
|
|
|
|
k = 0;
|
2011-02-23 19:40:14 +01:00
|
|
|
pktlen -= offsetof(struct tftp_t, x.tp_buf);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2009-06-24 14:42:30 +02:00
|
|
|
/* prepend tftp_prefix */
|
2009-06-24 14:42:31 +02:00
|
|
|
prefix_len = strlen(slirp->tftp_prefix);
|
2011-08-21 05:09:37 +02:00
|
|
|
spt->filename = g_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
|
2009-06-24 14:42:31 +02:00
|
|
|
memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
|
2009-06-29 08:47:30 +02:00
|
|
|
spt->filename[prefix_len] = '/';
|
2009-06-24 14:42:30 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
/* get name */
|
2009-06-29 08:47:30 +02:00
|
|
|
req_fname = spt->filename + prefix_len + 1;
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2009-06-24 14:42:30 +02:00
|
|
|
while (1) {
|
|
|
|
if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
|
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
2004-08-24 23:57:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-02-23 19:40:14 +01:00
|
|
|
req_fname[k] = tp->x.tp_buf[k];
|
2009-06-24 14:42:30 +02:00
|
|
|
if (req_fname[k++] == '\0') {
|
2004-08-24 23:57:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
/* check mode */
|
2009-06-24 14:42:30 +02:00
|
|
|
if ((pktlen - k) < 6) {
|
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
2004-08-24 23:57:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2011-02-23 19:40:14 +01:00
|
|
|
if (strcasecmp(&tp->x.tp_buf[k], "octet") != 0) {
|
2004-08-24 23:57:12 +02:00
|
|
|
tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-20 01:07:50 +01:00
|
|
|
k += 6; /* skipping octet */
|
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
/* do sanity checks on the filename */
|
2009-06-29 08:47:30 +02:00
|
|
|
if (!strncmp(req_fname, "../", 3) ||
|
|
|
|
req_fname[strlen(req_fname) - 1] == '/' ||
|
2009-06-24 14:42:30 +02:00
|
|
|
strstr(req_fname, "/../")) {
|
2004-08-24 23:57:12 +02:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the file exists */
|
2009-06-24 14:42:30 +02:00
|
|
|
if (tftp_read_data(spt, 0, NULL, 0) < 0) {
|
2004-08-24 23:57:12 +02:00
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 14:42:30 +02:00
|
|
|
if (tp->x.tp_buf[pktlen - 1] != 0) {
|
2007-02-20 01:07:50 +01:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-13 07:55:01 +02:00
|
|
|
while (k < pktlen && nb_options < ARRAY_SIZE(option_name)) {
|
2007-02-20 01:07:50 +01:00
|
|
|
const char *key, *value;
|
|
|
|
|
2011-02-23 19:40:14 +01:00
|
|
|
key = &tp->x.tp_buf[k];
|
2007-02-20 01:07:50 +01:00
|
|
|
k += strlen(key) + 1;
|
|
|
|
|
2009-06-24 14:42:30 +02:00
|
|
|
if (k >= pktlen) {
|
2007-02-20 01:07:50 +01:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-23 19:40:14 +01:00
|
|
|
value = &tp->x.tp_buf[k];
|
2007-02-20 01:07:50 +01:00
|
|
|
k += strlen(value) + 1;
|
|
|
|
|
2011-01-12 14:57:18 +01:00
|
|
|
if (strcasecmp(key, "tsize") == 0) {
|
2007-02-20 01:07:50 +01:00
|
|
|
int tsize = atoi(value);
|
|
|
|
struct stat stat_p;
|
|
|
|
|
2009-06-24 14:42:29 +02:00
|
|
|
if (tsize == 0) {
|
2009-06-24 14:42:30 +02:00
|
|
|
if (stat(spt->filename, &stat_p) == 0)
|
2007-02-20 01:07:50 +01:00
|
|
|
tsize = stat_p.st_size;
|
|
|
|
else {
|
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-13 07:55:01 +02:00
|
|
|
option_name[nb_options] = "tsize";
|
|
|
|
option_value[nb_options] = tsize;
|
|
|
|
nb_options++;
|
|
|
|
} else if (strcasecmp(key, "blksize") == 0) {
|
|
|
|
int blksize = atoi(value);
|
|
|
|
|
2016-11-21 20:45:49 +01:00
|
|
|
/* Accept blksize up to our maximum size */
|
|
|
|
if (blksize > 0) {
|
|
|
|
spt->block_size = MIN(blksize, TFTP_BLOCKSIZE_MAX);
|
2012-09-13 07:55:01 +02:00
|
|
|
option_name[nb_options] = "blksize";
|
2016-11-21 20:45:49 +01:00
|
|
|
option_value[nb_options] = spt->block_size;
|
2012-09-13 07:55:01 +02:00
|
|
|
nb_options++;
|
|
|
|
}
|
2007-02-20 01:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-13 07:55:01 +02:00
|
|
|
if (nb_options > 0) {
|
|
|
|
assert(nb_options <= ARRAY_SIZE(option_name));
|
|
|
|
tftp_send_oack(spt, option_name, option_value, nb_options, tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-13 12:39:36 +02:00
|
|
|
spt->block_nr = 0;
|
|
|
|
tftp_send_next_block(spt, tp);
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
|
|
|
|
struct tftp_t *tp, int pktlen)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
s = tftp_session_find(slirp, srcsas, tp);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-13 12:44:27 +02:00
|
|
|
tftp_send_next_block(&slirp->tftp_sessions[s], tp);
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
|
|
|
|
struct tftp_t *tp, int pktlen)
|
2010-01-07 18:01:28 +01:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
s = tftp_session_find(slirp, srcsas, tp);
|
2010-01-07 18:01:28 +01:00
|
|
|
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tftp_session_terminate(&slirp->tftp_sessions[s]);
|
|
|
|
}
|
|
|
|
|
2016-03-15 10:31:23 +01:00
|
|
|
void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
|
2004-08-24 23:57:12 +02:00
|
|
|
{
|
|
|
|
struct tftp_t *tp = (struct tftp_t *)m->m_data;
|
|
|
|
|
|
|
|
switch(ntohs(tp->tp_op)) {
|
|
|
|
case TFTP_RRQ:
|
2016-03-15 10:31:23 +01:00
|
|
|
tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
|
2004-08-24 23:57:12 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_ACK:
|
2016-03-15 10:31:23 +01:00
|
|
|
tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
|
2004-08-24 23:57:12 +02:00
|
|
|
break;
|
2010-01-07 18:01:28 +01:00
|
|
|
|
|
|
|
case TFTP_ERROR:
|
2016-03-15 10:31:23 +01:00
|
|
|
tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
|
2010-01-07 18:01:28 +01:00
|
|
|
break;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
}
|