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