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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <slirp.h>
|
2008-08-21 19:58:08 +02:00
|
|
|
#include "qemu-common.h" // for pstrcpy
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
struct tftp_session {
|
2004-08-25 22:55:44 +02:00
|
|
|
int in_use;
|
|
|
|
unsigned char filename[TFTP_FILENAME_MAX];
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-25 22:55:44 +02:00
|
|
|
struct in_addr client_ip;
|
|
|
|
u_int16_t client_port;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-25 22:55:44 +02:00
|
|
|
int timestamp;
|
2004-08-24 23:57:12 +02:00
|
|
|
};
|
|
|
|
|
2007-10-26 21:01:16 +02:00
|
|
|
static struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2004-08-26 00:12:49 +02:00
|
|
|
const char *tftp_prefix;
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
static void tftp_session_update(struct tftp_session *spt)
|
|
|
|
{
|
2004-08-25 22:55:44 +02:00
|
|
|
spt->timestamp = curtime;
|
|
|
|
spt->in_use = 1;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tftp_session_terminate(struct tftp_session *spt)
|
|
|
|
{
|
|
|
|
spt->in_use = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tftp_session_allocate(struct tftp_t *tp)
|
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
|
|
|
|
spt = &tftp_sessions[k];
|
|
|
|
|
2004-08-25 22:55:44 +02:00
|
|
|
if (!spt->in_use)
|
|
|
|
goto found;
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
/* sessions time out after 5 inactive seconds */
|
2004-08-25 22:55:44 +02:00
|
|
|
if ((int)(curtime - spt->timestamp) > 5000)
|
|
|
|
goto found;
|
2004-08-24 23:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
found:
|
|
|
|
memset(spt, 0, sizeof(*spt));
|
|
|
|
memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
|
|
|
|
spt->client_port = tp->udp.uh_sport;
|
|
|
|
|
|
|
|
tftp_session_update(spt);
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tftp_session_find(struct tftp_t *tp)
|
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
|
|
|
|
spt = &tftp_sessions[k];
|
|
|
|
|
|
|
|
if (spt->in_use) {
|
|
|
|
if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
|
|
|
|
if (spt->client_port == tp->udp.uh_sport) {
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr,
|
|
|
|
u_int8_t *buf, int len)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int bytes_read = 0;
|
2007-02-20 01:12:07 +01:00
|
|
|
char buffer[1024];
|
|
|
|
int n;
|
2004-08-24 23:57:12 +02:00
|
|
|
|
2007-02-20 01:12:07 +01:00
|
|
|
n = snprintf(buffer, sizeof(buffer), "%s/%s",
|
|
|
|
tftp_prefix, spt->filename);
|
|
|
|
if (n >= sizeof(buffer))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fd = open(buffer, O_RDONLY | O_BINARY);
|
2004-08-24 23:57:12 +02:00
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len) {
|
|
|
|
lseek(fd, block_nr * 512, SEEK_SET);
|
|
|
|
|
|
|
|
bytes_read = read(fd, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
static int tftp_send_oack(struct tftp_session *spt,
|
2007-02-20 01:07:50 +01:00
|
|
|
const char *key, uint32_t value,
|
|
|
|
struct tftp_t *recv_tp)
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr, daddr;
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
m = m_get();
|
|
|
|
|
|
|
|
if (!m)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(m->m_data, 0, m->m_size);
|
|
|
|
|
2007-10-26 21:01:16 +02:00
|
|
|
m->m_data += IF_MAXLINKHDR;
|
2007-02-20 01:07:50 +01:00
|
|
|
tp = (void *)m->m_data;
|
|
|
|
m->m_data += sizeof(struct udpiphdr);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2007-02-20 01:07:50 +01:00
|
|
|
tp->tp_op = htons(TFTP_OACK);
|
2008-09-20 10:07:15 +02:00
|
|
|
n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
|
|
|
|
key) + 1;
|
|
|
|
n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
|
|
|
|
value) + 1;
|
2007-02-20 01:07:50 +01:00
|
|
|
|
|
|
|
saddr.sin_addr = recv_tp->ip.ip_dst;
|
|
|
|
saddr.sin_port = recv_tp->udp.uh_dport;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2007-02-20 01:07:50 +01:00
|
|
|
daddr.sin_addr = spt->client_ip;
|
|
|
|
daddr.sin_port = spt->client_port;
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
m->m_len = sizeof(struct tftp_t) - 514 + n -
|
2007-02-20 01:07:50 +01:00
|
|
|
sizeof(struct ip) - sizeof(struct udphdr);
|
|
|
|
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
static int tftp_send_error(struct tftp_session *spt,
|
2004-08-24 23:57:12 +02:00
|
|
|
u_int16_t errorcode, const char *msg,
|
|
|
|
struct tftp_t *recv_tp)
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr, daddr;
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
|
|
|
int nobytes;
|
|
|
|
|
|
|
|
m = m_get();
|
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(m->m_data, 0, m->m_size);
|
|
|
|
|
2007-10-26 21:01:16 +02:00
|
|
|
m->m_data += IF_MAXLINKHDR;
|
2004-08-24 23:57:12 +02:00
|
|
|
tp = (void *)m->m_data;
|
|
|
|
m->m_data += sizeof(struct udpiphdr);
|
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
|
|
|
|
|
|
|
saddr.sin_addr = recv_tp->ip.ip_dst;
|
|
|
|
saddr.sin_port = recv_tp->udp.uh_dport;
|
|
|
|
|
|
|
|
daddr.sin_addr = spt->client_ip;
|
|
|
|
daddr.sin_port = spt->client_port;
|
|
|
|
|
|
|
|
nobytes = 2;
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
|
2004-08-24 23:57:12 +02:00
|
|
|
sizeof(struct ip) - sizeof(struct udphdr);
|
|
|
|
|
|
|
|
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
|
|
|
|
|
|
|
tftp_session_terminate(spt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
static int tftp_send_data(struct tftp_session *spt,
|
2004-08-24 23:57:12 +02:00
|
|
|
u_int16_t block_nr,
|
|
|
|
struct tftp_t *recv_tp)
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr, daddr;
|
|
|
|
struct mbuf *m;
|
|
|
|
struct tftp_t *tp;
|
|
|
|
int nobytes;
|
|
|
|
|
|
|
|
if (block_nr < 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = m_get();
|
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(m->m_data, 0, m->m_size);
|
|
|
|
|
2007-10-26 21:01:16 +02:00
|
|
|
m->m_data += IF_MAXLINKHDR;
|
2004-08-24 23:57:12 +02:00
|
|
|
tp = (void *)m->m_data;
|
|
|
|
m->m_data += sizeof(struct udpiphdr);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
tp->tp_op = htons(TFTP_DATA);
|
|
|
|
tp->x.tp_data.tp_block_nr = htons(block_nr);
|
|
|
|
|
|
|
|
saddr.sin_addr = recv_tp->ip.ip_dst;
|
|
|
|
saddr.sin_port = recv_tp->udp.uh_dport;
|
|
|
|
|
|
|
|
daddr.sin_addr = spt->client_ip;
|
|
|
|
daddr.sin_port = spt->client_port;
|
|
|
|
|
|
|
|
nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
|
|
|
|
|
|
|
|
if (nobytes < 0) {
|
|
|
|
m_free(m);
|
|
|
|
|
|
|
|
/* send "file not found" error back */
|
|
|
|
|
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
|
2004-08-24 23:57:12 +02:00
|
|
|
sizeof(struct ip) - sizeof(struct udphdr);
|
|
|
|
|
|
|
|
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
|
|
|
|
|
|
|
if (nobytes == 512) {
|
|
|
|
tftp_session_update(spt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tftp_session_terminate(spt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
|
|
|
|
{
|
|
|
|
struct tftp_session *spt;
|
|
|
|
int s, k, n;
|
|
|
|
u_int8_t *src, *dst;
|
|
|
|
|
|
|
|
s = tftp_session_allocate(tp);
|
|
|
|
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
spt = &tftp_sessions[s];
|
|
|
|
|
|
|
|
src = tp->x.tp_buf;
|
|
|
|
dst = spt->filename;
|
|
|
|
n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
|
|
|
|
|
|
|
|
/* get name */
|
|
|
|
|
|
|
|
for (k = 0; k < n; k++) {
|
|
|
|
if (k < TFTP_FILENAME_MAX) {
|
|
|
|
dst[k] = src[k];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
if (src[k] == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
if (k >= n) {
|
|
|
|
return;
|
|
|
|
}
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
k++;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
/* check mode */
|
|
|
|
if ((n - k) < 6) {
|
|
|
|
return;
|
|
|
|
}
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
if (memcmp(&src[k], "octet\0", 6) != 0) {
|
|
|
|
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 */
|
|
|
|
|
|
|
|
if ((spt->filename[0] != '/')
|
2008-09-20 10:07:15 +02:00
|
|
|
|| (spt->filename[strlen((char *)spt->filename) - 1] == '/')
|
|
|
|
|| strstr((char *)spt->filename, "/../")) {
|
2004-08-24 23:57:12 +02:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only allow exported prefixes */
|
|
|
|
|
2007-02-20 01:12:07 +01:00
|
|
|
if (!tftp_prefix) {
|
2004-08-24 23:57:12 +02:00
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the file exists */
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
if (tftp_read_data(spt, 0, spt->filename, 0) < 0) {
|
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-20 01:07:50 +01:00
|
|
|
if (src[n - 1] != 0) {
|
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (k < n) {
|
|
|
|
const char *key, *value;
|
|
|
|
|
2008-09-20 10:07:15 +02:00
|
|
|
key = (char *)src + k;
|
2007-02-20 01:07:50 +01:00
|
|
|
k += strlen(key) + 1;
|
|
|
|
|
|
|
|
if (k >= n) {
|
|
|
|
tftp_send_error(spt, 2, "Access violation", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-20 10:07:15 +02:00
|
|
|
value = (char *)src + k;
|
2007-02-20 01:07:50 +01:00
|
|
|
k += strlen(value) + 1;
|
|
|
|
|
|
|
|
if (strcmp(key, "tsize") == 0) {
|
|
|
|
int tsize = atoi(value);
|
|
|
|
struct stat stat_p;
|
|
|
|
|
|
|
|
if (tsize == 0 && tftp_prefix) {
|
|
|
|
char buffer[1024];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = snprintf(buffer, sizeof(buffer), "%s/%s",
|
|
|
|
tftp_prefix, spt->filename);
|
|
|
|
|
|
|
|
if (stat(buffer, &stat_p) == 0)
|
|
|
|
tsize = stat_p.st_size;
|
|
|
|
else {
|
|
|
|
tftp_send_error(spt, 1, "File not found", tp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tftp_send_oack(spt, "tsize", tsize, tp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-24 23:57:12 +02:00
|
|
|
tftp_send_data(spt, 1, tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tftp_handle_ack(struct tftp_t *tp, int pktlen)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = tftp_session_find(tp);
|
|
|
|
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
if (tftp_send_data(&tftp_sessions[s],
|
|
|
|
ntohs(tp->x.tp_data.tp_block_nr) + 1,
|
2004-08-24 23:57:12 +02:00
|
|
|
tp) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tftp_input(struct mbuf *m)
|
|
|
|
{
|
|
|
|
struct tftp_t *tp = (struct tftp_t *)m->m_data;
|
|
|
|
|
|
|
|
switch(ntohs(tp->tp_op)) {
|
|
|
|
case TFTP_RRQ:
|
|
|
|
tftp_handle_rrq(tp, m->m_len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_ACK:
|
|
|
|
tftp_handle_ack(tp, m->m_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|