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