VirtualBox

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

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

Argh

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.1 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 /** Happens during termination */
210 if (!RTCritSectIsOwner(&g_pDrv->CritSect))
211 return 0;
212
213 if (g_pDrv)
214 return g_pDrv->pPort->pfnCanReceive(g_pDrv->pPort);
215
216 return 0;
217}
218
219
220/**
221 * Function called by slirp to feed incoming data to the network port.
222 */
223void slirp_output(const uint8_t *pu8Buf, int cb)
224{
225 Log2(("slirp_output: pu8Buf=%p cb=%#x (g_pDrv=%p)\n"
226 "%.*Vhxd\n",
227 pu8Buf, cb, g_pDrv,
228 cb, pu8Buf));
229 if (g_pDrv)
230 {
231 /** Happens during termination */
232 if (!RTCritSectIsOwner(&g_pDrv->CritSect))
233 return;
234
235 int rc = g_pDrv->pPort->pfnReceive(g_pDrv->pPort, pu8Buf, cb);
236 AssertRC(rc);
237 }
238}
239
240/**
241 * Queries an interface to the driver.
242 *
243 * @returns Pointer to interface.
244 * @returns NULL if the interface was not supported by the driver.
245 * @param pInterface Pointer to this interface structure.
246 * @param enmInterface The requested interface identification.
247 * @thread Any thread.
248 */
249static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
250{
251 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
252 PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
253 switch (enmInterface)
254 {
255 case PDMINTERFACE_BASE:
256 return &pDrvIns->IBase;
257 case PDMINTERFACE_NETWORK_CONNECTOR:
258 return &pData->INetworkConnector;
259 default:
260 return NULL;
261 }
262}
263
264
265/**
266 * Destruct a driver instance.
267 *
268 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
269 * resources can be freed correctly.
270 *
271 * @param pDrvIns The driver instance data.
272 */
273static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
274{
275 PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
276
277 LogFlow(("drvNATDestruct:\n"));
278
279#if ARCH_BITS == 64
280 LogRel(("NAT: g_cpvHashUsed=%RU32 g_cpvHashCollisions=%RU32 g_cpvHashInserts=%RU64 g_cpvHashDone=%RU64\n",
281 g_cpvHashUsed, g_cpvHashCollisions, g_cpvHashInserts, g_cpvHashDone));
282#endif
283 slirp_term();
284 RTCritSectDelete(&pData->CritSect);
285 g_pDrv = NULL;
286}
287
288
289/**
290 * Sets up the redirectors.
291 *
292 * @returns VBox status code.
293 * @param pCfgHandle The drivers configuration handle.
294 */
295static int drvNATConstructRedir(PCFGMNODE pCfgHandle)
296{
297 /*
298 * Enumerate redirections.
299 */
300 for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfgHandle); pNode; pNode = CFGMR3GetNextChild(pNode))
301 {
302 /* protocol type */
303 bool fUDP;
304 int rc = CFGMR3QueryBool(pNode, "UDP", &fUDP);
305 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
306 fUDP = true;
307 else if (VBOX_FAILURE(rc))
308 {
309 AssertMsgFailed(("Configuration error: Boolean \"UDP\" -> %Vrc\n", rc));
310 return rc;
311 }
312
313 /* host port */
314 int32_t iHostPort;
315 rc = CFGMR3QueryS32(pNode, "HostPort", &iHostPort);
316 if (VBOX_FAILURE(rc))
317 {
318 AssertMsgFailed(("Configuration error: Boolean \"HostPort\" -> %Vrc\n", rc));
319 return rc;
320 }
321
322 /* guest port */
323 int32_t iGuestPort;
324 rc = CFGMR3QueryS32(pNode, "GuestPort", &iGuestPort);
325 if (VBOX_FAILURE(rc))
326 {
327 AssertMsgFailed(("Configuration error: Boolean \"GuestPort\" -> %Vrc\n", rc));
328 return rc;
329 }
330
331 /* guest address */
332 char szGuestIP[32];
333 rc = CFGMR3QueryString(pNode, "GuestIP", &szGuestIP[0], sizeof(szGuestIP));
334 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
335 strcpy(szGuestIP, "10.0.2.15");
336 else if (VBOX_FAILURE(rc))
337 {
338 AssertMsgFailed(("Configuration error: Boolean \"HostPort\" -> %Vrc\n", rc));
339 return rc;
340 }
341 struct in_addr GuestIP;
342 if (!inet_aton(szGuestIP, &GuestIP))
343 {
344 AssertMsgFailed(("Configuration error: Invalid \"GuestIP\"=\"%s\", inet_aton failed.\n", szGuestIP));
345 return VERR_NAT_REDIR_GUEST_IP;
346 }
347
348 /*
349 * Call slirp about it.
350 */
351 Log(("drvNATConstruct: Redir %d -> %s:%d\n", iHostPort, szGuestIP, iGuestPort));
352 if (slirp_redir(fUDP, iHostPort, GuestIP, iGuestPort) < 0)
353 {
354 AssertMsgFailed(("Configuration error: failed to setup redirection of %d to %s:%d. Probably a conflict with existing services or other rules.\n",
355 iHostPort, szGuestIP, iGuestPort));
356 return VERR_NAT_REDIR_SETUP;
357 }
358 } /* for each redir rule */
359
360 return VINF_SUCCESS;
361}
362
363
364/**
365 * Construct a NAT network transport driver instance.
366 *
367 * @returns VBox status.
368 * @param pDrvIns The driver instance data.
369 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
370 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
371 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
372 * iInstance it's expected to be used a bit in this function.
373 */
374static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
375{
376 PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
377 LogFlow(("drvNATConstruct:\n"));
378
379 /*
380 * Validate the config.
381 */
382 if (!CFGMR3AreValuesValid(pCfgHandle, "\0"))
383 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, "");
384
385 /*
386 * Init the static parts.
387 */
388 pData->pDrvIns = pDrvIns;
389 /* IBase */
390 pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
391 /* INetwork */
392 pData->INetworkConnector.pfnSend = drvNATSend;
393 pData->INetworkConnector.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
394 pData->INetworkConnector.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
395 pData->INetworkConnector.pfnNotifyCanReceive = drvNATNotifyCanReceive;
396
397 /*
398 * Query the network port interface.
399 */
400 pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
401 if (!pData->pPort)
402 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
403 N_("Configuration error: the above device/driver didn't export the network port interface!\n"));
404
405 /*
406 * The slirp lock..
407 */
408 int rc = RTCritSectInit(&pData->CritSect);
409 if (VBOX_FAILURE(rc))
410 return rc;
411#if 0
412 rc = RTSemEventCreate(&g_EventSem);
413 if (VBOX_SUCCESS(rc))
414 {
415 /*
416 * Start the select thread. (it'll block on the sem)
417 */
418 g_fThreadTerm = false;
419 rc = RTThreadCreate(&g_ThreadSelect, drvNATSelectThread, 0, NULL, "NATSEL");
420 if (VBOX_SUCCESS(rc))
421 {
422#endif
423 /*
424 * Initialize slirp.
425 */
426 rc = slirp_init();
427 if (VBOX_SUCCESS(rc))
428 {
429 rc = drvNATConstructRedir(pCfgHandle);
430 if (VBOX_SUCCESS(rc))
431 {
432 pDrvIns->pDrvHlp->pfnPDMPollerRegister(pDrvIns, drvNATPoller);
433 g_pDrv = pData;
434#if 0
435 RTSemEventSignal(g_EventSem);
436 RTThreadSleep(0);
437#endif
438 return VINF_SUCCESS;
439 }
440 /* failure path */
441 slirp_term();
442 }
443 else
444 {
445 switch (rc)
446 {
447 case VERR_NAT_DNS:
448 PDMDRV_SET_ERROR(pDrvIns, rc, N_("Domain Name Server (DNS) for NAT networking could not be determined"));
449 break;
450 default:
451 PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
452 AssertMsgFailed(("Add error message for rc=%d (%Vrc)\n", rc, rc));
453 break;
454 }
455 }
456#if 0
457 g_fThreadTerm = true;
458 RTSemEventSignal(g_EventSem);
459 RTThreadSleep(0);
460 }
461 RTSemEventDestroy(g_EventSem);
462 g_EventSem = NULL;
463 }
464#endif
465 RTCritSectDelete(&pData->CritSect);
466 return rc;
467}
468
469
470
471/**
472 * NAT network transport driver registration record.
473 */
474const PDMDRVREG g_DrvNAT =
475{
476 /* u32Version */
477 PDM_DRVREG_VERSION,
478 /* szDriverName */
479 "NAT",
480 /* pszDescription */
481 "NAT Network Transport Driver",
482 /* fFlags */
483 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
484 /* fClass. */
485 PDM_DRVREG_CLASS_NETWORK,
486 /* cMaxInstances */
487 1,
488 /* cbInstance */
489 sizeof(DRVNAT),
490 /* pfnConstruct */
491 drvNATConstruct,
492 /* pfnDestruct */
493 drvNATDestruct,
494 /* pfnIOCtl */
495 NULL,
496 /* pfnPowerOn */
497 NULL,
498 /* pfnReset */
499 NULL,
500 /* pfnSuspend */
501 NULL,
502 /* pfnResume */
503 NULL,
504 /* pfnDetach */
505 NULL,
506 /* pfnPowerOff */
507 NULL
508};
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