1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox network devices:
|
---|
4 | * NAT network transport driver
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
28 | #define __STDC_LIMIT_MACROS
|
---|
29 | #define __STDC_CONSTANT_MACROS
|
---|
30 | #include "Network/slirp/libslirp.h"
|
---|
31 | #include <VBox/pdmdrv.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/file.h>
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/critsect.h>
|
---|
37 | #include <iprt/cidr.h>
|
---|
38 | #include <iprt/stream.h>
|
---|
39 |
|
---|
40 | #include "Builtins.h"
|
---|
41 |
|
---|
42 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
43 | # ifndef RT_OS_WINDOWS
|
---|
44 | # include <unistd.h>
|
---|
45 | # endif
|
---|
46 | # include <errno.h>
|
---|
47 | # include <iprt/semaphore.h>
|
---|
48 | # include <iprt/req.h>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * @todo: This is a bad hack to prevent freezing the guest during high network
|
---|
53 | * activity. This needs to be fixed properly.
|
---|
54 | */
|
---|
55 | /*#define VBOX_NAT_DELAY_HACK*/
|
---|
56 |
|
---|
57 |
|
---|
58 | /*******************************************************************************
|
---|
59 | * Structures and Typedefs *
|
---|
60 | *******************************************************************************/
|
---|
61 | /**
|
---|
62 | * NAT network transport driver instance data.
|
---|
63 | */
|
---|
64 | typedef struct DRVNAT
|
---|
65 | {
|
---|
66 | /** The network interface. */
|
---|
67 | PDMINETWORKCONNECTOR INetworkConnector;
|
---|
68 | /** The port we're attached to. */
|
---|
69 | PPDMINETWORKPORT pPort;
|
---|
70 | /** The network config of the port we're attached to. */
|
---|
71 | PPDMINETWORKCONFIG pConfig;
|
---|
72 | /** Pointer to the driver instance. */
|
---|
73 | PPDMDRVINS pDrvIns;
|
---|
74 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
75 | /** Slirp critical section. */
|
---|
76 | RTCRITSECT CritSect;
|
---|
77 | #endif
|
---|
78 | /** Link state */
|
---|
79 | PDMNETWORKLINKSTATE enmLinkState;
|
---|
80 | /** NAT state for this instance. */
|
---|
81 | PNATState pNATState;
|
---|
82 | /** TFTP directory prefix. */
|
---|
83 | char *pszTFTPPrefix;
|
---|
84 | /** Boot file name to provide in the DHCP server response. */
|
---|
85 | char *pszBootFile;
|
---|
86 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
87 | /* polling thread */
|
---|
88 | PPDMTHREAD pThread;
|
---|
89 | /** Queue for NAT-thread-external events. */
|
---|
90 | PRTREQQUEUE pReqQueue;
|
---|
91 | /* Send queue */
|
---|
92 | PPDMQUEUE pSendQueue;
|
---|
93 | # ifdef VBOX_WITH_SLIRP_MT
|
---|
94 | PPDMTHREAD pGuestThread;
|
---|
95 | # endif
|
---|
96 | # ifndef RT_OS_WINDOWS
|
---|
97 | /** The write end of the control pipe. */
|
---|
98 | RTFILE PipeWrite;
|
---|
99 | /** The read end of the control pipe. */
|
---|
100 | RTFILE PipeRead;
|
---|
101 | # else
|
---|
102 | /** for external notification */
|
---|
103 | HANDLE hWakeupEvent;
|
---|
104 | # endif
|
---|
105 | #endif
|
---|
106 | } DRVNAT, *PDRVNAT;
|
---|
107 |
|
---|
108 | typedef struct DRVNATQUEUITEM
|
---|
109 | {
|
---|
110 | /** The core part owned by the queue manager. */
|
---|
111 | PDMQUEUEITEMCORE Core;
|
---|
112 | /** The buffer for output to guest. */
|
---|
113 | const uint8_t *pu8Buf;
|
---|
114 | /* size of buffer */
|
---|
115 | size_t cb;
|
---|
116 | void *mbuf;
|
---|
117 | } DRVNATQUEUITEM, *PDRVNATQUEUITEM;
|
---|
118 |
|
---|
119 | /** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
|
---|
120 | #define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Worker function for drvNATSend().
|
---|
125 | * @thread "NAT" thread.
|
---|
126 | */
|
---|
127 | static void drvNATSendWorker(PDRVNAT pThis, const void *pvBuf, size_t cb)
|
---|
128 | {
|
---|
129 | Assert(pThis->enmLinkState == PDMNETWORKLINKSTATE_UP);
|
---|
130 | if (pThis->enmLinkState == PDMNETWORKLINKSTATE_UP)
|
---|
131 | slirp_input(pThis->pNATState, (uint8_t *)pvBuf, cb);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Send data to the network.
|
---|
136 | *
|
---|
137 | * @returns VBox status code.
|
---|
138 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
139 | * @param pvBuf Data to send.
|
---|
140 | * @param cb Number of bytes to send.
|
---|
141 | * @thread EMT
|
---|
142 | */
|
---|
143 | static DECLCALLBACK(int) drvNATSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
|
---|
144 | {
|
---|
145 | PDRVNAT pThis = PDMINETWORKCONNECTOR_2_DRVNAT(pInterface);
|
---|
146 |
|
---|
147 | LogFlow(("drvNATSend: pvBuf=%p cb=%#x\n", pvBuf, cb));
|
---|
148 | Log2(("drvNATSend: pvBuf=%p cb=%#x\n%.*Rhxd\n", pvBuf, cb, cb, pvBuf));
|
---|
149 |
|
---|
150 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
151 |
|
---|
152 | PRTREQ pReq = NULL;
|
---|
153 | int rc;
|
---|
154 | /* don't queue new requests when the NAT thread is about to stop */
|
---|
155 | if (pThis->pThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
158 | rc = RTReqAlloc(pThis->pReqQueue, &pReq, RTREQTYPE_INTERNAL);
|
---|
159 | #else
|
---|
160 | rc = RTReqAlloc((PRTREQQUEUE)slirp_get_queue(pThis->pNATState), &pReq, RTREQTYPE_INTERNAL);
|
---|
161 | #endif
|
---|
162 | AssertReleaseRC(rc);
|
---|
163 | pReq->u.Internal.pfn = (PFNRT)drvNATSendWorker;
|
---|
164 | pReq->u.Internal.cArgs = 3;
|
---|
165 | pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
|
---|
166 | pReq->u.Internal.aArgs[1] = (uintptr_t)pvBuf;
|
---|
167 | pReq->u.Internal.aArgs[2] = (uintptr_t)cb;
|
---|
168 | pReq->fFlags = RTREQFLAGS_VOID;
|
---|
169 |
|
---|
170 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to wakeup the NAT thread fist */
|
---|
171 | if (RT_LIKELY(rc == VERR_TIMEOUT))
|
---|
172 | {
|
---|
173 | # ifndef RT_OS_WINDOWS
|
---|
174 | /* kick select() */
|
---|
175 | rc = RTFileWrite(pThis->PipeWrite, "", 1, NULL);
|
---|
176 | AssertRC(rc);
|
---|
177 | # else
|
---|
178 | /* kick WSAWaitForMultipleEvents */
|
---|
179 | rc = WSASetEvent(pThis->hWakeupEvent);
|
---|
180 | AssertRelease(rc == TRUE);
|
---|
181 | # endif
|
---|
182 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
|
---|
183 | AssertReleaseRC(rc);
|
---|
184 | }
|
---|
185 | else
|
---|
186 | AssertReleaseRC(rc);
|
---|
187 | RTReqFree(pReq);
|
---|
188 |
|
---|
189 | rc = WSASetEvent(pThis->hWakeupEvent);
|
---|
190 | AssertRelease(rc == TRUE);
|
---|
191 |
|
---|
192 | #else /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
193 |
|
---|
194 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
195 | AssertReleaseRC(rc);
|
---|
196 |
|
---|
197 | drvNATSendWorker(pThis, pvBuf, cb);
|
---|
198 |
|
---|
199 | RTCritSectLeave(&pThis->CritSect);
|
---|
200 |
|
---|
201 | #endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
202 |
|
---|
203 | LogFlow(("drvNATSend: end\n"));
|
---|
204 | return VINF_SUCCESS;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Set promiscuous mode.
|
---|
210 | *
|
---|
211 | * This is called when the promiscuous mode is set. This means that there doesn't have
|
---|
212 | * to be a mode change when it's called.
|
---|
213 | *
|
---|
214 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
215 | * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
|
---|
216 | * @thread EMT
|
---|
217 | */
|
---|
218 | static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
|
---|
219 | {
|
---|
220 | LogFlow(("drvNATSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
|
---|
221 | /* nothing to do */
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Worker function for drvNATNotifyLinkChanged().
|
---|
226 | * @thread "NAT" thread.
|
---|
227 | */
|
---|
228 | static void drvNATNotifyLinkChangedWorker(PDRVNAT pThis, PDMNETWORKLINKSTATE enmLinkState)
|
---|
229 | {
|
---|
230 | pThis->enmLinkState = enmLinkState;
|
---|
231 |
|
---|
232 | switch (enmLinkState)
|
---|
233 | {
|
---|
234 | case PDMNETWORKLINKSTATE_UP:
|
---|
235 | LogRel(("NAT: link up\n"));
|
---|
236 | slirp_link_up(pThis->pNATState);
|
---|
237 | break;
|
---|
238 |
|
---|
239 | case PDMNETWORKLINKSTATE_DOWN:
|
---|
240 | case PDMNETWORKLINKSTATE_DOWN_RESUME:
|
---|
241 | LogRel(("NAT: link down\n"));
|
---|
242 | slirp_link_down(pThis->pNATState);
|
---|
243 | break;
|
---|
244 |
|
---|
245 | default:
|
---|
246 | AssertMsgFailed(("drvNATNotifyLinkChanged: unexpected link state %d\n", enmLinkState));
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Notification on link status changes.
|
---|
252 | *
|
---|
253 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
254 | * @param enmLinkState The new link state.
|
---|
255 | * @thread EMT
|
---|
256 | */
|
---|
257 | static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
|
---|
258 | {
|
---|
259 | PDRVNAT pThis = PDMINETWORKCONNECTOR_2_DRVNAT(pInterface);
|
---|
260 |
|
---|
261 | LogFlow(("drvNATNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
|
---|
262 |
|
---|
263 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
264 |
|
---|
265 | PRTREQ pReq = NULL;
|
---|
266 | /* don't queue new requests when the NAT thread is about to stop */
|
---|
267 | if (pThis->pThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
268 | return;
|
---|
269 | int rc = RTReqAlloc(pThis->pReqQueue, &pReq, RTREQTYPE_INTERNAL);
|
---|
270 | AssertReleaseRC(rc);
|
---|
271 | pReq->u.Internal.pfn = (PFNRT)drvNATNotifyLinkChangedWorker;
|
---|
272 | pReq->u.Internal.cArgs = 2;
|
---|
273 | pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
|
---|
274 | pReq->u.Internal.aArgs[1] = (uintptr_t)enmLinkState;
|
---|
275 | pReq->fFlags = RTREQFLAGS_VOID;
|
---|
276 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to wakeup the NAT thread fist */
|
---|
277 | if (RT_LIKELY(rc == VERR_TIMEOUT))
|
---|
278 | {
|
---|
279 | # ifndef RT_OS_WINDOWS
|
---|
280 | /* kick select() */
|
---|
281 | rc = RTFileWrite(pThis->PipeWrite, "", 1, NULL);
|
---|
282 | AssertRC(rc);
|
---|
283 | # else
|
---|
284 | /* kick WSAWaitForMultipleEvents() */
|
---|
285 | rc = WSASetEvent(pThis->hWakeupEvent);
|
---|
286 | AssertRelease(rc == TRUE);
|
---|
287 | # endif
|
---|
288 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
|
---|
289 | AssertReleaseRC(rc);
|
---|
290 | }
|
---|
291 | else
|
---|
292 | AssertReleaseRC(rc);
|
---|
293 | RTReqFree(pReq);
|
---|
294 |
|
---|
295 | #else /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
296 |
|
---|
297 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
298 | AssertReleaseRC(rc);
|
---|
299 | drvNATNotifyLinkChangedWorker(pThis, enmLinkState);
|
---|
300 | RTCritSectLeave(&pThis->CritSect);
|
---|
301 |
|
---|
302 | #endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|
306 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Poller callback.
|
---|
310 | */
|
---|
311 | static DECLCALLBACK(void) drvNATPoller(PPDMDRVINS pDrvIns)
|
---|
312 | {
|
---|
313 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
314 | fd_set ReadFDs;
|
---|
315 | fd_set WriteFDs;
|
---|
316 | fd_set XcptFDs;
|
---|
317 | int nFDs = -1;
|
---|
318 | FD_ZERO(&ReadFDs);
|
---|
319 | FD_ZERO(&WriteFDs);
|
---|
320 | FD_ZERO(&XcptFDs);
|
---|
321 |
|
---|
322 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
323 | AssertReleaseRC(rc);
|
---|
324 |
|
---|
325 | slirp_select_fill(pThis->pNATState, &nFDs, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
326 |
|
---|
327 | struct timeval tv = {0, 0}; /* no wait */
|
---|
328 | int cChangedFDs = select(nFDs + 1, &ReadFDs, &WriteFDs, &XcptFDs, &tv);
|
---|
329 | if (cChangedFDs >= 0)
|
---|
330 | slirp_select_poll(pThis->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
331 |
|
---|
332 | RTCritSectLeave(&pThis->CritSect);
|
---|
333 | }
|
---|
334 |
|
---|
335 | #else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
336 |
|
---|
337 | static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
338 | {
|
---|
339 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
340 | fd_set ReadFDs;
|
---|
341 | fd_set WriteFDs;
|
---|
342 | fd_set XcptFDs;
|
---|
343 | int nFDs = -1;
|
---|
344 | unsigned int ms;
|
---|
345 | # ifdef RT_OS_WINDOWS
|
---|
346 | DWORD event;
|
---|
347 | HANDLE *phEvents;
|
---|
348 | unsigned int cBreak = 0;
|
---|
349 | # endif
|
---|
350 |
|
---|
351 | LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
|
---|
352 |
|
---|
353 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
354 | return VINF_SUCCESS;
|
---|
355 |
|
---|
356 | #ifdef RT_OS_WINDOWS
|
---|
357 | phEvents = slirp_get_events(pThis->pNATState);
|
---|
358 | #endif
|
---|
359 |
|
---|
360 | /*
|
---|
361 | * Polling loop.
|
---|
362 | */
|
---|
363 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
364 | {
|
---|
365 | FD_ZERO(&ReadFDs);
|
---|
366 | FD_ZERO(&WriteFDs);
|
---|
367 | FD_ZERO(&XcptFDs);
|
---|
368 | nFDs = -1;
|
---|
369 |
|
---|
370 | /*
|
---|
371 | * To prevent concurent execution of sending/receving threads
|
---|
372 | */
|
---|
373 | slirp_select_fill(pThis->pNATState, &nFDs, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
374 | ms = slirp_get_timeout_ms(pThis->pNATState);
|
---|
375 | # ifndef RT_OS_WINDOWS
|
---|
376 | struct timeval tv = { 0, ms*1000 };
|
---|
377 | FD_SET(pThis->PipeRead, &ReadFDs);
|
---|
378 | nFDs = ((int)pThis->PipeRead < nFDs ? nFDs : pThis->PipeRead);
|
---|
379 | int cChangedFDs = select(nFDs + 1, &ReadFDs, &WriteFDs, &XcptFDs, ms ? &tv : NULL);
|
---|
380 | if (cChangedFDs >= 0)
|
---|
381 | {
|
---|
382 | slirp_select_poll(pThis->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
383 | if (FD_ISSET(pThis->PipeRead, &ReadFDs))
|
---|
384 | {
|
---|
385 | /* drain the pipe */
|
---|
386 | char ch[1];
|
---|
387 | size_t cbRead;
|
---|
388 | RTFileRead(pThis->PipeRead, &ch, 1, &cbRead);
|
---|
389 | }
|
---|
390 | /* process _all_ outstanding requests but don't wait */
|
---|
391 | RTReqProcess(pThis->pReqQueue, 0);
|
---|
392 | }
|
---|
393 | # else /* RT_OS_WINDOWS */
|
---|
394 | event = WSAWaitForMultipleEvents(nFDs, phEvents, FALSE, ms ? ms : WSA_INFINITE, FALSE);
|
---|
395 | if ( (event < WSA_WAIT_EVENT_0 || event > WSA_WAIT_EVENT_0 + nFDs - 1)
|
---|
396 | && event != WSA_WAIT_TIMEOUT)
|
---|
397 | {
|
---|
398 | int error = WSAGetLastError();
|
---|
399 | LogRel(("WSAWaitForMultipleEvents returned %d (error %d)\n", event, error));
|
---|
400 | RTAssertReleasePanic();
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (event == WSA_WAIT_TIMEOUT)
|
---|
404 | {
|
---|
405 | /* only check for slow/fast timers */
|
---|
406 | slirp_select_poll(pThis->pNATState, /* fTimeout=*/true, /*fIcmp=*/false);
|
---|
407 | Log2(("%s: timeout\n", __FUNCTION__));
|
---|
408 | continue;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /* poll the sockets in any case */
|
---|
412 | Log2(("%s: poll\n", __FUNCTION__));
|
---|
413 | slirp_select_poll(pThis->pNATState, /* fTimeout=*/false, /* fIcmp=*/(event == WSA_WAIT_EVENT_0));
|
---|
414 | /* process _all_ outstanding requests but don't wait */
|
---|
415 | RTReqProcess(pThis->pReqQueue, 0);
|
---|
416 | # ifdef VBOX_NAT_DELAY_HACK
|
---|
417 | if (cBreak++ > 128)
|
---|
418 | {
|
---|
419 | cBreak = 0;
|
---|
420 | RTThreadSleep(2);
|
---|
421 | }
|
---|
422 | # endif
|
---|
423 | # endif /* RT_OS_WINDOWS */
|
---|
424 | }
|
---|
425 |
|
---|
426 | return VINF_SUCCESS;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Unblock the send thread so it can respond to a state change.
|
---|
431 | *
|
---|
432 | * @returns VBox status code.
|
---|
433 | * @param pDevIns The pcnet device instance.
|
---|
434 | * @param pThread The send thread.
|
---|
435 | */
|
---|
436 | static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
437 | {
|
---|
438 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
439 |
|
---|
440 | # ifndef RT_OS_WINDOWS
|
---|
441 | /* kick select() */
|
---|
442 | int rc = RTFileWrite(pThis->PipeWrite, "", 1, NULL);
|
---|
443 | AssertRC(rc);
|
---|
444 | # else
|
---|
445 | /* kick WSAWaitForMultipleEvents() */
|
---|
446 | WSASetEvent(pThis->hWakeupEvent);
|
---|
447 | # endif
|
---|
448 |
|
---|
449 | return VINF_SUCCESS;
|
---|
450 | }
|
---|
451 |
|
---|
452 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
453 | static DECLCALLBACK(int) drvNATAsyncIoGuest(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
454 | {
|
---|
455 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
456 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
457 | return VINF_SUCCESS;
|
---|
458 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
459 | {
|
---|
460 | slirp_process_queue(pThis->pNATState);
|
---|
461 | }
|
---|
462 | return VINF_SUCCESS;
|
---|
463 | }
|
---|
464 |
|
---|
465 | static DECLCALLBACK(int) drvNATAsyncIoGuestWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
466 | {
|
---|
467 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
468 |
|
---|
469 | return VINF_SUCCESS;
|
---|
470 | }
|
---|
471 | #endif
|
---|
472 |
|
---|
473 | #endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
474 |
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * Function called by slirp to check if it's possible to feed incoming data to the network port.
|
---|
478 | * @returns 1 if possible.
|
---|
479 | * @returns 0 if not possible.
|
---|
480 | */
|
---|
481 | int slirp_can_output(void *pvUser)
|
---|
482 | {
|
---|
483 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
484 |
|
---|
485 | Assert(pThis);
|
---|
486 |
|
---|
487 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
488 | /** Happens during termination */
|
---|
489 | if (!RTCritSectIsOwner(&pThis->CritSect))
|
---|
490 | return 0;
|
---|
491 |
|
---|
492 | int rc = pThis->pPort->pfnWaitReceiveAvail(pThis->pPort, 0);
|
---|
493 | return RT_SUCCESS(rc);
|
---|
494 | #else
|
---|
495 | return 1;
|
---|
496 | #endif
|
---|
497 | }
|
---|
498 |
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Function called by slirp to feed incoming data to the network port.
|
---|
502 | */
|
---|
503 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
504 | void slirp_output(void *pvUser, void *pvArg, const uint8_t *pu8Buf, int cb)
|
---|
505 | #else
|
---|
506 | void slirp_output(void *pvUser, const uint8_t *pu8Buf, int cb)
|
---|
507 | #endif
|
---|
508 | {
|
---|
509 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
510 |
|
---|
511 | LogFlow(("slirp_output BEGIN %x %d\n", pu8Buf, cb));
|
---|
512 | Log2(("slirp_output: pu8Buf=%p cb=%#x (pThis=%p)\n%.*Rhxd\n", pu8Buf, cb, pThis, cb, pu8Buf));
|
---|
513 |
|
---|
514 | Assert(pThis);
|
---|
515 |
|
---|
516 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
517 | /** Happens during termination */
|
---|
518 | if (!RTCritSectIsOwner(&pThis->CritSect))
|
---|
519 | return;
|
---|
520 |
|
---|
521 | int rc = pThis->pPort->pfnReceive(pThis->pPort, pu8Buf, cb);
|
---|
522 | AssertRC(rc);
|
---|
523 | LogFlow(("slirp_output END %x %d\n", pu8Buf, cb));
|
---|
524 | #else
|
---|
525 |
|
---|
526 | PDRVNATQUEUITEM pItem = (PDRVNATQUEUITEM)PDMQueueAlloc(pThis->pSendQueue);
|
---|
527 | if (pItem)
|
---|
528 | {
|
---|
529 | pItem->pu8Buf = pu8Buf;
|
---|
530 | pItem->cb = cb;
|
---|
531 | pItem->mbuf = pvArg;
|
---|
532 | Log2(("pItem:%p %.Rhxd\n", pItem, pItem->pu8Buf));
|
---|
533 | PDMQueueInsert(pThis->pSendQueue, &pItem->Core);
|
---|
534 | return;
|
---|
535 | }
|
---|
536 | static unsigned cDroppedPackets;
|
---|
537 | if (cDroppedPackets < 64)
|
---|
538 | {
|
---|
539 | cDroppedPackets++;
|
---|
540 | LogRel(("NAT: Dropping package (couldn't alloc queue item to)\n"));
|
---|
541 | }
|
---|
542 | RTMemFree((void *)pu8Buf);
|
---|
543 | #endif
|
---|
544 | }
|
---|
545 |
|
---|
546 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
547 | /**
|
---|
548 | * Queue callback for processing a queued item.
|
---|
549 | *
|
---|
550 | * @returns Success indicator.
|
---|
551 | * If false the item will not be removed and the flushing will stop.
|
---|
552 | * @param pDrvIns The driver instance.
|
---|
553 | * @param pItemCore Pointer to the queue item to process.
|
---|
554 | */
|
---|
555 | static DECLCALLBACK(bool) drvNATQueueConsumer(PPDMDRVINS pDrvIns, PPDMQUEUEITEMCORE pItemCore)
|
---|
556 | {
|
---|
557 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
558 | PDRVNATQUEUITEM pItem = (PDRVNATQUEUITEM)pItemCore;
|
---|
559 | PRTREQ pReq = NULL;
|
---|
560 | Log(("drvNATQueueConsumer(pItem:%p, pu8Buf:%p, cb:%d)\n", pItem, pItem->pu8Buf, pItem->cb));
|
---|
561 | Log2(("drvNATQueueConsumer: pu8Buf:\n%.Rhxd\n", pItem->pu8Buf));
|
---|
562 | int rc = pThis->pPort->pfnWaitReceiveAvail(pThis->pPort, 0);
|
---|
563 | if (RT_FAILURE(rc))
|
---|
564 | return false;
|
---|
565 | rc = pThis->pPort->pfnReceive(pThis->pPort, pItem->pu8Buf, pItem->cb);
|
---|
566 |
|
---|
567 | #if 0
|
---|
568 | rc = RTReqAlloc(pThis->pReqQueue, &pReq, RTREQTYPE_INTERNAL);
|
---|
569 | AssertReleaseRC(rc);
|
---|
570 | pReq->u.Internal.pfn = (PFNRT)slirp_post_sent;
|
---|
571 | pReq->u.Internal.cArgs = 2;
|
---|
572 | pReq->u.Internal.aArgs[0] = (uintptr_t)pThis->pNATState;
|
---|
573 | pReq->u.Internal.aArgs[1] = (uintptr_t)pItem->mbuf;
|
---|
574 | pReq->fFlags = RTREQFLAGS_VOID;
|
---|
575 | AssertRC(rc);
|
---|
576 | #else
|
---|
577 | /*Copy buffer again, till seeking good way of syncronization with slirp mbuf management code*/
|
---|
578 | AssertRelease(pItem->mbuf == NULL);
|
---|
579 | RTMemFree((void *)pItem->pu8Buf);
|
---|
580 | #endif
|
---|
581 | return RT_SUCCESS(rc);
|
---|
582 | }
|
---|
583 | #endif
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * Queries an interface to the driver.
|
---|
587 | *
|
---|
588 | * @returns Pointer to interface.
|
---|
589 | * @returns NULL if the interface was not supported by the driver.
|
---|
590 | * @param pInterface Pointer to this interface structure.
|
---|
591 | * @param enmInterface The requested interface identification.
|
---|
592 | * @thread Any thread.
|
---|
593 | */
|
---|
594 | static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
595 | {
|
---|
596 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
597 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
598 | switch (enmInterface)
|
---|
599 | {
|
---|
600 | case PDMINTERFACE_BASE:
|
---|
601 | return &pDrvIns->IBase;
|
---|
602 | case PDMINTERFACE_NETWORK_CONNECTOR:
|
---|
603 | return &pThis->INetworkConnector;
|
---|
604 | default:
|
---|
605 | return NULL;
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * Destruct a driver instance.
|
---|
612 | *
|
---|
613 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
614 | * resources can be freed correctly.
|
---|
615 | *
|
---|
616 | * @param pDrvIns The driver instance data.
|
---|
617 | */
|
---|
618 | static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
|
---|
619 | {
|
---|
620 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
621 |
|
---|
622 | LogFlow(("drvNATDestruct:\n"));
|
---|
623 |
|
---|
624 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
625 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
626 | AssertReleaseRC(rc);
|
---|
627 | #endif
|
---|
628 | slirp_term(pThis->pNATState);
|
---|
629 | pThis->pNATState = NULL;
|
---|
630 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
631 | RTCritSectLeave(&pThis->CritSect);
|
---|
632 | RTCritSectDelete(&pThis->CritSect);
|
---|
633 | #endif
|
---|
634 | }
|
---|
635 |
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Sets up the redirectors.
|
---|
639 | *
|
---|
640 | * @returns VBox status code.
|
---|
641 | * @param pCfgHandle The drivers configuration handle.
|
---|
642 | */
|
---|
643 | static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfgHandle, RTIPV4ADDR Network)
|
---|
644 | {
|
---|
645 | /*
|
---|
646 | * Enumerate redirections.
|
---|
647 | */
|
---|
648 | for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfgHandle); pNode; pNode = CFGMR3GetNextChild(pNode))
|
---|
649 | {
|
---|
650 | /*
|
---|
651 | * Validate the port forwarding config.
|
---|
652 | */
|
---|
653 | if (!CFGMR3AreValuesValid(pNode, "Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0"))
|
---|
654 | return PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown configuration in port forwarding"));
|
---|
655 |
|
---|
656 | /* protocol type */
|
---|
657 | bool fUDP;
|
---|
658 | char szProtocol[32];
|
---|
659 | int rc = CFGMR3QueryString(pNode, "Protocol", &szProtocol[0], sizeof(szProtocol));
|
---|
660 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
661 | {
|
---|
662 | rc = CFGMR3QueryBool(pNode, "UDP", &fUDP);
|
---|
663 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
664 | fUDP = false;
|
---|
665 | else if (RT_FAILURE(rc))
|
---|
666 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"UDP\" boolean failed"), iInstance);
|
---|
667 | }
|
---|
668 | else if (RT_SUCCESS(rc))
|
---|
669 | {
|
---|
670 | if (!RTStrICmp(szProtocol, "TCP"))
|
---|
671 | fUDP = false;
|
---|
672 | else if (!RTStrICmp(szProtocol, "UDP"))
|
---|
673 | fUDP = true;
|
---|
674 | else
|
---|
675 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""), iInstance, szProtocol);
|
---|
676 | }
|
---|
677 | else
|
---|
678 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Protocol\" string failed"), iInstance);
|
---|
679 |
|
---|
680 | /* host port */
|
---|
681 | int32_t iHostPort;
|
---|
682 | rc = CFGMR3QueryS32(pNode, "HostPort", &iHostPort);
|
---|
683 | if (RT_FAILURE(rc))
|
---|
684 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"HostPort\" integer failed"), iInstance);
|
---|
685 |
|
---|
686 | /* guest port */
|
---|
687 | int32_t iGuestPort;
|
---|
688 | rc = CFGMR3QueryS32(pNode, "GuestPort", &iGuestPort);
|
---|
689 | if (RT_FAILURE(rc))
|
---|
690 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestPort\" integer failed"), iInstance);
|
---|
691 |
|
---|
692 | /* guest address */
|
---|
693 | char szGuestIP[32];
|
---|
694 | rc = CFGMR3QueryString(pNode, "GuestIP", &szGuestIP[0], sizeof(szGuestIP));
|
---|
695 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
696 | RTStrPrintf(szGuestIP, sizeof(szGuestIP), "%d.%d.%d.%d",
|
---|
697 | (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, (Network & 0xE0) | 15);
|
---|
698 | else if (RT_FAILURE(rc))
|
---|
699 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestIP\" string failed"), iInstance);
|
---|
700 | struct in_addr GuestIP;
|
---|
701 | if (!inet_aton(szGuestIP, &GuestIP))
|
---|
702 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_GUEST_IP, RT_SRC_POS,
|
---|
703 | N_("NAT#%d: configuration error: invalid \"GuestIP\"=\"%s\", inet_aton failed"), iInstance, szGuestIP);
|
---|
704 |
|
---|
705 | /*
|
---|
706 | * Call slirp about it.
|
---|
707 | */
|
---|
708 | Log(("drvNATConstruct: Redir %d -> %s:%d\n", iHostPort, szGuestIP, iGuestPort));
|
---|
709 | if (slirp_redir(pThis->pNATState, fUDP, iHostPort, GuestIP, iGuestPort) < 0)
|
---|
710 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS,
|
---|
711 | N_("NAT#%d: configuration error: failed to set up redirection of %d to %s:%d. Probably a conflict with existing services or other rules"), iInstance, iHostPort, szGuestIP, iGuestPort);
|
---|
712 | } /* for each redir rule */
|
---|
713 |
|
---|
714 | return VINF_SUCCESS;
|
---|
715 | }
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Get the MAC address into the slirp stack.
|
---|
719 | */
|
---|
720 | static void drvNATSetMac(PDRVNAT pThis)
|
---|
721 | {
|
---|
722 | if (pThis->pConfig)
|
---|
723 | {
|
---|
724 | RTMAC Mac;
|
---|
725 | pThis->pConfig->pfnGetMac(pThis->pConfig, &Mac);
|
---|
726 | slirp_set_ethaddr(pThis->pNATState, Mac.au8);
|
---|
727 | }
|
---|
728 | }
|
---|
729 |
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * After loading we have to pass the MAC address of the ethernet device to the slirp stack.
|
---|
733 | * Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
|
---|
734 | * (usually done during guest boot).
|
---|
735 | */
|
---|
736 | static DECLCALLBACK(int) drvNATLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSMHandle)
|
---|
737 | {
|
---|
738 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
739 | drvNATSetMac(pThis);
|
---|
740 | return VINF_SUCCESS;
|
---|
741 | }
|
---|
742 |
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Some guests might not use DHCP to retrieve an IP but use a static IP.
|
---|
746 | */
|
---|
747 | static DECLCALLBACK(void) drvNATPowerOn(PPDMDRVINS pDrvIns)
|
---|
748 | {
|
---|
749 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
750 | drvNATSetMac(pThis);
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Construct a NAT network transport driver instance.
|
---|
756 | *
|
---|
757 | * @returns VBox status.
|
---|
758 | * @param pDrvIns The driver instance data.
|
---|
759 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
760 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
761 | * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
|
---|
762 | * iInstance it's expected to be used a bit in this function.
|
---|
763 | */
|
---|
764 | static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
765 | {
|
---|
766 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
767 | char szNetAddr[16];
|
---|
768 | char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
|
---|
769 | LogFlow(("drvNATConstruct:\n"));
|
---|
770 |
|
---|
771 | /*
|
---|
772 | * Validate the config.
|
---|
773 | */
|
---|
774 | if (!CFGMR3AreValuesValid(pCfgHandle, "PassDomain\0TFTPPrefix\0BootFile\0Network\0"))
|
---|
775 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown NAT configuration option, only supports PassDomain, TFTPPrefix, BootFile and Network"));
|
---|
776 |
|
---|
777 | /*
|
---|
778 | * Init the static parts.
|
---|
779 | */
|
---|
780 | pThis->pDrvIns = pDrvIns;
|
---|
781 | pThis->pNATState = NULL;
|
---|
782 | pThis->pszTFTPPrefix = NULL;
|
---|
783 | pThis->pszBootFile = NULL;
|
---|
784 | /* IBase */
|
---|
785 | pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
|
---|
786 | /* INetwork */
|
---|
787 | pThis->INetworkConnector.pfnSend = drvNATSend;
|
---|
788 | pThis->INetworkConnector.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
|
---|
789 | pThis->INetworkConnector.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * Get the configuration settings.
|
---|
793 | */
|
---|
794 | bool fPassDomain = true;
|
---|
795 | int rc = CFGMR3QueryBool(pCfgHandle, "PassDomain", &fPassDomain);
|
---|
796 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
797 | fPassDomain = true;
|
---|
798 | else if (RT_FAILURE(rc))
|
---|
799 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"PassDomain\" boolean failed"), pDrvIns->iInstance);
|
---|
800 |
|
---|
801 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "TFTPPrefix", &pThis->pszTFTPPrefix);
|
---|
802 | if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
803 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"TFTPPrefix\" string failed"), pDrvIns->iInstance);
|
---|
804 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "BootFile", &pThis->pszBootFile);
|
---|
805 | if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
806 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"BootFile\" string failed"), pDrvIns->iInstance);
|
---|
807 |
|
---|
808 | /*
|
---|
809 | * Query the network port interface.
|
---|
810 | */
|
---|
811 | pThis->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
|
---|
812 | if (!pThis->pPort)
|
---|
813 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
814 | N_("Configuration error: the above device/driver didn't export the network port interface"));
|
---|
815 | pThis->pConfig = (PPDMINETWORKCONFIG)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_CONFIG);
|
---|
816 | if (!pThis->pConfig)
|
---|
817 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
818 | N_("Configuration error: the above device/driver didn't export the network config interface"));
|
---|
819 |
|
---|
820 | /* Generate a network address for this network card. */
|
---|
821 | rc = CFGMR3QueryString(pCfgHandle, "Network", szNetwork, sizeof(szNetwork));
|
---|
822 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
823 | RTStrPrintf(szNetwork, sizeof(szNetwork), "10.0.%d.0/24", pDrvIns->iInstance + 2);
|
---|
824 | else if (RT_FAILURE(rc))
|
---|
825 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Network\" string failed"), pDrvIns->iInstance);
|
---|
826 |
|
---|
827 | RTIPV4ADDR Network;
|
---|
828 | RTIPV4ADDR Netmask;
|
---|
829 | rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
|
---|
830 | if (RT_FAILURE(rc))
|
---|
831 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: network '%s' describes not a valid IPv4 network"), pDrvIns->iInstance, szNetwork);
|
---|
832 |
|
---|
833 | RTStrPrintf(szNetAddr, sizeof(szNetAddr), "%d.%d.%d.%d",
|
---|
834 | (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, Network & 0xFF);
|
---|
835 |
|
---|
836 | /*
|
---|
837 | * The slirp lock..
|
---|
838 | */
|
---|
839 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
840 | rc = RTCritSectInit(&pThis->CritSect);
|
---|
841 | if (RT_FAILURE(rc))
|
---|
842 | return rc;
|
---|
843 | #endif
|
---|
844 | /*
|
---|
845 | * Initialize slirp.
|
---|
846 | */
|
---|
847 | rc = slirp_init(&pThis->pNATState, &szNetAddr[0], Netmask, fPassDomain, pThis->pszTFTPPrefix, pThis->pszBootFile, pThis);
|
---|
848 | if (RT_SUCCESS(rc))
|
---|
849 | {
|
---|
850 | slirp_register_timers(pThis->pNATState, pDrvIns);
|
---|
851 | int rc2 = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfgHandle, Network);
|
---|
852 | if (RT_SUCCESS(rc2))
|
---|
853 | {
|
---|
854 | /*
|
---|
855 | * Register a load done notification to get the MAC address into the slirp
|
---|
856 | * engine after we loaded a guest state.
|
---|
857 | */
|
---|
858 | rc2 = PDMDrvHlpSSMRegister(pDrvIns, pDrvIns->pDrvReg->szDriverName,
|
---|
859 | pDrvIns->iInstance, 0, 0,
|
---|
860 | NULL, NULL, NULL, NULL, NULL, drvNATLoadDone);
|
---|
861 | AssertRC(rc2);
|
---|
862 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
863 | pDrvIns->pDrvHlp->pfnPDMPollerRegister(pDrvIns, drvNATPoller);
|
---|
864 | #else
|
---|
865 | rc = RTReqCreateQueue(&pThis->pReqQueue);
|
---|
866 | if (RT_FAILURE(rc))
|
---|
867 | {
|
---|
868 | LogRel(("Can't create request queue\n"));
|
---|
869 | return rc;
|
---|
870 | }
|
---|
871 |
|
---|
872 | rc = PDMDrvHlpPDMQueueCreate(pDrvIns, sizeof(DRVNATQUEUITEM), 50, 0, drvNATQueueConsumer, &pThis->pSendQueue);
|
---|
873 | if (RT_FAILURE(rc))
|
---|
874 | {
|
---|
875 | LogRel(("Can't create send queue\n"));
|
---|
876 | return rc;
|
---|
877 | }
|
---|
878 |
|
---|
879 | # ifndef RT_OS_WINDOWS
|
---|
880 | /*
|
---|
881 | * Create the control pipe.
|
---|
882 | */
|
---|
883 | int fds[2];
|
---|
884 | if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
|
---|
885 | {
|
---|
886 | int rc = RTErrConvertFromErrno(errno);
|
---|
887 | AssertRC(rc);
|
---|
888 | return rc;
|
---|
889 | }
|
---|
890 | pThis->PipeRead = fds[0];
|
---|
891 | pThis->PipeWrite = fds[1];
|
---|
892 | # else
|
---|
893 | pThis->hWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto-reset event */
|
---|
894 | slirp_register_external_event(pThis->pNATState, pThis->hWakeupEvent, VBOX_WAKEUP_EVENT_INDEX);
|
---|
895 | # endif
|
---|
896 |
|
---|
897 | rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pThread, pThis, drvNATAsyncIoThread, drvNATAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "NAT");
|
---|
898 | AssertReleaseRC(rc);
|
---|
899 |
|
---|
900 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
901 | rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pGuestThread, pThis, drvNATAsyncIoGuest, drvNATAsyncIoGuestWakeup, 128 * _1K, RTTHREADTYPE_EMULATION, "NAT");
|
---|
902 | AssertReleaseRC(rc);
|
---|
903 | #endif
|
---|
904 | #endif
|
---|
905 |
|
---|
906 | pThis->enmLinkState = PDMNETWORKLINKSTATE_UP;
|
---|
907 |
|
---|
908 | /* might return VINF_NAT_DNS */
|
---|
909 | return rc;
|
---|
910 | }
|
---|
911 | /* failure path */
|
---|
912 | rc = rc2;
|
---|
913 | slirp_term(pThis->pNATState);
|
---|
914 | pThis->pNATState = NULL;
|
---|
915 | }
|
---|
916 | else
|
---|
917 | {
|
---|
918 | PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
|
---|
919 | AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
|
---|
920 | }
|
---|
921 |
|
---|
922 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
923 | RTCritSectDelete(&pThis->CritSect);
|
---|
924 | #endif
|
---|
925 | return rc;
|
---|
926 | }
|
---|
927 |
|
---|
928 |
|
---|
929 | /**
|
---|
930 | * NAT network transport driver registration record.
|
---|
931 | */
|
---|
932 | const PDMDRVREG g_DrvNAT =
|
---|
933 | {
|
---|
934 | /* u32Version */
|
---|
935 | PDM_DRVREG_VERSION,
|
---|
936 | /* szDriverName */
|
---|
937 | "NAT",
|
---|
938 | /* pszDescription */
|
---|
939 | "NAT Network Transport Driver",
|
---|
940 | /* fFlags */
|
---|
941 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
942 | /* fClass. */
|
---|
943 | PDM_DRVREG_CLASS_NETWORK,
|
---|
944 | /* cMaxInstances */
|
---|
945 | 16,
|
---|
946 | /* cbInstance */
|
---|
947 | sizeof(DRVNAT),
|
---|
948 | /* pfnConstruct */
|
---|
949 | drvNATConstruct,
|
---|
950 | /* pfnDestruct */
|
---|
951 | drvNATDestruct,
|
---|
952 | /* pfnIOCtl */
|
---|
953 | NULL,
|
---|
954 | /* pfnPowerOn */
|
---|
955 | drvNATPowerOn,
|
---|
956 | /* pfnReset */
|
---|
957 | NULL,
|
---|
958 | /* pfnSuspend */
|
---|
959 | NULL,
|
---|
960 | /* pfnResume */
|
---|
961 | NULL,
|
---|
962 | /* pfnDetach */
|
---|
963 | NULL,
|
---|
964 | /* pfnPowerOff */
|
---|
965 | NULL
|
---|
966 | };
|
---|