tftp.c 10.3 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 42 43
    if (spt->fd >= 0) {
        close(spt->fd);
        spt->fd = -1;
    }
44
    g_free(spt->filename);
45
    spt->slirp = NULL;
B
bellard 已提交
46 47
}

48
static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
B
bellard 已提交
49 50 51 52 53
{
  struct tftp_session *spt;
  int k;

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

56
    if (!tftp_session_in_use(spt))
B
bellard 已提交
57
        goto found;
B
bellard 已提交
58 59

    /* sessions time out after 5 inactive seconds */
60
    if ((int)(curtime - spt->timestamp) > 5000) {
61
        tftp_session_terminate(spt);
B
bellard 已提交
62
        goto found;
63
    }
B
bellard 已提交
64 65 66 67 68 69 70
  }

  return -1;

 found:
  memset(spt, 0, sizeof(*spt));
  memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
71
  spt->fd = -1;
B
bellard 已提交
72
  spt->client_port = tp->udp.uh_sport;
73
  spt->slirp = slirp;
B
bellard 已提交
74 75 76 77 78 79

  tftp_session_update(spt);

  return k;
}

80
static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
B
bellard 已提交
81 82 83 84 85
{
  struct tftp_session *spt;
  int k;

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

88
    if (tftp_session_in_use(spt)) {
B
bellard 已提交
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;
}

100
static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
101
                          uint8_t *buf, int len)
B
bellard 已提交
102
{
103
    int bytes_read = 0;
B
bellard 已提交
104

105 106 107
    if (spt->fd < 0) {
        spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
    }
B
bellard 已提交
108

109 110 111
    if (spt->fd < 0) {
        return -1;
    }
B
bellard 已提交
112

113 114
    if (len) {
        lseek(spt->fd, block_nr * 512, SEEK_SET);
B
bellard 已提交
115

116 117
        bytes_read = read(spt->fd, buf, len);
    }
B
bellard 已提交
118

119
    return bytes_read;
B
bellard 已提交
120 121
}

122
static int tftp_send_oack(struct tftp_session *spt,
123
                          const char *keys[], uint32_t values[], int nb,
124 125 126 127 128
                          struct tftp_t *recv_tp)
{
    struct sockaddr_in saddr, daddr;
    struct mbuf *m;
    struct tftp_t *tp;
129
    int i, n = 0;
130

131
    m = m_get(spt->slirp);
132 133 134 135 136 137

    if (!m)
	return -1;

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

138
    m->m_data += IF_MAXLINKHDR;
139 140
    tp = (void *)m->m_data;
    m->m_data += sizeof(struct udpiphdr);
141

142
    tp->tp_op = htons(TFTP_OACK);
143 144 145 146 147 148
    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;
    }
149 150 151

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

153 154 155
    daddr.sin_addr = spt->client_ip;
    daddr.sin_port = spt->client_port;

156
    m->m_len = sizeof(struct tftp_t) - 514 + n -
157
        sizeof(struct ip) - sizeof(struct udphdr);
158
    udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
159 160 161 162

    return 0;
}

163
static void tftp_send_error(struct tftp_session *spt,
164
                            uint16_t errorcode, const char *msg,
165
                            struct tftp_t *recv_tp)
B
bellard 已提交
166 167 168 169 170
{
  struct sockaddr_in saddr, daddr;
  struct mbuf *m;
  struct tftp_t *tp;

171
  m = m_get(spt->slirp);
B
bellard 已提交
172 173

  if (!m) {
174
    goto out;
B
bellard 已提交
175 176 177 178
  }

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

179
  m->m_data += IF_MAXLINKHDR;
B
bellard 已提交
180 181
  tp = (void *)m->m_data;
  m->m_data += sizeof(struct udpiphdr);
182

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

  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;

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

196
  udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
B
bellard 已提交
197

198
out:
B
bellard 已提交
199 200 201
  tftp_session_terminate(spt);
}

202 203
static void tftp_send_next_block(struct tftp_session *spt,
                                 struct tftp_t *recv_tp)
B
bellard 已提交
204 205 206 207 208 209
{
  struct sockaddr_in saddr, daddr;
  struct mbuf *m;
  struct tftp_t *tp;
  int nobytes;

210
  m = m_get(spt->slirp);
B
bellard 已提交
211 212

  if (!m) {
213
    return;
B
bellard 已提交
214 215 216 217
  }

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

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

B
bellard 已提交
222
  tp->tp_op = htons(TFTP_DATA);
223
  tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
B
bellard 已提交
224 225 226 227 228 229 230

  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;

231
  nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
B
bellard 已提交
232 233 234 235 236 237 238 239

  if (nobytes < 0) {
    m_free(m);

    /* send "file not found" error back */

    tftp_send_error(spt, 1, "File not found", tp);

240
    return;
B
bellard 已提交
241 242
  }

243
  m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
B
bellard 已提交
244 245
        sizeof(struct ip) - sizeof(struct udphdr);

246
  udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
B
bellard 已提交
247 248 249 250 251 252 253 254

  if (nobytes == 512) {
    tftp_session_update(spt);
  }
  else {
    tftp_session_terminate(spt);
  }

255
  spt->block_nr++;
B
bellard 已提交
256 257
}

258
static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
B
bellard 已提交
259 260
{
  struct tftp_session *spt;
261
  int s, k;
262
  size_t prefix_len;
263
  char *req_fname;
264 265 266
  const char *option_name[2];
  uint32_t option_value[2];
  int nb_options = 0;
B
bellard 已提交
267

T
Thomas Horsten 已提交
268 269 270 271 272 273
  /* 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]);
  }

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

  if (s < 0) {
    return;
  }

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

D
Deepak Kathayat 已提交
282
  /* unspecified prefix means service disabled */
283
  if (!slirp->tftp_prefix) {
284 285 286 287
      tftp_send_error(spt, 2, "Access violation", tp);
      return;
  }

288 289
  /* skip header fields */
  k = 0;
290
  pktlen -= offsetof(struct tftp_t, x.tp_buf);
B
bellard 已提交
291

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

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

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

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

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

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

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

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

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

344
  while (k < pktlen && nb_options < ARRAY_SIZE(option_name)) {
345 346
      const char *key, *value;

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

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

355
      value = &tp->x.tp_buf[k];
356 357
      k += strlen(value) + 1;

358
      if (strcasecmp(key, "tsize") == 0) {
359 360 361
	  int tsize = atoi(value);
	  struct stat stat_p;

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

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
          option_name[nb_options] = "tsize";
          option_value[nb_options] = tsize;
          nb_options++;
      } else if (strcasecmp(key, "blksize") == 0) {
          int blksize = atoi(value);

          /* If blksize option is bigger than what we will
           * emit, accept the option with our packet size.
           * Otherwise, simply do as we didn't see the option.
           */
          if (blksize >= 512) {
              option_name[nb_options] = "blksize";
              option_value[nb_options] = 512;
              nb_options++;
          }
386 387 388
      }
  }

389 390 391 392 393 394
  if (nb_options > 0) {
      assert(nb_options <= ARRAY_SIZE(option_name));
      tftp_send_oack(spt, option_name, option_value, nb_options, tp);
      return;
  }

395 396
  spt->block_nr = 0;
  tftp_send_next_block(spt, tp);
B
bellard 已提交
397 398
}

399
static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
B
bellard 已提交
400 401 402
{
  int s;

403
  s = tftp_session_find(slirp, tp);
B
bellard 已提交
404 405 406 407 408

  if (s < 0) {
    return;
  }

409
  tftp_send_next_block(&slirp->tftp_sessions[s], tp);
B
bellard 已提交
410 411
}

T
Thomas Horsten 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424
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 已提交
425 426 427 428 429 430
void tftp_input(struct mbuf *m)
{
  struct tftp_t *tp = (struct tftp_t *)m->m_data;

  switch(ntohs(tp->tp_op)) {
  case TFTP_RRQ:
431
    tftp_handle_rrq(m->slirp, tp, m->m_len);
B
bellard 已提交
432 433 434
    break;

  case TFTP_ACK:
435
    tftp_handle_ack(m->slirp, tp, m->m_len);
B
bellard 已提交
436
    break;
T
Thomas Horsten 已提交
437 438 439 440

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