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