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