1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox network devices:
|
---|
4 | * NAT network transport driver
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
28 | #define __STDC_LIMIT_MACROS
|
---|
29 | #define __STDC_CONSTANT_MACROS
|
---|
30 | #include "Network/slirp/libslirp.h"
|
---|
31 | #include <VBox/pdmdrv.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/file.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/critsect.h>
|
---|
36 | #include <iprt/cidr.h>
|
---|
37 |
|
---|
38 | #include "Builtins.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Structures and Typedefs *
|
---|
43 | *******************************************************************************/
|
---|
44 | /**
|
---|
45 | * NAT network transport driver instance data.
|
---|
46 | */
|
---|
47 | typedef struct DRVNAT
|
---|
48 | {
|
---|
49 | /** The network interface. */
|
---|
50 | PDMINETWORKCONNECTOR INetworkConnector;
|
---|
51 | /** The port we're attached to. */
|
---|
52 | PPDMINETWORKPORT pPort;
|
---|
53 | /** The network config of the port we're attached to. */
|
---|
54 | PPDMINETWORKCONFIG pConfig;
|
---|
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 | /** TFTP directory prefix. */
|
---|
64 | char *pszTFTPPrefix;
|
---|
65 | /** Boot file name to provide in the DHCP server response. */
|
---|
66 | char *pszBootFile;
|
---|
67 | } DRVNAT, *PDRVNAT;
|
---|
68 |
|
---|
69 | /** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
|
---|
70 | #define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
|
---|
71 |
|
---|
72 |
|
---|
73 | /*******************************************************************************
|
---|
74 | * Global Variables *
|
---|
75 | *******************************************************************************/
|
---|
76 | #if 0
|
---|
77 | /** If set the thread should terminate. */
|
---|
78 | static bool g_fThreadTerm = false;
|
---|
79 | /** The thread id of the select thread (drvNATSelectThread()). */
|
---|
80 | static RTTHREAD g_ThreadSelect;
|
---|
81 | #endif
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Send data to the network.
|
---|
86 | *
|
---|
87 | * @returns VBox status code.
|
---|
88 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
89 | * @param pvBuf Data to send.
|
---|
90 | * @param cb Number of bytes to send.
|
---|
91 | * @thread EMT
|
---|
92 | */
|
---|
93 | static DECLCALLBACK(int) drvNATSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
|
---|
94 | {
|
---|
95 | PDRVNAT pData = PDMINETWORKCONNECTOR_2_DRVNAT(pInterface);
|
---|
96 |
|
---|
97 | LogFlow(("drvNATSend: pvBuf=%p cb=%#x\n", pvBuf, cb));
|
---|
98 | Log2(("drvNATSend: pvBuf=%p cb=%#x\n"
|
---|
99 | "%.*Vhxd\n",
|
---|
100 | pvBuf, cb, cb, pvBuf));
|
---|
101 |
|
---|
102 | int rc = RTCritSectEnter(&pData->CritSect);
|
---|
103 | AssertReleaseRC(rc);
|
---|
104 |
|
---|
105 | Assert(pData->enmLinkState == PDMNETWORKLINKSTATE_UP);
|
---|
106 | if (pData->enmLinkState == PDMNETWORKLINKSTATE_UP)
|
---|
107 | slirp_input(pData->pNATState, (uint8_t *)pvBuf, cb);
|
---|
108 | RTCritSectLeave(&pData->CritSect);
|
---|
109 | LogFlow(("drvNATSend: end\n"));
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Set promiscuous mode.
|
---|
116 | *
|
---|
117 | * This is called when the promiscuous mode is set. This means that there doesn't have
|
---|
118 | * to be a mode change when it's called.
|
---|
119 | *
|
---|
120 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
121 | * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
|
---|
122 | * @thread EMT
|
---|
123 | */
|
---|
124 | static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
|
---|
125 | {
|
---|
126 | LogFlow(("drvNATSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
|
---|
127 | /* nothing to do */
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Notification on link status changes.
|
---|
133 | *
|
---|
134 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
135 | * @param enmLinkState The new link state.
|
---|
136 | * @thread EMT
|
---|
137 | */
|
---|
138 | static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
|
---|
139 | {
|
---|
140 | PDRVNAT pData = PDMINETWORKCONNECTOR_2_DRVNAT(pInterface);
|
---|
141 |
|
---|
142 | LogFlow(("drvNATNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
|
---|
143 |
|
---|
144 | int rc = RTCritSectEnter(&pData->CritSect);
|
---|
145 | AssertReleaseRC(rc);
|
---|
146 | pData->enmLinkState = enmLinkState;
|
---|
147 |
|
---|
148 | switch (enmLinkState)
|
---|
149 | {
|
---|
150 | case PDMNETWORKLINKSTATE_UP:
|
---|
151 | LogRel(("NAT: link up\n"));
|
---|
152 | slirp_link_up(pData->pNATState);
|
---|
153 | break;
|
---|
154 |
|
---|
155 | case PDMNETWORKLINKSTATE_DOWN:
|
---|
156 | case PDMNETWORKLINKSTATE_DOWN_RESUME:
|
---|
157 | LogRel(("NAT: link down\n"));
|
---|
158 | slirp_link_down(pData->pNATState);
|
---|
159 | break;
|
---|
160 |
|
---|
161 | default:
|
---|
162 | AssertMsgFailed(("drvNATNotifyLinkChanged: unexpected link state %d\n", enmLinkState));
|
---|
163 | }
|
---|
164 | RTCritSectLeave(&pData->CritSect);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Poller callback.
|
---|
170 | */
|
---|
171 | static DECLCALLBACK(void) drvNATPoller(PPDMDRVINS pDrvIns)
|
---|
172 | {
|
---|
173 | PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
|
---|
174 | fd_set ReadFDs;
|
---|
175 | fd_set WriteFDs;
|
---|
176 | fd_set XcptFDs;
|
---|
177 | int cFDs = -1;
|
---|
178 | FD_ZERO(&ReadFDs);
|
---|
179 | FD_ZERO(&WriteFDs);
|
---|
180 | FD_ZERO(&XcptFDs);
|
---|
181 |
|
---|
182 | int rc = RTCritSectEnter(&pData->CritSect);
|
---|
183 | AssertReleaseRC(rc);
|
---|
184 |
|
---|
185 | slirp_select_fill(pData->pNATState, &cFDs, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
186 |
|
---|
187 | struct timeval tv = {0, 0}; /* no wait */
|
---|
188 | int cReadFDs = select(cFDs + 1, &ReadFDs, &WriteFDs, &XcptFDs, &tv);
|
---|
189 | if (cReadFDs >= 0)
|
---|
190 | slirp_select_poll(pData->pNATState, &ReadFDs, &WriteFDs, &XcptFDs);
|
---|
191 |
|
---|
192 | RTCritSectLeave(&pData->CritSect);
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Function called by slirp to check if it's possible to feed incoming data to the network port.
|
---|
198 | * @returns 1 if possible.
|
---|
199 | * @returns 0 if not possible.
|
---|
200 | */
|
---|
201 | int slirp_can_output(void *pvUser)
|
---|
202 | {
|
---|
203 | PDRVNAT pData = (PDRVNAT)pvUser;
|
---|
204 |
|
---|
205 | Assert(pData);
|
---|
206 |
|
---|
207 | /** Happens during termination */
|
---|
208 | if (!RTCritSectIsOwner(&pData->CritSect))
|
---|
209 | return 0;
|
---|
210 |
|
---|
211 | int rc = pData->pPort->pfnWaitReceiveAvail(pData->pPort, 0);
|
---|
212 | return RT_SUCCESS(rc);
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Function called by slirp to feed incoming data to the network port.
|
---|
218 | */
|
---|
219 | void slirp_output(void *pvUser, const uint8_t *pu8Buf, int cb)
|
---|
220 | {
|
---|
221 | PDRVNAT pData = (PDRVNAT)pvUser;
|
---|
222 |
|
---|
223 | LogFlow(("slirp_output BEGIN %x %d\n", pu8Buf, cb));
|
---|
224 | Log2(("slirp_output: pu8Buf=%p cb=%#x (pData=%p)\n"
|
---|
225 | "%.*Vhxd\n",
|
---|
226 | pu8Buf, cb, pData,
|
---|
227 | cb, pu8Buf));
|
---|
228 |
|
---|
229 | Assert(pData);
|
---|
230 |
|
---|
231 | /** Happens during termination */
|
---|
232 | if (!RTCritSectIsOwner(&pData->CritSect))
|
---|
233 | return;
|
---|
234 |
|
---|
235 | int rc = pData->pPort->pfnReceive(pData->pPort, pu8Buf, cb);
|
---|
236 | AssertRC(rc);
|
---|
237 | LogFlow(("slirp_output END %x %d\n", pu8Buf, cb));
|
---|
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 | */
|
---|
249 | static 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 | */
|
---|
273 | static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
|
---|
274 | {
|
---|
275 | PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
|
---|
276 |
|
---|
277 | LogFlow(("drvNATDestruct:\n"));
|
---|
278 |
|
---|
279 | int rc = RTCritSectEnter(&pData->CritSect);
|
---|
280 | AssertReleaseRC(rc);
|
---|
281 | slirp_term(pData->pNATState);
|
---|
282 | pData->pNATState = NULL;
|
---|
283 | RTCritSectLeave(&pData->CritSect);
|
---|
284 |
|
---|
285 | RTCritSectDelete(&pData->CritSect);
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Sets up the redirectors.
|
---|
291 | *
|
---|
292 | * @returns VBox status code.
|
---|
293 | * @param pCfgHandle The drivers configuration handle.
|
---|
294 | */
|
---|
295 | static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pData, PCFGMNODE pCfgHandle, RTIPV4ADDR Network)
|
---|
296 | {
|
---|
297 | /*
|
---|
298 | * Enumerate redirections.
|
---|
299 | */
|
---|
300 | for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfgHandle); pNode; pNode = CFGMR3GetNextChild(pNode))
|
---|
301 | {
|
---|
302 | /*
|
---|
303 | * Validate the port forwarding config.
|
---|
304 | */
|
---|
305 | if (!CFGMR3AreValuesValid(pNode, "Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0"))
|
---|
306 | return PDMDRV_SET_ERROR(pData->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown configuration in port forwarding"));
|
---|
307 |
|
---|
308 | /* protocol type */
|
---|
309 | bool fUDP;
|
---|
310 | char szProtocol[32];
|
---|
311 | int rc = CFGMR3QueryString(pNode, "Protocol", &szProtocol[0], sizeof(szProtocol));
|
---|
312 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
313 | {
|
---|
314 | rc = CFGMR3QueryBool(pNode, "UDP", &fUDP);
|
---|
315 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
316 | fUDP = false;
|
---|
317 | else if (VBOX_FAILURE(rc))
|
---|
318 | return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"UDP\" boolean failed"), iInstance);
|
---|
319 | }
|
---|
320 | else if (VBOX_SUCCESS(rc))
|
---|
321 | {
|
---|
322 | if (!RTStrICmp(szProtocol, "TCP"))
|
---|
323 | fUDP = false;
|
---|
324 | else if (!RTStrICmp(szProtocol, "UDP"))
|
---|
325 | fUDP = true;
|
---|
326 | else
|
---|
327 | return PDMDrvHlpVMSetError(pData->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""), iInstance, szProtocol);
|
---|
328 | }
|
---|
329 | else
|
---|
330 | return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Protocol\" string failed"), iInstance);
|
---|
331 |
|
---|
332 | /* host port */
|
---|
333 | int32_t iHostPort;
|
---|
334 | rc = CFGMR3QueryS32(pNode, "HostPort", &iHostPort);
|
---|
335 | if (VBOX_FAILURE(rc))
|
---|
336 | return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"HostPort\" integer failed"), iInstance);
|
---|
337 |
|
---|
338 | /* guest port */
|
---|
339 | int32_t iGuestPort;
|
---|
340 | rc = CFGMR3QueryS32(pNode, "GuestPort", &iGuestPort);
|
---|
341 | if (VBOX_FAILURE(rc))
|
---|
342 | return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestPort\" integer failed"), iInstance);
|
---|
343 |
|
---|
344 | /* guest address */
|
---|
345 | char szGuestIP[32];
|
---|
346 | rc = CFGMR3QueryString(pNode, "GuestIP", &szGuestIP[0], sizeof(szGuestIP));
|
---|
347 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
348 | RTStrPrintf(szGuestIP, sizeof(szGuestIP), "%d.%d.%d.%d",
|
---|
349 | (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, (Network & 0xE0) | 15);
|
---|
350 | else if (VBOX_FAILURE(rc))
|
---|
351 | return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestIP\" string failed"), iInstance);
|
---|
352 | struct in_addr GuestIP;
|
---|
353 | if (!inet_aton(szGuestIP, &GuestIP))
|
---|
354 | return PDMDrvHlpVMSetError(pData->pDrvIns, VERR_NAT_REDIR_GUEST_IP, RT_SRC_POS, N_("NAT#%d: configuration error: invalid \"GuestIP\"=\"%s\", inet_aton failed"), iInstance, szGuestIP);
|
---|
355 |
|
---|
356 | /*
|
---|
357 | * Call slirp about it.
|
---|
358 | */
|
---|
359 | Log(("drvNATConstruct: Redir %d -> %s:%d\n", iHostPort, szGuestIP, iGuestPort));
|
---|
360 | if (slirp_redir(pData->pNATState, fUDP, iHostPort, GuestIP, iGuestPort) < 0)
|
---|
361 | return PDMDrvHlpVMSetError(pData->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS, N_("NAT#%d: configuration error: failed to set up redirection of %d to %s:%d. Probably a conflict with existing services or other rules"), iInstance, iHostPort, szGuestIP, iGuestPort);
|
---|
362 | } /* for each redir rule */
|
---|
363 |
|
---|
364 | return VINF_SUCCESS;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Get the MAC address into the slirp stack.
|
---|
369 | */
|
---|
370 | static void drvNATSetMac(PDRVNAT pData)
|
---|
371 | {
|
---|
372 | if (pData->pConfig)
|
---|
373 | {
|
---|
374 | PDMMAC Mac;
|
---|
375 | pData->pConfig->pfnGetMac(pData->pConfig, &Mac);
|
---|
376 | slirp_set_ethaddr(pData->pNATState, Mac.au8);
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * After loading we have to pass the MAC address of the ethernet device to the slirp stack.
|
---|
383 | * Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
|
---|
384 | * (usually done during guest boot).
|
---|
385 | */
|
---|
386 | static DECLCALLBACK(int) drvNATLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSMHandle)
|
---|
387 | {
|
---|
388 | PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
|
---|
389 | drvNATSetMac(pData);
|
---|
390 | return VINF_SUCCESS;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Some guests might not use DHCP to retrieve an IP but use a static IP.
|
---|
396 | */
|
---|
397 | static DECLCALLBACK(void) drvNATPowerOn(PPDMDRVINS pDrvIns)
|
---|
398 | {
|
---|
399 | PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
|
---|
400 | drvNATSetMac(pData);
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Construct a NAT network transport driver instance.
|
---|
406 | *
|
---|
407 | * @returns VBox status.
|
---|
408 | * @param pDrvIns The driver instance data.
|
---|
409 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
410 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
411 | * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
|
---|
412 | * iInstance it's expected to be used a bit in this function.
|
---|
413 | */
|
---|
414 | static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
415 | {
|
---|
416 | PDRVNAT pData = PDMINS2DATA(pDrvIns, PDRVNAT);
|
---|
417 | char szNetAddr[16];
|
---|
418 | char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
|
---|
419 | LogFlow(("drvNATConstruct:\n"));
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * Validate the config.
|
---|
423 | */
|
---|
424 | if (!CFGMR3AreValuesValid(pCfgHandle, "PassDomain\0TFTPPrefix\0BootFile\0Network\0"))
|
---|
425 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown NAT configuration option, only supports PassDomain, TFTPPrefix, BootFile and Network"));
|
---|
426 |
|
---|
427 | /*
|
---|
428 | * Init the static parts.
|
---|
429 | */
|
---|
430 | pData->pDrvIns = pDrvIns;
|
---|
431 | pData->pNATState = NULL;
|
---|
432 | pData->pszTFTPPrefix = NULL;
|
---|
433 | pData->pszBootFile = NULL;
|
---|
434 | /* IBase */
|
---|
435 | pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
|
---|
436 | /* INetwork */
|
---|
437 | pData->INetworkConnector.pfnSend = drvNATSend;
|
---|
438 | pData->INetworkConnector.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
|
---|
439 | pData->INetworkConnector.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * Get the configuration settings.
|
---|
443 | */
|
---|
444 | bool fPassDomain = true;
|
---|
445 | int rc = CFGMR3QueryBool(pCfgHandle, "PassDomain", &fPassDomain);
|
---|
446 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
447 | fPassDomain = true;
|
---|
448 | else if (VBOX_FAILURE(rc))
|
---|
449 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"PassDomain\" boolean failed"), pDrvIns->iInstance);
|
---|
450 |
|
---|
451 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "TFTPPrefix", &pData->pszTFTPPrefix);
|
---|
452 | if (VBOX_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
453 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"TFTPPrefix\" string failed"), pDrvIns->iInstance);
|
---|
454 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "BootFile", &pData->pszBootFile);
|
---|
455 | if (VBOX_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
456 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"BootFile\" string failed"), pDrvIns->iInstance);
|
---|
457 |
|
---|
458 | /*
|
---|
459 | * Query the network port interface.
|
---|
460 | */
|
---|
461 | pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
|
---|
462 | if (!pData->pPort)
|
---|
463 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
464 | N_("Configuration error: the above device/driver didn't export the network port interface"));
|
---|
465 | pData->pConfig = (PPDMINETWORKCONFIG)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_CONFIG);
|
---|
466 | if (!pData->pConfig)
|
---|
467 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
468 | N_("Configuration error: the above device/driver didn't export the network config interface"));
|
---|
469 |
|
---|
470 | /* Generate a network address for this network card. */
|
---|
471 | rc = CFGMR3QueryString(pCfgHandle, "Network", szNetwork, sizeof(szNetwork));
|
---|
472 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
473 | RTStrPrintf(szNetwork, sizeof(szNetwork), "10.0.%d.0/24", pDrvIns->iInstance + 2);
|
---|
474 | else if (VBOX_FAILURE(rc))
|
---|
475 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Network\" string failed"), pDrvIns->iInstance);
|
---|
476 |
|
---|
477 | RTIPV4ADDR Network;
|
---|
478 | RTIPV4ADDR Netmask;
|
---|
479 | rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
|
---|
480 | if (RT_FAILURE(rc))
|
---|
481 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: network '%s' describes not a valid IPv4 network"), pDrvIns->iInstance, szNetwork);
|
---|
482 |
|
---|
483 | RTStrPrintf(szNetAddr, sizeof(szNetAddr), "%d.%d.%d.%d",
|
---|
484 | (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, Network & 0xFF);
|
---|
485 |
|
---|
486 | /*
|
---|
487 | * The slirp lock..
|
---|
488 | */
|
---|
489 | rc = RTCritSectInit(&pData->CritSect);
|
---|
490 | if (VBOX_FAILURE(rc))
|
---|
491 | return rc;
|
---|
492 | #if 0
|
---|
493 | rc = RTSemEventCreate(&g_EventSem);
|
---|
494 | if (VBOX_SUCCESS(rc))
|
---|
495 | {
|
---|
496 | /*
|
---|
497 | * Start the select thread. (it'll block on the sem)
|
---|
498 | */
|
---|
499 | g_fThreadTerm = false;
|
---|
500 | rc = RTThreadCreate(&g_ThreadSelect, drvNATSelectThread, 0, NULL, "NATSEL");
|
---|
501 | if (VBOX_SUCCESS(rc))
|
---|
502 | {
|
---|
503 | #endif
|
---|
504 | /*
|
---|
505 | * Initialize slirp.
|
---|
506 | */
|
---|
507 | rc = slirp_init(&pData->pNATState, &szNetAddr[0], Netmask, fPassDomain, pData->pszTFTPPrefix, pData->pszBootFile, pData);
|
---|
508 | if (VBOX_SUCCESS(rc))
|
---|
509 | {
|
---|
510 | int rc2 = drvNATConstructRedir(pDrvIns->iInstance, pData, pCfgHandle, Network);
|
---|
511 | if (VBOX_SUCCESS(rc2))
|
---|
512 | {
|
---|
513 | /*
|
---|
514 | * Register a load done notification to get the MAC address into the slirp
|
---|
515 | * engine after we loaded a guest state.
|
---|
516 | */
|
---|
517 | rc2 = PDMDrvHlpSSMRegister(pDrvIns, pDrvIns->pDrvReg->szDriverName,
|
---|
518 | pDrvIns->iInstance, 0, 0,
|
---|
519 | NULL, NULL, NULL, NULL, NULL, drvNATLoadDone);
|
---|
520 | AssertRC(rc2);
|
---|
521 | pDrvIns->pDrvHlp->pfnPDMPollerRegister(pDrvIns, drvNATPoller);
|
---|
522 |
|
---|
523 | pData->enmLinkState = PDMNETWORKLINKSTATE_UP;
|
---|
524 | #if 0
|
---|
525 | RTSemEventSignal(g_EventSem);
|
---|
526 | RTThreadSleep(0);
|
---|
527 | #endif
|
---|
528 | /* might return VINF_NAT_DNS */
|
---|
529 | return rc;
|
---|
530 | }
|
---|
531 | /* failure path */
|
---|
532 | rc = rc2;
|
---|
533 | slirp_term(pData->pNATState);
|
---|
534 | pData->pNATState = NULL;
|
---|
535 | }
|
---|
536 | else
|
---|
537 | {
|
---|
538 | PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
|
---|
539 | AssertMsgFailed(("Add error message for rc=%d (%Vrc)\n", rc, rc));
|
---|
540 | }
|
---|
541 | #if 0
|
---|
542 | g_fThreadTerm = true;
|
---|
543 | RTSemEventSignal(g_EventSem);
|
---|
544 | RTThreadSleep(0);
|
---|
545 | }
|
---|
546 | RTSemEventDestroy(g_EventSem);
|
---|
547 | g_EventSem = NULL;
|
---|
548 | }
|
---|
549 | #endif
|
---|
550 | RTCritSectDelete(&pData->CritSect);
|
---|
551 | return rc;
|
---|
552 | }
|
---|
553 |
|
---|
554 |
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * NAT network transport driver registration record.
|
---|
558 | */
|
---|
559 | const PDMDRVREG g_DrvNAT =
|
---|
560 | {
|
---|
561 | /* u32Version */
|
---|
562 | PDM_DRVREG_VERSION,
|
---|
563 | /* szDriverName */
|
---|
564 | "NAT",
|
---|
565 | /* pszDescription */
|
---|
566 | "NAT Network Transport Driver",
|
---|
567 | /* fFlags */
|
---|
568 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
569 | /* fClass. */
|
---|
570 | PDM_DRVREG_CLASS_NETWORK,
|
---|
571 | /* cMaxInstances */
|
---|
572 | 16,
|
---|
573 | /* cbInstance */
|
---|
574 | sizeof(DRVNAT),
|
---|
575 | /* pfnConstruct */
|
---|
576 | drvNATConstruct,
|
---|
577 | /* pfnDestruct */
|
---|
578 | drvNATDestruct,
|
---|
579 | /* pfnIOCtl */
|
---|
580 | NULL,
|
---|
581 | /* pfnPowerOn */
|
---|
582 | drvNATPowerOn,
|
---|
583 | /* pfnReset */
|
---|
584 | NULL,
|
---|
585 | /* pfnSuspend */
|
---|
586 | NULL,
|
---|
587 | /* pfnResume */
|
---|
588 | NULL,
|
---|
589 | /* pfnDetach */
|
---|
590 | NULL,
|
---|
591 | /* pfnPowerOff */
|
---|
592 | NULL
|
---|
593 | };
|
---|