VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DrvNAT.cpp@ 14036

Last change on this file since 14036 was 14036, checked in by vboxsync, 16 years ago

win compile fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.2 KB
Line 
1/** @file
2 *
3 * VBox network devices:
4 * NAT network transport driver
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#define LOG_GROUP LOG_GROUP_DRV_NAT
28#define __STDC_LIMIT_MACROS
29#define __STDC_CONSTANT_MACROS
30#include "Network/slirp/libslirp.h"
31#include <VBox/pdmdrv.h>
32#include <iprt/assert.h>
33#include <iprt/file.h>
34#include <iprt/string.h>
35#include <iprt/critsect.h>
36#include <iprt/cidr.h>
37#include <iprt/stream.h>
38
39#include "Builtins.h"
40
41#ifdef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
42# ifndef RT_OS_WINDOWS
43# include <unistd.h>
44# endif
45# include <errno.h>
46# include<iprt/semaphore.h>
47#endif
48
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53/**
54 * NAT network transport driver instance data.
55 */
56typedef struct DRVNAT
57{
58 /** The network interface. */
59 PDMINETWORKCONNECTOR INetworkConnector;
60 /** The port we're attached to. */
61 PPDMINETWORKPORT pPort;
62 /** The network config of the port we're attached to. */
63 PPDMINETWORKCONFIG pConfig;
64 /** Pointer to the driver instance. */
65 PPDMDRVINS pDrvIns;
66#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
67 /** Slirp critical section. */
68 RTCRITSECT CritSect;
69#endif
70 /** Link state */
71 PDMNETWORKLINKSTATE enmLinkState;
72 /** NAT state for this instance. */
73 PNATState pNATState;
74 /** TFTP directory prefix. */
75 char *pszTFTPPrefix;
76 /** Boot file name to provide in the DHCP server response. */
77 char *pszBootFile;
78#ifdef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
79 /*polling thread*/
80 PPDMTHREAD pThread;
81 /*used for wakep of poling thread*/
82 RTSEMEVENT semSndMutex;
83#ifndef RT_OS_WINDOWS
84 /** The write end of the control pipe. */
85 RTFILE PipeWrite;
86 /** The read end of the control pipe. */
87 RTFILE PipeRead;
88#else
89 /* 1 - for Outher network events, and 0 for sending routine notification*/
90 HANDLE hNetworkEvent[2];
91#endif
92 /** Send buffer */
93 char cBuffer[1600];
94 size_t sBufferSize;
95#endif
96} DRVNAT, *PDRVNAT;
97
98/** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
99#define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
100
101
102
103/**
104 * Send data to the network.
105 *
106 * @returns VBox status code.
107 * @param pInterface Pointer to the interface structure containing the called function pointer.
108 * @param pvBuf Data to send.
109 * @param cb Number of bytes to send.
110 * @thread EMT
111 */
112static DECLCALLBACK(int) drvNATSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
113{
114 PDRVNAT pThis = PDMINETWORKCONNECTOR_2_DRVNAT(pInterface);
115
116 LogFlow(("drvNATSend: pvBuf=%p cb=%#x\n", pvBuf, cb));
117 Log2(("drvNATSend: pvBuf=%p cb=%#x\n%.*Rhxd\n", pvBuf, cb, cb, pvBuf));
118
119#ifdef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
120
121 /*notify select to wakeup*/
122 memcpy(pThis->cBuffer,pvBuf, cb);
123 pThis->sBufferSize = cb;
124# ifndef RT_OS_WINDOWS
125 int rc = RTFileWrite(pThis->PipeWrite, "1", 2, NULL);
126 AssertRC(rc);
127# else
128 WSASetEvent(pThis->hNetworkEvent[0]);
129# endif
130 RTSemEventWait(pThis->semSndMutex, RT_INDEFINITE_WAIT);
131
132#else /* ! VBOX_WITH_SIMPLEFIED_SLIRP_SYNC */
133
134 int rc = RTCritSectEnter(&pThis->CritSect);
135 AssertReleaseRC(rc);
136
137 Assert(pThis->enmLinkState == PDMNETWORKLINKSTATE_UP);
138 if (pThis->enmLinkState == PDMNETWORKLINKSTATE_UP)
139 slirp_input(pThis->pNATState, (uint8_t *)pvBuf, cb);
140
141 RTCritSectLeave(&pThis->CritSect);
142
143#endif /* !VBOX_WITH_SIMPLEFIED_SLIRP_SYNC */
144
145 LogFlow(("drvNATSend: end\n"));
146 return VINF_SUCCESS;
147}
148
149
150/**
151 * Set promiscuous mode.
152 *
153 * This is called when the promiscuous mode is set. This means that there doesn't have
154 * to be a mode change when it's called.
155 *
156 * @param pInterface Pointer to the interface structure containing the called function pointer.
157 * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
158 * @thread EMT
159 */
160static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
161{
162 LogFlow(("drvNATSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
163 /* nothing to do */
164}
165
166
167/**
168 * Notification on link status changes.
169 *
170 * @param pInterface Pointer to the interface structure containing the called function pointer.
171 * @param enmLinkState The new link state.
172 * @thread EMT
173 */
174static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
175{
176 PDRVNAT pThis = PDMINETWORKCONNECTOR_2_DRVNAT(pInterface);
177
178 LogFlow(("drvNATNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
179
180#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
181 int rc = RTCritSectEnter(&pThis->CritSect);
182 AssertReleaseRC(rc);
183#endif
184 pThis->enmLinkState = enmLinkState;
185
186 switch (enmLinkState)
187 {
188 case PDMNETWORKLINKSTATE_UP:
189 LogRel(("NAT: link up\n"));
190 slirp_link_up(pThis->pNATState);
191 break;
192
193 case PDMNETWORKLINKSTATE_DOWN:
194 case PDMNETWORKLINKSTATE_DOWN_RESUME:
195 LogRel(("NAT: link down\n"));
196 slirp_link_down(pThis->pNATState);
197 break;
198
199 default:
200 AssertMsgFailed(("drvNATNotifyLinkChanged: unexpected link state %d\n", enmLinkState));
201 }
202#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
203 RTCritSectLeave(&pThis->CritSect);
204#endif
205}
206
207
208#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
209/**
210 * Poller callback.
211 */
212static DECLCALLBACK(void) drvNATPoller(PPDMDRVINS pDrvIns)
213{
214 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
215 fd_set ReadFDs;
216 fd_set WriteFDs;
217 fd_set XcptFDs;
218 int nFDs = -1;
219 FD_ZERO(&ReadFDs);
220 FD_ZERO(&WriteFDs);
221 FD_ZERO(&XcptFDs);
222
223 int rc = RTCritSectEnter(&pThis->CritSect);
224 AssertReleaseRC(rc);
225
226 slirp_select_fill(pThis->pNATState, &nFDs, &ReadFDs, &WriteFDs, &XcptFDs);
227
228 struct timeval tv = {0, 0}; /* no wait */
229 int cChangedFDs = select(nFDs + 1, &ReadFDs, &WriteFDs, &XcptFDs, &tv);
230 if (cChangedFDs >= 0)
231 slirp_select_poll(pThis->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
232
233 RTCritSectLeave(&pThis->CritSect);
234}
235#else
236
237static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
238{
239 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
240 fd_set ReadFDs;
241 fd_set WriteFDs;
242 fd_set XcptFDs;
243 int nFDs = -1;
244 int rc;
245# ifdef RT_OS_WINDOWS
246 DWORD event;
247 HANDLE *phEvents;
248# endif
249 const struct timeval TimeWait = { 0, 2000 }; /* 2ms for the fast timer */
250 const struct timeval TimeNoWait = { 0, 0 }; /* return immediately */
251
252 LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
253
254 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
255 return VINF_SUCCESS;
256
257 /*
258 * Polling loop.
259 */
260 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
261 {
262 bool fWait = true;
263
264 FD_ZERO(&ReadFDs);
265 FD_ZERO(&WriteFDs);
266 FD_ZERO(&XcptFDs);
267 nFDs = -1;
268
269 /*
270 * To prevent concurent execution of sending/receving threads
271 */
272 slirp_select_fill(pThis->pNATState, &nFDs, &ReadFDs, &WriteFDs, &XcptFDs);
273# ifndef RT_OS_WINDOWS
274 struct timeval tv = fWait ? TimeWait : TimeNoWait;
275 FD_SET(pThis->PipeRead, &ReadFDs); /* Linux only */
276 nFDs = ((int)pThis->PipeRead < nFDs ? nFDs : pThis->PipeRead);
277 int cChangedFDs = select(nFDs + 1, &ReadFDs, &WriteFDs, &XcptFDs, &tv);
278 if (cChangedFDs >= 0)
279 {
280 slirp_select_poll(pThis->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
281 if (FD_ISSET(pThis->PipeRead, &ReadFDs))
282 {
283 /* drain the pipe */
284 char ch[2];
285 size_t cbRead;
286 RTFileRead(pThis->PipeRead, &ch, 2, &cbRead);
287 switch (ch[0])
288 {
289 case '1':
290 slirp_input(pThis->pNATState, (uint8_t *)pThis->cBuffer, pThis->sBufferSize);
291 RTSemEventSignal(pThis->semSndMutex);
292 fWait = 1;
293 break;
294 case '2':
295 break;
296 }
297 }
298 }
299# else /* RT_OS_WINDOWS */
300 phEvents = slirp_get_events(pThis->pNATState);
301 phEvents[0] = pThis->hNetworkEvent[0];
302 event = WSAWaitForMultipleEvents(nFDs, phEvents, FALSE, 2, FALSE);
303 AssertRelease(event != WSA_WAIT_FAILED);
304
305 /*
306 * see WSAWaitForMultipleEvents documentation: return value is a minimal index in array
307 */
308 if ((event - WSA_WAIT_EVENT_0) > 1)
309 slirp_select_poll(pThis->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
310
311 if ((event - WSA_WAIT_EVENT_0) == 0)
312 {
313 slirp_input(pThis->pNATState, (uint8_t *)pThis->cBuffer, pThis->sBufferSize);
314 WSAResetEvent(pThis->hNetworkEvent[0]);
315 RTSemEventSignal(pThis->semSndMutex);
316 }
317 WSAResetEvent(pThis->hNetworkEvent[0]);
318# endif /* RT_OS_WINDOWS */
319 }
320
321 return VINF_SUCCESS;
322}
323
324 /**
325 * Unblock the send thread so it can respond to a state change.
326 *
327 * @returns VBox status code.
328 * @param pDevIns The pcnet device instance.
329 * @param pThread The send thread.
330 */
331static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
332{
333 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
334
335# ifndef RT_OS_WINDOWS
336 int rc = RTFileWrite(pThis->PipeWrite, "2", 2, NULL);
337 AssertRC(rc);
338 RTSemEventSignal(pThis->semSndMutex);
339#endif
340 return VINF_SUCCESS;
341}
342
343#endif
344
345/**
346 * Function called by slirp to check if it's possible to feed incoming data to the network port.
347 * @returns 1 if possible.
348 * @returns 0 if not possible.
349 */
350int slirp_can_output(void *pvUser)
351{
352 PDRVNAT pThis = (PDRVNAT)pvUser;
353
354 Assert(pThis);
355
356#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
357 /** Happens during termination */
358 if (!RTCritSectIsOwner(&pThis->CritSect))
359 return 0;
360#endif
361
362 int rc = pThis->pPort->pfnWaitReceiveAvail(pThis->pPort, 0);
363 return RT_SUCCESS(rc);
364}
365
366
367/**
368 * Function called by slirp to feed incoming data to the network port.
369 */
370void slirp_output(void *pvUser, const uint8_t *pu8Buf, int cb)
371{
372 PDRVNAT pThis = (PDRVNAT)pvUser;
373
374 LogFlow(("slirp_output BEGIN %x %d\n", pu8Buf, cb));
375 Log2(("slirp_output: pu8Buf=%p cb=%#x (pThis=%p)\n%.*Rhxd\n", pu8Buf, cb, pThis, cb, pu8Buf));
376
377 Assert(pThis);
378
379#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
380 /** Happens during termination */
381 if (!RTCritSectIsOwner(&pThis->CritSect))
382 return;
383#endif
384
385 int rc = pThis->pPort->pfnReceive(pThis->pPort, pu8Buf, cb);
386 AssertRC(rc);
387 LogFlow(("slirp_output END %x %d\n", pu8Buf, cb));
388}
389
390/**
391 * Queries an interface to the driver.
392 *
393 * @returns Pointer to interface.
394 * @returns NULL if the interface was not supported by the driver.
395 * @param pInterface Pointer to this interface structure.
396 * @param enmInterface The requested interface identification.
397 * @thread Any thread.
398 */
399static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
400{
401 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
402 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
403 switch (enmInterface)
404 {
405 case PDMINTERFACE_BASE:
406 return &pDrvIns->IBase;
407 case PDMINTERFACE_NETWORK_CONNECTOR:
408 return &pThis->INetworkConnector;
409 default:
410 return NULL;
411 }
412}
413
414
415/**
416 * Destruct a driver instance.
417 *
418 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
419 * resources can be freed correctly.
420 *
421 * @param pDrvIns The driver instance data.
422 */
423static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
424{
425 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
426
427 LogFlow(("drvNATDestruct:\n"));
428
429#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
430 int rc = RTCritSectEnter(&pThis->CritSect);
431 AssertReleaseRC(rc);
432#endif
433 slirp_term(pThis->pNATState);
434 pThis->pNATState = NULL;
435#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
436 RTCritSectLeave(&pThis->CritSect);
437
438 RTCritSectDelete(&pThis->CritSect);
439#else
440 RTSemEventDestroy(pThis->semSndMutex);
441#endif
442}
443
444
445/**
446 * Sets up the redirectors.
447 *
448 * @returns VBox status code.
449 * @param pCfgHandle The drivers configuration handle.
450 */
451static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfgHandle, RTIPV4ADDR Network)
452{
453 /*
454 * Enumerate redirections.
455 */
456 for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfgHandle); pNode; pNode = CFGMR3GetNextChild(pNode))
457 {
458 /*
459 * Validate the port forwarding config.
460 */
461 if (!CFGMR3AreValuesValid(pNode, "Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0"))
462 return PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown configuration in port forwarding"));
463
464 /* protocol type */
465 bool fUDP;
466 char szProtocol[32];
467 int rc = CFGMR3QueryString(pNode, "Protocol", &szProtocol[0], sizeof(szProtocol));
468 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
469 {
470 rc = CFGMR3QueryBool(pNode, "UDP", &fUDP);
471 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
472 fUDP = false;
473 else if (RT_FAILURE(rc))
474 return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"UDP\" boolean failed"), iInstance);
475 }
476 else if (RT_SUCCESS(rc))
477 {
478 if (!RTStrICmp(szProtocol, "TCP"))
479 fUDP = false;
480 else if (!RTStrICmp(szProtocol, "UDP"))
481 fUDP = true;
482 else
483 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""), iInstance, szProtocol);
484 }
485 else
486 return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Protocol\" string failed"), iInstance);
487
488 /* host port */
489 int32_t iHostPort;
490 rc = CFGMR3QueryS32(pNode, "HostPort", &iHostPort);
491 if (RT_FAILURE(rc))
492 return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"HostPort\" integer failed"), iInstance);
493
494 /* guest port */
495 int32_t iGuestPort;
496 rc = CFGMR3QueryS32(pNode, "GuestPort", &iGuestPort);
497 if (RT_FAILURE(rc))
498 return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestPort\" integer failed"), iInstance);
499
500 /* guest address */
501 char szGuestIP[32];
502 rc = CFGMR3QueryString(pNode, "GuestIP", &szGuestIP[0], sizeof(szGuestIP));
503 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
504 RTStrPrintf(szGuestIP, sizeof(szGuestIP), "%d.%d.%d.%d",
505 (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, (Network & 0xE0) | 15);
506 else if (RT_FAILURE(rc))
507 return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestIP\" string failed"), iInstance);
508 struct in_addr GuestIP;
509 if (!inet_aton(szGuestIP, &GuestIP))
510 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_GUEST_IP, RT_SRC_POS,
511 N_("NAT#%d: configuration error: invalid \"GuestIP\"=\"%s\", inet_aton failed"), iInstance, szGuestIP);
512
513 /*
514 * Call slirp about it.
515 */
516 Log(("drvNATConstruct: Redir %d -> %s:%d\n", iHostPort, szGuestIP, iGuestPort));
517 if (slirp_redir(pThis->pNATState, fUDP, iHostPort, GuestIP, iGuestPort) < 0)
518 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS,
519 N_("NAT#%d: configuration error: failed to set up redirection of %d to %s:%d. Probably a conflict with existing services or other rules"), iInstance, iHostPort, szGuestIP, iGuestPort);
520 } /* for each redir rule */
521
522 return VINF_SUCCESS;
523}
524
525/**
526 * Get the MAC address into the slirp stack.
527 */
528static void drvNATSetMac(PDRVNAT pThis)
529{
530 if (pThis->pConfig)
531 {
532 RTMAC Mac;
533 pThis->pConfig->pfnGetMac(pThis->pConfig, &Mac);
534 slirp_set_ethaddr(pThis->pNATState, Mac.au8);
535 }
536}
537
538
539/**
540 * After loading we have to pass the MAC address of the ethernet device to the slirp stack.
541 * Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
542 * (usually done during guest boot).
543 */
544static DECLCALLBACK(int) drvNATLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSMHandle)
545{
546 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
547 drvNATSetMac(pThis);
548 return VINF_SUCCESS;
549}
550
551
552/**
553 * Some guests might not use DHCP to retrieve an IP but use a static IP.
554 */
555static DECLCALLBACK(void) drvNATPowerOn(PPDMDRVINS pDrvIns)
556{
557 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
558 drvNATSetMac(pThis);
559}
560
561
562/**
563 * Construct a NAT network transport driver instance.
564 *
565 * @returns VBox status.
566 * @param pDrvIns The driver instance data.
567 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
568 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
569 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
570 * iInstance it's expected to be used a bit in this function.
571 */
572static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
573{
574 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
575 char szNetAddr[16];
576 char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
577 LogFlow(("drvNATConstruct:\n"));
578
579 /*
580 * Validate the config.
581 */
582 if (!CFGMR3AreValuesValid(pCfgHandle, "PassDomain\0TFTPPrefix\0BootFile\0Network\0"))
583 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown NAT configuration option, only supports PassDomain, TFTPPrefix, BootFile and Network"));
584
585 /*
586 * Init the static parts.
587 */
588 pThis->pDrvIns = pDrvIns;
589 pThis->pNATState = NULL;
590 pThis->pszTFTPPrefix = NULL;
591 pThis->pszBootFile = NULL;
592 /* IBase */
593 pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
594 /* INetwork */
595 pThis->INetworkConnector.pfnSend = drvNATSend;
596 pThis->INetworkConnector.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
597 pThis->INetworkConnector.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
598
599 /*
600 * Get the configuration settings.
601 */
602 bool fPassDomain = true;
603 int rc = CFGMR3QueryBool(pCfgHandle, "PassDomain", &fPassDomain);
604 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
605 fPassDomain = true;
606 else if (RT_FAILURE(rc))
607 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"PassDomain\" boolean failed"), pDrvIns->iInstance);
608
609 rc = CFGMR3QueryStringAlloc(pCfgHandle, "TFTPPrefix", &pThis->pszTFTPPrefix);
610 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
611 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"TFTPPrefix\" string failed"), pDrvIns->iInstance);
612 rc = CFGMR3QueryStringAlloc(pCfgHandle, "BootFile", &pThis->pszBootFile);
613 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
614 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"BootFile\" string failed"), pDrvIns->iInstance);
615
616 /*
617 * Query the network port interface.
618 */
619 pThis->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
620 if (!pThis->pPort)
621 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
622 N_("Configuration error: the above device/driver didn't export the network port interface"));
623 pThis->pConfig = (PPDMINETWORKCONFIG)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_CONFIG);
624 if (!pThis->pConfig)
625 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
626 N_("Configuration error: the above device/driver didn't export the network config interface"));
627
628 /* Generate a network address for this network card. */
629 rc = CFGMR3QueryString(pCfgHandle, "Network", szNetwork, sizeof(szNetwork));
630 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
631 RTStrPrintf(szNetwork, sizeof(szNetwork), "10.0.%d.0/24", pDrvIns->iInstance + 2);
632 else if (RT_FAILURE(rc))
633 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Network\" string failed"), pDrvIns->iInstance);
634
635 RTIPV4ADDR Network;
636 RTIPV4ADDR Netmask;
637 rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
638 if (RT_FAILURE(rc))
639 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: network '%s' describes not a valid IPv4 network"), pDrvIns->iInstance, szNetwork);
640
641 RTStrPrintf(szNetAddr, sizeof(szNetAddr), "%d.%d.%d.%d",
642 (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, Network & 0xFF);
643
644 /*
645 * The slirp lock..
646 */
647#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
648 rc = RTCritSectInit(&pThis->CritSect);
649 if (RT_FAILURE(rc))
650 return rc;
651#endif
652 /*
653 * Initialize slirp.
654 */
655 rc = slirp_init(&pThis->pNATState, &szNetAddr[0], Netmask, fPassDomain, pThis->pszTFTPPrefix, pThis->pszBootFile, pThis);
656 if (RT_SUCCESS(rc))
657 {
658 int rc2 = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfgHandle, Network);
659 if (RT_SUCCESS(rc2))
660 {
661 /*
662 * Register a load done notification to get the MAC address into the slirp
663 * engine after we loaded a guest state.
664 */
665 rc2 = PDMDrvHlpSSMRegister(pDrvIns, pDrvIns->pDrvReg->szDriverName,
666 pDrvIns->iInstance, 0, 0,
667 NULL, NULL, NULL, NULL, NULL, drvNATLoadDone);
668 AssertRC(rc2);
669#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
670 pDrvIns->pDrvHlp->pfnPDMPollerRegister(pDrvIns, drvNATPoller);
671#else
672 rc = RTSemEventCreate(&pThis->semSndMutex);
673 AssertReleaseRC(rc);
674
675# ifndef RT_OS_WINDOWS
676 /*
677 * Create the control pipe.
678 */
679 int fds[2];
680 if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
681 {
682 int rc = RTErrConvertFromErrno(errno);
683 AssertRC(rc);
684 return rc;
685 }
686 pThis->PipeRead = fds[0];
687 pThis->PipeWrite = fds[1];
688# else
689 pThis->hNetworkEvent[0] = WSACreateEvent();
690 pThis->hNetworkEvent[1] = WSACreateEvent();
691# endif
692
693 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pThread, pThis, drvNATAsyncIoThread, drvNATAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "NAT");
694 AssertReleaseRC(rc);
695#endif
696
697 pThis->enmLinkState = PDMNETWORKLINKSTATE_UP;
698
699 /* might return VINF_NAT_DNS */
700 return rc;
701 }
702 /* failure path */
703 rc = rc2;
704 slirp_term(pThis->pNATState);
705 pThis->pNATState = NULL;
706 }
707 else
708 {
709 PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
710 AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
711 }
712
713#ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
714 RTCritSectDelete(&pThis->CritSect);
715#endif
716 return rc;
717}
718
719
720/**
721 * NAT network transport driver registration record.
722 */
723const PDMDRVREG g_DrvNAT =
724{
725 /* u32Version */
726 PDM_DRVREG_VERSION,
727 /* szDriverName */
728 "NAT",
729 /* pszDescription */
730 "NAT Network Transport Driver",
731 /* fFlags */
732 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
733 /* fClass. */
734 PDM_DRVREG_CLASS_NETWORK,
735 /* cMaxInstances */
736 16,
737 /* cbInstance */
738 sizeof(DRVNAT),
739 /* pfnConstruct */
740 drvNATConstruct,
741 /* pfnDestruct */
742 drvNATDestruct,
743 /* pfnIOCtl */
744 NULL,
745 /* pfnPowerOn */
746 drvNATPowerOn,
747 /* pfnReset */
748 NULL,
749 /* pfnSuspend */
750 NULL,
751 /* pfnResume */
752 NULL,
753 /* pfnDetach */
754 NULL,
755 /* pfnPowerOff */
756 NULL
757};
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