VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/tftp.c@ 43876

Last change on this file since 43876 was 43876, checked in by vboxsync, 12 years ago

fixed a few format specifier bugs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.2 KB
Line 
1/* $Id: tftp.c 43876 2012-11-15 13:44:09Z vboxsync $ */
2/** @file
3 * NAT - TFTP server.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * This code is based on:
20 *
21 * tftp.c - a simple, read-only tftp server for qemu
22 *
23 * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
24 *
25 * Permission is hereby granted, free of charge, to any person obtaining a copy
26 * of this software and associated documentation files (the "Software"), to deal
27 * in the Software without restriction, including without limitation the rights
28 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 * copies of the Software, and to permit persons to whom the Software is
30 * furnished to do so, subject to the following conditions:
31 *
32 * The above copyright notice and this permission notice shall be included in
33 * all copies or substantial portions of the Software.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
38 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41 * THE SOFTWARE.
42 */
43
44#include <slirp.h>
45#include <iprt/file.h>
46#include <iprt/asm-math.h>
47
48typedef enum ENMTFTPSESSIONFMT
49{
50 TFTPFMT_NONE = 0,
51 TFTPFMT_OCTET,
52 TFTPFMT_NETASCII,
53 TFTPFMT_MAIL,
54 TFTPFMT_NOT_FMT = 0xffff
55} ENMTFTPSESSIONFMT;
56
57typedef struct TFPTPSESSIONOPTDESC
58{
59 int fRequested;
60 uint64_t u64Value;
61} TFPTPSESSIONOPTDESC, *PTFPTPSESSIONOPTDESC;
62
63typedef struct TFTPSESSION
64{
65 int fInUse;
66 unsigned char pszFilename[TFTP_FILENAME_MAX];
67 struct in_addr IpClientAddress;
68 uint16_t u16ClientPort;
69 int iTimestamp;
70 uint64_t cbTransfered;
71 uint16_t cTftpAck;
72 ENMTFTPSESSIONFMT enmTftpFmt;
73 TFPTPSESSIONOPTDESC OptionBlkSize;
74 TFPTPSESSIONOPTDESC OptionTSize;
75 TFPTPSESSIONOPTDESC OptionTimeout;
76} TFTPSESSION, *PTFTPSESSION, **PPTFTPSESSION;
77
78#pragma pack(1)
79typedef struct TFTPCOREHDR
80{
81 uint16_t u16TftpOpCode;
82 /* Data lays here (might be raw uint8_t* or header of payload ) */
83} TFTPCOREHDR, *PTFTPCOREHDR;
84
85typedef struct TFTPIPHDR
86{
87 struct ip IPv4Hdr;
88 struct udphdr UdpHdr;
89 uint16_t u16TftpOpType;
90 TFTPCOREHDR Core;
91 /* Data lays here */
92} TFTPIPHDR, *PTFTPIPHDR;
93#pragma pack()
94
95typedef const PTFTPIPHDR PCTFTPIPHDR;
96
97typedef const PTFTPSESSION PCTFTPSESSION;
98
99
100typedef struct TFTPOPTIONDESC
101{
102 const char *pszName;
103 ENMTFTPSESSIONFMT enmType;
104 int cbName;
105 bool fHasValue;
106} TFTPOPTIONDESC, *PTFTPOPTIONDESC;
107
108typedef const PTFTPOPTIONDESC PCTFTPOPTIONDESC;
109static TFTPOPTIONDESC g_TftpTransferFmtDesc[] =
110{
111 {"octet", TFTPFMT_OCTET, 5, false}, /* RFC1350 */
112 {"netascii", TFTPFMT_NETASCII, 8, false}, /* RFC1350 */
113 {"mail", TFTPFMT_MAIL, 4, false}, /* RFC1350 */
114};
115
116static TFTPOPTIONDESC g_TftpDesc[] =
117{
118 {"blksize", TFTPFMT_NOT_FMT, 7, true}, /* RFC2348 */
119 {"timeout", TFTPFMT_NOT_FMT, 7, true}, /* RFC2349 */
120 {"tsize", TFTPFMT_NOT_FMT, 5, true}, /* RFC2349 */
121 {"size", TFTPFMT_NOT_FMT, 4, true}, /* RFC2349 */
122};
123
124/**
125 * This function evaluate file name.
126 * @param pu8Payload
127 * @param cbPayload
128 * @param cbFileName
129 * @return VINF_SUCCESS -
130 * VERR_INVALID_PARAMETER -
131 */
132DECLINLINE(int) tftpSecurityFilenameCheck(PNATState pData, PCTFTPSESSION pcTftpSession)
133{
134 size_t cbSessionFilename = 0;
135 int rc = VINF_SUCCESS;
136 AssertPtrReturn(pcTftpSession, VERR_INVALID_PARAMETER);
137 cbSessionFilename = RTStrNLen((const char *)pcTftpSession->pszFilename, TFTP_FILENAME_MAX);
138 if ( !RTStrNCmp((const char*)pcTftpSession->pszFilename, "../", 3)
139 || (pcTftpSession->pszFilename[cbSessionFilename - 1] == '/')
140 || RTStrStr((const char *)pcTftpSession->pszFilename, "/../"))
141 rc = VERR_FILE_NOT_FOUND;
142
143 /* only allow exported prefixes */
144 if ( RT_SUCCESS(rc)
145 && !tftp_prefix)
146 rc = VERR_INTERNAL_ERROR;
147 LogFlowFuncLeaveRC(rc);
148 return rc;
149}
150
151/*
152 * This function returns index of option descriptor in passed descriptor array
153 * @param piIdxOpt returned index value
154 * @param paTftpDesc array of known Tftp descriptors
155 * @param caTftpDesc size of array of tftp descriptors
156 * @param pszOpt name of option
157 */
158DECLINLINE(int) tftpFindDesciptorIndexByName(int *piIdxOpt, PCTFTPOPTIONDESC paTftpDesc, int caTftpDesc, const char *pszOptName)
159{
160 int rc = VINF_SUCCESS;
161 int idxOption = 0;
162 AssertReturn(piIdxOpt, VERR_INVALID_PARAMETER);
163 AssertReturn(paTftpDesc, VERR_INVALID_PARAMETER);
164 AssertReturn(pszOptName, VERR_INVALID_PARAMETER);
165 for (idxOption = 0; idxOption < caTftpDesc; ++idxOption)
166 {
167 if (!RTStrNICmp(pszOptName, paTftpDesc[idxOption].pszName, 10))
168 {
169 *piIdxOpt = idxOption;
170 return rc;
171 }
172 }
173 rc = VERR_NOT_FOUND;
174 return rc;
175}
176
177/**
178 * Helper function to look for index of descriptor in transfer format descriptors
179 * @param piIdxOpt returned value of index
180 * @param pszOpt name of option
181 */
182DECLINLINE(int) tftpFindTransferFormatIdxbyName(int *piIdxOpt, const char *pszOpt)
183{
184 return tftpFindDesciptorIndexByName(piIdxOpt, &g_TftpTransferFmtDesc[0], RT_ELEMENTS(g_TftpTransferFmtDesc), pszOpt);
185}
186
187/**
188 * Helper function to look for index of descriptor in options descriptors
189 * @param piIdxOpt returned value of index
190 * @param pszOpt name of option
191 */
192DECLINLINE(int) tftpFindOptionIdxbyName(int *piIdxOpt, const char *pszOpt)
193{
194 return tftpFindDesciptorIndexByName(piIdxOpt, &g_TftpDesc[0], RT_ELEMENTS(g_TftpDesc), pszOpt);
195}
196
197
198DECLINLINE(bool) tftpIsAcceptableOption(const char *pszOptionName)
199{
200 int idxOptDesc = 0;
201 AssertPtrReturn(pszOptionName, false);
202 AssertReturn(RTStrNLen(pszOptionName,10) >= 4, false);
203 AssertReturn(RTStrNLen(pszOptionName,10) < 8, false);
204 for(idxOptDesc = 0; idxOptDesc < RT_ELEMENTS(g_TftpTransferFmtDesc); ++idxOptDesc)
205 {
206 if (!RTStrNICmp(pszOptionName, g_TftpTransferFmtDesc[idxOptDesc].pszName, 10))
207 return true;
208 }
209 for(idxOptDesc = 0; idxOptDesc < RT_ELEMENTS(g_TftpDesc); ++idxOptDesc)
210 {
211 if (!RTStrNICmp(pszOptionName, g_TftpDesc[idxOptDesc].pszName, 10))
212 return true;
213 }
214 return false;
215}
216
217
218/**
219 * This helper function that validate if client want to operate in supported by server mode.
220 * @param pcTftpHeader comulative header (IP, UDP, TFTP)
221 * @param pcu8Options pointer to the options supposing that pointer points at the mode option
222 * @param cbOptions size of the options buffer
223 */
224DECLINLINE(int) tftpIsSupportedTransferMode(PCTFTPSESSION pcTftpSession)
225{
226 AssertPtrReturn(pcTftpSession, 0);
227 return (pcTftpSession->enmTftpFmt == TFTPFMT_OCTET);
228}
229
230
231DECLINLINE(void) tftpSessionUpdate(PNATState pData, PTFTPSESSION pTftpSession)
232{
233 pTftpSession->iTimestamp = curtime;
234 pTftpSession->fInUse = 1;
235}
236
237DECLINLINE(void) tftpSessionTerminate(PTFTPSESSION pTftpSession)
238{
239 pTftpSession->fInUse = 0;
240}
241
242DECLINLINE(int) tftpSessionParseAndMarkOption(const char *pcszRawOption, PTFPTPSESSIONOPTDESC pTftpSessionOption)
243{
244 int rc = VINF_SUCCESS;
245 rc = RTStrToInt64Full(pcszRawOption, 0, (int64_t *)&pTftpSessionOption->u64Value);
246 AssertRCReturn(rc, rc);
247 pTftpSessionOption->fRequested = 1;
248 return rc;
249}
250
251DECLINLINE(int) tftpSessionOptionParse(PTFTPSESSION pTftpSession, PCTFTPIPHDR pcTftpIpHeader)
252{
253 int rc = VINF_SUCCESS;
254 char *pszTftpRRQRaw;
255 size_t idxTftpRRQRaw = 0;
256 int cbTftpRRQRaw = 0;
257 int fWithArg = 0;
258 int idxOptionArg = 0;
259 AssertPtrReturn(pTftpSession, VERR_INVALID_PARAMETER);
260 AssertPtrReturn(pcTftpIpHeader, VERR_INVALID_PARAMETER);
261 AssertReturn(RT_N2H_U16(pcTftpIpHeader->u16TftpOpType) == TFTP_RRQ, VERR_INVALID_PARAMETER);
262 LogFlowFunc(("pTftpSession:%p, pcTftpIpHeader:%p\n", pTftpSession, pcTftpIpHeader));
263 pszTftpRRQRaw = (char *)&pcTftpIpHeader->Core;
264 cbTftpRRQRaw = RT_H2N_U16(pcTftpIpHeader->UdpHdr.uh_ulen) + sizeof(struct ip) - RT_OFFSETOF(TFTPIPHDR, Core);
265 while(cbTftpRRQRaw)
266 {
267 idxTftpRRQRaw = RTStrNLen(pszTftpRRQRaw, 512 - idxTftpRRQRaw) + 1;
268 if (RTStrNLen((char *)pTftpSession->pszFilename, TFTP_FILENAME_MAX) == 0)
269 {
270 rc = RTStrCopy((char *)pTftpSession->pszFilename, TFTP_FILENAME_MAX, pszTftpRRQRaw);
271 if (RT_FAILURE(rc))
272 {
273 LogFlowFuncLeaveRC(rc);
274 AssertRCReturn(rc,rc);
275 }
276 }
277 else if (pTftpSession->enmTftpFmt == TFTPFMT_NONE)
278 {
279 int idxFmt = 0;
280 rc = tftpFindTransferFormatIdxbyName(&idxFmt, pszTftpRRQRaw);
281 if (RT_FAILURE(rc))
282 {
283 LogFlowFuncLeaveRC(VERR_INTERNAL_ERROR);
284 return VERR_INTERNAL_ERROR;
285 }
286 AssertReturn( g_TftpTransferFmtDesc[idxFmt].enmType != TFTPFMT_NONE
287 && g_TftpTransferFmtDesc[idxFmt].enmType != TFTPFMT_NOT_FMT, VERR_INTERNAL_ERROR);
288 pTftpSession->enmTftpFmt = g_TftpTransferFmtDesc[idxFmt].enmType;
289 }
290 else if (fWithArg)
291 {
292 if (!RTStrICmp("blksize", g_TftpDesc[idxOptionArg].pszName))
293 {
294 rc = tftpSessionParseAndMarkOption(pszTftpRRQRaw, &pTftpSession->OptionBlkSize);
295 if (pTftpSession->OptionBlkSize.u64Value > UINT16_MAX)
296 rc = VERR_INVALID_PARAMETER;
297 }
298
299 if ( RT_SUCCESS(rc)
300 && !RTStrICmp("tsize", g_TftpDesc[idxOptionArg].pszName))
301 rc = tftpSessionParseAndMarkOption(pszTftpRRQRaw, &pTftpSession->OptionTSize);
302
303 /* @todo: we don't use timeout, but its value in the range 0-255 */
304 if ( RT_SUCCESS(rc)
305 && !RTStrICmp("timeout", g_TftpDesc[idxOptionArg].pszName))
306 rc = tftpSessionParseAndMarkOption(pszTftpRRQRaw, &pTftpSession->OptionTimeout);
307
308 /* @todo: unknown option detection */
309 if (RT_FAILURE(rc))
310 {
311 LogFlowFuncLeaveRC(rc);
312 AssertRCReturn(rc,rc);
313 }
314 fWithArg = 0;
315 idxOptionArg = 0;
316 }
317 else
318 {
319 rc = tftpFindOptionIdxbyName(&idxOptionArg, pszTftpRRQRaw);
320 if (RT_SUCCESS(rc))
321 fWithArg = 1;
322 else
323 {
324 LogFlowFuncLeaveRC(rc);
325 AssertRCReturn(rc,rc);
326 }
327 }
328 pszTftpRRQRaw += idxTftpRRQRaw;
329 cbTftpRRQRaw -= idxTftpRRQRaw;
330 }
331
332 LogFlowFuncLeaveRC(rc);
333 return rc;
334}
335
336static int tftpAllocateSession(PNATState pData, PCTFTPIPHDR pcTftpIpHeader, PPTFTPSESSION ppTftpSession)
337{
338 PTFTPSESSION pTftpSession = NULL;
339 int rc = VINF_SUCCESS;
340 int idxSession;
341 AssertPtrReturn(pData, VERR_INVALID_PARAMETER);
342 AssertPtrReturn(pcTftpIpHeader, VERR_INVALID_PARAMETER);
343 AssertPtrReturn(ppTftpSession, VERR_INVALID_PARAMETER);
344
345 for (idxSession = 0; idxSession < TFTP_SESSIONS_MAX; idxSession++)
346 {
347 pTftpSession = &((PTFTPSESSION)pData->pvTftpSessions)[idxSession];
348
349 if (!pTftpSession->fInUse)
350 goto found;
351
352 /* sessions time out after 5 inactive seconds */
353 if ((int)(curtime - pTftpSession->iTimestamp) > 5000)
354 goto found;
355 }
356
357 return VERR_NOT_FOUND;
358
359 found:
360 memset(pTftpSession, 0, sizeof(*pTftpSession));
361 memcpy(&pTftpSession->IpClientAddress, &pcTftpIpHeader->IPv4Hdr.ip_src, sizeof(pTftpSession->IpClientAddress));
362 pTftpSession->u16ClientPort = pcTftpIpHeader->UdpHdr.uh_sport;
363 rc = tftpSessionOptionParse(pTftpSession, pcTftpIpHeader);
364 AssertRCReturn(rc, VERR_INTERNAL_ERROR);
365 *ppTftpSession = pTftpSession;
366
367 tftpSessionUpdate(pData, pTftpSession);
368
369 return VINF_SUCCESS;
370}
371
372static int tftpSessionFind(PNATState pData, PCTFTPIPHDR pcTftpIpHeader, PPTFTPSESSION ppTftpSessions)
373{
374 PTFTPSESSION pTftpSession;
375 int idxTftpSession;
376 AssertPtrReturn(pData, VERR_INVALID_PARAMETER);
377 AssertPtrReturn(pcTftpIpHeader, VERR_INVALID_PARAMETER);
378 AssertPtrReturn(ppTftpSessions, VERR_INVALID_PARAMETER);
379
380 for (idxTftpSession = 0; idxTftpSession < TFTP_SESSIONS_MAX; idxTftpSession++)
381 {
382 pTftpSession = &((PTFTPSESSION)pData->pvTftpSessions)[idxTftpSession];
383
384 if (pTftpSession->fInUse)
385 {
386 if (!memcmp(&pTftpSession->IpClientAddress, &pcTftpIpHeader->IPv4Hdr.ip_src, sizeof(pTftpSession->IpClientAddress)))
387 {
388 if (pTftpSession->u16ClientPort == pcTftpIpHeader->UdpHdr.uh_sport)
389 {
390 *ppTftpSessions = pTftpSession;
391 return VINF_SUCCESS;
392 }
393 }
394 }
395 }
396
397 return VERR_NOT_FOUND;
398}
399
400DECLINLINE(int) pftpSessionOpenFile(PNATState pData, PTFTPSESSION pTftpSession, PRTFILE pSessionFile)
401{
402 char aszSessionFileName[TFTP_FILENAME_MAX];
403 size_t cbSessionFileName;
404 int rc = VINF_SUCCESS;
405 LogFlowFuncEnter();
406 cbSessionFileName = RTStrPrintf(aszSessionFileName, TFTP_FILENAME_MAX, "%s/%s",
407 tftp_prefix, pTftpSession->pszFilename);
408 if (cbSessionFileName >= TFTP_FILENAME_MAX)
409 {
410 LogFlowFuncLeaveRC(VERR_INTERNAL_ERROR);
411 return VERR_INTERNAL_ERROR;
412 }
413 LogFunc(("aszSessionFileName: %s\n", aszSessionFileName));
414
415 if (!RTFileExists(aszSessionFileName))
416 {
417 LogFlowFuncLeaveRC(VERR_FILE_NOT_FOUND);
418 return VERR_FILE_NOT_FOUND;
419 }
420
421 rc = RTFileOpen(pSessionFile, aszSessionFileName, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
422 LogFlowFuncLeaveRC(rc);
423 return rc;
424}
425
426DECLINLINE(int) tftpSessionEvaluateOptions(PNATState pData, PTFTPSESSION pTftpSession)
427{
428 int rc = VINF_SUCCESS;
429 RTFILE hSessionFile;
430 uint64_t cbSessionFile = 0;
431 LogFlowFunc(("pTftpSession:%p\n", pTftpSession));
432
433 rc = pftpSessionOpenFile(pData, pTftpSession, &hSessionFile);
434 if (RT_FAILURE(rc))
435 {
436 LogFlowFuncLeave();
437 return rc;
438 }
439
440 rc = RTFileGetSize(hSessionFile, &cbSessionFile);
441 RTFileClose(hSessionFile);
442 if (RT_FAILURE(rc))
443 {
444 LogFlowFuncLeave();
445 return rc;
446 }
447
448 if (pTftpSession->OptionTSize.fRequested)
449 {
450 pTftpSession->OptionTSize.u64Value = cbSessionFile;
451 }
452 if ( !pTftpSession->OptionBlkSize.u64Value
453 && !pTftpSession->OptionBlkSize.fRequested)
454 {
455 pTftpSession->OptionBlkSize.u64Value = 1428;
456 }
457 LogFlowFuncLeaveRC(rc);
458 return rc;
459}
460
461DECLINLINE(int) tftpSend(PNATState pData,
462 PTFTPSESSION pTftpSession,
463 struct mbuf *pMBuf,
464 PCTFTPIPHDR pcTftpIpHeaderRecv)
465{
466 int rc = VINF_SUCCESS;
467 struct sockaddr_in saddr, daddr;
468 LogFlowFunc(("pMBuf:%p, pcTftpIpHeaderRecv:%p\n", pMBuf, pcTftpIpHeaderRecv));
469 saddr.sin_addr = pcTftpIpHeaderRecv->IPv4Hdr.ip_dst;
470 saddr.sin_port = pcTftpIpHeaderRecv->UdpHdr.uh_dport;
471
472 daddr.sin_addr = pTftpSession->IpClientAddress;
473 daddr.sin_port = pTftpSession->u16ClientPort;
474
475
476 pMBuf->m_data += sizeof(struct udpiphdr);
477 pMBuf->m_len -= sizeof(struct udpiphdr);
478 udp_output2(pData, NULL, pMBuf, &saddr, &daddr, IPTOS_LOWDELAY);
479 LogFlowFuncLeaveRC(rc);
480 return rc;
481}
482DECLINLINE(int) tftpSendError(PNATState pData, PTFTPSESSION pTftpSession, uint16_t errorcode, const char *msg, PCTFTPIPHDR pcTftpIpHeaderRecv);
483
484DECLINLINE(int) tftpReadDataBlock(PNATState pData,
485 PTFTPSESSION pcTftpSession,
486 uint8_t *pu8Data,
487 int *pcbReadData)
488{
489 RTFILE hSessionFile;
490 int rc = VINF_SUCCESS;
491 uint16_t u16BlkSize = 0;
492 AssertPtrReturn(pData, VERR_INVALID_PARAMETER);
493 AssertPtrReturn(pcTftpSession, VERR_INVALID_PARAMETER);
494 AssertPtrReturn(pu8Data, VERR_INVALID_PARAMETER);
495 AssertPtrReturn(pcbReadData, VERR_INVALID_PARAMETER);
496 AssertReturn(pcTftpSession->OptionBlkSize.u64Value < UINT16_MAX, VERR_INVALID_PARAMETER);
497 LogFlowFunc(("pcTftpSession:%p, pu8Data:%p, pcbReadData:%p\n",
498 pcTftpSession,
499 pu8Data,
500 pcbReadData));
501
502 u16BlkSize = (uint16_t)pcTftpSession->OptionBlkSize.u64Value;
503 rc = pftpSessionOpenFile(pData, pcTftpSession, &hSessionFile);
504 if (RT_FAILURE(rc))
505 {
506 LogFlowFuncLeaveRC(rc);
507 return rc;
508 }
509
510 if (pcbReadData)
511 {
512 rc = RTFileSeek(hSessionFile,
513 pcTftpSession->cbTransfered,
514 RTFILE_SEEK_BEGIN,
515 NULL);
516 if (RT_FAILURE(rc))
517 {
518 RTFileClose(hSessionFile);
519 LogFlowFuncLeaveRC(rc);
520 return rc;
521 }
522 rc = RTFileRead(hSessionFile, pu8Data, u16BlkSize, (size_t *)pcbReadData);
523 if (RT_FAILURE(rc))
524 {
525 RTFileClose(hSessionFile);
526 LogFlowFuncLeaveRC(rc);
527 return rc;
528 }
529 }
530
531 rc = RTFileClose(hSessionFile);
532
533 LogFlowFuncLeaveRC(rc);
534 return rc;
535}
536
537DECLINLINE(int) tftpAddOptionToOACK(PNATState pData, struct mbuf *pMBuf, const char *pszOptName, uint64_t u64OptValue)
538{
539 char aszOptionBuffer[256];
540 size_t iOptLength = 0;
541 int rc = VINF_SUCCESS;
542 int cbMBufCurrent = pMBuf->m_len;
543 LogFlowFunc(("pMBuf:%p, pszOptName:%s, u16OptValue:%ld\n", pMBuf, pszOptName, u64OptValue));
544 AssertPtrReturn(pMBuf, VERR_INVALID_PARAMETER);
545 AssertPtrReturn(pszOptName, VERR_INVALID_PARAMETER);
546
547 RT_ZERO(aszOptionBuffer);
548 iOptLength += RTStrPrintf(aszOptionBuffer, 256 , "%s", pszOptName) + 1;
549 iOptLength += RTStrPrintf(aszOptionBuffer + iOptLength, 256 - iOptLength , "%llu", u64OptValue) + 1;
550 if (iOptLength > M_TRAILINGSPACE(pMBuf))
551 rc = VERR_BUFFER_OVERFLOW; /* buffer too small */
552 else
553 {
554 pMBuf->m_len += iOptLength;
555 m_copyback(pData, pMBuf, cbMBufCurrent, iOptLength, aszOptionBuffer);
556 }
557 LogFlowFuncLeaveRC(rc);
558 return rc;
559}
560
561DECLINLINE(int) tftpSendOACK(PNATState pData,
562 PTFTPSESSION pTftpSession,
563 PCTFTPIPHDR pcTftpIpHeaderRecv)
564{
565 struct mbuf *m;
566 PTFTPIPHDR pTftpIpHeader;
567 int rc = VINF_SUCCESS;
568
569 rc = tftpSessionEvaluateOptions(pData, pTftpSession);
570 if (RT_FAILURE(rc))
571 {
572 tftpSendError(pData, pTftpSession, 2, "Internal Error (blksize evaluation)", pcTftpIpHeaderRecv);
573 LogFlowFuncLeave();
574 return -1;
575 }
576
577 m = slirpTftpMbufAlloc(pData);
578 if (!m)
579 return -1;
580
581
582
583 m->m_data += if_maxlinkhdr;
584 m->m_pkthdr.header = mtod(m, void *);
585 pTftpIpHeader = mtod(m, PTFTPIPHDR);
586 m->m_len = sizeof(TFTPIPHDR) - sizeof(uint16_t); /* no u16TftpOpCode */
587
588 pTftpIpHeader->u16TftpOpType = RT_H2N_U16_C(TFTP_OACK);
589
590 if (pTftpSession->OptionBlkSize.fRequested)
591 {
592 if (pTftpSession->OptionBlkSize.u64Value > UINT16_MAX)
593 rc = VERR_INVALID_PARAMETER;
594 else
595 rc = tftpAddOptionToOACK(pData, m, "blksize", pTftpSession->OptionBlkSize.u64Value);
596 }
597 if ( RT_SUCCESS(rc)
598 && pTftpSession->OptionTSize.fRequested)
599 rc = tftpAddOptionToOACK(pData, m, "tsize", pTftpSession->OptionTSize.u64Value);
600
601 rc = tftpSend(pData, pTftpSession, m, pcTftpIpHeaderRecv);
602 return RT_SUCCESS(rc) ? 0 : -1;
603}
604
605DECLINLINE(int) tftpSendError(PNATState pData,
606 PTFTPSESSION pTftpSession,
607 uint16_t errorcode,
608 const char *msg,
609 PCTFTPIPHDR pcTftpIpHeaderRecv)
610{
611 struct mbuf *m = NULL;
612 PTFTPIPHDR pTftpIpHeader = NULL;
613
614 LogFlowFunc(("ENTER: errorcode: %RX16, msg: %s\n", errorcode, msg));
615 m = slirpTftpMbufAlloc(pData);
616 if (!m)
617 {
618 LogFlowFunc(("LEAVE: Can't allocate mbuf\n"));
619 return -1;
620 }
621
622 m->m_data += if_maxlinkhdr;
623 m->m_len = sizeof(TFTPIPHDR)
624 + strlen(msg) + 1; /* ending zero */
625 m->m_pkthdr.header = mtod(m, void *);
626 pTftpIpHeader = mtod(m, PTFTPIPHDR);
627
628 pTftpIpHeader->u16TftpOpType = RT_H2N_U16_C(TFTP_ERROR);
629 pTftpIpHeader->Core.u16TftpOpCode = RT_H2N_U16(errorcode);
630
631 m_copyback(pData, m, sizeof(TFTPIPHDR), strlen(msg) + 1 /* copy ending zerro*/, (c_caddr_t)msg);
632
633 tftpSend(pData, pTftpSession, m, pcTftpIpHeaderRecv);
634
635 tftpSessionTerminate(pTftpSession);
636
637 LogFlowFuncLeave();
638 return 0;
639}
640
641static int tftpSendData(PNATState pData,
642 PTFTPSESSION pTftpSession,
643 uint16_t u16Block,
644 PCTFTPIPHDR pcTftpIpHeaderRecv)
645{
646 struct mbuf *m;
647 PTFTPIPHDR pTftpIpHeader;
648 int cbRead = 0;
649 int rc = VINF_SUCCESS;
650
651 if (u16Block == pTftpSession->cTftpAck)
652 pTftpSession->cTftpAck++;
653 else
654 {
655 tftpSendError(pData, pTftpSession, 6, "ACK is wrong", pcTftpIpHeaderRecv);
656 tftpSessionTerminate(pTftpSession);
657 return -1;
658 }
659
660 m = slirpTftpMbufAlloc(pData);
661 if (!m)
662 return -1;
663
664 m->m_data += if_maxlinkhdr;
665 m->m_pkthdr.header = mtod(m, void *);
666 pTftpIpHeader = mtod(m, PTFTPIPHDR);
667 m->m_len = sizeof(TFTPIPHDR);
668
669 pTftpIpHeader->u16TftpOpType = RT_H2N_U16_C(TFTP_DATA);
670 pTftpIpHeader->Core.u16TftpOpCode = RT_H2N_U16(pTftpSession->cTftpAck);
671
672 rc = tftpReadDataBlock(pData, pTftpSession, (uint8_t *)&pTftpIpHeader->Core.u16TftpOpCode + sizeof(uint16_t), &cbRead);
673
674 if (RT_SUCCESS(rc))
675 {
676 pTftpSession->cbTransfered += cbRead;
677 m->m_len += cbRead;
678 tftpSend(pData, pTftpSession, m, pcTftpIpHeaderRecv);
679 if (cbRead > 0)
680 tftpSessionUpdate(pData, pTftpSession);
681 else
682 tftpSessionTerminate(pTftpSession);
683 }
684 else
685 {
686 m_freem(pData, m);
687 tftpSendError(pData, pTftpSession, 1, "File not found", pcTftpIpHeaderRecv);
688 /* send "file not found" error back */
689 return -1;
690 }
691
692 return 0;
693}
694
695DECLINLINE(void) tftpProcessRRQ(PNATState pData, PCTFTPIPHDR pTftpIpHeader, int pktlen)
696{
697 PTFTPSESSION pTftpSession = NULL;
698 uint8_t *pu8Payload = NULL;
699 int cbPayload = 0;
700 size_t cbFileName = 0;
701 int rc = VINF_SUCCESS;
702
703 AssertPtrReturnVoid(pTftpIpHeader);
704 AssertPtrReturnVoid(pData);
705 AssertReturnVoid(pktlen > sizeof(TFTPIPHDR));
706 LogFlowFunc(("ENTER: pTftpIpHeader:%p, pktlen:%d\n", pTftpIpHeader, pktlen));
707
708 rc = tftpAllocateSession(pData, pTftpIpHeader, &pTftpSession);
709 if ( RT_FAILURE(rc)
710 || pTftpSession == NULL)
711 {
712 LogFlowFuncLeave();
713 return;
714 }
715
716 pu8Payload = (uint8_t *)&pTftpIpHeader->Core;
717 cbPayload = pktlen - sizeof(TFTPIPHDR);
718
719 cbFileName = RTStrNLen((char *)pu8Payload, cbPayload);
720 /* We assume that file name should finish with '\0' and shouldn't bigger
721 * than buffer for name storage.
722 */
723 AssertReturnVoid( cbFileName < cbPayload
724 && cbFileName < TFTP_FILENAME_MAX /* current limit in tftp session handle */
725 && cbFileName);
726
727 /* Dont't bother with rest processing in case of invalid access */
728 if (RT_FAILURE(tftpSecurityFilenameCheck(pData, pTftpSession)))
729 {
730 tftpSendError(pData, pTftpSession, 2, "Access violation", pTftpIpHeader);
731 LogFlowFuncLeave();
732 return;
733 }
734
735
736
737 if (RT_UNLIKELY(!tftpIsSupportedTransferMode(pTftpSession)))
738 {
739 tftpSendError(pData, pTftpSession, 4, "Unsupported transfer mode", pTftpIpHeader);
740 LogFlowFuncLeave();
741 return;
742 }
743
744
745 tftpSendOACK(pData, pTftpSession, pTftpIpHeader);
746 LogFlowFuncLeave();
747 return;
748}
749
750static void tftpProcessACK(PNATState pData, PTFTPIPHDR pTftpIpHeader)
751{
752 int rc;
753 PTFTPSESSION pTftpSession = NULL;
754
755 rc = tftpSessionFind(pData, pTftpIpHeader, &pTftpSession);
756 if (RT_FAILURE(rc))
757 return;
758
759 AssertReturnVoid(tftpSendData(pData,
760 pTftpSession,
761 RT_N2H_U16(pTftpIpHeader->Core.u16TftpOpCode), pTftpIpHeader) == 0);
762}
763
764int slirpTftpInit(PNATState pData)
765{
766 AssertPtrReturn(pData, VERR_INVALID_PARAMETER);
767 pData->pvTftpSessions = RTMemAllocZ(sizeof(TFTPSESSION) * TFTP_SESSIONS_MAX);
768 AssertPtrReturn(pData->pvTftpSessions, VERR_NO_MEMORY);
769 return VINF_SUCCESS;
770}
771
772void slirpTftpTerm(PNATState pData)
773{
774 RTMemFree(pData->pvTftpSessions);
775}
776
777int slirpTftpInput(PNATState pData, struct mbuf *pMbuf)
778{
779 PTFTPIPHDR pTftpIpHeader = NULL;
780 AssertPtr(pData);
781 AssertPtr(pMbuf);
782 pTftpIpHeader = mtod(pMbuf, PTFTPIPHDR);
783
784 switch(RT_N2H_U16(pTftpIpHeader->u16TftpOpType))
785 {
786 case TFTP_RRQ:
787 tftpProcessRRQ(pData, pTftpIpHeader, m_length(pMbuf, NULL));
788 break;
789
790 case TFTP_ACK:
791 tftpProcessACK(pData, pTftpIpHeader);
792 break;
793 default:;
794 }
795 LogFlowFuncLeaveRC(VINF_SUCCESS);
796 return VINF_SUCCESS;
797}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette