1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox network devices:
|
---|
4 | * NAT network transport driver
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
28 | #include "Network/slirp/libslirp.h"
|
---|
29 | #include <VBox/pdm.h>
|
---|
30 | #include <VBox/cfgm.h>
|
---|
31 | #include <VBox/mm.h>
|
---|
32 | #include <VBox/err.h>
|
---|
33 |
|
---|
34 | #include <VBox/log.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 | #include "Builtins.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *******************************************************************************/
|
---|
45 | /**
|
---|
46 | * Block driver instance data.
|
---|
47 | */
|
---|
48 | typedef struct DRVNAT
|
---|
49 | {
|
---|
50 | /** The network interface. */
|
---|
51 | PDMINETWORKCONNECTOR INetworkConnector;
|
---|
52 | /** The port we're attached to. */
|
---|
53 | PPDMINETWORKPORT pPort;
|
---|
54 | /** Pointer to the driver instance. */
|
---|
55 | PPDMDRVINS pDrvIns;
|
---|
56 |
|
---|
57 | } DRVNAT, *PDRVNAT;
|
---|
58 |
|
---|
59 | /** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
|
---|
60 | #define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Global Variables *
|
---|
65 | *******************************************************************************/
|
---|
66 | /** @todo change this into a MAC -> PDRVNAT translation list. */
|
---|
67 | /** Pointer to the driver instance.
|
---|
68 | * This is required by slirp. */
|
---|
69 | static PDRVNAT g_pDrv = NULL;
|
---|
70 | #if 0
|
---|
71 | /** If set the thread should terminate. */
|
---|
72 | static bool g_fThreadTerm = false;
|
---|
73 | /** The thread id of the select thread (drvNATSelectThread()). */
|
---|
74 | static RTTHREAD g_ThreadSelect;
|
---|
75 |
|
---|
76 | static RTCRITSECT g_CritSectSlirp;
|
---|
77 | #endif
|
---|
78 |
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Send data to the network.
|
---|
82 | *
|
---|
83 | * @returns VBox status code.
|
---|
84 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
85 | * @param pvBuf Data to send.
|
---|
86 | * @param cb Number of bytes to send.
|
---|
87 | * @thread EMT
|
---|
88 | */
|
---|
89 | static DECLCALLBACK(int) drvNATSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
|
---|
90 | {
|
---|
91 | LogFlow(("drvNATSend: pvBuf=%p cb=%#x\n", pvBuf, cb));
|
---|
92 | Log2(("drvNATSend: pvBuf=%p cb=%#x\n"
|
---|
93 | "%.*Vhxd\n",
|
---|
94 | pvBuf, cb, cb, pvBuf));
|
---|
95 | slirp_input((uint8_t *)pvBuf, cb);
|
---|
96 | return VINF_SUCCESS;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Set promiscuous mode.
|
---|
102 | *
|
---|
103 | * This is called when the promiscuous mode is set. This means that there doesn't have
|
---|
104 | * to be a mode change when it's called.
|
---|
105 | *
|
---|
106 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
107 | * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
|
---|
108 | * @thread EMT
|
---|
109 | */
|
---|
110 | static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
|
---|
111 | {
|
---|
112 | LogFlow(("drvNATSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
|
---|
113 | /* nothing to do */
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Notification on link status changes.
|
---|
119 | *
|
---|
120 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
121 | * @param enmLinkState The new link state.
|
---|
122 | * @thread EMT
|
---|
123 | */
|
---|
124 | static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
|
---|
125 | {
|
---|
126 | LogFlow(("drvNATNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
|
---|
127 | switch (enmLinkState)
|
---|
128 | {
|
---|
129 | case PDMNETWORKLINKSTATE_UP:
|
---|
130 | LogRel(("NAT: link up\n"));
|
---|
131 | slirp_link_up();
|
---|
132 | break;
|
---|
133 | case PDMNETWORKLINKSTATE_DOWN:
|
---|
134 | case PDMNETWORKLINKSTATE_DOWN_RESUME:
|
---|
135 | LogRel(("NAT: link down\n"));
|
---|
136 | slirp_link_down();
|
---|
137 | break;
|
---|
138 | default:
|
---|
139 | AssertMsgFailed(("drvNATNotifyLinkChanged: unexpected link state %d\n", enmLinkState));
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * More receive buffer has become available.
|
---|
146 | *
|
---|
147 | * This is called when the NIC frees up receive buffers.
|
---|
148 | *
|
---|
149 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
150 | * @thread EMT
|
---|
151 | */
|
---|
152 | static DECLCALLBACK(void) drvNATNotifyCanReceive(PPDMINETWORKCONNECTOR pInterface)
|
---|
153 | {
|
---|
154 | LogFlow(("drvNATNotifyCanReceive:\n"));
|
---|
155 | /** @todo do something useful here. */
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Poller callback.
|
---|
161 | */
|
---|
162 | static DECLCALLBACK(void) drvNATPoller(PPDMDRVINS pDrvIns)
|
---|
163 | {
|
---|
164 | fd_set ReadFDs;
|
---|
165 | fd_set WriteFDs;
|
---|
166 | fd_set XcptFDs;
|
---|
167 | int cFDs = -1;
|
---|
168 | FD_ZERO(&ReadFDs);
|
---|
169 | FD_ZERO(&WriteFDs);
|
---|
170 | FD_ZERO(&XcptFDs);
|
---|
171 | slirp_select_fill(&cFDs, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
172 |
|
---|
173 | struct timeval tv = {0, 0}; /* no wait */
|
---|
174 | int cReadFDs = select(cFDs + 1, &ReadFDs, &WriteFDs, &XcptFDs, &tv);
|
---|
175 | if (cReadFDs >= 0)
|
---|
176 | slirp_select_poll(&ReadFDs, &WriteFDs, &XcptFDs);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Function called by slirp to check if it's possible to feed incoming data to the network port.
|
---|
182 | * @returns 1 if possible.
|
---|
183 | * @returns 0 if not possible.
|
---|
184 | */
|
---|
185 | int slirp_can_output(void)
|
---|
186 | {
|
---|
187 | if (g_pDrv)
|
---|
188 | return g_pDrv->pPort->pfnCanReceive(g_pDrv->pPort);
|
---|
189 | return 0;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Function called by slirp to feed incoming data to the network port.
|
---|
195 | */
|
---|
196 | void slirp_output(const uint8_t *pu8Buf, int cb)
|
---|
197 | {
|
---|
198 | Log2(("slirp_output: pu8Buf=%p cb=%#x (g_pDrv=%p)\n"
|
---|
199 | "%.*Vhxd\n",
|
---|
200 | pu8Buf, cb, g_pDrv,
|
---|
201 | cb, pu8Buf));
|
---|
202 | if (g_pDrv)
|
---|
203 | {
|
---|
204 | int rc = g_pDrv->pPort->pfnReceive(g_pDrv->pPort, pu8Buf, cb);
|
---|
205 | AssertRC(rc); NOREF(rc);
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Queries an interface to the driver.
|
---|
211 | *
|
---|
212 | * @returns Pointer to interface.
|
---|
213 | * @returns NULL if the interface was not supported by the driver.
|
---|
214 | * @param pInterface Pointer to this interface structure.
|
---|
215 | * @param enmInterface The requested interface identification.
|
---|
216 | * @thread Any thread.
|
---|
217 | */
|
---|
218 | static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
219 | {
|
---|
220 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
221 | PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
|
---|
222 | switch (enmInterface)
|
---|
223 | {
|
---|
224 | case PDMINTERFACE_BASE:
|
---|
225 | return &pDrvIns->IBase;
|
---|
226 | case PDMINTERFACE_NETWORK_CONNECTOR:
|
---|
227 | return &pData->INetworkConnector;
|
---|
228 | default:
|
---|
229 | return NULL;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Destruct a driver instance.
|
---|
236 | *
|
---|
237 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
238 | * resources can be freed correctly.
|
---|
239 | *
|
---|
240 | * @param pDrvIns The driver instance data.
|
---|
241 | */
|
---|
242 | static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
|
---|
243 | {
|
---|
244 | LogFlow(("drvNATDestruct:\n"));
|
---|
245 | #if ARCH_BITS == 64
|
---|
246 | LogRel(("NAT: g_cpvHashUsed=%RU32 g_cpvHashCollisions=%RU32 g_cpvHashInserts=%RU64 g_cpvHashDone=%RU64\n",
|
---|
247 | g_cpvHashUsed, g_cpvHashCollisions, g_cpvHashInserts, g_cpvHashDone));
|
---|
248 | #endif
|
---|
249 | slirp_term();
|
---|
250 | g_pDrv = NULL;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Sets up the redirectors.
|
---|
256 | *
|
---|
257 | * @returns VBox status code.
|
---|
258 | * @param pCfgHandle The drivers configuration handle.
|
---|
259 | */
|
---|
260 | static int drvNATConstructRedir(PCFGMNODE pCfgHandle)
|
---|
261 | {
|
---|
262 | /*
|
---|
263 | * Enumerate redirections.
|
---|
264 | */
|
---|
265 | for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfgHandle); pNode; pNode = CFGMR3GetNextChild(pNode))
|
---|
266 | {
|
---|
267 | /* protocol type */
|
---|
268 | bool fUDP;
|
---|
269 | int rc = CFGMR3QueryBool(pNode, "UDP", &fUDP);
|
---|
270 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
271 | fUDP = true;
|
---|
272 | else if (VBOX_FAILURE(rc))
|
---|
273 | {
|
---|
274 | AssertMsgFailed(("Configuration error: Boolean \"UDP\" -> %Vrc\n", rc));
|
---|
275 | return rc;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /* host port */
|
---|
279 | int32_t iHostPort;
|
---|
280 | rc = CFGMR3QueryS32(pNode, "HostPort", &iHostPort);
|
---|
281 | if (VBOX_FAILURE(rc))
|
---|
282 | {
|
---|
283 | AssertMsgFailed(("Configuration error: Boolean \"HostPort\" -> %Vrc\n", rc));
|
---|
284 | return rc;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /* guest port */
|
---|
288 | int32_t iGuestPort;
|
---|
289 | rc = CFGMR3QueryS32(pNode, "GuestPort", &iGuestPort);
|
---|
290 | if (VBOX_FAILURE(rc))
|
---|
291 | {
|
---|
292 | AssertMsgFailed(("Configuration error: Boolean \"GuestPort\" -> %Vrc\n", rc));
|
---|
293 | return rc;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /* guest address */
|
---|
297 | char szGuestIP[32];
|
---|
298 | rc = CFGMR3QueryString(pNode, "GuestIP", &szGuestIP[0], sizeof(szGuestIP));
|
---|
299 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
300 | strcpy(szGuestIP, "10.0.2.15");
|
---|
301 | else if (VBOX_FAILURE(rc))
|
---|
302 | {
|
---|
303 | AssertMsgFailed(("Configuration error: Boolean \"HostPort\" -> %Vrc\n", rc));
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 | struct in_addr GuestIP;
|
---|
307 | if (!inet_aton(szGuestIP, &GuestIP))
|
---|
308 | {
|
---|
309 | AssertMsgFailed(("Configuration error: Invalid \"GuestIP\"=\"%s\", inet_aton failed.\n", szGuestIP));
|
---|
310 | return VERR_NAT_REDIR_GUEST_IP;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /*
|
---|
314 | * Call slirp about it.
|
---|
315 | */
|
---|
316 | Log(("drvNATConstruct: Redir %d -> %s:%d\n", iHostPort, szGuestIP, iGuestPort));
|
---|
317 | if (slirp_redir(fUDP, iHostPort, GuestIP, iGuestPort) < 0)
|
---|
318 | {
|
---|
319 | AssertMsgFailed(("Configuration error: failed to setup redirection of %d to %s:%d. Probably a conflict with existing services or other rules.\n",
|
---|
320 | iHostPort, szGuestIP, iGuestPort));
|
---|
321 | return VERR_NAT_REDIR_SETUP;
|
---|
322 | }
|
---|
323 | } /* for each redir rule */
|
---|
324 |
|
---|
325 | return VINF_SUCCESS;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Construct a NAT network transport driver instance.
|
---|
331 | *
|
---|
332 | * @returns VBox status.
|
---|
333 | * @param pDrvIns The driver instance data.
|
---|
334 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
335 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
336 | * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
|
---|
337 | * iInstance it's expected to be used a bit in this function.
|
---|
338 | */
|
---|
339 | static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
340 | {
|
---|
341 | PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
|
---|
342 | LogFlow(("drvNATConstruct:\n"));
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * Validate the config.
|
---|
346 | */
|
---|
347 | if (!CFGMR3AreValuesValid(pCfgHandle, "\0"))
|
---|
348 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, "");
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * Init the static parts.
|
---|
352 | */
|
---|
353 | pData->pDrvIns = pDrvIns;
|
---|
354 | /* IBase */
|
---|
355 | pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
|
---|
356 | /* INetwork */
|
---|
357 | pData->INetworkConnector.pfnSend = drvNATSend;
|
---|
358 | pData->INetworkConnector.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
|
---|
359 | pData->INetworkConnector.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
|
---|
360 | pData->INetworkConnector.pfnNotifyCanReceive = drvNATNotifyCanReceive;
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * Query the network port interface.
|
---|
364 | */
|
---|
365 | pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
|
---|
366 | if (!pData->pPort)
|
---|
367 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
368 | N_("Configuration error: the above device/driver didn't export the network port interface!\n"));
|
---|
369 |
|
---|
370 | #if 0
|
---|
371 | /*
|
---|
372 | * The slirp lock and thread sem.
|
---|
373 | */
|
---|
374 | int rc = RTCritSectInit(&g_CritSectSlirp);
|
---|
375 | if (VBOX_FAILURE(rc))
|
---|
376 | return rc;
|
---|
377 | rc = RTSemEventCreate(&g_EventSem);
|
---|
378 | if (VBOX_SUCCESS(rc))
|
---|
379 | {
|
---|
380 | /*
|
---|
381 | * Start the select thread. (it'll block on the sem)
|
---|
382 | */
|
---|
383 | g_fThreadTerm = false;
|
---|
384 | rc = RTThreadCreate(&g_ThreadSelect, drvNATSelectThread, 0, NULL, "NATSEL");
|
---|
385 | if (VBOX_SUCCESS(rc))
|
---|
386 | {
|
---|
387 | #endif
|
---|
388 | int rc;
|
---|
389 | /*
|
---|
390 | * Initialize slirp.
|
---|
391 | */
|
---|
392 | rc = slirp_init();
|
---|
393 | if (VBOX_SUCCESS(rc))
|
---|
394 | {
|
---|
395 | rc = drvNATConstructRedir(pCfgHandle);
|
---|
396 | if (VBOX_SUCCESS(rc))
|
---|
397 | {
|
---|
398 | pDrvIns->pDrvHlp->pfnPDMPollerRegister(pDrvIns, drvNATPoller);
|
---|
399 | g_pDrv = pData;
|
---|
400 | #if 0
|
---|
401 | RTSemEventSignal(g_EventSem);
|
---|
402 | RTThreadSleep(0);
|
---|
403 | #endif
|
---|
404 | return VINF_SUCCESS;
|
---|
405 | }
|
---|
406 | /* failure path */
|
---|
407 | slirp_term();
|
---|
408 | }
|
---|
409 | else
|
---|
410 | {
|
---|
411 | switch (rc)
|
---|
412 | {
|
---|
413 | case VERR_NAT_DNS:
|
---|
414 | PDMDRV_SET_ERROR(pDrvIns, rc, N_("Domain Name Server (DNS) for NAT networking could not be determined"));
|
---|
415 | break;
|
---|
416 | default:
|
---|
417 | PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
|
---|
418 | AssertMsgFailed(("Add error message for rc=%d (%Vrc)\n", rc, rc));
|
---|
419 | break;
|
---|
420 | }
|
---|
421 | }
|
---|
422 | #if 0
|
---|
423 | g_fThreadTerm = true;
|
---|
424 | RTSemEventSignal(g_EventSem);
|
---|
425 | RTThreadSleep(0);
|
---|
426 | }
|
---|
427 | RTSemEventDestroy(g_EventSem);
|
---|
428 | g_EventSem = NULL;
|
---|
429 | }
|
---|
430 | RTCritSectDelete(&g_CritSectSlirp);
|
---|
431 | #endif
|
---|
432 | return rc;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * NAT network transport driver registration record.
|
---|
439 | */
|
---|
440 | const PDMDRVREG g_DrvNAT =
|
---|
441 | {
|
---|
442 | /* u32Version */
|
---|
443 | PDM_DRVREG_VERSION,
|
---|
444 | /* szDriverName */
|
---|
445 | "NAT",
|
---|
446 | /* pszDescription */
|
---|
447 | "NAT Network Transport Driver",
|
---|
448 | /* fFlags */
|
---|
449 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
450 | /* fClass. */
|
---|
451 | PDM_DRVREG_CLASS_NETWORK,
|
---|
452 | /* cMaxInstances */
|
---|
453 | 1,
|
---|
454 | /* cbInstance */
|
---|
455 | sizeof(DRVNAT),
|
---|
456 | /* pfnConstruct */
|
---|
457 | drvNATConstruct,
|
---|
458 | /* pfnDestruct */
|
---|
459 | drvNATDestruct,
|
---|
460 | /* pfnIOCtl */
|
---|
461 | NULL,
|
---|
462 | /* pfnPowerOn */
|
---|
463 | NULL,
|
---|
464 | /* pfnReset */
|
---|
465 | NULL,
|
---|
466 | /* pfnSuspend */
|
---|
467 | NULL,
|
---|
468 | /* pfnResume */
|
---|
469 | NULL,
|
---|
470 | /* pfnDetach */
|
---|
471 | NULL,
|
---|
472 | /* pfnPowerOff */
|
---|
473 | NULL
|
---|
474 | };
|
---|