VirtualBox

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

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

spaces

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

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