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 | slirp_term();
|
---|
246 | g_pDrv = NULL;
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Sets up the redirectors.
|
---|
252 | *
|
---|
253 | * @returns VBox status code.
|
---|
254 | * @param pCfgHandle The drivers configuration handle.
|
---|
255 | */
|
---|
256 | static int drvNATConstructRedir(PCFGMNODE pCfgHandle)
|
---|
257 | {
|
---|
258 | /*
|
---|
259 | * Enumerate redirections.
|
---|
260 | */
|
---|
261 | for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfgHandle); pNode; pNode = CFGMR3GetNextChild(pNode))
|
---|
262 | {
|
---|
263 | /* protocol type */
|
---|
264 | bool fUDP;
|
---|
265 | int rc = CFGMR3QueryBool(pNode, "UDP", &fUDP);
|
---|
266 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
267 | fUDP = true;
|
---|
268 | else if (VBOX_FAILURE(rc))
|
---|
269 | {
|
---|
270 | AssertMsgFailed(("Configuration error: Boolean \"UDP\" -> %Vrc\n", rc));
|
---|
271 | return rc;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /* host port */
|
---|
275 | int32_t iHostPort;
|
---|
276 | rc = CFGMR3QueryS32(pNode, "HostPort", &iHostPort);
|
---|
277 | if (VBOX_FAILURE(rc))
|
---|
278 | {
|
---|
279 | AssertMsgFailed(("Configuration error: Boolean \"HostPort\" -> %Vrc\n", rc));
|
---|
280 | return rc;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /* guest port */
|
---|
284 | int32_t iGuestPort;
|
---|
285 | rc = CFGMR3QueryS32(pNode, "GuestPort", &iGuestPort);
|
---|
286 | if (VBOX_FAILURE(rc))
|
---|
287 | {
|
---|
288 | AssertMsgFailed(("Configuration error: Boolean \"GuestPort\" -> %Vrc\n", rc));
|
---|
289 | return rc;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /* guest address */
|
---|
293 | char szGuestIP[32];
|
---|
294 | rc = CFGMR3QueryString(pNode, "GuestIP", &szGuestIP[0], sizeof(szGuestIP));
|
---|
295 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
296 | strcpy(szGuestIP, "10.0.2.15");
|
---|
297 | else if (VBOX_FAILURE(rc))
|
---|
298 | {
|
---|
299 | AssertMsgFailed(("Configuration error: Boolean \"HostPort\" -> %Vrc\n", rc));
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 | struct in_addr GuestIP;
|
---|
303 | if (!inet_aton(szGuestIP, &GuestIP))
|
---|
304 | {
|
---|
305 | AssertMsgFailed(("Configuration error: Invalid \"GuestIP\"=\"%s\", inet_aton failed.\n", szGuestIP));
|
---|
306 | return VERR_NAT_REDIR_GUEST_IP;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /*
|
---|
310 | * Call slirp about it.
|
---|
311 | */
|
---|
312 | Log(("drvNATConstruct: Redir %d -> %s:%d\n", iHostPort, szGuestIP, iGuestPort));
|
---|
313 | if (slirp_redir(fUDP, iHostPort, GuestIP, iGuestPort) < 0)
|
---|
314 | {
|
---|
315 | AssertMsgFailed(("Configuration error: failed to setup redirection of %d to %s:%d. Probably a conflict with existing services or other rules.\n",
|
---|
316 | iHostPort, szGuestIP, iGuestPort));
|
---|
317 | return VERR_NAT_REDIR_SETUP;
|
---|
318 | }
|
---|
319 | } /* for each redir rule */
|
---|
320 |
|
---|
321 | return VINF_SUCCESS;
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Construct a NAT network transport driver instance.
|
---|
327 | *
|
---|
328 | * @returns VBox status.
|
---|
329 | * @param pDrvIns The driver instance data.
|
---|
330 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
331 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
332 | * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
|
---|
333 | * iInstance it's expected to be used a bit in this function.
|
---|
334 | */
|
---|
335 | static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
336 | {
|
---|
337 | PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
|
---|
338 | LogFlow(("drvNATConstruct:\n"));
|
---|
339 |
|
---|
340 | /*
|
---|
341 | * Validate the config.
|
---|
342 | */
|
---|
343 | if (!CFGMR3AreValuesValid(pCfgHandle, "\0"))
|
---|
344 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, "");
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Init the static parts.
|
---|
348 | */
|
---|
349 | pData->pDrvIns = pDrvIns;
|
---|
350 | /* IBase */
|
---|
351 | pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
|
---|
352 | /* INetwork */
|
---|
353 | pData->INetworkConnector.pfnSend = drvNATSend;
|
---|
354 | pData->INetworkConnector.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
|
---|
355 | pData->INetworkConnector.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
|
---|
356 | pData->INetworkConnector.pfnNotifyCanReceive = drvNATNotifyCanReceive;
|
---|
357 |
|
---|
358 | /*
|
---|
359 | * Query the network port interface.
|
---|
360 | */
|
---|
361 | pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
|
---|
362 | if (!pData->pPort)
|
---|
363 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
364 | N_("Configuration error: the above device/driver didn't export the network port interface!\n"));
|
---|
365 |
|
---|
366 | #if 0
|
---|
367 | /*
|
---|
368 | * The slirp lock and thread sem.
|
---|
369 | */
|
---|
370 | int rc = RTCritSectInit(&g_CritSectSlirp);
|
---|
371 | if (VBOX_FAILURE(rc))
|
---|
372 | return rc;
|
---|
373 | rc = RTSemEventCreate(&g_EventSem);
|
---|
374 | if (VBOX_SUCCESS(rc))
|
---|
375 | {
|
---|
376 | /*
|
---|
377 | * Start the select thread. (it'll block on the sem)
|
---|
378 | */
|
---|
379 | g_fThreadTerm = false;
|
---|
380 | rc = RTThreadCreate(&g_ThreadSelect, drvNATSelectThread, 0, NULL, "NATSEL");
|
---|
381 | if (VBOX_SUCCESS(rc))
|
---|
382 | {
|
---|
383 | #endif
|
---|
384 | int rc;
|
---|
385 | /*
|
---|
386 | * Initialize slirp.
|
---|
387 | */
|
---|
388 | rc = slirp_init();
|
---|
389 | if (VBOX_SUCCESS(rc))
|
---|
390 | {
|
---|
391 | rc = drvNATConstructRedir(pCfgHandle);
|
---|
392 | if (VBOX_SUCCESS(rc))
|
---|
393 | {
|
---|
394 | pDrvIns->pDrvHlp->pfnPDMPollerRegister(pDrvIns, drvNATPoller);
|
---|
395 | g_pDrv = pData;
|
---|
396 | #if 0
|
---|
397 | RTSemEventSignal(g_EventSem);
|
---|
398 | RTThreadSleep(0);
|
---|
399 | #endif
|
---|
400 | return VINF_SUCCESS;
|
---|
401 | }
|
---|
402 | /* failure path */
|
---|
403 | slirp_term();
|
---|
404 | }
|
---|
405 | else
|
---|
406 | {
|
---|
407 | switch (rc)
|
---|
408 | {
|
---|
409 | case VERR_NAT_DNS:
|
---|
410 | PDMDRV_SET_ERROR(pDrvIns, rc, N_("Domain Name Server (DNS) for NAT networking could not be determined"));
|
---|
411 | break;
|
---|
412 | default:
|
---|
413 | PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
|
---|
414 | AssertMsgFailed(("Add error message for rc=%d (%Vrc)\n", rc, rc));
|
---|
415 | break;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | #if 0
|
---|
419 | g_fThreadTerm = true;
|
---|
420 | RTSemEventSignal(g_EventSem);
|
---|
421 | RTThreadSleep(0);
|
---|
422 | }
|
---|
423 | RTSemEventDestroy(g_EventSem);
|
---|
424 | g_EventSem = NULL;
|
---|
425 | }
|
---|
426 | RTCritSectDelete(&g_CritSectSlirp);
|
---|
427 | #endif
|
---|
428 | return rc;
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * NAT network transport driver registration record.
|
---|
435 | */
|
---|
436 | const PDMDRVREG g_DrvNAT =
|
---|
437 | {
|
---|
438 | /* u32Version */
|
---|
439 | PDM_DRVREG_VERSION,
|
---|
440 | /* szDriverName */
|
---|
441 | "NAT",
|
---|
442 | /* pszDescription */
|
---|
443 | "NAT Network Transport Driver",
|
---|
444 | /* fFlags */
|
---|
445 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
446 | /* fClass. */
|
---|
447 | PDM_DRVREG_CLASS_NETWORK,
|
---|
448 | /* cMaxInstances */
|
---|
449 | 1,
|
---|
450 | /* cbInstance */
|
---|
451 | sizeof(DRVNAT),
|
---|
452 | /* pfnConstruct */
|
---|
453 | drvNATConstruct,
|
---|
454 | /* pfnDestruct */
|
---|
455 | drvNATDestruct,
|
---|
456 | /* pfnIOCtl */
|
---|
457 | NULL,
|
---|
458 | /* pfnPowerOn */
|
---|
459 | NULL,
|
---|
460 | /* pfnReset */
|
---|
461 | NULL,
|
---|
462 | /* pfnSuspend */
|
---|
463 | NULL,
|
---|
464 | /* pfnResume */
|
---|
465 | NULL,
|
---|
466 | /* pfnDetach */
|
---|
467 | NULL,
|
---|
468 | /* pfnPowerOff */
|
---|
469 | NULL
|
---|
470 | };
|
---|