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
 * 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.
 */

P
Peter Maydell 已提交
25
#include "qemu/osdep.h"
B
bellard 已提交
26
#include <slirp.h>
J
Jan Kiszka 已提交
27
#include "qemu-common.h"
B
bellard 已提交
28

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

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

static void tftp_session_terminate(struct tftp_session *spt)
{
41 42 43 44
    if (spt->fd >= 0) {
        close(spt->fd);
        spt->fd = -1;
    }
45
    g_free(spt->filename);
46
    spt->slirp = NULL;
B
bellard 已提交
47 48
}

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

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

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

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

  return -1;

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

  tftp_session_update(spt);

  return k;
}

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

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

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

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

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

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

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

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

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

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

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

    if (!m)
	return -1;

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

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

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

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

154 155 156
    daddr.sin_addr = spt->client_ip;
    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);
160 161 162 163

    return 0;
}

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

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

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

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

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

B
bellard 已提交
184 185
  tp->tp_op = htons(TFTP_ERROR);
  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);
B
bellard 已提交
187 188 189 190 191 192 193

  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;

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

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

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

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

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

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

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

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

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

  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;

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

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

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

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

241
    return;
B
bellard 已提交
242 243
  }

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

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

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

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

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

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

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

  if (s < 0) {
    return;
  }

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

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

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

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] = '/';
298

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

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

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

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

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

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

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

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

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

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

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

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

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

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

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
          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++;
          }
387 388 389
      }
  }

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

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

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

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

  if (s < 0) {
    return;
  }

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

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

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

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

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