tftp.c 9.4 KB
Newer Older
B
bellard 已提交
1 2
/*
 * tftp.c - a simple, read-only tftp server for qemu
3
 *
B
bellard 已提交
4
 * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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>
J
Jan Kiszka 已提交
26
#include "qemu-common.h"
B
bellard 已提交
27

28 29 30 31
static inline int tftp_session_in_use(struct tftp_session *spt)
{
    return (spt->slirp != NULL);
}
B
bellard 已提交
32

33
static inline void tftp_session_update(struct tftp_session *spt)
B
bellard 已提交
34
{
B
bellard 已提交
35
    spt->timestamp = curtime;
B
bellard 已提交
36 37 38 39
}

static void tftp_session_terminate(struct tftp_session *spt)
{
40 41
    qemu_free(spt->filename);
    spt->slirp = NULL;
B
bellard 已提交
42 43
}

44
static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
B
bellard 已提交
45 46 47 48 49
{
  struct tftp_session *spt;
  int k;

  for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
50
    spt = &slirp->tftp_sessions[k];
B
bellard 已提交
51

52
    if (!tftp_session_in_use(spt))
B
bellard 已提交
53
        goto found;
B
bellard 已提交
54 55

    /* sessions time out after 5 inactive seconds */
56 57
    if ((int)(curtime - spt->timestamp) > 5000) {
        qemu_free(spt->filename);
B
bellard 已提交
58
        goto found;
59
    }
B
bellard 已提交
60 61 62 63 64 65 66 67
  }

  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;
68
  spt->slirp = slirp;
B
bellard 已提交
69 70 71 72 73 74

  tftp_session_update(spt);

  return k;
}

75
static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
B
bellard 已提交
76 77 78 79 80
{
  struct tftp_session *spt;
  int k;

  for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
81
    spt = &slirp->tftp_sessions[k];
B
bellard 已提交
82

83
    if (tftp_session_in_use(spt)) {
B
bellard 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      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;
100

101
  fd = open(spt->filename, O_RDONLY | O_BINARY);
B
bellard 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

  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;
}

118
static int tftp_send_oack(struct tftp_session *spt,
119 120 121 122 123 124 125 126
                          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;

127
    m = m_get(spt->slirp);
128 129 130 131 132 133

    if (!m)
	return -1;

    memset(m->m_data, 0, m->m_size);

134
    m->m_data += IF_MAXLINKHDR;
135 136
    tp = (void *)m->m_data;
    m->m_data += sizeof(struct udpiphdr);
137

138
    tp->tp_op = htons(TFTP_OACK);
139 140 141 142
    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;
143 144 145

    saddr.sin_addr = recv_tp->ip.ip_dst;
    saddr.sin_port = recv_tp->udp.uh_dport;
146

147 148 149
    daddr.sin_addr = spt->client_ip;
    daddr.sin_port = spt->client_port;

150
    m->m_len = sizeof(struct tftp_t) - 514 + n -
151 152 153 154 155 156
        sizeof(struct ip) - sizeof(struct udphdr);
    udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);

    return 0;
}

157 158 159
static void tftp_send_error(struct tftp_session *spt,
                            u_int16_t errorcode, const char *msg,
                            struct tftp_t *recv_tp)
B
bellard 已提交
160 161 162 163 164 165
{
  struct sockaddr_in saddr, daddr;
  struct mbuf *m;
  struct tftp_t *tp;
  int nobytes;

166
  m = m_get(spt->slirp);
B
bellard 已提交
167 168

  if (!m) {
169
    goto out;
B
bellard 已提交
170 171 172 173
  }

  memset(m->m_data, 0, m->m_size);

174
  m->m_data += IF_MAXLINKHDR;
B
bellard 已提交
175 176
  tp = (void *)m->m_data;
  m->m_data += sizeof(struct udpiphdr);
177

B
bellard 已提交
178 179
  tp->tp_op = htons(TFTP_ERROR);
  tp->x.tp_error.tp_error_code = htons(errorcode);
180
  pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
B
bellard 已提交
181 182 183 184 185 186 187 188 189

  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;

190
  m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
B
bellard 已提交
191 192 193 194
        sizeof(struct ip) - sizeof(struct udphdr);

  udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);

195
out:
B
bellard 已提交
196 197 198
  tftp_session_terminate(spt);
}

199
static int tftp_send_data(struct tftp_session *spt,
B
bellard 已提交
200 201 202 203 204 205 206 207 208 209 210 211
			  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;
  }

212
  m = m_get(spt->slirp);
B
bellard 已提交
213 214 215 216 217 218 219

  if (!m) {
    return -1;
  }

  memset(m->m_data, 0, m->m_size);

220
  m->m_data += IF_MAXLINKHDR;
B
bellard 已提交
221 222
  tp = (void *)m->m_data;
  m->m_data += sizeof(struct udpiphdr);
223

B
bellard 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  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;
  }

245
  m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
B
bellard 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259
        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;
}

