1 | /* $Id: DrvNAT.cpp 26309 2010-02-05 21:13:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DrvNAT - NAT network transport driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
27 | #define __STDC_LIMIT_MACROS
|
---|
28 | #define __STDC_CONSTANT_MACROS
|
---|
29 | #include "slirp/libslirp.h"
|
---|
30 | #include "slirp/ctl.h"
|
---|
31 | #include <VBox/pdmdrv.h>
|
---|
32 | #include <VBox/pdmnetifs.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/critsect.h>
|
---|
38 | #include <iprt/cidr.h>
|
---|
39 | #include <iprt/stream.h>
|
---|
40 | #include <iprt/uuid.h>
|
---|
41 |
|
---|
42 | #include "Builtins.h"
|
---|
43 |
|
---|
44 | #ifndef RT_OS_WINDOWS
|
---|
45 | # include <unistd.h>
|
---|
46 | # include <fcntl.h>
|
---|
47 | # include <poll.h>
|
---|
48 | # include <errno.h>
|
---|
49 | #endif
|
---|
50 | #ifdef RT_OS_FREEBSD
|
---|
51 | # include <netinet/in.h>
|
---|
52 | #endif
|
---|
53 | #include <iprt/semaphore.h>
|
---|
54 | #include <iprt/req.h>
|
---|
55 |
|
---|
56 | #define COUNTERS_INIT
|
---|
57 | #include "counters.h"
|
---|
58 |
|
---|
59 |
|
---|
60 | /*******************************************************************************
|
---|
61 | * Defined Constants And Macros *
|
---|
62 | *******************************************************************************/
|
---|
63 | #define GET_EXTRADATA(pthis, node, name, rc, type, type_name, var) \
|
---|
64 | do { \
|
---|
65 | (rc) = CFGMR3Query ## type((node), name, &(var)); \
|
---|
66 | if (RT_FAILURE((rc)) && (rc) != VERR_CFGM_VALUE_NOT_FOUND) \
|
---|
67 | return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \""name"\" " #type_name " failed"), \
|
---|
68 | (pthis)->pDrvIns->iInstance); \
|
---|
69 | } while (0)
|
---|
70 |
|
---|
71 | #define GET_ED_STRICT(pthis, node, name, rc, type, type_name, var) \
|
---|
72 | do { \
|
---|
73 | (rc) = CFGMR3Query ## type((node), name, &(var)); \
|
---|
74 | if (RT_FAILURE((rc))) \
|
---|
75 | return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \""name"\" " #type_name " failed"), \
|
---|
76 | (pthis)->pDrvIns->iInstance); \
|
---|
77 | } while (0)
|
---|
78 |
|
---|
79 | #define GET_EXTRADATA_N(pthis, node, name, rc, type, type_name, var, var_size) \
|
---|
80 | do { \
|
---|
81 | (rc) = CFGMR3Query ## type((node), name, &(var), var_size); \
|
---|
82 | if (RT_FAILURE((rc)) && (rc) != VERR_CFGM_VALUE_NOT_FOUND) \
|
---|
83 | return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \""name"\" " #type_name " failed"), \
|
---|
84 | (pthis)->pDrvIns->iInstance); \
|
---|
85 | } while (0)
|
---|
86 |
|
---|
87 | #define GET_BOOL(rc, pthis, node, name, var) \
|
---|
88 | GET_EXTRADATA(pthis, node, name, (rc), Bool, bolean, (var))
|
---|
89 | #define GET_STRING(rc, pthis, node, name, var, var_size) \
|
---|
90 | GET_EXTRADATA_N(pthis, node, name, (rc), String, string, (var), (var_size))
|
---|
91 | #define GET_STRING_ALLOC(rc, pthis, node, name, var) \
|
---|
92 | GET_EXTRADATA(pthis, node, name, (rc), StringAlloc, string, (var))
|
---|
93 | #define GET_S32(rc, pthis, node, name, var) \
|
---|
94 | GET_EXTRADATA(pthis, node, name, (rc), S32, int, (var))
|
---|
95 | #define GET_S32_STRICT(rc, pthis, node, name, var) \
|
---|
96 | GET_ED_STRICT(pthis, node, name, (rc), S32, int, (var))
|
---|
97 |
|
---|
98 |
|
---|
99 |
|
---|
100 | #define DO_GET_IP(rc, node, instance, status, x) \
|
---|
101 | do { \
|
---|
102 | char sz##x[32]; \
|
---|
103 | GET_STRING((rc), (node), (instance), #x, sz ## x[0], sizeof(sz ## x)); \
|
---|
104 | if (rc != VERR_CFGM_VALUE_NOT_FOUND) \
|
---|
105 | (status) = inet_aton(sz ## x, &x); \
|
---|
106 | } while (0)
|
---|
107 |
|
---|
108 | #define GETIP_DEF(rc, node, instance, x, def) \
|
---|
109 | do \
|
---|
110 | { \
|
---|
111 | int status = 0; \
|
---|
112 | DO_GET_IP((rc), (node), (instance), status, x); \
|
---|
113 | if (status == 0 || rc == VERR_CFGM_VALUE_NOT_FOUND) \
|
---|
114 | x.s_addr = def; \
|
---|
115 | } while (0)
|
---|
116 |
|
---|
117 | /*******************************************************************************
|
---|
118 | * Structures and Typedefs *
|
---|
119 | *******************************************************************************/
|
---|
120 | /**
|
---|
121 | * NAT network transport driver instance data.
|
---|
122 | *
|
---|
123 | * @implements PDMINETWORKUP
|
---|
124 | */
|
---|
125 | typedef struct DRVNAT
|
---|
126 | {
|
---|
127 | /** The network interface. */
|
---|
128 | PDMINETWORKUP INetworkUp;
|
---|
129 | /** The port we're attached to. */
|
---|
130 | PPDMINETWORKDOWN pIAboveNet;
|
---|
131 | /** The network config of the port we're attached to. */
|
---|
132 | PPDMINETWORKCONFIG pIAboveConfig;
|
---|
133 | /** Pointer to the driver instance. */
|
---|
134 | PPDMDRVINS pDrvIns;
|
---|
135 | /** Link state */
|
---|
136 | PDMNETWORKLINKSTATE enmLinkState;
|
---|
137 | /** NAT state for this instance. */
|
---|
138 | PNATState pNATState;
|
---|
139 | /** TFTP directory prefix. */
|
---|
140 | char *pszTFTPPrefix;
|
---|
141 | /** Boot file name to provide in the DHCP server response. */
|
---|
142 | char *pszBootFile;
|
---|
143 | /** tftp server name to provide in the DHCP server response. */
|
---|
144 | char *pszNextServer;
|
---|
145 | /* polling thread */
|
---|
146 | PPDMTHREAD pSlirpThread;
|
---|
147 | /** Queue for NAT-thread-external events. */
|
---|
148 | PRTREQQUEUE pSlirpReqQueue;
|
---|
149 | /** The guest IP for port-forwarding. */
|
---|
150 | uint32_t GuestIP;
|
---|
151 | uint32_t alignment1;
|
---|
152 |
|
---|
153 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
154 | PPDMTHREAD pGuestThread;
|
---|
155 | #endif
|
---|
156 | #ifndef RT_OS_WINDOWS
|
---|
157 | /** The write end of the control pipe. */
|
---|
158 | RTFILE PipeWrite;
|
---|
159 | /** The read end of the control pipe. */
|
---|
160 | RTFILE PipeRead;
|
---|
161 | # if HC_ARCH_BITS == 32
|
---|
162 | /** Alignment padding. */
|
---|
163 | //uint32_t alignment2;
|
---|
164 | # endif
|
---|
165 | #else
|
---|
166 | /** for external notification */
|
---|
167 | HANDLE hWakeupEvent;
|
---|
168 | #endif
|
---|
169 |
|
---|
170 | #define DRV_PROFILE_COUNTER(name, dsc) STAMPROFILE Stat ## name
|
---|
171 | #define DRV_COUNTING_COUNTER(name, dsc) STAMCOUNTER Stat ## name
|
---|
172 | #include "counters.h"
|
---|
173 | /** thread delivering packets for receiving by the guest */
|
---|
174 | PPDMTHREAD pRecvThread;
|
---|
175 | /** thread delivering urg packets for receiving by the guest */
|
---|
176 | PPDMTHREAD pUrgRecvThread;
|
---|
177 | /** event to wakeup the guest receive thread */
|
---|
178 | RTSEMEVENT EventRecv;
|
---|
179 | /** event to wakeup the guest urgent receive thread */
|
---|
180 | RTSEMEVENT EventUrgRecv;
|
---|
181 | /** Receive Req queue (deliver packets to the guest) */
|
---|
182 | PRTREQQUEUE pRecvReqQueue;
|
---|
183 | /** Receive Urgent Req queue (deliver packets to the guest) */
|
---|
184 | PRTREQQUEUE pUrgRecvReqQueue;
|
---|
185 |
|
---|
186 | /* makes access to device func RecvAvail and Recv atomical */
|
---|
187 | RTCRITSECT csDevAccess;
|
---|
188 | volatile uint32_t cUrgPkt;
|
---|
189 | volatile uint32_t cPkt;
|
---|
190 | PTMTIMERR3 pTmrSlow;
|
---|
191 | PTMTIMERR3 pTmrFast;
|
---|
192 | } DRVNAT;
|
---|
193 | AssertCompileMemberAlignment(DRVNAT, StatNATRecvWakeups, 8);
|
---|
194 | /** Pointer the NAT driver instance data. */
|
---|
195 | typedef DRVNAT *PDRVNAT;
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * NAT queue item.
|
---|
199 | */
|
---|
200 | typedef struct DRVNATQUEUITEM
|
---|
201 | {
|
---|
202 | /** The core part owned by the queue manager. */
|
---|
203 | PDMQUEUEITEMCORE Core;
|
---|
204 | /** The buffer for output to guest. */
|
---|
205 | const uint8_t *pu8Buf;
|
---|
206 | /* size of buffer */
|
---|
207 | size_t cb;
|
---|
208 | void *mbuf;
|
---|
209 | } DRVNATQUEUITEM;
|
---|
210 | /** Pointer to a NAT queue item. */
|
---|
211 | typedef DRVNATQUEUITEM *PDRVNATQUEUITEM;
|
---|
212 |
|
---|
213 |
|
---|
214 | static void drvNATNotifyNATThread(PDRVNAT pThis);
|
---|
215 | static DECLCALLBACK(void) drvNATSlowTimer(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser);
|
---|
216 | static DECLCALLBACK(void) drvNATFast(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser);
|
---|
217 |
|
---|
218 |
|
---|
219 | /** Converts a pointer to NAT::INetworkUp to a PRDVNAT. */
|
---|
220 | #define PDMINETWORKUP_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkUp)) )
|
---|
221 |
|
---|
222 | static DECLCALLBACK(void) drvNATSlowTimer(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser)
|
---|
223 | {
|
---|
224 | Assert(pvUser);
|
---|
225 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
226 | drvNATNotifyNATThread(pThis);
|
---|
227 | }
|
---|
228 |
|
---|
229 | static DECLCALLBACK(void) drvNATFastTimer(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser)
|
---|
230 | {
|
---|
231 | Assert(pvUser);
|
---|
232 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
233 | drvNATNotifyNATThread(pThis);
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | static DECLCALLBACK(int) drvNATRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
238 | {
|
---|
239 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
240 |
|
---|
241 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
242 | return VINF_SUCCESS;
|
---|
243 |
|
---|
244 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
245 | {
|
---|
246 | RTReqProcess(pThis->pRecvReqQueue, 0);
|
---|
247 | if (ASMAtomicReadU32(&pThis->cPkt) == 0)
|
---|
248 | RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
|
---|
249 | }
|
---|
250 | return VINF_SUCCESS;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | static DECLCALLBACK(int) drvNATRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
255 | {
|
---|
256 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
257 | int rc;
|
---|
258 | rc = RTSemEventSignal(pThis->EventRecv);
|
---|
259 |
|
---|
260 | STAM_COUNTER_INC(&pThis->StatNATRecvWakeups);
|
---|
261 | return VINF_SUCCESS;
|
---|
262 | }
|
---|
263 |
|
---|
264 | static DECLCALLBACK(int) drvNATUrgRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
265 | {
|
---|
266 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
267 |
|
---|
268 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
269 | return VINF_SUCCESS;
|
---|
270 |
|
---|
271 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
272 | {
|
---|
273 | RTReqProcess(pThis->pUrgRecvReqQueue, 0);
|
---|
274 | if (ASMAtomicReadU32(&pThis->cUrgPkt) == 0)
|
---|
275 | {
|
---|
276 | int rc = RTSemEventWait(pThis->EventUrgRecv, RT_INDEFINITE_WAIT);
|
---|
277 | AssertRC(rc);
|
---|
278 | }
|
---|
279 | }
|
---|
280 | return VINF_SUCCESS;
|
---|
281 | }
|
---|
282 | static DECLCALLBACK(int) drvNATUrgRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
283 | {
|
---|
284 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
285 | int rc = RTSemEventSignal(pThis->EventUrgRecv);
|
---|
286 | AssertRC(rc);
|
---|
287 |
|
---|
288 | return VINF_SUCCESS;
|
---|
289 | }
|
---|
290 |
|
---|
291 | static DECLCALLBACK(void) drvNATUrgRecvWorker(PDRVNAT pThis, uint8_t *pu8Buf, int cb, void *pvArg)
|
---|
292 | {
|
---|
293 | int rc = RTCritSectEnter(&pThis->csDevAccess);
|
---|
294 | AssertRC(rc);
|
---|
295 | rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
|
---|
296 | if (RT_SUCCESS(rc))
|
---|
297 | {
|
---|
298 | rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, pu8Buf, cb);
|
---|
299 | AssertRC(rc);
|
---|
300 | }
|
---|
301 | else if ( RT_FAILURE(rc)
|
---|
302 | && ( rc == VERR_TIMEOUT
|
---|
303 | && rc == VERR_INTERRUPTED))
|
---|
304 | {
|
---|
305 | AssertRC(rc);
|
---|
306 | }
|
---|
307 |
|
---|
308 | rc = RTCritSectLeave(&pThis->csDevAccess);
|
---|
309 | AssertRC(rc);
|
---|
310 |
|
---|
311 | slirp_ext_m_free(pThis->pNATState, pvArg);
|
---|
312 | if (ASMAtomicDecU32(&pThis->cUrgPkt) == 0)
|
---|
313 | {
|
---|
314 | drvNATRecvWakeup(pThis->pDrvIns, pThis->pRecvThread);
|
---|
315 | drvNATNotifyNATThread(pThis);
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | static DECLCALLBACK(void) drvNATRecvWorker(PDRVNAT pThis, uint8_t *pu8Buf, int cb, void *pvArg)
|
---|
321 | {
|
---|
322 | int rc;
|
---|
323 | STAM_PROFILE_START(&pThis->StatNATRecv, a);
|
---|
324 |
|
---|
325 | STAM_PROFILE_START(&pThis->StatNATRecvWait, b);
|
---|
326 |
|
---|
327 | while(ASMAtomicReadU32(&pThis->cUrgPkt) != 0)
|
---|
328 | {
|
---|
329 | rc = RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
|
---|
330 | if ( RT_FAILURE(rc)
|
---|
331 | && ( rc == VERR_TIMEOUT
|
---|
332 | || rc == VERR_INTERRUPTED))
|
---|
333 | goto done_unlocked;
|
---|
334 | }
|
---|
335 |
|
---|
336 | rc = RTCritSectEnter(&pThis->csDevAccess);
|
---|
337 |
|
---|
338 | rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
|
---|
339 | if (RT_SUCCESS(rc))
|
---|
340 | {
|
---|
341 | rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, pu8Buf, cb);
|
---|
342 | AssertRC(rc);
|
---|
343 | }
|
---|
344 | else if ( RT_FAILURE(rc)
|
---|
345 | && ( rc != VERR_TIMEOUT
|
---|
346 | && rc != VERR_INTERRUPTED))
|
---|
347 | {
|
---|
348 | AssertRC(rc);
|
---|
349 | }
|
---|
350 |
|
---|
351 | rc = RTCritSectLeave(&pThis->csDevAccess);
|
---|
352 | AssertRC(rc);
|
---|
353 | done_unlocked:
|
---|
354 | slirp_ext_m_free(pThis->pNATState, pvArg);
|
---|
355 | ASMAtomicDecU32(&pThis->cPkt);
|
---|
356 |
|
---|
357 | drvNATNotifyNATThread(pThis);
|
---|
358 |
|
---|
359 | STAM_PROFILE_STOP(&pThis->StatNATRecvWait, b);
|
---|
360 | STAM_PROFILE_STOP(&pThis->StatNATRecv, a);
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Worker function for drvNATSend().
|
---|
365 | * @thread "NAT" thread.
|
---|
366 | */
|
---|
367 | static void drvNATSendWorker(PDRVNAT pThis, void *pvBuf, size_t cb)
|
---|
368 | {
|
---|
369 | Assert(pThis->enmLinkState == PDMNETWORKLINKSTATE_UP);
|
---|
370 | if (pThis->enmLinkState == PDMNETWORKLINKSTATE_UP)
|
---|
371 | slirp_input(pThis->pNATState, pvBuf);
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * @interface_method_impl{PDMINETWORKUP,pfnSendDeprecated}
|
---|
377 | */
|
---|
378 | static DECLCALLBACK(int) drvNATSendDeprecated(PPDMINETWORKUP pInterface, const void *pvBuf, size_t cb)
|
---|
379 | {
|
---|
380 | PDRVNAT pThis = PDMINETWORKUP_2_DRVNAT(pInterface);
|
---|
381 |
|
---|
382 | LogFlow(("drvNATSend: pvBuf=%p cb=%#x\n", pvBuf, cb));
|
---|
383 | Log2(("drvNATSend: pvBuf=%p cb=%#x\n%.*Rhxd\n", pvBuf, cb, cb, pvBuf));
|
---|
384 |
|
---|
385 | PRTREQ pReq = NULL;
|
---|
386 | int rc;
|
---|
387 | void *buf;
|
---|
388 |
|
---|
389 | /* don't queue new requests when the NAT thread is about to stop */
|
---|
390 | if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
391 | return VINF_SUCCESS;
|
---|
392 |
|
---|
393 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
394 | rc = RTReqAlloc(pThis->pSlirpReqQueue, &pReq, RTREQTYPE_INTERNAL);
|
---|
395 | #else
|
---|
396 | rc = RTReqAlloc((PRTREQQUEUE)slirp_get_queue(pThis->pNATState), &pReq, RTREQTYPE_INTERNAL);
|
---|
397 | #endif
|
---|
398 | AssertRC(rc);
|
---|
399 |
|
---|
400 | /* @todo: Here we should get mbuf instead temporal buffer */
|
---|
401 | #if 0
|
---|
402 | buf = RTMemAlloc(cb);
|
---|
403 | if (buf == NULL)
|
---|
404 | {
|
---|
405 | LogRel(("NAT: Can't allocate send buffer\n"));
|
---|
406 | return VERR_NO_MEMORY;
|
---|
407 | }
|
---|
408 | memcpy(buf, pvBuf, cb);
|
---|
409 | #else
|
---|
410 | void *pvmBuf = slirp_ext_m_get(pThis->pNATState);
|
---|
411 | Assert(pvmBuf);
|
---|
412 | slirp_ext_m_append(pThis->pNATState, pvmBuf, (uint8_t *)pvBuf, cb);
|
---|
413 | #endif
|
---|
414 |
|
---|
415 | pReq->u.Internal.pfn = (PFNRT)drvNATSendWorker;
|
---|
416 | pReq->u.Internal.cArgs = 2;
|
---|
417 | pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
|
---|
418 | pReq->u.Internal.aArgs[1] = (uintptr_t)pvmBuf;
|
---|
419 | pReq->fFlags = RTREQFLAGS_VOID|RTREQFLAGS_NO_WAIT;
|
---|
420 |
|
---|
421 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to wakeup the NAT thread fist */
|
---|
422 | AssertRC(rc);
|
---|
423 | drvNATNotifyNATThread(pThis);
|
---|
424 | LogFlow(("drvNATSend: end\n"));
|
---|
425 | return VINF_SUCCESS;
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Get the NAT thread out of poll/WSAWaitForMultipleEvents
|
---|
431 | */
|
---|
432 | static void drvNATNotifyNATThread(PDRVNAT pThis)
|
---|
433 | {
|
---|
434 | int rc;
|
---|
435 | #ifndef RT_OS_WINDOWS
|
---|
436 | /* kick select() */
|
---|
437 | rc = RTFileWrite(pThis->PipeWrite, "", 1, NULL);
|
---|
438 | #else
|
---|
439 | /* kick WSAWaitForMultipleEvents */
|
---|
440 | rc = WSASetEvent(pThis->hWakeupEvent);
|
---|
441 | #endif
|
---|
442 | AssertRC(rc);
|
---|
443 | }
|
---|
444 |
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
|
---|
448 | */
|
---|
449 | static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
|
---|
450 | {
|
---|
451 | LogFlow(("drvNATSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
|
---|
452 | /* nothing to do */
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Worker function for drvNATNotifyLinkChanged().
|
---|
457 | * @thread "NAT" thread.
|
---|
458 | */
|
---|
459 | static void drvNATNotifyLinkChangedWorker(PDRVNAT pThis, PDMNETWORKLINKSTATE enmLinkState)
|
---|
460 | {
|
---|
461 | pThis->enmLinkState = enmLinkState;
|
---|
462 |
|
---|
463 | switch (enmLinkState)
|
---|
464 | {
|
---|
465 | case PDMNETWORKLINKSTATE_UP:
|
---|
466 | LogRel(("NAT: link up\n"));
|
---|
467 | slirp_link_up(pThis->pNATState);
|
---|
468 | break;
|
---|
469 |
|
---|
470 | case PDMNETWORKLINKSTATE_DOWN:
|
---|
471 | case PDMNETWORKLINKSTATE_DOWN_RESUME:
|
---|
472 | LogRel(("NAT: link down\n"));
|
---|
473 | slirp_link_down(pThis->pNATState);
|
---|
474 | break;
|
---|
475 |
|
---|
476 | default:
|
---|
477 | AssertMsgFailed(("drvNATNotifyLinkChanged: unexpected link state %d\n", enmLinkState));
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * Notification on link status changes.
|
---|
484 | *
|
---|
485 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
486 | * @param enmLinkState The new link state.
|
---|
487 | * @thread EMT
|
---|
488 | */
|
---|
489 | static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
|
---|
490 | {
|
---|
491 | PDRVNAT pThis = PDMINETWORKUP_2_DRVNAT(pInterface);
|
---|
492 |
|
---|
493 | LogFlow(("drvNATNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
|
---|
494 |
|
---|
495 | PRTREQ pReq = NULL;
|
---|
496 |
|
---|
497 | /* don't queue new requests when the NAT thread is about to stop */
|
---|
498 | if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
499 | return;
|
---|
500 |
|
---|
501 | int rc = RTReqAlloc(pThis->pSlirpReqQueue, &pReq, RTREQTYPE_INTERNAL);
|
---|
502 | AssertRC(rc);
|
---|
503 | pReq->u.Internal.pfn = (PFNRT)drvNATNotifyLinkChangedWorker;
|
---|
504 | pReq->u.Internal.cArgs = 2;
|
---|
505 | pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
|
---|
506 | pReq->u.Internal.aArgs[1] = (uintptr_t)enmLinkState;
|
---|
507 | pReq->fFlags = RTREQFLAGS_VOID;
|
---|
508 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to wakeup the NAT thread fist */
|
---|
509 | if (RT_LIKELY(rc == VERR_TIMEOUT))
|
---|
510 | {
|
---|
511 | drvNATNotifyNATThread(pThis);
|
---|
512 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
|
---|
513 | AssertRC(rc);
|
---|
514 | }
|
---|
515 | else
|
---|
516 | AssertRC(rc);
|
---|
517 | RTReqFree(pReq);
|
---|
518 | }
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * NAT thread handling the slirp stuff. The slirp implementation is single-threaded
|
---|
522 | * so we execute this enginre in a dedicated thread. We take care that this thread
|
---|
523 | * does not become the bottleneck: If the guest wants to send, a request is enqueued
|
---|
524 | * into the pSlirpReqQueue and handled asynchronously by this thread. If this thread
|
---|
525 | * wants to deliver packets to the guest, it enqueues a request into pRecvReqQueue
|
---|
526 | * which is later handled by the Recv thread.
|
---|
527 | */
|
---|
528 | static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
529 | {
|
---|
530 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
531 | int nFDs = -1;
|
---|
532 | int ms;
|
---|
533 | #ifdef RT_OS_WINDOWS
|
---|
534 | DWORD event;
|
---|
535 | HANDLE *phEvents;
|
---|
536 | unsigned int cBreak = 0;
|
---|
537 | #else /* RT_OS_WINDOWS */
|
---|
538 | struct pollfd *polls = NULL;
|
---|
539 | unsigned int cPollNegRet = 0;
|
---|
540 | #endif /* !RT_OS_WINDOWS */
|
---|
541 |
|
---|
542 | LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
|
---|
543 |
|
---|
544 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
545 | return VINF_SUCCESS;
|
---|
546 |
|
---|
547 | #ifdef RT_OS_WINDOWS
|
---|
548 | phEvents = slirp_get_events(pThis->pNATState);
|
---|
549 | #endif /* RT_OS_WINDOWS */
|
---|
550 |
|
---|
551 | /*
|
---|
552 | * Polling loop.
|
---|
553 | */
|
---|
554 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
555 | {
|
---|
556 | nFDs = -1;
|
---|
557 | /*
|
---|
558 | * To prevent concurent execution of sending/receving threads
|
---|
559 | */
|
---|
560 | #ifndef RT_OS_WINDOWS
|
---|
561 | nFDs = slirp_get_nsock(pThis->pNATState);
|
---|
562 | polls = NULL;
|
---|
563 | /* allocation for all sockets + Management pipe */
|
---|
564 | polls = (struct pollfd *)RTMemAlloc((1 + nFDs) * sizeof(struct pollfd) + sizeof(uint32_t));
|
---|
565 | if (polls == NULL)
|
---|
566 | return VERR_NO_MEMORY;
|
---|
567 |
|
---|
568 | /* don't pass the managemant pipe */
|
---|
569 | slirp_select_fill(pThis->pNATState, &nFDs, &polls[1]);
|
---|
570 | #if 0
|
---|
571 | ms = slirp_get_timeout_ms(pThis->pNATState);
|
---|
572 | #else
|
---|
573 | ms = 0;
|
---|
574 | #endif
|
---|
575 |
|
---|
576 | polls[0].fd = pThis->PipeRead;
|
---|
577 | /* POLLRDBAND usually doesn't used on Linux but seems used on Solaris */
|
---|
578 | polls[0].events = POLLRDNORM|POLLPRI|POLLRDBAND;
|
---|
579 | polls[0].revents = 0;
|
---|
580 |
|
---|
581 | int cChangedFDs = poll(polls, nFDs + 1, ms ? ms : -1);
|
---|
582 | if (cChangedFDs < 0)
|
---|
583 | {
|
---|
584 | if (errno == EINTR)
|
---|
585 | {
|
---|
586 | Log2(("NAT: signal was caught while sleep on poll\n"));
|
---|
587 | /* No error, just process all outstanding requests but don't wait */
|
---|
588 | cChangedFDs = 0;
|
---|
589 | }
|
---|
590 | else if (cPollNegRet++ > 128)
|
---|
591 | {
|
---|
592 | LogRel(("NAT:Poll returns (%s) suppressed %d\n", strerror(errno), cPollNegRet));
|
---|
593 | cPollNegRet = 0;
|
---|
594 | }
|
---|
595 | }
|
---|
596 |
|
---|
597 | if (cChangedFDs >= 0)
|
---|
598 | {
|
---|
599 | slirp_select_poll(pThis->pNATState, &polls[1], nFDs);
|
---|
600 | if (polls[0].revents & (POLLRDNORM|POLLPRI|POLLRDBAND))
|
---|
601 | {
|
---|
602 | /* drain the pipe */
|
---|
603 | char ch[1];
|
---|
604 | size_t cbRead;
|
---|
605 | int counter = 0;
|
---|
606 | /*
|
---|
607 | * drvNATSend decoupled so we don't know how many times
|
---|
608 | * device's thread sends before we've entered multiplex,
|
---|
609 | * so to avoid false alarm drain pipe here to the very end
|
---|
610 | *
|
---|
611 | * @todo: Probably we should counter drvNATSend to count how
|
---|
612 | * deep pipe has been filed before drain.
|
---|
613 | *
|
---|
614 | * XXX:Make it reading exactly we need to drain the pipe.
|
---|
615 | */
|
---|
616 | RTFileRead(pThis->PipeRead, &ch, 1, &cbRead);
|
---|
617 | }
|
---|
618 | }
|
---|
619 | /* process _all_ outstanding requests but don't wait */
|
---|
620 | RTReqProcess(pThis->pSlirpReqQueue, 0);
|
---|
621 | RTMemFree(polls);
|
---|
622 | #else /* RT_OS_WINDOWS */
|
---|
623 | slirp_select_fill(pThis->pNATState, &nFDs);
|
---|
624 | #if 0
|
---|
625 | ms = slirp_get_timeout_ms(pThis->pNATState);
|
---|
626 | #else
|
---|
627 | ms = 0;
|
---|
628 | #endif
|
---|
629 | struct timeval tv = { 0, ms*1000 };
|
---|
630 | event = WSAWaitForMultipleEvents(nFDs, phEvents, FALSE, ms ? ms : WSA_INFINITE, FALSE);
|
---|
631 | if ( (event < WSA_WAIT_EVENT_0 || event > WSA_WAIT_EVENT_0 + nFDs - 1)
|
---|
632 | && event != WSA_WAIT_TIMEOUT)
|
---|
633 | {
|
---|
634 | int error = WSAGetLastError();
|
---|
635 | LogRel(("NAT: WSAWaitForMultipleEvents returned %d (error %d)\n", event, error));
|
---|
636 | RTAssertPanic();
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (event == WSA_WAIT_TIMEOUT)
|
---|
640 | {
|
---|
641 | /* only check for slow/fast timers */
|
---|
642 | slirp_select_poll(pThis->pNATState, /* fTimeout=*/true, /*fIcmp=*/false);
|
---|
643 | continue;
|
---|
644 | }
|
---|
645 | /* poll the sockets in any case */
|
---|
646 | Log2(("%s: poll\n", __FUNCTION__));
|
---|
647 | slirp_select_poll(pThis->pNATState, /* fTimeout=*/false, /* fIcmp=*/(event == WSA_WAIT_EVENT_0));
|
---|
648 | /* process _all_ outstanding requests but don't wait */
|
---|
649 | RTReqProcess(pThis->pSlirpReqQueue, 0);
|
---|
650 | # ifdef VBOX_NAT_DELAY_HACK
|
---|
651 | if (cBreak++ > 128)
|
---|
652 | {
|
---|
653 | cBreak = 0;
|
---|
654 | RTThreadSleep(2);
|
---|
655 | }
|
---|
656 | # endif
|
---|
657 | #endif /* RT_OS_WINDOWS */
|
---|
658 | }
|
---|
659 |
|
---|
660 | return VINF_SUCCESS;
|
---|
661 | }
|
---|
662 |
|
---|
663 |
|
---|
664 | /**
|
---|
665 | * Unblock the send thread so it can respond to a state change.
|
---|
666 | *
|
---|
667 | * @returns VBox status code.
|
---|
668 | * @param pDevIns The pcnet device instance.
|
---|
669 | * @param pThread The send thread.
|
---|
670 | */
|
---|
671 | static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
672 | {
|
---|
673 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
674 |
|
---|
675 | drvNATNotifyNATThread(pThis);
|
---|
676 | return VINF_SUCCESS;
|
---|
677 | }
|
---|
678 |
|
---|
679 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
680 |
|
---|
681 | static DECLCALLBACK(int) drvNATAsyncIoGuest(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
682 | {
|
---|
683 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
684 |
|
---|
685 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
686 | return VINF_SUCCESS;
|
---|
687 |
|
---|
688 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
689 | slirp_process_queue(pThis->pNATState);
|
---|
690 |
|
---|
691 | return VINF_SUCCESS;
|
---|
692 | }
|
---|
693 |
|
---|
694 |
|
---|
695 | static DECLCALLBACK(int) drvNATAsyncIoGuestWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
696 | {
|
---|
697 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
698 |
|
---|
699 | return VINF_SUCCESS;
|
---|
700 | }
|
---|
701 |
|
---|
702 | #endif /* VBOX_WITH_SLIRP_MT */
|
---|
703 |
|
---|
704 | void slirp_arm_fast_timer(void *pvUser)
|
---|
705 | {
|
---|
706 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
707 | Assert(pThis);
|
---|
708 | TMTimerSetMillies(pThis->pTmrFast, 2);
|
---|
709 | }
|
---|
710 |
|
---|
711 | void slirp_arm_slow_timer(void *pvUser)
|
---|
712 | {
|
---|
713 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
714 | Assert(pThis);
|
---|
715 | TMTimerSetMillies(pThis->pTmrSlow, 500);
|
---|
716 | }
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * Function called by slirp to check if it's possible to feed incoming data to the network port.
|
---|
720 | * @returns 1 if possible.
|
---|
721 | * @returns 0 if not possible.
|
---|
722 | */
|
---|
723 | int slirp_can_output(void *pvUser)
|
---|
724 | {
|
---|
725 | return 1;
|
---|
726 | }
|
---|
727 |
|
---|
728 | void slirp_push_recv_thread(void *pvUser)
|
---|
729 | {
|
---|
730 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
731 | Assert(pThis);
|
---|
732 | drvNATUrgRecvWakeup(pThis->pDrvIns, pThis->pUrgRecvThread);
|
---|
733 | }
|
---|
734 |
|
---|
735 | void slirp_urg_output(void *pvUser, void *pvArg, const uint8_t *pu8Buf, int cb)
|
---|
736 | {
|
---|
737 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
738 | Assert(pThis);
|
---|
739 |
|
---|
740 | PRTREQ pReq = NULL;
|
---|
741 |
|
---|
742 | /* don't queue new requests when the NAT thread is about to stop */
|
---|
743 | if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
744 | return;
|
---|
745 |
|
---|
746 | int rc = RTReqAlloc(pThis->pUrgRecvReqQueue, &pReq, RTREQTYPE_INTERNAL);
|
---|
747 | AssertRC(rc);
|
---|
748 | ASMAtomicIncU32(&pThis->cUrgPkt);
|
---|
749 | pReq->u.Internal.pfn = (PFNRT)drvNATUrgRecvWorker;
|
---|
750 | pReq->u.Internal.cArgs = 4;
|
---|
751 | pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
|
---|
752 | pReq->u.Internal.aArgs[1] = (uintptr_t)pu8Buf;
|
---|
753 | pReq->u.Internal.aArgs[2] = (uintptr_t)cb;
|
---|
754 | pReq->u.Internal.aArgs[3] = (uintptr_t)pvArg;
|
---|
755 | pReq->fFlags = RTREQFLAGS_VOID|RTREQFLAGS_NO_WAIT;
|
---|
756 | rc = RTReqQueue(pReq, 0);
|
---|
757 | AssertRC(rc);
|
---|
758 | drvNATUrgRecvWakeup(pThis->pDrvIns, pThis->pUrgRecvThread);
|
---|
759 | }
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Function called by slirp to feed incoming data to the network port.
|
---|
763 | */
|
---|
764 | void slirp_output(void *pvUser, void *pvArg, const uint8_t *pu8Buf, int cb)
|
---|
765 | {
|
---|
766 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
767 | Assert(pThis);
|
---|
768 |
|
---|
769 | LogFlow(("slirp_output BEGIN %x %d\n", pu8Buf, cb));
|
---|
770 | Log2(("slirp_output: pu8Buf=%p cb=%#x (pThis=%p)\n%.*Rhxd\n", pu8Buf, cb, pThis, cb, pu8Buf));
|
---|
771 |
|
---|
772 | PRTREQ pReq = NULL;
|
---|
773 |
|
---|
774 | /* don't queue new requests when the NAT thread is about to stop */
|
---|
775 | if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
776 | return;
|
---|
777 |
|
---|
778 | int rc = RTReqAlloc(pThis->pRecvReqQueue, &pReq, RTREQTYPE_INTERNAL);
|
---|
779 | AssertRC(rc);
|
---|
780 | ASMAtomicIncU32(&pThis->cPkt);
|
---|
781 | pReq->u.Internal.pfn = (PFNRT)drvNATRecvWorker;
|
---|
782 | pReq->u.Internal.cArgs = 4;
|
---|
783 | pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
|
---|
784 | pReq->u.Internal.aArgs[1] = (uintptr_t)pu8Buf;
|
---|
785 | pReq->u.Internal.aArgs[2] = (uintptr_t)cb;
|
---|
786 | pReq->u.Internal.aArgs[3] = (uintptr_t)pvArg;
|
---|
787 | pReq->fFlags = RTREQFLAGS_VOID|RTREQFLAGS_NO_WAIT;
|
---|
788 | rc = RTReqQueue(pReq, 0);
|
---|
789 | AssertRC(rc);
|
---|
790 | drvNATRecvWakeup(pThis->pDrvIns, pThis->pRecvThread);
|
---|
791 | STAM_COUNTER_INC(&pThis->StatQueuePktSent);
|
---|
792 | }
|
---|
793 |
|
---|
794 |
|
---|
795 | /**
|
---|
796 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
797 | */
|
---|
798 | static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
799 | {
|
---|
800 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
801 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
802 |
|
---|
803 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
804 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
|
---|
805 | return NULL;
|
---|
806 | }
|
---|
807 |
|
---|
808 |
|
---|
809 | /**
|
---|
810 | * Get the MAC address into the slirp stack.
|
---|
811 | *
|
---|
812 | * Called by drvNATLoadDone and drvNATPowerOn.
|
---|
813 | */
|
---|
814 | static void drvNATSetMac(PDRVNAT pThis)
|
---|
815 | {
|
---|
816 | if (pThis->pIAboveConfig)
|
---|
817 | {
|
---|
818 | RTMAC Mac;
|
---|
819 | pThis->pIAboveConfig->pfnGetMac(pThis->pIAboveConfig, &Mac);
|
---|
820 | /* Re-activate the port forwarding. If */
|
---|
821 | slirp_set_ethaddr_and_activate_port_forwarding(pThis->pNATState, Mac.au8, pThis->GuestIP);
|
---|
822 | }
|
---|
823 | }
|
---|
824 |
|
---|
825 |
|
---|
826 | /**
|
---|
827 | * After loading we have to pass the MAC address of the ethernet device to the slirp stack.
|
---|
828 | * Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
|
---|
829 | * (usually done during guest boot).
|
---|
830 | */
|
---|
831 | static DECLCALLBACK(int) drvNATLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSMHandle)
|
---|
832 | {
|
---|
833 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
834 | drvNATSetMac(pThis);
|
---|
835 | return VINF_SUCCESS;
|
---|
836 | }
|
---|
837 |
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Some guests might not use DHCP to retrieve an IP but use a static IP.
|
---|
841 | */
|
---|
842 | static DECLCALLBACK(void) drvNATPowerOn(PPDMDRVINS pDrvIns)
|
---|
843 | {
|
---|
844 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
845 | drvNATSetMac(pThis);
|
---|
846 | }
|
---|
847 |
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * Sets up the redirectors.
|
---|
851 | *
|
---|
852 | * @returns VBox status code.
|
---|
853 | * @param pCfg The configuration handle.
|
---|
854 | */
|
---|
855 | static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfg, RTIPV4ADDR Network)
|
---|
856 | {
|
---|
857 | RTMAC Mac;
|
---|
858 | memset(&Mac, 0, sizeof(RTMAC)); /*can't get MAC here */
|
---|
859 | /*
|
---|
860 | * Enumerate redirections.
|
---|
861 | */
|
---|
862 | for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfg); pNode; pNode = CFGMR3GetNextChild(pNode))
|
---|
863 | {
|
---|
864 | /*
|
---|
865 | * Validate the port forwarding config.
|
---|
866 | */
|
---|
867 | if (!CFGMR3AreValuesValid(pNode, "Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0BindIP\0"))
|
---|
868 | return PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown configuration in port forwarding"));
|
---|
869 |
|
---|
870 | /* protocol type */
|
---|
871 | bool fUDP;
|
---|
872 | char szProtocol[32];
|
---|
873 | int rc;
|
---|
874 | GET_STRING(rc, pThis, pNode, "Protocol", szProtocol[0], sizeof(szProtocol));
|
---|
875 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
876 | {
|
---|
877 | fUDP = false;
|
---|
878 | GET_BOOL(rc, pThis, pNode, "UDP", fUDP);
|
---|
879 | }
|
---|
880 | else if (RT_SUCCESS(rc))
|
---|
881 | {
|
---|
882 | if (!RTStrICmp(szProtocol, "TCP"))
|
---|
883 | fUDP = false;
|
---|
884 | else if (!RTStrICmp(szProtocol, "UDP"))
|
---|
885 | fUDP = true;
|
---|
886 | else
|
---|
887 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
888 | N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""),
|
---|
889 | iInstance, szProtocol);
|
---|
890 | }
|
---|
891 | /* host port */
|
---|
892 | int32_t iHostPort;
|
---|
893 | GET_S32_STRICT(rc, pThis, pNode, "HostPort", iHostPort);
|
---|
894 |
|
---|
895 | /* guest port */
|
---|
896 | int32_t iGuestPort;
|
---|
897 | GET_S32_STRICT(rc, pThis, pNode, "GuestPort", iGuestPort);
|
---|
898 |
|
---|
899 | /* guest address */
|
---|
900 | struct in_addr GuestIP;
|
---|
901 | /* @todo (vvl) use CTL_* */
|
---|
902 | GETIP_DEF(rc, pThis, pNode, GuestIP, htonl(Network | CTL_GUEST));
|
---|
903 |
|
---|
904 | /* Store the guest IP for re-establishing the port-forwarding rules. Note that GuestIP
|
---|
905 | * is not documented. Without */
|
---|
906 | if (pThis->GuestIP == INADDR_ANY)
|
---|
907 | pThis->GuestIP = GuestIP.s_addr;
|
---|
908 |
|
---|
909 | /*
|
---|
910 | * Call slirp about it.
|
---|
911 | */
|
---|
912 | struct in_addr BindIP;
|
---|
913 | GETIP_DEF(rc, pThis, pNode, BindIP, INADDR_ANY);
|
---|
914 | if (slirp_redir(pThis->pNATState, fUDP, BindIP, iHostPort, GuestIP, iGuestPort, Mac.au8) < 0)
|
---|
915 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS,
|
---|
916 | N_("NAT#%d: configuration error: failed to set up "
|
---|
917 | "redirection of %d to %d. Probably a conflict with "
|
---|
918 | "existing services or other rules"), iInstance, iHostPort,
|
---|
919 | iGuestPort);
|
---|
920 | } /* for each redir rule */
|
---|
921 |
|
---|
922 | return VINF_SUCCESS;
|
---|
923 | }
|
---|
924 |
|
---|
925 |
|
---|
926 | /**
|
---|
927 | * Destruct a driver instance.
|
---|
928 | *
|
---|
929 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
930 | * resources can be freed correctly.
|
---|
931 | *
|
---|
932 | * @param pDrvIns The driver instance data.
|
---|
933 | */
|
---|
934 | static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
|
---|
935 | {
|
---|
936 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
937 | LogFlow(("drvNATDestruct:\n"));
|
---|
938 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
939 |
|
---|
940 | slirp_term(pThis->pNATState);
|
---|
941 | slirp_deregister_statistics(pThis->pNATState, pDrvIns);
|
---|
942 | pThis->pNATState = NULL;
|
---|
943 | #ifdef VBOX_WITH_STATISTICS
|
---|
944 | # define DRV_PROFILE_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
|
---|
945 | # define DRV_COUNTING_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
|
---|
946 | # include "counters.h"
|
---|
947 | #endif
|
---|
948 | }
|
---|
949 |
|
---|
950 |
|
---|
951 | /**
|
---|
952 | * Construct a NAT network transport driver instance.
|
---|
953 | *
|
---|
954 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
955 | */
|
---|
956 | static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
957 | {
|
---|
958 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
959 | LogFlow(("drvNATConstruct:\n"));
|
---|
960 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
961 |
|
---|
962 | /*
|
---|
963 | * Validate the config.
|
---|
964 | */
|
---|
965 | if (!CFGMR3AreValuesValid(pCfg,
|
---|
966 | "PassDomain\0TFTPPrefix\0BootFile\0Network"
|
---|
967 | "\0NextServer\0DNSProxy\0BindIP\0UseHostResolver\0"
|
---|
968 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
969 | "SlirpMTU\0"
|
---|
970 | #endif
|
---|
971 | "SocketRcvBuf\0SocketSndBuf\0TcpRcvSpace\0TcpSndSpace\0"))
|
---|
972 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
|
---|
973 | N_("Unknown NAT configuration option, only supports PassDomain,"
|
---|
974 | " TFTPPrefix, BootFile and Network"));
|
---|
975 |
|
---|
976 | /*
|
---|
977 | * Init the static parts.
|
---|
978 | */
|
---|
979 | pThis->pDrvIns = pDrvIns;
|
---|
980 | pThis->pNATState = NULL;
|
---|
981 | pThis->pszTFTPPrefix = NULL;
|
---|
982 | pThis->pszBootFile = NULL;
|
---|
983 | pThis->pszNextServer = NULL;
|
---|
984 | /* IBase */
|
---|
985 | pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
|
---|
986 | /* INetwork */
|
---|
987 | /** @todo implement the new INetworkUp interfaces. */
|
---|
988 | pThis->INetworkUp.pfnSendDeprecated = drvNATSendDeprecated;
|
---|
989 | pThis->INetworkUp.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
|
---|
990 | pThis->INetworkUp.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
|
---|
991 |
|
---|
992 | /*
|
---|
993 | * Get the configuration settings.
|
---|
994 | */
|
---|
995 | int rc;
|
---|
996 | bool fPassDomain = true;
|
---|
997 | GET_BOOL(rc, pThis, pCfg, "PassDomain", fPassDomain);
|
---|
998 |
|
---|
999 | GET_STRING_ALLOC(rc, pThis, pCfg, "TFTPPrefix", pThis->pszTFTPPrefix);
|
---|
1000 | GET_STRING_ALLOC(rc, pThis, pCfg, "BootFile", pThis->pszBootFile);
|
---|
1001 | GET_STRING_ALLOC(rc, pThis, pCfg, "NextServer", pThis->pszNextServer);
|
---|
1002 |
|
---|
1003 | int fDNSProxy = 0;
|
---|
1004 | GET_S32(rc, pThis, pCfg, "DNSProxy", fDNSProxy);
|
---|
1005 | int fUseHostResolver = 0;
|
---|
1006 | GET_S32(rc, pThis, pCfg, "UseHostResolver", fUseHostResolver);
|
---|
1007 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
1008 | int MTU = 1500;
|
---|
1009 | GET_S32(rc, pThis, pCfg, "SlirpMTU", MTU);
|
---|
1010 | #endif
|
---|
1011 |
|
---|
1012 | /*
|
---|
1013 | * Query the network port interface.
|
---|
1014 | */
|
---|
1015 | pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
|
---|
1016 | if (!pThis->pIAboveNet)
|
---|
1017 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
1018 | N_("Configuration error: the above device/driver didn't "
|
---|
1019 | "export the network port interface"));
|
---|
1020 | pThis->pIAboveConfig = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKCONFIG);
|
---|
1021 | if (!pThis->pIAboveConfig)
|
---|
1022 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
1023 | N_("Configuration error: the above device/driver didn't "
|
---|
1024 | "export the network config interface"));
|
---|
1025 |
|
---|
1026 | /* Generate a network address for this network card. */
|
---|
1027 | char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
|
---|
1028 | GET_STRING(rc, pThis, pCfg, "Network", szNetwork[0], sizeof(szNetwork));
|
---|
1029 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
1030 | RTStrPrintf(szNetwork, sizeof(szNetwork), "10.0.%d.0/24", pDrvIns->iInstance + 2);
|
---|
1031 |
|
---|
1032 | RTIPV4ADDR Network;
|
---|
1033 | RTIPV4ADDR Netmask;
|
---|
1034 | rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
|
---|
1035 | if (RT_FAILURE(rc))
|
---|
1036 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: "
|
---|
1037 | "network '%s' describes not a valid IPv4 network"),
|
---|
1038 | pDrvIns->iInstance, szNetwork);
|
---|
1039 |
|
---|
1040 | char szNetAddr[16];
|
---|
1041 | RTStrPrintf(szNetAddr, sizeof(szNetAddr), "%d.%d.%d.%d",
|
---|
1042 | (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16,
|
---|
1043 | (Network & 0xFF00) >> 8, Network & 0xFF);
|
---|
1044 |
|
---|
1045 | /*
|
---|
1046 | * Initialize slirp.
|
---|
1047 | */
|
---|
1048 | rc = slirp_init(&pThis->pNATState, &szNetAddr[0], Netmask, fPassDomain, !!fUseHostResolver, pThis);
|
---|
1049 | if (RT_SUCCESS(rc))
|
---|
1050 | {
|
---|
1051 | slirp_set_dhcp_TFTP_prefix(pThis->pNATState, pThis->pszTFTPPrefix);
|
---|
1052 | slirp_set_dhcp_TFTP_bootfile(pThis->pNATState, pThis->pszBootFile);
|
---|
1053 | slirp_set_dhcp_next_server(pThis->pNATState, pThis->pszNextServer);
|
---|
1054 | slirp_set_dhcp_dns_proxy(pThis->pNATState, !!fDNSProxy);
|
---|
1055 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
1056 | slirp_set_mtu(pThis->pNATState, MTU);
|
---|
1057 | #endif
|
---|
1058 | char *pszBindIP = NULL;
|
---|
1059 | GET_STRING_ALLOC(rc, pThis, pCfg, "BindIP", pszBindIP);
|
---|
1060 | rc = slirp_set_binding_address(pThis->pNATState, pszBindIP);
|
---|
1061 | if (rc != 0)
|
---|
1062 | LogRel(("NAT: value of BindIP has been ignored\n"));
|
---|
1063 |
|
---|
1064 | if(pszBindIP != NULL)
|
---|
1065 | MMR3HeapFree(pszBindIP);
|
---|
1066 | #define SLIRP_SET_TUNING_VALUE(name, setter) \
|
---|
1067 | do \
|
---|
1068 | { \
|
---|
1069 | int len = 0; \
|
---|
1070 | rc = CFGMR3QueryS32(pCfg, name, &len); \
|
---|
1071 | if (RT_SUCCESS(rc)) \
|
---|
1072 | setter(pThis->pNATState, len); \
|
---|
1073 | } while(0)
|
---|
1074 |
|
---|
1075 | SLIRP_SET_TUNING_VALUE("SocketRcvBuf", slirp_set_rcvbuf);
|
---|
1076 | SLIRP_SET_TUNING_VALUE("SocketSndBuf", slirp_set_sndbuf);
|
---|
1077 | SLIRP_SET_TUNING_VALUE("TcpRcvSpace", slirp_set_tcp_rcvspace);
|
---|
1078 | SLIRP_SET_TUNING_VALUE("TcpSndSpace", slirp_set_tcp_sndspace);
|
---|
1079 |
|
---|
1080 | slirp_register_statistics(pThis->pNATState, pDrvIns);
|
---|
1081 | #ifdef VBOX_WITH_STATISTICS
|
---|
1082 | # define DRV_PROFILE_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
|
---|
1083 | # define DRV_COUNTING_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
|
---|
1084 | # include "counters.h"
|
---|
1085 | #endif
|
---|
1086 |
|
---|
1087 | int rc2 = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfg, Network);
|
---|
1088 | if (RT_SUCCESS(rc2))
|
---|
1089 | {
|
---|
1090 | /*
|
---|
1091 | * Register a load done notification to get the MAC address into the slirp
|
---|
1092 | * engine after we loaded a guest state.
|
---|
1093 | */
|
---|
1094 | rc2 = PDMDrvHlpSSMRegisterLoadDone(pDrvIns, drvNATLoadDone);
|
---|
1095 | AssertRC(rc2);
|
---|
1096 | rc = RTReqCreateQueue(&pThis->pSlirpReqQueue);
|
---|
1097 | if (RT_FAILURE(rc))
|
---|
1098 | {
|
---|
1099 | LogRel(("NAT: Can't create request queue\n"));
|
---|
1100 | return rc;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 |
|
---|
1104 | rc = RTReqCreateQueue(&pThis->pRecvReqQueue);
|
---|
1105 | if (RT_FAILURE(rc))
|
---|
1106 | {
|
---|
1107 | LogRel(("NAT: Can't create request queue\n"));
|
---|
1108 | return rc;
|
---|
1109 | }
|
---|
1110 | rc = RTReqCreateQueue(&pThis->pUrgRecvReqQueue);
|
---|
1111 | if (RT_FAILURE(rc))
|
---|
1112 | {
|
---|
1113 | LogRel(("NAT: Can't create request queue\n"));
|
---|
1114 | return rc;
|
---|
1115 | }
|
---|
1116 | rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pRecvThread, pThis, drvNATRecv,
|
---|
1117 | drvNATRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATRX");
|
---|
1118 | AssertRC(rc);
|
---|
1119 | rc = RTSemEventCreate(&pThis->EventRecv);
|
---|
1120 |
|
---|
1121 | rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pUrgRecvThread, pThis, drvNATUrgRecv,
|
---|
1122 | drvNATUrgRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATURGRX");
|
---|
1123 | AssertRC(rc);
|
---|
1124 | rc = RTSemEventCreate(&pThis->EventRecv);
|
---|
1125 | rc = RTSemEventCreate(&pThis->EventUrgRecv);
|
---|
1126 | rc = RTCritSectInit(&pThis->csDevAccess);
|
---|
1127 | rc = PDMDrvHlpTMTimerCreate(pThis->pDrvIns, TMCLOCK_REAL/*enmClock*/, drvNATSlowTimer,
|
---|
1128 | pThis, TMTIMER_FLAGS_NO_CRIT_SECT/*flags*/, "NATSlowTmr", &pThis->pTmrSlow);
|
---|
1129 | rc = PDMDrvHlpTMTimerCreate(pThis->pDrvIns, TMCLOCK_REAL/*enmClock*/, drvNATFastTimer,
|
---|
1130 | pThis, TMTIMER_FLAGS_NO_CRIT_SECT/*flags*/, "NATFastTmr", &pThis->pTmrFast);
|
---|
1131 |
|
---|
1132 | #ifndef RT_OS_WINDOWS
|
---|
1133 | /*
|
---|
1134 | * Create the control pipe.
|
---|
1135 | */
|
---|
1136 | int fds[2];
|
---|
1137 | if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
|
---|
1138 | {
|
---|
1139 | rc = RTErrConvertFromErrno(errno);
|
---|
1140 | AssertRC(rc);
|
---|
1141 | return rc;
|
---|
1142 | }
|
---|
1143 | pThis->PipeRead = fds[0];
|
---|
1144 | pThis->PipeWrite = fds[1];
|
---|
1145 | #else
|
---|
1146 | pThis->hWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto-reset event */
|
---|
1147 | slirp_register_external_event(pThis->pNATState, pThis->hWakeupEvent,
|
---|
1148 | VBOX_WAKEUP_EVENT_INDEX);
|
---|
1149 | #endif
|
---|
1150 |
|
---|
1151 | rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pSlirpThread, pThis, drvNATAsyncIoThread,
|
---|
1152 | drvNATAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "NAT");
|
---|
1153 | AssertRC(rc);
|
---|
1154 |
|
---|
1155 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1156 | rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pGuestThread, pThis, drvNATAsyncIoGuest,
|
---|
1157 | drvNATAsyncIoGuestWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATGUEST");
|
---|
1158 | AssertRC(rc);
|
---|
1159 | #endif
|
---|
1160 |
|
---|
1161 | pThis->enmLinkState = PDMNETWORKLINKSTATE_UP;
|
---|
1162 |
|
---|
1163 | /* might return VINF_NAT_DNS */
|
---|
1164 | return rc;
|
---|
1165 | }
|
---|
1166 | /* failure path */
|
---|
1167 | rc = rc2;
|
---|
1168 | slirp_term(pThis->pNATState);
|
---|
1169 | pThis->pNATState = NULL;
|
---|
1170 | }
|
---|
1171 | else
|
---|
1172 | {
|
---|
1173 | PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
|
---|
1174 | AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | return rc;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 |
|
---|
1181 | /**
|
---|
1182 | * NAT network transport driver registration record.
|
---|
1183 | */
|
---|
1184 | const PDMDRVREG g_DrvNAT =
|
---|
1185 | {
|
---|
1186 | /* u32Version */
|
---|
1187 | PDM_DRVREG_VERSION,
|
---|
1188 | /* szName */
|
---|
1189 | "NAT",
|
---|
1190 | /* szRCMod */
|
---|
1191 | "",
|
---|
1192 | /* szR0Mod */
|
---|
1193 | "",
|
---|
1194 | /* pszDescription */
|
---|
1195 | "NAT Network Transport Driver",
|
---|
1196 | /* fFlags */
|
---|
1197 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
1198 | /* fClass. */
|
---|
1199 | PDM_DRVREG_CLASS_NETWORK,
|
---|
1200 | /* cMaxInstances */
|
---|
1201 | 16,
|
---|
1202 | /* cbInstance */
|
---|
1203 | sizeof(DRVNAT),
|
---|
1204 | /* pfnConstruct */
|
---|
1205 | drvNATConstruct,
|
---|
1206 | /* pfnDestruct */
|
---|
1207 | drvNATDestruct,
|
---|
1208 | /* pfnRelocate */
|
---|
1209 | NULL,
|
---|
1210 | /* pfnIOCtl */
|
---|
1211 | NULL,
|
---|
1212 | /* pfnPowerOn */
|
---|
1213 | drvNATPowerOn,
|
---|
1214 | /* pfnReset */
|
---|
1215 | NULL,
|
---|
1216 | /* pfnSuspend */
|
---|
1217 | NULL,
|
---|
1218 | /* pfnResume */
|
---|
1219 | NULL,
|
---|
1220 | /* pfnAttach */
|
---|
1221 | NULL,
|
---|
1222 | /* pfnDetach */
|
---|
1223 | NULL,
|
---|
1224 | /* pfnPowerOff */
|
---|
1225 | NULL,
|
---|
1226 | /* pfnSoftReset */
|
---|
1227 | NULL,
|
---|
1228 | /* u32EndVersion */
|
---|
1229 | PDM_DRVREG_VERSION
|
---|
1230 | };
|
---|
1231 |
|
---|