VirtualBox

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

Last change on this file since 76775 was 76775, checked in by vboxsync, 6 years ago

NAT/tftp: (bugref:9350) Do not allow access to paths not starting with tftp_prefix.

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

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