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/string.h>
|
---|
35 | #include <iprt/critsect.h>
|
---|
36 | #include <iprt/cidr.h>
|
---|
37 | #include <iprt/stream.h>
|
---|
38 |
|
---|
39 | #include "Builtins.h"
|
---|
40 |
|
---|
41 | #ifdef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
42 | # ifndef RT_OS_WINDOWS
|
---|
43 | # include <unistd.h>
|
---|
44 | # endif
|
---|
45 | # include <errno.h>
|
---|
46 | # include<iprt/semaphore.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *******************************************************************************/
|
---|
53 | /**
|
---|
54 | * NAT network transport driver instance data.
|
---|
55 | */
|
---|
56 | typedef struct DRVNAT
|
---|
57 | {
|
---|
58 | /** The network interface. */
|
---|
59 | PDMINETWORKCONNECTOR INetworkConnector;
|
---|
60 | /** The port we're attached to. */
|
---|
61 | PPDMINETWORKPORT pPort;
|
---|
62 | /** The network config of the port we're attached to. */
|
---|
63 | PPDMINETWORKCONFIG pConfig;
|
---|
64 | /** Pointer to the driver instance. */
|
---|
65 | PPDMDRVINS pDrvIns;
|
---|
66 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
67 | /** Slirp critical section. */
|
---|
68 | RTCRITSECT CritSect;
|
---|
69 | #endif
|
---|
70 | /** Link state */
|
---|
71 | PDMNETWORKLINKSTATE enmLinkState;
|
---|
72 | /** NAT state for this instance. */
|
---|
73 | PNATState pNATState;
|
---|
74 | /** TFTP directory prefix. */
|
---|
75 | char *pszTFTPPrefix;
|
---|
76 | /** Boot file name to provide in the DHCP server response. */
|
---|
77 | char *pszBootFile;
|
---|
78 | #ifdef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
79 | /*polling thread*/
|
---|
80 | PPDMTHREAD pThread;
|
---|
81 | /*used for wakep of poling thread*/
|
---|
82 | RTSEMEVENT semSndMutex;
|
---|
83 | #ifndef RT_OS_WINDOWS
|
---|
84 | /** The write end of the control pipe. */
|
---|
85 | RTFILE PipeWrite;
|
---|
86 | /** The read end of the control pipe. */
|
---|
87 | RTFILE PipeRead;
|
---|
88 | #else
|
---|
89 | /* 1 - for Outher network events, and 0 for sending routine notification*/
|
---|
90 | HANDLE hNetworkEvent[2];
|
---|
91 | #endif
|
---|
92 | /** Send buffer */
|
---|
93 | char cBuffer[1600];
|
---|
94 | size_t sBufferSize;
|
---|
95 | #endif
|
---|
96 | } DRVNAT, *PDRVNAT;
|
---|
97 |
|
---|
98 | /** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
|
---|
99 | #define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Send data to the network.
|
---|
105 | *
|
---|
106 | * @returns VBox status code.
|
---|
107 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
108 | * @param pvBuf Data to send.
|
---|
109 | * @param cb Number of bytes to send.
|
---|
110 | * @thread EMT
|
---|
111 | */
|
---|
112 | static DECLCALLBACK(int) drvNATSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
|
---|
113 | {
|
---|
114 | PDRVNAT pThis = PDMINETWORKCONNECTOR_2_DRVNAT(pInterface);
|
---|
115 |
|
---|
116 | LogFlow(("drvNATSend: pvBuf=%p cb=%#x\n", pvBuf, cb));
|
---|
117 | Log2(("drvNATSend: pvBuf=%p cb=%#x\n%.*Rhxd\n", pvBuf, cb, cb, pvBuf));
|
---|
118 |
|
---|
119 | #ifdef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
120 |
|
---|
121 | /*notify select to wakeup*/
|
---|
122 | memcpy(pThis->cBuffer,pvBuf, cb);
|
---|
123 | pThis->sBufferSize = cb;
|
---|
124 | # ifndef RT_OS_WINDOWS
|
---|
125 | int rc = RTFileWrite(pThis->PipeWrite, "1", 2, NULL);
|
---|
126 | AssertRC(rc);
|
---|
127 | # else
|
---|
128 | WSASetEvent(pThis->hNetworkEvent[0]);
|
---|
129 | # endif
|
---|
130 | RTSemEventWait(pThis->semSndMutex, RT_INDEFINITE_WAIT);
|
---|
131 |
|
---|
132 | #else /* ! VBOX_WITH_SIMPLEFIED_SLIRP_SYNC */
|
---|
133 |
|
---|
134 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
135 | AssertReleaseRC(rc);
|
---|
136 |
|
---|
137 | Assert(pThis->enmLinkState == PDMNETWORKLINKSTATE_UP);
|
---|
138 | if (pThis->enmLinkState == PDMNETWORKLINKSTATE_UP)
|
---|
139 | slirp_input(pThis->pNATState, (uint8_t *)pvBuf, cb);
|
---|
140 |
|
---|
141 | RTCritSectLeave(&pThis->CritSect);
|
---|
142 |
|
---|
143 | #endif /* !VBOX_WITH_SIMPLEFIED_SLIRP_SYNC */
|
---|
144 |
|
---|
145 | LogFlow(("drvNATSend: end\n"));
|
---|
146 | return VINF_SUCCESS;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Set promiscuous mode.
|
---|
152 | *
|
---|
153 | * This is called when the promiscuous mode is set. This means that there doesn't have
|
---|
154 | * to be a mode change when it's called.
|
---|
155 | *
|
---|
156 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
157 | * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
|
---|
158 | * @thread EMT
|
---|
159 | */
|
---|
160 | static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
|
---|
161 | {
|
---|
162 | LogFlow(("drvNATSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
|
---|
163 | /* nothing to do */
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Notification on link status changes.
|
---|
169 | *
|
---|
170 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
171 | * @param enmLinkState The new link state.
|
---|
172 | * @thread EMT
|
---|
173 | */
|
---|
174 | static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
|
---|
175 | {
|
---|
176 | PDRVNAT pThis = PDMINETWORKCONNECTOR_2_DRVNAT(pInterface);
|
---|
177 |
|
---|
178 | LogFlow(("drvNATNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
|
---|
179 |
|
---|
180 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
181 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
182 | AssertReleaseRC(rc);
|
---|
183 | #endif
|
---|
184 | pThis->enmLinkState = enmLinkState;
|
---|
185 |
|
---|
186 | switch (enmLinkState)
|
---|
187 | {
|
---|
188 | case PDMNETWORKLINKSTATE_UP:
|
---|
189 | LogRel(("NAT: link up\n"));
|
---|
190 | slirp_link_up(pThis->pNATState);
|
---|
191 | break;
|
---|
192 |
|
---|
193 | case PDMNETWORKLINKSTATE_DOWN:
|
---|
194 | case PDMNETWORKLINKSTATE_DOWN_RESUME:
|
---|
195 | LogRel(("NAT: link down\n"));
|
---|
196 | slirp_link_down(pThis->pNATState);
|
---|
197 | break;
|
---|
198 |
|
---|
199 | default:
|
---|
200 | AssertMsgFailed(("drvNATNotifyLinkChanged: unexpected link state %d\n", enmLinkState));
|
---|
201 | }
|
---|
202 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
203 | RTCritSectLeave(&pThis->CritSect);
|
---|
204 | #endif
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
209 | /**
|
---|
210 | * Poller callback.
|
---|
211 | */
|
---|
212 | static DECLCALLBACK(void) drvNATPoller(PPDMDRVINS pDrvIns)
|
---|
213 | {
|
---|
214 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
215 | fd_set ReadFDs;
|
---|
216 | fd_set WriteFDs;
|
---|
217 | fd_set XcptFDs;
|
---|
218 | int nFDs = -1;
|
---|
219 | FD_ZERO(&ReadFDs);
|
---|
220 | FD_ZERO(&WriteFDs);
|
---|
221 | FD_ZERO(&XcptFDs);
|
---|
222 |
|
---|
223 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
224 | AssertReleaseRC(rc);
|
---|
225 |
|
---|
226 | slirp_select_fill(pThis->pNATState, &nFDs, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
227 |
|
---|
228 | struct timeval tv = {0, 0}; /* no wait */
|
---|
229 | int cChangedFDs = select(nFDs + 1, &ReadFDs, &WriteFDs, &XcptFDs, &tv);
|
---|
230 | if (cChangedFDs >= 0)
|
---|
231 | slirp_select_poll(pThis->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
232 |
|
---|
233 | RTCritSectLeave(&pThis->CritSect);
|
---|
234 | }
|
---|
235 | #else
|
---|
236 |
|
---|
237 | static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
238 | {
|
---|
239 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
240 | fd_set ReadFDs;
|
---|
241 | fd_set WriteFDs;
|
---|
242 | fd_set XcptFDs;
|
---|
243 | int nFDs = -1;
|
---|
244 | int rc;
|
---|
245 | # ifdef RT_OS_WINDOWS
|
---|
246 | DWORD event;
|
---|
247 | HANDLE *phEvents;
|
---|
248 | # endif
|
---|
249 | const struct timeval TimeWait = { 0, 2000 }; /* 2ms for the fast timer */
|
---|
250 | const struct timeval TimeNoWait = { 0, 0 }; /* return immediately */
|
---|
251 |
|
---|
252 | LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
|
---|
253 |
|
---|
254 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
255 | return VINF_SUCCESS;
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Polling loop.
|
---|
259 | */
|
---|
260 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
261 | {
|
---|
262 | bool fWait = true;
|
---|
263 |
|
---|
264 | FD_ZERO(&ReadFDs);
|
---|
265 | FD_ZERO(&WriteFDs);
|
---|
266 | FD_ZERO(&XcptFDs);
|
---|
267 | nFDs = -1;
|
---|
268 |
|
---|
269 | /*
|
---|
270 | * To prevent concurent execution of sending/receving threads
|
---|
271 | */
|
---|
272 | slirp_select_fill(pThis->pNATState, &nFDs, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
273 | # ifndef RT_OS_WINDOWS
|
---|
274 | struct timeval tv = fWait ? TimeWait : TimeNoWait;
|
---|
275 | FD_SET(pThis->PipeRead, &ReadFDs); /* Linux only */
|
---|
276 | nFDs = ((int)pThis->PipeRead < nFDs ? nFDs : pThis->PipeRead);
|
---|
277 | int cChangedFDs = select(nFDs + 1, &ReadFDs, &WriteFDs, &XcptFDs, &tv);
|
---|
278 | if (cChangedFDs >= 0)
|
---|
279 | {
|
---|
280 | slirp_select_poll(pThis->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
281 | if (FD_ISSET(pThis->PipeRead, &ReadFDs))
|
---|
282 | {
|
---|
283 | /* drain the pipe */
|
---|
284 | char ch[2];
|
---|
285 | size_t cbRead;
|
---|
286 | RTFileRead(pThis->PipeRead, &ch, 2, &cbRead);
|
---|
287 | switch (ch[0])
|
---|
288 | {
|
---|
289 | case '1':
|
---|
290 | /* called from drvNATSend */
|
---|
291 | slirp_input(pThis->pNATState, (uint8_t *)pThis->cBuffer, pThis->sBufferSize);
|
---|
292 | RTSemEventSignal(pThis->semSndMutex);
|
---|
293 | fWait = 1;
|
---|
294 | break;
|
---|
295 | case '2':
|
---|
296 | /* wakeup only */
|
---|
297 | break;
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|
301 | # else /* RT_OS_WINDOWS */
|
---|
302 | phEvents = slirp_get_events(pThis->pNATState);
|
---|
303 | phEvents[0] = pThis->hNetworkEvent[0];
|
---|
304 | event = WSAWaitForMultipleEvents(nFDs, phEvents, FALSE, 2 /*ms*/, FALSE);
|
---|
305 | AssertRelease(event != WSA_WAIT_FAILED);
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * see WSAWaitForMultipleEvents documentation: return value is a minimal index in array
|
---|
309 | */
|
---|
310 | if ((event - WSA_WAIT_EVENT_0) > 1)
|
---|
311 | slirp_select_poll(pThis->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
312 |
|
---|
313 | if ((event - WSA_WAIT_EVENT_0) == 0)
|
---|
314 | {
|
---|
315 | /** XXX distinguish between drvNATSend and wakeup only */
|
---|
316 | slirp_input(pThis->pNATState, (uint8_t *)pThis->cBuffer, pThis->sBufferSize);
|
---|
317 | WSAResetEvent(pThis->hNetworkEvent[0]);
|
---|
318 | RTSemEventSignal(pThis->semSndMutex);
|
---|
319 | }
|
---|
320 | WSAResetEvent(pThis->hNetworkEvent[0]);
|
---|
321 | # endif /* RT_OS_WINDOWS */
|
---|
322 | }
|
---|
323 |
|
---|
324 | return VINF_SUCCESS;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Unblock the send thread so it can respond to a state change.
|
---|
329 | *
|
---|
330 | * @returns VBox status code.
|
---|
331 | * @param pDevIns The pcnet device instance.
|
---|
332 | * @param pThread The send thread.
|
---|
333 | */
|
---|
334 | static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
335 | {
|
---|
336 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
337 |
|
---|
338 | # ifndef RT_OS_WINDOWS
|
---|
339 | int rc = RTFileWrite(pThis->PipeWrite, "2", 2, NULL);
|
---|
340 | AssertRC(rc);
|
---|
341 | RTSemEventSignal(pThis->semSndMutex);
|
---|
342 | #endif
|
---|
343 | return VINF_SUCCESS;
|
---|
344 | }
|
---|
345 |
|
---|
346 | #endif
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Function called by slirp to check if it's possible to feed incoming data to the network port.
|
---|
350 | * @returns 1 if possible.
|
---|
351 | * @returns 0 if not possible.
|
---|
352 | */
|
---|
353 | int slirp_can_output(void *pvUser)
|
---|
354 | {
|
---|
355 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
356 |
|
---|
357 | Assert(pThis);
|
---|
358 |
|
---|
359 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
360 | /** Happens during termination */
|
---|
361 | if (!RTCritSectIsOwner(&pThis->CritSect))
|
---|
362 | return 0;
|
---|
363 | #endif
|
---|
364 |
|
---|
365 | int rc = pThis->pPort->pfnWaitReceiveAvail(pThis->pPort, 0);
|
---|
366 | return RT_SUCCESS(rc);
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Function called by slirp to feed incoming data to the network port.
|
---|
372 | */
|
---|
373 | void slirp_output(void *pvUser, const uint8_t *pu8Buf, int cb)
|
---|
374 | {
|
---|
375 | PDRVNAT pThis = (PDRVNAT)pvUser;
|
---|
376 |
|
---|
377 | LogFlow(("slirp_output BEGIN %x %d\n", pu8Buf, cb));
|
---|
378 | Log2(("slirp_output: pu8Buf=%p cb=%#x (pThis=%p)\n%.*Rhxd\n", pu8Buf, cb, pThis, cb, pu8Buf));
|
---|
379 |
|
---|
380 | Assert(pThis);
|
---|
381 |
|
---|
382 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
383 | /** Happens during termination */
|
---|
384 | if (!RTCritSectIsOwner(&pThis->CritSect))
|
---|
385 | return;
|
---|
386 | #endif
|
---|
387 |
|
---|
388 | int rc = pThis->pPort->pfnReceive(pThis->pPort, pu8Buf, cb);
|
---|
389 | AssertRC(rc);
|
---|
390 | LogFlow(("slirp_output END %x %d\n", pu8Buf, cb));
|
---|
391 | }
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Queries an interface to the driver.
|
---|
395 | *
|
---|
396 | * @returns Pointer to interface.
|
---|
397 | * @returns NULL if the interface was not supported by the driver.
|
---|
398 | * @param pInterface Pointer to this interface structure.
|
---|
399 | * @param enmInterface The requested interface identification.
|
---|
400 | * @thread Any thread.
|
---|
401 | */
|
---|
402 | static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
403 | {
|
---|
404 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
405 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
406 | switch (enmInterface)
|
---|
407 | {
|
---|
408 | case PDMINTERFACE_BASE:
|
---|
409 | return &pDrvIns->IBase;
|
---|
410 | case PDMINTERFACE_NETWORK_CONNECTOR:
|
---|
411 | return &pThis->INetworkConnector;
|
---|
412 | default:
|
---|
413 | return NULL;
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Destruct a driver instance.
|
---|
420 | *
|
---|
421 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
422 | * resources can be freed correctly.
|
---|
423 | *
|
---|
424 | * @param pDrvIns The driver instance data.
|
---|
425 | */
|
---|
426 | static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
|
---|
427 | {
|
---|
428 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
429 |
|
---|
430 | LogFlow(("drvNATDestruct:\n"));
|
---|
431 |
|
---|
432 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
433 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
434 | AssertReleaseRC(rc);
|
---|
435 | #endif
|
---|
436 | slirp_term(pThis->pNATState);
|
---|
437 | pThis->pNATState = NULL;
|
---|
438 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
439 | RTCritSectLeave(&pThis->CritSect);
|
---|
440 | RTCritSectDelete(&pThis->CritSect);
|
---|
441 | #else
|
---|
442 | RTSemEventDestroy(pThis->semSndMutex);
|
---|
443 | #endif
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Sets up the redirectors.
|
---|
449 | *
|
---|
450 | * @returns VBox status code.
|
---|
451 | * @param pCfgHandle The drivers configuration handle.
|
---|
452 | */
|
---|
453 | static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfgHandle, RTIPV4ADDR Network)
|
---|
454 | {
|
---|
455 | /*
|
---|
456 | * Enumerate redirections.
|
---|
457 | */
|
---|
458 | for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfgHandle); pNode; pNode = CFGMR3GetNextChild(pNode))
|
---|
459 | {
|
---|
460 | /*
|
---|
461 | * Validate the port forwarding config.
|
---|
462 | */
|
---|
463 | if (!CFGMR3AreValuesValid(pNode, "Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0"))
|
---|
464 | return PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown configuration in port forwarding"));
|
---|
465 |
|
---|
466 | /* protocol type */
|
---|
467 | bool fUDP;
|
---|
468 | char szProtocol[32];
|
---|
469 | int rc = CFGMR3QueryString(pNode, "Protocol", &szProtocol[0], sizeof(szProtocol));
|
---|
470 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
471 | {
|
---|
472 | rc = CFGMR3QueryBool(pNode, "UDP", &fUDP);
|
---|
473 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
474 | fUDP = false;
|
---|
475 | else if (RT_FAILURE(rc))
|
---|
476 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"UDP\" boolean failed"), iInstance);
|
---|
477 | }
|
---|
478 | else if (RT_SUCCESS(rc))
|
---|
479 | {
|
---|
480 | if (!RTStrICmp(szProtocol, "TCP"))
|
---|
481 | fUDP = false;
|
---|
482 | else if (!RTStrICmp(szProtocol, "UDP"))
|
---|
483 | fUDP = true;
|
---|
484 | else
|
---|
485 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""), iInstance, szProtocol);
|
---|
486 | }
|
---|
487 | else
|
---|
488 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Protocol\" string failed"), iInstance);
|
---|
489 |
|
---|
490 | /* host port */
|
---|
491 | int32_t iHostPort;
|
---|
492 | rc = CFGMR3QueryS32(pNode, "HostPort", &iHostPort);
|
---|
493 | if (RT_FAILURE(rc))
|
---|
494 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"HostPort\" integer failed"), iInstance);
|
---|
495 |
|
---|
496 | /* guest port */
|
---|
497 | int32_t iGuestPort;
|
---|
498 | rc = CFGMR3QueryS32(pNode, "GuestPort", &iGuestPort);
|
---|
499 | if (RT_FAILURE(rc))
|
---|
500 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestPort\" integer failed"), iInstance);
|
---|
501 |
|
---|
502 | /* guest address */
|
---|
503 | char szGuestIP[32];
|
---|
504 | rc = CFGMR3QueryString(pNode, "GuestIP", &szGuestIP[0], sizeof(szGuestIP));
|
---|
505 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
506 | RTStrPrintf(szGuestIP, sizeof(szGuestIP), "%d.%d.%d.%d",
|
---|
507 | (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, (Network & 0xE0) | 15);
|
---|
508 | else if (RT_FAILURE(rc))
|
---|
509 | return PDMDrvHlpVMSetError(pThis->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestIP\" string failed"), iInstance);
|
---|
510 | struct in_addr GuestIP;
|
---|
511 | if (!inet_aton(szGuestIP, &GuestIP))
|
---|
512 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_GUEST_IP, RT_SRC_POS,
|
---|
513 | N_("NAT#%d: configuration error: invalid \"GuestIP\"=\"%s\", inet_aton failed"), iInstance, szGuestIP);
|
---|
514 |
|
---|
515 | /*
|
---|
516 | * Call slirp about it.
|
---|
517 | */
|
---|
518 | Log(("drvNATConstruct: Redir %d -> %s:%d\n", iHostPort, szGuestIP, iGuestPort));
|
---|
519 | if (slirp_redir(pThis->pNATState, fUDP, iHostPort, GuestIP, iGuestPort) < 0)
|
---|
520 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS,
|
---|
521 | 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);
|
---|
522 | } /* for each redir rule */
|
---|
523 |
|
---|
524 | return VINF_SUCCESS;
|
---|
525 | }
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * Get the MAC address into the slirp stack.
|
---|
529 | */
|
---|
530 | static void drvNATSetMac(PDRVNAT pThis)
|
---|
531 | {
|
---|
532 | if (pThis->pConfig)
|
---|
533 | {
|
---|
534 | RTMAC Mac;
|
---|
535 | pThis->pConfig->pfnGetMac(pThis->pConfig, &Mac);
|
---|
536 | slirp_set_ethaddr(pThis->pNATState, Mac.au8);
|
---|
537 | }
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * After loading we have to pass the MAC address of the ethernet device to the slirp stack.
|
---|
543 | * Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
|
---|
544 | * (usually done during guest boot).
|
---|
545 | */
|
---|
546 | static DECLCALLBACK(int) drvNATLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSMHandle)
|
---|
547 | {
|
---|
548 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
549 | drvNATSetMac(pThis);
|
---|
550 | return VINF_SUCCESS;
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Some guests might not use DHCP to retrieve an IP but use a static IP.
|
---|
556 | */
|
---|
557 | static DECLCALLBACK(void) drvNATPowerOn(PPDMDRVINS pDrvIns)
|
---|
558 | {
|
---|
559 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
560 | drvNATSetMac(pThis);
|
---|
561 | }
|
---|
562 |
|
---|
563 |
|
---|
564 | /**
|
---|
565 | * Construct a NAT network transport driver instance.
|
---|
566 | *
|
---|
567 | * @returns VBox status.
|
---|
568 | * @param pDrvIns The driver instance data.
|
---|
569 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
570 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
571 | * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
|
---|
572 | * iInstance it's expected to be used a bit in this function.
|
---|
573 | */
|
---|
574 | static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
575 | {
|
---|
576 | PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
|
---|
577 | char szNetAddr[16];
|
---|
578 | char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
|
---|
579 | LogFlow(("drvNATConstruct:\n"));
|
---|
580 |
|
---|
581 | /*
|
---|
582 | * Validate the config.
|
---|
583 | */
|
---|
584 | if (!CFGMR3AreValuesValid(pCfgHandle, "PassDomain\0TFTPPrefix\0BootFile\0Network\0"))
|
---|
585 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown NAT configuration option, only supports PassDomain, TFTPPrefix, BootFile and Network"));
|
---|
586 |
|
---|
587 | /*
|
---|
588 | * Init the static parts.
|
---|
589 | */
|
---|
590 | pThis->pDrvIns = pDrvIns;
|
---|
591 | pThis->pNATState = NULL;
|
---|
592 | pThis->pszTFTPPrefix = NULL;
|
---|
593 | pThis->pszBootFile = NULL;
|
---|
594 | /* IBase */
|
---|
595 | pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
|
---|
596 | /* INetwork */
|
---|
597 | pThis->INetworkConnector.pfnSend = drvNATSend;
|
---|
598 | pThis->INetworkConnector.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
|
---|
599 | pThis->INetworkConnector.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
|
---|
600 |
|
---|
601 | /*
|
---|
602 | * Get the configuration settings.
|
---|
603 | */
|
---|
604 | bool fPassDomain = true;
|
---|
605 | int rc = CFGMR3QueryBool(pCfgHandle, "PassDomain", &fPassDomain);
|
---|
606 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
607 | fPassDomain = true;
|
---|
608 | else if (RT_FAILURE(rc))
|
---|
609 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"PassDomain\" boolean failed"), pDrvIns->iInstance);
|
---|
610 |
|
---|
611 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "TFTPPrefix", &pThis->pszTFTPPrefix);
|
---|
612 | if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
613 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"TFTPPrefix\" string failed"), pDrvIns->iInstance);
|
---|
614 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "BootFile", &pThis->pszBootFile);
|
---|
615 | if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
616 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"BootFile\" string failed"), pDrvIns->iInstance);
|
---|
617 |
|
---|
618 | /*
|
---|
619 | * Query the network port interface.
|
---|
620 | */
|
---|
621 | pThis->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
|
---|
622 | if (!pThis->pPort)
|
---|
623 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
624 | N_("Configuration error: the above device/driver didn't export the network port interface"));
|
---|
625 | pThis->pConfig = (PPDMINETWORKCONFIG)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_CONFIG);
|
---|
626 | if (!pThis->pConfig)
|
---|
627 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
628 | N_("Configuration error: the above device/driver didn't export the network config interface"));
|
---|
629 |
|
---|
630 | /* Generate a network address for this network card. */
|
---|
631 | rc = CFGMR3QueryString(pCfgHandle, "Network", szNetwork, sizeof(szNetwork));
|
---|
632 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
633 | RTStrPrintf(szNetwork, sizeof(szNetwork), "10.0.%d.0/24", pDrvIns->iInstance + 2);
|
---|
634 | else if (RT_FAILURE(rc))
|
---|
635 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Network\" string failed"), pDrvIns->iInstance);
|
---|
636 |
|
---|
637 | RTIPV4ADDR Network;
|
---|
638 | RTIPV4ADDR Netmask;
|
---|
639 | rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
|
---|
640 | if (RT_FAILURE(rc))
|
---|
641 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: network '%s' describes not a valid IPv4 network"), pDrvIns->iInstance, szNetwork);
|
---|
642 |
|
---|
643 | RTStrPrintf(szNetAddr, sizeof(szNetAddr), "%d.%d.%d.%d",
|
---|
644 | (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, Network & 0xFF);
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * The slirp lock..
|
---|
648 | */
|
---|
649 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
650 | rc = RTCritSectInit(&pThis->CritSect);
|
---|
651 | if (RT_FAILURE(rc))
|
---|
652 | return rc;
|
---|
653 | #endif
|
---|
654 | /*
|
---|
655 | * Initialize slirp.
|
---|
656 | */
|
---|
657 | rc = slirp_init(&pThis->pNATState, &szNetAddr[0], Netmask, fPassDomain, pThis->pszTFTPPrefix, pThis->pszBootFile, pThis);
|
---|
658 | if (RT_SUCCESS(rc))
|
---|
659 | {
|
---|
660 | int rc2 = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfgHandle, Network);
|
---|
661 | if (RT_SUCCESS(rc2))
|
---|
662 | {
|
---|
663 | /*
|
---|
664 | * Register a load done notification to get the MAC address into the slirp
|
---|
665 | * engine after we loaded a guest state.
|
---|
666 | */
|
---|
667 | rc2 = PDMDrvHlpSSMRegister(pDrvIns, pDrvIns->pDrvReg->szDriverName,
|
---|
668 | pDrvIns->iInstance, 0, 0,
|
---|
669 | NULL, NULL, NULL, NULL, NULL, drvNATLoadDone);
|
---|
670 | AssertRC(rc2);
|
---|
671 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
672 | pDrvIns->pDrvHlp->pfnPDMPollerRegister(pDrvIns, drvNATPoller);
|
---|
673 | #else
|
---|
674 | rc = RTSemEventCreate(&pThis->semSndMutex);
|
---|
675 | AssertReleaseRC(rc);
|
---|
676 |
|
---|
677 | # ifndef RT_OS_WINDOWS
|
---|
678 | /*
|
---|
679 | * Create the control pipe.
|
---|
680 | */
|
---|
681 | int fds[2];
|
---|
682 | if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
|
---|
683 | {
|
---|
684 | int rc = RTErrConvertFromErrno(errno);
|
---|
685 | AssertRC(rc);
|
---|
686 | return rc;
|
---|
687 | }
|
---|
688 | pThis->PipeRead = fds[0];
|
---|
689 | pThis->PipeWrite = fds[1];
|
---|
690 | # else
|
---|
691 | pThis->hNetworkEvent[0] = WSACreateEvent();
|
---|
692 | pThis->hNetworkEvent[1] = WSACreateEvent();
|
---|
693 | # endif
|
---|
694 |
|
---|
695 | rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pThread, pThis, drvNATAsyncIoThread, drvNATAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "NAT");
|
---|
696 | AssertReleaseRC(rc);
|
---|
697 | #endif
|
---|
698 |
|
---|
699 | pThis->enmLinkState = PDMNETWORKLINKSTATE_UP;
|
---|
700 |
|
---|
701 | /* might return VINF_NAT_DNS */
|
---|
702 | return rc;
|
---|
703 | }
|
---|
704 | /* failure path */
|
---|
705 | rc = rc2;
|
---|
706 | slirp_term(pThis->pNATState);
|
---|
707 | pThis->pNATState = NULL;
|
---|
708 | }
|
---|
709 | else
|
---|
710 | {
|
---|
711 | PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
|
---|
712 | AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
|
---|
713 | }
|
---|
714 |
|
---|
715 | #ifndef VBOX_WITH_SIMPLEFIED_SLIRP_SYNC
|
---|
716 | RTCritSectDelete(&pThis->CritSect);
|
---|
717 | #endif
|
---|
718 | return rc;
|
---|
719 | }
|
---|
720 |
|
---|
721 |
|
---|
722 | /**
|
---|
723 | * NAT network transport driver registration record.
|
---|
724 | */
|
---|
725 | const PDMDRVREG g_DrvNAT =
|
---|
726 | {
|
---|
727 | /* u32Version */
|
---|
728 | PDM_DRVREG_VERSION,
|
---|
729 | /* szDriverName */
|
---|
730 | "NAT",
|
---|
731 | /* pszDescription */
|
---|
732 | "NAT Network Transport Driver",
|
---|
733 | /* fFlags */
|
---|
734 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
735 | /* fClass. */
|
---|
736 | PDM_DRVREG_CLASS_NETWORK,
|
---|
737 | /* cMaxInstances */
|
---|
738 | 16,
|
---|
739 | /* cbInstance */
|
---|
740 | sizeof(DRVNAT),
|
---|
741 | /* pfnConstruct */
|
---|
742 | drvNATConstruct,
|
---|
743 | /* pfnDestruct */
|
---|
744 | drvNATDestruct,
|
---|
745 | /* pfnIOCtl */
|
---|
746 | NULL,
|
---|
747 | /* pfnPowerOn */
|
---|
748 | drvNATPowerOn,
|
---|
749 | /* pfnReset */
|
---|
750 | NULL,
|
---|
751 | /* pfnSuspend */
|
---|
752 | NULL,
|
---|
753 | /* pfnResume */
|
---|
754 | NULL,
|
---|
755 | /* pfnDetach */
|
---|
756 | NULL,
|
---|
757 | /* pfnPowerOff */
|
---|
758 | NULL
|
---|
759 | };
|
---|