VirtualBox

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

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

timer updates on WSAWaitForMultipleEvents TIMEWAIT was added

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