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