VirtualBox

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

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

More logging

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