260
static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
B
bellard 已提交
261 262
{
  struct tftp_session *spt;
263
  int s, k;
264
  size_t prefix_len;
265
  char *req_fname;
B
bellard 已提交
266

T
Thomas Horsten 已提交
267 268 269 270 271 272
  /* check if a session already exists and if so terminate it */
  s = tftp_session_find(slirp, tp);
  if (s >= 0) {
    tftp_session_terminate(&slirp->tftp_sessions[s]);
  }

273
  s = tftp_session_allocate(slirp, tp);
B
bellard 已提交
274 275 276 277 278

  if (s < 0) {
    return;
  }

279
  spt = &slirp->tftp_sessions[s];
B
bellard 已提交
280

281
  /* unspecifed prefix means service disabled */
282
  if (!slirp->tftp_prefix) {
283 284 285 286
      tftp_send_error(spt, 2, "Access violation", tp);
      return;
  }

287 288 289
  /* skip header fields */
  k = 0;
  pktlen -= ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
B
bellard 已提交
290

291
  /* prepend tftp_prefix */
292
  prefix_len = strlen(slirp->tftp_prefix);
293
  spt->filename = qemu_malloc(prefix_len + TFTP_FILENAME_MAX + 2);
294
  memcpy(spt->filename, slirp->tftp_prefix, prefix_len);
295
  spt->filename[prefix_len] = '/';
296

B
bellard 已提交
297
  /* get name */
298
  req_fname = spt->filename + prefix_len + 1;
B
bellard 已提交
299

300 301 302
  while (1) {
    if (k >= TFTP_FILENAME_MAX || k >= pktlen) {
      tftp_send_error(spt, 2, "Access violation", tp);
B
bellard 已提交
303 304
      return;
    }
305 306
    req_fname[k] = (char)tp->x.tp_buf[k];
    if (req_fname[k++] == '\0') {
B
bellard 已提交
307 308 309
      break;
    }
  }
310

B
bellard 已提交
311
  /* check mode */
312 313
  if ((pktlen - k) < 6) {
    tftp_send_error(spt, 2, "Access violation", tp);
B
bellard 已提交
314 315
    return;
  }
316

317
  if (memcmp(&tp->x.tp_buf[k], "octet\0", 6) != 0) {
B
bellard 已提交
318 319 320 321
      tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
      return;
  }

322 323
  k += 6; /* skipping octet */

B
bellard 已提交
324
  /* do sanity checks on the filename */
325 326
  if (!strncmp(req_fname, "../", 3) ||
      req_fname[strlen(req_fname) - 1] == '/' ||
327
      strstr(req_fname, "/../")) {
B
bellard 已提交
328 329 330 331 332
      tftp_send_error(spt, 2, "Access violation", tp);
      return;
  }

  /* check if the file exists */
333
  if (tftp_read_data(spt, 0, NULL, 0) < 0) {
B
bellard 已提交
334 335 336 337
      tftp_send_error(spt, 1, "File not found", tp);
      return;
  }

338
  if (tp->x.tp_buf[pktlen - 1] != 0) {
339 340 341 342
      tftp_send_error(spt, 2, "Access violation", tp);
      return;
  }

343
  while (k < pktlen) {
344 345
      const char *key, *value;

346
      key = (const char *)&tp->x.tp_buf[k];
347 348
      k += strlen(key) + 1;

349
      if (k >= pktlen) {
350 351 352 353
	  tftp_send_error(spt, 2, "Access violation", tp);
	  return;
      }

354
      value = (const char *)&tp->x.tp_buf[k];
355 356 357 358 359 360
      k += strlen(value) + 1;

      if (strcmp(key, "tsize") == 0) {
	  int tsize = atoi(value);
	  struct stat stat_p;

361
	  if (tsize == 0) {
362
	      if (stat(spt->filename, &stat_p) == 0)
363 364 365 366 367 368 369 370 371 372 373
		  tsize = stat_p.st_size;
	      else {
		  tftp_send_error(spt, 1, "File not found", tp);
		  return;
	      }
	  }

	  tftp_send_oack(spt, "tsize", tsize, tp);
      }
  }

B
bellard 已提交
374 375 376
  tftp_send_data(spt, 1, tp);
}

377
static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
B
bellard 已提交
378 379 380
{
  int s;

381
  s = tftp_session_find(slirp, tp);
B
bellard 已提交
382 383 384 385 386

  if (s < 0) {
    return;
  }

387
  if (tftp_send_data(&slirp->tftp_sessions[s],
388
		     ntohs(tp->x.tp_data.tp_block_nr) + 1,
B
bellard 已提交
389 390 391 392 393
		     tp) < 0) {
    return;
  }
}

T
Thomas Horsten 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406
static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
{
  int s;

  s = tftp_session_find(slirp, tp);

  if (s < 0) {
    return;
  }

  tftp_session_terminate(&slirp->tftp_sessions[s]);
}

B
bellard 已提交
407 408 409 410 411 412
void tftp_input(struct mbuf *m)
{
  struct tftp_t *tp = (struct tftp_t *)m->m_data;

  switch(ntohs(tp->tp_op)) {
  case TFTP_RRQ:
413
    tftp_handle_rrq(m->slirp, tp, m->m_len);
B
bellard 已提交
414 415 416
    break;

  case TFTP_ACK:
417
    tftp_handle_ack(m->slirp, tp, m->m_len);
B
bellard 已提交
418
    break;
T
Thomas Horsten 已提交
419 420 421 422

  case TFTP_ERROR:
    tftp_handle_error(m->slirp, tp, m->m_len);
    break;
B
bellard 已提交
423 424
  }
}