VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DrvNAT.cpp@ 700

Last change on this file since 700 was 700, checked in by vboxsync, 18 years ago

Another fix. Deadlock warning though.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette