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