VirtualBox

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

Last change on this file since 55371 was 55371, checked in by vboxsync, 10 years ago

DrvNat: logrel nit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 62.1 KB
Line 
1/* $Id: DrvNAT.cpp 55371 2015-04-22 11:22:39Z vboxsync $ */
2/** @file
3 * DrvNAT - NAT network transport driver.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_NAT
23#define __STDC_LIMIT_MACROS
24#define __STDC_CONSTANT_MACROS
25#include "slirp/libslirp.h"
26extern "C" {
27#include "slirp/slirp_dns.h"
28}
29#include "slirp/ctl.h"
30
31#include <VBox/vmm/dbgf.h>
32#include <VBox/vmm/pdmdrv.h>
33#include <VBox/vmm/pdmnetifs.h>
34#include <VBox/vmm/pdmnetinline.h>
35
36#include <iprt/assert.h>
37#include <iprt/critsect.h>
38#include <iprt/cidr.h>
39#include <iprt/file.h>
40#include <iprt/mem.h>
41#include <iprt/pipe.h>
42#include <iprt/string.h>
43#include <iprt/stream.h>
44#include <iprt/uuid.h>
45
46#include "VBoxDD.h"
47
48#ifndef RT_OS_WINDOWS
49# include <unistd.h>
50# include <fcntl.h>
51# include <poll.h>
52# include <errno.h>
53#endif
54#ifdef RT_OS_FREEBSD
55# include <netinet/in.h>
56#endif
57#include <iprt/semaphore.h>
58#include <iprt/req.h>
59#ifdef RT_OS_DARWIN
60# include <SystemConfiguration/SystemConfiguration.h>
61# include <CoreFoundation/CoreFoundation.h>
62#endif
63
64#define COUNTERS_INIT
65#include "counters.h"
66
67
68/*******************************************************************************
69* Defined Constants And Macros *
70*******************************************************************************/
71
72#define DRVNAT_MAXFRAMESIZE (16 * 1024)
73
74/**
75 * @todo: This is a bad hack to prevent freezing the guest during high network
76 * activity. Windows host only. This needs to be fixed properly.
77 */
78#define VBOX_NAT_DELAY_HACK
79
80#define GET_EXTRADATA(pthis, node, name, rc, type, type_name, var) \
81do { \
82 (rc) = CFGMR3Query ## type((node), name, &(var)); \
83 if (RT_FAILURE((rc)) && (rc) != VERR_CFGM_VALUE_NOT_FOUND) \
84 return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \"" name "\" " #type_name " failed"), \
85 (pthis)->pDrvIns->iInstance); \
86} while (0)
87
88#define GET_ED_STRICT(pthis, node, name, rc, type, type_name, var) \
89do { \
90 (rc) = CFGMR3Query ## type((node), name, &(var)); \
91 if (RT_FAILURE((rc))) \
92 return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \"" name "\" " #type_name " failed"), \
93 (pthis)->pDrvIns->iInstance); \
94} while (0)
95
96#define GET_EXTRADATA_N(pthis, node, name, rc, type, type_name, var, var_size) \
97do { \
98 (rc) = CFGMR3Query ## type((node), name, &(var), var_size); \
99 if (RT_FAILURE((rc)) && (rc) != VERR_CFGM_VALUE_NOT_FOUND) \
100 return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \"" name "\" " #type_name " failed"), \
101 (pthis)->pDrvIns->iInstance); \
102} while (0)
103
104#define GET_BOOL(rc, pthis, node, name, var) \
105 GET_EXTRADATA(pthis, node, name, (rc), Bool, bolean, (var))
106#define GET_STRING(rc, pthis, node, name, var, var_size) \
107 GET_EXTRADATA_N(pthis, node, name, (rc), String, string, (var), (var_size))
108#define GET_STRING_ALLOC(rc, pthis, node, name, var) \
109 GET_EXTRADATA(pthis, node, name, (rc), StringAlloc, string, (var))
110#define GET_S32(rc, pthis, node, name, var) \
111 GET_EXTRADATA(pthis, node, name, (rc), S32, int, (var))
112#define GET_S32_STRICT(rc, pthis, node, name, var) \
113 GET_ED_STRICT(pthis, node, name, (rc), S32, int, (var))
114
115
116
117#define DO_GET_IP(rc, node, instance, status, x) \
118do { \
119 char sz##x[32]; \
120 GET_STRING((rc), (node), (instance), #x, sz ## x[0], sizeof(sz ## x)); \
121 if (rc != VERR_CFGM_VALUE_NOT_FOUND) \
122 (status) = inet_aton(sz ## x, &x); \
123} while (0)
124
125#define GETIP_DEF(rc, node, instance, x, def) \
126do \
127{ \
128 int status = 0; \
129 DO_GET_IP((rc), (node), (instance), status, x); \
130 if (status == 0 || rc == VERR_CFGM_VALUE_NOT_FOUND) \
131 x.s_addr = def; \
132} while (0)
133
134/*******************************************************************************
135* Structures and Typedefs *
136*******************************************************************************/
137/**
138 * NAT network transport driver instance data.
139 *
140 * @implements PDMINETWORKUP
141 */
142typedef struct DRVNAT
143{
144 /** The network interface. */
145 PDMINETWORKUP INetworkUp;
146 /** The network NAT Engine configureation. */
147 PDMINETWORKNATCONFIG INetworkNATCfg;
148 /** The port we're attached to. */
149 PPDMINETWORKDOWN pIAboveNet;
150 /** The network config of the port we're attached to. */
151 PPDMINETWORKCONFIG pIAboveConfig;
152 /** Pointer to the driver instance. */
153 PPDMDRVINS pDrvIns;
154 /** Link state */
155 PDMNETWORKLINKSTATE enmLinkState;
156 /** NAT state for this instance. */
157 PNATState pNATState;
158 /** TFTP directory prefix. */
159 char *pszTFTPPrefix;
160 /** Boot file name to provide in the DHCP server response. */
161 char *pszBootFile;
162 /** tftp server name to provide in the DHCP server response. */
163 char *pszNextServer;
164 /** Polling thread. */
165 PPDMTHREAD pSlirpThread;
166 /** Queue for NAT-thread-external events. */
167 RTREQQUEUE hSlirpReqQueue;
168 /** The guest IP for port-forwarding. */
169 uint32_t GuestIP;
170 /** Link state set when the VM is suspended. */
171 PDMNETWORKLINKSTATE enmLinkStateWant;
172
173#ifndef RT_OS_WINDOWS
174 /** The write end of the control pipe. */
175 RTPIPE hPipeWrite;
176 /** The read end of the control pipe. */
177 RTPIPE hPipeRead;
178# if HC_ARCH_BITS == 32
179 uint32_t u32Padding;
180# endif
181#else
182 /** for external notification */
183 HANDLE hWakeupEvent;
184#endif
185
186#define DRV_PROFILE_COUNTER(name, dsc) STAMPROFILE Stat ## name
187#define DRV_COUNTING_COUNTER(name, dsc) STAMCOUNTER Stat ## name
188#include "counters.h"
189 /** thread delivering packets for receiving by the guest */
190 PPDMTHREAD pRecvThread;
191 /** thread delivering urg packets for receiving by the guest */
192 PPDMTHREAD pUrgRecvThread;
193 /** event to wakeup the guest receive thread */
194 RTSEMEVENT EventRecv;
195 /** event to wakeup the guest urgent receive thread */
196 RTSEMEVENT EventUrgRecv;
197 /** Receive Req queue (deliver packets to the guest) */
198 RTREQQUEUE hRecvReqQueue;
199 /** Receive Urgent Req queue (deliver packets to the guest). */
200 RTREQQUEUE hUrgRecvReqQueue;
201
202 /** makes access to device func RecvAvail and Recv atomical. */
203 RTCRITSECT DevAccessLock;
204 /** Number of in-flight urgent packets. */
205 volatile uint32_t cUrgPkts;
206 /** Number of in-flight regular packets. */
207 volatile uint32_t cPkts;
208
209 /** Transmit lock taken by BeginXmit and released by EndXmit. */
210 RTCRITSECT XmitLock;
211
212#ifdef RT_OS_DARWIN
213 /* Handle of the DNS watcher runloop source. */
214 CFRunLoopSourceRef hRunLoopSrcDnsWatcher;
215#endif
216} DRVNAT;
217AssertCompileMemberAlignment(DRVNAT, StatNATRecvWakeups, 8);
218/** Pointer to the NAT driver instance data. */
219typedef DRVNAT *PDRVNAT;
220
221
222/*******************************************************************************
223* Internal Functions *
224*******************************************************************************/
225static void drvNATNotifyNATThread(PDRVNAT pThis, const char *pszWho);
226DECLINLINE(void) drvNATUpdateDNS(PDRVNAT pThis, bool fFlapLink);
227static DECLCALLBACK(int) drvNATReinitializeHostNameResolving(PDRVNAT pThis);
228
229
230static DECLCALLBACK(int) drvNATRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
231{
232 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
233
234 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
235 return VINF_SUCCESS;
236
237 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
238 {
239 RTReqQueueProcess(pThis->hRecvReqQueue, 0);
240 if (ASMAtomicReadU32(&pThis->cPkts) == 0)
241 RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
242 }
243 return VINF_SUCCESS;
244}
245
246
247static DECLCALLBACK(int) drvNATRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
248{
249 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
250 int rc;
251 rc = RTSemEventSignal(pThis->EventRecv);
252
253 STAM_COUNTER_INC(&pThis->StatNATRecvWakeups);
254 return VINF_SUCCESS;
255}
256
257static DECLCALLBACK(int) drvNATUrgRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
258{
259 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
260
261 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
262 return VINF_SUCCESS;
263
264 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
265 {
266 RTReqQueueProcess(pThis->hUrgRecvReqQueue, 0);
267 if (ASMAtomicReadU32(&pThis->cUrgPkts) == 0)
268 {
269 int rc = RTSemEventWait(pThis->EventUrgRecv, RT_INDEFINITE_WAIT);
270 AssertRC(rc);
271 }
272 }
273 return VINF_SUCCESS;
274}
275
276static DECLCALLBACK(int) drvNATUrgRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
277{
278 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
279 int rc = RTSemEventSignal(pThis->EventUrgRecv);
280 AssertRC(rc);
281
282 return VINF_SUCCESS;
283}
284
285static DECLCALLBACK(void) drvNATUrgRecvWorker(PDRVNAT pThis, uint8_t *pu8Buf, int cb, struct mbuf *m)
286{
287 int rc = RTCritSectEnter(&pThis->DevAccessLock);
288 AssertRC(rc);
289 rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
290 if (RT_SUCCESS(rc))
291 {
292 rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, pu8Buf, cb);
293 AssertRC(rc);
294 }
295 else if ( rc != VERR_TIMEOUT
296 && rc != VERR_INTERRUPTED)
297 {
298 AssertRC(rc);
299 }
300
301 rc = RTCritSectLeave(&pThis->DevAccessLock);
302 AssertRC(rc);
303
304 slirp_ext_m_free(pThis->pNATState, m, pu8Buf);
305 if (ASMAtomicDecU32(&pThis->cUrgPkts) == 0)
306 {
307 drvNATRecvWakeup(pThis->pDrvIns, pThis->pRecvThread);
308 drvNATNotifyNATThread(pThis, "drvNATUrgRecvWorker");
309 }
310}
311
312
313static DECLCALLBACK(void) drvNATRecvWorker(PDRVNAT pThis, uint8_t *pu8Buf, int cb, struct mbuf *m)
314{
315 int rc;
316 STAM_PROFILE_START(&pThis->StatNATRecv, a);
317
318
319 while (ASMAtomicReadU32(&pThis->cUrgPkts) != 0)
320 {
321 rc = RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
322 if ( RT_FAILURE(rc)
323 && ( rc == VERR_TIMEOUT
324 || rc == VERR_INTERRUPTED))
325 goto done_unlocked;
326 }
327
328 rc = RTCritSectEnter(&pThis->DevAccessLock);
329 AssertRC(rc);
330
331 STAM_PROFILE_START(&pThis->StatNATRecvWait, b);
332 rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
333 STAM_PROFILE_STOP(&pThis->StatNATRecvWait, b);
334
335 if (RT_SUCCESS(rc))
336 {
337 rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, pu8Buf, cb);
338 AssertRC(rc);
339 }
340 else if ( rc != VERR_TIMEOUT
341 && rc != VERR_INTERRUPTED)
342 {
343 AssertRC(rc);
344 }
345
346 rc = RTCritSectLeave(&pThis->DevAccessLock);
347 AssertRC(rc);
348
349done_unlocked:
350 slirp_ext_m_free(pThis->pNATState, m, pu8Buf);
351 ASMAtomicDecU32(&pThis->cPkts);
352
353 drvNATNotifyNATThread(pThis, "drvNATRecvWorker");
354
355 STAM_PROFILE_STOP(&pThis->StatNATRecv, a);
356}
357
358/**
359 * Frees a S/G buffer allocated by drvNATNetworkUp_AllocBuf.
360 *
361 * @param pThis Pointer to the NAT instance.
362 * @param pSgBuf The S/G buffer to free.
363 */
364static void drvNATFreeSgBuf(PDRVNAT pThis, PPDMSCATTERGATHER pSgBuf)
365{
366 Assert((pSgBuf->fFlags & PDMSCATTERGATHER_FLAGS_MAGIC_MASK) == PDMSCATTERGATHER_FLAGS_MAGIC);
367 pSgBuf->fFlags = 0;
368 if (pSgBuf->pvAllocator)
369 {
370 Assert(!pSgBuf->pvUser);
371 slirp_ext_m_free(pThis->pNATState, (struct mbuf *)pSgBuf->pvAllocator, NULL);
372 pSgBuf->pvAllocator = NULL;
373 }
374 else if (pSgBuf->pvUser)
375 {
376 RTMemFree(pSgBuf->aSegs[0].pvSeg);
377 pSgBuf->aSegs[0].pvSeg = NULL;
378 RTMemFree(pSgBuf->pvUser);
379 pSgBuf->pvUser = NULL;
380 }
381 RTMemFree(pSgBuf);
382}
383
384/**
385 * Worker function for drvNATSend().
386 *
387 * @param pThis Pointer to the NAT instance.
388 * @param pSgBuf The scatter/gather buffer.
389 * @thread NAT
390 */
391static void drvNATSendWorker(PDRVNAT pThis, PPDMSCATTERGATHER pSgBuf)
392{
393#ifndef DEBUG_andy /* Assertion happens often to me after resuming a VM -- no time to investigate this now. */
394 Assert(pThis->enmLinkState == PDMNETWORKLINKSTATE_UP);
395#endif
396 if (pThis->enmLinkState == PDMNETWORKLINKSTATE_UP)
397 {
398 struct mbuf *m = (struct mbuf *)pSgBuf->pvAllocator;
399 if (m)
400 {
401 /*
402 * A normal frame.
403 */
404 pSgBuf->pvAllocator = NULL;
405 slirp_input(pThis->pNATState, m, pSgBuf->cbUsed);
406 }
407 else
408 {
409 /*
410 * GSO frame, need to segment it.
411 */
412 /** @todo Make the NAT engine grok large frames? Could be more efficient... */
413#if 0 /* this is for testing PDMNetGsoCarveSegmentQD. */
414 uint8_t abHdrScratch[256];
415#endif
416 uint8_t const *pbFrame = (uint8_t const *)pSgBuf->aSegs[0].pvSeg;
417 PCPDMNETWORKGSO pGso = (PCPDMNETWORKGSO)pSgBuf->pvUser;
418 uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso, pSgBuf->cbUsed); Assert(cSegs > 1);
419 for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
420 {
421 size_t cbSeg;
422 void *pvSeg;
423 m = slirp_ext_m_get(pThis->pNATState, pGso->cbHdrsTotal + pGso->cbMaxSeg, &pvSeg, &cbSeg);
424 if (!m)
425 break;
426
427#if 1
428 uint32_t cbPayload, cbHdrs;
429 uint32_t offPayload = PDMNetGsoCarveSegment(pGso, pbFrame, pSgBuf->cbUsed,
430 iSeg, cSegs, (uint8_t *)pvSeg, &cbHdrs, &cbPayload);
431 memcpy((uint8_t *)pvSeg + cbHdrs, pbFrame + offPayload, cbPayload);
432
433 slirp_input(pThis->pNATState, m, cbPayload + cbHdrs);
434#else
435 uint32_t cbSegFrame;
436 void *pvSegFrame = PDMNetGsoCarveSegmentQD(pGso, (uint8_t *)pbFrame, pSgBuf->cbUsed, abHdrScratch,
437 iSeg, cSegs, &cbSegFrame);
438 memcpy((uint8_t *)pvSeg, pvSegFrame, cbSegFrame);
439
440 slirp_input(pThis->pNATState, m, cbSegFrame);
441#endif
442 }
443 }
444 }
445 drvNATFreeSgBuf(pThis, pSgBuf);
446
447 /** @todo Implement the VERR_TRY_AGAIN drvNATNetworkUp_AllocBuf semantics. */
448}
449
450/**
451 * @interface_method_impl{PDMINETWORKUP,pfnBeginXmit}
452 */
453static DECLCALLBACK(int) drvNATNetworkUp_BeginXmit(PPDMINETWORKUP pInterface, bool fOnWorkerThread)
454{
455 PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
456 int rc = RTCritSectTryEnter(&pThis->XmitLock);
457 if (RT_FAILURE(rc))
458 {
459 /** @todo Kick the worker thread when we have one... */
460 rc = VERR_TRY_AGAIN;
461 }
462 return rc;
463}
464
465/**
466 * @interface_method_impl{PDMINETWORKUP,pfnAllocBuf}
467 */
468static DECLCALLBACK(int) drvNATNetworkUp_AllocBuf(PPDMINETWORKUP pInterface, size_t cbMin,
469 PCPDMNETWORKGSO pGso, PPPDMSCATTERGATHER ppSgBuf)
470{
471 PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
472 Assert(RTCritSectIsOwner(&pThis->XmitLock));
473
474 /*
475 * Drop the incoming frame if the NAT thread isn't running.
476 */
477 if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
478 {
479 Log(("drvNATNetowrkUp_AllocBuf: returns VERR_NET_NO_NETWORK\n"));
480 return VERR_NET_NO_NETWORK;
481 }
482
483 /*
484 * Allocate a scatter/gather buffer and an mbuf.
485 */
486 PPDMSCATTERGATHER pSgBuf = (PPDMSCATTERGATHER)RTMemAlloc(sizeof(*pSgBuf));
487 if (!pSgBuf)
488 return VERR_NO_MEMORY;
489 if (!pGso)
490 {
491 /*
492 * Drop the frame if it is too big.
493 */
494 if (cbMin >= DRVNAT_MAXFRAMESIZE)
495 {
496 Log(("drvNATNetowrkUp_AllocBuf: drops over-sized frame (%u bytes), returns VERR_INVALID_PARAMETER\n",
497 cbMin));
498 return VERR_INVALID_PARAMETER;
499 }
500
501 pSgBuf->pvUser = NULL;
502 pSgBuf->pvAllocator = slirp_ext_m_get(pThis->pNATState, cbMin,
503 &pSgBuf->aSegs[0].pvSeg, &pSgBuf->aSegs[0].cbSeg);
504 if (!pSgBuf->pvAllocator)
505 {
506 RTMemFree(pSgBuf);
507 return VERR_TRY_AGAIN;
508 }
509 }
510 else
511 {
512 /*
513 * Drop the frame if its segment is too big.
514 */
515 if (pGso->cbHdrsTotal + pGso->cbMaxSeg >= DRVNAT_MAXFRAMESIZE)
516 {
517 Log(("drvNATNetowrkUp_AllocBuf: drops over-sized frame (%u bytes), returns VERR_INVALID_PARAMETER\n",
518 pGso->cbHdrsTotal + pGso->cbMaxSeg));
519 return VERR_INVALID_PARAMETER;
520 }
521
522 pSgBuf->pvUser = RTMemDup(pGso, sizeof(*pGso));
523 pSgBuf->pvAllocator = NULL;
524 pSgBuf->aSegs[0].cbSeg = RT_ALIGN_Z(cbMin, 16);
525 pSgBuf->aSegs[0].pvSeg = RTMemAlloc(pSgBuf->aSegs[0].cbSeg);
526 if (!pSgBuf->pvUser || !pSgBuf->aSegs[0].pvSeg)
527 {
528 RTMemFree(pSgBuf->aSegs[0].pvSeg);
529 RTMemFree(pSgBuf->pvUser);
530 RTMemFree(pSgBuf);
531 return VERR_TRY_AGAIN;
532 }
533 }
534
535 /*
536 * Initialize the S/G buffer and return.
537 */
538 pSgBuf->fFlags = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1;
539 pSgBuf->cbUsed = 0;
540 pSgBuf->cbAvailable = pSgBuf->aSegs[0].cbSeg;
541 pSgBuf->cSegs = 1;
542
543#if 0 /* poison */
544 memset(pSgBuf->aSegs[0].pvSeg, 'F', pSgBuf->aSegs[0].cbSeg);
545#endif
546 *ppSgBuf = pSgBuf;
547 return VINF_SUCCESS;
548}
549
550/**
551 * @interface_method_impl{PDMINETWORKUP,pfnFreeBuf}
552 */
553static DECLCALLBACK(int) drvNATNetworkUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
554{
555 PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
556 Assert(RTCritSectIsOwner(&pThis->XmitLock));
557 drvNATFreeSgBuf(pThis, pSgBuf);
558 return VINF_SUCCESS;
559}
560
561/**
562 * @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
563 */
564static DECLCALLBACK(int) drvNATNetworkUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
565{
566 PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
567 Assert((pSgBuf->fFlags & PDMSCATTERGATHER_FLAGS_OWNER_MASK) == PDMSCATTERGATHER_FLAGS_OWNER_1);
568 Assert(RTCritSectIsOwner(&pThis->XmitLock));
569
570 int rc;
571 if (pThis->pSlirpThread->enmState == PDMTHREADSTATE_RUNNING)
572 {
573 /* Set an FTM checkpoint as this operation changes the state permanently. */
574 PDMDrvHlpFTSetCheckpoint(pThis->pDrvIns, FTMCHECKPOINTTYPE_NETWORK);
575
576
577 RTREQQUEUE hQueue = pThis->hSlirpReqQueue;
578
579 rc = RTReqQueueCallEx(hQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
580 (PFNRT)drvNATSendWorker, 2, pThis, pSgBuf);
581 if (RT_SUCCESS(rc))
582 {
583 drvNATNotifyNATThread(pThis, "drvNATNetworkUp_SendBuf");
584 return VINF_SUCCESS;
585 }
586
587 rc = VERR_NET_NO_BUFFER_SPACE;
588 }
589 else
590 rc = VERR_NET_DOWN;
591 drvNATFreeSgBuf(pThis, pSgBuf);
592 return rc;
593}
594
595/**
596 * @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
597 */
598static DECLCALLBACK(void) drvNATNetworkUp_EndXmit(PPDMINETWORKUP pInterface)
599{
600 PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
601 RTCritSectLeave(&pThis->XmitLock);
602}
603
604/**
605 * Get the NAT thread out of poll/WSAWaitForMultipleEvents
606 */
607static void drvNATNotifyNATThread(PDRVNAT pThis, const char *pszWho)
608{
609 int rc;
610#ifndef RT_OS_WINDOWS
611 /* kick poll() */
612 size_t cbIgnored;
613 rc = RTPipeWrite(pThis->hPipeWrite, "", 1, &cbIgnored);
614#else
615 /* kick WSAWaitForMultipleEvents */
616 rc = WSASetEvent(pThis->hWakeupEvent);
617#endif
618 AssertRC(rc);
619}
620
621/**
622 * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
623 */
624static DECLCALLBACK(void) drvNATNetworkUp_SetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
625{
626 LogFlow(("drvNATNetworkUp_SetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
627 /* nothing to do */
628}
629
630/**
631 * Worker function for drvNATNetworkUp_NotifyLinkChanged().
632 * @thread "NAT" thread.
633 */
634static void drvNATNotifyLinkChangedWorker(PDRVNAT pThis, PDMNETWORKLINKSTATE enmLinkState)
635{
636 pThis->enmLinkState = pThis->enmLinkStateWant = enmLinkState;
637 switch (enmLinkState)
638 {
639 case PDMNETWORKLINKSTATE_UP:
640 LogRel(("NAT: Link up\n"));
641 slirp_link_up(pThis->pNATState);
642 break;
643
644 case PDMNETWORKLINKSTATE_DOWN:
645 case PDMNETWORKLINKSTATE_DOWN_RESUME:
646 LogRel(("NAT: Link down\n"));
647 slirp_link_down(pThis->pNATState);
648 break;
649
650 default:
651 AssertMsgFailed(("drvNATNetworkUp_NotifyLinkChanged: unexpected link state %d\n", enmLinkState));
652 }
653}
654
655/**
656 * Notification on link status changes.
657 *
658 * @param pInterface Pointer to the interface structure containing the called function pointer.
659 * @param enmLinkState The new link state.
660 * @thread EMT
661 */
662static DECLCALLBACK(void) drvNATNetworkUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
663{
664 PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
665
666 LogFlow(("drvNATNetworkUp_NotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
667
668 /* Don't queue new requests when the NAT thread is about to stop.
669 * But the VM could also be paused. So memorize the desired state. */
670 if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
671 {
672 pThis->enmLinkStateWant = enmLinkState;
673 return;
674 }
675
676 PRTREQ pReq;
677 int rc = RTReqQueueCallEx(pThis->hSlirpReqQueue, &pReq, 0 /*cMillies*/, RTREQFLAGS_VOID,
678 (PFNRT)drvNATNotifyLinkChangedWorker, 2, pThis, enmLinkState);
679 if (RT_LIKELY(rc == VERR_TIMEOUT))
680 {
681 drvNATNotifyNATThread(pThis, "drvNATNetworkUp_NotifyLinkChanged");
682 rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
683 AssertRC(rc);
684 }
685 else
686 AssertRC(rc);
687 RTReqRelease(pReq);
688}
689
690static void drvNATNotifyApplyPortForwardCommand(PDRVNAT pThis, bool fRemove,
691 bool fUdp, const char *pHostIp,
692 uint16_t u16HostPort, const char *pGuestIp, uint16_t u16GuestPort)
693{
694 RTMAC Mac;
695 RT_ZERO(Mac); /* can't get MAC here */
696 if (pThis->pIAboveConfig)
697 pThis->pIAboveConfig->pfnGetMac(pThis->pIAboveConfig, &Mac);
698
699 struct in_addr guestIp, hostIp;
700
701 if ( pHostIp == NULL
702 || inet_aton(pHostIp, &hostIp) == 0)
703 hostIp.s_addr = INADDR_ANY;
704
705 if ( pGuestIp == NULL
706 || inet_aton(pGuestIp, &guestIp) == 0)
707 guestIp.s_addr = pThis->GuestIP;
708
709 if (fRemove)
710 slirp_remove_redirect(pThis->pNATState, fUdp, hostIp, u16HostPort, guestIp, u16GuestPort);
711 else
712 slirp_add_redirect(pThis->pNATState, fUdp, hostIp, u16HostPort, guestIp, u16GuestPort, Mac.au8);
713}
714
715DECLCALLBACK(int) drvNATNetworkNatConfig_RedirectRuleCommand(PPDMINETWORKNATCONFIG pInterface, bool fRemove,
716 bool fUdp, const char *pHostIp,
717 uint16_t u16HostPort, const char *pGuestIp, uint16_t u16GuestPort)
718{
719 LogFlowFunc(("fRemove=%d, fUdp=%d, pHostIp=%s, u16HostPort=%u, pGuestIp=%s, u16GuestPort=%u\n",
720 RT_BOOL(fRemove), RT_BOOL(fUdp), pHostIp, u16HostPort, pGuestIp,
721 u16GuestPort));
722 PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkNATCfg);
723 PRTREQ pReq;
724 int rc = RTReqQueueCallEx(pThis->hSlirpReqQueue, &pReq, 0 /*cMillies*/, RTREQFLAGS_VOID,
725 (PFNRT)drvNATNotifyApplyPortForwardCommand, 7, pThis, fRemove,
726 fUdp, pHostIp, u16HostPort, pGuestIp, u16GuestPort);
727 if (RT_LIKELY(rc == VERR_TIMEOUT))
728 {
729 drvNATNotifyNATThread(pThis, "drvNATNetworkNatConfig_RedirectRuleCommand");
730 rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
731 AssertRC(rc);
732 }
733 else
734 AssertRC(rc);
735
736 RTReqRelease(pReq);
737 port_forwarding_done:
738 return rc;
739}
740
741/**
742 * NAT thread handling the slirp stuff.
743 *
744 * The slirp implementation is single-threaded so we execute this enginre in a
745 * dedicated thread. We take care that this thread does not become the
746 * bottleneck: If the guest wants to send, a request is enqueued into the
747 * hSlirpReqQueue and handled asynchronously by this thread. If this thread
748 * wants to deliver packets to the guest, it enqueues a request into
749 * hRecvReqQueue which is later handled by the Recv thread.
750 */
751static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
752{
753 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
754 int nFDs = -1;
755#ifdef RT_OS_WINDOWS
756 HANDLE *phEvents = slirp_get_events(pThis->pNATState);
757 unsigned int cBreak = 0;
758#else /* RT_OS_WINDOWS */
759 unsigned int cPollNegRet = 0;
760#endif /* !RT_OS_WINDOWS */
761
762 LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
763
764 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
765 return VINF_SUCCESS;
766
767 if (pThis->enmLinkStateWant != pThis->enmLinkState)
768 drvNATNotifyLinkChangedWorker(pThis, pThis->enmLinkStateWant);
769
770 /*
771 * Polling loop.
772 */
773 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
774 {
775 /*
776 * To prevent concurrent execution of sending/receiving threads
777 */
778#ifndef RT_OS_WINDOWS
779 nFDs = slirp_get_nsock(pThis->pNATState);
780 /* allocation for all sockets + Management pipe */
781 struct pollfd *polls = (struct pollfd *)RTMemAlloc((1 + nFDs) * sizeof(struct pollfd) + sizeof(uint32_t));
782 if (polls == NULL)
783 return VERR_NO_MEMORY;
784
785 /* don't pass the management pipe */
786 slirp_select_fill(pThis->pNATState, &nFDs, &polls[1]);
787
788 polls[0].fd = RTPipeToNative(pThis->hPipeRead);
789 /* POLLRDBAND usually doesn't used on Linux but seems used on Solaris */
790 polls[0].events = POLLRDNORM | POLLPRI | POLLRDBAND;
791 polls[0].revents = 0;
792
793 int cChangedFDs = poll(polls, nFDs + 1, slirp_get_timeout_ms(pThis->pNATState));
794 if (cChangedFDs < 0)
795 {
796 if (errno == EINTR)
797 {
798 Log2(("NAT: signal was caught while sleep on poll\n"));
799 /* No error, just process all outstanding requests but don't wait */
800 cChangedFDs = 0;
801 }
802 else if (cPollNegRet++ > 128)
803 {
804 LogRel(("NAT: Poll returns (%s) suppressed %d\n", strerror(errno), cPollNegRet));
805 cPollNegRet = 0;
806 }
807 }
808
809 if (cChangedFDs >= 0)
810 {
811 slirp_select_poll(pThis->pNATState, &polls[1], nFDs);
812 if (polls[0].revents & (POLLRDNORM|POLLPRI|POLLRDBAND))
813 {
814 /* drain the pipe
815 *
816 * Note! drvNATSend decoupled so we don't know how many times
817 * device's thread sends before we've entered multiplex,
818 * so to avoid false alarm drain pipe here to the very end
819 *
820 * @todo: Probably we should counter drvNATSend to count how
821 * deep pipe has been filed before drain.
822 *
823 */
824 /** @todo XXX: Make it reading exactly we need to drain the
825 * pipe.*/
826 char ch;
827 size_t cbRead;
828 RTPipeRead(pThis->hPipeRead, &ch, 1, &cbRead);
829 }
830 }
831 /* process _all_ outstanding requests but don't wait */
832 RTReqQueueProcess(pThis->hSlirpReqQueue, 0);
833 RTMemFree(polls);
834
835#else /* RT_OS_WINDOWS */
836 nFDs = -1;
837 slirp_select_fill(pThis->pNATState, &nFDs);
838 DWORD dwEvent = WSAWaitForMultipleEvents(nFDs, phEvents, FALSE,
839 slirp_get_timeout_ms(pThis->pNATState),
840 /* :fAlertable */ TRUE);
841 if ( (dwEvent < WSA_WAIT_EVENT_0 || dwEvent > WSA_WAIT_EVENT_0 + nFDs - 1)
842 && dwEvent != WSA_WAIT_TIMEOUT && dwEvent != WSA_WAIT_IO_COMPLETION)
843 {
844 int error = WSAGetLastError();
845 LogRel(("NAT: WSAWaitForMultipleEvents returned %d (error %d)\n", dwEvent, error));
846 RTAssertPanic();
847 }
848
849 if (dwEvent == WSA_WAIT_TIMEOUT)
850 {
851 /* only check for slow/fast timers */
852 slirp_select_poll(pThis->pNATState, /* fTimeout=*/true);
853 continue;
854 }
855 /* poll the sockets in any case */
856 Log2(("%s: poll\n", __FUNCTION__));
857 slirp_select_poll(pThis->pNATState, /* fTimeout=*/false);
858 /* process _all_ outstanding requests but don't wait */
859 RTReqQueueProcess(pThis->hSlirpReqQueue, 0);
860# ifdef VBOX_NAT_DELAY_HACK
861 if (cBreak++ > 128)
862 {
863 cBreak = 0;
864 RTThreadSleep(2);
865 }
866# endif
867#endif /* RT_OS_WINDOWS */
868 }
869
870 return VINF_SUCCESS;
871}
872
873
874/**
875 * Unblock the send thread so it can respond to a state change.
876 *
877 * @returns VBox status code.
878 * @param pDevIns The pcnet device instance.
879 * @param pThread The send thread.
880 */
881static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
882{
883 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
884
885 drvNATNotifyNATThread(pThis, "drvNATAsyncIoWakeup");
886 return VINF_SUCCESS;
887}
888
889/**
890 * Function called by slirp to check if it's possible to feed incoming data to the network port.
891 * @returns 1 if possible.
892 * @returns 0 if not possible.
893 */
894int slirp_can_output(void *pvUser)
895{
896 return 1;
897}
898
899void slirp_push_recv_thread(void *pvUser)
900{
901 PDRVNAT pThis = (PDRVNAT)pvUser;
902 Assert(pThis);
903 drvNATUrgRecvWakeup(pThis->pDrvIns, pThis->pUrgRecvThread);
904}
905
906void slirp_urg_output(void *pvUser, struct mbuf *m, const uint8_t *pu8Buf, int cb)
907{
908 PDRVNAT pThis = (PDRVNAT)pvUser;
909 Assert(pThis);
910
911 PRTREQ pReq = NULL;
912
913 /* don't queue new requests when the NAT thread is about to stop */
914 if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
915 return;
916
917 ASMAtomicIncU32(&pThis->cUrgPkts);
918 int rc = RTReqQueueCallEx(pThis->hUrgRecvReqQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
919 (PFNRT)drvNATUrgRecvWorker, 4, pThis, pu8Buf, cb, m);
920 AssertRC(rc);
921 drvNATUrgRecvWakeup(pThis->pDrvIns, pThis->pUrgRecvThread);
922}
923
924/**
925 * Function called by slirp to wake up device after VERR_TRY_AGAIN
926 */
927void slirp_output_pending(void *pvUser)
928{
929 PDRVNAT pThis = (PDRVNAT)pvUser;
930 Assert(pThis);
931 LogFlowFuncEnter();
932 pThis->pIAboveNet->pfnXmitPending(pThis->pIAboveNet);
933 LogFlowFuncLeave();
934}
935
936/**
937 * Function called by slirp to feed incoming data to the NIC.
938 */
939void slirp_output(void *pvUser, struct mbuf *m, const uint8_t *pu8Buf, int cb)
940{
941 PDRVNAT pThis = (PDRVNAT)pvUser;
942 Assert(pThis);
943
944 LogFlow(("slirp_output BEGIN %p %d\n", pu8Buf, cb));
945 Log6(("slirp_output: pu8Buf=%p cb=%#x (pThis=%p)\n%.*Rhxd\n", pu8Buf, cb, pThis, cb, pu8Buf));
946
947 PRTREQ pReq = NULL;
948
949 /* don't queue new requests when the NAT thread is about to stop */
950 if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
951 return;
952
953 ASMAtomicIncU32(&pThis->cPkts);
954 int rc = RTReqQueueCallEx(pThis->hRecvReqQueue, NULL /*ppReq*/, 0 /*cMillies*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
955 (PFNRT)drvNATRecvWorker, 4, pThis, pu8Buf, cb, m);
956 AssertRC(rc);
957 drvNATRecvWakeup(pThis->pDrvIns, pThis->pRecvThread);
958 STAM_COUNTER_INC(&pThis->StatQueuePktSent);
959 LogFlowFuncLeave();
960}
961
962
963/**
964 * @interface_method_impl{PDMINETWORKNATCONFIG,pfnNotifyDnsChanged}
965 *
966 * We are notified that host's resolver configuration has changed. In
967 * the current setup we don't get any details and just reread that
968 * information ourselves.
969 */
970static DECLCALLBACK(void) drvNATNotifyDnsChanged(PPDMINETWORKNATCONFIG pInterface)
971{
972 PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkNATCfg);
973 drvNATUpdateDNS(pThis, /* fFlapLink */ true);
974}
975
976
977#ifdef RT_OS_DARWIN
978/**
979 * Callback for the SystemConfiguration framework to notify us whenever the DNS
980 * server changes.
981 *
982 * @returns nothing.
983 * @param hDynStor The DynamicStore handle.
984 * @param hChangedKey Array of changed keys we watch for.
985 * @param pvUser Opaque user data (NAT driver instance).
986 */
987static DECLCALLBACK(void) drvNatDnsChanged(SCDynamicStoreRef hDynStor, CFArrayRef hChangedKeys, void *pvUser)
988{
989 PDRVNAT pThis = (PDRVNAT)pvUser;
990
991 Log2(("NAT: System configuration has changed\n"));
992
993 /* Check if any of parameters we are interested in were actually changed. If the size
994 * of hChangedKeys is 0, it means that SCDynamicStore has been restarted. */
995 if (hChangedKeys && CFArrayGetCount(hChangedKeys) > 0)
996 {
997 /* Look to the updated parameters in particular. */
998 CFStringRef pDNSKey = CFSTR("State:/Network/Global/DNS");
999
1000 if (CFArrayContainsValue(hChangedKeys, CFRangeMake(0, CFArrayGetCount(hChangedKeys)), pDNSKey))
1001 {
1002 LogRel(("NAT: DNS servers changed, triggering reconnect\n"));
1003#if 0
1004 CFDictionaryRef hDnsDict = (CFDictionaryRef)SCDynamicStoreCopyValue(hDynStor, pDNSKey);
1005 if (hDnsDict)
1006 {
1007 CFArrayRef hArrAddresses = (CFArrayRef)CFDictionaryGetValue(hDnsDict, kSCPropNetDNSServerAddresses);
1008 if (hArrAddresses && CFArrayGetCount(hArrAddresses) > 0)
1009 {
1010 /* Dump DNS servers list. */
1011 for (int i = 0; i < CFArrayGetCount(hArrAddresses); i++)
1012 {
1013 CFStringRef pDNSAddrStr = (CFStringRef)CFArrayGetValueAtIndex(hArrAddresses, i);
1014 const char *pszDNSAddr = pDNSAddrStr ? CFStringGetCStringPtr(pDNSAddrStr, CFStringGetSystemEncoding()) : NULL;
1015 LogRel(("NAT: New DNS server#%d: %s\n", i, pszDNSAddr ? pszDNSAddr : "None"));
1016 }
1017 }
1018 else
1019 LogRel(("NAT: DNS server list is empty (1)\n"));
1020
1021 CFRelease(hDnsDict);
1022 }
1023 else
1024 LogRel(("NAT: DNS server list is empty (2)\n"));
1025#endif
1026 drvNATUpdateDNS(pThis, /* fFlapLink */ true);
1027 }
1028 else
1029 Log2(("NAT: No DNS changes detected\n"));
1030 }
1031 else
1032 Log2(("NAT: SCDynamicStore has been restarted\n"));
1033}
1034#endif
1035
1036/**
1037 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1038 */
1039static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1040{
1041 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1042 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
1043
1044 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1045 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
1046 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKNATCONFIG, &pThis->INetworkNATCfg);
1047 return NULL;
1048}
1049
1050
1051/**
1052 * Get the MAC address into the slirp stack.
1053 *
1054 * Called by drvNATLoadDone and drvNATPowerOn.
1055 */
1056static void drvNATSetMac(PDRVNAT pThis)
1057{
1058 if (pThis->pIAboveConfig)
1059 {
1060 RTMAC Mac;
1061 pThis->pIAboveConfig->pfnGetMac(pThis->pIAboveConfig, &Mac);
1062 /* Re-activate the port forwarding. If */
1063 slirp_set_ethaddr_and_activate_port_forwarding(pThis->pNATState, Mac.au8, pThis->GuestIP);
1064 }
1065}
1066
1067
1068/**
1069 * After loading we have to pass the MAC address of the ethernet device to the slirp stack.
1070 * Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
1071 * (usually done during guest boot).
1072 */
1073static DECLCALLBACK(int) drvNATLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSMHandle)
1074{
1075 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
1076 drvNATSetMac(pThis);
1077 return VINF_SUCCESS;
1078}
1079
1080
1081/**
1082 * Some guests might not use DHCP to retrieve an IP but use a static IP.
1083 */
1084static DECLCALLBACK(void) drvNATPowerOn(PPDMDRVINS pDrvIns)
1085{
1086 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
1087 drvNATSetMac(pThis);
1088}
1089
1090
1091/**
1092 * @interface_method_impl{PDMDEVREG,pfnResume}
1093 */
1094static DECLCALLBACK(void) drvNATResume(PPDMDRVINS pDrvIns)
1095{
1096 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
1097 VMRESUMEREASON enmReason = PDMDrvHlpVMGetResumeReason(pDrvIns);
1098
1099 switch (enmReason)
1100 {
1101 case VMRESUMEREASON_HOST_RESUME:
1102 bool fFlapLink;
1103#if HAVE_NOTIFICATION_FOR_DNS_UPDATE
1104 /* let event handler do it if necessary */
1105 fFlapLink = false;
1106#else
1107 /* XXX: when in doubt, use brute force */
1108 fFlapLink = true;
1109#endif
1110 drvNATUpdateDNS(pThis, fFlapLink);
1111 return;
1112 default: /* Ignore every other resume reason. */
1113 /* do nothing */
1114 return;
1115 }
1116}
1117
1118
1119static DECLCALLBACK(int) drvNATReinitializeHostNameResolving(PDRVNAT pThis)
1120{
1121 slirpReleaseDnsSettings(pThis->pNATState);
1122 slirpInitializeDnsSettings(pThis->pNATState);
1123 return VINF_SUCCESS;
1124}
1125
1126/**
1127 * This function at this stage could be called from two places, but both from non-NAT thread,
1128 * - drvNATResume (EMT?)
1129 * - drvNatDnsChanged (darwin, GUI or main) "listener"
1130 * When Main's interface IHost will support host network configuration change event on every host,
1131 * we won't call it from drvNATResume, but from listener of Main event in the similar way it done
1132 * for port-forwarding, and it wan't be on GUI/main thread, but on EMT thread only.
1133 *
1134 * Thread here is important, because we need to change DNS server list and domain name (+ perhaps,
1135 * search string) at runtime (VBOX_NAT_ENFORCE_INTERNAL_DNS_UPDATE), we can do it safely on NAT thread,
1136 * so with changing other variables (place where we handle update) the main mechanism of update
1137 * _won't_ be changed, the only thing will change is drop of fFlapLink parameter.
1138 */
1139DECLINLINE(void) drvNATUpdateDNS(PDRVNAT pThis, bool fFlapLink)
1140{
1141 int strategy = slirp_host_network_configuration_change_strategy_selector(pThis->pNATState);
1142 switch (strategy)
1143 {
1144
1145 case VBOX_NAT_DNS_DNSPROXY:
1146 {
1147 /**
1148 * XXX: Here or in _strategy_selector we should deal with network change
1149 * in "network change" scenario domain name change we have to update guest lease
1150 * forcibly.
1151 * Note at that built-in dhcp also updates DNS information on NAT thread.
1152 */
1153 /**
1154 * It's unsafe to to do it directly on non-NAT thread
1155 * so we schedule the worker and kick the NAT thread.
1156 */
1157 RTREQQUEUE hQueue = pThis->hSlirpReqQueue;
1158
1159 int rc = RTReqQueueCallEx(hQueue, NULL /*ppReq*/, 0 /*cMillies*/,
1160 RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
1161 (PFNRT)drvNATReinitializeHostNameResolving, 1, pThis);
1162 if (RT_SUCCESS(rc))
1163 drvNATNotifyNATThread(pThis, "drvNATUpdateDNS");
1164
1165 return;
1166 }
1167
1168 case VBOX_NAT_DNS_EXTERNAL:
1169 /*
1170 * Host resumed from a suspend and the network might have changed.
1171 * Disconnect the guest from the network temporarily to let it pick up the changes.
1172 */
1173
1174 if (fFlapLink)
1175 pThis->pIAboveConfig->pfnSetLinkState(pThis->pIAboveConfig,
1176 PDMNETWORKLINKSTATE_DOWN_RESUME);
1177 return;
1178
1179 case VBOX_NAT_DNS_HOSTRESOLVER:
1180 default:
1181 return;
1182 }
1183}
1184
1185
1186/**
1187 * Info handler.
1188 */
1189static DECLCALLBACK(void) drvNATInfo(PPDMDRVINS pDrvIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1190{
1191 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
1192 slirp_info(pThis->pNATState, pHlp, pszArgs);
1193}
1194
1195#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
1196static int drvNATConstructDNSMappings(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pMappingsCfg)
1197{
1198 int rc = VINF_SUCCESS;
1199 LogFlowFunc(("ENTER: iInstance:%d\n", iInstance));
1200 for (PCFGMNODE pNode = CFGMR3GetFirstChild(pMappingsCfg); pNode; pNode = CFGMR3GetNextChild(pNode))
1201 {
1202 if (!CFGMR3AreValuesValid(pNode, "HostName\0HostNamePattern\0HostIP\0"))
1203 return PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
1204 N_("Unknown configuration in dns mapping"));
1205 char szHostNameOrPattern[255];
1206 bool fMatch = false; /* false used for equal matching, and true if wildcard pattern is used. */
1207 RT_ZERO(szHostNameOrPattern);
1208 GET_STRING(rc, pThis, pNode, "HostName", szHostNameOrPattern[0], sizeof(szHostNameOrPattern));
1209 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1210 {
1211 GET_STRING(rc, pThis, pNode, "HostNamePattern", szHostNameOrPattern[0], sizeof(szHostNameOrPattern));
1212 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1213 {
1214 char szNodeName[225];
1215 RT_ZERO(szNodeName);
1216 CFGMR3GetName(pNode, szNodeName, sizeof(szNodeName));
1217 LogRel(("NAT: Neither 'HostName' nor 'HostNamePattern' is specified for mapping %s\n", szNodeName));
1218 continue;
1219 }
1220 fMatch = true;
1221 }
1222 struct in_addr HostIP;
1223 GETIP_DEF(rc, pThis, pNode, HostIP, INADDR_ANY);
1224 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1225 {
1226 LogRel(("NAT: DNS mapping %s is ignored (address not pointed)\n", szHostNameOrPattern));
1227 continue;
1228 }
1229 slirp_add_host_resolver_mapping(pThis->pNATState, fMatch ? NULL : szHostNameOrPattern, fMatch ? szHostNameOrPattern : NULL, HostIP.s_addr);
1230 }
1231 LogFlowFunc(("LEAVE: %Rrc\n", rc));
1232 return rc;
1233}
1234#endif /* !VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER */
1235
1236
1237/**
1238 * Sets up the redirectors.
1239 *
1240 * @returns VBox status code.
1241 * @param pCfg The configuration handle.
1242 */
1243static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfg, PRTNETADDRIPV4 pNetwork)
1244{
1245 RTMAC Mac;
1246 RT_ZERO(Mac); /* can't get MAC here */
1247
1248 /*
1249 * Enumerate redirections.
1250 */
1251 for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfg); pNode; pNode = CFGMR3GetNextChild(pNode))
1252 {
1253#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
1254 char szNodeName[32];
1255 CFGMR3GetName(pNode, szNodeName, 32);
1256 if ( !RTStrICmp(szNodeName, "HostResolverMappings")
1257 || !RTStrICmp(szNodeName, "AttachedDriver"))
1258 continue;
1259#endif
1260 /*
1261 * Validate the port forwarding config.
1262 */
1263 if (!CFGMR3AreValuesValid(pNode, "Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0BindIP\0"))
1264 return PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
1265 N_("Unknown configuration in port forwarding"));
1266
1267 /* protocol type */
1268 bool fUDP;
1269 char szProtocol[32];
1270 int rc;
1271 GET_STRING(rc, pThis, pNode, "Protocol", szProtocol[0], sizeof(szProtocol));
1272 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1273 {
1274 fUDP = false;
1275 GET_BOOL(rc, pThis, pNode, "UDP", fUDP);
1276 }
1277 else if (RT_SUCCESS(rc))
1278 {
1279 if (!RTStrICmp(szProtocol, "TCP"))
1280 fUDP = false;
1281 else if (!RTStrICmp(szProtocol, "UDP"))
1282 fUDP = true;
1283 else
1284 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
1285 N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""),
1286 iInstance, szProtocol);
1287 }
1288 else
1289 return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS,
1290 N_("NAT#%d: configuration query for \"Protocol\" failed"),
1291 iInstance);
1292 /* host port */
1293 int32_t iHostPort;
1294 GET_S32_STRICT(rc, pThis, pNode, "HostPort", iHostPort);
1295
1296 /* guest port */
1297 int32_t iGuestPort;
1298 GET_S32_STRICT(rc, pThis, pNode, "GuestPort", iGuestPort);
1299
1300 /* guest address */
1301 struct in_addr GuestIP;
1302 GETIP_DEF(rc, pThis, pNode, GuestIP, RT_H2N_U32(pNetwork->u | CTL_GUEST));
1303
1304 /* Store the guest IP for re-establishing the port-forwarding rules. Note that GuestIP
1305 * is not documented. Without */
1306 if (pThis->GuestIP == INADDR_ANY)
1307 pThis->GuestIP = GuestIP.s_addr;
1308
1309 /*
1310 * Call slirp about it.
1311 */
1312 struct in_addr BindIP;
1313 GETIP_DEF(rc, pThis, pNode, BindIP, INADDR_ANY);
1314 if (slirp_add_redirect(pThis->pNATState, fUDP, BindIP, iHostPort, GuestIP, iGuestPort, Mac.au8) < 0)
1315 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS,
1316 N_("NAT#%d: configuration error: failed to set up "
1317 "redirection of %d to %d. Probably a conflict with "
1318 "existing services or other rules"), iInstance, iHostPort,
1319 iGuestPort);
1320 } /* for each redir rule */
1321
1322 return VINF_SUCCESS;
1323}
1324
1325
1326/**
1327 * Destruct a driver instance.
1328 *
1329 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
1330 * resources can be freed correctly.
1331 *
1332 * @param pDrvIns The driver instance data.
1333 */
1334static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
1335{
1336 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
1337 LogFlow(("drvNATDestruct:\n"));
1338 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1339
1340 if (pThis->pNATState)
1341 {
1342 slirp_term(pThis->pNATState);
1343 slirp_deregister_statistics(pThis->pNATState, pDrvIns);
1344#ifdef VBOX_WITH_STATISTICS
1345# define DRV_PROFILE_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
1346# define DRV_COUNTING_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
1347# include "counters.h"
1348#endif
1349 pThis->pNATState = NULL;
1350 }
1351
1352 RTReqQueueDestroy(pThis->hSlirpReqQueue);
1353 pThis->hSlirpReqQueue = NIL_RTREQQUEUE;
1354
1355 RTReqQueueDestroy(pThis->hUrgRecvReqQueue);
1356 pThis->hUrgRecvReqQueue = NIL_RTREQQUEUE;
1357
1358 RTSemEventDestroy(pThis->EventRecv);
1359 pThis->EventRecv = NIL_RTSEMEVENT;
1360
1361 RTSemEventDestroy(pThis->EventUrgRecv);
1362 pThis->EventUrgRecv = NIL_RTSEMEVENT;
1363
1364 if (RTCritSectIsInitialized(&pThis->DevAccessLock))
1365 RTCritSectDelete(&pThis->DevAccessLock);
1366
1367 if (RTCritSectIsInitialized(&pThis->XmitLock))
1368 RTCritSectDelete(&pThis->XmitLock);
1369
1370#ifdef RT_OS_DARWIN
1371 /* Cleanup the DNS watcher. */
1372 CFRunLoopRef hRunLoopMain = CFRunLoopGetMain();
1373 CFRetain(hRunLoopMain);
1374 CFRunLoopRemoveSource(hRunLoopMain, pThis->hRunLoopSrcDnsWatcher, kCFRunLoopCommonModes);
1375 CFRelease(hRunLoopMain);
1376 CFRelease(pThis->hRunLoopSrcDnsWatcher);
1377 pThis->hRunLoopSrcDnsWatcher = NULL;
1378#endif
1379}
1380
1381
1382/**
1383 * Construct a NAT network transport driver instance.
1384 *
1385 * @copydoc FNPDMDRVCONSTRUCT
1386 */
1387static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1388{
1389 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
1390 LogFlow(("drvNATConstruct:\n"));
1391 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1392
1393 /*
1394 * Init the static parts.
1395 */
1396 pThis->pDrvIns = pDrvIns;
1397 pThis->pNATState = NULL;
1398 pThis->pszTFTPPrefix = NULL;
1399 pThis->pszBootFile = NULL;
1400 pThis->pszNextServer = NULL;
1401 pThis->hSlirpReqQueue = NIL_RTREQQUEUE;
1402 pThis->hUrgRecvReqQueue = NIL_RTREQQUEUE;
1403 pThis->EventRecv = NIL_RTSEMEVENT;
1404 pThis->EventUrgRecv = NIL_RTSEMEVENT;
1405#ifdef RT_OS_DARWIN
1406 pThis->hRunLoopSrcDnsWatcher = NULL;
1407#endif
1408
1409 /* IBase */
1410 pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
1411
1412 /* INetwork */
1413 pThis->INetworkUp.pfnBeginXmit = drvNATNetworkUp_BeginXmit;
1414 pThis->INetworkUp.pfnAllocBuf = drvNATNetworkUp_AllocBuf;
1415 pThis->INetworkUp.pfnFreeBuf = drvNATNetworkUp_FreeBuf;
1416 pThis->INetworkUp.pfnSendBuf = drvNATNetworkUp_SendBuf;
1417 pThis->INetworkUp.pfnEndXmit = drvNATNetworkUp_EndXmit;
1418 pThis->INetworkUp.pfnSetPromiscuousMode = drvNATNetworkUp_SetPromiscuousMode;
1419 pThis->INetworkUp.pfnNotifyLinkChanged = drvNATNetworkUp_NotifyLinkChanged;
1420
1421 /* NAT engine configuration */
1422 pThis->INetworkNATCfg.pfnRedirectRuleCommand = drvNATNetworkNatConfig_RedirectRuleCommand;
1423#if HAVE_NOTIFICATION_FOR_DNS_UPDATE && !defined(RT_OS_DARWIN)
1424 /*
1425 * On OS X we stick to the old OS X specific notifications for
1426 * now. Elsewhere use IHostNameResolutionConfigurationChangeEvent
1427 * by enbaling HAVE_NOTIFICATION_FOR_DNS_UPDATE in libslirp.h.
1428 * This code is still in a bit of flux and is implemented and
1429 * enabled in steps to simplify more conservative backporting.
1430 */
1431 pThis->INetworkNATCfg.pfnNotifyDnsChanged = drvNATNotifyDnsChanged;
1432#else
1433 pThis->INetworkNATCfg.pfnNotifyDnsChanged = NULL;
1434#endif
1435
1436 /*
1437 * Validate the config.
1438 */
1439 if (!CFGMR3AreValuesValid(pCfg,
1440 "PassDomain\0TFTPPrefix\0BootFile\0Network"
1441 "\0NextServer\0DNSProxy\0BindIP\0UseHostResolver\0"
1442 "SlirpMTU\0AliasMode\0"
1443 "SockRcv\0SockSnd\0TcpRcv\0TcpSnd\0"
1444 "ICMPCacheLimit\0"
1445 "SoMaxConnection\0"
1446#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
1447 "HostResolverMappings\0"
1448#endif
1449 ))
1450 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
1451 N_("Unknown NAT configuration option, only supports PassDomain,"
1452 " TFTPPrefix, BootFile and Network"));
1453
1454 /*
1455 * Get the configuration settings.
1456 */
1457 int rc;
1458 bool fPassDomain = true;
1459 GET_BOOL(rc, pThis, pCfg, "PassDomain", fPassDomain);
1460
1461 GET_STRING_ALLOC(rc, pThis, pCfg, "TFTPPrefix", pThis->pszTFTPPrefix);
1462 GET_STRING_ALLOC(rc, pThis, pCfg, "BootFile", pThis->pszBootFile);
1463 GET_STRING_ALLOC(rc, pThis, pCfg, "NextServer", pThis->pszNextServer);
1464
1465 int fDNSProxy = 0;
1466 GET_S32(rc, pThis, pCfg, "DNSProxy", fDNSProxy);
1467 int fUseHostResolver = 0;
1468 GET_S32(rc, pThis, pCfg, "UseHostResolver", fUseHostResolver);
1469 int MTU = 1500;
1470 GET_S32(rc, pThis, pCfg, "SlirpMTU", MTU);
1471 int i32AliasMode = 0;
1472 int i32MainAliasMode = 0;
1473 GET_S32(rc, pThis, pCfg, "AliasMode", i32MainAliasMode);
1474 int iIcmpCacheLimit = 100;
1475 GET_S32(rc, pThis, pCfg, "ICMPCacheLimit", iIcmpCacheLimit);
1476
1477 i32AliasMode |= (i32MainAliasMode & 0x1 ? 0x1 : 0);
1478 i32AliasMode |= (i32MainAliasMode & 0x2 ? 0x40 : 0);
1479 i32AliasMode |= (i32MainAliasMode & 0x4 ? 0x4 : 0);
1480 int i32SoMaxConn = 10;
1481 GET_S32(rc, pThis, pCfg, "SoMaxConnection", i32SoMaxConn);
1482 /*
1483 * Query the network port interface.
1484 */
1485 pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
1486 if (!pThis->pIAboveNet)
1487 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
1488 N_("Configuration error: the above device/driver didn't "
1489 "export the network port interface"));
1490 pThis->pIAboveConfig = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKCONFIG);
1491 if (!pThis->pIAboveConfig)
1492 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
1493 N_("Configuration error: the above device/driver didn't "
1494 "export the network config interface"));
1495
1496 /* Generate a network address for this network card. */
1497 char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
1498 GET_STRING(rc, pThis, pCfg, "Network", szNetwork[0], sizeof(szNetwork));
1499 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1500 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT%d: Configuration error: "
1501 "missing network"),
1502 pDrvIns->iInstance, szNetwork);
1503
1504 RTNETADDRIPV4 Network, Netmask;
1505
1506 rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
1507 if (RT_FAILURE(rc))
1508 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: "
1509 "network '%s' describes not a valid IPv4 network"),
1510 pDrvIns->iInstance, szNetwork);
1511
1512 /*
1513 * Initialize slirp.
1514 */
1515 rc = slirp_init(&pThis->pNATState, RT_H2N_U32(Network.u), Netmask.u,
1516 fPassDomain, !!fUseHostResolver, i32AliasMode,
1517 iIcmpCacheLimit, pThis);
1518 if (RT_SUCCESS(rc))
1519 {
1520 slirp_set_dhcp_TFTP_prefix(pThis->pNATState, pThis->pszTFTPPrefix);
1521 slirp_set_dhcp_TFTP_bootfile(pThis->pNATState, pThis->pszBootFile);
1522 slirp_set_dhcp_next_server(pThis->pNATState, pThis->pszNextServer);
1523 slirp_set_dhcp_dns_proxy(pThis->pNATState, !!fDNSProxy);
1524 slirp_set_mtu(pThis->pNATState, MTU);
1525 slirp_set_somaxconn(pThis->pNATState, i32SoMaxConn);
1526 char *pszBindIP = NULL;
1527 GET_STRING_ALLOC(rc, pThis, pCfg, "BindIP", pszBindIP);
1528 rc = slirp_set_binding_address(pThis->pNATState, pszBindIP);
1529 if (rc != 0 && pszBindIP && *pszBindIP)
1530 LogRel(("NAT: Value of BindIP has been ignored\n"));
1531
1532 if(pszBindIP != NULL)
1533 MMR3HeapFree(pszBindIP);
1534#define SLIRP_SET_TUNING_VALUE(name, setter) \
1535 do \
1536 { \
1537 int len = 0; \
1538 rc = CFGMR3QueryS32(pCfg, name, &len); \
1539 if (RT_SUCCESS(rc)) \
1540 setter(pThis->pNATState, len); \
1541 } while(0)
1542
1543 SLIRP_SET_TUNING_VALUE("SockRcv", slirp_set_rcvbuf);
1544 SLIRP_SET_TUNING_VALUE("SockSnd", slirp_set_sndbuf);
1545 SLIRP_SET_TUNING_VALUE("TcpRcv", slirp_set_tcp_rcvspace);
1546 SLIRP_SET_TUNING_VALUE("TcpSnd", slirp_set_tcp_sndspace);
1547
1548 slirp_register_statistics(pThis->pNATState, pDrvIns);
1549#ifdef VBOX_WITH_STATISTICS
1550# define DRV_PROFILE_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
1551# define DRV_COUNTING_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
1552# include "counters.h"
1553#endif
1554
1555#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
1556 PCFGMNODE pMappingsCfg = CFGMR3GetChild(pCfg, "HostResolverMappings");
1557
1558 if (pMappingsCfg)
1559 {
1560 rc = drvNATConstructDNSMappings(pDrvIns->iInstance, pThis, pMappingsCfg);
1561 AssertRC(rc);
1562 }
1563#endif
1564 rc = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfg, &Network);
1565 if (RT_SUCCESS(rc))
1566 {
1567 /*
1568 * Register a load done notification to get the MAC address into the slirp
1569 * engine after we loaded a guest state.
1570 */
1571 rc = PDMDrvHlpSSMRegisterLoadDone(pDrvIns, drvNATLoadDone);
1572 AssertLogRelRCReturn(rc, rc);
1573
1574 rc = RTReqQueueCreate(&pThis->hSlirpReqQueue);
1575 AssertLogRelRCReturn(rc, rc);
1576
1577 rc = RTReqQueueCreate(&pThis->hRecvReqQueue);
1578 AssertLogRelRCReturn(rc, rc);
1579
1580 rc = RTReqQueueCreate(&pThis->hUrgRecvReqQueue);
1581 AssertLogRelRCReturn(rc, rc);
1582
1583 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pRecvThread, pThis, drvNATRecv,
1584 drvNATRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATRX");
1585 AssertRCReturn(rc, rc);
1586
1587 rc = RTSemEventCreate(&pThis->EventRecv);
1588 AssertRCReturn(rc, rc);
1589
1590 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pUrgRecvThread, pThis, drvNATUrgRecv,
1591 drvNATUrgRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATURGRX");
1592 AssertRCReturn(rc, rc);
1593
1594 rc = RTSemEventCreate(&pThis->EventRecv);
1595 AssertRCReturn(rc, rc);
1596
1597 rc = RTSemEventCreate(&pThis->EventUrgRecv);
1598 AssertRCReturn(rc, rc);
1599
1600 rc = RTCritSectInit(&pThis->DevAccessLock);
1601 AssertRCReturn(rc, rc);
1602
1603 rc = RTCritSectInit(&pThis->XmitLock);
1604 AssertRCReturn(rc, rc);
1605
1606 char szTmp[128];
1607 RTStrPrintf(szTmp, sizeof(szTmp), "nat%d", pDrvIns->iInstance);
1608 PDMDrvHlpDBGFInfoRegister(pDrvIns, szTmp, "NAT info.", drvNATInfo);
1609
1610#ifndef RT_OS_WINDOWS
1611 /*
1612 * Create the control pipe.
1613 */
1614 rc = RTPipeCreate(&pThis->hPipeRead, &pThis->hPipeWrite, 0 /*fFlags*/);
1615 AssertRCReturn(rc, rc);
1616#else
1617 pThis->hWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto-reset event */
1618 slirp_register_external_event(pThis->pNATState, pThis->hWakeupEvent,
1619 VBOX_WAKEUP_EVENT_INDEX);
1620#endif
1621
1622 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pSlirpThread, pThis, drvNATAsyncIoThread,
1623 drvNATAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "NAT");
1624 AssertRCReturn(rc, rc);
1625
1626 pThis->enmLinkState = pThis->enmLinkStateWant = PDMNETWORKLINKSTATE_UP;
1627
1628#ifdef RT_OS_DARWIN
1629 /* Set up a watcher which notifies us everytime the DNS server changes. */
1630 int rc2 = VINF_SUCCESS;
1631 SCDynamicStoreContext SCDynStorCtx;
1632
1633 SCDynStorCtx.version = 0;
1634 SCDynStorCtx.info = pThis;
1635 SCDynStorCtx.retain = NULL;
1636 SCDynStorCtx.release = NULL;
1637 SCDynStorCtx.copyDescription = NULL;
1638
1639 SCDynamicStoreRef hDynStor = SCDynamicStoreCreate(NULL, CFSTR("org.virtualbox.drvnat"), drvNatDnsChanged, &SCDynStorCtx);
1640 if (hDynStor)
1641 {
1642 CFRunLoopSourceRef hRunLoopSrc = SCDynamicStoreCreateRunLoopSource(NULL, hDynStor, 0);
1643 if (hRunLoopSrc)
1644 {
1645 CFStringRef aWatchKeys[] =
1646 {
1647 CFSTR("State:/Network/Global/DNS")
1648 };
1649 CFArrayRef hArray = CFArrayCreate(NULL, (const void **)aWatchKeys, 1, &kCFTypeArrayCallBacks);
1650
1651 if (hArray)
1652 {
1653 if (SCDynamicStoreSetNotificationKeys(hDynStor, hArray, NULL))
1654 {
1655 CFRunLoopRef hRunLoopMain = CFRunLoopGetMain();
1656 CFRetain(hRunLoopMain);
1657 CFRunLoopAddSource(hRunLoopMain, hRunLoopSrc, kCFRunLoopCommonModes);
1658 CFRelease(hRunLoopMain);
1659 pThis->hRunLoopSrcDnsWatcher = hRunLoopSrc;
1660 }
1661 else
1662 rc2 = VERR_NO_MEMORY;
1663
1664 CFRelease(hArray);
1665 }
1666 else
1667 rc2 = VERR_NO_MEMORY;
1668
1669 if (RT_FAILURE(rc2)) /* Keep the runloop source referenced for destruction. */
1670 CFRelease(hRunLoopSrc);
1671 }
1672 CFRelease(hDynStor);
1673 }
1674 else
1675 rc2 = VERR_NO_MEMORY;
1676
1677 if (RT_FAILURE(rc2))
1678 LogRel(("NAT#%d: Failed to install DNS change notifier. The guest might loose DNS access when switching networks on the host\n",
1679 pDrvIns->iInstance));
1680#endif
1681
1682 /* might return VINF_NAT_DNS */
1683 return rc;
1684 }
1685
1686 /* failure path */
1687 slirp_term(pThis->pNATState);
1688 pThis->pNATState = NULL;
1689 }
1690 else
1691 {
1692 PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
1693 AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
1694 }
1695
1696 return rc;
1697}
1698
1699
1700/**
1701 * NAT network transport driver registration record.
1702 */
1703const PDMDRVREG g_DrvNAT =
1704{
1705 /* u32Version */
1706 PDM_DRVREG_VERSION,
1707 /* szName */
1708 "NAT",
1709 /* szRCMod */
1710 "",
1711 /* szR0Mod */
1712 "",
1713 /* pszDescription */
1714 "NAT Network Transport Driver",
1715 /* fFlags */
1716 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1717 /* fClass. */
1718 PDM_DRVREG_CLASS_NETWORK,
1719 /* cMaxInstances */
1720 ~0U,
1721 /* cbInstance */
1722 sizeof(DRVNAT),
1723 /* pfnConstruct */
1724 drvNATConstruct,
1725 /* pfnDestruct */
1726 drvNATDestruct,
1727 /* pfnRelocate */
1728 NULL,
1729 /* pfnIOCtl */
1730 NULL,
1731 /* pfnPowerOn */
1732 drvNATPowerOn,
1733 /* pfnReset */
1734 NULL,
1735 /* pfnSuspend */
1736 NULL,
1737 /* pfnResume */
1738 drvNATResume,
1739 /* pfnAttach */
1740 NULL,
1741 /* pfnDetach */
1742 NULL,
1743 /* pfnPowerOff */
1744 NULL,
1745 /* pfnSoftReset */
1746 NULL,
1747 /* u32EndVersion */
1748 PDM_DRVREG_VERSION
1749};
1750
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