VirtualBox

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

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

Attempt to fix slirp build for 64 bit (also eliminated global variables)

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