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