VirtualBox

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

Last change on this file since 80980 was 80980, checked in by vboxsync, 5 years ago

NAT: Make sure tftpSendError() always terminate the session.

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