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